diff --git a/DESIGN.md b/DESIGN.md index 0167535b8..0848a37e6 100644 --- a/DESIGN.md +++ b/DESIGN.md @@ -446,6 +446,15 @@ The proof is staged so each level is useful by itself and later levels build on - VCPU / EPT (the extended spec `Fl4Spec/Ext/Virt*`), on the joint state `VmEptState` (P3's relation with the VCPU list, the VCPU images and the EPT tables): retype of all 19 object types, the decoded untyped invocation including reset; the EPT syscall suite through FL4's dispatcher (`TCBSetEPTRoot`, EPT table map and unmap, `X86PageMapEPT`, the unmap of an EPT-mapped frame, `X86ASIDPoolAssign` of an EPT root); the VCPU report and detach chain; the IO-port cap's owner (`ArchCap.ioPortCap` carries it, decision Q2) and its revoke, finalisation and delete; a final thread cap's finalisation (VCPU detach, then the native tail); and FL4's `delete(slot, true)` against `capDeleteVF` for every cap (`VmEptState.capDelete`, the deletion loop with zombies, swaps and preemption); and every CNode invocation (labels 25–32: Revoke, Delete, CancelBadgedSends, Copy, Mint, Move, Mutate, Rotate) through FL4's dispatcher against `invokeCnodeVF` (`VmEptState.cnodeDecodeAll`): an error leaves the state unchanged, an `Ok` outcome is back at `VmEptState`, a preempted Revoke or Delete keeps the VM side's pre-state facts (`CnAllPostVE`). - x86 VM Stage 2 reaches kernel entry. `VmEptState.decodeDisp` assembles the proved VM/EPT and common capability arms. The entry uses `VmEptState.dispatchAt`, which discharges the VCPU, EPT-table, EPT-mapped frame and owned I/O-port cap arms and takes the native-capability cases as a premise (`VDispNativeAt`). CNode, untyped, EPT-root assignment and SetEPTRoot have separate decode theorems, and their native-capability routing at entry is covered by that premise. `VmEptState.handleInvocationE` and `afterV` carry the invocation outcomes and preemption through the VM relation. `VmEptState.kernel_entry` composes the native syscall ABI paths with the dispatcher and VM-aware finish. The separate `vmEnterEntry`, `unknownSyscallEntry`, `trapSyscallEntry`, `interruptEntry`, `userExceptionEntry`, `vmFaultEntry`, `vcpuExitStalled` and `vcpuExitUnstalled` cover the other x86 core entry arms. They start from `VmEptState` and return a related extended-spec outcome at `VEAny`. The finish includes awakening, scheduling, activation and checking the current VCPU. - Stage 2's [S] scope is the serial core entry after the gate, with one scheduling domain and the single-core IRQ view. Host interrupts use the native fresh-read `IrqHw` contract and `ValidIrqMasks`. Both fresh host and saved guest-exit vectors exclude remote-call IPI 126 and reschedule IPI 127. `VirtExitIrqF` covers an absent saved vector, active and inactive device lines, and saved non-IPI vectors above the device range. The trusted boundary includes entry/exit assembly, hardware guest execution, and the captured vector and VCPU image. Native syscall numbers are −11 through −1 with the trap flag clear. VMEnter (−23) requires a clear trap flag, an agreed enabled backend, and a bound VCPU. The ordinary unknown syscall theorem uses `UnkSys` (out-of-range number or set trap flag), `NoDbgSys` (excludes debug/TLS calls and requires a disabled backend or lack of a VCPU binding for −23), and the spec's ordinary-fault selection guard. Trap vectors are below 256 and follow FL4's zero-extended payload convention. The joint-state, native-capability, IPC and finish premises are listed in the entry and dispatcher modules. + - AArch64 P3's delete finaliser hooks for Notification, Endpoint and IrqHandler are [S] scoped to `numNodes = 1` (FL4's `possible_switch_to` branches on the affinity and the current core, the spec's is single-core); under it the waiters' locality follows from the per-step record. The Reply and thread / SchedContext hooks, not yet proved, take the same scope. + - AArch64 P3's Reply to a faulted caller (`JSyscallA.replyFaultA`, `P3/Stage3ReplyFaultArmA`; spec `fl4HandleFaultReply`) is [S] scoped to: an `UnknownSyscall` reply shorter than `TRAP_FULL_LENGTH` (37 words), or a fault not sent in the long form (`fault_w1 = 0`, as for every thread with `TRAP_SYSCALLS` clear): the long form's extra registers (x8–x30, TPIDR_EL0) are not in the spec; a caller whose timeout handler is not an endpoint cap; `numNodes = 1`. + - AArch64 P3 Stage 3 (`proofs/Fl4ProofsA64/P3/Stage3*`), proved: the syscall kernel-entry top `kernelEntry_recGA` / `kernelEntry_stage3A` (ghost sets float existentially in the exit record `A64RelS3`), over the dispatch groups CNode, VSpace, SchedContext, TCB, IPC, IRQ, flush, reply, domain, error and VCPU (Untyped: no constructor yet; see the scope-limit bullet) (labels 60–69, at the body and dispatch level: `VcpuScopeA`); the receive family and the combined syscalls (`kernelEntryComb_recGA`, `syscallComb_refinesA`); the preempted exit without the callers' `hPre` premise (`hPre_ofA`, from `HPreEnvA`: `hX` and `T` are derived from the record, and the `Clock` the delete/revoke loop carries to its `Preempted` exit is threaded through `DispPostRA`); and the VM-exit entries end to end (`kernelEntry_runExitA`, `kernelEntry_runVppiA`). The blocking receive with a reply object is proved for a reply object linked to another thread as well (case B of `take_reply`: the link is cancelled first, `Stage3RecvPushCancelA`), so the receive scope no longer assumes an unlinked reply object. The IRQ-control decode's node count is a record fact (`NdOKA.nodes`: the kernel-state word `NUM_NODES` equals the platform's `numNodes`, and every write set keeps it), not a [P] premise over store memory (`IrqPlatA.nodes` is gone). TCB SetIPCBuffer is proved for a zero address (`JSyscallA.setIpcBufZeroA`) and a non-zero one (`JSyscallA.setIpcBufNzA`, `P3/Stage3LiftTcbBufNzA`) on any target thread (the current thread included, with its `reschedule_required`) from a non-device Frame source; a device-frame source or a misaligned or invalid buffer is a decode error, covered by `DecErrSpecPA.thread` (`decErrSpecA`, `sendBodyErrAllA`). + - AArch64 P3 Stage 3, dispatch scope limits ([S]): the scope premise of the syscall tops (`hArm`) holds only for the invocations of `DispScopeA`. A decode that succeeds outside it is not covered, so the tops say nothing about: Untyped Retype / RetypeShared (`UtScopeA` has no constructor yet; first in line for the next stage); TCB Configure, SetSpace, ReadRegisters, WriteRegisters, CopyRegisters, SetTimer, SetHighVSpace, the breakpoint labels and single stepping; SetSchedParams with a changed SC or another handler cap kind; DomainScheduleConfigure / SetStart; ARMASIDControlMakePool; ARMPageDeriveSubframe; the Stage-2 frame map, the page-table map into a high window or Stage-2, and the T2 unmap shrink; Notification send to a bound thread running a VCPU. Their decode `Err` returns are covered (`decErrSpecA`, `ErrScopeA`). + - AArch64 P3 Stage 3, architecture switch (`ArchSwFA`, `P3/ScArchSwA`): the `KERNEL_ROOT` word of the kernel-state block is a record fact (`NdOKA.kroot`: it names the spec's global user VSpace `armUsGlobalVspace`, kept by every write set), and `execIdle` / `execThread` take it, the no-high-window-at-address-0 fact, the run links (`RunLinkA`) and "the current core is core 0" from the record (`ArchTieA`, `JStage3A.archRestA`) instead of quantifying over every store. `ArchSwFA` is therefore single-core (`c = 0`, from `numNodes = 1`): its multicore form belongs to the SMP stage, which must restate the per-core root views. The [P] has a Lean inhabitant for the model architecture: `witArchSwOf : ArchSwFA st AR1` (`P3/A64ArchSwH`, with `P3/A64ArchSwA`–`G`) proves `set_vm_root` and `set_kernel_vm_root` against `threadRootWritesA` / `kernelRootWritesA` on core 0, so the `ArchSwFA` premise is satisfiable. + - AArch64 P3 Stage 3, the scheduler premise of the tops (`hGap`, `SchedGapFreeA` at the body's reached record, `P3/Stage3SysTopA`): [S] `oneDomain`, [P] `minAgree` (`fl4MinBudget = MIN_BUDGET`), [P] `ticks` (`ScCfgTimeAgreeA`), [J] `timer` (the schedule-timer inputs and `SniHypA` after `switch_sched_context`, guarded by the run that reaches it: awaken, select the action, switch). + - AArch64 P3 Stage 3 non-vacuity (mechanized): `Wit.wit_send_nonvacuous` (`P3/A64Wit2TopA`) shows that the premises of `kernelEntrySend_recGA` are jointly satisfiable. It exhibits, for a concrete model architecture (`P3/A64Wit2ArchA`) and the w1 boot state, a store, an arch state, a spec state and the ghost sets satisfying every premise of the theorem (`SendEntryHyps` is the theorem's binder list; `wit_top_send` also derives its conclusion there), with `witArchSwOf` as the `ArchSwFA`. Everything is existential except one hypothesis, `OpaqueWA` (witness-world [P]; it is not a premise of any top): facts about `opaque` spec constants that no Lean term proves or refutes (any value of the constant's type is a model; `P3/A64Wit2PlatA` gives the model values): one domain, `maxIRQ = 5375`, GICv2, `numNodes = 1`, `maxTicksToUs = U64::MAX`, kernel WCET 10 µs, 1000 ticks per timer unit, timer unit 1, a 10^9 Hz counter, `fl4MaxPeriodUs = MAX_PERIOD_US`, a monotone raw counter, and two facts that are properties of the witness world rather than of FL4's platform: `irq` (the IRQ oracle only yields the VGIC maintenance line, a non-kernel IRQ) and `hNow` (the spec's clock and the raw counter agree at a state whose last observation is the next raw sample; both oracles are `0`). The witness also discharges `hGap` (`wit2b_hGapA`, proved at any record state). Only the Send / NBSend / Call top has a mechanized witness; the Recv, Comb and VM-exit tops share its platform premises but are not instantiated yet. + - AArch64 P3 Stage 3, declared Stage-4 limits ([S4]): the guest-resume `finish` (`hNative` / `hNat`: an `ARMVCPU_Run` that resumes the guest, so the caller stays current in `runningVM`, is outside the top), and the guest entries `vgic`, `vppi`, `vcpuExit` and `vcpuFault`, which are deferred to the S4 guest-lifecycle follow-up. + - AArch64 P3 Stage 3, still open (work in progress, not limits): the last premise duplication in the top assembly (the interrupt entry's IRQ set against the preempted exit's, once the interrupt top exists). The preempted exit takes its platform facts from the entry's `FaultEnvA` (`ExitPlatA`), its timeout-send premise (`SendPA`) is guarded by the run that reaches the preempted state, and the polled line's `reserved` clause is a [P] clause of `PollAgreeA` (fl4-kernel#154). - x86 VM Stage 3 replaces Stage 2's arm premises. `VEAnyX.outerS3` is one theorem over the nine concrete entries (`S3Ev`), from `VEAnyX` and the event's `S3Pre` to an extended-spec outcome at `VEAnyX`. Its syscall case, `VmEptState.kernel_entryS3`, takes the start record `StartS3` in place of `KEntryArmsV`, `VDispNativeAt` and the finish premises; the other cases take no `EventFinishVE` / `VcpuExitFinishVE`. The Stage 2 theorems are kept. The extended spec's FrameBatch (`performFrameBatchWith`) is parameterized by the deletion it uses, and the VM-aware step uses `capDeleteVF`. - Stage 3's remaining premises. [J] `StartS3`: `X : SInvXV` (the native carried bundle without `NativeScopeS`), current thread not idle, `SpecIoPorts`, `SpecUtZ`, `DevRangeS`, `GpmOutV`, `UtInv''`, `RV`, `IdleVtS`; `S3Fault` and the scheduler, domain, FPU-owner, idle and clock facts of the other events. [P] `StartS3.air` (`ArchIrqRel`), the platform arguments of `outerS3` (including `FpuImplDet`, `FpuWriteDet`, `StoreExt`) and the hardware arguments of each event. [S] `StartS3.core` (`SelectedCoreScope` for the invoked capability of the current thread on the executing core: SchedControl names that core, Untyped only on core 0), and Stage 2's event guards above. - Stage 3 non-vacuity: `JBootQ.VWit.witness_schedControl` and `witness_untyped` (`P3/VmWitS3`) give a concrete store, arch and spec state satisfying `VEAnyX ∧ S3Pre` for `SysCall` on a SchedControl cap and `SysNBSendWait` on an Untyped cap, with a `runningVM` TCB and its VCPU in the CSpace. The quantified fields `air` and `core` are not vacuous there: a dispatch pair exists (`w_dispatch`: the `FaultIP` write and a passing `budget_ok` on the FL4 side, a passing budget check on the spec side), and at it the invoked slot is looked up and decodes to the SchedControl / Untyped cap and `SelectedCoreScope` holds (`w_core_exercised`). The witness clock sits at the cap of `getCurrentTime` (`tW = -(getCurrentTime_buffer + 1)`), so the entry's time read is `tW` whatever the opaque `timeOracle` gives and the consumed time is 0; with the clock at 0 the budget check would depend on `timeOracle 1`. `air` asks for at least one IOAPIC (`ArchIrqRel`, as real x86): the witness arch `warV` is `warI1` (one IOAPIC of 24 pins, as the spec's `init_arch_state`) with the platform's `virtualization` code and VMX capability words. Premises: [P] `virtKind = .vmx ∨ .svm` (a running guest forces a backend) and [P] one scheduling domain. [J] `SInvXV` is an entry premise, as `SInvX` was; the witness satisfies it. diff --git a/README.md b/README.md index 2ab1e4783..79676e1be 100644 --- a/README.md +++ b/README.md @@ -248,7 +248,13 @@ guest's hardware writes included, so the runtime's executions from `w0` / `w1` are serializable (`w0_serializable_impl`, `w1_serializable_impl`), from four memory-model and IPI premises. `cargo xtask gate` checks the x86_64 extraction; the AArch64 one is extracted and checked on its own -(`lake build Fl4ProofsA64`). P3 also covers the x86_64 VCPU/EPT +(`lake build Fl4ProofsA64`). AArch64 P3 Stage 3 proves the syscall +kernel-entry top (`kernelEntry_recGA`), the receive family and combined +syscalls, the preempted exit and the VM-exit entries `runExit` / `runVppi`; +the guest-resume `finish` and the other guest entries are declared Stage-4 +limits, `ArchSwFA` is single-core (the SMP stage restates it per core), and +the premises of the Send top are shown jointly satisfiable (`wit_send_nonvacuous`, +under the witness-world hypothesis `OpaqueWA`) (DESIGN.md §9). P3 also covers the x86_64 VCPU/EPT extension on a joint VM state (`VmEptState`): retype and untyped, the EPT syscalls through the dispatcher, the VCPU report and detach, the IO-port cap's owner, revoke and delete, capability delete of every cap diff --git a/proofs/Fl4ProofsA64.lean b/proofs/Fl4ProofsA64.lean index 90cfdcb13..8696b012b 100644 --- a/proofs/Fl4ProofsA64.lean +++ b/proofs/Fl4ProofsA64.lean @@ -318,6 +318,65 @@ import Fl4ProofsA64.P1.MdbLiveM import Fl4ProofsA64.P1.MdbLiveP import Fl4ProofsA64.P1.MdbLiveQ import Fl4ProofsA64.P1.MdbLiveW +import Fl4ProofsA64.P1.MdbLiveX +import Fl4ProofsA64.P1.MdbLiveY +import Fl4ProofsA64.P1.MdbVcCap +import Fl4ProofsA64.P1.MdbPacL +import Fl4ProofsA64.P1.MdbRegL +import Fl4ProofsA64.P1.MdbVcRdL +import Fl4ProofsA64.P1.MdbFpuL +import Fl4ProofsA64.P1.MdbDfM +import Fl4ProofsA64.P1.MdbDomQ +import Fl4ProofsA64.P1.MdbRegRL +import Fl4ProofsA64.P1.MdbDelIn +import Fl4ProofsA64.P1.MdbInsL +import Fl4ProofsA64.P1.MdbTcbCapL +import Fl4ProofsA64.P1.MdbInsJ +import Fl4ProofsA64.P1.MdbSetSpL +import Fl4ProofsA64.P1.MdbEptS +import Fl4ProofsA64.P1.MdbIpcAl +import Fl4ProofsA64.P1.MdbIpcRR +import Fl4ProofsA64.P1.MdbFrUb +import Fl4ProofsA64.P1.MdbIpcBuf +import Fl4ProofsA64.P1.MdbIrqC +import Fl4ProofsA64.P1.MdbEptRL +import Fl4ProofsA64.P1.MdbSigL +import Fl4ProofsA64.P1.MdbIrqH +import Fl4ProofsA64.P1.MdbAsidA +import Fl4ProofsA64.P1.MdbXferL +import Fl4ProofsA64.P1.MdbSendL +import Fl4ProofsA64.P1.MdbSendR +import Fl4ProofsA64.P1.MdbSendD +import Fl4ProofsA64.P1.MdbSendB +import Fl4ProofsA64.P1.MdbSendC +import Fl4ProofsA64.P1.MdbSendE +import Fl4ProofsA64.P1.MdbDropR +import Fl4ProofsA64.P1.MdbPoolM +import Fl4ProofsA64.P1.MdbPoolG +import Fl4ProofsA64.P1.MdbPoolI +import Fl4ProofsA64.P1.MdbPoolT +import Fl4ProofsA64.P1.MdbPoolN +import Fl4ProofsA64.P1.MdbPoolA +import Fl4ProofsA64.P1.S1aPlace +import Fl4ProofsA64.P1.MdbArmQ +import Fl4ProofsA64.P1.MdbPoolDel +import Fl4ProofsA64.P1.S1aKeep +import Fl4ProofsA64.P1.S1aRetype +import Fl4ProofsA64.P1.MdbPoolIdx +import Fl4ProofsA64.P1.S1aUser +import Fl4ProofsA64.P1.S1aKsGlob +import Fl4ProofsA64.P1.S1aNonUser +import Fl4ProofsA64.P1.S1aVcH +import Fl4ProofsA64.P1.S1aKsBr +import Fl4ProofsA64.P1.S1aTcbRt +import Fl4ProofsA64.P1.S1aPoolOff +import Fl4ProofsA64.P1.S1aBoot +import Fl4ProofsA64.P1.S1aUbRt +import Fl4ProofsA64.P1.S1aPoolRt +import Fl4ProofsA64.P1.MdbCnArmQ +import Fl4ProofsA64.P1.S1aFrW +import Fl4ProofsA64.P1.BootWit +import Fl4ProofsA64.P1.BootBxi import Fl4ProofsA64.P1.FastVmLocal import Fl4ProofsA64.P2.OwnMap import Fl4ProofsA64.P2.E2E @@ -351,6 +410,23 @@ import Fl4ProofsA64.P2.AccessVmTail import Fl4ProofsA64.P2.MoveSl import Fl4ProofsA64.P2.AccessVmMv import Fl4ProofsA64.P2.AccessVmObl +import Fl4ProofsA64.P1.LocalVmTimer +import Fl4ProofsA64.P1.LocalSys +import Fl4ProofsA64.P1.LocalInv +import Fl4ProofsA64.P1.FastRR +import Fl4ProofsA64.P1.FastRRSum +import Fl4ProofsA64.P1.FastRRSync +import Fl4ProofsA64.P1.FastRRSame +import Fl4ProofsA64.P1.LocalRRVmAcc +import Fl4ProofsA64.P1.LocalRRVm +import Fl4ProofsA64.P1.RRFootBase +import Fl4ProofsA64.P1.FastRREnt +import Fl4ProofsA64.P1.FastRRFoot +import Fl4ProofsA64.P1.FastRRLk +import Fl4ProofsA64.P1.FastTailNvm +import Fl4ProofsA64.P1.FastTailRR +import Fl4ProofsA64.P1.LocalRRVmInv +import Fl4ProofsA64.P1.LocalInvDec import Fl4ProofsA64.P1.LocalTimer import Fl4ProofsA64.P1.LocalTimerSet import Fl4ProofsA64.P1.LocalNbw @@ -447,22 +523,6 @@ import Fl4ProofsA64.P2.VcpuLayJ import Fl4ProofsA64.P2.VexitAAcc import Fl4ProofsA64.P2.VexitAFin import Fl4ProofsA64.P2.VexitASuf -import Fl4ProofsA64.P1.LocalVmTimer -import Fl4ProofsA64.P1.LocalSys -import Fl4ProofsA64.P1.LocalInv -import Fl4ProofsA64.P1.FastRR -import Fl4ProofsA64.P1.FastRRSum -import Fl4ProofsA64.P1.FastRRSync -import Fl4ProofsA64.P1.FastRRSame -import Fl4ProofsA64.P1.LocalRRVmAcc -import Fl4ProofsA64.P1.LocalRRVm -import Fl4ProofsA64.P1.RRFootBase -import Fl4ProofsA64.P1.FastRREnt -import Fl4ProofsA64.P1.FastRRFoot -import Fl4ProofsA64.P1.FastRRLk -import Fl4ProofsA64.P1.FastTailNvm -import Fl4ProofsA64.P1.FastTailRR -import Fl4ProofsA64.P1.LocalRRVmInv import Fl4ProofsA64.P2.ProgGenSy0 import Fl4ProofsA64.P2.ProgGenSy1 import Fl4ProofsA64.P2.ProgGenSy2 @@ -554,7 +614,6 @@ import Fl4ProofsA64.P2.VexitAHvcR import Fl4ProofsA64.P2.VexitALP import Fl4ProofsA64.P1.QueueUserWr import Fl4ProofsA64.P2.VexitAUserWr -import Fl4ProofsA64.P1.LocalInvDec import Fl4ProofsA64.P2.OwnVInstA import Fl4ProofsA64.P2.E2EFl4VX import Fl4ProofsA64.P2.E2EFl4VXS @@ -808,66 +867,7 @@ import Fl4ProofsA64.P2.VtMv import Fl4ProofsA64.P2.VtLP import Fl4ProofsA64.P2.CutVtA import Fl4ProofsA64.P2.GateAgA -import Fl4ProofsA64.P1.MdbLiveX -import Fl4ProofsA64.P1.MdbLiveY -import Fl4ProofsA64.P1.MdbVcCap -import Fl4ProofsA64.P1.MdbPacL -import Fl4ProofsA64.P1.MdbRegL -import Fl4ProofsA64.P1.MdbVcRdL -import Fl4ProofsA64.P1.MdbFpuL -import Fl4ProofsA64.P1.MdbDfM -import Fl4ProofsA64.P1.MdbDomQ -import Fl4ProofsA64.P1.MdbRegRL -import Fl4ProofsA64.P1.MdbDelIn -import Fl4ProofsA64.P1.MdbInsL -import Fl4ProofsA64.P1.MdbTcbCapL -import Fl4ProofsA64.P1.MdbInsJ -import Fl4ProofsA64.P1.MdbSetSpL -import Fl4ProofsA64.P1.MdbEptS -import Fl4ProofsA64.P1.MdbIpcAl -import Fl4ProofsA64.P1.MdbIpcRR -import Fl4ProofsA64.P1.MdbFrUb -import Fl4ProofsA64.P1.MdbIpcBuf -import Fl4ProofsA64.P1.MdbIrqC -import Fl4ProofsA64.P1.MdbEptRL -import Fl4ProofsA64.P1.MdbSigL -import Fl4ProofsA64.P1.MdbIrqH import Fl4ProofsA64.P1.MdbSetCapG -import Fl4ProofsA64.P1.MdbAsidA -import Fl4ProofsA64.P1.MdbXferL -import Fl4ProofsA64.P1.MdbSendL -import Fl4ProofsA64.P1.MdbSendR -import Fl4ProofsA64.P1.MdbSendD -import Fl4ProofsA64.P1.MdbSendB -import Fl4ProofsA64.P1.MdbSendC -import Fl4ProofsA64.P1.MdbSendE -import Fl4ProofsA64.P1.MdbDropR -import Fl4ProofsA64.P1.MdbPoolM -import Fl4ProofsA64.P1.MdbPoolG -import Fl4ProofsA64.P1.MdbPoolI -import Fl4ProofsA64.P1.MdbPoolT -import Fl4ProofsA64.P1.MdbPoolN -import Fl4ProofsA64.P1.MdbPoolA -import Fl4ProofsA64.P1.S1aPlace -import Fl4ProofsA64.P1.MdbArmQ -import Fl4ProofsA64.P1.MdbPoolDel -import Fl4ProofsA64.P1.S1aKeep -import Fl4ProofsA64.P1.S1aRetype -import Fl4ProofsA64.P1.MdbPoolIdx -import Fl4ProofsA64.P1.S1aUser -import Fl4ProofsA64.P1.S1aKsGlob -import Fl4ProofsA64.P1.S1aNonUser -import Fl4ProofsA64.P1.S1aVcH -import Fl4ProofsA64.P1.S1aKsBr -import Fl4ProofsA64.P1.S1aTcbRt -import Fl4ProofsA64.P1.S1aPoolOff -import Fl4ProofsA64.P1.S1aBoot -import Fl4ProofsA64.P1.S1aUbRt -import Fl4ProofsA64.P1.S1aPoolRt -import Fl4ProofsA64.P1.MdbCnArmQ -import Fl4ProofsA64.P1.S1aFrW -import Fl4ProofsA64.P1.BootWit -import Fl4ProofsA64.P1.BootBxi import Fl4ProofsA64.P2.BootEpA import Fl4ProofsA64.P1.BootW1 import Fl4ProofsA64.P1.BootW1Bxi @@ -890,6 +890,42 @@ import Fl4ProofsA64.P1.MdbArmDfB import Fl4ProofsA64.P1.MdbArmDm import Fl4ProofsA64.P1.S1aArmCol import Fl4ProofsA64.P1.S1aBootV +-- P3 for the AArch64 extraction against Fl4SpecA64 (A64-P3 S3 gens 1–6): the joint relation, its boot +-- witness at A64-Boot's w0, the entry-level combination and the frame lemmas. +import Fl4ProofsA64.P3.CapRelA +import Fl4ProofsA64.P3.StateRelA +import Fl4ProofsA64.P3.JointA +import Fl4ProofsA64.P3.SInvXA +import Fl4ProofsA64.P3.JBootA +import Fl4ProofsA64.P3.JBootW0A +import Fl4ProofsA64.P3.FrameA +import Fl4ProofsA64.P3.EntryA +import Fl4ProofsA64.P3.SpecFrA +import Fl4ProofsA64.P3.EmptyA +import Fl4ProofsA64.P3.SlotJA +import Fl4ProofsA64.P3.MoveA +import Fl4ProofsA64.P3.SwapA +import Fl4ProofsA64.P3.InsertA +import Fl4ProofsA64.P3.JEntW0A +import Fl4ProofsA64.P3.JBootW1A +import Fl4ProofsA64.P3.JBootW1SA +import Fl4ProofsA64.P3.JEntW1A +import Fl4ProofsA64.P3.InsDisA +import Fl4ProofsA64.P3.LookupA +import Fl4ProofsA64.P3.CnodeSpecA +import Fl4ProofsA64.P3.CnodeCapsA +import Fl4ProofsA64.P3.RestartA +import Fl4ProofsA64.P3.NoChildA +import Fl4ProofsA64.P3.CnodeCopyA +import Fl4ProofsA64.P3.CnodeRotA +import Fl4ProofsA64.P3.FrontEndExtraA +import Fl4ProofsA64.P3.CnodeFrontA +import Fl4ProofsA64.P3.VsTabJA +import Fl4ProofsA64.P3.VsMapA +import Fl4ProofsA64.P3.VsMapPageA +import Fl4ProofsA64.P3.VsMapJA +import Fl4ProofsA64.P3.TabOffW1A +import Fl4ProofsA64.P3.TabNewA import Fl4ProofsA64.P1.S1aArmTop import Fl4ProofsA64.P1.S1aRtCol import Fl4ProofsA64.P1.S1aRtTop @@ -1075,7 +1111,369 @@ import Fl4ProofsA64.P1.BootMdb1 import Fl4ProofsA64.P1.BootSysL1 import Fl4ProofsA64.P1.BootScAp import Fl4ProofsA64.P2.BootSysA +import Fl4ProofsA64.P3.VsRelA +import Fl4ProofsA64.P3.VsTieA +import Fl4ProofsA64.P3.VsInvA +import Fl4ProofsA64.P3.VsBootA +import Fl4ProofsA64.P3.VsFindA +import Fl4ProofsA64.P3.PtrA +import Fl4ProofsA64.P3.VsWalkA +import Fl4ProofsA64.P3.VsLeafA +import Fl4ProofsA64.P3.VsUnmapA import Fl4ProofsA64.P1.BootW1Win +import Fl4ProofsA64.P3.NtfnRelA +import Fl4ProofsA64.P3.NtfnSpecA +import Fl4ProofsA64.P3.NtfnWriteA +import Fl4ProofsA64.P3.NtfnSendA +import Fl4ProofsA64.P3.NtfnQueueA +import Fl4ProofsA64.P3.NtfnRecvA +import Fl4ProofsA64.P3.NtfnJoinA +import Fl4ProofsA64.P3.EpRelA +import Fl4ProofsA64.P3.EpSpecA +import Fl4ProofsA64.P3.EpWriteA +import Fl4ProofsA64.P3.EpQueueA +import Fl4ProofsA64.P3.EpRegsA +import Fl4ProofsA64.P3.EpMrsA +import Fl4ProofsA64.P3.SchedOpsA +import Fl4ProofsA64.P3.NtfnWakeA +import Fl4ProofsA64.P3.NtfnBlockA +import Fl4ProofsA64.P3.EpCancelA +import Fl4ProofsA64.P3.SpecDetA +import Fl4ProofsA64.P3.EpBufA +import Fl4ProofsA64.P3.EpCancelAllA +import Fl4ProofsA64.P3.IpcCancelA +import Fl4ProofsA64.P3.EpSetMrA +import Fl4ProofsA64.P3.EpCopyMrsA +import Fl4ProofsA64.P3.EpXferA +import Fl4ProofsA64.P3.EpSetMrsNA +import Fl4ProofsA64.P3.EpFaultCapA +import Fl4ProofsA64.P3.EpFaultMoreA +import Fl4ProofsA64.P3.EpFaultXferA +import Fl4ProofsA64.P3.EpFaultTmoA +import Fl4ProofsA64.P3.EpSendRecvA +import Fl4ProofsA64.P3.EpSendBlkA +import Fl4ProofsA64.P3.EpRecvSendA +import Fl4ProofsA64.P3.EpRecvBlkA +import Fl4ProofsA64.P3.EpRecvTopA +import Fl4ProofsA64.P3.EpRecvBndA +import Fl4ProofsA64.P3.EpCancelBadgedTopA +import Fl4ProofsA64.P3.NtfnCancelAllA +import Fl4ProofsA64.P3.EpCancelIpcA +import Fl4ProofsA64.P3.DelSpecA +import Fl4ProofsA64.P3.ExtVsSpecA +import Fl4ProofsA64.P3.ExtVsRelA +import Fl4ProofsA64.P3.ExtVsWalkA +import Fl4ProofsA64.P3.ExtVsUnmapA +import Fl4ProofsA64.P3.ExtVsTopA +import Fl4ProofsA64.P3.ExtVsHitA +import Fl4ProofsA64.P3.FinVsA +import Fl4ProofsA64.P3.FinVsJA +import Fl4ProofsA64.P3.FinVsXA +import Fl4ProofsA64.P3.TabZeroA +import Fl4ProofsA64.P3.SetCapJA +import Fl4ProofsA64.P3.UnmapTopJA +import Fl4ProofsA64.P3.FinDefsA +import Fl4ProofsA64.P3.FinBootA +import Fl4ProofsA64.P3.UnmapDecJA +import Fl4ProofsA64.P3.UnmapS1JA +import Fl4ProofsA64.P3.UnmapL4vJA +import Fl4ProofsA64.P3.FinKeepA +import Fl4ProofsA64.P3.UnmapPtS1JA +import Fl4ProofsA64.P3.CarryA +import Fl4ProofsA64.P3.VsArmA +import Fl4ProofsA64.P3.VsTopPtA +import Fl4ProofsA64.P3.VsTopFrA +import Fl4ProofsA64.P3.VsTopRootA +import Fl4ProofsA64.P3.VsAssignA +import Fl4ProofsA64.P3.VsFlushA +import Fl4ProofsA64.P3.ExtMapFrA +import Fl4ProofsA64.P3.ExtMapPtA +import Fl4ProofsA64.P3.FrFlushA +import Fl4ProofsA64.P3.VsS2FlushA +import Fl4ProofsA64.P3.VsMakePoolA +import Fl4ProofsA64.P3.PtCapBootA +import Fl4ProofsA64.P3.ExtVsPerfA +import Fl4ProofsA64.P3.ExtVsDefA +import Fl4ProofsA64.P3.ScRelA +import Fl4ProofsA64.P3.ScSpecA +import Fl4ProofsA64.P3.ScRefillA +import Fl4ProofsA64.P3.ScJoinA +import Fl4ProofsA64.P3.ScRingA +import Fl4ProofsA64.P3.ScBudgetA +import Fl4ProofsA64.P3.ScCheckA +import Fl4ProofsA64.P3.ScSchedA +import Fl4ProofsA64.P3.ScCoreA +import Fl4ProofsA64.P3.ScStableA +import Fl4ProofsA64.P3.ScEnqA +import Fl4ProofsA64.P3.ScSwitchA +import Fl4ProofsA64.P3.ScDonA +import Fl4ProofsA64.P3.ScUnblockA +import Fl4ProofsA64.P3.ScSporA +import Fl4ProofsA64.P3.ScTailA +import Fl4ProofsA64.P3.ScDonRefA +import Fl4ProofsA64.P3.ScRelEnqA +import Fl4ProofsA64.P3.ScPostA +import Fl4ProofsA64.P3.ScResumeA +import Fl4ProofsA64.P3.ScDonBA +import Fl4ProofsA64.P3.ScWakeDonA +import Fl4ProofsA64.P3.ScGlueA +import Fl4ProofsA64.P3.ScBootA +import Fl4ProofsA64.P3.ScConsA +import Fl4ProofsA64.P3.ScRetA +import Fl4ProofsA64.P3.ScRecvA +import Fl4ProofsA64.P3.ScFrameA +import Fl4ProofsA64.P3.ScSWrA +import Fl4ProofsA64.P3.ScDomKeepA +import Fl4ProofsA64.P3.ScIdleA +import Fl4ProofsA64.P3.ScDonFromA +import Fl4ProofsA64.P3.ScReplyA +import Fl4ProofsA64.P3.ScTailEA +import Fl4ProofsA64.P3.ScReplyBA +import Fl4ProofsA64.P3.ScPopA +import Fl4ProofsA64.P3.ScPopDonA +import Fl4ProofsA64.P3.BufNativeFrA +import Fl4ProofsA64.P3.IpcScGlueA +import Fl4ProofsA64.P3.IpcScRecvReturnA +import Fl4ProofsA64.P3.EpReplyStackA +import Fl4ProofsA64.P3.EpReplyPushA +import Fl4ProofsA64.P3.IpcScReplyA +import Fl4ProofsA64.P3.IpcScPushA +import Fl4ProofsA64.P3.IpcScPushBlockA +import Fl4ProofsA64.P3.IpcScPushTopA +import Fl4ProofsA64.P3.IpcScPopLinksA +import Fl4ProofsA64.P3.IpcScPopA +import Fl4ProofsA64.P3.IpcScPopReadA +import Fl4ProofsA64.P3.IpcScPopHeapA +import Fl4ProofsA64.P3.IpcScPopPrepA +import Fl4ProofsA64.P3.IpcScPopDonateA +import Fl4ProofsA64.P3.IpcScPopTopA +import Fl4ProofsA64.P3.IpcScRemoveSpliceA +import Fl4ProofsA64.P3.IpcScRemoveA +import Fl4ProofsA64.P3.IpcScRemoveCutA +import Fl4ProofsA64.P3.IpcScRemoveTopA +import Fl4ProofsA64.P3.IpcScBootA +import Fl4ProofsA64.P3.IpcScEndpointFrameA +import Fl4ProofsA64.P3.IpcScEndpointPushSpecA +import Fl4ProofsA64.P3.IpcScEndpointPushProgramA +import Fl4ProofsA64.P3.IpcScEndpointRefillA +import Fl4ProofsA64.P3.IpcScRecvPushA +import Fl4ProofsA64.P3.IpcScSendRunningA +import Fl4ProofsA64.P3.IpcScSendSwitchA +import Fl4ProofsA64.P3.IpcScSendWakeA +import Fl4ProofsA64.P3.IpcScSendPushA +import Fl4ProofsA64.P3.ScUnbRepA +import Fl4ProofsA64.P3.ScDeqCoreA +import Fl4ProofsA64.P3.ScDeqA +import Fl4ProofsA64.P3.ScRelRmA +import Fl4ProofsA64.P3.ScUnbTcbA +import Fl4ProofsA64.P3.ScUnbEmptyA +import Fl4ProofsA64.P3.ScUnbAllA +import Fl4ProofsA64.P3.ScBindCoreA +import Fl4ProofsA64.P3.ScUnbInvokeA +import Fl4ProofsA64.P3.ScUnbObjA +import Fl4ProofsA64.P3.ScConsumeOpA +import Fl4ProofsA64.P3.ScYieldCoreA +import Fl4ProofsA64.P3.ScBindStallA +import Fl4ProofsA64.P3.ScSetStateA +import Fl4ProofsA64.P3.ScNtfnLinkA +import Fl4ProofsA64.P3.ScDecodeOpA +import Fl4ProofsA64.P3.ScInvokeRestartA +import Fl4ProofsA64.P3.ScUnbComposeA +import Fl4ProofsA64.P3.ScInvokeTopA +import Fl4ProofsA64.P3.ScUnbTopA +import Fl4ProofsA64.P3.ScConsumeMsgA +import Fl4ProofsA64.P3.ScYieldRelA +import Fl4ProofsA64.P3.ScBindLinkA +import Fl4ProofsA64.P3.ScBindUnblockA +import Fl4ProofsA64.P3.ScBindExecA +import Fl4ProofsA64.P3.ScBindTcbA +import Fl4ProofsA64.P3.ScBindTopA +import Fl4ProofsA64.P3.ScYieldMsgA +import Fl4ProofsA64.P3.ScYieldFrameA +import Fl4ProofsA64.P3.ScYieldResumeA +import Fl4ProofsA64.P3.ScYieldLinkA +import Fl4ProofsA64.P3.ScYieldCompleteA +import Fl4ProofsA64.P3.ScYieldQueueA +import Fl4ProofsA64.P3.ScYieldSwitchA +import Fl4ProofsA64.P3.ScYieldRunA +import Fl4ProofsA64.P3.ScYieldBodyA +import Fl4ProofsA64.P3.ScYieldTopA +import Fl4ProofsA64.P3.IpcInvokeDispatchA +import Fl4ProofsA64.P3.DomainSetDispatchA +import Fl4ProofsA64.P3.IrqInvokeDispatchA +import Fl4ProofsA64.P3.IrqInvokeTopA +import Fl4ProofsA64.P3.LocalInvokePostA +import Fl4ProofsA64.P3.IpcScReplyBodyA +import Fl4ProofsA64.P3.IpcScReplyTimeoutA +import Fl4ProofsA64.P3.ScCfgPlatA +import Fl4ProofsA64.P3.ScCfgTopA +import Fl4ProofsA64.P3.ScBudgetTopA +import Fl4ProofsA64.P3.ScActivateA +import Fl4ProofsA64.P3.ScArchSwA +import Fl4ProofsA64.P3.ScDeadlineA +import Fl4ProofsA64.P3.ScDomainA +import Fl4ProofsA64.P3.ScDomainChooseA +import Fl4ProofsA64.P3.ScSchedCtxA +import Fl4ProofsA64.P3.ScFinishVcpuA +import Fl4ProofsA64.P3.ScAwakenA +import Fl4ProofsA64.P3.ScSchedWordsA +import Fl4ProofsA64.P3.ScSelectA +import Fl4ProofsA64.P3.ScChooseA +import Fl4ProofsA64.P3.ScSchedChargeA +import Fl4ProofsA64.P3.ScInstallA +import Fl4ProofsA64.P3.ScScheduleTimerA +import Fl4ProofsA64.P3.ScScheduleA +import Fl4ProofsA64.P3.TcbInvokeDispatchA +import Fl4ProofsA64.P3.TcbInvokePacA +import Fl4ProofsA64.P3.TcbInvokeRegsA +import Fl4ProofsA64.P3.TcbInvokeFlagsA +import Fl4ProofsA64.P3.TcbInvokeFpuA +import Fl4ProofsA64.P3.TcbInvokeNtfnA +import Fl4ProofsA64.P3.TcbInvokeResumeA +import Fl4ProofsA64.P3.TcbInvokeSlotA +import Fl4ProofsA64.P3.TcbInvokeTimerA +import Fl4ProofsA64.P3.TcbInvokeSetTimerEntryA +import Fl4ProofsA64.P3.TcbInvokeRootsA +import Fl4ProofsA64.P3.TcbInvokeSlotFinalA +import Fl4ProofsA64.P3.TcbInvokeThreadFieldsA +import Fl4ProofsA64.P3.TcbInvokeMcpA +import Fl4ProofsA64.P3.TcbInvokeTimerSpecA +import Fl4ProofsA64.P3.TcbInvokeNtfnPostA +import Fl4ProofsA64.P3.TcbInvokeSlotPostA +import Fl4ProofsA64.P3.TcbInvokeRegisterSelectA +import Fl4ProofsA64.P3.TcbInvokeHighSelectA +import Fl4ProofsA64.P3.TcbInvokeReadPostA +import Fl4ProofsA64.P3.TcbInvokeReadCallPostA +import Fl4ProofsA64.P3.TcbInvokeReadCallPerformA +import Fl4ProofsA64.P3.TcbInvokeSchedSelectA +import Fl4ProofsA64.P3.TcbInvokeSchedControlA +import Fl4ProofsA64.P3.TcbInvokePriorityA +import Fl4ProofsA64.P3.TcbInvokeTimeoutA +import Fl4ProofsA64.P3.TcbInvokeTlsPostA +import Fl4ProofsA64.P3.TcbInvokeBufferA +import Fl4ProofsA64.P3.TcbInvokeBufferCheckA +import Fl4ProofsA64.P3.TcbInvokeRegOrderA +import Fl4ProofsA64.P3.TcbInvokeWriteLoopsA +import Fl4ProofsA64.P3.TcbInvokeWriteSpecA +import Fl4ProofsA64.P3.TcbInvokeWriteStepA +import Fl4ProofsA64.P3.TcbInvokeWritePrefixA +import Fl4ProofsA64.P3.TcbInvokeScBacklinkA +import Fl4ProofsA64.P3.TcbInvokeCopyOrderA +import Fl4ProofsA64.P3.TcbInvokeCopyLoopsA +import Fl4ProofsA64.P3.TcbInvokeCopyPostA +import Fl4ProofsA64.P3.TcbInvokeCopyTransferA +import Fl4ProofsA64.P3.TcbInvokeCopySelectA +import Fl4ProofsA64.P3.TcbInvokeCopySpecA +import Fl4ProofsA64.P3.TcbInvokePacSelectA +import Fl4ProofsA64.P3.TcbInvokeReadLoopsA +import Fl4ProofsA64.P3.TcbInvokeReadCallA +import Fl4ProofsA64.P3.TcbInvokeReadDataA +import Fl4ProofsA64.P3.TcbInvokeReadReplyA +import Fl4ProofsA64.P3.DevFrDisjA +import Fl4ProofsA64.P3.InvokeRestartJointA +import Fl4ProofsA64.P3.IpcExtraCapTransferA +import Fl4ProofsA64.P3.IrqDeleteEntryFrameA +import Fl4ProofsA64.P3.IrqFinalCoverageA +import Fl4ProofsA64.P3.LocalIpcInvokeA +import Fl4ProofsA64.P3.LocalIpcRecvInvokeA +import Fl4ProofsA64.P3.LocalScInvokeA +import Fl4ProofsA64.P3.OuterEntryA +import Fl4ProofsA64.P3.ReplyLiveJointA +import Fl4ProofsA64.P3.ReplyLiveSpecA +import Fl4ProofsA64.P3.ReplyPopFootA +import Fl4ProofsA64.P3.ReplyRemoveAllFootA +import Fl4ProofsA64.P3.ReplyRemoveBorA +import Fl4ProofsA64.P3.ReplyRemoveFootA +import Fl4ProofsA64.P3.ReplyRemoveKeepA +import Fl4ProofsA64.P3.ReplyRemoveTopFootA +import Fl4ProofsA64.P3.ReplyRemoveUnblockJointA +import Fl4ProofsA64.P3.ReplyRunExA +import Fl4ProofsA64.P3.ReplyTailExA +import Fl4ProofsA64.P3.ReplyUnblockFrameA +import Fl4ProofsA64.P3.ReplyXferFactsA +import Fl4ProofsA64.P3.ReplyXferKeepA +import Fl4ProofsA64.P3.SlotCoverageA +import Fl4ProofsA64.P3.SlotCoverageBootA +import Fl4ProofsA64.P3.TcbReadJointA +import Fl4ProofsA64.P3.CancelSchedA +import Fl4ProofsA64.P3.ScCancelYieldA +import Fl4ProofsA64.P3.TcbCancelRunA +import Fl4ProofsA64.P3.CancelIpcFootA +import Fl4ProofsA64.P3.CancelIpcKeepA +import Fl4ProofsA64.P3.TcbInactiveExA +import Fl4ProofsA64.P3.ScCancelYieldBA +import Fl4ProofsA64.P3.ReplyStackPopAllA +import Fl4ProofsA64.P3.CancelIpcReplyA +import Fl4ProofsA64.P3.CancelIpcReplyProgA +import Fl4ProofsA64.P3.TcbSuspendCancelA +import Fl4ProofsA64.P3.TcbSuspendA +import Fl4ProofsA64.P3.TcbRestartViewsA +import Fl4ProofsA64.P3.ScRestartUnbA +import Fl4ProofsA64.P3.TcbRestartResumeA +import Fl4ProofsA64.P3.TcbRestartFrameA +import Fl4ProofsA64.P3.TcbRestartTailA +import Fl4ProofsA64.P3.TcbRestartTailInactA +import Fl4ProofsA64.P3.TcbRestartA +import Fl4ProofsA64.P3.TcbResumeLocalA +import Fl4ProofsA64.P3.TcbRegsContA +import Fl4ProofsA64.P3.TcbWriteContA +import Fl4ProofsA64.P3.TcbWriteSchedA +import Fl4ProofsA64.P3.TcbRegViewA +import Fl4ProofsA64.P3.TcbWriteViewA +import Fl4ProofsA64.P3.TcbCopyViewA +import Fl4ProofsA64.P3.FrReplyA +import Fl4ProofsA64.P3.FrSubframeA +import Fl4ProofsA64.P3.AsidCtrlTopA +import Fl4ProofsA64.P3.ArchTopA +import Fl4ProofsA64.P3.W1UserBytesA +import Fl4ProofsA64.P3.UtIpcHeapA +import Fl4ProofsA64.P3.Stage3A +import Fl4ProofsA64.P3.Stage3BootNullA +import Fl4ProofsA64.P3.Stage3BootA +import Fl4ProofsA64.P3.CnodeFinalA +import Fl4ProofsA64.P3.CnodeDelInvA +import Fl4ProofsA64.P3.CnodeDelHookA +import Fl4ProofsA64.P3.CnodeDelFootA +import Fl4ProofsA64.P3.CnodeDelStepA +import Fl4ProofsA64.P3.Stage3ChKeepA +import Fl4ProofsA64.P3.Stage3ChCancelA +import Fl4ProofsA64.P3.Stage3ChReplyA +import Fl4ProofsA64.P3.Stage3ChPrimA +import Fl4ProofsA64.P3.Stage3DelFinChA +import Fl4ProofsA64.P3.CnodeDelZombieA +import Fl4ProofsA64.P3.CnodeDelWA +import Fl4ProofsA64.P3.CnodeDelRoundA +import Fl4ProofsA64.P3.CnodeDelSwapA +import Fl4ProofsA64.P3.CnodeDelDefA +import Fl4ProofsA64.P3.CnodeEmptyClA +import Fl4ProofsA64.P3.CnodeDelImmA +import Fl4ProofsA64.P3.CnodeDelFullA +import Fl4ProofsA64.P3.CnodeRevokeA +import Fl4ProofsA64.P3.CnodeDecDelA +import Fl4ProofsA64.P3.CnodeCancelA +import Fl4ProofsA64.P3.CnodeTopA +import Fl4ProofsA64.P3.CnodeFrontAllA +import Fl4ProofsA64.P1.MdbSemA +import Fl4ProofsA64.P1.MdbInvA +import Fl4ProofsA64.P1.MdbInvOpsA +import Fl4ProofsA64.P1.MdbInvInsA +import Fl4ProofsA64.P1.MdbIrqInvA +import Fl4ProofsA64.P1.MdbDelInvA +import Fl4ProofsA64.P1.MdbBootInvA +import Fl4ProofsA64.P3.Stage3EntryProjA +import Fl4ProofsA64.P3.Stage3SchedPresA +import Fl4ProofsA64.P3.Stage3J7A +import Fl4ProofsA64.P3.IrqSlotsFootA +import Fl4ProofsA64.P3.Stage3SchedWrA +import Fl4ProofsA64.P3.Stage3J5A +import Fl4ProofsA64.P3.Stage3SchedWrNA +import Fl4ProofsA64.P3.SpecFrameCalcA +import Fl4ProofsA64.P3.SpecFrameSchedA +import Fl4ProofsA64.P3.Stage3SchedActA +import Fl4ProofsA64.P3.Stage3SchedAwakenA +import Fl4ProofsA64.P3.Stage3SchedSchedA +import Fl4ProofsA64.P3.CnodeMdbBridgeA +import Fl4ProofsA64.P3.IpcExtraCapInsertDischA import Fl4ProofsA64.P1.S1aR2DelArm import Fl4ProofsA64.P1.S1aR2OfImage import Fl4ProofsA64.P1.S1aR2Transfer @@ -1101,6 +1499,142 @@ import Fl4ProofsA64.P1.S1aKtPoolArms import Fl4ProofsA64.P1.S1aKtRetype import Fl4ProofsA64.P1.S1aStepR2K import Fl4ProofsA64.P1.S1aStepR2KBoot +import Fl4ProofsA64.P3.IpcRecvSlotA +import Fl4ProofsA64.P3.IpcXferLoopSpecA +import Fl4ProofsA64.P3.IpcXferDeriveA +import Fl4ProofsA64.P3.InsBaseA +import Fl4ProofsA64.P3.IpcXferStepA +import Fl4ProofsA64.P3.IpcXferLoopA +import Fl4ProofsA64.P3.IpcExtraCapTransferDischA +import Fl4ProofsA64.P3.IpcXferOrthA +import Fl4ProofsA64.P3.IpcXferCapsDoA +import Fl4ProofsA64.P3.IpcCapsSendA +import Fl4ProofsA64.P3.IpcCapsRecvA +import Fl4ProofsA64.P3.IpcCapsReplyA +import Fl4ProofsA64.P3.IpcCapsFullA +import Fl4ProofsA64.P3.Stage3McsFrA +import Fl4ProofsA64.P3.Stage3McsUnbA +import Fl4ProofsA64.P3.Stage3TcbNtfnFrA +import Fl4ProofsA64.P3.Stage3TcbRestartFrA +import Fl4ProofsA64.P3.Stage3TcbF0A +import Fl4ProofsA64.P3.Stage3PreemptA +import Fl4ProofsA64.P3.Stage3BufWrA +import Fl4ProofsA64.P3.Stage3PartialSpecA +import Fl4ProofsA64.P3.SpecFrameIpcA +import Fl4ProofsA64.P3.Stage3PartialA +import Fl4ProofsA64.P3.UtSpecA +import Fl4ProofsA64.P3.UtDecodeA +import Fl4ProofsA64.P3.UtObjA +import Fl4ProofsA64.P3.UtTcbScA +import Fl4ProofsA64.P3.UtVcpuA +import Fl4ProofsA64.P3.UtVsObjA +import Fl4ProofsA64.P3.UtTcbCreateA +import Fl4ProofsA64.P3.UtDropA +import Fl4ProofsA64.P3.UtClosA +import Fl4ProofsA64.P3.UtResetSpecA +import Fl4ProofsA64.P3.UtResetRunA +import Fl4ProofsA64.P3.UtResetLoopA +import Fl4ProofsA64.P3.UtFrA +import Fl4ProofsA64.P3.UtRtFrA +import Fl4ProofsA64.P3.UtRtEndA +import Fl4ProofsA64.P3.UtRetVcpuA +import Fl4ProofsA64.P3.UtRtThA +import Fl4ProofsA64.P3.UtRtSpecA +import Fl4ProofsA64.P3.UtRtInvA +import Fl4ProofsA64.P3.UtRetDecVcpuA +import Fl4ProofsA64.P3.UtRtWitA +import Fl4ProofsA64.P3.UtRtCapsA +import Fl4ProofsA64.P3.UtRtCdtA +import Fl4ProofsA64.P3.UtRtFrontA +import Fl4ProofsA64.P3.UtRtSiteA +import Fl4ProofsA64.P3.UtRtDecSiteA +import Fl4ProofsA64.P3.UtDropVsA +import Fl4ProofsA64.P3.UtDropVs2A +import Fl4ProofsA64.P3.SchedFootA +import Fl4ProofsA64.P3.EpFootHelpA +import Fl4ProofsA64.P3.IpcScFootA +import Fl4ProofsA64.P3.Stage3IpcFootA +import Fl4ProofsA64.P3.Stage3TimeoutFootA +import Fl4ProofsA64.P3.SchedFootNA +import Fl4ProofsA64.P3.DelHookKindA +import Fl4ProofsA64.P3.Stage3TA +import Fl4ProofsA64.P3.Stage3VsWriteTA +import Fl4ProofsA64.P3.DelRecA +import Fl4ProofsA64.P3.DelStepMdbA +import Fl4ProofsA64.P3.DelSwapMdbA +import Fl4ProofsA64.P3.DelStepSpecA +import Fl4ProofsA64.P3.DelStepKeepA +import Fl4ProofsA64.P3.DelBlkKeepA +import Fl4ProofsA64.P3.DelBufKeepA +import Fl4ProofsA64.P3.DelCtOffA +import Fl4ProofsA64.P3.DelStepAsmA +import Fl4ProofsA64.P3.Stage3SchedWrTA +import Fl4ProofsA64.P3.Stage3PreemptTA +import Fl4ProofsA64.P3.Stage3SchedOpsTA +import Fl4ProofsA64.P3.Stage3SchedWrNTA +import Fl4ProofsA64.P3.DelPpA +import Fl4ProofsA64.P3.DelPpLiftA +import Fl4ProofsA64.P3.DelTimeSpecA +import Fl4ProofsA64.P3.DelClockA +import Fl4ProofsA64.P3.DelRdThA +import Fl4ProofsA64.P3.DelNtfnFrameA +import Fl4ProofsA64.P3.SigKeepA +import Fl4ProofsA64.P3.SigFootA +import Fl4ProofsA64.P3.SigTcbKeepA +import Fl4ProofsA64.P3.DelNtfnEpA +import Fl4ProofsA64.P3.DelNtfnUnbindA +import Fl4ProofsA64.P3.DelNtfnBufA +import Fl4ProofsA64.P3.DelNtfnJ5A +import Fl4ProofsA64.P3.DelNtfnBoundA +import Fl4ProofsA64.P3.DelNtfnLoopFrA +import Fl4ProofsA64.P3.DelSidleStepA +import Fl4ProofsA64.P3.DelSactSlotA +import Fl4ProofsA64.P3.DelSinvLoopA +import Fl4ProofsA64.P3.DelNtfnEntA +import Fl4ProofsA64.P3.DelNtfnUnlinkA +import Fl4ProofsA64.P3.DelNtfnRecA +import Fl4ProofsA64.P3.DelEpKeepA +import Fl4ProofsA64.P3.DelEpLoopA +import Fl4ProofsA64.P3.DelEpRecA +import Fl4ProofsA64.P3.DelEpHookA +import Fl4ProofsA64.P3.DelIrqHookA +import Fl4ProofsA64.P3.ScDonQueuedA +import Fl4ProofsA64.P3.ReplyNotIdleA +import Fl4ProofsA64.P3.ReplyStallA +import Fl4ProofsA64.P3.ReplyHookStallA +import Fl4ProofsA64.P3.ReplyHookNoopA +import Fl4ProofsA64.P3.ReplyCallerA +import Fl4ProofsA64.P3.ReplyCancelFootA +import Fl4ProofsA64.P3.Stage3CancelFootA +import Fl4ProofsA64.P3.Stage3CancelFootOffA +import Fl4ProofsA64.P3.Stage3RecvPushCancelA +import Fl4ProofsA64.P3.ReplyHookRecvA +import Fl4ProofsA64.P3.ReplyHookRecvArmA +import Fl4ProofsA64.P3.DelRvVrA +import Fl4ProofsA64.P3.DelIrqFrA +import Fl4ProofsA64.P3.DelIrqWrA +import Fl4ProofsA64.P3.DelIrqWriteA +import Fl4ProofsA64.P3.DelIrqPostA +import Fl4ProofsA64.P3.DelScInactA +import Fl4ProofsA64.P3.DelScSpecA +import Fl4ProofsA64.P3.DelScStallA +import Fl4ProofsA64.P3.DelScChainA +import Fl4ProofsA64.P3.DelScRecA +import Fl4ProofsA64.P3.DelScHookA +import Fl4ProofsA64.P3.ReplyRemoveInvA +import Fl4ProofsA64.P3.Stage3SchedWrSTA +import Fl4ProofsA64.P3.ReplyHookRemoveA +import Fl4ProofsA64.P3.ReplyHookA +import Fl4ProofsA64.P3.ReplyRemoveFootQA +import Fl4ProofsA64.P3.ReplyRemoveQA +import Fl4ProofsA64.P3.DelNtfnHookA +import Fl4ProofsA64.P3.DelAsidSpecA +import Fl4ProofsA64.P3.DelAsidA +import Fl4ProofsA64.P3.DelAsidPoolA +import Fl4ProofsA64.P3.Stage3VsWriteA +import Fl4ProofsA64.P3.Stage3VsFootA +import Fl4ProofsA64.P3.Stage3D1bA +import Fl4ProofsA64.P3.Stage3FieldsA import Fl4ProofsA64.P1.S1aCKeep import Fl4ProofsA64.P1.S1aCBootSym import Fl4ProofsA64.P1.S1aCRej @@ -1116,6 +1650,52 @@ import Fl4ProofsA64.P2.BufWireA import Fl4ProofsA64.P2.DelLkA import Fl4ProofsA64.P2.GateExecA import Fl4ProofsA64.P2.GateBootA +import Fl4ProofsA64.P3.NtfnWakeFootA +import Fl4ProofsA64.P3.Stage3NtfnFootA +import Fl4ProofsA64.P3.Stage3NtfnIdleFootA +import Fl4ProofsA64.P3.Stage3RecvFootA +import Fl4ProofsA64.P3.Stage3SignalFootA +import Fl4ProofsA64.P3.Stage3ReplyWA +import Fl4ProofsA64.P3.Stage3RestartA +import Fl4ProofsA64.P3.DelJ7LA +import Fl4ProofsA64.P3.Stage3VsFootMapA +import Fl4ProofsA64.P3.Stage3VsFootFlushA +import Fl4ProofsA64.P3.Stage3VsCloseA +import Fl4ProofsA64.P3.Stage3CapSlotA +import Fl4ProofsA64.P3.Stage3MapAlikeA +import Fl4ProofsA64.P3.Stage3CapFrameA +import Fl4ProofsA64.P3.Stage3CapBufA +import Fl4ProofsA64.P3.Stage3TcbReadFrA +import Fl4ProofsA64.P3.Stage3FBootA +import Fl4ProofsA64.P3.Stage3CapVsA +import Fl4ProofsA64.P3.Stage3CapVsFrameA +import Fl4ProofsA64.P3.Stage3ArmFrUnmA +import Fl4ProofsA64.P3.Stage3VsZeroA +import Fl4ProofsA64.P3.Stage3UnmapSpecA +import Fl4ProofsA64.P3.Stage3ArmFrUnmMA +import Fl4ProofsA64.P3.Stage3ArmFrUnmZA +import Fl4ProofsA64.P3.Stage3UnmapExtSpecA +import Fl4ProofsA64.P3.Stage3ArmFrUnmXA +import Fl4ProofsA64.P3.Stage3VsLeafA +import Fl4ProofsA64.P3.Stage3MapStoreA +import Fl4ProofsA64.P3.Stage3MapShapeA +import Fl4ProofsA64.P3.Stage3MapUnmA +import Fl4ProofsA64.P3.Stage3ArmFrMapA +import Fl4ProofsA64.P3.Stage3MapExtA +import Fl4ProofsA64.P3.Stage3MapExtCoreA +import Fl4ProofsA64.P3.Stage3ArmFrMapHiA +import Fl4ProofsA64.P3.Stage3ArmPtUnmUA +import Fl4ProofsA64.P3.Stage3ArmPtUnmMA +import Fl4ProofsA64.P3.Stage3ArmPtUnmXA +import Fl4ProofsA64.P3.Stage3TcbSuspFrA +import Fl4ProofsA64.P3.Stage3TcbRestartFr2A +import Fl4ProofsA64.P3.Stage3TcbSuspResA +import Fl4ProofsA64.P3.Stage3TcbRegsFrA +import Fl4ProofsA64.P3.Stage3CapVsFrameGA +import Fl4ProofsA64.P3.Stage3VsTyPostA +import Fl4ProofsA64.P3.Stage3ArmAssignA +import Fl4ProofsA64.P3.Stage3PtMapShapeA +import Fl4ProofsA64.P3.Stage3ArmPtMapA import Fl4ProofsA64.P1.S1aCNtfnBind import Fl4ProofsA64.P1.S1aCTimer import Fl4ProofsA64.P1.S1aCCbs @@ -1153,3 +1733,608 @@ import Fl4ProofsA64.P1.S1aCRecv import Fl4ProofsA64.P1.S1aCIrq import Fl4ProofsA64.P1.S1aCReplyErr import Fl4ProofsA64.P1.S1aCEnt +import Fl4ProofsA64.P3.BootAddrA +import Fl4ProofsA64.P3.Stage3KtA +import Fl4ProofsA64.P3.Stage3CapDerivA +import Fl4ProofsA64.P3.CnSlotSbA +import Fl4ProofsA64.P3.Stage3CnMoveA +import Fl4ProofsA64.P3.Stage3ArmCnMoveA +import Fl4ProofsA64.P3.Stage3ArmCnRotA +import Fl4ProofsA64.P3.Stage3ArmCnSwapA +import Fl4ProofsA64.P3.Stage3CnInsA +import Fl4ProofsA64.P3.Stage3ArmCnCopyA +import Fl4ProofsA64.P3.Stage3Rec5A +import Fl4ProofsA64.P3.Stage3SchedValidA +import Fl4ProofsA64.P3.VcpuPremisesA +import Fl4ProofsA64.P3.Stage3DelInA +import Fl4ProofsA64.P3.Stage3SysPlaceA +import Fl4ProofsA64.P3.Stage3OffAsEA +import Fl4ProofsA64.P3.Stage3DecExtraA +import Fl4ProofsA64.P3.Stage3RsSiteA +import Fl4ProofsA64.P1.S1aStepPool +import Fl4ProofsA64.P3.Stage3FinA +import Fl4ProofsA64.P3.Stage3T3ArmsA +import Fl4ProofsA64.P3.Stage3T3ArmHiA +import Fl4ProofsA64.P3.Stage3T3DecEqA +import Fl4ProofsA64.P3.Stage3T3ArmCoreA +import Fl4ProofsA64.P3.Stage3T3ArmsCnA +import Fl4ProofsA64.P3.Stage3DispBaseA +import Fl4ProofsA64.P3.Stage3VcpuKeepA +import Fl4ProofsA64.P3.Stage3VcpuRunA +import Fl4ProofsA64.P3.Stage3DispVcpuA +import Fl4ProofsA64.P3.Stage3DispVsA +import Fl4ProofsA64.P3.Stage3DispCnA +import Fl4ProofsA64.P3.Stage3DispUtA +import Fl4ProofsA64.P3.Stage3DispTopA +import Fl4ProofsA64.P1.S1aStepFix +import Fl4ProofsA64.P3.Stage3SyscallA +import Fl4ProofsA64.P3.Stage3SchedActResA +import Fl4ProofsA64.P3.Stage3SchedRunA +import Fl4ProofsA64.P3.Stage3SchedCurA +import Fl4ProofsA64.P3.Stage3J7StepA +import Fl4ProofsA64.P3.Stage3SchedHoareA +import Fl4ProofsA64.P3.Stage3SchedValidStepA +import Fl4ProofsA64.P3.Stage3SchedSelectA +import Fl4ProofsA64.P3.Stage3SchedExitA +import Fl4ProofsA64.P3.Stage3SchedActivateA +import Fl4ProofsA64.P3.Stage3SchedExitFieldsA +import Fl4ProofsA64.P3.Stage3DelSchedValidA +import Fl4ProofsA64.P3.Stage3DelReplyRemoveA +import Fl4ProofsA64.P3.Stage3DelXSchedValidA +import Fl4ProofsA64.P3.Stage3SchedIdleA +import Fl4ProofsA64.P3.Stage3SigSchedValidA +import Fl4ProofsA64.P3.Stage3ExitHcA +import Fl4ProofsA64.P3.Stage3ExitAtA +import Fl4ProofsA64.P3.Stage3FiresA +import Fl4ProofsA64.P3.Stage3FireTwinsA +import Fl4ProofsA64.P3.Stage3FireReprA +import Fl4ProofsA64.P3.Stage3FireRecordA +import Fl4ProofsA64.P3.Stage3SigSwA +import Fl4ProofsA64.P3.DelUnmapSpecA +import Fl4ProofsA64.P3.DelVsKeepA +import Fl4ProofsA64.P3.DelSysKeepA +import Fl4ProofsA64.P3.DelZeroKeepA +import Fl4ProofsA64.P3.DelUnmapLeafA +import Fl4ProofsA64.P3.DelVaA +import Fl4ProofsA64.P3.FinHookPtA +import Fl4ProofsA64.P3.DelFrameLeafA +import Fl4ProofsA64.P3.FinHookFrameA +import Fl4ProofsA64.P3.DelPoolKeepA +import Fl4ProofsA64.P3.DelRootA +import Fl4ProofsA64.P3.FinHookRootA +import Fl4ProofsA64.P3.FinHookRootKA +import Fl4ProofsA64.P3.DelPoolR2A +import Fl4ProofsA64.P3.DelTabKeepA +import Fl4ProofsA64.P3.DelPoolRecA +import Fl4ProofsA64.P3.DelRdXA +import Fl4ProofsA64.P3.DelRestoreA +import Fl4ProofsA64.P3.DelSlotWA +import Fl4ProofsA64.P3.DelCapKeepA +import Fl4ProofsA64.P3.DelJ7StepA +import Fl4ProofsA64.P3.DelJ5StepA +import Fl4ProofsA64.P3.DelMdbLeafA +import Fl4ProofsA64.P3.FinHookPoolA +import Fl4ProofsA64.P3.DelThrUnbindA +import Fl4ProofsA64.P3.DelThrFrontA +import Fl4ProofsA64.P3.DelSuspendSpecA +import Fl4ProofsA64.P3.DelSuspendA +import Fl4ProofsA64.P3.DelThrTimerA +import Fl4ProofsA64.P3.DelThrScA +import Fl4ProofsA64.P3.DelThrSusA +import Fl4ProofsA64.P3.DelThrFpuA +import Fl4ProofsA64.P3.DelThrHookA +import Fl4ProofsA64.P3.FrAgDefA +import Fl4ProofsA64.P3.DelRec6A +import Fl4ProofsA64.P3.DelThrTimeA +import Fl4ProofsA64.P3.Stage3DelTopA +import Fl4ProofsA64.P3.Stage3DelSvAkA +import Fl4ProofsA64.P3.Stage3DelSvLiftA +import Fl4ProofsA64.P3.Stage3DelSvReplyA +import Fl4ProofsA64.P3.Stage3DelSvSuspendA +import Fl4ProofsA64.P3.Stage3DelSvIpcA +import Fl4ProofsA64.P3.Stage3DelSvScA +import Fl4ProofsA64.P3.Stage3DelSvThrA +import Fl4ProofsA64.P3.Stage3DelAsmA +import Fl4ProofsA64.P3.DelSuspendLiveA +import Fl4ProofsA64.P3.Stage3TcbSuspendArmA +import Fl4ProofsA64.P3.Stage3BodyKeepA +import Fl4ProofsA64.P3.Stage3CancelBodyA +import Fl4ProofsA64.P3.Stage3BodyOKA +import Fl4ProofsA64.P3.Stage3DispIpcA +import Fl4ProofsA64.P3.Stage3TcbUnbindA +import Fl4ProofsA64.P3.Stage3TcbMcpA +import Fl4ProofsA64.P3.Stage3TcbBindA +import Fl4ProofsA64.P3.Stage3DispTcbA +import Fl4ProofsA64.P3.Stage3CancelLoopA +import Fl4ProofsA64.P3.Stage3CancelBadgedA +import Fl4ProofsA64.P3.Stage3SigWakeFootA +import Fl4ProofsA64.P3.Stage3LiftMachA +import Fl4ProofsA64.P3.Stage3LiftBodyA +import Fl4ProofsA64.P3.Stage3DelBodyA +import Fl4ProofsA64.P3.Stage3EpilogueA +import Fl4ProofsA64.P3.Stage3WrapA +import Fl4ProofsA64.P3.Stage3InvSpecIpcA +import Fl4ProofsA64.P3.Stage3InvSpecVsA +import Fl4ProofsA64.P3.Stage3InvSpecCnA +import Fl4ProofsA64.P3.Stage3InvSpecFrontA +import Fl4ProofsA64.P3.Stage3InvSpecScA +import Fl4ProofsA64.P3.Stage3InvSpecTcbA +import Fl4ProofsA64.P3.Stage3InvSpecTcb2A +import Fl4ProofsA64.P3.Stage3InvSpecTcb3A +import Fl4ProofsA64.P3.Stage3InvSpecTcbTopA +import Fl4ProofsA64.P3.Stage3InvSpecTopA +import Fl4ProofsA64.P3.Stage3InvSpecL1A +import Fl4ProofsA64.P3.Stage3InvSpecL1FlA +import Fl4ProofsA64.P3.Stage3InvSpecL1RpA +import Fl4ProofsA64.P3.Stage3InvSpecL1IrqA +import Fl4ProofsA64.P3.Stage3InvSpecL1CtlA +import Fl4ProofsA64.P3.Stage3InvSpecL1IrqTopA +import Fl4ProofsA64.P3.Stage3PinFrontA +import Fl4ProofsA64.P3.Stage3PinCnA +import Fl4ProofsA64.P3.Stage3PinVsA +import Fl4ProofsA64.P3.Stage3SiteA +import Fl4ProofsA64.P3.Stage3ScopeArmsA +import Fl4ProofsA64.P3.Stage3KEntryFaultFinA +import Fl4ProofsA64.P3.Stage3SysFaultIpA +import Fl4ProofsA64.P3.Stage3SysFrontA +import Fl4ProofsA64.P3.Stage3SysTopA +import Fl4ProofsA64.P3.Stage3SysSendA +import Fl4ProofsA64.P3.Stage3BudgetFalseA +import Fl4ProofsA64.P3.Stage3ScopeA +import Fl4ProofsA64.P3.Stage3CnArmFactsA +import Fl4ProofsA64.P3.Stage3CnRotFactsA +import Fl4ProofsA64.P3.Stage3VsDecFactsA +import Fl4ProofsA64.P3.Stage3EpiP1A +import Fl4ProofsA64.P3.UtRtJ7A +import Fl4ProofsA64.P3.UtIdleAllA +import Fl4ProofsA64.P3.UtVsConvA +import Fl4ProofsA64.P3.UtDropJ57A +import Fl4ProofsA64.P3.UtDropTopA +import Fl4ProofsA64.P3.UtDropCA +import Fl4ProofsA64.P3.UtDropDevA +import Fl4ProofsA64.P3.UtResetDevA +import Fl4ProofsA64.P3.UtRtArchA +import Fl4ProofsA64.P3.UtVsOkBootA +import Fl4ProofsA64.P3.UtSbufA +import Fl4ProofsA64.P3.UtSysPlaceA +import Fl4ProofsA64.P3.UtRtResetA +import Fl4ProofsA64.P3.UtVsGrowA +import Fl4ProofsA64.P3.UtRtVsA +import Fl4ProofsA64.P3.UtD1bA +import Fl4ProofsA64.P3.UtVsOkJA +import Fl4ProofsA64.P3.UtCrtResetA +import Fl4ProofsA64.P3.UtRtCreateA +import Fl4ProofsA64.P3.UtRtResetCrtA +import Fl4ProofsA64.P3.UtRstReadyA +import Fl4ProofsA64.P3.UtRtSchedA +import Fl4ProofsA64.P3.UtRtFrameA +import Fl4ProofsA64.P3.UtRtDomA +import Fl4ProofsA64.P3.UtRtKeptA +import Fl4ProofsA64.P3.UtRtRdA +import Fl4ProofsA64.P3.UtFullIpcA +import Fl4ProofsA64.P3.UtFullPlaceA +import Fl4ProofsA64.P3.UtFullSchedA +import Fl4ProofsA64.P3.UtFullCapA +import Fl4ProofsA64.P3.UtFullSlotA +import Fl4ProofsA64.P3.UtFullDeadA +import Fl4ProofsA64.P3.UtFullTabA +import Fl4ProofsA64.P3.UtFullAllocA +import Fl4ProofsA64.P3.UtFullUbA +import Fl4ProofsA64.P3.UtFullJ7A +import Fl4ProofsA64.P3.UtJ5CtxA +import Fl4ProofsA64.P3.UtJ5InsA +import Fl4ProofsA64.P3.UtFullSqA +import Fl4ProofsA64.P3.UtFullCapTyA +import Fl4ProofsA64.P3.UtJ5SraA +import Fl4ProofsA64.P3.UtRtUmA +import Fl4ProofsA64.P3.UtAsmCapsA +import Fl4ProofsA64.P3.UtAsmSpecA +import Fl4ProofsA64.P3.UtAsmIpcA +import Fl4ProofsA64.P3.UtAsmHeapA +import Fl4ProofsA64.P3.UtAsmSchedA +import Fl4ProofsA64.P3.UtAsmSysA +import Fl4ProofsA64.P3.UtApA +import Fl4ProofsA64.P3.UtApSysA +import Fl4ProofsA64.P3.UtApCapsA +import Fl4ProofsA64.P3.UtApSpecA +import Fl4ProofsA64.P3.UtApHeapA +import Fl4ProofsA64.P3.UtApSpecObjA +import Fl4ProofsA64.P3.UtApIpcA +import Fl4ProofsA64.P3.UtApSchedA +import Fl4ProofsA64.P3.UtApSpec2A +import Fl4ProofsA64.P3.UtApCaps2A +import Fl4ProofsA64.P3.UtRtAsidA +import Fl4ProofsA64.P3.UtApHeap2A +import Fl4ProofsA64.P3.UtApSys2A +import Fl4ProofsA64.P3.UtAsmAllA +import Fl4ProofsA64.P3.UtAsmAll0A +import Fl4ProofsA64.P3.UtRtRecA +import Fl4ProofsA64.P3.UtRtRecTopA +import Fl4ProofsA64.P1.S1aRtTopK +import Fl4ProofsA64.P3.UtDecRecA +import Fl4ProofsA64.P3.UtApRstA +import Fl4ProofsA64.P3.UtJ5LoopA +import Fl4ProofsA64.P3.UtFullJ5A +import Fl4ProofsA64.P3.UtRtGlobA +import Fl4ProofsA64.P3.UtRtPostA +import Fl4ProofsA64.P3.UtRtZeroPostA +import Fl4ProofsA64.P3.UtRtTcbWordsA +import Fl4ProofsA64.P3.UtRtTcbPostA +import Fl4ProofsA64.P3.UtRtVcpuWordsA +import Fl4ProofsA64.P3.UtRtVcpuPostA +import Fl4ProofsA64.P3.UtRtHighWordsA +import Fl4ProofsA64.P3.UtRtHighPostA +import Fl4ProofsA64.P3.UtRtTopA +import Fl4ProofsA64.P3.UtPpWuA +import Fl4ProofsA64.P3.UtPpScA +import Fl4ProofsA64.P3.UtPpTsA +import Fl4ProofsA64.P3.UtPpHookA +import Fl4ProofsA64.P3.UtRtResetPpA +import Fl4ProofsA64.P3.UtClrA +import Fl4ProofsA64.P3.UtChunkA +import Fl4ProofsA64.P3.UtMakePoolA +import Fl4ProofsA64.P3.UtZeroSpecA +import Fl4ProofsA64.P3.UtResetA +import Fl4ProofsA64.P1.S1aRtTopRst +import Fl4ProofsA64.P3.UtRtRecRstA +import Fl4ProofsA64.P3.UtDecRecRstA +import Fl4ProofsA64.P3.UtApRec5A +import Fl4ProofsA64.P3.UtRec6KeepA +import Fl4ProofsA64.P3.UtVcInitA +import Fl4ProofsA64.P3.UtApRec6A +import Fl4ProofsA64.P3.UtVcTrA +import Fl4ProofsA64.P3.Stage3IpcGlueA +import Fl4ProofsA64.P3.Stage3SignalIdleA +import Fl4ProofsA64.P3.Stage3TcbWordsA +import Fl4ProofsA64.P3.Stage3TcbTlsA +import Fl4ProofsA64.P3.Stage3TcbPacA +import Fl4ProofsA64.P3.TcbArchKeepsA +import Fl4ProofsA64.P3.Stage3ScNtfnA +import Fl4ProofsA64.P3.Stage3DispScA +import Fl4ProofsA64.P3.Stage3TcbFpuA +import Fl4ProofsA64.P3.Stage3TcbFpuRdA +import Fl4ProofsA64.P3.Stage3ScCfgSpecA +import Fl4ProofsA64.P3.Stage3ScCfgRecA +import Fl4ProofsA64.P3.Stage3ReplyOneA +import Fl4ProofsA64.P3.Stage3ReplyMidA +import Fl4ProofsA64.P3.Stage3ScConsumedA +import Fl4ProofsA64.P3.Stage3TcbFlagsA +import Fl4ProofsA64.P3.Stage3TcbBodyA +import Fl4ProofsA64.P3.Stage3ScYieldA +import Fl4ProofsA64.P3.Stage3ScYieldRqA +import Fl4ProofsA64.P3.Stage3ScYieldSwA +import Fl4ProofsA64.P3.Stage3TcbResumeNoopA +import Fl4ProofsA64.P3.Stage3TcbResumeSpecA +import Fl4ProofsA64.P3.Stage3TcbResumeRqA +import Fl4ProofsA64.P3.Stage3Lift3QA +import Fl4ProofsA64.P3.Stage3TcbTlsSelfA +import Fl4ProofsA64.P3.Stage3SendNoblockA +import Fl4ProofsA64.P3.Stage3ReplyDetachedA +import Fl4ProofsA64.P3.Stage3ReplyKeepSpecA +import Fl4ProofsA64.P3.Stage3ReplyLiveKeepA +import Fl4ProofsA64.P3.Stage3ReplyRemQA +import Fl4ProofsA64.P3.Stage3ReplyArmA +import Fl4ProofsA64.P3.Stage3RqDomPrioA +import Fl4ProofsA64.P3.Stage3IpcGlueUA +import Fl4ProofsA64.P3.Stage3SendRecvFactsA +import Fl4ProofsA64.P3.Stage3RecvBufA +import Fl4ProofsA64.P3.Stage3SendSpecFrA +import Fl4ProofsA64.P3.Stage3IpcKeepSA +import Fl4ProofsA64.P3.Stage3IpcSchedValidA +import Fl4ProofsA64.P3.Stage3IpcFl4KeepA +import Fl4ProofsA64.P3.EpSendFpA +import Fl4ProofsA64.P3.Stage3SendNarrowA +import Fl4ProofsA64.P3.Stage3SendRecvArmA +import Fl4ProofsA64.P3.Stage3SendDonateKeepA +import Fl4ProofsA64.P3.Stage3SendDonateArmA +import Fl4ProofsA64.P3.Stage3SendNoDonKeepA +import Fl4ProofsA64.P3.Stage3SendNoDonArmA +import Fl4ProofsA64.P3.Stage3SendFaultSpecA +import Fl4ProofsA64.P3.Stage3SendFaultValidA +import Fl4ProofsA64.P3.EpSendTmoBA +import Fl4ProofsA64.P3.Stage3TimeoutSendBA +import Fl4ProofsA64.P3.MachKeepA +import Fl4ProofsA64.P3.Stage3KEntryBudgetRecA +import Fl4ProofsA64.P3.Stage3KEntryArmsA +import Fl4ProofsA64.P3.Stage3KEntryChargeA +import Fl4ProofsA64.P3.Stage3KEntryP1A +import Fl4ProofsA64.P3.Stage3KEntryRestartA +import Fl4ProofsA64.P3.Stage3KEntryTopA +import Fl4ProofsA64.P3.Stage3KEntryActA +import Fl4ProofsA64.P3.Stage3SchedBindA +import Fl4ProofsA64.P3.Stage3SchedCtQA +import Fl4ProofsA64.P3.Stage3KEntrySchedA +import Fl4ProofsA64.P3.Stage3SigBudgetA +import Fl4ProofsA64.P3.Stage3KEntryBudgetA +import Fl4ProofsA64.P3.Stage3SchedExitFrA +import Fl4ProofsA64.P3.Stage3KEntryExitA +import Fl4ProofsA64.P3.Stage3KEntryTimerIrqA +import Fl4ProofsA64.P3.Stage3KEntryIrqA +import Fl4ProofsA64.P3.Stage3KEntryFireSigA +import Fl4ProofsA64.P3.Stage3KEntrySendKeepA +import Fl4ProofsA64.P3.ScEntrySendA +import Fl4ProofsA64.P3.Stage3TimeoutSenderA +import Fl4ProofsA64.P3.Stage3KEntrySendA +import Fl4ProofsA64.P3.Stage3KEntrySendWireA +import Fl4ProofsA64.P3.Stage3PreemptChargeA +import Fl4ProofsA64.P3.Stage3PreemptIrqA +import Fl4ProofsA64.P3.Stage3EntryValidA +import Fl4ProofsA64.P3.Stage3AfterPreemptA +import Fl4ProofsA64.P3.Stage3HPreA +import Fl4ProofsA64.P3.Stage3KEntryBudgetSysA +import Fl4ProofsA64.P3.Stage3KEntryFaultA +import Fl4ProofsA64.P3.Stage3KEntryFiredA +import Fl4ProofsA64.P3.Stage3KEntryRootA +import Fl4ProofsA64.P3.Stage3KEntryTailA +import Fl4ProofsA64.P3.Stage3KEntryFinishFA +import Fl4ProofsA64.P3.Stage3KEntryActSpecA +import Fl4ProofsA64.P3.Stage3KEntryActRecA +import Fl4ProofsA64.P3.Stage3WkKeepA +import Fl4ProofsA64.P3.Stage3EpWakeFrA +import Fl4ProofsA64.P3.Stage3WakeInvA +import Fl4ProofsA64.P3.Stage3SigWakeFoot2A +import Fl4ProofsA64.P3.Stage3SigWakeFoot3A +import Fl4ProofsA64.P3.Stage3SortA +import Fl4ProofsA64.P3.Stage3QKeepPresA +import Fl4ProofsA64.P3.Stage3FaultSetA +import Fl4ProofsA64.P3.Stage3FaultTailA +import Fl4ProofsA64.P3.Stage3FaultSpecA +import Fl4ProofsA64.P3.Stage3FaultArmA +import Fl4ProofsA64.P3.Stage3FaultNoDonKeepA +import Fl4ProofsA64.P3.Stage3FaultNoDonArmA +import Fl4ProofsA64.P3.EpFaultReplyA +import Fl4ProofsA64.P3.Stage3ReplyFaultKeepSpecA +import Fl4ProofsA64.P3.Stage3SendQA +import Fl4ProofsA64.P3.Stage3SchedTailQA +import Fl4ProofsA64.P3.Stage3RecvPreA +import Fl4ProofsA64.P3.Stage3ReplyExitA +import Fl4ProofsA64.P3.Stage3ReplyRecvA +import Fl4ProofsA64.P3.Stage3RecvPollA +import Fl4ProofsA64.P3.Stage3RecvActiveA +import Fl4ProofsA64.P3.Stage3SortInsA +import Fl4ProofsA64.P3.Stage3RecvBlockKeepA +import Fl4ProofsA64.P3.Stage3RecvBlkSpecA +import Fl4ProofsA64.P3.Stage3RecvBlockA +import Fl4ProofsA64.P3.Stage3RecvSigKeepA +import Fl4ProofsA64.P3.Stage3RecvWaitBlkA +import Fl4ProofsA64.P3.Stage3RecvWaitA +import Fl4ProofsA64.P3.Stage3RecvWaitSpecScA +import Fl4ProofsA64.P3.Stage3RecvWaitScBodyA +import Fl4ProofsA64.P3.Stage3RecvWaitUnbA +import Fl4ProofsA64.P3.Stage3PreSchedA +import Fl4ProofsA64.P3.Stage3RecvWaitScA +import Fl4ProofsA64.P3.Stage3RecvSendSpecA +import Fl4ProofsA64.P3.Stage3RecvSendQA +import Fl4ProofsA64.P3.Stage3RecvSendValidA +import Fl4ProofsA64.P3.Stage3RecvSendFpA +import Fl4ProofsA64.P3.Stage3RecvSendFrA +import Fl4ProofsA64.P3.Stage3RecvSendSwA +import Fl4ProofsA64.P3.Stage3RecvSendCscA +import Fl4ProofsA64.P3.Stage3RecvTmoFpA +import Fl4ProofsA64.P3.Stage3RecvSendA +import Fl4ProofsA64.P3.Stage3RecvBoundA +import Fl4ProofsA64.P3.Stage3RecvReplyPinA +import Fl4ProofsA64.P3.Stage3RecvPushFpA +import Fl4ProofsA64.P3.Stage3RecvTmoPushFpA +import Fl4ProofsA64.P3.Stage3RecvPushA +import Fl4ProofsA64.P3.Stage3RecvBlkSpecRA +import Fl4ProofsA64.P3.Stage3RecvBlockKeepRA +import Fl4ProofsA64.P3.Stage3RecvBlockRA +import Fl4ProofsA64.P3.Stage3RecvBlockUnbA +import Fl4ProofsA64.P3.Stage3RecvBlockAnyA +import Fl4ProofsA64.P3.Stage3RecvTmoSelA +import Fl4ProofsA64.P3.Stage3RecvCapFaultA +import Fl4ProofsA64.P3.Stage3NBSendRecvA +import Fl4ProofsA64.P3.Stage3CombinedFrontA +import Fl4ProofsA64.P3.Stage3RecvPassiveA +import Fl4ProofsA64.P3.Stage3NBSignalWakeA +import Fl4ProofsA64.P3.Stage3NBSendIpcA +import Fl4ProofsA64.P3.Stage3SendDonPlainA +import Fl4ProofsA64.P3.Stage3SendDonPlainArmA +import Fl4ProofsA64.P3.Stage3NBWakeEntryA +import Fl4ProofsA64.P3.Stage3RecvBlockRUnbA +import Fl4ProofsA64.P3.EpSendBlkFpA +import Fl4ProofsA64.P3.Stage3SendBlkSpecA +import Fl4ProofsA64.P3.Stage3SendBlkA +import Fl4ProofsA64.P3.Stage3SendBlkArmA +import Fl4ProofsA64.P3.Stage3TimeoutSendBlkA +import Fl4ProofsA64.P3.Stage3PrioReorderA +import Fl4ProofsA64.P3.Stage3PrioSchedA +import Fl4ProofsA64.P3.Stage3PrioIpcA +import Fl4ProofsA64.P3.Stage3TcbPrioA +import Fl4ProofsA64.P3.Stage3TcbPrioPlainA +import Fl4ProofsA64.P3.Stage3TcbPrioEpA +import Fl4ProofsA64.P3.Stage3TcbPrioEpArmA +import Fl4ProofsA64.P3.Stage3TcbPrioNtfnA +import Fl4ProofsA64.P3.Stage3TcbPrioQA +import Fl4ProofsA64.P3.Stage3TcbPrioQInvA +import Fl4ProofsA64.P3.Stage3TcbPrioQStepA +import Fl4ProofsA64.P3.Stage3TcbPrioQArmA +import Fl4ProofsA64.P3.Stage3DomIpiA +import Fl4ProofsA64.P3.VcpuSpecA +import Fl4ProofsA64.P3.VcpuRelA +import Fl4ProofsA64.P3.VcpuFrameMemA +import Fl4ProofsA64.P3.VcpuRegJA +import Fl4ProofsA64.P3.VcpuXferJA +import Fl4ProofsA64.P3.VcpuSetTcbJA +import Fl4ProofsA64.P3.VcpuCheckJA +import Fl4ProofsA64.P3.VcpuRunBaseA +import Fl4ProofsA64.P3.VcpuRunBodyA +import Fl4ProofsA64.P3.VcpuRunJA +import Fl4ProofsA64.P3.VcpuTimestampA +import Fl4ProofsA64.P3.VcpuExitJA +import Fl4ProofsA64.P3.VcpuTopA +import Fl4ProofsA64.P3.VcpuDelCommA +import Fl4ProofsA64.P3.VcpuDelSpecA +import Fl4ProofsA64.P3.VcpuDelFrameA +import Fl4ProofsA64.P3.VcpuDelStepsA +import Fl4ProofsA64.P3.VcpuDelReadA +import Fl4ProofsA64.P3.VcpuDelHookA +import Fl4ProofsA64.P3.VcpuDelFinHookA +import Fl4ProofsA64.P3.Stage3DelAsmTopA +import Fl4ProofsA64.P3.Stage3TcbDelStepA +import Fl4ProofsA64.P3.Stage3TcbBufStepA +import Fl4ProofsA64.P3.Stage3LiftTcbBufA +import Fl4ProofsA64.P3.Stage3LiftTcbTimeoutA +import Fl4ProofsA64.P3.Stage3CapInsertTA +import Fl4ProofsA64.P3.Stage3BufNewA +import Fl4ProofsA64.P3.Stage3CapInsertBufA +import Fl4ProofsA64.P3.Stage3TcbDelGA +import Fl4ProofsA64.P3.Stage3TcbBufStepsA +import Fl4ProofsA64.P3.Stage3TcbReschedA +import Fl4ProofsA64.P3.Stage3LiftTcbBufNzA +import Fl4ProofsA64.P3.Stage3LiftTcbTimeoutNnA +import Fl4ProofsA64.P3.Stage3P1StepA +import Fl4ProofsA64.P3.Stage3SetPrioFrA +import Fl4ProofsA64.P3.Stage3TcbPrioAnyA +import Fl4ProofsA64.P3.Stage3LiftTcbSspA +import Fl4ProofsA64.P3.Stage3InsP1A +import Fl4ProofsA64.P3.Stage3LiftTcbSspEpA +import Fl4ProofsA64.P3.Stage3LiftFlushA +import Fl4ProofsA64.P3.Stage3DispFlushA +import Fl4ProofsA64.P3.Stage3DispIrqA +import Fl4ProofsA64.P3.Stage3LiftGetAddrA +import Fl4ProofsA64.P3.Stage3ClearAfA +import Fl4ProofsA64.P3.Stage3LiftTestClearA +import Fl4ProofsA64.P3.Stage3DispReplyA +import Fl4ProofsA64.P3.Stage3LiftDomainA +import Fl4ProofsA64.P3.Stage3DispDomA +import Fl4ProofsA64.P3.Stage3ErrPreA +import Fl4ProofsA64.P3.Stage3ErrKeepA +import Fl4ProofsA64.P3.Stage3ErrKeep2A +import Fl4ProofsA64.P3.Stage3DispErrA +import Fl4ProofsA64.P3.Stage3LiftIrqClearA +import Fl4ProofsA64.P3.Stage3LiftIrqSetA +import Fl4ProofsA64.P3.Stage3LiftIrqSigA +import Fl4ProofsA64.P3.Stage3LiftIrqIssueA +import Fl4ProofsA64.P3.Stage3LiftIrqDecA +import Fl4ProofsA64.P3.Stage3StallA +import Fl4ProofsA64.P3.Stage3ErrIrqA +import Fl4ProofsA64.P3.Stage3VsArmFactsA +import Fl4ProofsA64.P3.Stage3VsNoPreA +import Fl4ProofsA64.P3.Stage3VsRunA +import Fl4ProofsA64.P3.Stage3VsBodyA +import Fl4ProofsA64.P3.Stage3ReplyErrBufA +import Fl4ProofsA64.P3.Stage3ReplyErrGlueA +import Fl4ProofsA64.P3.Stage3ReplyErrA +import Fl4ProofsA64.P3.Stage3ReplyErrTopA +import Fl4ProofsA64.P3.Stage3KEntrySendBlkA +import Fl4ProofsA64.P3.Stage3IdleSchedA +import Fl4ProofsA64.P3.Stage3ReplyFaultClrA +import Fl4ProofsA64.P3.Stage3ReplyFaultMidA +import Fl4ProofsA64.P3.Stage3ReplyLiveKeepFA +import Fl4ProofsA64.P3.Stage3ReplyFaultGlueA +import Fl4ProofsA64.P3.Stage3ReplyFaultArmA +import Fl4ProofsA64.P3.Stage3ReplyTmoMidA +import Fl4ProofsA64.P3.Stage3SikxAlgA +import Fl4ProofsA64.P3.Stage3ReplyTmoFootA +import Fl4ProofsA64.P3.Stage3ReplyTmoRecvA +import Fl4ProofsA64.P3.Stage3ReplyTmoBlkA +import Fl4ProofsA64.P3.Stage3ReplyTmoDispA +import Fl4ProofsA64.P3.Stage3IpiKeepA +import Fl4ProofsA64.P3.Stage3DelFrameA +import Fl4ProofsA64.P3.Stage3DelBodyHookA +import Fl4ProofsA64.P3.Stage3DelFinVsA +import Fl4ProofsA64.P3.Stage3DelFinIpcA +import Fl4ProofsA64.P3.Stage3DelFinReplyA +import Fl4ProofsA64.P3.Stage3DelFinVcpuA +import Fl4ProofsA64.P3.Stage3DelFinIrqA +import Fl4ProofsA64.P3.Stage3DelFinScA +import Fl4ProofsA64.P3.Stage3DelFinThrA +import Fl4ProofsA64.P3.Stage3DelBodyLoopA +import Fl4ProofsA64.P3.Stage3BA +import Fl4ProofsA64.P3.Stage3WakePInvA +import Fl4ProofsA64.P3.SendSignalDetA +import Fl4ProofsA64.P3.Stage3SigWakeRecA +import Fl4ProofsA64.P3.Stage3SigBoundCancelA +import Fl4ProofsA64.P3.Stage3SigBoundRecA +import Fl4ProofsA64.P3.Stage3BodyCurA +import Fl4ProofsA64.P3.Stage3IpcBodyA +import Fl4ProofsA64.P3.Stage3SendBudgetA +import Fl4ProofsA64.P3.Stage3IpcWakeA +import Fl4ProofsA64.P3.Stage3ReplyWakeA +import Fl4ProofsA64.P3.Stage3ScBindSpecA +import Fl4ProofsA64.P3.Stage3ScBindTcbA +import Fl4ProofsA64.P3.Stage3ScBindArmA +import Fl4ProofsA64.P3.Stage3ScUnbA +import Fl4ProofsA64.P3.Stage3ErrKeepMacA +import Fl4ProofsA64.P3.Stage3ErrKeepVsA +import Fl4ProofsA64.P3.Stage3ErrKeepVsPoolA +import Fl4ProofsA64.P3.Stage3ErrKeepVsDecA +import Fl4ProofsA64.P3.Stage3ErrKeepVcpuA +import Fl4ProofsA64.P3.Stage3DecErrBaseA +import Fl4ProofsA64.P3.Stage3DecErrCnA +import Fl4ProofsA64.P3.Stage3DecErrKeepA +import Fl4ProofsA64.P3.Stage3DecErrMiscA +import Fl4ProofsA64.P3.Stage3DecErrPtA +import Fl4ProofsA64.P3.Stage3DecErrDomA +import Fl4ProofsA64.P3.Stage3DecErrIrqA +import Fl4ProofsA64.P3.Stage3DecErrScA +import Fl4ProofsA64.P3.Stage3DecErrScCapA +import Fl4ProofsA64.P3.Stage3DecErrUtA +import Fl4ProofsA64.P3.Stage3DecErrVsA +import Fl4ProofsA64.P3.Stage3DecErrTcbRegsA +import Fl4ProofsA64.P3.Stage3DecErrTcbAuthA +import Fl4ProofsA64.P3.Stage3DecErrTcbPacA +import Fl4ProofsA64.P3.Stage3DecErrTcbBufA +import Fl4ProofsA64.P3.Stage3DecErrTcbRootsA +import Fl4ProofsA64.P3.Stage3DecErrTcbSpaceA +import Fl4ProofsA64.P3.Stage3DecErrTcbA +import Fl4ProofsA64.P3.Stage3DecErrTcbObjA +import Fl4ProofsA64.P3.Stage3DecErrTcbHighA +import Fl4ProofsA64.P3.Stage3DecErrTcbSchedA +import Fl4ProofsA64.P3.Stage3DecErrVcpuA +import Fl4ProofsA64.P3.Stage3DecErrAllA +import Fl4ProofsA64.P3.Stage3RecvDonFrameA +import Fl4ProofsA64.P3.Stage3RecvDonSpecA +import Fl4ProofsA64.P3.Stage3RecvDonA +import Fl4ProofsA64.P3.Stage3RecvWaitDonA +import Fl4ProofsA64.P3.Stage3RecvBoundDonA +import Fl4ProofsA64.P3.Stage3RecvPushDonFpA +import Fl4ProofsA64.P3.Stage3RecvSendDonA +import Fl4ProofsA64.P3.Stage3RecvPushDonA +import Fl4ProofsA64.P3.Stage3RecvPassiveDonA +import Fl4ProofsA64.P3.Stage3RecvFrontA +import Fl4ProofsA64.P3.Stage3CancelReplyPA +import Fl4ProofsA64.P3.Stage3CancelReplyRecA +import Fl4ProofsA64.P3.Stage3RecvCancelA +import Fl4ProofsA64.P3.Stage3VcpuBridgeA +import Fl4ProofsA64.P3.Stage3T3ArmsVcpuA +import Fl4ProofsA64.P3.Stage3T3ArmsVcpu2A +import Fl4ProofsA64.P3.Stage3T3ArmsVcpuRunA +import Fl4ProofsA64.P3.Stage3VcpuExitA +import Fl4ProofsA64.P3.Stage3VcpuExitWireA +import Fl4ProofsA64.P3.Stage3SignalVmA +import Fl4ProofsA64.P3.Stage3SigVmArmA +import Fl4ProofsA64.P3.Stage3YieldBodyA +import Fl4ProofsA64.P3.Stage3RecvScopeA +import Fl4ProofsA64.P3.Stage3SysRecvA +import Fl4ProofsA64.P3.Stage3SendErrAllA +import Fl4ProofsA64.P3.Stage3FirstScopeA +import Fl4ProofsA64.P3.Stage3SysCombA +import Fl4ProofsA64.P3.Stage3RecvUnbRecA +import Fl4ProofsA64.P3.A64Wit2ArchA +import Fl4ProofsA64.P3.A64Wit2bGapA +import Fl4ProofsA64.P3.A64Wit2bNatA +import Fl4ProofsA64.P3.A64Wit2bBodyA +import Fl4ProofsA64.P3.A64Wit2PlatA +import Fl4ProofsA64.P3.A64WitW1A +import Fl4ProofsA64.P3.A64WitStA +import Fl4ProofsA64.P3.A64WitJA +import Fl4ProofsA64.P3.A64WitRecA +import Fl4ProofsA64.P3.A64WitSyscA +import Fl4ProofsA64.P3.A64WitCopyA +import Fl4ProofsA64.P3.A64WitTopA +import Fl4ProofsA64.P3.A64Wit2BudgetA +import Fl4ProofsA64.P3.A64Wit2EntryA +import Fl4ProofsA64.P3.A64Wit2TierA +import Fl4ProofsA64.P3.A64Wit2TopA +import Fl4ProofsA64.P3.Stage3DomIpiA +import Fl4ProofsA64.P3.A64ArchSwA +import Fl4ProofsA64.P3.A64ArchSwB +import Fl4ProofsA64.P3.A64ArchSwC +import Fl4ProofsA64.P3.A64ArchSwD +import Fl4ProofsA64.P3.A64ArchSwE +import Fl4ProofsA64.P3.A64ArchSwF +import Fl4ProofsA64.P3.A64ArchSwG +import Fl4ProofsA64.P3.A64ArchSwH diff --git a/proofs/Fl4ProofsA64/P1/MdbBootInvA.lean b/proofs/Fl4ProofsA64/P1/MdbBootInvA.lean new file mode 100644 index 000000000..c3f120c12 --- /dev/null +++ b/proofs/Fl4ProofsA64/P1/MdbBootInvA.lean @@ -0,0 +1,462 @@ +/- +P1 (aarch64, A64-P1-MDB wave 4): **boot satisfaction of `MdbInvA` / `ParentAdjA`, `DelInvP1`, `IrqRunA`, and +`HandlersActiveA` at `w0` and `w1`** (Architect ruling F1: `BootMdb1.lean` untouched). + +* **`w1Par`** (F1): the true post-`create_root_task` parent map (`ts 0 ↦ some (rs 2)`, `ts 1 ↦ some (rs 3)`, + `ts 2 ↦ some (rs 10)`, `none` elsewhere), matching `init_root_thread`'s three `cdt::insert` calls from + revocable root CNode slots. +* **`w1_treeA_par`**: `TreeA bst (w1OS …) (w1G nc) w1Par (w1S …)`. +* **`w1_mdbInvA`** / **`w1_parentAdjA`**, **`w1_delInvP1`**, **`w1_irqRunA`**, **`w1_handlersActiveA`** + (and their `w0` counterparts `w0_mdbInvA`, `w0_parentAdjA`, `w0_delInvP1`, `w0_irqRunA`, `w0_handlersActiveA`). + +No named premise. New P1 module; zero edits to `BootMdb1.lean` or any other existing file. +-/ +import Fl4ProofsA64.P1.BootSysL1 +import Fl4ProofsA64.P1.MdbInvA +import Fl4ProofsA64.P1.MdbIrqInvA +import Fl4ProofsA64.P1.MdbDelInvA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P1 +open fl4_core_a64 Fl4ProofsA64.P0.Consts Fl4Proofs.P3 + +local notation "wa" => core.num.U64.wrapping_add + +namespace W7 +namespace R +namespace ST +namespace BootWit + +noncomputable section + +/-! ## `w0` boot witnesses (all `bootOS` slots `Null`, empty ghost `[]`) -/ + +section w0 +variable {kb ib r now mt : U64} {nc : Nat} (P : BootLay kb ib nc) +include P + +theorem w0_mdbInvA : MdbInvA bst [] (fun _ => none) (bootS kb ib nc r now mt) where + orig _ _ h := by cases h + desc _ _ h := (Mdb.not_desc_of_none rfl h).elim + after _ hy := by simp at hy + chunk _ hch := by cases hch + badges _ hy := by simp at hy + utrev _ hx := by simp at hx + self _ hx := by simp at hx + +theorem w0_parentAdjA : ParentAdjA bst [] (fun _ => none) (bootS kb ib nc r now mt) := + (w0_mdbInvA P).parentAdj + +theorem w0_delInvP1 (isTcb : U64 → Prop) (isCnode : U64 → Nat → Prop) : + DelInvP1 bst isTcb isCnode (bootOS kb ib nc) (bootS kb ib nc r now mt) := + DelInvP1.empty (boot_null P) + +theorem w0_irqRunA : IrqRunA bst [] (bootS kb ib nc r now mt) := IrqRunA.empty + +theorem w0_handlersActiveA : HandlersActiveA bst [] (bootS kb ib nc r now mt) := HandlersActiveA.empty + +end w0 + +/-! ## `w1` true boot parent map `w1Par` (F1) -/ + +/-- **The true `w1` parent map** (Architect ruling F1): the three `cdt::insert` pairs in `init_root_thread`. -/ +def w1Par (x : U64) : Option U64 := + if x = ts 0#u64 then some (rs 2) + else if x = ts 1#u64 then some (rs 3) + else if x = ts 2#u64 then some (rs 10) + else none + +private theorem ts0_ne_ts1 : ts 0#u64 ≠ ts 1#u64 := by decide +private theorem ts0_ne_ts2 : ts 0#u64 ≠ ts 2#u64 := by decide +private theorem ts1_ne_ts2 : ts 1#u64 ≠ ts 2#u64 := by decide + +theorem w1Par_ts0 : w1Par (ts 0#u64) = some (rs 2) := rfl +theorem w1Par_ts1 : w1Par (ts 1#u64) = some (rs 3) := by unfold w1Par; rw [if_neg ts0_ne_ts1.symm, if_pos rfl] +theorem w1Par_ts2 : w1Par (ts 2#u64) = some (rs 10) := by + unfold w1Par; rw [if_neg ts0_ne_ts2.symm, if_neg ts1_ne_ts2.symm, if_pos rfl] + +theorem w1Par_rs {i : Nat} (hi : i < 8192) : w1Par (rs i) = none := by + unfold w1Par + rw [if_neg (rs_ne_ts hi (by simp)), if_neg (rs_ne_ts hi (by simp)), if_neg (rs_ne_ts hi (by simp))] + +theorem w1Par_some_iff {y x : U64} : + w1Par y = some x ↔ (y = ts 0#u64 ∧ x = rs 2) ∨ (y = ts 1#u64 ∧ x = rs 3) ∨ (y = ts 2#u64 ∧ x = rs 10) := by + unfold w1Par + by_cases h0 : y = ts 0#u64 + · subst h0; simp [ts0_ne_ts1, ts0_ne_ts2, eq_comm] + · by_cases h1 : y = ts 1#u64 + · subst h1; simp [ts0_ne_ts1.symm, ts1_ne_ts2, eq_comm] + · by_cases h2 : y = ts 2#u64 + · subst h2; simp [ts0_ne_ts2.symm, ts1_ne_ts2.symm, eq_comm] + · simp [h0, h1, h2] + +theorem w1Par_desc_iff {y x : U64} : + Mdb.Desc w1Par y x ↔ (y = ts 0#u64 ∧ x = rs 2) ∨ (y = ts 1#u64 ∧ x = rs 3) ∨ (y = ts 2#u64 ∧ x = rs 10) := by + constructor + · intro h + induction h with + | single h1 => exact w1Par_some_iff.1 h1 + | tail _ h2 ih => + rcases ih with ⟨-, rfl⟩ | ⟨-, rfl⟩ | ⟨-, rfl⟩ + · rw [w1Par_rs (by omega)] at h2; cases h2 + · rw [w1Par_rs (by omega)] at h2; cases h2 + · rw [w1Par_rs (by omega)] at h2; cases h2 + · intro h; exact Relation.TransGen.single (w1Par_some_iff.2 h) + +theorem w1_mdbOK_par {nc : Nat} (hnc : nc ≤ 128) : Mdb.MdbOK w1Par (w1G nc) where + nodup := w1G_nodup hnc + par_mem y x h := by + rcases w1Par_some_iff.1 h with ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ <;> + exact ⟨w1G_mem.2 (Or.inl (by simp)), w1G_mem.2 (Or.inl (by simp))⟩ + seg ch hch A u B hsp := by + simp only [w1G, List.mem_append, List.mem_cons, List.mem_nil_iff, or_false, List.mem_map] at hch + rcases hch with (rfl | rfl | rfl) | ⟨i, hi, rfl⟩ + · -- `ch = [rs 2, ts 0]` + rcases A with _ | ⟨a, A⟩ + · simp only [List.nil_append, List.cons.injEq] at hsp + obtain ⟨rfl, rfl⟩ := hsp + refine ⟨[ts 0#u64], [], rfl, fun v => ?_⟩ + rw [w1Par_desc_iff] + have h23 : rs 2 ≠ rs 3 := fun e => by have := rs_inj (by omega) (by omega) e; omega + have h210 : rs 2 ≠ rs 10 := fun e => by have := rs_inj (by omega) (by omega) e; omega + simp [h23, h210] + · rcases A with _ | ⟨a2, A⟩ + · simp only [List.cons_append, List.nil_append, List.cons.injEq] at hsp + obtain ⟨-, rfl, rfl⟩ := hsp + refine ⟨[], [], rfl, fun v => ?_⟩ + rw [w1Par_desc_iff] + have h0 : ts 0#u64 ≠ rs 2 := (rs_ne_ts (by omega) (by simp)).symm + have h1 : ts 0#u64 ≠ rs 3 := (rs_ne_ts (by omega) (by simp)).symm + have h2 : ts 0#u64 ≠ rs 10 := (rs_ne_ts (by omega) (by simp)).symm + simp [h0, h1, h2] + · simp at hsp + · -- `ch = [rs 3, ts 1]` + rcases A with _ | ⟨a, A⟩ + · simp only [List.nil_append, List.cons.injEq] at hsp + obtain ⟨rfl, rfl⟩ := hsp + refine ⟨[ts 1#u64], [], rfl, fun v => ?_⟩ + rw [w1Par_desc_iff] + have h32 : rs 3 ≠ rs 2 := fun e => by have := rs_inj (by omega) (by omega) e; omega + have h310 : rs 3 ≠ rs 10 := fun e => by have := rs_inj (by omega) (by omega) e; omega + simp [h32, h310] + · rcases A with _ | ⟨a2, A⟩ + · simp only [List.cons_append, List.nil_append, List.cons.injEq] at hsp + obtain ⟨-, rfl, rfl⟩ := hsp + refine ⟨[], [], rfl, fun v => ?_⟩ + rw [w1Par_desc_iff] + have h0 : ts 1#u64 ≠ rs 2 := (rs_ne_ts (by omega) (by simp)).symm + have h1 : ts 1#u64 ≠ rs 3 := (rs_ne_ts (by omega) (by simp)).symm + have h2 : ts 1#u64 ≠ rs 10 := (rs_ne_ts (by omega) (by simp)).symm + simp [h0, h1, h2] + · simp at hsp + · -- `ch = [rs 10, ts 2]` + rcases A with _ | ⟨a, A⟩ + · simp only [List.nil_append, List.cons.injEq] at hsp + obtain ⟨rfl, rfl⟩ := hsp + refine ⟨[ts 2#u64], [], rfl, fun v => ?_⟩ + rw [w1Par_desc_iff] + have h102 : rs 10 ≠ rs 2 := fun e => by have := rs_inj (by omega) (by omega) e; omega + have h103 : rs 10 ≠ rs 3 := fun e => by have := rs_inj (by omega) (by omega) e; omega + simp [h102, h103] + · rcases A with _ | ⟨a2, A⟩ + · simp only [List.cons_append, List.nil_append, List.cons.injEq] at hsp + obtain ⟨-, rfl, rfl⟩ := hsp + refine ⟨[], [], rfl, fun v => ?_⟩ + rw [w1Par_desc_iff] + have h0 : ts 2#u64 ≠ rs 2 := (rs_ne_ts (by omega) (by simp)).symm + have h1 : ts 2#u64 ≠ rs 3 := (rs_ne_ts (by omega) (by simp)).symm + have h2 : ts 2#u64 ≠ rs 10 := (rs_ne_ts (by omega) (by simp)).symm + simp [h0, h1, h2] + · simp at hsp + · -- `ch = [rs i]`, `i ∈ w1Single nc` + obtain ⟨h1, -, h3, h4, h5⟩ := w1Single_mem.1 hi + rcases A with _ | ⟨a, A⟩ + · simp only [List.nil_append, List.cons.injEq] at hsp + obtain ⟨rfl, rfl⟩ := hsp + refine ⟨[], [], rfl, fun v => ?_⟩ + rw [w1Par_desc_iff] + have hi2 : rs i ≠ rs 2 := fun e => h3 (rs_inj (by omega) (by omega) e) + have hi3 : rs i ≠ rs 3 := fun e => h4 (rs_inj (by omega) (by omega) e) + have hi10 : rs i ≠ rs 10 := fun e => h5 (rs_inj (by omega) (by omega) e) + simp [hi2, hi3, hi10] + · simp at hsp + +/-! ## Pure `capOf` helpers (independent of `BootLay1`) -/ + +private theorem rev_flag_true : ((bwv (3 * P48b) &&& 281474976710656#u64) != 0#u64) = true := by decide +private theorem rev_rt0_true : ((bwv (3 * P48b + RTb % P48b) &&& 281474976710656#u64) != 0#u64) = true := by decide +private theorem rev_rt1_true : ((bwv (3 * P48b + RTb % P48b + 32) &&& 281474976710656#u64) != 0#u64) = true := by decide +private theorem rev_rt2_true : ((bwv (3 * P48b + RTb % P48b + 64) &&& 281474976710656#u64) != 0#u64) = true := by decide + +private theorem capOf_no_badged (nc i : Nat) : isBadgedA (capOf nc i) = false := by + capsplit (simp [fixC, tailC, frC, isBadgedA]) + +private theorem capOf_no_irq (nc i : Nat) (j : U64) : capOf nc i ≠ .IrqHandler j := by + capsplit (intro h; simp [fixC, tailC, frC] at h) + +private theorem frame_small_bits : capability.FrameSize.Small.bits = ok 12#u64 := rfl + +private theorem sraA_small_frame (ptr r1 r2 : U64) (d1 d2 m1 m2 e1 e2 : Bool) (a1 a2 v1 v2 : U64) + (hno : ptr.val + 4095 < 2 ^ 64) : + sraA (.Frame ptr .Small r1 d1 m1 e1 a1 v1) (.Frame ptr .Small r2 d2 m2 e2 a2 v2) = true := by + apply sraA_of_eq + unfold objects.same_region_as + dsimp only + simp only [frame_small_bits, mskA_eq 12#u64, lift, bind_ok, le_refl, ↓reduceIte, ge_iff_le] + have hm : (mskA 12#u64).val = 4095 := by rw [mskA_val (by decide)]; decide + have hle : ptr ≤ wa ptr (mskA 12#u64) := by + change ptr.val ≤ (ptr.val + (mskA 12#u64).val) % 2 ^ 64 + rw [hm, Nat.mod_eq_of_lt hno]; omega + simp [hle] + +private theorem sraA_ut_self (p b i : U64) (d : Bool) (hb : b.val < 64) + (hno : p.val + (2 ^ b.val - 1) < 2 ^ 64) : + sraA (.Untyped p b i d) (.Untyped p b i d) = true := by + rw [sraA_ut] + have hp : physA (.Untyped p b i d) = true := by + have h := physA_eq (.Untyped p b i d); simp only [objects.cap_is_physical] at h; exact (okinj h).symm + have hptr : ptrA (.Untyped p b i d) = p := by + have h := ptrA_eq (.Untyped p b i d); simp only [capability.Cap.ptr] at h; exact (okinj h).symm + have hsz : sizeA (.Untyped p b i d) = b := by + have h := sizeA_eq (.Untyped p b i d); simp only [objects.cap_size_bits] at h; exact (okinj h).symm + have hm := mskA_val hb + have hle : p ≤ wa p (mskA b) := by + change p.val ≤ (p.val + (mskA b).val) % 2 ^ 64 + rw [hm, Nat.mod_eq_of_lt hno]; omega + simp [hp, hptr, hsz, hle] + +private theorem sraA_frC (p v : Nat) (hno : p % 2 ^ 64 + 4095 < 2 ^ 64) : + sraA (frC p v) (frC p v) = true := + sraA_small_frame (bwv p) 3#u64 3#u64 false false true true false false 1#u64 1#u64 (bwv v) (bwv v) hno + +private theorem capOf_self_sra (nc i : Nat) (hnc : nc ≤ 128) (h0 : cw0 nc i ≠ 0) : + sraA (capOf nc i) (capOf nc i) = true := by + have hnn : capOf nc i ≠ .Null := fun e => h0 ((capOf_null nc i).1 e) + capsplit (first + | exact absurd rfl hnn + | exact absurd (capOf_big (by omega)) h0 + | (simp only [fixC, tailC]; exact sraA_frC _ _ (by omega)) + | (simp only [fixC, tailC]; exact sraA_ut_self _ _ _ _ (by decide) (by decide)) + | (apply sraA_of_eq; decide) + | (apply sraA_of_eq; simp [objects.same_region_as, fixC, tailC, frC])) + +/-! ## `w1` boot witnesses -/ + +section w1 +variable {kb ib r now mt sl : U64} {nc : Nat} {uw : U64 → U64} (P : BootLay1 kb ib nc) +include P + +set_option hygiene false in +local notation "S1" => w1S kb ib nc r now mt sl uw +set_option hygiene false in +local notation "OS1" => w1OS kb ib nc + +/-- **`UtInvA` at `w1` with `w1Par`**. -/ +theorem w1_utInvA_par : UtInvA bst OS1 w1Par S1 := by + have h0 : UtInvA bst OS1 (fun _ => none) S1 := w1_utInvA P + have hts0 : capA bst S1 (ts 0#u64) = capOf nc 2 := by + have hc := cap_rt (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) P (i := 0#u64) (by simp) + simpa [cap2] using hc + have hts1 : capA bst S1 (ts 1#u64) = capOf nc 3 := by + have hc := cap_rt (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) P (i := 1#u64) (by simp) + simpa [cap3] using hc + have hts2 : capA bst S1 (ts 2#u64) = bufC := by + have hc := cap_rt (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) P (i := 2#u64) (by simp) + simpa using hc + refine ⟨h0.reg, fun x hx p b i d hcx y hd => ?_, h0.noov, h0.excl, h0.fib, + (fun x hx p b i d hcx y hy hne hp hnd => by + rcases h0.drange x hx p b i d hcx y hy hne hp hnd with h | h <;> + (cases h with | single h1 => cases h1 | tail _ h1 => cases h1)), + fun x y hd hu => ?_⟩ + · -- `x = rs (52 + nc)` cannot be `rs 2`, `rs 3`, or `rs 10`. + obtain ⟨rfl, -⟩ := w1_ut P hx hcx + have hnc := P.w0.nc128 + rcases w1Par_desc_iff.1 hd with ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ <;> + · have := rs_inj (by omega) (by omega) e; omega + · -- `x ∈ {ts 0, ts 1, ts 2}`, none of which holds an `Untyped` cap. + rcases w1Par_desc_iff.1 hd with ⟨rfl, -⟩ | ⟨rfl, -⟩ | ⟨rfl, -⟩ + · rw [hts0, cap2] at hu; cases hu + · rw [hts1, cap3] at hu; cases hu + · rw [hts2] at hu; cases hu + +private theorem w1_cap_rs {i : Nat} (hi : i < 8192) : capA bst S1 (rs i) = capOf nc i := + cap_root P hi (rs_val hi) + +private theorem w1_cap_ts0 : capA bst S1 (ts 0#u64) = capOf nc 2 := by + have hc := cap_rt (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) P (i := 0#u64) (by simp) + simpa [cap2] using hc + +private theorem w1_cap_ts1 : capA bst S1 (ts 1#u64) = capOf nc 3 := by + have hc := cap_rt (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) P (i := 1#u64) (by simp) + simpa [cap3] using hc + +private theorem w1_cap_ts2 : capA bst S1 (ts 2#u64) = bufC := by + have hc := cap_rt (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) P (i := 2#u64) (by simp) + simpa using hc + +/-- **`TreeA` at `w1` with `w1Par`** (Architect ruling F1). -/ +theorem w1_treeA_par : TreeA bst OS1 (w1G nc) w1Par S1 := + ⟨w1_mdbLk P, w1_mdbOK_par P.w0.nc128, w1_utInvA_par P⟩ + +private theorem w1_rev_rs {i : Nat} (hi : i < 8192) (hcw : cw0 nc i ≠ 0) : revA bst S1 (rs i) = true := by + unfold revA + rw [rs_md P hi (k := 24) rfl (by omega) (by omega)] + unfold mdbR + rw [if_neg (by omega)] + by_cases h2 : i = 2 + · rw [if_pos h2]; exact rev_rt0_true + · rw [if_neg h2] + by_cases h3 : i = 3 + · rw [if_pos h3]; exact rev_rt1_true + · rw [if_neg h3] + by_cases h10 : i = 10 + · rw [if_pos h10]; exact rev_rt2_true + · rw [if_neg h10]; exact rev_flag_true + +private theorem w1_no_badged {y : U64} (hy : y ∈ OS1) : isBadgedA (capA bst S1 y) = false := by + rcases w1_capS P hy with h | ⟨i, -, rfl, h⟩ | ⟨-, h⟩ | ⟨-, h⟩ | ⟨-, h⟩ | ⟨-, -, -, -, h⟩ <;> rw [h] + · rfl + · exact capOf_no_badged nc i + · exact capOf_no_badged nc 2 + · exact capOf_no_badged nc 3 + · rfl + · rfl + +/-- No `w1` slot holds an `IrqHandler` cap. -/ +theorem w1_no_irq {y : U64} (hy : y ∈ OS1) (j : U64) : capA bst S1 y ≠ .IrqHandler j := by + rcases w1_capS P hy with h | ⟨i, -, rfl, h⟩ | ⟨-, h⟩ | ⟨-, h⟩ | ⟨-, h⟩ | ⟨-, -, -, -, h⟩ <;> rw [h] + · intro e; cases e + · exact capOf_no_irq nc i j + · exact capOf_no_irq nc 2 j + · exact capOf_no_irq nc 3 j + · intro e; cases e + · intro e; cases e + +/-- No `w1` slot holds a `Zombie` cap. -/ +theorem w1_no_zombie {y : U64} (hy : y ∈ OS1) (p zt n : U64) : capA bst S1 y ≠ .Zombie p zt n := by + rcases w1_capS P hy with h | ⟨i, -, rfl, h⟩ | ⟨-, h⟩ | ⟨-, h⟩ | ⟨-, h⟩ | ⟨-, -, -, -, h⟩ <;> rw [h] + · intro e; cases e + · exact (rootK_of nc i).nz p zt n + · exact (rootK_of nc 2).nz p zt n + · exact (rootK_of nc 3).nz p zt n + · exact rootK_buf.nz p zt n + · intro e; cases e + +/-- **`MdbInvA` at `w1` with `w1Par`**. -/ +theorem w1_mdbInvA : MdbInvA bst (w1G nc) w1Par S1 := by + have hnc := P.w0.nc128 + have hLk : MdbLk bst OS1 (w1G nc) S1 := w1_mdbLk P + have hmemOS : ∀ x ∈ (w1G nc).flatten, x ∈ OS1 := fun x hx => ((hLk.members x).1 hx).1 + refine ⟨fun c p h => ?_, fun x y h => ?_, fun y hy hn x hx hnd => ?_, fun ch hch => ?_, + fun y hy _ hb => ?_, fun x hx hu => ?_, fun x hx _ => ?_⟩ + · -- `orig` + rcases w1Par_some_iff.1 h with ⟨-, rfl⟩ | ⟨-, rfl⟩ | ⟨-, rfl⟩ + · exact w1_rev_rs P (by omega) (cw0_2 nc) + · exact w1_rev_rs P (by omega) (cw0_3 nc) + · exact w1_rev_rs P (by omega) (cw0_10 nc) + · -- `desc` + rcases w1Par_desc_iff.1 h with ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ + · unfold isPMA isPFA + rw [w1_rev_rs P (by omega) (cw0_2 nc), w1_cap_rs P (by omega), w1_cap_ts0 P, cap2] + have hs : sraA (capOf nc 2) (capOf nc 2) = true := capOf_self_sra nc 2 hnc (cw0_2 nc) + rw [cap2] at hs + simp [hs, isBadgedA] + · unfold isPMA isPFA + rw [w1_rev_rs P (by omega) (cw0_3 nc), w1_cap_rs P (by omega), w1_cap_ts1 P, cap3] + have hs : sraA (capOf nc 3) (capOf nc 3) = true := capOf_self_sra nc 3 hnc (cw0_3 nc) + rw [cap3] at hs + simp [hs, isBadgedA] + · unfold isPMA isPFA + rw [w1_rev_rs P (by omega) (cw0_10 nc), w1_cap_rs P (by omega), w1_cap_ts2 P] + have h10 : capOf nc 10 = (fixC 10).1 := capOf_lo (by omega) + rw [h10] + have hs : sraA (fixC 10).1 bufC = true := by + simp only [fixC, frC, bufC] + exact sraA_small_frame _ _ _ _ _ _ _ _ _ _ _ _ _ (by decide) + rw [hs] + simp [fixC, frC, isBadgedA] + · -- `after`: only `rs 2`, `rs 3`, `rs 10` have `NxA ≠ 0`, and each has `Desc w1Par (NxA y) y`! + rcases w1G_mem.1 hy with hy6 | ⟨i, hi, rfl⟩ + · simp only [List.mem_cons, List.not_mem_nil, or_false] at hy6 + rcases hy6 with rfl | rfl | rfl | rfl | rfl | rfl + · rw [nx_2 P] at hnd + rcases hx with rfl | hx + · exact absurd (w1Par_desc_iff.2 (Or.inl ⟨rfl, rfl⟩)) hnd + · exfalso; rcases w1Par_desc_iff.1 hx with ⟨e, _⟩ | ⟨e, _⟩ | ⟨e, _⟩ <;> + exact rs_ne_ts (by omega) (by simp) e + · exact absurd (ts_nx P (by simp)) hn + · rw [nx_3 P] at hnd + rcases hx with rfl | hx + · exact absurd (w1Par_desc_iff.2 (Or.inr (Or.inl ⟨rfl, rfl⟩))) hnd + · exfalso; rcases w1Par_desc_iff.1 hx with ⟨e, _⟩ | ⟨e, _⟩ | ⟨e, _⟩ <;> + exact rs_ne_ts (by omega) (by simp) e + · exact absurd (ts_nx P (by simp)) hn + · rw [nx_10 P] at hnd + rcases hx with rfl | hx + · exact absurd (w1Par_desc_iff.2 (Or.inr (Or.inr ⟨rfl, rfl⟩))) hnd + · exfalso; rcases w1Par_desc_iff.1 hx with ⟨e, _⟩ | ⟨e, _⟩ | ⟨e, _⟩ <;> + exact rs_ne_ts (by omega) (by simp) e + · exact absurd (ts_nx P (by simp)) hn + · exact absurd (nx_single P hi) hn + · -- `chunk`: every chain has length ≤ 2, so `ChunkL` is trivially true. + have hlen := w1G_len ch hch + rcases ch with _ | ⟨a, _ | ⟨b, _ | ⟨c, L⟩⟩⟩ + · trivial + · exact ⟨trivial, trivial⟩ + · simp [Mdb.ChunkL, Mdb.CA] + · simp at hlen + · -- `badges`: no badged cap in `w1`. + rw [w1_no_badged P (hmemOS y hy)] at hb; cases hb + · -- `utrev`: only `rs (52 + nc)` holds `Untyped`. + have hxu : ∃ p b j d, capA bst S1 x = .Untyped p b j d := by + cases hc : capA bst S1 x <;> rw [hc] at hu <;> try cases hu + exact ⟨_, _, _, _, rfl⟩ + obtain ⟨p, b, j, d, hc⟩ := hxu + obtain ⟨rfl, -⟩ := w1_ut P (hmemOS x hx) hc + have hcw : cw0 nc (52 + nc) ≠ 0 := by + intro h0 + have e := (capOf_null nc (52 + nc)).2 h0 + rw [w1_cap_rs P (by omega), e] at hc; cases hc + exact w1_rev_rs P (by omega) hcw + · -- `self` + unfold srMA + rcases w1G_mem.1 hx with hx6 | ⟨i, hi, rfl⟩ + · simp only [List.mem_cons, List.not_mem_nil, or_false] at hx6 + rcases hx6 with rfl | rfl | rfl | rfl | rfl | rfl + · rw [w1_cap_rs P (by omega)]; exact capOf_self_sra nc 2 hnc (cw0_2 nc) + · rw [w1_cap_ts0 P]; exact capOf_self_sra nc 2 hnc (cw0_2 nc) + · rw [w1_cap_rs P (by omega)]; exact capOf_self_sra nc 3 hnc (cw0_3 nc) + · rw [w1_cap_ts1 P]; exact capOf_self_sra nc 3 hnc (cw0_3 nc) + · rw [w1_cap_rs P (by omega)]; exact capOf_self_sra nc 10 hnc (cw0_10 nc) + · rw [w1_cap_ts2 P]; simp only [bufC]; exact sraA_small_frame _ _ _ _ _ _ _ _ _ _ _ _ _ (by decide) + · obtain ⟨h1, h2, -⟩ := w1Single_mem.1 hi + rw [w1_cap_rs P (by omega)] + exact capOf_self_sra nc i hnc h2 + +/-- **`ParentAdjA` at `w1` with `w1Par`**. -/ +theorem w1_parentAdjA : ParentAdjA bst (w1G nc) w1Par S1 := (w1_mdbInvA P).parentAdj + +/-- **`DelInvP1` at `w1`** (any `(isTcb, isCnode)`). -/ +theorem w1_delInvP1 (isTcb : U64 → Prop) (isCnode : U64 → Nat → Prop) : DelInvP1 bst isTcb isCnode OS1 S1 := + DelInvP1.of_no_zombie fun y hy p zt n => w1_no_zombie P hy p zt n + +/-- **`IrqRunA` at `w1`**. -/ +theorem w1_irqRunA : IrqRunA bst (w1G nc) S1 := by + have hLk : MdbLk bst OS1 (w1G nc) S1 := w1_mdbLk P + exact IrqRunA.of_no_irq fun y hy i => w1_no_irq P ((hLk.members y).1 hy).1 i + +/-- **`HandlersActiveA` at `w1`**. -/ +theorem w1_handlersActiveA : HandlersActiveA bst (w1G nc) S1 := by + have hLk : MdbLk bst OS1 (w1G nc) S1 := w1_mdbLk P + exact HandlersActiveA.of_no_irq fun y hy i => w1_no_irq P ((hLk.members y).1 hy).1 i + +end w1 + +end + +end BootWit +end ST +end R +end W7 + +end Fl4ProofsA64.P1 diff --git a/proofs/Fl4ProofsA64/P1/MdbDelIn.lean b/proofs/Fl4ProofsA64/P1/MdbDelIn.lean index 06d48fa65..8fac572ad 100644 --- a/proofs/Fl4ProofsA64/P1/MdbDelIn.lean +++ b/proofs/Fl4ProofsA64/P1/MdbDelIn.lean @@ -134,7 +134,7 @@ theorem vsk_sts {kb : U64} (hR : VrOK Ω Sg Scs Z OS Vc Ub kb Pt) {s s' : S} /-- **The current core's node words keep every `VsR` word** (off `Ω`). -/ theorem vsk_node {kb : U64} (hR : VrOK Ω Sg Scs Z OS Vc Ub kb Pt) {s s' : S} (q : SchedQueuesInvX st T Cs Ω H X s Q Rq) (hw : WritesIn st s s' (NodeW st s)) : VsKeep st kb Pt s s' := - VsKeep.of_w hR.al hw fun b hb ⟨o, ho, _, e⟩ => + VsKeep.of_w hR.al hw fun b hb ⟨o, ho, _, _, e⟩ => out_ne_node (q.omega _ (hR.om b hb)) q.cur (q.nodes _ q.cur) ho (by rw [e]) end st diff --git a/proofs/Fl4ProofsA64/P1/MdbDelInvA.lean b/proofs/Fl4ProofsA64/P1/MdbDelInvA.lean new file mode 100644 index 000000000..eab3a39de --- /dev/null +++ b/proofs/Fl4ProofsA64/P1/MdbDelInvA.lean @@ -0,0 +1,205 @@ +/- +P1 (aarch64, A64-P1-MDB wave 3): **zombie cap validity `DelInvP1` / `ZOKP1`** +(x86 counterpart: `Fl4Proofs.P3.CnDelInv` `ZOK` / `DelInv.zwf`; l4v source: `Invariants_AI.thy` `valid_cap` +for `ZombieCap`, `well_formed_cnode_n`, `tcb_at`). + +* **`ZOKP1 isTcb isCnode p zt n`** (Architect ruling F3, parametric over `(isTcb, isCnode)`): + a TCB zombie (`zt = 65`) names a TCB (`isTcb p`) with `n ≤ 7` (`tcbSlotsA`); a CNode zombie (`zt ≤ 64`) + names a CNode of radix `zt` (`isCnode p zt`) with `n ≤ 2 ^ zt`. + At Stage-3 merge, instantiating with `(fun p => th p = some .tcb, fun p r => th p = some (.cnode r))` is + definitionally equal to CNode's `ZOKA th p zt n` (`CnodeDelInvA.lean`), and `DelInvP1` directly implies + `DelInvA st th s`. +* **`DelInvH st H OS s`**: P1 heap specialisation (`H p = some .tcb`, `H p = some (.other (r + 5))`). +* **`CapTcbCn st isTcb isCnode OS s`**: cap-validity precondition on `Thread` and `CNode` caps needed when + `finalise_cap` turns a final `Thread` or `CNode` into a `Zombie`. +* Preservation across every A64 cap-mutating step: `empty`, `of_no_zombie`, `keepO`, `of_kept`, `rename`, + `remove`, `set_zombie_tcb`, `set_zombie_cnode`, `set_zombie_dec`, `recap_non_zombie`, `insert_non_zombie`, + `retype` (monotone `(isTcb, isCnode)`), and `reset` (frame on every existing zombie's `p`, discharged by + `ensure_no_children` + `RngKeyA`). + +No named premise. New P1 module; zero edits to existing files. +-/ +import Fl4ProofsA64.P1.MdbTreeA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P1 +open fl4_core_a64 Fl4ProofsA64.P0.Consts Fl4Proofs.P3 + +section +variable {S : Type _} {st : machine.Store S} [L : LawfulStore st] + +namespace W7 +namespace R +namespace ST + +noncomputable section + +/-! ## Definitions -/ + +/-- **A well-formed zombie**, parametric over `(isTcb, isCnode)` (Architect ruling F3; x86 `ZOK`; defeq to +CNode's `ZOKA th` at `isTcb p := th p = some .tcb`, `isCnode p r := th p = some (.cnode r)`). -/ +def ZOKP1 (isTcb : U64 → Prop) (isCnode : U64 → Nat → Prop) (p zt n : U64) : Prop := + (zt.val = 65 ∧ isTcb p ∧ n.val ≤ 7) ∨ + (zt.val ≤ 64 ∧ isCnode p zt.val ∧ n.val ≤ 2 ^ zt.val) + +/-- **Every `OS` slot's zombie is well formed** (x86 `DelInv.zwf`; implies CNode's `DelInvA st th s`). -/ +def DelInvP1 (st : machine.Store S) [L : LawfulStore st] + (isTcb : U64 → Prop) (isCnode : U64 → Nat → Prop) (OS : Set U64) (s : S) : Prop := + ∀ y ∈ OS, ∀ p zt n, capA st s y = .Zombie p zt n → ZOKP1 isTcb isCnode p zt n + +/-- P1 heap specialisation of `DelInvP1` (strengthens `CnOmega`'s zombie clause). -/ +def DelInvH (st : machine.Store S) [L : LawfulStore st] (H : Heap) (OS : Set U64) (s : S) : Prop := + DelInvP1 st (fun p => H p = some .tcb) (fun p r => H p = some (.other (r + 5))) OS s + +/-- `Thread` and `CNode` cap-validity precondition for `finalise_cap` zombie creation. -/ +def CapTcbCn (st : machine.Store S) [L : LawfulStore st] + (isTcb : U64 → Prop) (isCnode : U64 → Nat → Prop) (OS : Set U64) (s : S) : Prop := + (∀ y ∈ OS, ∀ p, capA st s y = .Thread p → isTcb p) ∧ + (∀ y ∈ OS, ∀ p r g b, capA st s y = .CNode p r g b → r.val ≤ 64 ∧ isCnode p r.val) + +variable {isTcb isTcb' : U64 → Prop} {isCnode isCnode' : U64 → Nat → Prop} {OS : Set U64} + +/-! ## Boot and generic preservation -/ + +theorem DelInvP1.of_no_zombie {s : S} (h : ∀ y ∈ OS, ∀ p zt n, capA st s y ≠ .Zombie p zt n) : + DelInvP1 st isTcb isCnode OS s := + fun y hy p zt n hc => absurd hc (h y hy p zt n) + +theorem DelInvP1.empty {s : S} (h : ∀ y ∈ OS, capA st s y = .Null) : DelInvP1 st isTcb isCnode OS s := + DelInvP1.of_no_zombie fun y hy _ _ _ hc => by rw [h y hy] at hc; cases hc + +/-- Monotonicity in `(isTcb, isCnode)` (used by `untyped::retype`). -/ +theorem ZOKP1.mono {p zt n : U64} (h : ZOKP1 isTcb isCnode p zt n) + (ht : ∀ q, isTcb q → isTcb' q) (hc : ∀ q r, isCnode q r → isCnode' q r) : + ZOKP1 isTcb' isCnode' p zt n := by + rcases h with ⟨h1, h2, h3⟩ | ⟨h1, h2, h3⟩ + · exact Or.inl ⟨h1, ht p h2, h3⟩ + · exact Or.inr ⟨h1, hc p zt.val h2, h3⟩ + +/-- Generic transfer: every zombie after comes from a zombie before at some `OS` slot. -/ +theorem DelInvP1.of_kept {s s' : S} (hD : DelInvP1 st isTcb isCnode OS s) + (h : ∀ y ∈ OS, ∀ p zt n, capA st s' y = .Zombie p zt n → + ∃ y0 ∈ OS, capA st s y0 = .Zombie p zt n) : + DelInvP1 st isTcb isCnode OS s' := fun y hy p zt n hc => by + obtain ⟨y0, hy0, hc0⟩ := h y hy p zt n hc + exact hD y0 hy0 p zt n hc0 + +theorem DelInvP1.keepO {s s' : S} (hD : DelInvP1 st isTcb isCnode OS s) (k : OSKeep st OS s s') : + DelInvP1 st isTcb isCnode OS s' := + hD.of_kept fun y hy _ _ _ hc => ⟨y, hy, by rw [← k.capA hy]; exact hc⟩ + +/-! ## Primitive cap operations -/ + +/-- `cdt::move_cap`, `cdt::swap`, `cdt::cap_swap_for_delete`: any permutation `e` of `OS` preserving zombies. -/ +theorem DelInvP1.rename {s s' : S} (hD : DelInvP1 st isTcb isCnode OS s) (e : U64 → U64) + (he : ∀ y ∈ OS, e y ∈ OS) + (hc : ∀ y ∈ OS, ∀ p zt n, capA st s' y = .Zombie p zt n → capA st s (e y) = .Zombie p zt n) : + DelInvP1 st isTcb isCnode OS s' := + hD.of_kept fun y hy p zt n h => ⟨e y, he y hy, hc y hy p zt n h⟩ + +/-- `cdt::empty_slot`: slot `x` becomes `Null`, other `OS` slots keep their caps. -/ +theorem DelInvP1.remove {s s' : S} (hD : DelInvP1 st isTcb isCnode OS s) {x : U64} + (hx : capA st s' x = .Null) (hoth : ∀ y ∈ OS, y ≠ x → capA st s' y = capA st s y) : + DelInvP1 st isTcb isCnode OS s' := + hD.of_kept fun y hy p zt n h => by + by_cases e : y = x + · subst e; rw [hx] at h; cases h + · exact ⟨y, hy, by rw [← hoth y hy e]; exact h⟩ + +/-- In-place `set_cap` of a non-zombie cap (`Untyped` free-index raise, VSpace / EPT / ASIDPool map / unmap). -/ +theorem DelInvP1.recap_non_zombie {s s' : S} (hD : DelInvP1 st isTcb isCnode OS s) {x : U64} {c : capability.Cap} + (hnz : ∀ p zt n, c ≠ .Zombie p zt n) (hx : capA st s' x = c) + (hoth : ∀ y ∈ OS, y ≠ x → capA st s' y = capA st s y) : + DelInvP1 st isTcb isCnode OS s' := + hD.of_kept fun y hy p zt n h => by + by_cases e : y = x + · subst e; rw [hx] at h; exact absurd h (hnz p zt n) + · exact ⟨y, hy, by rw [← hoth y hy e]; exact h⟩ + +/-- `cdt::insert` / `insert_with_cap` / `insert_new_cap` of a non-zombie cap (source may become full untyped). -/ +theorem DelInvP1.insert_non_zombie {s s' : S} (hD : DelInvP1 st isTcb isCnode OS s) {src dest : U64} + {nc : capability.Cap} (hnz : ∀ p zt n, nc ≠ .Zombie p zt n) (hd : capA st s' dest = nc) + (hs : ∀ p zt n, capA st s' src = .Zombie p zt n → capA st s src = .Zombie p zt n) + (hoth : ∀ y ∈ OS, y ≠ src → y ≠ dest → capA st s' y = capA st s y) : + DelInvP1 st isTcb isCnode OS s' := + hD.of_kept fun y hy p zt n h => by + by_cases e1 : y = dest + · subst e1; rw [hd] at h; exact absurd h (hnz p zt n) + · by_cases e2 : y = src + · subst e2; exact ⟨y, hy, hs p zt n h⟩ + · exact ⟨y, hy, by rw [← hoth y hy e2 e1]; exact h⟩ + +/-- `finalise_cap` turning a `Thread p` cap into `Zombie p 65 7` (or any `n ≤ 7`). -/ +theorem DelInvP1.set_zombie_tcb {s s' : S} (hD : DelInvP1 st isTcb isCnode OS s) + (hC : CapTcbCn st isTcb isCnode OS s) {sl p n : U64} (hsl : sl ∈ OS) + (hold : capA st s sl = .Thread p) (hn : n.val ≤ 7) + (hnew : capA st s' sl = .Zombie p 65#u64 n) + (hoth : ∀ y ∈ OS, y ≠ sl → capA st s' y = capA st s y) : + DelInvP1 st isTcb isCnode OS s' := by + intro y hy q zt m hc + by_cases e : y = sl + · subst e; rw [hnew] at hc + injection hc with e1 e2 e3; subst e1 e2 e3 + exact Or.inl ⟨rfl, hC.1 y hy p hold, hn⟩ + · rw [hoth y hy e] at hc; exact hD y hy q zt m hc + +/-- `finalise_cap` turning a `CNode p r g b` cap into `Zombie p r n` with `n ≤ 2^r`. -/ +theorem DelInvP1.set_zombie_cnode {s s' : S} (hD : DelInvP1 st isTcb isCnode OS s) + (hC : CapTcbCn st isTcb isCnode OS s) {sl p r g b n : U64} (hsl : sl ∈ OS) + (hold : capA st s sl = .CNode p r g b) (hn : n.val ≤ 2 ^ r.val) + (hnew : capA st s' sl = .Zombie p r n) + (hoth : ∀ y ∈ OS, y ≠ sl → capA st s' y = capA st s y) : + DelInvP1 st isTcb isCnode OS s' := by + intro y hy q zt m hc + by_cases e : y = sl + · subst e; rw [hnew] at hc + injection hc with e1 e2 e3; subst e1 e2 e3 + obtain ⟨hr, hcn⟩ := hC.2 y hy p r g b hold + exact Or.inr ⟨hr, hcn, hn⟩ + · rw [hoth y hy e] at hc; exact hD y hy q zt m hc + +/-- `reduce_zombie` decrementing a zombie's count (`n' ≤ n`). -/ +theorem DelInvP1.set_zombie_dec {s s' : S} (hD : DelInvP1 st isTcb isCnode OS s) + {sl p zt n n' : U64} (hsl : sl ∈ OS) + (hold : capA st s sl = .Zombie p zt n) (hle : n'.val ≤ n.val) + (hnew : capA st s' sl = .Zombie p zt n') + (hoth : ∀ y ∈ OS, y ≠ sl → capA st s' y = capA st s y) : + DelInvP1 st isTcb isCnode OS s' := by + intro y hy q zt' m hc + by_cases e : y = sl + · subst e; rw [hnew] at hc + injection hc with e1 e2 e3; subst e1 e2 e3 + rcases hD y hy p zt n hold with ⟨h1, h2, h3⟩ | ⟨h1, h2, h3⟩ + · exact Or.inl ⟨h1, h2, le_trans hle h3⟩ + · exact Or.inr ⟨h1, h2, le_trans hle h3⟩ + · rw [hoth y hy e] at hc; exact hD y hy q zt' m hc + +/-- **`untyped::retype`**: `(isTcb, isCnode)` grow monotonically, and new caps are never `Zombie`. -/ +theorem DelInvP1.retype {s s' : S} (hD : DelInvP1 st isTcb isCnode OS s) + (ht : ∀ q, isTcb q → isTcb' q) (hc : ∀ q r, isCnode q r → isCnode' q r) + (hz : ∀ y ∈ OS, ∀ p zt n, capA st s' y = .Zombie p zt n → capA st s y = .Zombie p zt n) : + DelInvP1 st isTcb' isCnode' OS s' := + fun y hy p zt n h => (hD y hy p zt n (hz y hy p zt n h)).mono ht hc + +/-- **`untyped::reset`** (Architect ruling #3): `(isTcb, isCnode)` are unchanged on every existing zombie's +object pointer `p` (discharged by `ensure_no_children` + `RngKeyA`: no zombie lies in the dropped region). -/ +theorem DelInvP1.reset {s s' : S} (hD : DelInvP1 st isTcb isCnode OS s) + (hz : ∀ y ∈ OS, ∀ p zt n, capA st s' y = .Zombie p zt n → capA st s y = .Zombie p zt n) + (ht : ∀ y ∈ OS, ∀ p zt n, capA st s y = .Zombie p zt n → isTcb p → isTcb' p) + (hc : ∀ y ∈ OS, ∀ p zt n, capA st s y = .Zombie p zt n → ∀ r, isCnode p r → isCnode' p r) : + DelInvP1 st isTcb' isCnode' OS s' := by + intro y hy p zt n h + have h0 := hz y hy p zt n h + rcases hD y hy p zt n h0 with ⟨h1, h2, h3⟩ | ⟨h1, h2, h3⟩ + · exact Or.inl ⟨h1, ht y hy p zt n h0 h2, h3⟩ + · exact Or.inr ⟨h1, hc y hy p zt n h0 zt.val h2, h3⟩ + +end + +end ST +end R +end W7 + +end + +end Fl4ProofsA64.P1 diff --git a/proofs/Fl4ProofsA64/P1/MdbInvA.lean b/proofs/Fl4ProofsA64/P1/MdbInvA.lean new file mode 100644 index 000000000..78f44b382 --- /dev/null +++ b/proofs/Fl4ProofsA64/P1/MdbInvA.lean @@ -0,0 +1,352 @@ +/- +P1 (aarch64, A64-P1-MDB wave 1): **the MDB invariant `MdbInvA` and `ParentAdjA`** +(x86 counterpart: `Fl4Proofs.P3.MdbInv`; l4v source: `CSpace_AI.thy` `valid_mdb`). + +* **`MdbInvA st G par s`**: ties the slot bits (`revA`, `fbA`) and caps (`capA`) to the ghost derivation tree + `(G, par)`: `orig`, `desc`, `after`, `chunk`, `badges`, `utrev`, `self`. +* **`ParentAdjA st G par s`**: exact match to CNode consumer `Fl4ProofsA64.P3.ParentAdjA` (`CnodeRevokeA.lean`), + derived by **`MdbInvA.parentAdj`**. +* **`TreeA.tail_no_desc`**: the `NxA u = 0` tail (`NxA st s u = 0#u64 → ¬ ∃ v, Mdb.Desc par v u`), derived from + `Mdb.MdbOK.seg` + `MdbLk`. +* Frame and in-place cap rewrite lemmas: **`MdbInvA.of_same`**, **`MdbInvA.keepO`**, **`MdbInvA.recap_alike`**, + **`MdbInvA.set_zombie`**, **`MdbInvA.set_zombie_dec`**. + +No named premise. New P1 module; zero edits to existing files. +-/ +import Fl4ProofsA64.P1.MdbSemA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P1 +open fl4_core_a64 Fl4ProofsA64.P0.Consts Fl4Proofs.P3 + +section +variable {S : Type _} {st : machine.Store S} [L : LawfulStore st] + +local notation "wa" => core.num.U64.wrapping_add + +namespace W7 +namespace R +namespace ST + +noncomputable section + +/-! ## The MDB invariant and `ParentAdjA` -/ + +/-- **The AArch64 MDB invariant** (x86 `Fl4Proofs.P3.MdbInv`; l4v `valid_mdb`). -/ +structure MdbInvA (st : machine.Store S) [L : LawfulStore st] + (G : List (List U64)) (par : U64 → Option U64) (s : S) : Prop where + orig : ∀ c p, par c = some p → revA st s p = true + desc : ∀ x y, Mdb.Desc par y x → isPMA st s x y = true + after : ∀ y ∈ G.flatten, NxA st s y ≠ 0#u64 → ∀ x, (x = y ∨ Mdb.Desc par y x) → + ¬ Mdb.Desc par (NxA st s y) x → isPMA st s x (NxA st s y) = false + chunk : ∀ ch ∈ G, Mdb.ChunkL (srFA st s) ch + badges : ∀ y ∈ G.flatten, NxA st s y ≠ 0#u64 → isBadgedA (capA st s y) = true → + srMA st s y (NxA st s y) = true → badgeA (capA st s y) ≠ badgeA (capA st s (NxA st s y)) → + badgeA (capA st s (NxA st s y)) ≠ 0#u64 → fbA st s (NxA st s y) = true + utrev : ∀ x ∈ G.flatten, isUtA (capA st s x) = true → revA st s x = true + self : ∀ x ∈ G.flatten, isZbA (capA st s x) = false → srMA st s x x = true + +/-- **`is_mdb_parent_of` at `NEXT` is ghost descent** (exact match to `CnodeRevokeA.ParentAdjA`). -/ +def ParentAdjA (st : machine.Store S) [L : LawfulStore st] + (G : List (List U64)) (par : U64 → Option U64) (s : S) : Prop := + ∀ u ∈ G.flatten, NxA st s u ≠ 0#u64 → + ∃ b, cdt.is_mdb_parent_of st s u (NxA st s u) = ok b ∧ + (b = true ↔ Mdb.Desc par (NxA st s u) u) + +/-- **`ParentAdjA` from `MdbInvA`** (x86 `MdbInv.parentAdj`). -/ +theorem MdbInvA.parentAdj {G : List (List U64)} {par : U64 → Option U64} {s : S} + (hI : MdbInvA st G par s) : ParentAdjA st G par s := by + intro u hu hn + refine ⟨isPMA st s u (NxA st s u), is_mdb_parent_of_eqA s u (NxA st s u), ?_⟩ + by_cases hd : Mdb.Desc par (NxA st s u) u + · exact ⟨fun _ => hd, fun _ => hI.desc _ _ hd⟩ + · have hf := hI.after u hu hn u (Or.inl rfl) hd + exact ⟨fun h => absurd h (by simp [hf]), fun h => absurd h hd⟩ + +/-- **The `next u = 0` tail**: a slot with `NxA u = 0` has no ghost descendant (from `TreeA`). -/ +theorem TreeA.tail_no_desc {T Ω Sg OS : Set U64} {G : List (List U64)} {par : U64 → Option U64} {s : S} + (hO : OSOK T Ω Sg OS) (hT : TreeA st OS G par s) {u : U64} (hn : NxA st s u = 0#u64) : + ¬ ∃ v, Mdb.Desc par v u := by + rintro ⟨v, hv⟩ + obtain ⟨b, -, hb⟩ := Relation.TransGen.tail'_iff.1 hv + have huG : u ∈ G.flatten := (hT.tree.par_mem b u hb).2 + obtain ⟨ch, hch, huc⟩ := List.mem_flatten.1 huG + obtain ⟨A, B, hsplit⟩ := List.append_of_mem huc + obtain ⟨dd, rest, hpost, hd⟩ := hT.tree.seg ch hch A u B hsplit + have hvd := (hd v).1 hv + cases dd with + | nil => simp at hvd + | cons d0 d' => + have e : ch = A ++ u :: d0 :: (d' ++ rest) := by rw [hsplit, hpost]; simp + have hnx := nx_split hT.lk hch e + have hd0G : d0 ∈ G.flatten := List.mem_flatten.2 ⟨ch, hch, by rw [e]; simp⟩ + exact hO.nz d0 ((hT.lk.members d0).1 hd0G).1 (hnx.symm.trans hn) + +/-! ## Helpers on `MdbLk` and `Mdb.MdbOK` -/ + +theorem desc_memA {G : List (List U64)} {par : U64 → Option U64} (hOk : Mdb.MdbOK par G) + {x y : U64} (h : Mdb.Desc par y x) : y ∈ G.flatten ∧ x ∈ G.flatten := by + induction h with + | single h1 => exact hOk.par_mem _ _ h1 + | tail _ h2 ih => exact ⟨ih.1, (hOk.par_mem _ _ h2).2⟩ + +theorem MdbLk.next_memA {OS : Set U64} {G : List (List U64)} {s : S} (hM : MdbLk st OS G s) + {y : U64} (hy : y ∈ G.flatten) (hn : NxA st s y ≠ 0#u64) : NxA st s y ∈ G.flatten := by + obtain ⟨ch, hch, hyc⟩ := List.mem_flatten.1 hy + obtain ⟨A, B, hsp⟩ := List.append_of_mem hyc + cases B with + | nil => + have := hM.last ch hch y (by rw [hsp]; simp) + exact absurd this hn + | cons z R => + have e := nx_split hM hch hsp + rw [e] + exact List.mem_flatten.2 ⟨ch, hch, by rw [hsp]; simp⟩ + +/-! ## Transport across preserved slot contents -/ + +/-- **`MdbInvA` when every chain member keeps its cap, `NEXT`, `rev`, and `fb`.** -/ +theorem MdbInvA.of_same {OS : Set U64} {G : List (List U64)} {par : U64 → Option U64} {s s' : S} + (hM : MdbLk st OS G s) (hOk : Mdb.MdbOK par G) (hI : MdbInvA st G par s) + (hc : ∀ x ∈ G.flatten, capA st s' x = capA st s x) + (hnx : ∀ x ∈ G.flatten, NxA st s' x = NxA st s x) + (hrv : ∀ x ∈ G.flatten, revA st s' x = revA st s x) + (hfb : ∀ x ∈ G.flatten, fbA st s' x = fbA st s x) : MdbInvA st G par s' := by + have hpm : ∀ x y, x ∈ G.flatten → y ∈ G.flatten → isPMA st s' x y = isPMA st s x y := by + intro x y hx hy + unfold isPMA; rw [hrv x hx, hc x hx, hc y hy, hfb y hy] + have hsr : ∀ x y, x ∈ G.flatten → y ∈ G.flatten → srMA st s' x y = srMA st s x y := by + intro x y hx hy + unfold srMA; rw [hc x hx, hc y hy] + refine ⟨fun c p h => ?_, fun x y h => ?_, fun y hy hn x hx hd => ?_, fun ch hch => ?_, + fun y hy hn hb hs hbd hb0 => ?_, fun x hx hu => ?_, fun x hx hz => ?_⟩ + · rw [hrv p (hOk.par_mem _ _ h).2]; exact hI.orig c p h + · obtain ⟨h1, h2⟩ := desc_memA hOk h + rw [hpm x y h2 h1]; exact hI.desc x y h + · rw [hnx y hy] at hn hd ⊢ + have hx' : x ∈ G.flatten := by rcases hx with rfl | hx; exact hy; exact (desc_memA hOk hx).2 + rw [hpm x _ hx' (hM.next_memA hy hn)]; exact hI.after y hy hn x hx hd + · refine Mdb.chunkL_congr (hI.chunk ch hch) (fun u hu v hv => srFA_congr ?_ ?_ ?_) + · exact hsr u v (List.mem_flatten.2 ⟨_, hch, hu⟩) (List.mem_flatten.2 ⟨_, hch, hv⟩) + · rw [hc u (List.mem_flatten.2 ⟨_, hch, hu⟩)] + · rw [hrv u (List.mem_flatten.2 ⟨_, hch, hu⟩)] + · rw [hnx y hy] at hn hs hbd hb0 ⊢ + have hn' := hM.next_memA hy hn + rw [hc y hy] at hb hbd + rw [hc _ hn'] at hbd hb0 + rw [hsr y _ hy hn'] at hs + rw [hfb _ hn'] + exact hI.badges y hy hn hb hs hbd hb0 + · rw [hc x hx] at hu; rw [hrv x hx]; exact hI.utrev x hx hu + · rw [hc x hx] at hz; rw [hsr x x hx hx]; exact hI.self x hx hz + +/-- **`MdbInvA` across an `OSKeep` step** (preemption point, restart, non-MDB writes). -/ +theorem MdbInvA.keepO {OS : Set U64} {G : List (List U64)} {par : U64 → Option U64} {s s' : S} + (hM : MdbLk st OS G s) (hOk : Mdb.MdbOK par G) (hI : MdbInvA st G par s) (k : OSKeep st OS s s') : + MdbInvA st G par s' := by + have hmem : ∀ x ∈ G.flatten, x ∈ OS := fun x hx => ((hM.members x).1 hx).1 + exact hI.of_same hM hOk + (fun x hx => k.capA (hmem x hx)) + (fun x hx => by unfold NxA; rw [(k x (hmem x hx)).2.2.2]) + (fun x hx => revA_congr (k x (hmem x hx)).2.2.2) + (fun x hx => fbA_congr (k x (hmem x hx)).2.2.2) + +/-! ## In-place cap rewrites (links and flags unchanged) -/ + +/-- **In-place cap rewrite keeping `shapeA` and `badgeA`** (untyped free-index raise / reset, VSpace / EPT / +ASIDPool map / unmap / assign). -/ +theorem MdbInvA.recap_alike {OS : Set U64} {G : List (List U64)} {par : U64 → Option U64} {s s' : S} + (hM : MdbLk st OS G s) (hOk : Mdb.MdbOK par G) (hI : MdbInvA st G par s) + (hsh : ∀ x ∈ G.flatten, shapeA (capA st s' x) = shapeA (capA st s x)) + (hbd : ∀ x ∈ G.flatten, badgeA (capA st s' x) = badgeA (capA st s x)) + (hnx : ∀ x ∈ G.flatten, NxA st s' x = NxA st s x) + (hrv : ∀ x ∈ G.flatten, revA st s' x = revA st s x) + (hfb : ∀ x ∈ G.flatten, fbA st s' x = fbA st s x) : MdbInvA st G par s' := by + have hpm : ∀ x y, x ∈ G.flatten → y ∈ G.flatten → isPMA st s' x y = isPMA st s x y := by + intro x y hx hy + unfold isPMA; rw [hrv x hx, hfb y hy] + exact isPFA_congr (hsh x hx) (hbd x hx) (hsh y hy) (hbd y hy) + have hsr : ∀ x y, x ∈ G.flatten → y ∈ G.flatten → srMA st s' x y = srMA st s x y := by + intro x y hx hy + unfold srMA; exact sraA_congr (hsh x hx) (hsh y hy) + refine ⟨fun c p h => ?_, fun x y h => ?_, fun y hy hn x hx hd => ?_, fun ch hch => ?_, + fun y hy hn hb hs hbd' hb0 => ?_, fun x hx hu => ?_, fun x hx hz => ?_⟩ + · rw [hrv p (hOk.par_mem _ _ h).2]; exact hI.orig c p h + · obtain ⟨h1, h2⟩ := desc_memA hOk h + rw [hpm x y h2 h1]; exact hI.desc x y h + · rw [hnx y hy] at hn hd ⊢ + have hx' : x ∈ G.flatten := by rcases hx with rfl | hx; exact hy; exact (desc_memA hOk hx).2 + rw [hpm x _ hx' (hM.next_memA hy hn)]; exact hI.after y hy hn x hx hd + · refine Mdb.chunkL_congr (hI.chunk ch hch) (fun u hu v hv => srFA_congr ?_ ?_ ?_) + · exact hsr u v (List.mem_flatten.2 ⟨_, hch, hu⟩) (List.mem_flatten.2 ⟨_, hch, hv⟩) + · exact isFrA_of_shape (hsh u (List.mem_flatten.2 ⟨_, hch, hu⟩)) + · exact hrv u (List.mem_flatten.2 ⟨_, hch, hu⟩) + · rw [hnx y hy] at hn hs hbd' hb0 ⊢ + have hn' := hM.next_memA hy hn + rw [isBadgedA_of_shape (hsh y hy)] at hb + rw [hbd y hy, hbd _ hn'] at hbd' + rw [hbd _ hn'] at hb0 + rw [hsr y _ hy hn'] at hs + rw [hfb _ hn'] + exact hI.badges y hy hn hb hs hbd' hb0 + · rw [isUtA_of_shape (hsh x hx)] at hu; rw [hrv x hx]; exact hI.utrev x hx hu + · rw [isZbA_of_shape (hsh x hx)] at hz; rw [hsr x x hx hx]; exact hI.self x hx hz + +/-- **Writing a `Zombie` into a final slot** (`finalise_cap` of final `CNode` / `Thread`, or decrementing a +`Zombie`). Requires that no other member of `G` has `same_region_as` with `sl`'s old cap except `Untyped` +ancestors/peers (which agree on `same_region_as` by `sraA_ut_of_ua`), and `sl` has no descendants. -/ +theorem MdbInvA.set_zombie {OS : Set U64} {G : List (List U64)} {par : U64 → Option U64} {s s' : S} + (hM : MdbLk st OS G s) (hOk : Mdb.MdbOK par G) (hI : MdbInvA st G par s) + {sl p t n : U64} (hsl : sl ∈ G.flatten) + (hnew : capA st s' sl = .Zombie p t n) + (hua : UA (.Zombie p t n) (capA st s sl)) + (hoth : ∀ x ∈ G.flatten, x ≠ sl → capA st s' x = capA st s x) + (hnx : ∀ x ∈ G.flatten, NxA st s' x = NxA st s x) + (hrv : ∀ x ∈ G.flatten, revA st s' x = revA st s x) + (hfb : ∀ x ∈ G.flatten, fbA st s' x = fbA st s x) + -- No other member has `same_region_as` with `sl`'s old cap unless it is an `Untyped`. + (hfin_l : ∀ x ∈ G.flatten, x ≠ sl → isUtA (capA st s x) = false → sraA (capA st s x) (capA st s sl) = false) + -- `sl` has no ghost descendants (from finality: any descendant of CNode/Thread/Zombie would be in its region). + (hnd : ∀ y, ¬ Mdb.Desc par y sl) : MdbInvA st G par s' := by + -- For any `x ≠ sl`, `sraA (capA s' x) (capA s' sl) = sraA (capA s x) (capA s sl)`. + have hsr_r : ∀ x ∈ G.flatten, x ≠ sl → srMA st s' x sl = srMA st s x sl := by + intro x hx hne + unfold srMA + rw [hoth x hx hne, hnew] + cases hcx : capA st s x + case Untyped pu bu iu du => exact sraA_ut_of_ua pu bu iu du hua + all_goals + have hnu : isUtA (capA st s x) = false := by rw [hcx]; rfl + have h0 := hfin_l x hx hne hnu + rw [hcx] at h0 + rw [h0] + apply sraA_of_eq + simp [objects.same_region_as] + -- `sl`'s own cap has `sraA = false` on the left, `isBadgedA = false`, `isFrA = false`, `isUtA = false`. + have hsr_sl : ∀ y, srMA st s' sl y = false := by + intro y; unfold srMA; rw [hnew]; exact sraA_zombie_left p t n _ + have hpm_sl : ∀ y, isPMA st s' sl y = false := by + intro y; unfold isPMA isPFA; rw [hnew, sraA_zombie_left]; simp + have hpm_r : ∀ x, x ∈ G.flatten → x ≠ sl → isPMA st s' x sl = isPMA st s x sl := by + intro x hx hne + unfold isPMA isPFA + rw [hrv x hx, hfb sl hsl, hoth x hx hne] + have hsr := hsr_r x hx hne + unfold srMA at hsr + rw [hoth x hx hne] at hsr + rw [hsr] + by_cases hbx : isBadgedA (capA st s x) = true + · have hnu : isUtA (capA st s x) = false := by + cases hcx : capA st s x <;> rw [hcx] at hbx <;> simp [isBadgedA] at hbx <;> rfl + have h0 := hfin_l x hx hne hnu + simp [h0] + · simp [hbx] + have hpm_oo : ∀ x y, x ∈ G.flatten → y ∈ G.flatten → x ≠ sl → y ≠ sl → isPMA st s' x y = isPMA st s x y := by + intro x y hx hy hxn hyn + unfold isPMA; rw [hrv x hx, hfb y hy, hoth x hx hxn, hoth y hy hyn] + refine ⟨fun c q h => ?_, fun x y h => ?_, fun y hy hn x hx hd => ?_, fun ch hch => ?_, + fun y hy hn hb hs hbd hb0 => ?_, fun x hx hu => ?_, fun x hx hz => ?_⟩ + · rw [hrv q (hOk.par_mem _ _ h).2]; exact hI.orig c q h + · obtain ⟨h1, h2⟩ := desc_memA hOk h + have hxn : x ≠ sl := fun e => hnd y (e ▸ h) + by_cases hyn : y = sl + · subst hyn; rw [hpm_r x h2 hxn]; exact hI.desc x y h + · rw [hpm_oo x y h2 h1 hxn hyn]; exact hI.desc x y h + · rw [hnx y hy] at hn hd ⊢ + have hn' := hM.next_memA hy hn + have hx' : x ∈ G.flatten := by rcases hx with rfl | hx; exact hy; exact (desc_memA hOk hx).2 + by_cases hxn : x = sl + · subst hxn; exact hpm_sl _ + · by_cases hnn : NxA st s y = sl + · rw [hnn]; rw [hpm_r x hx' hxn]; rw [← hnn]; exact hI.after y hy hn x hx hd + · rw [hpm_oo x _ hx' hn' hxn hnn]; exact hI.after y hy hn x hx hd + · -- chunk: `Mdb.ChunkL (srFA st s') ch` + have hch_old := hI.chunk ch hch + have hmem_ch : ∀ u ∈ ch, u ∈ G.flatten := fun u hu => List.mem_flatten.2 ⟨ch, hch, hu⟩ + -- Prove by induction on `ch` using `Mdb.chunkL_congr`? No, `srFA` at `u = sl` changes to `false`! + -- Instead prove directly by induction on any sublist of `ch` whose elements are in `G.flatten` and `Nodup`. + have hnd_ch : ch.Nodup := (List.nodup_flatten.1 hOk.nodup).1 ch hch + suffices ∀ L : List U64, L.Nodup → (∀ u ∈ L, u ∈ G.flatten) → Mdb.ChunkL (srFA st s) L → + Mdb.ChunkL (srFA st s') L from this ch hnd_ch hmem_ch hch_old + intro L + induction L with + | nil => intro _ _ _; trivial + | cons a L ih => + intro hndL hmemL hC + rw [List.nodup_cons] at hndL + refine ⟨?_, ih hndL.2 (fun u hu => hmemL u (List.mem_cons_of_mem a hu)) hC.2⟩ + by_cases has : a = sl + · rw [has] + have hfalse : ∀ v, srFA st s' sl v = false := by + intro v; unfold srFA; rw [hsr_sl v, hnew]; rfl + clear hC ih hndL hmemL has + induction L with + | nil => trivial + | cons v L ihL => exact ⟨fun _ w _ => hfalse w, ihL⟩ + · refine Mdb.ca_congr hC.1 (fun v hv => srFA_congr ?_ ?_ ?_) + · by_cases hvs : v = sl + · subst hvs; exact hsr_r a (hmemL a (List.mem_cons_self ..)) has + · unfold srMA + rw [hoth a (hmemL a (List.mem_cons_self ..)) has, hoth v (hmemL v (List.mem_cons_of_mem a hv)) hvs] + · rw [hoth a (hmemL a (List.mem_cons_self ..)) has] + · exact hrv a (hmemL a (List.mem_cons_self ..)) + · rw [hnx y hy] at hn hs hbd hb0 ⊢ + have hn' := hM.next_memA hy hn + by_cases hys : y = sl + · subst hys; rw [hnew] at hb; cases hb + · by_cases hns : NxA st s y = sl + · rw [hns, hnew] at hb0; exact absurd rfl hb0 + · rw [hoth y hy hys] at hb hbd + rw [hoth _ hn' hns] at hbd hb0 + unfold srMA at hs; rw [hoth y hy hys, hoth _ hn' hns] at hs + rw [hfb _ hn'] + exact hI.badges y hy hn hb hs hbd hb0 + · by_cases hxs : x = sl + · subst hxs; rw [hnew] at hu; cases hu + · rw [hoth x hx hxs] at hu; rw [hrv x hx]; exact hI.utrev x hx hu + · by_cases hxs : x = sl + · subst hxs; rw [hnew] at hz; cases hz + · rw [hoth x hx hxs] at hz + unfold srMA; rw [hoth x hx hxs] + exact hI.self x hx hz + +/-- **Decrementing a `Zombie`'s count** (`reduce_zombie` immediate step): special case of `set_zombie`. -/ +theorem MdbInvA.set_zombie_dec {OS : Set U64} {G : List (List U64)} {par : U64 → Option U64} {s s' : S} + (hM : MdbLk st OS G s) (hOk : Mdb.MdbOK par G) (hI : MdbInvA st G par s) + {sl p t n n' : U64} (hsl : sl ∈ G.flatten) + (hold : capA st s sl = .Zombie p t n) + (hnew : capA st s' sl = .Zombie p t n') + (hoth : ∀ x ∈ G.flatten, x ≠ sl → capA st s' x = capA st s x) + (hnx : ∀ x ∈ G.flatten, NxA st s' x = NxA st s x) + (hrv : ∀ x ∈ G.flatten, revA st s' x = revA st s x) + (hfb : ∀ x ∈ G.flatten, fbA st s' x = fbA st s x) : MdbInvA st G par s' := by + have hua : UA (.Zombie p t n') (capA st s sl) := by + rw [hold] + refine ⟨?_, ?_, ?_, rfl, fun _ _ _ _ h => by cases h⟩ + · have e1 := physA_eq (.Zombie p t n'); have e2 := physA_eq (.Zombie p t n) + simp only [objects.cap_is_physical] at e1 e2; exact (okinj e1).symm.trans (okinj e2) + · have e1 := ptrA_eq (.Zombie p t n'); have e2 := ptrA_eq (.Zombie p t n) + simp only [capability.Cap.ptr] at e1 e2; exact (okinj e1).symm.trans (okinj e2) + · have e1 := sizeA_eq (.Zombie p t n'); have e2 := sizeA_eq (.Zombie p t n) + simp only [objects.cap_size_bits] at e1 e2; exact okinj (e1.symm.trans e2) + refine hI.set_zombie hM hOk hsl hnew hua hoth hnx hrv hfb ?_ ?_ + · intro x hx _ hnu + rw [hold] + cases hcx : capA st s x + case Untyped => rw [hcx] at hnu; cases hnu + all_goals (apply sraA_of_eq; simp [objects.same_region_as]) + · intro y hy + have hp := hI.desc sl y hy + unfold isPMA isPFA at hp + rw [hold, sraA_zombie_left] at hp + simp at hp + +end + +end ST +end R +end W7 + +end + +end Fl4ProofsA64.P1 diff --git a/proofs/Fl4ProofsA64/P1/MdbInvInsA.lean b/proofs/Fl4ProofsA64/P1/MdbInvInsA.lean new file mode 100644 index 000000000..6ec02842d --- /dev/null +++ b/proofs/Fl4ProofsA64/P1/MdbInvInsA.lean @@ -0,0 +1,744 @@ +/- +P1 (aarch64, A64-P1-MDB wave 2): **insertion preservation of `MdbInvA`** +(x86 counterparts: `Fl4Proofs.P3.MdbInvIns`, `Fl4Proofs.P3.CtlIns`, `Fl4Proofs.P3.VsMakePoolMdb`, +`Fl4Proofs.P3.VsSubMdb`, `Fl4Proofs.P3.UntypedMdb`; l4v source: `CSpace_AI.thy` `valid_mdb` across +`cteInsert`, `should_be_parent_of`). + +* **`MdbInvA.sib`** (x86 `MdbInv.sib`): if FL4's parent test from `src` fails for a derived cap, `src` has no + descendants. +* **`InsCtxA`** / **`InsCtxA.inv`** (Architect ruling F2; x86 `MdbInvIns.InsCtx.inv`): derived cap insertion + (`cdt::insert_with_cap` / `cdt::insert` in CNode Copy/Mint, TCB, IPC, SetIRQHandler), updating `par` by + `Function.update par dest (if bI then some src else par src)` (`should_be_parent_of`). +* **`CtlCtxA`** / **`CtlCtxA.inv`** (x86 `CtlIns.CtlCtx.inv`): `IrqControl` issuing a fresh `IrqHandler irq` + cap (`interrupt::invoke_irq_control`), using freshness from `HandlersActiveA.fresh_sra`. +* **`FreshChildCtxA`** / **`FreshChildCtxA.inv`**: inserting a new child `dest` right after a revocable + non-badged `src` (`MakePool`, `DeriveSubframe`, `untyped::retype` `insert_new_cap`). + +No named premise. New P1 module; zero edits to existing files. +-/ +import Fl4ProofsA64.P1.MdbInvOpsA +import Fl4ProofsA64.P1.MdbIns +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P1 +open fl4_core_a64 Fl4ProofsA64.P0.Consts Fl4Proofs.P3 + +section +variable {S : Type _} {st : machine.Store S} [L : LawfulStore st] + +namespace W7 +namespace R +namespace ST + +noncomputable section + +/-! ## Shared insertion helpers -/ + +theorem desc_tail_childA {par : U64 → Option U64} {y x : U64} (h : Mdb.Desc par y x) : ∃ z, par z = some x := by + obtain ⟨b, -, hb⟩ := Relation.TransGen.tail'_iff.1 h; exact ⟨b, hb⟩ + +/-- **The sibling condition** (x86 `MdbInv.sib`): if FL4's parent test from `src` fails for a derived cap, +`src` has no descendants. -/ +theorem MdbInvA.sib {G : List (List U64)} {par : U64 → Option U64} {s : S} (hI : MdbInvA st G par s) + {src : U64} (hsrc : src ∈ G.flatten) (hzb : isZbA (capA st s src) = false) {c : capability.Cap} + (hsh : shapeA c = shapeA (capA st s src)) + (hbd : badgeA c = badgeA (capA st s src) ∨ badgeA (capA st s src) = 0#u64) + (hb : isPFA (revA st s src) (capA st s src) c (revFA c (capA st s src)) = false) : + ∀ y, ¬ Mdb.Desc par y src := by + intro y hy + obtain ⟨z, hz⟩ := desc_tail_childA hy + have hr := hI.orig z src hz + rw [hr] at hb + have := isPFA_child (isPFA_self (hI.self src hsrc hzb)) hsh hbd + rw [hb] at this; cases this + +theorem np_desc_iffA {par : U64 → Option U64} {src x : U64} (b : Bool) : + ((if b then some src else par src) = some x ∨ + ∃ p, (if b then some src else par src) = some p ∧ Mdb.Desc par p x) ↔ + (b = true ∧ x = src) ∨ Mdb.Desc par src x := by + cases b + · simp only [Bool.false_eq_true, ↓reduceIte, false_and, false_or] + constructor + · rintro (h | ⟨p, hp, h⟩) + · exact Relation.TransGen.single h + · exact Relation.TransGen.head hp h + · intro h + obtain ⟨p, hp, h2⟩ := Relation.TransGen.head'_iff.1 h + rcases Relation.reflTransGen_iff_eq_or_transGen.1 h2 with rfl | h3 + · exact Or.inl hp + · exact Or.inr ⟨p, hp, h3⟩ + · simp only [↓reduceIte, Option.some.injEq, true_and] + constructor + · rintro (rfl | ⟨p, rfl, h⟩) + · exact Or.inl rfl + · exact Or.inr h + · rintro (rfl | h) + · exact Or.inl rfl + · exact Or.inr ⟨src, rfl, h⟩ + +theorem mem_insG_flat {G : List (List U64)} {src dest z : U64} (hsrc : src ∈ G.flatten) : + z ∈ (insG src dest G).flatten ↔ z = dest ∨ z ∈ G.flatten := by + unfold insG + simp only [List.mem_flatten, List.mem_map] + constructor + · rintro ⟨_, ⟨ch, hch, rfl⟩, hz⟩ + by_cases hs : src ∈ ch + · rcases List.mem_cons.1 ((insA_perm hs).mem_iff.1 hz) with h | h + · exact Or.inl h + · exact Or.inr ⟨ch, hch, h⟩ + · rw [insA_of_not_mem hs] at hz; exact Or.inr ⟨ch, hch, hz⟩ + · rintro (rfl | ⟨ch, hch, hz⟩) + · obtain ⟨ch, hch, hs⟩ := List.mem_flatten.1 hsrc + exact ⟨_, ⟨ch, hch, rfl⟩, (insA_perm hs).mem_iff.2 (List.mem_cons_self ..)⟩ + · by_cases hs : src ∈ ch + · exact ⟨_, ⟨ch, hch, rfl⟩, (insA_perm hs).mem_iff.2 (List.mem_cons_of_mem _ hz)⟩ + · exact ⟨_, ⟨ch, hch, rfl⟩, by rw [insA_of_not_mem hs]; exact hz⟩ + +private theorem ca_of_falseA {R : U64 → U64 → Bool} {a : U64} : + ∀ {L : List U64}, (∀ v ∈ L, R a v = false) → Mdb.CA R a L + | [], _ => trivial + | v :: L, hf => ⟨fun _ w hw => hf w (List.mem_cons_of_mem v hw), + ca_of_falseA fun w hw => hf w (List.mem_cons_of_mem v hw)⟩ + +/-- Ghost parent update for `cdt::insert_with_cap` (`should_be_parent_of`). -/ +def insParA (par : U64 → Option U64) (src dest : U64) (b : Bool) : U64 → Option U64 := + Function.update par dest (if b then some src else par src) + +/-! ## 1. Derived cap insertion (`InsCtxA`, x86 `MdbInvIns.InsCtx`) -/ + +/-- The situation of a derived-cap insertion (`cdt::insert_with_cap`), at the level of slot contents. -/ +structure InsCtxA (st : machine.Store S) [L : LawfulStore st] (OS : Set U64) + (G : List (List U64)) (par : U64 → Option U64) (s s' : S) (src dest : U64) + (c c1 : capability.Cap) : Prop where + hM : MdbLk st OS G s + hOk : Mdb.MdbOK par G + hI : MdbInvA st G par s + hsrc : src ∈ G.flatten + hdest : dest ∉ G.flatten + hnext : ∀ a, (a = dest ∨ a ∈ G.flatten) → NxA st s' a = + if a = dest then NxA st s src else if a = src then dest else NxA st s a + hcap : ∀ a, (a = dest ∨ a ∈ G.flatten) → capA st s' a = + if a = dest then c else if a = src then c1 else capA st s a + hrev : ∀ a, (a = dest ∨ a ∈ G.flatten) → revA st s' a = + if a = dest then revFA c (capA st s src) else revA st s a + hfb : ∀ a, (a = dest ∨ a ∈ G.flatten) → fbA st s' a = + if a = dest then revFA c (capA st s src) else fbA st s a + hsh : shapeA c = shapeA (capA st s src) + hbd : badgeA c = badgeA (capA st s src) ∨ badgeA (capA st s src) = 0#u64 + hsh1 : shapeA c1 = shapeA (capA st s src) + hbd1 : badgeA c1 = badgeA (capA st s src) + hut : isUtA (capA st s src) = true → ∀ y, ¬ Mdb.Desc par y src + hzb : isZbA (capA st s src) = false + +namespace InsCtxA +variable {OS : Set U64} {G : List (List U64)} {par : U64 → Option U64} {s s' : S} {src dest : U64} + {c c1 : capability.Cap} (h : InsCtxA st OS G par s s' src dest c c1) +include h + +theorem dne {z : U64} (hz : z ∈ G.flatten) : z ≠ dest := fun e => h.hdest (e ▸ hz) +theorem sd : src ≠ dest := h.dne h.hsrc + +theorem cz {z : U64} (hz : z ∈ G.flatten) : + shapeA (capA st s' z) = shapeA (capA st s z) ∧ badgeA (capA st s' z) = badgeA (capA st s z) := by + rw [h.hcap z (Or.inr hz), if_neg (h.dne hz)] + by_cases hzs : z = src + · subst hzs; simp [h.hsh1, h.hbd1] + · simp [hzs] + +theorem rz {z : U64} (hz : z ∈ G.flatten) : revA st s' z = revA st s z := by + rw [h.hrev z (Or.inr hz), if_neg (h.dne hz)] +theorem fz {z : U64} (hz : z ∈ G.flatten) : fbA st s' z = fbA st s z := by + rw [h.hfb z (Or.inr hz), if_neg (h.dne hz)] +theorem nz {z : U64} (hz : z ∈ G.flatten) (hzs : z ≠ src) : NxA st s' z = NxA st s z := by + rw [h.hnext z (Or.inr hz), if_neg (h.dne hz), if_neg hzs] +theorem ns : NxA st s' src = dest := by rw [h.hnext src (Or.inr h.hsrc), if_neg h.sd, if_pos rfl] +theorem nd : NxA st s' dest = NxA st s src := by rw [h.hnext dest (Or.inl rfl), if_pos rfl] +theorem cd : capA st s' dest = c := by rw [h.hcap dest (Or.inl rfl), if_pos rfl] +theorem cs : capA st s' src = c1 := by rw [h.hcap src (Or.inr h.hsrc), if_neg h.sd, if_pos rfl] +theorem rd : revA st s' dest = revFA c (capA st s src) := by rw [h.hrev dest (Or.inl rfl), if_pos rfl] +theorem fd : fbA st s' dest = revFA c (capA st s src) := by rw [h.hfb dest (Or.inl rfl), if_pos rfl] + +theorem pm {x y : U64} (hx : x ∈ G.flatten) (hy : y ∈ G.flatten) : isPMA st s' x y = isPMA st s x y := by + unfold isPMA; rw [h.rz hx, h.fz hy] + exact isPFA_congr (h.cz hx).1 (h.cz hx).2 (h.cz hy).1 (h.cz hy).2 + +theorem sm {x y : U64} (hx : x ∈ G.flatten) (hy : y ∈ G.flatten) : srMA st s' x y = srMA st s x y := by + unfold srMA; exact sraA_congr (h.cz hx).1 (h.cz hy).1 + +theorem sf {x y : U64} (hx : x ∈ G.flatten) (hy : y ∈ G.flatten) : srFA st s' x y = srFA st s x y := + srFA_congr (h.sm hx hy) (isFrA_of_shape (h.cz hx).1) (h.rz hx) + +theorem pd (z : U64) : par z ≠ some dest := fun e => h.hdest (h.hOk.par_mem z dest e).2 +theorem dn : par dest = none := by + cases e : par dest with + | none => rfl + | some p => exact absurd (h.hOk.par_mem dest p e).1 h.hdest + +theorem desc (b : Bool) (y x : U64) : Mdb.Desc (insParA par src dest b) y x ↔ + Mdb.Desc par y x ∨ (y = dest ∧ ((b = true ∧ x = src) ∨ Mdb.Desc par src x)) := by + have hnp : (if b then some src else par src) ≠ some dest := by + cases b <;> simp [h.sd, h.pd src] + unfold insParA + rw [Mdb.desc_update_iff h.pd h.dn hnp, np_desc_iffA] + +theorem dest_no_desc (x : U64) : ¬ Mdb.Desc par dest x := fun e => h.hdest (desc_memA h.hOk e).1 + +theorem memI (z : U64) : z ∈ (insG src dest G).flatten ↔ z = dest ∨ z ∈ G.flatten := + mem_insG_flat h.hsrc + +theorem psd : isPMA st s' src dest = isPFA (revA st s src) (capA st s src) c (revFA c (capA st s src)) := by + unfold isPMA + rw [h.rz h.hsrc, h.cs, h.cd, h.fd] + exact isPFA_congr h.hsh1 h.hbd1 rfl rfl + +local notation "bI" => isPFA (revA st s src) (capA st s src) c (revFA c (capA st s src)) + +theorem orig' (z p : U64) (hz : insParA par src dest bI z = some p) : revA st s' p = true := by + unfold insParA at hz + by_cases hzd : z = dest + · subst hzd + rw [Function.update_self] at hz + cases hb : bI + · rw [hb] at hz; simp only [Bool.false_eq_true, ↓reduceIte] at hz + rw [h.rz (h.hOk.par_mem _ _ hz).2]; exact h.hI.orig _ _ hz + · rw [hb] at hz + have hp : p = src := by simpa using hz.symm + rw [hp, h.rz h.hsrc] + simp only [isPFA, Bool.and_eq_true] at hb + exact hb.1.1 + · rw [Function.update_of_ne hzd] at hz + rw [h.rz (h.hOk.par_mem _ _ hz).2]; exact h.hI.orig _ _ hz + +theorem desc' (x y : U64) (hd : Mdb.Desc (insParA par src dest bI) y x) : isPMA st s' x y = true := by + rcases (h.desc bI y x).1 hd with hd | ⟨hy, hd⟩ + · obtain ⟨h1, h2⟩ := desc_memA h.hOk hd + rw [h.pm h2 h1]; exact h.hI.desc x y hd + · rw [hy] + rcases hd with ⟨hb, hxs⟩ | hd + · rw [hxs, h.psd]; exact hb + · have hx := (desc_memA h.hOk hd).2 + have h0 := h.hI.desc x src hd + unfold isPMA at h0 ⊢ + rw [h.rz hx, h.cd, h.fd, isPFA_congr (h.cz hx).1 (h.cz hx).2 rfl rfl] + exact isPFA_child h0 h.hsh h.hbd + +theorem after_dest (hn : NxA st s src ≠ 0#u64) : + isPFA (revFA c (capA st s src)) c (capA st s (NxA st s src)) (fbA st s (NxA st s src)) = false := by + cases hrv : revFA c (capA st s src) + · simp [isPFA] + rcases revFA_cases h.hsh hrv with ⟨hbc, hbne⟩ | hut' + · have hs0 : badgeA (capA st s src) = 0#u64 := h.hbd.resolve_left hbne + have hbs : isBadgedA (capA st s src) = true := by rw [← isBadgedA_of_shape h.hsh]; exact hbc + have hc0 : badgeA c ≠ 0#u64 := by intro e; exact hbne (by rw [e, hs0]) + unfold isPFA + by_cases hsr : sraA c (capA st s (NxA st s src)) = true + · have hsr' : srMA st s src (NxA st s src) = true := by + unfold srMA; rw [← sraA_congr h.hsh rfl]; exact hsr + by_cases hbN : badgeA (capA st s (NxA st s src)) = badgeA c + · have hfN := h.hI.badges src h.hsrc hn hbs hsr' (by rw [hs0, hbN]; exact Ne.symm hc0) + (by rw [hbN]; exact hc0) + simp [hfN, hbc, hc0] + · simp [hbN, hbc, hc0] + · simp at hsr; simp [hsr] + · have hus : isUtA (capA st s src) = true := by rw [← isUtA_of_shape h.hsh]; exact hut' + have ha := h.hI.after src h.hsrc hn src (Or.inl rfl) (h.hut hus _) + unfold isPMA isPFA at ha + rw [h.hI.utrev src h.hsrc hus] at ha + have hnb : isBadgedA (capA st s src) = false := by + cases e : capA st s src <;> simp_all [isUtA, isBadgedA] + simp only [Bool.true_and, hnb, Bool.false_eq_true, ↓reduceIte, Bool.and_true] at ha + unfold isPFA + rw [sraA_congr h.hsh rfl, ha]; simp + +theorem after' (y : U64) (hy : y ∈ (insG src dest G).flatten) (hn : NxA st s' y ≠ 0#u64) (x : U64) + (hx : x = y ∨ Mdb.Desc (insParA par src dest bI) y x) + (hnd : ¬ Mdb.Desc (insParA par src dest bI) (NxA st s' y) x) : isPMA st s' x (NxA st s' y) = false := by + rcases (h.memI y).1 hy with hyd | hy + · rw [hyd] at hn hnd hx ⊢ + rw [h.nd] at hn hnd ⊢ + have hN := h.hM.next_memA h.hsrc hn + rcases hx with hxd | hx + · subst hxd + unfold isPMA + rw [h.rd, h.cd, h.fz hN, isPFA_congr rfl rfl (h.cz hN).1 (h.cz hN).2] + exact h.after_dest hn + · have hx' : x = src ∨ Mdb.Desc par src x := by + rcases (h.desc bI dest x).1 hx with e | ⟨-, ⟨-, e⟩ | e⟩ + · exact absurd e (h.dest_no_desc x) + · exact Or.inl e + · exact Or.inr e + have hnd' : ¬ Mdb.Desc par (NxA st s src) x := fun e => hnd ((h.desc bI _ x).2 (Or.inl e)) + have hxm : x ∈ G.flatten := by + rcases hx' with e | e + · rw [e]; exact h.hsrc + · exact (desc_memA h.hOk e).2 + rw [h.pm hxm hN] + exact h.hI.after src h.hsrc hn x hx' hnd' + · by_cases hys : y = src + · rw [hys] at hn hnd hx hy ⊢ + rw [h.ns] at hn hnd ⊢ + rcases hx with hxy | hx + · rw [hxy] at hnd ⊢ + rw [h.psd] + cases hb : bI + · rfl + · exact absurd ((h.desc bI dest src).2 (Or.inr ⟨rfl, Or.inl ⟨hb, rfl⟩⟩)) hnd + · have hx' : Mdb.Desc par src x := ((h.desc bI src x).1 hx).resolve_right (fun e => h.sd e.1) + exact absurd ((h.desc bI dest x).2 (Or.inr ⟨rfl, Or.inr hx'⟩)) hnd + · rw [h.nz hy hys] at hn hnd ⊢ + have hw := h.hM.next_memA hy hn + have hx' : x = y ∨ Mdb.Desc par y x := by + rcases hx with e | e + · exact Or.inl e + · exact Or.inr (((h.desc bI y x).1 e).resolve_right (fun e => h.dne hy e.1)) + have hnd' : ¬ Mdb.Desc par (NxA st s y) x := fun e => hnd ((h.desc bI _ x).2 (Or.inl e)) + have hxm : x ∈ G.flatten := by + rcases hx' with e | e + · rw [e]; exact hy + · exact (desc_memA h.hOk e).2 + rw [h.pm hxm hw] + exact h.hI.after y hy hn x hx' hnd' + +theorem chunk' (ch' : List U64) (hch' : ch' ∈ insG src dest G) : Mdb.ChunkL (srFA st s') ch' := by + rw [insG_eq] at hch' + obtain ⟨ch, hch, rfl⟩ := List.mem_map.1 hch' + have hchm : ∀ z, z ∈ ch → z ∈ G.flatten := fun z hz => List.mem_flatten.2 ⟨_, hch, hz⟩ + by_cases hsc : src ∈ ch + · obtain ⟨A, B, rfl⟩ := List.append_of_mem hsc + have hnd0 : (A ++ src :: B).Nodup := (List.nodup_flatten.1 h.hOk.nodup).1 _ hch + rw [Mdb.insAfter_split (Mdb.nodup_mid hnd0).1] + refine Mdb.chunkL_ins2 (h.hI.chunk _ hch) (fun u hu v hv => h.sf (hchm u hu) (hchm v hv)) + (fun u hu => srFA_congr ?_ (isFrA_of_shape (h.cz (hchm u hu)).1) (h.rz (hchm u hu))) + ?_ (srFA_of_srMA ?_) + · unfold srMA; rw [h.cd]; exact sraA_congr (h.cz (hchm u hu)).1 h.hsh + · cases hfc : isFrA c + · have hfs : isFrA (capA st s src) = false := by rw [← isFrA_of_shape h.hsh, hfc] + have hca := ca_srMA (Mdb.chunkL_ca (h.hI.chunk _ hch)) (Or.inl hfs) + refine Mdb.ca_congr hca (fun v hv => ?_) + rw [srFA_nf (by rw [h.cd]; exact hfc)] + unfold srMA; rw [h.cd]; exact sraA_congr h.hsh (h.cz (hchm v (by simp [hv]))).1 + · exact ca_trueA (fun v => by simp [srFA, h.cd, hfc, h.rd, revFA_frame hfc]) B + · unfold srMA; rw [h.cs, h.cd, sraA_congr h.hsh1 h.hsh]; exact h.hI.self src h.hsrc h.hzb + · rw [Mdb.insAfter_of_not_mem hsc] + exact Mdb.chunkL_congr (h.hI.chunk _ hch) (fun u hu v hv => h.sf (hchm u hu) (hchm v hv)) + +theorem badges' (y : U64) (hy : y ∈ (insG src dest G).flatten) (hn : NxA st s' y ≠ 0#u64) + (hbg : isBadgedA (capA st s' y) = true) (hsr : srMA st s' y (NxA st s' y) = true) + (hbne : badgeA (capA st s' y) ≠ badgeA (capA st s' (NxA st s' y))) + (hb0 : badgeA (capA st s' (NxA st s' y)) ≠ 0#u64) : fbA st s' (NxA st s' y) = true := by + rcases (h.memI y).1 hy with hyd | hy + · rw [hyd] at hn hsr hbne hb0 hbg ⊢ + rw [h.nd] at hn hsr hbne hb0 ⊢ + have hN := h.hM.next_memA h.hsrc hn + rw [h.cd] at hbg hbne + unfold srMA at hsr; rw [h.cd] at hsr + rw [h.fz hN] + have hbs : isBadgedA (capA st s src) = true := by rw [← isBadgedA_of_shape h.hsh]; exact hbg + rw [(h.cz hN).2] at hbne hb0 + refine h.hI.badges src h.hsrc hn hbs (by unfold srMA; rw [← sraA_congr h.hsh (h.cz hN).1]; exact hsr) ?_ hb0 + rcases h.hbd with e | e + · rw [← e]; exact hbne + · rw [e]; exact Ne.symm hb0 + · by_cases hys : y = src + · rw [hys] at hn hsr hbne hb0 hbg ⊢ + rw [h.ns] at hn hsr hbne hb0 ⊢ + rw [h.fd] + rw [h.cs] at hbg hbne + rw [h.cd] at hbne hb0 + have hbc : isBadgedA c = true := by rw [isBadgedA_of_shape h.hsh, ← isBadgedA_of_shape h.hsh1]; exact hbg + rw [revFA_badged h.hsh hbc, ← h.hbd1] + simpa using Ne.symm hbne + · rw [h.nz hy hys] at hn hsr hbne hb0 ⊢ + have hw := h.hM.next_memA hy hn + rw [h.fz hw] + rw [(h.cz hy).2, (h.cz hw).2] at hbne + rw [(h.cz hw).2] at hb0 + rw [isBadgedA_of_shape (h.cz hy).1] at hbg + exact h.hI.badges y hy hn hbg (by rw [← h.sm hy hw]; exact hsr) hbne hb0 + +theorem utrev' (x : U64) (hx : x ∈ (insG src dest G).flatten) (hu : isUtA (capA st s' x) = true) : + revA st s' x = true := by + rcases (h.memI x).1 hx with hxd | hx + · rw [hxd] at hu ⊢; rw [h.rd]; rw [h.cd] at hu; exact revFA_ut hu + · rw [h.rz hx]; rw [isUtA_of_shape (h.cz hx).1] at hu; exact h.hI.utrev x hx hu + +theorem self' (x : U64) (hx : x ∈ (insG src dest G).flatten) (hz : isZbA (capA st s' x) = false) : + srMA st s' x x = true := by + rcases (h.memI x).1 hx with hxd | hx + · rw [hxd]; unfold srMA; rw [h.cd, sraA_congr h.hsh h.hsh]; exact h.hI.self src h.hsrc h.hzb + · rw [isZbA_of_shape (h.cz hx).1] at hz + rw [h.sm hx hx]; exact h.hI.self x hx hz + +/-- **`MdbInvA` after inserting a derived cap.** -/ +theorem inv : MdbInvA st (insG src dest G) (insParA par src dest bI) s' := + ⟨h.orig', h.desc', h.after', h.chunk', h.badges', h.utrev', h.self'⟩ + +end InsCtxA + +/-! ## 2. Fresh `IrqHandler` insertion from `IrqControl` (`CtlCtxA`, x86 `CtlIns`) -/ + +/-- The situation of `invoke_irq_control` inserting a fresh `IrqHandler irq` after `IrqControl`. -/ +structure CtlCtxA (st : machine.Store S) [L : LawfulStore st] (OS : Set U64) + (G : List (List U64)) (par : U64 → Option U64) (s s' : S) (src dest irq : U64) : Prop where + hM : MdbLk st OS G s + hOk : Mdb.MdbOK par G + hI : MdbInvA st G par s + hsrc : src ∈ G.flatten + hdest : dest ∉ G.flatten + hsrc_c : capA st s src = .IrqControl + hfr : ∀ y ∈ G.flatten, capA st s y ≠ .IrqHandler irq + hnext : ∀ a, (a = dest ∨ a ∈ G.flatten) → NxA st s' a = + if a = dest then NxA st s src else if a = src then dest else NxA st s a + hcap : ∀ a, (a = dest ∨ a ∈ G.flatten) → capA st s' a = + if a = dest then .IrqHandler irq else capA st s a + hrev : ∀ a, (a = dest ∨ a ∈ G.flatten) → revA st s' a = + if a = dest then true else revA st s a + hfb : ∀ a, (a = dest ∨ a ∈ G.flatten) → fbA st s' a = + if a = dest then true else fbA st s a + +namespace CtlCtxA +variable {OS : Set U64} {G : List (List U64)} {par : U64 → Option U64} {s s' : S} {src dest irq : U64} + (h : CtlCtxA st OS G par s s' src dest irq) +include h + +theorem dne {z : U64} (hz : z ∈ G.flatten) : z ≠ dest := fun e => h.hdest (e ▸ hz) +theorem sd : src ≠ dest := h.dne h.hsrc +theorem cz {z : U64} (hz : z ∈ G.flatten) : capA st s' z = capA st s z := by + rw [h.hcap z (Or.inr hz), if_neg (h.dne hz)] +theorem rz {z : U64} (hz : z ∈ G.flatten) : revA st s' z = revA st s z := by + rw [h.hrev z (Or.inr hz), if_neg (h.dne hz)] +theorem fz {z : U64} (hz : z ∈ G.flatten) : fbA st s' z = fbA st s z := by + rw [h.hfb z (Or.inr hz), if_neg (h.dne hz)] +theorem nz {z : U64} (hz : z ∈ G.flatten) (hzs : z ≠ src) : NxA st s' z = NxA st s z := by + rw [h.hnext z (Or.inr hz), if_neg (h.dne hz), if_neg hzs] +theorem ns : NxA st s' src = dest := by rw [h.hnext src (Or.inr h.hsrc), if_neg h.sd, if_pos rfl] +theorem nd : NxA st s' dest = NxA st s src := by rw [h.hnext dest (Or.inl rfl), if_pos rfl] +theorem cd : capA st s' dest = .IrqHandler irq := by rw [h.hcap dest (Or.inl rfl), if_pos rfl] +theorem rd : revA st s' dest = true := by rw [h.hrev dest (Or.inl rfl), if_pos rfl] +theorem fd : fbA st s' dest = true := by rw [h.hfb dest (Or.inl rfl), if_pos rfl] + +theorem pm {x y : U64} (hx : x ∈ G.flatten) (hy : y ∈ G.flatten) : isPMA st s' x y = isPMA st s x y := by + unfold isPMA; rw [h.rz hx, h.fz hy, h.cz hx, h.cz hy] +theorem sm {x y : U64} (hx : x ∈ G.flatten) (hy : y ∈ G.flatten) : srMA st s' x y = srMA st s x y := by + unfold srMA; rw [h.cz hx, h.cz hy] +theorem sf {x y : U64} (hx : x ∈ G.flatten) (hy : y ∈ G.flatten) : srFA st s' x y = srFA st s x y := + srFA_congr (h.sm hx hy) (by rw [h.cz hx]) (h.rz hx) + +theorem sr_left {a : U64} (ha : a ∈ G.flatten) : srMA st s' a dest = srMA st s a src := by + unfold srMA; rw [h.cz ha, h.cd, h.hsrc_c, sraA_irqH_right, sraA_irqC_right] + simp [h.hfr a ha] + +theorem sr_right {a : U64} (ha : a ∈ G.flatten) : srMA st s' dest a = false := by + unfold srMA; rw [h.cd, h.cz ha, sraA_irqH_left] + simp [h.hfr a ha] + +theorem psd : isPMA st s' src dest = revA st s src := by + unfold isPMA isPFA + rw [h.rz h.hsrc, h.cz h.hsrc, h.hsrc_c, h.cd] + have e : sraA .IrqControl (.IrqHandler irq) = true := by + apply sraA_of_eq; simp [objects.same_region_as] + simp [e, isBadgedA] + +theorem pd (z : U64) : par z ≠ some dest := fun e => h.hdest (h.hOk.par_mem z dest e).2 +theorem dn : par dest = none := by + cases e : par dest with + | none => rfl + | some p => exact absurd (h.hOk.par_mem dest p e).1 h.hdest + +theorem desc (b : Bool) (y x : U64) : Mdb.Desc (insParA par src dest b) y x ↔ + Mdb.Desc par y x ∨ (y = dest ∧ ((b = true ∧ x = src) ∨ Mdb.Desc par src x)) := by + have hnp : (if b then some src else par src) ≠ some dest := by + cases b <;> simp [h.sd, h.pd src] + unfold insParA + rw [Mdb.desc_update_iff h.pd h.dn hnp, np_desc_iffA] + +theorem dest_no_desc (x : U64) : ¬ Mdb.Desc par dest x := fun e => h.hdest (desc_memA h.hOk e).1 +theorem memI (z : U64) : z ∈ (insG src dest G).flatten ↔ z = dest ∨ z ∈ G.flatten := mem_insG_flat h.hsrc + +/-- **`MdbInvA` after `invoke_irq_control` inserting a fresh `IrqHandler irq` after `IrqControl`.** -/ +theorem inv : MdbInvA st (insG src dest G) (insParA par src dest (revA st s src)) s' := by + set bC := revA st s src + refine ⟨fun z p hz => ?_, fun x y hd => ?_, fun y hy hn x hx hnd => ?_, fun ch' hch' => ?_, + fun y hy hn hbg hsr hbne hb0 => ?_, fun x hx hu => ?_, fun x hx hz => ?_⟩ + · unfold insParA at hz + by_cases hzd : z = dest + · subst hzd; rw [Function.update_self] at hz + cases hb : bC + · rw [hb] at hz; simp only [Bool.false_eq_true, ↓reduceIte] at hz + rw [h.rz (h.hOk.par_mem _ _ hz).2]; exact h.hI.orig _ _ hz + · rw [hb] at hz; have hp : p = src := by simpa using hz.symm + rw [hp, h.rz h.hsrc]; exact hb + · rw [Function.update_of_ne hzd] at hz + rw [h.rz (h.hOk.par_mem _ _ hz).2]; exact h.hI.orig _ _ hz + · rcases (h.desc bC y x).1 hd with hd | ⟨hyd, ⟨hb, hxs⟩ | hd⟩ + · obtain ⟨h1, h2⟩ := desc_memA h.hOk hd + rw [h.pm h2 h1]; exact h.hI.desc x y hd + · rw [hyd, hxs, h.psd]; exact hb + · rw [hyd] + have hx := (desc_memA h.hOk hd).2 + have h0 := h.hI.desc x src hd + have hsrx : srMA st s' x dest = srMA st s x src := h.sr_left hx + unfold isPMA isPFA at h0 ⊢ + rw [h.rz hx, h.cz hx] + have hsr0 : sraA (capA st s x) (capA st s src) = true := by + simp only [Bool.and_eq_true] at h0; exact h0.1.2 + have hnb : isBadgedA (capA st s x) = false := by + rw [h.hsrc_c, sraA_irqC_right] at hsr0 + have : capA st s x = .IrqControl := by simpa using hsr0 + rw [this]; rfl + have hsrd : sraA (capA st s x) (capA st s' dest) = true := by + have := hsrx; unfold srMA at this; rw [h.cz hx] at this; rw [this]; exact hsr0 + simp only [Bool.and_eq_true] at h0 + simp [h0.1.1, hsrd, hnb] + · rcases (h.memI y).1 hy with hyd | hy + · rw [hyd] at hn hnd hx ⊢ + rw [h.nd] at hn hnd ⊢ + have hN := h.hM.next_memA h.hsrc hn + rcases hx with hxd | hx + · rw [hxd]; unfold isPMA isPFA; have := h.sr_right hN; unfold srMA at this; rw [this]; simp + · have hx' : x = src ∨ Mdb.Desc par src x := by + rcases (h.desc bC dest x).1 hx with e | ⟨-, ⟨-, e⟩ | e⟩ + · exact absurd e (h.dest_no_desc x) + · exact Or.inl e + · exact Or.inr e + have hnd' : ¬ Mdb.Desc par (NxA st s src) x := fun e => hnd ((h.desc bC _ x).2 (Or.inl e)) + have hxm : x ∈ G.flatten := by rcases hx' with rfl | e; exact h.hsrc; exact (desc_memA h.hOk e).2 + rw [h.pm hxm hN]; exact h.hI.after src h.hsrc hn x hx' hnd' + · by_cases hys : y = src + · rw [hys] at hn hnd hx hy ⊢ + rw [h.ns] at hn hnd ⊢ + rcases hx with hxs | hx + · rw [hxs] at hnd ⊢ + rw [h.psd] + cases hb : bC + · exact hb + · exact absurd ((h.desc bC dest src).2 (Or.inr ⟨rfl, Or.inl ⟨hb, rfl⟩⟩)) hnd + · have hx' : Mdb.Desc par src x := ((h.desc bC src x).1 hx).resolve_right (fun e => h.sd e.1) + exact absurd ((h.desc bC dest x).2 (Or.inr ⟨rfl, Or.inr hx'⟩)) hnd + · rw [h.nz hy hys] at hn hnd ⊢ + have hw := h.hM.next_memA hy hn + have hx' : x = y ∨ Mdb.Desc par y x := by + rcases hx with rfl | e + · exact Or.inl rfl + · exact Or.inr (((h.desc bC y x).1 e).resolve_right (fun e => h.dne hy e.1)) + have hnd' : ¬ Mdb.Desc par (NxA st s y) x := fun e => hnd ((h.desc bC _ x).2 (Or.inl e)) + have hxm : x ∈ G.flatten := by rcases hx' with rfl | e; exact hy; exact (desc_memA h.hOk e).2 + rw [h.pm hxm hw]; exact h.hI.after y hy hn x hx' hnd' + · rw [insG_eq] at hch' + obtain ⟨ch, hch, rfl⟩ := List.mem_map.1 hch' + have hchm : ∀ z, z ∈ ch → z ∈ G.flatten := fun z hz => List.mem_flatten.2 ⟨_, hch, hz⟩ + by_cases hsc : src ∈ ch + · obtain ⟨A, B, rfl⟩ := List.append_of_mem hsc + have hnd0 : (A ++ src :: B).Nodup := (List.nodup_flatten.1 h.hOk.nodup).1 _ hch + rw [Mdb.insAfter_split (Mdb.nodup_mid hnd0).1] + refine Mdb.chunkL_ins2 (h.hI.chunk _ hch) (fun u hu v hv => h.sf (hchm u hu) (hchm v hv)) + (fun u hu => srFA_congr (h.sr_left (hchm u hu)) (by rw [h.cz (hchm u hu)]) (h.rz (hchm u hu))) + ?_ (srFA_of_srMA ?_) + · exact ca_of_falseA fun v hv => by + unfold srFA; rw [h.sr_right (hchm v (by simp [hv])), h.cd]; rfl + · unfold srMA; rw [h.cz h.hsrc, h.hsrc_c, h.cd]; apply sraA_of_eq; simp [objects.same_region_as] + · rw [Mdb.insAfter_of_not_mem hsc] + exact Mdb.chunkL_congr (h.hI.chunk _ hch) (fun u hu v hv => h.sf (hchm u hu) (hchm v hv)) + · rcases (h.memI y).1 hy with rfl | hy + · rw [h.cd] at hbg; cases hbg + · by_cases hys : y = src + · subst hys; rw [h.cz h.hsrc, h.hsrc_c] at hbg; cases hbg + · rw [h.nz hy hys] at hn hsr hbne hb0 ⊢ + have hw := h.hM.next_memA hy hn + rw [h.fz hw]; rw [h.cz hy] at hbg hbne; rw [h.cz hw] at hbne hb0 + exact h.hI.badges y hy hn hbg (by rw [← h.sm hy hw]; exact hsr) hbne hb0 + · rcases (h.memI x).1 hx with rfl | hx + · rw [h.cd] at hu; cases hu + · rw [h.cz hx] at hu; rw [h.rz hx]; exact h.hI.utrev x hx hu + · rcases (h.memI x).1 hx with rfl | hx + · unfold srMA; rw [h.cd, sraA_irqH_left]; simp + · rw [h.cz hx] at hz; rw [h.sm hx hx]; exact h.hI.self x hx hz + +end CtlCtxA + +/-! ## 3. Fresh child insertion (`MakePool`, `DeriveSubframe`, `untyped::retype` `insert_new_cap`) -/ + +/-- Inserting a child `dest` right after a revocable, non-badged `src` (`MakePool`, `DeriveSubframe`, +`insert_new_cap`), where `src` may have its `Untyped` free index updated (`c1` same `shapeA` as `src`). -/ +structure FreshChildCtxA (st : machine.Store S) [L : LawfulStore st] (OS : Set U64) + (G : List (List U64)) (par : U64 → Option U64) (s s' : S) (src dest : U64) + (c c1 : capability.Cap) (rv fb : Bool) : Prop where + hM : MdbLk st OS G s + hOk : Mdb.MdbOK par G + hI : MdbInvA st G par s + hsrc : src ∈ G.flatten + hdest : dest ∉ G.flatten + hsrc_rev : revA st s src = true + hsrc_nb : isBadgedA (capA st s src) = false + hsh1 : shapeA c1 = shapeA (capA st s src) + hbd1 : badgeA c1 = badgeA (capA st s src) + hc_nb : isBadgedA c = false + hc_ut : isUtA c = true → rv = true + hc_self : isZbA c = false → sraA c c = true + hc_in_src : sraA (capA st s src) c = true + hsr_left : ∀ a ∈ G.flatten, sraA (capA st s a) c = srMA st s a src + hanc_nb : ∀ a ∈ G.flatten, Mdb.Desc par src a → isBadgedA (capA st s a) = false + hdest_after : NxA st s src ≠ 0#u64 → isPFA rv c (capA st s (NxA st s src)) (fbA st s (NxA st s src)) = false + hdest_ca : ∀ ch ∈ G, ∀ A B, ch = A ++ src :: B → Mdb.CA (srFA st s') dest B + hnext : ∀ a, (a = dest ∨ a ∈ G.flatten) → NxA st s' a = + if a = dest then NxA st s src else if a = src then dest else NxA st s a + hcap : ∀ a, (a = dest ∨ a ∈ G.flatten) → capA st s' a = + if a = dest then c else if a = src then c1 else capA st s a + hrev : ∀ a, (a = dest ∨ a ∈ G.flatten) → revA st s' a = + if a = dest then rv else revA st s a + hfb : ∀ a, (a = dest ∨ a ∈ G.flatten) → fbA st s' a = + if a = dest then fb else fbA st s a + +namespace FreshChildCtxA +variable {OS : Set U64} {G : List (List U64)} {par : U64 → Option U64} {s s' : S} {src dest : U64} + {c c1 : capability.Cap} {rv fb : Bool} (h : FreshChildCtxA st OS G par s s' src dest c c1 rv fb) +include h + +theorem dne {z : U64} (hz : z ∈ G.flatten) : z ≠ dest := fun e => h.hdest (e ▸ hz) +theorem sd : src ≠ dest := h.dne h.hsrc +theorem cz {z : U64} (hz : z ∈ G.flatten) : + shapeA (capA st s' z) = shapeA (capA st s z) ∧ badgeA (capA st s' z) = badgeA (capA st s z) := by + rw [h.hcap z (Or.inr hz), if_neg (h.dne hz)] + by_cases hzs : z = src + · subst hzs; simp [h.hsh1, h.hbd1] + · simp [hzs] +theorem rz {z : U64} (hz : z ∈ G.flatten) : revA st s' z = revA st s z := by + rw [h.hrev z (Or.inr hz), if_neg (h.dne hz)] +theorem fz {z : U64} (hz : z ∈ G.flatten) : fbA st s' z = fbA st s z := by + rw [h.hfb z (Or.inr hz), if_neg (h.dne hz)] +theorem nz {z : U64} (hz : z ∈ G.flatten) (hzs : z ≠ src) : NxA st s' z = NxA st s z := by + rw [h.hnext z (Or.inr hz), if_neg (h.dne hz), if_neg hzs] +theorem ns : NxA st s' src = dest := by rw [h.hnext src (Or.inr h.hsrc), if_neg h.sd, if_pos rfl] +theorem nd : NxA st s' dest = NxA st s src := by rw [h.hnext dest (Or.inl rfl), if_pos rfl] +theorem cd : capA st s' dest = c := by rw [h.hcap dest (Or.inl rfl), if_pos rfl] +theorem cs : capA st s' src = c1 := by rw [h.hcap src (Or.inr h.hsrc), if_neg h.sd, if_pos rfl] +theorem rd : revA st s' dest = rv := by rw [h.hrev dest (Or.inl rfl), if_pos rfl] +theorem fd : fbA st s' dest = fb := by rw [h.hfb dest (Or.inl rfl), if_pos rfl] + +theorem pm {x y : U64} (hx : x ∈ G.flatten) (hy : y ∈ G.flatten) : isPMA st s' x y = isPMA st s x y := by + unfold isPMA; rw [h.rz hx, h.fz hy] + exact isPFA_congr (h.cz hx).1 (h.cz hx).2 (h.cz hy).1 (h.cz hy).2 +theorem sm {x y : U64} (hx : x ∈ G.flatten) (hy : y ∈ G.flatten) : srMA st s' x y = srMA st s x y := by + unfold srMA; exact sraA_congr (h.cz hx).1 (h.cz hy).1 +theorem sf {x y : U64} (hx : x ∈ G.flatten) (hy : y ∈ G.flatten) : srFA st s' x y = srFA st s x y := + srFA_congr (h.sm hx hy) (isFrA_of_shape (h.cz hx).1) (h.rz hx) + +theorem sr_left {a : U64} (ha : a ∈ G.flatten) : srMA st s' a dest = srMA st s a src := by + unfold srMA; rw [h.cd, sraA_congr (h.cz ha).1 rfl]; exact h.hsr_left a ha + +theorem pd (z : U64) : par z ≠ some dest := fun e => h.hdest (h.hOk.par_mem z dest e).2 +theorem dn : par dest = none := by + cases e : par dest with + | none => rfl + | some p => exact absurd (h.hOk.par_mem dest p e).1 h.hdest + +theorem desc (y x : U64) : Mdb.Desc (insParA par src dest true) y x ↔ + Mdb.Desc par y x ∨ (y = dest ∧ (x = src ∨ Mdb.Desc par src x)) := by + have hnp : (if true then some src else par src) ≠ some dest := by simp [h.sd] + unfold insParA + rw [Mdb.desc_update_iff h.pd h.dn hnp, np_desc_iffA] + simp + +theorem dest_no_desc (x : U64) : ¬ Mdb.Desc par dest x := fun e => h.hdest (desc_memA h.hOk e).1 +theorem memI (z : U64) : z ∈ (insG src dest G).flatten ↔ z = dest ∨ z ∈ G.flatten := mem_insG_flat h.hsrc + +/-- **`MdbInvA` after inserting a fresh child (`MakePool`, `DeriveSubframe`, `retype`).** -/ +theorem inv : MdbInvA st (insG src dest G) (insParA par src dest true) s' := by + refine ⟨fun z p hz => ?_, fun x y hd => ?_, fun y hy hn x hx hnd => ?_, fun ch' hch' => ?_, + fun y hy hn hbg hsr hbne hb0 => ?_, fun x hx hu => ?_, fun x hx hz => ?_⟩ + · unfold insParA at hz + by_cases hzd : z = dest + · subst hzd; rw [Function.update_self] at hz + have hp : p = src := by simpa using hz.symm + rw [hp, h.rz h.hsrc]; exact h.hsrc_rev + · rw [Function.update_of_ne hzd] at hz + rw [h.rz (h.hOk.par_mem _ _ hz).2]; exact h.hI.orig _ _ hz + · rcases (h.desc y x).1 hd with hd | ⟨hyd, hxs | hd⟩ + · obtain ⟨h1, h2⟩ := desc_memA h.hOk hd + rw [h.pm h2 h1]; exact h.hI.desc x y hd + · rw [hyd, hxs] + unfold isPMA isPFA + rw [h.rz h.hsrc, h.hsrc_rev, h.cs, h.cd, sraA_congr h.hsh1 rfl, h.hc_in_src, + isBadgedA_of_shape h.hsh1, h.hsrc_nb] + rfl + · rw [hyd] + have hx := (desc_memA h.hOk hd).2 + have h0 := h.hI.desc x src hd + unfold isPMA isPFA at h0 ⊢ + simp only [Bool.and_eq_true] at h0 + rw [h.rz hx, h0.1.1, isBadgedA_of_shape (h.cz hx).1, h.hanc_nb x hx hd] + have hsr := h.sr_left hx + unfold srMA at hsr; rw [hsr, h0.1.2]; rfl + · rcases (h.memI y).1 hy with hyd | hy + · rw [hyd] at hn hnd hx ⊢ + rw [h.nd] at hn hnd ⊢ + have hN := h.hM.next_memA h.hsrc hn + rcases hx with hxd | hx + · rw [hxd]; unfold isPMA + rw [h.rd, h.cd, h.fz hN, isPFA_congr rfl rfl (h.cz hN).1 (h.cz hN).2] + exact h.hdest_after hn + · have hx' : x = src ∨ Mdb.Desc par src x := by + rcases (h.desc dest x).1 hx with e | ⟨-, e⟩ + · exact absurd e (h.dest_no_desc x) + · exact e + have hnd' : ¬ Mdb.Desc par (NxA st s src) x := fun e => hnd ((h.desc _ x).2 (Or.inl e)) + have hxm : x ∈ G.flatten := by rcases hx' with rfl | e; exact h.hsrc; exact (desc_memA h.hOk e).2 + rw [h.pm hxm hN]; exact h.hI.after src h.hsrc hn x hx' hnd' + · by_cases hys : y = src + · rw [hys] at hn hnd hx hy ⊢ + rw [h.ns] at hn hnd ⊢ + rcases hx with hxs | hx + · rw [hxs] at hnd; exact absurd ((h.desc dest src).2 (Or.inr ⟨rfl, Or.inl rfl⟩)) hnd + · have hx' : Mdb.Desc par src x := ((h.desc src x).1 hx).resolve_right (fun e => h.sd e.1) + exact absurd ((h.desc dest x).2 (Or.inr ⟨rfl, Or.inr hx'⟩)) hnd + · rw [h.nz hy hys] at hn hnd ⊢ + have hw := h.hM.next_memA hy hn + have hx' : x = y ∨ Mdb.Desc par y x := by + rcases hx with rfl | e + · exact Or.inl rfl + · exact Or.inr (((h.desc y x).1 e).resolve_right (fun e => h.dne hy e.1)) + have hnd' : ¬ Mdb.Desc par (NxA st s y) x := fun e => hnd ((h.desc _ x).2 (Or.inl e)) + have hxm : x ∈ G.flatten := by rcases hx' with rfl | e; exact hy; exact (desc_memA h.hOk e).2 + rw [h.pm hxm hw]; exact h.hI.after y hy hn x hx' hnd' + · rw [insG_eq] at hch' + obtain ⟨ch, hch, rfl⟩ := List.mem_map.1 hch' + have hchm : ∀ z, z ∈ ch → z ∈ G.flatten := fun z hz => List.mem_flatten.2 ⟨_, hch, hz⟩ + by_cases hsc : src ∈ ch + · obtain ⟨A, B, rfl⟩ := List.append_of_mem hsc + have hnd0 : (A ++ src :: B).Nodup := (List.nodup_flatten.1 h.hOk.nodup).1 _ hch + rw [Mdb.insAfter_split (Mdb.nodup_mid hnd0).1] + refine Mdb.chunkL_ins2 (h.hI.chunk _ hch) (fun u hu v hv => h.sf (hchm u hu) (hchm v hv)) + (fun u hu => srFA_congr (h.sr_left (hchm u hu)) (isFrA_of_shape (h.cz (hchm u hu)).1) (h.rz (hchm u hu))) + (h.hdest_ca _ hch A B rfl) (srFA_of_srMA ?_) + unfold srMA; rw [h.cs, h.cd, sraA_congr h.hsh1 rfl]; exact h.hc_in_src + · rw [Mdb.insAfter_of_not_mem hsc] + exact Mdb.chunkL_congr (h.hI.chunk _ hch) (fun u hu v hv => h.sf (hchm u hu) (hchm v hv)) + · rcases (h.memI y).1 hy with rfl | hy + · rw [h.cd] at hbg; rw [h.hc_nb] at hbg; cases hbg + · by_cases hys : y = src + · subst hys; rw [h.cs, isBadgedA_of_shape h.hsh1, h.hsrc_nb] at hbg; cases hbg + · rw [h.nz hy hys] at hn hsr hbne hb0 ⊢ + have hw := h.hM.next_memA hy hn + rw [h.fz hw] + rw [(h.cz hy).2, (h.cz hw).2] at hbne + rw [(h.cz hw).2] at hb0 + rw [isBadgedA_of_shape (h.cz hy).1] at hbg + exact h.hI.badges y hy hn hbg (by rw [← h.sm hy hw]; exact hsr) hbne hb0 + · rcases (h.memI x).1 hx with rfl | hx + · rw [h.cd] at hu; rw [h.rd]; exact h.hc_ut hu + · rw [h.rz hx]; rw [isUtA_of_shape (h.cz hx).1] at hu; exact h.hI.utrev x hx hu + · rcases (h.memI x).1 hx with rfl | hx + · rw [h.cd] at hz; unfold srMA; rw [h.cd]; exact h.hc_self hz + · rw [isZbA_of_shape (h.cz hx).1] at hz; rw [h.sm hx hx]; exact h.hI.self x hx hz + +end FreshChildCtxA + +end + +end ST +end R +end W7 + +end + +end Fl4ProofsA64.P1 diff --git a/proofs/Fl4ProofsA64/P1/MdbInvOpsA.lean b/proofs/Fl4ProofsA64/P1/MdbInvOpsA.lean new file mode 100644 index 000000000..ba3fe0995 --- /dev/null +++ b/proofs/Fl4ProofsA64/P1/MdbInvOpsA.lean @@ -0,0 +1,511 @@ +/- +P1 (aarch64, A64-P1-MDB wave 2): **renaming and removal preservation of `MdbInvA`** +(x86 counterparts: `Fl4Proofs.P3.MdbInvRen`, `Fl4Proofs.P3.MdbInvEmp`; l4v source: `CSpace_AI.thy` +`valid_mdb` across `cteMove`, `cteSwap`, `emptySlot`). + +* **`MdbInvA.rename`** (x86 `MdbInv.rename`): any bijection `e : U64 ≃ U64` fixing `0` that transports + `capA` (up to `shapeA` and `badgeA`), `NxA`, `revA`, and `fbA` on `G.flatten` preserves `MdbInvA` at + `(G.map (List.map e), Mdb.conjPar e par)`. Covers: + * `cdt::move_cap` (`e = Equiv.swap src dest`, `dest ∉ G.flatten`), + * `cdt::swap` (two members, `e = Equiv.swap x1 x2`), + * `cdt::cap_swap_for_delete` (including when the zombie's home slot is empty and linkless!). +* **`EmpCtxA`** and **`EmpCtxA.inv`** (x86 `MdbInvEmp.EmpCtx.inv`): removing slot `x` from `G` (`remG x G`, + `Mdb.removePar x par`) where `prev x` is linked to `next x` and `next x` inherits `fbA x` (`fbA next || fbA x`) + preserves `MdbInvA`. Covers `cdt::empty_slot` (both Null and `IrqHandler` cleanup). + +No named premise. New P1 module; zero edits to existing files. +-/ +import Fl4ProofsA64.P1.MdbInvA +import Fl4ProofsA64.P1.MdbRem +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P1 +open fl4_core_a64 Fl4ProofsA64.P0.Consts Fl4Proofs.P3 + +section +variable {S : Type _} {st : machine.Store S} [L : LawfulStore st] + +local notation "wa" => core.num.U64.wrapping_add + +namespace W7 +namespace R +namespace ST + +noncomputable section + +/-! ## Renaming (`move_cap`, `swap`, `cap_swap_for_delete`) -/ + +/-- **`MdbInvA` after renaming slots** (x86 `MdbInv.rename`). Handles `move_cap`, `swap` (two members), and +`cap_swap_for_delete` (including empty linkless first slot) via `e = Equiv.swap x1 x2`. -/ +theorem MdbInvA.rename {OS : Set U64} {G : List (List U64)} {par : U64 → Option U64} {s s' : S} + (hM : MdbLk st OS G s) (hOk : Mdb.MdbOK par G) (hI : MdbInvA st G par s) + (e : U64 ≃ U64) (he0 : e 0#u64 = 0#u64) + (hnext : ∀ a ∈ G.flatten, NxA st s' (e a) = e (NxA st s a)) + (hcap : ∀ a ∈ G.flatten, + shapeA (capA st s' (e a)) = shapeA (capA st s a) ∧ badgeA (capA st s' (e a)) = badgeA (capA st s a)) + (hrev : ∀ a ∈ G.flatten, revA st s' (e a) = revA st s a) + (hfb : ∀ a ∈ G.flatten, fbA st s' (e a) = fbA st s a) : + MdbInvA st (G.map (List.map e)) (Mdb.conjPar e par) s' := by + have hmem : ∀ z, z ∈ (G.map (List.map e)).flatten ↔ e.symm z ∈ G.flatten := by + intro z + simp only [List.map_flatten.symm, List.mem_map] + constructor + · rintro ⟨a, ha, rfl⟩; simpa using ha + · intro h; exact ⟨_, h, by simp⟩ + have hD : ∀ y x, Mdb.Desc (Mdb.conjPar e par) y x ↔ Mdb.Desc par (e.symm y) (e.symm x) := by + intro y x + have := Mdb.desc_conj e par (e.symm y) (e.symm x) + simpa using this + have hpm : ∀ x y, x ∈ G.flatten → y ∈ G.flatten → isPMA st s' (e x) (e y) = isPMA st s x y := by + intro x y hx hy + unfold isPMA + rw [hrev x hx, hfb y hy] + exact isPFA_congr (hcap x hx).1 (hcap x hx).2 (hcap y hy).1 (hcap y hy).2 + have hsm : ∀ x y, x ∈ G.flatten → y ∈ G.flatten → srMA st s' (e x) (e y) = srMA st s x y := by + intro x y hx hy; unfold srMA; exact sraA_congr (hcap x hx).1 (hcap y hy).1 + have hn0 : ∀ y ∈ G.flatten, NxA st s' (e y) ≠ 0#u64 → NxA st s y ≠ 0#u64 := by + intro y hy h h0; rw [hnext y hy, h0, he0] at h; exact h rfl + refine ⟨fun c p h => ?_, fun x y h => ?_, fun y hy hn x hx hd => ?_, fun ch hch => ?_, + fun y hy hn hb hs hbd hb0 => ?_, fun x hx hu => ?_, fun x hx hz => ?_⟩ + · change (par (e.symm c)).map e = some p at h + obtain ⟨q, hq, rfl⟩ := Option.map_eq_some_iff.1 h + rw [hrev q (hOk.par_mem _ _ hq).2]; exact hI.orig _ _ hq + · have h' := (hD y x).1 h + obtain ⟨h1, h2⟩ := desc_memA hOk h' + have := hpm _ _ h2 h1 + simp only [Equiv.apply_symm_apply] at this + rw [this]; exact hI.desc _ _ h' + · have hy' := (hmem y).1 hy + obtain ⟨y0, rfl⟩ : ∃ y0, y = e y0 := ⟨e.symm y, by simp⟩ + simp only [Equiv.symm_apply_apply] at hy' + have hn' := hn0 y0 hy' hn + rw [hnext y0 hy'] at hd ⊢ + obtain ⟨x0, rfl⟩ : ∃ x0, x = e x0 := ⟨e.symm x, by simp⟩ + have hx' : x0 = y0 ∨ Mdb.Desc par y0 x0 := by + rcases hx with h | h + · exact Or.inl (e.injective h) + · exact Or.inr (by simpa using (hD _ _).1 h) + have hd' : ¬ Mdb.Desc par (NxA st s y0) x0 := fun h => hd (by rw [hD]; simpa using h) + have hxm : x0 ∈ G.flatten := by + rcases hx' with h | h + · rw [h]; exact hy' + · exact (desc_memA hOk h).2 + rw [hpm _ _ hxm (hM.next_memA hy' hn')] + exact hI.after y0 hy' hn' x0 hx' hd' + · obtain ⟨ch0, hch0, rfl⟩ := List.mem_map.1 hch + exact Mdb.chunkL_map e (hI.chunk _ hch0) (fun u hu v hv => + srFA_congr (hsm u v (List.mem_flatten.2 ⟨_, hch0, hu⟩) (List.mem_flatten.2 ⟨_, hch0, hv⟩)) + (isFrA_of_shape (hcap u (List.mem_flatten.2 ⟨_, hch0, hu⟩)).1) (hrev u (List.mem_flatten.2 ⟨_, hch0, hu⟩))) + · have hy' := (hmem y).1 hy + obtain ⟨y0, rfl⟩ : ∃ y0, y = e y0 := ⟨e.symm y, by simp⟩ + simp only [Equiv.symm_apply_apply] at hy' + have hn' := hn0 y0 hy' hn + have hw := hM.next_memA hy' hn' + rw [hnext y0 hy'] at hs hbd hb0 ⊢ + rw [hfb _ hw] + rw [(hcap y0 hy').2, (hcap _ hw).2] at hbd + rw [(hcap _ hw).2] at hb0 + rw [isBadgedA_of_shape (hcap y0 hy').1] at hb + rw [hsm _ _ hy' hw] at hs + exact hI.badges y0 hy' hn' hb hs hbd hb0 + · have hx' := (hmem x).1 hx + obtain ⟨x0, rfl⟩ : ∃ x0, x = e x0 := ⟨e.symm x, by simp⟩ + simp only [Equiv.symm_apply_apply] at hx' + rw [isUtA_of_shape (hcap x0 hx').1] at hu + rw [hrev x0 hx']; exact hI.utrev x0 hx' hu + · have hx' := (hmem x).1 hx + obtain ⟨x0, rfl⟩ : ∃ x0, x = e x0 := ⟨e.symm x, by simp⟩ + simp only [Equiv.symm_apply_apply] at hx' + rw [isZbA_of_shape (hcap x0 hx').1] at hz + rw [hsm _ _ hx' hx']; exact hI.self x0 hx' hz + +/-- Corollary at A64's `renG x1 x2 G` (`swp x1 x2 = Equiv.swap x1 x2`). -/ +theorem MdbInvA.ren_swp {OS : Set U64} {G : List (List U64)} {par : U64 → Option U64} {s s' : S} + (hM : MdbLk st OS G s) (hOk : Mdb.MdbOK par G) (hI : MdbInvA st G par s) + {x1 x2 : U64} (h1 : x1 ≠ 0#u64) (h2 : x2 ≠ 0#u64) + (hnext : ∀ a ∈ G.flatten, NxA st s' (swp x1 x2 a) = swp x1 x2 (NxA st s a)) + (hcap : ∀ a ∈ G.flatten, + shapeA (capA st s' (swp x1 x2 a)) = shapeA (capA st s a) ∧ + badgeA (capA st s' (swp x1 x2 a)) = badgeA (capA st s a)) + (hrev : ∀ a ∈ G.flatten, revA st s' (swp x1 x2 a) = revA st s a) + (hfb : ∀ a ∈ G.flatten, fbA st s' (swp x1 x2 a) = fbA st s a) : + MdbInvA st (renG x1 x2 G) (Mdb.conjPar (Equiv.swap x1 x2) par) s' := by + rw [renG_eq] + have he0 : Equiv.swap x1 x2 0#u64 = 0#u64 := Equiv.swap_apply_of_ne_of_ne h1.symm h2.symm + exact hI.rename hM hOk (Equiv.swap x1 x2) he0 + (fun a ha => by simpa only [swap_eq_swp] using hnext a ha) + (fun a ha => by simpa only [swap_eq_swp] using hcap a ha) + (fun a ha => by simpa only [swap_eq_swp] using hrev a ha) + (fun a ha => by simpa only [swap_eq_swp] using hfb a ha) + +/-! ## Chain-link helpers for removal -/ + +theorem MdbLk.adjA {OS : Set U64} {G : List (List U64)} {s : S} (hM : MdbLk st OS G s) + {ch : List U64} (hch : ch ∈ G) {P Q : List U64} {u v : U64} (he : ch = P ++ u :: v :: Q) : + NxA st s u = v ∧ L.mem s (wa v 16#u64) = u := by + refine hM.chain ch hch P.length u v ?_ ?_ + · rw [he, List.getElem?_append_right (le_refl _), Nat.sub_self]; rfl + · rw [he, List.getElem?_append_right (by omega), show P.length + 1 - P.length = 1 by omega]; rfl + +theorem MdbLk.next_linkA {OS : Set U64} {G : List (List U64)} {s : S} (hM : MdbLk st OS G s) + {y : U64} (hy : y ∈ G.flatten) (hn : NxA st s y ≠ 0#u64) : + NxA st s y ∈ G.flatten ∧ L.mem s (wa (NxA st s y) 16#u64) = y := by + obtain ⟨ch, hch, hyc⟩ := List.mem_flatten.1 hy + obtain ⟨A, B, hsp⟩ := List.append_of_mem hyc + cases B with + | nil => exact absurd (hM.last ch hch y (by rw [hsp]; simp)) hn + | cons z R => + have ⟨e1, e2⟩ := hM.adjA hch (P := A) (Q := R) hsp + rw [e1] + exact ⟨List.mem_flatten.2 ⟨ch, hch, by rw [hsp]; simp⟩, e2⟩ + +theorem MdbLk.prev_linkA {OS : Set U64} {G : List (List U64)} {s : S} (hM : MdbLk st OS G s) + {y : U64} (hy : y ∈ G.flatten) (hp : L.mem s (wa y 16#u64) ≠ 0#u64) : + L.mem s (wa y 16#u64) ∈ G.flatten ∧ NxA st s (L.mem s (wa y 16#u64)) = y := by + obtain ⟨ch, hch, hyc⟩ := List.mem_flatten.1 hy + obtain ⟨A, B, hsp⟩ := List.append_of_mem hyc + rcases List.eq_nil_or_concat A with rfl | ⟨A', p, rfl⟩ + · exact absurd (hM.head ch hch y (by rw [hsp]; simp)) hp + · have ⟨e1, e2⟩ := hM.adjA hch (P := A') (u := p) (v := y) (Q := B) (by simpa using hsp) + rw [e2] + exact ⟨List.mem_flatten.2 ⟨ch, hch, by rw [hsp]; simp⟩, e1⟩ + +theorem MdbLk.next_ne_selfA {OS : Set U64} {G : List (List U64)} {s : S} (hM : MdbLk st OS G s) + (hnd : G.flatten.Nodup) {y : U64} (hy : y ∈ G.flatten) (hn : NxA st s y ≠ 0#u64) : + NxA st s y ≠ y := by + obtain ⟨ch, hch, hyc⟩ := List.mem_flatten.1 hy + obtain ⟨A, B, hsp⟩ := List.append_of_mem hyc + cases B with + | nil => exact absurd (hM.last ch hch y (by rw [hsp]; simp)) hn + | cons z R => + have hndc : (A ++ y :: z :: R).Nodup := by rw [← hsp]; exact (List.nodup_flatten.1 hnd).1 _ hch + have hne : y ≠ z := by + rw [List.nodup_append] at hndc + have := hndc.2.1 + rw [List.nodup_cons] at this + intro e; subst e; exact this.1 (List.mem_cons_self ..) + rw [(hM.adjA hch (P := A) (Q := R) hsp).1] + exact Ne.symm hne + +theorem MdbLk.prev_in_blockA {OS : Set U64} {G : List (List U64)} {par : U64 → Option U64} {s : S} + (hM : MdbLk st OS G s) (hOk : Mdb.MdbOK par G) + {a n : U64} (hd : Mdb.Desc par n a) : + L.mem s (wa n 16#u64) = a ∨ Mdb.Desc par (L.mem s (wa n 16#u64)) a := by + obtain ⟨-, ha⟩ := desc_memA hOk hd + obtain ⟨ch, hch, hach⟩ := List.mem_flatten.1 ha + obtain ⟨A, post, rfl⟩ := List.append_of_mem hach + obtain ⟨d, rest, rfl, hdd⟩ := hOk.seg _ hch A a post rfl + have hn := (hdd n).1 hd + obtain ⟨d1, d2, rfl⟩ := List.append_of_mem hn + rcases List.eq_nil_or_concat d1 with rfl | ⟨d1', z, rfl⟩ + · left + exact (hM.adjA hch (P := A) (Q := d2 ++ rest) (by simp)).2 + · right + have := (hM.adjA hch (P := A ++ a :: d1') (u := z) (v := n) (Q := d2 ++ rest) (by simp)).2 + rw [this]; exact (hdd z).2 (by simp) + +private theorem chain_uniqueA {G : List (List U64)} (hnd : G.flatten.Nodup) + {c1 c2 : List U64} (h1 : c1 ∈ G) (h2 : c2 ∈ G) {x : U64} (hx1 : x ∈ c1) (hx2 : x ∈ c2) : c1 = c2 := by + by_contra hne + have hp := (List.nodup_flatten.1 hnd).2 + obtain ⟨A, B, hG⟩ := List.append_of_mem h1 + rw [hG] at hp h2 + rcases List.mem_append.1 h2 with h2A | h2B + · have hpair := (List.pairwise_append.1 hp).2.2 c2 h2A c1 (List.mem_cons_self ..) + exact hpair hx2 hx1 + · rcases List.mem_cons.1 h2B with rfl | h2B' + · exact hne rfl + · have hpair := (List.pairwise_cons.1 (List.pairwise_append.1 hp).2.1).1 c2 h2B' + exact hpair hx1 hx2 + +theorem MdbLk.stretchA {OS : Set U64} {G : List (List U64)} {par : U64 → Option U64} {s : S} + (hM : MdbLk st OS G s) (hOk : Mdb.MdbOK par G) + {a p x n : U64} (hp : p ∈ G.flatten) (hpa : a = p ∨ Mdb.Desc par p a) + (hpx : NxA st s p = x) (hx0 : x ≠ 0#u64) (hxn : NxA st s x = n) (hn0 : n ≠ 0#u64) : + ∃ ch ∈ G, ∃ A B C, ch = A ++ a :: (B ++ n :: C) ∧ x ∈ B := by + obtain ⟨ch, hch, hpch⟩ := List.mem_flatten.1 hp + obtain ⟨A0, R, rfl⟩ := List.append_of_mem hpch + obtain ⟨R', rfl⟩ : ∃ R', R = x :: R' := by + cases R with + | nil => exact absurd (hpx.symm.trans (hM.last _ hch p (by simp))) hx0 + | cons r R' => + have e := (hM.adjA hch (P := A0) (Q := R') rfl).1 + rw [hpx] at e; exact ⟨R', by rw [e]⟩ + obtain ⟨R'', rfl⟩ : ∃ R'', R' = n :: R'' := by + cases R' with + | nil => exact absurd (hxn.symm.trans (hM.last _ hch x (by simp))) hn0 + | cons r R'' => + have e := (hM.adjA hch (P := A0 ++ [p]) (u := x) (v := r) (Q := R'') (by simp)).1 + rw [hxn] at e; exact ⟨R'', by rw [e]⟩ + rcases hpa with rfl | hd + · exact ⟨A0 ++ a :: x :: n :: R'', hch, A0, [x], R'', by simp, by simp⟩ + · have ha := (desc_memA hOk hd).2 + obtain ⟨ch', hch', hach⟩ := List.mem_flatten.1 ha + obtain ⟨A1, post, rfl⟩ := List.append_of_mem hach + have hppost := Mdb.desc_after (hOk.seg _ hch') rfl hd + have e := chain_uniqueA hOk.nodup hch' hch (by simp [hppost]) hpch + obtain ⟨M, S, rfl⟩ := List.append_of_mem hppost + have hnd : (A1 ++ a :: (M ++ p :: S)).Nodup := (List.nodup_flatten.1 hOk.nodup).1 _ hch' + have hS : S = x :: n :: R'' := + Mdb.nodup_suffix_unique (x := p) (p1 := A1 ++ a :: M) (p2 := A0) hnd (by simp) e + subst hS + exact ⟨A1 ++ a :: (M ++ p :: x :: n :: R''), hch', A1, M ++ [p, x], R'', by simp, by simp⟩ + +/-! ## Removal (`empty_slot`) -/ + +/-- The situation of emptying slot `x`, at the level of slot contents (x86 `EmpCtx`). -/ +structure EmpCtxA (st : machine.Store S) [L : LawfulStore st] (OS : Set U64) + (G : List (List U64)) (par : U64 → Option U64) (s s' : S) (x : U64) : Prop where + h0 : ∀ z ∈ OS, z ≠ 0#u64 + hM : MdbLk st OS G s + hOk : Mdb.MdbOK par G + hI : MdbInvA st G par s + hx : x ∈ G.flatten + hcap : ∀ a ∈ G.flatten, a ≠ x → capA st s' a = capA st s a + hrev : ∀ a ∈ G.flatten, a ≠ x → revA st s' a = revA st s a + hfb : ∀ a ∈ G.flatten, a ≠ x → fbA st s' a = + if NxA st s x ≠ 0#u64 ∧ a = NxA st s x then (fbA st s a || fbA st s x) else fbA st s a + hnext : ∀ a ∈ G.flatten, a ≠ x → NxA st s' a = + if L.mem s (wa x 16#u64) ≠ 0#u64 ∧ a = L.mem s (wa x 16#u64) then NxA st s x else NxA st s a + +namespace EmpCtxA +variable {OS : Set U64} {G : List (List U64)} {par : U64 → Option U64} {s s' : S} {x : U64} + (h : EmpCtxA st OS G par s s' x) +include h + +theorem nz {z : U64} (hz : z ∈ G.flatten) : z ≠ 0#u64 := h.h0 z ((h.hM.members z).1 hz).1 + +theorem no_self_par : par x ≠ some x := fun e => by + have hd : Mdb.Desc par x x := Relation.TransGen.single e + obtain ⟨ch, hch, hxch⟩ := List.mem_flatten.1 h.hx + obtain ⟨A, B, hsp⟩ := List.append_of_mem hxch + obtain ⟨dd, rest, hB, hdd⟩ := h.hOk.seg ch hch A x B hsp + have hxdd : x ∈ dd := (hdd x).1 hd + have hnd : (A ++ x :: (dd ++ rest)).Nodup := by rw [← hB, ← hsp]; exact (List.nodup_flatten.1 h.hOk.nodup).1 _ hch + rw [List.nodup_append, List.nodup_cons] at hnd + exact hnd.2.1.1 (List.mem_append_left rest hxdd) + +theorem memE (z : U64) : z ∈ (remG x G).flatten ↔ z ∈ G.flatten ∧ z ≠ x := by + rw [remG_eq]; exact Mdb.mem_removeChains h.hOk.nodup + +theorem descE (y z : U64) : Mdb.Desc (Mdb.removePar x par) y z ↔ y ≠ x ∧ z ≠ x ∧ Mdb.Desc par y z := + Mdb.desc_remove_iff h.no_self_par y z + +theorem pm {a y : U64} (ha : a ∈ G.flatten) (hax : a ≠ x) (hy : y ∈ G.flatten) (hyx : y ≠ x) : + isPMA st s' a y = if NxA st s x ≠ 0#u64 ∧ y = NxA st s x then + isPFA (revA st s a) (capA st s a) (capA st s y) (fbA st s y || fbA st s x) else isPMA st s a y := by + unfold isPMA + rw [h.hrev a ha hax, h.hcap a ha hax, h.hcap y hy hyx, h.hfb y hy hyx] + split <;> rfl + +theorem pm_false {a y : U64} (ha : a ∈ G.flatten) (hax : a ≠ x) (hy : y ∈ G.flatten) (hyx : y ≠ x) + (hf : isPMA st s a y = false) : isPMA st s' a y = false := by + rw [h.pm ha hax hy hyx] + split + · exact isPFA_fb_or hf + · exact hf + +theorem sm {u v : U64} (hu : u ∈ G.flatten) (hux : u ≠ x) (hv : v ∈ G.flatten) (hvx : v ≠ x) : + srMA st s' u v = srMA st s u v := by + unfold srMA; rw [h.hcap u hu hux, h.hcap v hv hvx] + +theorem orig' (c p : U64) (hc : Mdb.removePar x par c = some p) : revA st s' p = true := by + unfold Mdb.removePar at hc + by_cases hcx : c = x + · simp [hcx] at hc + simp only [hcx, ↓reduceIte] at hc + by_cases hpx : par c = some x + · rw [if_pos hpx] at hc + have hp := (h.hOk.par_mem _ _ hc).2 + have hpx' : p ≠ x := fun e => h.no_self_par (e ▸ hc) + rw [h.hrev p hp hpx']; exact h.hI.orig _ _ hc + · rw [if_neg hpx] at hc + have hp := (h.hOk.par_mem _ _ hc).2 + have hpx' : p ≠ x := fun e => hpx (e ▸ hc) + rw [h.hrev p hp hpx']; exact h.hI.orig _ _ hc + +theorem desc' (a y : U64) (hd : Mdb.Desc (Mdb.removePar x par) y a) : isPMA st s' a y = true := by + obtain ⟨hyx, hax, hd⟩ := (h.descE y a).1 hd + obtain ⟨hy, ha⟩ := desc_memA h.hOk hd + have h0 := h.hI.desc a y hd + rw [h.pm ha hax hy hyx] + split + · rename_i hyn + obtain ⟨hn0, rfl⟩ := hyn + have hpn : L.mem s (wa (NxA st s x) 16#u64) = x := (h.hM.next_linkA h.hx hn0).2 + have hxa : Mdb.Desc par x a := by + rcases h.hM.prev_in_blockA h.hOk hd with e | e + · rw [hpn] at e; exact absurd e.symm hax + · rwa [hpn] at e + have hx1 := h.hI.desc a x hxa + unfold isPMA isPFA at h0 hx1 + unfold isPFA + cases hfx : fbA st s x + · simpa using h0 + · simp only [hfx, Bool.not_true, Bool.and_false, Bool.or_false, Bool.and_true, Bool.or_true] at hx1 ⊢ + simp only [Bool.and_eq_true] at hx1 h0 ⊢ + refine ⟨h0.1, ?_⟩ + revert hx1; split <;> simp_all + · exact h0 + +theorem after' (y : U64) (hy : y ∈ (remG x G).flatten) (hn : NxA st s' y ≠ 0#u64) (a : U64) + (ha : a = y ∨ Mdb.Desc (Mdb.removePar x par) y a) + (hnd : ¬ Mdb.Desc (Mdb.removePar x par) (NxA st s' y) a) : isPMA st s' a (NxA st s' y) = false := by + obtain ⟨hy, hyx⟩ := (h.memE y).1 hy + have ha' : a = y ∨ Mdb.Desc par y a := by + rcases ha with e | e + · exact Or.inl e + · exact Or.inr ((h.descE y a).1 e).2.2 + have hax : a ≠ x := by + rcases ha with e | e + · rw [e]; exact hyx + · exact ((h.descE y a).1 e).2.1 + have ham : a ∈ G.flatten := by + rcases ha' with e | e + · rw [e]; exact hy + · exact (desc_memA h.hOk e).2 + rw [h.hnext y hy hyx] at hn hnd ⊢ + by_cases hyp : L.mem s (wa x 16#u64) ≠ 0#u64 ∧ y = L.mem s (wa x 16#u64) + · rw [if_pos hyp] at hn hnd ⊢ + obtain ⟨hp0, rfl⟩ := hyp + have hxl := h.hM.prev_linkA h.hx hp0 + have hN := (h.hM.next_linkA h.hx hn).1 + have hNx : NxA st s x ≠ x := h.hM.next_ne_selfA h.hOk.nodup h.hx hn + have hnd' : ¬ Mdb.Desc par (NxA st s x) a := fun e => hnd ((h.descE _ a).2 ⟨hNx, hax, e⟩) + by_cases hxa : Mdb.Desc par x a + · exact h.pm_false ham hax hN hNx (h.hI.after x h.hx hn a (Or.inr hxa) hnd') + · have hx0 : x ≠ 0#u64 := h.nz h.hx + have h1 := h.hI.after (L.mem s (wa x 16#u64)) hxl.1 (by rw [hxl.2]; exact hx0) a ha' (by rw [hxl.2]; exact hxa) + rw [hxl.2] at h1 + rw [h.pm ham hax hN hNx, if_pos ⟨hn, rfl⟩] + obtain ⟨ch, hch, A, B, C, he, hxB⟩ := h.hM.stretchA h.hOk hxl.1 ha' hxl.2 hx0 rfl hn + have hca' : Mdb.CA (srFA st s) a (B ++ NxA st s x :: C) := by + have := h.hI.chunk ch hch; rw [he] at this; exact Mdb.chunkL_ca this + cases hc : isPFA (revA st s a) (capA st s a) (capA st s (NxA st s x)) (fbA st s (NxA st s x) || fbA st s x) + · rfl + exfalso + unfold isPFA at hc + simp only [Bool.and_eq_true] at hc + obtain ⟨⟨hra, hsN⟩, hbN⟩ := hc + have hca := ca_srMA hca' (Or.inr hra) + have hsx : srMA st s a x = true := Mdb.ca_use hca hsN x hxB + unfold isPMA isPFA at h1 + unfold srMA at hsx + rw [hra, hsx] at h1 + cases hba : isBadgedA (capA st s a) + · simp [hba] at h1 + rw [hba] at h1 hbN + simp only [↓reduceIte, Bool.true_and] at h1 hbN + have ha0 : badgeA (capA st s a) ≠ 0#u64 := by intro e; simp [e] at h1 + have e0 : (badgeA (capA st s a) == 0#u64) = false := by simpa using ha0 + rw [e0, Bool.false_or] at h1 hbN + simp only [Bool.and_eq_true, beq_iff_eq, Bool.not_eq_true', Bool.or_eq_false_iff] at hbN + obtain ⟨hbNa, hfN, hfx⟩ := hbN + rw [hfx] at h1 + simp only [Bool.not_false, Bool.and_true, beq_eq_false_iff_ne] at h1 + have hbx := (sraA_badged hba hsx).1 + have hfN' := h.hI.badges x h.hx hn hbx (by unfold srMA; exact sraA_badged_trans hba hsx hsN) + (by rw [hbNa]; exact h1) (by rw [hbNa]; exact ha0) + rw [hfN'] at hfN; cases hfN + · rw [if_neg hyp] at hn hnd ⊢ + have hw := (h.hM.next_linkA hy hn).1 + have hwx : NxA st s y ≠ x := by + intro e + have := (h.hM.next_linkA hy hn).2 + rw [e] at this + exact hyp ⟨fun e0 => by rw [e0] at this; exact h.nz hy this.symm, this.symm⟩ + have hnd' : ¬ Mdb.Desc par (NxA st s y) a := fun e => hnd ((h.descE _ a).2 ⟨hwx, hax, e⟩) + exact h.pm_false ham hax hw hwx (h.hI.after y hy hn a ha' hnd') + +theorem chunk' (ch' : List U64) (hch' : ch' ∈ remG x G) : Mdb.ChunkL (srFA st s') ch' := by + rw [remG_eq] at hch' + unfold Mdb.removeChains at hch' + obtain ⟨hch', -⟩ := List.mem_filter.1 hch' + obtain ⟨ch, hch, rfl⟩ := List.mem_map.1 hch' + letI : BEq U64 := instBEqOfDecidableEq + have hchm : ∀ z, z ∈ ch → z ∈ G.flatten := fun z hz => List.mem_flatten.2 ⟨_, hch, hz⟩ + have hnd0 : ch.Nodup := (List.nodup_flatten.1 h.hOk.nodup).1 _ hch + have hne : ∀ z, z ∈ ch.erase x → z ≠ x := fun z hz e => by + subst e; exact (List.Nodup.not_mem_erase hnd0) hz + have hm : ∀ z, z ∈ ch.erase x → z ∈ G.flatten := fun z hz => hchm z (List.erase_subset hz) + have hk : Mdb.ChunkL (srFA st s) (ch.erase x) := by + by_cases hxc : x ∈ ch + · obtain ⟨A, B, rfl⟩ := List.append_of_mem hxc + rw [Mdb.erase_mid (Mdb.nodup_mid hnd0).1] + exact Mdb.chunkL_erase (h.hI.chunk _ hch) + · rw [Mdb.erase_not_mem' hxc]; exact h.hI.chunk _ hch + exact Mdb.chunkL_congr hk (fun u hu v hv => srFA_congr (h.sm (hm u hu) (hne u hu) (hm v hv) (hne v hv)) + (by rw [h.hcap u (hm u hu) (hne u hu)]) (h.hrev u (hm u hu) (hne u hu))) + +theorem badges' (y : U64) (hy : y ∈ (remG x G).flatten) (hn : NxA st s' y ≠ 0#u64) + (hbg : isBadgedA (capA st s' y) = true) (hsr : srMA st s' y (NxA st s' y) = true) + (hbne : badgeA (capA st s' y) ≠ badgeA (capA st s' (NxA st s' y))) + (hb0 : badgeA (capA st s' (NxA st s' y)) ≠ 0#u64) : fbA st s' (NxA st s' y) = true := by + obtain ⟨hy, hyx⟩ := (h.memE y).1 hy + rw [h.hnext y hy hyx] at hn hsr hbne hb0 ⊢ + rw [h.hcap y hy hyx] at hbg hbne + by_cases hyp : L.mem s (wa x 16#u64) ≠ 0#u64 ∧ y = L.mem s (wa x 16#u64) + · rw [if_pos hyp] at hn hsr hbne hb0 ⊢ + obtain ⟨hp0, rfl⟩ := hyp + have hxl := h.hM.prev_linkA h.hx hp0 + have hN := (h.hM.next_linkA h.hx hn).1 + have hNx : NxA st s x ≠ x := h.hM.next_ne_selfA h.hOk.nodup h.hx hn + rw [h.hcap _ hN hNx] at hbne hb0 + rw [h.sm hy hyx hN hNx] at hsr + rw [h.hfb _ hN hNx, if_pos ⟨hn, rfl⟩] + cases hfx : fbA st s x + · rw [Bool.or_false] + have hx0 : x ≠ 0#u64 := h.nz h.hx + obtain ⟨ch, hch, A, B, C, he, hxB⟩ := h.hM.stretchA h.hOk hxl.1 (Or.inl rfl) hxl.2 hx0 rfl hn + have hca : Mdb.CA (srMA st s) (L.mem s (wa x 16#u64)) (B ++ NxA st s x :: C) := by + have := h.hI.chunk ch hch; rw [he] at this + exact ca_srMA (Mdb.chunkL_ca this) (Or.inl (isFrA_of_badged hbg)) + have hsx : srMA st s (L.mem s (wa x 16#u64)) x = true := Mdb.ca_use hca hsr x hxB + unfold srMA at hsx hsr + have hbx := (sraA_badged hbg hsx).1 + by_cases hbxN : badgeA (capA st s x) = badgeA (capA st s (NxA st s x)) + · have := h.hI.badges (L.mem s (wa x 16#u64)) hxl.1 (by rw [hxl.2]; exact hx0) hbg (by rw [hxl.2]; exact hsx) + (by rw [hxl.2, hbxN]; exact hbne) (by rw [hxl.2, hbxN]; exact hb0) + rw [hxl.2, hfx] at this; cases this + · exact h.hI.badges x h.hx hn hbx (by unfold srMA; exact sraA_badged_trans hbg hsx hsr) hbxN hb0 + · simp + · rw [if_neg hyp] at hn hsr hbne hb0 ⊢ + have hw := (h.hM.next_linkA hy hn).1 + have hwx : NxA st s y ≠ x := by + intro e + have := (h.hM.next_linkA hy hn).2 + rw [e] at this + exact hyp ⟨fun e0 => by rw [e0] at this; exact h.nz hy this.symm, this.symm⟩ + rw [h.hcap _ hw hwx] at hbne hb0 + rw [h.sm hy hyx hw hwx] at hsr + have := h.hI.badges y hy hn hbg hsr hbne hb0 + rw [h.hfb _ hw hwx] + split + · simp [this] + · exact this + +theorem utrev' (z : U64) (hz : z ∈ (remG x G).flatten) (hu : isUtA (capA st s' z) = true) : + revA st s' z = true := by + obtain ⟨hz, hzx⟩ := (h.memE z).1 hz + rw [h.hcap z hz hzx] at hu; rw [h.hrev z hz hzx]; exact h.hI.utrev z hz hu + +theorem self' (z : U64) (hz : z ∈ (remG x G).flatten) (hzb : isZbA (capA st s' z) = false) : + srMA st s' z z = true := by + obtain ⟨hz, hzx⟩ := (h.memE z).1 hz + rw [h.hcap z hz hzx] at hzb; rw [h.sm hz hzx hz hzx]; exact h.hI.self z hz hzb + +/-- **`MdbInvA` after emptying a slot.** -/ +theorem inv : MdbInvA st (remG x G) (Mdb.removePar x par) s' := + ⟨h.orig', h.desc', h.after', h.chunk', h.badges', h.utrev', h.self'⟩ + +end EmpCtxA + +end + +end ST +end R +end W7 + +end + +end Fl4ProofsA64.P1 diff --git a/proofs/Fl4ProofsA64/P1/MdbIrqInvA.lean b/proofs/Fl4ProofsA64/P1/MdbIrqInvA.lean new file mode 100644 index 000000000..e2b3ce16f --- /dev/null +++ b/proofs/Fl4ProofsA64/P1/MdbIrqInvA.lean @@ -0,0 +1,484 @@ +/- +P1 (aarch64, A64-P1-MDB wave 3): **`IrqRunA` and `HandlersActiveA` (`valid_irq_handlers`)** +(x86 counterparts: `Fl4Proofs.P3.FinBase` / `FinOps` for `OKey.irq`, `Fl4Proofs.P3.IrqControl` `HandlersActive`; +l4v sources: `CSpace_AI.thy` `mdb_chunked`, `Invariants_AI.thy` `valid_irq_handlers`). + +* **`IrqRunA st G s`**: character-for-character match to CNode consumer `Fl4ProofsA64.P3.IrqRunA` + (`CnodeFinalA.lean:243–248`): all `IrqHandler i` caps for one IRQ `i` lie in one MDB chain, contiguously. +* **`HandlersActiveA st G s`**: every `IrqHandler i` cap in `G` has `i.val < 5376` and a nonzero IRQ-state word + `L.mem s (irqStAddrA (L.ksb s) i) ≠ 0#u64`. +* Mutual-dependence bridges (F4): + * **`HandlersActiveA.fresh_of_inactive`**: an inactive IRQ (`irq_state = 0`) has no `IrqHandler irq` cap in `G` + (needed by `invoke_irq_control` to preserve `IrqRunA` and `MdbInvA`). + * **`IrqRunA.final_unique`**: a final `IrqHandler i` cap (`is_final_capability_with_cap = ok true`) is the unique + `IrqHandler i` cap in `G` (needed by `empty_slot` + `deleted_irq_handler` to preserve `HandlersActiveA`). +* Step preservation across `empty`, `of_no_irq`, `keepO`, `keepIrq`, `ren` (move/swap), `rem` (`empty_slot`), + `ins_derived`, `ins_non_irq`, `ins_fresh` (`invoke_irq_control`), and `empty_irq` (`deleted_irq_handler`). + +No named premise. New P1 module; zero edits to existing files. +-/ +import Fl4ProofsA64.P1.MdbInvInsA +import Fl4ProofsA64.P1.MdbSwap +import Fl4ProofsA64.P1.MdbIns +import Fl4ProofsA64.P1.MdbRem +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P1 +open fl4_core_a64 Fl4ProofsA64.P0.Consts Fl4Proofs.P3 + +section +variable {S : Type _} {st : machine.Store S} [L : LawfulStore st] + +local notation "wa" => core.num.U64.wrapping_add + +namespace W7 +namespace R +namespace ST + +noncomputable section + +/-! ## Definitions -/ + +/-- **The handler caps of one IRQ lie in one MDB chain, contiguously** (exact match to `CnodeFinalA.IrqRunA`). -/ +structure IrqRunA (st : machine.Store S) [L : LawfulStore st] (G : List (List U64)) (s : S) : Prop where + one : ∀ y ∈ G.flatten, ∀ y' ∈ G.flatten, ∀ i, capA st s y = .IrqHandler i → capA st s y' = .IrqHandler i → + ∃ ch ∈ G, y ∈ ch ∧ y' ∈ ch + chunk : ∀ ch ∈ G, ∀ (a b c : Nat) (y z y' : U64) (i : U64), ch[a]? = some y → ch[b]? = some z → ch[c]? = some y' → + a < b → b < c → capA st s y = .IrqHandler i → capA st s y' = .IrqHandler i → capA st s z = .IrqHandler i + +/-- Address of IRQ `i`'s state word (`ks::IRQ_STATE + 8 * i`, `ks::IRQ_STATE = 224`). -/ +def irqStAddrA (kb i : U64) : U64 := + wa kb (wa 224#u64 (core.num.U64.wrapping_mul i 8#u64)) + +/-- **IRQ handler caps exist only for active IRQs** (l4v `valid_irq_handlers`; x86 `HandlersActive`, A64 5376 IRQs). -/ +def HandlersActiveA (st : machine.Store S) [L : LawfulStore st] (G : List (List U64)) (s : S) : Prop := + ∀ y ∈ G.flatten, ∀ i, capA st s y = .IrqHandler i → + i.val < 5376 ∧ L.mem s (irqStAddrA (L.ksb s) i) ≠ 0#u64 + +/-! ## Boot and basic transport -/ + +theorem IrqRunA.of_no_irq {G : List (List U64)} {s : S} + (h : ∀ y ∈ G.flatten, ∀ i, capA st s y ≠ .IrqHandler i) : IrqRunA st G s where + one y hy _ _ i hc := absurd hc (h y hy i) + chunk ch hch _ _ _ y _ _ i ha _ _ _ _ hc := + absurd hc (h y (List.mem_flatten.2 ⟨ch, hch, List.mem_of_getElem? ha⟩) i) + +theorem IrqRunA.empty {s : S} : IrqRunA st [] s := + IrqRunA.of_no_irq fun _ hy => by simp at hy + +theorem HandlersActiveA.of_no_irq {G : List (List U64)} {s : S} + (h : ∀ y ∈ G.flatten, ∀ i, capA st s y ≠ .IrqHandler i) : HandlersActiveA st G s := + fun y hy i hc => absurd hc (h y hy i) + +theorem HandlersActiveA.empty {s : S} : HandlersActiveA st [] s := + HandlersActiveA.of_no_irq fun _ hy => by simp at hy + +theorem IrqRunA.keepIrq {G : List (List U64)} {s s' : S} (h : IrqRunA st G s) + (hc : ∀ y ∈ G.flatten, ∀ i, capA st s' y = .IrqHandler i ↔ capA st s y = .IrqHandler i) : + IrqRunA st G s' where + one y hy y' hy' i k k' := h.one y hy y' hy' i ((hc y hy i).1 k) ((hc y' hy' i).1 k') + chunk ch hch a b c y z y' i ha hb hc' hab hbc ky ky' := by + have mem : ∀ {n : Nat} {w : U64}, ch[n]? = some w → w ∈ G.flatten := fun e => + List.mem_flatten.2 ⟨ch, hch, List.mem_of_getElem? e⟩ + exact (hc z (mem hb) i).2 + (h.chunk ch hch a b c y z y' i ha hb hc' hab hbc ((hc y (mem ha) i).1 ky) ((hc y' (mem hc') i).1 ky')) + +theorem IrqRunA.keepO {OS : Set U64} {G : List (List U64)} {s s' : S} (hM : MdbLk st OS G s) + (h : IrqRunA st G s) (k : OSKeep st OS s s') : IrqRunA st G s' := + h.keepIrq fun y hy _ => by rw [k.capA ((hM.members y).1 hy).1] + +theorem HandlersActiveA.keepIrq {G : List (List U64)} {s s' : S} (h : HandlersActiveA st G s) + (hc : ∀ y ∈ G.flatten, ∀ i, capA st s' y = .IrqHandler i → capA st s y = .IrqHandler i) + (hk : L.ksb s' = L.ksb s) + (hst : ∀ i : U64, i.val < 5376 → L.mem s' (irqStAddrA (L.ksb s) i) = L.mem s (irqStAddrA (L.ksb s) i)) : + HandlersActiveA st G s' := fun y hy i k => by + have ⟨h1, h2⟩ := h y hy i (hc y hy i k) + exact ⟨h1, by rw [hk, hst i h1]; exact h2⟩ + +theorem HandlersActiveA.keepO {OS : Set U64} {G : List (List U64)} {s s' : S} (hM : MdbLk st OS G s) + (h : HandlersActiveA st G s) (k : OSKeep st OS s s') (hk : L.ksb s' = L.ksb s) + (hst : ∀ i : U64, i.val < 5376 → L.mem s' (irqStAddrA (L.ksb s) i) = L.mem s (irqStAddrA (L.ksb s) i)) : + HandlersActiveA st G s' := + h.keepIrq (fun y hy _ e => by rwa [k.capA ((hM.members y).1 hy).1] at e) hk hst + +/-! ## Freshness from inactive IRQ (for `invoke_irq_control`) -/ + +/-- An inactive IRQ has no `IrqHandler` cap in `G` (x86 `fresh_of_inactive`). -/ +theorem HandlersActiveA.fresh_of_inactive {G : List (List U64)} {s : S} (h : HandlersActiveA st G s) + {irq : U64} (hin : L.mem s (irqStAddrA (L.ksb s) irq) = 0#u64) : + ∀ y ∈ G.flatten, capA st s y ≠ .IrqHandler irq := + fun y hy hc => (h y hy irq hc).2 hin + +theorem HandlersActiveA.fresh_sra {G : List (List U64)} {s : S} (h : HandlersActiveA st G s) + {irq : U64} (hin : L.mem s (irqStAddrA (L.ksb s) irq) = 0#u64) : + ∀ y ∈ G.flatten, sraA (.IrqHandler irq) (capA st s y) = false := fun y hy => by + rw [sraA_irqH_left] + simp [h.fresh_of_inactive hin y hy] + +/-! ## Uniqueness from `is_final_capability_with_cap` (for `deleted_irq_handler`) -/ + +private theorem soa_irq_self (i : U64) : objects.same_object_as (.IrqHandler i) (.IrqHandler i) = ok true := by + simp [objects.same_object_as, objects.same_region_as] + +/-- Under `IrqRunA` + `MdbLk`, if `x` holds `IrqHandler i` and `is_final_capability_with_cap = ok true`, +then no other slot in `G` holds `IrqHandler i`. -/ +theorem IrqRunA.final_unique {OS : Set U64} {G : List (List U64)} {s : S} (h0 : ∀ z ∈ OS, z ≠ 0#u64) + (hM : MdbLk st OS G s) (hI : IrqRunA st G s) {x i : U64} (hx : x ∈ G.flatten) + (hcx : capA st s x = .IrqHandler i) + (hfin : cdt.is_final_capability_with_cap st s x (.IrqHandler i) = ok true) : + ∀ y ∈ G.flatten, y ≠ x → capA st s y ≠ .IrqHandler i := by + intro y hy hyx hcy + obtain ⟨ch, hch, hxch, hych⟩ := hI.one x hx y hy i hcx hcy + obtain ⟨ix, hix, hgx⟩ := List.getElem_of_mem hxch + obtain ⟨iy, hiy, hgy⟩ := List.getElem_of_mem hych + have hne : ix ≠ iy := fun e => by subst e; exact hyx (by rw [← hgy, ← hgx]) + have hprev_eq : ∀ {k : Nat} (hk : k + 1 < ch.length), + L.mem s (wa ch[k + 1] 16#u64) = ch[k] ∧ NxA st s ch[k] = ch[k + 1] := by + intro k hk + have ⟨e1, e2⟩ := hM.chain ch hch k ch[k] ch[k + 1] + (List.getElem?_eq_getElem (by omega)) (List.getElem?_eq_getElem hk) + exact ⟨e2, e1⟩ + have hprev_fn : obj.Slot.prev st x s = ok (L.mem s (wa x 16#u64)) := by + unfold obj.Slot.prev; simp [cte_prev, L.read_eq, lift, bind_ok] + have hnext_fn : obj.Slot.next st x s = ok (NxA st s x) := next_eq s x + have hmem_nz : ∀ {k : Nat} (hk : k < ch.length), ch[k] ≠ 0#u64 := fun {k} hk => + h0 ch[k] ((hM.members ch[k]).1 (List.mem_flatten.2 ⟨ch, hch, List.getElem_mem hk⟩)).1 + rcases lt_or_gt_of_ne hne with hlt | hgt + · have h1 : ix + 1 < ch.length := by omega + have hnx : NxA st s x = ch[ix + 1] := by rw [← hgx]; exact (hprev_eq h1).2 + have hcz : capA st s ch[ix + 1] = .IrqHandler i := by + by_cases e : ix + 1 = iy + · simp only [e, hgy]; exact hcy + · exact hI.chunk ch hch ix (ix + 1) iy x ch[ix + 1] y i + (by rw [List.getElem?_eq_getElem hix, hgx]) + (by rw [List.getElem?_eq_getElem h1]) + (by rw [List.getElem?_eq_getElem hiy, hgy]) + (by omega) (by omega) hcx hcy + have hnz : ch[ix + 1] ≠ 0#u64 := hmem_nz h1 + unfold cdt.is_final_capability_with_cap at hfin + rw [hprev_fn, bind_ok] at hfin + simp only [obj.Slot.is_null, bind_ok] at hfin + by_cases hp0 : L.mem s (wa x 16#u64) = 0#u64 + · simp [hp0, hnext_fn, hnx, hnz, capA_eq, hcz, soa_irq_self, bind_ok] at hfin + · simp only [hp0, decide_false, Bool.false_eq_true, ↓reduceIte, capA_eq, bind_ok] at hfin + obtain ⟨b, hb⟩ : ∃ b, objects.same_object_as (capA st s (L.mem s (wa x 16#u64))) (.IrqHandler i) = ok b := by + have hp := P0.objects.same_object_as.p0 (capA st s (L.mem s (wa x 16#u64))) (.IrqHandler i) + revert hp; cases objects.same_object_as _ _ <;> simp [WP.spec_vis, WP.spec_div, WP.spec_ok] + rw [hb, bind_ok] at hfin + cases b + · simp [hnext_fn, hnx, hnz, hcz, soa_irq_self, bind_ok] at hfin + · simp at hfin + · have h1 : ix - 1 + 1 < ch.length := by omega + have hix' : ix - 1 + 1 = ix := by omega + have hpv : L.mem s (wa x 16#u64) = ch[ix - 1] := by + have := (hprev_eq h1).1 + simp only [hix', hgx] at this; exact this + have hcz : capA st s ch[ix - 1] = .IrqHandler i := by + by_cases e : iy = ix - 1 + · simp only [← e, hgy]; exact hcy + · exact hI.chunk ch hch iy (ix - 1) ix y ch[ix - 1] x i + (by rw [List.getElem?_eq_getElem hiy, hgy]) + (by rw [List.getElem?_eq_getElem (by omega)]) + (by rw [List.getElem?_eq_getElem hix, hgx]) + (by omega) (by omega) hcy hcx + have hnz : ch[ix - 1] ≠ 0#u64 := hmem_nz (by omega) + unfold cdt.is_final_capability_with_cap at hfin + rw [hprev_fn, hpv, bind_ok] at hfin + simp [obj.Slot.is_null, hnz, capA_eq, hcz, soa_irq_self, bind_ok] at hfin + +/-! ## Renaming (`move_cap`, `swap`, `cap_swap_for_delete`) -/ + +/-- `IrqRunA` along a transposition `swp x1 x2` (`renG x1 x2 G`). -/ +theorem IrqRunA.ren {G : List (List U64)} {s s' : S} (h : IrqRunA st G s) {x1 x2 : U64} + (hc : ∀ y ∈ G.flatten, ∀ i, capA st s' (swp x1 x2 y) = .IrqHandler i ↔ capA st s y = .IrqHandler i) : + IrqRunA st (renG x1 x2 G) s' where + one y hy y' hy' i k k' := by + have a := mem_renG_flat.1 hy + have a' := mem_renG_flat.1 hy' + have k0 : capA st s (swp x1 x2 y) = .IrqHandler i := (hc _ a i).1 (by rwa [swp_swp]) + have k0' : capA st s (swp x1 x2 y') = .IrqHandler i := (hc _ a' i).1 (by rwa [swp_swp]) + obtain ⟨c0, hc0, hy0, hy0'⟩ := h.one _ a _ a' i k0 k0' + exact ⟨c0.map (swp x1 x2), List.mem_map.2 ⟨c0, hc0, rfl⟩, + List.mem_map.2 ⟨_, hy0, swp_swp ..⟩, List.mem_map.2 ⟨_, hy0', swp_swp ..⟩⟩ + chunk ch hch a b c y z y' i ha hb hc' hab hbc ky ky' := by + obtain ⟨c0, hc0, rfl⟩ := List.mem_map.1 hch + rw [List.getElem?_map] at ha hb hc' + obtain ⟨y0, hy0, rfl⟩ := Option.map_eq_some_iff.1 ha + obtain ⟨z0, hz0, rfl⟩ := Option.map_eq_some_iff.1 hb + obtain ⟨y1, hy1, rfl⟩ := Option.map_eq_some_iff.1 hc' + have mem : ∀ {n : Nat} {w : U64}, c0[n]? = some w → w ∈ G.flatten := fun e => + List.mem_flatten.2 ⟨c0, hc0, List.mem_of_getElem? e⟩ + exact (hc z0 (mem hz0) i).2 + (h.chunk c0 hc0 a b c y0 z0 y1 i hy0 hz0 hy1 hab hbc + ((hc y0 (mem hy0) i).1 ky) ((hc y1 (mem hy1) i).1 ky')) + +/-- `HandlersActiveA` along a transposition `swp x1 x2` (`renG x1 x2 G`). -/ +theorem HandlersActiveA.ren {G : List (List U64)} {s s' : S} (h : HandlersActiveA st G s) {x1 x2 : U64} + (hc : ∀ y ∈ G.flatten, ∀ i, capA st s' (swp x1 x2 y) = .IrqHandler i → capA st s y = .IrqHandler i) + (hk : L.ksb s' = L.ksb s) + (hst : ∀ i : U64, i.val < 5376 → L.mem s' (irqStAddrA (L.ksb s) i) = L.mem s (irqStAddrA (L.ksb s) i)) : + HandlersActiveA st (renG x1 x2 G) s' := fun y hy i k => by + have a := mem_renG_flat.1 hy + have k0 : capA st s (swp x1 x2 y) = .IrqHandler i := hc _ a i (by rwa [swp_swp]) + have ⟨h1, h2⟩ := h _ a i k0 + exact ⟨h1, by rw [hk, hst i h1]; exact h2⟩ + +/-! ## Removal (`empty_slot`) -/ + +/-- `IrqRunA` along removing slot `x` from `G` (`remG x G`). -/ +theorem IrqRunA.rem {G : List (List U64)} {s s' : S} (h : IrqRunA st G s) (hnd : G.flatten.Nodup) {x : U64} + (hk : ∀ y ∈ G.flatten, y ≠ x → capA st s' y = capA st s y) : IrqRunA st (remG x G) s' := by + have hk' : ∀ y ∈ (remG x G).flatten, capA st s' y = capA st s y := fun y hy => + hk y ((remG_flat hnd).1 hy).1 ((remG_flat hnd).1 hy).2 + refine ⟨fun y hy y' hy' i k k' => ?_, fun ch' hch' a b c y z y' i ha hb hc' hab hbc ky ky' => ?_⟩ + · rw [hk' y hy] at k; rw [hk' y' hy'] at k' + have ay := (remG_flat hnd).1 hy + have ay' := (remG_flat hnd).1 hy' + obtain ⟨ch, hch, hych, hy'ch⟩ := h.one y ay.1 y' ay'.1 i k k' + have hndc : ch.Nodup := (List.nodup_flatten.1 hnd).1 ch hch + have hye : y ∈ ch.erase x := (List.mem_erase_of_ne ay.2).2 hych + have hy'e : y' ∈ ch.erase x := (List.mem_erase_of_ne ay'.2).2 hy'ch + have hne : ch.erase x ≠ [] := fun e => by rw [e] at hye; cases hye + exact ⟨ch.erase x, remG_of hch hne, hye, hy'e⟩ + · obtain ⟨ch, hch, rfl, -⟩ := remG_mem hch' + have hndc : ch.Nodup := (List.nodup_flatten.1 hnd).1 ch hch + have hmem : ∀ w ∈ ch.erase x, w ∈ (remG x G).flatten := fun w hw => + List.mem_flatten.2 ⟨ch.erase x, hch', hw⟩ + rw [hk' y (hmem y (List.mem_of_getElem? ha))] at ky + rw [hk' y' (hmem y' (List.mem_of_getElem? hc'))] at ky' + rw [hk' z (hmem z (List.mem_of_getElem? hb))] + by_cases hxc : x ∈ ch + · obtain ⟨A, B, hsp⟩ := List.append_of_mem hxc + have hnxA : x ∉ A := by + rw [hsp, List.nodup_append] at hndc + intro hA; exact hndc.2.2 x hA x (List.mem_cons_self ..) rfl + have he : ch.erase x = A ++ B := by + rw [hsp, List.erase_append_right _ hnxA, List.erase_cons_head] + rw [he] at ha hb hc' + -- Map indices `n` in `A ++ B` to `if n < A.length then n else n + 1` in `A ++ x :: B`. + have mapI : ∀ {n : Nat} {w : U64}, (A ++ B)[n]? = some w → + ch[if n < A.length then n else n + 1]? = some w := by + intro n w hn + rw [hsp] + by_cases hlt : n < A.length + · simp only [hlt, ↓reduceIte] + rw [List.getElem?_append_left hlt] at hn ⊢; exact hn + · simp only [hlt, ↓reduceIte] + rw [List.getElem?_append_right (by omega)] at hn + rw [List.getElem?_append_right (by omega)] + have e : n + 1 - A.length = (n - A.length) + 1 := by omega + rw [e]; exact hn + have ha' := mapI ha; have hb' := mapI hb; have hc'' := mapI hc' + exact h.chunk ch hch _ _ _ y z y' i ha' hb' hc'' (by split_ifs <;> omega) (by split_ifs <;> omega) ky ky' + · rw [List.erase_of_not_mem hxc] at ha hb hc' + exact h.chunk ch hch a b c y z y' i ha hb hc' hab hbc ky ky' + +/-- `HandlersActiveA` along removing slot `x` (IRQ states unchanged). -/ +theorem HandlersActiveA.rem {G : List (List U64)} {s s' : S} (h : HandlersActiveA st G s) (hnd : G.flatten.Nodup) + {x : U64} (hk : ∀ y ∈ G.flatten, y ≠ x → capA st s' y = capA st s y) + (hk' : L.ksb s' = L.ksb s) + (hst : ∀ i : U64, i.val < 5376 → L.mem s' (irqStAddrA (L.ksb s) i) = L.mem s (irqStAddrA (L.ksb s) i)) : + HandlersActiveA st (remG x G) s' := fun y hy i k => by + have ay := (remG_flat hnd).1 hy + rw [hk y ay.1 ay.2] at k + have ⟨h1, h2⟩ := h y ay.1 i k + exact ⟨h1, by rw [hk', hst i h1]; exact h2⟩ + +/-- `HandlersActiveA` along `empty_slot` with `IrqHandler irq` cleanup (`deleted_irq_handler` sets +`irq_state(irq) := 0`, keeping every other IRQ's state), given that `x` was the unique `IrqHandler irq` in `G`. -/ +theorem HandlersActiveA.empty_irq {G : List (List U64)} {s s' : S} (h : HandlersActiveA st G s) + (hnd : G.flatten.Nodup) {x irq : U64} + (huniq : ∀ y ∈ G.flatten, y ≠ x → capA st s y ≠ .IrqHandler irq) + (hk : ∀ y ∈ G.flatten, y ≠ x → capA st s' y = capA st s y) + (hk' : L.ksb s' = L.ksb s) + (hst : ∀ j : U64, j.val < 5376 → j ≠ irq → + L.mem s' (irqStAddrA (L.ksb s) j) = L.mem s (irqStAddrA (L.ksb s) j)) : + HandlersActiveA st (remG x G) s' := fun y hy j k => by + have ay := (remG_flat hnd).1 hy + rw [hk y ay.1 ay.2] at k + have hjne : j ≠ irq := fun e => huniq y ay.1 ay.2 (e ▸ k) + have ⟨h1, h2⟩ := h y ay.1 j k + exact ⟨h1, by rw [hk', hst j h1 hjne]; exact h2⟩ + +/-! ## Insertion (`insert_with_cap`, `insert`, `insert_new_cap`, `invoke_irq_control`) -/ + +/-- `IrqRunA` along inserting `dest` after `src` (`insG src dest G`), when either: +(a) `dest` is not an `IrqHandler`, OR +(b) `dest` is `IrqHandler i` and `src` holds `IrqHandler i` (derived copy), OR +(c) `dest` is `IrqHandler i` and no existing slot in `G` holds `IrqHandler i` (fresh `invoke_irq_control`). -/ +theorem IrqRunA.ins {G : List (List U64)} {s s' : S} (h : IrqRunA st G s) (hnd : G.flatten.Nodup) + {src dest : U64} (hsrc : src ∈ G.flatten) (hdest : dest ∉ G.flatten) + (hk : ∀ y ∈ G.flatten, ∀ i, capA st s' y = .IrqHandler i ↔ capA st s y = .IrqHandler i) + (hd : ∀ i, capA st s' dest = .IrqHandler i → + capA st s src = .IrqHandler i ∨ ∀ y ∈ G.flatten, capA st s y ≠ .IrqHandler i) + (hs_eq : ∀ i, capA st s src = .IrqHandler i → capA st s' dest = .IrqHandler i) : + IrqRunA st (insG src dest G) s' := by + have hfl : ∀ z, z ∈ (insG src dest G).flatten ↔ z = dest ∨ z ∈ G.flatten := by + intro z; rw [mem_insG_flat hsrc] + refine ⟨fun y hy y' hy' i ky ky' => ?_, fun ch' hch' a b c y z y' i ha hb hc' hab hbc ky ky' => ?_⟩ + · -- `one` + rcases (hfl y).1 hy with hyd | hy0 <;> rcases (hfl y').1 hy' with hyd' | hy0' + · rw [hyd, hyd'] + obtain ⟨ch, hch, hsch⟩ := List.mem_flatten.1 hsrc + exact ⟨insA src dest ch, List.mem_map.2 ⟨ch, hch, rfl⟩, + (insA_perm hsch).mem_iff.2 (List.mem_cons_self ..), (insA_perm hsch).mem_iff.2 (List.mem_cons_self ..)⟩ + · rw [hyd] at ky ⊢ + rcases hd i ky with hs | hfr + · obtain ⟨ch, hch, hsch, hy'ch⟩ := h.one src hsrc y' hy0' i hs ((hk y' hy0' i).1 ky') + exact ⟨insA src dest ch, List.mem_map.2 ⟨ch, hch, rfl⟩, + (insA_perm hsch).mem_iff.2 (List.mem_cons_self ..), (insA_perm hsch).mem_iff.2 (List.mem_cons_of_mem _ hy'ch)⟩ + · exact absurd ((hk y' hy0' i).1 ky') (hfr y' hy0') + · rw [hyd'] at ky' ⊢ + rcases hd i ky' with hs | hfr + · obtain ⟨ch, hch, hych, hsch⟩ := h.one y hy0 src hsrc i ((hk y hy0 i).1 ky) hs + exact ⟨insA src dest ch, List.mem_map.2 ⟨ch, hch, rfl⟩, + (insA_perm hsch).mem_iff.2 (List.mem_cons_of_mem _ hych), (insA_perm hsch).mem_iff.2 (List.mem_cons_self ..)⟩ + · exact absurd ((hk y hy0 i).1 ky) (hfr y hy0) + · obtain ⟨ch, hch, hych, hy'ch⟩ := h.one y hy0 y' hy0' i ((hk y hy0 i).1 ky) ((hk y' hy0' i).1 ky') + refine ⟨insA src dest ch, List.mem_map.2 ⟨ch, hch, rfl⟩, ?_, ?_⟩ + · by_cases hsch : src ∈ ch + · exact (insA_perm hsch).mem_iff.2 (List.mem_cons_of_mem _ hych) + · rw [insA_of_not_mem hsch]; exact hych + · by_cases hsch : src ∈ ch + · exact (insA_perm hsch).mem_iff.2 (List.mem_cons_of_mem _ hy'ch) + · rw [insA_of_not_mem hsch]; exact hy'ch + · -- `chunk` + obtain ⟨ch, hch, rfl⟩ := List.mem_map.1 hch' + have hmem_ch : ∀ w ∈ ch, w ∈ G.flatten := fun w hw => List.mem_flatten.2 ⟨ch, hch, hw⟩ + by_cases hsch : src ∈ ch + · obtain ⟨A, B, hsp⟩ := List.append_of_mem hsch + have hndc : ch.Nodup := (List.nodup_flatten.1 hnd).1 ch hch + have hnsA : src ∉ A := by + rw [hsp, List.nodup_append] at hndc + intro hA; exact hndc.2.2 src hA src (List.mem_cons_self ..) rfl + have he : insA src dest ch = A ++ src :: dest :: B := by + rw [hsp, insA_eq, Mdb.insAfter_split hnsA] + rw [he] at ha hb hc' + -- Helper: index `n` in `A ++ src :: dest :: B` corresponds to either `n = A.length + 1` (i.e. `dest`) + -- or an index `idx n` in `ch = A ++ src :: B`. + have get_idx : ∀ {n : Nat} {w : U64}, (A ++ src :: dest :: B)[n]? = some w → + (n = A.length + 1 ∧ w = dest) ∨ + (n ≤ A.length ∧ ch[n]? = some w) ∨ + (A.length + 1 < n ∧ ch[n - 1]? = some w) := by + intro n w hn + by_cases h1 : n < A.length + · right; left; refine ⟨by omega, ?_⟩ + rw [List.getElem?_append_left h1] at hn + rw [hsp, List.getElem?_append_left h1]; exact hn + · by_cases h2 : n = A.length + · right; left; refine ⟨by omega, ?_⟩ + subst h2 + rw [List.getElem?_append_right (le_refl _), Nat.sub_self] at hn + rw [hsp, List.getElem?_append_right (le_refl _), Nat.sub_self]; exact hn + · by_cases h3 : n = A.length + 1 + · left; refine ⟨h3, ?_⟩ + rw [h3, List.getElem?_append_right (by omega)] at hn + have e1 : A.length + 1 - A.length = 1 := by omega + rw [e1] at hn; simpa using hn.symm + · right; right; refine ⟨by omega, ?_⟩ + rw [List.getElem?_append_right (by omega)] at hn + have e2 : n - A.length = (n - A.length - 2) + 2 := by omega + rw [e2] at hn + rw [hsp, List.getElem?_append_right (by omega)] + have e3 : n - 1 - A.length = (n - A.length - 2) + 1 := by omega + rw [e3]; exact hn + have hsrc_at : ch[A.length]? = some src := by + rw [hsp, List.getElem?_append_right (le_refl _), Nat.sub_self]; rfl + rcases get_idx hb with ⟨rfl, rfl⟩ | ⟨hb1, hb2⟩ | ⟨hb1, hb2⟩ + · -- `b = A.length + 1`, so `z = dest`; `a ≤ A.length` and `A.length + 1 < c`, so `y, y' ∈ ch`. + have ha' : a ≤ A.length ∧ ch[a]? = some y := by + rcases get_idx ha with ⟨h0, _⟩ | ⟨h0, h1⟩ | ⟨h0, _⟩ <;> [omega; exact ⟨h0, h1⟩; omega] + have hc'' : ch[c - 1]? = some y' := by + rcases get_idx hc' with ⟨h0, _⟩ | ⟨h0, _⟩ | ⟨_, h1⟩ <;> [omega; omega; exact h1] + obtain ⟨ha1, ha2⟩ := ha' + have ky0 := (hk y (hmem_ch y (List.mem_of_getElem? ha2)) i).1 ky + have ky0' := (hk y' (hmem_ch y' (List.mem_of_getElem? hc'')) i).1 ky' + have hsrc_i : capA st s src = .IrqHandler i := by + by_cases e : a = A.length + · subst e; rw [hsrc_at] at ha2; injection ha2 with es; exact es ▸ ky0 + · exact h.chunk ch hch a A.length (c - 1) y src y' i ha2 hsrc_at hc'' (by omega) (by omega) ky0 ky0' + exact hs_eq i hsrc_i + · -- `b ≤ A.length`, so `z ∈ ch` and `a < b ≤ A.length`, so `y ∈ ch`. + have ha' : ch[a]? = some y := by + rcases get_idx ha with ⟨h0, _⟩ | ⟨_, h1⟩ | ⟨h0, _⟩ <;> [omega; exact h1; omega] + have ky0 := (hk y (hmem_ch y (List.mem_of_getElem? ha')) i).1 ky + rcases get_idx hc' with ⟨_, rfl⟩ | ⟨_, hc1⟩ | ⟨_, hc1⟩ + · -- `y' = dest` + rcases hd i ky' with hsi | hfr + · by_cases e : b = A.length + · have hb2' : ch[A.length]? = some z := by rw [← e]; exact hb2 + rw [hsrc_at] at hb2'; injection hb2' with es + exact (hk z (hmem_ch z (List.mem_of_getElem? hb2)) i).2 (es ▸ hsi) + · exact (hk z (hmem_ch z (List.mem_of_getElem? hb2)) i).2 + (h.chunk ch hch a b A.length y z src i ha' hb2 hsrc_at hab (by omega) ky0 hsi) + · exact absurd ky0 (hfr y (hmem_ch y (List.mem_of_getElem? ha'))) + · exact (hk z (hmem_ch z (List.mem_of_getElem? hb2)) i).2 + (h.chunk ch hch a b c y z y' i ha' hb2 hc1 hab hbc ky0 + ((hk y' (hmem_ch y' (List.mem_of_getElem? hc1)) i).1 ky')) + · exact (hk z (hmem_ch z (List.mem_of_getElem? hb2)) i).2 + (h.chunk ch hch a b (c - 1) y z y' i ha' hb2 hc1 hab (by omega) ky0 + ((hk y' (hmem_ch y' (List.mem_of_getElem? hc1)) i).1 ky')) + · -- `A.length + 1 < b`, so `z ∈ ch` at `b - 1` and `y' ∈ ch` at `c - 1`. + have hc'' : ch[c - 1]? = some y' := by + rcases get_idx hc' with ⟨h0, _⟩ | ⟨h0, _⟩ | ⟨_, h1⟩ <;> [omega; omega; exact h1] + have ky0' := (hk y' (hmem_ch y' (List.mem_of_getElem? hc'')) i).1 ky' + rcases get_idx ha with ⟨_, rfl⟩ | ⟨ha1, ha2⟩ | ⟨ha1, ha2⟩ + · -- `y = dest` + rcases hd i ky with hsi | hfr + · exact (hk z (hmem_ch z (List.mem_of_getElem? hb2)) i).2 + (h.chunk ch hch A.length (b - 1) (c - 1) src z y' i hsrc_at hb2 hc'' (by omega) (by omega) hsi ky0') + · exact absurd ky0' (hfr y' (hmem_ch y' (List.mem_of_getElem? hc''))) + · exact (hk z (hmem_ch z (List.mem_of_getElem? hb2)) i).2 + (h.chunk ch hch a (b - 1) (c - 1) y z y' i ha2 hb2 hc'' (by omega) (by omega) + ((hk y (hmem_ch y (List.mem_of_getElem? ha2)) i).1 ky) ky0') + · exact (hk z (hmem_ch z (List.mem_of_getElem? hb2)) i).2 + (h.chunk ch hch (a - 1) (b - 1) (c - 1) y z y' i ha2 hb2 hc'' (by omega) (by omega) + ((hk y (hmem_ch y (List.mem_of_getElem? ha2)) i).1 ky) ky0') + · rw [insA_of_not_mem hsch] at ha hb hc' + have mem : ∀ {n : Nat} {w : U64}, ch[n]? = some w → w ∈ G.flatten := fun e => + List.mem_flatten.2 ⟨ch, hch, List.mem_of_getElem? e⟩ + exact (hk z (mem hb) i).2 + (h.chunk ch hch a b c y z y' i ha hb hc' hab hbc + ((hk y (mem ha) i).1 ky) ((hk y' (mem hc') i).1 ky')) + +/-- `HandlersActiveA` along `insert_with_cap` of a derived cap (IRQ states unchanged). -/ +theorem HandlersActiveA.ins_derived {G : List (List U64)} {s s' : S} (h : HandlersActiveA st G s) + {src dest : U64} (hsrc : src ∈ G.flatten) + (hk : ∀ y ∈ G.flatten, ∀ i, capA st s' y = .IrqHandler i → capA st s y = .IrqHandler i) + (hd : ∀ i, capA st s' dest = .IrqHandler i → capA st s src = .IrqHandler i) + (hks : L.ksb s' = L.ksb s) + (hst : ∀ i : U64, i.val < 5376 → L.mem s' (irqStAddrA (L.ksb s) i) = L.mem s (irqStAddrA (L.ksb s) i)) : + HandlersActiveA st (insG src dest G) s' := fun y hy i k => by + rcases (mem_insG_flat hsrc).1 hy with rfl | hy0 + · have ⟨h1, h2⟩ := h src hsrc i (hd i k) + exact ⟨h1, by rw [hks, hst i h1]; exact h2⟩ + · have ⟨h1, h2⟩ := h y hy0 i (hk y hy0 i k) + exact ⟨h1, by rw [hks, hst i h1]; exact h2⟩ + +/-- `HandlersActiveA` along `invoke_irq_control` (`set_irq_state(IRQ_SIGNAL, irq)` + `insert(IrqHandler irq)`). -/ +theorem HandlersActiveA.invoke_irq_control {G : List (List U64)} {s s' : S} (h : HandlersActiveA st G s) + {src dest irq : U64} (hsrc : src ∈ G.flatten) (hirq : irq.val < 5376) + (hk : ∀ y ∈ G.flatten, ∀ i, capA st s' y = .IrqHandler i → capA st s y = .IrqHandler i) + (hd : capA st s' dest = .IrqHandler irq) + (hks : L.ksb s' = L.ksb s) + (hst_irq : L.mem s' (irqStAddrA (L.ksb s) irq) ≠ 0#u64) + (hst_oth : ∀ j : U64, j.val < 5376 → j ≠ irq → + L.mem s' (irqStAddrA (L.ksb s) j) = L.mem s (irqStAddrA (L.ksb s) j)) : + HandlersActiveA st (insG src dest G) s' := fun y hy j k => by + rcases (mem_insG_flat hsrc).1 hy with rfl | hy0 + · rw [hd] at k; injection k with ej; subst ej + exact ⟨hirq, by rw [hks]; exact hst_irq⟩ + · have ⟨h1, h2⟩ := h y hy0 j (hk y hy0 j k) + by_cases e : j = irq + · subst e; exact ⟨hirq, by rw [hks]; exact hst_irq⟩ + · exact ⟨h1, by rw [hks, hst_oth j h1 e]; exact h2⟩ + +end + +end ST +end R +end W7 + +end + +end Fl4ProofsA64.P1 diff --git a/proofs/Fl4ProofsA64/P1/MdbLiveX.lean b/proofs/Fl4ProofsA64/P1/MdbLiveX.lean index 1230c6140..a766c06ad 100644 --- a/proofs/Fl4ProofsA64/P1/MdbLiveX.lean +++ b/proofs/Fl4ProofsA64/P1/MdbLiveX.lean @@ -316,7 +316,7 @@ theorem smp.remote_tcb_stall.dfS (hA : P0.ArchTotal ar) (s : S) (a : A) (t : obj rintro ⟨s', a'⟩ hr simp only [WP.uncurry'_pair] at hr ⊢ obtain ⟨⟨h1, -, ec, -, -, f, -, w⟩, -, l⟩ := hr - exact ⟨h1, ec, f, w.mono fun b ⟨o, ho, h88, e⟩ => Or.inr (Or.inl ⟨_, q.cur, o, ho, h88, e⟩), l⟩ + exact ⟨h1, ec, f, w.mono fun b ⟨o, ho, h88, _, e⟩ => Or.inr (Or.inl ⟨_, q.cur, o, ho, h88, e⟩), l⟩ /-- **The restart** `set_thread_state t TS_RESTART` of a runnable thread: `BlkOK`, a `K8` footprint with no SC, and the frame `tc t 3328`. -/ diff --git a/proofs/Fl4ProofsA64/P1/MdbPtCapA.lean b/proofs/Fl4ProofsA64/P1/MdbPtCapA.lean index b35c019d1..338cf10f3 100644 --- a/proofs/Fl4ProofsA64/P1/MdbPtCapA.lean +++ b/proofs/Fl4ProofsA64/P1/MdbPtCapA.lean @@ -162,7 +162,9 @@ structure CapSrcClosed (Q : capability.Cap → Prop) : Prop where irq : ∀ i, Q (.IrqHandler i) der : ∀ {d c : capability.Cap}, (AlikeA d c ∨ d = .Null) → FE d c → Q c → Q d sub : ∀ {p p' : U64} {sz : capability.FrameSize} {r r' : U64} {dv m e : Bool} {a v : U64} (k : Nat), - Q (.Frame p sz r dv m e a v) → p'.val = p.val + k * 4096 → Q (.Frame p' .Small r' dv false false 0#u64 0#u64) + Q (.Frame p sz r dv m e a v) → p'.val = p.val + k * 4096 → + (∀ bits, capability.FrameSize.bits sz = ok bits → k * 4096 + 4096 ≤ 2 ^ bits.val) → + Q (.Frame p' .Small r' dv false false 0#u64 0#u64) remap : ∀ {d c : capability.Cap}, CapMeta d c → Q c → Q d /-- **A cap every closed class holding of the `OS` caps at `s0` holds of** (the carry's cap predicate). -/ @@ -220,7 +222,7 @@ theorem PtCapP.closed (Pt : Set U64) : CapSrcClosed (PtCapP Pt) where ut := PtCapP.ut Pt irq := fun _ _ _ _ _ h => by cases h der := fun ha hf h => PtCapP.al ha hf h - sub := fun _ _ _ _ _ _ _ h => by cases h + sub := fun _ _ _ _ _ _ _ _ h => by cases h remap := by intro d c hm h p m a v e subst e diff --git a/proofs/Fl4ProofsA64/P1/MdbSemA.lean b/proofs/Fl4ProofsA64/P1/MdbSemA.lean new file mode 100644 index 000000000..2d1ed6940 --- /dev/null +++ b/proofs/Fl4ProofsA64/P1/MdbSemA.lean @@ -0,0 +1,350 @@ +/- +P1 (aarch64, A64-P1-MDB wave 1): **the meaning of FL4's MDB tests as pure functions of the slot contents** +(x86 counterpart: `Fl4Proofs.P3.MdbSem`; l4v source: `CSpace_AI.thy` `cdt_parent_defs`, `same_region_as`, +`is_cap_revocable`). + +* `revA` / `fbA`: the `MDB_REVOCABLE` and `MDB_FIRST_BADGED` bits of a slot's `NEXT` word (`slot_rev_eqA`, + `slot_fb_eqA`). +* `shapeA`: resets every cap field ignored by `objects.same_region_as` (incl. AArch64 `HighVSpace`); + `sraA_shape`, `sraA_congr`. +* `sraA_ut_of_ua`: explicitly verifies against extracted `objects.same_region_as` that + `same_region_as (Untyped ..) d` agrees with `same_region_as (Untyped ..) c` whenever `UA d c` holds + (needed for `MdbInvA.set_zombie`). +* `badgeA`, `isBadgedA`, `isZbA`, `revFA` (`objects.is_cap_revocable`). +* `isPFA` / `isPMA`: pure form of `cdt.is_mdb_parent_of` (`is_mdb_parent_of_eqA`). +* `srMA` / `srFA`: same-region and chunk relation (allowing non-revocable `Frame` caps, for `DeriveSubframe`). + +No named premise. New P1 module; zero edits to existing files. +-/ +import Fl4ProofsA64.P1.MdbTreeA +import Fl4Proofs.P3.MdbChunk +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P1 +open fl4_core_a64 Fl4ProofsA64.P0.Consts Fl4Proofs.P3 Classical + +section +variable {S : Type _} {st : machine.Store S} [L : LawfulStore st] + +local notation "wa" => core.num.U64.wrapping_add + +namespace W7 +namespace R +namespace ST + +noncomputable section + +/-! ## MDB flag bits -/ + +/-- `MDB_REVOCABLE` bit of slot `x`. -/ +def revA (st : machine.Store S) [L : LawfulStore st] (s : S) (x : U64) : Bool := + (L.mem s (wa x 24#u64) &&& 281474976710656#u64) != 0#u64 + +/-- `MDB_FIRST_BADGED` bit of slot `x`. -/ +def fbA (st : machine.Store S) [L : LawfulStore st] (s : S) (x : U64) : Bool := + (L.mem s (wa x 24#u64) &&& 562949953421312#u64) != 0#u64 + +theorem mdb_rev_eq : obj.MDB_REVOCABLE = ok 281474976710656#u64 := by + have h := P0.Consts.obj.MDB_REVOCABLE.spec + revert h; cases obj.MDB_REVOCABLE <;> simp [WP.spec_vis, WP.spec_div, WP.spec_ok] + +theorem mdb_fb_eq : obj.MDB_FIRST_BADGED = ok 562949953421312#u64 := by + have h := P0.Consts.obj.MDB_FIRST_BADGED.spec + revert h; cases obj.MDB_FIRST_BADGED <;> simp [WP.spec_vis, WP.spec_div, WP.spec_ok] + +theorem slot_rev_eqA (s : S) (x : U64) : obj.Slot.revocable st x s = ok (revA st s x) := by + unfold obj.Slot.revocable obj.Slot.next_word revA + simp only [cte_next, L.read_eq, mdb_rev_eq, lift, bind_ok] + +theorem slot_fb_eqA (s : S) (x : U64) : obj.Slot.first_badged st x s = ok (fbA st s x) := by + unfold obj.Slot.first_badged obj.Slot.next_word fbA + simp only [cte_next, L.read_eq, mdb_fb_eq, lift, bind_ok] + +theorem revA_congr {s s' : S} {x x' : U64} (h : L.mem s' (wa x' 24#u64) = L.mem s (wa x 24#u64)) : + revA st s' x' = revA st s x := by + unfold revA; rw [h] + +theorem fbA_congr {s s' : S} {x x' : U64} (h : L.mem s' (wa x' 24#u64) = L.mem s (wa x 24#u64)) : + fbA st s' x' = fbA st s x := by + unfold fbA; rw [h] + +theorem revA_of_eq {s : S} {x : U64} {b : Bool} (h : obj.Slot.revocable st x s = ok b) : + revA st s x = b := by + rw [slot_rev_eqA] at h; exact okinj h + +theorem fbA_of_eq {s : S} {x : U64} {b : Bool} (h : obj.Slot.first_badged st x s = ok b) : + fbA st s x = b := by + rw [slot_fb_eqA] at h; exact okinj h + +/-! ## Cap shapes and `same_region_as` -/ + +/-- A cap with the fields `same_region_as` ignores reset (x86 `shape`, plus AArch64 `HighVSpace`). -/ +def shapeA : capability.Cap → capability.Cap + | .Untyped p sb _ _ => .Untyped p sb 0#u64 false + | .Endpoint p _ _ _ _ _ => .Endpoint p 0#u64 false false false false + | .Notification p _ _ _ => .Notification p 0#u64 false false + | .Reply p _ => .Reply p false + | .CNode p r _ _ => .CNode p r 0#u64 0#u64 + | .SchedControl _ => .SchedControl 0#u64 + | .Frame p sz _ _ _ _ _ _ => .Frame p sz 0#u64 false false false 0#u64 0#u64 + | .PageTable p _ _ _ => .PageTable p false 0#u64 0#u64 + | .PageDirectory p _ _ _ => .PageDirectory p false 0#u64 0#u64 + | .Pdpt p _ _ _ => .Pdpt p false 0#u64 0#u64 + | .Pml4 p _ _ => .Pml4 p false 0#u64 + | .Ept p level _ _ _ => .Ept p level false 0#u64 0#u64 + | .IoPort f l _ => .IoPort f l 0#u64 + | .AsidPool p _ => .AsidPool p 0#u64 + | .HighVSpace p _ _ => .HighVSpace p false 0#u64 + | c => c + +theorem sr_shapeA (a b : capability.Cap) : + objects.same_region_as a b = objects.same_region_as (shapeA a) (shapeA b) := by + cases a <;> cases b <;> rfl + +theorem sraA_shape (a b : capability.Cap) : sraA a b = sraA (shapeA a) (shapeA b) := by + have h1 := sraA_eq a b + rw [sr_shapeA, sraA_eq] at h1 + exact (okinj h1).symm + +theorem sraA_of_eq {a b : capability.Cap} {v : Bool} (h : objects.same_region_as a b = ok v) : + sraA a b = v := by + rw [sraA_eq] at h; exact okinj h + +theorem sraA_congr {a a' b b' : capability.Cap} (ha : shapeA a = shapeA a') (hb : shapeA b = shapeA b') : + sraA a b = sraA a' b' := by + rw [sraA_shape, ha, hb, ← sraA_shape] + +/-- Explicit check (Architect note 1): `same_region_as (Untyped ..) d` agrees with `UA d c`. -/ +theorem sraA_ut_of_ua (p b i : U64) (dev : Bool) {d c : capability.Cap} (h : UA d c) : + sraA (.Untyped p b i dev) d = sraA (.Untyped p b i dev) c := + sra_ut_congr p b i i dev dev h.phys h.ptr h.size + +theorem sraA_zombie_left (p t n : U64) (b : capability.Cap) : sraA (.Zombie p t n) b = false := by + apply sraA_of_eq; cases b <;> rfl + +theorem sraA_null_left (b : capability.Cap) : sraA .Null b = false := by + apply sraA_of_eq; cases b <;> rfl + +theorem sraA_ep (p b : U64) (w r g gr : Bool) (x : capability.Cap) : + sraA (.Endpoint p b w r g gr) x = (match x with | .Endpoint q .. => decide (q = p) | _ => false) := by + apply sraA_of_eq + cases x <;> simp [objects.same_region_as] + split <;> simp_all + +theorem sraA_ntfn (p b : U64) (w r : Bool) (x : capability.Cap) : + sraA (.Notification p b w r) x = (match x with | .Notification q .. => decide (q = p) | _ => false) := by + apply sraA_of_eq + cases x <;> simp [objects.same_region_as] + split <;> simp_all + +theorem sraA_irqH_left (i : U64) (b : capability.Cap) : sraA (.IrqHandler i) b = decide (b = .IrqHandler i) := by + apply sraA_of_eq; cases b <;> simp [objects.same_region_as] + split_ifs <;> simp_all [eq_comm] + +theorem sraA_irqH_right (a : capability.Cap) (i : U64) : + sraA a (.IrqHandler i) = (decide (a = .IrqControl) || decide (a = .IrqHandler i)) := by + apply sraA_of_eq; cases a <;> simp [objects.same_region_as, objects.cap_is_physical] + split_ifs <;> simp_all [eq_comm] + +theorem sraA_irqC_right (a : capability.Cap) : sraA a .IrqControl = decide (a = .IrqControl) := by + apply sraA_of_eq; cases a <;> simp [objects.same_region_as, objects.cap_is_physical] + +/-! ## Badges, kinds and `is_cap_revocable` -/ + +/-- The badge of an endpoint or notification cap (else 0). -/ +def badgeA : capability.Cap → U64 + | .Endpoint _ b _ _ _ _ => b + | .Notification _ b _ _ => b + | _ => 0#u64 + +/-- Endpoint or notification (the badged kinds). -/ +def isBadgedA : capability.Cap → Bool + | .Endpoint .. => true + | .Notification .. => true + | _ => false + +/-- Zombie caps. -/ +def isZbA : capability.Cap → Bool + | .Zombie .. => true + | _ => false + +/-- Pure form of `objects.is_cap_revocable`. -/ +def revFA : capability.Cap → capability.Cap → Bool + | .Endpoint _ b .., .Endpoint _ b' .. => b != b' + | .Endpoint .., _ => true + | .Notification _ b .., .Notification _ b' .. => b != b' + | .Notification .., _ => true + | .IrqHandler _, .IrqControl => true + | .Untyped .., _ => true + | .IoPort .., .IoPortControl => true + | _, _ => false + +theorem revFA_eq (d s : capability.Cap) : objects.is_cap_revocable d s = ok (revFA d s) := by + cases d <;> cases s <;> rfl + +theorem isBadgedA_of_shape {c c' : capability.Cap} (h : shapeA c = shapeA c') : isBadgedA c = isBadgedA c' := by + cases c <;> cases c' <;> simp_all [shapeA, isBadgedA] + +theorem isUtA_of_shape {c c' : capability.Cap} (h : shapeA c = shapeA c') : isUtA c = isUtA c' := by + cases c <;> cases c' <;> simp_all [shapeA, isUtA] + +theorem isZbA_of_shape {c c' : capability.Cap} (h : shapeA c = shapeA c') : isZbA c = isZbA c' := by + cases c <;> cases c' <;> simp_all [shapeA, isZbA] + +theorem isFrA_of_shape {c c' : capability.Cap} (h : shapeA c = shapeA c') : isFrA c = isFrA c' := by + cases c <;> cases c' <;> simp_all [shapeA, isFrA] + +theorem isFrA_of_badged {c : capability.Cap} (h : isBadgedA c = true) : isFrA c = false := by + cases c <;> simp_all [isBadgedA, isFrA] + +theorem revFA_frame {c : capability.Cap} (h : isFrA c = true) (d : capability.Cap) : revFA c d = false := by + cases c <;> simp [isFrA] at h; cases d <;> rfl + +theorem revFA_badged {c s : capability.Cap} (hs : shapeA c = shapeA s) (hb : isBadgedA c = true) : + revFA c s = (badgeA c != badgeA s) := by + cases c <;> cases s <;> simp_all [shapeA, isBadgedA, revFA, badgeA] + +theorem revFA_ut {c s : capability.Cap} (h : isUtA c = true) : revFA c s = true := by + cases c <;> simp_all [isUtA, revFA] + +theorem revFA_cases {c s : capability.Cap} (hs : shapeA c = shapeA s) (h : revFA c s = true) : + (isBadgedA c = true ∧ badgeA c ≠ badgeA s) ∨ isUtA c = true := by + cases c <;> cases s <;> simp_all [shapeA, isBadgedA, revFA, badgeA, isUtA] + +theorem sraA_badged {a b : capability.Cap} (ha : isBadgedA a = true) (h : sraA a b = true) : + isBadgedA b = true ∧ shapeA b = shapeA a := by + cases a <;> simp [isBadgedA] at ha + · rw [sraA_ep] at h; cases b <;> simp at h; subst h; simp [isBadgedA, shapeA] + · rw [sraA_ntfn] at h; cases b <;> simp at h; subst h; simp [isBadgedA, shapeA] + +theorem sraA_badged_trans {a b c : capability.Cap} (ha : isBadgedA a = true) (hb : sraA a b = true) + (hc : sraA a c = true) : sraA b c = true := by + have h1 := (sraA_badged ha hb).2 + rw [sraA_congr h1 rfl]; exact hc + +/-! ## `is_mdb_parent_of` as a pure function -/ + +/-- `is_mdb_parent_of a b`, from `a`'s revocable bit and cap and `b`'s cap and first-badged bit. -/ +def isPFA (rev : Bool) (ca cb : capability.Cap) (fb : Bool) : Bool := + rev && sraA ca cb && (if isBadgedA ca then badgeA ca == 0#u64 || (badgeA cb == badgeA ca && !fb) else true) + +/-- FL4's parent test between two slots, from memory. -/ +def isPMA (st : machine.Store S) [L : LawfulStore st] (s : S) (x y : U64) : Bool := + isPFA (revA st s x) (capA st s x) (capA st s y) (fbA st s y) + +theorem is_mdb_parent_of_eqA (s : S) (a b : U64) : + cdt.is_mdb_parent_of st s a b = ok (isPMA st s a b) := by + unfold cdt.is_mdb_parent_of isPMA + rw [slot_rev_eqA] + simp only [bind_ok] + cases hr : revA st s a + · simp [isPFA] + simp only [↓reduceIte, capA_eq, bind_ok, sraA_eq] + cases hs : sraA (capA st s a) (capA st s b) + · simp [isPFA, hs] + simp only [↓reduceIte] + generalize capA st s a = ca at hs ⊢ + generalize capA st s b = cb at hs ⊢ + cases ca + case Endpoint p bd w r g gr => + rw [sraA_ep] at hs + cases cb <;> simp at hs + by_cases h0 : bd = 0#u64 + · simp [isPFA, isBadgedA, badgeA, h0, sraA_ep, hs] + · simp [isPFA, isBadgedA, badgeA, h0, sraA_ep, hs, slot_fb_eqA] + split <;> simp_all + case Notification p bd w r => + rw [sraA_ntfn] at hs + cases cb <;> simp at hs + by_cases h0 : bd = 0#u64 + · simp [isPFA, isBadgedA, badgeA, h0, sraA_ntfn, hs] + · simp [isPFA, isBadgedA, badgeA, h0, sraA_ntfn, hs, slot_fb_eqA] + split <;> simp_all + all_goals simp [isPFA, isBadgedA, hs] + +theorem isPFA_congr {r f : Bool} {a a' b b' : capability.Cap} (ha : shapeA a = shapeA a') (hba : badgeA a = badgeA a') + (hb : shapeA b = shapeA b') (hbb : badgeA b = badgeA b') : isPFA r a b f = isPFA r a' b' f := by + unfold isPFA; rw [sraA_congr ha hb, isBadgedA_of_shape ha, hba, hbb] + +theorem isPFA_fb_or {r : Bool} {a b : capability.Cap} {f g : Bool} (h : isPFA r a b f = false) : + isPFA r a b (f || g) = false := by + unfold isPFA at h ⊢; cases f <;> cases g <;> simp_all + +theorem isPFA_self {ca : capability.Cap} (hs : sraA ca ca = true) : isPFA true ca ca false = true := by + unfold isPFA; simp [hs] + +theorem isPFA_child {rv : Bool} {ca cs c : capability.Cap} {fs : Bool} (h : isPFA rv ca cs fs = true) + (hsh : shapeA c = shapeA cs) (hbd : badgeA c = badgeA cs ∨ badgeA cs = 0#u64) : + isPFA rv ca c (revFA c cs) = true := by + unfold isPFA at h ⊢ + simp only [Bool.and_eq_true] at h ⊢ + obtain ⟨⟨h1, h2⟩, h3⟩ := h + have h2' : sraA ca c = true := by rw [sraA_congr rfl hsh]; exact h2 + refine ⟨⟨h1, h2'⟩, ?_⟩ + by_cases hbca : isBadgedA ca = true + · simp only [hbca, ↓reduceIte] at h3 ⊢ + by_cases h0 : badgeA ca = 0#u64 + · simp [h0] + · have e0 : (badgeA ca == 0#u64) = false := by simpa using h0 + simp only [e0, Bool.false_or, Bool.and_eq_true, beq_iff_eq] at h3 ⊢ + obtain ⟨hb1, -⟩ := h3 + have hcs0 : badgeA cs ≠ 0#u64 := by rw [hb1]; exact h0 + have hbc : badgeA c = badgeA cs := hbd.resolve_right hcs0 + have hbad : isBadgedA c = true := by + rw [isBadgedA_of_shape hsh]; exact (sraA_badged hbca h2).1 + refine ⟨by rw [hbc, hb1], ?_⟩ + rw [revFA_badged hsh hbad, hbc]; simp + · simp [hbca] + +/-! ## Same-region and chunk relation -/ + +/-- Same region, between the caps of two slots. -/ +def srMA (st : machine.Store S) [L : LawfulStore st] (s : S) (x y : U64) : Bool := + sraA (capA st s x) (capA st s y) + +/-- The relation the chains are chunked by (x86 `srF`): same region, except that a non-revocable `Frame` cap +is unconstrained (`DeriveSubframe`). -/ +def srFA (st : machine.Store S) [L : LawfulStore st] (s : S) (x y : U64) : Bool := + srMA st s x y || (isFrA (capA st s x) && !revA st s x) + +theorem srFA_of_srMA {s : S} {x y : U64} (h : srMA st s x y = true) : srFA st s x y = true := by + simp [srFA, h] + +theorem srFA_nf {s : S} {x y : U64} (h : isFrA (capA st s x) = false) : srFA st s x y = srMA st s x y := by + simp [srFA, h] + +theorem srFA_rev {s : S} {x y : U64} (h : revA st s x = true) : srFA st s x y = srMA st s x y := by + simp [srFA, h] + +theorem srFA_congr {s s' : S} {u v u' v' : U64} (hs : srMA st s' u' v' = srMA st s u v) + (hf : isFrA (capA st s' u') = isFrA (capA st s u)) (hr : revA st s' u' = revA st s u) : + srFA st s' u' v' = srFA st s u v := by + simp only [srFA, hs, hf, hr] + +theorem ca_trueA {R : U64 → U64 → Bool} {a : U64} (h : ∀ v, R a v = true) : ∀ L, Mdb.CA R a L + | [] => trivial + | v :: L => ⟨fun h0 => (by rw [h v] at h0; cases h0), ca_trueA h L⟩ + +theorem chunkL_srFA {s : S} {L : List U64} (h : Mdb.ChunkL (srMA st s) L) : Mdb.ChunkL (srFA st s) L := by + induction L with + | nil => trivial + | cons a L ih => + refine ⟨?_, ih h.2⟩ + cases hf : (isFrA (capA st s a) && !revA st s a) + · exact Mdb.ca_congr h.1 (fun v _ => by simp [srFA, hf]) + · exact ca_trueA (fun v => by simp [srFA, hf]) L + +/-- The chunk condition at a slot that is not an unconstrained frame. -/ +theorem ca_srMA {s : S} {a : U64} {L : List U64} (h : Mdb.CA (srFA st s) a L) + (hf : isFrA (capA st s a) = false ∨ revA st s a = true) : Mdb.CA (srMA st s) a L := + Mdb.ca_congr h (fun v _ => by rcases hf with hf | hf <;> simp [srFA, hf]) + +end + +end ST +end R +end W7 + +end + +end Fl4ProofsA64.P1 diff --git a/proofs/Fl4ProofsA64/P1/QueueCallers.lean b/proofs/Fl4ProofsA64/P1/QueueCallers.lean index 5377bf27d..601f369cf 100644 --- a/proofs/Fl4ProofsA64/P1/QueueCallers.lean +++ b/proofs/Fl4ProofsA64/P1/QueueCallers.lean @@ -935,7 +935,7 @@ theorem sched.append.ready (s : S) (t : obj.Tcb) (Q : U64 → List U64) (hT : Tc attribute [local step] smp.remote_queue_update.spec -theorem append_foot_tail (s s1 : S) (t c p off : U64) (q : tcbqueue.Queue) (xs : List U64) +theorem append_foot_tail' (s s1 : S) (t c p off : U64) (q : tcbqueue.Queue) (xs : List U64) (hT : TcbPlaced T) (ht : t ∈ T) (hp : p.val < 256) (hN : NodePlaced st T s c) (hN' : NodePlaced st T s (L.cpu s)) (h1 : WritesIn st s s1 (L.NodeOf s c (136 ≤ ·))) -- a64: 128->136 (hr : QueueRel (L.mem s1) T .Sched q xs) (hn : t ∉ xs) @@ -946,6 +946,7 @@ theorem append_foot_tail (s s1 : S) (t c p off : U64) (q : tcbqueue.Queue) (xs : let s4 ← sched.set_flag st s3 t sched.Q_QUEUED true smp.remote_queue_update st s4 t) ⦃ s' => WritesIn st s s' (L.QFoot s (t :: xs) c (136 ≤ ·) t) ∧ -- a64: 128->136 + (L.mem s (affA t) = L.cpu s → WritesIn st s s' (L.QFootN s (t :: xs) c (136 ≤ ·) t)) ∧ L.mem s' (inqA t) = L.mem s (inqA t) ||| sched.Q_QUEUED ⦄ := by have hta : TcbAddr t := hT t ht have hW : ∀ w ∈ t :: xs, w ∈ T := by @@ -971,23 +972,45 @@ theorem append_foot_tail (s s1 : S) (t c p off : U64) (q : tcbqueue.Queue) (xs : simp only [ite_true] at s4_post have w34 : WritesIn st s3 s4 (· = inqA t) := by rw [s4_post]; exact writesIn_inq hta have w04 := w03.trans w34 - refine WP.spec_mono (smp.remote_queue_update.spec s4 t) ?_ - intro s5 h5 + refine WP.spec_mono (smp.remote_queue_update.spec2 s4 t) ?_ + rintro s5 ⟨h5, hl5⟩ have w45 := remote_writesIn hN' w04.1 w04.2.1 h5 - refine ⟨(w04.trans w45).mono fun b hb => ?_, ?_⟩ + refine ⟨(w04.trans w45).mono fun b hb => ?_, fun hloc => ?_, ?_⟩ · simp only [LawfulStore.QFoot]; tauto + · have w04' : WritesIn st s s4 (L.QFoot s (t :: xs) c (136 ≤ ·) t) := + w04.mono fun b hb => by simp only [LawfulStore.QFoot]; tauto + have h4 : L.mem s4 (affA t) = L.cpu s4 := by + rw [w04.2.1]; exact (aff_qfoot hT hW hN hN' ht w04' ht).trans hloc + rw [hl5 h4] + exact w04.mono fun b hb => by simp only [LawfulStore.QFootN]; tauto · have ni := inq_not_link_node (W := t :: xs) (Q := (fun o => o = 120 ∨ o = 128)) hT hW hN' ht -- a64: IPI_PENDING pair have ni' := inq_not_link_node (W := t :: xs) (Q := (136 ≤ ·)) hT hW hN ht -- a64: 128->136 rw [w45.mem_eq (inqA_aligned hta) ni.2, s4_post, L.mem_wr_same (inqA_aligned hta), w03.mem_eq (inqA_aligned hta) (by rintro (((h | h) | h)) <;> [exact ni'.2 h; exact ni'.1 h; exact ni'.2 h])] +theorem append_foot_tail (s s1 : S) (t c p off : U64) (q : tcbqueue.Queue) (xs : List U64) + (hT : TcbPlaced T) (ht : t ∈ T) (hp : p.val < 256) (hN : NodePlaced st T s c) + (hN' : NodePlaced st T s (L.cpu s)) (h1 : WritesIn st s s1 (L.NodeOf s c (136 ≤ ·))) -- a64: 128->136 + (hr : QueueRel (L.mem s1) T .Sched q xs) (hn : t ∉ xs) + (hoff : off = rqOff p) (hoff8 : core.num.U64.wrapping_add off 8#u64 = rqTailOff p) : + (do + let (q1, s2) ← tcbqueue.append st s1 tcbqueue.Link.Sched q t + let s3 ← sched.q_set st s2 c off q1 + let s4 ← sched.set_flag st s3 t sched.Q_QUEUED true + smp.remote_queue_update st s4 t) ⦃ s' => + WritesIn st s s' (L.QFoot s (t :: xs) c (136 ≤ ·) t) ∧ -- a64: 128->136 + L.mem s' (inqA t) = L.mem s (inqA t) ||| sched.Q_QUEUED ⦄ := + WP.spec_mono (append_foot_tail' s s1 t c p off q xs hT ht hp hN hN' h1 hr hn hoff hoff8) fun _ h => ⟨h.1, h.2.2⟩ + /-- The footprint of `append`, as for `enqueue`. -/ -theorem sched.append.foot (s : S) (t : obj.Tcb) (xs : List U64) (hT : TcbPlaced T) (ht : t ∈ T) +theorem sched.append.foot' (s : S) (t : obj.Tcb) (xs : List U64) (hT : TcbPlaced T) (ht : t ∈ T) (hp : (L.mem s (prioA t)).val < 256) (hN : NodePlaced st T s (L.mem s (affA t))) (hN' : NodePlaced st T s (L.cpu s)) (hr : QueueRel (L.mem s) T .Sched (L.rqQ s (L.mem s (affA t)) (L.mem s (prioA t))) xs) (hq : (L.mem s (inqA t) &&& sched.Q_QUEUED) = 0#u64 → t ∉ xs) : sched.append st s t ⦃ s' => WritesIn st s s' (L.QFoot s (t :: xs) (L.mem s (affA t)) (136 ≤ ·) t) ∧ -- a64: 128->136 + (L.mem s (affA t) = L.cpu s → + WritesIn st s s' (L.QFootN s (t :: xs) (L.mem s (affA t)) (136 ≤ ·) t)) ∧ L.mem s' (inqA t) = if (L.mem s (inqA t) &&& sched.Q_QUEUED) ≠ 0#u64 then L.mem s (inqA t) else L.mem s (inqA t) ||| sched.Q_QUEUED ⦄ := by unfold sched.append @@ -995,7 +1018,7 @@ theorem sched.append.foot (s : S) (t : obj.Tcb) (xs : List U64) (hT : TcbPlaced · have hq1 : (L.mem s (inqA t) &&& sched.Q_QUEUED) ≠ 0#u64 := by intro hc; have := ‹b = true›; rw [b_post] at this; exact of_decide_eq_true this hc simp only [ne_eq, hq1, not_false_eq_true, if_true] - exact ⟨WritesIn.refl _ _, by trivial⟩ + exact ⟨WritesIn.refl _ _, fun _ => WritesIn.refl _ _, by trivial⟩ have hq0 : (L.mem s (inqA t) &&& sched.Q_QUEUED) = 0#u64 := by by_contra hc; exact ‹¬b = true› (by rw [b_post]; exact decide_eq_true hc) simp only [hq0, ne_eq, not_true_eq_false, if_false] @@ -1019,11 +1042,32 @@ theorem sched.append.foot (s : S) (t : obj.Tcb) (xs : List U64) (hT : TcbPlaced rw [show L.nodeAt (L.wr s (L.nodeAt s (L.mem s (affA t)) l1Off) v1) (L.mem s (affA t)) (l2Off ((L.mem s (prioA t)).val / 64)) = L.nodeAt s (L.mem s (affA t)) (l2Off ((L.mem s (prioA t)).val / 64)) by simp] at hr1 - exact append_foot_tail s _ t _ _ _ q xs hT ht hp hN hN' w1 (hqq ▸ hr1) (hq hq0) rfl off_post1 + exact append_foot_tail' s _ t _ _ _ q xs hT ht hp hN hN' w1 (hqq ▸ hr1) (hq hq0) rfl off_post1 · rw [ite_cond_eq_false _ _ (eq_false hb1)] simp only [bind_ok] - exact append_foot_tail s s t _ _ _ q xs hT ht hp hN hN' (WritesIn.refl _ _) (hqq ▸ hr) (hq hq0) rfl off_post1 + exact append_foot_tail' s s t _ _ _ q xs hT ht hp hN hN' (WritesIn.refl _ _) (hqq ▸ hr) (hq hq0) rfl off_post1 + +theorem sched.append.foot (s : S) (t : obj.Tcb) (xs : List U64) (hT : TcbPlaced T) (ht : t ∈ T) + (hp : (L.mem s (prioA t)).val < 256) + (hN : NodePlaced st T s (L.mem s (affA t))) (hN' : NodePlaced st T s (L.cpu s)) + (hr : QueueRel (L.mem s) T .Sched (L.rqQ s (L.mem s (affA t)) (L.mem s (prioA t))) xs) + (hq : (L.mem s (inqA t) &&& sched.Q_QUEUED) = 0#u64 → t ∉ xs) : + sched.append st s t ⦃ s' => WritesIn st s s' (L.QFoot s (t :: xs) (L.mem s (affA t)) (136 ≤ ·) t) ∧ -- a64: 128->136 + L.mem s' (inqA t) = if (L.mem s (inqA t) &&& sched.Q_QUEUED) ≠ 0#u64 then L.mem s (inqA t) + else L.mem s (inqA t) ||| sched.Q_QUEUED ⦄ := + WP.spec_mono (sched.append.foot' s t xs hT ht hp hN hN' hr hq) fun _ h => ⟨h.1, h.2.2⟩ + +/-- (record edit B2) `append`'s footprint, with the current core's IPI pair only for a thread of another core. -/ +theorem sched.append.footS (s : S) (t : obj.Tcb) (xs : List U64) (hT : TcbPlaced T) (ht : t ∈ T) + (hp : (L.mem s (prioA t)).val < 256) + (hN : NodePlaced st T s (L.mem s (affA t))) (hN' : NodePlaced st T s (L.cpu s)) + (hr : QueueRel (L.mem s) T .Sched (L.rqQ s (L.mem s (affA t)) (L.mem s (prioA t))) xs) + (hq : (L.mem s (inqA t) &&& sched.Q_QUEUED) = 0#u64 → t ∉ xs) : + sched.append st s t ⦃ s' => WritesIn st s s' (L.QFootS s (t :: xs) (L.mem s (affA t)) (136 ≤ ·) t) ∧ + L.mem s' (inqA t) = if (L.mem s (inqA t) &&& sched.Q_QUEUED) ≠ 0#u64 then L.mem s (inqA t) + else L.mem s (inqA t) ||| sched.Q_QUEUED ⦄ := + WP.spec_mono (sched.append.foot' s t xs hT ht hp hN hN' hr hq) fun _ h => ⟨h.1.qfootS h.2.1, h.2.2⟩ /-- `tcbSchedAppend` preserves the invariant, as `enqueue` does. -/ theorem sched.append.inv {Cs : Set U64} (s : S) (t : obj.Tcb) (Q : U64 → U64 → List U64) (R : U64 → List U64) diff --git a/proofs/Fl4ProofsA64/P1/QueueDecCnode.lean b/proofs/Fl4ProofsA64/P1/QueueDecCnode.lean index 56db2d662..51f99b408 100644 --- a/proofs/Fl4ProofsA64/P1/QueueDecCnode.lean +++ b/proofs/Fl4ProofsA64/P1/QueueDecCnode.lean @@ -25,7 +25,7 @@ is in `QueueFinal`. -/ /-- The current node's words (but its debug head) are `Cur0W` words. -/ theorem NodeW.cur0w {s : S} {b : U64} (hc : L.cpu s ∈ Cs) (h : NodeW st s b) : Cur0W st T Cs Ω H Sg Scs Z s b := by - obtain ⟨o, ho, h88, rfl⟩ := h + obtain ⟨o, ho, h88, -, rfl⟩ := h exact Or.inl (kw_node hc ho h88) /-- The CDT placement facts (P3's: cap slots, `valid_caps`, `valid_mdb`). -/ diff --git a/proofs/Fl4ProofsA64/P1/QueueDecSc.lean b/proofs/Fl4ProofsA64/P1/QueueDecSc.lean index e853420a2..6d5bc3132 100644 --- a/proofs/Fl4ProofsA64/P1/QueueDecSc.lean +++ b/proofs/Fl4ProofsA64/P1/QueueDecSc.lean @@ -96,7 +96,7 @@ theorem schedcontext.bind_ntfn.ko (s : S) (c n : U64) (h : BlkOK st T Cs Ω H Sg /-- The current node's words (but its debug head) are `KO` words. -/ theorem NodeW.ko {s : S} {b : U64} (hc : L.cpu s ∈ Cs) (h : NodeW st s b) : KO st T Cs Ω H Sg Scs Z s b := by - obtain ⟨o, ho, h88, rfl⟩ := h + obtain ⟨o, ho, h88, -, rfl⟩ := h exact Or.inl (kw_node hc ho h88) /-- **`bind_ntfn`** of an SC and a notification bound to nothing. -/ @@ -284,7 +284,7 @@ theorem IpiW.nodeW {s s1 s' : S} (hN : NodePlaced st T s (L.cpu s)) (hk : L.ksb rcases h with rfl | ⟨o, v, ho, rfl⟩ · exact WritesIn.refl _ _ · rw [hc, nodeAt_of_ksb hk] - exact (writesIn_wr (nodeAt_aligned hN (by omega) (by omega))).mono fun b e => ⟨o, by omega, by omega, e⟩ + exact (writesIn_wr (nodeAt_aligned hN (by omega) (by omega))).mono fun b e => ⟨o, by omega, by omega, by omega, e⟩ /-- `remote_tcb_stall` from `BXI`. -/ theorem smp.remote_tcb_stall.bxi {A : Type _} {ar : machine.Arch A} (hA : P0.ArchTotal ar) (s : S) (a : A) diff --git a/proofs/Fl4ProofsA64/P1/QueueDecTcb2.lean b/proofs/Fl4ProofsA64/P1/QueueDecTcb2.lean index 57bcc5d35..70f4174ca 100644 --- a/proofs/Fl4ProofsA64/P1/QueueDecTcb2.lean +++ b/proofs/Fl4ProofsA64/P1/QueueDecTcb2.lean @@ -382,9 +382,9 @@ theorem Stalled.stall {A : Type _} {ar : machine.Arch A} (hA : P0.ArchTotal ar) refine ⟨hb2, ea.trans h1.act, ec.trans h1.cur, ecs.trans h1.csc, fun z hz K hK hK8 => (fld z hz K hK hK8).trans (h1.fld z hz K hK hK8), ?_⟩ refine (h1.node.trans wn).mono fun b hb => ?_ - rcases hb with hb | ⟨o, ho, h88, e⟩ + rcases hb with hb | ⟨o, ho, h88, h8, e⟩ · exact hb - · exact ⟨o, ho, h88, by rw [e, h1.node.2.1, nodeAt_of_ksb h1.node.1]⟩ + · exact ⟨o, ho, h88, h8, by rw [e, h1.node.2.1, nodeAt_of_ksb h1.node.1]⟩ set_option maxHeartbeats 2000000 in theorem tcb.copy_registers.blk {A : Type _} {ar : machine.Arch A} (hA : P0.ArchTotal ar) {s : S} {a : A} diff --git a/proofs/Fl4ProofsA64/P1/QueueDecVcpuStA.lean b/proofs/Fl4ProofsA64/P1/QueueDecVcpuStA.lean index 890ab063c..987bb0379 100644 --- a/proofs/Fl4ProofsA64/P1/QueueDecVcpuStA.lean +++ b/proofs/Fl4ProofsA64/P1/QueueDecVcpuStA.lean @@ -361,7 +361,7 @@ theorem vcpu.st_restart {s s2 : S} {vc : U64} {cap : capability.Cap} {extra : Ae have hz := hw 0#u64 (by simp) (by simp) have hO := hq.omega _ hz.2.1 have e2 : L.mem s2 (core.num.U64.wrapping_add vc 0#u64) = L.mem s (core.num.U64.wrapping_add vc 0#u64) := - h2.node.mem_eq hz.1 (fun ⟨o, ho, _, e⟩ => out_ne_nodeA hO hcs hN ho e) + h2.node.mem_eq hz.1 (fun ⟨o, ho, _, _, e⟩ => out_ne_nodeA hO hcs hN ho e) have hN2 : NodePlaced st T s2 (L.cpu s2) := by rw [h2.node.2.1]; exact hN.ksb h2.node.1 refine WP.spec_mono (WP.spec_and (Stalled.restartT D h2) (sched.set_thread_state.vckW2 s2 (L.curW s) sched.TS_RESTART hTP hN2 hct)) fun s3 ⟨c3, w3, _, _⟩ => ⟨c3, ?_⟩ diff --git a/proofs/Fl4ProofsA64/P1/QueueInv.lean b/proofs/Fl4ProofsA64/P1/QueueInv.lean index 2430fb69b..5f4bc4c4a 100644 --- a/proofs/Fl4ProofsA64/P1/QueueInv.lean +++ b/proofs/Fl4ProofsA64/P1/QueueInv.lean @@ -69,6 +69,46 @@ of core `c` (node offsets `P`) for thread `t`. -/ def LawfulStore.QFoot (s : S) (W : List U64) (c : U64) (P : Nat → Prop) (t b : U64) : Prop := LinkOf .Sched W b ∨ L.NodeOf s c P b ∨ b = inqA t ∨ L.NodeOf s (L.cpu s) (fun o => o = 120 ∨ o = 128) b -- a64: IPI_PENDING pair +/-- (record edit B2) `QFoot` without the current core's IPI pair: the footprint of a queue operation on a thread of +the current core, whose `remote_queue_update` writes nothing. -/ +def LawfulStore.QFootN (s : S) (W : List U64) (c : U64) (P : Nat → Prop) (t b : U64) : Prop := + LinkOf .Sched W b ∨ L.NodeOf s c P b ∨ b = inqA t + +/-- (record edit B2) `QFoot` with the current core's IPI pair only for a queue of another core. -/ +def LawfulStore.QFootS (s : S) (W : List U64) (c : U64) (P : Nat → Prop) (t b : U64) : Prop := + LinkOf .Sched W b ∨ L.NodeOf s c P b ∨ b = inqA t ∨ + (c ≠ L.cpu s ∧ L.NodeOf s (L.cpu s) (fun o => o = 120 ∨ o = 128) b) + +theorem LawfulStore.QFootN.toQFoot {s : S} {W : List U64} {c : U64} {P : Nat → Prop} {t b : U64} + (h : L.QFootN s W c P t b) : L.QFoot s W c P t b := by + unfold LawfulStore.QFootN at h; unfold LawfulStore.QFoot; tauto + +theorem LawfulStore.QFootN.toS {s : S} {W : List U64} {c : U64} {P : Nat → Prop} {t b : U64} + (h : L.QFootN s W c P t b) : L.QFootS s W c P t b := by + unfold LawfulStore.QFootN at h; unfold LawfulStore.QFootS; tauto + +theorem LawfulStore.QFootS.toQFoot {s : S} {W : List U64} {c : U64} {P : Nat → Prop} {t b : U64} + (h : L.QFootS s W c P t b) : L.QFoot s W c P t b := by + unfold LawfulStore.QFootS at h; unfold LawfulStore.QFoot; tauto + +/-- A footprint `QFoot`, and its IPI-free part when the queue is the current core's, is a footprint `QFootS`. -/ +theorem WritesIn.qfootS {s s' : S} {W : List U64} {c : U64} {P : Nat → Prop} {t : U64} + (h1 : WritesIn st s s' (L.QFoot s W c P t)) + (h2 : c = L.cpu s → WritesIn st s s' (L.QFootN s W c P t)) : WritesIn st s s' (L.QFootS s W c P t) := by + by_cases hc : c = L.cpu s + · exact (h2 hc).mono fun _ hb => hb.toS + · exact h1.mono fun b hb => by unfold LawfulStore.QFoot at hb; unfold LawfulStore.QFootS; tauto + +/-- (record edit B2) A `QFootS` footprint of the current core's queue has no IPI word. -/ +theorem WritesIn.qfootN_of {s s' : S} {W : List U64} {c : U64} {P : Nat → Prop} {t : U64} + (h : WritesIn st s s' (L.QFootS s W c P t)) (hc : c = L.cpu s) : WritesIn st s s' (L.QFootN s W c P t) := + h.mono fun _ hb => by + rcases hb with h | h | h | ⟨hne, -⟩ + · exact Or.inl h + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr h) + · exact absurd hc hne + /-! ## What lies outside a footprint -/ section foot @@ -166,6 +206,13 @@ theorem field_aligned {y K : U64} (hy : TcbAddr y) (hK : K.val < 4096) (hK8 : K. unfold Aligned simp [core.num.U64.wrapping_add_val_eq, U64.size_def, U64.numBits]; omega +/-- A thread's `AFFINITY` word is outside every queue footprint. -/ +theorem aff_not_qfoot {s : S} {W : List U64} {c t : U64} {P : Nat → Prop} (hT : TcbPlaced T) + (hW : ∀ w ∈ W, w ∈ T) (hN : NodePlaced st T s c) (hN' : NodePlaced st T s (L.cpu s)) (ht : t ∈ T) : + ¬ L.QFoot s W c P t (affA t) := by + rw [affA_eq] + exact qfoot_field hT hW hN hN' ht ht (by simp) (by simp) (by simp) (fun _ => by simp) + /-! ## What survives a footprint -/ section keep @@ -266,7 +313,7 @@ attribute [local step] smp.remote_queue_update.spec /-! ## The footprint of `enqueue` -/ -theorem enqueue_foot_tail (s s1 : S) (t c p off : U64) (q : tcbqueue.Queue) (xs : List U64) +theorem enqueue_foot_tail' (s s1 : S) (t c p off : U64) (q : tcbqueue.Queue) (xs : List U64) (hT : TcbPlaced T) (ht : t ∈ T) (hp : p.val < 256) (hN : NodePlaced st T s c) (hN' : NodePlaced st T s (L.cpu s)) (h1 : WritesIn st s s1 (L.NodeOf s c (136 ≤ ·))) -- a64: 128->136 (hr : QueueRel (L.mem s1) T .Sched q xs) (hn : t ∉ xs) @@ -277,6 +324,7 @@ theorem enqueue_foot_tail (s s1 : S) (t c p off : U64) (q : tcbqueue.Queue) (xs let s4 ← sched.set_flag st s3 t sched.Q_QUEUED true smp.remote_queue_update st s4 t) ⦃ s' => WritesIn st s s' (L.QFoot s (t :: xs) c (136 ≤ ·) t) ∧ -- a64: 128->136 + (L.mem s (affA t) = L.cpu s → WritesIn st s s' (L.QFootN s (t :: xs) c (136 ≤ ·) t)) ∧ L.mem s' (inqA t) = L.mem s (inqA t) ||| sched.Q_QUEUED ⦄ := by have hta : TcbAddr t := hT t ht have hW : ∀ w ∈ t :: xs, w ∈ T := by @@ -302,25 +350,47 @@ theorem enqueue_foot_tail (s s1 : S) (t c p off : U64) (q : tcbqueue.Queue) (xs simp only [ite_true] at s4_post have w34 : WritesIn st s3 s4 (· = inqA t) := by rw [s4_post]; exact writesIn_inq hta have w04 := w03.trans w34 - refine WP.spec_mono (smp.remote_queue_update.spec s4 t) ?_ - intro s5 h5 + refine WP.spec_mono (smp.remote_queue_update.spec2 s4 t) ?_ + rintro s5 ⟨h5, hl5⟩ have w45 := remote_writesIn hN' w04.1 w04.2.1 h5 - refine ⟨(w04.trans w45).mono fun b hb => ?_, ?_⟩ + refine ⟨(w04.trans w45).mono fun b hb => ?_, fun hloc => ?_, ?_⟩ · simp only [LawfulStore.QFoot]; tauto + · have w04' : WritesIn st s s4 (L.QFoot s (t :: xs) c (136 ≤ ·) t) := + w04.mono fun b hb => by simp only [LawfulStore.QFoot]; tauto + have h4 : L.mem s4 (affA t) = L.cpu s4 := by + rw [w04.2.1]; exact (aff_qfoot hT hW hN hN' ht w04' ht).trans hloc + rw [hl5 h4] + exact w04.mono fun b hb => by simp only [LawfulStore.QFootN]; tauto · have ni := inq_not_link_node (W := t :: xs) (Q := (fun o => o = 120 ∨ o = 128)) hT hW hN' ht -- a64: IPI_PENDING pair have ni' := inq_not_link_node (W := t :: xs) (Q := (136 ≤ ·)) hT hW hN ht -- a64: 128->136 rw [w45.mem_eq (inqA_aligned hta) ni.2, s4_post, L.mem_wr_same (inqA_aligned hta), w03.mem_eq (inqA_aligned hta) (by rintro (((h | h) | h)) <;> [exact ni'.2 h; exact ni'.1 h; exact ni'.2 h])] +theorem enqueue_foot_tail (s s1 : S) (t c p off : U64) (q : tcbqueue.Queue) (xs : List U64) + (hT : TcbPlaced T) (ht : t ∈ T) (hp : p.val < 256) (hN : NodePlaced st T s c) + (hN' : NodePlaced st T s (L.cpu s)) (h1 : WritesIn st s s1 (L.NodeOf s c (136 ≤ ·))) -- a64: 128->136 + (hr : QueueRel (L.mem s1) T .Sched q xs) (hn : t ∉ xs) + (hoff : off = rqOff p) (hoff8 : core.num.U64.wrapping_add off 8#u64 = rqTailOff p) : + (do + let (q1, s2) ← tcbqueue.prepend st s1 tcbqueue.Link.Sched q t + let s3 ← sched.q_set st s2 c off q1 + let s4 ← sched.set_flag st s3 t sched.Q_QUEUED true + smp.remote_queue_update st s4 t) ⦃ s' => + WritesIn st s s' (L.QFoot s (t :: xs) c (136 ≤ ·) t) ∧ -- a64: 128->136 + L.mem s' (inqA t) = L.mem s (inqA t) ||| sched.Q_QUEUED ⦄ := + WP.spec_mono (enqueue_foot_tail' s s1 t c p off q xs hT ht hp hN hN' h1 hr hn hoff hoff8) fun _ h => ⟨h.1, h.2.2⟩ + /-- The footprint of `enqueue`: the `Sched` links of `t` and of its queue, the bitmap and ready-queue words of its core, its `IN_QUEUES` word (which gets `Q_QUEUED`), and the current core's `IPI_PENDING`. -/ -theorem sched.enqueue.foot (s : S) (t : obj.Tcb) (xs : List U64) (hT : TcbPlaced T) (ht : t ∈ T) +theorem sched.enqueue.foot' (s : S) (t : obj.Tcb) (xs : List U64) (hT : TcbPlaced T) (ht : t ∈ T) (hp : (L.mem s (prioA t)).val < 256) (hN : NodePlaced st T s (L.mem s (affA t))) (hN' : NodePlaced st T s (L.cpu s)) (hr : QueueRel (L.mem s) T .Sched (L.rqQ s (L.mem s (affA t)) (L.mem s (prioA t))) xs) (hq : (L.mem s (inqA t) &&& sched.Q_QUEUED) = 0#u64 → t ∉ xs) : sched.enqueue st s t ⦃ s' => WritesIn st s s' (L.QFoot s (t :: xs) (L.mem s (affA t)) (136 ≤ ·) t) ∧ -- a64: 128->136 + (L.mem s (affA t) = L.cpu s → + WritesIn st s s' (L.QFootN s (t :: xs) (L.mem s (affA t)) (136 ≤ ·) t)) ∧ L.mem s' (inqA t) = if (L.mem s (inqA t) &&& sched.Q_QUEUED) ≠ 0#u64 then L.mem s (inqA t) else L.mem s (inqA t) ||| sched.Q_QUEUED ⦄ := by unfold sched.enqueue @@ -328,7 +398,7 @@ theorem sched.enqueue.foot (s : S) (t : obj.Tcb) (xs : List U64) (hT : TcbPlaced · have hq1 : (L.mem s (inqA t) &&& sched.Q_QUEUED) ≠ 0#u64 := by intro hc; have := ‹b = true›; rw [b_post] at this; exact of_decide_eq_true this hc simp only [ne_eq, hq1, not_false_eq_true, if_true] - exact ⟨WritesIn.refl _ _, by trivial⟩ + exact ⟨WritesIn.refl _ _, fun _ => WritesIn.refl _ _, by trivial⟩ have hq0 : (L.mem s (inqA t) &&& sched.Q_QUEUED) = 0#u64 := by by_contra hc; exact ‹¬b = true› (by rw [b_post]; exact decide_eq_true hc) simp only [hq0, ne_eq, not_true_eq_false, if_false] @@ -352,22 +422,43 @@ theorem sched.enqueue.foot (s : S) (t : obj.Tcb) (xs : List U64) (hT : TcbPlaced rw [show L.nodeAt (L.wr s (L.nodeAt s (L.mem s (affA t)) l1Off) v1) (L.mem s (affA t)) (l2Off ((L.mem s (prioA t)).val / 64)) = L.nodeAt s (L.mem s (affA t)) (l2Off ((L.mem s (prioA t)).val / 64)) by simp] at hr1 - exact enqueue_foot_tail s _ t _ _ _ q xs hT ht hp hN hN' w1 (hqq ▸ hr1) (hq hq0) rfl off_post1 + exact enqueue_foot_tail' s _ t _ _ _ q xs hT ht hp hN hN' w1 (hqq ▸ hr1) (hq hq0) rfl off_post1 · rw [ite_cond_eq_false _ _ (eq_false hb1)] simp only [bind_ok] - exact enqueue_foot_tail s s t _ _ _ q xs hT ht hp hN hN' (WritesIn.refl _ _) (hqq ▸ hr) (hq hq0) rfl off_post1 + exact enqueue_foot_tail' s s t _ _ _ q xs hT ht hp hN hN' (WritesIn.refl _ _) (hqq ▸ hr) (hq hq0) rfl off_post1 + +theorem sched.enqueue.foot (s : S) (t : obj.Tcb) (xs : List U64) (hT : TcbPlaced T) (ht : t ∈ T) + (hp : (L.mem s (prioA t)).val < 256) + (hN : NodePlaced st T s (L.mem s (affA t))) (hN' : NodePlaced st T s (L.cpu s)) + (hr : QueueRel (L.mem s) T .Sched (L.rqQ s (L.mem s (affA t)) (L.mem s (prioA t))) xs) + (hq : (L.mem s (inqA t) &&& sched.Q_QUEUED) = 0#u64 → t ∉ xs) : + sched.enqueue st s t ⦃ s' => WritesIn st s s' (L.QFoot s (t :: xs) (L.mem s (affA t)) (136 ≤ ·) t) ∧ -- a64: 128->136 + L.mem s' (inqA t) = if (L.mem s (inqA t) &&& sched.Q_QUEUED) ≠ 0#u64 then L.mem s (inqA t) + else L.mem s (inqA t) ||| sched.Q_QUEUED ⦄ := + WP.spec_mono (sched.enqueue.foot' s t xs hT ht hp hN hN' hr hq) fun _ h => ⟨h.1, h.2.2⟩ + +/-- (record edit B2) `enqueue`'s footprint, with the current core's IPI pair only for a thread of another core. -/ +theorem sched.enqueue.footS (s : S) (t : obj.Tcb) (xs : List U64) (hT : TcbPlaced T) (ht : t ∈ T) + (hp : (L.mem s (prioA t)).val < 256) + (hN : NodePlaced st T s (L.mem s (affA t))) (hN' : NodePlaced st T s (L.cpu s)) + (hr : QueueRel (L.mem s) T .Sched (L.rqQ s (L.mem s (affA t)) (L.mem s (prioA t))) xs) + (hq : (L.mem s (inqA t) &&& sched.Q_QUEUED) = 0#u64 → t ∉ xs) : + sched.enqueue st s t ⦃ s' => WritesIn st s s' (L.QFootS s (t :: xs) (L.mem s (affA t)) (136 ≤ ·) t) ∧ + L.mem s' (inqA t) = if (L.mem s (inqA t) &&& sched.Q_QUEUED) ≠ 0#u64 then L.mem s (inqA t) + else L.mem s (inqA t) ||| sched.Q_QUEUED ⦄ := + WP.spec_mono (sched.enqueue.foot' s t xs hT ht hp hN hN' hr hq) fun _ h => ⟨h.1.qfootS h.2.1, h.2.2⟩ /-! ## The footprint of `dequeue` -/ /-- The footprint of `dequeue`: the `Sched` links of its queue, the bitmap and ready-queue words of its core, and its `IN_QUEUES` word (which loses `Q_QUEUED`). -/ -theorem sched.dequeue.foot (s : S) (t : obj.Tcb) (xs : List U64) (hT : TcbPlaced T) (ht : t ∈ T) +theorem sched.dequeue.footN (s : S) (t : obj.Tcb) (xs : List U64) (hT : TcbPlaced T) (ht : t ∈ T) (hp : (L.mem s (prioA t)).val < 256) (hN : NodePlaced st T s (L.mem s (affA t))) (hN' : NodePlaced st T s (L.cpu s)) (hr : QueueRel (L.mem s) T .Sched (L.rqQ s (L.mem s (affA t)) (L.mem s (prioA t))) xs) (hq : (L.mem s (inqA t) &&& sched.Q_QUEUED) ≠ 0#u64 → t ∈ xs) : - sched.dequeue st s t ⦃ s' => WritesIn st s s' (L.QFoot s xs (L.mem s (affA t)) (136 ≤ ·) t) ∧ -- a64: 128->136 + sched.dequeue st s t ⦃ s' => WritesIn st s s' (L.QFootN s xs (L.mem s (affA t)) (136 ≤ ·) t) ∧ -- a64: 128->136 L.mem s' (inqA t) = if (L.mem s (inqA t) &&& sched.Q_QUEUED) ≠ 0#u64 then L.mem s (inqA t) &&& ~~~sched.Q_QUEUED else L.mem s (inqA t) ⦄ := by have hta : TcbAddr t := hT t ht @@ -402,14 +493,14 @@ theorem sched.dequeue.foot (s : S) (t : obj.Tcb) (xs : List U64) (hT : TcbPlaced rw [s3_post, L.mem_wr_same (inqA_aligned hta), w02.mem_eq (inqA_aligned hta) (by rintro (h | h) <;> [exact ni.1 h; exact ni.2 h])] have fin : ∀ s4, WritesIn st s3 s4 (L.NodeOf s (L.mem s (affA t)) (136 ≤ ·)) → -- a64: 128->136 - WritesIn st s s4 (L.QFoot s xs (L.mem s (affA t)) (136 ≤ ·) t) ∧ -- a64: 128->136 + WritesIn st s s4 (L.QFootN s xs (L.mem s (affA t)) (136 ≤ ·) t) ∧ -- a64: 128->136 L.mem s4 (inqA t) = L.mem s (inqA t) &&& ~~~sched.Q_QUEUED := by intro s4 w34 refine ⟨(w03.trans w34).mono fun b hb => ?_, ?_⟩ · rcases hb with ((h | h) | h) | h · exact Or.inl h · exact Or.inr (Or.inl h) - · exact Or.inr (Or.inr (Or.inl h)) + · exact Or.inr (Or.inr h) · exact Or.inr (Or.inl h) · rw [w34.mem_eq (inqA_aligned hta) ni.2, hinq3] step as ⟨b1, b1_post⟩ @@ -438,13 +529,34 @@ theorem sched.dequeue.foot (s : S) (t : obj.Tcb) (xs : List U64) (hT : TcbPlaced simp only [hq0, ne_eq, not_true_eq_false, ite_false] exact ⟨WritesIn.refl _ _, trivial⟩ +theorem sched.dequeue.foot (s : S) (t : obj.Tcb) (xs : List U64) (hT : TcbPlaced T) (ht : t ∈ T) + (hp : (L.mem s (prioA t)).val < 256) + (hN : NodePlaced st T s (L.mem s (affA t))) (hN' : NodePlaced st T s (L.cpu s)) + (hr : QueueRel (L.mem s) T .Sched (L.rqQ s (L.mem s (affA t)) (L.mem s (prioA t))) xs) + (hq : (L.mem s (inqA t) &&& sched.Q_QUEUED) ≠ 0#u64 → t ∈ xs) : + sched.dequeue st s t ⦃ s' => WritesIn st s s' (L.QFoot s xs (L.mem s (affA t)) (136 ≤ ·) t) ∧ -- a64: 128->136 + L.mem s' (inqA t) = if (L.mem s (inqA t) &&& sched.Q_QUEUED) ≠ 0#u64 + then L.mem s (inqA t) &&& ~~~sched.Q_QUEUED else L.mem s (inqA t) ⦄ := + WP.spec_mono (sched.dequeue.footN s t xs hT ht hp hN hN' hr hq) fun _ h => ⟨h.1.mono fun _ hb => hb.toQFoot, h.2⟩ + +/-- (record edit B2) `dequeue` writes no IPI word. -/ +theorem sched.dequeue.footS (s : S) (t : obj.Tcb) (xs : List U64) (hT : TcbPlaced T) (ht : t ∈ T) + (hp : (L.mem s (prioA t)).val < 256) + (hN : NodePlaced st T s (L.mem s (affA t))) (hN' : NodePlaced st T s (L.cpu s)) + (hr : QueueRel (L.mem s) T .Sched (L.rqQ s (L.mem s (affA t)) (L.mem s (prioA t))) xs) + (hq : (L.mem s (inqA t) &&& sched.Q_QUEUED) ≠ 0#u64 → t ∈ xs) : + sched.dequeue st s t ⦃ s' => WritesIn st s s' (L.QFootS s xs (L.mem s (affA t)) (136 ≤ ·) t) ∧ + L.mem s' (inqA t) = if (L.mem s (inqA t) &&& sched.Q_QUEUED) ≠ 0#u64 + then L.mem s (inqA t) &&& ~~~sched.Q_QUEUED else L.mem s (inqA t) ⦄ := + WP.spec_mono (sched.dequeue.footN s t xs hT ht hp hN hN' hr hq) fun _ h => ⟨h.1.mono fun _ hb => hb.toS, h.2⟩ + /-! ## The footprints of `release_enqueue` and `release_remove` -/ /-- The node offsets the release queue code writes: `REPROGRAM` and the release queue's head and tail. -/ def RelOffs (o : Nat) : Prop := o = 56 ∨ o = 64 ∨ o = 72 -theorem release_tail_foot (s s3 : S) (c x : U64) (t : obj.Tcb) (hT : TcbPlaced T) (ht : t ∈ T) +theorem release_tail_foot' (s s3 : S) (c x : U64) (t : obj.Tcb) (hT : TcbPlaced T) (ht : t ∈ T) (hN : NodePlaced st T s c) (hN' : NodePlaced st T s (L.cpu s)) (hk : L.ksb s3 = L.ksb s) (hc : L.cpu s3 = L.cpu s) : (do @@ -453,6 +565,7 @@ theorem release_tail_foot (s s3 : S) (c x : U64) (t : obj.Tcb) (hT : TcbPlaced T let s5 ← if (i != x) = true then sched.set_reprogram_on st s4 c else ok s4 smp.remote_queue_update st s5 t) ⦃ s' => WritesIn st s3 s' (fun b => b = inqA t ∨ L.NodeOf s c RelOffs b ∨ L.NodeOf s (L.cpu s) (fun o => o = 120 ∨ o = 128) b) ∧ -- a64: IPI_PENDING pair + (L.mem s3 (affA t) = L.cpu s3 → WritesIn st s3 s' (fun b => b = inqA t ∨ L.NodeOf s c RelOffs b)) ∧ L.mem s' (inqA t) = L.mem s3 (inqA t) ||| sched.Q_RELEASE ⦄ := by have hta : TcbAddr t := hT t ht step as ⟨s4, s4_post⟩ @@ -466,16 +579,26 @@ theorem release_tail_foot (s s3 : S) (c x : U64) (t : obj.Tcb) (hT : TcbPlaced T have fin : ∀ s5, WritesIn st s4 s5 (L.NodeOf s c RelOffs) → smp.remote_queue_update st s5 t ⦃ s' => WritesIn st s3 s' (fun b => b = inqA t ∨ L.NodeOf s c RelOffs b ∨ L.NodeOf s (L.cpu s) (fun o => o = 120 ∨ o = 128) b) ∧ -- a64: IPI_PENDING pair + (L.mem s3 (affA t) = L.cpu s3 → WritesIn st s3 s' (fun b => b = inqA t ∨ L.NodeOf s c RelOffs b)) ∧ L.mem s' (inqA t) = L.mem s3 (inqA t) ||| sched.Q_RELEASE ⦄ := by intro s5 w45 - refine WP.spec_mono (smp.remote_queue_update.spec s5 t) ?_ - intro s6 h6 + refine WP.spec_mono (smp.remote_queue_update.spec2 s5 t) ?_ + rintro s6 ⟨h6, hl6⟩ have w56 := remote_writesIn hN' (w45.1.trans hk4) (w45.2.1.trans hc4) h6 - refine ⟨((w34.trans w45).trans w56).mono fun b hb => ?_, ?_⟩ + refine ⟨((w34.trans w45).trans w56).mono fun b hb => ?_, fun hloc => ?_, ?_⟩ · rcases hb with (h | h) | h · exact Or.inl h · exact Or.inr (Or.inl h) · exact Or.inr (Or.inr h) + · have naff := aff_not_qfoot (W := []) (c := c) (P := RelOffs) hT (by simp) hN hN' ht + have al : Aligned (affA t) := by rw [affA_eq]; exact field_aligned hta (by simp) (by simp) + have a4 : L.mem s4 (affA t) = L.mem s3 (affA t) := + w34.mem_eq al (fun e => naff (by unfold LawfulStore.QFoot; exact Or.inr (Or.inr (Or.inl e)))) + have a5 : L.mem s5 (affA t) = L.mem s4 (affA t) := + w45.mem_eq al (fun e => naff (by unfold LawfulStore.QFoot; exact Or.inr (Or.inl e))) + have h5 : L.mem s5 (affA t) = L.cpu s5 := by rw [a5, a4, w45.2.1, w34.2.1]; exact hloc + rw [hl6 h5] + exact (w34.trans w45).mono fun b hb => hb · have n1 := inq_not_link_node (W := []) (Q := (fun o => o = 120 ∨ o = 128)) hT (by simp) hN' ht -- a64: IPI_PENDING pair have n2 := inq_not_link_node (W := []) (Q := RelOffs) hT (by simp) hN ht rw [w56.mem_eq (inqA_aligned hta) n1.2, w45.mem_eq (inqA_aligned hta) n2.2, hi4] @@ -486,8 +609,20 @@ theorem release_tail_foot (s s3 : S) (c x : U64) (t : obj.Tcb) (hT : TcbPlaced T exact writesIn_node (P := RelOffs) hN (by rw [repr_val]; simp [RelOffs]) (by rw [repr_val]; omega) (by rw [repr_val]) · simp only [bind_ok]; exact fin s4 (WritesIn.refl _ _) +theorem release_tail_foot (s s3 : S) (c x : U64) (t : obj.Tcb) (hT : TcbPlaced T) (ht : t ∈ T) + (hN : NodePlaced st T s c) (hN' : NodePlaced st T s (L.cpu s)) + (hk : L.ksb s3 = L.ksb s) (hc : L.cpu s3 = L.cpu s) : + (do + let s4 ← sched.set_flag st s3 t sched.Q_RELEASE true + let i ← obj.node_get st s4 c layout.node.RELEASE_HEAD + let s5 ← if (i != x) = true then sched.set_reprogram_on st s4 c else ok s4 + smp.remote_queue_update st s5 t) ⦃ s' => + WritesIn st s3 s' (fun b => b = inqA t ∨ L.NodeOf s c RelOffs b ∨ L.NodeOf s (L.cpu s) (fun o => o = 120 ∨ o = 128) b) ∧ -- a64: IPI_PENDING pair + L.mem s' (inqA t) = L.mem s3 (inqA t) ||| sched.Q_RELEASE ⦄ := + WP.spec_mono (release_tail_foot' s s3 c x t hT ht hN hN' hk hc) fun _ h => ⟨h.1, h.2.2⟩ + /-- After the list operation: store the new queue, then the tail. -/ -theorem release_qset_foot (s s1 : S) (c : U64) (nq : tcbqueue.Queue) (x : U64) (t : obj.Tcb) (xs : List U64) +theorem release_qset_foot' (s s1 : S) (c : U64) (nq : tcbqueue.Queue) (x : U64) (t : obj.Tcb) (xs : List U64) (hT : TcbPlaced T) (ht : t ∈ T) (hN : NodePlaced st T s c) (hN' : NodePlaced st T s (L.cpu s)) (h1 : WritesIn st s s1 (LinkOf .Sched (t :: xs))) (hW : ∀ w ∈ t :: xs, w ∈ T) : (do @@ -497,6 +632,7 @@ theorem release_qset_foot (s s1 : S) (c : U64) (nq : tcbqueue.Queue) (x : U64) ( let s4 ← if (i != x) = true then sched.set_reprogram_on st s3 c else ok s3 smp.remote_queue_update st s4 t) ⦃ s' => WritesIn st s s' (L.QFoot s (t :: xs) c RelOffs t) ∧ + (L.mem s (affA t) = L.cpu s → WritesIn st s s' (L.QFootN s (t :: xs) c RelOffs t)) ∧ L.mem s' (inqA t) = L.mem s (inqA t) ||| sched.Q_RELEASE ⦄ := by have hta : TcbAddr t := hT t ht step as ⟨s2, s2_post⟩ @@ -506,26 +642,51 @@ theorem release_qset_foot (s s1 : S) (c : U64) (nq : tcbqueue.Queue) (x : U64) ( (by rw [relh_val])).trans (writesIn_node (P := RelOffs) hN (by rw [relt_val]; simp [RelOffs]) (by rw [relt_val]; omega) (by rw [relt_val]))).mono fun b hb => by rcases hb with hb | hb <;> exact hb have w02 := h1.trans w12 - refine WP.spec_mono (release_tail_foot s s2 c x t hT ht hN hN' w02.1 w02.2.1) ?_ - rintro s' ⟨w2', hi⟩ + refine WP.spec_mono (release_tail_foot' s s2 c x t hT ht hN hN' w02.1 w02.2.1) ?_ + rintro s' ⟨w2', hl2, hi⟩ have ni := inq_not_link_node (W := t :: xs) (Q := RelOffs) hT hW hN ht - refine ⟨(w02.trans w2').mono fun b hb => ?_, ?_⟩ + refine ⟨(w02.trans w2').mono fun b hb => ?_, fun hloc => ?_, ?_⟩ · rcases hb with (h | h) | h | h | h · exact Or.inl h · exact Or.inr (Or.inl h) · exact Or.inr (Or.inr (Or.inl h)) · exact Or.inr (Or.inl h) · exact Or.inr (Or.inr (Or.inr h)) + · have naff := aff_not_qfoot (W := t :: xs) (c := c) (P := RelOffs) hT hW hN hN' ht + have al : Aligned (affA t) := by rw [affA_eq]; exact field_aligned hta (by simp) (by simp) + have a2 : L.mem s2 (affA t) = L.mem s (affA t) := + w02.mem_eq al (fun e => e.elim (fun e => naff (Or.inl e)) (fun e => naff (Or.inr (Or.inl e)))) + have h2 : L.mem s2 (affA t) = L.cpu s2 := by rw [a2, w02.2.1]; exact hloc + exact (w02.trans (hl2 h2)).mono fun b hb => by + rcases hb with (h | h) | h | h + · exact Or.inl h + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr h) + · exact Or.inr (Or.inl h) · rw [hi, w02.mem_eq (inqA_aligned hta) (by rintro (h | h) <;> [exact ni.1 h; exact ni.2 h])] +theorem release_qset_foot (s s1 : S) (c : U64) (nq : tcbqueue.Queue) (x : U64) (t : obj.Tcb) (xs : List U64) + (hT : TcbPlaced T) (ht : t ∈ T) (hN : NodePlaced st T s c) (hN' : NodePlaced st T s (L.cpu s)) + (h1 : WritesIn st s s1 (LinkOf .Sched (t :: xs))) (hW : ∀ w ∈ t :: xs, w ∈ T) : + (do + let s2 ← sched.q_set st s1 c layout.node.RELEASE_HEAD nq + let s3 ← sched.set_flag st s2 t sched.Q_RELEASE true + let i ← obj.node_get st s3 c layout.node.RELEASE_HEAD + let s4 ← if (i != x) = true then sched.set_reprogram_on st s3 c else ok s3 + smp.remote_queue_update st s4 t) ⦃ s' => + WritesIn st s s' (L.QFoot s (t :: xs) c RelOffs t) ∧ + L.mem s' (inqA t) = L.mem s (inqA t) ||| sched.Q_RELEASE ⦄ := + WP.spec_mono (release_qset_foot' s s1 c nq x t xs hT ht hN hN' h1 hW) fun _ h => ⟨h.1, h.2.2⟩ + /-- The footprint of `release_enqueue`: the `Sched` links of `t` and of the release queue, `REPROGRAM` and the release queue words of its core, its `IN_QUEUES` word (which gets `Q_RELEASE`), and the current core's `IPI_PENDING`. -/ -theorem sched.release_enqueue.foot (s : S) (t : obj.Tcb) (xs : List U64) (hT : TcbPlaced T) (ht : t ∈ T) +theorem sched.release_enqueue.foot' (s : S) (t : obj.Tcb) (xs : List U64) (hT : TcbPlaced T) (ht : t ∈ T) (hn : t ∉ xs) (hN : NodePlaced st T s (L.mem s (affA t))) (hN' : NodePlaced st T s (L.cpu s)) (h : QueueRel (L.mem s) T .Sched (L.relQ s (L.mem s (affA t))) xs) : sched.release_enqueue st s t ⦃ s' => WritesIn st s s' (L.QFoot s (t :: xs) (L.mem s (affA t)) RelOffs t) ∧ + (L.mem s (affA t) = L.cpu s → WritesIn st s s' (L.QFootN s (t :: xs) (L.mem s (affA t)) RelOffs t)) ∧ L.mem s' (inqA t) = L.mem s (inqA t) ||| sched.Q_RELEASE ⦄ := by have hta : TcbAddr t := hT t ht have hW : ∀ w ∈ t :: xs, w ∈ T := by @@ -540,15 +701,15 @@ theorem sched.release_enqueue.foot (s : S) (t : obj.Tcb) (xs : List U64) (hT : T · refine WP.spec_bind (tcbqueue.prepend.rel s .Sched q t xs hT h0 ht hn) ?_ rintro ⟨nq, s1⟩ h1 simp only [WP.uncurry'_pair] at h1 - exact release_qset_foot s s1 _ nq q.head t xs hT ht hN hN' h1.2.writesIn hW + exact release_qset_foot' s s1 _ nq q.head t xs hT ht hN hN' h1.2.writesIn hW · refine WP.spec_bind (tcbqueue.prepend.rel s .Sched q t xs hT h0 ht hn) ?_ rintro ⟨nq, s1⟩ h1 simp only [WP.uncurry'_pair] at h1 - exact release_qset_foot s s1 _ nq q.head t xs hT ht hN hN' h1.2.writesIn hW + exact release_qset_foot' s s1 _ nq q.head t xs hT ht hN hN' h1.2.writesIn hW · refine WP.spec_bind (tcbqueue.append.rel s .Sched q t xs hT h0 ht hn) ?_ rintro ⟨nq, s1⟩ h1 simp only [WP.uncurry'_pair] at h1 - exact release_qset_foot s s1 _ nq q.head t xs hT ht hN hN' h1.2.writesIn hW + exact release_qset_foot' s s1 _ nq q.head t xs hT ht hN hN' h1.2.writesIn hW · have hne : xs ≠ [] := by rw [Ne, ← h0.empty_iff hT]; simpa [b_post] using ‹¬b = true› obtain ⟨a, post, hxa⟩ := List.exists_cons_of_ne_nil hne @@ -573,25 +734,49 @@ theorem sched.release_enqueue.foot (s : S) (t : obj.Tcb) (xs : List U64) (hT : T rintro s2 ⟨-, hf2⟩ rw [← hx'] at hf2 have w02 := hf2.writesIn - refine WP.spec_mono (release_tail_foot s s2 _ q.head t hT ht hN hN' w02.1 w02.2.1) ?_ - rintro s' ⟨w2', hi⟩ + refine WP.spec_mono (release_tail_foot' s s2 _ q.head t hT ht hN hN' w02.1 w02.2.1) ?_ + rintro s' ⟨w2', hl2, hi⟩ have ni := inq_not_link_node (W := t :: xs) (Q := RelOffs) hT hW hN ht - refine ⟨(w02.trans w2').mono fun b hb => ?_, ?_⟩ + refine ⟨(w02.trans w2').mono fun b hb => ?_, fun hloc => ?_, ?_⟩ · rcases hb with h | h | h | h · exact Or.inl h · exact Or.inr (Or.inr (Or.inl h)) · exact Or.inr (Or.inl h) · exact Or.inr (Or.inr (Or.inr h)) + · have naff := aff_not_qfoot (W := t :: xs) (c := L.mem s (affA t)) (P := RelOffs) hT hW hN hN' ht + have al : Aligned (affA t) := by rw [affA_eq]; exact field_aligned hta (by simp) (by simp) + have a2 : L.mem s2 (affA t) = L.mem s (affA t) := w02.mem_eq al (fun e => naff (Or.inl e)) + have h2 : L.mem s2 (affA t) = L.cpu s2 := by rw [a2, w02.2.1]; exact hloc + exact (w02.trans (hl2 h2)).mono fun b hb => by + rcases hb with h | h | h + · exact Or.inl h + · exact Or.inr (Or.inr h) + · exact Or.inr (Or.inl h) · rw [hi, w02.mem_eq (inqA_aligned hta) ni.1] +theorem sched.release_enqueue.foot (s : S) (t : obj.Tcb) (xs : List U64) (hT : TcbPlaced T) (ht : t ∈ T) + (hn : t ∉ xs) (hN : NodePlaced st T s (L.mem s (affA t))) (hN' : NodePlaced st T s (L.cpu s)) + (h : QueueRel (L.mem s) T .Sched (L.relQ s (L.mem s (affA t))) xs) : + sched.release_enqueue st s t ⦃ s' => WritesIn st s s' (L.QFoot s (t :: xs) (L.mem s (affA t)) RelOffs t) ∧ + L.mem s' (inqA t) = L.mem s (inqA t) ||| sched.Q_RELEASE ⦄ := + WP.spec_mono (sched.release_enqueue.foot' s t xs hT ht hn hN hN' h) fun _ h => ⟨h.1, h.2.2⟩ + +/-- (record edit B2) `release_enqueue`'s footprint, with the IPI pair only for a thread of another core. -/ +theorem sched.release_enqueue.footS (s : S) (t : obj.Tcb) (xs : List U64) (hT : TcbPlaced T) (ht : t ∈ T) + (hn : t ∉ xs) (hN : NodePlaced st T s (L.mem s (affA t))) (hN' : NodePlaced st T s (L.cpu s)) + (h : QueueRel (L.mem s) T .Sched (L.relQ s (L.mem s (affA t))) xs) : + sched.release_enqueue st s t ⦃ s' => WritesIn st s s' (L.QFootS s (t :: xs) (L.mem s (affA t)) RelOffs t) ∧ + L.mem s' (inqA t) = L.mem s (inqA t) ||| sched.Q_RELEASE ⦄ := + WP.spec_mono (sched.release_enqueue.foot' s t xs hT ht hn hN hN' h) fun _ h => ⟨h.1.qfootS h.2.1, h.2.2⟩ + /-- The footprint of `release_remove`: the `Sched` links of the release queue, `REPROGRAM` and the release queue words of its core, and its `IN_QUEUES` word (which loses `Q_RELEASE`). -/ -theorem sched.release_remove.foot (s : S) (t : obj.Tcb) (xs : List U64) (hT : TcbPlaced T) (ht : t ∈ T) +theorem sched.release_remove.footN (s : S) (t : obj.Tcb) (xs : List U64) (hT : TcbPlaced T) (ht : t ∈ T) (hN : NodePlaced st T s (L.mem s (affA t))) (hN' : NodePlaced st T s (L.cpu s)) (h : QueueRel (L.mem s) T .Sched (L.relQ s (L.mem s (affA t))) xs) (hq : (L.mem s (inqA t) &&& sched.Q_RELEASE) ≠ 0#u64 → t ∈ xs) : - sched.release_remove st s t ⦃ s' => WritesIn st s s' (L.QFoot s xs (L.mem s (affA t)) RelOffs t) ∧ + sched.release_remove st s t ⦃ s' => WritesIn st s s' (L.QFootN s xs (L.mem s (affA t)) RelOffs t) ∧ L.mem s' (inqA t) = if (L.mem s (inqA t) &&& sched.Q_RELEASE) ≠ 0#u64 then L.mem s (inqA t) &&& ~~~sched.Q_RELEASE else L.mem s (inqA t) ⦄ := by have hta : TcbAddr t := hT t ht @@ -611,7 +796,7 @@ theorem sched.release_remove.foot (s : S) (t : obj.Tcb) (xs : List U64) (hT : Tc let (q1, s2) ← tcbqueue.remove st s1 tcbqueue.Link.Sched q t let s3 ← sched.q_set st s2 (L.mem s (affA t)) layout.node.RELEASE_HEAD q1 sched.set_flag st s3 t sched.Q_RELEASE false) ⦃ s' => - WritesIn st s s' (L.QFoot s xs (L.mem s (affA t)) RelOffs t) ∧ + WritesIn st s s' (L.QFootN s xs (L.mem s (affA t)) RelOffs t) ∧ L.mem s' (inqA t) = L.mem s (inqA t) &&& ~~~sched.Q_RELEASE ⦄ := by intro s1 w01 h1 step as ⟨q, q_post⟩ @@ -636,7 +821,7 @@ theorem sched.release_remove.foot (s : S) (t : obj.Tcb) (xs : List U64) (hT : Tc · exact Or.inr (Or.inl h) · exact Or.inl h · exact Or.inr (Or.inl h) - · exact Or.inr (Or.inr (Or.inl h)) + · exact Or.inr (Or.inr h) · rw [s4_post, L.mem_wr_same (inqA_aligned hta), w03.mem_eq (inqA_aligned hta) (by rintro ((h | h) | h) <;> [exact ni.2 h; exact ni.1 h; exact ni.2 h])] split @@ -653,6 +838,25 @@ theorem sched.release_remove.foot (s : S) (t : obj.Tcb) (xs : List U64) (hT : Tc simp only [hq0, ne_eq, not_true_eq_false, ite_false] exact ⟨WritesIn.refl _ _, trivial⟩ +theorem sched.release_remove.foot (s : S) (t : obj.Tcb) (xs : List U64) (hT : TcbPlaced T) (ht : t ∈ T) + (hN : NodePlaced st T s (L.mem s (affA t))) (hN' : NodePlaced st T s (L.cpu s)) + (h : QueueRel (L.mem s) T .Sched (L.relQ s (L.mem s (affA t))) xs) + (hq : (L.mem s (inqA t) &&& sched.Q_RELEASE) ≠ 0#u64 → t ∈ xs) : + sched.release_remove st s t ⦃ s' => WritesIn st s s' (L.QFoot s xs (L.mem s (affA t)) RelOffs t) ∧ + L.mem s' (inqA t) = if (L.mem s (inqA t) &&& sched.Q_RELEASE) ≠ 0#u64 + then L.mem s (inqA t) &&& ~~~sched.Q_RELEASE else L.mem s (inqA t) ⦄ := + WP.spec_mono (sched.release_remove.footN s t xs hT ht hN hN' h hq) fun _ h => ⟨h.1.mono fun _ hb => hb.toQFoot, h.2⟩ + +/-- (record edit B2) `release_remove` writes no IPI word. -/ +theorem sched.release_remove.footS (s : S) (t : obj.Tcb) (xs : List U64) (hT : TcbPlaced T) (ht : t ∈ T) + (hN : NodePlaced st T s (L.mem s (affA t))) (hN' : NodePlaced st T s (L.cpu s)) + (h : QueueRel (L.mem s) T .Sched (L.relQ s (L.mem s (affA t))) xs) + (hq : (L.mem s (inqA t) &&& sched.Q_RELEASE) ≠ 0#u64 → t ∈ xs) : + sched.release_remove st s t ⦃ s' => WritesIn st s s' (L.QFootS s xs (L.mem s (affA t)) RelOffs t) ∧ + L.mem s' (inqA t) = if (L.mem s (inqA t) &&& sched.Q_RELEASE) ≠ 0#u64 + then L.mem s (inqA t) &&& ~~~sched.Q_RELEASE else L.mem s (inqA t) ⦄ := + WP.spec_mono (sched.release_remove.footN s t xs hT ht hN hN' h hq) fun _ h => ⟨h.1.mono fun _ hb => hb.toS, h.2⟩ + end end Fl4ProofsA64.P1 diff --git a/proofs/Fl4ProofsA64/P1/QueueInvAll.lean b/proofs/Fl4ProofsA64/P1/QueueInvAll.lean index 640b63d79..2e28fcd30 100644 --- a/proofs/Fl4ProofsA64/P1/QueueInvAll.lean +++ b/proofs/Fl4ProofsA64/P1/QueueInvAll.lean @@ -194,9 +194,10 @@ def CapOK (st : machine.Store S) [L : LawfulStore st] (T Rp Scs Nt : Set U64) (H CapTyped H Rp Nt c ∧ (∀ p, c = .Thread p → p ∈ T) ∧ (∀ p b, c = .SchedContext p b → p ∈ Scs ∧ p ≠ 0#u64 ∧ L.mem s (scTcbA p) ≠ L.idleW s) -/-- The words of the current core's node (where `remote_tcb_stall` writes). -/ +/-- The words of the current core's node (where `remote_tcb_stall` writes). (A64-P3 record edit #6, approved merged- +statement edit: `IDLE_THREAD` (offset 8, never written after boot) excluded; the stall writes offsets 120 / 128 only.) -/ def NodeW (st : machine.Store S) [L : LawfulStore st] (s : S) (b : U64) : Prop := - ∃ o : U64, o.val < 4288 ∧ o.val ≠ 88 ∧ b = L.nodeAt s (L.cpu s) o + ∃ o : U64, o.val < 4288 ∧ o.val ≠ 88 ∧ o.val ≠ 8 ∧ b = L.nodeAt s (L.cpu s) o /-- What `decode_invocation` may assume: the invariants, the current SC is active, the invoked and the extra caps are typed (the extra caps also after writes diff --git a/proofs/Fl4ProofsA64/P1/QueueS2Ty.lean b/proofs/Fl4ProofsA64/P1/QueueS2Ty.lean index f60cf2548..54f0a123e 100644 --- a/proofs/Fl4ProofsA64/P1/QueueS2Ty.lean +++ b/proofs/Fl4ProofsA64/P1/QueueS2Ty.lean @@ -54,9 +54,11 @@ remaining depth)? Pool entries: `assign` writes tag 0 / 1 / 2 (vspace.rs:1239– table descriptor only into an invalid slot found by `lookup_slot` with `shift ≠ 12` (so `rem > 0`), naming an unmapped `PageTable` (12-bit) object, one level down; `unmap_table` clears it; leaf descriptors with `e & 3 = 3` sit at `rem = 0` (shift 12), where `desc` asks nothing; block descriptors have `e & 3 = 1`. `n` is constant by the [P]. -A case hypothesis of the deletion's / decode's start state. One new named premise: [P] `ArchPlat.s2_ipa_bits_const`. +A case hypothesis of the deletion's / decode's start state. One new named premise: [P] `ArchPlat.s2_ipa_bits_const` +(`ArchPlat.pauth` is a later field). -/ import Fl4ProofsA64.P1.QueueHiTy +import Fl4SpecA64.Ext.Platform open Aeneas Aeneas.Std Result namespace Fl4ProofsA64.P1 @@ -69,6 +71,9 @@ structure ArchPlat {A : Type _} (ar : machine.Arch A) : Prop where /-- **[P]** `Arch::s2_ipa_bits` is one constant at every arch state (fl4-arch-aarch64 arch.rs:195, mmu.rs:395–428: computed once on the boot core from `pa_bits` and cached). -/ s2_ipa_bits_const : ∃ n : U64, ∀ a, ar.s2_ipa_bits a = ok n + /-- **[P]** the hardware pointer-authentication probe (`Arch::has_pauth`) agrees with the spec's platform + constant `fl4A64Platform.hasPauth`. -/ + pauth : ∀ a, ar.has_pauth a = ok Fl4SpecA64.fl4A64Platform.hasPauth /-- The Stage-2 IPA-width constant of the platform. -/ noncomputable def ArchPlat.ipa {A : Type _} {ar : machine.Arch A} (h : ArchPlat ar) : U64 := diff --git a/proofs/Fl4ProofsA64/P1/QueueSched.lean b/proofs/Fl4ProofsA64/P1/QueueSched.lean index 1acddcbe2..f9275ca47 100644 --- a/proofs/Fl4ProofsA64/P1/QueueSched.lean +++ b/proofs/Fl4ProofsA64/P1/QueueSched.lean @@ -60,6 +60,23 @@ theorem smp.remote_queue_update.spec (s : S) (t : obj.Tcb) : | (left; simp_all; done) | exact Or.inr ‹_› +/-- (record edit B2) A thread of the current core: `remote_queue_update` leaves the store unchanged. -/ +theorem smp.remote_queue_update.local (s : S) (t : obj.Tcb) (h : L.mem s (affA t) = L.cpu s) : + smp.remote_queue_update st s t ⦃ s' => s' = s ⦄ := by + unfold smp.remote_queue_update + rw [L.cur_cpu_eq] + step as ⟨cpu, hcpu⟩ + rw [hcpu, h, ite_cond_eq_true _ _ (eq_true rfl)] + exact (WP.spec_ok _).2 rfl + +/-- (record edit B2) `remote_queue_update`'s footprint, and no write at all for a thread of the current core. -/ +theorem smp.remote_queue_update.spec2 (s : S) (t : obj.Tcb) : + smp.remote_queue_update st s t ⦃ s' => (s' = s ∨ ∃ o v, (o.val = 120 ∨ o.val = 128) ∧ + s' = L.wr s (L.nodeAt s (L.cpu s) o) v) ∧ (L.mem s (affA t) = L.cpu s → s' = s) ⦄ := by + by_cases h : L.mem s (affA t) = L.cpu s + · exact WP.spec_mono (smp.remote_queue_update.local s t h) fun s' e => ⟨Or.inl e, fun _ => e⟩ + · exact WP.spec_mono (smp.remote_queue_update.spec s t) fun s' h' => ⟨h', fun e => absurd e h⟩ + /-! ## Placement of the nodes -/ /-- The first byte of core `cpu`'s node (`node_base`). -/ diff --git a/proofs/Fl4ProofsA64/P1/S1aCBudget.lean b/proofs/Fl4ProofsA64/P1/S1aCBudget.lean index 68177e559..f0ffc8d8d 100644 --- a/proofs/Fl4ProofsA64/P1/S1aCBudget.lean +++ b/proofs/Fl4ProofsA64/P1/S1aCBudget.lean @@ -225,16 +225,19 @@ theorem sched.end_timeslice.rpw (hA : P0.ArchTotal ar) {Vc' Ub' Pt' : Set U64} { set_option maxHeartbeats 16000000 in /-- **`charge_budget` to the Reply-walk post** (QueueBudget2's `charge_budget.all` walk with `RpW`: the ring and the SC's `CONSUMED` word, the node's `CONSUMED` word, `end_timeslice.rpw`, `reschedule_required`, `set_reprogram`). Per-state -facts as `charge_budget.all`'s, the placement `Pz`, the current thread can block, `RunVcpuInv`. -/ -theorem sched.charge_budget.rpw (hA : P0.ArchTotal ar) {Vc' Ub' Pt' : Set U64} {s : S} +facts as `charge_budget.all`'s, the placement `Pz`, `RunVcpuInv`; the current thread's SC fact and `CanBlock` only +when it is runnable (they are read on the `is_schedulable` branch alone, so an idle current thread qualifies). -/ +theorem sched.charge_budget.rpwC (hA : P0.ArchTotal ar) {Vc' Ub' Pt' : Set U64} {s : S} {extra : Aeneas.Std.Array obj.Slot 3#usize} {slot : U64} (a : A) (used : U64) (cto : Bool) (B : SysL st H Cs OS ia ie s) (Y : SymL st H Cs s) (h : BK s) (Pz : SysPlace st H Cs H.frame H.replies H.ntfns OS s extra slot) (hSA : ScApart H.scs H.scBits OS) (hTS : TcbSlotsOS H.tcbs OS) (hks : HeapKsOff H (L.ksb s)) (hvr : VrOK H.omega H.frame H.scs H.scBits OS Vc' Ub' (L.ksb s) Pt') - (hcsc : L.mem s (scA (L.curW s)) ≠ 0#u64 → L.cscW s = L.mem s (scA (L.curW s))) + (hcsc : Runnable (L.mem s (stA (L.curW s))) → + L.mem s (scA (L.curW s)) ≠ 0#u64 → L.cscW s = L.mem s (scA (L.curW s))) (hna : L.actionW s ≠ L.curW s) (hcA : L.cscW s ∈ Ac) - (hcb : CanBlock st H.tcbs Ac s (L.curW s)) (hRV : RunVcpuInv st H.tcbs s) : + (hcb : Runnable (L.mem s (stA (L.curW s))) → CanBlock st H.tcbs Ac s (L.curW s)) + (hRV : RunVcpuInv st H.tcbs s) : sched.charge_budget st ar s a used cto ⦃ s' _ => RpPost st H Cs OS Vc ia ie Ac s s' ⦄ := by have h0 := B.ly.h0 obtain ⟨⟨Q, R, q⟩, hB, hX, hS, hSg⟩ := id h @@ -366,9 +369,14 @@ theorem sched.charge_budget.rpw (hA : P0.ArchTotal ar) {Vc' Ub' Pt' : Set U64} { have hcT : L.curW s ∈ H.tcbs := hcT0 have esc : L.mem s3 (scA (L.curW s3)) = L.mem s (scA (L.curW s)) := by rw [ec3, ec2, (f3 _ hcT).1, (f2 _ hcT).1] - have hc : CurOK st H.tcbs s3 := ⟨hrun, hbd, hrl, by - rw [ecs3, ecs2, esc]; exact hcsc (by rw [← esc]; exact hbd), by rw [ea3, ea2, ec3, ec2]; exact hna, hrm⟩ have est : L.mem s3 (stA (L.curW s)) = L.mem s (stA (L.curW s)) := (f3 _ hcT).2.1.trans (f2 _ hcT).2.1 + have hrunS : Runnable (L.mem s (stA (L.curW s))) := by + have e := hrun + rw [ec3, ec2, est] at e + exact e + have hc : CurOK st H.tcbs s3 := ⟨hrun, hbd, hrl, by + rw [ecs3, ecs2, esc]; exact hcsc hrunS (by rw [← esc]; exact hbd), by rw [ea3, ea2, ec3, ec2]; exact hna, hrm⟩ + have hcb := hcb hrunS have eq' : L.mem s3 (inqA (L.curW s)) = L.mem s (inqA (L.curW s)) := (f3 _ hcT).2.2.trans (f2 _ hcT).2.2 have esa : L.mem s3 (scA (L.curW s)) = L.mem s (scA (L.curW s)) := (f3 _ hcT).1.trans (f2 _ hcT).1 have hcb3 : CanBlock st H.tcbs Ac s3 (L.curW s3) := by @@ -399,6 +407,23 @@ theorem sched.charge_budget.rpw (hA : P0.ArchTotal ar) {Vc' Ub' Pt' : Set U64} { · simp only [WP.spec_ok, WP.uncurry'_pair] exact R3.post +set_option maxHeartbeats 16000000 in +/-- **`charge_budget` to the Reply-walk post** (QueueBudget2's `charge_budget.all` walk with `RpW`: the ring and the SC's +`CONSUMED` word, the node's `CONSUMED` word, `end_timeslice.rpw`, `reschedule_required`, `set_reprogram`). Per-state +facts as `charge_budget.all`'s, the placement `Pz`, the current thread can block, `RunVcpuInv`. -/ +theorem sched.charge_budget.rpw (hA : P0.ArchTotal ar) {Vc' Ub' Pt' : Set U64} {s : S} + {extra : Aeneas.Std.Array obj.Slot 3#usize} {slot : U64} (a : A) (used : U64) (cto : Bool) + (B : SysL st H Cs OS ia ie s) (Y : SymL st H Cs s) (h : BK s) + (Pz : SysPlace st H Cs H.frame H.replies H.ntfns OS s extra slot) + (hSA : ScApart H.scs H.scBits OS) (hTS : TcbSlotsOS H.tcbs OS) (hks : HeapKsOff H (L.ksb s)) + (hvr : VrOK H.omega H.frame H.scs H.scBits OS Vc' Ub' (L.ksb s) Pt') + (hcsc : L.mem s (scA (L.curW s)) ≠ 0#u64 → L.cscW s = L.mem s (scA (L.curW s))) + (hna : L.actionW s ≠ L.curW s) (hcA : L.cscW s ∈ Ac) + (hcb : CanBlock st H.tcbs Ac s (L.curW s)) (hRV : RunVcpuInv st H.tcbs s) : + sched.charge_budget st ar s a used cto ⦃ s' _ => RpPost st H Cs OS Vc ia ie Ac s s' ⦄ := + sched.charge_budget.rpwC (Vc := Vc) hA a used cto B Y h Pz hSA hTS hks hvr (fun _ => hcsc) hna hcA + (fun _ => hcb) hRV + section top2 variable {Uq Pp Tb Hr Te Kt : Set U64} {Lv Re bits KLv : U64 → Nat} {n : U64} diff --git a/proofs/Fl4ProofsA64/P1/S1aCReplyErr.lean b/proofs/Fl4ProofsA64/P1/S1aCReplyErr.lean index 8f60c1312..8ff165124 100644 --- a/proofs/Fl4ProofsA64/P1/S1aCReplyErr.lean +++ b/proofs/Fl4ProofsA64/P1/S1aCReplyErr.lean @@ -29,13 +29,11 @@ variable {K : capability.Cap → Prop} {Ty : S → Prop} {Uq Pp Tb Hr Te Kt : Se {n : U64} set_option maxHeartbeats 16000000 in -/-- **`reply_error` keeps the S1a step post** (module docstring). -/ -theorem syscall.reply_error.s1a {s : S} (t : U64) (e : error.SysError) +/-- **The walk of `reply_error`**: the step post `RpPost` (no slot changes). -/ +theorem syscall.reply_error.rpw {s : S} (t : U64) (e : error.SysError) (I : DelIn st ar H Cs OS Vc Uq Pt K Ty s) (B : SysL st H Cs OS ia ie s) (Y : SymL st H Cs s) (h : BK s) - (hc0 : 0#u64 ∈ Cs) (hks : HeapKsOff H (L.ksb s)) (ht : t ∈ H.tcbs) - (C : S1aArmCol st H Cs OS Vc Ub s) (Df : DevFrAl st OS s) - (R : S1aR2 st H OS Pt Pp Tb Hr Te Kt Lv Re bits n s) (sep : S1aKtUt st OS Kt s) (KC : S1aHiK st OS Kt KLv s) : - syscall.reply_error st s t e ⦃ s' => S1aStepR2K st Cs Ub Kt KLv ia ie n s' ⦄ := by + (hc0 : 0#u64 ∈ Cs) (hks : HeapKsOff H (L.ksb s)) (ht : t ∈ H.tcbs) : + syscall.reply_error st s t e ⦃ s' => RpPost st H Cs OS Vc ia ie Ac s s' ⦄ := by have hS : P0.StoreTotal st := L.total have hH := I.del.heap have hO := I.del.os @@ -108,8 +106,19 @@ theorem syscall.reply_error.s1a {s : S} (t : U64) (e : error.SysError) refine WP.spec_bind (P0.msginfo.MessageInfo.to_word.p0 _) fun w _ => ?_ refine WP.spec_mono (hR s3 _ w h3 (by simp [layout.reg.MSG_INFO, layout.reg.X1])) fun s' hf => ?_ obtain ⟨⟨⟨J, k⟩, tk⟩, x⟩ := hf - exact S1aStepR2K.ofRpPost I (RpW.post ⟨J.sys, J.sym, J.blk, XC.keep k tk, x, - (XferSlots.refl s).keep fun z hz => k.capA hz⟩) C Df R sep KC + exact RpW.post ⟨J.sys, J.sym, J.blk, XC.keep k tk, x, + (XferSlots.refl s).keep fun z hz => k.capA hz⟩ + +set_option maxHeartbeats 16000000 in +/-- **`reply_error` keeps the S1a step post** (module docstring). -/ +theorem syscall.reply_error.s1a {s : S} (t : U64) (e : error.SysError) + (I : DelIn st ar H Cs OS Vc Uq Pt K Ty s) (B : SysL st H Cs OS ia ie s) (Y : SymL st H Cs s) (h : BK s) + (hc0 : 0#u64 ∈ Cs) (hks : HeapKsOff H (L.ksb s)) (ht : t ∈ H.tcbs) + (C : S1aArmCol st H Cs OS Vc Ub s) (Df : DevFrAl st OS s) + (R : S1aR2 st H OS Pt Pp Tb Hr Te Kt Lv Re bits n s) (sep : S1aKtUt st OS Kt s) (KC : S1aHiK st OS Kt KLv s) : + syscall.reply_error st s t e ⦃ s' => S1aStepR2K st Cs Ub Kt KLv ia ie n s' ⦄ := + WP.spec_mono (syscall.reply_error.rpw t e I B Y h hc0 hks ht) fun _ P => + S1aStepR2K.ofRpPost I P C Df R sep KC set_option maxHeartbeats 4000000 in /-- **`reply_success_empty` keeps the S1a step post**: the `BADGE` and `MSG_INFO` registers (`XJX.regT` over the diff --git a/proofs/Fl4ProofsA64/P1/S1aCYield.lean b/proofs/Fl4ProofsA64/P1/S1aCYield.lean index a5f31f940..054e0c594 100644 --- a/proofs/Fl4ProofsA64/P1/S1aCYield.lean +++ b/proofs/Fl4ProofsA64/P1/S1aCYield.lean @@ -26,17 +26,17 @@ local notation "wa" => core.num.U64.wrapping_add local notation "BK" s => BlkOK st H.tcbs Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s set_option maxHeartbeats 8000000 in -/-- **`Yield` keeps the S1a step post** (module docstring). -/ -theorem syscall.handle_yield.s1a (hA : P0.ArchTotal ar) {extra : Aeneas.Std.Array obj.Slot 3#usize} {slot : U64} - {s : S} (a : A) - (I : DelIn st ar H Cs OS Vc Uq Pt K Ty s) (B : SysL st H Cs OS ia ie s) (Y : SymL st H Cs s) - (h : BK s) (hks : HeapKsOff H (L.ksb s)) (Pz : SysPlace st H Cs H.frame H.replies H.ntfns OS s extra slot) +/-- **`Yield` to the Reply-walk post** (`charge_budget.rpw`, then the current SC's consumed word). -/ +theorem syscall.handle_yield.rpw (hA : P0.ArchTotal ar) {Vc' Ub' Pt' : Set U64} + {extra : Aeneas.Std.Array obj.Slot 3#usize} {slot : U64} {s : S} (a : A) + (B : SysL st H Cs OS ia ie s) (Y : SymL st H Cs s) + (h : BK s) (Pz : SysPlace st H Cs H.frame H.replies H.ntfns OS s extra slot) + (hSA : ScApart H.scs H.scBits OS) (hTS : TcbSlotsOS H.tcbs OS) (hks : HeapKsOff H (L.ksb s)) + (hvr : VrOK H.omega H.frame H.scs H.scBits OS Vc' Ub' (L.ksb s) Pt') (hcsc : L.mem s (scA (L.curW s)) ≠ 0#u64 → L.cscW s = L.mem s (scA (L.curW s))) (hna : L.actionW s ≠ L.curW s) (hcA : L.cscW s ∈ Ac) - (hcb : CanBlock st H.tcbs Ac s (L.curW s)) (hRV : RunVcpuInv st H.tcbs s) - (C : S1aArmCol st H Cs OS Vc Ub s) (Df : DevFrAl st OS s) - (R : S1aR2 st H OS Pt Pp Tb Hr Te Kt Lv Re bits n s) (sep : S1aKtUt st OS Kt s) (KC : S1aHiK st OS Kt KLv s) : - syscall.handle_yield st ar s a ⦃ s' _ => S1aStepR2K st Cs Ub Kt KLv ia ie n s' ⦄ := by + (hcb : CanBlock st H.tcbs Ac s (L.curW s)) (hRV : RunVcpuInv st H.tcbs s) : + syscall.handle_yield st ar s a ⦃ s' _ => RpPost st H Cs OS Vc ia ie Ac s s' ⦄ := by have h0 := B.ly.h0 obtain ⟨⟨Q, Rq, q⟩, hB, hX, hS, hSg⟩ := id h have hcS : L.cscW s ∈ H.scs := hX.2.2.cur @@ -48,8 +48,8 @@ theorem syscall.handle_yield.s1a (hA : P0.ArchTotal ar) {extra : Aeneas.Std.Arra step with sched.consumed.spec as ⟨i1⟩ step as ⟨total, _⟩ step with sporadic.head.spec as ⟨hd, ehd⟩ - refine WP.spec_bind (sched.charge_budget.rpw (Vc := Vc) hA a hd.amount false B Y h Pz I.del.sca I.ts hks - I.del.vr hcsc hna hcA hcb hRV) ?_ + refine WP.spec_bind (sched.charge_budget.rpw (Vc := Vc) hA a hd.amount false B Y h Pz hSA hTS hks + hvr hcsc hna hcA hcb hRV) ?_ rintro ⟨s1, a1⟩ P1 simp only [WP.uncurry'_pair] at P1 ⊢ step with obj.Sc.set_consumed.spec as ⟨s2, e2⟩ @@ -70,8 +70,25 @@ theorem syscall.handle_yield.s1a (hA : P0.ArchTotal ar) {extra : Aeneas.Std.Arra scw_dmd P1.2.2.1 h0 P1.2.1 P1.1.vr hcS (by simp [layout.sc.CONSUMED]) (by simp [layout.sc.CONSUMED]) ⟨by simp [layout.sc.CONSUMED], by simp [layout.sc.CONSUMED], by simp [layout.sc.CONSUMED], by simp [layout.sc.CONSUMED], fun e => absurd e (by simp [layout.sc.CONSUMED])⟩ - exact S1aStepR2K.ofRpPost I (P1.dmdK Pz.heap Pz.os I.del.sca Pz.core0 I.del.vr hks - (fun k hk => by rw [h0] at hk; cases hk) d2 b2).1 C Df R sep KC + exact (P1.dmdK Pz.heap Pz.os hSA Pz.core0 hvr hks (fun k hk => by rw [h0] at hk; cases hk) d2 b2).1 + +set_option maxHeartbeats 8000000 in +/-- **`Yield` keeps the S1a step post** (module docstring). -/ +theorem syscall.handle_yield.s1a (hA : P0.ArchTotal ar) {extra : Aeneas.Std.Array obj.Slot 3#usize} {slot : U64} + {s : S} (a : A) + (I : DelIn st ar H Cs OS Vc Uq Pt K Ty s) (B : SysL st H Cs OS ia ie s) (Y : SymL st H Cs s) + (h : BK s) (hks : HeapKsOff H (L.ksb s)) (Pz : SysPlace st H Cs H.frame H.replies H.ntfns OS s extra slot) + (hcsc : L.mem s (scA (L.curW s)) ≠ 0#u64 → L.cscW s = L.mem s (scA (L.curW s))) + (hna : L.actionW s ≠ L.curW s) (hcA : L.cscW s ∈ Ac) + (hcb : CanBlock st H.tcbs Ac s (L.curW s)) (hRV : RunVcpuInv st H.tcbs s) + (C : S1aArmCol st H Cs OS Vc Ub s) (Df : DevFrAl st OS s) + (R : S1aR2 st H OS Pt Pp Tb Hr Te Kt Lv Re bits n s) (sep : S1aKtUt st OS Kt s) (KC : S1aHiK st OS Kt KLv s) : + syscall.handle_yield st ar s a ⦃ s' _ => S1aStepR2K st Cs Ub Kt KLv ia ie n s' ⦄ := + WP.spec_mono (syscall.handle_yield.rpw (Vc := Vc) hA a B Y h Pz I.del.sca I.ts hks I.del.vr hcsc hna hcA hcb hRV) + fun r P => by + obtain ⟨s', a'⟩ := r + simp only [WP.uncurry'_pair] at P ⊢ + exact S1aStepR2K.ofRpPost I P C Df R sep KC set_option maxHeartbeats 8000000 in /-- **`check_budget` to the Reply-walk post** (S1aCBudget's `check_budget_restart.rpw`, its first part). -/ diff --git a/proofs/Fl4ProofsA64/P1/S1aDevFr.lean b/proofs/Fl4ProofsA64/P1/S1aDevFr.lean index 1f0907c4d..774e11d98 100644 --- a/proofs/Fl4ProofsA64/P1/S1aDevFr.lean +++ b/proofs/Fl4ProofsA64/P1/S1aDevFr.lean @@ -56,7 +56,7 @@ theorem DevFrP.closed : CapSrcClosed DevFrP where obtain ⟨r', y1, y2, y3, y4, hc⟩ := hf.1 p sz r true m e a v hd exact h p sz r' y1 y2 y3 y4 hc bits hb sub := by - intro p p' sz r r' dv m e a v k h hp q sz' r'' m' e' a' v' hq bits hb + intro p p' sz r r' dv m e a v k h hp _ q sz' r'' m' e' a' v' hq bits hb cases hq have al := h.aligned4K rfl have eq : bits = 12#u64 := (okinj hb).symm diff --git a/proofs/Fl4ProofsA64/P1/S1aHiCap.lean b/proofs/Fl4ProofsA64/P1/S1aHiCap.lean index 78ba9193a..74d0e7734 100644 --- a/proofs/Fl4ProofsA64/P1/S1aHiCap.lean +++ b/proofs/Fl4ProofsA64/P1/S1aHiCap.lean @@ -91,7 +91,7 @@ theorem HvRootP.closed (R : U64 → Prop) : · obtain ⟨m', a', hc⟩ := al_hv ha exact h p m' a' hc · cases e - sub := fun _ _ _ _ _ _ e => by cases e + sub := fun _ _ _ _ _ _ _ e => by cases e remap := by intro d c hm h p m a hd subst hd diff --git a/proofs/Fl4ProofsA64/P1/S1aR2Ept.lean b/proofs/Fl4ProofsA64/P1/S1aR2Ept.lean index cf56e62cc..138566336 100644 --- a/proofs/Fl4ProofsA64/P1/S1aR2Ept.lean +++ b/proofs/Fl4ProofsA64/P1/S1aR2Ept.lean @@ -38,7 +38,7 @@ theorem EptPlaceP.closed : CapSrcClosed (EptPlaceP H Pt) where zombie := fun _ _ _ _ _ _ _ _ e => by cases e ut := fun _ _ _ _ _ _ _ _ _ e => by cases e irq := fun _ _ _ _ _ _ e => by cases e - sub := fun _ _ _ _ _ _ _ _ e => by cases e + sub := fun _ _ _ _ _ _ _ _ _ e => by cases e der := by intro d c hc _ valid p l m a va he subst d diff --git a/proofs/Fl4ProofsA64/P1/S1aRtTopK.lean b/proofs/Fl4ProofsA64/P1/S1aRtTopK.lean new file mode 100644 index 000000000..05b1d319c --- /dev/null +++ b/proofs/Fl4ProofsA64/P1/S1aRtTopK.lean @@ -0,0 +1,287 @@ +/- +P1 (aarch64): **the RAM creation of a Retype decode without a reset, at the retype level**, from the restart state +`s2`, at explicit creation parameters (A64-P3 Untyped, Architect ruling (c)). The decode-level top (`s1aRtVJH` and the +frame lemmas `tabK`, `keepOwnedVsWords`, `keepActivePaths`, `newHighInit`, `keepOldHi`) gives its creation end at +existential parameters; here the same facts are restated at the `retype` call itself, reusing the decode-level +lemmas' `UtEnd` consequences. Merged P1 signatures are unchanged. + +Named premises: none new. New theorems only. +-/ +import Fl4ProofsA64.P1.S1aRtV +import Fl4ProofsA64.P1.S1aR2RetypeEnd + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P1.W7.R.ST +open fl4_core_a64 Fl4ProofsA64.P0.Consts Fl4Proofs.P3 + +local notation "wa" => core.num.U64.wrapping_add + +variable {S : Type _} {st : machine.Store S} [L : LawfulStore st] {H H' : Heap} +variable {OS Cs Pt Kt : Set U64} {ia ie : Nat} {A : Type _} {ar : machine.Arch A} +variable {U Ac Sg Scs Vc' Ub' Vc Ub : Set U64} {Z : U64 → Nat} {s0 s2 : S} + {extra : Aeneas.Std.Array obj.Slot 3#usize} {slot p sb fi aligned nt us dest off win : U64} + +set_option maxHeartbeats 4000000 in +/-- **The retype-level `SysL` post** (`sysLBH`'s non-reset RAM branch) with the new `Tcb` window and the new +`HighVSpace` words. -/ +theorem untyped.retype.sysLRam (hA : P0.ArchTotal ar) + (D : DecPre st H.tcbs Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits U Ac s0 (.Untyped p sb fi false) extra) + (P : SysPlace st H Cs H.frame H.replies H.ntfns OS s0 extra slot) (B : SysL st H Cs OS ia ie s0) + (hcap : obj.Slot.cap st slot s0 = ok (.Untyped p sb fi false)) + (e : sched.set_thread_state st s0 (L.curW s0) sched.TS_RESTART = ok s2) (a : A) + (hU : UtArgs st s0 extra slot p sb fi false false aligned nt us dest off win) + (hNew : ∀ q k, H' q = some k → H q = some k ∨ + (nt.val ≠ 0 ∧ aligned.val ≤ q.val ∧ q.val + 2 ^ k.bits ≤ aligned.val + win.val * 2 ^ (zA nt.val us).val)) + (hHH' : ∀ q k, H q = some k → H' q = some k) + (hEx : ∀ q k, H' q = some k → H q = some k ∨ + ((false : Bool) = false ∧ ∃ j, j < win.val ∧ q = bjA aligned (zA nt.val us) j ∧ newKind nt.val us.val = some k)) + (hHn : (false : Bool) = false → + ∀ j, j < win.val → ∀ k, newKind nt.val us.val = some k → H' (bjA aligned (zA nt.val us) j) = some k) : + untyped.retype st ar s2 a slot false aligned nt us dest off win false ⦃ r => + SysL st H' Cs OS ia ie r.2.1 ∧ (nt.val = 1 → WinT st r.2.1 aligned (zA nt.val us) win.val) ∧ + WinHi st r.2.1 aligned nt us win.val ⦄ := by + have R := UtDecP.of_live D B.sys P hcap B.ln B.vr B.ly + obtain ⟨Ac0, hB0⟩ := R.bxi + have B0 : BufInv st H OS s0 := BufInv.of B.cn B.bh + obtain ⟨Ac', hb⟩ := R.blk + obtain ⟨Q, Rq, q⟩ := hb.1 + obtain ⟨hdev, hal, hfi, hA0, hAw, hinj, hcan, -⟩ : + UtAr H p sb fi false aligned nt us dest off win := R.arith _ _ _ _ _ _ _ hU + refine WP.spec_mono (WP.spec_and (WP.spec_and (untyped.retype.stZFCA hA R.heap R.os R.win R.ty R.stz R.fr R.cz q + R.dbg R.run e R.core0 a R.ext R.src R.cap hU hdev hal hfi hA0 hAw hNew hHH' hHn hinj hcan) + (untyped.retype.urU hA R.heap R.os R.win R.ty R.stz R.ur q R.dbg R.run e R.core0 a R.ext R.src R.cap hU hdev + hal hfi hA0 hAw hNew hinj hcan)) + (untyped.retype.live0IBH hA R B.ln B.vr B.ly e a hU hHH' hEx hHn + (fun H' OS1 s' => (BufInv st H' OS1 s' ∧ CapInv st (IdP (L.idleW s') (L.mem s' (scA (L.idleW s')))) OS1 s' ∧ + (CapInv st (ScCapP H'.scs) OS1 s' ∧ CtOff st (L.ksb s') OS1 s' ∧ (FrOff st Cs OS1 (L.ksb s') s' ∧ (EptSlot st H' OS1 s' ∧ IpcAl st H' s')))) ∧ + ((nt.val = 1 → WinT st s' aligned (zA nt.val us) win.val) ∧ WinHi st s' aligned nt us win.val)) + fun s1 s' lo hi U UB WH => ⟨⟨U.buf R.heap B0 R.cap, U.ci R B.ly B.ci (UtArgs.devTS hU), + ⟨U.sc R B.ly B.sc (UtArgs.devTS hU) (fun _ _ _ h => h) + (fun j hj h5 => ⟨us.val, hHn (UtArgs.dev5 hU h5) j hj _ (by rw [h5]; simp [newKind])⟩), + U.ct R B.ly B.ct (UtArgs.us64 hU), ⟨U.fo R B.fo (UtDecP.fo R hal hA0 hAw), ⟨U.ept R.heap B0 B.ep R.cap, U.ipc R.heap R.bxi.choose_spec R.core0 B.ipc⟩⟩⟩⟩, + ⟨fun n => UB.wt rfl n, WH⟩⟩)) ?_ + rintro ⟨r, s', a'⟩ ⟨⟨⟨-, h1, h2, h3⟩, h4⟩, ⟨h5, h6, h7⟩, h8⟩ + exact ⟨⟨⟨h1, h2, h3, h4⟩, h5, h6, h7, h8.1.1.cn, h8.1.1.bh, h8.1.2.1, h8.1.2.2.1, h8.1.2.2.2.1, + h8.1.2.2.2.2.1, h8.1.2.2.2.2.2.1, h8.1.2.2.2.2.2.2⟩, h8.2⟩ + + +section top +variable (D : DecPre st H.tcbs Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits U Ac s0 (.Untyped p sb fi false) extra) + (P : SysPlace st H Cs H.frame H.replies H.ntfns OS s0 extra slot) (B : SysL st H Cs OS ia ie s0) + (hcap : obj.Slot.cap st slot s0 = ok (.Untyped p sb fi false)) + (e : sched.set_thread_state st s0 (L.curW s0) sched.TS_RESTART = ok s2) + (hU : UtArgs st s0 extra slot p sb fi false false aligned nt us dest off win) +include D P B hcap e hU + +/-- The restart write and the creation's frame, at any word set off the objects the decode keeps. -/ +theorem untyped.retype.tabKRam (hA : P0.ArchTotal ar) + (hvr : VrOK H.omega Sg Scs Z OS Vc' Ub' (L.ksb s0) Pt) (a : A) : + untyped.retype st ar s2 a slot false aligned nt us dest off win false ⦃ r => + WritesIn st s0 r.2.1 (fun b => ¬ AsW (L.ksb s0) b) ⦄ := by + have R := UtDecP.of_live D B.sys P hcap B.ln B.vr B.ly + obtain ⟨Ac', hb⟩ := R.blk + obtain ⟨Q, Rq, q⟩ := hb.1 + have F : TabF st H Cs OS s0 := TabF.of hvr q B.ly.ksw R.core0 + have hX : ∀ OS1 : Set U64, (∀ x ∈ OS1, x ∈ OS) → ∀ x ∈ OS1, ∀ o : U64, o.val < 32 → o.val % 8 = 0 → + wa x o ∉ {b | AsW (L.ksb s0) b} := fun OS1 hs x hx o ho _ hT => + F.os _ hT ⟨x, hs x hx, by rw [wv (R.os.fit x (hs x hx)) ho]; omega, by rw [wv (R.os.fit x (hs x hx)) ho]; omega⟩ + have hKs : (L.ksb s0).val + 788736 ≤ p.val ∨ p.val + 2 ^ sb.val ≤ (L.ksb s0).val := + (R.stz.2.2 slot R.src p sb fi R.cap).2 + obtain ⟨-, w⟩ := restart_frame (OS := OS) R.os q e + have w2 : WritesIn st s0 s2 (fun b => ¬ AsW (L.ksb s0) b) := WritesIn.monoA w fun b _ hb' hT => by + rcases hb' with e' | e' + · exact F.tcb R.heap q.curT (K := layout.tcb.STATE) (by simp) hT e'.symm + · exact F.node rfl F.cur F.ncur (by simp) hT e'.symm + have hb0 : 0 < aligned.val := + lt_of_lt_of_le (B.ly.ut0 slot R.src p sb fi R.cap) (CrtA.of_args R hU).pa + have C := (CrtP.of0 R e hU).widen (hX OS fun _ h => h) + refine WP.spec_mono (untyped.retype.liveR hA C hb0 a) ?_ + rintro ⟨r, s', a'⟩ ⟨E, -⟩ + refine WritesIn.monoA (w2.trans E.fr) fun b _ hb' hT => ?_ + rcases hb' with h | h + · exact h hT + · refine F.notRFw R.heap (fun _ h => h) w2.1 (fun _ hT _ ⟨g1, g2⟩ => ?_) hT h + have h1 := (CrtA.of_args R hU).pa + have h2 := (CrtA.of_args R hU).aw + exact F.reg hKs hT ⟨by omega, by omega⟩ + +/-- Retype frames placed VSpace words outside the allocation window. -/ +theorem untyped.retype.keepVsWordsRam (hA : P0.ArchTotal ar) + (vr : VrOK H.omega Sg Scs Z OS Vc' Ub' (L.ksb s0) Pt) {X : Set U64} + (pt : ∀ b ∈ X, VsR (L.ksb s0) Pt b) + (freeOff : ∀ b ∈ X, ¬ (p.val + fi.val ≤ b.val ∧ b.val < p.val + 2 ^ sb.val)) (a : A) : + untyped.retype st ar s2 a slot false aligned nt us dest off win false ⦃ r => + ∀ b ∈ X, L.mem r.2.1 b = L.mem s0 b ⦄ := by + have R := UtDecP.of_live D B.sys P hcap B.ln B.vr B.ly + obtain ⟨_, hb⟩ := R.blk + obtain ⟨_, _, q⟩ := hb.1 + have osOff : ∀ x ∈ OS, ∀ o : U64, o.val < 32 → wa x o ∉ X := by + intro x hx o ho hin + have value := wv (R.os.fit x hx) ho + exact vr.os _ (pt _ hin) ⟨x, hx, by rw [value]; omega, by rw [value]; omega⟩ + have outside : ∀ b ∈ X, OutsideObjs st H.tcbs Cs s0 b := + fun b hb => q.omega b (vr.om b (pt b hb)) + have tcb : ∀ {t k b : U64}, H t = some .tcb → k.val < 4096 → b ∈ X → wa t k ≠ b := + fun ht hk hb eq => out_ne_field (outside _ hb) ht (q.tcbs _ ht) hk (congrArg UScalar.val eq) + have node : ∀ {s1 : S} {c o b : U64}, L.ksb s1 = L.ksb s0 → c ∈ Cs → o.val < 4288 → b ∈ X → + L.nodeAt s1 c o ≠ b := by + intro s1 c o b hks hc ho hb eq + rw [nodeAt_of_ksb hks] at eq + exact out_ne_node (outside b hb) hc (q.nodes c hc) ho (congrArg UScalar.val eq) + have w := (restart_frame R.os q e).2 + have restart : WritesIn st s0 s2 (fun b => b ∉ X) := w.mono fun b h hb => by + rcases h with eq | eq + · rw [stA_eq] at eq + exact tcb q.curT (by simp) hb eq.symm + · exact node rfl q.cur (by simp) hb eq.symm + have arith := UtArgs.arithA R.ty R.ext R.cap R.utv hU + have offWindow : ∀ b ∈ X, WKA nt → + ¬ (aligned.val ≤ b.val ∧ b.val < aligned.val + win.val * 2 ^ (zA nt.val us).val) := by + intro b hb _ ⟨l, u⟩ + have low : p.val + fi.val ≤ aligned.val := by simpa using arith.2.2.2.1 + exact freeOff b hb ⟨le_trans low l, lt_of_lt_of_le u arith.2.2.2.2.1⟩ + have positive : 0 < aligned.val := + lt_of_lt_of_le (B.ly.ut0 slot R.src p sb fi R.cap) (CrtA.of_args R hU).pa + have C := (CrtP.of0 R e hU).widen (X := X) (fun x hx o ho _ => osOff x hx o ho) + refine WP.spec_mono (untyped.retype.liveR hA C positive a) ?_ + rintro ⟨_, s', _⟩ ⟨E, _⟩ b hb + have whole : WritesIn st s0 s' (fun b => b ∉ X) := + (restart.trans E.fr).mono fun b h hb => by + rcases h with out | (⟨t, ht, k, hk, eq⟩ | ⟨_, out⟩) | ⟨wk, lo, hi⟩ | eq | ⟨t, ht, eq⟩ + · exact out hb + · exact tcb ht (by omega) hb eq.symm + · exact out (Set.mem_union_right _ hb) + · exact offWindow b hb wk ⟨lo, hi⟩ + · exact node restart.1 R.core0 (by simp) hb eq.symm + · exact tcb ht (by simp) hb eq.symm + exact whole.mem_eq (vr.al b (pt b hb)) (fun h => h hb) + +end top + +section joint +variable (D : DecPre st H.tcbs Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits U Ac s0 (.Untyped p sb fi false) extra) + (P : SysPlace st H Cs H.frame H.replies H.ntfns OS s0 extra slot) (B : SysL st H Cs OS ia ie s0) + (hcap : obj.Slot.cap st slot s0 = ok (.Untyped p sb fi false)) + (e : sched.set_thread_state st s0 (L.curW s0) sched.TS_RESTART = ok s2) + (hU : UtArgs st s0 extra slot p sb fi false false aligned nt us dest off win) +include D P B hcap e hU + +/-- **The retype-level joint post** (`s1aRtVJH` with the frame lemmas, at the creation's own parameters): P1's +`ArmQIVAt` and the arm columns at the grown heap, the creation end `S1aRtIn`, and the frame facts `S1aRtIn.r2` +and `HiCap511` read. `hUe`: the creation's end (`UtEnd` from the restart state). -/ +theorem untyped.retype.s1aTopRam (hA : P0.ArchTotal ar) (Y : SymL st H Cs s0) + {Pp Tb Hr Te : Set U64} {Lv Re bits : U64 → Nat} {n : U64} + (vr : VrOK H.omega H.frame H.scs H.scBits OS Vc' Ub' (L.ksb s0) Pt) (hPN : PoolNamed st OS s0) + (C : S1aArmCol st H Cs OS Vc Ub s0) (K : S1aCapCol st H OS Ub s0) (hal : ∀ b ∈ Ub, Aligned b) + (hVC : VcCap st Vc OS s0) (h : S1aR2 st H OS Pt Pp Tb Hr Te Kt Lv Re bits n s0) (sep : S1aKtUt st OS Kt s0) + {KLv : U64 → Nat} (kt : KTab ar Kt KLv) (a : A) + (hNew : ∀ q k, H' q = some k → H q = some k ∨ + (nt.val ≠ 0 ∧ aligned.val ≤ q.val ∧ q.val + 2 ^ k.bits ≤ aligned.val + win.val * 2 ^ (zA nt.val us).val)) + (hHH' : ∀ q k, H q = some k → H' q = some k) + (hEx : ∀ q k, H' q = some k → H q = some k ∨ + ((false : Bool) = false ∧ ∃ j, j < win.val ∧ q = bjA aligned (zA nt.val us) j ∧ newKind nt.val us.val = some k)) + (hHn : (false : Bool) = false → + ∀ j, j < win.val → ∀ k, newKind nt.val us.val = some k → H' (bjA aligned (zA nt.val us) j) = some k) + (hUe : untyped.retype st ar s2 a slot false aligned nt us dest off win false ⦃ r => + ∃ lo hi : Nat, UtEnd st H Cs OS s0 slot p sb false H H' OS s2 r.2.1 lo hi aligned nt us dest off win ⦄) : + untyped.retype st ar s2 a slot false aligned nt us dest off win false ⦃ r => + ∃ (Vn Vc'' : Set U64) (lo hi : Nat), + S1aRtIn st H Cs OS U Ac s0 ia ie slot p sb fi false extra H H' OS s2 r.2.1 lo hi aligned nt us dest off + win ∧ + ArmQIVAt st H Cs OS ia ie s0 H' (s1aRtSlots OS false nt us aligned win) Vn r.2.1 ∧ TabKeep st s0 r.2.1 ∧ + Vc'' ⊆ Vc ∪ Vn ∧ VcCap st Vc'' (s1aRtSlots OS false nt us aligned win) r.2.1 ∧ + S1aArmCol st H' Cs (s1aRtSlots OS false nt us aligned win) Vc'' Ub r.2.1 ∧ + S1aCapCol st H' (s1aRtSlots OS false nt us aligned win) Ub r.2.1 ∧ + (∀ b, VsR (L.ksb s0) Pt b → S1aRetypeWordOwner st H OS Kt s0 b → L.mem r.2.1 b = L.mem s0 b) ∧ + (∀ asid raw, ActiveAsidRoot st Pp r.2.1 asid raw ↔ ActiveAsidRoot st Pp s0 asid raw) ∧ + (∀ tag asid sh base t, S1aPath st Pp n r.2.1 tag asid sh base t ↔ S1aPath st Pp n s0 tag asid sh base t) ∧ + (∀ x ∈ OS, capA st s0 x = .Null → + (cdt.ensure_no_children st s0 slot = ok (.Ok ()) → x.val + 32 ≤ p.val ∨ p.val + 2 ^ sb.val ≤ x.val) → + ∀ t m asid, capA st r.2.1 x = .HighVSpace t m asid → S1aHighInit st Kt r.2.1 t) ∧ + HrW st (HvRt st OS s0) s0 r.2.1 ∧ WinHi st r.2.1 aligned nt us win.val ⦄ := by + have R := UtDecP.of_live D B.sys P hcap B.ln B.vr B.ly + obtain ⟨_, hb⟩ := R.blk + obtain ⟨_, _, q⟩ := hb.1 + have source : capA st s0 slot = .Untyped p sb fi false := R.cap + have X : UtX H H 0 0 us := UtX.refl (UtArgs.us64 hU) + have X' : ∀ lo hi : Nat, UtX H H lo hi us := fun _ _ => UtX.refl (UtArgs.us64 hU) + have keep := (restart_frame (OS := OS) R.os q e).1 + have full := (UtArgs.zalA R.ty R.ext R.cap R.utv hU).1 + have positive : 0 < aligned.val := + lt_of_lt_of_le (B.ly.ut0 slot R.src p sb fi R.cap) (CrtA.of_args R hU).pa + -- the words of live objects and kernel tables + have owned : untyped.retype st ar s2 a slot false aligned nt us dest off win false ⦃ r => + ∀ b ∈ {b | VsR (L.ksb s0) Pt b ∧ S1aRetypeWordOwner st H OS Kt s0 b}, L.mem r.2.1 b = L.mem s0 b ⦄ := by + refine untyped.retype.keepVsWordsRam D P B hcap e hU hA vr + (X := {b | VsR (L.ksb s0) Pt b ∧ S1aRetypeWordOwner st H OS Kt s0 b}) (fun _ g => g.1) ?_ a + intro b hb' touch + obtain ⟨t, k, obj, ⟨lo, hi⟩, _⟩ := hb'.2 + obtain ⟨_, _, valid, _, _⟩ := R.stz.1 + have off := valid slot P.src p sb fi source t k obj + rcases off with left | right <;> omega + -- the high roots' reserved words + have hiWords : untyped.retype st ar s2 a slot false aligned nt us dest off win false ⦃ r => + HrW st (HvRt st OS s0) s0 r.2.1 ⦄ := by + have hg := h.footprint + have hv : HvPt st Pt OS s0 := fun y hy t mp asid cap => + h.highFootprint ⟨t, h.highRoots ▸ ⟨y, hy, mp, asid, cap⟩, Or.inl rfl⟩ + let X : Set U64 := {b | ∃ t ∈ HvRt st OS s0, b = wa t 4088#u64} + have pt : ∀ b ∈ X, VsR (L.ksb s0) Pt b := by + rintro _ ⟨t, ht, rfl⟩ + exact Or.inl ⟨t, hv.rt ht, 4088#u64, by simp, by simp, rfl⟩ + have val : ∀ t ∈ HvRt st OS s0, (wa t 4088#u64).val = t.val + 4088 := by + intro t ht + have hf := hg.fit R.heap t (hv.rt ht) + exact wadd_val (by change t.val + 4088 < 2 ^ 64; omega) + have freeOff : ∀ b ∈ X, ¬ (p.val + fi.val ≤ b.val ∧ b.val < p.val + 2 ^ sb.val) := by + rintro b ⟨t, ht, rfl⟩ bounds + obtain ⟨_, obj, z, he, hlo, hhi⟩ := hg t (hv.rt ht) + obtain ⟨⟨_, _, hvalid, _, _⟩, _, _⟩ := R.stz + have hoff := hvalid slot R.src p sb fi source obj (.other z) he + simp only [HKind.bits] at hoff + rw [val t ht] at bounds + omega + exact WP.spec_mono (untyped.retype.keepVsWordsRam D P B hcap e hU hA vr pt freeOff a) + fun _ k t ht => k _ ⟨t, ht, rfl⟩ + have C0 := CrtP.of0 R e hU + refine WP.spec_mono (WP.spec_and (untyped.retype.sysLRam D P B hcap e a hU hNew hHH' hEx hHn (hA := hA)) + (WP.spec_and hUe (WP.spec_and (untyped.retype.tabKRam D P B hcap e hU hA vr a) + (WP.spec_and owned (WP.spec_and hiWords + (untyped.retype.newHighInit hA kt C0 positive full a)))))) ?_ + rintro ⟨r, s', a'⟩ ⟨⟨B', WT, WH⟩, ⟨lo, hi, E⟩, writes, words, hiw, fresh⟩ + have hw : win.val = 0 ∨ ∃ reset, UtArgs st s0 extra slot p sb fi false reset aligned nt us dest off win := + Or.inr ⟨_, hU⟩ + have rt : S1aRtIn st H Cs OS U Ac s0 ia ie slot p sb fi false extra H H' OS s2 s' lo hi aligned nt us dest off + win := ⟨D, P, B, hcap, E, X' lo hi, hw, Or.inl rfl⟩ + have B'' : SysL st H' Cs (OS ∪ NewSl false nt us aligned win) ia ie s' := B'.growN (E.freshN (X' lo hi) B'.ly.ksw) + have blk := UtEnd.blk R B.ly E (X' lo hi) hw ⟨R.blk2 e, fun _ n => WT n⟩ + have tk : TabKeep st s0 s' := TabKeep.ofW vr writes fun _ g => g + have hR := UtEnd.rtArmI D P B Y hcap H H' OS s2 s' lo hi aligned nt us dest off win E (X' lo hi) hw (Or.inl rfl) + have hE := UtEnd.s1aEndV D P B hcap C K hal hVC H H' OS s2 s' lo hi aligned nt us dest off win E (X' lo hi) hw + (Or.inl rfl) + obtain ⟨_, hQ⟩ := RtArmI.fin P.heap hPN C.case.pobj B'' blk hR tk + obtain ⟨Vn, Vc'', hVn, hVT, hsub, hC, hcol, hK⟩ := S1aEndV.fin P vr hPN C B'' writes hE + have words' : ∀ b, VsR (L.ksb s0) Pt b → S1aRetypeWordOwner st H OS Kt s0 b → L.mem s' b = L.mem s0 b := + fun b g1 g2 => words b ⟨g1, g2⟩ + have roots : ∀ asid raw, ActiveAsidRoot st Pp s' asid raw ↔ ActiveAsidRoot st Pp s0 asid raw := fun asid raw => + tk.activeRootIff fun t ht o width al => + let ⟨placed, owner⟩ := h.poolWordOwner P.heap ht width al + words' _ (Or.inl placed) owner + have paths : ∀ tag asid sh base t, + S1aPath st Pp n s' tag asid sh base t ↔ S1aPath st Pp n s0 tag asid sh base t := by + intro tag asid sh base t + refine S1aPath.iffOfWords roots ?_ ?_ + · intro asid raw entry tag + obtain ⟨placed, owner⟩ := h.highWordOwner P.heap entry tag + exact words' _ (Or.inl placed) owner + · intro tag asid sh base t path o width al + obtain ⟨x, hx, root, off', z, obj, _, bound, address, _, key, ptr, _⟩ := h.pathWordObject path width + exact words' _ (Or.inl (h.pathWord path width al)) + (by rw [address]; exact ⟨root, .other z, obj, inObjWord P.heap obj bound, Or.inr ⟨x, hx, key, ptr⟩⟩) + exact ⟨Vn, Vc'', lo, hi, rt, hQ.reVn hVT hVn, tk, hsub, hC, hcol, hK, words', roots, paths, + fun x hx empty _ => fresh x hx ((keep.capA hx).trans empty), hiw, WH⟩ + +end joint + +end Fl4ProofsA64.P1.W7.R.ST diff --git a/proofs/Fl4ProofsA64/P1/S1aRtTopRst.lean b/proofs/Fl4ProofsA64/P1/S1aRtTopRst.lean new file mode 100644 index 000000000..d5652e099 --- /dev/null +++ b/proofs/Fl4ProofsA64/P1/S1aRtTopRst.lean @@ -0,0 +1,444 @@ +/- +P1 (aarch64): **the RAM creation of a Retype decode after a reset, at the retype level**, from the reset's exit `s3` +(any successful `reset_untyped` from the restart state `s2`), at explicit creation parameters (A64-P3 Untyped, +Architect ruling (c); the `reset = true` sibling of `S1aRtTopK`). The reset's retype is `reset_untyped` followed by the +creation (`untyped.retype.resetEqA`), so the facts here are stated at `retype … false` from `s3`; at an unused untyped +`s3 = s2`. The decode-level lemmas' reset branches (`sysLBH`, `tabK`, `keepVsWords`, `newHighInit`) are restated at +that call. Merged P1 signatures are unchanged. + +Named premises: none new. New theorems only. +-/ +import Fl4ProofsA64.P1.S1aRtTopK +import Fl4ProofsA64.P3.UtCrtResetA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P1.W7.R.ST +open fl4_core_a64 Fl4ProofsA64.P0.Consts Fl4Proofs.P3 + +local notation "wa" => core.num.U64.wrapping_add + +variable {S : Type _} {st : machine.Store S} [L : LawfulStore st] {H H' : Heap} +variable {OS Cs Pt Kt : Set U64} {ia ie : Nat} {A : Type _} {ar : machine.Arch A} +variable {U Ac Sg Scs Vc' Ub' Vc Ub : Set U64} {Z : U64 → Nat} {s0 s2 s3 : S} + {extra : Aeneas.Std.Array obj.Slot 3#usize} {slot p sb fi aligned nt us dest off win : U64} + +/-- The retype that resets is the reset followed by the retype that does not. -/ +theorem untyped.retype.resetEqA {s s3 : S} {a a3 : A} {slot base nt us dest off len : U64} {dev : Bool} + (e : untyped.reset_untyped st ar s a slot = ok (core.result.Result.Ok (), s3, a3)) : + untyped.retype st ar s a slot true base nt us dest off len dev = + untyped.retype st ar s3 a3 slot false base nt us dest off len dev := by + unfold untyped.retype + simp only [Bool.false_eq_true, ↓reduceIte, e, bind_ok, core.result.Result.Insts.CoreOpsTry.branch, + uncurry_apply_pair] + +section reset +variable (D : DecPre st H.tcbs Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits U Ac s0 (.Untyped p sb fi false) extra) + (P : SysPlace st H Cs H.frame H.replies H.ntfns OS s0 extra slot) (B : SysL st H Cs OS ia ie s0) + (hcap : obj.Slot.cap st slot s0 = ok (.Untyped p sb fi false)) + (e : sched.set_thread_state st s0 (L.curW s0) sched.TS_RESTART = ok s2) + {a a3 : A} + (hU : UtArgs st s0 extra slot p sb fi false true aligned nt us dest off win) + (eres : untyped.reset_untyped st ar s2 a slot = ok (core.result.Result.Ok (), s3, a3)) +include D P B hcap e hU eres + +/-- A RAM reset's source has no CDT child. -/ +theorem untyped.retype.rstChildlessA : cdt.ensure_no_children st s0 slot = ok (.Ok ()) := by + obtain ⟨-, -, -, -, -, -, -, -, -, -, resetEq, -⟩ := id hU + obtain ⟨v, nc⟩ := resetEq.1 rfl + cases v + exact nc + +/-- **The reset's exit**: the kept slots' ranges (`UtRV`) and A64 P1's `BlkOK` over the dropped heap. -/ +theorem untyped.retype.rstExitA (hA : P0.ArchTotal ar) : + UtRV st (H.drop (RegionD H p.val (p.val + 2 ^ sb.val))) (OffR p.val (p.val + 2 ^ sb.val) OS) s3 ∧ + ∃ Ac1, BlkOK st (H.drop (RegionD H p.val (p.val + 2 ^ sb.val))).tcbs Cs + (H.drop (RegionD H p.val (p.val + 2 ^ sb.val))).omega (H.drop (RegionD H p.val (p.val + 2 ^ sb.val))) + (H.drop (RegionD H p.val (p.val + 2 ^ sb.val))).frame (H.drop (RegionD H p.val (p.val + 2 ^ sb.val))).replies + (H.drop (RegionD H p.val (p.val + 2 ^ sb.val))).scs (H.drop (RegionD H p.val (p.val + 2 ^ sb.val))).ntfns + (H.drop (RegionD H p.val (p.val + 2 ^ sb.val))).scBits Ac1 s3 := by + have R := UtDecP.of_live D B.sys P hcap B.ln B.vr B.ly + obtain ⟨C, keep, -⟩ := utCrtP_resetA hA R e a a3 hU eres + obtain ⟨x, hxm, hx0, idx, dep, val, ers, hv64, erd, ees, hrs, -⟩ := id hU + have he := hrs.1 rfl + obtain ⟨Ac, Q, Rq, q, k, w, hact, q2, hDb2, hSt2, hF2, hCZ2, hb2, hcs2, hW2, hnd0, hnd2⟩ := R.restart e he + obtain ⟨hcap0, hpal, hfit, P0, hnode0, hsz⟩ := R.rst rfl he + have P2 := DropOK.restart P0 q w hact + have hnode2 : RegionOffNodes st Cs s2 p.val (p.val + 2 ^ sb.val) := by + unfold RegionOffNodes LawfulStore.nodeBase at hnode0 ⊢; rw [w.1]; exact hnode0 + have hRk2 : ResetOK st H Cs s2 slot p sb fi false := ⟨by rw [capA_eq, hcs2], hpal, hfit, P2, hnode2, hsz⟩ + have hrun := resetP1Step_holds (st := st) hA H Cs Ac s2 a slot p sb fi false hb2 hRk2.1 R.slotw hpal hfit P2 + hnode2 hsz + rw [eres, WP.spec_ok] at hrun + obtain ⟨H2, Ac2, hbR, hsh, -, -⟩ := hrun + have e2 := reset_heap_eq hsh + subst e2 + refine ⟨?_, Ac2, hbR⟩ + refine UtRV.mono (H := H) (fun y hy => ?_) fun q k hq => (Heap.drop_eq_some.1 hq).2 + by_cases hyx : y = slot + · rw [hyx, C.cap] + have h1 := R.ur slot R.src + rw [R.cap] at h1 + exact h1.fi + · rw [keep y hy hyx]; exact R.ur y hy.1 + +variable (hNew : ∀ q k, H' q = some k → (H.drop (RegionD H p.val (p.val + 2 ^ sb.val))) q = some k ∨ + (nt.val ≠ 0 ∧ aligned.val ≤ q.val ∧ q.val + 2 ^ k.bits ≤ aligned.val + win.val * 2 ^ (zA nt.val us).val)) + (hHH' : ∀ q k, (H.drop (RegionD H p.val (p.val + 2 ^ sb.val))) q = some k → H' q = some k) + (hEx : ∀ q k, H' q = some k → (H.drop (RegionD H p.val (p.val + 2 ^ sb.val))) q = some k ∨ + (false = false ∧ ∃ j, j < win.val ∧ q = bjA aligned (zA nt.val us) j ∧ newKind nt.val us.val = some k)) + (hHn : false = false → + ∀ j, j < win.val → ∀ k, newKind nt.val us.val = some k → H' (bjA aligned (zA nt.val us) j) = some k) +include hNew hHH' hEx hHn + +set_option maxHeartbeats 4000000 in +/-- **The retype-level `SysL` post after a reset** (`sysLBH`'s RAM-reset branch) over the kept slots, with the new +`Tcb` window and the new `HighVSpace` words. -/ +theorem untyped.retype.sysLRst (hA : P0.ArchTotal ar) : + untyped.retype st ar s3 a3 slot false aligned nt us dest off win false ⦃ r => + SysL st H' Cs (OffR p.val (p.val + 2 ^ sb.val) OS) ia ie r.2.1 ∧ + (nt.val = 1 → WinT st r.2.1 aligned (zA nt.val us) win.val) ∧ WinHi st r.2.1 aligned nt us win.val ⦄ := by + have R := UtDecP.of_live D B.sys P hcap B.ln B.vr B.ly + obtain ⟨Ac0, hB0⟩ := R.bxi + have B0 : BufInv st H OS s0 := BufInv.of B.cn B.bh + obtain ⟨C, keep, hw1⟩ := utCrtP_resetA hA R e a a3 hU eres + obtain ⟨hR1, -⟩ := untyped.retype.rstExitA D P B hcap e hU eres hA + obtain ⟨x, hxm, hx0, idx, dep, val, ers, hv64, erd, ees, hrs, -⟩ := id hU + have he := hrs.1 rfl + obtain ⟨-, -, -, -, -, -, -, -, -, -, -, -, -, -, -, hnd0, -⟩ := R.restart e he + have F := DropF.of R (R.drop rfl he) hnd0 + obtain ⟨hsrc1, hnn, hRT, hBT, hKT, hIT, hhole, hH1, -, -, hkp, hkps, hoff⟩ := id F + have hb0 : 0 < aligned.val := + lt_of_lt_of_le (B.ly.ut0 slot R.src p sb fi R.cap) (CrtA.of_args R hU).pa + obtain ⟨hn, -, hval⟩ := untyped.retype_size.okA ers + have hzl := UtArgs.zalA R.ty R.ext R.cap R.utv hU + obtain ⟨hdev, hal, hfi, hA0, hAw, hinj, hcan, -⟩ := id C.ar + have hS1 := C.stz + obtain ⟨hSA, -, hK1⟩ := id hS1 + obtain ⟨hS1', -, hV1, hUZ1, -⟩ := hSA + obtain ⟨G1, par1, hM1, -, -, hNl1, -, hTr1, hUi1⟩ := id hS1' + have hnA := nodeAt_aligned C.n0 (by rw [debug_val]; omega) (by rw [debug_val]) + have hnF := KsOffA.node hK1 C.core0 C.n0 (by rw [debug_val]; omega) + have hcd : ∀ j, j < win.val → ∃ z ∈ OffR p.val (p.val + 2 ^ sb.val) OS, CovBy (capA st s3 z) (djA dest off j) := + fun j hj => by + obtain ⟨z, hz, p', r', g', b', ez, hcv⟩ := C.zO j hj + exact ⟨z, hz, by rw [ez]; exact hcv⟩ + have stz := untyped.retype.stZ hA C.heap C.os C.stz a3 C.src C.cap nt us dest off win aligned false rfl C.hn C.hz + hdev hal hfi hA0 hAw hNew C.core0 C.n0 C.nc C.hd C.dO C.dn C.dc hinj hcan C.hus hcd + have fr := untyped.retype.fr hA C.heap C.os C.stz C.fr hHH' a3 C.src C.cap nt us dest off win aligned false rfl C.hn + C.hz hdev hal hfi hA0 hAw hHn C.core0 C.n0 C.nc C.hd C.dO C.dn C.dc hinj hcan C.hus + have cov := untyped.retype.covR (R := CovZ) CovZ.null (fun _ _ h => h.ut) hA C.heap C.os ⟨hM1, hTr1, hUi1⟩ hNl1 hV1 + hUZ1 C.cz a3 C.src C.cap nt us dest off win aligned false rfl C.hn C.hz hdev hal hfi hA0 hAw hnA C.nc hnF C.hd C.dO + C.dn C.dc hinj hcan C.hus (fun j hj => by + obtain ⟨z, hz, p', r', g', b', ez, hcv⟩ := C.zO j hj + exact ⟨z, hz, by rw [ez]; exact CovZ.cnode hcv⟩) + have ur := untyped.retype.ur hA C.heap C.os C.stz hR1 a3 C.src C.cap nt us dest off win aligned false rfl C.hn C.hz + hdev hal hfi hA0 hAw hNew hzl.1 hzl.2 (by rw [← hval]; exact untyped.retype_size.ge4 ers) C.core0 C.n0 C.nc C.hd + C.dO C.dn C.dc hinj hcan C.hus + have live : untyped.retype st ar s3 a3 slot false aligned nt us dest off win false ⦃ r => + (LiveNamed st H' Cs (OffR p.val (p.val + 2 ^ sb.val) OS) ia ie r.2.1 ∧ ValidReplies st H' r.2.1 ∧ + A64Layout st H' (OffR p.val (p.val + 2 ^ sb.val) OS) ia ie r.2.1) ∧ + ((BufInv st H' (OffR p.val (p.val + 2 ^ sb.val) OS) r.2.1 ∧ + CapInv st (IdP (L.idleW r.2.1) (L.mem r.2.1 (scA (L.idleW r.2.1)))) (OffR p.val (p.val + 2 ^ sb.val) OS) r.2.1 ∧ + (CapInv st (ScCapP H'.scs) (OffR p.val (p.val + 2 ^ sb.val) OS) r.2.1 ∧ + CtOff st (L.ksb r.2.1) (OffR p.val (p.val + 2 ^ sb.val) OS) r.2.1 ∧ + (FrOff st Cs (OffR p.val (p.val + 2 ^ sb.val) OS) (L.ksb r.2.1) r.2.1 ∧ + (EptSlot st H' (OffR p.val (p.val + 2 ^ sb.val) OS) r.2.1 ∧ IpcAl st H' r.2.1)))) ∧ + (nt.val = 1 → WinT st r.2.1 aligned (zA nt.val us) win.val) ∧ WinHi st r.2.1 aligned nt us win.val) ⦄ := by + refine WP.spec_mono (WP.spec_and (utEnd_resetA hA R B.ly e a a3 hU eres hHH' hEx hHn) + (untyped.retype.liveRWH hA C hb0 a3)) ?_ + rintro r ⟨E, -, -, WT, WH⟩ + exact UtEnd.live (I := fun H2 O2 s2' => + (BufInv st H2 O2 s2' ∧ CapInv st (IdP (L.idleW s2') (L.mem s2' (scA (L.idleW s2')))) O2 s2' ∧ + (CapInv st (ScCapP H2.scs) O2 s2' ∧ CtOff st (L.ksb s2') O2 s2' ∧ (FrOff st Cs O2 (L.ksb s2') s2' ∧ + (EptSlot st H2 O2 s2' ∧ IpcAl st H2 s2')))) ∧ + (nt.val = 1 → WinT st s2' aligned (zA nt.val us) win.val) ∧ WinHi st s2' aligned nt us win.val) R B.ln B.vr + B.ly + (fun U => ⟨⟨U.buf R.heap B0 R.cap, U.ci R B.ly B.ci (fun h => absurd h (by simp)), + ⟨U.sc R B.ly B.sc (fun h => absurd h (by simp)) hkps + (fun j hj h5 => ⟨us.val, hHn rfl j hj _ (by rw [h5]; simp [newKind])⟩), + U.ct R B.ly B.ct (UtArgs.us64 hU), ⟨U.fo R B.fo (fun _ _ _ x hx => by + have := (CrtA.of_args R hU).pa + have := (CrtA.of_args R hU).aw + rcases hx.2 with h | h <;> omega), ⟨U.ept R.heap B0 B.ep R.cap, + U.ipc R.heap R.bxi.choose_spec R.core0 B.ipc⟩⟩⟩⟩, fun n => WT n, WH⟩) hBT E + refine WP.spec_mono (WP.spec_and (WP.spec_and (WP.spec_and stz (WP.spec_and fr cov)) ur) live) ?_ + rintro ⟨r, s', a'⟩ ⟨⟨⟨⟨-, h1⟩, h2, -, h3⟩, h4⟩, ⟨h5, h6, h7⟩, h8, h9⟩ + exact ⟨⟨⟨h1, h2, h3, h4⟩, h5, h6, h7, h8.1.cn, h8.1.bh, h8.2.1, h8.2.2.1, h8.2.2.2.1, h8.2.2.2.2.1, + h8.2.2.2.2.2.1, h8.2.2.2.2.2.2⟩, h9⟩ + +end reset + +section frames +variable (D : DecPre st H.tcbs Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits U Ac s0 (.Untyped p sb fi false) extra) + (P : SysPlace st H Cs H.frame H.replies H.ntfns OS s0 extra slot) (B : SysL st H Cs OS ia ie s0) + (hcap : obj.Slot.cap st slot s0 = ok (.Untyped p sb fi false)) + (e : sched.set_thread_state st s0 (L.curW s0) sched.TS_RESTART = ok s2) + {a a3 : A} + (hU : UtArgs st s0 extra slot p sb fi false true aligned nt us dest off win) + (eres : untyped.reset_untyped st ar s2 a slot = ok (core.result.Result.Ok (), s3, a3)) +include D P B hcap e hU eres + +set_option maxHeartbeats 4000000 in +/-- The restart write, the reset and the creation's frame, at any word set off the objects the decode keeps. -/ +theorem untyped.retype.tabKRst (hA : P0.ArchTotal ar) (hvr : VrOK H.omega Sg Scs Z OS Vc' Ub' (L.ksb s0) Pt) : + untyped.retype st ar s3 a3 slot false aligned nt us dest off win false ⦃ r => + WritesIn st s0 r.2.1 (fun b => ¬ AsW (L.ksb s0) b) ⦄ := by + have R := UtDecP.of_live D B.sys P hcap B.ln B.vr B.ly + obtain ⟨Ac', hb⟩ := R.blk + obtain ⟨Q, Rq, q⟩ := hb.1 + have F : TabF st H Cs OS s0 := TabF.of hvr q B.ly.ksw R.core0 + have hX : ∀ OS1 : Set U64, (∀ x ∈ OS1, x ∈ OS) → ∀ x ∈ OS1, ∀ o : U64, o.val < 32 → o.val % 8 = 0 → + wa x o ∉ {b | AsW (L.ksb s0) b} := fun OS1 hs x hx o ho _ hT => + F.os _ hT ⟨x, hs x hx, by rw [wv (R.os.fit x (hs x hx)) ho]; omega, by rw [wv (R.os.fit x (hs x hx)) ho]; omega⟩ + have hKs : (L.ksb s0).val + 788736 ≤ p.val ∨ p.val + 2 ^ sb.val ≤ (L.ksb s0).val := + (R.stz.2.2 slot R.src p sb fi R.cap).2 + obtain ⟨-, w⟩ := restart_frame (OS := OS) R.os q e + have w2 : WritesIn st s0 s2 (fun b => ¬ AsW (L.ksb s0) b) := WritesIn.monoA w fun b _ hb' hT => by + rcases hb' with e' | e' + · exact F.tcb R.heap q.curT (K := layout.tcb.STATE) (by simp) hT e'.symm + · exact F.node rfl F.cur F.ncur (by simp) hT e'.symm + have hb0 : 0 < aligned.val := + lt_of_lt_of_le (B.ly.ut0 slot R.src p sb fi R.cap) (CrtA.of_args R hU).pa + obtain ⟨C, -, hw1⟩ := utCrtP_resetA hA R e a a3 hU eres + have w3 : WritesIn st s0 s3 (fun b => ¬ AsW (L.ksb s0) b) := + WritesIn.monoA (w2.trans hw1) fun b _ hb' hT => Or.elim hb' (· hT) + (F.rst w.1 w.2.1 hKs R.src (R.os.fit slot R.src) hT) + refine WP.spec_mono (untyped.retype.liveR hA (C.widen (hX _ fun x hx => hx.1)) hb0 a3) ?_ + rintro ⟨r, s', a'⟩ ⟨E, -⟩ + refine WritesIn.monoA (w3.trans E.fr) fun b _ hb' hT => ?_ + rcases hb' with h | h + · exact h hT + · refine F.notRFw R.heap (fun t ht => (Heap.drop_eq_some.1 ht).2) w3.1 (fun _ hT _ ⟨g1, g2⟩ => ?_) hT h + have h1 := (CrtA.of_args R hU).pa + have h2 := (CrtA.of_args R hU).aw + exact F.reg hKs hT ⟨by omega, by omega⟩ + +set_option maxHeartbeats 4000000 in +/-- A reset and its creation keep the placed VSpace words off the source's region. -/ +theorem untyped.retype.keepVsWordsRst (hA : P0.ArchTotal ar) + (vr : VrOK H.omega Sg Scs Z OS Vc' Ub' (L.ksb s0) Pt) {X : Set U64} + (pt : ∀ b ∈ X, VsR (L.ksb s0) Pt b) + (resetOff : ∀ b ∈ X, ¬ (p.val ≤ b.val ∧ b.val < p.val + 2 ^ sb.val)) : + untyped.retype st ar s3 a3 slot false aligned nt us dest off win false ⦃ r => + ∀ b ∈ X, L.mem r.2.1 b = L.mem s0 b ⦄ := by + have R := UtDecP.of_live D B.sys P hcap B.ln B.vr B.ly + obtain ⟨_, hb⟩ := R.blk + obtain ⟨_, _, q⟩ := hb.1 + have osOff : ∀ x ∈ OS, ∀ o : U64, o.val < 32 → wa x o ∉ X := by + intro x hx o ho hin + have value := wv (R.os.fit x hx) ho + exact vr.os _ (pt _ hin) ⟨x, hx, by rw [value]; omega, by rw [value]; omega⟩ + have outside : ∀ b ∈ X, OutsideObjs st H.tcbs Cs s0 b := + fun b hb => q.omega b (vr.om b (pt b hb)) + have tcb : ∀ {t k b : U64}, H t = some .tcb → k.val < 4096 → b ∈ X → wa t k ≠ b := + fun ht hk hb eq => out_ne_field (outside _ hb) ht (q.tcbs _ ht) hk (congrArg UScalar.val eq) + have node : ∀ {s1 : S} {c o b : U64}, L.ksb s1 = L.ksb s0 → c ∈ Cs → o.val < 4288 → b ∈ X → + L.nodeAt s1 c o ≠ b := by + intro s1 c o b hks hc ho hb eq + rw [nodeAt_of_ksb hks] at eq + exact out_ne_node (outside b hb) hc (q.nodes c hc) ho (congrArg UScalar.val eq) + have slotOff : ∀ b ∈ X, ¬ (b = slot ∨ b = wa slot 8#u64) := by + intro b hb + rintro (e' | e') + · rw [e'] at hb + have hfit := R.os.fit slot R.src + have hzero : (0#u64 : U64).val = 0 := rfl + have eq : wa slot 0#u64 = slot := UScalar.eq_of_val_eq (by + rw [wadd_val (by rw [hzero]; omega), hzero, Nat.add_zero]) + exact osOff slot R.src 0#u64 (by simp) (eq.symm ▸ hb) + · exact osOff slot R.src 8#u64 (by simp) (e' ▸ hb) + have w := (restart_frame R.os q e).2 + have restart : WritesIn st s0 s2 (fun b => b ∉ X) := w.mono fun b h hb => by + rcases h with eq | eq + · rw [stA_eq] at eq + exact tcb q.curT (by simp) hb eq.symm + · exact node rfl q.cur (by simp) hb eq.symm + obtain ⟨C, -, hw1⟩ := utCrtP_resetA hA R e a a3 hU eres + have skipReset : ∀ b ∈ X, ¬ RstW st s2 slot p sb b := by + intro b hb + rintro (h | h | h) + · exact resetOff b hb h + · exact slotOff b hb h + · rcases PPW.of w.1 w.2.1 h with h | h | h + · exact vr.ks b (pt b hb) _ (by simp) h.symm + · exact node rfl q.cur (by simp) hb h.symm + · exact node rfl q.cur (by simp) hb h.symm + have afterReset : WritesIn st s0 s3 (fun b => b ∉ X) := + (restart.trans (hw1.mono fun b h hb => skipReset b hb h)).mono fun _ h => h.elim id id + have arith := UtArgs.arithA R.ty R.ext R.cap R.utv hU + have offWindow : ∀ b ∈ X, WKA nt → + ¬ (aligned.val ≤ b.val ∧ b.val < aligned.val + win.val * 2 ^ (zA nt.val us).val) := + fun b hb _ ⟨l, u⟩ => resetOff b hb ⟨le_trans (CrtA.of_args R hU).pa l, lt_of_lt_of_le u arith.2.2.2.2.1⟩ + have positive : 0 < aligned.val := + lt_of_lt_of_le (B.ly.ut0 slot R.src p sb fi R.cap) (CrtA.of_args R hU).pa + refine WP.spec_mono (untyped.retype.liveR hA (C.widen (X := X) (fun x hx o ho _ => osOff x hx.1 o ho)) + positive a3) ?_ + rintro ⟨_, s', _⟩ ⟨E, _⟩ b hb + have whole : WritesIn st s0 s' (fun b => b ∉ X) := + (afterReset.trans E.fr).mono fun b h hb => by + rcases h with out | (⟨t, ht, k, hk, eq⟩ | ⟨_, out⟩) | ⟨wk, lo, hi⟩ | eq | ⟨t, ht, eq⟩ + · exact out hb + · exact tcb (Heap.drop_eq_some.1 ht).2 (by omega) hb eq.symm + · exact out (Set.mem_union_right _ hb) + · exact offWindow b hb wk ⟨lo, hi⟩ + · exact node afterReset.1 R.core0 (by simp) hb eq.symm + · exact tcb (Heap.drop_eq_some.1 ht).2 (by simp) hb eq.symm + exact whole.mem_eq (vr.al b (pt b hb)) (fun h => h hb) + +set_option maxHeartbeats 2000000 in +/-- Each null slot off the source's region that the creation names as a `HighVSpace` holds an initialized root. -/ +theorem untyped.retype.newHighInitRst (hA : P0.ArchTotal ar) {KLv : U64 → Nat} (K : KTab ar Kt KLv) : + untyped.retype st ar s3 a3 slot false aligned nt us dest off win false ⦃ r => + ∀ x ∈ OS, capA st s0 x = .Null → + (cdt.ensure_no_children st s0 slot = ok (.Ok ()) → x.val + 32 ≤ p.val ∨ p.val + 2 ^ sb.val ≤ x.val) → + ∀ t m asid, capA st r.2.1 x = .HighVSpace t m asid → S1aHighInit st Kt r.2.1 t ⦄ := by + have R := UtDecP.of_live D B.sys P hcap B.ln B.vr B.ly + have childless := untyped.retype.rstChildlessA D P B hcap e hU eres + obtain ⟨C, keep, -⟩ := utCrtP_resetA hA R e a a3 hU eres + have positive : 0 < aligned.val := + lt_of_lt_of_le (B.ly.ut0 slot R.src p sb fi R.cap) (CrtA.of_args R hU).pa + have full := (UtArgs.zalA R.ty R.ext R.cap R.utv hU).1 + refine WP.spec_mono (untyped.retype.newHighInit hA K C positive full a3) ?_ + intro r new x hx empty off t m asid held + have xs : x ≠ slot := by + rintro rfl + rw [R.cap] at empty + cases empty + have xo : x ∈ OffR p.val (p.val + 2 ^ sb.val) OS := ⟨hx, off childless⟩ + exact new x xo ((keep x xo xs).trans empty) t m asid held + +end frames + +section joint +variable (D : DecPre st H.tcbs Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits U Ac s0 (.Untyped p sb fi false) extra) + (P : SysPlace st H Cs H.frame H.replies H.ntfns OS s0 extra slot) (B : SysL st H Cs OS ia ie s0) + (hcap : obj.Slot.cap st slot s0 = ok (.Untyped p sb fi false)) + (e : sched.set_thread_state st s0 (L.curW s0) sched.TS_RESTART = ok s2) + {a a3 : A} + (hU : UtArgs st s0 extra slot p sb fi false true aligned nt us dest off win) + (eres : untyped.reset_untyped st ar s2 a slot = ok (core.result.Result.Ok (), s3, a3)) +include D P B hcap e hU eres + +set_option maxHeartbeats 4000000 in +/-- **The retype-level joint post after a reset**: `S1aRtTopK`'s `s1aTopRam` over the dropped heap and slots. The +creation end `S1aRtIn` has `H1` the dropped heap, `s1` the reset's exit and the hole the whole region. -/ +theorem untyped.retype.s1aTopRst (hA : P0.ArchTotal ar) (Y : SymL st H Cs s0) + {Pp Tb Hr Te : Set U64} {Lv Re bits : U64 → Nat} {n : U64} + (vr : VrOK H.omega H.frame H.scs H.scBits OS Vc' Ub' (L.ksb s0) Pt) (hPN : PoolNamed st OS s0) + (C : S1aArmCol st H Cs OS Vc Ub s0) (K : S1aCapCol st H OS Ub s0) (hal : ∀ b ∈ Ub, Aligned b) + (hVC : VcCap st Vc OS s0) (h : S1aR2 st H OS Pt Pp Tb Hr Te Kt Lv Re bits n s0) (sep : S1aKtUt st OS Kt s0) + {KLv : U64 → Nat} (kt : KTab ar Kt KLv) + (hNew : ∀ q k, H' q = some k → (H.drop (RegionD H p.val (p.val + 2 ^ sb.val))) q = some k ∨ + (nt.val ≠ 0 ∧ aligned.val ≤ q.val ∧ q.val + 2 ^ k.bits ≤ aligned.val + win.val * 2 ^ (zA nt.val us).val)) + (hHH' : ∀ q k, (H.drop (RegionD H p.val (p.val + 2 ^ sb.val))) q = some k → H' q = some k) + (hEx : ∀ q k, H' q = some k → (H.drop (RegionD H p.val (p.val + 2 ^ sb.val))) q = some k ∨ + (false = false ∧ ∃ j, j < win.val ∧ q = bjA aligned (zA nt.val us) j ∧ newKind nt.val us.val = some k)) + (hHn : false = false → + ∀ j, j < win.val → ∀ k, newKind nt.val us.val = some k → H' (bjA aligned (zA nt.val us) j) = some k) : + untyped.retype st ar s3 a3 slot false aligned nt us dest off win false ⦃ r => + ∃ (Vn Vc'' : Set U64), + S1aRtIn st H Cs OS U Ac s0 ia ie slot p sb fi false extra (H.drop (RegionD H p.val (p.val + 2 ^ sb.val))) H' + (OffR p.val (p.val + 2 ^ sb.val) OS) s3 r.2.1 p.val (p.val + 2 ^ sb.val) aligned nt us dest off win ∧ + ArmQIVAt st H Cs OS ia ie s0 H' (s1aRtSlots (OffR p.val (p.val + 2 ^ sb.val) OS) false nt us aligned win) Vn + r.2.1 ∧ TabKeep st s0 r.2.1 ∧ + Vc'' ⊆ Vc ∪ Vn ∧ VcCap st Vc'' (s1aRtSlots (OffR p.val (p.val + 2 ^ sb.val) OS) false nt us aligned win) r.2.1 ∧ + S1aArmCol st H' Cs (s1aRtSlots (OffR p.val (p.val + 2 ^ sb.val) OS) false nt us aligned win) Vc'' Ub r.2.1 ∧ + S1aCapCol st H' (s1aRtSlots (OffR p.val (p.val + 2 ^ sb.val) OS) false nt us aligned win) Ub r.2.1 ∧ + (∀ b, VsR (L.ksb s0) Pt b → S1aRetypeWordOwner st H OS Kt s0 b → L.mem r.2.1 b = L.mem s0 b) ∧ + (∀ asid raw, ActiveAsidRoot st Pp r.2.1 asid raw ↔ ActiveAsidRoot st Pp s0 asid raw) ∧ + (∀ tag asid sh base t, S1aPath st Pp n r.2.1 tag asid sh base t ↔ S1aPath st Pp n s0 tag asid sh base t) ∧ + (∀ x ∈ OS, capA st s0 x = .Null → + (cdt.ensure_no_children st s0 slot = ok (.Ok ()) → x.val + 32 ≤ p.val ∨ p.val + 2 ^ sb.val ≤ x.val) → + ∀ t m asid, capA st r.2.1 x = .HighVSpace t m asid → S1aHighInit st Kt r.2.1 t) ∧ + HrW st (HvRt st OS s0) s0 r.2.1 ∧ WinHi st r.2.1 aligned nt us win.val ⦄ := by + have R := UtDecP.of_live D B.sys P hcap B.ln B.vr B.ly + obtain ⟨_, hb⟩ := R.blk + obtain ⟨_, _, q⟩ := hb.1 + have source : capA st s0 slot = .Untyped p sb fi false := R.cap + have childless := untyped.retype.rstChildlessA D P B hcap e hU eres + have X : UtX H (H.drop (RegionD H p.val (p.val + 2 ^ sb.val))) p.val (p.val + 2 ^ sb.val) us := + UtX.drop (R.ur.rin R.src R.cap) (UtArgs.us64 hU) + -- the words of live objects and kernel tables + have owned : untyped.retype st ar s3 a3 slot false aligned nt us dest off win false ⦃ r => + ∀ b ∈ {b | VsR (L.ksb s0) Pt b ∧ S1aRetypeWordOwner st H OS Kt s0 b}, L.mem r.2.1 b = L.mem s0 b ⦄ := by + refine untyped.retype.keepVsWordsRst D P B hcap e hU eres hA vr + (X := {b | VsR (L.ksb s0) Pt b ∧ S1aRetypeWordOwner st H OS Kt s0 b}) (fun _ g => g.1) ?_ + intro b hb' + obtain ⟨t, k, obj, inside, kernel | held⟩ := hb'.2 + · obtain ⟨ht, rfl⟩ := kernel + have off := sep t ht slot P.src p sb fi false source + obtain ⟨lo, hi⟩ := inside + change b.val < t.val + 4096 at hi + rcases off with left | right <;> omega + · exact untypedWordOffNamed P B source childless obj inside held + -- the high roots' reserved words + have hiWords : untyped.retype st ar s3 a3 slot false aligned nt us dest off win false ⦃ r => + HrW st (HvRt st OS s0) s0 r.2.1 ⦄ := by + have hg := h.footprint + have hv : HvPt st Pt OS s0 := fun y hy t mp asid cap => + h.highFootprint ⟨t, h.highRoots ▸ ⟨y, hy, mp, asid, cap⟩, Or.inl rfl⟩ + let X : Set U64 := {b | ∃ t ∈ HvRt st OS s0, b = wa t 4088#u64} + have pt : ∀ b ∈ X, VsR (L.ksb s0) Pt b := by + rintro _ ⟨t, ht, rfl⟩ + exact Or.inl ⟨t, hv.rt ht, 4088#u64, by simp, by simp, rfl⟩ + have val : ∀ t ∈ HvRt st OS s0, (wa t 4088#u64).val = t.val + 4088 := by + intro t ht + have hf := hg.fit R.heap t (hv.rt ht) + exact wadd_val (by change t.val + 4088 < 2 ^ 64; omega) + have resetOff : ∀ b ∈ X, ¬ (p.val ≤ b.val ∧ b.val < p.val + 2 ^ sb.val) := by + rintro b ⟨t, ht, rfl⟩ bounds + have named := ht + obtain ⟨x, hx, m, asid, hc⟩ := named + have hoff := PtHG.keyOffNC P B R.src R.cap childless hg hx + (by rw [hc]; exact RngKeyA.hv t m asid) (by rw [hc, ptrA_hv]) (hv.rt ht) + rw [val t ht] at bounds + omega + exact WP.spec_mono (untyped.retype.keepVsWordsRst D P B hcap e hU eres hA vr pt resetOff) + fun _ k t ht => k _ ⟨t, ht, rfl⟩ + refine WP.spec_mono (WP.spec_and (untyped.retype.sysLRst D P B hcap e hU eres hNew hHH' hEx hHn hA) + (WP.spec_and (utEnd_resetA hA R B.ly e a a3 hU eres hHH' hEx hHn) + (WP.spec_and (untyped.retype.tabKRst D P B hcap e hU eres hA vr) + (WP.spec_and owned (WP.spec_and hiWords + (untyped.retype.newHighInitRst D P B hcap e hU eres hA kt)))))) ?_ + rintro ⟨r, s', a'⟩ ⟨⟨B', WT, WH⟩, E, writes, words, hiw, fresh⟩ + have hw : win.val = 0 ∨ ∃ reset, UtArgs st s0 extra slot p sb fi false reset aligned nt us dest off win := + Or.inr ⟨_, hU⟩ + have hp : (H.drop (RegionD H p.val (p.val + 2 ^ sb.val))) = H ∨ + (false = false ∧ (∃ v, cdt.ensure_no_children st s0 slot = ok (.Ok v)) ∧ p.val = p.val ∧ + p.val + 2 ^ sb.val = p.val + 2 ^ sb.val) := + Or.inr ⟨rfl, ⟨(), childless⟩, rfl, rfl⟩ + have rt : S1aRtIn st H Cs OS U Ac s0 ia ie slot p sb fi false extra (H.drop (RegionD H p.val (p.val + 2 ^ sb.val))) H' + (OffR p.val (p.val + 2 ^ sb.val) OS) s3 s' p.val (p.val + 2 ^ sb.val) aligned nt us dest off win := + ⟨D, P, B, hcap, E, X, hw, hp⟩ + have B'' : SysL st H' Cs (OffR p.val (p.val + 2 ^ sb.val) OS ∪ NewSl false nt us aligned win) ia ie s' := + B'.growN (E.freshN X B'.ly.ksw) + have hbR := (untyped.retype.rstExitA D P B hcap e hU eres hA).2 + have blk := UtEnd.blk R B.ly E X hw ⟨hbR, fun _ n => WT n⟩ + have tk : TabKeep st s0 s' := TabKeep.ofW vr writes fun _ g => g + have hR := UtEnd.rtArmI D P B Y hcap _ H' _ s3 s' _ _ aligned nt us dest off win E X hw hp + have hE := UtEnd.s1aEndV D P B hcap C K hal hVC _ H' _ s3 s' _ _ aligned nt us dest off win E X hw hp + obtain ⟨_, hQ⟩ := RtArmI.fin P.heap hPN C.case.pobj B'' blk hR tk + obtain ⟨Vn, Vc'', hVn, hVT, hsub, hC, hcol, hK⟩ := S1aEndV.fin P vr hPN C B'' writes hE + have words' : ∀ b, VsR (L.ksb s0) Pt b → S1aRetypeWordOwner st H OS Kt s0 b → L.mem s' b = L.mem s0 b := + fun b g1 g2 => words b ⟨g1, g2⟩ + have roots : ∀ asid raw, ActiveAsidRoot st Pp s' asid raw ↔ ActiveAsidRoot st Pp s0 asid raw := fun asid raw => + tk.activeRootIff fun t ht o width al => + let ⟨placed, owner⟩ := h.poolWordOwner P.heap ht width al + words' _ (Or.inl placed) owner + have paths : ∀ tag asid sh base t, + S1aPath st Pp n s' tag asid sh base t ↔ S1aPath st Pp n s0 tag asid sh base t := by + intro tag asid sh base t + refine S1aPath.iffOfWords roots ?_ ?_ + · intro asid raw entry tag + obtain ⟨placed, owner⟩ := h.highWordOwner P.heap entry tag + exact words' _ (Or.inl placed) owner + · intro tag asid sh base t path o width al + obtain ⟨x, hx, root, off', z, obj, _, bound, address, _, key, ptr, _⟩ := h.pathWordObject path width + exact words' _ (Or.inl (h.pathWord path width al)) + (by rw [address]; exact ⟨root, .other z, obj, inObjWord P.heap obj bound, Or.inr ⟨x, hx, key, ptr⟩⟩) + exact ⟨Vn, Vc'', rt, hQ.reVn hVT hVn, tk, hsub, hC, hcol, hK, words', roots, paths, fresh, hiw, WH⟩ + +end joint + +end Fl4ProofsA64.P1.W7.R.ST diff --git a/proofs/Fl4ProofsA64/P1/S1aStepFix.lean b/proofs/Fl4ProofsA64/P1/S1aStepFix.lean new file mode 100644 index 000000000..327c5bf46 --- /dev/null +++ b/proofs/Fl4ProofsA64/P1/S1aStepFix.lean @@ -0,0 +1,247 @@ +/- +P1 (aarch64, A64-P3 Stage 3, T3 G1, P1 half): **the S1a step over the heap-keeping arms at fixed witnesses, with `FrUb`**. + +`s1aStepR2All`'s post `S1aStepR2K` is existential in `H' OS' Vc' Ub' Pt'`. On `DecArmS1aEnt` (the arms that keep the +heap) the witnesses are the decode's own, so: + +* **`S1aStepFix st H Cs OS Vc Ub Pt Kt KLv ia ie n s'`**: `S1aStepR2K` at `H' = H`, `OS' = OS`, `Vc' = Vc`, `Pt' = Pt` + and `Ub' = Ub ∩ FrameWords OS s'` (the `S1aColU.of` witness; `Ub' ⊆ Ub`), **plus `FrUb st Ub' OS s'`** (F2). + `S1aStepFix.toK` projects to `S1aStepR2K`. +* **`FrUbP.closed`**: `FrUbP Ub` is a `CapSrcClosed` instance (uses the bounded `CapSrcClosed.sub`, commit + `03f497fe1`), so `ArmQIAt`'s cap-source clause carries `FrUb` through every arm (`ArmQIAt.frUb`); Assign keeps it + by `AsgPost.frUb`. +* **`syscall.decode_invocation.s1aStepFix`**: `s1aStepR2All`'s statement on `DecArmS1aEnt` with the deletion facts' frame + set `Uq := Ub` (J7's `Ub`). + +Named premises: those of `s1aStepR2All` minus `KTab`, `A64FrameMasks` and `hal` (used by the Untyped arms only): +[P] `P0.ArchTotal`, `MaxSpi`, `ArchPlat`; case fact [J] `hScC`. No new premise. +-/ +import Fl4ProofsA64.P1.S1aCEnt + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P1.W7.R.ST +open fl4_core_a64 Fl4ProofsA64.P0.Consts Fl4Proofs.P3 + +local notation "wa" => core.num.U64.wrapping_add + +variable {S : Type _} {st : machine.Store S} [L : LawfulStore st] {H : Heap} + {Cs OS Vc Ub U Ac Pt Pp Tb Hr Te Kt Uq : Set U64} {Lv Re bits KLv : U64 → Nat} {n : U64} {ia ie : Nat} + {A : Type _} {ar : machine.Arch A} {K : capability.Cap → Prop} {Ty : S → Prop} + +/-- **`FrUbP Ub` is a `CapSrcClosed` instance** (the subframe case by the bounded `sub`). -/ +theorem FrUbP.closed (Ub : Set U64) : CapSrcClosed (FrUbP Ub) where + null := FrUbP.null Ub + zombie := FrUbP.zombie Ub + ut := by + intro p b i e ptr sz r x1 x2 x3 x4 h + cases h + irq := by + intro i ptr sz r x1 x2 x3 x4 h + cases h + der := fun ha hf h => FrUbP.al ha hf h + sub := by + intro p p' sz r r' dv m e a v k h hp hk ptr size rights x1 x2 x3 x4 hd bits hb b al lo hi + cases hd + obtain ⟨bp, hbp⟩ : ∃ bp, capability.FrameSize.bits sz = ok bp := by cases sz <;> exact ⟨_, rfl⟩ + have eq : bits = 12#u64 := (okinj hb).symm + have hk' := hk bp hbp + subst eq + have h12 : (12#u64).val = 12 := rfl + rw [h12] at hi + exact h p sz r m e a v rfl bp hbp b al (by omega) (by omega) + remap := by + intro d c hm h p sz r x1 x2 x3 x4 hd bits hb + subst hd + rcases hm with ⟨_, _, _, _, m0, e0, a0, v0, _, _, _, _, hc, h1⟩ | ⟨_, _, _, _, _, _, _, -, h1⟩ | + ⟨_, _, _, _, _, -, h1⟩ | ⟨_, _, _, _, _, -, h1⟩ | ⟨_, _, _, _, _, _, _, _, -, h1⟩ <;> cases h1 + exact h _ _ _ m0 e0 a0 v0 hc bits hb + +/-- **`ArmQIAt` carries `FrUb`** (its cap-source clause at `FrUbP`). -/ +theorem ArmQIAt.frUb {Vq : Set U64} {s0 s : S} (h : ArmQIAt st H Cs OS Vq ia ie s0 s) (h0 : FrUb st Ub OS s0) : + FrUb st Ub OS s := + h.capInv (FrUbP.closed Ub) h0 + +/-- **ASIDPool Assign keeps `FrUb`** (its caps are kept or VSpace root caps). -/ +theorem AsgPost.frUb {s s' : S} (A : AsgPost st H OS s s') (h : FrUb st Ub OS s) : FrUb st Ub OS s' := by + intro y hy + rcases A.cap y hy with e | ⟨-, hk⟩ + · rw [e]; exact h y hy + · intro p sz r x1 x2 x3 x4 he + rw [he] at hk + rcases hk with ⟨_, _, _, h1⟩ | ⟨_, _, _, _, _, h1⟩ | ⟨_, _, _, h1⟩ <;> cases h1 + +/-- `FrUb` at the frame words inside a smaller set. -/ +theorem FrUb.inter_fw {s : S} (h : FrUb st Ub OS s) : FrUb st (Ub ∩ FrameWords st OS s) OS s := + fun y hy ptr size rights x1 x2 x3 x4 hc bits hb b al lo hi => + ⟨h y hy ptr size rights x1 x2 x3 x4 hc bits hb b al lo hi, al, + ⟨y, hy, ptr, size, rights, x1, x2, x3, x4, bits, hc, hb, lo, hi⟩⟩ + +variable (st) in +/-- **The S1a step post at fixed witnesses, with `FrUb`** (module docstring). -/ +def S1aStepFix (H : Heap) (Cs OS Vc Ub Pt Kt : Set U64) (KLv : U64 → Nat) (ia ie : Nat) (n : U64) (s' : S) : Prop := + ∃ Ub', (SysL st H Cs OS ia ie s' ∧ SymL st H Cs s' ∧ Ub' ⊆ Ub ∧ + VcCap st Vc OS s' ∧ S1aArmCol st H Cs OS Vc Ub' s' ∧ S1aCapCol st H OS Ub' s' ∧ + PtCap st Pt OS s' ∧ HvPt st Pt OS s' ∧ PtHG H Pt ∧ DevFrAl st OS s' ∧ HiCap511 st OS s' ∧ + EptPlaced st H OS Pt s' ∧ PtObjects st H OS s' ∧ HvObjects st H OS s') ∧ FrUb st Ub' OS s' ∧ + S1aKtUt st OS Kt s' ∧ S1aR2Ex st H OS Pt Kt n s' ∧ S1aHiK st OS Kt KLv s' + +/-- `S1aStepFix` projects to `S1aStepR2K` (witnesses `H`, `OS`, `Vc`, `Ub'`, `Pt`). -/ +theorem S1aStepFix.toK {s : S} (h : S1aStepFix st H Cs OS Vc Ub Pt Kt KLv ia ie n s) : + S1aStepR2K st Cs Ub Kt KLv ia ie n s := + let ⟨Ub', i, _, kt, r2, col⟩ := h + ⟨H, OS, Vc, Ub', Pt, i, kt, r2, col⟩ + +/-- `FrUb` of `S1aStepFix`. -/ +theorem S1aStepFix.frUb {s : S} (h : S1aStepFix st H Cs OS Vc Ub Pt Kt KLv ia ie n s) : + ∃ Ub', Ub' ⊆ Ub ∧ FrUb st Ub' OS s := + let ⟨Ub', i, f, _⟩ := h + ⟨Ub', i.2.2.1, f⟩ + +/-- **The post from its parts** (the witness `Ub ∩ FrameWords OS s'`, as `S1aColU.of`). -/ +theorem S1aStepFix.mk {s : S} (B : SysL st H Cs OS ia ie s) (Y : SymL st H Cs s) + (C : S1aArmCol st H Cs OS Vc Ub s) (fu : FrUb st Ub OS s) (vc : VcCap st Vc OS s) (pc : PtCap st Pt OS s) + (hv : HvPt st Pt OS s) (hg : PtHG H Pt) (df : DevFrAl st OS s) (hi : HiCap511 st OS s) + (ep : EptPlaced st H OS Pt s) (po : PtObjects st H OS s) (ho : HvObjects st H OS s) + (kt : S1aKtUt st OS Kt s) (r2 : S1aR2Ex st H OS Pt Kt n s) (col : S1aHiK st OS Kt KLv s) : + S1aStepFix st H Cs OS Vc Ub Pt Kt KLv ia ie n s := + ⟨Ub ∩ FrameWords st OS s, ⟨B, Y, Set.inter_subset_left, vc, C.sub Set.inter_subset_left, + ⟨fun b hb => FrameWords.ubFr s b hb.2, B.userOffFr, B.sys.fr⟩, pc, hv, hg, df, hi, ep, po, ho⟩, + fu.inter_fw, kt, r2, col⟩ + +/-- **A cap-keeping arm or step**: `S1aStepR2K.ofKeep'` at fixed witnesses, with `FrUb`. -/ +theorem S1aStepFix.ofKeep {s q : S} + (d : ArmQIAt st H Cs OS Vc ia ie s q ∧ TabKeepOrZero st s q ∧ ArmVsK st H OS Vc s q ∧ OSKeep st OS s q) + (vr : VrOK H.omega H.frame H.scs H.scBits OS Vc Ub (L.ksb s) Pt) (fu : FrUb st Ub OS s) + (vcc : VcCap st Vc OS s) (C : S1aArmCol st H Cs OS Vc Ub s) + (Df : DevFrAl st OS s) (R : S1aR2 st H OS Pt Pp Tb Hr Te Kt Lv Re bits n s) (sep : S1aKtUt st OS Kt s) + (KC : S1aHiK st OS Kt KLv s) : + S1aStepFix st H Cs OS Vc Ub Pt Kt KLv ia ie n q := by + obtain ⟨arm, tab, mem, slots⟩ := d + have vs : VsKeep st (L.ksb s) Pt s q := mem.2 Ub Pt vr fu + have R' : S1aR2 st H OS Pt Pp Tb Hr Te Kt Lv Re bits n q := + R.ofImage mem.1 vs (.ofSame R.capRefs.refs fun _ hy => Or.inl (slots.capA hy)) + have col : S1aHiK st OS Kt KLv q := KC.keep ⟨EdgeKeepOn.ofVsKeep R vs, arm.hiRoots R.highHeaders⟩ + exact S1aStepFix.mk arm.1 arm.2.1 (S1aArmCol.ofTZ tab C) (arm.frUb fu) (arm.2.2.2.1 vcc) (arm.ptCap R.pageTables) + (arm.hvPt R.hvPt) R.footprint (arm.devFr Df) R'.highHeaders (arm.eptPlaced R.eptRoots) + (arm.ptCap R.pageObjects) (arm.hvObjects R.highObjects) (sep.keep slots) R'.ex col + +/-- **The Reply arm** (two transfers): `S1aStepR2K.ofXfer2` at fixed witnesses, with `FrUb`. -/ +theorem S1aStepFix.ofXfer2 {s q : S} + (d : ArmQIAt st H Cs OS Vc ia ie s q ∧ TabKeepOrZero st s q ∧ ArmVsK st H OS Vc s q ∧ XferSlots2 st H OS Vc s q) + (vr : VrOK H.omega H.frame H.scs H.scBits OS Vc Ub (L.ksb s) Pt) (fu : FrUb st Ub OS s) + (vcc : VcCap st Vc OS s) (C : S1aArmCol st H Cs OS Vc Ub s) + (Df : DevFrAl st OS s) (R : S1aR2 st H OS Pt Pp Tb Hr Te Kt Lv Re bits n s) (sep : S1aKtUt st OS Kt s) + (KC : S1aHiK st OS Kt KLv s) : + S1aStepFix st H Cs OS Vc Ub Pt Kt KLv ia ie n q := by + obtain ⟨arm, tab, mem, m, x1, v1, x2, v2⟩ := d + have vs : VsKeep st (L.ksb s) Pt s q := mem.2 Ub Pt vr fu + have Rm : S1aR2 st H OS Pt Pp Tb Hr Te Kt Lv Re bits n m := + R.ofImage v1.1.1 (v1.1.2 Ub Pt vr fu) (x1.s1aImage R.capRefs.refs) + have vr' : VrOK H.omega H.frame H.scs H.scBits OS Vc Ub (L.ksb m) Pt := by rw [v1.1.1]; exact vr + have R' : S1aR2 st H OS Pt Pp Tb Hr Te Kt Lv Re bits n q := + Rm.ofImage v2.1.1 (v2.1.2 Ub Pt vr' (v1.2 Ub fu)) (x2.s1aImage Rm.capRefs.refs) + have col : S1aHiK st OS Kt KLv q := KC.keep ⟨EdgeKeepOn.ofVsKeep R vs, arm.hiRoots R.highHeaders⟩ + exact S1aStepFix.mk arm.1 arm.2.1 (S1aArmCol.ofTZ tab C) (arm.frUb fu) (arm.2.2.2.1 vcc) (arm.ptCap R.pageTables) + (arm.hvPt R.hvPt) R.footprint (arm.devFr Df) R'.highHeaders (arm.eptPlaced R.eptRoots) + (arm.ptCap R.pageObjects) (arm.hvObjects R.highObjects) (x2.ktUt (x1.ktUt sep)) R'.ex col + +set_option maxHeartbeats 4000000 in +/-- **The S1a step at fixed witnesses over `DecArmS1aEnt`** (module docstring). -/ +theorem syscall.decode_invocation.s1aStepFix (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + {s : S} {cap : capability.Cap} {extra : Aeneas.Std.Array obj.Slot 3#usize} {slot : U64} + (I : DelIn st ar H Cs OS Vc Ub Pt K Ty s) (hPI : PoolIdx st OS s) + (D : DecPre st H.tcbs Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits U Ac s cap extra) + (P : SysPlace st H Cs H.frame H.replies H.ntfns OS s extra slot) (B : SysL st H Cs OS ia ie s) + (Y : SymL st H Cs s) (hcap : obj.Slot.cap st slot s = ok cap) (hks : HeapKsOff H (L.ksb s)) + (hPN : PoolNamed st OS s) (C : S1aArmCol st H Cs OS Vc Ub s) (a : A) + {lbl : U64} (hc : DecArmS1aEnt cap lbl) + (hIq : ∀ irq, cap = .IrqHandler irq → + (lbl = fl4_abi.label.IRQClearIRQHandler ∨ lbl = fl4_abi.label.IRQSetIRQHandler) → irq.val < 5376) + (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (mcp : ∀ y ∈ H.tcbs, (L.mem s (wa y 3440#u64)).val < 256) (cs : L.mem s (scA (L.curW s)) ≠ 0#u64) + (fh : ∀ t, cap = .Thread t → NullOrEp H (capA st s (slotIdx t layout.tcb.SLOT_FAULT_HANDLER))) + (hScC : ∀ cpu, cap = .SchedControl cpu → cpu ∈ Cs ∧ + ∀ x ∈ extra.val, x ≠ 0#u64 → ∀ p sz, capA st s x = .SchedContext p sz → sz.val = H.scBits p) + (R : S1aR2 st H OS Pt Pp Tb Hr Te Kt Lv Re bits hPl.ipa s) (sep : S1aKtUt st OS Kt s) + (Df : DevFrAl st OS s) (KC : S1aHiK st OS Kt KLv s) (len : U64) (blk call don fp : Bool) (buf : U64) : + syscall.decode_invocation st ar s a lbl len slot cap blk call don fp buf extra ⦃ _ s' _ => + S1aStepFix st H Cs OS Vc Ub Pt Kt KLv ia ie hPl.ipa s' ⦄ := by + have keep : ∀ {m : Result ((core.result.Result Unit error.Exc) × S × A)}, + m ⦃ _ s' _ => ArmQIAt st H Cs OS Vc ia ie s s' ∧ TabKeepOrZero st s s' ∧ ArmVsK st H OS Vc s s' ∧ + OSKeep st OS s s' ⦄ → m ⦃ _ s' _ => S1aStepFix st H Cs OS Vc Ub Pt Kt KLv ia ie hPl.ipa s' ⦄ := fun t => + WP.spec_mono t fun r d => by + obtain ⟨_, _, _⟩ := r + exact S1aStepFix.ofKeep d I.del.vr I.fu I.vcc C Df R sep KC + have hSA : ScApart H.scs H.scBits OS := I.del.sca + have QH : DecArmQH cap lbl → syscall.decode_invocation st ar s a lbl len slot cap blk call don fp buf extra ⦃ _ s' _ => + S1aStepFix st H Cs OS Vc Ub Pt Kt KLv ia ie hPl.ipa s' ⦄ := fun hq => by + have dsc := R.assignDescK I.del.heap KC + replace R := R.relabel I.del.heap KC + have W := HiDelTyR.ofR2 R + replace R := R.activePools + have J := I.forHiDelR hPl W + have z := W.finKZ (hPl := hPl) I + have Cp : PtCap st Pt OS s ∧ PtHG H Pt := ⟨R.pageTables, R.footprint⟩ + obtain ⟨pools, tables, level, _, stage2, depth, width, hp, ht, he, ty, no3, hi, ty2, place2, heap2⟩ := + R.core.hvs W.pools W.tables W.granules W.stage2 + have dec := S1aDecHyps.of mcp cs fh + ⟨pools, tables, level, Gran stage2 width, s2gRe stage2 depth, hp, ht, he, ty, no3, hi, + S2DecTy.ofTeH P.heap ty2 place2 heap2⟩ ⟨R.pageTables, R.footprint, R.highHeaders⟩ P B hks hcap Df + refine WP.spec_mono (WP.spec_and (WP.spec_and + (syscall.decode_invocation.armQH_s1a (Vc := Vc) (Ub := Ub) hA hM hPl J z hPI D P B Y hcap hks + a hq hIq hSC dec len blk call don fp buf) + (syscall.decode_invocation.armQH_r2R hA hM hPl I W hPI D P B Y hcap hks R sep Df dec a hq hIq hSC len blk call + don fp buf)) + (syscall.decode_invocation.armQH_edge hA hM hPl J z W hPI D P B Y hcap hks R sep Df dec a hq hIq hSC len blk + call don fp buf)) fun r ⟨⟨d, kt⟩, ed⟩ => ?_ + obtain ⟨_, _, _⟩ := r + obtain ⟨_, _, _, _, _, _, _, R'⟩ := kt.2 + exact S1aStepFix.mk d.1.1 d.1.2.1 (d.2.2.1 C).1 (d.1.frUb I.fu) (d.1.2.2.2.1 I.vcc) (ArmQIAt.ptCap d.1 Cp.1) + (d.1.hvPt R.hvPt) Cp.2 (d.1.devFr Df) R'.highHeaders (d.1.eptPlaced R.eptRoots) (d.1.ptCap R.pageObjects) + (d.1.hvObjects R.highObjects) kt.1 kt.2 (KC.keep ⟨ed, d.1.hiRoots R.highHeaders⟩) + rcases hc with hq | hq | ⟨pp, ab, rfl, hl⟩ | hc + · exact QH hq + · exact QH (Or.inl (hq.imp id Or.inl)) + · subst lbl + have dsc := R.assignDescK I.del.heap KC + replace R := R.relabel I.del.heap KC + have ptKs := hks.ptAsWG R.footprint B.ly.ksw (R.footprint.fit I.del.heap) + refine WP.spec_mono (WP.spec_and (WP.spec_and + (syscall.decode_invocation.assignLW D P B Y C.case.po a rfl len blk call don fp buf) + (syscall.decode_invocation.assignExactR2D R dsc I.del.vr D P B hcap hPN ptKs a len blk call don fp buf)) + (syscall.decode_invocation.assign_kt R I.del.vr D P hcap a len blk call don fp buf)) + fun r ⟨⟨d, ⟨_, _, _, _, _, R'⟩⟩, col⟩ => ?_ + obtain ⟨_, s', _⟩ := r + have tk : TabKeep st s s' := ⟨d.2.2.ksb, fun j => by rw [d.2.2.ksb]; exact d.2.2.tab I.del.vr j⟩ + exact S1aStepFix.mk d.1.1 d.1.2 (S1aArmCol.ofTab tk C) (d.2.2.frUb I.fu) (d.2.2.vcCap I.vcc) R'.pageTables + R'.hvPt R'.footprint (d.2.2.devFr Df) R'.highHeaders R'.eptRoots R'.pageObjects R'.highObjects + (d.2.2.ktUt sep) R'.ex (KC.keep col) + · rcases hc with hr | ⟨t, rfl, hl⟩ | ⟨p, b, rfl, hl⟩ | ⟨t, rfl, hl | hl | hl | hl | hl | hl⟩ | ⟨hf, hl⟩ | + ⟨p, b, rfl, hl | hl⟩ | ⟨cpu, rfl⟩ | ⟨p, g, rfl⟩ + · refine WP.spec_mono (syscall.decode_invocation.rejected_eq hA hcap P.src B a hr len blk call don fp buf) + fun r e => ?_ + obtain ⟨_, s', _⟩ := r + have e' : s' = s := e + subst e' + exact S1aStepFix.ofKeep ⟨ArmQIAt.refl B Y D.bxi.blk, TabKeepOrZero.refl s', ArmVsK.refl s', OSKeep.rfl' s'⟩ + I.del.vr I.fu I.vcc C Df R sep KC + · exact keep (syscall.decode_invocation.unknownTcb_armS hA I D P B Y a hl len blk call don fp buf) + · exact keep (syscall.decode_invocation.unknownSc_armS hA I D P B Y a hl len blk call don fp buf) + · exact keep (syscall.decode_invocation.writeFpu_armS hA I D P B Y a hl len blk call don fp buf) + · exact keep (syscall.decode_invocation.setTimer_armS hA I D P B Y hks hSA hcap a hl len blk call don fp buf) + · exact keep (syscall.decode_invocation.bindN_armS hA I D P B Y hks hSA hcap a hl len blk call don fp buf) + · exact keep (syscall.decode_invocation.resume_armS hA I D P B Y hks hSA hcap a hl len blk call don fp buf) + · exact keep (syscall.decode_invocation.writeRegs_armS hA I D P B Y hks hSA hcap a hl len blk call don fp buf) + · exact keep (syscall.decode_invocation.copyRegs_armS hA I D P B Y hks hcap a hl len blk call don fp buf) + · exact keep (syscall.decode_invocation.cbs_armS I D P B Y hks hSA hcap hf a hl len blk call don fp buf) + · exact keep (syscall.decode_invocation.scBind_armS hA I D P B Y hks hSA hcap a hl len blk call don fp buf) + · exact keep (syscall.decode_invocation.scYieldTo_armS hA I D P B Y hks hSA hcap a hl len blk call don fp buf) + · obtain ⟨hcpu, hsz⟩ := hScC cpu rfl + exact keep (syscall.decode_invocation.schedControl_armS hA I D P B Y hks hcpu hsz a lbl len blk call don fp buf) + · exact WP.spec_mono (syscall.decode_invocation.reply_armS hA I D P B Y hks a lbl len blk call don fp buf) + fun r d => by + obtain ⟨_, _, _⟩ := r + exact S1aStepFix.ofXfer2 d I.del.vr I.fu I.vcc C Df R sep KC + +end Fl4ProofsA64.P1.W7.R.ST diff --git a/proofs/Fl4ProofsA64/P1/S1aStepPool.lean b/proofs/Fl4ProofsA64/P1/S1aStepPool.lean new file mode 100644 index 000000000..416edf5f8 --- /dev/null +++ b/proofs/Fl4ProofsA64/P1/S1aStepPool.lean @@ -0,0 +1,100 @@ +/- +P1 (aarch64, A64-P3 Stage 3, T3 step 2): **`PoolNamed` across the heap-keeping decode arms** (`DecArmS1aEnt`). + +`S1aStepFix` / `S1aStepEnt` do not export `PoolNamed`, which A64-P3's `J7A` carries at the post state. This sibling of +`syscall.decode_invocation.fixHeap` (same hypotheses, plus the entry's `PoolNamed`) reads it off each arm's +`ArmQIAt` (or `AsgPost`). No existing statement changed. Named premises: those of `fixHeap`. +-/ +import Fl4ProofsA64.P1.S1aCEnt + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P1.W7.R.ST +open fl4_core_a64 Fl4ProofsA64.P0.Consts Fl4Proofs.P3 + +local notation "wa" => core.num.U64.wrapping_add + +variable {S : Type _} {st : machine.Store S} [L : LawfulStore st] {H : Heap} + {Cs OS Vc Ub U Ac Pt Pp Tb Hr Te Kt Uq : Set U64} {Lv Re bits KLv : U64 → Nat} {n : U64} {ia ie : Nat} + {A : Type _} {ar : machine.Arch A} {K : capability.Cap → Prop} {Ty : S → Prop} + +set_option maxHeartbeats 4000000 in +/-- **On the heap-keeping arms `PoolNamed` is kept** (module docstring). -/ +theorem syscall.decode_invocation.poolNamedFix (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + {s : S} {cap : capability.Cap} {extra : Aeneas.Std.Array obj.Slot 3#usize} {slot : U64} + (I : DelIn st ar H Cs OS Vc Uq Pt K Ty s) (hPI : PoolIdx st OS s) + (D : DecPre st H.tcbs Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits U Ac s cap extra) + (P : SysPlace st H Cs H.frame H.replies H.ntfns OS s extra slot) (B : SysL st H Cs OS ia ie s) + (Y : SymL st H Cs s) (hcap : obj.Slot.cap st slot s = ok cap) (hks : HeapKsOff H (L.ksb s)) + (C : S1aArmCol st H Cs OS Vc Ub s) (a : A) + {lbl : U64} (hc : DecArmS1aEnt cap lbl) + (hIq : ∀ irq, cap = .IrqHandler irq → + (lbl = fl4_abi.label.IRQClearIRQHandler ∨ lbl = fl4_abi.label.IRQSetIRQHandler) → irq.val < 5376) + (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (mcp : ∀ y ∈ H.tcbs, (L.mem s (wa y 3440#u64)).val < 256) (cs : L.mem s (scA (L.curW s)) ≠ 0#u64) + (fh : ∀ t, cap = .Thread t → NullOrEp H (capA st s (slotIdx t layout.tcb.SLOT_FAULT_HANDLER))) + (hScC : ∀ cpu, cap = .SchedControl cpu → cpu ∈ Cs ∧ + ∀ x ∈ extra.val, x ≠ 0#u64 → ∀ p sz, capA st s x = .SchedContext p sz → sz.val = H.scBits p) + (R : S1aR2 st H OS Pt Pp Tb Hr Te Kt Lv Re bits hPl.ipa s) + (Df : DevFrAl st OS s) (KC : S1aHiK st OS Kt KLv s) (hPN : PoolNamed st OS s) (len : U64) (blk call don fp : Bool) (buf : U64) : + syscall.decode_invocation st ar s a lbl len slot cap blk call don fp buf extra ⦃ _ s' _ => + PoolNamed st OS s' ⦄ := by + have hSA : ScApart H.scs H.scBits OS := I.del.sca + have keepF : ∀ {m : Result ((core.result.Result Unit error.Exc) × S × A)}, + m ⦃ _ s' _ => ArmQIAt st H Cs OS Vc ia ie s s' ∧ TabKeepOrZero st s s' ∧ ArmVsK st H OS Vc s s' ∧ + OSKeep st OS s s' ⦄ → + m ⦃ _ s' _ => PoolNamed st OS s' ⦄ := fun t => + WP.spec_mono t fun r d => by + obtain ⟨_, _, _⟩ := r + exact d.1.2.2.2.2.2.1 hPN + have QH : DecArmQH cap lbl → syscall.decode_invocation st ar s a lbl len slot cap blk call don fp buf extra ⦃ _ s' _ => + PoolNamed st OS s' ⦄ := fun hq => by + replace R := R.relabel I.del.heap KC + have W := HiDelTyR.ofR2 R + replace R := R.activePools + have J := I.forHiDelR hPl W + have z := W.finKZ (hPl := hPl) I + obtain ⟨pools, tables, level, _, stage2, depth, width, hp, ht, he, ty, no3, hi, ty2, place2, heap2⟩ := + R.core.hvs W.pools W.tables W.granules W.stage2 + have dec := S1aDecHyps.of mcp cs fh + ⟨pools, tables, level, Gran stage2 width, s2gRe stage2 depth, hp, ht, he, ty, no3, hi, + S2DecTy.ofTeH P.heap ty2 place2 heap2⟩ ⟨R.pageTables, R.footprint, R.highHeaders⟩ P B hks hcap Df + exact WP.spec_mono (syscall.decode_invocation.armQH_s1a (Vc := Vc) (Ub := Ub) hA hM hPl J z hPI D P B Y hcap hks + a hq hIq hSC dec len blk call don fp buf) fun r d => by + obtain ⟨_, _, _⟩ := r + exact d.1.2.2.2.2.2.1 hPN + rcases hc with hq | hc | ⟨pp, ab, rfl, hl⟩ | hc + · exact QH hq + · exact QH (Or.inl (hc.imp id Or.inl)) + · subst lbl + exact WP.spec_mono (syscall.decode_invocation.assignLW D P B Y C.case.po a rfl len blk call don fp buf) + fun r d => by + obtain ⟨_, _, _⟩ := r + exact d.2.2.poolNamed I.del.vr hPN + · rcases hc with hr | ⟨t, rfl, hl⟩ | ⟨p, b, rfl, hl⟩ | ⟨t, rfl, hl | hl | hl | hl | hl | hl⟩ | ⟨hf, hl⟩ | + ⟨p, b, rfl, hl | hl⟩ | ⟨cpu, rfl⟩ | ⟨p, g, rfl⟩ + · refine WP.spec_mono (syscall.decode_invocation.rejected_eq hA hcap P.src B a hr len blk call don fp buf) + fun r e => ?_ + obtain ⟨_, s', _⟩ := r + have e' : s' = s := e + subst e' + exact hPN + · exact keepF (syscall.decode_invocation.unknownTcb_armS hA I D P B Y a hl len blk call don fp buf) + · exact keepF (syscall.decode_invocation.unknownSc_armS hA I D P B Y a hl len blk call don fp buf) + · exact keepF (syscall.decode_invocation.writeFpu_armS hA I D P B Y a hl len blk call don fp buf) + · exact keepF (syscall.decode_invocation.setTimer_armS hA I D P B Y hks hSA hcap a hl len blk call don fp buf) + · exact keepF (syscall.decode_invocation.bindN_armS hA I D P B Y hks hSA hcap a hl len blk call don fp buf) + · exact keepF (syscall.decode_invocation.resume_armS hA I D P B Y hks hSA hcap a hl len blk call don fp buf) + · exact keepF (syscall.decode_invocation.writeRegs_armS hA I D P B Y hks hSA hcap a hl len blk call don fp buf) + · exact keepF (syscall.decode_invocation.copyRegs_armS hA I D P B Y hks hcap a hl len blk call don fp buf) + · exact keepF (syscall.decode_invocation.cbs_armS I D P B Y hks hSA hcap hf a hl len blk call don fp buf) + · exact keepF (syscall.decode_invocation.scBind_armS hA I D P B Y hks hSA hcap a hl len blk call don fp buf) + · exact keepF (syscall.decode_invocation.scYieldTo_armS hA I D P B Y hks hSA hcap a hl len blk call don fp buf) + · obtain ⟨hcpu, hsz⟩ := hScC cpu rfl + exact keepF (syscall.decode_invocation.schedControl_armS hA I D P B Y hks hcpu hsz a lbl len blk call don fp buf) + · exact WP.spec_mono (syscall.decode_invocation.reply_armS hA I D P B Y hks a lbl len blk call don fp buf) + fun r d => by + obtain ⟨_, _, _⟩ := r + exact d.1.2.2.2.2.2.1 hPN + +end Fl4ProofsA64.P1.W7.R.ST diff --git a/proofs/Fl4ProofsA64/P1/S1aVsUnmB.lean b/proofs/Fl4ProofsA64/P1/S1aVsUnmB.lean index 436a1385f..3cd519874 100644 --- a/proofs/Fl4ProofsA64/P1/S1aVsUnmB.lean +++ b/proofs/Fl4ProofsA64/P1/S1aVsUnmB.lean @@ -950,6 +950,154 @@ theorem w1K_s1aR2 (Re bits : U64 → Nat) (n : U64) : exact (and3z edge).elim +/-! ## R2 at the plain `w1` heap (`Kt = ∅`; the Stage-3 record's `w1` witness) -/ + +/-- `w1K_s1aR2Core` at the plain `w1` heap: no kernel table. -/ +theorem w1_s1aR2Core (Re bits : U64 → Nat) (n : U64) : + S1aR2Core bst (w1H kb ib nc) OS1 Set.univ w1Tb ∅ ∅ ∅ w1Lv Re bits n S1 := by + refine ⟨w1_vsLk P Re bits, w1_ptFndAll P _ Re bits (fun _ _ => rfl) n, w1_rootAgreement P, + (w1_column P Set.univ).1.2.2.2.2.1.toR, ?_, + fun _ h => (Set.notMem_empty _ h).elim⟩ + refine ⟨fun pool hp o ho h8 hn => ?_, fun _ h => (Set.notMem_empty _ h).elim, + fun _ h => (Set.notMem_empty _ h).elim⟩ + rw [w1_active_root P hp ho hn] + exact Or.inl ⟨rfl, Set.notMem_empty _⟩ + +set_option maxHeartbeats 4000000 in +private theorem w1_pathBackingPlain (n : U64) (Re bits : U64 → Nat) : + S1aPathBacking bst (w1H kb ib nc) OS1 Set.univ w1Tb ∅ + w1Lv Re bits n S1 where + tables tag a sh va t path := by + obtain ⟨rfl, rfl, h⟩ := w1_path P path + rcases h with ⟨rfl, rfl, rfl⟩ | ⟨rfl, rfl, rfl⟩ | ⟨rfl, rfl, rfl⟩ | ⟨rfl, rfl, rfl⟩ + · exact ⟨Or.inl vVS, by decide⟩ + · exact ⟨Or.inr (Or.inl vL1), by decide⟩ + · exact ⟨Or.inr (Or.inr (Or.inl vL2)), by decide⟩ + · exact ⟨Or.inr (Or.inr (Or.inr vL3)), by decide⟩ + children tag a sh va t path child := by + obtain ⟨rfl, rfl, h⟩ := w1_path P path + rcases h with ⟨rfl, rfl, rfl⟩ | ⟨rfl, rfl, rfl⟩ | ⟨rfl, rfl, rfl⟩ | ⟨rfl, rfl, rfl⟩ + · simp [s1aTopShift] at child + · obtain ⟨member, cap⟩ := w1_slotCap P (i := 16 + nc + 0) (by have := P.w0.nc128; omega) + exact ⟨_, member, 0#u64, cap.trans (capOf_tail 0), rfl⟩ + · obtain ⟨member, cap⟩ := w1_slotCap P (i := 16 + nc + 1) (by have := P.w0.nc128; omega) + exact ⟨_, member, 0#u64, cap.trans (capOf_tail 1), rfl⟩ + · obtain ⟨member, cap⟩ := w1_slotCap P (i := 16 + nc + 2) (by have := P.w0.nc128; omega) + exact ⟨_, member, bwv 4194304, cap.trans (capOf_tail 2), rfl⟩ + leaves tag a sh vaBase t path o width al range valid := by + obtain ⟨rfl, rfl, h⟩ := w1_path P path + rcases h with ⟨rfl, rfl, rfl⟩ | ⟨rfl, rfl, rfl⟩ | ⟨rfl, rfl, rfl⟩ | ⟨rfl, rfl, rfl⟩ + · have ho : o.val < 4096 := by simp [s1aTableWidth, s1aTopShift] at width; omega + rw [w1_desc0 P o ho] at valid + split at valid + · exact ((by unfold s1aLeafValid; decide : ¬ s1aLeafValid (bwv (tabE L1b)) 39) valid).elim + · exact ((by unfold s1aLeafValid; decide : ¬ s1aLeafValid 0#u64 39) valid).elim + · have ho : o.val < 4096 := by simpa [s1aTableWidth, s1aTopShift] using width + rw [w1_desc1 P o ho] at valid + split at valid + · exact ((by unfold s1aLeafValid; decide : ¬ s1aLeafValid (bwv (tabE L2b)) 30) valid).elim + · exact ((by unfold s1aLeafValid; decide : ¬ s1aLeafValid 0#u64 30) valid).elim + · have ho : o.val < 4096 := by simpa [s1aTableWidth, s1aTopShift] using width + rw [w1_desc2 P o ho] at valid + split at valid + · exact ((by unfold s1aLeafValid; decide : ¬ s1aLeafValid (bwv (tabE L3b)) 21) valid).elim + · exact ((by unfold s1aLeafValid; decide : ¬ s1aLeafValid 0#u64 21) valid).elim + · have ho : o.val < 4096 := by simpa [s1aTableWidth, s1aTopShift] using width + have word := w1_leafWord (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) P ho + have small : o.val < 280 := by + by_contra long + have edge : 280 ≤ o.val := Nat.le_of_not_gt long + have empty : l3W o.val = 0#u64 := by + unfold l3W + rw [if_neg (by omega), if_neg (by omega), if_neg (by omega), if_neg (by omega)] + rw [word, empty] at valid + exact (by unfold s1aLeafValid; decide : ¬ s1aLeafValid 0#u64 12) valid + let j := o.val / 8 + have val : o.val = 8 * j := by dsimp [j]; omega + have bound : j < 35 := by omega + have equal : bL.mem S1 (wa (bwv L3b) o) = l3W (8 * j) := by rw [word, val] + simp only [equal] + obtain ⟨x, hx, cap, -⟩ := + w1_frameLeaf (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) P j bound + have addr : s1aCapAddress 0#u64 n (bwv (4194304 + j * 4096)) = + 4194304 + o.val / 8 * 2 ^ 12 := by + unfold s1aCapAddress + rw [if_neg (by decide), wv (by omega : 4194304 + j * 4096 < 2 ^ 64)] + rfl + refine ⟨x, hx, .Small, 3#u64, false, _, rfl, cap, addr, Or.inr ⟨_, 12, ?_, le_rfl, le_rfl⟩⟩ + have old := w1_frameOK P x hx _ .Small _ true false _ _ cap 12#u64 (by rfl) + exact old + +set_option maxRecDepth 20000 in +set_option maxHeartbeats 4000000 in +/-- **R2 and the kernel-table separation at the plain `w1` heap**, over `w1Pt`. -/ +theorem w1_s1aR2Plain (Re bits : U64 → Nat) (n : U64) : + S1aR2 bst (w1H kb ib nc) OS1 w1Pt Set.univ w1Tb ∅ ∅ ∅ w1Lv Re bits n S1 ∧ + S1aKtUt bst OS1 ∅ S1 := by + refine ⟨?_, fun k hk => (Set.notMem_empty k hk).elim⟩ + have highAbsent : ∀ x ∈ OS1, ∀ p m a, capA bst S1 x ≠ .HighVSpace p m a := + fun _ hx p m a eq => (w1_ptCapK P hx).2 p m a eq + have highEmpty : HvRt bst OS1 S1 = ∅ := by + apply Set.eq_empty_iff_forall_notMem.mpr + rintro p ⟨x, hx, m, a, eq⟩ + exact highAbsent x hx p m a eq + have poolPlace : bwv PLb ∈ w1Pt := Or.inr (Or.inl vPL) + have poolExact : w1H kb ib nc (bwv PLb) = some (.other 12) := + (w1_pool (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) P).obj + have tbPt : w1Tb ⊆ w1Pt := by + rintro p (h | h | h | h) + · exact Or.inl h + · exact Or.inr (Or.inr (Or.inl h)) + · exact Or.inr (Or.inr (Or.inr (Or.inl h))) + · exact Or.inr (Or.inr (Or.inr (Or.inr h))) + refine ⟨w1_s1aR2Core P Re bits n, ⟨fun p hp => ?_, tbPt, + ?_, HrOK.empty, + ⟨by simp, by simp, by simp⟩⟩, ⟨?_, ?_, ?_, ?_⟩, ⟨?_, ?_, ?_, ?_⟩, w1_pathBackingPlain P n Re bits, + ?_, (w1_s1aPtCol (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) P).ph, + w1_ptObjects (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) P, ?_, + w1_eptPlaced (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) P w1Pt, fun _ hx p m a cap => (highAbsent _ hx p m a cap).elim, HiCap511.ofNoHv highAbsent, by simp, + highEmpty.symm, ?_, ?_, ?_, w1_rootSlots P ∅ n⟩ + · rwa [w1_active_pool P hp] + · rintro t ⟨p, hp, _⟩ + exact hp.elim + · intro i bound p word nz + obtain ⟨rfl, rfl⟩ := w1_asidIndex P bound nz word + obtain ⟨member, cap⟩ := w1_slotCap P (i := 6) (by decide) + exact ⟨trivial, poolPlace, poolExact, _, member, _, cap.trans (capOf_lo (by decide)), rfl⟩ + · intro x hx p base eq + obtain ⟨rfl, rfl⟩ := w1_poolExact P hx eq + refine ⟨0#u64, by decide, rfl, by decide, ?_⟩ + change bL.mem S1 (wa kb (AtOff 0#u64)) = bwv PLb + rw [w1_asid_word P 0#u64 (by decide), ite_eq_left (show (0#u64 : U64).val = 0 from rfl)] + · intro i j p hi hj nz left right + exact (w1_asidIndex P hi nz left).1.trans (w1_asidIndex P hj nz right).1.symm + · intro raw entry + exact (by decide : 0#u64 ≠ 1#u64) (w1_entryExact P entry).1 + · intro x hx y hy key kx ky + have left := w1_tableRef P hx kx + have right := w1_tableRef P hy ky + rcases left with ⟨kl, a⟩ | ⟨kl, a⟩ <;> rcases right with ⟨kr, b⟩ | ⟨kr, b⟩ + · exact a.trans b.symm + · rcases kl with rfl | rfl | rfl <;> exact ((by decide : _ ≠ _) kr).elim + · rcases kr with rfl | rfl | rfl <;> exact ((by decide : _ ≠ _) kl).elim + · exact a.trans b.symm + · intro x hx y hy k kx ky unm + rcases w1_tableRef P hx kx with ⟨_, eq⟩ | ⟨_, eq⟩ <;> rw [unm] at eq <;> cases eq + · intro x hx p a eq + exact (Bool.false_ne_true (w1_nativeExact P hx eq).2.1).elim + · intro x hx p a eq + exact (highAbsent x hx p false a eq).elim + · intro x hx p m a v cap + exact (w1_ptCapK P hx).1 p m a v cap + · intro x hx p m a cap + obtain ⟨rfl, _, _⟩ := w1_nativeExact P hx cap + exact ⟨w1Tb_h (Or.inl vVS), Or.inl vVS⟩ + · rintro t ⟨p, hp, _⟩ + exact hp.elim + · exact ⟨Set.empty_subset _, fun t ht => (Set.notMem_empty t ht).elim⟩ + · exact fun t ht => (Set.notMem_empty t ht).elim + + end w1 end diff --git a/proofs/Fl4ProofsA64/P3/A64ArchSwA.lean b/proofs/Fl4ProofsA64/P3/A64ArchSwA.lean new file mode 100644 index 000000000..15d59e742 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/A64ArchSwA.lean @@ -0,0 +1,119 @@ +/- +P3 (aarch64, A64-P3 non-vacuity witness 2, line archsw, part 1): **the root log of the model arch and `execIdle`.** + +`ArchSwFA` (`P3/ScArchSwA`) asks the arch for a per-core view of the hardware roots (`roots`), a relation `rootWrites` +saying that a step wrote an ordered list of root writes, and two refinement facts about FL4's own `set_kernel_vm_root` / +`set_vm_root`. This module gives `roots` / `rootWrites` for the model arch `witArA` (`P3/A64Wit2ArchA`), whose state +logs the raw arguments of `set_vspace`, `set_high_vspace` and `set_stage2_vspace`: + +* decoders (`decAsid`, `decRoot`, `decHigh`, `decS2`): a raw `(pointer, asid)` pair is the spec's root + `⟨specPtr pointer, asid⟩` (`asid = 0` is `none`); a zero pointer is no high window, a zero ASID no Stage-2 root; +* `witView a`: the Stage-1 / high-window / Stage-2 view of core 0; `witRoots a c` is that view for core 0 and a constant for + every other core (the model has one core: `c ≠ 0` is only constrained by `rootFrame`'s frame clause); +* `witRootWrites`: `rootWrites` is defined as `rootFrame`'s conclusion, so `rootFrame` is immediate; +* `skvr_res`: what `set_kernel_vm_root` does to the model, exactly (high window cleared, Stage-1 root = the kernel root + word, ASID 0); +* `witExecIdle`: `ArchSwFA.execIdle` at core 0, given [J] `hk`: the kernel root word names the spec's global VSpace + (`specPtr (L.mem s (krA (st := st) s)) = σ.archState.armUsGlobalVspace`; F4 puts this fact in the record, until then it is a + parameter, not a premise of any top). + +Named premises: [J] `hk` (F4). New module; no existing statement changed. +-/ +import Fl4ProofsA64.P3.A64Wit2ArchA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Wit2 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P1.W7.R.ST.BootWit Fl4ProofsA64.P3 + Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section +open Classical + +/-! ## The view -/ + +/-- A raw ASID word as the spec's: `0` is none. -/ +def decAsid (i : U64) : Option Asid := if i = 0#u64 then none else some (asidOfA i) + +/-- A raw `(root pointer, asid)` pair as the spec's hardware root. -/ +def decRoot (r i : U64) : HwRootA := ⟨specPtr r, decAsid i⟩ + +/-- A raw high-window pair: a zero pointer or ASID is no window. -/ +def decHigh (p i : U64) : Option (ObjRef × Asid) := if p = 0#u64 then none else (decAsid i).map fun x => (specPtr p, x) + +/-- A raw Stage-2 pair: a zero ASID is no root. -/ +def decS2 (r i : U64) : Option HwRootA := if i = 0#u64 then none else some (decRoot r i) + +/-- The view of core 0. -/ +def witView (a : WA) : RootViewA := ⟨decRoot a.s1.1 a.s1.2, decHigh a.hi.1 a.hi.2, decS2 a.s2.1 a.s2.2⟩ + +/-- The constant view of every other core. -/ +def witView0 : RootViewA := ⟨⟨0, none⟩, none, none⟩ + +/-- `ArchSwFA.roots` of the model. -/ +def witRoots (a : WA) (c : Nat) : RootViewA := if c = 0 then witView a else witView0 + +/-- `ArchSwFA.rootWrites` of the model: the frame conclusion of `rootFrame`. -/ +def witRootWrites (a a' : WA) (c : Nat) (W : List RootWriteA) : Prop := + witRoots a' c = W.foldl RootViewA.write (witRoots a c) ∧ ∀ d, d ≠ c → witRoots a' d = witRoots a d + +theorem witRootFrame : ∀ a a' c W, witRootWrites a a' c W → + witRoots a' c = W.foldl RootViewA.write (witRoots a c) ∧ ∀ d, d ≠ c → witRoots a' d = witRoots a d := + fun _ _ _ _ h => h + +/-- Writes that leave core 0's view alone keep every core's. -/ +theorem witRoots_ne {a a' : WA} {d : Nat} (hd : d ≠ 0) : witRoots a' d = witRoots a d := by + simp [witRoots, hd] + +theorem witRoots_zero (a : WA) : witRoots a 0 = witView a := by simp [witRoots] + +/-- A step that moves core 0's view as the writes say. -/ +theorem witRootWrites_of {a a' : WA} {W : List RootWriteA} (h : witView a' = W.foldl RootViewA.write (witView a)) : + witRootWrites a a' 0 W := ⟨by rw [witRoots_zero, witRoots_zero]; exact h, fun _ hd => by simp [witRoots, hd]⟩ + +/-! ## The model's operations on the log -/ + +theorem ar1_setHigh (a : WA) (r i : U64) : AR1.set_high_vspace a r i = ok { a with hi := (r, i) } := rfl + +theorem ar1_setVspace (a : WA) (r i : U64) : + AR1.set_vspace a r i = ok (if r = a.s1.1 ∧ i = a.s1.2 then a else { a with s1 := (r, i), hi := (0#u64, 0#u64) }) := rfl + +theorem ar1_setStage2 (a : WA) (r i : U64) : AR1.set_stage2_vspace a r i = ok { a with s2 := (r, i) } := rfl + +/-- `set_vspace` after a cleared high window. -/ +theorem ar1_setVspace_hi0 (a : WA) (r i : U64) : + AR1.set_vspace { a with hi := (0#u64, 0#u64) } r i = ok { a with hi := (0#u64, 0#u64), s1 := (r, i) } := by + rw [ar1_setVspace] + by_cases h : r = a.s1.1 ∧ i = a.s1.2 + · rw [if_pos h] + obtain ⟨ms, dl, dlO, ⟨r0, i0⟩, hi, s2, fpu, vcpu⟩ := a + obtain ⟨rfl, rfl⟩ := h + rfl + · rw [if_neg h] + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **What `set_kernel_vm_root` does to the model** (module docstring). -/ +theorem skvr_res (s : S) (a : WA) : + vspace.set_kernel_vm_root st AR1 s a = + ok (s, { a with hi := (0#u64, 0#u64), s1 := (L.mem s (krA (st := st) s), 0#u64) }) := by + unfold vspace.set_kernel_vm_root vspace.kernel_root obj.ks_get + simp only [L.ks_base_eq, L.read_eq, bind_ok, lift] + rw [ar1_setHigh, bind_ok, ar1_setVspace_hi0] + simp only [bind_ok] + rfl + +/-- **`ArchSwFA.execIdle` at core 0**, given the kernel-root tie `hk` (module docstring). -/ +theorem witExecIdle {σ : State Unit} {s : S} {a : WA} + (hk : specPtr (L.mem s (krA (st := st) s)) = σ.archState.armUsGlobalVspace) : + vspace.set_kernel_vm_root st AR1 s a ⦃ r => r.1 = s ∧ witRootWrites a r.2 0 (kernelRootWritesA σ) ⦄ := by + rw [skvr_res] + refine (WP.spec_ok _).2 ⟨rfl, witRootWrites_of ?_⟩ + rw [kernelRoot_viewA] + simp [witView, decRoot, decAsid, decHigh, decS2, globalRootA, hk] + +end + +end + +end Fl4ProofsA64.P3.Wit2 diff --git a/proofs/Fl4ProofsA64/P3/A64ArchSwB.lean b/proofs/Fl4ProofsA64/P3/A64ArchSwB.lean new file mode 100644 index 000000000..7ec7c162b --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/A64ArchSwB.lean @@ -0,0 +1,157 @@ +/- +P3 (aarch64, A64-P3 non-vacuity witness 2, line archsw, part 2): **`set_vm_root` on the model arch, arm by arm.** + +What FL4's `vspace.set_vm_root` does to the model's root log (`P3/A64ArchSwA`), from the code alone +(`P1/VmRootRd` names its pieces: `set_vm_root_split`, `vmHead`, `vmTail`, `s2Arm`), and the facts of the joint record +`JAtZAX` that tie the words it reads to the spec: + +* code level: `s2Arm_nonEpt` (a non-`Ept` VTABLE cap clears Stage 2), `vmHead_nonRoot` (a cap that is not a mapped `Pml4` + is the kernel root), `vmTail_notRun` (the tail is a no-op off `TS_RUNNING_VM`), `svr_bound` (a bound VCPU: kernel root, + then Stage 2), `svr_global` (no bound VCPU, a non-root VTABLE cap, not running: the kernel root); +* record level: `J_vtcap` (the VTABLE slot's cap is related to the spec's `vtCapA`), `J_vcpu` (the bound-VCPU word is zero + iff the spec's `vcpuOfA` is `none`), `J_state` (the STATE word is the spec's `tsCodeA`), `tsCodeA_eq8`; +* spec level: `nonRoot_vmRoot`, `nonEpt_s2Root` (a related cap that is no mapped `Pml4` / no `Ept` gives the global root / + no Stage-2 root). + +Named premises: none new (the kernel-root tie `hk` of `P3/A64ArchSwA` stays a parameter). New module. +-/ +import Fl4ProofsA64.P3.A64ArchSwA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Wit2 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P1.W7.R.ST.BootWit Fl4ProofsA64.P3 + Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section +open Classical + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-! ## Code level -/ + +/-- A VTABLE cap that is no `Ept` clears Stage 2. -/ +theorem s2Arm_nonEpt (s : S) (a : WA) (c : capability.Cap) (h : ∀ p i b asid x, c ≠ .Ept p i b asid x) : + s2Arm (st := st) AR1 s a c = ok (s, { a with s2 := (0#u64, 0#u64) }) := by + cases c <;> first | (simp only [s2Arm, ar1_setStage2, bind_ok]; done) | exact absurd rfl (h _ _ _ _ _) + +/-- A VTABLE cap that is no mapped `Pml4` installs the kernel root. -/ +theorem vmHead_nonRoot (s : S) (a : WA) (t : U64) (c : capability.Cap) (h : ∀ p asid, c ≠ .Pml4 p true asid) : + vmHead (st := st) AR1 s a t c = vspace.set_kernel_vm_root st AR1 s a := by + cases c <;> first | rfl | (rename_i p b asid; cases b <;> first | rfl | exact absurd rfl (h _ _)) + +theorem ts_running_eq : sched.TS_RUNNING_VM = 8#u64 := by unfold sched.TS_RUNNING_VM; rfl + +theorem tcb_state_eq (s : S) (t : U64) : obj.Tcb.state st t s = ok (L.mem s (stA t)) := by + unfold obj.Tcb.state stA + simp only [lift, bind_ok, L.read_eq] + +/-- Off `TS_RUNNING_VM` the tail of `set_vm_root` does nothing. -/ +theorem vmTail_notRun (s : S) (a : WA) (t : U64) (h : L.mem s (stA t) ≠ 8#u64) : + vmTail (st := st) AR1 s a t = ok (s, a) := by + unfold vmTail + rw [tcb_state_eq, bind_ok, if_neg (by rw [ts_running_eq]; exact h)] + +/-- **A bound VCPU**: kernel root, then Stage 2 (a non-`Ept` VTABLE cap clears it). -/ +theorem svr_bound (s : S) (a : WA) (t : U64) (hb : L.mem s (vcA t) ≠ 0#u64) + (hne : ∀ p i b asid x, capA st s (vtA t) ≠ .Ept p i b asid x) : + vspace.set_vm_root st AR1 s a t = + ok (s, { a with hi := (0#u64, 0#u64), s1 := (L.mem s (krA (st := st) s), 0#u64), s2 := (0#u64, 0#u64) }) := by + rw [set_vm_root_split, eq_of_spec_fd (vcpu.bound.spec (L := L) s t)] + simp only [bind_ok] + rw [if_pos (by simpa [vcA] using hb), skvr_res] + simp only [bind_ok] + show vspace.set_stage2_root st AR1 s _ t = _ + rw [set_stage2_root_unf (st := st) AR1, capA_eq, bind_ok, s2Arm_nonEpt _ _ _ hne] + +/-- **No bound VCPU, a non-root VTABLE cap, not running**: the kernel root. -/ +theorem svr_global (s : S) (a : WA) (t : U64) (hb : L.mem s (vcA t) = 0#u64) + (hnr : ∀ p asid, capA st s (vtA t) ≠ .Pml4 p true asid) (hrun : L.mem s (stA t) ≠ 8#u64) : + vspace.set_vm_root st AR1 s a t = + ok (s, { a with hi := (0#u64, 0#u64), s1 := (L.mem s (krA (st := st) s), 0#u64) }) := by + rw [set_vm_root_split, eq_of_spec_fd (vcpu.bound.spec (L := L) s t)] + simp only [bind_ok] + rw [if_neg (by unfold vcA at hb; simp [hb]), capA_eq, bind_ok, vmHead_nonRoot _ _ _ _ hnr, skvr_res] + simp only [bind_ok] + exact vmTail_notRun s _ t hrun + +/-! ## Record level -/ + +variable {AR : WA → HwRootA → Prop} {Z : XSetsA} {s : S} {a : WA} {σ : State Unit} {m : McState} + +/-- The VTABLE slot's cap, related to the spec's. -/ +theorem J_vtcap (J : JAtZAX (L := L) AR Z s a σ m) {t : U64} (ht : t ∈ Z.T) : + ∃ ac, vtCapA σ (specPtr t) = some ac ∧ CapRelA (capA st s (vtA t)) ac := by + obtain ⟨x, hk, hs⟩ := J.1.st.tcb t ((J.1.thT t).2 ht) + obtain ⟨ac, e, hrel⟩ := hs 1 (by unfold tcbSlotsA; omega) + have h1 : layout.tcb.SLOT_VTABLE.val = 1 := by unfold layout.tcb.SLOT_VTABLE; rfl + have ev : vtA t = slotAddrA (specPtr t) (BitVec.ofNat 64 1) := by + have h2 := okinj ((slot_vt t).symm.trans (tcb_slot_val t layout.tcb.SLOT_VTABLE)) + rw [h2, slotIdx_addrA, h1] + refine ⟨ac, ?_, ?_⟩ + · unfold vtCapA; rw [hk]; exact e + · rw [ev]; exact hrel + +/-- The bound-VCPU word is zero iff the spec thread has no VCPU. -/ +theorem J_vcpu (J : JAtZAX (L := L) AR Z s a σ m) {t : U64} (ht : t ∈ Z.T) : + vcpuOfA σ (specPtr t) = none ↔ L.mem s (vcA t) = 0#u64 := by + obtain ⟨x, hk, hv, -⟩ := J.2.1.tcb t ht + unfold vcpuOfA; rw [hk] + show x.tcbArch.tcbVcpu = none ↔ _ + rw [hv] + unfold optRefA vcA + split_ifs with h <;> simp [h] + +theorem tsCodeA_eq8 (x : ThreadState) : tsCodeA x = 8 ↔ x = .ext .runningVM := by + cases x with + | ext e => cases e; simp [tsCodeA] + | _ => simp [tsCodeA] + +/-- The STATE word is the spec's state code: `TS_RUNNING_VM` iff the spec thread is running a guest. -/ +theorem J_state (J : JAtZAX (L := L) AR Z s a σ m) {t : U64} (ht : t ∈ Z.T) : + runningVmA σ (specPtr t) ↔ L.mem s (stA t) = 8#u64 := by + obtain ⟨x, hk, hv, -⟩ := J.1.sch.tcb t ht + unfold runningVmA + constructor + · rintro ⟨y, hy, hys⟩ + rw [hk] at hy + obtain rfl : x = y := by cases hy; rfl + apply UScalar.eq_of_val_eq + rw [hv, hys]; rfl + · intro h + refine ⟨x, hk, (tsCodeA_eq8 _).1 ?_⟩ + rw [← hv, h]; rfl + +/-! ## Spec level -/ + +/-- A cap that is no mapped `Pml4` gives the global root. -/ +theorem nonRoot_vmRoot {ac : Cap} {c : capability.Cap} (h : CapEqA c ac) (hnr : ∀ p asid, c ≠ .Pml4 p true asid) + (σ' : State Unit) (t : ObjRef) (hv : vtCapA σ' t = some ac) : + vmRootA σ' t = ⟨σ'.archState.armUsGlobalVspace, none⟩ := by + unfold vmRootA; rw [hv] + rcases h.eq_or_pt with e | ⟨p', asid', va', e⟩ + · subst e + cases c with + | Pml4 p b asid => cases b; rfl; exact absurd rfl (hnr _ _) + | Frame p sz r dev m ept asid va => cases ept <;> rfl + | _ => rfl + · subst e; rfl + +/-- A cap that is no `Ept` gives no Stage-2 root. -/ +theorem nonEpt_s2Root {ac : Cap} {c : capability.Cap} (h : CapEqA c ac) (hne : ∀ p i b asid x, c ≠ .Ept p i b asid x) + (σ' : State Unit) (t : ObjRef) (hv : vtCapA σ' t = some ac) : + vmS2RootA σ' t = none := by + unfold vmS2RootA; rw [hv] + rcases h.eq_or_pt with e | ⟨p', asid', va', e⟩ + · subst e + cases c with + | Ept p i b asid x => exact absurd rfl (hne _ _ _ _ _) + | Frame p sz r dev m ept asid va => cases ept <;> rfl + | _ => rfl + · subst e; rfl + +end + +end + +end Fl4ProofsA64.P3.Wit2 diff --git a/proofs/Fl4ProofsA64/P3/A64ArchSwC.lean b/proofs/Fl4ProofsA64/P3/A64ArchSwC.lean new file mode 100644 index 000000000..9a4c2d4ea --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/A64ArchSwC.lean @@ -0,0 +1,93 @@ +/- +P3 (aarch64, A64-P3 non-vacuity witness 2, line archsw, part 3): **`ArchSwFA.execThread` at core 0 for the easy arms.** + +`set_vm_root t` of a thread `t ∈ Z.T` against the spec's `threadRootWritesA σ (specPtr t)`, from `P3/A64ArchSwB`: + +* `execThread_bound`: a bound VCPU, a VTABLE cap that is no `Ept`: the writes are the kernel root and no Stage-2 root; +* `execThread_global`: no bound VCPU, a VTABLE cap that is no mapped `Pml4`, not running a guest: the kernel root. +* `native_global_view`: the spec's write list in that case folds to the global root. + +Not here (CP2): a mapped `Pml4` cap (`find_vspace_for_asid`, `high_root_of`), an `Ept` cap (`ept::find`), a thread running +a guest (`set_stage2_root` of the guest). + +Named premises: [J] `hk` (the kernel-root tie, F4; a parameter). New module. +-/ +import Fl4ProofsA64.P3.A64ArchSwB +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Wit2 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P1.W7.R.ST.BootWit Fl4ProofsA64.P3 + Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section +open Classical + +/-- The spec's write list of a thread with no VCPU, the global root and no guest folds to the global root. -/ +theorem native_global_view {σ : State Unit} {t : ObjRef} (h : vcpuOfA σ t = none) + (hr : vmRootA σ t = ⟨σ.archState.armUsGlobalVspace, none⟩) (hnr : ¬ runningVmA σ t) (before : RootViewA) : + (threadRootWritesA σ t).foldl RootViewA.write before = + ⟨⟨σ.archState.armUsGlobalVspace, none⟩, none, before.stage2⟩ := by + simp [threadRootWritesA, h, hr, hnr, RootViewA.write] + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {AR : WA → HwRootA → Prop} {Z : XSetsA} {s : S} {a : WA} {σ : State Unit} {m : McState} {t : U64} + +/-- **`execThread` at core 0, a bound VCPU, no `Ept` VTABLE cap.** -/ +theorem execThread_bound (J : JAtZAX (L := L) AR Z s a σ m) (ht : t ∈ Z.T) (hb : vcpuOfA σ (specPtr t) ≠ none) + (hne : ∀ p i b asid x, capA st s (vtA t) ≠ .Ept p i b asid x) + (hk : specPtr (L.mem s (krA (st := st) s)) = σ.archState.armUsGlobalVspace) : + vspace.set_vm_root st AR1 s a t ⦃ r => r.1 = s ∧ witRootWrites a r.2 0 (threadRootWritesA σ (specPtr t)) ⦄ := by + have hb' : L.mem s (vcA t) ≠ 0#u64 := fun h => hb ((J_vcpu J ht).2 h) + rw [svr_bound s a t hb' hne] + refine (WP.spec_ok _).2 ⟨rfl, witRootWrites_of ?_⟩ + obtain ⟨v, hv⟩ : ∃ v, vcpuOfA σ (specPtr t) = some v := Option.ne_none_iff_exists'.1 hb + rw [boundRoot_viewA hv] + obtain ⟨ac, hvt, hce⟩ := J_vtcap J ht + rw [nonEpt_s2Root hce.2 hne σ _ hvt] + simp [witView, decRoot, decAsid, decHigh, decS2, globalRootA, hk] + +/-- **`execThread` at core 0, no bound VCPU, a VTABLE cap that is no mapped `Pml4`, not running a guest.** -/ +theorem execThread_global (J : JAtZAX (L := L) AR Z s a σ m) (ht : t ∈ Z.T) (hb : vcpuOfA σ (specPtr t) = none) + (hnr : ∀ p asid, capA st s (vtA t) ≠ .Pml4 p true asid) (hrun : ¬ runningVmA σ (specPtr t)) + (hk : specPtr (L.mem s (krA (st := st) s)) = σ.archState.armUsGlobalVspace) : + vspace.set_vm_root st AR1 s a t ⦃ r => r.1 = s ∧ witRootWrites a r.2 0 (threadRootWritesA σ (specPtr t)) ⦄ := by + have hb' : L.mem s (vcA t) = 0#u64 := (J_vcpu J ht).1 hb + have hrun' : L.mem s (stA t) ≠ 8#u64 := fun h => hrun ((J_state J ht).2 h) + rw [svr_global s a t hb' hnr hrun'] + refine (WP.spec_ok _).2 ⟨rfl, witRootWrites_of ?_⟩ + obtain ⟨ac, hvt, hce⟩ := J_vtcap J ht + rw [native_global_view hb (nonRoot_vmRoot hce.2 hnr σ _ hvt) hrun] + simp [witView, decRoot, decAsid, decHigh, decS2, globalRootA, hk] + +/-! ## The `ArchSwFA` record of the model -/ + +/-- **`ArchSwFA` for the model arch**, from the two refinement facts (`execIdle` / `execThread`, at every core as the +structure states them): the logging `roots` / `rootWrites` / `rootFrame` of `P3/A64ArchSwA`, the FPU / VCPU views of the +arch state, and `returnStep := False` (the model never reports a return, so `returnFrame` is vacuous). -/ +def witArchSw + (hIdle : ∀ (s : S) (a : WA) (c : Nat) (σ : State Unit) (Z : XSetsA) (m : McState) (AR : WA → HwRootA → Prop), + JAtZAX (L := L) AR Z s a σ m → (L.cpu s).val = c → IdleVtA σ → + specPtr (L.mem s (krA (L := L) s)) = σ.archState.armUsGlobalVspace → (L.cpu s).val = 0 → + vspace.set_kernel_vm_root st AR1 s a ⦃ r => r.1 = s ∧ witRootWrites a r.2 c (kernelRootWritesA σ) ⦄) + (hThread : ∀ (s : S) (a : WA) (c : Nat) (σ : State Unit) (t : U64) (Z : XSetsA) (m : McState) + (AR : WA → HwRootA → Prop), JAtZAX (L := L) AR Z s a σ m → (L.cpu s).val = c → t ∈ Z.T → VsRootOKA σ → + (∀ y ∈ Z.Y, y ≠ 0#u64) → specPtr (L.mem s (krA (L := L) s)) = σ.archState.armUsGlobalVspace → + RunLinkA (L := L) Z s t → (L.cpu s).val = 0 → + vspace.set_vm_root st AR1 s a t ⦃ r => r.1 = s ∧ witRootWrites a r.2 c (threadRootWritesA σ (specPtr t)) ⦄) : + ArchSwFA st AR1 where + roots := witRoots + rootWrites := witRootWrites + rootFrame := witRootFrame + execIdle := hIdle + execThread := hThread + fpu := fun a c => a.fpu c + vcpu := fun a c => a.vcpu c + returnStep := fun _ _ _ _ _ => False + returnFrame := fun _ _ _ _ _ _ h => h.elim + +end + +end + +end Fl4ProofsA64.P3.Wit2 diff --git a/proofs/Fl4ProofsA64/P3/A64ArchSwD.lean b/proofs/Fl4ProofsA64/P3/A64ArchSwD.lean new file mode 100644 index 000000000..efca1a000 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/A64ArchSwD.lean @@ -0,0 +1,152 @@ +/- +P3 (aarch64, A64-P3 non-vacuity witness 2, line archsw, part 4): **`find_high_asid` and `ept::find` against l4v's +`vspace_for_asid`**, the twins of `find_asid_findA` (`P3/VsFindA`) for the tags `ROOT_HIGH` and `ROOT_S2`. + +* `ent_kind`: a nonzero pool entry under `VsRelA` names a root whose spec kind is its tag's (Stage 1, Stage 2, high); +* `HighFindRelA` / `S2FindRelA` and `find_high_asidA` / `ept_findA`: an `Ok w` is l4v's `vspace_for_asid` and a root of + the right kind in the right set; an `Err` is l4v's `None` or a root of another kind. + +Named premises: none (`hX` is `Stage2CoreA.obj`, as in `find_asid_findA`). New module. +-/ +import Fl4ProofsA64.P3.A64ArchSwC +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Wit2 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3 Fl4SpecA64 + +noncomputable section + +theorem rs2 : vspace.ROOT_HIGH = 2#u64 := by unfold vspace.ROOT_HIGH; rfl +theorem rss : vspace.ROOT_S2 = 1#u64 := by unfold vspace.ROOT_S2; rfl + +theorem tag_eq_iff (w k : U64) (hk : k.val < 4) : w &&& 3#u64 = k ↔ tagA w = k.val := by + constructor + · intro h; unfold tagA; rw [← and3_valA, h] + · intro h; apply UScalar.eq_of_val_eq; rw [and3_valA]; unfold tagA at h; exact h + +/-- What the entry of a root says of its kind. -/ +def RootKindOfTag (σ : State Unit) (X Y : Set U64) (w : U64) : Prop := + (tagA w = 0 ∧ rootKindAt σ (specPtr (untagA w)) = .stage1) ∨ + (tagA w = 1 ∧ rootKindAt σ (specPtr (untagA w)) = .stage2 ∧ untagA w ∈ X) ∨ + (tagA w = 2 ∧ rootKindAt σ (specPtr (untagA w)) = .high ∧ untagA w ∈ Y) + +theorem ent_kind {Pp Tb : Set U64} {Lv : U64 → Nat} {X Y : Set U64} {mem : U64 → U64} {ksb : U64} {σ : State Unit} + (hR : VsRelA Pp Tb Lv X Y mem ksb σ) + (hX : ∀ r ∈ X, ∃ es, σ.kheap (specPtr r) = some (.archObj (.ext (.stage2Root es)))) + {w : U64} (hw : w ≠ 0#u64) (hk : EntKindA Tb Lv X Y w) : RootKindOfTag σ X Y w := by + rcases hk with h | ⟨h0, hT, -⟩ | ⟨h1, hx⟩ | ⟨h2, hy⟩ + · exact absurd h hw + · obtain ⟨t, hkt, -⟩ := hR.pt _ hT + exact Or.inl ⟨h0, rootKind_ptA hkt⟩ + · obtain ⟨es, hes⟩ := hX _ hx + exact Or.inr (Or.inl ⟨h1, rootKind_s2A hes, hx⟩) + · obtain ⟨l1, hl, -⟩ := hR.high _ hy + exact Or.inr (Or.inr ⟨h2, rootKind_highA hl, hy⟩) + +/-- What `find_high_asid`'s result says of l4v's `vspace_for_asid`. -/ +def HighFindRelA (σ : State Unit) (Y : Set U64) (a : Asid) : core.result.Result U64 error.LookupFault → Prop + | .Ok w => vspaceForAsid a σ = some (specPtr w) ∧ rootKindAt σ (specPtr w) = .high ∧ w ∈ Y + | .Err _ => ∀ p, vspaceForAsid a σ = some p → rootKindAt σ p ≠ .high + +/-- What `ept::find`'s result says of l4v's `vspace_for_asid`. -/ +def S2FindRelA (σ : State Unit) (X : Set U64) (a : Asid) : core.result.Result U64 error.LookupFault → Prop + | .Ok w => vspaceForAsid a σ = some (specPtr w) ∧ rootKindAt σ (specPtr w) = .stage2 ∧ w ∈ X + | .Err _ => ∀ p, vspaceForAsid a σ = some p → rootKindAt σ p ≠ .stage2 + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {Pp Tb : Set U64} {Lv : U64 → Nat} {X Y : Set U64} {σ : State Unit} + +set_option maxHeartbeats 2000000 in +theorem find_high_asidA {s : S} (hR : VsRelA Pp Tb Lv X Y (L.mem s) (L.ksb s) σ) + (hX : ∀ r ∈ X, ∃ es, σ.kheap (specPtr r) = some (.archObj (.ext (.stage2Root es)))) {asid : U64} + (h16 : asid.val < 65536) : + vspace.find_high_asid st s asid ⦃ r => HighFindRelA σ Y (BitVec.ofNat 16 asid.val) r ⦄ := by + unfold vspace.find_high_asid + refine WP.spec_bind (find_asid_rawA hR h16) fun r hr => ?_ + rcases r with w | e + · obtain ⟨hs, hw0, hk⟩ := hr + have K := ent_kind hR hX hw0 hk + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok, lift, rtag, rs2] + split + · rename_i hne + have hne' : ¬ w &&& 3#u64 = 2#u64 := by simpa using hne + have ht : tagA w ≠ 2 := fun h => hne' ((tag_eq_iff w 2#u64 (by decide)).2 h) + refine (WP.spec_ok _).2 ?_ + show ∀ p, vspaceForAsid _ σ = some p → rootKindAt σ p ≠ .high + intro p hp + rw [hs] at hp + obtain rfl := Option.some.inj hp + rcases K with ⟨-, h⟩ | ⟨-, h, -⟩ | ⟨h, -⟩ + · rw [h]; decide + · rw [h]; decide + · exact absurd h ht + · rename_i hz + have ht : w &&& 3#u64 = 2#u64 := by apply UScalar.eq_of_val_eq; simpa using hz + have ht2 : tagA w = 2 := (tag_eq_iff w 2#u64 (by decide)).1 ht + refine (WP.spec_ok _).2 ?_ + show vspaceForAsid _ σ = some (specPtr (w &&& (~~~ (3#u64 : U64)))) ∧ _ + have hu : w &&& (~~~ (3#u64 : U64)) = untagA w := rfl + rw [hu] + rcases K with ⟨h, -⟩ | ⟨h, -⟩ | ⟨-, hk2, hy⟩ + · rw [ht2] at h; cases h + · rw [ht2] at h; cases h + · exact ⟨hs, hk2, hy⟩ + · simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame.from, + bind_tc_ok] + refine (WP.spec_ok _).2 ?_ + show ∀ p, vspaceForAsid _ σ = some p → rootKindAt σ p ≠ .high + exact fun p hp => by + have hr' : vspaceForAsid (BitVec.ofNat 16 asid.val) σ = none := hr + rw [hr'] at hp; cases hp + +set_option maxHeartbeats 2000000 in +theorem ept_findA {s : S} (hR : VsRelA Pp Tb Lv X Y (L.mem s) (L.ksb s) σ) + (hX : ∀ r ∈ X, ∃ es, σ.kheap (specPtr r) = some (.archObj (.ext (.stage2Root es)))) {asid : U64} + (h16 : asid.val < 65536) : + ept.find st s asid ⦃ r => S2FindRelA σ X (BitVec.ofNat 16 asid.val) r ⦄ := by + unfold ept.find + refine WP.spec_bind (find_asid_rawA hR h16) fun r hr => ?_ + rcases r with w | e + · obtain ⟨hs, hw0, hk⟩ := hr + have K := ent_kind hR hX hw0 hk + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok, lift, rtag, rss] + split + · rename_i hne + have hne' : ¬ w &&& 3#u64 = 1#u64 := by simpa using hne + have ht : tagA w ≠ 1 := fun h => hne' ((tag_eq_iff w 1#u64 (by decide)).2 h) + refine (WP.spec_ok _).2 ?_ + show ∀ p, vspaceForAsid _ σ = some p → rootKindAt σ p ≠ .stage2 + intro p hp + rw [hs] at hp + obtain rfl := Option.some.inj hp + rcases K with ⟨-, h⟩ | ⟨h, -, -⟩ | ⟨-, h, -⟩ + · rw [h]; decide + · exact absurd h ht + · rw [h]; decide + · rename_i hz + have ht : w &&& 3#u64 = 1#u64 := by apply UScalar.eq_of_val_eq; simpa using hz + have ht1 : tagA w = 1 := (tag_eq_iff w 1#u64 (by decide)).1 ht + refine (WP.spec_ok _).2 ?_ + show vspaceForAsid _ σ = some (specPtr (w &&& (~~~ (3#u64 : U64)))) ∧ _ + have hu : w &&& (~~~ (3#u64 : U64)) = untagA w := rfl + rw [hu] + rcases K with ⟨h, -⟩ | ⟨-, hk2, hx⟩ | ⟨h, -⟩ + · rw [ht1] at h; cases h + · exact ⟨hs, hk2, hx⟩ + · rw [ht1] at h; cases h + · simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame.from, + bind_tc_ok] + refine (WP.spec_ok _).2 ?_ + show ∀ p, vspaceForAsid _ σ = some p → rootKindAt σ p ≠ .stage2 + exact fun p hp => by + have hr' : vspaceForAsid (BitVec.ofNat 16 asid.val) σ = none := hr + rw [hr'] at hp; cases hp + +end + +end + +end Fl4ProofsA64.P3.Wit2 diff --git a/proofs/Fl4ProofsA64/P3/A64ArchSwE.lean b/proofs/Fl4ProofsA64/P3/A64ArchSwE.lean new file mode 100644 index 000000000..11ed2bbe1 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/A64ArchSwE.lean @@ -0,0 +1,266 @@ +/- +P3 (aarch64, A64-P3 non-vacuity witness 2, line archsw, part 5): **the mapped-`Pml4` arm of `set_vm_root`.** + +* spec side: `headWritesA` / `tailWritesA` split `threadRootWritesA` of a thread with no VCPU into the native root writes + and the guest tail (`threadRootWritesA_unbound`); `fold_global`, `fold_nohigh`, `fold_high` fold the head writes; +* `high_root_corr`: FL4's `high_root_of` against the spec's `highRootA` (HIGH slot cap, `find_high_asid`); + [J] `hY0`: a high root is not at address 0 (the record does not say so; FL4's `high = 0` test needs it); +* raw side: `raw_nohigh`, `raw_high` (the two orders of `set_vspace` / `set_high_vspace` on the log); +* `head_pml4`: `vmHead` on a mapped `Pml4` cap writes what the spec's `headWritesA` says, in each of the arm's branches + (`find_vspace_for_asid` `Ok` equal / unequal / `Err`; running a guest or not; with / without a high window), given + `VsRootOKA` (`vspaceForAsid` of a root cap is Stage 1; ASID ≠ 0) and the kernel-root tie `hk`. + +Named premises: [J] `hY0` (record gap, reported), [J] `hk` (F4). New module. +-/ +import Fl4ProofsA64.P3.A64ArchSwD +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Wit2 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P1.W7.R.ST.BootWit Fl4ProofsA64.P3 + Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section +open Classical + +/-! ## Spec side -/ + +/-- The native writes of `set_vm_root` for a thread with no VCPU (the head of `threadRootWritesA`). -/ +def headWritesA (σ : State Unit) (t : ObjRef) : List RootWriteA := + match (if (vmRootA σ t).asid.isNone ∨ runningVmA σ t then none else highRootA σ t) with + | none => [.high none (vmRootA σ t).asid, .stage1 (vmRootA σ t)] + | some p => [.stage1 (vmRootA σ t), .high (some p) (vmRootA σ t).asid] + +/-- The guest tail of `set_vm_root` (the Stage-2 root of the guest a running thread enters). -/ +def tailWritesA (σ : State Unit) (t : ObjRef) : List RootWriteA := + if runningVmA σ t then + (match runGuestA σ t with + | some (_, guest) => [.stage2 (vmS2RootA σ guest)] + | none => []) + else [] + +theorem threadRootWritesA_unbound (σ : State Unit) (t : ObjRef) (h : vcpuOfA σ t = none) : + threadRootWritesA σ t = headWritesA σ t ++ tailWritesA σ t := by + simp [threadRootWritesA, headWritesA, tailWritesA, h] + all_goals first | rfl | (congr 1) | convert rfl + +theorem fold_global {σ : State Unit} {t : ObjRef} (hr : vmRootA σ t = ⟨σ.archState.armUsGlobalVspace, none⟩) + (before : RootViewA) : + (headWritesA σ t).foldl RootViewA.write before = ⟨⟨σ.archState.armUsGlobalVspace, none⟩, none, before.stage2⟩ := by + simp [headWritesA, hr, RootViewA.write] + +theorem fold_nohigh {σ : State Unit} {t : ObjRef} {P : ObjRef} {A : Asid} (hr : vmRootA σ t = ⟨P, some A⟩) + (h : runningVmA σ t ∨ highRootA σ t = none) (before : RootViewA) : + (headWritesA σ t).foldl RootViewA.write before = ⟨⟨P, some A⟩, none, before.stage2⟩ := by + rcases h with h | h <;> simp [headWritesA, hr, h, RootViewA.write] + +theorem fold_high {σ : State Unit} {t : ObjRef} {P p : ObjRef} {A : Asid} (hr : vmRootA σ t = ⟨P, some A⟩) + (hn : ¬ runningVmA σ t) (hh : highRootA σ t = some p) (before : RootViewA) : + (headWritesA σ t).foldl RootViewA.write before = ⟨⟨P, some A⟩, some (p, A), before.stage2⟩ := by + simp [headWritesA, hr, hn, hh, RootViewA.write] + +/-- A mapped `Pml4` cap whose ASID's root is the cap's: the spec installs it. -/ +theorem vmRoot_pml4_true {σ : State Unit} {t : ObjRef} {ptr asid : U64} + (hv : vtCapA σ t = some (absCapA (.Pml4 ptr true asid))) + (hc : vspaceForAsid (asidOfA asid) σ = some (specPtr ptr)) : + vmRootA σ t = ⟨specPtr ptr, some (asidOfA asid)⟩ := by + unfold vmRootA; rw [hv]; simp [absCapA, mapDataA, hc] + +theorem vmRoot_pml4_false {σ : State Unit} {t : ObjRef} {ptr asid : U64} + (hv : vtCapA σ t = some (absCapA (.Pml4 ptr true asid))) + (hc : ¬ vspaceForAsid (asidOfA asid) σ = some (specPtr ptr)) : + vmRootA σ t = ⟨σ.archState.armUsGlobalVspace, none⟩ := by + unfold vmRootA; rw [hv]; simp [absCapA, mapDataA, hc] + +/-! ## `high_root_of` -/ + +theorem hro_nonHigh {S : Type} {st : machine.Store S} [L : LawfulStore st] (s : S) (t : U64) + (h : ∀ p b asid, capA st s (hvA t) ≠ .HighVSpace p b asid) : vspace.high_root_of st s t = ok 0#u64 := by + unfold vspace.high_root_of + rw [slot_hv, bind_ok, capA_eq, bind_ok] + generalize capA st s (hvA t) = c at h ⊢ + cases c <;> first | rfl | exact absurd rfl (h _ _ _) + +theorem hro_high {S : Type} {st : machine.Store S} [L : LawfulStore st] (s : S) (t p : U64) (b : Bool) (asid : U64) + (hc : capA st s (hvA t) = .HighVSpace p b asid) : + vspace.high_root_of st s t = (if b then (do + let r ← vspace.find_high_asid st s asid + match r with + | core.result.Result.Ok r1 => if r1 = p then ok p else ok 0#u64 + | core.result.Result.Err _ => ok 0#u64) else ok 0#u64) := by + unfold vspace.high_root_of + rw [slot_hv, bind_ok, capA_eq, bind_ok, hc] + try rfl + +theorem highRoot_none {ac : Cap} {c : capability.Cap} (h : CapEqA c ac) (hn : ∀ p b asid, c ≠ .HighVSpace p b asid) + (σ' : State Unit) (t : ObjRef) (hv : highCapA σ' t = some ac) : highRootA σ' t = none := by + unfold highRootA; rw [hv] + rcases h.eq_or_pt with e | ⟨p', asid', va', e⟩ + · subst e + cases c with + | HighVSpace p b asid => exact absurd rfl (hn _ _ _) + | Frame p sz r dev m ept asid va => cases ept <;> rfl + | _ => rfl + · subst e; rfl + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {AR : WA → HwRootA → Prop} {Z : XSetsA} {s : S} {a : WA} {σ : State Unit} {m : McState} {t : U64} + +/-- A TCB slot's cap, related to the spec's. -/ +theorem J_tcbCap (J : JAtZAX (L := L) AR Z s a σ m) (ht : t ∈ Z.T) {i : U64} (hi : i.val < 7) : + ∃ x ac, σ.kheap (specPtr t) = some (.tcb x) ∧ tcbCnodeMap x (tcbCnodeIndex i.val) = some ac ∧ + CapRelA (capA st s (slotIdx t i)) ac := by + obtain ⟨x, hk, hs⟩ := J.1.st.tcb t ((J.1.thT t).2 ht) + obtain ⟨ac, e, hrel⟩ := hs i.val (by unfold tcbSlotsA; omega) + refine ⟨x, ac, hk, e, ?_⟩ + rw [slotIdx_addrA]; exact hrel + +theorem J_hcap (J : JAtZAX (L := L) AR Z s a σ m) (ht : t ∈ Z.T) : + ∃ ac, highCapA σ (specPtr t) = some ac ∧ CapRelA (capA st s (hvA t)) ac := by + have h6 : layout.tcb.SLOT_HIGH_VSPACE.val = 6 := by unfold layout.tcb.SLOT_HIGH_VSPACE; rfl + obtain ⟨x, ac, hk, e, hrel⟩ := J_tcbCap J ht (i := layout.tcb.SLOT_HIGH_VSPACE) (by rw [h6]; omega) + have ev : hvA t = slotIdx t layout.tcb.SLOT_HIGH_VSPACE := okinj ((slot_hv t).symm.trans (tcb_slot_val t _)) + refine ⟨ac, ?_, by rw [ev]; exact hrel⟩ + unfold highCapA; rw [hk]; rw [h6] at e; exact e + +/-- **`high_root_of` against `highRootA`** (module docstring). -/ +theorem high_root_corr (J : JAtZAX (L := L) AR Z s a σ m) (ht : t ∈ Z.T) (hY0 : ∀ y ∈ Z.Y, y ≠ 0#u64) : + ∃ hv, vspace.high_root_of st s t = ok hv ∧ (hv = 0#u64 → highRootA σ (specPtr t) = none) ∧ + (hv ≠ 0#u64 → highRootA σ (specPtr t) = some (specPtr hv)) := by + obtain ⟨ac, hhc, hrel⟩ := J_hcap J ht + by_cases hH : ∃ p b asid, capA st s (hvA t) = .HighVSpace p b asid + · obtain ⟨p, b, asid, hc⟩ := hH + rw [hc] at hrel + cases b with + | false => + have hac : ac = absCapA (.HighVSpace p false asid) := hrel.2.symm + refine ⟨0#u64, ?_, fun _ => ?_, fun h => absurd rfl h⟩ + · rw [hro_high s t p false asid hc]; simp + · unfold highRootA; rw [hhc, hac]; rfl + | true => + have hac : ac = absCapA (.HighVSpace p true asid) := hrel.2.symm + have h16 : asid.val < 65536 := by simpa [CapWfA] using hrel.1 + obtain ⟨r, hr, hrel2⟩ := spec_existsA (find_high_asidA (L := L) J.1.vs (fun r hr => J.2.2.1.obj r hr) h16) + have hspec : highRootA σ (specPtr t) = + if vspaceForAsid (asidOfA asid) σ = some (specPtr p) ∧ rootKindAt σ (specPtr p) = .high then some (specPtr p) + else none := by + unfold highRootA; rw [hhc, hac]; simp [absCapA, asidDataA] + rcases r with r1 | e + · by_cases he : r1 = p + · subst he + obtain ⟨h1, h2, h3⟩ := hrel2 + refine ⟨r1, ?_, fun h => absurd h (hY0 _ h3), fun _ => ?_⟩ + · rw [hro_high s t r1 true asid hc]; simp [hr, bind_ok] + · rw [hspec, if_pos ⟨h1, h2⟩] + · refine ⟨0#u64, ?_, fun _ => ?_, fun h => absurd rfl h⟩ + · rw [hro_high s t p true asid hc]; simp [hr, he, bind_ok] + · rw [hspec, if_neg] + rintro ⟨h1, -⟩ + have h1' : vspaceForAsid (BitVec.ofNat 16 asid.val) σ = some (specPtr r1) := hrel2.1 + have : specPtr r1 = specPtr p := Option.some.inj (h1'.symm.trans h1) + exact he (specPtr_inj this) + · refine ⟨0#u64, ?_, fun _ => ?_, fun h => absurd rfl h⟩ + · rw [hro_high s t p true asid hc]; simp [hr, bind_ok] + · rw [hspec, if_neg] + rintro ⟨h1, h2⟩ + exact hrel2 _ h1 h2 + · push_neg at hH + have hne : ∀ p b asid, capA st s (hvA t) ≠ .HighVSpace p b asid := hH + exact ⟨0#u64, hro_nonHigh s t hne, fun _ => highRoot_none hrel.2 (fun p b asid => by + intro h; exact hne p b asid h) σ _ hhc, fun h => absurd rfl h⟩ + +/-! ## The raw side -/ + +theorem raw_nohigh (a : WA) (ptr asid : U64) : + ∃ a1 : WA, (do let a2 ← AR1.set_high_vspace a 0#u64 asid; AR1.set_vspace a2 ptr asid) = ok a1 ∧ + a1.s1 = (ptr, asid) ∧ a1.hi.1 = 0#u64 ∧ a1.s2 = a.s2 := by + rw [ar1_setHigh, bind_ok, ar1_setVspace] + by_cases h : ptr = ({ a with hi := (0#u64, asid) } : WA).s1.1 ∧ asid = ({ a with hi := (0#u64, asid) } : WA).s1.2 + · rw [if_pos h] + exact ⟨_, rfl, Prod.ext h.1.symm h.2.symm, rfl, rfl⟩ + · rw [if_neg h] + exact ⟨_, rfl, rfl, rfl, rfl⟩ + +theorem raw_high (a : WA) (ptr asid hv : U64) : + ∃ a1 : WA, (do let a2 ← AR1.set_vspace a ptr asid; AR1.set_high_vspace a2 hv asid) = ok a1 ∧ + a1.s1 = (ptr, asid) ∧ a1.hi = (hv, asid) ∧ a1.s2 = a.s2 := by + rw [ar1_setVspace, bind_ok] + by_cases h : ptr = a.s1.1 ∧ asid = a.s1.2 + · rw [if_pos h, ar1_setHigh] + exact ⟨_, rfl, Prod.ext h.1.symm h.2.symm, rfl, rfl⟩ + · rw [if_neg h, ar1_setHigh] + exact ⟨_, rfl, rfl, rfl, rfl⟩ + +/-! ## The `Pml4` arm -/ + +/-- **`vmHead` on a mapped `Pml4` cap** (module docstring). -/ +theorem head_pml4 (J : JAtZAX (L := L) AR Z s a σ m) (ht : t ∈ Z.T) (hVS : VsRootOKA σ) + (hY0 : ∀ y ∈ Z.Y, y ≠ 0#u64) (hk : specPtr (L.mem s (krA (st := st) s)) = σ.archState.armUsGlobalVspace) + {ptr asid : U64} (hc : capA st s (vtA t) = .Pml4 ptr true asid) : + ∃ a1, vmHead (st := st) AR1 s a t (.Pml4 ptr true asid) = ok (s, a1) ∧ + witView a1 = (headWritesA σ (specPtr t)).foldl RootViewA.write (witView a) := by + obtain ⟨ac, hvt, hrel⟩ := J_vtcap J ht + rw [hc] at hrel + have hac : ac = absCapA (.Pml4 ptr true asid) := hrel.2.symm + subst hac + have h16 : asid.val < 65536 := by simpa [CapWfA] using hrel.1 + obtain ⟨r, hr, hrel2⟩ := spec_existsA (find_asid_findA (L := L) J.1.vs (fun r hr => J.2.2.1.obj r hr) h16) + have hfv : vspace.find_vspace_for_asid st s asid = ok r := hr + -- the kernel-root outcome, shared by the three branches that fall back to it + have kern : ¬ vspaceForAsid (asidOfA asid) σ = some (specPtr ptr) → + ∃ a1, vspace.set_kernel_vm_root st AR1 s a = ok (s, a1) ∧ + witView a1 = (headWritesA σ (specPtr t)).foldl RootViewA.write (witView a) := by + intro hn + rw [fold_global (vmRoot_pml4_false hvt hn)] + refine ⟨_, skvr_res s a, ?_⟩ + simp [witView, decRoot, decAsid, decHigh, decS2, hk] + rcases r with r1 | e + · by_cases he : r1 = ptr + · subst he + have hcond : vspaceForAsid (asidOfA asid) σ = some (specPtr r1) := hrel2.1 + have hroot := vmRoot_pml4_true hvt hcond + have hasid : asid ≠ 0#u64 := by + intro h0 + have hne := hVS.userAsid (specPtr t) (asidOfA asid) (by rw [hroot]) + apply hne + rw [h0]; rfl + have hdec : decAsid asid = some (asidOfA asid) := by unfold decAsid; rw [if_neg hasid] + simp only [vmHead, hfv, bind_ok, ↓reduceIte, eq_self_iff_true] + by_cases hrun : L.mem s (stA t) = 8#u64 + · obtain ⟨a1, hra, h1, h2, h3⟩ := raw_nohigh a r1 asid + rw [tcb_state_eq, bind_ok, if_pos (by rw [ts_running_eq]; exact hrun), bind_ok, if_pos rfl, hra, bind_ok] + refine ⟨a1, rfl, ?_⟩ + rw [fold_nohigh hroot (Or.inl ((J_state J ht).2 hrun))] + simp [witView, decRoot, h1, h2, h3, decHigh, hdec] + · obtain ⟨hv, hhv, hz, hnz⟩ := high_root_corr J ht hY0 + have hrun' : ¬ runningVmA σ (specPtr t) := fun h => hrun ((J_state J ht).1 h) + rw [tcb_state_eq, bind_ok, if_neg (by rw [ts_running_eq]; exact hrun), hhv] + simp only [bind_ok] + by_cases hv0 : hv = 0#u64 + · obtain ⟨a1, hra, h1, h2, h3⟩ := raw_nohigh a r1 asid + rw [if_pos hv0, hra, bind_ok] + refine ⟨a1, rfl, ?_⟩ + rw [fold_nohigh hroot (Or.inr (hz hv0))] + simp [witView, decRoot, h1, h2, h3, decHigh, hdec] + · obtain ⟨a1, hra, h1, h2, h3⟩ := raw_high a r1 asid hv + rw [if_neg hv0, hra, bind_ok] + refine ⟨a1, rfl, ?_⟩ + rw [fold_high hroot hrun' (hnz hv0)] + simp [witView, decRoot, h1, h2, h3, decHigh, hdec, hv0] + · have hn : ¬ vspaceForAsid (asidOfA asid) σ = some (specPtr ptr) := by + intro h + have h1 : vspaceForAsid (BitVec.ofNat 16 asid.val) σ = some (specPtr r1) := hrel2.1 + exact he (specPtr_inj (Option.some.inj ((show vspaceForAsid (asidOfA asid) σ = _ from h1).symm.trans h))) + simp only [vmHead, hfv, bind_ok, ↓reduceIte, he] + exact kern hn + · have hn : ¬ vspaceForAsid (asidOfA asid) σ = some (specPtr ptr) := by + intro h + exact hrel2 _ h (hVS.s1 (specPtr t) (specPtr ptr) (asidOfA asid) _ hvt h) + simp only [vmHead, hfv, bind_ok, ↓reduceIte] + exact kern hn + +end + +end + +end Fl4ProofsA64.P3.Wit2 diff --git a/proofs/Fl4ProofsA64/P3/A64ArchSwF.lean b/proofs/Fl4ProofsA64/P3/A64ArchSwF.lean new file mode 100644 index 000000000..231b07248 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/A64ArchSwF.lean @@ -0,0 +1,159 @@ +/- +P3 (aarch64, A64-P3 non-vacuity witness 2, line archsw, part 6): **`set_stage2_root` and the guest tail of `set_vm_root`.** + +* `s2root_view`: `set_stage2_root u` (any VTABLE cap, `Ept` included: `ept::find` against `vmS2RootA`) keeps the Stage-1 and + high-window words of the log and sets the Stage-2 word to the spec's `vmS2RootA` of `u`; +* `vmTail_run`, `tail_view`: the tail of `set_vm_root` (`TS_RUNNING_VM`: `RUN_VCPU` word, the VCPU's guest-TCB word, then + `set_stage2_root` of the guest) writes the spec's `tailWritesA`; +* [J] `RunLinkA` (`P3/ScArchSwA`, record edit F4; an antecedent of `ArchSwFA.execThread`): a running thread's nonzero `RUN_VCPU` word is a record VCPU and that VCPU's nonzero + TCB word is a record thread. `JAtZAX` does not say it (`VcpuCoreA.obj` / `tcb` give the words' meaning only for members of + `Z.V` / `Z.T`), but FL4 reads the guest's VTABLE slot from raw memory, so without it the spec and FL4 can disagree. + +Named premises: [J] `RunLinkA` (an `ArchSwFA.execThread` antecedent, from the record's `vcRun`). New module. +-/ +import Fl4ProofsA64.P3.A64ArchSwE +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Wit2 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P1.W7.R.ST.BootWit Fl4ProofsA64.P3 + Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section +open Classical + +theorem vcpuTCB_zero : vcpu.TCB = 0#u64 := by unfold vcpu.TCB; rfl + +theorem vcpusOf_eq {σ : State Unit} {p : ObjRef} {x : Vcpu} (hk : σ.kheap p = some (.archObj (.vcpu x))) : + vcpusOf σ p = some x := by + unfold vcpusOf aobjsOf Fl4Spec.optMap + rw [hk] <;> rfl + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {AR : WA → HwRootA → Prop} {Z : XSetsA} {s : S} {a : WA} {σ : State Unit} {m : McState} {t : U64} + +theorem gtA_eq (s : S) (t : U64) : gtA (L := L) s t = L.mem s (runVcpuA t) := by + unfold gtA; rw [vcpuTCB_zero]; exact SFF.wadd0' _ + +/-- **`set_stage2_root u` against `vmS2RootA`** (module docstring). -/ +theorem s2root_view (J : JAtZAX (L := L) AR Z s a σ m) {u : U64} (hu : u ∈ Z.T) (a1 : WA) : + ∃ a', vspace.set_stage2_root st AR1 s a1 u = ok (s, a') ∧ a'.s1 = a1.s1 ∧ a'.hi = a1.hi ∧ + decS2 a'.s2.1 a'.s2.2 = vmS2RootA σ (specPtr u) := by + obtain ⟨ac, hvt, hrel⟩ := J_vtcap J hu + rw [set_stage2_root_unf (st := st) AR1, capA_eq, bind_ok] + by_cases hE : ∃ p i b asid x, capA st s (vtA u) = .Ept p i b asid x + · obtain ⟨p, i, b, asid, x, hc⟩ := hE + rw [hc] at hrel ⊢ + have hac : ac = absCapA (.Ept p i b asid x) := hrel.2.symm + have hi0 : i = 0#u64 := UScalar.eq_of_val_eq hrel.1.1 + have h16 : asid.val < 65536 := by have := hrel.1.2; simpa using this + show ∃ a', s2Ept (st := st) AR1 s a1 p i b asid = ok (s, a') ∧ _ + unfold s2Ept + rw [if_pos hi0] + have hnone : ∃ a', (do let a2 ← AR1.set_stage2_vspace a1 0#u64 0#u64; ok (s, a2) : Result (S × WA)) = ok (s, a') ∧ + a'.s1 = a1.s1 ∧ a'.hi = a1.hi ∧ decS2 a'.s2.1 a'.s2.2 = (none : Option HwRootA) := + ⟨{ a1 with s2 := (0#u64, 0#u64) }, by rw [ar1_setStage2, bind_ok], rfl, rfl, by simp [decS2]⟩ + cases b with + | false => + simp only [Bool.false_eq_true, ↓reduceIte] + obtain ⟨a', h1, h2, h3, h4⟩ := hnone + refine ⟨a', h1, h2, h3, ?_⟩ + rw [h4]; unfold vmS2RootA; rw [hvt, hac]; rfl + | true => + simp only [↓reduceIte] + obtain ⟨r, hr, hrel2⟩ := spec_existsA (ept_findA (L := L) J.1.vs (fun r hr => J.2.2.1.obj r hr) h16) + rw [hr, bind_ok] + have hspec : vmS2RootA σ (specPtr u) = + if vspaceForAsid (asidOfA asid) σ = some (specPtr p) ∧ rootKindAt σ (specPtr p) = .stage2 then + some ⟨specPtr p, some (asidOfA asid)⟩ else none := by + unfold vmS2RootA; rw [hvt, hac]; simp [absCapA, asidDataA] + rcases r with r1 | e + · by_cases he : r1 = p + · subst he + obtain ⟨h1, h2, -⟩ := hrel2 + have hasid : asid ≠ 0#u64 := by + intro h0 + have h1' : vspaceForAsid (BitVec.ofNat 16 asid.val) σ = some (specPtr r1) := h1 + rw [h0] at h1' + have h1'' : vspaceForAsid (BitVec.ofNat 16 0) σ = some (specPtr r1) := h1' + rw [vspaceForAsid_zeroA] at h1'' + cases h1'' + simp only [bind_ok, ↓reduceIte, eq_self_iff_true, ar1_setStage2] + refine ⟨{ a1 with s2 := (r1, asid) }, rfl, rfl, rfl, ?_⟩ + rw [hspec, if_pos ⟨h1, h2⟩] + simp [decS2, decRoot, decAsid, hasid] + · simp only [bind_ok, he, ↓reduceIte] + obtain ⟨a', h1, h2, h3, h4⟩ := hnone + refine ⟨a', h1, h2, h3, ?_⟩ + rw [h4, hspec, if_neg] + rintro ⟨h1', -⟩ + have h1'' : vspaceForAsid (BitVec.ofNat 16 asid.val) σ = some (specPtr r1) := hrel2.1 + exact he (specPtr_inj (Option.some.inj ((show vspaceForAsid (asidOfA asid) σ = _ from h1'').symm.trans h1'))) + · simp only [bind_ok] + obtain ⟨a', h1, h2, h3, h4⟩ := hnone + refine ⟨a', h1, h2, h3, ?_⟩ + rw [h4, hspec, if_neg] + rintro ⟨h1', h2'⟩ + exact hrel2 _ h1' h2' + · push_neg at hE + have hne : ∀ p i b asid x, capA st s (vtA u) ≠ .Ept p i b asid x := hE + rw [s2Arm_nonEpt s a1 _ hne] + exact ⟨{ a1 with s2 := (0#u64, 0#u64) }, rfl, rfl, rfl, + by rw [nonEpt_s2Root hrel.2 hne σ _ hvt]; simp [decS2]⟩ + +/-- The tail of `set_vm_root` of a running thread. -/ +theorem vmTail_run (s : S) (a1 : WA) (t : U64) (hrun : L.mem s (stA t) = 8#u64) : + vmTail (st := st) AR1 s a1 t = + (if (L.mem s (runVcpuA t) != 0#u64) = true then + (if (L.mem s (L.mem s (runVcpuA t)) != 0#u64) = true then + vspace.set_stage2_root st AR1 s a1 (L.mem s (L.mem s (runVcpuA t))) else ok (s, a1)) + else ok (s, a1)) := by + unfold vmTail + rw [tcb_state_eq, bind_ok, if_pos (by rw [ts_running_eq]; exact hrun), eq_of_spec_fd P0.Consts.layout.tcb.RUN_VCPU.spec] + simp only [bind_ok, lift, L.read_eq, vcpuTCB_zero] + have e : ∀ v : U64, core.num.U64.wrapping_add v 0#u64 = v := fun v => SFF.wadd0' v + simp only [e] + rfl + +/-- **The tail of `set_vm_root` writes `tailWritesA`** (module docstring). -/ +theorem tail_view (J : JAtZAX (L := L) AR Z s a σ m) (ht : t ∈ Z.T) (hRL : RunLinkA (L := L) Z s t) (a1 : WA) : + ∃ a2, vmTail (st := st) AR1 s a1 t = ok (s, a2) ∧ + witView a2 = (tailWritesA σ (specPtr t)).foldl RootViewA.write (witView a1) := by + by_cases hrun : L.mem s (stA t) = 8#u64 + · have hrunS : runningVmA σ (specPtr t) := (J_state J ht).2 hrun + obtain ⟨x, hk, -, hrv⟩ := J.2.1.tcb t ht + rw [vmTail_run s a1 t hrun] + by_cases hr0 : L.mem s (runVcpuA t) = 0#u64 + · have hg : runGuestA σ (specPtr t) = none := by + unfold runGuestA; rw [hk]; simp [hrv, hr0, optRefA] + refine ⟨a1, ?_, ?_⟩ + · rw [if_neg (by simp [hr0])] + · unfold tailWritesA; rw [if_pos hrunS, hg]; rfl + · obtain ⟨hV, hT⟩ := hRL hr0 + obtain ⟨xv, hkv, hvt, -⟩ := J.2.1.obj _ hV + have hvo := vcpusOf_eq hkv + have hrv' : x.tcbExt.tcbRunVcpu = some (specPtr (L.mem s (runVcpuA t))) := by + rw [hrv]; unfold optRefA; rw [if_neg hr0] + by_cases hg0 : L.mem s (L.mem s (runVcpuA t)) = 0#u64 + · have hg : runGuestA σ (specPtr t) = none := by + unfold runGuestA; rw [hk]; simp [hrv', hvo, hvt, optRefA, hg0] + refine ⟨a1, ?_, ?_⟩ + · rw [if_pos (by simpa using hr0), if_neg (by simp [hg0])] + · unfold tailWritesA; rw [if_pos hrunS, hg]; rfl + · have hgT := hT hg0 + have hg : runGuestA σ (specPtr t) = some (specPtr (L.mem s (runVcpuA t)), specPtr (L.mem s (L.mem s (runVcpuA t)))) := by + unfold runGuestA; rw [hk]; simp [hrv', hvo, hvt, optRefA, hg0] + obtain ⟨a2, h1, h2, h3, h4⟩ := s2root_view J hgT a1 + refine ⟨a2, ?_, ?_⟩ + · rw [if_pos (by simpa using hr0), if_pos (by simpa using hg0), h1] + · unfold tailWritesA; rw [if_pos hrunS, hg] + simp [witView, RootViewA.write, h2, h3, h4] + · have hrunS : ¬ runningVmA σ (specPtr t) := fun h => hrun ((J_state J ht).1 h) + refine ⟨a1, vmTail_notRun s a1 t hrun, ?_⟩ + unfold tailWritesA; rw [if_neg hrunS]; rfl + +end + +end + +end Fl4ProofsA64.P3.Wit2 diff --git a/proofs/Fl4ProofsA64/P3/A64ArchSwG.lean b/proofs/Fl4ProofsA64/P3/A64ArchSwG.lean new file mode 100644 index 000000000..3ddba9aae --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/A64ArchSwG.lean @@ -0,0 +1,81 @@ +/- +P3 (aarch64, A64-P3 non-vacuity witness 2, line archsw, part 7): **`ArchSwFA.execThread` at core 0, every arm.** + +`head_any` dispatches `vmHead` over the VTABLE cap (a mapped `Pml4` is `head_pml4`, every other cap the kernel root); +`execThread_full` composes it with the guest tail (`tail_view`) for a thread with no VCPU, and with the kernel root plus +`set_stage2_root` (`s2root_view`) for a thread with a bound VCPU, against `threadRootWritesA` (`threadRootWritesA_unbound`, +`boundRoot_viewA`). + +Named premises: [J] `hk` (F4: `specPtr (mem (krA s)) = armUsGlobalVspace`), [J] `hY0` (a high root is not at address 0), +[J] `hRL` (`RunLinkA`: a running thread's run VCPU and its guest TCB are record members); the last two are gaps of the +record, reported. New module. +-/ +import Fl4ProofsA64.P3.A64ArchSwF +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Wit2 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P1.W7.R.ST.BootWit Fl4ProofsA64.P3 + Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section +open Classical + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {AR : WA → HwRootA → Prop} {Z : XSetsA} {s : S} {a : WA} {σ : State Unit} {m : McState} {t : U64} + +/-- The head of `set_vm_root` (no bound VCPU) writes `headWritesA`, for every VTABLE cap. -/ +theorem head_any (J : JAtZAX (L := L) AR Z s a σ m) (ht : t ∈ Z.T) (hVS : VsRootOKA σ) + (hY0 : ∀ y ∈ Z.Y, y ≠ 0#u64) (hk : specPtr (L.mem s (krA (st := st) s)) = σ.archState.armUsGlobalVspace) : + ∃ a1, vmHead (st := st) AR1 s a t (capA st s (vtA t)) = ok (s, a1) ∧ + witView a1 = (headWritesA σ (specPtr t)).foldl RootViewA.write (witView a) := by + by_cases hP : ∃ ptr asid, capA st s (vtA t) = .Pml4 ptr true asid + · obtain ⟨ptr, asid, hc⟩ := hP + rw [hc] + exact head_pml4 J ht hVS hY0 hk hc + · push_neg at hP + obtain ⟨ac, hvt, hrel⟩ := J_vtcap J ht + rw [vmHead_nonRoot s a t _ hP, skvr_res] + refine ⟨_, rfl, ?_⟩ + rw [fold_global (nonRoot_vmRoot hrel.2 hP σ _ hvt)] + simp [witView, decRoot, decAsid, decHigh, decS2, hk] + +/-- **`ArchSwFA.execThread` at core 0** (module docstring). -/ +theorem execThread_full (J : JAtZAX (L := L) AR Z s a σ m) (ht : t ∈ Z.T) (hVS : VsRootOKA σ) + (hY0 : ∀ y ∈ Z.Y, y ≠ 0#u64) (hk : specPtr (L.mem s (krA (st := st) s)) = σ.archState.armUsGlobalVspace) + (hRL : RunLinkA (L := L) Z s t) : + vspace.set_vm_root st AR1 s a t ⦃ r => r.1 = s ∧ witRootWrites a r.2 0 (threadRootWritesA σ (specPtr t)) ⦄ := by + by_cases hb : vcpuOfA σ (specPtr t) = none + · obtain ⟨a1, h1, h2⟩ := head_any J ht hVS hY0 hk + obtain ⟨a2, h3, h4⟩ := tail_view J ht hRL a1 + have hbz : L.mem s (vcA t) = 0#u64 := (J_vcpu J ht).1 hb + have e : vspace.set_vm_root st AR1 s a t = ok (s, a2) := by + rw [set_vm_root_split, eq_of_spec_fd (vcpu.bound.spec (L := L) s t)] + simp only [bind_ok] + rw [if_neg (by unfold vcA at hbz; simp [hbz]), capA_eq, bind_ok, h1] + simp only [bind_ok] + exact h3 + rw [e] + refine (WP.spec_ok _).2 ⟨rfl, witRootWrites_of ?_⟩ + rw [h4, h2, threadRootWritesA_unbound σ _ hb, List.foldl_append] + · have hb' : L.mem s (vcA t) ≠ 0#u64 := fun h => hb ((J_vcpu J ht).2 h) + obtain ⟨v, hv⟩ : ∃ v, vcpuOfA σ (specPtr t) = some v := Option.ne_none_iff_exists'.1 hb + obtain ⟨a2, h1, h2, h3, h4⟩ := s2root_view J ht + ({ a with hi := (0#u64, 0#u64), s1 := (L.mem s (krA (st := st) s), 0#u64) } : WA) + have e : vspace.set_vm_root st AR1 s a t = ok (s, a2) := by + rw [set_vm_root_split, eq_of_spec_fd (vcpu.bound.spec (L := L) s t)] + simp only [bind_ok] + rw [if_pos (by simpa [vcA] using hb'), skvr_res] + simp only [bind_ok] + show vspace.set_stage2_root st AR1 s _ t = _ + rw [h1] + rw [e] + refine (WP.spec_ok _).2 ⟨rfl, witRootWrites_of ?_⟩ + rw [boundRoot_viewA hv] + simp [witView, decRoot, decAsid, decHigh, h2, h3, h4, hk, globalRootA] + +end + +end + +end Fl4ProofsA64.P3.Wit2 diff --git a/proofs/Fl4ProofsA64/P3/A64ArchSwH.lean b/proofs/Fl4ProofsA64/P3/A64ArchSwH.lean new file mode 100644 index 000000000..fef4daa20 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/A64ArchSwH.lean @@ -0,0 +1,39 @@ +/- +P3 (aarch64, A64-P3 non-vacuity witness 2, line archsw, part 8): **an inhabitant of `ArchSwFA` for the model arch.** + +`witArchSwOf : ArchSwFA st AR1` is unconditional. The four facts the record does not give `JAtZAX` (the single core, the +`KERNEL_ROOT` tie, high windows off address `0`, the run links) are antecedents of `ArchSwFA.execIdle` / `execThread` +themselves (record edit F4, `P3/ScArchSwA`), so the arm lemmas (`P3/A64ArchSwA`..`G`) consume them directly; the top +theorems derive them from the record (`JStage3A.kroot`, `JStage3A.archRestA`). + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.A64ArchSwG +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Wit2 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P1.W7.R.ST.BootWit Fl4ProofsA64.P3 + Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **`ArchSwFA` for the model arch** (module docstring). -/ +def witArchSwOf : ArchSwFA st AR1 := + witArchSw + (fun s a c σ Z m AR J hc _ hk hc0 => by + have h0 : c = 0 := hc.symm.trans hc0 + subst h0 + exact witExecIdle hk) + (fun s a c σ t Z m AR J hc ht hVS hY0 hk hRL hc0 => by + have h0 : c = 0 := hc.symm.trans hc0 + subst h0 + exact execThread_full J ht hVS hY0 hk hRL) + +end + +end + +end Fl4ProofsA64.P3.Wit2 diff --git a/proofs/Fl4ProofsA64/P3/A64Wit2ArchA.lean b/proofs/Fl4ProofsA64/P3/A64Wit2ArchA.lean new file mode 100644 index 000000000..3f136a70e --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/A64Wit2ArchA.lean @@ -0,0 +1,310 @@ +/- +P3 (aarch64, A64-P3 non-vacuity witness 2, part 1): **a model `machine.Arch` for the Stage-3 syscall top**. + +The first witness's `witAr` returned a default everywhere, so it violated `FaultEnvA.htu` / `hmb` (`ticks_to_us ≡ 0`, +`kernel_wcet_ticks ≡ 0`) and could not carry `NowAgreeA`; this module replaces it. + +* `WA`: the arch state of the model. `ms` is the machine state (the arch's clock *is* the spec's: `now` reads + `fl4RawCounterOracle (ms.timeState + 1)` and advances `ms` exactly as `rawStateA`), `dl` / `dlO` the programmed + deadline of core 0 and a constant view of every other core, `s1` / `hi` / `s2` the last raw arguments of + `set_vspace` / `set_high_vspace` / `set_stage2_vspace` (what `ArchSwFA`'s root log will be decoded from), + `fpu` / `vcpu` the per-core FPU and VCPU views (unused by the operations yet; they are `ArchSwFA`'s data). +* `witArA`: the arch. Timer conversions, WCET, period and `max_spi` are the spec's / the platform's; `take_pending_irq` + never takes an interrupt; `set_vspace` clears the high window when the Stage-1 root changes (`RootViewA.write`). +* `OpaqueWA` [P]: the facts about `opaque` spec constants the top asks (no Lean term proves or refutes them; any + value of the constant's type is a model): one domain, `maxTicksToUs = U64::MAX`, WCET 10 µs, `fl4MinBudget`, + `fl4MaxReleaseTime` and the tick conversions agree with l4v's, the period fits, the IRQ oracle only yields the + VGIC maintenance line (a non-kernel IRQ: `getActiveIRQ true` never reports one), GICv2, `numNodes = 1`. +* Proved at `w1Ar witArA`: `ArchTotal`, `MaxSpi`, `ArchPlat`, `NowAgreeA` (at `WClock`), the budget / conversion + agreements (`Fl4MinBudgetAgreeA`, `Fl4TicksToUsAgreeA`, `ScCfgTimeAgreeA`, `sporadic.max_release_time`), + `IrqArchA`, `SniHypA`, `ArRootKeepA`, `FinPlatA`, `FaultEnvA`. +Not here: `IrqPlatA` (dedupe / F1 change it), `PollAgreeA` and `HPreEnvA` (dedupe C), `ArchSwFA`. + +Named premises: [P] `OpaqueWA`. New module; no existing statement changed. +-/ +import Fl4ProofsA64.P3.Stage3SysSendA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Wit2 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P1.W7.R.ST.BootWit Fl4ProofsA64.P3 + Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section +open Classical + +/-! ## The arch -/ + +/-- **The model arch state** (module docstring). -/ +structure WA where + ms : MachineState + dl : DeadlineViewA + dlO : DeadlineViewA + s1 : U64 × U64 + hi : U64 × U64 + s2 : U64 × U64 + fpu : Nat → FpuCoreA + vcpu : Nat → VcpuCoreViewA + +/-- **The model arch** (module docstring). -/ +def witArA : machine.Arch WA where + private_device_frame := fun _ _ => ok false + dma_ram := fun _ _ _ => ok false + dma_frame := fun a _ _ _ => ok a + virtualization := fun _ => ok 0#u64 + vcpu_capability := fun _ _ => ok 0#u64 + vcpu_barrier := fun a => ok a + vcpu_sync := fun a _ _ => ok a + vcpu_release := fun a _ => ok a + invalidate_ept := fun a _ _ => ok a + tdx_active := fun _ => ok false + tdx_report := fun a _ _ _ _ _ _ _ _ _ => ok (0#u64, a) + tdx_extend_rtmr := fun a _ _ _ _ _ _ _ => ok (0#u64, a) + tdx_get_quote := fun a _ _ => ok (0#u64, a) + now := fun a => ok (⟨fl4RawCounterOracle (a.ms.timeState + 1)⟩, { a with ms := rawStateA a.ms }) + set_deadline := fun a d => ok { a with dl := ⟨a.dl.backend, d.bv, fl4ProgrammedDeadline d.bv⟩ } + us_to_ticks := fun _ u => ok ⟨fl4UsToTicks u.bv⟩ + ticks_to_us := fun _ v => ok ⟨ticksToUs v.bv⟩ + max_us := fun _ => ok ⟨fl4MaxPeriodUs * 8#64⟩ + kernel_wcet_ticks := fun _ => ok ⟨kernelWCETTicks⟩ + irq_pending := fun a => ok (false, a) + take_pending_irq := fun a => ok (syscall.NO_IRQ, a) + mask_irq := fun a _ _ => ok a + ack_irq := fun a _ => ok a + irq_free := fun a _ => ok a + set_vspace := fun a r i => + ok (if r = a.s1.1 ∧ i = a.s1.2 then a else { a with s1 := (r, i), hi := (0#u64, 0#u64) }) + invalidate_page := fun a _ _ => ok a + invalidate_asid := fun a _ => ok a + invalidate_page_structures := fun a _ => ok a + ioapic_map := fun a _ _ _ _ _ => ok (false, a) + ioapic_map_remapped := fun a _ _ _ _ _ _ => ok (false, a) + num_ioapics := fun _ => ok 0#u64 + ioapic_pins := fun _ _ => ok 0#u64 + port_in := fun a _ _ => ok (0#u64, a) + port_out := fun a _ _ _ => ok a + fpu_flush := fun a _ => ok a + fpu_sanitise := fun a _ => ok a + fpu_reload := fun a _ => ok a + fpu_release := fun a _ => ok a + remote_stall := fun a _ => ok a + send_reschedule := fun a _ => ok a + cache_op := fun a _ _ _ _ _ _ => ok a + hw_access_flag := fun _ => ok fl4A64Platform.hwAccessFlag + max_spi := fun _ => ok ⟨fl4A64Platform.maxSpi⟩ + configure_irq := fun a _ _ _ => ok a + msi_map := fun a _ _ _ _ => ok (0#u64, a) + s2_ipa_bits := fun _ => ok 40#u64 + set_stage2_vspace := fun a r i => ok { a with s2 := (r, i) } + invalidate_stage2_page := fun a _ _ => ok a + invalidate_stage2_vmid := fun a _ => ok a + vcpu_flush := fun a _ => ok a + vcpu_flush_state := fun a _ _ => ok a + vcpu_vgic_vtr := fun _ => ok ⟨fl4A64Platform.vgicVtr⟩ + vcpu_gic_version := fun _ => ok 0#u64 + vcpu_midr := fun _ => ok ⟨fl4A64Platform.vcpuMidr⟩ + has_pauth := fun _ => ok fl4A64Platform.hasPauth + next_pac_epoch := fun a => ok (0#u64, a) + kernel_ttbr1_entry := fun _ _ _ => ok 0#u64 + set_high_vspace := fun a r i => ok { a with hi := (r, i) } + invalidate_high_page := fun a _ => ok a + invalidate_high_vspace := fun a _ => ok a + putc := fun a _ => ok a + get_char := fun a => ok (0#u64, a) + halt := fun a => ok a + +/-- **The arch of the witness**: the model with one kernel TTBR1 table (as `w1_stage3_one`). -/ +abbrev AR1 : machine.Arch WA := w1Ar witArA + +/-- The clock relation of the model: the arch state's machine state is the spec's. -/ +def WClock (a : WA) (ms : MachineState) : Prop := a.ms = ms + +/-- The deadline view of the model: core 0 is `dl`, every other core `dlO`. -/ +def witTm (a : WA) (c : Nat) : DeadlineViewA := if c = 0 then a.dl else a.dlO + +/-- **The facts about `opaque` constants the witness world needs** [P] (module docstring). Only independent facts, one per +constant (a value of the constant's type is a model: `P3/A64Wit2PlatA`); `hag`, `hconv`, `hlimit`, `maxP` and `v2` are +*derived* below from the real definitions (`fl4MinBudget`, `MIN_BUDGET`, `fl4TicksToUs`, ...). `irq` is a property of +the witness world, not of FL4's platform: the oracle never yields a kernel-visible line (it will be listed in §9). -/ +structure OpaqueWA : Prop where + /-- one scheduling domain (`opaque kernelConfig`) -/ + nd : numDomains = 1 + /-- `maxIRQ = 5375` (`opaque kernelConfig`; `HPreEnvA.hMax`) -/ + maxIRQ : kernelConfig.maxIRQ = 5375 + /-- GICv2 (`opaque kernelConfig`) -/ + gic : kernelConfig.configArmGicV3 = false + /-- `numNodes = 1` (`opaque fl4A64Platform`; [S]) -/ + nodes : fl4A64Platform.numNodes = 1 + /-- `maxTicksToUs = U64::MAX` (`opaque maxTicksToUs`) -/ + max : maxTicksToUs = core.num.U64.MAX.bv + /-- kernel WCET 10 µs (`opaque timeConsts`; FL4's build) -/ + wcetUs : kernelWCETUs = 10 + /-- 1000 ticks per timer unit (`opaque timeConsts`) -/ + tpt : ticksPerTimerUnit = 1000 + /-- timer unit 1 (`opaque timeConsts`) -/ + tu : timerUnit = 1 + /-- a 10^9 Hz counter (`opaque fl4TimerFrequencyHz`) -/ + freq : fl4TimerFrequencyHz = 1000000000 + /-- `fl4MaxPeriodUs = MAX_PERIOD_US` (`opaque fl4MaxPeriodUs`) -/ + period : fl4MaxPeriodUs = MAX_PERIOD_US + /-- the IRQ oracle only yields the VGIC maintenance line, a non-kernel IRQ (`opaque irqOracleAx`; witness world) -/ + irq : ∀ n, irqOracle n = irqVGICMaintenance + /-- the raw counter oracle is monotone (`opaque fl4RawCounterOracle`) -/ + hC : ∀ i, fl4RawCounterOracle i ≤ fl4RawCounterOracle (i + 1) + /-- the spec's clock and the raw counter agree at a state whose last observation is the next raw sample (`opaque + timeOracle`, `opaque fl4RawCounterOracle`; witness world: both oracles are `0`; `KEntryFaultInA.hNow`) -/ + hNow : ∀ ms : MachineState, ms.lastMachineTime = fl4RawCounterOracle (ms.timeState + 1) → + fl4ReadRawCounter ms = getCurrentTime ms + +theorem wConvA (t : Word64) : BitVec.ofNat 64 (t.toNat * 1000000 / 1000000000) = t / (1000 / 1) := by + apply BitVec.eq_of_toNat_eq + rw [BitVec.toNat_ofNat, BitVec.toNat_udiv] + have h1 : t.toNat * 1000000 / 1000000000 = t.toNat / 1000 := by omega + have h2 : ((1000 : Word64) / 1).toNat = 1000 := rfl + rw [h1, h2] + exact Nat.mod_eq_of_lt (lt_of_le_of_lt (Nat.div_le_self _ _) t.isLt) + +/-- GICv2. -/ +theorem OpaqueWA.v2 (O : OpaqueWA) : config_ARM_GIC_V3 = false := O.gic + +/-- **`fl4MinBudget = MIN_BUDGET`**, from the real definitions. -/ +theorem OpaqueWA.hag (O : OpaqueWA) : fl4MinBudget = MIN_BUDGET := by + unfold fl4MinBudget MIN_BUDGET kernelWCETTicks usToTicks fl4UsToTicks + rw [O.wcetUs, O.tpt, O.tu, O.freq] + first | decide | rfl + +/-- **`fl4TicksToUs = ticksToUs`**, from the real definitions. -/ +theorem OpaqueWA.hconv (O : OpaqueWA) : Fl4TicksToUsLegacyAgree := by + intro t + unfold fl4TicksToUs ticksToUs + rw [O.tpt, O.tu, O.freq] + exact wConvA t + +/-- **`fl4MaxReleaseTime = MAX_RELEASE_TIME`**, from the real definitions. -/ +theorem OpaqueWA.hlimit (O : OpaqueWA) : fl4MaxReleaseTime = MAX_RELEASE_TIME := by + unfold fl4MaxReleaseTime MAX_RELEASE_TIME MAX_PERIOD usToTicks fl4UsToTicks + rw [O.period, O.tpt, O.tu, O.freq] + first | decide | rfl + +/-- `fl4MaxPeriodUs · 8` fits a `u64`. -/ +theorem OpaqueWA.maxP (O : OpaqueWA) : fl4MaxPeriodUs.toNat < 2 ^ 61 := by + rw [O.period]; first | decide | rfl + +/-! ## Totality and the platform facts -/ + +theorem ar1_total : P0.ArchTotal AR1 := by + constructor <;> intros <;> first + | exact (WP.spec_ok _).2 trivial + | (simp only [AR1, w1Ar]; split <;> exact (WP.spec_ok _).2 trivial) + +theorem ar1_spi : MaxSpi AR1 := fun _ => (WP.spec_ok _).2 (by + have h := BitVec.le_def.1 fl4A64Platform.maxSpi_le + have e : (1019 : BitVec 64).toNat = 1019 := rfl + show fl4A64Platform.maxSpi.toNat ≤ 1019 + omega) + +theorem ar1_plat : ArchPlat AR1 := ⟨⟨40#u64, fun _ => rfl⟩, fun _ => rfl⟩ + +/-- **`NowAgreeA` at `WClock`.** -/ +theorem ar1_now : NowAgreeA AR1 WClock := by + intro a ms h + subst h + exact (WP.spec_ok _).2 ⟨rfl, rfl⟩ + +theorem ar1_minBudget (a : WA) : sporadic.min_budget AR1 a ⦃ mb => mb.bv = MIN_BUDGET ⦄ := by + unfold sporadic.min_budget + show (do let i ← (ok ⟨kernelWCETTicks⟩ : Result U64); ok (core.num.U64.wrapping_mul i 2#u64)) ⦃ _ ⦄ + simp only [bind_ok] + rw [WP.spec_ok, core.num.U64.wrapping_mul_bv_eq] + show kernelWCETTicks * 2 = MIN_BUDGET + exact BitVec.mul_comm _ _ + +theorem ar1_fl4MinBudget (O : OpaqueWA) : ∀ a, Fl4MinBudgetAgreeA AR1 a := fun a => + WP.spec_mono (ar1_minBudget a) fun _ h => h.trans O.hag.symm + +theorem ar1_ticksToUs : ∀ a v, AR1.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄ := + fun _ _ => (WP.spec_ok _).2 rfl + +theorem ar1_toUs (O : OpaqueWA) : ∀ a, Fl4TicksToUsAgreeA AR1 a := + fun _ v => (WP.spec_ok _).2 (O.hconv v.bv).symm + +theorem ar1_usToTicks (a : WA) : ∀ us, AR1.us_to_ticks a us ⦃ v => v.bv = fl4UsToTicks us.bv ⦄ := + fun _ => (WP.spec_ok _).2 rfl + +/-- `max_period_us` returns `fl4MaxPeriodUs`. -/ +theorem ar1_maxPeriod' (O : OpaqueWA) (a : WA) : + sporadic.max_period_us AR1 a ⦃ v => v.bv = fl4MaxPeriodUs ⦄ := by + unfold sporadic.max_period_us + show (do let i ← (ok (⟨fl4MaxPeriodUs * 8#64⟩ : U64) : Result U64); i / 8#u64) ⦃ _ ⦄ + simp only [bind_ok] + refine WP.spec_mono (U64.div_spec (x := ⟨fl4MaxPeriodUs * 8#64⟩) (y := 8#u64) (by decide)) ?_ + intro z hz + have hM := O.maxP + have h1 : (⟨fl4MaxPeriodUs * 8#64⟩ : U64).val = fl4MaxPeriodUs.toNat * 8 := by + show (fl4MaxPeriodUs * 8#64).toNat = _ + rw [BitVec.toNat_mul] + have e8 : (8#64 : BitVec 64).toNat = 8 := rfl + rw [e8] + exact Nat.mod_eq_of_lt (lt_of_lt_of_eq (Nat.mul_lt_mul_of_pos_right hM (by decide)) (by norm_num)) + have h8 : (8#u64 : U64).val = 8 := rfl + rw [h1, h8, Nat.mul_div_cancel _ (by decide : 0 < 8)] at hz + exact BitVec.eq_of_toNat_eq hz + +/-- **`ScCfgTimeAgreeA` at the model arch.** -/ +theorem ar1_cfgTime (O : OpaqueWA) (a : WA) : ScCfgTimeAgreeA AR1 a where + maxPeriod := by + obtain ⟨v, hv, e⟩ := spec_existsA (ar1_maxPeriod' O a) + exact ⟨v, hv, e⟩ + usToTicks := ar1_usToTicks a + minBudget := WP.spec_mono (ar1_minBudget a) fun _ h => h.trans O.hag.symm + wcetUs := O.wcetUs + +/-- `max_release_time` is the spec's limit (the `hLimit` of `HPreEnvA`). -/ +theorem ar1_limit (O : OpaqueWA) (a : WA) : + sporadic.max_release_time AR1 a ⦃ v => v.bv = fl4MaxReleaseTime ⦄ := + (ar1_cfgTime O a).maxRelease + +/-- **`IrqArchA` at the model arch** (`AR0 = MR = True`). -/ +theorem ar1_irqArch (O : OpaqueWA) : IrqArchA AR1 (fun _ _ => True) (fun _ _ => True) where + maskKeep _ _ _ _ _ := (WP.spec_ok _).2 trivial + ackKeep _ _ _ _ := (WP.spec_ok _).2 trivial + mask _ ms irq b _ _ := (WP.spec_ok _).2 + ⟨{ ms with irqMasks := Function.update ms.irqMasks (BitVec.ofNat 13 irq.val) b }, + by first | exact Set.mem_singleton _ | simp [maskInterrupt, NonDet.modify], trivial⟩ + ack _ _ _ _ := (WP.spec_ok _).2 trivial + signalMask h := by rw [O.v2] at h; cases h + +/-- **`SniHypA` at the model arch** (core 0, the one core). -/ +theorem ar1_sni (a : WA) : SniHypA AR1 witTm a 0 where + precision := ⟨⟨fl4UsToTicks (1#u64).bv⟩, rfl, rfl⟩ + program d := (WP.spec_ok _).2 ⟨rfl, rfl, rfl, fun c hc => by simp [witTm, hc]⟩ + +/-- **`ArRootKeepA` at the model arch** (the trivial relation). -/ +theorem ar1_rootKeep : ArRootKeepA AR1 (fun (_ : WA) (_ : HwRootA) => True) where + set_vspace _ _ _ c _ := by + refine (WP.spec_ok _).2 ⟨c, trivial⟩ + set_stage2_vspace _ _ _ c _ := (WP.spec_ok _).2 ⟨c, trivial⟩ + set_high_vspace _ _ _ c _ := (WP.spec_ok _).2 ⟨c, trivial⟩ + set_deadline _ _ c _ := (WP.spec_ok _).2 ⟨c, trivial⟩ + send_reschedule _ _ c _ := (WP.spec_ok _).2 ⟨c, trivial⟩ + +/-- **`FinPlatA` at the model arch** (the trivial relation). -/ +theorem ar1_finPlat (O : OpaqueWA) : FinPlatA AR1 (fun (_ : WA) (_ : HwRootA) => True) where + h1 := O.nodes + arK := ar1_rootKeep + toUs := ar1_toUs O + hmax := O.max + htu := ar1_ticksToUs + +/-- **`FaultEnvA` at the model arch**, over any store and ghost sets whose cores are in range. -/ +theorem ar1_faultEnv {S : Type} {st : machine.Store S} [LawfulStore st] (O : OpaqueWA) (Z : XSetsA) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) : FaultEnvA st AR1 Z where + hA := ar1_total + hCs := hCs + h1 := O.nodes + hmb := ar1_minBudget + hmax := O.max + htu := ar1_ticksToUs + +end + +end Fl4ProofsA64.P3.Wit2 diff --git a/proofs/Fl4ProofsA64/P3/A64Wit2BudgetA.lean b/proofs/Fl4ProofsA64/P3/A64Wit2BudgetA.lean new file mode 100644 index 000000000..7c2e23c94 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/A64Wit2BudgetA.lean @@ -0,0 +1,99 @@ +/- +P3 (aarch64, A64-P3 non-vacuity witness 2, W4, part 2): **a sufficient budget prefix is the identity where the +timestamp rewrites the words it reads.** + +`JSyscallA.budgetOkIdA`: at the syscall-entry record, if the stored `CUR_TIME` is the raw read (`hT`), the head refill is +sufficient (`hsuf`, at every spec state the timestamp reaches) and the store rewrites a word it reads to itself (`hwr`), +then `syscall.budget_ok` returns `true` and the same store: `update_timestamp` writes `CUR_TIME := now` and +`CONSUMED := consumed + (now - cur_time)`, and the sufficient `check_budget_restart` changes nothing. + +Named premises: `hT` [S], `hsuf` [S] (the witness's), `hwr` (the concrete store); the clock [P], `hC` [P], `hvp` [J] as in +`JSyscallA.budgetOkSysA`. New module. +-/ +import Fl4ProofsA64.P3.Stage3KEntryBudgetSysA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +set_option maxRecDepth 100000 + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {ar : machine.Arch A} +variable {AR AR' : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {nA : U64} {s : S} {a : A} {σ : State Unit} {m : McState} + +set_option maxHeartbeats 2000000 in +/-- **A sufficient budget prefix at a fixpoint of the timestamp is the identity** (module docstring). -/ +theorem JSyscallA.budgetOkIdA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar nA)) s a σ m) + (hvp : ∀ b, VsR (L.ksb s) (Pp ∪ Tb) b → Aligned b ∧ ¬ PPW st s b) + {Clock : A → MachineState → Prop} (P : NowAgreeA ar Clock) (hClock : Clock a σ.machineState) + (hnd : numDomains = 1) (C : CounterStepA σ) + (hNow : fl4ReadRawCounter σ.machineState = getCurrentTime σ.machineState) + (hAr : sched.update_timestamp st ar s a ⦃ r => AR' r.2 Z.hw ⦄) + (hmb : ∀ a', Fl4MinBudgetAgreeA ar a') + (hT : (L.curTime s).bv = rawNextA σ.machineState) + (hsuf : ∀ σ1, fl4UpdateTimestamp (Z := Unit) σ = ({((), σ1)}, False) → + fl4GetScRefillSufficient (Z := Unit) σ1.curSc σ1.consumedTime σ1 = ({(true, σ1)}, False)) + (hwr : ∀ b, L.wr s b (L.mem s b) = s) : + ∃ a1, syscall.budget_ok st ar s a = ok (true, s, a1) ∧ sched.update_timestamp st ar s a = ok (s, a1) := by + have E0 := J.toJStage3A.yieldQueueA + have hw := update_timestamp_wordsA E0.sched.toSchedSRN a P hClock + obtain ⟨⟨s1, a1⟩, e1, now0, hn, -, hs⟩ := spec_existsA hw + have hnow0 : now0 = L.curTime s := UScalar.eq_of_val_eq (congrArg BitVec.toNat (hn.trans hT.symm)) + have hs1 : s1 = s := by + have hs' : s1 = L.wr (L.wr s (L.nodeA s layout.node.CUR_TIME) now0) (L.nodeA s layout.node.CONSUMED) + (core.num.U64.wrapping_add (L.mem s (L.nodeA s layout.node.CONSUMED)) + (core.num.U64.wrapping_sub now0 (L.curTime s))) := hs + have z : core.num.U64.wrapping_sub now0 (L.curTime s) = 0#u64 := by + rw [hnow0] + have hx : (L.curTime s).val < U64.size := by + have := (L.curTime s).bv.isLt + simpa [U64.size_def, U64.numBits] using this + have e : ∀ x c : Nat, x < c → (x + (c - x)) % c = 0 := fun x c h => by + rw [Nat.add_sub_cancel' (Nat.le_of_lt h)]; exact Nat.mod_self c + exact UScalar.eq_of_val_eq (by + simp [core.num.U64.wrapping_sub_val_eq] + exact e _ _ hx) + rw [hs', z, SFF.wadd0', hnow0] + have h1 : L.wr s (L.nodeA s layout.node.CUR_TIME) (L.curTime s) = s := hwr _ + rw [h1] + exact hwr _ + rw [hs1] at e1 + refine ⟨a1, ?_, e1⟩ + have ts := J.toJStage3A.timestampA J.sact J.noSwitch hvp P hClock hnd J.validTime C J.curInv J.offReady J.actIdle + hNow hAr + obtain ⟨⟨s2, a2⟩, e2, σ1, e1σ, J1, sa1, -, V1, CI1, R1, Act1, Cl1, Ent1⟩ := spec_existsA ts + have eq2 : (s2, a2) = (s, a1) := ok_inj_ff (e2.symm.trans e1) + obtain ⟨hs2, ha2⟩ := Prod.mk.inj eq2 + obtain ⟨b, dec, spec, off⟩ := spec_existsA + (entryBudgetDecisionA J1.sr.toSchedSRN CI1 J1.sinv.mc a2 (hmb a2)) + have hb : b = true := by + have h := hsuf σ1 e1σ + rw [spec] at h + exact (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst h))).1 + subst hb + rw [hs2, ha2] at dec + obtain ⟨code, -⟩ := budgetSuffA (st := st) (ar := ar) dec spec hnd + unfold syscall.budget_ok + rw [e1, bind_ok] + simp only [uncurry_apply_pair] + unfold sched.check_budget_restart + rw [code, bind_ok] + simp only [uncurry_apply_pair] + rw [eq_of_spec_fd (sched.cur_thread.spec (L := L) s)] + simp only [bind_ok, ↓reduceIte] + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/A64Wit2EntryA.lean b/proofs/Fl4ProofsA64/P3/A64Wit2EntryA.lean new file mode 100644 index 000000000..a231e79a9 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/A64Wit2EntryA.lean @@ -0,0 +1,184 @@ +/- +P3 (aarch64, A64-P3 non-vacuity witness 2, W4 entry facts, part 1): **the `FAULT_IP` write and the budget prefix are +no-ops at the witness.** + +* the witness's root TCB carries `FAULT_IP = NEXT_IP - 4` (`witRegs`, `witRtW 528`), so the entry's `FAULT_IP` write + (`syscallFront_refinesA`) leaves the state alone (`wit_faultIP_id`); +* the timestamp's raw read is the stored `CUR_TIME` (the witness's `now` is the first raw-counter sample, + `wNow`), so `update_timestamp` rewrites the words it reads (`wit_timestamp_id`). + +Named premises: none beyond the record (`JSyscallA` at `witS`, `NowAgreeA`). New module. +-/ +import Fl4ProofsA64.P3.A64WitTopA +import Fl4ProofsA64.P3.A64Wit2PlatA +import Fl4ProofsA64.P3.A64Wit2BudgetA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Wit +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P1.W7.R.ST.BootWit Fl4ProofsA64.P3 + Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched + +noncomputable section + +open Fl4ProofsA64.P3.Wit2 (WA AR1) + +variable {r now mt sl : U64} {uw : U64 → U64} + +local notation "S2" => witS r now mt sl uw +local notation "RT" => (bwv RTb) + +theorem bwr_self (s : BS) (b : U64) : bL.wr s b (bL.mem s b) = s := by + cases s with + | mk m kb cpu => + show (⟨Function.update m b (m b), kb, cpu⟩ : BS) = ⟨m, kb, cpu⟩ + rw [Function.update_eq_self] + +/-- Any register word of the root TCB. -/ +theorem wit_regWord (i : U64) (hi : i.val < 37) : bL.mem S2 (regWA RT i) = witRtW (256 + 8 * i.val) := by + have v : (regWA RT i).val = RTb + (256 + 8 * i.val) := by + have h := Endp.regA_val (t := RT) (i := i) rtAddr hi + rw [vRT] at h + show (Endp.regA RT i).val = _ + omega + exact wit_rtWords _ _ (by omega) (by omega) (by rw [v, vRT]) + +/-- **The `FAULT_IP` write is a no-op at the witness.** -/ +theorem wit_faultIP_id : + bL.wr S2 (Endp.regA (bL.curW S2) layout.reg.FAULT_IP) + (core.num.U64.wrapping_sub (bL.mem S2 (regWA (bL.curW S2) layout.reg.NEXT_IP)) 4#u64) = S2 := by + rw [curW2] + have e1 : bL.mem S2 (regWA RT layout.reg.NEXT_IP) = bwv 4194304 := by + rw [wit_regWord _ (by simp)] + simp [witRtW, rtW] + have e2 : bL.mem S2 (Endp.regA RT layout.reg.FAULT_IP) = 4194300#u64 := by + have := wit_regWord (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) layout.reg.FAULT_IP (by simp) + exact this.trans (by simp [witRtW]) + have e3 : core.num.U64.wrapping_sub (bwv 4194304) 4#u64 = 4194300#u64 := by + first | decide | rfl + rw [e1, e3, ← e2] + exact bwr_self _ _ + + +/-! ## The clock: the witness's `now` is the first raw sample -/ + +/-- The first raw-counter sample after boot (`fl4ReadRawCounter` on `initASt`'s machine state). -/ +def wTick : Fl4SpecA64.Ticks := + Fl4SpecA64.fl4RawCounterOracle ((Fl4SpecA64.initASt (Z := Unit)).machineState.timeState + 1) + +/-- The witness's `now`: the stored `CUR_TIME` and the last observation are the next raw sample. -/ +def wNow : U64 := ⟨wTick⟩ + +theorem wit_rawNext {mt sl : U64} {uw : U64 → U64} : + Sched.rawNextA (witSA wNow mt sl uw).machineState = wTick := rfl + +theorem wit_counterStep {mt sl : U64} {uw : U64 → U64} : Sched.CounterStepA (witSA wNow mt sl uw) := by + unfold Sched.CounterStepA + exact BitVec.le_refl _ + +theorem wit_hNow (O : Wit2.OpaqueWA) {mt sl : U64} {uw : U64 → U64} : + Fl4SpecA64.fl4ReadRawCounter (witSA wNow mt sl uw).machineState = + Fl4SpecA64.getCurrentTime (witSA wNow mt sl uw).machineState := + O.hNow _ rfl + +/-! ## The sufficient head refill -/ + +theorem sc_ne_rt : specPtr (bwv SCb) ≠ specPtr (bwv RTb) := fun h => by + have := congrArg UScalar.val (specPtr_inj h) + rw [vSC, vRT] at this + revert this + norm_num + +/-- **The head refill is sufficient** at every state the timestamp reaches from the witness. -/ +theorem wit_hsuf (O : Wit2.OpaqueWA) {mt sl : U64} {uw : U64 → U64} (hsl : Fl4SpecA64.MIN_BUDGET ≤ sl.bv) + {σ1 : Fl4SpecA64.State Unit} + (e : Fl4SpecA64.fl4UpdateTimestamp (Z := Unit) (witSA wNow mt sl uw) = ({((), σ1)}, False)) : + Fl4SpecA64.fl4GetScRefillSufficient (Z := Unit) σ1.curSc σ1.consumedTime σ1 = ({(true, σ1)}, False) := by + have eτ := Sched.fl4UpdateTimestamp_eqA (witSA wNow mt sl uw) + rw [e] at eτ + have h : σ1 = Fl4SpecA64.fl4TimestampState (Sched.rawNextA (witSA wNow mt sl uw).machineState) + { witSA wNow mt sl uw with machineState := Sched.rawStateA (witSA wNow mt sl uw).machineState } := + (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst eτ))).2 + have hk : σ1.kheap (specPtr (bwv SCb)) = some (.schedContext (rootScA wNow sl) 0) := by + rw [h] + exact (Function.update_of_ne sc_ne_rt _ _).trans w1KA_sc + have hcs : σ1.curSc = specPtr (bwv SCb) := by rw [h]; rfl + have hu : σ1.consumedTime = 0 := by + rw [h] + show (witSA wNow mt sl uw).consumedTime + (wTick - wNow.bv) = 0 + show (0 : Fl4SpecA64.Ticks) + (wTick - wTick) = 0 + simp + rw [hcs, hu, Sched.fl4GetScRefillSufficient_eqA hk (by simp [rootScA]) 0] + have hmin := O.hag + simp [Fl4SpecA64.fl4RefillSufficient, Fl4SpecA64.refillCapacity, Fl4SpecA64.refillHd, rootScA, hmin, hsl, Fl4SpecA64.the] + +/-! ## The VSpace words are not the preemption point's -/ + +theorem wit_hvp {r now mt sl : U64} {uw : U64 → U64} : + ∀ b, VsR (bL.ksb (witS r now mt sl uw)) + (Fl4ProofsA64.P2.BootEpB.w1Pp KB IB 1 r now mt sl uw ∪ w1Tb) b → + Aligned b ∧ ¬ PPW bst (witS r now mt sl uw) b := by + intro b hb + have vW : (core.num.U64.wrapping_add (bL.ksb (witS r now mt sl uw)) layout.ks.WORK_UNITS).val = KBn + 40 := by + have oW : layout.ks.WORK_UNITS.val = 40 := by simp [layout.ks.WORK_UNITS] + rw [ksb2, wa_valm, kbv, oW] + exact Nat.mod_eq_of_lt (by norm_num) + have vN : ∀ o : U64, o.val = 48 ∨ o.val = 40 → o.val < 4288 → + (bL.nodeA (witS r now mt sl uw) o).val = KBn + 223488 + o.val := by + intro o _ ho + have v := nodeV (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) (c := 0#u64) (o := o) (by simp) ho + show (bL.nodeAt (witS r now mt sl uw) (bL.cpu (witS r now mt sl uw)) o).val = _ + rw [cpu2, nodeAt21, v] + simp + have vT := vN layout.node.CUR_TIME (Or.inl (by simp [layout.node.CUR_TIME])) (by simp [layout.node.CUR_TIME]) + have vC := vN layout.node.CONSUMED (Or.inr (by simp [layout.node.CONSUMED])) (by simp [layout.node.CONSUMED]) + have oT : layout.node.CUR_TIME.val = 48 := by simp [layout.node.CUR_TIME] + have oC : layout.node.CONSUMED.val = 40 := by simp [layout.node.CONSUMED] + rw [oT] at vT + rw [oC] at vC + have npp : ∀ x : U64, x.val ≠ KBn + 40 → x.val ≠ KBn + 223488 + 48 → x.val ≠ KBn + 223488 + 40 → + ¬ PPW bst (witS r now mt sl uw) x := by + rintro x h1 h2 h3 (e | e | e) + · exact h1 (e ▸ vW) + · exact h2 (e ▸ vT) + · exact h3 (e ▸ vC) + rcases hb with ⟨p, hp, o, ho, h8, e⟩ | ⟨o, h1, h2, h8, e⟩ + · have pv : p.val = PLb ∨ p.val = VSb ∨ p.val = L1b ∨ p.val = L2b ∨ p.val = L3b := by + rcases hp with hp | hp + · exact Or.inl (by rw [Fl4ProofsA64.P2.BootEpB.w1_pp lay1 hp, vPL]) + · rcases hp with h | h | h | h <;> simp [h] + have bv : b.val = p.val + o.val := by + rw [e, wa_valm] + exact Nat.mod_eq_of_lt (by rcases pv with h | h | h | h | h <;> omega) + refine ⟨?_, npp b ?_ ?_ ?_⟩ + · show b.val % 8 = 0 + rw [bv]; rcases pv with h | h | h | h | h <;> omega + all_goals (rw [bv]; rcases pv with h | h | h | h | h <;> omega) + · have bv : b.val = KBn + o.val := by + rw [e, wa_valm, ksb2, kbv] + exact Nat.mod_eq_of_lt (by omega) + refine ⟨?_, npp b ?_ ?_ ?_⟩ + · show b.val % 8 = 0 + rw [bv]; omega + all_goals (rw [bv]; omega) + +/-! ## The entry record at the witness -/ + +/-- **The budget prefix is the identity at the witness.** -/ +theorem wit_budget_id (O : Wit2.OpaqueWA) {r mt sl : U64} {uw : U64 → U64} (hsl : Fl4SpecA64.MIN_BUDGET ≤ sl.bv) + (hr : specPtr r = Fl4SpecA64.armGlobalPtPtr) + (a : WA) (ha : Wit2.WClock a (witSA wNow mt sl uw).machineState) : + ∃ a1, syscall.budget_ok bst AR1 (witS r wNow mt sl uw) a = ok (true, witS r wNow mt sl uw, a1) ∧ + sched.update_timestamp bst AR1 (witS r wNow mt sl uw) a = ok (witS r wNow mt sl uw, a1) := by + have J : JSyscallA bst (fun _ _ => True) witZA (KtH (w1H KB IB 1) {bwv KTb}) (w1OS KB IB 1) w1U (w1H KB IB 1).scs + (Fl4ProofsA64.P2.BootEpB.w1Pp KB IB 1 r wNow mt sl uw) w1Tb w1Lv (IB : U64).val ((IB : U64).val + 1 * 8192) + ∅ ∅ ∅ w1BfA (fun _ => []) (w1SchedScCoreA IB) Fl4SpecA64.fl4A64Platform.numNodes (J5A bst) + (J7A bst (J7R2A bst AR1 Wit2.ar1_plat.ipa)) (witS r wNow mt sl uw) a (witSA wNow mt sl uw) (w1MA IB 1) := by + rw [O.nodes] + exact wit_syscallA (fun _ _ => True) Wit2.witArA (a := a) r wNow mt sl uw Wit2.ar1_plat.ipa trivial O.nodes hsl hr + exact J.budgetOkIdA (AR' := fun _ _ => True) wit_hvp Wit2.ar1_now ha O.nd wit_counterStep (wit_hNow O) + (WP.spec_mono (Fl4ProofsA64.P0.sched.update_timestamp.p0 bst AR1 _ a bstTotal Wit2.ar1_total) fun _ _ => trivial) + (Wit2.ar1_fl4MinBudget O) ((J.sr.sch.time).symm.trans rfl) (fun _ e => wit_hsuf O hsl e) (fun b => bwr_self _ b) + +end + +end Fl4ProofsA64.P3.Wit diff --git a/proofs/Fl4ProofsA64/P3/A64Wit2PlatA.lean b/proofs/Fl4ProofsA64/P3/A64Wit2PlatA.lean new file mode 100644 index 000000000..8e704e673 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/A64Wit2PlatA.lean @@ -0,0 +1,85 @@ +/- +P3 (aarch64, A64-P3 non-vacuity witness 2, part 2): **`OpaqueWA` is consistent with the types of the constants it +constrains.** + +`OpaqueWA` (`P3/A64Wit2ArchA`) states facts about `opaque` constants of `Fl4SpecA64` (`kernelConfig`, `timeConsts`, +`fl4TimerFrequencyHz`, `fl4MaxPeriodUs`, `maxTicksToUs`, `irqOracleAx`, `fl4RawCounterOracle`, `fl4A64Platform`). No +Lean term proves or refutes a property of an `opaque` constant (any value of its type is a model), so what can be +shown is that the facts are jointly satisfiable by *values* (pattern: `Fl4Proofs/P2/WitPlat.lean`, `opaqueW_types`): + +* `kernelConfig`: a `KernelConfig` with one domain, `maxIRQ = 5375`, GICv2 (`kernelConfig_ex`); +* `timeConsts`: a `TimeConsts` with WCET 10 µs, 1000 ticks per µs and timer unit 1 (`timer_ex`; those three are the + fields behind `kernelWCETUs`, `ticksPerTimerUnit`, `timerUnit`). `fl4TimerFrequencyHz`, `fl4MaxPeriodUs`, + `maxTicksToUs` are free `Word64`s (`free_ex`). The relations `hag`, `hconv`, `hlimit`, `maxP` are not restated here: + `P3/A64Wit2ArchA` proves them from the real definitions (`fl4MinBudget`, `MIN_BUDGET`, `fl4TicksToUs`, ...) and the + primitive facts above, so nothing is transcribed by hand; +* the IRQ oracle: the constant function `irqVGICMaintenance` is an `IrqOracle` for that `maxIRQ` (`oracle_ex`); +* the raw counter oracle: a monotone function exists (`counter_ex`). +`maxTicksToUs = U64::MAX` and `numNodes = 1` constrain constants of types with no further relation to the above +(`maxTicksToUs : Word64`; `Fl4A64Platform` has the default instance with `numNodes = 1`, `platform_ex`). + +**The values** (strongest form available, since no Lean term proves or refutes a property of an `opaque` constant): a model of +`OpaqueWA` is `kernelConfig := kernelConfig_ex`'s `KernelConfig`, `timeConsts` with WCET 10 µs / 1000 ticks per unit / timer +unit 1 (`timer_ex`), `fl4TimerFrequencyHz = 10^9`, `fl4MaxPeriodUs = MAX_PERIOD_US`, `maxTicksToUs = U64::MAX` +(`free_ex`), `irqOracleAx` the constant `irqVGICMaintenance` oracle (`oracle_ex`), `fl4RawCounterOracle ≡ 0` (monotone, +`counter_ex`), `timeOracle ≡ 0` (so `hNow`: at a state whose last observation is the next raw sample `0`, both clocks give +`0` and the same successor state), `fl4A64Platform := default` (`platform_ex`). The remaining facts (`hag`, `hconv`, +`hlimit`, `maxP`, `v2`) are derived from the real definitions, not assumed. `irq` and `hNow` are witness-world facts. + +Named premises: none new. New module; no existing statement changed. +-/ +import Fl4ProofsA64.P3.A64Wit2ArchA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Wit2 +open Fl4SpecA64 + +noncomputable section + +/-- A `KernelConfig` with one domain, `maxIRQ = 5375` and GICv2. -/ +theorem kernelConfig_ex : ∃ kc : KernelConfig, kc.numDomains = 1 ∧ kc.maxIRQ = 5375 ∧ kc.configArmGicV3 = false := + ⟨{ numDomains := 1, numDomains_not_zero := by decide, numDomains_fits_domainBits := by decide, + resetChunkBits := 8, retypeFanOutLimit := 256, maxIRQ := 5375, maxIRQ_less_2p_irqBits := by decide, + irqVTimerEvent_le_maxIRQ := by decide, physBase := 0x40000000, configArmGicV3 := false, + configDisableWfiWfeTraps := false, l1CacheLineSizeBits := 6, cacheLineBits_sanity := by decide }, + rfl, rfl, rfl⟩ + +/-- A `TimeConsts` with FL4's WCET (10 µs), 1000 ticks per µs and timer unit 1. -/ +def wTCA : TimeConsts := + { kernelWCETUs := 10, ticksPerTimerUnit := 1000, timerUnit := 1, + kernelWCET_us_pos2 := by decide + MIN_BUDGET_le_MAX_PERIOD := by decide + ticks_per_timer_unit_non_zero := by decide + MIN_BUDGET_bound := by decide + getCurrentTime_buffer_bound := by decide + kernelWCET_pos' := by decide + MIN_BUDGET_pos' := by decide + init_domain_time_pos := by decide + init_domain_time_bound := by decide + getCurrentTime_buffer_pos := by decide } + +/-- **The three timer facts on `timeConsts`** (`kernelWCETUs`, `ticksPerTimerUnit`, `timerUnit` are the fields of the +opaque `timeConsts`, by definition): a `TimeConsts` with those values exists. The relations `hag`, `hconv`, `hlimit` +are *proved* from the real definitions in `P3/A64Wit2ArchA` (`OpaqueWA.hag` ...), not transcribed here. -/ +theorem timer_ex : ∃ tc : TimeConsts, tc.kernelWCETUs = 10 ∧ tc.ticksPerTimerUnit = 1000 ∧ tc.timerUnit = 1 := + ⟨wTCA, rfl, rfl, rfl⟩ + +/-- The three free constants: any `Word64` value is a model of `fl4TimerFrequencyHz = 10^9`, +`fl4MaxPeriodUs = MAX_PERIOD_US`, `maxTicksToUs = U64::MAX`. -/ +theorem free_ex : ∃ f p m : Word64, f = 1000000000 ∧ p = MAX_PERIOD_US ∧ m = core.num.U64.MAX.bv := + ⟨_, _, _, rfl, rfl, rfl⟩ + +/-- An `IrqOracle` yielding only the VGIC maintenance line, for `maxIRQ = 5375`. -/ +theorem oracle_ex (h : kernelConfig.maxIRQ = 5375) : ∃ o : IrqOracle, ∀ n, o.oracle n = irqVGICMaintenance := + ⟨⟨fun _ => irqVGICMaintenance, fun _ => by + unfold maxIRQ; rw [h]; decide⟩, fun _ => rfl⟩ + +/-- A monotone raw counter exists. -/ +theorem counter_ex : ∃ f : Nat → Ticks, ∀ i, f i ≤ f (i + 1) := ⟨fun _ => 0, fun _ => BitVec.le_refl _⟩ + +/-- The default platform has one node. -/ +theorem platform_ex : ∃ p : Fl4A64Platform, p.numNodes = 1 := ⟨default, rfl⟩ + +end + +end Fl4ProofsA64.P3.Wit2 diff --git a/proofs/Fl4ProofsA64/P3/A64Wit2TierA.lean b/proofs/Fl4ProofsA64/P3/A64Wit2TierA.lean new file mode 100644 index 000000000..b14522463 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/A64Wit2TierA.lean @@ -0,0 +1,347 @@ +/- +P3 (aarch64, A64-P3 non-vacuity witness 2, W4 / Tier A): **`syscallSend_refinesA` at the witness.** + +At `witS` (with `now = wNow`, the first raw-counter sample; `P3/A64Wit2EntryA`): + +* `wit_entryIn`: the fault-entry record `KEntryFaultInA` (the syscall-entry record at the witness, the clock, the budget + premises); +* `wit_hCharge`: `SysChargeA` is vacuous: the head refill is sufficient, so the exhausted-budget branch is not reached; +* `wit_scope`: `SendScopeA` at the post-budget state, which is `witS` again (`wit_faultIP_id`, `wit_budget_id`): `hFault` + (the root TCB's fault handler is `Null`: `JSyscallA.faultNullA`), `hArm` (the CNode Copy arm: Plan B's + `cnodeCopyArm_scopeA`), `hErr` (the decode never fails: `wit_noDecodeErr`), `HEnv` (`wit_notPreempted`; the poll + `witPoll` is `getActiveIRQ true`, which never reports a line: the IRQ oracle yields the VGIC maintenance interrupt); +* `wit_tierA`: `syscallSend_refinesA` for `Send`, `NBSend` and `Call` at the witness. + +Named premises: `OpaqueWA` [P] (`P3/A64Wit2ArchA`: facts about `opaque` constants; `irq` and `hNow` are witness-world), the +witness parameters (`WitUw uw`, `sl >= MIN_BUDGET`). New module. +-/ +import Fl4ProofsA64.P3.A64Wit2EntryA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Wit +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P1.W7.R.ST.BootWit Fl4ProofsA64.P3 + Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry + +noncomputable section + +open Fl4ProofsA64.P3.Wit2 (WA AR1 OpaqueWA WClock) + +local instance witPlain : PlainStore bst := ⟨fun _ => rfl⟩ + +/-! ## The poll -/ + +/-- The spec's poll of the model: `getActiveIRQ true`, as a word option. -/ +def witPoll : Fl4SpecA64.SMonad Unit (Option Fl4SpecA64.Word) := do + let r ← Fl4SpecA64.doMachineOp (Z := Unit) (Fl4SpecA64.getActiveIRQ true) + pure (r.map fun i => BitVec.ofNat 64 i.toNat) + +/-- The oracle yields a non-kernel line, so `getActiveIRQ true` reports none and counts the read. -/ +theorem getActiveIRQ_none (O : OpaqueWA) (ms : Fl4SpecA64.MachineState) : + Fl4SpecA64.getActiveIRQ true ms = ({(none, { ms with irqState := ms.irqState + 1 })}, False) := by + unfold Fl4SpecA64.getActiveIRQ + simp only [nd_gets_bindSc, nd_modify_bindA] + have hm : Fl4SpecA64.irqOracle (ms.irqState + 1) ∈ Fl4SpecA64.nonKernelIRQs := by + rw [O.irq] + simp [Fl4SpecA64.nonKernelIRQs] + simp [Function.comp, hm] + +theorem witPoll_eq (O : OpaqueWA) (τ : Fl4SpecA64.State Unit) : + witPoll τ = ({(none, { τ with machineState := { τ.machineState with irqState := τ.machineState.irqState + 1 } })}, + False) := by + unfold witPoll + have e : Fl4SpecA64.doMachineOp (Z := Unit) (Fl4SpecA64.getActiveIRQ true) τ = + ({(none, { τ with machineState := { τ.machineState with irqState := τ.machineState.irqState + 1 } })}, False) := by + unfold Fl4SpecA64.doMachineOp + rw [nd_gets_bindSc] + have sel : Fl4Spec.NonDet.selectF (Fl4SpecA64.getActiveIRQ true τ.machineState) τ = + ({((none, { τ.machineState with irqState := τ.machineState.irqState + 1 }), τ)}, False) := by + rw [getActiveIRQ_none O]; exact selectF_oneA _ _ + rw [nd_bind_oneSc sel] + dsimp only + rw [nd_modify_bindA] + rfl + rw [nd_bind_oneSc e] + rfl + +theorem nd_bind_congrW {σ α β : Type} (m : Fl4Spec.NonDet σ α) (F G : α → Fl4Spec.NonDet σ β) + (h : ∀ a s, F a s = G a s) (s : σ) : (m >>= F) s = (m >>= G) s := by + have : F = G := funext fun a => funext (h a) + rw [this] + +theorem witPoll_pending : pendingIrqA witPoll preemptServiceA = + Fl4SpecA64.maybeHandleInterrupt (Z := Unit) true := by + funext σ + unfold pendingIrqA witPoll Fl4SpecA64.maybeHandleInterrupt + refine (nd_bind_assocSc _ _ _ σ).trans ?_ + refine nd_bind_congrW _ _ _ (fun r σ' => ?_) σ + rw [nd_pure_bindSc] + cases r with + | none => rfl + | some i => + simp only [Option.map_some, preemptServiceA] + have : BitVec.ofNat 13 (BitVec.ofNat 64 i.toNat).toNat = i := by + apply BitVec.eq_of_toNat_eq + simp only [BitVec.toNat_ofNat] + have hl : Fl4SpecA64.irqLen = 13 := rfl + have h13 : i.toNat < 2 ^ Fl4SpecA64.irqLen := i.isLt + have h2 : (2 : ℕ) ^ Fl4SpecA64.irqLen = 2 ^ 13 := by rw [hl] + rw [h2] at h13 + have h64 : i.toNat < 2 ^ 64 := lt_trans h13 (by norm_num) + first + | omega + | (rw [Nat.mod_eq_of_lt h64]; exact Nat.mod_eq_of_lt h13) + | simp [Nat.mod_eq_of_lt h64, Nat.mod_eq_of_lt h13] + rw [this] + +theorem wit_pollAgree (O : OpaqueWA) : PollAgreeA AR1 (fun _ _ => True) witPoll := by + intro a c τ _ + have hv : AR1.take_pending_irq a = ok (syscall.NO_IRQ, a) := rfl + rw [hv] + refine (WP.spec_ok _).2 ⟨{ τ with machineState := { τ.machineState with irqState := τ.machineState.irqState + 1 } }, + ?_, ⟨_, rfl, rfl, rfl⟩, trivial, fun h => absurd rfl h⟩ + simp only [if_pos rfl] + rw [witPoll_eq O τ] + exact Set.mem_singleton _ + +/-! ## The fault handler of the root thread is `Null` -/ + +theorem wit_faultCap {r now mt sl : U64} {uw : U64 → U64} : + capA bst (witS r now mt sl uw) (slotIdx (bL.curW (witS r now mt sl uw)) layout.tcb.SLOT_FAULT_HANDLER) = .Null := by + rw [curW2] + have h3 : layout.tcb.SLOT_FAULT_HANDLER.val = 3 := by unfold layout.tcb.SLOT_FAULT_HANDLER; rfl + have v0 : (slotIdx (bwv RTb) layout.tcb.SLOT_FAULT_HANDLER).val = RTb + 96 := by + rw [slotIdx_valG (by rw [vRT, h3]; omega), vRT, h3] + have v1 : (core.num.U64.wrapping_add (slotIdx (bwv RTb) layout.tcb.SLOT_FAULT_HANDLER) 8#u64).val = RTb + 104 := by + rw [wa_valm, v0] + exact Nat.mod_eq_of_lt (by simp) + have w0 : bL.mem (witS r now mt sl uw) (slotIdx (bwv RTb) layout.tcb.SLOT_FAULT_HANDLER) = 0#u64 := by + have := wit_rtWords (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) _ 96 (by norm_num) (by norm_num) + (by rw [v0, vRT]) + rw [this]; simp [witRtW, rtW] + have w1 : bL.mem (witS r now mt sl uw) + (core.num.U64.wrapping_add (slotIdx (bwv RTb) layout.tcb.SLOT_FAULT_HANDLER) 8#u64) = 0#u64 := by + have := wit_rtWords (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) _ 104 (by norm_num) (by norm_num) + (by rw [v1, vRT]) + rw [this]; simp [witRtW, rtW] + show CapRt.decodeF (bL.mem (witS r now mt sl uw) _).bv (bL.mem (witS r now mt sl uw) _).bv = _ + rw [w0, w1] + first | rfl | decide | simp [CapRt.decodeF] + +/-! ## The entry record and the charge -/ + +/-- The record at the entry of the witness. -/ +abbrev WitJ (Z : XSetsA) (r now mt sl : U64) (uw : U64 → U64) (s : BS) (a : WA) (σ : Fl4SpecA64.State Unit) : Prop := + JSyscallA bst (fun _ _ => True) Z (KtH (w1H KB IB 1) {bwv KTb}) (w1OS KB IB 1) w1U (w1H KB IB 1).scs + (Fl4ProofsA64.P2.BootEpB.w1Pp KB IB 1 r now mt sl uw) w1Tb w1Lv (IB : U64).val ((IB : U64).val + 1 * 8192) + ∅ ∅ ∅ w1BfA (fun _ => []) (w1SchedScCoreA IB) Fl4SpecA64.fl4A64Platform.numNodes (J5A bst) + (J7A bst (J7R2A bst AR1 Wit2.ar1_plat.ipa)) s a σ (w1MA IB 1) + +theorem wit_entryIn (O : OpaqueWA) {r mt sl : U64} {uw : U64 → U64} (hsl : Fl4SpecA64.MIN_BUDGET ≤ sl.bv) + (hr : specPtr r = Fl4SpecA64.armGlobalPtPtr) (a : WA) + (ha : WClock a (witSA wNow mt sl uw).machineState) : + KEntryFaultInA bst AR1 (fun _ _ => True) (fun _ _ => True) witZA (KtH (w1H KB IB 1) {bwv KTb}) (w1OS KB IB 1) w1U + (w1H KB IB 1).scs (Fl4ProofsA64.P2.BootEpB.w1Pp KB IB 1 r wNow mt sl uw) w1Tb w1Lv (IB : U64).val + ((IB : U64).val + 1 * 8192) ∅ ∅ ∅ w1BfA (fun _ => []) (w1SchedScCoreA IB) Wit2.ar1_plat.ipa (w1MA IB 1) WClock + (witS r wNow mt sl uw) a (witSA wNow mt sl uw) where + J := by + rw [O.nodes] + exact wit_syscallA (fun _ _ => True) Wit2.witArA (a := a) r wNow mt sl uw Wit2.ar1_plat.ipa trivial O.nodes hsl hr + hvp := wit_hvp + P := Wit2.ar1_now + hClock := ha + hnd := O.nd + C := wit_counterStep + hC := O.hC + hNow := wit_hNow O + hAr := WP.spec_mono (Fl4ProofsA64.P0.sched.update_timestamp.p0 bst AR1 _ a bstTotal Wit2.ar1_total) fun _ _ => trivial + hmb := Wit2.ar1_fl4MinBudget O + +/-- The `FAULT_IP` write leaves the spec state alone: the witness's `FAULT_IP` register is `NEXT_IP - 4`. -/ +theorem wit_pc {mt sl : U64} {uw : U64 → U64} : + Fl4SpecA64.fl4SyscallFaultIP (Z := Unit) (witSA wNow mt sl uw).curThread (witSA wNow mt sl uw) = + ({((), witSA wNow mt sl uw)}, False) := by + have hk : (witSA wNow mt sl uw).kheap (witSA wNow mt sl uw).curThread = some (.tcb witTcbA) := + Function.update_self _ _ _ + rw [faultIP_eqA hk] + have e : setReg witTcbA Fl4SpecA64.faultRegister + (witTcbA.tcbArch.tcbContext.userRegs Fl4SpecA64.nextInstructionRegister - 4) = witTcbA := by + have h : witTcbA.tcbArch.tcbContext.userRegs Fl4SpecA64.nextInstructionRegister - 4 = + witTcbA.tcbArch.tcbContext.userRegs Fl4SpecA64.faultRegister := by decide + rw [h] + unfold setReg + have hr : Function.update (Fl4SpecA64.archTcbContextGet witTcbA.tcbArch).userRegs Fl4SpecA64.faultRegister + (witTcbA.tcbArch.tcbContext.userRegs Fl4SpecA64.faultRegister) = + (Fl4SpecA64.archTcbContextGet witTcbA.tcbArch).userRegs := Function.update_eq_self _ _ + simp only [setRegUc, hr] + rfl + rw [e] + unfold updK + rw [Function.update_eq_self_iff.2 hk.symm] + +/-- **`SysChargeA` at the witness**: the head refill is sufficient, so the exhausted-budget branch is vacuous. -/ +theorem wit_hCharge (O : OpaqueWA) {r mt sl : U64} {uw : U64 → U64} (hsl : Fl4SpecA64.MIN_BUDGET ≤ sl.bv) (a : WA) : + SysChargeA bst AR1 (fun _ _ => True) (fun _ _ => True) witZA (KtH (w1H KB IB 1) {bwv KTb}) (w1OS KB IB 1) w1U + (w1H KB IB 1).scs (Fl4ProofsA64.P2.BootEpB.w1Pp KB IB 1 r wNow mt sl uw) w1Tb w1Lv (IB : U64).val + ((IB : U64).val + 1 * 8192) ∅ ∅ ∅ w1BfA (fun _ => []) (w1SchedScCoreA IB) Wit2.ar1_plat.ipa (w1MA IB 1) WClock + (witS r wNow mt sl uw) a (witSA wNow mt sl uw) := by + unfold SysChargeA + intro Q Rl τPC hpc J + have hτ : τPC = witSA wNow mt sl uw := by + have := hpc.symm.trans wit_pc + exact (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst this))).2 + subst hτ + unfold KEntryChargeA + intro s1 a1 σ1 e _ _ _ _ _ _ _ suf + have h := wit_hsuf O hsl e + rw [h] at suf + exact absurd (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst suf))).1 (by decide) + + +/-! ## The scope -/ + +theorem wit_hCs (O : OpaqueWA) : ∀ c ∈ witZA.Cs, c.val < Fl4SpecA64.fl4A64Platform.numNodes := fun c hc => by + rw [O.nodes]; exact hc + +/-- The root thread's fault handler is `Null` at the spec's state too (`getCap` of the handler slot). -/ +theorem wit_faultHandler_null {Z : XSetsA} {r mt sl : U64} {uw : U64 → U64} {ab : WA} {τ : Fl4SpecA64.State Unit} + (Jr : WitJ Z r wNow mt sl uw (witS r wNow mt sl uw) ab τ) : + Fl4SpecA64.getCap (Z := Unit) (Fl4SpecA64.getTcbFaultHandlerPtr τ.curThread) τ = + ({(Fl4SpecA64.Cap.nullCap, τ)}, False) := by + have ct : τ.curThread = specPtr (bL.curW (witS r wNow mt sl uw)) := Jr.j.1.sch.curThread + have htZ : bL.curW (witS r wNow mt sl uw) ∈ Z.T := Jr.sr.curT + obtain ⟨x, hk, hs⟩ := Jr.j.1.st.tcb _ ((Jr.j.1.thT _).2 htZ) + obtain ⟨ac, e, hrel⟩ := hs 3 (by unfold tcbSlotsA; omega) + have h3 : layout.tcb.SLOT_FAULT_HANDLER.val = 3 := by unfold layout.tcb.SLOT_FAULT_HANDLER; rfl + have ev : slotIdx (bL.curW (witS r wNow mt sl uw)) layout.tcb.SLOT_FAULT_HANDLER = + slotAddrA (specPtr (bL.curW (witS r wNow mt sl uw))) (BitVec.ofNat 64 3) := by + rw [slotIdx_addrA, h3] + have hc := wit_faultCap (r := r) (now := wNow) (mt := mt) (sl := sl) (uw := uw) + rw [ev] at hc + have hrel' : CapRelA (capA bst (witS r wNow mt sl uw) (slotAddrA (specPtr (bL.curW (witS r wNow mt sl uw))) (BitVec.ofNat 64 3))) ac := hrel + rw [hc] at hrel' + have hrel := hrel' + have hac : ac = Fl4SpecA64.Cap.nullCap := hrel.2.symm + subst hac + rw [ct] + exact getCap_tcb_eqA hk e + +/-- **`SendScopeA` at the witness** (module docstring). -/ +theorem wit_scope (O : OpaqueWA) {r mt sl : U64} {uw : U64 → U64} (U : WitUw uw) (rep : Bool) + (call : Fl4SpecA64.Syscall) {Q : U64 → U64 → List U64} {Rl : U64 → List U64} {ab : WA} + {τ : Fl4SpecA64.State Unit} + (Jr : WitJ { witZA with Q := Q, R := Rl } r wNow mt sl uw (witS r wNow mt sl uw) ab τ) : + SendScopeA bst AR1 (fun _ _ => True) { witZA with Q := Q, R := Rl } (KtH (w1H KB IB 1) {bwv KTb}) (w1OS KB IB 1) + w1U (w1H KB IB 1).scs (Fl4ProofsA64.P2.BootEpB.w1Pp KB IB 1 r wNow mt sl uw) w1Tb w1Lv (IB : U64).val + ((IB : U64).val + 1 * 8192) ∅ ∅ ∅ w1BfA (fun _ => []) (w1SchedScCoreA IB) Fl4SpecA64.fl4A64Platform.numNodes + (witS r wNow mt sl uw) ab τ (w1MA IB 1) WClock Wit2.ar1_plat rep (Fl4SpecA64.maybeHandleInterrupt true) call := by + have hfm : A64FrameMasks bst (witS r wNow mt sl uw) := wit_frameMasks + refine { hFault := ?_, hArm := ?_, hErr := ?_, HEnv := ?_ } + · intro calling blocking donate _ _ f f' _ + obtain ⟨σ2, s2, hm, hf, B, Q2, R2, J2⟩ := Jr.faultNullA Wit2.ar1_total Wit2.ar1_plat (wit_hCs O) O.nodes f f' + wit_faultCap (wit_faultHandler_null Jr) + rw [hf] + exact (WP.spec_ok _).2 ⟨σ2, hm, A64RelS3.ofJ J2 B.1⟩ + · intro calling blocking donate _ info buf r1 e mi sl' c excaps buffer argsU einfo ebuf elr _ Q' r0 s0 a0 hdec0 hgood + obtain ⟨hlb, p, rx, gs, g, hslc⟩ := wit_hSel info buf r1 einfo ebuf elr + have hr0 : r0 = .Ok () := by + cases r0 with + | Ok u => cases u; rfl + | Err x => exact absurd (hgood x rfl) (Jr.cnCopy_notPreemptedA Q' hslc hlb hdec0) + subst hr0 + have h21 := lblA_copy + have h25 := lblA_mint + have h26 := lblA_move + have h27 := lblA_mutate + have h28 := lblA_rotate + by_cases hrt : info.label = fl4_abi.label.CNodeRotate + · exact DispScopeA.of4 (Jr.cnodeRotate_runA Wit2.ar1_total Wit2.ar1_spi Wit2.ar1_plat hfm Q' hslc hrt hdec0) + by_cases hcm : info.label = fl4_abi.label.CNodeCopy ∨ info.label = fl4_abi.label.CNodeMint + · obtain ⟨X⟩ := Jr.cnCopy_armA Q' hslc hcm hdec0 + exact DispScopeA.of4 (Jr.cnodeCopyArm_scopeA Wit2.ar1_total Wit2.ar1_spi Wit2.ar1_plat hfm Q' X blocking calling + donate) + · have hmm : info.label = fl4_abi.label.CNodeMove ∨ info.label = fl4_abi.label.CNodeMutate := by + rw [u64_eq_iffA] at hrt + rw [u64_eq_iffA, u64_eq_iffA] at hcm ⊢ + omega + exact DispScopeA.of4 (Jr.cnodeMove_runA Wit2.ar1_total Wit2.ar1_spi Wit2.ar1_plat hfm Q' hslc hmm hdec0) + · intro calling blocking donate _ info buf r1 e x s' a' h1 h2 h3 h4 hdec _ + exact (wit_noDecodeErr U h1 h2 h3 h4 hdec).elim + · refine + { K := Wit2.ar1_irqArch O + hMax := O.maxIRQ + hn := rfl + hLimit := fun a' => Wit2.ar1_limit O a' + hSend := fun calling blocking donate s' a' τ' _ hh => (wit_notPreempted U hh).elim + poll := ⟨witPoll, wit_pollAgree O, witPoll_pending, ?_⟩ } + intro s' a' r' τ' irq a'' τ'' _ _ ht hne _ + have hv : AR1.take_pending_irq r'.2 = ok (syscall.NO_IRQ, r'.2) := rfl + rw [hv] at ht + exact absurd (congrArg Prod.fst (ok_inj_ff ht)).symm hne + +/-! ## The entry runs -/ + +/-- **`kRuns` at the witness**: the syscall number is in range and the entry's `FAULT_IP` write finds the thread's +`TRAP_SYSCALLS` flag clear (its flags word is zero). -/ +theorem wit_kRuns {r mt sl : U64} {uw : U64 → U64} (number : I64) (rep : Bool) (call : Fl4SpecA64.Syscall) + (hnum : number = syscallNumberA call) (hc : call = .sysSend ∨ call = .sysNBSend ∨ call = .sysCall) : + kRuns bst (witS r wNow mt sl uw) (.syscall number rep call) := by + have rng : ¬ number < fl4_abi.syscall.SYSCALL_MIN ∧ ¬ number > fl4_abi.syscall.SYSCALL_MAX := by + subst hnum + rcases hc with rfl | rfl | rfl <;> simp only [syscallNumberA] <;> + first | decide | scalar_tac | (constructor <;> simp [fl4_abi.syscall.SYSCALL_MIN, fl4_abi.syscall.SYSCALL_MAX, + fl4_abi.syscall.SysSend, fl4_abi.syscall.SysNBSend, fl4_abi.syscall.SysCall] <;> decide) + have ct : sched.cur_thread bst (witS r wNow mt sl uw) = ok (bwv RTb) := by rw [cur_thread_ok, curW2] + have hset : obj.Tcb.set_reg bst (bwv RTb) (witS r wNow mt sl uw) layout.reg.FAULT_IP + (core.num.U64.wrapping_sub (bL.mem (witS r wNow mt sl uw) (regWA (bwv RTb) layout.reg.NEXT_IP)) 4#u64) = + ok (bL.wr (witS r wNow mt sl uw) (Endp.regA (bwv RTb) layout.reg.FAULT_IP) + (core.num.U64.wrapping_sub (bL.mem (witS r wNow mt sl uw) (regWA (bwv RTb) layout.reg.NEXT_IP)) 4#u64)) := by + obtain ⟨q, hq, e⟩ := spec_existsA (obj.Tcb.set_reg.spec (st := bst) (witS r wNow mt sl uw) (bwv RTb) + layout.reg.FAULT_IP (core.num.U64.wrapping_sub (bL.mem (witS r wNow mt sl uw) + (regWA (bwv RTb) layout.reg.NEXT_IP)) 4#u64)) + rw [hq, e] + have fid : bL.wr (witS r wNow mt sl uw) (Endp.regA (bwv RTb) layout.reg.FAULT_IP) + (core.num.U64.wrapping_sub (bL.mem (witS r wNow mt sl uw) (regWA (bwv RTb) layout.reg.NEXT_IP)) 4#u64) = + witS r wNow mt sl uw := by + have := wit_faultIP_id (r := r) (now := wNow) (mt := mt) (sl := sl) (uw := uw) + rwa [curW2] at this + have hf : bL.mem (witS r wNow mt sl uw) (core.num.U64.wrapping_add (bwv RTb) 3456#u64) = 0#u64 := by + have v : (core.num.U64.wrapping_add (bwv RTb) 3456#u64).val = (bwv RTb).val + 3456 := by + rw [wa_valm, vRT]; exact Nat.mod_eq_of_lt (by simp) + have := wit_rtWords (r := r) (now := wNow) (mt := mt) (sl := sl) (uw := uw) _ 3456 (by norm_num) (by norm_num) v + rw [this]; simp [witRtW, rtW] + refine ⟨rng.1, rng.2, bwv RTb, _, witS r wNow mt sl uw, 0#u64, ct, tcb_reg_eqA _ _ _, hset.trans (by rw [fid]), ?_, ?_⟩ + · rw [flags_ok, hf] + · first | simp | (apply UScalar.eq_of_val_eq; simp) + +/-- The witness's `r` exists: FL4's pointer of the spec's global page-table pointer. -/ +theorem wit_r_ex : ∃ r : U64, specPtr r = Fl4SpecA64.armGlobalPtPtr := + ⟨fl4Ptr Fl4SpecA64.armGlobalPtPtr, by + unfold specPtr fl4Ptr + exact BitVec.sub_add_cancel _ _⟩ + +/-! ## Tier A -/ + +set_option maxHeartbeats 4000000 in +/-- **`syscallSend_refinesA` at the witness**, for `Send`, `NBSend` and `Call`. -/ +theorem wit_tierA (O : OpaqueWA) {r mt sl : U64} {uw : U64 → U64} (U : WitUw uw) + (hsl : Fl4SpecA64.MIN_BUDGET ≤ sl.bv) (hr : specPtr r = Fl4SpecA64.armGlobalPtPtr) (a : WA) (ha : WClock a (witSA wNow mt sl uw).machineState) + (number : I64) (rep : Bool) (call : Fl4SpecA64.Syscall) (hnum : number = syscallNumberA call) + (hc : call = .sysSend ∨ call = .sysNBSend ∨ call = .sysCall) : + kBodyA bst AR1 (witS r wNow mt sl uw) a (.syscall number rep call) ⦃ res => + StepPostA (fun s' a' τ => A64RelS3 bst (fun _ _ => True) AR1 Wit2.ar1_plat.ipa + Fl4SpecA64.fl4A64Platform.numNodes witZA.Cs s' a' τ (w1MA IB 1)) + (kOpA (.syscall number rep call)) (witSA wNow mt sl uw) res ⦄ := + syscallSend_refinesA Wit2.ar1_total Wit2.ar1_spi Wit2.ar1_plat ⟨fun _ => (WP.spec_ok _).2 rfl⟩ + (Wit2.ar1_faultEnv O witZA (wit_hCs O)) O.hlimit O.hag O.hconv O.v2 number rep call hnum hc + (wit_entryIn O hsl hr a ha) (wit_hCharge O hsl a) + (fun Q Rl sb ab τ τPC _ eb _ Jr _ => by + obtain ⟨a1, hb, -⟩ := wit_budget_id O hsl hr a ha + rw [wit_faultIP_id, hb] at eb + have e := ok_inj_ff eb + have hsb : sb = witS r wNow mt sl uw := (congrArg (fun p => p.2.1) e).symm + subst hsb + exact wit_scope O U rep call Jr) + +end + +end Fl4ProofsA64.P3.Wit diff --git a/proofs/Fl4ProofsA64/P3/A64Wit2TopA.lean b/proofs/Fl4ProofsA64/P3/A64Wit2TopA.lean new file mode 100644 index 000000000..c35a3376a --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/A64Wit2TopA.lean @@ -0,0 +1,182 @@ +/- +P3 (aarch64, A64-P3 non-vacuity witness 2, Tier B): **`kernelEntrySend_recGA` at the witness.** + +* `SendEntryHyps`: *every* premise of `kernelEntrySend_recGA`, bundled as a structure (the binders copied verbatim); + `SendEntryHyps.concl` applies the theorem to it, so the bundle is exactly the theorem's premises. +* `wit_top_send`: an inhabitant of `SendEntryHyps` at the witness together with the theorem's conclusion there: + the store `witS`, the arch state `a`, the spec state `witSA`, the ghost sets `witZA`, `KtH ..`, `w1OS ..`, ... satisfy + each premise (`ArchTotal`, `MaxSpi`, `ArchPlat`, `IrqPlatA`, `FaultEnvA`, the four platform agreements, `witArchSwOf`, + `FinPlatA`, the call, `kRuns`, `KEntryFaultInA`, `SysChargeA`, the scope, `hGap`, `hNat`), and `kCodeA ... (.syscall ..)` + ends in `StepPostA (KExitA ..)`. + +Named premises: `OpaqueWA` [P], the witness parameters. New module. +-/ +import Fl4ProofsA64.P3.A64Wit2TierA +import Fl4ProofsA64.P3.A64Wit2bBodyA +import Fl4ProofsA64.P3.A64Wit2bGapA +import Fl4ProofsA64.P3.A64ArchSwH +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + +/-- **The premises of `kernelEntrySend_recGA`** (module docstring). -/ +structure SendEntryHyps (st : machine.Store S) [L : LawfulStore st] (ar : machine.Arch A) + (AR AR' : A → HwRootA → Prop) (Z : XSetsA) (H : Heap) (OS U Ac Pp Tb : Set U64) (Lv : U64 → Nat) (ia ie : Nat) + (N E Rp : Set U64) (Bf : Set (U64 × Fl4SpecA64.Word)) (stack : U64 → List U64) (κ : U64 → U64) (m : McState) + (Clock : A → MachineState → Prop) (s : S) (a : A) (σ : State Unit) (tm : A → Nat → DeadlineViewA) + (view : State Unit → Fl4TimerView) (number : I64) (rep : Bool) (call : Syscall) : Type where + plain : PlainStore st + hA : P0.ArchTotal ar + hM : MaxSpi ar + hPl : ArchPlat ar + hIP : IrqPlatA st ar + Env : FaultEnvA st ar Z + hlimit : fl4MaxReleaseTime = MAX_RELEASE_TIME + hag : fl4MinBudget = MIN_BUDGET + hconv : Fl4TicksToUsLegacyAgree + v2 : config_ARM_GIC_V3 = false + arch : ArchSwFA st ar + P : FinPlatA ar AR' + hnum : number = syscallNumberA call + hc : call = .sysSend ∨ call = .sysNBSend ∨ call = .sysCall + run : kRuns st s (.syscall number rep call) + I : KEntryFaultInA st ar AR AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ hPl.ipa m Clock s a σ + hCharge : SysChargeA st ar AR AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ hPl.ipa m Clock s a σ + hScope : ∀ (Q : U64 → U64 → List U64) (Rl : U64 → List U64) sb ab τ (τPC : State Unit), + fl4SyscallFaultIP (Z := Unit) σ.curThread σ = ({((), τPC)}, False) → + syscall.budget_ok st ar (L.wr s (Endp.regA (L.curW s) layout.reg.FAULT_IP) + (core.num.U64.wrapping_sub (L.mem s (regWA (L.curW s) layout.reg.NEXT_IP)) 4#u64)) a = ok (true, sb, ab) → + fl4BudgetOk (Z := Unit) τPC = ({(true, τ)}, False) → + JSyscallA st AR' { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes + (J5A st) (J7A st (J7R2A st ar hPl.ipa)) sb ab τ m → CounterStepA τ → + SendScopeA st ar AR' { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ + fl4A64Platform.numNodes sb ab τ m Clock hPl rep (maybeHandleInterrupt true) call + hGap : ∀ s' a' τ, kBodyA st ar s a (.syscall number rep call) = ok (s', a') → + ((), τ) ∈ (kOpA (.syscall number rep call) σ).1 → + A64RelS3 st AR' ar hPl.ipa fl4A64Platform.numNodes Z.Cs s' a' τ m → SchedGapFreeA st m s' a' τ ar tm view + hNat : ∀ s' a' τ, kBodyA st ar s a (.syscall number rep call) = ok (s', a') → + ((), τ) ∈ (kOpA (.syscall number rep call) σ).1 → + A64RelS3 st AR' ar hPl.ipa fl4A64Platform.numNodes Z.Cs s' a' τ m → ∀ τ1 τ2, + ((), τ1) ∈ (fl4Schedule kLimitA view τ).1 → ((), τ2) ∈ (fl4ActivateThread (Z := Unit) τ1).1 → + ∀ y, τ2.kheap τ2.curThread = some (.tcb y) → y.tcbState ≠ .ext .runningVM + +variable {AR AR' : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {m : McState} {Clock : A → MachineState → Prop} {s : S} {a : A} {σ : State Unit} + {tm : A → Nat → DeadlineViewA} {view : State Unit → Fl4TimerView} {number : I64} {rep : Bool} {call : Syscall} + +/-- The premises give `kernelEntrySend_recGA`'s conclusion. -/ +theorem SendEntryHyps.concl (h : SendEntryHyps st ar AR AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ m Clock s a σ + tm view number rep call) : + kCodeA st ar s a (.syscall number rep call) ⦃ out => + StepPostA (KExitA st ar AR' h.hPl.ipa m) + (kOpA (.syscall number rep call) >>= fun _ => fl4Finish kLimitA view) σ out ⦄ := by + haveI := h.plain + exact kernelEntrySend_recGA h.hA h.hM h.hPl h.hIP h.Env h.hlimit h.hag h.hconv h.v2 h.arch h.P number rep call h.hnum + h.hc h.run h.I h.hCharge h.hScope h.hGap h.hNat + +end + +end + +end Fl4ProofsA64.P3 + +namespace Fl4ProofsA64.P3.Wit +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P1.W7.R.ST.BootWit Fl4ProofsA64.P3 + Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry + Fl4Spec Fl4SpecA64 + +noncomputable section + + +set_option maxHeartbeats 4000000 in +/-- **`kernelEntrySend_recGA` at the witness** (module docstring): the premises and the conclusion at one state. -/ +theorem wit_top_send (O : Wit2.OpaqueWA) {r mt sl : U64} {uw : U64 → U64} (U : WitUw uw) + (hsl : Fl4SpecA64.MIN_BUDGET ≤ sl.bv) (hr : specPtr r = Fl4SpecA64.armGlobalPtPtr) (a : Wit2.WA) + (ha : Wit2.WClock a (witSA wNow mt sl uw).machineState) (number : I64) (rep : Bool) (call : Fl4SpecA64.Syscall) + (hnum : number = syscallNumberA call) (hc : call = .sysSend ∨ call = .sysNBSend ∨ call = .sysCall) : + ∃ Hy : SendEntryHyps bst Wit2.AR1 (fun _ _ => True) (fun _ _ => True) witZA (KtH (w1H KB IB 1) {bwv KTb}) + (w1OS KB IB 1) w1U (w1H KB IB 1).scs (Fl4ProofsA64.P2.BootEpB.w1Pp KB IB 1 r wNow mt sl uw) w1Tb w1Lv + (IB : U64).val ((IB : U64).val + 1 * 8192) ∅ ∅ ∅ w1BfA (fun _ => []) (w1SchedScCoreA IB) (w1MA IB 1) Wit2.WClock + (witS r wNow mt sl uw) a (witSA wNow mt sl uw) Wit2.witTm + (gapViewA bst Wit2.AR1 (kBodyA bst Wit2.AR1 (witS r wNow mt sl uw) a (.syscall number rep call))) number rep call, + kCodeA bst Wit2.AR1 (witS r wNow mt sl uw) a (.syscall number rep call) ⦃ out => + StepPostA (KExitA bst Wit2.AR1 (fun _ _ => True) Wit2.ar1_plat.ipa (w1MA IB 1)) + (kOpA (.syscall number rep call) >>= fun _ => fl4Finish kLimitA + (gapViewA bst Wit2.AR1 (kBodyA bst Wit2.AR1 (witS r wNow mt sl uw) a (.syscall number rep call)))) + (witSA wNow mt sl uw) out ⦄ := by + let Hy : SendEntryHyps bst Wit2.AR1 (fun _ _ => True) (fun _ _ => True) witZA (KtH (w1H KB IB 1) {bwv KTb}) + (w1OS KB IB 1) w1U (w1H KB IB 1).scs (Fl4ProofsA64.P2.BootEpB.w1Pp KB IB 1 r wNow mt sl uw) w1Tb w1Lv + (IB : U64).val ((IB : U64).val + 1 * 8192) ∅ ∅ ∅ w1BfA (fun _ => []) (w1SchedScCoreA IB) (w1MA IB 1) Wit2.WClock + (witS r wNow mt sl uw) a (witSA wNow mt sl uw) Wit2.witTm + (gapViewA bst Wit2.AR1 (kBodyA bst Wit2.AR1 (witS r wNow mt sl uw) a (.syscall number rep call))) number rep call := + { plain := ⟨fun _ => rfl⟩ + hA := Wit2.ar1_total + hM := Wit2.ar1_spi + hPl := Wit2.ar1_plat + hIP := ⟨fun _ => (WP.spec_ok _).2 rfl⟩ + Env := Wit2.ar1_faultEnv O witZA (wit_hCs O) + hlimit := O.hlimit + hag := O.hag + hconv := O.hconv + v2 := O.v2 + arch := Wit2.witArchSwOf + P := Wit2.ar1_finPlat O + hnum := hnum + hc := hc + run := wit_kRuns number rep call hnum hc + I := wit_entryIn O hsl hr a ha + hCharge := wit_hCharge O hsl a + hScope := fun Q Rl sb ab τ τPC _ eb _ Jr _ => by + obtain ⟨a1, hb, -⟩ := wit_budget_id O hsl hr a ha + rw [wit_faultIP_id, hb] at eb + have e := ok_inj_ff eb + have hsb : sb = witS r wNow mt sl uw := (congrArg (fun p => p.2.1) e).symm + subst hsb + exact wit_scope O U rep call Jr + hGap := Wit2.wit2b_hGapA O Wit2.witArchSwOf (wit_hCs O) _ a _ (.syscall number rep call) + hNat := wit2b_hNatA O U hsl hr a ha number rep call hc } + exact ⟨Hy, Hy.concl⟩ + + +/-! ## Every witness-side parameter is satisfiable -/ + +/-- A concrete model arch state carrying a given machine state (the rest is the trivial view). -/ +def witA0 (ms : MachineState) : Wit2.WA where + ms := ms + dl := ⟨.el1, 0, 0⟩ + dlO := ⟨.el1, 0, 0⟩ + s1 := (0#u64, 0#u64) + hi := (0#u64, 0#u64) + s2 := (0#u64, 0#u64) + fpu := fun _ => ⟨none, fun _ => default, default, fun _ => 0, .enabled, false⟩ + vcpu := fun _ => ⟨none, false, fun _ => default, default, none, false, false⟩ + +/-- **Non-vacuity of `kernelEntrySend_recGA`** (module docstring): with `OpaqueWA` the only hypothesis, every witness-side +parameter is existential and instantiated by a concrete value: `r` from `wit_r_ex`, `uw` from `witUw_ex`, `sl = MIN_BUDGET`, +`mt = 0`, the arch state `witA0` of `witSA`'s machine state, `call = Send`, `number = syscallNumberA .sysSend`. -/ +theorem wit_send_nonvacuous (O : Wit2.OpaqueWA) : + ∃ (r mt sl : U64) (uw : U64 → U64) (a : Wit2.WA) (number : I64) (rep : Bool) (call : Syscall), + Nonempty (SendEntryHyps bst Wit2.AR1 (fun _ _ => True) (fun _ _ => True) witZA (KtH (w1H KB IB 1) {bwv KTb}) + (w1OS KB IB 1) w1U (w1H KB IB 1).scs (Fl4ProofsA64.P2.BootEpB.w1Pp KB IB 1 r wNow mt sl uw) w1Tb w1Lv + (IB : U64).val ((IB : U64).val + 1 * 8192) ∅ ∅ ∅ w1BfA (fun _ => []) (w1SchedScCoreA IB) (w1MA IB 1) + Wit2.WClock (witS r wNow mt sl uw) a (witSA wNow mt sl uw) Wit2.witTm + (gapViewA bst Wit2.AR1 (kBodyA bst Wit2.AR1 (witS r wNow mt sl uw) a (.syscall number rep call))) + number rep call) := by + obtain ⟨r, hr⟩ := wit_r_ex + obtain ⟨uw, U⟩ := witUw_ex + obtain ⟨Hy, -⟩ := wit_top_send O (r := r) (mt := 0#u64) (sl := ⟨Fl4SpecA64.MIN_BUDGET⟩) (uw := uw) U + (BitVec.le_refl _) hr (witA0 (witSA wNow 0#u64 ⟨Fl4SpecA64.MIN_BUDGET⟩ uw).machineState) rfl + (syscallNumberA .sysSend) true .sysSend rfl (Or.inl rfl) + exact ⟨r, 0#u64, ⟨Fl4SpecA64.MIN_BUDGET⟩, uw, _, syscallNumberA .sysSend, true, .sysSend, ⟨Hy⟩⟩ + +end + +end Fl4ProofsA64.P3.Wit diff --git a/proofs/Fl4ProofsA64/P3/A64Wit2bBodyA.lean b/proofs/Fl4ProofsA64/P3/A64Wit2bBodyA.lean new file mode 100644 index 000000000..c80cbf1d3 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/A64Wit2bBodyA.lean @@ -0,0 +1,355 @@ +/- +P3 (aarch64, A64-P3 non-vacuity witness 2b): **no thread runs a guest at the witness's body end, and `hNat`**. + +The body of the witness's `Send` / `NBSend` / `Call` is the CNode Copy. `wit_bodyNoVm` follows every outcome of the +spec operation `kOpA` from `witSA`: the `FAULT_IP` write is the identity, a budget prefix that answers `true` writes no +TCB, the front lookups and the decode each have one outcome (`cnCopy_decodeEqA`: an `insertCall`), and the perform +(`capInsert`) and the epilogue create no `runningVM` thread. No preemption: the operation never throws. + +`wit2b_hNatA` is the premise `hNat` of `kernelEntry_recGA` / `kernelEntrySend_recGA` at the witness. + +Premises: those of `wit_tierA` ([P] `OpaqueWA`, the witness parameters). New module. +-/ +import Fl4ProofsA64.P3.A64Wit2TierA +import Fl4ProofsA64.P3.A64Wit2bNatA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P3 Classical + +noncomputable section + +/-! ## Operations that never throw and create no `runningVM` thread -/ + +/-- Every outcome of `m` is a value satisfying `P`, at a state with no new `runningVM` thread. -/ +def OkVmA {ε α : Type} (P : α → Prop) (m : NonDetE (State Unit) ε α) : Prop := + ∀ s x, x ∈ (m.run s).1 → (∃ a, x.1 = .ok a ∧ P a) ∧ VmKA s x.2 + +theorem OkVmA.bind {ε α β : Type} {P : α → Prop} {Q : β → Prop} {m : NonDetE (State Unit) ε α} + {f : α → NonDetE (State Unit) ε β} (hm : OkVmA P m) (hf : ∀ a, P a → OkVmA Q (f a)) : OkVmA Q (m >>= f) := by + intro s x hx + rw [ExceptT.run_bind] at hx + obtain ⟨y, hy, hx⟩ := mem_bindA.1 hx + obtain ⟨⟨a, e, pa⟩, k⟩ := hm s y hy + rw [e] at hx + obtain ⟨v, k'⟩ := hf a pa y.2 x hx + exact ⟨v, PreO.trans k k'⟩ + +theorem OkVmA.lift {ε α : Type} {m : SMonad Unit α} (h : PresR VmKA m) : + OkVmA (fun _ => True) (NonDetE.liftE (ε := ε) m) := by + intro s x hx + unfold NonDetE.liftE at hx + rw [ExceptT.run_lift] at hx + obtain ⟨y, hy, hx⟩ := mem_bindA.1 hx + obtain rfl := mem_pureA hx + exact ⟨⟨y.1, rfl, trivial⟩, h s y hy⟩ + +theorem OkVmA.pure {ε α : Type} {P : α → Prop} {a : α} (h : P a) : + OkVmA P (Pure.pure a : NonDetE (State Unit) ε α) := by + intro s x hx + obtain rfl := mem_pureA (show x ∈ (NonDet.pure (Except.ok a) s).1 from hx) + exact ⟨⟨a, rfl, h⟩, PreO.refl _⟩ + +/-- A lifted step that moves the state. -/ +theorem run_lift_bindA {ε α β : Type} {m : SMonad Unit α} {a : α} {s s' : State Unit} (h : m s = ({(a, s')}, False)) + (f : α → NonDetE (State Unit) ε β) : (NonDetE.liftE (ε := ε) m >>= f).run s = (f a).run s' := by + have e : (NonDetE.liftE (ε := ε) m).run s = ({(.ok a, s')}, False) := by + unfold NonDetE.liftE + rw [ExceptT.run_lift] + show (m >>= _) s = _ + rw [nd_bind_oneSc h] + rfl + rw [ExceptT.run_bind, nd_bind_oneSc e] + +/-! ## The leaves of CNode Copy -/ + +instance (c : Cap) (sl : CslotPtr) : VPresInst (setCap (Z := Unit) c sl) := + ⟨fun s x hx p y hy v => by + obtain ⟨-, -, -, -, -, kb, -⟩ := setCap_tkeepA c sl s x hx + obtain ⟨z, hz, -, e⟩ := kb p y hy + exact ⟨z, hz, e ▸ v⟩⟩ + +instance (t : Cdt) : VPresInst (setCdt (Z := Unit) t) := + ⟨fun s x hx p y hy v => by + unfold setCdt at hx + obtain ⟨z, hz, hx⟩ := mem_bindA.1 hx + obtain rfl : z = (s, s) := hz + obtain rfl : x = ((), { s with cdt := t }) := hx + exact ⟨y, hy, v⟩⟩ + +instance (c : Cap) (src dst : CslotPtr) : VPresInst (capInsert (Z := Unit) c src dst) := + ⟨by + unfold capInsert setUntypedCapAsFull getCap updateCdt setOriginal doExtendedOp + vmpres⟩ + +instance (t : ObjRef) (buf : Option ObjRef) : TPresInst (setMrs (Z := Unit) t buf []) := + ⟨setMrs_tpres _ _ _ (Nat.zero_le _)⟩ + +instance (calling : Bool) (t : ObjRef) : VPresInst (epiSpecA calling t []) := + ⟨by + unfold epiSpecA replyFromKernel setMessageInfo + vmpres⟩ + +/-- The perform and epilogue of a `Copy` never throw and create no `runningVM` thread. -/ +theorem copyFin_okVm (blocking calling donate : Bool) (t : ObjRef) (c : Cap) (src dst : CslotPtr) : + OkVmA (fun _ => True) (finSpecA blocking calling donate t (.invokeCNode (.insertCall c src dst))) := by + unfold finSpecA withoutPreemption + refine (OkVmA.lift VPresInst.pres).bind fun _ _ => ?_ + have e : fl4PerformInvocation (Z := Unit) blocking calling donate (.invokeCNode (.insertCall c src dst)) = + (NonDetE.liftE (capInsert (Z := Unit) c src dst) >>= fun _ => Pure.pure []) := rfl + rw [e] + refine OkVmA.bind (P := fun l => l = []) ?_ fun reply hr => ?_ + · exact (OkVmA.lift VPresInst.pres).bind fun _ _ => OkVmA.pure rfl + · subst hr + exact OkVmA.lift VPresInst.pres + +/-- A budget prefix that answers `true` creates no `runningVM` thread: the charging branch answers `false`. -/ +theorem budgetOk_vm {σ τ : State Unit} (h : (true, τ) ∈ (fl4BudgetOk (Z := Unit) σ).1) : VmKA σ τ := by + unfold fl4BudgetOk at h + obtain ⟨⟨u, s1⟩, h1, h⟩ := mem_bindA.1 h + have k1 : VmKA σ s1 := by + have P : PresR VmKA (fl4UpdateTimestamp (Z := Unit)) := by + unfold fl4UpdateTimestamp + refine PresR.bind VPresInst.pres fun _ => PresR.modify _ fun _ p y hy v => ⟨y, hy, v⟩ + exact P σ _ h1 + unfold fl4CheckBudgetRestart at h + obtain ⟨⟨result, s2⟩, h2, h⟩ := mem_bindA.1 h + obtain ⟨⟨cur, s3⟩, h3, h⟩ := mem_bindA.1 h + obtain ⟨⟨ts, s4⟩, h4, h⟩ := mem_bindA.1 h + obtain ⟨⟨u', s5⟩, h5, h⟩ := mem_bindA.1 h + have e := mem_pureA h + obtain ⟨er, es⟩ := Prod.mk.inj e + subst er es + have k3 : VmKA s2 s3 := (VPresInst.pres (m := NonDet.gets fun s : State Unit => s.curThread)) _ _ h3 + have k4 : VmKA s3 s4 := (VPresInst.pres (m := getThreadState (Z := Unit) cur)) _ _ h4 + have k5 : VmKA s4 τ := by + rw [NonDet.when, if_neg (fun h => h.1 rfl)] at h5 + obtain ⟨-, rfl⟩ := Prod.mk.inj (mem_pureA h5) + exact PreO.refl _ + have k2 : VmKA s1 s2 := by + unfold fl4CheckBudget at h2 + obtain ⟨⟨csc, t1⟩, g1, h2⟩ := mem_bindA.1 h2 + obtain ⟨⟨used, t2⟩, g2, h2⟩ := mem_bindA.1 h2 + obtain ⟨⟨suf, t3⟩, g3, h2⟩ := mem_bindA.1 h2 + have a1 : VmKA s1 t1 := (VPresInst.pres (m := NonDet.gets fun s : State Unit => s.curSc)) _ _ g1 + have a2 : VmKA t1 t2 := (VPresInst.pres (m := NonDet.gets fun s : State Unit => s.consumedTime)) _ _ g2 + have a3 : VmKA t2 t3 := (VPresInst.pres (m := fl4GetScRefillSufficient (Z := Unit) csc used)) _ _ g3 + refine PreO.trans a1 (PreO.trans a2 (PreO.trans a3 ?_)) + cases suf with + | true => + rw [if_pos rfl] at h2 + obtain ⟨⟨d, t4⟩, g4, h2⟩ := mem_bindA.1 h2 + obtain ⟨-, es⟩ := Prod.mk.inj (mem_pureA h2) + subst es + exact (VPresInst.pres (m := NonDet.gets (isCurDomainExpired (Z := Unit)))) _ _ g4 + | false => + rw [if_neg Bool.false_ne_true] at h2 + obtain ⟨⟨d, t4⟩, g4, h2⟩ := mem_bindA.1 h2 + obtain ⟨⟨d', t5⟩, g5, h2⟩ := mem_bindA.1 h2 + cases (Prod.mk.inj (mem_pureA h2)).1 + exact PreO.trans k1 (PreO.trans k2 (PreO.trans k3 (PreO.trans k4 k5))) + +end + +end Fl4ProofsA64.P3.Sched + +/-! ## The witness -/ + +namespace Fl4ProofsA64.P3.Wit +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P1.W7.R.ST.BootWit Fl4ProofsA64.P3 + Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry + Fl4Spec Fl4SpecA64 + +noncomputable section + +open Fl4ProofsA64.P3.Wit2 (WA AR1 OpaqueWA WClock) + +local instance witPlainB : PlainStore bst := ⟨fun _ => rfl⟩ + +local notation "SL2" => (bwv (RRb + 64)) +local notation "RC" => capability.Cap.CNode (bwv RRb) 13#u64 51#u64 0#u64 + +/-- No thread of the witness's spec state runs a guest: its TCBs are the running root and the idle ones. -/ +theorem wit_noVm {now mt sl : U64} {uw : U64 → U64} : NoVmA (witSA now mt sl uw) := by + intro p x hp v + change Function.update (w1KA KB IB 1 now mt sl) (specPtr (bwv RTb)) (some (.tcb witTcbA)) p = some (.tcb x) at hp + by_cases c : p = specPtr (bwv RTb) + · subst c + rw [Function.update_self] at hp + obtain rfl := KernelObject.tcb.inj (Option.some.inj hp) + cases v + rw [Function.update_of_ne c] at hp + unfold w1KA bootKA at hp + split_ifs at hp <;> cases hp <;> cases v + +set_option maxHeartbeats 4000000 in +/-- **Every outcome of the CNode Copy invocation at the witness** creates no `runningVM` thread and does not throw. -/ +theorem wit_invoke_okVm {Z : XSetsA} {r mt sl : U64} {uw : U64 → U64} (U : WitUw uw) {ab : WA} + {τ : Fl4SpecA64.State Unit} (Jr : WitJ Z r wNow mt sl uw (witS r wNow mt sl uw) ab τ) + (rep calling blocking donate : Bool) {x : Tcb} + (hk : τ.kheap (specPtr (bL.curW (witS r wNow mt sl uw))) = some (.tcb x)) : + ∀ y, y ∈ ((fl4HandleInvocationR (Z := Unit) rep calling blocking donate false + (toBl (bL.mem (witS r wNow mt sl uw) (regWA (bL.curW (witS r wNow mt sl uw)) layout.reg.CAP)).bv)).run τ).1 → + y.1 = .ok () ∧ VmKA τ y.2 := by + intro y hy + have cp : bL.mem (witS r wNow mt sl uw) (regWA (bL.curW (witS r wNow mt sl uw)) layout.reg.CAP) = 2#u64 := by + rw [curW2] + have e : layout.reg.CAP = 0#u64 := by simp [layout.reg.CAP, layout.reg.X0] + rw [e, wit_regWord 0#u64 (by simp)] + simp [witRtW] + rw [cp] at hy + have einfo : msg.msg_info bst (witS r wNow mt sl uw) (bL.curW (witS r wNow mt sl uw)) = + ok { label := 24#u64, caps_unwrapped := 0#u64, extra_caps := 1#u64, length := 5#u64 } := by + rw [curW2]; exact wit_msgInfo + have ebuf : msg.lookup_ipc_buffer bst (witS r wNow mt sl uw) false (bL.curW (witS r wNow mt sl uw)) = + ok (bwv IPb) := by rw [curW2]; exact wit_ipcBuffer + have elr : cspace.lookup_slot bst (witS r wNow mt sl uw) (bL.curW (witS r wNow mt sl uw)) 2#u64 = + ok (.Ok { slot := SL2, bits_left := 0#u64 }) := by + rw [curW2]; exact wit_lookupSlot (by simp) (by rw [sl2v]; simp) + have eex : ipc.lookup_extra_caps bst (witS r wNow mt sl uw) (bL.curW (witS r wNow mt sl uw)) (bwv IPb) + { label := 24#u64, caps_unwrapped := 0#u64, extra_caps := 1#u64, length := 5#u64 } = ok (.Ok witExtra) := by + rw [curW2]; exact wit_extraCaps U + obtain ⟨mi, sl', c, excaps, buffer, argsU, Q⟩ := Jr.siteA einfo ebuf elr eex + have F := Q.F + have ct : τ.curThread = specPtr (bL.curW (witS r wNow mt sl uw)) := Jr.j.1.sch.curThread + -- the CNode Copy arm's facts + obtain ⟨p, rx, gs, g, hslc⟩ : ∃ p rx gs g, capA bst (witS r wNow mt sl uw) SL2 = .CNode p rx gs g := + ⟨_, _, _, _, sl2_cap⟩ + obtain ⟨s', hdec⟩ := wit_decodeOk (r := r) (now := wNow) (mt := mt) (sl := sl) U AR1 ab false false false + have hlen : msgLenA { label := 24#u64, caps_unwrapped := 0#u64, extra_caps := 1#u64, length := 5#u64 } + (bwv IPb) = 5#u64 := by + unfold msgLenA + have n : msg.N_MSG_REGISTERS = 4#u64 := by simp [msg.N_MSG_REGISTERS] + have ne : (bwv IPb) ≠ 0#u64 := fun e => by + have := congrArg UScalar.val e + rw [vIP] at this + simp at this + simp [n, ne] + obtain ⟨X⟩ := Jr.cnCopy_armA Q hslc (Or.inl (by rw [u64_eq_iffA]; have := lblA_copy; simpa using this.symm)) + (bB := false) (bC := false) (bD := false) (s' := s') (a' := ab) + (by rw [hlen, show capA bst (witS r wNow mt sl uw) SL2 = RC from sl2_cap]; exact hdec) + -- the spec's front lookups and decode, one outcome each + have htZ : bL.curW (witS r wNow mt sl uw) ∈ Z.T := Jr.sr.curT + have hth : Z.th (bL.curW (witS r wNow mt sl uw)) = some .tcb := (Jr.j.1.thT _).2 htZ + obtain ⟨lr, elr', se, hse, hrel⟩ := spec_existsA + (lookup_slot_refinesA Z.th (witS r wNow mt sl uw) τ Jr.j.1.st Jr.j.1.cv _ hth 2#u64) + obtain rfl := ok_inj_ff (elr'.symm.trans elr) + cases se with + | error _ => exact hrel.1.elim + | ok v => + obtain ⟨sl2, rest⟩ := v + obtain ⟨⟨hsl, -⟩, q, hq1, hq, -⟩ := hrel + have esl : sl2 = sl' := cslotAddrA_inj Q.place ⟨q, hq1, Or.inl hq⟩ F.pin.habs (hsl.symm.trans F.pin.hslot) + subst esl + have lcs := lcs_okA (ofBl (toBl (2#u64 : U64).bv)) hse F.pin.hcap + have front : (frontSpecA τ.curThread (toBl (2#u64 : U64).bv) mi).run τ = + ({(.ok (sl2, c, excaps, buffer), τ)}, False) := by + rw [ct] + have eb := F.pin.hbuf + have ex := F.pin.hex + rw [ct] at eb ex + unfold frontSpecA + rw [run_bind_okA lcs] + dsimp only + rw [run_bind_okA (run_liftEA eb), run_bind_okA ex] + rfl + obtain ⟨anc, -, -, hD⟩ := cnCopy_decodeEqA (mv := false) (call := calling) (lbl := 24#u64) Jr F rfl + (by simp) ⟨_, X.hCs.1, Or.inl X.hCs.2⟩ ⟨_, X.hCd.1, Or.inl X.hCd.2⟩ X.hsn X.hdn X.hn X.hslc X.hl4 X.hi X.hd0 + X.hres X.hx0 X.h0 X.h2 X.h3 X.hroot X.hsrc X.hcop + have dec : (decSpecA calling τ.curThread (toBl (2#u64 : U64).bv) mi (sl2, c, excaps, buffer)).run τ = + ({(.ok (.invokeCNode (.insertCall anc X.ss X.sd)), τ)}, False) := by + unfold decSpecA + dsimp only + rw [run_bind_okA (run_liftEA F.pin.hargs)] + exact hD _ + have gc : (NonDet.gets fun s : Fl4SpecA64.State Unit => s.curThread) τ = ({(τ.curThread, τ)}, False) := rfl + rw [fl4HandleInvocationR_piecesA, run_bind_okA (run_liftEA gc)] at hy + unfold withoutPreemption at hy + rw [run_bind_okA (run_liftEA F.pin.hmi), syscall_okA front dec] at hy + obtain ⟨⟨u, e, -⟩, k⟩ := copyFin_okVm _ _ _ _ _ _ _ τ y hy + exact ⟨e, k⟩ + +set_option maxHeartbeats 4000000 in +/-- **No thread runs a guest after any outcome of the witness's `Send` / `NBSend` / `Call`.** -/ +theorem wit_bodyNoVm (O : OpaqueWA) {r mt sl : U64} {uw : U64 → U64} (U : WitUw uw) + (hsl : Fl4SpecA64.MIN_BUDGET ≤ sl.bv) (hr : specPtr r = Fl4SpecA64.armGlobalPtPtr) (a : WA) + (ha : WClock a (witSA wNow mt sl uw).machineState) (number : I64) (rep : Bool) (call : Syscall) + (hc : call = .sysSend ∨ call = .sysNBSend ∨ call = .sysCall) {τf : Fl4SpecA64.State Unit} + (h : ((), τf) ∈ (kOpA (.syscall number rep call) (witSA wNow mt sl uw)).1) : NoVmA τf := by + -- the post-budget record + have I := wit_entryIn O (r := r) (mt := mt) (uw := uw) hsl hr a ha + obtain ⟨a1, hb, -⟩ := wit_budget_id O hsl hr a ha (uw := uw) (mt := mt) (r := r) + have budget := JSyscallA.budgetOkSysA (AR' := fun _ _ => True) I.J I.hvp I.P I.hClock I.hnd I.C I.hC I.hNow I.hAr I.hmb + (fun s1 a1 σ1 e _ _ _ _ _ _ _ suf => by + rw [wit_hsuf O hsl e] at suf + exact absurd (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst suf))).1 (by decide)) + obtain ⟨⟨b, sb, ab⟩, eb, τ, e1, -, tr⟩ := spec_existsA budget + have ee := ok_inj_ff (eb.symm.trans hb) + have hsb : sb = witS r wNow mt sl uw := congrArg (fun p => p.2.1) ee + have hb1 : b = true := congrArg Prod.fst ee + obtain ⟨Jr, -⟩ := tr hb1 + rw [hsb] at Jr + have e1' : fl4BudgetOk (Z := Unit) (witSA wNow mt sl uw) = ({(true, τ)}, False) := by + rw [hb1] at e1; exact e1 + -- the spec prefix + have nvτ : NoVmA τ := wit_noVm.of_vm (budgetOk_vm (by rw [e1']; exact Set.mem_singleton _)) + have htZ : bL.curW (witS r wNow mt sl uw) ∈ witZA.T := Jr.sr.curT + obtain ⟨x, hk, -⟩ := Jr.ep.tcb _ htZ + have ct : τ.curThread = specPtr (bL.curW (witS r wNow mt sl uw)) := Jr.j.1.sch.curThread + have reg : x.tcbArch.tcbContext.userRegs cap_register = + (bL.mem (witS r wNow mt sl uw) (regWA (bL.curW (witS r wNow mt sl uw)) layout.reg.CAP)).bv := by + have e : Endp.regA (bL.curW (witS r wNow mt sl uw)) (regIdxA cap_register) = + regWA (bL.curW (witS r wNow mt sl uw)) layout.reg.CAP := by + unfold Endp.regA regWA + rw [UScalar.eq_of_val_eq (show (regIdxA cap_register).val = layout.reg.CAP.val by + simp [cap_register, capRegister, regIdxA, layout.reg.CAP, layout.reg.X0])] + rw [← e] + exact ((Jr.ep.at_tcb htZ hk).regs cap_register).symm + have capRead : getCapReg (Z := Unit) cap_register τ = ({(toBl + (bL.mem (witS r wNow mt sl uw) (regWA (bL.curW (witS r wNow mt sl uw)) layout.reg.CAP)).bv, τ)}, False) := by + unfold getCapReg NonDet.liftM + rw [nd_gets_bindSc, ct, nd_bind_singleA' (asUser_getRegister_eqA hk _), reg] + rfl + have inv : ∀ calling blocking donate y, + y ∈ ((NonDetE.liftE (getCapReg (Z := Unit) cap_register) >>= fun cptr => + fl4HandleInvocationR (Z := Unit) rep calling blocking donate false cptr).run τ).1 → + y.1 = .ok () ∧ VmKA τ y.2 := fun calling blocking donate y hy => by + rw [run_bind_okA (run_liftEA capRead)] at hy + exact wit_invoke_okVm U Jr rep calling blocking donate hk y hy + have body : ∀ y, y ∈ ((fl4HandleEvent (Z := Unit) rep (.syscallEvent call)).run (witSA wNow mt sl uw)).1 → + y.1 = .ok () ∧ VmKA τ y.2 := fun y hy => by + unfold fl4HandleEvent at hy + dsimp only at hy + rw [run_lift_bindA e1'] at hy + simp only [Fl4Spec.NonDetE.whenE, ↓reduceIte, fl4HandleSyscall] at hy + rcases hc with rfl | rfl | rfl <;> simp only [fl4HandleSyscallWith] at hy <;> exact inv _ _ _ y hy + unfold kOpA at h + dsimp only at h + rw [nd_gets_bindSc, nd_bind_oneSc wit_pc] at h + obtain ⟨⟨v, σ1⟩, hv, h⟩ := mem_bindA.1 h + obtain ⟨-, rfl⟩ := Prod.mk.inj (mem_pureA h) + unfold Fl4Spec.NonDetE.handleE at hv + rw [ExceptT.run_mk] at hv + obtain ⟨y, hy, hv⟩ := mem_bindA.1 hv + obtain ⟨ey, ky⟩ := body y hy + rw [ey] at hv + obtain ⟨-, es⟩ := Prod.mk.inj (mem_pureA hv) + subst es + exact nvτ.of_vm ky + +/-- **`hNat` of `kernelEntry_recGA` / `kernelEntrySend_recGA` at the witness** (module docstring). -/ +theorem wit2b_hNatA (O : OpaqueWA) {r mt sl : U64} {uw : U64 → U64} (U : WitUw uw) + (hsl : Fl4SpecA64.MIN_BUDGET ≤ sl.bv) (hr : specPtr r = Fl4SpecA64.armGlobalPtPtr) (a : WA) + (ha : WClock a (witSA wNow mt sl uw).machineState) (number : I64) (rep : Bool) (call : Syscall) + (hc : call = .sysSend ∨ call = .sysNBSend ∨ call = .sysCall) {view : Fl4SpecA64.State Unit → Fl4TimerView} : + ∀ s' a' τ, kBodyA bst AR1 (witS r wNow mt sl uw) a (.syscall number rep call) = ok (s', a') → + ((), τ) ∈ (kOpA (.syscall number rep call) (witSA wNow mt sl uw)).1 → + A64RelS3 bst (fun _ _ => True) AR1 Wit2.ar1_plat.ipa Fl4SpecA64.fl4A64Platform.numNodes witZA.Cs s' a' τ + (w1MA IB 1) → ∀ τ1 τ2, ((), τ1) ∈ (fl4Schedule kLimitA view τ).1 → + ((), τ2) ∈ (fl4ActivateThread (Z := Unit) τ1).1 → + ∀ y, τ2.kheap τ2.curThread = some (.tcb y) → y.tcbState ≠ .ext .runningVM := + hNat_of_noVmA fun _ _ _ _ op _ => wit_bodyNoVm O U hsl hr a ha number rep call hc op + +end + +end Fl4ProofsA64.P3.Wit diff --git a/proofs/Fl4ProofsA64/P3/A64Wit2bGapA.lean b/proofs/Fl4ProofsA64/P3/A64Wit2bGapA.lean new file mode 100644 index 000000000..3fe1698d0 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/A64Wit2bGapA.lean @@ -0,0 +1,284 @@ +/- +P3 (aarch64, A64-P3 non-vacuity witness 2b): **`hGap` of `kernelEntry_recGA`, discharged from the record**. + +`hGap` asks `SchedGapFreeA` at every state the body reaches. Its three platform fields are facts of the arch; its +`timer` field is about the state `s4` that `schedule` reaches after the SC switch. Nothing here is specific to a +witness state: + +* `gapViewA`: the timer view of the model: constant, the `ARMED` word and the earliest custom timer of the `s4` that the + body's end state determines (the program from the body's end to the SC switch is deterministic); +* `switch_factsA`: from the record at the body's end, `s4` runs core 0 and its current thread has an active SC. It runs + the scheduler's refinement (`timer.fire`, `awaken`, select, switch) once more, up to `s4`; +* `schedGapFreeA_recA`: `SchedGapFreeA` at any record state; +* `wit2b_hGapA`: the premise `hGap` of `kernelEntry_recGA` / `kernelEntrySend_recGA` at the model arch `AR1`. + +Premises of `wit2b_hGapA`: [P] `OpaqueWA`, `ArchSwFA` (inhabited for the model arch on the `ArchSwFA` line), [S] `hCs`. +New module. +-/ +import Fl4ProofsA64.P3.Stage3SysTopA +import Fl4ProofsA64.P3.A64Wit2ArchA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3 + Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.OuterEntry Classical + +noncomputable section + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +/-! ## The view -/ + +/-- `schedule` from the body's end state `r` up to the SC switch (the `s4` of `SchedGapFreeA.timer`). -/ +def gapPhaseA (st : machine.Store S) (ar : machine.Arch A) [LawfulStore st] (r : S × A) : Result S := do + let start ← sched.awaken st ar r.1 r.2 + let sel ← scheduleSelectActionA (st := st) (ar := ar) start.1 start.2 + let s0 ← sched.set_action st sel.1 sched.ACTION_RESUME_CURRENT + let sw ← sched.switch_sched_context st ar s0 sel.2 + ok sw.1 + +/-- What a timer view shows of the concrete state `s`. -/ +def ViewOfA (st : machine.Store S) [L : LawfulStore st] (s : S) (v : Fl4TimerView) : Prop := + v.armed = (L.mem s (L.nodeAt s (L.cpu s) layout.node.ARMED)).bv ∧ + ∀ e, fl4_core_a64.timer.earliest st s = ok e → v.earliest = e.bv + +/-- **The timer view** of the run whose body is `body` (module docstring). -/ +def gapViewA (st : machine.Store S) (ar : machine.Arch A) [LawfulStore st] (body : Result (S × A)) : + State Unit → Fl4TimerView := fun _ => + @Classical.epsilon _ ⟨⟨0, 0⟩⟩ fun v => ∀ r s4, body = ok r → gapPhaseA st ar r = ok s4 → ViewOfA st s4 v + +theorem viewOf_existsA (s : S) : ∃ v, ViewOfA st s v := by + by_cases h : ∃ e, fl4_core_a64.timer.earliest st s = ok e + · obtain ⟨e, he⟩ := h + refine ⟨⟨(L.mem s (L.nodeAt s (L.cpu s) layout.node.ARMED)).bv, e.bv⟩, rfl, fun e' h' => ?_⟩ + rw [he] at h' + rw [ok_inj_ff h'] + · exact ⟨⟨(L.mem s (L.nodeAt s (L.cpu s) layout.node.ARMED)).bv, 0⟩, rfl, fun e he => (h ⟨e, he⟩).elim⟩ + +theorem gapView_specA {body : Result (S × A)} {r : S × A} {s4 : S} (hb : body = ok r) + (hp : gapPhaseA st ar r = ok s4) (σ : State Unit) : ViewOfA st s4 (gapViewA st ar body σ) := by + have ex : ∃ v, ∀ r' s', body = ok r' → gapPhaseA st ar r' = ok s' → ViewOfA st s' v := by + obtain ⟨v, hv⟩ := viewOf_existsA (st := st) s4 + refine ⟨v, fun r' s' hb' hp' => ?_⟩ + rw [hb] at hb' + rw [← ok_inj_ff hb', hp] at hp' + rw [← ok_inj_ff hp'] + exact hv + exact Classical.epsilon_spec ex r s4 hb hp + +theorem earliest_okA (s : S) : ∃ e, fl4_core_a64.timer.earliest st s = ok e := by + unfold fl4_core_a64.timer.earliest + rw [L.cur_cpu_eq, bind_ok, eq_of_spec_fd (obj.node_get.spec s _ _), bind_ok, + eq_of_spec_fd (obj.Tcb.is_null.spec _), bind_ok] + split + · exact ⟨_, rfl⟩ + · exact ⟨_, eq_of_spec_fd (obj.Tcb.timer_deadline.val s _)⟩ + +/-! ## The state at the SC switch -/ + +private theorem resumeWordA' : (.resumeCurThread : SchedulerAction) = actOfA sched.ACTION_RESUME_CURRENT := by + unfold sched.ACTION_RESUME_CURRENT actOfA + decide + +private theorem setActionResumeA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {σ : State Unit} (H : YieldQueueRelA (st := st) Z m κ s σ) : + sched.set_action st s sched.ACTION_RESUME_CURRENT ⦃ s' => + YieldQueueRelA (st := st) Z m κ s' { σ with schedulerAction := .resumeCurThread } ∧ + MFrA (st := st) Z m s s' σ { σ with schedulerAction := .resumeCurThread } ⦄ := by + refine WP.spec_mono (sched.set_action.spec s _) ?_ + rintro s' rfl + have f := H.sched.toSchedSRN.act_wr sched.ACTION_RESUME_CURRENT + obtain ⟨states, mcp, links⟩ := yieldQueueFrameA (σ' := { σ with schedulerAction := .resumeCurThread }) + H.sched H.states H.mcp H.links rfl (f.mono fun _ w => Or.inr w) + exact ⟨⟨H.sched.set_action _ resumeWordA' (fun t e => by cases e), states, mcp, links⟩, MFrA.of_sw rfl f⟩ + +/-- The context of the selection's outcome holds after the action reset (`ScScheduleA`'s private lemma). -/ +private theorem contextResetA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s s1 : S} {σ : State Unit} + (H1 : YieldQueueRelA (st := st) Z m κ s1 { σ with schedulerAction := .resumeCurThread }) + (context : ScheduleContextInputsA (st := st) Z m s σ) : + ScheduleContextInputsA (st := st) Z m s1 { σ with schedulerAction := .resumeCurThread } := by + obtain ⟨t, ht, hc, link⟩ := context.selected + obtain ⟨t', htb, fromWord⟩ := H1.sched.sch.tcb (L.curW s1) H1.sched.curT + have hh : σ.kheap σ.curThread = some (.tcb t') := by rw [H1.sched.sch.curThread]; exact htb + obtain rfl := KernelObject.tcb.inj (Option.some.inj (ht.symm.trans hh)) + have nz : L.mem s1 (scA (L.curW s1)) ≠ 0#u64 := by + intro e + rw [fromWord, optScA, if_pos e] at link + cases link + have word : L.mem s1 (scA (L.curW s1)) = L.mem s (scA (L.curW s)) := by + refine H1.sched.scp.inj _ (H1.sched.rest.scMem _ H1.sched.curT nz) _ hc ?_ + rw [fromWord, optScA, if_neg nz] at link + exact Option.some.inj link + refine ⟨⟨t, ht, by rw [word]; exact hc, by rw [word]; exact link⟩, ?_⟩ + rw [word] + exact context.refresh + +set_option maxHeartbeats 1000000 in +/-- **The concrete state after the SC switch** (module docstring): core 0, and the current thread has a related SC with +a nonzero refill capacity. -/ +theorem switch_factsA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {s : S} {a : A} {σ : State Unit} {m : McState} + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) (arch : ArchSwFA st ar) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (oneDomain : numDomains = 1) (minAgree : fl4MinBudget = MIN_BUDGET) (ticks : ∀ a', ScCfgTimeAgreeA ar a') + (hmax : maxTicksToUs = core.num.U64.MAX.bv) (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (B : BodyOKA σ m) {start selected : S × A} {s0 s4 : S} + (wake : sched.awaken st ar s a = ok start) + (sel : scheduleSelectActionA (st := st) (ar := ar) start.1 start.2 = ok selected) + (e0 : sched.set_action st selected.1 sched.ACTION_RESUME_CURRENT = ok s0) + (e4 : sched.switch_sched_context st ar s0 selected.2 = ok (s4, selected.2)) : + (L.cpu s4).val = 0 ∧ L.mem s4 (scA (L.curW s4)) ≠ 0#u64 ∧ + (L.rmax s4 (L.mem s4 (scA (L.curW s4)))).val ≠ 0 := by + have hcore : m.curCore < fl4A64Platform.numNodes := by + rw [J.sr.sch.core]; exact hCs _ J.sr.sch.cpuCs + have hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄ := fun a => + WP.spec_mono (ticks a).minBudget fun _ h => h.trans minAgree + have fireR := JStage3A.fire_budgetA hA hPl J hcore B.sw B.toBodyExitA + (JStage3A.fireSigA hA hPl hCs h1 hmb hmax htu) + have F0 : fl4_core_a64.timer.fire st ar s a ⦃ r => ∃ σ1 Q R, + fl4TimerFires σ σ1 ∧ YieldQueueRelA (st := st) { Z with Q := Q, R := R } m κ r.1 σ1 ∧ IdleThreadsA m σ1 ∧ + (σ = σ → fl4TimerFires σ σ1 → FiredFFA st ar AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ + fl4A64Platform.numNodes (J5A st) (J7A st (J7R2A st ar hPl.ipa)) m s a BodyExitA r.1 σ1) ⦄ := by + refine WP.spec_mono (reachedResultA fireR) ?_ + rintro r ⟨er, σ1, Q, Rl, hf, J1, R1, X1⟩ + refine ⟨σ1, Q, Rl, hf, J1.yieldQueueA, fun c hc x hx => ?_, fun _ _ => ⟨r.2, Q, Rl, er, J1, R1, X1⟩⟩ + obtain ⟨y, hy, hs⟩ := J1.sidle.f3 c hc + rw [hy] at hx + obtain rfl := KernelObject.tcb.inj (Option.some.inj hx) + rw [hs]; rfl + obtain ⟨r, er, sF, σ1, σ2, Q, R, FFh, fires, waking, wakeRel, Fa, SF⟩ := spec_ex_fd (awaken_stepA (FF := fun _ s1 σ0 σ1 => σ0 = σ → fl4TimerFires σ σ1 → + FiredFFA st ar AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) m s a BodyExitA s1 σ1) F0) + rw [wake] at er + obtain rfl := ok_inj_ff er + obtain ⟨a1, Qf, Rf, ef, J1, R1, B1⟩ := FFh rfl fires + have steps := JStage3A.selectionA (Z := Z) (Q := Qf) (R := Rf) J1 hCs h1 R1 waking wakeRel ⟨Fa.wr, Fa.keep, Fa.sz⟩ SF + (awaken_archA ef wake) (JStage3A.vsRootOK J1) (JStage3A.idleVtOf J1 J1.idleGapA.1 J1.idleGapA.2.1) + obtain ⟨r2, er2, σ3, Q', _, selection, _, H3, _, _⟩ := + spec_ex_fd (select_action_refinesA wakeRel arch steps.action steps.domain steps.roots) + rw [sel] at er2 + obtain rfl := ok_inj_ff er2 + have core := JStage3A.tailCoreA J1 R1 B1 h1 oneDomain waking selection H3 + obtain ⟨t0, e0', H0, F0'⟩ := spec_ex_fd (setActionResumeA H3) + rw [e0] at e0' + obtain rfl := ok_inj_ff e0' + have ready := contextResetA H0 core.1 + obtain ⟨t, ht, hc, link⟩ := ready.selected + have account := switch_sched_context_refinesA (a := selected.2) H0 ht hc rfl link ready.refresh + (fun s1 σ1' H1 F1 h => core.2 s0 _ s1 σ1' H0 rfl F0' H1 F1 h) minAgree (ticks _) + obtain ⟨r4, e4', τ, sw, -, H4, -⟩ := spec_ex_fd account + rw [e4] at e4' + obtain rfl := ok_inj_ff e4' + have cpu : (L.cpu s4).val = 0 := by + have := hCs _ H4.sched.sch.cpuCs + change (L.cpu s4).val < fl4A64Platform.numNodes at this + omega + -- the selected thread's SC is active before the switch, and the switch keeps that + have mem : ((), τ) ∈ (fl4SwitchSchedContext (Z := Unit) + { σ3 with schedulerAction := .resumeCurThread }).1 := by rw [sw]; exact Set.mem_singleton _ + have K : ActKeepA { σ3 with schedulerAction := .resumeCurThread } τ := + (inferInstance : APresInst (fl4SwitchSchedContext (Z := Unit))).pres _ _ mem + obtain ⟨y, n, hy, rel⟩ := H0.sched.scr.sc _ hc + have act : scActive y = true := by + by_cases sp : y.scSporadic = true + · have w := rel.sporadic.symm.trans sp + have ne := rel.spor (of_decide_eq_true w) + rw [rel.max] at ne + simpa [scActive, activeSc, Nat.pos_iff_ne_zero] using ne + · exact (ready.refresh y n hy sp).1 + obtain ⟨y', hy', act'⟩ := K.2.2.2.2.2.2 _ y n hy + have cur : τ.curThread = specPtr (L.curW s4) := H4.sched.sch.curThread + have htτ : τ.kheap (specPtr (L.curW s4)) = some (.tcb t) := by + rw [← cur, K.2.1] + exact (K.2.2.2.1 _ t).2 ht + obtain ⟨x4, hx4, link4⟩ := H4.sched.sch.tcb (L.curW s4) H4.sched.curT + obtain rfl := KernelObject.tcb.inj (Option.some.inj (htτ.symm.trans hx4)) + have nz : L.mem s4 (scA (L.curW s4)) ≠ 0#u64 := by + intro z + rw [link, optScA, if_pos z] at link4 + cases link4 + refine ⟨cpu, nz, ?_⟩ + have hname : scNameA m (L.mem s0 (scA (L.curW s0))) = scNameA m (L.mem s4 (scA (L.curW s4))) := by + rw [link, optScA, if_neg nz] at link4 + exact Option.some.inj link4 + obtain ⟨z, k, hz, rel4⟩ := H4.sched.scr.sc _ (H4.sched.rest.scMem _ H4.sched.curT nz) + rw [← hname, hy'] at hz + obtain ⟨rfl, -⟩ := KernelObject.schedContext.inj (Option.some.inj hz) + rw [rel4.max] + have := act'.trans act + simpa [scActive, activeSc, Nat.pos_iff_ne_zero] using this + +/-! ## `SchedGapFreeA` from the record -/ + +/-- **`SchedGapFreeA` at a record state** (module docstring), with the view `gapViewA` of the run that reached it. -/ +theorem schedGapFreeA_recA {AR : A → HwRootA → Prop} {Cs : Set U64} {s : S} {a : A} {σ : State Unit} {m : McState} + {tm : A → Nat → DeadlineViewA} {body : Result (S × A)} + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) (arch : ArchSwFA st ar) + (hCs : ∀ c ∈ Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (oneDomain : numDomains = 1) (minAgree : fl4MinBudget = MIN_BUDGET) (ticks : ∀ a', ScCfgTimeAgreeA ar a') + (hmax : maxTicksToUs = core.num.U64.MAX.bv) (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (sni : ∀ a', SniHypA ar tm a' 0) (hb : body = ok (s, a)) + (R : A64RelS3 st AR ar hPl.ipa fl4A64Platform.numNodes Cs s a σ m) : + SchedGapFreeA st m s a σ ar tm (gapViewA st ar body) := by + obtain ⟨Z, H, OS, U, Ac, Pp, Tb, Lv, ia, ie, N, E, Rp, Bf, stack, κ, hZ, J, B⟩ := R + refine ⟨oneDomain, minAgree, ticks, ?_⟩ + intro start selected Z3 σ3 wake sel C σ0 s0 s4 σ4 e0 e4 sw _ _ hY _ + have hCs' : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes := fun c hc => hCs c (hZ ▸ hc) + obtain ⟨cpu, nz, rm⟩ := switch_factsA hA hPl arch hCs' h1 oneDomain minAgree ticks hmax htu J B wake sel e0 e4 + refine ⟨?_, cpu ▸ sni selected.2⟩ + obtain ⟨κ4, Y⟩ := hY + have phase : gapPhaseA st ar (s, a) = ok s4 := by + unfold gapPhaseA + rw [wake, bind_ok, sel, bind_ok, e0, bind_ok, e4, bind_ok] + have view := gapView_specA (st := st) (ar := ar) hb phase σ4 + obtain ⟨earliest, he⟩ := earliest_okA (st := st) s4 + refine ⟨earliest, he, view.2 _ he, view.1, ?_⟩ + obtain ⟨x, hx, link⟩ := Y.sched.sch.tcb (L.curW s4) Y.sched.curT + have cur : σ4.curThread = specPtr (L.curW s4) := Y.sched.sch.curThread + rw [← cur] at hx + have mem : ((), σ4) ∈ (fl4SwitchSchedContext (Z := Unit) σ0).1 := by rw [sw]; exact Set.mem_singleton _ + have hc : x.tcbSchedContext = some σ4.curSc := fl4SwitchSchedContext_hc σ0 trivial ((), σ4) mem x hx + have hname : scNameA m (L.mem s4 (scA (L.curW s4))) = σ4.curSc := by + rw [hc, optScA, if_neg nz] at link + exact (Option.some.inj link).symm + obtain ⟨y, n, hy, rel⟩ := Y.sched.scr.sc _ (Y.sched.rest.scMem _ Y.sched.curT nz) + rw [hname] at hy + refine ⟨x, y, n, hx, hc, hy, ?_⟩ + rw [rel.max] at rm + simpa [scActive, activeSc, Nat.pos_iff_ne_zero] using rm + +end + +end + +end Fl4ProofsA64.P3.Sched + +/-! ## The model arch -/ + +namespace Fl4ProofsA64.P3.Wit2 +open Fl4SpecA64 Fl4Spec fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3 + Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched + +noncomputable section + +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **`hGap` of `kernelEntry_recGA` at the model arch** (module docstring). For `kernelEntrySend_recGA` take +`e := .syscall number rep call` and `Cs := Z.Cs`. -/ +theorem wit2b_hGapA (O : OpaqueWA) {AR' : WA → HwRootA → Prop} (arch : ArchSwFA st AR1) {Cs : Set U64} + (hCs : ∀ c ∈ Cs, c.val < fl4A64Platform.numNodes) {m : McState} (s : S) (a : WA) (σ : State Unit) + (e : KEventA) : + ∀ s' a' τ, kBodyA st AR1 s a e = ok (s', a') → ((), τ) ∈ (kOpA e σ).1 → + A64RelS3 st AR' AR1 ar1_plat.ipa fl4A64Platform.numNodes Cs s' a' τ m → + SchedGapFreeA st m s' a' τ AR1 witTm (gapViewA st AR1 (kBodyA st AR1 s a e)) := + fun _ _ _ eb _ R => schedGapFreeA_recA ar1_total ar1_plat arch hCs O.nodes O.nd O.hag (ar1_cfgTime O) O.max + ar1_ticksToUs ar1_sni eb R + +end + +end Fl4ProofsA64.P3.Wit2 diff --git a/proofs/Fl4ProofsA64/P3/A64Wit2bNatA.lean b/proofs/Fl4ProofsA64/P3/A64Wit2bNatA.lean new file mode 100644 index 000000000..23d823ff0 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/A64Wit2bNatA.lean @@ -0,0 +1,200 @@ +/- +P3 (aarch64, A64-P3 non-vacuity witness 2b): **`hNat` ([S4] `hNative`) from "no thread runs a guest" at the body's end**. + +`hNat` of `kernelEntry_recGA` quantifies over every outcome of `fl4Schedule` (whose `fl4Awaken` is any finite sequence +of notification signals) and of `fl4ActivateThread`. Neither creates `runningVM`: + +* `NoVmA σ`: no TCB of `σ` is `runningVM`; +* `sendSignal_vm` / `noVm_timerFires`: a signal and the fires loop keep `NoVmA`; the rest of `schedule` keeps every TCB + object (`schedTail_cells`); +* `noVm_activate`: so does `activate_thread`; +* `hNat_of_noVmA`: the premise `hNat` of `kernelEntry_recGA` / `kernelEntrySend_recGA` from `NoVmA` at every state the + body reaches. + +Premise left: [J] `NoVmA τ` at the body's end. The record does not give it: `RunVcpuInv` is the converse direction (a +thread with a `RUN_VCPU` link is `runningVM`), and no field of `JStage3A` ties a `runningVM` thread to a VCPU, so a +platform with no VCPU object is not enough. New module. +-/ +import Fl4ProofsA64.P3.Stage3SysTopA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P3 Classical + +noncomputable section + +/-- No thread is running a guest. -/ +def NoVmA (σ : State Unit) : Prop := ∀ p x, σ.kheap p = some (.tcb x) → x.tcbState ≠ .ext .runningVM + +/-- Every `runningVM` thread of `σ'` was one of `σ`. -/ +def VmKA (σ σ' : State Unit) : Prop := + ∀ p y, σ'.kheap p = some (.tcb y) → y.tcbState = .ext .runningVM → + ∃ x, σ.kheap p = some (.tcb x) ∧ x.tcbState = .ext .runningVM + +instance : PreO VmKA where + refl _ := fun _ y h v => ⟨y, h, v⟩ + trans h1 h2 := fun p z h v => by + obtain ⟨y, hy, vy⟩ := h2 p z h v + exact h1 p y hy vy + +theorem NoVmA.of_vm {σ σ' : State Unit} (h : NoVmA σ) (k : VmKA σ σ') : NoVmA σ' := fun p y hy v => by + obtain ⟨x, hx, vx⟩ := k p y hy v + exact h p x hx vx + +class VPresInst {α : Type} (m : SMonad Unit α) : Prop where + pres : PresR VmKA m + +instance (priority := 20) {α : Type} {m : SMonad Unit α} [h : TPresInst m] : VPresInst m := + ⟨fun s x hx p y hy v => by + obtain ⟨-, -, -, -, -, kb, -⟩ := h.pres s x hx + obtain ⟨z, hz, -, e⟩ := kb p y hy + exact ⟨z, hz, e ▸ v⟩⟩ + +instance (priority := 10) {α : Type} {m : SMonad Unit α} [h : KPresInst m] : VPresInst m := + ⟨fun s x hx p y hy v => ⟨y, ((h.pres s x hx).2 p y).2 hy, v⟩⟩ + +instance {α β : Type} {m : SMonad Unit α} {f : α → SMonad Unit β} [hm : VPresInst m] + [hf : ∀ a, VPresInst (f a)] : VPresInst (m >>= f) := + ⟨PresR.bind hm.pres fun a => (hf a).pres⟩ +instance {P : Prop} {d : Decidable P} {m : SMonad Unit Unit} [h : VPresInst m] : + VPresInst (@NonDet.when (State Unit) P d m) := ⟨PresR.when h.pres⟩ + +macro "vmpres" : tactic => `(tactic| + repeat' (first + | exact VPresInst.pres + | refine PresR.bind_pure _ _ ?_ + | refine PresR.bind ?_ (fun _ => ?_) + | refine PresR.when ?_ + | refine PresR.ite' ?_ ?_ + | (refine PresR.modify _ (fun _ p y hy v => ⟨y, hy, v⟩)) + | dsimp only + | split)) + +/-- A TCB update that sets no `runningVM` state. -/ +theorem threadSet_vm (f : Tcb → Tcb) + (hf : ∀ x, (f x).tcbState = .ext .runningVM → x.tcbState = .ext .runningVM) (t : ObjRef) : + PresR VmKA (threadSet (Z := Unit) f t) := by + intro σ r hr p y hy v + obtain ⟨x0, h0, e⟩ := threadSet_spec hr + rw [e] at hy + change Function.update σ.kheap t (some (.tcb (f x0))) p = some (.tcb y) at hy + by_cases c : p = t + · subst c + rw [Function.update_self] at hy + obtain rfl := KernelObject.tcb.inj (Option.some.inj hy) + exact ⟨x0, h0, hf _ v⟩ + · rw [Function.update_of_ne c] at hy + exact ⟨y, hy, v⟩ + +theorem setThreadState_vm (t : ObjRef) (ts : ThreadState) (h : ts ≠ .ext .runningVM) : + PresR VmKA (setThreadState (Z := Unit) t ts) := by + unfold setThreadState setThreadStateAct setSchedulerAction + refine PresR.bind (threadSet_vm (fun x => { x with tcbState := ts }) (fun _ v => (h v).elim) t) (fun _ => ?_) + refine PresR.bind VPresInst.pres (fun _ => ?_) + refine PresR.bind VPresInst.pres (fun _ => ?_) + refine PresR.bind VPresInst.pres (fun _ => ?_) + exact PresR.when (PresR.modify _ fun _ p y hy v => ⟨y, hy, v⟩) + +instance (t : ObjRef) : VPresInst (setThreadState (Z := Unit) t .running) := + ⟨setThreadState_vm t _ (by intro h; cases h)⟩ +instance (t : ObjRef) : VPresInst (setThreadState (Z := Unit) t .inactive) := + ⟨setThreadState_vm t _ (by intro h; cases h)⟩ +instance (t : ObjRef) : VPresInst (setThreadState (Z := Unit) t .restart) := + ⟨setThreadState_vm t _ (by intro h; cases h)⟩ + +theorem setTcbSc_vm (t : ObjRef) (v : Option ObjRef) : + PresR VmKA (setTcbObjRef (Z := Unit) tcbSchedContextUpdate t v) := by + unfold setTcbObjRef + exact threadSet_vm (tcbSchedContextUpdate fun _ => v) (fun _ h => h) t + +instance (t : ObjRef) (v : Option ObjRef) : VPresInst (setTcbObjRef (Z := Unit) tcbSchedContextUpdate t v) := + ⟨setTcbSc_vm t v⟩ + +instance (t : ObjRef) : VPresInst (testReschedule (Z := Unit) t) := + ⟨by unfold testReschedule; vmpres⟩ + +instance (sc t : ObjRef) : VPresInst (schedContextDonate (Z := Unit) sc t) := + ⟨by unfold schedContextDonate; vmpres⟩ + +instance (t r : ObjRef) : VPresInst (replyUnlinkTcb (Z := Unit) t r) := + ⟨by unfold replyUnlinkTcb setReplyObjRef updateSkObjRef; vmpres⟩ + +instance (t r : ObjRef) : VPresInst (replyRemoveTcb (Z := Unit) t r) := + ⟨by unfold replyRemoveTcb setReplyObjRef getReplyObjRef updateSkObjRef getSkObjRef; vmpres⟩ + +instance (t : ObjRef) : VPresInst (threadSet (Z := Unit) (fun tcb => { tcb with tcbFault := none }) t) := + ⟨threadSet_vm (fun tcb => { tcb with tcbFault := none }) (fun _ h => h) t⟩ + +instance (t : ObjRef) : VPresInst (cancelIpc (Z := Unit) t) := + ⟨by unfold cancelIpc blockedCancelIpc cancelSignal getBlockingObject tcbEpDequeue tcbNtfnDequeue; vmpres⟩ + +instance (sc : ObjRef) : VPresInst (schedContextResume (Z := Unit) sc) := + ⟨by unfold schedContextResume postpone tcbReleaseEnqueue setReleaseQueue; vmpres⟩ + +instance (t n : ObjRef) : VPresInst (maybeDonateSc (Z := Unit) t n) := ⟨by + unfold maybeDonateSc getNtfnObjRef getSkObjRef + refine PresR.bind VPresInst.pres (fun _ => ?_) + refine PresR.when (PresR.bind VPresInst.pres (fun o => ?_)) + cases o with + | none => exact PresR.pure _ + | some sc => vmpres⟩ + +theorem sendSignal_vm (n : ObjRef) (b : Badge) : PresR VmKA (sendSignal (Z := Unit) n b) := by + unfold sendSignal updateWaitingNtfn + vmpres + +theorem noVm_timerFires {σ σ' : State Unit} (h : fl4TimerFires σ σ') (nv : NoVmA σ) : NoVmA σ' := by + induction h with + | refl => exact nv + | tail _ step ih => + obtain ⟨σ1, c, rfl⟩ := step + change NoVmA σ1 + rcases c with rfl | ⟨n, b, e⟩ + · exact ih + · refine ih.of_vm (sendSignal_vm n b _ ((), σ1) ?_) + rw [e]; exact Set.mem_singleton _ + +/-- `fl4Schedule` creates no `runningVM` thread. -/ +theorem noVm_schedule (limit : Nat) (timer : State Unit → Fl4TimerView) {σ τ : State Unit} (nv : NoVmA σ) + (h : ((), τ) ∈ (fl4Schedule (Z := Unit) limit timer σ).1) : NoVmA τ := by + unfold fl4Schedule at h + obtain ⟨⟨u, σ1⟩, h1, h2⟩ := mem_bindA.1 h + have K := schedTail_cells limit timer σ1 _ h2 + have n1 := noVm_timerFires (mem_fl4Awaken.1 h1) nv + exact fun p x hx => n1 p x ((K.2 p x).2 hx) + +/-- `activate_thread` creates no `runningVM` thread. -/ +theorem noVm_activate {σ τ : State Unit} (nv : NoVmA σ) + (h : ((), τ) ∈ (fl4ActivateThread (Z := Unit) σ).1) : NoVmA τ := by + have P : PresR VmKA (fl4ActivateThread (Z := Unit)) := by + unfold fl4ActivateThread fl4ActivateState getTcbObjRef + vmpres + exact nv.of_vm (P σ ((), τ) h) + +/-- **`hNat` from the body's end state** (the shape `kernelEntry_recGA` hands out, at one `τ`). -/ +theorem hNat_atA {limit : Nat} {view : State Unit → Fl4TimerView} {τ : State Unit} (nv : NoVmA τ) : + ∀ τ1 τ2, ((), τ1) ∈ (fl4Schedule (Z := Unit) limit view τ).1 → ((), τ2) ∈ (fl4ActivateThread (Z := Unit) τ1).1 → + ∀ y, τ2.kheap τ2.curThread = some (.tcb y) → y.tcbState ≠ .ext .runningVM := + fun _ _ h1 h2 y hy => noVm_activate (noVm_schedule limit view nv h1) h2 _ y hy + +section +open fl4_core_a64 Fl4ProofsA64.P1 +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +/-- **`hNat` of `kernelEntry_recGA`** (module docstring). For `kernelEntrySend_recGA` take +`e := .syscall number rep call` and `Cs := Z.Cs`. -/ +theorem hNat_of_noVmA {AR : A → HwRootA → Prop} {nA : U64} {n : Nat} {Cs : Set U64} {m : McState} {limit : Nat} + {view : State Unit → Fl4TimerView} {s : S} {a : A} {σ : State Unit} {e : KEventA} + (nv : ∀ s' a' τ, kBodyA st ar s a e = ok (s', a') → ((), τ) ∈ (kOpA e σ).1 → + A64RelS3 st AR ar nA n Cs s' a' τ m → NoVmA τ) : + ∀ s' a' τ, kBodyA st ar s a e = ok (s', a') → ((), τ) ∈ (kOpA e σ).1 → + A64RelS3 st AR ar nA n Cs s' a' τ m → ∀ τ1 τ2, ((), τ1) ∈ (fl4Schedule (Z := Unit) limit view τ).1 → + ((), τ2) ∈ (fl4ActivateThread (Z := Unit) τ1).1 → + ∀ y, τ2.kheap τ2.curThread = some (.tcb y) → y.tcbState ≠ .ext .runningVM := + fun s' a' τ eb op R => hNat_atA (nv s' a' τ eb op R) + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/A64WitCopyA.lean b/proofs/Fl4ProofsA64/P3/A64WitCopyA.lean new file mode 100644 index 000000000..17acdb3c3 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/A64WitCopyA.lean @@ -0,0 +1,460 @@ +/- +P3 (aarch64, A64-P3 non-vacuity witness, part 5): **the CNode Copy send path runs `Ok` at the witness**. + +At `witS` (`P3/A64WitStA`) with the IPC buffer holding the extra cap's CPtr (word 122) and the rights (word 5), the +current (root) thread's `Send` on CPtr `2` (its CSpace's root CNode cap) is a `CNodeCopy`: + +* `msg_info` is label `CNodeCopy`, one extra cap, length 5; `lookup_ipc_buffer` the root IPC buffer; +* `lookup_slot` of CPtr 2 is root CNode slot 2, which holds the CNode cap (`wit_hSel`: the selector of Plan B's + `cnodeCopyBodyA`); +* `lookup_extra_caps` returns slot 2 as the source root; +* `decode_invocation` returns `Ok` (`wit_decodeOk`): destination slot 1000 (empty), source slot 2 (the CNode cap), + rights all. + +Hypotheses: the two IPC-buffer words (`WitUw`). No premise. New module; no existing statement changed. +-/ +import Fl4ProofsA64.P3.A64WitSyscA +import Fl4ProofsA64.P0.Auto +import Fl4ProofsA64.P1.LocalVcpuKeep +import Fl4ProofsA64.P1.VcpuHooks +import Fl4ProofsA64.P2.GuardBase +import Fl4ProofsA64.P1.FastFault +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Wit +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P1.W7.R.ST.BootWit Fl4ProofsA64.P3 + Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-- The two IPC-buffer words of the message: word 122 (the first extra cap's CPtr) is 2, word 5 (the rights) is 15. -/ +structure WitUw (uw : U64 → U64) : Prop where + cap0 : ∀ b : U64, b.val = IPb + 976 → uw b = 2#u64 + rights : ∀ b : U64, b.val = IPb + 40 → uw b = 15#u64 + +/-- Such user words exist. -/ +theorem witUw_ex : ∃ uw, WitUw uw := + ⟨fun b => if b.val = IPb + 976 then 2#u64 else 15#u64, + ⟨fun _ h => if_pos h, fun _ h => if_neg (by omega)⟩⟩ + +/-- The concrete store is total (A64 P0's `StoreTotal`). -/ +theorem bstTotal : Fl4ProofsA64.P0.StoreTotal bst := + ⟨fun _ => (WP.spec_ok _).2 trivial, fun _ _ => (WP.spec_ok _).2 trivial, fun _ _ => (WP.spec_ok _).2 trivial, + fun _ _ _ => (WP.spec_ok _).2 trivial, fun _ _ _ => (WP.spec_ok _).2 trivial, + fun _ _ _ => (WP.spec_ok _).2 trivial, fun _ => (WP.spec_ok _).2 trivial, fun _ => (WP.spec_ok _).2 trivial, + fun _ _ _ => (WP.spec_ok _).2 trivial⟩ + +variable {r now mt sl : U64} {uw : U64 → U64} + +local notation "S1" => w1S KB IB 1 r now mt sl uw +local notation "S2" => witS r now mt sl uw +local notation "RT" => (bwv RTb) +local notation "RC" => capability.Cap.CNode (bwv RRb) 13#u64 51#u64 0#u64 + +/-! ## Reads -/ + +theorem regW2 (i : U64) (hi : i.val < 6) : + obj.Tcb.reg bst RT S2 i = ok (witRtW (256 + 8 * i.val)) := by + rw [tcb_reg_eqA] + have v : (regWA RT i).val = RTb + (256 + 8 * i.val) := by + have h := Endp.regA_val (t := RT) (i := i) rtAddr (by omega) + rw [vRT] at h + show (Endp.regA RT i).val = _ + omega + exact congrArg ok (wit_rtWords _ _ (by omega) (by omega) (by rw [v, vRT])) + +theorem ipb_word {k : Nat} (hk : k < 512) {b : U64} (hb : b.val = IPb + 8 * k) : bL.mem S2 b = uw b := by + have e : bL.mem S2 b = bL.mem S1 b := ks_keep (Or.inr (Or.inl ⟨by omega, by omega⟩)) + rw [e] + change w1M KB IB 1 r now mt sl uw b = _ + rw [w1M_root lay1 (o := 401408 + 8 * k) (by omega) (by omega)] + unfold rootW + rw [if_neg (by omega), if_neg (by omega), if_neg (by omega), if_neg (by omega), if_pos (by omega)] + +theorem bufGet {k : U64} (hk : k.val < 512) {b : U64} (hb : b.val = IPb + 8 * k.val) : + msg.buf_get bst S2 (bwv IPb) k = ok (uw b) := by + rw [buf_get_eqA] + have e : bufWA (bwv IPb) k = b := by + apply UScalar.eq_of_val_eq + unfold bufWA + have e8 : (8#u64 : U64).val = 8 := rfl + rw [wa_valm, core.num.U64.wrapping_mul_val_eq, vIP, hb, e8] + have : UScalar.size UScalarTy.U64 = 2 ^ 64 := by simp [U64.size_def, U64.numBits] + rw [this, Nat.mod_eq_of_lt (by omega), Nat.mod_eq_of_lt (by omega)] + omega + rw [e, ipb_word hk hb] + +/-- A root CNode slot is not changed. -/ +theorem capS2_rs {x : U64} {i : Nat} (hi : i < 8192) (hx : x.val = RRb + 32 * i) : capA bst S2 x = capOf 1 i := by + rw [← cap_root (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) lay1 hi hx] + have h8 : (wa x 8#u64).val = RRb + 32 * i + 8 := by + rw [wa_valm, hx]; simp; omega + have e1 : bL.mem S2 x = bL.mem S1 x := ks_keep (Or.inr (Or.inl ⟨by omega, by omega⟩)) + have e2 : bL.mem S2 (wa x 8#u64) = bL.mem S1 (wa x 8#u64) := ks_keep (Or.inr (Or.inl ⟨by omega, by omega⟩)) + unfold capA + rw [e1, e2] + +/-- A root TCB slot is not changed. -/ +theorem capS2_ts {i : U64} (hi : i.val < 7) : capA bst S2 (slotIdx RT i) = + capA bst S1 (slotIdx RT i) := by + have v : (slotIdx RT i).val = RTb + 32 * i.val := by + have e : slotIdx RT i = ts i := rfl + rw [e, ts_val (by omega)] + omega + have h8 : (wa (slotIdx RT i) 8#u64).val = RTb + 32 * i.val + 8 := by + rw [wa_valm, v]; simp; omega + have e1 : bL.mem S2 (slotIdx RT i) = bL.mem S1 (slotIdx RT i) := + ks_keep (Or.inr (Or.inl ⟨by omega, by omega⟩)) + have e2 : bL.mem S2 (wa (slotIdx RT i) 8#u64) = bL.mem S1 (wa (slotIdx RT i) 8#u64) := + ks_keep (Or.inr (Or.inl ⟨by omega, by omega⟩)) + unfold capA + rw [e1, e2] + +theorem tcbSlot_ok (i : U64) (hi : i.val < 7) : obj.Tcb.slot RT i = ok (slotIdx RT i) := by + have z : layout.cte.SIZE = 32#u64 := by simp [layout.cte.SIZE] + unfold obj.Tcb.slot slotIdx + simp only [lift, bind_ok, z] + +/-! ## The prefix lookups -/ + +/-- **`msg_info`**: label `CNodeCopy`, one extra cap, length 5. -/ +theorem wit_msgInfo : msg.msg_info bst S2 RT = + ok { label := 24#u64, caps_unwrapped := 0#u64, extra_caps := 1#u64, length := 5#u64 } := by + have e : layout.reg.MSG_INFO = 1#u64 := by simp [layout.reg.MSG_INFO, layout.reg.X1] + have v : witRtW (256 + 8 * (1#u64 : U64).val) = 98437#u64 := by simp [witRtW] + have s12 : (98437#u64 : U64) >>> 12#i32 = ok 24#u64 := by rfl + have s9 : (98437#u64 : U64) >>> 9#i32 = ok 192#u64 := by rfl + have s7 : (98437#u64 : U64) >>> 7#i32 = ok 769#u64 := by rfl + have m1 : (192#u64 : U64) &&& 7#u64 = 0#u64 := by rfl + have m2 : (769#u64 : U64) &&& 3#u64 = 1#u64 := by rfl + have m3 : (98437#u64 : U64) &&& 127#u64 = 5#u64 := by rfl + have le : ¬ (5#u64 : U64) > fl4_abi.consts.MSG_MAX_LENGTH := by + simp [fl4_abi.consts.MSG_MAX_LENGTH] + unfold msg.msg_info + rw [e, regW2 1#u64 (by simp), bind_ok, v] + unfold msginfo.MessageInfo.from_word_clamped msginfo.MessageInfo.from_word + rw [s12, bind_ok, s9, bind_ok] + simp only [lift, bind_ok] + rw [s7, bind_ok] + simp only [bind_ok, m1, m2, m3] + rw [if_neg le] + +theorem wit_ipcBuffer : msg.lookup_ipc_buffer bst S2 false RT = ok (bwv IPb) := by + have ipo : layout.tcb.IPC_BUFFER = ok 3512#u64 := by + unfold layout.tcb.IPC_BUFFER layout.tcb.F; rfl + have w : obj.Tcb.ipc_buffer bst RT S2 = ok (bwv 4198400) := by + unfold obj.Tcb.ipc_buffer + rw [ipo] + simp only [bind_ok, lift] + have v : (wa RT 3512#u64).val = RTb + 3512 := by + rw [fldV rtMem1 (by simp), vRT]; simp + refine congrArg ok ?_ + exact (wit_rtWords _ 3512 (by omega) (by omega) (by rw [v, vRT])).trans (by simp [witRtW, rtW]) + have sb : layout.tcb.SLOT_BUFFER = 2#u64 := by simp [layout.tcb.SLOT_BUFFER] + have c : capA bst S2 (slotIdx RT 2#u64) = .Frame (bwv IPb) .Small 3#u64 false false false 0#u64 (bwv 0) := by + rw [capS2_ts (by simp), cap_rt lay1 (by simp)] + simp + unfold msg.lookup_ipc_buffer + rw [w, bind_ok, sb, tcbSlot_ok 2#u64 (by simp), bind_ok, capA_eq, c, bind_ok] + have rw3 : (3#u64 : U64) = capability.VM_READ_WRITE := by simp [capability.VM_READ_WRITE] + simp only [Bool.false_eq_true, if_false, rw3, if_true] + have fb : capability.FrameSize.bits .Small = ok 12#u64 := by rfl + have s1 : (12#u64 : U64) &&& 63#u64 = 12#u64 := by rfl + have s2 : (1#u64 : U64) <<< (12#u64 : U64) = ok 4096#u64 := by rfl + have s3 : core.num.U64.wrapping_sub 4096#u64 1#u64 = 4095#u64 := by rfl + have s4 : (bwv 4198400) &&& 4095#u64 = 0#u64 := by rfl + have s5 : wa (bwv IPb) 0#u64 = bwv IPb := by + apply UScalar.eq_of_val_eq + rw [wa_valm, vIP]; simp + rw [fb, bind_ok] + simp only [lift, bind_ok, s1] + rw [s2, bind_ok] + simp only [bind_ok, s3, s4, s5] + +/-- **The CSpace walk of the root CNode**: one level, 13 bits behind a 51-bit zero guard. -/ +theorem wit_resolve {cptr : U64} (hc : cptr.val < 8192) {slot : U64} (hs : slot.val = RRb + 32 * cptr.val) : + cspace.resolve_address_bits bst S2 RC cptr 64#u64 = ok (.Ok { slot := slot, bits_left := 0#u64 }) := by + have body : cspace.resolve_address_bits_loop.body bst S2 cptr RC 64#u64 65#u64 = + ok (ControlFlow.done (.Ok { slot := slot, bits_left := 0#u64 })) := by + have l : core.num.U64.wrapping_add 13#u64 51#u64 = 64#u64 := by rfl + have f : (65#u64 : U64) - 1#u64 = ok 64#u64 := by rfl + have sh : (core.num.U64.wrapping_sub 64#u64 51#u64) &&& 63#u64 = 13#u64 := by rfl + have c13 : cptr >>> (13#u64 : U64) = ok 0#u64 := by + obtain ⟨z, hz, hv⟩ := spec_ex_fd (U64.ShiftRight_spec cptr 13#u64 (by simp)) + rw [hz] + refine congrArg ok (UScalar.eq_of_val_eq ?_) + rw [hv.1] + simp [Nat.shiftRight_eq_div_pow] + omega + have m51 : cspace.mask 51#u64 = ok 2251799813685247#u64 := by + have a : (51#u64 : U64) &&& 63#u64 = 51#u64 := by rfl + have b : (1#u64 : U64) <<< (51#u64 : U64) = ok 2251799813685248#u64 := by rfl + unfold cspace.mask + rw [if_neg (by scalar_tac)] + simp only [lift, bind_ok, a] + rw [b, bind_ok] + rfl + have g : (0#u64 : U64) &&& 2251799813685247#u64 = 0#u64 := by rfl + have s64 : (64#u64 : U64) - 64#u64 = ok 0#u64 := by rfl + have z63 : (0#u64 : U64) &&& 63#u64 = 0#u64 := by rfl + have c0 : cptr >>> (0#u64 : U64) = ok cptr := by + obtain ⟨z, hz, hv⟩ := spec_ex_fd (U64.ShiftRight_spec cptr 0#u64 (by simp)) + rw [hz] + refine congrArg ok (UScalar.eq_of_val_eq ?_) + rw [hv.1] + simp + have m13 : cspace.mask 13#u64 = ok 8191#u64 := by + have a : (13#u64 : U64) &&& 63#u64 = 13#u64 := by rfl + have b : (1#u64 : U64) <<< (13#u64 : U64) = ok 8192#u64 := by rfl + unfold cspace.mask + rw [if_neg (by scalar_tac)] + simp only [lift, bind_ok, a] + rw [b, bind_ok] + rfl + have off : cptr &&& 8191#u64 = cptr := by + apply UScalar.eq_of_val_eq + rw [UScalar.val_and] + have : (8191#u64 : U64).val = 2 ^ 13 - 1 := rfl + rw [this, Nat.and_two_pow_sub_one_eq_mod] + omega + have idx : obj.Slot.index (bwv RRb) cptr = ok slot := by + unfold obj.Slot.index + simp only [lift, bind_ok] + refine congrArg ok (UScalar.eq_of_val_eq ?_) + have z : layout.cte.SIZE.val = 32 := by simp [layout.cte.SIZE] + have : UScalar.size UScalarTy.U64 = 2 ^ 64 := by simp [U64.size_def, U64.numBits] + rw [wa_valm, core.num.U64.wrapping_mul_val_eq, vRR, z, this, hs, Nat.mod_eq_of_lt (by omega), + Nat.mod_eq_of_lt (by omega)] + omega + unfold cspace.resolve_address_bits_loop.body + simp only [lift, bind_ok, l, f, sh, c13, m51, g, s64, z63, c0, m13, off, idx] + simp + unfold cspace.resolve_address_bits + rw [rabLoop_unfold, body, bind_ok] + +theorem wit_lookupSlot {cptr : U64} (hc : cptr.val < 8192) {slot : U64} (hs : slot.val = RRb + 32 * cptr.val) : + cspace.lookup_slot bst S2 RT cptr = ok (.Ok { slot := slot, bits_left := 0#u64 }) := by + have ct : layout.tcb.SLOT_CTABLE = 0#u64 := by simp [layout.tcb.SLOT_CTABLE] + have c : capA bst S2 (slotIdx RT 0#u64) = RC := by + rw [capS2_ts (by simp), cap_rt lay1 (by simp)] + simp + have w : cspace.WORD_BITS = 64#u64 := by simp [cspace.WORD_BITS] + unfold cspace.lookup_slot + rw [ct, tcbSlot_ok 0#u64 (by simp), bind_ok, capA_eq, c, bind_ok, w] + exact wit_resolve hc hs + +theorem wit_cnodeOp (src : Bool) {cptr : U64} (hc : cptr.val < 8192) {slot : U64} (hs : slot.val = RRb + 32 * cptr.val) : + cspace.lookup_slot_for_cnode_op bst S2 src RC cptr 64#u64 = ok (.Ok slot) := by + have w : cspace.WORD_BITS = 64#u64 := by simp [cspace.WORD_BITS] + unfold cspace.lookup_slot_for_cnode_op + simp only [bind_ok, if_true, w] + rw [if_neg (by simp), if_neg (by simp), wit_resolve hc hs, bind_ok] + simp + +/-- Root CNode slot 2. -/ +local notation "SL2" => (bwv (RRb + 64)) + +theorem sl2v : (SL2).val = RRb + 32 * 2 := bwv_val (by omega) + +theorem sl2_cap : capA bst S2 SL2 = RC := by + rw [capS2_rs (i := 2) (by omega) sl2v] + unfold capOf + rw [slotC_lo (by omega)] + rfl + +/-- The extra caps: the source root, root CNode slot 2. -/ +def witExtra : Aeneas.Std.Array obj.Slot 3#usize := Array.make 3#usize [bwv (RRb + 64), 0#u64, 0#u64] + +theorem wit_extraCaps (U : WitUw uw) : ipc.lookup_extra_caps bst S2 RT (bwv IPb) + { label := 24#u64, caps_unwrapped := 0#u64, extra_caps := 1#u64, length := 5#u64 } = ok (.Ok witExtra) := by + have ne : (bwv IPb) ≠ 0#u64 := fun e => by + have := congrArg UScalar.val e + rw [vIP] at this + simp at this + have bc : msg.BUF_CAPS = ok 122#u64 := by + have a : (1#u64 : U64) + 120#u64 = ok 121#u64 := by rfl + have b : (121#u64 : U64) + 1#u64 = ok 122#u64 := by rfl + have m : msg.MSG_MAX_LENGTH = 120#u64 := by simp [msg.MSG_MAX_LENGTH] + unfold msg.BUF_CAPS msg.BUF_USER_DATA + rw [m, a, bind_ok, b] + have g : msg.buf_get bst S2 (bwv IPb) 122#u64 = ok 2#u64 := by + have v : (bwv (IPb + 976)).val = IPb + 976 := bwv_val (by omega) + rw [bufGet (k := 122#u64) (b := bwv (IPb + 976)) (by simp) (by rw [v]; simp), U.cap0 _ v] + have l := wit_lookupSlot (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) (cptr := 2#u64) (by simp) + (slot := SL2) (by rw [sl2v]; simp) + have b0 : ipc.lookup_extra_caps_loop.body bst S2 RT (bwv IPb) 1#u64 ipc.NO_EXTRA_CAPS 0#u64 = + ok (ControlFlow.cont (witExtra, 1#u64)) := by + unfold ipc.lookup_extra_caps_loop.body + rw [if_pos (by simp), if_pos (by simp), bc, bind_ok] + simp only [lift, bind_ok] + have u : ipc.NO_EXTRA_CAPS.update (UScalar.cast .Usize 0#u64) SL2 = ok witExtra := by + have c : UScalar.cast .Usize 0#u64 = 0#usize := by + apply UScalar.eq_of_val_eq; simp + rw [c] + obtain ⟨z, hz, hv⟩ := spec_ex_fd (Aeneas.Std.Array.update_spec ipc.NO_EXTRA_CAPS 0#usize SL2 (by simp)) + rw [hz, hv] + refine congrArg ok ?_ + first + | (apply Subtype.eq; rfl) + | (unfold ipc.NO_EXTRA_CAPS witExtra Aeneas.Std.Array.make Aeneas.Std.Array.set; rfl) + | (simp [ipc.NO_EXTRA_CAPS, witExtra, Aeneas.Std.Array.make, Aeneas.Std.Array.set]; done) + have q : (0#u64 : U64) + 1#u64 = ok 1#u64 := by rfl + rw [show core.num.U64.wrapping_add 122#u64 0#u64 = 122#u64 from rfl, g, bind_ok, l, bind_ok] + simp only [] + rw [u, bind_ok, q, bind_ok] + have b1 : ipc.lookup_extra_caps_loop.body bst S2 RT (bwv IPb) 1#u64 witExtra 1#u64 = + ok (ControlFlow.done (.Ok witExtra)) := by + unfold ipc.lookup_extra_caps_loop.body + rw [if_neg (by simp)] + unfold ipc.lookup_extra_caps + rw [if_neg ne] + unfold ipc.lookup_extra_caps_loop + rw [loop.eq_def] + simp only [b0, bind_ok] + rw [loop.eq_def] + simp only [b1, bind_ok] + +/-! ## The decode -/ + +/-- **The selector of Plan B's `cnodeCopyBodyA`**: the label is in 24..28 and the invoked cap is a CNode cap. -/ +theorem wit_hSel : ∀ (info : msginfo.MessageInfo) (buf : U64) (r1 : cspace.Resolved), + msg.msg_info bst S2 (bL.curW S2) = ok info → msg.lookup_ipc_buffer bst S2 false (bL.curW S2) = ok buf → + cspace.lookup_slot bst S2 (bL.curW S2) (bL.mem S2 (regWA (bL.curW S2) layout.reg.CAP)) = ok (.Ok r1) → + (24 ≤ info.label.val ∧ info.label.val ≤ 28) ∧ ∃ p rx gs g, capA bst S2 r1.slot = .CNode p rx gs g := by + intro info buf r1 h1 _ h3 + rw [curW2] at h1 h3 + rw [wit_msgInfo] at h1 + have cp : bL.mem S2 (regWA RT layout.reg.CAP) = 2#u64 := by + have e : layout.reg.CAP = 0#u64 := by simp [layout.reg.CAP, layout.reg.X0] + have h := regW2 (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) 0#u64 (by simp) + rw [tcb_reg_eqA] at h + rw [e] + exact (ok_inj_ff h).trans (by simp [witRtW]) + rw [cp, wit_lookupSlot (cptr := 2#u64) (by simp) (slot := SL2) (by rw [sl2v]; simp)] at h3 + have e1 := ok_inj_ff h1 + have e3 := ok_inj_ff h3 + injection e3 with e3 + subst e1 e3 + exact ⟨by simp, _, _, _, _, sl2_cap⟩ + +/-- **The CNode Copy decode returns `Ok`** at the witness (module docstring). -/ +theorem wit_decodeOk (U : WitUw uw) {A : Type} (ar : machine.Arch A) (a : A) (blk call don : Bool) : + ∃ s' : BS, syscall.decode_invocation bst ar S2 a 24#u64 5#u64 SL2 RC blk call don false (bwv IPb) witExtra = + ok (.Ok (), s', a) := by + have ct : sched.cur_thread bst S2 = ok RT := by rw [cur_thread_ok, curW2] + have arg : ∀ (i k : U64), i.val < 4 → msg.msg_register i = ok k → k.val < 6 → + msg.syscall_arg bst S2 RT (bwv IPb) i = ok (witRtW (256 + 8 * k.val)) := fun i k hi e hk => by + unfold msg.syscall_arg + have n : msg.N_MSG_REGISTERS = 4#u64 := by simp [msg.N_MSG_REGISTERS] + rw [n, if_pos (by scalar_tac), e, bind_ok, regW2 k hk] + have a0 : msg.syscall_arg bst S2 RT (bwv IPb) 0#u64 = ok 1000#u64 := by + rw [arg 0#u64 2#u64 (by simp) (by simp [msg.msg_register, layout.reg.X2]) (by simp)] + simp [witRtW] + have a1 : msg.syscall_arg bst S2 RT (bwv IPb) 1#u64 = ok 64#u64 := by + rw [arg 1#u64 3#u64 (by simp) (by simp [msg.msg_register, layout.reg.X3]) (by simp)] + simp [witRtW] + have a2 : msg.syscall_arg bst S2 RT (bwv IPb) 2#u64 = ok 2#u64 := by + rw [arg 2#u64 4#u64 (by simp) (by simp [msg.msg_register, layout.reg.X4]) (by simp)] + simp [witRtW] + have a3 : msg.syscall_arg bst S2 RT (bwv IPb) 3#u64 = ok 64#u64 := by + rw [arg 3#u64 5#u64 (by simp) (by simp [msg.msg_register, layout.reg.X5]) (by simp)] + simp [witRtW] + -- the destination: slot 1000 of the invoked CNode, empty + have d : (bwv (RRb + 32000)).val = RRb + 32 * 1000 := bwv_val (by omega) + have dcap : capA bst S2 (bwv (RRb + 32000)) = .Null := by + rw [capS2_rs (i := 1000) (by omega) d] + unfold capOf + rw [slotC_hi (by omega), tailC_big (by omega)] + have tgt : cspace.lookup_target_slot bst S2 RC 1000#u64 64#u64 = ok (.Ok (bwv (RRb + 32000))) := by + unfold cspace.lookup_target_slot + exact wit_cnodeOp false (by simp) (by rw [d]; simp) + have src : cspace.lookup_source_slot bst S2 RC 2#u64 64#u64 = ok (.Ok SL2) := by + unfold cspace.lookup_source_slot + exact wit_cnodeOp true (by simp) (by rw [sl2v]; simp) + have empty : cdt.ensure_empty_slot bst S2 (bwv (RRb + 32000)) = ok (.Ok ()) := by + unfold cdt.ensure_empty_slot + rw [capA_eq, dcap, bind_ok] + simp [capability.Cap.is_null] + -- the derived cap + have copied : cnode.copied_cap bst S2 24#u64 5#u64 (bwv IPb) SL2 RC = ok (.Ok (RC, false)) := by + have a4 : msg.syscall_arg bst S2 RT (bwv IPb) 4#u64 = ok 15#u64 := by + have n : msg.N_MSG_REGISTERS = 4#u64 := by simp [msg.N_MSG_REGISTERS] + have ne : (bwv IPb) ≠ 0#u64 := fun e => by + have := congrArg UScalar.val e + rw [vIP] at this + simp at this + have v : (bwv (IPb + 40)).val = IPb + 40 := bwv_val (by omega) + unfold msg.syscall_arg + rw [n, if_neg (by scalar_tac), if_pos (by simpa using ne)] + simp only [lift, bind_ok] + rw [show core.num.U64.wrapping_add 4#u64 1#u64 = 5#u64 from rfl, + bufGet (k := 5#u64) (b := bwv (IPb + 40)) (by simp) (by rw [v]; simp), U.rights _ v] + have lc : (24#u64 : U64) = fl4_abi.label.CNodeCopy := by simp [fl4_abi.label.CNodeCopy] + unfold cnode.copied_cap + have fw : capability.Rights.from_word 15#u64 = ok ⟨true, true, true, true⟩ := by + have a1 : (15#u64 : U64) &&& 1#u64 = 1#u64 := by rfl + have a2 : (15#u64 : U64) &&& 2#u64 = 2#u64 := by rfl + have a4 : (15#u64 : U64) &&& 4#u64 = 4#u64 := by rfl + have a8 : (15#u64 : U64) &&& 8#u64 = 8#u64 := by rfl + unfold capability.Rights.from_word + simp only [lift, bind_ok, a1, a2, a4, a8] + rfl + have mk : capability.mask_cap_rights ⟨true, true, true, true⟩ RC = ok RC := by rfl + have dv : objects.derive_cap bst S2 SL2 RC = ok (.Ok RC) := by rfl + rw [ct, bind_ok, if_pos lc, if_neg (by scalar_tac), a4, bind_ok, fw, bind_ok, mk, bind_ok, dv, bind_ok] + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok] + -- the effects + obtain ⟨s2, h2, -⟩ := spec_ex_fd (Fl4ProofsA64.P0.sched.set_thread_state.p0 bst S2 RT sched.TS_RESTART bstTotal) + obtain ⟨s3, h3, -⟩ := spec_ex_fd (Fl4ProofsA64.P0.cdt.insert_with_cap.p0 bst s2 RC RC SL2 (bwv (RRb + 32000)) bstTotal) + have scap : obj.Slot.cap bst SL2 S2 = ok RC := by rw [capA_eq, sl2_cap] + have copy : cnode.decode_copy bst S2 24#u64 5#u64 (bwv IPb) witExtra (bwv (RRb + 32000)) = ok (.Ok (), s3) := by + have x0 : Aeneas.Std.Array.index_usize witExtra 0#usize = ok SL2 := by rfl + have nn : obj.Slot.is_null SL2 = ok false := by + unfold obj.Slot.is_null + refine congrArg ok (decide_eq_false fun e => ?_) + have := congrArg UScalar.val e + rw [sl2v] at this + simp at this + unfold cnode.decode_copy + rw [if_neg (by scalar_tac), x0, bind_ok, nn, bind_ok] + simp only [Bool.false_eq_true, if_false] + rw [ct, bind_ok, a2, bind_ok, a3, bind_ok, scap, bind_ok, empty, bind_ok] + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok] + rw [src, bind_ok] + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok] + rw [scap, bind_ok] + simp only [capability.Cap.is_null, bind_ok, Bool.false_eq_true, if_false] + rw [copied, bind_ok] + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok] + have nz : capability.Cap.is_null RC = ok false := by simp [capability.Cap.is_null] + change (do + let b2 ← capability.Cap.is_null RC + if b2 then (do let r3 ← error.illegal Unit; ok (r3, S2)) + else do + let t2 ← sched.set_thread_state bst S2 RT sched.TS_RESTART + if false then (do let t3 ← cdt.move_cap bst t2 RC SL2 (bwv (RRb + 32000)); ok (core.result.Result.Ok (), t3)) + else (do + let t3 ← cdt.insert_with_cap bst t2 RC RC SL2 (bwv (RRb + 32000)) + ok (core.result.Result.Ok (), t3))) = _ + simp only [nz, h2, h3, bind_tc_ok, bind_ok, Bool.false_eq_true, if_false] + have cn : cnode.decode bst ar S2 a 24#u64 5#u64 RC (bwv IPb) witExtra = ok (.Ok (), s3, a) := by + have l1 : ¬ (24#u64 : U64) < fl4_abi.label.CNodeRevoke := by simp [fl4_abi.label.CNodeRevoke] + have l2 : ¬ (24#u64 : U64) > fl4_abi.label.CNodeRotate := by simp [fl4_abi.label.CNodeRotate] + have l3 : (24#u64 : U64) >= fl4_abi.label.CNodeCopy := by simp [fl4_abi.label.CNodeCopy] + have l4 : (24#u64 : U64) <= fl4_abi.label.CNodeMutate := by simp [fl4_abi.label.CNodeMutate] + unfold cnode.decode + rw [if_neg l1, if_neg l2, if_neg (by scalar_tac), ct, bind_ok, a0, bind_ok, a1, bind_ok, tgt, bind_ok] + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok] + rw [if_pos l3, if_pos l4, copy, bind_ok] + rfl + refine ⟨s3, ?_⟩ + unfold syscall.decode_invocation + rw [ct, bind_ok] + simp only [Bool.false_eq_true, if_false] + exact cn + +end + +end Fl4ProofsA64.P3.Wit diff --git a/proofs/Fl4ProofsA64/P3/A64WitJA.lean b/proofs/Fl4ProofsA64/P3/A64WitJA.lean new file mode 100644 index 000000000..a364fbb5d --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/A64WitJA.lean @@ -0,0 +1,383 @@ +/- +P3 (aarch64, A64-P3 non-vacuity witness, part 2): **`JSyscallA` is inhabited.** + +`wit_syscallA`: the syscall-entry record at the post-`schedule` state `witS` (`P3/A64WitStA`), against the spec state +`witSA` (`w1`'s with the current thread the root TCB, the current SC the root SC, the action `resume_cur_thread`, the +root TCB's `x0`–`x5` the CNode Copy message) and the ghosts `witZA`, with J5 / J7 concrete (`J5A`, `J7A` at `J7R2A`), +the heap `KtH (w1H …) {kt}` and the arch `w1Ar ar` (as `w1_stage3_one`). + +The record is carried from `w1_stage3_one` across the nine changed words by `JStage3A.of_stepFinA`; A64 P1's `BXI`, +`SysL` / `SymL` and `J7A` are carried by their frame lemmas. + +Hypotheses (those of `w1_stage3_one`): `AR a bootHwA`, [S] `numNodes = 1`, `sl ≥ MIN_BUDGET`. +`ar` is an arbitrary arch. New module; no existing statement changed. +-/ +import Fl4ProofsA64.P3.A64WitStA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Wit +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P1.W7.R.ST.BootWit Fl4ProofsA64.P3 + Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## The spec state and the ghosts -/ + +/-- The root TCB's registers: the CNode Copy message over `rootRegsA`. -/ +def witRegs : Fl4SpecA64.Register → Fl4SpecA64.Word + | .x0 => 2 | .x1 => 98437 | .x2 => 1000 | .x3 => 64 | .x4 => 2 | .x5 => 64 | .faultIP => 4194300 + | r => rootRegsA r + +def witTcbA : Fl4SpecA64.Tcb := + { rootTcbA with + tcbArch := { rootTcbA.tcbArch with + tcbContext := { rootTcbA.tcbArch.tcbContext with userRegs := witRegs } } } + +/-- **The spec state of the witness** (module docstring). -/ +def witSA (now mt sl : U64) (uw : U64 → U64) : Fl4SpecA64.State Unit := + { w1SUA KB IB 1 now mt sl uw with + kheap := Function.update (w1KA KB IB 1 now mt sl) (specPtr (bwv RTb)) (some (.tcb witTcbA)) + curThread := specPtr (bwv RTb) + curSc := specPtr (bwv SCb) + schedulerAction := .resumeCurThread } + +/-- **The ghosts of the witness**: `w1`'s with core 0's current thread and SC. -/ +def witZA : XSetsA := + { w1ZA KB IB 1 with + curOf := Function.update (w1ZA KB IB 1).curOf 0 (specPtr (bwv RTb)) + curScOf := Function.update (w1ZA KB IB 1).curScOf 0 (specPtr (bwv SCb)) } + +variable {r now mt sl : U64} {uw : U64 → U64} + +local notation "S1" => w1S KB IB 1 r now mt sl uw +local notation "S2" => witS r now mt sl uw +local notation "H1" => w1H KB IB 1 +local notation "σ1" => w1SUA KB IB 1 now mt sl uw +local notation "σ2" => witSA now mt sl uw +local notation "M1" => w1MA IB 1 + +/-- The two heaps agree off the root TCB. -/ +theorem kh_cases (p : Fl4SpecA64.ObjRef) : + (σ2).kheap p = (σ1).kheap p ∨ + (p = specPtr (bwv RTb) ∧ (σ1).kheap p = some (.tcb rootTcbA) ∧ (σ2).kheap p = some (.tcb witTcbA)) := by + by_cases h : p = specPtr (bwv RTb) + · subst h + exact Or.inr ⟨rfl, w1KA_rt, Function.update_self _ _ _⟩ + · exact Or.inl (Function.update_of_ne h _ _) + +theorem kh_obj {p : Fl4SpecA64.ObjRef} {ko : Fl4SpecA64.KernelObject} (hk : ∀ x, ko ≠ .tcb x) : + (σ2).kheap p = some ko ↔ (σ1).kheap p = some ko := by + rcases kh_cases (now := now) (mt := mt) (sl := sl) (uw := uw) p with e | ⟨-, e1, e2⟩ + · rw [e] + · rw [e1, e2] + constructor <;> intro h <;> exact absurd (Option.some.inj h).symm (hk _) + +/-- A TCB of `σ2` is `σ1`'s, or the root TCB with the new registers. -/ +theorem kh_back {p : Fl4SpecA64.ObjRef} {y : Fl4SpecA64.Tcb} (h : (σ2).kheap p = some (.tcb y)) : + ∃ x, (σ1).kheap p = some (.tcb x) ∧ (y = x ∨ (x = rootTcbA ∧ y = witTcbA)) := by + rcases kh_cases (now := now) (mt := mt) (sl := sl) (uw := uw) p with e | ⟨-, e1, e2⟩ + · exact ⟨y, e ▸ h, Or.inl rfl⟩ + · rw [e2] at h + cases h + exact ⟨_, e1, Or.inr ⟨rfl, rfl⟩⟩ + +theorem kh_fwd {p : Fl4SpecA64.ObjRef} {x : Fl4SpecA64.Tcb} (h : (σ1).kheap p = some (.tcb x)) : + ∃ y, (σ2).kheap p = some (.tcb y) ∧ (y = x ∨ (x = rootTcbA ∧ y = witTcbA)) := by + rcases kh_cases (now := now) (mt := mt) (sl := sl) (uw := uw) p with e | ⟨-, e1, e2⟩ + · exact ⟨x, e.trans h, Or.inl rfl⟩ + · rw [e1] at h + cases h + exact ⟨_, e2, Or.inr ⟨rfl, rfl⟩⟩ + +theorem kh_rt : (σ2).kheap (specPtr (bwv RTb)) = some (.tcb witTcbA) := Function.update_self _ _ _ + +/-- The caps of every spec slot are kept. -/ +theorem capAt_eq (c : Fl4SpecA64.CslotPtr) : specCapAtA σ2 c = specCapAtA σ1 c := by + rcases kh_cases (now := now) (mt := mt) (sl := sl) (uw := uw) c.1 with e | ⟨-, e1, e2⟩ + · unfold specCapAtA; rw [e] + · unfold specCapAtA; rw [e1, e2]; rfl + +theorem specSched : SpecSchedA σ1 σ2 where + kh p := by + rcases kh_cases (now := now) (mt := mt) (sl := sl) (uw := uw) p with e | ⟨-, e1, e2⟩ + · exact Or.inl e + · exact Or.inr (Or.inl ⟨_, _, e1, e2, rfl, rfl, rfl⟩) + irqS := rfl + irqN := rfl + asid := rfl + vcur := rfl + cdt := rfl + orig := rfl + +theorem ssz : SSzA σ1 σ2 := fun _ y _ h => ⟨y, (kh_obj (fun _ e => by cases e)).1 h⟩ + +theorem qkeep : QKeepA σ1 σ2 where + prio t y h := by + obtain ⟨x, hx, c⟩ := kh_back h + refine ⟨x, hx, ?_⟩ + rcases c with rfl | ⟨rfl, rfl⟩ <;> rfl + ep p x h := ⟨x, (kh_obj (fun _ e => by cases e)).1 h, List.Sublist.refl _⟩ + ntfn p x h := ⟨x, (kh_obj (fun _ e => by cases e)).1 h, List.Sublist.refl _⟩ + sc p y k h := ⟨y, k, (kh_obj (fun _ e => by cases e)).1 h, fun h => ⟨h, id⟩⟩ + rt p y h := ⟨y, (kh_obj (fun _ e => by cases e)).1 h, id⟩ + dom := ⟨rfl, rfl, rfl, rfl⟩ + +/-! ## The heap has no endpoint, notification or reply -/ + +section heap + +theorem w1H_kinds {q : U64} {k : HKind} (h : (H1) q = some k) : k ≠ .ep ∧ k ≠ .ntfn ∧ k ≠ .reply := by + unfold w1H bootH ks64H at h + split_ifs at h <;> cases h <;> exact ⟨fun e => (by cases e), fun e => (by cases e), fun e => (by cases e)⟩ + +end heap + +/-! ## The record -/ + +section rec +variable {A : Type} (AR : A → HwRootA → Prop) (ar : machine.Arch A) {a : A} (n : U64) + +local notation "KTH" => KtH (w1H KB IB 1) {bwv KTb} + +theorem kth_kinds {q : U64} {k : HKind} (h : (KTH) q = some k) : k ≠ .ep ∧ k ≠ .ntfn ∧ k ≠ .reply := by + by_cases c : q ∈ ({bwv KTb} : Set U64) + · rw [KtH.kt _ c] at h + cases h + exact ⟨fun e => (by cases e), fun e => (by cases e), fun e => (by cases e)⟩ + · have e : (KTH) q = (H1) q := Heap.add_of_not c + exact w1H_kinds (e ▸ h) + +variable + (J : JStage3EntryA bst AR (w1ZA KB IB 1) (KtH (w1H KB IB 1) {bwv KTb}) (w1OS KB IB 1) w1U + (w1H KB IB 1).scs (Fl4ProofsA64.P2.BootEpB.w1Pp KB IB 1 r now mt sl uw) w1Tb w1Lv + (IB : U64).val ((IB : U64).val + 1 * 8192) ∅ ∅ ∅ w1BfA (fun _ => []) + (w1SchedScCoreA IB) 1 (J5A bst) (J7A bst (J7R2A bst (w1Ar ar) n)) (w1S KB IB 1 r now mt sl uw) a + (w1SUA KB IB 1 now mt sl uw) (w1MA IB 1)) +include J + +theorem hT {t : U64} (ht : t ∈ (KTH).tcbs) : t ∈ (H1).tcbs := by rw [J.tcbs] at ht; exact ht +theorem hT' {t : U64} (ht : t ∈ (H1).tcbs) : t ∈ (KTH).tcbs := by rw [J.tcbs]; exact ht +theorem hTeq : (KTH).tcbs = (H1).tcbs := J.tcbs +theorem hSeq : (KTH).scs = (H1).scs := J.scs + +theorem scBitsEq {c : U64} (hc : c ∈ (H1).scs) : (KTH).scBits c = (H1).scBits c := by + have hc' : c ∈ (KTH).scs := by rw [hSeq AR ar n J]; exact hc + obtain ⟨z, hz⟩ := hc' + have n' : c ∉ ({bwv KTb} : Set U64) := fun m => by + rw [KtH.kt _ m] at hz; cases hz + have e : (KTH) c = (H1) c := Heap.add_of_not n' + unfold Heap.scBits + rw [e] + +/-- A word of `Ω` is not changed. -/ +theorem omega_keep {b : U64} (hb : b ∈ (KTH).omega) : ¬ WitW b := by + obtain ⟨⟨Q, R, q⟩, -⟩ := J.sys.ent.bxi + have o := q.omega b hb + intro w + have c0 : (0#u64 : U64) ∈ bootCs 1 := q.cur + have t := o.2.1 (bwv RTb) (hT' AR ar n J rtMem1) + have nd := o.2.2 0#u64 c0 + have e : bL.nodeBase S1 0#u64 = KBn + 223488 := by + simp [LawfulStore.nodeBase, ksb1, kbv] + rw [e] at nd + rw [vRT] at t + unfold WitW at w + omega + +theorem omegaMem {b : U64} (hb : b ∈ (KTH).omega) : bL.mem S2 b = bL.mem S1 b := mem2 (omega_keep AR ar n J hb) + +/-- A TCB word at an offset from 552 is not changed (heap-side form). -/ +theorem tfld {t K : U64} (ht : t ∈ (KTH).tcbs) (h : 552 ≤ K.val) (hK : K.val < 4096) : + bL.mem S2 (wa t K) = bL.mem S1 (wa t K) := fld_hi (hT AR ar n J ht) hK h + +theorem sqi2 {Q : U64 → U64 → List U64} {R : U64 → List U64} + (h : SchedQueuesInv bst (KTH).tcbs (bootCs 1) (KTH).omega (KTH) S1 Q R) : + SchedQueuesInv bst (KTH).tcbs (bootCs 1) (KTH).omega (KTH) S2 Q R := by + have hTP := h.tcbs + have act0 : bL.actionW S2 = 0#u64 := actW2 + refine inv_of_agree h (by rfl) (by rfl) (fun y hy => ⟨?_, ?_⟩) (fun c hc o ho _ h3 => ?_) + (fun y hy => inqA2 (hT AR ar n J hy)) (fun y hy => affA2 (hT AR ar n J hy)) + (fun y hy => Or.inl (prioA2 (hT AR ar n J hy))) (Or.inl act0) + (fun y hy => ⟨stA2 (hT AR ar n J hy), by + rw [blockA_eq]; exact tfld AR ar n J hy (by simp) (by simp)⟩) + (fun m => absurd act0 (fun e => (hTP _ m).ne0 (by rw [e]; rfl))) ?_ ?_ ?_ + · rw [nextA_eq'] + exact tfld AR ar n J hy (by simp [nextOff]) (by simp [nextOff]) + · rw [prevA_eq'] + exact tfld AR ar n J hy (by simp [prevOff]) (by simp [prevOff]) + · have hc' : c.val < 128 := by + have := (J.sr.csN) + obtain ⟨k, -, k128, hk⟩ := this + exact lt_of_lt_of_le ((hk c).1 hc) k128 + exact node_keep hc' ho fun _ => by omega + · obtain ⟨E, EQ, w1, w2, w5, w3, w4, w6⟩ := h.eps + have noE : ∀ e ∈ E, False := fun e he => by + rcases (w6.2.2.1 e).1 he with k | k + · exact (kth_kinds k).1 rfl + · exact (kth_kinds k).2.1 rfl + have hsb : ∀ y ∈ (KTH).tcbs, bL.mem S2 (stA y) = bL.mem S1 (stA y) ∧ bL.mem S2 (blockA y) = bL.mem S1 (blockA y) := + fun y hy => ⟨stA2 (hT AR ar n J hy), by rw [blockA_eq]; exact tfld AR ar n J hy (by simp) (by simp)⟩ + refine ⟨E, EQ, fun e he => (noE e he).elim, w2, w5, fun e he => (noE e he).elim, fun y hy hyX hb => ?_, + w6.agree (fun y hy => Or.inl hy) (fun y hy _ _ => hsb y hy) (fun _ _ _ hy => hy) + (fun e he => (noE e he).elim) (fun e he => (noE e he).elim) (fun e he => (noE e he).elim)⟩ + rw [(hsb y hy).1] at hb + rw [(hsb y hy).2]; exact w4 y hy hyX hb + · rw [show bL.mem S2 (bL.nodeAt S2 (bL.cpu S2) layout.node.CUR_THREAD) = bwv RTb from curW2] + exact hT' AR ar n J rtMem1 + · rw [show bL.mem S2 (bL.nodeAt S2 (bL.cpu S2) layout.node.IDLE_THREAD) = bL.idleW S1 from idleW2] + exact h.idleT + +theorem bxi2 : BXI bst (KTH).tcbs (bootCs 1) (KTH).omega (KTH) (KTH).frame (KTH).replies (KTH).scs (KTH).ntfns + (KTH).scBits (H1).scs S2 := by + obtain ⟨⟨Q, R, q⟩, hB, hX, hSg, hS⟩ : BXI bst (KTH).tcbs (bootCs 1) (KTH).omega (KTH) (KTH).frame (KTH).replies + (KTH).scs (KTH).ntfns (KTH).scBits (H1).scs S1 := J.sys.ent.bxi + have hTP := q.tcbs + have rtT : bwv RTb ∈ (KTH).tcbs := hT' AR ar n J rtMem1 + have scT : bwv SCb ∈ (KTH).scs := by rw [hSeq AR ar n J]; exact w1_rsc + have scMem : ∀ t ∈ (KTH).tcbs, bL.mem S1 (scA t) ≠ 0#u64 → bL.mem S1 (scA t) ∈ (H1).scs := fun t ht h0 => by + have := J.sr.rest.scMem t (hT AR ar n J ht) h0 + exact this + have act0 : bL.actionW S2 = 0#u64 := actW2 + have rtsc : bL.mem S2 (scA (bwv RTb)) = bwv SCb := (scA2 rtMem1).trans rt_scA1 + have sc0 : (bwv SCb) ≠ 0#u64 := fun e => by + have := congrArg UScalar.val e + rw [vSC] at this + simp at this + have idT : bL.idleW S1 ∈ (KTH).tcbs := q.idleT + have B : BoundInv bst (KTH).tcbs (H1).scs S2 := by + refine ⟨fun t ht h => ?_, fun t ht h => ?_, fun m => ?_, ?_, fun _ => ?_, ?_, fun t ht _ h0 => ?_⟩ + · rw [inqA2 (hT AR ar n J ht)] at h; rw [scA2 (hT AR ar n J ht)]; exact hB.q t ht h + · rw [inqA2 (hT AR ar n J ht)] at h; rw [scA2 (hT AR ar n J ht)]; exact hB.r t ht h + · rw [act0] at m; exact absurd rfl (fun e : (0#u64 : U64) = 0#u64 => (hTP _ m).ne0 (by rfl)) + · rw [idleW2, scA2 (hT AR ar n J idT)]; exact hB.idle + · rw [curW2, rtsc]; exact sc0 + · rw [idleW2, stA2 (hT AR ar n J idT)]; exact hB.idleNB + · rw [scA2 (hT AR ar n J ht)] at h0 ⊢; exact scMem t ht h0 + have X : XInv bst (KTH).tcbs (bootCs 1) (KTH).omega (KTH).frame (KTH).replies (KTH).scs (KTH).ntfns + (KTH).scBits S2 := by + refine hX.writes hTP writes12 (fun b w hp => ?_) (by rw [cscW2]; exact scT) + (hX.tn (TmFr.of_writes hTP q.nodes hX.1 writes12 (fun b w tm => ?_))) idleW2 + · have om : b ∈ (KTH).frame → False := fun h => omega_keep AR ar n J (hSg.sub _ h) w + rcases hp with ⟨r', hr, e⟩ | ⟨c, hc, e⟩ | ⟨t, ht, e⟩ | ⟨k, hk, rfl⟩ + · rcases e with rfl | rfl | rfl + · exact om (hX.1.tcbW hr) + · exact om (hX.1.prevW hr) + · exact om (hX.1.nextW hr) + · rcases e with rfl | rfl | rfl | rfl | rfl | rfl + · exact om (hX.1.fixW c hc).1 + · exact om (hX.1.fixW c hc).2 + · exact om (hX.1.repW c hc) + · exact om (hX.1.snW c hc) + · exact om (hX.1.syW c hc) + · exact om (hX.1.mxW c hc) + · have ht' := hT AR ar n J ht + have key : ∀ K : U64, 552 ≤ K.val → K.val < 4096 → b = wa t K → False := fun K h1 h2 e => by + rw [e, WitW, fldV ht' h2] at w + rcases tcbLoc ht' with l | l <;> omega + rcases e with rfl | rfl | rfl | rfl | rfl | rfl + · exact key _ (by simp) (by simp) rfl + · exact key _ (by simp) (by simp) rfl + · exact key _ (by simp) (by simp) (stA_eq t) + · exact key _ (by simp) (by simp) rfl + · exact key _ (by simp) (by simp) rfl + · exact key _ (by simp) (by simp) rfl + · exact om (hX.1.ntW k hk) + · rcases tm with ⟨t, ht, K, h1, h2, -, e⟩ | ⟨c, hc, o, ho, e⟩ | ⟨k, hk, -⟩ + · have ht' := hT AR ar n J ht + rw [e, WitW, fldV ht' (by omega)] at w + rcases tcbLoc ht' with l | l <;> omega + · obtain ⟨j, -, k128, hj⟩ := J.sr.csN + have hc' : c.val < 128 := lt_of_lt_of_le ((hj c).1 hc) k128 + have o' : o.val < 4288 := by omega + have v := nodeV (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) (c := c) (o := o) (by omega) o' + rw [e, WitW, v] at w + omega + · exact (kth_kinds hk).2.1 rfl + have SC : SCsOK bst (KTH).tcbs (KTH).omega (KTH).frame (KTH).scs (KTH).scBits (H1).scs S2 := by + have sg : ∀ b ∈ (KTH).frame, bL.mem S2 b = bL.mem S1 b := fun b h => omegaMem AR ar n J (hSg.sub _ h) + have hfix : ∀ c ∈ (KTH).scs, bL.mem S2 (maxA c) = bL.mem S1 (maxA c) ∧ bL.mem S2 (headA c) = bL.mem S1 (headA c) ∧ + bL.mem S2 (tailA c) = bL.mem S1 (tailA c) := fun c hc => by + have hc' : c ∈ (H1).scs := by rw [← hSeq AR ar n J]; exact hc + exact ⟨scW_keep hc' (by simp [layout.sc.REFILL_MAX]), scW_keep hc' (by simp [layout.sc.REFILL_HEAD]), + scW_keep hc' (by simp [layout.sc.REFILL_TAIL])⟩ + have wf : ∀ c ∈ (KTH).scs, RefillsWF bst S1 c ((KTH).scBits c) → RefillsWF bst S2 c ((KTH).scBits c) := + fun c hc hW => by + obtain ⟨f3, f4, f5⟩ := hfix c hc + exact hW.of_hdr f3 (by rw [show bL.rhead S2 c = bL.mem S2 (headA c) from rfl, f4]; exact hW.head_lt) + (by rw [show bL.rtail S2 c = bL.mem S2 (tailA c) from rfl, f5]; exact hW.tail_lt) + refine ⟨hS.sub, hS.ks, hS.ksΩ, fun y hy h0 hne b hb => ?_, fun c hc => (hS.all c hc).of_wf (hfix c hc).1 (wf c hc), + fun c hc => by rw [show bL.rmax S2 c = bL.rmax S1 c from (hfix c hc).1]; exact hS.act c hc, + fun c hc h0 => hS.spor c hc (by + have hc' : c ∈ (H1).scs := by rw [← hSeq AR ar n J]; exact hc + have e : bL.mem S2 (sporA c) = bL.mem S1 (sporA c) := scW_keep hc' (by simp [layout.sc.SPORADIC]) + rw [← e]; exact h0), hS.spSg, + fun c hc h0 => hS.rep c hc (by + have hc' : c ∈ (H1).scs := by rw [← hSeq AR ar n J]; exact hc + have e : bL.mem S2 (scReplyA c) = bL.mem S1 (scReplyA c) := scW_keep hc' (by simp [layout.sc.REPLY]) + rwa [e] at h0), hS.repSg, hS.scKs⟩ + rw [scA2 (hT AR ar n J hy)] at h0 hne hb + rw [cscW2] at hne ⊢ + have m := scMem y hy h0 + have z : (KTH).scBits (bwv SCb) = 7 := by + rw [scBitsEq AR ar n J w1_rsc] + rcases scLoc w1_rsc with h | h + · rw [vSC] at h; omega + · exact h.2 + rcases scLoc m with h | h + · have zy : (KTH).scBits (bL.mem S1 (scA y)) = 8 := (scBitsEq AR ar n J m).trans h.2.2 + unfold InSC at hb + unfold Outside + rw [zy] at hb + rw [z, vSC] + omega + · exact absurd h.1 hne + exact ⟨⟨Q, R, sqi2 AR ar n J q⟩, B, X, ⟨hSg.sub, hSg.ks, hSg.ksΩ⟩, SC⟩ + +/-- `SysL` and `SymL` at the witness (A64 P1's `SysL.dfN`: no liveness word is written). -/ +theorem sys2 : SysL bst (KTH) (bootCs 1) (w1OS KB IB 1) (IB : U64).val ((IB : U64).val + 1 * 8192) S2 ∧ + SymL bst (KTH) (bootCs 1) S2 := by + have bx := J.sys.ent.bxi + obtain ⟨⟨Q, R, q⟩, -⟩ := bx + have cs128 : ∀ c ∈ bootCs 1, c.val < 128 := fun c hc => by + obtain ⟨k, -, k128, hk⟩ := J.sr.csN + exact lt_of_lt_of_le ((hk c).1 hc) k128 + have l : LvF bst (KTH).tcbs (bootCs 1) (KTH).replies (KTH).scs (KTH).ntfns LvX.none S1 S2 := by + refine ⟨rfl, rfl, idleW2, fun c hc => ?_, fun y hy K hK _ => ?_, fun c hc o ho _ => ?_, + fun k hk => ?_, fun p hp => ?_⟩ + · rw [nodeAt21] + exact node_keep (cs128 c hc) (by simp [layout.node.DEBUG_TCBS]) + fun _ => by simp [layout.node.DEBUG_TCBS] + · refine tfld AR ar n J hy ?_ hK.lt.1 + unfold LvO at hK; omega + · have hc' : c ∈ (H1).scs := by rw [← hSeq AR ar n J]; exact hc + exact scW_keep hc' (by unfold LvSO at ho; omega) + · exact ((kth_kinds hk).2.1 rfl).elim + · exact ((kth_kinds hp).2.2 rfl).elim + have w' : WritesIn bst S1 S2 (fun b => DfW bst (KTH).tcbs (bootCs 1) (KTH).frame (KTH).scBits ∅ S1 b ∨ False) := by + refine writes12.mono fun b w => Or.inl (Or.inl ?_) + have node : ∀ o : U64, o.val < 4288 → o.val ≠ 88 → b.val = (bL.nodeAt S1 0#u64 o).val → + K8.KW bst (KTH).tcbs (bootCs 1) (KTH).frame ∅ (KTH).scBits ∅ S1 b := fun o h1 h2 e => + Or.inr (Or.inl ⟨0#u64, q.cur, o, h1, h2, UScalar.eq_of_val_eq e⟩) + have v : ∀ o : U64, o.val < 4288 → (bL.nodeAt S1 0#u64 o).val = KBn + 223488 + o.val := fun o ho => by + rw [nodeV (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) (by simp) ho]; simp + rcases w with w | w | w | ⟨w1, w2⟩ + · exact node 0#u64 (by simp) (by simp) (by rw [v _ (by simp), w]; simp) + · exact node 16#u64 (by simp) (by simp) (by rw [v _ (by simp), w]; simp) + · exact node 24#u64 (by simp) (by simp) (by rw [v _ (by simp), w]; simp) + · have hk : (bwv (b.val - RTb)).val = b.val - RTb := bwv_val (by omega) + refine Or.inl ⟨bwv RTb, hT' AR ar n J rtMem1, bwv (b.val - RTb), by omega, by omega, by omega, + ⟨by omega, by omega, by omega, by omega⟩, UScalar.eq_of_val_eq ?_⟩ + rw [fldV rtMem1 (by omega), vRT, hk] + omega + have d : DFB bst (KTH) (bootCs 1) (bL.curW S1) ∅ (fun _ => False) S1 S2 := + ⟨w', l.mono ⟨fun _ _ h => h.elim, fun _ _ h => h.elim, fun _ _ h => h.elim, fun _ _ h => h.elim⟩⟩ + exact SysL.dfN J.sys.ent.heap J.sys.ent.os J.sys.ent.bxi.blk (fun _ h => h.elim) (fun _ _ _ _ _ h => h) + J.sys.sysl J.symL d l + +end rec + +end + +end Fl4ProofsA64.P3.Wit diff --git a/proofs/Fl4ProofsA64/P3/A64WitRecA.lean b/proofs/Fl4ProofsA64/P3/A64WitRecA.lean new file mode 100644 index 000000000..c9c3a6b60 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/A64WitRecA.lean @@ -0,0 +1,315 @@ +/- +P3 (aarch64, A64-P3 non-vacuity witness, part 3): **`wit_syscallA`: `JSyscallA` is inhabited.** + +The syscall-entry record at the post-`schedule` state `witS` (`P3/A64WitStA`) against `witSA` and `witZA` +(`P3/A64WitJA`), with J5 / J7 concrete (`J5A`, `J7A` at `J7R2A`), the heap `KtH (w1H …) {kt}` and the arch `w1Ar ar` +(as `w1_stage3_one`). The record is carried from `w1_stage3_one` across the nine changed words by +`JStage3A.of_stepFinA`; A64 P1's `BXI`, `SysL` / `SymL` (`P3/A64WitJA`) and `J7A` are carried by their frame lemmas. + +Hypotheses (those of `w1_stage3_one`): `AR a bootHwA`, [S] `numNodes = 1`, `sl ≥ MIN_BUDGET`; `ar` is +an arbitrary arch. New module; no existing statement changed. +-/ +import Fl4ProofsA64.P3.A64WitJA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Wit +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P1.W7.R.ST.BootWit Fl4ProofsA64.P3 + Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +variable {r now mt sl : U64} {uw : U64 → U64} + +local notation "S1" => w1S KB IB 1 r now mt sl uw +local notation "S2" => witS r now mt sl uw +local notation "H1" => w1H KB IB 1 +local notation "σ1" => w1SUA KB IB 1 now mt sl uw +local notation "σ2" => witSA now mt sl uw +local notation "M1" => w1MA IB 1 +local notation "KTH" => KtH (w1H KB IB 1) {bwv KTb} +local notation "OS1" => w1OS KB IB 1 + +theorem curOf0 : witZA.curOf 0 = specPtr (bwv RTb) := by simp [witZA] +theorem curScOf0 : witZA.curScOf 0 = specPtr (bwv SCb) := by simp [witZA] +theorem curOfNe {k : Fl4SpecA64.CoreId} (h : k ≠ 0) : witZA.curOf k = (w1ZA KB IB 1).curOf k := by + simp [witZA, Function.update_of_ne h] +theorem curScOfNe {k : Fl4SpecA64.CoreId} (h : k ≠ 0) : witZA.curScOf k = (w1ZA KB IB 1).curScOf k := by + simp [witZA, Function.update_of_ne h] + +/-! ## Generic frames of J7 -/ + +section generic +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} {n : U64} + {Z : XSetsA} {H : Heap} {OS : Set U64} {ia ie : Nat} {s s' : S} {σ σ' : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} + +/-- **J7 across a step writing no `Ω` word and no ASID-table word, keeping every `OS` slot word.** -/ +theorem J7A_keepStepA {W : U64 → Prop} {Ac : Set U64} + (h : J7A st (J7R2A st ar n) Z H OS s σ m) (w : WritesIn st s s' W) (hk : L.ksb s' = L.ksb s) + (hΩ : ∀ b, W b → b ∉ H.omega) (hAs : ∀ b, W b → ¬ AsW (L.ksb s) b) + (hAt : ∀ j, L.mem s' (wa (L.ksb s') (AtOff j)) = L.mem s (wa (L.ksb s) (AtOff j))) + (osk : OSKeep st OS s s') (sy : SysL st H Z.Cs OS ia ie s' ∧ SymL st H Z.Cs s') + (blk : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s') : + J7A st (J7R2A st ar n) Z H OS s' σ' m := by + obtain ⟨Pt, Ub, Pp, Tb, Hr, Te, Kt, Lv, Re, bits, KLv, X⟩ := J7A.unp h + have armK : ArmVsK st H OS Z.V s s' := + ⟨hk, fun Ub' Pt' vr _ b hb => w.mem_eq (vr.al _ hb) fun c => hΩ b c (vr.om b hb)⟩ + have armQ : ArmQIAt st H Z.Cs OS Z.V ia ie s s' := ArmQIAt.ofW X.vr w hAs osk sy blk armK.toVs + have tab : TabKeepOrZero st s s' := ⟨hk, fun j => Or.inl (hAt j)⟩ + have Fx := S1aStepFix.ofKeep (Kt := Kt) (KLv := KLv) ⟨armQ, tab, armK, osk⟩ X.vr X.fu X.vcc X.col X.df X.r2 X.sep + X.hiK + have hsp : ∀ p, SchedPageA st Z OS s' p → p ∈ Pt := by + obtain ⟨_, -, -, -, ⟨_, _, _, _, _, _, _, R'⟩, -⟩ := Fx + exact schedPage_ptA R' X.sp + have pn : PoolNamed st OS s' := armQ.2.2.2.2.2.1 X.pn + exact J7A.of_stepFixedA Fx X.vr hk X.al X.vu pn hsp (fun b hb o ho => by rw [hk]; exact X.ks b hb o ho) + (J7R2K.of_stepFixedA (σ' := σ') (m := m) Fx X.ktK) + +end generic + +section rec +variable {A : Type} {AR : A → HwRootA → Prop} {ar : machine.Arch A} {a : A} {n : U64} + +variable + (J : JStage3EntryA bst AR (w1ZA KB IB 1) (KtH (w1H KB IB 1) {bwv KTb}) (w1OS KB IB 1) w1U + (w1H KB IB 1).scs (Fl4ProofsA64.P2.BootEpB.w1Pp KB IB 1 r now mt sl uw) w1Tb w1Lv + (IB : U64).val ((IB : U64).val + 1 * 8192) ∅ ∅ ∅ w1BfA (fun _ => []) + (w1SchedScCoreA IB) 1 (J5A bst) (J7A bst (J7R2A bst (w1Ar ar) n)) (w1S KB IB 1 r now mt sl uw) a + (w1SUA KB IB 1 now mt sl uw) (w1MA IB 1)) +include J + +theorem wstep : WritesIn bst S1 S2 (SchedStepNWA (st := bst) ∅ ∅ ∅ (w1ZA KB IB 1) M1 σ1 S1) := by + refine writes12.mono fun b w => Or.inl ?_ + have ks : (b.val = KBn + 223488 ∨ b.val = KBn + 223504 ∨ b.val = KBn + 223512) → + SchedStepWA (st := bst) (w1ZA KB IB 1) M1 σ1 S1 b := fun h => + Or.inl (Or.inr (Or.inl (by unfold SKsA; rw [ksb1, kbv]; omega))) + rcases w with w | w | w | ⟨w1, w2⟩ + · exact ks (Or.inl w) + · exact ks (Or.inr (Or.inl w)) + · exact ks (Or.inr (Or.inr w)) + · have rt : bwv RTb ∈ (w1ZA KB IB 1).T := rtMem1 + refine Or.inr ⟨bwv RTb, rt, by rw [vRT]; omega, by rw [vRT]; omega, fun e => ?_, fun e => ?_⟩ + · have := congrArg UScalar.val e + rw [fldV rtMem1 (by simp), vRT] at this + simp at this; omega + · unfold runVcpuA at e + have := congrArg UScalar.val e + rw [fldV rtMem1 (by simp), vRT] at this + simp at this; omega + +theorem osk2 : OSKeep bst OS1 S1 S2 := J.toJStage3A.osKeep_stepNA (wstep J) + +theorem j72 : J7A bst (J7R2A bst (w1Ar ar) n) (w1ZA KB IB 1) KTH OS1 S2 σ2 M1 := by + have hk : bL.ksb S2 = bL.ksb S1 := rfl + have bx := bxi2 AR ar n J + have hΩ : ∀ b, WitW b → b ∉ (KTH).omega := fun b w hb => omega_keep AR ar n J hb w + have hAs : ∀ b, WitW b → ¬ AsW (bL.ksb S1) b := fun b w h => by + obtain ⟨o, o1, o2, -, e⟩ := h + have ev : b.val = KBn + o.val := by + rw [e, wa_valm, ksb1, kbv]; exact Nat.mod_eq_of_lt (by omega) + unfold WitW at w + omega + have hAt : ∀ j, bL.mem S2 (wa (bL.ksb S2) (AtOff j)) = bL.mem S1 (wa (bL.ksb S1) (AtOff j)) := fun j => by + have v := AtOff.v j + refine ks_keep (Or.inr (Or.inl ?_)) + have e : (wa (bL.ksb S2) (AtOff j)).val = KBn + (AtOff j).val := by + rw [wa_valm, ksb2, kbv]; exact Nat.mod_eq_of_lt (by omega) + omega + exact J7A_keepStepA (Z := w1ZA KB IB 1) (σ' := σ2) J.j7 writes12 hk hΩ hAs hAt (osk2 J) (sys2 AR ar n J) + bx.blk + +theorem sr2 : SchedSRA bst witZA M1 (w1SchedScCoreA IB) S2 σ2 := by + have R := J.sr + have h := R.sch + have TT : (w1ZA KB IB 1).T = (H1).tcbs := rfl + have tT : ∀ t ∈ witZA.T, t ∈ (H1).tcbs := fun t ht => ht + have cT : ∀ c ∈ witZA.C, c ∈ (H1).scs := fun c hc => hc + have cs128 : ∀ c ∈ witZA.Cs, c.val < 128 := fun c hc => by + obtain ⟨k, -, k128, hk⟩ := R.csN + exact lt_of_lt_of_le ((hk c).1 hc) k128 + have c0 : ∀ c : U64, c.val = 0 → c = 0#u64 := fun c e => UScalar.eq_of_val_eq (by simp [e]) + have nk : ∀ c ∈ witZA.Cs, ∀ o : U64, o.val < 4288 → (c.val = 0 → o.val ≠ 0 ∧ o.val ≠ 16 ∧ o.val ≠ 24) → + bL.mem S2 (bL.nodeAt S2 c o) = bL.mem S1 (bL.nodeAt S1 c o) := fun c hc o ho h => by + rw [nodeAt21]; exact node_keep (cs128 c hc) ho h + have cpuK : ∀ o : U64, o.val < 4288 → o.val ≠ 0 → o.val ≠ 16 → o.val ≠ 24 → + bL.mem S2 (bL.nodeAt S2 (bL.cpu S2) o) = bL.mem S1 (bL.nodeAt S1 (bL.cpu S1) o) := fun o ho h1 h2 h3 => + nk 0#u64 h.cpuCs o ho fun _ => ⟨h1, h2, h3⟩ + have scK : ∀ c ∈ witZA.C, ∀ o : U64, o.val < 128 → bL.mem S2 (wa c o) = bL.mem S1 (wa c o) := + fun c hc o ho => scW_keep (cT c hc) ho + have sck : ∀ p y k, (σ2).kheap p = some (.schedContext y k) ↔ (σ1).kheap p = some (.schedContext y k) := + fun p y k => kh_obj (fun _ e => by cases e) + have scKh : ∀ c ∈ witZA.C, (σ2).kheap (scNameA M1 c) = (σ1).kheap (scNameA M1 c) := fun c hc => by + obtain ⟨y, k, e⟩ := R.scr.sc c hc + rw [e.1] + exact (sck _ _ _).2 e.1 + have rq2 : (σ2).readyQueues = (σ1).readyQueues := rfl + have rl2 : (σ2).releaseQueue = [] := rfl + have rtsc : (σ2).curSc = specPtr (bwv SCb) := rfl + have scT : bwv SCb ∈ witZA.C := w1_rsc + have sn : scRenA M1 (bwv SCb) = specPtr (bwv SCb) := w1Ren_SC layA lay1 + have rtT : bwv RTb ∈ witZA.T := rtMem1 + have sch : SchedRelNoStA (L := bL) witZA.T witZA.C witZA.Cs witZA.curOf witZA.curScOf S2 σ2 M1 := by + refine + { core := h.core + cpuCs := h.cpuCs + curOfW := fun c hc => ?_ + curScOfW := fun c hc => ?_ + cur := ?_ + csc := ?_ + idle := ?_ + idleOf := fun c hc => ?_ + idleScOf := fun c hc => ?_ + act := ?_ + time := ?_ + aff := fun t ht => ?_ + affT := h.affT + tcb := fun t ht => ?_ + sc := fun c hc => ?_ + scOff := h.scOff } + · by_cases e : c.val = 0 + · obtain rfl := c0 c e + rw [nodeAt21] + show witZA.curOf 0 = _ + exact curOf0.trans (congrArg specPtr curW2.symm) + · rw [nk c hc _ (by simp [layout.node.CUR_THREAD]) fun h => absurd h e] + exact (curOfNe e).trans (h.curOfW c hc) + · by_cases e : c.val = 0 + · obtain rfl := c0 c e + rw [nodeAt21] + show witZA.curScOf 0 = _ + refine curScOf0.trans ?_ + rw [show bL.mem S2 (bL.nodeAt S1 0#u64 layout.node.CUR_SC) = bwv SCb from cscW2] + exact sn.symm + · rw [nk c hc _ (by simp [layout.node.CUR_SC]) fun h => absurd h e] + exact (curScOfNe e).trans (h.curScOfW c hc) + · exact curOf0 + · exact curScOf0 + · rw [idleW2]; exact h.idle + · rw [nk c hc _ (by simp [layout.node.IDLE_THREAD]) fun _ => by simp [layout.node.IDLE_THREAD]] + exact h.idleOf c hc + · rw [nk c hc _ (by simp [layout.node.IDLE_SC]) fun _ => by simp [layout.node.IDLE_SC]] + exact h.idleScOf c hc + · rw [actW2]; rfl + · have e : bL.curTime S2 = bL.curTime S1 := + cpuK _ (by simp [layout.node.CUR_TIME]) (by simp [layout.node.CUR_TIME]) (by simp [layout.node.CUR_TIME]) + (by simp [layout.node.CUR_TIME]) + rw [e]; exact h.time + · rw [affA2 (tT t ht)]; exact h.aff t ht + · obtain ⟨x, hx, e⟩ := h.tcb t ht + obtain ⟨y, hy, c⟩ := kh_fwd hx + refine ⟨y, hy, ?_⟩ + rw [scA2 (tT t ht), ← e] + rcases c with rfl | ⟨rfl, rfl⟩ <;> rfl + · obtain ⟨y, k, e, f⟩ := h.sc c hc + refine ⟨y, k, (sck _ _ _).2 e, ?_⟩ + rw [scK c hc _ (by simp)]; exact f + have rest : SchedRestA (L := bL) witZA S2 σ2 M1 := by + have T := R.rest + refine + { rq := T.rq + rqDom := T.rqDom + rel := T.rel + consumed := ?_ + repr := ?_ + curDom := ⟨T.curDom.1, ?_⟩ + domTime := ?_ + prio := fun t ht => ?_ + scMem := fun t ht h0 => ?_ + refillMax := fun c hc => ?_ } + · rw [cpuK _ (by simp [layout.node.CONSUMED]) (by simp [layout.node.CONSUMED]) (by simp [layout.node.CONSUMED]) + (by simp [layout.node.CONSUMED])] + exact T.consumed + · rw [cpuK _ (by simp [layout.node.REPROGRAM]) (by simp [layout.node.REPROGRAM]) (by simp [layout.node.REPROGRAM]) + (by simp [layout.node.REPROGRAM])] + exact T.repr + · rw [show bL.ksb S2 = KB from rfl, ksw_keep (by simp)]; exact T.curDom.2 + · rw [show bL.ksb S2 = KB from rfl, ksw_keep (by simp)]; exact T.domTime + · obtain ⟨x, hx, d, dm, p⟩ := T.prio t ht + obtain ⟨y, hy, c⟩ := kh_fwd hx + have e : bL.mem S2 (domA t) = bL.mem S1 (domA t) := fld_hi (tT t ht) (by simp) (by simp) + refine ⟨y, hy, ?_, e.trans dm, ?_⟩ + · rcases c with rfl | ⟨rfl, rfl⟩ <;> exact d + · rw [prioA2 (tT t ht), p] + rcases c with rfl | ⟨rfl, rfl⟩ <;> rfl + · rw [scA2 (tT t ht)] at h0 ⊢; exact T.scMem t ht h0 + · obtain ⟨y, k, e, f⟩ := T.refillMax c hc + refine ⟨y, k, (sck _ _ _).2 e, ?_⟩ + rw [scK c hc _ (by simp)]; exact f + have scr : ScStateRelA bst witZA.C M1 S2 σ2 := by + refine R.scr.frameW writes12 (fun b w f => ?_) scKh + obtain ⟨c, hc, x, k, e, o1, o2⟩ := f + have z : k + Fl4SpecA64.minSchedContextBits ≤ (H1).scBits c := by + rw [← scBitsEq AR ar n J (cT c hc)]; exact (J.scSize c hc x k e).1 + unfold WitW at w + rcases scLoc (cT c hc) with l | l + · have : 2 ^ (k + Fl4SpecA64.minSchedContextBits) ≤ 2 ^ 8 := Nat.pow_le_pow_right (by omega) (by omega) + omega + · have : 2 ^ (k + Fl4SpecA64.minSchedContextBits) ≤ 2 ^ 7 := Nat.pow_le_pow_right (by omega) (by omega) + rw [l.1, vSC] at o1 o2 + omega + obtain ⟨⟨Q, Rq, q⟩, -⟩ := bxi2 AR ar n J + have q0 : SchedQueuesInv bst (KTH).tcbs (bootCs 1) (KTH).omega (KTH) S1 witZA.Q witZA.R := by + obtain ⟨⟨Q', R', q'⟩, -⟩ := J.sys.ent.bxi + exact SchedQueuesInv.reCoreA q' (by rw [J.tcbs]; exact R.core) + have core : SchedCoreA bst witZA.T witZA.Cs S2 witZA.Q witZA.R := by + have c := (sqi2 AR ar n J q0).toCoreA + rw [J.tcbs] at c + exact c + have run : RunQA σ2 := by + refine ⟨fun d p t ht => ?_, fun t ht => ?_⟩ + · obtain ⟨x, hx, e⟩ := R.run.1 d p t ht + obtain ⟨y, hy, c⟩ := kh_fwd hx + refine ⟨y, hy, ?_⟩ + rcases c with rfl | ⟨rfl, rfl⟩ <;> exact e + · rw [rl2] at ht; cases ht + have inv : ScInvA M1 σ2 := by + have I := R.inv + refine + { refills := fun p y k hy => I.refills p y k ((sck _ _ _).1 hy) + rr := fun p y k hy => I.rr p y k ((sck _ _ _).1 hy) + ntfnIdle := fun p x hx => I.ntfnIdle p x ((kh_obj (fun _ e => by cases e)).1 hx) + ntfnIdleOf := fun p x hx => I.ntfnIdleOf p x ((kh_obj (fun _ e => by cases e)).1 hx) + relSc := fun u hu => by rw [rl2] at hu; cases hu + sorted := by rw [rl2]; exact List.Pairwise.nil + back := fun u x p hx hs => ?_ + tcbScNotIdle := fun u x p hx hs => ?_ } + · obtain ⟨x0, h0, c⟩ := kh_back hx + have hs0 : x0.tcbSchedContext = some p := by rcases c with rfl | ⟨rfl, rfl⟩ <;> exact hs + obtain ⟨y, k, e, f⟩ := I.back u x0 p h0 hs0 + exact ⟨y, k, (sck _ _ _).2 e, f⟩ + · obtain ⟨x0, h0, c⟩ := kh_back hx + have hs0 : x0.tcbSchedContext = some p := by rcases c with rfl | ⟨rfl, rfl⟩ <;> exact hs + exact I.tcbScNotIdle u x0 p h0 hs0 + have rd : RdA σ2 := by + intro u x hx hc x' scp y k h1 h2 h3 h4 h5 + obtain ⟨x0, h0, c⟩ := kh_back hx + have hc0 : inClassRSc x0.tcbState x0.tcbFault := by rcases c with rfl | ⟨rfl, rfl⟩ <;> exact hc + obtain ⟨x1, g0, c'⟩ := kh_back h1 + have hs0 : x1.tcbSchedContext = some scp := by rcases c' with rfl | ⟨rfl, rfl⟩ <;> exact h2 + exact J.sr.rd u x0 h0 hc0 x1 scp y k g0 hs0 ((sck _ _ _).1 h3) h4 (by + change u ∉ ([] : List _) + exact List.not_mem_nil) + refine ⟨⟨sch, rest, scr, R.scp.of_kheap scKh, R.thT, R.place, R.csN, R.ksFit, ?_, core, ?_, run, ?_, ?_, inv, + fun c hc => ?_, fun c hc => ?_⟩, rd⟩ + · rw [curW2]; exact rtT + · rw [curW2, affA2 rtMem1, rt_affA1]; rfl + · intro u hu; cases hu + · change bL.cscW S2 ∈ _ + rw [cscW2]; exact scT + · unfold scCoreA + rw [scK c hc _ (by simp [layout.sc.CORE])] + exact R.scCore c hc + · by_cases e : c.val = 0 + · obtain rfl := c0 c e + rw [nodeAt21, show bL.mem S2 (bL.nodeAt S1 0#u64 layout.node.CUR_SC) = bwv SCb from cscW2] + exact scT + · rw [nk c hc _ (by simp [layout.node.CUR_SC]) fun h => absurd h e] + exact R.cscAll c hc + +end rec + +end + +end Fl4ProofsA64.P3.Wit diff --git a/proofs/Fl4ProofsA64/P3/A64WitStA.lean b/proofs/Fl4ProofsA64/P3/A64WitStA.lean new file mode 100644 index 000000000..60c7e30c4 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/A64WitStA.lean @@ -0,0 +1,216 @@ +/- +P3 (aarch64, A64-P3 non-vacuity witness, part 1): **a post-`schedule` state** built from A64-Boot's `w1` +(`P3/A64WitW1A`, concrete `kb`, `ib`, one core), and its FL4 frame facts. + +The memory `witM` is `w1`'s with nine words changed: +* core 0's node: `CUR_THREAD` := the root TCB, `SCHED_ACTION` := 0 (`resume_cur_thread`), `CUR_SC` := the root SC + (what `schedule` + `activate_thread` onto the root thread leave there); +* the root TCB's registers `x0`–`x5`: a CNode Copy message (`x0` = the CPtr of the root CNode cap, `x1` = the message + info `CNodeCopy`, one extra cap, length 5; `x2`–`x5` = destination index / depth, source index / depth). + +The state is built directly, not as the output of a run of `schedule` (no kernel-exit producer exists yet). + +No premise. New module; no existing statement changed. +-/ +import Fl4ProofsA64.P3.A64WitW1A +import Fl4ProofsA64.P3.Stage3SyscallA +import Fl4ProofsA64.P3.Stage3IpcGlueA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Wit +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P1.W7.R.ST.BootWit Fl4ProofsA64.P3 + Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add +/-! ## The state -/ + +/-- **The nine changed words** (module docstring). -/ +def witM (mem : U64 → U64) (b : U64) : U64 := + if b.val = KBn + 223488 then bwv RTb + else if b.val = KBn + 223504 then 0#u64 + else if b.val = KBn + 223512 then bwv SCb + else if b.val = RTb + 256 then 2#u64 + else if b.val = RTb + 264 then 98437#u64 + else if b.val = RTb + 272 then 1000#u64 + else if b.val = RTb + 280 then 64#u64 + else if b.val = RTb + 288 then 2#u64 + else if b.val = RTb + 296 then 64#u64 + else if b.val = RTb + 528 then 4194300#u64 + else mem b + +/-- **The witness state.** -/ +def witS (r now mt sl : U64) (uw : U64 → U64) : BS := ⟨witM (w1M KB IB 1 r now mt sl uw), KB, 0#u64⟩ + +/-- The words `witM` may change. -/ +def WitW (b : U64) : Prop := + b.val = KBn + 223488 ∨ b.val = KBn + 223504 ∨ b.val = KBn + 223512 ∨ (RTb + 256 ≤ b.val ∧ b.val < RTb + 552) + +theorem witM_keep {mem : U64 → U64} {b : U64} (h : ¬ WitW b) : witM mem b = mem b := by + unfold WitW at h + unfold witM + rw [if_neg (by omega), if_neg (by omega), if_neg (by omega), if_neg (by omega), if_neg (by omega), + if_neg (by omega), if_neg (by omega), if_neg (by omega), if_neg (by omega), if_neg (by omega)] + +variable {r now mt sl : U64} {uw : U64 → U64} + +local notation "S1" => w1S KB IB 1 r now mt sl uw +local notation "S2" => witS r now mt sl uw +local notation "H1" => w1H KB IB 1 + +theorem mem2 {b : U64} (h : ¬ WitW b) : bL.mem S2 b = bL.mem S1 b := witM_keep h + +theorem ksb2 : bL.ksb S2 = KB := rfl +theorem ksb1 : bL.ksb S1 = KB := rfl +theorem cpu2 : bL.cpu S2 = 0#u64 := rfl +theorem cpu1 : bL.cpu S1 = 0#u64 := rfl + +theorem writes12 : WritesIn bst S1 S2 WitW := ⟨rfl, rfl, fun _ _ h => mem2 h⟩ + +theorem nodeAt21 (c o : U64) : bL.nodeAt S2 c o = bL.nodeAt S1 c o := nodeAt_of_ksb (by rfl) c o + +theorem nodeV {c o : U64} (hc : c.val ≤ 128) (ho : o.val < 4288) : + (bL.nodeAt S1 c o).val = KBn + 223488 + c.val * 4288 + o.val := by + have e : bL.nodeAt S1 c o = bL.nodeAt (bootS KB IB 1 r now mt) c o := nodeAt_of_ksb (by rfl) c o + rw [e, nodeAt_bv (r := r) (now := now) (mt := mt) lay0 hc ho, kbv] + +/-- A node word other than core 0's `CUR_THREAD`, `SCHED_ACTION`, `CUR_SC`. -/ +theorem node_keep {c o : U64} (hc : c.val < 128) (ho : o.val < 4288) + (h : c.val = 0 → o.val ≠ 0 ∧ o.val ≠ 16 ∧ o.val ≠ 24) : + bL.mem S2 (bL.nodeAt S1 c o) = bL.mem S1 (bL.nodeAt S1 c o) := by + refine mem2 fun w => ?_ + have v := nodeV (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) (c := c) (o := o) (by omega) ho + unfold WitW at w + omega + +/-! ## Where the objects lie -/ + +/-- A TCB is an idle TCB or the root TCB. -/ +theorem tcbLoc {t : U64} (ht : t ∈ (H1).tcbs) : + (IBn ≤ t.val ∧ t.val + 4096 ≤ IBn + 1 * 8192) ∨ t.val = RTb := by + rcases w1H_tcbs lay1 ht with ⟨c, hc, e⟩ | e + · rw [ibv] at e; exact Or.inl ⟨by omega, by omega⟩ + · exact Or.inr e + +/-- An SC is an idle SC or the root SC. -/ +theorem scLoc {c : U64} (hc : c ∈ (H1).scs) : + (IBn + 4096 ≤ c.val ∧ c.val + 4096 ≤ IBn + 1 * 8192 ∧ (H1).scBits c = 8) ∨ (c = bwv SCb ∧ (H1).scBits c = 7) := by + rcases w1Sc lay1 hc with ⟨h, z⟩ | h + · obtain ⟨j, hj, e, -⟩ := memS lay0 h + rw [ibv] at e + exact Or.inl ⟨by omega, by omega, z⟩ + · exact Or.inr h + +theorem tcbFit {t : U64} (ht : t ∈ (H1).tcbs) : t.val + 4096 < 2 ^ 64 := by + rcases tcbLoc ht with h | h <;> omega + +theorem fldV {t K : U64} (ht : t ∈ (H1).tcbs) (hK : K.val < 4096) : (wa t K).val = t.val + K.val := by + have := tcbFit ht + rw [wa_valm] + exact Nat.mod_eq_of_lt (by omega) + +/-- A TCB word other than the root TCB's registers `x0`–`x5`. -/ +theorem fld_keep {t K : U64} (ht : t ∈ (H1).tcbs) (hK : K.val < 4096) (h : t.val = RTb → K.val < 256 ∨ 552 ≤ K.val) : + bL.mem S2 (wa t K) = bL.mem S1 (wa t K) := by + refine mem2 fun w => ?_ + rw [WitW, fldV ht hK] at w + rcases tcbLoc ht with l | l <;> omega + +/-- A TCB field from offset 552 (past the register file). -/ +theorem fld_hi {t K : U64} (ht : t ∈ (H1).tcbs) (hK : K.val < 4096) (h : 552 ≤ K.val) : + bL.mem S2 (wa t K) = bL.mem S1 (wa t K) := fld_keep ht hK fun _ => Or.inr h + +theorem scA2 {t : U64} (ht : t ∈ (H1).tcbs) : bL.mem S2 (scA t) = bL.mem S1 (scA t) := + fld_hi ht (by simp) (by simp) +theorem stA2 {t : U64} (ht : t ∈ (H1).tcbs) : bL.mem S2 (stA t) = bL.mem S1 (stA t) := by + rw [stA_eq]; exact fld_hi ht (by simp) (by simp) +theorem inqA2 {t : U64} (ht : t ∈ (H1).tcbs) : bL.mem S2 (inqA t) = bL.mem S1 (inqA t) := + fld_hi ht (by simp) (by simp) +theorem affA2 {t : U64} (ht : t ∈ (H1).tcbs) : bL.mem S2 (affA t) = bL.mem S1 (affA t) := + fld_hi ht (by simp) (by simp) +theorem prioA2 {t : U64} (ht : t ∈ (H1).tcbs) : bL.mem S2 (prioA t) = bL.mem S1 (prioA t) := + fld_hi ht (by simp) (by simp) + +/-- A word below the idle area's end that is not in core 0's node, or in the root-task block outside the root TCB. -/ +theorem ks_keep {b : U64} (h : b.val < KBn + 223488 ∨ (KBn + 223520 ≤ b.val ∧ b.val < RTb + 256) ∨ RTb + 552 ≤ b.val) : + bL.mem S2 b = bL.mem S1 b := by + refine mem2 fun w => ?_ + unfold WitW at w + omega + +theorem ksw_keep {o : U64} (ho : o.val < 223488) : bL.mem S2 (wa KB o) = bL.mem S1 (wa KB o) := by + refine ks_keep (Or.inl ?_) + rw [wa_valm, kbv] + rw [Nat.mod_eq_of_lt (by omega)] + omega + +theorem scW_keep {c o : U64} (hc : c ∈ (H1).scs) (ho : o.val < 128) : bL.mem S2 (wa c o) = bL.mem S1 (wa c o) := by + have v : (wa c o).val = c.val + o.val := by + rw [wa_valm] + refine Nat.mod_eq_of_lt ?_ + rcases scLoc hc with h | ⟨h, -⟩ + · omega + · rw [h, vSC]; omega + refine ks_keep ?_ + rw [v] + rcases scLoc hc with h | ⟨h, -⟩ + · exact Or.inr (Or.inl ⟨by omega, by omega⟩) + · rw [h, vSC]; exact Or.inr (Or.inr (by omega)) + +/-! ## The new scheduler words -/ + +theorem curW2 : bL.curW S2 = bwv RTb := by + have v := nodeV (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) (c := 0#u64) + (o := layout.node.CUR_THREAD) (by simp) (by simp [layout.node.CUR_THREAD]) + change witM _ (bL.nodeAt S2 0#u64 layout.node.CUR_THREAD) = _ + rw [nodeAt21] + unfold witM + rw [if_pos (by rw [v]; simp [layout.node.CUR_THREAD])] + +theorem actW2 : bL.actionW S2 = 0#u64 := by + have v := nodeV (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) (c := 0#u64) + (o := layout.node.SCHED_ACTION) (by simp) (by simp [layout.node.SCHED_ACTION]) + have e : (bL.nodeAt S1 0#u64 layout.node.SCHED_ACTION).val = KBn + 223504 := by + rw [v]; simp [layout.node.SCHED_ACTION] + change witM _ (bL.nodeAt S2 0#u64 layout.node.SCHED_ACTION) = _ + rw [nodeAt21] + unfold witM + rw [if_neg (by omega), if_pos e] + +theorem cscW2 : bL.cscW S2 = bwv SCb := by + have v := nodeV (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) (c := 0#u64) + (o := layout.node.CUR_SC) (by simp) (by simp [layout.node.CUR_SC]) + have e : (bL.nodeAt S1 0#u64 layout.node.CUR_SC).val = KBn + 223512 := by + rw [v]; simp [layout.node.CUR_SC] + change witM _ (bL.nodeAt S2 0#u64 layout.node.CUR_SC) = _ + rw [nodeAt21] + unfold witM + rw [if_neg (by omega), if_neg (by omega), if_pos e] + +theorem idleW2 : bL.idleW S2 = bL.idleW S1 := by + change bL.mem S2 (bL.nodeAt S2 0#u64 layout.node.IDLE_THREAD) = bL.mem S1 (bL.nodeAt S1 0#u64 layout.node.IDLE_THREAD) + rw [nodeAt21] + exact node_keep (by simp) (by simp [layout.node.IDLE_THREAD]) fun _ => by simp [layout.node.IDLE_THREAD] + +/-! ## Root TCB words -/ + +theorem rtMem1 : bwv RTb ∈ (H1).tcbs := w1_rtT + +theorem rt_word {o : U64} {k : Nat} (hk : o.val = k) (h : k < 4096) : + bL.mem S1 (wa (bwv RTb) o) = rtW IB k := by + have v := fldV (t := bwv RTb) (K := o) rtMem1 (by omega) + exact w1M_rt lay1 h (by rw [v, vRT, hk]) + +theorem rt_scA1 : bL.mem S1 (scA (bwv RTb)) = bwv SCb := + (rt_word (o := 3464#u64) (k := 3464) (by simp) (by omega)).trans (by simp [rtW]) + +theorem rt_stA1 : bL.mem S1 (stA (bwv RTb)) = 1#u64 := by + rw [stA_eq]; exact (rt_word (k := 3328) (by simp) (by omega)).trans (by simp [rtW]) + +theorem rt_affA1 : bL.mem S1 (affA (bwv RTb)) = 0#u64 := + (rt_word (o := 3520#u64) (k := 3520) (by simp) (by omega)).trans (by simp [rtW]) + +end + +end Fl4ProofsA64.P3.Wit diff --git a/proofs/Fl4ProofsA64/P3/A64WitSyscA.lean b/proofs/Fl4ProofsA64/P3/A64WitSyscA.lean new file mode 100644 index 000000000..9c0cc7e24 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/A64WitSyscA.lean @@ -0,0 +1,372 @@ +/- +P3 (aarch64, A64-P3 non-vacuity witness, part 4): **`wit_syscallA`: `JSyscallA` is inhabited.** + +The syscall-entry record `JSyscallA` at the post-`schedule` state `witS` (`P3/A64WitStA`) against `witSA` and `witZA` +(`P3/A64WitJA`), with J5 / J7 concrete (`J5A`, `J7A` at `J7R2A`), the heap `KtH (w1H …) {kt}` and the arch `w1Ar ar` +(as `w1_stage3_one`). Carried from `w1_stage3_one` across the nine changed words by `JStage3A.of_stepFinA`. + +* `wit_syscallA`: the record, from the hypotheses of `w1_stage3_one` (`AR a bootHwA`, [S] `numNodes = 1`, + `sl ≥ MIN_BUDGET`; `ar` an arbitrary arch). + +Named premise: [S] `numNodes = 1`. New module; no existing statement changed. +-/ +import Fl4ProofsA64.P3.A64WitRecA +import Fl4ProofsA64.P3.DelNtfnBufA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Wit +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P1.W7.R.ST.BootWit Fl4ProofsA64.P3 + Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +variable {r now mt sl : U64} {uw : U64 → U64} + +local notation "S1" => w1S KB IB 1 r now mt sl uw +local notation "S2" => witS r now mt sl uw +local notation "H1" => w1H KB IB 1 +local notation "σ1" => w1SUA KB IB 1 now mt sl uw +local notation "σ2" => witSA now mt sl uw +local notation "M1" => w1MA IB 1 +local notation "KTH" => KtH (w1H KB IB 1) {bwv KTb} +local notation "OS1" => w1OS KB IB 1 + +/-! ## The thread relations -/ + +/-- The root TCB's words at the witness. -/ +def witRtW (o : Nat) : U64 := + if o = 256 then 2#u64 else if o = 264 then 98437#u64 else if o = 272 then 1000#u64 else + if o = 280 then 64#u64 else if o = 288 then 2#u64 else if o = 296 then 64#u64 else if o = 528 then 4194300#u64 else rtW IB o + +theorem wit_rtWords : TcbWordsA (bL.mem S2) (bwv RTb) witRtW := by + intro x o ho _ hx + rw [vRT] at hx + have h1 : bL.mem S1 x = rtW IB o := w1M_rt lay1 ho hx + change witM (w1M KB IB 1 r now mt sl uw) x = _ + change w1M KB IB 1 r now mt sl uw x = _ at h1 + unfold witM witRtW + rw [if_neg (by omega), if_neg (by omega), if_neg (by omega), h1] + have e : ∀ k : Nat, (x.val = RTb + k) = (o = k) := fun k => propext (by omega) + simp only [e] + +theorem wit_rtRegs (q : Fl4SpecA64.Register) : + (witRtW (256 + 8 * (regIdxA q).val)).bv = witTcbA.tcbArch.tcbContext.userRegs q := by + cases q <;> rfl + +theorem rtAddr : TcbAddr (bwv RTb) := w1_tcbPlaced (kb := KB) (ib := IB) (nc := 1) lay1 _ rtMem1 + +/-- Every TCB's endpoint and notification relations at the witness. -/ +theorem wit_tcbs : ∀ t ∈ (H1).tcbs, ∃ x, (σ2).kheap (specPtr t) = some (.tcb x) ∧ + TcbRelE ∅ (bL.mem S2) t x ∧ TcbRel1 (bL.mem S2) t x := by + intro t ht + rcases w1T lay1 ht with h | rfl + · obtain ⟨c, hc, W⟩ := w1_wordsA (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) lay1 h + obtain ⟨c', hc', rfl⟩ := bootT_of layA h + have e : (σ2).kheap (specPtr (idleTA IB c')) = + some (.tcb (idleTcbA (scRenA (bootMA IB 1) (idleSA IB c')))) := + (Function.update_of_ne (idle_ne_rtA layA lay1 hc') _ _).trans (w1KA_idle layA lay1 hc') + have W2 : TcbWordsA (bL.mem S2) (idleTA IB c') (tcbW IB c) := fun x o ho h' hx => by + refine (mem2 fun w => ?_).trans (W x o ho h' hx) + unfold WitW at w + rcases w1H_tcbs lay1 ht with ⟨k, hk, ek⟩ | ek + · rw [ibv] at ek; omega + · have := idleTA_val' layA hc' + rw [ibv] at this; omega + exact ⟨_, e, idle_relsA _ (w1_tcbPlaced (kb := KB) (ib := IB) (nc := 1) lay1 _ ht) W2⟩ + · exact ⟨_, kh_rt, tcbRels_of_wordsA rtAddr wit_rtWords rfl (Or.inl rfl) wit_rtRegs rfl rfl rfl rfl rfl⟩ + +/-! ## `of_stepFinA` keeping the queues -/ + +section generic +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {AR : A → HwRootA → Prop} {Z : XSetsA} + {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} + {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} {k : Nat} {ar : machine.Arch A} + {nA : U64} {s s' : S} {a : A} {σ σ' : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + {f g : Fl4SpecA64.CoreId → Fl4SpecA64.ObjRef} + +/-- `JStage3A.of_stepFinA` for a step that only moves the current core's thread and SC ghosts. -/ +theorem stage3_curA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ k (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (w : WritesIn st s s' (SchedStepNWA (st := st) N E Rp Z m σ s)) (HS : SpecSchedA σ σ') (SSz : SSzA σ σ') + (Qk : QKeepA σ σ') (hum : σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory) + (hcurOf : ∀ c, c ≠ m.curCore → f c = Z.curOf c) + (bxi : BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s') + (sysl : SysL st H Z.Cs OS ia ie s') (symL : SymL st H Z.Cs s') + (userOK : Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s') + (j7 : J7A st (J7R2A st ar nA) Z H OS s' σ' m) + (sr : SchedSRA st { Z with curOf := f, curScOf := g } m κ s' σ') + (ep : EpStateRel Z.T E Rp Bf (L.mem s') σ') (ntfn : NtfnStateRel N Z.T (L.mem s') σ') + (reply : ReplyStackRelA (L.mem s') Rp Z.C σ' (scNameA m) stack) + (bufCover : BufCover st Z.T Bf s' σ') (bufAl : UtBufAlA Z.T σ') (idleNoFault : IdleNoFaultA m σ') + (mcp : McpRelA Z.T m (L.mem s') σ') (yieldRel : YieldRelA Z.T Z.C m (L.mem s') σ') (sinv : SInvXA σ' m k) + (sidle : SIdleA σ' m k) (runVcpu : RunVcpuInv st Z.T s') (blkQ : BlkQueuedA Z.T E N (L.mem s') σ') + (schedValid : SchedValidA σ') : + JStage3A st AR { Z with curOf := f, curScOf := g } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ k (J5A st) + (J7A st (J7R2A st ar nA)) s' a σ' m := + J.of_stepFinA (Q := Z.Q) (Rl := Z.R) (curOf := f) (curScOf := g) w HS SSz Qk hum hcurOf bxi sysl symL userOK j7 sr ep + ntfn reply bufCover bufAl idleNoFault mcp yieldRel sinv sidle runVcpu blkQ schedValid + +end generic + +/-! ## The record -/ + +section rec +variable {A : Type} {AR : A → HwRootA → Prop} {ar : machine.Arch A} {a : A} {n : U64} + +variable + (J : JStage3EntryA bst AR (w1ZA KB IB 1) (KtH (w1H KB IB 1) {bwv KTb}) (w1OS KB IB 1) w1U + (w1H KB IB 1).scs (Fl4ProofsA64.P2.BootEpB.w1Pp KB IB 1 r now mt sl uw) w1Tb w1Lv + (IB : U64).val ((IB : U64).val + 1 * 8192) ∅ ∅ ∅ w1BfA (fun _ => []) + (w1SchedScCoreA IB) 1 (J5A bst) (J7A bst (J7R2A bst (w1Ar ar) n)) (w1S KB IB 1 r now mt sl uw) a + (w1SUA KB IB 1 now mt sl uw) (w1MA IB 1)) +include J + +theorem wit_stage3A : JStage3A bst AR witZA KTH OS1 w1U (H1).scs + (Fl4ProofsA64.P2.BootEpB.w1Pp KB IB 1 r now mt sl uw) w1Tb w1Lv (IB : U64).val ((IB : U64).val + 1 * 8192) + ∅ ∅ ∅ w1BfA (fun _ => []) (w1SchedScCoreA IB) 1 (J5A bst) (J7A bst (J7R2A bst (w1Ar ar) n)) S2 a σ2 M1 := by + have tT : ∀ t ∈ (w1ZA KB IB 1).T, t ∈ (H1).tcbs := fun t ht => ht + have cT : ∀ c ∈ (w1ZA KB IB 1).C, c ∈ (H1).scs := fun c hc => hc + have sck : ∀ p y k, (σ2).kheap p = some (.schedContext y k) ↔ (σ1).kheap p = some (.schedContext y k) := + fun p y k => kh_obj (fun _ e => by cases e) + have tk : ∀ t ∈ (w1ZA KB IB 1).T, ∀ K : U64, 552 ≤ K.val → K.val < 4096 → + bL.mem S2 (wa t K) = bL.mem S1 (wa t K) := fun t ht K h1 h2 => fld_hi (tT t ht) h2 h1 + have hcurOf : ∀ k, k ≠ (M1).curCore → witZA.curOf k = (w1ZA KB IB 1).curOf k := fun k hk => curOfNe hk + have userOK : Fl4ProofsA64.P1.UserOK bst (KTH).tcbs (w1ZA KB IB 1).Cs (KTH).omega (KTH) w1U S2 := fun b hb => + let ⟨o, h⟩ := J.sys.ent.user b hb + ⟨o.ksb rfl, h⟩ + have ep : EpStateRel (w1ZA KB IB 1).T ∅ ∅ w1BfA (bL.mem S2) σ2 := by + refine ⟨fun e h => absurd h (Set.notMem_empty _), fun e h => absurd h (Set.notMem_empty _), fun t ht => ?_, + fun bp h k hk => ?_⟩ + · obtain ⟨x, hx, e, -⟩ := wit_tcbs (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) t (tT t ht) + exact ⟨x, hx, e⟩ + · have B := J.ep.buf bp h k hk + change bp ∈ ({(bwv IPb, specPtr (bwv IPb))} : Set (U64 × Fl4SpecA64.Word)) at h + rw [Set.mem_singleton_iff] at h + have e : bL.mem S2 (bufW bp.1 k) = bL.mem S1 (bufW bp.1 k) := by + refine ks_keep (Or.inr (Or.inl ?_)) + rw [h, bufW_ipb hk] + omega + rw [e]; exact B + have ntfn : NtfnStateRel ∅ (w1ZA KB IB 1).T (bL.mem S2) σ2 := by + refine ⟨fun e h => absurd h (Set.notMem_empty _), fun t ht => ?_⟩ + obtain ⟨x, hx, -, e⟩ := wit_tcbs (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) t (tT t ht) + exact ⟨x, hx, e⟩ + have reply : ReplyStackRelA (bL.mem S2) ∅ (w1ZA KB IB 1).C σ2 (scNameA M1) (fun _ => []) := by + refine ⟨fun c hc => ?_, fun _ _ _ _ _ h => absurd h List.not_mem_nil, fun _ h => absurd h (Set.notMem_empty _), + fun _ h => absurd h (Set.notMem_empty _)⟩ + obtain ⟨⟨x, k, e, f⟩, -⟩ := J.reply.stackRel c hc + exact ⟨⟨x, k, (sck _ _ _).2 e, f⟩, fun _ h => absurd h List.not_mem_nil, List.nodup_nil, + fun i hi => absurd hi (Nat.not_lt_zero _)⟩ + have bufCover : BufCover bst (w1ZA KB IB 1).T w1BfA S2 σ2 := by + refine Fl4ProofsA64.P3.BufCover.frameK J.bufCover (fun t ht => tk t ht _ (by simp) (by simp)) (fun t ht => ?_) (fun t ht => ?_) + (fun p x hx => ?_) + · have e : sbWordA t = slotIdx t layout.tcb.SLOT_BUFFER := by + have z : layout.cte.SIZE = 32#u64 := by simp [layout.cte.SIZE] + unfold sbWordA slotIdx; rw [z] + have hy : IsSlotA (w1ZA KB IB 1).th (sbWordA t) := by + rw [e]; exact isSlot_tcbA ((J.sr.thT t).2 ht) (by simp [layout.tcb.SLOT_BUFFER]) + have o := osk2 J _ (J.j5.2.1 _ hy) + unfold capA; rw [o.1, o.2.1] + · obtain ⟨x, hx, -⟩ := J.ep.tcb t ht + exact ⟨x, hx⟩ + · obtain ⟨y, hy, c⟩ := kh_fwd hx + refine ⟨y, hy, ?_⟩ + rcases c with rfl | ⟨rfl, rfl⟩ <;> exact ⟨rfl, rfl⟩ + have bufAl : UtBufAlA (w1ZA KB IB 1).T σ2 := fun t ht y hy => by + obtain ⟨x, hx, c⟩ := kh_back hy + have := J.bufAl t ht x hx + rcases c with rfl | ⟨rfl, rfl⟩ <;> exact this + have idle : IdleNoFaultA M1 σ2 := fun c hc => by + obtain ⟨x, hx, f⟩ := J.idleNoFault c hc + obtain ⟨y, hy, d⟩ := kh_fwd hx + refine ⟨y, hy, ?_⟩ + rcases d with rfl | ⟨rfl, rfl⟩ <;> exact f + have mcp : McpRelA (w1ZA KB IB 1).T M1 (bL.mem S2) σ2 := + J.mcp.congrA (fun t ht => ⟨tk t ht _ (by simp) (by simp), tk t ht _ (by simp) (by simp)⟩) + (fun t ht x hx => by + obtain ⟨y, hy, d⟩ := kh_fwd hx + refine ⟨y, hy, ?_⟩ + rcases d with rfl | ⟨rfl, rfl⟩ <;> exact ⟨rfl, rfl⟩) + have yr : YieldRelA (w1ZA KB IB 1).T (w1ZA KB IB 1).C M1 (bL.mem S2) σ2 := by + have ye : ∀ t ∈ (w1ZA KB IB 1).T, bL.mem S2 (yieldA t) = bL.mem S1 (yieldA t) := fun t ht => + tk t ht _ (by simp) (by simp) + refine ⟨fun t ht => ?_, fun t ht h => ?_, fun c hc y k h p hp => ?_⟩ + · obtain ⟨x, hx, f⟩ := J.yieldRel.tcb t ht + obtain ⟨y, hy, d⟩ := kh_fwd hx + refine ⟨y, hy, ?_⟩ + rw [ye t ht] + rcases d with rfl | ⟨rfl, rfl⟩ <;> exact f + · rw [ye t ht] at h ⊢ + obtain ⟨m, y, k, e, f⟩ := J.yieldRel.toSc t ht h + exact ⟨m, y, k, (sck _ _ _).2 e, f⟩ + · obtain ⟨t, ht, e, f⟩ := J.yieldRel.frm c hc y k ((sck _ _ _).1 h) p hp + exact ⟨t, ht, e, (ye t ht).trans f⟩ + have I := J.sinv + have rq0 : ∀ d p, (σ2).readyQueues d p = [] := fun _ _ => rfl + have rl0 : (σ2).releaseQueue = [] := rfl + have sinv : SInvXA σ2 M1 1 := by + refine + { mc := I.mc + view := I.view + curTcb := ⟨_, kh_rt⟩ + idleTcb := ?_ + onlyIdle := fun t x h e => ?_ + idleSc := ?_ + curSc := ⟨_, _, (sck _ _ _).2 w1KA_sc⟩ + ctQ := fun _ d p h => by rw [rq0] at h; cases h + idleQ := fun d p c _ h => by rw [rq0] at h; cases h + rq := fun d p t h => by rw [rq0] at h; cases h + rls := fun t h => by rw [rl0] at h; cases h + rpos := fun p y k h => I.rpos p y k ((sck _ _ _).1 h) + irqN := ?_ + zc := fun t x hx e hr => by rw [rl0] at hr; cases hr + rlsRun := fun t h => by rw [rl0] at h; cases h + curScReady := fun hne => absurd rfl hne + faultSt := fun t x h hf => by + obtain ⟨x0, h0, d⟩ := kh_back h + rcases d with rfl | ⟨rfl, rfl⟩ + · exact I.faultSt _ _ h0 hf + · exact I.faultSt t rootTcbA h0 hf } + · obtain ⟨x, hx, e⟩ := I.idleTcb + obtain ⟨y, hy, d⟩ := kh_fwd hx + refine ⟨y, hy, ?_⟩ + rcases d with rfl | ⟨rfl, rfl⟩ <;> exact e + · obtain ⟨x0, h0, d⟩ := kh_back h + refine I.onlyIdle t x0 h0 ?_ + rcases d with rfl | ⟨rfl, rfl⟩ + · exact e + · cases e + · obtain ⟨x, y, k, hx, e1, e2, e3⟩ := I.idleSc + obtain ⟨x', hx', d⟩ := kh_fwd hx + refine ⟨x', y, k, hx', ?_, (sck _ _ _).2 e2, e3⟩ + rcases d with rfl | ⟨rfl, rfl⟩ <;> exact e1 + · exact SpecIrqA.of_eq (SpecIrqA.updTcbA (x' := witTcbA) I.irqN w1KA_rt rfl) rfl rfl + have sidle : SIdleA σ2 M1 1 := by + have D := J.sidle + have ik : ∀ c < 1, (σ2).kheap ((M1).idleThreadOf c) = (σ1).kheap ((M1).idleThreadOf c) := fun c hc => + Function.update_of_ne (idle_ne_rtA layA lay1 hc) _ _ + refine + { f3 := fun c hc => by rw [ik c hc]; exact D.f3 c hc + f4 := fun y k h => D.f4 y k ((sck _ _ _).1 h) + f5 := fun p y k h e => D.f5 p y k ((sck _ _ _).1 h) e + f6 := D.f6.of_eq capAt_eq + f7 := fun y k h => D.f7 y k ((sck _ _ _).1 h) + idleSlot := fun c hc => (idleVtSameA (ik c hc) (D.idleSlot c hc) (D.idleBound c hc)).1 + idleBound := fun c hc => (idleVtSameA (ik c hc) (D.idleSlot c hc) (D.idleBound c hc)).2 } + have runVcpu : RunVcpuInv bst (w1ZA KB IB 1).T S2 := fun t ht h => by + have e : bL.mem S2 (runVcpuA t) = bL.mem S1 (runVcpuA t) := tk t ht _ (by simp) (by simp) + rw [stA2 (tT t ht)] + rw [e] at h + exact J.runVcpu t ht h + have blkQ : BlkQueuedA (w1ZA KB IB 1).T ∅ ∅ (bL.mem S2) σ2 := by + refine ⟨fun t ht x hx e he => ?_, fun t ht x hx p he => ?_⟩ + · obtain ⟨x0, h0, d⟩ := kh_back hx + have he0 : Fl4SpecA64.epBlocked x0.tcbState = some e := by + rcases d with rfl | ⟨rfl, rfl⟩ + · exact he + · cases he + exact absurd (J.blkQ.ep t ht x0 h0 e he0).1 (Set.notMem_empty _) + · obtain ⟨x0, h0, d⟩ := kh_back hx + have he0 : x0.tcbState = .blockedOnNotification p := by + rcases d with rfl | ⟨rfl, rfl⟩ + · exact he + · cases he + exact absurd (J.blkQ.ntfn t ht x0 h0 p he0).1 (Set.notMem_empty _) + have act : ∀ p, ScActiveAtA σ1 p → ScActiveAtA σ2 p := fun p ⟨y, k, e, f⟩ => ⟨y, k, (sck _ _ _).2 e, f⟩ + have schedValid : SchedValidA σ2 := by + refine ⟨fun d p t h => (by rw [rq0] at h; cases h), fun _ _ => ?_, fun t x c hx hb hs => ?_, + fun t h => (by rw [rl0] at h; cases h)⟩ + · exact ⟨⟨witTcbA, specPtr (bwv SCb), kh_rt, rfl, _, _, (sck _ _ _).2 w1KA_sc, rfl⟩, + by rw [rl0]; exact List.not_mem_nil⟩ + · obtain ⟨x0, h0, d⟩ := kh_back hx + rcases d with rfl | ⟨rfl, rfl⟩ + · exact act _ (J.schedValid.blk t x c h0 hb hs) + · simp [BlockedTsA, witTcbA, rootTcbA] at hb + have h := stage3_curA (Z := w1ZA KB IB 1) + (f := Function.update (w1ZA KB IB 1).curOf 0 (specPtr (bwv RTb))) + (g := Function.update (w1ZA KB IB 1).curScOf 0 (specPtr (bwv SCb))) (s' := S2) (σ' := σ2) J.toJStage3A + (wstep J) specSched ssz qkeep rfl hcurOf (bxi2 AR ar n J) (sys2 AR ar n J).1 (sys2 AR ar n J).2 userOK (j72 J) + (sr2 J) ep ntfn reply bufCover bufAl idle mcp yr sinv sidle runVcpu blkQ schedValid + exact h + +theorem wit_syscallA_of (h1 : Fl4SpecA64.fl4A64Platform.numNodes = 1) : JSyscallA bst AR witZA KTH OS1 w1U (H1).scs + (Fl4ProofsA64.P2.BootEpB.w1Pp KB IB 1 r now mt sl uw) w1Tb w1Lv (IB : U64).val ((IB : U64).val + 1 * 8192) + ∅ ∅ ∅ w1BfA (fun _ => []) (w1SchedScCoreA IB) 1 (J5A bst) (J7A bst (J7R2A bst (w1Ar ar) n)) S2 a σ2 M1 := by + have sck : ∀ p y k, (σ2).kheap p = some (.schedContext y k) ↔ (σ1).kheap p = some (.schedContext y k) := + fun p y k => kh_obj (fun _ e => by cases e) + have rsc : ∀ y k, (σ2).kheap (σ2).curSc = some (.schedContext y k) → y = rootScA now sl := fun y k h => by + have h' : (σ1).kheap (specPtr (bwv SCb)) = some (.schedContext y k) := (sck _ _ _).1 h + have e : (σ1).kheap (specPtr (bwv SCb)) = some (.schedContext (rootScA now sl) 0) := w1KA_sc + rw [e] at h' + cases h' + rfl + have curInv : CurInvA σ2 := + { tcb := ⟨witTcbA, kh_rt, rfl, rfl⟩ + sc := ⟨_, _, (sck _ _ _).2 w1KA_sc⟩ + active := fun _ y k h => by rw [rsc y k h]; rfl + notRel := List.not_mem_nil } + have offOK : OffOKA σ2 := fun h => by + change (0 : Fl4SpecA64.Word) < 0 at h + exact absurd h (BitVec.lt_irrefl _) + have offReady : OffReadyA σ2 := fun y k h => by + rw [rsc y k h] + change now.bv.toNat + (0 : Fl4SpecA64.Word).toNat ≤ now.bv.toNat + simp + have actIdle : ActIdleA σ2 := by + refine ⟨fun t h => (by cases h), ?_⟩ + obtain ⟨x, hx, e1, e2, -⟩ := J.actIdle.2 + obtain ⟨y, hy, d⟩ := kh_fwd hx + refine ⟨y, hy, ?_, ?_, List.not_mem_nil⟩ + · rcases d with rfl | ⟨rfl, rfl⟩ <;> exact e1 + · rcases d with rfl | ⟨rfl, rfl⟩ <;> exact e2 + have curScNotIdle : (σ2).curSc ≠ Fl4SpecA64.idleScPtr → Sched.NotIdleScA M1 (σ2).curSc := fun _ => by + refine ⟨w1_sc_ne_idleA layA layK lay1, fun c hc e => ?_⟩ + have hc' : c < 1 := lt_of_lt_of_le hc (le_of_eq h1) + have := congrArg UScalar.val (specPtr_inj (show specPtr (bwv SCb) = specPtr (idleSA IB c) from e)) + rw [vSC, idleSA_val layA hc', ibv] at this + omega + have sact : SActA σ2 := ⟨fun u h => (by cases h), witTcbA, kh_rt, Or.inl rfl⟩ + have J3 := wit_stage3A J + refine + { toJStage3EntryA := + { toJStage3A := J3 + curInv := curInv + offOK := offOK + offReady := offReady + actIdle := actIdle + curScNotIdle := curScNotIdle + validTime := J.validTime + sact := sact } + act := rfl + running := ?_ + curSc := fun _ => ?_ + cur := ?_ } + · rw [curW2, stA2 rtMem1, rt_stA1] + exact Or.inl (by simp [sched.TS_RUNNING]) + · rw [curW2, cscW2, scA2 rtMem1, rt_scA1] + · rw [curW2, inqA2 rtMem1, scA2 rtMem1, rt_scA1] + have e : bL.mem S1 (inqA (bwv RTb)) = 0#u64 := + (rt_word (o := 3592#u64) (k := 3592) (by simp) (by omega)).trans (by simp [rtW]) + rw [e] + refine ⟨by apply UScalar.eq_of_val_eq; simp, fun h => ?_⟩ + have := congrArg UScalar.val h + rw [vSC] at this + simp at this + +end rec + +/-- **`JSyscallA` is inhabited** (module docstring). -/ +theorem wit_syscallA {A : Type} (AR : A → HwRootA → Prop) (ar : machine.Arch A) {a : A} (r now mt sl : U64) + (uw : U64 → U64) (n : U64) (hAR : AR a bootHwA) (h1 : Fl4SpecA64.fl4A64Platform.numNodes = 1) + (hsl : Fl4SpecA64.MIN_BUDGET ≤ sl.bv) (hr : specPtr r = Fl4SpecA64.armGlobalPtPtr) : + JSyscallA bst AR witZA (KtH (w1H KB IB 1) {bwv KTb}) (w1OS KB IB 1) w1U (w1H KB IB 1).scs + (Fl4ProofsA64.P2.BootEpB.w1Pp KB IB 1 r now mt sl uw) w1Tb w1Lv (IB : U64).val ((IB : U64).val + 1 * 8192) + ∅ ∅ ∅ w1BfA (fun _ => []) (w1SchedScCoreA IB) 1 (J5A bst) (J7A bst (J7R2A bst (w1Ar ar) n)) + (witS r now mt sl uw) a (witSA now mt sl uw) (w1MA IB 1) := + wit_syscallA_of (w1_stage3_one AR ar r now mt sl uw n hAR h1 hsl hr) h1 + +end + +end Fl4ProofsA64.P3.Wit diff --git a/proofs/Fl4ProofsA64/P3/A64WitTopA.lean b/proofs/Fl4ProofsA64/P3/A64WitTopA.lean new file mode 100644 index 000000000..6099a2fc4 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/A64WitTopA.lean @@ -0,0 +1,169 @@ +/- +P3 (aarch64, A64-P3 non-vacuity witness, part 6): **the end-to-end witness of a CNode Copy `Send`**. + +* the arch is `AR1 = w1Ar witArA` (`P3/A64Wit2ArchA`); `A64FrameMasks bst` is `wit_frameMasks`. +* `wit_nonvacuity`: `JSyscallA` at `witS` over that arch, together with the FL4 run of the current thread's CNode Copy + `Send`: the four prefix lookups succeed, the selector `hSel` of Plan B's `cnodeCopyBodyA` holds, and + `decode_invocation` returns `Ok`. +* `wit_noDecodeErr` / `wit_notPreempted`: the antecedents of the body's `hErr` and `hPre` premises are false at the + witness. + +Hypotheses: [S] `numNodes = 1` (`h1`; `fl4A64Platform` is opaque), and the parameters of the +state (`sl ≥ MIN_BUDGET`, the IPC-buffer words `WitUw`), which exist (`witParams_ex`). +Not covered: `hFault` (a refinement obligation for `handle_fault` at every fault, with no failing lookup in its +hypotheses). New module; no existing statement changed. +-/ +import Fl4ProofsA64.P3.A64WitCopyA +import Fl4ProofsA64.P3.A64Wit2ArchA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Wit +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P1.W7.R.ST.BootWit Fl4ProofsA64.P3 + Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched + +noncomputable section + +/-! ## The arch: the model arch of `P3/A64Wit2ArchA` -/ + +open Fl4ProofsA64.P3.Wit2 (WA AR1 ar1_total ar1_spi ar1_plat) + +variable {r now mt sl : U64} {uw : U64 → U64} + +local notation "S2" => witS r now mt sl uw +local notation "RT" => (bwv RTb) +local notation "RC" => capability.Cap.CNode (bwv RRb) 13#u64 51#u64 0#u64 +local notation "SL2" => (bwv (RRb + 64)) +variable {a : WA} + +/-- The message info the witness reads. -/ +def witInfo : msginfo.MessageInfo := + { label := 24#u64, caps_unwrapped := 0#u64, extra_caps := 1#u64, length := 5#u64 } + +theorem wit_frameMasks : A64FrameMasks bst S2 := rfl + +theorem wit_capWord : bL.mem S2 (regWA RT layout.reg.CAP) = 2#u64 := by + have e : layout.reg.CAP = 0#u64 := by simp [layout.reg.CAP, layout.reg.X0] + have h := regW2 (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) 0#u64 (by simp) + rw [tcb_reg_eqA] at h + rw [e] + exact (ok_inj_ff h).trans (by simp [witRtW]) + +theorem wit_slot2 : cspace.lookup_slot bst S2 RT 2#u64 = ok (.Ok { slot := SL2, bits_left := 0#u64 }) := + wit_lookupSlot (cptr := 2#u64) (by simp) (slot := SL2) (by rw [bwv_val (by omega)]; simp) + +theorem wit_msgLen : msgLenA witInfo (bwv IPb) = 5#u64 := by + show msgLenA { label := 24#u64, caps_unwrapped := 0#u64, extra_caps := 1#u64, length := 5#u64 } _ = _ + have n : msg.N_MSG_REGISTERS = 4#u64 := by simp [msg.N_MSG_REGISTERS] + have ne : (bwv IPb) ≠ 0#u64 := fun e => by + have := congrArg UScalar.val e + rw [vIP] at this + simp at this + unfold msgLenA + rw [n, if_pos (by scalar_tac), if_neg ne] + +/-- The run's prefix, from any successful lookups: they are the witness's. -/ +theorem wit_prefix (U : WitUw uw) {info : msginfo.MessageInfo} {buf : U64} {r1 : cspace.Resolved} + {e : Aeneas.Std.Array obj.Slot 3#usize} + (h1 : msg.msg_info bst S2 (bL.curW S2) = ok info) + (h2 : msg.lookup_ipc_buffer bst S2 false (bL.curW S2) = ok buf) + (h3 : cspace.lookup_slot bst S2 (bL.curW S2) (bL.mem S2 (regWA (bL.curW S2) layout.reg.CAP)) = ok (.Ok r1)) + (h4 : ipc.lookup_extra_caps bst S2 (bL.curW S2) buf info = ok (.Ok e)) : + info = witInfo ∧ buf = bwv IPb ∧ r1 = { slot := SL2, bits_left := 0#u64 } ∧ e = witExtra := by + rw [curW2] at h1 h2 h3 h4 + rw [wit_msgInfo] at h1 + rw [wit_ipcBuffer] at h2 + rw [wit_capWord, wit_slot2] at h3 + have e1 := ok_inj_ff h1 + have e2 := ok_inj_ff h2 + have e3 := ok_inj_ff h3 + injection e3 with e3 + subst e1 e2 + rw [wit_extraCaps U] at h4 + have e4 := ok_inj_ff h4 + injection e4 with e4 + exact ⟨rfl, rfl, e3.symm, e4.symm⟩ + +theorem wit_cap2 : capA bst S2 SL2 = RC := by + rw [capS2_rs (i := 2) (by omega) (bwv_val (by omega))] + unfold capOf + rw [slotC_lo (by omega)] + rfl + +/-- **`hErr`'s antecedent is false at the witness**: the decode does not return an error. -/ +theorem wit_noDecodeErr (U : WitUw uw) {blk call don : Bool} {info : msginfo.MessageInfo} {buf : U64} + {r1 : cspace.Resolved} {e : Aeneas.Std.Array obj.Slot 3#usize} {x : error.Exc} {s' : BS} {a' : WA} + (h1 : msg.msg_info bst S2 (bL.curW S2) = ok info) + (h2 : msg.lookup_ipc_buffer bst S2 false (bL.curW S2) = ok buf) + (h3 : cspace.lookup_slot bst S2 (bL.curW S2) (bL.mem S2 (regWA (bL.curW S2) layout.reg.CAP)) = ok (.Ok r1)) + (h4 : ipc.lookup_extra_caps bst S2 (bL.curW S2) buf info = ok (.Ok e)) + (h : syscall.decode_invocation bst AR1 S2 a info.label (msgLenA info buf) r1.slot (capA bst S2 r1.slot) blk + call don false buf e = ok (.Err x, s', a')) : False := by + obtain ⟨rfl, rfl, rfl, rfl⟩ := wit_prefix U h1 h2 h3 h4 + obtain ⟨s3, d⟩ := wit_decodeOk (r := r) (now := now) (mt := mt) (sl := sl) U AR1 a blk call don + rw [wit_msgLen, wit_cap2] at h + have := ok_inj_ff (d.symm.trans h) + cases this + +/-- **`hPre`'s antecedent is false at the witness**: `handle_invocation` does not return `Preempted`. -/ +theorem wit_notPreempted (U : WitUw uw) {call blk don : Bool} {x : error.Exc} {s' : BS} {a' : WA} + (h : syscall.handle_invocation bst AR1 S2 a call blk don false (bL.mem S2 (regWA (bL.curW S2) layout.reg.CAP)) = + ok (.Err x, s', a')) : False := by + obtain ⟨s3, d⟩ := wit_decodeOk (r := r) (now := now) (mt := mt) (sl := sl) U AR1 a blk call don + have ct : sched.cur_thread bst S2 = ok RT := by rw [cur_thread_ok, curW2] + have n : msg.N_MSG_REGISTERS = 4#u64 := by simp [msg.N_MSG_REGISTERS] + have ne : (bwv IPb) ≠ 0#u64 := fun e => by + have := congrArg UScalar.val e + rw [vIP] at this + simp at this + have c : obj.Slot.cap bst SL2 S2 = ok RC := by rw [capA_eq, wit_cap2] + rw [curW2, wit_capWord] at h + unfold syscall.handle_invocation at h + rw [ct, bind_ok, wit_msgInfo, bind_ok, wit_slot2, bind_ok] at h + try simp only at h + rw [c, bind_ok, wit_ipcBuffer, bind_ok, wit_extraCaps U, bind_ok] at h + try simp only at h + rw [n, if_pos (by scalar_tac), if_neg ne, bind_ok, d, bind_ok] at h + try simp only at h + obtain ⟨i, -, h⟩ := bind_eq_ok h + split at h + · obtain ⟨s2, -, h⟩ := bind_eq_ok h + obtain ⟨s4, -, h⟩ := bind_eq_ok h + have := ok_inj_ff h + cases this + · have := ok_inj_ff h + cases this + +/-- Parameters of the witness state exist. -/ +theorem witParams_ex : ∃ (sl : U64) (uw : U64 → U64), Fl4SpecA64.MIN_BUDGET ≤ sl.bv ∧ WitUw uw := by + obtain ⟨uw, U⟩ := witUw_ex + exact ⟨⟨Fl4SpecA64.MIN_BUDGET⟩, uw, BitVec.le_refl _, U⟩ + +/-- **The end-to-end non-vacuity witness of a CNode Copy `Send`** (module docstring). -/ +theorem wit_nonvacuity (r now mt sl : U64) (uw : U64 → U64) (U : WitUw uw) (hsl : Fl4SpecA64.MIN_BUDGET ≤ sl.bv) + (h1 : Fl4SpecA64.fl4A64Platform.numNodes = 1) (hr : specPtr r = Fl4SpecA64.armGlobalPtPtr) : + JSyscallA bst (fun _ _ => True) witZA (KtH (w1H KB IB 1) {bwv KTb}) (w1OS KB IB 1) w1U (w1H KB IB 1).scs + (Fl4ProofsA64.P2.BootEpB.w1Pp KB IB 1 r now mt sl uw) w1Tb w1Lv (IB : U64).val ((IB : U64).val + 1 * 8192) + ∅ ∅ ∅ w1BfA (fun _ => []) (w1SchedScCoreA IB) 1 (J5A bst) (J7A bst (J7R2A bst AR1 ar1_plat.ipa)) + (witS r now mt sl uw) a (witSA now mt sl uw) (w1MA IB 1) ∧ + A64FrameMasks bst (witS r now mt sl uw) ∧ + ∃ (info : msginfo.MessageInfo) (buf : U64) (r1 : cspace.Resolved) (e : Aeneas.Std.Array obj.Slot 3#usize), + msg.msg_info bst (witS r now mt sl uw) (bL.curW (witS r now mt sl uw)) = ok info ∧ + msg.lookup_ipc_buffer bst (witS r now mt sl uw) false (bL.curW (witS r now mt sl uw)) = ok buf ∧ + cspace.lookup_slot bst (witS r now mt sl uw) (bL.curW (witS r now mt sl uw)) (bL.mem (witS r now mt sl uw) (regWA (bL.curW (witS r now mt sl uw)) layout.reg.CAP)) = ok (.Ok r1) ∧ + ipc.lookup_extra_caps bst (witS r now mt sl uw) (bL.curW (witS r now mt sl uw)) buf info = ok (.Ok e) ∧ + (24 ≤ info.label.val ∧ info.label.val ≤ 28) ∧ (∃ p rx gs g, capA bst (witS r now mt sl uw) r1.slot = .CNode p rx gs g) ∧ + ∀ blk call don : Bool, ∃ s' : BS, syscall.decode_invocation bst AR1 (witS r now mt sl uw) a info.label (msgLenA info buf) r1.slot + (capA bst (witS r now mt sl uw) r1.slot) blk call don false buf e = ok (.Ok (), s', a) := by + refine ⟨wit_syscallA _ _ r now mt sl uw _ trivial h1 hsl hr, wit_frameMasks, witInfo, bwv IPb, + { slot := SL2, bits_left := 0#u64 }, witExtra, ?_, ?_, ?_, ?_, by simp [witInfo], ⟨_, _, _, _, wit_cap2⟩, + fun blk call don => ?_⟩ + · rw [curW2]; exact wit_msgInfo + · rw [curW2]; exact wit_ipcBuffer + · rw [curW2, wit_capWord]; exact wit_slot2 + · rw [curW2]; exact wit_extraCaps U + · obtain ⟨s3, d⟩ := wit_decodeOk (r := r) (now := now) (mt := mt) (sl := sl) U AR1 a blk call don + exact ⟨s3, by rw [wit_msgLen, wit_cap2]; exact d⟩ + +end + +end Fl4ProofsA64.P3.Wit diff --git a/proofs/Fl4ProofsA64/P3/A64WitW1A.lean b/proofs/Fl4ProofsA64/P3/A64WitW1A.lean new file mode 100644 index 000000000..53ae3eb05 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/A64WitW1A.lean @@ -0,0 +1,83 @@ +/- +P3 (aarch64, A64-P3 non-vacuity witness, part 0): **`w1` on a one-node platform.** + +A64-Boot's `w1` at `nc = 1` (`kb = PPTR_BASE + 2^30`, `ib = PPTR_BASE + 2^31`): the layout facts (`lay1`, `layA`, +`layK`, `canon1`) and **`w1_stage3_one`**, the `nc = 1` instance of `w1_stage3` (`P3/Stage3BootA`) with J5 / J7 +concrete, at the heap `KtH (w1H …) {kt}` and the arch `w1Ar ar` (as `w1_stage3_conc` at 128 nodes). + +Named premise: [S] `numNodes = 1` (`fl4A64Platform` is opaque). Other hypotheses: `AR a bootHwA`, `sl ≥ MIN_BUDGET`. +New module; no existing statement changed. +-/ +import Fl4ProofsA64.P3.Stage3J5A +import Fl4ProofsA64.P3.Stage3BootA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Wit +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P1.W7.R.ST.BootWit Fl4ProofsA64.P3 + Fl4ProofsA64.P3.Endp + +noncomputable section + +/-- The kernel-state block. -/ +scoped notation "KBn" => (18446603337294938112 : Nat) +/-- The idle area. -/ +scoped notation "IBn" => (18446603338368679936 : Nat) +scoped notation "KB" => (18446603337294938112#u64 : U64) +scoped notation "IB" => (18446603338368679936#u64 : U64) + +theorem kbv : (KB : U64).val = KBn := by simp +theorem ibv : (IB : U64).val = IBn := by simp + +theorem lay0 : BootLay KB IB 1 := by + refine ⟨?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_⟩ <;> (try simp only [kbv, ibv]) <;> omega + +theorem lay1 : BootLay1 KB IB 1 := ⟨lay0, by rw [kbv]; norm_num, by rw [ibv]; norm_num⟩ + +theorem hsc1 : 18446603336221208576 < (IB : U64).val ∨ (IB : U64).val + 1 * 8192 ≤ 18446603336221208576 := + Or.inl (by rw [ibv]; omega) + +theorem hsk1 : 18446603336221208576 < (KB : U64).val ∨ (KB : U64).val + 788736 ≤ 18446603336221208576 := + Or.inl (by rw [kbv]; omega) + +theorem layA : BootLayA IB 1 := bootLayA_of lay0 hsc1 + +theorem layK : BootKsA KB IB 1 := bootKsA_of lay0 hsk1 + +theorem canon1 : BootCanon KB IB 1 ∧ (KB : U64).val + 788736 ≤ layout.PPTR_BASE.val + 2 ^ 44 ∧ + (IB : U64).val + 1 * 8192 ≤ layout.PPTR_BASE.val + 2 ^ 44 := by + refine ⟨⟨?_, ?_, ?_, ?_⟩, ?_, ?_⟩ + all_goals (try simp only [kbv, ibv, layout.PPTR_BASE.val_eq, layout.PPTR_TOP.val_eq]) + all_goals (try norm_num) + all_goals (try omega) + +/-- **The `w1` entry record on a one-node platform, with J5 and J7 concrete** (module docstring). -/ +theorem w1_stage3_one {A : Type} (AR : A → HwRootA → Prop) (ar : machine.Arch A) {a : A} + (r now mt sl : U64) (uw : U64 → U64) (n : U64) (hAR : AR a bootHwA) + (h1 : Fl4SpecA64.fl4A64Platform.numNodes = 1) (hsl : Fl4SpecA64.MIN_BUDGET ≤ sl.bv) + (hr : specPtr r = Fl4SpecA64.armGlobalPtPtr) : + JStage3EntryA bst AR (w1ZA KB IB 1) (KtH (w1H KB IB 1) {bwv KTb}) (w1OS KB IB 1) w1U (w1H KB IB 1).scs + (Fl4ProofsA64.P2.BootEpB.w1Pp KB IB 1 r now mt sl uw) w1Tb w1Lv + (IB : U64).val ((IB : U64).val + 1 * 8192) ∅ ∅ ∅ w1BfA (fun _ => []) + (w1SchedScCoreA IB) 1 (J5A bst) (J7A bst (J7R2A bst (w1Ar ar) n)) (w1S KB IB 1 r now mt sl uw) a + (w1SUA KB IB 1 now mt sl uw) (w1MA IB 1) := by + have J := w1_stage3 (kb := KB) (ib := IB) (nc := 1) lay1 canon1.1 canon1.2.1 canon1.2.2 hsc1 hsk1 AR (a := a) + r now mt sl uw hAR (le_of_eq h1.symm) (le_of_eq h1) hsl hr (fun _ _ _ _ _ _ => True) + (fun _ _ _ _ _ _ => True) trivial trivial + refine J.addKt (KtOut.noUb (w1K_out lay1)) (fun _ h => absurd h (Set.notMem_empty _)) (fun q hq b hb => ?_) + (fun q hq => by + have hq' := ktv hq + have eB := Fl4ProofsA64.P0.Consts.layout.PPTR_BASE.val_eq + omega) + (fun y hy p size rights mapped ept asid va h => ?_) + (w1_J5A (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) lay1 layA layK + (w1SUA KB IB 1 now mt sl uw) (w1MA IB 1)) + (w1_J7A_full (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) ar lay1 n (w1SUA KB IB 1 now mt sl uw) + (w1MA IB 1)) + · have hq' := ktv hq + obtain ⟨-, h1, h2⟩ := hb + omega + · exact (w1_noDevFrA lay1 hy h).elim + +end + +end Fl4ProofsA64.P3.Wit diff --git a/proofs/Fl4ProofsA64/P3/ArchTopA.lean b/proofs/Fl4ProofsA64/P3/ArchTopA.lean new file mode 100644 index 000000000..3e9439edd --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ArchTopA.lean @@ -0,0 +1,200 @@ +/- +P3 (aarch64, A64-P3 S3 gen 40, Step 5): +Unified AArch64 VSpace / ASID / Frame top at `syscall.decode_invocation` against +`Fl4SpecA64.fl4DecodeInvocation` + `Fl4SpecA64.fl4PerformInvocation` (`decInv_arch_jA`). + +Covers the 7 VSpace / ASID / Frame cap kinds (`IsArchVsCapA`): +* `Cap::Pml4` (Stage-1 VSpace root, labels 42–45 + illegal): `VsFlushA.decInv_vspace_jA` +* `Cap::PageTable` (Stage-1, Stage-2, `ArmHighVSpace`, labels 47–48 + illegal): `ExtMapPtA.decInv_pt_all_jA` +* `Cap::Frame` (Stage-1, Stage-2, `ArmHighVSpace`, labels 49–57 + illegal): `FrSubframeA.decInv_frame_all_jA` +* `Cap::AsidPool` (Stage-1, Stage-2, `ArmHighVSpace`, label 59 + illegal): `VsAssignA.decInv_asidPool_jA` +* `Cap::AsidControl` (label 58 `ARMASIDControlMakePool` + illegal): `AsidCtrlTopA.decInv_asidControl_all_jA` +* `Cap::Ept` (level-0 Stage-2 VSpace root, labels 42–45 + illegal): `VsS2FlushA.decInv_s2Vspace_jA` +* `Cap::HighVSpace` (all labels `IllegalOperation`): `decInv_highVspace_jA` + +`Cap::Vcpu` stays out of S3 (excluded from `IsArchVsCapA`). +Named premise (Architect 21:51Z D1): `hD` **[J] `DeadDrop4KA`** for a childless 4 KiB untyped (label 58's dead-object +drop, producer the Untyped line's `UtDropA`), passed to `decInv_asidControl_all_jA`. +-/ +import Fl4ProofsA64.P3.VsFlushA +import Fl4ProofsA64.P3.ExtMapPtA +import Fl4ProofsA64.P3.FrSubframeA +import Fl4ProofsA64.P3.VsAssignA +import Fl4ProofsA64.P3.AsidCtrlTopA +import Fl4ProofsA64.P3.VsS2FlushA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## Cap-kind predicate (`IsArchVsCapA`) and top-level postcondition (`ArchDecTopPostA`) -/ + +/-- The VSpace / ASID / Frame arch cap family covered by A64-P3 S3 (`Cap::Vcpu` excluded). -/ +def IsArchVsCapA : capability.Cap → Prop + | .Pml4 _ _ _ => True + | .PageTable _ _ _ _ => True + | .Frame _ _ _ _ _ _ _ _ => True + | .AsidPool _ _ => True + | .AsidControl => True + | .Ept _ _ _ _ _ => True + | .HighVSpace _ _ _ => True + | _ => False + +section post +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-- Top-level `decode_invocation` postcondition against `Fl4SpecA64.fl4DecodeInvocation` and +`Fl4SpecA64.fl4PerformInvocation`, generalizing `ArchTopPostA` from `Fl4ArchInvocation` to +`Fl4Invocation`. -/ +def ArchDecTopPostA (AR : A → HwRootA → Prop) (Z : XSetsA) (OS : Set U64) (m : Fl4SpecA64.McState) + (block call cd : Bool) (σ : Fl4SpecA64.State Unit) + (D : Set (Except Fl4SpecA64.SyscallError Fl4SpecA64.Fl4Invocation × Fl4SpecA64.State Unit) × Prop) + (s : S) (a : A) : core.result.Result Unit error.Exc × S × A → Prop + | (.Err e, s', a') => s' = s ∧ a' = a ∧ ∃ e', D = ({(.error e', σ)}, False) ∧ VsErrRelA e e' + | (.Ok (), s', a') => ∃ inv, ((.ok inv, σ) ∈ D.1 ∧ ¬ D.2) ∧ ∃ σ1, + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ∃ (σ2 : Fl4SpecA64.State Unit) (rv : List Fl4SpecA64.Data), + (.ok rv, σ2) ∈ ((Fl4SpecA64.fl4PerformInvocation (Z := Unit) block call cd inv).run σ1).1 ∧ + ((∃ Z' : XSetsA, + JAtZAX (L := L) AR Z' s' a' σ2 m ∧ + TreeA st OS Z'.G.chains Z'.G.par s' ∧ + (CarryA Z (L.mem s) σ → CarryA Z' (L.mem s') σ2)) ∨ + (∃ σ3 : Fl4SpecA64.State Unit, + ((), σ3) ∈ ((do + Fl4Spec.NonDet.when call (Fl4SpecA64.replyFromKernel (Z := Unit) σ.curThread (0, rv)) + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .running) σ2).1 ∧ + JAtZAX (L := L) AR Z s' a' σ3 m ∧ + TreeA st OS Z.G.chains Z.G.par s' ∧ + (CarryA Z (L.mem s) σ → CarryA Z (L.mem s') σ3))) + +/-- Lift `ArchTopPostA` along `Fl4Invocation.invokeArchObject` to `ArchDecTopPostA`. -/ +theorem ArchDecTopPostA.of_archTopA {AR : A → HwRootA → Prop} {Z : XSetsA} {OS : Set U64} + {m : Fl4SpecA64.McState} {block call cd : Bool} {σ : Fl4SpecA64.State Unit} + {d : Fl4SpecA64.SEMonad Unit Fl4SpecA64.Fl4ArchInvocation} {s : S} {a : A} : + ∀ {res}, ArchTopPostA (L := L) AR Z OS m call σ (d.run σ) s a res → + ArchDecTopPostA (L := L) AR Z OS m block call cd σ + ((Fl4Spec.NonDetE.liftME Fl4SpecA64.Fl4Invocation.invokeArchObject d).run σ) s a res + | (.Err _, _, _), ⟨hs, ha, e', hD, hEr⟩ => + ⟨hs, ha, e', run_liftME_errA hD, hEr⟩ + | (.Ok (), _, _), ⟨inv, ⟨hmem, hnf⟩, σ1, hst, σ2, rv, hperf, hrest⟩ => by + refine ⟨.invokeArchObject inv, ⟨?_, ?_⟩, σ1, hst, σ2, rv, hperf, hrest⟩ + · unfold Fl4Spec.NonDetE.liftME + exact ⟨(Except.ok inv, σ), hmem, rfl⟩ + · unfold Fl4Spec.NonDetE.liftME + rintro (hf | ⟨⟨e | v, σ'⟩, -, hf⟩) + · exact hnf hf + · exact hf + · exact hf + +end post + +/-! ## Unified VSpace / ASID / Frame top (`decInv_arch_jA`) -/ + +section arch_top +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- **Unified AArch64 VSpace / ASID / Frame top at `syscall.decode_invocation` against +`Fl4SpecA64.fl4DecodeInvocation`**: covers all 7 VSpace / ASID / Frame cap kinds (`Pml4`, +`PageTable`, `Frame`, `AsidPool`, `AsidControl`, `Ept`, `HighVSpace`) across all labels (42–45, +47–59, and illegal labels). `Cap::Vcpu` is excluded (`IsArchVsCapA`). -/ +theorem decInv_arch_jA {ar : machine.Arch A} (K : ArchKeepsA ar AR) + (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) + (hvcR : ∀ v ∈ Z.V, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → + wa (L.curW s) K ≠ v ∧ wa (L.curW s) K ≠ wa v 24#u64) + (hTO : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s q) (hEO : ExtTabOffA (L := L) Z m s) + (hK : Aligned (L.ksb s)) (hT : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s)) + (hYAs : ∀ y ∈ Z.Y, ∀ j : U64, j.val < 512 → ¬ AsW (L.ksb s) (peAF y j)) + (hIpa : S2IpaE ar a) + (hPC : PtCapInA Z (L.mem s)) (hFI : FinInvA Z.th (L.mem s) σ Z.G) + (hTr : TreeA st OS Z.G.chains Z.G.par s) + (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) + (hcO : ∀ y ∈ OS, obj.sext48 y = ok y) + (hD : ∀ u frame fi, capA st s u = .Untyped frame 12#u64 fi false → + cdt.ensure_no_children st s u = ok (core.result.Result.Ok ()) → DeadDrop4KA Z H frame fi) + (cte : Fl4SpecA64.CslotPtr) (hab : AbsSlotA Z.th cte) (hos : cslotAddrA cte ∈ OS) + (cap : capability.Cap) (harch : IsArchVsCapA cap) + (hcap : capAtA (L.mem s) (cslotAddrA cte) = cap) + (lbl length buf : U64) (block call cd fpb : Bool) (ex : Aeneas.Std.Array obj.Slot 3#usize) + (args : List U64) (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) + (xSlot : Fl4SpecA64.CapRef) (exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) + (hex : ExtraOKA Z.th (L.mem s) ex exs) + (firstPhase : Bool) (bufOpt : Option Fl4SpecA64.ObjRef) + (hPtMap : ∀ ptr mp asid va, cap = .PageTable ptr mp asid va → + lbl = fl4_abi.label.ARMPageTableMap → + UnmPtA Z.S1 Z.Y OS H.tcbs (L.mem s) σ ptr ∧ ptr ∉ S2PgE Z.X Z.T2 ∧ ptr ∉ Z.Y) + (hFrMap : ∀ fp sz rt dev mp e asid va, cap = .Frame fp sz rt dev mp e asid va → + lbl = fl4_abi.label.ARMPageMap → FrPlA sz fp mp asid) + (hFrSub : ∀ fp sz rt dev mp e asid va, cap = .Frame fp sz rt dev mp e asid va → + lbl = fl4_abi.label.ARMPageDeriveSubframe → sz = .Large → dev = false → + (fp.val % 2 ^ 21 = 0 ∧ CapRt.Canonical fp) ∧ + ∀ w ∈ Z.G.chains.flatten, ∀ (q vm' : U64), fp.val ≤ q.val → q.val < fp.val + 2 ^ 21 → + OvlA (absCapA (.Frame q .Small vm' false false false 0#u64 0#u64)) (absCapA (capAtA (L.mem s) w)) → + isFrameA (capAtA (L.mem s) w) = true ∧ ∃ ch ∈ Z.G.chains, cslotAddrA cte ∈ ch ∧ w ∈ ch) + (hAsidAssign : ∀ p base, cap = .AsidPool p base → + lbl = fl4_abi.label.ARMASIDPoolAssign → + ∀ u ∈ OS, ∀ k root oldAsid oldVa, + capA st s u = assignUnmappedA k root oldAsid oldVa → + UnmRootAssignA k (L.mem s) σ root) : + syscall.decode_invocation st ar s a lbl length (cslotAddrA cte) cap block call cd fpb buf ex + ⦃ res => ArchDecTopPostA (L := L) AR Z OS m block call cd σ + ((Fl4SpecA64.fl4DecodeInvocation (Z := Unit) call firstPhase lbl.bv (args.map UScalar.bv) + xSlot cte (absCapA cap) exs bufOpt).run σ) s a res ⦄ := by + cases cap with + | Pml4 ptr mp asid => + exact WP.spec_mono + (decInv_vspace_jA K E RS hTr cte hab ptr asid mp hcap lbl length buf block call cd fpb call ex + args hlen hargs xSlot exs) + fun _ h => ArchDecTopPostA.of_archTopA (ArchTopPostA.of_vsTopA h) + | PageTable ptr mp asid va => + exact WP.spec_mono + (decInv_pt_all_jA K E RS hTO hEO hK hT hIpa hPC hFI hTr hnul cte hab hos ptr asid va mp hcap + lbl length buf block call cd fpb call ex args hlen hargs (exAtA ex 0) (exAtA_idx0 ex) + xSlot exs (hex.rel1 E.j.1.place) (hPtMap ptr mp asid va rfl)) + fun _ h => ArchDecTopPostA.of_archTopA (ArchTopPostA.of_vsTopA h) + | Frame fp sz rt dev mp e asid va => + exact WP.spec_mono + (decInv_frame_all_jA K E RS hvcR hTO hEO hK hT hIpa hTr hnul hcO cte hab hos fp rt asid va sz + dev mp e hcap lbl length buf block call cd fpb ex args hlen hargs (exAtA ex 0) + (exAtA_idx0 ex) xSlot exs (hex.rel1 E.j.1.place) + (hFrMap fp sz rt dev mp e asid va rfl) (hFrSub fp sz rt dev mp e asid va rfl)) + fun _ h => ArchDecTopPostA.of_archTopA h + | AsidPool p base => + exact WP.spec_mono + (decInv_asidPool_jA ar E RS hTr hnul cte hab hcap lbl length buf block call cd fpb call ex + args xSlot exs hex (hAsidAssign p base rfl)) + fun _ h => ArchDecTopPostA.of_archTopA (ArchTopPostA.of_vsTopA h) + | AsidControl => + exact WP.spec_mono + (decInv_asidControl_all_jA ar E RS hT hYAs hTr hnul hcO hD lbl length buf cte block call cd fpb + call ex args xSlot exs hlen hargs hex) + fun _ h => ArchDecTopPostA.of_archTopA h + | Ept ptr lvl mp asid va => + obtain ⟨_, _, hwf, _⟩ := StateRelA.getAbsA E.j.1.st hab + change CapWfA (capAtA (L.mem s) (cslotAddrA cte)) at hwf + rw [hcap] at hwf + have hl0 : lvl = 0#u64 := UScalar.eq_of_val_eq hwf.1 + subst hl0 + exact WP.spec_mono + (decInv_s2Vspace_jA K E RS hTr hIpa cte hab ptr asid va mp hcap lbl length buf block call cd + fpb call ex args hlen hargs xSlot exs) + fun _ h => ArchDecTopPostA.of_archTopA (ArchTopPostA.of_vsTopA h) + | HighVSpace ptr mp asid => + exact WP.spec_mono + (decInv_highVspace_jA ar cte ptr asid mp lbl length buf block call cd fpb call ex args xSlot exs) + fun _ h => ArchDecTopPostA.of_archTopA (ArchTopPostA.of_vsTopA h) + | _ => cases harch + +end arch_top + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/AsidCtrlTopA.lean b/proofs/Fl4ProofsA64/P3/AsidCtrlTopA.lean new file mode 100644 index 000000000..d933e6542 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/AsidCtrlTopA.lean @@ -0,0 +1,2850 @@ +/- +P3 (aarch64, A64-P3 S3 gen 40, Architect 10:07Z Step 4): **Label 58 `ARMASIDControlMakePool`** +(`vspace.decode`'s `Cap::AsidControl` arm) on unified `ArchTopPostA`. + +* **`decode_loop0_specA`**: `vspace.decode_loop0 st s 0#u64` scans the 128-entry ASID table + (`atAF (L.ksb s) i`), returning either `128#u64` (full: `(dom (asidTable σ))ᶜ = ∅`) or the first + free index `i < 128` (`BitVec.ofNat 7 i.val ∈ (dom (asidTable σ))ᶜ`). +* **`freeMemory_runA` / `retypeRegion_asidPoolA`**: deterministic spec outcomes of `deleteObjects` + (via `doMachineOp (freeMemory ...)` + `detype`) and `retypeRegion ... .asidPoolObj` (updating + `kheap (specPtr frame)` to an empty `ASIDPool`). +* **`makePool_perform_jA`**: the 5-step perform tail (`set_thread_state TS_RESTART`, + `set_cap x (Untyped frame 12 4096 false)`, `clear_free frame 4096`, `cdt.insert (AsidPool frame base) x v`, + `set_asid_table i frame`) refines `setThreadState .restart` + `performAsidControlInvocation (.makePool ...)`, + preserving `JAtZAX` at `Z' = { Z with Pp := insert frame Z.Pp, G := G' }`, `TreeA` on `OS`, and `CarryA`, + using `ObjAllocA.disjoint_of_drop` (every retained `JointObjectA Z H` misses `[frame, frame + 4096)`: `ObjAllocA` at the + parent and the [J] dead-object drop `DeadDrop4KA`, Architect 21:51Z D1). +* **`decInv_asidControl_all_jA`**: full `Cap::AsidControl` top at `syscall.decode_invocation` against + `fl4ArchDecodeInvocation` on `ArchTopPostA` (both `ARMASIDControlMakePool` and illegal labels). + +Named premise (Architect 21:51Z D1, statement change): `hD` **[J] `DeadDrop4KA`** in `makePool_perform_jA` and +`decInv_asidControl_all_jA` (the dead objects of the childless parent untyped dropped, as spec `deleteObjects`; +producer the Untyped line's `UtDropA`), replacing `ObjNamedA.disjoint_of_childless`. +-/ +import Fl4ProofsA64.P3.VsMakePoolA +import Fl4ProofsA64.P3.FrReplyA +import Fl4ProofsA64.P3.NoChildA +import Fl4ProofsA64.P3.EmptyA + +open Aeneas Aeneas.Std Result +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## `vspace.decode_loop0` against `asidTable` -/ + +section loop0 +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +theorem decode_loop0_specA (s : S) : + vspace.decode_loop0 st s 0#u64 ⦃ i => + i.val ≤ 128 ∧ + (∀ j : U64, j.val < i.val → L.mem s (atAF (L.ksb s) j) ≠ 0#u64) ∧ + (i.val < 128 → L.mem s (atAF (L.ksb s) i) = 0#u64) ⦄ := by + unfold vspace.decode_loop0 + apply loop.spec_decr_nat (measure := fun i : U64 => 128 - i.val) + (inv := fun i : U64 => i.val ≤ 128 ∧ ∀ j : U64, j.val < i.val → L.mem s (atAF (L.ksb s) j) ≠ 0#u64) + · rintro i ⟨hi, hall⟩ + unfold vspace.decode_loop0.body vspace.N_ASID_POOLS + have h128 : (1#u64 <<< fl4_abi.consts.bits.NUM_ASID_POOLS : Result U64) = ok 128#u64 := by + apply eq_of_spec_fd; unfold fl4_abi.consts.bits.NUM_ASID_POOLS; step* + rw [h128, bind_ok] + by_cases hlt : i < 128#u64 + · rw [if_pos hlt] + have hiLt : i.val < 128 := hlt + have hat : vspace.asid_table st s i = ok (L.mem s (atAF (L.ksb s) i)) := by + rw [asid_table_eq] + have hiand : i &&& 127#u64 = i := UScalar.eq_of_val_eq (by + rw [UScalar.val_and, show (127#u64 : U64).val = 2 ^ 7 - 1 from rfl, Nat.and_two_pow_sub_one_eq_mod, + Nat.mod_eq_of_lt (by omega)]) + rw [hiand] + rw [hat, bind_ok] + by_cases hne : (L.mem s (atAF (L.ksb s) i) != 0#u64) = true + · rw [if_pos hne] + have hne' : L.mem s (atAF (L.ksb s) i) ≠ 0#u64 := bne_iff_ne.1 hne + have hinc : (i + 1#u64) ⦃ i1 => i1.val = i.val + 1 ⦄ := by step as ⟨i1, hi1⟩; omega + obtain ⟨i1, hi1Ok, hi1Val⟩ := spec_ex_fd hinc + rw [hi1Ok, bind_ok, WP.spec_ok] + refine ⟨⟨by omega, fun j hj => ?_⟩, by omega⟩ + by_cases hji : j.val < i.val + · exact hall j hji + · have : j = i := UScalar.eq_of_val_eq (by omega) + subst this; exact hne' + · rw [if_neg hne, WP.spec_ok] + have hz : L.mem s (atAF (L.ksb s) i) = 0#u64 := by + by_contra h; exact hne (bne_iff_ne.2 h) + exact ⟨hi, hall, fun _ => hz⟩ + · rw [if_neg hlt, WP.spec_ok] + have hge : 128 ≤ i.val := by + have : ¬ (i.val < 128) := hlt; omega + exact ⟨hi, hall, fun h => by omega⟩ + · exact ⟨by decide, fun j hj => by have : (0#u64 : U64).val = 0 := rfl; omega⟩ + +theorem asidTable_full_of_loop0A {Pp Tb : Set U64} {Lv : U64 → Nat} {X Y : Set U64} + {s : S} {σ : Fl4SpecA64.State Unit} + (hV : VsRelA Pp Tb Lv X Y (L.mem s) (L.ksb s) σ) + (hall : ∀ j : U64, j.val < 128 → L.mem s (atAF (L.ksb s) j) ≠ 0#u64) : + (Fl4SpecA64.dom (Fl4SpecA64.asidTable σ))ᶜ = ∅ := by + ext x + simp only [Set.mem_compl_iff, Set.mem_empty_iff_false, iff_false, not_not] + set j : U64 := ⟨BitVec.ofNat 64 x.toNat⟩ + have hjLt : j.val < 128 := by + change (BitVec.ofNat 64 x.toNat).toNat < 128 + have hx : x.toNat < 2 ^ 7 := x.isLt + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)] + omega + have hjEq : BitVec.ofNat 7 j.val = x := by + apply BitVec.eq_of_toNat_eq + have hx : x.toNat < 2 ^ 7 := x.isLt + have hbv : j.val = x.toNat := by + change (BitVec.ofNat 64 x.toNat).toNat = x.toNat + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)] + rw [BitVec.toNat_ofNat, hbv, Nat.mod_eq_of_lt hx] + have ht := hV.table j hjLt + rw [hjEq] at ht + have hne := hall j hjLt + unfold Fl4SpecA64.dom Fl4SpecA64.asidTable + change σ.archState.armAsidTable x ≠ none + rw [ht]; unfold optPtrA; rw [if_neg hne]; intro h; cases h + +theorem asidTable_free_of_loop0A {Pp Tb : Set U64} {Lv : U64 → Nat} {X Y : Set U64} + {s : S} {σ : Fl4SpecA64.State Unit} + (hV : VsRelA Pp Tb Lv X Y (L.mem s) (L.ksb s) σ) + {i : U64} (hi : i.val < 128) (hz : L.mem s (atAF (L.ksb s) i) = 0#u64) : + BitVec.ofNat 7 i.val ∈ (Fl4SpecA64.dom (Fl4SpecA64.asidTable σ))ᶜ := by + simp only [Set.mem_compl_iff, Fl4SpecA64.dom, Fl4SpecA64.asidTable, Set.mem_setOf_eq, not_not] + rw [hV.table i hi, hz]; rfl + +end loop0 + +/-! ## Spec decode & perform helpers for `AsidControl` -/ + +section spec_helpers +open Fl4Spec Fl4SpecA64 + +private theorem dec_asidCtrlA (calling : Bool) (lbl : U64) (args : List Data) (xSlot : CapRef) (cte : CslotPtr) + (exs : List (Cap × CslotPtr)) (σ : State Unit) : + (fl4ArchDecodeInvocation (Z := Unit) calling (fl4LabelOf lbl.bv) args xSlot cte .asidControlCap exs).run σ = + (NonDetE.liftME Fl4ArchInvocation.spec + (decodeAsidControlInvocation (Z := Unit) (specLabelWord (fl4LabelOf lbl.bv)) args cte .asidControlCap exs)).run σ := by + unfold fl4ArchDecodeInvocation archDecodeInvocation + rfl + +private theorem selectExt_runA (a : State DetExt → AsidHighIndex) (S : Set AsidHighIndex) (σ : State Unit) : + (NonDetE.liftE (ε := SyscallError) (selectExt (Z := Unit) a S)).run σ = + ({p | ∃ x ∈ S, p = (.ok x, σ)}, False) := by + simp only [liftE_runD, selectExt, nd_bind_applyD, NonDet.get, NonDet.select, NonDet.assert, + StateExt.selectSwitch, NonDet.pure, pure, Bool.false_eq_true, ↓reduceIte] + apply Prod.ext + · ext ⟨r, s'⟩; simp only [Set.mem_ofPred_eq, Set.mem_singleton_iff] + constructor + · rintro ⟨⟨x, s1⟩, ⟨⟨_, s0⟩, h0, ⟨hx, ⟨hxs, hxe⟩, ⟨x1, ⟨_, hx1⟩, heq⟩⟩⟩, h2⟩ + cases h0; cases h2; cases hx1; cases heq + exact ⟨hx.1, hxs, by simp [hxe]⟩ + · rintro ⟨x, hx, heq⟩; cases heq + exact ⟨(x, σ), ⟨(σ, σ), rfl, ⟨(x, σ), ⟨hx, rfl⟩, ⟨((), σ), ⟨hx, rfl⟩, rfl⟩⟩⟩, rfl⟩ + · apply propext; simp only [iff_false, not_or, not_exists, not_and] + refine ⟨⟨id, fun _ h0 => ?_⟩, fun _ _ => id⟩ + simp only [Set.mem_singleton_iff] at h0; subst h0 + refine ⟨id, fun x hx => ?_⟩ + simp only [Set.mem_ofPred_eq] at hx + refine ⟨by simp [hx.1], fun _ _ => id⟩ + +private theorem selectExt_bind_errA {α : Type} (a : State DetExt → AsidHighIndex) {S : Set AsidHighIndex} + {x0 : AsidHighIndex} (hx0 : x0 ∈ S) {e : SyscallError} {σ : State Unit} + {f : AsidHighIndex → SEMonad Unit α} (hf : ∀ x ∈ S, (f x).run σ = ({(.error e, σ)}, False)) : + ((NonDetE.liftE (selectExt (Z := Unit) a S)) >>= f).run σ = ({(.error e, σ)}, False) := by + rw [ExceptT.run_bind, nd_bind_applyD, selectExt_runA] + apply Prod.ext + · ext ⟨r, s'⟩; simp only [Set.mem_ofPred_eq, Set.mem_singleton_iff] + constructor + · rintro ⟨⟨v, s1⟩, ⟨x, hx, heq⟩, hr⟩ + cases heq; dsimp only at hr; rw [hf x hx] at hr; simpa using hr + · rintro heq; cases heq + exact ⟨(.ok x0, σ), ⟨x0, hx0, rfl⟩, by dsimp only; rw [hf x0 hx0]; exact Set.mem_singleton _⟩ + · apply propext; simp only [false_or, iff_false, not_exists, not_and] + rintro ⟨v, s1⟩ ⟨x, hx, heq⟩ + cases heq; dsimp only; rw [hf x hx]; exact id + +private theorem selectExt_bind_okA {α : Type} (a : State DetExt → AsidHighIndex) {S : Set AsidHighIndex} + {x0 : AsidHighIndex} (hx0 : x0 ∈ S) {v : α} {σ : State Unit} + {f : AsidHighIndex → SEMonad Unit α} (hf0 : (f x0).run σ = ({(.ok v, σ)}, False)) + (hfail : ∀ x ∈ S, ¬ ((f x).run σ).2) : + (.ok v, σ) ∈ (((NonDetE.liftE (selectExt (Z := Unit) a S)) >>= f).run σ).1 ∧ + ¬ (((NonDetE.liftE (selectExt (Z := Unit) a S)) >>= f).run σ).2 := by + rw [ExceptT.run_bind, nd_bind_applyD, selectExt_runA] + refine ⟨⟨(.ok x0, σ), ⟨x0, hx0, rfl⟩, by dsimp only; rw [hf0]; exact Set.mem_singleton _⟩, ?_⟩ + simp only [false_or, not_exists, not_and] + rintro ⟨r, s1⟩ ⟨x, hx, heq⟩ + cases heq; exact hfail x hx + +private theorem mapMx_storeWord_exA (xs : List Word) (hal : ∀ p ∈ xs, p &&& mask 3 = 0) (ms : MachineState) : + ∃ ms', ((), ms') ∈ (NonDet.mapMx (fun p => storeWord p 0) xs ms).1 := by + induction xs generalizing ms with + | nil => exact ⟨ms, Set.mem_singleton _⟩ + | cons p ps ih => + unfold NonDet.mapMx + have hp : p &&& mask 3 = 0 := hal p (List.mem_cons_self ..) + have hsw : ∃ ms1, ((), ms1) ∈ (storeWord p 0 ms).1 := by + unfold storeWord NonDet.assert NonDet.modify + simp [hp, NonDet.bind, NonDet.pure, pure] + exact ⟨_, ⟨((), ms), Set.mem_singleton _, Set.mem_singleton _⟩⟩ + obtain ⟨ms1, hms1⟩ := hsw + obtain ⟨ms2, hms2⟩ := ih (fun q hq => hal q (List.mem_cons_of_mem p hq)) ms1 + exact ⟨ms2, ⟨((), ms1), hms1, hms2⟩⟩ + +private theorem deleteObjects_exA (ptr : Word) (bits : Nat) (hal : ptr.toNat % 8 = 0) (σ : State Unit) : + ∃ ms', ((), detype {x | ptr ≤ x ∧ x ≤ ptr + 2 ^ bits - 1} { σ with machineState := ms' }) ∈ + (deleteObjects (Z := Unit) ptr bits σ).1 := by + have hall : ∀ p ∈ uptoEnumStep ptr (ptr + BitVec.ofNat 64 wordSize) (ptr + 2 ^ bits - 1), p &&& mask 3 = 0 := by + intro p hp + unfold uptoEnumStep at hp + split_ifs at hp with hlt + · cases hp + · rw [List.mem_map] at hp + obtain ⟨x, _, rfl⟩ := hp + have hws : BitVec.ofNat 64 wordSize = 8#64 := by decide + have hm3 : (mask 3 : BitVec 64) = 7#64 := by decide + rw [hws, hm3] + have hsub : ptr + 8#64 - ptr = 8#64 := by bv_omega + rw [hsub] + apply BitVec.eq_of_toNat_eq + rw [BitVec.toNat_and, show (7#64 : BitVec 64).toNat = 7 from rfl, Nat.and_two_pow_sub_one_eq_mod _ 3, + BitVec.toNat_add, BitVec.toNat_mul, show (8#64 : BitVec 64).toNat = 8 from rfl, + show (0 : BitVec 64).toNat = 0 from rfl] + omega + obtain ⟨ms', hms'⟩ := mapMx_storeWord_exA _ hall σ.machineState + refine ⟨ms', ?_⟩ + unfold deleteObjects doMachineOp freeMemory + refine ⟨((), { σ with machineState := ms' }), ?_, Set.mem_singleton _⟩ + refine ⟨(σ.machineState, σ), Set.mem_singleton _, ?_⟩ + refine ⟨(((), ms'), σ), ⟨hms', rfl⟩, ?_⟩ + exact ⟨((), { σ with machineState := ms' }), Set.mem_singleton _, Set.mem_singleton _⟩ + +private theorem retypeRegion_asidPool_memA (ptr : ObjRef) (σ : State Unit) : + ([ptr], { σ with kheap := Function.update σ.kheap ptr (some (.archObj (.asidPool fun _ => none))) }) ∈ + (retypeRegion (Z := Unit) ptr 1 0 (.archObject .asidPoolObj) false σ).1 := by + unfold retypeRegion + have hne : (ApiObjectType.archObject .asidPoolObj ≠ .untyped) := by intro h; cases h + have hptrs : ((List.range 1).map fun p => ptrAdd ptr (p * 2 ^ objBitsApi (.archObject .asidPoolObj) 0)) = [ptr] := by + simp [ptrAdd] + refine ⟨(2 ^ objBitsApi (.archObject .asidPoolObj) 0, σ), Set.mem_singleton _, ?_⟩ + refine ⟨([ptr], σ), by rw [hptrs]; exact Set.mem_singleton _, ?_⟩ + refine ⟨((), { σ with kheap := Function.update σ.kheap ptr (some (.archObj (.asidPool fun _ => none))) }), ?_, + Set.mem_singleton _⟩ + unfold NonDet.when; simp only [hne, ↓reduceIte] + refine ⟨(σ.kheap, σ), Set.mem_singleton _, ?_⟩ + refine ⟨(σ.curDomain, σ), Set.mem_singleton _, ?_⟩ + refine ⟨(Function.update σ.kheap ptr (some (.archObj (.asidPool fun _ => none))), σ), + by simp [defaultObject, defaultArchObject], Set.mem_singleton _⟩ + +private theorem asid_high_low_of_shl9A {i base : U64} (hi : i.val < 128) (hb : base.val = i.val * 512) : + asidLowBitsOf (asidOfA base) = 0 ∧ + asidHighBitsOf (asidOfA base) = BitVec.ofNat 7 i.val ∧ + ((ucast asidRepLen (BitVec.ofNat 7 i.val) : Asid) <<< asidLowBits) = asidOfA base := by + have hbLt : base.val < 2 ^ 16 := by omega + have hbv : (asidOfA base).toNat = i.val * 512 := by + unfold asidOfA + have h16 : (asidRepLen : Nat) = 16 := rfl + rw [BitVec.toNat_ofNat, hb, h16, Nat.mod_eq_of_lt (by omega)] + refine ⟨?_, ?_, ?_⟩ + · apply BitVec.eq_of_toNat_eq + unfold asidLowBitsOf asidLowLen + rw [BitVec.toNat_setWidth, hbv, show (0 : BitVec 9).toNat = 0 from rfl] + omega + · apply BitVec.eq_of_toNat_eq + unfold asidHighBitsOf asidLowBits + rw [BitVec.toNat_setWidth, BitVec.toNat_ushiftRight, hbv, BitVec.toNat_ofNat, + Nat.shiftRight_eq_div_pow, show 2 ^ 9 = 512 by decide, + Nat.mul_div_cancel _ (by omega : 0 < 512), Nat.mod_eq_of_lt (by omega)] + · apply BitVec.eq_of_toNat_eq + unfold ucast asidRepLen asidLen asidLowBits asidLowLen + rw [BitVec.toNat_shiftLeft, BitVec.toNat_setWidth, BitVec.toNat_ofNat, hbv, + Nat.mod_eq_of_lt (by omega), Nat.mod_eq_of_lt (by omega), + Nat.shiftLeft_eq, show 2 ^ 9 = 512 by decide, Nat.mod_eq_of_lt (by omega)] + +private theorem run_ensureNoChildrenA {sl : CslotPtr} {σ : State Unit} : + (ensureNoChildren (Z := Unit) sl).run σ = + if ∃ c, σ.cdt c = some sl then ({(.error .revokeFirst, σ)}, False) else ({(.ok (), σ)}, False) := by + unfold ensureNoChildren NonDetE.whenE + rw [run_bind_okD (run_liftE_stD (m := NonDet.gets _) rfl)] + by_cases h : ∃ c, σ.cdt c = some sl + · rw [if_pos h, if_pos h]; rfl + · rw [if_neg h, if_neg h]; rfl + +end spec_helpers + +/-! ## Disjointness of a childless 12-bit untyped frame from `JointObjectA` and `FootNA` -/ + +section frame_disj +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +private theorem not_mem_detype_of_disjA {frame p : U64} {bytes : Nat} (hb : 0 < bytes) + (hal : frame.val % 4096 = 0) (hfit : frame.val + 4096 ≤ 2 ^ 64) + (hd : p.val + bytes ≤ frame.val ∨ frame.val + 4096 ≤ p.val) : + specPtr p ∉ ({x : Fl4SpecA64.ObjRef | specPtr frame ≤ x ∧ x ≤ specPtr frame + 2 ^ 12 - 1} : Set Fl4SpecA64.ObjRef) := by + rintro ⟨h1, h2⟩ + have h1' : (specPtr frame).toNat ≤ (specPtr p).toNat := BitVec.le_def.1 h1 + have h2' : (specPtr p).toNat ≤ (specPtr frame + 2 ^ 12 - 1).toNat := BitVec.le_def.1 h2 + have hspAl : (specPtr frame).toNat % 4096 = 0 := by + have := specPtr_mod frame (n := 12) (by omega) + simpa [show 2 ^ 12 = 4096 by decide, hal] using this + have hspFit : (specPtr frame).toNat + 4096 ≤ 2 ^ 64 := by + have hlt := (specPtr frame).isLt + omega + have htop : (specPtr frame + 2 ^ 12 - 1).toNat = (specPtr frame).toNat + 4095 := by + have hbv : specPtr frame + 2 ^ 12 - 1 = specPtr frame + 4095#64 := by + rw [show (2 ^ 12 : BitVec 64) = 4096#64 by decide]; bv_omega + rw [hbv, BitVec.toNat_add, show (4095#64 : BitVec 64).toNat = 4095 from rfl, Nat.mod_eq_of_lt (by omega)] + set k : Nat := (specPtr p).toNat - (specPtr frame).toNat + have hkLt : k < 4096 := by omega + have hpk : specPtr p = specPtr frame + BitVec.ofNat 64 k := by + apply BitVec.eq_of_toNat_eq + rw [BitVec.toNat_add, BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega), Nat.mod_eq_of_lt (by omega)] + omega + have hpEq : p = wa frame ⟨BitVec.ofNat 64 k⟩ := specPtr_inj (by rw [specPtr_wa]; exact hpk) + have hpVal : p.val = frame.val + k := by + rw [hpEq, wa_valP] + have hkVal : (⟨BitVec.ofNat 64 k⟩ : U64).val = k := by + change (BitVec.ofNat 64 k).toNat = k + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)] + rw [hkVal, Nat.mod_eq_of_lt (by omega)] + omega + +private theorem mem_off_frameA {frame b : U64} + (hdisj : ∀ p bytes, JointObjectA Z H p bytes → p.val + bytes ≤ frame.val ∨ frame.val + 4096 ≤ p.val) + (hksb : (L.ksb s).val + ksSizeA ≤ frame.val ∨ frame.val + 4096 ≤ (L.ksb s).val) + (hksFit : (L.ksb s).val + ksSizeA ≤ 2 ^ 64) + (hJ : JAtZAX (L := L) AR Z s a σ m) + (hscs : H.scs = Z.C) + (hscFit : ∀ c ∈ H.scs, c.val + 2 ^ H.scBits c ≤ 2 ^ 64 ∧ 2 ^ H.scBits c ≥ 128) + (hfrGe : 4096 ≤ frame.val) + (hvsFit : ∀ p ∈ Z.Pp, p.val + 4096 ≤ 2 ^ 64) + (hs1Fit : ∀ p ∈ Z.S1, p.val + 4096 ≤ 2 ^ 64) + (hyFit : ∀ y ∈ Z.Y, y.val + 8192 ≤ 2 ^ 64) + (hxFit : ∀ x ∈ Z.X, x.val + 8192 ≤ 2 ^ 64) + (ht2Fit : ∀ t ∈ Z.T2, t.val + 4096 ≤ 2 ^ 64) + (hfn : FootNA (L := L) Z s b) : + ¬ (frame.val ≤ b.val ∧ b.val < frame.val + 4096) := by + have hnodeOff : ∀ c ∈ Z.Cs, ∀ o : U64, o.val < 4288 → + ¬ (frame.val ≤ (L.nodeAt s c o).val ∧ (L.nodeAt s c o).val < frame.val + 4096) := by + intro c hc o ho ⟨hb1, hb2⟩ + obtain ⟨n, _, hn128, hcs⟩ := hJ.1.csN + have hcLt : c.val < 128 := by have := (hcs c).1 hc; omega + have hnv : (L.nodeAt s c o).val = (L.ksb s).val + 223488 + c.val * 4288 + o.val := by + unfold LawfulStore.nodeAt + have h0 : (wa (L.ksb s) 223488#u64).val = (L.ksb s).val + 223488 := by + rw [wa_valP, show (223488#u64 : U64).val = 223488 from rfl, Nat.mod_eq_of_lt (by unfold ksSizeA at hksFit; omega)] + have h1 : (core.num.U64.wrapping_mul c 4288#u64).val = c.val * 4288 := by + simp [core.num.U64.wrapping_mul_val_eq, U64.size_def, U64.numBits]; omega + have h2 : (wa (wa (L.ksb s) 223488#u64) (core.num.U64.wrapping_mul c 4288#u64)).val = + (L.ksb s).val + 223488 + c.val * 4288 := by + rw [wa_valP, h0, h1, Nat.mod_eq_of_lt (by unfold ksSizeA at hksFit; omega)] + rw [wa_valP, h2, Nat.mod_eq_of_lt (by unfold ksSizeA at hksFit; omega)] + unfold ksSizeA at hksb; omega + cases hfn with + | node c w hc hw => + rcases hw with rfl | rfl | rfl | rfl + · exact hnodeOff c hc _ (by simp [layout.node.CUR_THREAD]) + · exact hnodeOff c hc _ (by simp [layout.node.IDLE_THREAD]) + · exact hnodeOff c hc _ (by simp [layout.node.CUR_SC]) + · exact hnodeOff c hc _ (by simp [layout.node.IDLE_SC]) + | cur w hw => + rcases hw with rfl | rfl + · exact hnodeOff _ hJ.1.sch.cpuCs _ (by simp [layout.node.SCHED_ACTION]) + · exact hnodeOff _ hJ.1.sch.cpuCs _ (by simp [layout.node.CUR_TIME]) + | cur2 w hw => + rcases hw with rfl | rfl + · exact hnodeOff _ hJ.1.sch.cpuCs _ (by simp [layout.node.CONSUMED]) + · exact hnodeOff _ hJ.1.sch.cpuCs _ (by simp [layout.node.REPROGRAM]) + | tcb t _ ht hw => + intro ⟨hb1, hb2⟩ + have hth : Z.th t = some .tcb := (hJ.1.thT t).2 ht + have hd := hdisj t ObjKindA.tcb.size (Or.inr (Or.inr (Or.inl ⟨.tcb, hth, rfl⟩))) + have hfit := hJ.1.place.fit t .tcb hth + have hwb : t.val ≤ b.val ∧ b.val < t.val + ObjKindA.tcb.size := by + simp only [ObjKindA.size] at hfit ⊢ + rcases hw with rfl | rfl | rfl | rfl | rfl + · have hstLt : layout.tcb.STATE.val < 4096 := by simp [layout.tcb.STATE] + rw [stA, wa_valP, Nat.mod_eq_of_lt (by omega)]; omega + · rw [scA, wa_valP, show (3464#u64 : U64).val = 3464 from rfl, Nat.mod_eq_of_lt (by omega)]; omega + · rw [affA, wa_valP, show (3520#u64 : U64).val = 3520 from rfl, Nat.mod_eq_of_lt (by omega)]; omega + · rw [wa_valP, show (3664#u64 : U64).val = 3664 from rfl, Nat.mod_eq_of_lt (by omega)]; omega + · rw [runVcpuA, wa_valP, show (3672#u64 : U64).val = 3672 from rfl, Nat.mod_eq_of_lt (by omega)]; omega + omega + | tcb2 t _ ht hw => + intro ⟨hb1, hb2⟩ + have hth : Z.th t = some .tcb := (hJ.1.thT t).2 ht + have hd := hdisj t ObjKindA.tcb.size (Or.inr (Or.inr (Or.inl ⟨.tcb, hth, rfl⟩))) + have hfit := hJ.1.place.fit t .tcb hth + have hwb : t.val ≤ b.val ∧ b.val < t.val + ObjKindA.tcb.size := by + simp only [ObjKindA.size] at hfit ⊢ + rcases hw with rfl | rfl + · rw [prioA, wa_valP, show (3448#u64 : U64).val = 3448 from rfl, Nat.mod_eq_of_lt (by omega)]; omega + · rw [domA, wa_valP, show (3432#u64 : U64).val = 3432 from rfl, Nat.mod_eq_of_lt (by omega)]; omega + omega + | sc c hc => + intro ⟨hb1, hb2⟩ + have hcH : c ∈ H.scs := hscs.symm ▸ hc + have hd := hdisj c (2 ^ H.scBits c) (JointObjectA.of_sc hcH) + have ⟨hf, hg⟩ := hscFit c hcH + have hv : (wa c 24#u64).val = c.val + 24 := by + rw [wa_valP, show (24#u64 : U64).val = 24 from rfl, Nat.mod_eq_of_lt (by omega)] + omega + | sc2 c hc => + intro ⟨hb1, hb2⟩ + have hcH : c ∈ H.scs := hscs.symm ▸ hc + have hd := hdisj c (2 ^ H.scBits c) (JointObjectA.of_sc hcH) + have ⟨hf, hg⟩ := hscFit c hcH + have hv : (wa c 72#u64).val = c.val + 72 := by + rw [wa_valP, show (72#u64 : U64).val = 72 from rfl, Nat.mod_eq_of_lt (by omega)] + omega + | irq i hi => + intro ⟨hb1, hb2⟩ + have hv := irqWordA_val hksFit hi + unfold ksSizeA at hksb; omega + | glob _ hw => + intro ⟨hb1, hb2⟩ + have hwb : (L.ksb s).val ≤ b.val ∧ b.val < (L.ksb s).val + ksSizeA := by + unfold ksSizeA at hksFit ⊢ + rcases hw with rfl | rfl + · rw [wa_valP, show (8#u64 : U64).val = 8 from rfl, Nat.mod_eq_of_lt (by omega)]; omega + · rw [wa_valP, show (16#u64 : U64).val = 16 from rfl, Nat.mod_eq_of_lt (by omega)]; omega + omega + | vcpu v _ hv hw => + intro ⟨hb1, hb2⟩ + have hd := hdisj v 4096 (Or.inl ⟨rfl, Or.inr (Or.inr (Or.inr (Or.inl hv)))⟩) + rcases hw with rfl | rfl + · omega + · by_cases hlt : v.val + 24 < 2 ^ 64 + · have h24 : (wa v 24#u64).val = v.val + 24 := by + rw [wa_valP, show (24#u64 : U64).val = 24 from rfl, Nat.mod_eq_of_lt hlt] + omega + · have hlt2 : v.val < 2 ^ 64 := by simpa using v.hBounds + have h24 : (wa v 24#u64).val = v.val + 24 - 2 ^ 64 := by + rw [wa_valP, show (24#u64 : U64).val = 24 from rfl, + Nat.mod_eq_sub_mod (by omega), Nat.mod_eq_of_lt (by omega)] + omega + | vs _ hw => + intro ⟨hb1, hb2⟩ + cases hw with + | asid hi hhi => + have hv := atAF_valA (by unfold ksSizeA at hksFit; omega) hhi + unfold ksSizeA at hksb; omega + | pool p lo hp hlo => + have hd := hdisj p 4096 (Or.inl ⟨rfl, Or.inl hp⟩) + have hv := peAF_valA (hvsFit p hp) hlo + omega + | pt p i hp hi => + have hd := hdisj p 4096 (Or.inl ⟨rfl, Or.inr (Or.inl hp)⟩) + have hv := peAF_valA (hs1Fit p hp) hi + omega + | high y i hy hi => + have hd := hdisj y 8192 (Or.inr (Or.inl ⟨rfl, Or.inr hy⟩)) + have hf := hyFit y hy + have hy4 : (wa y 4096#u64).val = y.val + 4096 := by + rw [wa_valP, show (4096#u64 : U64).val = 4096 from rfl, Nat.mod_eq_of_lt (by omega)] + have hv := peAF_valA (by rw [hy4]; omega) hi + omega + | ext _ hw => + intro ⟨hb1, hb2⟩ + cases hw with + | s2lo r i hr hi => + have hd := hdisj r 8192 (Or.inr (Or.inl ⟨rfl, Or.inl hr⟩)) + have hf := hxFit r hr + have hv := peAF_valA (p := r) (by omega) hi + omega + | s2hi r i hr hi => + have hd := hdisj r 8192 (Or.inr (Or.inl ⟨rfl, Or.inl hr⟩)) + have hf := hxFit r hr + have hr4 : (wa r 4096#u64).val = r.val + 4096 := by + rw [wa_valP, show (4096#u64 : U64).val = 4096 from rfl, Nat.mod_eq_of_lt (by omega)] + have hv := peAF_valA (by rw [hr4]; omega) hi + omega + | s2pt t i ht hi => + have hd := hdisj t 4096 (Or.inl ⟨rfl, Or.inr (Or.inr (Or.inl ht))⟩) + have hv := peAF_valA (ht2Fit t ht) hi + omega + | wl0 y hy => + have hd := hdisj y 8192 (Or.inr (Or.inl ⟨rfl, Or.inr hy⟩)) + have hf := hyFit y hy + have hv := peAF_valA (p := y) (lo := 511#u64) (by omega) (by decide) + have h511 : (511#u64 : U64).val = 511 := rfl + omega + | wlo y i hy hi => + have hd := hdisj y 8192 (Or.inr (Or.inl ⟨rfl, Or.inr hy⟩)) + have hf := hyFit y hy + have hv := peAF_valA (p := y) (lo := i) (by omega) (by omega) + omega + +private theorem isSlot_off_frameA {frame y : U64} {k : U64} (hk : k.val < 32) + (hP : PlaceA Z.th (L.ksb s)) + (hdisj : ∀ p bytes, JointObjectA Z H p bytes → p.val + bytes ≤ frame.val ∨ frame.val + 4096 ≤ p.val) + (hy : IsSlotA Z.th y) : + ¬ (frame.val ≤ (wa y k).val ∧ (wa y k).val < frame.val + 4096) := by + intro ⟨h1, h2⟩ + obtain ⟨p, ⟨r, hth, idx, hidx, rfl⟩ | ⟨hth, idx, hidx, rfl⟩⟩ := hy + · have hd := hdisj p (ObjKindA.cnode r).size (Or.inr (Or.inr (Or.inl ⟨.cnode r, hth, rfl⟩))) + have hfit := hP.fit p (.cnode r) hth + have hsz : (ObjKindA.cnode r).size = 2 ^ r * 32 := by rw [ObjKindA.size, Nat.pow_add] + rw [hsz] at hd hfit + have hlt : idx * 32 + 32 ≤ 2 ^ r * 32 := by + have : idx + 1 ≤ 2 ^ r := hidx + calc idx * 32 + 32 = (idx + 1) * 32 := by omega + _ ≤ 2 ^ r * 32 := Nat.mul_le_mul_right 32 this + have hsv : (slotAddrA (specPtr p) (BitVec.ofNat 64 idx)).val = p.val + idx * 32 := by + rw [slotAddrA_sp] + change (p.bv + BitVec.ofNat 64 idx * 32#64).toNat = p.val + idx * 32 + have hidx64 : idx < 2 ^ 64 := by omega + have hmul64 : idx * 32 < 2 ^ 64 := by omega + rw [BitVec.toNat_add, BitVec.toNat_mul, BitVec.toNat_ofNat, + show (32#64 : BitVec 64).toNat = 32 from rfl, + Nat.mod_eq_of_lt hidx64, Nat.mod_eq_of_lt hmul64, + show p.bv.toNat = p.val from rfl, Nat.mod_eq_of_lt (by omega)] + have hwv : (wa (slotAddrA (specPtr p) (BitVec.ofNat 64 idx)) k).val = p.val + idx * 32 + k.val := by + rw [wa_valP, hsv, Nat.mod_eq_of_lt (by omega)] + omega + · have hd := hdisj p ObjKindA.tcb.size (Or.inr (Or.inr (Or.inl ⟨.tcb, hth, rfl⟩))) + have hfit := hP.fit p .tcb hth + simp only [ObjKindA.size] at hd hfit + have hidxLt : idx < 7 := hidx + have hsv : (slotAddrA (specPtr p) (BitVec.ofNat 64 idx)).val = p.val + idx * 32 := by + rw [slotAddrA_sp] + change (p.bv + BitVec.ofNat 64 idx * 32#64).toNat = p.val + idx * 32 + have hidx64 : idx < 2 ^ 64 := by omega + have hmul64 : idx * 32 < 2 ^ 64 := by omega + rw [BitVec.toNat_add, BitVec.toNat_mul, BitVec.toNat_ofNat, + show (32#64 : BitVec 64).toNat = 32 from rfl, + Nat.mod_eq_of_lt hidx64, Nat.mod_eq_of_lt hmul64, + show p.bv.toNat = p.val from rfl, Nat.mod_eq_of_lt (by omega)] + have hwv : (wa (slotAddrA (specPtr p) (BitVec.ofNat 64 idx)) k).val = p.val + idx * 32 + k.val := by + rw [wa_valP, hsv, Nat.mod_eq_of_lt (by omega)] + omega + +end frame_disj + +/-! ## `MakePool` perform tail (`s1 → s2 → s3 → s4 → s5`) on `JAtZAX`, `TreeA`, `CarryA` -/ + +section perform +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +private theorem setcap_ut_treeA {T Ω Sg : Set U64} (hO : OSOK T Ω Sg OS) {s1 : S} + {G : List (List U64)} {par : U64 → Option U64} (hT : TreeA st OS G par s1) + {x frame fi : U64} (hx : x ∈ OS) (hcx : capA st s1 x = .Untyped frame 12#u64 fi false) : + let s2 := L.wr (L.wr s1 x ⟨(CapRt.encodeF (.Untyped frame 12#u64 4096#u64 false)).1⟩) + (wa x 8#u64) ⟨(CapRt.encodeF (.Untyped frame 12#u64 4096#u64 false)).2⟩ + TreeA st OS G par s2 := by + intro s2 + have hw : CapRt.EncWf (.Untyped frame 12#u64 4096#u64 false) := by + have w := CapRt.decodeF_encWf (L.mem s1 x).bv (L.mem s1 (wa x 8#u64)).bv + have e : CapRt.decodeF (L.mem s1 x).bv (L.mem s1 (wa x 8#u64)).bv = .Untyped frame 12#u64 fi false := hcx + rw [e] at w; exact ⟨w.1, by decide⟩ + have ha0x := hO.al0 hx + have ha8x := hO.al hx (k := 8#u64) (by simp) (by simp) + have hsc_eq : obj.Slot.set_cap st x s1 (.Untyped frame 12#u64 4096#u64 false) = ok s2 := + set_cap_eq (CapRt.encode_eq _) + obtain ⟨hcx2', hko'⟩ := setcap_frA hO hx hw hsc_eq + have hcx2 : capA st s2 x = .Untyped frame 12#u64 4096#u64 false := hcx2' + have hko : ∀ y ∈ OS, y ≠ x → capA st s2 y = capA st s1 y := hko' + have hslotOff : ∀ y ∈ OS, ∀ k : U64, k = 16#u64 ∨ k = 24#u64 → + L.mem s2 (wa y k) = L.mem s1 (wa y k) := fun y hy k hk => by + have hay : Aligned (wa y k) := by + rcases hk with rfl | rfl <;> exact hO.al hy (by simp) (by simp) + have hfitx := hO.fit x hx; have hfity := hO.fit y hy + have hne0 : x ≠ wa y k := by + intro e + have hkv : (wa y k).val = y.val + k.val := by + rcases hk with rfl | rfl + · rw [wa_valP, show (16#u64 : U64).val = 16 from rfl, Nat.mod_eq_of_lt (by omega)] + · rw [wa_valP, show (24#u64 : U64).val = 24 from rfl, Nat.mod_eq_of_lt (by omega)] + have hev := congrArg UScalar.val e; rw [hkv] at hev + by_cases hyx : y = x + · subst hyx + rcases hk with rfl | rfl + · have : (16#u64 : U64).val = 16 := rfl; omega + · have : (24#u64 : U64).val = 24 := rfl; omega + · rcases hO.disj x hx y hy (Ne.symm hyx) with hd | hd <;> rcases hk with rfl | rfl + · have : (16#u64 : U64).val = 16 := rfl; omega + · have : (24#u64 : U64).val = 24 := rfl; omega + · have : (16#u64 : U64).val = 16 := rfl; omega + · have : (24#u64 : U64).val = 24 := rfl; omega + have hne8 : wa x 8#u64 ≠ wa y k := by + intro e + have hx8 : (wa x 8#u64).val = x.val + 8 := by + rw [wa_valP, show (8#u64 : U64).val = 8 from rfl, Nat.mod_eq_of_lt (by omega)] + have hkv : (wa y k).val = y.val + k.val := by + rcases hk with rfl | rfl + · rw [wa_valP, show (16#u64 : U64).val = 16 from rfl, Nat.mod_eq_of_lt (by omega)] + · rw [wa_valP, show (24#u64 : U64).val = 24 from rfl, Nat.mod_eq_of_lt (by omega)] + have hev := congrArg UScalar.val e; rw [hx8, hkv] at hev + by_cases hyx : y = x + · subst hyx + rcases hk with rfl | rfl + · have : (16#u64 : U64).val = 16 := rfl; omega + · have : (24#u64 : U64).val = 24 := rfl; omega + · rcases hO.disj x hx y hy (Ne.symm hyx) with hd | hd <;> rcases hk with rfl | rfl + · have : (16#u64 : U64).val = 16 := rfl; omega + · have : (24#u64 : U64).val = 24 := rfl; omega + · have : (16#u64 : U64).val = 16 := rfl; omega + · have : (24#u64 : U64).val = 24 := rfl; omega + rw [L.mem_wr_ne ha8x hay (fun e => hne8 (UScalar.eq_of_val_eq e)), + L.mem_wr_ne ha0x hay (fun e => hne0 (UScalar.eq_of_val_eq e))] + have hnx : ∀ y ∈ OS, NxA st s2 y = NxA st s1 y := fun y hy => by + unfold NxA; rw [hslotOff y hy 24#u64 (Or.inr rfl)] + have hLk : MdbLk st OS G s2 := by + refine ⟨hT.lk.nodup, hT.lk.ne_nil, ?_, ?_, ?_, ?_⟩ + · intro ch hch i a b ha hb + have haOS := ((hT.lk.members a).1 (List.mem_flatten.2 ⟨ch, hch, List.mem_of_getElem? ha⟩)).1 + have hbOS := ((hT.lk.members b).1 (List.mem_flatten.2 ⟨ch, hch, List.mem_of_getElem? hb⟩)).1 + rw [hnx a haOS, hslotOff b hbOS 16#u64 (Or.inl rfl)] + exact hT.lk.chain ch hch i a b ha hb + · intro ch hch a ha + have haOS := ((hT.lk.members a).1 (List.mem_flatten.2 ⟨ch, hch, List.mem_of_head? ha⟩)).1 + rw [hslotOff a haOS 16#u64 (Or.inl rfl)] + exact hT.lk.head ch hch a ha + · intro ch hch a ha + have haOS := ((hT.lk.members a).1 (List.mem_flatten.2 ⟨ch, hch, List.mem_of_getLast? ha⟩)).1 + rw [hnx a haOS] + exact hT.lk.last ch hch a ha + · intro y; rw [hT.lk.members y] + constructor + · rintro ⟨hy, hne⟩; refine ⟨hy, ?_⟩ + by_cases hyx : y = x + · subst hyx; rw [hcx2]; intro h; cases h + · rw [hko y hy hyx]; exact hne + · rintro ⟨hy, hne⟩; refine ⟨hy, ?_⟩ + by_cases hyx : y = x + · subst hyx; rw [hcx]; intro h; cases h + · rw [hko y hy hyx] at hne; exact hne + have hbaseEq : baseA (capA st s2 x) = baseA (capA st s1 x) := by rw [hcx2, hcx]; rfl + have htopEq : topA (capA st s2 x) = topA (capA st s1 x) := by rw [hcx2, hcx]; rfl + have hphysEq : physA (capA st s2 x) = physA (capA st s1 x) := by rw [hcx2, hcx]; rfl + have hfiLe : fi.val ≤ 4096 := by + have := hT.ut.fib x hx frame 12#u64 fi false hcx + simpa [show (12#u64 : U64).val = 12 from rfl] using this + have hUt : UtInvA st OS par s2 := by + refine ⟨?_, ?_, ?_, ?_, ?_, ?_, ?_⟩ + · intro y hy hp + by_cases hyx : y = x + · subst hyx; rw [hbaseEq, htopEq]; exact hT.ut.reg _ hx (hphysEq ▸ hp) + · rw [hko y hy hyx] at hp ⊢; exact hT.ut.reg y hy hp + · intro u hu p b i d hcu y hyd + have hyOS := (desc_in hT.parIn hyd).1 + have hcapY : physA (capA st s2 y) = physA (capA st s1 y) ∧ + baseA (capA st s2 y) = baseA (capA st s1 y) ∧ + topA (capA st s2 y) = topA (capA st s1 y) := by + by_cases hyx : y = x + · subst hyx; exact ⟨hphysEq, hbaseEq, htopEq⟩ + · rw [hko y hyOS hyx]; exact ⟨rfl, rfl, rfl⟩ + rw [hcapY.1, hcapY.2.1, hcapY.2.2] + by_cases hux : u = x + · subst hux; rw [hcx2] at hcu; cases hcu + obtain ⟨hp, hb, ht⟩ := hT.ut.inc u hu frame 12#u64 fi false hcx y hyd + exact ⟨hp, hb, by have : (4096#u64 : U64).val = 4096 := rfl; omega⟩ + · rw [hko u hu hux] at hcu + exact hT.ut.inc u hu p b i d hcu y hyd + · intro u hu p b i d hcu y hy hp + have hcapY : physA (capA st s2 y) = physA (capA st s1 y) ∧ + baseA (capA st s2 y) = baseA (capA st s1 y) ∧ + topA (capA st s2 y) = topA (capA st s1 y) ∧ + isUtA (capA st s2 y) = isUtA (capA st s1 y) := by + by_cases hyx : y = x + · subst hyx; exact ⟨hphysEq, hbaseEq, htopEq, by rw [hcx2, hcx]; rfl⟩ + · rw [hko y hy hyx]; exact ⟨rfl, rfl, rfl, rfl⟩ + have hcapU : baseA (capA st s2 u) = baseA (capA st s1 u) ∧ + topA (capA st s2 u) = topA (capA st s1 u) := by + by_cases hux : u = x + · subst hux; exact ⟨hbaseEq, htopEq⟩ + · rw [hko u hu hux]; exact ⟨rfl, rfl⟩ + rw [hcapY.1] at hp + by_cases hux : u = x + · subst hux; rw [hcx2] at hcu; cases hcu + rcases hT.ut.noov u hu frame 12#u64 fi false hcx y hy hp with hdis | ⟨his, h1, h2⟩ + · left; unfold DisjRA at hdis ⊢; rw [hcapY.2.1, hcapY.2.2.1] + have : (4096#u64 : U64).val = 4096 := rfl + rcases hdis with h | h <;> [left; right] <;> omega + · right; exact ⟨hcapY.2.2.2 ▸ his, + ⟨by rw [hcapY.2.1, hcapU.1]; exact h1, by rw [hcapU.2, hcapY.2.2.1]; exact h2⟩⟩ + · rw [hko u hu hux] at hcu + rcases hT.ut.noov u hu p b i d hcu y hy hp with hdis | ⟨his, h1, h2⟩ + · left; unfold DisjRA at hdis ⊢; rw [hcapY.2.1, hcapY.2.2.1]; exact hdis + · right; exact ⟨hcapY.2.2.2 ▸ his, + ⟨by rw [hcapY.2.1, hcapU.1]; exact h1, by rw [hcapU.2, hcapY.2.2.1]; exact h2⟩⟩ + · intro u1 hu1 u2 hu2 hne p b i1 i2 d1 d2 h1 h2 + by_cases e1 : u1 = x + · subst e1; rw [hcx2] at h1; cases h1; exact Or.inl (by decide) + · by_cases e2 : u2 = x + · subst e2; rw [hcx2] at h2; cases h2; exact Or.inr (by decide) + · rw [hko u1 hu1 e1] at h1; rw [hko u2 hu2 e2] at h2 + exact hT.ut.excl u1 hu1 u2 hu2 hne p b i1 i2 d1 d2 h1 h2 + · intro u hu p b i d hcu + by_cases hux : u = x + · subst hux; rw [hcx2] at hcu; cases hcu; decide + · rw [hko u hu hux] at hcu; exact hT.ut.fib u hu p b i d hcu + · intro u hu p b i d hcu y hy hyu hp hnd + have hu' : ∃ i', capA st s1 u = .Untyped p b i' d := by + by_cases hux : u = x + · subst hux; rw [hcx2] at hcu; cases hcu; exact ⟨fi, hcx⟩ + · rw [hko u hu hux] at hcu; exact ⟨i, hcu⟩ + obtain ⟨i', hcu'⟩ := hu' + have hnd1 : ¬ DisjA (capA st s1 y) (capA st s1 u) := fun hdis => hnd (by + have h1 : baseA (capA st s2 y) = baseA (capA st s1 y) ∧ topA (capA st s2 y) = topA (capA st s1 y) := by + by_cases e : y = x + · subst e; exact ⟨hbaseEq, htopEq⟩ + · rw [hko y hy e]; exact ⟨rfl, rfl⟩ + have h2 : baseA (capA st s2 u) = baseA (capA st s1 u) ∧ topA (capA st s2 u) = topA (capA st s1 u) := by + by_cases e : u = x + · subst e; exact ⟨hbaseEq, htopEq⟩ + · rw [hko u hu e]; exact ⟨rfl, rfl⟩ + unfold DisjA at hdis ⊢; rw [h1.1, h1.2, h2.1, h2.2]; exact hdis) + have hp1 : physA (capA st s1 y) = true := by + by_cases e : y = x + · subst e; exact hphysEq ▸ hp + · rw [hko y hy e] at hp; exact hp + exact hT.ut.drange u hu p b i' d hcu' y hy hyu hp1 hnd1 + · intro u y hyd hu + have ⟨hyOS, huOS⟩ := desc_in hT.parIn hyd + have hu1 : isUtA (capA st s1 y) = true := by + by_cases e : y = x + · subst e; rw [hcx]; rfl + · rw [hko y hyOS e] at hu; exact hu + have hres := hT.ut.anc u y hyd hu1 + by_cases e : u = x + · subst e; rw [hcx2]; rfl + · rw [hko u huOS e]; exact hres + exact ⟨hLk, hT.tree, hUt⟩ + +private theorem insA_splitA {x y : U64} {c : List U64} (hx : x ∈ c) (hnd : c.Nodup) : + ∃ a b, c = a ++ x :: b ∧ insA x y c = a ++ x :: y :: b := by + induction c with + | nil => cases hx + | cons hd tl ih => + simp only [List.mem_cons] at hx + rcases List.nodup_cons.1 hnd with ⟨hnot, hnd'⟩ + rcases hx with rfl | htl + · refine ⟨[], tl, rfl, ?_⟩ + unfold insA; simp + · obtain ⟨a, b, h1, h2⟩ := ih htl hnd' + have hne : hd ≠ x := fun e => hnot (e ▸ htl) + refine ⟨hd :: a, b, by rw [h1]; rfl, ?_⟩ + unfold insA; simp [hne, h2] + +private theorem insA_mem_iffA {x y z : U64} {c : List U64} (hx : x ∈ c) : + z ∈ insA x y c ↔ z = y ∨ z ∈ c := by + induction c with + | nil => cases hx + | cons hd tl ih => + unfold insA + by_cases he : hd = x + · subst he; simp [List.mem_cons]; tauto + · rw [if_neg he] + have htl : x ∈ tl := by + simp only [List.mem_cons] at hx; rcases hx with rfl | h + · exact absurd rfl he + · exact h + simp only [List.mem_cons, ih htl]; tauto + +private theorem nodup_of_mem_flattenA {α : Type} {G : List (List α)} (hnd : G.flatten.Nodup) + {c : List α} (hc : c ∈ G) : c.Nodup := by + induction G with + | nil => cases hc + | cons hd tl ih => + simp only [List.flatten_cons, List.nodup_append] at hnd + rcases List.mem_cons.1 hc with rfl | htl + · exact hnd.1 + · exact ih hnd.2.1 htl + +private theorem nodup_flatten_eqA {α : Type} {G : List (List α)} (hnd : G.flatten.Nodup) + {c1 c2 : List α} (hc1 : c1 ∈ G) (hc2 : c2 ∈ G) {x : α} (h1 : x ∈ c1) (h2 : x ∈ c2) : c1 = c2 := by + induction G with + | nil => cases hc1 + | cons hd tl ih => + simp only [List.flatten_cons, List.nodup_append] at hnd + rcases List.mem_cons.1 hc1 with rfl | h1' <;> rcases List.mem_cons.1 hc2 with rfl | h2' + · rfl + · exact absurd h2 (fun hm => hnd.2.2 x h1 x (List.mem_flatten.2 ⟨c2, h2', hm⟩) rfl) + · exact absurd h1 (fun hm => hnd.2.2 x h2 x (List.mem_flatten.2 ⟨c1, h1', hm⟩) rfl) + · exact ih hnd.2.1 h1' h2' + +private theorem idx_eq_of_getElem_sdA {a b : List U64} {ss sd : U64} + (hnot : sd ∉ a ++ ss :: b) {idx : Nat} (h : (a ++ ss :: sd :: b)[idx]? = some sd) : + idx = a.length + 1 := by + by_contra hne + rcases lt_or_gt_of_ne hne with hlt | hgt + · have h1 : (a ++ ss :: sd :: b)[idx]? = (a ++ [ss])[idx]? := by + rw [show a ++ ss :: sd :: b = (a ++ [ss]) ++ sd :: b by simp [List.append_assoc], + List.getElem?_append_left (by simp; omega)] + rw [h1] at h + have hm := List.mem_of_getElem? h + simp only [List.mem_append, List.mem_singleton] at hm + rcases hm with ha | rfl + · exact hnot (List.mem_append_left _ ha) + · exact hnot (List.mem_append_right _ (List.mem_cons_self ..)) + · have h1 : (a ++ ss :: sd :: b)[idx]? = b[idx - (a.length + 2)]? := by + rw [show a ++ ss :: sd :: b = (a ++ [ss, sd]) ++ b by simp [List.append_assoc], + List.getElem?_append_right (by simp; omega)] + simp + rw [h1] at h + exact hnot (List.mem_append_right _ (List.mem_cons_of_mem _ (List.mem_of_getElem? h))) + +private theorem finMA_insAsidPoolA {mem1 mem2 : U64 → U64} {G : List (List U64)} + {ss sd : U64} {p base : U64} + (hF : FinMA mem1 G) (hnd : G.flatten.Nodup) + (hsG : ss ∈ G.flatten) (hdG : sd ∉ G.flatten) + (hssUt : okeyA (capAtA mem1 ss) = none) + (hkeep : ∀ y ∈ G.flatten, capAtA mem2 y = capAtA mem1 y) + (hsd : capAtA mem2 sd = .AsidPool p base) + (hnoOvl : ∀ y ∈ G.flatten, ¬ OvlA (absCapA (.AsidPool p base)) (absCapA (capAtA mem1 y))) : + FinMA mem2 (insG ss sd G) := by + have hsdNe : sd ≠ ss := fun e => hdG (e ▸ hsG) + have hnoKey : ∀ y ∈ G.flatten, okeyA (capAtA mem1 y) ≠ some (.pool p) := fun y hy hk => by + exact hnoOvl y hy (ovl_of_keyA (a := .AsidPool p base) (b := capAtA mem1 y) hk.symm (by simp [okeyA])) + have hmem_ins : ∀ y, y ∈ (insG ss sd G).flatten ↔ y = sd ∨ y ∈ G.flatten := by + intro y + constructor + · intro hy + obtain ⟨ch, hch, hych⟩ := List.mem_flatten.1 hy + obtain ⟨ch0, hch0, rfl⟩ := List.mem_map.1 hch + by_cases hss : ss ∈ ch0 + · rcases (insA_mem_iffA hss).1 hych with rfl | h0 + · exact Or.inl rfl + · exact Or.inr (List.mem_flatten.2 ⟨ch0, hch0, h0⟩) + · rw [insA_of_not_mem hss] at hych + exact Or.inr (List.mem_flatten.2 ⟨ch0, hch0, hych⟩) + · rintro (rfl | hy) + · obtain ⟨ch0, hch0, hss⟩ := List.mem_flatten.1 hsG + exact List.mem_flatten.2 ⟨_, List.mem_map.2 ⟨ch0, hch0, rfl⟩, + (insA_mem_iffA hss).2 (Or.inl rfl)⟩ + · obtain ⟨ch0, hch0, hy0⟩ := List.mem_flatten.1 hy + refine List.mem_flatten.2 ⟨_, List.mem_map.2 ⟨ch0, hch0, rfl⟩, ?_⟩ + by_cases hss : ss ∈ ch0 + · exact (insA_mem_iffA hss).2 (Or.inr hy0) + · rw [insA_of_not_mem hss]; exact hy0 + refine ⟨?_, ?_, ?_, ?_⟩ + · -- conn + intro ch1 hch1 ch2 hch2 x hx y hy ho + obtain ⟨c1, hc1, rfl⟩ := List.mem_map.1 hch1 + obtain ⟨c2, hc2, rfl⟩ := List.mem_map.1 hch2 + have hx' : x = sd ∨ x ∈ c1 := by + by_cases h1 : ss ∈ c1 + · exact (insA_mem_iffA h1).1 hx + · rw [insA_of_not_mem h1] at hx; exact Or.inr hx + have hy' : y = sd ∨ y ∈ c2 := by + by_cases h2 : ss ∈ c2 + · exact (insA_mem_iffA h2).1 hy + · rw [insA_of_not_mem h2] at hy; exact Or.inr hy + rcases hx' with rfl | hx1 + · rcases hy' with rfl | hy2 + · have h1 : ss ∈ c1 := by + by_contra hn; rw [insA_of_not_mem hn] at hx; exact hdG (List.mem_flatten.2 ⟨c1, hc1, hx⟩) + have h2 : ss ∈ c2 := by + by_contra hn; rw [insA_of_not_mem hn] at hy; exact hdG (List.mem_flatten.2 ⟨c2, hc2, hy⟩) + have hce : c1 = c2 := nodup_flatten_eqA hnd hc1 hc2 h1 h2 + subst hce; rfl + · have hyG := List.mem_flatten.2 ⟨c2, hc2, hy2⟩ + rw [hsd, hkeep y hyG] at ho; exact absurd ho (hnoOvl y hyG) + · rcases hy' with rfl | hy2 + · have hxG := List.mem_flatten.2 ⟨c1, hc1, hx1⟩ + rw [hsd, hkeep x hxG] at ho; exact absurd ho.symm (hnoOvl x hxG) + · have hxG := List.mem_flatten.2 ⟨c1, hc1, hx1⟩ + have hyG := List.mem_flatten.2 ⟨c2, hc2, hy2⟩ + rw [hkeep x hxG, hkeep y hyG] at ho + have := hF.conn c1 hc1 c2 hc2 x hx1 y hy2 ho + subst this; rfl + · -- chunk + intro ch hch i j k y z y' hi hj hk hij hjk hkey hne hnf + obtain ⟨c0, hc0, rfl⟩ := List.mem_map.1 hch + by_cases hss : ss ∈ c0 + · have hc0Nd : c0.Nodup := nodup_of_mem_flattenA hnd hc0 + obtain ⟨a, b, hc0Eq, hinsEq⟩ := insA_splitA (y := sd) hss hc0Nd + rw [hinsEq] at hi hj hk + have hsdNot : sd ∉ a ++ ss :: b := fun hm => hdG (List.mem_flatten.2 ⟨c0, hc0, hc0Eq.symm ▸ hm⟩) + have hne_y_sd : y ≠ sd := by + intro e; subst y + have hy'G : y' ∈ G.flatten := by + have hy'm := List.mem_of_getElem? hk + have hy'ne : y' ≠ sd := by + intro e'; subst y' + have h1 := idx_eq_of_getElem_sdA hsdNot hi + have h2 := idx_eq_of_getElem_sdA hsdNot hk + omega + simp only [List.mem_append, List.mem_cons] at hy'm + rcases hy'm with h | rfl | rfl | h + · exact List.mem_flatten.2 ⟨c0, hc0, hc0Eq.symm ▸ List.mem_append_left _ h⟩ + · exact hsG + · exact absurd rfl hy'ne + · exact List.mem_flatten.2 ⟨c0, hc0, hc0Eq.symm ▸ List.mem_append_right _ (List.mem_cons_of_mem _ h)⟩ + rw [hsd, hkeep y' hy'G] at hkey + exact hnoKey y' hy'G hkey.symm + have hne_y'_sd : y' ≠ sd := by + intro e; subst e + have hyG : y ∈ G.flatten := by + have hym := List.mem_of_getElem? hi + simp only [List.mem_append, List.mem_cons] at hym + rcases hym with h | rfl | rfl | h + · exact List.mem_flatten.2 ⟨c0, hc0, hc0Eq.symm ▸ List.mem_append_left _ h⟩ + · exact hsG + · exact absurd rfl hne_y_sd + · exact List.mem_flatten.2 ⟨c0, hc0, hc0Eq.symm ▸ List.mem_append_right _ (List.mem_cons_of_mem _ h)⟩ + rw [hsd, hkeep y hyG] at hkey + exact hnoKey y hyG hkey + have hmap : ∀ (t : Nat) (w : U64), (a ++ ss :: sd :: b)[t]? = some w → w ≠ sd → + c0[if t < a.length + 1 then t else t - 1]? = some w ∧ t ≠ a.length + 1 := by + intro t w ht hwne + have htne : t ≠ a.length + 1 := by + intro e; subst e + have : (a ++ ss :: sd :: b)[a.length + 1]? = some sd := by + rw [show a ++ ss :: sd :: b = (a ++ [ss]) ++ sd :: b by simp [List.append_assoc], + List.getElem?_append_right (by simp)] + simp + rw [this] at ht; exact hwne (Option.some.inj ht).symm + refine ⟨?_, htne⟩ + rw [hc0Eq] + by_cases hlt : t < a.length + 1 + · rw [if_pos hlt] + have h1 : (a ++ ss :: sd :: b)[t]? = (a ++ [ss])[t]? := by + rw [show a ++ ss :: sd :: b = (a ++ [ss]) ++ sd :: b by simp [List.append_assoc], + List.getElem?_append_left (by simp; omega)] + have h2 : (a ++ ss :: b)[t]? = (a ++ [ss])[t]? := by + rw [show a ++ ss :: b = (a ++ [ss]) ++ b by simp [List.append_assoc], + List.getElem?_append_left (by simp; omega)] + rw [h2, ← h1]; exact ht + · rw [if_neg hlt] + have hgt : a.length + 2 ≤ t := by omega + have h1 : (a ++ ss :: sd :: b)[t]? = b[t - (a.length + 2)]? := by + rw [show a ++ ss :: sd :: b = (a ++ [ss, sd]) ++ b by simp [List.append_assoc], + List.getElem?_append_right (by simp; omega)] + simp + have h2 : (a ++ ss :: b)[t - 1]? = b[t - (a.length + 2)]? := by + rw [show a ++ ss :: b = (a ++ [ss]) ++ b by simp [List.append_assoc], + List.getElem?_append_right (by simp; omega)] + simp [show t - 1 - (a.length + 1) = t - (a.length + 2) by omega] + rw [h2, ← h1]; exact ht + obtain ⟨hi0, hine⟩ := hmap i y hi hne_y_sd + obtain ⟨hk0, hkne⟩ := hmap k y' hk hne_y'_sd + have hyG : y ∈ G.flatten := List.mem_flatten.2 ⟨c0, hc0, List.mem_of_getElem? hi0⟩ + have hy'G : y' ∈ G.flatten := List.mem_flatten.2 ⟨c0, hc0, List.mem_of_getElem? hk0⟩ + rw [hkeep y hyG, hkeep y' hy'G] at hkey + rw [hkeep y hyG] at hne hnf ⊢ + by_cases hz_sd : z = sd + · subst z + have hj_eq : j = a.length + 1 := idx_eq_of_getElem_sdA hsdNot hj + have hss_at : c0[a.length]? = some ss := by + rw [hc0Eq, List.getElem?_append_right le_rfl]; simp + by_cases hia : i = a.length + · subst hia + rw [if_pos (by omega)] at hi0 + rw [hss_at] at hi0; cases Option.some.inj hi0 + exact absurd hssUt hne + · have hiLt : i < a.length := by omega + have hkGt : a.length < (if k < a.length + 1 then k else k - 1) := by + rw [if_neg (by omega)]; omega + rw [if_pos (by omega)] at hi0 + have hch := hF.chunk c0 hc0 i a.length (if k < a.length + 1 then k else k - 1) y ss y' + hi0 hss_at hk0 hiLt hkGt hkey hne hnf + exact absurd (hssUt.symm.trans hch) hne.symm + · obtain ⟨hj0, hjne⟩ := hmap j z hj hz_sd + have hzG : z ∈ G.flatten := List.mem_flatten.2 ⟨c0, hc0, List.mem_of_getElem? hj0⟩ + rw [hkeep z hzG] + have h1 : (if i < a.length + 1 then i else i - 1) < (if j < a.length + 1 then j else j - 1) := by + split_ifs <;> omega + have h2 : (if j < a.length + 1 then j else j - 1) < (if k < a.length + 1 then k else k - 1) := by + split_ifs <;> omega + exact hF.chunk c0 hc0 _ _ _ y z y' hi0 hj0 hk0 h1 h2 hkey hne hnf + · rw [insA_of_not_mem hss] at hi hj hk + have hyG := List.mem_flatten.2 ⟨c0, hc0, List.mem_of_getElem? hi⟩ + have hzG := List.mem_flatten.2 ⟨c0, hc0, List.mem_of_getElem? hj⟩ + have hy'G := List.mem_flatten.2 ⟨c0, hc0, List.mem_of_getElem? hk⟩ + rw [hkeep y hyG, hkeep y' hy'G] at hkey + rw [hkeep y hyG] at hne hnf ⊢ + rw [hkeep z hzG] + exact hF.chunk c0 hc0 i j k y z y' hi hj hk hij hjk hkey hne hnf + · -- key + intro x hx y hy hne ho hzx hzy hnf + rcases (hmem_ins x).1 hx with rfl | hx0 + · rcases (hmem_ins y).1 hy with rfl | hy0 + · exact absurd rfl hne + · rw [hsd, hkeep y hy0] at ho; exact absurd ho (hnoOvl y hy0) + · rcases (hmem_ins y).1 hy with rfl | hy0 + · rw [hsd, hkeep x hx0] at ho; exact absurd ho.symm (hnoOvl x hx0) + · rw [hkeep x hx0] at ho hzx hnf ⊢ + rw [hkeep y hy0] at ho hzy hnf ⊢ + exact hF.key x hx0 y hy0 hne ho hzx hzy hnf + · -- zfin + intro x hx y hy hne hzx ho + rcases (hmem_ins x).1 hx with rfl | hx0 + · rw [hsd] at hzx; cases hzx + · rcases (hmem_ins y).1 hy with rfl | hy0 + · rw [hsd, hkeep x hx0] at ho; exact absurd ho.symm (hnoOvl x hx0) + · rw [hkeep x hx0] at hzx ho + rw [hkeep y hy0] at ho + exact hF.zfin x hx0 y hy0 hne hzx ho + +set_option maxHeartbeats 2000000 in +/-- The 5-step `MakePool` perform tail refines `setThreadState .restart` + +`performAsidControlInvocation (.makePool (specPtr frame) dest parent (asidOfA base))`, +preserving `JAtZAX` at `Z' = { Z with Pp := insert frame Z.Pp, G := G' }`, `TreeA` on `OS`, and `CarryA`. -/ +theorem makePool_perform_jA (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) + (hT : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s)) + (hYAs : ∀ y ∈ Z.Y, ∀ j : U64, j.val < 512 → ¬ AsW (L.ksb s) (peAF y j)) + (hTr : TreeA st OS Z.G.chains Z.G.par s) + (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) + (hcO : ∀ y ∈ OS, obj.sext48 y = ok y) + {parent dest : Fl4SpecA64.CslotPtr} {frame fi i base : U64} + (hAp : AbsSlotA Z.th parent) (hAd : AbsSlotA Z.th dest) + (hcp : capA st s (cslotAddrA parent) = .Untyped frame 12#u64 fi false) + (hnc : cdt.ensure_no_children st s (cslotAddrA parent) = ok (core.result.Result.Ok ())) + (hcd : capA st s (cslotAddrA dest) = .Null) + (hi : i.val < 128) (hfree : L.mem s (atAF (L.ksb s) i) = 0#u64) + (hbase : base.val = i.val * 512) (hD : DeadDrop4KA Z H frame fi) : + (do + let s1 ← sched.set_thread_state st s (L.curW s) sched.TS_RESTART + let s2 ← obj.Slot.set_cap st (cslotAddrA parent) s1 (.Untyped frame 12#u64 4096#u64 false) + let s3 ← st.clear_free s2 frame 4096#u64 + let s4 ← cdt.insert st s3 (.AsidPool frame base) (cslotAddrA parent) (cslotAddrA dest) + let s5 ← vspace.set_asid_table st s4 i frame + ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s5, a)) ⦃ res => + ∃ σ1, Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ∃ σ' rv, (.ok rv, σ') ∈ ((Fl4SpecA64.fl4ArchPerformInvocation (Z := Unit) + (.spec (.invokeASIDControl (.makePool (specPtr frame) dest parent (asidOfA base))))).run σ1).1 ∧ + ∃ Z' : XSetsA, + res = (.Ok (), res.2.1, a) ∧ + Z'.Pp = insert frame Z.Pp ∧ + JAtZAX (L := L) AR Z' res.2.1 a σ' m ∧ + TreeA st OS Z'.G.chains Z'.G.par res.2.1 ∧ + (CarryA Z (L.mem s) σ → CarryA Z' (L.mem res.2.1) σ') ⦄ := by + have hO := E.sys.ent.os + have hRev := E.sys.sysl.sys.stz.1.2.1 + have hpS := hAp.isSlot E.j.1.place + have hdS := hAd.isSlot E.j.1.place + have hpO : cslotAddrA parent ∈ OS := RS.sub _ hpS + have hdO : cslotAddrA dest ∈ OS := RS.sub _ hdS + have hwa0 : ∀ y : U64, wa y 0#u64 = y := fun y => + UScalar.eq_of_val_eq (by rw [wa_valP, show (0#u64 : U64).val = 0 from rfl, Nat.add_zero, Nat.mod_eq_of_lt (by simpa using y.hBounds)]) + have hksDisj : (L.ksb s).val + ksSizeA ≤ frame.val ∨ frame.val + 4096 ≤ (L.ksb s).val := by + have hks := (E.sys.sysl.sys.stz.2.2 _ hpO frame 12#u64 fi hcp).2 + have h12 : (12#u64 : U64).val = 12 := rfl + rw [h12] at hks + unfold ksSizeA + rcases hks with h | h <;> [left; right] <;> omega + have hfrAl : frame.val % 4096 = 0 := by + have h1 := (E.sys.sysl.sys.ur _ hpO frame 12#u64 fi false hcp).1.1 + have h12 : (12#u64 : U64).val = 12 := rfl + rw [h12] at h1; exact h1 + have hfrAl8 : Aligned frame := by unfold Aligned; omega + have hfrFit : frame.val + 4096 ≤ 2 ^ 64 := by + have hfl : frame.val < 2 ^ 64 := by simpa using frame.hBounds + omega + have hfrCan : CapRt.Canonical frame := by + have w := CapRt.decodeF_encWf (L.mem s (cslotAddrA parent)).bv (L.mem s (wa (cslotAddrA parent) 8#u64)).bv + have e : CapRt.decodeF (L.mem s (cslotAddrA parent)).bv (L.mem s (wa (cslotAddrA parent) 8#u64)).bv = + .Untyped frame 12#u64 fi false := hcp + rw [e] at w; exact w.1 + have hfrNz : frame ≠ 0#u64 := by + have hpos : 0 < frame.val := E.sys.sysl.ly.ut0 _ hpO frame 12#u64 fi hcp + intro h; rw [h] at hpos; simp at hpos + have hdisj : ∀ p bytes, JointObjectA Z H p bytes → p.val + bytes ≤ frame.val ∨ frame.val + 4096 ≤ p.val := + fun p bytes hp => by + exact E.objAlloc.disjoint_of_drop hpO hcp hD hp + -- Step 1: `sched.set_thread_state TS_RESTART` + obtain ⟨Rp, Nt, extra, sp, hPpl⟩ := RS.pl + obtain ⟨xT, hkT, hst, hset, E1, -, hOS1, -, -⟩ := E.restartA hPpl RS.sym RS.run RS.cur RS.vs RS.vc + rw [hst, bind_ok] + set s1 := L.wr s (stA (L.curW s)) sched.TS_RESTART + set σ1 := stUpdA σ σ.curThread xT .restart + have hTr1 : TreeA st OS Z.G.chains Z.G.par s1 := treeA_of_mem hTr hOS1 + have hks1 : L.ksb s1 = L.ksb s := L.ksb_wr _ _ _ + have hcpu1 : L.cpu s1 = L.cpu s := L.cpu_wr _ _ _ + have hcp1 : capA st s1 (cslotAddrA parent) = .Untyped frame 12#u64 fi false := by + rw [hOS1.capA hpO, hcp] + have hcd1 : capA st s1 (cslotAddrA dest) = .Null := by + rw [hOS1.capA hdO, hcd] + have hnul1 : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s1 y = .Null := fun y hy hn => by + rw [hOS1.capA hy]; exact hnul y hy hn + have hnd1 : ∀ y, ¬ Fl4Proofs.P3.Mdb.Desc Z.G.par y (cslotAddrA parent) := + cdt.ensure_no_children.noDesc hO hTr hpO hcp (hRev _ hpO (by rw [hcp]; rfl)) hnc + -- Spec step 2a: `deleteObjects (specPtr frame) pageBits` + have hspAl8 : (specPtr frame).toNat % 8 = 0 := by + have := specPtr_mod frame (n := 3) (by omega) + simpa [show 2 ^ 3 = 8 by decide, show frame.val % 8 = 0 by omega] using this + obtain ⟨ms', hdel⟩ := deleteObjects_exA (specPtr frame) Fl4SpecA64.pageBits hspAl8 σ1 + set Dset : Set Fl4SpecA64.ObjRef := {q | specPtr frame ≤ q ∧ q ≤ specPtr frame + 2 ^ Fl4SpecA64.pageBits - 1} + set σ2 : Fl4SpecA64.State Unit := Fl4SpecA64.detype Dset { σ1 with machineState := ms' } + have hdet_inv : ∀ q ko, σ2.kheap q = some ko → σ1.kheap q = some ko := fun q ko h => by + by_cases hd : q ∈ Dset + · simp [σ2, Fl4SpecA64.detype, hd] at h + · simpa [σ2, Fl4SpecA64.detype, hd] using h + have hkh2_eq : ∀ p bytes, 0 < bytes → JointObjectA Z H p bytes → σ2.kheap (specPtr p) = σ1.kheap (specPtr p) := by + intro p bytes hb hp + have hnot : specPtr p ∉ Dset := not_mem_detype_of_disjA hb hfrAl hfrFit (hdisj p bytes hp) + simp [σ2, Fl4SpecA64.detype, hnot] + have hkh2_sc : ∀ c ∈ Z.C, σ2.kheap (scRenA m c) = σ1.kheap (scRenA m c) := by + intro c hc + unfold scRenA + by_cases he : specPtr c = m.idleScOf m.curCore + · rw [if_pos he] + have hnot : Fl4SpecA64.idleScPtr ∉ Dset := by + have h := not_mem_detype_of_disjA (p := fl4Ptr Fl4SpecA64.idleScPtr) (bytes := 4096) (by omega) hfrAl hfrFit + (hdisj _ 4096 (Or.inl ⟨rfl, Or.inr (Or.inr (Or.inr (Or.inr rfl)))⟩)) + simpa [Dset, Fl4SpecA64.pageBits, specPtr_fl4Ptr] using h + simp [σ2, Fl4SpecA64.detype, hnot] + · rw [if_neg he] + exact hkh2_eq c (2 ^ H.scBits c) (Nat.pos_of_ne_zero (by positivity)) + (JointObjectA.of_sc (E.scs.symm ▸ hc)) + -- Step 2b: `obj.Slot.set_cap parent (.Untyped frame 12 4096 false)` + have hwUt : CapRt.EncWf (.Untyped frame 12#u64 4096#u64 false) := + ⟨hfrCan, by decide⟩ + set s2 := L.wr (L.wr s1 (cslotAddrA parent) ⟨(CapRt.encodeF (.Untyped frame 12#u64 4096#u64 false)).1⟩) + (wa (cslotAddrA parent) 8#u64) ⟨(CapRt.encodeF (.Untyped frame 12#u64 4096#u64 false)).2⟩ + have hsc_eq : obj.Slot.set_cap st (cslotAddrA parent) s1 (.Untyped frame 12#u64 4096#u64 false) = ok s2 := + set_cap_eq (CapRt.encode_eq _) + rw [hsc_eq, bind_ok] + have hTr2 : TreeA st OS Z.G.chains Z.G.par s2 := setcap_ut_treeA hO hTr1 hpO hcp1 + have hks2 : L.ksb s2 = L.ksb s := by simp [s2, hks1] + have hcpu2 : L.cpu s2 = L.cpu s := by simp [s2, hcpu1] + have hfr2 := setcap_frA hO hpO hwUt hsc_eq + have hcp2 : capA st s2 (cslotAddrA parent) = .Untyped frame 12#u64 4096#u64 false := hfr2.1 + have hko2 : ∀ y ∈ OS, y ≠ cslotAddrA parent → capA st s2 y = capA st s1 y := hfr2.2 + have hne_pd : cslotAddrA dest ≠ cslotAddrA parent := fun e => by rw [e, hcp1] at hcd1; cases hcd1 + have hcd2 : capA st s2 (cslotAddrA dest) = .Null := by rw [hko2 _ hdO hne_pd, hcd1] + -- Spec step 2c: `getCap parent` + `setCap (maxFreeIndexUpdate pcap) parent` on `σ2` + have hR_detype : StateRelA Z.th (L.mem s1) σ2 := by + refine ⟨fun p r hp => ?_, fun p hp => ?_⟩ + · obtain ⟨cs, hk, hw, hs⟩ := E1.j.1.st.cnode p r hp + exact ⟨cs, by rw [hkh2_eq p (ObjKindA.cnode r).size (by simp [ObjKindA.size]) + (Or.inr (Or.inr (Or.inl ⟨.cnode r, hp, rfl⟩)))]; exact hk, hw, hs⟩ + · obtain ⟨t, hk, hs⟩ := E1.j.1.st.tcb p hp + exact ⟨t, by rw [hkh2_eq p ObjKindA.tcb.size (by decide) + (Or.inr (Or.inr (Or.inl ⟨.tcb, hp, rfl⟩)))]; exact hk, hs⟩ + obtain ⟨ap, hgp2, hpr2⟩ := hR_detype.getAbsA hAp + have hap_eq : ap = .untypedCap false (specPtr frame) 12 fi.val := by + have h := hpr2.2; change CapEqA (capA st s1 (cslotAddrA parent)) ap at h + rw [hcp1] at h; exact h.symm + have hmax_eq : Fl4SpecA64.maxFreeIndexUpdate ap = absCapA (.Untyped frame 12#u64 4096#u64 false) := by + rw [hap_eq]; rfl + obtain ⟨ko3, hset3, hR3, hSS3⟩ := StateRelA.setAbsA (mem' := L.mem s2) hR_detype (hks1 ▸ E1.j.1.place) hAp + (a := Fl4SpecA64.maxFreeIndexUpdate ap) + (by + show CapRelA (capA st s2 _) _ + rw [hcp2, hmax_eq] + have hwf : CapWfA (.Untyped frame 12#u64 4096#u64 false) := by + have h := hpr2.1; change CapWfA (capA st s1 (cslotAddrA parent)) at h + rw [hcp1] at h; exact h + exact ⟨hwf, CapEqA.of_abs rfl⟩) + (fun y hy hne => hko2 y (RS.sub y hy) hne) + set σ3 : Fl4SpecA64.State Unit := setObjA σ2 parent.1 ko3 + -- Step 3: `st.clear_free s2 frame 4096#u64` (`L.zr s2 frame 4096#u64`) + rw [L.clear_free_eq, bind_ok] + set s3 := L.zr s2 frame 4096#u64 + have hks3 : L.ksb s3 = L.ksb s := by simp [s3, hks2] + have hcpu3 : L.cpu s3 = L.cpu s := by simp [s3, hcpu2] + have hzr_off : ∀ b, Aligned b → ¬ (frame.val ≤ b.val ∧ b.val < frame.val + 4096) → L.mem s3 b = L.mem s2 b := by + intro b hal hoff + have h4096 : (4096#u64 : U64).val = 4096 := rfl + rw [L.mem_zr s2 frame 4096#u64 b hfrAl8 (by decide) (by rw [h4096]; exact hfrFit) hal, + if_neg (by rw [h4096]; exact hoff)] + have hzr_in : ∀ lo : U64, lo.val < 512 → L.mem s3 (peAF frame lo) = 0#u64 := by + intro lo hlo + have hv := peAF_valA hfrFit hlo + have hal : Aligned (peAF frame lo) := by unfold Aligned; omega + have h4096 : (4096#u64 : U64).val = 4096 := rfl + rw [L.mem_zr s2 frame 4096#u64 (peAF frame lo) hfrAl8 (by decide) (by rw [h4096]; exact hfrFit) hal, + if_pos (by rw [h4096]; omega)] + have hzr_slot : ∀ y, IsSlotA Z.th y → ∀ k : U64, k.val < 32 → k.val % 8 = 0 → + L.mem s3 (wa y k) = L.mem s2 (wa y k) := fun y hy k hk hk8 => by + have hal : Aligned (wa y k) := hO.al (RS.sub y hy) hk hk8 + exact hzr_off (wa y k) hal (isSlot_off_frameA hk E.j.1.place hdisj hy) + have hcap3_is : ∀ y, IsSlotA Z.th y → capA st s3 y = capA st s2 y := fun y hy => by + unfold capA + have h0 := hzr_slot y hy 0#u64 (by decide) (by decide) + have h8 := hzr_slot y hy 8#u64 (by decide) (by decide) + rw [hwa0] at h0; rw [h0, h8] + have hcap3_all : ∀ y ∈ OS, capA st s3 y = capA st s2 y := fun y hy => by + by_cases his : IsSlotA Z.th y + · exact hcap3_is y his + · have h2nul : capA st s2 y = .Null := by + have hyx : y ≠ cslotAddrA parent := fun e => his (e ▸ hpS) + rw [hko2 y hy hyx]; exact hnul1 y hy his + rw [h2nul] + have hal0 := hO.al0 hy + have h4096 : (4096#u64 : U64).val = 4096 := rfl + by_cases hin : frame.val ≤ y.val ∧ y.val < frame.val + 4096 + · unfold capA + rw [L.mem_zr s2 frame 4096#u64 y hfrAl8 (by decide) (by rw [h4096]; exact hfrFit) hal0, + if_pos (by rw [h4096]; exact hin)] + rfl + · have hy0 : L.mem s3 y = L.mem s2 y := hzr_off y hal0 hin + unfold capA at h2nul ⊢ + rw [hy0] + have hnull : ∀ w1, CapRt.decodeF (L.mem s2 y).bv w1 = .Null := by + intro w1; unfold CapRt.decodeF at h2nul ⊢ + generalize ((L.mem s2 y).bv >>> 59).toNat = tag at h2nul ⊢ + rcases tag with _ | _ | _ | _ | _ | _ | _ | _ | _ | _ | _ | _ | _ | _ | _ | _ | _ | _ | _ | _ | _ | _ | _ | _ | _ | _ | _ + all_goals first | rfl | cases h2nul | (cases h58 : (L.mem s2 y).bv.getLsbD 58 <;> (simp only [h58, Bool.false_eq_true, ↓reduceIte] at h2nul; cases h2nul)) + exact hnull _ + have hTr3 : TreeA st OS Z.G.chains Z.G.par s3 := by + have hmem3 : ∀ y ∈ Z.G.chains.flatten, L.mem s3 y = L.mem s2 y ∧ L.mem s3 (wa y 8#u64) = L.mem s2 (wa y 8#u64) ∧ + L.mem s3 (wa y 16#u64) = L.mem s2 (wa y 16#u64) ∧ L.mem s3 (wa y 24#u64) = L.mem s2 (wa y 24#u64) := by + intro y hy + have ⟨hyO, hyNe⟩ := (hTr2.lk.members y).1 hy + have hyIs : IsSlotA Z.th y := by + by_contra hn + have hyx : y ≠ cslotAddrA parent := fun e => hn (e ▸ hpS) + rw [hko2 y hyO hyx, hnul1 y hyO hn] at hyNe; exact hyNe rfl + refine ⟨?_, hzr_slot y hyIs 8#u64 (by decide) (by decide), + hzr_slot y hyIs 16#u64 (by decide) (by decide), hzr_slot y hyIs 24#u64 (by decide) (by decide)⟩ + simpa [hwa0] using hzr_slot y hyIs 0#u64 (by decide) (by decide) + refine ⟨⟨hTr2.lk.nodup, hTr2.lk.ne_nil, + fun c hc i a b ha hb => by + have hma := List.mem_flatten.2 ⟨c, hc, List.mem_of_getElem? ha⟩ + have hmb := List.mem_flatten.2 ⟨c, hc, List.mem_of_getElem? hb⟩ + have hch := hTr2.lk.chain c hc i a b ha hb + exact ⟨by unfold NxA at *; rw [(hmem3 _ hma).2.2.2]; exact hch.1, + by rw [(hmem3 _ hmb).2.2.1]; exact hch.2⟩, + fun c hc a ha => by + have hm := List.mem_flatten.2 ⟨c, hc, List.mem_of_head? ha⟩ + rw [(hmem3 _ hm).2.2.1]; exact hTr2.lk.head c hc a ha, + fun c hc a ha => by + have hm := List.mem_flatten.2 ⟨c, hc, List.mem_of_getLast? ha⟩ + unfold NxA; rw [(hmem3 _ hm).2.2.2]; exact hTr2.lk.last c hc a ha, + fun y => by rw [hTr2.lk.members y]; constructor <;> rintro ⟨hy, hn⟩ <;> + exact ⟨hy, by rwa [hcap3_all y hy] at *⟩⟩, + hTr2.tree, ?_⟩ + refine ⟨fun y hy hp => by rw [hcap3_all y hy] at hp ⊢; exact hTr2.ut.reg y hy hp, + fun u hu p b i d hc y hd => by + rw [hcap3_all u hu] at hc; rw [hcap3_all y (desc_in hTr2.parIn hd).1] + exact hTr2.ut.inc u hu p b i d hc y hd, + fun u hu p b i d hc y hy hp => by + rw [hcap3_all u hu] at hc ⊢; rw [hcap3_all y hy] at hp ⊢ + exact hTr2.ut.noov u hu p b i d hc y hy hp, + fun u1 hu1 u2 hu2 hne p b i1 i2 d1 d2 h1 h2 => by + rw [hcap3_all u1 hu1] at h1; rw [hcap3_all u2 hu2] at h2 + exact hTr2.ut.excl u1 hu1 u2 hu2 hne p b i1 i2 d1 d2 h1 h2, + fun u hu p b i d hc => by rw [hcap3_all u hu] at hc; exact hTr2.ut.fib u hu p b i d hc, + fun u hu p b i d hc y hy hyu hp hnd => by + rw [hcap3_all u hu] at hc hnd; rw [hcap3_all y hy] at hp hnd + exact hTr2.ut.drange u hu p b i d hc y hy hyu hp hnd, + fun u y hd hu => by + have ⟨hyO, huO⟩ := desc_in hTr2.parIn hd + rw [hcap3_all y hyO] at hu; rw [hcap3_all u huO] + exact hTr2.ut.anc u y hd hu⟩ + -- Spec step 3b: `retypeRegion (specPtr frame) 1 0 (.archObject .asidPoolObj) false` on `σ3` + set σ4 : Fl4SpecA64.State Unit := + { σ3 with kheap := Function.update σ3.kheap (specPtr frame) (some (.archObj (.asidPool fun _ => none))) } + have hret4 := retypeRegion_asidPool_memA (specPtr frame) σ3 + have hkh4_ne : ∀ p bytes, 0 < bytes → JointObjectA Z H p bytes → specPtr p ≠ specPtr frame := by + intro p bytes hb hp e + have hp_eq : p = frame := specPtr_inj e + subst hp_eq + rcases hdisj p bytes hp with h | h <;> omega + have hR4 : StateRelA Z.th (L.mem s3) σ4 := by + refine ⟨fun p r hp => ?_, fun p hp => ?_⟩ + · obtain ⟨cs, hk, hw, hs⟩ := hR3.cnode p r hp + have hne := hkh4_ne p (ObjKindA.cnode r).size (by simp [ObjKindA.size]) + (Or.inr (Or.inr (Or.inl ⟨.cnode r, hp, rfl⟩))) + refine ⟨cs, by simp [σ4, Function.update_of_ne hne, hk], hw, fun bs hbs => ?_⟩ + have hslot : IsSlotA Z.th (slotAddrA (specPtr p) (Fl4SpecA64.ofBl bs)) := by + have hAb : AbsSlotA Z.th (specPtr p, bs) := ⟨p, rfl, Or.inl (by rw [hbs]; exact hp)⟩ + exact hAb.isSlot E.j.1.place + have hc := hcap3_is _ hslot + change capAtA (L.mem s3) _ = capAtA (L.mem s2) _ at hc + unfold SlotRelA; rw [hc]; exact hs bs hbs + · obtain ⟨t, hk, hs⟩ := hR3.tcb p hp + have hne := hkh4_ne p ObjKindA.tcb.size (by decide) (Or.inr (Or.inr (Or.inl ⟨.tcb, hp, rfl⟩))) + refine ⟨t, by simp [σ4, Function.update_of_ne hne, hk], fun bs hbs => ?_⟩ + have hslot : IsSlotA Z.th (slotAddrA (specPtr p) (BitVec.ofNat 64 bs)) := + ⟨p, Or.inr ⟨hp, bs, hbs, rfl⟩⟩ + have hc := hcap3_is _ hslot + change capAtA (L.mem s3) _ = capAtA (L.mem s2) _ at hc + unfold SlotRelA; rw [hc]; exact hs bs hbs + have hcp3 : capA st s3 (cslotAddrA parent) = .Untyped frame 12#u64 4096#u64 false := by + rw [hcap3_is _ hpS, hcp2] + have hcd3 : capA st s3 (cslotAddrA dest) = .Null := by + rw [hcap3_is _ hdS, hcd2] + have hnul3 : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s3 y = .Null := fun y hy hn => by + rw [hcap3_all y hy] + have hyx : y ≠ cslotAddrA parent := fun e => hn (e ▸ hpS) + rw [hko2 y hy hyx]; exact hnul1 y hy hn + have hrv3_slot : ∀ y, IsSlotA Z.th y → L.mem s3 (wa y 24#u64) = L.mem s1 (wa y 24#u64) := fun y hy => by + rw [hzr_slot y hy 24#u64 (by decide) (by decide)] + have ha0 := hO.al0 hpO + have ha8 := hO.al hpO (k := 8#u64) (by simp) (by simp) + have hay := hO.al (RS.sub y hy) (k := 24#u64) (by simp) (by simp) + have hyO := RS.sub y hy + have hfitp := hO.fit _ hpO; have hfity := hO.fit y hyO + have hy24 : (wa y 24#u64).val = y.val + 24 := by + rw [wa_valP, show (24#u64 : U64).val = 24 from rfl, Nat.mod_eq_of_lt (by omega)] + have hp8 : (wa (cslotAddrA parent) 8#u64).val = (cslotAddrA parent).val + 8 := by + rw [wa_valP, show (8#u64 : U64).val = 8 from rfl, Nat.mod_eq_of_lt (by omega)] + have hne8 : (wa (cslotAddrA parent) 8#u64).val ≠ (wa y 24#u64).val := by + rw [hp8, hy24] + by_cases e : cslotAddrA parent = y + · rw [e]; omega + · rcases hO.disj _ hpO y hyO e with hd | hd <;> omega + have hne0 : (cslotAddrA parent).val ≠ (wa y 24#u64).val := by + rw [hy24] + by_cases e : cslotAddrA parent = y + · rw [e]; omega + · rcases hO.disj _ hpO y hyO e with hd | hd <;> omega + rw [L.mem_wr_ne ha8 hay hne8, L.mem_wr_ne ha0 hay hne0] + have hC4 : CdtRelA st Z.th s3 σ4 Z.G := by + have hcdt_eq : σ4.cdt = σ1.cdt ∧ σ4.isOriginalCap = σ1.isOriginalCap := ⟨rfl, rfl⟩ + refine ⟨hTr3.sub (fun y hy => RS.sub y hy) hnul3, + fun sl hsl => by rw [hcdt_eq.1]; exact E1.j.1.cdt.cdt sl hsl, + fun sl q hq => by rw [hcdt_eq.1] at hq; exact E1.j.1.cdt.cdt_slot sl q hq, + fun sl hsl => by + rw [hcdt_eq.2, E1.j.1.cdt.orig sl hsl] + unfold revA; rw [hrv3_slot _ (hsl.isSlot E.j.1.place)], + E1.j.1.cdt.par_slot, + fun a p hp => by + have hpIs : IsSlotA Z.th p := by + have ⟨pa, hpa⟩ := hTr3.tree.par_mem a p hp + have ⟨_, hne⟩ := (hTr3.lk.members p).1 hpa + by_contra hn; exact hne (hnul3 p ((hTr3.lk.members p).1 hpa).1 hn) + unfold revA; rw [hrv3_slot p hpIs]; exact E1.j.1.cdt.prev a p hp⟩ + -- Step 4: `cdt.insert s3 (.AsidPool frame base) parent dest` + have hbaseWf : CapWfA (.AsidPool frame base) := by + change base.val < 2 ^ 16 ∧ base.val % 512 = 0; omega + have hwAp : CapRt.EncWf (.AsidPool frame base) := hfrCan + have hSBK : SameBadgeKindA (.AsidPool frame base) (capA st s3 (cslotAddrA parent)) := by + rw [hcp3]; simp [SameBadgeKindA] + have horig4 : σ4.isOriginalCap parent = true := by + have h1 : σ4.isOriginalCap parent = σ1.isOriginalCap parent := rfl + rw [h1, E1.j.1.cdt.orig parent hAp] + have hr1 : revA (L.mem s1) (cslotAddrA parent) = revA (L.mem s) (cslotAddrA parent) := by + unfold revA; rw [(hOS1 _ hpO).2.2.2] + rw [hr1] + exact revA_of_revocableA (E.sys.sysl.sys.stz.1.2.1 _ hpO (by rw [hcp]; rfl)) + have hInsB : insBA σ4 parent (absCapA (.AsidPool frame base)) (absCapA (capA st s3 (cslotAddrA parent))) = true := by + rw [hcp3] + unfold insBA + rw [horig4] + simp only [absCapA, Fl4SpecA64.shouldBeParentOf, Fl4SpecA64.sameRegionAs, + Fl4SpecA64.isPhysical, Fl4SpecA64.objRefOf, Fl4SpecA64.aobjRef, Fl4SpecA64.ArchCap.acapObj, + Fl4SpecA64.the, Fl4SpecA64.objSize, Fl4SpecA64.archObjSize, Fl4SpecA64.pageBits, + decide_eq_true_eq, true_and, and_true] + have hspAl : (specPtr frame).toNat % 4096 = 0 := by + have := specPtr_mod frame (n := 12) (by omega) + simpa [show 2 ^ 12 = 4096 by decide, hfrAl] using this + have hspLt : (specPtr frame).toNat < 2 ^ 64 := (specPtr frame).isLt + have hspFit : (specPtr frame).toNat + 4096 ≤ 2 ^ 64 := by omega + have h4096 : (specPtr frame + (1 : BitVec 64) <<< 12 - 1).toNat = (specPtr frame).toNat + 4095 := by + have hsub : specPtr frame + (1 : BitVec 64) <<< 12 - 1 = specPtr frame + 4095#64 := by + rw [add_sub_assoc]; rfl + rw [hsub, BitVec.toNat_add, show (4095#64 : BitVec 64).toNat = 4095 from rfl, Nat.mod_eq_of_lt (by omega)] + have hpow1 : (↑(1 <<< 12 : Nat) : BitVec 64) = (1 : BitVec 64) <<< 12 := by decide + have hpow2 : (1 : BitVec 64) <<< (12#u64 : U64).val = (1 : BitVec 64) <<< 12 := rfl + rw [hpow1, hpow2] + refine ⟨rfl, BitVec.le_refl _, ?_, BitVec.le_refl _⟩ + rw [BitVec.le_def, h4096]; omega + -- Track `capInsert` refinement, `TreeA` (via `UtInvA.insK`), and `WritesIn` on `OS_is` + have hsn3 : capA st s3 (cslotAddrA parent) ≠ .Null := by rw [hcp3]; intro h; cases h + have hpG3 : cslotAddrA parent ∈ Z.G.chains.flatten := (hTr3.lk.members _).2 ⟨hpO, hsn3⟩ + have hdG3 : cslotAddrA dest ∉ Z.G.chains.flatten := fun hm => ((hTr3.lk.members _).1 hm).2 hcd3 + have hpd3 : cslotAddrA parent ≠ cslotAddrA dest := fun e => hdG3 (e ▸ hpG3) + have hne_slot : parent ≠ dest := fun e => hpd3 (by rw [e]) + set OS_is : Set U64 := {y ∈ OS | IsSlotA Z.th y} + have hO_is : OSOK H.tcbs {b | InOS OS_is b} ∅ OS_is := + ⟨fun x hx => by + have hfit := hO.fit x hx.1 + refine ⟨(hO.slot x hx.1).1, fun o ho _ => Or.inr ⟨⟨x, hx, ?_, ?_⟩, Set.notMem_empty _⟩⟩ + · rw [wa_valP, Nat.mod_eq_of_lt (by omega)]; omega + · rw [wa_valP, Nat.mod_eq_of_lt (by omega)]; omega, + fun x hx => hO.fit x hx.1, fun x hx y hy => hO.disj x hx.1 y hy.1, fun x hx => hO.nz x hx.1⟩ + have hInsAll : cdt.insert st s3 (.AsidPool frame base) (cslotAddrA parent) (cslotAddrA dest) ⦃ s4 => + (∃ σ5, ((), σ5) ∈ (Fl4SpecA64.capInsert (Z := Unit) (absCapA (.AsidPool frame base)) parent dest σ4).1 ∧ + StateRelA Z.th (L.mem s4) σ5 ∧ + CdtRelA st Z.th s4 σ5 (insGA Z.G (cslotAddrA parent) (cslotAddrA dest) + (insBA σ4 parent (absCapA (.AsidPool frame base)) (absCapA (capA st s3 (cslotAddrA parent))))) ∧ + TreeA st OS (insG (cslotAddrA parent) (cslotAddrA dest) Z.G.chains) + (insGA Z.G (cslotAddrA parent) (cslotAddrA dest) + (insBA σ4 parent (absCapA (.AsidPool frame base)) (absCapA (capA st s3 (cslotAddrA parent))))).par s4 ∧ + SpecSlotA σ4 σ5) ∧ + WritesIn st s3 s4 (W7.CapW H.tcbs {b | InOS OS_is b} ∅) ∧ + (∀ y ∈ OS, y ≠ cslotAddrA dest → capA st s4 y = capA st s3 y) ∧ + capA st s4 (cslotAddrA dest) = .AsidPool frame base ⦄ := by + unfold cdt.insert; rw [capA_eq, bind_ok] + have F3 := hTr3.lk.lkF hO.nz + have inOS3 : ∀ y ∈ Z.G.chains.flatten, y ∈ OS := fun y hy => ((hTr3.lk.members y).1 hy).1 + have inOSis3 : ∀ y ∈ Z.G.chains.flatten, y ∈ OS_is := fun y hy => by + have ⟨hyO, hyNe⟩ := (hTr3.lk.members y).1 hy + refine ⟨hyO, ?_⟩; by_contra hn; exact hyNe (hnul3 y hyO hn) + have hNm3 : NxA st s3 (cslotAddrA parent) ≠ 0#u64 → NxA st s3 (cslotAddrA parent) ∈ Z.G.chains.flatten := + F3.nxm _ hpG3 + have hNd3 : NxA st s3 (cslotAddrA parent) ≠ cslotAddrA dest := fun e => by + have h1 : NxA st s3 (cslotAddrA parent) ≠ 0#u64 := fun e0 => hO.nz _ hdO (e ▸ e0) + exact hdG3 (e ▸ hNm3 h1) + have hlr0 := cdt.insert_with_cap.lr hO (LR.self s3) hpO hdO hpd3 (hcO _ hdO) rfl hsn3 hwAp + (fun h => inOS3 _ (hNm3 h)) + have hlr := cdt.insert_with_cap.lrF hO_is (LR.self s3) ⟨hpO, hpS⟩ ⟨hdO, hdS⟩ hpd3 (hcO _ hdO) rfl hsn3 hwAp + (fun h => inOSis3 _ (hNm3 h)) + have hwk := insWC_walkA hO hpO hdO hpd3 rfl hwAp (fun h => inOS3 _ (hNm3 h)) + obtain ⟨asc, hgs, hsrel⟩ := hR4.getAbsA hAp + obtain ⟨adc, hgd, hdrel⟩ := hR4.getAbsA hAd + have hadc : adc = .nullCap := by + have h := hdrel.2; change CapEqA (capA st s3 (cslotAddrA dest)) adc at h + rw [hcd3] at h; exact h.symm + subst hadc + have hasc : asc = absCapA (capA st s3 (cslotAddrA parent)) := by + have h := hsrel.2; change CapEqA (capA st s3 (cslotAddrA parent)) asc at h + rw [hcp3] at h ⊢; exact h.symm + subst hasc + have hP3 : PlaceA Z.th (L.ksb s3) := hks3 ▸ E.j.1.place + have hnotFull : ¬ FullCondA (capA st s3 (cslotAddrA parent)) (.AsidPool frame base) := by + rw [hcp3]; unfold FullCondA; simp + have hv : revFA (.AsidPool frame base) (capA st s3 (cslotAddrA parent)) = + decide (Fl4SpecA64.isCapRevocable (absCapA (.AsidPool frame base)) (absCapA (capA st s3 (cslotAddrA parent)))) := by + rw [Bool.eq_iff_iff]; simp only [decide_eq_true_eq]; exact revFA_spec _ _ hSBK hbaseWf + refine WP.spec_mono (WP.spec_and (WP.spec_and hlr hlr0) hwk) ?_ + rintro s4 ⟨⟨h1, ⟨hL, ed, -, ns, ek, -⟩⟩, hb, hdb, -, hnf⟩ + have hkeep4 : ∀ y ∈ OS, y ≠ cslotAddrA dest → capA st s4 y = capA st s3 y := fun y hy hne => by + by_cases e : y = cslotAddrA parent + · rw [e]; exact hnf hnotFull + · exact ek y hy e hne + have hLk4 : MdbLk st OS (insG (cslotAddrA parent) (cslotAddrA dest) Z.G.chains) s4 := by + refine hTr3.lk.ins hO.nz hpG3 hdO hcd3 (by rw [ed]; intro h; cases h) + (fun y hy hyd => by rw [hkeep4 y hy hyd]) + (by rw [(hL _ hpO).2]; simp) + (by rw [(hL _ hdO).1]; simp [hNd3.symm]) + (by rw [(hL _ hdO).2]; simp [hpd3.symm]) + (fun hN => by rw [(hL _ (inOS3 _ (hNm3 hN))).1]; simp [hN]) + (fun y hy h1 h2 => by rw [(hL y hy).2]; simp [h1, h2]) + (fun y hy h1 h2 => by rw [(hL y hy).1]; simp [h1, h2]) + have hUt4 : UtInvA st OS (Function.update Z.G.par (cslotAddrA dest) (some (cslotAddrA parent))) s4 := by + refine UtInvA.insK hTr3.ut hTr3.parIn hpO hdO hpd3 + (fun z e => hdG3 (hTr3.tree.par_mem z _ e).2) + (by cases e : Z.G.par (cslotAddrA dest) with + | none => rfl + | some q => exact absurd (hTr3.tree.par_mem _ q e).1 hdG3) + hkeep4 hcp3 (by decide) + (by rw [ed]; exact physA_ap _ _) + (by rw [ed]; rfl) + (by rw [ed, hcp3]; unfold baseA; rw [ptrA_ap, ptrA_ut]) + (by rw [ed, hcp3]; unfold topA; rw [ptrA_ap, ptrA_ut, sizeA_ap, sizeA_ut]; simp [fl4_abi.consts.bits.ASID_POOL]) + hnd1 + have hT4 : TreeA st OS (insG (cslotAddrA parent) (cslotAddrA dest) Z.G.chains) + (insGA Z.G (cslotAddrA parent) (cslotAddrA dest) + (insBA σ4 parent (absCapA (.AsidPool frame base)) (absCapA (capA st s3 (cslotAddrA parent))))).par s4 := by + rw [hInsB]; simp only [insGA, ite_true] + exact ⟨hLk4, by rw [insG_eq]; exact hTr3.tree.insert hpG3 hdG3 (Or.inl rfl), hUt4⟩ + have hsetUt4 : ((), σ4) ∈ (Fl4SpecA64.setUntypedCapAsFull (Z := Unit) + (absCapA (capA st s3 (cslotAddrA parent))) (absCapA (.AsidPool frame base)) parent σ4).1 := by + unfold Fl4SpecA64.setUntypedCapAsFull + rw [if_neg (fun h => hnotFull ((utCondA_iff _ _).1 h))] + exact Set.mem_singleton _ + obtain ⟨ko5, hset5, hR5, hSS5⟩ := StateRelA.setAbsA (mem' := L.mem s4) hR4 hP3 hAd + (a := absCapA (.AsidPool frame base)) + (by show CapRelA (capA st s4 _) _; rw [ed]; exact ⟨hbaseWf, CapEqA.of_abs rfl⟩) + (fun y hy hne => hkeep4 y (RS.sub y hy) hne) + have hmem5 := capInsert_memA hgs hgd hsetUt4 hset5 + let σ5 := insFinA (setObjA σ4 dest.1 ko5) parent dest + (insBA σ4 parent (absCapA (.AsidPool frame base)) (absCapA (capA st s3 (cslotAddrA parent)))) + (decide (Fl4SpecA64.isCapRevocable (absCapA (.AsidPool frame base)) + (absCapA (capA st s3 (cslotAddrA parent))))) + have hC5 : CdtRelA st Z.th s4 σ5 (insGA Z.G (cslotAddrA parent) (cslotAddrA dest) + (insBA σ4 parent (absCapA (.AsidPool frame base)) (absCapA (capA st s3 (cslotAddrA parent))))) := by + refine CdtRelA.insertA hP3 hC4 hAp hAd _ (decide (Fl4SpecA64.isCapRevocable (absCapA (.AsidPool frame base)) + (absCapA (capA st s3 (cslotAddrA parent))))) ?_ (by simp [σ5, insFinA, setObjA]) (by simp [σ5, insFinA, setObjA]) ?_ hne_slot + (fun h => by unfold insBA at h; exact (of_decide_eq_true h).1) + (fun sl h => hdG3 (hC4.tree.tree.par_mem _ _ + (by have e := hC4.cdt sl (hC4.cdt_slot sl _ h).1; rw [h] at e; rw [← e]; rfl)).2) + · refine hT4.sub (fun y hy => RS.sub y hy) fun y hy hn' => ?_ + have f2 : y ≠ cslotAddrA dest := fun e => hn' (e ▸ hdS) + rw [hkeep4 y hy f2]; exact hnul3 y hy hn' + · intro sl hsl + by_cases e : sl = dest + · rw [if_pos e, e, ← hv]; exact revA_of_val hdb + · rw [if_neg e] + exact revA_of_keep (hb _ (RS.sub _ (hsl.isSlot hP3)) (fun h => e (cslotAddrA_inj hP3 hsl hAd h))) + exact ⟨⟨_, hmem5, hR5.of_kheap rfl, hC5, hT4, hSS5.trans (SpecSlotA.insFin _ _ _ _ _)⟩, + h1.1, hkeep4, ed⟩ + refine WP.spec_bind hInsAll fun s4 ⟨⟨σ5, hins5, hR5, hC5, hT5, hSS5⟩, hw4, hkeep4, hsd4⟩ => ?_ + -- Step 5: `vspace.set_asid_table s4 i frame` + have hks4 : L.ksb s4 = L.ksb s := hw4.1.trans hks3 + have hcpu4 : L.cpu s4 = L.cpu s := hw4.2.1.trans hcpu3 + rw [vspace.set_asid_table.eq, hks4, bind_ok] + have hAtEq : wa (L.ksb s) (AtOff i) = atAF (L.ksb s) i := by + unfold AtOff atAF + have hm : i &&& 127#u64 = i := UScalar.eq_of_val_eq (by rw [and_maskA _ 7 _ rfl]; exact Nat.mod_eq_of_lt hi) + rw [hm] + rw [hAtEq] + set s5 := L.wr s4 (atAF (L.ksb s) i) frame + set hiBv : Fl4SpecA64.AsidHighIndex := BitVec.ofNat 7 i.val + set σ6 : Fl4SpecA64.State Unit := + { σ5 with archState := { σ5.archState with + armAsidTable := Function.update σ5.archState.armAsidTable hiBv (some (specPtr frame)) } } + have ⟨hlow0, hhiEq, _⟩ := asid_high_low_of_shl9A hi hbase + -- Spec perform membership + have hperf : (.ok ([] : List Fl4SpecA64.Data), σ6) ∈ + ((Fl4SpecA64.fl4ArchPerformInvocation (Z := Unit) + (.spec (.invokeASIDControl (.makePool (specPtr frame) dest parent (asidOfA base))))).run σ1).1 := by + unfold Fl4SpecA64.fl4ArchPerformInvocation Fl4SpecA64.archPerformInvocation + Fl4SpecA64.performAsidControlInvocation + dsimp only + refine mem_liftED ?_ + refine ⟨((), σ6), ?_, Set.mem_singleton _⟩ + refine ⟨((), σ2), hdel, ?_⟩ + refine ⟨(ap, σ2), hgp2, ?_⟩ + refine ⟨((), σ3), hset3, ?_⟩ + refine ⟨([specPtr frame], σ4), hret4, ?_⟩ + refine ⟨((), σ5), hins5, ?_⟩ + refine ⟨((), σ5), by simp [Fl4Spec.NonDet.assert, hlow0], ?_⟩ + refine ⟨(σ5.archState.armAsidTable, σ5), Set.mem_singleton _, ?_⟩ + refine ⟨(Function.update σ5.archState.armAsidTable hiBv (some (specPtr frame)), σ5), + by rw [hhiEq]; exact Set.mem_singleton _, Set.mem_singleton _⟩ + -- Memory preservation across `s1 → s2 → s3 → s4 → s5` + have hksFit : (L.ksb s).val + 788736 ≤ 2 ^ 64 := E.j.1.ksFit + have hAtAl : Aligned (atAF (L.ksb s) i) := + FootNA.aligned E.j.1 (bxi_nodes E.sys.ent.bxi) E.scAl E.ap.vcAl (atAF (L.ksb s) i) (.vs _ (.asid i hi)) + have hAtWr : L.mem s5 (atAF (L.ksb s) i) = frame := L.mem_wr_same hAtAl + have hAtOffOS : ∀ y ∈ OS, ∀ k : U64, k.val < 32 → (atAF (L.ksb s) i).val ≠ (wa y k).val := fun y hy k hk e => by + have hyw : (wa y k).val = y.val + k.val := by rw [wa_valP]; have := hO.fit y hy; omega + exact (E.ap.vs _ (.asid i hi)).1 ⟨y, hy, by omega, by omega⟩ + have hcap5_eq : ∀ y ∈ OS, capA st s5 y = capA st s4 y := fun y hy => by + unfold capA + have ha0 := hO.al0 hy + have ha8 := hO.al hy (k := 8#u64) (by simp) (by simp) + rw [L.mem_wr_ne hAtAl ha0 (by simpa [hwa0] using hAtOffOS y hy 0#u64 (by decide)), + L.mem_wr_ne hAtAl ha8 (hAtOffOS y hy 8#u64 (by decide))] + have hTr5 : TreeA st OS (insG (cslotAddrA parent) (cslotAddrA dest) Z.G.chains) + (insGA Z.G (cslotAddrA parent) (cslotAddrA dest) + (insBA σ4 parent (absCapA (.AsidPool frame base)) (absCapA (capA st s3 (cslotAddrA parent))))).par s5 := by + refine treeA_of_mem hT5 fun y hy => ?_ + have ha0 := hO.al0 hy + have ha8 := hO.al hy (k := 8#u64) (by simp) (by simp) + have ha16 := hO.al hy (k := 16#u64) (by simp) (by simp) + have ha24 := hO.al hy (k := 24#u64) (by simp) (by simp) + exact ⟨L.mem_wr_ne hAtAl ha0 (by simpa [hwa0] using hAtOffOS y hy 0#u64 (by decide)), + L.mem_wr_ne hAtAl ha8 (hAtOffOS y hy 8#u64 (by decide)), + L.mem_wr_ne hAtAl ha16 (hAtOffOS y hy 16#u64 (by decide)), + L.mem_wr_ne hAtAl ha24 (hAtOffOS y hy 24#u64 (by decide))⟩ + -- Every `FootNA` word except `atAF (L.ksb s) i` keeps its `s1` value in `s5`! + have hfn_s5 : ∀ b, FootNA (L := L) Z s1 b → b ≠ atAF (L.ksb s) i → L.mem s5 b = L.mem s1 b := by + intro b hb hneAt + have halB : Aligned b := FootNA.aligned E1.j.1 + (fun c hc o ho => by rw [nodeAt_ksb hks1]; exact bxi_nodes E.sys.ent.bxi c hc o ho) + E.scAl E.ap.vcAl b hb + have hb0 : FootNA (L := L) Z s b := by + have hna := nodeAt_ksb hks1 + cases hb with + | node c _ hc ho => simp only [hna] at ho; exact .node c _ hc ho + | cur _ ho => simp only [hcpu1, hna] at ho; exact .cur _ ho + | cur2 _ ho => simp only [hcpu1, hna] at ho; exact .cur2 _ ho + | tcb y _ hy ho => exact .tcb y _ hy ho + | tcb2 y _ hy ho => exact .tcb2 y _ hy ho + | sc c hc => exact .sc c hc + | sc2 c hc => exact .sc2 c hc + | irq i hi => rw [hks1] at *; exact .irq i hi + | glob _ ho => rw [hks1] at ho; exact .glob _ ho + | vcpu v _ hv hw => exact .vcpu v _ hv hw + | vs _ h => rw [hks1] at h; exact .vs _ h + | ext _ h => exact .ext _ h + have hnotCap : ¬ W7.CapW H.tcbs {b | InOS OS b} ∅ b := footN_offA E b hb0 + have hnotCapIs : ¬ W7.CapW H.tcbs {b | InOS OS_is b} ∅ b := fun hc => + hnotCap (hc.imp id fun ⟨⟨y, hy, h1, h2⟩, h3⟩ => ⟨⟨y, hy.1, h1, h2⟩, h3⟩) + have h54 : L.mem s5 b = L.mem s4 b := L.mem_wr_ne hAtAl halB (fun e => hneAt.symm (UScalar.eq_of_val_eq e)) + have h43 : L.mem s4 b = L.mem s3 b := hw4.2.2 b halB hnotCapIs + have h32 : L.mem s3 b = L.mem s2 b := by + refine hzr_off b halB (mem_off_frameA hdisj hksDisj E.j.1.ksFit E.j E.scs ?_ ?_ ?_ ?_ ?_ ?_ ?_ hb0) + · intro c hc; obtain ⟨z, hz⟩ := hc + have hfit := E.sys.ent.heap.fits c (.sc z) hz + have hge : 128 ≤ 2 ^ H.scBits c := by + have hb7 := E.sys.ent.heap.scmin c z hz + simp [Heap.scBits, hz, HKind.bits] + calc 128 = 2 ^ 7 := by decide + _ ≤ 2 ^ z := Nat.pow_le_pow_right (by omega) hb7 + exact ⟨by simpa [Heap.scBits, hz, HKind.bits] using hfit, hge⟩ + · have hfrPos : 0 < frame.val := E.sys.sysl.ly.ut0 _ hpO frame 12#u64 fi hcp + omega + · intro p hp + have h1 : p.val % 4096 = 0 := Nat.mod_eq_zero_of_dvd (E.j.1.vs.pal p hp) + have h2 : p.val < 2 ^ 64 := by simpa using p.hBounds + omega + · intro p hp + have h1 : p.val % 4096 = 0 := Nat.mod_eq_zero_of_dvd (E.j.1.vs.al p hp) + have h2 : p.val < 2 ^ 64 := by simpa using p.hBounds + omega + · intro y hy + have h1 : y.val % 8192 = 0 := Nat.mod_eq_zero_of_dvd (E.j.1.vs.yal y hy) + have h2 : y.val < 2 ^ 64 := by simpa using y.hBounds + omega + · intro x hx + have h1 : x.val % 8192 = 0 := Nat.mod_eq_zero_of_dvd (E.j.1.ext.xal x hx) + have h2 : x.val < 2 ^ 64 := by simpa using x.hBounds + omega + · intro t ht + have h1 : t.val % 4096 = 0 := Nat.mod_eq_zero_of_dvd (E.j.1.ext.tal t ht) + have h2 : t.val < 2 ^ 64 := by simpa using t.hBounds + omega + have h21 : L.mem s2 b = L.mem s1 b := by + have ha0 := hO.al0 hpO + have ha8 := hO.al hpO (k := 8#u64) (by simp) (by simp) + have hne0 : (cslotAddrA parent).val ≠ b.val := fun e => + hnotCap (Or.inr ⟨by rw [← UScalar.eq_of_val_eq e]; exact ⟨_, hpO, by omega, by omega⟩, by simp⟩) + have hne8 : (wa (cslotAddrA parent) 8#u64).val ≠ b.val := fun e => + hnotCap (Or.inr ⟨by rw [← UScalar.eq_of_val_eq e]; refine ⟨_, hpO, ?_, ?_⟩ <;> rw [wa_valP, show (8#u64 : U64).val = 8 from rfl] <;> have := hO.fit _ hpO <;> omega, by simp⟩) + rw [L.mem_wr_ne ha8 halB hne8, L.mem_wr_ne ha0 halB hne0] + rw [h54, h43, h32, h21] + -- Also the new pool's 512 words stay `0#u64` in `s5`! + have hpool5 : ∀ lo : U64, lo.val < 512 → L.mem s5 (peAF frame lo) = 0#u64 := by + intro lo hlo + have hv := peAF_valA hfrFit hlo + have hal : Aligned (peAF frame lo) := by unfold Aligned; omega + have hneAt : (atAF (L.ksb s) i).val ≠ (peAF frame lo).val := fun e => by + have h1 := atAF_valA hksFit hi + unfold ksSizeA at hksDisj; omega + have hnotCapIs : ¬ W7.CapW H.tcbs {b | InOS OS_is b} ∅ (peAF frame lo) := by + rintro (⟨t, ht, o, ho, heq⟩ | ⟨⟨y, hy, h1, h2⟩, -⟩) + · have hth : Z.th t = some .tcb := (E.j.1.thT t).2 (E.tcbs ▸ ht) + have hd := hdisj t ObjKindA.tcb.size (Or.inr (Or.inr (Or.inl ⟨.tcb, hth, rfl⟩))) + have hfit := E.j.1.place.fit t .tcb hth + have hts : ObjKindA.tcb.size = 4096 := rfl + rw [hts] at hd hfit + have hto : (wa t o).val = t.val + o.val := by rw [wa_valP, Nat.mod_eq_of_lt (by omega)] + have hev := congrArg UScalar.val heq + rw [hv, hto] at hev; omega + · set k : U64 := ⟨BitVec.ofNat 64 ((peAF frame lo).val - y.val)⟩ + have hkv : k.val = (peAF frame lo).val - y.val := by + change ((peAF frame lo).val - y.val) % 2 ^ 64 = _ + exact Nat.mod_eq_of_lt (by omega) + have hwy : (wa y k).val = (peAF frame lo).val := by + have := hO.fit y hy.1 + rw [wa_valP, hkv, Nat.mod_eq_of_lt (by omega)]; omega + have hyk := isSlot_off_frameA (k := k) (by omega) E.j.1.place hdisj hy.2 + rw [hwy] at hyk; omega + rw [L.mem_wr_ne hAtAl hal hneAt, hw4.2.2 _ hal hnotCapIs, hzr_in lo hlo] + -- Spec `kheap` preservation for all non-CNode/TCB/slot objects from `σ1` to `σ6` + have hkh6_nonSlot : ∀ q, q ∉ Dset → q ≠ specPtr frame → + (∀ r cs, σ1.kheap q ≠ some (.cnode r cs)) → (∀ t, σ1.kheap q ≠ some (.tcb t)) → + σ6.kheap q = σ1.kheap q := by + intro q hqD hqF hnc hnt + have h21 : σ2.kheap q = σ1.kheap q := by simp [σ2, Fl4SpecA64.detype, hqD] + have h32 : σ3.kheap q = σ2.kheap q := by + rcases hSS3.kh q with h | ⟨r, c1, c2, h1, _⟩ | ⟨t1, t2, h1, _, _⟩ + · exact h + · rw [h21] at h1; exact absurd h1 (hnc r c1) + · rw [h21] at h1; exact absurd h1 (hnt t1) + have h43 : σ4.kheap q = σ3.kheap q := by simp [σ4, Function.update_of_ne hqF] + have h54 : σ5.kheap q = σ4.kheap q := by + rcases hSS5.kh q with h | ⟨r, c1, c2, h1, _⟩ | ⟨t1, t2, h1, _, _⟩ + · exact h + · rw [h43, h32, h21] at h1; exact absurd h1 (hnc r c1) + · rw [h43, h32, h21] at h1; exact absurd h1 (hnt t1) + exact h54.trans (h43.trans (h32.trans h21)) + have hkh6_tcb : ∀ t, Z.th t = some .tcb → + ∃ x1 x6, σ1.kheap (specPtr t) = some (.tcb x1) ∧ σ6.kheap (specPtr t) = some (.tcb x6) ∧ + tcbNcA x6 = tcbNcA x1 := by + intro t ht + obtain ⟨x1, hx1, -⟩ := E1.j.1.st.tcb t ht + have h21 : σ2.kheap (specPtr t) = some (.tcb x1) := by + rw [hkh2_eq t ObjKindA.tcb.size (by decide) (Or.inr (Or.inr (Or.inl ⟨.tcb, ht, rfl⟩))), hx1] + have ⟨x3, hx3, he3⟩ : ∃ x3, σ3.kheap (specPtr t) = some (.tcb x3) ∧ tcbNcA x3 = tcbNcA x1 := by + rcases hSS3.kh (specPtr t) with h | ⟨_, _, _, h1, _⟩ | ⟨t1, t2, h1, h2, hrest⟩ + · exact ⟨x1, h.trans h21, rfl⟩ + · rw [h21] at h1; cases h1 + · rw [h21] at h1; cases Option.some.inj h1; exact ⟨t2, h2, hrest⟩ + have h43 : σ4.kheap (specPtr t) = some (.tcb x3) := by + have hne := hkh4_ne t ObjKindA.tcb.size (by decide) (Or.inr (Or.inr (Or.inl ⟨.tcb, ht, rfl⟩))) + simp [σ4, Function.update_of_ne hne, hx3] + rcases hSS5.kh (specPtr t) with h | ⟨_, _, _, h1, _⟩ | ⟨t1, t2, h1, h2, hrest⟩ + · exact ⟨x1, x3, hx1, h.trans h43, he3⟩ + · rw [h43] at h1; cases h1 + · rw [h43] at h1; cases Option.some.inj h1; exact ⟨x1, t2, hx1, h2, hrest.trans he3⟩ + -- `atAF (L.ksb s) i` is off every non-ASID-table `FootNA` word + have hAtOffFoot : ∀ b, FootNA (L := L) Z s1 b → (∀ j : U64, j.val < 128 → b ≠ atAF (L.ksb s) j) → b ≠ atAF (L.ksb s) i := + fun b _ h => h i hi + have hAtVal := atAF_valA hksFit hi + have hAtNeNode : ∀ c ∈ Z.Cs, ∀ o, o.val < 4288 → L.nodeAt s1 c o ≠ atAF (L.ksb s) i := by + intro c hc o ho e + obtain ⟨n, _, hn128, hcs⟩ := E.j.1.csN + have hcLt : c.val < 128 := by have := (hcs c).1 hc; omega + have hnv : (L.nodeAt s1 c o).val = (L.ksb s).val + 223488 + c.val * 4288 + o.val := by + rw [nodeAt_ksb hks1]; unfold LawfulStore.nodeAt + have h0 : (wa (L.ksb s) 223488#u64).val = (L.ksb s).val + 223488 := by + rw [wa_valP, show (223488#u64 : U64).val = 223488 from rfl, Nat.mod_eq_of_lt (by omega)] + have h1 : (core.num.U64.wrapping_mul c 4288#u64).val = c.val * 4288 := by + simp [core.num.U64.wrapping_mul_val_eq, U64.size_def, U64.numBits]; omega + have h2 : (wa (wa (L.ksb s) 223488#u64) (core.num.U64.wrapping_mul c 4288#u64)).val = + (L.ksb s).val + 223488 + c.val * 4288 := by + rw [wa_valP, h0, h1, Nat.mod_eq_of_lt (by omega)] + rw [wa_valP, h2, Nat.mod_eq_of_lt (by omega)] + have hev := congrArg UScalar.val e; rw [hnv, hAtVal] at hev; omega + have hAtNeIrq : ∀ k : U64, k.val < 5376 → irqWordA (L.ksb s) k ≠ atAF (L.ksb s) i := by + intro k hk e + have hv := irqWordA_val E.j.1.ksFit hk + have hev := congrArg UScalar.val e; rw [hv, hAtVal] at hev; omega + have hAtNeGlob : ∀ o : U64, o.val < 772352 → wa (L.ksb s) o ≠ atAF (L.ksb s) i := by + intro o ho e + have hv : (wa (L.ksb s) o).val = (L.ksb s).val + o.val := by + rw [wa_valP, Nat.mod_eq_of_lt (by omega)] + have hev := congrArg UScalar.val e; rw [hv, hAtVal] at hev; omega + have hAtNeAt : ∀ j : U64, j.val < 128 → j ≠ i → atAF (L.ksb s) j ≠ atAF (L.ksb s) i := by + intro j hj hne e + have hv := atAF_valA hksFit hj + have hev := congrArg UScalar.val e; rw [hv, hAtVal] at hev + exact hne (UScalar.eq_of_val_eq (by omega)) + -- Assemble `Z'` and `JAtZAX` + set G' := insGA Z.G (cslotAddrA parent) (cslotAddrA dest) + (insBA σ4 parent (absCapA (.AsidPool frame base)) (absCapA (capA st s3 (cslotAddrA parent)))) + set Z' : XSetsA := { Z with Pp := insert frame Z.Pp, G := G' } + have hks5 : L.ksb s5 = L.ksb s := by simp [s5, hks4] + have hcpu5 : L.cpu s5 = L.cpu s := by simp [s5, hcpu4] + have hks51 : L.ksb s5 = L.ksb s1 := by rw [hks5, hks1] + have hcpu51 : L.cpu s5 = L.cpu s1 := by rw [hcpu5, hcpu1] + -- Helper: `atAF (L.ksb s) i` is off TCBs and SCs + have hAtOffHeap : ∀ b, (∃ t ∈ Z.T, t.val ≤ b.val ∧ b.val < t.val + ObjKindA.tcb.size) ∨ + (∃ c ∈ Z.C, c.val ≤ b.val ∧ b.val < c.val + 128) → b ≠ atAF (L.ksb s) i := by + intro b hb e; subst e + rcases hb with ⟨t, ht, h1, h2⟩ | ⟨c, hc, h1, h2⟩ + · have hht : t ∈ H.tcbs := E.tcbs.symm ▸ ht + have hk := E.ko.1 t .tcb hht (fun _ h => by cases h) + have hts : ObjKindA.tcb.size = 4096 := rfl + rw [hts] at h2; simp [HKind.bits] at hk; omega + · obtain ⟨z, hz⟩ : c ∈ H.scs := E.scs.symm ▸ hc + have hk := E.ko.1 c (.sc z) hz (fun _ h => by cases h) + have hb7 := E.sys.ent.heap.scmin c z hz + have : 128 ≤ 2 ^ z := by + calc 128 = 2 ^ 7 := by decide + _ ≤ 2 ^ z := Nat.pow_le_pow_right (by omega) hb7 + simp [HKind.bits] at hk; omega + have hAsI : AsW (L.ksb s) (atAF (L.ksb s) i) := atAF_asWA hi + -- Spec global / TCB / SC preservation from `σ1` to `σ5` (and `σ6` agrees on everything except `armAsidTable`) + have hGlob5 : SpecGlobA σ1 σ5 := + { cur := hSS5.cur.trans (hSS3.cur) + idle := hSS5.idle.trans (hSS3.idle) + csc := hSS5.csc.trans (hSS3.csc) + act := hSS5.act.trans (hSS3.act) + time := hSS5.time.trans (hSS3.time) + irqS := hSS5.irqS.trans (hSS3.irqS) + irqN := hSS5.irqN.trans (hSS3.irqN) + rq := hSS5.rq.trans (hSS3.rq) + rls := hSS5.rls.trans (hSS3.rls) + cons := hSS5.cons.trans (hSS3.cons) + repr := hSS5.repr.trans (hSS3.repr) + dom := hSS5.dom.trans (hSS3.dom) + domT := hSS5.domT.trans (hSS3.domT) + vcur := hSS5.vcur.trans (hSS3.vcur) + asid := hSS5.asid.trans (hSS3.asid) + domk := DomKeepA.trans (σ2 := σ2) ⟨rfl, rfl, rfl, rfl⟩ (hSS3.domk.trans (DomKeepA.trans (σ2 := σ4) ⟨rfl, rfl, rfl, rfl⟩ hSS5.domk)) } + have hTcbKeep6 : TcbKeepA Z.T σ1 σ6 := by + intro t ht x hx1 + have hth : Z.th t = some .tcb := (E1.j.1.thT t).2 ht + have h21 : σ2.kheap (specPtr t) = some (.tcb x) := by + rw [hkh2_eq t ObjKindA.tcb.size (by decide) (Or.inr (Or.inr (Or.inl ⟨.tcb, hth, rfl⟩))), hx1] + obtain ⟨x3, hx3, hnc3⟩ := hSS3.tcb h21 + have h43 : σ4.kheap (specPtr t) = some (.tcb x3) := by + have hne := hkh4_ne t ObjKindA.tcb.size (by decide) (Or.inr (Or.inr (Or.inl ⟨.tcb, hth, rfl⟩))) + simp [σ4, Function.update_of_ne hne, hx3] + obtain ⟨x5, hx5, hnc5⟩ := hSS5.tcb h43 + exact ⟨x5, hx5, hnc5.trans hnc3⟩ + have hScKeep6 : ∀ c ∈ Z.C, σ6.kheap (scRenA m c) = σ1.kheap (scRenA m c) := by + intro c hc + obtain ⟨sc, _, hsc1, _⟩ := E1.j.1.sch.sc c hc + have h2 := hkh2_sc c hc + have hneF : scRenA m c ≠ specPtr frame := by + unfold scRenA; split_ifs with he + · have := hkh4_ne (fl4Ptr Fl4SpecA64.idleScPtr) 4096 (by omega) + (Or.inl ⟨rfl, Or.inr (Or.inr (Or.inr (Or.inr rfl)))⟩) + simpa [specPtr_fl4Ptr] using this + · exact hkh4_ne c (2 ^ H.scBits c) (Nat.pos_of_ne_zero (by positivity)) + (JointObjectA.of_sc (E.scs.symm ▸ hc)) + have h32 : σ3.kheap (scRenA m c) = σ2.kheap (scRenA m c) := by + rcases hSS3.kh (scRenA m c) with h | ⟨_, _, _, h1, _⟩ | ⟨_, _, h1, _, _⟩ + · exact h + · rw [h2, hsc1] at h1; cases h1 + · rw [h2, hsc1] at h1; cases h1 + have h43 : σ4.kheap (scRenA m c) = σ3.kheap (scRenA m c) := by simp [σ4, Function.update_of_ne hneF] + have h54 : σ5.kheap (scRenA m c) = σ4.kheap (scRenA m c) := by + rcases hSS5.kh (scRenA m c) with h | ⟨_, _, _, h1, _⟩ | ⟨_, _, h1, _, _⟩ + · exact h + · rw [h43, h32, h2, hsc1] at h1; cases h1 + · rw [h43, h32, h2, hsc1] at h1; cases h1 + exact h54.trans (h43.trans (h32.trans h2)) + -- Build `JAtZAX` at `(Z', s5, a, σ6, m)` + have hslot5_eq : ∀ y ∈ OS, L.mem s5 y = L.mem s4 y ∧ L.mem s5 (wa y 8#u64) = L.mem s4 (wa y 8#u64) ∧ + L.mem s5 (wa y 16#u64) = L.mem s4 (wa y 16#u64) ∧ L.mem s5 (wa y 24#u64) = L.mem s4 (wa y 24#u64) := by + intro y hy + have ha0 := hO.al0 hy + have ha8 := hO.al hy (k := 8#u64) (by simp) (by simp) + have ha16 := hO.al hy (k := 16#u64) (by simp) (by simp) + have ha24 := hO.al hy (k := 24#u64) (by simp) (by simp) + exact ⟨L.mem_wr_ne hAtAl ha0 (by simpa [hwa0] using hAtOffOS y hy 0#u64 (by decide)), + L.mem_wr_ne hAtAl ha8 (hAtOffOS y hy 8#u64 (by decide)), + L.mem_wr_ne hAtAl ha16 (hAtOffOS y hy 16#u64 (by decide)), + L.mem_wr_ne hAtAl ha24 (hAtOffOS y hy 24#u64 (by decide))⟩ + have hcap5_cases : ∀ y, IsSlotA Z.th y → + capAtA (L.mem s5) y = capAtA (L.mem s1) y ∨ + capAtA (L.mem s5) y = .Untyped frame 12#u64 4096#u64 false ∨ + capAtA (L.mem s5) y = .AsidPool frame base := by + intro y hy + change capA st s5 y = capA st s1 y ∨ capA st s5 y = .Untyped frame 12#u64 4096#u64 false ∨ + capA st s5 y = .AsidPool frame base + rw [hcap5_eq y (RS.sub y hy)] + by_cases e1 : y = cslotAddrA dest + · subst e1; exact Or.inr (Or.inr hsd4) + · rw [hkeep4 y (RS.sub y hy) e1, hcap3_is y hy] + by_cases e2 : y = cslotAddrA parent + · subst e2; exact Or.inr (Or.inl hcp2) + · exact Or.inl (hko2 y (RS.sub y hy) e2) + have hJ6 : JAtZAX (L := L) AR Z' s5 a σ6 m := by + have hSt6 : StateRelA Z.th (L.mem s5) σ6 := by + refine ⟨fun p r hp => ?_, fun p hp => ?_⟩ + · obtain ⟨cs, hk, hw, hs⟩ := hR5.cnode p r hp + refine ⟨cs, hk, hw, fun bs hbs => ?_⟩ + have hAb : AbsSlotA Z.th (specPtr p, bs) := ⟨p, rfl, Or.inl (by rw [hbs]; exact hp)⟩ + have hc := hcap5_eq _ (RS.sub _ (hAb.isSlot E.j.1.place)) + change capAtA (L.mem s5) _ = capAtA (L.mem s4) _ at hc + unfold cslotAddrA at hc + unfold SlotRelA; rw [hc]; exact hs bs hbs + · obtain ⟨t, hk, hs⟩ := hR5.tcb p hp + refine ⟨t, hk, fun bs hbs => ?_⟩ + have hslot : IsSlotA Z.th (slotAddrA (specPtr p) (BitVec.ofNat 64 bs)) := + ⟨p, Or.inr ⟨hp, bs, hbs, rfl⟩⟩ + have hc := hcap5_eq _ (RS.sub _ hslot) + change capAtA (L.mem s5) _ = capAtA (L.mem s4) _ at hc + unfold SlotRelA; rw [hc]; exact hs bs hbs + have hCdt6 : CdtRelA st Z.th s5 σ6 G' := + ⟨treeA_of_mem hC5.tree fun y hy => hslot5_eq y (RS.sub y hy), hC5.cdt, hC5.cdt_slot, + fun sl hs => by + have h4w := hslot5_eq (cslotAddrA sl) (RS.sub _ (hs.isSlot E.j.1.place)) + have hrv : revA (L.mem s5) (cslotAddrA sl) = revA (L.mem s4) (cslotAddrA sl) := by + unfold revA; rw [h4w.2.2.2] + rw [hrv]; exact hC5.orig sl hs, + hC5.par_slot, + fun a p hp => by + have hpIs : IsSlotA Z.th p := by + have ⟨_, hpa⟩ := hC5.tree.tree.par_mem a p hp + exact ((hC5.tree.lk.members p).1 hpa).1 + have h4w := hslot5_eq p (RS.sub p hpIs) + unfold revA; rw [h4w.2.2.2]; exact hC5.prev a p hp⟩ + have hSch6_s1 := E1.j.1.sch.of_spec (σ' := σ5) hTcbKeep6 hScKeep6 hGlob5 + have hIrq6_s1 := E1.j.1.irq.of_spec (σ' := σ5) hGlob5 + have hRest6_s1 := E1.j.1.rest.of_spec (σ' := σ5) hTcbKeep6 hScKeep6 hGlob5 + have hnode51 : ∀ c ∈ Z.Cs, ∀ o : U64, + (o = layout.node.CUR_THREAD ∨ o = layout.node.IDLE_THREAD ∨ o = layout.node.CUR_SC ∨ o = layout.node.IDLE_SC) → + L.mem s5 (L.nodeAt s5 c o) = L.mem s1 (L.nodeAt s1 c o) := fun c hc o ho => by + rw [nodeAt_ksb hks51] + have hoLt : o.val < 4288 := by + rcases ho with rfl | rfl | rfl | rfl <;> + simp [layout.node.CUR_THREAD, layout.node.IDLE_THREAD, layout.node.CUR_SC, layout.node.IDLE_SC] + exact hfn_s5 _ (.node c _ hc (by rcases ho with rfl | rfl | rfl | rfl <;> simp)) (hAtNeNode c hc o hoLt) + have hcur51 : ∀ o : U64, + (o = layout.node.SCHED_ACTION ∨ o = layout.node.CUR_TIME ∨ o = layout.node.CONSUMED ∨ o = layout.node.REPROGRAM) → + L.mem s5 (L.nodeAt s5 (L.cpu s5) o) = L.mem s1 (L.nodeAt s1 (L.cpu s1) o) := fun o ho => by + rw [hcpu51, nodeAt_ksb hks51] + have hoLt : o.val < 4288 := by + rcases ho with rfl | rfl | rfl | rfl <;> + simp [layout.node.SCHED_ACTION, layout.node.CUR_TIME, layout.node.CONSUMED, layout.node.REPROGRAM] + have hfn : FootNA (L := L) Z s1 (L.nodeAt s1 (L.cpu s1) o) := by + rcases ho with rfl | rfl | rfl | rfl + · exact .cur _ (Or.inl rfl) + · exact .cur _ (Or.inr rfl) + · exact .cur2 _ (Or.inl rfl) + · exact .cur2 _ (Or.inr rfl) + exact hfn_s5 _ hfn (hAtNeNode _ E1.j.1.sch.cpuCs o hoLt) + have htcb51 : ∀ t ∈ Z.T, ∀ w : U64, + (w = stA t ∨ w = scA t ∨ w = affA t ∨ w = prioA t ∨ w = domA t) → + L.mem s5 w = L.mem s1 w := fun t ht w hw => by + have hfit := E.j.1.place.fit t .tcb ((E.j.1.thT t).2 ht) + have hwb : t.val ≤ w.val ∧ w.val < t.val + ObjKindA.tcb.size := by + simp only [ObjKindA.size] at hfit ⊢ + rcases hw with rfl | rfl | rfl | rfl | rfl + · have hstLt : layout.tcb.STATE.val < 4096 := by simp [layout.tcb.STATE] + rw [stA, wa_valP, Nat.mod_eq_of_lt (by omega)]; omega + · rw [scA, wa_valP, show (3464#u64 : U64).val = 3464 from rfl, Nat.mod_eq_of_lt (by omega)]; omega + · rw [affA, wa_valP, show (3520#u64 : U64).val = 3520 from rfl, Nat.mod_eq_of_lt (by omega)]; omega + · rw [prioA, wa_valP, show (3448#u64 : U64).val = 3448 from rfl, Nat.mod_eq_of_lt (by omega)]; omega + · rw [domA, wa_valP, show (3432#u64 : U64).val = 3432 from rfl, Nat.mod_eq_of_lt (by omega)]; omega + have hfn : FootNA (L := L) Z s1 w := by + rcases hw with rfl | rfl | rfl | rfl | rfl + · exact .tcb t _ ht (Or.inl rfl) + · exact .tcb t _ ht (Or.inr (Or.inl rfl)) + · exact .tcb t _ ht (Or.inr (Or.inr (Or.inl rfl))) + · exact .tcb2 t _ ht (Or.inl rfl) + · exact .tcb2 t _ ht (Or.inr rfl) + exact hfn_s5 w hfn (hAtOffHeap w (Or.inl ⟨t, ht, hwb.1, hwb.2⟩)) + have hsc51 : ∀ c ∈ Z.C, L.mem s5 (wa c 24#u64) = L.mem s1 (wa c 24#u64) ∧ + L.mem s5 (wa c 72#u64) = L.mem s1 (wa c 72#u64) := fun c hc => by + obtain ⟨z, hz⟩ : c ∈ H.scs := E.scs.symm ▸ hc + have hfit := E.sys.ent.heap.fits c (.sc z) hz + have hb7 := E.sys.ent.heap.scmin c z hz + have h128 : 128 ≤ 2 ^ z := by + calc 128 = 2 ^ 7 := by decide + _ ≤ 2 ^ z := Nat.pow_le_pow_right (by omega) hb7 + simp only [HKind.bits] at hfit + have hw24 : (wa c 24#u64).val = c.val + 24 := by + rw [wa_valP, show (24#u64 : U64).val = 24 from rfl, Nat.mod_eq_of_lt (by omega)] + have hw72 : (wa c 72#u64).val = c.val + 72 := by + rw [wa_valP, show (72#u64 : U64).val = 72 from rfl, Nat.mod_eq_of_lt (by omega)] + exact ⟨hfn_s5 _ (.sc c hc) (hAtOffHeap _ (Or.inr ⟨c, hc, by omega, by omega⟩)), + hfn_s5 _ (.sc2 c hc) (hAtOffHeap _ (Or.inr ⟨c, hc, by omega, by omega⟩))⟩ + have hSch6 : SchedRelA (L := L) Z.T Z.C Z.Cs Z.curOf Z.curScOf s5 σ6 m := + { core := by rw [hcpu51]; exact hSch6_s1.core + cpuCs := by rw [hcpu51]; exact hSch6_s1.cpuCs + curOfW := fun c hc => by rw [hnode51 c hc _ (Or.inl rfl)]; exact hSch6_s1.curOfW c hc + curScOfW := fun c hc => by rw [hnode51 c hc _ (Or.inr (Or.inr (Or.inl rfl)))]; exact hSch6_s1.curScOfW c hc + cur := hSch6_s1.cur + csc := hSch6_s1.csc + idle := by + unfold LawfulStore.idleW + rw [hcpu51, hnode51 _ E1.j.1.sch.cpuCs _ (Or.inr (Or.inl rfl))] + exact hSch6_s1.idle + idleOf := fun c hc => by rw [hnode51 c hc _ (Or.inr (Or.inl rfl))]; exact hSch6_s1.idleOf c hc + idleScOf := fun c hc => by rw [hnode51 c hc _ (Or.inr (Or.inr (Or.inr rfl)))]; exact hSch6_s1.idleScOf c hc + act := by unfold LawfulStore.actionW; rw [hcur51 _ (Or.inl rfl)]; exact hSch6_s1.act + time := by unfold LawfulStore.curTime LawfulStore.nodeA; rw [hcur51 _ (Or.inr (Or.inl rfl))]; exact hSch6_s1.time + aff := fun t ht => by rw [htcb51 t ht _ (Or.inr (Or.inr (Or.inl rfl)))]; exact hSch6_s1.aff t ht + affT := hSch6_s1.affT + tcb := fun t ht => by + rw [htcb51 t ht _ (Or.inl rfl), htcb51 t ht _ (Or.inr (Or.inl rfl))] + exact hSch6_s1.tcb t ht + sc := fun c hc => by rw [(hsc51 c hc).1]; exact hSch6_s1.sc c hc + scOff := hSch6_s1.scOff } + have hIrq6 : IrqRelA (L.mem s5) (L.ksb s5) σ6 := + { st := fun k hk => by + rw [hks51, hfn_s5 _ (.irq k hk) (by rw [hks1]; exact hAtNeIrq k hk)] + exact hIrq6_s1.st k hk + node := fun k hk => by rw [hks51]; exact hIrq6_s1.node k hk + nodeHi := fun k hk => by rw [hks51]; exact hIrq6_s1.nodeHi k hk } + have hRest6 : SchedRestA (L := L) Z' s5 σ6 m := + { rq := by rw [hcpu51]; exact hRest6_s1.rq + rqDom := hRest6_s1.rqDom + rel := by rw [hcpu51]; exact hRest6_s1.rel + consumed := by rw [hcur51 _ (Or.inr (Or.inr (Or.inl rfl)))]; exact hRest6_s1.consumed + repr := by rw [hcur51 _ (Or.inr (Or.inr (Or.inr rfl)))]; exact hRest6_s1.repr + curDom := by + rw [hks51, hfn_s5 _ (.glob _ (Or.inl rfl)) (by rw [hks1]; exact hAtNeGlob 8#u64 (by decide))] + exact hRest6_s1.curDom + domTime := by + rw [hks51, hfn_s5 _ (.glob _ (Or.inr rfl)) (by rw [hks1]; exact hAtNeGlob 16#u64 (by decide))] + exact hRest6_s1.domTime + prio := fun t ht => by + rw [htcb51 t ht _ (Or.inr (Or.inr (Or.inr (Or.inl rfl)))), + htcb51 t ht _ (Or.inr (Or.inr (Or.inr (Or.inr rfl))))] + exact hRest6_s1.prio t ht + scMem := fun t ht => by rw [htcb51 t ht _ (Or.inr (Or.inl rfl))]; exact hRest6_s1.scMem t ht + refillMax := fun c hc => by rw [(hsc51 c hc).2]; exact hRest6_s1.refillMax c hc } + -- Reachability monotonicity across installing the empty pool `frame` at `(L.ksb s5, i)` + have hPoolNeF : ∀ p ∈ Z.Pp, p ≠ frame := fun p hp0 e => + hkh4_ne p 4096 (by omega) (Or.inl ⟨rfl, Or.inl hp0⟩) (congrArg specPtr e) + have hActP51 : ∀ p ∈ Z.Pp, ActivePoolA (L.mem s5) (L.ksb s5) p → ActivePoolA (L.mem s1) (L.ksb s1) p := by + rintro p hp0 ⟨hpne, hi', hhi', htab⟩ + rw [hks5] at htab + by_cases he : hi' = i + · subst he; rw [hAtWr] at htab; exact absurd htab.symm (hPoolNeF p hp0) + · rw [hfn_s5 _ (by rw [← hks1]; exact .vs _ (.asid hi' hhi')) (hAtNeAt hi' hhi' he), ← hks1] at htab + exact ⟨hpne, hi', hhi', htab⟩ + have hActR51 : ∀ asid raw, ActiveAsidRootA (L.mem s5) (L.ksb s5) (insert frame Z.Pp) asid raw → + ActiveAsidRootA (L.mem s1) (L.ksb s1) Z.Pp asid raw := by + rintro asid raw ⟨hi', lo, hhi', hlo, ha', pool, hp, hpne, htab, hw, hrne⟩ + rcases Set.mem_insert_iff.1 hp with rfl | hp0 + · rw [hpool5 lo hlo] at hw; exact absurd hw.symm hrne + · rw [hks5] at htab + by_cases he : hi' = i + · subst he; rw [hAtWr] at htab; exact absurd htab.symm (hPoolNeF pool hp0) + · rw [hfn_s5 _ (by rw [← hks1]; exact .vs _ (.asid hi' hhi')) (hAtNeAt hi' hhi' he), ← hks1] at htab + rw [hfn_s5 _ (.vs _ (.pool pool lo hp0 hlo)) (fun e => E.vsLeafA.2.2 pool hp0 lo hlo (e ▸ hAsI))] at hw + exact ⟨hi', lo, hhi', hlo, ha', pool, hp0, hpne, htab, hw, hrne⟩ + have hActH51 : ∀ y, ActiveHighRootA (L.mem s5) (L.ksb s5) (insert frame Z.Pp) y → + ActiveHighRootA (L.mem s1) (L.ksb s1) Z.Pp y := by + rintro y ⟨asid, raw, ha, htag, hy⟩ + exact ⟨asid, raw, hActR51 asid raw ha, htag, hy⟩ + have hVsP51 : ∀ {tag sh q}, VsPathA (insert frame Z.Pp) Z.S1 Z.Y (L.mem s5) (L.ksb s5) tag sh q → + VsPathA Z.Pp Z.S1 Z.Y (L.mem s1) (L.ksb s1) tag sh q := by + intro tag sh q hp + induction hp with + | native ha htag htb => exact .native (hActR51 _ _ ha) htag htb + | high ha htag hy => exact .high (hActR51 _ _ ha) htag hy + | @child tag t k sh _ htY h12 hk h3 ih => + have hpe : L.mem s5 (peAF t k) = L.mem s1 (peAF t k) := by + rcases htY with ht | ⟨y, hy, rfl⟩ + · exact hfn_s5 _ (.vs _ (.pt t k ht hk)) (fun e => E.vsLeafA.2.1 t ht k hk (e ▸ hAsI)) + · exact hfn_s5 _ (.vs _ (.high y k hy hk)) + (fun e => hT (wa y 4096#u64) k hk (Or.inr (Or.inr (Or.inr ⟨y, hy, rfl⟩))) (e ▸ hAsI)) + rw [hpe] at h3 ⊢ + exact .child ih htY h12 hk h3 + have hVsR51 : ∀ q, VsReachA (insert frame Z.Pp) Z.S1 Z.Y (L.mem s5) (L.ksb s5) q → + VsReachA Z.Pp Z.S1 Z.Y (L.mem s1) (L.ksb s1) q := by + rintro q ⟨tag, sh, htag, hp⟩ + exact ⟨tag, sh, htag, hVsP51 hp⟩ + -- `VsRelA` at `Z'.Pp = insert frame Z.Pp` + have hVs6 : VsRelA (insert frame Z.Pp) Z.S1 Z.Lv Z.X Z.Y (L.mem s5) (L.ksb s5) σ6 := by + have htab1 : σ5.archState.armAsidTable = σ1.archState.armAsidTable := hGlob5.asid + refine ⟨?_, ?_, ?_, ?_, ?_, ?_, ?_, E1.j.1.vs.lv, E1.j.1.vs.al, ?_, E1.j.1.vs.yal, E1.j.1.vs.ytb⟩ + · -- table + intro hi' hhi' + rw [hks5] + by_cases he : hi' = i + · subst he + simp only [σ6, hiBv, Function.update_self, hAtWr, optPtrA, if_neg hfrNz] + · have hneBv : BitVec.ofNat 7 hi'.val ≠ hiBv := fun e => he (UScalar.eq_of_val_eq (by + have h1 := congrArg BitVec.toNat e + rw [BitVec.toNat_ofNat, BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega), Nat.mod_eq_of_lt (by omega)] at h1 + exact h1)) + simp only [σ6, Function.update_of_ne hneBv, htab1] + rw [hfn_s5 _ (by rw [← hks1]; exact .vs _ (.asid hi' hhi')) (hAtNeAt hi' hhi' he)] + simpa [hks1] using E1.j.1.vs.table hi' hhi' + · -- tmem + intro hi' hhi' hne + rw [hks5] at hne ⊢ + by_cases he : hi' = i + · subst he; rw [hAtWr]; exact Set.mem_insert frame Z.Pp + · rw [hfn_s5 _ (by rw [← hks1]; exact .vs _ (.asid hi' hhi')) (hAtNeAt hi' hhi' he)] at hne ⊢ + rw [← hks1] + exact Set.mem_insert_of_mem frame (E1.j.1.vs.tmem hi' hhi' (by rw [hks1]; exact hne)) + · -- pool + intro p hp + rcases Set.mem_insert_iff.1 hp with rfl | hp0 + · refine ⟨fun _ => none, ?_, fun _ lo hlo => by rw [hpool5 lo hlo]; rfl⟩ + have h54 : σ5.kheap (specPtr p) = σ4.kheap (specPtr p) := by + rcases hSS5.kh (specPtr p) with h | ⟨_, _, _, h1, _⟩ | ⟨_, _, h1, _, _⟩ + · exact h + · simp [σ4] at h1 + · simp [σ4] at h1 + simpa [σ6, σ4] using h54 + · obtain ⟨f, hf1, hfe⟩ := E1.j.1.vs.pool p hp0 + have hkh : σ6.kheap (specPtr p) = some (.archObj (.asidPool f)) := by + rw [hkh6_nonSlot (specPtr p) + (not_mem_detype_of_disjA (by omega) hfrAl hfrFit (hdisj p 4096 (Or.inl ⟨rfl, Or.inl hp0⟩))) + (hkh4_ne p 4096 (by omega) (Or.inl ⟨rfl, Or.inl hp0⟩)) + (fun _ _ h => by rw [hf1] at h; cases h) (fun _ h => by rw [hf1] at h; cases h), hf1] + refine ⟨f, hkh, fun ha lo hlo => ?_⟩ + rw [hfn_s5 _ (.vs _ (.pool p lo hp0 hlo)) (fun e => E.vsLeafA.2.2 p hp0 lo hlo (e ▸ hAsI))] + exact hfe (hActP51 p hp0 ha) lo hlo + · -- kind + intro p hp ha lo hlo + rcases Set.mem_insert_iff.1 hp with rfl | hp0 + · rw [hpool5 lo hlo]; exact Or.inl rfl + · rw [hfn_s5 _ (.vs _ (.pool p lo hp0 hlo)) (fun e => E.vsLeafA.2.2 p hp0 lo hlo (e ▸ hAsI))] + exact E1.j.1.vs.kind p hp0 (hActP51 p hp0 ha) lo hlo + · -- asid0 + intro hne + rw [hks5] at hne ⊢ + by_cases h0 : i = 0#u64 + · subst h0; rw [hAtWr]; exact hpool5 0#u64 (by decide) + · have hne0 : (0#u64 : U64) ≠ i := Ne.symm h0 + have hmem0 := hfn_s5 _ (by rw [← hks1]; exact .vs _ (.asid 0#u64 (by decide))) (hAtNeAt 0#u64 (by decide) hne0) + rw [hmem0] at hne ⊢ + have hne1 : L.mem s1 (atAF (L.ksb s1) 0#u64) ≠ 0#u64 := by rw [hks1]; exact hne + have hp0 : L.mem s1 (atAF (L.ksb s1) 0#u64) ∈ Z.Pp := E1.j.1.vs.tmem 0#u64 (by decide) hne1 + rw [← hks1] + rw [hfn_s5 _ (.vs _ (.pool _ 0#u64 hp0 (by decide))) + (fun e => E.vsLeafA.2.2 _ hp0 0#u64 (by decide) (e ▸ hAsI))] + exact E1.j.1.vs.asid0 hne1 + · -- pt + intro p hp + obtain ⟨t, ht1, hty, hpe⟩ := E1.j.1.vs.pt p hp + have hkh : σ6.kheap (specPtr p) = some (.archObj (.pageTable t)) := by + rw [hkh6_nonSlot (specPtr p) + (not_mem_detype_of_disjA (by omega) hfrAl hfrFit (hdisj p 4096 (Or.inl ⟨rfl, Or.inr (Or.inl hp)⟩))) + (hkh4_ne p 4096 (by omega) (Or.inl ⟨rfl, Or.inr (Or.inl hp)⟩)) + (fun _ _ h => by rw [ht1] at h; cases h) (fun _ h => by rw [ht1] at h; cases h), ht1] + refine ⟨t, hkh, hty, fun hr k hk => ?_⟩ + rw [hfn_s5 _ (.vs _ (.pt p k hp hk)) + (fun e => E.vsLeafA.2.1 p hp k hk (e ▸ hAsI))] + exact hpe (hVsR51 p hr) k hk + · -- high + intro y hy + obtain ⟨l1, hl1, hpe⟩ := E1.j.1.vs.high y hy + have hkh : σ6.kheap (specPtr y) = some (.archObj (.ext (.highVSpace l1))) := by + rw [hkh6_nonSlot (specPtr y) + (not_mem_detype_of_disjA (by omega) hfrAl hfrFit (hdisj y 8192 (Or.inr (Or.inl ⟨rfl, Or.inr hy⟩)))) + (hkh4_ne y 8192 (by omega) (Or.inr (Or.inl ⟨rfl, Or.inr hy⟩))) + (fun _ _ h => by rw [hl1] at h; cases h) (fun _ h => by rw [hl1] at h; cases h), hl1] + refine ⟨l1, hkh, fun ha k hk => ?_⟩ + rw [hfn_s5 _ (.vs _ (.high y k hy hk)) + (fun e => hT (wa y 4096#u64) k hk (Or.inr (Or.inr (Or.inr ⟨y, hy, rfl⟩))) (e ▸ hAsI))] + exact hpe (hActH51 y ha) k hk + · -- pal + intro p hp + rcases Set.mem_insert_iff.1 hp with rfl | hp0 + · exact Nat.dvd_of_mod_eq_zero hfrAl + · exact E1.j.1.vs.pal p hp0 + -- `ExtVsRelA` at `Z'.Pp = insert frame Z.Pp` + have hExt6 : ExtVsRelA (insert frame Z.Pp) Z.S1 Z.X Z.Y Z.T2 Z.Sh2 (L.mem s5) σ6 := by + have hExt1 := E1.j.1.ext + have hS2Root51 : ∀ r ∈ Z.X, ∀ j : U64, j.val < 1024 → L.mem s5 (peAF r j) = L.mem s1 (peAF r j) := by + intro r hr j hj + by_cases hj5 : j.val < 512 + · exact hfn_s5 _ (.ext _ (.s2lo r j hr hj5)) + (fun e => hT r j hj5 (Or.inl (Or.inr ⟨r, hr, Or.inl rfl⟩)) (e ▸ hAsI)) + · set j' : U64 := ⟨BitVec.ofNat 64 (j.val - 512)⟩ + have hj'v : j'.val = j.val - 512 := by + change (j.val - 512) % 2 ^ 64 = _; exact Nat.mod_eq_of_lt (by omega) + have hpeEq : peAF r j = peAF (wa r 4096#u64) j' := by + have hral : r.val % 8192 = 0 := Nat.mod_eq_zero_of_dvd (hExt1.xal r hr) + have hrlt : r.val < 2 ^ 64 := by simpa using r.hBounds + apply UScalar.eq_of_val_eq + unfold peAF + have hm1 : (core.num.U64.wrapping_mul j 8#u64).val = 8 * j.val := by + simp [core.num.U64.wrapping_mul_val_eq, U64.size_def, U64.numBits]; omega + have hm2 : (core.num.U64.wrapping_mul j' 8#u64).val = 8 * j'.val := by + simp [core.num.U64.wrapping_mul_val_eq, U64.size_def, U64.numBits]; omega + have hr4 : (wa r 4096#u64).val = r.val + 4096 := by + rw [wa_valP, show (4096#u64 : U64).val = 4096 from rfl, Nat.mod_eq_of_lt (by omega)] + rw [wa_valP, wa_valP, hm1, hm2, hr4, hj'v, Nat.mod_eq_of_lt (by omega), Nat.mod_eq_of_lt (by omega)] + omega + rw [hpeEq] + exact hfn_s5 _ (.ext _ (.s2hi r j' hr (by omega))) + (fun e => hT (wa r 4096#u64) j' (by omega) (Or.inl (Or.inr ⟨r, hr, Or.inr rfl⟩)) (e ▸ hAsI)) + have hActS2_51 : ∀ r, ActiveS2RootA (insert frame Z.Pp) Z.X σ6 r → ActiveS2RootA Z.Pp Z.X σ1 r := by + rintro r ⟨hr, asid, p, f, ha0, hp, htab, hkh6, hf⟩ + rcases Set.mem_insert_iff.1 hp with rfl | hp0 + · obtain ⟨f0, hkhF, -⟩ := hVs6.pool p (Set.mem_insert p Z.Pp) + have h54 : σ5.kheap (specPtr p) = σ4.kheap (specPtr p) := by + rcases hSS5.kh (specPtr p) with h | ⟨_, _, _, h1, _⟩ | ⟨_, _, h1, _, _⟩ + · exact h + · simp [σ4] at h1 + · simp [σ4] at h1 + have hkhF' : σ6.kheap (specPtr p) = some (.archObj (.asidPool (fun _ => none))) := by + simpa [σ6, σ4] using h54 + rw [hkhF'] at hkh6 + cases hkh6; simp at hf + · obtain ⟨f1, hf1, -⟩ := E1.j.1.vs.pool p hp0 + have hneP : specPtr p ≠ specPtr frame := hkh4_ne p 4096 (by omega) (Or.inl ⟨rfl, Or.inl hp0⟩) + have hkhEq : σ6.kheap (specPtr p) = σ1.kheap (specPtr p) := + hkh6_nonSlot (specPtr p) + (not_mem_detype_of_disjA (by omega) hfrAl hfrFit (hdisj p 4096 (Or.inl ⟨rfl, Or.inl hp0⟩))) + hneP (fun _ _ h => by rw [hf1] at h; cases h) (fun _ h => by rw [hf1] at h; cases h) + rw [hkhEq] at hkh6 + have hneHi : Fl4SpecA64.asidHighBitsOf asid ≠ hiBv := by + intro he + simp only [σ6, he, Function.update_self] at htab + exact hneP (Option.some.inj htab).symm + simp only [σ6, Function.update_of_ne hneHi, hGlob5.asid] at htab + exact ⟨hr, asid, p, f, ha0, hp0, htab, hkh6, hf⟩ + have hS2R51 : ∀ q, S2ReachA (insert frame Z.Pp) Z.X Z.T2 Z.Sh2 (L.mem s5) σ6 q → + S2ReachA Z.Pp Z.X Z.T2 Z.Sh2 (L.mem s1) σ1 q := by + intro q hq + induction hq with + | root ha => exact .root (hActS2_51 _ ha) + | @stepRoot r j ha hj h3 ht2 => + have ha1 := hActS2_51 _ ha + have hj1k : j.val < 1024 := by + unfold Fl4SpecA64.s2Geom Fl4SpecA64.s2GeomOf at hj + split_ifs at hj <;> dsimp only at hj <;> omega + rw [hS2Root51 r ha1.1 j hj1k] at h3 ht2 ⊢ + exact .stepRoot ha1 hj h3 ht2 + | @stepPt t k _ ht h12 hk h3 ht2 ih => + rw [hfn_s5 _ (.ext _ (.s2pt t k ht hk)) + (fun e => hT t k hk (Or.inl (Or.inl ht)) (e ▸ hAsI))] at h3 ht2 ⊢ + exact .stepPt ih ht h12 hk h3 ht2 + refine ⟨?_, ?_, hExt1.tsh, ?_, ?_, hExt1.xal, hExt1.tal, ?_, hExt1.t2x, ?_, ?_⟩ + · intro r hr + obtain ⟨es, hes1, hpe⟩ := hExt1.s2root r hr + have hkh : σ6.kheap (specPtr r) = some (.archObj (.ext (.stage2Root es))) := by + rw [hkh6_nonSlot (specPtr r) + (not_mem_detype_of_disjA (by omega) hfrAl hfrFit (hdisj r 8192 (Or.inr (Or.inl ⟨rfl, Or.inl hr⟩)))) + (hkh4_ne r 8192 (by omega) (Or.inr (Or.inl ⟨rfl, Or.inl hr⟩))) + (fun _ _ h => by rw [hes1] at h; cases h) (fun _ h => by rw [hes1] at h; cases h), hes1] + refine ⟨es, hkh, fun ha j hj htm => ?_⟩ + rw [hS2Root51 r hr j hj] + exact hpe (hActS2_51 r ha) j hj htm + · intro t ht + obtain ⟨pt, hpt1, hty, hpe⟩ := hExt1.s2pt t ht + have hkh : σ6.kheap (specPtr t) = some (.archObj (.pageTable pt)) := by + rw [hkh6_nonSlot (specPtr t) + (not_mem_detype_of_disjA (by omega) hfrAl hfrFit (hdisj t 4096 (Or.inl ⟨rfl, Or.inr (Or.inr (Or.inl ht))⟩))) + (hkh4_ne t 4096 (by omega) (Or.inl ⟨rfl, Or.inr (Or.inr (Or.inl ht))⟩)) + (fun _ _ h => by rw [hpt1] at h; cases h) (fun _ h => by rw [hpt1] at h; cases h), hpt1] + refine ⟨pt, hkh, hty, fun hr k hk => ?_⟩ + rw [hfn_s5 _ (.ext _ (.s2pt t k ht hk)) + (fun e => hT t k hk (Or.inl (Or.inl ht)) (e ▸ hAsI))] + exact hpe (hS2R51 t hr) k hk + · intro y hy + rw [hfn_s5 _ (.ext _ (.wl0 y hy)) + (fun e => hYAs y hy 511#u64 (by decide) (e ▸ hAsI))] + exact hExt1.wl0 y hy + · intro y hy k hk + rw [hfn_s5 _ (.ext _ (.wlo y k hy hk)) + (fun e => hYAs y hy k (by omega) (e ▸ hAsI))] + exact hExt1.wlo y hy k hk + · -- pgd + intro p hp + refine ⟨?_, (hExt1.pgd p hp).2⟩ + intro hvs + rcases hvs with (rfl | hp0) | hp1 | ⟨y, hy, rfl⟩ + · rcases hp with ht2 | ⟨r, hr, rfl | rfl⟩ + · rcases hdisj p 4096 (Or.inl ⟨rfl, Or.inr (Or.inr (Or.inl ht2))⟩) with h | h <;> omega + · rcases hdisj p 8192 (Or.inr (Or.inl ⟨rfl, Or.inl hr⟩)) with h | h <;> omega + · have hral : r.val % 8192 = 0 := Nat.mod_eq_zero_of_dvd (hExt1.xal r hr) + have hrlt : r.val < 2 ^ 64 := by simpa using r.hBounds + have h4 : (wa r 4096#u64).val = r.val + 4096 := by + rw [wa_valP, show (4096#u64 : U64).val = 4096 from rfl, Nat.mod_eq_of_lt (by omega)] + rcases hdisj r 8192 (Or.inr (Or.inl ⟨rfl, Or.inl hr⟩)) with h | h <;> omega + · exact (hExt1.pgd p hp).1 (Or.inl hp0) + · exact (hExt1.pgd p hp).1 (Or.inr (Or.inl hp1)) + · exact (hExt1.pgd _ hp).1 (Or.inr (Or.inr ⟨y, hy, rfl⟩)) + · -- ypg + intro y hy hvs + rcases hvs with (rfl | hp0) | hp1 | ⟨y', hy', hEq⟩ + · rcases hdisj y 8192 (Or.inr (Or.inl ⟨rfl, Or.inr hy⟩)) with h | h <;> omega + · exact hExt1.ypg y hy (Or.inl hp0) + · exact hExt1.ypg y hy (Or.inr (Or.inl hp1)) + · exact hExt1.ypg y hy (Or.inr (Or.inr ⟨y', hy', hEq⟩)) + · -- ypp + intro y hy hmem + rcases Set.mem_insert_iff.1 hmem with hEq | hp0 + · have hyal : y.val % 8192 = 0 := Nat.mod_eq_zero_of_dvd (E1.j.1.vs.yal y hy) + have hylt : y.val < 2 ^ 64 := by simpa using y.hBounds + have h4 : (wa y 4096#u64).val = y.val + 4096 := by + rw [wa_valP, show (4096#u64 : U64).val = 4096 from rfl, Nat.mod_eq_of_lt (by omega)] + have hfv := congrArg UScalar.val hEq + rcases hdisj y 8192 (Or.inr (Or.inl ⟨rfl, Or.inr hy⟩)) with h | h <;> omega + · exact hExt1.ypp y hy hp0 + -- Cap-based fields (`cv`, `pml`, `VcpuCoreA.cap`, `Stage2CoreA.cap`, `HighVSpaceCoreA.cap`) + have hJAt6 : JAtZA (L := L) AR Z' s5 a σ6 m := by + refine ⟨⟨hSt6, hCdt6, hSch6, E1.j.1.ar, hks51.symm ▸ E1.j.1.place, hIrq6, hRest6⟩, + E1.j.1.thT, E1.j.1.csN, ?_, ?_, ?_, ?_, hks51.symm ▸ E1.j.1.ksFit, E1.j.1.scOffD, + ?_, ?_, hVs6, hExt6⟩ + · intro c hc; rw [hnode51 c hc _ (Or.inr (Or.inl rfl))]; exact E1.j.1.idleT c hc + · intro c hc; rw [hnode51 c hc _ (Or.inr (Or.inr (Or.inr rfl)))]; exact E1.j.1.idleC c hc + · intro c hc; rw [hnode51 c hc _ (Or.inl rfl)]; exact E1.j.1.curT c hc + · intro c hc; rw [hnode51 c hc _ (Or.inr (Or.inr (Or.inl rfl)))]; exact E1.j.1.cscC c hc + · intro y hy; rcases hcap5_cases y hy with h | h | h <;> rw [h] + · exact E1.j.1.cv y hy + · trivial + · trivial + · intro y hy p mp asid h + rcases hcap5_cases y hy with h' | h' | h' <;> rw [h'] at h + · exact E1.j.1.pml y hy p mp asid h + · cases h + · cases h + -- Extension records: `VcpuCoreA`, `Stage2CoreA`, `HighVSpaceCoreA` + have hVc6 : VcpuCoreA (L := L) Z' s5 σ6 := by + refine ⟨fun v hv => ?_, fun q x hq => ?_, fun t ht => ?_, fun y hy p h => ?_⟩ + · obtain ⟨x, hx1, h1, h2⟩ := E1.j.2.1.obj v hv + have hkh : σ6.kheap (specPtr v) = some (.archObj (.vcpu x)) := by + rw [hkh6_nonSlot (specPtr v) + (not_mem_detype_of_disjA (by omega) hfrAl hfrFit (hdisj v 4096 (Or.inl ⟨rfl, Or.inr (Or.inr (Or.inr (Or.inl hv)))⟩))) + (hkh4_ne v 4096 (by omega) (Or.inl ⟨rfl, Or.inr (Or.inr (Or.inr (Or.inl hv)))⟩)) + (fun _ _ h => by rw [hx1] at h; cases h) (fun _ h => by rw [hx1] at h; cases h), hx1] + refine ⟨x, hkh, ?_, ?_⟩ + · rw [hfn_s5 v (.vcpu v v hv (Or.inl rfl)) + (fun e => E.vsVc _ (.asid i hi) ⟨v, hv, 0#u64, by decide, by decide, by rw [hwa0]; exact e.symm⟩)] + exact h1 + · rw [hfn_s5 _ (.vcpu v (wa v 24#u64) hv (Or.inr rfl)) + (fun e => E.vsVc _ (.asid i hi) ⟨v, hv, 24#u64, by decide, by decide, e.symm⟩)] + exact h2 + · -- back + have hq4 : σ4.kheap q = some (.archObj (.vcpu x)) := by + rcases hSS5.kh q with h | ⟨_, _, _, _, h2⟩ | ⟨_, _, _, h2, _⟩ + · exact h.symm.trans hq + · rw [h2] at hq; cases hq + · rw [h2] at hq; cases hq + have hq3 : σ3.kheap q = some (.archObj (.vcpu x)) := by + by_cases he : q = specPtr frame + · subst he; simp [σ4] at hq4 + · simpa [σ4, Function.update_of_ne he] using hq4 + have hq2 : σ2.kheap q = some (.archObj (.vcpu x)) := by + rcases hSS3.kh q with h | ⟨_, _, _, _, h2⟩ | ⟨_, _, _, h2, _⟩ + · exact h.symm.trans hq3 + · rw [h2] at hq3; cases hq3 + · rw [h2] at hq3; cases hq3 + exact E1.j.2.1.back q x (hdet_inv q _ hq2) + · obtain ⟨x1, x6, hx1, hx6, hnc⟩ := hkh6_tcb t ((E1.j.1.thT t).2 ht) + obtain ⟨_, _, _, _, har', hex'⟩ := tcbNcA_eq hnc + obtain ⟨x1', hx1', h1, h2⟩ := E1.j.2.1.tcb t ht + rw [hx1] at hx1'; cases Option.some.inj hx1' + have hfit := E.j.1.place.fit t .tcb ((E.j.1.thT t).2 ht) + have hts : ObjKindA.tcb.size = 4096 := rfl + rw [hts] at hfit + have h1v : (wa t 3664#u64).val = t.val + 3664 := by + rw [wa_valP, show (3664#u64 : U64).val = 3664 from rfl, Nat.mod_eq_of_lt (by omega)] + have h2v : (runVcpuA t).val = t.val + 3672 := by + unfold runVcpuA; rw [wa_valP, show (3672#u64 : U64).val = 3672 from rfl, Nat.mod_eq_of_lt (by omega)] + refine ⟨x6, hx6, ?_, ?_⟩ + · rw [har', hfn_s5 _ (.tcb t (wa t 3664#u64) ht (Or.inr (Or.inr (Or.inr (Or.inl rfl))))) + (hAtOffHeap _ (Or.inl ⟨t, ht, by omega, by rw [hts]; omega⟩))] + exact h1 + · rw [hex', hfn_s5 _ (.tcb t (runVcpuA t) ht (Or.inr (Or.inr (Or.inr (Or.inr rfl))))) + (hAtOffHeap _ (Or.inl ⟨t, ht, by omega, by rw [hts]; omega⟩))] + exact h2 + · rcases hcap5_cases y hy with h' | h' | h' <;> rw [h'] at h + · exact E1.j.2.1.cap y hy p h + · cases h + · cases h + have hS26 : Stage2CoreA (L := L) Z' s5 σ6 := by + refine ⟨fun r hr => ?_, fun q es hq => ?_, fun y hy p l mp asid va h => ?_⟩ + · obtain ⟨es, hes1⟩ := E1.j.2.2.1.obj r hr + exact ⟨es, by rw [hkh6_nonSlot (specPtr r) + (not_mem_detype_of_disjA (by omega) hfrAl hfrFit (hdisj r 8192 (Or.inr (Or.inl ⟨rfl, Or.inl hr⟩)))) + (hkh4_ne r 8192 (by omega) (Or.inr (Or.inl ⟨rfl, Or.inl hr⟩))) + (fun _ _ h => by rw [hes1] at h; cases h) (fun _ h => by rw [hes1] at h; cases h), hes1]⟩ + · have hq4 : σ4.kheap q = some (.archObj (.ext (.stage2Root es))) := by + rcases hSS5.kh q with h | ⟨_, _, _, _, h2⟩ | ⟨_, _, _, h2, _⟩ + · exact h.symm.trans hq + · rw [h2] at hq; cases hq + · rw [h2] at hq; cases hq + have hq3 : σ3.kheap q = some (.archObj (.ext (.stage2Root es))) := by + by_cases he : q = specPtr frame + · subst he; simp [σ4] at hq4 + · simpa [σ4, Function.update_of_ne he] using hq4 + have hq2 : σ2.kheap q = some (.archObj (.ext (.stage2Root es))) := by + rcases hSS3.kh q with h | ⟨_, _, _, _, h2⟩ | ⟨_, _, _, h2, _⟩ + · exact h.symm.trans hq3 + · rw [h2] at hq3; cases hq3 + · rw [h2] at hq3; cases hq3 + exact E1.j.2.2.1.back q es (hdet_inv q _ hq2) + · rcases hcap5_cases y hy with h' | h' | h' <;> rw [h'] at h + · exact E1.j.2.2.1.cap y hy p l mp asid va h + · cases h + · cases h + have hHv6 : HighVSpaceCoreA (L := L) Z' s5 σ6 := by + refine ⟨fun r hr => ?_, fun q l1 hq => ?_, fun y hy p mp asid h => ?_⟩ + · obtain ⟨l1, hl1⟩ := E1.j.2.2.2.obj r hr + exact ⟨l1, by rw [hkh6_nonSlot (specPtr r) + (not_mem_detype_of_disjA (by omega) hfrAl hfrFit (hdisj r 8192 (Or.inr (Or.inl ⟨rfl, Or.inr hr⟩)))) + (hkh4_ne r 8192 (by omega) (Or.inr (Or.inl ⟨rfl, Or.inr hr⟩))) + (fun _ _ h => by rw [hl1] at h; cases h) (fun _ h => by rw [hl1] at h; cases h), hl1]⟩ + · have hq4 : σ4.kheap q = some (.archObj (.ext (.highVSpace l1))) := by + rcases hSS5.kh q with h | ⟨_, _, _, _, h2⟩ | ⟨_, _, _, h2, _⟩ + · exact h.symm.trans hq + · rw [h2] at hq; cases hq + · rw [h2] at hq; cases hq + have hq3 : σ3.kheap q = some (.archObj (.ext (.highVSpace l1))) := by + by_cases he : q = specPtr frame + · subst he; simp [σ4] at hq4 + · simpa [σ4, Function.update_of_ne he] using hq4 + have hq2 : σ2.kheap q = some (.archObj (.ext (.highVSpace l1))) := by + rcases hSS3.kh q with h | ⟨_, _, _, _, h2⟩ | ⟨_, _, _, h2, _⟩ + · exact h.symm.trans hq3 + · rw [h2] at hq3; cases hq3 + · rw [h2] at hq3; cases hq3 + exact E1.j.2.2.2.back q l1 (hdet_inv q _ hq2) + · rcases hcap5_cases y hy with h' | h' | h' <;> rw [h'] at h + · exact E1.j.2.2.2.cap y hy p mp asid h + · cases h + · cases h + exact ⟨hJAt6, hVc6, hS26, hHv6⟩ + -- Build `CarryA` preservation + have hCarry : CarryA Z (L.mem s) σ → CarryA Z' (L.mem s5) σ6 := by + intro hC + refine ⟨hC.1.of_capsA rfl (fun _ h => h) (fun _ h => h) (fun y hy => ?_), ?_⟩ + · rcases hcap5_cases y hy with h | h | h + · exact Or.inl (by rw [h]; show capA st s1 y = capA st s y; exact hOS1.capA (RS.sub y hy)) + · exact Or.inr (fun _ _ _ he => by rw [h] at he; cases he) + · exact Or.inr (fun _ _ _ he => by rw [h] at he; cases he) + · have hty : TyKeepA σ σ6 := by + have h10 := TyKeepA.stUpdA hkT .restart + have h21 : TyKeepA σ1 σ2 := + ⟨fun o r cs ho => ⟨cs, hdet_inv o _ ho⟩, fun o t ho => ⟨t, hdet_inv o _ ho⟩⟩ + have h32 : TyKeepA σ2 σ3 := by + refine ⟨fun o r cs ho => ?_, fun o t ho => ?_⟩ + · rcases hSS3.kh o with h | ⟨r', c1, c2, h1, h2⟩ | ⟨_, _, _, h2, _⟩ + · exact ⟨cs, h ▸ ho⟩ + · rw [h2] at ho; cases Option.some.inj ho; exact ⟨c1, h1⟩ + · rw [h2] at ho; cases ho + · rcases hSS3.kh o with h | ⟨_, _, _, _, h2⟩ | ⟨t1, t2, h1, h2, _⟩ + · exact ⟨t, h ▸ ho⟩ + · rw [h2] at ho; cases ho + · rw [h2] at ho; cases Option.some.inj ho; exact ⟨t1, h1⟩ + have h43 : TyKeepA σ3 σ4 := by + refine ⟨fun o r cs ho => ?_, fun o t ho => ?_⟩ + · by_cases he : o = specPtr frame + · simp [σ4, he] at ho + · simp [σ4, he] at ho; exact ⟨cs, ho⟩ + · by_cases he : o = specPtr frame + · simp [σ4, he] at ho + · simp [σ4, he] at ho; exact ⟨t, ho⟩ + have h54 : TyKeepA σ4 σ5 := by + refine ⟨fun o r cs ho => ?_, fun o t ho => ?_⟩ + · rcases hSS5.kh o with h | ⟨r', c1, c2, h1, h2⟩ | ⟨_, _, _, h2, _⟩ + · exact ⟨cs, h ▸ ho⟩ + · rw [h2] at ho; cases Option.some.inj ho; exact ⟨c1, h1⟩ + · rw [h2] at ho; cases ho + · rcases hSS5.kh o with h | ⟨_, _, _, _, h2⟩ | ⟨t1, t2, h1, h2, _⟩ + · exact ⟨t, h ▸ ho⟩ + · rw [h2] at ho; cases ho + · rw [h2] at ho; cases Option.some.inj ho; exact ⟨t1, h1⟩ + exact ((((h10.trans h21).trans h32).trans h43).trans h54).trans (TyKeepA.of_kheap rfl) + have hfm3 : FinMA (L.mem s3) Z.G.chains := by + have hok3 : ∀ y ∈ Z.G.chains.flatten, okeyA (capAtA (L.mem s3) y) = okeyA (capAtA (L.mem s) y) := fun y hy => by + have hyO := ((hTr.lk.members y).1 hy).1 + show okeyA (capA st s3 y) = okeyA (capA st s y) + rw [hcap3_all y hyO] + by_cases e : y = cslotAddrA parent + · subst e; rw [hcp2, hcp]; rfl + · rw [hko2 y hyO e, hOS1.capA hyO] + have hzb3 : ∀ y ∈ Z.G.chains.flatten, isZbA (capAtA (L.mem s3) y) = isZbA (capAtA (L.mem s) y) := fun y hy => by + have hyO := ((hTr.lk.members y).1 hy).1 + show isZbA (capA st s3 y) = isZbA (capA st s y) + rw [hcap3_all y hyO] + by_cases e : y = cslotAddrA parent + · subst e; rw [hcp2, hcp]; rfl + · rw [hko2 y hyO e, hOS1.capA hyO] + have hfr3 : ∀ y ∈ Z.G.chains.flatten, isFrameA (capAtA (L.mem s3) y) = isFrameA (capAtA (L.mem s) y) := fun y hy => by + have hyO := ((hTr.lk.members y).1 hy).1 + show isFrameA (capA st s3 y) = isFrameA (capA st s y) + rw [hcap3_all y hyO] + by_cases e : y = cslotAddrA parent + · subst e; rw [hcp2, hcp]; rfl + · rw [hko2 y hyO e, hOS1.capA hyO] + have hgr3 : ∀ y ∈ Z.G.chains.flatten, grefA (capAtA (L.mem s3) y) = grefA (capAtA (L.mem s) y) := fun y hy => by + have hyO := ((hTr.lk.members y).1 hy).1 + show grefA (capA st s3 y) = grefA (capA st s y) + rw [hcap3_all y hyO] + by_cases e : y = cslotAddrA parent + · subst e; rw [hcp2, hcp]; rfl + · rw [hko2 y hyO e, hOS1.capA hyO] + have hov3 : ∀ x ∈ Z.G.chains.flatten, ∀ y ∈ Z.G.chains.flatten, + OvlA (absCapA (capAtA (L.mem s3) x)) (absCapA (capAtA (L.mem s3) y)) ↔ + OvlA (absCapA (capAtA (L.mem s) x)) (absCapA (capAtA (L.mem s) y)) := fun x hx y hy => by + simp only [ovlA_iff, hgr3 x hx, hgr3 y hy] + refine ⟨fun ch1 h1 ch2 h2 x hx y hy ho => + hC.2.fm.conn ch1 h1 ch2 h2 x hx y hy + ((hov3 x (List.mem_flatten.2 ⟨ch1, h1, hx⟩) y (List.mem_flatten.2 ⟨ch2, h2, hy⟩)).1 ho), + fun ch hch i j k y z y' hy hz hy' hij hjk heq hne hnf => by + have hyf := List.mem_flatten.2 ⟨ch, hch, List.mem_of_getElem? hy⟩ + have hzf := List.mem_flatten.2 ⟨ch, hch, List.mem_of_getElem? hz⟩ + have hy'f := List.mem_flatten.2 ⟨ch, hch, List.mem_of_getElem? hy'⟩ + rw [hok3 z hzf, hok3 y hyf] + rw [hok3 y hyf, hok3 y' hy'f] at heq + rw [hok3 y hyf] at hne hnf + exact hC.2.fm.chunk ch hch i j k y z y' hy hz hy' hij hjk heq hne hnf, + fun x hx y hy hne ho hzx hzy hnf => by + rw [hok3 x hx, hok3 y hy] + exact hC.2.fm.key x hx y hy hne ((hov3 x hx y hy).1 ho) + (by rwa [← hzb3 x hx]) (by rwa [← hzb3 y hy]) (by rwa [← hfr3 x hx, ← hfr3 y hy]), + fun x hx y hy hne hzx ho => + hC.2.fm.zfin x hx y hy hne (by rwa [← hzb3 x hx]) ((hov3 x hx y hy).1 ho)⟩ + have hnoOvl3 : ∀ y ∈ Z.G.chains.flatten, + ¬ OvlA (absCapA (.AsidPool frame base)) (absCapA (capAtA (L.mem s3) y)) := by + intro y hy ho + have ⟨r, hr1, hr2⟩ := (ovlA_iff _ _).1 ho + have hrEq : r = .objRef (specPtr frame) := Option.some.inj (by simpa [grefA] using hr1.symm) + subst hrEq + have hyO := ((hTr3.lk.members y).1 hy).1 + have hphys_of : ∀ c : capability.Cap, grefA c = some (.objRef (specPtr frame)) → + physA c = true ∧ isUtA c = false ∧ baseA c = frame.val := by + intro c hc + cases c <;> simp only [grefA] at hc + all_goals try cases hc + all_goals + have hp : _ = frame := specPtr_inj (by injection hc with h1; injection h1) + subst hp + refine ⟨?_, rfl, ?_⟩ + · exact okinj ((physA_eq _).symm.trans rfl) + · unfold baseA; exact congrArg UScalar.val (okinj ((ptrA_eq _).symm.trans rfl)) + have hphys_base := hphys_of (capA st s3 y) hr2 + have hyne : y ≠ cslotAddrA parent := fun e => by + have := hphys_base.2.1; rw [e, hcp3] at this; cases this + have hdisjA : DisjA (capA st s3 y) (capA st s3 (cslotAddrA parent)) := by + by_contra hndisj + rcases hTr3.ut.drange _ hpO frame 12#u64 4096#u64 false hcp3 y hyO hyne hphys_base.1 hndisj with hd | hd + · exact hnd1 y hd + · have hut := hTr3.ut.anc y (cslotAddrA parent) hd (by rw [hcp3]; rfl) + rw [hphys_base.2.1] at hut; cases hut + have hbaseP : baseA (capA st s3 (cslotAddrA parent)) = frame.val := by + rw [hcp3]; unfold baseA; rw [ptrA_ut] + have htopP : topA (capA st s3 (cslotAddrA parent)) = frame.val + 4095 := by + rw [hcp3]; unfold topA; rw [ptrA_ut, sizeA_ut] + have hm : (mskA 12#u64).val = 4095 := mskA_val (by decide) + rw [wa_valP, hm]; exact Nat.mod_eq_of_lt (by omega) + have hregY := hTr3.ut.reg y hyO hphys_base.1 + rcases hdisjA with h1 | h2 + · rw [hbaseP] at h1; omega + · rw [htopP] at h2; omega + have hfm4 : FinMA (L.mem s4) (insG (cslotAddrA parent) (cslotAddrA dest) Z.G.chains) := + finMA_insAsidPoolA hfm3 hTr3.lk.nodup hpG3 hdG3 + (by show okeyA (capA st s3 _) = none; rw [hcp3]; rfl) + (fun y hy => hkeep4 y ((hTr3.lk.members y).1 hy).1 (fun e => hdG3 (e ▸ hy))) + hsd4 hnoOvl3 + have hfm5 : FinMA (L.mem s5) G'.chains := by + have hcap5m : ∀ y ∈ G'.chains.flatten, capAtA (L.mem s5) y = capAtA (L.mem s4) y := fun y hy => by + have hyO := ((hT5.lk.members y).1 hy).1 + exact hcap5_eq y hyO + refine ⟨fun ch1 h1 ch2 h2 x hx y hy ho => + hfm4.conn ch1 h1 ch2 h2 x hx y hy (by + rwa [hcap5m x (List.mem_flatten.2 ⟨ch1, h1, hx⟩), hcap5m y (List.mem_flatten.2 ⟨ch2, h2, hy⟩)] at ho), + fun ch hch i j k y z y' hy hz hy' hij hjk heq hne hnf => by + have hyf := List.mem_flatten.2 ⟨ch, hch, List.mem_of_getElem? hy⟩ + have hzf := List.mem_flatten.2 ⟨ch, hch, List.mem_of_getElem? hz⟩ + have hy'f := List.mem_flatten.2 ⟨ch, hch, List.mem_of_getElem? hy'⟩ + rw [hcap5m z hzf, hcap5m y hyf] + rw [hcap5m y hyf, hcap5m y' hy'f] at heq + rw [hcap5m y hyf] at hne hnf + exact hfm4.chunk ch hch i j k y z y' hy hz hy' hij hjk heq hne hnf, + fun x hx y hy hne ho hzx hzy hnf => by + rw [hcap5m x hx] at ho hzx hnf ⊢ + rw [hcap5m y hy] at ho hzy hnf ⊢ + exact hfm4.key x hx y hy hne ho hzx hzy hnf, + fun x hx y hy hne hzx ho => by + rw [hcap5m x hx] at hzx ho + rw [hcap5m y hy] at ho + exact hfm4.zfin x hx y hy hne hzx ho⟩ + exact ⟨hC.2.kt.of_typesA hty.1 hty.2, hfm5⟩ + exact (WP.spec_ok _).2 ⟨σ1, hset, σ6, [], hperf, Z', rfl, rfl, hJ6, hTr5, hCarry⟩ + +end perform + +/-! ## Complete `Cap::AsidControl` top (`decInv_asidControl_all_jA`) on `ArchTopPostA` -/ + +section asid_ctrl_top +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +set_option maxRecDepth 8000 in +private theorem labNA_ne_makePool_lt74 : ∀ n, n < 74 → n ≠ 58 → + labNA n ≠ Fl4SpecA64.archL .armASIDControlMakePool := by + decide + +private theorem invType_specLabelWord_eqA {l : Fl4SpecA64.Fl4Label} + (h : Fl4SpecA64.invocationType (Fl4SpecA64.specLabelWord l) = + .archInvocationLabel .armASIDControlMakePool) : + l = Fl4SpecA64.archL .armASIDControlMakePool := by + cases l with + | spec l' => + unfold Fl4SpecA64.specLabelWord at h + rw [invocationType_fromEnumA l'] at h + rw [h] + | ext e => + unfold Fl4SpecA64.specLabelWord at h + have h0 : Fl4SpecA64.invocationType (0 : Fl4SpecA64.Word) = .genInvocationLabel .invalidInvocation := + invocationType_fromEnumA (.genInvocationLabel .invalidInvocation) + rw [h0] at h; cases h + +private theorem extra01_eqA {ex : Aeneas.Std.Array obj.Slot 3#usize} : + vspace.extra ex 0#usize = ok (if exAtA ex 0 = 0#u64 then none else some (exAtA ex 0)) ∧ + vspace.extra ex 1#usize = ok (if exAtA ex 1 = 0#u64 then none else some (exAtA ex 1)) := by + constructor + · exact extra0_eqA (exAtA_idx0 ex) + · unfold vspace.extra obj.Slot.is_null + have hidx : ex.index_usize 1#usize = ok (exAtA ex 1) := exAtA_idx1 ex + simp only [show ¬(1#usize ≥ 3#usize) by decide, ↓reduceIte, hidx, bind_ok] + by_cases h : exAtA ex 1 = 0#u64 <;> simp [h] + +set_option maxHeartbeats 2000000 in +/-- **Complete `Cap::AsidControl` top** (`ARMASIDControlMakePool` label 58 and illegal labels) +at `syscall.decode_invocation` against `fl4ArchDecodeInvocation` on `ArchTopPostA`. -/ +theorem decInv_asidControl_all_jA (ar : machine.Arch A) + (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) + (hT : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s)) + (hYAs : ∀ y ∈ Z.Y, ∀ j : U64, j.val < 512 → ¬ AsW (L.ksb s) (peAF y j)) + (hTr : TreeA st OS Z.G.chains Z.G.par s) + (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) + (hcO : ∀ y ∈ OS, obj.sext48 y = ok y) + (hD : ∀ u frame fi, capA st s u = .Untyped frame 12#u64 fi false → + cdt.ensure_no_children st s u = ok (core.result.Result.Ok ()) → DeadDrop4KA Z H frame fi) + (lbl length buf : U64) (cte : Fl4SpecA64.CslotPtr) + (block call cd fp calling : Bool) (ex : Aeneas.Std.Array obj.Slot 3#usize) + (args : List U64) (xSlot : Fl4SpecA64.CapRef) (exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) + (hlen : length.val = args.length) + (hargs : ∀ (k : U64) (v : U64), args[k.val]? = some v → msg.syscall_arg st s (L.curW s) buf k = ok v) + (hex : ExtraOKA Z.th (L.mem s) ex exs) : + syscall.decode_invocation st ar s a lbl length (cslotAddrA cte) .AsidControl + block call cd fp buf ex ⦃ res => + ArchTopPostA (L := L) AR Z OS m calling σ ((Fl4SpecA64.fl4ArchDecodeInvocation (Z := Unit) calling + (Fl4SpecA64.fl4LabelOf lbl.bv) (args.map UScalar.bv) xSlot cte .asidControlCap exs).run σ) s a res ⦄ := by + rw [dec_asidCtrlA] + unfold syscall.decode_invocation vspace.decode + rw [cur_thread_ok, bind_ok] + by_cases hlbl : lbl = fl4_abi.label.ARMASIDControlMakePool + · subst hlbl + have hl : Fl4SpecA64.invocationType (Fl4SpecA64.specLabelWord + (Fl4SpecA64.fl4LabelOf fl4_abi.label.ARMASIDControlMakePool.bv)) = + .archInvocationLabel .armASIDControlMakePool := by + rw [fl4LabelOf_bvA, Fl4ProofsA64.P0.Consts.fl4_abi.label.ARMASIDControlMakePool.val_eq, + show labNA 58 = Fl4SpecA64.archL .armASIDControlMakePool from rfl] + exact invocationType_fromEnumA _ + simp only [bne_self_eq_false, Bool.false_eq_true, ↓reduceIte, extra01_eqA.1, extra01_eqA.2, bind_ok] + -- Check extra caps 0 and 1 and `length >= 2` + by_cases hx0z : exAtA ex 0 = 0#u64 + · rw [if_pos hx0z]; dsimp only; simp only [error.truncated, bind_ok] + have hexLt : ¬ (exs.length > 1) := by + intro hgt + obtain ⟨ps, hth, hy1, haddr, -⟩ := hex.2.1 0 (by omega) + have hAb : AbsSlotA Z.th (exs[0]'(by omega)).2 := ⟨ps, hy1, Or.inl hth⟩ + exact E.sys.ent.os.nz _ (RS.sub _ (hAb.isSlot E.j.1.place)) (haddr.symm.trans hx0z) + refine (WP.spec_ok _).2 ⟨rfl, rfl, .truncatedMessage, run_liftME_errA ?_, Or.inl trivial⟩ + unfold Fl4SpecA64.decodeAsidControlInvocation + rw [if_pos hl, if_neg (fun h => hexLt h.2)]; rfl + rw [if_neg hx0z]; dsimp only + by_cases hx1z : exAtA ex 1 = 0#u64 + · rw [if_pos hx1z]; dsimp only; simp only [error.truncated, bind_ok] + have hexLt : ¬ (exs.length > 1) := by + intro hgt + obtain ⟨ps, hth, hy1, haddr, -⟩ := hex.2.1 1 hgt + have hAb : AbsSlotA Z.th (exs[1]'hgt).2 := ⟨ps, hy1, Or.inl hth⟩ + exact E.sys.ent.os.nz _ (RS.sub _ (hAb.isSlot E.j.1.place)) (haddr.symm.trans hx1z) + refine (WP.spec_ok _).2 ⟨rfl, rfl, .truncatedMessage, run_liftME_errA ?_, Or.inl trivial⟩ + unfold Fl4SpecA64.decodeAsidControlInvocation + rw [if_pos hl, if_neg (fun h => hexLt h.2)]; rfl + rw [if_neg hx1z]; dsimp only + have hexGt1 : exs.length > 1 := by + by_contra hle; push_neg at hle + exact hx1z (hex.2.2 1 (by omega) (by omega)) + by_cases hlen2 : length ≥ 2#u64 + swap + · rw [if_neg hlen2]; simp only [error.truncated, bind_ok] + have hargLt : ¬ ((args.map UScalar.bv).length > 1) := by + simp only [List.length_map]; have : ¬ (2 ≤ length.val) := hlen2; omega + refine (WP.spec_ok _).2 ⟨rfl, rfl, .truncatedMessage, run_liftME_errA ?_, Or.inl trivial⟩ + unfold Fl4SpecA64.decodeAsidControlInvocation + rw [if_pos hl, if_neg (fun h => hargLt h.1)]; rfl + rw [if_pos hlen2] + have hge2 : 2 ≤ args.length := by have : 2 ≤ length.val := hlen2; omega + obtain ⟨a0, a1, arest, rfl⟩ : ∃ a0 a1 arest, args = a0 :: a1 :: arest := by + match args, hge2 with | a0 :: a1 :: arest, _ => exact ⟨a0, a1, arest, rfl⟩ + obtain ⟨e0, e1, erest, rfl⟩ : ∃ e0 e1 erest, exs = e0 :: e1 :: erest := by + match exs, hexGt1 with | e0 :: e1 :: erest, _ => exact ⟨e0, e1, erest, rfl⟩ + have hcond : (List.map UScalar.bv (a0 :: a1 :: arest)).length > 1 ∧ (e0 :: e1 :: erest).length > 1 := by simp + have hsrc0 := hex.2.1 0 (by simp) + have hsrc1 := hex.2.1 1 (by simp) + obtain ⟨ps0, hth0, hy0, haddr0, hcr0⟩ := hsrc0 + obtain ⟨ps1, hth1, hy1, haddr1, hcr1⟩ := hsrc1 + simp only [List.getElem_cons_zero, List.getElem_cons_succ] at hth0 hy0 haddr0 hcr0 hth1 hy1 haddr1 hcr1 + have hAp : AbsSlotA Z.th e0.2 := ⟨ps0, hy0, Or.inl hth0⟩ + have hAr : AbsSlotA Z.th e1.2 := ⟨ps1, hy1, Or.inl hth1⟩ + rw [hargs 0#u64 a0 rfl, bind_ok, hargs 1#u64 a1 rfl, bind_ok, capA_eq, bind_ok, capA_eq, bind_ok] + -- `decode_loop0` + obtain ⟨i, hiOk, hiLe, hiAll, hiZ⟩ := spec_ex_fd (decode_loop0_specA (st := st) s) + rw [hiOk, bind_ok] + unfold vspace.N_ASID_POOLS + have h128 : (1#u64 <<< fl4_abi.consts.bits.NUM_ASID_POOLS : Result U64) = ok 128#u64 := by + apply eq_of_spec_fd; unfold fl4_abi.consts.bits.NUM_ASID_POOLS; step* + rw [h128, bind_ok] + by_cases hfull : i = 128#u64 + · subst hfull + rw [if_pos rfl] + have hEmp := asidTable_full_of_loop0A E.j.1.vs hiAll + refine (WP.spec_ok _).2 ⟨rfl, rfl, .deleteFirst, run_liftME_errA ?_, Or.inl trivial⟩ + unfold Fl4SpecA64.decodeAsidControlInvocation + rw [if_pos hl, if_pos hcond] + rw [run_bind_okD (run_liftE_stD (m := Fl4Spec.NonDet.gets _) rfl)] + dsimp only [Fl4Spec.NonDetE.returnOk]; rw [ndE_pure_bindD] + unfold Fl4Spec.NonDetE.whenE; rw [if_pos hEmp] + exact run_bind_errorD (run_throwErrorD _ σ) _ + rw [if_neg hfull] + have hiLt : i.val < 128 := by + have : i.val ≠ 128 := fun e => hfull (UScalar.eq_of_val_eq e); omega + have hiFree := asidTable_free_of_loop0A E.j.1.vs hiLt (hiZ hiLt) + have hNotEmp : (Fl4SpecA64.dom (Fl4SpecA64.asidTable σ))ᶜ ≠ ∅ := + fun he => by rw [he] at hiFree; exact Set.notMem_empty _ hiFree + obtain ⟨base, hbaseOk, hbaseVal⟩ := spec_ex_fd (shl9_val hiLt) + rw [hbaseOk, bind_ok] + -- Helper: reduce `decodeAsidControlInvocation` to the `selectExt >>= ...` form + have hDecPrefix : ∀ (cont : Fl4SpecA64.AsidHighIndex → Fl4SpecA64.SEMonad Unit Fl4SpecA64.ArchInvocation), + (Fl4SpecA64.decodeAsidControlInvocation (Z := Unit) + (Fl4SpecA64.specLabelWord (Fl4SpecA64.fl4LabelOf fl4_abi.label.ARMASIDControlMakePool.bv)) + (List.map UScalar.bv (a0 :: a1 :: arest)) cte .asidControlCap (e0 :: e1 :: erest)).run σ = + ((Fl4Spec.NonDetE.liftE (Fl4SpecA64.selectExt (Z := Unit) + (fun _ => Fl4SpecA64.freeAsidSelect (Fl4SpecA64.asidTable σ)) + (Fl4SpecA64.dom (Fl4SpecA64.asidTable σ))ᶜ)) >>= fun free => do + let base' ← pure ((Fl4SpecA64.ucast Fl4SpecA64.asidRepLen free : Fl4SpecA64.Asid) <<< Fl4SpecA64.asidLowBits) + let (p, n) ← match e0.1 with + | .untypedCap false p n _ => pure (p, n) + | _ => Fl4Spec.NonDetE.throwError (.invalidCapability 1) + let frame ← if n = Fl4SpecA64.pageBits then do + Fl4SpecA64.ensureNoChildren e0.2 + pure p + else Fl4Spec.NonDetE.throwError (.invalidCapability 1) + let destSlot ← Fl4SpecA64.lookupTargetSlot e1.1 (Fl4SpecA64.toBl a0.bv) (Fl4SpecA64.unat a1.bv) + Fl4SpecA64.ensureEmpty destSlot + pure (.invokeASIDControl (.makePool frame destSlot e0.2 base'))).run σ := by + intro _ + unfold Fl4SpecA64.decodeAsidControlInvocation + rw [if_pos hl, if_pos hcond] + rw [run_bind_okD (run_liftE_stD (m := Fl4Spec.NonDet.gets _) rfl)] + dsimp only [Fl4Spec.NonDetE.returnOk]; rw [ndE_pure_bindD] + unfold Fl4Spec.NonDetE.whenE; rw [if_neg hNotEmp, ndE_pure_bindD] + rfl + -- Match on `untyped := capA st s (exAtA ex 0)` + cases hcu : capA st s (exAtA ex 0) with + | Untyped ptr size_bits fi is_device => + have he0_eq : e0.1 = .untypedCap is_device (specPtr ptr) size_bits.val fi.val := by + have h := hcr0.2; change CapEqA (capA st s (exAtA ex 0)) e0.1 at h + rw [hcu] at h; exact h.symm + cases is_device with + | true => + simp only [↓reduceIte, error.invalid_cap, bind_ok] + refine (WP.spec_ok _).2 ⟨rfl, rfl, .invalidCapability 1, run_liftME_errA ?_, Or.inr ⟨1#u64, rfl, rfl⟩⟩ + rw [hDecPrefix (fun _ => Fl4Spec.NonDetE.throwError (.invalidCapability 1))] + refine selectExt_bind_errA _ hiFree fun free _ => ?_ + rw [ndE_pure_bindD, he0_eq]; dsimp only + exact run_bind_errorD (run_throwErrorD _ σ) _ + | false => + simp only [Bool.false_eq_true, ↓reduceIte] + by_cases hsz : size_bits = fl4_abi.consts.bits.ASID_POOL + swap + · rw [if_neg hsz]; simp only [error.invalid_cap, bind_ok] + have hne12 : size_bits.val ≠ Fl4SpecA64.pageBits := fun e => + hsz (UScalar.eq_of_val_eq (by simpa [Fl4SpecA64.pageBits, fl4_abi.consts.bits.ASID_POOL] using e)) + refine (WP.spec_ok _).2 ⟨rfl, rfl, .invalidCapability 1, run_liftME_errA ?_, Or.inr ⟨1#u64, rfl, rfl⟩⟩ + rw [hDecPrefix (fun _ => Fl4Spec.NonDetE.throwError (.invalidCapability 1))] + refine selectExt_bind_errA _ hiFree fun free _ => ?_ + rw [ndE_pure_bindD, he0_eq]; dsimp only + rw [ndE_pure_bindD, if_neg hne12] + exact run_bind_errorD (run_throwErrorD _ σ) _ + rw [if_pos hsz] + have hsz12 : size_bits = 12#u64 := UScalar.eq_of_val_eq (by rw [hsz]; simp [fl4_abi.consts.bits.ASID_POOL]) + subst hsz12 + have hpb : (specPtr ptr, (12#u64 : U64).val).2 = Fl4SpecA64.pageBits := rfl + -- `cdt.ensure_no_children` + have hRev := E.sys.sysl.sys.stz.1.2.1 + have hpO : cslotAddrA e0.2 ∈ OS := RS.sub _ (hAp.isSlot E.j.1.place) + have hcu' : capA st s (cslotAddrA e0.2) = .Untyped ptr 12#u64 fi false := by rw [← haddr0]; exact hcu + have hncRel := noChildren_of_adjA E.j.1.place E.sys.ent.os hTr E.j.1.cdt hAp hpO hcu' + (hRev _ hpO (by rw [hcu']; rfl)) + rw [haddr0] + by_cases hhasC : ∃ c, σ.cdt c = some e0.2 + · have hncErr : cdt.ensure_no_children st s (cslotAddrA e0.2) = + ok (core.result.Result.Err (error.Exc.Sys error.SysError.RevokeFirst)) := by + unfold NoChildrenRelA at hncRel; rw [if_pos hhasC] at hncRel; exact hncRel + rw [hncErr, bind_ok] + simp only [core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame, + core.convert.FromSame.from, bind_ok, bind_tc_ok] + refine (WP.spec_ok _).2 ⟨rfl, rfl, .revokeFirst, run_liftME_errA ?_, Or.inl trivial⟩ + rw [hDecPrefix (fun _ => Fl4Spec.NonDetE.throwError .revokeFirst)] + refine selectExt_bind_errA _ hiFree fun free _ => ?_ + rw [ndE_pure_bindD, he0_eq]; dsimp only + rw [ndE_pure_bindD, if_pos hpb] + exact run_bind_errorD (by rw [run_ensureNoChildrenA, if_pos hhasC]) _ + · have hncOk : cdt.ensure_no_children st s (cslotAddrA e0.2) = ok (core.result.Result.Ok ()) := by + unfold NoChildrenRelA at hncRel; rw [if_neg hhasC] at hncRel; exact hncRel + rw [hncOk, bind_ok] + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok] + -- `cspace.lookup_target_slot` + have hlt := lookup_slot_for_cnode_op_refinesA Z.th s σ E.j.1.st E.j.1.cv false + (capA st s (exAtA ex 1)) e1.1 hcr1 (E.j.1.cv _ (by rw [haddr1]; exact hAr.isSlot E.j.1.place)) a0 a1 + obtain ⟨rl, hrl, e_l, he_l, hrel_l⟩ := spec_ex_fd hlt + have he_l' : (Fl4SpecA64.lookupTargetSlot (Z := Unit) e1.1 (Fl4SpecA64.toBl a0.bv) (Fl4SpecA64.unat a1.bv)).run σ = + ({(e_l, σ)}, False) := he_l + rw [show cspace.lookup_target_slot st s (capA st s (exAtA ex 1)) a0 a1 = + cspace.lookup_slot_for_cnode_op st s false (capA st s (exAtA ex 1)) a0 a1 from rfl, hrl, bind_ok] + cases rl with + | Err e => + cases e_l with + | ok _ => exact hrel_l.elim + | error e' => + simp only [core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame, + core.convert.FromSame.from, bind_ok, bind_tc_ok] + refine (WP.spec_ok _).2 ⟨rfl, rfl, e', run_liftME_errA ?_, Or.inl hrel_l⟩ + rw [hDecPrefix (fun _ => Fl4Spec.NonDetE.throwError e')] + refine selectExt_bind_errA _ hiFree fun free _ => ?_ + rw [ndE_pure_bindD, he0_eq]; dsimp only + rw [ndE_pure_bindD, if_pos hpb, run_bind_okD (by rw [run_ensureNoChildrenA, if_neg hhasC]), ndE_pure_bindD] + exact run_bind_errorD he_l' _ + | Ok dst => + cases e_l with + | error _ => exact hrel_l.elim + | ok dest => + obtain ⟨hdstEq, pCn, hpCn1, hpCnTh, _⟩ := hrel_l + subst hdstEq + have hAd : AbsSlotA Z.th dest := ⟨pCn, hpCn1, Or.inl hpCnTh⟩ + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok] + -- `cdt.ensure_empty_slot` + obtain ⟨ad, hadRel, hgd⟩ := E.j.1.st.slot_capA hpCn1 hpCnTh + have hee := ensureEmpty_runA dest σ hgd + unfold cdt.ensure_empty_slot + rw [capA_eq, bind_ok] + by_cases hdn : capA st s (cslotAddrA dest) = .Null + · have hadn : ad = .nullCap := by have h := hadRel.2; change CapEqA (capA st s _) ad at h; rw [hdn] at h; exact h.symm + simp only [hadn, ↓reduceIte] at hee + rw [hdn, cap_is_null_nullA] + simp only [bind_ok, ↓reduceIte, core.result.Result.Insts.CoreOpsTry.branch] + -- `1#u64 <<< (12#u64 &&& 63#u64)` = `4096#u64` + have hsh12 : (1#u64 <<< (12#u64 &&& 63#u64)) ⦃ w => w = 4096#u64 ⦄ := by + step as ⟨w, hw⟩; apply UScalar.eq_of_val_eq; simpa [U64.size, U64.numBits] using hw + simp only [lift, bind_ok, eq_of_spec_fd hsh12] + have hperf := makePool_perform_jA E RS hT hYAs hTr hnul hcO hAp hAd hcu' hncOk hdn hiLt (hiZ hiLt) hbaseVal + (hD _ ptr fi hcu' hncOk) + refine WP.spec_mono hperf fun res ⟨σ1, hset, σ', rv, hp, Z', hresEq, _, hJ', hT', hC'⟩ => ?_ + rcases res with ⟨r, s', a'⟩ + rcases hresEq with ⟨rfl, rfl, rfl⟩ + have ⟨_, _, hbaseSpec⟩ := asid_high_low_of_shl9A hiLt hbaseVal + have hDecOk : (.ok (.spec (.invokeASIDControl (.makePool (specPtr ptr) dest e0.2 (asidOfA base)))), σ) ∈ + ((Fl4Spec.NonDetE.liftME Fl4SpecA64.Fl4ArchInvocation.spec + (Fl4SpecA64.decodeAsidControlInvocation (Z := Unit) + (Fl4SpecA64.specLabelWord (Fl4SpecA64.fl4LabelOf fl4_abi.label.ARMASIDControlMakePool.bv)) + (List.map UScalar.bv (a0 :: a1 :: arest)) cte .asidControlCap (e0 :: e1 :: erest))).run σ).1 ∧ + ¬ ((Fl4Spec.NonDetE.liftME Fl4SpecA64.Fl4ArchInvocation.spec + (Fl4SpecA64.decodeAsidControlInvocation (Z := Unit) + (Fl4SpecA64.specLabelWord (Fl4SpecA64.fl4LabelOf fl4_abi.label.ARMASIDControlMakePool.bv)) + (List.map UScalar.bv (a0 :: a1 :: arest)) cte .asidControlCap (e0 :: e1 :: erest))).run σ).2 := by + have hsub : (.ok (.invokeASIDControl (.makePool (specPtr ptr) dest e0.2 (asidOfA base))), σ) ∈ + ((Fl4SpecA64.decodeAsidControlInvocation (Z := Unit) + (Fl4SpecA64.specLabelWord (Fl4SpecA64.fl4LabelOf fl4_abi.label.ARMASIDControlMakePool.bv)) + (List.map UScalar.bv (a0 :: a1 :: arest)) cte .asidControlCap (e0 :: e1 :: erest)).run σ).1 ∧ + ¬ ((Fl4SpecA64.decodeAsidControlInvocation (Z := Unit) + (Fl4SpecA64.specLabelWord (Fl4SpecA64.fl4LabelOf fl4_abi.label.ARMASIDControlMakePool.bv)) + (List.map UScalar.bv (a0 :: a1 :: arest)) cte .asidControlCap (e0 :: e1 :: erest)).run σ).2 := by + rw [hDecPrefix (fun _ => Fl4Spec.NonDetE.throwError .deleteFirst)] + refine selectExt_bind_okA _ hiFree ?_ fun free _ => ?_ + · rw [ndE_pure_bindD, he0_eq]; dsimp only + rw [ndE_pure_bindD, if_pos hpb, run_bind_okD (by rw [run_ensureNoChildrenA, if_neg hhasC]), + ndE_pure_bindD, run_bind_okD he_l', run_bind_okD hee, hbaseSpec]; rfl + · rw [ndE_pure_bindD, he0_eq]; dsimp only + rw [ndE_pure_bindD, if_pos hpb, run_bind_okD (by rw [run_ensureNoChildrenA, if_neg hhasC]), + ndE_pure_bindD, run_bind_okD he_l', run_bind_okD hee]; exact id + unfold Fl4Spec.NonDetE.liftME + rw [ExceptT.run_bind, nd_bind_applyD] + refine ⟨⟨(.ok _, σ), hsub.1, Set.mem_singleton _⟩, ?_⟩ + simp only [not_or, not_exists, not_and] + refine ⟨hsub.2, fun r _ => ?_⟩ + cases r.1 <;> exact id + exact ⟨_, hDecOk, σ1, hset, σ', rv, hp, Or.inl ⟨Z', hJ', hT', hC'⟩⟩ + · rw [cap_is_null_neA hdn] + simp only [bind_ok, Bool.false_eq_true, ↓reduceIte, core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame, + core.convert.FromSame.from, bind_tc_ok] + have hne : ad ≠ .nullCap := hadRel.ne_null hdn + simp only [hne, ↓reduceIte] at hee + refine (WP.spec_ok _).2 ⟨rfl, rfl, .deleteFirst, run_liftME_errA ?_, Or.inl trivial⟩ + rw [hDecPrefix (fun _ => Fl4Spec.NonDetE.throwError .deleteFirst)] + refine selectExt_bind_errA _ hiFree fun free _ => ?_ + rw [ndE_pure_bindD, he0_eq]; dsimp only + rw [ndE_pure_bindD, if_pos hpb, run_bind_okD (by rw [run_ensureNoChildrenA, if_neg hhasC]), + ndE_pure_bindD, run_bind_okD he_l'] + exact run_bind_errorD hee _ + | _ => + simp only [error.invalid_cap, bind_ok] + have hnotUt : ∀ p n fi, e0.1 ≠ .untypedCap false p n fi := by + intro p n fi h + have hc := hcr0.2; change CapEqA (capA st s (exAtA ex 0)) e0.1 at hc + rw [hcu, h] at hc + first | cases hc | simp [CapEqA, absCapA, absFrameE] at hc + refine (WP.spec_ok _).2 ⟨rfl, rfl, .invalidCapability 1, run_liftME_errA ?_, Or.inr ⟨1#u64, rfl, rfl⟩⟩ + rw [hDecPrefix (fun _ => Fl4Spec.NonDetE.throwError (.invalidCapability 1))] + refine selectExt_bind_errA _ hiFree fun free _ => ?_ + rw [ndE_pure_bindD] + cases h0 : e0.1 with + | untypedCap d p n fi => + cases d with + | true => dsimp only; exact run_bind_errorD (run_throwErrorD _ σ) _ + | false => exact absurd h0 (hnotUt p n fi) + | _ => dsimp only; exact run_bind_errorD (run_throwErrorD _ σ) _ + · -- `lbl ≠ ARMASIDControlMakePool` -> `IllegalOperation` + dsimp only + have hne : (lbl != fl4_abi.label.ARMASIDControlMakePool) = true := bne_iff_ne.2 hlbl + rw [bind_ok, if_pos hne]; simp only [error.illegal, bind_ok] + have hnotL : Fl4SpecA64.invocationType (Fl4SpecA64.specLabelWord (Fl4SpecA64.fl4LabelOf lbl.bv)) ≠ + .archInvocationLabel .armASIDControlMakePool := by + intro he + have hlEq := invType_specLabelWord_eqA he + rw [fl4LabelOf_bvA] at hlEq + by_cases hlt : lbl.val < 74 + · exact labNA_ne_makePool_lt74 lbl.val hlt (fun h => hlbl (UScalar.eq_of_val_eq (by + rw [h, Fl4ProofsA64.P0.Consts.fl4_abi.label.ARMASIDControlMakePool.val_eq]))) hlEq + · rw [labNA_geA (by omega)] at hlEq; cases hlEq + refine (WP.spec_ok _).2 ⟨rfl, rfl, .illegalOperation, run_liftME_errA ?_, Or.inl trivial⟩ + unfold Fl4SpecA64.decodeAsidControlInvocation + rw [if_neg hnotL]; rfl + +end asid_ctrl_top + +end +end Fl4ProofsA64.P3 + diff --git a/proofs/Fl4ProofsA64/P3/BootAddrA.lean b/proofs/Fl4ProofsA64/P3/BootAddrA.lean new file mode 100644 index 000000000..32954452a --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/BootAddrA.lean @@ -0,0 +1,35 @@ +/- +P3 (aarch64): **the kernel-window addresses of the P3 boot witnesses** (record edit #5 step A). `w0` / `w1` sit at +`kb = PPTR_BASE + 2^30`, `ib = PPTR_BASE + 2^31`, 128 cores, so every object of the boot heaps lies in +`[PPTR_BASE, PPTR_BASE + 2^44)` (the [P] kernel-window bound of `Fl4SpecA64/PORTING.md`, "Platform"). A64-Boot's +low-address `bootLay_ex` / `bootLay1_ex` stay for P1 / P2. +-/ +import Fl4ProofsA64.P1.BootW1 +import Fl4ProofsA64.P1.S1aBootDel0 +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P1.W7.R.ST.BootWit + +theorem kbC_val : (18446603337294938112#u64 : U64).val = 18446603337294938112 := by simp +theorem ibC_val : (18446603338368679936#u64 : U64).val = 18446603338368679936 := by simp + +/-- **`BootLay` at the P3 witness addresses.** -/ +theorem bootLayC_ex : BootLay 18446603337294938112#u64 18446603338368679936#u64 128 := by + refine ⟨?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_⟩ <;> (try simp only [kbC_val, ibC_val]) <;> omega + +/-- **`BootLay1` at the P3 witness addresses.** -/ +theorem bootLay1C_ex : BootLay1 18446603337294938112#u64 18446603338368679936#u64 128 := + ⟨bootLayC_ex, by rw [kbC_val]; norm_num, by rw [ibC_val]; norm_num⟩ + +/-- **`BootCanon` and the `2^44` kernel-window bound at the P3 witness addresses**: what a generic `w0` takes as [P]. -/ +theorem bootWinC_ex : + BootCanon 18446603337294938112#u64 18446603338368679936#u64 128 ∧ + (18446603337294938112#u64 : U64).val + 788736 ≤ layout.PPTR_BASE.val + 2 ^ 44 ∧ + (18446603338368679936#u64 : U64).val + 128 * 8192 ≤ layout.PPTR_BASE.val + 2 ^ 44 := by + refine ⟨⟨?_, ?_, ?_, ?_⟩, ?_, ?_⟩ + all_goals (try simp only [kbC_val, ibC_val, layout.PPTR_BASE.val_eq, layout.PPTR_TOP.val_eq]) + all_goals (try norm_num) + all_goals (try omega) + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/BufNativeFrA.lean b/proofs/Fl4ProofsA64/P3/BufNativeFrA.lean new file mode 100644 index 000000000..2ee3a6972 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/BufNativeFrA.lean @@ -0,0 +1,121 @@ +/- +P3 (AArch64): the IPC-buffer slot's native-frame invariant at boot and across notification and MCS writes. +Endpoint writes use `BufNativeA.ewA` in `EpSendRecvA`. IPC scheduler calls use the MCS write frame. The +slot-2 writers themselves are handled by the stage-3 joint invariant. + +No named premises. +-/ +import Fl4ProofsA64.P3.EpSendRecvA +import Fl4ProofsA64.P3.ScFrameA +import Fl4ProofsA64.P3.JBootW1A + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Notif Sched + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section Frame +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {s s' : S} + +/-- The two cap words of the IPC-buffer slot lie outside the write footprint. -/ +theorem BufNativeA.writesIn {T : Set U64} {W : U64 → Prop} (hNative : BufNativeA T (L.mem s)) + (hT : TcbPlaced T) (hw : WritesIn st s s' W) + (ho : ∀ t ∈ T, ∀ K : U64, (K = 64#u64 ∨ K = 72#u64) → ¬ W (wa t K)) : + BufNativeA T (L.mem s') := by + apply hNative.frame + intro t ht + rw [sbWordA8_eq, sbWordA_eq] + exact ⟨hw.2.2 _ (field_aligned (hT t ht) (by simp) (by simp)) (ho t ht _ (Or.inl rfl)), + hw.2.2 _ (field_aligned (hT t ht) (by simp) (by simp)) (ho t ht _ (Or.inr rfl))⟩ + +/-- The cap words are below the registers and scheduler fields of any TCB. -/ +theorem bufSlot_offset_neA {t u K : U64} (ht : TcbAddr t) (hu : TcbAddr u) + (hK : K.val ≤ 72) (J : U64) (hlo : 256 ≤ J.val) (hhi : J.val < 4096) : wa t K ≠ wa u J := by + intro h + have he := congrArg UScalar.val ((tt_eq_iff ht hu (by omega) hhi).1 h).2 + omega + +/-- Notification object and TCB field writes leave the IPC-buffer cap alone. -/ +theorem BufNativeA.nwA {N T : Set U64} (hNative : BufNativeA T (L.mem s)) (P : NPlaced T N) + (hw : WritesIn st s s' (NWrite N T)) : BufNativeA T (L.mem s') := by + apply hNative.writesIn P.tcb hw + intro t ht K hK + have hKval : K.val ≤ 72 := by rcases hK with rfl | rfl <;> simp + have hsmall : K.val < 4096 := by omega + rintro (⟨n, hn, hlo, hhi⟩ | ⟨u, hu, h⟩) + · have hp := P.disj n hn t ht + rw [tw_val (P.tcb t ht) hsmall] at hlo hhi + omega + · have off := bufSlot_offset_neA (P.tcb t ht) (P.tcb u hu) (K := K) hKval + rcases h with h | h | h | h | h | h + · exact off 3328#u64 (by simp) (by simp) (h.trans (stA_eq u)) + · exact off 3336#u64 (by simp) (by simp) (h.trans (tBlockA_eq u)) + · exact off 256#u64 (by simp) (by simp) (h.trans (badgeRegA_eq u)) + · exact off 3496#u64 (by simp) (by simp) (h.trans (nextEp_eq u)) + · exact off 3504#u64 (by simp) (by simp) (h.trans (prevEp_eq u)) + · exact off 3376#u64 (by simp) (by simp) h + +/-- The MCS footprint consists of the SCs, node words and scheduler fields outside the IPC-buffer cap. -/ +theorem BufNativeA.mwrA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {σ : Fl4SpecA64.State Unit} + (hNative : BufNativeA Z.T (L.mem s)) (H : SchedSRN st Z m κ s σ) + (hw : WritesIn st s s' (MWrA (st := st) Z m σ s)) : BufNativeA Z.T (L.mem s') := by + apply hNative.writesIn H.core.tcbs hw + intro t ht K hK + have hta := H.core.tcbs t ht + have hth := (H.thT t).2 ht + have hKval : K.val ≤ 72 := by rcases hK with rfl | rfl <;> simp + have hsmall : K.val < 4096 := by omega + have hv := tw_val hta hsmall + apply not_mwrA (b := wa t K) + · rintro ⟨c, hc, x, n, hx, hb⟩ + have ho := (H.scp.out c hc x n hx _ hb).1 t .tcb hth + change (wa t K).val < t.val ∨ t.val + 4096 ≤ (wa t K).val at ho + omega + · intro u hu + have off := bufSlot_offset_neA hta (H.core.tcbs u hu) (K := K) hKval + exact ⟨off 3464#u64 (by simp) (by simp), off 3480#u64 (by simp) (by simp), + off 3488#u64 (by simp) (by simp), off 3592#u64 (by simp) (by simp)⟩ + · have hi : ¬ IrqNodeA (L.ksb s) t := by + rintro ⟨i, hi, rfl⟩ + rw [H.place.irq i hi] at hth + cases hth + have ho := H.place.ks t .tcb hth hi + change t.val + 4096 ≤ (L.ksb s).val ∨ (L.ksb s).val + ksSizeA ≤ t.val at ho + omega + +end Frame + +section Boot +open Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P1.W7.R.ST.BootWit +variable {kb ib r now mt sl : U64} {nc : Nat} {uw : U64 → U64} + +/-- Every idle TCB's buffer cap is null at `w0`. -/ +theorem w0_bufNativeA (P : BootLay kb ib nc) : + BufNativeA (bootH kb ib nc).tcbs (bootM kb ib nc r now mt) := by + intro t ht + have hc : capAtA (bootM kb ib nc r now mt) (sbWordA t) = .Null := + BootWit.boot_null P _ (boot_sbW t ht) + rw [bufSlot_eqA, hc] + trivial + +/-- The root TCB's buffer cap is a native frame and the idle TCBs' caps remain null at `w1`. -/ +theorem w1_bufNativeA (P : BootLay1 kb ib nc) : + BufNativeA (w1H kb ib nc).tcbs (w1M kb ib nc r now mt sl uw) := by + intro t ht + rcases w1T P ht with hi | rfl + · have hc : capAtA (w1M kb ib nc r now mt sl uw) (sbWordA t) = .Null := + cap_idle P (boot_sbW t hi) + rw [bufSlot_eqA, hc] + trivial + · rw [rt_capA P (i := 2) (by decide)] + show false = false + rfl + +end Boot + +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/CancelIpcFootA.lean b/proofs/Fl4ProofsA64/P3/CancelIpcFootA.lean new file mode 100644 index 000000000..4d4946b4d --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/CancelIpcFootA.lean @@ -0,0 +1,285 @@ +/- +P3 (AArch64): the concrete write footprint of `ipc::cancel_ipc` (every arm but `BlockedOnReply`). + +`cancel_ipc_rel` relates the operation and keeps both IPC relations and the scheduler relation +but reports no write set. **`cancel_ipc_footA`** (same hypotheses, no scheduler relation) +states `WritesIn` over `CancelWA`: the endpoint code's words (`EWrite`), the notification code's +words (`NWrite`) and the current core's action word. Combined with `cancel_ipc_rel` through +`WP.spec_and` it carries the relations that read other words across the cancellation. + +* `sts_footA`: `set_thread_state t` writes `t`'s state word and the action word (P1 + `schedule_tcb_act`). + +No named premise. +-/ +import Fl4ProofsA64.P3.IpcCancelA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif + +noncomputable section + +/-- The words a cancellation may write (module docstring). -/ +def CancelWA (N T E R : Set U64) (Bf : Set (U64 × Fl4SpecA64.Word)) (ab : U64) (b : U64) : Prop := + EWrite T E R Bf b ∨ NWrite N T b ∨ b = ab + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {N T E R : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + +/-- `set_thread_state t v` writes `t`'s state word and the action word. -/ +theorem sts_footA (s : S) (t v : U64) (hst : Aligned (stA t)) + (hAct : Aligned (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION)) : + sched.set_thread_state st s t v ⦃ s' => WritesIn st s s' + (fun b => b = stA t ∨ b = L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) ⦄ := by + unfold sched.set_thread_state + refine WP.spec_bind (obj.Tcb.set_state.spec s t v) ?_ + intro s1 hs1 + rw [hs1] + have w1 : WritesIn st s (L.wr s (stA t) v) (· = stA t) := writesIn_wr hst + have hA : L.nodeAt (L.wr s (stA t) v) (L.cpu (L.wr s (stA t) v)) layout.node.SCHED_ACTION = + L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION := by + rw [nodeAt_of_ksb (L.ksb_wr _ _ _), L.cpu_wr] + refine WP.spec_mono (schedule_tcb_act (st := st) (L.wr s (stA t) v) t) ?_ + rintro s' (rfl | rfl) + · exact w1.mono fun b h => Or.inl h + · have w2 := writesIn_wr (st := st) (s := L.wr s (stA t) v) (v := sched.ACTION_CHOOSE_NEW) + (show Aligned (actA (L := L) (L.wr s (stA t) v)) by + show Aligned (L.nodeAt (L.wr s (stA t) v) (L.cpu (L.wr s (stA t) v)) layout.node.SCHED_ACTION) + rw [hA]; exact hAct) + exact (w1.trans w2).mono fun b h => h.elim Or.inl fun e => Or.inr (e.trans hA) + +private theorem cw_e {ab b : U64} (h : EWrite T E R Bf b) : CancelWA N T E R Bf ab b := Or.inl h +private theorem cw_n {ab b : U64} (h : NWrite N T b) : CancelWA N T E R Bf ab b := Or.inr (Or.inl h) + +/-- The endpoint arm's writes (after the fault clear). -/ +theorem ep_arm_footA (hP : IPlacedA N T E R Bf) (s : S) (a : A) (σ : Fl4SpecA64.State Unit) {t : U64} (ht : t ∈ T) + {x : Fl4SpecA64.Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) {e : U64} (he : e ∈ E) + {z : Fl4SpecA64.Endpoint} (hkz : σ.kheap (specPtr e) = some (.endpoint z)) (hq : specPtr t ∈ z.epQueue) + (hR1 : EpStateRel T E R Bf (L.mem (L.wr s (tFaultA t) 0#u64)) (updK σ (specPtr t) (.tcb { x with tcbFault := none }))) + (hW1 : WritesIn st s (L.wr s (tFaultA t) 0#u64) (EWrite T E R Bf)) + (hb1 : L.mem (L.wr s (tFaultA t) 0#u64) (tBlockA t) = e) {w : U64} + (hcase : (∃ d, x.tcbState = .blockedOnSend (specPtr e) d ∧ w ≠ sched.TS_BLOCKED_ON_RECV) ∨ + (∃ ro d, x.tcbState = .blockedOnReceive (specPtr e) ro d ∧ w = sched.TS_BLOCKED_ON_RECV)) + (hAct : Aligned (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION)) : + epArmProg st s a t w ⦃ r => WritesIn st s r.1 + (CancelWA N T E R Bf (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION)) ⦄ := by + unfold epArmProg + have hPE := hP.e + have hta := hPE.tcb t ht + have hst := (a_st hta).1 + have hte : specPtr e ≠ specPtr t := by intro h; rw [h, hk] at hkz; cases hkz + have hkz1 : (updK σ (specPtr t) (.tcb { x with tcbFault := none })).kheap (specPtr e) = some (.endpoint z) := by + rw [updK_kheap, Function.update_of_ne hte, hkz] + step with obj.Tcb.block_obj.spec as ⟨ep, hep⟩ + have hep' : ep = e := hep.trans hb1 + subst hep' + refine WP.spec_bind (ep_dequeue_raw hPE _ _ hR1 he hkz1 hq) ?_ + rintro s2 ⟨-, hR2, hW2, -, -⟩ + have hW02 := WritesIn.ew hW1 hW2 + have hk2 : (updK (updK σ (specPtr t) (.tcb { x with tcbFault := none })) (specPtr ep) + (.endpoint (epDeq z (specPtr t)))).kheap (specPtr t) = some (.tcb { x with tcbFault := none }) := by + rw [updK_kheap, Function.update_of_ne hte.symm, updK_kheap, Function.update_self] + have hA2 : L.nodeAt s2 (L.cpu s2) layout.node.SCHED_ACTION = L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION := by + rw [nodeAt_of_ksb hW02.1, hW02.2.1] + -- the final Inactive write from a state reached by endpoint writes + have fin : ∀ s3 : S, WritesIn st s s3 (EWrite T E R Bf) → + (do + let s4 ← sched.set_thread_state st s3 t sched.TS_INACTIVE + ok (s4, a)) ⦃ r => WritesIn st s r.1 + (CancelWA N T E R Bf (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION)) ⦄ := by + intro s3 w3 + have hA3 : L.nodeAt s3 (L.cpu s3) layout.node.SCHED_ACTION = L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION := by + rw [nodeAt_of_ksb w3.1, w3.2.1] + refine WP.spec_bind (sts_footA s3 t sched.TS_INACTIVE hst (by rw [hA3]; exact hAct)) ?_ + intro s4 w4 + rw [WP.spec_ok] + refine (w3.trans w4).mono fun b h => ?_ + rcases h with h | h | h + · exact cw_e h + · subst h; exact cw_e (stA_ew hPE ht) + · exact Or.inr (Or.inr (h.trans hA3)) + rcases hcase with ⟨d, hs, hwr⟩ | ⟨ro, d, hs, hwr⟩ + · simp only [hwr, ↓reduceIte, bind_ok] + exact fin s2 hW02 + · simp only [hwr, ↓reduceIte] + have hrec := (hR2.at_tcb ht hk2).recv (specPtr ep) ro d hs + step with obj.Tcb.reply_obj.spec as ⟨i, hi⟩ + have hi' : i = L.mem s2 (tReplyA t) := hi + by_cases hi0 : i = 0#u64 + · simp only [hi0, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte, bind_ok] + exact fin s2 hW02 + · have hro : ro = some (specPtr i) := by rw [← hrec.1, ← hi', optRef_ne hi0] + subst hro + have hiR : i ∈ R := by rw [hi']; exact hrec.2 (by simp) + have hnz : (i != 0#u64) = true := by simpa using hi0 + simp only [hnz, ↓reduceIte] + have hal := (a_rtcb hPE hiR).1 + refine WP.spec_bind (Pₘ := fun s4 => WritesIn st s s4 + (fun b => EWrite T E R Bf b ∨ b = L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION)) ?_ ?_ + · unfold ipc.reply_unlink + refine WP.spec_bind (obj.Reply.set_tcb.spec s2 i 0#u64) ?_ + intro s3 hs3 + rw [hs3] + have w3 : WritesIn st s (L.wr s2 (rTcbA i) 0#u64) (EWrite T E R Bf) := + WritesIn.ew hW02 ((writesIn_wr hal).mono fun b hb => by subst hb; exact Or.inr (Or.inl ⟨i, hiR, rTcbA_val i⟩)) + have hA3 : L.nodeAt (L.wr s2 (rTcbA i) 0#u64) (L.cpu (L.wr s2 (rTcbA i) 0#u64)) layout.node.SCHED_ACTION = + L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION := by rw [nodeAt_of_ksb w3.1, w3.2.1] + refine WP.spec_mono (sts_footA _ t sched.TS_INACTIVE hst (by rw [hA3]; exact hAct)) ?_ + intro s4 w4 + refine (w3.trans w4).mono fun b h => ?_ + rcases h with h | h | h + · exact Or.inl h + · subst h; exact Or.inl (stA_ew hPE ht) + · exact Or.inr (h.trans hA3) + · intro s4 w4 + have hA4 : L.nodeAt s4 (L.cpu s4) layout.node.SCHED_ACTION = L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION := by + rw [nodeAt_of_ksb w4.1, w4.2.1] + refine WP.spec_bind (sts_footA s4 t sched.TS_INACTIVE hst (by rw [hA4]; exact hAct)) ?_ + intro s5 w5 + rw [WP.spec_ok] + refine (w4.trans w5).mono fun b h => ?_ + rcases h with (h | h) | h | h + · exact cw_e h + · exact Or.inr (Or.inr h) + · subst h; exact cw_e (stA_ew hPE ht) + · exact Or.inr (Or.inr (h.trans hA4)) + +/-- The notification arm's writes (after the fault clear). -/ +theorem ntfn_arm_footA (hP : IPlacedA N T E R Bf) (s : S) (a : A) (σ : Fl4SpecA64.State Unit) + (hRN : NtfnStateRel N T (L.mem s) σ) (hRE : EpStateRel T E R Bf (L.mem s) σ) {t : U64} (ht : t ∈ T) + {x : Fl4SpecA64.Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) {n : U64} (hn : n ∈ N) + {z : Fl4SpecA64.Notification} (hkz : σ.kheap (specPtr n) = some (.notification z)) + (hq : specPtr t ∈ z.ntfnObj.ntfnQueue) (hb1 : L.mem (L.wr s (tFaultA t) 0#u64) (tBlockA t) = n) + (hAct : Aligned (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION)) : + (do + let i ← obj.Tcb.block_obj st t (L.wr s (tFaultA t) 0#u64) + let s2 ← notification.cancel_signal st (L.wr s (tFaultA t) 0#u64) t i + ok (s2, a)) ⦃ r => WritesIn st s r.1 + (CancelWA N T E R Bf (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION)) ⦄ := by + have hPN := hP.n + have hta := hP.e.tcb t ht + have hst := (a_st hta).1 + obtain ⟨hRN1, -, hW1, -, -⟩ := clear_both hP s σ hRN hRE ht hk + have hnt : specPtr n ≠ specPtr t := by intro h; rw [h, hk] at hkz; cases hkz + have hkz1 : (updK σ (specPtr t) (.tcb { x with tcbFault := none })).kheap (specPtr n) = some (.notification z) := by + rw [updK_kheap, Function.update_of_ne hnt, hkz] + obtain ⟨xs, hw, -, hQ, -⟩ : ∃ xs : List U64, z.ntfnObj = .waitingNtfn (xs.map specPtr) ∧ + stOf (L.mem (L.wr s (tFaultA t) 0#u64)) n = 1#u64 ∧ + QueueRel (L.mem (L.wr s (tFaultA t) 0#u64)) T .Ep (qOf (L.mem (L.wr s (tFaultA t) 0#u64)) n) xs ∧ + ∀ y ∈ xs, BlockedOn (L.mem (L.wr s (tFaultA t) 0#u64)) n y := by + have ho := (hRN1.at_ntfn hn hkz1).obj + have hq' := hq + rcases hzo : z.ntfnObj with _ | q | b <;> rw [hzo] at ho hq' + · simp [Fl4SpecA64.Ntfn.ntfnQueue] at hq' + · obtain ⟨h1, xs, -, rfl, h2, h3⟩ := ho + exact ⟨xs, rfl, h1, h2, h3⟩ + · simp [Fl4SpecA64.Ntfn.ntfnQueue] at hq' + have htx : t ∈ xs := by + have hq' := hq + rw [hw] at hq' + simp only [Fl4SpecA64.Ntfn.ntfnQueue, List.mem_map] at hq' + obtain ⟨y, hy, ey⟩ := hq' + rw [← specPtr_inj ey]; exact hy + step with obj.Tcb.block_obj.spec as ⟨i, hi⟩ + have hi' : i = n := hi.trans hb1 + subst hi' + refine WP.spec_bind (Pₘ := fun s3 => WritesIn st s s3 + (CancelWA N T E R Bf (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION))) ?_ ?_ + · unfold notification.cancel_signal + refine WP.spec_bind (ntfn_dequeue_full hPN _ hn t xs hQ htx) ?_ + rintro s2 ⟨-, -, h3⟩ + have w2 : WritesIn st s s2 (fun b => EWrite T E R Bf b ∨ NWrite N T b) := hW1.trans h3.foot + have hA2 : L.nodeAt s2 (L.cpu s2) layout.node.SCHED_ACTION = L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION := by + rw [nodeAt_of_ksb w2.1, w2.2.1] + refine WP.spec_mono (sts_footA s2 t sched.TS_INACTIVE hst (by rw [hA2]; exact hAct)) ?_ + intro s3 w3 + refine (w2.trans w3).mono fun b h => ?_ + rcases h with (h | h) | h | h + · exact cw_e h + · exact cw_n h + · subst h; exact cw_n (Or.inr ⟨t, ht, Or.inl rfl⟩) + · exact Or.inr (Or.inr (h.trans hA2)) + · intro s3 w3 + rw [WP.spec_ok] + exact w3 + +set_option maxHeartbeats 4000000 in +/-- **The write footprint of `cancel_ipc`** (every state but `BlockedOnReply`; module docstring). -/ +theorem cancel_ipc_footA (hP : IPlacedA N T E R Bf) (s : S) (a : A) (σ : Fl4SpecA64.State Unit) + (hRN : NtfnStateRel N T (L.mem s) σ) (hRE : EpStateRel T E R Bf (L.mem s) σ) + {t : U64} (ht : t ∈ T) {x : Fl4SpecA64.Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hnr : ∀ r, x.tcbState ≠ .blockedOnReply r) + (hE : ∀ e', Fl4SpecA64.epBlocked x.tcbState = some e' → L.mem s (tBlockA t) ∈ E ∧ + ∃ z, σ.kheap e' = some (.endpoint z) ∧ specPtr t ∈ z.epQueue) + (hN : ∀ n', x.tcbState = .blockedOnNotification n' → L.mem s (tBlockA t) ∈ N ∧ + ∃ z, σ.kheap n' = some (.notification z) ∧ specPtr t ∈ z.ntfnObj.ntfnQueue) + (hAct : Aligned (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION)) : + ipc.cancel_ipc st ar s a t ⦃ r => WritesIn st s r.1 + (CancelWA N T E R Bf (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION)) ⦄ := by + have hPE := hP.e + have hta := hPE.tcb t ht + have hx := hRE.at_tcb ht hk + have hxN := hRN.at_tcb ht hk + obtain ⟨-, hR1, hW1, -, hag⟩ := clear_both hP s σ hRN hRE ht hk + have vf := val_fault hta + have hb1 : L.mem (L.wr s (tFaultA t) 0#u64) (tBlockA t) = L.mem s (tBlockA t) := + hag _ (a_blk hta).1 (by dsimp only; rw [val_blk hta, vf]; omega) + unfold ipc.cancel_ipc faults.clear_fault + step with obj.Tcb.set_fault_type.spec as ⟨s1, hs1⟩ + have e1 : s1 = L.wr s (tFaultA t) 0#u64 := by rw [hs1]; unfold fl4_abi.consts.fault.NullFault; rfl + subst e1 + step with obj.Tcb.state.spec as ⟨w, hw⟩ + have hw' : w.val = tsCode x.tcbState := by + rw [hw, hag _ (a_st hta).1 (by dsimp only; rw [val_st hta, vf]; omega)]; exact hx.st + have hcS : ∀ e d, x.tcbState = .blockedOnSend e d → w = sched.TS_BLOCKED_ON_SEND := fun e d hs => + UScalar.eq_of_val_eq (by rw [hw', hs, tsVals.2.2.2.1]; rfl) + have hcR : ∀ e r d, x.tcbState = .blockedOnReceive e r d → w = sched.TS_BLOCKED_ON_RECV := fun e r d hs => + UScalar.eq_of_val_eq (by rw [hw', hs, tsVals.2.2.1]; rfl) + have hSR4 : sched.TS_BLOCKED_ON_RECV ≠ sched.TS_BLOCKED_ON_SEND := by rw [tsVals.2.2.1, tsVals.2.2.2.1]; decide + have harm : ∀ (e' : Fl4SpecA64.ObjRef), Fl4SpecA64.epBlocked x.tcbState = some e' → + ∃ e ∈ E, e' = specPtr e ∧ L.mem (L.wr s (tFaultA t) 0#u64) (tBlockA t) = e ∧ + ∃ z, σ.kheap (specPtr e) = some (.endpoint z) ∧ specPtr t ∈ z.epQueue := fun e' hb => by + obtain ⟨heE, z, hkz, hq⟩ := hE e' hb + have he' : specPtr (L.mem s (tBlockA t)) = e' := hx.blk e' hb + subst he' + exact ⟨_, heE, rfl, hb1, z, hkz, hq⟩ + by_cases hSd : ∃ e' d, x.tcbState = .blockedOnSend e' d + · obtain ⟨e', d, hs⟩ := hSd + obtain ⟨e, he, rfl, hbe, z, hkz, hq⟩ := harm e' (by rw [hs]; rfl) + have hwS := hcS _ d hs + rw [if_pos hwS] + exact ep_arm_footA hP s a σ ht hk he hkz hq hR1 hW1 hbe + (Or.inl ⟨d, hs, by rw [hwS]; exact hSR4.symm⟩) hAct + by_cases hRc : ∃ e' ro d, x.tcbState = .blockedOnReceive e' ro d + · obtain ⟨e', ro, d, hs⟩ := hRc + obtain ⟨e, he, rfl, hbe, z, hkz, hq⟩ := harm e' (by rw [hs]; rfl) + have hwR := hcR _ ro d hs + rw [if_neg (by rw [hwR]; exact hSR4), if_pos hwR] + exact ep_arm_footA hP s a σ ht hk he hkz hq hR1 hW1 hbe (Or.inr ⟨ro, d, hs, hwR⟩) hAct + have hwS : w ≠ sched.TS_BLOCKED_ON_SEND := fun h => + hSd (tsCode_four (by rw [← hw', h, tsVals.2.2.2.1]; rfl)) + have hwR : w ≠ sched.TS_BLOCKED_ON_RECV := fun h => + hRc (tsCode_three (by rw [← hw', h, tsVals.2.2.1]; rfl)) + rw [if_neg hwS, if_neg hwR] + by_cases hNc : ∃ n', x.tcbState = .blockedOnNotification n' + · obtain ⟨n', hs⟩ := hNc + obtain ⟨hnN, z, hkz, hq⟩ := hN n' hs + have hn' : specPtr (L.mem s (tBlockA t)) = n' := hxN.blk n' hs + subst hn' + have hw6 : w = sched.TS_BLOCKED_ON_NTFN := UScalar.eq_of_val_eq (by rw [hw', hs, tsVals.2.2.2.2]; rfl) + rw [if_pos hw6] + exact ntfn_arm_footA hP s a σ hRN hRE ht hk hnN hkz hq hb1 hAct + have hw6 : w ≠ sched.TS_BLOCKED_ON_NTFN := fun h => + hNc (tsCode_six' (by rw [← hw', h, tsVals.2.2.2.2]; rfl)) + have hw5 : w ≠ sched.TS_BLOCKED_ON_REPLY := fun h => by + obtain ⟨r, hr⟩ := tsCode_five (by rw [← hw', h]; unfold sched.TS_BLOCKED_ON_REPLY; rfl) + exact hnr r hr + rw [if_neg hw6, if_neg hw5, WP.spec_ok] + exact hW1.mono fun b h => cw_e h + +end +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/CancelIpcKeepA.lean b/proofs/Fl4ProofsA64/P3/CancelIpcKeepA.lean new file mode 100644 index 000000000..410889ffe --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/CancelIpcKeepA.lean @@ -0,0 +1,264 @@ +/- +P3 (AArch64): the spec frame of l4v `cancel_ipc` (every arm but `BlockedOnReply`). + +`CancelKeepA σ σ'`: every object that is not a TCB, an SC, an endpoint, a notification or a +reply is kept; a TCB changes only its state and fault; an SC changes only its reply list; the +user memory is kept. **`cancelIpc_keepA`**: under the hypotheses of `cancel_ipc_rel` (spec side), +`cancelIpc t σ` has one outcome and it is `CancelKeepA`-related to `σ`. With `cancel_ipc_footA` +it carries the relations that read caps, MCPs, yield links and IPC buffers across a cancellation. + +No named premise. +-/ +import Fl4ProofsA64.P3.IpcCancelA + +namespace Fl4ProofsA64.P3.Endp +open Fl4Spec Fl4SpecA64 Fl4ProofsA64.P3.Notif + +noncomputable section + +/-- What a cancellation keeps in the spec state (module docstring). -/ +structure CancelKeepA (σ σ' : State Unit) : Prop where + obj : ∀ p o, σ.kheap p = some o → (∀ x, o ≠ .tcb x) → (∀ y n, o ≠ .schedContext y n) → + (∀ e, o ≠ .endpoint e) → (∀ e, o ≠ .notification e) → (∀ r, o ≠ .reply r) → σ'.kheap p = some o + tcb : ∀ p x, σ.kheap p = some (.tcb x) → ∃ y, σ'.kheap p = some (.tcb y) ∧ + y = { x with tcbState := y.tcbState, tcbFault := y.tcbFault } + sc : ∀ p y n, σ.kheap p = some (.schedContext y n) → ∃ y', σ'.kheap p = some (.schedContext y' n) ∧ + y' = { y with scReplies := y'.scReplies } + mem : σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory + +theorem CancelKeepA.refl (σ : State Unit) : CancelKeepA σ σ := + ⟨fun _ _ h _ _ _ _ _ => h, fun _ x h => ⟨x, h, by cases x; rfl⟩, fun _ y _ h => ⟨y, h, by cases y; rfl⟩, rfl⟩ + +private theorem tcb_with_transA {x y z : Tcb} (h1 : y = { x with tcbState := y.tcbState, tcbFault := y.tcbFault }) + (h2 : z = { y with tcbState := z.tcbState, tcbFault := z.tcbFault }) : + z = { x with tcbState := z.tcbState, tcbFault := z.tcbFault } := by + cases x; cases y; cases z + simp only [Tcb.mk.injEq] at h1 h2 ⊢ + simp_all + +private theorem sc_with_transA {x y z : SchedContext} (h1 : y = { x with scReplies := y.scReplies }) + (h2 : z = { y with scReplies := z.scReplies }) : z = { x with scReplies := z.scReplies } := by + cases x; cases y; cases z + simp only [SchedContext.mk.injEq] at h1 h2 ⊢ + simp_all + +theorem CancelKeepA.trans {σ1 σ2 σ3 : State Unit} (h1 : CancelKeepA σ1 σ2) (h2 : CancelKeepA σ2 σ3) : + CancelKeepA σ1 σ3 := by + refine ⟨fun p o hp a b c d e => h2.obj p o (h1.obj p o hp a b c d e) a b c d e, fun p x hp => ?_, + fun p y n hp => ?_, h2.mem.trans h1.mem⟩ + · obtain ⟨y, hy, ey⟩ := h1.tcb p x hp + obtain ⟨z, hz, ez⟩ := h2.tcb p _ hy + exact ⟨z, hz, tcb_with_transA ey ez⟩ + · obtain ⟨y', hy, ey⟩ := h1.sc p y n hp + obtain ⟨z, hz, ez⟩ := h2.sc p _ n hy + exact ⟨z, hz, sc_with_transA ey ez⟩ + +theorem CancelKeepA.of_kheap {σ σ' : State Unit} (hk : σ'.kheap = σ.kheap) + (hm : σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory) : CancelKeepA σ σ' := + ⟨fun _ _ h _ _ _ _ _ => by rw [hk]; exact h, fun _ x h => ⟨x, by rw [hk]; exact h, by cases x; rfl⟩, + fun _ y _ h => ⟨y, by rw [hk]; exact h, by cases y; rfl⟩, hm⟩ + +theorem CancelKeepA.stsAct (σ : State Unit) (t : ObjRef) : CancelKeepA σ (Notif.stsAct σ t) := + CancelKeepA.of_kheap (stsAct_kheap σ t) (by rw [stsAct_ms]) + +/-- An update of a TCB's state and fault. -/ +theorem CancelKeepA.updTcb {σ : State Unit} {p : ObjRef} {x y : Tcb} (hk : σ.kheap p = some (.tcb x)) + (hy : y = { x with tcbState := y.tcbState, tcbFault := y.tcbFault }) : + CancelKeepA σ (updK σ p (.tcb y)) := by + refine ⟨fun q o hq a _ _ _ _ => ?_, fun q x' hq => ?_, fun q y' n hq => ?_, rfl⟩ + · have e : q ≠ p := fun e => by subst e; rw [hk] at hq; cases hq; exact a x rfl + rw [updK_kheap, Function.update_of_ne e]; exact hq + · by_cases e : q = p + · subst e + rw [hk] at hq + cases hq + exact ⟨y, by rw [updK_kheap, Function.update_self], hy⟩ + · exact ⟨x', by rw [updK_kheap, Function.update_of_ne e]; exact hq, by cases x'; rfl⟩ + · have e : q ≠ p := fun e => by subst e; rw [hk] at hq; cases hq + exact ⟨y', by rw [updK_kheap, Function.update_of_ne e]; exact hq, by cases y'; rfl⟩ + +/-- An update of an endpoint, notification or reply by an object of the same kind. -/ +theorem CancelKeepA.updIpc {σ : State Unit} {p : ObjRef} {o o' : KernelObject} (hk : σ.kheap p = some o) + (ho : (∃ e, o = .endpoint e) ∨ (∃ e, o = .notification e) ∨ (∃ r, o = .reply r)) + (ho' : (∀ x, o' ≠ .tcb x) ∧ ∀ y n, o' ≠ .schedContext y n) : + CancelKeepA σ (updK σ p o') := by + refine ⟨fun q o2 hq _ _ c d e => ?_, fun q x' hq => ?_, fun q y' n hq => ?_, rfl⟩ + · have ne : q ≠ p := fun ne => by + subst ne; rw [hk] at hq; cases hq + rcases ho with ⟨e', rfl⟩ | ⟨e', rfl⟩ | ⟨r, rfl⟩ + · exact c e' rfl + · exact d e' rfl + · exact e r rfl + rw [updK_kheap, Function.update_of_ne ne]; exact hq + · have ne : q ≠ p := fun ne => by + subst ne; rw [hk] at hq; cases hq + rcases ho with ⟨e', h⟩ | ⟨e', h⟩ | ⟨r, h⟩ <;> cases h + exact ⟨x', by rw [updK_kheap, Function.update_of_ne ne]; exact hq, by cases x'; rfl⟩ + · have ne : q ≠ p := fun ne => by + subst ne; rw [hk] at hq; cases hq + rcases ho with ⟨e', h⟩ | ⟨e', h⟩ | ⟨r, h⟩ <;> cases h + exact ⟨y', by rw [updK_kheap, Function.update_of_ne ne]; exact hq, by cases y'; rfl⟩ + +/-- What the non-reply arms also keep: every SC object, and every reply's `reply_sc`. -/ +structure StackKeepA (σ σ' : State Unit) : Prop where + sc : ∀ p y n, σ.kheap p = some (.schedContext y n) → σ'.kheap p = some (.schedContext y n) + reply : ∀ p r', σ'.kheap p = some (.reply r') → ∃ r, σ.kheap p = some (.reply r) ∧ r'.replySc = r.replySc + +theorem StackKeepA.trans {σ1 σ2 σ3 : State Unit} (h1 : StackKeepA σ1 σ2) (h2 : StackKeepA σ2 σ3) : + StackKeepA σ1 σ3 := + ⟨fun p y n h => h2.sc p y n (h1.sc p y n h), fun p r h => by + obtain ⟨r2, h2', e2⟩ := h2.reply p r h + obtain ⟨r1, h1', e1⟩ := h1.reply p r2 h2' + exact ⟨r1, h1', e2.trans e1⟩⟩ + +theorem StackKeepA.stsAct (σ : State Unit) (t : ObjRef) : StackKeepA σ (Notif.stsAct σ t) := + ⟨fun _ _ _ h => by rw [stsAct_kheap]; exact h, fun _ r h => ⟨r, by rw [stsAct_kheap] at h; exact h, rfl⟩⟩ + +/-- An update of an object that is not an SC, by one that is not an SC; a reply keeps its `reply_sc`. -/ +theorem StackKeepA.upd {σ : State Unit} {p : ObjRef} {o o' : KernelObject} (hk : σ.kheap p = some o) + (ho : ∀ y n, o ≠ .schedContext y n) + (hr : ∀ r', o' = .reply r' → ∃ r, o = .reply r ∧ r'.replySc = r.replySc) : + StackKeepA σ (updK σ p o') := by + refine ⟨fun q y n hq => ?_, fun q r' hq => ?_⟩ + · have e : q ≠ p := fun e => by subst e; rw [hk] at hq; cases hq; exact ho y n rfl + rw [updK_kheap, Function.update_of_ne e]; exact hq + · by_cases e : q = p + · subst e + rw [updK_kheap, Function.update_self] at hq + cases hq + obtain ⟨r, h, he⟩ := hr r' rfl + exact ⟨r, by rw [hk, h], he⟩ + · rw [updK_kheap, Function.update_of_ne e] at hq + exact ⟨r', hq, rfl⟩ + +/-- A thread state with no pending IPC. -/ +def IpcFreeA (ts : ThreadState) : Prop := + (∀ e d, ts ≠ .blockedOnSend e d) ∧ (∀ e r d, ts ≠ .blockedOnReceive e r d) ∧ + (∀ n, ts ≠ .blockedOnNotification n) ∧ (∀ r, ts ≠ .blockedOnReply r) + +theorem IpcFreeA.inactive : IpcFreeA .inactive := + ⟨fun _ _ h => (by cases h), fun _ _ _ h => (by cases h), fun _ h => (by cases h), fun _ h => (by cases h)⟩ + +/-- **The spec frame of `cancel_ipc`** (module docstring). -/ +theorem cancelIpc_keepA {σ : State Unit} {t : ObjRef} {x : Tcb} (hk : σ.kheap t = some (.tcb x)) + (hnr : ∀ r, x.tcbState ≠ .blockedOnReply r) + (hE : ∀ e', epBlocked x.tcbState = some e' → ∃ z, σ.kheap e' = some (.endpoint z) ∧ t ∈ z.epQueue) + (hN : ∀ n', x.tcbState = .blockedOnNotification n' → + ∃ z, σ.kheap n' = some (.notification z) ∧ t ∈ z.ntfnObj.ntfnQueue) + (hrep : RepOKA σ t) : + ∃ σ', cancelIpc (Z := Unit) t σ = ({((), σ')}, False) ∧ CancelKeepA σ σ' ∧ StackKeepA σ σ' ∧ + ∃ y, σ'.kheap t = some (.tcb y) ∧ IpcFreeA y.tcbState := by + let σ1 := updK σ t (.tcb { x with tcbFault := none }) + have k1 : CancelKeepA σ σ1 := CancelKeepA.updTcb hk rfl + have hk1 : σ1.kheap t = some (.tcb { x with tcbFault := none }) := by + simp only [σ1, updK_kheap, Function.update_self] + have tcbS : ∀ (τ : State Unit) (p : ObjRef) (a b : Tcb), τ.kheap p = some (.tcb a) → + StackKeepA τ (updK τ p (.tcb b)) := fun τ p a b h => + StackKeepA.upd h (fun _ _ e => by cases e) (fun _ e => by cases e) + have s1 : StackKeepA σ σ1 := tcbS σ t _ _ hk + -- the endpoint arms + have ep_arm : ∀ e (ro : Option ObjRef), + ((∃ d, x.tcbState = .blockedOnSend e d ∧ ro = none) ∨ (∃ d, x.tcbState = .blockedOnReceive e ro d)) → + ∃ σ', cancelIpc (Z := Unit) t σ = ({((), σ')}, False) ∧ CancelKeepA σ σ' ∧ StackKeepA σ σ' ∧ + ∃ y, σ'.kheap t = some (.tcb y) ∧ IpcFreeA y.tcbState := by + intro e ro hcase + have hb : epBlocked x.tcbState = some e := by + rcases hcase with ⟨d, hs, -⟩ | ⟨d, hs⟩ <;> rw [hs] <;> rfl + obtain ⟨z, hkz, hq⟩ := hE e hb + have het : e ≠ t := by intro h; rw [h, hk] at hkz; cases hkz + have hkz1 : σ1.kheap e = some (.endpoint z) := by + simp only [σ1, updK_kheap, Function.update_of_ne het]; exact hkz + have hdq := tcbEpDequeue_eq (Z := Unit) hkz1 hq + let σ2 := updK σ1 e (.endpoint (epDeq z t)) + have k2 : CancelKeepA σ1 σ2 := CancelKeepA.updIpc (o' := .endpoint (epDeq z t)) hkz1 (Or.inl ⟨z, rfl⟩) ⟨fun _ h => (by cases h), fun _ _ h => (by cases h)⟩ + have hk2 : σ2.kheap t = some (.tcb { x with tcbFault := none }) := by + simp only [σ2, updK_kheap, Function.update_of_ne het.symm]; exact hk1 + have s2 : StackKeepA σ1 σ2 := StackKeepA.upd (o' := .endpoint (epDeq z t)) hkz1 (fun _ _ e => by cases e) + (fun _ e => by cases e) + cases ro with + | none => + refine ⟨_, cancelIpc_blocked hk none (hcase.imp (fun ⟨d, hs, h⟩ => ⟨e, d, hs, h⟩) fun ⟨d, hs⟩ => ⟨e, d, hs⟩) + (bci_none hb hdq hk2), ?_⟩ + exact ⟨(k1.trans k2).trans ((CancelKeepA.updTcb hk2 rfl).trans (CancelKeepA.stsAct _ _)), + (s1.trans s2).trans ((tcbS _ _ _ _ hk2).trans (StackKeepA.stsAct _ _)), + { { x with tcbFault := none } with tcbState := .inactive }, + by rw [stsAct_kheap, updK_kheap, Function.update_self], IpcFreeA.inactive⟩ + | some r => + have hrecv : ∃ d, x.tcbState = .blockedOnReceive e (some r) d := by + rcases hcase with ⟨d, hs, h⟩ | h + · cases h + · exact h + obtain ⟨d, hs⟩ := hrecv + obtain ⟨xr, hkr, hback⟩ := hrep x hk e r d hs + have hrt : r ≠ t := by intro h; rw [h, hk] at hkr; cases hkr + have hre : r ≠ e := by intro h; rw [h, hkz] at hkr; cases hkr + have hkr2 : σ2.kheap r = some (.reply xr) := by + simp only [σ2, σ1, updK_kheap, Function.update_of_ne hre, Function.update_of_ne hrt]; exact hkr + have hun := replyUnlinkTcb_eq hkr2 hback hk2 (ep := e) (d := d) (by exact hs) + let σ3 := updK σ2 r (.reply { xr with replyTcb := none }) + have k3 : CancelKeepA σ2 σ3 := CancelKeepA.updIpc (o' := .reply { xr with replyTcb := none }) hkr2 (Or.inr (Or.inr ⟨xr, rfl⟩)) + ⟨fun _ h => (by cases h), fun _ _ h => (by cases h)⟩ + have hk3 : σ3.kheap t = some (.tcb { x with tcbFault := none }) := by + simp only [σ3, updK_kheap, Function.update_of_ne hrt.symm]; exact hk2 + have s3 : StackKeepA σ2 σ3 := StackKeepA.upd (o' := .reply { xr with replyTcb := none }) hkr2 + (fun _ _ e => by cases e) (fun r' e => by cases e; exact ⟨xr, rfl, rfl⟩) + let σ4 := Notif.stsAct (updK σ3 t (.tcb { { x with tcbFault := none } with tcbState := .inactive })) t + have k4 : CancelKeepA σ3 σ4 := (CancelKeepA.updTcb hk3 rfl).trans (CancelKeepA.stsAct _ _) + have hk4 : σ4.kheap t = some (.tcb { { x with tcbFault := none } with tcbState := .inactive }) := by + simp only [σ4, stsAct_kheap, updK_kheap, Function.update_self] + have s4 : StackKeepA σ3 σ4 := (tcbS _ _ _ _ hk3).trans (StackKeepA.stsAct _ _) + refine ⟨_, cancelIpc_blocked hk (some r) (Or.inr ⟨e, d, hs⟩) (bci_reply hb hdq hun hk4), ?_⟩ + exact ⟨(((k1.trans k2).trans k3).trans k4).trans ((CancelKeepA.updTcb hk4 rfl).trans (CancelKeepA.stsAct _ _)), + (((s1.trans s2).trans s3).trans s4).trans ((tcbS _ _ _ _ hk4).trans (StackKeepA.stsAct _ _)), + { { { x with tcbFault := none } with tcbState := .inactive } with tcbState := .inactive }, + by rw [stsAct_kheap, updK_kheap, Function.update_self], IpcFreeA.inactive⟩ + rcases hs : x.tcbState with _ | _ | _ | ⟨e, ro, d⟩ | ⟨e, d⟩ | r | n | _ | _ + · exact ⟨_, cancelIpc_plain hk (by intro e d h; rw [hs] at h; cases h) (by intro e r d h; rw [hs] at h; cases h) + (by intro n h; rw [hs] at h; cases h) (by intro r h; rw [hs] at h; cases h), k1, s1, _, hk1, + by change IpcFreeA x.tcbState; rw [hs]; exact ⟨fun _ _ h => (by cases h), fun _ _ _ h => (by cases h), + fun _ h => (by cases h), fun _ h => (by cases h)⟩⟩ + · exact ⟨_, cancelIpc_plain hk (by intro e d h; rw [hs] at h; cases h) (by intro e r d h; rw [hs] at h; cases h) + (by intro n h; rw [hs] at h; cases h) (by intro r h; rw [hs] at h; cases h), k1, s1, _, hk1, + by change IpcFreeA x.tcbState; rw [hs]; exact ⟨fun _ _ h => (by cases h), fun _ _ _ h => (by cases h), + fun _ h => (by cases h), fun _ h => (by cases h)⟩⟩ + · exact ⟨_, cancelIpc_plain hk (by intro e d h; rw [hs] at h; cases h) (by intro e r d h; rw [hs] at h; cases h) + (by intro n h; rw [hs] at h; cases h) (by intro r h; rw [hs] at h; cases h), k1, s1, _, hk1, + by change IpcFreeA x.tcbState; rw [hs]; exact ⟨fun _ _ h => (by cases h), fun _ _ _ h => (by cases h), + fun _ h => (by cases h), fun _ h => (by cases h)⟩⟩ + · exact ep_arm e ro (Or.inr ⟨d, hs⟩) + · exact ep_arm e none (Or.inl ⟨d, hs, rfl⟩) + · exact absurd hs (hnr r) + · obtain ⟨z, hkz, hq⟩ := hN n hs + have hnt : n ≠ t := by intro h; rw [h, hk] at hkz; cases hkz + have hkz1 : σ1.kheap n = some (.notification z) := by + simp only [σ1, updK_kheap, Function.update_of_ne hnt]; exact hkz + have hdq := tcbNtfnDequeue_eq hkz1 hq + refine ⟨_, cancelIpc_ntfn hk hs (by + unfold cancelSignal + rw [nd_bind_one hdq] + exact setThreadState_eq _ (by rw [updK_kheap, Function.update_of_ne hnt.symm]; exact hk1)), ?_⟩ + have hkt1 : (updK σ1 n (.notification (ntfnObjUpdate (fun _ => match removeAll t (ntfnQueueOf z) with + | [] => Ntfn.idleNtfn + | _ => Ntfn.waitingNtfn (removeAll t (ntfnQueueOf z))) z))).kheap t = some (.tcb { x with tcbFault := none }) := by + rw [updK_kheap, Function.update_of_ne hnt.symm]; exact hk1 + refine ⟨?_, (s1.trans (StackKeepA.upd hkz1 (fun _ _ e => by cases e) (fun _ e => by cases e))).trans + ((tcbS _ _ _ _ hkt1).trans (StackKeepA.stsAct _ _)), + { { x with tcbFault := none } with tcbState := .inactive }, + by rw [stsAct_kheap, updK_kheap, Function.update_self], IpcFreeA.inactive⟩ + refine (k1.trans (CancelKeepA.updIpc (o' := .notification (ntfnObjUpdate (fun _ => match removeAll t (ntfnQueueOf z) with + | [] => Ntfn.idleNtfn + | _ => Ntfn.waitingNtfn (removeAll t (ntfnQueueOf z))) z)) hkz1 (Or.inr (Or.inl ⟨z, rfl⟩)) + ⟨fun _ h => (by cases h), fun _ _ h => (by cases h)⟩)).trans + ((CancelKeepA.updTcb (by rw [updK_kheap, Function.update_of_ne hnt.symm]; exact hk1) rfl).trans + (CancelKeepA.stsAct _ _)) + · exact ⟨_, cancelIpc_plain hk (by intro e d h; rw [hs] at h; cases h) (by intro e r d h; rw [hs] at h; cases h) + (by intro n h; rw [hs] at h; cases h) (by intro r h; rw [hs] at h; cases h), k1, s1, _, hk1, + by change IpcFreeA x.tcbState; rw [hs]; exact ⟨fun _ _ h => (by cases h), fun _ _ _ h => (by cases h), + fun _ h => (by cases h), fun _ h => (by cases h)⟩⟩ + · exact ⟨_, cancelIpc_plain hk (by intro e d h; rw [hs] at h; cases h) (by intro e r d h; rw [hs] at h; cases h) + (by intro n h; rw [hs] at h; cases h) (by intro r h; rw [hs] at h; cases h), k1, s1, _, hk1, + by change IpcFreeA x.tcbState; rw [hs]; exact ⟨fun _ _ h => (by cases h), fun _ _ _ h => (by cases h), + fun _ h => (by cases h), fun _ h => (by cases h)⟩⟩ + +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/CancelIpcReplyA.lean b/proofs/Fl4ProofsA64/P3/CancelIpcReplyA.lean new file mode 100644 index 000000000..8a3638ab4 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/CancelIpcReplyA.lean @@ -0,0 +1,598 @@ +/- +P3 (AArch64): the `BlockedOnReply` arm of `cancel_ipc` refines l4v `cancel_ipc` +(`reply_remove_tcb`). + +Rust clears the fault, then `reply_remove_tcb`: at the head of its SC's stack it clears the +SC's reply word; below the head it clears the neighbouring links; in both cases it clears the +removed reply's links and unlinks it (`reply_unlink`). + +**`cancel_ipc_replyA`** gives the spec outcome, `NtfnStateRel`, `IpcSchedRelA`, +`EpStateRel`, `ReplyStackRelA` with the stack cut at the reply, `ReplyIsolatedA`, the write +footprint `CancelReplyWA` and the spec frame `CancelKeepA`. + +Premises on the caller side: the reply the thread is blocked on is tracked (`r ∈ R`), points +back to it, and the SCs holding it are covered (`ScReplyCovA`); the tracked SCs are nonnull. + +No named premise. +-/ +import Fl4ProofsA64.P3.CancelIpcReplyProgA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4SpecA64 Sched + +noncomputable section + +/-! ## The relational arm -/ + +/-- What the `BlockedOnReply` arm writes (module docstring). -/ +def CancelReplyWA (N T C E R : Set U64) (Bf : Set (U64 × Word)) (ab : U64) (b : U64) : Prop := + CancelWA N T E R Bf ab b ∨ RLinkW R b ∨ ∃ sc ∈ C, b = scReplyA sc + +section Store +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + +/-- The final `reply_unlink` of the arm: every relation, from the state with the links cleared. -/ +theorem replyUnlinkEndA (hSched : CancelScheduleA st N Z.T (IpcSchedRelA st Z m κ)) + (hOE : SchedOpsEA st Z.T E R Bf (IpcSchedRelA st Z m κ)) (hP : IPlacedA N Z.T E R Bf) + (P : IpcSchedPlacementA Z.th N E R Bf) + {s : S} {σ : State Unit} {Q : U64 → U64 → List U64} {RR : U64 → List U64} {stack : U64 → List U64} + (H : SchedSRA st { Z with Q := Q, R := RR } m κ s σ) + (RN : NtfnStateRel N Z.T (L.mem s) σ) (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) {t r : U64} (ht : t ∈ Z.T) (hr : r ∈ R) + (hi : ReplyIsolatedA (L.mem s) Z.C stack r) + {x : Tcb} {xr : Reply} (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hkr : σ.kheap (specPtr r) = some (.reply xr)) (hback : xr.replyTcb = some (specPtr t)) + (hst : x.tcbState = .blockedOnReply (specPtr r)) + (hAct : Aligned (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION)) : + ipc.reply_unlink st s r t ⦃ s' => ∃ σ', + replyUnlinkTcb (Z := Unit) (specPtr t) (specPtr r) σ = ({((), σ')}, False) ∧ + NtfnStateRel N Z.T (L.mem s') σ' ∧ IpcSchedRelA st Z m κ s' σ' ∧ EpStateRel Z.T E R Bf (L.mem s') σ' ∧ + ReplyStackRelA (L.mem s') R Z.C σ' (scNameA m) stack ∧ ReplyIsolatedA (L.mem s') Z.C stack r ∧ + WritesIn st s s' (CancelReplyWA N Z.T Z.C E R Bf (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION)) ∧ + CancelKeepA σ σ' ∧ (∃ y, σ'.kheap (specPtr t) = some (.tcb y) ∧ y.tcbState = .inactive) ⦄ := by + have hblk : runnable x.tcbState = false ∧ x.tcbState ≠ .idleThreadState := by + rw [hst]; exact ⟨rfl, by simp⟩ + have hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue := + not_queued_codeA HE ht hk (by rw [hst]; simp [tsCode]) + have hrt : specPtr r ≠ specPtr t := by intro e; rw [e, hk] at hkr; cases hkr + have hSR : IpcSchedRelA st Z m κ s σ := ⟨Q, RR, H⟩ + have hN := reply_unlink_relI hSched hOE hP hSR RN HE (WritesIn.refl _ _) (EpStep.refl _) ht hk hk + (OnlyT.refl _ _) hblk hq (by rw [hst]; simp [tsCode]) hr hkr + have hU := unlinkReplyRelA H P hP.e HE HS ht hr hi hk hkr hback (Or.inl hst) hq + have hW := replyUnlinkFootA H P hP.e HE ht hr hk hkr (Or.inl hst) hq + refine WP.spec_mono (WP.spec_and hN (WP.spec_and hU hW)) ?_ + rintro s' ⟨⟨hN', hE', hS'⟩, ⟨σ', hu, H', HE', HS', hi'⟩, hw⟩ + have hfree : replyUnlinkTcb (Z := Unit) (specPtr t) (specPtr r) σ = + ({((), stsAct (updK (updK σ (specPtr r) (.reply { xr with replyTcb := none })) (specPtr t) + (.tcb { x with tcbState := .inactive })) (specPtr t))}, False) := by + have hkt' : (updK σ (specPtr r) (.reply { xr with replyTcb := none })).kheap (specPtr t) = some (.tcb x) := by + rw [updK_kheap, Function.update_of_ne hrt.symm, hk] + unfold replyUnlinkTcb + rw [nd_bind_one (getReply_eq hkr), nd_bind_one (assert_true hback σ), nd_bind_one (getThreadState_eq hk), + nd_bind_one (assert_true (Or.inl hst) σ), nd_bind_one (setReplyTcb_eq none hkr)] + exact setThreadState_eq .inactive hkt' + have hσ : σ' = stsAct (updK (updK σ (specPtr r) (.reply { xr with replyTcb := none })) (specPtr t) + (.tcb { x with tcbState := .inactive })) (specPtr t) := + congrArg Prod.snd (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst (hu.symm.trans hfree))) + subst hσ + refine ⟨_, hu, hN', hS', hE', HS', hi', ?_, ?_, + ⟨{ x with tcbState := .inactive }, by rw [stsAct_kheap, updK_kheap, Function.update_self], rfl⟩⟩ + · refine hw.mono fun b hb => ?_ + rcases hb with h | h + · exact Or.inl (Or.inl h) + · exact Or.inl (Or.inr (Or.inr h)) + · refine ((CancelKeepA.updIpc (o' := .reply { xr with replyTcb := none }) hkr (Or.inr (Or.inr ⟨xr, rfl⟩)) + ⟨fun _ h => (by cases h), fun _ _ h => (by cases h)⟩).trans (CancelKeepA.updTcb (x := x) + (y := { x with tcbState := .inactive }) ?_ (by cases x; rfl))).trans (CancelKeepA.stsAct _ _) + rw [updK_kheap, Function.update_of_ne hrt.symm, hk] + +/-- What an arm of `reply_remove_tcb` establishes, from the state after the fault clear. -/ +def ReplyArmPostA (st : machine.Store S) [L : LawfulStore st] (Z : XSetsA) (m : McState) (κ : U64 → U64) + (N E R : Set U64) (Bf : Set (U64 × Word)) (ab : U64) (s1 : S) (σ1 : State Unit) (t r : U64) + (s2 : S) : Prop := + ∃ σ' stack', replyRemoveTcb (Z := Unit) (specPtr t) (specPtr r) σ1 = ({((), σ')}, False) ∧ + NtfnStateRel N Z.T (L.mem s2) σ' ∧ IpcSchedRelA st Z m κ s2 σ' ∧ + EpStateRel Z.T E R Bf (L.mem s2) σ' ∧ ReplyStackRelA (L.mem s2) R Z.C σ' (scNameA m) stack' ∧ + ReplyIsolatedA (L.mem s2) Z.C stack' r ∧ + WritesIn st s1 s2 (CancelReplyWA N Z.T Z.C E R Bf ab) ∧ CancelKeepA σ1 σ' ∧ + ∃ y, σ'.kheap (specPtr t) = some (.tcb y) ∧ y.tcbState = .inactive + +/-- A link-word write keeps the notification relation. -/ +theorem ntfnLinkKeepA (hP : IPlacedA N Z.T E R Bf) {s2 s3 : S} {σ2 : State Unit} + (h : NtfnStateRel N Z.T (L.mem s2) σ2) (w : WritesIn st s2 s3 (RLinkW R)) : + NtfnStateRel N Z.T (L.mem s3) σ2 := by + refine h.frame hP.n (fun b hb hf => w.mem_eq hb fun ⟨q, hq, e⟩ => ?_) + ⟨fun _ _ h => h, fun _ x h => ⟨x, h, rfl, rfl, rfl⟩⟩ + have n := replyNextPlaceA (Z := Z) hP.e hq + have p := replyPrevPlaceA (Z := Z) hP.e hq + refine replyWordOffNFootA hP hq (b := b) ?_ ?_ hf <;> rcases e with rfl | rfl <;> omega + +set_option maxHeartbeats 1000000 in +/-- The detached arm: the reply is in no SC's stack. -/ +theorem replyArmDetachedA + (hSched : CancelScheduleA st N Z.T (IpcSchedRelA st Z m κ)) + (hOE : SchedOpsEA st Z.T E R Bf (IpcSchedRelA st Z m κ)) (hP : IPlacedA N Z.T E R Bf) + (P : IpcSchedPlacementA Z.th N E R Bf) + {s1 : S} {σ1 : State Unit} {Q : U64 → U64 → List U64} {RR : U64 → List U64} {stack : U64 → List U64} + (H1' : SchedSRA st { Z with Q := Q, R := RR } m κ s1 σ1) + (RN1 : NtfnStateRel N Z.T (L.mem s1) σ1) (HE1 : EpStateRel Z.T E R Bf (L.mem s1) σ1) + (HS1 : ReplyStackRelA (L.mem s1) R Z.C σ1 (scNameA m) stack) + {t r : U64} (ht : t ∈ Z.T) (hr : r ∈ R) {x1 : Tcb} {xr : Reply} + (kt1 : σ1.kheap (specPtr t) = some (.tcb x1)) (hst1 : x1.tcbState = .blockedOnReply (specPtr r)) + (kr1 : σ1.kheap (specPtr r) = some (.reply xr)) (hback : xr.replyTcb = some (specPtr t)) + (cov1 : ScReplyCovA Z.C m σ1 (specPtr r)) + (hrobj : obj.Tcb.reply_obj st t s1 = ok r) + (hAct : Aligned (L.nodeAt s1 (L.cpu s1) layout.node.SCHED_ACTION)) + (hd : ∀ sc ∈ Z.C, r ∉ stack sc) : + ipc.reply_remove_tcb st s1 t ⦃ s2 => ReplyArmPostA st Z m κ N E R Bf + (L.nodeAt s1 (L.cpu s1) layout.node.SCHED_ACTION) s1 σ1 t r s2 ⦄ := by + have hPE := hP.e + have hta := hPE.tcb t ht + have hrt : specPtr r ≠ specPtr t := by intro e; rw [e, kt1] at kr1; cases kr1 + obtain ⟨hh, hn, hp⟩ := HS1.detached r hr hd + have hnR : L.mem s1 (rNextA r) ≠ 0#u64 → L.mem s1 (rNextA r) ∈ R := fun h => (hn h).1 + have hpR : L.mem s1 (rPrevA r) ≠ 0#u64 → L.mem s1 (rPrevA r) ∈ R := fun h => (hp h).1 + have en := (eq_of_spec_fd (obj.Reply.next.spec (L := L) s1 r)).trans + (congrArg ok (replyMaskFA (Z := Z) hPE _ hnR)) + have ep := (eq_of_spec_fd (obj.Reply.prev.spec (L := L) s1 r)).trans + (congrArg ok (replyMaskFA (Z := Z) hPE _ hpR)) + rw [replyRemoveTcbSpliceProgramA s1 t r _ _ hrobj en ep (fun _ => hh)] + refine WP.spec_bind (WP.spec_and (replySplicePrepareRefA H1' P hPE HE1 hr hnR hpR) + (replySplicePrepareFootA (Z := Z) hPE s1 hr hnR hpR)) ?_ + rintro s2 ⟨⟨H2, E2, mP, mN, mPn, mNp, mPr, mNr⟩, w2⟩ + obtain ⟨S2, i2⟩ := HS1.detachedSplice (fun _ hd => replyPointerNonzeroA (Z := Z) hPE hd) hr hd + mP mN mPn mNp mPr mNr + have hA2 : L.nodeAt s2 (L.cpu s2) layout.node.SCHED_ACTION = L.nodeAt s1 (L.cpu s1) layout.node.SCHED_ACTION := by + rw [nodeAt_of_ksb w2.1, w2.2.1] + refine WP.spec_mono (replyUnlinkEndA hSched hOE hP P H2 (ntfnLinkKeepA hP RN1 w2) E2 S2 ht hr i2 kt1 kr1 + hback hst1 (by rw [hA2]; exact hAct)) ?_ + rintro s3 ⟨σ3, hu, N3, I3, E3, S3, i3, w3, K3, hin⟩ + rw [hA2] at w3 + refine ⟨σ3, stack, ?_, N3, I3, E3, S3, i3, ?_, K3, hin⟩ + · rw [replyRemoveTcb_noneA kt1 hst1 (HS1.scWithReply_none hd cov1)]; exact hu + exact ((WritesIn.mono w2 + fun b hb => (Or.inr (Or.inl hb) : CancelReplyWA N Z.T Z.C E R Bf (L.nodeAt s1 (L.cpu s1) layout.node.SCHED_ACTION) b)).trans w3).mono fun b hb => hb.elim id id + +set_option maxHeartbeats 1000000 in +/-- The head arm: the reply heads its SC's stack. -/ +theorem replyArmHeadA + (hSched : CancelScheduleA st N Z.T (IpcSchedRelA st Z m κ)) + (hOE : SchedOpsEA st Z.T E R Bf (IpcSchedRelA st Z m κ)) (hP : IPlacedA N Z.T E R Bf) + (P : IpcSchedPlacementA Z.th N E R Bf) + {s1 : S} {σ1 : State Unit} {Q : U64 → U64 → List U64} {RR : U64 → List U64} {stack : U64 → List U64} + (H1' : SchedSRA st { Z with Q := Q, R := RR } m κ s1 σ1) + (RN1 : NtfnStateRel N Z.T (L.mem s1) σ1) (HE1 : EpStateRel Z.T E R Bf (L.mem s1) σ1) + (HS1 : ReplyStackRelA (L.mem s1) R Z.C σ1 (scNameA m) stack) + {t r : U64} (ht : t ∈ Z.T) (hr : r ∈ R) {x1 : Tcb} {xr : Reply} + (kt1 : σ1.kheap (specPtr t) = some (.tcb x1)) (hst1 : x1.tcbState = .blockedOnReply (specPtr r)) + (kr1 : σ1.kheap (specPtr r) = some (.reply xr)) (hback : xr.replyTcb = some (specPtr t)) + (cov1 : ScReplyCovA Z.C m σ1 (specPtr r)) + (hrobj : obj.Tcb.reply_obj st t s1 = ok r) + (hAct : Aligned (L.nodeAt s1 (L.cpu s1) layout.node.SCHED_ACTION)) + (hC0 : ∀ c ∈ Z.C, c ≠ 0#u64) {sc : U64} (hc : sc ∈ Z.C) {rest : List U64} (hl : stack sc = r :: rest) : + ipc.reply_remove_tcb st s1 t ⦃ s2 => ReplyArmPostA st Z m κ N E R Bf + (L.nodeAt s1 (L.cpu s1) layout.node.SCHED_ACTION) s1 σ1 t r s2 ⦄ := by + have hPE := hP.e + have hta := hPE.tcb t ht + have hrt : specPtr r ≠ specPtr t := by intro e; rw [e, kt1] at kr1; cases kr1 + have hm : r ∈ stack sc := by rw [hl]; simp + have hh0 : (stack sc).head? = some r := by rw [hl]; rfl + obtain ⟨⟨y, n, hy, hL⟩, hR, hnd, hW⟩ := HS1.stackRel sc hc + have hsf := HS1.scWithReply_some hc hm cov1 + have kt1ne : specPtr t ≠ scNameA m sc := fun e => by rw [e, hy] at kt1; cases kt1 + have kr1ne : specPtr r ≠ scNameA m sc := fun e => by rw [e, hy] at kr1; cases kr1 + obtain ⟨rwNext, decNext, decPrev⟩ := replyHeadWordsA H1' hPE HS1 hc hl + have nx := (eq_of_spec_fd (obj.Reply.next.spec (L := L) s1 r)).trans (congrArg ok decNext) + have hhb := nx_head1 (L.mem s1 (rNextA r)) + rw [decNext, ← rwNext] at hhb + have hprev : L.mem s1 (rPrevA r) = rest.headD 0#u64 := by + have hv := (hW 0 (by rw [hl]; simp)).1 + simp only [hl, List.getElem_cons_zero] at hv + rw [hv] + cases rest <;> rfl + have hpR : L.mem s1 (rPrevA r) ≠ 0#u64 → L.mem s1 (rPrevA r) ∈ R := by + rw [hprev] + cases rest with + | nil => intro h; exact absurd rfl h + | cons d l => intro _; exact hR d (by rw [hl]; simp) + have ep := (eq_of_spec_fd (obj.Reply.prev.spec (L := L) s1 r)).trans + (congrArg ok (replyMaskFA (Z := Z) hPE _ hpR)) + have hsc0 := hC0 sc hc + rw [replyRemoveTcbHeadProgramA s1 t r sc _ hrobj nx ep hsc0 hhb] + have hhd : ∀ y' k', σ1.kheap (scNameA m sc) = some (.schedContext y' k') → + y'.scReplies.head? = some (specPtr r) := by + intro y' k' h + rw [hy] at h + cases h + rw [hL, hl] + rfl + have hrsc : xr.replySc = some (scNameA m sc) := (HS1.owner r hr xr kr1 _).2 ⟨sc, hc, rfl, hh0⟩ + have hpl : Aligned (scReplyA sc) := by + obtain ⟨y', k', -, hx'⟩ := H1'.scr.sc sc hc + have v : (scReplyA sc).val = sc.val + 32 := by + unfold scReplyA + rw [sc_word_val hx'.pl (by unfold Fl4SpecA64.minSchedContextBits; omega) (by simp [layout.sc.REPLY])] + simp [layout.sc.REPLY] + have h8 : sc.val % 8 = 0 := by + have hb : 8 ∣ 2 ^ (k' + Fl4SpecA64.minSchedContextBits) := + (show 8 ∣ 2 ^ 3 by decide).trans (pow_dvd_pow 2 (by unfold Fl4SpecA64.minSchedContextBits; omega)) + exact Nat.mod_eq_zero_of_dvd (hb.trans (Nat.dvd_of_mod_eq_zero hx'.pl.align)) + unfold Aligned + rw [v] + omega + have hSet : obj.Sc.set_reply st sc s1 0#u64 ⦃ s2 => s2 = L.wr s1 (scReplyA sc) 0#u64 ⦄ := + obj.Sc.set_reply.spec s1 sc 0#u64 + refine WP.spec_bind (WP.spec_and (reply_remove_tcb_scA H1' hc kr1 hhd) hSet) ?_ + rintro s2 ⟨⟨y', k', hy', hu, H2, wm, -⟩, hs2⟩ + rw [hy] at hy' + cases hy' + have wsc : WritesIn st s1 s2 (· = scReplyA sc) := by rw [hs2]; exact writesIn_wr hpl + set σa := repStA σ1 (scNameA m sc) y n [] with hσa + set σm := rscA σa (specPtr r) none with hσm + have kra : σa.kheap (specPtr r) = some (.reply xr) := by rw [hσa, repStA_ne kr1ne]; exact kr1 + have hm1 : σm = updK σa (specPtr r) (.reply { xr with replySc := none }) := by + rw [hσm, rscA_of kra]; rfl + have ktm : σm.kheap (specPtr t) = some (.tcb x1) := by + rw [hm1, updK_kheap, Function.update_of_ne hrt.symm, hσa, repStA_ne kt1ne]; exact kt1 + have krm : σm.kheap (specPtr r) = some (.reply { xr with replySc := none }) := by + rw [hm1, updK_kheap, Function.update_self] + have Ka : CancelKeepA σ1 σa := CancelKeepA.updSc hy [] + have Km : CancelKeepA σa σm := by + rw [hm1] + exact CancelKeepA.updIpc kra (Or.inr (Or.inr ⟨xr, rfl⟩)) ⟨fun _ h => (by cases h), fun _ _ h => (by cases h)⟩ + have N2 : NtfnStateRel N Z.T (L.mem s2) σm := by + refine RN1.frame hP.n (fun b hb hf => ?_) ?_ + · exact wm.mem_eq hb fun h => notifFootOffMWrA H1'.toSchedSRN P.notification hf h + · refine ⟨fun p z h => ?_, fun p z h => ?_⟩ + · have e1 : p ≠ scNameA m sc := fun e => by rw [e, hy] at h; cases h + have e2 : p ≠ specPtr r := fun e => by rw [e, kr1] at h; cases h + rw [hm1, updK_kheap, Function.update_of_ne e2, hσa, repStA_ne e1]; exact h + · have e1 : p ≠ scNameA m sc := fun e => by rw [e, hy] at h; cases h + have e2 : p ≠ specPtr r := fun e => by rw [e, kr1] at h; cases h + exact ⟨z, by rw [hm1, updK_kheap, Function.update_of_ne e2, hσa, repStA_ne e1]; exact h, rfl, rfl, rfl⟩ + -- the endpoint relation across the SC word and the spec updates + have hscOff : FootEq Z.T E R Bf (L.mem s1) (L.mem s2) := + (epFrameOfMcsA H1'.toSchedSRN P (σ := σ1) (σ' := σ1) ⟨wm, SKeepA.refl _, SSzA.refl _⟩ rfl).1 + have E2 : EpStateRel Z.T E R Bf (L.mem s2) σm := by + have Ea : EpStateRel Z.T E R Bf (L.mem s2) σa := by + refine HE1.frame hPE hscOff ?_ + refine ⟨fun p z h => ?_, fun p z h => ⟨z, ?_, rfl⟩, fun p z h => ⟨z, ?_, rfl, rfl, rfl, rfl, rfl⟩, rfl⟩ + all_goals + have e1 : p ≠ scNameA m sc := fun e => by rw [e, hy] at h; cases h + rw [hσa, repStA_ne e1]; exact h + rw [hm1] + obtain ⟨xr', hkr', hrt'⟩ := Ea.reply r hr + rw [kra] at hkr' + cases hkr' + exact Ea.upd_reply hPE hr { xr with replySc := none } (fun _ _ _ => rfl) hrt' + -- the link clears + have hmem2 : ∀ d ∈ R, L.mem s2 (rNextA d) = L.mem s1 (rNextA d) ∧ L.mem s2 (rPrevA d) = L.mem s1 (rPrevA d) := by + intro d hd' + have n' := replyNextPlaceA (Z := Z) hPE hd' + have p' := replyPrevPlaceA (Z := Z) hPE hd' + have off : ∀ b : U64, d.val ≤ b.val → b.val < d.val + 32 → b ≠ scReplyA sc := by + intro b h1 h2 e + have hdR := P.reply d hd' + have hsc := H1'.scp.th_out (a := b) ?_ hdR + · change b.val < d.val ∨ d.val + 32 ≤ b.val at hsc + omega + · obtain ⟨y2, k2, hk2, hx2⟩ := H1'.scr.sc sc hc + refine ⟨sc, hc, y2, k2, hk2, ?_⟩ + rw [e] + have h32 := hx2.hdr (K := 32#u64) (by simp) (by simp) + have ee : scReplyA sc = scWA sc 32#u64 := by unfold scReplyA scWA layout.sc.REPLY; rfl + rw [ee] + exact hx2.in_obj h32.2 + exact ⟨wsc.mem_eq n'.1 (off _ (by omega) (by omega)), wsc.mem_eq p'.1 (off _ (by omega) (by omega))⟩ + have hpR2 : L.mem s1 (rPrevA r) ≠ 0#u64 → L.mem s1 (rPrevA r) ∈ R := hpR + refine WP.spec_bind (WP.spec_and + (replySplicePrepareRefA H2 P hPE E2 hr (fun h => absurd rfl h) hpR2) + (replySplicePrepareFootA (Z := Z) hPE s2 hr (fun h => absurd rfl h) hpR2)) ?_ + rintro s3 ⟨⟨H3, E3, mP, mN, -, mNp, mPr, mNr⟩, w3⟩ + have S3 := HS1.popAll (fun _ hd => replyPointerNonzeroA (Z := Z) hPE hd) + (fun _ hd => replyPointerEvenA (Z := Z) hPE hd) hc hl + (mem' := L.mem s3) (σ' := σm) + (fun d hd hne => by + rw [mP d hd hne (replyPointerNonzeroA (Z := Z) hPE hd), (hmem2 d hd).2]) + (fun d hd hne hne' => by + rw [mN d hd hne (by rw [← hprev] at hne'; exact hne'), (hmem2 d hd).1]) + (fun h => by + have e : L.mem s1 (rPrevA r) = rest.headD 0#u64 := hprev + rw [← e] + exact mNp (by rw [e]; exact h)) + mPr mNr (y := y) (n := n) hy + (by + have e1 : scNameA m sc ≠ specPtr r := fun e => kr1ne e.symm + rw [hm1, updK_kheap, Function.update_of_ne e1, hσa] + exact updKSc_same _ _ _) + (fun q hq hne => by + obtain ⟨⟨yq, nq, hkq, -⟩, -⟩ := HS1.stackRel q hq + have e1 : scNameA m q ≠ scNameA m sc := fun e => hne (H1'.scp.inj q hq sc hc e) + have e2 : scNameA m q ≠ specPtr r := fun e => by rw [e, kr1] at hkq; cases hkq + rw [hm1, updK_kheap, Function.update_of_ne e2, hσa, repStA_ne e1]) + (xr := xr) kr1 krm + (fun d hd => let ⟨z, hz, _⟩ := HE1.reply d hd; ⟨z, hz⟩) + (fun d hd hne => by + obtain ⟨z, hz, -⟩ := HE1.reply d hd + have e1 : specPtr d ≠ scNameA m sc := fun e => by rw [e, hy] at hz; cases hz + have e2 : specPtr d ≠ specPtr r := fun e => hne (specPtr_inj e) + rw [hm1, updK_kheap, Function.update_of_ne e2, hσa, repStA_ne e1]) + have N3 := ntfnLinkKeepA hP N2 w3 + have hA3 : L.nodeAt s3 (L.cpu s3) layout.node.SCHED_ACTION = L.nodeAt s1 (L.cpu s1) layout.node.SCHED_ACTION := by + rw [nodeAt_of_ksb w3.1, w3.2.1, nodeAt_of_ksb wsc.1, wsc.2.1] + have hrs' : if Fl4SpecA64.the y.scReplies.head? = specPtr r then xr.replySc = some (scNameA m sc) + else xr.replySc = none := by + rw [if_pos (show Fl4SpecA64.the y.scReplies.head? = specPtr r by rw [hhd y n hy]; rfl)]; exact hrsc + refine WP.spec_mono (replyUnlinkEndA hSched hOE hP P H3 N3 E3 S3.1 ht hr S3.2 ktm krm + hback hst1 (by rw [hA3]; exact hAct)) ?_ + rintro s4 ⟨σ4, hu4, N4, I4, E4, S4, i4, w4, K4, hin⟩ + rw [hA3] at w4 + refine ⟨σ4, _, ?_, N4, I4, E4, S4, i4, ?_, (Ka.trans Km).trans K4, hin⟩ + · rw [replyRemoveTcb_someA kt1 hst1 hsf hy kr1 hrs' hu]; exact hu4 + have wsc' : WritesIn st s1 s2 (CancelReplyWA N Z.T Z.C E R Bf (L.nodeAt s1 (L.cpu s1) layout.node.SCHED_ACTION)) := + wsc.mono fun b hb => Or.inr (Or.inr ⟨sc, hc, hb⟩) + exact ((wsc'.trans (WritesIn.mono w3 + fun b hb => (Or.inr (Or.inl hb) : CancelReplyWA N Z.T Z.C E R Bf (L.nodeAt s1 (L.cpu s1) layout.node.SCHED_ACTION) b))).trans w4).mono fun b hb => by + rcases hb with (h | h) | h <;> exact h + +set_option maxHeartbeats 1000000 in +/-- The cut arm: the reply is below the head of its SC's stack. -/ +theorem replyArmCutA + (hSched : CancelScheduleA st N Z.T (IpcSchedRelA st Z m κ)) + (hOE : SchedOpsEA st Z.T E R Bf (IpcSchedRelA st Z m κ)) (hP : IPlacedA N Z.T E R Bf) + (P : IpcSchedPlacementA Z.th N E R Bf) + {s1 : S} {σ1 : State Unit} {Q : U64 → U64 → List U64} {RR : U64 → List U64} {stack : U64 → List U64} + (H1' : SchedSRA st { Z with Q := Q, R := RR } m κ s1 σ1) + (RN1 : NtfnStateRel N Z.T (L.mem s1) σ1) (HE1 : EpStateRel Z.T E R Bf (L.mem s1) σ1) + (HS1 : ReplyStackRelA (L.mem s1) R Z.C σ1 (scNameA m) stack) + {t r : U64} (ht : t ∈ Z.T) (hr : r ∈ R) {x1 : Tcb} {xr : Reply} + (kt1 : σ1.kheap (specPtr t) = some (.tcb x1)) (hst1 : x1.tcbState = .blockedOnReply (specPtr r)) + (kr1 : σ1.kheap (specPtr r) = some (.reply xr)) (hback : xr.replyTcb = some (specPtr t)) + (cov1 : ScReplyCovA Z.C m σ1 (specPtr r)) + (hrobj : obj.Tcb.reply_obj st t s1 = ok r) + (hAct : Aligned (L.nodeAt s1 (L.cpu s1) layout.node.SCHED_ACTION)) + {sc : U64} (hc : sc ∈ Z.C) {k : Nat} (hk' : k + 1 < (stack sc).length) (he : (stack sc)[k + 1] = r) : + ipc.reply_remove_tcb st s1 t ⦃ s2 => ReplyArmPostA st Z m κ N E R Bf + (L.nodeAt s1 (L.cpu s1) layout.node.SCHED_ACTION) s1 σ1 t r s2 ⦄ := by + have hPE := hP.e + have hta := hPE.tcb t ht + have hrt : specPtr r ≠ specPtr t := by intro e; rw [e, kt1] at kr1; cases kr1 + have hm : r ∈ stack sc := by rw [← he]; exact List.getElem_mem hk' + obtain ⟨⟨y, n, hy, hL⟩, hR, hnd, hW⟩ := HS1.stackRel sc hc + have hsf := HS1.scWithReply_some hc hm cov1 + have kt1ne : specPtr t ≠ scNameA m sc := fun e => by rw [e, hy] at kt1; cases kt1 + have kr1ne : specPtr r ≠ scNameA m sc := fun e => by rw [e, hy] at kr1; cases kr1 + subst he + have hprevR : (stack sc)[k] ∈ R := hR _ (List.getElem_mem (by omega : k < (stack sc).length)) + have h0 : 0 < (stack sc).length := by omega + have hHead : (stack sc).head? = some (stack sc)[0] := by + rw [List.head?_eq_getElem?, List.getElem?_eq_getElem h0] + have hne : (stack sc)[0] ≠ (stack sc)[k + 1] := by + intro e + have j := (List.nodup_iff_injective_get.1 hnd) (a₁ := ⟨0, h0⟩) (a₂ := ⟨k + 1, hk'⟩) + (by simpa using e) + exact Nat.ne_of_lt (Nat.zero_lt_succ k) (congrArg Fin.val j) + have hrs : xr.replySc = none := by + cases ho : xr.replySc with + | none => rfl + | some p => + obtain ⟨c, hc', _, hh⟩ := (HS1.owner _ hr xr kr1 p).1 ho + have e := HS1.unique c hc' sc hc _ (List.mem_of_mem_head? hh) hm + subst c + exact (hne (Option.some.inj (hHead.symm.trans hh))).elim + have hh : y.scReplies.head? = some (specPtr (stack sc)[0]) := by rw [hL, List.head?_map, hHead]; rfl + have hnePtr : specPtr (stack sc)[0] ≠ specPtr (stack sc)[k + 1] := fun e => hne (specPtr_inj e) + obtain ⟨-, H2, F⟩ := reply_remove_cutA H1' hc kr1 hrs hy hh hnePtr + obtain ⟨hu, -, -⟩ := reply_remove_tcb_cutA H1' hc kr1 hy hh hnePtr + set σa := repStA σ1 (scNameA m sc) y n + (y.scReplies.takeWhile fun d => decide (d ≠ specPtr (stack sc)[k + 1])) with hσa + have hof q (e : q ≠ scNameA m sc) : σa.kheap q = σ1.kheap q := repStA_ne e + have kta : σa.kheap (specPtr t) = some (.tcb x1) := by rw [hof _ kt1ne]; exact kt1 + have kra : σa.kheap (specPtr (stack sc)[k + 1]) = some (.reply xr) := by rw [hof _ kr1ne]; exact kr1 + have hsame : rscA σa (specPtr (stack sc)[k + 1]) none = σa := by + rw [rscA_of kra] + have e : ({ xr with replySc := none } : Reply) = xr := by + rw [← hrs] + rw [e] + unfold updKSc + rw [← kra, Function.update_eq_self] + rw [hsame] at hu + have Ka : CancelKeepA σ1 σa := CancelKeepA.updSc hy _ + have E2 := HE1.frame hPE (fun _ _ _ => rfl) (epFrameOfMcsA H1'.toSchedSRN P F rfl).2 + have N2 : NtfnStateRel N Z.T (L.mem s1) σa := by + refine RN1.frame hP.n (fun _ _ _ => rfl) ⟨fun p z h => ?_, fun p z h => ⟨z, ?_, rfl, rfl, rfl⟩⟩ + all_goals + have e1 : p ≠ scNameA m sc := fun e => by rw [e, hy] at h; cases h + rw [hof _ e1]; exact h + have hEq : (y.scReplies.takeWhile fun d => decide (d ≠ specPtr (stack sc)[k + 1])) = + ((stack sc).take (k + 1)).map specPtr := by rw [hL]; exact takeWhileSpecPtrPA _ hnd _ hk' + have hxa : σa.kheap (scNameA m sc) = + some (.schedContext { y with scReplies := ((stack sc).take (k + 1)).map specPtr } n) := by + rw [hσa] + unfold repStA + rw [hEq, updKSc_same] + have hraw : L.mem s1 (rNextA (stack sc)[k + 1]) = (stack sc)[k] := by + have h := (hW (k + 1) hk').2 + rw [h] + exact List.getD_eq_getElem _ _ (by omega) + have hev := replyPointerEvenA (Z := Z) hPE hprevR + have en : obj.Reply.next st (stack sc)[k + 1] s1 = ok (stack sc)[k] := + (eq_of_spec_fd (obj.Reply.next.spec s1 _)).trans (by rw [hraw, mask1_of_even hev]) + have eb : headBit (L.mem s1 (rNextA (stack sc)[k + 1])) = false := by rw [hraw]; exact headBitEvenA hev + have hpraw := (hW (k + 1) hk').1 + have hp : (stack sc).getD (k + 1 + 1) 0#u64 ≠ 0#u64 → (stack sc).getD (k + 1 + 1) 0#u64 ∈ R := by + intro h + by_cases hlt : k + 1 + 1 < (stack sc).length + · rw [List.getD_eq_getElem _ _ hlt] + exact hR _ (List.getElem_mem hlt) + · rw [List.getD_eq_default _ _ (by omega)] at h + exact (h rfl).elim + have ep : obj.Reply.prev st (stack sc)[k + 1] s1 = ok ((stack sc).getD (k + 1 + 1) 0#u64) := by + refine (eq_of_spec_fd (obj.Reply.prev.spec s1 _)).trans ?_ + rw [hpraw, replyMaskFA (Z := Z) hPE _ hp] + rw [replyRemoveTcbSpliceProgramA s1 t _ _ _ hrobj en ep (fun _ => eb)] + refine WP.spec_bind (WP.spec_and (replySplicePrepareRefA H2 P hPE E2 hr (fun _ => hprevR) hp) + (replySplicePrepareFootA (Z := Z) hPE s1 hr (fun _ => hprevR) hp)) ?_ + rintro s2 ⟨⟨H3, E3, mP, mN, mPn, mNp, mPr, mNr⟩, w2⟩ + have ev d (hd : d ∈ R) := replyPointerEvenA (Z := Z) hPE hd + have hn d (hd : d ∈ R) (hd1 : d ≠ (stack sc)[k + 1]) + (hd2 : ∀ h : k + 1 + 1 < (stack sc).length, d ≠ (stack sc)[k + 1 + 1]) := + mN d hd hd1 (by + by_cases h : k + 1 + 1 < (stack sc).length + · rw [List.getD_eq_getElem _ _ h]; exact hd2 h + · rw [List.getD_eq_default _ _ (by omega)]; exact replyPointerNonzeroA (Z := Z) hPE hd) + have hp1 (h : k + 1 + 1 < (stack sc).length) : + L.mem s2 (rNextA (stack sc)[k + 1 + 1]) = 0#u64 := by + have e := List.getD_eq_getElem (stack sc) (0#u64) h + have hnz := replyPointerNonzeroA (Z := Z) hPE (hR _ (List.getElem_mem h)) + exact e ▸ mNp (fun z => hnz (e.symm.trans z)) + obtain ⟨S3, i3⟩ := HS1.cut ev hc (k := k + 1) (by omega) hk' mP hn + (mPn (replyPointerNonzeroA (Z := Z) hPE hprevR)) hp1 mPr mNr hy hxa + (fun c hc' hne e => hne (H1'.scp.inj c hc' sc hc e)) + (fun d hd => let ⟨z, hz, _⟩ := HE1.reply d hd; ⟨z, hz⟩) hof + have hrs' : if Fl4SpecA64.the y.scReplies.head? = specPtr (stack sc)[k + 1] + then xr.replySc = some (scNameA m sc) else xr.replySc = none := by + rw [if_neg (show ¬ Fl4SpecA64.the y.scReplies.head? = specPtr (stack sc)[k + 1] by rw [hh]; exact hnePtr)] + exact hrs + have hA2 : L.nodeAt s2 (L.cpu s2) layout.node.SCHED_ACTION = L.nodeAt s1 (L.cpu s1) layout.node.SCHED_ACTION := by + rw [nodeAt_of_ksb w2.1, w2.2.1] + refine WP.spec_mono (replyUnlinkEndA hSched hOE hP P H3 (ntfnLinkKeepA hP N2 w2) E3 S3 ht hr i3 kta kra + hback hst1 (by rw [hA2]; exact hAct)) ?_ + rintro s3 ⟨σ3, hu3, N3, I3, E4, S4, i4, w3, K3, hin⟩ + rw [hA2] at w3 + refine ⟨σ3, _, ?_, N3, I3, E4, S4, i4, ?_, Ka.trans K3, hin⟩ + · rw [replyRemoveTcb_someA kt1 hst1 hsf hy kr1 hrs' hu]; exact hu3 + exact ((WritesIn.mono w2 + fun b hb => (Or.inr (Or.inl hb) : CancelReplyWA N Z.T Z.C E R Bf (L.nodeAt s1 (L.cpu s1) layout.node.SCHED_ACTION) b)).trans w3).mono fun b hb => hb.elim id id + +/-- **The `BlockedOnReply` arm of `cancel_ipc`** (module docstring). -/ +theorem cancel_ipc_replyA (hSched : CancelScheduleA st N Z.T (IpcSchedRelA st Z m κ)) + (hOE : SchedOpsEA st Z.T E R Bf (IpcSchedRelA st Z m κ)) (hP : IPlacedA N Z.T E R Bf) + (P : IpcSchedPlacementA Z.th N E R Bf) (s : S) (a : A) (σ : State Unit) {stack : U64 → List U64} + (H : IpcSchedRelA st Z m κ s σ) (RN : NtfnStateRel N Z.T (L.mem s) σ) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {t r : U64} (ht : t ∈ Z.T) (hr : r ∈ R) {x : Tcb} {xr : Reply} + (hk : σ.kheap (specPtr t) = some (.tcb x)) (hst : x.tcbState = .blockedOnReply (specPtr r)) + (hkr : σ.kheap (specPtr r) = some (.reply xr)) (hback : xr.replyTcb = some (specPtr t)) + (cov : ScReplyCovA Z.C m σ (specPtr r)) (hC0 : ∀ c ∈ Z.C, c ≠ 0#u64) + (hAct : Aligned (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION)) : + ipc.cancel_ipc st ar s a t ⦃ out => out.2 = a ∧ ∃ σ' stack', + cancelIpc (Z := Unit) (specPtr t) σ = ({((), σ')}, False) ∧ + NtfnStateRel N Z.T (L.mem out.1) σ' ∧ IpcSchedRelA st Z m κ out.1 σ' ∧ + EpStateRel Z.T E R Bf (L.mem out.1) σ' ∧ ReplyStackRelA (L.mem out.1) R Z.C σ' (scNameA m) stack' ∧ + ReplyIsolatedA (L.mem out.1) Z.C stack' r ∧ + WritesIn st s out.1 (CancelReplyWA N Z.T Z.C E R Bf (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION)) ∧ + CancelKeepA σ σ' ∧ (∃ y, σ'.kheap (specPtr t) = some (.tcb y) ∧ y.tcbState = .inactive) ⦄ := by + classical + have hPE := hP.e + have hta := hPE.tcb t ht + have hrt : specPtr r ≠ specPtr t := by intro e; rw [e, hk] at hkr; cases hkr + -- the fault clear + obtain ⟨RN1, HE1, W1, S1, hag⟩ := clear_both hP s σ RN HE ht hk + obtain ⟨s1, hs1⟩ : ∃ s1, s1 = L.wr s (tFaultA t) 0#u64 := ⟨_, rfl⟩ + rw [← hs1] at RN1 HE1 W1 hag + set x1 : Tcb := { x with tcbFault := none } with hx1 + set σ1 := updK σ (specPtr t) (.tcb x1) with hσ1 + have H1 : IpcSchedRelA st Z m κ s1 σ1 := by + refine hOE.stable _ _ _ _ H W1 S1 (NoBlockE.updT hk _ (Or.inl ⟨rfl, fun hc => ?_⟩)) + rcases hc with h | h | ⟨e, d, h, -⟩ | h + · exact Or.inl h + · exact Or.inr (Or.inl h) + · rw [show x1.tcbState = x.tcbState from rfl, hst] at h; cases h + · exact Or.inr (Or.inr (Or.inr h)) + have kt1 : σ1.kheap (specPtr t) = some (.tcb x1) := by rw [hσ1, updK_kheap, Function.update_self] + have kr1 : σ1.kheap (specPtr r) = some (.reply xr) := by + rw [hσ1, updK_kheap, Function.update_of_ne hrt, hkr] + have hst1 : x1.tcbState = .blockedOnReply (specPtr r) := hst + have hq1 : ∀ e ∈ E, ∀ z, σ1.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue := + not_queued_codeA HE1 ht kt1 (by rw [hst1]; simp [tsCode]) + have K1 : CancelKeepA σ σ1 := CancelKeepA.updTcb (x := x) (y := x1) hk (by cases x; rfl) + have far1 : ∀ q, q ≠ specPtr t → σ1.kheap q = σ.kheap q := fun q hq => by + rw [hσ1, updK_kheap, Function.update_of_ne hq] + -- the reply words are off the fault word + have hfw : ∀ d ∈ R, L.mem s1 (rNextA d) = L.mem s (rNextA d) ∧ L.mem s1 (rPrevA d) = L.mem s (rPrevA d) := by + intro d hd + have h := hPE.rt d hd t ht + have n := replyNextPlaceA (Z := Z) hPE hd + have p := replyPrevPlaceA (Z := Z) hPE hd + have vf := val_fault hta + exact ⟨hag _ n.1 (by dsimp only; rw [n.2, vf]; omega), hag _ p.1 (by dsimp only; rw [p.2, vf]; omega)⟩ + have HS1 : ReplyStackRelA (L.mem s1) R Z.C σ1 (scNameA m) stack := by + refine HS.frame hfw (fun c _ y k hy => ⟨y, k, ?_, rfl⟩) (fun d _ y hy => ⟨y, ?_, rfl⟩) + · rw [far1 _ (fun e => by rw [e, hk] at hy; cases hy)]; exact hy + · by_cases e : specPtr d = specPtr t + · rw [e, kt1] at hy; cases hy + · rw [far1 _ e] at hy; exact hy + have cov1 : ScReplyCovA Z.C m σ1 (specPtr r) := by + intro p y k hy hm + refine cov p y k ?_ hm + by_cases e : p = specPtr t + · rw [e, kt1] at hy; cases hy + · rw [far1 _ e] at hy; exact hy + have W1' : WritesIn st s s1 (CancelReplyWA N Z.T Z.C E R Bf (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION)) := + W1.mono fun b hb => Or.inl (Or.inl hb) + have hA1 : L.nodeAt s1 (L.cpu s1) layout.node.SCHED_ACTION = L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION := by + rw [nodeAt_of_ksb W1.1, W1.2.1] + -- the program up to the state read + have hrw : L.mem s1 (tReplyA t) = r := specPtr_inj ((HE1.at_tcb ht kt1).reply _ hst1) + have hrobj : obj.Tcb.reply_obj st t s1 = ok r := + (eq_of_spec_fd (obj.Tcb.reply_obj.spec (st := st) s1 t)).trans (congrArg ok hrw) + have hprog : ipc.cancel_ipc st ar s a t = (do + let s2 ← ipc.reply_remove_tcb st s1 t + ok (s2, a)) := by + have e1 : faults.clear_fault st s t = ok s1 := by + unfold faults.clear_fault + rw [eq_of_spec_fd (obj.Tcb.set_fault_type.spec (st := st) s t _), hs1] + unfold fl4_abi.consts.fault.NullFault + rfl + have e2 : obj.Tcb.state st t s1 = ok sched.TS_BLOCKED_ON_REPLY := by + refine (eq_of_spec_fd (obj.Tcb.state.spec (st := st) s1 t)).trans (congrArg ok ?_) + apply UScalar.eq_of_val_eq + have h := (HE1.at_tcb ht kt1).st + rw [hst1] at h + rw [show sched.TS_BLOCKED_ON_REPLY.val = tsCode (.blockedOnReply (specPtr r)) by + unfold sched.TS_BLOCKED_ON_REPLY; rfl] + exact h + have n1 : sched.TS_BLOCKED_ON_REPLY ≠ sched.TS_BLOCKED_ON_SEND := by + unfold sched.TS_BLOCKED_ON_REPLY sched.TS_BLOCKED_ON_SEND; decide + have n2 : sched.TS_BLOCKED_ON_REPLY ≠ sched.TS_BLOCKED_ON_RECV := by + unfold sched.TS_BLOCKED_ON_REPLY sched.TS_BLOCKED_ON_RECV; decide + have n3 : sched.TS_BLOCKED_ON_REPLY ≠ sched.TS_BLOCKED_ON_NTFN := by + unfold sched.TS_BLOCKED_ON_REPLY sched.TS_BLOCKED_ON_NTFN; decide + unfold ipc.cancel_ipc + rw [e1, bind_ok, e2, bind_ok, if_neg n1, if_neg n2, if_neg n3, if_pos rfl] + rw [hprog, cancelIpc_replyEqA hk hst] + obtain ⟨Q, RR, H1'⟩ := H1 + have hAct1 : Aligned (L.nodeAt s1 (L.cpu s1) layout.node.SCHED_ACTION) := by rw [hA1]; exact hAct + have arm : ipc.reply_remove_tcb st s1 t ⦃ s2 => ReplyArmPostA st Z m κ N E R Bf + (L.nodeAt s1 (L.cpu s1) layout.node.SCHED_ACTION) s1 σ1 t r s2 ⦄ := by + by_cases hd : ∀ sc ∈ Z.C, r ∉ stack sc + · exact replyArmDetachedA hSched hOE hP P H1' RN1 HE1 HS1 ht hr kt1 hst1 kr1 hback cov1 hrobj hAct1 hd + have hLive : ∃ sc ∈ Z.C, r ∈ stack sc := by push_neg at hd; exact hd + obtain ⟨sc, hc, hm⟩ := hLive + obtain ⟨k, hk', he⟩ := List.getElem_of_mem hm + cases k with + | zero => + obtain ⟨rest, hl⟩ : ∃ rest, stack sc = r :: rest := by + cases ee : stack sc with + | nil => rw [ee] at hk'; cases hk' + | cons d l => + simp only [ee, List.getElem_cons_zero] at he + exact ⟨l, by rw [he]⟩ + exact replyArmHeadA hSched hOE hP P H1' RN1 HE1 HS1 ht hr kt1 hst1 kr1 hback cov1 hrobj hAct1 hC0 hc hl + | succ k => + exact replyArmCutA hSched hOE hP P H1' RN1 HE1 HS1 ht hr kt1 hst1 kr1 hback cov1 hrobj hAct1 hc hk' he + refine WP.spec_bind arm ?_ + rintro s2 ⟨σ', stack', h1, h2, h3, h4, h5, h6, h7, h8, h9⟩ + rw [WP.spec_ok] + rw [hσ1, hx1] at h1 + rw [hA1] at h7 + exact ⟨rfl, σ', stack', h1, h2, h3, h4, h5, h6, (W1'.trans h7).mono fun _ hb => hb.elim id id, + K1.trans h8, h9⟩ + +end Store +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/CancelIpcReplyProgA.lean b/proofs/Fl4ProofsA64/P3/CancelIpcReplyProgA.lean new file mode 100644 index 000000000..30a7ce462 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/CancelIpcReplyProgA.lean @@ -0,0 +1,216 @@ +/- +P3 (AArch64): the spec equations, notification frames and program forms of the +`BlockedOnReply` arm of `cancel_ipc` (`CancelIpcReplyA`). + +No named premise. +-/ +import Fl4ProofsA64.P3.CancelSchedA +import Fl4ProofsA64.P3.CancelIpcFootA +import Fl4ProofsA64.P3.CancelIpcKeepA +import Fl4ProofsA64.P3.ReplyRemoveAllFootA +import Fl4ProofsA64.P3.ReplyStackPopAllA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4SpecA64 Sched + +noncomputable section + +/-! ## Spec side -/ + +section Spec + +theorem cancelIpc_replyEqA {σ : State Unit} {t r : ObjRef} {x : Tcb} (hk : σ.kheap t = some (.tcb x)) + (hs : x.tcbState = .blockedOnReply r) : + cancelIpc (Z := Unit) t σ = replyRemoveTcb (Z := Unit) t r (updK σ t (.tcb { x with tcbFault := none })) := by + unfold cancelIpc + rw [nd_bind_one (getThreadState_eq hk), nd_bind_one (threadSet_eq _ hk), hs] + +theorem replyRemoveTcb_noneA {σ : State Unit} {t r : ObjRef} {x : Tcb} (hk : σ.kheap t = some (.tcb x)) + (hs : x.tcbState = .blockedOnReply r) (hf : scWithReply r σ = none) : + replyRemoveTcb (Z := Unit) t r σ = replyUnlinkTcb (Z := Unit) t r σ := by + unfold replyRemoveTcb + rw [nd_bind_oneSc (getThreadState_eq hk), nd_bind_oneSc (assert_trueSc hs σ), nd_gets_bindSc, hf] + all_goals exact nd_bind_oneSc (show Fl4Spec.NonDet.pure () σ = ({((), σ)}, False) from rfl) _ + +theorem replySc_getRA {σ : State Unit} {r : ObjRef} {xr : Reply} (hk : σ.kheap r = some (.reply xr)) : + getReplyObjRef (Z := Unit) Reply.replySc r σ = ({(xr.replySc, σ)}, False) := by + unfold getReplyObjRef getSkObjRef + exact nd_bind_oneSc (getReply_eq hk) _ + +theorem replyRemoveTcb_someA {σ σm : State Unit} {t r p : ObjRef} {x : Tcb} (hk : σ.kheap t = some (.tcb x)) + (hs : x.tcbState = .blockedOnReply r) (hf : scWithReply r σ = some p) + {y : SchedContext} {n : Nat} (hy : σ.kheap p = some (.schedContext y n)) + {xr : Reply} (hkr : σ.kheap r = some (.reply xr)) + (hrs : if Fl4SpecA64.the y.scReplies.head? = r then xr.replySc = some p else xr.replySc = none) + (hu : (updateSchedContext (Z := Unit) p (scRepliesUpdate (List.takeWhile fun d => decide (d ≠ r))) >>= + fun _ => setReplyObjRef (Z := Unit) replyScUpdate r none) σ = ({((), σm)}, False)) : + replyRemoveTcb (Z := Unit) t r σ = replyUnlinkTcb (Z := Unit) t r σm := by + unfold replyRemoveTcb + rw [nd_bind_oneSc (getThreadState_eq hk), nd_bind_oneSc (assert_trueSc hs σ), nd_gets_bindSc, hf] + simp only [nd_bind_assocSc, nd_bind_oneSc (scReplies_getA hy), nd_bind_oneSc (replySc_getRA hkr)] + rw [nd_bind_oneSc (assert_trueSc (σ := State Unit) hrs σ), ← nd_bind_assocSc] + exact nd_bind_oneSc hu _ + +/-- `cancel_ipc`'s frame for a reply list change. -/ +theorem CancelKeepA.updSc {σ : State Unit} {p : ObjRef} {y : SchedContext} {n : Nat} + (hk : σ.kheap p = some (.schedContext y n)) (l : List ObjRef) : + CancelKeepA σ (updK σ p (.schedContext { y with scReplies := l } n)) := by + refine ⟨fun q o hq _ b _ _ _ => ?_, fun q x' hq => ?_, fun q y' k hq => ?_, rfl⟩ + · have e : q ≠ p := fun e => by subst e; rw [hk] at hq; cases hq; exact b y n rfl + rw [updK_kheap, Function.update_of_ne e]; exact hq + · have e : q ≠ p := fun e => by subst e; rw [hk] at hq; cases hq + exact ⟨x', by rw [updK_kheap, Function.update_of_ne e]; exact hq, by cases x'; rfl⟩ + · by_cases e : q = p + · subst e + rw [hk] at hq + cases hq + exact ⟨_, by rw [updK_kheap, Function.update_self], rfl⟩ + · exact ⟨y', by rw [updK_kheap, Function.update_of_ne e]; exact hq, by cases y'; rfl⟩ + +end Spec + +/-! ## The notification frame of scheduler and reply writes -/ + +section Placement +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +local notation "wa" => core.num.U64.wrapping_add + +variable {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + +private theorem typedWordOffStateBlockR (H : SchedSRN st Z m κ s σ) {p b : U64} {k : ObjKindA} + (hp : Z.th p = some k) (hne : k ≠ .cnode 0) (hlo : p.val ≤ b.val) (hhi : b.val < p.val + k.size) : + b.val < (L.ksb s).val ∨ (L.ksb s).val + ksSizeA ≤ b.val := by + have h := H.place.ks p k hp (fun irq => hne (H.place.irqK hp irq)) + omega + +private theorem typedWordOffScR (H : SchedSRN st Z m κ s σ) {p b : U64} {k : ObjKindA} + (hp : Z.th p = some k) (hlo : p.val ≤ b.val) (hhi : b.val < p.val + k.size) : + ¬ ScFootA Z.C m σ b := by + intro h + have hd := H.scp.th_out h hp + omega + +private theorem tcbFieldOffDistinctR (H : SchedSRN st Z m κ s σ) {t b : U64} (ht : t ∈ Z.T) + (hlo : t.val ≤ b.val) (hhi : b.val < t.val + 4096) {u off : U64} (hu : u ∈ Z.T) + (hof : off.val < 4096) (hd : b.val - t.val ≠ off.val) : b ≠ wa u off := by + intro eq + have hev := congrArg UScalar.val eq + rw [Notif.tfield_val (H.core.tcbs u hu) hof] at hev + by_cases htu : t = u + · subst u + omega + · have h := H.place.apart t u .tcb .tcb ((H.thT t).2 ht) ((H.thT u).2 hu) htu + change t.val + 4096 ≤ u.val ∨ u.val + 4096 ≤ t.val at h + omega + +private theorem tcbFieldOffWriteR (H : SchedSRN st Z m κ s σ) {t b : U64} (ht : t ∈ Z.T) + (hlo : t.val ≤ b.val) (hhi : b.val < t.val + 4096) + (hoff : ∀ k : Nat, k = 3464 ∨ k = 3480 ∨ k = 3488 ∨ k = 3592 → b.val - t.val ≠ k) : + ¬ MWrA (st := st) Z m σ s b := by + have hty := (H.thT t).2 ht + refine not_mwrA (typedWordOffScR H hty hlo hhi) ?_ (typedWordOffStateBlockR H hty (by decide) hlo hhi) + intro u hu + exact ⟨tcbFieldOffDistinctR H ht hlo hhi hu (off := 3464#u64) (by decide) (hoff _ (by decide)), + tcbFieldOffDistinctR H ht hlo hhi hu (off := 3480#u64) (by decide) (hoff _ (by decide)), + tcbFieldOffDistinctR H ht hlo hhi hu (off := 3488#u64) (by decide) (hoff _ (by decide)), + tcbFieldOffDistinctR H ht hlo hhi hu (off := 3592#u64) (by decide) (hoff _ (by decide))⟩ + +/-- The notification footprint is outside the scheduler write footprint. -/ +theorem notifFootOffMWrA {N : Set U64} (H : SchedSRN st Z m κ s σ) + (hN : ∀ n ∈ N, Z.th n = some (.other 64)) {b : U64} (hb : Notif.NFoot N Z.T b) : + ¬ MWrA (st := st) Z m σ s b := by + rcases hb with ⟨n, hn, hlo, hhi⟩ | ⟨t, ht, h⟩ + · have hp := hN n hn + have hhi' : b.val < n.val + 64 := by omega + have hK := typedWordOffStateBlockR H hp (by intro e; cases e) hlo hhi' + have hS := typedWordOffScR H hp hlo hhi' + refine H.not_mwr hS (fun t ht => ?_) hK + have hty := (H.thT t).2 ht + have hne : n ≠ t := by + rintro rfl + rw [hty] at hp + cases hp + have h := H.place.apart n t (.other 64) .tcb hp hty hne + change n.val + 64 ≤ t.val ∨ t.val + 4096 ≤ n.val at h + omega + have hval : b.val = t.val + 3328 ∨ b.val = t.val + 3336 ∨ b.val = t.val + 256 ∨ + b.val = t.val + 3448 ∨ b.val = t.val + 3496 ∨ b.val = t.val + 3504 := by + rcases h with h | h | h | h | h | h <;> + rw [h] <;> + simp only [stA_eq, Notif.tBlockA_eq, Notif.badgeRegA_eq, Notif.prioA_eq', Notif.nextEp_eq, + Notif.prevEp_eq] <;> + rw [Notif.tfield_val (H.core.tcbs t ht)] <;> simp + exact tcbFieldOffWriteR H ht (by omega) (by omega) (by intro k hk; omega) + +end Placement + +/-- A reply object's words are outside the notification footprint. -/ +theorem replyWordOffNFootA {N T E R : Set U64} {Bf : Set (U64 × Word)} (hP : IPlacedA N T E R Bf) + {r b : U64} (hr : r ∈ R) (hlo : r.val ≤ b.val) (hhi : b.val < r.val + 32) : ¬ Notif.NFoot N T b := by + rintro (⟨n, hn, h1, h2⟩ | ⟨t, ht, h⟩) + · have := hP.nr n hn r hr; omega + have hta := hP.n.tcb t ht + have hval : b.val = t.val + 3328 ∨ b.val = t.val + 3336 ∨ b.val = t.val + 256 ∨ + b.val = t.val + 3448 ∨ b.val = t.val + 3496 ∨ b.val = t.val + 3504 := by + rcases h with h | h | h | h | h | h <;> + rw [h] <;> + simp only [stA_eq, Notif.tBlockA_eq, Notif.badgeRegA_eq, Notif.prioA_eq', Notif.nextEp_eq, + Notif.prevEp_eq] <;> + rw [Notif.tfield_val hta] <;> simp + have := hP.e.rt r hr t ht + omega + +/-! ## The program -/ + +section Program +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +theorem splicePrepare_eqRA (s : S) (r nx pv : U64) : + replySplicePrepareA (st := st) s r nx pv = (do + let s1 ← clearLinkFA (st := st) s false nx + let s2 ← clearLinkFA (st := st) s1 true pv + let s3 ← obj.Reply.set_prev st r s2 0#u64 + obj.Reply.set_next st r s3 0#u64 false) := rfl + +theorem replyRemoveTcbSpliceProgramA (s : S) (t r nx pv : U64) (hr : obj.Tcb.reply_obj st t s = ok r) + (hn : obj.Reply.next st r s = ok nx) (hp : obj.Reply.prev st r s = ok pv) + (hh : nx ≠ 0#u64 → headBit (L.mem s (rNextA r)) = false) : + ipc.reply_remove_tcb st s t = (do + let s1 ← replySplicePrepareA (st := st) s r nx pv + ipc.reply_unlink st s1 r t) := by + unfold ipc.reply_remove_tcb + rw [hr, bind_ok, hn, bind_ok, hp, bind_ok] + by_cases h : (nx != 0#u64) = true + · have h0 : nx ≠ 0#u64 := by simpa using h + rw [if_pos h, eq_of_spec_fd (obj.Reply.next_is_head.spec s r), bind_ok, hh h0, + if_neg Bool.false_ne_true] + rw [splicePrepare_eqRA] + unfold clearLinkFA + simp only [h, Bool.false_eq_true, ↓reduceIte, Std.bind_assoc] + · rw [if_neg h] + rw [splicePrepare_eqRA] + unfold clearLinkFA + simp only [h, Bool.false_eq_true, ↓reduceIte, Std.bind_assoc] + +theorem replyRemoveTcbHeadProgramA (s : S) (t r sc pv : U64) (hr : obj.Tcb.reply_obj st t s = ok r) + (hn : obj.Reply.next st r s = ok sc) (hp : obj.Reply.prev st r s = ok pv) (h0 : sc ≠ 0#u64) + (hh : headBit (L.mem s (rNextA r)) = true) : + ipc.reply_remove_tcb st s t = (do + let s0 ← obj.Sc.set_reply st sc s 0#u64 + let s1 ← replySplicePrepareA (st := st) s0 r 0#u64 pv + ipc.reply_unlink st s1 r t) := by + unfold ipc.reply_remove_tcb + rw [hr, bind_ok, hn, bind_ok, hp, bind_ok, + if_pos (show (sc != 0#u64) = true by simpa using h0), + eq_of_spec_fd (obj.Reply.next_is_head.spec s r), bind_ok, hh, if_pos rfl] + simp only [splicePrepare_eqRA] + unfold clearLinkFA + simp only [bne_self_eq_false, Bool.false_eq_true, ↓reduceIte, bind_ok, Std.bind_assoc] + +end Program + +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/CancelSchedA.lean b/proofs/Fl4ProofsA64/P3/CancelSchedA.lean new file mode 100644 index 000000000..33a17b4e0 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/CancelSchedA.lean @@ -0,0 +1,152 @@ +/- +P3 (AArch64): the IPC cancellation's `schedule_tcb` contract over the merged relation. + +**`cancelScheduleA_of_placement`**: `CancelScheduleA st N Z.T (IpcSchedRelA st Z m κ)` from the +IPC/scheduler placement. `schedule_tcb` writes only the current core's action word, which lies +outside every notification and TCB word `NtfnStateRel` reads (`notifFootOffWrite`, a copy of the +private helper chain of `IpcScGlueA`). + +No named premise. +-/ +import Fl4ProofsA64.P3.IpcScGlueA +import Fl4ProofsA64.P3.IpcCancelA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4SpecA64 Sched + +noncomputable section + +section Placement +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +local notation "wa" => core.num.U64.wrapping_add + +variable {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + +private theorem typedWordOffStateBlock (H : SchedSRN st Z m κ s σ) {p b : U64} {k : ObjKindA} + (hp : Z.th p = some k) (hne : k ≠ .cnode 0) (hlo : p.val ≤ b.val) (hhi : b.val < p.val + k.size) : + b.val < (L.ksb s).val ∨ (L.ksb s).val + ksSizeA ≤ b.val := by + have h := H.place.ks p k hp (fun irq => hne (H.place.irqK hp irq)) + omega + +private theorem typedWordOffSc (H : SchedSRN st Z m κ s σ) {p b : U64} {k : ObjKindA} + (hp : Z.th p = some k) (hlo : p.val ≤ b.val) (hhi : b.val < p.val + k.size) : + ¬ ScFootA Z.C m σ b := by + intro h + have hd := H.scp.th_out h hp + omega + +private theorem schedReadOff (H : SchedSRN st Z m κ s σ) {b : U64} + (hK : b.val < (L.ksb s).val ∨ (L.ksb s).val + ksSizeA ≤ b.val) + (hS : ¬ ScFootA Z.C m σ b) + (hT : ∀ u ∈ Z.T, ∀ off : U64, off.val < 4096 → + (off = 3520#u64 ∨ off = 3448#u64 ∨ off = 3432#u64 ∨ off = 3464#u64 ∨ + off = 3592#u64 ∨ off = 3480#u64 ∨ off = 3488#u64) → b ≠ wa u off) : + ¬ SReadA st Z m s σ b := by + have hk (off : U64) (ho : off.val < ksSizeA) : b ≠ wa (L.ksb s) off := by + intro eq + have hfit := H.ksFit + have hv : (wa (L.ksb s) off).val = (L.ksb s).val + off.val := by rw [wa_valm]; omega + rw [eq, hv] at hK + omega + rintro (⟨c, hc, off, ho, rfl⟩ | e | e | ⟨u, hu, off, hoff, e⟩ | h) + · have he := H.node_in hc ho + omega + · exact hk 8#u64 (by decide) e + · exact hk 16#u64 (by decide) e + · apply hT u hu off _ hoff e + rcases hoff with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> decide + · exact hS h + +private theorem otherWordOffSched (H : SchedSRN st Z m κ s σ) {p b : U64} {len : Nat} + (hp : Z.th p = some (.other len)) (hlo : p.val ≤ b.val) (hhi : b.val < p.val + len) : + (¬ SReadA st Z m s σ b) ∧ (¬ MWrA (st := st) Z m σ s b) := by + have hK := typedWordOffStateBlock H hp (by intro e; cases e) hlo hhi + have hS := typedWordOffSc H hp hlo hhi + have hT : ∀ t ∈ Z.T, b.val < t.val ∨ t.val + 4096 ≤ b.val := by + intro t ht + have hty := (H.thT t).2 ht + have hne : p ≠ t := by + rintro rfl + rw [hty] at hp + cases hp + have h := H.place.apart p t (.other len) .tcb hp hty hne + change p.val + len ≤ t.val ∨ t.val + 4096 ≤ p.val at h + omega + refine ⟨schedReadOff H hK hS ?_, H.not_mwr hS hT hK⟩ + intro t ht off hof _ eq + have h := hT t ht + rw [eq, Notif.tfield_val (H.core.tcbs t ht) hof] at h + omega + +private theorem tcbFieldOffDistinct (H : SchedSRN st Z m κ s σ) {t b : U64} (ht : t ∈ Z.T) + (hlo : t.val ≤ b.val) (hhi : b.val < t.val + 4096) {u off : U64} (hu : u ∈ Z.T) + (hof : off.val < 4096) (hd : b.val - t.val ≠ off.val) : b ≠ wa u off := by + intro eq + have hev := congrArg UScalar.val eq + rw [Notif.tfield_val (H.core.tcbs u hu) hof] at hev + by_cases htu : t = u + · subst u + omega + · have h := H.place.apart t u .tcb .tcb ((H.thT t).2 ht) ((H.thT u).2 hu) htu + change t.val + 4096 ≤ u.val ∨ u.val + 4096 ≤ t.val at h + omega + +private theorem tcbFieldOffRead (H : SchedSRN st Z m κ s σ) {t b : U64} (ht : t ∈ Z.T) + (hlo : t.val ≤ b.val) (hhi : b.val < t.val + 4096) + (hoff : ∀ k : Nat, k = 3520 ∨ k = 3448 ∨ k = 3432 ∨ k = 3464 ∨ k = 3592 ∨ k = 3480 ∨ k = 3488 → + b.val - t.val ≠ k) : ¬ SReadA st Z m s σ b := by + have hty := (H.thT t).2 ht + refine schedReadOff H (typedWordOffStateBlock H hty (by decide) hlo hhi) (typedWordOffSc H hty hlo hhi) ?_ + intro u hu off ho hd + apply tcbFieldOffDistinct H ht hlo hhi hu ho + apply hoff + rcases hd with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> decide + +private theorem tcbFieldOffWrite (H : SchedSRN st Z m κ s σ) {t b : U64} (ht : t ∈ Z.T) + (hlo : t.val ≤ b.val) (hhi : b.val < t.val + 4096) + (hoff : ∀ k : Nat, k = 3464 ∨ k = 3480 ∨ k = 3488 ∨ k = 3592 → b.val - t.val ≠ k) : + ¬ MWrA (st := st) Z m σ s b := by + have hty := (H.thT t).2 ht + refine not_mwrA (typedWordOffSc H hty hlo hhi) ?_ (typedWordOffStateBlock H hty (by decide) hlo hhi) + intro u hu + exact ⟨tcbFieldOffDistinct H ht hlo hhi hu (off := 3464#u64) (by decide) (hoff _ (by decide)), + tcbFieldOffDistinct H ht hlo hhi hu (off := 3480#u64) (by decide) (hoff _ (by decide)), + tcbFieldOffDistinct H ht hlo hhi hu (off := 3488#u64) (by decide) (hoff _ (by decide)), + tcbFieldOffDistinct H ht hlo hhi hu (off := 3592#u64) (by decide) (hoff _ (by decide))⟩ + +private theorem notifFootOffWrite {N : Set U64} (H : SchedSRN st Z m κ s σ) + (hN : ∀ n ∈ N, Z.th n = some (.other 64)) {b : U64} (hb : Notif.NFoot N Z.T b) : + ¬ MWrA (st := st) Z m σ s b := by + rcases hb with ⟨n, hn, hlo, hhi⟩ | ⟨t, ht, h⟩ + · exact (otherWordOffSched H (hN n hn) hlo (by omega)).2 + have hval : b.val = t.val + 3328 ∨ b.val = t.val + 3336 ∨ b.val = t.val + 256 ∨ + b.val = t.val + 3448 ∨ b.val = t.val + 3496 ∨ b.val = t.val + 3504 := by + rcases h with h | h | h | h | h | h <;> + rw [h] <;> + simp only [stA_eq, Notif.tBlockA_eq, Notif.badgeRegA_eq, Notif.prioA_eq', Notif.nextEp_eq, + Notif.prevEp_eq] <;> + rw [Notif.tfield_val (H.core.tcbs t ht)] <;> simp + exact tcbFieldOffWrite H ht (by omega) (by omega) (by intro k hk; omega) + + +end Placement + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **The cancellation's `schedule_tcb` contract over `IpcSchedRelA`** (module docstring). -/ +theorem cancelScheduleA_of_placement {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} + {Bf : Set (U64 × Word)} (P : IpcSchedPlacementA Z.th N E R Bf) : + CancelScheduleA st N Z.T (IpcSchedRelA st Z m κ) := by + rintro s σ t hRel ⟨Q, R', H⟩ _ + refine WP.spec_mono (schedule_tcb_refinesA H t (fun _ ht _ hx => (hRel.at_tcb ht hx).st)) ?_ + rintro s' ⟨hw, H'⟩ + exact ⟨fun b hb hf => hw.mem_eq hb (fun eq => notifFootOffWrite H.toSchedSRN P.notification hf + (eq.symm ▸ swAction (Z := { Z with Q := Q, R := R' }) H)), ⟨Q, R', H'⟩⟩ + +end +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/CapRelA.lean b/proofs/Fl4ProofsA64/P3/CapRelA.lean new file mode 100644 index 000000000..4b77cb4ef --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/CapRelA.lean @@ -0,0 +1,329 @@ +/- +P3 (aarch64, A64-P3 S3 gen 1): **the abstraction of AArch64 capabilities**, the A64 twin of x86's `P3/CapRel` +(`absCap`, `CapWf`, `CapRel`), against `Fl4SpecA64` (l4v `rt` AARCH64 with the `ext` hook). + +FL4's AArch64 kernel reuses x86's `capability.Cap` constructors (`Fl4CoreA64/Types.lean`; `arch/aarch64/objects.rs` +`create_cap`): the VSpace root is `Cap::Pml4`, page tables of every level are `Cap::PageTable`, a Stage-2 VSpace is +`Cap::Ept { level: 0 }`, an `ArmHighVSpace` is `Cap::HighVSpace`. The spec side is l4v's AARCH64 `arch_cap` with +the `ext` hook (`Fl4SpecA64/Ext/ArchTypes`): + +| FL4 cap | `absCapA` | x86 counterpart | +|---|---|---| +| `Pml4 p m asid` | `pageTableCap p VSRootPT_T (asid, 0)` | `pml4Cap` | +| `PageTable p m asid va` | `pageTableCap p NormalPT_T (asid, va)` | `pageTableCap` | +| `Frame p sz r dev m ept asid va` | `absFrameE ept …`: `frameCap p (vm rights) sz dev (asid, va)`, or (S3 gen 18) `ext (stage2FrameCap …)` if `ept` | `pageCap` | +| `Vcpu p` | `vcpuCap p` | `vcpuCap` | +| `Ept p 0 m asid _` | `ext (stage2VSpaceCap p asid)` | `eptCap` | +| `HighVSpace p m asid` | `ext (highVSpaceCap p asid)` | none | +| `AsidControl`, `AsidPool p b` | `asidControlCap`, `asidPoolCap p b` | same | +| `PageDirectory`, `Pdpt`, `IoPortControl`, `IoPort`, `TdxControl` | `nullCap`, and `CapWfA` is `False` | x86 caps | + +l4v's `SGISignalCap` and `SMCCap` are the image of no FL4 cap (FL4's AArch64 ABI has neither). + +Losses, deliberate as on x86: `SchedControl { cpu }` is `schedControlCap` (the per-core SchedControl is +`Ext/MultiCore`'s business); `IrqHandler { irq }` is the 13-bit `irq` (`CapWfA`: `irq < 5376`, `fl4IrqCount`); +`SchedContext { size_bits }` is relative to `min_sched_context_bits`; `Reply { grant }` is +`{AllowGrant, AllowWrite}` or `{AllowWrite}`; a mapped cap's 64-bit ASID word is cut to l4v's 16-bit `asid` +(`CapWfA`: below `2^16`). + +**Gen 12 (ruling 15:01Z item 2): pointers are named by `specPtr`** (`P3/PtrA`, the uniform shift of FL4's kernel +window into l4v's): every object pointer of `absCapA` is `specPtr p` (was `p.bv`); badges, guards, virtual addresses +and ASIDs are unchanged. `CapWfA` gains `SpNoOvA` for untyped and frame caps (the region does not wrap at the shifted +name; it held for free under the identity naming via A64 P1's `UtInvA.reg`). + +No named premise. New definitions only. +-/ +import Fl4ProofsA64.P3.PtrA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 + +noncomputable section + +/-- The low `n` bits of `x`, most significant first (l4v `drop (64 - n) (to_bl x)`; x86 `bitsOf`). -/ +def bitsOfA (x : Nat) : Nat → List Bool + | 0 => [] + | n + 1 => x.testBit n :: bitsOfA x n + +/-- The rights set of the flags `write`, `read`, `grant`, `grantReply` (x86 `rightsOf`). -/ +def rightsOfA (w r g gr : Bool) : Fl4SpecA64.CapRights := + {x | match x with + | .allowWrite => w = true + | .allowRead => r = true + | .allowGrant => g = true + | .allowGrantReply => gr = true} + +/-- Reply cap rights (x86 `replyRights`). -/ +def replyRightsA (grant : Bool) : Fl4SpecA64.CapRights := + if grant then {.allowGrant, .allowWrite} else {.allowWrite} + +/-- FL4's `VM_*` rights word (`capability.rs`: 1 kernel only, 2 read only, 3 read write; the same numbers on both +architectures) as l4v VM rights. -/ +def vmRightsOfA (r : U64) : Fl4SpecA64.VMRights := + if r.val = 3 then Fl4SpecA64.vmReadWrite else if r.val = 2 then Fl4SpecA64.vmReadOnly else Fl4SpecA64.vmKernelOnly + +/-- FL4's frame sizes as l4v AARCH64 page sizes (4 KiB, 2 MiB, 1 GiB). -/ +def frameSizeOfA : capability.FrameSize → Fl4SpecA64.VMPageSize + | .Small => .armSmallPage + | .Large => .armLargePage + | .Huge => .armHugePage + +/-- Zombie type `65` is a TCB (`capability::ZOMBIE_TCB`), otherwise the CNode radix. -/ +def zombieBitsA (zt : U64) : Option Nat := if zt.val = 65 then none else some zt.val + +/-- An FL4 ASID word as l4v's 16-bit `asid`. -/ +def asidOfA (w : U64) : Fl4SpecA64.Asid := BitVec.ofNat 16 w.val + +/-- A mapped cap's map data: the ASID and the virtual address (l4v `acap_map_data`). -/ +def mapDataA (m : Bool) (asid va : U64) : Option (Fl4SpecA64.Asid × Fl4SpecA64.VSpaceRef) := + if m then some (asidOfA asid, va.bv) else none + +/-- A root cap's assignment: the ASID (`ExtArchCap` map data). -/ +def asidDataA (m : Bool) (asid : U64) : Option Fl4SpecA64.Asid := if m then some (asidOfA asid) else none + +/-- The abstract cap of an FL4 frame (`Cap::Frame { is_ept, .. }`): the spec's Stage-2 frame cap if `is_ept` +(`Ext/ArchTypes` `stage2FrameCap`, ExtVsA gen 3), else l4v's frame cap (ExtVsA gen 3's `P3/ExtVsTopA` definition, +moved here at S3 gen 18 for `absCapA`). -/ +def absFrameE (isEpt : Bool) (p : Fl4SpecA64.ObjRef) (Rt : Fl4SpecA64.CapRights) (sz : Fl4SpecA64.VMPageSize) + (dev : Bool) (m : Option (Fl4SpecA64.Asid × Fl4SpecA64.VSpaceRef)) : Fl4SpecA64.ArchCap := + if isEpt then .ext (.stage2FrameCap p Rt sz dev m) else .frameCap p Rt sz dev m + +/-- **The abstract cap of a decoded FL4 AArch64 cap** (module docstring's table). Faithful under `CapWfA`. -/ +def absCapA : capability.Cap → Fl4SpecA64.Cap + | .Null => .nullCap + | .Untyped p sb fi dev => .untypedCap dev (specPtr p) sb.val fi.val + | .Endpoint p b s r g gr => .endpointCap (specPtr p) b.bv (rightsOfA s r g gr) + | .Notification p b s r => .notificationCap (specPtr p) b.bv (rightsOfA s r false false) + | .Reply p g => .replyCap (specPtr p) (replyRightsA g) + | .CNode p r gs g => .cnodeCap (specPtr p) r.val (bitsOfA g.val gs.val) + | .Thread p => .threadCap (specPtr p) + | .IrqControl => .irqControlCap + | .IrqHandler i => .irqHandlerCap (BitVec.ofNat 13 i.val) + | .Zombie p zt n => .zombie (specPtr p) (zombieBitsA zt) n.val + | .Domain => .domainCap + | .SchedContext p sb => .schedContextCap (specPtr p) (sb.val - Fl4SpecA64.minSchedContextBits) + | .SchedControl _ => .schedControlCap + | .Frame p sz r dev m ept asid va => + .archObjectCap (absFrameE ept (specPtr p) (vmRightsOfA r) (frameSizeOfA sz) dev (mapDataA m asid va)) + | .PageTable p m asid va => .archObjectCap (.pageTableCap (specPtr p) .normalPtT (mapDataA m asid va)) + | .Pml4 p m asid => .archObjectCap (.pageTableCap (specPtr p) .vsRootPtT (mapDataA m asid 0#u64)) + | .Vcpu p => .archObjectCap (.vcpuCap (specPtr p)) + | .Ept p _ m asid _ => .archObjectCap (.ext (.stage2VSpaceCap (specPtr p) (asidDataA m asid))) + | .HighVSpace p m asid => .archObjectCap (.ext (.highVSpaceCap (specPtr p) (asidDataA m asid))) + | .AsidControl => .archObjectCap .asidControlCap + | .AsidPool p base => .archObjectCap (.asidPoolCap (specPtr p) (asidOfA base)) + | .PageDirectory .. => .nullCap + | .Pdpt .. => .nullCap + | .IoPortControl => .nullCap + | .IoPort .. => .nullCap + | .TdxControl => .nullCap + +/-- (gen 12) **A region's last byte does not wrap under the spec naming** (`specPtr`): l4v's `r ≤ r + (1 << n) - 1` +at `r = specPtr p`. It holds for every region inside FL4's kernel window (`specPtr` maps the window into +`[2^39, 2^47 + 2^39)`); `CapWfA` asks it of untyped and frame caps, so `same_region_as` of such a cap with itself is +l4v's (`InsDisA.srA_shape`). -/ +def SpNoOvA (p : U64) (n : Nat) : Prop := specPtr p ≤ specPtr p + ((1 : BitVec 64) <<< n) - 1 + +/-- **The well-formedness under which `absCapA` is faithful** (x86 `CapWf`, plus the AArch64 cuts): a CNode cap's +radix, guard size and guard fit; an IRQ handler is one of FL4's 5376 IRQs; an SC cap is at least +`min_sched_context_bits`; a frame's rights word is 1..3; a mapped cap's ASID fits l4v's 16 bits; (gen 12) an untyped's or a frame's range +does not wrap under `specPtr` (`SpNoOvA`); (S3 gen 34, ruling F4) an unmapped frame cap has ASID 0 (l4v's +`Option (asid × vaddr)`: FL4 writes `asid: 0` with `mapped: false` on every path, `vspace.rs` 659–660, 782, 889, +`capability.rs` 374–383, and `derive_cap` keeps `(mapped, asid)`; carried beside the 16-bit bound); a mapped frame has a nonzero ASID, and a Stage-2 frame is mapped +(the frame writers use assigned ASIDs or reset mapping metadata together); a Stage-2 root cap is +level 0 (FL4's AArch64 kernel makes no other level); an ASID pool base is aligned to 512 (l4v `valid_cap`; +FL4's boot masks off the low nine bits and MakePool shifts the index left by nine); the x86-only caps never occur. -/ +def CapWfA : capability.Cap → Prop + | .CNode _ r gs g => r.val < 64 ∧ gs.val < 64 ∧ g.val < 2 ^ gs.val + | .IrqHandler i => i.val < Fl4SpecA64.fl4IrqCount + | .Zombie _ zt _ => zt.val ≤ 64 ∨ zt.val = 65 + | .SchedContext _ sb => Fl4SpecA64.minSchedContextBits ≤ sb.val + | .Untyped p sb _ _ => SpNoOvA p sb.val + | .Frame p sz r _ m ept asid _ => 1 ≤ r.val ∧ r.val ≤ 3 ∧ + (asid.val < 2 ^ 16 ∧ (m = false → asid.val = 0) ∧ (m = true → asid ≠ 0#u64) ∧ + (ept = true → m = true)) ∧ SpNoOvA p (Fl4SpecA64.pageBitsForSize (frameSizeOfA sz)) + | .PageTable _ _ asid _ => asid.val < 2 ^ 16 + | .Pml4 _ _ asid => asid.val < 2 ^ 16 + | .Ept _ level _ asid _ => level.val = 0 ∧ asid.val < 2 ^ 16 + | .HighVSpace _ _ asid => asid.val < 2 ^ 16 + | .AsidPool _ base => base.val < 2 ^ 16 ∧ base.val % 512 = 0 + | .PageDirectory .. => False + | .Pdpt .. => False + | .IoPortControl => False + | .IoPort .. => False + | .TdxControl => False + | _ => True + +/-- 48-bit sign-extension (matching `CapRt.unpackF` on 48-bit values): when a Stage-2 `PageTable` + cap stores a 48-bit IPA `va' < 2^48`, `Cap::decode` sign-extends bit 47 into bits `63..48`, + while `ept::unmap_table` / `ept::map_table` only index bits `47..21`. -/ +def unpack48A (va : BitVec 64) : BitVec 64 := + if va &&& 0x800000000000#64 = 0#64 then va else va ||| 0xffff000000000000#64 + +/-- **Normalized capability equality** (S3 gen 40, Architect 09:59Z ruling 1): identical to + `absCapA c = a`, except that a mapped `PageTable` cap may carry `unpack48A va'` when + `va'.toNat < 2 ^ 48` (Stage-2 IPA with bit 47 set). For Stage-1 (`va' < 2^47`) and + HighVSpace (`va' ≥ 2^48`), this forces `va.bv = va'` strictly. -/ +def CapEqA : capability.Cap → Fl4SpecA64.Cap → Prop + | .PageTable p true asid va, .archObjectCap (.pageTableCap p' .normalPtT (some (asid', va'))) => + specPtr p = p' ∧ asidOfA asid = asid' ∧ + (va.bv = va' ∨ (va.bv = unpack48A va' ∧ va'.toNat < 2 ^ 48)) + | c, a => absCapA c = a + +/-- **The capability relation**: FL4 cap `c` represents abstract cap `a` (x86 `CapRel`). -/ +def CapRelA (c : capability.Cap) (a : Fl4SpecA64.Cap) : Prop := CapWfA c ∧ CapEqA c a + +theorem CapEqA.of_abs {c : capability.Cap} {a : Fl4SpecA64.Cap} (h : absCapA c = a) : CapEqA c a := by + cases c with + | PageTable p m asid va => + cases m + · exact h + · subst h + exact ⟨rfl, rfl, Or.inl rfl⟩ + | _ => exact h + +/-- `CapEqA` is `absCapA` equality except at a mapped Stage-2 `PageTable` cap. -/ +theorem CapEqA.eq_or_pt {c : capability.Cap} {a : Fl4SpecA64.Cap} (h : CapEqA c a) : + a = absCapA c ∨ ∃ p' asid' va', a = .archObjectCap (.pageTableCap p' .normalPtT (some (asid', va'))) := by + cases c with + | PageTable p m asid va => + cases m + · exact Or.inl h.symm + · cases a <;> first | exact Or.inl h.symm | skip + case archObjectCap ac => + cases ac <;> first | exact Or.inl h.symm | skip + case pageTableCap p' ty mv => + cases ty <;> first | exact Or.inl h.symm | skip + case normalPtT => + cases mv with + | none => exact Or.inl h.symm + | some pr => exact Or.inr ⟨_, pr.1, pr.2, rfl⟩ + | _ => exact Or.inl h.symm + +theorem CapRelA.of_abs {c : capability.Cap} {a : Fl4SpecA64.Cap} (hw : CapWfA c) (h : absCapA c = a) : + CapRelA c a := ⟨hw, CapEqA.of_abs h⟩ + +theorem CapRelA.null : CapRelA .Null .nullCap := ⟨trivial, rfl⟩ + +/-- On a 48-bit value, masking `unpack48A va'` to 48 bits recovers `va'`. -/ +theorem unpack48A_and_m48 {va' : BitVec 64} (h : va'.toNat < 2 ^ 48) : + unpack48A va' &&& 0xffffffffffff#64 = va' := by + unfold unpack48A + have hm : va' &&& 0xffffffffffff#64 = va' := by + apply BitVec.eq_of_toNat_eq + simp only [BitVec.toNat_and, BitVec.toNat_ofNat] + exact (Nat.and_two_pow_sub_one_eq_mod va'.toNat 48).trans (Nat.mod_eq_of_lt h) + split_ifs + · exact hm + · apply BitVec.eq_of_toNat_eq + simp only [BitVec.toNat_and, BitVec.toNat_or, BitVec.toNat_ofNat] + apply Nat.eq_of_testBit_eq + intro i + simp only [Nat.testBit_and, Nat.testBit_or] + have hhi : (0xffff000000000000 : Nat) = (2 ^ 16 - 1) * 2 ^ 48 := by norm_num + have hlo : (0xffffffffffff : Nat) = 2 ^ 48 - 1 := by norm_num + change ((va'.toNat.testBit i || (0xffff000000000000 : Nat).testBit i) && (0xffffffffffff : Nat).testBit i) = + va'.toNat.testBit i + rw [hhi, hlo, Nat.testBit_two_pow_sub_one] + by_cases hi : i < 48 + · have h0 : ((2 ^ 16 - 1) * 2 ^ 48).testBit i = false := by + rw [Nat.testBit_mul_two_pow]; simp [Nat.not_le.mpr hi] + rw [h0, Bool.or_false, decide_eq_true hi, Bool.and_true] + · have h0 : va'.toNat.testBit i = false := + Nat.testBit_lt_two_pow (Nat.lt_of_lt_of_le h (Nat.pow_le_pow_right (by norm_num) (Nat.le_of_not_lt hi))) + rw [h0, decide_eq_false hi, Bool.and_false] + +/-- For any mapped `PageTable` related by `CapEqA` whose spec address is `< 2^47` (Stage-1), + the concrete and abstract virtual addresses are equal. -/ +theorem CapEqA.pt_va_eq_of_lt47 {p asid va : U64} {p' : Fl4SpecA64.ObjRef} {asid' : Fl4SpecA64.Asid} + {va' : Fl4SpecA64.VSpaceRef} + (h : CapEqA (.PageTable p true asid va) (.archObjectCap (.pageTableCap p' .normalPtT (some (asid', va'))))) + (hlt : va'.toNat < 2 ^ 47) : va.bv = va' := by + rcases h.2.2 with hv | ⟨hv, _⟩ + · exact hv + · have hbit : va' &&& 0x800000000000#64 = 0#64 := by + apply BitVec.eq_of_toNat_eq + simp only [BitVec.toNat_and, BitVec.toNat_ofNat] + have h47 : (0x800000000000 : Nat) = 2 ^ 47 := by norm_num + apply Nat.eq_of_testBit_eq + intro i + change (va'.toNat &&& (0x800000000000 : Nat)).testBit i = (0 : Nat).testBit i + rw [h47, Nat.testBit_and, Nat.zero_testBit] + by_cases hi : i = 47 + · subst hi; rw [Nat.testBit_lt_two_pow hlt, Bool.false_and] + · rw [Nat.testBit_two_pow, decide_eq_false (Ne.symm hi), Bool.and_false] + rw [hv, unpack48A, if_pos hbit] + +/-- For any mapped `PageTable` related by `CapEqA` whose spec address is `≥ 2^48` (HighVSpace), + the concrete and abstract virtual addresses are equal. -/ +theorem CapEqA.pt_va_eq_of_ge48 {p asid va : U64} {p' : Fl4SpecA64.ObjRef} {asid' : Fl4SpecA64.Asid} + {va' : Fl4SpecA64.VSpaceRef} + (h : CapEqA (.PageTable p true asid va) (.archObjectCap (.pageTableCap p' .normalPtT (some (asid', va'))))) + (hge : 2 ^ 48 ≤ va'.toNat) : va.bv = va' := by + rcases h.2.2 with hv | ⟨_, hlt⟩ + · exact hv + · omega + +/-- For any mapped `PageTable` related by `CapEqA` whose spec address is `< 2^48` (Stage-2), + masking `va.bv` to 48 bits recovers `va'`. -/ +theorem CapEqA.pt_va_mask48 {p asid va : U64} {p' : Fl4SpecA64.ObjRef} {asid' : Fl4SpecA64.Asid} + {va' : Fl4SpecA64.VSpaceRef} + (h : CapEqA (.PageTable p true asid va) (.archObjectCap (.pageTableCap p' .normalPtT (some (asid', va'))))) + (hlt : va'.toNat < 2 ^ 48) : va.bv &&& 0xffffffffffff#64 = va' := by + rcases h.2.2 with hv | ⟨hv, _⟩ + · rw [hv] + apply BitVec.eq_of_toNat_eq + simp only [BitVec.toNat_and, BitVec.toNat_ofNat] + exact (Nat.and_two_pow_sub_one_eq_mod va'.toNat 48).trans (Nat.mod_eq_of_lt hlt) + · rw [hv]; exact unpack48A_and_m48 hlt + +/-- `CapEqA` on a mapped `PageTable` always implies `a` is an `archObjectCap (.pageTableCap … .normalPtT (some …))`. -/ +theorem CapEqA.pt_mapped_iff {p asid va : U64} {a : Fl4SpecA64.Cap} : + CapEqA (.PageTable p true asid va) a ↔ + ∃ asid' va', a = .archObjectCap (.pageTableCap (specPtr p) .normalPtT (some (asid', va'))) ∧ + asidOfA asid = asid' ∧ (va.bv = va' ∨ (va.bv = unpack48A va' ∧ va'.toNat < 2 ^ 48)) := by + cases a with + | archObjectCap ac => + cases ac with + | pageTableCap p' ty md => + cases ty with + | normalPtT => + cases md with + | none => exact ⟨fun h => by simp [CapEqA, absCapA, mapDataA] at h, fun ⟨_, _, h, _⟩ => by cases h⟩ + | some av => + rcases av with ⟨asid', va'⟩ + constructor + · rintro ⟨rfl, ha, hv⟩ + exact ⟨asid', va', rfl, ha, hv⟩ + · rintro ⟨_, _, h, ha, hv⟩ + cases h + exact ⟨rfl, ha, hv⟩ + | vsRootPtT => exact ⟨fun h => by simp [CapEqA, absCapA, mapDataA] at h, fun ⟨_, _, h, _⟩ => by cases h⟩ + | _ => exact ⟨fun h => by simp [CapEqA, absCapA, mapDataA] at h, fun ⟨_, _, h, _⟩ => by cases h⟩ + | _ => exact ⟨fun h => by simp [CapEqA, absCapA, mapDataA] at h, fun ⟨_, _, h, _⟩ => by cases h⟩ + +/-- For any cap that is not a mapped `PageTable`, `CapEqA c a` is `absCapA c = a`. -/ +theorem CapEqA.eq_abs {c : capability.Cap} {a : Fl4SpecA64.Cap} + (h : CapEqA c a) (hne : ∀ p asid va, c ≠ .PageTable p true asid va) : absCapA c = a := by + cases c with + | PageTable p m asid va => + cases m + · exact h + · exact absurd rfl (hne p asid va) + | _ => exact h + +/-- `CapEqA` preserves `isCnodeCap`. -/ +theorem CapEqA.isCnode_eq {c : capability.Cap} {a : Fl4SpecA64.Cap} (h : CapEqA c a) : + Fl4SpecA64.isCnodeCap a = Fl4SpecA64.isCnodeCap (absCapA c) := by + cases c with + | PageTable p m asid va => + cases m + · rw [← h] + · obtain ⟨_, _, rfl, _, _⟩ := CapEqA.pt_mapped_iff.mp h + rfl + | _ => rw [← h] + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/CarryA.lean b/proofs/Fl4ProofsA64/P3/CarryA.lean new file mode 100644 index 000000000..2ee3623e8 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/CarryA.lean @@ -0,0 +1,559 @@ +/- +P3 (aarch64, A64-P3 S3 gen 29, ruling 00:45Z items 3–4): **the carried invariants across the unmap tops** — +`CarryA Z mem σ` = gen 28's `PtCapInA Z mem` (`P3/FinKeepA`) ∧ gen 27's `FinInvA Z.th mem σ Z.G` (`P3/FinDefsA`), both +standalone (ruling: not bundled into `JAtZA` / `JEntA` before the S3 + IPC + MCS join). The x86 counterparts are +`P3/FinKeep`'s `FinM.of_same` / `KTyped.of_types` at the VSpace tops and `EptCore.capTbl`'s keepers. + +* **The spec side, `TyKeepA σ σ'`**: every CNode of `σ'` was a CNode of the same radix in `σ`, every TCB a TCB (what + `KTypedA.of_typesA` reads). **`OutTyA m`**: every outcome of `m` is `TyKeepA`-related to its start; closed under the + monad's binds (both monads, `catch`, `when`, `mapMx`) and true of the read-only primitives (the `OutKmA` library of + `P3/FinVsJA`, for this relation). The operations: `setObject` of a non-CNode / non-TCB object (`setObject_tyA`), + `store_pte`, `do_machine_op`, the TLB flushes, `find_vspace_for_asid`, the table walk, l4v's `unmap_page` / + `unmap_page_table`, `get_cap`, **`set_cap`** (a CNode stays a CNode of its radix, a TCB a TCB), the Ext slot store + and unmaps (`extUnmapPage` / `extUnmapTable`), and the performs of the unmap invocations: **`perfPgUnmap_tyA`** + (`perform_page_invocation (PageUnmap …)`), **`perfPtUnmap_tyA`** (`perform_page_table_invocation + (PageTableUnmap …)`), **`perfExtPgUnmap_tyA`** / **`perfExtPtUnmap_tyA`** (`performExtVSpace` `.pageUnmap` / + `.tableUnmap`). **`TyKeepA.stUpdA`**: the restart. +* **The FL4 side**: **`setcap_frA`** (after `set_cap` of an encodable cap at an `OS` slot, the slot holds the cap and + every other `OS` slot its old one). +* **`CarryA.of_setcapA`**: `CarryA` is kept by a step that keeps the `OS` slots (`OSKeep`) and then writes the invoked + slot (`set_cap`) with a cap that is no mapped page-table cap and `CapFinEqA` to the old one, the spec side + `TyKeepA`-related (`PtCapInA.of_capsA`, `FinInvA.of_keepA`; the chains' slots are in `OS` by the tree's `MdbLk`). + +Gen 30 (ruling 01:13Z item 1): the spec side of the map tops — **`threadSet_tyA`**, **`setThreadState_tyA`**, +**`restart_tyA`**, **`perfPt_tyA`** / **`perfPg_tyA`** (every `perform_page_table_invocation` / +`perform_page_invocation`); **`CarryA.of_slotFrA`** (`CarryA` across a map's slot frame `SlotFrA`, at new ghosts +whose Stage-1 tables at depth ≥ 1 and Stage-2 tables include the old ones); `CapFinEqA.frame2A` / `.pt2A`. + +No named premise. No existing statement changed. +-/ +import Fl4ProofsA64.P3.FinKeepA +import Fl4ProofsA64.P3.SetCapJA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## The spec side -/ + +section spec +open Fl4Spec + +/-- **The CNodes and TCBs of `σ'` were CNodes (same radix) and TCBs of `σ`** (module docstring). -/ +def TyKeepA (σ σ' : Fl4SpecA64.State Unit) : Prop := + (∀ o r cs, σ'.kheap o = some (.cnode r cs) → ∃ cs0, σ.kheap o = some (.cnode r cs0)) ∧ + ∀ o t, σ'.kheap o = some (.tcb t) → ∃ t0, σ.kheap o = some (.tcb t0) + +theorem TyKeepA.refl (σ : Fl4SpecA64.State Unit) : TyKeepA σ σ := + ⟨fun _ _ cs h => ⟨cs, h⟩, fun _ t h => ⟨t, h⟩⟩ + +theorem TyKeepA.trans {σ1 σ2 σ3 : Fl4SpecA64.State Unit} (h1 : TyKeepA σ1 σ2) (h2 : TyKeepA σ2 σ3) : + TyKeepA σ1 σ3 := + ⟨fun o r cs h => let ⟨c1, e⟩ := h2.1 o r cs h; h1.1 o r c1 e, + fun o t h => let ⟨t1, e⟩ := h2.2 o t h; h1.2 o t1 e⟩ + +theorem TyKeepA.of_kheap {σ σ' : Fl4SpecA64.State Unit} (h : σ'.kheap = σ.kheap) : TyKeepA σ σ' := by + refine ⟨fun o r cs e => ⟨cs, ?_⟩, fun o t e => ⟨t, ?_⟩⟩ <;> rw [← h] <;> exact e + +/-- A heap update at `p` by an object of the old one's kind. -/ +theorem TyKeepA.upd {σ : Fl4SpecA64.State Unit} {p : Fl4SpecA64.ObjRef} {o o' : Fl4SpecA64.KernelObject} + (h : σ.kheap p = some o) (hc : ∀ r cs, o' = .cnode r cs → ∃ cs0, o = .cnode r cs0) + (ht : ∀ t, o' = .tcb t → ∃ t0, o = .tcb t0) : + TyKeepA σ { σ with kheap := Function.update σ.kheap p (some o') } := by + refine ⟨fun q r cs hq => ?_, fun q t hq => ?_⟩ + · simp only [Function.update_apply] at hq + by_cases e : q = p + · rw [if_pos e] at hq + obtain ⟨cs0, rfl⟩ := hc r cs (Option.some.inj hq) + exact ⟨cs0, e ▸ h⟩ + · rw [if_neg e] at hq; exact ⟨cs, hq⟩ + · simp only [Function.update_apply] at hq + by_cases e : q = p + · rw [if_pos e] at hq + obtain ⟨t0, rfl⟩ := ht t (Option.some.inj hq) + exact ⟨t0, e ▸ h⟩ + · rw [if_neg e] at hq; exact ⟨t, hq⟩ + +/-- **The restart keeps the kinds** (the TCB stays a TCB). -/ +theorem TyKeepA.stUpdA {σ : Fl4SpecA64.State Unit} {t : Fl4SpecA64.ObjRef} {x : Fl4SpecA64.Tcb} + (hk : σ.kheap t = some (.tcb x)) (ts : Fl4SpecA64.ThreadState) : TyKeepA σ (stUpdA σ t x ts) := + TyKeepA.upd hk (fun _ _ e => by cases e) fun _ _ => ⟨x, rfl⟩ + +/-- **Every outcome is `TyKeepA`-related to its start** (module docstring). -/ +def OutTyA {α : Type} (m : NonDet (Fl4SpecA64.State Unit) α) : Prop := + ∀ σ r, r ∈ (m σ).1 → TyKeepA σ r.2 + +section out +variable {α β ε : Type} + +theorem OutTyA.bind {m : NonDet (Fl4SpecA64.State Unit) α} {f : α → NonDet (Fl4SpecA64.State Unit) β} + (hm : OutTyA m) (hf : ∀ a, OutTyA (f a)) : OutTyA (m >>= f) := fun σ r hr => by + obtain ⟨x, hx, hr⟩ := hr + exact (hm σ x hx).trans (hf x.1 x.2 r hr) + +theorem OutTyA.one {m : NonDet (Fl4SpecA64.State Unit) α} (h : ∀ σ, ∃ v, m σ = ({(v, σ)}, False)) : + OutTyA m := fun σ r hr => by + obtain ⟨v, e⟩ := h σ + rw [e] at hr + obtain rfl : r = (v, σ) := hr + exact TyKeepA.refl σ + +theorem OutTyA.pure (a : α) : OutTyA (Pure.pure a : NonDet (Fl4SpecA64.State Unit) α) := OutTyA.one fun _ => ⟨a, rfl⟩ + +theorem OutTyA.gets (g : Fl4SpecA64.State Unit → α) : OutTyA (NonDet.gets g) := OutTyA.one fun _ => ⟨_, rfl⟩ + +theorem OutTyA.assertOpt (o : Option α) : OutTyA (NonDet.assertOpt o : NonDet (Fl4SpecA64.State Unit) α) := by + cases o with + | none => exact fun _ _ h => absurd h (Set.notMem_empty _) + | some a => exact OutTyA.one fun _ => ⟨a, rfl⟩ + +theorem OutTyA.getsThe (g : Fl4SpecA64.State Unit → Option α) : OutTyA (NonDet.getsThe g) := fun σ r hr => + OutTyA.assertOpt (g σ) σ r hr + +theorem OutTyA.getsMap {γ : Type} (g : Fl4SpecA64.State Unit → γ → Option α) (p : γ) : + OutTyA (NonDet.getsMap g p) := + OutTyA.bind (OutTyA.gets g) fun x => OutTyA.assertOpt (x p) + +theorem OutTyA.assert (P : Prop) : OutTyA (NonDet.assert P : NonDet (Fl4SpecA64.State Unit) Unit) := fun σ r hr => by + obtain ⟨-, rfl⟩ := hr + exact TyKeepA.refl σ + +theorem OutTyA.selectF (S : Set α × Prop) : OutTyA (NonDet.selectF S : NonDet (Fl4SpecA64.State Unit) α) := + fun σ r hr => by rw [hr.2]; exact TyKeepA.refl σ + +theorem OutTyA.modify {g : Fl4SpecA64.State Unit → Fl4SpecA64.State Unit} (hg : ∀ σ, TyKeepA σ (g σ)) : + OutTyA (NonDet.modify g) := fun σ r hr => by + obtain rfl : r = ((), g σ) := hr + exact hg σ + +theorem OutTyA.when {P : Prop} [Decidable P] {m : NonDet (Fl4SpecA64.State Unit) Unit} (hm : OutTyA m) : + OutTyA (NonDet.when P m) := by + unfold NonDet.when + split + · exact hm + · exact OutTyA.one fun _ => ⟨(), rfl⟩ + +theorem OutTyA.mapMx {f : α → NonDet (Fl4SpecA64.State Unit) β} (hf : ∀ a, OutTyA (f a)) : + ∀ l : List α, OutTyA (NonDet.mapMx f l) + | [] => OutTyA.one fun _ => ⟨(), rfl⟩ + | x :: xs => OutTyA.bind (hf x) fun _ => OutTyA.mapMx hf xs + +/-! The exception monad, through `ExceptT.run`. -/ + +theorem OutTyA.ebind {m : NonDetE (Fl4SpecA64.State Unit) ε α} {f : α → NonDetE (Fl4SpecA64.State Unit) ε β} + (hm : OutTyA m.run) (hf : ∀ a, OutTyA (f a).run) : OutTyA (m >>= f).run := fun σ r hr => by + obtain ⟨⟨e | a, σ1⟩, hx, hr⟩ := hr + · obtain rfl : r = (.error e, σ1) := hr + exact hm σ _ hx + · exact (hm σ _ hx).trans (hf a σ1 r hr) + +theorem OutTyA.liftE {m : NonDet (Fl4SpecA64.State Unit) α} (hm : OutTyA m) : + OutTyA (NonDetE.liftE (ε := ε) m).run := fun σ r hr => by + obtain ⟨x, hx, hr⟩ := hr + obtain rfl : r = (.ok x.1, x.2) := hr + exact hm σ x hx + +theorem OutTyA.retOk (a : α) : OutTyA (NonDetE.returnOk a : NonDetE (Fl4SpecA64.State Unit) ε α).run := + OutTyA.one fun _ => ⟨_, rfl⟩ + +theorem OutTyA.throw (e : ε) : OutTyA (NonDetE.throwError e : NonDetE (Fl4SpecA64.State Unit) ε α).run := + OutTyA.one fun _ => ⟨_, rfl⟩ + +theorem OutTyA.unlessE {P : Prop} [Decidable P] {m : NonDetE (Fl4SpecA64.State Unit) ε Unit} (hm : OutTyA m.run) : + OutTyA (NonDetE.unlessE P m).run := by + unfold NonDetE.unlessE + split + · exact OutTyA.one fun _ => ⟨_, rfl⟩ + · exact hm + +theorem OutTyA.throwOpt (e : ε) (o : Option α) : + OutTyA (NonDetE.throwOpt e o : NonDetE (Fl4SpecA64.State Unit) ε α).run := by + cases o with + | none => exact OutTyA.one fun _ => ⟨_, rfl⟩ + | some a => exact OutTyA.one fun _ => ⟨_, rfl⟩ + +theorem OutTyA.catch {m : NonDetE (Fl4SpecA64.State Unit) ε α} {h : ε → NonDet (Fl4SpecA64.State Unit) α} + (hm : OutTyA m.run) (hh : ∀ e, OutTyA (h e)) : OutTyA (NonDetE.catch m h) := fun σ r hr => by + obtain ⟨⟨e | a, σ1⟩, hx, hr⟩ := hr + · exact (hm σ _ hx).trans (hh e σ1 r hr) + · obtain rfl : r = (a, σ1) := hr + exact hm σ _ hx + +end out + +/-! The spec operations. -/ + +theorem setObject_tyA (p : Fl4SpecA64.ObjRef) {o : Fl4SpecA64.KernelObject} (hc : ∀ r cs, o ≠ .cnode r cs) + (ht : ∀ t, o ≠ .tcb t) : OutTyA (Fl4SpecA64.setObject (Z := Unit) p o) := fun σ r hr => by + obtain ⟨o0, h0, e⟩ := setObject_mem hr + rw [e] + exact TyKeepA.upd h0 (fun r cs e => (hc r cs e).elim) fun t e => (ht t e).elim + +theorem storePte_tyA (ptT : Fl4SpecA64.PtType) (p : Fl4SpecA64.ObjRef) (pte : Fl4SpecA64.PTE) : + OutTyA (Fl4SpecA64.storePte (Z := Unit) ptT p pte) := by + unfold Fl4SpecA64.storePte Fl4SpecA64.setPt + exact OutTyA.bind (OutTyA.assert _) fun _ => OutTyA.bind (OutTyA.pure _) fun _ => + OutTyA.bind (OutTyA.getsMap _ _) fun _ => OutTyA.bind (OutTyA.assert _) fun _ => + OutTyA.bind (OutTyA.getsMap _ _) fun _ => setObject_tyA _ (fun _ _ h => by cases h) fun _ h => by cases h + +theorem doMachineOp_tyA {α : Type} (mop : NonDet Fl4SpecA64.MachineState α) : + OutTyA (Fl4SpecA64.doMachineOp (Z := Unit) mop) := by + unfold Fl4SpecA64.doMachineOp + exact OutTyA.bind (OutTyA.gets _) fun _ => OutTyA.bind (OutTyA.selectF _) fun ⟨_, ms'⟩ => + OutTyA.bind (OutTyA.modify fun σ => TyKeepA.of_kheap rfl) fun _ => OutTyA.pure _ + +theorem invTlbVa_tyA (asid : Fl4SpecA64.Asid) (v : Fl4SpecA64.VSpaceRef) : + OutTyA (Fl4SpecA64.invalidateTlbByAsidVa (Z := Unit) asid v) := by + unfold Fl4SpecA64.invalidateTlbByAsidVa Fl4SpecA64.loadVmid + refine OutTyA.bind (OutTyA.bind (OutTyA.gets _) fun _ => OutTyA.pure _) fun o => ?_ + cases o with + | none => exact OutTyA.pure _ + | some _ => exact doMachineOp_tyA _ + +theorem invTlb_tyA (asid : Fl4SpecA64.Asid) : OutTyA (Fl4SpecA64.invalidateTlbByAsid (Z := Unit) asid) := by + unfold Fl4SpecA64.invalidateTlbByAsid Fl4SpecA64.loadVmid + refine OutTyA.bind (OutTyA.bind (OutTyA.gets _) fun _ => OutTyA.pure _) fun o => ?_ + cases o with + | none => exact OutTyA.pure _ + | some _ => exact doMachineOp_tyA _ + +theorem findVs_tyA (asid : Fl4SpecA64.Asid) : OutTyA (Fl4SpecA64.findVspaceForAsid (Z := Unit) asid).run := by + unfold Fl4SpecA64.findVspaceForAsid + exact OutTyA.ebind (OutTyA.liftE (OutTyA.gets _)) fun _ => OutTyA.ebind (OutTyA.liftE (OutTyA.gets _)) fun o => + OutTyA.throwOpt _ o + +theorem ptLookupN_tyA : ∀ (n : Nat) (level : Fl4SpecA64.VmLevel) (pt : Fl4SpecA64.ObjRef) + (v : Fl4SpecA64.VSpaceRef) (t : Fl4SpecA64.ObjRef), + OutTyA (Fl4SpecA64.ptLookupFromLevelN (Z := Unit) n level pt v t).run + | 0, level, pt, v, t => by + rw [Fl4SpecA64.ptLookupFromLevelN] + exact OutTyA.ebind (OutTyA.unlessE (OutTyA.throw _)) fun _ => OutTyA.throw _ + | n + 1, level, pt, v, t => by + rw [Fl4SpecA64.ptLookupFromLevelN] + refine OutTyA.ebind (OutTyA.unlessE (OutTyA.throw _)) fun _ => ?_ + refine OutTyA.ebind (OutTyA.retOk _) fun _ => ?_ + refine OutTyA.ebind (OutTyA.liftE (OutTyA.getsThe _)) fun _ => ?_ + refine OutTyA.ebind (OutTyA.unlessE (OutTyA.throw _)) fun _ => ?_ + refine OutTyA.ebind (OutTyA.retOk _) fun ptr => ?_ + by_cases hp : ptr = t + · rw [if_pos hp]; exact OutTyA.retOk _ + · rw [if_neg hp]; exact ptLookupN_tyA n _ _ _ _ + +theorem unmapPage_tyA (sz : Fl4SpecA64.VMPageSize) (asid : Fl4SpecA64.Asid) (v : Fl4SpecA64.VSpaceRef) + (p : Fl4SpecA64.ObjRef) : OutTyA (Fl4SpecA64.unmapPage (Z := Unit) sz asid v p) := by + unfold Fl4SpecA64.unmapPage + refine OutTyA.catch ?_ fun _ => OutTyA.pure _ + refine OutTyA.ebind (findVs_tyA _) fun _ => ?_ + refine OutTyA.ebind (OutTyA.liftE (OutTyA.getsThe _)) fun x => ?_ + obtain ⟨lev, slot⟩ := x + refine OutTyA.ebind (OutTyA.unlessE (OutTyA.throw _)) fun _ => ?_ + refine OutTyA.ebind (OutTyA.liftE (OutTyA.getsMap _ _)) fun _ => ?_ + refine OutTyA.ebind (OutTyA.unlessE (OutTyA.throw _)) fun _ => ?_ + refine OutTyA.ebind (OutTyA.liftE (storePte_tyA _ _ _)) fun _ => ?_ + refine OutTyA.ebind (OutTyA.liftE (doMachineOp_tyA _)) fun _ => ?_ + exact OutTyA.liftE (invTlbVa_tyA _ _) + +theorem unmapPageTable_tyA (asid : Fl4SpecA64.Asid) (v : Fl4SpecA64.VSpaceRef) (pt : Fl4SpecA64.ObjRef) : + OutTyA (Fl4SpecA64.unmapPageTable (Z := Unit) asid v pt) := by + unfold Fl4SpecA64.unmapPageTable Fl4SpecA64.ptLookupFromLevel + refine OutTyA.catch ?_ fun _ => OutTyA.pure _ + refine OutTyA.ebind (findVs_tyA _) fun _ => ?_ + refine OutTyA.ebind (ptLookupN_tyA _ _ _ _ _) fun x => ?_ + obtain ⟨slot, level⟩ := x + refine OutTyA.ebind (OutTyA.liftE (storePte_tyA _ _ _)) fun _ => ?_ + refine OutTyA.ebind (OutTyA.liftE (doMachineOp_tyA _)) fun _ => ?_ + exact OutTyA.liftE (invTlb_tyA _) + +theorem getCap_tyA (sl : Fl4SpecA64.CslotPtr) : OutTyA (Fl4SpecA64.getCap (Z := Unit) sl) := fun σ r hr => by + rw [(getCap_memA hr).1]; exact TyKeepA.refl σ + +/-- **`set_cap` keeps the kinds**: a CNode is replaced by a CNode of its radix, a TCB by a TCB. -/ +theorem setCap_tyA (cap : Fl4SpecA64.Cap) (sl : Fl4SpecA64.CslotPtr) : + OutTyA (Fl4SpecA64.setCap (Z := Unit) cap sl) := fun σ r hr => by + obtain ⟨p, cref⟩ := sl + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + obtain ⟨e1, e2⟩ := getObject_mem hx + obtain ⟨obj, σ1⟩ := x + simp only at e1 e2 hr + subst e1 + have key : ∀ o, (∀ r cs, o = .cnode r cs → ∃ cs0, obj = .cnode r cs0) → (∀ t, o = .tcb t → ∃ t0, obj = .tcb t0) → + r ∈ ((Pure.pure o >>= fun o' => Fl4SpecA64.setObject (Z := Unit) p o') σ1).1 → TyKeepA σ1 r.2 := by + intro o hc ht h + obtain ⟨y, hy, h⟩ := mem_bindA.1 h + obtain rfl : y = (o, σ1) := hy + obtain ⟨o0, -, hr2⟩ := setObject_mem h + rw [hr2] + exact TyKeepA.upd e2 hc ht + have kfail : ∀ f : Fl4SpecA64.KernelObject → NonDet (Fl4SpecA64.State Unit) Unit, + r ∈ ((NonDet.fail >>= f) σ1).1 → False := by + intro f h + obtain ⟨y, hy, -⟩ := mem_bindA.1 h + exact absurd hy (Set.notMem_empty _) + cases obj with + | cnode sz cn => + simp only at hr + split_ifs at hr + · exact key _ (fun r cs e => by cases e; exact ⟨cn, rfl⟩) (fun t e => by cases e) hr + · exact (kfail _ hr).elim + | tcb t => + simp only at hr + split_ifs at hr <;> + first + | exact (kfail _ hr).elim + | exact key _ (fun r cs e => by cases e) (fun _ _ => ⟨t, rfl⟩) hr + | _ => + try simp only at hr + exact (kfail _ hr).elim + +/-! The Ext operations (`Ext/Stage2`). -/ + +theorem storeExtSlot_tyA (sl : Fl4SpecA64.ExtSlot) (pte : Fl4SpecA64.PTE) : + OutTyA (Fl4SpecA64.storeExtSlot (Z := Unit) sl pte) := by + cases sl with + | root r i => + unfold Fl4SpecA64.storeExtSlot + exact OutTyA.bind (OutTyA.getsThe _) fun _ => setObject_tyA _ (fun _ _ h => by cases h) fun _ h => by cases h + | pt p => exact storePte_tyA _ _ _ + +theorem findExtRoot_tyA (k : Fl4SpecA64.ExtRootKind) (asid : Fl4SpecA64.Asid) : + OutTyA (Fl4SpecA64.findExtRoot (Z := Unit) k asid).run := by + unfold Fl4SpecA64.findExtRoot Fl4SpecA64.findAnyRootForAsid + refine OutTyA.ebind (OutTyA.ebind (OutTyA.liftE (OutTyA.gets _)) fun o => OutTyA.throwOpt _ o) fun _ => ?_ + exact OutTyA.ebind (OutTyA.liftE (OutTyA.gets _)) fun _ => + OutTyA.ebind (OutTyA.unlessE (OutTyA.throw _)) fun _ => OutTyA.retOk _ + +theorem extUnmapPage_tyA (k : Fl4SpecA64.ExtRootKind) (sz : Fl4SpecA64.VMPageSize) (asid : Fl4SpecA64.Asid) + (v : Fl4SpecA64.VSpaceRef) (p : Fl4SpecA64.ObjRef) : + OutTyA (Fl4SpecA64.extUnmapPage (Z := Unit) k sz asid v p) := by + unfold Fl4SpecA64.extUnmapPage + refine OutTyA.catch ?_ fun _ => OutTyA.pure _ + refine OutTyA.ebind (OutTyA.unlessE (OutTyA.throw _)) fun _ => ?_ + refine OutTyA.ebind (findExtRoot_tyA _ _) fun _ => ?_ + refine OutTyA.ebind (OutTyA.liftE (OutTyA.gets _)) fun _ => ?_ + refine OutTyA.ebind (OutTyA.unlessE (OutTyA.throw _)) fun _ => ?_ + refine OutTyA.ebind (OutTyA.liftE (OutTyA.gets _)) fun _ => ?_ + refine OutTyA.ebind (OutTyA.unlessE (OutTyA.throw _)) fun _ => ?_ + exact OutTyA.liftE (storeExtSlot_tyA _ _) + +theorem extUnmapTable_tyA (k : Fl4SpecA64.ExtRootKind) (asid : Fl4SpecA64.Asid) (v : Fl4SpecA64.VSpaceRef) + (p : Fl4SpecA64.ObjRef) : OutTyA (Fl4SpecA64.extUnmapTable (Z := Unit) k asid v p) := by + unfold Fl4SpecA64.extUnmapTable + refine OutTyA.catch ?_ fun _ => OutTyA.pure _ + refine OutTyA.ebind (findExtRoot_tyA _ _) fun _ => ?_ + refine OutTyA.ebind (OutTyA.liftE (OutTyA.gets _)) fun sl => ?_ + cases sl with + | some sl => exact OutTyA.liftE (storeExtSlot_tyA _ _) + | none => exact OutTyA.retOk _ + +/-! The performs of the unmap invocations. -/ + +/-- **l4v's `perform_page_invocation (PageUnmap …)` keeps the kinds.** -/ +theorem perfPgUnmap_tyA (cap : Fl4SpecA64.ArchCap) (cte : Fl4SpecA64.CslotPtr) : + OutTyA (Fl4SpecA64.performPageInvocation (Z := Unit) (.pageUnmap cap cte)) := by + unfold Fl4SpecA64.performPageInvocation Fl4SpecA64.performPgInvUnmap + refine OutTyA.bind ?_ fun _ => OutTyA.pure _ + refine OutTyA.bind (OutTyA.assert _) fun _ => ?_ + split + · exact OutTyA.bind (unmapPage_tyA _ _ _ _) fun _ => OutTyA.bind (getCap_tyA _) fun _ => setCap_tyA _ _ + · exact OutTyA.bind (getCap_tyA _) fun _ => setCap_tyA _ _ + +/-- **l4v's `perform_page_table_invocation (PageTableUnmap …)` keeps the kinds.** -/ +theorem perfPtUnmap_tyA (cap : Fl4SpecA64.ArchCap) (cte : Fl4SpecA64.CslotPtr) : + OutTyA (Fl4SpecA64.performPageTableInvocation (Z := Unit) (.pageTableUnmap cap cte)) := by + unfold Fl4SpecA64.performPageTableInvocation Fl4SpecA64.performPtInvUnmap + refine OutTyA.bind (OutTyA.assert _) fun _ => ?_ + split + · refine OutTyA.bind (OutTyA.pure _) fun _ => OutTyA.bind (unmapPageTable_tyA _ _ _) fun _ => + OutTyA.bind (OutTyA.pure _) fun _ => OutTyA.bind (OutTyA.mapMx (fun _ => storePte_tyA _ _ _) _) fun _ => + OutTyA.bind (doMachineOp_tyA _) fun _ => ?_ + exact OutTyA.bind (OutTyA.bind (getCap_tyA _) fun _ => OutTyA.pure _) fun _ => setCap_tyA _ _ + · exact OutTyA.bind (OutTyA.bind (getCap_tyA _) fun _ => OutTyA.pure _) fun _ => setCap_tyA _ _ + +/-- **`performExtVSpace (.pageUnmap …)` keeps the kinds.** -/ +theorem perfExtPgUnmap_tyA (k : Fl4SpecA64.ExtRootKind) (cap : Fl4SpecA64.ArchCap) (cte : Fl4SpecA64.CslotPtr) : + OutTyA (Fl4SpecA64.performExtVSpace (Z := Unit) (.pageUnmap k cap cte)) := by + unfold Fl4SpecA64.performExtVSpace + dsimp only + have tl : ∀ c sl, OutTyA (Fl4SpecA64.setCap (Z := Unit) c sl >>= fun _ => (Pure.pure [] : NonDet _ (List Fl4SpecA64.Data))) := + fun _ _ => OutTyA.bind (setCap_tyA _ _) fun _ => OutTyA.pure _ + split <;> first + | exact OutTyA.bind (extUnmapPage_tyA _ _ _ _ _) fun _ => tl _ _ + | exact OutTyA.bind (OutTyA.when (extUnmapPage_tyA _ _ _ _ _)) fun _ => tl _ _ + | exact OutTyA.bind (OutTyA.pure _) fun _ => tl _ _ + | exact tl _ _ + +/-- **`performExtVSpace (.tableUnmap …)` keeps the kinds.** -/ +theorem perfExtPtUnmap_tyA (k : Fl4SpecA64.ExtRootKind) (cap : Fl4SpecA64.ArchCap) (cte : Fl4SpecA64.CslotPtr) : + OutTyA (Fl4SpecA64.performExtVSpace (Z := Unit) (.tableUnmap k cap cte)) := by + unfold Fl4SpecA64.performExtVSpace + dsimp only + have tl : ∀ c sl, OutTyA (Fl4SpecA64.setCap (Z := Unit) c sl >>= fun _ => (Pure.pure [] : NonDet _ (List Fl4SpecA64.Data))) := + fun _ _ => OutTyA.bind (setCap_tyA _ _) fun _ => OutTyA.pure _ + split <;> first + | exact OutTyA.bind (extUnmapTable_tyA _ _ _ _) fun _ => + OutTyA.bind (OutTyA.mapMx (fun _ => storePte_tyA _ _ _) _) fun _ => tl _ _ + | exact OutTyA.bind (OutTyA.bind (extUnmapTable_tyA _ _ _ _) fun _ => + OutTyA.mapMx (fun _ => storePte_tyA _ _ _) _) fun _ => tl _ _ + | exact OutTyA.bind (OutTyA.pure _) fun _ => tl _ _ + | exact tl _ _ + +/-! The restart and the performs of the map invocations (gen 30). -/ + +/-- **l4v's `thread_set` keeps the kinds** (the TCB stays a TCB). -/ +theorem threadSet_tyA (f : Fl4SpecA64.Tcb → Fl4SpecA64.Tcb) (t : Fl4SpecA64.ObjRef) : + OutTyA (Fl4SpecA64.threadSet (Z := Unit) f t) := fun σ r hr => by + unfold Fl4SpecA64.threadSet at hr + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + have hx' : x ∈ (NonDet.assertOpt (Fl4SpecA64.getTcb (Z := Unit) t σ) σ).1 := hx + cases hg : Fl4SpecA64.getTcb (Z := Unit) t σ with + | none => rw [hg] at hx'; exact absurd hx' (Set.notMem_empty _) + | some y => + rw [hg] at hx' + obtain rfl : x = (y, σ) := hx' + have hk : σ.kheap t = some (.tcb y) := by + cases h : σ.kheap t with + | none => simp [Fl4SpecA64.getTcb, Fl4SpecA64.readObject, h] at hg + | some o => + cases o with + | tcb z => + simp only [Fl4SpecA64.getTcb, Fl4SpecA64.readObject, h, Option.some.injEq] at hg + rw [hg] + | _ => simp [Fl4SpecA64.getTcb, Fl4SpecA64.readObject, h] at hg + obtain ⟨-, -, e⟩ := setObject_mem hr + rw [e] + exact TyKeepA.upd hk (fun _ _ e => by cases e) fun _ _ => ⟨y, rfl⟩ + +/-- **l4v's `set_thread_state` keeps the kinds.** -/ +theorem setThreadState_tyA (t : Fl4SpecA64.ObjRef) (ts : Fl4SpecA64.ThreadState) : + OutTyA (Fl4SpecA64.setThreadState (Z := Unit) t ts) := by + unfold Fl4SpecA64.setThreadState Fl4SpecA64.setThreadStateAct Fl4SpecA64.setSchedulerAction + exact OutTyA.bind (threadSet_tyA _ _) fun _ => OutTyA.bind (OutTyA.gets _) fun _ => + OutTyA.bind (OutTyA.gets _) fun _ => OutTyA.bind (OutTyA.gets _) fun _ => + OutTyA.when (OutTyA.modify fun _ => TyKeepA.of_kheap rfl) + +/-- **The restart** (a `set_thread_state` with a single outcome) keeps the kinds. -/ +theorem restart_tyA {σ σ1 : Fl4SpecA64.State Unit} {t : Fl4SpecA64.ObjRef} {ts : Fl4SpecA64.ThreadState} + (h : Fl4SpecA64.setThreadState (Z := Unit) t ts σ = ({((), σ1)}, False)) : TyKeepA σ σ1 := + setThreadState_tyA t ts σ ((), σ1) (by rw [h]; exact Set.mem_singleton _) + +/-- **l4v's `perform_page_table_invocation` keeps the kinds** (map and unmap). -/ +theorem perfPt_tyA (iv : Fl4SpecA64.PageTableInvocation) : + OutTyA (Fl4SpecA64.performPageTableInvocation (Z := Unit) iv) := by + cases iv with + | pageTableMap cap cte pte slot level => + unfold Fl4SpecA64.performPageTableInvocation Fl4SpecA64.performPtInvMap + exact OutTyA.bind (setCap_tyA _ _) fun _ => OutTyA.bind (storePte_tyA _ _ _) fun _ => doMachineOp_tyA _ + | pageTableUnmap cap cte => exact perfPtUnmap_tyA cap cte + +/-- **l4v's `perform_page_invocation` keeps the kinds** (map, unmap, get-address, flush). -/ +theorem perfPg_tyA (iv : Fl4SpecA64.PageInvocation) : + OutTyA (Fl4SpecA64.performPageInvocation (Z := Unit) iv) := by + cases iv with + | pageMap cap cte e => + obtain ⟨pte, slot, level⟩ := e + unfold Fl4SpecA64.performPageInvocation Fl4SpecA64.performPgInvMap + refine OutTyA.bind ?_ fun _ => OutTyA.pure _ + exact OutTyA.bind (OutTyA.getsMap _ _) fun _ => OutTyA.bind (setCap_tyA _ _) fun _ => + OutTyA.bind (storePte_tyA _ _ _) fun _ => OutTyA.bind (doMachineOp_tyA _) fun _ => + OutTyA.when (OutTyA.bind (OutTyA.assertOpt _) fun ⟨_, _⟩ => invTlbVa_tyA _ _) + | pageUnmap cap cte => exact perfPgUnmap_tyA cap cte + | pageGetAddr p => + unfold Fl4SpecA64.performPageInvocation Fl4SpecA64.performPgInvGetAddr + exact OutTyA.pure _ + | pageFlush ty a b c sp asid => + unfold Fl4SpecA64.performPageInvocation Fl4SpecA64.performFlush + exact OutTyA.bind (OutTyA.bind (OutTyA.pure _) fun _ => OutTyA.bind (OutTyA.pure _) fun _ => + OutTyA.when (doMachineOp_tyA _)) fun _ => OutTyA.pure _ + +end spec + +/-! ## The FL4 side and the keeper -/ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **After `set_cap`, the slot holds the cap and the other `OS` slots their old ones** (module docstring). -/ +theorem setcap_frA {T Ω Sg OS : Set U64} (hO : OSOK T Ω Sg OS) {slot : U64} (hos : slot ∈ OS) + {c : capability.Cap} (hEnc : CapRt.EncWf c) {s2 s3 : S} (hsc : obj.Slot.set_cap st slot s2 c = ok s3) : + capAtA (L.mem s3) slot = c ∧ ∀ y ∈ OS, y ≠ slot → capAtA (L.mem s3) y = capAtA (L.mem s2) y := by + obtain ⟨w0, w1, hen, hd⟩ := CapRt.decode_encode _ hEnc + have a0x := hO.al0 hos + have a8x := hO.al hos (k := 8#u64) (by simp) (by simp) + obtain ⟨v8, -, -⟩ := vals hO hos + rw [set_cap_eq hen] at hsc + have e3 := okinj hsc + subst e3 + refine ⟨?_, fun y hy hne => ?_⟩ + · have hd' := hd + unfold capAtA + rw [L.mem_wr_ne a8x a0x (by omega), L.mem_wr_same a0x, L.mem_wr_same a8x] + rw [CapRt.decode_eq] at hd' + exact okinj hd' + · rw [capAtA_eq, capAtA_eq] + exact capA_wr2_other hO hos hy hne + +/-- **The carried invariants** (module docstring): `PtCapInA` and `FinInvA`. -/ +def CarryA (Z : XSetsA) (mem : U64 → U64) (σ : Fl4SpecA64.State Unit) : Prop := + PtCapInA Z mem ∧ FinInvA Z.th mem σ Z.G + +/-- **`CarryA` across a step keeping the `OS` slots, then the invoked slot's write** (module docstring). -/ +theorem CarryA.of_setcapA {Z : XSetsA} {s s2 s3 : S} {σ σ' : Fl4SpecA64.State Unit} (C : CarryA Z (L.mem s) σ) + {T Ω Sg OS : Set U64} (hO : OSOK T Ω Sg OS) (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hT : TreeA st OS Z.G.chains Z.G.par s) (hk : OSKeep st OS s s2) {slot : U64} (hos : slot ∈ OS) + {c : capability.Cap} (hEnc : CapRt.EncWf c) (hsc : obj.Slot.set_cap st slot s2 c = ok s3) + (hpt : ∀ p a v, c ≠ .PageTable p true a v) (hfe : CapFinEqA c (capAtA (L.mem s) slot)) + (hty : TyKeepA σ σ') : CarryA Z (L.mem s3) σ' := by + obtain ⟨hc, ho⟩ := setcap_frA hO hos hEnc hsc + have ho' : ∀ y ∈ OS, y ≠ slot → capAtA (L.mem s3) y = capAtA (L.mem s) y := fun y hy hne => by + rw [ho y hy hne, capAtA_eq, capAtA_eq]; exact hk.capA hy + refine ⟨C.1.of_capsA rfl (fun _ h => h) (fun _ h => h) fun y hy => ?_, C.2.of_keepA hty.1 hty.2 fun y hy => ?_⟩ + · by_cases e : y = slot + · right; rw [e, hc]; exact hpt + · left; exact ho' y (hsub y hy) e + · by_cases e : y = slot + · rw [e, hc]; exact hfe + · rw [ho' y ((hT.lk.members y).1 hy).1 e]; exact CapFinEqA.refl _ + +/-- (gen 30) **`CarryA` across a map's slot frame** (`SlotFrA`, `P3/VsTabJA`): the other `OS` slots keep their caps, +the invoked slot's new cap is `CapFinEqA` to the old one and, if a mapped page-table cap, names a table of the new +ghosts at depth ≥ 1; the ghosts' Stage-1 tables at depth ≥ 1 and Stage-2 tables are kept; the spec side +`TyKeepA`-related. -/ +theorem CarryA.of_slotFrA {Z Z' : XSetsA} {s s' : S} {σ σ' : Fl4SpecA64.State Unit} (C : CarryA Z (L.mem s) σ) + {OS : Set U64} (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) (hT : TreeA st OS Z.G.chains Z.G.par s) + {sl : U64} {c : capability.Cap} (F : SlotFrA (L := L) OS sl c s s') (hth : Z'.th = Z.th) (hG : Z'.G = Z.G) + (hS : ∀ p, p ∈ Z.S1 ∧ 1 ≤ Z.Lv p → p ∈ Z'.S1 ∧ 1 ≤ Z'.Lv p) (hT2 : ∀ p ∈ Z.T2, p ∈ Z'.T2) + (hpt : ∀ p a v, c = .PageTable p true a v → (p ∈ Z'.S1 ∧ 1 ≤ Z'.Lv p) ∨ p ∈ Z'.T2) + (hfe : CapFinEqA c (capAtA (L.mem s) sl)) (hty : TyKeepA σ σ') : CarryA Z' (L.mem s') σ' := by + refine ⟨fun y hy ptr asid va e => ?_, ?_⟩ + · rw [hth] at hy + by_cases hne : y = sl + · rw [hne, F.1] at e; exact hpt _ _ _ e + · rw [(F.2 y (hsub y hy)).1 hne] at e + rcases C.1 y hy ptr asid va e with h | h + · exact Or.inl (hS _ h) + · exact Or.inr (hT2 _ h) + · rw [hth, hG] + refine C.2.of_keepA hty.1 hty.2 fun y hy => ?_ + by_cases hne : y = sl + · rw [hne, F.1]; exact hfe + · rw [(F.2 y ((hT.lk.members y).1 hy).1).1 hne]; exact CapFinEqA.refl _ + +/-- (gen 30) A frame cap rewritten with new mapping data. -/ +theorem CapFinEqA.frame2A (p rt asid va asid' va' : U64) (sz : capability.FrameSize) (d m m' e : Bool) : + CapFinEqA (.Frame p sz rt d m e asid va) (.Frame p sz rt d m' e asid' va') := + ⟨Set.ext fun r => by rw [genObjRefs_absA, genObjRefs_absA]; exact Iff.rfl, rfl, rfl, rfl⟩ + +/-- (gen 30) A page-table cap rewritten with new mapping data. -/ +theorem CapFinEqA.pt2A (p asid va asid' va' : U64) (m m' : Bool) : + CapFinEqA (.PageTable p m asid va) (.PageTable p m' asid' va') := + ⟨Set.ext fun r => by rw [genObjRefs_absA, genObjRefs_absA]; exact Iff.rfl, rfl, rfl, rfl⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/CnSlotSbA.lean b/proofs/Fl4ProofsA64/P3/CnSlotSbA.lean new file mode 100644 index 000000000..07c164e12 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/CnSlotSbA.lean @@ -0,0 +1,56 @@ +/- +P3 (AArch64, merge line, D8 step 5 (iii)): **a CNode slot is not a TCB slot**, in particular not any TCB's IPC-buffer +cap slot (`sbWordA t`). + +* `cnSlot_ne_tcbSlotA`: the address of a CNode slot of the typed heap (`sl.1 = specPtr p`, `th p` a CNode of + `sl.2.length` bits, what `cspace` lookups return, `P3/LookupA`) differs from the address of every TCB slot of `th` + (`PlaceA` disjointness via `cslotAddrA_inj`). +* `cnSlot_ne_sbA`: the IPC-buffer slot case (`bufSlot_eqA`), the shape the slot-writing arms need + (`∀ t ∈ Z.T, sbWordA t ≠ cslotAddrA cte`, with `JAtZA.thT`). + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.EmptyA +import Fl4ProofsA64.P3.EpBufA +import Fl4ProofsA64.P3.Stage3Rec5A +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4SpecA64 + +/-- **A CNode slot of `th` is at no TCB slot's address** (module docstring). -/ +theorem cnSlot_ne_tcbSlotA {th : TypedHeapA} {kb : U64} (hP : PlaceA th kb) {sl : CslotPtr} {p : U64} + (e1 : sl.1 = specPtr p) (hc : th p = some (.cnode sl.2.length)) {t : U64} (ht : th t = some .tcb) + {i : Nat} (hi : i < tcbSlotsA) : cslotAddrA (specPtr t, tcbCnodeIndex i) ≠ cslotAddrA sl := by + intro e + have hA : AbsSlotA th (specPtr t, tcbCnodeIndex i) := ⟨t, rfl, Or.inr ⟨ht, i, hi, rfl⟩⟩ + have hB : AbsSlotA th sl := ⟨p, e1, Or.inl hc⟩ + have e2 := cslotAddrA_inj hP hA hB e + have e3 : specPtr t = specPtr p := by rw [← e1, ← e2] + rw [specPtr_inj e3, hc] at ht + cases ht + +/-- **A CNode slot of `th` is no TCB's IPC-buffer cap slot** (module docstring). -/ +theorem cnSlot_ne_sbA {th : TypedHeapA} {kb : U64} (hP : PlaceA th kb) {sl : CslotPtr} {p : U64} + (e1 : sl.1 = specPtr p) (hc : th p = some (.cnode sl.2.length)) {t : U64} (ht : th t = some .tcb) : + sbWordA t ≠ cslotAddrA sl := by + have ea : cslotAddrA (specPtr t, tcbCnodeIndex 2) = sbWordA t := by + unfold cslotAddrA + rw [ofBl_tcbIdx 2 (by omega)] + exact Endp.bufSlot_eqA t + rw [← ea] + exact cnSlot_ne_tcbSlotA hP e1 hc ht (i := 2) (by unfold tcbSlotsA; omega) + +/-- **A CNode slot of `th` is no TCB's fault-handler slot.** -/ +theorem cnSlot_ne_fhA {th : TypedHeapA} {kb : U64} (hP : PlaceA th kb) {sl : CslotPtr} {p : U64} + (e1 : sl.1 = specPtr p) (hc : th p = some (.cnode sl.2.length)) {t : U64} (ht : th t = some .tcb) : + slotIdx t layout.tcb.SLOT_FAULT_HANDLER ≠ cslotAddrA sl := by + have ea : cslotAddrA (specPtr t, tcbCnodeIndex 3) = + slotIdx t layout.tcb.SLOT_FAULT_HANDLER := by + unfold cslotAddrA + rw [ofBl_tcbIdx 3 (by omega)] + exact (slotIdx_fhA t).symm + rw [← ea] + exact cnSlot_ne_tcbSlotA hP e1 hc ht (i := 3) (by unfold tcbSlotsA; omega) + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/CnodeCancelA.lean b/proofs/Fl4ProofsA64/P3/CnodeCancelA.lean new file mode 100644 index 000000000..4464fa93b --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/CnodeCancelA.lean @@ -0,0 +1,234 @@ +/- +P3 (AArch64, A64-P3 CNode wave 5): **CNode CancelBadgedSends at the decode** — FL4's `cnode::decode` at label 23 +(`decode_cancel_badged_sends`) against l4v's `decode_cnode_invocation` (the `cancelArmA` arm), then +`set_thread_state Restart` and `invoke_cnode` (`when (b ≠ 0) (cancel_badged_sends ep b)`), wiring the IPC step +`restart_cancel_badged_rel` (`P3/EpCancelBadgedTopA`) (x86 `P3/CnodeCancel`, `cnode_cancel_refines`). + +* **`full_endpoint_relA`** (x86 `full_endpoint_rel`): FL4's `full_endpoint` is `some (p, b)` exactly for an endpoint + cap with all four rights, and then (only then) the spec's `has_cancel_send_rights` holds of the abstract cap. +* **`InvPostCA`**: the outcome: the spec's syscall error with the state unchanged, or an `Ok` whose spec invocation is + `cancelBadgedSendsCall (endpointCap ep b allRights)`, after the spec's restart and `invoke_cnode`, with the IPC + relation `EpStateRel` and the scheduler relation `SR` at the two new states (CancelBadgedSends touches no cap slot). +* **`cnode_cancel_refinesCA`**. + +Hypotheses (Stage-3, ledger): the scheduler operations `SchedOpsEA` (JOIN `ipcSchedOpsA`) and `EPlaced`; the relation +`EpStateRel ∧ SR` at the decode state; the caller a running TCB of `T`; the endpoint of a full-rights endpoint cap +among `E`; the endpoint's queued threads on the current core (`hon`, [S] local scope). No named premise. New module. +-/ +import Fl4ProofsA64.P3.CnodeDecDelA +import Fl4ProofsA64.P3.EpCancelBadgedTopA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Classical + +noncomputable section + +/-! ## `full_endpoint` -/ + +theorem rightsOfA_all (a b c d : Bool) : + rightsOfA a b c d = Fl4SpecA64.allRights ↔ (a = true ∧ b = true ∧ c = true ∧ d = true) := by + constructor + · intro h + have h1 : Fl4SpecA64.Rights.allowWrite ∈ rightsOfA a b c d := by rw [h]; trivial + have h2 : Fl4SpecA64.Rights.allowRead ∈ rightsOfA a b c d := by rw [h]; trivial + have h3 : Fl4SpecA64.Rights.allowGrant ∈ rightsOfA a b c d := by rw [h]; trivial + have h4 : Fl4SpecA64.Rights.allowGrantReply ∈ rightsOfA a b c d := by rw [h]; trivial + exact ⟨h1, h2, h3, h4⟩ + · rintro ⟨rfl, rfl, rfl, rfl⟩ + ext x; cases x <;> simp [rightsOfA, Fl4SpecA64.allRights] + +/-- FL4's `full_endpoint` against the spec's `has_cancel_send_rights` (module docstring). -/ +theorem full_endpoint_relA (c : capability.Cap) : + ∃ o, cnode.full_endpoint c = ok o ∧ + (∀ p b, o = some (p, b) ↔ c = .Endpoint p b true true true true) ∧ + (Fl4SpecA64.hasCancelSendRights (absCapA c) ↔ o.isSome) := by + cases c + case Endpoint p b w r g gr => + cases w <;> cases r <;> cases g <;> cases gr <;> + simp [cnode.full_endpoint, absCapA, Fl4SpecA64.hasCancelSendRights, rightsOfA_all] + all_goals exact ⟨none, rfl, by simp, by simp [absCapA, Fl4SpecA64.hasCancelSendRights]⟩ + +theorem dec_cancel_errA {slbl : Fl4SpecA64.Data} {ac : Fl4SpecA64.Cap} {excaps : List Fl4SpecA64.Cap} + {y0 y1 : Fl4SpecA64.Data} {rest : List Fl4SpecA64.Data} {σ : Fl4SpecA64.State Unit} + {e' : Fl4SpecA64.SyscallError} (hg : Fl4SpecA64.genInvocationType slbl = .cnodeCancelBadgedSends) + (hl : (Fl4SpecA64.lookupTargetSlot (Z := Unit) ac (Fl4SpecA64.dataToCptr y0) (Fl4SpecA64.dataToNat y1)).run σ = + ({(.error e', σ)}, False)) : + (Fl4SpecA64.decodeCnodeInvocation (Z := Unit) slbl (y0 :: y1 :: rest) ac excaps).run σ = + ({(.error e', σ)}, False) := by + rw [decode_cancelA slbl ac excaps y0 y1 rest hg] + exact run_bind_errorA hl _ + +theorem dec_cancel_okA {slbl : Fl4SpecA64.Data} {ac : Fl4SpecA64.Cap} {excaps : List Fl4SpecA64.Cap} + {y0 y1 : Fl4SpecA64.Data} {rest : List Fl4SpecA64.Data} {σ : Fl4SpecA64.State Unit} + {d : Fl4SpecA64.CslotPtr} (hg : Fl4SpecA64.genInvocationType slbl = .cnodeCancelBadgedSends) + (hl : (Fl4SpecA64.lookupTargetSlot (Z := Unit) ac (Fl4SpecA64.dataToCptr y0) (Fl4SpecA64.dataToNat y1)).run σ = + ({(.ok d, σ)}, False)) : + (Fl4SpecA64.decodeCnodeInvocation (Z := Unit) slbl (y0 :: y1 :: rest) ac excaps).run σ = + (cancelArmA d).run σ := by + rw [decode_cancelA slbl ac excaps y0 y1 rest hg, run_bind_okA hl] + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {T E R : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {SR : S → Fl4SpecA64.State Unit → Prop} + +variable (st T E R Bf SR) in +/-- **The outcome of a CancelBadgedSends** against a spec decode `msp` (module docstring). -/ +def InvPostCA (σ : Fl4SpecA64.State Unit) (msp : Fl4SpecA64.SEMonad Unit Fl4SpecA64.CnodeInvocation) (s : S) : + core.result.Result Unit error.Exc × S → Prop + | (r, s') => + (∃ e, r = .Err e ∧ s' = s ∧ ∃ e', msp.run σ = ({(.error e', σ)}, False) ∧ SysErrRelA e e') ∨ + ∃ ep b : U64, r = .Ok () ∧ + msp.run σ = ({(.ok (.cancelBadgedSendsCall (.endpointCap (specPtr ep) b.bv Fl4SpecA64.allRights)), σ)}, False) ∧ + ∃ σ1, Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ ∃ σ', + (.ok (), σ') ∈ ((Fl4SpecA64.invokeCnode (Z := Unit) + (.cancelBadgedSendsCall (.endpointCap (specPtr ep) b.bv Fl4SpecA64.allRights))).run σ1).1 ∧ + EpStateRel T E R Bf (L.mem s') σ' ∧ SR s' σ' + +set_option maxHeartbeats 4000000 in +/-- **CNode CancelBadgedSends**, decode and invoke (x86 `cnode_cancel_refines`; module docstring). -/ +theorem cnode_cancel_refinesCA (arI : machine.Arch A) {th : TypedHeapA} (s : S) (a : A) + (σ : Fl4SpecA64.State Unit) (hR : StateRelA th (L.mem s) σ) (hV : SlotsValidA th (L.mem s)) + (hO : SchedOpsEA st T E R Bf SR) (hP : EPlaced T E R Bf) (hRE : EpStateRel T E R Bf (L.mem s) σ) + (hSR : SR s σ) (hct : L.curW s ∈ T) {xt : Fl4SpecA64.Tcb} + (hkt : σ.kheap (specPtr (L.curW s)) = some (.tcb xt)) (hrun : xt.tcbState = .running) + (hcur : σ.curThread = specPtr (L.curW s)) + (hE : ∀ d p b, AbsSlotA th d → capA st s (cslotAddrA d) = .Endpoint p b true true true true → p ∈ E) + (hon : ∀ p ∈ E, ∀ z, σ.kheap (specPtr p) = some (.endpoint z) → ∀ t ∈ T, specPtr t ∈ z.epQueue → + L.mem s (affA t) = L.cpu s) + (lbl : U64) (slbl : Fl4SpecA64.Data) (hlbl : Fl4SpecA64.genInvocationType slbl = cnodeLabelOfA lbl) + (hl : lbl.val = 23) (length buf : U64) (args : List U64) (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) + (cap : capability.Cap) (ac : Fl4SpecA64.Cap) (hca : CapRelA cap ac) (hcv : CapValidA th cap) + (extra : Aeneas.Std.Array obj.Slot 3#usize) (excaps : List Fl4SpecA64.Cap) : + cnode.decode st arI s a lbl length cap buf extra ⦃ res => + InvPostCA st T E R Bf SR σ (Fl4SpecA64.decodeCnodeInvocation slbl (args.map UScalar.bv) ac excaps) s + (res.1, res.2.1) ∧ res.2.2 = a ⦄ := by + have hg : Fl4SpecA64.genInvocationType slbl = .cnodeCancelBadgedSends := by + rw [hlbl]; simp [cnodeLabelOfA, hl] + have hin : Fl4SpecA64.genInvocationType slbl ∈ + Fl4SpecA64.GenInvocationLabels.uptoEnum .cnodeRevoke .cnodeRotate := by rw [hg]; decide + obtain rfl : lbl = fl4_abi.label.CNodeCancelBadgedSends := by rw [u64_eq_iffA]; simp [hl] + unfold cnode.decode + have n1 : ¬ fl4_abi.label.CNodeCancelBadgedSends < fl4_abi.label.CNodeRevoke := by + have := lblA_revoke; have := lblA_cancel; scalar_tac + have n2 : ¬ fl4_abi.label.CNodeCancelBadgedSends > fl4_abi.label.CNodeRotate := by + have := lblA_cancel; have := lblA_rotate; scalar_tac + rw [if_neg n1, if_neg n2] + by_cases h2 : length < 2#u64 + · rw [if_pos h2] + simp only [error.truncated, bind_ok] + rw [WP.spec_ok] + exact ⟨Or.inl ⟨_, rfl, rfl, .truncatedMessage, decode_shortA _ _ _ σ hin _ (by simp; scalar_tac), trivial⟩, rfl⟩ + rw [if_neg h2] + obtain ⟨y0, y1, rest, rfl⟩ : ∃ y0 y1 rest, args = y0 :: y1 :: rest := by + match args with + | [] => exfalso; simp at hlen; scalar_tac + | [_] => exfalso; simp at hlen; scalar_tac + | y0 :: y1 :: rest => exact ⟨y0, y1, rest, rfl⟩ + simp only [cur_thread_ok, hargs 0#u64 y0 (by simp), hargs 1#u64 y1 (by simp), bind_ok] + simp only [List.map_cons] + unfold cspace.lookup_target_slot + obtain ⟨r1, hr1, e1, he1, hrel1⟩ := spec_existsA + (lookup_slot_for_cnode_op_refinesA th s σ hR hV false cap ac hca hcv y0 y1) + have hlt : (Fl4SpecA64.lookupTargetSlot (Z := Unit) ac (Fl4SpecA64.dataToCptr y0.bv) + (Fl4SpecA64.dataToNat y1.bv)).run σ = ({(e1, σ)}, False) := he1 + rw [hr1]; simp only [bind_ok, core.result.Result.Insts.CoreOpsTry.branch] + cases r1 with + | Err e => + cases e1 with + | ok _ => exact hrel1.elim + | error e' => + simp only [core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, bind_ok, bind_tc_ok] + rw [WP.spec_ok] + exact ⟨Or.inl ⟨_, rfl, rfl, e', dec_cancel_errA hg hlt, + hrel1⟩, rfl⟩ + | Ok dest => + cases e1 with + | error _ => exact hrel1.elim + | ok dsl => + obtain ⟨rfl, pd, hpd1, hpd, hcrd⟩ := hrel1 + have g1 : ¬ fl4_abi.label.CNodeCancelBadgedSends ≥ fl4_abi.label.CNodeCopy := by + have := lblA_copy; have := lblA_cancel; scalar_tac + have k1 : ¬ fl4_abi.label.CNodeCancelBadgedSends = fl4_abi.label.CNodeRevoke := by + rw [u64_eq_iffA]; simp + have k2 : ¬ fl4_abi.label.CNodeCancelBadgedSends = fl4_abi.label.CNodeDelete := by + rw [u64_eq_iffA]; simp + simp only [g1, k1, k2, ↓reduceIte, bind_ok] + have hAd : AbsSlotA th dsl := ⟨pd, hpd1, Or.inl hpd⟩ + obtain ⟨ad, hcad, hgd⟩ := hR.slot_capA hpd1 hpd + replace hcad : CapRelA (capA st s (cslotAddrA dsl)) ad := hcad + unfold cnode.decode_cancel_badged_sends + obtain ⟨o, ho, hoe, hos⟩ := full_endpoint_relA (capA st s (cslotAddrA dsl)) + simp only [cur_thread_ok, capA_eq, bind_ok, ho] + have hsp := (dec_cancel_okA (excaps := excaps) (rest := rest.map UScalar.bv) hg hlt).trans (cancelArm_runA (σ := σ) dsl hgd) + cases o with + | none => + have hn : ¬ Fl4SpecA64.hasCancelSendRights ad := by + rcases hcad.2.eq_or_pt with e | ⟨_, _, _, e⟩ + · rw [e]; intro h; have := hos.1 h; simp at this + · rw [e]; intro h; simp [Fl4SpecA64.hasCancelSendRights] at h + simp only [hn, ↓reduceIte] at hsp + simp only [error.illegal, bind_ok, uncurry_apply_pair] + rw [WP.spec_ok] + exact ⟨Or.inl ⟨_, rfl, rfl, .illegalOperation, hsp, trivial⟩, rfl⟩ + | some pb => + obtain ⟨p, b⟩ := pb + have hce := (hoe p b).1 rfl + have hpE : p ∈ E := hE dsl p b hAd hce + have hrA : rightsOfA true true true true = Fl4SpecA64.allRights := + (rightsOfA_all _ _ _ _).2 ⟨rfl, rfl, rfl, rfl⟩ + have hcad2 : absCapA (capA st s (cslotAddrA dsl)) = ad := + hcad.2.eq_abs fun p' asid va e => by rw [hce] at e; cases e + have hadE : ad = .endpointCap (specPtr p) b.bv Fl4SpecA64.allRights := by + rw [← hcad2, hce] + show Fl4SpecA64.Cap.endpointCap _ _ _ = _ + rw [hrA] + have hy : Fl4SpecA64.hasCancelSendRights ad := by + rw [hadE]; rfl + rw [if_pos hy, hadE] at hsp + have hcode : sched.TS_RESTART.val = tsCode .restart := by unfold sched.TS_RESTART; rfl + have hsetC : Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), stsAct (updK σ (specPtr (L.curW s)) (.tcb { xt with tcbState := .restart })) + (specPtr (L.curW s)))}, False) := by + rw [hcur]; exact setThreadState_eq .restart hkt + have hq := not_ep_queued hRE hct hkt (by rw [hrun]; decide) + obtain ⟨s1, hs1, hR1, hS1⟩ := spec_existsA (set_thread_state_rel hO hP s σ hRE hSR hct hkt hq .restart + (by decide) sched.TS_RESTART hcode (NoBlockE.updT hkt _ (Or.inr (Or.inl ⟨rfl, + (by rw [hrun]; exact fun h => by cases h), Or.inl (Or.inl (by rw [hrun]; rfl))⟩)))) + simp only [hs1, bind_ok, uncurry_apply_pair] + by_cases hb : b = 0#u64 + · subst hb + simp only [bne_self_eq_false, Bool.false_eq_true, ↓reduceIte, bind_ok, uncurry_apply_pair] + rw [WP.spec_ok] + refine ⟨Or.inr ⟨p, 0#u64, rfl, hsp, _, hsetC, _, ?_, hR1, hS1⟩, rfl⟩ + show _ ∈ ((Fl4SpecA64.invokeCnode (Z := Unit) (.cancelBadgedSendsCall + (.endpointCap (specPtr p) 0 Fl4SpecA64.allRights))).run _).1 + rw [invokeCnode_cancel_zeroA]; rfl + · have hbn : (b != 0#u64) = true := by simpa using hb + have hbv : b.bv ≠ 0 := by + intro h; apply hb; apply UScalar.eq_of_val_eq + have := congrArg BitVec.toNat h; simpa using this + simp only [hbn, ↓reduceIte, bind_ok, Std.bind_assoc, uncurry_apply_pair] + have hon1 : ∀ z, (stsAct (updK σ (specPtr (L.curW s)) (.tcb { xt with tcbState := .restart })) + (specPtr (L.curW s))).kheap (specPtr p) = some (.endpoint z) → ∀ t ∈ T, specPtr t ∈ z.epQueue → + L.mem s1 (affA t) = L.cpu s1 := fun z hz t ht hm => + (hO.coreKeep _ _ _ _ hSR hS1).onCore ht (hon p hpE z (by + rw [stsAct_kheap, updK_kheap] at hz + by_cases hpc : specPtr p = specPtr (L.curW s) + · rw [hpc, Function.update_self] at hz; cases hz + · rwa [Function.update_of_ne hpc] at hz) t ht hm) + refine WP.spec_bind (cancel_badged_rel hO hP s1 _ hR1 hS1 hpE b hon1) ?_ + rintro s2 ⟨-, σ', hm, hR2, hS2⟩ + rw [WP.spec_ok] + refine ⟨Or.inr ⟨p, b, rfl, hsp, _, hsetC, σ', ?_, hR2, hS2⟩, rfl⟩ + rw [invokeCnode_cancel_nzA _ _ hbv] + exact liftE_memA hm + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/CnodeCapsA.lean b/proofs/Fl4ProofsA64/P3/CnodeCapsA.lean new file mode 100644 index 000000000..87997f5e1 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/CnodeCapsA.lean @@ -0,0 +1,374 @@ +/- +P3 (aarch64, A64-P3 S3 gen 9, ruling 13:20Z item 1): **the cap transformations of CNode Copy, Mint, Move and Mutate +against the AArch64 spec's** — the A64 twin of x86's `P3/CnodeCaps`. + +* `Rights::from_word` is l4v `data_to_rights` (`rightsW_specA`); +* `mask_cap_rights` is l4v `mask_cap` (`mask_relA`; the frame rights through `mask_vm_rights` / `validate_vm_rights`, + `acap_rights_update` of AArch64's `FrameCap`), and `mask_cap` with all rights is the identity on related caps + (`maskCap_allA`); +* `update_cap_data` is l4v `update_cap_data` (`update_relA`): badges for unbadged endpoint / notification caps + (`badge_bits = 64`), guard and guard size for CNode caps (AArch64 `cnode_padding_bits = 0`, the guard the next 58 + bits); no AArch64 cap carries data (`arch_update_cap_data` only touches SMC caps, which `absCapA` never makes); +* `derive_cap` is l4v `derive_cap` (`derive_relA`; AArch64 `arch_derive_cap`: a frame cap loses its mapping, an + unmapped page-table / VSpace-root cap and (FL4 hook, `Ext`) an unassigned Stage-2 / `ArmHighVSpace` cap cannot be + derived), for an untyped cap under the explicit hypothesis that FL4's `ensure_no_children` decides the spec's + `∃ c, cdt c = some slot` (`NoChildrenRelA`, x86 `NoChildrenRel`). + +Each FL4 result stays `CapWfA`, `EncWf` and `CapValidA`, keeps the source's badge kind (`SameBadgeKindA`) and +(gen 9, for `cap_insert_dA`'s `hsh`) its shape (`shapeA`), as `cap_insert_dA` / `cap_move_jA` need. + +Case hypotheses only (`NoChildrenRelA` for untyped sources); no named premise. New module. +-/ +import Fl4ProofsA64.P3.CnodeSpecA +import Fl4ProofsA64.P3.InsDisA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Classical + +noncomputable section + +/-! ## Rights -/ + +/-- `Rights::from_word w` (x86 `rightsW`). -/ +def rightsWA (w : U64) : capability.Rights := + { write := (w &&& 1#u64) != 0#u64, read := (w &&& 2#u64) != 0#u64, + grant := (w &&& 4#u64) != 0#u64, grant_reply := (w &&& 8#u64) != 0#u64 } + +theorem from_word_eqA (w : U64) : capability.Rights.from_word w = ok (rightsWA w) := by + simp [capability.Rights.from_word, rightsWA, lift] + +theorem and_pow_neA (w : U64) (k : Nat) (hk : k < 64) (c : U64) (hc : c.bv = BitVec.twoPow 64 k) : + ((w &&& c) != 0#u64) = w.bv.getLsbD k := by + have : (w &&& c).bv = w.bv &&& BitVec.twoPow 64 k := by rw [← hc]; rfl + have h2 : BitVec.twoPow 64 k ≠ 0#64 := by + intro h; have := congrArg (fun x => x.getLsbD k) h + simp only [BitVec.getLsbD_twoPow, BitVec.getLsbD_zero] at this + simp [hk] at this + rw [BitVec.and_twoPow] at this + cases hb : w.bv.getLsbD k + · simp only [hb, Bool.false_eq_true, ↓reduceIte] at this + simp [bne_iff_ne, ne_eq, UScalar.eq_equiv_bv_eq, this] + · simp only [hb, ↓reduceIte] at this + have h3 : (w &&& c) ≠ 0#u64 := by + intro e; rw [UScalar.eq_equiv_bv_eq, this] at e; exact h2 e + simpa [bne_iff_ne] using h3 + +/-- `data_to_rights` of a word: its low four bits. -/ +theorem dataToRights_eqA (w : BitVec 64) : + Fl4SpecA64.dataToRights w = rightsOfA (w.getLsbD 0) (w.getLsbD 1) (w.getLsbD 2) (w.getLsbD 3) := by + ext x; cases x <;> simp [Fl4SpecA64.dataToRights, rightsOfA, Fl4SpecA64.dataTo16, BitVec.getLsbD_setWidth] + +theorem rightsW_specA (w : U64) : + rightsOfA (rightsWA w).write (rightsWA w).read (rightsWA w).grant (rightsWA w).grant_reply = + Fl4SpecA64.dataToRights w.bv := by + rw [dataToRights_eqA] + simp only [rightsWA] + rw [and_pow_neA w 0 (by decide) _ (by decide), and_pow_neA w 1 (by decide) _ (by decide), + and_pow_neA w 2 (by decide) _ (by decide), and_pow_neA w 3 (by decide) _ (by decide)] + +/-! ## `mask_cap_rights` -/ + +theorem rightsOf_interA (a b c d a' b' c' d' : Bool) : + rightsOfA a b c d ∩ rightsOfA a' b' c' d' = rightsOfA (a && a') (b && b') (c && c') (d && d') := by + ext x; cases x <;> simp [rightsOfA] + +theorem rightsOf_ntfnA (a b c d : Bool) : + rightsOfA a b c d \ {.allowGrant, .allowGrantReply} = rightsOfA a b false false := by + ext x; cases x <;> simp [rightsOfA] + +theorem replyRights_maskA (g a b c d : Bool) : + (replyRightsA g ∩ rightsOfA a b c d) \ {.allowRead, .allowGrantReply} ∪ {.allowWrite} = + replyRightsA (g && c) := by + ext x; cases g <;> cases c <;> cases x <;> simp [replyRightsA, rightsOfA] + +theorem vm_maskA (v : U64) (hv : 1 ≤ v.val ∧ v.val ≤ 3) (r : capability.Rights) : + ∃ m, capability.mask_vm_rights v r = ok m ∧ 1 ≤ m.val ∧ m.val ≤ 3 ∧ + vmRightsOfA m = Fl4SpecA64.validateVmRights (vmRightsOfA v ∩ rightsOfA r.write r.read r.grant r.grant_reply) := by + have h3 : v.val = 1 ∨ v.val = 2 ∨ v.val = 3 := by omega + have e1 : capability.VM_KERNEL_ONLY = 1#u64 := by simp [capability.VM_KERNEL_ONLY] + have e2 : capability.VM_READ_ONLY = 2#u64 := by simp [capability.VM_READ_ONLY] + have e3 : capability.VM_READ_WRITE = 3#u64 := by simp [capability.VM_READ_WRITE] + have hv' : v = 1#u64 ∨ v = 2#u64 ∨ v = 3#u64 := by + rcases h3 with h | h | h <;> [left; (right; left); (right; right)] <;> (apply UScalar.eq_of_val_eq; simpa using h) + unfold capability.mask_vm_rights + rw [e1, e2, e3] + rcases hv' with rfl | rfl | rfl <;> cases hr : r.read <;> cases hw : r.write <;> + simp [vmRightsOfA, Fl4SpecA64.validateVmRights, Fl4SpecA64.vmReadWrite, Fl4SpecA64.vmReadOnly, + Fl4SpecA64.vmKernelOnly, rightsOfA, hr, hw] + +/-- **`mask_cap_rights r` is l4v `mask_cap`** with the rights of `r` (x86 `mask_rel`; gen 9: and keeps the shape). -/ +theorem mask_relA (r : capability.Rights) (c : capability.Cap) (hc : CapWfA c) : + ∃ c', capability.mask_cap_rights r c = ok c' ∧ CapWfA c' ∧ + absCapA c' = Fl4SpecA64.maskCap (rightsOfA r.write r.read r.grant r.grant_reply) (absCapA c) ∧ + (CapRt.EncWf c → CapRt.EncWf c') ∧ (∀ th, CapValidA th c → CapValidA th c') ∧ SameBadgeKindA c' c ∧ + shapeA c' = shapeA c ∧ (c' = .Null ↔ c = .Null) := by + cases c + case Frame p sz v dev m ept asid va => + obtain ⟨hv1, hv3, ha, hov⟩ := hc + obtain ⟨mv, hm, h1, h2, hmv⟩ := vm_maskA v ⟨hv1, hv3⟩ r + refine ⟨.Frame p sz mv dev m ept asid va, by simp [capability.mask_cap_rights, hm], + ⟨h1, h2, ha, hov⟩, ?_, ?_, fun _ h => h, trivial, by simp [shapeA], by simp⟩ + · cases ept <;> simp [absCapA, absFrameE, Fl4SpecA64.maskCap, Fl4SpecA64.capRights, Fl4SpecA64.capRightsUpdate, + Fl4SpecA64.acapRightsUpdate, Fl4SpecA64.ArchCap.acapRights, hmv] + · intro h + cases ept <;> (simp only [CapRt.EncWf] at h ⊢; obtain ⟨a, b, c, d⟩ := h; exact ⟨a, by omega, c, d⟩) + case Endpoint p b s rc g gr => + refine ⟨.Endpoint p b (s && r.write) (rc && r.read) (g && r.grant) (gr && r.grant_reply), ?_, trivial, ?_, + fun h => h, fun _ h => h, trivial, by simp [shapeA], by simp⟩ + · cases s <;> cases rc <;> cases g <;> cases gr <;> simp [capability.mask_cap_rights] + · simp [absCapA, Fl4SpecA64.maskCap, Fl4SpecA64.capRights, Fl4SpecA64.capRightsUpdate, rightsOf_interA] + case Notification p b s rc => + refine ⟨.Notification p b (s && r.write) (rc && r.read), ?_, trivial, ?_, + fun h => h, fun _ h => h, trivial, by simp [shapeA], by simp⟩ + · cases s <;> cases rc <;> simp [capability.mask_cap_rights] + · simp [absCapA, Fl4SpecA64.maskCap, Fl4SpecA64.capRights, Fl4SpecA64.capRightsUpdate, rightsOf_interA, + rightsOf_ntfnA] + case Reply p g => + refine ⟨.Reply p (g && r.grant), ?_, trivial, ?_, fun h => h, fun _ h => h, trivial, by simp [shapeA], by simp⟩ + · cases g <;> simp [capability.mask_cap_rights] + · simp only [absCapA, Fl4SpecA64.maskCap, Fl4SpecA64.capRights, Fl4SpecA64.capRightsUpdate, replyRights_maskA] + all_goals + refine ⟨_, rfl, hc, ?_, fun h => h, fun _ h => h, by simp [SameBadgeKindA], rfl, Iff.rfl⟩ + simp [absCapA, Fl4SpecA64.maskCap, Fl4SpecA64.capRightsUpdate, Fl4SpecA64.acapRightsUpdate] + +/-- With all rights, `mask_cap` leaves a related cap alone (Move, Mutate; x86 `maskCap_all`). -/ +theorem maskCap_allA (c : capability.Cap) (hc : CapWfA c) : + Fl4SpecA64.maskCap Fl4SpecA64.allRights (absCapA c) = absCapA c := by + cases c + case Frame p sz v dev m ept asid va => + have h3 : v.val = 1 ∨ v.val = 2 ∨ v.val = 3 := by obtain ⟨h1, h2, -⟩ := hc; omega + cases ept <;> + simp only [absCapA, absFrameE, Bool.false_eq_true, ↓reduceIte, Fl4SpecA64.maskCap, Fl4SpecA64.capRights, + Fl4SpecA64.capRightsUpdate, Fl4SpecA64.acapRightsUpdate, Fl4SpecA64.ArchCap.acapRights, + Fl4SpecA64.ExtArchCap.rights, Fl4SpecA64.ExtArchCap.rightsUpdate, Fl4SpecA64.allRights, Set.inter_univ] <;> + rcases h3 with h | h | h <;> + simp [vmRightsOfA, h, Fl4SpecA64.validateVmRights, Fl4SpecA64.vmReadWrite, Fl4SpecA64.vmReadOnly, + Fl4SpecA64.vmKernelOnly] + case Notification p b s r => + simp only [absCapA, Fl4SpecA64.maskCap, Fl4SpecA64.capRights, Fl4SpecA64.capRightsUpdate, Fl4SpecA64.allRights, + Set.inter_univ, rightsOf_ntfnA] + case Reply p g => + simp only [absCapA, Fl4SpecA64.maskCap, Fl4SpecA64.capRights, Fl4SpecA64.capRightsUpdate, Fl4SpecA64.allRights, + Set.inter_univ] + congr 1; ext x; cases g <;> cases x <;> simp [replyRightsA] + all_goals simp [absCapA, Fl4SpecA64.maskCap, Fl4SpecA64.capRights, Fl4SpecA64.capRightsUpdate, + Fl4SpecA64.acapRightsUpdate, Fl4SpecA64.allRights] + +/-! ## `update_cap_data` -/ + +theorem objects_mask_eqA (b : U64) : objects.mask b = cspace.mask b := rfl + +theorem cap_is_null_nullA : capability.Cap.is_null .Null = ok true := rfl +theorem cap_is_null_neA {c : capability.Cap} (h : c ≠ .Null) : capability.Cap.is_null c = ok false := by + cases c <;> simp_all <;> rfl + +theorem spec_existsA {α : Type} {m : Result α} {P : α → Prop} (h : m ⦃ r => P r ⦄) : ∃ v, m = ok v ∧ P v := by + revert h; cases m <;> simp [WP.spec_vis, WP.spec_div, WP.spec_ok] + +theorem shr_eqA (x : U64) (k : I32) (h0 : 0 ≤ k.val) (h : k.val < 64) : x >>> k = ok ⟨x.bv >>> k.toNat⟩ := by + have hk : k.toNat < 64 := by scalar_tac + simp only [HShiftRight.hShiftRight, UScalar.shiftRight_IScalar, h0, UScalar.shiftRight, ite_true] + simp [hk, BitVec.ushiftRight_eq] + +theorem spec_mask_andA (x : BitVec 64) (n : Nat) (hn : n < 64) : + (x &&& (Fl4SpecA64.mask n : BitVec 64)).toNat = x.toNat % 2 ^ n := by + have hp : 2 ^ n < 2 ^ 64 := Nat.pow_lt_pow_right (by decide) hn + have hm : (Fl4SpecA64.mask n : BitVec 64).toNat = 2 ^ n - 1 := by + have h2 : 1 ≤ 2 ^ n := Nat.one_le_two_pow + simp only [Fl4SpecA64.mask] + rw [BitVec.toNat_sub, Nat.one_shiftLeft] + simp; omega + rw [BitVec.toNat_and, hm, Nat.and_two_pow_sub_one_eq_mod] + +theorem spec_mask_64A (x : BitVec 64) : x &&& (Fl4SpecA64.mask 64 : BitVec 64) = x := by + have : (Fl4SpecA64.mask 64 : BitVec 64) = BitVec.allOnes 64 := by + apply BitVec.eq_of_toNat_eq; simp [Fl4SpecA64.mask] + rw [this, BitVec.and_allOnes] + +/-- The spec's `update_cap_data` on a CNode cap (x86 `update_cnode_spec`; AArch64 `cnode_padding_bits = 0`). -/ +theorem update_cnode_specA (pr : Bool) (w : U64) (p : BitVec 64) (r : Nat) (gd : List Bool) : + Fl4SpecA64.updateCapData pr w.bv (.cnodeCap p r gd) = + if w.val % 64 + r > 64 then .nullCap else .cnodeCap p r (bitsOfA (w.val / 64) (w.val % 64)) := by + have hsG : ((w.bv >>> 0) &&& (Fl4SpecA64.mask 6 : BitVec 64)).toNat = w.val % 64 := by + rw [spec_mask_andA _ _ (by decide)]; simp + have h58 : ((w.bv >>> 6) &&& (Fl4SpecA64.mask 58 : BitVec 64)).toNat = w.val / 64 := by + rw [spec_mask_andA _ _ (by decide), BitVec.toNat_ushiftRight, Nat.shiftRight_eq_div_pow] + apply Nat.mod_eq_of_lt + have := w.hBounds; simp [U64.size_def, UScalarTy.U64_numBits_eq, U64.numBits] at this ⊢; omega + simp only [Fl4SpecA64.updateCapData, Fl4SpecA64.isEpCap, Fl4SpecA64.isNtfnCap, Fl4SpecA64.isCnodeCap, + Fl4SpecA64.theCnodeCap, Fl4SpecA64.updateCnodeCapData, Fl4SpecA64.cnodePaddingBits, + Fl4SpecA64.cnodeGuardSizeBits, Fl4SpecA64.wordBits, Fl4SpecA64.unat, Bool.false_eq_true, ↓reduceIte, + Nat.zero_add, hsG] + have hl : w.val % 64 ≤ 64 := by omega + rw [toBl_dropA _ _ hl, h58] + +/-- **`update_cap_data preserve w` is l4v `update_cap_data preserve w`** (x86 `update_rel`; gen 9: a non-null +result keeps the shape). -/ +theorem update_relA (pr : Bool) (w : U64) (c : capability.Cap) (hc : CapWfA c) (he : CapRt.EncWf c) : + ∃ c', objects.update_cap_data pr w c = ok c' ∧ CapWfA c' ∧ + absCapA c' = Fl4SpecA64.updateCapData pr w.bv (absCapA c) ∧ CapRt.EncWf c' ∧ + (∀ th, CapValidA th c → CapValidA th c') ∧ SameBadgeKindA c' c ∧ (c' ≠ .Null → shapeA c' = shapeA c) := by + cases c + case Endpoint p b s r g gr => + by_cases hp : pr + · subst hp + exact ⟨.Null, rfl, trivial, by simp [absCapA, Fl4SpecA64.updateCapData, Fl4SpecA64.isEpCap], trivial, + fun _ _ => trivial, trivial, fun h => absurd rfl h⟩ + · by_cases hb : b = 0#u64 + · refine ⟨.Endpoint p w s r g gr, by simp [objects.update_cap_data, hp, hb], trivial, ?_, he, + fun _ h => h, trivial, fun _ => by simp [shapeA]⟩ + have : b.bv = 0#64 := by rw [hb]; rfl + simp [absCapA, Fl4SpecA64.updateCapData, Fl4SpecA64.isEpCap, Fl4SpecA64.capEpBadge, Fl4SpecA64.badgeUpdate, + this, hp, Fl4SpecA64.badgeBits, spec_mask_64A] + · refine ⟨.Null, by simp [objects.update_cap_data, hp, hb], trivial, ?_, trivial, fun _ _ => trivial, + trivial, fun h => absurd rfl h⟩ + have : b.bv ≠ 0#64 := by intro e; exact hb (UScalar.eq_of_val_eq (by simpa using congrArg BitVec.toNat e)) + simp [absCapA, Fl4SpecA64.updateCapData, Fl4SpecA64.isEpCap, Fl4SpecA64.capEpBadge, this] + case Notification p b s r => + by_cases hp : pr + · subst hp + exact ⟨.Null, rfl, trivial, by simp [absCapA, Fl4SpecA64.updateCapData, Fl4SpecA64.isEpCap, + Fl4SpecA64.isNtfnCap], trivial, fun _ _ => trivial, trivial, fun h => absurd rfl h⟩ + · by_cases hb : b = 0#u64 + · refine ⟨.Notification p w s r, by simp [objects.update_cap_data, hp, hb], trivial, ?_, he, + fun _ h => h, trivial, fun _ => by simp [shapeA]⟩ + have : b.bv = 0#64 := by rw [hb]; rfl + simp [absCapA, Fl4SpecA64.updateCapData, Fl4SpecA64.isEpCap, Fl4SpecA64.isNtfnCap, Fl4SpecA64.capEpBadge, + Fl4SpecA64.badgeUpdate, this, hp, Fl4SpecA64.badgeBits, spec_mask_64A] + · refine ⟨.Null, by simp [objects.update_cap_data, hp, hb], trivial, ?_, trivial, fun _ _ => trivial, + trivial, fun h => absurd rfl h⟩ + have : b.bv ≠ 0#64 := by intro e; exact hb (UScalar.eq_of_val_eq (by simpa using congrArg BitVec.toNat e)) + simp [absCapA, Fl4SpecA64.updateCapData, Fl4SpecA64.isEpCap, Fl4SpecA64.isNtfnCap, Fl4SpecA64.capEpBadge, + this] + case CNode ptr radix gs g => + obtain ⟨hr, hgs, hg⟩ := hc + obtain ⟨hcan, -, -, hg58⟩ := he + have hG : (w &&& 63#u64).val = w.val % 64 := by + simp; exact Nat.and_two_pow_sub_one_eq_mod w.val 6 + have hGl : (w &&& 63#u64).val < 64 := by rw [hG]; omega + have hsum : (core.num.U64.wrapping_add (w &&& 63#u64) radix).val = w.val % 64 + radix.val := by + rw [core.num.U64.wrapping_add_val_eq, hG]; simp [U64.size_def, UScalarTy.U64_numBits_eq, U64.numBits]; omega + have hspec := update_cnode_specA pr w (specPtr ptr) radix.val (bitsOfA g.val gs.val) + unfold objects.update_cap_data + simp only [lift, bind_ok, absCapA] + rw [hspec] + by_cases hgt : w.val % 64 + radix.val > 64 + · have : core.num.U64.wrapping_add (w &&& 63#u64) radix > 64#u64 := by scalar_tac + simp only [this, ↓reduceIte, hgt] + exact ⟨.Null, rfl, trivial, rfl, trivial, fun _ _ => trivial, trivial, fun h => absurd rfl h⟩ + · have : ¬ core.num.U64.wrapping_add (w &&& 63#u64) radix > 64#u64 := by scalar_tac + simp only [this, ↓reduceIte, hgt] + rw [shr_eqA _ _ (by decide) (by decide), objects_mask_eqA] + simp only [bind_ok] + obtain ⟨m, hm1, hm2⟩ := spec_existsA (mask_specA (w &&& 63#u64) hGl) + rw [hm1]; simp only [bind_ok] + have hgv : ((⟨w.bv >>> 6⟩ : U64) &&& m).val = (w.val / 64) % 2 ^ (w.val % 64) := by + have : ((⟨w.bv >>> 6⟩ : U64) &&& m).val = (w.bv >>> 6).toNat &&& m.val := by + show ((w.bv >>> 6) &&& m.bv).toNat = _ + rw [BitVec.toNat_and]; rfl + rw [this, hm2, Nat.and_two_pow_sub_one_eq_mod, BitVec.toNat_ushiftRight, Nat.shiftRight_eq_div_pow, hG] + simp + refine ⟨.CNode ptr radix (w &&& 63#u64) (⟨w.bv >>> 6⟩ &&& m), rfl, ⟨hr, hGl, ?_⟩, ?_, ⟨hcan, hr, hGl, ?_⟩, + fun _ h => h, trivial, fun _ => by simp [shapeA]⟩ + · rw [hgv, hG]; exact Nat.mod_lt _ (by positivity) + · have e : bitsOfA (w.val / 64 % 2 ^ (w.val % 64)) (w.val % 64) = bitsOfA (w.val / 64) (w.val % 64) := + (bitsOfA_eq_iff _ _ _).2 (Nat.mod_mod _ _) + simp only [absCapA, hgv, hG, e] + · rw [hgv] + have := w.hBounds; simp [U64.size_def, UScalarTy.U64_numBits_eq, U64.numBits] at this + have : w.val / 64 < 2 ^ 58 := by omega + exact lt_of_le_of_lt (Nat.mod_le _ _) this + all_goals + refine ⟨_, rfl, hc, ?_, he, fun _ h => h, by simp [SameBadgeKindA], fun _ => rfl⟩ + try (simp only [absCapA, absFrameE]; split_ifs) + all_goals simp [absCapA, Fl4SpecA64.updateCapData, Fl4SpecA64.isEpCap, Fl4SpecA64.isNtfnCap, Fl4SpecA64.isCnodeCap, + Fl4SpecA64.isArchCap, Fl4SpecA64.archUpdateCapData, Fl4SpecA64.Cap.theArchCap, Fl4SpecA64.ArchCap.isSMCCap] + +/-! ## `derive_cap` -/ + +/-- FL4's `derive_cap` outcome against the spec's (x86 `DeriveRel`; gen 9: with the shape). -/ +def DeriveRelA (c : capability.Cap) : core.result.Result capability.Cap error.Exc → + Except Fl4SpecA64.SyscallError Fl4SpecA64.Cap → Prop + | .Ok c', .ok a => CapWfA c' ∧ absCapA c' = a ∧ CapRt.EncWf c' ∧ (∀ th, CapValidA th c → CapValidA th c') ∧ + SameBadgeKindA c' c ∧ (c' ≠ .Null → shapeA c' = shapeA c) + | .Err e, .error e' => SysErrRelA e e' + | _, _ => False + +section +variable {S : Type} {st : machine.Store S} + +/-- **Hypothesis** (an MDB fact, not proved here; x86 `NoChildrenRel`): at slot `slot`, FL4's `ensure_no_children` +decides whether the spec's `cdt` has a child of `sl`. -/ +def NoChildrenRelA (s : S) (σ : Fl4SpecA64.State Unit) (slot : obj.Slot) (sl : Fl4SpecA64.CslotPtr) : Prop := + cdt.ensure_no_children st s slot = + ok (if ∃ x, σ.cdt x = some sl then .Err (.Sys .RevokeFirst) else .Ok ()) + +theorem ensureNoChildren_runA (σ : Fl4SpecA64.State Unit) (sl : Fl4SpecA64.CslotPtr) : + (Fl4SpecA64.ensureNoChildren (Z := Unit) sl).run σ = + ({(if ∃ x, σ.cdt x = some sl then .error .revokeFirst else .ok (), σ)}, False) := by + unfold Fl4SpecA64.ensureNoChildren + rw [run_bind_okA (run_liftEA (m := Fl4Spec.NonDet.gets fun s : Fl4SpecA64.State Unit => s.cdt) (s := σ) rfl)] + by_cases h : ∃ x, σ.cdt x = some sl + · simp only [Fl4Spec.NonDetE.whenE, h, ↓reduceIte]; rfl + · simp only [Fl4Spec.NonDetE.whenE, h, ↓reduceIte]; rfl + +/-- **`derive_cap` is l4v `derive_cap`** (x86 `derive_rel`); for an untyped cap under `NoChildrenRelA`. Neither +changes the state. -/ +theorem derive_relA (s : S) (σ : Fl4SpecA64.State Unit) (slot : obj.Slot) (sl : Fl4SpecA64.CslotPtr) + (c : capability.Cap) (hc : CapWfA c) (he : CapRt.EncWf c) + (hnc : (match c with | .Untyped .. => True | _ => False) → NoChildrenRelA (st := st) s σ slot sl) : + ∃ r, objects.derive_cap st s slot c = ok r ∧ ∃ r', + (Fl4SpecA64.deriveCap (Z := Unit) sl (absCapA c)).run σ = ({(r', σ)}, False) ∧ DeriveRelA c r r' := by + cases c + case Untyped p sb fi dev => + have h := hnc trivial + unfold objects.derive_cap + rw [h] + simp only [bind_ok, absCapA, Fl4SpecA64.deriveCap] + rw [ExceptT.run_bind, nd_bind_singleA (ensureNoChildren_runA σ sl)] + by_cases hx : ∃ x, σ.cdt x = some sl + · simp only [hx, ↓reduceIte, core.result.Result.Insts.CoreOpsTry.branch, bind_ok, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame, + core.convert.FromSame.from] + exact ⟨.Err (.Sys .RevokeFirst), by simp, _, rfl, show SysErrRelA (.Sys .RevokeFirst) .revokeFirst from trivial⟩ + · simp only [hx, ↓reduceIte, core.result.Result.Insts.CoreOpsTry.branch, bind_ok] + exact ⟨_, rfl, _, rfl, hc, rfl, he, fun _ h => h, trivial, fun _ => rfl⟩ + case Frame p sz v dev m ept asid va => + obtain ⟨h1, h2, h3, h4⟩ := hc + cases ept <;> refine ⟨_, rfl, _, rfl, ?_⟩ <;> + refine ⟨⟨h1, h2, by simp, h4⟩, ?_, ?_, fun _ _ => by simp [CapValidA], trivial, fun _ => by simp [shapeA]⟩ + · simp [absCapA, absFrameE, mapDataA] + · simp only [CapRt.EncWf] at he ⊢; obtain ⟨a, b, -, -⟩ := he; exact ⟨a, b, by simp, Or.inl fun j hj _ => by simp⟩ + · simp [absCapA, absFrameE, mapDataA] + · simp only [CapRt.EncWf] at he ⊢; obtain ⟨a, b, -, -⟩ := he; exact ⟨a, b, by simp, Or.inl fun j hj _ => by simp⟩ + case PageTable p m asid va => + cases m + · exact ⟨_, rfl, _, rfl, trivial⟩ + · exact ⟨_, rfl, _, rfl, hc, rfl, he, fun _ h => h, trivial, fun _ => rfl⟩ + case Pml4 p m asid => + cases m + · exact ⟨_, rfl, _, rfl, trivial⟩ + · exact ⟨_, rfl, _, rfl, hc, rfl, he, fun _ h => h, trivial, fun _ => rfl⟩ + case Ept p level m asid va => + cases m + · exact ⟨_, rfl, _, rfl, trivial⟩ + · exact ⟨_, rfl, _, rfl, hc, rfl, he, fun _ h => h, trivial, fun _ => rfl⟩ + case HighVSpace p m asid => + cases m + · exact ⟨_, rfl, _, rfl, trivial⟩ + · exact ⟨_, rfl, _, rfl, hc, rfl, he, fun _ h => h, trivial, fun _ => rfl⟩ + case PageDirectory => exact hc.elim + case Pdpt => exact hc.elim + case IoPortControl => exact hc.elim + case TdxControl => exact hc.elim + case IoPort => exact hc.elim + case IrqControl => exact ⟨_, rfl, _, rfl, trivial, rfl, trivial, fun _ _ => trivial, trivial, fun h => absurd rfl h⟩ + case Zombie => exact ⟨_, rfl, _, rfl, trivial, rfl, trivial, fun _ _ => trivial, trivial, fun h => absurd rfl h⟩ + all_goals exact ⟨_, rfl, _, rfl, hc, rfl, he, fun _ h => h, by simp [SameBadgeKindA], fun _ => rfl⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/CnodeCopyA.lean b/proofs/Fl4ProofsA64/P3/CnodeCopyA.lean new file mode 100644 index 000000000..fa847f078 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/CnodeCopyA.lean @@ -0,0 +1,921 @@ +/- +P3 (aarch64, A64-P3 S3 gen 10, ruling 13:44Z item 1): **the CNode Copy, Mint, Move and Mutate invocations** (FL4 +`cnode::decode` with `decode_copy`, `copied_cap`) against l4v `decode_cnode_invocation` followed by +`set_thread_state Restart` and `invoke_cnode` (as `handle_invocation` runs them) — the A64 port of x86's +`P3/CnodeCopy` (`copied_rel`, `decode_copy_rel`, `cnode_copy_refines`) and `P3/CnodeDecode` (`decode_illegal`, the +dispatch), on the A64 leaves: `cap_insert_dA` (Copy, Mint) and `cap_move_jA` (Move, Mutate), both after +`JEntA.restartA` (`P3/RestartA`). + +* `cnodeLabelOfA`: FL4's AArch64 CNode labels (21..28) as the spec's. +* **`copyOkA_of`** (spec side): when l4v's `derive_cap` of the masked (Mint: and badged) source gives a non-null cap, + the source is no zombie, a badged source keeps its badge (an unbadged one may get a new one: Mint), and an untyped + source is unchanged and has no `cdt` child (`CopyOkA`; x86 `CnodeShape.copied_shape`'s `isZb` / badge / `isUt` + parts, on the spec caps). +* `UA.of_shapeA` / `sbk_of_shapeA`: a cap of the source's shape is `UA`-alike to it (an untyped cap unchanged) and of + its badge kind — what `cap_insert_dA` / `cap_move_jA` ask (`hal`, `hk`). +* **`copied_relA`** (x86 `copied_rel`): FL4's `copied_cap` against the spec's `copyRestA`, the result `CapWfA`, + `EncWf`, `CapValidA`, of the source's shape, an untyped source's abstract cap unchanged, and (copy / mint) + `CopyOkA`; `derive_cap`'s `ensure_no_children` by `NoChildrenRelA` (from `noChildren_of_adjA` at the call site). +* **`decode_copy_relA`** (x86 `decode_copy_rel`): `decode_copy` against `copyTailA`, ending in `InvPostA`: the spec's + error with the state unchanged, or the spec's invocation after which `set_thread_state Restart` (`JEntA.restartA`) + and `invoke_cnode` (`cap_insert_dA` / `cap_move_jA`) end in a `JAtZAX`-related state, with the MDB tree and + `SInvXA` carried. +* **`cnode_copy_refinesA`** (x86 `cnode_copy_refines`): `cnode::decode` for labels 24..27 (the target lookup, + `lookup_slot_for_cnode_op_refinesA`, then `decode_copy_relA`); `cnode_illegal_refinesA`: every label outside 21..28 + (`IllegalOperation`). (Gen 11: the dispatch `cnode_decode_refinesA` moved to `P3/CnodeRotA`, extended with Rotate.) + +Case hypotheses (decode-site facts, not premises): those of `JEntA.restartA` (`P`, `Y`, `hrun`, `hcur`, `hvs`, +`hvc`), A64 P1's tree at `OS` with `th`'s slots in it and its other slots null (`hT`, `hsub`, `hnul`, as +`cap_insert_dA`), the message (`hargs`: `syscall_arg i` is the `i`-th argument, as x86), the extra cap +(`ExtraRelA`), the label (`hlbl`). No named premise. New module. +-/ +import Fl4ProofsA64.P3.RestartA +import Fl4ProofsA64.P3.NoChildA +import Fl4ProofsA64.P1.MdbWin +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## Labels -/ + +/-- FL4's AArch64 CNode labels (`fl4_abi::label`, 21..28) as the spec's (x86 `cnodeLabelOf`). -/ +def cnodeLabelOfA (lbl : U64) : Fl4SpecA64.GenInvocationLabels := + if lbl.val = 21 then .cnodeRevoke else if lbl.val = 22 then .cnodeDelete + else if lbl.val = 23 then .cnodeCancelBadgedSends else if lbl.val = 24 then .cnodeCopy + else if lbl.val = 25 then .cnodeMint else if lbl.val = 26 then .cnodeMove + else if lbl.val = 27 then .cnodeMutate else if lbl.val = 28 then .cnodeRotate + else .invalidInvocation + +theorem u64_eq_iffA (x y : U64) : x = y ↔ x.val = y.val := ⟨congrArg _, UScalar.eq_of_val_eq⟩ + +@[simp] theorem lblA_revoke : fl4_abi.label.CNodeRevoke.val = 21 := by simp [fl4_abi.label.CNodeRevoke] +@[simp] theorem lblA_copy : fl4_abi.label.CNodeCopy.val = 24 := by simp [fl4_abi.label.CNodeCopy] +@[simp] theorem lblA_mint : fl4_abi.label.CNodeMint.val = 25 := by simp [fl4_abi.label.CNodeMint] +@[simp] theorem lblA_move : fl4_abi.label.CNodeMove.val = 26 := by simp [fl4_abi.label.CNodeMove] +@[simp] theorem lblA_mutate : fl4_abi.label.CNodeMutate.val = 27 := by simp [fl4_abi.label.CNodeMutate] +@[simp] theorem lblA_rotate : fl4_abi.label.CNodeRotate.val = 28 := by simp [fl4_abi.label.CNodeRotate] + +/-! ## The caps a copy makes, spec side -/ + +/-- **What `cap_insert_dA` asks of a copied cap, on the spec caps** (module docstring): the source `a0` is no zombie, +a badged source's badge is kept, an untyped source is unchanged and childless at `sl`. -/ +def CopyOkA (σ : Fl4SpecA64.State Unit) (sl : Fl4SpecA64.CslotPtr) (a0 an : Fl4SpecA64.Cap) : Prop := + (∀ p n z, a0 ≠ .zombie p n z) ∧ + (∀ r b rs, (a0 = .endpointCap r b rs ∨ a0 = .notificationCap r b rs) → b ≠ 0 → Fl4SpecA64.capEpBadge an = b) ∧ + (∀ dv p b i, a0 = .untypedCap dv p b i → an = a0 ∧ ¬ ∃ x, σ.cdt x = some sl) + +theorem run_single_okA {σ : Fl4SpecA64.State Unit} {a b : Fl4SpecA64.Cap} + (h : ({(.ok a, σ)}, False) = (({(.ok b, σ)}, False) : Set (Except Fl4SpecA64.SyscallError Fl4SpecA64.Cap × + Fl4SpecA64.State Unit) × Prop)) : a = b := by + have := congrArg Prod.fst h + simpa using this + +theorem run_single_errA {σ : Fl4SpecA64.State Unit} {e : Fl4SpecA64.SyscallError} {b : Fl4SpecA64.Cap} + (h : ({(.error e, σ)}, False) = (({(.ok b, σ)}, False) : Set (Except Fl4SpecA64.SyscallError Fl4SpecA64.Cap × + Fl4SpecA64.State Unit) × Prop)) : False := by + have := congrArg Prod.fst h + simp at this + +/-- **`derive_cap` of the masked (Mint: and badged) source** (module docstring). -/ +theorem copyOk_ofA (σ : Fl4SpecA64.State Unit) (sl : Fl4SpecA64.CslotPtr) (mint : Bool) (R : Fl4SpecA64.CapRights) + (d : Fl4SpecA64.Data) (a0 an : Fl4SpecA64.Cap) + (hd : (Fl4SpecA64.deriveCap (Z := Unit) sl (if mint then Fl4SpecA64.updateCapData false d + (Fl4SpecA64.maskCap R a0) else Fl4SpecA64.maskCap R a0)).run σ = ({(.ok an, σ)}, False)) + (hn : an ≠ .nullCap) : CopyOkA σ sl a0 an := by + refine ⟨fun p n z e => ?_, fun r b rs e hb => ?_, fun dv p b i e => ?_⟩ + · subst e + cases mint <;> + simp [Fl4SpecA64.maskCap, Fl4SpecA64.capRightsUpdate, Fl4SpecA64.updateCapData, Fl4SpecA64.isEpCap, + Fl4SpecA64.isNtfnCap, Fl4SpecA64.isCnodeCap, Fl4SpecA64.isArchCap, Fl4SpecA64.deriveCap] at hd <;> + first | exact hn hd.symm | exact hn (run_single_okA hd).symm + · have hb' : b ≠ 0#64 := hb + rcases e with rfl | rfl <;> cases mint <;> + simp [Fl4SpecA64.maskCap, Fl4SpecA64.capRightsUpdate, Fl4SpecA64.updateCapData, Fl4SpecA64.isEpCap, + Fl4SpecA64.isNtfnCap, Fl4SpecA64.capEpBadge, hb', Fl4SpecA64.deriveCap] at hd + all_goals first + | exact absurd hd.symm hn + | (rw [← hd]; rfl) + | exact absurd (run_single_okA hd).symm hn + · subst e + have hx : (if mint then Fl4SpecA64.updateCapData false d (Fl4SpecA64.maskCap R (.untypedCap dv p b i)) + else Fl4SpecA64.maskCap R (.untypedCap dv p b i)) = .untypedCap dv p b i := by + cases mint <;> simp [Fl4SpecA64.maskCap, Fl4SpecA64.capRightsUpdate, Fl4SpecA64.updateCapData, + Fl4SpecA64.isEpCap, Fl4SpecA64.isNtfnCap, Fl4SpecA64.isCnodeCap, Fl4SpecA64.isArchCap] + rw [hx] at hd + unfold Fl4SpecA64.deriveCap at hd + rw [ExceptT.run_bind, nd_bind_singleA (ensureNoChildren_runA σ sl)] at hd + by_cases hc : ∃ x, σ.cdt x = some sl + · simp only [hc, ↓reduceIte] at hd + exact (run_single_errA hd).elim + · simp only [hc, ↓reduceIte] at hd + exact ⟨(run_single_okA hd).symm, hc⟩ + +/-! ## Caps of the source's shape -/ + +theorem physA_shapeA (c : capability.Cap) : physA (shapeA c) = physA c := + okinj ((physA_eq _).symm.trans ((show objects.cap_is_physical (shapeA c) = objects.cap_is_physical c by + cases c <;> rfl).trans (physA_eq c))) + +theorem ptrA_shapeA (c : capability.Cap) : ptrA (shapeA c) = ptrA c := + okinj ((ptrA_eq _).symm.trans ((show capability.Cap.ptr (shapeA c) = capability.Cap.ptr c by + cases c <;> rfl).trans (ptrA_eq c))) + +theorem sizeA_shapeA (c : capability.Cap) : sizeA (shapeA c) = sizeA c := + okinj ((sizeA_eq _).symm.trans ((show objects.cap_size_bits (shapeA c) = objects.cap_size_bits c by + cases c <;> rfl).trans (sizeA_eq c))) + +theorem isUtA_shapeA (c : capability.Cap) : isUtA (shapeA c) = isUtA c := by cases c <;> rfl + +theorem isUtA_ut {c : capability.Cap} (h : isUtA c = true) : ∃ p b i e, c = .Untyped p b i e := by + cases c <;> simp_all [isUtA] + +/-- **A cap of the source's shape is alike to it** (module docstring), an untyped one being the source. -/ +theorem UA.of_shapeA {d c : capability.Cap} (hsh : shapeA d = shapeA c) (hu : isUtA c = true → d = c) : UA d c := + ⟨by rw [← physA_shapeA d, hsh, physA_shapeA], by rw [← ptrA_shapeA d, hsh, ptrA_shapeA], + by rw [← sizeA_shapeA d, hsh, sizeA_shapeA], by rw [← isUtA_shapeA d, hsh, isUtA_shapeA], + fun p b i e h => by + have hc : isUtA c = true := by rw [← isUtA_shapeA c, ← hsh, isUtA_shapeA, h]; rfl + have e2 := hu hc + exact ⟨i, e, e2 ▸ h, le_refl _, Or.inl rfl⟩⟩ + +/-- A cap of the source's shape has its badge kind. -/ +theorem sbk_of_shapeA {d c : capability.Cap} (h : shapeA d = shapeA c) : SameBadgeKindA d c := by + cases d + case Endpoint => cases c <;> simp_all [shapeA, SameBadgeKindA] + case Notification => cases c <;> simp_all [shapeA, SameBadgeKindA] + all_goals trivial + +theorem absA_null_iff {c : capability.Cap} (hc : CapWfA c) : absCapA c = .nullCap ↔ c = .Null := by + constructor + · intro h; cases c <;> simp_all [absCapA, CapWfA] + · rintro rfl; rfl + +/-- An untyped cap of an untyped source's shape with its abstract cap is the source. -/ +theorem ut_of_absA {d c : capability.Cap} (hsh : shapeA d = shapeA c) (hc : isUtA c = true) + (ha : absCapA d = absCapA c) : d = c := by + obtain ⟨p, b, i, e, rfl⟩ := isUtA_ut hc + have hd : isUtA d = true := by rw [← isUtA_shapeA d, hsh]; rfl + obtain ⟨p', b', i', e', rfl⟩ := isUtA_ut hd + simp only [absCapA, Fl4SpecA64.Cap.untypedCap.injEq] at ha + obtain ⟨h1, h2, h3, h4⟩ := ha + rw [specPtr_inj h2, UScalar.eq_of_val_eq h3, UScalar.eq_of_val_eq h4, h1] + +/-! ## `copied_cap` -/ + +/-- FL4's `copied_cap` outcome against the spec's `copyRestA` (x86 `CopiedRel`; module docstring). -/ +def CopiedRelA (σ : Fl4SpecA64.State Unit) (srcC : capability.Cap) (sl dsl : Fl4SpecA64.CslotPtr) : + core.result.Result (capability.Cap × Bool) error.Exc → + Except Fl4SpecA64.SyscallError Fl4SpecA64.CnodeInvocation → Prop + | .Err e, .error e' => SysErrRelA e e' + | .Ok (nc, mv), R => R = finishResA mv sl dsl (.ok (absCapA nc)) ∧ CapWfA nc ∧ CapRt.EncWf nc ∧ + (∀ th, CapValidA th srcC → CapValidA th nc) ∧ (nc ≠ .Null → shapeA nc = shapeA srcC) ∧ + (isUtA srcC = true → nc ≠ .Null → absCapA nc = absCapA srcC) ∧ + (mv = false → nc ≠ .Null → CopyOkA σ sl (absCapA srcC) (absCapA nc)) + | _, _ => False + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **`copied_cap` refines the per-label part of `decode_cnode_invocation`** (`copyRestA`; x86 `copied_rel`). -/ +theorem copied_relA (s : S) (σ : Fl4SpecA64.State Unit) (lbl : U64) (slbl : Fl4SpecA64.Data) + (hlbl : Fl4SpecA64.genInvocationType slbl = cnodeLabelOfA lbl) (hl : 24 ≤ lbl.val ∧ lbl.val ≤ 27) + (length buf ct : U64) (hct : sched.cur_thread st s = ok ct) (rest : List U64) + (hlen : length.val = rest.length + 4) + (hargs : ∀ (i : U64) x, rest[i.val - 4]? = some x → 4 ≤ i.val → msg.syscall_arg st s ct buf i = ok x) + (sl dsl : Fl4SpecA64.CslotPtr) (srcC : capability.Cap) (hw : CapWfA srcC) (he : CapRt.EncWf srcC) + (hnc : isUtA srcC = true → NoChildrenRelA (st := st) s σ (cslotAddrA sl) sl) : + ∃ r, cnode.copied_cap st s lbl length buf (cslotAddrA sl) srcC = ok r ∧ ∃ R, + (copyRestA slbl (rest.map UScalar.bv) sl dsl (absCapA srcC)).run σ = ({(R, σ)}, False) ∧ + CopiedRelA σ srcC sl dsl r R := by + have hcases : lbl.val = 24 ∨ lbl.val = 25 ∨ lbl.val = 26 ∨ lbl.val = 27 := by omega + -- the derive step shared by Copy and Mint + have hder : ∀ c1, CapWfA c1 → CapRt.EncWf c1 → (∀ th, CapValidA th srcC → CapValidA th c1) → + shapeA c1 = shapeA srcC → + (∀ r', (Fl4SpecA64.deriveCap (Z := Unit) sl (absCapA c1)).run σ = ({(r', σ)}, False) → + (copyRestA slbl (rest.map UScalar.bv) sl dsl (absCapA srcC)).run σ = + ({(finishResA false sl dsl r', σ)}, False) ∧ + ∀ an, r' = .ok an → an ≠ .nullCap → CopyOkA σ sl (absCapA srcC) an) → + ∃ r, (do + let r1 ← objects.derive_cap st s (cslotAddrA sl) c1 + let cf ← core.result.Result.Insts.CoreOpsTry.branch r1 + match cf with + | core.ops.control_flow.ControlFlow.Continue val => ok (core.result.Result.Ok (val, false)) + | core.ops.control_flow.ControlFlow.Break residual => + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual (capability.Cap × Bool) + (core.convert.FromSame error.Exc) residual) = ok r ∧ ∃ R, + (copyRestA slbl (rest.map UScalar.bv) sl dsl (absCapA srcC)).run σ = ({(R, σ)}, False) ∧ + CopiedRelA σ srcC sl dsl r R := by + intro c1 hw1 he1 hv1 hs1 hrest + obtain ⟨r, hr, r', hr', hd⟩ := derive_relA (st := st) s σ (cslotAddrA sl) sl c1 hw1 he1 (fun h => hnc (by + have hu : isUtA c1 = true := by revert h; cases c1 <;> simp [isUtA] + rw [← isUtA_shapeA, hs1, isUtA_shapeA] at hu + exact hu)) + rw [hr]; simp only [bind_ok] + obtain ⟨hR, hok⟩ := hrest r' hr' + cases r with + | Ok c' => + cases r' with + | error _ => exact hd.elim + | ok a' => + obtain ⟨hw', ha', he', hv', -, hs'⟩ := hd + refine ⟨.Ok (c', false), by simp [core.result.Result.Insts.CoreOpsTry.branch], _, hR, by rw [ha'], hw', he', + fun th h => hv' th (hv1 th h), fun h => (hs' h).trans hs1, fun hu hn' => ?_, fun _ hn' => ?_⟩ + · have hc := hok a' rfl (by rw [← ha']; exact fun e => hn' ((absA_null_iff hw').1 e)) + obtain ⟨p, b, i, e, hsr⟩ := isUtA_ut hu + rw [ha'] + exact (hc.2.2 e (specPtr p) b.val i.val (by rw [hsr]; rfl)).1 + · rw [ha']; exact hok a' rfl (by rw [← ha']; exact fun e => hn' ((absA_null_iff hw').1 e)) + | Err e => + cases r' with + | ok _ => exact hd.elim + | error e' => + refine ⟨.Err e, ?_, _, hR, hd⟩ + simp [core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame, + core.convert.FromSame.from] + unfold cnode.copied_cap + rw [hct]; simp only [bind_ok] + rcases hcases with hv | hv | hv | hv + · -- Copy + have hg : Fl4SpecA64.genInvocationType slbl = .cnodeCopy := by rw [hlbl]; simp [cnodeLabelOfA, hv] + have e : lbl = fl4_abi.label.CNodeCopy := by rw [u64_eq_iffA]; simp [hv] + rw [if_pos e] + cases rest with + | nil => + have : length < 5#u64 := by scalar_tac + rw [if_pos this] + exact ⟨_, rfl, _, copyRest_truncA _ _ _ _ σ _ (Or.inl ⟨hg, rfl⟩), trivial⟩ + | cons w rest => + have : ¬ length < 5#u64 := by scalar_tac + rw [if_neg this] + rw [hargs 4#u64 w (by simp) (by simp)]; simp only [bind_ok]; rw [from_word_eqA]; simp only [bind_ok] + obtain ⟨c1, hm, hw1, ha1, he1, hv1, -, hs1, -⟩ := mask_relA (rightsWA w) srcC hw + rw [hm]; simp only [bind_ok] + refine hder c1 hw1 (he1 he) hv1 hs1 (fun r' hr' => ?_) + rw [ha1, rightsW_specA] at hr' + refine ⟨copyRest_copyA _ _ _ _ σ hg w.bv _ hr', fun an e hn => ?_⟩ + subst e + exact copyOk_ofA σ sl false _ 0 _ an hr' hn + · -- Mint + have hg : Fl4SpecA64.genInvocationType slbl = .cnodeMint := by rw [hlbl]; simp [cnodeLabelOfA, hv] + have e : lbl = fl4_abi.label.CNodeMint := by rw [u64_eq_iffA]; simp [hv] + have e' : ¬ lbl = fl4_abi.label.CNodeCopy := by rw [u64_eq_iffA]; simp [hv] + rw [if_neg e', if_pos e] + match rest, hlen, hargs with + | [], hlen, _ => + have : length < 6#u64 := by scalar_tac + rw [if_pos this] + exact ⟨_, rfl, _, copyRest_truncA _ _ _ _ σ _ (Or.inr (Or.inl ⟨hg, by simp⟩)), trivial⟩ + | [_], hlen, _ => + have : length < 6#u64 := by simp at hlen; scalar_tac + rw [if_pos this] + exact ⟨_, rfl, _, copyRest_truncA _ _ _ _ σ _ (Or.inr (Or.inl ⟨hg, by simp⟩)), trivial⟩ + | w :: d :: rest, hlen, hargs => + have : ¬ length < 6#u64 := by simp at hlen; scalar_tac + rw [if_neg this] + rw [hargs 4#u64 w (by simp) (by simp)]; simp only [bind_ok]; rw [from_word_eqA]; simp only [bind_ok] + rw [hargs 5#u64 d (by simp) (by simp)]; simp only [bind_ok] + obtain ⟨c1, hm, hw1, ha1, he1, hv1, -, hs1, -⟩ := mask_relA (rightsWA w) srcC hw + rw [hm]; simp only [bind_ok] + obtain ⟨c2, hu, hw2, ha2, he2, hv2, -, hs2⟩ := update_relA false d c1 hw1 (he1 he) + rw [hu]; simp only [bind_ok] + by_cases hc2 : c2 = .Null + · -- a null update: `derive_cap` of `Null` is `Null` + subst hc2 + have hR : (copyRestA slbl ((w :: d :: rest).map UScalar.bv) sl dsl (absCapA srcC)).run σ = + ({(finishResA false sl dsl (.ok .nullCap), σ)}, False) := by + refine copyRest_mintA _ _ _ _ σ hg w.bv d.bv _ ?_ + rw [← rightsW_specA, ← ha1, ← ha2]; rfl + refine ⟨.Ok (.Null, false), by simp [objects.derive_cap, core.result.Result.Insts.CoreOpsTry.branch], _, hR, rfl, trivial, by simp [CapRt.EncWf], fun _ _ => trivial, + fun h => absurd rfl h, fun _ h => absurd rfl h, fun _ h => absurd rfl h⟩ + have hs12 : shapeA c2 = shapeA srcC := (hs2 hc2).trans hs1 + refine hder c2 hw2 he2 (fun th h => hv2 th (hv1 th h)) hs12 (fun r' hr' => ?_) + rw [ha2, ha1, rightsW_specA] at hr' + refine ⟨copyRest_mintA _ _ _ _ σ hg w.bv d.bv _ hr', fun an e hn => ?_⟩ + subst e + exact copyOk_ofA σ sl true _ d.bv _ an hr' hn + · -- Move + have hg : Fl4SpecA64.genInvocationType slbl = .cnodeMove := by rw [hlbl]; simp [cnodeLabelOfA, hv] + have e : lbl = fl4_abi.label.CNodeMove := by rw [u64_eq_iffA]; simp [hv] + have e1 : ¬ lbl = fl4_abi.label.CNodeCopy := by rw [u64_eq_iffA]; simp [hv] + have e2 : ¬ lbl = fl4_abi.label.CNodeMint := by rw [u64_eq_iffA]; simp [hv] + rw [if_neg e1, if_neg e2, if_pos e] + refine ⟨.Ok (srcC, true), rfl, _, copyRest_moveA _ _ _ _ σ hg _, ?_, hw, he, fun _ h => h, fun _ => rfl, + fun _ _ => rfl, fun h => absurd h (by simp)⟩ + rw [maskCap_allA _ hw] + · -- Mutate + have hg : Fl4SpecA64.genInvocationType slbl = .cnodeMutate := by rw [hlbl]; simp [cnodeLabelOfA, hv] + have e1 : ¬ lbl = fl4_abi.label.CNodeCopy := by rw [u64_eq_iffA]; simp [hv] + have e2 : ¬ lbl = fl4_abi.label.CNodeMint := by rw [u64_eq_iffA]; simp [hv] + have e3 : ¬ lbl = fl4_abi.label.CNodeMove := by rw [u64_eq_iffA]; simp [hv] + rw [if_neg e1, if_neg e2, if_neg e3] + cases rest with + | nil => + have : length < 5#u64 := by scalar_tac + rw [if_pos this] + exact ⟨_, rfl, _, copyRest_truncA _ _ _ _ σ _ (Or.inr (Or.inr ⟨hg, rfl⟩)), trivial⟩ + | cons d rest => + have : ¬ length < 5#u64 := by scalar_tac + rw [if_neg this] + rw [hargs 4#u64 d (by simp) (by simp)]; simp only [bind_ok] + obtain ⟨c', hu, hw', ha', he', hv', -, hs'⟩ := update_relA true d srcC hw he + rw [hu]; simp only [bind_ok] + refine ⟨.Ok (c', true), rfl, _, copyRest_mutateA _ _ _ _ σ hg d.bv _, ?_, hw', he', hv', hs', + fun hu' _ => ?_, fun h => absurd h (by simp)⟩ + · rw [maskCap_allA _ hw, ha'] + · obtain ⟨p, b, i, e, rfl⟩ := isUtA_ut hu' + rw [ha'] + simp [absCapA, Fl4SpecA64.updateCapData, Fl4SpecA64.isEpCap, Fl4SpecA64.isNtfnCap, + Fl4SpecA64.isCnodeCap, Fl4SpecA64.isArchCap] + +end + + +/-! ## `decode_copy` -/ + +/-- Extra caps (x86 `ExtraRel`): `extra[0]` (`x0`) is null and there are none, or it is a slot of `th` whose cap is +related to the spec's first extra cap. -/ +def ExtraRelA (th : TypedHeapA) (mem : U64 → U64) (x0 : U64) (excaps : List Fl4SpecA64.Cap) : Prop := + (x0 = 0#u64 ∧ excaps = []) ∨ + (x0 ≠ 0#u64 ∧ IsSlotA th x0 ∧ ∃ a rest, CapRelA (capAtA mem x0) a ∧ excaps = a :: rest) + +/-- A spec computation with the single, state-preserving outcome `R` (x86 `ofExcept`). -/ +def ofExceptA {α : Type} (R : Except Fl4SpecA64.SyscallError α) : Fl4SpecA64.SEMonad Unit α := + match R with + | .ok a => Fl4Spec.NonDetE.returnOk a + | .error e => Fl4Spec.NonDetE.throwError e + +theorem run_ofExceptA {α : Type} (R : Except Fl4SpecA64.SyscallError α) (σ : Fl4SpecA64.State Unit) : + (ofExceptA R).run σ = ({(R, σ)}, False) := by + cases R <;> rfl + +/-- An outcome of `m` is one of `liftE m`. -/ +theorem liftE_memA {α : Type} {m : Fl4Spec.NonDet (Fl4SpecA64.State Unit) α} {σ σ' : Fl4SpecA64.State Unit} {a : α} + (h : (a, σ') ∈ (m σ).1) : (.ok a, σ') ∈ ((Fl4Spec.NonDetE.liftE (ε := Unit) m).run σ).1 := by + unfold Fl4Spec.NonDetE.liftE + rw [ExceptT.run_lift] + show (Except.ok a, σ') ∈ ((m >>= _) σ).1 + simp only [nd_bind_applyA] + exact ⟨(a, σ'), h, rfl⟩ + +/-- A CNode slot's cap and its single `get_cap` outcome (x86 `StateRel.slot_cap`). -/ +theorem StateRelA.slot_capA {th : TypedHeapA} {mem : U64 → U64} {σ : Fl4SpecA64.State Unit} + (hR : StateRelA th mem σ) {sl : Fl4SpecA64.CslotPtr} {p : U64} (h1 : sl.1 = (specPtr p)) + (hp : th p = some (.cnode sl.2.length)) : + ∃ a, CapRelA (capAtA mem (cslotAddrA sl)) a ∧ Fl4SpecA64.getCap (Z := Unit) sl σ = ({(a, σ)}, False) := by + obtain ⟨cs, hk, hwf, hs⟩ := hR.cnode p _ hp + obtain ⟨a, ha, hca⟩ := hs sl.2 rfl + obtain ⟨o, idx⟩ := sl + simp only at h1; subst h1 + exact ⟨a, hca, getCap_cnodeA hk hwf ha⟩ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-- The outcome of a Copy / Mint / Move / Mutate against a spec decode `msp` (x86 `InvPost`; module docstring). -/ +def InvPostA (AR : A → HwRootA → Prop) (Z : XSetsA) (OS : Set U64) (σ : Fl4SpecA64.State Unit) + (m : Fl4SpecA64.McState) (msp : Fl4SpecA64.SEMonad Unit Fl4SpecA64.CnodeInvocation) (s : S) (a : A) : + core.result.Result Unit error.Exc × S → Prop + | (.Err e, s') => s' = s ∧ ∃ e', msp.run σ = ({(.error e', σ)}, False) ∧ SysErrRelA e e' + | (.Ok (), s') => ∃ inv, msp.run σ = ({(.ok inv, σ)}, False) ∧ ∃ σ1, + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ∃ σ' G', (.ok (), σ') ∈ ((Fl4SpecA64.invokeCnode (Z := Unit) inv).run σ1).1 ∧ + JAtZAX (L := L) AR { Z with G := G' } s' a σ' m ∧ TreeA st OS G'.chains G'.par s' ∧ + ∀ n, SInvXA σ m n → SInvXA σ' m n + +theorem InvPostA.congr {AR : A → HwRootA → Prop} {Z : XSetsA} {OS : Set U64} {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} {msp msp' : Fl4SpecA64.SEMonad Unit Fl4SpecA64.CnodeInvocation} + (h : msp.run σ = msp'.run σ) {s : S} {a : A} {prog : Result (core.result.Result Unit error.Exc × S)} + (hp : prog ⦃ res => InvPostA (L := L) AR Z OS σ m msp' s a res ⦄) : + prog ⦃ res => InvPostA (L := L) AR Z OS σ m msp s a res ⦄ := by + refine WP.spec_mono hp ?_ + rintro ⟨r, s'⟩ hq + cases r with + | Err e => obtain ⟨h1, e', h2, h3⟩ := hq; exact ⟨h1, e', h.trans h2, h3⟩ + | Ok u => obtain ⟨inv, h2, h3⟩ := hq; exact ⟨inv, h.trans h2, h3⟩ + +theorem InvPostA.of_run {AR : A → HwRootA → Prop} {Z : XSetsA} {OS : Set U64} {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} {msp msp' : Fl4SpecA64.SEMonad Unit Fl4SpecA64.CnodeInvocation} + (h : msp.run σ = msp'.run σ) {s : S} {a : A} {res : core.result.Result Unit error.Exc × S} + (hq : InvPostA (L := L) AR Z OS σ m msp' s a res) : InvPostA (L := L) AR Z OS σ m msp s a res := by + obtain ⟨r, s'⟩ := res + cases r with + | Err e => obtain ⟨h1, e', h2, h3⟩ := hq; exact ⟨h1, e', h.trans h2, h3⟩ + | Ok u => obtain ⟨inv, h2, h3⟩ := hq; exact ⟨inv, h.trans h2, h3⟩ + +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +private theorem copyRestA_pt_run (slbl : Fl4SpecA64.Data) (args : List Fl4SpecA64.Data) + (sl dsl : Fl4SpecA64.CslotPtr) (σ : Fl4SpecA64.State Unit) + (p : Fl4SpecA64.ObjRef) (asid : Fl4SpecA64.Asid) (vx : Fl4SpecA64.VSpaceRef) + (hgt4 : IsCopyLabelA (Fl4SpecA64.genInvocationType slbl)) : + let cx := Fl4SpecA64.Cap.archObjectCap (.pageTableCap p .normalPtT (some (asid, vx))) + (copyRestA slbl args sl dsl cx).run σ = + match Fl4SpecA64.genInvocationType slbl, args with + | .cnodeCopy, _ :: _ => ({(finishResA false sl dsl (.ok cx), σ)}, False) + | .cnodeMint, _ :: _ :: _ => ({(finishResA false sl dsl (.ok cx), σ)}, False) + | .cnodeMove, _ => ({(finishResA true sl dsl (.ok cx), σ)}, False) + | .cnodeMutate, _ :: _ => ({(finishResA true sl dsl (.ok cx), σ)}, False) + | _, _ => ({(.error .truncatedMessage, σ)}, False) := by + intro cx + have hm : ∀ r, Fl4SpecA64.maskCap r cx = cx := fun _ => rfl + have hu : ∀ b d, Fl4SpecA64.updateCapData b d cx = cx := fun _ _ => rfl + have hd : (Fl4SpecA64.deriveCap (Z := Unit) sl cx).run σ = ({(.ok cx, σ)}, False) := rfl + rcases hgt4 with hgt | hgt | hgt | hgt + · cases args with + | nil => rw [hgt]; exact copyRest_truncA _ _ _ _ σ [] (Or.inl ⟨hgt, rfl⟩) + | cons w rs => rw [copyRest_copyA _ _ _ _ σ hgt w rs (by rw [hm]; exact hd), hgt] + · cases args with + | nil => rw [hgt]; exact copyRest_truncA _ _ _ _ σ [] (Or.inr (Or.inl ⟨hgt, by simp⟩)) + | cons w rs => + cases rs with + | nil => rw [hgt]; exact copyRest_truncA _ _ _ _ σ [w] (Or.inr (Or.inl ⟨hgt, by simp⟩)) + | cons d rs' => rw [copyRest_mintA _ _ _ _ σ hgt w d rs' (by rw [hm, hu]; exact hd), hgt] + · rw [copyRest_moveA _ _ _ _ σ hgt args, hm, hgt] + · cases args with + | nil => rw [hgt]; exact copyRest_truncA _ _ _ _ σ [] (Or.inr (Or.inr ⟨hgt, rfl⟩)) + | cons d rs => rw [copyRest_mutateA _ _ _ _ σ hgt d rs, hm, hu, hgt] + +private theorem pair_singleton_eqA {α β : Type} {x y : α} {b : β} + (h : ({(x, b)}, False) = (({(y, b)}, False) : Set (α × β) × Prop)) : x = y := by + have := congrArg Prod.fst h + simpa using this + +private theorem copyRestA_pt_err (slbl : Fl4SpecA64.Data) (args : List Fl4SpecA64.Data) + (sl dsl : Fl4SpecA64.CslotPtr) (σ : Fl4SpecA64.State Unit) + (p : Fl4SpecA64.ObjRef) (asid : Fl4SpecA64.Asid) (v1 v2 : Fl4SpecA64.VSpaceRef) + (hgt4 : IsCopyLabelA (Fl4SpecA64.genInvocationType slbl)) (e : Fl4SpecA64.SyscallError) + (h : (copyRestA slbl args sl dsl (.archObjectCap (.pageTableCap p .normalPtT (some (asid, v1))))).run σ = + ({(.error e, σ)}, False)) : + (copyRestA slbl args sl dsl (.archObjectCap (.pageTableCap p .normalPtT (some (asid, v2))))).run σ = + ({(.error e, σ)}, False) := by + rw [copyRestA_pt_run _ _ _ _ _ _ _ _ hgt4] at h ⊢ + rcases hgt4 with hgt | hgt | hgt | hgt <;> rw [hgt] at h ⊢ <;> + cases args with + | nil => + first | exact h | (have h0 := pair_singleton_eqA h; simp [finishResA] at h0) + | cons w1 rs1 => + cases rs1 with + | nil => first | exact h | (have h0 := pair_singleton_eqA h; simp [finishResA] at h0) + | cons w2 rs2 => have h0 := pair_singleton_eqA h; simp [finishResA] at h0 + +private theorem copyRestA_pt_ok (slbl : Fl4SpecA64.Data) (args : List Fl4SpecA64.Data) + (sl dsl : Fl4SpecA64.CslotPtr) (σ : Fl4SpecA64.State Unit) + (p : Fl4SpecA64.ObjRef) (asid : Fl4SpecA64.Asid) (v1 v2 : Fl4SpecA64.VSpaceRef) + (hgt4 : IsCopyLabelA (Fl4SpecA64.genInvocationType slbl)) (mv : Bool) (c : Fl4SpecA64.Cap) + (h : (copyRestA slbl args sl dsl (.archObjectCap (.pageTableCap p .normalPtT (some (asid, v1))))).run σ = + ({(finishResA mv sl dsl (.ok c), σ)}, False)) : + c = .archObjectCap (.pageTableCap p .normalPtT (some (asid, v1))) ∧ + (copyRestA slbl args sl dsl (.archObjectCap (.pageTableCap p .normalPtT (some (asid, v2))))).run σ = + ({(finishResA mv sl dsl (.ok (.archObjectCap (.pageTableCap p .normalPtT (some (asid, v2))))), σ)}, False) := by + have hne1 : Fl4SpecA64.Cap.archObjectCap (.pageTableCap p .normalPtT (some (asid, v1))) ≠ .nullCap := by simp + rw [copyRestA_pt_run _ _ _ _ _ _ _ _ hgt4] at h ⊢ + rcases hgt4 with hgt | hgt | hgt | hgt <;> rw [hgt] at h ⊢ <;> + cases args with + | nil => + have h0 := pair_singleton_eqA h + by_cases hc : c = .nullCap <;> + cases mv <;> simp only [finishResA, Bool.false_eq_true, ↓reduceIte, hne1, hc] at h0 <;> + first | (injection h0 with h1; injection h1 with h2; exact ⟨h2.symm, rfl⟩) | cases h0 + | cons w1 rs1 => + cases rs1 with + | nil => + have h0 := pair_singleton_eqA h + by_cases hc : c = .nullCap <;> + cases mv <;> simp only [finishResA, Bool.false_eq_true, ↓reduceIte, hne1, hc] at h0 <;> + first | (injection h0 with h1; injection h1 with h2; exact ⟨h2.symm, rfl⟩) | cases h0 + | cons w2 rs2 => + have h0 := pair_singleton_eqA h + by_cases hc : c = .nullCap <;> + cases mv <;> simp only [finishResA, Bool.false_eq_true, ↓reduceIte, hne1, hc] at h0 <;> + first | (injection h0 with h1; injection h1 with h2; exact ⟨h2.symm, rfl⟩) | cases h0 + +/-- **`decode_copy` refines the spec's Copy / Mint / Move / Mutate branch** (`copyTailA`, after the target slot +lookup), then `set_thread_state Restart` and `invoke_cnode` (x86 `decode_copy_rel`; module docstring). -/ +theorem decode_copy_relA (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + {Rp Nt : Set U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} {slot : U64} + (P : SysPlace st H Z.Cs H.frame Rp Nt OS s extra slot) (Y : SymL st H Z.Cs s) + (hrun : Runnable (L.mem s (stA (L.curW s)))) + (hcur : (L.mem s (inqA (L.curW s)) &&& sched.Q_RELEASE) = 0#u64 ∧ L.mem s (scA (L.curW s)) ≠ 0#u64 ∧ + L.mem s (scA (L.curW s)) ∈ Ac) + (hvs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → Aligned b ∧ b ≠ stA (L.curW s)) + (hvc : ∀ v ∈ Z.V, stA (L.curW s) ≠ v ∧ stA (L.curW s) ≠ wa v 24#u64) + (hT : TreeA st OS Z.G.chains Z.G.par s) (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) + (lbl : U64) (slbl : Fl4SpecA64.Data) (hlbl : Fl4SpecA64.genInvocationType slbl = cnodeLabelOfA lbl) + (hl : 24 ≤ lbl.val ∧ lbl.val ≤ 27) (length buf : U64) (args : List U64) + (hlen : length.val = args.length) (h2 : 2 ≤ args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) + (x0 : U64) (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) + (excaps : List Fl4SpecA64.Cap) (hex : ExtraRelA Z.th (L.mem s) x0 excaps) + (dsl : Fl4SpecA64.CslotPtr) (pd : U64) (hpd1 : dsl.1 = (specPtr pd)) (hpd : Z.th pd = some (.cnode dsl.2.length)) + (hpdn : ¬ IrqNodeA (L.ksb s) pd) : + cnode.decode_copy st s lbl length buf extra (cslotAddrA dsl) ⦃ res => + InvPostA (L := L) AR Z OS σ m (copyTailA slbl ((args.map UScalar.bv).drop 2) excaps dsl) s a res ⦄ := by + have hct : sched.cur_thread st s = ok (L.curW s) := cur_thread_ok s + have hR := E.j.1.st + have hV : SlotsValidA Z.th (L.mem s) := E.j.1.cv + have hP := E.j.1.place + have hgl : IsCopyLabelA (Fl4SpecA64.genInvocationType slbl) := by + rw [hlbl]; unfold cnodeLabelOfA IsCopyLabelA + have : lbl.val = 24 ∨ lbl.val = 25 ∨ lbl.val = 26 ∨ lbl.val = 27 := by omega + rcases this with h | h | h | h <;> simp [h] + unfold cnode.decode_copy + by_cases h4 : length < 4#u64 + · rw [if_pos h4] + simp only [error.truncated, bind_ok, WP.spec_ok] + refine ⟨rfl, .truncatedMessage, ?_, trivial⟩ + rw [copyTail_shortA _ _ hgl _ _ (fun ⟨h, _⟩ => by simp at h; scalar_tac)]; rfl + rw [if_neg h4] + obtain ⟨y0, y1, i1, d1, rest, rfl⟩ : ∃ y0 y1 i1 d1 rest, args = y0 :: y1 :: i1 :: d1 :: rest := by + match args, h2 with + | [_, _], _ => exfalso; simp at hlen; scalar_tac + | [_, _, _], _ => exfalso; simp at hlen; scalar_tac + | y0 :: y1 :: i1 :: d1 :: rest, _ => exact ⟨y0, y1, i1, d1, rest, rfl⟩ + simp only [List.map_cons, List.drop_succ_cons, List.drop_zero] + rw [hx0]; simp only [bind_ok, obj.Slot.is_null] + rcases hex with ⟨rfl, rfl⟩ | ⟨hx0n, hx0s, ac0, erest, hca0, rfl⟩ + · simp only [decide_true, ↓reduceIte, error.truncated, bind_ok, WP.spec_ok] + refine ⟨rfl, .truncatedMessage, ?_, trivial⟩ + rw [copyTail_shortA _ _ hgl _ _ (fun ⟨_, h⟩ => by simp at h)]; rfl + simp only [hx0n, decide_false, Bool.false_eq_true, ↓reduceIte] + simp only [hct, hargs 2#u64 i1 (by simp), hargs 3#u64 d1 (by simp), capA_eq, bind_ok] + rw [copyTail_bodyA _ hgl] + -- the destination must be empty + obtain ⟨ad, hcad, hgd⟩ := hR.slot_capA hpd1 hpd + replace hcad : CapRelA (capA st s (cslotAddrA dsl)) ad := hcad + unfold cdt.ensure_empty_slot + simp only [capA_eq, bind_ok] + have hee := ensureEmpty_runA dsl σ hgd + by_cases hdn : capA st s (cslotAddrA dsl) = .Null + swap + · rw [cap_is_null_neA hdn] + simp only [bind_ok, Bool.false_eq_true, ↓reduceIte, core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame, + core.convert.FromSame.from, bind_tc_ok] + rw [WP.spec_ok] + have : ad ≠ .nullCap := hcad.ne_null hdn + simp only [this, ↓reduceIte] at hee + exact ⟨rfl, .deleteFirst, run_bind_errorA hee _, trivial⟩ + have hadn : ad = .nullCap := by + have h := hcad.2; rw [hdn] at h; exact h.symm + simp only [hadn, ↓reduceIte] at hee + rw [hdn, cap_is_null_nullA] + simp only [bind_ok, ↓reduceIte, core.result.Result.Insts.CoreOpsTry.branch] + refine InvPostA.congr (run_bind_okA hee _) ?_ + -- the source slot + rw [show cspace.lookup_source_slot st s (capA st s x0) i1 d1 = + cspace.lookup_slot_for_cnode_op st s true (capA st s x0) i1 d1 from rfl] + obtain ⟨r1, hr1, e1, he1, hrel1⟩ := spec_existsA + (lookup_slot_for_cnode_op_refinesA Z.th s σ hR hV true (capA st s x0) ac0 hca0 (hV x0 hx0s) i1 d1) + rw [hr1]; simp only [bind_ok] + have hls : (Fl4SpecA64.lookupSourceSlot (Z := Unit) ac0 (Fl4SpecA64.dataToCptr i1.bv) + (Fl4SpecA64.dataToNat d1.bv)).run σ = ({(e1, σ)}, False) := he1 + cases r1 with + | Err e => + cases e1 with + | ok _ => exact hrel1.elim + | error e' => + simp only [core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame, + core.convert.FromSame.from, bind_ok, bind_tc_ok] + rw [WP.spec_ok] + exact ⟨rfl, e', run_bind_errorA hls _, hrel1⟩ + | Ok src => + cases e1 with + | error _ => exact hrel1.elim + | ok sl => + obtain ⟨rfl, ps, hps1, hps, -⟩ := hrel1 + refine InvPostA.congr (run_bind_okA hls _) ?_ + have hAs : AbsSlotA Z.th sl := ⟨ps, hps1, Or.inl hps⟩ + have hsO : cslotAddrA sl ∈ OS := hsub _ (hAs.isSlot hP) + obtain ⟨srcA, hsca, hgs⟩ := hR.slot_capA hps1 hps + replace hsca : CapRelA (capA st s (cslotAddrA sl)) srcA := hsca + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok, capA_eq] + refine InvPostA.congr (run_bind_okA (run_liftEA hgs) _) ?_ + by_cases hsn : capA st s (cslotAddrA sl) = .Null + · rw [hsn, cap_is_null_nullA] + have : srcA = .nullCap := by + have h := hsca.2; rw [hsn] at h; exact h.symm + simp only [bind_ok, ↓reduceIte, cnode.missing_source] + rw [WP.spec_ok] + refine ⟨rfl, .failedLookup true (.missingCapability d1.val), ?_, ⟨rfl, rfl⟩⟩ + simp only [this, Fl4Spec.NonDetE.whenE, ↓reduceIte, Fl4Spec.NonDetE.throwError, ndE_throw_bindA]; rfl + have hsan : srcA ≠ .nullCap := hsca.ne_null hsn + rw [cap_is_null_neA hsn] + simp only [bind_ok, Bool.false_eq_true, ↓reduceIte] + refine InvPostA.congr (show _ = (copyRestA slbl (rest.map UScalar.bv) sl dsl srcA).run σ by + simp only [hsan, Fl4Spec.NonDetE.whenE, ↓reduceIte, ndE_pure_bindA]) ?_ + -- the new cap + have hlen' : length.val = rest.length + 4 := by simp at hlen; omega + have hargs' : ∀ (i : U64) x, rest[i.val - 4]? = some x → 4 ≤ i.val → + msg.syscall_arg st s (L.curW s) buf i = ok x := by + intro i x hx hi + apply hargs i x + have : i.val = (i.val - 4) + 4 := by omega + rw [this]; simpa using hx + have hnc0 : isUtA (capA st s (cslotAddrA sl)) = true → + NoChildrenRelA (st := st) s σ (cslotAddrA sl) sl := by + intro hu + obtain ⟨p, b, i, e, hcu⟩ := isUtA_ut hu + exact noChildren_of_adjA hP E.sys.ent.os hT E.j.1.cdt hAs hsO hcu (E.sys.sysl.sys.stz.1.2.1 _ hsO hu) + obtain ⟨r2, hr2, R, hR2, hrel2⟩ := copied_relA s σ lbl slbl hlbl hl length buf (L.curW s) hct rest hlen' + hargs' sl dsl _ hsca.1 (CapRt.decodeF_encWf _ _) hnc0 + have hR2' : ∃ R', (copyRestA slbl (rest.map UScalar.bv) sl dsl srcA).run σ = ({(R', σ)}, False) ∧ + (∀ e, R = .error e → R' = .error e) ∧ + (∀ nc mv, r2 = .Ok (nc, mv) → R = finishResA mv sl dsl (.ok (absCapA nc)) → + ∃ anc, CapEqA nc anc ∧ R' = finishResA mv sl dsl (.ok anc) ∧ + (nc ≠ .Null → anc ≠ .nullCap) ∧ (nc = .Null → anc = .nullCap)) := by + have h := hsca.2 + cases hc' : capA st s (cslotAddrA sl) with + | PageTable p m asid va => + rw [hc'] at h + cases m + · rw [hc', h] at hR2 + refine ⟨R, hR2, fun _ he => he, fun nc mv hr2eq hR => ⟨absCapA nc, CapEqA.of_abs rfl, hR, + fun hnn => by + rw [hr2eq] at hrel2 + exact fun e => hnn ((absA_null_iff hrel2.2.1).1 e), + fun hnn => by subst hnn; rfl⟩⟩ + · obtain ⟨asid', va', rfl, has, hva⟩ := CapEqA.pt_mapped_iff.mp h + have hgt4 : IsCopyLabelA (Fl4SpecA64.genInvocationType slbl) := by + have h4 : lbl.val = 24 ∨ lbl.val = 25 ∨ lbl.val = 26 ∨ lbl.val = 27 := by omega + rcases h4 with hv | hv | hv | hv + · exact Or.inl (by simp [hlbl, cnodeLabelOfA, hv]) + · exact Or.inr (Or.inl (by simp [hlbl, cnodeLabelOfA, hv])) + · exact Or.inr (Or.inr (Or.inl (by simp [hlbl, cnodeLabelOfA, hv]))) + · exact Or.inr (Or.inr (Or.inr (by simp [hlbl, cnodeLabelOfA, hv]))) + rw [hc', show absCapA (.PageTable p true asid va) = + Fl4SpecA64.Cap.archObjectCap (.pageTableCap (specPtr p) .normalPtT (some (asid', va.bv))) from by + simp [absCapA, mapDataA, has]] at hR2 + cases r2 with + | Err e => + cases R with + | ok _ => exact hrel2.elim + | error e' => + exact ⟨.error e', copyRestA_pt_err slbl (rest.map UScalar.bv) sl dsl σ (specPtr p) asid' va.bv va' hgt4 e' hR2, + fun _ he => by cases he; rfl, fun _ _ h0 => by cases h0⟩ + | Ok v => + obtain ⟨nc, mv⟩ := v + obtain ⟨hReq, hw', _, _, hsh', _, _⟩ := hrel2 + rw [hReq] at hR2 + obtain ⟨habsNc, hok'⟩ := copyRestA_pt_ok slbl (rest.map UScalar.bv) sl dsl σ (specPtr p) asid' va.bv va' hgt4 mv (absCapA nc) hR2 + have hncEq : CapEqA nc (.archObjectCap (.pageTableCap (specPtr p) .normalPtT (some (asid', va')))) := by + cases nc with + | PageTable p' m' asid'' va'' => + cases m' + · simp [absCapA, mapDataA] at habsNc + · simp only [absCapA, mapDataA, ↓reduceIte, Fl4SpecA64.Cap.archObjectCap.injEq, + Fl4SpecA64.ArchCap.pageTableCap.injEq, Option.some.injEq, Prod.mk.injEq, true_and] at habsNc + exact ⟨habsNc.1, habsNc.2.1, by rw [habsNc.2.2]; exact hva⟩ + | Frame _ _ _ _ _ ept _ _ => cases ept <;> simp [absCapA, absFrameE] at habsNc + | _ => simp [absCapA] at habsNc + refine ⟨finishResA mv sl dsl (.ok (.archObjectCap (.pageTableCap (specPtr p) .normalPtT (some (asid', va'))))), + hok', fun _ he => by rw [hReq, habsNc] at he; cases mv <;> simp [finishResA] at he, + fun nc' mv' hv _ => ?_⟩ + cases hv + exact ⟨.archObjectCap (.pageTableCap (specPtr p) .normalPtT (some (asid', va'))), + hncEq, rfl, fun _ => by simp, fun h0 => by subst h0; simp [absCapA] at habsNc⟩ + | _ => + rw [hc'] at h; rw [hc', h] at hR2 + refine ⟨R, hR2, fun _ he => he, fun nc mv hr2eq hR => ⟨absCapA nc, CapEqA.of_abs rfl, hR, + fun hnn => by + rw [hr2eq] at hrel2 + exact fun e => hnn ((absA_null_iff hrel2.2.1).1 e), + fun hnn => by subst hnn; rfl⟩⟩ + obtain ⟨R', hR2', hRerr, hRok⟩ := hR2' + refine InvPostA.congr (hR2'.trans (run_ofExceptA R' σ).symm) ?_ + rw [hr2]; simp only [bind_ok, core.result.Result.Insts.CoreOpsTry.branch] + cases r2 with + | Err e => + cases R with + | ok _ => exact hrel2.elim + | error e' => + have hRe' := hRerr e' rfl + subst hRe' + simp only [core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame, core.convert.FromSame.from, bind_ok, bind_tc_ok] + rw [WP.spec_ok] + exact ⟨rfl, e', run_ofExceptA _ σ, hrel2⟩ + | Ok v => + obtain ⟨nc, mv⟩ := v + obtain ⟨hReq, hw', he', hcv', hsh', hut', hok'⟩ := hrel2 + obtain ⟨anc, hanc, hR'eq, hannOf, hnullOf⟩ := hRok nc mv rfl hReq + subst hR'eq + simp only [bind_ok, uncurry_apply_pair] + by_cases hnn : nc = .Null + · subst hnn + rw [cap_is_null_nullA] + simp only [bind_ok, ↓reduceIte, error.illegal] + rw [WP.spec_ok] + exact ⟨rfl, .illegalOperation, by rw [run_ofExceptA, hnullOf rfl]; simp [finishResA], trivial⟩ + have hann : anc ≠ .nullCap := hannOf hnn + rw [cap_is_null_neA hnn] + simp only [bind_ok, Bool.false_eq_true, ↓reduceIte] + -- the caller's restart + obtain ⟨x, -, heq, hset, E2, -, hOK, -, hSI⟩ := E.restartA P Y hrun hcur hvs hvc + rw [heq]; simp only [bind_ok] + have hAd : AbsSlotA Z.th dsl := ⟨pd, hpd1, Or.inl hpd⟩ + have hdO : cslotAddrA dsl ∈ OS := hsub _ (hAd.isSlot hP) + have hsS : IsSlotA Z.th (cslotAddrA sl) := hAs.isSlot hP + have hT2 := hT.keepO hOK + have hnul2 : ∀ y ∈ OS, ¬ IsSlotA Z.th y → + capA st (L.wr s (stA (L.curW s)) sched.TS_RESTART) y = .Null := + fun y hy hn => by rw [hOK.capA hy]; exact hnul y hy hn + have hsc2 : capA st (L.wr s (stA (L.curW s)) sched.TS_RESTART) (cslotAddrA sl) = + capA st s (cslotAddrA sl) := hOK.capA hsO + have hdc2 : capA st (L.wr s (stA (L.curW s)) sched.TS_RESTART) (cslotAddrA dsl) = + capA st s (cslotAddrA dsl) := hOK.capA hdO + have hsn2 : capAtA (L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART)) (cslotAddrA sl) ≠ .Null := by + rw [capAtA_eq, hsc2]; exact hsn + have hdn2 : capAtA (L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART)) (cslotAddrA dsl) = .Null := by + rw [capAtA_eq, hdc2]; exact hdn + have hcan : obj.sext48 (cslotAddrA dsl) = ok (cslotAddrA dsl) := OSOK.canon E.sys.ent.os P.win hdO + have hsh2 : shapeA nc = shapeA (capA st (L.wr s (stA (L.curW s)) sched.TS_RESTART) (cslotAddrA sl)) := by + rw [hsc2]; exact hsh' hnn + have hal : UA nc (capA st (L.wr s (stA (L.curW s)) sched.TS_RESTART) (cslotAddrA sl)) := by + rw [hsc2]; exact UA.of_shapeA (hsh' hnn) (fun hu => ut_of_absA (hsh' hnn) hu (hut' hu hnn)) + have hdo : ¬ IrqNodeA (L.ksb (L.wr s (stA (L.curW s)) sched.TS_RESTART)) (fl4Ptr dsl.1) ∨ + IrqCapA (absCapA nc) := by + left; rw [L.ksb_wr, hpd1, fl4Ptr_specPtr]; exact hpdn + cases mv + · -- insert + have hok := hok' rfl hnn + simp only [Bool.false_eq_true, ↓reduceIte] + have hins : cdt.insert st (L.wr s (stA (L.curW s)) sched.TS_RESTART) nc (cslotAddrA sl) (cslotAddrA dsl) = + cdt.insert_with_cap st (L.wr s (stA (L.curW s)) sched.TS_RESTART) nc (capA st s (cslotAddrA sl)) + (cslotAddrA sl) (cslotAddrA dsl) := by + unfold cdt.insert; rw [capA_eq, hsc2]; simp only [bind_ok] + rw [← hins] + obtain ⟨s3, hs3, σ', hm, hJ, hT', hI⟩ := spec_existsA (cap_insert_dA E2 hT2 hsub hnul2 hAs hAd hsn2 hdn2 + hcan hw' hanc he' hnn hal (by rw [hsc2]; exact sbk_of_shapeA (hsh' hnn)) + (fun hu y hy => by + rw [hsc2] at hu + obtain ⟨p, b, i, e, hcu⟩ := isUtA_ut hu + exact (hok.2.2 e (specPtr p) b.val i.val (by rw [hcu]; rfl)).2 + ((E.j.1.cdt.hasChildA hP hAs).2 (has_child_desc.2 ⟨y, hy⟩))) + hsh2 (by rw [hsc2]; intro p t n e; exact hok.1 (specPtr p) (zombieBitsA t) n.val (by rw [e]; rfl)) + (by rw [hsc2]; exact hok.2.1) hdo (fun h => hcv' Z.th (by rw [← hsc2]; exact h))) + rw [hs3]; simp only [bind_ok] + rw [WP.spec_ok] + refine ⟨.insertCall anc sl dsl, ?_, _, hset, σ', _, liftE_memA hm, hJ, hT', + fun n h => hI n (hSI n h)⟩ + rw [run_ofExceptA]; simp [finishResA, hann] + · -- move + simp only [↓reduceIte] + have hoff : NodeOffA (L.ksb (L.wr s (stA (L.curW s)) sched.TS_RESTART)) nc := + (NodeOffA.of_ctOff (E2.sys.sysl.ct _ (hsub _ hsS))).of_shapeA hsh2 + obtain ⟨s3, hs3, σ', hm, hJ, hT'⟩ := spec_existsA (cap_move_jA E2 hT2 hsub hnul2 hAs hAd hsn2 hdn2 + hcan ⟨hw', hanc⟩ he' hnn hal ((E2.j.capObj hsS).of_shapeA hsh2 + fun h => hcv' Z.th (by rw [← hsc2]; exact h))) + rw [hs3]; simp only [bind_ok] + rw [WP.spec_ok] + have hokAnc : CapOkA σ.interruptIrqNode dsl anc := by + have h0 := capOk_insA E2.j.1.irq hdo hoff + cases nc with + | PageTable p m asid va => + cases m + · rw [← hanc]; exact h0 + · obtain ⟨_, _, rfl, _, _⟩ := CapEqA.pt_mapped_iff.mp hanc + refine ⟨fun irq hirq hsl => ?_, fun ⟨_, _, _, _, e⟩ => by cases e⟩ + have hi := h0.1 irq hirq hsl + simp [IrqCapA, absCapA] at hi + | _ => rw [← hanc]; exact h0 + refine ⟨.moveCall anc sl dsl, ?_, _, hset, σ', _, liftE_memA hm, hJ, hT', + fun n h => (hSI n h).capMove hokAnc hm⟩ + rw [run_ofExceptA]; simp [finishResA, hann] + + +/-- **A CNode reached from a valid root is off FL4's IRQ-node slots** (gen 11, `CnodeRotA`; gen 12: here, also for `cnode_copy_refinesA`'s destination). -/ +theorem noIrq_of_cnRefA {th : TypedHeapA} {kb : U64} {mem : U64 → U64} (hP : PlaceA th kb) + (hV : SlotsValidA th mem) {c0 : capability.Cap} (hcv : CapValidA th c0) {p : U64} (h : CnRefA th mem c0 p) : + ¬ IrqNodeA kb p := by + intro hn + obtain ⟨r, gs, g, hrc⟩ := h + have hv : CapValidA th (.CNode p r gs g) := by + rcases hrc with ⟨q, r', idx, hq, hl', he⟩ | he + · rw [← he] + exact hV _ (AbsSlotA.isSlot hP ⟨q, rfl, Or.inl (by rw [hl']; exact hq)⟩) + · rw [he]; exact hcv + obtain ⟨h1, h2⟩ := hv + have := hP.irqK h1 hn + injection this with e + exact h2 e + +/-- **FL4's CNode Copy, Mint, Move and Mutate refine l4v's** (x86 `cnode_copy_refines`): for labels 24..27, +`cnode::decode` ends with the error `decode_cnode_invocation` throws (state unchanged), or, when the spec decodes an +invocation, in a state related to the spec's after `set_thread_state Restart` and `invoke_cnode`. -/ +theorem cnode_copy_refinesA (arI : machine.Arch A) (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + {Rp Nt : Set U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} {slot : U64} + (P : SysPlace st H Z.Cs H.frame Rp Nt OS s extra slot) (Y : SymL st H Z.Cs s) + (hrun : Runnable (L.mem s (stA (L.curW s)))) + (hcur : (L.mem s (inqA (L.curW s)) &&& sched.Q_RELEASE) = 0#u64 ∧ L.mem s (scA (L.curW s)) ≠ 0#u64 ∧ + L.mem s (scA (L.curW s)) ∈ Ac) + (hvs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → Aligned b ∧ b ≠ stA (L.curW s)) + (hvc : ∀ v ∈ Z.V, stA (L.curW s) ≠ v ∧ stA (L.curW s) ≠ wa v 24#u64) + (hT : TreeA st OS Z.G.chains Z.G.par s) (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) + (lbl : U64) (slbl : Fl4SpecA64.Data) (hlbl : Fl4SpecA64.genInvocationType slbl = cnodeLabelOfA lbl) + (hl : 24 ≤ lbl.val ∧ lbl.val ≤ 27) (length buf : U64) (args : List U64) (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) + (cap : capability.Cap) (ac : Fl4SpecA64.Cap) (hca : CapRelA cap ac) (hcv : CapValidA Z.th cap) + (x0 : U64) (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) + (excaps : List Fl4SpecA64.Cap) (hex : ExtraRelA Z.th (L.mem s) x0 excaps) : + cnode.decode st arI s a lbl length cap buf extra ⦃ res => + InvPostA (L := L) AR Z OS σ m (Fl4SpecA64.decodeCnodeInvocation slbl (args.map UScalar.bv) ac excaps) s a + (res.1, res.2.1) ∧ res.2.2 = a ⦄ := by + have hgl : IsCopyLabelA (Fl4SpecA64.genInvocationType slbl) := by + rw [hlbl]; unfold cnodeLabelOfA IsCopyLabelA + have : lbl.val = 24 ∨ lbl.val = 25 ∨ lbl.val = 26 ∨ lbl.val = 27 := by omega + rcases this with h | h | h | h <;> simp [h] + unfold cnode.decode + have n1 : ¬ lbl < fl4_abi.label.CNodeRevoke := by have := lblA_revoke; scalar_tac + have n2 : ¬ lbl > fl4_abi.label.CNodeRotate := by have := lblA_rotate; scalar_tac + rw [if_neg n1, if_neg n2] + by_cases h2 : length < 2#u64 + · rw [if_pos h2] + simp only [error.truncated, bind_ok] + rw [WP.spec_ok] + exact ⟨⟨rfl, .truncatedMessage, decode_shortA _ _ _ σ hgl.facts.1 _ (by simp; scalar_tac), trivial⟩, rfl⟩ + rw [if_neg h2] + obtain ⟨y0, y1, rest, rfl⟩ : ∃ y0 y1 rest, args = y0 :: y1 :: rest := by + match args with + | [] => exfalso; simp at hlen; scalar_tac + | [_] => exfalso; simp at hlen; scalar_tac + | y0 :: y1 :: rest => exact ⟨y0, y1, rest, rfl⟩ + simp only [cur_thread_ok, hargs 0#u64 y0 (by simp), hargs 1#u64 y1 (by simp), bind_ok] + unfold cspace.lookup_target_slot + obtain ⟨r1, hr1, e1, he1, hrel1⟩ := spec_existsA + (lookup_slot_for_cnode_op_refinesA Z.th s σ E.j.1.st E.j.1.cv false cap ac hca hcv y0 y1) + rw [hr1]; simp only [bind_ok, core.result.Result.Insts.CoreOpsTry.branch] + have hlt : (Fl4SpecA64.lookupTargetSlot (Z := Unit) ac (Fl4SpecA64.dataToCptr y0.bv) + (Fl4SpecA64.dataToNat y1.bv)).run σ = ({(e1, σ)}, False) := he1 + have hdec := decode_lookupA slbl ac excaps hgl.facts.1 y0.bv y1.bv (rest.map UScalar.bv) + simp only [List.map_cons] + rw [hdec] + cases r1 with + | Err e => + cases e1 with + | ok _ => exact hrel1.elim + | error e' => + simp only [core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame, core.convert.FromSame.from, bind_ok, bind_tc_ok] + rw [WP.spec_ok] + exact ⟨⟨rfl, e', run_bind_errorA hlt _, hrel1⟩, rfl⟩ + | Ok dest => + cases e1 with + | error _ => exact hrel1.elim + | ok dsl => + obtain ⟨rfl, pd, hpd1, hpd, hcrd⟩ := hrel1 + have g1 : lbl ≥ fl4_abi.label.CNodeCopy := by have := lblA_copy; scalar_tac + have g2 : lbl ≤ fl4_abi.label.CNodeMutate := by have := lblA_mutate; scalar_tac + simp only [g1, g2, ↓reduceIte, bind_ok] + have hpdn : ¬ IrqNodeA (L.ksb s) pd := noIrq_of_cnRefA E.j.1.place E.j.1.cv hcv hcrd + obtain ⟨⟨r2, s2⟩, hdc, hpost⟩ := spec_existsA + (decode_copy_relA E P Y hrun hcur hvs hvc hT hsub hnul lbl slbl hlbl hl length buf (y0 :: y1 :: rest) hlen + (by simp) hargs x0 hx0 excaps hex dsl pd hpd1 hpd hpdn) + rw [hdc]; simp only [bind_ok, uncurry_apply_pair] + rw [WP.spec_ok] + simp only [List.map_cons, List.drop_succ_cons, List.drop_zero] at hpost + exact ⟨InvPostA.of_run (run_bind_okA hlt _) hpost, rfl⟩ + +/-- The labels outside `CNodeRevoke ..= CNodeRotate` are the spec's `IllegalOperation` (x86 `decode_illegal`'s +use in `cnode_decode_refines`). -/ +theorem cnode_illegal_refinesA (arI : machine.Arch A) (lbl : U64) (slbl : Fl4SpecA64.Data) + (hlbl : Fl4SpecA64.genInvocationType slbl = cnodeLabelOfA lbl) (hli : lbl.val < 21 ∨ 28 < lbl.val) + (length buf : U64) (args : List Fl4SpecA64.Data) (cap : capability.Cap) (ac : Fl4SpecA64.Cap) + (extra : Aeneas.Std.Array obj.Slot 3#usize) (excaps : List Fl4SpecA64.Cap) : + cnode.decode st arI s a lbl length cap buf extra ⦃ res => + InvPostA (L := L) AR Z OS σ m (Fl4SpecA64.decodeCnodeInvocation slbl args ac excaps) s a + (res.1, res.2.1) ∧ res.2.2 = a ⦄ := by + have hinv : cnodeLabelOfA lbl = .invalidInvocation := by + unfold cnodeLabelOfA; split_ifs <;> first | rfl | omega + have h1 : Fl4SpecA64.genInvocationType slbl ∉ + Fl4SpecA64.GenInvocationLabels.uptoEnum .cnodeRevoke .cnodeRotate := by + rw [hlbl, hinv]; decide + have hd := decode_illegalA slbl args ac excaps σ h1 + unfold cnode.decode + rcases hli with h | h + · have n1 : lbl < fl4_abi.label.CNodeRevoke := by have := lblA_revoke; scalar_tac + rw [if_pos n1] + simp only [error.illegal, bind_ok] + rw [WP.spec_ok] + exact ⟨⟨rfl, .illegalOperation, hd, trivial⟩, rfl⟩ + · have n1 : ¬ lbl < fl4_abi.label.CNodeRevoke := by have := lblA_revoke; scalar_tac + have n2 : lbl > fl4_abi.label.CNodeRotate := by have := lblA_rotate; scalar_tac + rw [if_neg n1, if_pos n2] + simp only [error.illegal, bind_ok] + rw [WP.spec_ok] + exact ⟨⟨rfl, .illegalOperation, hd, trivial⟩, rfl⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/CnodeDecDelA.lean b/proofs/Fl4ProofsA64/P3/CnodeDecDelA.lean new file mode 100644 index 000000000..59aaef583 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/CnodeDecDelA.lean @@ -0,0 +1,469 @@ +/- +P3 (AArch64, A64-P3 CNode wave 5): **CNode Revoke and Delete at the decode** — FL4's `cnode::decode` at labels 21 / 22 +against l4v's `decode_cnode_invocation`, then `set_thread_state Restart` and the spec's `fl4_invoke_cnode` +(`fl4CapRevoke` / `fl4CapDelete`), with the deletion engine of waves 1–4 in between (x86 `cnode_revoke_refinesW` / +`cnode_delete_refinesW`, `XTopDelW`'s `EX.restart`). + +* **`InvPostDA`** (Architect F3): the post beside S3's unedited `InvPostA`: the spec's syscall error with the state + and arch state unchanged, or the spec's invocation after `set_thread_state Restart`, whose `fl4_invoke_cnode` + outcome is `Ok` or the preemption's `error ()`, with `DStateA` at the new state and the loop's `WPostA`. +* **`DelSiteA`** ([J], ruling 20:32Z R2 and the Wave-1 premises): the decode-site facts `DStateA` needs beyond S3's + record: the converse object coverage (`FinCovA`), `FrBaseApartA`, `IrqRunA`, and `DelInvA` (typed-slot zombies + `ZOKA`, R2). **`DelSiteA.restartA`** carries them across the caller's restart (`OSKeep`). +* **`DStateA.ofEntA`**: the deletion's related states at an entry record: `JEntA`'s relation, placement and CDT + relation, A64 P1's MDB part of `SysInvA` (`MdbST`, `ZombiesFinal`) on the decode-site tree `hT`, the slot set facts. +* **`cnode_revoke_refinesWA`**, **`cnode_delete_refinesWA`** (and `decDel_restartWA`): the hooks at any loop + invariant `W`, `hXr` giving `W` at the restarted state from its `JAtZAX`; **`cnode_revoke_refinesDA`**, + **`cnode_delete_refinesDA`** are their instances at `W = DelJWA ... X`. + +Named premises: `DelSiteA` ([J]); `hCan` (P1 `DCan OS`); `DelHypsA`'s hooks at `DelJWA ... X`; `hPA` (`ParentAdjA` +from `W`, R1); `hXr` (`X` across the caller's restart, a `DelJWA` `X` obligation; Stage-3 ledger). New module. +-/ +import Fl4ProofsA64.P3.CnodeRevokeA +import Fl4ProofsA64.P3.CnodeDelWA +import Fl4ProofsA64.P3.CnodeCopyA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +@[simp] theorem lblA_delete : fl4_abi.label.CNodeDelete.val = 22 := by simp [fl4_abi.label.CNodeDelete] +@[simp] theorem lblA_cancel : fl4_abi.label.CNodeCancelBadgedSends.val = 23 := by + simp [fl4_abi.label.CNodeCancelBadgedSends] + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-! ## The outcome -/ + +variable (st) in +/-- **The outcome of a Revoke / Delete** against a spec decode `msp` (module docstring; x86 `PInvPostW`). -/ +def InvPostDA (AR : A → HwRootA → Prop) (Z : XSetsA) (OS : Set U64) (σ : Fl4SpecA64.State Unit) + (msp : Fl4SpecA64.SEMonad Unit Fl4SpecA64.CnodeInvocation) (s : S) (a : A) (W Wp : DelWA S A) : + core.result.Result Unit error.Exc × S × A → Prop + | (r, s', a') => + (∃ e, r = .Err e ∧ s' = s ∧ a' = a ∧ ∃ e', msp.run σ = ({(.error e', σ)}, False) ∧ SysErrRelA e e') ∨ + ∃ inv, msp.run σ = ({(.ok inv, σ)}, False) ∧ ∃ σ1, + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ ∃ σ' G', + ((r = .Ok () ∧ (.ok (), σ') ∈ ((Fl4SpecA64.fl4InvokeCnode (Z := Unit) inv).run σ1).1) ∨ + (r = .Err .Preempted ∧ (.error (), σ') ∈ ((Fl4SpecA64.fl4InvokeCnode (Z := Unit) inv).run σ1).1)) ∧ + DStateA st Z.th OS s' σ' G' ∧ WPostA W Wp r s' a' σ' G' + +/-! ## The decode-site facts -/ + +/-- **The deletion's decode-site facts** ([J], module docstring). -/ +structure DelSiteA (st : machine.Store S) [L : LawfulStore st] (th : TypedHeapA) (OS : Set U64) (s : S) + (σ : Fl4SpecA64.State Unit) (G : List (List U64)) : Prop where + /-- [J] converse object coverage -/ + cov : FinCovA th σ + /-- [J] -/ + fr : FrBaseApartA st OS s + /-- [J] -/ + irq : IrqRunA st G s + /-- [J] R2: every typed-slot zombie is well formed -/ + zw : DelInvA st th s + +theorem FinCovA.stUpdA {th : TypedHeapA} {σ : Fl4SpecA64.State Unit} {t : Fl4SpecA64.ObjRef} {x : Fl4SpecA64.Tcb} + {ts : Fl4SpecA64.ThreadState} (h : FinCovA th σ) (hk : σ.kheap t = some (.tcb x)) : + FinCovA th (stUpdA σ t x ts) := by + refine ⟨fun q r cs hk1 hw => ?_, fun q x' hk1 => ?_⟩ + · exact h.cnode q r cs (stUpd_back (o := .cnode r cs) hk1 (fun y h => by cases h)) hw + · by_cases e : q = t + · subst e; exact h.tcb q x hk + · exact h.tcb q x' (by simpa [setObjA, Function.update_of_ne e] using hk1) + +variable {th : TypedHeapA} {OS : Set U64} {s s1 : S} {σ σ1 : Fl4SpecA64.State Unit} {G : MdbGhostA} + +/-- **The decode-site facts across the caller's restart** (module docstring). -/ +theorem DelSiteA.restartA (h : DelSiteA st th OS s σ G.chains) (hT : TreeA st OS G.chains G.par s) + (hsub : ∀ y, IsSlotA th y → y ∈ OS) (hOK : OSKeep st OS s s1) (hcov : FinCovA th σ1) : + DelSiteA st th OS s1 σ1 G.chains := by + refine ⟨hcov, ?_, ?_, ?_⟩ + · refine FrBaseApartA.of h.fr (fun y hy hf => ⟨y, hy, ?_, ?_⟩) (fun z hz hk => ⟨z, hz, ?_, ?_⟩) + · rwa [hOK.capA hy] at hf + · rw [hOK.capA hy] + · rwa [hOK.capA hz] at hk + · rw [hOK.capA hz] + · exact h.irq.keepIrq fun y hy i => by rw [hOK.capA ((hT.lk.members y).1 hy).1] + · intro y hy p zt n hc + rw [hOK.capA (hsub y hy)] at hc + exact h.zw y hy p zt n hc + +/-! ## The deletion's related states at an entry record -/ + +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {a : A} {m : Fl4SpecA64.McState} + +/-- **`DStateA` at an entry record** (module docstring). -/ +theorem DStateA.ofEntA (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + (hT : TreeA st OS Z.G.chains Z.G.par s) (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) (site : DelSiteA st Z.th OS s σ Z.G.chains) : + DStateA st Z.th OS s σ Z.G := by + obtain ⟨G', par', lk', rng', kind', nl', cls', -, -⟩ := E.sys.sysl.sys.stz.1.1 + exact + { rel := E.j.1.st + place := E.j.1.place + cov := site.cov + lk := hT.lk + rng := rng'.congr (lk'.chains_iff hT.lk E.sys.ent.os.nz) + kind := kind' + cls := cls' + zf := E.sys.sysl.sys.stz.2.1 + nz := E.sys.ent.os.nz + sub := hsub + nul := hnul + fr := site.fr + irq := site.irq + cdt := E.j.1.cdt + tree := hT + nl := nl' + zw := site.zw + cv := fun y hy => by + by_cases hs : IsSlotA Z.th y + · exact (E.j.capObj hs).2.2.2.2 + · rw [hnul y hy hs]; exact trivial } + +/-! ## Revoke and Delete -/ + +variable {Rp Nt : Set U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} {slot : U64} + +/-- The caller's restart at the decode site, as the deletion loops need it: the post-restart state and ghost with +`DStateA`, `W` (given from the restart's `JAtZAX` by `hXr`), and the spec's restart outcome (shared by Revoke and +Delete). -/ +theorem decDel_restartWA (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + (P : SysPlace st H Z.Cs H.frame Rp Nt OS s extra slot) (Y : SymL st H Z.Cs s) + (hrun : Runnable (L.mem s (stA (L.curW s)))) + (hcur : (L.mem s (inqA (L.curW s)) &&& sched.Q_RELEASE) = 0#u64 ∧ L.mem s (scA (L.curW s)) ≠ 0#u64 ∧ + L.mem s (scA (L.curW s)) ∈ Ac) + (hvs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → Aligned b ∧ b ≠ stA (L.curW s)) + (hvc : ∀ v ∈ Z.V, stA (L.curW s) ≠ v ∧ stA (L.curW s) ≠ wa v 24#u64) + (hT : TreeA st OS Z.G.chains Z.G.par s) (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) (site : DelSiteA st Z.th OS s σ Z.G.chains) + {W : DelWA S A} + (hXr : ∀ x, σ.kheap σ.curThread = some (.tcb x) → + (∃ m', JAtZAX (L := L) AR { Z with G := Z.G } (L.wr s (stA (L.curW s)) sched.TS_RESTART) a + (stUpdA σ σ.curThread x .restart) m') → + W (L.wr s (stA (L.curW s)) sched.TS_RESTART) a (stUpdA σ σ.curThread x .restart) Z.G) : + ∃ x, sched.set_thread_state st s (L.curW s) sched.TS_RESTART = ok (L.wr s (stA (L.curW s)) sched.TS_RESTART) ∧ + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + DStateA st Z.th OS (L.wr s (stA (L.curW s)) sched.TS_RESTART) (stUpdA σ σ.curThread x .restart) Z.G ∧ + W (L.wr s (stA (L.curW s)) sched.TS_RESTART) a (stUpdA σ σ.curThread x .restart) Z.G := by + obtain ⟨x, hkx, heq, hset, E2, -, hOK, -, -⟩ := E.restartA P Y hrun hcur hvs hvc + have hT2 := hT.keepO hOK + have hnul2 : ∀ y ∈ OS, ¬ IsSlotA Z.th y → + capA st (L.wr s (stA (L.curW s)) sched.TS_RESTART) y = .Null := + fun y hy hn => by rw [hOK.capA hy]; exact hnul y hy hn + refine ⟨x, heq, hset, DStateA.ofEntA E2 hT2 hsub hnul2 + (site.restartA hT hsub hOK (site.cov.stUpdA hkx)), hXr x hkx ⟨m, E2.j⟩⟩ + +/-- `decDel_restartWA` at `W = DelJWA ... X`. -/ +theorem decDel_restartA (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + (P : SysPlace st H Z.Cs H.frame Rp Nt OS s extra slot) (Y : SymL st H Z.Cs s) + (hrun : Runnable (L.mem s (stA (L.curW s)))) + (hcur : (L.mem s (inqA (L.curW s)) &&& sched.Q_RELEASE) = 0#u64 ∧ L.mem s (scA (L.curW s)) ≠ 0#u64 ∧ + L.mem s (scA (L.curW s)) ∈ Ac) + (hvs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → Aligned b ∧ b ≠ stA (L.curW s)) + (hvc : ∀ v ∈ Z.V, stA (L.curW s) ≠ v ∧ stA (L.curW s) ≠ wa v 24#u64) + (hT : TreeA st OS Z.G.chains Z.G.par s) (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) (site : DelSiteA st Z.th OS s σ Z.G.chains) + {X : DelWA S A} + (hXr : ∀ x, σ.kheap σ.curThread = some (.tcb x) → + X (L.wr s (stA (L.curW s)) sched.TS_RESTART) a (stUpdA σ σ.curThread x .restart) Z.G) : + ∃ x, sched.set_thread_state st s (L.curW s) sched.TS_RESTART = ok (L.wr s (stA (L.curW s)) sched.TS_RESTART) ∧ + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + DStateA st Z.th OS (L.wr s (stA (L.curW s)) sched.TS_RESTART) (stUpdA σ σ.curThread x .restart) Z.G ∧ + DelJWA (st := st) AR Z s X (L.wr s (stA (L.curW s)) sched.TS_RESTART) a (stUpdA σ σ.curThread x .restart) Z.G := + decDel_restartWA E P Y hrun hcur hvs hvc hT hsub hnul site fun x hkx hj => + ⟨⟨L.ksb_wr _ _ _, by rw [LawfulStore.cpu_wr]⟩, hj, hXr x hkx⟩ + +theorem dec_revoke_errA {slbl : Fl4SpecA64.Data} {ac : Fl4SpecA64.Cap} {excaps : List Fl4SpecA64.Cap} + {y0 y1 : Fl4SpecA64.Data} {rest : List Fl4SpecA64.Data} {σ : Fl4SpecA64.State Unit} + {e' : Fl4SpecA64.SyscallError} (hg : Fl4SpecA64.genInvocationType slbl = .cnodeRevoke) + (hl : (Fl4SpecA64.lookupTargetSlot (Z := Unit) ac (Fl4SpecA64.dataToCptr y0) (Fl4SpecA64.dataToNat y1)).run σ = + ({(.error e', σ)}, False)) : + (Fl4SpecA64.decodeCnodeInvocation (Z := Unit) slbl (y0 :: y1 :: rest) ac excaps).run σ = + ({(.error e', σ)}, False) := by + rw [decode_revokeA slbl ac excaps y0 y1 rest hg] + exact run_bind_errorA hl _ + +theorem dec_revoke_okA {slbl : Fl4SpecA64.Data} {ac : Fl4SpecA64.Cap} {excaps : List Fl4SpecA64.Cap} + {y0 y1 : Fl4SpecA64.Data} {rest : List Fl4SpecA64.Data} {σ : Fl4SpecA64.State Unit} + {d : Fl4SpecA64.CslotPtr} (hg : Fl4SpecA64.genInvocationType slbl = .cnodeRevoke) + (hl : (Fl4SpecA64.lookupTargetSlot (Z := Unit) ac (Fl4SpecA64.dataToCptr y0) (Fl4SpecA64.dataToNat y1)).run σ = + ({(.ok d, σ)}, False)) : + (Fl4SpecA64.decodeCnodeInvocation (Z := Unit) slbl (y0 :: y1 :: rest) ac excaps).run σ = + ({(.ok (.revokeCall d), σ)}, False) := by + rw [decode_revokeA slbl ac excaps y0 y1 rest hg, run_bind_okA hl] + rfl + +theorem dec_delete_errA {slbl : Fl4SpecA64.Data} {ac : Fl4SpecA64.Cap} {excaps : List Fl4SpecA64.Cap} + {y0 y1 : Fl4SpecA64.Data} {rest : List Fl4SpecA64.Data} {σ : Fl4SpecA64.State Unit} + {e' : Fl4SpecA64.SyscallError} (hg : Fl4SpecA64.genInvocationType slbl = .cnodeDelete) + (hl : (Fl4SpecA64.lookupTargetSlot (Z := Unit) ac (Fl4SpecA64.dataToCptr y0) (Fl4SpecA64.dataToNat y1)).run σ = + ({(.error e', σ)}, False)) : + (Fl4SpecA64.decodeCnodeInvocation (Z := Unit) slbl (y0 :: y1 :: rest) ac excaps).run σ = + ({(.error e', σ)}, False) := by + rw [decode_deleteA slbl ac excaps y0 y1 rest hg] + exact run_bind_errorA hl _ + +theorem dec_delete_okA {slbl : Fl4SpecA64.Data} {ac : Fl4SpecA64.Cap} {excaps : List Fl4SpecA64.Cap} + {y0 y1 : Fl4SpecA64.Data} {rest : List Fl4SpecA64.Data} {σ : Fl4SpecA64.State Unit} + {d : Fl4SpecA64.CslotPtr} (hg : Fl4SpecA64.genInvocationType slbl = .cnodeDelete) + (hl : (Fl4SpecA64.lookupTargetSlot (Z := Unit) ac (Fl4SpecA64.dataToCptr y0) (Fl4SpecA64.dataToNat y1)).run σ = + ({(.ok d, σ)}, False)) : + (Fl4SpecA64.decodeCnodeInvocation (Z := Unit) slbl (y0 :: y1 :: rest) ac excaps).run σ = + ({(.ok (.deleteCall d), σ)}, False) := by + rw [decode_deleteA slbl ac excaps y0 y1 rest hg, run_bind_okA hl] + rfl + +set_option maxHeartbeats 4000000 in +/-- **CNode Revoke**, decode and invoke (x86 `cnode_revoke_refinesW`; module docstring): FL4's `cnode::decode` at label +21 against l4v's `decode_cnode_invocation`, then `set_thread_state Restart` and `fl4_invoke_cnode` +(`fl4CapRevoke`), over the deletion's hooks `Hk` at any `W`. -/ +theorem cnode_revoke_refinesWA (arI : machine.Arch A) (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + (P : SysPlace st H Z.Cs H.frame Rp Nt OS s extra slot) (Y : SymL st H Z.Cs s) + (hrun : Runnable (L.mem s (stA (L.curW s)))) + (hcur : (L.mem s (inqA (L.curW s)) &&& sched.Q_RELEASE) = 0#u64 ∧ L.mem s (scA (L.curW s)) ≠ 0#u64 ∧ + L.mem s (scA (L.curW s)) ∈ Ac) + (hvs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → Aligned b ∧ b ≠ stA (L.curW s)) + (hvc : ∀ v ∈ Z.V, stA (L.curW s) ≠ v ∧ stA (L.curW s) ≠ wa v 24#u64) + (hT : TreeA st OS Z.G.chains Z.G.par s) (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) (site : DelSiteA st Z.th OS s σ Z.G.chains) + (hCan : ∀ x ∈ OS, obj.sext48 x = ok x) {W Wx Wp : DelWA S A} + {Rd Rz : S → A → Fl4SpecA64.State Unit → MdbGhostA → U64 → Prop} + (Hk : DelHypsA st arI H.tcbs Z.th OS Fl4SpecA64.fl4FinaliseCap W + Wx Wp Rd Rz) + (hPA : ∀ s' a' σ' G', DStateA st Z.th OS s' σ' G' → W s' a' σ' G' → + ParentAdjA st G'.chains G'.par s') + (hXr : ∀ x, σ.kheap σ.curThread = some (.tcb x) → + (∃ m', JAtZAX (L := L) AR { Z with G := Z.G } (L.wr s (stA (L.curW s)) sched.TS_RESTART) a + (stUpdA σ σ.curThread x .restart) m') → + W (L.wr s (stA (L.curW s)) sched.TS_RESTART) a (stUpdA σ σ.curThread x .restart) Z.G) + (lbl : U64) (slbl : Fl4SpecA64.Data) (hlbl : Fl4SpecA64.genInvocationType slbl = cnodeLabelOfA lbl) + (hl : lbl.val = 21) (length buf : U64) (args : List U64) (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) + (cap : capability.Cap) (ac : Fl4SpecA64.Cap) (hca : CapRelA cap ac) (hcv : CapValidA Z.th cap) + (excaps : List Fl4SpecA64.Cap) : + cnode.decode st arI s a lbl length cap buf extra ⦃ res => + InvPostDA st AR Z OS σ (Fl4SpecA64.decodeCnodeInvocation slbl (args.map UScalar.bv) ac excaps) s a + W Wp res ⦄ := by + have hg : Fl4SpecA64.genInvocationType slbl = .cnodeRevoke := by rw [hlbl]; simp [cnodeLabelOfA, hl] + have hin : Fl4SpecA64.genInvocationType slbl ∈ + Fl4SpecA64.GenInvocationLabels.uptoEnum .cnodeRevoke .cnodeRotate := by rw [hg]; decide + obtain rfl : lbl = fl4_abi.label.CNodeRevoke := by rw [u64_eq_iffA]; simp [hl] + unfold cnode.decode + have n1 : ¬ fl4_abi.label.CNodeRevoke < fl4_abi.label.CNodeRevoke := by + have := lblA_revoke; have := lblA_revoke; scalar_tac + have n2 : ¬ fl4_abi.label.CNodeRevoke > fl4_abi.label.CNodeRotate := by + have := lblA_revoke; have := lblA_rotate; scalar_tac + rw [if_neg n1, if_neg n2] + by_cases h2 : length < 2#u64 + · rw [if_pos h2] + simp only [error.truncated, bind_ok] + rw [WP.spec_ok] + exact Or.inl ⟨_, rfl, rfl, rfl, .truncatedMessage, decode_shortA _ _ _ σ hin _ (by simp; scalar_tac), trivial⟩ + rw [if_neg h2] + obtain ⟨y0, y1, rest, rfl⟩ : ∃ y0 y1 rest, args = y0 :: y1 :: rest := by + match args with + | [] => exfalso; simp at hlen; scalar_tac + | [_] => exfalso; simp at hlen; scalar_tac + | y0 :: y1 :: rest => exact ⟨y0, y1, rest, rfl⟩ + simp only [cur_thread_ok, hargs 0#u64 y0 (by simp), hargs 1#u64 y1 (by simp), bind_ok] + simp only [List.map_cons] + unfold cspace.lookup_target_slot + obtain ⟨r1, hr1, e1, he1, hrel1⟩ := spec_existsA + (lookup_slot_for_cnode_op_refinesA Z.th s σ E.j.1.st E.j.1.cv false cap ac hca hcv y0 y1) + have hlt : (Fl4SpecA64.lookupTargetSlot (Z := Unit) ac (Fl4SpecA64.dataToCptr y0.bv) + (Fl4SpecA64.dataToNat y1.bv)).run σ = ({(e1, σ)}, False) := he1 + rw [hr1]; simp only [bind_ok, core.result.Result.Insts.CoreOpsTry.branch] + cases r1 with + | Err e => + cases e1 with + | ok _ => exact hrel1.elim + | error e' => + simp only [core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, bind_ok, bind_tc_ok] + rw [WP.spec_ok] + exact Or.inl ⟨_, rfl, rfl, rfl, e', dec_revoke_errA hg hlt, hrel1⟩ + | Ok dest => + cases e1 with + | error _ => exact hrel1.elim + | ok dsl => + obtain ⟨rfl, pd, hpd1, hpd, hcrd⟩ := hrel1 + have g1 : ¬ fl4_abi.label.CNodeRevoke ≥ fl4_abi.label.CNodeCopy := by + have := lblA_copy; have := lblA_revoke; scalar_tac + + simp only [g1, ↓reduceIte, bind_ok] + obtain ⟨x, heq, hset, D1, hW1⟩ := decDel_restartWA E P Y hrun hcur hvs hvc hT hsub hnul site hXr + have hAd : AbsSlotA Z.th dsl := ⟨pd, hpd1, Or.inl hpd⟩ + rw [heq]; simp only [bind_ok] + refine WP.spec_mono (revoke_refinesGA E.sys.ent.os hCan Hk hPA dsl hAd _ a _ Z.G D1 hW1) ?_ + rintro ⟨r, s', a'⟩ ⟨σ', G', D', hres, hW'⟩ + refine Or.inr ⟨.revokeCall dsl, dec_revoke_okA hg hlt, + _, hset, σ', G', ?_, D', hW'⟩ + rw [fl4InvokeCnode_revokeA] + exact hres + +/-- **CNode Revoke**, decode and invoke (x86 `cnode_revoke_refinesW`; module docstring): FL4's `cnode::decode` at label +21 against l4v's `decode_cnode_invocation`, then `set_thread_state Restart` and `fl4_invoke_cnode` +(`fl4CapRevoke`), over the deletion's hooks `Hk` at `DelJWA ... X` +(`cnode_revoke_refinesWA`). -/ +theorem cnode_revoke_refinesDA (arI : machine.Arch A) (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + (P : SysPlace st H Z.Cs H.frame Rp Nt OS s extra slot) (Y : SymL st H Z.Cs s) + (hrun : Runnable (L.mem s (stA (L.curW s)))) + (hcur : (L.mem s (inqA (L.curW s)) &&& sched.Q_RELEASE) = 0#u64 ∧ L.mem s (scA (L.curW s)) ≠ 0#u64 ∧ + L.mem s (scA (L.curW s)) ∈ Ac) + (hvs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → Aligned b ∧ b ≠ stA (L.curW s)) + (hvc : ∀ v ∈ Z.V, stA (L.curW s) ≠ v ∧ stA (L.curW s) ≠ wa v 24#u64) + (hT : TreeA st OS Z.G.chains Z.G.par s) (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) (site : DelSiteA st Z.th OS s σ Z.G.chains) + (hCan : ∀ x ∈ OS, obj.sext48 x = ok x) {X Xx Xp : DelWA S A} + {Rd Rz : S → A → Fl4SpecA64.State Unit → MdbGhostA → U64 → Prop} + (Hk : DelHypsA st arI H.tcbs Z.th OS Fl4SpecA64.fl4FinaliseCap (DelJWA (st := st) AR Z s X) + (DelJWA (st := st) AR Z s Xx) (DelJWA (st := st) AR Z s Xp) Rd Rz) + (hPA : ∀ s' a' σ' G', DStateA st Z.th OS s' σ' G' → DelJWA (st := st) AR Z s X s' a' σ' G' → + ParentAdjA st G'.chains G'.par s') + (hXr : ∀ x, σ.kheap σ.curThread = some (.tcb x) → + X (L.wr s (stA (L.curW s)) sched.TS_RESTART) a (stUpdA σ σ.curThread x .restart) Z.G) + (lbl : U64) (slbl : Fl4SpecA64.Data) (hlbl : Fl4SpecA64.genInvocationType slbl = cnodeLabelOfA lbl) + (hl : lbl.val = 21) (length buf : U64) (args : List U64) (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) + (cap : capability.Cap) (ac : Fl4SpecA64.Cap) (hca : CapRelA cap ac) (hcv : CapValidA Z.th cap) + (excaps : List Fl4SpecA64.Cap) : + cnode.decode st arI s a lbl length cap buf extra ⦃ res => + InvPostDA st AR Z OS σ (Fl4SpecA64.decodeCnodeInvocation slbl (args.map UScalar.bv) ac excaps) s a + (DelJWA (st := st) AR Z s X) (DelJWA (st := st) AR Z s Xp) res ⦄ := + cnode_revoke_refinesWA arI E P Y hrun hcur hvs hvc hT hsub hnul site hCan Hk hPA + (fun x hkx hj => ⟨⟨L.ksb_wr _ _ _, by rw [LawfulStore.cpu_wr]⟩, hj, hXr x hkx⟩) + lbl slbl hlbl hl length buf args hlen hargs cap ac hca hcv excaps + +set_option maxHeartbeats 4000000 in +/-- **CNode Delete**, decode and invoke (x86 `cnode_delete_refinesW`; module docstring): FL4's `cnode::decode` at label +22 against l4v's `decode_cnode_invocation`, then `set_thread_state Restart` and `fl4_invoke_cnode` +(`fl4CapDelete`), over the deletion's hooks `Hk` at any `W`. -/ +theorem cnode_delete_refinesWA (arI : machine.Arch A) (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + (P : SysPlace st H Z.Cs H.frame Rp Nt OS s extra slot) (Y : SymL st H Z.Cs s) + (hrun : Runnable (L.mem s (stA (L.curW s)))) + (hcur : (L.mem s (inqA (L.curW s)) &&& sched.Q_RELEASE) = 0#u64 ∧ L.mem s (scA (L.curW s)) ≠ 0#u64 ∧ + L.mem s (scA (L.curW s)) ∈ Ac) + (hvs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → Aligned b ∧ b ≠ stA (L.curW s)) + (hvc : ∀ v ∈ Z.V, stA (L.curW s) ≠ v ∧ stA (L.curW s) ≠ wa v 24#u64) + (hT : TreeA st OS Z.G.chains Z.G.par s) (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) (site : DelSiteA st Z.th OS s σ Z.G.chains) + (hCan : ∀ x ∈ OS, obj.sext48 x = ok x) {W Wx Wp : DelWA S A} + {Rd Rz : S → A → Fl4SpecA64.State Unit → MdbGhostA → U64 → Prop} + (Hk : DelHypsA st arI H.tcbs Z.th OS Fl4SpecA64.fl4FinaliseCap W + Wx Wp Rd Rz) + (hXr : ∀ x, σ.kheap σ.curThread = some (.tcb x) → + (∃ m', JAtZAX (L := L) AR { Z with G := Z.G } (L.wr s (stA (L.curW s)) sched.TS_RESTART) a + (stUpdA σ σ.curThread x .restart) m') → + W (L.wr s (stA (L.curW s)) sched.TS_RESTART) a (stUpdA σ σ.curThread x .restart) Z.G) + (lbl : U64) (slbl : Fl4SpecA64.Data) (hlbl : Fl4SpecA64.genInvocationType slbl = cnodeLabelOfA lbl) + (hl : lbl.val = 22) (length buf : U64) (args : List U64) (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) + (cap : capability.Cap) (ac : Fl4SpecA64.Cap) (hca : CapRelA cap ac) (hcv : CapValidA Z.th cap) + (excaps : List Fl4SpecA64.Cap) : + cnode.decode st arI s a lbl length cap buf extra ⦃ res => + InvPostDA st AR Z OS σ (Fl4SpecA64.decodeCnodeInvocation slbl (args.map UScalar.bv) ac excaps) s a + W Wp res ⦄ := by + have hg : Fl4SpecA64.genInvocationType slbl = .cnodeDelete := by rw [hlbl]; simp [cnodeLabelOfA, hl] + have hin : Fl4SpecA64.genInvocationType slbl ∈ + Fl4SpecA64.GenInvocationLabels.uptoEnum .cnodeRevoke .cnodeRotate := by rw [hg]; decide + obtain rfl : lbl = fl4_abi.label.CNodeDelete := by rw [u64_eq_iffA]; simp [hl] + unfold cnode.decode + have n1 : ¬ fl4_abi.label.CNodeDelete < fl4_abi.label.CNodeRevoke := by + have := lblA_revoke; have := lblA_delete; scalar_tac + have n2 : ¬ fl4_abi.label.CNodeDelete > fl4_abi.label.CNodeRotate := by + have := lblA_delete; have := lblA_rotate; scalar_tac + rw [if_neg n1, if_neg n2] + by_cases h2 : length < 2#u64 + · rw [if_pos h2] + simp only [error.truncated, bind_ok] + rw [WP.spec_ok] + exact Or.inl ⟨_, rfl, rfl, rfl, .truncatedMessage, decode_shortA _ _ _ σ hin _ (by simp; scalar_tac), trivial⟩ + rw [if_neg h2] + obtain ⟨y0, y1, rest, rfl⟩ : ∃ y0 y1 rest, args = y0 :: y1 :: rest := by + match args with + | [] => exfalso; simp at hlen; scalar_tac + | [_] => exfalso; simp at hlen; scalar_tac + | y0 :: y1 :: rest => exact ⟨y0, y1, rest, rfl⟩ + simp only [cur_thread_ok, hargs 0#u64 y0 (by simp), hargs 1#u64 y1 (by simp), bind_ok] + simp only [List.map_cons] + unfold cspace.lookup_target_slot + obtain ⟨r1, hr1, e1, he1, hrel1⟩ := spec_existsA + (lookup_slot_for_cnode_op_refinesA Z.th s σ E.j.1.st E.j.1.cv false cap ac hca hcv y0 y1) + have hlt : (Fl4SpecA64.lookupTargetSlot (Z := Unit) ac (Fl4SpecA64.dataToCptr y0.bv) + (Fl4SpecA64.dataToNat y1.bv)).run σ = ({(e1, σ)}, False) := he1 + rw [hr1]; simp only [bind_ok, core.result.Result.Insts.CoreOpsTry.branch] + cases r1 with + | Err e => + cases e1 with + | ok _ => exact hrel1.elim + | error e' => + simp only [core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, bind_ok, bind_tc_ok] + rw [WP.spec_ok] + exact Or.inl ⟨_, rfl, rfl, rfl, e', dec_delete_errA hg hlt, hrel1⟩ + | Ok dest => + cases e1 with + | error _ => exact hrel1.elim + | ok dsl => + obtain ⟨rfl, pd, hpd1, hpd, hcrd⟩ := hrel1 + have g1 : ¬ fl4_abi.label.CNodeDelete ≥ fl4_abi.label.CNodeCopy := by + have := lblA_copy; have := lblA_delete; scalar_tac + have k1 : ¬ fl4_abi.label.CNodeDelete = fl4_abi.label.CNodeRevoke := by + rw [u64_eq_iffA]; simp + simp only [g1, k1, ↓reduceIte, bind_ok] + obtain ⟨x, heq, hset, D1, hW1⟩ := decDel_restartWA E P Y hrun hcur hvs hvc hT hsub hnul site hXr + have hAd : AbsSlotA Z.th dsl := ⟨pd, hpd1, Or.inl hpd⟩ + rw [heq]; simp only [bind_ok] + refine WP.spec_mono (delete_exposed_capDeleteA E.sys.ent.os hCan Hk dsl hAd _ a _ Z.G D1 hW1) ?_ + rintro ⟨r, s', a'⟩ ⟨σ', G', D', hres, hW'⟩ + refine Or.inr ⟨.deleteCall dsl, dec_delete_okA hg hlt, + _, hset, σ', G', ?_, D', hW'⟩ + rw [fl4InvokeCnode_deleteA] + exact hres + +/-- **CNode Delete**, decode and invoke (x86 `cnode_delete_refinesW`; module docstring): FL4's `cnode::decode` at label +22 against l4v's `decode_cnode_invocation`, then `set_thread_state Restart` and `fl4_invoke_cnode` +(`fl4CapDelete`), over the deletion's hooks `Hk` at `DelJWA ... X` +(`cnode_delete_refinesWA`). -/ +theorem cnode_delete_refinesDA (arI : machine.Arch A) (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + (P : SysPlace st H Z.Cs H.frame Rp Nt OS s extra slot) (Y : SymL st H Z.Cs s) + (hrun : Runnable (L.mem s (stA (L.curW s)))) + (hcur : (L.mem s (inqA (L.curW s)) &&& sched.Q_RELEASE) = 0#u64 ∧ L.mem s (scA (L.curW s)) ≠ 0#u64 ∧ + L.mem s (scA (L.curW s)) ∈ Ac) + (hvs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → Aligned b ∧ b ≠ stA (L.curW s)) + (hvc : ∀ v ∈ Z.V, stA (L.curW s) ≠ v ∧ stA (L.curW s) ≠ wa v 24#u64) + (hT : TreeA st OS Z.G.chains Z.G.par s) (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) (site : DelSiteA st Z.th OS s σ Z.G.chains) + (hCan : ∀ x ∈ OS, obj.sext48 x = ok x) {X Xx Xp : DelWA S A} + {Rd Rz : S → A → Fl4SpecA64.State Unit → MdbGhostA → U64 → Prop} + (Hk : DelHypsA st arI H.tcbs Z.th OS Fl4SpecA64.fl4FinaliseCap (DelJWA (st := st) AR Z s X) + (DelJWA (st := st) AR Z s Xx) (DelJWA (st := st) AR Z s Xp) Rd Rz) + (hXr : ∀ x, σ.kheap σ.curThread = some (.tcb x) → + X (L.wr s (stA (L.curW s)) sched.TS_RESTART) a (stUpdA σ σ.curThread x .restart) Z.G) + (lbl : U64) (slbl : Fl4SpecA64.Data) (hlbl : Fl4SpecA64.genInvocationType slbl = cnodeLabelOfA lbl) + (hl : lbl.val = 22) (length buf : U64) (args : List U64) (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) + (cap : capability.Cap) (ac : Fl4SpecA64.Cap) (hca : CapRelA cap ac) (hcv : CapValidA Z.th cap) + (excaps : List Fl4SpecA64.Cap) : + cnode.decode st arI s a lbl length cap buf extra ⦃ res => + InvPostDA st AR Z OS σ (Fl4SpecA64.decodeCnodeInvocation slbl (args.map UScalar.bv) ac excaps) s a + (DelJWA (st := st) AR Z s X) (DelJWA (st := st) AR Z s Xp) res ⦄ := + cnode_delete_refinesWA arI E P Y hrun hcur hvs hvc hT hsub hnul site hCan Hk + (fun x hkx hj => ⟨⟨L.ksb_wr _ _ _, by rw [LawfulStore.cpu_wr]⟩, hj, hXr x hkx⟩) + lbl slbl hlbl hl length buf args hlen hargs cap ac hca hcv excaps + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/CnodeDelDefA.lean b/proofs/Fl4ProofsA64/P3/CnodeDelDefA.lean new file mode 100644 index 000000000..f1d3b929e --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/CnodeDelDefA.lean @@ -0,0 +1,582 @@ +/- +P3 (AArch64, A64-P3 CNode wave 2): **FL4's `finalise_slot_deferred` refines l4v `finalise_slot slot False`** (x86 +`P3/DelDef.deferred_loopZG`). Each round finalises the cap; a zombie that is not removable and not cyclic is swapped +into its first slot (`reduce_zombie slot False`), then the preemption point. The fuel never runs out: each such round +strictly decreases the measure `psiDA` (`P3/CnodeDelInvA`). + +* `psi_swapA`, `psiD_finA`, `psiDA_sameA`: the measure across the swap, a finalisation, a state with the same caps. +* `ChgOKA` / `StrictChgA`: how the caps of the other slots changed and when the measure fell (x86 `ChgOK`, + `StrictChg`); the loop's callers (Wave 3) need them. +* **`deferred_loopZGA`** (`DefInvGA` → `DefPostGA`): over a spec finaliser `Fn` and the hypotheses `DelHypsA`. The + loop's zombie write, swap (null or non-null first slot, `cap_swap_for_delete_hsA`) and preemption are exactly those + of `P3/CnodeDelRoundA` / `CnodeDelStepA` / `CnodeDelSwapA`. + +Not carried (x86's `MdbStep`): no A64 consumer asks for it; the ghost relation is `DStateA`'s. +Named premises: none new (`DelHypsA`'s hooks; `DStateA`'s [J] facts). +-/ +import Fl4ProofsA64.P3.CnodeDelRoundA +import Fl4ProofsA64.P3.CnodeDelSwapA +import Fl4ProofsA64.P3.CnodeDelZombieA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {th : TypedHeapA} {OS : Set U64} + +/-! ## The measure -/ + +theorem nnSA_renA (G : MdbGhostA) (x1 x2 : U64) : nnSA (swGA G x1 x2) = (nnSA G).image (Equiv.swap x1 x2) := by + classical + ext y + simp only [nnSA, swGA, renG, ← List.map_flatten, List.mem_toFinset, List.mem_map, Finset.mem_image, swap_eq_swp] + +/-- **The measure across a swap of a zombie into its first slot** (x86 `psi_swap`). -/ +theorem psi_swapA {s s' : S} {G : MdbGhostA} {p x : U64} (hM : MdbLk st OS G.chains s) + (hM' : MdbLk st OS (swGA G p x).chains s') + (hcap : ∀ y ∈ OS, capA st s' y = if y = x then capA st s p else if y = p then capA st s x else capA st s y) + (hxs : x ∈ OS) (hpx : p ≠ x) {zt n : U64} (hz : capA st s x = .Zombie p zt n) (hnh : ¬ HomeZA st s p) : + psiDA st s' (swGA G p x) < psiDA st s G ∧ (nnSA (swGA G p x)).card = (nnSA G).card := by + classical + have hcard : (nnSA (swGA G p x)).card = (nnSA G).card := by + rw [nnSA_renA]; exact Finset.card_image_of_injective _ (Equiv.injective _) + refine ⟨?_, hcard⟩ + unfold psiDA + rw [hcard] + have hxm : x ∈ mzSA st s G := by + rw [mem_mzSA hM] + refine ⟨hxs, (by rw [hz]; exact fun e => by cases e), ?_⟩ + rintro ⟨zt', n', h⟩; rw [hz] at h; cases h; exact hpx rfl + have hsub : mzSA st s' (swGA G p x) ⊆ ((mzSA st s G).erase x).image (Equiv.swap p x) := by + intro z hz' + have hz1 : z ∈ nnSA (swGA G p x) := (Finset.mem_filter.1 hz').1 + have hz2 : ¬ HomeZA st s' z := (Finset.mem_filter.1 hz').2 + rw [nnSA_renA, Finset.mem_image] at hz1 + obtain ⟨y, hy, rfl⟩ := hz1 + have hys : y ∈ OS := ((mem_nnSA hM y).1 hy).1 + refine Finset.mem_image.2 ⟨y, Finset.mem_erase.2 ⟨?_, ?_⟩, rfl⟩ + · rintro rfl + apply hz2 + rw [Equiv.swap_apply_right] + refine ⟨zt, n, ?_⟩ + rw [hcap p ((mem_nnSA hM' _).1 (by rw [nnSA_renA]; exact Finset.mem_image.2 ⟨y, hy, by rw [Equiv.swap_apply_right]⟩)).1, + if_neg hpx, if_pos rfl, hz] + · rw [mem_mzSA hM] + refine ⟨hys, ((mem_nnSA hM y).1 hy).2, ?_⟩ + by_cases hyp : y = p + · subst hyp; exact hnh + · by_cases hyx : y = x + · subst hyx; rintro ⟨zt', n', h⟩; rw [hz] at h; cases h; exact hpx rfl + · rw [Equiv.swap_apply_of_ne_of_ne hyp hyx] at hz2 + intro hh; apply hz2 + obtain ⟨zt', n', h⟩ := hh + exact ⟨zt', n', by rw [hcap y hys, if_neg hyx, if_neg hyp, h]⟩ + have h1 := Finset.card_le_card hsub + have h2 := Finset.card_image_le (s := (mzSA st s G).erase x) (f := Equiv.swap p x) + have h3 := Finset.card_erase_of_mem hxm + have h4 : 0 < (mzSA st s G).card := Finset.card_pos.2 ⟨x, hxm⟩ + omega + +/-- **The measure after a finalisation** (x86 `psiD_fin`). -/ +theorem psiD_finA {s s' : S} {G G' : MdbGhostA} (hM : MdbLk st OS G.chains s) (hM' : MdbLk st OS G'.chains s') + {x : U64} (hx : capA st s' x = capA st s x) + (hch : ∀ y ∈ OS, y ≠ x → capA st s' y = capA st s y ∨ (capA st s' y = .Null ∧ ¬ IsZombieA (capA st s y))) : + psiDA st s' G' ≤ psiDA st s G ∧ (nnSA G').card ≤ (nnSA G).card := by + have hnn : ∀ y ∈ OS, capA st s' y ≠ .Null → capA st s y ≠ .Null := by + intro y hy h + by_cases hyx : y = x + · rw [hyx, ← hx]; rw [hyx] at h; exact h + · rcases hch y hy hyx with e | ⟨e, -⟩ + · rw [← e]; exact h + · exact absurd e h + have hmz : ∀ y ∈ OS, capA st s' y ≠ .Null → ¬ HomeZA st s' y → ¬ HomeZA st s y := by + intro y hy h hh hz + apply hh + by_cases hyx : y = x + · subst hyx; unfold HomeZA at hz ⊢; rw [hx]; exact hz + · rcases hch y hy hyx with e | ⟨e, -⟩ + · unfold HomeZA at hz ⊢; rw [e]; exact hz + · exact absurd e h + refine ⟨psiDA_le hM hM' hnn hmz, Finset.card_le_card fun y hy => ?_⟩ + rw [mem_nnSA hM'] at hy; rw [mem_nnSA hM]; exact ⟨hy.1, hnn y hy.1 hy.2⟩ + +/-- A state with the same caps has the same measure. -/ +theorem psiDA_sameA {s s' : S} {G : MdbGhostA} (hM : MdbLk st OS G.chains s) (hM' : MdbLk st OS G.chains s') + (hc : ∀ y ∈ OS, capA st s' y = capA st s y) : psiDA st s' G = psiDA st s G := by + apply le_antisymm + · exact psiDA_le hM hM' (fun y hy h => by rw [← hc y hy]; exact h) + (fun y hy _ h => by unfold HomeZA at h ⊢; rw [hc y hy] at h; exact h) + · exact psiDA_le hM' hM (fun y hy h => by rw [hc y hy]; exact h) + (fun y hy _ h => by unfold HomeZA at h ⊢; rw [← hc y hy] at h; exact h) + +/-! ## Changes come with a smaller measure -/ + +/-- How the cap of an other slot changed: kept, or emptied / turned into a home zombie (x86 `ChgOK`). -/ +def ChgOKA (st : machine.Store S) [L : LawfulStore st] (s0 s : S) (y : U64) : Prop := + capA st s y = capA st s0 y ∨ ((capA st s y = .Null ∨ HomeZA st s y) ∧ ¬ HomeZA st s0 y) + +theorem ChgOKA.refl (s : S) (y : U64) : ChgOKA st s s y := Or.inl rfl + +theorem ChgOKA.trans {s0 s1 s2 : S} {y : U64} (h1 : ChgOKA st s0 s1 y) (h2 : ChgOKA st s1 s2 y) : + ChgOKA st s0 s2 y := by + rcases h1 with e1 | ⟨h1, n1⟩ + · rcases h2 with e2 | ⟨h2, n2⟩ + · exact Or.inl (e2.trans e1) + · exact Or.inr ⟨h2, fun ⟨zt, n, h⟩ => n2 ⟨zt, n, by rw [e1]; exact h⟩⟩ + · rcases h2 with e2 | ⟨h2, -⟩ + · refine Or.inr ⟨?_, n1⟩ + rcases h1 with h | ⟨zt, n, h⟩ + · exact Or.inl (e2.trans h) + · exact Or.inr ⟨zt, n, e2.trans h⟩ + · exact Or.inr ⟨h2, n1⟩ + +theorem chgOKA_of_fin {s0 s : S} {y : U64} + (h : capA st s y = capA st s0 y ∨ (capA st s y = .Null ∧ ¬ IsZombieA (capA st s0 y))) : ChgOKA st s0 s y := by + rcases h with h | ⟨h, hz⟩ + · exact Or.inl h + · exact Or.inr ⟨Or.inl h, fun ⟨zt, n, e⟩ => hz ⟨y, zt, n, e⟩⟩ + +/-- Every slot whose cap changed (other than `x`, or `x` too if it held a zombie) comes with a strictly smaller +measure (x86 `StrictChg`). -/ +def StrictChgA (st : machine.Store S) [L : LawfulStore st] (OS : Set U64) (s0 s : S) (G0 G : MdbGhostA) + (x : U64) : Prop := + ∀ y ∈ OS, capA st s y ≠ capA st s0 y → (y ≠ x ∨ IsZombieA (capA st s0 x)) → psiDA st s G < psiDA st s0 G0 + +theorem StrictChgA.refl (s : S) (G : MdbGhostA) (x : U64) : StrictChgA st OS s s G G x := + fun _ _ h => absurd rfl h + +theorem StrictChgA.trans {s0 s1 s2 : S} {G0 G1 G2 : MdbGhostA} {x : U64} + (h1 : StrictChgA st OS s0 s1 G0 G1 x) (h2 : StrictChgA st OS s1 s2 G1 G2 x) + (hl1 : psiDA st s1 G1 ≤ psiDA st s0 G0) (hl2 : psiDA st s2 G2 ≤ psiDA st s1 G1) : + StrictChgA st OS s0 s2 G0 G2 x := by + intro y hy hne hc + by_cases e : capA st s1 y = capA st s0 y + · have hne' : capA st s2 y ≠ capA st s1 y := by rw [e]; exact hne + have hc' : y ≠ x ∨ IsZombieA (capA st s1 x) := by + rcases hc with h | h + · exact Or.inl h + · by_cases hyx : y = x + · subst hyx; rw [e]; exact Or.inr h + · exact Or.inl hyx + have := h2 y hy hne' hc' + omega + · have := h1 y hy e hc + omega + +theorem StrictChgA.of_lt {s0 s1 : S} {G0 G1 : MdbGhostA} {x : U64} (h : psiDA st s1 G1 < psiDA st s0 G0) : + StrictChgA st OS s0 s1 G0 G1 x := fun _ _ _ _ => h + +/-- After a finalisation that only empties other slots (x86 `StrictChg.fin`). -/ +theorem StrictChgA.fin {s0 s1 : S} {G0 G1 : MdbGhostA} (hM : MdbLk st OS G0.chains s0) + (hM1 : MdbLk st OS G1.chains s1) {x : U64} (hx : capA st s1 x = capA st s0 x) + (hch : ∀ y ∈ OS, y ≠ x → capA st s1 y = capA st s0 y ∨ (capA st s1 y = .Null ∧ ¬ IsZombieA (capA st s0 y))) : + StrictChgA st OS s0 s1 G0 G1 x := by + intro y hy hne _ + have hyx : y ≠ x := fun e => hne (by rw [e, hx]) + rcases hch y hy hyx with e | ⟨e, -⟩ + · exact absurd e hne + · have hnn : ∀ z ∈ OS, capA st s1 z ≠ .Null → capA st s0 z ≠ .Null := by + intro z hz h + by_cases hzx : z = x + · rw [hzx, ← hx]; rw [hzx] at h; exact h + · rcases hch z hz hzx with e' | ⟨e', -⟩ + · rw [← e']; exact h + · exact absurd e' h + have hmz : ∀ z ∈ OS, capA st s1 z ≠ .Null → ¬ HomeZA st s1 z → ¬ HomeZA st s0 z := by + intro z hz h hh hz0 + apply hh + by_cases hzx : z = x + · subst hzx; unfold HomeZA at hz0 ⊢; rw [hx]; exact hz0 + · rcases hch z hz hzx with e' | ⟨e', -⟩ + · unfold HomeZA at hz0 ⊢; rw [e']; exact hz0 + · exact absurd e' h + refine psiDA_lt_nn hM hM1 hnn hmz (z := y) ((mem_nnSA hM y).2 ⟨hy, fun h => hne (by rw [e, h])⟩) ?_ + rw [mem_nnSA hM1]; rintro ⟨-, h⟩; exact h e + +/-! ## Small facts -/ + +/-- The `Zombie` remainder of `finalise_cap` comes from a thread, a CNode or the same zombie. -/ +theorem finRemA_zombie {c : capability.Cap} {f : Bool} {p zt n : U64} (h : finRemA c f = .Zombie p zt n) : + (∃ q, c = .Thread q) ∨ (∃ q r gs g, c = .CNode q r gs g) ∨ (∃ m, c = .Zombie p zt m ∧ m = n) := by + cases c with + | Thread t => exact Or.inl ⟨t, rfl⟩ + | CNode q r gs g => exact Or.inr (Or.inl ⟨q, r, gs, g, rfl⟩) + | Zombie q t m => + simp only [finRemA, capability.Cap.Zombie.injEq] at h + obtain ⟨e1, e2, e3⟩ := h + subst e1 e2 e3 + exact Or.inr (Or.inr ⟨m, rfl, rfl⟩) + | _ => simp [finRemA] at h + +theorem assertOpt_oneA {α : Type} {o : Option α} {a : α} (h : o = some a) (σ : Fl4SpecA64.State Unit) : + (NonDet.assertOpt o : NonDet (Fl4SpecA64.State Unit) α) σ = ({(a, σ)}, False) := by + subst h; simp [NonDet.assertOpt]; rfl + +/-- **The spec's `get_cap` of a typed slot has one outcome**, related to FL4's cap there. -/ +theorem getCap_oneA {mem : U64 → U64} {σ : Fl4SpecA64.State Unit} (hR : StateRelA th mem σ) {sl : CslotPtr} + (hs : AbsSlotA th sl) : + ∃ c, getCap (Z := Unit) sl σ = ({(c, σ)}, False) ∧ CapRelA (capAtA mem (cslotAddrA sl)) c := by + obtain ⟨o, idx⟩ := sl + obtain ⟨p, e1, hc | ⟨ht, i, hi, e2⟩⟩ := hs + · simp only at e1 hc; subst e1 + obtain ⟨cs, hk, hwf, hr⟩ := hR.cnode p _ hc + obtain ⟨c, hc', hrel⟩ := hr idx rfl + refine ⟨c, ?_, hrel⟩ + have h2 : (NonDet.assert (wellFormedCnodeN idx.length cs) : NonDet (Fl4SpecA64.State Unit) Unit) σ = + ({((), σ)}, False) := by simp [NonDet.assert, hwf] + unfold getCap + show (getObject _ >>= _) σ = _ + rw [nd_bind_singleA (getObject_oneA hk)] + dsimp only + simp only [nd_bind_singleA h2, nd_pure_bindA] + exact assertOpt_oneA hc' σ + · simp only at e1 e2; subst e1 e2 + obtain ⟨x, hk, hr⟩ := hR.tcb p ht + obtain ⟨c, hc', hrel⟩ := hr i hi + refine ⟨c, ?_, ?_⟩ + · unfold getCap + show (getObject _ >>= _) σ = _ + rw [nd_bind_singleA (getObject_oneA hk)] + show ((pure (tcbCnodeMap x) : NonDet (Fl4SpecA64.State Unit) _) >>= + fun caps => NonDet.assertOpt (caps (tcbCnodeIndex i))) σ = _ + rw [nd_pure_bindA] + exact assertOpt_oneA hc' σ + · unfold cslotAddrA; rw [ofBl_tcbIdx i (by unfold tcbSlotsA at hi; omega)]; exact hrel + +/-- The spec's `get_cap` of a typed slot: its one outcome `ac`, related to the concrete cap by `CapEqA` (equal to +`absCapA` except at a mapped Stage-2 `PageTable` cap; Architect ruling 2026-12-02 22:35Z). -/ +theorem getCapOne_eqA {s : S} {σ : Fl4SpecA64.State Unit} (hR : StateRelA th (L.mem s) σ) {sl : CslotPtr} + (hA : AbsSlotA th sl) : + ∃ ac, getCap (Z := Unit) sl σ = ({(ac, σ)}, False) ∧ CapEqA (capA st s (cslotAddrA sl)) ac := by + obtain ⟨b, hg, hcb⟩ := getCap_oneA hR hA + exact ⟨b, hg, hcb.2⟩ + +theorem capRemoveable_zombieA {kb : U64} (hP : PlaceA th kb) {p zt n : U64} (h : ZOKA th p zt n) {sl : CslotPtr} + (hs : AbsSlotA th sl) : + capRemoveable (absCapA (.Zombie p zt n)) sl ↔ n.val = 0 ∨ (n.val = 1 ∧ p = cslotAddrA sl) := by + show (n.val = 0 ∨ (n.val = 1 ∧ homeSlA p zt = sl)) ↔ _ + rw [home_iffA hP h hs] + +theorem swp_capA {α : Type} {p x : U64} (hpx : p ≠ x) (f : U64 → α) (y : U64) : + f (swp p x y) = if y = x then f p else if y = p then f x else f y := by + unfold swp + by_cases h1 : y = p + · subst h1; simp [hpx] + · by_cases h2 : y = x + · subst h2; simp [h1] + · simp [h1, h2] + +end + +/-! ## The deferred loop -/ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-- The outcome of the loop is `WPostA`: `W` unless preempted (x86 `WPost`). -/ +def WPostA {α : Type} (W Wp : DelWA S A) (r : core.result.Result α error.Exc) (s : S) (a : A) + (σ : Fl4SpecA64.State Unit) (G : MdbGhostA) : Prop := + match r with + | .Err .Preempted => Wp s a σ G + | _ => W s a σ G + +variable (st) in +/-- **The deferred loop's invariant** over a spec finaliser `Fn`, from `s0` / `σ0` / `G0` (x86 `DefInvG`). -/ +def DefInvGA (Fn : FinFnA) (W : DelWA S A) (th : TypedHeapA) (OS : Set U64) (sl : CslotPtr) (s0 : S) + (σ0 : Fl4SpecA64.State Unit) (G0 : MdbGhostA) (x : S × A × U64) : Prop := + ∃ σ G, DStateA st th OS x.1 σ G ∧ psiDA st x.1 G < x.2.2.val ∧ (nnSA G).card ≤ (nnSA G0).card ∧ + psiDA st x.1 G ≤ psiDA st s0 G0 ∧ + (∀ y, RDGA Fn (.finaliseSlotCall sl false) σ y → RDGA Fn (.finaliseSlotCall sl false) σ0 y) ∧ + (∀ y ∈ OS, y ≠ cslotAddrA sl → ChgOKA st s0 x.1 y) ∧ + StrictChgA st OS s0 x.1 G0 G (cslotAddrA sl) ∧ W x.1 x.2.1 σ G + +variable (st) in +/-- **What the deferred loop ends with** (x86 `DefPostG`). -/ +def DefPostGA (Fn : FinFnA) (W Wx Wp : DelWA S A) (Rd : S → A → Fl4SpecA64.State Unit → MdbGhostA → U64 → Prop) + (th : TypedHeapA) (OS : Set U64) (sl : CslotPtr) (s0 : S) (σ0 : Fl4SpecA64.State Unit) (G0 : MdbGhostA) + (res : core.result.Result cdt.Finalised error.Exc × S × A) : Prop := + ∃ σ' G', DStateA st th OS res.2.1 σ' G' ∧ (nnSA G').card ≤ (nnSA G0).card ∧ + psiDA st res.2.1 G' ≤ psiDA st s0 G0 ∧ + (∀ y ∈ OS, y ≠ cslotAddrA sl → ChgOKA st s0 res.2.1 y) ∧ + StrictChgA st OS s0 res.2.1 G0 G' (cslotAddrA sl) ∧ + ((∃ f, res.1 = .Ok f ∧ CleanCapA f.cleanup ∧ CapWfA f.cleanup ∧ + (∀ i, f.cleanup = .IrqHandler i → ∀ y ∈ OS, y ≠ cslotAddrA sl → capA st res.2.1 y ≠ .IrqHandler i) ∧ + (f.success = false → HomeZA st res.2.1 (cslotAddrA sl)) ∧ + (f.success = true → capA st res.2.1 (cslotAddrA sl) ≠ .Null → + Rd res.2.1 res.2.2 σ' G' (cslotAddrA sl)) ∧ + (f.success = false ∨ capA st res.2.1 (cslotAddrA sl) = .Null → W res.2.1 res.2.2 σ' G') ∧ + RDGA Fn (.finaliseSlotCall sl false) σ0 + (.ok (f.success, if f.success then absCapA f.cleanup else .nullCap), σ')) ∨ + (res.1 = .Err .Preempted ∧ RDGA Fn (.finaliseSlotCall sl false) σ0 (.error (), σ'))) ∧ + WPostA Wx Wp res.1 res.2.1 res.2.2 σ' G' + + +/-! ## The loop -/ + +theorem absCapA_ne_null {c : capability.Cap} (hcn : c ≠ .Null) (hwf : CapWfA c) : absCapA c ≠ .nullCap := by + cases c <;> first | exact absurd rfl hcn | (intro h; simp [absCapA] at h; done) | (exfalso; simp [CapWfA] at hwf; done) + +/-- `is_final_cap'` of a cap related by `CapEqA` is that of its abstraction (it reads only the object references). -/ +theorem isFinalCap'_eqA {c : capability.Cap} {a : Fl4SpecA64.Cap} (h : CapEqA c a) (σ : Fl4SpecA64.State Unit) : + isFinalCap' a σ ↔ isFinalCap' (absCapA c) σ := by + unfold isFinalCap'; rw [h.genObjRefs_eq] + +/-- The cap related by `CapEqA` to a non-null well-formed cap is not null. -/ +theorem CapEqA.ne_null {c : capability.Cap} {a : Fl4SpecA64.Cap} (h : CapEqA c a) (hcn : c ≠ .Null) + (hwf : CapWfA c) : a ≠ .nullCap := by + rcases h.eq_or_pt with e | ⟨_, _, _, e⟩ + · rw [e]; exact absCapA_ne_null hcn hwf + · rw [e]; intro h; cases h + +theorem finRemA_cases (c : capability.Cap) (f : Bool) : + finRemA c f = .Null ∨ ∃ p zt n, finRemA c f = .Zombie p zt n := by + cases c with + | CNode p r gs g => by_cases hf : f <;> simp [finRemA, hf] + | Thread t => by_cases hf : f <;> simp [finRemA, hf] + | Zombie p t n => exact Or.inr ⟨p, t, n, rfl⟩ + | _ => exact Or.inl rfl + +theorem rd_reduce_zombieA {Fn : FinFnA} {p zt n : U64} {sl : CslotPtr} {σ σ' : Fl4SpecA64.State Unit} + (hne : homeSlA p zt ≠ sl) (hn : 1 ≤ n.val) + (hsw : ((), σ') ∈ (capSwapForDelete (Z := Unit) (homeSlA p zt) sl σ).1) : + ∃ v, RDGA Fn (.reduceZombieCall (absCapA (.Zombie p zt n)) sl false) σ (.ok v, σ') := by + obtain ⟨n1, hn1⟩ : ∃ n1, n.val = n1 + 1 := ⟨n.val - 1, by omega⟩ + have habs : absCapA (.Zombie p zt n) = .zombie (specPtr p) (zombieBitsA zt) (n1 + 1) := by + rw [← hn1]; rfl + rw [habs] + have e : firstCslotOf (Cap.zombie (specPtr p) (zombieBitsA zt) (n1 + 1)) = homeSlA p zt := rfl + have hne' : firstCslotOf (Cap.zombie (specPtr p) (zombieBitsA zt) (n1 + 1)) ≠ sl := by rw [e]; exact hne + have hsw' : ((), σ') ∈ (capSwapForDelete (Z := Unit) + (firstCslotOf (Cap.zombie (specPtr p) (zombieBitsA zt) (n1 + 1))) sl σ).1 := by rw [e]; exact hsw + exact rd_reduce_falseGA (Fn := Fn) (ptr := specPtr p) (bits := zombieBitsA zt) (n := n1) (sl := sl) hne' hsw' + +section +open Classical + +set_option maxHeartbeats 4000000 in +/-- **FL4's deferred finalisation loop refines `finalise_slot slot False`** (module docstring; x86 +`deferred_loopZG`). `hsl`: the slot's address is a canonical (`sext48`-fixed) pointer (the decode site's lookup). -/ +theorem deferred_loopZGA {ar : machine.Arch A} {T Ω Sg OS : Set U64} {th : TypedHeapA} + (hO : OSOK T Ω Sg OS) {Fn : FinFnA} {W Wx Wp : DelWA S A} + {Rd Rz : S → A → Fl4SpecA64.State Unit → MdbGhostA → U64 → Prop} + (H : DelHypsA st ar T th OS Fn W Wx Wp Rd Rz) + (sl : CslotPtr) (hA : AbsSlotA th sl) (hsl : obj.sext48 (cslotAddrA sl) = ok (cslotAddrA sl)) + (s0 : S) (σ0 : Fl4SpecA64.State Unit) (G0 : MdbGhostA) (s : S) (a : A) (fuel : U64) + (hinv : DefInvGA st Fn W th OS sl s0 σ0 G0 (s, a, fuel)) : + cdt.finalise_slot_deferred_loop st ar s a (cslotAddrA sl) fuel ⦃ res => + DefPostGA st Fn W Wx Wp Rd th OS sl s0 σ0 G0 res ⦄ := by + unfold cdt.finalise_slot_deferred_loop + refine loop.spec_decr_nat (fun x => x.2.2.val) (DefInvGA st Fn W th OS sl s0 σ0 G0) _ _ _ ?_ hinv + rintro ⟨s, a, fuel⟩ ⟨σ, G, hDS, hlt, hcd, hps, htr, hch, hsc, hW⟩ + replace hlt : psiDA st s G < fuel.val := hlt + replace hps : psiDA st s G ≤ psiDA st s0 G0 := hps + unfold cdt.finalise_slot_deferred_loop.body + have hf : fuel > 0#u64 := by scalar_tac + obtain ⟨fuel1, hv, hv1⟩ := spec_existsA (U64.sub_spec (x := fuel) (y := 1#u64) (by scalar_tac)) + have hv1' : fuel1.val + 1 = fuel.val := by scalar_tac + simp only [hf, ↓reduceIte, hv, bind_ok, capA_eq] + have hxO := hDS.mem_slot hA + obtain ⟨ac, hg, hace⟩ := getCapOne_eqA hDS.rel hA + obtain ⟨c, hc⟩ : ∃ c, capA st s (cslotAddrA sl) = c := ⟨_, rfl⟩ + rw [hc] at hace ⊢ + try simp only [bind_ok] + by_cases hcn : c = .Null + · subst hcn + have hacn : ac = Fl4SpecA64.Cap.nullCap := (hace.eq_abs fun _ _ _ e => by cases e).symm + rw [hacn] at hg + rw [cap_is_null_nullA]; simp only [bind_ok, ↓reduceIte, WP.spec_ok] + exact ⟨σ, G, hDS, hcd, hps, hch, hsc, Or.inl ⟨_, rfl, trivial, trivial, (fun _ h => by cases h), fun h => by simp at h, + fun _ h => absurd hc h, fun _ => hW, htr _ (rd_fin_nullGA hg)⟩, H.wx _ _ _ _ hW⟩ + rw [cap_is_null_neA hcn]; simp only [bind_ok, Bool.false_eq_true, ↓reduceIte] + have hcan : ac ≠ .nullCap := hace.ne_null hcn (hc ▸ hDS.wf hxO) + obtain ⟨bf, hbf1, hbf2a⟩ : ∃ bf : Bool, cdt.is_final_capability_with_cap st s (cslotAddrA sl) c = ok bf ∧ + (NeedsFinCA c → (bf = true ↔ isFinalCap' (absCapA c) σ)) := by + by_cases hN : NeedsFinCA c + · obtain ⟨⟨b, hb, e⟩, -⟩ := finalHypA th OS G.chains s σ sl c hDS.toFinStA hA hc hN + exact ⟨b, hb, fun _ => e⟩ + · obtain ⟨b, hb, -⟩ := spec_existsA (P0.cdt.is_final_capability_with_cap.p0 st s (cslotAddrA sl) c L.total) + exact ⟨b, hb, fun h => absurd h hN⟩ + rw [hbf1]; simp only [bind_ok] + have hbf2 : NeedsFinCA c → (bf = true ↔ isFinalCap' ac σ) := fun h => (hbf2a h).trans (isFinalCap'_eqA hace σ).symm + apply WP.spec_bind (fin_stepA H hDS hW hA hc hcn hg hace hbf2) + rintro ⟨⟨rem, cl⟩, s1, a1⟩ ⟨hrem, hcl, hclw, σ1, G1, ⟨d, hd, hfc⟩, D1, hx1, hch1, hWF, hRd1, hRz1, hirq1⟩ + try simp only [uncurry_apply_pair] at hrem hcl hclw hx1 hch1 hWF hRd1 hRz1 hfc D1 ⊢ + try dsimp only at hrem hcl hclw hx1 hch1 hWF hRd1 hRz1 hfc D1 ⊢ + have hdd : d = decide (isFinalCap' ac σ) := Bool.eq_iff_iff.2 (hd.trans decide_eq_true_iff.symm) + have hfc' : ((absCapA rem, absCapA cl), σ1) ∈ (Fn ac (decide (isFinalCap' ac σ)) σ).1 := by + rw [← hdd]; exact hfc + have hx1' : capA st s1 (cslotAddrA sl) = capA st s (cslotAddrA sl) := hx1.trans hc.symm + obtain ⟨hps1, hcd1⟩ := psiD_finA hDS.lk D1.lk hx1' hch1 + have hsc1 : StrictChgA st OS s0 s1 G0 G1 (cslotAddrA sl) := + hsc.trans (StrictChgA.fin hDS.lk D1.lk hx1' hch1) hps hps1 + have hch1' : ∀ y ∈ OS, y ≠ cslotAddrA sl → ChgOKA st s0 s1 y := + fun y hy hyx => (hch y hy hyx).trans (chgOKA_of_fin (hch1 y hy hyx)) + have hnoW : ∀ h : capA st s1 (cslotAddrA sl) = .Null, False := fun h => hcn (hx1.symm.trans h) + rcases finRemA_cases c bf with hN | ⟨p, zt, n, hZ⟩ + · have hN' : rem = .Null := hrem.trans hN + subst hN' + simp only [cdt.cap_removable, bind_ok, ↓reduceIte, WP.spec_ok] + exact ⟨σ1, G1, D1, le_trans hcd1 hcd, le_trans hps1 hps, hch1', hsc1, Or.inl ⟨_, rfl, hcl, hclw, hirq1, + fun h => by simp at h, fun _ _ => hRd1 (Or.inl rfl), + fun h => h.elim (fun e => by simp at e) (fun e => absurd (hx1.symm.trans e) hcn), + htr _ (rd_fin_removableGA hg hcan hfc' trivial)⟩, hWF.2 (Or.inl rfl)⟩ + -- a zombie remains + have hZ' : rem = .Zombie p zt n := hrem.trans hZ + subst hZ' + obtain ⟨hcanp, hwfz, hzok, hRk, hpc, hcls, hu⟩ := zombie_factsA hDS hA hc hbf2a hZ + (fun y hy hne => (hch1 y hy hne).imp_right And.left) + obtain ⟨b, hrm, hrmS⟩ := cap_removable_zombieA hDS.place hzok hA + have hrmv' := capRemoveable_zombieA hDS.place hzok hA + rw [hrm]; simp only [bind_ok] + by_cases hb : b = true + · rw [if_pos hb] + simp only [WP.spec_ok] + have hrmv : n.val = 0 ∨ (n.val = 1 ∧ p = cslotAddrA sl) := hrmv'.1 (hrmS.1 hb) + exact ⟨σ1, G1, D1, le_trans hcd1 hcd, le_trans hps1 hps, hch1', hsc1, Or.inl ⟨_, rfl, hcl, hclw, hirq1, + fun h => by simp at h, fun _ _ => hRd1 (Or.inr ⟨p, zt, n, rfl, hrmv⟩), + fun h => h.elim (fun e => by simp at e) (fun e => absurd (hx1.symm.trans e) hcn), + htr _ (rd_fin_removableGA hg hcan hfc' (hrmv'.2 hrmv))⟩, hWF.2 (Or.inr ⟨p, zt, n, rfl, hrmv⟩)⟩ + rw [if_neg hb] + have hrmv : ¬ (n.val = 0 ∨ (n.val = 1 ∧ p = cslotAddrA sl)) := fun h => hb (hrmS.2 (hrmv'.2 h)) + have hnrm : ¬ capRemoveable (absCapA (.Zombie p zt n)) sl := fun h => hrmv (hrmv'.1 h) + have hW1 := hWF.1 (fun h => by + rcases h with e | ⟨p', zt', n', e, h⟩ + · cases e + · cases e; exact hrmv h) + have hxn1 : capA st s1 (cslotAddrA sl) ≠ .Null := by rw [hx1]; exact hcn + have hsame : ∀ p' zt' n', capA st s1 (cslotAddrA sl) = .Zombie p' zt' n' → p' = p ∧ zt' = zt ∧ n' = n := by + intro p' zt' n' hq + have hc' : c = .Zombie p' zt' n' := hx1.symm.trans hq + have hZ2 := hZ + rw [hc'] at hZ2 + simp only [finRemA, capability.Cap.Zombie.injEq] at hZ2 + exact hZ2 + have hshape : ∀ p' zt' n', capA st s1 (cslotAddrA sl) = .Zombie p' zt' n' → p' = p ∧ zt' = zt ∧ + (n'.val = n.val ∨ (n'.val = n.val + 1 ∧ + capA st s1 (cslotAddrA (specPtr p, natToCref (zombieCteBits (zombieBitsA zt)) n.val)) = .Null)) := by + intro p' zt' n' hq + obtain ⟨e1, e2, e3⟩ := hsame p' zt' n' hq + exact ⟨e1, e2, Or.inl (by rw [e3])⟩ + have hsrc := finRemA_zombie hZ + have hnz : ¬ IsZombieA (capA st s1 (cslotAddrA sl)) → (∃ q, capA st s1 (cslotAddrA sl) = .Thread q) ∨ + ∃ q r gs g, capA st s1 (cslotAddrA sl) = .CNode q r gs g := by + intro hnz + rcases hsrc with ⟨q, hq⟩ | ⟨q, r, gs, g, hq⟩ | ⟨m, hq, -⟩ + · exact Or.inl ⟨q, by rw [hx1, hq]⟩ + · exact Or.inr ⟨q, r, gs, g, by rw [hx1, hq]⟩ + · exact absurd ⟨p, zt, m, by rw [hx1, hq]⟩ hnz + apply WP.spec_bind (set_zombie_sA hO D1 hA hcanp hwfz hzok (by rw [hx1]; exact hRk) (by rw [hx1]; exact hpc) + (by rw [hx1]; exact hcls) (hu.imp_right fun ⟨n0, h⟩ => ⟨n0, by rw [hx1]; exact h⟩)) + rintro s2 ⟨hwp, cx, co, σ2, hset, hSS2, D2, hZm⟩ + have hW2 : W s2 a1 σ2 G1 := H.zomb s1 s2 a1 σ1 σ2 G1 sl p zt n hW1 + (fun q hq => hRz1 q (hx1.symm.trans hq) p zt n rfl) hshape hnz D1 D2 hwp hSS2 hset hA hxn1 cx co hZm + have hps2 : psiDA st s2 G1 ≤ psiDA st s1 G1 := by + refine psiDA_le D1.lk D2.lk (fun y hy h => ?_) (fun y hy _ hh => ?_) + · by_cases e : y = cslotAddrA sl + · rw [e]; exact hxn1 + · rw [← co y hy e]; exact h + · by_cases e : y = cslotAddrA sl + · subst e + rintro ⟨zt', n', hq⟩ + obtain ⟨e1, e2, -⟩ := hsame _ zt' n' hq + exact hh ⟨zt, n, by rw [cx, ← e1]⟩ + · rintro ⟨zt', n', hq⟩ + exact hh ⟨zt', n', by rw [co y hy e]; exact hq⟩ + have hsc2 : StrictChgA st OS s0 s2 G0 G1 (cslotAddrA sl) := by + refine hsc1.trans (fun y hy hne hcnd => ?_) (le_trans hps1 hps) hps2 + by_cases hyx : y = cslotAddrA sl + · subst hyx + exfalso + rcases hcnd with h | ⟨p', zt', n', h⟩ + · exact h rfl + · obtain ⟨e1, e2, e3⟩ := hsame _ _ _ h + apply hne; rw [cx, h, e1, e2, e3] + · exact absurd (co y hy hyx) hne + have hch2 : ∀ y ∈ OS, y ≠ cslotAddrA sl → ChgOKA st s0 s2 y := + fun y hy hyx => (hch1' y hy hyx).trans (Or.inl (co y hy hyx)) + obtain ⟨hcy, hcyS⟩ := cap_cyclic_zombieA hDS.place hzok hA + rw [hcy]; simp only [bind_ok] + by_cases hpx : p = cslotAddrA sl + · rw [if_pos (decide_eq_true hpx)] + simp only [WP.spec_ok] + exact ⟨σ2, G1, D2, le_trans hcd1 hcd, le_trans hps2 (le_trans hps1 hps), hch2, hsc2, + Or.inl ⟨_, rfl, hcl, hclw, + fun i hi y hy hne => by rw [co y hy hne]; exact hirq1 i hi y hy hne, fun _ => ⟨zt, n, by rw [cx, hpx]⟩, fun h => by simp at h, fun _ => hW2, + htr _ (rd_fin_cyclicGA hg hcan hfc' hnrm (hcyS.2 hpx) hset)⟩, H.wx _ _ _ _ hW2⟩ + rw [if_neg (by simp [hpx])] + -- swap the zombie into its first slot + have hpO : p ∈ OS := by + have h := D2.mem_slot hzok.absSlot + rwa [hzok.addr] at h + have hne' : homeSlA p zt ≠ _ := fun e => hpx ((home_iffA hDS.place hzok hA).1 e) + have hxn2 : capA st s2 (cslotAddrA sl) ≠ .Null := by rw [cx]; exact fun e => by cases e + have hs1 : obj.sext48 (cslotAddrA (homeSlA p zt)) = ok (cslotAddrA (homeSlA p zt)) := by + rw [hzok.addr]; exact hcanp.sx + have hsw := cap_swap_for_delete_hsA hO D2 (sa := homeSlA p zt) hzok.absSlot hA hne' hxn2 hs1 hsl + rw [hzok.addr] at hsw + apply WP.spec_bind hsw + rintro s3 ⟨hw3, hcap3, σ3, hsw3, hSS3, D3, hSw⟩ + have hcap3' : ∀ y ∈ OS, capA st s3 y = if y = cslotAddrA sl then capA st s2 p + else if y = p then capA st s2 (cslotAddrA sl) else capA st s2 y := + fun y hy => (hcap3 y hy).trans (swp_capA hpx (capA st s2) y) + have hnh : ¬ HomeZA st s2 p := by + rintro ⟨zt', n', h⟩ + exact (D2.zu hpO hxO hpx h cx) rfl + obtain ⟨hlt3, hcd3⟩ := psi_swapA D2.lk D3.lk hcap3' hxO hpx cx hnh + have hn1 : 1 ≤ n.val := by + by_contra h; exact hrmv (Or.inl (by omega)) + have hW3 := H.swap s2 s3 a1 σ2 σ3 G1 sl p zt n hW2 hn1 D2 D3 hw3 hSS3 hsw3 hA cx hpx hcap3' hSw + have hsc3 : StrictChgA st OS s0 s3 G0 (swGA G1 p (cslotAddrA sl)) (cslotAddrA sl) := + hsc2.trans (StrictChgA.of_lt hlt3) (le_trans hps2 (le_trans hps1 hps)) hlt3.le + have hch3 : ∀ y ∈ OS, y ≠ cslotAddrA sl → ChgOKA st s2 s3 y := by + intro y hy hyx + by_cases hyp : y = p + · rw [hyp] + refine Or.inr ⟨Or.inr ⟨zt, n, ?_⟩, hnh⟩ + rw [hcap3' p hpO, if_neg hpx, if_pos rfl, cx] + · left; rw [hcap3' y hy, if_neg hyx, if_neg hyp] + have hch3' : ∀ y ∈ OS, y ≠ cslotAddrA sl → ChgOKA st s0 s3 y := + fun y hy hyx => (hch2 y hy hyx).trans (hch3 y hy hyx) + obtain ⟨v, hrz⟩ := rd_reduce_zombieA (Fn := Fn) (σ := σ2) hne' hn1 hsw3 + have hcy' : ¬ (capCyclicZombie (absCapA (.Zombie p zt n)) sl ∧ false = false) := + fun h => hpx (hcyS.1 h.1) + apply WP.spec_bind (H.pp s3 a1 σ3 _ D3 hW3) + rintro ⟨r4, s4, a4⟩ ⟨hk4, σ4, D4, hcase⟩ + simp only at hk4 D4 hcase ⊢ + have hps4 : psiDA st s4 (swGA G1 p (cslotAddrA sl)) = psiDA st s3 (swGA G1 p (cslotAddrA sl)) := + psiDA_sameA D3.lk D4.lk hk4 + have hch4 : ∀ y ∈ OS, y ≠ cslotAddrA sl → ChgOKA st s0 s4 y := + fun y hy hyx => (hch3' y hy hyx).trans (Or.inl (hk4 y hy)) + have hle3 : psiDA st s3 (swGA G1 p (cslotAddrA sl)) ≤ psiDA st s0 G0 := + le_trans hlt3.le (le_trans hps2 (le_trans hps1 hps)) + have hsc4 : StrictChgA st OS s0 s4 G0 (swGA G1 p (cslotAddrA sl)) (cslotAddrA sl) := + hsc3.trans (fun y hy hne _ => absurd (hk4 y hy) hne) hle3 (le_of_eq hps4) + have hcdle : (nnSA (swGA G1 p (cslotAddrA sl))).card ≤ (nnSA G0).card := + le_trans hcd3.le (le_trans hcd1 hcd) + rcases hcase with ⟨rfl, hpm, hW4⟩ | ⟨rfl, hpm, hW4⟩ + · simp only [uncurry_apply_pair, core.result.Result.Insts.CoreOpsTry.branch, bind_ok, WP.spec_ok] + refine ⟨⟨σ4, _, D4, ?_, hcdle, ?_, fun y hy => htr _ (rd_fin_reduceGA hg hcan hfc' hnrm hcy' hset hrz hpm hy), + hch4, hsc4, hW4⟩, (by show fuel1.val < fuel.val; omega)⟩ + · dsimp only + rw [hps4] + have hf1 : psiDA st s G ≤ fuel1.val := by omega + exact lt_of_lt_of_le hlt3 (le_trans hps2 (le_trans hps1 hf1)) + · dsimp only + rw [hps4]; exact hle3 + · simp only [uncurry_apply_pair, core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, bind_ok, bind_tc_ok, WP.spec_ok] + exact ⟨σ4, _, D4, hcdle, by rw [hps4]; exact hle3, hch4, hsc4, + Or.inr ⟨rfl, htr _ (rd_fin_pp_errGA hg hcan hfc' hnrm hcy' hset hrz hpm)⟩, hW4⟩ + +end + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/CnodeDelFootA.lean b/proofs/Fl4ProofsA64/P3/CnodeDelFootA.lean new file mode 100644 index 000000000..83e96630c --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/CnodeDelFootA.lean @@ -0,0 +1,84 @@ +/- +P3 (AArch64, A64-P3 CNode wave 2): **the slot steps of the deletion without `SysInvA`**. + +`empty_slot_jA` (`P3/SlotJA`) and `cap_swap_for_delete_jA` (`P3/SwapA`) ask for a whole entry state `JEntA`, which a +deletion loop cannot rebuild after a step. They read it only through the apartness of the relation's non-slot words +(`footN_offA`) and their alignment, which depend on the state only through its kernel-state base and its CPU: every +slot write keeps both. So **`DelFootA`**, taken once from the entry `JEntA` (**`DelFootA.of_entA`**), serves every +later state of the loop with the same base and CPU (**`SameCoreA`**), and the leaves here (**`empty_slot_dA`**, +**`cap_swap_for_delete_dA`**) take `JAtZAX` instead of `JEntA` and return `SameCoreA` again. + +No named premise. +-/ +import Fl4ProofsA64.P3.SlotJA +import Fl4ProofsA64.P3.SwapA +import Fl4ProofsA64.P3.CnodeDelHookA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- The kernel-state base and the CPU of `s0`. -/ +def SameCoreA (st : machine.Store S) [L : LawfulStore st] (s0 s : S) : Prop := + L.ksb s = L.ksb s0 ∧ L.cpu s = L.cpu s0 + +theorem SameCoreA.refl (s : S) : SameCoreA st s s := ⟨rfl, rfl⟩ + +theorem SameCoreA.writes {s0 s s' : S} {W : U64 → Prop} (h : SameCoreA st s0 s) (w : WritesIn st s s' W) : + SameCoreA st s0 s' := ⟨w.1.trans h.1, w.2.1.trans h.2⟩ + +/-- The relation's non-slot words do not depend on the ghost, and on the state only through `SameCoreA`. -/ +theorem footNA_coreA {Z : XSetsA} {G : MdbGhostA} {s0 s : S} (h : SameCoreA st s0 s) {b : U64} + (hb : FootNA (L := L) { Z with G := G } s b) : FootNA (L := L) Z s0 b := by + have hn : ∀ c o : U64, L.nodeAt s c o = L.nodeAt s0 c o := fun c o => nodeAt_ksb h.1 c o + cases hb with + | node c w hc hw => exact .node c b hc (by simpa only [hn] using hw) + | cur w hw => exact .cur b (by simpa only [hn, h.2] using hw) + | tcb t w ht hw => exact .tcb t b ht hw + | sc c hc => exact .sc c hc + | vcpu v w hv hw => exact .vcpu v b hv hw + | irq i hi => rw [h.1]; exact .irq i hi + | cur2 w hw => exact .cur2 b (by simpa only [hn, h.2] using hw) + | glob w hw => exact .glob b (by simpa only [h.1] using hw) + | tcb2 t w ht hw => exact .tcb2 t b ht hw + | sc2 c hc => exact .sc2 c hc + | vs w hw => exact .vs b (by simpa only [h.1] using hw) + | ext w hw => exact .ext b hw + +/-- **The static placement facts of a deletion** (module docstring), at the entry state `s0`. -/ +structure DelFootA (st : machine.Store S) [L : LawfulStore st] (Z : XSetsA) (T Ω Sg OS : Set U64) (s0 : S) : + Prop where + os : OSOK T Ω Sg OS + off : ∀ b, FootNA (L := L) Z s0 b → ¬ W7.CapW T {b | InOS OS b} ∅ b + al : ∀ b, FootNA (L := L) Z s0 b → Aligned b + +theorem DelFootA.of_entA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {s : S} {a : A} {σ : State Unit} {m : McState} + (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) : + DelFootA st Z H.tcbs H.omega H.frame OS s := + ⟨E.sys.ent.os, footN_offA E, FootNA.aligned E.j.1 (bxi_nodes E.sys.ent.bxi) E.scAl E.ap.vcAl⟩ + +/-- `JAtZAX` across slot writes at a later state of the deletion. -/ +theorem DelFootA.slotWrites {A : Type} {AR : A → HwRootA → Prop} {a : A} {Z : XSetsA} {T Ω Sg OS : Set U64} + {s0 s s' : S} (F : DelFootA st Z T Ω Sg OS s0) (hk : SameCoreA st s0 s) {G G' : MdbGhostA} + {σ σ' : State Unit} {m : McState} (J : JAtZAX (L := L) AR { Z with G := G } s a σ m) + (hw : WritesIn st s s' (W7.CapW T {b | InOS OS b} ∅)) (HS : SpecSlotA σ σ') (hst : StateRelA Z.th (L.mem s') σ') + (hcdt : CdtRelA st Z.th s' σ' G') (hcap : ∀ y, IsSlotA Z.th y → CapObjOkA Z (capAtA (L.mem s') y)) : + JAtZAX (L := L) AR { Z with G := G' } s' a σ' m := by + have h := J.of_slotWrites hw (fun b hb hf => F.off b (footNA_coreA hk hf) hb) + (fun b hf => F.al b (footNA_coreA hk hf)) G' HS hst hcdt hcap + exact h + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/CnodeDelFullA.lean b/proofs/Fl4ProofsA64/P3/CnodeDelFullA.lean new file mode 100644 index 000000000..40db5a5dd --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/CnodeDelFullA.lean @@ -0,0 +1,90 @@ +/- +P3 (AArch64, A64-P3 CNode wave 3): **FL4's `cdt::delete(slot, true)` refines `cte_delete slot True`** (x86 +`P3/DelFull`): `finalise_slot_immediate` (`P3/CnodeDelImmA`), then an unconditional `empty_slot` with the cleanup cap +(`P3/CnodeEmptyClA`); and the spec's `cap_delete` (`fl4_cap_delete` at `Fn = fl4FinaliseCap`). + +* **`delete_exposedGA`** (`DelPostGA`): the joint post over `Fn` / `DelHypsA`: the slot emptied on `Ok`, the measure + strictly lower when it held a cap (the revoke loop's measure), the other slots kept, emptied or turned into zombies + at home, `W` on `Ok`, `Wp` on preemption. +* **`delete_exposed_capDeleteA`**: the same with the spec outcome of `capDeleteGA Fn slot` (`cap_delete`). + +`hCan`: every `OS` slot is canonical (P1 `DCan OS`, from `OSOK.canon` / `WinOK`; Stage-3 ledger). Named premises: none +new (`DelHypsA`'s hooks; `DStateA`'s [J] facts). +-/ +import Fl4ProofsA64.P3.CnodeDelImmA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +variable (st) in +/-- **What `cdt::delete(slot, true)` ends with** (x86 `DelPost`). -/ +def DelPostGA (Fn : FinFnA) (W Wp : DelWA S A) (th : TypedHeapA) (OS : Set U64) (s : S) (σ : Fl4SpecA64.State Unit) + (G : MdbGhostA) (sl : CslotPtr) (res : core.result.Result Unit error.Exc × S × A) : Prop := + ∃ σ' G', DStateA st th OS res.2.1 σ' G' ∧ (nnSA G').card ≤ (nnSA G).card ∧ psiDA st res.2.1 G' ≤ psiDA st s G ∧ + ((res.1 = .Ok () ∧ (∃ v, RDGA Fn (.cTEDeleteCall sl true) σ (.ok v, σ')) ∧ + capA st res.2.1 (cslotAddrA sl) = .Null ∧ + (capA st s (cslotAddrA sl) ≠ .Null → psiDA st res.2.1 G' < psiDA st s G) ∧ + (∀ y ∈ OS, y ≠ cslotAddrA sl → ChgZA st s res.2.1 y)) ∨ + (res.1 = .Err .Preempted ∧ RDGA Fn (.cTEDeleteCall sl true) σ (.error (), σ'))) ∧ + WPostA W Wp res.1 res.2.1 res.2.2 σ' G' + +/-- **FL4's exposed delete refines `cte_delete slot True`** (module docstring; x86 `delete_full`). -/ +theorem delete_exposedGA {ar : machine.Arch A} {T Ω Sg OS : Set U64} {th : TypedHeapA} + (hO : OSOK T Ω Sg OS) (hCan : ∀ x ∈ OS, obj.sext48 x = ok x) {Fn : FinFnA} {W Wx Wp : DelWA S A} + {Rd Rz : S → A → Fl4SpecA64.State Unit → MdbGhostA → U64 → Prop} + (H : DelHypsA st ar T th OS Fn W Wx Wp Rd Rz) (sl : CslotPtr) (hA : AbsSlotA th sl) + (s : S) (a : A) (σ : Fl4SpecA64.State Unit) (G : MdbGhostA) (D : DStateA st th OS s σ G) (hW : W s a σ G) : + cdt.delete st ar s a (cslotAddrA sl) true ⦃ res => DelPostGA st Fn W Wp th OS s σ G sl res ⦄ := by + unfold cdt.delete + simp only [↓reduceIte] + apply WP.spec_bind (finalise_slot_immediate_refinesGA hO hCan H sl hA s a σ G D hW) + rintro ⟨r1, s1, a1⟩ ⟨σ1, G1, D1, hcd1, hps1, hnul1, hres, hW1⟩ + simp only [uncurry_apply_pair] at D1 hcd1 hps1 hnul1 hres hW1 ⊢ + rcases hres with ⟨f, rfl, hcl, hclw, hclu, hrd, hcz1, hRd1, hWn1⟩ | ⟨rfl, hrd⟩ + · simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok] + have hWx1 : Wx s1 a1 σ1 G1 := hW1 + apply WP.spec_bind (empty_slot_clA hO H.empty H.post D1 hWx1 hWn1 hA hRd1 hcl hclw hclu) + rintro ⟨s2, a2⟩ ⟨σ2, G2, hes, D2, hx2, hoth2, hps2, hcd2, hlt2, hW2⟩ + simp only [uncurry_apply_pair] at hes D2 hx2 hoth2 hps2 hcd2 hlt2 hW2 ⊢ + refine let_pairDA s2 a2 (fun s2 a2 => ok (core.result.Result.Ok (), s2, a2)) _ ?_ + rw [WP.spec_ok] + obtain ⟨v, hv⟩ := rd_cte_okGA (σ2 := σ2) hrd (by simp only [or_true, NonDet.when, ↓reduceIte]; exact hes) + refine ⟨σ2, G2, D2, le_trans hcd2 hcd1, le_trans hps2 hps1, Or.inl ⟨rfl, ⟨v, hv⟩, hx2, fun h0 => ?_, + fun y hy hyx => (hcz1 y hy hyx).trans (ChgZA.of_eq (hoth2 y hy hyx))⟩, hW2⟩ + by_cases hn1 : capA st s1 (cslotAddrA sl) = .Null + · exact lt_of_le_of_lt hps2 (hnul1 h0 hn1) + · exact lt_of_lt_of_le (hlt2 hn1).2 hps1 + · simp only [core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame.from, + bind_ok, bind_tc_ok, WP.spec_ok] + exact ⟨σ1, G1, D1, hcd1, hps1, Or.inr ⟨rfl, rd_cte_errGA hrd⟩, hW1⟩ + +/-- **The exposed delete against the spec's `cap_delete`** (`capDeleteGA Fn`; `fl4_cap_delete` at +`Fn = fl4FinaliseCap`). -/ +theorem delete_exposed_capDeleteA {ar : machine.Arch A} {T Ω Sg OS : Set U64} {th : TypedHeapA} + (hO : OSOK T Ω Sg OS) (hCan : ∀ x ∈ OS, obj.sext48 x = ok x) {Fn : FinFnA} {W Wx Wp : DelWA S A} + {Rd Rz : S → A → Fl4SpecA64.State Unit → MdbGhostA → U64 → Prop} + (H : DelHypsA st ar T th OS Fn W Wx Wp Rd Rz) (sl : CslotPtr) (hA : AbsSlotA th sl) + (s : S) (a : A) (σ : Fl4SpecA64.State Unit) (G : MdbGhostA) (D : DStateA st th OS s σ G) (hW : W s a σ G) : + cdt.delete st ar s a (cslotAddrA sl) true ⦃ res => ∃ σ' G', DStateA st th OS res.2.1 σ' G' ∧ + ((res.1 = .Ok () ∧ (.ok (), σ') ∈ ((capDeleteGA Fn sl).run σ).1) ∨ + (res.1 = .Err .Preempted ∧ (.error (), σ') ∈ ((capDeleteGA Fn sl).run σ).1)) ∧ + WPostA W Wp res.1 res.2.1 res.2.2 σ' G' ⦄ := by + refine WP.spec_mono (delete_exposedGA hO hCan H sl hA s a σ G D hW) ?_ + rintro r ⟨σ', G', D', -, -, hres, hW'⟩ + rcases hres with ⟨h1, ⟨v, hv⟩, -⟩ | ⟨h1, hv⟩ + · exact ⟨σ', G', D', Or.inl ⟨h1, capDeleteGA_ok hv⟩, hW'⟩ + · exact ⟨σ', G', D', Or.inr ⟨h1, capDeleteGA_err hv⟩, hW'⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/CnodeDelHookA.lean b/proofs/Fl4ProofsA64/P3/CnodeDelHookA.lean new file mode 100644 index 000000000..c1ac4c6df --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/CnodeDelHookA.lean @@ -0,0 +1,270 @@ +/- +P3 (AArch64, A64-P3 CNode wave 1): **the object-finaliser hook of the deletion** (x86 `P3/DelW`'s +`FinObjHypWRcZG` / `DelHypsWRZG`, over a spec finaliser `Fn`). + +* **`FinObjKindA`**: the caps whose finalisation is another line's; the rest (null, untyped, IRQ / SC / domain / ASID + control, CNode, `Zombie`) only the CSpace sees, and **`finalise_cap_trivA`** proves them (a final CNode cap becomes + the zombie over it, a `Zombie` stays, the others leave nothing). +* **`FinHookA Fn W Wx Rd Rz`**: FL4's `finalise_cap … f false` of an object cap `c` refines `Fn ac`, `ac` the slot's + spec cap (`CapEqA c ac`: equal to `absCapA c` but at a mapped Stage-2 table cap; Architect ruling 2026-12-02 22:35Z). Architect ruling of + 2026-12-02 18:58Z (frames): `f` is FL4's flag, equal to `decide (isFinalCap' …)` for the caps that `NeedsFinCA` + (every kind here but a frame), and the spec outcome is at `decide …`; S3's frame producer accepts either `f`. + `exposed = false` (cdt.rs `finaliseSlot`). +* The preemption point (**`PPHypDA`**, producer MCS-18), the cleanup (**`PostDelHypA`**, producer the IRQ line), + the loop's own slot steps as the invariant `W` sees them (**`RdTrivHypA`**, **`ZombieHypA`**, **`SwapHypA`**, + **`EmptyHypA`**), and the bundle **`DelHypsA`**. + +Named premises: those of `DStateA` (`P3/CnodeFinalA`'s [J] facts). The hypotheses here are the Stage-3 hook's. +-/ +import Fl4ProofsA64.P3.CnodeDelInvA +import Fl4ProofsA64.P3.DelSpecA +import Fl4ProofsA64.P3.SwapA +import Fl4ProofsA64.P3.FinDefsA +import Fl4ProofsA64.P3.DelStepMdbA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 + +noncomputable section + +/-! ## Kinds, remainders, cleanups -/ + +/-! `FinObjKindA` (the caps whose finalisation is an object's; x86 `FinObjKind`) is S3's, in `P3/FinDefsA`. -/ + +/-- The remainder of `finalise_cap` with the flag `f`: a final CNode or thread cap leaves the zombie over it, a +`Zombie` stays, nothing else is left (x86 `finRem`). -/ +def finRemA (c : capability.Cap) (f : Bool) : capability.Cap := + match c with + | .CNode p r _ _ => if f then .Zombie p r ⟨BitVec.ofNat 64 (2 ^ r.val)⟩ else .Null + | .Thread t => if f then .Zombie t capability.ZOMBIE_TCB 7#u64 else .Null + | .Zombie p t n => .Zombie p t n + | _ => .Null + +/-- Cleanup caps: none, or an IRQ handler (x86 `CleanCap`; A64 has no I/O port). -/ +def CleanCapA : capability.Cap → Prop + | .Null | .IrqHandler _ => True + | _ => False + +/-- A removable remainder at `x` (FL4's `cap_removable`; x86 `RmOK`). -/ +def RmOKA (rem : capability.Cap) (x : U64) : Prop := + rem = .Null ∨ ∃ p zt n, rem = .Zombie p zt n ∧ (n.val = 0 ∨ (n.val = 1 ∧ p = x)) + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-- The invariant of the loop. -/ +abbrev DelWA (S A : Type) := S → A → Fl4SpecA64.State Unit → MdbGhostA → Prop + +/-- The invariant after a finalisation with remainder `rem` at `x`: `W` while the loop goes on, the window invariant +`Wx` when the remainder is removable (x86 `WF`). -/ +abbrev WFA (W Wx : DelWA S A) (rem : capability.Cap) (x : U64) (s : S) (a : A) (σ : Fl4SpecA64.State Unit) + (G : MdbGhostA) : Prop := + (¬ RmOKA rem x → W s a σ G) ∧ (RmOKA rem x → Wx s a σ G) + +/-- A write of slot words only (TCB words and `OS` slot words). -/ +abbrev SlotFrameA (T OS : Set U64) (s s' : S) : Prop := WritesIn st s s' (W7.CapW T {b | InOS OS b} ∅) + +variable (st) in +/-- **The object finaliser** (module docstring; x86 `FinObjHypWRcZG`). -/ +def FinHookA (ar : machine.Arch A) (th : TypedHeapA) (OS : Set U64) (Fn : FinFnA) (W Wx : DelWA S A) + (Rd Rz : S → A → Fl4SpecA64.State Unit → MdbGhostA → U64 → Prop) : Prop := + ∀ (s : S) (a : A) (σ : Fl4SpecA64.State Unit) (G : MdbGhostA) (sl : CslotPtr) (c : capability.Cap) + (ac : Fl4SpecA64.Cap) (f : Bool), + DStateA st th OS s σ G → W s a σ G → AbsSlotA th sl → capA st s (cslotAddrA sl) = c → FinObjKindA c → + getCap (Z := Unit) sl σ = ({(ac, σ)}, False) → CapEqA c ac → + (NeedsFinCA c → (f = true ↔ isFinalCap' ac σ)) → + objects.finalise_cap st ar s a c f false ⦃ r => + r.1.1 = finRemA c f ∧ CleanCapA r.1.2 ∧ CapWfA r.1.2 ∧ + ∃ σ' G', (∃ d : Bool, (d = true ↔ isFinalCap' ac σ) ∧ + ((absCapA r.1.1, absCapA r.1.2), σ') ∈ (Fn ac d σ).1) ∧ + DStateA st th OS r.2.1 σ' G' ∧ capA st r.2.1 (cslotAddrA sl) = c ∧ + (∀ y ∈ OS, y ≠ cslotAddrA sl → capA st r.2.1 y = capA st s y ∨ + (capA st r.2.1 y = .Null ∧ ¬ IsZombieA (capA st s y))) ∧ + WFA W Wx r.1.1 (cslotAddrA sl) r.2.1 r.2.2 σ' G' ∧ + (RmOKA r.1.1 (cslotAddrA sl) → Rd r.2.1 r.2.2 σ' G' (cslotAddrA sl)) ∧ + (∀ p zt n, r.1.1 = .Zombie p zt n → Rz r.2.1 r.2.2 σ' G' (cslotAddrA sl)) ⦄ + +/-- **`Fn` is FL4's spec finaliser on the caps the loop finalises itself** (x86 `FinAgree`). -/ +def FinAgreeA (Fn : FinFnA) : Prop := + ∀ c : capability.Cap, ¬ FinObjKindA c → CapWfA c → Fn (absCapA c) = fl4FinaliseCap (Z := Unit) (absCapA c) + +theorem finAgreeA_fl4 : FinAgreeA fl4FinaliseCap := fun _ _ _ => rfl + +variable (st) in +/-- **The preemption point** (producer MCS-18; x86 `PPHypDW`): every cap kept, a spec outcome of the same kind, `W` +after `Ok`, `Wp` after a preemption. -/ +def PPHypDA (ar : machine.Arch A) (th : TypedHeapA) (OS : Set U64) (W Wp : DelWA S A) : Prop := + ∀ (s : S) (a : A) (σ : Fl4SpecA64.State Unit) (G : MdbGhostA), DStateA st th OS s σ G → W s a σ G → + sched.preemption_point st ar s a ⦃ r => + (∀ y ∈ OS, capA st r.2.1 y = capA st s y) ∧ ∃ σ', + DStateA st th OS r.2.1 σ' G ∧ + ((r.1 = .Ok () ∧ (.ok (), σ') ∈ ((preemptionPoint (Z := Unit)).run σ).1 ∧ W r.2.1 r.2.2 σ' G) ∨ + (r.1 = .Err .Preempted ∧ (.error (), σ') ∈ ((preemptionPoint (Z := Unit)).run σ).1 ∧ + Wp r.2.1 r.2.2 σ' G)) ⦄ + +variable (st) in +/-- **The cleanup of a deleted cap** (producer the IRQ line; x86 `PostDelHypW`). No slot still holds the cleanup's IRQ +handler (the deleted cap was the final one, seL4 `isFinalCap`; Architect, 2026-12-04): the cleanup makes the IRQ +inactive, and `HandlersActiveA` asks every handler cap's IRQ active. -/ +def PostDelHypA (ar : machine.Arch A) (th : TypedHeapA) (OS : Set U64) (W : DelWA S A) : Prop := + ∀ (s : S) (a : A) (σ : Fl4SpecA64.State Unit) (G : MdbGhostA) (cl : capability.Cap), + DStateA st th OS s σ G → W s a σ G → CleanCapA cl → cl ≠ .Null → CapWfA cl → + (∀ i, cl = .IrqHandler i → ∀ y ∈ OS, capA st s y ≠ .IrqHandler i) → + objects.post_cap_deletion st ar s a cl ⦃ r => + (∀ y ∈ OS, capA st r.1 y = capA st s y) ∧ ∃ σ', + ((), σ') ∈ (postCapDeletion (Z := Unit) (absCapA cl) σ).1 ∧ DStateA st th OS r.1 σ' G ∧ W r.1 r.2 σ' G ⦄ + +variable (st) in +/-- **`Rd` at the slot of a removable cap the loop finalises itself** (x86 `RdTrivHyp`). -/ +def RdTrivHypA (th : TypedHeapA) (OS : Set U64) (W : DelWA S A) + (Rd : S → A → Fl4SpecA64.State Unit → MdbGhostA → U64 → Prop) : Prop := + ∀ (s : S) (a : A) (σ : Fl4SpecA64.State Unit) (G : MdbGhostA) (sl : CslotPtr) (c : capability.Cap), + DStateA st th OS s σ G → W s a σ G → AbsSlotA th sl → capA st s (cslotAddrA sl) = c → c ≠ .Null → + ¬ FinObjKindA c → + (∀ p r gs g, c = .CNode p r gs g → isFinalCap' (absCapA c) σ → r.val = 0 ∧ p = cslotAddrA sl) → + (∀ p zt n, c = .Zombie p zt n → n.val = 0 ∨ (n.val = 1 ∧ p = cslotAddrA sl)) → + Rd s a σ G (cslotAddrA sl) + +variable (st) in +/-- **A zombie written over the slot keeps `W`** (x86 `ZombieHypWZ`): the same zombie, the same counted down with the +deleted end slot null, or a thread / CNode cap replaced by a zombie covering its object. -/ +def ZombieHypA (T : Set U64) (th : TypedHeapA) (OS : Set U64) (W : DelWA S A) + (Rz : S → A → Fl4SpecA64.State Unit → MdbGhostA → U64 → Prop) : Prop := + ∀ (s s' : S) (a : A) (σ σ' : Fl4SpecA64.State Unit) (G : MdbGhostA) (sl : CslotPtr) (p zt n : U64), + W s a σ G → (∀ q, capA st s (cslotAddrA sl) = .Thread q → Rz s a σ G (cslotAddrA sl)) → + (∀ p' zt' n', capA st s (cslotAddrA sl) = .Zombie p' zt' n' → p' = p ∧ zt' = zt ∧ + (n'.val = n.val ∨ (n'.val = n.val + 1 ∧ + capA st s (cslotAddrA (specPtr p, natToCref (zombieCteBits (zombieBitsA zt)) n.val)) = .Null))) → + (¬ IsZombieA (capA st s (cslotAddrA sl)) → + (∃ q, capA st s (cslotAddrA sl) = .Thread q) ∨ ∃ q r gs g, capA st s (cslotAddrA sl) = .CNode q r gs g) → + DStateA st th OS s σ G → DStateA st th OS s' σ' G → SlotFrameA (st := st) T OS s s' → SpecSlotA σ σ' → + setCap (Z := Unit) (absCapA (.Zombie p zt n)) sl σ = ({((), σ')}, False) → AbsSlotA th sl → + capA st s (cslotAddrA sl) ≠ .Null → capA st s' (cslotAddrA sl) = .Zombie p zt n → + (∀ y ∈ OS, y ≠ cslotAddrA sl → capA st s' y = capA st s y) → + ZombMdbA st G.chains (cslotAddrA sl) s s' → + W s' a σ' G + +variable (st) in +/-- **A zombie swapped into its first slot keeps `W`** (x86 `SwapHypWZ`; the leaf `cap_swap_for_delete_jA`). -/ +def SwapHypA (T : Set U64) (th : TypedHeapA) (OS : Set U64) (W : DelWA S A) : Prop := + ∀ (s s' : S) (a : A) (σ σ' : Fl4SpecA64.State Unit) (G : MdbGhostA) (sl : CslotPtr) (p zt n : U64), + W s a σ G → 1 ≤ n.val → DStateA st th OS s σ G → + DStateA st th OS s' σ' (swGA G p (cslotAddrA sl)) → SlotFrameA (st := st) T OS s s' → SpecSlotA σ σ' → + ((), σ') ∈ (capSwapForDelete (Z := Unit) (homeSlA p zt) sl σ).1 → AbsSlotA th sl → + capA st s (cslotAddrA sl) = .Zombie p zt n → p ≠ cslotAddrA sl → + (∀ y ∈ OS, capA st s' y = if y = cslotAddrA sl then capA st s p + else if y = p then capA st s (cslotAddrA sl) else capA st s y) → + SwapMdbA st G.chains p (cslotAddrA sl) s s' → + W s' a σ' (swGA G p (cslotAddrA sl)) + +variable (st) in +/-- **A ready non-null slot emptied keeps `W`** (x86 `EmptyHypWR`; `empty_slot` with a `Null` cleanup, the leaf +`empty_slot_jA`). -/ +def EmptyHypA (T : Set U64) (th : TypedHeapA) (OS : Set U64) (W Wx : DelWA S A) + (Rd : S → A → Fl4SpecA64.State Unit → MdbGhostA → U64 → Prop) : Prop := + ∀ (s s' : S) (a : A) (σ σ' : Fl4SpecA64.State Unit) (G : MdbGhostA) (sl : CslotPtr), + Wx s a σ G → Rd s a σ G (cslotAddrA sl) → DStateA st th OS s σ G → + DStateA st th OS s' σ' (remGA G (cslotAddrA sl)) → SlotFrameA (st := st) T OS s s' → SpecSlotA σ σ' → + ((), σ') ∈ (emptySlot (Z := Unit) sl .nullCap σ).1 → AbsSlotA th sl → + capA st s (cslotAddrA sl) ≠ .Null → capA st s' (cslotAddrA sl) = .Null → + (∀ y ∈ OS, y ≠ cslotAddrA sl → capA st s' y = capA st s y) → + EmpMdbA st G.chains (cslotAddrA sl) s s' → + W s' a σ' (remGA G (cslotAddrA sl)) + +variable (st) in +/-- **The deletion's hypotheses over a spec finaliser `Fn`** (x86 `DelHypsWRZG`). -/ +structure DelHypsA (ar : machine.Arch A) (T : Set U64) (th : TypedHeapA) (OS : Set U64) (Fn : FinFnA) + (W Wx Wp : DelWA S A) (Rd Rz : S → A → Fl4SpecA64.State Unit → MdbGhostA → U64 → Prop) : Prop where + agree : FinAgreeA Fn + fin : FinHookA st ar th OS Fn W Wx Rd Rz + triv : RdTrivHypA st th OS W Rd + pp : PPHypDA st ar th OS W Wp + post : PostDelHypA st ar th OS W + zomb : ZombieHypA st T th OS W Rz + swap : SwapHypA st T th OS W + empty : EmptyHypA st T th OS W Wx Rd + /-- The loop's invariant gives the window's. -/ + wx : ∀ s a σ G, W s a σ G → Wx s a σ G + +/-! ## The caps the loop finalises itself -/ + +theorem shl_radixA {r : U64} (hr : r.val < 64) : + (1#u64 <<< (r &&& 63#u64)) = ok (⟨BitVec.ofNat 64 (2 ^ r.val)⟩ : U64) := by + have h63 : (r &&& 63#u64).val = r.val := by + have : (r &&& 63#u64).val = r.val % 64 := by simp; exact Nat.and_two_pow_sub_one_eq_mod r.val 6 + rw [this]; exact Nat.mod_eq_of_lt hr + obtain ⟨z, hz, hv, -⟩ := spec_existsA (U64.ShiftLeft_spec 1#u64 (r &&& 63#u64) (by rw [h63]; omega)) + rw [hz] + congr 1 + apply UScalar.eq_of_val_eq + have hlt : 2 ^ r.val < 2 ^ 64 := Nat.pow_lt_pow_right (by decide) hr + have h1 : (1#u64 : U64).val = 1 := rfl + rw [hv, h63, Nat.shiftLeft_eq, h1, Nat.one_mul] + show _ = (BitVec.ofNat 64 (2 ^ r.val)).toNat + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt hlt] + exact Nat.mod_eq_of_lt (by simpa [U64.size, U64.numBits] using hlt) + +/-- **The caps only the CSpace sees** (module docstring): FL4's `finalise_cap` returns the remainder `finRemA c f` +with no effect, and the spec's `finalise_cap` has that outcome at the spec's finality. -/ +theorem finalise_cap_trivA (ar : machine.Arch A) {th : TypedHeapA} {OS : Set U64} {s : S} {σ : Fl4SpecA64.State Unit} + {G : MdbGhostA} (D : DStateA st th OS s σ G) (a : A) {sl : CslotPtr} (hsl : AbsSlotA th sl) + {c : capability.Cap} (hc : capA st s (cslotAddrA sl) = c) (hk : ¬ FinObjKindA c) {f : Bool} + (hf : NeedsFinCA c → (f = true ↔ isFinalCap' (absCapA c) σ)) {d : Bool} + (hd : d = true ↔ isFinalCap' (absCapA c) σ) : + objects.finalise_cap st ar s a c f false = ok ((finRemA c f, .Null), s, a) ∧ + ((absCapA (finRemA c f), Cap.nullCap), σ) ∈ (fl4FinaliseCap (Z := Unit) (absCapA c) d σ).1 ∧ + CapWfA (finRemA c f) := by + have hwf : CapWfA c := hc ▸ D.wf (D.mem_slot hsl) + have plain : ¬ Fl4HookCapA (absCapA c) → fl4FinaliseCap (Z := Unit) (absCapA c) d = finaliseCap (absCapA c) d := + fun h => fl4FinaliseCap_plainA h d + have fd : NeedsFinCA c → f = d := fun h => Bool.eq_iff_iff.2 ((hf h).trans hd.symm) + cases c <;> simp only [FinObjKindA, not_true_eq_false] at hk <;> simp only [CapWfA] at hwf + case Null => + exact ⟨by simp [objects.finalise_cap, objects.finalise_cap_object, finRemA, uncurry_apply_pair], by rw [plain (by simp [absCapA, Fl4HookCapA])]; exact rfl, trivial⟩ + case Untyped => exact ⟨by simp [objects.finalise_cap, objects.finalise_cap_object, finRemA, uncurry_apply_pair], by rw [plain (by simp [absCapA, Fl4HookCapA])]; exact rfl, trivial⟩ + case IrqControl => exact ⟨by simp [objects.finalise_cap, objects.finalise_cap_object, finRemA, uncurry_apply_pair], by rw [plain (by simp [absCapA, Fl4HookCapA])]; exact rfl, trivial⟩ + case Domain => exact ⟨by simp [objects.finalise_cap, objects.finalise_cap_object, finRemA, uncurry_apply_pair], by rw [plain (by simp [absCapA, Fl4HookCapA])]; exact rfl, trivial⟩ + case SchedControl => exact ⟨by simp [objects.finalise_cap, objects.finalise_cap_object, finRemA, uncurry_apply_pair], by rw [plain (by simp [absCapA, Fl4HookCapA])]; exact rfl, trivial⟩ + case AsidControl => exact ⟨by simp [objects.finalise_cap, objects.finalise_cap_object, finRemA, uncurry_apply_pair], by rw [plain (by simp [absCapA, Fl4HookCapA])]; exact rfl, trivial⟩ + case CNode p r gs g => + have e := fd trivial + subst e + have h63 : (r &&& 63#u64).val < 64 := by + have : (r &&& 63#u64).val = r.val % 64 := by simp; exact Nat.and_two_pow_sub_one_eq_mod r.val 6 + omega + refine ⟨?_, ?_, ?_⟩ + · cases f + · simp [objects.finalise_cap, objects.finalise_cap_object, finRemA, uncurry_apply_pair] + · simp only [objects.finalise_cap, objects.finalise_cap_object, bind_ok, uncurry_apply_pair, ↓reduceIte, + lift, shl_radixA hwf.1, finRemA] + · rw [plain (by simp [absCapA, Fl4HookCapA])] + cases f + · exact rfl + · have hz : zombieBitsA r = some r.val := by + unfold zombieBitsA; rw [if_neg (by omega)] + have hn : (⟨BitVec.ofNat 64 (2 ^ r.val)⟩ : U64).val = 2 ^ r.val := by + show (BitVec.ofNat 64 (2 ^ r.val)).toNat = _ + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (Nat.pow_lt_pow_right (by decide) hwf.1)] + simp only [finRemA, ↓reduceIte, absCapA, hz, hn] + exact rfl + · cases f + · trivial + · exact Or.inl (by omega) + case Zombie p zt n => + have hfin : isFinalCap' (absCapA (.Zombie p zt n)) σ := + (finalHypA (st := st) th OS G.chains s σ sl _ D.toFinStA hsl hc trivial).2 ⟨p, zt, n, rfl⟩ + have e : d = true := hd.2 hfin + subst e + refine ⟨by simp [objects.finalise_cap, objects.finalise_cap_object, finRemA, uncurry_apply_pair], ?_, hwf⟩ + rw [plain (by simp [absCapA, Fl4HookCapA])] + show _ ∈ ((NonDet.assert (true = true) >>= fun _ => _) σ).1 + exact mem_bind_ndD (x := ((), σ)) (by simp [NonDet.assert]) rfl + all_goals exact absurd hwf id + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/CnodeDelImmA.lean b/proofs/Fl4ProofsA64/P3/CnodeDelImmA.lean new file mode 100644 index 000000000..5a9b35944 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/CnodeDelImmA.lean @@ -0,0 +1,676 @@ +/- +P3 (AArch64, A64-P3 CNode wave 3): **FL4's `finalise_slot_immediate` refines `finalise_slot slot True`** (x86 +`P3/DelImm.immediate_loopZG`). Each round finalises the cap; a zombie that is not removable deletes its last slot +(`delete_deferred`, l4v `reduce_zombie slot True`: `cte_delete end_slot False`, `P3/CnodeEmptyClA`) and, if the slot +still holds it, counts it down; then the preemption point. + +* A zombie's slots (`zSlotA`, `zSlot_absA`, `zslot_valA`) and **`zlast_nullA`**: after the last slot's deletion it is + null (a zombie at home there would be the same zombie at home with count `1`, removable, or a second zombie to the + same object, `DStateA.zu`). +* The measure **`muIA`** of the slot alone (`0` empty, the count of a zombie at home, `2^59 + 1 +` the count of a + zombie elsewhere, `2^60 + 2` otherwise; zombie counts are at most `2^59`, `zcount_leA`, as a CNode's radix is at + most `59`): every round lowers it, so `WALK_FUEL` never runs out. +* **`immediate_loopZGA`** (`ImmInvGA` → `ImmPostGA`) over a spec finaliser `Fn` and `DelHypsA`, and + **`finalise_slot_immediate_refinesGA`** from `WALK_FUEL`. + +`hCan` (every `OS` slot is a canonical address, P1 `DCan OS`): the inner `delete_deferred` of a zombie's last slot needs +it for that slot (the same row as `deferred_loopZGA`'s `hsl` in the Stage-3 ledger; P1 `OSOK.canon` gives it from +`WinOK`). Named premises: none new (`DelHypsA`'s hooks; `DStateA`'s [J] facts). +-/ +import Fl4ProofsA64.P3.CnodeEmptyClA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 + +noncomputable section + +/-! ## A zombie's slots -/ + +theorem natToCref_eqA (r i : Nat) (hr : r ≤ 64) : Fl4SpecA64.natToCref r i = bitsOfA (i % 2 ^ 64) r := by + unfold Fl4SpecA64.natToCref + rw [show Fl4SpecA64.wordBits = 64 from rfl, toBl_dropA _ _ hr] + simp + +theorem natToCref_length_cnA (r i : Nat) (hr : r ≤ 64) : (Fl4SpecA64.natToCref r i).length = r := by + rw [natToCref_eqA _ _ hr, length_bitsOfA] + +theorem cslotAddrA_natToCrefA (p : BitVec 64) (r i : Nat) (hr : r ≤ 64) (hi : i < 2 ^ r) : + cslotAddrA (p, Fl4SpecA64.natToCref r i) = slotAddrA p (BitVec.ofNat 64 i) := by + show slotAddrA p (Fl4SpecA64.ofBl (Fl4SpecA64.natToCref r i)) = _ + rw [natToCref_eqA _ _ hr, ofBl_bitsOfA _ _ hr] + have h1 : i < 2 ^ 64 := lt_of_lt_of_le hi (Nat.pow_le_pow_right (by decide) hr) + rw [Nat.mod_eq_of_lt h1, Nat.mod_eq_of_lt hi] + +theorem natToCref_tcbA : ∀ i < 7, Fl4SpecA64.natToCref 3 i = Fl4SpecA64.tcbCnodeIndex i := by + intro i hi + rw [natToCref_eqA _ _ (by decide)] + interval_cases i <;> first | rfl | decide + +/-- The zombie's slot `i` as the spec names it (`(ptr, nat_to_cref (zombie_cte_bits bits) i)`). -/ +abbrev zSlotA (p zt : U64) (i : Nat) : CslotPtr := + (specPtr p, Fl4SpecA64.natToCref (zombieCteBits (zombieBitsA zt)) i) + +section +variable {th : TypedHeapA} + +theorem zSlot_absA {p zt n : U64} (h : ZOKA th p zt n) {i : Nat} (hi : i < n.val) : + AbsSlotA th (zSlotA p zt i) ∧ cslotAddrA (zSlotA p zt i) = slotAddrA (specPtr p) (BitVec.ofNat 64 i) := by + rcases h with ⟨h65, ht, hn⟩ | ⟨h64, hc, hn⟩ + · have hz : zombieBitsA zt = none := by simp [zombieBitsA, h65] + have e : zombieCteBits (zombieBitsA zt) = 3 := by rw [hz]; rfl + have hi7 : i < tcbSlotsA := lt_of_lt_of_le hi hn + have hi7' : i < 7 := hi7 + refine ⟨⟨p, rfl, Or.inr ⟨ht, i, hi7, ?_⟩⟩, ?_⟩ + · show Fl4SpecA64.natToCref (zombieCteBits (zombieBitsA zt)) i = _ + rw [e]; exact natToCref_tcbA i hi7' + · show cslotAddrA (specPtr p, Fl4SpecA64.natToCref (zombieCteBits (zombieBitsA zt)) i) = _ + rw [e] + exact cslotAddrA_natToCrefA _ 3 i (by decide) (by omega) + · have hz : zombieBitsA zt = some zt.val := by unfold zombieBitsA; rw [if_neg (by omega)] + have e : zombieCteBits (zombieBitsA zt) = zt.val := by rw [hz]; rfl + refine ⟨⟨p, rfl, Or.inl ?_⟩, ?_⟩ + · show th p = some (.cnode (Fl4SpecA64.natToCref (zombieCteBits (zombieBitsA zt)) i).length) + rw [e, natToCref_length_cnA _ _ h64]; exact hc + · show cslotAddrA (specPtr p, Fl4SpecA64.natToCref (zombieCteBits (zombieBitsA zt)) i) = _ + rw [e] + exact cslotAddrA_natToCrefA _ zt.val i h64 (by omega) + +/-- A zombie's slot `i` is at `p + 32 i`, inside the zombie's object. -/ +theorem zslot_valA {kb : U64} (hP : PlaceA th kb) {p zt n : U64} (h : ZOKA th p zt n) {i : Nat} (hi : i < n.val) : + (cslotAddrA (zSlotA p zt i)).val = p.val + i * 32 ∧ ∃ k, th p = some k ∧ i * 32 + 32 ≤ k.size := by + obtain ⟨-, hE⟩ := zSlot_absA h hi + obtain ⟨k, hk, hb⟩ : ∃ k, th p = some k ∧ i * 32 + 32 ≤ k.size := by + rcases h with ⟨-, ht, hn⟩ | ⟨-, hc, hn⟩ + · refine ⟨_, ht, ?_⟩ + show i * 32 + 32 ≤ 4096 + unfold tcbSlotsA at hn; omega + · refine ⟨_, hc, ?_⟩ + have e : (ObjKindA.cnode zt.val).size = 2 ^ zt.val * 32 := by + show 2 ^ (zt.val + 5) = _ + rw [pow_add]; norm_num + rw [e] + have h1 : i + 1 ≤ 2 ^ zt.val := by omega + have h2 := Nat.mul_le_mul_right 32 h1 + omega + refine ⟨?_, k, hk, hb⟩ + have hf := hP.fit p k hk + have hlt : p.val + i * 32 < 2 ^ 64 := by omega + rw [hE, slotAddrA_sp] + show (p.bv + BitVec.ofNat 64 i * 32#64).toNat = _ + have hp' : p.bv.toNat = p.val := rfl + rw [BitVec.toNat_add, BitVec.toNat_mul, BitVec.toNat_ofNat, hp'] + have h32 : (32#64).toNat = 32 := rfl + rw [h32, Nat.mod_eq_of_lt (show i < 2 ^ 64 by omega), Nat.mod_eq_of_lt (show i * 32 < 2 ^ 64 by omega), + Nat.mod_eq_of_lt hlt] + +theorem cnode_radix_leA {kb : U64} (hP : PlaceA th kb) {p : U64} {r : Nat} (h : th p = some (.cnode r)) : + r ≤ 59 := by + have hf := hP.fit p _ h + have e : (ObjKindA.cnode r).size = 2 ^ (r + 5) := rfl + rw [e] at hf + by_contra hc + have : 2 ^ 65 ≤ 2 ^ (r + 5) := Nat.pow_le_pow_right (by decide) (by omega) + have h2 : (2 : Nat) ^ 64 < 2 ^ 65 := by norm_num + omega + +end + +theorem wsub1A (n : U64) (h : 1 ≤ n.val) : (core.num.U64.wrapping_sub n 1#u64).val = n.val - 1 := by + rw [core.num.U64.wrapping_sub_val_eq] + have hb : n.val < 2 ^ 64 := n.hBounds + have hs : UScalar.size UScalarTy.U64 = 2 ^ 64 := by simp [U64.size, U64.numBits] + rw [hs, show (1#u64 : U64).val = 1 from rfl] + rw [show n.val + (2 ^ 64 - 1) = (n.val - 1) + 2 ^ 64 by omega, Nat.add_mod_right] + exact Nat.mod_eq_of_lt (by omega) + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {th : TypedHeapA} {OS : Set U64} {s : S} {σ : Fl4SpecA64.State Unit} {G : MdbGhostA} + +/-- A zombie's count is at most `2 ^ 59`. -/ +theorem zcount_leA (D : DStateA st th OS s σ G) {x p zt n : U64} (hx : x ∈ OS) + (h : capA st s x = .Zombie p zt n) : n.val ≤ 2 ^ 59 := by + have hxs : IsSlotA th x := D.isSlot_of hx (fun e => by rw [h] at e; cases e) + rcases D.zw x hxs p zt n h with ⟨-, -, hn⟩ | ⟨-, hc, hn⟩ + · unfold tcbSlotsA at hn; omega + · have h1 := cnode_radix_leA D.place hc + have h2 : 2 ^ zt.val ≤ 2 ^ 59 := Nat.pow_le_pow_right (by decide) h1 + omega + +/-- **A zombie counted down leaves its last slot null** (module docstring; x86 `zlast_null`). -/ +theorem zlast_nullA (D : DStateA st th OS s σ G) {x p zt n : U64} (hxO : x ∈ OS) + (hx : capA st s x = .Zombie p zt n) {m : Nat} (hm : m + 1 = n.val) (hrm : ¬ (n.val = 1 ∧ p = x)) + (h : capA st s (cslotAddrA (zSlotA p zt m)) = .Null ∨ HomeZA st s (cslotAddrA (zSlotA p zt m))) : + capA st s (cslotAddrA (zSlotA p zt m)) = .Null := by + rcases h with h | ⟨zt', n', e⟩ + · exact h + exfalso + have hxs : IsSlotA th x := D.isSlot_of hxO (fun e => by rw [hx] at e; cases e) + have hz := D.zw x hxs p zt n hx + have hmn : m < n.val := by omega + obtain ⟨hEa, -⟩ := zSlot_absA hz hmn + obtain ⟨hyv, kp, hkp, hb⟩ := zslot_valA D.place hz hmn + have hyO := D.mem_slot hEa + have hys : IsSlotA th (cslotAddrA (zSlotA p zt m)) := hEa.isSlot D.place + have hz' := D.zw _ hys _ _ _ e + by_cases hyp : cslotAddrA (zSlotA p zt m) = p + · have hm0 : m = 0 := by + have := congrArg UScalar.val hyp + omega + by_cases hxy : x = cslotAddrA (zSlotA p zt m) + · rw [← hxy, hx] at e + simp only [capability.Cap.Zombie.injEq] at e + exact hrm ⟨by omega, e.1⟩ + · exact D.zu hxO hyO hxy hx e hyp.symm + · obtain ⟨k, hk⟩ : ∃ k, th (cslotAddrA (zSlotA p zt m)) = some k := by + rcases hz' with ⟨-, h1, -⟩ | ⟨-, h1, -⟩ <;> exact ⟨_, h1⟩ + rcases D.place.apart p _ kp k hkp hk (Ne.symm hyp) with h1 | h1 + · omega + · exact hyp (UScalar.eq_of_val_eq (by omega)) + +/-! ## The measure of the immediate loop -/ + +variable (st) in +/-- **The measure of the immediate loop at the slot `x`** (module docstring; x86 `muI` at `K = 2 ^ 59`). -/ +def muIA (s : S) (x : U64) : Nat := + match capA st s x with + | .Null => 0 + | .Zombie p _ n => if p = x then n.val else 2 ^ 59 + 1 + n.val + | _ => 2 ^ 60 + 2 + +theorem muIA_zombie {x p zt n : U64} (h : capA st s x = .Zombie p zt n) : + muIA st s x = if p = x then n.val else 2 ^ 59 + 1 + n.val := by + unfold muIA; rw [h] + +theorem muIA_null {x : U64} (h : capA st s x = .Null) : muIA st s x = 0 := by + unfold muIA; rw [h] + +theorem muIA_other {x : U64} {c : capability.Cap} (h : capA st s x = c) (hn : c ≠ .Null) (hz : ¬ IsZombieA c) : + muIA st s x = 2 ^ 60 + 2 := by + unfold muIA; rw [h] + cases c <;> first | rfl | exact absurd rfl hn | exact absurd ⟨_, _, _, rfl⟩ hz + +theorem muIA_leA (D : DStateA st th OS s σ G) {x : U64} (hx : x ∈ OS) : muIA st s x ≤ 2 ^ 60 + 2 := by + by_cases hz : IsZombieA (capA st s x) + · obtain ⟨p, zt, n, e⟩ := hz + have := zcount_leA D hx e + rw [muIA_zombie e]; split_ifs <;> omega + · by_cases hn : capA st s x = .Null + · rw [muIA_null hn]; omega + · rw [muIA_other rfl hn hz] + +/-- A round that counts the zombie in the slot down lowers `muIA` (x86 `muI_dec`). -/ +theorem muI_decA {s s' : S} {x p zt n w : U64} {c : capability.Cap} (hc : capA st s x = c) (hcn : c ≠ .Null) + (hzr : IsZombieA c → c = .Zombie p zt n) (hnK : n.val ≤ 2 ^ 59) (hn1 : 1 ≤ n.val) + (h' : capA st s' x = .Zombie p zt w) (hw : w.val = n.val - 1) : muIA st s' x + 1 ≤ muIA st s x := by + rw [muIA_zombie h', hw] + by_cases hzc : IsZombieA c + · rw [muIA_zombie (hc.trans (hzr hzc))]; split_ifs <;> omega + · rw [muIA_other hc hcn hzc]; split_ifs <;> omega + +/-- A round after which the slot is empty or holds a zombie at home, when the zombie it made was not at home, lowers +`muIA` (x86 `muI_chg`). -/ +theorem muI_chgA {s s' : S} {x p zt n : U64} {c : capability.Cap} (hc : capA st s x = c) (hcn : c ≠ .Null) + (hzr : IsZombieA c → c = .Zombie p zt n) (hpx : p ≠ x) + (h' : capA st s' x = .Null ∨ HomeZA st s' x) + (hle : ∀ p' zt' n', capA st s' x = .Zombie p' zt' n' → n'.val ≤ 2 ^ 59) : + muIA st s' x + 1 ≤ muIA st s x := by + have h1 : muIA st s' x ≤ 2 ^ 59 := by + rcases h' with e | ⟨zt', n', e⟩ + · rw [muIA_null e]; omega + · rw [muIA_zombie e, if_pos rfl]; exact hle _ _ _ e + by_cases hzc : IsZombieA c + · rw [muIA_zombie (hc.trans (hzr hzc)), if_neg hpx]; omega + · rw [muIA_other hc hcn hzc]; omega + +/-! ## How the other slots change -/ + +variable (st) in +/-- A slot's cap kept, emptied, or a zombie at home (x86 `ChgZ`). -/ +def ChgZA (s0 s : S) (y : U64) : Prop := + capA st s y = capA st s0 y ∨ capA st s y = .Null ∨ HomeZA st s y + +theorem ChgZA.trans {s0 s1 s2 : S} {y : U64} (h1 : ChgZA st s0 s1 y) (h2 : ChgZA st s1 s2 y) : ChgZA st s0 s2 y := by + rcases h2 with e | h | h + · rcases h1 with e1 | n1 | ⟨zt, n, z1⟩ + · exact Or.inl (e.trans e1) + · exact Or.inr (Or.inl (e.trans n1)) + · exact Or.inr (Or.inr ⟨zt, n, e.trans z1⟩) + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr h) + +theorem ChgZA.of_eq {s0 s1 : S} {y : U64} (h : capA st s1 y = capA st s0 y) : ChgZA st s0 s1 y := Or.inl h + +theorem ChgZA.of_chgOK {s0 s1 : S} {y : U64} (h : ChgOKA st s0 s1 y) : ChgZA st s0 s1 y := by + rcases h with e | ⟨h | h, -⟩ + · exact Or.inl e + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr h) + +theorem ChgZA.of_fin {s0 s1 : S} {y : U64} + (h : capA st s1 y = capA st s0 y ∨ (capA st s1 y = .Null ∧ ¬ IsZombieA (capA st s0 y))) : ChgZA st s0 s1 y := by + rcases h with e | ⟨e, -⟩ + · exact Or.inl e + · exact Or.inr (Or.inl e) + +end + +/-! ## The immediate loop -/ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +variable (st) in +/-- **The immediate loop's invariant** (x86 `ImmInvG`). -/ +def ImmInvGA (Fn : FinFnA) (W : DelWA S A) (th : TypedHeapA) (OS : Set U64) (sl : CslotPtr) (s0 : S) + (σ0 : Fl4SpecA64.State Unit) (G0 : MdbGhostA) (x : S × A × U64) : Prop := + ∃ σ G, DStateA st th OS x.1 σ G ∧ muIA st x.1 (cslotAddrA sl) < x.2.2.val ∧ (nnSA G).card ≤ (nnSA G0).card ∧ + psiDA st x.1 G ≤ psiDA st s0 G0 ∧ + (capA st s0 (cslotAddrA sl) ≠ .Null → capA st x.1 (cslotAddrA sl) = .Null → psiDA st x.1 G < psiDA st s0 G0) ∧ + (∀ y, RDGA Fn (.finaliseSlotCall sl true) σ y → RDGA Fn (.finaliseSlotCall sl true) σ0 y) ∧ + (∀ y ∈ OS, y ≠ cslotAddrA sl → ChgZA st s0 x.1 y) ∧ W x.1 x.2.1 σ G + +variable (st) in +/-- **What the immediate loop ends with** (x86 `ImmPostG`). -/ +def ImmPostGA (Fn : FinFnA) (W Wx Wp : DelWA S A) (Rd : S → A → Fl4SpecA64.State Unit → MdbGhostA → U64 → Prop) + (th : TypedHeapA) (OS : Set U64) (sl : CslotPtr) (s0 : S) (σ0 : Fl4SpecA64.State Unit) (G0 : MdbGhostA) + (res : core.result.Result cdt.Finalised error.Exc × S × A) : Prop := + ∃ σ' G', DStateA st th OS res.2.1 σ' G' ∧ (nnSA G').card ≤ (nnSA G0).card ∧ + psiDA st res.2.1 G' ≤ psiDA st s0 G0 ∧ + (capA st s0 (cslotAddrA sl) ≠ .Null → capA st res.2.1 (cslotAddrA sl) = .Null → + psiDA st res.2.1 G' < psiDA st s0 G0) ∧ + ((∃ f, res.1 = .Ok f ∧ CleanCapA f.cleanup ∧ CapWfA f.cleanup ∧ + (∀ i, f.cleanup = .IrqHandler i → ∀ y ∈ OS, y ≠ cslotAddrA sl → capA st res.2.1 y ≠ .IrqHandler i) ∧ + RDGA Fn (.finaliseSlotCall sl true) σ0 (.ok (true, absCapA f.cleanup), σ') ∧ + (∀ y ∈ OS, y ≠ cslotAddrA sl → ChgZA st s0 res.2.1 y) ∧ + (capA st res.2.1 (cslotAddrA sl) ≠ .Null → Rd res.2.1 res.2.2 σ' G' (cslotAddrA sl)) ∧ + (capA st res.2.1 (cslotAddrA sl) = .Null → W res.2.1 res.2.2 σ' G')) ∨ + (res.1 = .Err .Preempted ∧ RDGA Fn (.finaliseSlotCall sl true) σ0 (.error (), σ'))) ∧ + WPostA Wx Wp res.1 res.2.1 res.2.2 σ' G' + +section +open Classical + +set_option maxHeartbeats 8000000 in +/-- **FL4's immediate finalisation loop refines `finalise_slot slot True`** (module docstring; x86 +`immediate_loopZG`). -/ +theorem immediate_loopZGA {ar : machine.Arch A} {T Ω Sg OS : Set U64} {th : TypedHeapA} + (hO : OSOK T Ω Sg OS) (hCan : ∀ x ∈ OS, obj.sext48 x = ok x) {Fn : FinFnA} {W Wx Wp : DelWA S A} + {Rd Rz : S → A → Fl4SpecA64.State Unit → MdbGhostA → U64 → Prop} + (H : DelHypsA st ar T th OS Fn W Wx Wp Rd Rz) + (sl : CslotPtr) (hA : AbsSlotA th sl) + (s0 : S) (σ0 : Fl4SpecA64.State Unit) (G0 : MdbGhostA) (s : S) (a : A) (fuel : U64) + (hinv : ImmInvGA st Fn W th OS sl s0 σ0 G0 (s, a, fuel)) : + cdt.finalise_slot_immediate_loop st ar s a (cslotAddrA sl) fuel ⦃ res => + ImmPostGA st Fn W Wx Wp Rd th OS sl s0 σ0 G0 res ⦄ := by + unfold cdt.finalise_slot_immediate_loop + refine loop.spec_decr_nat (fun x => x.2.2.val) (ImmInvGA st Fn W th OS sl s0 σ0 G0) _ _ _ ?_ hinv + rintro ⟨s, a, fuel⟩ ⟨σ, G, hDS, hlt, hcd, hps, hnul, htr, hcz, hW⟩ + replace hlt : muIA st s (cslotAddrA sl) < fuel.val := hlt + replace hcd : (nnSA G).card ≤ (nnSA G0).card := hcd + replace hps : psiDA st s G ≤ psiDA st s0 G0 := hps + replace hnul : capA st s0 (cslotAddrA sl) ≠ .Null → capA st s (cslotAddrA sl) = .Null → + psiDA st s G < psiDA st s0 G0 := hnul + replace hcz : ∀ y ∈ OS, y ≠ cslotAddrA sl → ChgZA st s0 s y := hcz + replace hW : W s a σ G := hW + unfold cdt.finalise_slot_immediate_loop.body + have hf : fuel > 0#u64 := by scalar_tac + obtain ⟨fuel1, hv, hv1⟩ := spec_existsA (U64.sub_spec (x := fuel) (y := 1#u64) (by scalar_tac)) + have hv1' : fuel1.val + 1 = fuel.val := by scalar_tac + simp only [hf, ↓reduceIte, hv, bind_ok, capA_eq] + have hxO := hDS.mem_slot hA + obtain ⟨ac, hg, hace⟩ := getCapOne_eqA hDS.rel hA + obtain ⟨c, hc⟩ : ∃ c, capA st s (cslotAddrA sl) = c := ⟨_, rfl⟩ + rw [hc] at hace ⊢ + try simp only [bind_ok] + by_cases hcn : c = .Null + · subst hcn + have hacn : ac = Fl4SpecA64.Cap.nullCap := (hace.eq_abs fun _ _ _ e => by cases e).symm + rw [hacn] at hg + rw [cap_is_null_nullA]; simp only [bind_ok, ↓reduceIte, WP.spec_ok] + exact ⟨σ, G, hDS, hcd, hps, hnul, Or.inl ⟨_, rfl, trivial, trivial, (fun _ h => by cases h), htr _ (rd_fin_nullGA hg), hcz, + fun h => absurd hc h, fun _ => hW⟩, H.wx _ _ _ _ hW⟩ + rw [cap_is_null_neA hcn]; simp only [bind_ok, Bool.false_eq_true, ↓reduceIte] + have hcan : ac ≠ .nullCap := hace.ne_null hcn (hc ▸ hDS.wf hxO) + obtain ⟨bf, hbf1, hbf2a⟩ : ∃ bf : Bool, cdt.is_final_capability_with_cap st s (cslotAddrA sl) c = ok bf ∧ + (NeedsFinCA c → (bf = true ↔ isFinalCap' (absCapA c) σ)) := by + by_cases hN : NeedsFinCA c + · obtain ⟨⟨b, hb, e⟩, -⟩ := finalHypA th OS G.chains s σ sl c hDS.toFinStA hA hc hN + exact ⟨b, hb, fun _ => e⟩ + · obtain ⟨b, hb, -⟩ := spec_existsA (P0.cdt.is_final_capability_with_cap.p0 st s (cslotAddrA sl) c L.total) + exact ⟨b, hb, fun h => absurd h hN⟩ + rw [hbf1]; simp only [bind_ok] + have hbf2 : NeedsFinCA c → (bf = true ↔ isFinalCap' ac σ) := fun h => (hbf2a h).trans (isFinalCap'_eqA hace σ).symm + apply WP.spec_bind (fin_stepA H hDS hW hA hc hcn hg hace hbf2) + rintro ⟨⟨rem, cl⟩, s1, a1⟩ ⟨hrem, hcl, hclw, σ1, G1, ⟨d, hd, hfc⟩, D1, hx1, hch1, hWF, hRd1, hRz1, hirq1⟩ + try simp only [uncurry_apply_pair] at hrem hcl hclw hx1 hch1 hWF hRd1 hRz1 hfc D1 ⊢ + try dsimp only at hrem hcl hclw hx1 hch1 hWF hRd1 hRz1 hfc D1 ⊢ + have hdd : d = decide (isFinalCap' ac σ) := Bool.eq_iff_iff.2 (hd.trans decide_eq_true_iff.symm) + have hfc' : ((absCapA rem, absCapA cl), σ1) ∈ (Fn ac (decide (isFinalCap' ac σ)) σ).1 := by + rw [← hdd]; exact hfc + have hx1' : capA st s1 (cslotAddrA sl) = capA st s (cslotAddrA sl) := hx1.trans hc.symm + obtain ⟨hps1, hcd1⟩ := psiD_finA hDS.lk D1.lk hx1' hch1 + have hcz1 : ∀ y ∈ OS, y ≠ cslotAddrA sl → ChgZA st s0 s1 y := + fun y hy hyx => (hcz y hy hyx).trans (ChgZA.of_fin (hch1 y hy hyx)) + have hnul1 : capA st s0 (cslotAddrA sl) ≠ .Null → capA st s1 (cslotAddrA sl) = .Null → + psiDA st s1 G1 < psiDA st s0 G0 := + fun _ h => absurd (hx1.symm.trans h) hcn + rcases finRemA_cases c bf with hN | ⟨p, zt, n, hZ⟩ + · have hN' : rem = .Null := hrem.trans hN + subst hN' + simp only [cdt.cap_removable, bind_ok, ↓reduceIte, WP.spec_ok] + exact ⟨σ1, G1, D1, le_trans hcd1 hcd, le_trans hps1 hps, hnul1, Or.inl ⟨_, rfl, hcl, hclw, hirq1, + htr _ (rd_fin_removableGA hg hcan hfc' trivial), hcz1, fun _ => hRd1 (Or.inl rfl), + fun h => absurd (hx1.symm.trans h) hcn⟩, hWF.2 (Or.inl rfl)⟩ + -- a zombie remains + have hZ' : rem = .Zombie p zt n := hrem.trans hZ + subst hZ' + obtain ⟨hcanp, hwfz, hzok, hRk, hpc, hcls, hu⟩ := zombie_factsA hDS hA hc hbf2a hZ + (fun y hy hne => (hch1 y hy hne).imp_right And.left) + obtain ⟨b, hrm, hrmS⟩ := cap_removable_zombieA hDS.place hzok hA + have hrmv' := capRemoveable_zombieA hDS.place hzok hA + rw [hrm]; simp only [bind_ok] + by_cases hb : b = true + · rw [if_pos hb] + simp only [WP.spec_ok] + have hrmv : n.val = 0 ∨ (n.val = 1 ∧ p = cslotAddrA sl) := hrmv'.1 (hrmS.1 hb) + exact ⟨σ1, G1, D1, le_trans hcd1 hcd, le_trans hps1 hps, hnul1, Or.inl ⟨_, rfl, hcl, hclw, hirq1, + htr _ (rd_fin_removableGA hg hcan hfc' (hrmv'.2 hrmv)), hcz1, + fun _ => hRd1 (Or.inr ⟨p, zt, n, rfl, hrmv⟩), fun h => absurd (hx1.symm.trans h) hcn⟩, + hWF.2 (Or.inr ⟨p, zt, n, rfl, hrmv⟩)⟩ + rw [if_neg hb] + have hrmv : ¬ (n.val = 0 ∨ (n.val = 1 ∧ p = cslotAddrA sl)) := fun h => hb (hrmS.2 (hrmv'.2 h)) + have hnrm : ¬ capRemoveable (absCapA (.Zombie p zt n)) sl := fun h => hrmv (hrmv'.1 h) + have hW1 := hWF.1 (fun h => by + rcases h with e | ⟨p', zt', n', e, h⟩ + · cases e + · cases e; exact hrmv h) + have hxn1 : capA st s1 (cslotAddrA sl) ≠ .Null := by rw [hx1]; exact hcn + have hsame : ∀ p' zt' n', capA st s1 (cslotAddrA sl) = .Zombie p' zt' n' → p' = p ∧ zt' = zt ∧ n' = n := by + intro p' zt' n' hq + have hc' : c = .Zombie p' zt' n' := hx1.symm.trans hq + have hZ2 := hZ + rw [hc'] at hZ2 + simp only [finRemA, capability.Cap.Zombie.injEq] at hZ2 + exact hZ2 + have hshape : ∀ p' zt' n', capA st s1 (cslotAddrA sl) = .Zombie p' zt' n' → p' = p ∧ zt' = zt ∧ + (n'.val = n.val ∨ (n'.val = n.val + 1 ∧ + capA st s1 (cslotAddrA (specPtr p, natToCref (zombieCteBits (zombieBitsA zt)) n.val)) = .Null)) := by + intro p' zt' n' hq + obtain ⟨e1, e2, e3⟩ := hsame p' zt' n' hq + exact ⟨e1, e2, Or.inl (by rw [e3])⟩ + have hsrc := finRemA_zombie hZ + have hnz : ¬ IsZombieA (capA st s1 (cslotAddrA sl)) → (∃ q, capA st s1 (cslotAddrA sl) = .Thread q) ∨ + ∃ q r gs g, capA st s1 (cslotAddrA sl) = .CNode q r gs g := by + intro hnz + rcases hsrc with ⟨q, hq⟩ | ⟨q, r, gs, g, hq⟩ | ⟨m, hq, -⟩ + · exact Or.inl ⟨q, by rw [hx1, hq]⟩ + · exact Or.inr ⟨q, r, gs, g, by rw [hx1, hq]⟩ + · exact absurd ⟨p, zt, m, by rw [hx1, hq]⟩ hnz + have hzr : IsZombieA c → c = .Zombie p zt n := by + rintro ⟨p', t', n', hq⟩ + rcases hsrc with ⟨q, h⟩ | ⟨q, r, gs, g, h⟩ | ⟨m, h, hm⟩ + · rw [h] at hq; cases hq + · rw [h] at hq; cases hq + · rw [h, hm] + apply WP.spec_bind (set_zombie_sA hO D1 hA hcanp hwfz hzok (by rw [hx1]; exact hRk) (by rw [hx1]; exact hpc) + (by rw [hx1]; exact hcls) (hu.imp_right fun ⟨n0, h⟩ => ⟨n0, by rw [hx1]; exact h⟩)) + rintro s2 ⟨hwp, cx, co, σ2, hset, hSS2, D2, hZm⟩ + have hW2 : W s2 a1 σ2 G1 := H.zomb s1 s2 a1 σ1 σ2 G1 sl p zt n hW1 + (fun q hq => hRz1 q (hx1.symm.trans hq) p zt n rfl) hshape hnz D1 D2 hwp hSS2 hset hA hxn1 cx co hZm + have hps2 : psiDA st s2 G1 ≤ psiDA st s1 G1 := by + refine psiDA_le D1.lk D2.lk (fun y hy h => ?_) (fun y hy _ hh => ?_) + · by_cases e : y = cslotAddrA sl + · rw [e]; exact hxn1 + · rw [← co y hy e]; exact h + · by_cases e : y = cslotAddrA sl + · subst e + rintro ⟨zt', n', hq⟩ + obtain ⟨e1, e2, -⟩ := hsame _ zt' n' hq + exact hh ⟨zt, n, by rw [cx, ← e1]⟩ + · rintro ⟨zt', n', hq⟩ + exact hh ⟨zt', n', by rw [co y hy e]; exact hq⟩ + have hcz2 : ∀ y ∈ OS, y ≠ cslotAddrA sl → ChgZA st s0 s2 y := + fun y hy hyx => (hcz1 y hy hyx).trans (ChgZA.of_eq (co y hy hyx)) + have hnK : n.val ≤ 2 ^ 59 := zcount_leA D2 hxO cx + -- the zombie's last slot + have hn1 : 1 ≤ n.val := by + by_contra h; exact hrmv (Or.inl (by omega)) + obtain ⟨m, hm⟩ : ∃ m, n.val = m + 1 := ⟨n.val - 1, by omega⟩ + have hwv := wsub1A n hn1 + have hmn : m < n.val := by omega + obtain ⟨hEa, hEaddr⟩ := zSlot_absA hzok hmn + have hidx : obj.Slot.index p (core.num.U64.wrapping_sub n 1#u64) = ok (cslotAddrA (zSlotA p zt m)) := by + obtain ⟨v, hv0, hve⟩ := spec_existsA (slot_index_specA p (core.num.U64.wrapping_sub n 1#u64)) + rw [hv0, hve, hEaddr] + congr 2 + apply BitVec.eq_of_toNat_eq + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by have hb : n.val < 2 ^ 64 := n.hBounds; omega)] + show (core.num.U64.wrapping_sub n 1#u64).val = m + omega + simp only [lift, bind_ok, hidx] + have hzS : absCapA (.Zombie p zt n) = Fl4SpecA64.Cap.zombie (specPtr p) (zombieBitsA zt) (m + 1) := by + show Fl4SpecA64.Cap.zombie (specPtr p) (zombieBitsA zt) n.val = _ + rw [hm] + have hcy' : ¬ (capCyclicZombie (absCapA (.Zombie p zt n)) sl ∧ true = false) := fun h => by simp at h + have hEO : cslotAddrA (zSlotA p zt m) ∈ OS := D2.mem_slot hEa + apply WP.spec_bind (delete_deferred_refinesGA hO H (zSlotA p zt m) hEa (hCan _ hEO) s2 a1 σ2 G1 D2 hW2) + rintro ⟨r3, s3, a3⟩ ⟨σ3, G3, D3, hcd3, hps3, hch3, hsc3, hres3, hW3⟩ + simp only [uncurry_apply_pair] at D3 hcd3 hps3 hch3 hsc3 hres3 hW3 ⊢ + have hle2 : psiDA st s2 G1 ≤ psiDA st s0 G0 := le_trans hps2 (le_trans hps1 hps) + have hcd3' : (nnSA G3).card ≤ (nnSA G0).card := le_trans hcd3 (le_trans hcd1 hcd) + -- a changed cap at the slot comes with a strictly smaller `psiDA` + have hstr : capA st s3 (cslotAddrA sl) ≠ capA st s2 (cslotAddrA sl) → psiDA st s3 G3 < psiDA st s2 G1 := by + intro hne + refine hsc3 (cslotAddrA sl) hxO hne ?_ + by_cases he : cslotAddrA sl = cslotAddrA (zSlotA p zt m) + · right; rw [← he, cx]; exact ⟨p, zt, n, rfl⟩ + · left; exact he + rcases hres3 with ⟨rfl, ⟨v3, hrd3⟩, hxend⟩ | ⟨rfl, hrd3⟩ + swap + · -- the deletion of the last slot was preempted + simp only [core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame.from, + bind_ok, bind_tc_ok, WP.spec_ok] + have hz3 : RDGA Fn (.reduceZombieCall (absCapA (.Zombie p zt n)) sl true) σ2 (.error (), σ3) := by + rw [hzS]; exact rd_reduce_true_errGA (ptr := specPtr p) (bits := zombieBitsA zt) (n := m) hrd3 + refine ⟨σ3, G3, D3, hcd3', le_trans hps3 hle2, fun _ h => ?_, Or.inr ⟨rfl, + htr _ (rd_fin_reduce_errGA hg hcan hfc' hnrm hcy' hset hz3)⟩, hW3⟩ + have hne : capA st s3 (cslotAddrA sl) ≠ capA st s2 (cslotAddrA sl) := by + rw [h, cx]; exact fun e => by cases e + exact lt_of_lt_of_le (hstr hne) hle2 + -- the last slot deleted: the cap in the slot now + have hcz3 : ∀ y ∈ OS, y ≠ cslotAddrA sl → ChgZA st s0 s3 y := by + intro y hy hyx + by_cases hye : y = cslotAddrA (zSlotA p zt m) + · rw [hye] + rcases hxend with h | h + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr h) + · exact (hcz2 y hy hyx).trans (ChgZA.of_chgOK (hch3 y hy hye)) + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok, capA_eq] + obtain ⟨c3, hc3⟩ : ∃ c3, capA st s3 (cslotAddrA sl) = c3 := ⟨_, rfl⟩ + have hg3x : (∀ p asid va, c3 ≠ .PageTable p true asid va) → + getCap (Z := Unit) sl σ3 = ({(absCapA c3, σ3)}, False) := fun hne => by + obtain ⟨ac3, h, hr⟩ := getCapOne_eqA (st := st) D3.rel hA + rw [hc3] at hr + rw [← hr.eq_abs hne] at h; exact h + rw [hc3] + by_cases hun : c3 = .Zombie p zt n + · -- unchanged: count the zombie down + subst hun + have hg3 := hg3x fun _ _ _ e => by cases e + have hwm : (core.num.U64.wrapping_sub n 1#u64).val = m := by rw [hwv]; omega + have hzokw : ZOKA th p zt (core.num.U64.wrapping_sub n 1#u64) := by + rcases hzok with ⟨h1, h2, h3⟩ | ⟨h1, h2, h3⟩ + · exact Or.inl ⟨h1, h2, by omega⟩ + · exact Or.inr ⟨h1, h2, by omega⟩ + have hsz : sizeA (.Zombie p zt (core.num.U64.wrapping_sub n 1#u64)) = sizeA (.Zombie p zt n) := by + have h : objects.cap_size_bits (.Zombie p zt (core.num.U64.wrapping_sub n 1#u64)) = + objects.cap_size_bits (.Zombie p zt n) := rfl + exact okinj ((sizeA_eq _).symm.trans (h.trans (sizeA_eq _))) + have hclsw : clsA (.Zombie p zt (core.num.U64.wrapping_sub n 1#u64)) = clsA (capA st s3 (cslotAddrA sl)) := by + rw [hc3]; unfold clsA; rw [ptrA_zombie, ptrA_zombie, hsz] + have hxn3 : capA st s3 (cslotAddrA sl) ≠ .Null := by rw [hc3]; exact fun e => by cases e + obtain ⟨s4, hs4, hwp4, cx4, co4, σ4, hset4, hSS4, D4, hZm4⟩ := spec_existsA (set_zombie_sA (n := core.num.U64.wrapping_sub n 1#u64) hO D3 hA hcanp hwfz hzokw + (by rw [hc3]; exact RngKeyA.zombie _ _ _) (by rw [hc3, ptrA_zombie]) hclsw (Or.inr ⟨n, hc3⟩)) + simp only [↓reduceIte, if_true, eq_self_iff_true, lift, bind_ok, hs4, uncurry_apply_pair] + have hW4 : W s4 a3 σ4 G3 := H.zomb s3 s4 a3 σ3 σ4 G3 sl p zt (core.num.U64.wrapping_sub n 1#u64) hW3 (fun q hq => by rw [hc3] at hq; cases hq) + (fun p' zt' n' hq => by + rw [hc3] at hq + simp only [capability.Cap.Zombie.injEq] at hq + obtain ⟨rfl, rfl, rfl⟩ := hq + refine ⟨rfl, rfl, Or.inr ⟨by omega, ?_⟩⟩ + rw [hwm] + exact zlast_nullA D3 hxO hc3 hm.symm (fun h => hrmv (Or.inr h)) hxend) + (fun h => absurd ⟨p, zt, n, hc3⟩ h) D3 D4 hwp4 hSS4 hset4 hA hxn3 cx4 co4 hZm4 + have hcz4 : ∀ y ∈ OS, y ≠ cslotAddrA sl → ChgZA st s0 s4 y := + fun y hy hyx => (hcz3 y hy hyx).trans (ChgZA.of_eq (co4 y hy hyx)) + have habs4 : absCapA (.Zombie p zt (core.num.U64.wrapping_sub n 1#u64)) = + Fl4SpecA64.Cap.zombie (specPtr p) (zombieBitsA zt) m := by + show Fl4SpecA64.Cap.zombie (specPtr p) (zombieBitsA zt) (core.num.U64.wrapping_sub n 1#u64).val = _ + rw [hwm] + have hset4' : setCap (Z := Unit) (Fl4SpecA64.Cap.zombie (specPtr p) (zombieBitsA zt) m) sl σ3 = + ({((), σ4)}, False) := by rw [← habs4]; exact hset4 + have hg3' : getCap (Z := Unit) sl σ3 = + ({(Fl4SpecA64.Cap.zombie (specPtr p) (zombieBitsA zt) (m + 1), σ3)}, False) := by rw [hg3, hzS] + obtain ⟨v4, hz4⟩ := rd_reduce_true_decGA (Fn := Fn) (ptr := specPtr p) (bits := zombieBitsA zt) (n := m) + (sl := sl) (σ := σ2) (σ1 := σ3) (σ2 := σ4) hrd3 hg3' hset4' + rw [← hzS] at hz4 + have hphi : muIA st s4 (cslotAddrA sl) + 1 ≤ muIA st s (cslotAddrA sl) := + muI_decA hc hcn hzr hnK hn1 cx4 hwv + have hps4 : psiDA st s4 G3 ≤ psiDA st s3 G3 := by + refine psiDA_le D3.lk D4.lk (fun y hy h => ?_) (fun y hy _ hh => ?_) + · by_cases e : y = cslotAddrA sl + · rw [e]; exact hxn3 + · rw [← co4 y hy e]; exact h + · by_cases e : y = cslotAddrA sl + · subst e + rintro ⟨zt', n', hq⟩ + rw [hc3] at hq + simp only [capability.Cap.Zombie.injEq] at hq + exact hh ⟨zt, core.num.U64.wrapping_sub n 1#u64, by rw [cx4, hq.1]⟩ + · rintro ⟨zt', n', hq⟩ + exact hh ⟨zt', n', by rw [co4 y hy e]; exact hq⟩ + apply WP.spec_bind (H.pp s4 a3 σ4 G3 D4 hW4) + rintro ⟨r5, s5, a5⟩ ⟨hk5, σ5, D5, hcase⟩ + simp only at hk5 D5 hcase ⊢ + have hps5 : psiDA st s5 G3 = psiDA st s4 G3 := psiDA_sameA D4.lk D5.lk hk5 + have hmu5 : muIA st s5 (cslotAddrA sl) = muIA st s4 (cslotAddrA sl) := by + unfold muIA; rw [hk5 _ hxO] + have hle5 : psiDA st s5 G3 ≤ psiDA st s0 G0 := by + rw [hps5]; exact le_trans hps4 (le_trans hps3 hle2) + have hnul5 : capA st s0 (cslotAddrA sl) ≠ .Null → capA st s5 (cslotAddrA sl) = .Null → + psiDA st s5 G3 < psiDA st s0 G0 := by + intro _ h; rw [hk5 _ hxO, cx4] at h; cases h + have hcz5 : ∀ y ∈ OS, y ≠ cslotAddrA sl → ChgZA st s0 s5 y := + fun y hy hyx => (hcz4 y hy hyx).trans (ChgZA.of_eq (hk5 y hy)) + rcases hcase with ⟨rfl, hpm, hW5⟩ | ⟨rfl, hpm, hW5⟩ + · simp only [uncurry_apply_pair, core.result.Result.Insts.CoreOpsTry.branch, bind_ok, WP.spec_ok] + refine ⟨⟨σ5, G3, D5, ?_, hcd3', hle5, hnul5, fun y hy => ?_, hcz5, hW5⟩, ?_⟩ + · show muIA st s5 (cslotAddrA sl) < fuel1.val + rw [hmu5]; omega + · exact htr _ (rd_fin_reduceGA hg hcan hfc' hnrm hcy' hset hz4 hpm hy) + · show fuel1.val < fuel.val; omega + · simp only [uncurry_apply_pair, core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, bind_ok, bind_tc_ok, WP.spec_ok] + exact ⟨σ5, G3, D5, hcd3', hle5, hnul5, + Or.inr ⟨rfl, htr _ (rd_fin_pp_errGA hg hcan hfc' hnrm hcy' hset hz4 hpm)⟩, hW5⟩ + · -- changed: the spec's assertion + apply WP.spec_bind (Pₘ := fun r => r = (s3, a3)) (by + cases c3 <;> simp only [WP.spec_ok] + case Zombie p2 t2 n2 => + split_ifs with h1 h2 h3 + · exact absurd (by rw [h1, h2, h3]) hun + · simp only [bind_ok, WP.spec_ok] + · simp only [WP.spec_ok] + · simp only [WP.spec_ok]) + rintro _ rfl + simp only [uncurry_apply_pair] + -- the slot is empty or holds a zombie at home, and the zombie made was not at home + have hchg : (capA st s3 (cslotAddrA sl) = .Null ∨ HomeZA st s3 (cslotAddrA sl)) ∧ p ≠ cslotAddrA sl := by + by_cases he : cslotAddrA sl = cslotAddrA (zSlotA p zt m) + · rw [he] + refine ⟨hxend, fun hpx => ?_⟩ + have hv := (zslot_valA D3.place hzok hmn).1 + have hm0 : m = 0 := by + have := congrArg UScalar.val hpx + omega + exact hrmv (Or.inr ⟨by omega, hpx.trans he.symm⟩) + · rcases hch3 _ hxO he with e | ⟨h, hh⟩ + · rw [hc3, cx] at e; exact absurd e hun + · exact ⟨h, fun hpx => hh ⟨zt, n, by rw [cx, hpx]⟩⟩ + have hnc : absCapA c3 ≠ Fl4SpecA64.Cap.zombie (specPtr p) (zombieBitsA zt) (m + 1) := by + rcases hchg.1 with h0 | ⟨zt', n', e⟩ + · rw [hc3] at h0; subst h0; exact fun h => by cases h + · rw [hc3] at e; subst e + intro h + simp only [absCapA, Fl4SpecA64.Cap.zombie.injEq] at h + exact hchg.2 (specPtr_inj h.1).symm + have ha : absCapA c3 = .nullCap ∨ isZombie (absCapA c3) ∧ firstCslotOf (absCapA c3) = sl ∧ + firstCslotOf (Fl4SpecA64.Cap.zombie (specPtr p) (zombieBitsA zt) (m + 1)) ≠ sl := by + rcases hchg.1 with h0 | ⟨zt', n', e⟩ + · rw [hc3] at h0; subst h0; exact Or.inl rfl + · have hz3 := D3.zw _ (hA.isSlot D3.place) _ _ _ e + rw [hc3] at e; subst e + refine Or.inr ⟨rfl, (home_iffA D3.place hz3 hA).2 rfl, ?_⟩ + rw [← hzS, firstCslot_zombieA] + exact fun h => hchg.2 ((home_iffA D3.place hzok hA).1 h) + have hg3 := hg3x (by + rcases hchg.1 with h | ⟨zt', n', h⟩ <;> rw [hc3] at h <;> (rw [h]; intro p' asid va e; cases e)) + obtain ⟨v4, hz4⟩ := rd_reduce_true_chgGA (Fn := Fn) (ptr := specPtr p) (bits := zombieBitsA zt) (n := m) + (sl := sl) (σ := σ2) (σ1 := σ3) hrd3 hg3 hnc ha + rw [← hzS] at hz4 + have hstr3 : psiDA st s3 G3 < psiDA st s2 G1 := hstr (by rw [hc3, cx]; exact hun) + have hphi : muIA st s3 (cslotAddrA sl) + 1 ≤ muIA st s (cslotAddrA sl) := + muI_chgA hc hcn hzr hchg.2 hchg.1 (fun _ _ _ e => zcount_leA D3 hxO e) + apply WP.spec_bind (H.pp s3 a3 σ3 G3 D3 hW3) + rintro ⟨r5, s5, a5⟩ ⟨hk5, σ5, D5, hcase⟩ + simp only at hk5 D5 hcase ⊢ + have hps5 : psiDA st s5 G3 = psiDA st s3 G3 := psiDA_sameA D3.lk D5.lk hk5 + have hmu5 : muIA st s5 (cslotAddrA sl) = muIA st s3 (cslotAddrA sl) := by + unfold muIA; rw [hk5 _ hxO] + have hlt5 : psiDA st s5 G3 < psiDA st s0 G0 := by + rw [hps5]; exact lt_of_lt_of_le hstr3 hle2 + have hcz5 : ∀ y ∈ OS, y ≠ cslotAddrA sl → ChgZA st s0 s5 y := + fun y hy hyx => (hcz3 y hy hyx).trans (ChgZA.of_eq (hk5 y hy)) + rcases hcase with ⟨rfl, hpm, hW5⟩ | ⟨rfl, hpm, hW5⟩ + · simp only [uncurry_apply_pair, core.result.Result.Insts.CoreOpsTry.branch, bind_ok, WP.spec_ok] + refine ⟨⟨σ5, G3, D5, ?_, hcd3', hlt5.le, fun _ _ => hlt5, fun y hy => ?_, hcz5, hW5⟩, ?_⟩ + · show muIA st s5 (cslotAddrA sl) < fuel1.val + rw [hmu5]; omega + · exact htr _ (rd_fin_reduceGA hg hcan hfc' hnrm hcy' hset hz4 hpm hy) + · show fuel1.val < fuel.val; omega + · simp only [uncurry_apply_pair, core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, bind_ok, bind_tc_ok, WP.spec_ok] + exact ⟨σ5, G3, D5, hcd3', hlt5.le, fun _ _ => hlt5, + Or.inr ⟨rfl, htr _ (rd_fin_pp_errGA hg hcan hfc' hnrm hcy' hset hz4 hpm)⟩, hW5⟩ + +end + +/-- **FL4's `finalise_slot_immediate` refines `finalise_slot slot True`** from `WALK_FUEL`. -/ +theorem finalise_slot_immediate_refinesGA {ar : machine.Arch A} {T Ω Sg OS : Set U64} {th : TypedHeapA} + (hO : OSOK T Ω Sg OS) (hCan : ∀ x ∈ OS, obj.sext48 x = ok x) {Fn : FinFnA} {W Wx Wp : DelWA S A} + {Rd Rz : S → A → Fl4SpecA64.State Unit → MdbGhostA → U64 → Prop} + (H : DelHypsA st ar T th OS Fn W Wx Wp Rd Rz) (sl : CslotPtr) (hA : AbsSlotA th sl) + (s : S) (a : A) (σ : Fl4SpecA64.State Unit) (G : MdbGhostA) (D : DStateA st th OS s σ G) (hW : W s a σ G) : + cdt.finalise_slot_immediate st ar s a (cslotAddrA sl) ⦃ res => + ImmPostGA st Fn W Wx Wp Rd th OS sl s σ G res ⦄ := by + unfold cdt.finalise_slot_immediate + have hWF : layout.WALK_FUEL.val = 2 ^ 64 - 1 := by simp [layout.WALK_FUEL, U64.rMax] + refine immediate_loopZGA hO hCan H sl hA s σ G s a layout.WALK_FUEL + ⟨σ, G, D, ?_, le_refl _, le_refl _, fun h0 h => absurd h h0, fun _ h => h, fun _ _ _ => Or.inl rfl, hW⟩ + show muIA st s (cslotAddrA sl) < layout.WALK_FUEL.val + rw [hWF] + have := muIA_leA D (D.mem_slot hA) + omega + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/CnodeDelInvA.lean b/proofs/Fl4ProofsA64/P3/CnodeDelInvA.lean new file mode 100644 index 000000000..5886baadc --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/CnodeDelInvA.lean @@ -0,0 +1,301 @@ +/- +P3 (AArch64, A64-P3 CNode wave 1): **the deletion's invariant and measure** (x86 `P3/DelInv`, `CnDelInv`). + +* **`ZOKA th p zt n`**: a well-formed zombie, over a TCB (type 65, at most 7 slots) or over a CNode of radix `zt` + (at most `2 ^ zt` slots). **`DelInvA`**: every zombie of a typed slot is well formed. +* **`DStateA`**: the related states of the deletion loops: `FinStA` (`P3/CnodeFinalA`, with its named premises), + the CDT relation, A64 P1's tree over `OS`, `NullLk` (an empty slot has clear links), `DelInvA` and the caps' validity (`CapValidA`). x86's other two + `DelInv` fields are A64 P1's: no two zombies cover one object (`ZombiesFinal`, **`DStateA.zu`**) and `NullLk`. +* The zombie's first slot (**`homeSlA`**), the agreement of `cap_removable` / `cap_cyclic_zombie` with the spec, and + the spec's `assertE (first_cslot_of cap ≠ slot)` for a non-cyclic zombie (**`homeSl_neA`**). +* **The measure** `psiDA` (linked slots, plus those not holding a zombie in its own first slot), its monotonicity + lemmas, and **`muFuelA`**: it is below `WALK_FUEL = u64::MAX` (every linked slot is a distinct 32-byte slot). + +Named premises: none new (those of `DStateA.toFinStA`). +-/ +import Fl4ProofsA64.P3.CnodeFinalA +import Fl4ProofsA64.P1.MdbNull + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-! ## The invariant -/ + +/-- **A well-formed zombie** (module docstring; x86 `ZOK`). -/ +def ZOKA (th : TypedHeapA) (p zt n : U64) : Prop := + (zt.val = 65 ∧ th p = some .tcb ∧ n.val ≤ tcbSlotsA) ∨ + (zt.val ≤ 64 ∧ th p = some (.cnode zt.val) ∧ n.val ≤ 2 ^ zt.val) + +/-- Every typed slot's zombie is well formed (x86 `DelInv.zwf`). -/ +def DelInvA (st : machine.Store S) [L : LawfulStore st] (th : TypedHeapA) (s : S) : Prop := + ∀ y, IsSlotA th y → ∀ p zt n, capA st s y = .Zombie p zt n → ZOKA th p zt n + +/-- **The deletion's related states** (module docstring; x86 `DState`). -/ +structure DStateA (st : machine.Store S) [L : LawfulStore st] (th : TypedHeapA) (OS : Set U64) (s : S) + (σ : Fl4SpecA64.State Unit) (G : MdbGhostA) : Prop extends FinStA st th OS G.chains s σ where + cdt : CdtRelA st th s σ G + tree : TreeA st OS G.chains G.par s + nl : NullLk st OS s + zw : DelInvA st th s + /-- Every cap of `OS` is valid (a CNode / thread cap names its object; part of `JAtZAX.capObj`). -/ + cv : ∀ y ∈ OS, CapValidA th (capA st s y) + +variable {th : TypedHeapA} {OS : Set U64} {s : S} {σ : Fl4SpecA64.State Unit} {G : MdbGhostA} + +/-- No two zombies cover one object (x86 `DelInv.zu`; A64 P1's `ZombiesFinal`). -/ +theorem DStateA.zu (D : DStateA st th OS s σ G) {y y' : U64} (hy : y ∈ OS) (hy' : y' ∈ OS) (hne : y ≠ y') + {p zt n p' zt' n' : U64} (h : capA st s y = .Zombie p zt n) (h' : capA st s y' = .Zombie p' zt' n') : p ≠ p' := by + intro e + refine D.zf y hy p zt n h y' hy' (Ne.symm hne) (by rw [h']; exact RngKeyA.zombie _ _ _) ?_ + rw [h', ptrA_zombie, e] + +/-- An empty slot has clear links (x86 `DelInv.nc`; A64 P1's `NullLk`). -/ +theorem DStateA.nc (D : DStateA st th OS s σ G) {y : U64} (hy : y ∈ OS) (h : capA st s y = .Null) : + L.mem s (wa y 16#u64) = 0#u64 ∧ NxA st s y = 0#u64 := D.nl y hy h + +theorem DStateA.mem_slot (D : DStateA st th OS s σ G) {sl : CslotPtr} (h : AbsSlotA th sl) : cslotAddrA sl ∈ OS := + D.sub _ (h.isSlot D.place) + +/-- A non-null cap of `OS` sits in a typed slot. -/ +theorem DStateA.isSlot_of (D : DStateA st th OS s σ G) {y : U64} (hy : y ∈ OS) (h : capA st s y ≠ .Null) : + IsSlotA th y := by + by_contra hn + exact h (D.nul y hy hn) + +/-! ## A zombie's first slot -/ + +/-- The spec's first slot of a zombie. -/ +abbrev homeSlA (p zt : U64) : CslotPtr := (specPtr p, List.replicate (zombieCteBits (zombieBitsA zt)) false) + +theorem replicate_bitsOfA (k : Nat) : List.replicate k false = bitsOfA 0 k := by + induction k with + | zero => rfl + | succ k ih => simp [bitsOfA, List.replicate_succ, ih] + +theorem zombieCteBits_leA {zt : U64} (h : zt.val ≤ 64 ∨ zt.val = 65) : zombieCteBits (zombieBitsA zt) ≤ 64 := by + unfold zombieBitsA zombieCteBits + split_ifs with h65 + · show (3 : Nat) ≤ 64 + decide + · show zt.val ≤ 64 + omega + +theorem home_addrA (p : U64) {zt : U64} (h : zt.val ≤ 64 ∨ zt.val = 65) : cslotAddrA (homeSlA p zt) = p := by + show slotAddrA (specPtr p) (ofBl (List.replicate (zombieCteBits (zombieBitsA zt)) false)) = p + rw [replicate_bitsOfA, ofBl_bitsOfA 0 _ (zombieCteBits_leA h), Nat.zero_mod, slotAddrA_sp] + show (⟨p.bv + BitVec.ofNat 64 0 * 32#64⟩ : U64) = p + simp + +theorem ZOKA.kind {p zt n : U64} (h : ZOKA th p zt n) : zt.val ≤ 64 ∨ zt.val = 65 := by + rcases h with ⟨h, -⟩ | ⟨h, -⟩ + · exact Or.inr h + · exact Or.inl h + +theorem ZOKA.addr {p zt n : U64} (h : ZOKA th p zt n) : cslotAddrA (homeSlA p zt) = p := home_addrA p h.kind + +theorem ZOKA.absSlot {p zt n : U64} (h : ZOKA th p zt n) : AbsSlotA th (homeSlA p zt) := by + rcases h with ⟨h65, ht, -⟩ | ⟨h64, hc, -⟩ + · refine ⟨p, rfl, Or.inr ⟨ht, 0, by decide, ?_⟩⟩ + show List.replicate (zombieCteBits (zombieBitsA zt)) false = _ + have : zombieBitsA zt = none := by simp [zombieBitsA, h65] + rw [this]; decide + · refine ⟨p, rfl, Or.inl ?_⟩ + have : zombieBitsA zt = some zt.val := by + unfold zombieBitsA; rw [if_neg (by omega)] + show th p = some (.cnode (List.replicate (zombieCteBits (zombieBitsA zt)) false).length) + rw [this]; simpa [zombieCteBits] using hc + +theorem firstCslot_zombieA (p zt n : U64) : firstCslotOf (absCapA (.Zombie p zt n)) = homeSlA p zt := rfl + +/-- The zombie's first slot is `sl` iff FL4's base is `sl`'s address. -/ +theorem home_iffA {kb : U64} (hP : PlaceA th kb) {p zt n : U64} (h : ZOKA th p zt n) {sl : CslotPtr} + (hs : AbsSlotA th sl) : homeSlA p zt = sl ↔ p = cslotAddrA sl := by + constructor + · intro e; rw [← e, h.addr] + · intro e + exact cslotAddrA_inj hP h.absSlot hs (h.addr.trans e) + +/-- **The spec's `assertE`**: a zombie that is not cyclic does not start at its own slot. -/ +theorem homeSl_neA {kb : U64} (hP : PlaceA th kb) {p zt n : U64} (h : ZOKA th p zt n) {sl : CslotPtr} + (hs : AbsSlotA th sl) (hne : p ≠ cslotAddrA sl) : firstCslotOf (absCapA (.Zombie p zt n)) ≠ sl := by + rw [firstCslot_zombieA] + exact fun e => hne ((home_iffA hP h hs).1 e) + +/-! ## `cap_removable` and `cap_cyclic_zombie` -/ + +theorem cap_removable_zombieA {kb : U64} (hP : PlaceA th kb) {p zt n : U64} (h : ZOKA th p zt n) {sl : CslotPtr} + (hs : AbsSlotA th sl) : + ∃ b, cdt.cap_removable (.Zombie p zt n) (cslotAddrA sl) = ok b ∧ + (b = true ↔ capRemoveable (absCapA (.Zombie p zt n)) sl) := by + have hr : capRemoveable (absCapA (.Zombie p zt n)) sl ↔ n.val = 0 ∨ (n.val = 1 ∧ p = cslotAddrA sl) := by + show (n.val = 0 ∨ (n.val = 1 ∧ homeSlA p zt = sl)) ↔ _ + rw [home_iffA hP h hs] + have e0 : n = 0#u64 ↔ n.val = 0 := ⟨fun e => by rw [e]; rfl, fun e => UScalar.eq_of_val_eq e⟩ + have e1 : n = 1#u64 ↔ n.val = 1 := ⟨fun e => by rw [e]; rfl, fun e => UScalar.eq_of_val_eq e⟩ + simp only [cdt.cap_removable] + by_cases h0 : n = 0#u64 + · rw [if_pos h0] + exact ⟨true, rfl, by rw [hr]; simp [e0.1 h0]⟩ + rw [if_neg h0] + by_cases h1 : n = 1#u64 + · rw [if_pos h1] + refine ⟨decide (cslotAddrA sl = p), by simp [obj.Slot.Insts.CoreCmpPartialEqSlot.eq, eq_comm], ?_⟩ + rw [hr, decide_eq_true_iff] + simp [e1.1 h1, eq_comm] + · rw [if_neg h1] + refine ⟨false, rfl, ?_⟩ + rw [hr] + simp [mt e0.2 h0, mt e1.2 h1] + +theorem cap_cyclic_zombieA {kb : U64} (hP : PlaceA th kb) {p zt n : U64} (h : ZOKA th p zt n) {sl : CslotPtr} + (hs : AbsSlotA th sl) : + cdt.cap_cyclic_zombie (.Zombie p zt n) (cslotAddrA sl) = ok (decide (p = cslotAddrA sl)) ∧ + (capCyclicZombie (absCapA (.Zombie p zt n)) sl ↔ p = cslotAddrA sl) := + ⟨rfl, home_iffA hP h hs⟩ + +/-! ## The measure -/ + +/-- The slot `y` holds a zombie at home (in the zombie's first slot). -/ +def HomeZA (st : machine.Store S) [L : LawfulStore st] (s : S) (y : U64) : Prop := ∃ zt n, capA st s y = .Zombie y zt n + +open Classical in +/-- The linked slots. -/ +def nnSA (G : MdbGhostA) : Finset U64 := G.chains.flatten.toFinset + +open Classical in +/-- The linked slots not holding a zombie at home. -/ +def mzSA (st : machine.Store S) [L : LawfulStore st] (s : S) (G : MdbGhostA) : Finset U64 := + (nnSA G).filter (fun y => ¬ HomeZA st s y) + +/-- **The measure of the deletion** (x86 `psiD`). -/ +def psiDA (st : machine.Store S) [L : LawfulStore st] (s : S) (G : MdbGhostA) : Nat := + (nnSA G).card + (mzSA st s G).card + +theorem mem_nnSA (hM : MdbLk st OS G.chains s) (y : U64) : y ∈ nnSA G ↔ y ∈ OS ∧ capA st s y ≠ .Null := by + classical + unfold nnSA; rw [List.mem_toFinset, hM.members y] + +theorem nnSA_card (hM : MdbLk st OS G.chains s) : (nnSA G).card = G.chains.flatten.length := by + classical + exact List.toFinset_card_of_nodup hM.nodup + +theorem mem_mzSA (hM : MdbLk st OS G.chains s) (y : U64) : + y ∈ mzSA st s G ↔ y ∈ OS ∧ capA st s y ≠ .Null ∧ ¬ HomeZA st s y := by + classical + unfold mzSA; rw [Finset.mem_filter, mem_nnSA hM, and_assoc] + +section mono +variable {s' : S} {G' : MdbGhostA} + +/-- The measure does not grow when no slot becomes linked and no home zombie leaves home. -/ +theorem psiDA_le (hM : MdbLk st OS G.chains s) (hM' : MdbLk st OS G'.chains s') + (hnn : ∀ y ∈ OS, capA st s' y ≠ .Null → capA st s y ≠ .Null) + (hmz : ∀ y ∈ OS, capA st s' y ≠ .Null → ¬ HomeZA st s' y → ¬ HomeZA st s y) : + psiDA st s' G' ≤ psiDA st s G := by + unfold psiDA + have h1 : nnSA G' ⊆ nnSA G := fun y hy => by + rw [mem_nnSA hM'] at hy; rw [mem_nnSA hM]; exact ⟨hy.1, hnn y hy.1 hy.2⟩ + have h2 : mzSA st s' G' ⊆ mzSA st s G := fun y hy => by + rw [mem_mzSA hM'] at hy; rw [mem_mzSA hM]; exact ⟨hy.1, hnn y hy.1 hy.2.1, hmz y hy.1 hy.2.1 hy.2.2⟩ + exact Nat.add_le_add (Finset.card_le_card h1) (Finset.card_le_card h2) + +/-- Strictly smaller, when a slot leaves `mzSA`. -/ +theorem psiDA_lt (hM : MdbLk st OS G.chains s) (hM' : MdbLk st OS G'.chains s') + (hnn : ∀ y ∈ OS, capA st s' y ≠ .Null → capA st s y ≠ .Null) + (hmz : ∀ y ∈ OS, capA st s' y ≠ .Null → ¬ HomeZA st s' y → ¬ HomeZA st s y) + {z : U64} (hz : z ∈ mzSA st s G) (hz' : z ∉ mzSA st s' G') : psiDA st s' G' < psiDA st s G := by + unfold psiDA + have h1 : nnSA G' ⊆ nnSA G := fun y hy => by + rw [mem_nnSA hM'] at hy; rw [mem_nnSA hM]; exact ⟨hy.1, hnn y hy.1 hy.2⟩ + have h2 : mzSA st s' G' ⊂ mzSA st s G := by + refine Finset.ssubset_iff_subset_ne.2 ⟨fun y hy => ?_, fun e => hz' (e ▸ hz)⟩ + rw [mem_mzSA hM'] at hy; rw [mem_mzSA hM]; exact ⟨hy.1, hnn y hy.1 hy.2.1, hmz y hy.1 hy.2.1 hy.2.2⟩ + have := Finset.card_lt_card h2 + have := Finset.card_le_card h1 + omega + +/-- Strictly smaller, when a slot becomes empty. -/ +theorem psiDA_lt_nn (hM : MdbLk st OS G.chains s) (hM' : MdbLk st OS G'.chains s') + (hnn : ∀ y ∈ OS, capA st s' y ≠ .Null → capA st s y ≠ .Null) + (hmz : ∀ y ∈ OS, capA st s' y ≠ .Null → ¬ HomeZA st s' y → ¬ HomeZA st s y) + {z : U64} (hz : z ∈ nnSA G) (hz' : z ∉ nnSA G') : psiDA st s' G' < psiDA st s G := by + unfold psiDA + have h1 : nnSA G' ⊂ nnSA G := by + refine Finset.ssubset_iff_subset_ne.2 ⟨fun y hy => ?_, fun e => hz' (e ▸ hz)⟩ + rw [mem_nnSA hM'] at hy; rw [mem_nnSA hM]; exact ⟨hy.1, hnn y hy.1 hy.2⟩ + have h2 : mzSA st s' G' ⊆ mzSA st s G := fun y hy => by + rw [mem_mzSA hM'] at hy; rw [mem_mzSA hM]; exact ⟨hy.1, hnn y hy.1 hy.2.1, hmz y hy.1 hy.2.1 hy.2.2⟩ + have := Finset.card_lt_card h1 + have := Finset.card_le_card h2 + omega + +end mono + +theorem psiDA_bound (hM : MdbLk st OS G.chains s) : psiDA st s G ≤ 2 * G.chains.flatten.length := by + classical + unfold psiDA + have : mzSA st s G ⊆ nnSA G := Finset.filter_subset _ _ + have := Finset.card_le_card this + rw [nnSA_card hM] at * + omega + +/-- A typed slot's address is a multiple of 32. -/ +theorem isSlotA_al32 {kb y : U64} (hP : PlaceA th kb) (h : IsSlotA th y) : y.val % 32 = 0 := by + have key : ∀ (p : U64) (k : ObjKindA) (i : Nat), th p = some k → 32 ∣ k.size → i * 32 + 32 ≤ k.size → + (slotAddrA (specPtr p) (BitVec.ofNat 64 i)).val % 32 = 0 := by + intro p k i hp hd hi + have ha := hP.align p k hp + have hf := hP.fit p k hp + have hp32 : p.val % 32 = 0 := Nat.mod_eq_zero_of_dvd (hd.trans (Nat.dvd_of_mod_eq_zero ha)) + have hlt : p.val + i * 32 < 2 ^ 64 := by omega + have e : (slotAddrA (specPtr p) (BitVec.ofNat 64 i)).val = p.val + i * 32 := by + rw [slotAddrA_sp] + show (p.bv + BitVec.ofNat 64 i * 32#64).toNat = _ + have hp' : p.bv.toNat = p.val := rfl + rw [BitVec.toNat_add, BitVec.toNat_mul, BitVec.toNat_ofNat, hp'] + have h32 : (32#64).toNat = 32 := rfl + rw [h32, Nat.mod_eq_of_lt (show i < 2 ^ 64 by omega), Nat.mod_eq_of_lt (show i * 32 < 2 ^ 64 by omega), + Nat.mod_eq_of_lt hlt] + rw [e]; omega + obtain ⟨p, ⟨r, hr, i, hi, rfl⟩ | ⟨ht, i, hi, rfl⟩⟩ := h + · have e : (ObjKindA.cnode r).size = 2 ^ r * 32 := by + show 2 ^ (r + 5) = _ + rw [pow_add]; norm_num + refine key p _ i hr (e ▸ Dvd.intro_left _ rfl) ?_ + rw [e]; omega + · refine key p _ i ht (by decide) ?_ + show i * 32 + 32 ≤ 4096 + unfold tcbSlotsA at hi; omega + +/-- **The measure is below `WALK_FUEL`** (module docstring). -/ +theorem muFuelA (D : DStateA st th OS s σ G) : psiDA st s G < 2 ^ 64 - 1 := by + classical + have h1 := psiDA_bound D.lk + have h2 : (nnSA G).card ≤ (Finset.range (2 ^ 59)).card := by + refine Finset.card_le_card_of_injOn (fun y => y.val / 32) (fun y _ => ?_) (fun y hy y' hy' e => ?_) + · have : y.val < 2 ^ 64 := by have := y.hBounds; simpa using this + simp only [Finset.coe_range, Set.mem_Iio]; omega + · have al : ∀ z ∈ nnSA G, z.val % 32 = 0 := fun z hz => by + rw [mem_nnSA D.lk] at hz + exact isSlotA_al32 D.place (D.isSlot_of hz.1 hz.2) + have a := al y hy + have a' := al y' hy' + simp only at e + exact UScalar.eq_of_val_eq (by omega) + rw [Finset.card_range, nnSA_card D.lk] at h2 + omega + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/CnodeDelRoundA.lean b/proofs/Fl4ProofsA64/P3/CnodeDelRoundA.lean new file mode 100644 index 000000000..e07694268 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/CnodeDelRoundA.lean @@ -0,0 +1,292 @@ +/- +P3 (AArch64, A64-P3 CNode wave 2): **one finalisation round** (x86 `P3/DelRound` / `DelFinStep`). + +* **`fin_stepA`**: `objects::finalise_cap … f false` of a non-null slot cap refines the spec finaliser `Fn`, for every + kind: the caps the CSpace alone sees by `finalise_cap_trivA` (no effect), the objects' by the hook `FinHookA`. +* **`zombie_factsA`**: when a `Zombie` remains, what the loop's zombie write (`set_zombie_sA`) asks of it, at any + state after the finalisation: its pointer canonical, well formed, over the object of the old cap (key, base and + class), and either the only range key of that base or the same zombie as before. + +No named premise. +-/ +import Fl4ProofsA64.P3.CnodeDelHookA +import Fl4ProofsA64.P1.FastFault +import Fl4ProofsA64.P1.MdbFin +import Fl4ProofsA64.P1.QueueZombieEnc + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-- The pointer of a slot's CNode or zombie cap is canonical. -/ +theorem capA_canonA (s : S) (x : U64) : + (∀ q r g b, capA st s x = .CNode q r g b → Canon q) ∧ (∀ q, capA st s x = .Thread q → Canon q) ∧ + (∀ q t n, capA st s x = .Zombie q t n → Canon q) := by + have h := decode_canon' (L.mem s x) (L.mem s (wa x 8#u64)) + rw [CapRt.decode_eq, WP.spec_ok] at h + exact ⟨h.2.1, h.1, h.2.2⟩ + +variable {th : TypedHeapA} {OS : Set U64} {s : S} {σ : Fl4SpecA64.State Unit} {G : MdbGhostA} + +/-- **A final range key cap is the only range key of its base.** -/ +theorem final_uniqueA (D : DStateA st th OS s σ G) {sl : CslotPtr} (hsl : AbsSlotA th sl) {c : capability.Cap} + (hc : capA st s (cslotAddrA sl) = c) (hk : RngKeyA c) (hf : isFinalCap' (absCapA c) σ) : + ∀ y ∈ OS, y ≠ cslotAddrA sl → RngKeyA (capA st s y) → ptrA (capA st s y) ≠ ptrA c := by + have hxO : cslotAddrA sl ∈ OS := D.mem_slot hsl + have hwf : CapWfA c := hc ▸ D.wf hxO + let Q : capability.Cap → Prop := fun d => (RngKeyA d ∨ isFrA d = true) ∧ specPtr (ptrA d) = specPtr (ptrA c) + have hov : ∀ y ∈ OS, (genObjRefs (absCapA c) ∩ genObjRefs (absCapA (capA st s y)) ≠ ∅ ↔ Q (capA st s y)) := by + intro y hy + rw [genRefs_keyA hk hwf] + exact overlap_objA (D.wf hy) + have hnull : ¬ Q .Null := fun h => by + rcases h.1 with h | h + · exact RngKeyA.not_null h + · exact Bool.false_ne_true h + have alone := (isFinalCap_iffA D.toFinStA hsl hc Q hov ⟨Or.inl hk, rfl⟩ hnull).1 hf + intro y hy hne ky e + exact alone y hy hne ⟨Or.inl ky, by rw [e]⟩ + +theorem zombieBitsTcbA : capability.ZOMBIE_TCB.val = 65 := by simp [capability.ZOMBIE_TCB] + +/-- **What the loop's zombie write asks of a `Zombie` remainder** (module docstring). -/ +theorem zombie_factsA (D : DStateA st th OS s σ G) {sl : CslotPtr} (hsl : AbsSlotA th sl) {c : capability.Cap} + (hc : capA st s (cslotAddrA sl) = c) {f : Bool} + (hf : NeedsFinCA c → (f = true ↔ isFinalCap' (absCapA c) σ)) {p zt n : U64} + (hr : finRemA c f = .Zombie p zt n) {s' : S} + (hk : ∀ y ∈ OS, y ≠ cslotAddrA sl → capA st s' y = capA st s y ∨ capA st s' y = .Null) : + Canon p ∧ CapWfA (.Zombie p zt n) ∧ ZOKA th p zt n ∧ RngKeyA c ∧ ptrA c = p ∧ + clsA (.Zombie p zt n) = clsA c ∧ + ((∀ y ∈ OS, y ≠ cslotAddrA sl → RngKeyA (capA st s' y) → ptrA (capA st s' y) ≠ p) ∨ + ∃ n0, c = .Zombie p zt n0) := by + have hxO : cslotAddrA sl ∈ OS := D.mem_slot hsl + have hwf : CapWfA c := hc ▸ D.wf hxO + have hxs : IsSlotA th (cslotAddrA sl) := hsl.isSlot D.place + have hcv : CapValidA th c := hc ▸ D.cv _ hxO + have canon := capA_canonA (st := st) s (cslotAddrA sl) + rw [hc] at canon + -- a final cap's uniqueness, carried to `s'` + have uniq : RngKeyA c → ptrA c = p → isFinalCap' (absCapA c) σ → + ∀ y ∈ OS, y ≠ cslotAddrA sl → RngKeyA (capA st s' y) → ptrA (capA st s' y) ≠ p := by + intro kc pc fin y hy hne ky e + rcases hk y hy hne with k | k + · rw [k] at ky e + exact final_uniqueA D hsl hc kc fin y hy hne ky (e.trans pc.symm) + · rw [k] at ky; exact RngKeyA.not_null ky + have key : ∀ d : capability.Cap, keyCtorA d = true → RngKeyA d := fun d h => (rngKeyA_iffA d).2 h + have pt : ∀ (d : capability.Cap) (q : U64), capability.Cap.ptr d = ok q → ptrA d = q := fun d q h => + okinj ((ptrA_eq d).symm.trans h) + cases c with + | CNode q r g b => + simp only [finRemA] at hr + by_cases hft : f = true + swap + · rw [if_neg hft] at hr; cases hr + rw [if_pos hft, capability.Cap.Zombie.injEq] at hr + obtain ⟨e1, e2, e3⟩ := hr + rw [← e2, ← e3] + have hr64 : r.val < 64 := hwf.1 + have fin : isFinalCap' (absCapA (.CNode q r g b)) σ := (hf trivial).1 hft + have kc := key (.CNode q r g b) rfl + have pc : ptrA (.CNode q r g b) = p := (pt _ q rfl).trans e1 + have hv : (⟨BitVec.ofNat 64 (2 ^ r.val)⟩ : U64).val = 2 ^ r.val := by + show (BitVec.ofNat 64 (2 ^ r.val)).toNat = _ + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (Nat.pow_lt_pow_right (by decide) hr64)] + have hz65 : r ≠ capability.ZOMBIE_TCB := fun e => by + have := congrArg (fun x : U64 => x.val) e + simp only [zombieBitsTcbA] at this + omega + have hcan : Canon p := by rw [← e1]; exact canon.1 q r g b rfl + have hth : th p = some (.cnode r.val) := by rw [← e1]; exact hcv.1 + refine ⟨hcan, Or.inl (by omega), Or.inr ⟨by omega, hth, by omega⟩, kc, pc, ?_, Or.inl (uniq kc pc fin)⟩ + have h : objects.cap_size_bits (.Zombie p r ⟨BitVec.ofNat 64 (2 ^ r.val)⟩) = + objects.cap_size_bits (.CNode q r g b) := by simp only [objects.cap_size_bits, if_neg hz65] + have e : sizeA (.Zombie p r ⟨BitVec.ofNat 64 (2 ^ r.val)⟩) = sizeA (.CNode q r g b) := + okinj ((sizeA_eq _).symm.trans (h.trans (sizeA_eq _))) + unfold clsA; rw [ptrA_zombie, pc, e] + | Thread t => + simp only [finRemA] at hr + by_cases hft : f = true + swap + · rw [if_neg hft] at hr; cases hr + rw [if_pos hft, capability.Cap.Zombie.injEq] at hr + obtain ⟨e1, e2, e3⟩ := hr + rw [← e2, ← e3] + have fin : isFinalCap' (absCapA (.Thread t)) σ := (hf trivial).1 hft + have kc := key (.Thread t) rfl + have pc : ptrA (.Thread t) = p := (pt _ t rfl).trans e1 + have hcan : Canon p := by rw [← e1]; exact canon.2.1 t rfl + have hth : th p = some .tcb := by rw [← e1]; exact hcv + refine ⟨hcan, Or.inr zombieBitsTcbA, Or.inl ⟨zombieBitsTcbA, hth, by decide⟩, kc, pc, ?_, + Or.inl (uniq kc pc fin)⟩ + have h : objects.cap_size_bits (.Zombie p capability.ZOMBIE_TCB 7#u64) = objects.cap_size_bits (.Thread t) := by + simp only [objects.cap_size_bits, ↓reduceIte] + have e : sizeA (.Zombie p capability.ZOMBIE_TCB 7#u64) = sizeA (.Thread t) := + okinj ((sizeA_eq _).symm.trans (h.trans (sizeA_eq _))) + unfold clsA; rw [ptrA_zombie, pc, e] + | Zombie q t m => + simp only [finRemA, capability.Cap.Zombie.injEq] at hr + obtain ⟨e1, e2, e3⟩ := hr + have kc := key (.Zombie q t m) rfl + have pc : ptrA (.Zombie q t m) = p := (pt _ q rfl).trans e1 + have h := D.zw _ hxs q t m hc + refine ⟨by rw [← e1]; exact canon.2.2 q t m rfl, by rw [← e2]; exact hwf, by rw [← e1, ← e2, ← e3]; exact h, + kc, pc, by rw [← e1, ← e2, ← e3], Or.inr ⟨m, by rw [e1, e2]⟩⟩ + | _ => simp [finRemA] at hr + +/-- What `finalise_cap`'s outcome says of an IRQ-handler cleanup: it is the final handler cap's own. -/ +def ClIrqOKA (c : capability.Cap) (f : Bool) {T : Type} (m : Result ((capability.Cap × capability.Cap) × T)) : Prop := + ∀ r, m = ok r → ∀ i, r.1.2 = .IrqHandler i → c = .IrqHandler i ∧ f = true + +theorem ClIrqOKA.ret {c : capability.Cap} {f : Bool} {T : Type} {x : (capability.Cap × capability.Cap) × T} + (h : ∀ i, x.1.2 = .IrqHandler i → c = .IrqHandler i ∧ f = true) : ClIrqOKA c f (ok x) := by + intro r e + cases ok_inj_ff e + exact h + +theorem ClIrqOKA.bind {c : capability.Cap} {f : Bool} {T V : Type} {m : Result V} + {g : V → Result ((capability.Cap × capability.Cap) × T)} (h : ∀ v, ClIrqOKA c f (g v)) : + ClIrqOKA c f (Std.bind m g) := by + intro r e + obtain ⟨x, -, hx⟩ := bind_eq_ok e + exact h x r hx + +theorem ClIrqOKA.ite {c : capability.Cap} {f : Bool} {T : Type} {p : Prop} [Decidable p] + {m n : Result ((capability.Cap × capability.Cap) × T)} (h : p → ClIrqOKA c f m) (h' : ClIrqOKA c f n) : + ClIrqOKA c f (if p then m else n) := by + split + · next hp => exact h hp + · exact h' + +/-- **`finalise_cap`'s IRQ-handler cleanup is the final handler cap's own** (`objects.rs` `finalise_cap`'s +`IrqHandler` arm; `ioport` cleanups are of another kind). -/ +theorem finalise_cap_clA {ar : machine.Arch A} (s : S) (a : A) (c : capability.Cap) (f e : Bool) : + ClIrqOKA c f (objects.finalise_cap st ar s a c f e) := by + unfold objects.finalise_cap + refine ClIrqOKA.bind fun ⟨s1, a1⟩ => ?_ + unfold objects.finalise_cap_object + cases c <;> dsimp only <;> + repeat' first + | exact ClIrqOKA.ret (fun _ h => by first | (cases h; done) | (cases h; exact ⟨rfl, by assumption⟩)) + | refine ClIrqOKA.ite (fun _ => ?_) ?_ + | refine ClIrqOKA.bind (fun _ => ?_) + +/-- A total-correctness post with a fact of every `ok` outcome. -/ +theorem spec_and_okA {T : Type} {m : Result T} {P Q : T → Prop} (h : m ⦃ r => P r ⦄) (h' : ∀ r, m = ok r → Q r) : + m ⦃ r => P r ∧ Q r ⦄ := by + obtain ⟨v, e, hv⟩ : ∃ v, m = ok v ∧ P v := by + revert h; cases m <;> simp [WP.spec_vis, WP.spec_div, WP.spec_ok] + rw [e, WP.spec_ok] + exact ⟨hv, h' v e⟩ + +/-- **One finalisation round** (module docstring): the shape of `FinHookA`'s conclusion, for every kind; and an +IRQ-handler cleanup leaves no other slot with that handler (`finalHypA`: the cap was final). -/ +theorem fin_stepA0 {ar : machine.Arch A} {T : Set U64} {Fn : FinFnA} {W Wx Wp : DelWA S A} + {Rd Rz : S → A → Fl4SpecA64.State Unit → MdbGhostA → U64 → Prop} + (H : DelHypsA st ar T th OS Fn W Wx Wp Rd Rz) (D : DStateA st th OS s σ G) {a : A} (hW : W s a σ G) + {sl : CslotPtr} (hsl : AbsSlotA th sl) {c : capability.Cap} (hc : capA st s (cslotAddrA sl) = c) + (hcn : c ≠ .Null) {ac : Fl4SpecA64.Cap} (hg : getCap (Z := Unit) sl σ = ({(ac, σ)}, False)) + (hac : CapEqA c ac) {f : Bool} (hf : NeedsFinCA c → (f = true ↔ isFinalCap' ac σ)) : + objects.finalise_cap st ar s a c f false ⦃ r => + r.1.1 = finRemA c f ∧ CleanCapA r.1.2 ∧ CapWfA r.1.2 ∧ + ∃ σ' G', (∃ d : Bool, (d = true ↔ isFinalCap' ac σ) ∧ + ((absCapA r.1.1, absCapA r.1.2), σ') ∈ (Fn ac d σ).1) ∧ + DStateA st th OS r.2.1 σ' G' ∧ capA st r.2.1 (cslotAddrA sl) = c ∧ + (∀ y ∈ OS, y ≠ cslotAddrA sl → capA st r.2.1 y = capA st s y ∨ + (capA st r.2.1 y = .Null ∧ ¬ IsZombieA (capA st s y))) ∧ + WFA W Wx r.1.1 (cslotAddrA sl) r.2.1 r.2.2 σ' G' ∧ + (RmOKA r.1.1 (cslotAddrA sl) → Rd r.2.1 r.2.2 σ' G' (cslotAddrA sl)) ∧ + (∀ q, c = .Thread q → ∀ p zt n, r.1.1 = .Zombie p zt n → Rz r.2.1 r.2.2 σ' G' (cslotAddrA sl)) ⦄ := by + by_cases hk : FinObjKindA c + · refine WP.spec_mono (H.fin s a σ G sl c ac f D hW hsl hc hk hg hac hf) ?_ + rintro r ⟨h1, h2, h3, σ', G', h4, h5, h6, h7, h8, h9, h10⟩ + exact ⟨h1, h2, h3, σ', G', h4, h5, h6, h7, h8, h9, fun _ _ => h10⟩ + · have hxO : cslotAddrA sl ∈ OS := D.mem_slot hsl + have hwf : CapWfA c := hc ▸ D.wf hxO + have hae : absCapA c = ac := hac.eq_abs fun p asid va e => hk (by rw [e]; trivial) + subst hae + let d : Bool := open Classical in decide (isFinalCap' (absCapA c) σ) + have hd : d = true ↔ isFinalCap' (absCapA c) σ := by classical exact decide_eq_true_iff + obtain ⟨e, hspec, -⟩ := finalise_cap_trivA (st := st) ar D a hsl hc hk hf hd + rw [e, WP.spec_ok] + have hag := H.agree c hk hwf + refine ⟨rfl, trivial, trivial, σ, G, ⟨d, hd, by rw [hag]; exact hspec⟩, D, hc, fun _ _ _ => Or.inl rfl, + ⟨fun _ => hW, fun _ => H.wx _ _ _ _ hW⟩, fun hrm => ?_, fun q hq => ?_⟩ + · refine H.triv s a σ G sl c D hW hsl hc hcn hk (fun p r gs g e' fin => ?_) (fun p zt n e' => ?_) + · subst e' + have hft : f = true := (hf trivial).2 fin + have hr64 : r.val < 64 := hwf.1 + have hv : (⟨BitVec.ofNat 64 (2 ^ r.val)⟩ : U64).val = 2 ^ r.val := by + show (BitVec.ofNat 64 (2 ^ r.val)).toNat = _ + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (Nat.pow_lt_pow_right (by decide) hr64)] + simp only [finRemA, hft, ↓reduceIte] at hrm + rcases hrm with h | ⟨p', zt', n', h, h'⟩ + · cases h + · cases h + rw [hv] at h' + have hpos : 0 < 2 ^ r.val := Nat.two_pow_pos _ + rcases h' with h' | ⟨h1, h2⟩ + · omega + · refine ⟨?_, h2⟩ + rcases Nat.eq_zero_or_pos r.val with h0 | h0 + · exact h0 + · have : 2 ≤ 2 ^ r.val := by + calc 2 = 2 ^ 1 := rfl + _ ≤ 2 ^ r.val := Nat.pow_le_pow_right (by decide) h0 + omega + · subst e' + simp only [finRemA] at hrm + rcases hrm with h | ⟨p', zt', n', h, h'⟩ + · cases h + · cases h; exact h' + · subst hq; exact (hk trivial).elim + + +theorem fin_stepA {ar : machine.Arch A} {T : Set U64} {Fn : FinFnA} {W Wx Wp : DelWA S A} + {Rd Rz : S → A → Fl4SpecA64.State Unit → MdbGhostA → U64 → Prop} + (H : DelHypsA st ar T th OS Fn W Wx Wp Rd Rz) (D : DStateA st th OS s σ G) {a : A} (hW : W s a σ G) + {sl : CslotPtr} (hsl : AbsSlotA th sl) {c : capability.Cap} (hc : capA st s (cslotAddrA sl) = c) + (hcn : c ≠ .Null) {ac : Fl4SpecA64.Cap} (hg : getCap (Z := Unit) sl σ = ({(ac, σ)}, False)) + (hac : CapEqA c ac) {f : Bool} (hf : NeedsFinCA c → (f = true ↔ isFinalCap' ac σ)) : + objects.finalise_cap st ar s a c f false ⦃ r => + r.1.1 = finRemA c f ∧ CleanCapA r.1.2 ∧ CapWfA r.1.2 ∧ + ∃ σ' G', (∃ d : Bool, (d = true ↔ isFinalCap' ac σ) ∧ + ((absCapA r.1.1, absCapA r.1.2), σ') ∈ (Fn ac d σ).1) ∧ + DStateA st th OS r.2.1 σ' G' ∧ capA st r.2.1 (cslotAddrA sl) = c ∧ + (∀ y ∈ OS, y ≠ cslotAddrA sl → capA st r.2.1 y = capA st s y ∨ + (capA st r.2.1 y = .Null ∧ ¬ IsZombieA (capA st s y))) ∧ + WFA W Wx r.1.1 (cslotAddrA sl) r.2.1 r.2.2 σ' G' ∧ + (RmOKA r.1.1 (cslotAddrA sl) → Rd r.2.1 r.2.2 σ' G' (cslotAddrA sl)) ∧ + (∀ q, c = .Thread q → ∀ p zt n, r.1.1 = .Zombie p zt n → Rz r.2.1 r.2.2 σ' G' (cslotAddrA sl)) ∧ + (∀ i, r.1.2 = .IrqHandler i → ∀ y ∈ OS, y ≠ cslotAddrA sl → capA st r.2.1 y ≠ .IrqHandler i) ⦄ := by + refine WP.spec_mono (spec_and_okA (fin_stepA0 H D hW hsl hc hcn hg hac hf) (finalise_cap_clA (st := st) s a c f false)) ?_ + rintro r ⟨⟨h1, h2, h3, σ', G', h4, h5, h6, h7, h8, h9, h10⟩, hcl⟩ + refine ⟨h1, h2, h3, σ', G', h4, h5, h6, h7, h8, h9, h10, fun i hi y hy hne => ?_⟩ + obtain ⟨ec, ef⟩ := hcl i hi + subst ec + have hxO : cslotAddrA sl ∈ OS := D.mem_slot hsl + have hwf : CapWfA (.IrqHandler i) := hc ▸ D.wf hxO + have hae : absCapA (.IrqHandler i) = ac := hac.eq_abs fun p asid va e => by cases e + have fin : isFinalCap' (absCapA (.IrqHandler i)) σ := hae ▸ (hf trivial).1 ef + have F := D.toFinStA + have others : ∀ w ∈ OS, w ≠ cslotAddrA sl → capA st s w ≠ .IrqHandler i := by + refine (isFinalCap_iffA F hsl hc (fun d => d = .IrqHandler i) (fun z hz => ?_) rfl (by simp)).1 fin + rw [genRefs_irqA] + exact overlap_irqA hwf (F.wf hz) + rcases h7 y hy hne with e | ⟨e, -⟩ + · rw [e]; exact others y hy hne + · rw [e]; simp + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/CnodeDelStepA.lean b/proofs/Fl4ProofsA64/P3/CnodeDelStepA.lean new file mode 100644 index 000000000..cf5a2071a --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/CnodeDelStepA.lean @@ -0,0 +1,372 @@ +/- +P3 (AArch64, A64-P3 CNode wave 2): **the deletion's related states across its slot steps**. + +`DStateA` (`P3/CnodeDelInvA`) holds A64 P1's MDB facts and the three [J] facts of finality (`P3/CnodeFinalA`). Here +each of them is carried across a swap of two caps (**`DStateA.swapped`**) and across the removal of one slot +(**`DStateA.removed`**); the spec side of the converse object coverage `FinCovA` is **`WfKeepA`** (a spec slot step +keeps which objects are well-formed CNodes of each radix). FL4's two slot steps then give `DStateA` again, from `DStateA` alone: +**`empty_slot_sA`**, **`cap_swap_for_delete_sA`**. + +No new named premise: `FrBaseApartA` and `IrqRunA` (module docstring of `P3/CnodeFinalA`) are preserved here by the +loop's steps. +-/ +import Fl4ProofsA64.P3.CnodeDelHookA +import Fl4ProofsA64.P3.SlotJA +import Fl4ProofsA64.P1.MdbLoop +import Fl4ProofsA64.P1.MdbSwap +import Fl4ProofsA64.P3.DelSwapMdbA +import Fl4ProofsA64.P3.DelMdbLeafA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## The spec side of the converse coverage -/ + +/-- **A spec step that keeps which objects are well-formed CNodes of each radix.** -/ +def WfKeepA (σ σ' : Fl4SpecA64.State Unit) : Prop := + ∀ q n, (∃ cs, σ'.kheap q = some (.cnode n cs) ∧ wellFormedCnodeN n cs) ↔ + (∃ cs, σ.kheap q = some (.cnode n cs) ∧ wellFormedCnodeN n cs) + +theorem WfKeepA.refl (σ : Fl4SpecA64.State Unit) : WfKeepA σ σ := fun _ _ => Iff.rfl + +theorem WfKeepA.trans {σ1 σ2 σ3 : Fl4SpecA64.State Unit} (h : WfKeepA σ1 σ2) (h' : WfKeepA σ2 σ3) : + WfKeepA σ1 σ3 := fun q n => (h' q n).trans (h q n) + +theorem WfKeepA.of_kheap {σ σ' : Fl4SpecA64.State Unit} (h : σ'.kheap = σ.kheap) : WfKeepA σ σ' := + fun q n => by rw [h] + +/-- Every outcome of `f` keeps the well-formed CNodes. -/ +def NdWfA {α : Type} (f : NonDet (Fl4SpecA64.State Unit) α) : Prop := ∀ σ r, r ∈ (f σ).1 → WfKeepA σ r.2 + +theorem NdWfA.keep {α : Type} {f : NonDet (Fl4SpecA64.State Unit) α} (h : NdK f) : NdWfA f := + fun σ r hr => WfKeepA.of_kheap (h σ r hr).1 + +theorem NdWfA.bind {α β : Type} {m : NonDet (Fl4SpecA64.State Unit) α} {f : α → NonDet (Fl4SpecA64.State Unit) β} + (hm : NdWfA m) (hf : ∀ a, NdWfA (f a)) : NdWfA (m >>= f) := fun σ r hr => by + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + exact (hm σ x hx).trans (hf x.1 x.2 r hr) + +theorem setCap_wfA (cap : Cap) (sl : CslotPtr) : NdWfA (Fl4SpecA64.setCap (Z := Unit) cap sl) := by + obtain ⟨p, cref⟩ := sl + intro σ r hr q n + obtain ⟨o, o', ho, hr2, -, hsh⟩ := setCap_memA hr + rw [hr2] + by_cases e : q = p + · subst e; simp only [Function.update_self, Option.some.injEq, ho]; exact hsh n + · simp [Function.update_of_ne e] + +syntax "wf_step" : tactic +macro_rules + | `(tactic| wf_step) => `(tactic| first + | exact NdWfA.keep (ndk_pure _) | exact NdWfA.keep (ndk_pure' _) | exact NdWfA.keep (ndk_gets _) + | exact NdWfA.keep ndk_get | exact NdWfA.keep (ndk_assert _) | exact NdWfA.keep (ndk_getCap _) + | exact NdWfA.keep (ndk_setCdt _) | exact NdWfA.keep (ndk_updateCdt _) | exact NdWfA.keep (ndk_setOriginal _ _) + | exact NdWfA.keep (ndk_doExtendedOp _) | exact NdWfA.keep (ndk_postCapDeletion _) + | exact setCap_wfA _ _ + | (refine NdWfA.bind ?_ (fun _ => ?_))) + +theorem capSwap_wfA (c1 c2 : Cap) (s1 s2 : CslotPtr) : NdWfA (capSwap (Z := Unit) c1 s1 c2 s2) := by + unfold capSwap; repeat wf_step + +theorem capSwapForDelete_wfA (s1 s2 : CslotPtr) : NdWfA (capSwapForDelete (Z := Unit) s1 s2) := by + unfold capSwapForDelete NonDet.when + split_ifs + · exact NdWfA.bind (NdWfA.keep (ndk_getCap _)) fun _ => + NdWfA.bind (NdWfA.keep (ndk_getCap _)) fun _ => capSwap_wfA _ _ _ _ + · exact NdWfA.keep (ndk_pure _) + +theorem emptySlot_wfA (sl : CslotPtr) (info : Cap) : NdWfA (emptySlot (Z := Unit) sl info) := by + unfold emptySlot + refine NdWfA.bind (NdWfA.keep (ndk_getCap _)) fun cap => ?_ + split + · exact NdWfA.keep (ndk_pure _) + · repeat wf_step + +/-- The converse coverage across a spec slot step. -/ +theorem FinCovA.step {th : TypedHeapA} {σ σ' : Fl4SpecA64.State Unit} (h : FinCovA th σ) (H : SchedFrA σ σ') + (W : WfKeepA σ σ') : FinCovA th σ' := by + refine ⟨fun q r cs hk hw => ?_, fun q x hk => ?_⟩ + · obtain ⟨cs0, hk0, hw0⟩ := (W q r).1 ⟨cs, hk, hw⟩ + exact h.cnode q r cs0 hk0 hw0 + · obtain ⟨x0, hk0, -⟩ := H.tcb' q x hk + exact h.tcb q x0 hk0 + +/-! ## The state facts across a swap and a removal -/ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {OS : Set U64} {s s' : S} + +/-- [J] `FrBaseApartA` holds again when every frame and every range key cap of the new state was one of the old +state (somewhere in `OS`), at the same base. -/ +theorem FrBaseApartA.of (h : FrBaseApartA st OS s) + (hf : ∀ y ∈ OS, isFrA (capA st s' y) = true → + ∃ y' ∈ OS, isFrA (capA st s y') = true ∧ ptrA (capA st s y') = ptrA (capA st s' y)) + (hk : ∀ z ∈ OS, RngKeyA (capA st s' z) → + ∃ z' ∈ OS, RngKeyA (capA st s z') ∧ ptrA (capA st s z') = ptrA (capA st s' z)) : + FrBaseApartA st OS s' := by + intro y hy z hz fy kz e + obtain ⟨y', hy', fy', ey⟩ := hf y hy fy + obtain ⟨z', hz', kz', ez⟩ := hk z hz kz + exact h y' hy' z' hz' fy' kz' (by rw [ez, ey, e]) + +/-- [J] `IrqRunA` along the renaming of two slots. -/ +theorem IrqRunA.ren {G : List (List U64)} (h : IrqRunA st G s) {x1 x2 : U64} + (hc : ∀ y ∈ G.flatten, capA st s' (swp x1 x2 y) = capA st s y) : IrqRunA st (renG x1 x2 G) s' := by + refine ⟨fun y hy y' hy' i k k' => ?_, fun ch hch a b c y z y' i ha hb hc' hab hbc ky ky' => ?_⟩ + · have a := mem_renG_flat.1 hy + have a' := mem_renG_flat.1 hy' + have e : capA st s' y = capA st s (swp x1 x2 y) := by rw [← hc _ a, swp_swp] + have e' : capA st s' y' = capA st s (swp x1 x2 y') := by rw [← hc _ a', swp_swp] + rw [e] at k; rw [e'] at k' + obtain ⟨c, hc0, h1, h2⟩ := h.one _ a _ a' i k k' + exact ⟨_, List.mem_map.2 ⟨c, hc0, rfl⟩, List.mem_map.2 ⟨_, h1, swp_swp x1 x2 y⟩, + List.mem_map.2 ⟨_, h2, swp_swp x1 x2 y'⟩⟩ + · obtain ⟨c0, hc0, rfl⟩ := List.mem_map.1 hch + rw [List.getElem?_map] at ha hb hc' + obtain ⟨y0, hy0, rfl⟩ := Option.map_eq_some_iff.1 ha + obtain ⟨z0, hz0, rfl⟩ := Option.map_eq_some_iff.1 hb + obtain ⟨y1, hy1, rfl⟩ := Option.map_eq_some_iff.1 hc' + have mem : ∀ {n : Nat} {w : U64}, c0[n]? = some w → w ∈ G.flatten := fun e => + List.mem_flatten.2 ⟨c0, hc0, List.mem_of_getElem? e⟩ + rw [hc _ (mem hy0)] at ky; rw [hc _ (mem hy1)] at ky'; rw [hc _ (mem hz0)] + exact h.chunk c0 hc0 a b c y0 z0 y1 i hy0 hz0 hy1 hab hbc ky ky' + +/-- [J] `IrqRunA` along the removal of a slot from the chains. -/ +theorem IrqRunA.rem {G : List (List U64)} (h : IrqRunA st G s) (hnd : G.flatten.Nodup) {x : U64} + (hk : ∀ y ∈ G.flatten, y ≠ x → capA st s' y = capA st s y) : IrqRunA st (remG x G) s' := by + have hk' : ∀ y ∈ (remG x G).flatten, capA st s' y = capA st s y := fun y hy => + hk y ((remG_flat hnd).1 hy).1 ((remG_flat hnd).1 hy).2 + refine ⟨fun y hy y' hy' i k k' => ?_, fun ch' hch' a b c y z y' i ha hb hc' hab hbc ky ky' => ?_⟩ + · rw [hk' y hy] at k; rw [hk' y' hy'] at k' + have a := (remG_flat hnd).1 hy + have a' := (remG_flat hnd).1 hy' + obtain ⟨c, hc0, hyc, hyc'⟩ := h.one y a.1 y' a'.1 i k k' + have h1 : y ∈ c.erase x := (List.mem_erase_of_ne a.2).2 hyc + exact ⟨_, remG_of hc0 (List.ne_nil_of_mem h1), h1, (List.mem_erase_of_ne a'.2).2 hyc'⟩ + · have mem : ∀ {n w}, ch'[n]? = some w → w ∈ (remG x G).flatten := fun e => + List.mem_flatten.2 ⟨ch', hch', List.mem_of_getElem? e⟩ + rw [hk' y (mem ha)] at ky; rw [hk' y' (mem hc')] at ky' + rw [hk' z (mem hb)] + obtain ⟨c0, hc0, rfl, -⟩ := remG_mem hch' + have hcn := ch_nodup hnd hc0 + by_cases hx : x ∈ c0 + · obtain ⟨i0, hi0, hxi⟩ := List.getElem_of_mem hx + have ex : c0[i0]? = some x := by simp [hi0, hxi] + rw [erase_idx hcn ex] at ha hb hc' + refine h.chunk c0 hc0 _ _ _ y z y' i ha hb hc' ?_ ?_ ky ky' <;> split <;> split <;> omega + · rw [List.erase_of_not_mem hx] at ha hb hc' + exact h.chunk c0 hc0 a b c y z y' i ha hb hc' hab hbc ky ky' + +/-- `ZombiesFinal` when each `OS` cap is kept or nulled. -/ +theorem zombiesFinal_keptA (h : ZombiesFinal st OS s) + (hc : ∀ y ∈ OS, capA st s' y = capA st s y ∨ capA st s' y = .Null) : ZombiesFinal st OS s' := by + intro z hz p t n hz' y hy hne ky + have e1 : capA st s z = .Zombie p t n := by + rcases hc z hz with e | e + · rw [← e]; exact hz' + · rw [e] at hz'; cases hz' + rcases hc y hy with e | e + · rw [e] at ky ⊢; exact h z hz p t n e1 y hy hne ky + · rw [e] at ky; exact absurd ky RngKeyA.not_null + +end + +/-! ## `DStateA` across the steps -/ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {th : TypedHeapA} {OS : Set U64} {s s' : S} {σ σ' : Fl4SpecA64.State Unit} {G : MdbGhostA} + +/-- A zombie's well-formedness is a property of the cap, so any state whose zombies were all zombies before keeps +`DelInvA`. -/ +theorem DelInvA.of (D : DStateA st th OS s σ G) + (hc : ∀ y, IsSlotA th y → ∀ p zt n, capA st s' y = .Zombie p zt n → + ∃ y' ∈ OS, capA st s y' = .Zombie p zt n) : DelInvA st th s' := by + intro y hy p zt n h + obtain ⟨y', hy', h'⟩ := hc y hy p zt n h + exact D.zw y' (D.isSlot_of hy' (by rw [h']; exact fun e => by cases e)) p zt n h' + +/-- **`DStateA` after two caps changed places** (the ghost renamed by the transposition). -/ +theorem DStateA.swapped (D : DStateA st th OS s σ G) {x1 x2 : U64} (hx1 : x1 ∈ OS) (hx2 : x2 ∈ OS) + (hs1 : IsSlotA th x1) (hs2 : IsSlotA th x2) + (hrel : StateRelA th (L.mem s') σ') (hplace : PlaceA th (L.ksb s')) + (hcdt : CdtRelA st th s' σ' (swGA G x1 x2)) (htree : TreeA st OS (swGA G x1 x2).chains (swGA G x1 x2).par s') + (hcov : FinCovA th σ') (hzf : ZombiesFinal st OS s') (hnl : NullLk st OS s') + (hc : ∀ y ∈ OS, capA st s' y = capA st s (swp x1 x2 y)) : DStateA st th OS s' σ' (swGA G x1 x2) := by + have hc' : ∀ y ∈ OS, capA st s' (swp x1 x2 y) = capA st s y := fun y hy => by + rw [hc _ (swp_os hx1 hx2 hy), swp_swp] + have inOS : ∀ y ∈ G.chains.flatten, y ∈ OS := fun y hy => ((D.lk.members y).1 hy).1 + have hc'' : ∀ y ∈ G.chains.flatten, capA st s' (swp x1 x2 y) = capA st s y := fun y hy => hc' y (inOS y hy) + refine + { rel := hrel, place := hplace, cov := hcov, lk := htree.lk, rng := D.rng.ren hc'', + kind := D.kind.ren hx1 hx2 hc', cls := D.cls.ren hx1 hx2 hc', zf := hzf, nz := D.nz, sub := D.sub, + nul := fun y hy hn => ?_, fr := ?_, irq := D.irq.ren hc'', cdt := hcdt, tree := htree, nl := hnl, + zw := ?_, cv := fun y hy => by rw [hc y hy]; exact D.cv _ (swp_os hx1 hx2 hy) } + · have e : swp x1 x2 y = y := by + unfold swp + rw [if_neg (fun e => hn (by rw [e]; exact hs1)), if_neg (fun e => hn (by rw [e]; exact hs2))] + rw [hc y hy, e]; exact D.nul y hy hn + · refine D.fr.of (fun y hy f => ?_) (fun z hz k => ?_) + · rw [hc y hy] at f ⊢; exact ⟨_, swp_os hx1 hx2 hy, f, rfl⟩ + · rw [hc z hz] at k ⊢; exact ⟨_, swp_os hx1 hx2 hz, k, rfl⟩ + · refine DelInvA.of D (fun y hy p zt n h => ?_) + have hyO := D.sub y hy + rw [hc y hyO] at h + exact ⟨_, swp_os hx1 hx2 hyO, h⟩ + +/-- **`DStateA` after one slot was emptied** (the ghost without it). -/ +theorem DStateA.removed (D : DStateA st th OS s σ G) {x : U64} (hx : x ∈ OS) + (hrel : StateRelA th (L.mem s') σ') (hplace : PlaceA th (L.ksb s')) + (hcdt : CdtRelA st th s' σ' (remGA G x)) (htree : TreeA st OS (remGA G x).chains (remGA G x).par s') + (hcov : FinCovA th σ') (hrng : RngOneA64 st (remGA G x).chains s') (hkind : KindA st OS s') + (hnl : NullLk st OS s') (hx0 : capA st s' x = .Null) (hk : ∀ y ∈ OS, y ≠ x → capA st s' y = capA st s y) : + DStateA st th OS s' σ' (remGA G x) := by + have kn : ∀ y ∈ OS, capA st s' y = capA st s y ∨ capA st s' y = .Null := fun y hy => by + by_cases e : y = x + · exact Or.inr (e ▸ hx0) + · exact Or.inl (hk y hy e) + have inOS : ∀ y ∈ G.chains.flatten, y ∈ OS := fun y hy => ((D.lk.members y).1 hy).1 + have back : ∀ y ∈ OS, capA st s' y ≠ .Null → capA st s y = capA st s' y := fun y hy hn => by + rcases kn y hy with e | e + · exact e.symm + · exact absurd e hn + refine + { rel := hrel, place := hplace, cov := hcov, lk := htree.lk, rng := hrng, kind := hkind, + cls := D.cls.rem hx0 (fun y hy ne => hk y hy ne), zf := zombiesFinal_keptA D.zf kn, nz := D.nz, sub := D.sub, + nul := fun y hy hn => ?_, fr := ?_, + irq := D.irq.rem D.lk.nodup (fun y hy ne => hk y (inOS y hy) ne), cdt := hcdt, tree := htree, nl := hnl, + zw := ?_, cv := fun y hy => by + rcases kn y hy with e | e <;> rw [e] + · exact D.cv y hy + · trivial } + · rcases kn y hy with e | e + · rw [e]; exact D.nul y hy hn + · exact e + · refine D.fr.of (fun y hy f => ?_) (fun z hz k => ?_) + · have n0 : capA st s' y ≠ .Null := fun e => by rw [e] at f; exact Bool.false_ne_true f + exact ⟨y, hy, by rw [back y hy n0]; exact f, by rw [back y hy n0]⟩ + · have n0 : capA st s' z ≠ .Null := fun e => RngKeyA.not_null (e ▸ k) + exact ⟨z, hz, by rw [back z hz n0]; exact k, by rw [back z hz n0]⟩ + · refine DelInvA.of D (fun y hy p zt n h => ?_) + have hyO := D.sub y hy + have n0 : capA st s' y ≠ .Null := fun e => by rw [e] at h; cases h + exact ⟨y, hyO, by rw [back y hyO n0]; exact h⟩ + +theorem DStateA.mdbS (D : DStateA st th OS s σ G) : MdbS st OS s := + ⟨G.chains, D.lk, D.rng, D.kind, D.nl, D.cls⟩ + +end + +/-! ## The leaves at `DStateA` -/ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {A : Type} {T Ω Sg OS : Set U64} {th : TypedHeapA} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} + {G : MdbGhostA} + +/-- **`cdt::empty_slot` with a `Null` cleanup keeps the deletion's states.** -/ +theorem empty_slot_sA {ar : machine.Arch A} (hO : OSOK T Ω Sg OS) (D : DStateA st th OS s σ G) + {xs : CslotPtr} (hxs : AbsSlotA th xs) (hcn : capA st s (cslotAddrA xs) ≠ .Null) : + cdt.empty_slot st ar s a (cslotAddrA xs) .Null ⦃ q => q.2 = a ∧ SlotFrameA (st := st) T OS s q.1 ∧ + capA st q.1 (cslotAddrA xs) = .Null ∧ + (∀ y ∈ OS, y ≠ cslotAddrA xs → capA st q.1 y = capA st s y) ∧ + ∃ σ', ((), σ') ∈ (emptySlot (Z := Unit) xs .nullCap σ).1 ∧ SpecSlotA σ σ' ∧ + DStateA st th OS q.1 σ' (remGA G (cslotAddrA xs)) ∧ EmpMdbA st G.chains (cslotAddrA xs) s q.1 ⦄ := by + have hxO : cslotAddrA xs ∈ OS := D.mem_slot hxs + have hxG : cslotAddrA xs ∈ G.chains.flatten := (D.lk.members _).2 ⟨hxO, hcn⟩ + have Fl := D.lk.lkF hO.nz + have inOS : ∀ y ∈ G.chains.flatten, y ∈ OS := fun y hy => ((D.lk.members y).1 hy).1 + have hMW := empty_slot_mdbWA (ar := ar) hO hO.nz D.lk D.rng D.kind (a := a) hxO (cl := .Null) + (Pw := fun _ => False) (fun _ h => h.elim) + (fun s1 a1 _ => by rw [pcd_null]; exact (WP.spec_ok _).2 (WritesIn.refl _ _)) hcn + refine WP.spec_mono (WP.spec_and (WP.spec_and (WP.spec_and (WP.spec_and + (empty_slot_refinesA s a σ G D.rel D.cdt D.place hO D.tree D.rng D.kind D.sub D.nul hxs hcn) + (emptySlot_osA hO a hxO (fun h => inOS _ (Fl.pvm _ hxG h)) (fun h => inOS _ (Fl.nxm _ hxG h)))) + (emptySlot_treeGA hO D.tree D.rng D.kind a hxO hcn)) + (cdt.empty_slot.mdbN hO D.nz D.lk D.rng D.kind D.nl D.cls (ar := ar) (a := a) hxO (cl := .Null) + (Pw := fun _ => False) (fun _ h => h.elim) + (fun s1 a1 _ => by rw [pcd_null]; exact (WP.spec_ok _).2 (WritesIn.refl _ _)))) hMW) ?_ + rintro q ⟨⟨⟨⟨⟨har, σ', hm, hst, hcdt, hT, hRO, hK, hSS, -⟩, hw⟩, -, -, -, hcr, hx0, -⟩, + -, -, -, -, hnl, -⟩, hmw⟩ + have H : SchedFrA σ σ' := emptySlot_ndr xs .nullCap σ _ hm + have W : WfKeepA σ σ' := emptySlot_wfA xs .nullCap σ _ hm + have hP : PlaceA th (L.ksb q.1) := by rw [hw.1]; exact D.place + exact ⟨har, hw, hx0, hcr, σ', hm, hSS, + D.removed hxO hst hP hcdt hT (D.cov.step H W) hRO hK hnl hx0 hcr, hmw⟩ + +/-- **`cdt::cap_swap_for_delete` of two non-null slots keeps the deletion's states.** -/ +theorem cap_swap_for_delete_sA (hO : OSOK T Ω Sg OS) (D : DStateA st th OS s σ G) + {sa sb : CslotPtr} (hA1 : AbsSlotA th sa) (hA2 : AbsSlotA th sb) (hne : sa ≠ sb) + (hn1 : capA st s (cslotAddrA sa) ≠ .Null) (hn2 : capA st s (cslotAddrA sb) ≠ .Null) + (hs1 : obj.sext48 (cslotAddrA sa) = ok (cslotAddrA sa)) + (hs2 : obj.sext48 (cslotAddrA sb) = ok (cslotAddrA sb)) : + cdt.cap_swap_for_delete st s (cslotAddrA sa) (cslotAddrA sb) ⦃ s' => SlotFrameA (st := st) T OS s s' ∧ + (∀ y ∈ OS, capA st s' y = capA st s (swp (cslotAddrA sa) (cslotAddrA sb) y)) ∧ + ∃ σ', ((), σ') ∈ (Fl4SpecA64.capSwapForDelete (Z := Unit) sa sb σ).1 ∧ SpecSlotA σ σ' ∧ + DStateA st th OS s' σ' (swGA G (cslotAddrA sa) (cslotAddrA sb)) ∧ + SwapMdbA st G.chains (cslotAddrA sa) (cslotAddrA sb) s s' ⦄ := by + have hP := D.place + have hx1 : cslotAddrA sa ∈ OS := D.mem_slot hA1 + have hx2 : cslotAddrA sb ∈ OS := D.mem_slot hA2 + have hS1 : IsSlotA th (cslotAddrA sa) := AbsSlotA.isSlot hP hA1 + have hS2 : IsSlotA th (cslotAddrA sb) := AbsSlotA.isSlot hP hA2 + have hAne : cslotAddrA sa ≠ cslotAddrA sb := fun h => hne (cslotAddrA_inj hP hA1 hA2 h) + have hG1 : cslotAddrA sa ∈ G.chains.flatten := (D.lk.members _).2 ⟨hx1, hn1⟩ + have hG2 : cslotAddrA sb ∈ G.chains.flatten := (D.lk.members _).2 ⟨hx2, hn2⟩ + have Fl := D.lk.lkF hO.nz + have inOS : ∀ y ∈ G.chains.flatten, y ∈ OS := fun y hy => ((D.lk.members y).1 hy).1 + have hmain : cdt.cap_swap_for_delete st s (cslotAddrA sa) (cslotAddrA sb) ⦃ s' => + (SlotFrameA (st := st) T OS s s' ∧ + ∀ y ∈ OS, capA st s' y = capA st s (swp (cslotAddrA sa) (cslotAddrA sb) y)) ∧ + ∃ σ', ((), σ') ∈ (Fl4SpecA64.capSwapForDelete (Z := Unit) sa sb σ).1 ∧ SpecSlotA σ σ' ∧ + StateRelA th (L.mem s') σ' ∧ CdtRelA st th s' σ' (swGA G (cslotAddrA sa) (cslotAddrA sb)) ∧ + TreeA st OS (swGA G (cslotAddrA sa) (cslotAddrA sb)).chains + (swGA G (cslotAddrA sa) (cslotAddrA sb)).par s' ⦄ := by + unfold cdt.cap_swap_for_delete + simp only [obj.Slot.Insts.CoreCmpPartialEqSlot.eq, bind_ok] + split + · rename_i heq + exact absurd (by simpa using heq) hAne + rw [capA_eq, bind_ok, capA_eq, bind_ok] + obtain ⟨b1, hg1, hcb1⟩ := D.rel.getAbsA hA1 + obtain ⟨b2, hg2, hcb2⟩ := D.rel.getAbsA hA2 + have hw1 : CapRt.EncWf (capA st s (cslotAddrA sa)) := CapRt.decodeF_encWf _ _ + have hw2 : CapRt.EncWf (capA st s (cslotAddrA sb)) := CapRt.decodeF_encWf _ _ + refine WP.spec_mono (WP.spec_and (WP.spec_and + (cap_swap_refinesA s σ G D.rel D.cdt hP hO D.tree D.sub D.nul hA1 hA2 hne hn1 hn2 hs1 hs2 hcb1 hcb2 + hw1 hw2 hn1 hn2 (UA.refl _) (UA.refl _)) + (cdt.swap.lrWF (osokA hO) (LR.self s) (inOS _ hG1) (inOS _ hG2) hAne hs1 hs2 hw1 hw2 + (fun h => inOS _ (Fl.pvm _ hG1 h)) (fun h => inOS _ (Fl.nxm _ hG1 h)) + (fun h => inOS _ (Fl.pvm _ hG2 h)) (fun h => inOS _ (Fl.nxm _ hG2 h)))) + (cdt.swap.lrN hO D.nz D.lk D.nl hx1 hG2 hAne hs1 hs2 rfl rfl)) ?_ + rintro s' ⟨⟨⟨σ', hm, hst, hcdt, hT, hSS, -⟩, hw, -⟩, -, e1, e2, ek⟩ + refine ⟨⟨hw, fun y hy => ?_⟩, σ', ?_, hSS, hst, hcdt, hT⟩ + · unfold swp + split_ifs with h1 h2 + · subst h1; exact e1 + · subst h2; exact e2 + · exact ek y hy h1 h2 + · unfold Fl4SpecA64.capSwapForDelete NonDet.when + rw [if_pos hne] + exact mem_bindA.2 ⟨(b1, σ), hg1, mem_bindA.2 ⟨(b2, σ), hg2, hm⟩⟩ + have hMW := cdt_cap_swap_for_delete_mdbWA hO hO.nz D.lk D.nl hx1 hG2 hAne hs1 hs2 + refine WP.spec_mono (WP.spec_and (WP.spec_and (WP.spec_and hmain (cdt.cap_swap_for_delete.zf hO D.zf D.mdbS hx1 hx2 hs1 hs2)) + (cdt.cap_swap_for_delete.mdbN hO D.nz D.lk D.rng D.kind D.nl D.cls hx1 hG2 hs1 hs2)) hMW) ?_ + rintro s' ⟨⟨⟨⟨⟨hw, hc⟩, σ', hm, hSS, hst, hcdt, hT⟩, hzf⟩, -, -, -, -, hnl, -⟩, hmw⟩ + have H : SchedFrA σ σ' := capSwapForDelete_ndr sa sb σ _ hm + have W : WfKeepA σ σ' := capSwapForDelete_wfA sa sb σ _ hm + have hP' : PlaceA th (L.ksb s') := by rw [hw.1]; exact D.place + exact ⟨hw, hc, σ', hm, hSS, D.swapped hx1 hx2 hS1 hS2 hst hP' hcdt hT (D.cov.step H W) hzf hnl hc, hmw⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/CnodeDelSwapA.lean b/proofs/Fl4ProofsA64/P3/CnodeDelSwapA.lean new file mode 100644 index 000000000..ea7c9822a --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/CnodeDelSwapA.lean @@ -0,0 +1,190 @@ +/- +P3 (AArch64, A64-P3 CNode wave 2): **the deletion's swap of a zombie into its first slot, the first slot possibly +empty** (x86 `P3/DelAbs.swap_null_full`, `DelOps.swap_home`). + +`reduce_zombie slot false` swaps the zombie's first slot `p` with the slot holding the zombie; slot `p` need not hold +a cap (the spec's `cap_swap_for_delete` swaps an empty slot too). S3's `cap_swap_refinesA` asks both slots non-null, +so: + +* **`cap_swap_null_refinesA`**: the refinement of `cap_swap` when the first slot is empty (links clear, `NullLk`): + S3's proof of `cap_swap_refinesA` with P1's `cdt.swap.treeN` / `lrN` in place of the non-null tree. +* **`cap_swap_for_delete_hsA`**: `cdt::cap_swap_for_delete` of any `OS` slot (null or not) with a non-null slot keeps + `DStateA` at the ghost renamed by the transposition; the non-null case is `cap_swap_for_delete_sA`. + +No named premise. +-/ +import Fl4ProofsA64.P3.CnodeDelStepA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +theorem encNull : CapRt.EncWf (capability.Cap.Null) := by simp [CapRt.EncWf] + +set_option maxHeartbeats 2000000 in +/-- **`cdt::swap` of an empty slot with a linked slot refines `cap_swap nullCap sa a2 sb`** (module docstring). -/ +theorem cap_swap_null_refinesA {T Ω Sg OS : Set U64} {th : TypedHeapA} {kb : U64} (s : S) (σ : State Unit) + (G : MdbGhostA) (hR : StateRelA th (L.mem s) σ) (hC : CdtRelA st th s σ G) (hP : PlaceA th kb) + (hO : OSOK T Ω Sg OS) (hT : TreeA st OS G.chains G.par s) (hN : NullLk st OS s) + (hsub : ∀ y, IsSlotA th y → y ∈ OS) (hnul : ∀ y ∈ OS, ¬ IsSlotA th y → capA st s y = .Null) + {sa sb : CslotPtr} (hA1 : AbsSlotA th sa) (hA2 : AbsSlotA th sb) (hne : sa ≠ sb) + (hn1 : capA st s (cslotAddrA sa) = .Null) + (hn2 : capAtA (L.mem s) (cslotAddrA sb) ≠ .Null) + (hs1 : obj.sext48 (cslotAddrA sa) = ok (cslotAddrA sa)) + (hs2 : obj.sext48 (cslotAddrA sb) = ok (cslotAddrA sb)) + {c2 : capability.Cap} {a2 : Cap} (hca2 : CapRelA c2 a2) (hw2 : CapRt.EncWf c2) + (hc2 : capA st s (cslotAddrA sb) = c2) : + cdt.swap st s .Null (cslotAddrA sa) c2 (cslotAddrA sb) ⦃ s' => + ∃ σ', ((), σ') ∈ (Fl4SpecA64.capSwap (Z := Unit) .nullCap sa a2 sb σ).1 ∧ + StateRelA th (L.mem s') σ' ∧ + CdtRelA st th s' σ' (swGA G (cslotAddrA sa) (cslotAddrA sb)) ∧ + TreeA st OS (swGA G (cslotAddrA sa) (cslotAddrA sb)).chains + (swGA G (cslotAddrA sa) (cslotAddrA sb)).par s' ∧ SpecSlotA σ σ' ∧ + ∀ y, IsSlotA th y → capAtA (L.mem s') y = capAtA (L.mem s) y ∨ capAtA (L.mem s') y = .Null ∨ + capAtA (L.mem s') y = c2 ⦄ := by + have hS1 := hA1.isSlot hP + have hS2 := hA2.isSlot hP + have hAne : cslotAddrA sa ≠ cslotAddrA sb := fun h => hne (cslotAddrA_inj hP hA1 hA2 h) + have hx1 : cslotAddrA sa ∈ OS := hsub _ hS1 + have hG2 : cslotAddrA sb ∈ G.chains.flatten := (hT.lk.members _).2 ⟨hsub _ hS2, hn2⟩ + have F := hT.lk.lkF hO.nz + have inOS : ∀ y ∈ G.chains.flatten, y ∈ OS := fun y hy => ((hT.lk.members y).1 hy).1 + have N1 := hN _ hx1 hn1 + refine WP.spec_mono (WP.spec_and (WP.spec_and + (cdt.swap.treeN hO hT hN hx1 hG2 hAne hs1 hs2 hn1 hc2) + (cdt.swap.lrW hO (LR.self s) hx1 (inOS _ hG2) hAne hs1 hs2 encNull hw2 + (fun h => absurd N1.1 h) (fun h => absurd N1.2 h) + (fun h => inOS _ (F.pvm _ hG2 h)) (fun h => inOS _ (F.nxm _ hG2 h)))) + (swap_bitA hO (LR.self s) hx1 (inOS _ hG2) hAne hs1 hs2 encNull hw2 + (fun h => absurd N1.1 h) (fun h => absurd N1.2 h) + (fun h => inOS _ (F.pvm _ hG2 h)) (fun h => inOS _ (F.nxm _ hG2 h)))) ?_ + rintro s' ⟨⟨hT', -, e1, e2, ek⟩, hb, hb1, hb2⟩ + have hk' : ∀ y, IsSlotA th y → y ≠ cslotAddrA sa → y ≠ cslotAddrA sb → + capAtA (L.mem s') y = capAtA (L.mem s) y := fun y hy f1 f2 => ek y (hsub y hy) f1 f2 + have hca1 : CapRelA (.Null : capability.Cap) .nullCap := ⟨trivial, rfl⟩ + obtain ⟨o1, h1, hR1, hQ1⟩ := StateRelA.setAbsA (mem' := mixA (L.mem s) (L.mem s') (cslotAddrA sb)) hR hP hA1 + (a := a2) (by + rw [capAt_mixA_other _ _ (slot_ne8 hP hS2 hS1 hAne)] + show CapRelA (capA st s' _) a2 + rw [e1]; exact hca2) + (fun y hy hy1 => by + by_cases e : y = cslotAddrA sb + · rw [e]; exact capAt_mixA_self _ _ _ + · rw [capAt_mixA_other _ _ (slot_ne8 hP hS2 hy e)]; exact hk' y hy hy1 e) + obtain ⟨o2, h2, hR2, hQ2⟩ := StateRelA.setAbsA (mem' := L.mem s') hR1 hP hA2 (a := .nullCap) + (by show CapRelA (capA st s' _) .nullCap; rw [e2]; exact hca1) + (fun y hy hy2 => (capAt_mixA_other _ _ (slot_ne8 hP hS2 hy hy2)).symm) + refine ⟨_, capSwap_memA h1 h2, hR2.of_kheap rfl, CdtRelA.swapA hP hC hA1 hA2 ?_ (swap_cdt_eqA _ _ _ hne) + (swap_io_eqA _ _ _ hne) ?_, hT', + (hQ1.trans hQ2).trans (SpecSlotA.swFin _ _ _), + fun y hy => ?_⟩ + · refine hT'.sub (fun y hy => hsub y hy) fun y hy hn' => ?_ + have f1 : y ≠ cslotAddrA sa := fun e => hn' (e ▸ hS1) + have f2 : y ≠ cslotAddrA sb := fun e => hn' (e ▸ hS2) + rw [ek y hy f1 f2]; exact hnul y hy hn' + · intro sl hsl + by_cases f1 : sl = sa + · rw [f1, Equiv.swap_apply_left]; exact revA_of_bit hb1 + · by_cases f2 : sl = sb + · rw [f2, Equiv.swap_apply_right]; exact revA_of_bit hb2 + · rw [Equiv.swap_apply_of_ne_of_ne f1 f2] + exact revA_of_keep (hb _ (hsub _ (hsl.isSlot hP)) (fun h => f1 (cslotAddrA_inj hP hsl hA1 h)) + (fun h => f2 (cslotAddrA_inj hP hsl hA2 h))) + · by_cases f1 : y = cslotAddrA sa + · exact Or.inr (Or.inr (by rw [f1]; exact e1)) + · by_cases f2 : y = cslotAddrA sb + · exact Or.inr (Or.inl (by rw [f2]; exact e2)) + · exact Or.inl (hk' y hy f1 f2) + +end + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {A : Type} {T Ω Sg OS : Set U64} {th : TypedHeapA} {s : S} {σ : Fl4SpecA64.State Unit} {G : MdbGhostA} + +/-- **`cdt::cap_swap_for_delete` of any `OS` slot with a non-null one keeps the deletion's states** (module +docstring; for two non-null slots `cap_swap_for_delete_sA`). -/ +theorem cap_swap_for_delete_hsA (hO : OSOK T Ω Sg OS) (D : DStateA st th OS s σ G) + {sa sb : CslotPtr} (hA1 : AbsSlotA th sa) (hA2 : AbsSlotA th sb) (hne : sa ≠ sb) + (hn2 : capA st s (cslotAddrA sb) ≠ .Null) + (hs1 : obj.sext48 (cslotAddrA sa) = ok (cslotAddrA sa)) + (hs2 : obj.sext48 (cslotAddrA sb) = ok (cslotAddrA sb)) : + cdt.cap_swap_for_delete st s (cslotAddrA sa) (cslotAddrA sb) ⦃ s' => SlotFrameA (st := st) T OS s s' ∧ + (∀ y ∈ OS, capA st s' y = capA st s (swp (cslotAddrA sa) (cslotAddrA sb) y)) ∧ + ∃ σ', ((), σ') ∈ (Fl4SpecA64.capSwapForDelete (Z := Unit) sa sb σ).1 ∧ SpecSlotA σ σ' ∧ + DStateA st th OS s' σ' (swGA G (cslotAddrA sa) (cslotAddrA sb)) ∧ + SwapMdbA st G.chains (cslotAddrA sa) (cslotAddrA sb) s s' ⦄ := by + by_cases hn1 : capA st s (cslotAddrA sa) = .Null + swap + · exact cap_swap_for_delete_sA hO D hA1 hA2 hne hn1 hn2 hs1 hs2 + have hP := D.place + have hx1 : cslotAddrA sa ∈ OS := D.mem_slot hA1 + have hx2 : cslotAddrA sb ∈ OS := D.mem_slot hA2 + have hS1 : IsSlotA th (cslotAddrA sa) := AbsSlotA.isSlot hP hA1 + have hS2 : IsSlotA th (cslotAddrA sb) := AbsSlotA.isSlot hP hA2 + have hAne : cslotAddrA sa ≠ cslotAddrA sb := fun h => hne (cslotAddrA_inj hP hA1 hA2 h) + have hG2 : cslotAddrA sb ∈ G.chains.flatten := (D.lk.members _).2 ⟨hx2, hn2⟩ + have Fl := D.lk.lkF hO.nz + have inOS : ∀ y ∈ G.chains.flatten, y ∈ OS := fun y hy => ((D.lk.members y).1 hy).1 + have hmain : cdt.cap_swap_for_delete st s (cslotAddrA sa) (cslotAddrA sb) ⦃ s' => + (SlotFrameA (st := st) T OS s s' ∧ + ∀ y ∈ OS, capA st s' y = capA st s (swp (cslotAddrA sa) (cslotAddrA sb) y)) ∧ + ∃ σ', ((), σ') ∈ (Fl4SpecA64.capSwapForDelete (Z := Unit) sa sb σ).1 ∧ SpecSlotA σ σ' ∧ + StateRelA th (L.mem s') σ' ∧ CdtRelA st th s' σ' (swGA G (cslotAddrA sa) (cslotAddrA sb)) ∧ + TreeA st OS (swGA G (cslotAddrA sa) (cslotAddrA sb)).chains + (swGA G (cslotAddrA sa) (cslotAddrA sb)).par s' ⦄ := by + unfold cdt.cap_swap_for_delete + simp only [obj.Slot.Insts.CoreCmpPartialEqSlot.eq, bind_ok] + split + · rename_i heq + exact absurd (by simpa using heq) hAne + rw [capA_eq, bind_ok, capA_eq, bind_ok, hn1] + obtain ⟨b1, hg1, hcb1⟩ := D.rel.getAbsA hA1 + obtain ⟨b2, hg2, hcb2⟩ := D.rel.getAbsA hA2 + have hw2 : CapRt.EncWf (capA st s (cslotAddrA sb)) := CapRt.decodeF_encWf _ _ + have hb1' : b1 = .nullCap := by + have := hcb1.2 + change CapEqA (capA st s (cslotAddrA sa)) b1 at this + rw [hn1] at this + change absCapA .Null = b1 at this + exact this.symm + subst hb1' + refine WP.spec_mono (WP.spec_and (WP.spec_and + (cap_swap_null_refinesA s σ G D.rel D.cdt hP hO D.tree D.nl D.sub D.nul hA1 hA2 hne hn1 + hn2 hs1 hs2 hcb2 hw2 rfl) + (cdt.swap.lrWF (osokA hO) (LR.self s) hx1 (inOS _ hG2) hAne hs1 hs2 encNull hw2 + (fun h => absurd (D.nl _ hx1 hn1).1 h) (fun h => absurd (D.nl _ hx1 hn1).2 h) + (fun h => inOS _ (Fl.pvm _ hG2 h)) (fun h => inOS _ (Fl.nxm _ hG2 h)))) + (cdt.swap.lrN hO D.nz D.lk D.nl hx1 hG2 hAne hs1 hs2 hn1 rfl)) ?_ + rintro s' ⟨⟨⟨σ', hm, hst, hcdt, hT, hSS, -⟩, hw, -⟩, -, e1, e2, ek⟩ + refine ⟨⟨hw, fun y hy => ?_⟩, σ', ?_, hSS, hst, hcdt, hT⟩ + · unfold swp + split_ifs with h1 h2 + · subst h1; exact e1 + · subst h2; exact (e2.trans hn1.symm) + · exact ek y hy h1 h2 + · unfold Fl4SpecA64.capSwapForDelete NonDet.when + rw [if_pos hne] + exact mem_bindA.2 ⟨(.nullCap, σ), hg1, mem_bindA.2 ⟨(b2, σ), hg2, hm⟩⟩ + have hMW := cdt_cap_swap_for_delete_mdbWA hO hO.nz D.lk D.nl hx1 hG2 hAne hs1 hs2 + refine WP.spec_mono (WP.spec_and (WP.spec_and (WP.spec_and hmain (cdt.cap_swap_for_delete.zf hO D.zf D.mdbS hx1 hx2 hs1 hs2)) + (cdt.cap_swap_for_delete.mdbN hO D.nz D.lk D.rng D.kind D.nl D.cls hx1 hG2 hs1 hs2)) hMW) ?_ + rintro s' ⟨⟨⟨⟨⟨hw, hc⟩, σ', hm, hSS, hst, hcdt, hT⟩, hzf⟩, -, -, -, -, hnl, -⟩, hmw⟩ + have H : SchedFrA σ σ' := capSwapForDelete_ndr sa sb σ _ hm + have W : WfKeepA σ σ' := capSwapForDelete_wfA sa sb σ _ hm + have hP' : PlaceA th (L.ksb s') := by rw [hw.1]; exact D.place + exact ⟨hw, hc, σ', hm, hSS, D.swapped hx1 hx2 hS1 hS2 hst hP' hcdt hT (D.cov.step H W) hzf hnl hc, hmw⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/CnodeDelWA.lean b/proofs/Fl4ProofsA64/P3/CnodeDelWA.lean new file mode 100644 index 000000000..28000dd2f --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/CnodeDelWA.lean @@ -0,0 +1,97 @@ +/- +P3 (AArch64, A64-P3 CNode wave 2): **the deletion's `swap` / `empty` / `zomb` hypotheses at the relation**. + +**`DelJWA AR Z s0 X`** is the loop invariant at the S3 relation: the kernel-state base and CPU of the entry state `s0`, +`JAtZAX` at the loop's ghost, and an extra invariant `X`. A zombie write, a zombie swap and an emptied slot keep it +as soon as they keep `X` (**`swapHyp_jA`**, **`emptyHyp_jA`**, **`zombieHyp_jA`**), so `DelHypsA` at the relation asks +only for the finaliser, preemption and cleanup hooks (**`delHypsA_jA`**). + +`X` is where a Stage-3 producer's own invariant goes; the leaves of the deletion need nothing in it. + +No named premise: the hooks and `X`'s steps are the Stage-3 hypotheses of `DelHypsA`. +-/ +import Fl4ProofsA64.P3.CnodeDelFootA +import Fl4ProofsA64.P3.CnodeDelHookA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-- **The loop invariant at the relation** (module docstring). -/ +def DelJWA (AR : A → HwRootA → Prop) (Z : XSetsA) (s0 : S) (X : DelWA S A) : DelWA S A := fun s a σ G => + SameCoreA st s0 s ∧ (∃ m, JAtZAX (L := L) AR { Z with G := G } s a σ m) ∧ X s a σ G + +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {T Ω Sg OS : Set U64} {s0 : S} + +theorem swapHyp_jA (F : DelFootA st Z T Ω Sg OS s0) {X : DelWA S A} (hX : SwapHypA st T Z.th OS X) : + SwapHypA st T Z.th OS (DelJWA (st := st) AR Z s0 X) := by + intro s s' a σ σ' G sl p zt n hW h1 D D' hw hSS hm hA hz hne hcap hM + obtain ⟨hk, ⟨m, J⟩, hx⟩ := hW + have hxs : IsSlotA Z.th (cslotAddrA sl) := hA.isSlot D.place + have hps : IsSlotA Z.th p := by + have hzok := D.zw _ hxs p zt n hz + have h := hzok.absSlot.isSlot D.place + rwa [hzok.addr] at h + refine ⟨hk.writes hw, ⟨m, F.slotWrites hk J hw hSS D'.rel D'.cdt fun y hy => ?_⟩, + hX s s' a σ σ' G sl p zt n hx h1 D D' hw hSS hm hA hz hne hcap hM⟩ + show CapObjOkA Z (capA st s' y) + rw [hcap y (D.sub y hy)] + split_ifs + · exact J.capObj hps + · exact J.capObj hxs + · exact J.capObj hy + +theorem emptyHyp_jA (F : DelFootA st Z T Ω Sg OS s0) {X Xx : DelWA S A} + {Rd : S → A → Fl4SpecA64.State Unit → MdbGhostA → U64 → Prop} (hX : EmptyHypA st T Z.th OS X Xx Rd) : + EmptyHypA st T Z.th OS (DelJWA (st := st) AR Z s0 X) (DelJWA (st := st) AR Z s0 Xx) Rd := by + intro s s' a σ σ' G sl hW hRd D D' hw hSS hm hA hn h0 hcap hM + obtain ⟨hk, ⟨m, J⟩, hx⟩ := hW + refine ⟨hk.writes hw, ⟨m, F.slotWrites hk J hw hSS D'.rel D'.cdt fun y hy => ?_⟩, + hX s s' a σ σ' G sl hx hRd D D' hw hSS hm hA hn h0 hcap hM⟩ + show CapObjOkA Z (capA st s' y) + by_cases e : y = cslotAddrA sl + · rw [e, h0]; exact CapObjOkA.null Z + · rw [hcap y (D.sub y hy) e]; exact J.capObj hy + +theorem zombieHyp_jA (F : DelFootA st Z T Ω Sg OS s0) {X : DelWA S A} + {Rz : S → A → Fl4SpecA64.State Unit → MdbGhostA → U64 → Prop} (hX : ZombieHypA st T Z.th OS X Rz) : + ZombieHypA st T Z.th OS (DelJWA (st := st) AR Z s0 X) Rz := by + intro s s' a σ σ' G sl p zt n hW hRz hz hnz D D' hw hSS hset hA hn hc hcap hM + obtain ⟨hk, ⟨m, J⟩, hx⟩ := hW + refine ⟨hk.writes hw, ⟨m, F.slotWrites hk J hw hSS D'.rel D'.cdt fun y hy => ?_⟩, + hX s s' a σ σ' G sl p zt n hx hRz hz hnz D D' hw hSS hset hA hn hc hcap hM⟩ + show CapObjOkA Z (capA st s' y) + by_cases e : y = cslotAddrA sl + · rw [e, hc] + exact ⟨fun _ h => (by cases h), fun _ _ _ _ _ h => (by cases h), fun _ _ _ h => (by cases h), + fun _ _ _ h => (by cases h), trivial⟩ + · rw [hcap y (D.sub y hy) e]; exact J.capObj hy + +/-- **`DelHypsA` at the relation**: the hooks at `DelJWA`, and the extra invariant `X` kept by the loop's own slot +steps. -/ +theorem delHypsA_jA {ar : machine.Arch A} (F : DelFootA st Z T Ω Sg OS s0) {Fn : FinFnA} {X Xx Xp : DelWA S A} + {Rd Rz : S → A → Fl4SpecA64.State Unit → MdbGhostA → U64 → Prop} + (agree : FinAgreeA Fn) + (fin : FinHookA st ar Z.th OS Fn (DelJWA (st := st) AR Z s0 X) (DelJWA (st := st) AR Z s0 Xx) Rd Rz) + (triv : RdTrivHypA st Z.th OS (DelJWA (st := st) AR Z s0 X) Rd) + (pp : PPHypDA st ar Z.th OS (DelJWA (st := st) AR Z s0 X) (DelJWA (st := st) AR Z s0 Xp)) + (post : PostDelHypA st ar Z.th OS (DelJWA (st := st) AR Z s0 X)) + (zomb : ZombieHypA st T Z.th OS X Rz) (swap : SwapHypA st T Z.th OS X) + (empty : EmptyHypA st T Z.th OS X Xx Rd) (wx : ∀ s a σ G, X s a σ G → Xx s a σ G) : + DelHypsA st ar T Z.th OS Fn (DelJWA (st := st) AR Z s0 X) (DelJWA (st := st) AR Z s0 Xx) + (DelJWA (st := st) AR Z s0 Xp) Rd Rz := + { agree := agree, fin := fin, triv := triv, pp := pp, post := post, zomb := zombieHyp_jA F zomb, + swap := swapHyp_jA F swap, empty := emptyHyp_jA F empty, + wx := fun s a σ G h => ⟨h.1, h.2.1, wx s a σ G h.2.2⟩ } + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/CnodeDelZombieA.lean b/proofs/Fl4ProofsA64/P3/CnodeDelZombieA.lean new file mode 100644 index 000000000..f52e956c2 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/CnodeDelZombieA.lean @@ -0,0 +1,246 @@ +/- +P3 (AArch64, A64-P3 CNode wave 2): **the zombie write of the deletion** (x86 `P3/DelOps.set_zombie`): `set_cap` of a +`Zombie` over the slot holding the CNode / thread cap being finalised, or over the same zombie counted down. + +* **`setCap_oneA`**: the spec's `set_cap` of a typed slot has one outcome. +* **`set_zombie_sA`**: FL4's write refines it, keeping `DStateA` (the ghost unchanged). + +The caller gives the shape of the old cap: a range key of base `p` and of the zombie's class, and either the only range +key of base `p` or already a zombie over it (A64 P1's `ZrA`). + +No named premise (`DStateA`'s [J] facts are kept). +-/ +import Fl4ProofsA64.P3.CnodeDelStepA +import Fl4ProofsA64.P1.MdbFin + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## The spec's one outcome -/ + +theorem getObject_oneA {σ : Fl4SpecA64.State Unit} {p : ObjRef} {ko : KernelObject} (hk : σ.kheap p = some ko) : + getObject (Z := Unit) p σ = ({(ko, σ)}, False) := by + simp [getObject, NonDet.getsThe, readObject, hk, NonDet.assertOpt]; rfl + +theorem setObject_oneA {σ : Fl4SpecA64.State Unit} {p : ObjRef} {ko ko' : KernelObject} + (hk : σ.kheap p = some ko) (ht : aType ko = aType ko') : + setObject (Z := Unit) p ko' σ = ({((), setObjA σ p ko')}, False) := by + unfold setObject + show (getObject p >>= _) σ = _ + rw [nd_bind_singleA (getObject_oneA hk)] + have h2 : (NonDet.assert (aType ko = aType ko') : NonDet (State Unit) Unit) σ = ({((), σ)}, False) := by + simp [NonDet.assert, ht] + show (NonDet.assert _ >>= _) σ = _ + rw [nd_bind_singleA h2] + have h3 : (NonDet.get : NonDet (State Unit) (State Unit)) σ = ({(σ, σ)}, False) := rfl + show (NonDet.get >>= _) σ = _ + rw [nd_bind_singleA h3] + rfl + +/-- **`set_cap` of a typed slot has one outcome**, with the CSpace relation after it. -/ +theorem setCap_oneA {th : TypedHeapA} {kb : U64} {mem mem' : U64 → U64} {σ : Fl4SpecA64.State Unit} + (hR : StateRelA th mem σ) (hP : PlaceA th kb) {sl : CslotPtr} (hs : AbsSlotA th sl) {a : Cap} + (hca : CapRelA (capAtA mem' (cslotAddrA sl)) a) + (hcap : ∀ y, IsSlotA th y → y ≠ cslotAddrA sl → capAtA mem' y = capAtA mem y) : + ∃ σ', setCap (Z := Unit) a sl σ = ({((), σ')}, False) ∧ StateRelA th mem' σ' ∧ SpecSlotA σ σ' ∧ + σ'.cdt = σ.cdt ∧ σ'.isOriginalCap = σ.isOriginalCap := by + obtain ⟨o, hm, hR', hS⟩ := hR.setAbsA hP hs hca hcap + have one : ∃ o', setCap (Z := Unit) a sl σ = ({((), setObjA σ sl.1 o')}, False) := by + obtain ⟨q, idx⟩ := sl + obtain ⟨p, e1, hc | ⟨ht, i, hi, e2⟩⟩ := hs + · simp only at e1 hc + subst e1 + obtain ⟨cs, hk, hwf, -⟩ := hR.cnode p _ hc + have hd : idx ∈ dom cs := by rw [hwf]; rfl + have hty : aType (.cnode idx.length cs) = aType (.cnode idx.length (Function.update cs idx (some a))) := by + simp only [aType, hwf, wf_updA a hwf rfl, ↓reduceIte] + refine ⟨.cnode idx.length (Function.update cs idx (some a)), ?_⟩ + unfold setCap + show (getObject _ >>= _) σ = _ + rw [nd_bind_singleA (getObject_oneA hk)] + simp only [hd, hwf, and_self, ↓reduceIte] + rw [nd_pure_bindA] + exact setObject_oneA hk hty + · simp only at e1 e2 + subst e1 e2 + obtain ⟨x, hk, -⟩ := hR.tcb p ht + refine ⟨.tcb (tcbSetA x i a), ?_⟩ + have hset : ∀ x' : Tcb, setObject (Z := Unit) (specPtr p) (.tcb x') σ = + ({((), setObjA σ (specPtr p) (.tcb x'))}, False) := fun x' => setObject_oneA hk rfl + unfold tcbSlotsA at hi + have e : ∀ j, j < 8 → (tcbCnodeIndex i = tcbCnodeIndex j ↔ i = j) := fun j hj => tcbIdx_eq (by omega) hj + unfold setCap + show (getObject _ >>= _) σ = _ + rw [nd_bind_singleA (getObject_oneA hk)] + simp only [tcbStage2Slot, tcbHighVSpaceSlot, e 0 (by decide), e 1 (by decide), e 2 (by decide), + e 3 (by decide), e 4 (by decide), e 5 (by decide), e 6 (by decide)] + unfold tcbSetA + split_ifs <;> first | omega | (rw [nd_pure_bindA]; exact hset _) + obtain ⟨o', h1⟩ := one + have e : setObjA σ sl.1 o = setObjA σ sl.1 o' := by + have := hm; rw [h1] at this + exact (Prod.mk.inj (Set.mem_singleton_iff.1 this)).2 + rw [e] at hR' hS + exact ⟨_, h1, hR', hS, rfl, rfl⟩ + +/-! ## The zombie write -/ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- [J] `IrqRunA` with the ghost kept, when no handler cap appears or goes. -/ +theorem IrqRunA.keepIrq {G : List (List U64)} {s s' : S} (h : IrqRunA st G s) + (hc : ∀ y ∈ G.flatten, ∀ i, capA st s' y = .IrqHandler i ↔ capA st s y = .IrqHandler i) : + IrqRunA st G s' := by + refine ⟨fun y hy y' hy' i k k' => h.one y hy y' hy' i ((hc y hy i).1 k) ((hc y' hy' i).1 k'), + fun ch hch a b c y z y' i ha hb hc' hab hbc ky ky' => ?_⟩ + have mem : ∀ {n : Nat} {w : U64}, ch[n]? = some w → w ∈ G.flatten := fun e => + List.mem_flatten.2 ⟨ch, hch, List.mem_of_getElem? e⟩ + exact (hc z (mem hb) i).2 (h.chunk ch hch a b c y z y' i ha hb hc' hab hbc ((hc y (mem ha) i).1 ky) + ((hc y' (mem hc') i).1 ky')) + +variable {T Ω Sg OS : Set U64} {th : TypedHeapA} {s : S} {σ : Fl4SpecA64.State Unit} {G : MdbGhostA} + +/-- **The zombie write** (module docstring). -/ +theorem set_zombie_sA (hO : OSOK T Ω Sg OS) (D : DStateA st th OS s σ G) + {sl : CslotPtr} (hA : AbsSlotA th sl) {p t n : U64} (hp : Canon p) + (hwf : CapWfA (.Zombie p t n)) (hzok : ZOKA th p t n) + (hk0 : RngKeyA (capA st s (cslotAddrA sl))) (hp0 : ptrA (capA st s (cslotAddrA sl)) = p) + (hcls : clsA (.Zombie p t n) = clsA (capA st s (cslotAddrA sl))) + (hu : (∀ y ∈ OS, y ≠ cslotAddrA sl → RngKeyA (capA st s y) → ptrA (capA st s y) ≠ p) ∨ + ∃ n0, capA st s (cslotAddrA sl) = .Zombie p t n0) : + obj.Slot.set_cap st (cslotAddrA sl) s (.Zombie p t n) ⦃ s' => + SlotFrameA (st := st) T OS s s' ∧ capA st s' (cslotAddrA sl) = .Zombie p t n ∧ + (∀ y ∈ OS, y ≠ cslotAddrA sl → capA st s' y = capA st s y) ∧ + ∃ σ', setCap (Z := Unit) (absCapA (.Zombie p t n)) sl σ = ({((), σ')}, False) ∧ SpecSlotA σ σ' ∧ + DStateA st th OS s' σ' G ∧ ZombMdbA st G.chains (cslotAddrA sl) s s' ⦄ := by + have hP := D.place + set x := cslotAddrA sl with hxdef + have hxO : x ∈ OS := D.mem_slot hA + have hxs : IsSlotA th x := hA.isSlot hP + have ht : t.val < 256 := by + have h : t.val ≤ 64 ∨ t.val = 65 := hzok.kind + omega + have hd : capability.Cap.decode (zw p t) n = ok (.Zombie p t n) := by + have := decode_zw p t n hp + rwa [and255 ht] at this + have hwp := W7.obj.Slot.set_cap.capw (st := st) (osw hO hxO) (s := s) (c := .Zombie p t n) + have hzf := ZombiesFinal.setZ hO D.zf hxO hp (t := t) (n := n) ⟨hk0, hp0, hu⟩ + rw [set_cap_zombie, WP.spec_ok] at hwp hzf ⊢ + set s' := L.wr (L.wr s x (zw p t)) (wa x 8#u64) n with hs' + have cx : capA st s' x = .Zombie p t n := capA_wr2_self hO hxO hd + have co : ∀ y ∈ OS, y ≠ x → capA st s' y = capA st s y := fun y hy e => capA_wr2_other hO hxO hy e + have hl : ∀ y ∈ OS, L.mem s' (wa y 16#u64) = L.mem s (wa y 16#u64) ∧ + L.mem s' (wa y 24#u64) = L.mem s (wa y 24#u64) := set_cap_links hO hxO + have kz : RngKeyA (.Zombie p t n : capability.Cap) := RngKeyA.zombie _ _ _ + have hc0 : capA st s x ≠ .Null := fun e => RngKeyA.not_null (e ▸ hk0) + have hzn : (.Zombie p t n : capability.Cap) ≠ .Null := fun e => by cases e + have hrel : CapRelA (capAtA (L.mem s') x) (absCapA (.Zombie p t n)) := by + show CapRelA (capA st s' x) _ + rw [cx]; exact ⟨hwf, rfl⟩ + have hcapT : ∀ y, IsSlotA th y → y ≠ x → capAtA (L.mem s') y = capAtA (L.mem s) y := + fun y hy e => co y (D.sub y hy) e + obtain ⟨σ', hset, hR', hSS, hcdt, hio⟩ := setCap_oneA D.rel hP hA hrel hcapT + have hmem : ((), σ') ∈ (setCap (Z := Unit) (absCapA (.Zombie p t n)) sl σ).1 := by rw [hset]; rfl + have inOS : ∀ y ∈ G.chains.flatten, y ∈ OS := fun y hy => ((D.lk.members y).1 hy).1 + have kk : RngKeyA (.Zombie p t n : capability.Cap) ↔ RngKeyA (capA st s x) := ⟨fun _ => hk0, fun _ => kz⟩ + have cl : RngKeyA (capA st s x) → clsA (.Zombie p t n) = clsA (capA st s x) := fun _ => hcls + -- the chain facts, the ghost kept + have hlk : MdbLk st OS G.chains s' := D.lk.set_cap hO hxO hd hzn hc0 + have hrng : RngOneA64 st G.chains s' := D.rng.set_cap hO inOS hxO hd kk cl + have hcl' : ClsA st OS s' := D.cls.set_cap hO hxO hd kk cl + have hnl : NullLk st OS s' := D.nl.set_cap hO hxO hd hzn + have hkind : KindA st OS s' := by + rcases hu with hu | ⟨n0, hz0⟩ + · refine ⟨fun y hy y' hy' ky ky' hpe => ?_⟩ + by_cases e1 : y = x + · by_cases e2 : y' = x + · rw [e1, e2] + · rw [e1, cx, co y' hy' e2, ptrA_zombie] at hpe + rw [co y' hy' e2] at ky' + exact absurd hpe.symm (hu y' hy' e2 ky') + · by_cases e2 : y' = x + · rw [e2, cx, co y hy e1, ptrA_zombie] at hpe + rw [co y hy e1] at ky + exact absurd hpe (hu y hy e1 ky) + · rw [co y hy e1] at ky hpe ⊢ + rw [co y' hy' e2] at ky' hpe ⊢ + exact D.kind.kind y hy y' hy' ky ky' hpe + · refine D.kind.set_cap hO hxO hd kk fun _ => ?_ + rw [hz0, ptrA_zombie, ptrA_zombie]; exact ⟨rfl, rfl⟩ + have hua : UA (.Zombie p t n) (capA st s x) := + ⟨kz.1.trans hk0.1.symm, (ptrA_zombie _ _ _).trans hp0.symm, congrArg Prod.snd hcls, + kz.2.1.trans hk0.2.1.symm, fun _ _ _ _ e => by cases e⟩ + have hut : UtInvA st OS G.par s' := by + refine D.tree.ut.recap D.tree.parIn fun y hy => ?_ + by_cases e : y = x + · rw [e, cx]; exact hua + · rw [co y hy e]; exact UA.refl _ + have htree : TreeA st OS G.chains G.par s' := ⟨hlk, D.tree.tree, hut⟩ + have hnul' : ∀ y ∈ OS, ¬ IsSlotA th y → capA st s' y = .Null := fun y hy hn => by + rw [co y hy (fun e => hn (e ▸ hxs))]; exact D.nul y hy hn + have hcdtR : CdtRelA st th s' σ' G := by + have C := D.cdt + refine ⟨cdtTree_of htree D.sub hnul', fun sl' h' => ?_, fun sl' q h' => ?_, fun sl' h' => ?_, + C.par_slot, fun a' b' h' => ?_⟩ + · rw [hcdt]; exact C.cdt sl' h' + · rw [hcdt] at h'; exact C.cdt_slot sl' q h' + · rw [hio, C.orig sl' h'] + unfold revA + rw [(hl _ (D.mem_slot h')).2] + · obtain ⟨par', -, rfl⟩ := C.par_slot a' b' h' + have hb : b' ∈ OS := (D.tree.parIn _ _ h').2 + unfold revA + rw [(hl _ hb).2] + exact C.prev _ _ h' + have H : SchedFrA σ σ' := setCap_ndr _ _ σ _ hmem + have W : WfKeepA σ σ' := setCap_wfA _ _ σ _ hmem + have hD' : DStateA st th OS s' σ' G := by + refine + { rel := hR', place := (by rw [hwp.1]; exact D.place), cov := D.cov.step H W, lk := hlk, rng := hrng, kind := hkind, + cls := hcl', zf := hzf, nz := D.nz, sub := D.sub, nul := hnul', fr := ?_, irq := ?_, cdt := hcdtR, + tree := htree, nl := hnl, zw := ?_, + cv := fun y hy => by + by_cases e : y = x + · rw [e, cx]; trivial + · rw [co y hy e]; exact D.cv y hy } + · refine D.fr.of (fun y hy f => ?_) (fun y hy k => ?_) + · by_cases e : y = x + · rw [e, cx] at f; cases f + · rw [co y hy e] at f ⊢; exact ⟨y, hy, f, rfl⟩ + · by_cases e : y = x + · rw [e, cx, ptrA_zombie]; exact ⟨x, hxO, hk0, hp0⟩ + · rw [co y hy e] at k ⊢; exact ⟨y, hy, k, rfl⟩ + · refine D.irq.keepIrq fun y hy i => ?_ + by_cases e : y = x + · rw [e, cx] + refine ⟨fun h => (by cases h), fun h => ?_⟩ + rw [h] at hk0 + have e1 := physA_eq (.IrqHandler i) + simp only [objects.cap_is_physical] at e1 + have e2 := hk0.1 + rw [← okinj e1] at e2 + exact absurd e2 Bool.false_ne_true + · rw [co y (inOS y hy) e] + · intro y hy p' zt' n' h + by_cases e : y = x + · rw [e, cx] at h; cases h; exact hzok + · rw [co y (D.sub y hy) e] at h; exact D.zw y hy p' zt' n' h + have hmw : ZombMdbA st G.chains x s s' := + { next := fun a ha => by unfold NxA; rw [(hl a (inOS a ha)).2] + rev := fun a ha => by unfold Fl4ProofsA64.P1.W7.R.ST.revA; rw [(hl a (inOS a ha)).2] + fb := fun a ha => by unfold fbA; rw [(hl a (inOS a ha)).2] + cover := fun _ => by rw [cx]; exact hua } + exact ⟨hwp, cx, co, σ', hset, hSS, hD', hmw⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/CnodeEmptyClA.lean b/proofs/Fl4ProofsA64/P3/CnodeEmptyClA.lean new file mode 100644 index 000000000..e5b7a23e6 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/CnodeEmptyClA.lean @@ -0,0 +1,254 @@ +/- +P3 (AArch64, A64-P3 CNode wave 2 end): **`empty_slot` with a cleanup cap** and **FL4's `delete_deferred` refines +`cte_delete slot False`** (x86 `P3/DelEmpty`, `P3/DelDelete`). + +* `empty_slot_splitA`: FL4's `empty_slot slot cl` of a non-null slot is `empty_slot slot Null` then + `post_cap_deletion cl`; `emptySlot_clA`: the same on the spec side. +* **`empty_slot_clA`** (`EmptyPostWA`): `empty_slot` of a typed slot with a cleanup cap (`Null` or `IrqHandler`): + the slot emptied (`empty_slot_sA`, `DelHypsA.empty`), then the cleanup (`DelHypsA.post`, producer the IRQ line); + the measure falls when the slot held a cap. +* **`delete_deferred_refinesGA`** (`DDPostGA`): `finalise_slot_deferred` (`deferred_loopZGA`), then `empty_slot` when + it succeeded; the spec's `rec_del (CTEDeleteCall slot False)`. + +Named premises: none new (`DelHypsA`'s hooks; `DStateA`'s [J] facts). +-/ +import Fl4ProofsA64.P3.CnodeDelDefA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 + +noncomputable section + +/-! ## The spec's `empty_slot` with a cleanup cap -/ + +theorem emptySlot_null_memA {σ : Fl4SpecA64.State Unit} {slot : CslotPtr} (info : Cap) + (hg : getCap (Z := Unit) slot σ = ({(.nullCap, σ)}, False)) : + ((), σ) ∈ (emptySlot (Z := Unit) slot info σ).1 := by + unfold emptySlot + refine mem_bindA.2 ⟨(.nullCap, σ), by rw [hg]; rfl, ?_⟩ + dsimp only + rw [if_pos rfl] + rfl + +/-- **The spec's `empty_slot slot cl`** is `empty_slot slot NullCap` then `post_cap_deletion cl` (x86 `emptySlot_cl`). -/ +theorem emptySlot_clA {σ σ1 σ2 : Fl4SpecA64.State Unit} {slot : CslotPtr} {c cl : Cap} + (hg : getCap (Z := Unit) slot σ = ({(c, σ)}, False)) (hcn : c ≠ .nullCap) + (h1 : ((), σ1) ∈ (emptySlot (Z := Unit) slot .nullCap σ).1) + (h2 : ((), σ2) ∈ (postCapDeletion (Z := Unit) cl σ1).1) : + ((), σ2) ∈ (emptySlot (Z := Unit) slot cl σ).1 := by + unfold emptySlot at h1 ⊢ + obtain ⟨x, hx, h1⟩ := mem_bindA.1 h1 + rw [hg] at hx + obtain rfl : x = (c, σ) := hx + refine mem_bindA.2 ⟨(c, σ), by rw [hg]; rfl, ?_⟩ + dsimp only at h1 ⊢ + rw [if_neg hcn] at h1 ⊢ + iterate 7 + obtain ⟨x, hx, h1⟩ := mem_bindA.1 h1 + refine mem_bindA.2 ⟨x, hx, ?_⟩ + have e : ((), σ1) = ((), x.2) := h1 + have e2 : σ1 = x.2 := congrArg Prod.snd e + rw [← e2] + exact h2 + +/-! ## FL4's `empty_slot` with a cleanup cap -/ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-- `empty_slot`'s work before `post_cap_deletion`, on a slot with a cap (x86 `emptyPre`). -/ +def emptyPreA (st : machine.Store S) (s : S) (slot : obj.Slot) : Result S := do + let prev ← obj.Slot.prev st slot s + let next ← obj.Slot.next st slot s + let fb ← obj.Slot.first_badged st slot s + let b1 ← obj.Slot.is_null prev + let s1 ← if b1 then ok s else obj.Slot.set_next st prev s next + let b2 ← obj.Slot.is_null next + let s2 ← if b2 then ok s1 else do + let s3 ← obj.Slot.set_prev st next s1 prev + let nfb ← obj.Slot.first_badged st next s3 + if nfb then obj.Slot.set_first_badged st next s3 true + else obj.Slot.set_first_badged st next s3 fb + obj.Slot.clear st slot s2 + +theorem empty_slot_nullA (ar : machine.Arch A) {s : S} (a : A) {x : U64} (cl : capability.Cap) + (h : capA st s x = .Null) : cdt.empty_slot st ar s a x cl = ok (s, a) := by + unfold cdt.empty_slot + rw [capA_eq, h] + simp only [bind_ok, cap_is_null_nullA, ↓reduceIte] + +theorem empty_slot_preA (ar : machine.Arch A) (s : S) (a : A) (x : U64) (cl : capability.Cap) + (hcn : capA st s x ≠ .Null) : + cdt.empty_slot st ar s a x cl = (do let s3 ← emptyPreA st s x; objects.post_cap_deletion st ar s3 a cl) := by + unfold cdt.empty_slot emptyPreA + rw [capA_eq] + simp only [bind_ok, cap_is_null_neA hcn, Bool.false_eq_true, ↓reduceIte] + simp only [Std.bind_assoc] + +/-- **FL4's `empty_slot slot cl`** of a non-null slot: `empty_slot slot Null`, then `post_cap_deletion cl`. -/ +theorem empty_slot_splitA (ar : machine.Arch A) (s : S) (a : A) (x : U64) (cl : capability.Cap) + (hcn : capA st s x ≠ .Null) : + cdt.empty_slot st ar s a x cl = + (do let p ← cdt.empty_slot st ar s a x .Null; objects.post_cap_deletion st ar p.1 p.2 cl) := by + rw [empty_slot_preA ar s a x cl hcn, empty_slot_preA ar s a x .Null hcn] + simp only [Std.bind_assoc] + congr 1 + funext y + show _ = (do let p ← ok (y, a); objects.post_cap_deletion st ar p.1 p.2 cl) + rw [bind_tc_ok] + +end + +/-! ## `empty_slot` at the deletion's states -/ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {T Ω Sg OS : Set U64} {th : TypedHeapA} + +variable (st) in +/-- **What emptying the slot gives**, with `W` after (x86 `EmptyPostW`). -/ +def EmptyPostWA (W : DelWA S A) (th : TypedHeapA) (OS : Set U64) (s : S) (σ : Fl4SpecA64.State Unit) + (G : MdbGhostA) (sl : CslotPtr) (cl : capability.Cap) (r : S × A) : Prop := + ∃ σ' G', ((), σ') ∈ (emptySlot (Z := Unit) sl (absCapA cl) σ).1 ∧ DStateA st th OS r.1 σ' G' ∧ + capA st r.1 (cslotAddrA sl) = .Null ∧ + (∀ y ∈ OS, y ≠ cslotAddrA sl → capA st r.1 y = capA st s y) ∧ + psiDA st r.1 G' ≤ psiDA st s G ∧ (nnSA G').card ≤ (nnSA G).card ∧ + (capA st s (cslotAddrA sl) ≠ .Null → (nnSA G').card < (nnSA G).card ∧ psiDA st r.1 G' < psiDA st s G) ∧ + W r.1 r.2 σ' G' + +/-- **`empty_slot slot cl`** of a typed slot with a cleanup cap (x86 `empty_stepWR'`): `Wx` before, `W` when the slot +is already empty, the slot ready (`Rd`) when not. -/ +theorem empty_slot_clA {ar : machine.Arch A} (hO : OSOK T Ω Sg OS) {W Wx : DelWA S A} + {Rd : S → A → Fl4SpecA64.State Unit → MdbGhostA → U64 → Prop} + (hE : EmptyHypA st T th OS W Wx Rd) (hPD : PostDelHypA st ar th OS W) + {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {G : MdbGhostA} (D : DStateA st th OS s σ G) + (hWx : Wx s a σ G) {sl : CslotPtr} (hWn : capA st s (cslotAddrA sl) = .Null → W s a σ G) + (hA : AbsSlotA th sl) (hRd : capA st s (cslotAddrA sl) ≠ .Null → Rd s a σ G (cslotAddrA sl)) + {cl : capability.Cap} (hcl : CleanCapA cl) (hclw : CapWfA cl) + (hclu : ∀ i, cl = .IrqHandler i → ∀ y ∈ OS, y ≠ cslotAddrA sl → capA st s y ≠ .IrqHandler i) : + cdt.empty_slot st ar s a (cslotAddrA sl) cl ⦃ r => EmptyPostWA st W th OS s σ G sl cl r ⦄ := by + obtain ⟨ac, hg, hace⟩ := getCapOne_eqA (st := st) D.rel hA + have hxO := D.mem_slot hA + by_cases hcn : capA st s (cslotAddrA sl) = .Null + · rw [empty_slot_nullA ar a cl hcn, WP.spec_ok] + refine ⟨σ, G, ?_, D, hcn, fun _ _ _ => rfl, le_refl _, le_refl _, fun h => absurd hcn h, hWn hcn⟩ + rw [hcn] at hace + rw [(hace.eq_abs fun _ _ _ e => by cases e).symm] at hg + exact emptySlot_null_memA _ hg + rw [empty_slot_splitA ar s a _ cl hcn] + apply WP.spec_bind (empty_slot_sA (ar := ar) (a := a) hO D hA hcn) + rintro ⟨s1, a1⟩ ⟨ha1, hfr, hx1, hoth, σ1, hes, hSS, D1, hMw⟩ + dsimp only at ha1 hfr hx1 hoth hes hSS D1 ⊢ + subst a1 + have hW1 := hE s s1 a σ σ1 G sl hWx (hRd hcn) D D1 hfr hSS hes hA hcn hx1 hoth hMw + have hnn : ∀ y ∈ OS, capA st s1 y ≠ .Null → capA st s y ≠ .Null := fun y hy h => by + by_cases e : y = cslotAddrA sl + · rw [e]; exact hcn + · rw [← hoth y hy e]; exact h + have hmz : ∀ y ∈ OS, capA st s1 y ≠ .Null → ¬ HomeZA st s1 y → ¬ HomeZA st s y := fun y hy h hh hz => by + apply hh + by_cases e : y = cslotAddrA sl + · rw [e] at h; exact absurd hx1 h + · obtain ⟨zt, n, hz⟩ := hz; exact ⟨zt, n, by rw [hoth y hy e]; exact hz⟩ + have hxm : cslotAddrA sl ∈ nnSA G := (mem_nnSA D.lk _).2 ⟨hxO, hcn⟩ + have hxm1 : cslotAddrA sl ∉ nnSA (remGA G (cslotAddrA sl)) := by + rw [mem_nnSA D1.lk]; rintro ⟨-, h⟩; exact h hx1 + have hps1 := psiDA_le D.lk D1.lk hnn hmz + have hpslt := psiDA_lt_nn D.lk D1.lk hnn hmz hxm hxm1 + have hcd : (nnSA (remGA G (cslotAddrA sl))).card < (nnSA G).card := by + refine Finset.card_lt_card (Finset.ssubset_iff_subset_ne.2 ⟨fun y hy => ?_, fun e => hxm1 (e ▸ hxm)⟩) + rw [mem_nnSA D1.lk] at hy; rw [mem_nnSA D.lk]; exact ⟨hy.1, hnn y hy.1 hy.2⟩ + have hacn : ac ≠ .nullCap := hace.ne_null hcn (D.wf hxO) + by_cases hcln : cl = .Null + · subst hcln + rw [show objects.post_cap_deletion st ar s1 a .Null = ok (s1, a) from rfl, WP.spec_ok] + exact ⟨σ1, _, hes, D1, hx1, hoth, hps1, hcd.le, fun _ => ⟨hcd, hpslt⟩, hW1⟩ + have hclu1 : ∀ i, cl = .IrqHandler i → ∀ y ∈ OS, capA st s1 y ≠ .IrqHandler i := fun i hi y hy => by + by_cases e : y = cslotAddrA sl + · rw [e, hx1]; simp + · rw [hoth y hy e]; exact hclu i hi y hy e + refine WP.spec_mono (hPD s1 a σ1 _ cl D1 hW1 hcl hcln hclw hclu1) ?_ + rintro ⟨s2, a2⟩ ⟨hk2, σ2, hpd, D2, hW2⟩ + dsimp only at hk2 hpd D2 hW2 ⊢ + have hps2 := psiDA_sameA D1.lk D2.lk hk2 + refine ⟨σ2, _, emptySlot_clA hg hacn hes hpd, D2, by rw [hk2 _ hxO]; exact hx1, + fun y hy hyx => by rw [hk2 y hy]; exact hoth y hy hyx, by rw [hps2]; exact hps1, hcd.le, + fun _ => ⟨hcd, by rw [hps2]; exact hpslt⟩, hW2⟩ + +end + +/-! ## `delete_deferred` -/ + +/-- A `let` of a pair (x86 `TcbCtl.let_pair2`). -/ +theorem let_pairDA {α β γ : Type} (x : α) (y : β) (f : α → β → Result γ) (Q : γ → Prop) + (h : f x y ⦃ res => Q res ⦄) : (let (s1, a1) := (x, y); f s1 a1) ⦃ res => Q res ⦄ := h + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +variable (st) in +/-- **What `delete_deferred` ends with** (x86 `DDPostG`). -/ +def DDPostGA (Fn : FinFnA) (W Wp : DelWA S A) (th : TypedHeapA) (OS : Set U64) (s : S) (σ : Fl4SpecA64.State Unit) + (G : MdbGhostA) (sl : CslotPtr) (res : core.result.Result Unit error.Exc × S × A) : Prop := + ∃ σ' G', DStateA st th OS res.2.1 σ' G' ∧ (nnSA G').card ≤ (nnSA G).card ∧ + psiDA st res.2.1 G' ≤ psiDA st s G ∧ + (∀ y ∈ OS, y ≠ cslotAddrA sl → ChgOKA st s res.2.1 y) ∧ + StrictChgA st OS s res.2.1 G G' (cslotAddrA sl) ∧ + ((res.1 = .Ok () ∧ (∃ v, RDGA Fn (.cTEDeleteCall sl false) σ (.ok v, σ')) ∧ + (capA st res.2.1 (cslotAddrA sl) = .Null ∨ HomeZA st res.2.1 (cslotAddrA sl))) ∨ + (res.1 = .Err .Preempted ∧ RDGA Fn (.cTEDeleteCall sl false) σ (.error (), σ'))) ∧ + WPostA W Wp res.1 res.2.1 res.2.2 σ' G' + +/-- **FL4's `delete_deferred` refines `cte_delete slot False`** (module docstring; x86 `delete_deferred_refinesZG`). -/ +theorem delete_deferred_refinesGA {ar : machine.Arch A} {T Ω Sg OS : Set U64} {th : TypedHeapA} + (hO : OSOK T Ω Sg OS) {Fn : FinFnA} {W Wx Wp : DelWA S A} + {Rd Rz : S → A → Fl4SpecA64.State Unit → MdbGhostA → U64 → Prop} + (H : DelHypsA st ar T th OS Fn W Wx Wp Rd Rz) + (sl : CslotPtr) (hA : AbsSlotA th sl) (hsl : obj.sext48 (cslotAddrA sl) = ok (cslotAddrA sl)) + (s : S) (a : A) (σ : Fl4SpecA64.State Unit) (G : MdbGhostA) (D : DStateA st th OS s σ G) (hW : W s a σ G) : + cdt.delete_deferred st ar s a (cslotAddrA sl) ⦃ res => DDPostGA st Fn W Wp th OS s σ G sl res ⦄ := by + unfold cdt.delete_deferred cdt.finalise_slot_deferred + have hWF : layout.WALK_FUEL.val = 2 ^ 64 - 1 := by simp [layout.WALK_FUEL, U64.rMax] + apply WP.spec_bind (deferred_loopZGA hO H sl hA hsl s σ G s a layout.WALK_FUEL + ⟨σ, G, D, by show psiDA st s G < layout.WALK_FUEL.val; rw [hWF]; exact muFuelA D, le_refl _, le_refl _, + fun _ h => h, fun _ _ _ => ChgOKA.refl _ _, StrictChgA.refl _ _ _, hW⟩) + rintro ⟨r, s1, a1⟩ ⟨σ1, G1, D1, hcd1, hps1, hch1, hsc1, hres, hW1⟩ + simp only [uncurry_apply_pair] at D1 hcd1 hps1 hch1 hsc1 hres hW1 ⊢ + rcases hres with ⟨f, rfl, hcl, hclw, hclu, hsf, hRd1, hWn1, hrd⟩ | ⟨rfl, hrd⟩ + · simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok] + cases hs : f.success + · simp only [Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + rw [hs] at hrd + obtain ⟨v, hv⟩ := rd_cte_okGA (σ2 := σ1) hrd (by simp only [Bool.false_eq_true, or_self]; rfl) + exact ⟨σ1, G1, D1, hcd1, hps1, hch1, hsc1, Or.inl ⟨rfl, ⟨v, hv⟩, Or.inr (hsf hs)⟩, hWn1 (Or.inl hs)⟩ + · simp only [↓reduceIte] + rw [hs] at hrd + simp only [↓reduceIte] at hrd + have hWx1 : Wx s1 a1 σ1 G1 := hW1 + apply WP.spec_bind (empty_slot_clA hO H.empty H.post D1 hWx1 (fun h => hWn1 (Or.inr h)) hA (hRd1 hs) hcl hclw hclu) + rintro ⟨s2, a2⟩ ⟨σ2, G2, hes, D2, hx2, hoth2, hps2, hcd2, hlt2, hW2⟩ + dsimp only at hes D2 hx2 hoth2 hps2 hcd2 hlt2 hW2 ⊢ + refine let_pairDA s2 a2 (fun s2 a2 => ok (core.result.Result.Ok (), s2, a2)) _ ?_ + rw [WP.spec_ok] + obtain ⟨v, hv⟩ := rd_cte_okGA (σ2 := σ2) hrd (by simp only [or_true, NonDet.when, ↓reduceIte]; exact hes) + refine ⟨σ2, G2, D2, le_trans hcd2 hcd1, le_trans hps2 hps1, + fun y hy hyx => (hch1 y hy hyx).trans (Or.inl (hoth2 y hy hyx)), ?_, Or.inl ⟨rfl, ⟨v, hv⟩, Or.inl hx2⟩, hW2⟩ + refine hsc1.trans (fun y hy hne _ => ?_) hps1 hps2 + by_cases hyx : y = cslotAddrA sl + · subst hyx + by_cases hn : capA st s1 (cslotAddrA sl) = .Null + · rw [hx2, hn] at hne; exact absurd rfl hne + · exact (hlt2 hn).2 + · exact absurd (hoth2 y hy hyx) hne + · simp only [core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame.from, + bind_ok, bind_tc_ok, WP.spec_ok] + exact ⟨σ1, G1, D1, hcd1, hps1, hch1, hsc1, Or.inr ⟨rfl, rd_cte_errGA hrd⟩, hW1⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/CnodeFinalA.lean b/proofs/Fl4ProofsA64/P3/CnodeFinalA.lean new file mode 100644 index 000000000..176d4431f --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/CnodeFinalA.lean @@ -0,0 +1,554 @@ +/- +P3 (AArch64, A64-P3 CNode wave 1): **finality** — FL4's neighbour test `is_final_capability` (cdt.rs +`is_final_capability_with_cap`) against the spec's `is_final_cap'` (x86 `FinalHyp` / `finalHyp_full`, +`P3/DelRound`). + +* **`NeedsFinCA c`**: the caps whose finalisation reads the finality flag: every object cap but a frame (the spec's + frame finalisation ignores it, `AARCH64/VSpace` `arch_finalise_cap`; FL4 reads it only for the platform's DMA + retire), and a `Zombie`. +* **`FinStA`**: the state facts used, at one state: the CSpace relation and placement, the converse object coverage + (`FinCovA`), A64 P1's MDB facts (`MdbLk`, `RngOneA64`, `KindA`, `ClsA`, `ZombiesFinal`), the slot set, and the two + named premises below. +* **`FinalHypA`**, proved by **`finalHypA`**: at `FinStA`, for a cap that `NeedsFinCA` the neighbour test returns + `decide (isFinalCap' (absCapA c) σ)`, and a `Zombie` is final (l4v `zombies_final`). + +Named premises (Architect ruling of 2026-12-02 18:58Z): + +* **[J] `FrBaseApartA`**: no `OS` frame cap shares its base with a non-frame, non-untyped physical cap + (`RngKeyA`) of an `OS` slot. Producer (Stage 3): boot, S3's `ObjNamedA` / `JointObjectA`, `S1aR2`'s typed-object + disjointness and the capability operations' frames. It subsumes the entry line's `DevFrDisjA` for finality. +* **[J] `IrqRunA`**: the `IrqHandler i` caps of one IRQ lie in one MDB chain, contiguously (`RngOneA64` for IRQ + handlers). A P1-MDB / Stage-3 obligation, tracked together with l4v `valid_irq_handlers` (an inactive IRQ has no + handler cap): the S1A slot discharges it after B2, or the integration if it lands first. It must not survive as a + top-level premise. +* **[J] `FinCovA`**: the converse object coverage of the frozen heap correspondence; its two fields are, word for + word, those of the entry line's `SlotHeapCovA th σ` (`P3/SlotCoverageA`, absent on this branch). +-/ +import Fl4ProofsA64.P3.EpBufA +import Fl4ProofsA64.P3.FinDefsA +import Fl4ProofsA64.P3.LookupA +import Fl4ProofsA64.P3.EmptyA +import Fl4ProofsA64.P3.SpecFrA +import Fl4ProofsA64.P3.ScSpecA +import Fl4ProofsA64.P1.MdbZF +import Fl4ProofsA64.P1.MdbCovZL + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## The caps whose finalisation reads the flag -/ + +/-- **The caps whose finalisation reads the finality flag, and `Zombie`s** (module docstring; x86 `NeedsFin`). -/ +def NeedsFinCA : capability.Cap → Prop + | .CNode .. | .Thread _ | .Endpoint .. | .Notification .. | .Reply .. | .SchedContext .. | .IrqHandler _ + | .PageTable .. | .Pml4 .. | .Vcpu _ | .Ept .. | .HighVSpace .. | .AsidPool .. | .Zombie .. => True + | _ => False + +/-- A `Zombie` cap. -/ +def IsZombieA (c : capability.Cap) : Prop := ∃ p t n, c = .Zombie p t n + +/-! ## Range keys by constructor -/ + +/-- The constructors of `RngKeyA` caps. -/ +def keyCtorA : capability.Cap → Bool + | .Endpoint .. | .Notification .. | .Reply .. | .CNode .. | .Thread _ | .SchedContext .. | .Zombie .. + | .PageTable .. | .PageDirectory .. | .Pdpt .. | .Pml4 .. | .Vcpu _ | .Ept .. | .AsidPool .. + | .HighVSpace .. => true + | _ => false + +theorem rngKeyA_iffA (c : capability.Cap) : RngKeyA c ↔ keyCtorA c = true := by + have h := physA_eq c + unfold RngKeyA + cases c <;> simp only [objects.cap_is_physical] at h <;> rw [← okinj h] <;> simp [keyCtorA, isUtA, isFrA] + +/-! ## The generic object references of an abstract cap -/ + +theorem genRefs_keyA {d : capability.Cap} (hk : RngKeyA d) (hw : CapWfA d) : + genObjRefs (absCapA d) = {GenObjRef.objRef (specPtr (ptrA d))} := by + have b := ptrA_eq d + have k := (rngKeyA_iffA d).1 hk + cases d <;> simp only [keyCtorA, Bool.false_eq_true] at k <;> simp only [CapWfA] at hw <;> + simp only [capability.Cap.ptr] at b <;> rw [← okinj b] <;> + simp [genObjRefs, objRefs, capIrqs, capIrqOpt, archCapSetMap, absCapA, aobjRef, ArchCap.acapObj, + ExtArchCap.objRef, archGenObjRefs] + +theorem genRefs_frameA {d : capability.Cap} (hf : isFrA d = true) : + genObjRefs (absCapA d) = {GenObjRef.objRef (specPtr (ptrA d))} := by + have b := ptrA_eq d + cases d <;> simp only [isFrA, Bool.false_eq_true] at hf + rename_i p sz r dev m ept asid va + simp only [capability.Cap.ptr] at b + rw [← okinj b] + cases ept <;> + simp [genObjRefs, objRefs, capIrqs, capIrqOpt, archCapSetMap, absCapA, absFrameE, aobjRef, ArchCap.acapObj, + ExtArchCap.objRef, archGenObjRefs] + +theorem genRefs_irqA (i : U64) : + genObjRefs (absCapA (.IrqHandler i)) = {GenObjRef.iRQRef (BitVec.ofNat 13 i.val)} := by + have e : capIrqs (absCapA (.IrqHandler i)) = {BitVec.ofNat 13 i.val} := by + ext x + show some (BitVec.ofNat 13 i.val) = some x ↔ x = BitVec.ofNat 13 i.val + exact ⟨fun h => (Option.some.inj h).symm, fun h => h ▸ rfl⟩ + have e1 : objRefs (absCapA (.IrqHandler i)) = ∅ := rfl + have e2 : archGenRefs (absCapA (.IrqHandler i)) = ∅ := rfl + unfold genObjRefs + rw [e, e1, e2] + simp only [Set.image_empty, Set.empty_union, Set.union_empty] + exact Set.image_singleton + +theorem genRefs_noneA {d : capability.Cap} (hk : ¬ RngKeyA d) (hf : isFrA d = false) (hi : ∀ i, d ≠ .IrqHandler i) : + genObjRefs (absCapA d) = ∅ := by + rw [rngKeyA_iffA] at hk + cases d <;> simp only [keyCtorA, isFrA, not_true_eq_false, Bool.true_eq_false] at hk hf <;> + first + | exact absurd rfl (hi _) + | simp [genObjRefs, objRefs, capIrqs, capIrqOpt, archCapSetMap, absCapA, aobjRef, archGenObjRefs] + +private theorem inter_singletonA {α : Type} (a : α) (X : Set α) : ({a} : Set α) ∩ X ≠ ∅ ↔ a ∈ X := by + rw [← Set.nonempty_iff_ne_empty, Set.singleton_inter_nonempty] + +/-- The overlap with a cap naming exactly the object `a`. -/ +theorem overlap_objA {a : ObjRef} {d : capability.Cap} (hw : CapWfA d) : + ({GenObjRef.objRef a} : Set GenObjRef) ∩ genObjRefs (absCapA d) ≠ ∅ ↔ + (RngKeyA d ∨ isFrA d = true) ∧ specPtr (ptrA d) = a := by + rw [inter_singletonA] + by_cases hk : RngKeyA d + · rw [genRefs_keyA hk hw, Set.mem_singleton_iff, GenObjRef.objRef.injEq] + exact ⟨fun e => ⟨Or.inl hk, e.symm⟩, fun e => e.2.symm⟩ + by_cases hf : isFrA d = true + · rw [genRefs_frameA hf, Set.mem_singleton_iff, GenObjRef.objRef.injEq] + exact ⟨fun e => ⟨Or.inr hf, e.symm⟩, fun e => e.2.symm⟩ + by_cases hi : ∃ i, d = .IrqHandler i + · obtain ⟨i, rfl⟩ := hi + rw [genRefs_irqA]; simp [hk, hf] + rw [genRefs_noneA hk (by simpa using hf) (fun i e => hi ⟨i, e⟩)] + simp [hk, hf] + +/-- The overlap with an IRQ handler cap. -/ +theorem overlap_irqA {i : U64} (hi : CapWfA (.IrqHandler i)) {d : capability.Cap} (hw : CapWfA d) : + ({GenObjRef.iRQRef (BitVec.ofNat 13 i.val)} : Set GenObjRef) ∩ genObjRefs (absCapA d) ≠ ∅ ↔ + d = .IrqHandler i := by + rw [inter_singletonA] + by_cases hk : RngKeyA d + · rw [genRefs_keyA hk hw] + refine ⟨fun h => by simp at h, fun h => ?_⟩ + subst h; rw [rngKeyA_iffA] at hk; cases hk + by_cases hf : isFrA d = true + · rw [genRefs_frameA hf] + refine ⟨fun h => by simp at h, fun h => ?_⟩ + subst h; cases hf + by_cases hd : ∃ j, d = .IrqHandler j + · obtain ⟨j, rfl⟩ := hd + rw [genRefs_irqA] + simp only [Set.mem_singleton_iff, capability.Cap.IrqHandler.injEq] + have h1 : i.val < 8192 := by + have : i.val < Fl4SpecA64.fl4IrqCount := hi + unfold Fl4SpecA64.fl4IrqCount at this; omega + have h2 : j.val < 8192 := by + have : j.val < Fl4SpecA64.fl4IrqCount := hw + unfold Fl4SpecA64.fl4IrqCount at this; omega + constructor + · intro e + have := congrArg BitVec.toNat (GenObjRef.iRQRef.inj e) + change i.val % 8192 = j.val % 8192 at this + rw [Nat.mod_eq_of_lt h1, Nat.mod_eq_of_lt h2] at this + exact (UScalar.eq_of_val_eq this).symm + · rintro rfl; rfl + rw [genRefs_noneA hk (by simpa using hf) (fun j e => hd ⟨j, e⟩)] + exact ⟨fun h => h.elim, fun e => absurd ⟨i, e⟩ hd⟩ + +/-! ## FL4's `same_object_as` -/ + +/-- Two caps of one range key kind and class are `same_object_as` each other, `Zombie`s apart. -/ +theorem so_keyA {c d : capability.Cap} (hk : RngKeyA c) (hz : ¬ IsZombieA c) (hc : CapWfA c) (hd : CapWfA d) + (kd : kindA d = kindA c) (cd : clsA d = clsA c) : + objects.same_object_as c d = ok true ∧ objects.same_object_as d c = ok true := by + have k := (rngKeyA_iffA c).1 hk + have e1 := ptrA_of_cls cd + have e2 := congrArg Prod.snd cd + simp only [clsA] at e2 + have p1 := ptrA_eq c + have p2 := ptrA_eq d + cases c <;> simp only [keyCtorA, Bool.false_eq_true] at k + case CNode p r g b => exact so_cnode p r g b d kd cd + case Thread p => exact so_thread p d kd cd + case Zombie p t n => exact absurd ⟨p, t, n, rfl⟩ hz + case SchedContext p sb => + cases d <;> simp only [kindA] at kd <;> try omega + rename_i p' sb' + have s1 := sizeA_eq (.SchedContext p sb) + have s2 := sizeA_eq (.SchedContext p' sb') + simp only [objects.cap_size_bits] at s1 s2 + simp only [capability.Cap.ptr] at p1 p2 + rw [← okinj p1, ← okinj p2] at e1 + rw [← okinj s1, ← okinj s2] at e2 + subst e1 e2 + simp [objects.same_object_as, objects.same_region_as] + case Ept p l m a v => + cases d <;> simp only [kindA] at kd <;> try omega + rename_i p' l' m' a' v' + simp only [capability.Cap.ptr] at p1 p2 + rw [← okinj p1, ← okinj p2] at e1 + have : l' = l := UScalar.eq_of_val_eq (by rw [hc.1, hd.1]) + subst e1 this + simp [objects.same_object_as, objects.same_region_as] + all_goals + cases d <;> simp only [kindA] at kd <;> try omega + all_goals + simp only [capability.Cap.ptr] at p1 p2 + rw [← okinj p1, ← okinj p2] at e1 + subst e1 + simp [objects.same_object_as, objects.same_region_as] + +/-- Only a range key cap of the same base is `same_object_as` a range key cap, either way. -/ +theorem so_key_falseA {c d : capability.Cap} (hk : RngKeyA c) (h : ¬ (RngKeyA d ∧ ptrA d = ptrA c)) : + objects.same_object_as d c = ok false ∧ objects.same_object_as c d = ok false := by + have k := (rngKeyA_iffA c).1 hk + have p1 := ptrA_eq c + have p2 := ptrA_eq d + rw [rngKeyA_iffA] at h + cases c <;> simp only [keyCtorA, Bool.false_eq_true] at k <;> simp only [capability.Cap.ptr] at p1 <;> + rw [← okinj p1] at h <;> cases d <;> simp only [capability.Cap.ptr] at p2 <;> (try rw [← okinj p2] at h) <;> + simp only [keyCtorA, true_and, Bool.false_eq_true, false_and, not_false_eq_true] at h <;> + first + | (have h' := Ne.symm h; simp [objects.same_object_as, objects.same_region_as, objects.cap_is_physical, h, h']) + | simp [objects.same_object_as, objects.same_region_as, objects.cap_is_physical] + +/-- Only the IRQ's own handler cap is `same_object_as` an IRQ handler cap, either way. -/ +theorem so_irq_falseA {i : U64} {d : capability.Cap} (h : d ≠ .IrqHandler i) : + objects.same_object_as d (.IrqHandler i) = ok false ∧ objects.same_object_as (.IrqHandler i) d = ok false := by + cases d <;> simp [objects.same_object_as, objects.same_region_as, objects.cap_is_physical] + rename_i j + have : j ≠ i := fun e => h (by rw [e]) + have hv : j.val ≠ i.val := fun e => this (UScalar.eq_of_val_eq e) + exact ⟨by omega, by omega⟩ + +theorem so_irqA (i : U64) : objects.same_object_as (.IrqHandler i) (.IrqHandler i) = ok true := by + simp [objects.same_object_as, objects.same_region_as] + +/-! ## The named premises -/ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **[J]** (module docstring) No `OS` frame cap shares its base with a range key cap of an `OS` slot. -/ +def FrBaseApartA (st : machine.Store S) [L : LawfulStore st] (OS : Set U64) (s : S) : Prop := + ∀ y ∈ OS, ∀ z ∈ OS, isFrA (capA st s y) = true → RngKeyA (capA st s z) → ptrA (capA st s z) ≠ ptrA (capA st s y) + +/-- **[J]** (module docstring) The handler caps of one IRQ lie in one MDB chain, contiguously. -/ +structure IrqRunA (st : machine.Store S) [L : LawfulStore st] (G : List (List U64)) (s : S) : Prop where + one : ∀ y ∈ G.flatten, ∀ y' ∈ G.flatten, ∀ i, capA st s y = .IrqHandler i → capA st s y' = .IrqHandler i → + ∃ ch ∈ G, y ∈ ch ∧ y' ∈ ch + chunk : ∀ ch ∈ G, ∀ (a b c : Nat) (y z y' : U64) (i : U64), ch[a]? = some y → ch[b]? = some z → ch[c]? = some y' → + a < b → b < c → capA st s y = .IrqHandler i → capA st s y' = .IrqHandler i → capA st s z = .IrqHandler i + +/-- **[J]** The converse object coverage (module docstring: the fields of the entry line's `SlotHeapCovA`). -/ +structure FinCovA (th : TypedHeapA) (σ : Fl4SpecA64.State Unit) : Prop where + cnode : ∀ q r cs, σ.kheap q = some (.cnode r cs) → wellFormedCnodeN r cs → th (fl4Ptr q) = some (.cnode r) + tcb : ∀ q x, σ.kheap q = some (.tcb x) → th (fl4Ptr q) = some .tcb + +/-- **The state facts of finality** (module docstring). -/ +structure FinStA (st : machine.Store S) [L : LawfulStore st] (th : TypedHeapA) (OS : Set U64) (G : List (List U64)) + (s : S) (σ : Fl4SpecA64.State Unit) : Prop where + rel : StateRelA th (L.mem s) σ + place : PlaceA th (L.ksb s) + cov : FinCovA th σ + lk : MdbLk st OS G s + rng : RngOneA64 st G s + kind : KindA st OS s + cls : ClsA st OS s + zf : ZombiesFinal st OS s + nz : ∀ y ∈ OS, y ≠ 0#u64 + sub : ∀ y, IsSlotA th y → y ∈ OS + nul : ∀ y ∈ OS, ¬ IsSlotA th y → capA st s y = .Null + /-- [J] -/ + fr : FrBaseApartA st OS s + /-- [J] -/ + irq : IrqRunA st G s + +/-! ## The spec's slots are the typed slots -/ + +/-- A typed slot's address is the address of a spec slot. -/ +theorem exists_absSlotA {th : TypedHeapA} {kb y : U64} (hp : PlaceA th kb) (hy : IsSlotA th y) : + ∃ sl, AbsSlotA th sl ∧ cslotAddrA sl = y := by + obtain ⟨p, ⟨r, hr, i, hi, rfl⟩ | ⟨ht, i, hi, rfl⟩⟩ := hy + · refine ⟨(specPtr p, bitsOfA i r), ⟨p, rfl, Or.inl (by simpa using hr)⟩, ?_⟩ + change slotAddrA (specPtr p) (ofBl (bitsOfA i r)) = _ + rw [ofBl_bitsOfA i r (by have := cnode_r hp hr; omega), Nat.mod_eq_of_lt hi] + · refine ⟨(specPtr p, tcbCnodeIndex i), ⟨p, rfl, Or.inr ⟨ht, i, hi, rfl⟩⟩, ?_⟩ + change slotAddrA (specPtr p) (ofBl (tcbCnodeIndex i)) = _ + rw [ofBl_tcbIdx i (by unfold tcbSlotsA at hi; omega)] + +/-- `get_cap` of a typed spec slot, its one outcome. -/ +theorem StateRelA.getCap_oneA {th : TypedHeapA} {mem : U64 → U64} {σ : Fl4SpecA64.State Unit} + (H : StateRelA th mem σ) {sl : CslotPtr} (hs : AbsSlotA th sl) : + ∃ c, (getCap (Z := Unit) sl σ).1 = {(c, σ)} ∧ CapRelA (capAtA mem (cslotAddrA sl)) c := by + obtain ⟨o, idx⟩ := sl + obtain ⟨p, e1, hc | ⟨ht, i, hi, e2⟩⟩ := hs + · simp only at e1 hc + subst e1 + obtain ⟨cs, hk, hwf, hr⟩ := H.cnode p _ hc + obtain ⟨c, ha, hrel⟩ := hr idx rfl + exact ⟨c, congrArg Prod.fst (getCap_cnodeA hk hwf ha), hrel⟩ + · simp only at e1 e2 + subst e1 e2 + obtain ⟨x, hk, hr⟩ := H.tcb p ht + obtain ⟨c, ha, hrel⟩ := hr i hi + refine ⟨c, congrArg Prod.fst (Endp.getCap_tcb_eqA hk ha), ?_⟩ + unfold cslotAddrA + rw [ofBl_tcbIdx i (by unfold tcbSlotsA at hi; omega)] + exact hrel + +private theorem tcbCnodeMap_slotA {x : Tcb} {idx : CnodeIndex} {c : Cap} (h : tcbCnodeMap x idx = some c) : + ∃ i < tcbSlotsA, idx = tcbCnodeIndex i := by + simp only [tcbCnodeMap, Function.update_apply] at h + split_ifs at h <;> first + | contradiction + | exact ⟨tcbHighVSpaceSlot, by decide, by assumption⟩ + | exact ⟨tcbStage2Slot, by decide, by assumption⟩ + | exact ⟨4, by decide, by assumption⟩ + | exact ⟨3, by decide, by assumption⟩ + | exact ⟨2, by decide, by assumption⟩ + | exact ⟨1, by decide, by assumption⟩ + | exact ⟨0, by decide, by assumption⟩ + +/-- A successful spec read is at a typed slot (the converse coverage; entry line `SlotHeapCovA.getCap`). -/ +theorem FinCovA.absSlot {th : TypedHeapA} {σ : Fl4SpecA64.State Unit} (cov : FinCovA th σ) {q : CslotPtr} {c : Cap} + (hget : (c, σ) ∈ (Fl4SpecA64.getCap (Z := Unit) q σ).1) : AbsSlotA th q := by + obtain ⟨-, o, ho, hc⟩ := getCap_memA hget + obtain ⟨p, idx⟩ := q + have hp : p = specPtr (fl4Ptr p) := (specPtr_fl4Ptr p).symm + cases o with + | cnode radix cs => + have hwf : wellFormedCnodeN radix cs := by + by_contra hn + simp only [Fl4SpecA64.getCap] at hget + rw [Sched.nd_bind_oneSc (Sched.getObject_eqSc ho)] at hget + simp [Sched.nd_bind_applySc, NonDet.assert, hn] at hget + have hl : idx.length = radix := by + have hm : idx ∈ dom cs := by + change cs idx ≠ none + rw [show cs idx = some c from hc] + exact Option.some_ne_none _ + rwa [hwf] at hm + exact ⟨fl4Ptr p, hp, Or.inl (hl.symm ▸ cov.cnode p radix cs ho hwf)⟩ + | tcb x => + obtain ⟨i, hi, hij⟩ := tcbCnodeMap_slotA (show tcbCnodeMap x idx = some c from hc) + exact ⟨fl4Ptr p, hp, Or.inr ⟨cov.tcb p x ho, i, hi, hij⟩⟩ + | _ => cases hc + +theorem FinStA.wf {th : TypedHeapA} {OS : Set U64} {G : List (List U64)} {s : S} {σ : Fl4SpecA64.State Unit} + (F : FinStA st th OS G s σ) {y : U64} (hy : y ∈ OS) : CapWfA (capA st s y) := by + by_cases h : IsSlotA th y + · obtain ⟨sl, hs, rfl⟩ := exists_absSlotA F.place h + obtain ⟨c, -, hr⟩ := F.rel.getCap_oneA hs + exact hr.1 + · rw [F.nul y hy h]; trivial + +/-- **`is_final_cap'` of a slot's cap**, read on FL4's slots: `Q` the FL4 caps whose abstraction overlaps it. -/ +theorem isFinalCap_iffA {th : TypedHeapA} {OS : Set U64} {G : List (List U64)} {s : S} {σ : Fl4SpecA64.State Unit} + (F : FinStA st th OS G s σ) {sl : CslotPtr} (hsl : AbsSlotA th sl) {c : capability.Cap} + (hc : capA st s (cslotAddrA sl) = c) (Q : capability.Cap → Prop) + (hov : ∀ y ∈ OS, (genObjRefs (absCapA c) ∩ genObjRefs (absCapA (capA st s y)) ≠ ∅ ↔ Q (capA st s y))) + (hself : Q c) (hnull : ¬ Q .Null) : + isFinalCap' (absCapA c) σ ↔ ∀ w ∈ OS, w ≠ cslotAddrA sl → ¬ Q (capA st s w) := by + have hx : cslotAddrA sl ∈ OS := F.sub _ (hsl.isSlot F.place) + let overlaps : Set CslotPtr := {q | ∃ c', (getCap (Z := Unit) q σ).1 = {(c', σ)} ∧ + genObjRefs (absCapA c) ∩ genObjRefs c' ≠ ∅} + have mem : ∀ q, AbsSlotA th q → (q ∈ overlaps ↔ Q (capA st s (cslotAddrA q))) := by + intro q hq + obtain ⟨c', hg, hr⟩ := F.rel.getCap_oneA hq + have hy : cslotAddrA q ∈ OS := F.sub _ (hq.isSlot F.place) + have e : genObjRefs c' = genObjRefs (absCapA (capA st s (cslotAddrA q))) := CapEqA.genObjRefs_eq hr.2 + constructor + · rintro ⟨c'', hg', h⟩ + have : c'' = c' := by + have := hg'.symm.trans hg + exact congrArg Prod.fst (Set.singleton_eq_singleton_iff.1 this) + rw [this, e] at h + exact (hov _ hy).1 h + · intro h + exact ⟨c', hg, by rw [e]; exact (hov _ hy).2 h⟩ + have hsm : sl ∈ overlaps := (mem sl hsl).2 (hc.symm ▸ hself) + have abs : ∀ q ∈ overlaps, AbsSlotA th q := by + rintro q ⟨c', hg, -⟩ + exact F.cov.absSlot (c := c') (hg ▸ Set.mem_singleton _) + change (∃ cref, overlaps = {cref}) ↔ _ + constructor + · rintro ⟨q0, hq0⟩ w hw hne hQ + have hwslot : IsSlotA th w := by + by_contra h + rw [F.nul w hw h] at hQ + exact hnull hQ + obtain ⟨q, hq, ha⟩ := exists_absSlotA F.place hwslot + have hqm : q ∈ overlaps := (mem q hq).2 (ha.symm ▸ hQ) + rw [hq0] at hqm hsm + have eq : q = sl := (Set.mem_singleton_iff.1 hqm).trans (Set.mem_singleton_iff.1 hsm).symm + exact hne (ha.symm.trans (congrArg cslotAddrA eq)) + · intro alone + refine ⟨sl, Set.ext fun q => ⟨fun hq => ?_, fun e => (Set.mem_singleton_iff.1 e).symm ▸ hsm⟩⟩ + have ha := abs q hq + by_cases he : cslotAddrA q = cslotAddrA sl + · exact Set.mem_singleton_iff.2 (cslotAddrA_inj F.place ha hsl he) + · exact (alone _ (F.sub _ (ha.isSlot F.place)) he ((mem q ha).1 hq)).elim + +/-! ## FL4's neighbour test -/ + +/-- **The neighbour test**, over the caps `Q` that share the object: no other cap passes `same_object_as` unless it is +in `Q`, and if some other slot is in `Q` then a neighbour passes it. -/ +theorem is_final_iffA {OS : Set U64} {G : List (List U64)} {s : S} (hM : MdbLk st OS G s) + (h0 : ∀ w ∈ OS, w ≠ 0#u64) {x : U64} {c : capability.Cap} (hx : x ∈ OS) (hcx : capA st s x = c) + (hcn : c ≠ .Null) (Q : capability.Cap → Prop) + (hfalse : ∀ d, ¬ Q d → objects.same_object_as d c = ok false ∧ objects.same_object_as c d = ok false) + (hnb : ∀ y ∈ OS, y ≠ x → Q (capA st s y) → ∃ b ∈ OS, + (NxA st s x = b ∧ objects.same_object_as c (capA st s b) = ok true) ∨ + (L.mem s (wa x 16#u64) = b ∧ objects.same_object_as (capA st s b) c = ok true)) + [Decidable (∀ w ∈ OS, w ≠ x → ¬ Q (capA st s w))] : + cdt.is_final_capability_with_cap st s x c = ok (decide (∀ w ∈ OS, w ≠ x → ¬ Q (capA st s w))) := by + by_cases hu : ∀ w ∈ OS, w ≠ x → ¬ Q (capA st s w) + · rw [decide_eq_true hu] + have hxm : x ∈ G.flatten := (hM.members x).2 ⟨hx, by rw [hcx]; exact hcn⟩ + have F := hM.lkF h0 + have inOS : ∀ y ∈ G.flatten, y ∈ OS := fun y hy => ((hM.members y).1 hy).1 + refine is_final.true_of ?_ ?_ + · by_cases hp : L.mem s (wa x 16#u64) = 0#u64 + · exact Or.inl hp + · exact Or.inr (hcx ▸ (hfalse _ (hu _ (inOS _ (F.pvm x hxm hp)) (F.pvne x hxm))).1) + · by_cases hn : NxA st s x = 0#u64 + · exact Or.inl hn + · exact Or.inr (hcx ▸ (hfalse _ (hu _ (inOS _ (F.nxm x hxm hn)) (F.nxne x hxm))).2) + · rw [decide_eq_false hu] + push_neg at hu + obtain ⟨y, hy, hne, hQ⟩ := hu + have hwc : cdt.is_final_capability_with_cap st s x c = cdt.is_final_capability st s x := by + rw [is_final_wc, hcx] + rw [hwc] + obtain ⟨b, hb, ⟨hn, hso⟩ | ⟨hp, hso⟩⟩ := hnb y hy hne hQ + · exact is_final.false_next hcx hn (h0 b hb) rfl hso + · exact is_final.false_prev hcx hp (h0 b hb) rfl hso + +/-- **A chain neighbour of an IRQ's handler cap** (`MdbLk.cls_nb` along `IrqRunA`). -/ +theorem MdbLk.irq_nbA {OS : Set U64} {G : List (List U64)} {s : S} (hM : MdbLk st OS G s) (hI : IrqRunA st G s) + {x y i : U64} (hx : x ∈ OS) (hy : y ∈ OS) (hne : y ≠ x) (hcx : capA st s x = .IrqHandler i) + (hcy : capA st s y = .IrqHandler i) : + ∃ b ∈ OS, capA st s b = .IrqHandler i ∧ (NxA st s x = b ∨ L.mem s (wa x 16#u64) = b) := by + have hxG : x ∈ G.flatten := (hM.members x).2 ⟨hx, by rw [hcx]; simp⟩ + have hyG : y ∈ G.flatten := (hM.members y).2 ⟨hy, by rw [hcy]; simp⟩ + have inOS : ∀ ch ∈ G, ∀ (n : Nat) (w : U64), ch[n]? = some w → w ∈ OS := fun ch hch n w e => + ((hM.members w).1 (List.mem_flatten.2 ⟨ch, hch, List.mem_of_getElem? e⟩)).1 + obtain ⟨ch, hch, hxc, hyc⟩ := hI.one x hxG y hyG i hcx hcy + obtain ⟨n, hn, hxn⟩ := List.getElem_of_mem hxc + obtain ⟨k, hk, hyk⟩ := List.getElem_of_mem hyc + have en : ch[n]? = some x := by rw [getElem?_of_lt hn, hxn] + have ek : ch[k]? = some y := by rw [getElem?_of_lt hk, hyk] + have hnk : n ≠ k := fun e => hne (by subst e; exact hyk.symm.trans hxn) + rcases Nat.lt_or_gt_of_ne hnk with hlt | hlt + · have h1 : n + 1 < ch.length := by omega + have e1 := getElem?_of_lt h1 + have hb := inOS ch hch _ _ e1 + have hcb : capA st s ch[n + 1] = .IrqHandler i := by + by_cases e : n + 1 = k + · have : ch[n + 1] = y := by rw [← hyk]; congr + rw [this, hcy] + · exact hI.chunk ch hch n (n + 1) k x _ y i en e1 ek (by omega) (by omega) hcx hcy + exact ⟨_, hb, hcb, Or.inl (hM.chain ch hch n x _ en e1).1⟩ + · obtain ⟨j, rfl⟩ : ∃ j, n = j + 1 := ⟨n - 1, by omega⟩ + have h1 : j < ch.length := by omega + have e1 := getElem?_of_lt h1 + have hb := inOS ch hch _ _ e1 + have hcb : capA st s ch[j] = .IrqHandler i := by + by_cases e : j = k + · have : ch[j] = y := by rw [← hyk]; congr + rw [this, hcy] + · exact hI.chunk ch hch k j (j + 1) y _ x i ek e1 en (by omega) (by omega) hcy hcx + exact ⟨_, hb, hcb, Or.inr (hM.chain ch hch j _ x e1 en).2⟩ + +/-! ## Finality -/ + +/-- **FL4's finality test decides the spec's** (module docstring; x86 `FinalHyp`). -/ +def FinalHypA (st : machine.Store S) [L : LawfulStore st] (th : TypedHeapA) : Prop := + ∀ (OS : Set U64) (G : List (List U64)) (s : S) (σ : Fl4SpecA64.State Unit) (sl : CslotPtr) (c : capability.Cap), + FinStA st th OS G s σ → AbsSlotA th sl → capA st s (cslotAddrA sl) = c → NeedsFinCA c → + (∃ b, cdt.is_final_capability_with_cap st s (cslotAddrA sl) c = ok b ∧ (b = true ↔ isFinalCap' (absCapA c) σ)) ∧ + (IsZombieA c → isFinalCap' (absCapA c) σ) + +theorem finalHypA (th : TypedHeapA) : FinalHypA st th := by + classical + intro OS G s σ sl c F hsl hc hN + have hx : cslotAddrA sl ∈ OS := F.sub _ (hsl.isSlot F.place) + have hwf : CapWfA c := hc ▸ F.wf hx + have hcn : c ≠ .Null := fun e => by rw [e] at hN; exact hN + by_cases hI : ∃ i, c = .IrqHandler i + · obtain ⟨i, rfl⟩ := hI + have habs : isFinalCap' (absCapA (.IrqHandler i)) σ ↔ + ∀ w ∈ OS, w ≠ cslotAddrA sl → capA st s w ≠ .IrqHandler i := by + refine isFinalCap_iffA F hsl hc (fun d => d = .IrqHandler i) (fun y hy => ?_) rfl (by simp) + rw [genRefs_irqA] + exact overlap_irqA hwf (F.wf hy) + have hcon := is_final_iffA F.lk F.nz hx hc hcn (fun d => d = .IrqHandler i) + (fun d hd => so_irq_falseA hd) (fun y hy hne hQ => by + obtain ⟨b, hb, hcb, hl | hl⟩ := MdbLk.irq_nbA F.lk F.irq hx hy hne hc hQ + · exact ⟨b, hb, Or.inl ⟨hl, hcb ▸ so_irqA i⟩⟩ + · exact ⟨b, hb, Or.inr ⟨hl, hcb ▸ so_irqA i⟩⟩) + refine ⟨⟨_, hcon, ?_⟩, fun ⟨_, _, _, e⟩ => by cases e⟩ + rw [decide_eq_true_iff, habs] + have hk : RngKeyA c := by + rw [rngKeyA_iffA] + cases c <;> simp only [NeedsFinCA] at hN <;> first | rfl | exact absurd ⟨_, rfl⟩ hI + let Q : capability.Cap → Prop := fun d => RngKeyA d ∧ ptrA d = ptrA c + have habs : isFinalCap' (absCapA c) σ ↔ ∀ w ∈ OS, w ≠ cslotAddrA sl → ¬ Q (capA st s w) := by + refine isFinalCap_iffA F hsl hc Q (fun y hy => ?_) ⟨hk, rfl⟩ (fun h => RngKeyA.not_null h.1) + rw [genRefs_keyA hk hwf, overlap_objA (F.wf hy), specPtr_iff] + refine ⟨fun ⟨h, e⟩ => ?_, fun ⟨h, e⟩ => ⟨Or.inl h, e⟩⟩ + rcases h with h | h + · exact ⟨h, e⟩ + · exact absurd (hc.symm ▸ e.symm : ptrA (capA st s (cslotAddrA sl)) = ptrA (capA st s y)) + (F.fr y hy _ hx h (hc.symm ▸ hk)) + by_cases hz : IsZombieA c + · obtain ⟨p, t, n, rfl⟩ := hz + have alone : ∀ w ∈ OS, w ≠ cslotAddrA sl → ¬ Q (capA st s w) := fun w hw hne h => + F.zf _ hx p t n hc w hw hne h.1 (h.2.trans (ptrA_zombie p t n)) + have hcon : cdt.is_final_capability_with_cap st s (cslotAddrA sl) (.Zombie p t n) = ok true := by + have so : ∀ d, objects.same_object_as d (.Zombie p t n) = ok false ∧ + objects.same_object_as (.Zombie p t n) d = ok false := by + intro d + cases d <;> simp [objects.same_object_as, objects.same_region_as, objects.cap_is_physical] + refine is_final.true_of (Or.inr ?_) (Or.inr ?_) + · exact (so _).1 + · exact (so _).2 + exact ⟨⟨true, hcon, by simp [habs.2 alone]⟩, fun _ => habs.2 alone⟩ + have hcon := is_final_iffA F.lk F.nz hx hc hcn Q (fun d hd => so_key_falseA hk hd) (fun y hy hne hQ => by + have hcl : clsA (capA st s y) = clsA (capA st s (cslotAddrA sl)) := + F.cls.cls y hy _ hx hQ.1 (hc.symm ▸ hk) (hc.symm ▸ hQ.2) + obtain ⟨b, hb, kb, cb, hl⟩ := F.lk.cls_nb F.rng hx hy hne (hc.symm ▸ hk) hQ.1 hcl + rw [hc] at cb + have kd : kindA (capA st s b) = kindA c := by + have := F.kind.kind b hb _ hx kb (hc.symm ▸ hk) (by rw [hc]; exact ptrA_of_cls cb) + rwa [hc] at this + obtain ⟨s1, s2⟩ := so_keyA hk hz hwf (F.wf hb) kd cb + rcases hl with hl | hl + · exact ⟨b, hb, Or.inl ⟨hl, s1⟩⟩ + · exact ⟨b, hb, Or.inr ⟨hl, s2⟩⟩) + refine ⟨⟨_, hcon, ?_⟩, fun h => absurd h hz⟩ + rw [decide_eq_true_iff, habs] + +/-- **FL4's test as the `decide` the loops pass to `finalise_cap`.** -/ +theorem FinalHypA.eq_decide {th : TypedHeapA} (h : FinalHypA st th) {OS : Set U64} {G : List (List U64)} {s : S} + {σ : Fl4SpecA64.State Unit} {sl : CslotPtr} {c : capability.Cap} (F : FinStA st th OS G s σ) + (hsl : AbsSlotA th sl) (hc : capA st s (cslotAddrA sl) = c) (hN : NeedsFinCA c) [Decidable (isFinalCap' (absCapA c) σ)] : + cdt.is_final_capability_with_cap st s (cslotAddrA sl) c = ok (decide (isFinalCap' (absCapA c) σ)) := by + obtain ⟨⟨b, hb, e⟩, -⟩ := h OS G s σ sl c F hsl hc hN + rw [hb]; congr 1 + cases b <;> simp_all + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/CnodeFrontA.lean b/proofs/Fl4ProofsA64/P3/CnodeFrontA.lean new file mode 100644 index 000000000..87104972c --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/CnodeFrontA.lean @@ -0,0 +1,184 @@ +/- +P3 (aarch64, A64-P3 S3 gen 17, ruling 17:58Z item 1): **the CNode decode from the front end** — `cnode_decode_refinesA` +(`P3/CnodeRotA`) with its label, argument and extra-cap case hypotheses discharged from `P3/FrontEndExtraA`'s `J` +facts (x86: the front-fact half of `P3JCnCp.decCnCpSt`, `cnode_canon` of `P3/FrontEndLabel`). + +* **The label** (x86 `cnode_canon`): FL4's dispatcher hands l4v's decoders `fl4SpecLabel w` (`Ext/Label`: l4v's number + of FL4's label `w`, 0 for an FL4-only one). `invocationType_fromEnumA` (l4v's `invocation_type` of l4v's number of + a label is that label), `gen_specWordA` (`gen_invocation_type ∘ specLabelWord = fl4GenA`), `fl4Gen_tableA` (FL4's 74 + labels: 21..28 are l4v's eight CNode labels, every other number no CNode label), `cnodeDec_outA`, and + **`cnode_canonA`**: a spec word `sl` with `gen_invocation_type sl = cnodeLabelOfA lbl` on which the CNode decoder + runs as on `fl4SpecLabel lbl`. +* **`cnode_decode_frontA`**: `cnode.decode` at the message label `info.label`, length `msgLenA info buf` and the + looked-up extra slots refines `decodeCnodeInvocation (fl4SpecLabel mi.miLabel) (getMrs words) ac (extra caps)` + for Copy / Mint / Move / Mutate / Rotate (24..28) and every label outside 21..28: the label from + `JAtZA.frontJ` (`info.label.bv = mi.miLabel`) and `cnode_canonA`, `hlen` / `hargs` from `JAtZA.argsJ`, the extra + caps' `ExtraRel2A` from `JAtZA.frontJ`'s `ExtraOKA` by `ExtraOKA.rel2`. + +Case hypotheses (decode-site, not named premises): `cnode_decode_refinesA`'s own (`JEntA.restartA`'s `P`, `Y`, `hrun`, +`hcur`, `hvs`, `hvc`; the tree `hT`, `hsub`, `hnul`; the cap `hca`, `hcv`), and FrontEndExtraA's **`RegViewA`** / +**`BufViewA`** [J-side decode-site hypotheses, discharged when S4's IPC relation enters `JAtZA`]. (Gen 18, ruling +18:13Z item 2: gen 17's `hnz`, slots of `th` nonzero, is now `PlaceA.nz` / `PlaceA.slot_ne0` and no longer a +hypothesis; statement changed.) No named premise. New module. +-/ +import Fl4ProofsA64.P3.FrontEndExtraA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## The label -/ + +section lbl +open Fl4SpecA64 + +set_option maxRecDepth 8000 in +theorem enum_fromEnumA (l : InvocationLabel) : InvocationLabel.enum[l.fromEnum]? = some l ∧ l.fromEnum < 62 := by + cases l with + | genInvocationLabel g => cases g <;> decide + | archInvocationLabel g => cases g <;> decide + +/-- l4v's `invocation_type` of l4v's number of a label is that label. -/ +theorem invocationType_fromEnumA (l : InvocationLabel) : + invocationType (BitVec.ofNat 64 l.fromEnum) = l := by + obtain ⟨he, hlt⟩ := enum_fromEnumA l + have hd : dataToNat (BitVec.ofNat 64 l.fromEnum) = l.fromEnum := by + show (BitVec.ofNat 64 l.fromEnum).toNat = _ + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + unfold invocationType + rw [if_pos ⟨l, hd.symm⟩, hd] + unfold InvocationLabel.toEnum + rw [he]; rfl + +/-- The generic label l4v's decoders see for an FL4 label. -/ +def fl4GenA : Fl4Label → GenInvocationLabels + | .spec (.genInvocationLabel g) => g + | _ => .invalidInvocation + +theorem gen_specWordA (x : Fl4Label) : genInvocationType (specLabelWord x) = fl4GenA x := by + cases x with + | spec l => + unfold specLabelWord genInvocationType + rw [invocationType_fromEnumA] + cases l <;> rfl + | ext e => + have h0 : specLabelWord (.ext e) = + BitVec.ofNat 64 (InvocationLabel.genInvocationLabel .invalidInvocation).fromEnum := by + show (0 : Data) = _; decide + rw [h0]; unfold genInvocationType; rw [invocationType_fromEnumA]; rfl + +/-- `cnodeLabelOfA` on the label's number. -/ +def cnodeLabN (n : Nat) : GenInvocationLabels := + if n = 21 then .cnodeRevoke else if n = 22 then .cnodeDelete + else if n = 23 then .cnodeCancelBadgedSends else if n = 24 then .cnodeCopy + else if n = 25 then .cnodeMint else if n = 26 then .cnodeMove + else if n = 27 then .cnodeMutate else if n = 28 then .cnodeRotate + else .invalidInvocation + +theorem cnodeLabelOfA_eqN (lbl : U64) : cnodeLabelOfA lbl = cnodeLabN lbl.val := rfl + +/-- The generic label of FL4's label number `n`. -/ +def fl4GenN (n : Nat) : GenInvocationLabels := + fl4GenA ((fl4Numbering[n]?).getD (.spec (.genInvocationLabel .invalidInvocation))) + +set_option maxRecDepth 8000 in +/-- FL4's labels 21..28 are l4v's CNode labels, no other number is one. -/ +theorem fl4Gen_tableA : ∀ n, n < 74 → + (21 ≤ n ∧ n ≤ 28 → fl4GenN n = cnodeLabN n) ∧ + (¬ (21 ≤ n ∧ n ≤ 28) → fl4GenN n ∉ GenInvocationLabels.uptoEnum .cnodeRevoke .cnodeRotate) := by + decide + +theorem fl4GenN_ge {n : Nat} (h : 74 ≤ n) : fl4GenN n = .invalidInvocation := by + unfold fl4GenN + rw [List.getElem?_eq_none (by show n ≥ 74; exact h)]; rfl + +theorem fl4SpecLabel_genA (lbl : U64) : genInvocationType (fl4SpecLabel lbl.bv) = fl4GenN lbl.val := by + unfold fl4SpecLabel fl4LabelOf; rw [gen_specWordA]; rfl + +theorem cnodeDec_outA {l : Data} (h : genInvocationType l ∉ GenInvocationLabels.uptoEnum .cnodeRevoke .cnodeRotate) + (args : List Data) (cap : Cap) (exs : List Cap) : + decodeCnodeInvocation (Z := Unit) l args cap exs = Fl4Spec.NonDetE.throwError .illegalOperation := by + rw [cnodeDecode_eqA] + simp only [Fl4Spec.NonDetE.unlessE, if_neg h, Fl4Spec.NonDetE.throwError, ndE_throw_bindA] + +/-- **The CNode label, canonically** (x86 `cnode_canon`): a spec word whose generic label is `cnodeLabelOfA lbl`, on +which the CNode decoder runs as on `fl4SpecLabel lbl`. -/ +theorem cnode_canonA (lbl : U64) : ∃ sl : Data, genInvocationType sl = cnodeLabelOfA lbl ∧ + ∀ args cap exs, decodeCnodeInvocation (Z := Unit) (fl4SpecLabel lbl.bv) args cap exs = + decodeCnodeInvocation sl args cap exs := by + rw [cnodeLabelOfA_eqN] + by_cases h : 21 ≤ lbl.val ∧ lbl.val ≤ 28 + · refine ⟨fl4SpecLabel lbl.bv, ?_, fun _ _ _ => rfl⟩ + rw [fl4SpecLabel_genA]; exact (fl4Gen_tableA lbl.val (by omega)).1 h + · have hz : cnodeLabN lbl.val = .invalidInvocation := by + unfold cnodeLabN; split_ifs <;> first | rfl | omega + have hout : genInvocationType (fl4SpecLabel lbl.bv) ∉ + GenInvocationLabels.uptoEnum .cnodeRevoke .cnodeRotate := by + rw [fl4SpecLabel_genA] + by_cases h74 : lbl.val < 74 + · exact (fl4Gen_tableA lbl.val h74).2 h + · rw [fl4GenN_ge (by omega)]; decide + have h0 : genInvocationType (0 : Data) = .invalidInvocation := by + have := gen_specWordA (.ext .tcbReadFpu); exact this + refine ⟨0, by rw [h0, hz], fun args cap exs => ?_⟩ + rw [cnodeDec_outA hout, cnodeDec_outA (by rw [h0]; decide)] + +end lbl + +/-! ## The CNode decode at `J` -/ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +set_option maxHeartbeats 4000000 in +/-- **`cnode::decode` from the front end** (module docstring; ruling 17:58Z item 1): at the message label, FL4's +message length and the looked-up extra slots, FL4's CNode decode refines l4v's on the label word FL4's dispatcher +hands it (`fl4SpecLabel mi.miLabel`), the `getMrs` words and the extra caps, for labels 24..28 and every label outside +21..28; the label, argument and extra-cap facts come from `JAtZA.frontJ` / `JAtZA.argsJ` / `ExtraOKA.rel2`. -/ +theorem cnode_decode_frontA (arI : machine.Arch A) (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + {Rp Nt : Set U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} {slot : U64} + (P : SysPlace st H Z.Cs H.frame Rp Nt OS s extra slot) (Y : SymL st H Z.Cs s) + (hrun : Runnable (L.mem s (stA (L.curW s)))) + (hcur : (L.mem s (inqA (L.curW s)) &&& sched.Q_RELEASE) = 0#u64 ∧ L.mem s (scA (L.curW s)) ≠ 0#u64 ∧ + L.mem s (scA (L.curW s)) ∈ Ac) + (hvs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → Aligned b ∧ b ≠ stA (L.curW s)) + (hvc : ∀ v ∈ Z.V, stA (L.curW s) ≠ v ∧ stA (L.curW s) ≠ wa v 24#u64) + (hT : TreeA st OS Z.G.chains Z.G.par s) (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) + (hRV : RegViewA (L.mem s) σ (L.curW s)) + {info : msginfo.MessageInfo} (hinfo : msg.msg_info st s (L.curW s) = ok info) + {mi : Fl4SpecA64.MessageInfo} + (hmi : Fl4SpecA64.getMessageInfo (Z := Unit) (specPtr (L.curW s)) σ = ({(mi, σ)}, False)) + {buf : U64} {buffer : Option Fl4SpecA64.ObjRef} + (hBV : BufViewA (L.mem s) σ.machineState.underlyingMemory buf buffer) + (hr2 : ipc.lookup_extra_caps st s (L.curW s) buf info = ok (.Ok extra)) + {exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)} + (her : (Fl4SpecA64.lookupExtraCaps (Z := Unit) (specPtr (L.curW s)) buffer mi).run σ = ({(.ok exs, σ)}, False)) + {argsU : List U64} + (hmrs : Fl4SpecA64.getMrs (Z := Unit) (specPtr (L.curW s)) buffer mi σ = ({(argsU.map UScalar.bv, σ)}, False)) + (hl : info.label.val < 21 ∨ 28 < info.label.val ∨ (24 ≤ info.label.val ∧ info.label.val ≤ 28)) + (cap : capability.Cap) (ac : Fl4SpecA64.Cap) (hca : CapRelA cap ac) (hcv : CapValidA Z.th cap) : + cnode.decode st arI s a info.label (msgLenA info buf) cap buf extra ⦃ res => + InvPostA (L := L) AR Z OS σ m (Fl4SpecA64.decodeCnodeInvocation (Fl4SpecA64.fl4SpecLabel mi.miLabel) + (argsU.map UScalar.bv) ac (exs.map Prod.fst)) s a (res.1, res.2.1) ∧ res.2.2 = a ⦄ := by + obtain ⟨hLb, hXO⟩ := E.j.1.frontJ hRV hinfo hmi hBV hr2 her + obtain ⟨hLen, hArgs, -⟩ := E.j.1.argsJ hRV hinfo hmi hBV hmrs + have hex := ExtraOKA.rel2 E.j.1.place (hXO extra exs rfl rfl) + obtain ⟨sl, hsl, hdec⟩ := cnode_canonA info.label + rw [← hLb, hdec] + have hl' : info.label.val < 21 ∨ 28 < info.label.val ∨ (24 ≤ info.label.val ∧ info.label.val ≤ 27) ∨ + info.label.val = 28 := by omega + exact cnode_decode_refinesA arI E P Y hrun hcur hvs hvc hT hsub hnul info.label sl hsl hl' (msgLenA info buf) buf + argsU hLen hArgs cap ac hca hcv (exAtA extra 0) (exAtA extra 1) (exAtA_idx0 extra) (exAtA_idx1 extra) _ hex + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/CnodeFrontAllA.lean b/proofs/Fl4ProofsA64/P3/CnodeFrontAllA.lean new file mode 100644 index 000000000..7f4fe0cf1 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/CnodeFrontAllA.lean @@ -0,0 +1,91 @@ +/- +P3 (AArch64, A64-P3 Stage-3 integration step 3): **the CNode decode from the front end at every label** — +`cnode_decode_allA` (`P3/CnodeTopA`) with its label, argument and extra-cap hypotheses discharged from +`JAtZA.frontJ` / `JAtZA.argsJ` / `ExtraOKA.rel2` (as `P3/CnodeFrontA` does for labels 24..28). The label is +selected by `CnodeOutA`'s arms, so this is also the front end of Revoke / Delete / CancelBadgedSends (21..23). + +* **`cnode_decode_frontAllA`**. + +Premises: those of `cnode_decode_allA` (decode-site `DelSiteA` [J] and hooks, Stage-3 ledger) and FrontEndExtraA's +`RegViewA` / `BufViewA`. No new named premise. New module; no existing statement changed. +-/ +import Fl4ProofsA64.P3.CnodeTopA +import Fl4ProofsA64.P3.CnodeFrontA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {T E R : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {SR : S → Fl4SpecA64.State Unit → Prop} + +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +set_option maxHeartbeats 4000000 in +/-- **`cnode::decode` at every label from the front end** (module docstring). -/ +theorem cnode_decode_frontAllA (arI : machine.Arch A) (E0 : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + {Rp Nt : Set U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} {slot : U64} + (P : SysPlace st H Z.Cs H.frame Rp Nt OS s extra slot) (Y : SymL st H Z.Cs s) + (hrun : Runnable (L.mem s (stA (L.curW s)))) + (hcur : (L.mem s (inqA (L.curW s)) &&& sched.Q_RELEASE) = 0#u64 ∧ L.mem s (scA (L.curW s)) ≠ 0#u64 ∧ + L.mem s (scA (L.curW s)) ∈ Ac) + (hvs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → Aligned b ∧ b ≠ stA (L.curW s)) + (hvc : ∀ v ∈ Z.V, stA (L.curW s) ≠ v ∧ stA (L.curW s) ≠ wa v 24#u64) + (hT : TreeA st OS Z.G.chains Z.G.par s) (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) + -- the deletion arms + (site : DelSiteA st Z.th OS s σ Z.G.chains) (hCan : ∀ x ∈ OS, obj.sext48 x = ok x) {X Xx Xp : DelWA S A} + {Rd Rz : S → A → Fl4SpecA64.State Unit → MdbGhostA → U64 → Prop} + (Hk : DelHypsA st arI H.tcbs Z.th OS Fl4SpecA64.fl4FinaliseCap (DelJWA (st := st) AR Z s X) + (DelJWA (st := st) AR Z s Xx) (DelJWA (st := st) AR Z s Xp) Rd Rz) + (hPA : ∀ s' a' σ' G', DStateA st Z.th OS s' σ' G' → DelJWA (st := st) AR Z s X s' a' σ' G' → + ParentAdjA st G'.chains G'.par s') + (hXr : ∀ x, σ.kheap σ.curThread = some (.tcb x) → + X (L.wr s (stA (L.curW s)) sched.TS_RESTART) a (stUpdA σ σ.curThread x .restart) Z.G) + -- the CancelBadgedSends arm + (hO : SchedOpsEA st T E R Bf SR) (hPl : EPlaced T E R Bf) (hRE : EpStateRel T E R Bf (L.mem s) σ) + (hSR : SR s σ) (hct : L.curW s ∈ T) {xt : Fl4SpecA64.Tcb} + (hkt : σ.kheap (specPtr (L.curW s)) = some (.tcb xt)) (hrunx : xt.tcbState = .running) + (hcurS : σ.curThread = specPtr (L.curW s)) + (hE : ∀ d p b, AbsSlotA Z.th d → capA st s (cslotAddrA d) = .Endpoint p b true true true true → p ∈ E) + (hon : ∀ p ∈ E, ∀ z, σ.kheap (specPtr p) = some (.endpoint z) → ∀ t ∈ T, specPtr t ∈ z.epQueue → + L.mem s (affA t) = L.cpu s) + -- the message, from the front end + (hRV : RegViewA (L.mem s) σ (L.curW s)) + {info : msginfo.MessageInfo} (hinfo : msg.msg_info st s (L.curW s) = ok info) + {mi : Fl4SpecA64.MessageInfo} + (hmi : Fl4SpecA64.getMessageInfo (Z := Unit) (specPtr (L.curW s)) σ = ({(mi, σ)}, False)) + {buf : U64} {buffer : Option Fl4SpecA64.ObjRef} + (hBV : BufViewA (L.mem s) σ.machineState.underlyingMemory buf buffer) + (hr2 : ipc.lookup_extra_caps st s (L.curW s) buf info = ok (.Ok extra)) + {exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)} + (her : (Fl4SpecA64.lookupExtraCaps (Z := Unit) (specPtr (L.curW s)) buffer mi).run σ = ({(.ok exs, σ)}, False)) + {argsU : List U64} + (hmrs : Fl4SpecA64.getMrs (Z := Unit) (specPtr (L.curW s)) buffer mi σ = ({(argsU.map UScalar.bv, σ)}, False)) + (cap : capability.Cap) (ac : Fl4SpecA64.Cap) (hca : CapRelA cap ac) (hcv : CapValidA Z.th cap) : + cnode.decode st arI s a info.label (msgLenA info buf) cap buf extra ⦃ res => + CnodeOutA st T E R Bf SR AR Z OS σ m + (Fl4SpecA64.decodeCnodeInvocation (Fl4SpecA64.fl4SpecLabel mi.miLabel) (argsU.map UScalar.bv) ac + (exs.map Prod.fst)) s a + (DelJWA (st := st) AR Z s X) (DelJWA (st := st) AR Z s Xp) info.label res ⦄ := by + obtain ⟨hLb, hXO⟩ := E0.j.1.frontJ hRV hinfo hmi hBV hr2 her + obtain ⟨hLen, hArgs, -⟩ := E0.j.1.argsJ hRV hinfo hmi hBV hmrs + have hex := ExtraOKA.rel2 E0.j.1.place (hXO extra exs rfl rfl) + obtain ⟨sl, hsl, hdec⟩ := cnode_canonA info.label + rw [← hLb, hdec] + exact cnode_decode_allA arI E0 P Y hrun hcur hvs hvc hT hsub hnul site hCan Hk hPA hXr hO hPl hRE hSR hct hkt + hrunx hcurS hE hon info.label sl hsl (msgLenA info buf) buf argsU hLen hArgs cap ac hca hcv (exAtA extra 0) + (exAtA extra 1) (exAtA_idx0 extra) (exAtA_idx1 extra) _ hex + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/CnodeMdbBridgeA.lean b/proofs/Fl4ProofsA64/P3/CnodeMdbBridgeA.lean new file mode 100644 index 000000000..cc78dc3a1 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/CnodeMdbBridgeA.lean @@ -0,0 +1,41 @@ +/- +P3 (aarch64, Stage-3 ledger): the CNode line's named MDB premises from the A64 P1-MDB invariants. + +* `IrqRunA.ofP1`: `IrqRunA` from P1's (same two clauses; `Fl4ProofsA64.P1.…IrqRunA`). +* `ParentAdjA.ofP1`, `ParentAdjA.ofMdbInv`: `ParentAdjA` from P1's, or from `MdbInvA`. +* `DelInvA.ofP1`: `DelInvA` (with `ZOKA`) from `DelInvP1` over `OS`, for typed slots in `OS`. + +l4v `valid_irq_handlers` is P1's `HandlersActiveA` (no P3 premise restates it). +New module; no existing statement changed. +-/ +import Fl4ProofsA64.P3.CnodeRevokeA +import Fl4ProofsA64.P1.MdbInvA +import Fl4ProofsA64.P1.MdbIrqInvA +import Fl4ProofsA64.P1.MdbDelInvA + +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3 + +open Fl4ProofsA64.P1 + +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +theorem IrqRunA.ofP1 {G : List (List U64)} {s : S} (h : Fl4ProofsA64.P1.W7.R.ST.IrqRunA st G s) : + IrqRunA st G s := + ⟨h.one, h.chunk⟩ + +theorem ParentAdjA.ofP1 {G : List (List U64)} {par : U64 → Option U64} {s : S} + (h : Fl4ProofsA64.P1.W7.R.ST.ParentAdjA st G par s) : ParentAdjA st G par s := + h + +theorem ParentAdjA.ofMdbInv {G : List (List U64)} {par : U64 → Option U64} {s : S} + (h : Fl4ProofsA64.P1.W7.R.ST.MdbInvA st G par s) : ParentAdjA st G par s := + ParentAdjA.ofP1 h.parentAdj + +theorem DelInvA.ofP1 {th : TypedHeapA} {OS : Set U64} {s : S} (hsub : ∀ y, IsSlotA th y → y ∈ OS) + (h : Fl4ProofsA64.P1.W7.R.ST.DelInvP1 st (fun p => th p = some .tcb) (fun p r => th p = some (.cnode r)) OS s) : + DelInvA st th s := + fun y hy p zt n e => h y (hsub y hy) p zt n e + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/CnodeRevokeA.lean b/proofs/Fl4ProofsA64/P3/CnodeRevokeA.lean new file mode 100644 index 000000000..0311790b1 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/CnodeRevokeA.lean @@ -0,0 +1,213 @@ +/- +P3 (AArch64, A64-P3 CNode wave 4): **FL4's `cdt::revoke` refines `cap_revoke`** (x86 `P3/CnodeRevoke`, +`revoke_loop_refinesWG`), against `capRevokeGA (capDeleteGA Fn)` (`fl4_cap_revoke` at `Fn = fl4FinaliseCap`). + +Each round reads the slot's `NEXT`; it stops when there is none or `is_mdb_parent_of(slot, next)` fails, else deletes +`next` (`delete_exposedGA`) and runs the preemption point. The spec stops at a null cap or a slot without children and +otherwise deletes any descendant; FL4's `next` is one. + +* **`ParentAdjA`** ([J], Architect ruling 20:32Z R1; x86 `ParentAdj` from `MdbInv.desc` / `.after`): at every slot of + the tree with a nonzero `NEXT`, `is_mdb_parent_of` decides whether `NEXT` is a ghost descendant. It travels in the + loop's invariant `W` (`hPA`); its preservation across the deletion's steps is the obligation of the `DelHypsA` + instances (Stage-3 ledger; producer the A64 P1 MDB wave). +* The empty-tail case needs no premise: **`TreeA.desc_nextA`** (a slot with a descendant has a nonzero `NEXT`, itself + a descendant: the chain's segments, `Mdb.MdbOK`). +* `cdt_of_parA`, **`ghost_descA`** (x86 `cdt_of_par` / `ghost_desc`): a ghost descendant is a spec `descendantsOf`. +* **`revoke_loop_refinesGA`** (`RevInvGA` → `RevPostGA`), **`revoke_refinesGA`** from `WALK_FUEL` (`muFuelA`). The + measure is `psiDA`: deleting a non-null child lowers it (`DelPostGA`), the preemption point keeps it. + +Named premises: `ParentAdjA` ([J], through `hPA`); `hCan` (P1 `DCan OS`, ledger); `DelHypsA`'s hooks; `DStateA`'s [J]. +-/ +import Fl4ProofsA64.P3.CnodeDelFullA +import Fl4ProofsA64.P3.NoChildA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +variable (st) in +/-- **`is_mdb_parent_of` at `NEXT` is ghost descent** ([J], module docstring; x86 `ParentAdj`). -/ +def ParentAdjA (G : List (List U64)) (par : U64 → Option U64) (s : S) : Prop := + ∀ u ∈ G.flatten, NxA st s u ≠ 0#u64 → ∃ b, cdt.is_mdb_parent_of st s u (NxA st s u) = ok b ∧ + (b = true ↔ Fl4Proofs.P3.Mdb.Desc par (NxA st s u) u) + +/-- **A slot with a ghost descendant has a nonzero `NEXT`, itself a descendant** (module docstring). -/ +theorem TreeA.desc_nextA {T Ω Sg OS : Set U64} {G : List (List U64)} {par : U64 → Option U64} {s : S} + (hO : OSOK T Ω Sg OS) (hT : TreeA st OS G par s) {u y : U64} (hy : Fl4Proofs.P3.Mdb.Desc par y u) : + NxA st s u ≠ 0#u64 ∧ Fl4Proofs.P3.Mdb.Desc par (NxA st s u) u := by + obtain ⟨b, -, hb⟩ := Relation.TransGen.tail'_iff.1 hy + have huG : u ∈ G.flatten := (hT.tree.par_mem b u hb).2 + obtain ⟨ch, hch, huc⟩ := List.mem_flatten.1 huG + obtain ⟨A, B, hsplit⟩ := List.append_of_mem huc + obtain ⟨dd, rest, hpost, hd⟩ := hT.tree.seg ch hch A u B hsplit + have hyd := (hd y).1 hy + cases dd with + | nil => simp at hyd + | cons d0 d' => + have e : ch = A ++ u :: d0 :: (d' ++ rest) := by rw [hsplit, hpost]; simp + have hn := nx_split hT.lk hch e + have hd0 : Fl4Proofs.P3.Mdb.Desc par d0 u := (hd d0).2 (by simp) + have hd0G : d0 ∈ G.flatten := List.mem_flatten.2 ⟨ch, hch, by rw [e]; simp⟩ + rw [hn] + exact ⟨hO.nz _ ((hT.lk.members d0).1 hd0G).1, hd0⟩ + +variable {th : TypedHeapA} {σ : Fl4SpecA64.State Unit} {G : MdbGhostA} {s : S} + +/-- A ghost parent link to a slot is a spec CDT link (x86 `cdt_of_par`). -/ +theorem cdt_of_parA {kb : U64} (hP : PlaceA th kb) (hC : CdtRelA st th s σ G) {y : U64} {q : CslotPtr} + (hq : AbsSlotA th q) (h : G.par y = some (cslotAddrA q)) : + ∃ c, AbsSlotA th c ∧ cslotAddrA c = y ∧ σ.cdt c = some q := by + obtain ⟨c, hcs, rfl⟩ := hC.par_slot _ _ h + have h1 := hC.cdt c hcs + rw [h] at h1 + obtain ⟨q', hq', hqe⟩ := Option.map_eq_some_iff.1 h1 + exact ⟨c, hcs, rfl, by rw [hq', cslotAddrA_inj hP (hC.cdt_slot c q' hq').2 hq hqe]⟩ + +/-- **A ghost descendant of a slot is a spec descendant** (x86 `ghost_desc`). -/ +theorem ghost_descA {kb : U64} (hP : PlaceA th kb) (hC : CdtRelA st th s σ G) {sl : CslotPtr} (hs : AbsSlotA th sl) + {y : U64} (h : Fl4Proofs.P3.Mdb.Desc G.par y (cslotAddrA sl)) : + ∃ c, AbsSlotA th c ∧ cslotAddrA c = y ∧ c ∈ descendantsOf sl σ.cdt := by + refine Relation.TransGen.head_induction_on (motive := fun y _ => + ∃ c, AbsSlotA th c ∧ cslotAddrA c = y ∧ c ∈ descendantsOf sl σ.cdt) h ?_ ?_ + · intro y hb + obtain ⟨c, hcs, he, hcd⟩ := cdt_of_parA hP hC hs hb + exact ⟨c, hcs, he, Relation.TransGen.single hcd⟩ + · intro y b hab _ ih + obtain ⟨cb, hcb, rfl, hd⟩ := ih + obtain ⟨c, hcs, he, hcd⟩ := cdt_of_parA hP hC hcb hab + exact ⟨c, hcs, he, Relation.TransGen.tail hd hcd⟩ + +end + +/-! ## The revoke loop -/ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +variable (st) in +/-- **The revoke loop's invariant** (x86 `RevInvG`). -/ +def RevInvGA (Fn : FinFnA) (W : DelWA S A) (th : TypedHeapA) (OS : Set U64) (sl : CslotPtr) + (σ0 : Fl4SpecA64.State Unit) (x : S × A × U64) : Prop := + ∃ σ G, DStateA st th OS x.1 σ G ∧ W x.1 x.2.1 σ G ∧ psiDA st x.1 G < x.2.2.val ∧ + ∀ y, y ∈ ((capRevokeGA (capDeleteGA Fn) sl).run σ).1 → y ∈ ((capRevokeGA (capDeleteGA Fn) sl).run σ0).1 + +variable (st) in +/-- **What the revoke loop ends with** (x86 `RevPostWG`): an outcome of `cap_revoke` of the same kind. -/ +def RevPostGA (Fn : FinFnA) (W Wp : DelWA S A) (th : TypedHeapA) (OS : Set U64) (sl : CslotPtr) + (σ0 : Fl4SpecA64.State Unit) (res : core.result.Result Unit error.Exc × S × A) : Prop := + ∃ σ' G', DStateA st th OS res.2.1 σ' G' ∧ + ((res.1 = .Ok () ∧ (.ok (), σ') ∈ ((capRevokeGA (capDeleteGA Fn) sl).run σ0).1) ∨ + (res.1 = .Err .Preempted ∧ (.error (), σ') ∈ ((capRevokeGA (capDeleteGA Fn) sl).run σ0).1)) ∧ + WPostA W Wp res.1 res.2.1 res.2.2 σ' G' + +set_option maxHeartbeats 4000000 in +/-- **FL4's revoke loop refines `cap_revoke`** (module docstring; x86 `revoke_loop_refinesWG`). -/ +theorem revoke_loop_refinesGA {ar : machine.Arch A} {T Ω Sg OS : Set U64} {th : TypedHeapA} + (hO : OSOK T Ω Sg OS) (hCan : ∀ x ∈ OS, obj.sext48 x = ok x) {Fn : FinFnA} {W Wx Wp : DelWA S A} + {Rd Rz : S → A → Fl4SpecA64.State Unit → MdbGhostA → U64 → Prop} + (H : DelHypsA st ar T th OS Fn W Wx Wp Rd Rz) + (hPA : ∀ s a σ G, DStateA st th OS s σ G → W s a σ G → ParentAdjA st G.chains G.par s) + (sl : CslotPtr) (hA : AbsSlotA th sl) (σ0 : Fl4SpecA64.State Unit) (s : S) (a : A) (fuel : U64) + (hinv : RevInvGA st Fn W th OS sl σ0 (s, a, fuel)) : + cdt.revoke_loop st ar s a (cslotAddrA sl) fuel ⦃ res => RevPostGA st Fn W Wp th OS sl σ0 res ⦄ := by + unfold cdt.revoke_loop + refine loop.spec_decr_nat (fun x => x.2.2.val) (RevInvGA st Fn W th OS sl σ0) _ _ _ ?_ hinv + rintro ⟨s, a, fuel⟩ ⟨σ, G, D, hW, hlt, htr⟩ + replace hlt : psiDA st s G < fuel.val := hlt + replace hW : W s a σ G := hW + replace htr : ∀ y, y ∈ ((capRevokeGA (capDeleteGA Fn) sl).run σ).1 → + y ∈ ((capRevokeGA (capDeleteGA Fn) sl).run σ0).1 := htr + unfold cdt.revoke_loop.body + have hf : fuel > 0#u64 := by scalar_tac + obtain ⟨fuel1, hv, hv1⟩ := spec_existsA (U64.sub_spec (x := fuel) (y := 1#u64) (by scalar_tac)) + have hv1' : fuel1.val + 1 = fuel.val := by scalar_tac + simp only [hf, ↓reduceIte, hv, bind_ok, next_eq, obj.Slot.is_null] + obtain ⟨ac, hg, hace⟩ := getCapOne_eqA (st := st) D.rel hA + have hxO := D.mem_slot hA + have stop : (ac = .nullCap ∨ ¬ ∃ c, σ.cdt c = some sl) → + RevPostGA st Fn W Wp th OS sl σ0 (core.result.Result.Ok (), s, a) := fun h => + ⟨σ, G, D, Or.inl ⟨rfl, htr _ (by rw [capRevokeGA_stop hg h]; rfl)⟩, hW⟩ + -- no ghost descendant when `NEXT` is zero or the parent test fails + have nochild : ¬ (∃ y, Fl4Proofs.P3.Mdb.Desc G.par y (cslotAddrA sl)) → ¬ ∃ c, σ.cdt c = some sl := + fun h hh => h (has_child_desc.1 ((D.cdt.hasChildA D.place hA).1 hh)) + by_cases h0 : NxA st s (cslotAddrA sl) = 0#u64 + · simp only [h0, decide_true, ↓reduceIte, WP.spec_ok] + exact stop (Or.inr (nochild fun ⟨y, hy⟩ => (TreeA.desc_nextA hO D.tree hy).1 h0)) + have hcn : capA st s (cslotAddrA sl) ≠ .Null := fun e => h0 (D.nc hxO e).2 + have hx : cslotAddrA sl ∈ G.chains.flatten := (D.lk.members _).2 ⟨hxO, hcn⟩ + obtain ⟨b, hb, hbd⟩ := hPA s a σ G D hW _ hx h0 + simp only [h0, decide_false, Bool.false_eq_true, ↓reduceIte, hb, bind_ok] + cases b with + | false => + simp only [Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + exact stop (Or.inr (nochild fun ⟨y, hy⟩ => by + have := hbd.2 (TreeA.desc_nextA hO D.tree hy).2; cases this)) + | true => + simp only [↓reduceIte] + have hdesc := hbd.1 rfl + obtain ⟨cd, hcd, hcde, hcdesc⟩ := ghost_descA D.place D.cdt hA hdesc + have hacn : ac ≠ .nullCap := hace.ne_null hcn (D.wf hxO) + have hnx : NxA st s (cslotAddrA sl) ∈ G.chains.flatten := by + obtain ⟨b', hb', -⟩ := Relation.TransGen.head'_iff.1 hdesc + exact (D.tree.tree.par_mem _ _ hb').1 + have hcdO : cslotAddrA cd ∈ OS := D.mem_slot hcd + have hccn : capA st s (cslotAddrA cd) ≠ .Null := by + rw [hcde]; exact ((D.lk.members _).1 hnx).2 + obtain ⟨acc, hgc, hacc⟩ := getCapOne_eqA (st := st) D.rel hcd + have haccn : acc ≠ .nullCap := hacc.ne_null hccn (D.wf hcdO) + rw [← hcde] + apply WP.spec_bind (delete_exposedGA hO hCan H cd hcd s a σ G D hW) + rintro ⟨r1, s1, a1⟩ ⟨σd, Gd, D1, -, hps1, hres, hW1⟩ + simp only [uncurry_apply_pair] at D1 hps1 hres hW1 ⊢ + rcases hres with ⟨rfl, ⟨v, hrd⟩, -, hstr, -⟩ | ⟨rfl, hrd⟩ + swap + · simp only [core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame.from, + bind_ok, bind_tc_ok, WP.spec_ok] + exact ⟨σd, Gd, D1, Or.inr ⟨rfl, htr _ (capRevokeGA_del_preempt hg hacn hcdesc hgc haccn + (capDeleteGA_err hrd))⟩, hW1⟩ + have hdm := capDeleteGA_ok hrd + have hlt1 := hstr hccn + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok] + have hW1' : W s1 a1 σd Gd := hW1 + apply WP.spec_bind (H.pp s1 a1 σd Gd D1 hW1') + rintro ⟨r2, s2, a2⟩ ⟨hk2, σp, D2, hcase⟩ + simp only at hk2 D2 hcase ⊢ + have hps2 : psiDA st s2 Gd = psiDA st s1 Gd := psiDA_sameA D1.lk D2.lk hk2 + rcases hcase with ⟨rfl, hpm, hW2⟩ | ⟨rfl, hpm, hW2⟩ + · simp only [uncurry_apply_pair, core.result.Result.Insts.CoreOpsTry.branch, bind_ok, WP.spec_ok] + refine ⟨⟨σp, Gd, D2, hW2, ?_, fun y hy => htr _ (capRevokeGA_step hg hacn hcdesc hgc haccn hdm hpm hy)⟩, ?_⟩ + · show psiDA st s2 Gd < fuel1.val + rw [hps2]; omega + · show fuel1.val < fuel.val; omega + · simp only [uncurry_apply_pair, core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame.from, + bind_ok, bind_tc_ok, WP.spec_ok] + exact ⟨σp, Gd, D2, Or.inr ⟨rfl, htr _ (capRevokeGA_pp_preempt hg hacn hcdesc hgc haccn hdm hpm)⟩, hW2⟩ + +/-- **FL4's `revoke` refines `cap_revoke`** from `WALK_FUEL` (the measure is below it: `muFuelA`). -/ +theorem revoke_refinesGA {ar : machine.Arch A} {T Ω Sg OS : Set U64} {th : TypedHeapA} + (hO : OSOK T Ω Sg OS) (hCan : ∀ x ∈ OS, obj.sext48 x = ok x) {Fn : FinFnA} {W Wx Wp : DelWA S A} + {Rd Rz : S → A → Fl4SpecA64.State Unit → MdbGhostA → U64 → Prop} + (H : DelHypsA st ar T th OS Fn W Wx Wp Rd Rz) + (hPA : ∀ s a σ G, DStateA st th OS s σ G → W s a σ G → ParentAdjA st G.chains G.par s) + (sl : CslotPtr) (hA : AbsSlotA th sl) (s : S) (a : A) (σ : Fl4SpecA64.State Unit) (G : MdbGhostA) + (D : DStateA st th OS s σ G) (hW : W s a σ G) : + cdt.revoke st ar s a (cslotAddrA sl) ⦃ res => RevPostGA st Fn W Wp th OS sl σ res ⦄ := by + unfold cdt.revoke + have hWF : layout.WALK_FUEL.val = 2 ^ 64 - 1 := by simp [layout.WALK_FUEL, U64.rMax] + refine revoke_loop_refinesGA hO hCan H hPA sl hA σ s a layout.WALK_FUEL ⟨σ, G, D, hW, ?_, fun _ h => h⟩ + show psiDA st s G < layout.WALK_FUEL.val + rw [hWF]; exact muFuelA D + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/CnodeRotA.lean b/proofs/Fl4ProofsA64/P3/CnodeRotA.lean new file mode 100644 index 000000000..6c78396bd --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/CnodeRotA.lean @@ -0,0 +1,779 @@ +/- +P3 (aarch64, A64-P3 S3 gen 11, ruling 14:30Z item 1): **the CNode Rotate invocation** (FL4 `cnode::decode_rotate`, +`rotate_slots`) against l4v `decode_cnode_invocation` (the `CNodeRotate` branch) followed by `set_thread_state Restart` +and `invoke_cnode` (`cap_swap` when the source is the destination, otherwise two `cap_move`s) — the A64 port of x86's +`P3/CnodeRotate` (`decode_rotate_rel`, `cnode_rotate_refines`), on the A64 leaves after `JEntA.restartA`. + +* **`cap_move2_jA`** (the two-move leaf, ruling 14:30Z: at the `JAtZAX` level, not a `JEntA.moveA`): FL4's two + `move_cap`s (pivot → destination, then source → pivot) against l4v's two `cap_move`s, from `JEntA` before the first + move. Between the moves only `StateRelA`, `CdtRelA` and A64 P1's tree at `OS` are carried (`cap_move_refinesA`'s + post, with `cdt.move_cap.lr`'s frame for the null non-relation slots); `JAtZAX` is rebuilt once after both moves + (`JAtZAX.of_slotWrites` over the union of the two write sets, `WritesIn.trans1`), and `SInvXA` is carried across + both spec moves (`SInvXA.capMove`, the IRQ node kept by `SpecSlotA`); the ghost after is `rotGA` (`mvGA` twice). +* Spec side: `rotChkSA` / `rotSpecA` / `rotFinA` (the spec's Rotate branch after the target lookup, split at the cap + reads and at the new caps; x86 `copyTail_rotate`'s text), `copyTail_rotateA`, `copyTail_rotate_shortA`, + `rotFin_runA`. +* **`rotate_slots_relA`**: FL4's `rotate_slots` against `rotSpecA` with any tail `K` (`RotRelA`: the spec's error, + state unchanged, or both slots, their lookups' `SlotResRelA`, the distinctness and emptiness checks, non-null caps, + and the spec run reduced to `K` at the two caps). The ensure-empty branch and the source-is-destination branch + share one script (`on_goal` / `all_goals`). +* **`decode_rotate_relA`** (x86 `decode_rotate_rel`), **`cnode_rotate_refinesA`** (x86 `cnode_rotate_refines`), and + **`cnode_decode_refinesA`** (moved here from `CnodeCopyA` and extended in place, ruling 14:30Z item 1: label 28 + added; the extra caps now `ExtraRel2A`, from which the Copy labels' `ExtraRelA` follows, `ExtraRel2A.one`). +* (`noIrq_of_cnRefA`, gen 11 here; gen 12: moved to `CnodeCopyA` and used by `cnode_copy_refinesA` too.) + +Case hypotheses (decode-site facts, not premises): those of `decode_copy_relA` (`JEntA.restartA`'s `P`, `Y`, `hrun`, +`hcur`, `hvs`, `hvc`; the tree `hT`, `hsub`, `hnul`; `hargs`), the extra caps (`ExtraRel2A`, x86 `ExtraRel2`), the +label (`hlbl`). No named premise. New module. +-/ +import Fl4ProofsA64.P3.CnodeCopyA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## The two-move leaf -/ + +/-- The ghost after Rotate's two moves (pivot `sp` → destination `sd`, then source `ss` → pivot). -/ +def rotGA (G : MdbGhostA) (ss sp sd : U64) : MdbGhostA := mvGA (mvGA G sp sd) ss sp + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-- **FL4's two `move_cap`s refine l4v's two `cap_move`s with the whole relation** (module docstring; ruling 14:30Z +item 1): the pivot's new cap `np` moves to the empty destination, then the source's new cap `ns` to the pivot. -/ +theorem cap_move2_jA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} + {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} {ia ie : Nat} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} + (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + (hT : TreeA st OS Z.G.chains Z.G.par s) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) + {ss sp sd : Fl4SpecA64.CslotPtr} (hAs : AbsSlotA Z.th ss) (hAp : AbsSlotA Z.th sp) (hAd : AbsSlotA Z.th sd) + (hsp : cslotAddrA ss ≠ cslotAddrA sp) + (hsn : capAtA (L.mem s) (cslotAddrA ss) ≠ .Null) (hpn : capAtA (L.mem s) (cslotAddrA sp) ≠ .Null) + (hdn : capAtA (L.mem s) (cslotAddrA sd) = .Null) + (hpc : obj.sext48 (cslotAddrA sp) = ok (cslotAddrA sp)) (hdc : obj.sext48 (cslotAddrA sd) = ok (cslotAddrA sd)) + {np ns : capability.Cap} {ap an : Fl4SpecA64.Cap} + (hcp : CapRelA np ap) (hwp : CapRt.EncWf np) (hnp : np ≠ .Null) (halp : UA np (capA st s (cslotAddrA sp))) + (hop : CapObjOkA Z np) + (hcs : CapRelA ns an) (hws : CapRt.EncWf ns) (hns : ns ≠ .Null) (hals : UA ns (capA st s (cslotAddrA ss))) + (hos : CapObjOkA Z ns) : + cdt.move_cap st s np (cslotAddrA sp) (cslotAddrA sd) ⦃ s1 => + cdt.move_cap st s1 ns (cslotAddrA ss) (cslotAddrA sp) ⦃ s2 => + ∃ σ1 σ2, ((), σ1) ∈ (Fl4SpecA64.capMove (Z := Unit) ap sp sd σ).1 ∧ + ((), σ2) ∈ (Fl4SpecA64.capMove (Z := Unit) an ss sp σ1).1 ∧ + JAtZAX (L := L) AR { Z with G := rotGA Z.G (cslotAddrA ss) (cslotAddrA sp) (cslotAddrA sd) } s2 a σ2 m ∧ + TreeA st OS (rotGA Z.G (cslotAddrA ss) (cslotAddrA sp) (cslotAddrA sd)).chains + (rotGA Z.G (cslotAddrA ss) (cslotAddrA sp) (cslotAddrA sd)).par s2 ∧ + (CapOkA σ.interruptIrqNode sd ap → CapOkA σ.interruptIrqNode sp an → + ∀ n, SInvXA σ m n → SInvXA σ2 m n) ⦄ ⦄ := by + have hO := E.sys.ent.os + have hP := E.j.1.place + have hsS : IsSlotA Z.th (cslotAddrA ss) := AbsSlotA.isSlot hP hAs + have hpS : IsSlotA Z.th (cslotAddrA sp) := AbsSlotA.isSlot hP hAp + have hdS : IsSlotA Z.th (cslotAddrA sd) := AbsSlotA.isSlot hP hAd + have hpd : cslotAddrA sp ≠ cslotAddrA sd := fun e => hpn (by rw [e]; exact hdn) + have hsd : cslotAddrA ss ≠ cslotAddrA sd := fun e => hsn (by rw [e]; exact hdn) + have hpG : cslotAddrA sp ∈ Z.G.chains.flatten := (hT.lk.members _).2 ⟨hsub _ hpS, hpn⟩ + have F := hT.lk.lkF hO.nz + have inOS : ∀ y ∈ Z.G.chains.flatten, y ∈ OS := fun y hy => ((hT.lk.members y).1 hy).1 + have hal := FootNA.aligned E.j.1 (bxi_nodes E.sys.ent.bxi) E.scAl E.ap.vcAl + refine WP.spec_mono (WP.spec_and (WP.spec_and (cap_move_refinesA s σ Z.G E.j.1.st E.j.1.cdt hP hO hT hsub hnul + hAp hAd hpn hdn hdc hcp hwp hnp halp) (moveCap_osA hO (hsub _ hpS) (hsub _ hdS) + (fun h => inOS _ (F.pvm _ hpG h)) (fun h => inOS _ (F.nxm _ hpG h)))) + (cdt.move_cap.lr hO (LR.self s) (hsub _ hpS) (hsub _ hdS) hpd hdc hwp (fun h => inOS _ (F.pvm _ hpG h)) + (fun h => inOS _ (F.nxm _ hpG h)))) ?_ + rintro s1 ⟨⟨⟨σ1, hm1, hst1, hcdt1, hT1, hSS1, hcs1⟩, hw1⟩, -, -, es1, ek1⟩ + -- between the moves: the source cap and every null non-relation slot kept + have hsc1 : capA st s1 (cslotAddrA ss) = capA st s (cslotAddrA ss) := ek1 _ (hsub _ hsS) hsp hsd + have hsn1 : capAtA (L.mem s1) (cslotAddrA ss) ≠ .Null := by rw [capAtA_eq, hsc1]; exact hsn + have hnul1 : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s1 y = .Null := fun y hy hn => by + rw [ek1 y hy (fun e => hn (e ▸ hpS)) (fun e => hn (e ▸ hdS))]; exact hnul y hy hn + have hsG1 : cslotAddrA ss ∈ (mvGA Z.G (cslotAddrA sp) (cslotAddrA sd)).chains.flatten := + (hT1.lk.members _).2 ⟨hsub _ hsS, hsn1⟩ + have F1 := hT1.lk.lkF hO.nz + have inOS1 : ∀ y ∈ (mvGA Z.G (cslotAddrA sp) (cslotAddrA sd)).chains.flatten, y ∈ OS := + fun y hy => ((hT1.lk.members y).1 hy).1 + refine WP.spec_mono (WP.spec_and (cap_move_refinesA s1 σ1 _ hst1 hcdt1 hP hO hT1 hsub hnul1 hAs hAp hsn1 es1 hpc + hcs hws hns (by rw [hsc1]; exact hals)) (moveCap_osA hO (hsub _ hsS) (hsub _ hpS) + (fun h => inOS1 _ (F1.pvm _ hsG1 h)) (fun h => inOS1 _ (F1.nxm _ hsG1 h)))) ?_ + rintro s2 ⟨⟨σ2, hm2, hst2, hcdt2, hT2, hSS2, hcs2⟩, hw2⟩ + refine ⟨σ1, σ2, hm1, hm2, E.j.of_slotWrites (hw1.trans1 hw2) (fun b hb' hb => footN_offA E b hb hb') hal _ + (hSS1.trans hSS2) hst2 hcdt2 (fun y hy => ?_), hT2, fun o1 o2 n hI => ?_⟩ + · rcases hcs2 y hy with e | e | e <;> rw [e] + · exact CapObjOkA.null Z + · rcases hcs1 y hy with e' | e' | e' <;> rw [e'] + · exact CapObjOkA.null Z + · exact E.j.capObj hy + · exact hop + · exact hos + · have h1 : SInvXA σ1 m n := SInvXA.capMove hI o1 hm1 + exact SInvXA.capMove h1 (by rw [hSS1.irqN]; exact o2) hm2 + +end + +/-! ## The spec's Rotate branch -/ + +section +open Fl4Spec Fl4SpecA64 + +/-- The spec's Rotate branch from the source cap read on (`K`: what follows the two cap checks). -/ +def rotChkSA (pd sd : Data) (srcSlot pivotSlot : CslotPtr) + (K : CslotPtr → CslotPtr → Cap → Cap → SEMonad Unit CnodeInvocation) : SEMonad Unit CnodeInvocation := do + let srcCap ← NonDetE.liftE <| getCap srcSlot + NonDetE.whenE (srcCap = .nullCap) <| + NonDetE.throwError <| .failedLookup true <| .missingCapability (dataToNat sd) + let pivotCap ← NonDetE.liftE <| getCap pivotSlot + NonDetE.whenE (pivotCap = .nullCap) <| + NonDetE.throwError <| .failedLookup false <| .missingCapability (dataToNat pd) + K srcSlot pivotSlot srcCap pivotCap + +/-- The spec's Rotate branch after the target lookup, its tail `K` left open. -/ +def rotSpecA (e0 e1 : Cap) (a1 a2 a4 a5 : Data) (d : CslotPtr) + (K : CslotPtr → CslotPtr → Cap → Cap → SEMonad Unit CnodeInvocation) : SEMonad Unit CnodeInvocation := do + let srcSlot ← lookupSourceSlot e1 (dataToCptr a4) (dataToNat a5) + let pivotSlot ← lookupPivotSlot e0 (dataToCptr a1) (dataToNat a2) + NonDetE.whenE (pivotSlot = srcSlot ∨ pivotSlot = d) <| NonDetE.throwError .illegalOperation + NonDetE.unlessE (srcSlot = d) <| ensureEmpty d + rotChkSA a2 a5 srcSlot pivotSlot K + +/-- The spec's Rotate tail: the new caps, their null checks, the invocation. -/ +def rotFinA (pnd snd : Data) (d : CslotPtr) (srcSlot pivotSlot : CslotPtr) (srcCap pivotCap : Cap) : + SEMonad Unit CnodeInvocation := do + let newSrcCap ← NonDetE.returnOk <| updateCapData true snd srcCap + let newPivotCap ← NonDetE.returnOk <| updateCapData true pnd pivotCap + NonDetE.whenE (newSrcCap = .nullCap) <| NonDetE.throwError .illegalOperation + NonDetE.whenE (newPivotCap = .nullCap) <| NonDetE.throwError .illegalOperation + NonDetE.returnOk <| .rotateCall newSrcCap newPivotCap srcSlot pivotSlot d + +theorem copyTail_rotateA {label : Data} (hg : genInvocationType label = .cnodeRotate) + (a0 a1 a2 a3 a4 a5 : Data) (rest : List Data) (e0 e1 : Cap) (erest : List Cap) (d : CslotPtr) : + copyTailA label (a0 :: a1 :: a2 :: a3 :: a4 :: a5 :: rest) (e0 :: e1 :: erest) d = + rotSpecA e0 e1 a1 a2 a4 a5 d (rotFinA a0 a3 d) := by + have hnc : GenInvocationLabels.cnodeRotate ∉ GenInvocationLabels.uptoEnum .cnodeCopy .cnodeMutate := by decide + unfold copyTailA rotSpecA rotChkSA rotFinA + simp [hg, hnc, NonDetE.returnOk, the] + +theorem copyTail_rotate_shortA {label : Data} (hg : genInvocationType label = .cnodeRotate) + (args : List Data) (excaps : List Cap) (d : CslotPtr) (h : ¬ (args.length > 5 ∧ excaps.length > 1)) : + copyTailA label args excaps d = NonDetE.throwError .truncatedMessage := by + have hnc : GenInvocationLabels.cnodeRotate ∉ GenInvocationLabels.uptoEnum .cnodeCopy .cnodeMutate := by decide + unfold copyTailA + simp only [hg, hnc, and_false, ↓reduceIte, reduceCtorEq, true_and, if_neg h] + +theorem rotFin_runA (pnd snd : Data) (d sl pl : CslotPtr) (sC pC : Cap) (σ : State Unit) : + (rotFinA pnd snd d sl pl sC pC).run σ = + ({(if updateCapData true snd sC = .nullCap then .error .illegalOperation + else if updateCapData true pnd pC = .nullCap then .error .illegalOperation + else .ok (.rotateCall (updateCapData true snd sC) (updateCapData true pnd pC) sl pl d), σ)}, False) := by + unfold rotFinA + by_cases h1 : updateCapData true snd sC = .nullCap + · simp only [NonDetE.returnOk, ndE_pure_bindA, NonDetE.whenE, h1, ↓reduceIte, NonDetE.throwError, + ndE_throw_bindA]; rfl + · by_cases h2 : updateCapData true pnd pC = .nullCap + · simp only [NonDetE.returnOk, ndE_pure_bindA, NonDetE.whenE, h1, h2, ↓reduceIte, NonDetE.throwError, + ndE_throw_bindA]; rfl + · simp only [NonDetE.returnOk, ndE_pure_bindA, NonDetE.whenE, h1, h2, ↓reduceIte]; rfl + +theorem run_whenE_falseA {σ ε : Type} {P : Prop} [Decidable P] (h : ¬ P) (m : NonDetE σ ε Unit) (s : σ) : + (NonDetE.whenE P m).run s = ({(.ok (), s)}, False) := by + simp only [NonDetE.whenE, h, ↓reduceIte]; rfl + +theorem run_unlessE_trueA {σ ε : Type} {P : Prop} [Decidable P] (h : P) (m : NonDetE σ ε Unit) (s : σ) : + (NonDetE.unlessE P m).run s = ({(.ok (), s)}, False) := by + simp only [NonDetE.unlessE, h, ↓reduceIte]; rfl + +end + +/-! ## `rotate_slots` -/ + +theorem slot_eq_eqA (a b : obj.Slot) : obj.Slot.Insts.CoreCmpPartialEqSlot.eq a b = ok (decide (a = b)) := rfl + +theorem slot_ne_eqA (a b : obj.Slot) : + core.cmp.PartialEq.ne.trait_default obj.Slot.Insts.CoreCmpPartialEqSlot a b = ok (decide (a ≠ b)) := by + unfold core.cmp.PartialEq.ne.trait_default core.cmp.PartialEq.ne.default + simp [obj.Slot.Insts.CoreCmpPartialEqSlot.eq] + exact ⟨fun h => h ▸ rfl, UScalar.eq_of_val_eq⟩ + +/-- `extra[0]` (the pivot root) and `extra[1]` (the source root) against the spec's extra caps (x86 `ExtraRel2`). -/ +def ExtraRel2A (th : TypedHeapA) (mem : U64 → U64) (x0 x1 : U64) (excaps : List Fl4SpecA64.Cap) : Prop := + (x0 = 0#u64 ∧ excaps = []) ∨ + (x0 ≠ 0#u64 ∧ IsSlotA th x0 ∧ ∃ a, CapRelA (capAtA mem x0) a ∧ + ((x1 = 0#u64 ∧ excaps = [a]) ∨ + (x1 ≠ 0#u64 ∧ IsSlotA th x1 ∧ ∃ a' rest, CapRelA (capAtA mem x1) a' ∧ excaps = a :: a' :: rest))) + +theorem ExtraRel2A.one {th : TypedHeapA} {mem : U64 → U64} {x0 x1 : U64} {excaps : List Fl4SpecA64.Cap} + (h : ExtraRel2A th mem x0 x1 excaps) : ExtraRelA th mem x0 excaps := by + rcases h with h | ⟨h1, h2, a, ha, ⟨-, rfl⟩ | ⟨-, -, a', rest, -, rfl⟩⟩ + · exact Or.inl h + · exact Or.inr ⟨h1, h2, a, [], ha, rfl⟩ + · exact Or.inr ⟨h1, h2, a, a' :: rest, ha, rfl⟩ + +/-- FL4's `rotate_slots` outcome against the spec's `rotSpecA` with tail `K` (module docstring). -/ +def RotRelA (th : TypedHeapA) (mem : U64 → U64) (σ : Fl4SpecA64.State Unit) (c0 c1 : capability.Cap) + (dsl : Fl4SpecA64.CslotPtr) (msp : Fl4SpecA64.SEMonad Unit Fl4SpecA64.CnodeInvocation) + (K : Fl4SpecA64.CslotPtr → Fl4SpecA64.CslotPtr → Fl4SpecA64.Cap → Fl4SpecA64.Cap → + Fl4SpecA64.SEMonad Unit Fl4SpecA64.CnodeInvocation) : + core.result.Result (obj.Slot × obj.Slot) error.Exc → Prop + | .Err e => ∃ e', msp.run σ = ({(.error e', σ)}, False) ∧ SysErrRelA e e' + | .Ok (x, y) => ∃ sl pl, SlotResRelA th mem c1 (.Ok x) (.ok sl) ∧ SlotResRelA th mem c0 (.Ok y) (.ok pl) ∧ + pl ≠ sl ∧ pl ≠ dsl ∧ (sl ≠ dsl → capAtA mem (cslotAddrA dsl) = .Null) ∧ + capAtA mem x ≠ .Null ∧ capAtA mem y ≠ .Null ∧ + ∃ srcA pivA, CapEqA (capAtA mem x) srcA ∧ CapEqA (capAtA mem y) pivA ∧ + Fl4SpecA64.getCap (Z := Unit) sl σ = ({(srcA, σ)}, False) ∧ + Fl4SpecA64.getCap (Z := Unit) pl σ = ({(pivA, σ)}, False) ∧ + msp.run σ = (K sl pl srcA pivA).run σ + +theorem RotRelA.congr {th : TypedHeapA} {mem : U64 → U64} {σ : Fl4SpecA64.State Unit} {c0 c1 : capability.Cap} + {dsl : Fl4SpecA64.CslotPtr} {msp msp' : Fl4SpecA64.SEMonad Unit Fl4SpecA64.CnodeInvocation} + {K : Fl4SpecA64.CslotPtr → Fl4SpecA64.CslotPtr → Fl4SpecA64.Cap → Fl4SpecA64.Cap → + Fl4SpecA64.SEMonad Unit Fl4SpecA64.CnodeInvocation} + (h : msp.run σ = msp'.run σ) {prog : Result (core.result.Result (obj.Slot × obj.Slot) error.Exc)} + (hp : prog ⦃ r => RotRelA th mem σ c0 c1 dsl msp' K r ⦄) : + prog ⦃ r => RotRelA th mem σ c0 c1 dsl msp K r ⦄ := by + refine WP.spec_mono hp ?_ + intro r hq + match r, hq with + | .Err e, ⟨e', h2, h3⟩ => exact ⟨e', h.trans h2, h3⟩ + | .Ok (x, y), ⟨sl, pl, a1, a2, a3, a4, a5, a6, a7, srcA, pivA, h8, h9, g1, g2, h10⟩ => + exact ⟨sl, pl, a1, a2, a3, a4, a5, a6, a7, srcA, pivA, h8, h9, g1, g2, h.trans h10⟩ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **`rotate_slots` refines the spec's Rotate branch up to the new caps** (module docstring; x86 `decode_rotate_rel`'s +first half). -/ +theorem rotate_slots_relA {th : TypedHeapA} {kb : U64} (s : S) (σ : Fl4SpecA64.State Unit) + (hR : StateRelA th (L.mem s) σ) (hV : SlotsValidA th (L.mem s)) (hP : PlaceA th kb) + (buf : U64) (extra : Aeneas.Std.Array obj.Slot 3#usize) (x0 x1 : U64) + (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) + (hx1 : Aeneas.Std.Array.index_usize extra 1#usize = ok x1) + (e0 e1 : Fl4SpecA64.Cap) (hc0 : CapRelA (capA st s x0) e0) (hv0 : CapValidA th (capA st s x0)) + (hc1 : CapRelA (capA st s x1) e1) (hv1 : CapValidA th (capA st s x1)) + (a3 a4 a6 a7 : U64) + (h3 : msg.syscall_arg st s (L.curW s) buf 3#u64 = ok a3) (h4 : msg.syscall_arg st s (L.curW s) buf 4#u64 = ok a4) + (h6 : msg.syscall_arg st s (L.curW s) buf 6#u64 = ok a6) (h7 : msg.syscall_arg st s (L.curW s) buf 7#u64 = ok a7) + (dsl : Fl4SpecA64.CslotPtr) (pd : U64) (hpd1 : dsl.1 = (specPtr pd)) (hpd : th pd = some (.cnode dsl.2.length)) + (K : Fl4SpecA64.CslotPtr → Fl4SpecA64.CslotPtr → Fl4SpecA64.Cap → Fl4SpecA64.Cap → + Fl4SpecA64.SEMonad Unit Fl4SpecA64.CnodeInvocation) : + cnode.rotate_slots st s buf extra (cslotAddrA dsl) ⦃ r => + RotRelA th (L.mem s) σ (capA st s x0) (capA st s x1) dsl + (rotSpecA e0 e1 a3.bv a4.bv a6.bv a7.bv dsl K) K r ⦄ := by + have hAd : AbsSlotA th dsl := ⟨pd, hpd1, Or.inl hpd⟩ + unfold cnode.rotate_slots + simp only [cur_thread_ok, h3, h4, h6, h7, hx0, hx1, bind_ok] + rw [capA_eq s x0, capA_eq s x1] + simp only [bind_ok] + -- the source slot + rw [show cspace.lookup_source_slot st s (capA st s x1) a6 a7 = + cspace.lookup_slot_for_cnode_op st s true (capA st s x1) a6 a7 from rfl] + obtain ⟨r1, hr1, ee1, he1, hrel1⟩ := spec_existsA + (lookup_slot_for_cnode_op_refinesA th s σ hR hV true (capA st s x1) e1 hc1 hv1 a6 a7) + rw [hr1]; simp only [bind_ok] + have hls : (Fl4SpecA64.lookupSourceSlot (Z := Unit) e1 (Fl4SpecA64.dataToCptr a6.bv) + (Fl4SpecA64.dataToNat a7.bv)).run σ = ({(ee1, σ)}, False) := he1 + cases r1 with + | Err e => + cases ee1 with + | ok _ => exact hrel1.elim + | error e' => + simp only [core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, bind_ok, bind_tc_ok] + rw [WP.spec_ok] + exact ⟨e', run_bind_errorA hls _, hrel1⟩ + | Ok src => + cases ee1 with + | error _ => exact hrel1.elim + | ok sl => + obtain ⟨rfl, ps, hps1, hps, hcr1⟩ := hrel1 + refine RotRelA.congr (run_bind_okA hls _) ?_ + have hAs : AbsSlotA th sl := ⟨ps, hps1, Or.inl hps⟩ + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok] + -- the pivot slot + rw [show cspace.lookup_pivot_slot st s (capA st s x0) a3 a4 = + cspace.lookup_slot_for_cnode_op st s true (capA st s x0) a3 a4 from rfl] + obtain ⟨r2, hr2, ee2, he2, hrel2⟩ := spec_existsA + (lookup_slot_for_cnode_op_refinesA th s σ hR hV true (capA st s x0) e0 hc0 hv0 a3 a4) + rw [hr2]; simp only [bind_ok] + have hlp : (Fl4SpecA64.lookupPivotSlot (Z := Unit) e0 (Fl4SpecA64.dataToCptr a3.bv) + (Fl4SpecA64.dataToNat a4.bv)).run σ = ({(ee2, σ)}, False) := he2 + cases r2 with + | Err e => + cases ee2 with + | ok _ => exact hrel2.elim + | error e' => + simp only [core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, bind_ok, bind_tc_ok] + rw [WP.spec_ok] + exact ⟨e', run_bind_errorA hlp _, hrel2⟩ + | Ok piv => + cases ee2 with + | error _ => exact hrel2.elim + | ok pl => + obtain ⟨rfl, pp, hpl1, hpl, hcr2⟩ := hrel2 + refine RotRelA.congr (run_bind_okA hlp _) ?_ + have hAp : AbsSlotA th pl := ⟨pp, hpl1, Or.inl hpl⟩ + simp only [bind_ok, slot_eq_eqA] + by_cases e1 : cslotAddrA pl = cslotAddrA sl + · simp only [show decide (cslotAddrA pl = cslotAddrA sl) = true from decide_eq_true e1, ↓reduceIte, + error.illegal] + rw [WP.spec_ok] + refine ⟨.illegalOperation, ?_, trivial⟩ + have := cslotAddrA_inj hP hAp hAs e1 + simp only [this, true_or, Fl4Spec.NonDetE.whenE, ↓reduceIte, Fl4Spec.NonDetE.throwError, + ndE_throw_bindA]; rfl + have e1' : pl ≠ sl := fun h => e1 (by rw [h]) + by_cases e2 : cslotAddrA pl = cslotAddrA dsl + · simp only [show decide (cslotAddrA pl = cslotAddrA sl) = false from decide_eq_false e1, + show decide (cslotAddrA pl = cslotAddrA dsl) = true from decide_eq_true e2, Bool.false_eq_true, + ↓reduceIte, error.illegal] + rw [WP.spec_ok] + refine ⟨.illegalOperation, ?_, trivial⟩ + have := cslotAddrA_inj hP hAp hAd e2 + simp only [this, or_true, Fl4Spec.NonDetE.whenE, ↓reduceIte, Fl4Spec.NonDetE.throwError, + ndE_throw_bindA]; rfl + have e2' : pl ≠ dsl := fun h => e2 (by rw [h]) + simp only [show decide (cslotAddrA pl = cslotAddrA sl) = false from decide_eq_false e1, + show decide (cslotAddrA pl = cslotAddrA dsl) = false from decide_eq_false e2, Bool.false_eq_true, + ↓reduceIte, slot_ne_eqA, bind_ok] + refine RotRelA.congr (run_bind_okA (run_whenE_falseA (not_or.2 ⟨e1', e2'⟩) _ σ) _) ?_ + obtain ⟨srcA, hsca, hgs⟩ := hR.slot_capA hps1 hps + obtain ⟨pivA, hpca, hgp⟩ := hR.slot_capA hpl1 hpl + by_cases e3 : cslotAddrA sl = cslotAddrA dsl + on_goal 1 => + have e3' : sl = dsl := cslotAddrA_inj hP hAs hAd e3 + simp only [show decide (cslotAddrA sl ≠ cslotAddrA dsl) = false from decide_eq_false (fun h => h e3), + Bool.false_eq_true, ↓reduceIte] + refine RotRelA.congr (run_bind_okA (run_unlessE_trueA e3' _ σ) _) ?_ + have hdd : sl ≠ dsl → capAtA (L.mem s) (cslotAddrA dsl) = .Null := fun h => absurd e3' h + on_goal 2 => + have e3' : sl ≠ dsl := fun h => e3 (by rw [h]) + simp only [show decide (cslotAddrA sl ≠ cslotAddrA dsl) = true from decide_eq_true e3, ↓reduceIte] + unfold cdt.ensure_empty_slot + rw [capA_eq s (cslotAddrA dsl)] + simp only [bind_ok] + obtain ⟨ad, hcad, hgd⟩ := hR.slot_capA hpd1 hpd + replace hcad : CapRelA (capA st s (cslotAddrA dsl)) ad := hcad + have hee := ensureEmpty_runA dsl σ hgd + have hun : (Fl4Spec.NonDetE.unlessE (sl = dsl) (Fl4SpecA64.ensureEmpty dsl) : + Fl4SpecA64.SEMonad Unit Unit).run σ = (Fl4SpecA64.ensureEmpty (Z := Unit) dsl).run σ := by + simp only [Fl4Spec.NonDetE.unlessE, e3', ↓reduceIte] + by_cases hdn : capA st s (cslotAddrA dsl) = .Null + swap + · rw [cap_is_null_neA hdn] + simp only [bind_ok, Bool.false_eq_true, ↓reduceIte, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, bind_tc_ok] + rw [WP.spec_ok] + have : ad ≠ .nullCap := hcad.ne_null hdn + simp only [this, ↓reduceIte] at hee + exact ⟨.deleteFirst, run_bind_errorA (hun.trans hee) _, trivial⟩ + have hadn : ad = .nullCap := by have h := hcad.2; rw [hdn] at h; exact h.symm + simp only [hadn, ↓reduceIte] at hee + rw [hdn, cap_is_null_nullA] + simp only [bind_ok, ↓reduceIte] + refine RotRelA.congr (run_bind_okA (hun.trans hee) _) ?_ + have hdd : sl ≠ dsl → capAtA (L.mem s) (cslotAddrA dsl) = .Null := fun _ => hdn + all_goals + unfold rotChkSA + rw [capA_eq s (cslotAddrA sl)] + simp only [bind_ok] + refine RotRelA.congr (run_bind_okA (run_liftEA hgs) _) ?_ + by_cases hsn : capA st s (cslotAddrA sl) = .Null + · rw [hsn, cap_is_null_nullA] + have : srcA = .nullCap := by have h := hsca.2; rw [capAtA_eq, hsn] at h; exact h.symm + simp only [bind_ok, ↓reduceIte, cnode.missing_source] + rw [WP.spec_ok] + refine ⟨.failedLookup true (.missingCapability a7.val), ?_, ⟨rfl, rfl⟩⟩ + simp only [this, Fl4Spec.NonDetE.whenE, ↓reduceIte, Fl4Spec.NonDetE.throwError, ndE_throw_bindA]; rfl + have hsan : srcA ≠ .nullCap := hsca.ne_null hsn + rw [cap_is_null_neA hsn] + simp only [bind_ok, Bool.false_eq_true, ↓reduceIte] + rw [capA_eq s (cslotAddrA pl)] + simp only [bind_ok] + refine RotRelA.congr (run_bind_okA (run_whenE_falseA hsan _ σ) _) + (RotRelA.congr (run_bind_okA (run_liftEA hgp) _) ?_) + by_cases hpn : capA st s (cslotAddrA pl) = .Null + · rw [hpn, cap_is_null_nullA] + have : pivA = .nullCap := by have h := hpca.2; rw [capAtA_eq, hpn] at h; exact h.symm + simp only [bind_ok, ↓reduceIte, cnode.missing_source] + rw [WP.spec_ok] + refine ⟨.failedLookup false (.missingCapability a4.val), ?_, ⟨rfl, rfl⟩⟩ + simp only [this, Fl4Spec.NonDetE.whenE, ↓reduceIte, Fl4Spec.NonDetE.throwError, ndE_throw_bindA]; rfl + have hpan : pivA ≠ .nullCap := hpca.ne_null hpn + rw [cap_is_null_neA hpn] + simp only [bind_ok, Bool.false_eq_true, ↓reduceIte] + refine RotRelA.congr (run_bind_okA (run_whenE_falseA hpan _ σ) _) ?_ + rw [WP.spec_ok] + exact ⟨sl, pl, ⟨rfl, ps, hps1, hps, hcr1⟩, ⟨rfl, pp, hpl1, hpl, hcr2⟩, e1', e2', hdd, hsn, hpn, + srcA, pivA, hsca.2, hpca.2, hgs, hgp, rfl⟩ + +end + +/-! ## `decode_rotate` -/ + +private theorem capEqA_update {c nc : capability.Cap} {a : Fl4SpecA64.Cap} {d : Fl4SpecA64.Data} + (heq : CapEqA c a) (hupd : absCapA nc = Fl4SpecA64.updateCapData true d (absCapA c)) : + CapEqA nc (Fl4SpecA64.updateCapData true d a) := by + cases c with + | PageTable p m asid va => + cases m + · rw [← heq]; exact CapEqA.of_abs hupd + · obtain ⟨asid', va', rfl, has, hva⟩ := CapEqA.pt_mapped_iff.mp heq + have hu1 : Fl4SpecA64.updateCapData true d (.archObjectCap (.pageTableCap (specPtr p) .normalPtT (some (asid', va')))) = + .archObjectCap (.pageTableCap (specPtr p) .normalPtT (some (asid', va'))) := rfl + have hu2 : Fl4SpecA64.updateCapData true d (absCapA (.PageTable p true asid va)) = + .archObjectCap (.pageTableCap (specPtr p) .normalPtT (some (asid', va.bv))) := by + rw [← has]; rfl + rw [hu1]; rw [hu2] at hupd + cases nc with + | PageTable p' m' asid'' va'' => + cases m' + · simp [absCapA, mapDataA] at hupd + · simp only [absCapA, mapDataA, ↓reduceIte, Fl4SpecA64.Cap.archObjectCap.injEq, + Fl4SpecA64.ArchCap.pageTableCap.injEq, Option.some.injEq, Prod.mk.injEq, true_and] at hupd + exact ⟨hupd.1, hupd.2.1, by rw [hupd.2.2]; exact hva⟩ + | Frame _ _ _ _ _ ept _ _ => cases ept <;> simp [absCapA, absFrameE] at hupd + | _ => simp [absCapA] at hupd + | _ => rw [← heq]; exact CapEqA.of_abs hupd + +private theorem capOk_of_capEqA {nd : Fl4SpecA64.Irq → Fl4SpecA64.ObjRef} {sl : Fl4SpecA64.CslotPtr} + {c : capability.Cap} {a : Fl4SpecA64.Cap} (h : CapEqA c a) (hok : CapOkA nd sl (absCapA c)) : + CapOkA nd sl a := by + cases c with + | PageTable p m asid va => + cases m + · rw [← h]; exact hok + · obtain ⟨_, _, rfl, _, _⟩ := CapEqA.pt_mapped_iff.mp h + refine ⟨fun irq hirq hsl => ?_, fun ⟨_, _, _, _, e⟩ => by cases e⟩ + have hi := hok.1 irq hirq hsl; simp [IrqCapA, absCapA] at hi + | _ => rw [← h]; exact hok + +/-- An untyped cap is kept by `update_cap_data true` (Rotate's and Mutate's preserving update). -/ +theorem update_utA {c c' : capability.Cap} {w : Fl4SpecA64.Data} (hu : isUtA c = true) + (h : absCapA c' = Fl4SpecA64.updateCapData true w (absCapA c)) : absCapA c' = absCapA c := by + obtain ⟨p, b, i, e, rfl⟩ := isUtA_ut hu + rw [h] + simp [absCapA, Fl4SpecA64.updateCapData, Fl4SpecA64.isEpCap, Fl4SpecA64.isNtfnCap, Fl4SpecA64.isCnodeCap, + Fl4SpecA64.isArchCap] + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- **`decode_rotate` refines the spec's Rotate branch** (after the target slot lookup), then `set_thread_state +Restart` and `invoke_cnode` (`cap_swap_jA`, or `cap_move2_jA`) (x86 `decode_rotate_rel`; module docstring). -/ +theorem decode_rotate_relA (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + {Rp Nt : Set U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} {slot : U64} + (P : SysPlace st H Z.Cs H.frame Rp Nt OS s extra slot) (Y : SymL st H Z.Cs s) + (hrun : Runnable (L.mem s (stA (L.curW s)))) + (hcur : (L.mem s (inqA (L.curW s)) &&& sched.Q_RELEASE) = 0#u64 ∧ L.mem s (scA (L.curW s)) ≠ 0#u64 ∧ + L.mem s (scA (L.curW s)) ∈ Ac) + (hvs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → Aligned b ∧ b ≠ stA (L.curW s)) + (hvc : ∀ v ∈ Z.V, stA (L.curW s) ≠ v ∧ stA (L.curW s) ≠ wa v 24#u64) + (hT : TreeA st OS Z.G.chains Z.G.par s) (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) + (slbl : Fl4SpecA64.Data) (hg : Fl4SpecA64.genInvocationType slbl = .cnodeRotate) + (length buf : U64) (args : List U64) (hlen : length.val = args.length) (h2 : 2 ≤ args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) + (x0 x1 : U64) (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) + (hx1 : Aeneas.Std.Array.index_usize extra 1#usize = ok x1) + (excaps : List Fl4SpecA64.Cap) (hex : ExtraRel2A Z.th (L.mem s) x0 x1 excaps) + (dsl : Fl4SpecA64.CslotPtr) (pd : U64) (hpd1 : dsl.1 = (specPtr pd)) (hpd : Z.th pd = some (.cnode dsl.2.length)) + (hpdn : ¬ IrqNodeA (L.ksb s) pd) : + cnode.decode_rotate st s length buf extra (cslotAddrA dsl) ⦃ res => + InvPostA (L := L) AR Z OS σ m (copyTailA slbl ((args.map UScalar.bv).drop 2) excaps dsl) s a res ⦄ := by + have hR := E.j.1.st + have hV : SlotsValidA Z.th (L.mem s) := E.j.1.cv + have hP := E.j.1.place + unfold cnode.decode_rotate + by_cases h8 : length < 8#u64 + · rw [if_pos h8] + simp only [error.truncated, bind_ok, WP.spec_ok] + refine ⟨rfl, .truncatedMessage, ?_, trivial⟩ + rw [copyTail_rotate_shortA hg _ _ _ (fun ⟨h, _⟩ => by simp at h; scalar_tac)]; rfl + rw [if_neg h8] + obtain ⟨a0, a1, a2, a3, a4, a5, a6, a7, rest, rfl⟩ : + ∃ a0 a1 a2 a3 a4 a5 a6 a7 rest, args = a0 :: a1 :: a2 :: a3 :: a4 :: a5 :: a6 :: a7 :: rest := by + match args with + | a0 :: a1 :: a2 :: a3 :: a4 :: a5 :: a6 :: a7 :: rest => exact ⟨a0, a1, a2, a3, a4, a5, a6, a7, rest, rfl⟩ + | [] | [_] | [_, _] | [_, _, _] | [_, _, _, _] | [_, _, _, _, _] | [_, _, _, _, _, _] | [_, _, _, _, _, _, _] => + exfalso; simp at hlen; scalar_tac + simp only [List.map_cons, List.drop_succ_cons, List.drop_zero] + rw [hx0]; simp only [bind_ok, obj.Slot.is_null] + rcases hex with ⟨rfl, rfl⟩ | ⟨hx0n, hx0s, e0, hca0, hx1r⟩ + · simp only [decide_true, ↓reduceIte, error.truncated, bind_ok, WP.spec_ok] + refine ⟨rfl, .truncatedMessage, ?_, trivial⟩ + rw [copyTail_rotate_shortA hg _ _ _ (fun ⟨_, h⟩ => by simp at h)]; rfl + simp only [hx0n, decide_false, Bool.false_eq_true, ↓reduceIte] + rw [hx1]; simp only [bind_ok] + rcases hx1r with ⟨rfl, rfl⟩ | ⟨hx1n, hx1s, e1, erest, hca1, rfl⟩ + · simp only [decide_true, ↓reduceIte, error.truncated, bind_ok, WP.spec_ok] + refine ⟨rfl, .truncatedMessage, ?_, trivial⟩ + rw [copyTail_rotate_shortA hg _ _ _ (fun ⟨_, h⟩ => by simp at h)]; rfl + simp only [hx1n, decide_false, Bool.false_eq_true, ↓reduceIte] + simp only [cur_thread_ok, hargs 2#u64 a2 (by simp), hargs 5#u64 a5 (by simp), bind_ok] + rw [copyTail_rotateA hg] + obtain ⟨r, hr, hrel⟩ := spec_existsA (rotate_slots_relA s σ hR hV hP buf extra x0 x1 hx0 hx1 e0 e1 hca0 + (hV x0 hx0s) hca1 (hV x1 hx1s) a3 a4 a6 a7 (hargs 3#u64 a3 (by simp)) (hargs 4#u64 a4 (by simp)) + (hargs 6#u64 a6 (by simp)) (hargs 7#u64 a7 (by simp)) dsl pd hpd1 hpd (rotFinA a2.bv a5.bv dsl)) + rw [hr]; simp only [bind_ok, core.result.Result.Insts.CoreOpsTry.branch] + cases r with + | Err e => + obtain ⟨e', h1, h2⟩ := hrel + simp only [core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, bind_ok, bind_tc_ok] + rw [WP.spec_ok] + exact ⟨rfl, e', h1, h2⟩ + | Ok v => + obtain ⟨x, y⟩ := v + obtain ⟨sl, pl, ⟨rfl, ps, hps1, hps, hcr1⟩, ⟨rfl, pp, hpl1, hpl, hcr2⟩, hne1, hne2, hdd, hsn, hpn, + srcA, pivA, hsrcEq, hpivEq, -, -, hmsp⟩ := hrel + simp only [capAtA_eq] at hsn hpn hsrcEq hpivEq hmsp hdd + refine InvPostA.congr hmsp ?_ + have hAs : AbsSlotA Z.th sl := ⟨ps, hps1, Or.inl hps⟩ + have hAp : AbsSlotA Z.th pl := ⟨pp, hpl1, Or.inl hpl⟩ + have hAd : AbsSlotA Z.th dsl := ⟨pd, hpd1, Or.inl hpd⟩ + obtain ⟨_, hsca, -⟩ := hR.slot_capA hps1 hps + obtain ⟨_, hpca, -⟩ := hR.slot_capA hpl1 hpl + obtain ⟨ns, hns, hnsw, hnsa, hnse, hnsv, -, hnssh⟩ := + update_relA true a5 (capA st s (cslotAddrA sl)) hsca.1 (CapRt.decodeF_encWf _ _) + obtain ⟨np, hnp, hnpw, hnpa, hnpe, hnpv, -, hnpsh⟩ := + update_relA true a2 (capA st s (cslotAddrA pl)) hpca.1 (CapRt.decodeF_encWf _ _) + set nsA := Fl4SpecA64.updateCapData true a5.bv srcA + set npA := Fl4SpecA64.updateCapData true a2.bv pivA + have hnsEq : CapEqA ns nsA := capEqA_update hsrcEq hnsa + have hnpEq : CapEqA np npA := capEqA_update hpivEq hnpa + have hfin := rotFin_runA a2.bv a5.bv dsl sl pl srcA pivA σ + refine InvPostA.congr (hfin.trans (run_ofExceptA _ σ).symm) ?_ + simp only [bind_ok, uncurry_apply_pair, capA_eq, hns, hnp] + by_cases hnsn : ns = .Null + · subst hnsn + have hnsAn : nsA = .nullCap := hnsEq.symm + rw [cap_is_null_nullA] + simp only [bind_ok, ↓reduceIte, error.illegal] + rw [WP.spec_ok] + exact ⟨rfl, .illegalOperation, by rw [run_ofExceptA]; simp [nsA, hnsAn], trivial⟩ + have hnsan : nsA ≠ .nullCap := CapRelA.ne_null ⟨hnsw, hnsEq⟩ hnsn + rw [cap_is_null_neA hnsn] + simp only [bind_ok, Bool.false_eq_true, ↓reduceIte] + by_cases hnpn : np = .Null + · subst hnpn + have hnpAn : npA = .nullCap := hnpEq.symm + rw [cap_is_null_nullA] + simp only [bind_ok, ↓reduceIte, error.illegal] + rw [WP.spec_ok] + exact ⟨rfl, .illegalOperation, by rw [run_ofExceptA]; simp [nsA, npA, hnsan, hnpAn], trivial⟩ + have hnpan : npA ≠ .nullCap := CapRelA.ne_null ⟨hnpw, hnpEq⟩ hnpn + rw [cap_is_null_neA hnpn] + simp only [bind_ok, Bool.false_eq_true, ↓reduceIte] + have hinv : (ofExceptA (if nsA = .nullCap then .error .illegalOperation + else if npA = .nullCap then .error .illegalOperation + else .ok (Fl4SpecA64.CnodeInvocation.rotateCall nsA npA sl pl dsl))).run σ = + ({(.ok (.rotateCall nsA npA sl pl dsl), σ)}, False) := by + rw [run_ofExceptA]; simp [hnsan, hnpan] + -- the caller's restart + obtain ⟨xx, -, heq, hset, E2, -, hOK, -, hSI⟩ := E.restartA P Y hrun hcur hvs hvc + rw [heq]; simp only [bind_ok, slot_eq_eqA] + have hsS : IsSlotA Z.th (cslotAddrA sl) := hAs.isSlot hP + have hpS : IsSlotA Z.th (cslotAddrA pl) := hAp.isSlot hP + have hsO : cslotAddrA sl ∈ OS := hsub _ hsS + have hpO : cslotAddrA pl ∈ OS := hsub _ hpS + have hdO : cslotAddrA dsl ∈ OS := hsub _ (hAd.isSlot hP) + have hT2 := hT.keepO hOK + have hnul2 : ∀ y ∈ OS, ¬ IsSlotA Z.th y → + capA st (L.wr s (stA (L.curW s)) sched.TS_RESTART) y = .Null := + fun y hy hn => by rw [hOK.capA hy]; exact hnul y hy hn + have hsc2 : capA st (L.wr s (stA (L.curW s)) sched.TS_RESTART) (cslotAddrA sl) = + capA st s (cslotAddrA sl) := hOK.capA hsO + have hpc2 : capA st (L.wr s (stA (L.curW s)) sched.TS_RESTART) (cslotAddrA pl) = + capA st s (cslotAddrA pl) := hOK.capA hpO + have hsn2 : capAtA (L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART)) (cslotAddrA sl) ≠ .Null := by + rw [capAtA_eq, hsc2]; exact hsn + have hpn2 : capAtA (L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART)) (cslotAddrA pl) ≠ .Null := by + rw [capAtA_eq, hpc2]; exact hpn + have hcan : ∀ y ∈ OS, obj.sext48 y = ok y := fun y hy => OSOK.canon E.sys.ent.os P.win hy + have hshs : shapeA ns = shapeA (capA st (L.wr s (stA (L.curW s)) sched.TS_RESTART) (cslotAddrA sl)) := by + rw [hsc2]; exact hnssh hnsn + have hshp : shapeA np = shapeA (capA st (L.wr s (stA (L.curW s)) sched.TS_RESTART) (cslotAddrA pl)) := by + rw [hpc2]; exact hnpsh hnpn + have hals : UA ns (capA st (L.wr s (stA (L.curW s)) sched.TS_RESTART) (cslotAddrA sl)) := by + rw [hsc2] + exact UA.of_shapeA (hnssh hnsn) (fun hu => ut_of_absA (hnssh hnsn) hu (update_utA hu hnsa)) + have halp : UA np (capA st (L.wr s (stA (L.curW s)) sched.TS_RESTART) (cslotAddrA pl)) := by + rw [hpc2] + exact UA.of_shapeA (hnpsh hnpn) (fun hu => ut_of_absA (hnpsh hnpn) hu (update_utA hu hnpa)) + have hos : CapObjOkA Z ns := (E2.j.capObj hsS).of_shapeA hshs fun h => hnsv Z.th (by rw [← hsc2]; exact h) + have hop : CapObjOkA Z np := (E2.j.capObj hpS).of_shapeA hshp fun h => hnpv Z.th (by rw [← hpc2]; exact h) + have hofs : NodeOffA (L.ksb (L.wr s (stA (L.curW s)) sched.TS_RESTART)) ns := + (NodeOffA.of_ctOff (E2.sys.sysl.ct _ hsO)).of_shapeA hshs + have hofp : NodeOffA (L.ksb (L.wr s (stA (L.curW s)) sched.TS_RESTART)) np := + (NodeOffA.of_ctOff (E2.sys.sysl.ct _ hpO)).of_shapeA hshp + have hirq : ∀ (sl' : Fl4SpecA64.CslotPtr) (p : U64), sl'.1 = (specPtr p) → ¬ IrqNodeA (L.ksb s) p → + ¬ IrqNodeA (L.ksb (L.wr s (stA (L.curW s)) sched.TS_RESTART)) (fl4Ptr sl'.1) := + fun sl' p h1 hn => by rw [L.ksb_wr, h1, fl4Ptr_specPtr]; exact hn + have hsI := hirq sl ps hps1 (noIrq_of_cnRefA hP hV (hV x1 hx1s) hcr1) + have hpI := hirq pl pp hpl1 (noIrq_of_cnRefA hP hV (hV x0 hx0s) hcr2) + have hdI := hirq dsl pd hpd1 hpdn + by_cases e3 : cslotAddrA sl = cslotAddrA dsl + · -- the source is the destination: `cap_swap` + have e3' : sl = dsl := cslotAddrA_inj hP hAs hAd e3 + simp only [show decide (cslotAddrA sl = cslotAddrA dsl) = true from decide_eq_true e3, ↓reduceIte] + obtain ⟨s3, hs3, σ', hm, hJ, hT', hI⟩ := spec_existsA (cap_swap_jA E2 hT2 hsub hnul2 hAs hAp + (fun h => hne1 h.symm) hsn2 hpn2 (hcan _ hsO) (hcan _ hpO) ⟨hnsw, hnsEq⟩ ⟨hnpw, hnpEq⟩ hnse hnpe hnsn hnpn + hals halp hos hop) + rw [hs3]; simp only [bind_ok] + rw [WP.spec_ok] + refine ⟨.rotateCall nsA npA sl pl dsl, hinv, _, hset, σ', _, ?_, hJ, hT', + fun n h => hI (capOk_of_capEqA hnpEq (capOk_insA E2.j.1.irq (Or.inl hsI) hofp)) + (capOk_of_capEqA hnsEq (capOk_insA E2.j.1.irq (Or.inl hpI) hofs)) n (hSI n h)⟩ + dsimp only [Fl4SpecA64.invokeCnode] + rw [if_pos e3'] + exact liftE_memA hm + · -- two moves: the pivot to the destination, the source to the pivot + have e3' : sl ≠ dsl := fun h => e3 (by rw [h]) + simp only [show decide (cslotAddrA sl = cslotAddrA dsl) = false from decide_eq_false e3, Bool.false_eq_true, + ↓reduceIte] + have hdn2 : capAtA (L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART)) (cslotAddrA dsl) = .Null := by + rw [capAtA_eq, hOK.capA hdO]; exact hdd e3' + have hsp : cslotAddrA sl ≠ cslotAddrA pl := fun h => hne1 (cslotAddrA_inj hP hAp hAs h.symm) + obtain ⟨s4, hs4, L3⟩ := spec_existsA (cap_move2_jA E2 hT2 hsub hnul2 hAs hAp hAd hsp hsn2 hpn2 hdn2 + (hcan _ hpO) (hcan _ hdO) ⟨hnpw, hnpEq⟩ hnpe hnpn halp hop ⟨hnsw, hnsEq⟩ hnse hnsn hals hos) + obtain ⟨s5, hs5, σ1', σ', hm1, hm2, hJ, hT', hI⟩ := spec_existsA L3 + rw [hs4]; simp only [bind_ok] + rw [hs5]; simp only [bind_ok] + rw [WP.spec_ok] + refine ⟨.rotateCall nsA npA sl pl dsl, hinv, _, hset, σ', _, ?_, hJ, hT', + fun n h => hI (capOk_of_capEqA hnpEq (capOk_insA E2.j.1.irq (Or.inl hdI) hofp)) + (capOk_of_capEqA hnsEq (capOk_insA E2.j.1.irq (Or.inl hpI) hofs)) n (hSI n h)⟩ + dsimp only [Fl4SpecA64.invokeCnode] + rw [if_neg e3'] + exact liftE_memA (mem_bindA.2 ⟨((), σ1'), hm1, hm2⟩) + +/-- **CNode Rotate**, decode and invoke (x86 `cnode_rotate_refines`): FL4's `cnode::decode` at label 28 against +l4v's `decode_cnode_invocation`, then `set_thread_state Restart` and `invoke_cnode`. -/ +theorem cnode_rotate_refinesA (arI : machine.Arch A) (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + {Rp Nt : Set U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} {slot : U64} + (P : SysPlace st H Z.Cs H.frame Rp Nt OS s extra slot) (Y : SymL st H Z.Cs s) + (hrun : Runnable (L.mem s (stA (L.curW s)))) + (hcur : (L.mem s (inqA (L.curW s)) &&& sched.Q_RELEASE) = 0#u64 ∧ L.mem s (scA (L.curW s)) ≠ 0#u64 ∧ + L.mem s (scA (L.curW s)) ∈ Ac) + (hvs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → Aligned b ∧ b ≠ stA (L.curW s)) + (hvc : ∀ v ∈ Z.V, stA (L.curW s) ≠ v ∧ stA (L.curW s) ≠ wa v 24#u64) + (hT : TreeA st OS Z.G.chains Z.G.par s) (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) + (lbl : U64) (slbl : Fl4SpecA64.Data) (hlbl : Fl4SpecA64.genInvocationType slbl = cnodeLabelOfA lbl) + (hl : lbl.val = 28) (length buf : U64) (args : List U64) (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) + (cap : capability.Cap) (ac : Fl4SpecA64.Cap) (hca : CapRelA cap ac) (hcv : CapValidA Z.th cap) + (x0 x1 : U64) (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) + (hx1 : Aeneas.Std.Array.index_usize extra 1#usize = ok x1) + (excaps : List Fl4SpecA64.Cap) (hex : ExtraRel2A Z.th (L.mem s) x0 x1 excaps) : + cnode.decode st arI s a lbl length cap buf extra ⦃ res => + InvPostA (L := L) AR Z OS σ m (Fl4SpecA64.decodeCnodeInvocation slbl (args.map UScalar.bv) ac excaps) s a + (res.1, res.2.1) ∧ res.2.2 = a ⦄ := by + have hg : Fl4SpecA64.genInvocationType slbl = .cnodeRotate := by rw [hlbl]; simp [cnodeLabelOfA, hl] + have hin : Fl4SpecA64.genInvocationType slbl ∈ + Fl4SpecA64.GenInvocationLabels.uptoEnum .cnodeRevoke .cnodeRotate := by rw [hg]; decide + obtain rfl : lbl = fl4_abi.label.CNodeRotate := by rw [u64_eq_iffA]; simp [hl] + unfold cnode.decode + have n1 : ¬ fl4_abi.label.CNodeRotate < fl4_abi.label.CNodeRevoke := by + have := lblA_revoke; have := lblA_rotate; scalar_tac + have n2 : ¬ fl4_abi.label.CNodeRotate > fl4_abi.label.CNodeRotate := by have := lblA_rotate; scalar_tac + rw [if_neg n1, if_neg n2] + by_cases h2 : length < 2#u64 + · rw [if_pos h2] + simp only [error.truncated, bind_ok] + rw [WP.spec_ok] + exact ⟨⟨rfl, .truncatedMessage, decode_shortA _ _ _ σ hin _ (by simp; scalar_tac), trivial⟩, rfl⟩ + rw [if_neg h2] + obtain ⟨y0, y1, rest, rfl⟩ : ∃ y0 y1 rest, args = y0 :: y1 :: rest := by + match args with + | [] => exfalso; simp at hlen; scalar_tac + | [_] => exfalso; simp at hlen; scalar_tac + | y0 :: y1 :: rest => exact ⟨y0, y1, rest, rfl⟩ + simp only [cur_thread_ok, hargs 0#u64 y0 (by simp), hargs 1#u64 y1 (by simp), bind_ok] + simp only [List.map_cons] + rw [decode_lookupA slbl ac excaps hin y0.bv y1.bv (rest.map UScalar.bv)] + unfold cspace.lookup_target_slot + obtain ⟨r1, hr1, e1, he1, hrel1⟩ := spec_existsA + (lookup_slot_for_cnode_op_refinesA Z.th s σ E.j.1.st E.j.1.cv false cap ac hca hcv y0 y1) + have hlt : (Fl4SpecA64.lookupTargetSlot (Z := Unit) ac (Fl4SpecA64.dataToCptr y0.bv) + (Fl4SpecA64.dataToNat y1.bv)).run σ = ({(e1, σ)}, False) := he1 + rw [hr1]; simp only [bind_ok, core.result.Result.Insts.CoreOpsTry.branch] + cases r1 with + | Err e => + cases e1 with + | ok _ => exact hrel1.elim + | error e' => + simp only [core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, bind_ok, bind_tc_ok] + rw [WP.spec_ok] + exact ⟨⟨rfl, e', run_bind_errorA hlt _, hrel1⟩, rfl⟩ + | Ok dest => + cases e1 with + | error _ => exact hrel1.elim + | ok dsl => + obtain ⟨rfl, pd, hpd1, hpd, hcrd⟩ := hrel1 + have g1 : fl4_abi.label.CNodeRotate ≥ fl4_abi.label.CNodeCopy := by + have := lblA_copy; have := lblA_rotate; scalar_tac + have g2 : ¬ fl4_abi.label.CNodeRotate ≤ fl4_abi.label.CNodeMutate := by + have := lblA_mutate; have := lblA_rotate; scalar_tac + have k1 : ¬ fl4_abi.label.CNodeRotate = fl4_abi.label.CNodeRevoke := by rw [u64_eq_iffA]; simp + have k2 : ¬ fl4_abi.label.CNodeRotate = fl4_abi.label.CNodeDelete := by + rw [u64_eq_iffA]; simp [fl4_abi.label.CNodeDelete] + have k3 : ¬ fl4_abi.label.CNodeRotate = fl4_abi.label.CNodeCancelBadgedSends := by + rw [u64_eq_iffA]; simp [fl4_abi.label.CNodeCancelBadgedSends] + simp only [g1, g2, k1, k2, k3, ↓reduceIte, bind_ok] + obtain ⟨⟨r2, s2⟩, hdc, hpost⟩ := spec_existsA + (decode_rotate_relA E P Y hrun hcur hvs hvc hT hsub hnul slbl hg length buf (y0 :: y1 :: rest) hlen + (by simp) hargs x0 x1 hx0 hx1 excaps hex dsl pd hpd1 hpd (noIrq_of_cnRefA E.j.1.place E.j.1.cv hcv hcrd)) + rw [hdc]; simp only [bind_ok, uncurry_apply_pair] + rw [WP.spec_ok] + simp only [List.map_cons, List.drop_succ_cons, List.drop_zero] at hpost + exact ⟨InvPostA.of_run (run_bind_okA hlt _) hpost, rfl⟩ + +/-- **`cnode::decode` refines l4v's CNode decode** for the labels covered so far (x86 `cnode_decode_refines`, +restricted; gen 11: moved from `CnodeCopyA` and extended in place with Rotate, ruling 14:30Z item 1): Copy, Mint, +Move, Mutate (24..27), Rotate (28) and every label outside 21..28. Revoke, Delete and CancelBadgedSends (21..23) are +held (ruling 14:30Z item 2). The extra caps: `ExtraRel2A` (Copy's `ExtraRelA` by `ExtraRel2A.one`). -/ +theorem cnode_decode_refinesA (arI : machine.Arch A) (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + {Rp Nt : Set U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} {slot : U64} + (P : SysPlace st H Z.Cs H.frame Rp Nt OS s extra slot) (Y : SymL st H Z.Cs s) + (hrun : Runnable (L.mem s (stA (L.curW s)))) + (hcur : (L.mem s (inqA (L.curW s)) &&& sched.Q_RELEASE) = 0#u64 ∧ L.mem s (scA (L.curW s)) ≠ 0#u64 ∧ + L.mem s (scA (L.curW s)) ∈ Ac) + (hvs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → Aligned b ∧ b ≠ stA (L.curW s)) + (hvc : ∀ v ∈ Z.V, stA (L.curW s) ≠ v ∧ stA (L.curW s) ≠ wa v 24#u64) + (hT : TreeA st OS Z.G.chains Z.G.par s) (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) + (lbl : U64) (slbl : Fl4SpecA64.Data) (hlbl : Fl4SpecA64.genInvocationType slbl = cnodeLabelOfA lbl) + (hl : lbl.val < 21 ∨ 28 < lbl.val ∨ (24 ≤ lbl.val ∧ lbl.val ≤ 27) ∨ lbl.val = 28) (length buf : U64) + (args : List U64) (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) + (cap : capability.Cap) (ac : Fl4SpecA64.Cap) (hca : CapRelA cap ac) (hcv : CapValidA Z.th cap) + (x0 x1 : U64) (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) + (hx1 : Aeneas.Std.Array.index_usize extra 1#usize = ok x1) + (excaps : List Fl4SpecA64.Cap) (hex : ExtraRel2A Z.th (L.mem s) x0 x1 excaps) : + cnode.decode st arI s a lbl length cap buf extra ⦃ res => + InvPostA (L := L) AR Z OS σ m (Fl4SpecA64.decodeCnodeInvocation slbl (args.map UScalar.bv) ac excaps) s a + (res.1, res.2.1) ∧ res.2.2 = a ⦄ := by + rcases hl with h | h | h | h + · exact cnode_illegal_refinesA arI lbl slbl hlbl (Or.inl h) length buf _ cap ac extra excaps + · exact cnode_illegal_refinesA arI lbl slbl hlbl (Or.inr h) length buf _ cap ac extra excaps + · exact cnode_copy_refinesA arI E P Y hrun hcur hvs hvc hT hsub hnul lbl slbl hlbl h length buf args hlen hargs + cap ac hca hcv x0 hx0 excaps hex.one + · exact cnode_rotate_refinesA arI E P Y hrun hcur hvs hvc hT hsub hnul lbl slbl hlbl h length buf args hlen hargs + cap ac hca hcv x0 x1 hx0 hx1 excaps hex + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/CnodeSpecA.lean b/proofs/Fl4ProofsA64/P3/CnodeSpecA.lean new file mode 100644 index 000000000..6f1085e8f --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/CnodeSpecA.lean @@ -0,0 +1,280 @@ +/- +P3 (aarch64, A64-P3 S3 gen 9, ruling 13:20Z item 1): **l4v `decode_cnode_invocation` split at its binds** for the +CNode Copy, Mint, Move and Mutate labels — the A64 twin of x86's `P3/CnodeSpec` (`Fl4SpecA64/Decode`'s +`decodeCnodeInvocation` is the same text as x86's `Fl4Spec/Decode`'s). + +`copyTailA`, `copyBodyA` and `copyRestA` are the spec's text after the target slot lookup, after the extra-cap check +and after the source cap read; `cnodeDecode_eqA` says (by `rfl`) that they are the pieces of +`decode_cnode_invocation`; `decode_illegalA` (x86 `CnodeDecode.decode_illegal`) is the non-CNode labels. The lemmas +compute the pieces for the four labels, one outcome each (`finishResA`, `copyRest_*A`, `decode_shortA`, +`decode_lookupA`, `copyTail_shortA`, `copyTail_bodyA`, `ensureEmpty_runA`). + +Pure spec; no named premise. New module. +-/ +import Fl4ProofsA64.P3.LookupA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open Classical Fl4Spec Fl4SpecA64 + +noncomputable section + + +/-- The spec after the source cap is read: the per-label arguments, the +new cap, the invocation. -/ +def copyRestA (label : Data) (args : List Data) (srcSlot destSlot : CslotPtr) (srcCap : Cap) : + SEMonad Unit CnodeInvocation := do + let (rights, capData, isMove) ← (match genInvocationType label, args with + | .cnodeCopy, rightsWord :: _ => do + let rights ← NonDetE.returnOk <| dataToRights <| rightsWord + NonDetE.returnOk <| (rights, none, false) + | .cnodeMint, rightsWord :: capData :: _ => do + let rights ← NonDetE.returnOk <| dataToRights <| rightsWord + NonDetE.returnOk <| (rights, some capData, false) + | .cnodeMove, _ => NonDetE.returnOk (allRights, none, true) + | .cnodeMutate, capData :: _ => NonDetE.returnOk (allRights, some capData, true) + | _, _ => NonDetE.throwError .truncatedMessage : SEMonad Unit (CapRights × Option Data × Bool)) + let srcCap ← NonDetE.returnOk <| maskCap rights srcCap + let newCap ← (if isMove then NonDetE.returnOk else deriveCap srcSlot) (match capData with + | some w => updateCapData isMove w srcCap + | none => srcCap) + NonDetE.whenE (newCap = .nullCap) <| NonDetE.throwError .illegalOperation + NonDetE.returnOk <| (if isMove then CnodeInvocation.moveCall else .insertCall) newCap srcSlot destSlot + +/-- The spec's Copy/Mint/Move/Mutate branch. -/ +def copyBodyA (label : Data) (args : List Data) (excaps : List Cap) (destSlot : CslotPtr) : + SEMonad Unit CnodeInvocation := do + let srcIndex ← NonDetE.returnOk <| dataToCptr <| the args[0]? + let srcDepth ← NonDetE.returnOk <| dataToNat <| the args[1]? + let args ← NonDetE.returnOk <| args.drop 2 + let srcRootCap ← NonDetE.returnOk <| the excaps[0]? + ensureEmpty destSlot + let srcSlot ← + lookupSourceSlot srcRootCap srcIndex srcDepth + let srcCap ← NonDetE.liftE <| getCap srcSlot + NonDetE.whenE (srcCap = .nullCap) <| + NonDetE.throwError <| .failedLookup true <| .missingCapability srcDepth + copyRestA label args srcSlot destSlot srcCap + +/-- The spec after the target slot lookup. -/ +def copyTailA (label : Data) (args : List Data) (excaps : List Cap) (destSlot : CslotPtr) : + SEMonad Unit CnodeInvocation := + if args.length ≥ 2 ∧ excaps.length > 0 + ∧ genInvocationType label ∈ GenInvocationLabels.uptoEnum .cnodeCopy .cnodeMutate then + copyBodyA label args excaps destSlot + else if genInvocationType label = .cnodeRevoke then NonDetE.returnOk <| .revokeCall destSlot + else if genInvocationType label = .cnodeDelete then NonDetE.returnOk <| .deleteCall destSlot + else if genInvocationType label = .cnodeCancelBadgedSends then do + let cap ← NonDetE.liftE <| getCap destSlot + NonDetE.unlessE (hasCancelSendRights cap) <| NonDetE.throwError .illegalOperation + NonDetE.returnOk <| .cancelBadgedSendsCall cap + else if genInvocationType label = .cnodeRotate ∧ args.length > 5 + ∧ excaps.length > 1 then + do + let pivotNewData ← NonDetE.returnOk <| the args[0]? + let pivotIndex ← NonDetE.returnOk <| dataToCptr <| the args[1]? + let pivotDepth ← NonDetE.returnOk <| dataToNat <| the args[2]? + let srcNewData ← NonDetE.returnOk <| the args[3]? + let srcIndex ← NonDetE.returnOk <| dataToCptr <| the args[4]? + let srcDepth ← NonDetE.returnOk <| dataToNat <| the args[5]? + let pivotRootCap ← NonDetE.returnOk <| the excaps[0]? + let srcRootCap ← NonDetE.returnOk <| the excaps[1]? + + let srcSlot ← lookupSourceSlot srcRootCap srcIndex srcDepth + let pivotSlot ← lookupPivotSlot pivotRootCap pivotIndex pivotDepth + + NonDetE.whenE (pivotSlot = srcSlot ∨ pivotSlot = destSlot) <| + NonDetE.throwError .illegalOperation + + NonDetE.unlessE (srcSlot = destSlot) <| ensureEmpty destSlot + + let srcCap ← NonDetE.liftE <| getCap srcSlot + NonDetE.whenE (srcCap = .nullCap) <| + NonDetE.throwError <| .failedLookup true <| .missingCapability srcDepth + + let pivotCap ← NonDetE.liftE <| getCap pivotSlot + NonDetE.whenE (pivotCap = .nullCap) <| + NonDetE.throwError <| .failedLookup false <| .missingCapability pivotDepth + + let newSrcCap ← NonDetE.returnOk <| updateCapData true srcNewData srcCap + let newPivotCap ← NonDetE.returnOk <| updateCapData true pivotNewData pivotCap + + NonDetE.whenE (newSrcCap = .nullCap) <| NonDetE.throwError .illegalOperation + NonDetE.whenE (newPivotCap = .nullCap) <| NonDetE.throwError .illegalOperation + + NonDetE.returnOk <| .rotateCall newSrcCap newPivotCap srcSlot pivotSlot destSlot + else + NonDetE.throwError .truncatedMessage + +theorem cnodeDecode_eqA (label : Data) (args : List Data) (cap : Cap) (excaps : List Cap) : + decodeCnodeInvocation (Z := Unit) label args cap excaps = (do + NonDetE.unlessE (genInvocationType label ∈ GenInvocationLabels.uptoEnum .cnodeRevoke .cnodeRotate) <| + NonDetE.throwError .illegalOperation + NonDetE.whenE (args.length < 2) (NonDetE.throwError .truncatedMessage) + let index ← NonDetE.returnOk <| dataToCptr <| the args[0]? + let bits ← NonDetE.returnOk <| dataToNat <| the args[1]? + let args ← NonDetE.returnOk <| args.drop 2 + let destSlot ← lookupTargetSlot cap index bits + copyTailA label args excaps destSlot) := rfl + +/-! ## Computing the pieces -/ + +/-- The spec's outcome after the new cap: an error, `IllegalOperation` for +a null cap, or the insert/move invocation. -/ +def finishResA (mv : Bool) (srcSlot destSlot : CslotPtr) : Except SyscallError Cap → + Except SyscallError CnodeInvocation + | .error e => .error e + | .ok nc => if nc = Cap.nullCap then .error .illegalOperation + else .ok ((if mv then CnodeInvocation.moveCall else .insertCall) nc srcSlot destSlot) + +theorem run_finishA (m : SEMonad Unit Cap) (mv : Bool) (srcSlot destSlot : CslotPtr) (σ : State Unit) + {r' : Except SyscallError Cap} (hm : m.run σ = ({(r', σ)}, False)) : + (do + let newCap ← m + NonDetE.whenE (newCap = Cap.nullCap) <| NonDetE.throwError .illegalOperation + NonDetE.returnOk <| (if mv then CnodeInvocation.moveCall else .insertCall) newCap srcSlot destSlot : + SEMonad Unit CnodeInvocation).run σ = + ({(finishResA mv srcSlot destSlot r', σ)}, False) := by + cases r' with + | error e => rw [run_bind_errorA hm]; rfl + | ok nc => + rw [run_bind_okA hm] + by_cases hn : nc = .nullCap + · simp only [NonDetE.whenE, hn, ↓reduceIte, NonDetE.throwError, ndE_throw_bindA] + simp [finishResA] + · simp only [NonDetE.whenE, hn, ↓reduceIte, ndE_pure_bindA] + simp [finishResA, hn] + +section +variable (label : Data) (srcSlot destSlot : CslotPtr) (srcCap : Cap) (σ : State Unit) + +theorem copyRest_copyA (hgt : genInvocationType label = .cnodeCopy) (w : Data) (rest : List Data) + {r' : Except SyscallError Cap} + (hd : (deriveCap (Z := Unit) srcSlot (maskCap (dataToRights w) srcCap)).run σ = ({(r', σ)}, False)) : + (copyRestA label (w :: rest) srcSlot destSlot srcCap).run σ = + ({(finishResA false srcSlot destSlot r', σ)}, False) := by + unfold copyRestA + simp only [hgt, NonDetE.returnOk, ndE_pure_bindA] + exact run_finishA _ false _ _ σ hd + +theorem copyRest_mintA (hgt : genInvocationType label = .cnodeMint) (w d : Data) (rest : List Data) + {r' : Except SyscallError Cap} + (hd : (deriveCap (Z := Unit) srcSlot (updateCapData false d (maskCap (dataToRights w) srcCap))).run σ = + ({(r', σ)}, False)) : + (copyRestA label (w :: d :: rest) srcSlot destSlot srcCap).run σ = + ({(finishResA false srcSlot destSlot r', σ)}, False) := by + unfold copyRestA + simp only [hgt, NonDetE.returnOk, ndE_pure_bindA] + exact run_finishA _ false _ _ σ hd + +theorem copyRest_moveA (hgt : genInvocationType label = .cnodeMove) (args : List Data) : + (copyRestA label args srcSlot destSlot srcCap).run σ = + ({(finishResA true srcSlot destSlot (.ok (maskCap allRights srcCap)), σ)}, False) := by + unfold copyRestA + simp only [hgt, NonDetE.returnOk, ndE_pure_bindA, ↓reduceIte] + by_cases hn : maskCap allRights srcCap = Cap.nullCap + · simp only [NonDetE.whenE, hn, ↓reduceIte, NonDetE.throwError, ndE_throw_bindA]; simp [finishResA] + · simp only [NonDetE.whenE, hn, ↓reduceIte, ndE_pure_bindA]; simp [finishResA, hn] + +theorem copyRest_mutateA (hgt : genInvocationType label = .cnodeMutate) (d : Data) (rest : List Data) : + (copyRestA label (d :: rest) srcSlot destSlot srcCap).run σ = + ({(finishResA true srcSlot destSlot (.ok (updateCapData true d (maskCap allRights srcCap))), σ)}, False) := by + unfold copyRestA + simp only [hgt, NonDetE.returnOk, ndE_pure_bindA, ↓reduceIte] + by_cases hn : updateCapData true d (maskCap allRights srcCap) = Cap.nullCap + · simp only [NonDetE.whenE, hn, ↓reduceIte, NonDetE.throwError, ndE_throw_bindA]; simp [finishResA] + · simp only [NonDetE.whenE, hn, ↓reduceIte, ndE_pure_bindA]; simp [finishResA, hn] + +theorem copyRest_truncA (args : List Data) + (h : (genInvocationType label = .cnodeCopy ∧ args = []) ∨ + (genInvocationType label = .cnodeMint ∧ args.length < 2) ∨ + (genInvocationType label = .cnodeMutate ∧ args = [])) : + (copyRestA label args srcSlot destSlot srcCap).run σ = ({(.error .truncatedMessage, σ)}, False) := by + unfold copyRestA + rcases h with ⟨hg, rfl⟩ | ⟨hg, hl⟩ | ⟨hg, rfl⟩ + · simp only [hg, NonDetE.throwError, ndE_throw_bindA]; rfl + · match args, hl with + | [], _ => simp only [hg, NonDetE.throwError, ndE_throw_bindA]; rfl + | [_], _ => simp only [hg, NonDetE.throwError, ndE_throw_bindA]; rfl + · simp only [hg, NonDetE.throwError, ndE_throw_bindA]; rfl + +end + +/-! ## Labels -/ + +/-- The four labels this file covers. -/ +def IsCopyLabelA (g : GenInvocationLabels) : Prop := + g = .cnodeCopy ∨ g = .cnodeMint ∨ g = .cnodeMove ∨ g = .cnodeMutate + +theorem IsCopyLabelA.facts {g : GenInvocationLabels} (h : IsCopyLabelA g) : + g ∈ GenInvocationLabels.uptoEnum .cnodeRevoke .cnodeRotate ∧ + g ∈ GenInvocationLabels.uptoEnum .cnodeCopy .cnodeMutate ∧ + g ≠ .cnodeRevoke ∧ g ≠ .cnodeDelete ∧ g ≠ .cnodeCancelBadgedSends ∧ g ≠ .cnodeRotate := by + rcases h with rfl | rfl | rfl | rfl <;> decide + +/-! ## The prefix and the branch -/ + +section +variable (label : Data) (cap : Cap) (excaps : List Cap) (σ : State Unit) + +theorem decode_shortA (h1 : genInvocationType label ∈ GenInvocationLabels.uptoEnum .cnodeRevoke .cnodeRotate) + (args : List Data) (hl : args.length < 2) : + (decodeCnodeInvocation (Z := Unit) label args cap excaps).run σ = ({(.error .truncatedMessage, σ)}, False) := by + rw [cnodeDecode_eqA] + simp only [NonDetE.unlessE, h1, ↓reduceIte, ndE_pure_bindA, NonDetE.whenE, hl, NonDetE.throwError, + ndE_throw_bindA] + rfl + +theorem decode_lookupA (h1 : genInvocationType label ∈ GenInvocationLabels.uptoEnum .cnodeRevoke .cnodeRotate) + (x0 x1 : Data) (rest : List Data) : + decodeCnodeInvocation (Z := Unit) label (x0 :: x1 :: rest) cap excaps = + (lookupTargetSlot cap (dataToCptr x0) (dataToNat x1) >>= fun d => copyTailA label rest excaps d) := by + rw [cnodeDecode_eqA] + simp [NonDetE.unlessE, h1, NonDetE.whenE, NonDetE.returnOk, the] + +theorem copyTail_shortA (hg : IsCopyLabelA (genInvocationType label)) (args : List Data) (d : CslotPtr) + (h : ¬ (args.length ≥ 2 ∧ excaps.length > 0)) : + copyTailA label args excaps d = NonDetE.throwError .truncatedMessage := by + obtain ⟨-, -, h3, h4, h5, h6⟩ := hg.facts + unfold copyTailA + have : ¬ (args.length ≥ 2 ∧ excaps.length > 0 ∧ + genInvocationType label ∈ GenInvocationLabels.uptoEnum .cnodeCopy .cnodeMutate) := + fun ⟨a, b, _⟩ => h ⟨a, b⟩ + simp only [this, h3, h4, h5, false_and, ↓reduceIte, h6] + +theorem copyTail_bodyA (hg : IsCopyLabelA (genInvocationType label)) (y0 y1 : Data) (rest : List Data) + (a0 : Cap) (erest : List Cap) (d : CslotPtr) : + copyTailA label (y0 :: y1 :: rest) (a0 :: erest) d = (do + ensureEmpty d + let srcSlot ← lookupSourceSlot a0 (dataToCptr y0) (dataToNat y1) + let srcCap ← NonDetE.liftE <| getCap srcSlot + NonDetE.whenE (srcCap = .nullCap) <| + NonDetE.throwError <| .failedLookup true <| .missingCapability (dataToNat y1) + copyRestA label rest srcSlot d srcCap) := by + obtain ⟨-, h2, -⟩ := hg.facts + unfold copyTailA copyBodyA + simp [h2, NonDetE.returnOk, the] + +end + +theorem ensureEmpty_runA (sl : CslotPtr) (σ : State Unit) {c : Cap} (hc : getCap (Z := Unit) sl σ = ({(c, σ)}, False)) : + (ensureEmpty (Z := Unit) sl).run σ = + ({(if c = .nullCap then .ok () else .error .deleteFirst, σ)}, False) := by + unfold ensureEmpty + rw [run_bind_okA (run_liftEA hc)] + by_cases h : c = .nullCap + · simp only [NonDetE.whenE, h, ne_eq, not_true_eq_false, ↓reduceIte]; rfl + · simp only [NonDetE.whenE, h, ne_eq, not_false_eq_true, ↓reduceIte]; rfl + + +theorem decode_illegalA (label : Data) (args : List Data) (cap : Cap) (excaps : List Cap) (σ : State Unit) + (h1 : genInvocationType label ∉ GenInvocationLabels.uptoEnum .cnodeRevoke .cnodeRotate) : + (decodeCnodeInvocation (Z := Unit) label args cap excaps).run σ = + ({(.error .illegalOperation, σ)}, False) := by + rw [cnodeDecode_eqA] + simp only [NonDetE.unlessE, h1, ↓reduceIte, NonDetE.throwError, ndE_throw_bindA] + rfl + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/CnodeTopA.lean b/proofs/Fl4ProofsA64/P3/CnodeTopA.lean new file mode 100644 index 000000000..c9c1efe7d --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/CnodeTopA.lean @@ -0,0 +1,135 @@ +/- +P3 (AArch64, A64-P3 CNode wave 5): **the CNode capability at the dispatcher, every label** (x86 `XTopCnodeAll`'s +`decode_invocation_cnodeX`). + +* **`decodeCnode_programA`** (x86 `decode_invocation_cnode…` prefix; pattern of `TcbInvokeDispatchA.decodeTcb_programA`): + FL4's `syscall::decode_invocation` at a CNode cap is, in the first phase, the `InvalidCapability 0` error and + otherwise `cnode::decode`. **`decodeCnode_firstPhase_specA`**: the spec's first phase is the same error (`rfl`); + **`decodeCnode_secondPhase_specA`**: its second phase decodes with `decodeCnodeInvocation` (`rfl`). +* **`CnodeOutA`**: the outcome by label: Revoke / Delete (21, 22) `InvPostDA`, CancelBadgedSends (23) `InvPostCA`, and + Copy, Mint, Move, Mutate, Rotate (24..28) and every label outside 21..28 S3's unedited `InvPostA`. +* **`cnode_decode_allA`**: `cnode::decode` at every label, composing the three arms of this line + (`cnode_revoke_refinesDA`, `cnode_delete_refinesDA`, `cnode_cancel_refinesCA`) with S3's `cnode_decode_refinesA` + (unchanged). + +All premises are those of the composed theorems (module docstrings of `CnodeDecDelA`, `CnodeCancelA`, `CnodeRotA`; +Stage-3 ledger). No new named premise. New module. +-/ +import Fl4ProofsA64.P3.CnodeCancelA +import Fl4ProofsA64.P3.CnodeRotA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## The dispatcher -/ + +section disp +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {s : S} {a : A} {lbl len slot buf : U64} {block call donate first : Bool} +variable {extra : Aeneas.Std.Array obj.Slot 3#usize} {p r gs g : U64} + +/-- FL4's dispatcher at a CNode cap (module docstring). -/ +theorem decodeCnode_programA : + syscall.decode_invocation st ar s a lbl len slot (.CNode p r gs g) block call donate first buf extra = + if first then ok (.Err (.Sys (.InvalidCapability 0#u64)), s, a) + else cnode.decode st ar s a lbl len (.CNode p r gs g) buf extra := by + simp only [syscall.decode_invocation, cur_thread_ok, bind_ok] + +theorem decodeCnode_firstPhase_specA (label : Fl4SpecA64.Data) (args : List Fl4SpecA64.Data) + (c : Fl4SpecA64.CapRef) (sl : Fl4SpecA64.CslotPtr) (ptr : Fl4SpecA64.ObjRef) (bits : Nat) (gd : List Bool) + (caps : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) (buf : Option Fl4SpecA64.ObjRef) (call : Bool) : + Fl4SpecA64.fl4DecodeInvocation (Z := Unit) call true label args c sl (.cnodeCap ptr bits gd) caps buf = + Fl4Spec.NonDetE.throwError (.invalidCapability 0) := rfl + +end disp + +/-! ## The outcome by label -/ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {T E R : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {SR : S → Fl4SpecA64.State Unit → Prop} + +variable (st T E R Bf SR) in +/-- **The outcome of `cnode::decode` by label** (module docstring). -/ +def CnodeOutA (AR : A → HwRootA → Prop) (Z : XSetsA) (OS : Set U64) (σ : Fl4SpecA64.State Unit) + (m : Fl4SpecA64.McState) (msp : Fl4SpecA64.SEMonad Unit Fl4SpecA64.CnodeInvocation) (s : S) (a : A) + (W Wp : DelWA S A) (lbl : U64) (res : core.result.Result Unit error.Exc × S × A) : Prop := + (lbl.val = 21 ∨ lbl.val = 22 → InvPostDA st AR Z OS σ msp s a W Wp res) ∧ + (lbl.val = 23 → InvPostCA st T E R Bf SR σ msp s (res.1, res.2.1) ∧ res.2.2 = a) ∧ + (lbl.val < 21 ∨ 24 ≤ lbl.val → InvPostA (L := L) AR Z OS σ m msp s a (res.1, res.2.1) ∧ res.2.2 = a) + +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +set_option maxHeartbeats 4000000 in +/-- **`cnode::decode` at every label** (module docstring): the label's arm of this line or of S3. -/ +theorem cnode_decode_allA (arI : machine.Arch A) (E0 : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + {Rp Nt : Set U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} {slot : U64} + (P : SysPlace st H Z.Cs H.frame Rp Nt OS s extra slot) (Y : SymL st H Z.Cs s) + (hrun : Runnable (L.mem s (stA (L.curW s)))) + (hcur : (L.mem s (inqA (L.curW s)) &&& sched.Q_RELEASE) = 0#u64 ∧ L.mem s (scA (L.curW s)) ≠ 0#u64 ∧ + L.mem s (scA (L.curW s)) ∈ Ac) + (hvs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → Aligned b ∧ b ≠ stA (L.curW s)) + (hvc : ∀ v ∈ Z.V, stA (L.curW s) ≠ v ∧ stA (L.curW s) ≠ wa v 24#u64) + (hT : TreeA st OS Z.G.chains Z.G.par s) (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) + -- the deletion arms + (site : DelSiteA st Z.th OS s σ Z.G.chains) (hCan : ∀ x ∈ OS, obj.sext48 x = ok x) {X Xx Xp : DelWA S A} + {Rd Rz : S → A → Fl4SpecA64.State Unit → MdbGhostA → U64 → Prop} + (Hk : DelHypsA st arI H.tcbs Z.th OS Fl4SpecA64.fl4FinaliseCap (DelJWA (st := st) AR Z s X) + (DelJWA (st := st) AR Z s Xx) (DelJWA (st := st) AR Z s Xp) Rd Rz) + (hPA : ∀ s' a' σ' G', DStateA st Z.th OS s' σ' G' → DelJWA (st := st) AR Z s X s' a' σ' G' → + ParentAdjA st G'.chains G'.par s') + (hXr : ∀ x, σ.kheap σ.curThread = some (.tcb x) → + X (L.wr s (stA (L.curW s)) sched.TS_RESTART) a (stUpdA σ σ.curThread x .restart) Z.G) + -- the CancelBadgedSends arm + (hO : SchedOpsEA st T E R Bf SR) (hPl : EPlaced T E R Bf) (hRE : EpStateRel T E R Bf (L.mem s) σ) + (hSR : SR s σ) (hct : L.curW s ∈ T) {xt : Fl4SpecA64.Tcb} + (hkt : σ.kheap (specPtr (L.curW s)) = some (.tcb xt)) (hrunx : xt.tcbState = .running) + (hcurS : σ.curThread = specPtr (L.curW s)) + (hE : ∀ d p b, AbsSlotA Z.th d → capA st s (cslotAddrA d) = .Endpoint p b true true true true → p ∈ E) + (hon : ∀ p ∈ E, ∀ z, σ.kheap (specPtr p) = some (.endpoint z) → ∀ t ∈ T, specPtr t ∈ z.epQueue → + L.mem s (affA t) = L.cpu s) + -- the message + (lbl : U64) (slbl : Fl4SpecA64.Data) (hlbl : Fl4SpecA64.genInvocationType slbl = cnodeLabelOfA lbl) + (length buf : U64) (args : List U64) (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) + (cap : capability.Cap) (ac : Fl4SpecA64.Cap) (hca : CapRelA cap ac) (hcv : CapValidA Z.th cap) + (x0 x1 : U64) (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) + (hx1 : Aeneas.Std.Array.index_usize extra 1#usize = ok x1) + (excaps : List Fl4SpecA64.Cap) (hex : ExtraRel2A Z.th (L.mem s) x0 x1 excaps) : + cnode.decode st arI s a lbl length cap buf extra ⦃ res => + CnodeOutA st T E R Bf SR AR Z OS σ m (Fl4SpecA64.decodeCnodeInvocation slbl (args.map UScalar.bv) ac excaps) s a + (DelJWA (st := st) AR Z s X) (DelJWA (st := st) AR Z s Xp) lbl res ⦄ := by + by_cases h21 : lbl.val = 21 + · refine WP.spec_mono (cnode_revoke_refinesDA arI E0 P Y hrun hcur hvs hvc hT hsub hnul site hCan Hk hPA hXr + lbl slbl hlbl h21 length buf args hlen hargs cap ac hca hcv excaps) ?_ + intro res hres + exact ⟨fun _ => hres, fun h => by omega, fun h => by omega⟩ + by_cases h22 : lbl.val = 22 + · refine WP.spec_mono (cnode_delete_refinesDA arI E0 P Y hrun hcur hvs hvc hT hsub hnul site hCan Hk hXr + lbl slbl hlbl h22 length buf args hlen hargs cap ac hca hcv excaps) ?_ + intro res hres + exact ⟨fun _ => hres, fun h => by omega, fun h => by omega⟩ + by_cases h23 : lbl.val = 23 + · refine WP.spec_mono (cnode_cancel_refinesCA arI s a σ E0.j.1.st E0.j.1.cv hO hPl hRE hSR hct hkt hrunx hcurS + hE hon lbl slbl hlbl h23 length buf args hlen hargs cap ac hca hcv extra excaps) ?_ + intro res hres + exact ⟨fun h => by omega, fun _ => hres, fun h => by omega⟩ + have hl' : lbl.val < 21 ∨ 28 < lbl.val ∨ (24 ≤ lbl.val ∧ lbl.val ≤ 27) ∨ lbl.val = 28 := by omega + refine WP.spec_mono (cnode_decode_refinesA arI E0 P Y hrun hcur hvs hvc hT hsub hnul lbl slbl hlbl hl' length buf + args hlen hargs cap ac hca hcv x0 x1 hx0 hx1 excaps hex) ?_ + intro res hres + exact ⟨fun h => by omega, fun h => by omega, fun _ => hres⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelAsidA.lean b/proofs/Fl4ProofsA64/P3/DelAsidA.lean new file mode 100644 index 000000000..b67fbe818 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelAsidA.lean @@ -0,0 +1,398 @@ +/- +P3 (AArch64, delete hooks): **FL4's `delete_asid` at the joint relation** (`vspace.rs` `delete_asid`: the flush of the +root's kind, the pool entry cleared, `set_vm_root` of the current thread) against l4v `delete_asid`. + +* `delPoolWord_offA` / `pool_write_jointA`: `P3/VsAssignA`'s `JEntA.poolWord_offA` / `assign_write_jointA` with the + decode-site `RsSiteA` replaced by its only used part, the slot coverage `hsub` (copied: a deletion's current thread + need not be runnable); +* `JAtZAX.of_vmA` / `JAtZAX.of_hwA`: the relation across `VmA` (`set_vm_root`'s spec side) and a new hardware root; +* **`delete_asid_jA`**: either nothing happens on both sides, or one pool entry is cleared and the spec's pool entry + removed (up to `VmA`), with `JAtZAX` after at some hardware root. + +Hypotheses: `hsplit` (the entry naming the root's object is the root's own tagged word: the kinds are apart), `hcur` +(the current thread is a TCB). [P] `ArchKeepsA`, `DelArchA`, `P0.ArchTotal`. New module. +-/ +import Fl4ProofsA64.P3.VsAssignA +import Fl4ProofsA64.P3.DelAsidSpecA +import Fl4ProofsA64.P3.DelRecA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section Frame +open Fl4ProofsA64.P1.W7.R.ST +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- P1 placement, the heap relation, and the VCPU frame place a pool word off the non-VSpace footprint. -/ +theorem delPoolWord_offA (E : JEntTA st AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) {p i : U64} (hp : p ∈ Z.Pp) (hi : i.val < 512) + (w : PoolW st H Z.Cs OS s (peAF p i)) : + ¬ FootA (L := L) { extZA Z with Pp := ∅ } s (peAF p i) := by + have hpf := E.ap.vs _ (.pool p i hp hi) + have hfit : (L.ksb s).val + 788736 ≤ 2 ^ 64 := E.ent.ksw + have hKs (off : U64) (ho : off.val < 788736) : peAF p i ≠ wa (L.ksb s) off := by + intro e + have h := w.ks.2 + rw [e, wadd_val (by omega)] at h + omega + have hNode (c : U64) (hc : c ∈ Z.Cs) (off : U64) (ho : off.val < 4288) : + peAF p i ≠ L.nodeAt s c off := by + intro e + have h := w.ks.1 c hc + rw [e, nodeAt_val (E.sq.nodes c hc) ho] at h + omega + have hTcb (t : U64) (ht : t ∈ Z.T) (off : U64) (ho : off.val < 4096) : peAF p i ≠ wa t off := by + rw [← E.tcbs] at ht + intro e + have h := hpf.2 t ht + have hf := (E.sq.tcbs t ht).fits + simp only [Nat.reducePow] at hf + rw [e, wadd_val (by omega)] at h + omega + have hSc (c : U64) (hc : c ∈ Z.C) (off : U64) (ho : off.val < 128) : peAF p i ≠ wa c off := by + rw [← E.scs] at hc + obtain ⟨b, hb⟩ := hc + obtain ⟨q, z, hq, hle, hpg⟩ := E.ptH p (Or.inl hp) + have hne : q ≠ c := fun e => by rw [e, hb] at hq; cases hq + have hdis := E.ent.heap.disj q _ c _ hq hb hne + have hqfit := E.ent.heap.fits q _ hq + have hcfit := E.ent.heap.fits c _ hb + have hs : 128 ≤ 2 ^ b := Nat.pow_le_pow_right (by decide : 0 < (2 : Nat)) (E.ent.heap.scmin c _ hb) + simp only [HKind.bits] at hdis hqfit hcfit + intro e + have hv := congrArg UScalar.val e + rw [peAF_valA (by omega) hi, wadd_val (by omega)] at hv + omega + have hSlot (y : U64) (hy : SlotWA Z.th y) (off : U64) (ho : off.val < 32) : peAF p i ≠ wa y off := by + have hy0 : IsSlotA Z.th y := by + rcases hy with hy | ⟨q, r, hq, idx, hl, rfl⟩ + · exact hy + · have hab : AbsSlotA Z.th (specPtr q, idx) := ⟨q, rfl, Or.inl (by rw [hl]; exact hq)⟩ + exact hab.isSlot E.j.1.place + obtain ⟨q, k, j, hq, hSize, hv, _⟩ := slotW_valA E.j.1.place hy + have hf := E.j.1.place.fit q k hq + intro e + exact w.os y (hsub y hy0) (by rw [e, wadd_val (by omega)]; omega) + intro h + generalize heq : peAF p i = waddr at h + cases h with + | slot y _ hy he => + rw [← heq] at he + rcases he with e | e | e | e + · have hh := hSlot y hy 0#u64 (by decide) + exact hh (e.trans (by apply UScalar.eq_of_val_eq; rw [wa_valm]; simp)) + · exact hSlot y hy 8#u64 (by decide) e + · exact hSlot y hy 16#u64 (by decide) e + · exact hSlot y hy 24#u64 (by decide) e + | nslot b h => + cases h with + | node c _ hc he => + rw [← heq] at he + rcases he with h | h | h | h + · exact hNode c hc _ (by simp [layout.node.CUR_THREAD]) h + · exact hNode c hc _ (by simp [layout.node.IDLE_THREAD]) h + · exact hNode c hc _ (by simp [layout.node.CUR_SC]) h + · exact hNode c hc _ (by simp [layout.node.IDLE_SC]) h + | cur _ he => + rw [← heq] at he + rcases he with h | h + · exact hNode _ E.sq.cur _ (by simp [layout.node.SCHED_ACTION]) h + · exact hNode _ E.sq.cur _ (by simp [layout.node.CUR_TIME]) h + | tcb t _ ht he => + rw [← heq] at he + rcases he with h | h | h | h | h + · rw [stA_eq] at h; exact hTcb t ht _ (by decide) h + · exact hTcb t ht _ (by decide) h + · exact hTcb t ht _ (by decide) h + · exact hTcb t ht _ (by decide) h + · exact hTcb t ht _ (by decide) h + | sc c hc => exact hSc c hc 24#u64 (by decide) heq + | vcpu v _ hv he => + rw [← heq] at he + apply E.vsVc _ (.pool p i hp hi) + rcases he with e | e + · exact ⟨v, hv, 0#u64, by decide, by decide, e.trans (by + apply UScalar.eq_of_val_eq; rw [wa_valm]; simp)⟩ + · exact ⟨v, hv, 24#u64, by decide, by decide, e⟩ + | irq j hj => + have e := irqWordA_val (kb := L.ksb s) hfit hj + have h := w.ks.2 + rw [heq, e] at h + omega + | cur2 _ he => + rw [← heq] at he + rcases he with e | e + · exact hNode _ E.sq.cur _ (by simp [layout.node.CONSUMED]) e + · exact hNode _ E.sq.cur _ (by simp [layout.node.REPROGRAM]) e + | glob _ he => + rw [← heq] at he + rcases he with e | e + · exact hKs 8#u64 (by decide) e + · exact hKs 16#u64 (by decide) e + | tcb2 t _ ht he => + rw [← heq] at he + rcases he with e | e + · exact hTcb t ht _ (by decide) e + · exact hTcb t ht _ (by decide) e + | sc2 c hc => exact hSc c hc 72#u64 (by decide) heq + | vs _ he => + cases he with + | asid j hj => exact E.vsLeafA.2.2 p hp i hi (by rw [heq]; exact atAF_asWA hj) + | pool q _ hq _ => exact Set.notMem_empty q hq + | pt q _ hq _ => exact Set.notMem_empty q hq + | high q _ hq _ => exact Set.notMem_empty q hq + | ext _ he => exact extFoot_emptyE he + + +/-- The physical and abstract pool writes preserve the joint relation and the existing CDT. -/ +theorem pool_write_jointA (E : JEntTA st AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) {s2 : S} {σ2 : Fl4SpecA64.State Unit} + (J : JAtZAX (L := L) AR Z s2 a σ2 m) (hks : L.ksb s2 = L.ksb s) (hc : L.cpu s2 = L.cpu s) + {p i word : U64} (hp : p ∈ Z.Pp) (hi : i.val < 512) (w : PoolW st H Z.Cs OS s (peAF p i)) + {f : Fl4SpecA64.AsidPool} (hk : σ2.kheap (specPtr p) = some (.archObj (.asidPool f))) + (hkind : EntKindA Z.S1 Z.Lv Z.X Z.Y word) + (hzero : L.mem s2 (atAF (L.ksb s2) 0#u64) = p → i ≠ 0#u64) + (hunmVs : (tagA word = 0 → word ≠ 0#u64 → + ((∀ j : U64, j.val < 512 → dBitA (L.mem s2 (peAF (untagA word) j)) 0 = false) ∧ + ∀ t, σ2.kheap (specPtr (untagA word)) = some (.archObj (.pageTable t)) → + ∀ j : U64, j.val < 512 → Fl4SpecA64.ptApply t j.bv = .invalidPTE)) ∧ + (tagA word = 2 → + ((∀ j : U64, j.val < 512 → dBitA (L.mem s2 (peAF (wa (untagA word) 4096#u64) j)) 0 = false) ∧ + ∀ l1, σ2.kheap (specPtr (untagA word)) = some (.archObj (.ext (.highVSpace l1))) → + ∀ j : Fin 512, l1 j = .invalidPTE))) + (hunmS2 : tagA word = 1 → + ((∀ j : U64, j.val < 1024 → dBitA (L.mem s2 (peAF (untagA word) j)) 0 = false) ∧ + ∀ es, σ2.kheap (specPtr (untagA word)) = some (.archObj (.ext (.stage2Root es))) → + ∀ j : Fin 1024, es j = .invalidPTE)) : + let σ3 := poolSetA σ2 p (Function.update f (BitVec.ofNat 9 i.val) (poolEntA word)) + let s3 := L.wr s2 (peAF p i) word + JAtZAX (L := L) AR Z s3 a σ3 m ∧ OSKeep st OS s2 s3 ∧ TyKeepA σ2 σ3 := by + dsimp only + have hOff := delPoolWord_offA E hsub hp hi w + have hall := FootA.aligned J.1 (fun c hc' off ho hal => by + rw [nodeAt_ksb hks]; exact bxi_nodes E.ent.bxi c hc' off ho hal) E.scAl E.ap.vcAl + have hmem : ∀ b, Aligned b → b ≠ peAF p i → L.mem (L.wr s2 (peAF p i) word) b = L.mem s2 b := + fun b hb hn => mem_wr_ne' w.al hb hn.symm + have Hm : SameA (L := L) { extZA Z with Pp := ∅ } s2 (L.wr s2 (peAF p i) word) := + ⟨L.ksb_wr _ _ _, L.cpu_wr _ _ _, fun b hb => hmem b (hall b + (FootA.monoA (Z := { extZA Z with Pp := ∅ }) (Z' := Z) rfl rfl + (fun _ e => e) (fun _ e => e) (fun _ e => e) + (fun _ e => (Set.notMem_empty _ e).elim) (fun _ e => (Set.notMem_empty _ e).elim) + (fun _ e => (Set.notMem_empty _ e).elim) (fun _ e => (Set.notMem_empty _ e).elim) + (fun _ e => (Set.notMem_empty _ e).elim) hb)) (fun e => + hOff (e ▸ hb.of_ksA hks.symm hc.symm))⟩ + have hn : ¬ AsW (L.ksb s2) (peAF p i) := by rw [hks]; exact E.vsLeafA.2.2 p hp i hi + have hVR := J.1.vs.of_storePoolA hp hi hk hkind hzero J.1.ext.ypp hn + (fun b hb => hmem b (hall b (.nslot b (.vs b hb)))) (L.mem_wr_same w.al) hunmVs + have pal := Nat.mod_eq_zero_of_dvd (J.1.vs.pal p hp) + have hExt (b : U64) (hb : ExtFootE Z.X Z.Y Z.T2 b) : b ≠ peAF p i := by + obtain ⟨q, j, hj, rfl, hq, htype⟩ := hb.pgE J.1.ext J.1.vs.yal + intro h + have e := (peAF_injA (Nat.mod_eq_zero_of_dvd hq) pal hj hi h).1 + subst q + rcases htype with hq' | hq' + · exact (J.1.ext.pgd p hq').1 (Or.inl hp) + · exact J.1.ext.ypg p hq' (Or.inl hp) + have hkh (q : Fl4SpecA64.ObjRef) (hq : q ≠ specPtr p) : + (poolSetA σ2 p (Function.update f (BitVec.ofNat 9 i.val) (poolEntA word))).kheap q = σ2.kheap q := by + simp [poolSetA, setObjA, Function.update_of_ne hq] + have hnot (q : Fl4SpecA64.ObjRef) {o : Fl4SpecA64.KernelObject} (hq : σ2.kheap q = some o) + (hn' : o ≠ .archObj (.asidPool f)) : q ≠ specPtr p := + fun e => hn' (Option.some.inj (hq.symm.trans (e ▸ hk))) + have hER := J.1.ext.of_storePoolE (Lv := Z.Lv) (i := i) hp hk hkind + (fun b hb => hmem b (hall b (.nslot b (.ext b hb))) (hExt b hb)) hunmS2 + refine ⟨J.of_sameVsA (σ' := poolSetA σ2 p (Function.update f (BitVec.ofNat 9 i.val) (poolEntA word))) + (P0 := ∅) Hm (by constructor <;> first | rfl | exact ⟨rfl, rfl, rfl, rfl⟩) + (fun q r hq => ?_) (fun q hq => ?_) (fun t ht => ?_) (fun c hc' => ?_) (fun v hv => ?_) + rfl rfl (fun q v h => ?_) (fun q es h => ?_) (fun q l1 h => ?_) + (fun _ h1 h2 => ⟨h1, h2⟩) (by rw [L.ksb_wr]; exact hVR) hER, + osKeep_tabWriteA E.ent.os (fun ⟨y, hy, ho⟩ => w.os y hy ho) hmem, + TyKeepA.upd hk (fun _ _ e => by cases e) (fun _ e => by cases e)⟩ + · obtain ⟨cs, he, _, _⟩ := J.1.st.cnode q r hq + exact hkh _ (hnot _ he (by intro e; cases e)) + · obtain ⟨x, he, _⟩ := J.1.st.tcb q hq + exact hkh _ (hnot _ he (by intro e; cases e)) + · obtain ⟨x, he, _, _⟩ := J.1.sch.tcb t ht + exact hkh _ (hnot _ he (by intro e; cases e)) + · obtain ⟨x, n, he, _⟩ := J.1.sch.sc c hc' + exact hkh _ (hnot _ he (by intro e; cases e)) + · obtain ⟨x, he, _, _⟩ := J.2.1.obj v hv + exact hkh _ (hnot _ he (by intro e; cases e)) + · by_cases e : q = specPtr p + · rw [e] at h + simp [poolSetA, setObjA] at h + · rwa [hkh q e] at h + · by_cases e : q = specPtr p + · rw [e] at h + simp [poolSetA, setObjA] at h + · exact J.2.2.1.back q es (by rwa [hkh q e] at h) + · by_cases e : q = specPtr p + · rw [e] at h + simp [poolSetA, setObjA] at h + · exact J.2.2.2.back q l1 (by rwa [hkh q e] at h) + + +theorem JAtZAX.of_vmA (J : JAtZAX (L := L) AR Z s a σ m) {σ' : Fl4SpecA64.State Unit} (h : VmA σ σ') : + JAtZAX (L := L) AR Z s a σ' m := by + obtain ⟨mr, am, vt, nv, rfl⟩ := h + exact J.of_spec { toSpecGlobA := (by constructor <;> first | rfl | exact ⟨rfl, rfl, rfl, rfl⟩), kh := fun _ _ => rfl, ext := fun _ _ e _ => e, + cdt := rfl, orig := rfl } + +theorem JAtZAX.of_hwA (J : JAtZAX (L := L) AR Z s a σ m) {a' : A} {hw : HwRootA} (h : AR a' hw) : + JAtZAX (L := L) AR { Z with hw := hw } s a' σ m := + ⟨{ J.1 with ar := h, rest := { J.1.rest with } }, { J.2.1 with }, { J.2.2.1 with }, { J.2.2.2 with }⟩ + +theorem updKSc_selfA {σ : Fl4SpecA64.State Unit} {q : Fl4SpecA64.ObjRef} {o : Fl4SpecA64.KernelObject} + (h : σ.kheap q = some o) : Sched.updKSc σ q o = σ := by + unfold Sched.updKSc + have : Function.update σ.kheap q (some o) = σ.kheap := by + funext p + by_cases e : p = q + · subst e; simp [h] + · simp [e] + rw [this] + +/-- **`delete_asid` at the joint relation** (module docstring). -/ +theorem delete_asid_jA {ar : machine.Arch A} (hA : P0.ArchTotal ar) (K : ArchKeepsA ar AR) {Clock : A → Fl4SpecA64.MachineState → Prop} (D : DelArchA st ar AR Clock) + (E : JEntTA st AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + {asid root ptr : U64} (h16 : asid.val < 2 ^ 16) (hroot : untagA root = ptr) + {x : Fl4SpecA64.Tcb} (hcur : σ.kheap σ.curThread = some (.tcb x)) + (hsplit : ∀ p ∈ Z.Pp, ActivePoolA (L.mem s) (L.ksb s) p → ∀ w, L.mem s (peAF p (asid &&& 511#u64)) = w → + w ≠ 0#u64 → untagA w = ptr → w = root) : + vspace.delete_asid st ar s a asid root ⦃ r => ∃ σ' hw, + ((), σ') ∈ (Fl4SpecA64.deleteAsid (Z := Unit) (asidOfA asid) (specPtr ptr) σ).1 ∧ + JAtZAX (L := L) AR { Z with hw := hw } r.1 r.2 σ' m ∧ + ((r.1 = s ∧ σ' = σ) ∨ ∃ p ∈ Z.Pp, ActivePoolA (L.mem s) (L.ksb s) p ∧ ∃ f f', σ.kheap (specPtr p) = some (.archObj (.asidPool f)) ∧ + r.1 = L.wr s (peAF p (asid &&& 511#u64)) 0#u64 ∧ + VmA (Sched.updKSc σ (specPtr p) (.archObj (.asidPool + f'))) σ') ⦄ := by + have same : ∀ (hs : ((), σ) ∈ (Fl4SpecA64.deleteAsid (Z := Unit) (asidOfA asid) (specPtr ptr) σ).1), + ∃ σ' hw, ((), σ') ∈ (Fl4SpecA64.deleteAsid (Z := Unit) (asidOfA asid) (specPtr ptr) σ).1 ∧ + JAtZAX (L := L) AR { Z with hw := hw } s a σ' m ∧ + ((s = s ∧ σ' = σ) ∨ ∃ p ∈ Z.Pp, ActivePoolA (L.mem s) (L.ksb s) p ∧ ∃ f f', σ.kheap (specPtr p) = some (.archObj (.asidPool f)) ∧ + s = L.wr s (peAF p (asid &&& 511#u64)) 0#u64 ∧ + VmA (Sched.updKSc σ (specPtr p) (.archObj (.asidPool + f'))) σ') := + fun hs => ⟨σ, Z.hw, hs, E.j, Or.inl ⟨rfl, rfl⟩⟩ + obtain ⟨i, ei, vi⟩ := shrA asid 9#u64 512 (by decide) rfl + have hv127 : (i &&& 127#u64).val = asid.val / 512 := by + rw [and_maskA i 7 _ rfl, vi] + have e : (2 : Nat) ^ 7 = 128 := rfl + rw [e]; omega + have hlt : (i &&& 127#u64).val < 128 := by omega + have ht := E.j.1.vs.table _ hlt + rw [hv127, ← asidHi_eqA h16] at ht + change Fl4SpecA64.poolForAsid (asidOfA asid) σ = _ at ht + unfold vspace.delete_asid + rw [asidLowA, ei, bind_ok, asid_table_eq, bind_ok] + obtain ⟨pool, hpool⟩ : ∃ p, L.mem s (atAF (L.ksb s) (i &&& 127#u64)) = p := ⟨_, rfl⟩ + have hP0 := E.j.1.vs.tmem _ hlt + rw [hpool] at ht hP0 ⊢ + by_cases hp0 : pool = 0#u64 + · rw [if_pos hp0, WP.spec_ok] + rw [hp0, optPtrA_zero] at ht + exact same (deleteAsid_noneA ht) + rw [if_neg hp0] + rw [optPtrA_ne hp0] at ht + have hP : pool ∈ Z.Pp := hP0 hp0 + have hactP : ActivePoolA (L.mem s) (L.ksb s) pool := ⟨hp0, i &&& 127#u64, hlt, hpool⟩ + obtain ⟨f, hk, hf⟩ := E.j.1.vs.pool pool hP + have hlov : (asid &&& 511#u64).val = asid.val % 512 := by + rw [and_maskA asid 9 _ rfl]; rfl + have hlo : (asid &&& 511#u64).val < 512 := by omega + have hbits : Fl4SpecA64.asidLowBitsOf (asidOfA asid) = BitVec.ofNat 9 (asid &&& 511#u64).val := by + apply BitVec.eq_of_toNat_eq + rw [hlov] + simp [Fl4SpecA64.asidLowBitsOf, Fl4SpecA64.ucast, asidOfA, Fl4SpecA64.asidLowLen, Fl4SpecA64.asidLowBits, + Fl4SpecA64.asidRepLen, Fl4SpecA64.asidLen] + have hent := hf hactP _ hlo + rw [← hbits] at hent + simp only [lift, bind_ok, L.read_eq] + change (if L.mem s (peAF pool (asid &&& 511#u64)) = root then _ else _) ⦃ _ => _ ⦄ + by_cases hw : L.mem s (peAF pool (asid &&& 511#u64)) = root + swap + · rw [if_neg hw, WP.spec_ok] + refine same (deleteAsid_otherA ht hk fun he => hw ?_) + rw [hent] at he + unfold poolEntA at he + split at he + · cases he + · rename_i hne + injection he with he; injection he with he + exact hsplit pool hP hactP _ rfl hne (specPtr_inj he) + rw [if_pos hw] + have hflush : ∀ fa : Result A, + (fa = ar.invalidate_stage2_vmid a asid ∨ (∃ y, fa = ar.invalidate_high_vspace a y) ∨ + fa = ar.invalidate_asid a asid) → fa ⦃ a1 => AR a1 Z.hw ⦄ := by + rintro fa (rfl | ⟨y, rfl⟩ | rfl) + · exact K.invS2Vmid a _ asid E.j.1.ar + · exact K.invHiVs a _ y E.j.1.ar + · exact K.invAsid a _ asid E.j.1.ar + refine WP.spec_bind (Pₘ := fun a1 => AR a1 Z.hw) ?_ fun a1 ha1 => ?_ + · split + · exact hflush _ (Or.inl rfl) + · split + · exact hflush _ (Or.inr (Or.inl ⟨_, rfl⟩)) + · exact hflush _ (Or.inr (Or.inr rfl)) + have hW : PoolW st H Z.Cs OS s (peAF pool (asid &&& 511#u64)) := by + have h := E.poolOff (i &&& 127#u64) + rw [asid_table_eq, WP.spec_ok] at h + have e : (i &&& 127#u64) &&& 127#u64 = i &&& 127#u64 := + UScalar.eq_of_val_eq (by rw [and_maskA _ 7 _ rfl]; exact Nat.mod_eq_of_lt hlt) + rw [e, hpool] at h + exact h hp0 asid + by_cases hr0 : root = 0#u64 + · -- the entry already holds 0: FL4 writes 0 again, l4v's `delete_asid` finds another entry + have hmem0 : L.mem s (peAF pool (asid &&& 511#u64)) = 0#u64 := hw.trans hr0 + refine WP.spec_bind (L.write_spec s _ 0#u64) fun s1 e1 => ?_ + subst e1 + refine WP.spec_bind (sched.cur_thread.spec _) fun ct _ => ?_ + refine WP.spec_mono (WP.spec_and (vspace.set_vm_root.same hA _ a1 ct) (D.vmRoot _ a1 _ ct ha1)) ?_ + rintro r ⟨hs, hw', har⟩ + have hall := FootA.aligned E.j.1 (fun c hc' off ho hal => bxi_nodes E.ent.bxi c hc' off ho hal) E.scAl E.ap.vcAl + have J0 : JAtZAX (L := L) AR Z (L.wr s (peAF pool (asid &&& 511#u64)) 0#u64) a σ m := + E.j.of_mem ⟨L.ksb_wr _ _ _, L.cpu_wr _ _ _, fun b hb => by + by_cases e : b = peAF pool (asid &&& 511#u64) + · subst e; rw [L.mem_wr_same hW.al, hmem0] + · exact mem_wr_ne' hW.al (hall b hb) (Ne.symm e)⟩ + refine ⟨σ, hw', deleteAsid_otherA ht hk (fun he => ?_), ?_, + Or.inr ⟨pool, hP, hactP, f, f, hk, hs, ?_⟩⟩ + · rw [hent, hmem0, poolEntA_zero] at he; cases he + · rw [hs]; exact J0.of_hwA har + · rw [updKSc_selfA hk]; exact vmA_refl σ + have hzero : L.mem s (atAF (L.ksb s) 0#u64) = pool → (asid &&& 511#u64) ≠ 0#u64 := by + intro h0 hl + have h := E.j.1.vs.asid0 (by rw [h0]; exact hp0) + rw [h0] at h + rw [hl] at hw + exact hr0 (hw.symm.trans h) + have J := pool_write_jointA E hsub (s2 := s) (σ2 := σ) E.j rfl rfl (p := pool) (i := asid &&& 511#u64) + (word := 0#u64) hP hlo hW hk (Or.inl rfl) hzero + ⟨fun _ h => (h rfl).elim, fun h => by simp [tagA] at h⟩ (fun h => by simp [tagA] at h) + dsimp only at J + obtain ⟨J, -, -⟩ := J + rw [poolEntA_zero, ← hbits] at J + refine WP.spec_bind (L.write_spec s _ 0#u64) fun s1 e1 => ?_ + subst e1 + refine WP.spec_bind (sched.cur_thread.spec _) fun ct _ => ?_ + refine WP.spec_mono (WP.spec_and (vspace.set_vm_root.same hA _ a1 ct) (D.vmRoot _ a1 _ ct ha1)) ?_ + rintro r ⟨hs, hw', har⟩ + obtain ⟨σ', hmem, hvm⟩ := deleteAsid_hitA (pt := specPtr ptr) ht hk + (by rw [hent, hw]; unfold poolEntA; rw [if_neg hr0, hroot]) hcur + refine ⟨σ', hw', hmem, ?_, Or.inr ⟨pool, hP, hactP, f, _, hk, hs, hvm⟩⟩ + rw [hs] + exact (J.of_vmA hvm).of_hwA har + +end Frame + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelAsidPoolA.lean b/proofs/Fl4ProofsA64/P3/DelAsidPoolA.lean new file mode 100644 index 000000000..9df4788d1 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelAsidPoolA.lean @@ -0,0 +1,523 @@ +/- +P3 (AArch64, delete hooks): **the joint relation across clearing one ASID-table word** (`vspace.set_asid_table s i 0`, +the table entry `i` removed on the spec side: `dropPoolA`), and **FL4's `delete_asid_pool` at the joint relation**. + +* `AtOffA`: the ASID-table word `atAF ksb i` is off every non-VSpace footprint word (the nodes, IRQ words, globals, + TCBs, SCs, slots, VCPU words) and off every pool / table / window / Stage-2 word (`OffAsE`, window words + `hYAs`): the placement facts `JAtZAX.of_sameVsA` cannot use, because every footprint contains the ASID words; +* `VsRelA.of_clearA` / `ExtVsRelA.of_clearA`: both relations across the removal (reachability only shrinks); +* **`JAtZAX.of_tabClearA`**: `JAtZAX` at the same ghosts after the write of `0` and `dropPoolA`; +* **`delete_asid_pool_jA`**: nothing happens on both sides, or the installed pool's table entry is cleared (up to + `VmA`) with `JAtZAX` after at some hardware root. + +Hypotheses: `hsub` (slots are in `OS`), `hT` / `hYAs` (VS / Stage-2 / window pages are off the ASID-table block, as +the VS tops). [P] `ArchKeepsA`, `DelArchA`, `P0.ArchTotal`. New module. +-/ +import Fl4ProofsA64.P3.DelAsidA +import Fl4ProofsA64.P3.ScSchedA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section Clear +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- **The ASID-table word `i` is off every other footprint word** (module docstring). -/ +structure AtOffA (Z : XSetsA) (s : S) (i : U64) : Prop where + node : ∀ c ∈ Z.Cs, ∀ o : U64, o.val < 4288 → L.nodeAt s c o ≠ atAF (L.ksb s) i + irq : ∀ k : U64, k.val < 5376 → irqWordA (L.ksb s) k ≠ atAF (L.ksb s) i + glob : ∀ o : U64, o.val < 772352 → wa (L.ksb s) o ≠ atAF (L.ksb s) i + tcb : ∀ t ∈ Z.T, ∀ w : U64, t.val ≤ w.val → w.val < t.val + 4096 → w ≠ atAF (L.ksb s) i + sc : ∀ c ∈ Z.C, ∀ w : U64, c.val ≤ w.val → w.val < c.val + 128 → w ≠ atAF (L.ksb s) i + slot : ∀ y, SlotWA Z.th y → ∀ o : U64, o.val < 32 → wa y o ≠ atAF (L.ksb s) i + vcpu : ∀ v ∈ Z.V, v ≠ atAF (L.ksb s) i ∧ wa v 24#u64 ≠ atAF (L.ksb s) i + pg : ∀ q j : U64, j.val < 512 → (q ∈ S2PgE Z.X Z.T2 ∨ q ∈ VsPtA Z.Pp Z.S1 Z.Y) → peAF q j ≠ atAF (L.ksb s) i + extW : ∀ b, ExtFootE Z.X Z.Y Z.T2 b → b ≠ atAF (L.ksb s) i + +theorem atOffA (E : JEntTA st AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) (hT : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s)) + (hYAs : ∀ y ∈ Z.Y, ∀ j : U64, j.val < 512 → ¬ AsW (L.ksb s) (peAF y j)) {i : U64} (hi : i.val < 128) : + AtOffA (L := L) Z s i := by + have hksFit : (L.ksb s).val + 788736 ≤ 2 ^ 64 := E.j.1.ksFit + have hAtVal := atAF_valA hksFit hi + have hAsI : AsW (L.ksb s) (atAF (L.ksb s) i) := atAF_asWA hi + refine ⟨?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_⟩ + · intro c hc o ho e + obtain ⟨n, _, hn128, hcs⟩ := E.j.1.csN + have hcLt : c.val < 128 := by have := (hcs c).1 hc; omega + have hnv := Fl4ProofsA64.P3.Sched.nodeAt_valSc hksFit hcLt ho (s := s) + have hev := congrArg UScalar.val e + rw [hnv, hAtVal] at hev + omega + · intro k hk e + have hv := irqWordA_val hksFit hk + have hev := congrArg UScalar.val e + rw [hv, hAtVal] at hev + omega + · intro o ho e + have hv : (wa (L.ksb s) o).val = (L.ksb s).val + o.val := by + rw [wa_valP, Nat.mod_eq_of_lt (by omega)] + have hev := congrArg UScalar.val e + rw [hv, hAtVal] at hev + omega + · intro t ht w h1 h2 e + have hht : t ∈ H.tcbs := E.tcbs.symm ▸ ht + have hk := E.ko.1 t .tcb hht (fun _ h => by cases h) + simp [HKind.bits] at hk + have hev := congrArg UScalar.val e + rw [hAtVal] at hev + omega + · intro c hc w h1 h2 e + obtain ⟨z, hz⟩ : c ∈ H.scs := E.scs.symm ▸ hc + have hk := E.ko.1 c (.sc z) hz (fun _ h => by cases h) + have hb7 := E.ent.heap.scmin c z hz + have : 128 ≤ 2 ^ z := by + calc 128 = 2 ^ 7 := by decide + _ ≤ 2 ^ z := Nat.pow_le_pow_right (by omega) hb7 + simp [HKind.bits] at hk + have hev := congrArg UScalar.val e + rw [hAtVal] at hev + omega + · intro y hy o ho e + have hy0 : IsSlotA Z.th y := by + rcases hy with hy | ⟨q, r, hq, idx, hl, rfl⟩ + · exact hy + · have hab : AbsSlotA Z.th (specPtr q, idx) := ⟨q, rfl, Or.inl (by rw [hl]; exact hq)⟩ + exact hab.isSlot E.j.1.place + obtain ⟨q, k, j, hq, hSize, hv, _⟩ := slotW_valA E.j.1.place hy + have hf := E.j.1.place.fit q k hq + have hyw : (wa y o).val = y.val + o.val := by + rw [wa_valP, Nat.mod_eq_of_lt (by omega)] + have hev := congrArg UScalar.val e + exact (E.ap.vs _ (.asid i hi)).1 ⟨y, hsub y hy0, by omega, by omega⟩ + · intro v hv + refine ⟨fun e => ?_, fun e => ?_⟩ + · have hwa0 : wa v 0#u64 = v := UScalar.eq_of_val_eq (by + rw [wa_valP, show (0#u64 : U64).val = 0 from rfl, Nat.add_zero, Nat.mod_eq_of_lt (by simpa using v.hBounds)]) + exact E.vsVc _ (.asid i hi) ⟨v, hv, 0#u64, by decide, by decide, by rw [hwa0]; exact e.symm⟩ + · exact E.vsVc _ (.asid i hi) ⟨v, hv, 24#u64, by decide, by decide, e.symm⟩ + · intro q j hj hq e + exact hT q j hj hq (e ▸ hAsI) + · intro b hb + cases hb with + | s2lo r j hr hj => intro e; exact hT r j hj (Or.inl (Or.inr ⟨r, hr, Or.inl rfl⟩)) (by rw [e]; exact hAsI) + | s2hi r j hr hj => + intro e; exact hT (wa r 4096#u64) j hj (Or.inl (Or.inr ⟨r, hr, Or.inr rfl⟩)) (by rw [e]; exact hAsI) + | s2pt t j ht hj => intro e; exact hT t j hj (Or.inl (Or.inl ht)) (by rw [e]; exact hAsI) + | wl0 y hy => intro e; exact hYAs y hy 511#u64 (by decide) (by rw [e]; exact hAsI) + | wlo y j hy hj => intro e; exact hYAs y hy j (by omega) (by rw [e]; exact hAsI) + +/-- **`VsRelA` across removing the table entry `i`** (module docstring): the entry's word is `0`, the other +footprint words kept; reachability only shrinks. -/ +theorem VsRelA.of_clearA {Pp Tb : Set U64} {Lv : U64 → Nat} {X Y : Set U64} {mem mem' : U64 → U64} {ksb : U64} + {σ : Fl4SpecA64.State Unit} (R : VsRelA Pp Tb Lv X Y mem ksb σ) {i : U64} (hi : i.val < 128) + (hk : ksb.val + 788736 ≤ 2 ^ 64) + (hmem : ∀ b, VsFootA Pp Tb Y ksb b → b ≠ atAF ksb i → mem' b = mem b) + (h0 : mem' (atAF ksb i) = 0#u64) + (hpg : ∀ q j : U64, j.val < 512 → (q ∈ Pp ∨ q ∈ Tb ∨ ∃ y ∈ Y, q = wa y 4096#u64) → + peAF q j ≠ atAF ksb i) : + VsRelA Pp Tb Lv X Y mem' ksb (dropPoolA σ (BitVec.ofNat 7 i.val)) := by + have hneAt : ∀ j : U64, j.val < 128 → j ≠ i → atAF ksb j ≠ atAF ksb i := by + intro j hj hne e + have hv := congrArg UScalar.val e + rw [atAF_valA hk hj, atAF_valA hk hi] at hv + exact hne (UScalar.eq_of_val_eq (by omega)) + have hat : ∀ j : U64, j.val < 128 → j ≠ i → mem' (atAF ksb j) = mem (atAF ksb j) := + fun j hj hne => hmem _ (.asid j hj) (hneAt j hj hne) + have hpl : ∀ p ∈ Pp, ∀ lo : U64, lo.val < 512 → mem' (peAF p lo) = mem (peAF p lo) := + fun p hp lo hlo => hmem _ (.pool p lo hp hlo) (hpg p lo hlo (Or.inl hp)) + have hActP : ∀ p, ActivePoolA mem' ksb p → ActivePoolA mem ksb p := by + rintro p ⟨hpne, hj, hhj, htab⟩ + by_cases he : hj = i + · rw [he, h0] at htab; exact absurd htab.symm hpne + · exact ⟨hpne, hj, hhj, by rw [← hat hj hhj he]; exact htab⟩ + have hActR : ∀ asid raw, ActiveAsidRootA mem' ksb Pp asid raw → ActiveAsidRootA mem ksb Pp asid raw := by + rintro asid raw ⟨hj, lo, hhj, hlo, ha', pool, hp, hpne, htab, hw, hrne⟩ + by_cases he : hj = i + · rw [he, h0] at htab; exact absurd htab.symm hpne + · exact ⟨hj, lo, hhj, hlo, ha', pool, hp, hpne, by rw [← hat hj hhj he]; exact htab, + by rw [← hpl pool hp lo hlo]; exact hw, hrne⟩ + have hActH : ∀ y, ActiveHighRootA mem' ksb Pp y → ActiveHighRootA mem ksb Pp y := by + rintro y ⟨asid, raw, ha, htag, hy⟩ + exact ⟨asid, raw, hActR asid raw ha, htag, hy⟩ + have hPath : ∀ {tag sh q}, VsPathA Pp Tb Y mem' ksb tag sh q → VsPathA Pp Tb Y mem ksb tag sh q := by + intro tag sh q hp + induction hp with + | native ha htag htb => exact .native (hActR _ _ ha) htag htb + | high ha htag hy => exact .high (hActR _ _ ha) htag hy + | @child tag t j sh _ htY h12 hj h3 ih => + have hpe : mem' (peAF t j) = mem (peAF t j) := by + rcases htY with ht | ⟨y, hy, rfl⟩ + · exact hmem _ (.pt t j ht hj) (hpg t j hj (Or.inr (Or.inl ht))) + · exact hmem _ (.high y j hy hj) (hpg _ j hj (Or.inr (Or.inr ⟨y, hy, rfl⟩))) + rw [hpe] at h3 ⊢ + exact .child ih htY h12 hj h3 + have hReach : ∀ q, VsReachA Pp Tb Y mem' ksb q → VsReachA Pp Tb Y mem ksb q := by + rintro q ⟨tag, sh, htag, hp⟩ + exact ⟨tag, sh, htag, hPath hp⟩ + refine ⟨?_, ?_, ?_, ?_, ?_, ?_, ?_, R.lv, R.al, R.pal, R.yal, R.ytb⟩ + · intro hj hhj + by_cases he : hj = i + · rw [he] + simp only [dropPoolA, Function.update_self, h0, optPtrA_zero] + · have hneBv : BitVec.ofNat 7 hj.val ≠ BitVec.ofNat 7 i.val := fun e => he (UScalar.eq_of_val_eq (by + have h1 := congrArg BitVec.toNat e + rw [BitVec.toNat_ofNat, BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega), Nat.mod_eq_of_lt (by omega)] at h1 + exact h1)) + simp only [dropPoolA, Function.update_of_ne hneBv] + rw [hat hj hhj he] + exact R.table hj hhj + · intro hj hhj hne + by_cases he : hj = i + · rw [he] at hne; exact absurd h0 hne + · rw [hat hj hhj he] at hne ⊢ + exact R.tmem hj hhj hne + · intro p hp + obtain ⟨f, hkf, hf⟩ := R.pool p hp + exact ⟨f, hkf, fun ha lo hlo => by rw [hpl p hp lo hlo]; exact hf (hActP p ha) lo hlo⟩ + · intro p hp ha lo hlo + rw [hpl p hp lo hlo] + exact R.kind p hp (hActP p ha) lo hlo + · intro hne + by_cases h0i : i = 0#u64 + · rw [h0i] at h0; exact absurd h0 hne + · have hne0 : (0#u64 : U64) ≠ i := Ne.symm h0i + rw [hat 0#u64 (by decide) hne0] at hne ⊢ + have hp0 := R.tmem 0#u64 (by decide) hne + rw [hpl _ hp0 0#u64 (by decide)] + exact R.asid0 hne + · intro p hp + obtain ⟨t, hkt, hty, hpe⟩ := R.pt p hp + exact ⟨t, hkt, hty, fun hr j hj => by + rw [hmem _ (.pt p j hp hj) (hpg p j hj (Or.inr (Or.inl hp)))] + exact hpe (hReach p hr) j hj⟩ + · intro y hy + obtain ⟨l1, hkl, hpe⟩ := R.high y hy + exact ⟨l1, hkl, fun ha j hj => by + rw [hmem _ (.high y j hy hj) (hpg _ j hj (Or.inr (Or.inr ⟨y, hy, rfl⟩)))] + exact hpe (hActH y ha) j hj⟩ + +/-- **`ExtVsRelA` across removing a table entry**: the Stage-2 words kept; reachability only shrinks. -/ +theorem ExtVsRelA.of_clearA {Pp Tb X Y T2 : Set U64} {Sh2 : U64 → Nat} {mem mem' : U64 → U64} + {σ : Fl4SpecA64.State Unit} (E : ExtVsRelA Pp Tb X Y T2 Sh2 mem σ) (hj : BitVec 7) + (hmem : ∀ b, ExtFootE X Y T2 b → mem' b = mem b) : + ExtVsRelA Pp Tb X Y T2 Sh2 mem' (dropPoolA σ hj) := by + have E1 := E.of_mem hmem + have hact : ∀ r, ActiveS2RootA Pp X (dropPoolA σ hj) r → ActiveS2RootA Pp X σ r := by + rintro r ⟨hr, asid, p, f, ha0, hp, htab, hkh, hf⟩ + by_cases he : Fl4SpecA64.asidHighBitsOf asid = hj + · simp only [dropPoolA, he, Function.update_self] at htab + cases htab + · simp only [dropPoolA, Function.update_of_ne he] at htab + exact ⟨hr, asid, p, f, ha0, hp, htab, hkh, hf⟩ + have hreach : ∀ q, S2ReachA Pp X T2 Sh2 mem' (dropPoolA σ hj) q → S2ReachA Pp X T2 Sh2 mem' σ q := by + intro q hq + induction hq with + | root ha => exact .root (hact _ ha) + | stepRoot ha hi h3 ht2 => exact .stepRoot (hact _ ha) hi h3 ht2 + | stepPt _ ht h12 hi h3 ht2 ih => exact .stepPt ih ht h12 hi h3 ht2 + refine ⟨fun r hr => ?_, fun t ht => ?_, E1.tsh, E1.wl0, E1.wlo, E1.xal, E1.tal, E1.pgd, E1.t2x, E1.ypg, E1.ypp⟩ + · obtain ⟨es, hk, he⟩ := E1.s2root r hr + exact ⟨es, hk, fun ha => he (hact r ha)⟩ + · obtain ⟨pt, hk, hty, he⟩ := E1.s2pt t ht + exact ⟨pt, hk, hty, fun hr => he (hreach t hr)⟩ + +/-- **`JAtZAX` across clearing the ASID-table word `i`** (module docstring): the word becomes `0`, the spec's +table entry is removed (`dropPoolA`), the ghosts are unchanged. -/ +theorem JAtZAX.of_tabClearA (E : JEntTA st AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) (hT : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s)) + (hYAs : ∀ y ∈ Z.Y, ∀ j : U64, j.val < 512 → ¬ AsW (L.ksb s) (peAF y j)) {i : U64} (hi : i.val < 128) : + JAtZAX (L := L) AR Z (L.wr s (atAF (L.ksb s) i) 0#u64) a (dropPoolA σ (BitVec.ofNat 7 i.val)) m := by + have A := atOffA E hsub hT hYAs hi + have J := E.j + obtain ⟨J0, V, X0, Y0⟩ := J + have hal := FootA.aligned J0 (fun c hc' off ho hal => bxi_nodes E.ent.bxi c hc' off ho hal) E.scAl E.ap.vcAl + have hAtAl : Aligned (atAF (L.ksb s) i) := hal _ (.nslot _ (.vs _ (.asid i hi))) + set s' := L.wr s (atAF (L.ksb s) i) 0#u64 with hs' + have hks : L.ksb s' = L.ksb s := L.ksb_wr _ _ _ + have hcpu : L.cpu s' = L.cpu s := L.cpu_wr _ _ _ + have hksFit : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J0.ksFit + have hAt0 : L.mem s' (atAF (L.ksb s) i) = 0#u64 := L.mem_wr_same hAtAl + have hfr : ∀ b, FootA (L := L) Z s b → b ≠ atAF (L.ksb s) i → L.mem s' b = L.mem s b := + fun b hb hne => mem_wr_ne' hAtAl (hal b hb) hne.symm + have hfn : ∀ b, FootNA (L := L) Z s b → b ≠ atAF (L.ksb s) i → L.mem s' b = L.mem s b := + fun b hb hne => hfr b (.nslot b hb) hne + have hwa0 : ∀ y : U64, wa y 0#u64 = y := fun y => + UScalar.eq_of_val_eq (by + rw [wa_valP, show (0#u64 : U64).val = 0 from rfl, Nat.add_zero, Nat.mod_eq_of_lt (by simpa using y.hBounds)]) + have hSl : ∀ y, SlotWA Z.th y → L.mem s' y = L.mem s y ∧ L.mem s' (wa y 8#u64) = L.mem s (wa y 8#u64) ∧ + L.mem s' (wa y 16#u64) = L.mem s (wa y 16#u64) ∧ L.mem s' (wa y 24#u64) = L.mem s (wa y 24#u64) := by + intro y hy + refine ⟨hfr y (.slot y y hy (Or.inl rfl)) ?_, hfr _ (.slot y _ hy (Or.inr (Or.inl rfl))) (A.slot y hy 8#u64 (by decide)), + hfr _ (.slot y _ hy (Or.inr (Or.inr (Or.inl rfl)))) (A.slot y hy 16#u64 (by decide)), + hfr _ (.slot y _ hy (Or.inr (Or.inr (Or.inr rfl)))) (A.slot y hy 24#u64 (by decide))⟩ + have := A.slot y hy 0#u64 (by decide) + rwa [hwa0] at this + have hcap : ∀ y, SlotWA Z.th y → capAtA (L.mem s') y = capAtA (L.mem s) y := fun y hy => by + obtain ⟨e1, e2, -, -⟩ := hSl y hy + unfold capAtA; rw [e1, e2] + have hrev : ∀ y, SlotWA Z.th y → revA (L.mem s') y = revA (L.mem s) y := fun y hy => by + obtain ⟨-, -, -, e4⟩ := hSl y hy + unfold revA; rw [e4] + -- the node, current-core, TCB and SC words + have hnode : ∀ c ∈ Z.Cs, ∀ o : U64, + (o = layout.node.CUR_THREAD ∨ o = layout.node.IDLE_THREAD ∨ o = layout.node.CUR_SC ∨ o = layout.node.IDLE_SC) → + L.mem s' (L.nodeAt s' c o) = L.mem s (L.nodeAt s c o) := fun c hc o ho => by + rw [nodeAt_ksb hks] + have hoLt : o.val < 4288 := by + rcases ho with rfl | rfl | rfl | rfl <;> + simp [layout.node.CUR_THREAD, layout.node.IDLE_THREAD, layout.node.CUR_SC, layout.node.IDLE_SC] + exact hfn _ (.node c _ hc (by rcases ho with rfl | rfl | rfl | rfl <;> simp)) (A.node c hc o hoLt) + have hcur : ∀ o : U64, + (o = layout.node.SCHED_ACTION ∨ o = layout.node.CUR_TIME ∨ o = layout.node.CONSUMED ∨ o = layout.node.REPROGRAM) → + L.mem s' (L.nodeAt s' (L.cpu s') o) = L.mem s (L.nodeAt s (L.cpu s) o) := fun o ho => by + rw [hcpu, nodeAt_ksb hks] + have hoLt : o.val < 4288 := by + rcases ho with rfl | rfl | rfl | rfl <;> + simp [layout.node.SCHED_ACTION, layout.node.CUR_TIME, layout.node.CONSUMED, layout.node.REPROGRAM] + have hfnA : FootNA (L := L) Z s (L.nodeAt s (L.cpu s) o) := by + rcases ho with rfl | rfl | rfl | rfl + · exact .cur _ (Or.inl rfl) + · exact .cur _ (Or.inr rfl) + · exact .cur2 _ (Or.inl rfl) + · exact .cur2 _ (Or.inr rfl) + exact hfn _ hfnA (A.node _ J0.sch.cpuCs o hoLt) + have htcb : ∀ t ∈ Z.T, ∀ w : U64, (w = stA t ∨ w = scA t ∨ w = affA t ∨ w = wa t 3664#u64 ∨ w = runVcpuA t ∨ + w = prioA t ∨ w = domA t) → L.mem s' w = L.mem s w := fun t ht w hw => by + have hfit := E.j.1.place.fit t .tcb ((E.j.1.thT t).2 ht) + have hwb : t.val ≤ w.val ∧ w.val < t.val + 4096 := by + simp only [ObjKindA.size] at hfit + rcases hw with rfl | rfl | rfl | rfl | rfl | rfl | rfl + · have hstLt : layout.tcb.STATE.val < 4096 := by simp [layout.tcb.STATE] + rw [stA, wa_valP, Nat.mod_eq_of_lt (by omega)]; omega + · rw [scA, wa_valP, show (3464#u64 : U64).val = 3464 from rfl, Nat.mod_eq_of_lt (by omega)]; omega + · rw [affA, wa_valP, show (3520#u64 : U64).val = 3520 from rfl, Nat.mod_eq_of_lt (by omega)]; omega + · rw [wa_valP, show (3664#u64 : U64).val = 3664 from rfl, Nat.mod_eq_of_lt (by omega)]; omega + · unfold runVcpuA; rw [wa_valP, show (3672#u64 : U64).val = 3672 from rfl, Nat.mod_eq_of_lt (by omega)]; omega + · rw [prioA, wa_valP, show (3448#u64 : U64).val = 3448 from rfl, Nat.mod_eq_of_lt (by omega)]; omega + · rw [domA, wa_valP, show (3432#u64 : U64).val = 3432 from rfl, Nat.mod_eq_of_lt (by omega)]; omega + have hfnA : FootNA (L := L) Z s w := by + rcases hw with rfl | rfl | rfl | rfl | rfl | rfl | rfl + · exact .tcb t _ ht (Or.inl rfl) + · exact .tcb t _ ht (Or.inr (Or.inl rfl)) + · exact .tcb t _ ht (Or.inr (Or.inr (Or.inl rfl))) + · exact .tcb t _ ht (Or.inr (Or.inr (Or.inr (Or.inl rfl)))) + · exact .tcb t _ ht (Or.inr (Or.inr (Or.inr (Or.inr rfl)))) + · exact .tcb2 t _ ht (Or.inl rfl) + · exact .tcb2 t _ ht (Or.inr rfl) + exact hfn w hfnA (A.tcb t ht w hwb.1 hwb.2) + have hsc : ∀ c ∈ Z.C, L.mem s' (wa c 24#u64) = L.mem s (wa c 24#u64) ∧ + L.mem s' (wa c 72#u64) = L.mem s (wa c 72#u64) := fun c hc => by + obtain ⟨z, hz⟩ : c ∈ H.scs := E.scs.symm ▸ hc + have hfit := E.ent.heap.fits c (.sc z) hz + have hb7 := E.ent.heap.scmin c z hz + have h128 : 128 ≤ 2 ^ z := by + calc 128 = 2 ^ 7 := by decide + _ ≤ 2 ^ z := Nat.pow_le_pow_right (by omega) hb7 + simp only [HKind.bits] at hfit + have hw24 : (wa c 24#u64).val = c.val + 24 := by + rw [wa_valP, show (24#u64 : U64).val = 24 from rfl, Nat.mod_eq_of_lt (by omega)] + have hw72 : (wa c 72#u64).val = c.val + 72 := by + rw [wa_valP, show (72#u64 : U64).val = 72 from rfl, Nat.mod_eq_of_lt (by omega)] + exact ⟨hfn _ (.sc c hc) (A.sc c hc _ (by omega) (by omega)), + hfn _ (.sc2 c hc) (A.sc c hc _ (by omega) (by omega))⟩ + have hSch : SchedRelA (L := L) Z.T Z.C Z.Cs Z.curOf Z.curScOf s' (dropPoolA σ (BitVec.ofNat 7 i.val)) m := + { core := by rw [hcpu]; exact J0.sch.core + cpuCs := by rw [hcpu]; exact J0.sch.cpuCs + curOfW := fun c hc => by rw [hnode c hc _ (Or.inl rfl)]; exact J0.sch.curOfW c hc + curScOfW := fun c hc => by rw [hnode c hc _ (Or.inr (Or.inr (Or.inl rfl)))]; exact J0.sch.curScOfW c hc + cur := J0.sch.cur + csc := J0.sch.csc + idle := by + unfold LawfulStore.idleW + rw [hcpu, hnode _ J0.sch.cpuCs _ (Or.inr (Or.inl rfl))] + exact J0.sch.idle + idleOf := fun c hc => by rw [hnode c hc _ (Or.inr (Or.inl rfl))]; exact J0.sch.idleOf c hc + idleScOf := fun c hc => by rw [hnode c hc _ (Or.inr (Or.inr (Or.inr rfl)))]; exact J0.sch.idleScOf c hc + act := by unfold LawfulStore.actionW; rw [hcur _ (Or.inl rfl)]; exact J0.sch.act + time := by unfold LawfulStore.curTime LawfulStore.nodeA; rw [hcur _ (Or.inr (Or.inl rfl))]; exact J0.sch.time + aff := fun t ht => by rw [htcb t ht _ (Or.inr (Or.inr (Or.inl rfl)))]; exact J0.sch.aff t ht + affT := J0.sch.affT + tcb := fun t ht => by + rw [htcb t ht _ (Or.inl rfl), htcb t ht _ (Or.inr (Or.inl rfl))] + exact J0.sch.tcb t ht + sc := fun c hc => by rw [(hsc c hc).1]; exact J0.sch.sc c hc + scOff := J0.sch.scOff } + have hIrq : IrqRelA (L.mem s') (L.ksb s') (dropPoolA σ (BitVec.ofNat 7 i.val)) := + { st := fun k hk => by + rw [hks, hfn _ (.irq k hk) (A.irq k hk)] + exact J0.irq.st k hk + node := fun k hk => by rw [hks]; exact J0.irq.node k hk + nodeHi := fun k hk => by rw [hks]; exact J0.irq.nodeHi k hk } + have hRest : SchedRestA (L := L) Z s' (dropPoolA σ (BitVec.ofNat 7 i.val)) m := + { rq := by rw [hcpu]; exact J0.rest.rq + rqDom := J0.rest.rqDom + rel := by rw [hcpu]; exact J0.rest.rel + consumed := by rw [hcur _ (Or.inr (Or.inr (Or.inl rfl)))]; exact J0.rest.consumed + repr := by rw [hcur _ (Or.inr (Or.inr (Or.inr rfl)))]; exact J0.rest.repr + curDom := by + rw [hks, hfn _ (.glob _ (Or.inl rfl)) (A.glob 8#u64 (by decide))] + exact J0.rest.curDom + domTime := by + rw [hks, hfn _ (.glob _ (Or.inr rfl)) (A.glob 16#u64 (by decide))] + exact J0.rest.domTime + prio := fun t ht => by + rw [htcb t ht _ (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl rfl)))))), + htcb t ht _ (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr rfl))))))] + exact J0.rest.prio t ht + scMem := fun t ht => by rw [htcb t ht _ (Or.inr (Or.inl rfl))]; exact J0.rest.scMem t ht + refillMax := fun c hc => by rw [(hsc c hc).2]; exact J0.rest.refillMax c hc } + have hSt : StateRelA Z.th (L.mem s') (dropPoolA σ (BitVec.ofNat 7 i.val)) := by + refine ⟨fun p r hp => ?_, fun p hp => ?_⟩ + · obtain ⟨cs, h1, h2, h3⟩ := J0.st.cnode p r hp + refine ⟨cs, h1, h2, fun idx hl => ?_⟩ + obtain ⟨ac, e, hr⟩ := h3 idx hl + exact ⟨ac, e, by rw [hcap _ (Or.inr ⟨p, r, hp, idx, hl, rfl⟩)]; exact hr⟩ + · obtain ⟨x, h1, h2⟩ := J0.st.tcb p hp + refine ⟨x, h1, fun j hj => ?_⟩ + obtain ⟨ac, e, hr⟩ := h2 j hj + exact ⟨ac, e, by rw [hcap _ (Or.inl ⟨p, Or.inr ⟨hp, j, hj, rfl⟩⟩)]; exact hr⟩ + have hCdt : CdtRelA st Z.th s' (dropPoolA σ (BitVec.ofNat 7 i.val)) Z.G := + CdtRelA.mkA (treeA_of_mem J0.cdt.tree fun y hy => hSl y (Or.inl hy)) J0.cdt.cdt J0.cdt.cdt_slot + (fun sl hs => by rw [hrev _ (absSlot_w hs)]; exact J0.cdt.orig sl hs) J0.cdt.par_slot J0.cdt.po + have hVs : VsRelA Z.Pp Z.S1 Z.Lv Z.X Z.Y (L.mem s') (L.ksb s') (dropPoolA σ (BitVec.ofNat 7 i.val)) := by + rw [hks] + exact J0.vs.of_clearA hi hksFit (fun b hb hne => hfr b (.nslot b (.vs b hb)) hne) hAt0 + (fun q j hj hq => A.pg q j hj (Or.inr hq)) + have hExt : ExtVsRelA Z.Pp Z.S1 Z.X Z.Y Z.T2 Z.Sh2 (L.mem s') (dropPoolA σ (BitVec.ofNat 7 i.val)) := + J0.ext.of_clearA _ (fun b hb => hfn b (.ext b hb) (A.extW b hb)) + have J' : JAtZA (L := L) AR Z s' a (dropPoolA σ (BitVec.ofNat 7 i.val)) m := + { st := hSt + cdt := hCdt + sch := hSch + ar := J0.ar + place := by rw [hks]; exact J0.place + irq := hIrq + rest := hRest + thT := J0.thT + csN := J0.csN + idleT := fun c hc => by rw [hnode c hc _ (Or.inr (Or.inl rfl))]; exact J0.idleT c hc + idleC := fun c hc => by rw [hnode c hc _ (Or.inr (Or.inr (Or.inr rfl)))]; exact J0.idleC c hc + curT := fun c hc => by rw [hnode c hc _ (Or.inl rfl)]; exact J0.curT c hc + cscC := fun c hc => by rw [hnode c hc _ (Or.inr (Or.inr (Or.inl rfl)))]; exact J0.cscC c hc + ksFit := by rw [hks]; exact J0.ksFit + scOffD := J0.scOffD + cv := fun y hy => by rw [hcap y (Or.inl hy)]; exact J0.cv y hy + pml := fun y hy q mp asid e => by + rw [hcap y (Or.inl hy)] at e + exact J0.pml y hy q mp asid e + vs := hVs + ext := hExt } + refine ⟨J', ⟨fun v hv' => ?_, fun q x e => V.back q x e, fun t ht => ?_, + fun y hy q e => V.cap y hy q (by rw [← hcap y (Or.inl hy)]; exact e)⟩, + ⟨fun r hr => X0.obj r hr, fun q es e => X0.back q es e, + fun y hy q l mp asid va e => X0.cap y hy q l mp asid va (by rw [← hcap y (Or.inl hy)]; exact e)⟩, + ⟨fun r hr => Y0.obj r hr, fun q l1 e => Y0.back q l1 e, + fun y hy q mp asid e => Y0.cap y hy q mp asid (by rw [← hcap y (Or.inl hy)]; exact e)⟩⟩ + · obtain ⟨x, e, h1, h2⟩ := V.obj v hv' + obtain ⟨n0, n24⟩ := A.vcpu v hv' + rw [hfn v (.vcpu v v hv' (Or.inl rfl)) n0, hfn _ (.vcpu v (wa v 24#u64) hv' (Or.inr rfl)) n24] + exact ⟨x, e, h1, h2⟩ + · obtain ⟨x, e, h1, h2⟩ := V.tcb t ht + rw [htcb t ht _ (Or.inr (Or.inr (Or.inr (Or.inl rfl)))), htcb t ht _ (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl rfl)))))] + exact ⟨x, e, h1, h2⟩ + +/-- **The 512-entry flush loop keeps the arch relation** (`ArchKeepsA`; arch-only). -/ +theorem delPool_loopA {ar : machine.Arch A} (K : ArchKeepsA ar AR) (hw : HwRootA) (s : S) (base pool : U64) + (a0 : A) (h0 : AR a0 hw) : + vspace.delete_asid_pool_loop st ar s a0 base pool 0#u64 ⦃ a1 => AR a1 hw ⦄ := by + unfold vspace.delete_asid_pool_loop + apply loop.spec_decr_nat (measure := fun x : A × U64 => 512 - x.2.val) + (inv := fun x : A × U64 => AR x.1 hw ∧ x.2.val ≤ 512) + · rintro ⟨a1, i⟩ ⟨hA, hi⟩ + unfold vspace.delete_asid_pool_loop.body + dsimp only + by_cases hlt : i < 512#u64 + · rw [if_pos hlt] + have hiLt : i.val < 512 := hlt + have hm : (i * 8#u64) ⦃ i1 => i1.val = 8 * i.val ⦄ := by step as ⟨i1, h⟩; omega + obtain ⟨i1, hi1, hi1v⟩ := spec_ex_fd hm + rw [hi1, bind_ok] + simp only [lift, bind_ok, L.read_eq] + refine WP.spec_bind (Pₘ := fun a' => AR a' hw) ?_ fun a' ha' => ?_ + · split + · exact K.invS2Vmid a1 hw _ hA + · split + · exact K.invHiVs a1 hw _ hA + · split + · exact K.invAsid a1 hw _ hA + · exact (WP.spec_ok _).2 hA + · have hinc : (i + 1#u64) ⦃ i1 => i1.val = i.val + 1 ⦄ := by step as ⟨i1, hi1⟩; omega + obtain ⟨i1, hi1Ok, hi1Val⟩ := spec_ex_fd hinc + rw [hi1Ok, bind_ok, WP.spec_ok] + refine ⟨⟨ha', ?_⟩, ?_⟩ <;> (dsimp only; omega) + · rw [if_neg hlt, WP.spec_ok] + exact hA + · exact ⟨h0, by show (0#u64 : U64).val ≤ 512; decide⟩ + +/-- **`delete_asid_pool` at the joint relation** (module docstring). -/ +theorem delete_asid_pool_jA {ar : machine.Arch A} (hA : P0.ArchTotal ar) (K : ArchKeepsA ar AR) + {Clock : A → Fl4SpecA64.MachineState → Prop} (D : DelArchA st ar AR Clock) (E : JEntTA st AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) (hT : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s)) + (hYAs : ∀ y ∈ Z.Y, ∀ j : U64, j.val < 512 → ¬ AsW (L.ksb s) (peAF y j)) + {base pool : U64} (h16 : base.val < 2 ^ 16) (hb0 : base.val % 512 = 0) (hpool0 : pool ≠ 0#u64) + (hin : ∀ i : U64, i.val < 128 → base.val = i.val * 512 → L.mem s (atAF (L.ksb s) i) = pool) + {x : Fl4SpecA64.Tcb} (hcur : σ.kheap σ.curThread = some (.tcb x)) : + vspace.delete_asid_pool st ar s a base pool ⦃ r => ∃ σ' hw, + ((), σ') ∈ (Fl4SpecA64.deleteAsidPool (Z := Unit) (asidOfA base) (specPtr pool) σ).1 ∧ + JAtZAX (L := L) AR { Z with hw := hw } r.1 r.2 σ' m ∧ + (∃ i : U64, i.val < 128 ∧ base.val = i.val * 512 ∧ pool ∈ Z.Pp ∧ + r.1 = L.wr s (atAF (L.ksb s) i) 0#u64 ∧ + VmA (dropPoolA σ (BitVec.ofNat 7 i.val)) σ') ⦄ := by + obtain ⟨i, ei, vi⟩ := shrA base 9#u64 512 (by decide) rfl + have hlt : i.val < 128 := by omega + have hiand : i &&& 127#u64 = i := + UScalar.eq_of_val_eq (by rw [and_maskA i 7 _ rfl]; exact Nat.mod_eq_of_lt hlt) + have hhi : Fl4SpecA64.asidHighBitsOf (asidOfA base) = BitVec.ofNat 7 i.val := by + rw [vi]; exact asidHi_eqA h16 + have hlow : Fl4SpecA64.asidLowBitsOf (asidOfA base) = 0 := by + apply BitVec.eq_of_toNat_eq + simp [Fl4SpecA64.asidLowBitsOf, Fl4SpecA64.ucast, asidOfA, Fl4SpecA64.asidLowLen, Fl4SpecA64.asidLowBits, + Fl4SpecA64.asidRepLen, Fl4SpecA64.asidLen] + omega + have ht := E.j.1.vs.table i hlt + unfold vspace.delete_asid_pool + rw [asidLowA, ei, bind_ok, asid_table_eq, hiand, bind_ok] + by_cases hne : L.mem s (atAF (L.ksb s) i) = pool + swap + · exact absurd (hin i hlt (by omega)) hne + have hP : pool ∈ Z.Pp := hne ▸ E.j.1.vs.tmem i hlt (by rw [hne]; exact hpool0) + have hspT : σ.archState.armAsidTable (Fl4SpecA64.asidHighBitsOf (asidOfA base)) = some (specPtr pool) := by + rw [hhi, ht, hne, optPtrA_ne hpool0] + obtain ⟨f, hk, -⟩ := E.j.1.vs.pool pool hP + rw [if_neg (by rw [bne_iff_ne]; exact not_not.2 hne)] + refine WP.spec_bind (Pₘ := fun a1 => AR a1 Z.hw) (delPool_loopA K Z.hw s base pool a E.j.1.ar) fun a1 ha1 => ?_ + rw [bind_ok, vspace.set_asid_table.eq, bind_ok] + have hAtEq : wa (L.ksb s) (AtOff i) = atAF (L.ksb s) i := by + unfold AtOff atAF + rw [hiand] + rw [hAtEq] + have J := JAtZAX.of_tabClearA E hsub hT hYAs hlt + refine WP.spec_bind (sched.cur_thread.spec _) fun ct _ => ?_ + refine WP.spec_mono (WP.spec_and (vspace.set_vm_root.same hA _ a1 ct) (D.vmRoot _ a1 _ ct ha1)) ?_ + rintro r ⟨hs, hw', har⟩ + obtain ⟨σ', hmem, hvm⟩ := deleteAsidPool_hitA hlow hspT hk hcur + rw [hhi] at hvm + have hbi : base.val = i.val * 512 := by omega + refine ⟨σ', hw', hmem, ?_, i, hlt, hbi, hP, hs, hvm⟩ + rw [hs] + exact (J.of_vmA hvm).of_hwA har + +end Clear + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelAsidSpecA.lean b/proofs/Fl4ProofsA64/P3/DelAsidSpecA.lean new file mode 100644 index 000000000..9708ade81 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelAsidSpecA.lean @@ -0,0 +1,332 @@ +/- +P3 (AArch64, delete hooks): **spec outcomes of l4v `delete_asid` and `delete_asid_pool`**. + +`set_vm_root`, the TLB invalidations and the VMID bookkeeping change only the machine's opaque part and the three VMID +fields of the arch state (**`VmA`**): the heap, the ASID table and the user memory are kept. From that: + +* **`setVmRoot_memA`**: `set_vm_root t`, `t` a TCB, has a `VmA` outcome; +* **`deleteAsid_noneA`** / **`deleteAsid_otherA`** / **`deleteAsid_hitA`**: no pool for the ASID, or another entry: + nothing; the root's own entry: the entry is removed from the pool (up to `VmA`); +* **`deleteAsidPool_otherA`** / **`deleteAsidPool_hitA`**: another pool installed: nothing; the pool installed: its + ASID-table entry is removed (up to `VmA`). + +Only outcomes (membership) are stated: the delete hooks ask for one spec outcome. No named premise. New module. +-/ +import Fl4ProofsA64.P3.VsUnmapA +import Fl4ProofsA64.P3.EpBufA +import Fl4ProofsA64.P3.SpecFrA +import Fl4ProofsA64.P3.ScSpecA +import Fl4ProofsA64.P3.VsFindA + +namespace Fl4ProofsA64.P3 +open Fl4Spec Fl4SpecA64 Fl4ProofsA64.P3.Sched + +noncomputable section + +/-- Only the machine's opaque part and the VMID bookkeeping differ (module docstring). -/ +def VmA (σ σ' : State Unit) : Prop := + ∃ mr am vt nv, σ' = { σ with + machineState := { σ.machineState with machineStateRest := mr } + archState := { σ.archState with armAsidMap := am, armVmidTable := vt, armNextVmid := nv } } + +theorem vmA_refl (σ : State Unit) : VmA σ σ := ⟨_, _, _, _, rfl⟩ + +theorem vmA_trans : ∀ a b c : State Unit, VmA a b → VmA b c → VmA a c := by + rintro a b c ⟨m1, a1, v1, n1, rfl⟩ ⟨m2, a2, v2, n2, rfl⟩ + exact ⟨m2, a2, v2, n2, rfl⟩ + +theorem VmA.kheap {σ σ' : State Unit} (h : VmA σ σ') : σ'.kheap = σ.kheap := by + obtain ⟨_, _, _, _, rfl⟩ := h; rfl + +theorem VmA.asidTable {σ σ' : State Unit} (h : VmA σ σ') : + σ'.archState.armAsidTable = σ.archState.armAsidTable := by + obtain ⟨_, _, _, _, rfl⟩ := h; rfl + +theorem VmA.curThread {σ σ' : State Unit} (h : VmA σ σ') : σ'.curThread = σ.curThread := by + obtain ⟨_, _, _, _, rfl⟩ := h; rfl + +/-- `machine_op_lift` changes only the opaque part of the machine state. -/ +theorem machineOpLift_restA (g : MachineRestMonad Unit) : + NdOkA (fun ms ms' : MachineState => ∃ mr, ms' = { ms with machineStateRest := mr }) (machineOpLift g) := by + intro s + unfold machineOpLift machineRestLift + have ht : ∀ a b c : MachineState, (∃ mr, b = { a with machineStateRest := mr }) → + (∃ mr, c = { b with machineStateRest := mr }) → ∃ mr, c = { a with machineStateRest := mr } := by + rintro a b c ⟨m1, rfl⟩ ⟨m2, rfl⟩; exact ⟨m2, rfl⟩ + have hr : ∀ a : MachineState, ∃ mr, a = { a with machineStateRest := mr } := fun a => ⟨a.machineStateRest, rfl⟩ + exact (NdOkA.bind ht (NdOkA.gets hr _) fun mr => + NdOkA.bind ht (NdOkA.selectF hr ((ignoreFailure_okA g) mr).1 ((ignoreFailure_okA g) mr).2.1) fun x => + NdOkA.bind ht (NdOkA.modify fun _ => ⟨x.2, rfl⟩) fun _ => NdOkA.pure hr x.1) s + +theorem doMachineOp_vmA (g : MachineRestMonad Unit) : + NdOkA VmA (doMachineOp (Z := Unit) (machineOpLift g)) := by + intro σ + unfold doMachineOp + have h := machineOpLift_restA g σ.machineState + obtain ⟨hf, ⟨x, hx⟩, hq⟩ := h + have e1 : (NonDet.gets (fun s : State Unit => s.machineState)) σ = ({(σ.machineState, σ)}, False) := rfl + rw [nd_bind_oneSc e1] + refine ⟨?_, ?_, ?_⟩ + · rintro (h | ⟨y, -, h⟩) + · exact hf h + · rcases h with h | ⟨z, -, h⟩ + · exact h + · exact h + · refine ⟨((), { σ with machineState := x.2 }), ?_⟩ + refine mem_bindA.2 ⟨(x, σ), ⟨hx, rfl⟩, ?_⟩ + exact mem_bindA.2 ⟨((), { σ with machineState := x.2 }), rfl, rfl⟩ + · intro r hr + obtain ⟨y, hy, hr⟩ := mem_bindA.1 hr + obtain ⟨hy1, hy2⟩ := hy + obtain ⟨z, hz, hr⟩ := mem_bindA.1 hr + obtain ⟨mr, hmr⟩ := hq _ hy1 + cases hz + cases hr + refine ⟨mr, σ.archState.armAsidMap, σ.archState.armVmidTable, σ.archState.armNextVmid, ?_⟩ + show { y.2 with machineState := y.1.2 } = _ + rw [hy2, hmr] + +theorem loadVmid_vmA (asid : Asid) : NdOkA VmA (loadVmid (Z := Unit) asid) := by + unfold loadVmid + exact NdOkA.bind vmA_trans (NdOkA.gets vmA_refl _) fun _ => NdOkA.pure vmA_refl _ + +theorem updateAsidMap_vmA (asid : Asid) (v : Option Vmid) : NdOkA VmA (updateAsidMap (Z := Unit) asid v) := by + unfold updateAsidMap + exact NdOkA.bind vmA_trans (NdOkA.gets vmA_refl _) fun _ => NdOkA.bind vmA_trans (NdOkA.pure vmA_refl _) fun _ => + NdOkA.modify fun _ => ⟨_, _, _, _, rfl⟩ + +theorem invalidateVmidEntry_vmA (v : Vmid) : NdOkA VmA (invalidateVmidEntry (Z := Unit) v) := by + unfold invalidateVmidEntry + exact NdOkA.bind vmA_trans (NdOkA.gets vmA_refl _) fun _ => NdOkA.bind vmA_trans (NdOkA.pure vmA_refl _) fun _ => + NdOkA.modify fun _ => ⟨_, _, _, _, rfl⟩ + +theorem when_vmA {P : Prop} [Decidable P] {m : SMonad Unit Unit} (h : NdOkA VmA m) : + NdOkA VmA (NonDet.when P m) := by + unfold NonDet.when + split + · exact h + · exact NdOkA.pure vmA_refl _ + +theorem invalidateAsidEntry_vmA (asid : Asid) : NdOkA VmA (invalidateAsidEntry (Z := Unit) asid) := by + unfold invalidateAsidEntry invalidateAsid + exact NdOkA.bind vmA_trans (loadVmid_vmA asid) fun _ => + NdOkA.bind vmA_trans (when_vmA (invalidateVmidEntry_vmA _)) fun _ => updateAsidMap_vmA _ _ + +theorem invalidateTlbByAsid_vmA (asid : Asid) : NdOkA VmA (invalidateTlbByAsid (Z := Unit) asid) := by + unfold invalidateTlbByAsid + refine NdOkA.bind vmA_trans (loadVmid_vmA asid) fun mv => ?_ + cases mv with + | none => exact NdOkA.pure vmA_refl _ + | some vmid => exact doMachineOp_vmA _ + +theorem findFreeVmid_vmA : NdOkA VmA (findFreeVmid (Z := Unit)) := by + unfold findFreeVmid + refine NdOkA.bind vmA_trans (NdOkA.gets vmA_refl _) fun vt => + NdOkA.bind vmA_trans (NdOkA.gets vmA_refl _) fun nv => + NdOkA.bind vmA_trans (NdOkA.pure vmA_refl _) fun mv => ?_ + cases mv with + | some v => exact NdOkA.pure vmA_refl _ + | none => + unfold invalidateAsid + exact NdOkA.bind vmA_trans (updateAsidMap_vmA _ _) fun _ => + NdOkA.bind vmA_trans (doMachineOp_vmA _) fun _ => + NdOkA.bind vmA_trans (invalidateVmidEntry_vmA _) fun _ => + NdOkA.bind vmA_trans (NdOkA.pure vmA_refl _) fun _ => + NdOkA.bind vmA_trans (NdOkA.modify fun _ => ⟨_, _, _, _, rfl⟩) fun _ => NdOkA.pure vmA_refl _ + +theorem getVmid_vmA (asid : Asid) : NdOkA VmA (getVmid (Z := Unit) asid) := by + unfold getVmid + refine NdOkA.bind vmA_trans (loadVmid_vmA asid) fun mv => ?_ + cases mv with + | some v => exact NdOkA.pure vmA_refl _ + | none => + refine NdOkA.bind vmA_trans findFreeVmid_vmA fun v => NdOkA.bind vmA_trans ?_ fun _ => NdOkA.pure vmA_refl _ + unfold storeVmid + exact NdOkA.bind vmA_trans (updateAsidMap_vmA _ _) fun _ => NdOkA.bind vmA_trans (NdOkA.gets vmA_refl _) fun _ => + NdOkA.bind vmA_trans (NdOkA.pure vmA_refl _) fun _ => NdOkA.modify fun _ => ⟨_, _, _, _, rfl⟩ + +theorem armContextSwitch_vmA (p : ObjRef) (asid : Asid) : NdOkA VmA (armContextSwitch (Z := Unit) p asid) := by + unfold armContextSwitch setVSpaceRoot + exact NdOkA.bind vmA_trans (getVmid_vmA asid) fun _ => doMachineOp_vmA _ + +theorem setGlobalUserVspace_vmA : NdOkA VmA (setGlobalUserVspace (Z := Unit)) := by + unfold setGlobalUserVspace setVSpaceRoot + exact NdOkA.bind vmA_trans (NdOkA.gets vmA_refl _) fun _ => doMachineOp_vmA _ + +theorem findVspaceForAsid_vmA (asid : Asid) : NdOkA VmA (findVspaceForAsid (Z := Unit) asid).run := by + unfold findVspaceForAsid + refine NdOkA.runBind vmA_refl vmA_trans (NdOkA.liftE vmA_refl vmA_trans (NdOkA.gets vmA_refl _)) fun o => + NdOkA.runBind vmA_refl vmA_trans (NdOkA.liftE vmA_refl vmA_trans (NdOkA.gets vmA_refl _)) fun o' => ?_ + cases o' <;> exact NdOkA.pure vmA_refl _ + +/-- **`set_vm_root` of a TCB has an outcome**, changing only `VmA`'s parts. -/ +theorem setVmRoot_memA {σ : State Unit} {t : ObjRef} {x : Tcb} (hk : σ.kheap t = some (.tcb x)) : + ∃ σ', ((), σ') ∈ (setVmRoot (Z := Unit) t σ).1 ∧ VmA σ σ' := by + have hc : tcbCnodeMap x (tcbCnodeIndex 1) = some x.tcbVtable := by + unfold tcbCnodeMap + simp +decide [Function.update_apply] + unfold setVmRoot + rw [nd_bind_oneSc (show (Pure.pure (t, tcbCnodeIndex 1) : SMonad Unit _) σ + = ({((t, tcbCnodeIndex 1), σ)}, False) from rfl)] + rw [nd_bind_oneSc (Endp.getCap_tcb_eqA hk hc)] + have key : ∀ (m : NonDetE (State Unit) LookupFailure Unit) (h : LookupFailure → SMonad Unit Unit), + NdOkA VmA m.run → (∀ e, NdOkA VmA (h e)) → ∃ σ', ((), σ') ∈ (NonDetE.catch m h σ).1 ∧ VmA σ σ' := by + intro m h hm hh + obtain ⟨-, ⟨⟨⟨⟩, σ'⟩, hx⟩, hq⟩ := NdOkA.catch vmA_refl vmA_trans hm hh σ + exact ⟨σ', hx, hq _ hx⟩ + refine key _ _ ?_ fun _ => setGlobalUserVspace_vmA + split + · refine NdOkA.runBind vmA_refl vmA_trans (findVspaceForAsid_vmA _) fun p' => + NdOkA.runBind vmA_refl vmA_trans ?_ fun _ => + NdOkA.liftE vmA_refl vmA_trans (armContextSwitch_vmA _ _) + unfold NonDetE.whenE + split <;> exact NdOkA.pure vmA_refl _ + · exact NdOkA.pure vmA_refl _ + +/-! ## `delete_asid` -/ + +theorem deleteAsid_noneA {σ : State Unit} {asid : Asid} {pt : ObjRef} (h : poolForAsid asid σ = none) : + ((), σ) ∈ (deleteAsid (Z := Unit) asid pt σ).1 := by + unfold deleteAsid + refine mem_bindA.2 ⟨(none, σ), ?_, rfl⟩ + show (none, σ) ∈ ({(poolForAsid asid σ, σ)} : Set _) + rw [h]; rfl + +theorem getAsidPool_eqA {σ : State Unit} {p : ObjRef} {pool : AsidPool} + (h : σ.kheap p = some (.archObj (.asidPool pool))) : + getAsidPool (Z := Unit) p σ = ({(pool, σ)}, False) := by + show (NonDet.gets asidPoolsOf >>= fun m => NonDet.assertOpt (m p)) σ = _ + rw [nd_bind_oneSc (show NonDet.gets asidPoolsOf σ = ({(asidPoolsOf σ, σ)}, False) from rfl), + asidPoolsOf_eqA h] + rfl + +theorem setAsidPool_eqA {σ : State Unit} {p : ObjRef} {pool pool' : AsidPool} + (h : σ.kheap p = some (.archObj (.asidPool pool))) : + setAsidPool (Z := Unit) p pool' σ = ({((), updKSc σ p (.archObj (.asidPool pool')))}, False) := by + unfold setAsidPool + rw [nd_bind_oneSc (getAsidPool_eqA h)] + exact setObject_eqSc h rfl + +theorem deleteAsid_otherA {σ : State Unit} {asid : Asid} {pt p : ObjRef} {pool : AsidPool} + (hp : poolForAsid asid σ = some p) (hk : σ.kheap p = some (.archObj (.asidPool pool))) + (he : pool (asidLowBitsOf asid) ≠ some (.asidPoolVSpace pt)) : + ((), σ) ∈ (deleteAsid (Z := Unit) asid pt σ).1 := by + unfold deleteAsid + refine mem_bindA.2 ⟨(some p, σ), ?_, ?_⟩ + · show (some p, σ) ∈ ({(poolForAsid asid σ, σ)} : Set _) + rw [hp]; rfl + dsimp only + rw [nd_bind_oneSc (getAsidPool_eqA hk)] + unfold NonDet.when + rw [if_neg he] + rfl + +/-- **`delete_asid` of the entry's own root removes the entry** (module docstring). -/ +theorem deleteAsid_hitA {σ : State Unit} {asid : Asid} {pt p : ObjRef} {pool : AsidPool} {x : Tcb} + (hp : poolForAsid asid σ = some p) (hk : σ.kheap p = some (.archObj (.asidPool pool))) + (he : pool (asidLowBitsOf asid) = some (.asidPoolVSpace pt)) + (hcur : σ.kheap σ.curThread = some (.tcb x)) : + ∃ σ', ((), σ') ∈ (deleteAsid (Z := Unit) asid pt σ).1 ∧ + VmA (updKSc σ p (.archObj (.asidPool (Function.update pool (asidLowBitsOf asid) none)))) σ' := by + obtain ⟨-, ⟨⟨⟨⟩, σ1⟩, h1⟩, q1⟩ := invalidateTlbByAsid_vmA asid σ + have v1 : VmA σ σ1 := q1 _ h1 + obtain ⟨-, ⟨⟨⟨⟩, σ2⟩, h2⟩, q2⟩ := invalidateAsidEntry_vmA asid σ1 + have v2 : VmA σ σ2 := vmA_trans _ _ _ v1 (q2 _ h2) + have hk2 : σ2.kheap p = some (.archObj (.asidPool pool)) := by rw [v2.kheap]; exact hk + set σ3 := updKSc σ2 p (.archObj (.asidPool (Function.update pool (asidLowBitsOf asid) none))) with h3 + have hcp : σ.curThread ≠ p := by + intro e; rw [e, hk] at hcur; cases hcur + have hc3 : σ3.kheap σ3.curThread = some (.tcb x) := by + have e : σ3.curThread = σ.curThread := by rw [h3]; exact v2.curThread + rw [e, h3, updKSc_kheap, Function.update_of_ne hcp, v2.kheap] + exact hcur + obtain ⟨σ4, h4, v4⟩ := setVmRoot_memA (t := σ3.curThread) hc3 + have vu : VmA (updKSc σ p (.archObj (.asidPool (Function.update pool (asidLowBitsOf asid) none)))) σ3 := by + obtain ⟨mr, am, vt, nv, e⟩ := v2 + exact ⟨mr, am, vt, nv, by rw [h3, e]; rfl⟩ + refine ⟨σ4, ?_, vmA_trans _ _ _ vu v4⟩ + unfold deleteAsid + refine mem_bindA.2 ⟨(some p, σ), ?_, ?_⟩ + · show (some p, σ) ∈ ({(poolForAsid asid σ, σ)} : Set _) + rw [hp]; rfl + dsimp only + rw [nd_bind_oneSc (getAsidPool_eqA hk)] + unfold NonDet.when + rw [if_pos he] + refine mem_bindA.2 ⟨((), σ1), h1, mem_bindA.2 ⟨((), σ2), h2, ?_⟩⟩ + dsimp only + rw [nd_bind_oneSc (getAsidPool_eqA hk2), + nd_bind_oneSc (show (Pure.pure (Function.update pool (asidLowBitsOf asid) none) : SMonad Unit _) σ2 + = ({(Function.update pool (asidLowBitsOf asid) none, σ2)}, False) from rfl)] + rw [nd_bind_oneSc (setAsidPool_eqA (pool' := Function.update pool (asidLowBitsOf asid) none) hk2), ← h3, + nd_bind_oneSc (show NonDet.gets (fun s : State Unit => s.curThread) σ3 = ({(σ3.curThread, σ3)}, False) from rfl)] + exact h4 + +/-! ## `delete_asid_pool` -/ + +theorem mapM_vmA {α β : Type} {f : α → SMonad Unit β} (h : ∀ a, NdOkA VmA (f a)) : + ∀ l : List α, NdOkA VmA (NonDet.mapM f l) + | [] => NdOkA.pure vmA_refl _ + | a :: l => NdOkA.bind vmA_trans (h a) fun _ => + NdOkA.bind vmA_trans (mapM_vmA h l) fun _ => NdOkA.pure vmA_refl _ + +theorem deleteAsidPool_otherA {σ : State Unit} {base : Asid} {p : ObjRef} (hb : asidLowBitsOf base = 0) + (h : σ.archState.armAsidTable (asidHighBitsOf base) ≠ some p) : + ((), σ) ∈ (deleteAsidPool (Z := Unit) base p σ).1 := by + unfold deleteAsidPool + have e : NonDet.assert (asidLowBitsOf base = 0) σ = ({((), σ)}, False) := by + simp [NonDet.assert, hb] + rw [nd_bind_oneSc e, nd_bind_oneSc (show NonDet.gets (fun s : State Unit => asidTable s) σ + = ({(asidTable σ, σ)}, False) from rfl)] + unfold NonDet.when + rw [if_neg (show ¬ asidTable σ (asidHighBitsOf base) = some p from h)] + rfl + +/-- The state with the ASID table's entry `hi` removed. -/ +def dropPoolA (σ : State Unit) (hi : BitVec asidHighBits) : State Unit := + { σ with archState := { σ.archState with armAsidTable := Function.update σ.archState.armAsidTable hi none } } + +/-- **`delete_asid_pool` of the installed pool removes its table entry** (module docstring). -/ +theorem deleteAsidPool_hitA {σ : State Unit} {base : Asid} {p : ObjRef} {pool : AsidPool} {x : Tcb} + (hb : asidLowBitsOf base = 0) (h : σ.archState.armAsidTable (asidHighBitsOf base) = some p) + (hk : σ.kheap p = some (.archObj (.asidPool pool))) (hcur : σ.kheap σ.curThread = some (.tcb x)) : + ∃ σ', ((), σ') ∈ (deleteAsidPool (Z := Unit) base p σ).1 ∧ VmA (dropPoolA σ (asidHighBitsOf base)) σ' := by + have step : ∀ offset : Asid, NdOkA VmA (NonDet.when (pool (ucast asidLowLen offset) ≠ none) (do + invalidateTlbByAsid (Z := Unit) (base + offset) + invalidateAsidEntry (base + offset))) := fun offset => + when_vmA (NdOkA.bind vmA_trans (invalidateTlbByAsid_vmA _) fun _ => invalidateAsidEntry_vmA _) + unfold deleteAsidPool + generalize uptoEnum (0 : Asid) (mask asidLowBits) = l + obtain ⟨-, ⟨⟨r, σ1⟩, h1⟩, q1⟩ := mapM_vmA step l σ + have v1 : VmA σ σ1 := q1 _ h1 + set σ2 : State Unit := + { σ1 with archState := { σ1.archState with + armAsidTable := Function.update σ.archState.armAsidTable (asidHighBitsOf base) none } } with h2 + have hc2 : σ2.kheap σ2.curThread = some (.tcb x) := by + show σ1.kheap σ1.curThread = some (.tcb x) + rw [v1.kheap, v1.curThread]; exact hcur + obtain ⟨σ3, h3, v3⟩ := setVmRoot_memA (t := σ2.curThread) hc2 + have vu : VmA (dropPoolA σ (asidHighBitsOf base)) σ2 := by + obtain ⟨mr, am, vt, nv, e⟩ := v1 + exact ⟨mr, am, vt, nv, by rw [h2, e]; rfl⟩ + refine ⟨σ3, ?_, vmA_trans _ _ _ vu v3⟩ + have e : NonDet.assert (asidLowBitsOf base = 0) σ = ({((), σ)}, False) := by + simp [NonDet.assert, hb] + rw [nd_bind_oneSc e, nd_bind_oneSc (show NonDet.gets (fun s : State Unit => asidTable s) σ + = ({(asidTable σ, σ)}, False) from rfl)] + unfold NonDet.when + rw [if_pos (show asidTable σ (asidHighBitsOf base) = some p from h)] + rw [nd_bind_oneSc (getAsidPool_eqA hk)] + refine mem_bindA.2 ⟨(r, σ1), h1, ?_⟩ + dsimp only + rw [nd_bind_oneSc (show (Pure.pure (Function.update (asidTable σ) (asidHighBitsOf base) none) : SMonad Unit _) σ1 + = ({(Function.update (asidTable σ) (asidHighBitsOf base) none, σ1)}, False) from rfl), + nd_bind_oneSc (show NonDet.modify (fun s : State Unit => { s with archState := { s.archState with + armAsidTable := Function.update (asidTable σ) (asidHighBitsOf base) none } }) σ1 + = ({((), σ2)}, False) from rfl), + nd_bind_oneSc (show NonDet.gets (fun s : State Unit => s.curThread) σ2 = ({(σ2.curThread, σ2)}, False) from rfl)] + exact h3 + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelBlkKeepA.lean b/proofs/Fl4ProofsA64/P3/DelBlkKeepA.lean new file mode 100644 index 000000000..c4b505d41 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelBlkKeepA.lean @@ -0,0 +1,432 @@ +/- +P3 (AArch64, delete hooks, record edit #5): **`BlkQueuedA` across the finalisers' spec steps**. + +* `BlkMonoA p`: every TCB stays one, with its state unchanged or not blocked on a queue (`NoBlkA`); every endpoint and + notification other than `p` keeps its queue. A preorder, and every step of `cancel_all_ipc`, `cancel_all_signals` and + the notification unbinds is in it. +* `cancelAllIpc_blkA`, `cancelAllSignals_blkA`: the threads queued on `p` end unblocked. +* `BlkQueuedA.finA`: the record field at the final state. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3FieldsA +import Fl4ProofsA64.P3.SigTcbKeepA +import Fl4ProofsA64.P3.SpecFrameIpcA +import Fl4ProofsA64.P3.ReplyRemoveInvA +import Fl4ProofsA64.P3.NtfnSpecA +import Fl4ProofsA64.P3.EpSpecA +import Fl4ProofsA64.P3.EpCancelIpcA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4SpecA64 Fl4Spec Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched + +noncomputable section + +/-- A thread state that is queued nowhere on an endpoint or a notification. -/ +def NoBlkA (ts : ThreadState) : Prop := epBlocked ts = none ∧ ∀ p, ts ≠ .blockedOnNotification p + +theorem noBlk_restartA : NoBlkA .restart := ⟨rfl, fun _ h => by cases h⟩ +theorem noBlk_inactiveA : NoBlkA .inactive := ⟨rfl, fun _ h => by cases h⟩ + +/-- The TCB at `r` is not queued. -/ +def NoBA (σ : State Unit) (r : ObjRef) : Prop := ∃ x, σ.kheap r = some (.tcb x) ∧ NoBlkA x.tcbState + +/-- A step that blocks no thread and keeps the queues of the endpoints and notifications other than `p`. -/ +structure BlkMonoA (p : ObjRef) (σ σ' : State Unit) : Prop where + tcb : ∀ u x, σ.kheap u = some (.tcb x) → ∃ y, σ'.kheap u = some (.tcb y) ∧ + (y.tcbState = x.tcbState ∨ NoBlkA y.tcbState) + ep : ∀ r y, r ≠ p → σ.kheap r = some (.endpoint y) → σ'.kheap r = some (.endpoint y) + ntfn : ∀ r y, r ≠ p → σ.kheap r = some (.notification y) → + ∃ y', σ'.kheap r = some (.notification y') ∧ y'.ntfnObj = y.ntfnObj + +theorem BlkMonoA.refl (p : ObjRef) (σ : State Unit) : BlkMonoA p σ σ := + ⟨fun _ x h => ⟨x, h, Or.inl rfl⟩, fun _ _ _ h => h, fun _ y _ h => ⟨y, h, rfl⟩⟩ + +theorem BlkMonoA.trans {p : ObjRef} {σ1 σ2 σ3 : State Unit} (h1 : BlkMonoA p σ1 σ2) (h2 : BlkMonoA p σ2 σ3) : + BlkMonoA p σ1 σ3 := by + refine ⟨fun u x h => ?_, fun r y ne h => h2.ep r y ne (h1.ep r y ne h), fun r y ne h => ?_⟩ + · obtain ⟨y, hy, a⟩ := h1.tcb u x h + obtain ⟨z, hz, b⟩ := h2.tcb u y hy + refine ⟨z, hz, ?_⟩ + rcases b with b | b + · rcases a with a | a + · exact Or.inl (b.trans a) + · exact Or.inr (b ▸ a) + · exact Or.inr b + · obtain ⟨y1, hy1, a⟩ := h1.ntfn r y ne h + obtain ⟨y2, hy2, b⟩ := h2.ntfn r y1 ne hy1 + exact ⟨y2, hy2, b.trans a⟩ + +instance (p : ObjRef) : PreO (BlkMonoA p) := ⟨fun σ => BlkMonoA.refl p σ, fun h1 h2 => h1.trans h2⟩ + +theorem NoBA.mono {p r : ObjRef} {σ σ' : State Unit} (h : NoBA σ r) (K : BlkMonoA p σ σ') : NoBA σ' r := by + obtain ⟨x, hx, n⟩ := h + obtain ⟨y, hy, a⟩ := K.tcb r x hx + refine ⟨y, hy, ?_⟩ + rcases a with a | a + · rw [a]; exact n + · exact a + +/-- A step changing only sched contexts. -/ +theorem BlkMonoA.of_scOnly {p : ObjRef} {α : Type} {m : SMonad Unit α} (h : TcKPresA m) : PresR (BlkMonoA p) m := by + intro σ x hx + have k := h σ x hx + exact ⟨fun u y hu => ⟨y, k.1 u _ hu (fun _ _ e => by cases e), Or.inl rfl⟩, + fun r y _ hr => k.1 r _ hr (fun _ _ e => by cases e), + fun r y _ hr => ⟨y, k.1 r _ hr (fun _ _ e => by cases e), rfl⟩⟩ + +/-- One object replaced. -/ +theorem BlkMonoA.updA {p q : ObjRef} {σ : State Unit} {o0 o : KernelObject} (hq : σ.kheap q = some o0) + (ht : ∀ x, o0 = .tcb x → ∃ y, o = .tcb y ∧ (y.tcbState = x.tcbState ∨ NoBlkA y.tcbState)) + (he : ∀ y, o0 = .endpoint y → q = p) + (hn : ∀ y, o0 = .notification y → q = p ∨ ∃ y', o = .notification y' ∧ y'.ntfnObj = y.ntfnObj) : + BlkMonoA p σ { σ with kheap := Function.update σ.kheap q (some o) } := by + refine ⟨fun u x hu => ?_, fun r y ne hr => ?_, fun r y ne hr => ?_⟩ + · by_cases e : u = q + · subst e + rw [hq] at hu + obtain ⟨y, rfl, a⟩ := ht x (Option.some.inj hu) + exact ⟨y, Function.update_self _ _ _, a⟩ + · exact ⟨x, (Function.update_of_ne e _ _).trans hu, Or.inl rfl⟩ + · have e : r ≠ q := fun e => by + subst e + rw [hq] at hr + exact ne (he y (Option.some.inj hr)) + exact (Function.update_of_ne e _ _).trans hr + · by_cases e : r = q + · subst e + rw [hq] at hr + rcases hn y (Option.some.inj hr) with c | ⟨y', rfl, a⟩ + · exact absurd c ne + · exact ⟨y', Function.update_self _ _ _, a⟩ + · exact ⟨y, (Function.update_of_ne e _ _).trans hr, rfl⟩ + +/-! ## The leaves -/ + +theorem getSimpleKo_tkA {α : Type} [Nonempty α] (C : α → KernelObject) (p : ObjRef) : + TcKPresA (getSimpleKo (Z := Unit) C p) := by + unfold getSimpleKo; tckpres + +theorem setEndpoint_blkA (p : ObjRef) (z : Endpoint) : PresR (BlkMonoA p) (setEndpoint (Z := Unit) p z) := by + intro σ r hr + obtain ⟨y, hy, e⟩ := setSimpleKo_memA _ _ _ hr + rw [e] + exact BlkMonoA.updA hy (fun _ h => by cases h) (fun _ _ => rfl) (fun _ h => by cases h) + +theorem setNotification_blkA (p : ObjRef) (z : Notification) : + PresR (BlkMonoA p) (setSimpleKo (Z := Unit) KernelObject.notification p z) := by + intro σ r hr + obtain ⟨y, hy, e⟩ := setSimpleKo_memA _ _ _ hr + rw [e] + exact BlkMonoA.updA hy (fun _ h => by cases h) (fun _ h => by cases h) (fun _ _ => Or.inl rfl) + +theorem setReply_blkA (p q : ObjRef) (z : Reply) : + PresR (BlkMonoA p) (setSimpleKo (Z := Unit) KernelObject.reply q z) := by + intro σ r hr + obtain ⟨y, hy, e⟩ := setSimpleKo_memA _ _ _ hr + rw [e] + exact BlkMonoA.updA hy (fun _ h => by cases h) (fun _ h => by cases h) (fun _ h => by cases h) + +theorem threadSet_blkA (p t : ObjRef) (f : Tcb → Tcb) + (hf : ∀ x, (f x).tcbState = x.tcbState ∨ NoBlkA (f x).tcbState) : + PresR (BlkMonoA p) (threadSet (Z := Unit) f t) := by + intro σ r hr + obtain ⟨x, hx, e⟩ := threadSet_memA f t hr + rw [e] + exact BlkMonoA.updA hx (fun x' h => by cases h; exact ⟨_, rfl, hf x⟩) (fun _ h => by cases h) + (fun _ h => by cases h) + +theorem setThreadState_blkA (p t : ObjRef) {ts : ThreadState} (h : NoBlkA ts) : + PresR (BlkMonoA p) (setThreadState (Z := Unit) t ts) := by + unfold setThreadState + exact PresR.bind (threadSet_blkA p t _ fun _ => Or.inr h) fun _ => BlkMonoA.of_scOnly TcKInst.pres + +theorem tcbEpDequeue_blkA (t p : ObjRef) : PresR (BlkMonoA p) (tcbEpDequeue (Z := Unit) t p) := by + unfold tcbEpDequeue + refine PresR.bind (BlkMonoA.of_scOnly (getSimpleKo_tkA _ _)) fun ep => ?_ + refine PresR.bind (PresR.pure _) fun q => ?_ + refine PresR.bind (PresR.assert _) fun _ => ?_ + refine PresR.bind (PresR.pure _) fun q' => ?_ + exact PresR.bind (PresR.pure _) fun ep' => setEndpoint_blkA p _ + +theorem tcbNtfnDequeue_blkA (t p : ObjRef) : PresR (BlkMonoA p) (tcbNtfnDequeue (Z := Unit) t p) := by + unfold tcbNtfnDequeue + refine PresR.bind (BlkMonoA.of_scOnly (getSimpleKo_tkA _ _)) fun ntfn => ?_ + refine PresR.bind (PresR.pure _) fun q => ?_ + refine PresR.bind (PresR.assert _) fun _ => ?_ + refine PresR.bind (PresR.pure _) fun q' => ?_ + exact PresR.bind (PresR.pure _) fun n' => setNotification_blkA p _ + +theorem replyUnlinkTcb_blkA (p t r : ObjRef) : PresR (BlkMonoA p) (replyUnlinkTcb (Z := Unit) t r) := by + unfold replyUnlinkTcb + refine PresR.bind (BlkMonoA.of_scOnly (getSimpleKo_tkA _ _)) fun reply => ?_ + refine PresR.bind (PresR.assert _) fun _ => ?_ + refine PresR.bind (BlkMonoA.of_scOnly TcKInst.pres) fun ts => ?_ + refine PresR.bind (PresR.assert _) fun _ => ?_ + refine PresR.bind ?_ fun _ => setThreadState_blkA p t noBlk_inactiveA + unfold setReplyObjRef updateSkObjRef + exact PresR.bind (BlkMonoA.of_scOnly (getSimpleKo_tkA _ _)) fun sc => setReply_blkA p r _ + +/-- The restart tail of a queued thread's removal. -/ +theorem restartTail_tkA (t : ObjRef) : TcKPresA (do + let scOpt ← getTcbObjRef (Z := Unit) Tcb.tcbSchedContext t + ifSporadicCurScAssertRefillUnblockCheck scOpt + possibleSwitchTo t : SMonad Unit Unit) := by + tckpres + +theorem restartThreadIfNoFault_blkA (p t : ObjRef) : + PresR (BlkMonoA p) (restartThreadIfNoFault (Z := Unit) t) := by + unfold restartThreadIfNoFault + refine PresR.bind (BlkMonoA.of_scOnly TcKInst.pres) fun fault => ?_ + refine PresR.ite' ?_ (setThreadState_blkA p t noBlk_inactiveA) + exact PresR.bind (setThreadState_blkA p t noBlk_restartA) fun _ => BlkMonoA.of_scOnly (restartTail_tkA t) + +/-- `set_thread_state` to an unqueued state, then a step keeping it, leaves the thread unqueued. -/ +theorem stsThen_noBA {p t : ObjRef} {ts : ThreadState} (h : NoBlkA ts) {m : SMonad Unit Unit} + (hm : PresR (BlkMonoA p) m) {σ : State Unit} {r : Unit × State Unit} + (hr : r ∈ ((setThreadState (Z := Unit) t ts >>= fun _ => m) σ).1) : NoBA r.2 t := by + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + obtain ⟨y, hy, e⟩ := setThreadState_memA (σ1 := x.2) hx + exact NoBA.mono ⟨y, hy, e ▸ h⟩ (hm x.2 r hr) + +theorem restartThreadIfNoFault_noBA {t : ObjRef} {σ : State Unit} {r : Unit × State Unit} + (hr : r ∈ (restartThreadIfNoFault (Z := Unit) t σ).1) : NoBA r.2 t := by + unfold restartThreadIfNoFault at hr + obtain ⟨x, -, hr⟩ := mem_bindA.1 hr + split at hr + · exact stsThen_noBA (p := t) noBlk_restartA (BlkMonoA.of_scOnly (restartTail_tkA t)) hr + · obtain ⟨y, hy, e⟩ := setThreadState_memA (σ1 := r.2) hr + exact ⟨y, hy, e ▸ noBlk_inactiveA⟩ + +theorem removeAndRestartEp_blkA (t p : ObjRef) : + PresR (BlkMonoA p) (removeAndRestartEpQueuedThread (Z := Unit) t p) := by + unfold removeAndRestartEpQueuedThread + refine PresR.bind (tcbEpDequeue_blkA t p) fun _ => ?_ + refine PresR.bind (BlkMonoA.of_scOnly TcKInst.pres) fun st => ?_ + refine PresR.bind (PresR.pure _) fun o => ?_ + refine PresR.bind ?_ fun _ => restartThreadIfNoFault_blkA p t + cases o with + | none => exact PresR.pure _ + | some rp => exact replyUnlinkTcb_blkA p t rp + +theorem removeAndRestartEp_noBA {t p : ObjRef} {σ : State Unit} {r : Unit × State Unit} + (hr : r ∈ (removeAndRestartEpQueuedThread (Z := Unit) t p σ).1) : NoBA r.2 t := by + unfold removeAndRestartEpQueuedThread at hr + obtain ⟨_, -, hr⟩ := mem_bindA.1 hr + obtain ⟨_, -, hr⟩ := mem_bindA.1 hr + obtain ⟨_, -, hr⟩ := mem_bindA.1 hr + obtain ⟨_, -, hr⟩ := mem_bindA.1 hr + exact restartThreadIfNoFault_noBA hr + +theorem removeAndRestartNtfn_blkA (t p : ObjRef) : + PresR (BlkMonoA p) (removeAndRestartNtfnQueuedThread (Z := Unit) t p) := by + unfold removeAndRestartNtfnQueuedThread + refine PresR.bind (tcbNtfnDequeue_blkA t p) fun _ => ?_ + exact PresR.bind (setThreadState_blkA p t noBlk_restartA) fun _ => BlkMonoA.of_scOnly (restartTail_tkA t) + +theorem removeAndRestartNtfn_noBA {t p : ObjRef} {σ : State Unit} {r : Unit × State Unit} + (hr : r ∈ (removeAndRestartNtfnQueuedThread (Z := Unit) t p σ).1) : NoBA r.2 t := by + unfold removeAndRestartNtfnQueuedThread at hr + obtain ⟨_, -, hr⟩ := mem_bindA.1 hr + exact stsThen_noBA (p := p) noBlk_restartA (BlkMonoA.of_scOnly (restartTail_tkA t)) hr + +/-- A loop over queued threads each step of which leaves its thread unqueued. -/ +theorem mapMx_blkA {p : ObjRef} {β : Type} {f : ObjRef → SMonad Unit β} (hf : ∀ t, PresR (BlkMonoA p) (f t)) + (hn : ∀ t σ r, r ∈ (f t σ).1 → NoBA r.2 t) : + ∀ (l : List ObjRef) (σ : State Unit) (r : Unit × State Unit), r ∈ (NonDet.mapMx f l σ).1 → + BlkMonoA p σ r.2 ∧ ∀ t ∈ l, NoBA r.2 t + | [], σ, r, hr => by + unfold NonDet.mapMx at hr + have e : r = ((), σ) := hr + rw [e] + exact ⟨BlkMonoA.refl p σ, fun _ h => by cases h⟩ + | a :: l, σ, r, hr => by + unfold NonDet.mapMx at hr + obtain ⟨x, hx, hr⟩ := hr + obtain ⟨k, d⟩ := mapMx_blkA hf hn l x.2 r hr + refine ⟨(hf a σ x hx).trans k, fun t ht => ?_⟩ + rcases List.mem_cons.1 ht with e | h + · rw [e]; exact (hn a σ x hx).mono k + · exact d t h + +/-- **`cancel_all_ipc`**: `BlkMonoA`, and every thread queued on the endpoint ends unqueued. -/ +theorem cancelAllIpc_blkA {p : ObjRef} {σ : State Unit} {y : Endpoint} (hy : σ.kheap p = some (.endpoint y)) + {r : Unit × State Unit} (hr : r ∈ (cancelAllIpc (Z := Unit) p σ).1) : + BlkMonoA p σ r.2 ∧ ∀ t ∈ y.epQueue, NoBA r.2 t := by + unfold cancelAllIpc at hr + rw [nd_bind_one (getEndpoint_eq hy)] at hr + have go : ∀ q : List ObjRef, r ∈ ((NonDet.mapMx (fun t => removeAndRestartEpQueuedThread (Z := Unit) t p) q >>= + fun _ => rescheduleRequired) σ).1 → BlkMonoA p σ r.2 ∧ ∀ t ∈ q, NoBA r.2 t := by + intro q h + obtain ⟨x, hx, h⟩ := mem_bindA.1 h + obtain ⟨k, d⟩ := mapMx_blkA (fun t => removeAndRestartEp_blkA t p) (fun t _ _ h => removeAndRestartEp_noBA h) + q σ x hx + have k2 := BlkMonoA.of_scOnly (p := p) rescheduleRequired_tkA x.2 r h + exact ⟨k.trans k2, fun t ht => (d t ht).mono k2⟩ + cases y with + | idleEP => + have e : r = ((), σ) := hr + rw [e] + exact ⟨BlkMonoA.refl p σ, fun _ h => by cases h⟩ + | sendEP q => exact go q (by dsimp only at hr; rw [nd_pure_bind'] at hr; exact hr) + | recvEP q => exact go q (by dsimp only at hr; rw [nd_pure_bind'] at hr; exact hr) + +/-- **`cancel_all_signals`**: `BlkMonoA`, and every thread queued on the notification ends unqueued. -/ +theorem cancelAllSignals_blkA {p : ObjRef} {σ : State Unit} {y : Notification} + (hy : σ.kheap p = some (.notification y)) {r : Unit × State Unit} + (hr : r ∈ (cancelAllSignals (Z := Unit) p σ).1) : + BlkMonoA p σ r.2 ∧ ∀ q, y.ntfnObj = .waitingNtfn q → ∀ t ∈ q, NoBA r.2 t := by + unfold cancelAllSignals at hr + rw [nd_bind_one (getNotification_eq hy)] at hr + cases ho : y.ntfnObj with + | waitingNtfn q => + rw [ho] at hr + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + obtain ⟨k, d⟩ := mapMx_blkA (fun t => removeAndRestartNtfn_blkA t p) (fun t _ _ h => removeAndRestartNtfn_noBA h) + q σ x hx + have k2 := BlkMonoA.of_scOnly (p := p) rescheduleRequired_tkA x.2 r hr + refine ⟨k.trans k2, fun q' e t ht => ?_⟩ + injection e with e + exact (d t (e ▸ ht)).mono k2 + | idleNtfn => + rw [ho] at hr + have e : r = ((), σ) := hr + rw [e] + exact ⟨BlkMonoA.refl p σ, fun _ h => by cases h⟩ + | activeNtfn b => + rw [ho] at hr + have e : r = ((), σ) := hr + rw [e] + exact ⟨BlkMonoA.refl p σ, fun _ h => by cases h⟩ + +/-! ## The unbinds before `cancel_all_signals` -/ + +theorem setNtfnObjRef_blkA (p : ObjRef) (f : (Option ObjRef → Option ObjRef) → Notification → Notification) + (o : Option ObjRef) : PresR (BlkMonoA p) (setNtfnObjRef (Z := Unit) f p o) := by + unfold setNtfnObjRef updateSkObjRef + exact PresR.bind (BlkMonoA.of_scOnly (getSimpleKo_tkA _ _)) fun x => setNotification_blkA p _ + +theorem schedContextMaybeUnbindNtfn_blkA (p : ObjRef) : + PresR (BlkMonoA p) (schedContextMaybeUnbindNtfn (Z := Unit) p) := by + unfold schedContextMaybeUnbindNtfn getNtfnObjRef getSkObjRef + refine PresR.bind (PresR.bind (BlkMonoA.of_scOnly (getSimpleKo_tkA _ _)) fun _ => PresR.pure _) fun o => ?_ + cases o with + | none => exact PresR.pure _ + | some sc => + show PresR (BlkMonoA p) (doUnbindNtfnSc (Z := Unit) p sc) + unfold doUnbindNtfnSc + refine PresR.bind (setNtfnObjRef_blkA p _ _) fun _ => BlkMonoA.of_scOnly ?_ + unfold setScObjRef + exact TcKInst.pres + +theorem unbindMaybeNotification_blkA (p : ObjRef) : + PresR (BlkMonoA p) (unbindMaybeNotification (Z := Unit) p) := by + unfold unbindMaybeNotification getNtfnObjRef getSkObjRef + refine PresR.bind (PresR.bind (BlkMonoA.of_scOnly (getSimpleKo_tkA _ _)) fun _ => PresR.pure _) fun o => ?_ + cases o with + | none => exact PresR.pure _ + | some t => + show PresR (BlkMonoA p) (doUnbindNotification (Z := Unit) p t) + unfold doUnbindNotification setTcbObjRef + exact PresR.bind (setNtfnObjRef_blkA p _ _) fun _ => threadSet_blkA p t _ fun _ => Or.inl rfl + +/-! ## `cancel_ipc` of a receiver holding a reply object -/ + +/-- **`cancel_ipc` of a thread blocked on a receive**: `BlkMonoA`, and each thread queued on its endpoint ends +unqueued (the thread itself) or stays queued. -/ +theorem cancelIpc_recv_blkA {σ σ' : State Unit} {t e rp : ObjRef} {x : Tcb} (hk : σ.kheap t = some (.tcb x)) + {z : Endpoint} (hkz : σ.kheap e = some (.endpoint z)) (hq : t ∈ z.epQueue) {d : ReceiverPayload} + (hs : x.tcbState = .blockedOnReceive e (some rp) d) + (h : ((), σ') ∈ (cancelIpc (Z := Unit) t σ).1) : + BlkMonoA e σ σ' ∧ ∀ u ∈ z.epQueue, + NoBA σ' u ∨ ∃ z', σ'.kheap e = some (.endpoint z') ∧ u ∈ z'.epQueue := by + have het : e ≠ t := fun c => by rw [c, hk] at hkz; cases hkz + have hkz1 : (updK σ t (.tcb { x with tcbFault := none })).kheap e = some (.endpoint z) := by + rw [updK_kheap, Function.update_of_ne het]; exact hkz + have hf : BlkMonoA e σ (updK σ t (.tcb { x with tcbFault := none })) := + BlkMonoA.updA hk (fun y h => by cases h; exact ⟨_, rfl, Or.inl rfl⟩) (fun _ h => by cases h) + (fun _ h => by cases h) + have hdq := tcbEpDequeue_eq (Z := Unit) hkz1 hq + have hmono := tcbEpDequeue_blkA t e (updK σ t (.tcb { x with tcbFault := none })) + ((), updK (updK σ t (.tcb { x with tcbFault := none })) e (.endpoint (epDeq z t))) + (by rw [hdq]; exact rfl) + unfold cancelIpc at h + rw [nd_bind_one (getThreadState_eq hk), nd_bind_one (threadSet_eq _ hk)] at h + generalize hσ1 : updK σ t (.tcb { x with tcbFault := none }) = σ1 at h hdq hf hmono + rw [hs] at h + dsimp only at h + unfold blockedCancelIpc at h + obtain ⟨y1, hy1, h⟩ := mem_bindA.1 h + have e1 : y1 = (e, σ1) := hy1 + subst e1 + obtain ⟨y2, hy2, h⟩ := mem_bindA.1 h + rw [hdq] at hy2 + have e2 : y2 = ((), updK σ1 e (.endpoint (epDeq z t))) := hy2 + subst e2 + dsimp only at h + have rest : ∀ q, PresR (BlkMonoA q) (do + replyUnlinkTcb (Z := Unit) t rp + setThreadState t .inactive : SMonad Unit Unit) := fun q => + PresR.bind (replyUnlinkTcb_blkA q t rp) fun _ => setThreadState_blkA q t noBlk_inactiveA + have k2 := rest e _ _ h + have kt := rest t _ _ h + refine ⟨(hf.trans hmono).trans k2, fun u hu => ?_⟩ + by_cases c : u = t + · subst c + obtain ⟨_, -, h'⟩ := mem_bindA.1 h + obtain ⟨y, hy, ey⟩ := setThreadState_memA h' + exact Or.inl ⟨y, hy, ey ▸ noBlk_inactiveA⟩ + · refine Or.inr ⟨epDeq z t, kt.ep e _ het (by rw [updK_kheap, Function.update_self]), ?_⟩ + rw [epDeq_queue] + unfold removeAll + exact List.mem_filter.2 ⟨hu, decide_eq_true (Ne.symm c)⟩ + +/-! ## The record field -/ + +/-- **`BlkQueuedA` at the final state**: a `BlkMonoA p` step after which the threads queued on `p` are unqueued. -/ +theorem BlkQueuedA.finA {T E N : Set U64} {m m' : U64 → U64} {σ σ' : State Unit} {p : ObjRef} + (h : BlkQueuedA T E N m σ) (K : BlkMonoA p σ σ') + (ht : ∀ t ∈ T, ∃ x, σ.kheap (specPtr t) = some (.tcb x)) + (hm : ∀ t ∈ T, m' (Notif.tBlockA t) = m (Notif.tBlockA t)) + (de : ∀ y, σ.kheap p = some (.endpoint y) → ∀ r ∈ y.epQueue, + NoBA σ' r ∨ ∃ y', σ'.kheap p = some (.endpoint y') ∧ r ∈ y'.epQueue) + (dn : ∀ y q, σ.kheap p = some (.notification y) → y.ntfnObj = .waitingNtfn q → ∀ r ∈ q, + NoBA σ' r ∨ ∃ y' q', σ'.kheap p = some (.notification y') ∧ y'.ntfnObj = .waitingNtfn q' ∧ r ∈ q') : + BlkQueuedA T E N m' σ' where + ep := fun t h1 x' hk e hb => by + obtain ⟨x, hx⟩ := ht t h1 + obtain ⟨x1, hx1, hs⟩ := K.tcb _ _ hx + rw [hk] at hx1; cases hx1 + rcases hs with es | nb + swap + · rw [nb.1] at hb; cases hb + obtain ⟨a, y, b, c⟩ := h.ep t h1 x hx e (es ▸ hb) + by_cases ne : e = p + · subst ne + rcases de y b _ c with ⟨z, hz, nb⟩ | ⟨y', hy', c'⟩ + · rw [hk] at hz; cases hz + rw [nb.1] at hb; cases hb + · exact ⟨by rw [hm t h1]; exact a, y', hy', c'⟩ + · exact ⟨by rw [hm t h1]; exact a, y, K.ep _ _ ne b, c⟩ + ntfn := fun t h1 x' hk q hb => by + obtain ⟨x, hx⟩ := ht t h1 + obtain ⟨x1, hx1, hs⟩ := K.tcb _ _ hx + rw [hk] at hx1; cases hx1 + rcases hs with es | nb + swap + · exact absurd hb (nb.2 q) + obtain ⟨a, y, l, b, c, d⟩ := h.ntfn t h1 x hx q (es.symm.trans hb) + by_cases ne : q = p + · subst ne + rcases dn y l b c _ d with ⟨z, hz, nb⟩ | ⟨y', q', hy', c', d'⟩ + · rw [hk] at hz; cases hz + exact absurd hb (nb.2 _) + · exact ⟨by rw [hm t h1]; exact a, y', q', hy', c', d'⟩ + · obtain ⟨y', hy', e⟩ := K.ntfn _ _ ne b + exact ⟨by rw [hm t h1]; exact a, y', l, hy', e.trans c, d⟩ + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelBufKeepA.lean b/proofs/Fl4ProofsA64/P3/DelBufKeepA.lean new file mode 100644 index 000000000..a9a7590be --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelBufKeepA.lean @@ -0,0 +1,264 @@ +/- +P3 (AArch64, delete hooks E4, G3): **the IPC-buffer slot across the loop's three slot steps**. + +The loop record carries A64 P1's `BufHold` (every TCB's IPC-buffer slot holds `Null` or a non-device frame; at loop +entry it is part of P1's `BufInv`). It is kept by each step (**`bufHold_emptyA`**, **`bufHold_swapA`**, +**`bufHold_zombieA`**), a zombie's base is no buffer slot (**`base_not_bufSA`**), and from it each step leaves every +buffer slot's cap unchanged or `Null`, which keeps **`BufNativeA.stepA`** and **`BufCover.stepA`**. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.DelStepKeepA +import Fl4ProofsA64.P3.DelJ5StepA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 Fl4ProofsA64.P3.Endp + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {H : Heap} {OS T : Set U64} {th : TypedHeapA} {s r : S} {σ σ' : State Unit} + +/-! ## `BufHold` -/ + +theorem not_bufCap_zombieA {p zt n : U64} : ¬ BufCap (.Zombie p zt n) := by + rintro (h | ⟨_, _, _, _, _, _, _, h⟩) <;> cases h + +theorem not_bufCap_tcbCnA {c : capability.Cap} (h : IsTcbCnA c) : ¬ BufCap c := by + rcases h with ⟨t, rfl⟩ | ⟨q, rb, gs, g, rfl⟩ <;> rintro (h | ⟨_, _, _, _, _, _, _, h⟩) <;> cases h + +/-- A slot whose cap is no buffer cap is no buffer slot. -/ +theorem not_bufS_of_capA {x : U64} (h : BufHold st H OS s) (hx : x ∈ OS) (hc : ¬ BufCap (capA st s x)) : + x ∉ BufS H.tcbs := fun hb => hc (h x hx hb) + +theorem bufHold_emptyA {x : U64} (h : BufHold st H OS s) (h0 : capA st r x = .Null) + (hcap : ∀ y ∈ OS, y ≠ x → capA st r y = capA st s y) : BufHold st H OS r := fun y hy hb => by + by_cases e : y = x + · subst e; exact Or.inl h0 + · rw [hcap y hy e]; exact h y hy hb + +theorem bufHold_swapA {x p : U64} (h : BufHold st H OS s) (hx : x ∉ BufS H.tcbs) (hp : p ∉ BufS H.tcbs) + (hcap : ∀ y ∈ OS, capA st r y = if y = x then capA st s p else if y = p then capA st s x else capA st s y) : + BufHold st H OS r := fun y hy hb => by + have a : y ≠ x := fun e => hx (e ▸ hb) + have b : y ≠ p := fun e => hp (e ▸ hb) + rw [hcap y hy, if_neg a, if_neg b] + exact h y hy hb + +theorem bufHold_zombieA {x : U64} (h : BufHold st H OS s) (hx : x ∉ BufS H.tcbs) + (hcap : ∀ y ∈ OS, y ≠ x → capA st r y = capA st s y) : BufHold st H OS r := fun y hy hb => by + have a : y ≠ x := fun e => hx (e ▸ hb) + rw [hcap y hy a] + exact h y hy hb + +/-- The base of an object of `th` is no TCB's buffer slot. -/ +theorem base_not_bufSA {kb p : U64} {k : ObjKindA} (P : PlaceA th kb) (hT : ∀ t ∈ T, th t = some .tcb) + (hp : th p = some k) : p ∉ BufS T := by + rintro ⟨t, ht, e⟩ + have h64 : core.num.U64.wrapping_mul layout.tcb.SLOT_BUFFER layout.cte.SIZE = 64#u64 := by + first + | rfl + | decide + | (unfold layout.tcb.SLOT_BUFFER layout.cte.SIZE; rfl) + have ht' := hT t ht + have hf := P.fit t _ ht' + have hsz : ObjKindA.tcb.size = 4096 := by first | rfl | decide | simp [ObjKindA.size] + rw [hsz] at hf + have hv : (sbWordA t).val = t.val + 64 := by + unfold sbWordA + rw [h64] + simp [core.num.U64.wrapping_add_val_eq, U64.size_def, U64.numBits]; omega + have hpv : p.val = t.val + 64 := by rw [← e]; exact hv + have ne : p ≠ t := fun q => by rw [q] at hpv; omega + rcases P.apart p t _ _ hp ht' ne with a | a + · omega + · rw [hsz] at a; omega + +/-! ## The buffer fields -/ + +/-- `BufNativeA` across a step that leaves each buffer slot's cap or empties it. -/ +theorem BufNativeA.stepA (h : BufNativeA T (L.mem s)) + (k : ∀ t ∈ T, capA st r (sbWordA t) = capA st s (sbWordA t) ∨ capA st r (sbWordA t) = .Null) : + BufNativeA T (L.mem r) := by + intro t ht + have hn := h t ht + rw [bufSlot_eqA, capAtA_eq (st := st)] at hn ⊢ + rcases k t ht with e | e + · rw [e]; exact hn + · rw [e]; trivial + +/-- `BufCover` across the same, with the TCBs' buffer address words kept. -/ +theorem BufCover.stepA {Bf : Set (U64 × Fl4SpecA64.Word)} (h : BufCover st T Bf s σ) + (R : StateRelA th (L.mem s) σ) (R' : StateRelA th (L.mem r) σ') (hT : ∀ t ∈ T, th t = some .tcb) + (HS : SpecSlotA σ σ') (ib : ∀ t ∈ T, L.mem r (ibWordA t) = L.mem s (ibWordA t)) + (k : ∀ t ∈ T, capA st r (sbWordA t) = capA st s (sbWordA t) ∨ capA st r (sbWordA t) = .Null) : + BufCover st T Bf r σ' := by + intro t ht x' b sb w hx' hb hl + obtain ⟨x, hx, hs⟩ := R.tcb t (hT t ht) + obtain ⟨y', hy', hs'⟩ := R'.tcb t (hT t ht) + rw [hx'] at hy'; cases hy' + obtain ⟨y, hy, ey⟩ := HS.tcb hx + rw [hx'] at hy; cases hy + have hbuf : x'.tcbIpcBuffer = x.tcbIpcBuffer := by + have h0 := congrArg Fl4SpecA64.Tcb.tcbIpcBuffer ey; exact h0 + have slot : ∀ {m : U64 → U64} {z : Fl4SpecA64.Tcb} {a : U64}, + SlotRelA m a (tcbCnodeMap z (tcbCnodeIndex 2)) → CapEqA (capAtA m a) z.tcbIpcframe := by + rintro m z a ⟨ac, e, hc⟩ + rw [tcbCnodeMap_twoA] at e + cases e + exact hc.2 + have c := slot (hs 2 (by decide)) + have c' := slot (hs' 2 (by decide)) + rw [bufSlot_eqA, capAtA_eq (st := st)] at c c' + -- the new buffer cap is a frame + have hfr : ∃ p R vms md, x'.tcbIpcframe = .archObjectCap (.frameCap p R vms false md) := by + unfold ipcBufOfA at hb + split at hb + · next e => exact ⟨_, _, _, _, e⟩ + · cases hb + obtain ⟨fp, fR, fv, fm, hfr⟩ := hfr + have abs' : absCapA (capA st r (sbWordA t)) = x'.tcbIpcframe := by + rcases c'.eq_or_pt with e | ⟨_, _, _, e⟩ + · exact e.symm + · rw [hfr] at e; cases e + have same : capA st r (sbWordA t) = capA st s (sbWordA t) := by + rcases k t ht with e | e + · exact e + · rw [e, hfr] at abs'; simp [absCapA] at abs' + rw [same] at abs' c' + have hframe : x.tcbIpcframe = x'.tcbIpcframe := by + rw [hfr] at abs' ⊢ + generalize capA st s (sbWordA t) = cc at abs' c + cases cc <;> first + | (simp [absCapA] at abs'; done) + | (have c0 : absCapA _ = x.tcbIpcframe := c + exact c0.symm.trans abs') + have hb0 : ipcBufOfA b x = some sb := by + rw [← hb]; unfold ipcBufOfA; rw [hframe, hbuf] + have hlk : msg.lookup_ipc_buffer st r b t = msg.lookup_ipc_buffer st s b t := by + have hsl : obj.Tcb.slot t layout.tcb.SLOT_BUFFER = ok (sbWordA t) := by simp [obj.Tcb.slot, lift, sbWordA] + unfold msg.lookup_ipc_buffer + rw [ipc_buffer_word, ipc_buffer_word, ib t ht, hsl] + simp only [bind_ok] + rw [Fl4ProofsA64.P1.W7.R.ST.capA_eq, Fl4ProofsA64.P1.W7.R.ST.capA_eq, same] + rw [hlk] at hl + exact h t ht x b sb w hx hb0 hl + +/-! ## The pins of the tracked buffers (record edit #8b-C2, 12′) -/ + +/-- **A non-zero IPC-buffer lookup reads a non-device frame cap in the buffer slot, and lies in that frame.** -/ +theorem lookup_frameA {t w : U64} {rcv : Bool} (hl : msg.lookup_ipc_buffer st s rcv t = ok w) (hw : w ≠ 0#u64) : + ∃ ptr size rights x1 x2 x3 x4, capA st s (sbWordA t) = .Frame ptr size rights false x1 x2 x3 x4 ∧ + ∀ bits, size.bits = ok bits → ptr.val + 2 ^ bits.val ≤ 2 ^ 64 → + ptr.val ≤ w.val ∧ w.val < ptr.val + 2 ^ bits.val := by + have hs : obj.Tcb.slot t layout.tcb.SLOT_BUFFER = ok (sbWordA t) := by simp [obj.Tcb.slot, lift, sbWordA] + unfold msg.lookup_ipc_buffer at hl + rw [ipc_buffer_word, bind_ok, hs, bind_ok, Fl4ProofsA64.P1.W7.R.ST.capA_eq, bind_ok] at hl + generalize capA st s (sbWordA t) = c at hl ⊢ + cases c with + | Frame ptr size rights dev x1 x2 x3 x4 => + cases dev with + | true => exact absurd (ok_inj_ff (show (ok 0#u64 : Result U64) = ok w from hl)).symm hw + | false => + refine ⟨ptr, size, rights, x1, x2, x3, x4, rfl, fun bits hbits hfit => ?_⟩ + simp only [Bool.false_eq_true, ↓reduceIte] at hl + have h30 : bits.val < 31 := by + cases size <;> simp only [capability.FrameSize.bits] at hbits <;> rw [← ok_inj_ff hbits] <;> simp + have addr : ∀ r : Result U64, (do + let bits ← capability.FrameSize.bits size + let i ← lift (bits &&& 63#u64) + let i1 ← 1#u64 <<< i + let i2 ← lift (core.num.U64.wrapping_sub i1 1#u64) + let i3 ← lift (L.mem s (ibWordA t) &&& i2) + ok (core.num.U64.wrapping_add ptr i3)) = r → r = ok w → + ptr.val ≤ w.val ∧ w.val < ptr.val + 2 ^ bits.val := by + intro r hr hrw + rw [hrw] at hr + have h63 : (bits &&& 63#u64).val = bits.val := by + rw [UScalar.val_and, show (63#u64 : U64).val = 2 ^ 6 - 1 from rfl, Nat.and_two_pow_sub_one_eq_mod] + exact Nat.mod_eq_of_lt (by omega) + obtain ⟨i1, e1, h1⟩ : ∃ i1 : U64, (1#u64 <<< (bits &&& 63#u64) : Result U64) = ok i1 ∧ + i1.val = 2 ^ bits.val := by + have : (1#u64 <<< (bits &&& 63#u64) : Result U64) ⦃ z => z.val = 2 ^ bits.val ⦄ := by + step as ⟨z, hz1, hz2⟩ + rw [h63] at hz1 + have hlt : 2 ^ bits.val < 2 ^ 64 := Nat.pow_lt_pow_right (by decide) (by omega) + simpa [Nat.shiftLeft_eq, U64.size_def, U64.numBits, Nat.mod_eq_of_lt hlt] using hz1 + exact spec_existsA this + rw [hbits, bind_ok] at hr + simp only [lift, bind_ok] at hr + rw [e1, bind_ok] at hr + have e := congrArg UScalar.val (ok_inj_ff hr) + have hpos : 1 ≤ 2 ^ bits.val := Nat.one_le_two_pow + have hm : (core.num.U64.wrapping_sub i1 1#u64).val = 2 ^ bits.val - 1 := by + have hlt : 2 ^ bits.val < 2 ^ 64 := Nat.pow_lt_pow_right (by decide) (by omega) + have h1v : (1#u64 : U64).val = 1 := rfl + have hsz : UScalar.size UScalarTy.U64 = 2 ^ 64 := by simp [U64.size_def, U64.numBits] + rw [core.num.U64.wrapping_sub_val_eq, h1, h1v, hsz] + omega + have ha : (L.mem s (ibWordA t) &&& core.num.U64.wrapping_sub i1 1#u64).val < 2 ^ bits.val := by + rw [UScalar.val_and, hm, Nat.and_two_pow_sub_one_eq_mod] + exact Nat.mod_lt _ hpos + rw [wa_valP, Nat.mod_eq_of_lt (by omega)] at e + omega + split at hl + · exact addr _ rfl hl + · split at hl + · exact absurd (ok_inj_ff hl).symm hw + · split at hl + · exact addr _ rfl hl + · exact absurd (ok_inj_ff hl).symm hw + | _ => exact absurd (ok_inj_ff (show (ok 0#u64 : Result U64) = ok w from hl)).symm hw + +/-- **The tracked buffers still pinned after a step that keeps or nulls each buffer slot still cover the IPC +buffers**: a surviving thread's buffer is pinned by its own slot, whose frame is the heap object of the old pin. -/ +theorem BufCover.pinA {Bf : Set (U64 × Fl4SpecA64.Word)} (hH : HeapOK H) (hF : FrameOK st H OS s) + (C : BufCover st T Bf r σ') (hO : ∀ t ∈ T, sbWordA t ∈ OS) + (k : ∀ t ∈ T, capA st r (sbWordA t) = capA st s (sbWordA t) ∨ capA st r (sbWordA t) = .Null) + (pin : ∀ bp ∈ Bf, BufPinA st T OS s bp.1) : + BufCover st T {bp | bp ∈ Bf ∧ BufPinA st T OS r bp.1} r σ' := by + intro t ht x b sb w hx hb hl + obtain ⟨hm, h0⟩ := C t ht x b sb w hx hb hl + refine ⟨⟨hm, ?_⟩, h0⟩ + obtain ⟨ptr, size, rights, x1, x2, x3, x4, hc, hin⟩ := lookup_frameA hl h0 + have hcs : capA st s (sbWordA t) = .Frame ptr size rights false x1 x2 x3 x4 := by + rcases k t ht with e | e + · exact e.symm.trans hc + · rw [e] at hc; cases hc + obtain ⟨bits, hbits⟩ : ∃ bits, size.bits = ok bits := by cases size <;> exact ⟨_, rfl⟩ + have hp : H ptr = some (.other bits.val) := hF _ (hO t ht) ptr size rights x1 x2 x3 x4 hcs bits hbits + have fit : ptr.val + 2 ^ bits.val ≤ 2 ^ 64 := by + have := hH.fits ptr _ hp; simpa only [HKind.bits] using this + obtain ⟨w1, w2⟩ := hin bits hbits fit + obtain ⟨t', -, hO', ptr', size', rights', y1, y2, y3, y4, bits', hc', hbits', hw'⟩ := pin _ hm + have hp' : H ptr' = some (.other bits'.val) := hF _ hO' ptr' size' rights' y1 y2 y3 y4 hc' bits' hbits' + have e0 : bufW w 0#u64 = w := by + apply UScalar.eq_of_val_eq + unfold bufW + simp [core.num.U64.wrapping_add_val_eq, core.num.U64.wrapping_mul_val_eq] + obtain ⟨-, v1, v2⟩ := hw' 0#u64 (by simp) + rw [e0] at v1 v2 + have ep : ptr = ptr' := by + by_contra ne + have := hH.disj ptr _ ptr' _ hp hp' ne + simp only [HKind.bits] at this + omega + subst ep + have eb : bits.val = bits'.val := by + rw [hp] at hp' + injection hp' with h + injection h + refine ⟨t, ht, hO t ht, ptr, size, rights, x1, x2, x3, x4, bits, hc, hbits, fun j hj => ?_⟩ + rw [eb] + exact hw' j hj + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelCapKeepA.lean b/proofs/Fl4ProofsA64/P3/DelCapKeepA.lean new file mode 100644 index 000000000..4d2e3d166 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelCapKeepA.lean @@ -0,0 +1,225 @@ +/- +P3 (AArch64, delete hooks E4): **the cap-reading fields of the per-step record across the loop's slot steps**. + +The deletion loop's three slot steps change the `OS` caps in two shapes: + +* **`CapFromA OS s s'`** (every cap after is null, a zombie, or a cap some `OS` slot held before): the emptying + (`CapFromA.of_empty`), the zombie write (`CapFromA.of_zombie`) and the swap (`CapFromA.of_swap`). Every field that + only asks something of the untyped, frame, page-table, pool or window caps present is kept: `UtFreeA` shrinks + (`UtFreeA.fromA`), so `ObjAllocA`, `IdleAnchorOffA`, `UtBlk39A`, `SpecFreeA.heap`, `SpecZeroFreeA`; `DevFrDisjA`, + `SchedPageA` / `BufOffA`. +* **`CapToA OS s s'`** (every cap before is still held by some `OS` slot after, the swap: `CapToA.of_swap`): `DeadA` + is kept backwards (`DeadA.backA`), so every `DeadExtA` clause. + (Typed namers, contract §35.) + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.DelSlotWA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +variable (st) in +/-- Every `OS` cap after is null, a zombie, or a cap some `OS` slot held before. -/ +def CapFromA (OS : Set U64) (s s' : S) : Prop := + ∀ y ∈ OS, capA st s' y = .Null ∨ IsZombieA (capA st s' y) ∨ ∃ z ∈ OS, capA st s' y = capA st s z + +variable (st) in +/-- Every `OS` cap before is held by some `OS` slot after. -/ +def CapToA (OS : Set U64) (s s' : S) : Prop := + ∀ z ∈ OS, ∃ y ∈ OS, capA st s' y = capA st s z + +variable {OS : Set U64} {s s' : S} + +/-- A non-null, non-zombie cap after comes from a slot before. -/ +theorem CapFromA.src (h : CapFromA st OS s s') {y : U64} (hy : y ∈ OS) {c : capability.Cap} + (e : capA st s' y = c) (hn : c ≠ .Null) (hz : ¬ IsZombieA c) : ∃ z ∈ OS, capA st s z = c := by + rcases h y hy with h0 | h0 | ⟨z, hz', ez⟩ + · exact absurd (e.symm.trans h0) hn + · rw [e] at h0; exact absurd h0 hz + · exact ⟨z, hz', ez.symm.trans e⟩ + +theorem not_zombie_untypedA (q b fi : U64) (d : Bool) : ¬ IsZombieA (.Untyped q b fi d) := by + rintro ⟨_, _, _, h⟩; cases h + +/-- The emptying of `x`. -/ +theorem CapFromA.of_empty {x : U64} (h0 : capA st s' x = .Null) + (hcap : ∀ y ∈ OS, y ≠ x → capA st s' y = capA st s y) : CapFromA st OS s s' := fun y hy => by + by_cases e : y = x + · subst e; exact Or.inl h0 + · exact Or.inr (Or.inr ⟨y, hy, hcap y hy e⟩) + +/-- The zombie write at `x`. -/ +theorem CapFromA.of_zombie {x p zt n : U64} (h0 : capA st s' x = .Zombie p zt n) + (hcap : ∀ y ∈ OS, y ≠ x → capA st s' y = capA st s y) : CapFromA st OS s s' := fun y hy => by + by_cases e : y = x + · subst e; exact Or.inr (Or.inl ⟨p, zt, n, h0⟩) + · exact Or.inr (Or.inr ⟨y, hy, hcap y hy e⟩) + +/-- The swap of `x` and `p`. -/ +theorem CapFromA.of_swap {x p : U64} (hx : x ∈ OS) (hp : p ∈ OS) + (hcap : ∀ y ∈ OS, capA st s' y = if y = x then capA st s p else if y = p then capA st s x else capA st s y) : + CapFromA st OS s s' := fun y hy => by + refine Or.inr (Or.inr ?_) + rw [hcap y hy] + split_ifs + · exact ⟨p, hp, rfl⟩ + · exact ⟨x, hx, rfl⟩ + · exact ⟨y, hy, rfl⟩ + +theorem CapToA.of_swap {x p : U64} (hx : x ∈ OS) (hp : p ∈ OS) + (hcap : ∀ y ∈ OS, capA st s' y = if y = x then capA st s p else if y = p then capA st s x else capA st s y) : + CapToA st OS s s' := fun z hz => by + by_cases e1 : z = x + · subst e1 + refine ⟨p, hp, ?_⟩ + rw [hcap p hp] + by_cases e2 : p = z + · subst e2; simp + · simp [e2] + · by_cases e2 : z = p + · subst e2 + exact ⟨x, hx, by rw [hcap x hx]; simp⟩ + · exact ⟨z, hz, by rw [hcap z hz]; simp [e1, e2]⟩ + +/-! ## Forward fields -/ + +theorem CapFromA.untyped (h : CapFromA st OS s s') {y : U64} (hy : y ∈ OS) {q b fi : U64} {d : Bool} + (e : capA st s' y = .Untyped q b fi d) : ∃ z ∈ OS, capA st s z = .Untyped q b fi d := + h.src hy e (by intro h'; cases h') (not_zombie_untypedA q b fi d) + +theorem UtFreeA.fromA (h : CapFromA st OS s s') {dev : Bool → Prop} {a : Nat} (hf : UtFreeA st OS s' dev a) : + UtFreeA st OS s dev a := by + obtain ⟨u, hu, q, b, fi, d, hc, hd, h1, h2⟩ := hf + obtain ⟨z, hz, ez⟩ := h.untyped hu hc + exact ⟨z, hz, q, b, fi, d, ez, hd, h1, h2⟩ + +theorem ObjAllocA.fromA {Z Z' : XSetsA} {H : Heap} (h : CapFromA st OS s s') + (hJ : ∀ p bytes, JointObjectA Z' H p bytes → JointObjectA Z H p bytes) + (o : ObjAllocA (st := st) Z H OS s) : ObjAllocA (st := st) Z' H OS s' := by + intro p bytes hp u hu q b fi d hc + obtain ⟨z, hz, ez⟩ := h.untyped hu hc + exact o p bytes (hJ p bytes hp) z hz q b fi d ez + +/-- **`DevUtOffA` across a step whose untyped caps all come from before** (D1b-4). -/ +theorem DevUtOffA.fromA {Z Z' : XSetsA} {H : Heap} {ia ie : Nat} (h : CapFromA st OS s s') + (hk : L.ksb s' = L.ksb s) (eJ : ∀ p bytes, JointObjectA Z' H p bytes → JointObjectA Z H p bytes) + (eC : Z'.Cs = Z.Cs) (o : DevUtOffA st Z H OS ia ie s) : DevUtOffA st Z' H OS ia ie s' := by + refine ⟨fun u hu q b fi hc p bytes hj => ?_, fun u hu q b fi hc => ?_, fun u hu q b fi hc => ?_, + fun u hu q b fi hc v hv q' b' fi' hc' => ?_⟩ + · obtain ⟨z, hz, ez⟩ := h.untyped hu hc + exact o.obj z hz q b fi ez p bytes (eJ p bytes hj) + · obtain ⟨z, hz, ez⟩ := h.untyped hu hc + have hnb : ∀ c, L.nodeBase s' c = L.nodeBase s c := fun c => by simp [LawfulStore.nodeBase, hk] + have r := o.ks z hz q b fi ez + unfold RegionOffNodes at r ⊢ + rw [eC, hk] + exact ⟨fun c hcs => by rw [hnb c]; exact r.1 c hcs, r.2⟩ + · obtain ⟨z, hz, ez⟩ := h.untyped hu hc + exact o.idle z hz q b fi ez + · obtain ⟨z, hz, ez⟩ := h.untyped hu hc + obtain ⟨z', hz', ez'⟩ := h.untyped hv hc' + exact o.sep z hz q b fi ez z' hz' q' b' fi' ez' + +/-- `UtZombieA` across a step whose zombies all come from before. -/ +theorem UtZombieA.ofSrcA (h : UtZombieA st OS s) + (src : ∀ u ∈ OS, ∀ p t n, capA st s' u = .Zombie p t n → ∃ w ∈ OS, capA st s w = .Zombie p t n) : + UtZombieA st OS s' := fun u hu p t n e => by + obtain ⟨w, hw, ew⟩ := src u hu p t n e + exact h w hw p t n ew + +theorem IdleAnchorOffA.fromA (h : CapFromA st OS s s') (o : IdleAnchorOffA (st := st) OS s) : + IdleAnchorOffA (st := st) OS s' := by + intro u hu q b fi d hc + obtain ⟨z, hz, ez⟩ := h.untyped hu hc + exact o z hz q b fi d ez + +theorem UtBlk39A.fromA (h : CapFromA st OS s s') (o : UtBlk39A st OS s) : UtBlk39A st OS s' := by + intro u hu q b fi d hc + obtain ⟨z, hz, ez⟩ := h.untyped hu hc + exact o z hz q b fi d ez + +/-- `SpecFreeA`'s heap clause across a slot-only spec step. -/ +theorem SpecFreeA.heapFromA {σ σ' : Fl4SpecA64.State Unit} (h : CapFromA st OS s s') (HS : SpecSlotA σ σ') + (o : SpecFreeA st OS s σ) : + ∀ p : U64, UtFreeA st OS s' (fun _ => True) p.val → σ'.kheap (specPtr p) = none := by + intro p hf + have h0 := o.heap p (UtFreeA.fromA h hf) + rcases HS.kh (specPtr p) with e | ⟨r, cs, cs', e1, -⟩ | ⟨x, x', e1, -⟩ + · rw [e]; exact h0 + · rw [h0] at e1; cases e1 + · rw [h0] at e1; cases e1 + +theorem SpecZeroFreeA.fromA {σ σ' : Fl4SpecA64.State Unit} (h : CapFromA st OS s s') + (hm : σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory) (o : SpecZeroFreeA st OS s σ) : + SpecZeroFreeA st OS s' σ' := fun x hx => by + rw [hm]; exact o x (UtFreeA.fromA h hx) + +theorem DevFrDisjA.fromA {H : Heap} {σ σ' : Fl4SpecA64.State Unit} (h : CapFromA st OS s s') + (dp : ∀ p dev pg, σ.kheap p = some (.archObj (.dataPage dev pg)) → + σ'.kheap p = some (.archObj (.dataPage dev pg))) + (o : DevFrDisjA st H OS s σ) : DevFrDisjA st H OS s' σ' := by + intro y hy p size rights mapped ept asid va hc k hk + obtain ⟨z, hz, ez⟩ := h.src hy hc (by intro h'; cases h') (by rintro ⟨_, _, _, h'⟩; cases h') + obtain ⟨h1, dev, pg, h2⟩ := o z hz p size rights mapped ept asid va ez k hk + exact ⟨h1, dev, pg, dp _ _ _ h2⟩ + +/-- A page `SchedPageA` names after is one it named before. -/ +theorem SchedPageA.fromA {Z Z' : XSetsA} (h : CapFromA st OS s s') (e1 : Z'.Pp = Z.Pp) (e2 : Z'.S1 = Z.S1) + (e3 : Z'.T2 = Z.T2) (e4 : Z'.X = Z.X) (e5 : Z'.Y = Z.Y) {p : U64} (hp : SchedPageA st Z' OS s' p) : + SchedPageA st Z OS s p := by + unfold SchedPageA at hp ⊢ + rw [e1, e2, e3, e4, e5] at hp + rcases hp with hp | hp | hp | hp | hp | hp | hp | ⟨u, hu, hp⟩ + · exact Or.inl hp + · exact Or.inr (Or.inl hp) + · exact Or.inr (Or.inr (Or.inl hp)) + · exact Or.inr (Or.inr (Or.inr (Or.inl hp))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inl hp)))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl hp))))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl hp)))))) + · refine Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ?_)))))) + have nz : ∀ {c : capability.Cap}, capA st s' u = c → (c ≠ .Null) → ¬ IsZombieA c → + ∃ z ∈ OS, capA st s z = c := fun e hn hz => h.src hu e hn hz + rcases hp with ⟨m, a, v, e⟩ | ⟨m, a, e⟩ | ⟨a, v, e⟩ | ⟨q, a, v, e, ep⟩ | ⟨y, a, e, ep⟩ + · obtain ⟨z, hz, ez⟩ := nz e (by intro h'; cases h') (by rintro ⟨_, _, _, h'⟩; cases h') + exact ⟨z, hz, Or.inl ⟨m, a, v, ez⟩⟩ + · obtain ⟨z, hz, ez⟩ := nz e (by intro h'; cases h') (by rintro ⟨_, _, _, h'⟩; cases h') + exact ⟨z, hz, Or.inr (Or.inl ⟨m, a, ez⟩)⟩ + · obtain ⟨z, hz, ez⟩ := nz e (by intro h'; cases h') (by rintro ⟨_, _, _, h'⟩; cases h') + exact ⟨z, hz, Or.inr (Or.inr (Or.inl ⟨a, v, ez⟩))⟩ + · obtain ⟨z, hz, ez⟩ := nz e (by intro h'; cases h') (by rintro ⟨_, _, _, h'⟩; cases h') + exact ⟨z, hz, Or.inr (Or.inr (Or.inr (Or.inl ⟨q, a, v, ez, ep⟩)))⟩ + · obtain ⟨z, hz, ez⟩ := nz e (by intro h'; cases h') (by rintro ⟨_, _, _, h'⟩; cases h') + exact ⟨z, hz, Or.inr (Or.inr (Or.inr (Or.inr ⟨y, a, ez, ep⟩)))⟩ + +theorem BufOffA.fromA {Z Z' : XSetsA} {Bf : Set (U64 × Fl4SpecA64.Word)} (h : CapFromA st OS s s') + (hk : L.ksb s' = L.ksb s) (e1 : Z'.Pp = Z.Pp) (e2 : Z'.S1 = Z.S1) (e3 : Z'.T2 = Z.T2) (e4 : Z'.X = Z.X) + (e5 : Z'.Y = Z.Y) (e6 : Z'.V = Z.V) (o : BufOffA st Z OS s Bf) + (pin : ∀ bp ∈ Bf, BufPinA st Z'.T OS s' bp.1) : BufOffA st Z' OS s' Bf := + ⟨o.os, fun b hb => by rw [e1, e2, e5, hk]; exact o.vs b hb, + fun b hb p hp => o.page b hb p (SchedPageA.fromA h e1 e2 e3 e4 e5 hp), + fun b hb => by rw [e6]; exact o.vcw b hb, pin⟩ + +/-! ## Backward fields -/ + +/-- An object no cap names after was named by no cap before (every cap before is still held). -/ +theorem DeadA.backA (h : CapToA st OS s s') {k : DKindA} {p : U64} (hd : DeadA st OS k s' p) : + DeadA st OS k s p := by + intro u hu hn + obtain ⟨y, hy, ey⟩ := h u hu + exact hd y hy (by rw [ey]; exact hn) + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelClockA.lean b/proofs/Fl4ProofsA64/P3/DelClockA.lean new file mode 100644 index 000000000..c089ced5d --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelClockA.lean @@ -0,0 +1,199 @@ +/- +P3 (AArch64, delete hooks, contract §37 item 4 / §39): **the six VSpace finaliser hooks at `W ∧ DelPpTimeA`** +(`W` the `DelXA6` loop record). + +The concrete finaliser keeps the clock relation (`FinHookKA.timeA`'s `hA`) by the hook itself, run at the arch relation +`AR' a h := AR a h ∧ Clock a σ.machineState` for the entry's `σ`: `ArchKeepsA` at `AR'` is `ArchKeepsA.andA K +(DelArchA.clockK)`, `DelArchA` at `AR'` from the `Clock` fields, the record at `AR'` by `JStage3TA.setArA`, and the +post's record gives `AR'` at the final arch state (**`FinHookKA.clockOfA`**). The spec side is `finTimeSpec_vsA`. + +* **`finHookFrameTA`**, **`finHookPtTA`**, **`finHookPml4TA`**, **`finHookHighTA`**, **`finHookEptTA`**, + **`finHookPoolTA`**. + +Named premises: the hooks' own, and [P] `DelArchA`'s `Clock` fields (§36 ruling, "`Clock` in `DelArchA`"). New module. +-/ +import Fl4ProofsA64.P3.DelTimeSpecA +import Fl4ProofsA64.P3.FinHookFrameA +import Fl4ProofsA64.P3.FinHookPtA +import Fl4ProofsA64.P3.FinHookRootKA +import Fl4ProofsA64.P3.FinHookPoolA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Sched Fl4Spec + Fl4SpecA64 + +noncomputable section + +section +variable {m : Fl4SpecA64.McState} +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s : S} {a : A} {σ : State Unit} {m : McState} + +/-- **`JStage3TA` at another arch relation holding at the record's own arch state.** -/ +theorem JStage3TA.setArA {AR' : A → HwRootA → Prop} + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) (h : AR' a Z.hw) : + JStage3TA st AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m := + { J with j := ⟨{ J.j.1 with ar := h }, J.j.2⟩ } + +/-- **The loop invariant with a fact about the arch state added to the arch relation.** -/ +theorem DelJWQA.andArA {C : A → Prop} {s0 : S} {G : MdbGhostA} + (W : DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m J5 J7) s a σ G) (hC : C a) : + DelJWQA st s0 (DelXA6G st Ag (fun a h => AR a h ∧ C a) Z H OS U Pp Tb Lv ia ie N E Rp κ n m J5 J7) s a σ G := by + obtain ⟨sc, ⟨Q, R, hw, Ac, Bf, stack, J, X⟩, h⟩ := W + exact ⟨sc, ⟨Q, R, hw, Ac, Bf, stack, J.setArA ⟨J.j.1.ar, hC⟩, X⟩, h⟩ + +/-- `DelBufOffA` at a stronger arch relation. -/ +theorem DelBufOffA.andArA {C : A → Prop} + (h : DelBufOffA st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n J5 J7) : + DelBufOffA st (fun a h => AR a h ∧ C a) Z H OS U Pp Tb Lv ia ie N E Rp κ n J5 J7 := + fun Q R hw G Ac Bf stack m s a σ J => h Q R hw G Ac Bf stack m s a σ (J.mapARA fun _ _ h => h.1) + +/-- **`DelArchA` at `AR ∧ Clock · ms`.** -/ +theorem DelArchA.andClockA {ar : machine.Arch A} {Clock : A → MachineState → Prop} (Da : DelArchA st ar AR Clock) + (ms : MachineState) : DelArchA st ar (fun a h => AR a h ∧ Clock a ms) Clock := + { Da with + fpuRelease := fun a h t hh => WP.spec_and (Da.fpuRelease a h t hh.1) (Da.clockFpu a ms t hh.2) + vmRoot := fun s a h t hh => WP.spec_mono (WP.spec_and (Da.vmRoot s a h t hh.1) (Da.clockVm s a ms t hh.2)) + fun _ ⟨⟨h', e⟩, c⟩ => ⟨h', e, c⟩ + arIrqMask := fun a h i b hh => WP.spec_and (Da.arIrqMask a h i b hh.1) (Da.clockIrqMask a ms i b hh.2) + arIrqFree := fun a h i hh => WP.spec_and (Da.arIrqFree a h i hh.1) (Da.clockIrqFree a ms i hh.2) } + +variable {th : TypedHeapA} + +/-- **The concrete finaliser keeps a fact about the arch state** that a hook at `AR ∧ C` gives back (module +docstring). -/ +theorem FinHookKA.clockOfA {ar : machine.Arch A} {k : FinKindA} {Fn : FinFnA} {C : A → Prop} {s0 : S} + {J7x : JSlotA S} {Rd Rz : S → A → State Unit → MdbGhostA → U64 → Prop} + (H' : FinHookKA st k ar Z.th OS Fn + (DelJWQA st s0 (DelXA6G st Ag (fun a h => AR a h ∧ C a) Z H OS U Pp Tb Lv ia ie N E Rp κ n m J5 J7)) + (DelJWQA st s0 (DelXA6G st Ag (fun a h => AR a h ∧ C a) Z H OS U Pp Tb Lv ia ie N E Rp κ n m J5 J7x)) Rd Rz) + (s : S) (a : A) (σ : State Unit) (G : MdbGhostA) (sl : CslotPtr) (c : capability.Cap) (ac : Cap) (f : Bool) + (D : DStateA st Z.th OS s σ G) + (W : DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m J5 J7) s a σ G) (hsl : AbsSlotA Z.th sl) + (hc : capA st s (cslotAddrA sl) = c) (hk : finKindA c = some k) (hg : getCap (Z := Unit) sl σ = ({(ac, σ)}, False)) + (hac : CapEqA c ac) (hf : NeedsFinCA c → (f = true ↔ isFinalCap' ac σ)) (hC : C a) : + objects.finalise_cap st ar s a c f false ⦃ r => C r.2.2 ⦄ := by + refine WP.spec_mono (H' s a σ G sl c ac f D (W.andArA hC) hsl hc hk hg hac hf) ?_ + rintro r ⟨-, -, -, σ', G', -, -, -, -, ⟨hn, hx⟩, -, -⟩ + by_cases hr : RmOKA r.1.1 (cslotAddrA sl) + · obtain ⟨_, h⟩ := (hx hr).2.toDelXA.arA; exact h.2 + · obtain ⟨_, h⟩ := (hn hr).2.toDelXA.arA; exact h.2 + +variable {ar : machine.Arch A} {Clock : A → MachineState → Prop} + +/-- **The Frame hook at `W ∧ DelPpTimeA`**. -/ +theorem finHookFrameTA [PlainStore st] (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) (K : ArchKeepsA ar AR) + (Da : DelArchA st ar AR Clock) (hIpa : ∀ a, S2IpaE ar a) (s0 : S) + (hT : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s0)) + (hbf : DelBufOffA st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n (J5A st) (J7LA st (J7R2A st ar hPl.ipa))) + (Rv : S → A → State Unit → MdbGhostA → U64 → Prop) : + FinHookKA st .frame ar Z.th OS fl4FinaliseCap + (fun s a σ G => DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) + (J7LA st (J7R2A st ar hPl.ipa))) s a σ G ∧ DelPpTimeA Clock a σ) + (fun s a σ G => DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) + (J7LA st J7R2tA)) s a σ G ∧ DelPpTimeA Clock a σ) + (DelRdXA st ar Z H OS hPl.ipa Rv) (DelRzA st) := by + refine FinHookKA.timeA (finHookFrameA hA hPl K hIpa s0 hT hbf Rv) (finTimeSpec_vsA (by simp)) Da.clockFr ?_ + intro s a σ G sl c ac f D hW hsl hc hk hg hac hf hCl + exact FinHookKA.clockOfA (C := fun a => Clock a σ.machineState) + (finHookFrameA hA hPl (K.andA (Da.clockK σ.machineState)) hIpa s0 hT hbf.andArA Rv) + s a σ G sl c ac f D hW hsl hc hk hg hac hf hCl + +/-- **The PageTable hook at `W ∧ DelPpTimeA`**. -/ +theorem finHookPtTA (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) (K : ArchKeepsA ar AR) + (Da : DelArchA st ar AR Clock) (hIpa : ∀ a, S2IpaE ar a) (s0 : S) + (hT : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s0)) + (hbf : DelBufOffA st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n (J5A st) (J7LA st (J7R2A st ar hPl.ipa))) + (Rv : S → A → State Unit → MdbGhostA → U64 → Prop) : + FinHookKA st .pageTable ar Z.th OS fl4FinaliseCap + (fun s a σ G => DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) + (J7LA st (J7R2A st ar hPl.ipa))) s a σ G ∧ DelPpTimeA Clock a σ) + (fun s a σ G => DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) + (J7LA st J7R2tA)) s a σ G ∧ DelPpTimeA Clock a σ) + (DelRdXA st ar Z H OS hPl.ipa Rv) (DelRzA st) := by + refine FinHookKA.timeA (finHookPtA hA hPl K hIpa s0 hT hbf Rv) (finTimeSpec_vsA (by simp)) Da.clockFr ?_ + intro s a σ G sl c ac f D hW hsl hc hk hg hac hf hCl + exact FinHookKA.clockOfA (C := fun a => Clock a σ.machineState) + (finHookPtA hA hPl (K.andA (Da.clockK σ.machineState)) hIpa s0 hT hbf.andArA Rv) + s a σ G sl c ac f D hW hsl hc hk hg hac hf hCl + +/-- **The Pml4 hook at `W ∧ DelPpTimeA`**. -/ +theorem finHookPml4TA (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) (K : ArchKeepsA ar AR) + (Da : DelArchA st ar AR Clock) (s0 : S) + (hT : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s0)) + (hbf : DelBufOffA st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n (J5A st) (J7LA st (J7R2A st ar hPl.ipa))) + (Rv : S → A → State Unit → MdbGhostA → U64 → Prop) : + FinHookKA st .pml4 ar Z.th OS fl4FinaliseCap + (fun s a σ G => DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) + (J7LA st (J7R2A st ar hPl.ipa))) s a σ G ∧ DelPpTimeA Clock a σ) + (fun s a σ G => DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) + (J7LA st J7R2tA)) s a σ G ∧ DelPpTimeA Clock a σ) + (DelRdXA st ar Z H OS hPl.ipa Rv) (DelRzA st) := by + refine FinHookKA.timeA (finHookPml4A hA hPl K Da s0 hT hbf Rv) (finTimeSpec_vsA (by simp)) Da.clockFr ?_ + intro s a σ G sl c ac f D hW hsl hc hk hg hac hf hCl + exact FinHookKA.clockOfA (C := fun a => Clock a σ.machineState) + (finHookPml4A hA hPl (K.andA (Da.clockK σ.machineState)) (Da.andClockA σ.machineState) s0 hT hbf.andArA Rv) + s a σ G sl c ac f D hW hsl hc hk hg hac hf hCl + +/-- **The HighVSpace hook at `W ∧ DelPpTimeA`**. -/ +theorem finHookHighTA (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) (K : ArchKeepsA ar AR) + (Da : DelArchA st ar AR Clock) (s0 : S) + (hT : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s0)) + (hbf : DelBufOffA st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n (J5A st) (J7LA st (J7R2A st ar hPl.ipa))) + (Rv : S → A → State Unit → MdbGhostA → U64 → Prop) : + FinHookKA st .highVSpace ar Z.th OS fl4FinaliseCap + (fun s a σ G => DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) + (J7LA st (J7R2A st ar hPl.ipa))) s a σ G ∧ DelPpTimeA Clock a σ) + (fun s a σ G => DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) + (J7LA st J7R2tA)) s a σ G ∧ DelPpTimeA Clock a σ) + (DelRdXA st ar Z H OS hPl.ipa Rv) (DelRzA st) := by + refine FinHookKA.timeA (finHookHighA hA hPl K Da s0 hT hbf Rv) (finTimeSpec_vsA (by simp)) Da.clockFr ?_ + intro s a σ G sl c ac f D hW hsl hc hk hg hac hf hCl + exact FinHookKA.clockOfA (C := fun a => Clock a σ.machineState) + (finHookHighA hA hPl (K.andA (Da.clockK σ.machineState)) (Da.andClockA σ.machineState) s0 hT hbf.andArA Rv) + s a σ G sl c ac f D hW hsl hc hk hg hac hf hCl + +/-- **The Ept hook at `W ∧ DelPpTimeA`**. -/ +theorem finHookEptTA (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) (K : ArchKeepsA ar AR) + (Da : DelArchA st ar AR Clock) (s0 : S) + (hT : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s0)) + (hbf : DelBufOffA st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n (J5A st) (J7LA st (J7R2A st ar hPl.ipa))) + (Rv : S → A → State Unit → MdbGhostA → U64 → Prop) : + FinHookKA st .ept ar Z.th OS fl4FinaliseCap + (fun s a σ G => DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) + (J7LA st (J7R2A st ar hPl.ipa))) s a σ G ∧ DelPpTimeA Clock a σ) + (fun s a σ G => DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) + (J7LA st J7R2tA)) s a σ G ∧ DelPpTimeA Clock a σ) + (DelRdXA st ar Z H OS hPl.ipa Rv) (DelRzA st) := by + refine FinHookKA.timeA (finHookEptA hA hPl K Da s0 hT hbf Rv) (finTimeSpec_vsA (by simp)) Da.clockFr ?_ + intro s a σ G sl c ac f D hW hsl hc hk hg hac hf hCl + exact FinHookKA.clockOfA (C := fun a => Clock a σ.machineState) + (finHookEptA hA hPl (K.andA (Da.clockK σ.machineState)) (Da.andClockA σ.machineState) s0 hT hbf.andArA Rv) + s a σ G sl c ac f D hW hsl hc hk hg hac hf hCl + +/-- **The AsidPool hook at `W ∧ DelPpTimeA`**. -/ +theorem finHookPoolTA (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) (K : ArchKeepsA ar AR) + (Da : DelArchA st ar AR Clock) (s0 : S) (hT : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s0)) + (hYAs : ∀ y ∈ Z.Y, ∀ j : U64, j.val < 512 → ¬ AsW (L.ksb s0) (peAF y j)) + (Rv : S → A → State Unit → MdbGhostA → U64 → Prop) : + FinHookKA st .asidPool ar Z.th OS fl4FinaliseCap + (fun s a σ G => DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) + (J7LA st (J7R2A st ar hPl.ipa))) s a σ G ∧ DelPpTimeA Clock a σ) + (fun s a σ G => DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) + (J7LA st J7R2tA)) s a σ G ∧ DelPpTimeA Clock a σ) + (DelRdXA st ar Z H OS hPl.ipa Rv) (DelRzA st) := by + refine FinHookKA.timeA (finHookPoolA hA hPl K Da s0 hT hYAs Rv) (finTimeSpec_vsA (by simp)) Da.clockFr ?_ + intro s a σ G sl c ac f D hW hsl hc hk hg hac hf hCl + exact FinHookKA.clockOfA (C := fun a => Clock a σ.machineState) + (finHookPoolA hA hPl (K.andA (Da.clockK σ.machineState)) (Da.andClockA σ.machineState) s0 hT hYAs Rv) + s a σ G sl c ac f D hW hsl hc hk hg hac hf hCl + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelCtOffA.lean b/proofs/Fl4ProofsA64/P3/DelCtOffA.lean new file mode 100644 index 000000000..e05443b2e --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelCtOffA.lean @@ -0,0 +1,226 @@ +/- +P3 (AArch64, delete hooks E4): **container caps' slots off the IRQ node across the loop's three slot steps, and the +IRQ node off both slots of the swap**. + +The loop record carries A64 P1's `CtOff` (a CNode cap's slots and a zombie's first `n` slots are no IRQ-node slot; at +loop entry it is `SysL.ct`). + +* **`ctOff_emptyA`**, **`ctOff_swapA`**, **`ctOff_zombieA`** (the written zombie's slots: in a TCB by placement, in a + CNode from the replaced CNode / zombie cap's `CtOff`). +* **`offNode_homeA`**: the zombie's home slot is in no IRQ-node CNode (`CtOff`, `1 ≤ n`). +* **`offNode_slotA`**: a slot holding a zombie is in no IRQ-node CNode (`SpecIrqA.slot`). + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.CnodeDelInvA +import Fl4ProofsA64.P3.SpecFrA +import Fl4ProofsA64.P3.JointA +import Fl4ProofsA64.P1.MdbCtOff + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {OS : Set U64} {th : TypedHeapA} {s r : S} {σ σ' : State Unit} {G G' : MdbGhostA} + +variable (st) in +/-- `CtOff` across a step that keeps the state-block base and every slot's cap. -/ +theorem ctOff_sameA (h : CtOff st (L.ksb s) OS s) (hk : L.ksb r = L.ksb s) + (k : ∀ y ∈ OS, capA st r y = capA st s y) : CtOff st (L.ksb r) OS r := fun y hy => by + rw [hk, k y hy]; exact h y hy + +theorem ctOff_emptyA {x : U64} (h : CtOff st (L.ksb s) OS s) (hk : L.ksb r = L.ksb s) (h0 : capA st r x = .Null) + (hcap : ∀ y ∈ OS, y ≠ x → capA st r y = capA st s y) : CtOff st (L.ksb r) OS r := fun y hy => by + rw [hk] + by_cases e : y = x + · subst e; rw [h0]; exact CtOffP.null _ + · rw [hcap y hy e]; exact h y hy + +theorem ctOff_swapA {x p : U64} (h : CtOff st (L.ksb s) OS s) (hk : L.ksb r = L.ksb s) (hx : x ∈ OS) (hp : p ∈ OS) + (hcap : ∀ y ∈ OS, capA st r y = if y = x then capA st s p else if y = p then capA st s x else capA st s y) : + CtOff st (L.ksb r) OS r := fun y hy => by + rw [hk, hcap y hy] + split_ifs + · exact h p hp + · exact h x hx + · exact h y hy + +/-- No slot of a TCB of `th` is an IRQ-node slot. -/ +theorem tcb_slot_off_nodeA {kb q i : U64} (P : PlaceA th kb) (hq : th q = some .tcb) (hi : i.val < tcbSlotsA) : + ¬ IslN kb (slotIdx q i) := by + rintro ⟨a, ha, e⟩ + have hf := P.fit q _ hq + simp only [ObjKindA.size] at hf + have hi7 : i.val < 7 := hi + have hv : (slotIdx q i).val = q.val + 32 * i.val := by + unfold slotIdx + simp [core.num.U64.wrapping_add_val_eq, core.num.U64.wrapping_mul_val_eq, U64.size_def, U64.numBits]; omega + have hn := P.irq a ha + rw [← e] at hn + by_cases eq : slotIdx q i = q + · rw [eq, hq] at hn; cases hn + · rcases P.apart _ _ _ _ hn hq eq with h | h + · simp only [ObjKindA.size] at h; omega + · simp only [ObjKindA.size] at h; omega + +/-- **`CtOff` across the zombie write** at `x`. -/ +theorem ctOff_zombieA {x q zt n : U64} (h : CtOff st (L.ksb s) OS s) (hk : L.ksb r = L.ksb s) + (P : PlaceA th (L.ksb s)) (hx : x ∈ OS) (new : capA st r x = .Zombie q zt n) + (z : ZOKA th q zt n) (cv : CapValidA th (capA st s x)) + (same : ∀ p' zt' n', capA st s x = .Zombie p' zt' n' → p' = q ∧ zt' = zt ∧ n.val ≤ n'.val) + (old : ¬ IsZombieA (capA st s x) → + (∃ t, capA st s x = .Thread t) ∨ ∃ q rb gs g, capA st s x = .CNode q rb gs g) + (cov : ¬ IsZombieA (capA st s x) → ptrA (capA st r x) = ptrA (capA st s x)) + (hcap : ∀ y ∈ OS, y ≠ x → capA st r y = capA st s y) : CtOff st (L.ksb r) OS r := fun y hy => by + rw [hk] + by_cases e : y = x + swap + · rw [hcap y hy e]; exact h y hy + subst e + rw [new] + refine ⟨fun _ _ _ _ e' => (by cases e'), fun p t k e' i hi => ?_⟩ + injection e' with e1 e2 e3 + subst e1 e2 e3 + rcases z with ⟨-, hq, hn⟩ | ⟨-, hq, hn⟩ + · exact tcb_slot_off_nodeA P hq (Nat.lt_of_lt_of_le hi hn) + by_cases hz : IsZombieA (capA st s y) + · obtain ⟨p', zt', n', ez⟩ := hz + obtain ⟨e1, -, le⟩ := same p' zt' n' ez + have := h y hy + rw [ez, e1] at this + exact this.2 _ _ _ rfl i (Nat.lt_of_lt_of_le hi le) + have c := cov hz + rw [new] at c + have co := h y hy + rcases old hz with ⟨t', et⟩ | ⟨q', rb, gs, g, et⟩ + · rw [et] at c cv + have eq : q = t' := by + have a : ptrA (.Zombie q zt n) = q := + ok_inj_ff ((ptrA_eq _).symm.trans (by first | rfl | simp [capability.Cap.ptr])) + have b : ptrA (.Thread t') = t' := + ok_inj_ff ((ptrA_eq _).symm.trans (by first | rfl | simp [capability.Cap.ptr])) + exact a.symm.trans (c.trans b) + subst eq + have : th q = some .tcb := cv + rw [hq] at this; cases this + · rw [et] at c cv co + have eq : q = q' := by + have a : ptrA (.Zombie q zt n) = q := + ok_inj_ff ((ptrA_eq _).symm.trans (by first | rfl | simp [capability.Cap.ptr])) + have b : ptrA (.CNode q' rb gs g) = q' := + ok_inj_ff ((ptrA_eq _).symm.trans (by first | rfl | simp [capability.Cap.ptr])) + exact a.symm.trans (c.trans b) + subst eq + have hr : th q = some (.cnode rb.val) := cv.1 + have ez : zt.val = rb.val := by + rw [hq] at hr; injection hr with hr; injection hr + have hf := P.fit q _ hq + simp only [ObjKindA.size] at hf + have hlt : rb.val < 64 := by + by_contra hc + have : 2 ^ 64 < 2 ^ (zt.val + 5) := Nat.pow_lt_pow_right (by decide) (by omega) + omega + refine co.1 _ _ _ _ rfl i ?_ + rw [Nat.mod_eq_of_lt hlt, ← ez] + exact Nat.lt_of_lt_of_le hi hn + +/-! ## The IRQ node off the swapped slots -/ + +/-- **The zombie's home slot is in no IRQ-node CNode.** -/ +theorem offNode_homeA {kb p zt n : U64} {mem : U64 → U64} (I : IrqRelA mem kb σ) + (h : CtOffP kb (.Zombie p zt n)) (hn : 1 ≤ n.val) : OffNodeA σ.interruptIrqNode (specPtr p) := by + intro irq hirq e + have hi : irq.toNat < 5376 := hirq + have hv : (⟨BitVec.ofNat 64 irq.toNat⟩ : U64).val = irq.toNat := by + show (BitVec.ofNat 64 irq.toNat).toNat = _ + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + have hb : (BitVec.ofNat 13 (⟨BitVec.ofNat 64 irq.toNat⟩ : U64).val) = irq := by + rw [hv]; apply BitVec.eq_of_toNat_eq; rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + have hnode := I.node ⟨BitVec.ofNat 64 irq.toNat⟩ (by rw [hv]; exact hi) + rw [hb] at hnode + have ep := specPtr_inj (e.trans hnode) + have h0 : slotIdx p 0#u64 = p := by + unfold slotIdx + apply UScalar.eq_of_val_eq + have := p.hBounds + simp [core.num.U64.wrapping_add_val_eq, core.num.U64.wrapping_mul_val_eq, U64.size_def, U64.numBits] + all_goals (simp [U64.numBits] at this; try omega) + refine h.2 _ _ _ rfl 0#u64 (by have e0 : (0#u64 : U64).val = 0 := rfl; omega) ?_ + rw [h0] + exact ⟨_, by rw [hv]; exact hi, ep⟩ + +/-- The caps the deletion loop writes a zombie over. -/ +def ContCapA (c : capability.Cap) : Prop := + (∃ p zt n, c = .Zombie p zt n) ∨ (∃ t, c = .Thread t) ∨ ∃ q rb gs g, c = .CNode q rb gs g + +/-- **A slot holding a zombie, thread or CNode cap is in no IRQ-node CNode** (`SpecIrqA.slot`). -/ +theorem offNode_contA {kb : U64} {sl : CslotPtr} (P : PlaceA th kb) (R : StateRelA th (L.mem s) σ) + (I : IrqRelA (L.mem s) kb σ) (J : SpecIrqA σ) (hA : AbsSlotA th sl) + (hz : ContCapA (capA st s (cslotAddrA sl))) : OffNodeA σ.interruptIrqNode sl.1 := by + intro irq hirq e + have hi : irq.toNat < 5376 := hirq + have hv : (⟨BitVec.ofNat 64 irq.toNat⟩ : U64).val = irq.toNat := by + show (BitVec.ofNat 64 irq.toNat).toNat = _ + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + have hb : (BitVec.ofNat 13 (⟨BitVec.ofNat 64 irq.toNat⟩ : U64).val) = irq := by + rw [hv]; apply BitVec.eq_of_toNat_eq; rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + have hnode := I.node ⟨BitVec.ofNat 64 irq.toNat⟩ (by rw [hv]; exact hi) + rw [hb] at hnode + have hth := P.irq ⟨BitVec.ofNat 64 irq.toNat⟩ (by rw [hv]; exact hi) + obtain ⟨q, eq, hq⟩ := hA + have eqq : q = irqSlotA kb ⟨BitVec.ofNat 64 irq.toNat⟩ := specPtr_inj (eq.symm.trans (e.trans hnode)) + rw [← eqq] at hth + have hlen : sl.2.length = 0 := by + rcases hq with hq | ⟨hq, -⟩ + · rw [hth] at hq + injection hq with hq; injection hq with hq; exact hq.symm + · rw [hth] at hq; cases hq + obtain ⟨cs, hk, -, hs⟩ := R.cnode q 0 hth + obtain ⟨ac, hac, hr⟩ := hs sl.2 hlen + have ha : slotAddrA (specPtr q) (Fl4SpecA64.ofBl sl.2) = cslotAddrA sl := by unfold cslotAddrA; rw [eq] + rw [ha, capAtA_eq (st := st)] at hr + have hk' : σ.kheap (σ.interruptIrqNode irq) = some (.cnode 0 cs) := by rw [← e, eq]; exact hk + have hI := J.slot irq hirq _ sl.2 ac hk' hac + have h2 := hr.2 + generalize capA st s (cslotAddrA sl) = c at hz h2 + rcases hz with ⟨_, _, _, rfl⟩ | ⟨_, rfl⟩ | ⟨_, _, _, _, rfl⟩ <;> + (have h3 : absCapA _ = ac := h2 + rcases hI with h | ⟨_, _, _, h⟩ <;> rw [h] at h3 <;> simp [absCapA] at h3) + +theorem offNode_slotA {kb p zt n : U64} {sl : CslotPtr} (P : PlaceA th kb) (R : StateRelA th (L.mem s) σ) + (I : IrqRelA (L.mem s) kb σ) (J : SpecIrqA σ) (hA : AbsSlotA th sl) + (hz : capA st s (cslotAddrA sl) = .Zombie p zt n) : OffNodeA σ.interruptIrqNode sl.1 := + offNode_contA P R I J hA (Or.inl ⟨p, zt, n, hz⟩) + +/-! ## The spec scheduler facts across the zombie write -/ + +/-- **`SInvXA` across the zombie `set_cap`.** -/ +theorem sinv_zombieA {kb p zt n : U64} {sl : CslotPtr} {m : McState} {k : Nat} (h : SInvXA σ m k) + (P : PlaceA th kb) (R : StateRelA th (L.mem s) σ) (I : IrqRelA (L.mem s) kb σ) (hA : AbsSlotA th sl) + (hz : ContCapA (capA st s (cslotAddrA sl))) {r : Unit × State Unit} + (hr : r ∈ (Fl4SpecA64.setCap (Z := Unit) (absCapA (.Zombie p zt n)) sl σ).1) : SInvXA r.2 m k := by + have off := offNode_contA P R I h.irqN hA hz + have ok : CapOkA σ.interruptIrqNode sl (absCapA (.Zombie p zt n)) := + ⟨fun irq hi e => absurd e (off irq hi), fun ⟨_, _, _, _, e⟩ => by simp [absCapA] at e⟩ + exact h.of_fr (setCap_ndr _ _ σ r hr) (setCap_irq ok σ rfl h.irqN r hr).2 + +/-- A CNode cap is never replaced by a TCB zombie (typing). -/ +theorem cnode_not_tcbZombieA {q q' zt n : U64} {rb : U64} (z : ZOKA th q zt n) (cv : th q' = some (.cnode rb.val)) + (e : q = q') : zt ≠ capability.ZOMBIE_TCB := by + intro h + have h65 : zt.val = 65 := by + rw [h]; first | rfl | decide | (unfold capability.ZOMBIE_TCB; rfl) + rcases z with ⟨-, hq, -⟩ | ⟨le, -, -⟩ + · rw [e, cv] at hq; cases hq + · omega + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelEpHookA.lean b/proofs/Fl4ProofsA64/P3/DelEpHookA.lean new file mode 100644 index 000000000..b76893bc5 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelEpHookA.lean @@ -0,0 +1,133 @@ +/- +P3 (AArch64, delete hooks W3): **the Endpoint finaliser hook** `finHookEpA_atA`, in the `W` / `Wx` / `Rd` / `Rz` of the +other kinds. The cap's typing `H p = some .ep` is the record's `capTy`. + +Premises: [P] `hCs` (the cores of `Z.Cs` are below `numNodes`), `hmb`, `hmax`, `htu`; [S] `h1` +(`numNodes = 1`, the single-core refinement view: the waiters' locality follows from the record, +`JStage3TA.onCore_single`). New module. +-/ +import Fl4ProofsA64.P3.DelEpRecA +import Fl4ProofsA64.P3.DelNtfnHookA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 Fl4ProofsA64.P3.Endp +open Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Notif + +local notation "wa" => core.num.U64.wrapping_add + +noncomputable section + +section +variable {m : Fl4SpecA64.McState} +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Pp Tb : Set U64} {Lv : U64 → Nat} {ia ie : Nat} + {N E Rp : Set U64} {κ : U64 → U64} {n : Nat} + +/-- **The Endpoint finaliser hook at a state** (module docstring). -/ +theorem finHookEpA_atA {ipa : U64} (s0 : S) + (Rv : S → A → State Unit → MdbGhostA → U64 → Prop) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {s : S} {a : A} {σ : State Unit} {G : MdbGhostA} {sl : CslotPtr} {p b0 : U64} {b1 b2 b3 b4 : Bool} + {ac : Fl4SpecA64.Cap} {f : Bool} + (D : DStateA st Z.th OS s σ G) + (W : DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st (J7R2A st ar ipa))) s a σ G) + (hsl : AbsSlotA Z.th sl) (hc : capA st s (cslotAddrA sl) = .Endpoint p b0 b1 b2 b3 b4) + (hg : getCap (Z := Unit) sl σ = ({(ac, σ)}, False)) (hac : CapEqA (.Endpoint p b0 b1 b2 b3 b4) ac) + (hf : f = true ↔ isFinalCap' ac σ) (h1 : fl4A64Platform.numNodes = 1) : + objects.finalise_cap st ar s a (.Endpoint p b0 b1 b2 b3 b4) f false ⦃ r => + r.1.1 = .Null ∧ CleanCapA r.1.2 ∧ CapWfA r.1.2 ∧ + ∃ σ' G', (∃ d : Bool, (d = true ↔ isFinalCap' ac σ) ∧ + ((absCapA r.1.1, absCapA r.1.2), σ') ∈ (fl4FinaliseCap (Z := Unit) ac d σ).1) ∧ + DStateA st Z.th OS r.2.1 σ' G' ∧ capA st r.2.1 (cslotAddrA sl) = .Endpoint p b0 b1 b2 b3 b4 ∧ + (∀ y ∈ OS, y ≠ cslotAddrA sl → capA st r.2.1 y = capA st s y ∨ + (capA st r.2.1 y = .Null ∧ ¬ IsZombieA (capA st s y))) ∧ + WFA (DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st (J7R2A st ar ipa)))) + (DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st J7R2tA))) + r.1.1 (cslotAddrA sl) r.2.1 r.2.2 σ' G' ∧ + (RmOKA r.1.1 (cslotAddrA sl) → DelRdXA st ar Z H OS ipa Rv r.2.1 r.2.2 σ' G' (cslotAddrA sl)) ∧ + (∀ q zt k, r.1.1 = .Zombie q zt k → DelRzA st r.2.1 r.2.2 σ' G' (cslotAddrA sl)) ⦄ := by + have eac : ac = absCapA (.Endpoint p b0 b1 b2 b3 b4) := hac.symm + obtain ⟨hcore, ⟨Q, R, hw, Ac, Bf, stack, J, X⟩, hPA, hZT, hBH, hCT, hFx⟩ := W + have hOSx : cslotAddrA sl ∈ OS := D.mem_slot hsl + have hH : H p = some .ep := (J.capTy _ hOSx).1 p b0 b1 b2 b3 b4 hc + have unread : ¬ S1aR2Reads (capability.Cap.Endpoint p b0 b1 b2 b3 b4) := fun h => h + have rd : ∀ (s' : S) (a' : A) (σ' : State Unit) (G' : MdbGhostA) (Z' : XSetsA) (m' : McState), + capA st s' (cslotAddrA sl) = .Endpoint p b0 b1 b2 b3 b4 → + J7LA st (J7R2A st ar ipa) Z' H OS s' σ' m' → + J7LA st (J7R2A st ar ipa) Z' H OS s' σ' m' ∧ + (DelRdA st Rv s' a' σ' G' (cslotAddrA sl) ∧ RdThA st OS s' (cslotAddrA sl)) ∧ + J7LA st (J7R2xA st ar ipa (cslotAddrA sl)) Z' H OS s' σ' m' := by + intro s' a' σ' G' Z' m' e j + refine ⟨j, ⟨delRdA_other (fun r g h => by rw [e] at h; cases h) (fun r g h => by rw [e] at h; cases h) + (fun v h => by rw [e] at h; cases h), rdThA_of_ne fun t h => by rw [e] at h; cases h⟩, + j.pinUnreadA (by rw [e]; exact unread)⟩ + rw [fin_cap_eq_obj (by simp)] + cases f + · -- not final: nothing happens + have e : objects.finalise_cap_object st ar s a (.Endpoint p b0 b1 b2 b3 b4) false = + ok ((capability.Cap.Null, capability.Cap.Null), s, a) := rfl + have hnoop : ((Fl4SpecA64.Cap.nullCap, Fl4SpecA64.Cap.nullCap), σ) ∈ + (fl4FinaliseCap (Z := Unit) ac false σ).1 := by + rw [eac] + simp only [absCapA] + rw [fl4FinaliseCap_plainA (by intro h; exact h)] + exact mem_bindA.2 ⟨((), σ), rfl, rfl⟩ + rw [e, WP.spec_ok] + have rr := rd s a σ G _ m hc J.j7 + refine ⟨rfl, trivial, trivial, σ, G, ⟨false, hf, hnoop⟩, D, hc, fun y _ _ => Or.inl rfl, ?_, ?_, + fun _ _ _ h => by cases h⟩ + · exact ⟨fun h => absurd (Or.inl rfl) h, fun _ => ⟨hcore, ⟨Q, R, hw, Ac, Bf, stack, + J.mapJ7A fun _ _ => trivial, X⟩, hPA, hZT, hBH, hCT, hFx⟩⟩ + · exact fun _ => ⟨rr.2.1, m, rr.2.2⟩ + · -- final + refine WP.spec_mono (J.epFin_recA (b0 := b0) (b1 := b1) (b2 := b2) (b3 := b3) (b4 := b4) hH hCs hmb hmax htu + (fun _ _ t ht _ => J.onCore_single hCs h1 ht)) ?_ + rintro ⟨⟨rem, cl⟩, s', a'⟩ ⟨h0, ha, σ3, Q', R', h3, J', k, kb, cpu, F, vc, k6⟩ + dsimp only at h0 ha J' k kb cpu vc k6 ⊢ + obtain ⟨rfl, rfl⟩ := Prod.mk.inj h0 + subst ha + have caps : ∀ y ∈ OS, capA st s' y = capA st s y := fun y hy => k.capA hy + have hc' : capA st s' (cslotAddrA sl) = .Endpoint p b0 b1 b2 b3 b4 := (caps _ hOSx).trans hc + have hmem : ((Fl4SpecA64.Cap.nullCap, Fl4SpecA64.Cap.nullCap), σ3) ∈ + (fl4FinaliseCap (Z := Unit) ac true σ).1 := by + rw [eac] + simp only [absCapA] + rw [fl4FinaliseCap_plainA (by intro h; exact h)] + exact mem_bindA.2 ⟨((), σ3), h3, rfl⟩ + have D' : DStateA st Z.th OS s' σ3 G := DStateA.keepOSA (Z := { Z with G := G, Q := Q', R := R', hw := hw }) D J' k + have rr := rd s' a' σ3 G _ m hc' J'.j7 + have W' : DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st J7R2tA)) s' a' σ3 G := + ⟨⟨kb.trans hcore.1, cpu.trans hcore.2⟩, ⟨Q', R', hw, Ac, Bf, stack, J'.mapJ7A fun _ _ => trivial, (k6 D.sub (X.ghostA G Q R hw)).ghostA Z.G Z.Q Z.R Z.hw⟩, + ParentAdjA.keepOS D.lk k hPA, + ZombieTcbNoVcpuA.keepA caps J.zombieTcb (fun t ht => vc t (J.tcbs ▸ ht)) hZT, + bufHold_sameA st hBH caps, ctOff_sameA st hCT kb caps, hFx.keepA st D.sub caps⟩ + refine ⟨rfl, trivial, trivial, σ3, G, ⟨true, hf, hmem⟩, D', hc', fun y hy _ => Or.inl (caps y hy), + ⟨fun h => absurd (Or.inl rfl) h, fun _ => W'⟩, fun _ => ⟨rr.2.1, m, rr.2.2⟩, fun _ _ _ h => by cases h⟩ + +/-- **The Endpoint finaliser hook** (module docstring). -/ +theorem finHookEpA {ipa : U64} (s0 : S) + (Rv : S → A → State Unit → MdbGhostA → U64 → Prop) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (h1 : fl4A64Platform.numNodes = 1) : + FinHookKA st .endpoint ar Z.th OS fl4FinaliseCap + (DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st (J7R2A st ar ipa)))) + (DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st J7R2tA))) + (DelRdXA st ar Z H OS ipa Rv) (DelRzA st) := by + intro s a σ G sl c ac f D hW hsl hc hk hg hac hf + obtain ⟨p, b0, b1, b2, b3, b4, rfl⟩ : ∃ p b0 b1 b2 b3 b4, c = .Endpoint p b0 b1 b2 b3 b4 := by + cases c <;> simp [finKindA] at hk + exact ⟨_, _, _, _, _, _, rfl⟩ + exact finHookEpA_atA s0 Rv hCs hmb hmax htu D hW hsl hc hg hac (hf trivial) h1 + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelEpKeepA.lean b/proofs/Fl4ProofsA64/P3/DelEpKeepA.lean new file mode 100644 index 000000000..be0f28267 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelEpKeepA.lean @@ -0,0 +1,209 @@ +/- +P3 (AArch64, delete hooks W3): **the spec side of the Endpoint finaliser frames**. `EpSpecKeepA σ σ'` records what the +steps of `cancel_all_ipc` keep of the objects the other relations read: notifications are unchanged; an endpoint stays an +endpoint; a reply keeps its SC; a TCB keeps its arch, fault, bound notification, priority, MCP, yield-to and IPC buffer +fields, its state unchanged or (never from `blockedOnNotification`) `restart` / `inactive`; an SC keeps its reply stack +and yield-from link. Frames of `mcp`, `yieldRel` and the reply stacks from it. No named premise. New module. +-/ +import Fl4ProofsA64.P3.DelNtfnFrameA +import Fl4ProofsA64.P3.SigTcbKeepA +import Fl4ProofsA64.P3.NtfnSpecA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Sched Fl4SpecA64 + +/-- What a step of the endpoint loop keeps of one TCB. -/ +structure EpKeepTcbA (x y : Tcb) : Prop where + arch : y.tcbArch = x.tcbArch + fault : y.tcbFault = x.tcbFault + bound : y.tcbBoundNotification = x.tcbBoundNotification + prio : y.tcbPriority = x.tcbPriority + frame : y.tcbIpcframe = x.tcbIpcframe + buf : y.tcbIpcBuffer = x.tcbIpcBuffer + mcp : y.tcbMcpriority = x.tcbMcpriority + flg : y.tcbFlags = x.tcbFlags + yld : y.tcbYieldTo = x.tcbYieldTo + st : y.tcbState = x.tcbState ∨ + ((∀ n, x.tcbState ≠ .blockedOnNotification n) ∧ ∀ r, x.tcbState ≠ .blockedOnReply r) ∧ (y.tcbState = .restart ∨ y.tcbState = .inactive) + +theorem EpKeepTcbA.refl (x : Tcb) : EpKeepTcbA x x := ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, Or.inl rfl⟩ + +theorem EpKeepTcbA.trans {x y z : Tcb} (h1 : EpKeepTcbA x y) (h2 : EpKeepTcbA y z) : EpKeepTcbA x z := by + refine ⟨h2.arch.trans h1.arch, h2.fault.trans h1.fault, h2.bound.trans h1.bound, h2.prio.trans h1.prio, + h2.frame.trans h1.frame, h2.buf.trans h1.buf, h2.mcp.trans h1.mcp, h2.flg.trans h1.flg, h2.yld.trans h1.yld, ?_⟩ + rcases h1.st with e1 | ⟨n1, e1⟩ <;> rcases h2.st with e2 | ⟨n2, e2⟩ + · exact Or.inl (e2.trans e1) + · exact Or.inr ⟨⟨fun n h => n2.1 n (e1.trans h), fun r h => n2.2 r (e1.trans h)⟩, e2⟩ + · refine Or.inr ⟨n1, ?_⟩ + rw [e2]; exact e1 + · exact Or.inr ⟨n1, e2⟩ + +/-- One object replaced by another that agrees on what the framed relations read. -/ +def EpObjKeepA : KernelObject → KernelObject → Prop + | .endpoint _, o' => ∃ y, o' = .endpoint y + | .reply x, o' => ∃ y, o' = .reply y ∧ y.replySc = x.replySc + | .tcb x, o' => ∃ y, o' = .tcb y ∧ EpKeepTcbA x y + | .schedContext x n, o' => ∃ y, o' = .schedContext y n ∧ y.scReplies = x.scReplies ∧ y.scYieldFrom = x.scYieldFrom + | _, _ => False + +/-- Heaps related object by object: unchanged, or replaced as `EpObjKeepA` allows. -/ +def EpSpecKeepA (σ σ' : State Unit) : Prop := + ∀ p, σ'.kheap p = σ.kheap p ∨ ∃ o o', σ.kheap p = some o ∧ σ'.kheap p = some o' ∧ EpObjKeepA o o' + +theorem EpObjKeepA.trans {a b c : KernelObject} (h1 : EpObjKeepA a b) (h2 : EpObjKeepA b c) : EpObjKeepA a c := by + cases a <;> simp only [EpObjKeepA] at h1 ⊢ + case endpoint => + obtain ⟨y, rfl⟩ := h1 + exact h2 + case reply => + obtain ⟨y, rfl, e1⟩ := h1 + obtain ⟨z, rfl, f1⟩ := h2 + exact ⟨z, rfl, f1.trans e1⟩ + case tcb => + obtain ⟨y, rfl, k1⟩ := h1 + obtain ⟨z, rfl, k2⟩ := h2 + exact ⟨z, rfl, k1.trans k2⟩ + case schedContext => + obtain ⟨y, rfl, e1, e2⟩ := h1 + obtain ⟨z, rfl, f1, f2⟩ := h2 + exact ⟨z, rfl, f1.trans e1, f2.trans e2⟩ + +theorem EpSpecKeepA.refl (σ : State Unit) : EpSpecKeepA σ σ := fun _ => Or.inl rfl + +theorem EpSpecKeepA.trans {σ1 σ2 σ3 : State Unit} (h1 : EpSpecKeepA σ1 σ2) (h2 : EpSpecKeepA σ2 σ3) : + EpSpecKeepA σ1 σ3 := fun p => by + rcases h1 p with e1 | ⟨a, b, ha, hb, k1⟩ <;> rcases h2 p with e2 | ⟨c, d, hc, hd, k2⟩ + · exact Or.inl (e2.trans e1) + · exact Or.inr ⟨c, d, e1.symm.trans hc, hd, k2⟩ + · exact Or.inr ⟨a, b, ha, e2.trans hb, k1⟩ + · rw [hb] at hc; cases hc + exact Or.inr ⟨a, d, ha, hd, k1.trans k2⟩ + +theorem EpSpecKeepA.of_kheap {σ σ' : State Unit} (h : σ'.kheap = σ.kheap) : EpSpecKeepA σ σ' := + fun p => Or.inl (by rw [h]) + +/-- One object replaced. -/ +theorem EpSpecKeepA.of_upd {σ : State Unit} {q : ObjRef} {o o' : KernelObject} (hq : σ.kheap q = some o) + (k : EpObjKeepA o o') : EpSpecKeepA σ (updK σ q o') := fun p => by + by_cases e : p = q + · subst e + exact Or.inr ⟨o, o', hq, by rw [updK_kheap, Function.update_self], k⟩ + · exact Or.inl (by rw [updK_kheap, Function.update_of_ne e]) + +/-- A step that changes only sched contexts and keeps their reply stacks and yield-from links. -/ +theorem EpSpecKeepA.of_sc {σ σ' : State Unit} (h : NtfnSpecKeepA σ σ') (s : ScOnlyA σ σ') : EpSpecKeepA σ σ' := + fun p => by + rcases h p with e | ⟨o, o', e1, e2, k⟩ + · exact Or.inl e + · cases o <;> simp only [NtfnObjKeepA] at k + · exact Or.inl ((s.1 p _ e1 (fun _ _ e => by cases e)).trans e1.symm) + · exact Or.inl ((s.1 p _ e1 (fun _ _ e => by cases e)).trans e1.symm) + · obtain ⟨y, rfl, k1, k2⟩ := k + exact Or.inr ⟨_, _, e1, e2, y, rfl, k1, k2⟩ + +/-- **`rescheduleRequired` keeps.** -/ +theorem rescheduleRequired_epkA {σ : State Unit} {r : Unit × State Unit} + (h : r ∈ (rescheduleRequired (Z := Unit) σ).1) : EpSpecKeepA σ r.2 := + EpSpecKeepA.of_sc (rescheduleRequired_nkA _ _ h) (rescheduleRequired_tkA _ _ h) + +/-- **The spec's restart tail keeps.** -/ +theorem specRestartTail_epkA {t : ObjRef} {σ : State Unit} {r : Unit × State Unit} + (h : r ∈ (specRestartTail t σ).1) : EpSpecKeepA σ r.2 := + EpSpecKeepA.of_sc (specRestartTail_nkA _ _ _ h) (specRestartTail_tkA _ _ _ h) + +namespace EpSpecKeepA +variable {σ σ' : State Unit} (H : EpSpecKeepA σ σ') +include H + +theorem ntfn {p : ObjRef} {x : Notification} (h : σ.kheap p = some (.notification x)) : + σ'.kheap p = some (.notification x) := by + rcases H p with e | ⟨o, o', e1, -, k⟩ + · exact e.trans h + · rw [h] at e1; cases e1 + exact k.elim + +theorem tcb {p : ObjRef} {x : Tcb} (h : σ.kheap p = some (.tcb x)) : + ∃ y, σ'.kheap p = some (.tcb y) ∧ EpKeepTcbA x y := by + rcases H p with e | ⟨o, o', e1, e2, k⟩ + · exact ⟨x, e.trans h, EpKeepTcbA.refl x⟩ + · rw [h] at e1; cases e1 + obtain ⟨y, rfl, k⟩ := k + exact ⟨y, e2, k⟩ + +theorem sc {p : ObjRef} {x : SchedContext} {n : Nat} (h : σ.kheap p = some (.schedContext x n)) : + ∃ y, σ'.kheap p = some (.schedContext y n) ∧ y.scReplies = x.scReplies ∧ y.scYieldFrom = x.scYieldFrom := by + rcases H p with e | ⟨o, o', e1, e2, k⟩ + · exact ⟨x, e.trans h, rfl, rfl⟩ + · rw [h] at e1; cases e1 + obtain ⟨y, rfl, k1, k2⟩ := k + exact ⟨y, e2, k1, k2⟩ + +theorem sc_back {p : ObjRef} {y : SchedContext} {n : Nat} (h : σ'.kheap p = some (.schedContext y n)) : + ∃ x, σ.kheap p = some (.schedContext x n) ∧ y.scReplies = x.scReplies ∧ y.scYieldFrom = x.scYieldFrom := by + rcases H p with e | ⟨o, o', e1, e2, k⟩ + · exact ⟨y, e.symm.trans h, rfl, rfl⟩ + · rw [h] at e2; cases e2 + cases o <;> simp only [EpObjKeepA] at k + case endpoint => obtain ⟨z, e⟩ := k; cases e + case reply => obtain ⟨z, e, -⟩ := k; cases e + case tcb => obtain ⟨z, e, -⟩ := k; cases e + case schedContext => + obtain ⟨z, e, k1, k2⟩ := k + cases e + exact ⟨_, e1, k1, k2⟩ + +theorem reply_back {p : ObjRef} {y : Reply} (h : σ'.kheap p = some (.reply y)) : + ∃ x, σ.kheap p = some (.reply x) ∧ y.replySc = x.replySc := by + rcases H p with e | ⟨o, o', e1, e2, k⟩ + · exact ⟨y, e.symm.trans h, rfl⟩ + · rw [h] at e2; cases e2 + cases o <;> simp only [EpObjKeepA] at k + case endpoint => obtain ⟨z, e⟩ := k; cases e + case tcb => obtain ⟨z, e, -⟩ := k; cases e + case schedContext => obtain ⟨z, e, -⟩ := k; cases e + case reply => + obtain ⟨z, e, k1⟩ := k + cases e + exact ⟨_, e1, k1⟩ + +end EpSpecKeepA + +/-- `mcp` across the endpoint loop. -/ +theorem Sched.McpRelA.epFr {T : Set U64} {m : McState} {mem mem' : U64 → U64} {σ σ' : State Unit} + (h : McpRelA T m mem σ) (H : EpSpecKeepA σ σ') + (hm : ∀ t ∈ T, mem' (mcpA t) = mem (mcpA t) ∧ mem' (flagsA t) = mem (flagsA t)) : McpRelA T m mem' σ' := by + intro t ht + obtain ⟨x, hx, e, hf⟩ := h t ht + obtain ⟨y, hy, k⟩ := H.tcb hx + exact ⟨y, hy, by rw [(hm t ht).1, k.mcp]; exact e, fun g => (hf g).keep (hm t ht).2 k.flg⟩ + +/-- `yieldRel` across the endpoint loop. -/ +theorem Sched.YieldRelA.epFr {T C : Set U64} {m : McState} {mem mem' : U64 → U64} {σ σ' : State Unit} + (h : YieldRelA T C m mem σ) (H : EpSpecKeepA σ σ') (hm : ∀ t ∈ T, mem' (yieldA t) = mem (yieldA t)) : + YieldRelA T C m mem' σ' := by + refine ⟨fun t ht => ?_, fun t ht h0 => ?_, fun c hc y n hk p hp => ?_⟩ + · obtain ⟨x, hx, e⟩ := h.tcb t ht + obtain ⟨y, hy, k⟩ := H.tcb hx + exact ⟨y, hy, by rw [hm t ht, k.yld]; exact e⟩ + · rw [hm t ht] at h0 ⊢ + obtain ⟨hc, y, n, hy, e⟩ := h.toSc t ht h0 + obtain ⟨z, hz, -, k⟩ := H.sc hy + exact ⟨hc, z, n, hz, k.trans e⟩ + · obtain ⟨x, hx, -, k⟩ := H.sc_back hk + obtain ⟨t, ht, e1, e2⟩ := h.frm c hc x n hx p (k.symm.trans hp) + exact ⟨t, ht, e1, (hm t ht).trans e2⟩ + +/-- The reply stacks across the endpoint loop. -/ +theorem Endp.ReplyStackRelA.epFr {R C : Set U64} {mem mem' : U64 → U64} {σ σ' : State Unit} + {scRef : U64 → ObjRef} {stack : U64 → List U64} (h : ReplyStackRelA mem R C σ scRef stack) + (H : EpSpecKeepA σ σ') + (hm : ∀ r ∈ R, mem' (rNextA r) = mem (rNextA r) ∧ mem' (rPrevA r) = mem (rPrevA r)) : + ReplyStackRelA mem' R C σ' scRef stack := + h.frame hm + (fun _ _ _ n hk => by + obtain ⟨y, hy, k, -⟩ := H.sc hk + exact ⟨y, n, hy, k⟩) + (fun _ _ _ hk => H.reply_back hk) + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelEpLoopA.lean b/proofs/Fl4ProofsA64/P3/DelEpLoopA.lean new file mode 100644 index 000000000..ee72482be --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelEpLoopA.lean @@ -0,0 +1,766 @@ +/- +P3 (AArch64, delete hooks W3): **the footprint-and-keep sibling of `cancel_all_ipc`'s loop** (`P3/EpCancelAllA`'s +`all_tail` / `all_reply` / `all_deq` / `all_loop` / `cancel_all_rel`, whose statements are unchanged). +`cancel_all_rel` exports only `EpStateRel ∧ SR`; the sibling also exports + +* `EpSpecKeepA σ σ'` (`P3/DelEpKeepA`), `SSzA σ σ'`, +* `FpEA`: the exact write set `EpSigWA` of the loop (the endpoint's two words, the reply objects' `TCB` words, and the + state word and `Ep` links of the TCBs of a set `Q` holding the queued threads) plus the scheduler words `Wr σ s` of + `SchedFootEA`, and +* `SInvXA ∧ SIdleA ∧ SActA` (`LoopInvA`) carried across the spec steps. + +No named premise (the interfaces are `SchedOpsEA` / `SchedFootEA`); new module. +-/ +import Fl4ProofsA64.P3.EpCancelAllA +import Fl4ProofsA64.P3.EpFootHelpA +import Fl4ProofsA64.P3.DelEpKeepA +import Fl4ProofsA64.P3.DelSinvLoopA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open Fl4Spec Fl4SpecA64 Fl4ProofsA64.P3.Sched + +noncomputable section + +/-! ## The spec invariants -/ + +/-- `SInvXA` across one thread-state write to a TCB that is not idle, to a runnable state or of a TCB in no ready +queue (`SInvXA.restartA`'s sibling). -/ +theorem SInvXA.stateA {σ : State Unit} {m : McState} {n : Nat} (h : SInvXA σ m n) {t : ObjRef} {x : Tcb} + (hk : σ.kheap t = some (.tcb x)) (hti : t ≠ σ.idleThread) {ts : ThreadState} + (hr : runnable ts = true ∨ ∀ d p, t ∉ σ.readyQueues d p) (hrl : runnable ts = true ∨ t ∉ σ.releaseQueue) + (hni : ts ≠ .idleThreadState) (hf : x.tcbFault ≠ none → FaultStA ts) : SInvXA (stUpdA σ t x ts) m n := by + have hkt : (stUpdA σ t x ts).kheap t = some (.tcb { x with tcbState := ts }) := by simp [setObjA] + have hkn : ∀ p, p ≠ t → (stUpdA σ t x ts).kheap p = σ.kheap p := fun p hp => by + simp [setObjA, Function.update_of_ne hp] + have tcbK : ∀ p y, σ.kheap p = some (.tcb y) → ∃ y', (stUpdA σ t x ts).kheap p = some (.tcb y') ∧ + y'.tcbSchedContext = y.tcbSchedContext ∧ (p ≠ t → y' = y) ∧ (p = t → y'.tcbState = ts) := fun p y e => by + by_cases ep : p = t + · subst ep; rw [hk] at e; cases e; exact ⟨_, hkt, rfl, fun h => absurd rfl h, fun _ => rfl⟩ + · exact ⟨y, by rw [hkn p ep]; exact e, rfl, fun _ => rfl, fun h => absurd h ep⟩ + have scK : ∀ p y k, (stUpdA σ t x ts).kheap p = some (.schedContext y k) ↔ σ.kheap p = some (.schedContext y k) := + fun p y k => ⟨fun e => stUpd_back e (fun _ h => by cases h), fun e => stUpd_keep hk e (fun _ h => by cases h)⟩ + have CF := CurFactsA.back (σ := σ) (σ' := stUpdA σ t x ts) ⟨h.zc, h.rlsRun, h.curScReady⟩ (fun _ hq => hq) rfl rfl rfl + (fun p x' e => by + by_cases ep : p = t + · subst ep; rw [hkt] at e; cases e; exact ⟨x, hk, rfl⟩ + · rw [hkn p ep] at e; exact ⟨x', e, rfl⟩) + (fun p y e hm => by + obtain ⟨y', e', -, f, g⟩ := tcbK p y e + refine ⟨y', e', fun hy => ?_⟩ + by_cases ep : p = t + · rcases hrl with hh | hh + · rw [g ep]; exact hh + · exact absurd (ep ▸ hm) hh + · rw [f ep]; exact hy) + (fun y k e => ⟨y, (scK _ y k).1 (by exact e), fun a => ⟨a, id⟩⟩) + refine + { mc := h.mc + view := h.view + curTcb := ?_ + idleTcb := ?_ + onlyIdle := fun p y e e2 => ?_ + idleSc := ?_ + curSc := by obtain ⟨y, k, e⟩ := h.curSc; exact ⟨y, k, (scK _ y k).2 e⟩ + ctQ := h.ctQ + idleQ := h.idleQ + rq := fun d p y hy => ?_ + rls := fun y hy => ?_ + rpos := fun p y k e a => h.rpos p y k ((scK p y k).1 e) a + zc := CF.1 + rlsRun := CF.2.1 + curScReady := CF.2.2 + faultSt := fun p y e f => by + by_cases ep : p = t + · subst ep; rw [hkt] at e; cases e; exact hf f + · rw [hkn p ep] at e; exact h.faultSt p y e f + irqN := h.irqN.updTcbA hk rfl } + · obtain ⟨y, e⟩ := h.curTcb + obtain ⟨y', e', -⟩ := tcbK _ y e + exact ⟨y', e'⟩ + · obtain ⟨y, e, e2⟩ := h.idleTcb + obtain ⟨y', e', -, f, -⟩ := tcbK _ y e + exact ⟨y', e', by rw [f hti.symm]; exact e2⟩ + · by_cases ep : p = t + · subst ep; rw [hkt] at e; cases e; exact absurd e2 hni + · rw [hkn p ep] at e; exact h.onlyIdle p y e e2 + · obtain ⟨y, z, k, e1, e2, e3, e4⟩ := h.idleSc + obtain ⟨y', e1', f1, -⟩ := tcbK _ y e1 + exact ⟨y', z, k, e1', by rw [f1]; exact e2, (scK _ z k).2 e3, e4⟩ + · obtain ⟨z, e, e2⟩ := h.rq d p y hy + obtain ⟨y', e', -, f, g⟩ := tcbK y z e + by_cases ep : y = t + · rcases hr with hr | hr + swap + · exact absurd (ep ▸ hy) (hr d p) + exact ⟨y', e', by rw [g ep]; exact hr⟩ + · exact ⟨y', e', by rw [f ep]; exact e2⟩ + · obtain ⟨z, e⟩ := h.rls y hy + obtain ⟨y', e', -⟩ := tcbK y z e + exact ⟨y', e'⟩ + +/-- What the endpoint loop knows of a waiter: neither current nor any core's idle thread. -/ +structure OffCurA (σ : State Unit) (m : McState) (n : Nat) (t : ObjRef) : Prop where + cur : t ≠ σ.curThread + idle : t ≠ σ.idleThread + idles : ∀ c < n, m.idleThreadOf c ≠ t + +/-- A thread blocked on an endpoint is neither current nor idle. -/ +theorem LoopInvA.offCur {σ : State Unit} {m : McState} {n : Nat} (h : LoopInvA σ m n) {t : ObjRef} {x : Tcb} + (hk : σ.kheap t = some (.tcb x)) (hc : Notif.tsCode x.tcbState = 3 ∨ Notif.tsCode x.tcbState = 4) : + OffCurA σ m n t := by + have hs : (∃ e r d, x.tcbState = .blockedOnReceive e r d) ∨ ∃ e d, x.tcbState = .blockedOnSend e d := by + rcases hc with h | h + · exact Or.inl (Endp.tsCode_three h) + · exact Or.inr (Endp.tsCode_four h) + refine ⟨fun e => ?_, fun e => ?_, fun c hc e => ?_⟩ + · obtain ⟨z, hz, hz'⟩ := h.sact.f2 + rw [← e, hk] at hz + have := KernelObject.tcb.inj (Option.some.inj hz) + subst this + rcases hs with ⟨_, _, _, hs⟩ | ⟨_, _, hs⟩ <;> (rw [hs] at hz'; simp at hz') + · obtain ⟨z, hz, hz'⟩ := h.sinv.idleTcb + rw [← e, hk] at hz + have := KernelObject.tcb.inj (Option.some.inj hz) + subst this + rcases hs with ⟨_, _, _, hs⟩ | ⟨_, _, hs⟩ <;> (rw [hs] at hz'; cases hz') + · obtain ⟨z, hz, hz'⟩ := h.sidle.f3 c hc + rw [e, hk] at hz + have := KernelObject.tcb.inj (Option.some.inj hz) + subst this + rcases hs with ⟨_, _, _, hs⟩ | ⟨_, _, hs⟩ <;> (rw [hs] at hz'; cases hz') + +/-- **Restarting or deactivating a blocked thread that is neither current nor idle** keeps the invariants. -/ +theorem LoopInvA.setBlk {σ : State Unit} {m : McState} {n : Nat} (h : LoopInvA σ m n) {t : ObjRef} {x : Tcb} + (hk : σ.kheap t = some (.tcb x)) (hb : runnable x.tcbState = false) (o : OffCurA σ m n t) + {ts : ThreadState} (hts : ts = .restart ∨ ts = .inactive) (hfn : ts = .restart → x.tcbFault = none) : + LoopInvA (Notif.stsAct (Notif.updK σ t (.tcb { x with tcbState := ts })) t) m n := by + have eact : Notif.stsAct (Notif.updK σ t (.tcb { x with tcbState := ts })) t = stUpdA σ t x ts := by + unfold Notif.stsAct + rw [if_neg (fun h => o.cur h.1)] + rfl + rw [eact] + have nq : ∀ d p, t ∉ σ.readyQueues d p := fun d p ht => by + obtain ⟨z, hz, hr⟩ := h.sinv.rq d p t ht + rw [hk] at hz + have := KernelObject.tcb.inj (Option.some.inj hz) + subst this + rw [hb] at hr + cases hr + exact + { sinv := h.sinv.stateA hk o.idle (Or.inr nq) + (Or.inr fun hq => by + obtain ⟨z, hz, hr⟩ := h.sinv.rlsRun t hq + rw [hk] at hz + have := KernelObject.tcb.inj (Option.some.inj hz) + subst this + rw [hb] at hr + cases hr) (by rcases hts with rfl | rfl <;> (intro e; cases e)) + (fun f => by rcases hts with rfl | rfl <;> first | exact absurd (hfn rfl) f | exact Or.inr (Or.inr rfl)) + sidle := h.sidle.stUpdA hk o.idles + sact := + { f1 := fun u e => h.sact.f1 u e + f2 := by + obtain ⟨z, hz, hz'⟩ := h.sact.f2 + exact ⟨z, (Function.update_of_ne o.cur.symm _ _).trans hz, hz'⟩ } } + +/-- The spec's restart tail of a thread just set `Restart` keeps the invariants. -/ +theorem LoopInvA.restartTailA {σ : State Unit} {m : McState} {n : Nat} (h : LoopInvA σ m n) {t : ObjRef} {x : Tcb} + (hk : σ.kheap t = some (.tcb x)) (hr : x.tcbState = .restart) (o : OffCurA σ m n t) + {r : Unit × State Unit} (hm : r ∈ (Endp.specRestartTail t σ).1) : LoopInvA r.2 m n := by + unfold Endp.specRestartTail at hm + obtain ⟨scOpt, -, hm⟩ := eqPres_mem (threadGet_eqPres _ t) hm + obtain ⟨c, hc, hm⟩ := mem_bindA.1 hm + have R3 : RefK σ c.2 := ifCondRefillUnblockCheck_refK _ _ _ _ c hc + have h3 := h.refK R3 + obtain ⟨x3, hx3, ex3⟩ := (R3.2.1 t).tcbF hk + have cur3 : c.2.curThread = σ.curThread := by rw [R3.1] + have idle3 : c.2.idleThread = σ.idleThread := by rw [R3.1] + have ok3 : EnqOkA c.2 m n t := ⟨by rw [cur3]; exact o.cur, o.idles, x3, hx3, by rw [ex3.1, hr]; rfl⟩ + exact (h3.possibleSwitch ok3 (by rw [idle3]; exact o.idle) hm).1 + +/-- Replacing an endpoint keeps the invariants. -/ +theorem LoopInvA.updEp {σ : State Unit} {m : McState} {n : Nat} (h : LoopInvA σ m n) {p : ObjRef} {y y' : Endpoint} + (hk : σ.kheap p = some (.endpoint y)) : LoopInvA (Notif.updK σ p (.endpoint y')) m n := by + refine h.refK ⟨rfl, fun q => ?_, fun g => g.updNs p _ fun _ _ e => by cases e⟩ + show RefObjA (σ.kheap q) (Function.update σ.kheap p (some (.endpoint y')) q) + by_cases e : q = p + · subst e; rw [Function.update_self, hk]; exact .ep _ _ + · rw [Function.update_of_ne e]; exact .same _ + +/-- Replacing a reply object keeps the invariants. -/ +theorem LoopInvA.updReply {σ : State Unit} {m : McState} {n : Nat} (h : LoopInvA σ m n) {p : ObjRef} {y y' : Reply} + (hk : σ.kheap p = some (.reply y)) : LoopInvA (Notif.updK σ p (.reply y')) m n := by + refine h.refK ⟨rfl, fun q => ?_, fun g => g.updNs p _ fun _ _ e => by cases e⟩ + show RefObjA (σ.kheap q) (Function.update σ.kheap p (some (.reply y')) q) + by_cases e : q = p + · subst e; rw [Function.update_self, hk]; exact .reply _ _ + · rw [Function.update_of_ne e]; exact .same _ + +end + +end Fl4ProofsA64.P3 + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched + +noncomputable section + +theorem _root_.Fl4ProofsA64.P3.OffCurA.updKA {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} {n : Nat} + {t : Fl4SpecA64.ObjRef} (o : OffCurA σ m n t) (p : Fl4SpecA64.ObjRef) (k : Fl4SpecA64.KernelObject) : + OffCurA (updK σ p k) m n t := ⟨o.cur, o.idle, o.idles⟩ + +theorem _root_.Fl4ProofsA64.P3.OffCurA.stsA {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} {n : Nat} + {t : Fl4SpecA64.ObjRef} (o : OffCurA σ m n t) (u : Fl4SpecA64.ObjRef) : OffCurA (stsAct σ u) m n t := by + unfold stsAct + split + · exact ⟨o.cur, o.idle, o.idles⟩ + · exact o + +/-! ## The footprint -/ + +/-- The words the loop writes (without the scheduler words). -/ +def EpSigWA (R Q : Set U64) (e b : U64) : Prop := + EW e b ∨ (∃ r ∈ R, b = rTcbA r) ∨ ∃ t ∈ Q, b = stA t ∨ TL t b + +section Fl4 +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {T E R : Set U64} + {Bf : Set (U64 × Fl4SpecA64.Word)} {SR : S → Fl4SpecA64.State Unit → Prop} + {Wr : Fl4SpecA64.State Unit → S → U64 → Prop} + +variable (st R Wr) in +/-- The footprint shape of the loop's posts. -/ +def FpEA (Q : Set U64) (e : U64) (σ : Fl4SpecA64.State Unit) (s s' : S) : Prop := + WritesIn st s s' (fun b => EpSigWA R Q e b ∨ Wr σ s b) + +theorem FpEA.refl {Q : Set U64} {e : U64} (σ : Fl4SpecA64.State Unit) (s : S) : FpEA st R Wr Q e σ s s := + WritesIn.refl _ _ + +theorem FpEA.of_sw {Q : Set U64} {e : U64} {σ : Fl4SpecA64.State Unit} {s s' : S} + (h : WritesIn st s s' (EpSigWA R Q e)) : FpEA st R Wr Q e σ s s' := h.mono fun _ hb => Or.inl hb + +theorem FpEA.of_wr {Q : Set U64} {e : U64} {σ : Fl4SpecA64.State Unit} {s s' : S} (h : WritesIn st s s' (Wr σ s)) : + FpEA st R Wr Q e σ s s' := h.mono fun _ hb => Or.inr hb + +theorem FpEA.trans (F : SchedFootE0A st T E R Bf SR Wr) {Q : Set U64} {e : U64} {σ σ1 : Fl4SpecA64.State Unit} + {s s1 s2 : S} (h1 : FpEA st R Wr Q e σ s s1) (hz : SSzA σ σ1) (h2 : FpEA st R Wr Q e σ1 s1 s2) : + FpEA st R Wr Q e σ s s2 := + (WritesIn.trans (st := st) h1 h2).mono fun _ h => by + rcases h with (h | h) | (h | h) + · exact Or.inl h + · exact Or.inr h + · exact Or.inl h + · exact Or.inr (F.back hz h1.1 h) + +/-- `ep_dequeue` of a queue whose threads are in `Q` writes only the endpoint's words and their `Ep` links. -/ +theorem ep_dequeue_sigE (hP : EPlaced T E R Bf) (s : S) {e : U64} (he : e ∈ E) (t : U64) (xs : List U64) + (h : QueueRel (L.mem s) T .Ep (qOf (L.mem s) e) xs) (ht : t ∈ xs) {Q : Set U64} (hQ : ∀ w ∈ xs, w ∈ Q) : + endpoint.ep_dequeue st s e t ⦃ s' => WritesIn st s s' (EpSigWA R Q e) ⦄ := by + refine WP.spec_mono (ep_dequeue_full hP s he t xs h ht) ?_ + rintro s' ⟨-, -, hA, hW, -, -⟩ + refine ⟨hW.1, hW.2.1, fun b hb hn => hA b hb ⟨fun w => hn (Or.inl w), fun ⟨w, hw, hl⟩ => ?_⟩⟩ + exact hn (Or.inr (Or.inr ⟨w, hQ w hw, Or.inr hl⟩)) + +/-- `set_thread_state` after writes from `(s0, σ0)`, with the loop's footprint. -/ +theorem set_thread_state_sigE (hO : SchedOpsEA st T E R Bf SR) (hF : SchedFootE0A st T E R Bf SR Wr) + (hP : EPlaced T E R Bf) {s0 : S} + {σ0 : Fl4SpecA64.State Unit} (hSR0 : SR s0 σ0) (s : S) (σ : Fl4SpecA64.State Unit) + (hR : EpStateRel T E R Bf (L.mem s) σ) (hW0 : WritesIn st s0 s (EWrite T E R Bf)) (hS0 : EpStep σ0 σ) + {t : U64} (ht : t ∈ T) {x : Fl4SpecA64.Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) + (ts : Fl4SpecA64.ThreadState) (hts : tsCode ts < 3) (code : U64) (hcode : code.val = tsCode ts) + (hNB : NoBlockE σ0 (updK σ (specPtr t) (.tcb { x with tcbState := ts }))) + {Q : Set U64} {e : U64} (htQ : t ∈ Q) : + sched.set_thread_state st s t code ⦃ s' => + EpStateRel T E R Bf (L.mem s') (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := ts })) (specPtr t)) ∧ + SR s' (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := ts })) (specPtr t)) ∧ + FpEA st R Wr Q e σ s s' ⦄ := by + unfold sched.set_thread_state + step with obj.Tcb.set_state.spec as ⟨s1, hs1⟩ + subst hs1 + obtain ⟨hR1, hW1, hS1⟩ := write_state_rel hP s σ hR ht hk hq ts hts code hcode + have hSR1 := hO.stable _ _ _ _ hSR0 (WritesIn.ew hW0 hW1) (hS0.trans hS1) hNB + have hw1 : WritesIn st s (L.wr s (stA t) code) (EpSigWA R Q e) := + (writesIn_wr (a_st (hP.tcb t ht)).1).mono fun b hb => Or.inr (Or.inr ⟨t, htQ, Or.inl hb⟩) + refine WP.spec_mono (WP.spec_and (hO.scheduleTcb _ _ t hR1 hSR1 ht) (hF.scheduleTcb _ _ t hR1 hSR1 ht)) ?_ + rintro s2 ⟨⟨hf, hS⟩, hw⟩ + exact ⟨hR1.frame hP hf (KView.stsAct' _ _), hS, + FpEA.trans hF (FpEA.of_sw hw1) (ssz_updK_tcb σ _ _) (FpEA.of_wr hw)⟩ + +/-- `reply_unlink` of a receiver (`reply_unlink_rel`) with the loop's footprint. -/ +theorem reply_unlink_sigE (hO : SchedOpsEA st T E R Bf SR) (hF : SchedFootE0A st T E R Bf SR Wr) + (hP : EPlaced T E R Bf) {s0 : S} + {σ0 : Fl4SpecA64.State Unit} (hSR0 : SR s0 σ0) {s2 : S} {σ2 : Fl4SpecA64.State Unit} + (hR2 : EpStateRel T E R Bf (L.mem s2) σ2) (hW02 : WritesIn st s0 s2 (EWrite T E R Bf)) (hS02 : EpStep σ0 σ2) + {t : U64} (ht : t ∈ T) {x0 x1 : Fl4SpecA64.Tcb} (hk0 : σ0.kheap (specPtr t) = some (.tcb x0)) + (hk2 : σ2.kheap (specPtr t) = some (.tcb x1)) (hO2 : OnlyT σ0 σ2 (specPtr t)) + (hblk : Fl4SpecA64.runnable x0.tcbState = false ∧ x0.tcbState ≠ .idleThreadState) + (hq2 : ∀ e ∈ E, ∀ z, σ2.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) + {i : U64} (hiR : i ∈ R) {xr : Fl4SpecA64.Reply} (hkr2 : σ2.kheap (specPtr i) = some (.reply xr)) + {Q : Set U64} {e : U64} (htQ : t ∈ Q) : + ipc.reply_unlink st s2 i t ⦃ s3 => + EpStateRel T E R Bf (L.mem s3) (stsAct (updK (updK σ2 (specPtr i) (.reply { xr with replyTcb := none })) + (specPtr t) (.tcb { x1 with tcbState := .inactive })) (specPtr t)) ∧ + SR s3 (stsAct (updK (updK σ2 (specPtr i) (.reply { xr with replyTcb := none })) + (specPtr t) (.tcb { x1 with tcbState := .inactive })) (specPtr t)) ∧ + FpEA st R Wr Q e σ2 s2 s3 ⦄ := by + have hit : specPtr i ≠ specPtr t := by intro h; rw [h, hk2] at hkr2; cases hkr2 + have hal := (a_rtcb hP hiR).1 + have hag := agree_wr (L := L) (s := s2) (v := 0#u64) hal + have hR3 : EpStateRel T E R Bf (L.mem (L.wr s2 (rTcbA i) 0#u64)) + (updK σ2 (specPtr i) (.reply { xr with replyTcb := none })) := + hR2.upd_reply hP hiR _ (hag.mono fun b hb => by rw [rTcbA_val]; exact hb.2) (by rw [rd_wr_eq hal, optRef_zero]) + have hW3 : WritesIn st s2 (L.wr s2 (rTcbA i) 0#u64) (EWrite T E R Bf) := + (writesIn_wr hal).mono fun b hb => by subst hb; exact Or.inr (Or.inl ⟨i, hiR, rTcbA_val i⟩) + have hw3 : WritesIn st s2 (L.wr s2 (rTcbA i) 0#u64) (EpSigWA R Q e) := + (writesIn_wr hal).mono fun b hb => Or.inr (Or.inl ⟨i, hiR, hb⟩) + have hk3 : (updK σ2 (specPtr i) (.reply { xr with replyTcb := none })).kheap (specPtr t) = some (.tcb x1) := by + rw [updK_kheap, Function.update_of_ne hit.symm, hk2] + have hq3 := notq_updR (E := E) hq2 i { xr with replyTcb := none } + unfold ipc.reply_unlink + step with obj.Reply.set_tcb.spec as ⟨s3, hs3⟩ + subst hs3 + refine WP.spec_mono (set_thread_state_sigE hO hF hP hSR0 _ _ hR3 (WritesIn.ew hW02 hW3) + (hS02.trans (EpStep.reply hkr2 none)) ht hk3 hq3 .inactive (by decide) sched.TS_INACTIVE + (by unfold sched.TS_INACTIVE; rfl) + (NoBlockE.only hk0 (by rw [updK_kheap, Function.update_self]) ((hO2.updO _ fun _ h => by cases h).updP _) + (Or.inr (Or.inr ⟨rfl, hblk⟩))) (Q := Q) (e := e) htQ) ?_ + rintro s4 ⟨h1, h1', h2⟩ + exact ⟨h1, h1', FpEA.trans hF (FpEA.of_sw hw3) (ssz_updK_reply σ2 _ _) h2⟩ + +/-- A TCB state write keeps, from a state that is not `blockedOnNotification`. -/ +theorem epk_sts {σ : Fl4SpecA64.State Unit} {t : Fl4SpecA64.ObjRef} {x : Fl4SpecA64.Tcb} + (hk : σ.kheap t = some (.tcb x)) + (hn : ((∀ n, x.tcbState ≠ .blockedOnNotification n) ∧ ∀ r, x.tcbState ≠ .blockedOnReply r)) + {ts : Fl4SpecA64.ThreadState} (hts : ts = .restart ∨ ts = .inactive) : + EpSpecKeepA σ (stsAct (updK σ t (.tcb { x with tcbState := ts })) t) := + (EpSpecKeepA.of_upd hk (o' := .tcb { x with tcbState := ts }) + ⟨_, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, Or.inr ⟨hn, hts⟩⟩).trans + (EpSpecKeepA.of_kheap (stsAct_kheap _ _)) + +/-- **`restart_thread_if_no_fault`** (`restart_relO`) with the footprint, the keep and the invariants. -/ +theorem restart_sigE (hO : SchedOpsEA st T E R Bf SR) (hF : SchedFootEA st T E R Bf SR Wr) (hP : EPlaced T E R Bf) + (s : S) (σ : Fl4SpecA64.State Unit) (hR : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) {t : U64} + (ht : t ∈ T) {x : Fl4SpecA64.Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) + (hblk : RestartOK σ (specPtr t) x) (hon : L.mem s (affA t) = L.cpu s) + (hn : ((∀ n, x.tcbState ≠ .blockedOnNotification n) ∧ ∀ r, x.tcbState ≠ .blockedOnReply r)) {Q : Set U64} {e : U64} (htQ : t ∈ Q) : + endpoint.restart_thread_if_no_fault st s t ⦃ s' => + ¬ (Fl4SpecA64.restartThreadIfNoFault (Z := Unit) (specPtr t) σ).2 ∧ + ∃ σ', ((), σ') ∈ (Fl4SpecA64.restartThreadIfNoFault (Z := Unit) (specPtr t) σ).1 ∧ + EpStateRel T E R Bf (L.mem s') σ' ∧ SR s' σ' ∧ + (∀ p y, σ.kheap p = some (.endpoint y) → σ'.kheap p = some (.endpoint y)) ∧ KeepO σ σ' (specPtr t) ∧ + EpSpecKeepA σ σ' ∧ SSzA σ σ' ∧ FpEA st R Wr Q e σ s s' ∧ + (∀ m n, LoopInvA σ m n → OffCurA σ m n (specPtr t) → LoopInvA σ' m n) ⦄ := by + have hx := hR.at_tcb ht hk + unfold endpoint.restart_thread_if_no_fault + step with obj.Tcb.fault_type.spec as ⟨i, hi⟩ + by_cases h0 : i = fl4_abi.consts.fault.NullFault + · rw [if_pos h0] + have hf : x.tcbFault = none := hx.fault0.1 (by rw [← hi, h0]; unfold fl4_abi.consts.fault.NullFault; rfl) + have hcode : sched.TS_RESTART.val = tsCode .restart := by unfold sched.TS_RESTART; rfl + refine WP.spec_bind (set_thread_state_sigE hO hF.to0 hP hSR s σ hR (WritesIn.refl _ _) (EpStep.refl _) ht hk hq + .restart (by decide) sched.TS_RESTART hcode (NoBlockE.updT hk _ (Or.inr (Or.inl ⟨rfl, hblk.2.1, hblk.2.2 hf⟩))) + (Q := Q) (e := e) htQ) ?_ + rintro s1 ⟨hR1, hS1, hw1⟩ + obtain ⟨hk1, hq1, hkeep, hkT⟩ := sts_facts hk hq .restart + have hon1 := (hO.coreKeep _ _ _ _ hSR hS1).onCore ht hon + have hNQ := (hO.notQueued s σ t x hSR hk hblk.1).sts (specPtr t) (.tcb { x with tcbState := .restart }) (specPtr t) + have z1 : SSzA σ (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := .restart })) (specPtr t)) := + (ssz_updK_tcb σ _ _).trans (ssz_stsAct _ _) + refine WP.spec_mono (WP.spec_and + (hO.restartTail s1 _ t _ hR1 hS1 ht hk1 (Or.inr rfl) hq1 hon1 hNQ) + (hF.restartTail s1 _ t _ hR1 hS1 ht hk1 (Or.inr rfl) hq1 hon1 hNQ)) ?_ + rintro s2 ⟨⟨hnf, σ', hm, hf2, hv, hS2⟩, hw2⟩ + rw [restartThreadIfNoFault_eq hk hf] + refine ⟨hnf, σ', hm, hR1.frame hP hf2 hv, hS2, fun p y hy => hv.1 _ _ (hkeep p y hy), + hkT.trans (KeepO.of_kview hv _), (epk_sts hk hn (Or.inl rfl)).trans (specRestartTail_epkA hm), + z1.trans (specRestartTail_ssz _ hm), FpEA.trans hF.to0 hw1 z1 (FpEA.of_wr hw2), fun m n h o => ?_⟩ + have h1 := h.setBlk hk hblk.1 o (Or.inl rfl) (fun _ => hf) + have o1 : OffCurA (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := .restart })) (specPtr t)) m n (specPtr t) := + (o.updKA _ _).stsA _ + exact h1.restartTailA hk1 rfl o1 hm + · rw [if_neg h0] + have hf : x.tcbFault ≠ none := fun e => h0 (by rw [hi, hx.fault0.2 e]; unfold fl4_abi.consts.fault.NullFault; rfl) + have hcode : sched.TS_INACTIVE.val = tsCode .inactive := by unfold sched.TS_INACTIVE; rfl + refine WP.spec_mono (set_thread_state_sigE hO hF.to0 hP hSR s σ hR (WritesIn.refl _ _) (EpStep.refl _) ht hk hq + .inactive (by decide) sched.TS_INACTIVE hcode (NoBlockE.updT hk _ (Or.inr (Or.inr ⟨rfl, hblk.1, hblk.2.1⟩))) + (Q := Q) (e := e) htQ) ?_ + rintro s1 ⟨hR1, hS1, hw1⟩ + obtain ⟨-, -, hkeep, hkT⟩ := sts_facts hk hq .inactive + rw [restartThreadIfNoFault_fault hk hf] + exact ⟨fun h => h, _, rfl, hR1, hS1, hkeep, hkT, epk_sts hk hn (Or.inr rfl), + (ssz_updK_tcb σ _ _).trans (ssz_stsAct _ _), hw1, fun m n h o => h.setBlk hk hblk.1 o (Or.inr rfl) (fun e => by cases e)⟩ + +end Fl4 + +end + +end Fl4ProofsA64.P3.Endp + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched + +noncomputable section + +/-! ## The loop -/ + +section LoopF +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {T E R : Set U64} + {Bf : Set (U64 × Fl4SpecA64.Word)} {SR : S → Fl4SpecA64.State Unit → Prop} + {Wr : Fl4SpecA64.State Unit → S → U64 → Prop} + +/-- What the loop carries from its entry `(σ0, s0)` to `(σ', s)`. -/ +structure LKeepA (st : machine.Store S) [L : LawfulStore st] (R : Set U64) + (Wr : Fl4SpecA64.State Unit → S → U64 → Prop) (Q : Set U64) (e : U64) (σ0 : Fl4SpecA64.State Unit) (s0 : S) + (σ' : Fl4SpecA64.State Unit) (s : S) : Prop where + keep : EpSpecKeepA σ0 σ' + sz : SSzA σ0 σ' + fp : FpEA st R Wr Q e σ0 s0 s + inv : ∀ m n, LoopInvA σ0 m n → LoopInvA σ' m n + +theorem LKeepA.refl {Q : Set U64} {e : U64} (σ : Fl4SpecA64.State Unit) (s : S) : LKeepA st R Wr Q e σ s σ s := + ⟨EpSpecKeepA.refl σ, SSzA.refl σ, FpEA.refl σ s, fun _ _ h => h⟩ + +variable (st T E R Bf SR Wr) in +/-- `AInv` with the keep from the loop's entry and the remaining queue inside `Q`. -/ +def AInvF (Q : Set U64) (e : U64) (σ0 : Fl4SpecA64.State Unit) (s0 : S) (q0 : List Fl4SpecA64.ObjRef) + (x : S × U64 × U64) : Prop := + ∃ σ' y, ∃ rest : List U64, EpStateRel T E R Bf (L.mem x.1) σ' ∧ SR x.1 σ' ∧ + σ'.kheap (specPtr e) = some (.endpoint y) ∧ y.epQueue = rest.map specPtr ∧ x.2.1 = rest.headD 0#u64 ∧ + rest.length ≤ x.2.2.val ∧ (∀ t ∈ rest, RepOKA σ' (specPtr t)) ∧ + (∀ t ∈ rest, t ∈ T → L.mem x.1 (affA t) = L.cpu x.1) ∧ + (∀ z, z ∈ (Fl4Spec.NonDet.mapMx (rra e) (rest.map specPtr) σ').1 → + z ∈ (Fl4Spec.NonDet.mapMx (rra e) q0 σ0).1) ∧ + ((Fl4Spec.NonDet.mapMx (rra e) q0 σ0).2 → (Fl4Spec.NonDet.mapMx (rra e) (rest.map specPtr) σ').2) ∧ + LKeepA st R Wr Q e σ0 s0 σ' x.1 ∧ (∀ t ∈ rest, t ∈ Q) + +/-- The restart of the dequeued head from the state `σr` the dequeue (and the reply unlink) left, re-establishing the +invariant (a separate declaration, see `restart_spine`; gen 6: from any such `σr` with `RestartOK`). -/ +theorem all_tailF (hO : SchedOpsEA st T E R Bf SR) (hF : SchedFootEA st T E R Bf SR Wr) (hP : EPlaced T E R Bf) + {Q : Set U64} {e : U64} (he : e ∈ E) (σ0 : Fl4SpecA64.State Unit) (s0 : S) (q0 : List Fl4SpecA64.ObjRef) {s s1 : S} {σ' σr : Fl4SpecA64.State Unit} + {y : Fl4SpecA64.Endpoint} {rest' : List U64} {t0 nx fuel1 : U64} + (hR : EpStateRel T E R Bf (L.mem s) σ') (hky : σ'.kheap (specPtr e) = some (.endpoint y)) + (hq : y.epQueue = (t0 :: rest').map specPtr) (hnd : (t0 :: rest').Nodup) (ht0T : t0 ∈ T) + (hRr : EpStateRel T E R Bf (L.mem s1) σr) (hSr : SR s1 σr) {xr : Fl4SpecA64.Tcb} + (hktr : σr.kheap (specPtr t0) = some (.tcb xr)) (hok : RestartOK σr (specPtr t0) xr) + (hqr : ∀ e' ∈ E, ∀ z, σr.kheap (specPtr e') = some (.endpoint z) → specPtr t0 ∉ z.epQueue) + (hkyr : σr.kheap (specPtr e) = some (.endpoint (epDeq y (specPtr t0)))) (hkTr : KeepO σ' σr (specPtr t0)) + (hrr : Fl4SpecA64.removeAndRestartEpQueuedThread (Z := Unit) (specPtr t0) (specPtr e) σ' = + Fl4SpecA64.restartThreadIfNoFault (specPtr t0) σr) + (hrep : ∀ t ∈ t0 :: rest', RepOKA σ' (specPtr t)) + (hnx : nx = rest'.headD 0#u64) (hlen : rest'.length ≤ fuel1.val) + (hon : ∀ t ∈ t0 :: rest', t ∈ T → L.mem s1 (affA t) = L.cpu s1) + (hinc : ∀ z, z ∈ (Fl4Spec.NonDet.mapMx (rra e) ((t0 :: rest').map specPtr) σ').1 → + z ∈ (Fl4Spec.NonDet.mapMx (rra e) q0 σ0).1) + (hfail : (Fl4Spec.NonDet.mapMx (rra e) q0 σ0).2 → + (Fl4Spec.NonDet.mapMx (rra e) ((t0 :: rest').map specPtr) σ').2) + (K : LKeepA st R Wr Q e σ0 s0 σr s1) (hoc : ∀ m n, LoopInvA σ0 m n → OffCurA σr m n (specPtr t0)) + (hn : ((∀ n, xr.tcbState ≠ .blockedOnNotification n) ∧ ∀ r, xr.tcbState ≠ .blockedOnReply r)) (hqs : ∀ t ∈ t0 :: rest', t ∈ Q) : + (do + let s3 ← endpoint.restart_thread_if_no_fault st s1 t0 + ok (ControlFlow.cont (s3, nx, fuel1) : ControlFlow (S × U64 × U64) S)) ⦃ r => + ∃ x' : S × U64 × U64, r = (ControlFlow.cont x' : ControlFlow (S × U64 × U64) S) ∧ + AInvF st T E R Bf SR Wr Q e σ0 s0 q0 x' ∧ x'.2.2 = fuel1 ⦄ := by + have hrq : (epDeq y (specPtr t0)).epQueue = rest'.map specPtr := by + rw [epDeq_queue, hq, removeAll_map hnd, List.erase_cons_head] + refine WP.spec_bind (restart_sigE hO hF hP s1 σr hRr hSr ht0T hktr hqr hok (hon t0 List.mem_cons_self ht0T) hn + (Q := Q) (e := e) (hqs t0 List.mem_cons_self)) ?_ + rintro s2 ⟨hnf, σ'', hres, hR2, hS2, hkeep, hkT, hK, hZ, hW, hI⟩ + rw [WP.spec_ok] + refine ⟨_, rfl, ⟨σ'', epDeq y (specPtr t0), rest', hR2, hS2, hkeep _ _ hkyr, hrq, hnx, hlen, ?_, + fun t ht hT => (hO.coreKeep _ _ _ _ hSr hS2).onCore hT (hon t (List.mem_cons_of_mem _ ht) hT), + fun z hz => hinc z (mapMx_cons_mem (g := rra e) (l := rest'.map specPtr) + (by simp only [rra]; rw [hrr]; exact hres) z hz), + fun hw => Det.detAt_bind_fail ((Det.det_removeAndRestartEp _ _).at σ') + (by rw [hrr]; exact hnf) + (by rw [hrr]; exact hres) (hfail hw), + ⟨K.keep.trans hK, K.sz.trans hZ, FpEA.trans hF.to0 K.fp K.sz hW, + fun m n h => hI m n (K.inv m n h) (hoc m n h)⟩, + fun t ht => hqs t (List.mem_cons_of_mem _ ht)⟩, rfl⟩ + intro t ht + have htt : t ≠ t0 := fun e' => (List.nodup_cons.1 hnd).1 (e' ▸ ht) + have htT : t ∈ T := (hR.at_ep he hky).blocked (by rw [hq]; exact List.mem_map_of_mem (List.mem_cons_of_mem _ ht)) |>.1 + obtain ⟨z, hz, -⟩ := hR.tcb t htT + exact (hrep t (List.mem_cons_of_mem _ ht)).of_keepO (hkTr.trans hkT) (fun h => htt (specPtr_inj h)) hz + +/-- (gen 6, ruling (a)) **The restart of a receiver after `reply_unlink`**, from the `stsAct` state the unlink leaves +(a separate declaration): `SchedOpsEA.rd` before the unlink (the receiver in `inClassR`) gives `RelIfBound`, carried +across the reply word and the state word (`RelIfBound.keep`), so `Inactive → Restart` is `NoBlockE`'s branch 2. -/ +theorem all_replyF (hO : SchedOpsEA st T E R Bf SR) (hF : SchedFootEA st T E R Bf SR Wr) (hP : EPlaced T E R Bf) + {Q : Set U64} {e : U64} (he : e ∈ E) (σ0 : Fl4SpecA64.State Unit) (s0 : S) (q0 : List Fl4SpecA64.ObjRef) {s s3 : S} {σ' : Fl4SpecA64.State Unit} + {y : Fl4SpecA64.Endpoint} {rest' : List U64} {t0 nx fuel1 : U64} + (hR : EpStateRel T E R Bf (L.mem s) σ') (hky : σ'.kheap (specPtr e) = some (.endpoint y)) + (hq : y.epQueue = (t0 :: rest').map specPtr) (hnd : (t0 :: rest').Nodup) (ht0T : t0 ∈ T) + {xt : Fl4SpecA64.Tcb} (hkt : σ'.kheap (specPtr t0) = some (.tcb xt)) {ep : Fl4SpecA64.ObjRef} + {d : Fl4SpecA64.ReceiverPayload} {i : U64} (hst : xt.tcbState = .blockedOnReceive ep (some (specPtr i)) d) + {xr : Fl4SpecA64.Reply} (hkr : σ'.kheap (specPtr i) = some (.reply xr)) (hback : xr.replyTcb = some (specPtr t0)) + (hdq : Fl4SpecA64.tcbEpDequeue (Z := Unit) (specPtr t0) (specPtr e) σ' = + ({((), updK σ' (specPtr e) (.endpoint (epDeq y (specPtr t0))))}, False)) + (hrel : RelIfBound (updK σ' (specPtr e) (.endpoint (epDeq y (specPtr t0)))) (specPtr t0)) + (hq1 : ∀ e' ∈ E, ∀ z, (updK σ' (specPtr e) (.endpoint (epDeq y (specPtr t0)))).kheap (specPtr e') = + some (.endpoint z) → specPtr t0 ∉ z.epQueue) + (hR3 : EpStateRel T E R Bf (L.mem s3) (stsAct (updK (updK (updK σ' (specPtr e) (.endpoint (epDeq y (specPtr t0)))) + (specPtr i) (.reply { xr with replyTcb := none })) (specPtr t0) (.tcb { xt with tcbState := .inactive })) (specPtr t0))) + (hS3 : SR s3 (stsAct (updK (updK (updK σ' (specPtr e) (.endpoint (epDeq y (specPtr t0)))) + (specPtr i) (.reply { xr with replyTcb := none })) (specPtr t0) (.tcb { xt with tcbState := .inactive })) (specPtr t0))) + (hrep : ∀ t ∈ t0 :: rest', RepOKA σ' (specPtr t)) + (hnx : nx = rest'.headD 0#u64) (hlen : rest'.length ≤ fuel1.val) + (hon : ∀ t ∈ t0 :: rest', t ∈ T → L.mem s3 (affA t) = L.cpu s3) + (hinc : ∀ z, z ∈ (Fl4Spec.NonDet.mapMx (rra e) ((t0 :: rest').map specPtr) σ').1 → + z ∈ (Fl4Spec.NonDet.mapMx (rra e) q0 σ0).1) + (hfail : (Fl4Spec.NonDet.mapMx (rra e) q0 σ0).2 → + (Fl4Spec.NonDet.mapMx (rra e) ((t0 :: rest').map specPtr) σ').2) + (K : LKeepA st R Wr Q e σ0 s0 (stsAct (updK (updK (updK σ' (specPtr e) (.endpoint (epDeq y (specPtr t0)))) + (specPtr i) (.reply { xr with replyTcb := none })) (specPtr t0) (.tcb { xt with tcbState := .inactive })) + (specPtr t0)) s3) + (hoc : ∀ m n, LoopInvA σ0 m n → OffCurA (stsAct (updK (updK (updK σ' (specPtr e) + (.endpoint (epDeq y (specPtr t0)))) (specPtr i) (.reply { xr with replyTcb := none })) (specPtr t0) + (.tcb { xt with tcbState := .inactive })) (specPtr t0)) m n (specPtr t0)) + (hqs : ∀ t ∈ t0 :: rest', t ∈ Q) : + (do + let s4 ← endpoint.restart_thread_if_no_fault st s3 t0 + ok (ControlFlow.cont (s4, nx, fuel1) : ControlFlow (S × U64 × U64) S)) ⦃ r => + ∃ x' : S × U64 × U64, r = (ControlFlow.cont x' : ControlFlow (S × U64 × U64) S) ∧ + AInvF st T E R Bf SR Wr Q e σ0 s0 q0 x' ∧ x'.2.2 = fuel1 ⦄ := by + have hte : specPtr t0 ≠ specPtr e := by intro h; rw [h, hky] at hkt; cases hkt + have hie : specPtr i ≠ specPtr e := by intro h; rw [h, hky] at hkr; cases hkr + have hit : specPtr i ≠ specPtr t0 := by intro h; rw [h, hkt] at hkr; cases hkr + have hktd : (updK σ' (specPtr e) (.endpoint (epDeq y (specPtr t0)))).kheap (specPtr t0) = some (.tcb xt) := by + rw [updK_kheap, Function.update_of_ne hte, hkt] + have hkrd : (updK σ' (specPtr e) (.endpoint (epDeq y (specPtr t0)))).kheap (specPtr i) = some (.reply xr) := by + rw [updK_kheap, Function.update_of_ne hie, hkr] + have hk3 : (updK (updK σ' (specPtr e) (.endpoint (epDeq y (specPtr t0)))) (specPtr i) + (.reply { xr with replyTcb := none })).kheap (specPtr t0) = some (.tcb xt) := by + rw [updK_kheap, Function.update_of_ne hit.symm, hktd] + obtain ⟨hk4, hq4, hkeep4, hkT4⟩ := sts_facts hk3 (notq_updR (E := E) hq1 i { xr with replyTcb := none }) .inactive + exact all_tailF hO hF hP he σ0 s0 q0 hR hky hq hnd ht0T hR3 hS3 hk4 (unlink_ok (i := i) (xr := xr) hktd hrel) hq4 + (hkeep4 _ _ (by rw [updK_kheap, Function.update_of_ne hie.symm, updK_kheap, Function.update_self])) + (((KeepO.updE hky _ _).trans (KeepO.updR hkrd hback _)).trans hkT4) + (rra_reply hdq hktd hst (replyUnlinkTcb_eq hkrd hback hktd hst)) hrep hnx hlen hon hinc hfail + K hoc ⟨(fun _ h => by cases h), (fun _ h => by cases h)⟩ hqs + +/-- **One dequeue-and-restart step of the loop** (FL4's step after the successor read; gen 6: a receiver holding a +reply object too, `RepOKA`). -/ +theorem all_deqF (hO : SchedOpsEA st T E R Bf SR) (hF : SchedFootEA st T E R Bf SR Wr) (hP : EPlaced T E R Bf) + {Q : Set U64} {e : U64} (he : e ∈ E) (σ0 : Fl4SpecA64.State Unit) (s0 : S) (q0 : List Fl4SpecA64.ObjRef) {s : S} {σ' : Fl4SpecA64.State Unit} + {y : Fl4SpecA64.Endpoint} {rest' : List U64} {t0 nx fuel1 : U64} + (hR : EpStateRel T E R Bf (L.mem s) σ') (hSR : SR s σ') (hky : σ'.kheap (specPtr e) = some (.endpoint y)) + (hq : y.epQueue = (t0 :: rest').map specPtr) + (hQ : QueueRel (L.mem s) T .Ep (qOf (L.mem s) e) (t0 :: rest')) (ht0T : t0 ∈ T) {xt : Fl4SpecA64.Tcb} + (hkt : σ'.kheap (specPtr t0) = some (.tcb xt)) (hbe : L.mem s (tBlockA t0) = e) + (hc : tsCode xt.tcbState = 3 ∨ tsCode xt.tcbState = 4) + (hrep : ∀ t ∈ t0 :: rest', RepOKA σ' (specPtr t)) + (hnx : nx = rest'.headD 0#u64) (hlen : rest'.length ≤ fuel1.val) + (hon : ∀ t ∈ t0 :: rest', t ∈ T → L.mem s (affA t) = L.cpu s) + (hinc : ∀ z, z ∈ (Fl4Spec.NonDet.mapMx (rra e) ((t0 :: rest').map specPtr) σ').1 → + z ∈ (Fl4Spec.NonDet.mapMx (rra e) q0 σ0).1) + (hfail : (Fl4Spec.NonDet.mapMx (rra e) q0 σ0).2 → + (Fl4Spec.NonDet.mapMx (rra e) ((t0 :: rest').map specPtr) σ').2) + (K : LKeepA st R Wr Q e σ0 s0 σ' s) (hqs : ∀ t ∈ t0 :: rest', t ∈ Q) : + (do + let s1 ← endpoint.ep_dequeue st s e t0 + let i ← obj.Tcb.state st t0 s1 + let s2 ← + if i = sched.TS_BLOCKED_ON_RECV + then + do + let i1 ← obj.Tcb.reply_obj st t0 s1 + if i1 != 0#u64 + then ipc.reply_unlink st s1 i1 t0 + else ok s1 + else ok s1 + let s3 ← endpoint.restart_thread_if_no_fault st s2 t0 + ok (ControlFlow.cont (s3, nx, fuel1) : ControlFlow (S × U64 × U64) S)) ⦃ r => + ∃ x' : S × U64 × U64, r = (ControlFlow.cont x' : ControlFlow (S × U64 × U64) S) ∧ + AInvF st T E R Bf SR Wr Q e σ0 s0 q0 x' ∧ x'.2.2 = fuel1 ⦄ := by + have hd : specPtr t0 ∈ y.epQueue := by rw [hq]; simp + refine WP.spec_bind (WP.spec_and (ep_dequeue_rel hO hP s σ' hR hSR he hky hd) + (ep_dequeue_sigE hP s he t0 (t0 :: rest') hQ List.mem_cons_self (Q := Q) hqs)) ?_ + rintro s1 ⟨⟨hdq, hR1, hS1, hA1⟩, hw1⟩ + have hte : specPtr t0 ≠ specPtr e := by intro h; rw [h, hky] at hkt; cases hkt + have hktd : (updK σ' (specPtr e) (.endpoint (epDeq y (specPtr t0)))).kheap (specPtr t0) = some (.tcb xt) := by + rw [updK_kheap, Function.update_of_ne hte, hkt] + have hkyd : (updK σ' (specPtr e) (.endpoint (epDeq y (specPtr t0)))).kheap (specPtr e) = + some (.endpoint (epDeq y (specPtr t0))) := by rw [updK_kheap, Function.update_self] + have hta := hP.tcb t0 ht0T + have hb1 : L.mem s1 (tBlockA t0) = e := (hA1 _ (a_blk hta).1 ⟨t0, ht0T, (a_blk hta).2⟩).trans hbe + have hx1 := hR1.at_tcb ht0T hktd + have hq1 := dequeued_not_queued hR1 he hb1 hkyd (epDeq_not_mem hq hQ.nodup t0) + have hblk := blk_of_code hc + have K1 : LKeepA st R Wr Q e σ0 s0 (updK σ' (specPtr e) (.endpoint (epDeq y (specPtr t0)))) s1 := + ⟨K.keep.trans (EpSpecKeepA.of_upd hky ⟨_, rfl⟩), K.sz.trans (ssz_of_epStep (EpStep.ep hky)), + FpEA.trans hF.to0 K.fp K.sz (FpEA.of_sw hw1), fun m n h => (K.inv m n h).updEp hky⟩ + have oc1 : ∀ m n, LoopInvA σ0 m n → OffCurA (updK σ' (specPtr e) (.endpoint (epDeq y (specPtr t0)))) m n + (specPtr t0) := fun m n h => (K1.inv m n h).offCur hktd hc + have hnn : (∀ n, xt.tcbState ≠ .blockedOnNotification n) ∧ ∀ r, xt.tcbState ≠ .blockedOnReply r := + ⟨(fun n h => by rw [h] at hc; simp [tsCode] at hc), (fun r h => by rw [h] at hc; simp [tsCode] at hc)⟩ + have hcont := fun (hro : Fl4SpecA64.replyObject xt.tcbState = none) => + all_tailF hO hF hP he σ0 s0 q0 hR hky hq hQ.nodup ht0T hR1 hS1 hktd (RestartOK.of_blk hblk) hq1 hkyd + (KeepO.updE hky _ _) (rra_eq hdq hktd hro) hrep hnx hlen + (fun t ht hT => (hO.coreKeep _ _ _ _ hSR hS1).onCore hT (hon t ht hT)) hinc hfail K1 oc1 hnn hqs + step with obj.Tcb.state.spec as ⟨i, hi⟩ + by_cases hr : i = sched.TS_BLOCKED_ON_RECV + · simp only [hr, ↓reduceIte] + have h3 : tsCode xt.tcbState = 3 := by + rw [← hx1.st, ← hi, hr]; unfold sched.TS_BLOCKED_ON_RECV; rfl + obtain ⟨e', r', d, hst⟩ := tsCode_three h3 + have hrec := hx1.recv e' r' d hst + step with obj.Tcb.reply_obj.spec as ⟨i1, hi1⟩ + have hi1' : i1 = L.mem s1 (tReplyA t0) := hi1 + by_cases hi0 : i1 = 0#u64 + · have hro : r' = none := by rw [← hrec.1, ← hi1', hi0, optRef_zero] + simp only [hi0, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte, bind_ok] + exact hcont (by rw [hst, hro]; rfl) + · have hro : r' = some (specPtr i1) := by rw [← hrec.1, ← hi1', optRef_ne hi0] + subst hro + have hiR : i1 ∈ R := by rw [hi1']; exact hrec.2 (by simp) + obtain ⟨xr, hkr, hback⟩ := hrep t0 (by simp) xt hkt _ _ d hst + have hie : specPtr i1 ≠ specPtr e := by intro h; rw [h, hky] at hkr; cases hkr + have hkr1 : (updK σ' (specPtr e) (.endpoint (epDeq y (specPtr t0)))).kheap (specPtr i1) = some (.reply xr) := by + rw [updK_kheap, Function.update_of_ne hie, hkr] + have hrel := hO.rd _ _ _ _ hS1 hktd (Or.inr (Or.inr (Or.inr ⟨_, _, _, hst⟩))) + have hnz : (i1 != 0#u64) = true := by simpa using hi0 + simp only [hnz, ↓reduceIte] + refine WP.spec_bind (reply_unlink_sigE hO hF.to0 hP hS1 hR1 (WritesIn.refl _ _) (EpStep.refl _) ht0T hktd hktd + (OnlyT.refl _ _) ⟨hblk.2, hblk.1.ne_idle hblk.2⟩ hq1 hiR hkr1 (Q := Q) (e := e) + (hqs t0 List.mem_cons_self)) ?_ + rintro s3 ⟨hR3, hS3, hw3⟩ + have hit : specPtr i1 ≠ specPtr t0 := by intro h; rw [h, hktd] at hkr1; cases hkr1 + have hk3 : (updK (updK σ' (specPtr e) (.endpoint (epDeq y (specPtr t0)))) (specPtr i1) + (.reply { xr with replyTcb := none })).kheap (specPtr t0) = some (.tcb xt) := by + rw [updK_kheap, Function.update_of_ne hit.symm, hktd] + have o3 : ∀ m n, LoopInvA σ0 m n → OffCurA (updK (updK σ' (specPtr e) (.endpoint (epDeq y (specPtr t0)))) + (specPtr i1) (.reply { xr with replyTcb := none })) m n (specPtr t0) := fun m n h => + (oc1 m n h).updKA _ _ + have K3 : LKeepA st R Wr Q e σ0 s0 (stsAct (updK (updK (updK σ' (specPtr e) + (.endpoint (epDeq y (specPtr t0)))) (specPtr i1) (.reply { xr with replyTcb := none })) (specPtr t0) + (.tcb { xt with tcbState := .inactive })) (specPtr t0)) s3 := + ⟨K1.keep.trans ((EpSpecKeepA.of_upd hkr1 (o' := .reply { xr with replyTcb := none }) ⟨_, rfl, rfl⟩).trans + (epk_sts hk3 hnn (Or.inr rfl))), + K1.sz.trans ((ssz_updK_reply _ _ _).trans ((ssz_updK_tcb _ _ _).trans (ssz_stsAct _ _))), + FpEA.trans hF.to0 K1.fp K1.sz hw3, + fun m n h => ((K1.inv m n h).updReply hkr1).setBlk hk3 hblk.2 (o3 m n h) (Or.inr rfl) (fun e => by cases e)⟩ + refine all_replyF hO hF hP he σ0 s0 q0 hR hky hq hQ.nodup ht0T hkt hst hkr hback hdq hrel hq1 hR3 hS3 hrep hnx + hlen (fun t ht hT => (hO.coreKeep _ _ _ _ hSR hS3).onCore hT (hon t ht hT)) hinc hfail K3 + (fun m n h => ((o3 m n h).updKA _ _).stsA _) hqs + · simp only [hr, ↓reduceIte, bind_ok] + refine hcont ?_ + rcases hc with h3 | h4 + · exact absurd (UScalar.eq_of_val_eq (by rw [hi, hx1.st, h3]; unfold sched.TS_BLOCKED_ON_RECV; rfl)) hr + · obtain ⟨e', d, hst⟩ := tsCode_four h4; rw [hst]; rfl + +set_option maxHeartbeats 4000000 in +/-- **The loop of `cancel_all_ipc`** on `EpStateRel ∧ SR`. -/ +theorem all_loopF (hO : SchedOpsEA st T E R Bf SR) (hF : SchedFootEA st T E R Bf SR Wr) (hP : EPlaced T E R Bf) + {Q : Set U64} {e : U64} (he : e ∈ E) (σ0 : Fl4SpecA64.State Unit) (s0 : S) (q0 : List Fl4SpecA64.ObjRef) + (x0 : S × U64 × U64) (h0 : AInvF st T E R Bf SR Wr Q e σ0 s0 q0 x0) : + endpoint.cancel_all_ipc_loop st x0.1 e x0.2.1 x0.2.2 ⦃ s' => ¬ (Fl4Spec.NonDet.mapMx (rra e) q0 σ0).2 ∧ + ∃ σf, ((), σf) ∈ (Fl4Spec.NonDet.mapMx (rra e) q0 σ0).1 ∧ EpStateRel T E R Bf (L.mem s') σf ∧ SR s' σf ∧ + LKeepA st R Wr Q e σ0 s0 σf s' ⦄ := by + unfold endpoint.cancel_all_ipc_loop + apply loop.spec_decr_nat (measure := fun x : S × U64 × U64 => x.2.2.val) + (inv := AInvF st T E R Bf SR Wr Q e σ0 s0 q0) _ _ x0 ?_ h0 + rintro ⟨s, t, fuel⟩ ⟨σ', y, rest, hR, hSR, hky, hq, ht, hlen, hrep, hon, hinc, hfail, K, hqs⟩ + simp only at hR hSR hky hq ht hlen hrep hon hinc hfail K hqs ⊢ + have hEP := hR.at_ep he hky + obtain ⟨xs, hQ, hxs, hB⟩ := hEP.queue + have hxs' : xs = rest := by + rw [hq] at hxs + exact (List.map_injective_iff.2 (fun a b h => specPtr_inj h) hxs).symm + subst hxs' + unfold endpoint.cancel_all_ipc_loop.body + step with obj.Tcb.is_null.spec as ⟨bn, hbn⟩ + cases xs with + | nil => + simp only [List.headD_nil] at ht + subst ht + simp only [hbn, decide_true, ↓reduceIte, WP.spec_ok] + exact ⟨fun h => hfail h, σ', hinc _ rfl, hR, hSR, K⟩ + | cons t0 rest' => + simp only [List.headD_cons] at ht + subst t + obtain ⟨ht0T, ht00, hf0⟩ := cursor_facts hP hQ hlen + simp only [hbn, ht00, decide_false, Bool.false_eq_true, ↓reduceIte, hf0] + step as ⟨fuel1, hfuel1⟩ + have hseg := hQ.seg + rw [show t0 :: rest' = [] ++ t0 :: rest' from rfl, Seg_mid] at hseg + obtain ⟨-, -, hnext, -⟩ := hseg + unfold tcbqueue.next_of + step with tcbqueue.next.spec as ⟨nx, hnx⟩ + try simp only [bind_ok] + obtain ⟨hbe, hc⟩ := hB t0 (by simp) + obtain ⟨xt, hkt, hxt⟩ := hR.tcb t0 ht0T + refine WP.spec_mono (all_deqF hO hF hP he σ0 s0 q0 hR hSR hky hq hQ ht0T hkt hbe (by rw [← hxt.st]; exact hc) hrep (by rw [hnx, hnext]) + (by rw [hfuel1]; simp only [List.length_cons] at hlen; scalar_tac) hon hinc hfail K hqs) ?_ + rintro r ⟨x', rfl, hI, hf⟩ + exact ⟨hI, by rw [hf, hfuel1]; scalar_tac⟩ + +set_option maxHeartbeats 4000000 in +/-- **`cancel_all_ipc` refines `cancel_all_ipc`** on `EpStateRel ∧ SR`, the spec not failing, for an endpoint each of +whose queued threads has `RepOKA` (`hrep`; gen 6: receivers holding a reply object included). -/ +theorem cancel_all_relF (hO : SchedOpsEA st T E R Bf SR) (hF : SchedFootEA st T E R Bf SR Wr) + (hP : EPlaced T E R Bf) + (s : S) (σ : Fl4SpecA64.State Unit) (hR : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) {e : U64} (he : e ∈ E) + (hrep : ∀ y, σ.kheap (specPtr e) = some (.endpoint y) → ∀ t ∈ y.epQueue, RepOKA σ t) + (hon : ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → ∀ t ∈ T, specPtr t ∈ z.epQueue → + L.mem s (affA t) = L.cpu s) + {Q : Set U64} (hQs : ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → ∀ t ∈ T, specPtr t ∈ z.epQueue → t ∈ Q) : + endpoint.cancel_all_ipc st s e ⦃ s' => ¬ (Fl4SpecA64.cancelAllIpc (Z := Unit) (specPtr e) σ).2 ∧ + ∃ σ', ((), σ') ∈ (Fl4SpecA64.cancelAllIpc (Z := Unit) (specPtr e) σ).1 ∧ + EpStateRel T E R Bf (L.mem s') σ' ∧ SR s' σ' ∧ LKeepA st R Wr Q e σ s σ' s' ⦄ := by + obtain ⟨y, hky, hEP⟩ := hR.ep e he + unfold endpoint.cancel_all_ipc + step with obj.Ep.state.spec as ⟨i, hi⟩ + rw [epState_eq] at hi + by_cases hid : y = .idleEP + · subst hid + have h0 : i = obj.EP_IDLE := by rw [hi, hEP.1, ep_state_vals.1] + have hsp : Fl4SpecA64.cancelAllIpc (Z := Unit) (specPtr e) σ = ({((), σ)}, False) := by + unfold Fl4SpecA64.cancelAllIpc + rw [nd_bind_one (getEndpoint_eq hky)] + rfl + simp only [h0, ↓reduceIte, WP.spec_ok] + exact ⟨by rw [hsp]; exact fun h => h, σ, by rw [hsp]; rfl, hR, hSR, LKeepA.refl σ s⟩ + have hsp : Fl4SpecA64.cancelAllIpc (Z := Unit) (specPtr e) σ = + (Fl4Spec.NonDet.mapMx (rra e) y.epQueue >>= fun _ => Fl4SpecA64.rescheduleRequired) σ := by + unfold Fl4SpecA64.cancelAllIpc + rw [nd_bind_one (getEndpoint_eq hky)] + cases y with + | idleEP => exact absurd rfl hid + | sendEP q => exact nd_pure_bind' _ _ _ + | recvEP q => exact nd_pure_bind' _ _ _ + have h0 : i ≠ obj.EP_IDLE := by + rw [hi] + cases y with + | idleEP => exact absurd rfl hid + | sendEP q => rw [hEP.1, ep_state_vals.1]; decide + | recvEP q => rw [hEP.1, ep_state_vals.1]; decide + simp only [h0, ↓reduceIte] + obtain ⟨xs, hQ, hqx, -⟩ := hEP.queue + step with endpoint.get_queue.spec as ⟨qq, hqq⟩ + have hfuel : layout.WALK_FUEL.val = 2 ^ 64 - 1 := by simp [layout.WALK_FUEL, U64.rMax] + have hI0 : AInvF st T E R Bf SR Wr Q e σ s y.epQueue (s, qq.head, layout.WALK_FUEL) := + ⟨σ, y, xs, hR, hSR, hky, hqx, by rw [hqq, epQ_eq, hQ.head], by rw [hfuel]; exact len_le_max xs hQ.nodup (hQ.ne0 hP.tcb), + fun t ht => hrep y hky _ (by rw [hqx]; exact List.mem_map_of_mem ht), + fun t ht hT => hon y hky t hT (by rw [hqx]; exact List.mem_map_of_mem ht), + fun z hz => by rw [hqx]; exact hz, fun h => by rw [← hqx]; exact h, LKeepA.refl σ s, + fun t ht => hQs y hky t (hQ.mem t ht) (by rw [hqx]; exact List.mem_map_of_mem ht)⟩ + refine WP.spec_bind (all_loopF hO hF hP he σ s y.epQueue _ hI0) ?_ + rintro s1 ⟨hnf, σf, hmf, hRf, hSf, Kf⟩ + refine WP.spec_mono (WP.spec_and (hO.reschedule s1 σf hRf hSf) (hF.reschedule s1 σf hRf hSf)) ?_ + rintro s2 ⟨⟨hnr2, σr, hrs, hf2, hv2, hS2⟩, hw2⟩ + refine ⟨?_, σr, ?_, hRf.frame hP hf2 hv2, hS2, Kf.keep.trans (rescheduleRequired_epkA hrs), + Kf.sz.trans (rescheduleRequired_ssz hrs), FpEA.trans hF.to0 Kf.fp Kf.sz (FpEA.of_wr hw2), + fun m n h => (Kf.inv m n h).reschedule hrs⟩ + · rw [hsp] + exact fun h => hnr2 (Det.detAt_bind_fail + ((Det.det_mapMx (fun a => Det.det_removeAndRestartEp a _) y.epQueue).at σ) hnf hmf h) + rw [hsp] + exact ⟨((), σf), hmf, hrs⟩ + +end LoopF + +end + +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/DelEpRecA.lean b/proofs/Fl4ProofsA64/P3/DelEpRecA.lean new file mode 100644 index 000000000..32ebcd980 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelEpRecA.lean @@ -0,0 +1,552 @@ +/- +P3 (AArch64, delete hooks W3): **the record across the final Endpoint finaliser**. + +* `JStage3TA.repOKA`: a receiver's reply object points back to it, from A64 P1's `ReplyInv0.recv` in the record's `BXI`. +* `JStage3TA.ntfn_epFr`: the notification relation across `cancel_all_ipc`'s write set. +* `JStage3TA.epFin_recA`: `finalise_cap_object` of a final Endpoint cap refines the spec's `cancel_all_ipc` and + `JStage3TA` holds again at the new queues (`of_schedWrN`); every `OS` slot word is kept. + +Premises: [P] `hCs` / `hmb` / `hmax` / `htu`; [S] `hon` (the waiters of the endpoint being finalised are local to the +current core, at the call state). `H p = some .ep` is the cap's typing, taken as a hypothesis here. New module. +-/ +import Fl4ProofsA64.P3.Stage3BA +import Fl4ProofsA64.P3.Stage3QKeepPresA +import Fl4ProofsA64.P3.DelEpLoopA +import Fl4ProofsA64.P3.DelBlkKeepA +import Fl4ProofsA64.P3.DelNtfnRecA +import Fl4ProofsA64.P3.DelRvVrA +import Fl4ProofsA64.P3.DelRec6A +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 Fl4ProofsA64.P3.Endp +open Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Notif + +local notation "wa" => core.num.U64.wrapping_add + +noncomputable section + +private theorem mapMx_presEA {α β : Type} (f : α → SMonad Unit β) (hf : ∀ x, SPresA (f x)) : + ∀ l : List α, SPresA (NonDet.mapMx f l) + | [] => by unfold NonDet.mapMx; exact PresR.pure _ + | x :: l => by + unfold NonDet.mapMx + exact PresR.bind (hf x) fun _ => mapMx_presEA f hf l + +theorem removeAndRestartEp_presA (t e : ObjRef) : SPresA (removeAndRestartEpQueuedThread (Z := Unit) t e) := by + unfold removeAndRestartEpQueuedThread tcbEpDequeue replyUnlinkTcb restartThreadIfNoFault + spres + +/-- **The spec's endpoint waiter loop is a scheduler step.** -/ +theorem cancelAllIpc_presA (p : ObjRef) : SPresA (cancelAllIpc (Z := Unit) p) := by + unfold cancelAllIpc + refine PresR.bind SPresInst.pres fun ep => ?_ + split + · exact PresR.pure _ + · refine PresR.bind (PresR.pure _) fun q => PresR.bind (mapMx_presEA _ (fun t => removeAndRestartEp_presA t p) _) + fun _ => SPresInst.pres + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} [J7PpA st J7] {s s' : S} {a : A} {σ σ' : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- **Every receiver's reply object points back to it** (A64 P1's `ReplyInv0.recv`, through `EpStateRel`). -/ +theorem JStage3BA.repOKA + (J : JStage3BA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {t : U64} (ht : t ∈ Z.T) : RepOKA σ (specPtr t) := by + intro x hx ep r d hs + have h := J.ep.at_tcb ht hx + have h3 : (L.mem s (Fl4ProofsA64.P1.stA t)).val = 3 := by rw [h.st, hs]; rfl + obtain ⟨hr1, hr2⟩ := h.recv ep (some r) d hs + have h0 : L.mem s (tReplyA t) ≠ 0#u64 := fun e => by rw [e, optRef_zero] at hr1; cases hr1 + have hiR := hr2 (by simp) + have er : r = specPtr (L.mem s (tReplyA t)) := by + rw [optRef_ne h0] at hr1; exact (Option.some.inj hr1).symm + have blk := J.blkA + have R := blk.2.2.1.2.1 + have htH : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + have e1 : L.mem s (Fl4ProofsA64.P1.stA t) = sched.TS_BLOCKED_ON_RECV := by + apply UScalar.eq_of_val_eq + show (L.mem s (Fl4ProofsA64.P1.stA t)).val = _ + rw [h3]; unfold sched.TS_BLOCKED_ON_RECV; rfl + have back := (R.recv t htH e1 h0).2 + obtain ⟨xr, hkr, hb⟩ := J.ep.reply _ hiR + refine ⟨xr, by rw [er]; exact hkr, ?_⟩ + have t0 : t ≠ 0#u64 := J.sr.place.nz t _ ((J.sr.thT t).2 ht) + have back' : L.mem s (Endp.rTcbA (L.mem s (tReplyA t))) = t := back + rw [hb, back', optRef_ne t0] + +/-- `JStage3BA.repOKA` at `JStage3TA`. -/ +theorem JStage3TA.repOKA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {t : U64} (ht : t ∈ Z.T) : RepOKA σ (specPtr t) := + J.toBA.repOKA ht + +/-- An IPC object's word is not written by the scheduler. -/ +theorem JStage3BA.ipc_offMA + (J : JStage3BA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {b : U64} (hw : IpcObjWA N E Rp b) : ¬ MWrA (st := st) Z m σ s b := by + have hks := J.ipc_ksA hw + obtain ⟨e, len, he, e1, e2⟩ := J.ipc_thA hw + rintro (h | h | ⟨t, ht, e'⟩) + · obtain ⟨c, hc, x, k, hx, ha⟩ := h + have := (J.sr.scp.out c hc x k hx _ ha).1 e _ he + simp only [ObjKindA.size] at this + omega + · unfold SKsA at h + unfold ksSizeA at hks; omega + · have htH : H t = some .tcb := by + have : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + exact this + have hfit := (J.sr.core.tcbs t ht).fits + have k : ∀ K : U64, K.val < 4096 → b = wa t K → False := fun K hK eq => by + have hv : (wa t K).val = t.val + K.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact J.ipc_off_hobjA (v := t) (kv := .tcb) htH (by simp) + ⟨by rw [eq, hv]; omega, by rw [eq, hv]; simp only [HKind.bits]; omega⟩ hw + rcases e' with e' | e' | e' | e' + · rw [scA_eq] at e'; exact k _ (by simp) e' + · exact k 3480#u64 (by simp) e' + · exact k 3488#u64 (by simp) e' + · rw [inqA_eq] at e'; exact k _ (by simp) e' + +/-- `JStage3BA.ipc_offMA` at `JStage3TA`. -/ +theorem JStage3TA.ipc_offMA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {b : U64} (hw : IpcObjWA N E Rp b) : ¬ MWrA (st := st) Z m σ s b := + J.toBA.ipc_offMA hw + +/-- A word of the endpoint loop's write set is an IPC object word or a state / `Ep` link word of a TCB. -/ +theorem JStage3BA.epSig_casesA + (J : JStage3BA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {p : U64} (hp : p ∈ E) {Q : Set U64} (hQ : Q ⊆ Z.T) {b : U64} (h : EpSigWA Rp Q p b) : + IpcObjWA N E Rp b ∨ ∃ u ∈ Z.T, b = wa u 3328#u64 ∨ b = wa u 3496#u64 ∨ b = wa u 3504#u64 := by + rcases h with h | ⟨r, hr, rfl⟩ | ⟨t, ht, h⟩ + · refine Or.inl (Or.inr (Or.inl ⟨p, hp, ?_⟩)) + unfold EW at h; omega + · refine Or.inl (Or.inr (Or.inr ⟨r, hr, ?_⟩)) + rw [Endp.rTcbA_val]; omega + · have htT := hQ ht + refine Or.inr ⟨t, htT, ?_⟩ + rcases h with rfl | h + · rw [Fl4ProofsA64.P1.stA_eq]; exact Or.inl rfl + · have hta := J.iplaced.e.tcb t htT + have hfit := (J.sr.core.tcbs t htT).fits + have hv : ∀ K : U64, K.val < 4096 → (wa t K).val = t.val + K.val := fun K hK => by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + unfold TL at h + rcases h with h | h + · refine Or.inr (Or.inl (UScalar.eq_of_val_eq ?_)) + rw [h, hv _ (by simp)]; simp + · refine Or.inr (Or.inr (UScalar.eq_of_val_eq ?_)) + rw [h, hv _ (by simp)]; simp + +/-- `JStage3BA.epSig_casesA` at `JStage3TA`. -/ +theorem JStage3TA.epSig_casesA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {p : U64} (hp : p ∈ E) {Q : Set U64} (hQ : Q ⊆ Z.T) {b : U64} (h : EpSigWA Rp Q p b) : + IpcObjWA N E Rp b ∨ ∃ u ∈ Z.T, b = wa u 3328#u64 ∨ b = wa u 3496#u64 ∨ b = wa u 3504#u64 := + J.toBA.epSig_casesA hp hQ h + +/-- The endpoint loop's write set is a scheduler / IPC-object write set. -/ +theorem JStage3BA.epW_stepNA + (J : JStage3BA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {p : U64} (hp : p ∈ E) {Q : Set U64} (hQ : Q ⊆ Z.T) {b : U64} + (h : EpSigWA Rp Q p b ∨ MWrA (st := st) Z m σ s b) : SchedStepNWA (st := st) N E Rp Z m σ s b := by + rcases h with h | h + swap + · exact Or.inl (Or.inl h) + rcases J.epSig_casesA hp hQ h with h | ⟨u, hu, e⟩ + · exact Or.inr h + have hfit := (J.sr.core.tcbs u hu).fits + have hv : ∀ K : U64, K.val < 4096 → (wa u K).val = u.val + K.val := fun K hK => by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have key : ∀ K : U64, 224 ≤ K.val → K.val < 4096 → K.val ≠ 3664 → K.val ≠ 3672 → TcbStepWA Z.T (wa u K) := by + intro K k1 k2 k3 k4 + have e1 := hv K k2 + refine ⟨u, hu, by omega, by omega, fun h => k3 ?_, fun h => k4 ?_⟩ + · have := congrArg (fun w : U64 => w.val) h + simp only [e1, hv 3664#u64 (by simp)] at this + simpa using this + · unfold runVcpuA at h + have := congrArg (fun w : U64 => w.val) h + simp only [e1, hv 3672#u64 (by simp)] at this + simpa using this + refine Or.inl (Or.inr ?_) + rcases e with rfl | rfl | rfl + · exact key _ (by simp) (by simp) (by simp) (by simp) + · exact key _ (by simp) (by simp) (by simp) (by simp) + · exact key _ (by simp) (by simp) (by simp) (by simp) + +/-- `JStage3BA.epW_stepNA` at `JStage3TA`. -/ +theorem JStage3TA.epW_stepNA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {p : U64} (hp : p ∈ E) {Q : Set U64} (hQ : Q ⊆ Z.T) {b : U64} + (h : EpSigWA Rp Q p b ∨ MWrA (st := st) Z m σ s b) : SchedStepNWA (st := st) N E Rp Z m σ s b := + J.toBA.epW_stepNA hp hQ h + +/-- A TCB word at an offset the endpoint finaliser does not write. -/ +theorem JStage3BA.tcbWord_offEpA + (J : JStage3BA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {p : U64} (hp : p ∈ E) {Q : Set U64} (hQ : Q ⊆ Z.T) {t K : U64} (ht : t ∈ Z.T) (hK : K.val < 4096) + (h7 : K.val ≠ 3328 ∧ K.val ≠ 3496 ∧ K.val ≠ 3504 ∧ K.val ≠ 3464 ∧ K.val ≠ 3480 ∧ K.val ≠ 3488 ∧ + K.val ≠ 3592) : + ¬ (EpSigWA Rp Q p (wa t K) ∨ MWrA (st := st) Z m σ s (wa t K)) := by + have hfit := (J.sr.core.tcbs t ht).fits + have hv : (wa t K).val = t.val + K.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have htH : H t = some .tcb := by + have : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + exact this + rintro (h | h) + swap + · exact J.tcbWord_offMA ht hK ⟨h7.2.2.2.1, h7.2.2.2.2.1, h7.2.2.2.2.2.1, h7.2.2.2.2.2.2⟩ h + rcases J.epSig_casesA hp hQ h with h | ⟨u, hu, e⟩ + · exact J.ipc_off_hobjA (v := t) (kv := .tcb) htH (by simp) ⟨by omega, by simp only [HKind.bits]; omega⟩ h + have hfu := (J.sr.core.tcbs u hu).fits + have other : ∀ K' : U64, K'.val < 4096 → K.val ≠ K'.val → wa t K ≠ wa u K' := by + intro K' k1 k2 h + have hv' : (wa u K').val = u.val + K'.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have h' := congrArg (fun w : U64 => w.val) h + simp only [hv, hv'] at h' + by_cases e : u = t + · subst e; omega + · have := J.sr.place.apart u t .tcb .tcb ((J.sr.thT u).2 hu) ((J.sr.thT t).2 ht) e + have hsz : ObjKindA.size .tcb = 4096 := rfl + omega + rcases e with e | e | e + · exact other _ (by simp) (by simpa using h7.1) e + · exact other _ (by simp) (by simpa using h7.2.1) e + · exact other _ (by simp) (by simpa using h7.2.2.1) e + +/-- `JStage3BA.tcbWord_offEpA` at `JStage3TA`. -/ +theorem JStage3TA.tcbWord_offEpA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {p : U64} (hp : p ∈ E) {Q : Set U64} (hQ : Q ⊆ Z.T) {t K : U64} (ht : t ∈ Z.T) (hK : K.val < 4096) + (h7 : K.val ≠ 3328 ∧ K.val ≠ 3496 ∧ K.val ≠ 3504 ∧ K.val ≠ 3464 ∧ K.val ≠ 3480 ∧ K.val ≠ 3488 ∧ + K.val ≠ 3592) : + ¬ (EpSigWA Rp Q p (wa t K) ∨ MWrA (st := st) Z m σ s (wa t K)) := + J.toBA.tcbWord_offEpA hp hQ ht hK h7 + +/-- A reply object's link words are not written by the endpoint finaliser. -/ +theorem JStage3BA.reply_offEpA + (J : JStage3BA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {p : U64} (hp : p ∈ E) {Q : Set U64} (hQ : Q ⊆ Z.T) {r b : U64} (hr : r ∈ Rp) + (lo : r.val + 8 ≤ b.val) (hi : b.val < r.val + 32) : + ¬ (EpSigWA Rp Q p b ∨ MWrA (st := st) Z m σ s b) := by + have hrH : H r = some .reply := J.utIpc.reply r hr + have hpH : H p = some .ep := J.utIpc.ep p hp + have ne : p ≠ r := fun e => by rw [e, hrH] at hpH; cases hpH + have dj := J.ent.heap.disj p _ r _ hpH hrH ne + simp only [HKind.bits] at dj + have hw : IpcObjWA N E Rp b := Or.inr (Or.inr ⟨r, hr, by omega, hi⟩) + rintro (h | h) + swap + · exact J.ipc_offMA hw h + rcases h with h | ⟨r', hr', e⟩ | ⟨u, hu, h⟩ + · unfold EW at h + have := J.iplaced.e.er p hp r hr + omega + · have : b.val = r'.val := by rw [e, Endp.rTcbA_val] + by_cases er : r' = r + · subst er; omega + · have hr'H : H r' = some .reply := J.utIpc.reply r' hr' + have := J.ent.heap.disj r' _ r _ hr'H hrH er + simp only [HKind.bits] at this + omega + · have huT := hQ hu + have huH : H u = some .tcb := by + have : u ∈ H.tcbs := by rw [J.tcbs]; exact huT + exact this + have hta := J.iplaced.e.tcb u huT + refine J.ipc_off_hobjA (v := u) (kv := .tcb) huH (by simp) ?_ hw + unfold InObj + simp only [HKind.bits] + rcases h with rfl | h + · rw [Endp.val_st hta]; omega + · unfold TL at h; omega + +/-- `JStage3BA.reply_offEpA` at `JStage3TA`. -/ +theorem JStage3TA.reply_offEpA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {p : U64} (hp : p ∈ E) {Q : Set U64} (hQ : Q ⊆ Z.T) {r b : U64} (hr : r ∈ Rp) + (lo : r.val + 8 ≤ b.val) (hi : b.val < r.val + 32) : + ¬ (EpSigWA Rp Q p b ∨ MWrA (st := st) Z m σ s b) := + J.toBA.reply_offEpA hp hQ hr lo hi + +/-- **The notification relation across the endpoint loop**: the notification words are not written; a waiter's block +word is the endpoint, so no notification waiter is in `Q`; the new state words are read off `EpStateRel`. -/ +theorem JStage3TA.ntfn_epFr + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {p : U64} (hp : p ∈ E) {Q : Set U64} (hQ : ∀ q ∈ Q, q ∈ Z.T ∧ L.mem s (Notif.tBlockA q) = p) + (w : WritesIn st s s' (fun b => EpSigWA Rp Q p b ∨ MWrA (st := st) Z m σ s b)) + (K : EpSpecKeepA σ σ') (E' : EpStateRel Z.T E Rp Bf (L.mem s') σ') : + NtfnStateRel N Z.T (L.mem s') σ' := by + have hP := J.iplaced + have hQT : Q ⊆ Z.T := fun q h => (hQ q h).1 + have tk : ∀ t ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → + (K.val ≠ 3328 ∧ K.val ≠ 3496 ∧ K.val ≠ 3504 ∧ K.val ≠ 3464 ∧ K.val ≠ 3480 ∧ K.val ≠ 3488 ∧ K.val ≠ 3592) → + L.mem s' (wa t K) = L.mem s (wa t K) := fun t ht K h1 h2 h3 => + w.mem_eq (field_aligned (J.sr.core.tcbs t ht) h1 h2) (J.tcbWord_offEpA hp hQT ht h1 h3) + have nw : ∀ q ∈ N, ∀ K : U64, K.val < 40 → K.val % 8 = 0 → L.mem s' (wa q K) = L.mem s (wa q K) := by + intro q hq K h1 h2 + have hna := hP.n.ntfn q hq + have hv : (wa q K).val = q.val + K.val := Notif.nfield_val hna (by omega) + refine w.mem_eq (Notif.nfield_aligned hna (by omega) h2) ?_ + have hw : IpcObjWA N E Rp (wa q K) := Or.inl ⟨q, hq, by omega, by omega⟩ + rintro (h | h) + swap + · exact J.ipc_offMA hw h + rcases h with h | ⟨r, hr, e⟩ | ⟨u, hu, h⟩ + · unfold EW at h + have := hP.ne q hq p hp + omega + · have : (wa q K).val = r.val := by rw [e, Endp.rTcbA_val] + have := hP.nr q hq r hr + omega + · have huT := hQT hu + have hta := hP.e.tcb u huT + have := hP.n.disj q hq u huT + rcases h with h | h + · have : (wa q K).val = u.val + 3328 := by rw [h, Endp.val_st hta] + omega + · unfold TL at h; omega + refine ⟨fun q hq => ?_, fun t ht => ?_⟩ + · obtain ⟨x, hx, hr⟩ := J.ntfn.ntfn q hq + have n40 : ∀ K : U64, K.val < 40 → K.val % 8 = 0 → L.mem s' (wa q K) = L.mem s (wa q K) := nw q hq + refine ⟨x, K.ntfn hx, hr.frame ?_ fun y hy hm => ?_⟩ + · refine ⟨?_, ?_, ?_, ?_, ?_⟩ + · rw [Notif.epHeadA_eq]; exact n40 _ (by simp) (by simp) + · rw [Notif.epTSA_eq]; exact n40 _ (by simp) (by simp) + · rw [Notif.nBadgeA_eq]; exact n40 _ (by simp) (by simp) + · rw [Notif.nBoundA_eq]; exact n40 _ (by simp) (by simp) + · rw [Notif.nScA_eq]; exact n40 _ (by simp) (by simp) + · obtain ⟨hyT, -, hyb⟩ := hr.obj.blocked hm + have yQ : y ∉ Q := fun h => by + have e : q = p := hyb.symm.trans (hQ y h).2 + have h1 := hP.ne q hq p hp + rw [e] at h1; omega + have hta := hP.e.tcb y hyT + have offY : ∀ b : U64, Aligned b → y.val ≤ b.val → b.val < y.val + 4096 → + (b = Fl4ProofsA64.P1.stA y ∨ b = nextA .Ep y ∨ b = prevA .Ep y) → L.mem s' b = L.mem s b := by + intro b hb lo hi eb + have htH : H y = some .tcb := by + have : y ∈ H.tcbs := by rw [J.tcbs]; exact hyT + exact this + have offI : ¬ IpcObjWA N E Rp b := + J.ipc_off_hobjA (v := y) (kv := .tcb) htH (by simp) ⟨lo, by simp only [HKind.bits]; omega⟩ + have offU : ∀ u ∈ Q, ¬ (b = Fl4ProofsA64.P1.stA u ∨ TL u b) := by + intro u hu h + have huT := hQT hu + have ne : u ≠ y := fun e => yQ (e ▸ hu) + have := Endp.tcb_disj hP.e hyT huT ne + have hua := hP.e.tcb u huT + rcases h with h | h + · have : b.val = u.val + 3328 := by rw [h, Endp.val_st hua] + omega + · unfold TL at h; omega + refine w.mem_eq hb ?_ + rintro (h | h) + · rcases h with h | ⟨r, hr, e⟩ | ⟨u, hu, h⟩ + · refine offI (Or.inr (Or.inl ⟨p, hp, ?_⟩)); unfold EW at h; omega + · refine offI (Or.inr (Or.inr ⟨r, hr, ?_⟩)); rw [e, Endp.rTcbA_val]; omega + · exact offU u hu h + · have hfy := (J.sr.core.tcbs y hyT).fits + have ex : ∃ K : U64, K.val < 4096 ∧ b = wa y K ∧ + (K.val ≠ 3464 ∧ K.val ≠ 3480 ∧ K.val ≠ 3488 ∧ K.val ≠ 3592) := by + rcases eb with e | e | e + · exact ⟨3328#u64, by simp, by rw [e, Fl4ProofsA64.P1.stA_eq], by simp⟩ + · exact ⟨3496#u64, by simp, e, by simp⟩ + · exact ⟨3504#u64, by simp, e, by simp⟩ + obtain ⟨K, k1, rfl, k2⟩ := ex + exact J.tcbWord_offMA hyT k1 k2 h + have a1 := Endp.a_st hta + have n1 := Endp.a_next hta + have p1 := Endp.a_prev hta + refine ⟨offY _ a1.1 ?_ ?_ (Or.inl rfl), ?_, ⟨offY _ p1.1 ?_ ?_ (Or.inr (Or.inr rfl)), + offY _ n1.1 ?_ ?_ (Or.inr (Or.inl rfl))⟩⟩ + · rw [Endp.val_st hta]; omega + · rw [Endp.val_st hta]; omega + · rw [Notif.tBlockA_eq]; exact tk y hyT _ (by simp) (by simp) (by simp) + · rw [Endp.val_prev hta]; omega + · rw [Endp.val_prev hta]; omega + · rw [Endp.val_next hta]; omega + · rw [Endp.val_next hta]; omega + · obtain ⟨x, hx, hr⟩ := J.ntfn.tcb t ht + obtain ⟨y, hy, k⟩ := K.tcb hx + refine ⟨y, hy, ?_, fun q hq => ?_, ?_, ?_⟩ + · exact (E'.at_tcb ht hy).st + · have e : y.tcbState = x.tcbState := by + rcases k.st with e | ⟨-, e⟩ + · exact e + · rcases e with e | e <;> (rw [e] at hq; cases hq) + rw [Notif.tBlockA_eq, tk t ht _ (by simp) (by simp) (by simp), ← Notif.tBlockA_eq] + exact hr.blk q (e ▸ hq) + · rw [Notif.badgeRegA_eq, tk t ht _ (by simp) (by simp) (by simp), ← Notif.badgeRegA_eq] + unfold Notif.badgeOf + rw [k.arch]; exact hr.badge + · rw [Notif.prioA_eq', tk t ht _ (by simp) (by simp) (by simp), ← Notif.prioA_eq', k.prio] + exact hr.prio + +end + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +set_option maxHeartbeats 1000000 in +/-- **The record across the final Endpoint finaliser** (module docstring). -/ +theorem JStage3TA.epFin_recA {ar : machine.Arch A} {ipa : U64} + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7LA st (J7R2A st ar ipa)) s a σ m) + {p b0 : U64} {b1 b2 b3 b4 : Bool} (hH : H p = some .ep) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (hon : ∀ z, σ.kheap (specPtr p) = some (.endpoint z) → ∀ t ∈ Z.T, + specPtr t ∈ z.epQueue → L.mem s (affA t) = L.cpu s) : + objects.finalise_cap_object st ar s a (.Endpoint p b0 b1 b2 b3 b4) true ⦃ r => + r.1 = (.Null, .Null) ∧ r.2.2 = a ∧ ∃ σ' Q R, + ((), σ') ∈ (cancelAllIpc (Z := Unit) (specPtr p) σ).1 ∧ + JStage3TA st AR { Z with Q := Q, R := R } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7LA st (J7R2A st ar ipa)) r.2.1 a σ' m ∧ + OSKeep st OS s r.2.1 ∧ L.ksb r.2.1 = L.ksb s ∧ L.cpu r.2.1 = L.cpu s ∧ SchedFrameA σ σ' ∧ + (∀ t ∈ Z.T, L.mem r.2.1 (wa t 3664#u64) = L.mem s (wa t 3664#u64)) ∧ + Std6KeepA st Z H OS Bf s σ r.2.1 σ' ⦄ := by + have hp : p ∈ E := J.ipcHeapCov.ep p hH + have hc := J.curCore_lt hCs + have hP := J.iplaced + have blk : objects.finalise_cap_object st ar s a (.Endpoint p b0 b1 b2 b3 b4) true ⦃ r => + BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac r.2.1 ⦄ := + WP.spec_mono (Fl4ProofsA64.P1.fin_endpoint.blk s a p b0 b1 b2 b3 b4 true J.blkA hH) fun _ h => h.2.2.1 + have rvi := Fl4ProofsA64.P1.fin_endpoint.rvi (ar := ar) s a p b0 b1 b2 b3 b4 true J.blkA hH (J.tcbs ▸ J.runVcpu) + refine WP.spec_mono (WP.spec_and (WP.spec_and blk rvi) (?main : objects.finalise_cap_object st ar s a (.Endpoint p b0 b1 b2 b3 b4) true + ⦃ r => r.1 = (.Null, .Null) ∧ r.2.2 = a ∧ + (BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac r.2.1 → + RunVcpuInv st H.tcbs r.2.1 → ∃ σ' Q R, + ((), σ') ∈ (cancelAllIpc (Z := Unit) (specPtr p) σ).1 ∧ + JStage3TA st AR { Z with Q := Q, R := R } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7LA st (J7R2A st ar ipa)) r.2.1 a σ' m ∧ + OSKeep st OS s r.2.1 ∧ L.ksb r.2.1 = L.ksb s ∧ L.cpu r.2.1 = L.cpu s ∧ SchedFrameA σ σ' ∧ + (∀ t ∈ Z.T, L.mem r.2.1 (wa t 3664#u64) = L.mem s (wa t 3664#u64)) ∧ + Std6KeepA st Z H OS Bf s σ r.2.1 σ') ⦄)) ?_ + case main => + simp only [objects.finalise_cap_object, ↓reduceIte] + let Q : Set U64 := {q | q ∈ Z.T ∧ L.mem s (Notif.tBlockA q) = p} + have hQ : Q ⊆ Z.T := fun _ h => h.1 + have hrep : ∀ y, σ.kheap (specPtr p) = some (.endpoint y) → ∀ t ∈ y.epQueue, RepOKA σ t := by + intro y hy t ht + obtain ⟨xs, -, hxs, -⟩ := (J.ep.at_ep hp hy).queue + rw [hxs, List.mem_map] at ht + obtain ⟨u, hu, rfl⟩ := ht + have hm : specPtr u ∈ y.epQueue := by rw [hxs]; exact List.mem_map_of_mem hu + exact J.repOKA ((J.ep.at_ep hp hy).blocked hm).1 + have hQs : ∀ z, σ.kheap (specPtr p) = some (.endpoint z) → ∀ t ∈ Z.T, specPtr t ∈ z.epQueue → t ∈ Q := + fun z hz t ht hm => ⟨ht, ((J.ep.at_ep hp hz).blocked hm).2⟩ + refine WP.spec_bind (cancel_all_relF (J.schedOpsEA (ar := ar) hc hmb hmax htu) J.schedFootEA hP.e s σ J.ep + J.ipcSchedA hp hrep hon hQs) ?_ + rintro s' ⟨-, σ', h', E', SR', K⟩ + refine (WP.spec_ok _).2 ⟨rfl, rfl, fun blk' rv' => ?_⟩ + have F : SchedFrameA σ σ' := cancelAllIpc_presA (specPtr p) σ ((), σ') h' + have TK : TcbFinKeepA σ σ' := fun q x h => by + obtain ⟨y, hy, k⟩ := K.keep.tcb h + exact ⟨y, hy, k.fault, k.frame, k.buf⟩ + have wE : WritesIn st s s' (fun b => EpSigWA Rp Q p b ∨ MWrA (st := st) Z m σ s b) := K.fp + have w : WritesIn st s s' (SchedStepNWA (st := st) N E Rp Z m σ s) := wE.mono fun _ h => J.epW_stepNA hp hQ h + have tw : ∀ t ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → + (K.val ≠ 3328 ∧ K.val ≠ 3496 ∧ K.val ≠ 3504 ∧ K.val ≠ 3464 ∧ K.val ≠ 3480 ∧ K.val ≠ 3488 ∧ + K.val ≠ 3592) → L.mem s' (wa t K) = L.mem s (wa t K) := fun t ht K h1 h2 h3 => + wE.mem_eq (field_aligned (J.sr.core.tcbs t ht) h1 h2) (J.tcbWord_offEpA hp hQ ht h1 h3) + obtain ⟨Q', R', H3⟩ := SR' + have sq : SchedQueuesInv st H.tcbs Z.Cs H.omega H s' Q' R' := by + obtain ⟨Q0, R0, h0⟩ := blk'.1 + exact SchedQueuesInv.reQA h0 (by rw [J.tcbs]; exact H3.core) + have kb : L.ksb s' = L.ksb s := w.1 + have poolOff : PoolOff st H Z.Cs OS s' := fun i => by + have h0 := J.poolOff i + rw [vspace.asid_table.eq] at h0 ⊢ + have v := AtOff.v i + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.ent.ksw + have ev : (wa (L.ksb s) (AtOff i)).val = (L.ksb s).val + (AtOff i).val := by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have e : L.mem s' (wa (L.ksb s') (AtOff i)) = L.mem s (wa (L.ksb s) (AtOff i)) := by + rw [kb] + refine w.mem_eq ?_ (J.asid_offStepNA (by omega) (by omega)) + have h32 := J.j.1.ksbAl + show (wa (L.ksb s) (AtOff i)).val % 8 = 0 + omega + rw [e] + refine (WP.spec_ok _).2 fun hne asid => ?_ + have q := (WP.spec_ok _).1 h0 hne asid + exact ⟨q.al, q.om, q.sg, q.os, RegionOffNodes.ksb kb q.ks⟩ + have ntfn : NtfnStateRel N Z.T (L.mem s') σ' := J.ntfn_epFr hp (fun q h => h) wE K.keep E' + have reply : ReplyStackRelA (L.mem s') Rp Z.C σ' (scNameA m) stack := by + refine J.reply.epFr K.keep fun r hr => ?_ + obtain ⟨r32, -, rfit⟩ := hP.e.rep r hr + have one : ∀ o : U64, 8 ≤ o.val → o.val < 32 → o.val % 8 = 0 → L.mem s' (wa r o) = L.mem s (wa r o) := by + intro o o0 o1 o2 + have v : (wa r o).val = r.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + refine wE.mem_eq ?_ (J.reply_offEpA hp hQ hr (by omega) (by omega)) + show (wa r o).val % 8 = 0 + omega + have n16 : (layout.reply.NEXT).val = 16 := by unfold layout.reply.NEXT; rfl + have p8 : (layout.reply.PREV).val = 8 := by unfold layout.reply.PREV; rfl + exact ⟨one _ (by omega) (by omega) (by omega), one _ (by omega) (by omega) (by omega)⟩ + have mcp : McpRelA Z.T m (L.mem s') σ' := + J.mcp.epFr K.keep fun t ht => ⟨tw t ht 3440#u64 (by simp) (by simp) (by simp), + tw t ht 3456#u64 (by simp) (by simp) (by simp)⟩ + have yr : YieldRelA Z.T Z.C m (L.mem s') σ' := + J.yieldRel.epFr K.keep fun t ht => tw t ht 3472#u64 (by simp) (by simp) (by simp) + have buf : BufCover st Z.T Bf s' σ' := + J.bufCover_wA wE (fun t ht K hK => J.tcbWord_offEpA hp hQ ht (by omega) (by omega)) + (fun q x h => by + obtain ⟨y, hy, -, b, c⟩ := TK q x h + exact ⟨y, hy, b, c⟩) + have bufAl : UtBufAlA Z.T σ' := fun t ht y hy => by + obtain ⟨x, hx, -⟩ := J.ntfn.tcb t ht + obtain ⟨y', hy', -, -, e⟩ := TK _ _ hx + rw [hy] at hy'; cases hy' + rw [e]; exact J.bufAl t ht x hx + have idle : IdleNoFaultA m σ' := fun c hc' => by + obtain ⟨x, hx, hf⟩ := J.idleNoFault c hc' + obtain ⟨y, hy, e, -⟩ := TK _ _ hx + exact ⟨y, hy, e.trans hf⟩ + have scCov : ScCovA Z.C m σ' := fun q x k h => by + obtain ⟨y, hy⟩ := F.2.1 q x k h + exact J.scCov q y k hy + have inv := K.inv m n ⟨J.sinv, J.sidle, J.sact⟩ + have j := J.j57_stepNWA w σ' + have hvr : ∀ sc ∈ Z.C, ∀ q ∈ stack sc, RBlk st s' q := by + refine J.vr_specA blk' (fun _ _ h => h) (fun sc _ q _ c _ _ x o hx hs => ?_) E' + (fun c hc => tw c hc 3360#u64 (by simp) (by simp) (by simp)) + obtain ⟨y, hy, k⟩ := K.keep.tcb hx + refine ⟨y, hy, ?_⟩ + rcases k.st with e | ⟨⟨-, nr⟩, -⟩ + · exact e + · exact absurd hs (nr o) + have bq : BlkQueuedA Z.T E N (L.mem s') σ' := by + obtain ⟨y, hy, -⟩ := J.ep.ep p hp + obtain ⟨M, d⟩ := cancelAllIpc_blkA hy h' + refine J.blkQ.finA M (fun t ht => ?_) (fun t ht => ?_) (fun y' hy' r hr => ?_) (fun y' q hn => ?_) + · obtain ⟨x, hx, -⟩ := J.ntfn.tcb t ht + exact ⟨x, hx⟩ + · rw [Notif.tBlockA_eq]; exact tw t ht 3336#u64 (by simp) (by simp) (by simp) + · rw [hy] at hy'; cases hy'; exact Or.inl (d r hr) + · rw [hy] at hn; cases hn + have J' := JStage3TA.of_schedWrN (blkQ := bq) J (Q := Q') (Rl := R') (curOf := Z.curOf) (curScOf := Z.curScOf) (s' := s') + (a' := a) (σ' := σ') w F.1 F.2.1 (Sched.qk_of h') F.2.2 J.j.1.ar (fun _ _ => rfl) bufAl (J.ent_stepNWA w blk') sq poolOff H3 + scCov E' ntfn reply buf idle mcp yr inv.sinv inv.sidle (J.tcbs ▸ rv') hvr + (cancelAllIpc_noSwA (specPtr p) J.noSwitch h') inv.sact j.1 j.2 + exact ⟨σ', Q', R', h', J', J.sigW_osKeepA w, kb, w.2.1, + F, fun t ht => tw t ht 3664#u64 (by simp) (by simp) (by simp), + fun {_} sub h => Std6GA.schedStepA J h sub w (J.sigW_osKeepA w) F⟩ + · rintro r ⟨⟨b, rv⟩, h1, h2, h3⟩ + exact ⟨h1, h2, h3 b rv⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelFrameLeafA.lean b/proofs/Fl4ProofsA64/P3/DelFrameLeafA.lean new file mode 100644 index 000000000..a3c902a36 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelFrameLeafA.lean @@ -0,0 +1,191 @@ +/- +P3 (AArch64, delete hooks W1): **the mapped frame's unmap with what the deletion loop reads**, the Frame sibling of +`P3/DelUnmapLeafA`. + +* **`VcA`** (`VsSpecA ∧ ClrA`) along every outcome of the spec's finalisation of a mapped frame cap + (**`fl4FinaliseCap_frame_vcA`**), and its independence of `final` (**`fl4FinaliseCap_frame_indepA`**). +* **`unmap_frame_leafA`**: FL4's `ept::unmap` / `vspace::unmap_page`, every root kind, with `UnmapLeafPostA`. + +Named premises: `ArchKeepsA` [P], `S2IpaE` [P]. Decode-site hypotheses: `TabOffA`, `ExtTabOffA`, `OffAsE`. New module. +-/ +import Fl4ProofsA64.P3.DelUnmapLeafA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4Spec Fl4SpecA64 + +noncomputable section + +/-- A VSpace spec step that only invalidates entries. -/ +def VcA (σ σ' : State Unit) : Prop := VsSpecA σ σ' ∧ ClrA σ σ' + +theorem VcA.refl (σ : State Unit) : VcA σ σ := ⟨VsSpecA.refl σ, ClrA.refl σ⟩ + +theorem VcA.trans {σ1 σ2 σ3 : State Unit} (h1 : VcA σ1 σ2) (h2 : VcA σ2 σ3) : VcA σ1 σ3 := + ⟨h1.1.trans h2.1, h1.2.trans h2.2⟩ + +local notation "VT" => fun {a b c} (h1 : VcA a b) (h2 : VcA b c) => VcA.trans h1 h2 + +theorem ro_vcA {α : Type} {m : NonDet (State Unit) α} (h : ∀ σ, ∃ v, m σ = ({(v, σ)}, False)) : OutRA VcA m := + OutRA.one VcA.refl h + +theorem gets_vcA {α : Type} (g : State Unit → α) : OutRA VcA (NonDet.gets g) := ro_vcA fun _ => ⟨_, rfl⟩ + +theorem getsThe_vcA {α : Type} (g : State Unit → Option α) : OutRA VcA (NonDet.getsThe g) := fun σ r hr => + OutRA.assertOpt VcA.refl (g σ) σ r hr + +theorem getsMap_vcA {α γ : Type} (g : State Unit → γ → Option α) (p : γ) : OutRA VcA (NonDet.getsMap g p) := + OutRA.bind VT (gets_vcA g) fun x => OutRA.assertOpt VcA.refl (x p) + +theorem throwE_vcA {α ε : Type} (e : ε) : OutRA VcA (NonDetE.throwError e : NonDetE (State Unit) ε α).run := + ro_vcA fun _ => ⟨_, rfl⟩ + +theorem unlessE_vcA {ε : Type} {P : Prop} [Decidable P] {m : NonDetE (State Unit) ε Unit} (hm : OutRA VcA m.run) : + OutRA VcA (NonDetE.unlessE P m).run := by + unfold NonDetE.unlessE + split + · exact ro_vcA fun _ => ⟨_, rfl⟩ + · exact hm + +theorem storePte_vcA (ptT : PtType) (p : ObjRef) : OutRA VcA (storePte (Z := Unit) ptT p .invalidPTE) := + fun σ r hr => ⟨storePte_vsA _ _ _ σ r hr, storePte_clrA _ _ σ r hr⟩ + +theorem liftOp_vcA (g : MachineRestMonad Unit) : OutRA VcA (doMachineOp (Z := Unit) (machineOpLift g)) := + fun σ r hr => ⟨liftOp_vsA g σ r hr, liftOp_clrA g σ r hr⟩ + +theorem invTlbVa_vcA (asid : Asid) (v : VSpaceRef) : OutRA VcA (invalidateTlbByAsidVa (Z := Unit) asid v) := by + unfold invalidateTlbByAsidVa loadVmid + refine OutRA.bind VT (OutRA.bind VT (gets_vcA _) fun _ => ro_vcA fun _ => ⟨_, rfl⟩) fun o => ?_ + cases o with + | none => exact ro_vcA fun _ => ⟨_, rfl⟩ + | some _ => exact liftOp_vcA _ + +theorem findVs_vcA (asid : Asid) : OutRA VcA (findVspaceForAsid (Z := Unit) asid).run := + fun σ r hr => ⟨findVs_vsA asid σ r hr, findVs_clrA asid σ r hr⟩ + +/-- **l4v's `unmap_page` only invalidates an entry.** -/ +theorem unmapPage_vcA (sz : VMPageSize) (asid : Asid) (v : VSpaceRef) (p : ObjRef) : + OutRA VcA (unmapPage (Z := Unit) sz asid v p) := by + unfold unmapPage + refine OutRA.catch VT ?_ fun _ => ro_vcA fun _ => ⟨_, rfl⟩ + refine OutRA.ebind VT (findVs_vcA _) fun _ => ?_ + refine OutRA.ebind VT (OutRA.liftE (getsThe_vcA _)) fun x => ?_ + obtain ⟨lev, slot⟩ := x + refine OutRA.ebind VT (unlessE_vcA (throwE_vcA _)) fun _ => ?_ + refine OutRA.ebind VT (OutRA.liftE (getsMap_vcA _ _)) fun _ => ?_ + refine OutRA.ebind VT (unlessE_vcA (throwE_vcA _)) fun _ => ?_ + refine OutRA.ebind VT (OutRA.liftE (storePte_vcA _ _)) fun _ => ?_ + refine OutRA.ebind VT (OutRA.liftE (liftOp_vcA _)) fun _ => ?_ + exact OutRA.liftE (invTlbVa_vcA _ _) + +theorem extUnmapPage_vcA (k : ExtRootKind) (sz : VMPageSize) (asid : Asid) (va : VSpaceRef) (p : ObjRef) : + OutRA VcA (extUnmapPage (Z := Unit) k sz asid va p) := by + intro σ r hr + rw [extUnmapPage_runX] at hr + obtain rfl : r = ((), extUnmapPageStX σ k sz asid va p) := hr + show VcA σ (extUnmapPageStX σ k sz asid va p) + unfold extUnmapPageStX + split + · exact extSlot_clrA σ _ + · exact VcA.refl σ + +/-- **Every outcome of the finalisation of a mapped frame cap is a `VcA` step.** -/ +theorem fl4FinaliseCap_frame_vcA (isEpt : Bool) (p : ObjRef) (R : CapRights) (sz : VMPageSize) (dev : Bool) + (asid : Asid) (va : VSpaceRef) (final : Bool) : + OutRA VcA (fl4FinaliseCap (Z := Unit) + (.archObjectCap (absFrameE isEpt p R sz dev (some (asid, va)))) final) := by + have ret : ∀ x : Cap × Cap, OutRA VcA (pure x : SMonad Unit (Cap × Cap)) := fun _ => ro_vcA fun _ => ⟨_, rfl⟩ + have base : OutRA VcA (finaliseCap (Z := Unit) (.archObjectCap (.frameCap p R sz dev (some (asid, va)))) final) := by + have h : OutRA VcA (archFinaliseCap (Z := Unit) (.frameCap p R sz dev (some (asid, va))) final) := by + unfold archFinaliseCap + cases final <;> exact OutRA.bind VT (unmapPage_vcA _ _ _ _) fun _ => ret _ + simp only [finaliseCap] + first + | exact h + | exact OutRA.bind VT h fun _ => ret _ + cases isEpt with + | true => + simp only [absFrameE, ↓reduceIte, fl4FinaliseCap] + exact OutRA.bind VT (extUnmapPage_vcA _ _ _ _ _) fun _ => ret _ + | false => + simp only [absFrameE, Bool.false_eq_true, ↓reduceIte, fl4FinaliseCap] + unfold extKindOfAsid + refine OutRA.bind VT (OutRA.bind VT (gets_vcA _) fun _ => ?_) fun k => ?_ + · split <;> exact ro_vcA fun _ => ⟨_, rfl⟩ + · cases k with + | none => exact base + | some k => + cases k with + | high => exact OutRA.bind VT (extUnmapPage_vcA _ _ _ _ _) fun _ => ret _ + | stage1 => exact ret _ + | stage2 => exact ret _ + +theorem fl4FinaliseCap_frame_indepA (isEpt : Bool) (p : ObjRef) (R : CapRights) (sz : VMPageSize) (dev : Bool) + (md : Option (Asid × VSpaceRef)) (d f : Bool) : + fl4FinaliseCap (Z := Unit) (.archObjectCap (absFrameE isEpt p R sz dev md)) d = + fl4FinaliseCap (Z := Unit) (.archObjectCap (absFrameE isEpt p R sz dev md)) f := by + rcases md with _ | ⟨asid, va⟩ <;> cases isEpt <;> cases d <;> cases f <;> rfl + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {s : S} {a : A} {σ : State Unit} {m : McState} + +/-- **A mapped frame cap's unmap, every root kind** (module docstring). -/ +theorem unmap_frame_leafA {ar : machine.Arch A} (K : ArchKeepsA ar AR) (J : JAtZAX (L := L) AR Z s a σ m) + (hfa : ∀ w, FootA (L := L) Z s w → Aligned w) (hTO : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s q) + (hEO : ExtTabOffA (L := L) Z m s) (hK : Aligned (L.ksb s)) (hT : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s)) + (hIpa : S2IpaE ar a) {asid : U64} (h16 : asid.val < 65536) (sz : capability.FrameSize) (va pptr : U64) + (isEpt : Bool) (hVa : isEpt = false → VaOkE va) (Rt : CapRights) (dev final : Bool) : + (if isEpt then (do let i ← capability.FrameSize.bits sz; ept.unmap st ar s a asid va pptr i false) + else vspace.unmap_page st ar s a sz asid va pptr) + ⦃ r => UnmapLeafPostA (L := L) AR Z m s a σ + (fl4FinaliseCap (Z := Unit) (.archObjectCap (absFrameE isEpt (specPtr pptr) Rt (frameSizeOfA sz) dev + (some (BitVec.ofNat 16 asid.val, va.bv)))) final σ).1 r ⦄ := by + have har : AR a Z.hw := J.1.ar + have same : ∀ W : Set ((Cap × Cap) × State Unit), ((Cap.nullCap, Cap.nullCap), σ) ∈ W → + UnmapLeafPostA (L := L) AR Z m s a σ W (s, a) := + fun W h => ⟨σ, h, J, VsSpecA.refl σ, ClrA.refl σ, Or.inl ⟨rfl, rfl⟩⟩ + have vc := fl4FinaliseCap_frame_vcA isEpt (specPtr pptr) Rt (frameSizeOfA sz) dev + (BitVec.ofNat 16 asid.val) va.bv final + rcases unmap_frameXGE J.1.vs J.1.ext hK hT J.2.2.1.obj hIpa h16 sz va pptr isEpt hVa Rt dev final with + ⟨rfl, -, hF, hU⟩ | ⟨σ1, fk, hk, hF, hU⟩ + · rw [if_neg Bool.false_ne_true] + rw [hF] + obtain ⟨-, hm⟩ := finRetA (unmapPage (Z := Unit) (frameSizeOfA sz) (BitVec.ofNat 16 asid.val) va.bv (specPtr pptr)) + (Cap.nullCap, Cap.nullCap) σ + rcases UnmapRelA.extE J.1.vs J.1.ext hU with + ⟨hu, hW⟩ | ⟨slot, ⟨p, hp, i, hi, rfl⟩, hu, ⟨-, ⟨x0, hx0⟩, h3⟩, h4⟩ + · rw [hu, WP.spec_ok] + exact same _ ((hm _).2 ⟨((), σ), by rw [hW]; rfl, rfl⟩) + · rw [hu] + refine WP.spec_bind (K.invPage a Z.hw asid va har) fun a1 h1 => (WP.spec_ok _).2 ?_ + have J' := (J.of_s1ClearA hfa hTO hp hi (unmapPage_kmA _ _ _ _ σ x0 hx0) (h3 x0 hx0).2 (h3 x0 hx0).1 + (h4 x0 hx0)).of_arA h1 + have step := unmapPage_vcA _ _ _ _ σ x0 hx0 + exact ⟨x0.2, (hm _).2 ⟨x0, hx0, rfl⟩, J', step.1, step.2, Or.inr ⟨p, i, hi, Or.inl hp, rfl⟩⟩ + · rw [hF] + have cs : VcA σ σ1 := vc σ ((Cap.nullCap, Cap.nullCap), σ1) (by rw [hF]; rfl) + rcases hU with ⟨hu, hW⟩ | ⟨slot, σ', hu, hW, hR, hE, hw⟩ + · have e := congrArg Prod.fst hW + simp only [Set.singleton_eq_singleton_iff, Prod.mk.injEq, true_and] at e + rw [hu, WP.spec_ok] + exact same _ (by rw [e]; rfl) + · have e := congrArg Prod.fst hW + simp only [Set.singleton_eq_singleton_iff, Prod.mk.injEq, true_and] at e + subst e + obtain ⟨fa, hfl, hfk⟩ := hk (L.wr s slot 0#u64) + rw [hu, hfk] + refine WP.spec_bind (flushE_arKeepA K hfl har) fun a1 h1 => (WP.spec_ok _).2 ?_ + refine ⟨_, rfl, (J.of_extClearA hfa hTO hEO hw hR hE).of_arA h1, cs.1, cs.2, Or.inr ?_⟩ + obtain ⟨⟨q, j, hj, rfl, -, hq⟩, -⟩ := hw + refine ⟨q, j, hj, ?_, rfl⟩ + rcases hq with h | h | h + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr h) + · exact Or.inl h + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelHookKindA.lean b/proofs/Fl4ProofsA64/P3/DelHookKindA.lean new file mode 100644 index 000000000..a7816b87b --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelHookKindA.lean @@ -0,0 +1,87 @@ +/- +P3 (AArch64, delete hooks W0; Architect D10, 2026-12-03 04:41Z): **the object-finaliser hook split by cap kind**. + +`FinHookKA k` is `P3/CnodeDelHookA`'s `FinHookA` restricted to the caps of kind `k` (**`finKindA`**), and +**`finHookA_of_kinds`** assembles `FinHookA` from the thirteen of them; `FinHookA` itself is unchanged. + +No named premise. New module; no existing statement changed. +-/ +import Fl4ProofsA64.P3.CnodeDelHookA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 + +noncomputable section + +/-- The thirteen kinds of `FinObjKindA`. -/ +inductive FinKindA where + | frame | pageTable | pml4 | highVSpace | ept | asidPool + | endpoint | notification | reply | irqHandler + | thread | schedContext | vcpu + deriving DecidableEq + +/-- The kind of an object cap; `none` for the caps the CSpace alone sees. -/ +def finKindA : capability.Cap → Option FinKindA + | .Frame .. => some .frame + | .PageTable .. => some .pageTable + | .Pml4 .. => some .pml4 + | .HighVSpace .. => some .highVSpace + | .Ept .. => some .ept + | .AsidPool .. => some .asidPool + | .Endpoint .. => some .endpoint + | .Notification .. => some .notification + | .Reply .. => some .reply + | .IrqHandler _ => some .irqHandler + | .Thread _ => some .thread + | .SchedContext .. => some .schedContext + | .Vcpu .. => some .vcpu + | _ => none + +theorem finObjKindA_iff_kind (c : capability.Cap) : FinObjKindA c ↔ ∃ k, finKindA c = some k := by + cases c <;> simp [FinObjKindA, finKindA] + +section +variable {S : Type} (st : machine.Store S) [L : LawfulStore st] {A : Type} + +/-- **The finaliser hook of one kind**: `FinHookA` with `FinObjKindA c` replaced by `finKindA c = some k`. -/ +def FinHookKA (k : FinKindA) (ar : machine.Arch A) (th : TypedHeapA) (OS : Set U64) (Fn : FinFnA) (W Wx : DelWA S A) + (Rd Rz : S → A → Fl4SpecA64.State Unit → MdbGhostA → U64 → Prop) : Prop := + ∀ (s : S) (a : A) (σ : Fl4SpecA64.State Unit) (G : MdbGhostA) (sl : CslotPtr) (c : capability.Cap) + (ac : Fl4SpecA64.Cap) (f : Bool), + DStateA st th OS s σ G → W s a σ G → AbsSlotA th sl → capA st s (cslotAddrA sl) = c → finKindA c = some k → + getCap (Z := Unit) sl σ = ({(ac, σ)}, False) → CapEqA c ac → + (NeedsFinCA c → (f = true ↔ isFinalCap' ac σ)) → + objects.finalise_cap st ar s a c f false ⦃ r => + r.1.1 = finRemA c f ∧ CleanCapA r.1.2 ∧ CapWfA r.1.2 ∧ + ∃ σ' G', (∃ d : Bool, (d = true ↔ isFinalCap' ac σ) ∧ + ((absCapA r.1.1, absCapA r.1.2), σ') ∈ (Fn ac d σ).1) ∧ + DStateA st th OS r.2.1 σ' G' ∧ capA st r.2.1 (cslotAddrA sl) = c ∧ + (∀ y ∈ OS, y ≠ cslotAddrA sl → capA st r.2.1 y = capA st s y ∨ + (capA st r.2.1 y = .Null ∧ ¬ IsZombieA (capA st s y))) ∧ + WFA W Wx r.1.1 (cslotAddrA sl) r.2.1 r.2.2 σ' G' ∧ + (RmOKA r.1.1 (cslotAddrA sl) → Rd r.2.1 r.2.2 σ' G' (cslotAddrA sl)) ∧ + (∀ p zt n, r.1.1 = .Zombie p zt n → Rz r.2.1 r.2.2 σ' G' (cslotAddrA sl)) ⦄ + +variable {st} +variable {ar : machine.Arch A} {th : TypedHeapA} {OS : Set U64} {Fn : FinFnA} {W Wx : DelWA S A} + {Rd Rz : S → A → Fl4SpecA64.State Unit → MdbGhostA → U64 → Prop} + +/-- **`FinHookA` from its kinds.** -/ +theorem finHookA_of_kinds (H : ∀ k, FinHookKA st k ar th OS Fn W Wx Rd Rz) : FinHookA st ar th OS Fn W Wx Rd Rz := by + intro s a σ G sl c ac f D hW hsl hc hk hg hac hf + obtain ⟨k, hk'⟩ := (finObjKindA_iff_kind c).1 hk + exact H k s a σ G sl c ac f D hW hsl hc hk' hg hac hf + +/-- Each kind's hook is part of `FinHookA`. -/ +theorem FinHookA.kind (H : FinHookA st ar th OS Fn W Wx Rd Rz) (k : FinKindA) : + FinHookKA st k ar th OS Fn W Wx Rd Rz := by + intro s a σ G sl c ac f D hW hsl hc hk hg hac hf + exact H s a σ G sl c ac f D hW hsl hc ((finObjKindA_iff_kind c).2 ⟨k, hk⟩) hg hac hf + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelIrqFrA.lean b/proofs/Fl4ProofsA64/P3/DelIrqFrA.lean new file mode 100644 index 000000000..9d055d7bd --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelIrqFrA.lean @@ -0,0 +1,359 @@ +/- +P3 (AArch64, delete hooks, contract §64 item 1): **the IRQ-state twins of the scheduler-step frame lemmas**. + +`SpecGlobA` / `SpecSchedA` / `UtCnStepA` carry `irqS` (the spec's `interrupt_states` kept), which the IRQ cleanup +`post_cap_deletion (IrqHandler i)` changes at index `i`. Scripted copies (`handoff/tools/gen_irqfr.py`) without that +field: `SpecGlobIA`, `UtCnStepIA` with the `utFrI_*` frame lemmas, `SpecSchedIA`, `SchedKeepMemIA` (no `irqW`) and +`JAtZAX.of_schedI`, which takes the IRQ relation at the new state as a hypothesis in place of `H.irqW` / `HS.irqS`. +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3SchedPresA +import Fl4ProofsA64.P3.UtFrA +import Fl4ProofsA64.P3.Stage3SchedWrTA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +open Fl4SpecA64 Fl4Spec + +/-- **`SpecGlobA` without `irqS`** (`P3/FrameA`'s record, the IRQ states left free). -/ +structure SpecGlobIA (σ σ' : Fl4SpecA64.State Unit) : Prop where + cur : σ'.curThread = σ.curThread + idle : σ'.idleThread = σ.idleThread + csc : σ'.curSc = σ.curSc + act : σ'.schedulerAction = σ.schedulerAction + time : σ'.curTime = σ.curTime + irqN : σ'.interruptIrqNode = σ.interruptIrqNode + rq : σ'.readyQueues = σ.readyQueues + rls : σ'.releaseQueue = σ.releaseQueue + cons : σ'.consumedTime = σ.consumedTime + repr : σ'.reprogramTimer = σ.reprogramTimer + dom : σ'.curDomain = σ.curDomain + domT : σ'.domainTime = σ.domainTime + asid : σ'.archState.armAsidTable = σ.archState.armAsidTable + + +/-- **A spec step that changes at most CNodes**: globals, IRQ and ASID tables, user memory and every non-CNode object +kept. -/ +structure UtCnStepIA (σ σ' : State Unit) : Prop extends SpecGlobIA σ σ' where + kh : ∀ p, σ'.kheap p = σ.kheap p ∨ + ∃ r cs cs', σ.kheap p = some (.cnode r cs) ∧ σ'.kheap p = some (.cnode r cs') + um : σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory + + +theorem UtCnStepIA.keep {σ σ' : State Unit} (H : UtCnStepIA σ σ') {p : ObjRef} {ko : KernelObject} + (h : σ.kheap p = some ko) (hk : ∀ r cs, ko ≠ .cnode r cs) : σ'.kheap p = some ko := by + rcases H.kh p with e | ⟨r, cs, cs', e1, -⟩ + · rw [e]; exact h + · rw [h] at e1; exact absurd (Option.some.inj e1) (hk r cs) + +theorem UtCnStepIA.back {σ σ' : State Unit} (H : UtCnStepIA σ σ') {p : ObjRef} {ko : KernelObject} + (h : σ'.kheap p = some ko) (hk : ∀ r cs, ko ≠ .cnode r cs) : σ.kheap p = some ko := by + rcases H.kh p with e | ⟨r, cs, cs', -, e2⟩ + · rw [← e]; exact h + · rw [h] at e2; exact absurd (Option.some.inj e2) (hk r cs') + +theorem UtCnStepIA.sched {σ σ' : State Unit} (H : UtCnStepIA σ σ') : SchedSameA σ σ' := by + refine ⟨fun p ko hko => ?_, H.cur, H.idle, H.csc, H.rq, H.rls, H.time, H.cons⟩ + have hk : ∀ r cs, ko ≠ .cnode r cs := fun r cs e => by + rcases hko with ⟨x, e'⟩ | ⟨y, k, e'⟩ <;> rw [e'] at e <;> cases e + exact ⟨fun h => H.back h hk, fun h => H.keep h hk⟩ + +theorem UtCnStepIA.sstep {σ σ' : State Unit} (H : UtCnStepIA σ σ') : SStepA σ σ' := by + refine ⟨⟨H.cur, H.csc, H.idle, H.time, H.cons, H.repr, H.dom, H.domT⟩, H.act, H.rq, H.rls, ?_, ?_, ?_, ?_, ?_⟩ + · intro p x n h; rw [h]; exact H.keep h (fun _ _ e => by cases e) + · intro p x h; exact ⟨x, H.keep h (fun _ _ e => by cases e), rfl, rfl, rfl⟩ + · intro p x n h; rw [h]; exact H.back h (fun _ _ e => by cases e) + · intro p y h; exact ⟨y, H.back h (fun _ _ e => by cases e)⟩ + · intro p y h; exact ⟨y, H.back h (fun _ _ e => by cases e), rfl⟩ + +theorem UtCnStepIA.noBlock {σ σ' : State Unit} (H : UtCnStepIA σ σ') : NoBlockESc σ σ' := by + intro p x y hx hy + have := (H.keep hx (fun _ _ e => by cases e)).symm.trans hy + cases this + exact Or.inl ⟨rfl, id⟩ + +/-! ## Spec-only fields -/ +theorem utCnI_scCovA {C : Set U64} {m : McState} {σ σ' : State Unit} (h : ScCovA C m σ) (H : UtCnStepIA σ σ') : + ScCovA C m σ' := fun p x n hk => h p x n (H.back hk (fun _ _ e => by cases e)) + + +theorem utCnI_idleNoFaultA {m : McState} {σ σ' : State Unit} (h : IdleNoFaultA m σ) (H : UtCnStepIA σ σ') : + IdleNoFaultA m σ' := fun c hc => by + obtain ⟨x, hx, hf⟩ := h c hc + exact ⟨x, H.keep hx (fun _ _ e => by cases e), hf⟩ + +theorem utCnI_sinvA {m : McState} {n : Nat} {σ σ' : State Unit} (h : SInvXA σ m n) + (H : UtCnStepIA σ σ') (hI : SpecIrqA σ') : SInvXA σ' m n := + h.of_spec H.sched H.act hI + + +section fr +variable {Z : XSetsA} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {m : McState} {s s' : S} {σ σ' : State Unit} + {W : U64 → Prop} (hw : WritesIn st s s' W) (hW : ∀ a, W a → ¬ UtRdA st Z N E Rp Bf m s σ a) + (H : UtCnStepIA σ σ') +include hw hW H + +theorem utFrI_schedSRA {κ : U64 → U64} (h : SchedSRA st Z m κ s σ) : SchedSRA st Z m κ s' σ' := + h.stable hw (fun a ha hr => hW a ha (Or.inl hr)) H.sstep H.noBlock + +theorem utFrI_epA (h : EpStateRel Z.T E Rp Bf (L.mem s) σ) (hP : EPlaced Z.T E Rp Bf) : + EpStateRel Z.T E Rp Bf (L.mem s') σ' := by + refine h.frame hP (fun a ha hf => ut_rd_keepA hw hW ha (Or.inr (Or.inl hf))) + ⟨fun p x hk => H.keep hk (fun _ _ e => by cases e), + fun p x hk => ⟨x, H.keep hk (fun _ _ e => by cases e), rfl⟩, + fun p x hk => ⟨x, H.keep hk (fun _ _ e => by cases e), rfl, rfl, rfl, rfl, rfl⟩, H.um⟩ + +theorem utFrI_ntfnA (h : NtfnStateRel N Z.T (L.mem s) σ) (hP : NPlaced Z.T N) : + NtfnStateRel N Z.T (L.mem s') σ' := by + refine h.frame hP (fun a ha hf => ut_rd_keepA hw hW ha (Or.inr (Or.inr (Or.inl hf)))) + ⟨fun p x hk => H.keep hk (fun _ _ e => by cases e), + fun p x hk => ⟨x, H.keep hk (fun _ _ e => by cases e), rfl, rfl, rfl⟩⟩ + +theorem utFrI_replyA {C : Set U64} {scRef : U64 → ObjRef} {stack : U64 → List U64} + (h : ReplyStackRelA (L.mem s) Rp C σ scRef stack) (hal : ∀ r ∈ Rp, Aligned (rNextA r) ∧ Aligned (rPrevA r)) : + ReplyStackRelA (L.mem s') Rp C σ' scRef stack := by + refine h.frame (fun r hr => ?_) (fun c _ x n hk => ⟨x, n, H.keep hk (fun _ _ e => by cases e), rfl⟩) + (fun r _ x hk => ⟨x, H.back hk (fun _ _ e => by cases e), rfl⟩) + have k : ∀ a, (a = rNextA r ∨ a = rPrevA r) → UtRdA st Z N E Rp Bf m s σ a := + fun a ha => Or.inr (Or.inr (Or.inr (Or.inl ⟨r, hr, ha⟩))) + exact ⟨ut_rd_keepA hw hW (hal r hr).1 (k _ (Or.inl rfl)), ut_rd_keepA hw hW (hal r hr).2 (k _ (Or.inr rfl))⟩ + +set_option linter.unusedSectionVars false in +/-- A TCB word of `UtRdA`'s last disjunct is kept. -/ +theorem ut_rd_tcbIA {t a : U64} (ht : t ∈ Z.T) (hal : Aligned a) + (ha : a = mcpA t ∨ a = yieldA t ∨ a = sbWordA t ∨ a = wa (sbWordA t) 8#u64 ∨ a = wa t 3512#u64 ∨ + a = wa t 3664#u64 ∨ a = flagsA t) : L.mem s' a = L.mem s a := + ut_rd_keepA hw hW hal (Or.inr (Or.inr (Or.inr (Or.inr ⟨t, ht, ha⟩)))) + +theorem ut_ibSameIA {t : U64} (ht : t ∈ Z.T) (h1 : Aligned (wa t 3512#u64)) (h2 : Aligned (sbWordA t)) + (h3 : Aligned (wa (sbWordA t) 8#u64)) : IbSame (L := L) s s' t := + ⟨ut_rd_tcbIA hw hW H ht h1 (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl rfl))))), + ut_rd_tcbIA hw hW H ht h2 (Or.inr (Or.inr (Or.inl rfl))), + ut_rd_tcbIA hw hW H ht h3 (Or.inr (Or.inr (Or.inr (Or.inl rfl))))⟩ + +theorem utFrI_bufCoverA (h : BufCover st Z.T Bf s σ) + (hal : ∀ t ∈ Z.T, Aligned (wa t 3512#u64) ∧ Aligned (sbWordA t) ∧ Aligned (wa (sbWordA t) 8#u64)) : + BufCover st Z.T Bf s' σ' := by + intro t ht x b sb w hx hb hl + obtain ⟨h1, h2, h3⟩ := hal t ht + rw [lookup_ipc_buffer_sameA (ut_ibSameIA hw hW H ht h1 h2 h3)] at hl + exact h t ht x b sb w (H.back hx (fun _ _ e => by cases e)) hb hl + +theorem utFrI_mcpA (h : McpRelA Z.T m (L.mem s) σ) (hal : ∀ t ∈ Z.T, Aligned (mcpA t) ∧ Aligned (flagsA t)) : + McpRelA Z.T m (L.mem s') σ' := fun t ht => by + obtain ⟨x, hx, he, hf⟩ := h t ht + refine ⟨x, H.keep hx (fun _ _ e => by cases e), ?_, fun g => (hf g).keep ?_ rfl⟩ + · rw [ut_rd_tcbIA hw hW H ht (hal t ht).1 (Or.inl rfl)]; exact he + · exact ut_rd_tcbIA hw hW H ht (hal t ht).2 (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr rfl)))))) + +theorem utFrI_yieldA {C : Set U64} (h : YieldRelA Z.T C m (L.mem s) σ) (hal : ∀ t ∈ Z.T, Aligned (yieldA t)) : + YieldRelA Z.T C m (L.mem s') σ' := by + have k : ∀ t ∈ Z.T, L.mem s' (yieldA t) = L.mem s (yieldA t) := + fun t ht => ut_rd_tcbIA hw hW H ht (hal t ht) (Or.inr (Or.inl rfl)) + refine ⟨fun t ht => ?_, fun t ht hnz => ?_, fun c hc y n hy p hp => ?_⟩ + · obtain ⟨x, hx, he⟩ := h.tcb t ht + exact ⟨x, H.keep hx (fun _ _ e => by cases e), by rw [k t ht]; exact he⟩ + · rw [k t ht] at hnz ⊢ + obtain ⟨hc, y, n, hy, hf⟩ := h.toSc t ht hnz + exact ⟨hc, y, n, H.keep hy (fun _ _ e => by cases e), hf⟩ + · obtain ⟨t, ht, e1, e2⟩ := h.frm c hc y n (H.back hy (fun _ _ e => by cases e)) p hp + exact ⟨t, ht, e1, by rw [k t ht]; exact e2⟩ + + +end fr + +/-- **A scheduler spec step**: objects are kept, or are TCBs with the same cap slots and VCPU links, or are SCs, +notifications, endpoints or replies replaced by one of their kind; the IRQ state, ASID table, CDT and original-cap map +are kept. -/ +structure SpecSchedIA (σ σ' : Fl4SpecA64.State Unit) : Prop where + kh : ∀ p, σ'.kheap p = σ.kheap p ∨ + (∃ x x', σ.kheap p = some (.tcb x) ∧ σ'.kheap p = some (.tcb x') ∧ TcbSlotKeepA x x') ∨ + ∃ o o', σ.kheap p = some o ∧ σ'.kheap p = some o' ∧ FrameStepA o o' + irqN : σ'.interruptIrqNode = σ.interruptIrqNode + asid : σ'.archState.armAsidTable = σ.archState.armAsidTable + /-- (record edit #7) The current VCPU is kept. -/ + vcur : σ'.archState.armCurrentVcpu = σ.archState.armCurrentVcpu + cdt : σ'.cdt = σ.cdt + orig : σ'.isOriginalCap = σ.isOriginalCap + +namespace SpecSchedIA +variable {σ σ' : Fl4SpecA64.State Unit} (H : SpecSchedIA σ σ') +include H + +theorem keep {p : Fl4SpecA64.ObjRef} {ko : Fl4SpecA64.KernelObject} (h : σ.kheap p = some ko) + (ht : ∀ x, ko ≠ .tcb x) (hs : ¬ FrameKindA ko) : σ'.kheap p = some ko := by + rcases H.kh p with e | ⟨x, x', e1, -⟩ | ⟨o, o', e1, -, f⟩ + · rw [e]; exact h + · rw [h] at e1; exact absurd (Option.some.inj e1) (ht x) + · rw [h] at e1; cases e1; exact absurd f.kind hs + +theorem back {p : Fl4SpecA64.ObjRef} {ko : Fl4SpecA64.KernelObject} (h : σ'.kheap p = some ko) + (ht : ∀ x, ko ≠ .tcb x) (hs : ¬ FrameKindA ko) : σ.kheap p = some ko := by + rcases H.kh p with e | ⟨x, x', -, e2, -⟩ | ⟨o, o', -, e2, f⟩ + · rw [← e]; exact h + · rw [h] at e2; exact absurd (Option.some.inj e2) (ht x') + · rw [h] at e2; cases e2; exact absurd f.kind' hs + +theorem tcb {p : Fl4SpecA64.ObjRef} {x : Fl4SpecA64.Tcb} (h : σ.kheap p = some (.tcb x)) : + ∃ x', σ'.kheap p = some (.tcb x') ∧ TcbSlotKeepA x x' := by + rcases H.kh p with e | ⟨y, y', e1, e2, hy⟩ | ⟨o, o', e1, -, f⟩ + · exact ⟨x, by rw [e]; exact h, rfl, rfl, rfl⟩ + · rw [h] at e1 + obtain rfl : x = y := by injection e1 with e1; injection e1 + exact ⟨y', e2, hy⟩ + · rw [h] at e1; cases e1; exact absurd f.kind FrameKindA.tcb_not + +theorem tcbBack {p : Fl4SpecA64.ObjRef} {x : Fl4SpecA64.Tcb} (h : σ'.kheap p = some (.tcb x)) : + ∃ y, σ.kheap p = some (.tcb y) := by + rcases H.kh p with e | ⟨y, _, e1, -⟩ | ⟨o, o', -, e2, f⟩ + · exact ⟨x, by rw [← e]; exact h⟩ + · exact ⟨y, e1⟩ + · rw [h] at e2; cases e2; exact absurd f.kind' FrameKindA.tcb_not + +/-- An object that is neither a TCB nor of a changeable kind is untouched. -/ +theorem other {p : Fl4SpecA64.ObjRef} (ht : ∀ x, σ.kheap p ≠ some (.tcb x)) + (hs : ∀ o, FrameKindA o → σ.kheap p ≠ some o) : σ'.kheap p = σ.kheap p := by + rcases H.kh p with e | ⟨x, x', e1, -⟩ | ⟨o, o', e1, -, f⟩ + · exact e + · exact absurd e1 (ht x) + · exact absurd e1 (hs o f.kind) + +/-- A scheduler or IPC step keeps the cap every spec slot holds (CNodes are untouched; TCBs keep their slots). -/ +theorem specCapAt (sl : Fl4SpecA64.CslotPtr) : specCapAtA σ' sl = specCapAtA σ sl := by + unfold specCapAtA + rcases H.kh sl.1 with e | ⟨x, x', e1, e2, k⟩ | ⟨o, o', e1, e2, f⟩ + · rw [e] + · rw [e1, e2] + show Fl4SpecA64.tcbCnodeMap x' sl.2 = Fl4SpecA64.tcbCnodeMap x sl.2 + rw [k.1] + · rw [e1, e2] + rcases f with ⟨_, _, _, _, rfl, rfl⟩ | ⟨_, _, rfl, rfl⟩ | ⟨_, _, rfl, rfl⟩ | ⟨_, _, rfl, rfl⟩ <;> rfl + +end SpecSchedIA + +/-- **The memory a scheduler step keeps** among the words `JAtZAX` reads: the CSpace / TCB slots, the TCB VCPU +links, the VCPUs, the IRQ states and the VSpace and Stage-2 words. -/ +structure SchedKeepMemIA (Z : XSetsA) (s s' : S) : Prop where + ksb : L.ksb s' = L.ksb s + cpu : L.cpu s' = L.cpu s + slot : ∀ y, SlotWA Z.th y → ∀ w, (w = y ∨ w = wa y 8#u64 ∨ w = wa y 16#u64 ∨ w = wa y 24#u64) → + L.mem s' w = L.mem s w + tcbW : ∀ t ∈ Z.T, L.mem s' (wa t 3664#u64) = L.mem s (wa t 3664#u64) ∧ L.mem s' (runVcpuA t) = L.mem s (runVcpuA t) + vcW : ∀ v ∈ Z.V, L.mem s' v = L.mem s v ∧ L.mem s' (wa v 24#u64) = L.mem s (wa v 24#u64) + vs : ∀ w, VsFootA Z.Pp Z.S1 Z.Y (L.ksb s) w → L.mem s' w = L.mem s w + ext : ∀ w, ExtFootE Z.X Z.Y Z.T2 w → L.mem s' w = L.mem s w + +variable {A : Type} in +/-- **`JAtZAX` across a scheduler step**: the scheduler relations and the nodes' cursor typing come from the step's +post, the rest from `SchedKeepMemIA` and `SpecSchedIA`. The sibling of `JAtZAX.of_slot` (which needs agreement on all of +`FootNA`, including the words a scheduler step writes). -/ +theorem JAtZAX.of_schedI {AR : A → HwRootA → Prop} {Z : XSetsA} {s s' : S} {a a' : A} + {σ σ' : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} (J : JAtZAX (L := L) AR Z s a σ m) + {Q : U64 → U64 → List U64} {R : U64 → List U64} {curOf curScOf : Fl4SpecA64.CoreId → Fl4SpecA64.ObjRef} + (H : SchedKeepMemIA (st := st) Z s s') (HS : SpecSchedIA σ σ') (har : AR a' Z.hw) + (hirq : IrqRelA (L.mem s') (L.ksb s') σ') + (hsch : SchedRelA (L := L) Z.T Z.C Z.Cs curOf curScOf s' σ' m) + (hrest : SchedRestA (L := L) { Z with Q := Q, R := R, curOf := curOf, curScOf := curScOf } s' σ' m) + (hidleT : ∀ c ∈ Z.Cs, L.mem s' (L.nodeAt s' c layout.node.IDLE_THREAD) ∈ Z.T) + (hidleC : ∀ c ∈ Z.Cs, L.mem s' (L.nodeAt s' c layout.node.IDLE_SC) ∈ Z.C) + (hcurT : ∀ c ∈ Z.Cs, L.mem s' (L.nodeAt s' c layout.node.CUR_THREAD) ∈ Z.T) + (hcscC : ∀ c ∈ Z.Cs, L.mem s' (L.nodeAt s' c layout.node.CUR_SC) ∈ Z.C) : + JAtZAX (L := L) AR { Z with Q := Q, R := R, curOf := curOf, curScOf := curScOf } s' a' σ' m := by + obtain ⟨J, V, X, Y⟩ := J + have hcap : ∀ y, SlotWA Z.th y → capAtA (L.mem s') y = capAtA (L.mem s) y := fun y hy => by + unfold capAtA + rw [H.slot y hy _ (Or.inl rfl), H.slot y hy _ (Or.inr (Or.inl rfl))] + have hst : StateRelA Z.th (L.mem s') σ' := by + constructor + · intro p r hp + obtain ⟨cs, h1, h2, h3⟩ := J.st.cnode p r hp + refine ⟨cs, HS.keep h1 (fun _ h => by cases h) (fun h => by rcases h with ⟨_, _, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ <;> cases e), h2, fun idx hl => ?_⟩ + obtain ⟨ac, e, hr⟩ := h3 idx hl + exact ⟨ac, e, by rw [hcap _ (Or.inr ⟨p, r, hp, idx, hl, rfl⟩)]; exact hr⟩ + · intro t ht + obtain ⟨x, h1, h2⟩ := J.st.tcb t ht + obtain ⟨x', e', hx⟩ := HS.tcb h1 + refine ⟨x', e', fun i hi => ?_⟩ + obtain ⟨ac, e, hr⟩ := h2 i hi + refine ⟨ac, by rw [hx.1]; exact e, ?_⟩ + rw [hcap _ (Or.inl ⟨t, Or.inr ⟨ht, i, hi, rfl⟩⟩)]; exact hr + have hcdt : CdtRelA st Z.th s' σ' Z.G := + CdtRelA.mkA (treeA_of_mem J.cdt.tree fun y hy => + ⟨H.slot y (Or.inl hy) _ (Or.inl rfl), H.slot y (Or.inl hy) _ (Or.inr (Or.inl rfl)), + H.slot y (Or.inl hy) _ (Or.inr (Or.inr (Or.inl rfl))), H.slot y (Or.inl hy) _ (Or.inr (Or.inr (Or.inr rfl)))⟩) + (by rw [HS.cdt]; exact J.cdt.cdt) (by rw [HS.cdt]; exact J.cdt.cdt_slot) + (fun sl hs => by + have e : revA (L.mem s') (cslotAddrA sl) = revA (L.mem s) (cslotAddrA sl) := by + unfold revA + rw [H.slot _ (absSlot_w hs) _ (Or.inr (Or.inr (Or.inr rfl)))] + rw [HS.orig, e]; exact J.cdt.orig sl hs) + J.cdt.par_slot (by unfold SpecPOA; rw [HS.cdt, HS.orig]; exact J.cdt.po) + have J' : JAtZA (L := L) AR { Z with Q := Q, R := R, curOf := curOf, curScOf := curScOf } s' a' σ' m := + { st := hst + cdt := hcdt + sch := hsch + ar := har + place := by rw [H.ksb]; exact J.place + irq := hirq + rest := hrest + thT := J.thT + csN := J.csN + idleT := hidleT + idleC := hidleC + curT := hcurT + cscC := hcscC + ksFit := by rw [H.ksb]; exact J.ksFit + scOffD := J.scOffD + cv := fun y hy => by rw [hcap y (Or.inl hy)]; exact J.cv y hy + pml := fun y hy => by rw [hcap y (Or.inl hy)]; exact J.pml y hy + vs := by + refine (show VsRelA Z.Pp Z.S1 Z.Lv Z.X Z.Y (L.mem s') (L.ksb s') σ from ?_).of_spec HS.asid fun p hp => + HS.other (fun x h => J.vs.not_tcb hp h) (fun o ho h => ?_) + · rw [H.ksb]; exact J.vs.of_mem fun b hb => H.vs b hb + · rcases hp with ⟨q, hq, rfl⟩ | ⟨q, hq, rfl⟩ | ⟨q, hq, rfl⟩ + · obtain ⟨f, hk, -⟩ := J.vs.pool q hq; rw [hk] at h; cases h; rcases ho with ⟨_, _, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ <;> cases e + · obtain ⟨t, hk, -⟩ := J.vs.pt q hq; rw [hk] at h; cases h; rcases ho with ⟨_, _, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ <;> cases e + · obtain ⟨l1, hk, -⟩ := J.vs.high q hq; rw [hk] at h; cases h; rcases ho with ⟨_, _, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ <;> cases e + ext := by + refine (J.ext.of_mem fun b hb => H.ext b hb).of_spec HS.asid fun q hq => + HS.other (fun x h => ?_) (fun o ho h => ?_) + · rcases hq with hq | hq + · obtain ⟨f, hk, -⟩ := J.vs.pool q hq; rw [hk] at h; cases h + · exact J.ext.not_tcb hq x h + rcases hq with hq | hq | hq + · obtain ⟨f, hk, -⟩ := J.vs.pool q hq; rw [hk] at h; cases h; rcases ho with ⟨_, _, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ <;> cases e + · obtain ⟨es, hk, -⟩ := J.ext.s2root q hq; rw [hk] at h; cases h; rcases ho with ⟨_, _, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ <;> cases e + · obtain ⟨pt, hk, -⟩ := J.ext.s2pt q hq; rw [hk] at h; cases h; rcases ho with ⟨_, _, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ <;> cases e } + have hsl : ∀ y, IsSlotA Z.th y → capAtA (L.mem s') y = capAtA (L.mem s) y := fun y hy => hcap y (Or.inl hy) + refine ⟨J', ?_, ?_, ?_⟩ + · refine ⟨fun v hv => ?_, fun p x e => V.back p x (HS.back e (fun _ h => by cases h) (fun h => by rcases h with ⟨_, _, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ <;> cases e)), + fun t ht => ?_, fun y hy p e => V.cap y hy p (by rw [← hsl y hy]; exact e)⟩ + · obtain ⟨x, e, h2⟩ := V.obj v hv + rw [(H.vcW v hv).1, (H.vcW v hv).2] + exact ⟨x, HS.keep e (fun _ h => by cases h) (fun h => by rcases h with ⟨_, _, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ <;> cases e), h2⟩ + · obtain ⟨x, e, h1, h2⟩ := V.tcb t ht + obtain ⟨x', e', hx⟩ := HS.tcb e + rw [(H.tcbW t ht).1, (H.tcbW t ht).2] + exact ⟨x', e', by rw [hx.2.1]; exact h1, by rw [hx.2.2]; exact h2⟩ + · refine ⟨fun r hr => ?_, fun p es e => X.back p es (HS.back e (fun _ h => by cases h) (fun h => by rcases h with ⟨_, _, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ <;> cases e)), + fun y hy p l mp asid va e => X.cap y hy p l mp asid va (by rw [← hsl y hy]; exact e)⟩ + obtain ⟨es, e⟩ := X.obj r hr + exact ⟨es, HS.keep e (fun _ h => by cases h) (fun h => by rcases h with ⟨_, _, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ <;> cases e)⟩ + · refine ⟨fun r hr => ?_, fun p l1 e => Y.back p l1 (HS.back e (fun _ h => by cases h) (fun h => by rcases h with ⟨_, _, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ <;> cases e)), + fun y hy p mp asid e => Y.cap y hy p mp asid (by rw [← hsl y hy]; exact e)⟩ + obtain ⟨l1, e⟩ := Y.obj r hr + exact ⟨l1, HS.keep e (fun _ h => by cases h) (fun h => by rcases h with ⟨_, _, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ <;> cases e)⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelIrqHookA.lean b/proofs/Fl4ProofsA64/P3/DelIrqHookA.lean new file mode 100644 index 000000000..24ad62c7a --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelIrqHookA.lean @@ -0,0 +1,260 @@ +/- +P3 (AArch64, delete hooks W3): **the IrqHandler finaliser hook** `finHookIrqA : FinHookKA .irqHandler`. A final handler +cap deletes the IRQ-node slot (`deleting_irq_handler` → `delete_one`): its cap is `Null` or a notification cap +(`irqSlot_capA`, from `SpecIrqA.slot`), the notification finaliser keeps the record (`JStage3TA.ntfnFin_recA`) and the +inner `empty_slot` goes through `emptyHyp_delA`. + +Premises: [J] `F : DelFootA` (the deletion's foot, as the loop's swap / empty hooks take it); [P] `hCs` / `hmb` / +`hmax` / `htu`; [S] `h1` (`numNodes = 1`). New module. +-/ +import Fl4ProofsA64.P3.DelNtfnHookA +import Fl4ProofsA64.P3.DelStepAsmA +import Fl4ProofsA64.P3.IrqInvokeSlotsA +import Fl4ProofsA64.P3.DelStepSpecA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 Fl4ProofsA64.P3.Endp +open Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Notif + +local notation "wa" => core.num.U64.wrapping_add + +noncomputable section + +section +variable {m : Fl4SpecA64.McState} +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + +/-- **The cap of an IRQ-node slot is `Null` or a notification cap** (`SpecIrqA.slot` through the CSpace relation). -/ +theorem irqSlot_capA {th : TypedHeapA} {s : S} {σ : State Unit} (R : StateRelA th (L.mem s) σ) + (I : IrqRelA (L.mem s) (L.ksb s) σ) (P : PlaceA th (L.ksb s)) (J : SpecIrqA σ) {irq : U64} + (hi : irq.val < 5376) {ac : Fl4SpecA64.Cap} + (hg : getCap (Z := Unit) (σ.interruptIrqNode (BitVec.ofNat 13 irq.val), []) σ = ({(ac, σ)}, False)) : + IrqCapA ac := by + have hth := P.irq irq hi + have enode := I.node irq hi + obtain ⟨cs, hk, -, -⟩ := R.cnode _ 0 hth + have hk' : σ.kheap (σ.interruptIrqNode (BitVec.ofNat 13 irq.val)) = some (.cnode 0 cs) := by + rw [enode]; exact hk + have hsp := getCap_specAtA (σ := σ) (σ' := σ) (sl := (σ.interruptIrqNode (BitVec.ofNat 13 irq.val), [])) + (c := ac) (by rw [hg]; exact rfl) + unfold specCapAtA at hsp + dsimp only at hsp + rw [hk'] at hsp + dsimp only at hsp + split_ifs at hsp + have hlt : (BitVec.ofNat 13 irq.val).toNat < Fl4SpecA64.fl4IrqCount := by + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)]; exact hi + exact J.slot _ hlt _ [] ac hk' hsp + +/-- The final notification finaliser's outcome is `fast_finalise`'s. -/ +theorem ntfn_fastA {r : ObjRef} {σ σ1 σ2 σ3 : State Unit} + (h1 : ((), σ1) ∈ (schedContextMaybeUnbindNtfn (Z := Unit) r σ).1) + (h2 : ((), σ2) ∈ (unbindMaybeNotification (Z := Unit) r σ1).1) + (h3 : ((), σ3) ∈ (cancelAllSignals (Z := Unit) r σ2).1) : + ((), σ3) ∈ ((schedContextMaybeUnbindNtfn (Z := Unit) r >>= fun _ => + unbindMaybeNotification r >>= fun _ => cancelAllSignals r) σ).1 := + mem_bind_ndD h1 (mem_bind_ndD h2 h3) + +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Pp Tb : Set U64} {Lv : U64 → Nat} {ia ie : Nat} + {N E Rp : Set U64} {κ : U64 → U64} {n : Nat} + +open Classical in +set_option maxHeartbeats 1000000 in +/-- **Deleting the IRQ-node slot of a final handler** keeps the loop invariant. -/ +theorem deletingIrqHandler_recA {ipa : U64} (s0 : S) + (Rv : S → A → State Unit → MdbGhostA → U64 → Prop) + (F : DelFootA st Z H.tcbs H.omega H.frame OS s0) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (h1 : fl4A64Platform.numNodes = 1) + {s : S} {a : A} {σ : State Unit} {G : MdbGhostA} {irq : U64} (hi : irq.val < 5376) + (D : DStateA st Z.th OS s σ G) + (W : DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st (J7R2A st ar ipa))) s a σ G) : + interrupt.deleting_irq_handler st ar s a irq ⦃ r => + ∃ σ' G' x, x = irqSlotA (L.ksb s) irq ∧ ((), σ') ∈ (deletingIrqHandler (Z := Unit) (BitVec.ofNat 13 irq.val) σ).1 ∧ + DStateA st Z.th OS r.1 σ' G' ∧ + DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st (J7R2A st ar ipa))) + r.1 r.2 σ' G' ∧ + capA st r.1 x = .Null ∧ (∀ y ∈ OS, y ≠ x → capA st r.1 y = capA st s y) ∧ + (capA st s x = .Null ∨ ∃ p b0 b1 b2, capA st s x = .Notification p b0 b1 b2) ⦄ := by + have W0 := W + obtain ⟨hcore, ⟨Q, R, hw, Ac, Bf, stack, J, X⟩, hPA, hZT, hBH, hCT, hFx⟩ := W + have irqRel : IrqRelA (L.mem s) (L.ksb s) σ := J.j.1.irq + have place : PlaceA Z.th (L.ksb s) := J.j.1.place + obtain ⟨hA, he, hslot⟩ := irqSlot_relatedA (st := st) irqRel place hi + have lift : ∀ x : CslotPtr, x = (σ.interruptIrqNode (BitVec.ofNat 13 irq.val), []) → + ∀ σ', ((), σ') ∈ (capDeleteOne (Z := Unit) x σ).1 → + ((), σ') ∈ (deletingIrqHandler (Z := Unit) (BitVec.ofNat 13 irq.val) σ).1 := by + rintro x rfl σ' h + unfold deletingIrqHandler + exact mem_bind_ndD (x := ((σ.interruptIrqNode (BitVec.ofNat 13 irq.val), []), σ)) rfl h + unfold interrupt.deleting_irq_handler + rw [hslot, bind_ok, ← he] + generalize esl : ((σ.interruptIrqNode (BitVec.ofNat 13 irq.val), []) : CslotPtr) = sl at hA he ⊢ + obtain ⟨ac, hg, hace⟩ := getCapOne_eqA (st := st) D.rel hA + have hxO := D.mem_slot hA + by_cases hcn : capA st s (cslotAddrA sl) = .Null + · obtain ⟨run, post⟩ := irqDeleteNullA (st := st) (ar := ar) (a := a) D.rel hA + (by rw [capAtA_eq (st := st)]; exact hcn) + rw [run, WP.spec_ok] + exact ⟨σ, G, cslotAddrA sl, rfl, lift sl esl.symm σ post, D, W0, hcn, fun _ _ _ => rfl, Or.inl hcn⟩ + have hirqc : IrqCapA ac := irqSlot_capA D.rel irqRel place J.sinv.irqN hi (by rw [esl]; exact hg) + obtain ⟨p, b0, b1, b2, hc⟩ : ∃ p b0 b1 b2, capA st s (cslotAddrA sl) = .Notification p b0 b1 b2 := by + have hne := hace.ne_null hcn (D.wf hxO) + rcases hirqc with h | ⟨r, bd, rs, h⟩ + · exact absurd h hne + have e : absCapA (capA st s (cslotAddrA sl)) = .notificationCap r bd rs := by + rcases hace.eq_or_pt with e | ⟨_, _, _, e⟩ + · rw [← e]; exact h + · rw [h] at e; cases e + generalize capA st s (cslotAddrA sl) = c at e ⊢ + cases c <;> simp [absCapA] at e + exact ⟨_, _, _, _, rfl⟩ + have hH : H p = some .ntfn := (J.capTy _ hxO).2.1 p b0 b1 b2 hc + have unread : ¬ S1aR2Reads (capability.Cap.Notification p b0 b1 b2) := fun h => h + obtain ⟨bf, hbf1, hbf2⟩ := (finalHypA (st := st) Z.th OS G.chains s σ sl (.Notification p b0 b1 b2) D.toFinStA hA hc + trivial).1 + have ebf : bf = decide (isFinalCap' (absCapA (.Notification p b0 b1 b2)) σ) := by + rw [Bool.eq_iff_iff, decide_eq_true_iff]; exact hbf2 + rw [ebf] at hbf1 + let M : S → A → State Unit → Prop := fun s1 a1 σ1 => a1 = a ∧ DStateA st Z.th OS s1 σ1 G ∧ OSKeep st OS s s1 ∧ + DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st (J7R2A st ar ipa))) s1 a σ1 G + let Rr : S → A → State Unit → Prop := fun s2 a2 σ2 => + DStateA st Z.th OS s2 σ2 (remGA G (cslotAddrA sl)) ∧ + DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st (J7R2A st ar ipa))) + s2 a2 σ2 (remGA G (cslotAddrA sl)) ∧ + capA st s2 (cslotAddrA sl) = .Null ∧ (∀ y ∈ OS, y ≠ cslotAddrA sl → capA st s2 y = capA st s y) + have finish : objects.finalise_cap_object st ar s a (.Notification p b0 b1 b2) + (decide (isFinalCap' (absCapA (.Notification p b0 b1 b2)) σ)) ⦃ r => + ∃ σ1, ((), σ1) ∈ (fastFinalise (Z := Unit) (absCapA (.Notification p b0 b1 b2)) + (decide (isFinalCap' (absCapA (.Notification p b0 b1 b2)) σ)) σ).1 ∧ M r.2.1 r.2.2 σ1 ⦄ := by + cases decide (isFinalCap' (absCapA (.Notification p b0 b1 b2)) σ) + · have e : objects.finalise_cap_object st ar s a (.Notification p b0 b1 b2) false = + ok ((capability.Cap.Null, capability.Cap.Null), s, a) := rfl + rw [e, WP.spec_ok] + exact ⟨σ, rfl, rfl, D, fun _ _ => ⟨rfl, rfl, rfl, rfl⟩, W0⟩ + · refine WP.spec_mono (J.ntfnFin_recA (b0 := b0) (b1 := b1) (b2 := b2) hH hCs hmb hmax htu + (fun _ _ t ht _ => J.onCore_single hCs h1 ht)) ?_ + rintro ⟨⟨rem, cl⟩, s1, a1⟩ ⟨-, ha, σ1, σ2, σ3, Q', R', m1, m2, m3, J', k, kb, cpu, -, vc, k6⟩ + dsimp only at ha J' k kb cpu vc k6 ⊢ + subst ha + have caps : ∀ y ∈ OS, capA st s1 y = capA st s y := fun y hy => k.capA hy + have D' : DStateA st Z.th OS s1 σ3 G := DStateA.keepOSA (Z := { Z with G := G, Q := Q', R := R', hw := hw }) D J' k + refine ⟨σ3, ntfn_fastA m1 m2 m3, rfl, D', k, ⟨kb.trans hcore.1, cpu.trans hcore.2⟩, + ⟨Q', R', hw, Ac, Bf, stack, J', (k6 D.sub (X.ghostA G Q R hw)).ghostA Z.G Z.Q Z.R Z.hw⟩, ParentAdjA.keepOS D.lk k hPA, + ZombieTcbNoVcpuA.keepA caps J.zombieTcb (fun t ht => vc t (J.tcbs ▸ ht)) hZT, + bufHold_sameA st hBH caps, ctOff_sameA st hCT kb caps, hFx.keepA st D.sub caps⟩ + have empty : ∀ s1 a1 σ1, M s1 a1 σ1 → cdt.empty_slot st ar s1 a1 (cslotAddrA sl) .Null ⦃ r => + ∃ σ2, ((), σ2) ∈ (emptySlot (Z := Unit) sl .nullCap σ1).1 ∧ Rr r.1 r.2 σ2 ⦄ := by + rintro s1 a1 σ1 ⟨ha1, D1, k, W1⟩ + subst a1 + have c1 : capA st s1 (cslotAddrA sl) = .Notification p b0 b1 b2 := (k.capA hxO).trans hc + have cn1 : capA st s1 (cslotAddrA sl) ≠ .Null := by rw [c1]; simp + obtain ⟨hcore1, ⟨Q1, R1, hw1, Ac1, Bf1, stack1, J1, X1⟩, hPA1, hZT1, hBH1, hCT1, hFx1⟩ := W1 + have Wx1 : DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st J7R2tA)) s1 a σ1 G := + ⟨hcore1, ⟨Q1, R1, hw1, Ac1, Bf1, stack1, J1.mapJ7A fun _ _ => trivial, X1⟩, hPA1, hZT1, hBH1, hCT1, hFx1⟩ + have Rd1 : DelRdXA st ar Z H OS ipa (RvVcA st OS Rv) s1 a σ1 G (cslotAddrA sl) := + ⟨⟨delRdA_other (fun r g h => by rw [c1] at h; cases h) (fun r g h => by rw [c1] at h; cases h) + (fun v h => by rw [c1] at h; cases h), rdThA_of_ne fun t h => by rw [c1] at h; cases h⟩, + m, J1.j7.pinUnreadA (by rw [c1]; exact unread)⟩ + refine WP.spec_mono (empty_slot_sA (ar := ar) (a := a) F.os D1 hA cn1) ?_ + rintro ⟨s2, a2⟩ ⟨ha2, hfr, hx2, hoth, σ2, hes, hSS, D2, hMw⟩ + dsimp only at ha2 hfr hx2 hoth hes hSS D2 ⊢ + subst a2 + have W2 := emptyHyp_delPA (Rv := Rv) slotKeep_std6A std6A_monoA F s1 s2 a σ1 σ2 G sl Wx1 Rd1 D1 D2 hfr hSS hes hA cn1 hx2 hoth hMw + exact ⟨σ2, hes, D2, W2, hx2, fun y hy hne => (hoth y hy hne).trans (k.capA hy)⟩ + refine WP.spec_mono (irqDeleteNotificationA (ar := ar) (a := a) D.rel hA + (by rw [capAtA_eq (st := st)]; exact hc) hbf1 finish empty) ?_ + rintro r ⟨σ2, step, D2, W2, hx2, hoth⟩ + exact ⟨σ2, _, cslotAddrA sl, rfl, lift sl esl.symm σ2 step, D2, W2, hx2, hoth, Or.inr ⟨p, b0, b1, b2, hc⟩⟩ + +/-- **The IrqHandler finaliser hook** (module docstring). -/ +theorem finHookIrqA {ipa : U64} (s0 : S) + (Rv : S → A → State Unit → MdbGhostA → U64 → Prop) + (F : DelFootA st Z H.tcbs H.omega H.frame OS s0) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (h1 : fl4A64Platform.numNodes = 1) : + FinHookKA st .irqHandler ar Z.th OS fl4FinaliseCap + (DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st (J7R2A st ar ipa)))) + (DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st J7R2tA))) + (DelRdXA st ar Z H OS ipa (RvVcA st OS Rv)) (DelRzA st) := by + intro s a σ G sl c ac f D W hsl hc hk hg hac hf + obtain ⟨irq, rfl⟩ : ∃ irq, c = .IrqHandler irq := by + cases c <;> simp [finKindA] at hk + exact ⟨_, rfl⟩ + have eac : ac = absCapA (.IrqHandler irq) := hac.symm + have hfin := hf trivial + have hOSx : cslotAddrA sl ∈ OS := D.mem_slot hsl + have hwf : CapWfA (.IrqHandler irq) := hc ▸ D.wf hOSx + have hi : irq.val < 5376 := hwf + have unread : ¬ S1aR2Reads (capability.Cap.IrqHandler irq) := fun h => h + have post : ∀ (s' : S) (a' : A) (σ' : State Unit) (G' : MdbGhostA) (cl : capability.Cap), + DStateA st Z.th OS s' σ' G' → capA st s' (cslotAddrA sl) = .IrqHandler irq → + DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st (J7R2A st ar ipa))) s' a' σ' G' → + WFA (DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st (J7R2A st ar ipa)))) + (DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st J7R2tA))) + .Null (cslotAddrA sl) s' a' σ' G' ∧ + (RmOKA .Null (cslotAddrA sl) → + DelRdXA st ar Z H OS ipa (RvVcA st OS Rv) s' a' σ' G' (cslotAddrA sl)) := by + intro s' a' σ' G' cl D' e W' + obtain ⟨hcore, ⟨Q, R, hw, Ac, Bf, stack, J, X⟩, hPA, hZT, hBH, hCT, hFx⟩ := W' + refine ⟨⟨fun h => absurd (Or.inl rfl) h, fun _ => ⟨hcore, ⟨Q, R, hw, Ac, Bf, stack, + J.mapJ7A fun _ _ => trivial, X⟩, hPA, hZT, hBH, hCT, hFx⟩⟩, fun _ => ?_⟩ + exact ⟨⟨delRdA_other (fun r g h => by rw [e] at h; cases h) (fun r g h => by rw [e] at h; cases h) + (fun v h => by rw [e] at h; cases h), rdThA_of_ne fun t h => by rw [e] at h; cases h⟩, + m, J.j7.pinUnreadA (by rw [e]; exact unread)⟩ + unfold objects.finalise_cap + cases f + · simp only [Bool.false_eq_true, ↓reduceIte, bind_ok, uncurry_apply_pair] + have e : objects.finalise_cap_object st ar s a (.IrqHandler irq) false = + ok ((capability.Cap.Null, capability.Cap.Null), s, a) := rfl + have hnoop : ((Fl4SpecA64.Cap.nullCap, Fl4SpecA64.Cap.nullCap), σ) ∈ + (fl4FinaliseCap (Z := Unit) ac false σ).1 := by + rw [eac] + simp only [absCapA] + rw [fl4FinaliseCap_plainA (by intro h; exact h)] + exact rfl + rw [e, WP.spec_ok] + have p := post s a σ G .Null D hc W + exact ⟨rfl, trivial, trivial, σ, G, ⟨false, hfin, hnoop⟩, D, hc, fun y _ _ => Or.inl rfl, p.1, p.2, + fun _ _ _ h => by cases h⟩ + · simp only [↓reduceIte] + refine WP.spec_bind (deletingIrqHandler_recA s0 Rv F hCs hmb hmax htu h1 hi D W) ?_ + rintro ⟨s', a'⟩ ⟨σ', G', x, -, step, D', W', hx, hoth, hxc⟩ + dsimp only at D' W' hx hoth ⊢ + simp only [uncurry_apply_pair] + have ne : cslotAddrA sl ≠ x := fun e => by + rw [← e, hc] at hxc + rcases hxc with h | ⟨_, _, _, _, h⟩ <;> cases h + have hc' : capA st s' (cslotAddrA sl) = .IrqHandler irq := (hoth _ hOSx ne).trans hc + have e : objects.finalise_cap_object st ar s' a' (.IrqHandler irq) true = + ok ((capability.Cap.Null, capability.Cap.IrqHandler irq), s', a') := rfl + have hmem : ((Fl4SpecA64.Cap.nullCap, absCapA (.IrqHandler irq)), σ') ∈ + (fl4FinaliseCap (Z := Unit) ac true σ).1 := by + rw [eac] + simp only [absCapA] + rw [fl4FinaliseCap_plainA (by intro h; exact h)] + exact mem_bind_ndD step rfl + rw [e, WP.spec_ok] + have p := post s' a' σ' G' .Null D' hc' W' + refine ⟨rfl, (by first | trivial | exact Or.inr ⟨_, rfl⟩), hwf, σ', G', ⟨true, hfin, hmem⟩, D', hc', + fun y hy _ => ?_, p.1, p.2, fun _ _ _ h => by cases h⟩ + by_cases ey : y = x + · refine Or.inr ⟨ey ▸ hx, ?_⟩ + rw [ey] + rintro ⟨_, _, _, h⟩ + rw [h] at hxc + rcases hxc with h | ⟨_, _, _, _, h⟩ <;> cases h + · exact Or.inl (hoth y hy ey) + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelIrqPostA.lean b/proofs/Fl4ProofsA64/P3/DelIrqPostA.lean new file mode 100644 index 000000000..4c372069b --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelIrqPostA.lean @@ -0,0 +1,174 @@ +/- +P3 (AArch64, delete hooks, contract §64 item 1): **the IRQ cleanup `PostDelHypA`** at `W ∧ DelPpTimeA`. + +`objects::post_cap_deletion (IrqHandler i)` is `interrupt::deleted_irq_handler`: `set_irq_state i IRQ_INACTIVE` +(`ks_set` of `IRQ_STATE[i]`, `mask_irq i true`) and `irq_free i`. The spec side is `setIrqState .irqInactive` (the +IRQ state at `i` and the machine's IRQ mask). The memory side is `JStage3TA.irqWriteA` (`P3/DelIrqWriteA`); the arch +side keeps the arch relation and the clock relation by `DelArchA`'s `arIrqMask` / `arIrqFree` / `clockIrqMask` / +`clockIrqFree` (the two time fields are untouched by the IRQ mask, `clockFr`). + +* **`setIrqInactive_refinesA`**: the concrete IRQ-state write and its spec step. +* **`postDelHookA`**: `PostDelHypA` at `W = DelJWQA s0 (DelXA6 …) ∧ DelPpTimeA`. + +Named premises: [P] `DelArchA` (its IRQ fields). No new premise. New module. +-/ +import Fl4ProofsA64.P3.DelIrqWriteA +import Fl4ProofsA64.P3.DelPpA +import Fl4ProofsA64.P3.IrqInvokePerformA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {m : Fl4SpecA64.McState} +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-- The spec's IRQ state after `setIrqState .irqInactive irq`. -/ +def irqInactiveStA (σ : State Unit) (irq : U64) : Irq → IrqState := + Function.update σ.interruptStates (BitVec.ofNat 13 irq.val) .irqInactive + +/-- The spec's machine state after `maskInterrupt true irq`. -/ +def irqMaskedMsA (ms : MachineState) (irq : U64) : MachineState := + { ms with irqMasks := Function.update ms.irqMasks (BitVec.ofNat 13 irq.val) true } + +/-- The spec state after the cleanup: the IRQ state at `irq` inactive and its mask set. -/ +def irqPostStA (σ : State Unit) (irq : U64) : State Unit := + { σ with interruptStates := irqInactiveStA σ irq, machineState := irqMaskedMsA σ.machineState irq } + +/-- **The IRQ-state write of the cleanup, and its spec step.** -/ +theorem setIrqInactive_specA (σ : State Unit) (irq : U64) : + ((), irqPostStA σ irq) ∈ (postCapDeletion (Z := Unit) (absCapA (.IrqHandler irq)) σ).1 := by + show ((), irqPostStA σ irq) ∈ (setIrqState (Z := Unit) .irqInactive (BitVec.ofNat 13 irq.val) σ).1 + unfold setIrqState + rw [nd_modify_bindA] + have hm : ((), irqMaskedMsA σ.machineState irq) ∈ + (maskInterrupt true (BitVec.ofNat 13 irq.val) σ.machineState).1 := by + unfold maskInterrupt irqMaskedMsA + exact rfl + simp only [decide_true] + exact irqMachine_memA _ { σ with interruptStates := irqInactiveStA σ irq } hm + +variable {ar : machine.Arch A} + +/-- **The concrete cleanup of an IRQ handler** (module docstring). -/ +theorem deletedIrqHandler_refinesA {AR : A → HwRootA → Prop} {Clock : A → MachineState → Prop} + (Da : DelArchA st ar AR Clock) {hw : HwRootA} (s : S) (a : A) (σ : State Unit) (irq : U64) (hi : irq.val < 5376) + (h : IrqRelA (L.mem s) (L.ksb s) σ) (hAR : AR a hw) (hCl : Clock a σ.machineState) + (hf : (L.ksb s).val + ksSizeA ≤ 2 ^ 64) (hal' : ∀ j : U64, j.val < 5376 → Aligned (irqWordA (L.ksb s) j)) : + interrupt.deleted_irq_handler st ar s a irq ⦃ r => + r.1 = L.wr s (irqWordA (L.ksb s) irq) 0#u64 ∧ AR r.2 hw ∧ Clock r.2 (irqMaskedMsA σ.machineState irq) ∧ + IrqRelA (L.mem r.1) (L.ksb r.1) (irqPostStA σ irq) ⦄ := by + have hs : interrupt.state_off irq = ok (irqOffA irq) := by + unfold interrupt.state_off + obtain ⟨v, hv, e⟩ := spec_existsA P0.Consts.layout.ks.IRQ_STATE.spec + subst v + simp only [lift, bind_ok, hv, irqOffA] + have h0 : (interrupt.IRQ_INACTIVE : U64) = 0#u64 := by rw [interrupt.IRQ_INACTIVE] + have hset : interrupt.set_irq_state st ar s a interrupt.IRQ_INACTIVE irq ⦃ r => + r.1 = L.wr s (wa (L.ksb s) (irqOffA irq)) interrupt.IRQ_INACTIVE ∧ AR r.2 hw ∧ Clock r.2 σ.machineState ⦄ := by + unfold interrupt.set_irq_state + simp only [hs, bind_ok] + refine WP.spec_bind (obj.ks_set.spec s (irqOffA irq) interrupt.IRQ_INACTIVE) ?_ + rintro s1 rfl + refine WP.spec_bind (WP.spec_and (Da.arIrqMask a hw irq _ hAR) (Da.clockIrqMask a σ.machineState irq _ hCl)) ?_ + rintro a1 ⟨hA1, hC1⟩ + exact (WP.spec_ok _).2 ⟨rfl, hA1, hC1⟩ + unfold interrupt.deleted_irq_handler + refine WP.spec_bind hset ?_ + rintro ⟨s1, a1⟩ ⟨rfl, hA1, hC1⟩ + refine WP.spec_bind (WP.spec_and (Da.arIrqFree a1 hw irq hA1) (Da.clockIrqFree a1 σ.machineState irq hC1)) ?_ + rintro a2 ⟨hA2, hC2⟩ + refine (WP.spec_ok _).2 ⟨by rw [h0]; rfl, hA2, Da.clockFr _ _ _ hC2 rfl rfl, ?_⟩ + have hal := hal' irq hi + have hk : L.ksb (L.wr s (irqWordA (L.ksb s) irq) interrupt.IRQ_INACTIVE) = L.ksb s := + (writesIn_wr (st := st) (v := interrupt.IRQ_INACTIVE) (s := s) hal).1 + change IrqRelA (L.mem (L.wr s (irqWordA (L.ksb s) irq) interrupt.IRQ_INACTIVE)) + (L.ksb (L.wr s (irqWordA (L.ksb s) irq) interrupt.IRQ_INACTIVE)) (irqPostStA σ irq) + rw [hk] + constructor + · intro j hj + rw [L.mem_wr _ _ _ _ hal (hal' j hj)] + change irqInactiveStA σ irq (BitVec.ofNat 13 j.val) = _ + unfold irqInactiveStA + by_cases e : j = irq + · subst e; simp [irqStOfA, h0] <;> exact Function.update_self .. + · have en : irqWordA (L.ksb s) j ≠ irqWordA (L.ksb s) irq := by + intro eq + apply e + apply UScalar.eq_of_val_eq + have q := congrArg (fun u : U64 => u.val) eq + rw [irqWordA_val hf hj, irqWordA_val hf hi] at q + omega + have ei : BitVec.ofNat 13 j.val ≠ BitVec.ofNat 13 irq.val := by + intro eq + apply e + apply UScalar.eq_of_val_eq + have q := congrArg BitVec.toNat eq + have h1 : j.val < 2 ^ 13 := by omega + have h2 : irq.val < 2 ^ 13 := by omega + change j.val % 2 ^ 13 = irq.val % 2 ^ 13 at q + rwa [Nat.mod_eq_of_lt h1, Nat.mod_eq_of_lt h2] at q + exact (Function.update_of_ne ei _ _).trans (by rw [if_neg (Ne.symm en)]; exact h.st j hj) + · exact h.node + · exact h.nodeHi + +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {κ : U64 → U64} {n : Nat} {J7 : JSlotA S} {Clock : A → MachineState → Prop} + +set_option maxHeartbeats 2000000 in +/-- **`PostDelHypA` for the deletion loop** (module docstring), with `Φ`. -/ +theorem postDelHookPA [J7PpA st J7] [J7PpKA st J7] {Φ : Set (U64 × Fl4SpecA64.Word) → S → State Unit → Prop} + (hΦ : KsKeepPA st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n J7 Φ) (Da : DelArchA st ar AR Clock) (s0 : S) : + PostDelHypA st ar Z.th OS + (fun s a σ G => DelJWQA st s0 (DelXPA st Φ AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) J7) s a σ G ∧ + DelPpTimeA Clock a σ) := by + intro s a σ G cl D W hcl hnn hwf hno + have hirq : ∃ irq, cl = .IrqHandler irq := by + cases cl <;> first | exact ⟨_, rfl⟩ | exact absurd rfl hnn | (simp only [CleanCapA] at hcl) + obtain ⟨irq, rfl⟩ := hirq + obtain ⟨⟨hkc, ⟨Q, R, hw, Ac, Bf, stack, J, X⟩, hPA, -, hBH, hCt, hFx⟩, hT⟩ := W + have hi : irq.val < 5376 := hwf + have hno' := hno irq rfl + have hirqJ : IrqRelA (L.mem s) (L.ksb s) σ := J.j.1.irq + have hAR : AR a hw := J.j.1.ar + have hf := J.sr.ksFit + have hal' : ∀ j : U64, j.val < 5376 → Aligned (irqWordA (L.ksb s) j) := fun j hj => by + have := irqWordA_val hf hj + have := J.sr.toSchedSRN.ksb_al + show _ % 8 = 0 + omega + have e : objects.post_cap_deletion st ar s a (.IrqHandler irq) = interrupt.deleted_irq_handler st ar s a irq := by + unfold objects.post_cap_deletion; rfl + rw [e] + refine WP.spec_mono (deletedIrqHandler_refinesA Da s a σ irq hi hirqJ hAR hT.2.1 hf hal') ?_ + rintro r ⟨hr1, hA2, hC2, hIrq2⟩ + have w : WritesIn st s r.1 (fun b => b = irqWordA (L.ksb s) irq) := by + rw [hr1]; exact writesIn_wr (hal' irq hi) + obtain ⟨J', osk⟩ := J.irqWriteA hi w hno' (U' := irqInactiveStA σ irq) (ms := irqMaskedMsA σ.machineState irq) rfl hIrq2 hA2 + have X' : Φ Bf r.1 (irqPostStA σ irq) := hΦ J X D.sub osk w.1 (fun b hb off => w.mem_eq hb (fun e => off (by + have v := irqWordA_val hf hi + have ksz : ksSizeA = 788736 := rfl + rw [e, v]; omega))) rfl rfl + have D' := DelXA.stepPpPA J' X' D osk w.1 hPA hBH hCt hFx + exact ⟨fun y hy => osk.capA hy, _, setIrqInactive_specA σ irq, D'.1, + ⟨⟨hkc.writes w, D'.2⟩, hT.1, hC2, hT.2.2.1, hT.2.2.2⟩⟩ + +/-- **`PostDelHypA` for the deletion loop** (module docstring). -/ +theorem postDelHookA {R7 : Set U64 → JSlotA S} [J7PpA st (J7LA st R7)] [J7PpKA st (J7LA st R7)] + (Da : DelArchA st ar AR Clock) (s0 : S) : + PostDelHypA st ar Z.th OS + (fun s a σ G => DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st R7)) s a σ G ∧ + DelPpTimeA Clock a σ) := + postDelHookPA ksKeep_std6A Da s0 + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelIrqWrA.lean b/proofs/Fl4ProofsA64/P3/DelIrqWrA.lean new file mode 100644 index 000000000..fa4edbef7 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelIrqWrA.lean @@ -0,0 +1,502 @@ +/- +P3 (AArch64, delete hooks, contract §64 item 1): **`JStage3TA` across a write of one IRQ-state word**. + +`post_cap_deletion (IrqHandler i)` writes `IRQ_STATE[i]` (`ks_set` of `irqWordA`) and changes the spec's +`interrupt_states` at `i`. The off-lemmas `b ≠ irqWordA` are the single-word counterparts of the scheduler step's +(`P3/Stage3SchedWrTA`); `JStage3TA.of_irqWr` is a scripted copy of `of_schedWr` (`handoff/tools/gen_irqwr.py`) over +that write set, with the IRQ relation at the new state a hypothesis (`JAtZAX.of_schedI`, `P3/DelIrqFrA`). +No named premise. New module. +-/ +import Fl4ProofsA64.P3.DelIrqFrA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +section offIrqA +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} [J7PpA st J7] {s : S} {a : A} {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) +include J + +/-- **A heap word off the kernel globals is no IRQ-state word.** -/ +theorem JStage3TA.omega_offIrqA {i : U64} (hi : i.val < 5376) {b : U64} + (hg : ∀ o : U64, o.val < 223488 → wa (L.ksb s) o ≠ b) : b ≠ irqWordA (L.ksb s) i := by + intro e + refine hg (irqOffA i) ?_ (by rw [e]; rfl) + have := irqOffA_val hi + omega + +/-- **The VSpace, Stage-2 and unmapped-table words are no IRQ-state word.** -/ +theorem JStage3TA.vsr_offIrqA {i : U64} (hi : i.val < 5376) {Pt Ub : Set U64} + (vr : VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt) {b : U64} (h : VsR (L.ksb s) Pt b) : + b ≠ irqWordA (L.ksb s) i := + J.omega_offIrqA hi (vr.ks b h) + +/-- **An `OS` slot word is no IRQ-state word** (`KsBridge.slotKs`). -/ +theorem JStage3TA.os_offIrqA {i : U64} (hi : i.val < 5376) {b : U64} (h : InOS OS b) : + b ≠ irqWordA (L.ksb s) i := by + intro e + obtain ⟨_, _, -, arm, -, -⟩ := J7PpA.core (st := st) _ _ _ _ _ _ J.j7 + obtain ⟨y, hy, y1, y2⟩ := h + have hv := irqWordA_val J.sr.ksFit hi + have g := (arm.kb.slotKs y hy).1 + rw [e] at y1 y2 + omega + +/-- `OSKeep` across a write of one IRQ-state word. -/ +theorem JStage3TA.osKeep_irqA {i : U64} (hi : i.val < 5376) {s' : S} + (w : WritesIn st s s' (fun b => b = irqWordA (L.ksb s) i)) : OSKeep st OS s s' := by + intro y hy + have hfit := J.ent.os.fit y hy + have al0 := J.ent.os.al0 hy + have key : ∀ o : U64, o.val < 32 → o.val % 8 = 0 → L.mem s' (wa y o) = L.mem s (wa y o) := by + intro o o1 o2 + have hv : (wa y o).val = y.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have hal : Aligned (wa y o) := J.ent.os.al hy o1 o2 + exact w.mem_eq hal (J.os_offIrqA hi ⟨y, hy, by omega, by omega⟩) + have e0 : wa y 0#u64 = y := by apply UScalar.eq_of_val_eq; rw [wa_valm]; simp + exact ⟨by have := key 0#u64 (by simp) (by simp); rwa [e0] at this, key 8#u64 (by simp) (by simp), + key 16#u64 (by simp) (by simp), key 24#u64 (by simp) (by simp)⟩ + +/-- **A word inside a slot of the typed heap is no IRQ-state word.** -/ +theorem JStage3TA.slot_offIrqA {i : U64} (hi : i.val < 5376) {y b : U64} (hy : IsSlotA Z.th y) + (y1 : y.val ≤ b.val) (y2 : b.val < y.val + 32) : b ≠ irqWordA (L.ksb s) i := by + intro e + have P := J.sr.place + have obj : ∃ p k, Z.th p = some k ∧ p.val ≤ y.val ∧ y.val + 32 ≤ p.val + k.size ∧ + (k = .tcb → y.val + 32 ≤ p.val + 224) := by + obtain ⟨p, ⟨r, hr, i, hi, rfl⟩ | ⟨ht, i, hi, rfl⟩⟩ := hy + · obtain ⟨k, hk, hs, hv⟩ := slotVA_of P hr (j := i) (by rw [cnode_size]; omega) + exact ⟨p, k, hk, by omega, by omega, fun e => by + rw [e, hr] at hk; cases hk⟩ + · obtain ⟨k, hk, hs, hv⟩ := tcb_slotV P ht hi + have : i < 7 := hi + exact ⟨p, k, hk, by omega, by omega, fun _ => by omega⟩ + obtain ⟨p, k, hk, p1, p2, pt⟩ := obj + have hv := irqWordA_val J.sr.ksFit hi + have hksz : ksSizeA = 788736 := rfl + rw [e] at y1 y2 + by_cases hn : IrqNodeA (L.ksb s) p + · have ek := P.irqK hk hn + subst ek + obtain ⟨j, hj, rfl⟩ := hn + have ev := irqSlotA_val J.sr.ksFit hj + have : ObjKindA.size (.cnode 0) = 32 := rfl + omega + · have := P.ks p k hk hn + omega + +/-- **A word of a TCB of `Z.T` at a VCPU link is no IRQ-state word.** -/ +theorem JStage3TA.link_offIrqA {i : U64} (hi : i.val < 5376) {t K : U64} (ht : t ∈ Z.T) + (hK : K = 3664#u64 ∨ K = 3672#u64) : wa t K ≠ irqWordA (L.ksb s) i := by + intro e + have hfit := (J.sr.core.tcbs t ht).fits + have hKv : K.val = 3664 ∨ K.val = 3672 := by rcases hK with rfl | rfl <;> simp + have hv : (wa t K).val = t.val + K.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have hiw := irqWordA_val J.sr.ksFit hi + have := J.sr.tcb_ks ht + have hksz : ksSizeA = 788736 := rfl + rw [e] at hv + omega + +/-- **An ASID-table word is no IRQ-state word.** -/ +theorem JStage3TA.asid_offIrqA {i : U64} (hi : i.val < 5376) {b : U64} (h1 : (L.ksb s).val + 772352 ≤ b.val) + (h2 : b.val < (L.ksb s).val + 773376) : b ≠ irqWordA (L.ksb s) i := by + intro e + have hv := irqWordA_val J.sr.ksFit hi + rw [e] at h1 h2 + omega + +/-- (record edit #8b-B1) **A domain-schedule word is no IRQ-state word.** -/ +theorem JStage3TA.dom_offIrqA {i : U64} (hi : i.val < 5376) {b : U64} (hd : DomW (L.ksb s) b) : + b ≠ irqWordA (L.ksb s) i := by + intro e + have hv := irqWordA_val J.sr.ksFit hi + rw [e] at hd + unfold DomW at hd + omega + +/-- (record edit B2) **An IPI-pair word is no IRQ-state word.** -/ +theorem JStage3TA.ipi_offIrqA {i : U64} (hi : i.val < 5376) {o : U64} (ho : o.val = 120 ∨ o.val = 128) : + L.nodeAt s (L.cpu s) o ≠ irqWordA (L.ksb s) i := by + intro e + have hv := irqWordA_val J.sr.ksFit hi + obtain ⟨hv2, hcl⟩ := J.sr.toSchedSRN.ipi_val ho + have := congrArg UScalar.val e + omega + +end offIrqA + +set_option maxHeartbeats 1000000 in +/-- **`JStage3TA` across a write of one IRQ-state word** (module docstring). -/ +theorem JStage3TA.of_irqWr {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J5 J7 : JSlotA S} [J7PpA st J7] {s s' : S} + {a a' : A} {σ σ' : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {Q : U64 → U64 → List U64} {Rl : U64 → List U64} {curOf curScOf : Fl4SpecA64.CoreId → Fl4SpecA64.ObjRef} + {i : U64} (hi : i.val < 5376) (w : WritesIn st s s' (fun b => b = irqWordA (L.ksb s) i)) + (HS : SpecSchedIA σ σ') (SSz : SSzA σ σ') (Qk : QKeepA σ σ') (hirq : IrqRelA (L.mem s') (L.ksb s') σ') + (hum : σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory) (har : AR a' Z.hw) + (hcurOf : ∀ k, k ≠ m.curCore → curOf k = Z.curOf k) (bufAl : UtBufAlA Z.T σ') + (ent : EntryInvA3 st H Z.Cs OS U Ac Pp Tb Lv s') + (sq : SchedQueuesInv st H.tcbs Z.Cs H.omega H s' Q Rl) + (poolOff : PoolOff st H Z.Cs OS s') + (sr : SchedSRA st { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } m κ s' σ') + (scCov : ScCovA Z.C m σ') + (ep : EpStateRel Z.T E Rp Bf (L.mem s') σ') + (ntfn : NtfnStateRel N Z.T (L.mem s') σ') + (reply : ReplyStackRelA (L.mem s') Rp Z.C σ' (scNameA m) stack) + (bufCover : BufCover st Z.T Bf s' σ') + (idleNoFault : IdleNoFaultA m σ') + (mcp : McpRelA Z.T m (L.mem s') σ') + (yieldRel : YieldRelA Z.T Z.C m (L.mem s') σ') + (sinv : SInvXA σ' m n) + (sidle : SIdleA σ' m n) + (runVcpu : RunVcpuInv st Z.T s') (hvr : ∀ sc ∈ Z.C, ∀ r ∈ stack sc, RBlk st s' r) + (noSwitch : ∀ u, σ'.schedulerAction ≠ .switchThread u) + (sact : SActA σ') + (blkQ : BlkQueuedA Z.T E N (L.mem s') σ') + (j5 : J5 { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } H OS s' σ' m) + (j7 : J7 { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } H OS s' σ' m) : + JStage3TA st AR { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } H OS U Ac Pp Tb Lv ia ie N E Rp + Bf stack κ n J5 J7 s' a' σ' m := by + have hk := w.1 + have hc := w.2.1 + have osk := J.osKeep_irqA hi w + have capE : ∀ u ∈ OS, capA st s' u = capA st s u := fun u hu => by + unfold capA; rw [(osk u hu).1, (osk u hu).2.1] + have keep : ∀ b, Aligned b → b ≠ irqWordA (L.ksb s) i → L.mem s' b = L.mem s b := fun b hb hn => + w.mem_eq hb hn + obtain ⟨Pt, Ub, vr, arm, link, apart⟩ := J7PpA.core (st := st) _ _ _ _ _ _ J.j7 + have P := J.sr.place + have hT : ∀ t ∈ Z.T, t ∈ H.tcbs := fun t ht => by rw [J.tcbs]; exact ht + -- the page words + have page : ∀ p, SchedPageA st Z OS s p → ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → ¬ AsW (L.ksb s) (wa p o) → + L.mem s' (wa p o) = L.mem s (wa p o) := fun p hp o o1 o2 na => by + have hv : VsR (L.ksb s) Pt (wa p o) := Or.inl ⟨p, link p hp, o, o1, o2, rfl⟩ + exact keep _ (vr.al _ hv) (J.vsr_offIrqA hi vr hv) + have vsr : ∀ b, VsR (L.ksb s) Pt b → L.mem s' b = L.mem s b := fun b hb => by + refine keep _ (vr.al _ hb) ?_ + by_cases h : AsW (L.ksb s) b + · obtain ⟨o, o1, o2, -, e⟩ := h + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.ent.ksw + have ev : b.val = (L.ksb s).val + o.val := by + rw [e, wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact J.asid_offIrqA hi (by omega) (by omega) + · exact J.vsr_offIrqA hi vr hb + have vsfoot : ∀ b, VsFootA Z.Pp Z.S1 Z.Y (L.ksb s) b → L.mem s' b = L.mem s b := fun b hb => by + refine vsr b ?_ + rcases hb.vsR with ⟨p, hp, h⟩ | h + · refine Or.inl ⟨p, link p ?_, h⟩ + rcases hp with hp | hp | hp + · exact Or.inl hp + · exact Or.inr (Or.inl hp) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl hp)))))) + · exact Or.inr h + have word : ∀ p, SchedPageA st Z OS s p → ∀ i : U64, i.val < 512 → L.mem s' (peAF p i) = L.mem s (peAF p i) := + fun p hp i hi => by + refine vsr _ (Or.inl ⟨p, link p hp, core.num.U64.wrapping_mul i 8#u64, ?_, ?_, rfl⟩) + · rw [wm8_valA hi]; omega + · rw [wm8_valA hi]; omega + have extfoot : ∀ b, ExtFootE Z.X Z.Y Z.T2 b → L.mem s' b = L.mem s b := fun b hb => by + cases hb with + | s2lo r i hr hi => exact word r (Or.inr (Or.inr (Or.inr (Or.inl hr)))) i hi + | s2hi r i hr hi => exact word _ (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨r, hr, rfl⟩))))) i hi + | s2pt t i ht hi => exact word t (Or.inr (Or.inr (Or.inl ht))) i hi + | wl0 y hy => exact word y (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl hy)))))) _ (by simp) + | wlo y i hy hi => exact word y (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl hy)))))) i (by omega) + -- the VCPU words + have vcw : ∀ v ∈ Z.V, ∀ o : U64, o.val = 0 ∨ o.val = 24 → L.mem s' (wa v o) = L.mem s (wa v o) := by + intro v hv o ho + have hH := arm.vh v hv + have hfit := J.ent.heap.fits v _ hH + have hal := J.ent.heap.align v _ hH + simp only [HKind.bits] at hfit hal + have ev : (wa v o).val = v.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have al : Aligned (wa v o) := by show _ % 8 = 0; rw [ev]; omega + have inObj : InObj v (.other 12) (wa v o) := by + unfold InObj; simp only [HKind.bits]; omega + refine keep _ al (J.omega_offIrqA hi ?_) + intro g hg e + exact apart _ ⟨v, hv, o, by omega, by omega, rfl⟩ g hg e + have slotw : ∀ y, IsSlotA Z.th y → ∀ x, (x = y ∨ x = wa y 8#u64 ∨ x = wa y 16#u64 ∨ x = wa y 24#u64) → + L.mem s' x = L.mem s x := by + intro y hy x hx + obtain ⟨a32, f32⟩ := hy.al32 P + have hv : ∀ o : U64, o.val < 32 → (wa y o).val = y.val + o.val := fun o ho => by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have key : x.val % 8 = 0 ∧ y.val ≤ x.val ∧ x.val < y.val + 32 := by + rcases hx with rfl | rfl | rfl | rfl + · omega + all_goals + rw [hv _ (by simp)]; simp; omega + exact keep _ key.1 (J.slot_offIrqA hi hy key.2.1 key.2.2) + have cnw : ∀ y, SlotWA Z.th y → IsSlotA Z.th y := fun y hy => by + rcases hy with h | h + · exact h + · obtain ⟨p, r, hp, idx, hl, rfl⟩ := h + obtain ⟨e, hb, -⟩ := cn_slotV P hp hl + exact ⟨p, Or.inl ⟨r, hp, blvA idx, hb, e⟩⟩ + have tcbl : ∀ t ∈ Z.T, ∀ K : U64, K = 3664#u64 ∨ K = 3672#u64 → L.mem s' (wa t K) = L.mem s (wa t K) := by + intro t ht K hK + have hta := J.sr.core.tcbs t ht + refine keep _ (field_aligned (K := K) hta ?_ ?_) (J.link_offIrqA hi ht hK) + all_goals rcases hK with rfl | rfl <;> simp + have mem : SchedKeepMemIA (st := st) Z s s' := + { ksb := hk + cpu := hc + slot := fun y hy x hx => slotw y (cnw y hy) x hx + tcbW := fun t ht => ⟨tcbl t ht _ (Or.inl rfl), tcbl t ht _ (Or.inr rfl)⟩ + vcW := fun v hv => by + have e0 : wa v 0#u64 = v := by apply UScalar.eq_of_val_eq; rw [wa_valm]; simp + have h0 := vcw v hv 0#u64 (Or.inl (by simp)) + rw [e0] at h0 + exact ⟨h0, vcw v hv _ (Or.inr (by simp))⟩ + vs := vsfoot + ext := extfoot } + -- the node cursors + have nodes : ∀ c ∈ Z.Cs, + L.mem s' (L.nodeAt s' c layout.node.IDLE_THREAD) ∈ Z.T ∧ L.mem s' (L.nodeAt s' c layout.node.IDLE_SC) ∈ Z.C ∧ + L.mem s' (L.nodeAt s' c layout.node.CUR_THREAD) ∈ Z.T := by + intro c hcs + refine ⟨?_, ?_, ?_⟩ + · have e : L.mem s' (L.nodeAt s' c layout.node.IDLE_THREAD) = L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) := + specPtr_inj ((sr.sch.idleOf c hcs).symm.trans (J.sr.sch.idleOf c hcs)) + rw [e]; exact J.j.1.idleT c hcs + · have e : L.mem s' (L.nodeAt s' c layout.node.IDLE_SC) = L.mem s (L.nodeAt s c layout.node.IDLE_SC) := + specPtr_inj ((sr.sch.idleScOf c hcs).symm.trans (J.sr.sch.idleScOf c hcs)) + rw [e]; exact J.j.1.idleC c hcs + · by_cases h : c = L.cpu s + · have := sr.curT + unfold LawfulStore.curW at this + rw [hc, ← h] at this + exact this + · have hne : c.val ≠ m.curCore := fun e => h (UScalar.eq_of_val_eq (e.trans J.sr.sch.core)) + have h1 := sr.sch.curOfW c hcs + rw [show ({ Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } : XSetsA).curOf = curOf from rfl, + hcurOf _ hne] at h1 + have e : L.mem s' (L.nodeAt s' c layout.node.CUR_THREAD) = L.mem s (L.nodeAt s c layout.node.CUR_THREAD) := + specPtr_inj (h1.symm.trans (J.sr.sch.curOfW c hcs)) + rw [e]; exact J.j.1.curT c hcs + have st' : StEqA Z.T (L.mem s') σ' := fun _ ht _ hx => (ep.at_tcb ht hx).st + have jx := JAtZAX.of_schedI J.j (a' := a') (Q := Q) (R := Rl) (curOf := curOf) (curScOf := curScOf) mem HS har hirq + (sr.sch.withSt st') sr.rest (fun c h => (nodes c h).1) (fun c h => (nodes c h).2.1) + (fun c h => (nodes c h).2.2) sr.cscAll + have free : ∀ dev x, UtFreeA st OS s' dev x → UtFreeA st OS s dev x := by + rintro dev x ⟨u, hu, q, b, fi, d, hcap, h⟩ + exact ⟨u, hu, q, b, fi, d, (capE u hu).symm.trans hcap, h⟩ + have dead : ∀ k p, DeadA st OS k s' p → DeadA st OS k s p := fun k p h u hu hp => by + have := h u hu + rw [capE u hu] at this + exact this hp + have nsc : ∀ {p : Fl4SpecA64.ObjRef}, σ.kheap p = none → σ'.kheap p = none := fun {p} h => by + rcases HS.kh p with e | ⟨x, _, e1, -⟩ | ⟨o, o', e1, -, -⟩ + · rw [e]; exact h + · rw [h] at e1; cases e1 + · rw [h] at e1; cases e1 + have keepE : ∀ {q : Fl4SpecA64.ObjRef} {e : Fl4SpecA64.ExtArchObj Fl4SpecA64.PTE}, + σ.kheap q = some (.archObj (.ext e)) → σ'.kheap q = some (.archObj (.ext e)) := fun h1 => + HS.keep h1 (fun _ e => by cases e) (fun e => by rcases e with ⟨_, _, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ <;> cases e) + have unmS2 : ∀ u ∈ OS, ∀ p a v, capA st s u = .Ept p 0#u64 false a v → UnmS2RootA st s σ p → + UnmS2RootA st s' σ' p := by + intro u hu p a v hc U + obtain ⟨es, h1, h2⟩ := U.spec + refine { inv := fun i hi => ?_, spec := ⟨es, keepE h1, h2⟩ } + by_cases hlo : i.val < 512 + · rw [word p (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inr (Or.inl ⟨a, v, hc⟩))⟩))))))) i hlo] + exact U.inv i hi + · obtain ⟨j, hj, e⟩ := peAF_hiA p i (by omega) hi + rw [e, word _ (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inr (Or.inr (Or.inl ⟨p, a, v, hc, rfl⟩)))⟩))))))) j hj, ← e] + exact U.inv i hi + have unmHv : ∀ u ∈ OS, ∀ y a, capA st s u = .HighVSpace y false a → UnmHiRootA st s σ y → + UnmHiRootA st s' σ' y := by + intro u hu y a hc U + obtain ⟨l1, h1, h2⟩ := U.spec + exact { inv := fun i hi => by + rw [word _ (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inr (Or.inr (Or.inr ⟨y, a, hc, rfl⟩)))⟩))))))) i hi] + exact U.inv i hi + spec := ⟨l1, keepE h1, h2⟩ } + have unm : ∀ ty p, SchedPageA st Z OS s p → UnmTabA st Z m s σ ty p → + UnmTabA st { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } m s' σ' ty p := by + intro ty p hp U1 + obtain ⟨tb, h1, h2, h3⟩ := U1.spec + exact + { inv := fun i hi => by rw [word p hp i hi]; exact U1.inv i hi + spec := ⟨tb, HS.keep h1 (fun _ e => by cases e) (fun e => by rcases e with ⟨_, _, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ <;> cases e), h2, h3⟩ + off := ⟨fun i hi h => U1.off.mem i hi (footA_schedA hk hc h), U1.off.obj⟩ } + exact + { ent := ent + j := jx + tcbs := J.tcbs + scs := J.scs + sq := sq + ap := by + have e : L.ksb s' = L.ksb s := hk + rw [e]; exact { J.ap with } + pp := J.pp + s1 := J.s1 + ko := by rw [hk]; exact J.ko + ptH := J.ptH + tabInj := by rw [HS.asid]; exact J.tabInj + poolOff := poolOff + objAlloc := ObjAllocA.keep (Z := Z) J.objAlloc osk + idleAnchor := IdleAnchorOffA.keep J.idleAnchor osk + vsVc := by rw [hk]; exact J.vsVc + sr := sr + scCov := scCov + scSize := fun c hcm x k h => by + obtain ⟨y, hy⟩ := SSz _ x k h + exact J.scSize c hcm y k hy + ep := ep + ntfn := ntfn + reply := reply + bufCover := bufCover + bufNative := fun t ht => by + have hsl : IsSlotA Z.th (slotAddrA (specPtr t) (BitVec.ofNat 64 2)) := + ⟨t, Or.inr ⟨(J.sr.thT t).2 ht, 2, by unfold tcbSlotsA; omega, rfl⟩⟩ + have e : capAtA (L.mem s') (slotAddrA (specPtr t) (BitVec.ofNat 64 2)) = + capAtA (L.mem s) (slotAddrA (specPtr t) (BitVec.ofNat 64 2)) := by + unfold capAtA + rw [slotw _ hsl _ (Or.inl rfl), slotw _ hsl _ (Or.inr (Or.inl rfl))] + rw [e]; exact J.bufNative t ht + iplaced := J.iplaced + ipcPlace := J.ipcPlace + idleNoFault := idleNoFault + mcp := mcp + yieldRel := yieldRel + sinv := sinv + setsInH := { J.setsInH with } + utIpc := J.utIpc + ipcTh := J.ipcTh + deadExt := + { vcpu := fun v hv hd x hx => J.deadExt.vcpu v hv (dead _ v hd) x + (HS.back hx (fun _ e => by cases e) (fun e => by rcases e with ⟨_, _, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ <;> cases e)) + reply := fun r hr hd => J.deadExt.reply r hr (dead _ r hd) + asid := fun i p h hd => J.deadExt.asid i p (by rw [HS.asid] at h; exact h) (dead _ p hd) + tvcpu := fun t ht hd => by + rw [tcbl t ht 3664#u64 (Or.inl rfl)] + exact J.deadExt.tvcpu t ht (dead _ t hd) + vcDeadRun := fun v hv hd => by + rw [vcw v hv 24#u64 (Or.inr (by simp))] + exact J.deadExt.vcDeadRun v hv (dead _ v hd) + replyTcb := fun r hr hd x hx => by + obtain ⟨y, hy, k⟩ := Qk.rt _ x hx + exact k (J.deadExt.replyTcb r hr (dead _ r hd) y hy) } + specFree := + { heap := fun p h => nsc (J.specFree.heap p (free _ _ h)) + cdt := fun x y h p hf => J.specFree.cdt x y (by rw [HS.cdt] at h; exact h) p (free _ _ hf) + orig := fun x h p hf => J.specFree.orig x (by rw [HS.orig] at h; exact h) p (free _ _ hf) } + utBlk := fun u hu q b fi d h => J.utBlk u hu q b fi d ((capE u hu).symm.trans h) + unmPt := + { pt := fun u hu p asid va h => by + have h0 := (capE u hu).symm.trans h + exact unm _ p (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inl ⟨_, _, _, h0⟩⟩))))))) (J.unmPt.pt u hu p asid va h0) + vs := fun u hu p asid h => by + have h0 := (capE u hu).symm.trans h + exact unm _ p (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inl ⟨_, _, h0⟩)⟩))))))) (J.unmPt.vs u hu p asid h0) + s2 := fun u hu p asid va h => by + have h0 := (capE u hu).symm.trans h + exact unmS2 u hu p asid va h0 (J.unmPt.s2 u hu p asid va h0) + hv := fun u hu p asid h => by + have h0 := (capE u hu).symm.trans h + exact unmHv u hu p asid h0 (J.unmPt.hv u hu p asid h0) } + tabS1 := fun q hq => + ⟨fun i hi h => (J.tabS1 q hq).mem i hi (footA_schedA hk hc h), (J.tabS1 q hq).obj⟩ + tabExt := + ⟨fun q hq i hi h => J.tabExt.mem q hq i hi (by + exact footA_schedA (Z := extZA Z) hk hc h), J.tabExt.obj⟩ + specZero := fun x h => by rw [hum]; exact J.specZero x (free _ _ h) + origLive := fun x h => J.origLive x (by rw [HS.orig] at h; exact h) + nullOrig := fun sl h => by + rw [HS.orig] + exact J.nullOrig sl (by rw [← SpecSchedIA.specCapAt HS sl]; exact h) + thPos := fun p z h => J.thPos p z h + frBase := J.frBase.keepA capE + devFr := J.devFr.keepA capE fun p dev pg h => HS.keep h (fun _ e => by cases e) + (fun e => by rcases e with ⟨_, _, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ <;> cases e) + finCov := J.finCov.keepA + (fun q r cs h => HS.back h (fun _ e => by cases e) + (fun e => by rcases e with ⟨_, _, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ <;> cases e)) + (fun q x h => HS.tcbBack h) + idleAll := J.idleAll.keepA + (fun c hcs => specPtr_inj ((sr.sch.idleOf c hcs).symm.trans (J.sr.sch.idleOf c hcs))) + (fun c hcs => specPtr_inj ((sr.sch.idleScOf c hcs).symm.trans (J.sr.sch.idleScOf c hcs))) + ipcHeapCov := J.ipcHeapCov + zombieNoVcpu := J.zombieNoVcpu.keepA capE J.zombieTcb + fun t ht => tcbl t (J.tcbs ▸ ht) 3664#u64 (Or.inl rfl) + zombieTcb := J.zombieTcb.keepA capE + bufOff := J.bufOff.keepA (Z := Z) (Z' := { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf }) + hk rfl rfl rfl rfl rfl rfl rfl capE + vsOk := by + have e := (utVsPt_keepA J.ent.vs hk (fun b hb => by + refine keep b (J.vsOk.al b hb) ?_ + by_cases h : AsW (L.ksb s) b + · obtain ⟨o, o1, o2, -, e⟩ := h + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.ent.ksw + have ev : b.val = (L.ksb s).val + o.val := by + rw [e, wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact J.asid_offIrqA hi (by omega) (by omega) + · exact J.vsr_offIrqA hi J.vsOk hb)).2 + unfold UtVsOkA + rw [show L.ksb s' = L.ksb s from hk, e] + exact J.vsOk + domOK := J.domOK.keepS J.sr.toSchedSRN Qk.dom hk hc (fun b hb hd => w.mem_eq hb (J.dom_offIrqA hi hd)) + ipiZero := J.ipiKeepOf w fun o ho => J.ipi_offIrqA hi ho + bufVs := by + have e := (utVsPt_keepA J.ent.vs hk (fun b hb => by + refine keep b (J.vsOk.al b hb) ?_ + by_cases h : AsW (L.ksb s) b + · obtain ⟨o, o1, o2, -, e⟩ := h + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.ent.ksw + have ev : b.val = (L.ksb s).val + o.val := by + rw [e, wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact J.asid_offIrqA hi (by omega) (by omega) + · exact J.vsr_offIrqA hi J.vsOk hb)).2 + rw [show L.ksb s' = L.ksb s from hk, e] + exact J.bufVs + bufAl := bufAl + zombieBnd := J.zombieBnd.keepA capE + devUtOff := J.devUtOff.keepA (Z := Z) capE hk (fun _ _ h => h) rfl + sidle := sidle + capTy := J.capTy.keepA capE + scCpu := J.scCpu + scNZ := J.scNZ + vcRun := J.vcRun.keepA (fun t ht => tcbl t ht 3664#u64 (Or.inl rfl)) (fun v hv => vcw v hv 24#u64 (Or.inr (by simp))) + (fun v hv => by + have h := vcw v hv 0#u64 (Or.inl (by simp)) + rwa [wa_zeroVcA] at h) + (fun t ht => tcbl t ht 3672#u64 (Or.inr rfl)) + blkQ := blkQ + sact := sact + runVcpu := runVcpu + vr := hvr + ciSc := fun u hu p b h => J.ciSc u hu p b ((capE u hu).symm.trans h) + noSwitch := noSwitch + curVcpuNone := HS.vcur.trans J.curVcpuNone + ipcSorted := J.ipcSorted.of_q Qk + activeReplyScs := J.activeReplyScs.of_q Qk + j5 := j5 + j7 := j7 } + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelIrqWriteA.lean b/proofs/Fl4ProofsA64/P3/DelIrqWriteA.lean new file mode 100644 index 000000000..6da1ba9cb --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelIrqWriteA.lean @@ -0,0 +1,261 @@ +/- +P3 (AArch64, delete hooks, contract §64 item 1): **the per-step record across the IRQ cleanup's state write**. + +`JStage3TA.irqWriteA` is `Stage3PreemptTA`'s `workUnitsA` (a direct single-word transport, there the `WORK_UNITS` +global) at `IRQ_STATE[i]`: the write is off every word the record reads (`pp_rd_whereA`: node, TCB, SC, `other` heap +words and the two kernel globals `8`, `16`) and off the VSpace words; the spec side changes only the IRQ state at `i` +and the machine state, with the IRQ relation at the new state a hypothesis (`P3/DelIrqFrA`, `P3/DelIrqWrA`). +`HandlersActiveA` after the write: the IRQ `i` has no handler cap (the cleanup's premise, `hno`). + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.DelIrqWrA +import Fl4ProofsA64.P3.Stage3PreemptTA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J7 : JSlotA S} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- The VSpace words are kept by a write of one IRQ-state word. -/ +theorem JStage3TA.irq_vsrA {J5 : JSlotA S} [J7PpA st J7] + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) {i : U64} + (hi : i.val < 5376) {s' : S} (w : WritesIn st s s' (fun b => b = irqWordA (L.ksb s) i)) : + ∀ Pt Ub : Set U64, VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt → VsKeep st (L.ksb s) Pt s s' := by + intro Pt Ub vr b hb + exact w.mem_eq (vr.al _ hb) (J.vsr_offIrqA hi vr hb) + +set_option maxHeartbeats 2000000 in +/-- **`JStage3TA` across the write of `IRQ_STATE[i]` with the spec's IRQ state and machine state changed** (module +docstring). `hno`: no `OS` slot holds the handler of `i`. -/ +theorem JStage3TA.irqWriteA [J7PpA st J7] [J7PpKA st J7] + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + {i : U64} (hi : i.val < 5376) {s' : S} (w : WritesIn st s s' (fun b => b = irqWordA (L.ksb s) i)) + (hno : ∀ y ∈ OS, capA st s y ≠ .IrqHandler i) + {U' : Fl4SpecA64.Irq → Fl4SpecA64.IrqState} {ms : MachineState} + (hm : ms.underlyingMemory = σ.machineState.underlyingMemory) + (hirq : IrqRelA (L.mem s') (L.ksb s') { σ with interruptStates := U', machineState := ms }) + {a2 : A} (har : AR a2 Z.hw) : + JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s' a2 + { σ with interruptStates := U', machineState := ms } m ∧ OSKeep st OS s s' := by + obtain ⟨σ2, e2⟩ : ∃ σ2 : State Unit, σ2 = { σ with interruptStates := U', machineState := ms } := ⟨_, rfl⟩ + rw [← e2] at hirq ⊢ + have kh : σ2.kheap = σ.kheap := by rw [e2] + have um : σ2.machineState.underlyingMemory = σ.machineState.underlyingMemory := by rw [e2]; exact hm + obtain ⟨wu, ewu⟩ : ∃ wu, wu = irqWordA (L.ksb s) i := ⟨_, rfl⟩ + have hw : WritesIn st s s' (· = wu) := by rw [ewu]; exact w + have hblk := BXI.blk J.ent.bxi + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.ent.ksw + have wv : wu.val = (L.ksb s).val + 224 + 8 * i.val := by rw [ewu]; exact irqWordA_val J.sr.ksFit hi + have ha : Aligned wu := by + have := J.sr.toSchedSRN.ksb_al + show _ % 8 = 0 + omega + have hSg := hblk.2.2.2.2 + have hoff := irqOffA_val hi + have hΩ : wu ∈ H.omega := by + rw [ewu]; exact hSg.ksΩ (irqOffA i) (by omega) (by omega) + have hnS : wu ∉ H.frame := fun hs => by + have := hSg.ks _ hs + omega + have kb : L.ksb s' = L.ksb s := w.1 + have memk : ∀ b, Aligned b → b ≠ wu → L.mem s' b = L.mem s b := fun b hb ne => hw.mem_eq hb ne + have C := LvC.of_blk hblk + have blk2 : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s' := + (blk_of_capw hblk hw (fun b e => by subst e; exact Or.inr ⟨hΩ, hnS⟩)).1 + have hvw : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → Aligned b ∧ b ≠ wu := fun b hb => + ⟨J.vsOk.al b hb, fun e => J.vsr_offIrqA hi J.vsOk hb (e.trans ewu)⟩ + have hvsT := (utVsPt_keepA (s' := s') J.ent.vs kb (fun b hb => memk b (hvw b hb).1 (hvw b hb).2)).1 + -- heap objects other than `other` ones are off the state block + have offK : ∀ q k, H q = some k → (∀ y, k ≠ .other y) → ∀ b : U64, q.val ≤ b.val → b.val < q.val + 2 ^ k.bits → + b ≠ wu := by + intro q k hq ne b h1 h2 e + rw [e, wv] at h1 h2 + rcases J.ko.1 q k hq ne with g | g <;> omega + -- the write is off the record's words + have hW : ∀ b, b = wu → ¬ UtRdA st Z N E Rp Bf m s σ b := by + intro b e hrd + have hom := C.om wu hΩ + have tcbOff : ∀ t ∈ Z.T, ¬ (t.val ≤ b.val ∧ b.val < t.val + 4096) := by + intro t ht h + have htH : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + rw [e] at h + rcases hom.2.1 t htH with g | g <;> omega + cases pp_rd_whereA J hrd with + | tcb t ht h1 h2 => exact tcbOff t ht ⟨by omega, h2⟩ + | ibuf t ht h => + have g := pp_sbValA J ht + refine tcbOff t ht ?_ + rcases h with h | h <;> rw [h] <;> omega + | other q len hq h1 h2 => + have g := J.j.1.place.ks q _ hq (fun h => by cases J.j.1.place.irqK hq h) + simp only [ObjKindA.size, ksSizeA] at g + rw [e, wv] at h1 h2 + omega + | sc c y k hc hk h => + have hcH : c ∈ H.scs := by rw [J.scs]; exact hc + obtain ⟨z, hz⟩ := hcH + have hsz := (J.scSize c hc y k hk).1 + have eb : H.scBits c = z := by simp [Heap.scBits, hz] + rw [eb] at hsz + obtain ⟨g1, g2⟩ := h + refine offK c _ hz (fun _ h => by cases h) b g1 (lt_of_lt_of_le g2 ?_) e + exact Nat.add_le_add_left (Nat.pow_le_pow_right (by norm_num) hsz) _ + | node c o hc ho h => + have v' : b.val = L.nodeBase s c + o.val := by rw [h]; exact nodeAt_val (J.sq.nodes c hc) ho + have hc' : c ∈ Z.Cs := hc + rcases hom.2.2 c hc' with g | g <;> (rw [e] at v'; omega) + | glob o ho h => + have v' : b.val = (L.ksb s).val + o.val := by + rw [h, wa_valP]; exact Nat.mod_eq_of_lt (by omega) + rw [e, wv] at v' + omega + have H2 : UtCnStepIA σ σ2 := + ⟨by rw [e2]; exact ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩, + fun q => Or.inl (by rw [kh]), um⟩ + have HS : SpecSchedIA σ σ2 := + { kh := fun q => Or.inl (by rw [kh]) + irqN := by rw [e2] + asid := by rw [e2] + vcur := by rw [e2] + cdt := by rw [e2] + orig := by rw [e2] } + have SSz : SSzA σ σ2 := fun q y n h => ⟨y, by rw [← kh]; exact h⟩ + have osk : OSKeep st OS s s' := J.osKeep_irqA hi w + -- the scheduler queues + have sq2 : SchedQueuesInv st H.tcbs Z.Cs H.omega H s' Z.Q Z.R := by + have hom := C.om wu hΩ + refine utFr_sqA (Sg := H.frame) J.sq hw (fun y hy K h1 h2 h3 => ?_) (fun c hc o ho h8 => ?_) (fun b hb => ?_) + (fun q hq => ?_) + · have f := J.ent.heap.fits y .tcb hy + have al := J.ent.heap.align y .tcb hy + simp only [HKind.bits] at f al + have ev : (wa y K).val = y.val + K.val := by rw [wa_valP]; exact Nat.mod_eq_of_lt (by omega) + refine ⟨by unfold Aligned; rw [ev]; omega, fun e => ?_⟩ + have g := congrArg UScalar.val e + rw [ev] at g + rcases hom.2.1 y hy with k | k <;> omega + · refine ⟨nodeAt_aligned (J.sq.nodes c hc) ho h8, fun e => ?_⟩ + have g : (L.nodeAt s c o).val = L.nodeBase s c + o.val := nodeAt_val (J.sq.nodes c hc) ho + rw [e] at g + rcases hom.2.2 c hc with k | k <;> omega + · have g := C.sub b hb + exact ⟨(C.om b g).1, fun e => hnS (e ▸ hb)⟩ + · have hk : ∃ k, H q = some k ∧ (∀ y, k ≠ .other y) ∧ 4 ≤ k.bits := by + rcases hq with h | h + · exact ⟨_, h, fun _ g => (by cases g), by simp [HKind.bits]⟩ + · exact ⟨_, h, fun _ g => (by cases g), by simp [HKind.bits]⟩ + obtain ⟨k, hk, ne, b4⟩ := hk + have al := J.ent.heap.align q k hk + have f := J.ent.heap.fits q k hk + have h16 : 2 ^ 4 ≤ 2 ^ k.bits := Nat.pow_le_pow_right (by decide) b4 + have h8 : 8 ∣ q.val := by + have : 2 ^ 3 ∣ 2 ^ k.bits := Nat.pow_dvd_pow 2 (by omega) + exact (show (8 : Nat) ∣ 2 ^ k.bits from this).trans (Nat.dvd_of_mod_eq_zero al) + have eh : (epHeadA q).val = q.val := by simp [epHeadA] + have et : (epTSA q).val = q.val + 8 := by + unfold epTSA + have e8 : (layout.ep.TAIL_STATE : U64).val = 8 := by unfold layout.ep.TAIL_STATE; rfl + rw [wa_valP, e8] + exact Nat.mod_eq_of_lt (by omega) + exact ⟨by unfold Aligned; rw [eh]; omega, offK q k hk ne _ (by omega) (by omega), + by unfold Aligned; rw [et]; omega, offK q k hk ne _ (by omega) (by omega)⟩ + have hTable (ti : U64) : L.mem s' (wa (L.ksb s) (AtOff ti)) = L.mem s (wa (L.ksb s) (AtOff ti)) := by + have h := hvw _ (.inr (asW_at _ ti)) + exact memk _ h.1 h.2 + have tAl : ∀ t ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → Aligned (wa t K) := fun t ht K h1 h2 => + field_aligned (J.sr.core.tcbs t ht) h1 h2 + have sbAl : ∀ t ∈ Z.T, Aligned (wa t 3512#u64) ∧ Aligned (sbWordA t) ∧ Aligned (wa (sbWordA t) 8#u64) := by + intro t ht + have g := pp_sbValA J ht + have al := (J.sr.core.tcbs t ht).align + refine ⟨tAl t ht _ (by simp) (by simp), by unfold Aligned; rw [g.1]; omega, by unfold Aligned; rw [g.2]; omega⟩ + have rAl : ∀ r ∈ Rp, Aligned (rNextA r) ∧ Aligned (rPrevA r) := by + intro r hr' + obtain ⟨g1, -, g3⟩ := J.iplaced.e.rep r hr' + have e8 : (rPrevA r).val = r.val + 8 := by + unfold rPrevA layout.reply.PREV + rw [wa_valP]; exact Nat.mod_eq_of_lt (by simp; omega) + have e16 : (rNextA r).val = r.val + 16 := by + unfold rNextA layout.reply.NEXT + rw [wa_valP]; exact Nat.mod_eq_of_lt (by simp; omega) + constructor <;> show _ % 8 = 0 <;> omega + have hTr : TreeA st OS Z.G.chains Z.G.par s := + J.j.1.cdt.tree.grow J.j5.2.1 (fun x hx hn => J.j5.2.2 x hx hn) + -- the IRQ handlers after the write: `i` has none, every other IRQ state word is kept + have hne : ∀ j : U64, j.val < 5376 → j ≠ i → L.mem s' (irqStAddrA (L.ksb s) j) = L.mem s (irqStAddrA (L.ksb s) j) := by + intro j hj hji + have eI : irqStAddrA (L.ksb s) j = irqWordA (L.ksb s) j := rfl + have hv := irqWordA_val J.sr.ksFit hj + have hal : Aligned (irqWordA (L.ksb s) j) := by + have := J.sr.toSchedSRN.ksb_al + show _ % 8 = 0 + omega + rw [eI] + refine memk _ hal (fun e => hji ?_) + apply UScalar.eq_of_val_eq + rw [e, wv] at hv + omega + have j5 : J5A st Z H OS s' σ2 m := + ⟨⟨MdbInvA.keepO hTr.lk hTr.tree J.j5.1.1 osk, IrqRunA.keepO hTr.lk J.j5.1.2.1 osk, + fun y hy j k => by + have hyOS := ((hTr.lk.members y).1 hy).1 + have k0 : capA st s y = .IrqHandler j := by rw [← osk.capA hyOS]; exact k + obtain ⟨h1, h2⟩ := J.j5.1.2.2 y hy j k0 + refine ⟨h1, ?_⟩ + by_cases e : j = i + · exact absurd (e ▸ k0) (hno y hyOS) + · rw [kb, hne j h1 e]; exact h2⟩, J.j5.2.1, + fun y hy hn => by rw [osk.capA hy]; exact J.j5.2.2 y hy hn⟩ + have j7 : J7 Z H OS s' σ2 m := + J7PpKA.keep (st := st) _ _ _ _ _ σ σ2 _ J.j7 kb osk (J.irq_vsrA hi w) + refine ⟨?_, osk⟩ + have tK : ∀ t ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → L.mem s' (wa t K) = L.mem s (wa t K) := by + intro t ht K h1 h2 + have hta := J.sr.core.tcbs t ht + refine memk _ (tAl t ht K h1 h2) (fun e => ?_) + have htH : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + have ev : (wa t K).val = t.val + K.val := by + have f := hta.fits + rw [wa_valP]; exact Nat.mod_eq_of_lt (by omega) + rw [e, wv] at ev + rcases (C.om wu hΩ).2.1 t htH with g | g <;> omega + have runVcpu : RunVcpuInv st Z.T s' := J.runVcpu.keep + (fun t ht => by rw [stA_eq]; exact tK t ht _ (by simp) (by simp)) + (fun t ht => tK t ht 3672#u64 (by simp) (by simp)) + have vr' : ∀ sc ∈ Z.C, ∀ r ∈ stack sc, RBlk st s' r := J.vr_keepA + (fun r hr => by + obtain ⟨e, al⟩ := J.rTcbValA hr + have rep := J.iplaced.e.rep r hr + exact memk _ al (offK r .reply (J.utIpc.reply r hr) (fun _ h => by cases h) _ (by omega) + (by simp only [HKind.bits]; omega))) + (fun t ht => by rw [stA_eq]; exact tK t ht _ (by simp) (by simp)) + exact JStage3TA.of_irqWr (blkQ := J.blkQ.keepA (fun t ht => tK t ht 3336#u64 (by simp) (by simp)) + (fun _ _ x' hx => Or.inr ⟨x', by rwa [kh] at hx, rfl⟩) (fun _ _ h => by rw [kh]; exact h) + (fun _ _ h => by rw [kh]; exact h)) J (Q := Z.Q) (Rl := Z.R) (curOf := Z.curOf) (curScOf := Z.curScOf) hi w HS SSz (QKeepA.of_kheap kh ⟨by rw [e2], by rw [e2], by rw [e2], by rw [e2]⟩) hirq um har + (fun _ _ => rfl) (fun t ht y hy => J.bufAl t ht y (by rw [← kh]; exact hy)) + (ppEntA3 J.ent blk2 kb hvsT) sq2 (poolOff_keepTableA (Z := Z) J.poolOff kb hTable) + (utFrI_schedSRA hw hW H2 J.sr) (utCnI_scCovA J.scCov H2) (utFrI_epA hw hW H2 J.ep J.iplaced.e) + (utFrI_ntfnA hw hW H2 J.ntfn J.iplaced.n) (utFrI_replyA hw hW H2 J.reply rAl) + (utFrI_bufCoverA hw hW H2 J.bufCover sbAl) (utCnI_idleNoFaultA J.idleNoFault H2) + (utFrI_mcpA hw hW H2 J.mcp fun t ht => ⟨tAl t ht _ (by simp) (by simp), tAl t ht _ (by simp) (by simp)⟩) + (utFrI_yieldA hw hW H2 J.yieldRel fun t ht => tAl t ht _ (by simp) (by simp)) + (utCnI_sinvA J.sinv H2 (SpecIrqA.of_eq J.sinv.irqN kh (by rw [e2]))) + (J.sidle.congr kh (by rw [e2]) (by rw [e2]; exact BitVec.le_refl _)) runVcpu vr' (fun u => by rw [e2]; exact J.noSwitch u) + (J.sact.congr kh (by rw [e2]) (by rw [e2]) (by rw [e2])) j5 j7 + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelJ5StepA.lean b/proofs/Fl4ProofsA64/P3/DelJ5StepA.lean new file mode 100644 index 000000000..b62a3fa67 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelJ5StepA.lean @@ -0,0 +1,264 @@ +/- +P3 (AArch64, delete hooks E4; Architect Option H, 09:08Z): **the J5 slot across the loop's three slot steps**, from +A64 P1's state-level step lemmas. + +Each step takes the state-level MDB-word facts the twin hook hypotheses export: **`SwapMdbA`** (the links and flags +renamed by the transposition), **`EmpMdbA`** (the facts of `EmpCtxA`) and **`ZombMdbA`** (links and flags kept; the +written zombie covers the old object). Then `J5A` is rebuilt: `MdbInvA` by `MdbInvA.rename` / `EmpCtxA.inv` / +`MdbInvA.set_zombie(_dec)`, `IrqRunA` from the new `DStateA`, `HandlersActiveA` by `.ren` / `.rem` / the kept caps +(the IRQ state words are off the slot words), the slot sets from the new `DStateA`. `ParentAdjA` follows +(`MdbInvA.parentAdj`). No named premise. New module. +-/ +import Fl4ProofsA64.P3.DelJ7StepA +import Fl4ProofsA64.P3.DelStepMdbA +import Fl4ProofsA64.P3.Stage3J5A +import Fl4ProofsA64.P1.MdbInvOpsA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {th : TypedHeapA} {OS : Set U64} {s r : S} {σ σ' : State Unit} {G : MdbGhostA} + +/-- `kb` is aligned: IRQ node slot 0 is a slot of the typed heap. -/ +theorem ksb_alignedA {kb : U64} (P : PlaceA th kb) : Aligned kb := by + have h := P.align _ _ (P.irq 0#u64 (by simp)) + have hs : (ObjKindA.size (.cnode 0)) = 32 := rfl + rw [hs] at h + unfold Aligned + have e0 : (0#u64 : U64).val = 0 := rfl + have e32 : (32#u64 : U64).val = 32 := rfl + have e4 : (43232#u64 : U64).val = 43232 := rfl + unfold irqSlotA at h + simp only [core.num.U64.wrapping_add_val_eq, core.num.U64.wrapping_mul_val_eq, U64.size_def, U64.numBits, + e0, e32, e4] at h + have hs : UScalar.size UScalarTy.U64 = 18446744073709551616 := by + first | rfl | decide | (simp [U64.size_def, U64.numBits]; done) | + (simp [UScalar.size, UScalarTy.numBits, U64.numBits]; done) | (simp [U64.size, U64.numBits]; done) + simp only [hs] at h + omega + +/-- **The IRQ state words are off the slot words**, so a slot-only step keeps them. -/ +theorem irqSt_keptA {H : Heap} {Ub : Set U64} (hH : HeapOK H) (ts : TcbSlotsOS H.tcbs OS) + (kb : KsBridge st H OS Ub s) (P : PlaceA th (L.ksb s)) + (hksw : (L.ksb s).val + 788736 ≤ 2 ^ 64) + (w : WritesIn st s r (W7.CapW H.tcbs {b | InOS OS b} ∅)) : + ∀ i : U64, i.val < 5376 → L.mem r (irqStAddrA (L.ksb s) i) = L.mem s (irqStAddrA (L.ksb s) i) := by + intro i hi + have hal := ksb_alignedA P + have hv : (irqStAddrA (L.ksb s) i).val = (L.ksb s).val + 224 + i.val * 8 := by + unfold irqStAddrA + have e224 : (224#u64 : U64).val = 224 := rfl + have e8 : (8#u64 : U64).val = 8 := rfl + simp only [core.num.U64.wrapping_add_val_eq, core.num.U64.wrapping_mul_val_eq, U64.size_def, U64.numBits, + e224, e8] + have hs : UScalar.size UScalarTy.U64 = 18446744073709551616 := by + first | rfl | decide | (simp [U64.size_def, U64.numBits]; done) | + (simp [UScalar.size, UScalarTy.numBits, U64.numBits]; done) | (simp [U64.size, U64.numBits]; done) + simp only [hs]; omega + have notOS : ¬ InOS OS (irqStAddrA (L.ksb s) i) := by + rintro ⟨y, hy, h1, h2⟩ + rcases (kb.slotKs y hy).1 with h | h <;> omega + refine w.mem_eq (by unfold Aligned at hal ⊢; omega) fun h => ?_ + rcases h with h | ⟨h, -⟩ + · exact notOS (tlow_inOSA hH ts h) + · exact notOS h + +/-- A descendant relation of an `MdbOK` ghost has no loop. -/ +theorem desc_irreflA {par : U64 → Option U64} {Gl : List (List U64)} (hOk : Fl4Proofs.P3.Mdb.MdbOK par Gl) {x : U64} + (hx : x ∈ Gl.flatten) : ¬ Fl4Proofs.P3.Mdb.Desc par x x := fun hd => by + obtain ⟨ch, hch, hxch⟩ := List.mem_flatten.1 hx + obtain ⟨A, B, hsp⟩ := List.append_of_mem hxch + obtain ⟨dd, rest, hB, hdd⟩ := hOk.seg ch hch A x B hsp + have hxdd : x ∈ dd := (hdd x).1 hd + have hnd : (A ++ x :: (dd ++ rest)).Nodup := by + rw [← hB, ← hsp]; exact (List.nodup_flatten.1 hOk.nodup).1 _ hch + rw [List.nodup_append, List.nodup_cons] at hnd + exact hnd.2.1.1 (List.mem_append_left rest hxdd) + +/-- A thread or CNode cap. -/ +def IsTcbCnA (c : capability.Cap) : Prop := (∃ t, c = .Thread t) ∨ ∃ q rb gs g, c = .CNode q rb gs g + +/-- A thread / CNode cap is a range key (not an untyped or frame). -/ +theorem tcbCn_rngA {e : capability.Cap} (he : IsTcbCnA e) : RngKeyA e := by + refine ⟨?_, ?_, ?_⟩ + · have p := physA_eq e + rcases he with ⟨t, rfl⟩ | ⟨q, rb, gs, g, rfl⟩ <;> + (simp only [objects.cap_is_physical] at p; exact (okinj p).symm) + · rcases he with ⟨t, rfl⟩ | ⟨q, rb, gs, g, rfl⟩ <;> rfl + · rcases he with ⟨t, rfl⟩ | ⟨q, rb, gs, g, rfl⟩ <;> rfl + +theorem ptrA_thA (t : U64) : ptrA (.Thread t) = t := by + have h := ptrA_eq (.Thread t) + simp only [capability.Cap.ptr] at h + exact (okinj h).symm + +theorem ptrA_cnA (q rb gs g : U64) : ptrA (.CNode q rb gs g) = q := by + have h := ptrA_eq (.CNode q rb gs g) + simp only [capability.Cap.ptr] at h + exact (okinj h).symm + +/-- A non-untyped cap whose region holds a thread / CNode cap names the same object. -/ +theorem sra_obj_leftA {c d : capability.Cap} (h : sraA c d = true) (hu : isUtA c = false) (hd : IsTcbCnA d) : + RngKeyA c ∧ ptrA c = ptrA d := by + have h' : objects.same_region_as c d = ok true := h ▸ sraA_eq c d + rcases hd with ⟨t, rfl⟩ | ⟨q, rb, gs, g, rfl⟩ + · cases c <;> first + | (simp [isUtA] at hu; done) + | (simp [objects.same_region_as] at h'; done) + | skip + case Thread p => + by_cases e : p = t + · subst e; exact ⟨tcbCn_rngA (Or.inl ⟨_, rfl⟩), rfl⟩ + · have e' : ¬ t = p := fun h => e h.symm + simp [objects.same_region_as, e, e'] at h' + · cases c <;> first + | (simp [isUtA] at hu; done) + | (simp [objects.same_region_as] at h'; done) + | skip + case CNode p r' gs' g' => + by_cases e : p = q + · subst e; exact ⟨tcbCn_rngA (Or.inr ⟨_, _, _, _, rfl⟩), by rw [ptrA_cnA, ptrA_cnA]⟩ + · have e' : ¬ q = p := fun h => e h.symm + simp [objects.same_region_as, e, e'] at h' + +/-- A cap in the region of a thread / CNode cap names the same object. -/ +theorem sra_obj_rightA {c d : capability.Cap} (h : sraA d c = true) (hd : IsTcbCnA d) : + RngKeyA c ∧ ptrA c = ptrA d := by + have h' : objects.same_region_as d c = ok true := h ▸ sraA_eq d c + rcases hd with ⟨t, rfl⟩ | ⟨q, rb, gs, g, rfl⟩ + · cases c <;> first + | (simp [objects.same_region_as] at h'; done) + | skip + case Thread p => + by_cases e : p = t + · subst e; exact ⟨tcbCn_rngA (Or.inl ⟨_, rfl⟩), rfl⟩ + · have e' : ¬ t = p := fun h => e h.symm + simp [objects.same_region_as, e, e'] at h' + · cases c <;> first + | (simp [objects.same_region_as] at h'; done) + | skip + case CNode p r' gs' g' => + by_cases e : p = q + · subst e; exact ⟨tcbCn_rngA (Or.inr ⟨_, _, _, _, rfl⟩), by rw [ptrA_cnA, ptrA_cnA]⟩ + · have e' : ¬ q = p := fun h => e h.symm + simp [objects.same_region_as, e, e'] at h' + +/-! ## The three steps -/ + +/-- `J5A`'s slot sets come from the new deletion state. -/ +theorem j5_ofA {Z : XSetsA} {H : Heap} {m : McState} (D : DStateA st Z.th OS r σ' Z.G) + (hM : MdbInvA st Z.G.chains Z.G.par r) (hh : HandlersActiveA st Z.G.chains r) : + J5A st Z H OS r σ' m := + ⟨⟨hM, ⟨D.irq.one, D.irq.chunk⟩, hh⟩, D.sub, D.nul⟩ + +/-- **J5 across the swap of `x` and `p`.** -/ +theorem J5A.delSwapA {Z : XSetsA} {H : Heap} {m m' : McState} {x p : U64} (J : J5A st Z H OS s σ m) + (D : DStateA st Z.th OS s σ Z.G) (D' : DStateA st Z.th OS r σ' (swGA Z.G x p)) (hx : x ∈ OS) (hp : p ∈ OS) + (hcap : ∀ y ∈ OS, capA st r y = if y = x then capA st s p else if y = p then capA st s x else capA st s y) + (M : SwapMdbA st Z.G.chains x p s r) (hk : L.ksb r = L.ksb s) + (hst : ∀ i : U64, i.val < 5376 → L.mem r (irqStAddrA (L.ksb s) i) = L.mem s (irqStAddrA (L.ksb s) i)) : + J5A st { Z with G := swGA Z.G x p } H OS r σ' m' := by + have inOS : ∀ y ∈ Z.G.chains.flatten, y ∈ OS := fun y hy => ((D.lk.members y).1 hy).1 + have ec : ∀ a ∈ OS, capA st r (Equiv.swap x p a) = capA st s a := by + intro a ha + rw [Equiv.swap_apply_def] + by_cases h1 : a = x + · rw [if_pos h1, hcap p hp, h1] + by_cases h2 : p = x + · rw [if_pos h2, h2] + · rw [if_neg h2, if_pos rfl] + · rw [if_neg h1] + by_cases h2 : a = p + · rw [if_pos h2, hcap x hx, if_pos rfl, h2] + · rw [if_neg h2, hcap a ha, if_neg h1, if_neg h2] + have esw : swp x p = fun y => Equiv.swap x p y := by + funext y; rw [Equiv.swap_apply_def]; rfl + have eren : renG x p Z.G.chains = Z.G.chains.map (List.map (Equiv.swap x p)) := by + unfold renG; rw [esw] + have hI := J.1.1.rename (s' := r) D.lk D.tree.tree (Equiv.swap x p) + (Equiv.swap_apply_of_ne_of_ne (fun e => D.nz x hx e.symm) (fun e => D.nz p hp e.symm)) + (fun a ha => (M a ha).1) + (fun a ha => by rw [ec a (inOS a ha)]; exact ⟨rfl, rfl⟩) + (fun a ha => (M a ha).2.1) (fun a ha => (M a ha).2.2) + have hh := J.1.2.2.ren (s' := r) (x1 := x) (x2 := p) (fun y hy i k => by + rw [esw] at k + rwa [ec y (inOS y hy)] at k) hk hst + refine j5_ofA (Z := { Z with G := swGA Z.G x p }) D' ?_ hh + show MdbInvA st (renG x p Z.G.chains) _ r + rw [eren]; exact hI + +/-- **J5 across the emptying of `x`.** -/ +theorem J5A.emptyA {Z : XSetsA} {H : Heap} {m m' : McState} {x : U64} (J : J5A st Z H OS s σ m) + (D : DStateA st Z.th OS s σ Z.G) (D' : DStateA st Z.th OS r σ' (remGA Z.G x)) (hx : x ∈ OS) + (hn : capA st s x ≠ .Null) (hcap : ∀ y ∈ OS, y ≠ x → capA st r y = capA st s y) + (M : EmpMdbA st Z.G.chains x s r) (hk : L.ksb r = L.ksb s) + (hst : ∀ i : U64, i.val < 5376 → L.mem r (irqStAddrA (L.ksb s) i) = L.mem s (irqStAddrA (L.ksb s) i)) : + J5A st { Z with G := remGA Z.G x } H OS r σ' m' := by + have inOS : ∀ y ∈ Z.G.chains.flatten, y ∈ OS := fun y hy => ((D.lk.members y).1 hy).1 + have C : EmpCtxA st OS Z.G.chains Z.G.par s r x := + ⟨D.nz, D.lk, D.tree.tree, J.1.1, (D.lk.members x).2 ⟨hx, hn⟩, fun a ha ne => hcap a (inOS a ha) ne, + M.rev, M.fb, M.next⟩ + have hh := J.1.2.2.rem (s' := r) D.lk.nodup (x := x) (fun y hy ne => hcap y (inOS y hy) ne) hk hst + exact j5_ofA (Z := { Z with G := remGA Z.G x }) D' C.inv hh + +/-- **J5 across the zombie write at `x`.** -/ +theorem J5A.zombieA {Z : XSetsA} {H : Heap} {m m' : McState} {x q zt n : U64} (J : J5A st Z H OS s σ m) + (D : DStateA st Z.th OS s σ Z.G) (D' : DStateA st Z.th OS r σ' Z.G) (hx : x ∈ OS) + (hn : capA st s x ≠ .Null) (hz : capA st r x = .Zombie q zt n) + (hcap : ∀ y ∈ OS, y ≠ x → capA st r y = capA st s y) + (old : IsTcbCnA (capA st s x) ∨ ∃ n', capA st s x = .Zombie q zt n') + (M : ZombMdbA st Z.G.chains x s r) (hk : L.ksb r = L.ksb s) + (hst : ∀ i : U64, i.val < 5376 → L.mem r (irqStAddrA (L.ksb s) i) = L.mem s (irqStAddrA (L.ksb s) i)) : + J5A st Z H OS r σ' m' := by + have inOS : ∀ y ∈ Z.G.chains.flatten, y ∈ OS := fun y hy => ((D.lk.members y).1 hy).1 + have hxG : x ∈ Z.G.chains.flatten := (D.lk.members x).2 ⟨hx, hn⟩ + have hoth : ∀ y ∈ Z.G.chains.flatten, y ≠ x → capA st r y = capA st s y := fun y hy ne => hcap y (inOS y hy) ne + have hI : MdbInvA st Z.G.chains Z.G.par r := by + rcases old with old | ⟨n', e⟩ + · have nz : ¬ IsZombieA (capA st s x) := by + rintro ⟨_, _, _, e⟩ + rw [e] at old + rcases old with ⟨_, h⟩ | ⟨_, _, _, _, h⟩ <;> cases h + have ua := M.cover nz + rw [hz] at ua + have eptr : ptrA (capA st s x) = q := by + rw [← ua.ptr]; exact ptrA_zombie _ _ _ + have apart : ∀ y ∈ Z.G.chains.flatten, y ≠ x → RngKeyA (capA st s y) → ptrA (capA st s y) ≠ q := by + intro y hy ne k + have h := D'.zf x hx q zt n hz y (inOS y hy) ne (by rw [hoth y hy ne]; exact k) + rwa [hoth y hy ne] at h + refine J.1.1.set_zombie (s' := r) D.lk D.tree.tree hxG hz ua hoth M.next M.rev M.fb ?_ ?_ + · intro y hy ne nu + cases h : sraA (capA st s y) (capA st s x) with + | false => rfl + | true => + have ⟨k, e⟩ := sra_obj_leftA h nu old + exact absurd (e.trans eptr) (apart y hy ne k) + · intro y hd + have hp := J.1.1.desc x y hd + have hyG := (desc_memA D.tree.tree hd).1 + have ne : y ≠ x := fun e => desc_irreflA D.tree.tree hxG (e ▸ hd) + unfold isPMA isPFA at hp + simp only [Bool.and_eq_true] at hp + have ⟨k, e⟩ := sra_obj_rightA hp.1.2 old + exact apart y hyG ne k (e.trans eptr) + · exact J.1.1.set_zombie_dec (s' := r) D.lk D.tree.tree hxG e hz hoth M.next M.rev M.fb + have hh : HandlersActiveA st Z.G.chains r := fun y hy i k => by + have ne : y ≠ x := fun e => by rw [e, hz] at k; cases k + rw [hoth y hy ne] at k + have ⟨h1, h2⟩ := J.1.2.2 y hy i k + exact ⟨h1, by rw [hk, hst i h1]; exact h2⟩ + exact j5_ofA D' hI hh + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelJ7LA.lean b/proofs/Fl4ProofsA64/P3/DelJ7LA.lean new file mode 100644 index 000000000..1fa0e256c --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelJ7LA.lean @@ -0,0 +1,50 @@ +/- +P3 (AArch64, delete hooks E4; Architect ruling 09:08Z, "a field no hook reads inside the loop may be restored at the +top"): **`J7LA`**, the deletion loop's J7 slot: `P3/Stage3J7A`'s `J7A` without `UserOffFr`. + +`UserOffFr`'s childless clause reads the MDB successor of an untyped: no state-level argument keeps it across the +emptying of a slot (A64 P1 itself rebuilds it from `SysL`: `S1aColU.of`), and no finaliser hook reads it. The loop +carries `UbFr ∧ FrameOK`; the twin top restores `UserOffFr` from A64 P1's deletion top (`cdt.delete.s1aColU`), as it +does `SysL` (Option T). `J7A.toLA` enters the loop, `J7LA.toA` leaves it. No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3J7A +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} (st : machine.Store S) [L : LawfulStore st] + +/-- **J7 inside the deletion loop** (module docstring). -/ +def J7LA (R : Set U64 → JSlotA S) : JSlotA S := fun Z H OS s σ m => + ∃ Pt Ub : Set U64, + VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt ∧ PoolNamed st OS s ∧ + S1aArmCol st H Z.Cs OS Z.V Ub s ∧ (UbFr st OS Ub s ∧ FrameOK st H OS s) ∧ VcCap st Z.V OS s ∧ DevFrAl st OS s ∧ + HiCap511 st OS s ∧ (∀ p, SchedPageA st Z OS s p → p ∈ Pt) ∧ + (∀ b, VcW Z.V b → ∀ o : U64, o.val < 223488 → wa (L.ksb s) o ≠ b) ∧ FrUb st Ub OS s ∧ (∀ b ∈ Ub, Aligned b) ∧ (∀ b, VcW Z.V b → b ∉ Ub) ∧ R Pt Z H OS s σ m + +variable {st} + +/-- Entering the loop. -/ +theorem J7A.toLA {R : Set U64 → JSlotA S} {Z : XSetsA} {H : Heap} {OS : Set U64} {s : S} + {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} (h : J7A st R Z H OS s σ m) : J7LA st R Z H OS s σ m := by + obtain ⟨Pt, Ub, vr, pn, arm, capc, vc, df, hicap, link, vcks, r⟩ := h + exact ⟨Pt, Ub, vr, pn, arm, ⟨capc.uf, capc.fo⟩, vc, df, hicap, link, vcks, r⟩ + +/-- Leaving the loop, with `UserOffFr` restored. -/ +theorem J7LA.toA {R : Set U64 → JSlotA S} {Z : XSetsA} {H : Heap} {OS : Set U64} {s : S} + {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} (h : J7LA st R Z H OS s σ m) + (uo : UserOffFr st OS s) : J7A st R Z H OS s σ m := by + obtain ⟨Pt, Ub, vr, pn, arm, capc, vc, df, hicap, link, vcks, r⟩ := h + exact ⟨Pt, Ub, vr, pn, arm, ⟨capc.1, uo, capc.2⟩, vc, df, hicap, link, vcks, r⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelJ7StepA.lean b/proofs/Fl4ProofsA64/P3/DelJ7StepA.lean new file mode 100644 index 000000000..50531216b --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelJ7StepA.lean @@ -0,0 +1,250 @@ +/- +P3 (AArch64, delete hooks E4): **the J7 slot across the loop's three slot steps**. + +The steps write slot words only (`W7.CapW H.tcbs {InOS OS} ∅`), so every VSpace and ASID-table word of `J7LA`'s `Pt` +is kept (`vsKeep_capwA`); the cap-reading parts are closed under `CapFromA` (`P3/DelCapKeepA`), the word set `Ub` +shrinking to the words the remaining frames cover. What is left per step: + +* **swap** (`J7LA.swapA`): `PoolNamed` and R2 by the permutation (`S1aDelM.swap`); +* **zombie write** (`J7LA.zombieA`): the old and new cap at `x` are none that R2 or `PoolNamed` reads; +* **empty** (`J7LA.emptyA`): from the slot-pinned R2 of `Rd` (`J7R2xA.restoreA`), and `PoolNamed` from A64 P1's + `PoolEm` (the pool's table entry is gone, or another slot names the pool). + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.DelRestoreA +import Fl4ProofsA64.P3.DelCapKeepA +import Fl4ProofsA64.P1.S1aArmDelU +import Fl4ProofsA64.P1.MdbPoolDelR +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {OS : Set U64} {s r : S} + +/-- A low TCB word lies in one of the TCB's seven cap slots. -/ +theorem tlow_inOSA {H : Heap} (hH : HeapOK H) (ts : TcbSlotsOS H.tcbs OS) {b : U64} + (h : Fl4ProofsA64.P1.W7.TLow H.tcbs b) : InOS OS b := by + obtain ⟨t, ht, K, hK, e⟩ := h + have ht' : H t = some .tcb := ht + have hi : (⟨BitVec.ofNat 64 (K.val / 32)⟩ : U64).val = K.val / 32 := by + show (BitVec.ofNat 64 (K.val / 32)).toNat = _ + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + have hm : (core.num.U64.wrapping_mul ⟨BitVec.ofNat 64 (K.val / 32)⟩ 32#u64).val = K.val / 32 * 32 := by + have e32 : (32#u64 : U64).val = 32 := rfl + simp only [core.num.U64.wrapping_mul_val_eq, U64.size_def, U64.numBits, hi, e32] + have hs : UScalar.size UScalarTy.U64 = 18446744073709551616 := by + first | rfl | decide | (simp [U64.size_def, U64.numBits]; done) | + (simp [UScalar.size, UScalarTy.numBits, U64.numBits]; done) | (simp [U64.size, U64.numBits]; done) + simp only [hs]; omega + refine ⟨_, ts t ht ⟨BitVec.ofNat 64 (K.val / 32)⟩ (by rw [hi]; omega), ?_⟩ + have hy := wa_val_tcbA hH ht' (K := core.num.U64.wrapping_mul ⟨BitVec.ofNat 64 (K.val / 32)⟩ 32#u64) + (by rw [hm]; omega) + have hb := wa_val_tcbA hH ht' (K := K) (by omega) + unfold slotIdx + rw [hy, hm, e, hb] + omega + +/-- **The VSpace words are off the slot words** (`VrOK`, with the TCB slots in `OS`). -/ +theorem vsKeep_capwA {H : Heap} {Ω Sg Scs : Set U64} {Z : U64 → Nat} {Vc Ub Pt : Set U64} {kb : U64} + (hH : HeapOK H) (ts : TcbSlotsOS H.tcbs OS) (vr : VrOK Ω Sg Scs Z OS Vc Ub kb Pt) + (w : WritesIn st s r (W7.CapW H.tcbs {b | InOS OS b} ∅)) : VsKeep st kb Pt s r := fun b hb => + w.mem_eq (vr.al b hb) fun h => by + rcases h with h | ⟨h, -⟩ + · exact vr.os b hb (tlow_inOSA hH ts h) + · exact vr.os b hb h + +/-- A class holding of `Null` and of zombies is kept when every cap after is one of before. -/ +theorem CapInv.fromA {P : capability.Cap → Prop} (h : CapFromA st OS s r) (hn : P .Null) + (hz : ∀ c, IsZombieA c → P c) (o : CapInv st P OS s) : CapInv st P OS r := fun y hy => by + rcases h y hy with e | e | ⟨z, hz', e⟩ + · rw [e]; exact hn + · exact hz _ e + · rw [e]; exact o z hz' + +theorem vcCap_fromA {V : Set U64} (h : CapFromA st OS s r) (o : VcCap st V OS s) : VcCap st V OS r := + CapInv.fromA h (VcCapP.null V) (fun c ⟨p, t, n, e⟩ => e ▸ VcCapP.zombie V p t n) o + +theorem devFrAl_fromA (h : CapFromA st OS s r) (o : DevFrAl st OS s) : DevFrAl st OS r := + CapInv.fromA h (fun _ _ _ _ _ _ _ e => by cases e) + (fun c ⟨_, _, _, e⟩ => e ▸ (fun _ _ _ _ _ _ _ e' => by cases e')) o + +theorem frameOK_fromA {H : Heap} (h : CapFromA st OS s r) (o : FrameOK st H OS s) : FrameOK st H OS r := by + intro y hy ptr size rights x1 x2 x3 x4 hc + obtain ⟨z, hz, ez⟩ := h.src hy hc (by intro h'; cases h') (by rintro ⟨_, _, _, h'⟩; cases h') + exact o z hz ptr size rights x1 x2 x3 x4 ez + +variable (st) in +/-- The words of `Ub` the remaining non-device frames cover. -/ +def ubCovA (OS Ub : Set U64) (r : S) : Set U64 := + {b | b ∈ Ub ∧ ∃ y ∈ OS, ∃ ptr size rights x1 x2 x3 x4 bits, + capA st r y = .Frame ptr size rights false x1 x2 x3 x4 ∧ size.bits = ok bits ∧ + ptr.val ≤ b.val ∧ b.val < ptr.val + 2 ^ bits.val} + +/-- (record edit #5) `FrUb` at the covered words: a frame cap after was one before, and covers its own words. -/ +theorem frUb_fromA {OS Ub : Set U64} {s r : S} (fr : CapFromA st OS s r) (h : FrUb st Ub OS s) : + FrUb st (ubCovA st OS Ub r) OS r := by + intro y hy ptr size rights x1 x2 x3 x4 hc bits hb b al lo hi + obtain ⟨z, hz, ez⟩ := fr.src hy hc (by intro h'; cases h') (by rintro ⟨_, _, _, h'⟩; cases h') + exact ⟨h z hz ptr size rights x1 x2 x3 x4 ez bits hb b al lo hi, y, hy, ptr, size, rights, x1, x2, x3, x4, bits, + hc, hb, lo, hi⟩ + +/-- A smaller word set keeps `VrOK`. -/ +theorem VrOK.subUbA {Ω Sg Scs : Set U64} {Z : U64 → Nat} {Vc Ub Ub' Pt : Set U64} {kb : U64} + (h : Ub' ⊆ Ub) (vr : VrOK Ω Sg Scs Z OS Vc Ub kb Pt) : VrOK Ω Sg Scs Z OS Vc Ub' kb Pt := + { vr with ub := fun b hb hm => vr.ub b hb (h hm) } + +theorem poolNamed_ofA (hk : L.ksb r = L.ksb s) + (ht : ∀ j, L.mem r (wa (L.ksb s) (AtOff j)) = L.mem s (wa (L.ksb s) (AtOff j))) + (hc : ∀ z ∈ OS, ∀ p b, capA st s z = .AsidPool p b → ∃ y ∈ OS, capA st r y = .AsidPool p b) + (o : PoolNamed st OS s) : PoolNamed st OS r := by + intro i + have h0 := o i + rw [vspace.asid_table.eq] at h0 ⊢ + rw [hk, ht i] + refine (WP.spec_ok _).2 fun hne => ?_ + obtain ⟨z, hz, b, e⟩ := (WP.spec_ok _).1 h0 hne + obtain ⟨y, hy, ey⟩ := hc z hz _ _ e + exact ⟨y, hy, b, ey⟩ + +/-- **J7 across a slot-only step** (module docstring): `pn` and `hR` are what a step has to give. -/ +theorem J7LA.stepA {Z Z' : XSetsA} {H : Heap} {σ σ' : State Unit} {m m' : McState} {R R' : Set U64 → JSlotA S} + (J : J7LA st R Z H OS s σ m) (hH : HeapOK H) + (w : WritesIn st s r (W7.CapW H.tcbs {b | InOS OS b} ∅)) + (eC : Z'.Cs = Z.Cs) (eV : Z'.V = Z.V) (e1 : Z'.Pp = Z.Pp) (e2 : Z'.S1 = Z.S1) (e3 : Z'.T2 = Z.T2) + (e4 : Z'.X = Z.X) (e5 : Z'.Y = Z.Y) + (fr : CapFromA st OS s r) + (hY : ∀ y ∈ OS, ∀ q mp ad, capA st s y = .HighVSpace q mp ad → q ∈ Z.Y) + (pn : (∀ j, L.mem r (wa (L.ksb s) (AtOff j)) = L.mem s (wa (L.ksb s) (AtOff j))) → + PoolNamed st OS s → PoolNamed st OS r) + (hR : ∀ Pt, VsKeep st (L.ksb s) Pt s r → R Pt Z H OS s σ m → R' Pt Z' H OS r σ' m') : + J7LA st R' Z' H OS r σ' m' := by + obtain ⟨Pt, Ub, vr, hpn, arm, capc, vc, df, hicap, link, vcks, fu, ual, vcu, RR⟩ := J + have hk : L.ksb r = L.ksb s := w.1 + have mem : VsKeep st (L.ksb s) Pt s r := vsKeep_capwA hH arm.case.ts vr w + have ht : ∀ j, L.mem r (wa (L.ksb s) (AtOff j)) = L.mem s (wa (L.ksb s) (AtOff j)) := + fun j => mem _ (Or.inr (asW_at _ j)) + have tk : TabKeep st s r := ⟨hk, fun j => by rw [hk]; exact ht j⟩ + have sub : ubCovA st OS Ub r ⊆ Ub := fun b hb => hb.1 + have arm' := (S1aArmCol.ofTab tk arm).sub sub + refine ⟨Pt, ubCovA st OS Ub r, by rw [hk, eV]; exact VrOK.subUbA sub vr, pn ht hpn, by rw [eC, eV]; exact arm', + ⟨fun b hb => hb.2, frameOK_fromA fr capc.2⟩, by rw [eV]; exact vcCap_fromA fr vc, devFrAl_fromA fr df, + ?_, fun p hp => link p (SchedPageA.fromA fr e1 e2 e3 e4 e5 hp), by rw [eV, hk]; exact vcks, frUb_fromA fr fu, fun b hb => ual b hb.1, + fun b hv hb => vcu b (eV ▸ hv) hb.1, hR Pt mem RR⟩ + intro y hy p mp a hc + obtain ⟨z, hz, ez⟩ := fr.src hy hc (by intro h'; cases h') (by rintro ⟨_, _, _, h'⟩; cases h') + have hp : p ∈ Pt := link p (by + unfold SchedPageA + exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (hY z hz _ _ _ ez))))))) + have e : L.mem r (wa p 4088#u64) = L.mem s (wa p 4088#u64) := + mem _ (Or.inl ⟨p, hp, 4088#u64, by simp, by simp, rfl⟩) + rw [e]; exact hicap z hz p mp a ez + +/-! ## The three steps -/ + +variable {Z : XSetsA} {G' : MdbGhostA} {H : Heap} {σ σ' : State Unit} {m m' : McState} {ipa : U64} + +/-- **J7 across the swap of `x` and `p`.** -/ +theorem J7LA.swapA {x p : U64} (J : J7LA st (J7R2A st ar ipa) Z H OS s σ m) (hH : HeapOK H) + (w : WritesIn st s r (W7.CapW H.tcbs {b | InOS OS b} ∅)) (hx : x ∈ OS) (hp : p ∈ OS) + (hcap : ∀ y ∈ OS, capA st r y = if y = x then capA st s p else if y = p then capA st s x else capA st s y) + (hY : ∀ y ∈ OS, ∀ q mp ad, capA st s y = .HighVSpace q mp ad → q ∈ Z.Y) : + J7LA st (J7R2A st ar ipa) { Z with G := G' } H OS r σ' m' := by + have fr := CapFromA.of_swap (st := st) hx hp hcap + have tc := CapToA.of_swap (st := st) hx hp hcap + have first : capA st r x = capA st s p := by rw [hcap x hx, if_pos rfl] + have second : capA st r p = capA st s x := by + rw [hcap p hp] + by_cases e : p = x + · rw [if_pos e, e] + · rw [if_neg e, if_pos rfl] + refine J.stepA hH w rfl rfl rfl rfl rfl rfl rfl fr hY (fun ht o => poolNamed_ofA w.1 ht (fun z hz q b e => ?_) o) + (fun Pt mem R => ?_) + · obtain ⟨y, hy, ey⟩ := tc z hz + exact ⟨y, hy, ey.trans e⟩ + · obtain ⟨Pp, Tb, Hr, Te, Kt, Lv, Re, bits, R2, sep, KLv, ktK, hik⟩ := R + have hik' : S1aHiK st OS Kt KLv r := S1aHiK.keep hik ⟨EdgeKeepOn.ofVsKeep R2 mem, fun y hy R m a c => by + obtain ⟨z, hz, ez⟩ := fr.src hy c (by intro h'; cases h') (by rintro ⟨_, _, _, h'⟩; cases h') + exact ⟨z, hz, m, a, ez⟩⟩ + have M := S1aDelM.swap (r := r) R2.toDelM hx hp w.1 mem first second (fun z hz a b => by + rw [hcap z hz, if_neg a, if_neg b]) + refine ⟨Pp, Tb, _, Te, Kt, Lv, Re, bits, M, sep.ofRanges fun y hy q b i d e => ?_, KLv, ktK, hik'⟩ + obtain ⟨z, hz, ez⟩ := fr.untyped hy e + exact ⟨z, hz, q, b, i, d, ez, Nat.le_refl _, Nat.le_refl _⟩ + +/-- A zombie is read neither by R2 nor as an `AsidPool`. -/ +theorem zombie_unreadA {c : capability.Cap} (h : IsZombieA c) : ¬ S1aR2Reads c := by + obtain ⟨_, _, _, rfl⟩ := h + exact id + +/-- **J7 across the zombie write at `x`** (the old cap is a thread, a CNode or a zombie). -/ +theorem J7LA.zombieA {x q zt n : U64} (J : J7LA st (J7R2A st ar ipa) Z H OS s σ m) (hH : HeapOK H) + (w : WritesIn st s r (W7.CapW H.tcbs {b | InOS OS b} ∅)) (hx : x ∈ OS) + (hz : capA st r x = .Zombie q zt n) (hcap : ∀ y ∈ OS, y ≠ x → capA st r y = capA st s y) + (old : (∃ t, capA st s x = .Thread t) ∨ (∃ c rb gs g, capA st s x = .CNode c rb gs g) ∨ IsZombieA (capA st s x)) + (hY : ∀ y ∈ OS, ∀ q mp ad, capA st s y = .HighVSpace q mp ad → q ∈ Z.Y) : + J7LA st (J7R2A st ar ipa) { Z with G := G' } H OS r σ' m' := by + have fr := CapFromA.of_zombie (st := st) (OS := OS) (s := s) hz hcap + have unread : ¬ S1aR2Reads (capA st s x) := by + rcases old with ⟨t, e⟩ | ⟨c, rb, gs, g, e⟩ | h + · rw [e]; exact id + · rw [e]; exact id + · exact zombie_unreadA h + have notAp : ∀ p b, capA st s x ≠ .AsidPool p b := fun p b e => unread (by rw [e]; trivial) + refine J.stepA hH w rfl rfl rfl rfl rfl rfl rfl fr hY (fun ht o => poolNamed_ofA w.1 ht (fun z hz' p b e => ?_) o) + (fun Pt mem R => ?_) + · have ne : z ≠ x := fun h => notAp p b (h ▸ e) + exact ⟨z, hz', (hcap z hz' ne).trans e⟩ + · obtain ⟨Pp, Tb, Hr, Te, Kt, Lv, Re, bits, R2, sep, KLv, ktK, hik⟩ := R + have hik' : S1aHiK st OS Kt KLv r := S1aHiK.keep hik ⟨EdgeKeepOn.ofVsKeep R2 mem, fun y hy R m a c => by + obtain ⟨z, hz, ez⟩ := fr.src hy c (by intro h'; cases h') (by rintro ⟨_, _, _, h'⟩; cases h') + exact ⟨z, hz, m, a, ez⟩⟩ + have M := S1aDelPn.restore (r := r) (S1aDelM.drop R2.toDelM unread) w.1 mem + (fun y hy ne => Or.inl (hcap y hy ne)) (zombie_unreadA ⟨q, zt, n, hz⟩) + refine ⟨Pp, Tb, _, Te, Kt, Lv, Re, bits, M, sep.ofRanges fun y hy p b i d e => ?_, KLv, ktK, hik'⟩ + obtain ⟨z, hz', ez⟩ := fr.untyped hy e + exact ⟨z, hz', p, b, i, d, ez, Nat.le_refl _, Nat.le_refl _⟩ + +/-- **J7 across the emptying of `x`**, from the slot-pinned R2 of `Rd`. -/ +theorem J7LA.emptyA {x : U64} (J : J7LA st (J7R2xA st ar ipa x) Z H OS s σ m) (hH : HeapOK H) + (w : WritesIn st s r (W7.CapW H.tcbs {b | InOS OS b} ∅)) + (h0 : capA st r x = .Null) (hcap : ∀ y ∈ OS, y ≠ x → capA st r y = capA st s y) + (pe : PoolEm st OS s x) + (hY : ∀ y ∈ OS, ∀ q mp ad, capA st s y = .HighVSpace q mp ad → q ∈ Z.Y) : + J7LA st (J7R2A st ar ipa) { Z with G := G' } H OS r σ' m' := by + have fr := CapFromA.of_empty (st := st) (OS := OS) (s := s) h0 hcap + refine J.stepA hH w rfl rfl rfl rfl rfl rfl rfl fr hY (fun ht o => ?_) (fun Pt mem R => ?_) + · intro i + have h0' := o i + rw [vspace.asid_table.eq] at h0' ⊢ + rw [w.1, ht i] + refine (WP.spec_ok _).2 fun hne => ?_ + obtain ⟨y, hy, b, e⟩ := (WP.spec_ok _).1 h0' hne + by_cases ey : y = x + · subst ey + rcases pe _ _ e with g | ⟨y', hy', ne, b', e'⟩ + · exact absurd (g i rfl) hne + · exact ⟨y', hy', b', (hcap y' hy' ne).trans e'⟩ + · exact ⟨y, hy, b, (hcap y hy ey).trans e⟩ + · refine J7R2xA.restoreA (r := r) (Z' := { Z with G := G' }) (σ' := σ') (m' := m') R w.1 mem + (fun y hy ne => Or.inl (hcap y hy ne)) (by rw [h0]; exact id) (fun y hy => ?_) (fun y hy R m a c => ?_) + swap + · have ne : y ≠ x := fun e => by rw [e, h0] at c; cases c + exact ⟨y, ⟨hy, ne⟩, m, a, (hcap y hy ne).symm.trans c⟩ + by_cases e : y = x + · subst e + exact Or.inr fun p b i d h => by rw [h0] at h; cases h + · exact Or.inl (hcap y hy e) + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelMdbLeafA.lean b/proofs/Fl4ProofsA64/P3/DelMdbLeafA.lean new file mode 100644 index 000000000..1b8d9b6e9 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelMdbLeafA.lean @@ -0,0 +1,179 @@ +/- +P3 (AArch64, delete hooks E4; Architect Option H): **the MDB words across the loop's `empty_slot`**, exported for the +twin hook hypotheses (`P3/DelStepMdbA`'s `EmpMdbA`): + +* **`empty_slot_fbA`**: `MDB_FIRST_BADGED` (bit 49) of every other `OS` slot is kept, but `x`'s successor takes the + `or` with `x`'s; +* **`empty_slot_mdbWA`**: `EmpMdbA`, from it, A64 P1's `cdt.empty_slot.rvb` (bit 48) and `cdt.empty_slot.mdbFL` (the + links). + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.DelStepMdbA +import Fl4ProofsA64.P1.MdbEmpty +import Fl4ProofsA64.P1.MdbTreeU +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P0.Consts Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {T Ω Sg OS : Set U64} + +theorem fb_of_bitA {s s' : S} {y : U64} (h : L.mem s' (wa y 24#u64) &&& Fl4ProofsA64.P1.W7.R.ST.FB = L.mem s (wa y 24#u64) &&& Fl4ProofsA64.P1.W7.R.ST.FB) : + fbA st s' y = fbA st s y := by + unfold fbA; rw [h] + +theorem revA_congr_bitA {s s' : S} {y : U64} + (h : L.mem s' (wa y 24#u64) &&& RVB = L.mem s (wa y 24#u64) &&& RVB) : Fl4ProofsA64.P1.W7.R.ST.revA st s' y = Fl4ProofsA64.P1.W7.R.ST.revA st s y := by + unfold Fl4ProofsA64.P1.W7.R.ST.revA; unfold RVB at h; rw [h] + +theorem fb_setA (w : U64) : ((((w &&& ~~~ Fl4ProofsA64.P1.W7.R.ST.FB) ||| Fl4ProofsA64.P1.W7.R.ST.FB) &&& Fl4ProofsA64.P1.W7.R.ST.FB) != 0#u64) = true := by + have e : ((w &&& ~~~ Fl4ProofsA64.P1.W7.R.ST.FB) ||| Fl4ProofsA64.P1.W7.R.ST.FB) &&& Fl4ProofsA64.P1.W7.R.ST.FB = Fl4ProofsA64.P1.W7.R.ST.FB := by + rw [UScalar.eq_equiv_bv_eq] + apply BitVec.eq_of_getLsbD_eq; intro i + simp only [UScalar.bv_and, UScalar.bv_or, UScalar.bv_not, BitVec.getLsbD_and, BitVec.getLsbD_or, + BitVec.getLsbD_not] + cases w.bv.getLsbD i <;> cases (Fl4ProofsA64.P1.W7.R.ST.FB : U64).bv.getLsbD i <;> simp + rw [e]; rfl + +theorem fb_clrA (w : U64) : ((((w &&& ~~~ Fl4ProofsA64.P1.W7.R.ST.FB) ||| 0#u64) &&& Fl4ProofsA64.P1.W7.R.ST.FB) != 0#u64) = false := by + have e : ((w &&& ~~~ Fl4ProofsA64.P1.W7.R.ST.FB) ||| 0#u64) &&& Fl4ProofsA64.P1.W7.R.ST.FB = 0#u64 := by + rw [UScalar.eq_equiv_bv_eq] + apply BitVec.eq_of_getLsbD_eq; intro i + have z : (0#u64 : U64).bv.getLsbD i = false := by + have : (0#u64 : U64).bv = 0#64 := rfl + rw [this]; simp + simp only [UScalar.bv_and, UScalar.bv_or, UScalar.bv_not, BitVec.getLsbD_and, BitVec.getLsbD_or, + BitVec.getLsbD_not, z] + cases w.bv.getLsbD i <;> cases (Fl4ProofsA64.P1.W7.R.ST.FB : U64).bv.getLsbD i <;> simp + rw [e]; rfl + +set_option maxHeartbeats 2000000 in +/-- **`empty_slot` and bit 49** (module docstring). -/ +theorem empty_slot_fbA (hO : OSOK T Ω Sg OS) {s : S} {a : A} {x : U64} (hx : x ∈ OS) + {cl : capability.Cap} {Pw : U64 → Prop} (hpw : ∀ b, Pw b → ¬ InOS OS b) + (hpcd : ∀ (s1 : S) (a1 : A), L.ksb s1 = L.ksb s → + objects.post_cap_deletion st ar s1 a1 cl ⦃ r => WritesIn st s1 r.1 Pw ⦄) + (hn : capA st s x ≠ .Null) + (hpP : L.mem s (wa x 16#u64) ≠ 0#u64 → L.mem s (wa x 16#u64) ∈ OS) + (hnN : NxA st s x ≠ 0#u64 → NxA st s x ∈ OS) : + cdt.empty_slot st ar s a x cl ⦃ r => ∀ y ∈ OS, y ≠ x → + fbA st r.1 y = if NxA st s x ≠ 0#u64 ∧ y = NxA st s x then (fbA st s y || fbA st s x) + else fbA st s y ⦄ := by + unfold cdt.empty_slot + rw [capA_eq, bind_ok] + rw [is_null_f hn, bind_ok, if_neg (by simp)] + rw [slot_prev_eq, bind_ok, next_eq, bind_ok, slot_fb_eqA, bind_ok] + simp only [obj.Slot.is_null, bind_ok] + refine WP.spec_bind (Pₘ := fun s1 => (∀ y ∈ OS, L.mem s1 (wa y 24#u64) &&& Fl4ProofsA64.P1.W7.R.ST.FB = L.mem s (wa y 24#u64) &&& Fl4ProofsA64.P1.W7.R.ST.FB) ∧ + L.ksb s1 = L.ksb s) ?_ fun s1 h1 => ?_ + · split + · exact (WP.spec_ok _).2 ⟨fun _ _ => rfl, rfl⟩ + · rename_i hp0 + have hP0 : L.mem s (wa x 16#u64) ≠ 0#u64 := by simpa using hp0 + rw [set_next_eq, WP.spec_ok] + refine ⟨fun y hy => ?_, L.ksb_wr _ _ _⟩ + by_cases e : y = L.mem s (wa x 16#u64) + · rw [e, w24_self hO s (hpP hP0), ptr_bits _ _ _ _ fbc1] + · rw [w24_wr24 hO s (hpP hP0) hy e] + obtain ⟨b1, k1⟩ := h1 + refine WP.spec_bind (Pₘ := fun s2 => (∀ y ∈ OS, fbA st s2 y = + if NxA st s x ≠ 0#u64 ∧ y = NxA st s x then (fbA st s y || fbA st s x) else fbA st s y) ∧ + L.ksb s2 = L.ksb s) ?_ fun s2 h2 => ?_ + · split + · rename_i hn0 + have hN0 : NxA st s x = 0#u64 := by simpa using hn0 + refine (WP.spec_ok _).2 ⟨fun y hy => ?_, k1⟩ + rw [if_neg (fun h => h.1 hN0)] + exact fb_of_bitA (b1 y hy) + · rename_i hn0 + have hN0 : NxA st s x ≠ 0#u64 := by simpa using hn0 + have hNO := hnN hN0 + rw [set_prev_eq, bind_ok, slot_fb_eqA, bind_ok] + have e3 : fbA st (L.wr s1 (wa (NxA st s x) 16#u64) (L.mem s (wa x 16#u64))) (NxA st s x) = + fbA st s (NxA st s x) := fb_of_bitA (by rw [w24_wr16 hO s1 hNO hNO]; exact b1 _ hNO) + have key : ∀ (v : U64) (b : Bool), (v = Fl4ProofsA64.P1.W7.R.ST.FB ∨ v = 0#u64) → + (∀ w : U64, ((((w &&& ~~~ Fl4ProofsA64.P1.W7.R.ST.FB) ||| v) &&& Fl4ProofsA64.P1.W7.R.ST.FB) != 0#u64) = b) → + b = (fbA st s (NxA st s x) || fbA st s x) → + let s3 := L.wr s1 (wa (NxA st s x) 16#u64) (L.mem s (wa x 16#u64)) + let s2 := L.wr s3 (wa (NxA st s x) 24#u64) ((L.mem s3 (wa (NxA st s x) 24#u64) &&& ~~~ Fl4ProofsA64.P1.W7.R.ST.FB) ||| v) + (∀ y ∈ OS, fbA st s2 y = + if NxA st s x ≠ 0#u64 ∧ y = NxA st s x then (fbA st s y || fbA st s x) else fbA st s y) ∧ + L.ksb s2 = L.ksb s := by + intro v b hv hb hbe + refine ⟨fun y hy => ?_, by simp only [L.ksb_wr]; exact k1⟩ + by_cases e : y = NxA st s x + · rw [if_pos ⟨hN0, e⟩, e] + unfold fbA at hbe ⊢ + rw [w24_self hO _ hNO, hb, hbe] + · rw [if_neg (fun h => e h.2)] + exact fb_of_bitA (by rw [w24_wr24 hO _ hNO hy e, w24_wr16 hO s1 hNO hy]; exact b1 y hy) + by_cases hnf : fbA st (L.wr s1 (wa (NxA st s x) 16#u64) (L.mem s (wa x 16#u64))) (NxA st s x) = true + · rw [if_pos hnf, set_fb_eq_t] + exact (WP.spec_ok _).2 (key Fl4ProofsA64.P1.W7.R.ST.FB true (Or.inl rfl) fb_setA (by rw [← e3, hnf]; rfl)) + · rw [if_neg hnf] + have hnf' : fbA st s (NxA st s x) = false := by rw [← e3]; simpa using hnf + have run : ∀ (v : U64) (c : Bool), fbA st s x = c → v = (if c then Fl4ProofsA64.P1.W7.R.ST.FB else 0#u64) → + obj.Slot.set_first_badged st (NxA st s x) + (L.wr s1 (wa (NxA st s x) 16#u64) (L.mem s (wa x 16#u64))) (fbA st s x) = + ok (L.wr (L.wr s1 (wa (NxA st s x) 16#u64) (L.mem s (wa x 16#u64))) (wa (NxA st s x) 24#u64) + ((L.mem (L.wr s1 (wa (NxA st s x) 16#u64) (L.mem s (wa x 16#u64))) (wa (NxA st s x) 24#u64) &&& + ~~~ Fl4ProofsA64.P1.W7.R.ST.FB) ||| v)) := fun v c e ev => by + rw [e, ev] + cases c + · exact set_fb_eq_f _ _ + · exact set_fb_eq_t _ _ + by_cases hf : fbA st s x = true + · rw [run Fl4ProofsA64.P1.W7.R.ST.FB true hf rfl] + exact (WP.spec_ok _).2 (key Fl4ProofsA64.P1.W7.R.ST.FB true (Or.inl rfl) fb_setA (by rw [hf, hnf']; rfl)) + · have hf' : fbA st s x = false := by simpa using hf + rw [run 0#u64 false hf' rfl] + exact (WP.spec_ok _).2 (key 0#u64 false (Or.inr rfl) fb_clrA (by rw [hf', hnf']; rfl)) + obtain ⟨b2, k2⟩ := h2 + rw [clear_eq, bind_ok] + refine WP.spec_mono (hpcd _ a (by simp only [L.ksb_wr]; exact k2)) fun r hr => ?_ + intro y hy hne + have kx : SK st s2 + (L.wr (L.wr (L.wr (L.wr s2 x 0#u64) (wa x 8#u64) 0#u64) (wa x 16#u64) 0#u64) (wa x 24#u64) 0#u64) y := + (((SK.wr0 hO s2 hx hy hne _).trans (SK.wr hO _ hx hy hne (by simp) (by simp) _)).trans + (SK.wr hO _ hx hy hne (by simp) (by simp) _)).trans (SK.wr hO _ hx hy hne (by simp) (by simp) _) + rw [fb_of_bitA (st := st) (s := s2) (s' := r.1) (y := y) (by rw [(SK.of_w hO hr hpw hy).2.2.2, kx.2.2.2])] + exact b2 y hy + +/-- **`EmpMdbA` along `empty_slot`** (module docstring). -/ +theorem empty_slot_mdbWA (hO : OSOK T Ω Sg OS) (h0 : ∀ y ∈ OS, y ≠ 0#u64) {G : List (List U64)} {s : S} + (hM : MdbLk st OS G s) (hRO : RngOneA64 st G s) (hK : KindA st OS s) {a : A} {x : U64} (hx : x ∈ OS) + {cl : capability.Cap} {Pw : U64 → Prop} (hpw : ∀ b, Pw b → ¬ InOS OS b) + (hpcd : ∀ (s1 : S) (a1 : A), L.ksb s1 = L.ksb s → + objects.post_cap_deletion st ar s1 a1 cl ⦃ r => WritesIn st s1 r.1 Pw ⦄) + (hn : capA st s x ≠ .Null) : + cdt.empty_slot st ar s a x cl ⦃ r => EmpMdbA st G x s r.1 ⦄ := by + have inOS : ∀ y ∈ G.flatten, y ∈ OS := fun y hy => ((hM.members y).1 hy).1 + have F := hM.lkF h0 + have hxG : x ∈ G.flatten := (hM.members x).2 ⟨hx, hn⟩ + have hpP : L.mem s (wa x 16#u64) ≠ 0#u64 → L.mem s (wa x 16#u64) ∈ OS := fun h => inOS _ (F.pvm _ hxG h) + have hnN : NxA st s x ≠ 0#u64 → NxA st s x ∈ OS := fun h => inOS _ (F.nxm _ hxG h) + refine WP.spec_mono (WP.spec_and (WP.spec_and (cdt.empty_slot.mdbFL hO h0 hM hRO hK hx hpw hpcd) + (cdt.empty_slot.rvb hO hx hpw hpcd (fun _ => hpP) (fun _ => hnN))) + (empty_slot_fbA hO hx hpw hpcd hn hpP hnN)) ?_ + rintro r ⟨⟨⟨-, hnx, -⟩, hrv⟩, hfb⟩ + obtain ⟨hk, hp⟩ := hnx hn + refine ⟨fun y hy ne => revA_congr_bitA (hrv y (inOS y hy) ne), fun y hy ne => hfb y (inOS y hy) ne, + fun y hy ne => ?_⟩ + by_cases e : L.mem s (wa x 16#u64) ≠ 0#u64 ∧ y = L.mem s (wa x 16#u64) + · rw [if_pos e, e.2]; exact hp e.1 + · rw [if_neg e] + refine hk y (inOS y hy) ne fun h => e ⟨?_, h⟩ + rw [← h]; exact h0 y (inOS y hy) + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelNtfnBoundA.lean b/proofs/Fl4ProofsA64/P3/DelNtfnBoundA.lean new file mode 100644 index 000000000..9d1283536 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelNtfnBoundA.lean @@ -0,0 +1,157 @@ +/- +P3 (aarch64, delete hooks W3, contract §45 step 2, executor 18): **the endpoint relation and the spec's buffer +fields across the bound-TCB unlink** (`unbind_maybe_notification`'s successor, `irqUnbindNotificationTcb_someA`). +FL4 clears the notification's `BOUND_TCB` word (off `EFoot`) and the TCB's bound-notification word; the spec clears +the two links. `NtfnTcbKeepA` does not cover it (the bound notification changes), so this is its own frame. +No named premise; new module. +-/ +import Fl4ProofsA64.P3.DelNtfnEpA +import Fl4ProofsA64.P3.NtfnSendA +import Fl4ProofsA64.P3.ScRelA +import Fl4ProofsA64.P1.QueueObjs +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4SpecA64 Fl4ProofsA64.P3.Sched + +/-- The bound-TCB unlink keeps every TCB's IPC buffer fields. -/ +theorem unbindBound_bufA {σ : State Unit} {n t : ObjRef} {x : Notification} {y : Tcb} + (hk : σ.kheap n = some (.notification x)) (ht : σ.kheap t = some (.tcb y)) : + ∀ p z, σ.kheap p = some (.tcb z) → ∃ w, (updKSc (updKSc σ n (.notification { x with ntfnBoundTcb := none })) + t (.tcb { y with tcbBoundNotification := none })).kheap p = some (.tcb w) ∧ + w.tcbIpcframe = z.tcbIpcframe ∧ w.tcbIpcBuffer = z.tcbIpcBuffer := by + intro p z hp + by_cases e : p = t + · subst e + rw [ht] at hp; cases hp + exact ⟨{ y with tcbBoundNotification := none }, by rw [updKSc_kheap, Function.update_self], rfl, rfl⟩ + · have e' : p ≠ n := fun h => by rw [h, hk] at hp; cases hp + exact ⟨z, by rw [updKSc_kheap, Function.update_of_ne e, updKSc_kheap, Function.update_of_ne e', hp], rfl, rfl⟩ + +end Fl4ProofsA64.P3 + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched +open Fl4SpecA64 + +/-- **`EpStateRel` across the bound-TCB unlink.** -/ +theorem EpStateRel.unbindBound {N T E R : Set U64} {Bf : Set (U64 × Word)} {m m' : U64 → U64} + {σ : Fl4SpecA64.State Unit} (hR : EpStateRel T E R Bf m σ) (hP : IPlacedA N T E R Bf) {n t : U64} + (htT : t ∈ T) {x : Notification} {y : Tcb} + (hk : σ.kheap (specPtr n) = some (.notification x)) (ht : σ.kheap (specPtr t) = some (.tcb y)) + (hm : ∀ a, Aligned a → EFoot T E R Bf a → a ≠ tBoundA t → m' a = m a) (h0 : m' (tBoundA t) = 0#u64) : + EpStateRel T E R Bf m' (updKSc (updKSc σ (specPtr n) (.notification { x with ntfnBoundTcb := none })) + (specPtr t) (.tcb { y with tcbBoundNotification := none })) := by + have hta := hP.e.tcb t htT + have kh : ∀ p, p ≠ specPtr n → p ≠ specPtr t → + (updKSc (updKSc σ (specPtr n) (.notification { x with ntfnBoundTcb := none })) + (specPtr t) (.tcb { y with tcbBoundNotification := none })).kheap p = σ.kheap p := fun p h1 h2 => by + rw [updKSc_kheap, Function.update_of_ne h2, updKSc_kheap, Function.update_of_ne h1] + have off : ∀ a : U64, (a.val < t.val ∨ t.val + 4096 ≤ a.val) → a ≠ tBoundA t := fun a h e => by + have := val_bound hta + rw [← e] at this + omega + have inT : ∀ u ∈ T, ∀ a : U64, u.val ≤ a.val → a.val < u.val + 4096 → a.val ≠ u.val + 3368 → a ≠ tBoundA t := by + intro u hu a h1 h2 h3 + by_cases e : u = t + · subst e + intro h + rw [h, val_bound hta] at h3 + exact h3 rfl + · have := tcb_disj hP.e htT hu e + exact off a (by omega) + have hl : ∀ u ∈ T, ∀ a, Aligned a → TL u a → m' a = m a := fun u hu a ha h => + hm a ha (Or.inr (Or.inr (Or.inl ⟨u, hu, Or.inr h⟩))) + (inT u hu a (by unfold TL at h; omega) (by unfold TL at h; omega) (by unfold TL at h; omega)) + have hf : ∀ u ∈ T, ∀ a, Aligned a → TF u a → a.val ≠ u.val + 3368 → m' a = m a := fun u hu a ha h hne => + hm a ha (Or.inr (Or.inr (Or.inl ⟨u, hu, Or.inl h⟩))) (inT u hu a h.1 (by have := TF.range h; omega) hne) + have tcbR : ∀ u ∈ T, ∀ w z : Tcb, TcbRelE R m u w → z.tcbState = w.tcbState → z.tcbArch = w.tcbArch → + z.tcbFault = w.tcbFault → z.tcbPriority = w.tcbPriority → + z.tcbBoundNotification = optRef (m' (tBoundA u)) → TcbRelE R m' u z := by + intro u hu w z hx es ea ef ep eb + have hua := hP.e.tcb u hu + have wd : ∀ a, Aligned a ∧ TF u a → a.val ≠ u.val + 3368 → m' a = m a := fun a h hne => hf u hu a h.1 h.2 hne + have e_st : m' (stA u) = m (stA u) := wd _ (a_st hua) (by rw [val_st hua]; omega) + have e_blk : m' (tBlockA u) = m (tBlockA u) := wd _ (a_blk hua) (by rw [val_blk hua]; omega) + have e_badge : m' (tBadgeA u) = m (tBadgeA u) := wd _ (a_badge hua) (by rw [val_badge hua]; omega) + have e_flags : m' (tFlagsA u) = m (tFlagsA u) := wd _ (a_flags hua) (by rw [val_flags hua]; omega) + have e_reply : m' (tReplyA u) = m (tReplyA u) := wd _ (a_reply hua) (by rw [val_reply hua]; omega) + have e_fault : m' (tFaultA u) = m (tFaultA u) := wd _ (a_fault hua) (by rw [val_fault hua]; omega) + have e_prio : m' (prioA u) = m (prioA u) := wd _ (a_prio hua) (by rw [val_prio hua]; omega) + refine ⟨by rw [e_st, es]; exact hx.st, fun e h => by rw [e_blk]; exact hx.blk e (es ▸ h), + fun e d h => by rw [e_badge, e_flags]; exact hx.send e d (es ▸ h), + fun e r d h => by rw [e_reply]; exact hx.recv e r d (es ▸ h), + fun r h => by rw [e_reply]; exact hx.reply r (es ▸ h), fun r => ?_, + by rw [e_prio, ep]; exact hx.prio, by rw [e_fault, ef]; exact hx.fault0, + by rw [e_fault, ef]; exact hx.faultT, eb, ?_⟩ + · rw [wd _ (a_reg hua r) (by rw [regA_val hua (regIdxA_lt r)]; have := regIdxA_lt r; omega), ea] + exact hx.regs r + · rw [ef] + exact hx.faultR.congr e_fault (wd _ (a_fw0 hua) (by rw [val_fw0 hua]; omega)) + (wd _ (a_fw1 hua) (by rw [val_fw1 hua]; omega)) (wd _ (a_lf hua) (by rw [val_lf hua]; omega)) + (wd _ (a_lf0 hua) (by rw [val_lf0 hua]; omega)) (wd _ (a_lf1 hua) (by rw [val_lf1 hua]; omega)) + (wd _ (a_fw2 hua) (by rw [val_fw2 hua]; omega)) + refine ⟨fun e he => ?_, fun r hr => ?_, fun u hu => ?_, fun bp hb k hk' => ?_⟩ + · obtain ⟨z, hkz, hx⟩ := hR.ep e he + have hew : ∀ a : U64, (a.val = e.val ∨ a.val = e.val + 8) → a ≠ tBoundA t := fun a ha => by + have := hP.e.et e he t htT + exact off a (by omega) + refine ⟨z, (kh _ (fun h => by rw [h, hk] at hkz; cases hkz) (fun h => by rw [h, ht] at hkz; cases hkz)).trans hkz, + hx.frame (hm _ (a_head hP.e he).1 (a_head hP.e he).2 (hew _ (Or.inl (val_head e)))) + (hm _ (a_ts hP.e he).1 (a_ts hP.e he).2 (hew _ (Or.inr (val_ts hP.e he)))) fun u hu _ => ?_⟩ + have hua := hP.e.tcb u hu + exact ⟨hf u hu _ (a_st hua).1 (a_st hua).2 (by rw [val_st hua]; omega), + hf u hu _ (a_blk hua).1 (a_blk hua).2 (by rw [val_blk hua]; omega), + hl u hu _ (a_prev hua).1 (a_prev hua).2, hl u hu _ (a_next hua).1 (a_next hua).2⟩ + · obtain ⟨z, hkz, hx⟩ := hR.reply r hr + refine ⟨z, (kh _ (fun h => by rw [h, hk] at hkz; cases hkz) (fun h => by rw [h, ht] at hkz; cases hkz)).trans hkz, + ?_⟩ + have := hP.e.rt r hr t htT + rw [hx, hm _ (a_rtcb hP.e hr).1 (a_rtcb hP.e hr).2 (off _ (by rw [rTcbA_val]; omega))] + · obtain ⟨w, hkw, hx⟩ := hR.tcb u hu + by_cases e : u = t + · subst e + rw [ht] at hkw; cases hkw + refine ⟨{ y with tcbBoundNotification := none }, by rw [updKSc_kheap, Function.update_self], + tcbR u hu y _ hx rfl rfl rfl rfl ?_⟩ + rw [h0, optRef_zero] + · have e1 : specPtr u ≠ specPtr t := fun h => e (specPtr_inj h) + have e2 : specPtr u ≠ specPtr n := fun h => by rw [h, hk] at hkw; cases hkw + have hua := hP.e.tcb u hu + have eb : m' (tBoundA u) = m (tBoundA u) := by + refine hm _ (a_bound hua).1 (Or.inr (Or.inr (Or.inl ⟨u, hu, Or.inl (a_bound hua).2⟩))) ?_ + have := tcb_disj hP.e htT hu e + exact off _ (by rw [val_bound hua]; omega) + exact ⟨w, (kh _ e2 e1).trans hkw, tcbR u hu w w hx rfl rfl rfl rfl (by rw [eb]; exact hx.bound)⟩ + · have hw : BufW Bf (bufW bp.1 k) := ⟨bp, hb, k, hk', rfl⟩ + rw [hm _ (a_buf hP.e hb hk').1 (a_buf hP.e hb hk').2 (off _ (hP.e.bt _ hw t htT))] + exact hR.buf bp hb k hk' + +/-- **`EpStateRel` after FL4's two writes** of the bound-TCB unlink (the successor of +`irqUnbindNotificationTcb_someA`). -/ +theorem EpStateRel.unbindBound_wr {S : Type} {st : machine.Store S} [L : LawfulStore st] + {N T E R : Set U64} {Bf : Set (U64 × Word)} {s : S} {σ : Fl4SpecA64.State Unit} + (hR : EpStateRel T E R Bf (L.mem s) σ) (hP : IPlacedA N T E R Bf) {n t : U64} (hn : n ∈ N) (htT : t ∈ T) + {x : Notification} {y : Tcb} + (hk : σ.kheap (specPtr n) = some (.notification x)) (ht : σ.kheap (specPtr t) = some (.tcb y)) : + EpStateRel T E R Bf (L.mem (L.wr (L.wr s (ntfnBtA n) 0#u64) (boundA t) 0#u64)) + (updKSc (updKSc σ (specPtr n) (.notification { x with ntfnBoundTcb := none })) + (specPtr t) (.tcb { y with tcbBoundNotification := none })) := by + have hta := hP.e.tcb t htT + have hna := hP.n.ntfn n hn + have eb : boundA t = tBoundA t := rfl + have aB : Aligned (boundA t) := eb ▸ (a_bound hta).1 + have h24 : (layout.ntfn.BOUND_TCB).val = 24 := by simp [layout.ntfn.BOUND_TCB] + have vN : (ntfnBtA n).val = n.val + 24 := by + unfold ntfnBtA + rw [nfield_val hna (by omega), h24] + have aN : Aligned (ntfnBtA n) := by + unfold ntfnBtA + exact nfield_aligned hna (by omega) (by omega) + refine hR.unbindBound hP htT hk ht (fun a ha hf hne => ?_) ?_ + · rw [L.mem_wr_ne aB ha (fun h => hne (eb ▸ (UScalar.eq_of_val_eq h).symm)), + L.mem_wr_ne aN ha (fun h => nw_not_efoot hP hn (by omega) (by omega) hf)] + · rw [← eb] + exact L.mem_wr_same aB + +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/DelNtfnBufA.lean b/proofs/Fl4ProofsA64/P3/DelNtfnBufA.lean new file mode 100644 index 000000000..be81e2a1c --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelNtfnBufA.lean @@ -0,0 +1,51 @@ +/- +P3 (aarch64, delete hooks W3, contract §45 step 2, executor 18): **`BufCover` across a finaliser step that rewrites +TCBs**. `BufCover.frameK` is the sibling of `BufCover.stepA` (`P3/DelBufKeepA`) for a step whose spec side is not +slot-only (`cancel_all_signals` changes thread states): it asks the buffer address words and buffer slot caps of the +tracked TCBs to be kept, and the spec TCBs to keep their IPC buffer fields (`NtfnTcbKeepA.buf` for the restart +steps; the bound-notification unlink also does). +No named premise; new module. +-/ +import Fl4ProofsA64.P3.DelBufKeepA +import Fl4ProofsA64.P3.SigTcbKeepA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 Fl4ProofsA64.P3.Endp + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {T : Set U64} + {Bf : Set (U64 × Fl4SpecA64.Word)} {s r : S} {σ σ' : Fl4SpecA64.State Unit} + +/-- `BufCover` across a step keeping the buffer words and slot caps, with the spec's buffer fields kept. -/ +theorem BufCover.frameK (h : BufCover st T Bf s σ) + (ib : ∀ t ∈ T, L.mem r (ibWordA t) = L.mem s (ibWordA t)) + (k : ∀ t ∈ T, capA st r (sbWordA t) = capA st s (sbWordA t)) + (hs : ∀ t ∈ T, ∃ x, σ.kheap (specPtr t) = some (.tcb x)) + (K : ∀ p x, σ.kheap p = some (.tcb x) → ∃ y, σ'.kheap p = some (.tcb y) ∧ + y.tcbIpcframe = x.tcbIpcframe ∧ y.tcbIpcBuffer = x.tcbIpcBuffer) : BufCover st T Bf r σ' := by + intro t ht x' b sb w hx' hb hl + obtain ⟨x, hx⟩ := hs t ht + obtain ⟨y, hy, hf, hbuf⟩ := K _ _ hx + rw [hx'] at hy; cases hy + have hb0 : ipcBufOfA b x = some sb := by + rw [← hb]; unfold ipcBufOfA; rw [hf, hbuf] + have hlk : msg.lookup_ipc_buffer st r b t = msg.lookup_ipc_buffer st s b t := by + have hsl : obj.Tcb.slot t layout.tcb.SLOT_BUFFER = ok (sbWordA t) := by simp [obj.Tcb.slot, lift, sbWordA] + unfold msg.lookup_ipc_buffer + rw [ipc_buffer_word, ipc_buffer_word, ib t ht, hsl] + simp only [bind_ok] + rw [Fl4ProofsA64.P1.W7.R.ST.capA_eq, Fl4ProofsA64.P1.W7.R.ST.capA_eq, k t ht] + rw [hlk] at hl + exact h t ht x b sb w hx hb0 hl + +end + +/-- The restart steps' keep gives `BufCover.frameK`'s spec premise. -/ +theorem NtfnTcbKeepA.buf {σ σ' : Fl4SpecA64.State Unit} (K : NtfnTcbKeepA σ σ') : + ∀ p x, σ.kheap p = some (.tcb x) → ∃ y, σ'.kheap p = some (.tcb y) ∧ + y.tcbIpcframe = x.tcbIpcframe ∧ y.tcbIpcBuffer = x.tcbIpcBuffer := fun p x h => by + obtain ⟨y, hy, -, -, -, -, hf, hb, -⟩ := K.tcb p x h + exact ⟨y, hy, hf, hb⟩ + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelNtfnEntA.lean b/proofs/Fl4ProofsA64/P3/DelNtfnEntA.lean new file mode 100644 index 000000000..942de0188 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelNtfnEntA.lean @@ -0,0 +1,122 @@ +/- +P3 (AArch64, delete hooks W3): `EntryInvA3` across the Notification finaliser. + +* `JStage3TA.ent_stepNWA`: `EntryInvA3` across any step that writes only scheduler / IPC-object words and + re-establishes `BlkOK` (the VSpace words are outside `SchedStepNWA`; `UserOK` reads `ksb` only). +* `JStage3TA.blkA`: the record's `BXI` as `BlkOK`. +* `JStage3TA.ntfnFin_blkA`: `BlkOK` after `finalise_cap_object` of a notification cap (A64 P1's `fin_ntfn.blk`). + +No premise. +-/ +import Fl4ProofsA64.P3.Stage3BA +import Fl4ProofsA64.P1.QueueFinal +import Fl4ProofsA64.P3.DelNtfnLoopFrA +import Fl4ProofsA64.P3.DelNtfnBoundA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 Fl4ProofsA64.P3.Endp +open Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Notif + +local notation "wa" => core.num.U64.wrapping_add + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s s' : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} [J7PpA st J7] + +/-- The record's `BXI` as `BlkOK`. -/ +theorem JStage3BA.blkA + (J : JStage3BA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) : + BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s := by + obtain ⟨b1, b2, b3, b4, b5⟩ := J.ent.bxi + exact ⟨b1, b2, b3, b5, b4⟩ + +/-- `JStage3BA.blkA` at `JStage3TA`. -/ +theorem JStage3TA.blkA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) : + BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s := + J.toBA.blkA + +/-- **`EntryInvA3` across a scheduler / IPC-object write step** that re-establishes `BlkOK`. -/ +theorem JStage3BA.ent_stepNWA + (J : JStage3BA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (w : WritesIn st s s' (SchedStepNWA (st := st) N E Rp Z m σ s)) + (blk : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s') : + EntryInvA3 st H Z.Cs OS U Ac Pp Tb Lv s' := by + have kb : L.ksb s' = L.ksb s := w.1 + refine ppEntA3 J.ent blk kb (utVsPt_keepA J.ent.vs kb fun b hb => ?_).1 + have vr := J.vsOk + refine w.mem_eq (vr.al _ hb) ?_ + by_cases h : AsW (L.ksb s) b + · obtain ⟨o, o1, o2, -, e⟩ := h + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.ent.ksw + have ev : b.val = (L.ksb s).val + o.val := by + rw [e, wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact J.asid_offStepNA (by omega) (by omega) + · exact J.vsr_offStepNA vr hb h + +/-- `JStage3BA.ent_stepNWA` at `JStage3TA`. -/ +theorem JStage3TA.ent_stepNWA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (w : WritesIn st s s' (SchedStepNWA (st := st) N E Rp Z m σ s)) + (blk : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s') : + EntryInvA3 st H Z.Cs OS U Ac Pp Tb Lv s' := + J.toBA.ent_stepNWA w blk + +/-- **`BlkOK` after the Notification finaliser** (A64 P1's `fin_ntfn.blk`). -/ +theorem JStage3TA.ntfnFin_blkA {ar : machine.Arch A} + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {p b0 : U64} {b1 b2 fin : Bool} (hp : p ∈ H.ntfns) (hH : H p = some .ntfn) : + objects.finalise_cap_object st ar s a (.Notification p b0 b1 b2) fin ⦃ r => + r.1 = (.Null, .Null) ∧ r.2.2 = a ∧ + BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac r.2.1 ⦄ := + WP.spec_mono (Fl4ProofsA64.P1.fin_ntfn.blk s a p b0 b1 b2 fin J.blkA hp hH) + fun _ h => ⟨h.1, h.2.1, h.2.2.1⟩ + +/-- **The bound-TCB unlink's two writes** are scheduler / IPC-object writes (the write set of +`irqUnbindNotificationTcb_someA`). -/ +theorem JStage3TA.unbindTcb_wA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {p t : U64} (hp : p ∈ N) (ht : t ∈ Z.T) : + WritesIn st s (L.wr (L.wr s (ntfnBtA p) 0#u64) (boundA t) 0#u64) + (SchedStepNWA (st := st) N E Rp Z m σ s) := by + have hna := J.iplaced.n.ntfn p hp + have hta := J.iplaced.e.tcb t ht + have eb : boundA t = tBoundA t := rfl + have aB : Aligned (boundA t) := eb ▸ (a_bound hta).1 + have h24 : (layout.ntfn.BOUND_TCB).val = 24 := by simp [layout.ntfn.BOUND_TCB] + have vN : (ntfnBtA p).val = p.val + 24 := by + unfold ntfnBtA + rw [nfield_val hna (by omega), h24] + have aN : Aligned (ntfnBtA p) := by + unfold ntfnBtA + exact nfield_aligned hna (by omega) (by omega) + have wN : SchedStepNWA (st := st) N E Rp Z m σ s (ntfnBtA p) := + Or.inr (Or.inl ⟨p, hp, by omega, by omega⟩) + have wB : SchedStepNWA (st := st) N E Rp Z m σ s (boundA t) := by + have hfit := (J.sr.core.tcbs t ht).fits + have hv : ∀ K : U64, K.val < 4096 → (wa t K).val = t.val + K.val := fun K hK => by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have e : boundA t = wa t 3368#u64 := rfl + have v : (boundA t).val = t.val + 3368 := by rw [e, hv _ (by simp)]; simp + refine Or.inl (Or.inr ⟨t, ht, by omega, by omega, fun h => ?_, fun h => ?_⟩) + · have := congrArg (fun w : U64 => w.val) h + simp only [v, hv 3664#u64 (by simp)] at this + simp at this + · unfold runVcpuA at h + have := congrArg (fun w : U64 => w.val) h + simp only [v, hv 3672#u64 (by simp)] at this + simp at this + refine ⟨by rw [L.ksb_wr, L.ksb_wr], by rw [L.cpu_wr, L.cpu_wr], fun b hb hn => ?_⟩ + rw [L.mem_wr_ne aB hb (fun h => hn (UScalar.eq_of_val_eq h ▸ wB)), + L.mem_wr_ne aN hb (fun h => hn (UScalar.eq_of_val_eq h ▸ wN))] + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelNtfnEpA.lean b/proofs/Fl4ProofsA64/P3/DelNtfnEpA.lean new file mode 100644 index 000000000..cd73860aa --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelNtfnEpA.lean @@ -0,0 +1,132 @@ +/- +P3 (aarch64, delete hooks W3, contract §45 step 1, executor 18): **the endpoint relation across +`cancel_all_signals`**. `EpStateRel.frameN` is the sibling of `EpStateRel.frameX` (`P3/IpcCancelA`) for a step that +restarts notification waiters: FL4 changes the state word and the `Ep` links of a set `Q` of TCBs blocked on the +notification `n` (`SigWrA`), the spec keeps what `NtfnTcbKeepA` says, and the new state words are read off +`NtfnStateRel`. No waiter is on an endpoint queue (its block object is `n ∉ E`), so no endpoint reads a written word. +No named premise; new module. +-/ +import Fl4ProofsA64.P3.IpcCancelA +import Fl4ProofsA64.P3.EpWriteA +import Fl4ProofsA64.P3.SigFootA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4ProofsA64.P3.Notif +open Fl4SpecA64 + +/-- A word of the TCB at `y` is none of the words the loop writes in another TCB. -/ +private theorem otherTcbN {T E R : Set U64} {Bf : Set (U64 × Word)} (hP : EPlaced T E R Bf) {y q a : U64} + (hy : y ∈ T) (hq : q ∈ T) (hne : y ≠ q) (h1 : y.val ≤ a.val) (h2 : a.val < y.val + 4096) : + a ≠ stA q ∧ a ≠ nextA .Ep q ∧ a ≠ prevA .Ep q := by + have hqa := hP.tcb q hq + have := tcb_disj hP hy hq hne.symm + refine ⟨fun e => ?_, fun e => ?_, fun e => ?_⟩ + · have := val_st hqa; rw [← e] at this; omega + · have := val_next hqa; rw [← e] at this; omega + · have := val_prev hqa; rw [← e] at this; omega + +/-- A word outside every TCB is none of the written TCB words. -/ +private theorem offTcbN {T E R : Set U64} {Bf : Set (U64 × Word)} (hP : EPlaced T E R Bf) {q a : U64} + (hq : q ∈ T) (h : a.val < q.val ∨ q.val + 4096 ≤ a.val) : + a ≠ stA q ∧ a ≠ nextA .Ep q ∧ a ≠ prevA .Ep q := by + have hqa := hP.tcb q hq + refine ⟨fun e => ?_, fun e => ?_, fun e => ?_⟩ + · have := val_st hqa; rw [← e] at this; omega + · have := val_next hqa; rw [← e] at this; omega + · have := val_prev hqa; rw [← e] at this; omega + +/-- **`EpStateRel` across the restart of the waiters of a notification.** -/ +theorem EpStateRel.frameN {N T E R : Set U64} {Bf : Set (U64 × Word)} {m m' : U64 → U64} + {σ σ' : Fl4SpecA64.State Unit} (hR : EpStateRel T E R Bf m σ) (hP : IPlacedA N T E R Bf) {n : U64} (hn : n ∈ N) + {Q : Set U64} (hQ : ∀ q ∈ Q, q ∈ T ∧ m (tBlockA q) = n) + (hm : ∀ a, Aligned a → EFoot T E R Bf a → ¬ SigWrA Q n a → m' a = m a) + (hst : ∀ t ∈ T, ∀ z, σ'.kheap (specPtr t) = some (.tcb z) → (m' (stA t)).val = tsCode z.tcbState) + (hk : NtfnTcbKeepA σ σ') : EpStateRel T E R Bf m' σ' := by + have keep : ∀ a, Aligned a → EFoot T E R Bf a → + (∀ q ∈ Q, a ≠ stA q ∧ a ≠ nextA .Ep q ∧ a ≠ prevA .Ep q) → m' a = m a := by + intro a ha hf h + refine hm a ha hf ?_ + rintro (⟨h1, h2⟩ | ⟨q, hq, e | e | e⟩) + · exact nw_not_efoot hP hn h1 h2 hf + · exact (h q hq).1 e + · exact (h q hq).2.1 e + · exact (h q hq).2.2 e + have hTF : ∀ y ∈ T, ∀ a, Aligned a → TF y a → a ≠ stA y → m' a = m a := by + intro y hy a ha hf hne + refine keep a ha (Or.inr (Or.inr (Or.inl ⟨y, hy, Or.inl hf⟩))) fun q hq => ?_ + by_cases e : y = q + · subst e + have hya := hP.e.tcb y hy + exact ⟨hne, fun e => TL.not_tf (e ▸ (a_next hya).2) hf, fun e => TL.not_tf (e ▸ (a_prev hya).2) hf⟩ + · exact otherTcbN hP.e hy (hQ q hq).1 e hf.1 (by have := TF.range hf; omega) + refine ⟨fun e he => ?_, fun r hr => ?_, fun t ht => ?_, fun bp hb k hk' => ?_⟩ + · obtain ⟨x, hkx, hx⟩ := hR.ep e he + have hew : ∀ a : U64, (a.val = e.val ∨ a.val = e.val + 8) → + ∀ q ∈ Q, a ≠ stA q ∧ a ≠ nextA .Ep q ∧ a ≠ prevA .Ep q := by + intro a ha q hq + have := hP.e.et e he q (hQ q hq).1 + exact offTcbN hP.e (hQ q hq).1 (by omega) + refine ⟨x, hk.ep _ _ hkx, hx.frame (keep _ (a_head hP.e he).1 (a_head hP.e he).2 (hew _ (Or.inl (val_head e)))) + (keep _ (a_ts hP.e he).1 (a_ts hP.e he).2 (hew _ (Or.inr (val_ts hP.e he)))) fun y hy hq => ?_⟩ + have hya := hP.e.tcb y hy + have hb : m (tBlockA y) = e := (hx.blocked hq).2 + have hyQ : y ∉ Q := fun h => by + have e1 : e = n := hb.symm.trans (hQ y h).2 + have := hP.ne n hn e he + rw [e1] at this + omega + have hw : ∀ a : U64, y.val ≤ a.val → a.val < y.val + 4096 → + ∀ q ∈ Q, a ≠ stA q ∧ a ≠ nextA .Ep q ∧ a ≠ prevA .Ep q := fun a h1 h2 q hq => + otherTcbN hP.e hy (hQ q hq).1 (fun e => hyQ (e ▸ hq)) h1 h2 + have hl : ∀ a, Aligned a → TL y a → m' a = m a := fun a ha hl => by + refine keep a ha (Or.inr (Or.inr (Or.inl ⟨y, hy, Or.inr hl⟩))) (hw a ?_ ?_) <;> + (unfold TL at hl; omega) + have hf : ∀ a, Aligned a → TF y a → m' a = m a := fun a ha hf => + keep a ha (Or.inr (Or.inr (Or.inl ⟨y, hy, Or.inl hf⟩))) (hw a hf.1 (by have := TF.range hf; omega)) + exact ⟨hf _ (a_st hya).1 (a_st hya).2, hf _ (a_blk hya).1 (a_blk hya).2, + hl _ (a_prev hya).1 (a_prev hya).2, hl _ (a_next hya).1 (a_next hya).2⟩ + · obtain ⟨x, hkx, hx⟩ := hR.reply r hr + refine ⟨x, hk.reply _ _ hkx, ?_⟩ + rw [hx, keep _ (a_rtcb hP.e hr).1 (a_rtcb hP.e hr).2 fun q hq => ?_] + have := hP.e.rt r hr q (hQ q hq).1 + exact offTcbN hP.e (hQ q hq).1 (by rw [rTcbA_val]; omega) + · obtain ⟨x, hkx, hx⟩ := hR.tcb t ht + obtain ⟨z, hkz, ka, kf, kb, kp, -, -, ks⟩ := hk.tcb _ _ hkx + have hta := hP.e.tcb t ht + have w : ∀ a, Aligned a ∧ TF t a → a.val ≠ t.val + 3328 → m' a = m a := fun a h hne => + hTF t ht a h.1 h.2 (fun e => hne (by rw [e]; exact val_st hta)) + have e_blk : m' (tBlockA t) = m (tBlockA t) := w _ (a_blk hta) (by rw [val_blk hta]; omega) + have e_badge : m' (tBadgeA t) = m (tBadgeA t) := w _ (a_badge hta) (by rw [val_badge hta]; omega) + have e_flags : m' (tFlagsA t) = m (tFlagsA t) := w _ (a_flags hta) (by rw [val_flags hta]; omega) + have e_reply : m' (tReplyA t) = m (tReplyA t) := w _ (a_reply hta) (by rw [val_reply hta]; omega) + have e_bound : m' (tBoundA t) = m (tBoundA t) := w _ (a_bound hta) (by rw [val_bound hta]; omega) + have e_fault : m' (tFaultA t) = m (tFaultA t) := w _ (a_fault hta) (by rw [val_fault hta]; omega) + have e_prio : m' (prioA t) = m (prioA t) := w _ (a_prio hta) (by rw [val_prio hta]; omega) + refine ⟨z, hkz, ⟨hst t ht z hkz, fun e hb => ?_, fun e d hs => ?_, fun e r d hs => ?_, fun r hs => ?_, + fun r => ?_, by rw [e_prio, kp]; exact hx.prio, by rw [e_fault, kf]; exact hx.fault0, + by rw [e_fault, kf]; exact hx.faultT, by rw [e_bound, kb]; exact hx.bound, ?_⟩⟩ + · rcases ks with h | ⟨-, h⟩ + · rw [e_blk]; exact hx.blk e (h ▸ hb) + · rw [h] at hb; cases hb + · rcases ks with h | ⟨-, h⟩ + · rw [e_badge, e_flags]; exact hx.send e d (h ▸ hs) + · rw [h] at hs; cases hs + · rcases ks with h | ⟨-, h⟩ + · rw [e_reply]; exact hx.recv e r d (h ▸ hs) + · rw [h] at hs; cases hs + · rcases ks with h | ⟨-, h⟩ + · rw [e_reply]; exact hx.reply r (h ▸ hs) + · rw [h] at hs; cases hs + · rw [w _ (a_reg hta r) (by rw [regA_val hta (regIdxA_lt r)]; have := regIdxA_lt r; omega), ka]; exact hx.regs r + · rw [kf] + exact hx.faultR.congr e_fault (w _ (a_fw0 hta) (by rw [val_fw0 hta]; omega)) + (w _ (a_fw1 hta) (by rw [val_fw1 hta]; omega)) (w _ (a_lf hta) (by rw [val_lf hta]; omega)) + (w _ (a_lf0 hta) (by rw [val_lf0 hta]; omega)) (w _ (a_lf1 hta) (by rw [val_lf1 hta]; omega)) + (w _ (a_fw2 hta) (by rw [val_fw2 hta]; omega)) + · have hw : BufW Bf (bufW bp.1 k) := ⟨bp, hb, k, hk', rfl⟩ + rw [keep _ (a_buf hP.e hb hk').1 (a_buf hP.e hb hk').2 fun q hq => ?_, hk.um] + · exact hR.buf bp hb k hk' + · exact offTcbN hP.e (hQ q hq).1 (hP.e.bt _ hw q (hQ q hq).1) + +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/DelNtfnFrameA.lean b/proofs/Fl4ProofsA64/P3/DelNtfnFrameA.lean new file mode 100644 index 000000000..e3ed4f53a --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelNtfnFrameA.lean @@ -0,0 +1,154 @@ +/- +P3 (AArch64, delete hooks W3, contract §42-§43): **the spec side of the finaliser frames** shared by the Notification, +IrqHandler, Endpoint and Reply hooks. `NtfnSpecKeepA σ σ'` records what the notification finaliser's spec steps keep +of the objects that `mcp`, `yieldRel` and the reply stacks read; each relation is then framed from it and from the +concrete words it reads. Instance for the bound-TCB unlink. No named premise. New module. +-/ +import Fl4ProofsA64.P3.IrqInvokeUnbindA +import Fl4ProofsA64.P3.ScYieldRelA +import Fl4ProofsA64.P3.IpcScPushA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Sched Fl4SpecA64 + +/-- One object replaced by another that agrees on what the framed relations read. -/ +def NtfnObjKeepA : KernelObject → KernelObject → Prop + | .notification _, o' => ∃ y, o' = .notification y + | .tcb x, o' => ∃ y, o' = .tcb y ∧ y.tcbMcpriority = x.tcbMcpriority ∧ y.tcbYieldTo = x.tcbYieldTo ∧ + y.tcbFlags = x.tcbFlags + | .schedContext x n, o' => ∃ y, o' = .schedContext y n ∧ y.scReplies = x.scReplies ∧ y.scYieldFrom = x.scYieldFrom + | _, _ => False + +/-- Heaps related object by object: unchanged, or replaced as `NtfnObjKeepA` allows. -/ +def NtfnSpecKeepA (σ σ' : State Unit) : Prop := + ∀ p, σ'.kheap p = σ.kheap p ∨ ∃ o o', σ.kheap p = some o ∧ σ'.kheap p = some o' ∧ NtfnObjKeepA o o' + +theorem NtfnObjKeepA.trans {a b c : KernelObject} (h1 : NtfnObjKeepA a b) (h2 : NtfnObjKeepA b c) : + NtfnObjKeepA a c := by + cases a <;> simp only [NtfnObjKeepA] at h1 ⊢ + · obtain ⟨y, rfl, e1, e2, e3⟩ := h1 + obtain ⟨z, rfl, f1, f2, f3⟩ := h2 + exact ⟨z, rfl, f1.trans e1, f2.trans e2, f3.trans e3⟩ + · obtain ⟨y, rfl⟩ := h1 + exact h2 + · obtain ⟨y, rfl, e1, e2⟩ := h1 + obtain ⟨z, rfl, f1, f2⟩ := h2 + exact ⟨z, rfl, f1.trans e1, f2.trans e2⟩ + +theorem NtfnSpecKeepA.refl (σ : State Unit) : NtfnSpecKeepA σ σ := fun _ => Or.inl rfl + +theorem NtfnSpecKeepA.trans {σ1 σ2 σ3 : State Unit} (h1 : NtfnSpecKeepA σ1 σ2) (h2 : NtfnSpecKeepA σ2 σ3) : + NtfnSpecKeepA σ1 σ3 := fun p => by + rcases h1 p with e1 | ⟨a, b, ha, hb, k1⟩ <;> rcases h2 p with e2 | ⟨c, d, hc, hd, k2⟩ + · exact Or.inl (e2.trans e1) + · exact Or.inr ⟨c, d, e1.symm.trans hc, hd, k2⟩ + · exact Or.inr ⟨a, b, ha, e2.trans hb, k1⟩ + · rw [hb] at hc; cases hc + exact Or.inr ⟨a, d, ha, hd, k1.trans k2⟩ + +/-- One object replaced. -/ +theorem NtfnSpecKeepA.of_upd {σ σ' : State Unit} {q : ObjRef} {o o' : KernelObject} (hq : σ.kheap q = some o) + (h : σ'.kheap = Function.update σ.kheap q (some o')) (k : NtfnObjKeepA o o') : NtfnSpecKeepA σ σ' := fun p => by + by_cases e : p = q + · subst e + exact Or.inr ⟨o, o', hq, by rw [h, Function.update_self], k⟩ + · exact Or.inl (by rw [h, Function.update_of_ne e]) + +namespace NtfnSpecKeepA +variable {σ σ' : State Unit} (H : NtfnSpecKeepA σ σ') +include H + +theorem tcb {p : ObjRef} {x : Tcb} (h : σ.kheap p = some (.tcb x)) : + ∃ y, σ'.kheap p = some (.tcb y) ∧ y.tcbMcpriority = x.tcbMcpriority ∧ y.tcbYieldTo = x.tcbYieldTo ∧ + y.tcbFlags = x.tcbFlags := by + rcases H p with e | ⟨o, o', e1, e2, k⟩ + · exact ⟨x, e.trans h, rfl, rfl, rfl⟩ + · rw [h] at e1; cases e1 + obtain ⟨y, rfl, k1, k2, k3⟩ := k + exact ⟨y, e2, k1, k2, k3⟩ + +theorem sc {p : ObjRef} {x : SchedContext} {n : Nat} (h : σ.kheap p = some (.schedContext x n)) : + ∃ y, σ'.kheap p = some (.schedContext y n) ∧ y.scReplies = x.scReplies ∧ y.scYieldFrom = x.scYieldFrom := by + rcases H p with e | ⟨o, o', e1, e2, k⟩ + · exact ⟨x, e.trans h, rfl, rfl⟩ + · rw [h] at e1; cases e1 + obtain ⟨y, rfl, k1, k2⟩ := k + exact ⟨y, e2, k1, k2⟩ + +theorem sc_back {p : ObjRef} {y : SchedContext} {n : Nat} (h : σ'.kheap p = some (.schedContext y n)) : + ∃ x, σ.kheap p = some (.schedContext x n) ∧ y.scReplies = x.scReplies ∧ y.scYieldFrom = x.scYieldFrom := by + rcases H p with e | ⟨o, o', e1, e2, k⟩ + · exact ⟨y, e.symm.trans h, rfl, rfl⟩ + · rw [h] at e2; cases e2 + cases o <;> simp only [NtfnObjKeepA] at k + · obtain ⟨z, e, -⟩ := k; cases e + · obtain ⟨z, e⟩ := k; cases e + · obtain ⟨z, e, k1, k2⟩ := k + cases e + exact ⟨_, e1, k1, k2⟩ + +theorem reply_back {p : ObjRef} {r : Reply} (h : σ'.kheap p = some (.reply r)) : σ.kheap p = some (.reply r) := by + rcases H p with e | ⟨o, o', -, e2, k⟩ + · exact e.symm.trans h + · rw [h] at e2; cases e2 + cases o <;> simp only [NtfnObjKeepA] at k + · obtain ⟨z, e, -⟩ := k; cases e + · obtain ⟨z, e⟩ := k; cases e + · obtain ⟨z, e, -⟩ := k; cases e + +end NtfnSpecKeepA + +/-- `mcp` across a finaliser step. -/ +theorem Sched.McpRelA.ntfnFr {T : Set U64} {m : McState} {mem mem' : U64 → U64} {σ σ' : State Unit} + (h : McpRelA T m mem σ) (H : NtfnSpecKeepA σ σ') + (hm : ∀ t ∈ T, mem' (mcpA t) = mem (mcpA t) ∧ mem' (flagsA t) = mem (flagsA t)) : McpRelA T m mem' σ' := by + intro t ht + obtain ⟨x, hx, e, hf⟩ := h t ht + obtain ⟨y, hy, k, -, kf⟩ := H.tcb hx + exact ⟨y, hy, by rw [(hm t ht).1, k]; exact e, fun g => (hf g).keep (hm t ht).2 kf⟩ + +/-- `yieldRel` across a finaliser step. -/ +theorem Sched.YieldRelA.ntfnFr {T C : Set U64} {m : McState} {mem mem' : U64 → U64} {σ σ' : State Unit} + (h : YieldRelA T C m mem σ) (H : NtfnSpecKeepA σ σ') (hm : ∀ t ∈ T, mem' (yieldA t) = mem (yieldA t)) : + YieldRelA T C m mem' σ' := by + refine ⟨fun t ht => ?_, fun t ht h0 => ?_, fun c hc y n hk p hp => ?_⟩ + · obtain ⟨x, hx, e⟩ := h.tcb t ht + obtain ⟨y, hy, -, k, -⟩ := H.tcb hx + exact ⟨y, hy, by rw [hm t ht, k]; exact e⟩ + · rw [hm t ht] at h0 ⊢ + obtain ⟨hc, y, n, hy, e⟩ := h.toSc t ht h0 + obtain ⟨z, hz, -, k⟩ := H.sc hy + exact ⟨hc, z, n, hz, k.trans e⟩ + · obtain ⟨x, hx, -, k⟩ := H.sc_back hk + obtain ⟨t, ht, e1, e2⟩ := h.frm c hc x n hx p (k.symm.trans hp) + exact ⟨t, ht, e1, (hm t ht).trans e2⟩ + +/-- The reply stacks across a finaliser step. -/ +theorem Endp.ReplyStackRelA.ntfnFr {R C : Set U64} {mem mem' : U64 → U64} {σ σ' : State Unit} + {scRef : U64 → ObjRef} {stack : U64 → List U64} (h : ReplyStackRelA mem R C σ scRef stack) + (H : NtfnSpecKeepA σ σ') + (hm : ∀ r ∈ R, mem' (rNextA r) = mem (rNextA r) ∧ mem' (rPrevA r) = mem (rPrevA r)) : + ReplyStackRelA mem' R C σ' scRef stack := + h.frame hm + (fun _ _ _ n hk => by + obtain ⟨y, hy, k, -⟩ := H.sc hk + exact ⟨y, n, hy, k⟩) + (fun _ _ x hk => ⟨x, H.reply_back hk, rfl⟩) + +/-- The bound-TCB unlink (`irqUnbindNotificationTcb_someA`'s successor) is such a step. -/ +theorem NtfnSpecKeepA.unbindTcb {σ : State Unit} {n t : ObjRef} {x : Notification} {y : Tcb} + (hk : σ.kheap n = some (.notification x)) (ht : σ.kheap t = some (.tcb y)) : + NtfnSpecKeepA σ (updKSc (updKSc σ n (.notification { x with ntfnBoundTcb := none })) + t (.tcb { y with tcbBoundNotification := none })) := by + have hne : t ≠ n := by + intro e + rw [e, hk] at ht + cases ht + refine NtfnSpecKeepA.trans (σ2 := updKSc σ n (.notification { x with ntfnBoundTcb := none })) + (NtfnSpecKeepA.of_upd hk rfl ⟨_, rfl⟩) + (NtfnSpecKeepA.of_upd (q := t) (o := .tcb y) ?_ rfl ⟨_, rfl, rfl, rfl, rfl⟩) + rw [updKSc_kheap, Function.update_of_ne hne] + exact ht + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelNtfnHookA.lean b/proofs/Fl4ProofsA64/P3/DelNtfnHookA.lean new file mode 100644 index 000000000..0a63bb617 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelNtfnHookA.lean @@ -0,0 +1,143 @@ +/- +P3 (AArch64, delete hooks W3): **the Notification finaliser hook** `finHookNtfnA : FinHookKA .notification`, at the +`W` / `Wx` / `Rd` / `Rz` of the other kinds. The cap's typing `H p = some .ntfn` is the record's `capTy`. + +Premises: [P] `hCs` (the cores of `Z.Cs` are below `numNodes`), `hmb`, `hmax`, `htu`; [S] `h1` +(`numNodes = 1`, the single-core refinement view: the waiters' locality follows from the record, +`JStage3TA.onCore_single`). +-/ +import Fl4ProofsA64.P3.DelNtfnRecA +import Fl4ProofsA64.P3.DelRdXA +import Fl4ProofsA64.P3.DelHookKindA +import Fl4ProofsA64.P3.DelSpecA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 Fl4ProofsA64.P3.Endp +open Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Notif + +local notation "wa" => core.num.U64.wrapping_add + +noncomputable section + +section +variable {m : Fl4SpecA64.McState} +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + +/-- J7 with R2 over the slots other than one whose cap R2 does not read. -/ +theorem J7LA.pinUnreadA {Z : XSetsA} {H : Heap} {OS : Set U64} {ipa x : U64} {s : S} {σ : State Unit} + {m : McState} (h : J7LA st (J7R2A st ar ipa) Z H OS s σ m) (unread : ¬ S1aR2Reads (capA st s x)) : + J7LA st (J7R2xA st ar ipa x) Z H OS s σ m := + h.mapR fun _ ⟨Pp, Tb, _, Te, Kt, Lv, Re, bits, R2, sep, KLv, ktK, hik⟩ => + ⟨Pp, Tb, Te, Kt, Lv, Re, bits, R2.toDelM.drop unread, sep, KLv, ktK, hik.kt, fun y hy => hik.hdr y hy.1⟩ + +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Pp Tb : Set U64} {Lv : U64 → Nat} {ia ie : Nat} + {N E Rp : Set U64} {κ : U64 → U64} {n : Nat} + +/-- **The Notification finaliser hook at a state** (module docstring). -/ +theorem finHookNtfnA_atA {ipa : U64} (s0 : S) + (Rv : S → A → State Unit → MdbGhostA → U64 → Prop) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {s : S} {a : A} {σ : State Unit} {G : MdbGhostA} {sl : CslotPtr} {p b0 : U64} {b1 b2 : Bool} + {ac : Fl4SpecA64.Cap} {f : Bool} + (D : DStateA st Z.th OS s σ G) + (W : DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st (J7R2A st ar ipa))) s a σ G) + (hsl : AbsSlotA Z.th sl) (hc : capA st s (cslotAddrA sl) = .Notification p b0 b1 b2) + (hg : getCap (Z := Unit) sl σ = ({(ac, σ)}, False)) (hac : CapEqA (.Notification p b0 b1 b2) ac) + (hf : f = true ↔ isFinalCap' ac σ) (h1 : fl4A64Platform.numNodes = 1) : + objects.finalise_cap st ar s a (.Notification p b0 b1 b2) f false ⦃ r => + r.1.1 = .Null ∧ CleanCapA r.1.2 ∧ CapWfA r.1.2 ∧ + ∃ σ' G', (∃ d : Bool, (d = true ↔ isFinalCap' ac σ) ∧ + ((absCapA r.1.1, absCapA r.1.2), σ') ∈ (fl4FinaliseCap (Z := Unit) ac d σ).1) ∧ + DStateA st Z.th OS r.2.1 σ' G' ∧ capA st r.2.1 (cslotAddrA sl) = .Notification p b0 b1 b2 ∧ + (∀ y ∈ OS, y ≠ cslotAddrA sl → capA st r.2.1 y = capA st s y ∨ + (capA st r.2.1 y = .Null ∧ ¬ IsZombieA (capA st s y))) ∧ + WFA (DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st (J7R2A st ar ipa)))) + (DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st J7R2tA))) + r.1.1 (cslotAddrA sl) r.2.1 r.2.2 σ' G' ∧ + (RmOKA r.1.1 (cslotAddrA sl) → DelRdXA st ar Z H OS ipa Rv r.2.1 r.2.2 σ' G' (cslotAddrA sl)) ∧ + (∀ q zt k, r.1.1 = .Zombie q zt k → DelRzA st r.2.1 r.2.2 σ' G' (cslotAddrA sl)) ⦄ := by + have eac : ac = absCapA (.Notification p b0 b1 b2) := hac.symm + obtain ⟨hcore, ⟨Q, R, hw, Ac, Bf, stack, J, X⟩, hPA, hZT, hBH, hCT, hFx⟩ := W + have hOSx : cslotAddrA sl ∈ OS := D.mem_slot hsl + have hH : H p = some .ntfn := (J.capTy _ hOSx).2.1 p b0 b1 b2 hc + have unread : ¬ S1aR2Reads (capability.Cap.Notification p b0 b1 b2) := fun h => h + have rd : ∀ (s' : S) (a' : A) (σ' : State Unit) (G' : MdbGhostA) (Z' : XSetsA) (m' : McState), + capA st s' (cslotAddrA sl) = .Notification p b0 b1 b2 → + J7LA st (J7R2A st ar ipa) Z' H OS s' σ' m' → + J7LA st (J7R2A st ar ipa) Z' H OS s' σ' m' ∧ + (DelRdA st Rv s' a' σ' G' (cslotAddrA sl) ∧ RdThA st OS s' (cslotAddrA sl)) ∧ + J7LA st (J7R2xA st ar ipa (cslotAddrA sl)) Z' H OS s' σ' m' := by + intro s' a' σ' G' Z' m' e j + refine ⟨j, ⟨delRdA_other (fun r g h => by rw [e] at h; cases h) (fun r g h => by rw [e] at h; cases h) + (fun v h => by rw [e] at h; cases h), rdThA_of_ne fun t h => by rw [e] at h; cases h⟩, + j.pinUnreadA (by rw [e]; exact unread)⟩ + rw [fin_cap_eq_obj (by simp)] + cases f + · -- not final: nothing happens + have e : objects.finalise_cap_object st ar s a (.Notification p b0 b1 b2) false = + ok ((capability.Cap.Null, capability.Cap.Null), s, a) := rfl + have hnoop : ((Fl4SpecA64.Cap.nullCap, Fl4SpecA64.Cap.nullCap), σ) ∈ + (fl4FinaliseCap (Z := Unit) ac false σ).1 := by + rw [eac] + simp only [absCapA] + rw [fl4FinaliseCap_plainA (by intro h; exact h)] + exact mem_bindA.2 ⟨((), σ), rfl, rfl⟩ + rw [e, WP.spec_ok] + have rr := rd s a σ G _ m hc J.j7 + refine ⟨rfl, trivial, trivial, σ, G, ⟨false, hf, hnoop⟩, D, hc, fun y _ _ => Or.inl rfl, ?_, ?_, + fun _ _ _ h => by cases h⟩ + · exact ⟨fun h => absurd (Or.inl rfl) h, fun _ => ⟨hcore, ⟨Q, R, hw, Ac, Bf, stack, + J.mapJ7A fun _ _ => trivial, X⟩, hPA, hZT, hBH, hCT, hFx⟩⟩ + · exact fun _ => ⟨rr.2.1, m, rr.2.2⟩ + · -- final + refine WP.spec_mono (J.ntfnFin_recA (b0 := b0) (b1 := b1) (b2 := b2) hH hCs hmb hmax htu + (fun _ _ t ht _ => J.onCore_single hCs h1 ht)) ?_ + rintro ⟨⟨rem, cl⟩, s', a'⟩ ⟨h0, ha, σ1, σ2, σ3, Q', R', h1, h2, h3, J', k, kb, cpu, F, vc, k6⟩ + dsimp only at h0 ha J' k kb cpu vc k6 ⊢ + obtain ⟨rfl, rfl⟩ := Prod.mk.inj h0 + subst ha + have caps : ∀ y ∈ OS, capA st s' y = capA st s y := fun y hy => k.capA hy + have hc' : capA st s' (cslotAddrA sl) = .Notification p b0 b1 b2 := (caps _ hOSx).trans hc + have hmem : ((Fl4SpecA64.Cap.nullCap, Fl4SpecA64.Cap.nullCap), σ3) ∈ + (fl4FinaliseCap (Z := Unit) ac true σ).1 := by + rw [eac] + simp only [absCapA] + rw [fl4FinaliseCap_plainA (by intro h; exact h)] + exact mem_bindA.2 ⟨((), σ3), mem_bindA.2 ⟨((), σ1), h1, mem_bindA.2 ⟨((), σ2), h2, h3⟩⟩, rfl⟩ + have D' : DStateA st Z.th OS s' σ3 G := DStateA.keepOSA (Z := { Z with G := G, Q := Q', R := R', hw := hw }) D J' k + have rr := rd s' a' σ3 G _ m hc' J'.j7 + have W' : DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st J7R2tA)) s' a' σ3 G := + ⟨⟨kb.trans hcore.1, cpu.trans hcore.2⟩, ⟨Q', R', hw, Ac, Bf, stack, J'.mapJ7A fun _ _ => trivial, (k6 D.sub (X.ghostA G Q R hw)).ghostA Z.G Z.Q Z.R Z.hw⟩, + ParentAdjA.keepOS D.lk k hPA, + ZombieTcbNoVcpuA.keepA caps J.zombieTcb (fun t ht => vc t (J.tcbs ▸ ht)) hZT, + bufHold_sameA st hBH caps, ctOff_sameA st hCT kb caps, hFx.keepA st D.sub caps⟩ + refine ⟨rfl, trivial, trivial, σ3, G, ⟨true, hf, hmem⟩, D', hc', fun y hy _ => Or.inl (caps y hy), + ⟨fun h => absurd (Or.inl rfl) h, fun _ => W'⟩, fun _ => ⟨rr.2.1, m, rr.2.2⟩, fun _ _ _ h => by cases h⟩ + +/-- **The Notification finaliser hook** (module docstring). -/ +theorem finHookNtfnA {ipa : U64} (s0 : S) + (Rv : S → A → State Unit → MdbGhostA → U64 → Prop) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (h1 : fl4A64Platform.numNodes = 1) : + FinHookKA st .notification ar Z.th OS fl4FinaliseCap + (DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st (J7R2A st ar ipa)))) + (DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st J7R2tA))) + (DelRdXA st ar Z H OS ipa Rv) (DelRzA st) := by + intro s a σ G sl c ac f D hW hsl hc hk hg hac hf + obtain ⟨p, b0, b1, b2, rfl⟩ : ∃ p b0 b1 b2, c = .Notification p b0 b1 b2 := by + cases c <;> simp [finKindA] at hk + exact ⟨_, _, _, _, rfl⟩ + exact finHookNtfnA_atA s0 Rv hCs hmb hmax htu D hW hsl hc hg hac (hf trivial) h1 + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelNtfnJ5A.lean b/proofs/Fl4ProofsA64/P3/DelNtfnJ5A.lean new file mode 100644 index 000000000..d07b520a6 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelNtfnJ5A.lean @@ -0,0 +1,43 @@ +/- +P3 (aarch64, delete hooks W3, contract §45 step 2, executor 18): **the concrete J5 slot across a step that keeps the +`OS` slot words and the IRQ state words** (the scheduler / TCB writes of the finaliser). `J5A.keepA` is the part of +`JStage3TA.j5_vsWriteA` (`P3/DelVsKeepA`) after its write-set argument, for any such step. No named premise; new +module. +-/ +import Fl4ProofsA64.P3.Stage3BA +import Fl4ProofsA64.P3.DelVsKeepA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J7 : JSlotA S} {s s' : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- **J5 at the new state** from the kept `OS` slots and IRQ state words, at any ghost queues and spec state with the +same MDB ghost and typed heap. -/ +theorem JStage3BA.j5_keepA + (J : JStage3BA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + (osk : OSKeep st OS s s') (hk : L.ksb s' = L.ksb s) + (irqW : ∀ i : U64, i.val < 5376 → L.mem s' (irqStAddrA (L.ksb s) i) = L.mem s (irqStAddrA (L.ksb s) i)) + (σ' : Fl4SpecA64.State Unit) : J5A st Z H OS s' σ' m := by + obtain ⟨⟨mdb, irq, act⟩, sub, nul⟩ := J.j5 + have tr := J.j.1.cdt.tree + have osk' : OSKeep st {a | IsSlotA Z.th a} s s' := fun y hy => osk y (sub y hy) + exact ⟨⟨MdbInvA.keepO tr.lk tr.tree mdb osk', irq.keepO tr.lk osk', act.keepO tr.lk osk' hk irqW⟩, sub, + fun y hy hn => (osk.capA hy).trans (nul y hy hn)⟩ + +/-- `JStage3BA.j5_keepA` at `JStage3TA`. -/ +theorem JStage3TA.j5_keepA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + (osk : OSKeep st OS s s') (hk : L.ksb s' = L.ksb s) + (irqW : ∀ i : U64, i.val < 5376 → L.mem s' (irqStAddrA (L.ksb s) i) = L.mem s (irqStAddrA (L.ksb s) i)) + (σ' : Fl4SpecA64.State Unit) : J5A st Z H OS s' σ' m := + J.toBA.j5_keepA osk hk irqW σ' + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelNtfnLoopFrA.lean b/proofs/Fl4ProofsA64/P3/DelNtfnLoopFrA.lean new file mode 100644 index 000000000..b3bac7e75 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelNtfnLoopFrA.lean @@ -0,0 +1,233 @@ +/- +P3 (aarch64, delete hooks W3, contract §45 step 2, executor 18): **the frames of the record over +`cancel_all_signals`' write set** (`FpSA`: the notification's words and the queued TCBs' state and `Ep` link words, +`SigWrA`, and the scheduler's `MWrA`). That set is inside `SchedStepNWA`, so J5 (`j5_keepA`) and J7 (`J7PpKA`) are +kept; it misses a TCB's cap slots and buffer word, so `BufCover.frameK` applies. No named premise; new module. +-/ +import Fl4ProofsA64.P3.Stage3BA +import Fl4ProofsA64.P3.DelNtfnBufA +import Fl4ProofsA64.P3.DelNtfnJ5A +import Fl4ProofsA64.P3.SigFootA +import Fl4ProofsA64.P3.Stage3SchedWrNTA +import Fl4ProofsA64.P3.Stage3PreemptTA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 Fl4ProofsA64.P3.Endp + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Notif + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s s' : S} {a : A} {σ σ' : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + {Q : Set U64} {q : U64} [J7PpA st J7] + +/-- The loop's write set is a scheduler / IPC-object write set. -/ +theorem JStage3BA.sigW_stepNWA + (J : JStage3BA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (hq : q ∈ N) (hQ : Q ⊆ Z.T) {b : U64} (h : SigWrA Q q b ∨ MWrA (st := st) Z m σ s b) : + SchedStepNWA (st := st) N E Rp Z m σ s b := by + rcases h with (⟨h1, h2⟩ | ⟨t, ht, e⟩) | h + · exact Or.inr (Or.inl ⟨q, hq, h1, by omega⟩) + · have htT := hQ ht + have hfit := (J.sr.core.tcbs t htT).fits + have hv : ∀ K : U64, K.val < 4096 → (wa t K).val = t.val + K.val := fun K hK => by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have key : ∀ K : U64, 224 ≤ K.val → K.val < 4096 → K.val ≠ 3664 → K.val ≠ 3672 → + TcbStepWA Z.T (wa t K) := by + intro K k1 k2 k3 k4 + have e1 := hv K k2 + refine ⟨t, htT, by omega, by omega, fun h => k3 ?_, fun h => k4 ?_⟩ + · have := congrArg (fun w : U64 => w.val) h + simp only [e1, hv 3664#u64 (by simp)] at this + simpa using this + · unfold runVcpuA at h + have := congrArg (fun w : U64 => w.val) h + simp only [e1, hv 3672#u64 (by simp)] at this + simpa using this + refine Or.inl (Or.inr ?_) + rcases e with rfl | rfl | rfl + · rw [stA_eq]; exact key _ (by simp) (by simp) (by simp) (by simp) + · exact key 3496#u64 (by simp) (by simp) (by simp) (by simp) + · exact key 3504#u64 (by simp) (by simp) (by simp) (by simp) + · exact Or.inl (Or.inl h) + +/-- `JStage3BA.sigW_stepNWA` at `JStage3TA`. -/ +theorem JStage3TA.sigW_stepNWA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (hq : q ∈ N) (hQ : Q ⊆ Z.T) {b : U64} (h : SigWrA Q q b ∨ MWrA (st := st) Z m σ s b) : + SchedStepNWA (st := st) N E Rp Z m σ s b := + J.toBA.sigW_stepNWA hq hQ h + +/-- The loop keeps the `OS` slot words. -/ +theorem JStage3BA.sigW_osKeepA + (J : JStage3BA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (w : WritesIn st s s' (SchedStepNWA (st := st) N E Rp Z m σ s)) : OSKeep st OS s s' := + OSKeep.of_off J.ent.os w fun y hy o o1 o2 h => by + have hv : (wa y o).val = y.val + o.val := by + have := J.ent.os.fit y hy + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact J.os_offStepNA (J.ent.os.al hy o1 o2) ⟨y, hy, by omega, by omega⟩ h + +/-- `JStage3BA.sigW_osKeepA` at `JStage3TA`. -/ +theorem JStage3TA.sigW_osKeepA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (w : WritesIn st s s' (SchedStepNWA (st := st) N E Rp Z m σ s)) : OSKeep st OS s s' := + J.toBA.sigW_osKeepA w + +/-- **J5 and J7 across a step writing only `SchedStepNWA` words** (the loop, by `sigW_stepNWA`). -/ +theorem JStage3BA.j57_stepNWA [J7PpKA st J7] + (J : JStage3BA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + (w : WritesIn st s s' (SchedStepNWA (st := st) N E Rp Z m σ s)) (σ' : Fl4SpecA64.State Unit) : + J5A st Z H OS s' σ' m ∧ J7 Z H OS s' σ' m := by + have osk := J.sigW_osKeepA w + have kb : L.ksb s' = L.ksb s := w.1 + have irqW : ∀ i : U64, i.val < 5376 → + L.mem s' (irqStAddrA (L.ksb s) i) = L.mem s (irqStAddrA (L.ksb s) i) := by + intro i hi + have hv := irqWordA_val J.sr.ksFit hi + have hkb := J.sr.toSchedSRN.ksb_al + refine w.mem_eq ?_ (J.irq_offStepNA hi) + show (irqWordA (L.ksb s) i).val % 8 = 0 + unfold Aligned at hkb + omega + refine ⟨J.j5_keepA osk kb irqW σ', J7PpKA.keep (st := st) _ _ _ _ _ σ σ' _ J.j7 kb osk ?_⟩ + intro Pt Ub vr b hb + refine w.mem_eq (vr.al _ hb) ?_ + by_cases h : AsW (L.ksb s) b + · obtain ⟨o, o1, o2, -, e⟩ := h + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.ent.ksw + have ev : b.val = (L.ksb s).val + o.val := by + rw [e, wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact J.asid_offStepNA (by omega) (by omega) + · exact J.vsr_offStepNA vr hb h + +/-- `JStage3BA.j57_stepNWA` at `JStage3TA`. -/ +theorem JStage3TA.j57_stepNWA [J7PpKA st J7] + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + (w : WritesIn st s s' (SchedStepNWA (st := st) N E Rp Z m σ s)) (σ' : Fl4SpecA64.State Unit) : + J5A st Z H OS s' σ' m ∧ J7 Z H OS s' σ' m := + J.toBA.j57_stepNWA w σ' + +/-- A TCB word below the cap slots' end, or the IPC buffer word, is not written by the loop. -/ +theorem JStage3TA.sigW_tcbWordA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (hq : q ∈ N) (hQ : Q ⊆ Z.T) {t K : U64} (ht : t ∈ Z.T) (hK : K.val < 224 ∨ K.val = 3512) : + ¬ (SigWrA Q q (wa t K) ∨ MWrA (st := st) Z m σ s (wa t K)) := by + have hfit := (J.sr.core.tcbs t ht).fits + have hv : (wa t K).val = t.val + K.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have hth := (J.sr.thT t).2 ht + have htH : H t = some .tcb := by + have : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + exact this + have hsz : ObjKindA.size .tcb = 4096 := rfl + -- a word of a TCB of `T` at one of the offsets the loop writes is another word + have other : ∀ u ∈ Z.T, ∀ K' : U64, K'.val < 4096 → 224 ≤ K'.val → K'.val ≠ 3512 → wa t K ≠ wa u K' := by + intro u hu K' k1 k2 k3 h + have hfu := (J.sr.core.tcbs u hu).fits + have hv' : (wa u K').val = u.val + K'.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have h' := congrArg (fun w : U64 => w.val) h + simp only [hv, hv'] at h' + by_cases e : u = t + · subst e; omega + · have := J.sr.place.apart u t .tcb .tcb ((J.sr.thT u).2 hu) hth e + omega + rintro ((⟨h1, h2⟩ | ⟨u, hu, e⟩) | h | h | ⟨u, hu, e⟩) + · exact J.ipc_off_hobjA (v := t) (kv := .tcb) htH (by simp) ⟨by omega, by simp only [HKind.bits]; omega⟩ + (Or.inl ⟨q, hq, h1, by omega⟩) + · have huT := hQ hu + rcases e with e | e | e + · rw [stA_eq] at e; exact other u huT _ (by simp) (by simp) (by simp) e + · exact other u huT 3496#u64 (by simp) (by simp) (by simp) e + · exact other u huT 3504#u64 (by simp) (by simp) (by simp) e + · obtain ⟨c, hc, x, k, hx, ha⟩ := h + have := (J.sr.scp.out c hc x k hx _ ha).1 t _ hth + omega + · have := J.sr.tcb_ks ht + unfold SKsA at h + unfold ksSizeA at this; omega + · rcases e with e | e | e | e + · rw [scA_eq] at e; exact other u hu _ (by simp) (by simp) (by simp) e + · exact other u hu 3480#u64 (by simp) (by simp) (by simp) e + · exact other u hu 3488#u64 (by simp) (by simp) (by simp) e + · rw [inqA_eq] at e; exact other u hu _ (by simp) (by simp) (by simp) e + +/-- **`BufCover` across the loop.** -/ +theorem JStage3TA.bufCover_sigWA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (hq : q ∈ N) (hQ : Q ⊆ Z.T) + (w : WritesIn st s s' (fun b => SigWrA Q q b ∨ MWrA (st := st) Z m σ s b)) (K : NtfnTcbKeepA σ σ') : + BufCover st Z.T Bf s' σ' := by + have kept : ∀ t ∈ Z.T, ∀ K : U64, (K.val < 224 ∨ K.val = 3512) → K.val % 8 = 0 → + L.mem s' (wa t K) = L.mem s (wa t K) := by + intro t ht K hK h8 + have htH : H t = some .tcb := by + have : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + exact this + have hal := J.ent.heap.align t _ htH + have hfit := (J.sr.core.tcbs t ht).fits + have hv : (wa t K).val = t.val + K.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + simp only [HKind.bits] at hal + refine w.mem_eq ?_ (J.sigW_tcbWordA hq hQ ht hK) + show (wa t K).val % 8 = 0 + omega + have sb : ∀ t ∈ Z.T, sbWordA t = wa t 64#u64 := by + intro t _ + have h64 : core.num.U64.wrapping_mul layout.tcb.SLOT_BUFFER layout.cte.SIZE = 64#u64 := by + first + | rfl + | decide + | (unfold layout.tcb.SLOT_BUFFER layout.cte.SIZE; rfl) + unfold sbWordA + rw [h64] + have osk : OSKeep st {y | ∃ t ∈ Z.T, y = sbWordA t} s s' := by + rintro y ⟨t, ht, rfl⟩ + have hfit := (J.sr.core.tcbs t ht).fits + have e64 : (wa t 64#u64).val = t.val + 64 := by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by simp; omega) + have step : ∀ o : U64, o.val < 32 → o.val % 8 = 0 → + L.mem s' (wa (sbWordA t) o) = L.mem s (wa (sbWordA t) o) := by + intro o o1 o2 + have hK : (wa 64#u64 o).val = 64 + o.val := by + rw [wa_valm, show (64#u64 : U64).val = 64 by simp]; exact Nat.mod_eq_of_lt (by omega) + have e : wa (sbWordA t) o = wa t (wa 64#u64 o) := by + apply UScalar.eq_of_val_eq + have l : (wa (wa t 64#u64) o).val = t.val + 64 + o.val := by + rw [wa_valm, e64]; exact Nat.mod_eq_of_lt (by omega) + have r : (wa t (wa 64#u64 o)).val = t.val + (64 + o.val) := by + rw [wa_valm, hK]; exact Nat.mod_eq_of_lt (by omega) + rw [sb t ht, l, r]; omega + rw [e] + exact kept t ht _ (Or.inl (by omega)) (by omega) + have e0 : wa (sbWordA t) 0#u64 = sbWordA t := by apply UScalar.eq_of_val_eq; rw [wa_valm]; simp + exact ⟨by have := step 0#u64 (by simp) (by simp); rwa [e0] at this, step 8#u64 (by simp) (by simp), + step 16#u64 (by simp) (by simp), step 24#u64 (by simp) (by simp)⟩ + refine BufCover.frameK J.bufCover (fun t ht => ?_) (fun t ht => osk.capA ⟨t, ht, rfl⟩) (fun t ht => ?_) K.buf + · rw [ibWordA_eq] + exact kept t ht _ (Or.inr (by simp)) (by simp) + · obtain ⟨x, hx, -⟩ := J.ntfn.tcb t ht + exact ⟨x, hx⟩ + +end + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- The concrete J5 slot does not read the ghost queues or current objects, so J5 at `Z` is J5 at the queues +`of_schedWrN` writes. -/ +theorem J5A_updQ (Z : XSetsA) (Q : U64 → U64 → List U64) (Rl : U64 → List U64) + (curOf curScOf : Fl4SpecA64.CoreId → Fl4SpecA64.ObjRef) : + J5A st { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } = J5A st Z := rfl + +/-- The same for the loop's J7 slot. -/ +theorem J7LA_updQ (nA : U64) (Z : XSetsA) (Q : U64 → U64 → List U64) (Rl : U64 → List U64) + (curOf curScOf : Fl4SpecA64.CoreId → Fl4SpecA64.ObjRef) : + J7LA st (J7R2A st ar nA) { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } = + J7LA st (J7R2A st ar nA) Z := rfl + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelNtfnRecA.lean b/proofs/Fl4ProofsA64/P3/DelNtfnRecA.lean new file mode 100644 index 000000000..8c6cbab06 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelNtfnRecA.lean @@ -0,0 +1,806 @@ +/- +P3 (AArch64, delete hooks W3, contract §53): **the record across the final Notification finaliser**. + +* `JStage3TA.ntfnFin_recA`: `finalise_cap_object` of a final Notification cap refines the spec's chain + (`schedContextMaybeUnbindNtfn`, `unbindMaybeNotification`, `cancelAllSignals`) and `JStage3TA` holds again at the + new queues (`of_schedWrN`); every `OS` slot word is kept. +* `SchedQueuesInv.reQA`: the scheduler invariant at the queue lists of `SchedCoreA`. +* `DStateA.keepOSA`: the deletion's state facts across a step keeping the `OS` slot words. + +Premises: `H p = some .ntfn` (the cap's typing, taken as a hypothesis here); [P] `hCs` / `hmb` / `hmax` / `htu`; +[S] `hon` (the waiters are local, as in `P3/IrqInvokeSlotsA`). +-/ +import Fl4ProofsA64.P3.Stage3BA +import Fl4ProofsA64.P3.DelRec6A +import Fl4ProofsA64.P3.Stage3QKeepPresA +import Fl4ProofsA64.P3.DelNtfnUnlinkA +import Fl4ProofsA64.P3.DelBlkKeepA +import Fl4ProofsA64.P3.DelRvVrA +import Fl4ProofsA64.P3.DelNtfnLoopFrA +import Fl4ProofsA64.P3.DelNtfnBoundA +import Fl4ProofsA64.P3.DelNtfnEpA +import Fl4ProofsA64.P3.DelSinvLoopA +import Fl4ProofsA64.P3.Stage3McsFrA +import Fl4ProofsA64.P3.DelZeroKeepA +import Fl4ProofsA64.P3.Stage3SchedOpsTA +import Fl4ProofsA64.P3.CnodeMdbBridgeA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 Fl4ProofsA64.P3.Endp +open Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Notif + +local notation "wa" => core.num.U64.wrapping_add + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-- **The scheduler invariant at `SchedCoreA`'s queue lists**: the lists of the cores and priorities are the ones +memory holds. -/ +theorem SchedQueuesInv.reQA {T Cs Ω : Set U64} {H : Heap} {s : S} {Q Q' : U64 → U64 → List U64} + {R R' : U64 → List U64} (h : SchedQueuesInv st T Cs Ω H s Q' R') (c : SchedCoreA st T Cs s Q R) : + SchedQueuesInv st T Cs Ω H s Q R := by + have eQ : ∀ k ∈ Cs, ∀ p : U64, p.val < 256 → Q k p = Q' k p := fun k hk p hp => + QueueRel.unique c.tcbs ((c.ready k hk).1.rel p hp) ((h.ready k hk).1.rel p hp) + have eR : ∀ k ∈ Cs, R k = R' k := fun k hk => QueueRel.unique c.tcbs (c.release k hk) (h.release k hk) + exact + { tcbs := c.tcbs, nodes := c.nodes, cur := h.cur, aff := c.aff, prio := c.prio, ready := c.ready, + release := c.release, inQ := c.inQ, inR := c.inR, queued := c.queued, released := c.released, excl := c.excl, + action := h.action + runQ := fun k hk p hp t ht => h.runQ k hk p hp t (eQ k hk p hp ▸ ht) + runR := fun k hk t ht => h.runR k hk t (eR k hk ▸ ht) + actOK := fun ha => ⟨(h.actOK ha).1, fun k hk => eR k hk ▸ (h.actOK ha).2 k hk⟩ + eps := h.eps, curT := h.curT, idleT := h.idleT, ksOut := h.ksOut, omega := h.omega } + +/-- **`DStateA` across a step keeping the `OS` slot words**, from the new record. -/ +theorem DStateA.keepOSA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J7 : JSlotA S} {s s' : S} {a' : A} {σ σ' : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (D : DStateA st Z.th OS s σ Z.G) + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s' a' σ' m) + (k : OSKeep st OS s s') : DStateA st Z.th OS s' σ' Z.G := by + have caps : ∀ y ∈ OS, capA st s' y = capA st s y := fun y hy => k.capA hy + refine DStateA.ofTA J.toJEntTA (D.mdbS.keep k) (zombiesFinal_keptA D.zf fun y hy => Or.inl (caps y hy)) + (D.tree.keepO k) D.sub (fun y hy hn => (caps y hy).trans (D.nul y hy hn)) ?_ + refine + { cov := J.finCov + fr := D.fr.of (fun y hy f => ⟨y, hy, by rw [← caps y hy]; exact f, by rw [caps y hy]⟩) + (fun y hy f => ⟨y, hy, by rw [← caps y hy]; exact f, by rw [caps y hy]⟩) + irq := IrqRunA.ofP1 J.j5.1.2.1 + zw := DelInvA.of D (fun y hy p zt n h => ?_) } + have hyO := D.sub y hy + rw [caps y hyO] at h + exact ⟨y, hyO, h⟩ + +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} [J7PpA st J7] {s s' : S} {a : A} {σ σ' : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} + +/-- A TCB word at an offset the scheduler and SC code do not write is off `MWrA`. -/ +theorem JStage3BA.tcbWord_offMA + (J : JStage3BA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {t K : U64} (ht : t ∈ Z.T) (hK : K.val < 4096) + (h3 : K.val ≠ 3464 ∧ K.val ≠ 3480 ∧ K.val ≠ 3488 ∧ K.val ≠ 3592) : + ¬ MWrA (st := st) Z m σ s (wa t K) := by + have hfit := (J.sr.core.tcbs t ht).fits + have hv : (wa t K).val = t.val + K.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have hth := (J.sr.thT t).2 ht + have hsz : ObjKindA.size .tcb = 4096 := rfl + have other : ∀ u ∈ Z.T, ∀ K' : U64, K'.val < 4096 → K.val ≠ K'.val → wa t K ≠ wa u K' := by + intro u hu K' k1 k2 h + have hfu := (J.sr.core.tcbs u hu).fits + have hv' : (wa u K').val = u.val + K'.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have h' := congrArg (fun w : U64 => w.val) h + simp only [hv, hv'] at h' + by_cases e : u = t + · subst e; omega + · have := J.sr.place.apart u t .tcb .tcb ((J.sr.thT u).2 hu) hth e + omega + rintro (h | h | ⟨u, hu, e⟩) + · obtain ⟨c, hc, x, k, hx, ha⟩ := h + have := (J.sr.scp.out c hc x k hx _ ha).1 t _ hth + omega + · have := J.sr.tcb_ks ht + unfold SKsA at h + unfold ksSizeA at this; omega + · rcases e with e | e | e | e + · rw [scA_eq] at e; exact other u hu _ (by simp) (by simpa using h3.1) e + · exact other u hu 3480#u64 (by simp) (by simpa using h3.2.1) e + · exact other u hu 3488#u64 (by simp) (by simpa using h3.2.2.1) e + · rw [inqA_eq] at e; exact other u hu _ (by simp) (by simpa using h3.2.2.2) e + +/-- `JStage3BA.tcbWord_offMA` at `JStage3TA`. -/ +theorem JStage3TA.tcbWord_offMA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {t K : U64} (ht : t ∈ Z.T) (hK : K.val < 4096) + (h3 : K.val ≠ 3464 ∧ K.val ≠ 3480 ∧ K.val ≠ 3488 ∧ K.val ≠ 3592) : + ¬ MWrA (st := st) Z m σ s (wa t K) := + J.toBA.tcbWord_offMA ht hK h3 + +/-- **Locality under [S] `numNodes = 1`** (Architect ruling on `hon`): every TCB's affinity and the current core are +cores of `Z.Cs`, all below `numNodes`, so they are equal. -/ +theorem JStage3BA.onCore_single + (J : JStage3BA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + {t : U64} (ht : t ∈ Z.T) : L.mem s (affA t) = L.cpu s := by + have x := hCs _ (J.sr.core.aff t ht) + have y := hCs _ J.j.1.sch.cpuCs + exact UScalar.eq_of_val_eq (by omega) + +/-- `JStage3BA.onCore_single` at `JStage3TA`. -/ +theorem JStage3TA.onCore_single + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + {t : U64} (ht : t ∈ Z.T) : L.mem s (affA t) = L.cpu s := + J.toBA.onCore_single hCs h1 ht + +/-- The write set of the Notification finaliser. -/ +def NtfnFinW (st : machine.Store S) [L : LawfulStore st] (Z : XSetsA) (m : Fl4SpecA64.McState) + (σ : Fl4SpecA64.State Unit) (s : S) (Q : Set U64) (p b : U64) : Prop := + b = nScSc p ∨ b = ntfnBtA p ∨ (∃ u ∈ Z.T, b = boundA u) ∨ SigWrA Q p b ∨ MWrA (st := st) Z m σ s b + +/-- The two link words of a notification are words of its object. -/ +theorem JStage3BA.ntfnLink_ipcA + (J : JStage3BA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {p b : U64} (hp : p ∈ N) (h : b = nScSc p ∨ b = ntfnBtA p) : p.val ≤ b.val ∧ b.val < p.val + 40 := by + have hna := J.iplaced.n.ntfn p hp + rcases h with rfl | rfl + · have e : nScSc p = Notif.nScA p := rfl + rw [e, Notif.nScA_eq, Notif.nfield_val hna (by simp)]; simp + · have e : ntfnBtA p = Notif.nBoundA p := rfl + rw [e, Notif.nBoundA_eq, Notif.nfield_val hna (by simp)]; simp + +/-- `JStage3BA.ntfnLink_ipcA` at `JStage3TA`. -/ +theorem JStage3TA.ntfnLink_ipcA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {p b : U64} (hp : p ∈ N) (h : b = nScSc p ∨ b = ntfnBtA p) : p.val ≤ b.val ∧ b.val < p.val + 40 := + J.toBA.ntfnLink_ipcA hp h + +theorem JStage3TA.finW_stepNA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {p : U64} (hp : p ∈ N) {Q : Set U64} (hQ : Q ⊆ Z.T) {b : U64} (h : NtfnFinW st Z m σ s Q p b) : + SchedStepNWA (st := st) N E Rp Z m σ s b := by + rcases h with h | h | ⟨u, hu, rfl⟩ | h | h + · have := J.ntfnLink_ipcA hp (Or.inl h) + exact Or.inr (Or.inl ⟨p, hp, this.1, by omega⟩) + · have := J.ntfnLink_ipcA hp (Or.inr h) + exact Or.inr (Or.inl ⟨p, hp, this.1, by omega⟩) + · have hfit := (J.sr.core.tcbs u hu).fits + have hv : ∀ K : U64, K.val < 4096 → (wa u K).val = u.val + K.val := fun K hK => by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have e : boundA u = wa u 3368#u64 := rfl + have v : (boundA u).val = u.val + 3368 := by rw [e, hv _ (by simp)]; simp + refine Or.inl (Or.inr ⟨u, hu, by omega, by omega, fun h => ?_, fun h => ?_⟩) + · have := congrArg (fun w : U64 => w.val) h + simp only [v, hv 3664#u64 (by simp)] at this + simp at this + · unfold runVcpuA at h + have := congrArg (fun w : U64 => w.val) h + simp only [v, hv 3672#u64 (by simp)] at this + simp at this + · exact J.sigW_stepNWA hp hQ (Or.inl h) + · exact Or.inl (Or.inl h) + +/-- A TCB word at an offset the finaliser does not write. -/ +theorem JStage3TA.tcbWord_offFinA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {p : U64} (hp : p ∈ N) {Q : Set U64} (hQ : Q ⊆ Z.T) {t K : U64} (ht : t ∈ Z.T) (hK : K.val < 4096) + (h8 : K.val ≠ 3328 ∧ K.val ≠ 3368 ∧ K.val ≠ 3464 ∧ K.val ≠ 3480 ∧ K.val ≠ 3488 ∧ K.val ≠ 3496 ∧ + K.val ≠ 3504 ∧ K.val ≠ 3592) : + ¬ NtfnFinW st Z m σ s Q p (wa t K) := by + have hfit := (J.sr.core.tcbs t ht).fits + have hv : (wa t K).val = t.val + K.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have hth := (J.sr.thT t).2 ht + have htH : H t = some .tcb := by + have : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + exact this + have offIpc : ¬ IpcObjWA N E Rp (wa t K) := + J.ipc_off_hobjA (v := t) (kv := .tcb) htH (by simp) ⟨by omega, by simp only [HKind.bits]; omega⟩ + have hsz : ObjKindA.size .tcb = 4096 := rfl + have other : ∀ u ∈ Z.T, ∀ K' : U64, K'.val < 4096 → K.val ≠ K'.val → wa t K ≠ wa u K' := by + intro u hu K' k1 k2 h + have hfu := (J.sr.core.tcbs u hu).fits + have hv' : (wa u K').val = u.val + K'.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have h' := congrArg (fun w : U64 => w.val) h + simp only [hv, hv'] at h' + by_cases e : u = t + · subst e; omega + · have := J.sr.place.apart u t .tcb .tcb ((J.sr.thT u).2 hu) hth e + omega + rintro (h | h | ⟨u, hu, e⟩ | (⟨h1, h2⟩ | ⟨u, hu, e⟩) | h) + · have := J.ntfnLink_ipcA hp (Or.inl h) + exact offIpc (Or.inl ⟨p, hp, this.1, by omega⟩) + · have := J.ntfnLink_ipcA hp (Or.inr h) + exact offIpc (Or.inl ⟨p, hp, this.1, by omega⟩) + · exact other u hu 3368#u64 (by simp) (by simpa using h8.2.1) e + · exact offIpc (Or.inl ⟨p, hp, h1, by omega⟩) + · have huT := hQ hu + rcases e with e | e | e + · rw [stA_eq] at e; exact other u huT _ (by simp) (by simpa using h8.1) e + · exact other u huT 3496#u64 (by simp) (by simpa using h8.2.2.2.2.2.1) e + · exact other u huT 3504#u64 (by simp) (by simpa using h8.2.2.2.2.2.2.1) e + · exact J.tcbWord_offMA ht hK ⟨h8.2.2.1, h8.2.2.2.1, h8.2.2.2.2.1, h8.2.2.2.2.2.2.2⟩ h + +/-- A reply object's word is not written by the finaliser. -/ +theorem JStage3BA.reply_offFinA + (J : JStage3BA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {p : U64} (hp : p ∈ N) {Q : Set U64} (hQ : Q ⊆ Z.T) {r b : U64} (hr : r ∈ Rp) + (lo : r.val ≤ b.val) (hi : b.val < r.val + 32) : ¬ NtfnFinW st Z m σ s Q p b := by + have hrH : H r = some .reply := J.utIpc.reply r hr + have hpH : H p = some .ntfn := J.utIpc.ntfn p hp + have ne : p ≠ r := fun e => by rw [e, hrH] at hpH; cases hpH + have dj := J.ent.heap.disj p _ r _ hpH hrH ne + simp only [HKind.bits] at dj + have offN : ¬ (p.val ≤ b.val ∧ b.val < p.val + 40) := by omega + have hw : IpcObjWA N E Rp b := Or.inr (Or.inr ⟨r, hr, lo, hi⟩) + have offT : ∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → b ≠ wa u K := by + intro u hu K hK e + have hfu := (J.sr.core.tcbs u hu).fits + have hv' : (wa u K).val = u.val + K.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have huH : H u = some .tcb := by + have : u ∈ H.tcbs := by rw [J.tcbs]; exact hu + exact this + exact J.ipc_off_hobjA (v := u) (kv := .tcb) huH (by simp) + ⟨by rw [e, hv']; omega, by rw [e, hv']; simp only [HKind.bits]; omega⟩ hw + rintro (h | h | ⟨u, hu, e⟩ | (⟨h1, h2⟩ | ⟨u, hu, e⟩) | h) + · exact offN (J.ntfnLink_ipcA hp (Or.inl h)) + · exact offN (J.ntfnLink_ipcA hp (Or.inr h)) + · exact offT u hu 3368#u64 (by simp) e + · exact offN ⟨h1, by omega⟩ + · have huT := hQ hu + rcases e with e | e | e + · rw [stA_eq] at e; exact offT u huT _ (by simp) e + · exact offT u huT 3496#u64 (by simp) e + · exact offT u huT 3504#u64 (by simp) e + · exact (replyWordOffSchedA J.sr.toSchedSRN J.ipcPlace hr lo hi).2 h + +/-- `JStage3BA.reply_offFinA` at `JStage3TA`. -/ +theorem JStage3TA.reply_offFinA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {p : U64} (hp : p ∈ N) {Q : Set U64} (hQ : Q ⊆ Z.T) {r b : U64} (hr : r ∈ Rp) + (lo : r.val ≤ b.val) (hi : b.val < r.val + 32) : ¬ NtfnFinW st Z m σ s Q p b := + J.toBA.reply_offFinA hp hQ hr lo hi + +/-- **`BufCover` across a step keeping the TCBs' buffer word and buffer slot** (words below offset 224 and the +word at 3512) and the spec's buffer fields. -/ +theorem JStage3BA.bufCover_wA + (J : JStage3BA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {W : U64 → Prop} (w : WritesIn st s s' W) + (hW : ∀ t ∈ Z.T, ∀ K : U64, (K.val < 224 ∨ K.val = 3512) → ¬ W (wa t K)) + (K : ∀ p x, σ.kheap p = some (.tcb x) → ∃ y, σ'.kheap p = some (.tcb y) ∧ + y.tcbIpcframe = x.tcbIpcframe ∧ y.tcbIpcBuffer = x.tcbIpcBuffer) : + BufCover st Z.T Bf s' σ' := by + have kept : ∀ t ∈ Z.T, ∀ K : U64, (K.val < 224 ∨ K.val = 3512) → K.val % 8 = 0 → + L.mem s' (wa t K) = L.mem s (wa t K) := by + intro t ht K hK h8 + have htH : H t = some .tcb := by + have : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + exact this + have hal := J.ent.heap.align t _ htH + have hfit := (J.sr.core.tcbs t ht).fits + have hv : (wa t K).val = t.val + K.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + simp only [HKind.bits] at hal + refine w.mem_eq ?_ (hW t ht K hK) + show (wa t K).val % 8 = 0 + omega + have sb : ∀ t : U64, sbWordA t = wa t 64#u64 := by + intro t + have h64 : core.num.U64.wrapping_mul layout.tcb.SLOT_BUFFER layout.cte.SIZE = 64#u64 := by + first + | rfl + | decide + | (unfold layout.tcb.SLOT_BUFFER layout.cte.SIZE; rfl) + unfold sbWordA + rw [h64] + have osk : OSKeep st {y | ∃ t ∈ Z.T, y = sbWordA t} s s' := by + rintro y ⟨t, ht, rfl⟩ + have hfit := (J.sr.core.tcbs t ht).fits + have e64 : (wa t 64#u64).val = t.val + 64 := by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by simp; omega) + have step : ∀ o : U64, o.val < 32 → o.val % 8 = 0 → + L.mem s' (wa (sbWordA t) o) = L.mem s (wa (sbWordA t) o) := by + intro o o1 o2 + have hK : (wa 64#u64 o).val = 64 + o.val := by + rw [wa_valm, show (64#u64 : U64).val = 64 by simp]; exact Nat.mod_eq_of_lt (by omega) + have e : wa (sbWordA t) o = wa t (wa 64#u64 o) := by + apply UScalar.eq_of_val_eq + have l : (wa (wa t 64#u64) o).val = t.val + 64 + o.val := by + rw [wa_valm, e64]; exact Nat.mod_eq_of_lt (by omega) + have r : (wa t (wa 64#u64 o)).val = t.val + (64 + o.val) := by + rw [wa_valm, hK]; exact Nat.mod_eq_of_lt (by omega) + rw [sb t, l, r]; omega + rw [e] + exact kept t ht _ (Or.inl (by omega)) (by omega) + have e0 : wa (sbWordA t) 0#u64 = sbWordA t := by apply UScalar.eq_of_val_eq; rw [wa_valm]; simp + exact ⟨by have := step 0#u64 (by simp) (by simp); rwa [e0] at this, step 8#u64 (by simp) (by simp), + step 16#u64 (by simp) (by simp), step 24#u64 (by simp) (by simp)⟩ + refine BufCover.frameK J.bufCover (fun t ht => ?_) (fun t ht => osk.capA ⟨t, ht, rfl⟩) (fun t ht => ?_) K + · rw [ibWordA_eq] + exact kept t ht _ (Or.inr (by simp)) (by simp) + · obtain ⟨x, hx, -⟩ := J.ntfn.tcb t ht + exact ⟨x, hx⟩ + +/-- `JStage3BA.bufCover_wA` at `JStage3TA`. -/ +theorem JStage3TA.bufCover_wA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {W : U64 → Prop} (w : WritesIn st s s' W) + (hW : ∀ t ∈ Z.T, ∀ K : U64, (K.val < 224 ∨ K.val = 3512) → ¬ W (wa t K)) + (K : ∀ p x, σ.kheap p = some (.tcb x) → ∃ y, σ'.kheap p = some (.tcb y) ∧ + y.tcbIpcframe = x.tcbIpcframe ∧ y.tcbIpcBuffer = x.tcbIpcBuffer) : + BufCover st Z.T Bf s' σ' := + J.toBA.bufCover_wA w hW K + +/-- The TCB fields the record reads, kept across the whole finaliser. -/ +def TcbFinKeepA (σ σ' : Fl4SpecA64.State Unit) : Prop := + ∀ p x, σ.kheap p = some (.tcb x) → ∃ y, σ'.kheap p = some (.tcb y) ∧ y.tcbFault = x.tcbFault ∧ + y.tcbIpcframe = x.tcbIpcframe ∧ y.tcbIpcBuffer = x.tcbIpcBuffer + +theorem TcbFinKeepA.trans {σ1 σ2 σ3 : Fl4SpecA64.State Unit} (h1 : TcbFinKeepA σ1 σ2) (h2 : TcbFinKeepA σ2 σ3) : + TcbFinKeepA σ1 σ3 := fun p x h => by + obtain ⟨y, hy, a1, b1, c1⟩ := h1 p x h + obtain ⟨z, hz, a2, b2, c2⟩ := h2 p y hy + exact ⟨z, hz, a2.trans a1, b2.trans b1, c2.trans c1⟩ + +theorem NtfnTcbKeepA.fin {σ σ' : Fl4SpecA64.State Unit} (K : NtfnTcbKeepA σ σ') : TcbFinKeepA σ σ' := fun p x h => by + obtain ⟨y, hy, -, hf, -, -, hfr, hb, -⟩ := K.tcb p x h + exact ⟨y, hy, hf, hfr, hb⟩ + +/-- **The bound-TCB unlink at the record** (stated at the state after the SC unlink). -/ +theorem ntfnBoundUnlinkA {N T E R : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {s1 : S} + {σ1 : Fl4SpecA64.State Unit} (hR : Notif.NtfnStateRel N T (L.mem s1) σ1) (hE : EpStateRel T E R Bf (L.mem s1) σ1) + (hP : IPlacedA N T E R Bf) {p : U64} (hp : p ∈ N) : + ∃ s2 σ2, notification.unbind_maybe_notification st s1 p = ok s2 ∧ + unbindMaybeNotification (Z := Unit) (specPtr p) σ1 = ({((), σ2)}, False) ∧ + WritesIn st s1 s2 (fun b => b = ntfnBtA p ∨ ∃ u ∈ T, b = boundA u) ∧ + Notif.NtfnStateRel N T (L.mem s2) σ2 ∧ EpStateRel T E R Bf (L.mem s2) σ2 ∧ + SchedFrameA σ1 σ2 ∧ NtfnSpecKeepA σ1 σ2 ∧ TcbFinKeepA σ1 σ2 ∧ SStepA σ1 σ2 ∧ NoBlockESc σ1 σ2 ∧ + (∀ x, σ1.kheap (specPtr p) = some (.notification x) → + ∃ x', σ2.kheap (specPtr p) = some (.notification x') ∧ x'.ntfnObj = x.ntfnObj) := by + have hna := hP.n.ntfn p hp + obtain ⟨x, hk, hrel⟩ := hR.ntfn p hp + have eB : ntfnBtA p = Notif.nBoundA p := rfl + by_cases h0 : L.mem s1 (Notif.nBoundA p) = 0#u64 + · have hn : x.ntfnBoundTcb = none := by rw [hrel.bound, h0, Notif.optRef_zero] + obtain ⟨e1, e2⟩ := irqUnbindNotificationTcb_nullA (st := st) hk hn h0 + exact ⟨s1, σ1, e1, e2, WritesIn.refl _ _, hR, hE, ⟨SpecSchedA.refl _, SSzA.refl _, rfl⟩, + NtfnSpecKeepA.refl _, fun _ y h => ⟨y, h, rfl, rfl, rfl⟩, + ⟨SFixA.refl _, rfl, rfl, rfl, fun _ _ _ _ => rfl, fun _ y h => ⟨y, h, rfl, rfl, rfl⟩, fun _ _ _ _ => rfl, + fun _ y h => ⟨y, h⟩, fun _ y h => ⟨y, h, rfl⟩⟩, + fun _ x y hx hy => by rw [hx] at hy; cases hy; exact Or.inl ⟨rfl, id⟩, fun z hz => ⟨z, hz, rfl⟩⟩ + · have htT := hrel.bound_mem h0 + let t := L.mem s1 (Notif.nBoundA p) + obtain ⟨y, hky, -⟩ := hR.tcb t htT + have hn : x.ntfnBoundTcb = some (specPtr t) := by rw [hrel.bound]; exact Notif.optRef_ne h0 + obtain ⟨e1, e2⟩ := irqUnbindNotificationTcb_someA (st := st) hk hky h0 hn rfl + let s2 := L.wr (L.wr s1 (ntfnBtA p) 0#u64) (boundA t) 0#u64 + let x' : Notification := { x with ntfnBoundTcb := none } + let y' : Tcb := { y with tcbBoundNotification := none } + let σm := updKSc σ1 (specPtr p) (.notification x') + have ne : specPtr t ≠ specPtr p := fun e => by rw [e, hk] at hky; cases hky + have kym : σm.kheap (specPtr t) = some (.tcb y) := by + show (updKSc σ1 _ _).kheap _ = _ + rw [updKSc_kheap, Function.update_of_ne ne]; exact hky + have hta := hP.e.tcb t htT + have aB : Aligned (boundA t) := (a_bound hta).1 + have vN : (ntfnBtA p).val = p.val + 24 := by + rw [eB, Notif.nBoundA_eq, Notif.nfield_val hna (by simp)]; simp + have aN : Aligned (ntfnBtA p) := by + rw [eB, Notif.nBoundA_eq]; exact Notif.nfield_aligned hna (by simp) (by simp) + have wr : WritesIn st s1 s2 (fun b => b = ntfnBtA p ∨ ∃ u ∈ T, b = boundA u) := by + refine ⟨by rw [L.ksb_wr, L.ksb_wr], by rw [L.cpu_wr, L.cpu_wr], fun b hb hn => ?_⟩ + rw [L.mem_wr_ne aB hb (fun h => hn (Or.inr ⟨t, htT, (UScalar.eq_of_val_eq h).symm⟩)), + L.mem_wr_ne aN hb (fun h => hn (Or.inl (UScalar.eq_of_val_eq h).symm))] + have vb : (boundA t).val = t.val + 3368 := by + have e : boundA t = tBoundA t := rfl + rw [e]; exact val_bound hta + have zN : L.mem s2 (Notif.nBoundA p) = 0#u64 := by + have neq : (boundA t).val ≠ (ntfnBtA p).val := by + have := hP.n.disj p hp t htT + omega + show L.mem (L.wr (L.wr s1 (ntfnBtA p) 0#u64) (boundA t) 0#u64) (ntfnBtA p) = 0#u64 + rw [L.mem_wr_ne aB aN neq] + exact L.mem_wr_same aN + have sc2 : L.mem s2 (Notif.nScA p) = L.mem s1 (Notif.nScA p) := by + have f := Notif.foot_sc hP.n hp + have vS : (Notif.nScA p).val = p.val + 32 := by + rw [Notif.nScA_eq, Notif.nfield_val hna (by simp)]; simp + refine wr.mem_eq f.1 ?_ + rintro (e | ⟨u, hu, e⟩) + · have := congrArg (fun w : U64 => w.val) e + simp only [vS, vN] at this + omega + · have hua := hP.e.tcb u hu + have vu : (boundA u).val = u.val + 3368 := by + have e : boundA u = tBoundA u := rfl + rw [e]; exact val_bound hua + have := hP.n.disj p hp u hu + have := congrArg (fun w : U64 => w.val) e + simp only [vS, vu] at this + omega + have R1 : Notif.NtfnStateRel N T (L.mem s2) σm := by + refine NtfnStateRel.relinkA hR hP.n hp (x' := x') hk rfl (fun b hb f n1 _ => wr.mem_eq hb ?_) + (by rw [zN, Notif.optRef_zero]) (fun h => absurd zN h) (by rw [sc2]; exact hrel.sc) + rintro (e | ⟨u, hu, e⟩) + · exact n1 (e.trans eB) + · have hua := hP.e.tcb u hu + have vu : (boundA u).val = u.val + 3368 := by + have e : boundA u = tBoundA u := rfl + rw [e]; exact val_bound hua + have v : ∀ K : U64, K.val < 4096 → (wa u K).val = u.val + K.val := fun K hK => Notif.tfield_val hua hK + rcases f with ⟨q, hq, h1, h2⟩ | ⟨w, hw, h⟩ + · have := hP.n.disj q hq u hu + rw [e, vu] at h1 h2; omega + · have hwa := hP.e.tcb w hw + have vw : ∀ K : U64, K.val < 4096 → (wa w K).val = w.val + K.val := fun K hK => Notif.tfield_val hwa hK + have rng : ∃ k, (k = 3328 ∨ k = 3336 ∨ k = 256 ∨ k = 3448 ∨ k = 3496 ∨ k = 3504) ∧ b.val = w.val + k := by + rcases h with h | h | h | h | h | h <;> rw [h] + · exact ⟨3328, by simp, by rw [stA_eq, vw _ (by simp)]; simp⟩ + · exact ⟨3336, by simp, by rw [Notif.tBlockA_eq, vw _ (by simp)]; simp⟩ + · exact ⟨256, by simp, by rw [Notif.badgeRegA_eq, vw _ (by simp)]; simp⟩ + · exact ⟨3448, by simp, by rw [Notif.prioA_eq', vw _ (by simp)]; simp⟩ + · exact ⟨3496, by simp, by rw [Notif.nextEp_eq, vw _ (by simp)]; simp⟩ + · exact ⟨3504, by simp, by rw [Notif.prevEp_eq, vw _ (by simp)]; simp⟩ + obtain ⟨k, hk', hv⟩ := rng + rw [e, vu] at hv + by_cases euw : u = w + · subst euw; omega + · have := tcb_disj hP.e hu hw (Ne.symm euw) + omega + have eσ : updKSc (updKSc σ1 (specPtr p) (.notification x')) (specPtr t) (.tcb y') = + updKSc σm (specPtr t) (.tcb y') := rfl + have R2 : Notif.NtfnStateRel N T (L.mem s2) (updKSc σm (specPtr t) (.tcb y')) := by + refine R1.frame hP.n (fun _ _ _ => rfl) ⟨fun q z h => ?_, fun q z h => ?_⟩ + · have : q ≠ specPtr t := fun e => by rw [e, kym] at h; cases h + rw [updKSc_kheap, Function.update_of_ne this]; exact h + · by_cases e : q = specPtr t + · subst e; rw [kym] at h; cases h + exact ⟨y', by rw [updKSc_kheap, Function.update_self], rfl, rfl, rfl⟩ + · exact ⟨z, by rw [updKSc_kheap, Function.update_of_ne e]; exact h, rfl, rfl, rfl⟩ + have EP : EpStateRel T E R Bf (L.mem s2) (updKSc σm (specPtr t) (.tcb y')) := + EpStateRel.unbindBound_wr hE hP hp htT hk hky + have F : SchedFrameA σ1 (updKSc σm (specPtr t) (.tcb y')) := by + have f1 : SchedFrameA σ1 σm := SchedFrameA.upd_simple hk (Or.inr (Or.inl ⟨_, _, rfl, rfl⟩)) + (fun _ _ e => by cases e) + have f2 : SchedFrameA σm (updKSc σm (specPtr t) (.tcb y')) := SchedFrameA.upd_tcb kym ⟨rfl, rfl, rfl⟩ + exact PreO.trans f1 f2 + have atp : (updKSc σm (specPtr t) (.tcb y')).kheap (specPtr p) = some (.notification x') := by + rw [updKSc_kheap, Function.update_of_ne ne.symm] + show (updKSc σ1 _ _).kheap _ = _ + rw [updKSc_kheap, Function.update_self] + have other : ∀ q, q ≠ specPtr t → q ≠ specPtr p → (updKSc σm (specPtr t) (.tcb y')).kheap q = σ1.kheap q := by + intro q h1 h2 + rw [updKSc_kheap, Function.update_of_ne h1] + show (updKSc σ1 _ _).kheap _ = _ + rw [updKSc_kheap, Function.update_of_ne h2] + have att : (updKSc σm (specPtr t) (.tcb y')).kheap (specPtr t) = some (.tcb y') := by + rw [updKSc_kheap, Function.update_self] + have same : ∀ q (o : KernelObject), (∀ z, o ≠ .tcb z) → (∀ z, o ≠ .notification z) → + (σ1.kheap q = some o ↔ (updKSc σm (specPtr t) (.tcb y')).kheap q = some o) := by + intro q o h1 h2 + by_cases e : q = specPtr t + · subst e; rw [att, hky] + exact ⟨fun h => absurd (Option.some.inj h).symm (h1 _), fun h => absurd (Option.some.inj h).symm (h1 _)⟩ + · by_cases e' : q = specPtr p + · subst e'; rw [atp, hk] + exact ⟨fun h => absurd (Option.some.inj h).symm (h2 _), fun h => absurd (Option.some.inj h).symm (h2 _)⟩ + · rw [other q e e'] + have tcbF : ∀ q z, σ1.kheap q = some (.tcb z) → ∃ w, (updKSc σm (specPtr t) (.tcb y')).kheap q = some (.tcb w) ∧ + w.tcbState = z.tcbState ∧ w.tcbFault = z.tcbFault ∧ w.tcbSchedContext = z.tcbSchedContext ∧ + w.tcbDomain = z.tcbDomain ∧ w.tcbPriority = z.tcbPriority ∧ w.tcbIpcframe = z.tcbIpcframe ∧ + w.tcbIpcBuffer = z.tcbIpcBuffer := by + intro q z h + by_cases e : q = specPtr t + · subst e; rw [hky] at h; cases h + exact ⟨y', att, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ + · have e' : q ≠ specPtr p := fun e => by rw [e, hk] at h; cases h + exact ⟨z, (other q e e').trans h, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ + have tcbB : ∀ q w, (updKSc σm (specPtr t) (.tcb y')).kheap q = some (.tcb w) → ∃ z, σ1.kheap q = some (.tcb z) := by + intro q w h + by_cases e : q = specPtr t + · subst e; exact ⟨y, hky⟩ + · have e' : q ≠ specPtr p := fun e => by rw [e, atp] at h; cases h + exact ⟨w, (other q e e').symm.trans h⟩ + have S : SStepA σ1 (updKSc σm (specPtr t) (.tcb y')) := by + refine ⟨⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩, rfl, rfl, rfl, fun q z k h => ?_, fun q z h => ?_, + fun q z k h => ?_, tcbB, fun q w h => ?_⟩ + · exact ((same q _ (fun _ e => by cases e) (fun _ e => by cases e)).1 h).trans h.symm + · obtain ⟨w, hw, -, -, a, b, c, -⟩ := tcbF q z h + exact ⟨w, hw, a, b, c⟩ + · exact ((same q _ (fun _ e => by cases e) (fun _ e => by cases e)).2 h).trans h.symm + · by_cases e : q = specPtr t + · subst e; rw [att] at h; cases h + · by_cases e' : q = specPtr p + · subst e'; rw [atp] at h; cases h; exact ⟨x, hk, rfl⟩ + · exact ⟨w, (other q e e').symm.trans h, rfl⟩ + have B : NoBlockESc σ1 (updKSc σm (specPtr t) (.tcb y')) := by + intro q z w h h' + obtain ⟨w', hw, a, b, -⟩ := tcbF q z h + rw [hw] at h'; cases h' + exact Or.inl ⟨a, by rw [a, b]; exact id⟩ + refine ⟨s2, updKSc σm (specPtr t) (.tcb y'), e1, e2, wr, R2, EP, F, NtfnSpecKeepA.unbindTcb hk hky, + fun q z h => ?_, S, B, fun z hz => ?_⟩ + · obtain ⟨w, hw, -, b, -, -, -, c, d⟩ := tcbF q z h + exact ⟨w, hw, b, c, d⟩ + · rw [hk] at hz; cases hz + exact ⟨x', atp, rfl⟩ + +set_option maxHeartbeats 1000000 in +/-- **The record across the final Notification finaliser** (module docstring). -/ +theorem JStage3TA.ntfnFin_recA {ar : machine.Arch A} {ipa : U64} + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7LA st (J7R2A st ar ipa)) s a σ m) + {p b0 : U64} {b1 b2 : Bool} (hH : H p = some .ntfn) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (hon : ∀ z, σ.kheap (specPtr p) = some (.notification z) → ∀ t ∈ Z.T, + specPtr t ∈ z.ntfnObj.ntfnQueue → L.mem s (affA t) = L.cpu s) : + objects.finalise_cap_object st ar s a (.Notification p b0 b1 b2) true ⦃ r => + r.1 = (.Null, .Null) ∧ r.2.2 = a ∧ ∃ σ1 σ2 σ3 Q R, + ((), σ1) ∈ (schedContextMaybeUnbindNtfn (Z := Unit) (specPtr p) σ).1 ∧ + ((), σ2) ∈ (unbindMaybeNotification (Z := Unit) (specPtr p) σ1).1 ∧ + ((), σ3) ∈ (cancelAllSignals (Z := Unit) (specPtr p) σ2).1 ∧ + JStage3TA st AR { Z with Q := Q, R := R } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7LA st (J7R2A st ar ipa)) r.2.1 a σ3 m ∧ + OSKeep st OS s r.2.1 ∧ L.ksb r.2.1 = L.ksb s ∧ L.cpu r.2.1 = L.cpu s ∧ SchedFrameA σ σ3 ∧ + (∀ t ∈ Z.T, L.mem r.2.1 (wa t 3664#u64) = L.mem s (wa t 3664#u64)) ∧ + Std6KeepA st Z H OS Bf s σ r.2.1 σ3 ⦄ := by + have hp : p ∈ N := J.ipcHeapCov.ntfn p hH + have hc := J.curCore_lt hCs + have hP := J.iplaced + have hp0 : p ≠ 0#u64 := J.sr.place.nz p _ (J.ipcPlace.notification p hp) + have blk := J.ntfnFin_blkA (ar := ar) (p := p) (b0 := b0) (b1 := b1) (b2 := b2) (fin := true) hH hH + have rvi := Fl4ProofsA64.P1.fin_ntfn_obj.rvi (ar := ar) s a p b0 b1 b2 true J.blkA (show p ∈ H.ntfns from hH) hH + (J.tcbs ▸ J.runVcpu) + refine WP.spec_mono (WP.spec_and (WP.spec_and blk rvi) (?main : objects.finalise_cap_object st ar s a (.Notification p b0 b1 b2) true + ⦃ r => r.1 = (.Null, .Null) ∧ r.2.2 = a ∧ + (BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac r.2.1 → + RunVcpuInv st H.tcbs r.2.1 → ∃ σ1 σ2 σ3 Q R, + ((), σ1) ∈ (schedContextMaybeUnbindNtfn (Z := Unit) (specPtr p) σ).1 ∧ + ((), σ2) ∈ (unbindMaybeNotification (Z := Unit) (specPtr p) σ1).1 ∧ + ((), σ3) ∈ (cancelAllSignals (Z := Unit) (specPtr p) σ2).1 ∧ + JStage3TA st AR { Z with Q := Q, R := R } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7LA st (J7R2A st ar ipa)) r.2.1 a σ3 m ∧ + OSKeep st OS s r.2.1 ∧ L.ksb r.2.1 = L.ksb s ∧ L.cpu r.2.1 = L.cpu s ∧ SchedFrameA σ σ3 ∧ + (∀ t ∈ Z.T, L.mem r.2.1 (wa t 3664#u64) = L.mem s (wa t 3664#u64)) ∧ + Std6KeepA st Z H OS Bf s σ r.2.1 σ3) ⦄)) ?_ + case main => + simp only [objects.finalise_cap_object, ↓reduceIte] + refine WP.spec_bind (J.ntfnScUnlinkA hp hH hp0 hc) ?_ + rintro s1 ⟨σ1, sp1, H1, R1, E1, w1, w1m, F1, K1, T1, q1⟩ + obtain ⟨s2, σ2, ev2, sp2, w2, R2, E2, F2, K2, T2, S2, B2, q2⟩ := ntfnBoundUnlinkA (st := st) R1 E1 hP hp + rw [ev2, bind_ok] + have F12 : SchedFrameA σ σ2 := PreO.trans F1 F2 + have kb2 : L.ksb s2 = L.ksb s := w2.1.trans w1.1 + have H2 : SchedSRA st Z m κ s2 σ2 := by + refine H1.stable w2 (fun b hb => ?_) S2 B2 + have kb1 : L.ksb s1 = L.ksb s := w1.1 + -- both words are read by no scheduler relation at `s1` + have hna := hP.n.ntfn p hp + rcases hb with rfl | ⟨u, hu, rfl⟩ + · have v := J.ntfnLink_ipcA hp (b := ntfnBtA p) (Or.inr rfl) + rintro (⟨c, hc', o, ho, e⟩ | e | e | ⟨t, ht, K, hK, e⟩ | h) + · have hn := H1.node_in hc' ho + have hk := J.ipc_ksA (b := ntfnBtA p) (Or.inl ⟨p, hp, v.1, by omega⟩) + rw [← e, kb1] at hn + omega + · have hk := J.ipc_ksA (b := ntfnBtA p) (Or.inl ⟨p, hp, v.1, by omega⟩) + have hfit := J.sr.ksFit + have : (wa (L.ksb s1) 8#u64).val = (L.ksb s).val + 8 := by + rw [kb1, wa_valm, show (8#u64 : U64).val = 8 by simp]; unfold ksSizeA at hfit; exact Nat.mod_eq_of_lt (by omega) + rw [e, this] at hk + unfold ksSizeA at hk; omega + · have hk := J.ipc_ksA (b := ntfnBtA p) (Or.inl ⟨p, hp, v.1, by omega⟩) + have hfit := J.sr.ksFit + have : (wa (L.ksb s1) 16#u64).val = (L.ksb s).val + 16 := by + rw [kb1, wa_valm, show (16#u64 : U64).val = 16 by simp]; unfold ksSizeA at hfit; exact Nat.mod_eq_of_lt (by omega) + rw [e, this] at hk + unfold ksSizeA at hk; omega + · have htH : H t = some .tcb := by + have : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + exact this + have k4 : K.val < 4096 := by rcases hK with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> simp + have hfit := (J.sr.core.tcbs t ht).fits + have hv : (wa t K).val = t.val + K.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact J.ipc_off_hobjA (v := t) (kv := .tcb) htH (by simp) + ⟨by rw [e, hv]; omega, by rw [e, hv]; simp only [HKind.bits]; omega⟩ (Or.inl ⟨p, hp, v.1, by omega⟩) + · have := H1.scp.th_out h (J.ipcPlace.notification p hp) + simp only [ObjKindA.size] at this + omega + · have hfit := (J.sr.core.tcbs u hu).fits + have v : (boundA u).val = u.val + 3368 := by + have e : boundA u = wa u 3368#u64 := rfl + rw [e, wa_valm, show (3368#u64 : U64).val = 3368 by simp]; exact Nat.mod_eq_of_lt (by omega) + have hth := (J.sr.thT u).2 hu + have hks := J.sr.tcb_ks hu + have hsz : ObjKindA.size .tcb = 4096 := rfl + rintro (⟨c, hc', o, ho, e⟩ | e | e | ⟨t, ht, K, hK, e⟩ | h) + · have hn := H1.node_in hc' ho + rw [← e, kb1, v] at hn + unfold ksSizeA at hks hn; omega + · have hfit' := J.sr.ksFit + have : (wa (L.ksb s1) 8#u64).val = (L.ksb s).val + 8 := by + rw [kb1, wa_valm, show (8#u64 : U64).val = 8 by simp]; unfold ksSizeA at hfit'; exact Nat.mod_eq_of_lt (by omega) + have h' := congrArg (fun w : U64 => w.val) e + simp only [v, this] at h' + unfold ksSizeA at hks; omega + · have hfit' := J.sr.ksFit + have : (wa (L.ksb s1) 16#u64).val = (L.ksb s).val + 16 := by + rw [kb1, wa_valm, show (16#u64 : U64).val = 16 by simp]; unfold ksSizeA at hfit'; exact Nat.mod_eq_of_lt (by omega) + have h' := congrArg (fun w : U64 => w.val) e + simp only [v, this] at h' + unfold ksSizeA at hks; omega + · have k4 : K.val < 4096 := by rcases hK with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> simp + have ne : K.val ≠ 3368 := by rcases hK with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> simp + have hft := (J.sr.core.tcbs t ht).fits + have hv : (wa t K).val = t.val + K.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have h' := congrArg (fun w : U64 => w.val) e + simp only [v, hv] at h' + by_cases eu : t = u + · subst eu; omega + · have := J.sr.place.apart t u .tcb .tcb ((J.sr.thT t).2 ht) hth eu + omega + · have := H1.scp.th_out h hth + omega + -- the queued waiters and the loop's call + let Q : Set U64 := {q | q ∈ Z.T ∧ L.mem s2 (tBlockA q) = p} + have hQ : Q ⊆ Z.T := fun _ h => h.1 + have w02 : WritesIn st s s2 (NtfnFinW st Z m σ s Q p) := by + refine (w1.trans w2).mono fun b hb => ?_ + rcases hb with hb | hb | hb + · rcases w1m b hb with h | h + · exact Or.inl h + · exact Or.inr (Or.inr (Or.inr (Or.inr h))) + · exact Or.inr (Or.inl hb) + · exact Or.inr (Or.inr (Or.inl hb)) + have al : ∀ t ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → Aligned (wa t K) := + fun t ht K h1 h2 => field_aligned (J.sr.core.tcbs t ht) h1 h2 + have tw : ∀ s' : S, WritesIn st s s' (NtfnFinW st Z m σ s Q p) → ∀ t ∈ Z.T, ∀ K : U64, K.val < 4096 → + K.val % 8 = 0 → (K.val ≠ 3328 ∧ K.val ≠ 3368 ∧ K.val ≠ 3464 ∧ K.val ≠ 3480 ∧ K.val ≠ 3488 ∧ K.val ≠ 3496 ∧ + K.val ≠ 3504 ∧ K.val ≠ 3592) → L.mem s' (wa t K) = L.mem s (wa t K) := + fun s' w t ht K h1 h2 h3 => w.mem_eq (al t ht K h1 h2) (J.tcbWord_offFinA hp hQ ht h1 h3) + have hon2 : ∀ z, σ2.kheap (specPtr p) = some (.notification z) → ∀ t ∈ Z.T, + specPtr t ∈ z.ntfnObj.ntfnQueue → L.mem s2 (affA t) = L.cpu s2 := by + intro z hz t ht hm + obtain ⟨x, hx, -⟩ := J.ntfn.ntfn p hp + obtain ⟨x1, hx1, o1, -⟩ := q1 x hx + obtain ⟨x2, hx2, o2⟩ := q2 x1 hx1 + rw [hz] at hx2; cases hx2 + have e : L.mem s2 (affA t) = L.mem s (affA t) := tw s2 w02 t ht 3520#u64 (by simp) (by simp) (by simp) + rw [e, w2.2.1.trans w1.2.1] + exact hon x hx t ht (by rw [o2, o1] at hm; exact hm) + have hQs : ∀ z, σ2.kheap (specPtr p) = some (.notification z) → ∀ t ∈ Z.T, + specPtr t ∈ z.ntfnObj.ntfnQueue → t ∈ Q := by + intro z hz t ht hm + obtain ⟨x, hx, hr⟩ := R2.ntfn p hp + rw [hz] at hx; cases hx + exact ⟨ht, (hr.obj.blocked hm).2.2⟩ + obtain ⟨hHy, hO, -⟩ := J.schedOpsA (ar := ar) hc hmb hmax htu + refine WP.spec_bind (Notif.cancel_all_signals_relF hHy hO (J.schedFootNA hc hmb) hP.n s2 σ2 R2 + ⟨Z.Q, Z.R, H2⟩ hp hon2 hQs) ?_ + rintro s3 ⟨-, σ3, h3, R3, SR3, K3, -, w3, T3⟩ + refine (WP.spec_ok _).2 ⟨rfl, rfl, fun blk3 rv3 => ?_⟩ + have F : SchedFrameA σ σ3 := PreO.trans F12 (cancelAllSignals_presA (specPtr p) σ2 ((), σ3) h3) + have K : NtfnSpecKeepA σ σ3 := (K1.trans K2).trans K3 + have TK : TcbFinKeepA σ σ3 := (T1.fin.trans T2).trans T3.fin + have h1 : ((), σ1) ∈ (schedContextMaybeUnbindNtfn (Z := Unit) (specPtr p) σ).1 := by rw [sp1]; exact rfl + have h2 : ((), σ2) ∈ (unbindMaybeNotification (Z := Unit) (specPtr p) σ1).1 := by rw [sp2]; exact rfl + have w03 : WritesIn st s s3 (NtfnFinW st Z m σ s Q p) := by + refine (w02.trans w3).mono fun b hb => ?_ + rcases hb with hb | hb | hb + · exact hb + · exact Or.inr (Or.inr (Or.inr (Or.inl hb))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (mwr_back (Z := Z) (Z' := Z) rfl rfl F12.2.1 kb2 hb)))) + have w : WritesIn st s s3 (SchedStepNWA (st := st) N E Rp Z m σ s) := w03.mono fun _ h => J.finW_stepNA hp hQ h + obtain ⟨Q', R', H3⟩ := SR3 + have sq : SchedQueuesInv st H.tcbs Z.Cs H.omega H s3 Q' R' := by + obtain ⟨Q0, R0, h0⟩ := blk3.1 + exact SchedQueuesInv.reQA h0 (by rw [J.tcbs]; exact H3.core) + have kb3 : L.ksb s3 = L.ksb s := w03.1 + have poolOff : PoolOff st H Z.Cs OS s3 := fun i => by + have h0 := J.poolOff i + rw [vspace.asid_table.eq] at h0 ⊢ + have v := AtOff.v i + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.ent.ksw + have ev : (wa (L.ksb s) (AtOff i)).val = (L.ksb s).val + (AtOff i).val := by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have e : L.mem s3 (wa (L.ksb s3) (AtOff i)) = L.mem s (wa (L.ksb s) (AtOff i)) := by + rw [kb3] + refine w.mem_eq ?_ (J.asid_offStepNA (by omega) (by omega)) + have h32 := J.j.1.ksbAl + show (wa (L.ksb s) (AtOff i)).val % 8 = 0 + omega + rw [e] + refine (WP.spec_ok _).2 fun hne asid => ?_ + have q := (WP.spec_ok _).1 h0 hne asid + exact ⟨q.al, q.om, q.sg, q.os, RegionOffNodes.ksb kb3 q.ks⟩ + have ep : EpStateRel Z.T E Rp Bf (L.mem s3) σ3 := + EpStateRel.frameN E2 hP hp (Q := Q) (fun _ h => h) + (fun b hb hf hn => w3.mem_eq hb fun h => h.elim hn (endpFootOffWrite H2.toSchedSRN J.ipcPlace hf)) + (fun t ht z hz => by + obtain ⟨y, hy, r⟩ := R3.tcb t ht + rw [hz] at hy; cases hy + exact r.st) T3 + have reply : ReplyStackRelA (L.mem s3) Rp Z.C σ3 (scNameA m) stack := by + refine J.reply.ntfnFr K fun r hr => ?_ + obtain ⟨r32, -, rfit⟩ := hP.e.rep r hr + have one : ∀ o : U64, o.val < 32 → o.val % 8 = 0 → L.mem s3 (wa r o) = L.mem s (wa r o) := by + intro o o1 o2 + have v : (wa r o).val = r.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + refine w03.mem_eq ?_ (J.reply_offFinA hp hQ hr (by omega) (by omega)) + show (wa r o).val % 8 = 0 + omega + have n16 : (layout.reply.NEXT).val = 16 := by unfold layout.reply.NEXT; rfl + have p8 : (layout.reply.PREV).val = 8 := by unfold layout.reply.PREV; rfl + exact ⟨one _ (by omega) (by omega), one _ (by omega) (by omega)⟩ + have mcp : McpRelA Z.T m (L.mem s3) σ3 := + J.mcp.ntfnFr K fun t ht => ⟨tw s3 w03 t ht 3440#u64 (by simp) (by simp) (by simp), + tw s3 w03 t ht 3456#u64 (by simp) (by simp) (by simp)⟩ + have yr : YieldRelA Z.T Z.C m (L.mem s3) σ3 := + J.yieldRel.ntfnFr K fun t ht => tw s3 w03 t ht 3472#u64 (by simp) (by simp) (by simp) + have buf : BufCover st Z.T Bf s3 σ3 := + J.bufCover_wA w03 (fun t ht K hK => J.tcbWord_offFinA hp hQ ht (by omega) (by omega)) + (fun q x h => by + obtain ⟨y, hy, -, b, c⟩ := TK q x h + exact ⟨y, hy, b, c⟩) + have bufAl : UtBufAlA Z.T σ3 := fun t ht y hy => by + obtain ⟨x, hx, -⟩ := J.ntfn.tcb t ht + obtain ⟨y', hy', -, -, e⟩ := TK _ _ hx + rw [hy] at hy'; cases hy' + rw [e]; exact J.bufAl t ht x hx + have idle : IdleNoFaultA m σ3 := fun c hc' => by + obtain ⟨x, hx, hf⟩ := J.idleNoFault c hc' + obtain ⟨y, hy, e, -⟩ := TK _ _ hx + exact ⟨y, hy, e.trans hf⟩ + have scCov : ScCovA Z.C m σ3 := fun q x k h => by + obtain ⟨y, hy⟩ := F.2.1 q x k h + exact J.scCov q y k hy + have inv := J.ntfnFin_invA h1 h2 R2 hp h3 + have j := J.j57_stepNWA w σ3 + have U1 : RefK σ σ1 := schedContextMaybeUnbindNtfn_refK (specPtr p) σ ((), σ1) h1 + have U2 : RefK σ1 σ2 := unbindMaybeNotification_refK (specPtr p) σ1 ((), σ2) h2 + have nsw : NoSwA σ3 := by + have n1 : NoSwA σ1 := fun u c => J.noSwitch u (by rw [U1.1] at c; exact c) + have n2 : NoSwA σ2 := fun u c => n1 u (by rw [U2.1] at c; exact c) + exact cancelAllSignals_noSwA (specPtr p) n2 h3 + have hvr : ∀ sc ∈ Z.C, ∀ q ∈ stack sc, RBlk st s3 q := by + refine J.vr_specA blk3 (fun _ _ h => h) (fun sc _ q _ c _ _ x o hx hs => ?_) ep + (fun c hc => tw s3 w03 c hc 3360#u64 (by simp) (by simp) (by simp)) + obtain ⟨y2, hy2, k2⟩ := ((U1.trans U2).2.1 (specPtr c)).tcbF hx + obtain ⟨y3, hy3, k3⟩ := T3.tcb _ _ hy2 + refine ⟨y3, hy3, ?_⟩ + rcases k3.2.2.2.2.2.2 with e | ⟨⟨nn, hn⟩, -⟩ + · exact e.trans k2.1 + · rw [k2.1, hs] at hn; cases hn + have bq : BlkQueuedA Z.T E N (L.mem s3) σ3 := by + obtain ⟨x, hx, -⟩ := J.ntfn.ntfn p hp + obtain ⟨x1, hx1, o1, -⟩ := q1 x hx + obtain ⟨x2, hx2, o2⟩ := q2 x1 hx1 + obtain ⟨M3, d⟩ := cancelAllSignals_blkA hx2 h3 + have M := ((schedContextMaybeUnbindNtfn_blkA (specPtr p) σ _ h1).trans + (unbindMaybeNotification_blkA (specPtr p) σ1 _ h2)).trans M3 + refine J.blkQ.finA M (fun t ht => ?_) (fun t ht => ?_) (fun y hy => ?_) (fun y q hy ho r hr => ?_) + · obtain ⟨z, hz, -⟩ := J.ntfn.tcb t ht + exact ⟨z, hz⟩ + · rw [Notif.tBlockA_eq]; exact tw s3 w03 t ht 3336#u64 (by simp) (by simp) (by simp) + · rw [hx] at hy; cases hy + · rw [hx] at hy; cases hy + exact Or.inl (d q (by rw [o2, o1]; exact ho) r hr) + have J' := JStage3TA.of_schedWrN (blkQ := bq) J (Q := Q') (Rl := R') (curOf := Z.curOf) (curScOf := Z.curScOf) (s' := s3) + (a' := a) (σ' := σ3) w F.1 F.2.1 (((Sched.qk_of h1).trans (Sched.qk_of h2)).trans (Sched.qk_of h3)) F.2.2 + J.j.1.ar (fun _ _ => rfl) bufAl (J.ent_stepNWA w blk3) sq poolOff H3 + scCov ep R3 reply buf idle mcp yr inv.1 inv.2.1 (J.tcbs ▸ rv3) hvr nsw inv.2.2 j.1 j.2 + exact ⟨σ1, σ2, σ3, Q', R', h1, h2, h3, J', J.sigW_osKeepA w, kb3, w03.2.1, F, + fun t ht => tw s3 w03 t ht 3664#u64 (by simp) (by simp) (by simp), + fun {_} sub h => Std6GA.schedStepA J h sub w (J.sigW_osKeepA w) F⟩ + · rintro r ⟨⟨b, rv⟩, h1, h2, h3⟩ + exact ⟨h1, h2, h3 b.2.2 rv⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelNtfnUnbindA.lean b/proofs/Fl4ProofsA64/P3/DelNtfnUnbindA.lean new file mode 100644 index 000000000..4fd58b499 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelNtfnUnbindA.lean @@ -0,0 +1,50 @@ +/- +P3 (aarch64, delete hooks W3, contract §45 step 2, executor 18): **the finaliser keeps of the SC unlink**. The +successor of `sched_context_(maybe_)unbind_ntfn` (`scNtfnLinkStA`, `P3/ScNtfnLinkA`) changes the notification's SC +link and the SC's notification link only, so it satisfies both `NtfnSpecKeepA` (`P3/DelNtfnFrameA`) and +`NtfnTcbKeepA` (`P3/SigTcbKeepA`). No named premise; new module. +-/ +import Fl4ProofsA64.P3.DelNtfnFrameA +import Fl4ProofsA64.P3.SigTcbKeepA +import Fl4ProofsA64.P3.ScNtfnLinkA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open Fl4SpecA64 Fl4ProofsA64.P3.Sched + +variable {σ : State Unit} {p q : ObjRef} {xs : SchedContext} {ns : Nat} {xn : Notification} + +/-- The SC unlink keeps what `mcp`, `yieldRel` and the reply stacks read. -/ +theorem NtfnSpecKeepA.scNtfnLink (hs : σ.kheap p = some (.schedContext xs ns)) + (hn : σ.kheap q = some (.notification xn)) (b : Bool) : + NtfnSpecKeepA σ (scNtfnLinkStA σ p q xs ns xn b) := by + have hpq : p ≠ q := by intro e; rw [e, hn] at hs; cases hs + unfold scNtfnLinkStA + refine NtfnSpecKeepA.trans + (σ2 := updKSc σ q (.notification { xn with ntfnSc := if b then some p else none })) + (NtfnSpecKeepA.of_upd hn rfl ⟨_, rfl⟩) + (NtfnSpecKeepA.of_upd (q := p) (o := .schedContext xs ns) ?_ rfl ⟨_, rfl, rfl, rfl⟩) + rw [updKSc_kheap, Function.update_of_ne hpq] + exact hs + +/-- The SC unlink keeps what `EpStateRel` reads. -/ +theorem NtfnTcbKeepA.scNtfnLink (hs : σ.kheap p = some (.schedContext xs ns)) + (hn : σ.kheap q = some (.notification xn)) (b : Bool) : + NtfnTcbKeepA σ (scNtfnLinkStA σ p q xs ns xn b) := by + have hpq : p ≠ q := by intro e; rw [e, hn] at hs; cases hs + have h2 : (updKSc σ q (.notification { xn with ntfnSc := if b then some p else none })).kheap p = + some (.schedContext xs ns) := by + rw [updKSc_kheap, Function.update_of_ne hpq] + exact hs + unfold scNtfnLinkStA + refine NtfnTcbKeepA.trans + (σ2 := updKSc σ q (.notification { xn with ntfnSc := if b then some p else none })) + (NtfnTcbKeepA.of_ntfn hn rfl rfl) (NtfnTcbKeepA.of_scOnly ⟨fun r o hr ho => ?_, rfl⟩) + have hrp : r ≠ p := fun e => by + rw [e, h2] at hr + injection hr with hr + exact ho xs ns hr.symm + rw [updKSc_kheap, Function.update_of_ne hrp] + exact hr + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelNtfnUnlinkA.lean b/proofs/Fl4ProofsA64/P3/DelNtfnUnlinkA.lean new file mode 100644 index 000000000..6670dab37 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelNtfnUnlinkA.lean @@ -0,0 +1,481 @@ +/- +P3 (AArch64, delete hooks W3, contract §53): **the two unlinks of the Notification finaliser at the record**. + +* `JStage3TA.ntfnScUnlinkA`: `maybe_unbind_ntfn` refines `schedContextMaybeUnbindNtfn`, keeping `SchedSRA`, + `NtfnStateRel` and `EpStateRel`, with its two-word write set and the spec keeps. +* `NtfnStateRel.relinkA`: `NtfnStateRel` across a change of one notification's bound / SC links. +* `cancelAllSignals_presA`: the spec loop is a scheduler step (`SchedFrameA`). + +No premise. +-/ +import Fl4ProofsA64.P3.IrqInvokeUnbindA +import Fl4ProofsA64.P3.DelNtfnEntA +import Fl4ProofsA64.P3.DelNtfnUnbindA +import Fl4ProofsA64.P3.IpcScGlueA +import Fl4ProofsA64.P3.SpecFrameIpcA +import Fl4ProofsA64.P3.ScUnbInvokeA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 Fl4ProofsA64.P3.Endp +open Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Notif + +local notation "wa" => core.num.U64.wrapping_add + +noncomputable section + +/-- The notification object relation reads the head, tail/state and badge words and its members. -/ +theorem NtfnObjRel.frame3A {m m' : U64 → U64} {T : Set U64} {n : U64} {o : Ntfn} (h : Notif.NtfnObjRel m T n o) + (hh : m' (epHeadA n) = m (epHeadA n)) (ht : m' (epTSA n) = m (epTSA n)) + (hb : m' (Notif.nBadgeA n) = m (Notif.nBadgeA n)) + (hy : ∀ y ∈ T, specPtr y ∈ o.ntfnQueue → Notif.MemberEq m m' y) : Notif.NtfnObjRel m' T n o := by + have eq : Notif.qOf m' n = Notif.qOf m n := by simp only [Notif.qOf, hh, ht] + have es : Notif.stOf m' n = Notif.stOf m n := by simp only [Notif.stOf, ht] + cases o with + | idleNtfn => + obtain ⟨h1, h2⟩ := h + exact ⟨es ▸ h1, eq ▸ Notif.queueRel_congr_links h2 (by simp)⟩ + | activeNtfn b => + obtain ⟨h1, h2, h3⟩ := h + exact ⟨es ▸ h1, eq ▸ Notif.queueRel_congr_links h2 (by simp), hb ▸ h3⟩ + | waitingNtfn q => + obtain ⟨h1, xs, hne, rfl, h2, h3⟩ := h + have hy' : ∀ y ∈ xs, Notif.MemberEq m m' y := fun y hyx => + hy y (h2.mem y hyx) (by simp only [Ntfn.ntfnQueue]; exact List.mem_map_of_mem hyx) + refine ⟨es ▸ h1, xs, hne, rfl, eq ▸ Notif.queueRel_congr_links h2 (fun y hyx => (hy' y hyx).2.2), + fun y hyx => ?_⟩ + obtain ⟨e1, e2⟩ := h3 y hyx + exact ⟨(hy' y hyx).1 ▸ e1, (hy' y hyx).2.1 ▸ e2⟩ + +/-- **`NtfnStateRel` across a change of one notification's bound-TCB / SC links**: the words other than that +notification's two link words agree. -/ +theorem NtfnStateRel.relinkA {Zx : Type} {N T : Set U64} {m m' : U64 → U64} {σ : State Zx} + (hR : Notif.NtfnStateRel N T m σ) (hP : Notif.NPlaced T N) {n : U64} (hn : n ∈ N) {x x' : Notification} + (hk : σ.kheap (specPtr n) = some (.notification x)) (ho : x'.ntfnObj = x.ntfnObj) + (hm : ∀ a, Aligned a → Notif.NFoot N T a → a ≠ Notif.nBoundA n → a ≠ Notif.nScA n → m' a = m a) + (hb : x'.ntfnBoundTcb = Notif.optRef (m' (Notif.nBoundA n))) + (hbm : m' (Notif.nBoundA n) ≠ 0#u64 → m' (Notif.nBoundA n) ∈ T) + (hs : x'.ntfnSc = Notif.optRef (m' (Notif.nScA n))) : + Notif.NtfnStateRel N T m' { σ with kheap := Function.update σ.kheap (specPtr n) (some (.notification x')) } := by + have hna := hP.ntfn n hn + have vB : (Notif.nBoundA n).val = n.val + 24 := by + rw [Notif.nBoundA_eq, Notif.nfield_val hna (by simp)]; simp + have vS : (Notif.nScA n).val = n.val + 32 := by + rw [Notif.nScA_eq, Notif.nfield_val hna (by simp)]; simp + have nw : ∀ n' ∈ N, ∀ K : U64, K.val < 40 → K.val % 8 = 0 → (n' ≠ n ∨ (K.val ≠ 24 ∧ K.val ≠ 32)) → + m' (wa n' K) = m (wa n' K) := by + intro n' hn' K k1 k2 k3 + have ha' := hP.ntfn n' hn' + have w := Notif.nword hP hn' k1 k2 + have v : (wa n' K).val = n'.val + K.val := Notif.nfield_val ha' (by omega) + have al := hna.align + have al' := ha'.align + have ne : ∀ k : Nat, k = 24 ∨ k = 32 → n'.val + K.val ≠ n.val + k := by + intro k hk e + rcases k3 with h | ⟨h1, h2⟩ + · exact h (UScalar.eq_of_val_eq (by omega)) + · omega + refine hm _ w.1 w.2 (fun e => ?_) (fun e => ?_) + · have := congrArg (fun w : U64 => w.val) e + simp only [v, vB] at this + exact ne 24 (Or.inl rfl) this + · have := congrArg (fun w : U64 => w.val) e + simp only [v, vS] at this + exact ne 32 (Or.inr rfl) this + have tw : ∀ t ∈ T, ∀ a : U64, t.val ≤ a.val → a.val < t.val + 4096 → Aligned a → Notif.NFoot N T a → + m' a = m a := by + intro t ht a h1 h2 al f + have := hP.disj n hn t ht + refine hm a al f (fun e => ?_) (fun e => ?_) + · rw [e, vB] at h1 h2; omega + · rw [e, vS] at h1 h2; omega + have tf : ∀ t ∈ T, Notif.TFieldsEq m m' t := by + intro t ht + have hta := hP.tcb t ht + have go : ∀ a : U64, Aligned a ∧ Notif.NFoot N T a → t.val ≤ a.val → a.val < t.val + 4096 → m' a = m a := + fun a h h1 h2 => tw t ht a h1 h2 h.1 h.2 + have v : ∀ K : U64, K.val < 4096 → (wa t K).val = t.val + K.val := fun K hK => Notif.tfield_val hta hK + refine ⟨go _ (Notif.foot_st hP ht) ?_ ?_, go _ (Notif.foot_blk hP ht) ?_ ?_, + go _ (Notif.foot_breg hP ht) ?_ ?_, go _ (Notif.foot_prio hP ht) ?_ ?_⟩ <;> + first + | (rw [stA_eq, v _ (by simp)]; simp) + | (rw [Notif.tBlockA_eq, v _ (by simp)]; simp) + | (rw [Notif.badgeRegA_eq, v _ (by simp)]; simp) + | (rw [Notif.prioA_eq', v _ (by simp)]; simp) + have tl : ∀ t ∈ T, Notif.TLinksEq m m' t := by + intro t ht + have hta := hP.tcb t ht + have go : ∀ a : U64, Aligned a ∧ Notif.NFoot N T a → t.val ≤ a.val → a.val < t.val + 4096 → m' a = m a := + fun a h h1 h2 => tw t ht a h1 h2 h.1 h.2 + have v : ∀ K : U64, K.val < 4096 → (wa t K).val = t.val + K.val := fun K hK => Notif.tfield_val hta hK + refine ⟨go _ (Notif.foot_prev hP ht) ?_ ?_, go _ (Notif.foot_next hP ht) ?_ ?_⟩ <;> + first + | (rw [Notif.prevEp_eq, v _ (by simp)]; simp) + | (rw [Notif.nextEp_eq, v _ (by simp)]; simp) + refine hR.upd_ntfn hn x' (fun n' hn' ne => ?_) tf (fun t ht _ => tl t ht) ?_ + · exact ⟨by rw [Notif.epHeadA_eq]; exact nw n' hn' _ (by simp) (by simp) (Or.inl ne), + by rw [Notif.epTSA_eq]; exact nw n' hn' _ (by simp) (by simp) (Or.inl ne), + by rw [Notif.nBadgeA_eq]; exact nw n' hn' _ (by simp) (by simp) (Or.inl ne), + by rw [Notif.nBoundA_eq]; exact nw n' hn' _ (by simp) (by simp) (Or.inl ne), + by rw [Notif.nScA_eq]; exact nw n' hn' _ (by simp) (by simp) (Or.inl ne)⟩ + · obtain ⟨y, hky, hy⟩ := hR.ntfn n hn + rw [hk] at hky + cases hky + refine ⟨?_, hb, hbm, hs⟩ + rw [ho] + refine NtfnObjRel.frame3A hy.obj ?_ ?_ ?_ (fun t ht _ => ⟨(tf t ht).st, (tf t ht).blk, tl t ht⟩) + · rw [Notif.epHeadA_eq]; exact nw n hn _ (by simp) (by simp) (Or.inr (by simp)) + · rw [Notif.epTSA_eq]; exact nw n hn _ (by simp) (by simp) (Or.inr (by simp)) + · rw [Notif.nBadgeA_eq]; exact nw n hn _ (by simp) (by simp) (Or.inr (by simp)) + +/-- Replacing an object that is no endpoint, reply or TCB keeps the endpoint relation's spec view. -/ +theorem endpKView_updA {σ : State Unit} {q : ObjRef} {o o' : KernelObject} (hq : σ.kheap q = some o) + (h1 : ∀ x, o ≠ .endpoint x) (h2 : ∀ x, o ≠ .reply x) (h3 : ∀ x, o ≠ .tcb x) : + Endp.KView σ (updKSc σ q o') := by + have keep : ∀ p k, σ.kheap p = some k → k ≠ o → (updKSc σ q o').kheap p = some k := fun p k hp ne => by + have : p ≠ q := fun e => ne (Option.some.inj (hp.symm.trans (e ▸ hq))) + rw [updKSc_kheap, Function.update_of_ne this]; exact hp + exact ⟨fun p x h => keep p _ h (fun e => h1 x e.symm), fun p x h => ⟨x, keep p _ h (fun e => h2 x e.symm), rfl⟩, + fun p x h => ⟨x, keep p _ h (fun e => h3 x e.symm), rfl, rfl, rfl, rfl, rfl⟩, rfl⟩ + +/-- Replacing an object that is no notification or TCB keeps the notification relation's spec view. -/ +theorem ntfnKView_updA {σ : State Unit} {q : ObjRef} {o o' : KernelObject} (hq : σ.kheap q = some o) + (h1 : ∀ x, o ≠ .notification x) (h3 : ∀ x, o ≠ .tcb x) : + Notif.KView σ (updKSc σ q o') := by + have keep : ∀ p k, σ.kheap p = some k → k ≠ o → (updKSc σ q o').kheap p = some k := fun p k hp ne => by + have : p ≠ q := fun e => ne (Option.some.inj (hp.symm.trans (e ▸ hq))) + rw [updKSc_kheap, Function.update_of_ne this]; exact hp + exact ⟨fun p x h => keep p _ h (fun e => h1 x e.symm), + fun p x h => ⟨x, keep p _ h (fun e => h3 x e.symm), rfl, rfl, rfl⟩⟩ + +instance (t n : ObjRef) : SPresInst (removeAndRestartNtfnQueuedThread (Z := Unit) t n) := + ⟨by unfold removeAndRestartNtfnQueuedThread; spres⟩ + +private theorem mapMx_presA {α β : Type} (f : α → SMonad Unit β) (hf : ∀ x, SPresA (f x)) : + ∀ l : List α, SPresA (NonDet.mapMx f l) + | [] => by unfold NonDet.mapMx; exact PresR.pure _ + | x :: l => by + unfold NonDet.mapMx + exact PresR.bind (hf x) fun _ => mapMx_presA f hf l + +/-- **The spec's waiter loop is a scheduler step.** -/ +theorem cancelAllSignals_presA (p : ObjRef) : SPresA (cancelAllSignals (Z := Unit) p) := by + unfold cancelAllSignals + refine PresR.bind SPresInst.pres fun ntfn => ?_ + split + · refine PresR.bind (mapMx_presA _ (fun t => SPresInst.pres) _) fun _ => SPresInst.pres + · exact PresR.pure _ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} [J7PpA st J7] {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- A notification object's word is read by no scheduler relation. -/ +theorem JStage3TA.ntfnWord_offReadA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {p b : U64} (hp : p ∈ N) (b1 : p.val ≤ b.val) (b2 : b.val < p.val + 40) : ¬ SReadA st Z m s σ b := by + have hw : IpcObjWA N E Rp b := Or.inl ⟨p, hp, b1, by omega⟩ + have hks := J.ipc_ksA hw + have hth := J.ipcPlace.notification p hp + have ksoff : ∀ o : U64, o.val < ksSizeA → b ≠ wa (L.ksb s) o := by + intro o ho e + have hfit := J.sr.ksFit + have hv : (wa (L.ksb s) o).val = (L.ksb s).val + o.val := by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + rw [e, hv] at hks + omega + rintro (⟨c, hc, o, ho, e⟩ | e | e | ⟨t, ht, K, hK, e⟩ | h) + · have := J.sr.node_in hc ho + rw [← e] at this + omega + · exact ksoff 8#u64 (by decide) e + · exact ksoff 16#u64 (by decide) e + · have htH : H t = some .tcb := by + have : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + exact this + have k4 : K.val < 4096 := by rcases hK with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> simp + have hfit := (J.sr.core.tcbs t ht).fits + have hv : (wa t K).val = t.val + K.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact J.ipc_off_hobjA (v := t) (kv := .tcb) htH (by simp) + ⟨by rw [e, hv]; omega, by rw [e, hv]; simp only [HKind.bits]; omega⟩ hw + · have := J.sr.scp.th_out h hth + simp only [ObjKindA.size] at this + omega + +/-- A notification's `SC` word is read by no scheduler relation. -/ +theorem JStage3TA.nScSc_offReadA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {p : U64} (hp : p ∈ N) : ¬ SReadA st Z m s σ (nScSc p) := by + have hna := J.iplaced.n.ntfn p hp + have v : (nScSc p).val = p.val + 32 := by + have e : nScSc p = Notif.nScA p := rfl + rw [e, Notif.nScA_eq, Notif.nfield_val hna (by simp)]; simp + exact J.ntfnWord_offReadA hp (by omega) (by omega) + +/-- A TCB word the scheduler relation does not read. -/ +theorem JStage3TA.tcbWord_offReadA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {t K : U64} (ht : t ∈ Z.T) (hK : K.val < 4096) + (h7 : K.val ≠ 3520 ∧ K.val ≠ 3448 ∧ K.val ≠ 3432 ∧ K.val ≠ 3464 ∧ K.val ≠ 3592 ∧ K.val ≠ 3480 ∧ K.val ≠ 3488) : + ¬ SReadA st Z m s σ (wa t K) := by + have hfit := (J.sr.core.tcbs t ht).fits + have hv : (wa t K).val = t.val + K.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have hth := (J.sr.thT t).2 ht + have hks := J.sr.tcb_ks ht + have hsz : ObjKindA.size .tcb = 4096 := rfl + have ksoff : ∀ o : U64, o.val < ksSizeA → wa t K ≠ wa (L.ksb s) o := by + intro o ho e + have hfit := J.sr.ksFit + have hw : (wa (L.ksb s) o).val = (L.ksb s).val + o.val := by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have := congrArg (fun w : U64 => w.val) e + simp only [hv, hw] at this + unfold ksSizeA at hks ho + omega + rintro (⟨c, hc, o, ho, e⟩ | e | e | ⟨u, hu, K', hK', e⟩ | h) + · have hn := J.sr.node_in hc ho + rw [← e, hv] at hn + unfold ksSizeA at hks hn + omega + · exact ksoff 8#u64 (by decide) e + · exact ksoff 16#u64 (by decide) e + · have k4 : K'.val < 4096 := by rcases hK' with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> simp + have ne : K.val ≠ K'.val := by + rcases hK' with rfl | rfl | rfl | rfl | rfl | rfl | rfl + · simpa using h7.1 + · simpa using h7.2.1 + · simpa using h7.2.2.1 + · simpa using h7.2.2.2.1 + · simpa using h7.2.2.2.2.1 + · simpa using h7.2.2.2.2.2.1 + · simpa using h7.2.2.2.2.2.2 + have hfu := (J.sr.core.tcbs u hu).fits + have hv' : (wa u K').val = u.val + K'.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have h' := congrArg (fun w : U64 => w.val) e + simp only [hv, hv'] at h' + by_cases eu : u = t + · subst eu; omega + · have := J.sr.place.apart u t .tcb .tcb ((J.sr.thT u).2 hu) hth eu + omega + · have := J.sr.scp.th_out h hth + omega + +/-- With both links in place, `maybe_unbind_ntfn`'s spec is the SC's unbind. -/ +theorem schedContextMaybeUnbindNtfn_liveA {σ : State Unit} {p q : ObjRef} {x : Notification} + {y : SchedContext} {k : Nat} (hn : σ.kheap p = some (.notification x)) + (hs : σ.kheap q = some (.schedContext y k)) (hns : x.ntfnSc = some q) (hsn : y.scNtfn = some p) : + schedContextMaybeUnbindNtfn (Z := Unit) p σ = schedContextUnbindNtfn (Z := Unit) q σ := by + unfold schedContextMaybeUnbindNtfn schedContextUnbindNtfn + rw [nd_bind_oneSc (getNtfnSc_eqSc hn), nd_bind_oneSc (getScNtfn_eqA hs), hns, hsn] + rfl + +/-- The write set of the SC unlink: the notification's `SC` word and that SC's `NTFN` word. -/ +def ScUnlW (st : machine.Store S) [L : LawfulStore st] (s : S) (p b : U64) : Prop := + b = nScSc p ∨ (L.mem s (nScSc p) ≠ 0#u64 ∧ b = scWA (L.mem s (nScSc p)) 40#u64) + +/-- **The SC unlink at the record** (module docstring). -/ +theorem JStage3TA.ntfnScUnlinkA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {p : U64} (hp : p ∈ N) (hH : H p = some .ntfn) (hp0 : p ≠ 0#u64) + (hc : m.curCore < fl4A64Platform.numNodes) : + schedcontext.maybe_unbind_ntfn st s p ⦃ s' => ∃ σ', + schedContextMaybeUnbindNtfn (Z := Unit) (specPtr p) σ = ({((), σ')}, False) ∧ + SchedSRA st Z m κ s' σ' ∧ + Notif.NtfnStateRel N Z.T (L.mem s') σ' ∧ EpStateRel Z.T E Rp Bf (L.mem s') σ' ∧ + WritesIn st s s' (ScUnlW st s p) ∧ + (∀ b, ScUnlW st s p b → b = nScSc p ∨ MWrA (st := st) Z m σ s b) ∧ + SchedFrameA σ σ' ∧ NtfnSpecKeepA σ σ' ∧ NtfnTcbKeepA σ σ' ∧ + (∀ x, σ.kheap (specPtr p) = some (.notification x) → ∃ x', σ'.kheap (specPtr p) = some (.notification x') ∧ + x'.ntfnObj = x.ntfnObj ∧ x'.ntfnBoundTcb = x.ntfnBoundTcb) ⦄ := by + have hna := J.iplaced.n.ntfn p hp + have eS : nScSc p = Notif.nScA p := rfl + have al : Aligned (nScSc p) := by + rw [eS, Notif.nScA_eq]; exact Notif.nfield_aligned hna (by simp) (by simp) + obtain ⟨xn, hkn, hrel⟩ := J.ntfn.ntfn p hp + have hN : NtfnScEqA m p (L.mem s) σ := notifScWord hp J.ntfn J.sr.inv hc + have hnH : p ∈ H.ntfns := hH + have nsc := J.blkA.2.2.1.2.2.nsc p hnH + have hsc : ∀ sc, L.mem s (nScSc p) = sc → sc ≠ 0#u64 → sc ∈ Z.C ∧ L.mem s (scWA sc 40#u64) = p := by + intro sc e h0 + have e' : L.mem s (ntfnScA p) = sc := e + have := nsc (by rw [e']; exact h0) + rw [e', J.scs] at this + have e40 : scNtfnA sc = scWA sc 40#u64 := by unfold scNtfnA scWA layout.sc.NTFN; rfl + exact ⟨this.1, e40 ▸ this.2⟩ + have sr := irqUnbindNotificationScA J.sr p hp0 hN al (J.nScSc_offReadA hp) hsc + by_cases h0 : L.mem s (nScSc p) = 0#u64 + · have ev : schedcontext.maybe_unbind_ntfn st s p ⦃ s' => s' = s ⦄ := by + unfold schedcontext.maybe_unbind_ntfn + have hword : obj.Ntfn.sc st p s = ok (L.mem s (nScSc p)) := eq_of_spec_fd (Notif.obj.Ntfn.sc.spec s p) + rw [hword, bind_ok, eq_of_spec_fd (P1.obj.Sc.is_null.spec _), bind_ok, decide_eq_true h0, if_pos rfl, + WP.spec_ok] + have hn : xn.ntfnSc = none := by + obtain ⟨z, hz, hzs⟩ := hN + rw [hkn] at hz; cases hz + rw [hzs]; change optScA m (L.mem s (nScSc p)) = none + rw [optScA, if_pos h0] + have sp : schedContextMaybeUnbindNtfn (Z := Unit) (specPtr p) σ = ({((), σ)}, False) := by + unfold schedContextMaybeUnbindNtfn + rw [nd_bind_oneSc (getNtfnSc_eqSc hkn), hn] + rfl + refine WP.spec_mono ev ?_ + rintro s' rfl + refine ⟨σ, sp, J.sr, J.ntfn, J.ep, WritesIn.refl _ _, fun b hb => ?_, ⟨SpecSchedA.refl σ, SSzA.refl σ, rfl⟩, + NtfnSpecKeepA.refl σ, NtfnTcbKeepA.refl σ, fun x hx => ⟨x, hx, rfl, rfl⟩⟩ + rcases hb with e | ⟨e, -⟩ + · exact Or.inl e + · exact absurd h0 e + · obtain ⟨hcC, hback⟩ := hsc _ rfl h0 + obtain ⟨xs, ns, hks, hxs⟩ := J.sr.scr.sc _ hcC + have hxs' : xs.scNtfn = some (specPtr p) := by rw [hxs.ntfn, hback, optRefS_ne hp0] + have hxn : xn.ntfnSc = some (scNameA m (L.mem s (nScSc p))) := by + obtain ⟨z, hz, hzs⟩ := hN + rw [hkn] at hz; cases hz + rw [hzs]; change optScA m (L.mem s (nScSc p)) = _ + rw [optScA, if_neg h0] + rfl + have sp : schedContextMaybeUnbindNtfn (Z := Unit) (specPtr p) σ = + ({((), scNtfnLinkStA σ (scNameA m (L.mem s (nScSc p))) (specPtr p) xs ns xn false)}, False) := by + rw [schedContextMaybeUnbindNtfn_liveA hkn hks hxn hxs'] + exact schedContextUnbindNtfn_someA hks hkn hxs' + have ev : schedcontext.maybe_unbind_ntfn st s p ⦃ s' => + s' = L.wr (L.wr s (ntfnScA p) 0#u64) (scNtfnA (L.mem s (nScSc p))) 0#u64 ⦄ := by + unfold schedcontext.maybe_unbind_ntfn + have hword : obj.Ntfn.sc st p s = ok (L.mem s (nScSc p)) := eq_of_spec_fd (Notif.obj.Ntfn.sc.spec s p) + rw [hword, bind_ok, eq_of_spec_fd (P1.obj.Sc.is_null.spec _), bind_ok, decide_eq_false h0, + if_neg Bool.false_ne_true] + unfold schedcontext.unbind_ntfn + refine WP.spec_bind (obj.Sc.is_null.spec _) ?_ + rintro _ rfl + simp only [decide_eq_false h0, Bool.false_eq_true, ↓reduceIte] + refine WP.spec_bind (obj.Sc.ntfn.valA s _) ?_ + rintro _ rfl + rw [hback, show (p != 0#u64) = true from bne_iff_ne.2 hp0] + simp only [↓reduceIte] + refine WP.spec_bind (obj.Ntfn.set_sc.wr (st := st) s p 0#u64) ?_ + rintro _ rfl + refine WP.spec_mono (obj.Sc.set_ntfn.wr _ _ 0#u64) ?_ + rintro _ rfl + rfl + refine WP.spec_mono (WP.spec_and ev sr) ?_ + rintro s' ⟨es', σ', hsp, hsr, hsq, -⟩ + have eσ : σ' = scNtfnLinkStA σ (scNameA m (L.mem s (nScSc p))) (specPtr p) xs ns xn false := by + have := hsp.symm.trans sp + have h1 : ((), σ') ∈ ({((), scNtfnLinkStA σ (scNameA m (L.mem s (nScSc p))) (specPtr p) xs ns xn false)} : + Set (Unit × State Unit)) := by + rw [← (Prod.mk.inj this).1]; exact rfl + exact (Prod.mk.inj h1).2 + have e40 : scNtfnA (L.mem s (nScSc p)) = scWA (L.mem s (nScSc p)) 40#u64 := by + unfold scNtfnA scWA layout.sc.NTFN; rfl + have h40 := hxs.hdr (K := 40#u64) (by simp) (by simp) + have obj40 : ScObjA (L.mem s (nScSc p)) ns (scWA (L.mem s (nScSc p)) 40#u64) := hxs.in_obj h40.2 + have mw40 : MWrA (st := st) Z m σ s (scWA (L.mem s (nScSc p)) 40#u64) := mwr_scObj hcC hks obj40 + have wr : WritesIn st s s' (ScUnlW st s p) := by + subst es' + refine ⟨by rw [L.ksb_wr, L.ksb_wr], by rw [L.cpu_wr, L.cpu_wr], fun b hb hn => ?_⟩ + have a40 : Aligned (scNtfnA (L.mem s (nScSc p))) := e40 ▸ h40.1 + have aN : Aligned (ntfnScA p) := al + rw [L.mem_wr_ne a40 hb (fun h => hn (Or.inr ⟨h0, (e40 ▸ UScalar.eq_of_val_eq h).symm⟩)), + L.mem_wr_ne aN hb (fun h => hn (Or.inl (UScalar.eq_of_val_eq h).symm))] + have z40 : L.mem s' (nScSc p) = 0#u64 := by + subst es' + have a40 : Aligned (scNtfnA (L.mem s (nScSc p))) := e40 ▸ h40.1 + have ne : (scNtfnA (L.mem s (nScSc p))).val ≠ (nScSc p).val := by + intro h + have e : scWA (L.mem s (nScSc p)) 40#u64 = nScSc p := e40 ▸ UScalar.eq_of_val_eq h + exact J.nScSc_offReadA hp (e ▸ Or.inr (Or.inr (Or.inr (Or.inr ⟨_, hcC, xs, ns, hks, obj40⟩)))) + rw [L.mem_wr_ne a40 al ne] + exact L.mem_wr_same al + have hna' := J.iplaced.n + have offN : ∀ b, Notif.NFoot N Z.T b → b ≠ scWA (L.mem s (nScSc p)) 40#u64 := by + intro b hb e + have f : ScFootA Z.C m σ b := e ▸ ⟨_, hcC, xs, ns, hks, obj40⟩ + rcases hb with ⟨q, hq, h1, h2⟩ | ⟨t, ht, h⟩ + · have := J.sr.scp.th_out f (J.ipcPlace.notification q hq) + simp only [ObjKindA.size] at this + omega + · have hth := (J.sr.thT t).2 ht + have := J.sr.scp.th_out f hth + have sz : ObjKindA.size .tcb = 4096 := rfl + have hta := hna'.tcb t ht + have v : ∀ K : U64, K.val < 4096 → (wa t K).val = t.val + K.val := fun K hK => Notif.tfield_val hta hK + have rng : t.val ≤ b.val ∧ b.val < t.val + 4096 := by + rcases h with h | h | h | h | h | h <;> rw [h] + · rw [stA_eq, v _ (by simp)]; simp + · rw [Notif.tBlockA_eq, v _ (by simp)]; simp + · rw [Notif.badgeRegA_eq, v _ (by simp)]; simp + · rw [Notif.prioA_eq', v _ (by simp)]; simp + · rw [Notif.nextEp_eq, v _ (by simp)]; simp + · rw [Notif.prevEp_eq, v _ (by simp)]; simp + omega + have hq : specPtr p ≠ scNameA m (L.mem s (nScSc p)) := by + intro e; rw [e, hks] at hkn; cases hkn + let x' : Notification := { xn with ntfnSc := none } + let σm := updKSc σ (specPtr p) (.notification x') + have eσ2 : σ' = updKSc σm (scNameA m (L.mem s (nScSc p))) (.schedContext { xs with scNtfn := none } ns) := by + rw [eσ]; rfl + have ksm : σm.kheap (scNameA m (L.mem s (nScSc p))) = some (.schedContext xs ns) := by + show (updKSc σ _ _).kheap _ = _ + rw [updKSc_kheap, Function.update_of_ne hq.symm]; exact hks + have bnd : L.mem s' (Notif.nBoundA p) = L.mem s (Notif.nBoundA p) := by + have f := Notif.foot_bound hna' hp + refine wr.mem_eq f.1 fun h => ?_ + rcases h with e | ⟨-, e⟩ + · have := congrArg (fun w : U64 => w.val) e + simp only [eS, Notif.nBoundA_eq, Notif.nScA_eq] at this + rw [Notif.nfield_val hna (by simp), Notif.nfield_val hna (by simp)] at this + simp at this + · exact offN _ f.2 e + have R1 : Notif.NtfnStateRel N Z.T (L.mem s') σm := by + refine NtfnStateRel.relinkA J.ntfn hna' hp (x' := x') hkn rfl (fun b hb f _ ne => wr.mem_eq hb ?_) + (by rw [bnd]; exact hrel.bound) (by rw [bnd]; exact hrel.bound_mem) ?_ + · rintro (e | ⟨-, e⟩) + · exact ne (e.trans eS) + · exact offN _ f e + · rw [← eS, z40, Notif.optRef_zero] + have R2 : Notif.NtfnStateRel N Z.T (L.mem s') σ' := by + rw [eσ2] + exact R1.frame hna' (fun _ _ _ => rfl) (ntfnKView_updA ksm (fun _ e => by cases e) (fun _ e => by cases e)) + have EP : EpStateRel Z.T E Rp Bf (L.mem s') σ' := by + have k1 : Endp.KView σ σm := endpKView_updA hkn (fun _ e => by cases e) (fun _ e => by cases e) + (fun _ e => by cases e) + have k2 : Endp.KView σm σ' := by + rw [eσ2] + exact endpKView_updA ksm (fun _ e => by cases e) (fun _ e => by cases e) (fun _ e => by cases e) + have k : Endp.KView σ σ' := by + refine ⟨fun q x h => k2.1 q x (k1.1 q x h), fun q x h => ?_, fun q x h => ?_, k2.2.2.2.trans k1.2.2.2⟩ + · obtain ⟨y, hy, e⟩ := k1.2.1 q x h + obtain ⟨z, hz, f⟩ := k2.2.1 q y hy + exact ⟨z, hz, f.trans e⟩ + · obtain ⟨y, hy, e1, e2, e3, e4, e5⟩ := k1.2.2.1 q x h + obtain ⟨z, hz, f1, f2, f3, f4, f5⟩ := k2.2.2.1 q y hy + exact ⟨z, hz, f1.trans e1, f2.trans e2, f3.trans e3, f4.trans e4, f5.trans e5⟩ + refine J.ep.frameX J.iplaced.e (fun b hb f => wr.mem_eq hb ?_) k + rintro (e | ⟨-, e⟩) + · exact nw_not_efoot J.iplaced hp (a := b) + (by rw [e, eS, Notif.nScA_eq, Notif.nfield_val hna (by simp)]; simp) + (by rw [e, eS, Notif.nScA_eq, Notif.nfield_val hna (by simp)]; simp) f.1 + · exact endpFootOffWrite J.sr.toSchedSRN J.ipcPlace f.1 (e ▸ mw40) + have F : SchedFrameA σ σ' := by + have f1 : SchedFrameA σ σm := SchedFrameA.upd_simple hkn (Or.inr (Or.inl ⟨_, _, rfl, rfl⟩)) + (fun _ _ e => by cases e) + have f2 : SchedFrameA σm σ' := by rw [eσ2]; exact SchedFrameA.upd_sc ksm + exact PreO.trans f1 f2 + refine ⟨σ', hsp, hsr, R2, EP, wr, fun b hb => ?_, F, eσ ▸ NtfnSpecKeepA.scNtfnLink hks hkn false, + eσ ▸ NtfnTcbKeepA.scNtfnLink hks hkn false, fun x hx => ?_⟩ + · rcases hb with e | ⟨-, e⟩ + · exact Or.inl e + · exact Or.inr (e ▸ mw40) + · rw [hkn] at hx; cases hx + refine ⟨x', ?_, rfl, rfl⟩ + rw [eσ2, updKSc_kheap, Function.update_of_ne hq] + show (updKSc σ _ _).kheap _ = _ + rw [updKSc_kheap, Function.update_self] + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelPoolKeepA.lean b/proofs/Fl4ProofsA64/P3/DelPoolKeepA.lean new file mode 100644 index 000000000..5d81f5c37 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelPoolKeepA.lean @@ -0,0 +1,272 @@ +/- +P3 (AArch64, delete hooks W2): **the deletion loop's record across a pool-word zero and a new hardware root**, the +`delete_asid` sibling of `P3/DelZeroKeepA`'s `delVs_zeroKeepA`. + +* `ClrViewA σ σ'`: the spec step keeps or clears VSpace entries *as seen by the unmapped caps* (the tables, Stage-2 + roots and windows), and may change anything else (ASID pools): what `UnmPtSpecA` reads of `ClrA`. +* `UnmPtSpecA.hwA`: `UnmPtSpecA` does not depend on the ghost hardware root. +* `delVs_zeroKeepHwA`: `delVs_zeroKeepA` with the hardware-root ghost `hw'` free and `ClrViewA` for `ClrA`. +* `JStage3TA.of_hwA`: the record at a new hardware root, store and spec state unchanged. + +No new premise. New module; no existing statement changed. +-/ +import Fl4ProofsA64.P3.DelZeroKeepA +import Fl4ProofsA64.P3.DelAsidA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-- What `UnmPtSpecA` reads of the spec step (module docstring). -/ +structure ClrViewA (σ σ' : Fl4SpecA64.State Unit) : Prop where + pt : ∀ {p : Fl4SpecA64.ObjRef} {t : Fl4SpecA64.PT}, σ.kheap p = some (.archObj (.pageTable t)) → + ∃ t', σ'.kheap p = some (.archObj (.pageTable t')) ∧ Fl4SpecA64.ptType t' = Fl4SpecA64.ptType t ∧ + ∀ i, Fl4SpecA64.ptApply t' i = Fl4SpecA64.ptApply t i ∨ Fl4SpecA64.ptApply t' i = .invalidPTE + s2 : ∀ {p : Fl4SpecA64.ObjRef} {es}, σ.kheap p = some (.archObj (.ext (.stage2Root es))) → + ∃ es', σ'.kheap p = some (.archObj (.ext (.stage2Root es'))) ∧ ∀ i, es' i = es i ∨ es' i = .invalidPTE + hv : ∀ {p : Fl4SpecA64.ObjRef} {l}, σ.kheap p = some (.archObj (.ext (.highVSpace l))) → + ∃ l', σ'.kheap p = some (.archObj (.ext (.highVSpace l'))) ∧ ∀ i, l' i = l i ∨ l' i = .invalidPTE + data : ∀ {p : Fl4SpecA64.ObjRef} {dev : Bool} {pg : Fl4SpecA64.VMPageSize}, + σ.kheap p = some (.archObj (.dataPage dev pg)) → σ'.kheap p = some (.archObj (.dataPage dev pg)) + +theorem ClrA.view {σ σ' : Fl4SpecA64.State Unit} (h : ClrA σ σ') : ClrViewA σ σ' := + ⟨fun e => h.pt e, fun e => h.s2 e, fun e => h.hv e, fun e => h.dataA _ _ _ e⟩ + +/-- A step that changes the heap only at an ASID pool object (entry updates), whatever else it keeps. -/ +theorem ClrViewA.of_poolA {σ σ' : Fl4SpecA64.State Unit} {q : Fl4SpecA64.ObjRef} {f : Fl4SpecA64.AsidPool} + (hq : σ.kheap q = some (.archObj (.asidPool f))) (hk : ∀ p, p ≠ q → σ'.kheap p = σ.kheap p) : ClrViewA σ σ' := by + have ne : ∀ p (o : Fl4SpecA64.KernelObject), σ.kheap p = some o → (∀ a, o ≠ .archObj (.asidPool a)) → p ≠ q := by + intro p o e hn hp + subst hp + rw [hq] at e + exact hn f (Option.some.inj e).symm + refine ⟨fun {p t} e => ⟨t, ?_, rfl, fun i => Or.inl rfl⟩, fun {p es} e => ⟨es, ?_, fun i => Or.inl rfl⟩, + fun {p l} e => ⟨l, ?_, fun i => Or.inl rfl⟩, fun {p dev pg} e => ?_⟩ + · rw [hk p (ne p _ e (fun a h => by cases h))]; exact e + · rw [hk p (ne p _ e (fun a h => by cases h))]; exact e + · rw [hk p (ne p _ e (fun a h => by cases h))]; exact e + · rw [hk p (ne p _ e (fun a h => by cases h))]; exact e + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + +theorem UnmTabA.hwA {Z : XSetsA} {m : Fl4SpecA64.McState} {s : S} {σ : Fl4SpecA64.State Unit} {ty : Fl4SpecA64.PtType} + {p : U64} {hw : HwRootA} (h : UnmTabA (L := L) st Z m s σ ty p) : + UnmTabA (L := L) st { Z with hw := hw } m s σ ty p := + ⟨h.inv, h.spec, ⟨fun i hi hf => h.off.mem i hi + (FootA.monoA (Z := { { Z with hw := hw } with S1 := ∅ }) (Z' := { Z with S1 := ∅ }) rfl rfl (fun _ h => h) + (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) hf), + fun q hq => h.off.obj q hq⟩⟩ + +theorem UnmPtSpecA.hwA {Z : XSetsA} {m : Fl4SpecA64.McState} {OS : Set U64} {s : S} {σ : Fl4SpecA64.State Unit} + {hw : HwRootA} (h : UnmPtSpecA (L := L) st Z m OS s σ) : UnmPtSpecA (L := L) st { Z with hw := hw } m OS s σ := + ⟨fun u hu p a v c => (h.pt u hu p a v c).hwA, fun u hu p a c => (h.vs u hu p a c).hwA, h.s2, h.hv⟩ + +theorem VsSpecA.refl' (σ : Fl4SpecA64.State Unit) : VsSpecA σ σ := ⟨rfl, fun _ => Or.inl rfl, fun _ _ h => h⟩ + +/-- **The record at a new hardware root**: nothing else changes (module docstring). -/ +theorem JStage3TA.of_hwA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {Rj : Set U64 → JSlotA S} {s : S} {a a' : A} + {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7LA st Rj) s a σ m) + {hw : HwRootA} (har : AR a' hw) (hR : ∀ Pt, Rj Pt Z H OS s σ m → Rj Pt { Z with hw := hw } H OS s σ m) : + JStage3TA st AR { Z with hw := hw } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7LA st Rj) s a' σ m := by + have w : WritesIn st s s (fun _ => False) := WritesIn.refl s _ + have hP : ∀ Pt Ub : Set U64, VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt → + (∀ p, SchedPageA st Z OS s p → p ∈ Pt) → ∀ b, False → VsR (L.ksb s) Pt b := fun _ _ _ _ _ h => h.elim + obtain ⟨Pt, Ub, vr, pn, arm, capc, vc, df, hicap, link, vcks, fu, ual, vcu, R⟩ := J.j7 + exact J.of_vsWrite (hw := hw) (a' := a') w hP (fun _ h => h.elim) (VsSpecA.refl' σ) (fun _ _ _ h => h) (JAtZAX.of_hwA J.j har) J.ent J.vsOk J.bufVs J.sq + J.poolOff J.unmPt.hwA (J.j5_vsWriteA w hP hw σ) ⟨Pt, Ub, vr, pn, arm, capc, vc, df, hicap, link, vcks, fu, ual, vcu, hR Pt R⟩ + + +/-- The VSpace objects of the unmapped caps stay unmapped. -/ +theorem UnmPtSpecA.zeroViewA {Z : XSetsA} {m : Fl4SpecA64.McState} {OS : Set U64} {s : S} {σ σ' : Fl4SpecA64.State Unit} + {x : U64} (h : UnmPtSpecA st Z m OS s σ) (hx : Aligned x) (k : OSKeep st OS s (L.wr s x 0#u64)) + (al : ∀ p, SchedPageA st Z OS s p → ∀ i : U64, i.val < 512 → Aligned (peAF p i)) (HC : ClrViewA σ σ') : + UnmPtSpecA st Z m OS (L.wr s x 0#u64) σ' := by + have dead : ∀ w, Aligned w → dBitA (L.mem s w) 0 = false → dBitA (L.mem (L.wr s x 0#u64) w) 0 = false := by + intro w hw h0 + rw [L.mem_wr _ _ _ _ hx hw] + split + · exact dBitA_zeroA + · exact h0 + have tab : ∀ ty p, SchedPageA st Z OS s p → UnmTabA (L := L) st Z m s σ ty p → + UnmTabA (L := L) st Z m (L.wr s x 0#u64) σ' ty p := by + intro ty p hp U + obtain ⟨t, e, ety, inv⟩ := U.spec + obtain ⟨t', e', ty', en⟩ := HC.pt e + refine ⟨fun i hi => dead _ (al p hp i hi) (U.inv i hi), ⟨t', e', ty'.trans ety, fun i hi => ?_⟩, + ⟨fun i hi h => U.off.mem i hi + (footA_schedA (Z := Z) (Q := Z.Q) (Rl := Z.R) (curOf := Z.curOf) (curScOf := Z.curScOf) + (L.ksb_wr _ _ _) (L.cpu_wr _ _ _) + (FootA.monoA (Z := { Z with S1 := ∅ }) rfl rfl (fun _ h => h) (fun _ h => h) (fun _ h => h) + (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) h)), U.off.obj⟩⟩ + rcases en i.bv with q | q + · rw [q]; exact inv i hi + · exact q + exact + { pt := fun u hu p asid va hc => by + rw [k.capA hu] at hc + exact tab _ p (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inl ⟨_, _, _, hc⟩⟩))))))) (h.pt u hu p asid va hc) + vs := fun u hu p asid hc => by + rw [k.capA hu] at hc + exact tab _ p (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inl ⟨_, _, hc⟩)⟩))))))) (h.vs u hu p asid hc) + s2 := fun u hu p asid va hc => by + rw [k.capA hu] at hc + have U := h.s2 u hu p asid va hc + obtain ⟨es, e, inv⟩ := U.spec + obtain ⟨es', e', en⟩ := HC.s2 e + refine ⟨fun i hi => ?_, es', e', fun i => ?_⟩ + · by_cases h5 : i.val < 512 + · have hp : SchedPageA st Z OS s p := Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inr (Or.inl ⟨_, _, hc⟩))⟩)))))) + exact dead _ (al p hp i h5) (U.inv i hi) + · obtain ⟨j, hj, ej⟩ := peAF_hiA p i (by omega) hi + have hp : SchedPageA st Z OS s (wa p 4096#u64) := Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inr (Or.inr (Or.inl ⟨p, _, _, hc, rfl⟩)))⟩)))))) + have := U.inv i hi + rw [ej] at this ⊢ + exact dead _ (al _ hp j hj) this + · rcases en i with q | q + · rw [q]; exact inv i + · exact q + hv := fun u hu p asid hc => by + rw [k.capA hu] at hc + have U := h.hv u hu p asid hc + obtain ⟨l, e, inv⟩ := U.spec + obtain ⟨l', e', en⟩ := HC.hv e + have hp : SchedPageA st Z OS s (wa p 4096#u64) := Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inr (Or.inr (Or.inr ⟨p, _, hc, rfl⟩)))⟩)))))) + refine ⟨fun i hi => dead _ (al _ hp i hi) (U.inv i hi), l', e', fun i => ?_⟩ + rcases en i with q | q + · rw [q]; exact inv i + · exact q } + + +set_option maxHeartbeats 1000000 in +/-- **The loop record across a table-word zero** (module docstring). -/ +theorem delVs_zeroKeepHwA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {ipa : U64} {hw' : HwRootA} {s : S} {a a' : A} + {σ σ' : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7LA st (J7R2A st ar ipa)) s a σ m) + (D : DStateA st Z.th OS s σ Z.G) (hPA : ParentAdjA st Z.G.chains Z.G.par s) (hZT : ZombieTcbNoVcpuA st OS s) + {p i : U64} (hp : SchedPageA st Z OS s p) (hi : i.val < 512) + (hNA : ¬ AsW (L.ksb s) (peAF p i)) (hbf : ¬ BufW Bf (peAF p i)) + (jx : JAtZAX (L := L) AR { Z with hw := hw' } (L.wr s (peAF p i) 0#u64) a' σ' m) (HS : VsSpecA σ σ') (HC : ClrViewA σ σ') + (hdr : ∀ y ∈ OS, ∀ q mp ad, capA st s y = .HighVSpace q mp ad → + L.mem (L.wr s (peAF p i) 0#u64) (wa q 4088#u64) = L.mem s (wa q 4088#u64)) + (R' : ∀ Pt Ub : Set U64, VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt → + (∀ q, SchedPageA st Z OS s q → q ∈ Pt) → J7R2A st ar ipa Pt Z H OS s σ m → + J7R2A st ar ipa Pt Z H OS (L.wr s (peAF p i) 0#u64) σ' m) : + DStateA st Z.th OS (L.wr s (peAF p i) 0#u64) σ' Z.G ∧ + JStage3TA st AR { Z with hw := hw' } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7LA st (J7R2A st ar ipa)) + (L.wr s (peAF p i) 0#u64) a' σ' m ∧ + ParentAdjA st Z.G.chains Z.G.par (L.wr s (peAF p i) 0#u64) ∧ + ZombieTcbNoVcpuA st OS (L.wr s (peAF p i) 0#u64) ∧ OSKeep st OS s (L.wr s (peAF p i) 0#u64) ∧ + (∀ Rj' : Set U64 → JSlotA S, (∀ Pt Ub : Set U64, VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt → + (∀ q, SchedPageA st Z OS s q → q ∈ Pt) → J7R2A st ar ipa Pt Z H OS s σ m → Rj' Pt { Z with hw := hw' } H OS (L.wr s (peAF p i) 0#u64) σ' m) → + J7LA st Rj' { Z with hw := hw' } H OS (L.wr s (peAF p i) 0#u64) σ' m) := by + obtain ⟨Pt, Ub, vr, pn, arm, capc, vc, df, hicap, link, vcks, fu, ual, vcu, R⟩ := J.j7 + have vsr : VsR (L.ksb s) Pt (peAF p i) := vsW_pageA link hp hi + have hal : Aligned (peAF p i) := vr.al _ vsr + have kb : L.ksb (L.wr s (peAF p i) 0#u64) = L.ksb s := L.ksb_wr _ _ _ + have w : WritesIn st s (L.wr s (peAF p i) 0#u64) (PtW Pt) := by + refine (writesIn_wr hal).mono fun x hx => ?_ + subst hx + refine ⟨p, link p hp, core.num.U64.wrapping_mul i 8#u64, ?_, ?_, rfl⟩ + all_goals rw [wm8_valA hi]; omega + have w1 : WritesIn st s (L.wr s (peAF p i) 0#u64) (fun x => x = peAF p i) := writesIn_wr hal + have hP : ∀ Pt' Ub' : Set U64, VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub' (L.ksb s) Pt' → + (∀ q, SchedPageA st Z OS s q → q ∈ Pt') → ∀ b, b = peAF p i → VsR (L.ksb s) Pt' b := + fun Pt' _ _ link' b hb => hb ▸ vsW_pageA link' hp hi + have om : peAF p i ∈ H.omega ∧ peAF p i ∉ H.frame := ⟨vr.om _ vsr, vr.sg _ vsr⟩ + -- A64 P1's invariants + have osk := OSKeep.of_in J.ent.os w fun x hx b _ h1 h2 hF => vr.os b (Or.inl hF) ⟨x, hx, h1, h2⟩ + have hB := (blk_capw_wr (v := 0#u64) (BXI.blk J.ent.bxi) hal (Or.inr om)).bxi + have vsZ : VsZ st (L.ksb s) (utVsPtA st Pp Tb Lv s) s (L.wr s (peAF p i) 0#u64) := + VsZ.wr0 (fun b hb => J.vsOk.al b hb) hal + have vsT := utVsZero_keepA J.ent.vs kb vsZ + have vsOk' := utVsOk_zeroA J.ent.vs kb vsZ J.vsOk + have ent : EntryInvA3 st H Z.Cs OS U Ac Pp Tb Lv (L.wr s (peAF p i) 0#u64) := + ⟨J.ent.heap, hB, J.ent.os, J.ent.user.ksb kb, vsT.1, by rw [kb]; exact J.ent.ksw⟩ + have sq : SchedQueuesInv st H.tcbs Z.Cs H.omega H (L.wr s (peAF p i) 0#u64) Z.Q Z.R := + inv_of_capw J.sq w1 (fun b hb => Or.inl (Or.inl (by rw [hb]; exact om.1))) + have ht := tab_of_w vr w1 (fun b hb => hb ▸ hNA) + have poolOff : PoolOff st H Z.Cs OS (L.wr s (peAF p i) 0#u64) := PoolOff.ofTab kb ht J.poolOff + -- the unmapped objects, J5, J7 + have al : ∀ q, SchedPageA st Z OS s q → ∀ j : U64, j.val < 512 → Aligned (peAF q j) := + fun q hq j hj => vr.al _ (vsW_pageA link hq hj) + have unm : UnmPtSpecA st { Z with hw := hw' } m OS (L.wr s (peAF p i) 0#u64) σ' := (J.unmPt.zeroViewA hal osk al HC).hwA (hw := hw') + have j5 := J.j5_vsWriteA w1 hP hw' σ' + have capE : ∀ u ∈ OS, capA st (L.wr s (peAF p i) 0#u64) u = capA st s u := fun u hu => osk.capA hu + have mk : ∀ Rj' : Set U64 → JSlotA S, (∀ Pt Ub : Set U64, VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt → + (∀ q, SchedPageA st Z OS s q → q ∈ Pt) → J7R2A st ar ipa Pt Z H OS s σ m → Rj' Pt { Z with hw := hw' } H OS (L.wr s (peAF p i) 0#u64) σ' m) → + J7LA st Rj' { Z with hw := hw' } H OS (L.wr s (peAF p i) 0#u64) σ' m := by + intro Rj' h + refine ⟨Pt, Ub, by rw [kb]; exact vr, PoolNamed.ofTab kb ht osk pn, ?_, ⟨UbFr.ofOSK osk capc.1, FrameOK.ofOSK osk capc.2⟩, + CapInv.keepO vc osk, CapInv.keepO df osk, HiCap511.keep hicap capE hdr, fun q hq => link q ?_, + by rw [kb]; exact vcks, FrUb.keepO fu osk, ual, vcu, h Pt Ub vr link R⟩ + · refine ⟨⟨arm.case.slot, arm.case.ts, PoolOff.ofTab kb ht arm.case.po, + PoolObj.of_tab kb (fun j => Or.inl (by have e := ht j; rwa [kb] at e)) arm.case.pobj⟩, + KsBridge.ofKsb kb arm.kb, arm.nt, UbPoolT.keep (w1.mono fun x hx h => hNA (hx ▸ h)) + (fun j => vr.al _ (Or.inr (asW_at _ j))) arm.pool, arm.vh⟩ + · unfold SchedPageA at hq ⊢ + refine hq.imp_right (·.imp_right (·.imp_right (·.imp_right (·.imp_right (·.imp_right (·.imp_right ?_)))))) + rintro ⟨u, hu, h⟩ + refine ⟨u, hu, ?_⟩ + rw [capE u hu] at h + exact h + have j7 := mk (J7R2A st ar ipa) R' + have J' := J.of_vsWrite (hw := hw') (a' := a') (σ' := σ') w1 hP (fun b hb => hb ▸ hbf) HS (fun p dev pg h => HC.data h) jx ent vsOk' (bufVs_subA J.bufVs kb vsT.2) sq poolOff + unm j5 j7 + -- the deletion's related states + have tree := D.tree.keepO osk + have hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st (L.wr s (peAF p i) 0#u64) y = .Null := + fun y hy hn => (capE y hy).trans (D.nul y hy hn) + have site : DelSiteA st Z.th OS (L.wr s (peAF p i) 0#u64) σ' Z.G.chains := + { cov := ⟨fun q r cs hk hw => D.cov.cnode q r cs (HS.back hk (fun _ e => by cases e)) hw, + fun q x hk => D.cov.tcb q x (HS.back hk (fun _ e => by cases e))⟩ + fr := fun y hy z hz h1 h2 => by + rw [capE y hy] at h1; rw [capE z hz] at h2; rw [capE y hy, capE z hz] + exact D.fr y hy z hz h1 h2 + irq := Fl4ProofsA64.P3.IrqRunA.keepIrq D.irq fun y hy _ => by rw [capE y ((D.lk.members y).1 hy).1] + zw := fun y hy q zt k hc => by + rw [capE y (D.sub y hy)] at hc + exact D.zw y hy q zt k hc } + have D' : DStateA st Z.th OS (L.wr s (peAF p i) 0#u64) σ' Z.G := + DStateA.ofTA (Z := { Z with hw := hw' }) J'.toJEntTA (D.mdbS.keep osk) (ZombiesFinal.recap D.zf fun y hy => Or.inl (capE y hy)) tree D.sub hnul site + have hZT' : ZombieTcbNoVcpuA st OS (L.wr s (peAF p i) 0#u64) := by + intro u hu q k hc + rw [capE u hu] at hc + have old := hZT u hu q k hc + have hs : IsSlotA Z.th u := by + by_contra hn + rw [D.nul u hu hn] at hc; cases hc + have htcb : Z.th q = some .tcb := by + rcases D.zw u hs _ _ _ hc with ⟨-, h, -⟩ | ⟨h, -⟩ + · exact h + · exact absurd h (by simp [capability.ZOMBIE_TCB]) + have hT : q ∈ Z.T := (J.j.1.thT q).1 htcb + have hta := J.sr.core.tcbs q hT + have hv : (wa q 3664#u64).val = q.val + 3664 := field_val hta (K := 3664#u64) (by simp) + have hH : q ∈ H.tcbs := by rw [J.tcbs]; exact hT + obtain ⟨Qb, Rb, qq⟩ := J.ent.bxi.1 + have off := (qq.omega _ om.1).2.1 q hH + rw [L.mem_wr_ne hal (field_aligned (K := 3664#u64) hta (by simp) (by simp)) (by omega)] + exact old + exact ⟨D', J', ParentAdjA.keepOS D.lk osk hPA, hZT', osk, mk⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelPoolR2A.lean b/proofs/Fl4ProofsA64/P3/DelPoolR2A.lean new file mode 100644 index 000000000..514b93282 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelPoolR2A.lean @@ -0,0 +1,166 @@ +/- +P3 (AArch64, delete hooks W2): **A64 P1's `S1aFin.pool` as a state-level statement**: R2 over the slots other than the +finalised pool cap `x`, at the state after `delete_asid_pool` cleared the cap's ASID-table entry. The proof is +`S1aFin.pool`'s final branch (`P1/S1aR2FinPool`), copied (generated by `handoff/tools/gen_poolr2.py`): the call-level +theorem hides the state. Named premises: none (the finality `final = ok true` and `MdbS` are P1's). New module. +-/ +import Fl4ProofsA64.P1.S1aR2FinPool +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P1.W7.R.ST +open fl4_core_a64 Fl4ProofsA64.P0.Consts Fl4Proofs.P3 + +local notation "wa" => core.num.U64.wrapping_add + +variable {S : Type _} {st : machine.Store S} [L : LawfulStore st] {A : Type _} {ar : machine.Arch A} + {H : Heap} {OS Pt Pp Tb Hr Te Kt Vc Ub T Ω Sg : Set U64} {Lv Re bits : U64 → Nat} {n x p base : U64} {s : S} + +theorem S1aR2.poolClearedA (hO : OSOK T Ω Sg OS) + (vr : VrOK H.omega H.frame H.scs H.scBits OS Vc Ub (L.ksb s) Pt) (heap : HeapOK H) + (place : S1aR2Place Pt Pp Tb Hr Te Lv bits (L.ksb s)) (high : HvRt st OS s ⊆ Hr) + (M : S1aDelM st H OS Pt Pp Tb Te Kt Lv Re bits n s) (mdb : MdbS st OS s) (hx : x ∈ OS) + (cap : capA st s x = .AsidPool p base) + (final : cdt.is_final_capability_with_cap st s x (.AsidPool p base) = ok true) {i : U64} + (small : i.val < 128) (low : base.val = i.val * 512) (nz : p ≠ 0#u64) + (word : L.mem s (wa (L.ksb s) (AtOff i)) = p) : + S1aDelPn st H OS Pt Pp Tb Te Kt Lv Re bits n (L.wr s (wa (L.ksb s) (AtOff i)) 0#u64) x := by + have R : S1aR2 st H OS Pt Pp Tb (HvRt st OS s) Te Kt Lv Re bits n s := M + set w := wa (L.ksb s) (AtOff i) with hw + have table : VsR (L.ksb s) Pt w := Or.inr (asW_at _ i) + have aligned : Aligned w := vr.al w table + have ksb : L.ksb (L.wr s w 0#u64) = L.ksb s := LawfulStore.ksb_wr _ _ _ + have words (b : U64) (hb : Aligned b) : L.mem (L.wr s w 0#u64) b = if b = w then 0#u64 else L.mem s b := by + split + · rename_i e + rw [e] + exact L.mem_wr_same aligned + · rename_i ne + exact L.mem_wr_ne aligned hb (fun e => ne (UScalar.eq_of_val_eq e.symm)) + have mem : VsZ st (L.ksb s) Pt s (L.wr s w 0#u64) := fun b hb => by + rw [words b (vr.al b hb)] + split + · exact Or.inr rfl + · exact Or.inl rfl + have tab : TabKeepOrZero st s (L.wr s w 0#u64) := ⟨ksb, fun j => by + rw [ksb] + exact mem _ (Or.inr (asW_at _ j))⟩ + have caps (y : U64) (hy : y ∈ OS) : capA st (L.wr s w 0#u64) y = capA st s y := + capA_wr_off hO hy aligned (fun h => vr.os _ table ⟨y, hy, h⟩) + have headers : HrW st (HvRt st OS s) s (L.wr s w 0#u64) := fun R' hR => by + have live : VsR (L.ksb s) Pt (wa R' 4088#u64) := + Or.inl ⟨R', R.place.tables (R.place.high.sub hR), 4088#u64, by decide, by decide, rfl⟩ + rw [words _ (vr.al _ live), ite_eq_right] + intro e + exact R.place.high.asw R' hR (e ▸ asW_at (L.ksb s) i) + have here : S1aR2Place Pt Pp Tb (HvRt st OS s) Te Lv bits (L.ksb s) := place.monoHigh high + have sub : OS \ {x} ⊆ OS := fun _ h => h.1 + have same (y : U64) (hy : y ∈ OS \ {x}) := caps y hy.1 + have unmapped := R.eptRoots.unmZ R.core heap R.pageTables + have full := VsLk.activeZero R.core here tab mem caps unmapped + have notPool (y : U64) (c : capability.Cap) (eq : capA st s y = c) (kind : ∀ q b, c ≠ .AsidPool q b) : y ≠ x := + fun e => kind p base (eq.symm.trans (e ▸ cap)) + have keepWitness {y : U64} (hy : y ∈ OS) {c : capability.Cap} (eq : capA st (L.wr s w 0#u64) y = c) + (kind : ∀ q b, c ≠ .AsidPool q b) : y ∈ OS \ {x} := + ⟨hy, notPool y c ((caps y hy).symm.trans eq) kind⟩ + have mapped {y : U64} (hy : y ∈ OS) {t : U64} (m : MapV (capA st (L.wr s w 0#u64) y) t) : y ∈ OS \ {x} := + keepWitness hy rfl fun q b e => by + rw [e] at m + rcases m with ⟨_, _, e'⟩ | ⟨_, e'⟩ | ⟨_, e'⟩ | ⟨_, _, e', _⟩ | ⟨_, _, _, e'⟩ <;> cases e' + have lookup : VsLk st H (OS \ {x}) Pp Tb Te Kt Lv Re bits (L.wr s w 0#u64) := by + refine ⟨fun t reach member outside => ?_, fun t reach member => ?_, fun t kernel y hy m => ?_, + fun u hu y hy q size unm m => ?_, fun u hu q size unm => ?_, ⟨fun u hu q size unm => ?_, + fun t reach member outside => ?_, full.vh.2.2⟩⟩ + · obtain ⟨y, hy, m⟩ := full.bk1 t reach member outside + exact ⟨y, mapped hy m, m⟩ + · obtain ⟨y, hy, m⟩ := full.bk2 t reach member + exact ⟨y, mapped hy m, m⟩ + · exact full.kt t kernel y hy.1 m + · exact full.ex u hu.1 y hy.1 q size unm m + · exact full.zr u hu.1 q size unm + · exact full.vh.1 u hu.1 q size unm + · rcases full.vh.2.1 t reach member outside with plain | ⟨R', obj, which, y, hy, a'', eq⟩ + · exact Or.inl plain + · exact Or.inr ⟨R', obj, which, y, keepWitness hy eq (fun _ _ e => by cases e), a'', eq⟩ + have rootFull := R.core.root.activeZero here.pools tab mem caps + have root : RootAgreement st (OS \ {x}) Pp (L.wr s w 0#u64) := by + refine ⟨fun asid raw entry => ?_, fun u hu y hy r' a1 a2 t1 t2 c1 c2 => + rootFull.agree u hu.1 y hy.1 r' a1 a2 t1 t2 c1 c2⟩ + obtain ⟨y, hy, backed⟩ := rootFull.backed asid raw entry + refine ⟨y, keepWitness hy rfl fun q b e => ?_, backed⟩ + rw [e] at backed + rcases backed with ⟨_, e'⟩ | ⟨_, _, e'⟩ | ⟨_, e'⟩ <;> cases e' + have stage1 := R.core.stage1.activeZero here.pools here.tables tab mem + have highSame : HvRt st (OS \ {x}) (L.wr s w 0#u64) = HvRt st OS s := by + ext q + constructor + · rintro ⟨y, hy, m, a'', eq⟩ + exact ⟨y, hy.1, m, a'', (same y hy).symm.trans eq⟩ + · rintro ⟨y, hy, m, a'', eq⟩ + have ne : y ≠ x := notPool y _ eq (fun _ _ e => by cases e) + exact ⟨y, ⟨hy, ne⟩, m, a'', (caps y hy).trans eq⟩ + have stage2 : S2TyR st n (PoolSet st Pp (L.wr s w 0#u64)) (HvRt st (OS \ {x}) (L.wr s w 0#u64)) Te Re bits + (L.wr s w 0#u64) := by + rw [highSame] + exact R.core.stage2.activeZero here.pools here.granules here.stage2 tab mem headers + have poolWord (q : U64) (hq : q ∈ PoolSet st Pp s) (o : U64) (ho : o.val < 4096) (al : o.val % 8 = 0) : + L.mem (L.wr s w 0#u64) (wa q o) = L.mem s (wa q o) ∨ L.mem (L.wr s w 0#u64) (wa q o) = 0#u64 := + mem _ (Or.inl ⟨q, here.pools hq.1, o, ho, al, rfl⟩) + have entry (j : U64) {q : U64} (read : L.mem (L.wr s w 0#u64) (wa (L.ksb s) (AtOff j)) = q) (live : q ≠ 0#u64) : + L.mem s (wa (L.ksb s) (AtOff j)) = q ∧ wa (L.ksb s) (AtOff j) ≠ w := by + rw [words _ (vr.al _ (Or.inr (asW_at _ j)))] at read + split at read + · exact (live read.symm).elim + · rename_i ne + exact ⟨read, ne⟩ + have pools : S1aPoolCaps st H (OS \ {x}) Pt Pp (L.wr s w 0#u64) := by + refine ⟨fun j hj q read live => ?_, fun y hy q b eq => ?_, fun j k q hj hk live rj rk => ?_, + fun raw active => R.poolCaps.noZero raw (active.back tab poolWord)⟩ + · rw [ksb] at read + obtain ⟨old, away⟩ := entry j read live + obtain ⟨active, member, obj, y, hy, b, eq, bl⟩ := R.poolCaps.present j hj q old live + have ne : y ≠ x := fun e => by + rw [e, cap] at eq + injection eq with _ bases + have : j = i := UScalar.eq_of_val_eq (by have := congrArg UScalar.val bases; omega) + exact away (by rw [this]) + exact ⟨active, member, obj, y, ⟨hy, ne⟩, b, (caps y hy).trans eq, bl⟩ + · have prior := (same y hy).symm.trans eq + obtain ⟨j, hj, bl, live, old⟩ := R.poolCaps.cap y hy.1 q b prior + refine ⟨j, hj, bl, live, ?_⟩ + rw [ksb, words _ (vr.al _ (Or.inr (asW_at _ j))), ite_eq_right, old] + intro same' + have other := is_final.pool_only hO mdb hx cap final y hy.1 hy.2 q b prior + have hit : L.mem s (wa (L.ksb s) (AtOff j)) = p := by rw [same']; exact word + exact other (old.symm.trans hit) + · rw [ksb] at rj rk + exact R.poolCaps.index j k q hj hk live (entry j rj live).1 (entry k rk live).1 + have rootBack : ∀ asid raw, ActiveAsidRoot st Pp (L.wr s w 0#u64) asid raw → ActiveAsidRoot st Pp s asid raw := + fun _ _ e => e.back tab poolWord + have pathBack : ∀ tag asid sh va t, S1aPath st Pp n (L.wr s w 0#u64) tag asid sh va t → + S1aPath st Pp n s tag asid sh va t := fun _ _ _ _ _ path => by + refine path.back rootBack (fun asid raw rootEntry kind => ?_) + (fun _ _ _ _ _ old _ bd al => mem _ (Or.inl (R.pathWord old bd al))) + obtain ⟨y, hy, backed⟩ := R.core.root.backed asid raw rootEntry + rw [kind] at backed + rcases backed with ⟨bad, _⟩ | ⟨bad, _⟩ | ⟨_, backed⟩ + · cases bad + · cases bad + exact headers _ ⟨y, hy, true, asid, backed⟩ + have full' : S1aPathBacking st H OS Pp Tb Te Lv Re bits n (L.wr s w 0#u64) := + R.paths.shrink pathBack + (fun tag asid sh va t path _ bd al => mem _ (Or.inl (R.pathWord (pathBack tag asid sh va t path) bd al))) + (fun _ _ _ _ _ _ _ y hy _ eq => ⟨y, hy, (caps y hy).trans eq⟩) + (fun _ _ _ _ _ _ _ _ y hy _ _ _ _ _ => ⟨y, hy, caps y hy⟩) + have paths : S1aPathBacking st H (OS \ {x}) Pp Tb Te Lv Re bits n (L.wr s w 0#u64) := by + refine ⟨full'.tables, fun tag asid sh va t path child => ?_, + fun tag asid sh va t path o bound al range valid => ?_⟩ + · obtain ⟨y, hy, address, eq, ref⟩ := full'.children tag asid sh va t path child + exact ⟨y, keepWitness hy eq (fun _ _ e => by cases e), address, eq, ref⟩ + · obtain ⟨y, hy, sz, rights, dev, address, size, eq, ref, object⟩ := + full'.leaves tag asid sh va t path o bound al range valid + exact ⟨y, keepWitness hy eq (fun _ _ e => by cases e), sz, rights, dev, address, size, eq, ref, object⟩ + have done := R.zeroedOn heap (by obtain ⟨_, _, _, hK, _, _⟩ := mdb; exact hK) tab mem headers sub same lookup + root stage1 stage2 pools paths + exact done + +end Fl4ProofsA64.P1.W7.R.ST diff --git a/proofs/Fl4ProofsA64/P3/DelPoolRecA.lean b/proofs/Fl4ProofsA64/P3/DelPoolRecA.lean new file mode 100644 index 000000000..93e266966 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelPoolRecA.lean @@ -0,0 +1,90 @@ +/- +P3 (AArch64, delete hooks W2, Architect ruling E2′ / Option P): **`delete_asid_pool` at the loop record**, and the +window invariant's relaxed J7 slot. + +* `J7R2tA`: the R2 part of J7 relaxed to `True` (the window invariant `Wx`'s only difference from `W`); +* `J7R2xA`: R2 over the slots other than a given slot `x` (A64 P1's `S1aDelPn`), carried by `Rd(x)`; +* `JStage3TA.mapJ7A`: change the R2 part of the record's J7 slot; +* `VsSpecA.of_dropVmA`, `ClrViewA.of_kheapA`: the spec's `delete_asid_pool` outcome (an ASID-table entry removed, up + to `VmA`) is a VSpace-word step; +* **`delete_asid_pool_recA`**: `vspace.delete_asid_pool` at `JStage3TA` with the relaxed J7 slot, `DStateA`, + `ParentAdjA`, `ZombieTcbNoVcpuA`, `Std6KeepA`, and J7 for any R2 slot the caller can reach (A64 P1's `S1aR2.poolClearedA`). + +Named premises as `P3/FinHookRootA`'s. New module. +-/ +import Fl4ProofsA64.P3.DelTabKeepA +import Fl4ProofsA64.P3.DelRdXA +import Fl4ProofsA64.P3.DelRootA +import Fl4ProofsA64.P3.DelPoolR2A +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-- The spec step of `delete_asid_pool`'s hit (an ASID-table entry removed, up to `VmA`) is a VSpace-word step. -/ +theorem VsSpecA.of_dropVmA {σ σ' : State Unit} {hi : BitVec asidHighBits} (hvm : VmA (dropPoolA σ hi) σ') : + VsSpecA σ σ' := by + obtain ⟨mr, am, vt, nv, rfl⟩ := hvm + refine ⟨rfl, fun _ => Or.inl rfl, fun i p h => ?_⟩ + by_cases e : i = hi + · subst e + simp [dropPoolA] at h + · simpa [dropPoolA, Function.update_of_ne e] using h + +theorem ClrViewA.of_kheapA {σ σ' : State Unit} (h : σ'.kheap = σ.kheap) : ClrViewA σ σ' := + ⟨fun {p t} e => ⟨t, by rw [h]; exact e, rfl, fun _ => Or.inl rfl⟩, + fun {p es} e => ⟨es, by rw [h]; exact e, fun _ => Or.inl rfl⟩, + fun {p l} e => ⟨l, by rw [h]; exact e, fun _ => Or.inl rfl⟩, fun {p dev pg} e => by rw [h]; exact e⟩ + +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Pp Tb : Set U64} {Lv : U64 → Nat} {ia ie : Nat} + {N E Rp : Set U64} {κ : U64 → U64} {n : Nat} {Ac : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} + +/-- **`vspace.delete_asid_pool` at the loop record** (module docstring). -/ +theorem delete_asid_pool_recA {ar : machine.Arch A} (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (K : ArchKeepsA ar AR) {Clock : A → Fl4SpecA64.MachineState → Prop} (Da : DelArchA st ar AR Clock) {s : S} {a : A} {σ : State Unit} {m : McState} + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7LA st (J7R2A st ar hPl.ipa)) s a σ m) + (D : DStateA st Z.th OS s σ Z.G) (hPA : ParentAdjA st Z.G.chains Z.G.par s) (hZT : ZombieTcbNoVcpuA st OS s) + (hT : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s)) + (hYAs : ∀ y ∈ Z.Y, ∀ j : U64, j.val < 512 → ¬ AsW (L.ksb s) (peAF y j)) + {base pool : U64} (h16 : base.val < 2 ^ 16) (hb0 : base.val % 512 = 0) (hpool0 : pool ≠ 0#u64) + (hin : ∀ i : U64, i.val < 128 → base.val = i.val * 512 → L.mem s (atAF (L.ksb s) i) = pool) : + vspace.delete_asid_pool st ar s a base pool ⦃ r => ∃ σ' hw, + ((), σ') ∈ (deleteAsidPool (Z := Unit) (asidOfA base) (specPtr pool) σ).1 ∧ + DStateA st Z.th OS r.1 σ' Z.G ∧ (∀ y ∈ OS, capA st r.1 y = capA st s y) ∧ + JStage3TA st AR { Z with hw := hw } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7LA st J7R2tA) r.1 r.2 σ' m ∧ + ParentAdjA st Z.G.chains Z.G.par r.1 ∧ ZombieTcbNoVcpuA st OS r.1 ∧ + L.ksb r.1 = L.ksb s ∧ L.cpu r.1 = L.cpu s ∧ + (∀ Rj' : Set U64 → JSlotA S, (∀ Pt Ub : Set U64, VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt → + (∀ q, SchedPageA st Z OS s q → q ∈ Pt) → J7R2A st ar hPl.ipa Pt Z H OS s σ m → + Rj' Pt { Z with hw := hw } H OS r.1 σ' m) → J7LA st Rj' { Z with hw := hw } H OS r.1 σ' m) ∧ + Std6KeepA st Z H OS Bf s σ r.1 σ' ∧ + (∃ i : U64, i.val < 128 ∧ base.val = i.val * 512 ∧ r.1 = L.wr s (atAF (L.ksb s) i) 0#u64 ∧ + σ'.archState.armAsidTable = Function.update σ.archState.armAsidTable (BitVec.ofNat 7 i.val) none) ⦄ := by + obtain ⟨x, hcur⟩ := J.sinv.curTcb + have Jt : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7LA st J7R2tA) s a σ m := + J.mapJ7A fun _ _ => trivial + refine WP.spec_mono (delete_asid_pool_jA hA K Da J.toJEntTA D.sub hT hYAs h16 hb0 hpool0 hin hcur) ?_ + rintro ⟨s1, a1⟩ ⟨σ', hw', hmem, jx, hform⟩ + dsimp only at jx hform ⊢ + obtain ⟨i, hi, hbi, hP, e, hvm⟩ := hform + subst e + obtain ⟨D', J', hPA', hZT', osk, J7f⟩ := delTab_zeroKeepHwA Jt J.j7 D hPA hZT hi hYAs jx + (VsSpecA.of_dropVmA hvm) (ClrViewA.of_kheapA hvm.kheap) (fun _ _ _ _ _ => trivial) + exact ⟨σ', hw', hmem, D', fun y hy => osk.capA hy, J', hPA', hZT', L.ksb_wr _ _ _, L.cpu_wr _ _ _, + J7f, fun {_} sub h => Std6GA.vsZeroA J h sub (fun _ _ _ _ => vsW_asidA hi) (VsSpecA.of_dropVmA hvm) + (fun _ _ _ h' => (ClrViewA.of_kheapA hvm.kheap).data h'), ⟨i, hi, hbi, rfl, hvm.asidTable⟩⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelPpA.lean b/proofs/Fl4ProofsA64/P3/DelPpA.lean new file mode 100644 index 000000000..00854a23a --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelPpA.lean @@ -0,0 +1,305 @@ +/- +P3 (AArch64, delete hooks, contract §36/§37 item 4): **the deletion loop's preemption point** (`PPHypDA`) from the +shared closure `P3/Stage3PreemptTA`. + +* **`DelPpTimeA`** (§36 ruling; Task 0, Architect 12:42Z): what the loop carries beside `DelXA` for the preemption + point: the spec's current time is at most the last machine time (`ValidMachineTimeA`), the clock relation, the + counter step (`CounterStepA`) and the entry facts at a timestamp (`CurEntryGA`, guarded by an active current SC). + These are the `update_timestamp` facts `JStage3TA.timestampA` reads; the last two were over-quantified premises + (`∀ σ, ValidMachineTimeA σ → …`) before Task 0. **`DelPpTimeA.of_entryA`**: they hold at kernel entry + (`JStage3EntryA`'s `validTime` / `curInv` / `offReady` and `ScInvA.back`; the counter step is the entry's). +* **`DelXA.stepPpA`**: `DStateA` and `DelXA` across a step that gives back the per-step record `JStage3TA` and keeps + every `OS` slot word and the kernel-state base (`ParentAdjA`, `BufHold`, `CtOff` by their keep lemmas; + `ZombieTcbNoVcpuA` from the record). +* **`delPp_hookA`**: `PPHypDA` at `W = Wp = DelJWQA s0 (DelXA6 …) ∧ DelPpTimeA`, through `preemption_point_actA` + (`hSuf` only at an active current SC, §36 ruling). + +Named premises (the shared preemption-platform set with the Untyped line's `utPp_hookA`): +* No [J] `hvp`: no reachable VSpace word, and no ASID-table word, is a preemption-point word + (`JStage3TA.vsr_offPPWA`, from `vsOk` over the narrowed `ent.vs`). +* [P] `P` (`NowAgreeA`), `hC` (Task 0: the raw counter oracle is monotone, so the counter step holds again after + the timestamp's read), `hNow`, `hAr` (the arch relation after the timestamp), `hirq` (`irq_pending` is an outcome + of `getActiveIRQ true` keeping the spec memory, the last machine time, the time-oracle position, the clock and the + arch relation), `hmb` (the minimum budget), `hnd` (one domain). `hAr` / `hirq` are stated at every hardware + root: `DelXA`'s `hw` is existential (the finalisers move it). + +New module. +-/ +import Fl4ProofsA64.P3.Stage3PreemptTA +import Fl4ProofsA64.P3.DelZeroKeepA +import Fl4ProofsA64.P3.DelCtOffA +import Fl4ProofsA64.P3.CnodeDelHookA +import Fl4ProofsA64.P3.DelRec6A + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-- **What the deletion loop carries beside `DelXA` for the preemption point** (module docstring). -/ +def DelPpTimeA {A : Type} (Clock : A → MachineState → Prop) (a : A) (σ : State Unit) : Prop := + ValidMachineTimeA σ ∧ Clock a σ.machineState ∧ CounterStepA σ ∧ CurEntryGA σ + +/-- (Task 0) **`DelPpTimeA` at kernel entry** (module docstring). -/ +theorem DelPpTimeA.of_entryA {S A : Type} {st : machine.Store S} [L : LawfulStore st] {AR : A → HwRootA → Prop} + {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} + {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J5 J7 : JSlotA S} + {s : S} {a : A} {σ : State Unit} {m : McState} {Clock : A → MachineState → Prop} + (J : JStage3EntryA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (hCl : Clock a σ.machineState) (C : CounterStepA σ) : DelPpTimeA Clock a σ := + ⟨J.validTime, hCl, C, CurEntryGA.of_cur J.curInv J.offReady J.sr.inv⟩ + +section +variable {m : Fl4SpecA64.McState} +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {κ : U64 → U64} {n : Nat} {J5 J7 : JSlotA S} + +/-- **`DStateA` and `DelXA` across a step that returns the per-step record and keeps the `OS` slot words** +(module docstring). -/ +theorem DelXA.stepPpA {s s' : S} {a' : A} {σ σ' : State Unit} {G : MdbGhostA} {Q : U64 → U64 → List U64} + {R : U64 → List U64} {hw : HwRootA} {Ac : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} + {m : McState} + (J' : JStage3TA st AR { Z with G := G, Q := Q, R := R, hw := hw } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n + J5 J7 s' a' σ' m) + (D : DStateA st Z.th OS s σ G) (osk : OSKeep st OS s s') (kb : L.ksb s' = L.ksb s) + (hPA : ParentAdjA st G.chains G.par s) (hBH : BufHold st H OS s) (hCt : CtOff st (L.ksb s) OS s) + (hFx : DelCapFxA st Z H s) : + DStateA st Z.th OS s' σ' G ∧ DelXA st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m J5 J7 s' a' σ' G := by + have capE : ∀ y ∈ OS, capA st s' y = capA st s y := fun y hy => osk.capA hy + have hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s' y = .Null := fun y hy hn => (capE y hy).trans (D.nul y hy hn) + have site : DelSiteA st Z.th OS s' σ' G.chains := + { cov := J'.finCov + fr := fun y hy z hz h1 h2 => by + rw [capE y hy] at h1; rw [capE z hz] at h2; rw [capE y hy, capE z hz] + exact D.fr y hy z hz h1 h2 + irq := Fl4ProofsA64.P3.IrqRunA.keepIrq D.irq fun y hy _ => by rw [capE y ((D.lk.members y).1 hy).1] + zw := fun y hy q zt k hc => by + rw [capE y (D.sub y hy)] at hc + exact D.zw y hy q zt k hc } + have D' : DStateA st Z.th OS s' σ' G := + DStateA.ofTA (Z := { Z with G := G, Q := Q, R := R, hw := hw }) J'.toJEntTA (D.mdbS.keep osk) + (ZombiesFinal.recap D.zf fun y hy => Or.inl (capE y hy)) (D.tree.keepO osk) D.sub hnul site + exact ⟨D', ⟨Q, R, hw, Ac, Bf, stack, J'⟩, ParentAdjA.keepOS D.lk osk hPA, J'.zombieNoVcpu, + bufHold_sameA st hBH capE, ctOff_sameA st hCt kb capE, + hFx.keepA st D.sub capE⟩ + +/-- **No reachable VSpace word is a preemption-point word** (`WORK_UNITS`, the current core's `CUR_TIME` and +`CONSUMED`): `VrOK` puts the word in an `other` heap object off the kernel globals, and the node words lie in the +state block. Discharges Untyped's `hvp` from the narrowed `ent.vs`. -/ +theorem JStage3TA.vsr_offPPWA [J7PpA st J7] {Ac : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {s : S} + {a : A} {σ : State Unit} {m : McState} + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) {Pt Ub : Set U64} + (vr : VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt) {b : U64} (h : VsR (L.ksb s) Pt b) : + ¬ PPW st s b := by + have hc0 := J.sq.cur + have nd := J.sq.nodes _ hc0 + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.ent.ksw + have hcpu128 : (L.cpu s).val < 128 := by + obtain ⟨k, -, hk, h⟩ := J.j.1.csN + have := (h _).1 hc0 + omega + obtain ⟨-, p, k, hp, -, hin⟩ := vr.om b h + have offN := J.heap_offNodesPA hp hin.1 hin.2 + have d48 : layout.node.CUR_TIME.val < 4288 := by simp [layout.node.CUR_TIME] + have d40 : layout.node.CONSUMED.val < 4288 := by simp [layout.node.CONSUMED] + have v48 : layout.node.CUR_TIME.val = 48 := by unfold layout.node.CUR_TIME; rfl + have v40 : layout.node.CONSUMED.val = 40 := by unfold layout.node.CONSUMED; rfl + have hnb : L.nodeBase s (L.cpu s) = (L.ksb s).val + 223488 + (L.cpu s).val * 4288 := rfl + have vw : layout.ks.WORK_UNITS.val = 40 := by unfold layout.ks.WORK_UNITS; rfl + rintro (e | e | e) + · exact vr.ks b h _ (by omega) e.symm + · apply offN + rw [e]; unfold LawfulStore.nodeA + rw [nodeAt_val nd d48, v48, hnb] + omega + · apply offN + rw [e]; unfold LawfulStore.nodeA + rw [nodeAt_val nd d40, v40, hnb] + omega + +variable (st) in +/-- **What the loop's preemption point and IRQ cleanup must keep of a further clause `Φ`** of the loop record +(`DelXPA`): a step writing only the state block below the ASID table and keeping the spec kernel heap. -/ +def KsKeepPA (AR : A → HwRootA → Prop) (Z : XSetsA) (H : Heap) (OS U Pp Tb : Set U64) (Lv : U64 → Nat) (ia ie : Nat) + (N E Rp : Set U64) (κ : U64 → U64) (n : Nat) (J7 : JSlotA S) + (Φ : Set (U64 × Fl4SpecA64.Word) → S → State Unit → Prop) : Prop := + ∀ {G : MdbGhostA} {Q : U64 → U64 → List U64} {R : U64 → List U64} {hw : HwRootA} {Ac : Set U64} + {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {m : McState} {s s' : S} {a : A} {σ σ' : State Unit}, + JStage3TA st AR { Z with G := G, Q := Q, R := R, hw := hw } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n + (J5A st) J7 s a σ m → Φ Bf s σ → (∀ y, IsSlotA Z.th y → y ∈ OS) → OSKeep st OS s s' → L.ksb s' = L.ksb s → + (∀ b : U64, Aligned b → ¬ ((L.ksb s).val ≤ b.val ∧ b.val < (L.ksb s).val + 772352) → L.mem s' b = L.mem s b) → + σ'.kheap = σ.kheap → σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory → Φ Bf s' σ' + +/-- `Std6A` is kept by the loop's preemption point and IRQ cleanup. -/ +theorem ksKeep_std6A {R7 : Set U64 → JSlotA S} [J7PpA st (J7LA st R7)] : + KsKeepPA st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n (J7LA st R7) (Std6GA st Ag Z H OS) := + fun J h sub k kb mem kh um => + have h' := Std6GA.ksStepA J (h.ghostA _ _ _ _) sub k kb mem kh um + ⟨h'.utWin, h'.devWin, h'.vcBack, h'.runBack, h'.vcSnap, h'.vcOS, h'.userFr, h'.frTh, h'.bfSpec, h'.vcSym1, h'.vcSym2, h'.heapWin, + h'.coresAll, h'.zwf, h'.frOff, h'.frAg⟩ + +/-- `DelXA.stepPpA` with the further clause at the post-state. -/ +theorem DelXA.stepPpPA {Φ : Set (U64 × Fl4SpecA64.Word) → S → State Unit → Prop} {s s' : S} {a' : A} + {σ σ' : State Unit} {G : MdbGhostA} {Q : U64 → U64 → List U64} + {R : U64 → List U64} {hw : HwRootA} {Ac : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} + {m : McState} + (J' : JStage3TA st AR { Z with G := G, Q := Q, R := R, hw := hw } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n + J5 J7 s' a' σ' m) + (X' : Φ Bf s' σ') + (D : DStateA st Z.th OS s σ G) (osk : OSKeep st OS s s') (kb : L.ksb s' = L.ksb s) + (hPA : ParentAdjA st G.chains G.par s) (hBH : BufHold st H OS s) (hCt : CtOff st (L.ksb s) OS s) + (hFx : DelCapFxA st Z H s) : + DStateA st Z.th OS s' σ' G ∧ DelXPA st Φ AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m J5 J7 s' a' σ' G := by + obtain ⟨D1, X1⟩ := DelXA.stepPpA J' D osk kb hPA hBH hCt hFx + exact ⟨D1, ⟨⟨Q, R, hw, Ac, Bf, stack, J', X'⟩, X1.2⟩⟩ + +/-- **`PPHypDA` for the deletion loop** (module docstring), with `Φ`. -/ +theorem delPp_hookPA [J7PpA st J7] [J7PpKA st J7] {Φ : Set (U64 × Fl4SpecA64.Word) → S → State Unit → Prop} + (hΦ : KsKeepPA st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n J7 Φ) (ar : machine.Arch A) (hnd : numDomains = 1) + {Clock : A → MachineState → Prop} (P : NowAgreeA ar Clock) (s0 : S) + (hC : ∀ i, fl4RawCounterOracle i ≤ fl4RawCounterOracle (i + 1)) + (hNow : ∀ σ : State Unit, fl4ReadRawCounter σ.machineState = getCurrentTime σ.machineState) + (hAr : ∀ (s : S) (a : A) (hw : HwRootA), AR a hw → sched.update_timestamp st ar s a ⦃ r => AR r.2 hw ⦄) + (hirq : ∀ (a : A) (σ : State Unit) (hw : HwRootA), AR a hw → Clock a σ.machineState → + ar.irq_pending a ⦃ r => ∃ o ms, (o, { σ with machineState := ms }) ∈ + (doMachineOp (getActiveIRQ true) σ).1 ∧ (r.1 = true ↔ ¬ o = none) ∧ + ms.underlyingMemory = σ.machineState.underlyingMemory ∧ + ms.lastMachineTime = σ.machineState.lastMachineTime ∧ ms.timeState = σ.machineState.timeState ∧ + Clock r.2 ms ∧ AR r.2 hw ⦄) + (hmb : ∀ a : A, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) : + PPHypDA st ar Z.th OS + (fun s a σ G => DelJWQA st s0 (DelXPA st Φ AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) J7) s a σ G ∧ + DelPpTimeA Clock a σ) + (fun s a σ G => DelJWQA st s0 (DelXPA st Φ AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) J7) s a σ G ∧ + DelPpTimeA Clock a σ) := by + intro s a σ G D W + refine WP.spec_mono (preemption_point_actA (st := st) (ar := ar) OS + (fun s a σ => DStateA st Z.th OS s σ G ∧ + (DelJWQA st s0 (DelXPA st Φ AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) J7) s a σ G ∧ + DelPpTimeA Clock a σ)) hnd ?_ ?_ ?_ ?_ ?_ s a σ ⟨D, W⟩) ?_ + · rintro s a σ v s1 ⟨D, ⟨sc, ⟨Q, R, hw, Ac, Bf, stack, J, X⟩, hPA, -, hBH, hCt, hFx⟩, T⟩ h + have e1 : s1 = L.wr s (wa (L.ksb s) layout.ks.WORK_UNITS) v := by + have g := eq_of_spec_fd (obj.ks_set.spec (st := st) s layout.ks.WORK_UNITS v) + exact okinj (h.symm.trans g) + have hv : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → Aligned b ∧ b ≠ wa (L.ksb s) layout.ks.WORK_UNITS := + fun b hb => ⟨J.vsOk.al b hb, fun e => J.vsr_offPPWA J.vsOk hb (Or.inl e)⟩ + obtain ⟨J1, osk⟩ := J.workUnitsA hv v + rw [← e1] at J1 osk + have kb : L.ksb s1 = L.ksb s := by rw [e1]; exact L.ksb_wr _ _ _ + have cpu : L.cpu s1 = L.cpu s := by rw [e1]; exact L.cpu_wr _ _ _ + have X' : Φ Bf s1 σ := hΦ J X D.sub osk kb (fun b hb off => by + have e : layout.ks.WORK_UNITS.val = 40 := by unfold layout.ks.WORK_UNITS; rfl + have f := J.sr.ksFit + have ksz : ksSizeA = 788736 := rfl + have ev : (wa (L.ksb s) layout.ks.WORK_UNITS).val = (L.ksb s).val + 40 := by + rw [wa_valm, e]; exact Nat.mod_eq_of_lt (by omega) + have al : Aligned (wa (L.ksb s) layout.ks.WORK_UNITS) := by + have k8 : (L.ksb s).val % 8 = 0 := J.sr.toSchedSRN.ksb_al + show _ % 8 = 0 + rw [ev]; omega + rw [e1] + exact L.mem_wr_ne al hb (fun h => off (by rw [← h, ev]; omega))) rfl rfl + obtain ⟨D1, X1⟩ := DelXA.stepPpPA J1 X' D osk kb hPA hBH hCt hFx + exact ⟨⟨D1, ⟨⟨kb.trans sc.1, cpu.trans sc.2⟩, X1⟩, T⟩, fun y hy => osk.capA hy⟩ + · rintro s a σ ⟨D, ⟨sc, ⟨Q, R, hw, Ac, Bf, stack, J, X⟩, hPA, -, hBH, hCt, hFx⟩, V, Cl, C, hE⟩ + have har : AR a hw := J.j.1.ar + refine WP.spec_mono (WP.spec_and + (J.timestampA (fun b hb => ⟨J.vsOk.al b hb, J.vsr_offPPWA J.vsOk hb⟩) P Cl hnd V C (hNow σ) hE + (hAr s a hw har)) + (update_timestamp_wordsA J.sr.toSchedSRN a P Cl)) ?_ + rintro ⟨s', a'⟩ ⟨⟨σ1, h1, J1, k, V1, Cl1, osk, kb, cpu, E1, ems⟩, now, -, -, es⟩ + dsimp only at es + have kh : σ1.kheap = σ.kheap := by + have h2 := h1 + rw [← fl4UpdateTimestamp_eq_l4vA σ hnd (hNow σ)] at h2 + obtain ⟨τ, hτ⟩ : ∃ τ, τ = σ1 := ⟨_, rfl⟩ + rw [fl4UpdateTimestamp_eqA σ] at h2 + have e := congrArg Prod.snd (Set.mem_singleton_iff.1 h2) + dsimp only at e + rw [e]; rfl + have um : σ1.machineState.underlyingMemory = σ.machineState.underlyingMemory := by + have h2 := h1 + rw [← fl4UpdateTimestamp_eq_l4vA σ hnd (hNow σ)] at h2 + rw [fl4UpdateTimestamp_eqA σ] at h2 + have e := congrArg Prod.snd (Set.mem_singleton_iff.1 h2) + dsimp only at e + rw [e]; rfl + have X' : Φ Bf s' σ1 := hΦ J X D.sub osk kb (fun b hb off => by + have hc0 := J.sq.cur + have nd := J.sq.nodes _ hc0 + have d48 : layout.node.CUR_TIME.val < 4288 := by simp [layout.node.CUR_TIME] + have d40 : layout.node.CONSUMED.val < 4288 := by simp [layout.node.CONSUMED] + have hcpu128 : (L.cpu s).val < 128 := by + obtain ⟨k, -, hk, h⟩ := J.j.1.csN + have := (h _).1 hc0 + omega + have hnb : L.nodeBase s (L.cpu s) = (L.ksb s).val + 223488 + (L.cpu s).val * 4288 := rfl + have off' : ∀ d : U64, d.val < 4288 → b ≠ L.nodeA s d := fun d hd h => off (by + have e : (L.nodeA s d).val = L.nodeBase s (L.cpu s) + d.val := nodeAt_val nd hd + rw [h, e, hnb]; omega) + have alT : Aligned (L.nodeA s layout.node.CUR_TIME) := + nodeAt_aligned nd d48 (by unfold layout.node.CUR_TIME; rfl) + have alC : Aligned (L.nodeA s layout.node.CONSUMED) := + nodeAt_aligned nd d40 (by unfold layout.node.CONSUMED; rfl) + rw [es] + exact (L.mem_wr_ne alC hb (fun h => off' _ d40 (UScalar.eq_of_val_eq h).symm)).trans + (L.mem_wr_ne alT hb (fun h => off' _ d48 (UScalar.eq_of_val_eq h).symm))) kh um + obtain ⟨D1, X1⟩ := DelXA.stepPpPA J1 X' D osk kb hPA hBH hCt hFx + have C1 : CounterStepA σ1 := by + unfold CounterStepA rawNextA + rw [ems] + exact hC _ + exact ⟨σ1, h1, ⟨D1, ⟨⟨kb.trans sc.1, cpu.trans sc.2⟩, X1⟩, V1, Cl1, C1, E1⟩, k⟩ + · rintro s a σ ⟨D, ⟨sc, ⟨Q, R, hw, Ac, Bf, stack, J, X⟩, hPA, -, hBH, hCt, hFx⟩, V, Cl, C, hE⟩ + have har : AR a hw := J.j.1.ar + refine WP.spec_mono (hirq a σ hw har Cl) ?_ + rintro r ⟨o, ms, h1, h2, h3, h4, h4t, h5, h6⟩ + have osk : OSKeep st OS s s := fun _ _ => ⟨rfl, rfl, rfl, rfl⟩ + have X' : Φ Bf s { σ with machineState := ms } := hΦ J X D.sub osk rfl (fun _ _ _ => rfl) rfl h3 + obtain ⟨D1, X1⟩ := DelXA.stepPpPA (J.machineA h3 h6) X' D osk rfl hPA hBH hCt hFx + refine ⟨o, _, h1, h2, D1, ⟨sc, X1⟩, ?_, h5, ?_, hE⟩ + · show σ.curTime ≤ ms.lastMachineTime + rw [h4] + exact V + · show ms.lastMachineTime ≤ fl4RawCounterOracle (ms.timeState + 1) + rw [h4, h4t] + exact C + · rintro s a σ ⟨-, ⟨-, ⟨Q, R, hw, Ac, Bf, stack, J, -⟩, -⟩, -⟩ + exact J.scActiveA + · rintro s a σ ⟨-, ⟨-, ⟨Q, R, hw, Ac, Bf, stack, J, -⟩, -⟩, -⟩ hact + exact J.sufficientA (hmb a) hact + · rintro r ⟨k, σ', h | h⟩ + · exact ⟨k, σ', h.2.2.1, Or.inl ⟨h.1, h.2.1, h.2.2.2⟩⟩ + · exact ⟨k, σ', h.2.2.1, Or.inr ⟨h.1, h.2.1, h.2.2.2⟩⟩ + +/-- **`PPHypDA` for the deletion loop** (module docstring). -/ +theorem delPp_hookA {R7 : Set U64 → JSlotA S} [J7PpA st (J7LA st R7)] [J7PpKA st (J7LA st R7)] (ar : machine.Arch A) (hnd : numDomains = 1) + {Clock : A → MachineState → Prop} (P : NowAgreeA ar Clock) (s0 : S) + (hC : ∀ i, fl4RawCounterOracle i ≤ fl4RawCounterOracle (i + 1)) + (hNow : ∀ σ : State Unit, fl4ReadRawCounter σ.machineState = getCurrentTime σ.machineState) + (hAr : ∀ (s : S) (a : A) (hw : HwRootA), AR a hw → sched.update_timestamp st ar s a ⦃ r => AR r.2 hw ⦄) + (hirq : ∀ (a : A) (σ : State Unit) (hw : HwRootA), AR a hw → Clock a σ.machineState → + ar.irq_pending a ⦃ r => ∃ o ms, (o, { σ with machineState := ms }) ∈ + (doMachineOp (getActiveIRQ true) σ).1 ∧ (r.1 = true ↔ ¬ o = none) ∧ + ms.underlyingMemory = σ.machineState.underlyingMemory ∧ + ms.lastMachineTime = σ.machineState.lastMachineTime ∧ ms.timeState = σ.machineState.timeState ∧ + Clock r.2 ms ∧ AR r.2 hw ⦄) + (hmb : ∀ a : A, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) : + PPHypDA st ar Z.th OS + (fun s a σ G => DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st R7)) s a σ G ∧ + DelPpTimeA Clock a σ) + (fun s a σ G => DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st R7)) s a σ G ∧ + DelPpTimeA Clock a σ) := + delPp_hookPA ksKeep_std6A ar hnd P s0 hC hNow hAr hirq hmb + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelPpLiftA.lean b/proofs/Fl4ProofsA64/P3/DelPpLiftA.lean new file mode 100644 index 000000000..2129ae328 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelPpLiftA.lean @@ -0,0 +1,196 @@ +/- +P3 (AArch64, delete hooks, contract §37 item 4 / §39): **`DelPpTimeA` beside the loop's other hypotheses**. + +* **`DelPpTimeA.keepA`**: the time facts across a spec step that keeps the current time and the machine state. +* **`swapHyp_timeA`**, **`zombieHyp_timeA`**, **`emptyHyp_timeA`**, **`rdTriv_timeA`**: the loop's own slot steps + (`capSwapForDelete_msA`, `setCap_msA`, `emptySlot_msA`; the current time from `SpecSlotA`) and `RdTrivHypA` at + `W ∧ DelPpTimeA`. +* **`FinHookKA.timeA`**: a finaliser hook at `W` / `Wx` is one at `W ∧ DelPpTimeA` / `Wx ∧ DelPpTimeA`, given + `FinTimeSpecA` (the spec finaliser keeps the current time and the machine's time fields), a clock frame (the clock + relation reads only the machine's time fields) and the concrete finaliser keeping the clock relation. +* The arch route to the last of these (§39): **`JStage3TA.mapARA`** / **`DelXA.mapARA`** (the record at a weaker arch + relation), **`ArchKeepsA.andA`** (two arch relations kept together). + +No named premise: `FinHookKA.timeA`'s three are hypotheses of the lemma (§39 proposes their discharge). New module. +-/ +import Fl4ProofsA64.P3.DelPpA +import Fl4ProofsA64.P3.DelStepAsmA +import Fl4ProofsA64.P3.DelHookKindA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 + +noncomputable section + +section +variable {m : Fl4SpecA64.McState} +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {Clock : A → MachineState → Prop} + +/-- The time facts across a spec step that keeps the current time and the machine state (Task 0: and `CurEntryGA`'s +facts, `CurKeepA`). -/ +theorem DelPpTimeA.keepA {a : A} {σ σ' : State Unit} (T : DelPpTimeA Clock a σ) (ht : σ'.curTime = σ.curTime) + (hm : σ'.machineState = σ.machineState) (hk : CurKeepA σ σ') : DelPpTimeA Clock a σ' := by + obtain ⟨V, Cl, C, E⟩ := T + refine ⟨?_, ?_, ?_, E.keepA hk (by rw [ht]; exact BitVec.le_refl _)⟩ + · unfold ValidMachineTimeA; rw [ht, hm]; exact V + · rw [hm]; exact Cl + · unfold CounterStepA; rw [hm]; exact C + +/-- (Task 0) **A slot-only spec step keeps `CurEntryGA`'s facts**: SCs are kept, and a TCB's SC binding is kept. -/ +theorem SpecSlotA.curKeepA {σ σ' : State Unit} (H : SpecSlotA σ σ') : CurKeepA σ σ' := by + refine ⟨H.csc, H.cons, fun t h => by rw [← H.rls]; exact h, fun y k h a => ?_, fun t x' h e _ => ?_⟩ + · rcases H.kh σ.curSc with e | ⟨r, cs, cs', -, e2⟩ | ⟨x, x', -, e2, -⟩ + · exact ⟨y, k, e.symm.trans h, a, rfl⟩ + · rw [e2] at h; cases h + · rw [e2] at h; cases h + · rcases H.kh t with ek | ⟨r, cs, cs', -, e2⟩ | ⟨x, x0, e1, e2, en⟩ + · exact ⟨x', ek.symm.trans h, e⟩ + · rw [e2] at h; cases h + · rw [e2] at h + cases h + exact ⟨x, e1, by rw [← (tcbNcA_eq en).2.1]; exact e⟩ + +variable {T : Set U64} {th : TypedHeapA} {OS : Set U64} {W Wx : DelWA S A} + {Rd Rz : S → A → State Unit → MdbGhostA → U64 → Prop} + +/-- **`SwapHypA` at `W ∧ DelPpTimeA`**. -/ +theorem swapHyp_timeA (h : SwapHypA st T th OS W) : + SwapHypA st T th OS (fun s a σ G => W s a σ G ∧ DelPpTimeA Clock a σ) := by + intro s s' a σ σ' G sl p zt n hW h1 D D' w HS hm hA hz hne hcap M + exact ⟨h s s' a σ σ' G sl p zt n hW.1 h1 D D' w HS hm hA hz hne hcap M, + hW.2.keepA HS.time (capSwapForDelete_msA _ _ σ _ hm) HS.curKeepA⟩ + +/-- **`ZombieHypA` at `W ∧ DelPpTimeA`**. -/ +theorem zombieHyp_timeA (h : ZombieHypA st T th OS W Rz) : + ZombieHypA st T th OS (fun s a σ G => W s a σ G ∧ DelPpTimeA Clock a σ) Rz := by + intro s s' a σ σ' G sl p zt n hW hRz hZ hNZ D D' w HS hset hA hne hz hcap M + exact ⟨h s s' a σ σ' G sl p zt n hW.1 hRz hZ hNZ D D' w HS hset hA hne hz hcap M, + hW.2.keepA HS.time (setCap_msA _ _ σ ((), σ') (by rw [hset]; exact Set.mem_singleton _)) HS.curKeepA⟩ + +/-- **`EmptyHypA` at `W ∧ DelPpTimeA` / `Wx ∧ DelPpTimeA`**. -/ +theorem emptyHyp_timeA (h : EmptyHypA st T th OS W Wx Rd) : + EmptyHypA st T th OS (fun s a σ G => W s a σ G ∧ DelPpTimeA Clock a σ) + (fun s a σ G => Wx s a σ G ∧ DelPpTimeA Clock a σ) Rd := by + intro s s' a σ σ' G sl hW hRd D D' w HS hm hA hn h0 hcap M + exact ⟨h s s' a σ σ' G sl hW.1 hRd D D' w HS hm hA hn h0 hcap M, + hW.2.keepA HS.time (emptySlot_msA _ σ _ hm) HS.curKeepA⟩ + +/-- **`RdTrivHypA` at `W ∧ DelPpTimeA`**. -/ +theorem rdTriv_timeA (h : RdTrivHypA st th OS W Rd) : + RdTrivHypA st th OS (fun s a σ G => W s a σ G ∧ DelPpTimeA Clock a σ) Rd := by + intro s a σ G sl c D hW + exact h s a σ G sl c D hW.1 + +/-- **The spec finaliser keeps the current time and the machine's time fields** on the caps of kind `k` (Task 0: and +`CurEntryGA`'s facts, `CurKeepA`). -/ +def FinTimeSpecA (Fn : FinFnA) (k : FinKindA) : Prop := + ∀ (c : capability.Cap) (ac : Cap) (d : Bool) (σ : State Unit) (x : Cap × Cap) (σ' : State Unit), + finKindA c = some k → CapEqA c ac → (x, σ') ∈ (Fn ac d σ).1 → + σ'.curTime = σ.curTime ∧ σ'.machineState.lastMachineTime = σ.machineState.lastMachineTime ∧ + σ'.machineState.timeState = σ.machineState.timeState ∧ CurKeepA σ σ' + +/-- **A finaliser hook keeps `DelPpTimeA`** (module docstring). -/ +theorem FinHookKA.timeA {ar : machine.Arch A} {k : FinKindA} {Fn : FinFnA} + (H : FinHookKA st k ar th OS Fn W Wx Rd Rz) (hS : FinTimeSpecA Fn k) + (hFr : ∀ a ms ms', Clock a ms → ms'.timeState = ms.timeState → ms'.lastMachineTime = ms.lastMachineTime → + Clock a ms') + (hA : ∀ (s : S) (a : A) (σ : State Unit) (G : MdbGhostA) (sl : CslotPtr) (c : capability.Cap) (ac : Cap) + (f : Bool), DStateA st th OS s σ G → W s a σ G → AbsSlotA th sl → capA st s (cslotAddrA sl) = c → + finKindA c = some k → getCap (Z := Unit) sl σ = ({(ac, σ)}, False) → CapEqA c ac → + (NeedsFinCA c → (f = true ↔ isFinalCap' ac σ)) → Clock a σ.machineState → + objects.finalise_cap st ar s a c f false ⦃ r => Clock r.2.2 σ.machineState ⦄) : + FinHookKA st k ar th OS Fn (fun s a σ G => W s a σ G ∧ DelPpTimeA Clock a σ) + (fun s a σ G => Wx s a σ G ∧ DelPpTimeA Clock a σ) Rd Rz := by + intro s a σ G sl c ac f D hW hsl hc hk hg hac hf + refine WP.spec_mono (WP.spec_and (H s a σ G sl c ac f D hW.1 hsl hc hk hg hac hf) + (hA s a σ G sl c ac f D hW.1 hsl hc hk hg hac hf hW.2.2.1)) ?_ + rintro r ⟨⟨h1, h2, h3, σ', G', ⟨d, hd, hmem⟩, D', hc', hoth, ⟨hWn, hWx⟩, hRd, hRz⟩, hCl⟩ + obtain ⟨e1, e2, e3, ek⟩ := hS c ac d σ _ σ' hk hac hmem + have T' : DelPpTimeA Clock r.2.2 σ' := by + refine ⟨?_, hFr _ _ _ hCl e3 e2, ?_, hW.2.2.2.2.keepA ek (by rw [e1]; exact BitVec.le_refl _)⟩ + · unfold ValidMachineTimeA + rw [e1, e2] + exact hW.2.1 + · unfold CounterStepA rawNextA + rw [e2, e3] + exact hW.2.2.2.1 + exact ⟨h1, h2, h3, σ', G', ⟨d, hd, hmem⟩, D', hc', hoth, ⟨fun h => ⟨hWn h, T'⟩, fun h => ⟨hWx h, T'⟩⟩, hRd, hRz⟩ + +/-- (integration round 2) `FinHookKA.timeA` with the time facts of the finaliser given only from the states of `W` +(through `P`): `CurKeepA` is not stateless for the Reply arm (`reply_remove` donates). -/ +theorem FinHookKA.timePA {ar : machine.Arch A} {k : FinKindA} {Fn : FinFnA} + (H : FinHookKA st k ar th OS Fn W Wx Rd Rz) (P : State Unit → Prop) + (hP : ∀ (s : S) (a : A) (σ : State Unit) (G : MdbGhostA), W s a σ G → P σ) + (hS : ∀ (c : capability.Cap) (ac : Cap) (d : Bool) (σ : State Unit) (x : Cap × Cap) (σ' : State Unit), P σ → + finKindA c = some k → CapEqA c ac → (x, σ') ∈ (Fn ac d σ).1 → + σ'.curTime = σ.curTime ∧ σ'.machineState.lastMachineTime = σ.machineState.lastMachineTime ∧ + σ'.machineState.timeState = σ.machineState.timeState ∧ CurKeepA σ σ') + (hFr : ∀ a ms ms', Clock a ms → ms'.timeState = ms.timeState → ms'.lastMachineTime = ms.lastMachineTime → + Clock a ms') + (hA : ∀ (s : S) (a : A) (σ : State Unit) (G : MdbGhostA) (sl : CslotPtr) (c : capability.Cap) (ac : Cap) + (f : Bool), DStateA st th OS s σ G → W s a σ G → AbsSlotA th sl → capA st s (cslotAddrA sl) = c → + finKindA c = some k → getCap (Z := Unit) sl σ = ({(ac, σ)}, False) → CapEqA c ac → + (NeedsFinCA c → (f = true ↔ isFinalCap' ac σ)) → Clock a σ.machineState → + objects.finalise_cap st ar s a c f false ⦃ r => Clock r.2.2 σ.machineState ⦄) : + FinHookKA st k ar th OS Fn (fun s a σ G => W s a σ G ∧ DelPpTimeA Clock a σ) + (fun s a σ G => Wx s a σ G ∧ DelPpTimeA Clock a σ) Rd Rz := by + intro s a σ G sl c ac f D hW hsl hc hk hg hac hf + refine WP.spec_mono (WP.spec_and (H s a σ G sl c ac f D hW.1 hsl hc hk hg hac hf) + (hA s a σ G sl c ac f D hW.1 hsl hc hk hg hac hf hW.2.2.1)) ?_ + rintro r ⟨⟨h1, h2, h3, σ', G', ⟨d, hd, hmem⟩, D', hc', hoth, ⟨hWn, hWx⟩, hRd, hRz⟩, hCl⟩ + obtain ⟨e1, e2, e3, ek⟩ := hS c ac d σ _ σ' (hP s a σ G hW.1) hk hac hmem + have T' : DelPpTimeA Clock r.2.2 σ' := by + refine ⟨?_, hFr _ _ _ hCl e3 e2, ?_, hW.2.2.2.2.keepA ek (by rw [e1]; exact BitVec.le_refl _)⟩ + · unfold ValidMachineTimeA + rw [e1, e2] + exact hW.2.1 + · unfold CounterStepA rawNextA + rw [e2, e3] + exact hW.2.2.2.1 + exact ⟨h1, h2, h3, σ', G', ⟨d, hd, hmem⟩, D', hc', hoth, ⟨fun h => ⟨hWn h, T'⟩, fun h => ⟨hWx h, T'⟩⟩, hRd, hRz⟩ + +/-! ## The record at a weaker arch relation (the arch route, §39) -/ + +variable {AR AR' : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s : S} {a : A} {σ : State Unit} {m : McState} + +/-- **`JStage3TA` at a weaker arch relation**: the arch relation is read only by `XTopAtA.ar`. -/ +theorem JStage3TA.mapARA (f : ∀ a h, AR a h → AR' a h) + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) : + JStage3TA st AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m := + { J with j := ⟨{ J.j.1 with ar := f _ _ J.j.1.ar }, J.j.2⟩ } + +/-- **`DelXA` at a weaker arch relation**. -/ +theorem DelXA.mapARA (f : ∀ a h, AR a h → AR' a h) {G : MdbGhostA} + (X : DelXA st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m J5 J7 s a σ G) : + DelXA st AR' Z H OS U Pp Tb Lv ia ie N E Rp κ n m J5 J7 s a σ G := by + obtain ⟨⟨Q, R, hw, Ac, Bf, stack, J⟩, h⟩ := X + exact ⟨⟨Q, R, hw, Ac, Bf, stack, J.mapARA f⟩, h⟩ + +/-- The arch relation of a `DelXA` state at its (existential) hardware root. -/ +theorem DelXA.arA {G : MdbGhostA} (X : DelXA st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m J5 J7 s a σ G) : + ∃ hw, AR a hw := by + obtain ⟨⟨Q, R, hw, Ac, Bf, stack, J⟩, -⟩ := X + exact ⟨hw, J.j.1.ar⟩ + +end + +/-- **Two arch relations kept together** (`ArchKeepsA` field by field). -/ +theorem ArchKeepsA.andA {A : Type} {ar : machine.Arch A} {AR AR2 : A → HwRootA → Prop} (K : ArchKeepsA ar AR) + (K2 : ArchKeepsA ar AR2) : ArchKeepsA ar (fun a h => AR a h ∧ AR2 a h) where + invPage a h asid va hh := WP.spec_and (K.invPage a h asid va hh.1) (K2.invPage a h asid va hh.2) + invAsid a h asid hh := WP.spec_and (K.invAsid a h asid hh.1) (K2.invAsid a h asid hh.2) + cacheOp a h op root asid va n pa hh := + WP.spec_and (K.cacheOp a h op root asid va n pa hh.1) (K2.cacheOp a h op root asid va n pa hh.2) + invS2Page a h asid va hh := WP.spec_and (K.invS2Page a h asid va hh.1) (K2.invS2Page a h asid va hh.2) + invS2Vmid a h asid hh := WP.spec_and (K.invS2Vmid a h asid hh.1) (K2.invS2Vmid a h asid hh.2) + invHiPage a h va hh := WP.spec_and (K.invHiPage a h va hh.1) (K2.invHiPage a h va hh.2) + invHiVs a h y hh := WP.spec_and (K.invHiVs a h y hh.1) (K2.invHiVs a h y hh.2) + hwAccessFlag := K.hwAccessFlag + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelRdThA.lean b/proofs/Fl4ProofsA64/P3/DelRdThA.lean new file mode 100644 index 000000000..2e7f72c52 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelRdThA.lean @@ -0,0 +1,51 @@ +/- +P3 (AArch64, delete hooks W4, contract §35/§39): **`RdThA` at a non-final Thread cap** — the typed TCB witness the +Thread hook owes `Rd` (a non-final Thread cap's remainder is `Null`, removable). + +* `rngKey_threadA`, `ptrA_threadA`, `of_kindA_threadA`; +* **`rdThA_of_nonfinalA`**: another `OS` slot holds a Thread cap of the same TCB (`other_of_nonfinalA`, `KindA`). + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.DelStepAsmA +import Fl4ProofsA64.P3.DelRdXA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {OS : Set U64} + +theorem rngKey_threadA (t : U64) : RngKeyA (.Thread t) := by + have h := physA_eq (.Thread t) + simp only [objects.cap_is_physical] at h + exact ⟨(okinj h).symm, rfl, rfl⟩ + +theorem ptrA_threadA (t : U64) : ptrA (.Thread t) = t := by + have h := ptrA_eq (.Thread t) + simp only [capability.Cap.ptr] at h + exact (okinj h).symm + +theorem of_kindA_threadA {c : capability.Cap} (h : kindA c = 6) : ∃ q, c = .Thread q := by + cases c <;> simp only [kindA] at h <;> first | exact ⟨_, rfl⟩ | omega + +/-- **`RdThA` at a non-final Thread cap** (module docstring). -/ +theorem rdThA_of_nonfinalA {th : TypedHeapA} {G : List (List U64)} {s : S} {σ : State Unit} {sl : CslotPtr} + (F : FinStA st th OS G s σ) (hsl : AbsSlotA th sl) + (hn : ∀ t, capA st s (cslotAddrA sl) = .Thread t → ¬ isFinalCap' (absCapA (.Thread t)) σ) : + RdThA st OS s (cslotAddrA sl) := by + intro t e + obtain ⟨w, hw, ne, -, pw, kw⟩ := other_of_nonfinalA F hsl e (rngKey_threadA t) (hn t e) + obtain ⟨q, eq⟩ := of_kindA_threadA (kw.trans rfl) + rw [eq, ptrA_threadA, ptrA_threadA] at pw + exact ⟨w, hw, ne, Or.inl (by rw [eq, pw])⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelRdXA.lean b/proofs/Fl4ProofsA64/P3/DelRdXA.lean new file mode 100644 index 000000000..e87343da8 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelRdXA.lean @@ -0,0 +1,178 @@ +/- +P3 (AArch64, delete hooks W2, Architect ruling E2′ / Option P): **the slot-pinned `Rd`** `DelRdXA` (`DelRdA` and J7 with +R2 over the slots other than the finalised one) and its producers for the Frame, PageTable and VSpace-root kinds: +A64 P1's call-level `S1aFin.frame / pageTable / root`, read at the hook's own evaluated call, put into the J7 export of +the keep lemmas (`rdXA_of_J7A`). Also the relaxed J7 slots `J7R2tA`, `J7R2xA` (moved from `P3/DelPoolRecA`). +Named premises: none beyond P1's (`P0.ArchTotal`, `ArchPlat` [P]). New module. +-/ +import Fl4ProofsA64.P3.DelZeroKeepA +import Fl4ProofsA64.P3.CnodeFinalA +import Fl4ProofsA64.P1.S1aR2FinPt +import Fl4ProofsA64.P1.S1aR2FinFrame +import Fl4ProofsA64.P1.S1aR2FinRoot +import Fl4ProofsA64.P1.S1aStepR2Boot +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + +/-- **The relaxed R2 part of J7** (module docstring). -/ +def J7R2tA : Set U64 → JSlotA S := fun _ _ _ _ _ _ _ => True + +variable (st) in +/-- **R2 over the slots other than `x`** (A64 P1's `S1aDelPn`), with the key/untyped separation over all slots. -/ +def J7R2xA {A : Type} (ar : machine.Arch A) (n x : U64) (Pt : Set U64) : JSlotA S := fun _ H OS s _ _ => + ∃ (Pp Tb Te Kt : Set U64) (Lv Re bits : U64 → Nat), + S1aDelPn st H OS Pt Pp Tb Te Kt Lv Re bits n s x ∧ S1aKtUt st OS Kt s ∧ + ∃ KLv : U64 → Nat, KTab ar Kt KLv ∧ S1aHiK st (OS \ {x}) Kt KLv s + +theorem J7LA.mapR {Z : XSetsA} {H : Heap} {OS : Set U64} {R R' : Set U64 → JSlotA S} {s : S} + {σ : State Unit} {m : McState} (h : J7LA st R Z H OS s σ m) + (hR : ∀ Pt, R Pt Z H OS s σ m → R' Pt Z H OS s σ m) : J7LA st R' Z H OS s σ m := by + obtain ⟨Pt, Ub, vr, pn, arm, capc, vc, df, hicap, link, vcks, fu, ual, vcu, r⟩ := h + exact ⟨Pt, Ub, vr, pn, arm, capc, vc, df, hicap, link, vcks, fu, ual, vcu, hR Pt r⟩ + +theorem JStage3TA.mapJ7A {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} + {κ : U64 → U64} {n : Nat} {J5 : JSlotA S} {R R' : Set U64 → JSlotA S} {s : S} {a : A} {σ : State Unit} + {m : McState} + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7LA st R) s a σ m) + (hR : ∀ Pt, R Pt Z H OS s σ m → R' Pt Z H OS s σ m) : + JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7LA st R') s a σ m := + { J with j7 := J.j7.mapR hR } + +variable (st) in +/-- (Architect pre-approval, contract §24, `deadExt.tvcpu`) **A Thread cap reaching `Rd` is not the last**: another +`OS` slot holds a typed namer of the same TCB (`NamesDA .tcb`: a Thread cap, or a TCB zombie: what `¬ isFinalCap'` gives through +`isFinalCap_iffA`). A final Thread cap leaves a zombie (`finRemA`), never a removable +remainder, so only a non-final one reaches the emptying; the emptying needs this to keep `DeadExtA.tvcpu`. -/ +def RdThA (OS : Set U64) (s : S) (x : U64) : Prop := + ∀ t, capA st s x = .Thread t → ∃ y ∈ OS, y ≠ x ∧ NamesDA .tcb t (capA st s y) + +/-- A slot not holding a Thread cap has `RdThA`. -/ +theorem rdThA_of_ne {OS : Set U64} {s : S} {x : U64} (h : ∀ t, capA st s x ≠ .Thread t) : RdThA st OS s x := + fun t e => absurd e (h t) + +variable (st) in +/-- **`Rd` with the slot-pinned R2** (module docstring; with the Thread clause `RdThA`). -/ +def DelRdXA (ar : machine.Arch A) (Z : XSetsA) (H : Heap) (OS : Set U64) (ipa : U64) + (Rv : S → A → State Unit → MdbGhostA → U64 → Prop) : S → A → State Unit → MdbGhostA → U64 → Prop := + fun s a σ G x => (DelRdA st Rv s a σ G x ∧ RdThA st OS s x) ∧ ∃ m, J7LA st (J7R2xA st ar ipa x) Z H OS s σ m + +/-- J7 at an unchanged state, with the R2 slot replaced (the shape of the keep lemmas' J7 export). -/ +theorem J7LA.preMapA {Z : XSetsA} {H : Heap} {OS : Set U64} {ipa : U64} {s : S} {σ : State Unit} {m : McState} + (J : J7LA st (J7R2A st ar ipa) Z H OS s σ m) : + ∀ Rj' : Set U64 → JSlotA S, (∀ Pt Ub : Set U64, VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt → + (∀ q, SchedPageA st Z OS s q → q ∈ Pt) → J7R2A st ar ipa Pt Z H OS s σ m → Rj' Pt Z H OS s σ m) → + J7LA st Rj' Z H OS s σ m := by + intro Rj' h + obtain ⟨Pt, Ub, vr, pn, arm, capc, vc, df, hicap, link, vcks, fu, ual, vcu, R⟩ := J + exact ⟨Pt, Ub, vr, pn, arm, capc, vc, df, hicap, link, vcks, fu, ual, vcu, h Pt Ub vr link R⟩ + +/-- **The slot-pinned J7** from a keep lemma's J7 export and A64 P1's R2 over the other slots at the post state. -/ +theorem rdXA_of_J7A {Z Z' : XSetsA} {H : Heap} {OS : Set U64} {ipa x : U64} {s q : S} {σ σ' : State Unit} {m : McState} + (J7f : ∀ Rj' : Set U64 → JSlotA S, (∀ Pt Ub : Set U64, VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt → + (∀ p, SchedPageA st Z OS s p → p ∈ Pt) → J7R2A st ar ipa Pt Z H OS s σ m → Rj' Pt Z' H OS q σ' m) → + J7LA st Rj' Z' H OS q σ' m) + (caps : ∀ y ∈ OS, capA st q y = capA st s y) + (vz : ∀ (Pt Ub Pp Tb Hr Te Kt : Set U64) (Lv Re bits : U64 → Nat), + VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt → (∀ p, SchedPageA st Z OS s p → p ∈ Pt) → + S1aR2 st H OS Pt Pp Tb Hr Te Kt Lv Re bits ipa s → VsZ st (L.ksb s) Pt s q) + (pn : ∀ (Pt Ub Pp Tb Hr Te Kt : Set U64) (Lv Re bits : U64 → Nat), + VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt → S1aR2 st H OS Pt Pp Tb Hr Te Kt Lv Re bits ipa s → + S1aDelPn st H OS Pt (PoolSet st Pp s) Tb Te Kt Lv Re bits ipa q x) : + J7LA st (J7R2xA st ar ipa x) Z' H OS q σ' m := by + refine J7f _ fun Pt Ub vr link R => ?_ + obtain ⟨Pp, Tb, Hr, Te, Kt, Lv, Re, bits, R2, sep, KLv, ktK, hik⟩ := R + exact ⟨PoolSet st Pp s, Tb, Te, Kt, Lv, Re, bits, pn Pt Ub Pp Tb Hr Te Kt Lv Re bits vr R2, + sep.slots fun y hy => Or.inl (caps y hy), KLv, ktK, + S1aHiK.keep hik ⟨EdgeKeepOn.ofVsZ R2 (vz Pt Ub Pp Tb Hr Te Kt Lv Re bits vr link R2), + fun y hy R m a c => ⟨y, hy.1, m, a, by rwa [caps y hy.1] at c⟩⟩⟩ + +/-- No `PtW Pt` word is an ASID-table word, from R2's footprint and `HeapKsOff`. -/ +theorem ptKs_footA {H : Heap} {kb : U64} {Pt : Set U64} (hH : HeapOK H) (hK : HeapKsOff H kb) + (hw : kb.val + 788736 ≤ 2 ^ 64) (hF : PtHG H Pt) : ∀ b, PtW Pt b → ¬ AsW kb b := by + rintro b ⟨p, hp, o, ho, al, rfl⟩ + let i : U64 := ⟨BitVec.ofNat 64 (o.val / 8)⟩ + have hi : i.val = o.val / 8 := by + show (BitVec.ofNat 64 (o.val / 8)).toNat = o.val / 8 + rw [BitVec.toNat_ofNat] + exact Nat.mod_eq_of_lt (by omega) + have hi' : i.val < 512 := by omega + have e : peAF p i = wa p o := by + have : core.num.U64.wrapping_mul i 8#u64 = o := UScalar.eq_of_val_eq (by rw [wm8_valA hi']; omega) + unfold peAF + rw [this] + rw [← e] + exact ptHA_not_asW hH hK hw (P := Pt) (fun p hp => (hF p hp).2) hp hi' + +variable {H : Heap} {OS : Set U64} {s q : S} {a a' : A} {x : U64} + +/-- R2 over the other slots after the evaluated Frame finaliser. -/ +theorem pnFrameA [PlainStore st] (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) {V Ub Pt Pp Tb Hr Te Kt : Set U64} + {Lv Re bits : U64 → Nat} + (vr : VrOK H.omega H.frame H.scs H.scBits OS V Ub (L.ksb s) Pt) (os : OSOK H.tcbs H.omega H.frame OS) + (heap : HeapOK H) (hK : HeapKsOff H (L.ksb s)) (hw : (L.ksb s).val + 788736 ≤ 2 ^ 64) (kinds : KindA st OS s) + (hx : x ∈ OS) (R2 : S1aR2 st H OS Pt Pp Tb Hr Te Kt Lv Re bits hPl.ipa s) (f : vspace.FrameCap) + (cap : capA st s x = .Frame f.ptr f.size f.rights f.is_device f.mapped f.is_ept f.asid f.vaddr) + (fin : Bool) {n : capability.Cap × capability.Cap} + (e : objects.finalise_cap_object st ar s a + (.Frame f.ptr f.size f.rights f.is_device f.mapped f.is_ept f.asid f.vaddr) fin = ok (n, q, a')) : + S1aDelPn st H OS Pt (PoolSet st Pp s) Tb Te Kt Lv Re bits hPl.ipa q x := by + have R2a := S1aR2.activePools R2 + have high : HvRt st OS s ⊆ Hr := fun p hp => by rw [R2a.highRoots]; exact hp + have P1 := S1aFin.frame hA hPl vr os heap R2.place high R2a.toDelM kinds hx + (ptKs_footA heap hK hw R2a.footprint) f cap fin a + rw [fin_cap_eq_obj (by simp), e, WP.spec_ok] at P1 + exact P1.2 + +/-- R2 over the other slots after the evaluated PageTable finaliser. -/ +theorem pnPtA (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) {V Ub Pt Pp Tb Hr Te Kt : Set U64} + {Lv Re bits : U64 → Nat} + (vr : VrOK H.omega H.frame H.scs H.scBits OS V Ub (L.ksb s) Pt) (os : OSOK H.tcbs H.omega H.frame OS) + (heap : HeapOK H) (hK : HeapKsOff H (L.ksb s)) (hw : (L.ksb s).val + 788736 ≤ 2 ^ 64) (mdb : MdbS st OS s) + (hx : x ∈ OS) (R2 : S1aR2 st H OS Pt Pp Tb Hr Te Kt Lv Re bits hPl.ipa s) {ptr asid va : U64} {mapped f : Bool} + (cap : capA st s x = .PageTable ptr mapped asid va) + (final : cdt.is_final_capability_with_cap st s x (.PageTable ptr mapped asid va) = ok f) + {n : capability.Cap × capability.Cap} + (e : objects.finalise_cap_object st ar s a (.PageTable ptr mapped asid va) f = ok (n, q, a')) : + S1aDelPn st H OS Pt (PoolSet st Pp s) Tb Te Kt Lv Re bits hPl.ipa q x := by + have R2a := S1aR2.activePools R2 + have high : HvRt st OS s ⊆ Hr := fun p hp => by rw [R2a.highRoots]; exact hp + have P1 := S1aFin.pageTable hA hPl os vr heap R2.place high (ptKs_footA heap hK hw R2a.footprint) R2a.toDelM mdb + hx cap final a + rw [fin_cap_eq_obj (by simp), e, WP.spec_ok] at P1 + exact P1.2 + +/-- R2 over the other slots after the evaluated VSpace-root finaliser. -/ +theorem pnRootA (hA : P0.ArchTotal ar) {V Ub Pt Pp Tb Hr Te Kt : Set U64} + {Lv Re bits : U64 → Nat} {ipa : U64} + (vr : VrOK H.omega H.frame H.scs H.scBits OS V Ub (L.ksb s) Pt) (os : OSOK H.tcbs H.omega H.frame OS) + (heap : HeapOK H) (hK : HeapKsOff H (L.ksb s)) (hw : (L.ksb s).val + 788736 ≤ 2 ^ 64) (mdb : MdbS st OS s) + (hx : x ∈ OS) (R2 : S1aR2 st H OS Pt Pp Tb Hr Te Kt Lv Re bits ipa s) {c : capability.Cap} {ptr : U64} {f : Bool} + (cap : capA st s x = c) (root : S1aRootCap c ptr) (final : cdt.is_final_capability_with_cap st s x c = ok f) + (hcn : c ≠ .Null) (hz : ∀ p t n, c ≠ .Zombie p t n) {n : capability.Cap × capability.Cap} + (e : objects.finalise_cap_object st ar s a c f = ok (n, q, a')) : + S1aDelPn st H OS Pt (PoolSet st Pp s) Tb Te Kt Lv Re bits ipa q x := by + have R2a := S1aR2.activePools R2 + have high : HvRt st OS s ⊆ Hr := fun p hp => by rw [R2a.highRoots]; exact hp + have P1 := S1aFin.root (ar := ar) hA os vr heap R2.place high (ptKs_footA heap hK hw R2a.footprint) R2a.toDelM mdb + hx cap root final a + have eo : objects.finalise_cap st ar s a c f false = objects.finalise_cap_object st ar s a c f := by + rcases root with ⟨_, _, rfl⟩ | ⟨_, _, rfl⟩ | ⟨_, _, _, rfl⟩ <;> exact fin_cap_eq_obj (by simp) + rw [eo, e, WP.spec_ok] at P1 + exact P1.2 + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelRec6A.lean b/proofs/Fl4ProofsA64/P3/DelRec6A.lean new file mode 100644 index 000000000..769aac681 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelRec6A.lean @@ -0,0 +1,506 @@ +/- +P3 (AArch64, delete hooks, contract §81; Architect option A, 2026-12-04): **`DelXA6`**, the deletion loop's record with +the facts `JStage3TA` does not state. + +* **`Std6A`**: the record edit #6 facts (`utWin`, `vcBack`, `runBack`, `vcSnap`, `vcOS`, `userFr`, `bfSpec`), the VCPU + symmetries, the static `heapWin` / `coresAll`, `zwf`, and A64 P1's `FrOff` (the detach reads it at hook states). +* **`DelXA6`**: `DelXA` with `Std6A` at the same buffer pairs `Bf` inside the existential; `DelXA6G.toDelXA`. +* **`Std6A.ofJA`** (entry), **`Std6GA.keepA`** (a step keeping the `OS` caps, the VCPU objects and the TCBs' VCPU words). +* **`Std6GA.stepA`**: as `keepA` for a step that moves caps (each `OS` slot gets `Null`, a `Zombie`, or a cap an `OS` + slot held), the new `zwf` given; **`Std6GA.slotStepA`**: its instance for the loop's slot-word steps (swap, zombie + reduction, emptying). +* **`Std6GA.vsZeroA`**: `Std6A` across a page-table / ASID-table word zero (the VSpace finalisers' writes); + `Std6GA.unghostA` drops the loop's ghost updates from the sets. +* **`JStage3TA.vcw_offStepNA`** (every VCPU word is off the scheduler / IPC-object footprint) and + **`Std6GA.schedStepA`**: `Std6A` across a scheduler or IPC-object step. +* **`DelXA6G.toStage3A`**: a loop state recovers `JStage3A`, given A64 P1's `SysL` / `SymL` at the state and + `SchedValidA` (neither is part of the loop record), at the loop's `McState` `m`. + +The loop records (`DelXA` / `DelXPA` / `DelXA6`) take the entry's `McState` `m` as a parameter (contract §91): deletion +keeps it, so the record comes back at the same `m`. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.DelRecA +import Fl4ProofsA64.P3.FrAgDefA +import Fl4ProofsA64.P3.DelJ7LA +import Fl4ProofsA64.P3.Stage3J5A +import Fl4ProofsA64.P3.DelStepKeepA +import Fl4ProofsA64.P3.Stage3SchedWrNTA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} (st : machine.Store S) [L : LawfulStore st] {A : Type} + +/-- **[J] The facts of a loop state beyond `JStage3TA`** (module docstring), with the frame-word agreement on the +guard set `Ag` (`∅` for the callers without an entry agreement). -/ +structure Std6GA (Ag : Set U64) (Z : XSetsA) (H : Heap) (OS : Set U64) (Bf : Set (U64 × Fl4SpecA64.Word)) (s : S) + (σ : Fl4SpecA64.State Unit) : Prop where + utWin : UtWinCapA st OS s + devWin : DevWinCapA st OS s + vcBack : ∀ v ∈ Z.V, L.mem s v ≠ 0#u64 → L.mem s (wa (L.mem s v) 3664#u64) = v + runBack : ∀ t ∈ Z.T, L.mem s (runVcpuA t) ≠ 0#u64 → L.mem s (wa (L.mem s (runVcpuA t)) 24#u64) = t + vcSnap : VcpuStateSnapA Z.V (L.mem s) σ + vcOS : ∀ b, VcW Z.V b → ¬ InOS OS b + userFr : UserFrSpecA st OS s σ + frTh : FrThA st Z.th OS s + bfSpec : ∀ bp ∈ Bf, bp.2 = specPtr bp.1 + vcSym1 : VcSym1A st Z.T s + vcSym2 : VcSym2A st Z.V s + heapWin : ∀ p k, H p = some k → + layout.PPTR_BASE.val ≤ p.val ∧ p.val + 2 ^ k.bits ≤ layout.PPTR_BASE.val + 2 ^ 44 + coresAll : ∀ k < Fl4SpecA64.fl4A64Platform.numNodes, ∃ c ∈ Z.Cs, c.val = k + zwf : DelInvA st Z.th s + frOff : FrOff st Z.Cs OS (L.ksb s) s + frAg : FrAgA st OS Ag s σ + +/-- `Std6GA` at `Ag = ∅`: the record of the callers without an entry agreement. -/ +abbrev Std6A (Z : XSetsA) (H : Heap) (OS : Set U64) (Bf : Set (U64 × Fl4SpecA64.Word)) (s : S) + (σ : Fl4SpecA64.State Unit) : Prop := + Std6GA st ∅ Z H OS Bf s σ + +/-- **`DelXA` with a further clause `Φ`** on the buffer pairs, the store and the spec state, inside the existential. -/ +def DelXPA (Φ : Set (U64 × Fl4SpecA64.Word) → S → Fl4SpecA64.State Unit → Prop) + (AR : A → HwRootA → Prop) (Z : XSetsA) (H : Heap) (OS U Pp Tb : Set U64) (Lv : U64 → Nat) (ia ie : Nat) + (N E Rp : Set U64) (κ : U64 → U64) (n : Nat) (m : Fl4SpecA64.McState) (J5 J7 : JSlotA S) : DelWA S A := + fun s a σ G => + (∃ (Q : U64 → U64 → List U64) (R : U64 → List U64) (hw : HwRootA) (Ac : Set U64) + (Bf : Set (U64 × Fl4SpecA64.Word)) (stack : U64 → List U64), + JStage3TA st AR { Z with G := G, Q := Q, R := R, hw := hw } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 + s a σ m ∧ Φ Bf s σ) ∧ + ParentAdjA st G.chains G.par s ∧ ZombieTcbNoVcpuA st OS s ∧ + Fl4ProofsA64.P1.W7.R.ST.BufHold st H OS s ∧ Fl4ProofsA64.P1.W7.R.ST.CtOff st (L.ksb s) OS s ∧ + DelCapFxA st Z H s + +/-- **The deletion loop's record with `Std6GA`** (module docstring). -/ +def DelXA6G (Ag : Set U64) (AR : A → HwRootA → Prop) (Z : XSetsA) (H : Heap) (OS U Pp Tb : Set U64) (Lv : U64 → Nat) + (ia ie : Nat) (N E Rp : Set U64) (κ : U64 → U64) (n : Nat) (m : Fl4SpecA64.McState) (J5 J7 : JSlotA S) : DelWA S A := + DelXPA st (Std6GA st Ag Z H OS) AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m J5 J7 + +/-- `DelXA6G` at `Ag = ∅`. -/ +abbrev DelXA6 (AR : A → HwRootA → Prop) (Z : XSetsA) (H : Heap) (OS U Pp Tb : Set U64) (Lv : U64 → Nat) (ia ie : Nat) + (N E Rp : Set U64) (κ : U64 → U64) (n : Nat) (m : Fl4SpecA64.McState) (J5 J7 : JSlotA S) : DelWA S A := + DelXA6G st ∅ AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m J5 J7 + +variable {st} {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s s' : S} {a : A} {σ σ' : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + {G : MdbGhostA} {Ag : Set U64} + +/-- **A live frame word's object**: a non-device frame object `.other z` of `H` holds it; it is off the `OS` slots and +below the kernel-state block or above its first `223488` bytes. -/ +theorem JStage3TA.frwA {R7 : Set U64 → JSlotA S} + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7LA st R7) s a σ m) + {Cs : Set U64} (fo : FrOff st Cs OS (L.ksb s) s) {b : U64} (lv : FrameWordLive st OS s b) : + ∃ p z, H p = some (.other z) ∧ InObj p (.other z) b ∧ ¬ InOS OS b ∧ + ((L.ksb s).val + 223488 ≤ b.val ∨ b.val < (L.ksb s).val) := by + obtain ⟨u, hu, ptr, size, rights, x1, x2, x3, x4, bits, hc, hbits, h1, h2⟩ := lv + obtain ⟨-, -, -, -, -, ⟨-, frOK⟩, -⟩ := J.j7 + have hH : H ptr = some (.other bits.val) := frOK u hu ptr size rights x1 x2 x3 x4 hc bits hbits + obtain ⟨hos, -, hreg⟩ := fo u hu ptr size rights x1 x2 x3 x4 hc bits hbits + refine ⟨ptr, bits.val, hH, ⟨h1, by simpa [HKind.bits] using h2⟩, ?_, ?_⟩ + · rintro ⟨y, hy, y1, y2⟩ + rcases hos y hy with e | e <;> omega + · rcases hreg with e | e <;> omega + +/-- **No live frame word is a scheduler or IPC-object word** (`JStage3TA.vcw_offStepNA`'s twin). -/ +theorem JStage3TA.frw_offStepNA {R7 : Set U64 → JSlotA S} [J7PpA st (J7LA st R7)] + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7LA st R7) s a σ m) + {Cs : Set U64} (fo : FrOff st Cs OS (L.ksb s) s) {b : U64} (al : Aligned b) (lv : FrameWordLive st OS s b) : + ¬ SchedStepNWA (st := st) N E Rp Z m σ s b := by + obtain ⟨p, z, hH, inObj, -, hk⟩ := J.frwA fo lv + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.ent.ksw + have hsc : ∀ c ∈ H.scs, ¬ InSC c (H.scBits c) b := by + rintro c ⟨z', hz⟩ ⟨c1, c2⟩ + have e : H.scBits c = z' := by unfold Heap.scBits; rw [hz] + have ne : p ≠ c := fun h => by rw [h, hz] at hH; cases hH + have := J.ent.heap.disj p _ c _ hH hz ne + unfold InObj at inObj + simp only [HKind.bits] at this inObj + rw [e] at c2 + omega + have hg : ∀ g : U64, g.val < 223488 → wa (L.ksb s) g ≠ b := by + intro g hg e + have ev : (wa (L.ksb s) g).val = (L.ksb s).val + g.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + rw [← e, ev] at hk + omega + exact fun hw => hw.elim + (J.omega_offStepA ⟨al, p, _, hH, Or.inr (Or.inr (Or.inl ⟨z, rfl⟩)), inObj⟩ hsc hg) + (J.ipc_off_hobjA hH (by simp) inObj) + +/-- **No live frame word is a cap-slot word** (the loop's slot steps). -/ +theorem JStage3TA.frw_offCapWA {R7 : Set U64 → JSlotA S} + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7LA st R7) s a σ m) + {Cs : Set U64} (fo : FrOff st Cs OS (L.ksb s) s) {b : U64} (lv : FrameWordLive st OS s b) : + ¬ W7.CapW H.tcbs {b | InOS OS b} ∅ b := by + obtain ⟨p, z, hH, inObj, hos, -⟩ := J.frwA fo lv + rintro (⟨t, ht, K, hK, e⟩ | ⟨h, -⟩) + · have htH : H t = some .tcb := ht + have ne : p ≠ t := fun h => by rw [h, htH] at hH; cases hH + have hd := J.ent.heap.disj p _ t _ hH htH ne + have hft := J.ent.heap.fits t _ htH + unfold InObj at inObj + simp only [HKind.bits] at hd hft inObj + have ev : (wa t K).val = t.val + K.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + rw [e, ev] at inObj + omega + · exact hos h + +/-- **No live frame word is in the kernel-state block below the ASID table** (the preemption point, IRQ cleanup). -/ +theorem JStage3TA.frw_offKsA {R7 : Set U64 → JSlotA S} + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7LA st R7) s a σ m) + {Cs : Set U64} (fo : FrOff st Cs OS (L.ksb s) s) {b : U64} (lv : FrameWordLive st OS s b) : + ¬ ((L.ksb s).val ≤ b.val ∧ b.val < (L.ksb s).val + 772352) := by + obtain ⟨p, z, hH, inObj, -, hk⟩ := J.frwA fo lv + rintro ⟨g1, g2⟩ + by_cases c : b.val < (L.ksb s).val + 223488 + · omega + · exact J.heap_offNodesPA hH inObj.1 inObj.2 ⟨by omega, g2⟩ + +theorem DelXA6G.toDelXA (h : DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m J5 J7 s a σ G) : + DelXA st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m J5 J7 s a σ G := by + obtain ⟨⟨Q, R, hw, Ac, Bf, stack, J, -⟩, rest⟩ := h + exact ⟨⟨Q, R, hw, Ac, Bf, stack, J⟩, rest⟩ + +/-- **Entry**: `JStage3A` has them all. -/ +theorem Std6GA.ofJA (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (fa : FrAgA st OS Ag s σ) : Std6GA st Ag Z H OS Bf s σ where + utWin := J.utWin + devWin := J.devWin + vcBack := J.vcBack + runBack := J.runBack + vcSnap := J.vcSnap + vcOS := J.vcOS + userFr := J.userFr + frTh := J.frTh + bfSpec := J.bfSpec + vcSym1 := J.vcSym1 + vcSym2 := J.vcSym2 + heapWin := J.heapWin + coresAll := J.coresAll + zwf := J.zwf + frOff := J.sys.sysl.fo + frAg := fa + +/-- **Entry** without an agreement (`Ag = ∅`). -/ +theorem Std6A.ofJA (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) : + Std6A st Z H OS Bf s σ := + Std6GA.ofJA J FrAgA.nilA + +/-- `Std6A` reads only the sets `T`, `V`, `Cs` and the typed heap. -/ +theorem Std6GA.ghostA (h : Std6GA st Ag Z H OS Bf s σ) (G : MdbGhostA) (Q : U64 → U64 → List U64) (R : U64 → List U64) + (hw : HwRootA) : Std6GA st Ag { Z with G := G, Q := Q, R := R, hw := hw } H OS Bf s σ := + ⟨h.utWin, h.devWin, h.vcBack, h.runBack, h.vcSnap, h.vcOS, h.userFr, h.frTh, h.bfSpec, h.vcSym1, h.vcSym2, h.heapWin, h.coresAll, + h.zwf, h.frOff, h.frAg⟩ + +/-- **`Std6A` across a step** keeping the `OS` slots, the state-block base, the VCPUs' words, the TCBs' `VCPU` / +`RUN_VCPU` words, the spec's VCPU objects (backward) and data pages (forward). -/ +theorem Std6GA.keepA (h : Std6GA st Ag Z H OS Bf s σ) (R : VcRunA st Z.T Z.V s) + (sub : ∀ y, IsSlotA Z.th y → y ∈ OS) (k : OSKeep st OS s s') (kb : L.ksb s' = L.ksb s) + (hv : ∀ v ∈ Z.V, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → L.mem s' (wa v o) = L.mem s (wa v o)) + (ht : ∀ t ∈ Z.T, L.mem s' (wa t 3664#u64) = L.mem s (wa t 3664#u64)) + (hr : ∀ t ∈ Z.T, L.mem s' (runVcpuA t) = L.mem s (runVcpuA t)) + (hk : ∀ v ∈ Z.V, ∀ x, σ'.kheap (specPtr v) = some (.archObj (.vcpu x)) → + σ.kheap (specPtr v) = some (.archObj (.vcpu x))) + (dp : ∀ p dev pg, σ.kheap p = some (.archObj (.dataPage dev pg)) → + σ'.kheap p = some (.archObj (.dataPage dev pg))) + (ag : FrAgA st OS Ag s σ → FrAgA st OS Ag s' σ') : + Std6GA st Ag Z H OS Bf s' σ' := by + have capE : ∀ u ∈ OS, capA st s' u = capA st s u := fun u hu => k.capA hu + have h0 : ∀ v ∈ Z.V, L.mem s' v = L.mem s v := fun v m => by + have := hv v m 0#u64 (by simp) (by simp) + rwa [wa_zeroVcA] at this + have h24 : ∀ v ∈ Z.V, L.mem s' (wa v 24#u64) = L.mem s (wa v 24#u64) := + fun v m => hv v m 24#u64 (by simp) (by simp) + exact + { utWin := h.utWin.keepA capE + devWin := h.devWin.keepA capE + vcBack := vcBack_keepA R.vtcb h0 ht h.vcBack + runBack := runBack_keepA R.run hr h24 h.runBack + vcSnap := h.vcSnap.keepA hv hk + vcOS := h.vcOS + userFr := UserFrSpecA.keepA capE dp h.userFr + frTh := h.frTh.keepA capE fun _ _ e => e + bfSpec := h.bfSpec + vcSym1 := h.vcSym1.keepA R ht h0 + vcSym2 := h.vcSym2.keepA R h24 hr + heapWin := h.heapWin + coresAll := h.coresAll + zwf := fun y hy p zt c e => h.zwf y hy p zt c ((capE y (sub y hy)).symm.trans e) + frOff := FrOff.ks kb (CapInv.keepO h.frOff k) + frAg := ag h.frAg } + +/-- **`Std6A` across a step moving caps** (module docstring). -/ +theorem Std6GA.stepA (h : Std6GA st Ag Z H OS Bf s σ) (R : VcRunA st Z.T Z.V s) + (caps : ∀ y ∈ OS, capA st s' y = .Null ∨ (∃ p t c, capA st s' y = .Zombie p t c) ∨ + ∃ z ∈ OS, capA st s' y = capA st s z) + (zwf : DelInvA st Z.th s') (kb : L.ksb s' = L.ksb s) + (hv : ∀ v ∈ Z.V, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → L.mem s' (wa v o) = L.mem s (wa v o)) + (ht : ∀ t ∈ Z.T, L.mem s' (wa t 3664#u64) = L.mem s (wa t 3664#u64)) + (hr : ∀ t ∈ Z.T, L.mem s' (runVcpuA t) = L.mem s (runVcpuA t)) + (hk : ∀ v ∈ Z.V, ∀ x, σ'.kheap (specPtr v) = some (.archObj (.vcpu x)) → + σ.kheap (specPtr v) = some (.archObj (.vcpu x))) + (dp : ∀ p dev pg, σ.kheap p = some (.archObj (.dataPage dev pg)) → + σ'.kheap p = some (.archObj (.dataPage dev pg))) + (ag : FrAgA st OS Ag s σ → FrAgA st OS Ag s' σ') : + Std6GA st Ag Z H OS Bf s' σ' := by + have h0 : ∀ v ∈ Z.V, L.mem s' v = L.mem s v := fun v m => by + have := hv v m 0#u64 (by simp) (by simp) + rwa [wa_zeroVcA] at this + have h24 : ∀ v ∈ Z.V, L.mem s' (wa v 24#u64) = L.mem s (wa v 24#u64) := + fun v m => hv v m 24#u64 (by simp) (by simp) + exact + { utWin := fun u hu p sb fi e => by + rcases caps u hu with c | ⟨_, _, _, c⟩ | ⟨z, hz, c⟩ + · rw [c] at e; cases e + · rw [c] at e; cases e + · exact h.utWin z hz p sb fi (c.symm.trans e) + devWin := fun u hu => by + rcases caps u hu with c | ⟨_, _, _, c⟩ | ⟨z, hz, c⟩ + · exact ⟨fun p sb fi e => (by rw [c] at e; cases e), fun p sz r m e a v e' => (by rw [c] at e'; cases e')⟩ + · exact ⟨fun p sb fi e => (by rw [c] at e; cases e), fun p sz r m e a v e' => (by rw [c] at e'; cases e')⟩ + · exact ⟨fun p sb fi e => (h.devWin z hz).1 p sb fi (c.symm.trans e), + fun p sz r m e a v e' => (h.devWin z hz).2 p sz r m e a v (c.symm.trans e')⟩ + vcBack := vcBack_keepA R.vtcb h0 ht h.vcBack + runBack := runBack_keepA R.run hr h24 h.runBack + vcSnap := h.vcSnap.keepA hv hk + vcOS := h.vcOS + userFr := fun y hy p size rights mapped ept asid va e => by + rcases caps y hy with c | ⟨_, _, _, c⟩ | ⟨z, hz, c⟩ + · rw [c] at e; cases e + · rw [c] at e; cases e + · exact dp _ _ _ (h.userFr z hz p size rights mapped ept asid va (c.symm.trans e)) + frTh := fun y hy p size rights x1 x2 x3 x4 bits e hb => by + rcases caps y hy with c | ⟨_, _, _, c⟩ | ⟨z, hz, c⟩ + · rw [c] at e; cases e + · rw [c] at e; cases e + · exact h.frTh z hz p size rights x1 x2 x3 x4 bits (c.symm.trans e) hb + bfSpec := h.bfSpec + vcSym1 := h.vcSym1.keepA R ht h0 + vcSym2 := h.vcSym2.keepA R h24 hr + heapWin := h.heapWin + coresAll := h.coresAll + zwf := zwf + frOff := fun y hy => by + rw [kb] + rcases caps y hy with c | ⟨_, _, _, c⟩ | ⟨z, hz, c⟩ + · rw [c]; exact FrOffP.null _ _ _ + · rw [c]; exact FrOffP.zombie _ _ _ _ _ _ + · rw [c]; exact h.frOff z hz + frAg := ag h.frAg } + +/-- **`Std6A` across a slot-word step of the loop** (swap, zombie reduction, emptying). -/ +theorem Std6GA.slotStepA {J5 : JSlotA S} {R7 : Set U64 → JSlotA S} + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7LA st R7) s a σ m) + (h : Std6GA st Ag Z H OS Bf s σ) + (w : WritesIn st s s' (W7.CapW H.tcbs {b | InOS OS b} ∅)) (HS : SpecSlotA σ σ') + (caps : ∀ y ∈ OS, capA st s' y = .Null ∨ (∃ p t c, capA st s' y = .Zombie p t c) ∨ + ∃ z ∈ OS, capA st s' y = capA st s z) + (zwf : DelInvA st Z.th s') (um : σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory) : + Std6GA st Ag Z H OS Bf s' σ' := by + obtain ⟨Pt, Ub, -, -, armc, -⟩ := J.j7 + have vw : ∀ v ∈ Z.V, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → L.mem s' (wa v o) = L.mem s (wa v o) := + fun v hv o ho h8 => by + obtain ⟨al, off⟩ := vcw_notCapWA J.ent.heap armc.vh h.vcOS hv ho h8 + exact w.mem_eq al off + have tw : ∀ t ∈ Z.T, ∀ K : U64, 224 ≤ K.val → K.val + 8 ≤ 4096 → K.val % 8 = 0 → + L.mem s' (wa t K) = L.mem s (wa t K) := fun t ht => + tcbFld_keptA J.toJEntTA w t (J.tcbs ▸ ht) + exact h.stepA J.vcRun caps zwf w.1 vw (fun t ht => tw t ht _ (by simp) (by simp) (by simp)) + (fun t ht => tw t ht 3672#u64 (by simp) (by simp) (by simp)) + (fun v _ x e => HS.back e (fun _ _ c => by cases c) (fun _ c => by cases c)) + (fun p dev pg e => HS.keep e (fun _ _ c => by cases c) (fun _ c => by cases c)) + (fun ag => ag.stepA (fun _ lv => lv.fromA caps) + (fun b al lv => w.mem_eq al (J.frw_offCapWA h.frOff lv)) um) + +/-- **No VCPU word is a scheduler or IPC-object word** (module docstring). -/ +theorem JStage3TA.vcw_offStepNA {J5 : JSlotA S} {R7 : Set U64 → JSlotA S} [J7PpA st (J7LA st R7)] + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7LA st R7) s a σ m) + {v o : U64} (hv : v ∈ Z.V) (ho : o.val < 4096) (h8 : o.val % 8 = 0) : + Aligned (wa v o) ∧ ¬ SchedStepNWA (st := st) N E Rp Z m σ s (wa v o) := by + obtain ⟨Pt, Ub, -, -, arm, -, -, -, -, -, apart, -⟩ := J.j7 + have hH := arm.vh v hv + have hfit := J.ent.heap.fits v _ hH + have hal := J.ent.heap.align v _ hH + simp only [HKind.bits] at hfit hal + have ev : (wa v o).val = v.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have al : Aligned (wa v o) := by show _ % 8 = 0; rw [ev]; omega + have inObj : InObj v (.other 12) (wa v o) := by + unfold InObj; simp only [HKind.bits]; omega + have hsc : ∀ c ∈ H.scs, ¬ InSC c (H.scBits c) (wa v o) := by + rintro c ⟨z, hz⟩ ⟨c1, c2⟩ + have e : H.scBits c = z := by unfold Heap.scBits; rw [hz] + have ne : v ≠ c := fun h => by rw [h, hz] at hH; cases hH + have := J.ent.heap.disj v _ c _ hH hz ne + simp only [HKind.bits] at this + rw [e] at c2 + omega + have hg : ∀ g : U64, g.val < 223488 → wa (L.ksb s) g ≠ wa v o := by + intro g hg e + exact apart _ ⟨v, hv, o, ho, h8, rfl⟩ g hg e + exact ⟨al, fun hw => hw.elim + (J.omega_offStepA ⟨al, v, _, hH, Or.inr (Or.inr (Or.inl ⟨12, rfl⟩)), inObj⟩ hsc hg) + (J.ipc_off_hobjA hH (by simp) inObj)⟩ + +/-- **`Std6A` across a scheduler or IPC-object step** (module docstring). -/ +theorem Std6GA.schedStepA {J5 : JSlotA S} {R7 : Set U64 → JSlotA S} [J7PpA st (J7LA st R7)] + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7LA st R7) s a σ m) + (h : Std6GA st Ag Z H OS Bf s σ) (sub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (w : WritesIn st s s' (SchedStepNWA (st := st) N E Rp Z m σ s)) (k : OSKeep st OS s s') (HF : SchedFrameA σ σ') : + Std6GA st Ag Z H OS Bf s' σ' := by + have HS : SpecSchedA σ σ' := HF.1 + have link : ∀ t ∈ Z.T, ∀ K : U64, K = 3664#u64 ∨ K = 3672#u64 → L.mem s' (wa t K) = L.mem s (wa t K) := + fun t ht K hK => by + have hfit := (J.sr.core.tcbs t ht).fits + have hKv : K.val = 3664 ∨ K.val = 3672 := by rcases hK with rfl | rfl <;> simp + have al : Aligned (wa t K) := field_aligned (J.sr.core.tcbs t ht) (by omega) (by omega) + exact w.mem_eq al (J.link_offStepNA ht hK) + exact h.keepA J.vcRun sub k w.1 + (fun v hv o ho h8 => by + obtain ⟨al, off⟩ := J.vcw_offStepNA hv ho h8 + exact w.mem_eq al off) + (fun t ht => link t ht _ (Or.inl rfl)) (fun t ht => link t ht _ (Or.inr rfl)) + (fun v _ x e => HS.back e (fun _ c => by cases c) + (fun c => by rcases c with ⟨_, _, c⟩ | ⟨_, c⟩ | ⟨_, c⟩ | ⟨_, c⟩ <;> cases c)) + (fun p dev pg e => HS.keep e (fun _ c => by cases c) + (fun c => by rcases c with ⟨_, _, c⟩ | ⟨_, c⟩ | ⟨_, c⟩ | ⟨_, c⟩ <;> cases c)) + (fun ag => ag.stepA (fun _ lv => lv.keepA fun u hu => k.capA hu) + (fun b al lv => w.mem_eq al (J.frw_offStepNA h.frOff al lv)) HF.2.2) + +/-- **`Std6A` across a step writing only the state block below the ASID table** (the loop's preemption point and IRQ +cleanup) and keeping the spec kernel heap. -/ +theorem Std6GA.ksStepA {J5 : JSlotA S} {R7 : Set U64 → JSlotA S} [J7PpA st (J7LA st R7)] + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7LA st R7) s a σ m) + (h : Std6GA st Ag Z H OS Bf s σ) (sub : ∀ y, IsSlotA Z.th y → y ∈ OS) (k : OSKeep st OS s s') + (kb : L.ksb s' = L.ksb s) + (mem : ∀ b : U64, Aligned b → ¬ ((L.ksb s).val ≤ b.val ∧ b.val < (L.ksb s).val + 772352) → + L.mem s' b = L.mem s b) + (kh : σ'.kheap = σ.kheap) (um : σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory) : + Std6GA st Ag Z H OS Bf s' σ' := by + obtain ⟨Pt, Ub, -, -, arm, -, -, -, -, -, apart, -⟩ := J.j7 + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.ent.ksw + have tw : ∀ t ∈ Z.T, ∀ K : U64, K.val + 8 ≤ 4096 → K.val % 8 = 0 → L.mem s' (wa t K) = L.mem s (wa t K) := + fun t ht K h1 h2 => by + have f := (J.sr.core.tcbs t ht).fits + have ev : (wa t K).val = t.val + K.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have htH : H t = some .tcb := by + have : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + exact this + refine mem _ (field_aligned (J.sr.core.tcbs t ht) (by omega) h2) (fun g => ?_) + have := J.ko.1 t .tcb htH (fun _ c => by cases c) + simp only [HKind.bits] at this + omega + have vw : ∀ v ∈ Z.V, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → L.mem s' (wa v o) = L.mem s (wa v o) := + fun v hv o ho h8 => by + have hH := arm.vh v hv + have hfit := J.ent.heap.fits v _ hH + have hal := J.ent.heap.align v _ hH + simp only [HKind.bits] at hfit hal + have ev : (wa v o).val = v.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + refine mem _ (show _ % 8 = 0 by rw [ev]; omega) (fun g => ?_) + by_cases c : (wa v o).val < (L.ksb s).val + 223488 + · obtain ⟨d, hd⟩ : ∃ d : U64, d.val = (wa v o).val - (L.ksb s).val := + ⟨⟨BitVec.ofNat 64 ((wa v o).val - (L.ksb s).val)⟩, by + show (BitVec.ofNat 64 _).toNat = _ + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)]⟩ + have e : wa (L.ksb s) d = wa v o := by + apply UScalar.eq_of_val_eq + rw [wa_valm, hd, Nat.mod_eq_of_lt (by omega)] + omega + exact apart _ ⟨v, hv, o, ho, h8, rfl⟩ d (by omega) e + · exact J.heap_offNodesPA (b := wa v o) hH (by omega) (by simp only [HKind.bits]; omega) ⟨by omega, g.2⟩ + exact h.keepA J.vcRun sub k kb vw (fun t ht => tw t ht _ (by simp) (by simp)) + (fun t ht => tw t ht 3672#u64 (by simp) (by simp)) + (fun v _ x e => by rwa [kh] at e) (fun p dev pg e => by rw [kh]; exact e) + (fun ag => ag.stepA (fun _ lv => lv.keepA fun u hu => k.capA hu) + (fun b al lv => mem b al (J.frw_offKsA h.frOff lv)) um) + +/-- `Std6A` at the loop's ghost-updated sets is `Std6A` at the base sets. -/ +theorem Std6GA.unghostA {G : MdbGhostA} {Q : U64 → U64 → List U64} {R : U64 → List U64} {hw : HwRootA} + (h : Std6GA st Ag { Z with G := G, Q := Q, R := R, hw := hw } H OS Bf s σ) : Std6GA st Ag Z H OS Bf s σ := + ⟨h.utWin, h.devWin, h.vcBack, h.runBack, h.vcSnap, h.vcOS, h.userFr, h.frTh, h.bfSpec, h.vcSym1, h.vcSym2, h.heapWin, h.coresAll, + h.zwf, h.frOff, h.frAg⟩ + +/-- **`Std6A` across a VSpace-word zero** (a page-table word or an ASID-table word: the arch finalisers' writes), with +a `VsSpecA` spec step keeping the data pages. -/ +theorem Std6GA.vsZeroA {R7 : Set U64 → JSlotA S} + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7LA st R7) s a σ m) + (h : Std6GA st Ag Z H OS Bf s σ) (sub : ∀ y, IsSlotA Z.th y → y ∈ OS) {b : U64} + (hb : ∀ Pt Ub : Set U64, VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt → + (∀ q, SchedPageA st Z OS s q → q ∈ Pt) → VsR (L.ksb s) Pt b) + (HS : VsSpecA σ σ') + (dp : ∀ p dev pg, σ.kheap p = some (.archObj (.dataPage dev pg)) → + σ'.kheap p = some (.archObj (.dataPage dev pg))) : + Std6GA st Ag Z H OS Bf (L.wr s b 0#u64) σ' := by + obtain ⟨Pt, Ub, vr, -, arm, -, -, -, -, link, -, frub, -⟩ := J.j7 + have vsr := hb Pt Ub vr link + have hal : Aligned b := vr.al _ vsr + have om : b ∈ H.omega := vr.om _ vsr + have osk : OSKeep st OS s (L.wr s b 0#u64) := + OSKeep.of_in J.ent.os (writesIn_wr hal) fun x hx b' _ h1 h2 hF => vr.os b' (hF ▸ vsr) ⟨x, hx, h1, h2⟩ + have tw : ∀ t ∈ Z.T, ∀ K : U64, 3664 ≤ K.val → K.val + 8 ≤ 4096 → K.val % 8 = 0 → + L.mem (L.wr s b 0#u64) (wa t K) = L.mem s (wa t K) := fun t ht K k1 k2 k3 => by + have hta := J.sr.core.tcbs t ht + have hv : (wa t K).val = t.val + K.val := field_val hta (K := K) (by omega) + have hH : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + obtain ⟨Qb, Rb, qq⟩ := J.ent.bxi.1 + have off := (qq.omega _ om).2.1 t hH + exact L.mem_wr_ne hal (field_aligned (K := K) hta (by omega) k3) (by omega) + refine h.keepA J.vcRun sub osk (L.ksb_wr _ _ _) ?_ (fun t ht => tw t ht _ (by simp) (by simp) (by simp)) + (fun t ht => tw t ht 3672#u64 (by simp) (by simp) (by simp)) ?_ dp ?_ + · intro v hv o ho h8 + have hH := arm.vh v hv + have hfit := J.ent.heap.fits v _ hH + have hal' := J.ent.heap.align v _ hH + simp only [HKind.bits] at hfit hal' + have ev : (wa v o).val = v.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact L.mem_wr_ne hal (show _ % 8 = 0 by rw [ev]; omega) + (fun e => vr.vc b vsr ⟨v, hv, o, ho, h8, UScalar.eq_of_val_eq e⟩) + · intro v _ x e + rcases HS.kh (specPtr v) with e' | ⟨_, a2, -, e2, -, n2⟩ + · rw [← e']; exact e + · rw [e] at e2 + have e3 := Option.some.inj e2 + injection e3 with e4 + exact absurd e4.symm (n2 x) + · intro ag + refine ag.stepA (fun _ lv => lv.keepA fun u hu => osk.capA hu) (fun b' al lv => ?_) + (by rw [HS.eq]) + obtain ⟨u, hu, ptr, size, rights, x1, x2, x3, x4, bits, hc, hbits, h1, h2⟩ := lv + have hub : b' ∈ Ub := frub u hu ptr size rights x1 x2 x3 x4 hc bits hbits b' al h1 h2 + exact L.mem_wr_ne hal al (fun e => vr.ub b vsr (by rw [UScalar.eq_of_val_eq e]; exact hub)) + +/-- **`Std6A` carried across a step**, given the typed slots are `OS` slots (the finalisers' export). -/ +@[reducible] def Std6KeepA (st : machine.Store S) [LawfulStore st] (Z : XSetsA) (H : Heap) (OS : Set U64) + (Bf : Set (U64 × Fl4SpecA64.Word)) (s : S) (σ : Fl4SpecA64.State Unit) (s' : S) (σ' : Fl4SpecA64.State Unit) : + Prop := + ∀ {Ag : Set U64}, (∀ y, IsSlotA Z.th y → y ∈ OS) → Std6GA st Ag Z H OS Bf s σ → Std6GA st Ag Z H OS Bf s' σ' + +/-- **A loop state back at `JStage3A`** (module docstring). -/ +theorem DelXA6G.toStage3A {R7 : Set U64 → JSlotA S} + (h : DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st R7) s a σ G) + (sysl : SysL st H Z.Cs OS ia ie s) (symL : SymL st H Z.Cs s) (sv : SchedValidA σ) : + ∃ (Q : U64 → U64 → List U64) (R : U64 → List U64) (hw : HwRootA) (Ac : Set U64) + (Bf : Set (U64 × Fl4SpecA64.Word)) (stack : U64 → List U64), + JStage3A st AR { Z with G := G, Q := Q, R := R, hw := hw } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n + (J5A st) (J7A st R7) s a σ m := by + obtain ⟨⟨Q, R, hw, Ac, Bf, stack, J, X⟩, -, -, -, -, fx⟩ := h + have J' : JStage3TA st AR { Z with G := G, Q := Q, R := R, hw := hw } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n + (J5A st) (J7A st R7) s a σ m := { J with j7 := J7LA.toA J.j7 sysl.userOffFr } + exact ⟨Q, R, hw, Ac, Bf, stack, J'.toJA sysl X.vcSym1 X.vcSym2 X.heapWin X.coresAll fx.ptCapIn symL fx.fhOK + X.zwf sv X.utWin X.devWin X.vcBack X.runBack X.vcSnap X.vcOS X.userFr X.frTh X.bfSpec⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelRecA.lean b/proofs/Fl4ProofsA64/P3/DelRecA.lean new file mode 100644 index 000000000..d2d9f5de1 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelRecA.lean @@ -0,0 +1,187 @@ +/- +P3 (AArch64, delete hooks W0; Architect freeze E1/E2/E7, 2026-12-03 04:50Z): **the record the deletion loop carries**. + +`P3/CnodeDelWA`'s `DelJWA` pins the ready / release queue ghosts and the hardware root (`JAtZAX` at `Z` but for `G`), +which the finalisers change. So the delete hooks are stated at: + +* **`DelXA`** (E2; Option T, Architect 08:39Z): the per-step record `JStage3TA` (`P3/Stage3TA`: `JStage3A` without A64 + P1's `SysL`, which the deletion's twin top restores from P1's whole-function deletion tops) at the loop's MDB ghost `G`, with the queue ghosts `Q` / `R`, the + hardware root `hw`, the active SCs `Ac`, the IPC buffers `Bf`, the reply `stack` and the view `m` existential, and + `ParentAdjA` (revoke's [J]); +* **`DelJWQA`** (E1): `DelXA` at the entry state's kernel-state base and CPU; +* **`DelRdA`** (E2): what a final cap of a removable remainder leaves behind, for `DeadExtA` at the slot's emptying: a + Reply is in no SC's reply stack and names no TCB; an AsidPool is in no ASID-table entry. `Rv` is S4's VCPU part; +* **`DelRzA`** (E2): the TCB of a Thread cap (about to become a zombie) has no VCPU. + +`IpcHeapCovA` and `ZombieTcbNoVcpuA` are the record's D1 clauses (`P3/Stage3FieldsA`); `DelXA` still states the +latter as a conjunct, and A64 P1's `BufHold` (G3; at loop entry part of P1's `BufInv`), and P1's `CtOff` (at loop entry `SysL.ct`). + +**[P] `DelArchA`** (E7): the arch facts the finalisers need beyond `ArchKeepsA` (TLB and cache maintenance) and +`ArchSwFA.execThread` (the store side of `set_vm_root`); and the IRQ cleanup's `mask_irq` / `irq_free` keeps; (record edit #6) the clock across `vcpu_flush` / +`vcpu_flush_state`. + +No existing statement changed (the two clause definitions moved to `P3/Stage3FieldsA`, unchanged). +-/ +import Fl4ProofsA64.P3.DelHookKindA +import Fl4ProofsA64.P3.CnodeDelWA +import Fl4ProofsA64.P3.CnodeRevokeA +import Fl4ProofsA64.P3.Stage3TA +import Fl4ProofsA64.P3.DelCtOffA +import Fl4ProofsA64.P3.Stage3Rec5A +import Fl4ProofsA64.P3.VsMapJA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 + +noncomputable section + +/-- **[P] The arch facts of the finalisers beyond `ArchKeepsA`** (module docstring): `fpu_release` keeps the arch +relation, and `set_vm_root` leaves it at some hardware root. -/ +structure DelArchA {S A : Type} (st : machine.Store S) (ar : machine.Arch A) (AR : A → HwRootA → Prop) + (Clock : A → Fl4SpecA64.MachineState → Prop) : Prop where + fpuRelease : ∀ a h (t : U64), AR a h → ar.fpu_release a t ⦃ a' => AR a' h ⦄ + vmRoot : ∀ s a h (t : U64), AR a h → vspace.set_vm_root st ar s a t ⦃ r => ∃ h', AR r.2 h' ⦄ + /-- (§36/§39) The clock relation of the preemption point (`DelPpTimeA`) is kept by the TLB / cache maintenance + steps (`ArchKeepsA`'s), -/ + clockK : ∀ ms, ArchKeepsA ar (fun a _ => Clock a ms) + /-- by `fpu_release`, -/ + clockFpu : ∀ a ms (t : U64), Clock a ms → ar.fpu_release a t ⦃ a' => Clock a' ms ⦄ + /-- by `set_vm_root`, -/ + clockVm : ∀ s a ms (t : U64), Clock a ms → vspace.set_vm_root st ar s a t ⦃ r => Clock r.2 ms ⦄ + /-- and reads only the machine's time fields (`MachPlatCA.frame`'s shape). -/ + clockFr : ∀ a ms ms', Clock a ms → ms'.timeState = ms.timeState → ms'.lastMachineTime = ms.lastMachineTime → + Clock a ms' + /-- [P] (Architect, 2026-12-04) The IRQ cleanup's `mask_irq` and `irq_free` keep the arch relation -/ + arIrqMask : ∀ a h (i : U64) (b : Bool), AR a h → ar.mask_irq a i b ⦃ a' => AR a' h ⦄ + arIrqFree : ∀ a h (i : U64), AR a h → ar.irq_free a i ⦃ a' => AR a' h ⦄ + /-- [P] and the clock relation. -/ + clockIrqMask : ∀ a ms (i : U64) (b : Bool), Clock a ms → ar.mask_irq a i b ⦃ a' => Clock a' ms ⦄ + clockIrqFree : ∀ a ms (i : U64), Clock a ms → ar.irq_free a i ⦃ a' => Clock a' ms ⦄ + /-- [P] (Architect, 2026-12-04) The VCPU flush operations keep the clock relation. -/ + clockVcpuFlush : ∀ a ms (vc : U64), Clock a ms → ar.vcpu_flush a vc ⦃ a' => Clock a' ms ⦄ + clockVcpuFlushState : ∀ a ms (vc om : U64), Clock a ms → ar.vcpu_flush_state a vc om ⦃ a' => Clock a' ms ⦄ + +section +variable {S : Type} (st : machine.Store S) [L : LawfulStore st] {A : Type} + +/-- `BufHold` across a step that keeps every slot's cap. -/ +theorem bufHold_sameA {H : Heap} {OS : Set U64} {s r : S} (h : Fl4ProofsA64.P1.W7.R.ST.BufHold st H OS s) + (k : ∀ y ∈ OS, Fl4ProofsA64.P1.W7.R.ST.capA st r y = Fl4ProofsA64.P1.W7.R.ST.capA st s y) : + Fl4ProofsA64.P1.W7.R.ST.BufHold st H OS r := fun y hy hb => by rw [k y hy]; exact h y hy hb + +/-- **The record's slot-cap fields across the loop** (record edit #5, `fhOK` and `ptCapIn`): every TCB's fault-handler +slot holds `Null` or an endpoint cap, and every mapped page-table cap names a page table of the ghost. `fhSlot` is +static. -/ +structure DelCapFxA (Z : XSetsA) (H : Heap) (s : S) : Prop where + fhSlot : ∀ t ∈ Z.T, IsSlotA Z.th (slotIdx t layout.tcb.SLOT_FAULT_HANDLER) + fhOK : ∀ t ∈ Z.T, NullOrEp H (Fl4ProofsA64.P1.W7.R.ST.capA st s (slotIdx t layout.tcb.SLOT_FAULT_HANDLER)) + ptCapIn : PtCapInA Z (L.mem s) + +/-- A slot whose cap is neither `Null` nor an endpoint is no fault-handler slot. -/ +theorem DelCapFxA.ne_fhA {Z : XSetsA} {H : Heap} {s : S} (h : DelCapFxA st Z H s) {y : U64} + (c : ¬ NullOrEp H (Fl4ProofsA64.P1.W7.R.ST.capA st s y)) : + ∀ t ∈ Z.T, y ≠ slotIdx t layout.tcb.SLOT_FAULT_HANDLER := + fun t ht e => c (e ▸ h.fhOK t ht) + +/-- The base of an object of `th` is no TCB's fault-handler slot. -/ +theorem base_ne_fhA {th : TypedHeapA} {kb p t : U64} {k : ObjKindA} (P : PlaceA th kb) (ht : th t = some .tcb) + (hp : th p = some k) : p ≠ slotIdx t layout.tcb.SLOT_FAULT_HANDLER := by + intro e + have hf := P.fit t _ ht + have hsz : ObjKindA.tcb.size = 4096 := by first | rfl | decide | simp [ObjKindA.size] + rw [hsz] at hf + have e3 : layout.tcb.SLOT_FAULT_HANDLER.val = 3 := by simp [layout.tcb.SLOT_FAULT_HANDLER] + have hv : (slotIdx t layout.tcb.SLOT_FAULT_HANDLER).val = t.val + 96 := by + rw [slotIdx_valm t _ (by rw [e3]; omega), e3] + exact Nat.mod_eq_of_lt (by omega) + have hpv : p.val = t.val + 96 := by rw [e]; exact hv + have ne : p ≠ t := fun q => by rw [q] at hpv; omega + rcases P.apart p t _ _ hp ht ne with a | a + · omega + · rw [hsz] at a; omega + +/-- `DelCapFxA` across a step in which a typed slot's cap is kept, nulled, or (off the fault-handler slots) replaced by +a cap some typed slot held or by no mapped page-table cap. -/ +theorem DelCapFxA.stepA {Z : XSetsA} {H : Heap} {s r : S} (h : DelCapFxA st Z H s) + (e : ∀ y, IsSlotA Z.th y → + Fl4ProofsA64.P1.W7.R.ST.capA st r y = Fl4ProofsA64.P1.W7.R.ST.capA st s y ∨ + Fl4ProofsA64.P1.W7.R.ST.capA st r y = .Null ∨ + ((∀ t ∈ Z.T, y ≠ slotIdx t layout.tcb.SLOT_FAULT_HANDLER) ∧ + ((∃ z, IsSlotA Z.th z ∧ + Fl4ProofsA64.P1.W7.R.ST.capA st r y = Fl4ProofsA64.P1.W7.R.ST.capA st s z) ∨ + ∀ ptr asid va, Fl4ProofsA64.P1.W7.R.ST.capA st r y ≠ .PageTable ptr true asid va))) : + DelCapFxA st Z H r where + fhSlot := h.fhSlot + fhOK := fun t ht => by + rcases e _ (h.fhSlot t ht) with k | k | ⟨k, -⟩ + · rw [k]; exact h.fhOK t ht + · rw [k]; exact Or.inl rfl + · exact absurd rfl (k t ht) + ptCapIn := fun y hy ptr asid va c => by + rw [capAtA_eq] at c + rcases e y hy with k | k | ⟨-, ⟨z, hz, k⟩ | k⟩ + · exact h.ptCapIn y hy ptr asid va (by rw [capAtA_eq, ← k]; exact c) + · rw [k] at c; cases c + · exact h.ptCapIn z hz ptr asid va (by rw [capAtA_eq, ← k]; exact c) + · exact absurd c (k ptr asid va) + +/-- `DelCapFxA` across a step that keeps every `OS` slot's cap. -/ +theorem DelCapFxA.keepA {Z : XSetsA} {H : Heap} {OS : Set U64} {s r : S} (h : DelCapFxA st Z H s) + (sub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (k : ∀ y ∈ OS, Fl4ProofsA64.P1.W7.R.ST.capA st r y = Fl4ProofsA64.P1.W7.R.ST.capA st s y) : + DelCapFxA st Z H r := + h.stepA st fun y hy => Or.inl (k y (sub y hy)) + +/-- **The deletion loop's record** (module docstring). -/ +def DelXA (AR : A → HwRootA → Prop) (Z : XSetsA) (H : Heap) (OS U Pp Tb : Set U64) (Lv : U64 → Nat) (ia ie : Nat) + (N E Rp : Set U64) (κ : U64 → U64) (n : Nat) (m : Fl4SpecA64.McState) (J5 J7 : JSlotA S) : DelWA S A := + fun s a σ G => + (∃ (Q : U64 → U64 → List U64) (R : U64 → List U64) (hw : HwRootA) (Ac : Set U64) + (Bf : Set (U64 × Fl4SpecA64.Word)) (stack : U64 → List U64), + JStage3TA st AR { Z with G := G, Q := Q, R := R, hw := hw } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 + s a σ m) ∧ + ParentAdjA st G.chains G.par s ∧ ZombieTcbNoVcpuA st OS s ∧ + Fl4ProofsA64.P1.W7.R.ST.BufHold st H OS s ∧ Fl4ProofsA64.P1.W7.R.ST.CtOff st (L.ksb s) OS s ∧ + DelCapFxA st Z H s + +/-- **The loop invariant at the relation, queues and hardware root free** (module docstring). -/ +def DelJWQA (s0 : S) (X : DelWA S A) : DelWA S A := fun s a σ G => SameCoreA st s0 s ∧ X s a σ G + +/-- **`Rd`**: what the finalisation of a final Reply / AsidPool cap leaves (module docstring; `Rv` is S4's). -/ +def DelRdA (Rv : S → A → Fl4SpecA64.State Unit → MdbGhostA → U64 → Prop) : + S → A → Fl4SpecA64.State Unit → MdbGhostA → U64 → Prop := fun s a σ G x => + (∀ r g, capA st s x = .Reply r g → isFinalCap' (absCapA (capA st s x)) σ → + (∀ p y k, σ.kheap p = some (.schedContext y k) → specPtr r ∉ y.scReplies) ∧ L.mem s (Endp.rTcbA r) = 0#u64) ∧ + (∀ p base, capA st s x = .AsidPool p base → isFinalCap' (absCapA (capA st s x)) σ → + ∀ i, σ.archState.armAsidTable i ≠ some (specPtr p)) ∧ + (∀ v, capA st s x = .Vcpu v → Rv s a σ G x) + +/-- **`Rz`**: the TCB of a Thread slot has no VCPU (module docstring). -/ +def DelRzA : S → A → Fl4SpecA64.State Unit → MdbGhostA → U64 → Prop := fun s _ _ _ x => + ∀ t, capA st s x = .Thread t → L.mem s (core.num.U64.wrapping_add t 3664#u64) = 0#u64 + +variable {st} + +/-- A slot whose cap is none of Reply, AsidPool, VCPU is ready. -/ +theorem delRdA_other {Rv : S → A → Fl4SpecA64.State Unit → MdbGhostA → U64 → Prop} {s : S} {a : A} + {σ : Fl4SpecA64.State Unit} {G : MdbGhostA} {x : U64} + (h1 : ∀ r g, capA st s x ≠ .Reply r g) (h2 : ∀ p base, capA st s x ≠ .AsidPool p base) + (h3 : ∀ v, capA st s x ≠ .Vcpu v) : DelRdA st Rv s a σ G x := + ⟨fun r g h => absurd h (h1 r g), fun p base h => absurd h (h2 p base), fun v h => absurd h (h3 v)⟩ + +/-- **`DelRdA` is `RdTrivHypA`** at every `W`: the caps the loop finalises itself are of none of those kinds. -/ +theorem rdTrivHyp_delRdA {th : TypedHeapA} {OS : Set U64} {W : DelWA S A} + {Rv : S → A → Fl4SpecA64.State Unit → MdbGhostA → U64 → Prop} : + RdTrivHypA st th OS W (DelRdA st Rv) := by + intro s a σ G sl c _ _ _ hc _ hk _ _ + refine delRdA_other (fun r g h => ?_) (fun p base h => ?_) (fun v h => ?_) <;> + (rw [hc] at h; subst h; exact hk trivial) + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelRestoreA.lean b/proofs/Fl4ProofsA64/P3/DelRestoreA.lean new file mode 100644 index 000000000..892620d35 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelRestoreA.lean @@ -0,0 +1,54 @@ +/- +P3 (AArch64, delete hooks E4, Architect ruling E2′ / Option P, condition 2): **`empty_slot` restores R2 over every +slot** from the slot-pinned `Rd`: once the pending slot holds a capability R2 does not read, the VSpace words are kept +and the other slots keep their capabilities (or hold ones R2 does not read), `J7R2A` holds again (A64 P1's +`S1aDelPn.restore`). `J7LA.restoreA` is the same on the whole J7 slot, given how its other fields move. +Named premises: none. New module. +-/ +import Fl4ProofsA64.P3.DelRdXA +import Fl4ProofsA64.P1.S1aR2Loop +import Fl4ProofsA64.P1.S1aKtUtArms +import Fl4ProofsA64.P1.S1aKtLvCol +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + +/-- **R2 over every slot, from R2 over the other slots** (module docstring). -/ +theorem J7R2xA.restoreA {Z Z' : XSetsA} {H : Heap} {OS Pt : Set U64} {ipa x : U64} {s r : S} {σ σ' : State Unit} + {m m' : McState} (h : J7R2xA st ar ipa x Pt Z H OS s σ m) (base : L.ksb r = L.ksb s) + (mem : VsKeep st (L.ksb s) Pt s r) (other : ∀ y ∈ OS, y ≠ x → S1aCapSame st s r y) + (unread : ¬ S1aR2Reads (capA st r x)) + (ut : ∀ y ∈ OS, capA st r y = capA st s y ∨ ∀ p b i d, capA st r y ≠ .Untyped p b i d) + (roots : ∀ y ∈ OS, ∀ R m a, capA st r y = .HighVSpace R m a → R ∈ HvRt st (OS \ {x}) s) : + J7R2A st ar ipa Pt Z' H OS r σ' m' := by + obtain ⟨Pp, Tb, Te, Kt, Lv, Re, bits, pending, sep, KLv, ktK, hik⟩ := h + have hik' : S1aHiK st OS Kt KLv r := S1aHiK.keep hik ⟨EdgeKeepOn.ofVsKeep pending mem, roots⟩ + exact ⟨Pp, Tb, _, Te, Kt, Lv, Re, bits, S1aDelPn.restore pending base mem other unread, sep.slots ut, + KLv, ktK, hik'⟩ + +/-- **J7 restored** (module docstring): `keep` moves the fields other than the R2 slot to the new state. -/ +theorem J7LA.restoreA {Z Z' : XSetsA} {H : Heap} {OS : Set U64} {ipa x : U64} {s r : S} {σ σ' : State Unit} + {m m' : McState} (J : J7LA st (J7R2xA st ar ipa x) Z H OS s σ m) + (keep : ∀ R' : Set U64 → JSlotA S, (∀ Pt, J7R2xA st ar ipa x Pt Z H OS s σ m → + (∀ Ub : Set U64, VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt → R' Pt Z' H OS r σ' m')) → + J7LA st R' Z' H OS r σ' m') + (base : L.ksb r = L.ksb s) + (mem : ∀ Pt Ub : Set U64, VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt → + VsKeep st (L.ksb s) Pt s r) + (other : ∀ y ∈ OS, y ≠ x → S1aCapSame st s r y) (unread : ¬ S1aR2Reads (capA st r x)) + (ut : ∀ y ∈ OS, capA st r y = capA st s y ∨ ∀ p b i d, capA st r y ≠ .Untyped p b i d) + (roots : ∀ y ∈ OS, ∀ R m a, capA st r y = .HighVSpace R m a → R ∈ HvRt st (OS \ {x}) s) : + J7LA st (J7R2A st ar ipa) Z' H OS r σ' m' := + keep _ fun Pt h Ub vr => h.restoreA base (mem Pt Ub vr) other unread ut roots + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelRootA.lean b/proofs/Fl4ProofsA64/P3/DelRootA.lean new file mode 100644 index 000000000..c4d6f347d --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelRootA.lean @@ -0,0 +1,153 @@ +/- +P3 (AArch64, delete hooks W2): **`delete_asid` at the loop record** and the VSpace-root finaliser hooks built on it. + +* `VsSpecA.of_poolVmA`, `ClrViewA.of_poolA`: the spec's `delete_asid` outcome (one pool entry removed, up to `VmA`) is a + `VsSpecA` / `ClrViewA` step; +* `poolZeroR2A`: A64 P1's R2 part of `J7` across FL4's zero of an active pool word (`S1aR2.clearing`); +* **`delete_asid_recA`**: `vspace.delete_asid` at `JStage3TA` (the unified record at the loop's ghosts, a new hardware + root), `DStateA`, `ParentAdjA`, `ZombieTcbNoVcpuA` and `Std6KeepA` (`Std6GA.vsZeroA`). + +Named premises as `P3/FinHookPtA`'s (`BufOffTabA` [J] through `hbf`, `ArchKeepsA`, `ArchPlat`, `DelArchA` [P]). New module. +-/ +import Fl4ProofsA64.P3.DelPoolKeepA +import Fl4ProofsA64.P3.FinHookFrameA +import Fl4ProofsA64.P1.S1aR2FinRoot +import Fl4ProofsA64.P3.DelRec6A +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-- `delete_asid`'s spec step (one pool entry replaced, then `VmA`) is a VSpace-word step. -/ +theorem VsSpecA.of_poolVmA {σ σ' : State Unit} {q : ObjRef} {f f' : AsidPool} + (hk : σ.kheap q = some (.archObj (.asidPool f))) + (hvm : VmA (Sched.updKSc σ q (.archObj (.asidPool f'))) σ') : VsSpecA σ σ' := by + obtain ⟨mr, am, vt, nv, rfl⟩ := hvm + refine ⟨rfl, fun p => ?_, fun _ _ h => h⟩ + have n1 : ∀ v, ArchKernelObj.asidPool f ≠ ArchKernelObj.vcpu v := fun v h => by cases h + have n2 : ∀ v, ArchKernelObj.asidPool f' ≠ ArchKernelObj.vcpu v := fun v h => by cases h + by_cases e : p = q + · subst e + exact Or.inr ⟨ArchKernelObj.asidPool f, ArchKernelObj.asidPool f', hk, by simp [Sched.updKSc], n1, n2⟩ + · exact Or.inl (by simp [Sched.updKSc, e]) + +theorem ClrViewA.of_poolVmA {σ σ' : State Unit} {q : ObjRef} {f f' : AsidPool} + (hk : σ.kheap q = some (.archObj (.asidPool f))) + (hvm : VmA (Sched.updKSc σ q (.archObj (.asidPool f'))) σ') : ClrViewA σ σ' := by + obtain ⟨mr, am, vt, nv, rfl⟩ := hvm + exact ClrViewA.of_poolA (f := f) hk fun p e => by simp [Sched.updKSc, e] + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Pp Tb : Set U64} {Lv : U64 → Nat} {ia ie : Nat} + {N E Rp : Set U64} {κ : U64 → U64} {n : Nat} + +/-- **A64 P1's R2 across FL4's zero of an active pool word** (module docstring). -/ +theorem poolZeroR2A {Ub Pt Pp2 Tb2 Hr Te Kt : Set U64} {Lv2 Re bits : U64 → Nat} {n' : U64} {s : S} {V : Set U64} + {p i : U64} (vr : VrOK H.omega H.frame H.scs H.scBits OS V Ub (L.ksb s) Pt) + (R2 : S1aR2 st H OS Pt Pp2 Tb2 Hr Te Kt Lv2 Re bits n' s) (heap : HeapOK H) (kinds : KindA st OS s) + (hpS : p ∈ PoolSet st Pp2 s) (hi : i.val < 512) (hNA : ¬ AsW (L.ksb s) (peAF p i)) (osk : OSKeep st OS s (L.wr s (peAF p i) 0#u64)) : + HrW st Hr s (L.wr s (peAF p i) 0#u64) ∧ + S1aR2 st H OS Pt (PoolSet st Pp2 s) Tb2 (HvRt st OS (L.wr s (peAF p i) 0#u64)) Te Kt Lv2 Re bits n' (L.wr s (peAF p i) 0#u64) := by + have pw : PtW (PoolSet st Pp2 s) (peAF p i) := ⟨p, hpS, core.num.U64.wrapping_mul i 8#u64, by rw [wm8_valA hi]; omega, + by rw [wm8_valA hi]; omega, rfl⟩ + have write : Wr0 st (PoolSet st Pp2 s) s (L.wr s (peAF p i) 0#u64) := Or.inr ⟨_, pw, rfl⟩ + have inside : PoolSet st Pp2 s ⊆ Pt := fun _ hp => R2.place.pools hp + have hal : Aligned (peAF p i) := vr.al _ (Or.inl (ptw_mono inside pw)) + have headers : HrW st Hr s (L.wr s (peAF p i) 0#u64) := by + refine Wr0.hrw (fun b hb => vr.al b (Or.inl (ptw_mono inside hb))) + (fun h hh => vr.al _ (Or.inl ⟨h, R2.place.tables (R2.place.high.sub hh), 4088#u64, by decide, by decide, rfl⟩)) + (fun b hb h hh => ?_) write + obtain ⟨p', hp', o, ho, al, rfl⟩ := hb + exact R2.place.high.pp p' hp' o ho al h hh + have ht := tab_of_w vr (writesIn_wr (v := 0#u64) hal) (fun b hb => hb ▸ hNA) + exact ⟨headers, S1aR2.clearing (S1aR2.activePools R2) heap kinds R2.place ⟨L.ksb_wr _ _ _, ht⟩ + (VsZ.wr0 vr.al hal) headers (fun y hy => osk.capA hy)⟩ + +variable {Ac : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} + +/-- **`vspace.delete_asid` at the loop record** (module docstring). -/ +theorem delete_asid_recA {ar : machine.Arch A} (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) (K : ArchKeepsA ar AR) + {Clock : A → Fl4SpecA64.MachineState → Prop} (Da : DelArchA st ar AR Clock) {s : S} {a : A} {σ : State Unit} {m : McState} + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7LA st (J7R2A st ar hPl.ipa)) s a σ m) + (D : DStateA st Z.th OS s σ Z.G) (hPA : ParentAdjA st Z.G.chains Z.G.par s) (hZT : ZombieTcbNoVcpuA st OS s) + {asid root ptr : U64} (h16 : asid.val < 2 ^ 16) (hroot : untagA root = ptr) + (hsplit : ∀ p ∈ Z.Pp, ActivePoolA (L.mem s) (L.ksb s) p → ∀ w, L.mem s (peAF p (asid &&& 511#u64)) = w → + w ≠ 0#u64 → untagA w = ptr → w = root) + (hbf : ∀ p ∈ Z.Pp, ∀ i : U64, i.val < 512 → ¬ BufW Bf (peAF p i)) + (hNA : ∀ p ∈ Z.Pp, ∀ i : U64, i.val < 512 → ¬ AsW (L.ksb s) (peAF p i)) : + vspace.delete_asid st ar s a asid root ⦃ r => ∃ σ' hw, + ((), σ') ∈ (deleteAsid (Z := Unit) (asidOfA asid) (specPtr ptr) σ).1 ∧ + DStateA st Z.th OS r.1 σ' Z.G ∧ (∀ y ∈ OS, capA st r.1 y = capA st s y) ∧ + JStage3TA st AR { Z with hw := hw } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7LA st (J7R2A st ar hPl.ipa)) r.1 r.2 σ' m ∧ + ParentAdjA st Z.G.chains Z.G.par r.1 ∧ ZombieTcbNoVcpuA st OS r.1 ∧ + L.ksb r.1 = L.ksb s ∧ L.cpu r.1 = L.cpu s ∧ + (∀ Pt Ub : Set U64, VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt → + (∀ q, SchedPageA st Z OS s q → q ∈ Pt) → VsZ st (L.ksb s) Pt s r.1) ∧ + (∀ Rj' : Set U64 → JSlotA S, (∀ Pt Ub : Set U64, VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt → + (∀ q, SchedPageA st Z OS s q → q ∈ Pt) → J7R2A st ar hPl.ipa Pt Z H OS s σ m → Rj' Pt { Z with hw := hw } H OS r.1 σ' m) → + J7LA st Rj' { Z with hw := hw } H OS r.1 σ' m) ∧ Std6KeepA st Z H OS Bf s σ r.1 σ' ⦄ := by + obtain ⟨x, hcur⟩ := J.sinv.curTcb + refine WP.spec_mono (delete_asid_jA hA K Da J.toJEntTA D.sub h16 hroot hcur hsplit) ?_ + rintro ⟨s1, a1⟩ ⟨σ', hw', hmem, jx, hform⟩ + dsimp only at jx hform ⊢ + rcases hform with ⟨e, eσ⟩ | ⟨p, hp, hact, f, f', hk, e, hvm⟩ + · subst e + subst eσ + exact ⟨_, hw', hmem, D, fun _ _ => rfl, J.of_hwA jx.1.ar (fun _ h => h), hPA, hZT, rfl, rfl, fun _ _ _ _ => VsZ.refl _, fun Rj' h => by + obtain ⟨Pt, Ub, vr, pn, arm, capc, vc, df, hicap, link, vcks, fu, ual, vcu, R⟩ := J.j7 + exact ⟨Pt, Ub, vr, pn, arm, capc, vc, df, hicap, link, vcks, fu, ual, vcu, h Pt Ub vr link R⟩, + fun {_} _ h => h⟩ + · subst e + have hlov : (asid &&& 511#u64).val = asid.val % 512 := by + rw [and_maskA asid 9 _ rfl]; rfl + have hi : (asid &&& 511#u64).val < 512 := by omega + have hpS : SchedPageA st Z OS s p := Or.inl hp + obtain ⟨Pt, Ub, vr, -, -, -, -, -, -, link, -, -⟩ := J.j7 + have hal : Aligned (peAF p (asid &&& 511#u64)) := vr.al _ (vsW_pageA link hpS hi) + have osk : OSKeep st OS s (L.wr s (peAF p (asid &&& 511#u64)) 0#u64) := + OSKeep.of_in J.ent.os (writesIn_wr hal) fun x hx b hb h1 h2 hF => + vr.os b (hF ▸ vsW_pageA link hpS hi) ⟨x, hx, h1, h2⟩ + have pool2 : ∀ {Pt2 Pp2 : Set U64} {Tb2 Hr Te Kt : Set U64} {Lv2 Re bits : U64 → Nat} {n' : U64}, + S1aR2 st H OS Pt2 Pp2 Tb2 Hr Te Kt Lv2 Re bits n' s → p ∈ PoolSet st Pp2 s := by + intro Pt2 Pp2 Tb2 Hr Te Kt Lv2 Re bits n' R2 + obtain ⟨hpne, hi', hlt, htab⟩ := hact + have h127 : (hi' &&& 127#u64) = hi' := UScalar.eq_of_val_eq (by + rw [and_maskA hi' 7 _ rfl]; exact Nat.mod_eq_of_lt (by omega)) + have hat : wa (L.ksb s) (AtOff hi') = atAF (L.ksb s) hi' := by + unfold AtOff atAF; rw [h127] + have word : L.mem s (wa (L.ksb s) (AtOff hi')) = p := by rw [hat]; exact htab + exact ⟨(R2.poolCaps.present hi' hlt p word hpne).1, AtOff hi', (AtOff.v hi').1, (AtOff.v hi').2.1, + (AtOff.v hi').2.2, word, hpne⟩ + have R' : ∀ Pt' Ub' : Set U64, VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub' (L.ksb s) Pt' → + (∀ q, SchedPageA st Z OS s q → q ∈ Pt') → J7R2A st ar hPl.ipa Pt' Z H OS s σ m → + J7R2A st ar hPl.ipa Pt' Z H OS (L.wr s (peAF p (asid &&& 511#u64)) 0#u64) σ' m := by + rintro Pt' Ub' vr' link' ⟨Pp2, Tb2, Hr, Te, Kt, Lv2, Re, bits, R2, sep, KLv, ktK, hik⟩ + exact ⟨_, Tb2, _, Te, Kt, Lv2, Re, bits, + (poolZeroR2A vr' R2 J.ent.heap D.kind (pool2 R2) hi (hNA p hp _ hi) osk).2, sep.keep osk, KLv, ktK, + S1aHiK.keep hik ⟨EdgeKeepOn.ofVsZ R2 (VsZ.wr0 vr'.al (vr'.al _ (vsW_pageA link' hpS hi))), + fun y hy R m a c => ⟨y, hy, m, a, by rwa [osk.capA hy] at c⟩⟩⟩ + have hdr : ∀ y ∈ OS, ∀ q mp ad, capA st s y = .HighVSpace q mp ad → + L.mem (L.wr s (peAF p (asid &&& 511#u64)) 0#u64) (wa q 4088#u64) = L.mem s (wa q 4088#u64) := by + intro y hy q mq ad hq + obtain ⟨Pt1, Ub1, vr1, -, -, -, -, -, -, -, -, -, -, -, ⟨Pp2, Tb2, Hr, Te, Kt, Lv2, Re, bits, R2, -⟩⟩ := J.j7 + exact (poolZeroR2A vr1 R2 J.ent.heap D.kind (pool2 R2) hi (hNA p hp _ hi) osk).1 q + (by rw [R2.highRoots]; exact ⟨y, hy, mq, ad, hq⟩) + obtain ⟨D', J', hPA', hZT', osk', J7f⟩ := delVs_zeroKeepHwA J D hPA hZT hpS hi (hNA p hp _ hi) (hbf p hp _ hi) jx + (VsSpecA.of_poolVmA hk hvm) (ClrViewA.of_poolVmA hk hvm) hdr R' + exact ⟨σ', hw', hmem, D', fun y hy => osk'.capA hy, J', hPA', hZT', L.ksb_wr _ _ _, L.cpu_wr _ _ _, + fun _ _ vr link => VsZ.wr0 vr.al (vr.al _ (vsW_pageA link hpS hi)), J7f, + fun {_} sub h => Std6GA.vsZeroA J h sub (fun _ _ _ link => vsW_pageA link hpS hi) (VsSpecA.of_poolVmA hk hvm) + (fun _ _ _ h' => (ClrViewA.of_poolVmA hk hvm).data h')⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelRvVrA.lean b/proofs/Fl4ProofsA64/P3/DelRvVrA.lean new file mode 100644 index 000000000..b410c1d11 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelRvVrA.lean @@ -0,0 +1,149 @@ +/- +P3 (AArch64, delete hooks): **record edit #4's per-hook facts**: `vr` across a finaliser that keeps the stacked +replies' callers blocked on reply (`vr_specA`, by `ReplyInv.reply` at the new state), and no pending thread switch +across the spec's `cancel_all_ipc` / `cancel_all_signals` (both end in `reschedule_required` unless they do nothing). + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3BA +import Fl4ProofsA64.P3.DelSinvLoopA +import Fl4ProofsA64.P3.IpcCancelA +import Fl4ProofsA64.P3.DelNtfnEntA +import Fl4ProofsA64.P1.QueueFinRv +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Sched + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} [J7PpA st J7] {s s' : S} {a : A} {σ σ' : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} + +/-- **`vr` after a finaliser** (module docstring): the stacked replies' callers keep their spec state and their +`TCB.REPLY` word. -/ +theorem JStage3BA.vr_specA + (J : JStage3BA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {Ac' : Set U64} (blk' : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac' s') + {stack' : U64 → List U64} (hsub : ∀ c, ∀ r ∈ stack' c, r ∈ stack c) + (hst : ∀ sc ∈ Z.C, ∀ r ∈ stack' sc, ∀ c ∈ Z.T, c = L.mem s (Fl4ProofsA64.P1.rTcbA r) → + ∀ x q, σ.kheap (specPtr c) = some (.tcb x) → x.tcbState = .blockedOnReply q → + ∃ y, σ'.kheap (specPtr c) = some (.tcb y) ∧ y.tcbState = x.tcbState) + (E' : EpStateRel Z.T E Rp Bf (L.mem s') σ') + (hrep : ∀ c ∈ Z.T, L.mem s' (tReplyA c) = L.mem s (tReplyA c)) : + ∀ sc ∈ Z.C, ∀ r ∈ stack' sc, RBlk st s' r := by + intro sc hc r h + have hr := hsub sc r h + have mR : r ∈ Rp := (J.reply.stackRel sc hc).2.1 r hr + have old := J.vr sc hc r hr + have back := J.blkA.2.2.1.2.1.back r (J.utIpc.reply r mR) old.tcb + obtain ⟨c, ec⟩ : ∃ c, L.mem s (Fl4ProofsA64.P1.rTcbA r) = c := ⟨_, rfl⟩ + have hcH : c ∈ H.tcbs := ec ▸ back.2.1 + have cT : c ∈ Z.T := J.tcbs ▸ hcH + have h5 : (L.mem s (Fl4ProofsA64.P1.stA c)).val = 5 := by + have e := old.blk + rw [ec] at e + rw [e]; unfold sched.TS_BLOCKED_ON_REPLY; rfl + obtain ⟨x, hx, rel⟩ := J.ep.tcb c cT + have h5x : Notif.tsCode x.tcbState = 5 := by rw [← rel.st]; exact h5 + obtain ⟨q, hq⟩ := tsCode_five h5x + obtain ⟨y, hy, ey⟩ := hst sc hc r h c cT ec.symm x q hx hq + obtain ⟨y', hy', rel'⟩ := E'.tcb c cT + rw [hy] at hy' + cases hy' + have e5 : L.mem s' (Fl4ProofsA64.P1.stA c) = sched.TS_BLOCKED_ON_REPLY := by + apply UScalar.eq_of_val_eq + have : (L.mem s' (Fl4ProofsA64.P1.stA c)).val = 5 := by + have := rel'.st + rw [ey, h5x] at this + exact this + rw [this]; unfold sched.TS_BLOCKED_ON_REPLY; rfl + have R' := blk'.2.2.1.2.1.reply c hcH e5 + have eA : Fl4ProofsA64.P1.replyA c = tReplyA c := by + unfold Fl4ProofsA64.P1.replyA tReplyA; rfl + have er : L.mem s' (Fl4ProofsA64.P1.replyA c) = r := by + rw [eA, hrep c cT, ← eA, ← ec] + exact back.2.2.1 + have et : L.mem s' (Fl4ProofsA64.P1.rTcbA r) = c := by + have := R'.2 + rwa [er] at this + exact ⟨by rw [et, ← ec]; exact old.tcb, by rw [et]; exact e5⟩ + +/-- `JStage3BA.vr_specA` at `JStage3TA`. -/ +theorem JStage3TA.vr_specA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {Ac' : Set U64} (blk' : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac' s') + {stack' : U64 → List U64} (hsub : ∀ c, ∀ r ∈ stack' c, r ∈ stack c) + (hst : ∀ sc ∈ Z.C, ∀ r ∈ stack' sc, ∀ c ∈ Z.T, c = L.mem s (Fl4ProofsA64.P1.rTcbA r) → + ∀ x q, σ.kheap (specPtr c) = some (.tcb x) → x.tcbState = .blockedOnReply q → + ∃ y, σ'.kheap (specPtr c) = some (.tcb y) ∧ y.tcbState = x.tcbState) + (E' : EpStateRel Z.T E Rp Bf (L.mem s') σ') + (hrep : ∀ c ∈ Z.T, L.mem s' (tReplyA c) = L.mem s (tReplyA c)) : + ∀ sc ∈ Z.C, ∀ r ∈ stack' sc, RBlk st s' r := + J.toBA.vr_specA blk' hsub hst E' hrep + +end + +/-- No thread switch is pending. -/ +abbrev NoSwA (σ : State Unit) : Prop := ∀ u, σ.schedulerAction ≠ .switchThread u + +theorem cancelAllIpc_noSwA (p : ObjRef) {σ : State Unit} (h : NoSwA σ) {r : Unit × State Unit} + (hr : r ∈ (cancelAllIpc (Z := Unit) p σ).1) : NoSwA r.2 := by + unfold cancelAllIpc at hr + have hg : PresR (@Eq (State Unit)) (getEndpoint (Z := Unit) p) := by + show PresR (@Eq (State Unit)) (getSimpleKo KernelObject.endpoint p) + unfold getSimpleKo + refine PresR.bind (getObject_eqPres p) (fun _ => ?_) + eqpres + obtain ⟨ep, -, hr⟩ := eqPres_mem hg hr + have fin : ∀ (f : NonDet (State Unit) Unit) (τ : State Unit), + r ∈ ((f >>= fun _ => rescheduleRequired (Z := Unit)) τ).1 → NoSwA r.2 := fun f τ hf u c => by + obtain ⟨w, -, hf⟩ := mem_bindA.1 hf + rw [(rescheduleRequired_memA hf).2] at c + cases c + cases ep with + | idleEP => + have : r = ((), σ) := hr + rw [this]; exact h + | sendEP q => + obtain ⟨qq, -, hr⟩ := eqPres_mem (PresR.pure _) hr + exact fin _ _ hr + | recvEP q => + obtain ⟨qq, -, hr⟩ := eqPres_mem (PresR.pure _) hr + exact fin _ _ hr + +theorem cancelAllSignals_noSwA (p : ObjRef) {σ : State Unit} (h : NoSwA σ) {r : Unit × State Unit} + (hr : r ∈ (cancelAllSignals (Z := Unit) p σ).1) : NoSwA r.2 := by + unfold cancelAllSignals at hr + have hg : PresR (@Eq (State Unit)) (getNotification (Z := Unit) p) := by + show PresR (@Eq (State Unit)) (getSimpleKo KernelObject.notification p) + unfold getSimpleKo + refine PresR.bind (getObject_eqPres p) (fun _ => ?_) + eqpres + obtain ⟨x, -, hr⟩ := eqPres_mem hg hr + have idle : r ∈ ((pure () : NonDet (State Unit) Unit) σ).1 → NoSwA r.2 := fun hr => by + have : r = ((), σ) := hr + rw [this]; exact h + cases hx : x.ntfnObj with + | waitingNtfn q => + simp only [hx] at hr + intro u c + obtain ⟨w, -, hr⟩ := mem_bindA.1 hr + rw [(rescheduleRequired_memA hr).2] at c + cases c + | idleNtfn => + simp only [hx] at hr + exact idle hr + | activeNtfn b => + simp only [hx] at hr + exact idle hr + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelSactSlotA.lean b/proofs/Fl4ProofsA64/P3/DelSactSlotA.lean new file mode 100644 index 000000000..28202f373 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelSactSlotA.lean @@ -0,0 +1,29 @@ +/- +P3 (AArch64, delete hooks; entry record edit F1-F2, `SActA`): **`SActA` across a slot-only spec step** +(`SpecSlotA` keeps the scheduler action, both threads and every TCB's state). A leaf below `P3/DelSlotWA`. No named +premise; new module. +-/ +import Fl4ProofsA64.P3.Stage3CapFrameA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open Fl4SpecA64 Fl4Spec + +/-- **`SActA` across a slot-only spec step.** -/ +theorem SActA.slotA {σ σ' : State Unit} (h : SActA σ) (HS : SpecSlotA σ σ') : SActA σ' where + f1 := fun u e => by + rw [HS.act] at e + rw [HS.cur, HS.idle] + exact h.f1 u e + f2 := by + obtain ⟨x, hx, hs⟩ := h.f2 + rw [HS.cur] + rcases HS.kh σ.curThread with e | ⟨r, cs, cs', e1, -⟩ | ⟨y, y', e1, e2, g⟩ + · exact ⟨x, by rw [e]; exact hx, hs⟩ + · rw [hx] at e1; cases e1 + · rw [hx] at e1 + have hxy := KernelObject.tcb.inj (Option.some.inj e1) + subst hxy + exact ⟨y', e2, by rw [(tcbNcA_eq g).1]; exact hs⟩ + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelScChainA.lean b/proofs/Fl4ProofsA64/P3/DelScChainA.lean new file mode 100644 index 000000000..423bc3f97 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelScChainA.lean @@ -0,0 +1,1115 @@ +/- +P3 (AArch64, delete hooks W4, SC finaliser): **the final SC cap's finalisation at the record, scheduler level**: +`finalise_cap_object` of a final SC cap refines the spec's five steps (`sched_context_unbind_all_tcbs`, `_ntfn`, +`_reply`, `_yield_from`, `_set_inactive`), `SchedSRA` and `SInvXA ∧ SIdleA ∧ SActA` hold after, and no switch is +pending. `ScFinEndA` gives the end state's write set, memory values and spec objects; the rebuild of `JStage3TA` +from it is `DelScRecA`. + +Hypotheses at the cap: `hp` (a related SC), `hni` (not the current core's idle SC, the record's `ciSc`). +Premises: [P] `hA`, `hCs`, `hmax`, `htu`; [S] `h1`. New module. +-/ +import Fl4ProofsA64.P3.DelScSpecA +import Fl4ProofsA64.P3.DelScStallA +import Fl4ProofsA64.P3.DelNtfnHookA +import Fl4ProofsA64.P3.DelEpRecA +import Fl4ProofsA64.P3.ScYieldFrameA +import Fl4ProofsA64.P3.NtfnJoinA +import Fl4ProofsA64.P3.Stage3McsFrA +import Fl4ProofsA64.P3.Stage3PreemptTA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 Fl4ProofsA64.P3.Endp +open Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Notif + +local notation "wa" => core.num.U64.wrapping_add + +noncomputable section + +theorem scBindAssocA {α β γ : Type} (r : Result α) (f : α → Result β) (g : β → Result γ) : + Std.bind (Std.bind r f) g = Std.bind r (fun x => Std.bind (f x) g) := by + cases r with + | ret x => simp only [bind_ok] + | vis i k => simp only [bind_vis]; congr 1; funext y; cases i; exact PEmpty.elim y + | div => simp only [bind_div] + +/-- The three unbinding calls as one step before a continuation. -/ +theorem scUnb3_assocA {S A β : Type} (m1 : Result (S × A)) (g h : S → Result S) (f : S → A → Result β) : + Std.bind m1 (fun p => Std.bind (g p.1) fun s2 => Std.bind (h s2) fun s3 => f s3 p.2) = + Std.bind (Std.bind m1 fun p => Std.bind (g p.1) fun s2 => Std.bind (h s2) fun s3 => ok (s3, p.2)) + (fun r => f r.1 r.2) := by + refine Eq.symm ((scBindAssocA _ _ _).trans ?_) + congr 1 + funext p + refine (scBindAssocA _ _ _).trans ?_ + congr 1 + funext s2 + refine (scBindAssocA _ _ _).trans ?_ + congr 1 + funext s3 + exact bind_ok _ _ + +/-- `scUnb3_assocA` as a `⦃⦄` rule, on the `do` form of the bodies. -/ +theorem scUnb3_specA {S A β : Type} {m1 : Result (S × A)} {g h : S → Result S} {f : S → A → Result β} + {P : S × A → Prop} {Q : β → Prop} + (H : (do + let (s1, a1) ← m1 + let s2 ← g s1 + let s3 ← h s2 + ok (s3, a1)) ⦃ r => P r ⦄) + (K : ∀ r, P r → f r.1 r.2 ⦃ x => Q x ⦄) : + (do + let (s1, a1) ← m1 + let s2 ← g s1 + let s3 ← h s2 + f s3 a1) ⦃ x => Q x ⦄ := by + have H' : Std.bind m1 (fun p => Std.bind (g p.1) fun s2 => Std.bind (h s2) fun s3 => ok (s3, p.2)) ⦃ r => P r ⦄ := H + show Std.bind m1 (fun p => Std.bind (g p.1) fun s2 => Std.bind (h s2) fun s3 => f s3 p.2) ⦃ x => Q x ⦄ + rw [scUnb3_assocA] + exact WP.spec_bind H' K + +/-- The spec fields the MCP, yield and state relations read: every TCB's state, MCP and yield target, and every SC's +yielder. -/ +structure ScKeepYA (σ σ' : State Unit) : Prop where + tcb : ∀ p x, σ.kheap p = some (.tcb x) → ∃ y, σ'.kheap p = some (.tcb y) ∧ y.tcbState = x.tcbState ∧ + y.tcbMcpriority = x.tcbMcpriority ∧ y.tcbYieldTo = x.tcbYieldTo ∧ y.tcbFlags = x.tcbFlags + sc : ∀ p x k, σ.kheap p = some (.schedContext x k) → + ∃ y, σ'.kheap p = some (.schedContext y k) ∧ y.scYieldFrom = x.scYieldFrom + +theorem ScKeepYA.refl (σ : State Unit) : ScKeepYA σ σ := + ⟨fun _ x h => ⟨x, h, rfl, rfl, rfl, rfl⟩, fun _ x _ h => ⟨x, h, rfl⟩⟩ + +theorem ScKeepYA.trans {σ1 σ2 σ3 : State Unit} (h1 : ScKeepYA σ1 σ2) (h2 : ScKeepYA σ2 σ3) : ScKeepYA σ1 σ3 := by + refine ⟨fun p x h => ?_, fun p x k h => ?_⟩ + · obtain ⟨y, hy, a, b, c, d⟩ := h1.tcb p x h + obtain ⟨z, hz, a', b', c', d'⟩ := h2.tcb p y hy + exact ⟨z, hz, a'.trans a, b'.trans b, c'.trans c, d'.trans d⟩ + · obtain ⟨y, hy, a⟩ := h1.sc p x k h + obtain ⟨z, hz, a'⟩ := h2.sc p y k hy + exact ⟨z, hz, a'.trans a⟩ + +/-- A notification replaced by a notification. -/ +theorem ScKeepYA.updNtfn {σ : State Unit} {p : ObjRef} {x y : Notification} + (h : σ.kheap p = some (.notification x)) : ScKeepYA σ (updKSc σ p (.notification y)) := by + refine ⟨fun q z hz => ⟨z, ?_, rfl, rfl, rfl, rfl⟩, fun q z k hz => ⟨z, ?_, rfl⟩⟩ <;> + (have e : q ≠ p := fun e => by rw [e, h] at hz; cases hz + rw [updKSc_kheap, Function.update_of_ne e]; exact hz) + +/-- A reply replaced by a reply. -/ +theorem ScKeepYA.updReply {σ : State Unit} {p : ObjRef} {x y : Reply} + (h : σ.kheap p = some (.reply x)) : ScKeepYA σ (updKSc σ p (.reply y)) := by + refine ⟨fun q z hz => ⟨z, ?_, rfl, rfl, rfl, rfl⟩, fun q z k hz => ⟨z, ?_, rfl⟩⟩ <;> + (have e : q ≠ p := fun e => by rw [e, h] at hz; cases hz + rw [updKSc_kheap, Function.update_of_ne e]; exact hz) + +/-- An SC replaced by one with the same yielder. -/ +theorem ScKeepYA.updSc {σ : State Unit} {p : ObjRef} {x y : SchedContext} {k : Nat} + (h : σ.kheap p = some (.schedContext x k)) (e : y.scYieldFrom = x.scYieldFrom) : + ScKeepYA σ (updKSc σ p (.schedContext y k)) := by + refine ⟨fun q z hz => ⟨z, ?_, rfl, rfl, rfl, rfl⟩, fun q z j hz => ?_⟩ + · have e : q ≠ p := fun e => by rw [e, h] at hz; cases hz + rw [updKSc_kheap, Function.update_of_ne e]; exact hz + · by_cases c : q = p + · subst c; rw [h] at hz; cases hz + exact ⟨y, by rw [updKSc_kheap, Function.update_self], e⟩ + · exact ⟨z, by rw [updKSc_kheap, Function.update_of_ne c]; exact hz, rfl⟩ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} [J7PpA st J7] {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- An IPC word is not a TCB word. -/ +theorem JStage3TA.ipc_ne_tcbA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {t K b : U64} (ht : t ∈ Z.T) (hK : K.val < 4096) (h : IpcObjWA N E Rp b) : wa t K ≠ b := by + intro e + have htH : H t = some .tcb := by + have : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + exact this + have hfit := (J.sr.core.tcbs t ht).fits + have hv : (wa t K).val = t.val + K.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact J.ipc_off_hobjA (v := t) (kv := .tcb) htH (by simp) + ⟨by rw [← e, hv]; omega, by rw [← e, hv]; simp only [HKind.bits]; omega⟩ h + +/-- A notification's `SC` word is read by no scheduler relation at a later state of the same SCs and TCBs. -/ +theorem JStage3TA.nScSc_offRead_laterA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {Z' : XSetsA} (eT : Z'.T = Z.T) (eth : Z'.th = Z.th) {s1 : S} {σ1 : State Unit} + (H1 : SchedSRA st Z' m κ s1 σ1) (kb : L.ksb s1 = L.ksb s) {p : U64} (hp : p ∈ N) : + ¬ SReadA st Z' m s1 σ1 (nScSc p) := by + have hna := J.iplaced.n.ntfn p hp + have v : (nScSc p).val = p.val + 32 := by + have e : nScSc p = Notif.nScA p := rfl + rw [e, Notif.nScA_eq, Notif.nfield_val hna (by simp)]; simp + have hw : IpcObjWA N E Rp (nScSc p) := Or.inl ⟨p, hp, by omega, by omega⟩ + have hks := J.ipc_ksA hw + have hth : Z'.th p = some (.other 64) := by rw [eth]; exact J.ipcPlace.notification p hp + have ksoff : ∀ o : U64, o.val < ksSizeA → nScSc p ≠ wa (L.ksb s1) o := by + intro o ho e + have hfit := J.sr.ksFit + have hv : (wa (L.ksb s1) o).val = (L.ksb s).val + o.val := by + rw [kb, wa_valm]; exact Nat.mod_eq_of_lt (by omega) + rw [e, hv] at hks + omega + rintro (⟨c, hc, o, ho, e⟩ | e | e | ⟨t, ht, K, hK, e⟩ | h) + · have := H1.node_in hc ho + rw [← e, kb] at this + omega + · exact ksoff 8#u64 (by decide) e + · exact ksoff 16#u64 (by decide) e + · have k4 : K.val < 4096 := by rcases hK with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> simp + exact J.ipc_ne_tcbA (eT ▸ ht) k4 hw e.symm + · have := H1.scp.th_out h hth + simp only [ObjKindA.size] at this + omega + +/-- **`EntryInvA3` across a step that re-establishes `BlkOK` at a new active SC set** (the SC finaliser deactivates +its SC: P1's `K7.fin_sc.blk`). -/ +theorem JStage3TA.ent_stepAcA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {s' : S} {Ac' : Set U64} (w : WritesIn st s s' (SchedStepNWA (st := st) N E Rp Z m σ s)) + (blk : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac' s') : + EntryInvA3 st H Z.Cs OS U Ac' Pp Tb Lv s' := by + have kb : L.ksb s' = L.ksb s := w.1 + have hvs : VsTyR st (L.ksb s') (PoolSet st Pp s') ∅ Tb Lv s' := by + refine (utVsPt_keepA J.ent.vs kb fun b hb => ?_).1 + have vr := J.vsOk + refine w.mem_eq (vr.al _ hb) ?_ + by_cases h : AsW (L.ksb s) b + · obtain ⟨o, o1, o2, -, e⟩ := h + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.ent.ksw + have ev : b.val = (L.ksb s).val + o.val := by + rw [e, wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact J.asid_offStepNA (by omega) (by omega) + · exact J.vsr_offStepNA vr hb h + obtain ⟨b1, b2, b3, b4, b5⟩ := blk + exact ⟨J.ent.heap, ⟨b1, b2, b3, b5, b4⟩, J.ent.os, J.ent.user.ksb kb, hvs, by rw [kb]; exact J.ent.ksw⟩ + +theorem tcbSchedDeq_kheapA (t : ObjRef) {σ : State Unit} {r : Unit × State Unit} + (hr : r ∈ (tcbSchedAction (Z := Unit) tcbSchedDequeue t σ).1) : r.2.kheap = σ.kheap := by + unfold tcbSchedAction at hr + obtain ⟨d, -, hr⟩ := eqPres_mem (threadGet_eqPres _ t) hr + obtain ⟨pr, -, hr⟩ := eqPres_mem (threadGet_eqPres _ t) hr + have hq : PresR (@Eq (State Unit)) (getTcbQueue (Z := Unit) d pr) := by unfold getTcbQueue; eqpres + obtain ⟨q, -, hr⟩ := eqPres_mem hq hr + obtain ⟨q', hq', hr⟩ := mem_bindA.1 hr + have eq' : q' = (tcbSchedDequeue t q, σ) := hq' + subst eq' + have er : r = ((), { σ with readyQueues := fun d' p => + if d' = d ∧ p = pr then tcbSchedDequeue t q else σ.readyQueues d' p }) := hr + rw [er] + +theorem tcbReleaseRemove_kheapA (t : ObjRef) {σ : State Unit} {r : Unit × State Unit} + (hr : r ∈ (tcbReleaseRemove (Z := Unit) t σ).1) : r.2.kheap = σ.kheap := by + have e : r = ((), releaseRemoveStA σ t) := by rw [tcbReleaseRemove_eqA] at hr; exact hr + rw [e]; rfl + +/-- `sched_context_unbind_all_tcbs` keeps every SC's reply list. -/ +theorem unbindAllTcbs_repA {σ : State Unit} {p : ObjRef} {x : SchedContext} {k : Nat} + (hk : σ.kheap p = some (.schedContext x k)) {r : Unit × State Unit} + (hr : r ∈ (schedContextUnbindAllTcbs (Z := Unit) p σ).1) : + ∀ q y n, σ.kheap q = some (.schedContext y n) → + ∃ y', r.2.kheap q = some (.schedContext y' n) ∧ y'.scReplies = y.scReplies := by + intro q z n hq + unfold schedContextUnbindAllTcbs at hr + rw [nd_bind_oneSc (Sched.getSchedContext_eqA hk)] at hr + unfold Fl4Spec.NonDet.when at hr + split_ifs at hr with hc + swap + · have : r = ((), σ) := hr + rw [this]; exact ⟨z, hq, rfl⟩ + obtain ⟨t, ht⟩ := Option.ne_none_iff_exists'.1 hc.1 + unfold schedContextUnbindTcb at hr + rw [nd_bind_oneSc (Sched.getSchedContext_eqA hk), ht, + nd_bind_oneSc (show Fl4Spec.NonDet.assertOpt (σ := State Unit) (some t) σ = ({(t, σ)}, False) from rfl), + nd_gets_bindSc] at hr + obtain ⟨a, ha, hr⟩ := mem_bindA.1 hr + have A : a.2.kheap = σ.kheap := by + unfold Fl4Spec.NonDet.when at ha + split_ifs at ha + · have e := (rescheduleRequired_memA ha).1.1 + rw [e] + · have : a = ((), σ) := ha + rw [this] + obtain ⟨b, hb, hr⟩ := mem_bindA.1 hr + have B : b.2.kheap = σ.kheap := (tcbSchedDeq_kheapA t hb).trans A + obtain ⟨c, hc', hr⟩ := mem_bindA.1 hr + have C : c.2.kheap = σ.kheap := (tcbReleaseRemove_kheapA t hc').trans B + obtain ⟨d, hd, hr⟩ := mem_bindA.1 hr + unfold setTcbObjRef at hd + obtain ⟨zt, hzt, ez⟩ := threadSet_memA _ _ hd + unfold setScObjRef at hr + obtain ⟨y, j, hy, ey⟩ := updateSchedContext_memA _ _ hr + have D : d.2.kheap q = some (.schedContext z n) := by + have ne : q ≠ t := fun e => by rw [e, ← C, hzt] at hq; cases hq + rw [ez] + change Function.update c.2.kheap t _ q = _ + rw [Function.update_of_ne ne, C]; exact hq + rw [ey] + by_cases e : q = p + · subst e + rw [D] at hy; cases hy + exact ⟨scTcbUpdate (fun _ => none) z, by change Function.update d.2.kheap q _ q = _; rw [Function.update_self], rfl⟩ + · exact ⟨z, by change Function.update d.2.kheap p _ q = _; rw [Function.update_of_ne e]; exact D, rfl⟩ + +/-- `unbind_reply` leaves the head reply's `next` word zero. -/ +theorem sc_unbind_reply_next0A {s : S} {sc : U64} (hsc0 : sc ≠ 0#u64) (hal : Aligned (scReplyA sc)) + (hr : L.mem s (scReplyA sc) ≠ 0#u64 → Aligned (rNextA (L.mem s (scReplyA sc)))) : + schedcontext.unbind_reply st s sc ⦃ s' => L.mem s (scReplyA sc) ≠ 0#u64 → + L.mem s' (rNextA (L.mem s (scReplyA sc))) = 0#u64 ⦄ := by + unfold schedcontext.unbind_reply + refine WP.spec_bind (obj.Sc.is_null.spec _) ?_ + rintro b rfl + rw [if_neg (by simpa using hsc0)] + refine WP.spec_bind (obj.Sc.reply.val s sc) ?_ + rintro w rfl + by_cases hw0 : L.mem s (scReplyA sc) = 0#u64 + · have hb : ¬ ((L.mem s (scReplyA sc) != 0#u64) = true) := by simp [hw0] + rw [if_neg hb, WP.spec_ok] + exact fun h => absurd hw0 h + · have hb : (L.mem s (scReplyA sc) != 0#u64) = true := by simpa using hw0 + rw [if_pos hb] + refine WP.spec_bind (obj.Reply.set_next.val0 s _ 0#u64) ?_ + rintro s1 rfl + refine WP.spec_mono (obj.Sc.set_reply.spec _ sc 0#u64) ?_ + rintro s2 rfl + intro _ + have z : (0#u64 &&& ~~~1#u64) ||| 0#u64 = 0#u64 := by decide + rcases mem_wr_cases (st := st) hal (hr hw0) + (s := L.wr s (rNextA (L.mem s (scReplyA sc))) ((0#u64 &&& ~~~1#u64) ||| 0#u64)) (v := 0#u64) with + ⟨-, e⟩ | ⟨-, e⟩ + · exact e + · rw [e, L.mem_wr_same (hr hw0), z] + +/-- The words the five steps write: the scheduler / SC words, the unbound notification's `SC` word, the head reply's +`next`, and the yielder's reply registers and yield target. -/ +def ScFinWA (st : machine.Store S) [L : LawfulStore st] (Z : XSetsA) (m : Fl4SpecA64.McState) + (σ : Fl4SpecA64.State Unit) (s : S) (q h yf b : U64) : Prop := + MWrA (st := st) Z m σ s b ∨ (q ≠ 0#u64 ∧ b = nScSc q) ∨ (h ≠ 0#u64 ∧ b = rNextA h) ∨ + (yf ≠ 0#u64 ∧ (ScConsumedRegsA yf b ∨ b = yieldA yf)) + +/-- **The end state of the five steps**, against the state `s`/`σ` before them: `q` the unbound notification, `h` the +head reply of the SC's stack, `yf` the yielder, `d` the consumed time it was told. -/ +structure ScFinEndA (st : machine.Store S) [L : LawfulStore st] (Z : XSetsA) (N Rp : Set U64) (m : Fl4SpecA64.McState) + (s s' : S) (σ σ' : Fl4SpecA64.State Unit) (p q h yf : U64) (d : Fl4SpecA64.Data) : Prop where + wr : WritesIn st s s' (ScFinWA st Z m σ s q h yf) + ntf : q ≠ 0#u64 → q ∈ N ∧ L.mem s' (nScSc q) = 0#u64 + rep : h ≠ 0#u64 → h ∈ Rp ∧ L.mem s' (rNextA h) = 0#u64 + head : ∃ x n, σ.kheap (scNameA m p) = some (.schedContext x n) ∧ x.scReplies.head? = optRefS h + yT : yf ≠ 0#u64 → yf ∈ Z.T ∧ L.mem s' (wa yf 256#u64) = 0#u64 ∧ + (L.mem s' (wa yf 264#u64)).bv = Fl4SpecA64.messageInfoToData ⟨Fl4SpecA64.natToLen 1, 0, 0, 0⟩ ∧ + (L.mem s' (wa yf 272#u64)).bv = d + tcb : ∀ r x, σ.kheap r = some (.tcb x) → ∃ y o, σ'.kheap r = some (.tcb y) ∧ + ((yf ≠ 0#u64 ∧ r = specPtr yf → y = { yieldDoneA x d with tcbSchedContext := o }) ∧ + (¬ (yf ≠ 0#u64 ∧ r = specPtr yf) → y = { x with tcbSchedContext := o })) + ep : ∀ r x, σ.kheap r = some (.endpoint x) → σ'.kheap r = some (.endpoint x) + nt : ∀ r x, σ.kheap r = some (.notification x) → ∃ o, σ'.kheap r = some (.notification { x with ntfnSc := o }) ∧ + (q ≠ 0#u64 ∧ r = specPtr q → o = none) ∧ (¬ (q ≠ 0#u64 ∧ r = specPtr q) → o = x.ntfnSc) + re : ∀ r x, σ.kheap r = some (.reply x) → ∃ o, σ'.kheap r = some (.reply { x with replySc := o }) ∧ + (h ≠ 0#u64 ∧ r = specPtr h → o = none) ∧ (¬ (h ≠ 0#u64 ∧ r = specPtr h) → o = x.replySc) + sc : ∀ r x n, σ.kheap r = some (.schedContext x n) → ∃ y, σ'.kheap r = some (.schedContext y n) ∧ + (r = scNameA m p → y.scReplies = []) ∧ (r ≠ scNameA m p → y.scReplies = x.scReplies) + mcp : McpRelA Z.T m (L.mem s') σ' + yield : YieldRelA Z.T Z.C m (L.mem s') σ' + +set_option maxHeartbeats 3200000 in +/-- **The SC's first three unbinding steps** (`unbind_all_tcbs`, `unbind_ntfn`, `unbind_reply`) at the scheduler level: +the finaliser's prefix and the whole body of the `SchedContextUnbind` label. -/ +theorem JStage3TA.scUnb_schedA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {p : U64} (hp : p ∈ Z.C) (hni : specPtr p ≠ m.idleScOf m.curCore) + (hA : P0.ArchTotal ar) (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) : + (do + let (s1, a1) ← schedcontext.unbind_all_tcbs st ar s a p + let s2 ← schedcontext.unbind_ntfn st s1 p + let s3 ← schedcontext.unbind_reply st s2 p + ok (s3, a1)) ⦃ r => r.2 = a ∧ ∃ (s1 s2 : S) (σ1 σ2 σ3 : State Unit) (Q1 : U64 → U64 → List U64) + (R1 : U64 → List U64) (x1 x2 : SchedContext) (n1 : Nat), + MFrA (st := st) Z m s s1 σ σ1 ∧ + schedContextUnbindAllTcbs (Z := Unit) (scNameA m p) σ = ({((), σ1)}, False) ∧ + RStepA m n σ σ1 ∧ σ1.kheap (scNameA m p) = some (.schedContext x1 n1) ∧ + schedContextUnbindNtfn (Z := Unit) (scNameA m p) σ1 = ({((), σ2)}, False) ∧ RefK σ1 σ2 ∧ + σ2.kheap (scNameA m p) = some (.schedContext x2 n1) ∧ + (L.mem s1 (scWA p 40#u64) = 0#u64 ∧ σ2 = σ1 ∨ L.mem s1 (scWA p 40#u64) ≠ 0#u64 ∧ + L.mem s1 (scWA p 40#u64) ∈ N ∧ L.mem s2 (nScSc (L.mem s1 (scWA p 40#u64))) = 0#u64 ∧ + ∃ xn, σ1.kheap (specPtr (L.mem s1 (scWA p 40#u64))) = some (.notification xn) ∧ + σ2 = scNtfnLinkStA σ1 (scNameA m p) (specPtr (L.mem s1 (scWA p 40#u64))) x1 n1 xn false) ∧ + WritesIn st s s2 (fun b => MWrA (st := st) Z m σ s b ∨ + (L.mem s1 (scWA p 40#u64) ≠ 0#u64 ∧ b = nScSc (L.mem s1 (scWA p 40#u64)))) ∧ + L.ksb s2 = L.ksb s ∧ SSzA σ σ2 ∧ + schedContextUnbindReply (Z := Unit) (scNameA m p) σ2 = ({((), σ3)}, False) ∧ + SchedSRA st { Z with Q := Q1, R := R1 } m κ r.1 σ3 ∧ + WritesIn st s2 r.1 (fun b => MWrA (st := st) { Z with Q := Q1, R := R1 } m σ2 s2 b ∨ + (L.mem s2 (scReplyA p) ≠ 0#u64 ∧ b = rNextA (L.mem s2 (scReplyA p)))) ∧ + SSzA σ2 σ3 ∧ (LoopInvA σ3 m n ∧ σ3.schedulerAction = σ2.schedulerAction) ∧ + (∃ x3, σ3.kheap (scNameA m p) = some (.schedContext x3 n1) ∧ x3.scTcb = none) ∧ ScKeepYA σ σ3 ∧ + (L.mem s2 (scReplyA p) ≠ 0#u64 → L.mem s2 (scReplyA p) ∈ Rp ∧ L.mem r.1 (rNextA (L.mem s2 (scReplyA p))) = 0#u64 ∧ + Aligned (rNextA (L.mem s2 (scReplyA p))) ∧ IpcObjWA N E Rp (rNextA (L.mem s2 (scReplyA p)))) ∧ + x2.scReplies.head? = optRefS (L.mem s2 (scReplyA p)) ∧ + ((∀ r o, σ2.kheap r = some o → (∀ y n, o ≠ .schedContext y n) → (∀ z, o ≠ .reply z) → σ3.kheap r = some o) ∧ + (∀ r z, σ2.kheap r = some (.reply z) → ∃ o, σ3.kheap r = some (.reply { z with replySc := o }) ∧ + (L.mem s2 (scReplyA p) ≠ 0#u64 ∧ r = specPtr (L.mem s2 (scReplyA p)) → o = none) ∧ + (¬ (L.mem s2 (scReplyA p) ≠ 0#u64 ∧ r = specPtr (L.mem s2 (scReplyA p))) → o = z.replySc)) ∧ + (∀ r y n, σ2.kheap r = some (.schedContext y n) → ∃ y', σ3.kheap r = some (.schedContext y' n) ∧ + (r = scNameA m p → y'.scReplies = []) ∧ (r ≠ scNameA m p → y'.scReplies = y.scReplies))) ∧ + (∀ t ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → + (K.val ≠ 3464 ∧ K.val ≠ 3480 ∧ K.val ≠ 3488 ∧ K.val ≠ 3592) → L.mem r.1 (wa t K) = L.mem s (wa t K)) ∧ + StEqA Z.T (L.mem r.1) σ3 ∧ McpRelA Z.T m (L.mem r.1) σ3 ∧ YieldRelA Z.T Z.C m (L.mem r.1) σ3 ⦄ := by + have hcc := J.curCore_lt hCs + have hpS : p ∈ H.scs := by rw [J.scs]; exact hp + have hp0 := J.scNZ p hp + have hnm : scNameA m p = specPtr p := if_neg hni + have hni' : scNameA m p ≠ idleScPtr := by rw [hnm]; exact J.sr.sch.scOff p hp hni + have hniI : ¬ IdleScA m p := by + rintro ⟨k, hk, e⟩ + have : k = m.curCore := by + rw [h1] at hk hcc + exact (Nat.lt_one_iff.1 hk).trans (Nat.lt_one_iff.1 hcc).symm + exact hni (this ▸ e.symm) + have L0 : LoopInvA σ m n := ⟨J.sinv, J.sidle, J.sact⟩ + obtain ⟨xs, ns, hk, hx⟩ := J.sr.scr.sc p hp + have hbind : ∀ t, L.mem s (scTcbA p) = t → t ≠ 0#u64 → + t ∈ Z.T ∧ L.mem s (scA t) = p ∧ L.mem s (affA t) = L.cpu s ∧ + smp.remote_tcb_stall st ar s a t = ok (s, a) := by + intro t e h0 + have h0' : L.mem s (scTcbA p) ≠ 0#u64 := e ▸ h0 + have ht : t ∈ Z.T := by rw [← J.tcbs, ← e]; exact J.blkA.2.2.1.2.2.yt.1 p hpS h0' + exact ⟨ht, e ▸ J.blkA.2.2.1.2.2.tsc p hpS hp0 h0', J.onCore_single hCs h1 ht, J.stall_singleA hCs h1 ht⟩ + have hna : L.mem s (scTcbA p) ≠ 0#u64 → L.actionW s ≠ L.mem s (scTcbA p) := by + intro h0 e + have ht := (hbind _ rfl h0).1 + refine J.noSwitch (specPtr (L.mem s (scTcbA p))) ?_ + rw [J.sr.sch.act, e] + unfold actOfA + rw [if_neg h0, if_neg] + intro c + have := (J.sr.core.tcbs _ ht).align + rw [c] at this + simp at this + have hidle : L.mem s (scTcbA p) ≠ 0#u64 → L.idleW s ≠ L.mem s (scTcbA p) := by + intro h0 e + have hxT : xs.scTcb = some σ.idleThread := by + rw [J.sr.sch.idle, e, ← optRefS_ne h0, scTcbA_eqSc] + exact hx.tcb + exact hni' (J.sidle.f5 _ xs ns hk hxT) + have hst : StEqA Z.T (L.mem s) σ := by + intro t ht x hxt + obtain ⟨y, hy, rel⟩ := J.ep.tcb t ht + rw [hxt] at hy; cases hy + rw [← Notif.tsCode_eqA]; exact rel.st + -- `unbind_all_tcbs` + refine WP.spec_bind (WP.spec_and (sc_unbind_all_tcbs_stA J.sr hst a hp hp0 hni' hbind) + (K7.schedcontext.unbind_all_tcbs.blk (ar := ar) hA s a p J.blkA hpS hp0 hna hidle)) ?_ + rintro ⟨s1, a1⟩ ⟨⟨ea1, σ1, Q1, R1, sp1, H1, F1⟩, blk1⟩ + simp only [WP.uncurry'_pair] at blk1 ⊢ + obtain ⟨blk1, -, -, -, t1, -⟩ := blk1 + dsimp only at H1 F1 + have m1 : ((), σ1) ∈ (schedContextUnbindAllTcbs (Z := Unit) (scNameA m p) σ).1 := by rw [sp1]; exact rfl + have S1 := unbindAllTcbs_stepA L0 hk hni' m1 + -- `unbind_ntfn` + obtain ⟨x1, n1, hk1, hx1⟩ := H1.scr.sc p hp + have tc1 : x1.scTcb = none := by + rw [hx1.tcb, ← scTcbA_eqSc, t1, optRefS_zero] + have e40 : scNtfnA p = scWA p 40#u64 := by unfold scNtfnA scWA layout.sc.NTFN; rfl + have step2 : schedcontext.unbind_ntfn st s1 p ⦃ s2 => ∃ σ2 x2, + schedContextUnbindNtfn (Z := Unit) (scNameA m p) σ1 = ({((), σ2)}, False) ∧ + SchedSRA st { Z with Q := Q1, R := R1 } m κ s2 σ2 ∧ + σ2.kheap (scNameA m p) = some (.schedContext x2 n1) ∧ x2.scTcb = none ∧ + RefK σ1 σ2 ∧ ScKeepYA σ1 σ2 ∧ + (∀ t ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → + (K.val ≠ 3464 ∧ K.val ≠ 3480 ∧ K.val ≠ 3488 ∧ K.val ≠ 3592) → L.mem s2 (wa t K) = L.mem s1 (wa t K)) ∧ + L.ksb s2 = L.ksb s1 ∧ SSzA σ1 σ2 ∧ + WritesIn st s1 s2 (fun b => MWrA (st := st) { Z with Q := Q1, R := R1 } m σ1 s1 b ∨ + (L.mem s1 (scWA p 40#u64) ≠ 0#u64 ∧ b = nScSc (L.mem s1 (scWA p 40#u64)))) ∧ + (L.mem s1 (scWA p 40#u64) = 0#u64 ∧ σ2 = σ1 ∨ L.mem s1 (scWA p 40#u64) ≠ 0#u64 ∧ + L.mem s1 (scWA p 40#u64) ∈ N ∧ L.mem s2 (nScSc (L.mem s1 (scWA p 40#u64))) = 0#u64 ∧ + ∃ xn, σ1.kheap (specPtr (L.mem s1 (scWA p 40#u64))) = some (.notification xn) ∧ + σ2 = scNtfnLinkStA σ1 (scNameA m p) (specPtr (L.mem s1 (scWA p 40#u64))) x1 n1 xn false) ⦄ := by + by_cases h0 : L.mem s1 (scWA p 40#u64) = 0#u64 + · have ev : schedcontext.unbind_ntfn st s1 p ⦃ s2 => s2 = s1 ⦄ := by + unfold schedcontext.unbind_ntfn + refine WP.spec_bind (obj.Sc.is_null.spec _) ?_ + rintro _ rfl + simp only [decide_eq_false hp0, Bool.false_eq_true, ↓reduceIte] + refine WP.spec_bind (obj.Sc.ntfn.valA s1 p) ?_ + rintro _ rfl + rw [h0, if_neg (by simp), WP.spec_ok] + have hn : x1.scNtfn = none := by rw [hx1.ntfn, h0, optRefS_zero] + have sp : schedContextUnbindNtfn (Z := Unit) (scNameA m p) σ1 = ({((), σ1)}, False) := by + unfold schedContextUnbindNtfn getScObjRef + rw [nd_bind_assocSc, nd_bind_oneSc (Sched.getSchedContext_eqA hk1), nd_pure_bindSc, hn] + rfl + refine WP.spec_mono ev ?_ + rintro s2 rfl + exact ⟨σ1, x1, sp, H1, hk1, tc1, RefK.refl _, ScKeepYA.refl _, + fun _ _ _ _ _ _ => rfl, rfl, SSzA.refl _, WritesIn.refl _ _, Or.inl ⟨h0, rfl⟩⟩ + · obtain ⟨q, eq⟩ : ∃ q, L.mem s1 (scWA p 40#u64) = q := ⟨_, rfl⟩ + have q0 : q ≠ 0#u64 := eq ▸ h0 + have qH : q ∈ H.ntfns := by + rw [← eq, ← e40]; exact blk1.2.2.1.2.2.scn p hpS (by rw [e40]; exact h0) + have qN : q ∈ N := J.ipcHeapCov.ntfn q qH + have al : Aligned (nScSc q) := by + have e : nScSc q = Notif.nScA q := rfl + rw [e, Notif.nScA_eq]; exact Notif.nfield_aligned (J.iplaced.n.ntfn q qN) (by simp) (by simp) + have hout : ¬ SReadA st { Z with Q := Q1, R := R1 } m s1 σ1 (nScSc q) := + J.nScSc_offRead_laterA (Z' := { Z with Q := Q1, R := R1 }) rfl rfl H1 F1.wr.1 qN + obtain ⟨xn, hkn, -⟩ := J.ntfn.ntfn q qN + have hkn1 : σ1.kheap (specPtr q) = some (.notification xn) := + F1.keep.obj _ _ hkn (fun _ _ e => by cases e) (fun _ e => by cases e) + have hn : x1.scNtfn = some (specPtr q) := by rw [hx1.ntfn, eq, optRefS_ne q0] + have sp := schedContextUnbindNtfn_someA hk1 hkn1 hn + refine WP.spec_mono (sc_unbind_ntfn_someA H1 hp hp0 q0 al hout eq hkn1) ?_ + rintro s2 ⟨σ2, sp2, H2, nq, w⟩ + have e2 : σ2 = scNtfnLinkStA σ1 (scNameA m p) (specPtr q) x1 n1 xn false := by + have h : ((), σ2) ∈ (schedContextUnbindNtfn (Z := Unit) (scNameA m p) σ1).1 := by rw [sp2]; exact rfl + rw [sp] at h + exact (Prod.mk.inj h).2 + have ne : specPtr q ≠ scNameA m p := fun e => by rw [e, hk1] at hkn1; cases hkn1 + have mid : (updKSc σ1 (specPtr q) (.notification { xn with ntfnSc := none })).kheap (scNameA m p) = + some (.schedContext x1 n1) := by rw [updKSc_kheap, Function.update_of_ne ne.symm]; exact hk1 + have at2 : σ2.kheap (scNameA m p) = some (.schedContext { x1 with scNtfn := none } n1) := by + rw [e2]; unfold scNtfnLinkStA; rw [updKSc_kheap, Function.update_self]; rfl + have z0 : L.mem s2 (nScSc q) = 0#u64 := by + obtain ⟨z, hz, ez⟩ := nq + have : z.ntfnSc = none := by + rw [e2] at hz + unfold scNtfnLinkStA at hz + rw [updKSc_kheap, Function.update_of_ne ne, updKSc_kheap, Function.update_self] at hz + cases hz; rfl + rw [this] at ez + by_contra c + unfold optScA at ez + rw [if_neg c] at ez + cases ez + have wr2 : WritesIn st s1 s2 (fun b => MWrA (st := st) { Z with Q := Q1, R := R1 } m σ1 s1 b ∨ + (L.mem s1 (scWA p 40#u64) ≠ 0#u64 ∧ b = nScSc (L.mem s1 (scWA p 40#u64)))) := + w.mono fun _ hb => hb.imp id (fun e => ⟨h0, e.trans (by rw [eq])⟩) + have desc : L.mem s1 (scWA p 40#u64) = 0#u64 ∧ σ2 = σ1 ∨ L.mem s1 (scWA p 40#u64) ≠ 0#u64 ∧ + L.mem s1 (scWA p 40#u64) ∈ N ∧ L.mem s2 (nScSc (L.mem s1 (scWA p 40#u64))) = 0#u64 ∧ + ∃ xn, σ1.kheap (specPtr (L.mem s1 (scWA p 40#u64))) = some (.notification xn) ∧ + σ2 = scNtfnLinkStA σ1 (scNameA m p) (specPtr (L.mem s1 (scWA p 40#u64))) x1 n1 xn false := by + refine Or.inr ⟨h0, ?_⟩ + rw [eq] + exact ⟨qN, z0, xn, hkn1, e2⟩ + refine ⟨σ2, _, sp2, H2, at2, tc1, e2 ▸ scNtfnUnlink_refKA hk1 hkn1 false, ?_, ?_, w.1, ?_, + wr2, desc⟩ + · rw [e2] + exact (ScKeepYA.updNtfn hkn1).trans (ScKeepYA.updSc mid rfl) + · intro t ht K hK h8 hne + refine w.mem_eq (field_aligned (J.sr.core.tcbs t ht) hK h8) ?_ + rintro (h | h) + · exact J.tcbWord_offMA ht hK hne (mwr_back (Z := Z) (Z' := { Z with Q := Q1, R := R1 }) rfl rfl F1.sz F1.wr.1 h) + · have v : (nScSc q).val = q.val + 32 := by + have e : nScSc q = Notif.nScA q := rfl + rw [e, Notif.nScA_eq, Notif.nfield_val (J.iplaced.n.ntfn q qN) (by simp)]; simp + exact J.ipc_ne_tcbA ht hK (Or.inl ⟨q, qN, by omega, by omega⟩) h + · rw [e2] + intro r y k h + unfold scNtfnLinkStA at h + by_cases c : r = scNameA m p + · subst c; rw [updKSc_kheap, Function.update_self] at h; cases h; exact ⟨x1, hk1⟩ + · rw [updKSc_kheap, Function.update_of_ne c, updKSc_kheap] at h + by_cases d : r = specPtr q + · subst d; rw [Function.update_self] at h; cases h + · rw [Function.update_of_ne d] at h; exact ⟨y, h⟩ + refine WP.spec_bind (WP.spec_and step2 (K7.schedcontext.unbind_ntfn.blk s1 p blk1 fun _ => hpS)) ?_ + rintro s2 ⟨⟨σ2, x2, m2, H2, hk2, tc2, K2, Y2, w2, kb2, z2, wr2, desc2⟩, blk2, -⟩ + have L2 := S1.inv.refK K2 + -- `unbind_reply` + have kb02 : L.ksb s2 = L.ksb s := kb2.trans F1.wr.1 + have z02 : SSzA σ σ2 := F1.sz.trans z2 + have rep : L.mem s2 (scReplyA p) ≠ 0#u64 → L.mem s2 (scReplyA p) ∈ Rp := + fun h => J.ipcHeapCov.reply _ (blk2.2.2.1.2.1.screp p hpS h) + have hkr : ∀ R, x2.scReplies.head? = some R → ∃ xr, σ2.kheap R = some (.reply xr) := by + intro R hR + have e : x2.scReplies.head? = optRefS (L.mem s2 (scReplyA p)) := by + obtain ⟨y, k, hy, rel⟩ := H2.scr.sc p hp + rw [hk2] at hy; cases hy + rw [scReplyA_eqS]; exact rel.reply + rw [e] at hR + by_cases h0 : L.mem s2 (scReplyA p) = 0#u64 + · rw [h0, optRefS_zero] at hR; cases hR + · rw [optRefS_ne h0] at hR + cases hR + obtain ⟨xr, hr, -⟩ := J.ep.reply _ (rep h0) + have hr1 := F1.keep.obj _ _ hr (fun _ _ e => by cases e) (fun _ e => by cases e) + have K := K2.2.1 (specPtr (L.mem s2 (scReplyA p))) + rw [hr1] at K + generalize σ2.kheap (specPtr (L.mem s2 (scReplyA p))) = o at K ⊢ + cases K + all_goals exact ⟨_, rfl⟩ + have hrw : L.mem s2 (scReplyA p) ≠ 0#u64 → Aligned (rNextA (L.mem s2 (scReplyA p))) ∧ + ¬ SReadA st { Z with Q := Q1, R := R1 } m s2 σ2 (rNextA (L.mem s2 (scReplyA p))) ∧ + IpcObjWA N E Rp (rNextA (L.mem s2 (scReplyA p))) := by + intro h0 + have hr := rep h0 + obtain ⟨r32, -, rfit⟩ := J.iplaced.e.rep _ hr + have n16 : (layout.reply.NEXT).val = 16 := by unfold layout.reply.NEXT; rfl + have v : (rNextA (L.mem s2 (scReplyA p))).val = (L.mem s2 (scReplyA p)).val + 16 := by + have e : rNextA (L.mem s2 (scReplyA p)) = wa (L.mem s2 (scReplyA p)) layout.reply.NEXT := by + unfold rNextA; rfl + rw [e, wa_valm, n16]; exact Nat.mod_eq_of_lt (by omega) + refine ⟨by unfold Aligned; rw [v]; omega, ?_, Or.inr (Or.inr ⟨_, hr, by omega, by omega⟩)⟩ + exact (replyWordOffSchedA H2.toSchedSRN J.ipcPlace hr (by omega) (by omega)).1 + have alr : Aligned (scReplyA p) := by + obtain ⟨y, k, hy, rel⟩ := H2.scr.sc p hp + rw [scReplyA_eqS] + exact (rel.hdr (K := 32#u64) (by simp) (by simp)).1 + refine WP.spec_bind (WP.spec_and (sc_unbind_replyA H2 hp hp0 (fun x k h R hR => by + rw [hk2] at h; cases h; exact hkr R hR) (fun h => ⟨(hrw h).1, (hrw h).2.1⟩)) + (sc_unbind_reply_next0A hp0 alr (fun h => (hrw h).1))) ?_ + rintro s3 ⟨⟨x, k, hx2, sp3, H3, w3, z3⟩, n0⟩ + rw [hk2] at hx2; cases hx2 + have L3 := scUnbRep_invA L2 hk2 hni' hkr + obtain ⟨x3, hk3, tc3, Y3⟩ : ∃ x3, (unbRepStA σ2 (scNameA m p) x2 n1).kheap (scNameA m p) = + some (.schedContext x3 n1) ∧ x3.scTcb = none ∧ ScKeepYA σ2 (unbRepStA σ2 (scNameA m p) x2 n1) := by + cases hl : x2.scReplies with + | nil => rw [unbRepStA_nil hl]; exact ⟨x2, hk2, tc2, ScKeepYA.refl _⟩ + | cons R l => + obtain ⟨xr, hr⟩ := hkr R (by rw [hl]; rfl) + have ne : scNameA m p ≠ R := fun e => by rw [e, hr] at hk2; cases hk2 + rw [unbRepStA_cons hl, rscA_of hr] + have mid : (updKSc σ2 R (.reply { xr with replySc := none })).kheap (scNameA m p) = + some (.schedContext x2 n1) := by rw [updKSc_kheap, Function.update_of_ne ne]; exact hk2 + refine ⟨{ x2 with scReplies := [] }, by unfold repStA; rw [updKSc_kheap, Function.update_self], tc2, ?_⟩ + exact (ScKeepYA.updReply hr).trans (ScKeepYA.updSc mid rfl) + have hhead : x2.scReplies.head? = optRefS (L.mem s2 (scReplyA p)) := by + obtain ⟨y, k, hy, rel⟩ := H2.scr.sc p hp + rw [hk2] at hy; cases hy + rw [scReplyA_eqS]; exact rel.reply + have desc3 : (∀ r o, σ2.kheap r = some o → (∀ y n, o ≠ .schedContext y n) → (∀ z, o ≠ .reply z) → + (unbRepStA σ2 (scNameA m p) x2 n1).kheap r = some o) ∧ + (∀ r z, σ2.kheap r = some (.reply z) → ∃ o, + (unbRepStA σ2 (scNameA m p) x2 n1).kheap r = some (.reply { z with replySc := o }) ∧ + (L.mem s2 (scReplyA p) ≠ 0#u64 ∧ r = specPtr (L.mem s2 (scReplyA p)) → o = none) ∧ + (¬ (L.mem s2 (scReplyA p) ≠ 0#u64 ∧ r = specPtr (L.mem s2 (scReplyA p))) → o = z.replySc)) ∧ + (∀ r y n, σ2.kheap r = some (.schedContext y n) → ∃ y', + (unbRepStA σ2 (scNameA m p) x2 n1).kheap r = some (.schedContext y' n) ∧ + (r = scNameA m p → y'.scReplies = []) ∧ (r ≠ scNameA m p → y'.scReplies = y.scReplies)) := by + cases hl : x2.scReplies with + | nil => + have h0 : L.mem s2 (scReplyA p) = 0#u64 := by + by_contra c + rw [hl, optRefS_ne c] at hhead + cases hhead + rw [unbRepStA_nil hl] + refine ⟨fun _ _ h _ _ => h, fun r z h => ⟨z.replySc, h, fun c => absurd h0 c.1, fun _ => rfl⟩, + fun r y n h => ⟨y, h, fun e => ?_, fun _ => rfl⟩⟩ + rw [e, hk2] at h; cases h; exact hl + | cons R l => + have h0 : L.mem s2 (scReplyA p) ≠ 0#u64 := by + intro c + rw [hl, c, optRefS_zero] at hhead + cases hhead + have eR : R = specPtr (L.mem s2 (scReplyA p)) := by + rw [hl, optRefS_ne h0] at hhead + exact Option.some.inj hhead + obtain ⟨xr, hr⟩ := hkr R (by rw [hl]; rfl) + have ne : scNameA m p ≠ R := fun e => by rw [e, hr] at hk2; cases hk2 + rw [unbRepStA_cons hl, rscA_of hr] + unfold repStA + refine ⟨fun r o h h1 h2 => ?_, fun r z h => ?_, fun r y n h => ?_⟩ + · have a : r ≠ scNameA m p := fun e => by rw [e, hk2] at h; cases h; exact h1 _ _ rfl + have b : r ≠ R := fun e => by rw [e, hr] at h; cases h; exact h2 _ rfl + simp only [updKSc_kheap, Function.update_of_ne a, Function.update_of_ne b] + exact h + · have a : r ≠ scNameA m p := fun e => by rw [e, hk2] at h; cases h + by_cases b : r = R + · subst b + rw [hr] at h; cases h + refine ⟨none, ?_, fun _ => rfl, fun c => absurd ⟨h0, eR⟩ c⟩ + simp only [updKSc_kheap, Function.update_of_ne a, Function.update_self] + · refine ⟨z.replySc, ?_, fun c => absurd (c.2.trans eR.symm) b, fun _ => rfl⟩ + simp only [updKSc_kheap, Function.update_of_ne a, Function.update_of_ne b] + exact h + · have b : r ≠ R := fun e => by rw [e, hr] at h; cases h + by_cases a : r = scNameA m p + · subst a + exact ⟨{ x2 with scReplies := [] }, by rw [updKSc_kheap, Function.update_self]; rw [hk2] at h; cases h; rfl, + fun _ => rfl, fun c => absurd rfl c⟩ + · refine ⟨y, ?_, fun c => absurd c a, fun _ => rfl⟩ + simp only [updKSc_kheap, Function.update_of_ne a, Function.update_of_ne b] + exact h + generalize unbRepStA σ2 (scNameA m p) x2 n1 = σ3 at sp3 H3 z3 L3 hk3 Y3 desc3 + -- the relations of the yield step, back from the start + have Y1 : ScKeepYA σ σ1 := ⟨fun q z h => by + obtain ⟨y, hy, o, rfl⟩ := F1.keep.tcb q z h + exact ⟨_, hy, rfl, rfl, rfl, rfl⟩, fun q x k hx => by + obtain ⟨y, hy, e, -⟩ := S1.sc q x k hx + exact ⟨y, hy, e⟩⟩ + have Y03 : ScKeepYA σ σ3 := Y1.trans (Y2.trans Y3) + have words : ∀ t ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → + (K.val ≠ 3464 ∧ K.val ≠ 3480 ∧ K.val ≠ 3488 ∧ K.val ≠ 3592) → L.mem s3 (wa t K) = L.mem s (wa t K) := by + intro t ht K hK h8 hne + have al := field_aligned (J.sr.core.tcbs t ht) hK h8 + have a1 : L.mem s1 (wa t K) = L.mem s (wa t K) := F1.wr.mem_eq al (J.tcbWord_offMA ht hK hne) + have a3 : L.mem s3 (wa t K) = L.mem s2 (wa t K) := by + refine w3.mem_eq al ?_ + rintro (h | ⟨h0, h⟩) + · exact J.tcbWord_offMA ht hK hne + (mwr_back (Z := Z) (Z' := { Z with Q := Q1, R := R1 }) rfl rfl z02 kb02 h) + · exact J.ipc_ne_tcbA ht hK (hrw h0).2.2 h + rw [a3, w2 t ht K hK h8 hne, a1] + have hst3 : StEqA Z.T (L.mem s3) σ3 := by + intro t ht z hz + obtain ⟨z0, hz0, -⟩ := J.sr.sch.tcb t ht + obtain ⟨y, hy, es, -⟩ := Y03.tcb _ _ hz0 + rw [hz] at hy; cases hy + have e : stA t = wa t 3328#u64 := by rw [stA_eq] + rw [e, words t ht 3328#u64 (by simp) (by simp) (by simp), ← e, es] + exact hst t ht z0 hz0 + obtain ⟨M3, R3⟩ := yieldFrameA (mem' := L.mem s3) (σ' := σ3) J.mcp J.yieldRel + (fun t ht => ⟨words t ht 3440#u64 (by simp) (by simp) (by simp), words t ht 3456#u64 (by simp) (by simp) (by simp)⟩) + (fun t ht => words t ht 3472#u64 (by simp) (by simp) (by simp)) + (fun t _ z hz => by + obtain ⟨y, hy, -, b, c, d⟩ := Y03.tcb _ _ hz + exact ⟨y, hy, b, c, d⟩) + (fun c hc => by + obtain ⟨z, k, hz, -⟩ := J.sr.scr.sc c hc + obtain ⟨y, hy, e⟩ := Y03.sc _ _ _ hz + exact ⟨z, k, y, hz, hy, e⟩) + rw [WP.spec_ok] + have hrp : L.mem s2 (scReplyA p) ≠ 0#u64 → L.mem s2 (scReplyA p) ∈ Rp ∧ + L.mem s3 (rNextA (L.mem s2 (scReplyA p))) = 0#u64 ∧ + Aligned (rNextA (L.mem s2 (scReplyA p))) ∧ IpcObjWA N E Rp (rNextA (L.mem s2 (scReplyA p))) := + fun h0 => ⟨rep h0, n0 h0, (hrw h0).1, (hrw h0).2.2⟩ + have sp1' : schedContextUnbindAllTcbs (Z := Unit) (scNameA m p) σ = ({((), σ1)}, False) := sp1 + exact ⟨ea1, s1, s2, σ1, σ2, σ3, Q1, R1, x1, x2, n1, F1, sp1', S1, hk1, m2, K2, hk2, desc2, F1.wr.trans wr2 |>.mono + (fun b h => h.elim Or.inl fun h => h.imp (mwr_back (Z := Z) (Z' := { Z with Q := Q1, R := R1 }) rfl rfl F1.sz F1.wr.1) id), + kb02, z02, sp3, H3, w3, z3, L3, ⟨x3, hk3, tc3⟩, Y03, hrp, hhead, desc3, words, hst3, M3, R3⟩ + +set_option maxHeartbeats 3200000 in +/-- **The final SC cap's finalisation at the scheduler level** (module docstring). -/ +theorem JStage3TA.scFin_schedA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {p b : U64} (hp : p ∈ Z.C) (hni : specPtr p ≠ m.idleScOf m.curCore) + (hA : P0.ArchTotal ar) (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (h1 : fl4A64Platform.numNodes = 1) (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) : + objects.finalise_cap_object st ar s a (.SchedContext p b) true ⦃ r => + r.1 = (.Null, .Null) ∧ r.2.2 = a ∧ ∃ σ1 σ2 σ3 σ4 σ5 Q R, + ((), σ1) ∈ (schedContextUnbindAllTcbs (Z := Unit) (specPtr p) σ).1 ∧ + ((), σ2) ∈ (schedContextUnbindNtfn (Z := Unit) (specPtr p) σ1).1 ∧ + ((), σ3) ∈ (schedContextUnbindReply (Z := Unit) (specPtr p) σ2).1 ∧ + ((), σ4) ∈ (schedContextUnbindYieldFrom (Z := Unit) (specPtr p) σ3).1 ∧ + ((), σ5) ∈ (schedContextSetInactive (Z := Unit) (specPtr p) σ4).1 ∧ + SchedSRA st { Z with Q := Q, R := R } m κ r.2.1 σ5 ∧ LoopInvA σ5 m n ∧ NoSwA σ5 ∧ SchedFrameA σ σ5 ∧ + ∃ q h yf d, ScFinEndA st Z N Rp m s r.2.1 σ σ5 p q h yf d ⦄ := by + have hcc := J.curCore_lt hCs + have hp0 := J.scNZ p hp + have hnm : scNameA m p = specPtr p := if_neg hni + have hni' : scNameA m p ≠ idleScPtr := by rw [hnm]; exact J.sr.sch.scOff p hp hni + have hniI : ¬ IdleScA m p := by + rintro ⟨k, hk, e⟩ + have : k = m.curCore := by + rw [h1] at hk hcc + exact (Nat.lt_one_iff.1 hk).trans (Nat.lt_one_iff.1 hcc).symm + exact hni (this ▸ e.symm) + obtain ⟨xs, ns, hk, -⟩ := J.sr.scr.sc p hp + simp only [objects.finalise_cap_object, ↓reduceIte] + refine scUnb3_specA (J.scUnb_schedA hp hni hA hCs h1) ?_ + rintro ⟨s3, a1⟩ ⟨ea1, s1, s2, σ1, σ2, σ3, Q1, R1, x1, x2, n1, F1, sp1, S1, hk1, sp2, K2, hk2, desc2, w02, kb02, z02, + sp3, H3, w3, z3, L3, ⟨x3, hk3, tc3⟩, Y03, hrp, hhead, desc3, words, hst3, M3, R3⟩ + have m1 : ((), σ1) ∈ (schedContextUnbindAllTcbs (Z := Unit) (scNameA m p) σ).1 := by rw [sp1]; exact rfl + have m2 : ((), σ2) ∈ (schedContextUnbindNtfn (Z := Unit) (scNameA m p) σ1).1 := by rw [sp2]; exact rfl + -- `unbind_yield_from` + have hyw : x3.scYieldFrom = optRefS (L.mem s3 (scYieldFromA p)) := by + obtain ⟨y, k, hy, rel⟩ := H3.scr.sc p hp + rw [hk3] at hy; cases hy + have e : scYieldFromA p = scWA p 56#u64 := by unfold scYieldFromA scWA layout.sc.YIELD_FROM; rfl + rw [e]; exact rel.yieldFrom + refine WP.spec_bind (obj.Sc.yield_from.val s3 p) ?_ + intro i ei + have ei' : i = L.mem s3 (scYieldFromA p) := by + rw [ei] + refine WP.spec_bind (obj.Tcb.is_null.spec i) ?_ + intro bb ebb + have step4 : (if bb = true then ok (s3, a1) else schedcontext.complete_yield_to st ar s3 a1 i) ⦃ r => ∃ σ4 x4, + ((), σ4) ∈ (schedContextUnbindYieldFrom (Z := Unit) (scNameA m p) σ3).1 ∧ + SchedSRA st { Z with Q := Q1, R := R1 } m κ r.1 σ4 ∧ + σ4.kheap (scNameA m p) = some (.schedContext x4 n1) ∧ x4.scTcb = none ∧ RefK σ3 σ4 ∧ r.2 = a1 ∧ + McpRelA Z.T m (L.mem r.1) σ4 ∧ YieldRelA Z.T Z.C m (L.mem r.1) σ4 ∧ + WritesIn st s3 r.1 (fun b => MWrA (st := st) { Z with Q := Q1, R := R1 } m σ3 s3 b ∨ + (i ≠ 0#u64 ∧ (ScConsumedRegsA i b ∨ b = yieldA i))) ∧ + (i = 0#u64 ∧ σ4 = σ3 ∨ i ≠ 0#u64 ∧ i ∈ Z.T ∧ + (∀ r, r ≠ scNameA m p → r ≠ specPtr i → σ4.kheap r = σ3.kheap r) ∧ + ∃ d : Fl4SpecA64.Data, (∀ x, σ3.kheap (specPtr i) = some (.tcb x) → + σ4.kheap (specPtr i) = some (.tcb (yieldDoneA x d))) ∧ + L.mem r.1 (wa i 256#u64) = 0#u64 ∧ + (L.mem r.1 (wa i 264#u64)).bv = Fl4SpecA64.messageInfoToData ⟨Fl4SpecA64.natToLen 1, 0, 0, 0⟩ ∧ + (L.mem r.1 (wa i 272#u64)).bv = d) ⦄ := by + subst ebb + by_cases h0 : i = 0#u64 + · rw [if_pos (by simpa using h0), WP.spec_ok] + have hn : x3.scYieldFrom = none := by rw [hyw, ← ei', h0, optRefS_zero] + have sp : schedContextUnbindYieldFrom (Z := Unit) (scNameA m p) σ3 = ({((), σ3)}, False) := by + unfold schedContextUnbindYieldFrom + rw [nd_bind_oneSc (Sched.getSchedContext_eqA hk3), hn] + rfl + exact ⟨σ3, x3, by rw [sp]; exact rfl, H3, hk3, tc3, RefK.refl _, rfl, M3, R3, WritesIn.refl _ _, + Or.inl ⟨h0, rfl⟩⟩ + · rw [if_neg (by simpa using h0)] + have hyf : x3.scYieldFrom = some (specPtr i) := by rw [hyw, ← ei', optRefS_ne h0] + refine WP.spec_mono (yield_complete_liveWithA H3 hst3 M3 R3 a1 hp J.scNZ h0 ei'.symm maxTicksToUs ticksToUs + hmax (htu a1)) ?_ + rintro r ⟨ea4, σ4, -, H4, -, M4, R4, -, -, -, -, scs, wr4, ev, far, d, done, v0, v1, v2⟩ + rw [completeYieldToWith_eq_l4v] at ev + have sp : schedContextUnbindYieldFrom (Z := Unit) (scNameA m p) σ3 = ({((), σ4)}, False) := by + unfold schedContextUnbindYieldFrom + rw [nd_bind_oneSc (Sched.getSchedContext_eqA hk3), hyf] + exact ev + obtain ⟨y4, hy4, et, -⟩ := scs x3 n1 hk3 + obtain ⟨t, ht, e, hw⟩ := R3.frm p hp x3 n1 hk3 _ hyf + have : t = i := specPtr_inj e + subst this + obtain ⟨z, hz, hzy⟩ := R3.tcb t ht + have hzy' : z.tcbYieldTo = some (scNameA m p) := by + rw [hzy, hw]; simp [optScA, hp0, scNameA_eq] + exact ⟨σ4, y4, by rw [sp]; exact rfl, H4, hy4, et.trans tc3, + completeYieldTo_refKA hz hzy' hk3 (r := ((), σ4)) (by rw [ev]; exact rfl), ea4, M4, R4, + wr4.mono fun _ hb => by + rcases hb with hb | hb | hb + · exact Or.inl hb + · exact Or.inr ⟨h0, Or.inl hb⟩ + · exact Or.inr ⟨h0, Or.inr hb⟩, + Or.inr ⟨h0, ht, far, d, done, v0, v1, v2⟩⟩ + refine WP.spec_bind step4 ?_ + rintro ⟨s4, a2⟩ ⟨σ4, x4, m4, H4, hk4, tc4, K4, ea2, M4, R4, wr4, desc4⟩ + simp only [WP.uncurry'_pair, uncurry_apply_pair] + have L4 := L3.1.refK K4 + -- `set_inactive` + have inact := sc_set_inactiveA H4 hp hniI (fun x k h => by + rw [hk4] at h; cases h; exact tc4) + have assoc : ∀ f : S → Result ((capability.Cap × capability.Cap) × S × A), + (do + let s5 ← obj.Sc.set_refill_max st p s4 0#u64 + let s6 ← obj.Sc.set_sporadic st p s5 0#u64 + f s6) = ((do + let s5 ← obj.Sc.set_refill_max st p s4 0#u64 + obj.Sc.set_sporadic st p s5 0#u64) >>= f) := fun f => (scBindAssocA _ _ _).symm + rw [assoc] + refine WP.spec_bind inact ?_ + rintro s6 ⟨x, k, hx4, sp5, H5, wr6, F6, -⟩ + rw [hk4] at hx4; cases hx4 + have F5 : SchedFrameA σ (updKSc σ4 (scNameA m p) (.schedContext (inactScA x4) n1)) := by + have f1 : SchedFrameA σ σ1 := + (inferInstance : SPresInst (schedContextUnbindAllTcbs (Z := Unit) (scNameA m p))).pres σ ((), σ1) m1 + have f2 : SchedFrameA σ1 σ2 := + (inferInstance : SPresInst (schedContextUnbindNtfn (Z := Unit) (scNameA m p))).pres σ1 ((), σ2) m2 + have f3 : SchedFrameA σ2 σ3 := + (inferInstance : SPresInst (schedContextUnbindReply (Z := Unit) (scNameA m p))).pres σ2 ((), σ3) + (by rw [sp3]; exact rfl) + have u4 : SPresA (schedContextUnbindYieldFrom (Z := Unit) (scNameA m p)) := by + unfold schedContextUnbindYieldFrom; spres + have u5 : SPresA (schedContextSetInactive (Z := Unit) (scNameA m p)) := by + unfold schedContextSetInactive; spres + have f4 : SchedFrameA σ3 σ4 := u4 σ3 ((), σ4) m4 + have f5 := u5 σ4 ((), updKSc σ4 (scNameA m p) (.schedContext (inactScA x4) n1)) (by rw [sp5]; exact rfl) + exact PreO.trans f1 (PreO.trans f2 (PreO.trans f3 (PreO.trans f4 f5))) + have kb23 : L.ksb s3 = L.ksb s2 := w3.1 + have kb03 : L.ksb s3 = L.ksb s := kb23.trans kb02 + have z03 : SSzA σ σ3 := z02.trans z3 + have kb04 : L.ksb s4 = L.ksb s := wr4.1.trans kb03 + have z04 : SSzA σ σ4 := z03.trans (by + intro r y n h + have h' := K4.2.1 r + rw [h] at h' + generalize e : σ3.kheap r = o at h' + cases h' with + | same => exact ⟨y, rfl⟩ + | sc x _ _ _ => exact ⟨x, rfl⟩) + have wrAll : WritesIn st s s6 (ScFinWA st Z m σ s (L.mem s1 (scWA p 40#u64)) (L.mem s2 (scReplyA p)) i) := by + refine (((w02.trans w3).trans wr4).trans F6.wr).mono fun b hb => ?_ + rcases hb with ((hb | hb) | hb) | hb + · rcases hb with hb | hb + · exact Or.inl hb + · exact Or.inr (Or.inl hb) + · rcases hb with hb | hb + · exact Or.inl (mwr_back (Z := Z) (Z' := { Z with Q := Q1, R := R1 }) rfl rfl z02 kb02 hb) + · exact Or.inr (Or.inr (Or.inl hb)) + · rcases hb with hb | hb + · exact Or.inl (mwr_back (Z := Z) (Z' := { Z with Q := Q1, R := R1 }) rfl rfl z03 kb03 hb) + · exact Or.inr (Or.inr (Or.inr hb)) + · exact Or.inl (mwr_back (Z := Z) (Z' := { Z with Q := Q1, R := R1 }) rfl rfl z04 kb04 hb) + -- the head reply + have h2s : L.mem s2 (scReplyA p) ≠ 0#u64 → L.mem s2 (scReplyA p) ∈ Rp := fun h => (hrp h).1 + have offT : ∀ t ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → + (K.val ≠ 3464 ∧ K.val ≠ 3480 ∧ K.val ≠ 3488 ∧ K.val ≠ 3592) → + ∀ s' s'', WritesIn st s' s'' (MWrA (st := st) { Z with Q := Q1, R := R1 } m σ4 s4) → + L.mem s'' (wa t K) = L.mem s' (wa t K) := by + intro t ht K hK h8 hne s' s'' w + exact w.mem_eq (field_aligned (J.sr.core.tcbs t ht) hK h8) fun h => + J.tcbWord_offMA ht hK hne (mwr_back (Z := Z) (Z' := { Z with Q := Q1, R := R1 }) rfl rfl z04 kb04 h) + have offI : ∀ b, Aligned b → IpcObjWA N E Rp b → L.mem s6 b = L.mem s4 b := fun b al h => + F6.wr.mem_eq al fun h' => + J.ipc_offMA h (mwr_back (Z := Z) (Z' := { Z with Q := Q1, R := R1 }) rfl rfl z04 kb04 h') + have off34 : ∀ b, Aligned b → IpcObjWA N E Rp b → L.mem s4 b = L.mem s3 b := fun b al h => by + refine wr4.mem_eq al ?_ + rintro (h' | ⟨h0, h'⟩) + · exact J.ipc_offMA h (mwr_back (Z := Z) (Z' := { Z with Q := Q1, R := R1 }) rfl rfl z03 kb03 h') + · have hi : i ∈ Z.T := by + rcases desc4 with ⟨c, -⟩ | ⟨-, hi, -⟩ + · exact absurd c h0 + · exact hi + rcases h' with (e | e | e) | e + · exact J.ipc_ne_tcbA hi (K := 256#u64) (by simp) h e.symm + · exact J.ipc_ne_tcbA hi (K := 264#u64) (by simp) h e.symm + · exact J.ipc_ne_tcbA hi (K := 272#u64) (by simp) h e.symm + · exact J.ipc_ne_tcbA hi (K := 3472#u64) (by simp) h e.symm + have ntf : L.mem s1 (scWA p 40#u64) ≠ 0#u64 → + L.mem s1 (scWA p 40#u64) ∈ N ∧ L.mem s6 (nScSc (L.mem s1 (scWA p 40#u64))) = 0#u64 := by + intro h0 + rcases desc2 with ⟨c, -⟩ | ⟨-, qN, z0, -⟩ + · exact absurd c h0 + refine ⟨qN, ?_⟩ + have v : (nScSc (L.mem s1 (scWA p 40#u64))).val = (L.mem s1 (scWA p 40#u64)).val + 32 := by + have e : ∀ q, nScSc q = Notif.nScA q := fun _ => rfl + rw [e, Notif.nScA_eq, Notif.nfield_val (J.iplaced.n.ntfn _ qN) (by simp)]; simp + have al : Aligned (nScSc (L.mem s1 (scWA p 40#u64))) := by + have e : ∀ q, nScSc q = Notif.nScA q := fun _ => rfl + rw [e, Notif.nScA_eq]; exact Notif.nfield_aligned (J.iplaced.n.ntfn _ qN) (by simp) (by simp) + have hw : IpcObjWA N E Rp (nScSc (L.mem s1 (scWA p 40#u64))) := Or.inl ⟨_, qN, by omega, by omega⟩ + rw [offI _ al hw, off34 _ al hw, w3.mem_eq al ?_, z0] + rintro (h' | ⟨h0', h'⟩) + · exact J.ipc_offMA hw (mwr_back (Z := Z) (Z' := { Z with Q := Q1, R := R1 }) rfl rfl z02 kb02 h') + · have hr := h2s h0' + have n16 : (layout.reply.NEXT).val = 16 := by unfold layout.reply.NEXT; rfl + obtain ⟨-, -, rfit⟩ := J.iplaced.e.rep _ hr + have vr : (rNextA (L.mem s2 (scReplyA p))).val = (L.mem s2 (scReplyA p)).val + 16 := by + have e : rNextA (L.mem s2 (scReplyA p)) = wa (L.mem s2 (scReplyA p)) layout.reply.NEXT := by + unfold rNextA; rfl + rw [e, wa_valm, n16]; exact Nat.mod_eq_of_lt (by omega) + have := J.iplaced.nr _ qN _ hr + have := congrArg (fun w : U64 => w.val) h' + simp only [v, vr] at this + omega + have rp : L.mem s2 (scReplyA p) ≠ 0#u64 → + L.mem s2 (scReplyA p) ∈ Rp ∧ L.mem s6 (rNextA (L.mem s2 (scReplyA p))) = 0#u64 := by + intro h0 + refine ⟨h2s h0, ?_⟩ + rw [offI _ (hrp h0).2.2.1 (hrp h0).2.2.2, off34 _ (hrp h0).2.2.1 (hrp h0).2.2.2] + exact (hrp h0).2.1 + have yT : i ≠ 0#u64 → i ∈ Z.T ∧ L.mem s6 (wa i 256#u64) = 0#u64 ∧ + (L.mem s6 (wa i 264#u64)).bv = Fl4SpecA64.messageInfoToData ⟨Fl4SpecA64.natToLen 1, 0, 0, 0⟩ := by + intro h0 + rcases desc4 with ⟨c, -⟩ | ⟨-, hi, -, d, -, v0, v1, -⟩ + · exact absurd c h0 + exact ⟨hi, by rw [offT i hi 256#u64 (by simp) (by simp) (by simp) s4 s6 F6.wr]; exact v0, + by rw [offT i hi 264#u64 (by simp) (by simp) (by simp) s4 s6 F6.wr]; exact v1⟩ + obtain ⟨d, done, v272⟩ : ∃ d : Fl4SpecA64.Data, + (i ≠ 0#u64 → (∀ r, r ≠ scNameA m p → r ≠ specPtr i → σ4.kheap r = σ3.kheap r) ∧ + ∀ x, σ3.kheap (specPtr i) = some (.tcb x) → σ4.kheap (specPtr i) = some (.tcb (yieldDoneA x d))) ∧ + (i ≠ 0#u64 → (L.mem s6 (wa i 272#u64)).bv = d) := by + rcases desc4 with ⟨c, -⟩ | ⟨-, hi, far, d, done, -, -, v2⟩ + · exact ⟨0, fun h => absurd c h, fun h => absurd c h⟩ + · exact ⟨d, fun _ => ⟨far, done⟩, + fun _ => by rw [offT i hi 272#u64 (by simp) (by simp) (by simp) s4 s6 F6.wr]; exact v2⟩ + -- the spec objects + have rep1 := unbindAllTcbs_repA hk m1 + have K1 := F1.keep + have e5 : ∀ r, r ≠ scNameA m p → (updKSc σ4 (scNameA m p) (.schedContext (inactScA x4) n1)).kheap r = σ4.kheap r := + fun r h => by rw [updKSc_kheap, Function.update_of_ne h] + have ne5 : ∀ r o, σ4.kheap r = some o → (∀ y n, o ≠ .schedContext y n) → r ≠ scNameA m p := + fun r o h hn e => by rw [e, hk4] at h; cases h; exact hn _ _ rfl + have o2 : ∀ r o, σ1.kheap r = some o → (∀ y n, o ≠ .schedContext y n) → (∀ z, o ≠ .notification z) → + σ2.kheap r = some o := by + intro r o h hs hnn + rcases desc2 with ⟨-, e⟩ | ⟨-, -, -, xn, hxn, e⟩ + · rw [e]; exact h + · have a : r ≠ scNameA m p := fun e => by + obtain ⟨y, hy, -⟩ := rep1 _ _ _ hk + rw [e, hy] at h; cases h; exact hs _ _ rfl + have b : r ≠ specPtr (L.mem s1 (scWA p 40#u64)) := fun e => by rw [e, hxn] at h; cases h; exact hnn _ rfl + rw [e] + unfold scNtfnLinkStA + rw [updKSc_kheap, Function.update_of_ne a, updKSc_kheap, Function.update_of_ne b]; exact h + have tcbE : ∀ r z, σ.kheap r = some (.tcb z) → ∃ y o, + (updKSc σ4 (scNameA m p) (.schedContext (inactScA x4) n1)).kheap r = some (.tcb y) ∧ + ((i ≠ 0#u64 ∧ r = specPtr i → y = { yieldDoneA z d with tcbSchedContext := o }) ∧ + (¬ (i ≠ 0#u64 ∧ r = specPtr i) → y = { z with tcbSchedContext := o })) := by + intro r z h + obtain ⟨y1, h1, o, e1⟩ := K1.tcb r z h + have h2 := o2 r _ h1 (fun _ _ e => by cases e) (fun _ e => by cases e) + have h3 := desc3.1 r _ h2 (fun _ _ e => by cases e) (fun _ e => by cases e) + by_cases c : i ≠ 0#u64 ∧ r = specPtr i + · have h4 := (done c.1).2 y1 (by rw [← c.2]; exact h3) + rw [← c.2] at h4 + have a := ne5 r _ h4 (fun _ _ e => by cases e) + refine ⟨_, o, (e5 r a).trans h4, fun _ => ?_, fun n => absurd c n⟩ + rw [e1]; rfl + · have h4 : σ4.kheap r = some (.tcb y1) := by + rcases desc4 with ⟨-, e⟩ | ⟨h0, -⟩ + · rw [e]; exact h3 + · have a : r ≠ scNameA m p := fun e => by rw [e, hk3] at h3; cases h3 + rw [(done h0).1 r a (fun e => c ⟨h0, e⟩)]; exact h3 + have a := ne5 r _ h4 (fun _ _ e => by cases e) + exact ⟨y1, o, (e5 r a).trans h4, fun c' => absurd c' c, fun _ => e1⟩ + have keep45 : ∀ r o, σ3.kheap r = some o → (∀ y n, o ≠ .schedContext y n) → (∀ z, o ≠ .tcb z) → + (updKSc σ4 (scNameA m p) (.schedContext (inactScA x4) n1)).kheap r = some o := by + intro r o h hs ht + have h4 : σ4.kheap r = some o := by + rcases desc4 with ⟨-, e⟩ | ⟨h0, hi, -⟩ + · rw [e]; exact h + · have a : r ≠ scNameA m p := fun e => by rw [e, hk3] at h; cases h; exact hs _ _ rfl + obtain ⟨z, hz, -⟩ := J.sr.sch.tcb i hi + obtain ⟨y, hy, -⟩ := Y03.tcb _ _ hz + have b : r ≠ specPtr i := fun e => by rw [e, hy] at h; cases h; exact ht _ rfl + rw [(done h0).1 r a b]; exact h + rw [e5 r (ne5 r o h4 hs)]; exact h4 + have epE : ∀ r z, σ.kheap r = some (.endpoint z) → + (updKSc σ4 (scNameA m p) (.schedContext (inactScA x4) n1)).kheap r = some (.endpoint z) := by + intro r z h + have h1 := K1.obj r _ h (fun _ _ e => by cases e) (fun _ e => by cases e) + have h2 := o2 r _ h1 (fun _ _ e => by cases e) (fun _ e => by cases e) + have h3 := desc3.1 r _ h2 (fun _ _ e => by cases e) (fun _ e => by cases e) + exact keep45 r _ h3 (fun _ _ e => by cases e) (fun _ e => by cases e) + have ntE : ∀ r z, σ.kheap r = some (.notification z) → ∃ o, + (updKSc σ4 (scNameA m p) (.schedContext (inactScA x4) n1)).kheap r = + some (.notification { z with ntfnSc := o }) ∧ + (L.mem s1 (scWA p 40#u64) ≠ 0#u64 ∧ r = specPtr (L.mem s1 (scWA p 40#u64)) → o = none) ∧ + (¬ (L.mem s1 (scWA p 40#u64) ≠ 0#u64 ∧ r = specPtr (L.mem s1 (scWA p 40#u64))) → o = z.ntfnSc) := by + intro r z h + have h1 := K1.obj r _ h (fun _ _ e => by cases e) (fun _ e => by cases e) + have done2 : ∀ w : Notification, σ2.kheap r = some (.notification w) → + (updKSc σ4 (scNameA m p) (.schedContext (inactScA x4) n1)).kheap r = some (.notification w) := by + intro w h2 + have h3 := desc3.1 r _ h2 (fun _ _ e => by cases e) (fun _ e => by cases e) + exact keep45 r _ h3 (fun _ _ e => by cases e) (fun _ e => by cases e) + by_cases c : L.mem s1 (scWA p 40#u64) ≠ 0#u64 ∧ r = specPtr (L.mem s1 (scWA p 40#u64)) + · rcases desc2 with ⟨c0, -⟩ | ⟨-, -, -, xn, hxn, e⟩ + · exact absurd c0 c.1 + have : xn = z := by + rw [← c.2, h1] at hxn + cases hxn; rfl + subst this + have a : r ≠ scNameA m p := fun e' => by + obtain ⟨y, hy, -⟩ := rep1 _ _ _ hk + rw [e', hy] at h1; cases h1 + refine ⟨none, done2 _ ?_, fun _ => rfl, fun n => absurd c n⟩ + rw [e] + unfold scNtfnLinkStA + rw [updKSc_kheap, Function.update_of_ne a, updKSc_kheap, ← c.2, Function.update_self] + rfl + · refine ⟨z.ntfnSc, done2 _ ?_, fun c' => absurd c' c, fun _ => rfl⟩ + rcases desc2 with ⟨-, e⟩ | ⟨h0, -, -, xn, -, e⟩ + · rw [e]; exact h1 + · have a : r ≠ scNameA m p := fun e' => by + obtain ⟨y, hy, -⟩ := rep1 _ _ _ hk + rw [e', hy] at h1; cases h1 + rw [e] + unfold scNtfnLinkStA + rw [updKSc_kheap, Function.update_of_ne a, updKSc_kheap, Function.update_of_ne (fun e' => c ⟨h0, e'⟩)] + exact h1 + have reE : ∀ r z, σ.kheap r = some (.reply z) → ∃ o, + (updKSc σ4 (scNameA m p) (.schedContext (inactScA x4) n1)).kheap r = some (.reply { z with replySc := o }) ∧ + (L.mem s2 (scReplyA p) ≠ 0#u64 ∧ r = specPtr (L.mem s2 (scReplyA p)) → o = none) ∧ + (¬ (L.mem s2 (scReplyA p) ≠ 0#u64 ∧ r = specPtr (L.mem s2 (scReplyA p))) → o = z.replySc) := by + intro r z h + have h1 := K1.obj r _ h (fun _ _ e => by cases e) (fun _ e => by cases e) + have h2 := o2 r _ h1 (fun _ _ e => by cases e) (fun _ e => by cases e) + obtain ⟨o, h3, a, b⟩ := desc3.2.1 r z h2 + exact ⟨o, keep45 r _ h3 (fun _ _ e => by cases e) (fun _ e => by cases e), a, b⟩ + have scE : ∀ r y n, σ.kheap r = some (.schedContext y n) → ∃ y', + (updKSc σ4 (scNameA m p) (.schedContext (inactScA x4) n1)).kheap r = some (.schedContext y' n) ∧ + (r = scNameA m p → y'.scReplies = []) ∧ (r ≠ scNameA m p → y'.scReplies = y.scReplies) := by + intro r y n h + obtain ⟨y1, h1, e1⟩ := rep1 r y n h + obtain ⟨y2, h2, e2⟩ : ∃ y2, σ2.kheap r = some (.schedContext y2 n) ∧ y2.scReplies = y1.scReplies := by + rcases desc2 with ⟨-, e⟩ | ⟨-, -, -, xn, hxn, e⟩ + · exact ⟨y1, by rw [e]; exact h1, rfl⟩ + · have b : r ≠ specPtr (L.mem s1 (scWA p 40#u64)) := fun e' => by rw [e', hxn] at h1; cases h1 + by_cases a : r = scNameA m p + · subst a + rw [h1] at hk1 + have : y1 = x1 ∧ n = n1 := by + obtain ⟨u, hu, -⟩ := rep1 _ _ _ hk + cases hk1; exact ⟨rfl, rfl⟩ + obtain ⟨rfl, rfl⟩ := this + refine ⟨{ y1 with scNtfn := none }, ?_, rfl⟩ + rw [e] + unfold scNtfnLinkStA + rw [updKSc_kheap, Function.update_self] + rfl + · refine ⟨y1, ?_, rfl⟩ + rw [e] + unfold scNtfnLinkStA + rw [updKSc_kheap, Function.update_of_ne a, updKSc_kheap, Function.update_of_ne b] + exact h1 + obtain ⟨y3, h3, a3, b3⟩ := desc3.2.2 r y2 n h2 + by_cases c : r = scNameA m p + · subst c + rw [hk3] at h3 + cases h3 + have e4 : x4.scReplies = x3.scReplies := by + have h' := K4.2.1 (scNameA m p) + rw [hk3, hk4] at h' + cases h' with + | same => rfl + | sc _ _ _ q => exact q.2.1 + refine ⟨inactScA x4, by rw [updKSc_kheap, Function.update_self], fun _ => ?_, fun c => absurd rfl c⟩ + show x4.scReplies = [] + rw [e4]; exact a3 rfl + · have h4 : ∃ y4, σ4.kheap r = some (.schedContext y4 n) ∧ y4.scReplies = y3.scReplies := by + have h' := K4.2.1 r + rw [h3] at h' + generalize e : σ4.kheap r = o at h' + cases h' with + | same => exact ⟨y3, rfl, rfl⟩ + | sc _ z _ q => exact ⟨z, rfl, q.2.1⟩ + obtain ⟨y4, h4, e4⟩ := h4 + exact ⟨y4, (e5 r c).trans h4, fun c' => absurd c' c, fun _ => e4.trans ((b3 c).trans (e2.trans e1))⟩ + -- MCP and yield across `set_inactive` + have words6 : ∀ t ∈ Z.T, ∀ K : U64, K = 3440#u64 ∨ K = 3472#u64 ∨ K = 3456#u64 → + L.mem s6 (wa t K) = L.mem s4 (wa t K) := by + intro t ht K hK + rcases hK with rfl | rfl | rfl <;> exact offT t ht _ (by simp) (by simp) (by simp) s4 s6 F6.wr + obtain ⟨M6, R6⟩ := yieldFrameA (mem' := L.mem s6) (σ' := updKSc σ4 (scNameA m p) (.schedContext (inactScA x4) n1)) + M4 R4 (fun t ht => ⟨words6 t ht _ (Or.inl rfl), words6 t ht _ (Or.inr (Or.inr rfl))⟩) + (fun t ht => words6 t ht _ (Or.inr (Or.inl rfl))) + (fun t _ z hz => ⟨z, (e5 _ (ne5 _ _ hz (fun _ _ e => by cases e))).trans hz, rfl, rfl, rfl⟩) + (fun c hc => by + obtain ⟨z, k, hz, -⟩ := H4.scr.sc c hc + by_cases e : scNameA m c = scNameA m p + · rw [e] at hz ⊢ + rw [hk4] at hz; cases hz + exact ⟨x4, n1, inactScA x4, hk4, by rw [updKSc_kheap, Function.update_self], rfl⟩ + · exact ⟨z, k, z, hz, (e5 _ e).trans hz, rfl⟩) + have END : ScFinEndA st Z N Rp m s s6 σ (updKSc σ4 (scNameA m p) (.schedContext (inactScA x4) n1)) p + (L.mem s1 (scWA p 40#u64)) (L.mem s2 (scReplyA p)) i d := by + refine ⟨wrAll, ntf, rp, ?_, fun h0 => ⟨(yT h0).1, (yT h0).2.1, (yT h0).2.2, v272 h0⟩, tcbE, epE, ntE, reE, scE, + M6, R6⟩ + obtain ⟨y1, h1, e1⟩ := rep1 _ _ _ hk + refine ⟨xs, ns, hk, ?_⟩ + have e2 : x2.scReplies = xs.scReplies := by + rcases desc2 with ⟨-, e⟩ | ⟨-, -, -, xn, hxn, e⟩ + · rw [e, h1] at hk2 + cases hk2; exact e1 + · have h2 := hk2 + rw [e] at h2 + unfold scNtfnLinkStA at h2 + rw [updKSc_kheap, Function.update_self] at h2 + rw [h1] at hk1 + cases h2 + cases hk1 + exact e1 + rw [← e2]; exact hhead + refine (WP.spec_ok _).2 ⟨rfl, ea2.trans ea1, σ1, σ2, σ3, σ4, _, Q1, R1, ?_, ?_, ?_, ?_, ?_, H5, L4.inactScA hk4 hni', + ?_, F5, _, _, _, _, END⟩ + · rw [← hnm]; exact m1 + · rw [← hnm]; exact m2 + · rw [← hnm, sp3]; exact rfl + · rw [← hnm]; exact m4 + · rw [← hnm, sp5]; exact rfl + · intro u c + have n1' : NoSwA σ1 := S1.ns J.noSwitch + have e2 : σ2.schedulerAction = σ1.schedulerAction := by rw [K2.1] + have e4 : σ4.schedulerAction = σ3.schedulerAction := by rw [K4.1] + exact n1' u (by rw [← e2, ← L3.2, ← e4]; exact c) + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelScHookA.lean b/proofs/Fl4ProofsA64/P3/DelScHookA.lean new file mode 100644 index 000000000..a60166fb3 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelScHookA.lean @@ -0,0 +1,123 @@ +/- +P3 (AArch64, delete hooks W4): **the SchedContext finaliser hook** `finHookScA : FinHookKA .schedContext`, at the +`W` / `Wx` / `Rd` / `Rz` of the other kinds. The cap's typing `H p = some (.sc _)` is the record's `capTy`, and that it +does not name the current core's idle SC is `ciSc`. A non-final cap is a no-op; a final one is `scFin_recA`. + +Premises: [P] `hA`, `hCs`, `hmax`, `htu`; [S] `h1`. New module. +-/ +import Fl4ProofsA64.P3.DelScRecA +import Fl4ProofsA64.P3.DelNtfnHookA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 Fl4ProofsA64.P3.Endp +open Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Notif + +local notation "wa" => core.num.U64.wrapping_add + +noncomputable section + +section +variable {m : Fl4SpecA64.McState} +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Pp Tb : Set U64} {Lv : U64 → Nat} {ia ie : Nat} + {N E Rp : Set U64} {κ : U64 → U64} {n : Nat} + +/-- **The SchedContext finaliser hook** (module docstring). -/ +theorem finHookScA {ipa : U64} (s0 : S) + (Rv : S → A → State Unit → MdbGhostA → U64 → Prop) + (hA : P0.ArchTotal ar) (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (h1 : fl4A64Platform.numNodes = 1) : + FinHookKA st .schedContext ar Z.th OS fl4FinaliseCap + (DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st (J7R2A st ar ipa)))) + (DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st J7R2tA))) + (DelRdXA st ar Z H OS ipa Rv) (DelRzA st) := by + intro s a σ G sl c ac f D hW hsl hc hk hg hac hf + obtain ⟨p, b, rfl⟩ : ∃ p b, c = .SchedContext p b := by + cases c <;> simp [finKindA] at hk + exact ⟨_, _, rfl⟩ + have hf := hf trivial + have eac : ac = absCapA (.SchedContext p b) := hac.symm + obtain ⟨hcore, ⟨Q, R, hw, Ac, Bf, stack, J, X⟩, hPA, hZT, hBH, hCT, hFx⟩ := hW + have hOSx : cslotAddrA sl ∈ OS := D.mem_slot hsl + have hH : H p = some (.sc b.val) := (J.capTy _ hOSx).2.2.2.1 p b hc + have hni := J.ciSc _ hOSx p b hc + have hp : p ∈ Z.C := by + rw [← J.scs] + first + | exact ⟨_, hH⟩ + | (change H p ∈ _; rw [hH]; simp) + | simp [Heap.scs, hH] + have unread : ¬ S1aR2Reads (capability.Cap.SchedContext p b) := fun h => h + have rd : ∀ (s' : S) (a' : A) (σ' : State Unit) (G' : MdbGhostA) (Z' : XSetsA) (m' : McState), + capA st s' (cslotAddrA sl) = .SchedContext p b → + J7LA st (J7R2A st ar ipa) Z' H OS s' σ' m' → + J7LA st (J7R2A st ar ipa) Z' H OS s' σ' m' ∧ + (DelRdA st Rv s' a' σ' G' (cslotAddrA sl) ∧ RdThA st OS s' (cslotAddrA sl)) ∧ + J7LA st (J7R2xA st ar ipa (cslotAddrA sl)) Z' H OS s' σ' m' := by + intro s' a' σ' G' Z' m' e j + refine ⟨j, ⟨delRdA_other (fun r g h => by rw [e] at h; cases h) (fun r g h => by rw [e] at h; cases h) + (fun v h => by rw [e] at h; cases h), rdThA_of_ne fun t h => by rw [e] at h; cases h⟩, + j.pinUnreadA (by rw [e]; exact unread)⟩ + have plain : ∀ d, fl4FinaliseCap (Z := Unit) ac d = finaliseCap ac d := fun d => + fl4FinaliseCap_plainA (by rw [eac]; intro h; exact h) d + rw [fin_cap_eq_obj (by simp)] + cases f + · -- not final: nothing happens + have e : objects.finalise_cap_object st ar s a (.SchedContext p b) false = + ok ((capability.Cap.Null, capability.Cap.Null), s, a) := rfl + have hnoop : ((Fl4SpecA64.Cap.nullCap, Fl4SpecA64.Cap.nullCap), σ) ∈ + (fl4FinaliseCap (Z := Unit) ac false σ).1 := by + have skip : ∀ f : SMonad Unit Unit, ((), σ) ∈ (NonDet.when (false = true) f σ).1 := fun f => by + unfold NonDet.when + rw [if_neg (by simp)] + exact rfl + rw [plain, eac] + simp only [absCapA, finaliseCap] + exact mem_bindA.2 ⟨((), σ), skip _, mem_bindA.2 ⟨((), σ), skip _, mem_bindA.2 ⟨((), σ), skip _, + mem_bindA.2 ⟨((), σ), skip _, mem_bindA.2 ⟨((), σ), skip _, rfl⟩⟩⟩⟩⟩ + rw [e, WP.spec_ok] + have rr := rd s a σ G _ m hc J.j7 + refine ⟨rfl, trivial, trivial, σ, G, ⟨false, hf, hnoop⟩, D, hc, fun y _ _ => Or.inl rfl, ?_, ?_, + fun _ _ _ h => by cases h⟩ + · exact ⟨fun h => absurd (Or.inl rfl) h, fun _ => ⟨hcore, ⟨Q, R, hw, Ac, Bf, stack, + J.mapJ7A fun _ _ => trivial, X⟩, hPA, hZT, hBH, hCT, hFx⟩⟩ + · exact fun _ => ⟨rr.2.1, m, rr.2.2⟩ + · -- final + refine WP.spec_mono (J.scFin_recA (b := b) hp hni hA hCs h1 hmax htu) ?_ + rintro ⟨⟨rem, cl⟩, s', a'⟩ ⟨h0, ha, σ1, σ2, σ3, σ4, σ', Q', R', Ac', stack', m1, m2, m3, m4, m5, J', k, kb, cpu, + F, vc, k6⟩ + dsimp only at h0 ha J' k kb cpu vc k6 ⊢ + obtain ⟨rfl, rfl⟩ := Prod.mk.inj h0 + subst ha + have caps : ∀ y ∈ OS, capA st s' y = capA st s y := fun y hy => k.capA hy + have hc' : capA st s' (cslotAddrA sl) = .SchedContext p b := (caps _ hOSx).trans hc + have hmem : ((Fl4SpecA64.Cap.nullCap, Fl4SpecA64.Cap.nullCap), σ') ∈ + (fl4FinaliseCap (Z := Unit) ac true σ).1 := by + have run : ∀ (f : SMonad Unit Unit) (τ τ' : State Unit), ((), τ') ∈ (f τ).1 → + ((), τ') ∈ (NonDet.when (true = true) f τ).1 := fun f τ τ' h => by + unfold NonDet.when + rw [if_pos rfl] + exact h + rw [plain, eac] + simp only [absCapA, finaliseCap] + exact mem_bindA.2 ⟨((), σ1), run _ _ _ m1, mem_bindA.2 ⟨((), σ2), run _ _ _ m2, + mem_bindA.2 ⟨((), σ3), run _ _ _ m3, mem_bindA.2 ⟨((), σ4), run _ _ _ m4, + mem_bindA.2 ⟨((), σ'), run _ _ _ m5, rfl⟩⟩⟩⟩⟩ + have D' : DStateA st Z.th OS s' σ' G := DStateA.keepOSA (Z := { Z with G := G, Q := Q', R := R', hw := hw }) D J' k + have rr := rd s' a' σ' G _ m hc' J'.j7 + have W' : DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st J7R2tA)) s' a' σ' G := + ⟨⟨kb.trans hcore.1, cpu.trans hcore.2⟩, ⟨Q', R', hw, Ac', Bf, stack', J'.mapJ7A fun _ _ => trivial, (k6 D.sub (X.ghostA G Q R hw)).ghostA Z.G Z.Q Z.R Z.hw⟩, + ParentAdjA.keepOS D.lk k hPA, + ZombieTcbNoVcpuA.keepA caps J.zombieTcb (fun t ht => vc t (J.tcbs ▸ ht)) hZT, + bufHold_sameA st hBH caps, ctOff_sameA st hCT kb caps, hFx.keepA st D.sub caps⟩ + refine ⟨rfl, trivial, trivial, σ', G, ⟨true, hf, hmem⟩, D', hc', fun y hy _ => Or.inl (caps y hy), + ⟨fun h => absurd (Or.inl rfl) h, fun _ => W'⟩, fun _ => ⟨rr.2.1, m, rr.2.2⟩, fun _ _ _ h => by cases h⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelScInactA.lean b/proofs/Fl4ProofsA64/P3/DelScInactA.lean new file mode 100644 index 000000000..a6eced9fc --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelScInactA.lean @@ -0,0 +1,240 @@ +/- +P3 (AArch64, delete hooks W4, SC finaliser): **`set_refill_max sc 0; set_sporadic sc 0` refines +`sched_context_set_inactive`** on `SchedSRA`, for an SC with no bound TCB (after `unbind_all_tcbs`) that is no core's +idle SC. + +* `inactScA`, `schedContextSetInactive_eqA`: the single spec outcome. +* `SchedSRA.scInactA`: the SC object made inactive (a copy of `P3/ScConsA`'s `SchedSRA.scObjA` for an SC no TCB is + bound to, so every clause about an active SC is vacuous at it). +* `sc_set_inactiveA`: the concrete refinement. + +Hypotheses: `hni` (not an idle SC), `hnt` (no bound TCB). No named premise. New module. +-/ +import Fl4ProofsA64.P3.ScConsA +import Fl4ProofsA64.P3.ScReplyA +import Fl4ProofsA64.P3.ScSpecA +import Fl4ProofsA64.P1.QueueFinSc +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-- The SC `sched_context_set_inactive` leaves. -/ +def inactScA (x : Fl4SpecA64.SchedContext) : Fl4SpecA64.SchedContext := + { x with scRefills := [], scRefillMax := 0, scBudget := 0, scSporadic := false } + +theorem schedContextSetInactive_eqA {σ : Fl4SpecA64.State Unit} {p : Fl4SpecA64.ObjRef} + {x : Fl4SpecA64.SchedContext} {n : Nat} (hk : σ.kheap p = some (.schedContext x n)) : + Fl4SpecA64.schedContextSetInactive (Z := Unit) p σ = ({((), updKSc σ p (.schedContext (inactScA x) n))}, False) := by + have step : ∀ (y z : Fl4SpecA64.SchedContext), + (updKSc (updKSc σ p (.schedContext y n)) p (.schedContext z n)) = updKSc σ p (.schedContext z n) := by + intro y z + simp [updKSc, Function.update_idem] + have at_ : ∀ y : Fl4SpecA64.SchedContext, + (updKSc σ p (.schedContext y n)).kheap p = some (.schedContext y n) := fun y => by simp [updKSc] + unfold Fl4SpecA64.schedContextSetInactive + rw [nd_bind_oneSc (updateSchedContext_eqA hk _), nd_bind_oneSc (updateSchedContext_eqA (at_ _) _), step, + nd_bind_oneSc (updateSchedContext_eqA (at_ _) _), step, updateSchedContext_eqA (at_ _), step] + rfl + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **One SC no TCB is bound to made inactive** (module docstring). -/ +theorem SchedSRA.scInactA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s s' : S} + {σ : Fl4SpecA64.State Unit} (H : SchedSRA st Z m κ s σ) {sc : U64} (hc : sc ∈ Z.C) + {x : Fl4SpecA64.SchedContext} {n : Nat} (hk : σ.kheap (scNameA m sc) = some (.schedContext x n)) + (hnt : x.scTcb = none) + (hw : WritesIn st s s' (ScObjA sc n)) (hx' : ScRelWA st (IdleScA m sc) s' sc (inactScA x) n) + (hcc : L.mem s' (scCoreA sc) = L.mem s (scCoreA sc)) : + SchedSRA st Z m κ s' (updKSc σ (scNameA m sc) (.schedContext (inactScA x) n)) := by + obtain ⟨x', hx'e⟩ : ∃ x', x' = inactScA x := ⟨_, rfl⟩ + rw [← hx'e] at hx' ⊢ + have e3 : x'.scPeriod = x.scPeriod := by rw [hx'e]; rfl + have e4 : x'.scTcb = x.scTcb := by rw [hx'e]; rfl + have hoff : Fl4SpecA64.scActive x' = false := by + rw [hx'e]; simp [Fl4SpecA64.scActive, Fl4SpecA64.activeSc, inactScA] + have hnb : ∀ u xu, σ.kheap u = some (.tcb xu) → xu.tcbSchedContext ≠ some (scNameA m sc) := by + intro u xu hu hp + obtain ⟨y, k, hy, hyu⟩ := H.inv.back u xu _ hu hp + rw [hk] at hy; cases hy + rw [hnt] at hyu; cases hyu + set σ' := updKSc σ (scNameA m sc) (.schedContext x' n) with hσ' + have hr56 : layout.node.REPROGRAM.val = 56 := by unfold layout.node.REPROGRAM; rfl + have hw' : WritesIn st s s' (fun b => ScFootA Z.C m σ b ∨ b = L.nodeAt s (L.cpu s) layout.node.REPROGRAM) := + hw.mono fun b hb => Or.inl ⟨sc, hc, x, n, hk, hb⟩ + obtain ⟨nK, gK, tK⟩ := H.keepW hw' (by rw [hr56]; omega) (fun b hb => hb) + have hkN' : σ'.kheap (scNameA m sc) = some (.schedContext x' n) := by simp [σ', updKSc] + have hne : ∀ q o, σ.kheap q = some o → (∀ y k, o ≠ .schedContext y k) → q ≠ scNameA m sc := + fun q o hq ho e => by rw [e, hk] at hq; cases hq; exact ho x n rfl + have hobj : ∀ q o, σ.kheap q = some o → (∀ y k, o ≠ .schedContext y k) → σ'.kheap q = some o := + fun q o hq ho => by simp [σ', updKSc, hne q o hq ho, hq] + have hkt : ∀ q u, σ.kheap q = some (.tcb u) → σ'.kheap q = some (.tcb u) := + fun q u hq => hobj q _ hq (fun _ _ h => by cases h) + have hkt' : ∀ q u, σ'.kheap q = some (.tcb u) → σ.kheap q = some (.tcb u) := by + intro q u hq + by_cases e : q = scNameA m sc + · rw [e] at hq; simp [σ', updKSc] at hq + · simpa [σ', updKSc, e] using hq + have hSC : ∀ p y k, σ'.kheap p = some (.schedContext y k) → + σ.kheap p = some (.schedContext y k) ∨ (p = scNameA m sc ∧ y = x' ∧ k = n) := by + intro p y k hp + by_cases e : p = scNameA m sc + · rw [e] at hp; simp [σ', updKSc] at hp; exact Or.inr ⟨e, hp.1.symm, hp.2.symm⟩ + · left; simpa [σ', updKSc, e] using hp + -- `specRtA` of a TCB bound to an active SC + have hrt : ∀ u xu p y k, σ.kheap u = some (.tcb xu) → xu.tcbSchedContext = some p → + σ.kheap p = some (.schedContext y k) → Fl4SpecA64.scActive y = true → specRtA σ' u = specRtA σ u := by + intro u xu p y k hu hp hy ha + have hne0 := H.inv.ne hy ha + have e : p ≠ scNameA m sc := fun e => hnb u xu hu (e ▸ hp) + rw [specRtA_of hu hp hy hne0] + rw [specRtA_of (hkt _ _ hu) hp (by simpa [σ', updKSc, e] using hy) hne0] + have hinv : ScInvA m σ' := by + refine ⟨fun p y k hy ha hp => ?_, fun p y k hy ha hp => ?_, fun q z hz => H.inv.ntfnIdle q z ?_, + fun q z hz => H.inv.ntfnIdleOf q z ?_, fun u hu => ?_, ?_, fun u xu p hu hp => ?_, + fun u xu p hu hp => H.inv.tcbScNotIdle u xu p (hkt' _ _ hu) hp⟩ + · rcases hSC p y k hy with h | ⟨-, ey, -⟩ + · exact H.inv.refills p y k h ha hp + · rw [ey, hoff] at ha; cases ha + · rcases hSC p y k hy with h | ⟨-, ey, -⟩ + · exact H.inv.rr p y k h ha hp + · rw [ey, hoff] at ha; cases ha + · by_cases e : q = scNameA m sc + · rw [e] at hz; simp [σ', updKSc] at hz + · simpa [σ', updKSc, e] using hz + · by_cases e : q = scNameA m sc + · rw [e] at hz; simp [σ', updKSc] at hz + · simpa [σ', updKSc, e] using hz + · obtain ⟨xu, p, y, k, h1, h2, h3, h4⟩ := H.inv.relSc u hu + have e : p ≠ scNameA m sc := fun e => hnb u xu h1 (e ▸ h2) + exact ⟨xu, p, y, k, hkt _ _ h1, h2, by simpa [σ', updKSc, e] using h3, h4⟩ + · show σ.releaseQueue.Pairwise _ + refine List.Pairwise.imp_of_mem (fun {a b} ha hb hab => ?_) H.inv.sorted + obtain ⟨xa, pa, ya, ka, a1, a2, a3, a4⟩ := H.inv.relSc a ha + obtain ⟨xb, pb, yb, kb, b1, b2, b3, b4⟩ := H.inv.relSc b hb + rw [hrt a xa pa ya ka a1 a2 a3 a4, hrt b xb pb yb kb b1 b2 b3 b4]; exact hab + · obtain ⟨y, k, hy, hyu⟩ := H.inv.back u xu p (hkt' _ _ hu) hp + have e : p ≠ scNameA m sc := fun e => hnb u xu (hkt' _ _ hu) (e ▸ hp) + exact ⟨y, k, by simpa [σ', updKSc, e] using hy, hyu⟩ + have hrd : RdA σ' := by + intro u xu hu hc x0 scp y k h1 h2 h3 h4 h5 + have hu0 := hkt' _ _ hu + have h10 := hkt' _ _ h1 + have e : scp ≠ scNameA m sc := fun e => hnb u x0 h10 (e ▸ h2) + exact H.rd u xu hu0 hc x0 scp y k h10 h2 (by simpa [σ', updKSc, e] using h3) h4 h5 + have hkc : ∀ c ∈ Z.C, ∀ y k, σ.kheap (scNameA m c) = some (.schedContext y k) → + ∃ y', σ'.kheap (scNameA m c) = some (.schedContext y' k) := by + intro c _ y k hy + by_cases e : scNameA m c = scNameA m sc + · rw [e, hk] at hy; cases hy; exact ⟨x', by rw [e]; simp [σ', updKSc]⟩ + · exact ⟨y, by simpa [σ', updKSc, e] using hy⟩ + obtain ⟨hS, hP⟩ := H.scr.upd H.scp hc hk hw hx' + have tw : ∀ t ∈ Z.T, ∀ K : U64, (K = 3520#u64 ∨ K = 3448#u64 ∨ K = 3432#u64 ∨ K = 3464#u64 ∨ K = 3592#u64 ∨ + K = 3480#u64 ∨ K = 3488#u64) → L.mem s' (wa t K) = L.mem s (wa t K) := fun t ht K hK => + tK t ht K (by rcases hK with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> simp) + (by rcases hK with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> simp) + have hcore : SchedCoreA st Z.T Z.Cs s' Z.Q Z.R := + H.core.agree hw.1 (fun u hu => ⟨tw u hu 3480#u64 (by simp), tw u hu 3488#u64 (by simp)⟩) + (fun c hc o h1 h2 h3 => nK c hc o h1 h2 (by rw [hr56]; omega)) (fun u hu => tw u hu 3592#u64 (by simp)) + (fun u hu => tw u hu 3520#u64 (by simp)) (fun u hu => tw u hu 3448#u64 (by simp)) + refine H.rebuildS (Q' := Z.Q) hw.1 hw.2.1 (fun c hc o h1 h2 h3 h4 => nK c hc o (by omega) h2 (by rw [hr56]; exact h4)) + (fun K hK => gK K (by rcases hK with rfl | rfl <;> simp) (by rcases hK with rfl | rfl <;> simp)) + (fun t ht K hK => tw t ht K (by rcases hK with h | h | h | h <;> simp [h])) hS hP + (fun t ht u hu => ⟨u, hkt _ u hu, rfl, rfl, rfl⟩) ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ + (by + show σ.reprogramTimer = _ + rw [hw.mem_eq H.reprA.1 (fun hb => H.reprA.2 _ ⟨sc, hc, x, n, hk, hb⟩ rfl)] + exact H.rest.repr) + (by + show σ.schedulerAction = _ + rw [nK _ H.sch.cpuCs layout.node.SCHED_ACTION (by simp [layout.node.SCHED_ACTION]) + (by simp [layout.node.SCHED_ACTION]) (by rw [hr56]; simp [layout.node.SCHED_ACTION])] + exact H.sch.act) + H.actOn hrd hinv hcore (fun p hp => H.rest.rq p hp) H.rest.rqDom rfl + ⟨fun d p u hu => let ⟨w, h1, h2⟩ := H.run.1 d p u hu; ⟨w, hkt _ w h1, h2⟩, + fun u hu => let ⟨w, h1, h2⟩ := H.run.2 u hu; ⟨w, hkt _ w h1, h2⟩⟩ + (fun c' hc' => by + by_cases e : c' = sc + · rw [e]; exact hcc + obtain ⟨y', k, hky, hy⟩ := H.scr.sc c' hc' + have h16 := hy.hdr (K := 16#u64) (by simp) (by simp) + have hcE : scCoreA c' = scWA c' 16#u64 := by unfold scCoreA scWA layout.sc.CORE; rfl + refine hw.mem_eq (H.scCoreFoot hc').1 (fun ho => ?_) + rw [hcE] at ho; exact H.scp.disj c' hc' sc hc e y' k x n hky hk _ (hy.in_obj h16.2) ho) + +/-- **`set_refill_max sc 0; set_sporadic sc 0` refines `sched_context_set_inactive`** (module docstring). -/ +theorem sc_set_inactiveA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} + {σ : Fl4SpecA64.State Unit} (H : SchedSRA st Z m κ s σ) {sc : U64} (hc : sc ∈ Z.C) (hni : ¬ IdleScA m sc) + (hnt : ∀ x n, σ.kheap (scNameA m sc) = some (.schedContext x n) → x.scTcb = none) : + (do + let s1 ← obj.Sc.set_refill_max st sc s 0#u64 + obj.Sc.set_sporadic st sc s1 0#u64) ⦃ s' => ∃ x n, + σ.kheap (scNameA m sc) = some (.schedContext x n) ∧ + Fl4SpecA64.schedContextSetInactive (Z := Unit) (scNameA m sc) σ = + ({((), updKSc σ (scNameA m sc) (.schedContext (inactScA x) n))}, False) ∧ + SchedSRA st Z m κ s' (updKSc σ (scNameA m sc) (.schedContext (inactScA x) n)) ∧ + WritesIn st s s' (fun b => b = scWA sc 72#u64 ∨ b = scWA sc 64#u64) ∧ + MFrA (st := st) Z m s s' σ (updKSc σ (scNameA m sc) (.schedContext (inactScA x) n)) ∧ + L.mem s' (scWA sc 72#u64) = 0#u64 ∧ L.mem s' (scWA sc 64#u64) = 0#u64 ⦄ := by + obtain ⟨x, n, hk, hx⟩ := H.scr.sc sc hc + have e72 : maxA sc = scWA sc 72#u64 := maxA_eqS sc + have e64 : wa sc layout.sc.SPORADIC = scWA sc 64#u64 := by unfold scWA layout.sc.SPORADIC; rfl + have h72 := hx.hdr (K := 72#u64) (by simp) (by simp) + have h64 := hx.hdr (K := 64#u64) (by simp) (by simp) + obtain ⟨-, -, hfit⟩ := hx.bounds + have val : ∀ K : U64, K.val < 96 → (scWA sc K).val = sc.val + K.val := fun K hK => scWA_val (by omega) + refine WP.spec_bind (obj.Sc.set_refill_max.spec s sc 0#u64) ?_ + rintro s1 rfl + refine WP.spec_mono (obj.Sc.set_sporadic.wr _ sc 0#u64) ?_ + rintro s2 rfl + rw [e72, e64] + set s2 := L.wr (L.wr s (scWA sc 72#u64) 0#u64) (scWA sc 64#u64) 0#u64 with hs2 + have wr : WritesIn st s s2 (fun b => b = scWA sc 72#u64 ∨ b = scWA sc 64#u64) := + ((writesIn_wr (st := st) (s := s) (v := 0#u64) h72.1).trans + (writesIn_wr (st := st) (v := 0#u64) h64.1)).mono fun _ h => h + have wrO : WritesIn st s s2 (ScObjA sc n) := wr.mono fun b hb => by + rcases hb with rfl | rfl + · exact hx.in_obj h72.2 + · exact hx.in_obj h64.2 + have keep : ∀ K : U64, K.val < 96 → K.val % 8 = 0 → K.val ≠ 72 → K.val ≠ 64 → + L.mem s2 (scWA sc K) = L.mem s (scWA sc K) := by + intro K h1 h2 h3 h4 + refine wr.mem_eq (hx.hdr h1 h2).1 ?_ + rintro (e | e) <;> have := congrArg (fun w : U64 => w.val) e <;> simp only [val K h1] at this <;> + rw [val _ (by simp)] at this <;> simp at this <;> omega + have z64 : L.mem s2 (scWA sc 64#u64) = 0#u64 := L.mem_wr_same h64.1 + have z72 : L.mem s2 (scWA sc 72#u64) = 0#u64 := by + rw [hs2, L.mem_wr_ne h64.1 h72.1 (by rw [val _ (by simp), val _ (by simp)]; simp)] + exact L.mem_wr_same h72.1 + have em : L.rmax s2 sc = 0#u64 := by simp only [LawfulStore.rmax, maxA_eqS]; exact z72 + have ep : L.rperiod s2 sc = L.rperiod s sc := by + simp only [LawfulStore.rperiod, periodA_eqS]; exact keep _ (by simp) (by simp) (by simp) (by simp) + have rel : ScRelWA st (IdleScA m sc) s2 sc (inactScA x) n := + ⟨hx.pl, by rw [ep]; exact hx.period, + by rw [keep 8#u64 (by simp) (by simp) (by simp) (by simp)]; exact hx.consumed, + by rw [keep 24#u64 (by simp) (by simp) (by simp) (by simp)]; exact hx.tcb, + by rw [keep 32#u64 (by simp) (by simp) (by simp) (by simp)]; exact hx.reply, + by rw [keep 40#u64 (by simp) (by simp) (by simp) (by simp)]; exact hx.ntfn, + by rw [keep 48#u64 (by simp) (by simp) (by simp) (by simp)]; exact hx.badge, + by rw [keep 56#u64 (by simp) (by simp) (by simp) (by simp)]; exact hx.yieldFrom, + by rw [z64]; simp [inactScA], fun h => absurd z64 h, fun h => absurd h hni, + by rw [em]; simp [inactScA], fun h => absurd (by rw [em]; simp) h⟩ + have hcc : L.mem s2 (scCoreA sc) = L.mem s (scCoreA sc) := by + rw [show scCoreA sc = scWA sc 16#u64 by unfold scCoreA scWA layout.sc.CORE; rfl] + exact keep _ (by simp) (by simp) (by simp) (by simp) + have H' := H.scInactA hc hk (hnt x n hk) wrO rel hcc + refine ⟨x, n, hk, schedContextSetInactive_eqA hk, H', wr, ?_, z72, z64⟩ + refine ⟨wrO.mono fun b hb => mwr_scObj hc hk hb, SKeepA.of_obj fun q o hq ho => ?_, SSzA.of_upd hk rfl⟩ + have hne : q ≠ scNameA m sc := fun e => by rw [e, hk] at hq; cases hq; exact ho x n rfl + simp [updKSc, hne, hq] + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/DelScRecA.lean b/proofs/Fl4ProofsA64/P3/DelScRecA.lean new file mode 100644 index 000000000..9b544c02f --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelScRecA.lean @@ -0,0 +1,833 @@ +/- +P3 (AArch64, delete hooks W4, SC finaliser): **the record across the final SC finaliser**: `JStage3TA` is rebuilt at +the end state of the five steps (`ScFinEndA`, `DelScChainA`). The finalised SC's reply stack becomes empty +(`ReplyStackRelA.unbindHead`), the active SC set may lose the SC (`BlkOK` from P1's `fin_sc.blk`), the unbound +notification loses its SC and a pending yielder is told the consumed time. + +Premises: [P] `hA`, `hCs`, `hmax`, `htu`; [S] `h1`. New module. +-/ +import Fl4ProofsA64.P3.DelRec6A +import Fl4ProofsA64.P3.Stage3QKeepPresA +import Fl4ProofsA64.P3.DelScChainA +import Fl4ProofsA64.P3.DelRvVrA +import Fl4ProofsA64.P3.IpcCancelA +import Fl4ProofsA64.P3.EpCopyMrsA +import Fl4ProofsA64.P3.IpcScGlueA +import Fl4ProofsA64.P3.IpcScPopLinksA +import Fl4ProofsA64.P3.Stage3SchedWrSTA +import Fl4ProofsA64.P3.DelBlkKeepA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 Fl4ProofsA64.P3.Endp +open Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Notif + +local notation "wa" => core.num.U64.wrapping_add + +noncomputable section + +/-- **The reply-stack relation after `sched_context_unbind_reply`**: the SC's whole stack is detached, only the +head's `next` word is cleared. -/ +theorem ReplyStackRelA.unbindHeadA {R C : Set U64} {mem mem' : U64 → U64} {σ σ' : State Unit} + {scRef : U64 → ObjRef} {Lst : U64 → List U64} + (h : ReplyStackRelA mem R C σ scRef Lst) (hR0 : ∀ d ∈ R, d ≠ 0#u64) (hEv : ∀ d ∈ R, d.val % 2 = 0) + {sc hd : U64} (hsc : sc ∈ C) (hh : (Lst sc).head?.map specPtr = optRefS hd) + (hmP : ∀ d ∈ R, mem' (rPrevA d) = mem (rPrevA d)) + (hmN : ∀ d ∈ R, ¬ (hd ≠ 0#u64 ∧ d = hd) → mem' (rNextA d) = mem (rNextA d)) + (hmH : hd ≠ 0#u64 → mem' (rNextA hd) = 0#u64) + (hk' : ∃ y n, σ'.kheap (scRef sc) = some (.schedContext y n) ∧ y.scReplies = []) + (hkq : ∀ q ∈ C, q ≠ sc → ∀ x n, σ.kheap (scRef q) = some (.schedContext x n) → + ∃ y k, σ'.kheap (scRef q) = some (.schedContext y k) ∧ y.scReplies = x.scReplies) + (hRep : ∀ d ∈ R, ∃ x, σ.kheap (specPtr d) = some (.reply x)) + (hre : ∀ d ∈ R, ∀ x, σ.kheap (specPtr d) = some (.reply x) → ∃ o, + σ'.kheap (specPtr d) = some (.reply { x with replySc := o }) ∧ + (hd ≠ 0#u64 ∧ specPtr d = specPtr hd → o = none) ∧ (¬ (hd ≠ 0#u64 ∧ specPtr d = specPtr hd) → o = x.replySc)) : + ReplyStackRelA mem' R C σ' scRef (Function.update Lst sc []) := by + have hL'sc : Function.update Lst sc [] sc = [] := Function.update_self _ _ _ + have hL'q : ∀ q, q ≠ sc → Function.update Lst sc [] q = Lst q := fun q hq => Function.update_of_ne hq _ _ + obtain ⟨-, hLR, hLnd, hLw⟩ := h.stackRel sc hsc + have hsub : ∀ q ∈ C, ∀ d ∈ Function.update Lst sc [] q, q ≠ sc ∧ d ∈ Lst q := by + intro q _ d hd' + by_cases e : q = sc + · subst e; rw [hL'sc] at hd'; cases hd' + · rw [hL'q q e] at hd'; exact ⟨e, hd'⟩ + have off : ∀ d, d ∈ Lst sc → ∀ q ∈ C, d ∉ Function.update Lst sc [] q := by + intro d hm q hq h' + obtain ⟨e, h2⟩ := hsub q hq d h' + exact e (h.unique q hq sc hsc d h2 hm) + -- the head word + have head : hd ≠ 0#u64 → ∃ rest, Lst sc = hd :: rest := by + intro h0 + rw [optRefS_ne h0] at hh + cases hl : Lst sc with + | nil => rw [hl] at hh; cases hh + | cons a l => + rw [hl] at hh + exact ⟨l, by rw [specPtr_inj (Option.some.inj hh)]⟩ + have empty : hd = 0#u64 → Lst sc = [] := by + intro h0 + rw [h0, optRefS_zero] at hh + cases hl : Lst sc with + | nil => rfl + | cons a l => rw [hl] at hh; cases hh + have inL : ∀ d, hd ≠ 0#u64 ∧ d = hd → d ∈ Lst sc := by + rintro d ⟨h0, rfl⟩ + obtain ⟨rest, e⟩ := head h0 + rw [e]; simp + have nx : ∀ d ∈ R, d ∉ Lst sc → mem' (rNextA d) = mem (rNextA d) := + fun d hd' hn => hmN d hd' fun c => hn (inL d c) + refine ⟨fun q hq => ?_, fun q hq q' hq' d h1 h2 => ?_, fun d hd' x' hx' p => ?_, fun d hd' hdd => ?_⟩ + · by_cases e : q = sc + · subst e + obtain ⟨y, n, hy, hl⟩ := hk' + refine ⟨⟨y, n, hy, by rw [hL'sc]; exact hl⟩, by rw [hL'sc]; simp, by rw [hL'sc]; exact List.nodup_nil, ?_⟩ + rw [hL'sc] + intro i hi + cases hi + · rw [hL'q q e] + obtain ⟨⟨x, n, hx, hl⟩, hqR, hqnd, hqw⟩ := h.stackRel q hq + obtain ⟨y, k, hy, ey⟩ := hkq q hq e x n hx + refine ⟨⟨y, k, hy, ey.trans hl⟩, hqR, hqnd, fun i hi => ?_⟩ + have hm := List.getElem_mem hi + have hn : (Lst q)[i] ∉ Lst sc := fun h' => e (h.unique q hq sc hsc _ hm h') + rw [hmP _ (hqR _ hm), nx _ (hqR _ hm) hn] + exact hqw i hi + · exact h.unique q hq q' hq' d (hsub q hq d h1).2 (hsub q' hq' d h2).2 + · obtain ⟨x, hx⟩ := hRep d hd' + obtain ⟨o, ho, o1, o2⟩ := hre d hd' x hx + rw [ho] at hx' + cases hx' + have old := h.owner d hd' x hx p + by_cases c : hd ≠ 0#u64 ∧ specPtr d = specPtr hd + · have ed : d = hd := specPtr_inj c.2 + refine ⟨fun hp => (by rw [o1 c] at hp; cases hp), fun ⟨q, hq, _, hhd⟩ => ?_⟩ + exact absurd (List.mem_of_mem_head? hhd) (off d (inL d ⟨c.1, ed⟩) q hq) + · change o = some p ↔ _ + rw [o2 c, old] + constructor + · rintro ⟨q, hq, hqp, hhd⟩ + by_cases e : q = sc + · subst e + rw [hhd] at hh + have h0 : hd ≠ 0#u64 := fun z => by rw [z, optRefS_zero] at hh; cases hh + rw [optRefS_ne h0] at hh + exact absurd ⟨h0, Option.some.inj hh⟩ c + · exact ⟨q, hq, hqp, by rw [hL'q q e]; exact hhd⟩ + · rintro ⟨q, hq, hqp, hhd⟩ + by_cases e : q = sc + · subst e; rw [hL'sc] at hhd; cases hhd + · exact ⟨q, hq, hqp, by rw [hL'q q e] at hhd; exact hhd⟩ + · by_cases hm : d ∈ Lst sc + · have h0 : hd ≠ 0#u64 := fun z => by rw [empty z] at hm; cases hm + obtain ⟨rest, hL⟩ := head h0 + obtain ⟨j, hj, he⟩ := List.getElem_of_mem hm + subst he + obtain ⟨gp, gn⟩ := hLw j hj + have mR : ∀ i (hi : i < (Lst sc).length), (Lst sc)[i] ∈ R := fun i hi => hLR _ (List.getElem_mem hi) + have ne : ∀ i (hi : i < (Lst sc).length), 0 < i → (Lst sc)[i] ≠ hd := by + intro i hi hpos e + have e0 : (Lst sc)[0]'(by omega) = hd := by simp [hL] + have := (List.Nodup.getElem_inj_iff hLnd (hi := hi) (hj := by omega)).1 (e.trans e0.symm) + omega + have keepN : ∀ i (hi : i < (Lst sc).length), 0 < i → mem' (rNextA (Lst sc)[i]) = mem (rNextA (Lst sc)[i]) := + fun i hi hpos => hmN _ (mR i hi) fun c => ne i hi hpos c.2 + have nextW : mem' (rNextA (Lst sc)[j]) = (match j with | 0 => 0#u64 | i + 1 => (Lst sc).getD i 0#u64) := by + cases j with + | zero => + have e0 : (Lst sc)[0] = hd := by simp [hL] + rw [e0]; exact hmH h0 + | succ i => rw [keepN _ hj (by omega)]; exact gn + refine ⟨?_, fun hne => ?_, fun hne => ?_⟩ + · rw [nextW] + cases j with + | zero => exact headBitEvenA (r := 0#u64) (by simp) + | succ i => + show headBit ((Lst sc).getD i 0#u64) = false + rw [List.getD_eq_getElem _ _ (by omega)] + exact headBitEvenA (hEv _ (mR i (by omega))) + · cases j with + | zero => rw [nextW] at hne; exact absurd rfl hne + | succ i => + have hi : i < (Lst sc).length := by omega + have e1 : mem' (rNextA (Lst sc)[i + 1]) = (Lst sc)[i] := by + rw [nextW]; exact List.getD_eq_getElem _ _ hi + rw [e1] + refine ⟨mR i hi, off _ (List.getElem_mem hi), ?_⟩ + rw [hmP _ (mR i hi), (hLw i hi).1] + exact List.getD_eq_getElem _ _ hj + · have gp' : mem' (rPrevA (Lst sc)[j]) = (Lst sc).getD (j + 1) 0#u64 := (hmP _ (mR j hj)).trans gp + by_cases hj1 : j + 1 < (Lst sc).length + · have e1 : mem' (rPrevA (Lst sc)[j]) = (Lst sc)[j + 1] := by rw [gp']; exact List.getD_eq_getElem _ _ hj1 + rw [e1] + refine ⟨mR _ hj1, off _ (List.getElem_mem hj1), ?_⟩ + rw [keepN _ hj1 (by omega), (hLw (j + 1) hj1).2] + exact List.getD_eq_getElem _ _ hj + · rw [gp', List.getD_eq_default _ _ (by omega)] at hne + exact absurd rfl hne + · have old : ∀ q ∈ C, d ∉ Lst q := by + intro q hq h' + by_cases e : q = sc + · exact hm (e ▸ h') + · exact hdd q hq (by rw [hL'q q e]; exact h') + obtain ⟨hbit, hn, hp⟩ := h.detached d hd' old + have notIn : ∀ e, (∀ q ∈ C, e ∉ Lst q) → (∀ q ∈ C, e ∉ Function.update Lst sc [] q) ∧ e ∉ Lst sc := + fun e he => ⟨fun q hq h' => he q hq (hsub q hq e h').2, he sc hsc⟩ + rw [nx d hd' hm, hmP d hd'] + refine ⟨hbit, fun hne => ?_, fun hne => ?_⟩ + · obtain ⟨a, b, c⟩ := hn hne + exact ⟨a, (notIn _ b).1, (hmP _ a).trans c⟩ + · obtain ⟨a, b, c⟩ := hp hne + exact ⟨a, (notIn _ b).1, (nx _ a (notIn _ b).2).trans c⟩ + +/-- `NtfnObjRel` reads the queue, state and badge words, not `SC` or the bound TCB. -/ +theorem ntfnObjRel_frame4A {m m' : U64 → U64} {T : Set U64} {n : U64} {o : Fl4SpecA64.Ntfn} + (h : NtfnObjRel m T n o) (h1 : m' (epHeadA n) = m (epHeadA n)) (h2 : m' (epTSA n) = m (epTSA n)) + (h3 : m' (nBadgeA n) = m (nBadgeA n)) + (hy : ∀ y ∈ T, specPtr y ∈ o.ntfnQueue → Notif.MemberEq m m' y) : NtfnObjRel m' T n o := by + have eS : stOf m' n = stOf m n := by simp only [stOf, h2] + have eQ : qOf m' n = qOf m n := by simp only [qOf, h1, h2] + cases o with + | idleNtfn => + obtain ⟨a, b⟩ := h + exact ⟨eS ▸ a, eQ ▸ queueRel_congr_links b (by simp)⟩ + | activeNtfn v => + obtain ⟨a, b, c⟩ := h + exact ⟨eS ▸ a, eQ ▸ queueRel_congr_links b (by simp), h3 ▸ c⟩ + | waitingNtfn q => + obtain ⟨a, xs, hne, rfl, b, c⟩ := h + have hy' : ∀ y ∈ xs, Notif.MemberEq m m' y := fun y hyx => + hy y (b.mem y hyx) (by simp only [Fl4SpecA64.Ntfn.ntfnQueue]; exact List.mem_map_of_mem hyx) + refine ⟨eS ▸ a, xs, hne, rfl, eQ ▸ queueRel_congr_links b (fun y hyx => (hy' y hyx).2.2), fun y hyx => ?_⟩ + obtain ⟨e1, e2⟩ := c y hyx + exact ⟨(hy' y hyx).1 ▸ e1, (hy' y hyx).2.1 ▸ e2⟩ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- **`ScFinEndA` without the finalised SC's reply stack**: the end state of a chain of SC steps (`q` the unbound +notification, `h` the head reply detached, `yf` the yielder told the consumed time `d`), against the state before it. +`DelScRecA.scFin_recA` and the Thread finaliser's SC unbinding (`DelThrScA`) rebuild the record from it. -/ +structure ScEndGA (st : machine.Store S) [L : LawfulStore st] (Z : XSetsA) (N Rp : Set U64) (m : Fl4SpecA64.McState) + (s s' : S) (σ σ' : Fl4SpecA64.State Unit) (q h yf : U64) (d : Fl4SpecA64.Data) : Prop where + wr : WritesIn st s s' (ScFinWA st Z m σ s q h yf) + ntf : q ≠ 0#u64 → q ∈ N ∧ L.mem s' (nScSc q) = 0#u64 + rep : h ≠ 0#u64 → h ∈ Rp ∧ L.mem s' (rNextA h) = 0#u64 + yT : yf ≠ 0#u64 → yf ∈ Z.T ∧ L.mem s' (wa yf 256#u64) = 0#u64 ∧ + (L.mem s' (wa yf 264#u64)).bv = Fl4SpecA64.messageInfoToData ⟨Fl4SpecA64.natToLen 1, 0, 0, 0⟩ ∧ + (L.mem s' (wa yf 272#u64)).bv = d + tcb : ∀ r x, σ.kheap r = some (.tcb x) → ∃ y o, σ'.kheap r = some (.tcb y) ∧ + ((yf ≠ 0#u64 ∧ r = specPtr yf → y = { yieldDoneA x d with tcbSchedContext := o }) ∧ + (¬ (yf ≠ 0#u64 ∧ r = specPtr yf) → y = { x with tcbSchedContext := o })) + ep : ∀ r x, σ.kheap r = some (.endpoint x) → σ'.kheap r = some (.endpoint x) + nt : ∀ r x, σ.kheap r = some (.notification x) → ∃ o, σ'.kheap r = some (.notification { x with ntfnSc := o }) ∧ + (q ≠ 0#u64 ∧ r = specPtr q → o = none) ∧ (¬ (q ≠ 0#u64 ∧ r = specPtr q) → o = x.ntfnSc) + re : ∀ r x, σ.kheap r = some (.reply x) → ∃ o, σ'.kheap r = some (.reply { x with replySc := o }) ∧ + (h ≠ 0#u64 ∧ r = specPtr h → o = none) ∧ (¬ (h ≠ 0#u64 ∧ r = specPtr h) → o = x.replySc) + mcp : McpRelA Z.T m (L.mem s') σ' + yield : YieldRelA Z.T Z.C m (L.mem s') σ' + +theorem ScFinEndA.toG {Z : XSetsA} {N Rp : Set U64} {m : Fl4SpecA64.McState} {s s' : S} + {σ σ' : Fl4SpecA64.State Unit} {p q h yf : U64} {d : Fl4SpecA64.Data} + (E : ScFinEndA st Z N Rp m s s' σ σ' p q h yf d) : ScEndGA st Z N Rp m s s' σ σ' q h yf d := + ⟨E.wr, E.ntf, E.rep, E.yT, E.tcb, E.ep, E.nt, E.re, E.mcp, E.yield⟩ + +set_option maxHeartbeats 3200000 in +/-- **The record rebuilt at the end of a chain of SC steps** (`ScEndGA`): the caller supplies P1's `BlkOK`, the +scheduler relation, the loop invariant, no pending switch and the spec frame at the end state, the reply-stack relation +with a sub-stack, an SC object `P` at the start (the anchor of `BlkMonoA`), and `hQ7` (J7 does not read the ghost queues; `rfl` for +the concrete slots). -/ +theorem JStage3TA.rec_scEndA {J7 : JSlotA S} [J7PpA st J7] [J7PpKA st J7] + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + {s' : S} {σ' : Fl4SpecA64.State Unit} {Q' : U64 → U64 → List U64} {R' : U64 → List U64} {Ac' : Set U64} + {q h yf : U64} {d : Fl4SpecA64.Data} + (bk : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac' s') + (H5 : SchedSRA st { Z with Q := Q', R := R' } m κ s' σ') (inv : LoopInvA σ' m n) (nsw : NoSwA σ') + (F : SchedFrameA σ σ') (Qk : QKeepA σ σ') (END : ScEndGA st Z N Rp m s s' σ σ' q h yf d) + {stack' : U64 → List U64} (hsub : ∀ c, ∀ r ∈ stack' c, r ∈ stack c) + (reply : ReplyStackRelA (L.mem s') Rp Z.C σ' (scNameA m) stack') + {P : Fl4SpecA64.ObjRef} (hpk : ∃ xh kh, σ.kheap P = some (.schedContext xh kh)) + (hQ7 : ∀ Q Rl, J7 { Z with Q := Q, R := Rl } = J7 Z) : + JStage3TA st AR { Z with Q := Q', R := R' } H OS U Ac' Pp Tb Lv ia ie N E Rp Bf stack' κ n (J5A st) J7 s' a σ' m ∧ + WritesIn st s s' (SchedStepNWA (st := st) N E Rp Z m σ s) ∧ OSKeep st OS s s' ∧ + (∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → + (K.val ≠ 256 ∧ K.val ≠ 264 ∧ K.val ≠ 272 ∧ K.val ≠ 3464 ∧ K.val ≠ 3472 ∧ K.val ≠ 3480 ∧ K.val ≠ 3488 ∧ + K.val ≠ 3592) → L.mem s' (wa u K) = L.mem s (wa u K)) := by + have hP := J.iplaced + have tH : ∀ t ∈ Z.T, TcbAddr t := fun t ht => J.sr.core.tcbs t ht + have hRp : h ≠ 0#u64 → h ∈ Rp := fun c => (END.rep c).1 + have vnext : ∀ r ∈ Rp, (rNextA r).val = r.val + 16 := fun r hr => (replyNextPlaceA (Z := Z) hP.e hr).2 + have vq : q ≠ 0#u64 → (nScSc q).val = q.val + 32 := fun c => by + have e : nScSc q = Notif.nScA q := rfl + rw [e, Notif.nScA_eq, Notif.nfield_val (hP.n.ntfn q (END.ntf c).1) (by simp)]; simp + have w : WritesIn st s s' (SchedStepNWA (st := st) N E Rp Z m σ s) := by + refine END.wr.mono fun c hc => ?_ + rcases hc with hc | ⟨c0, rfl⟩ | ⟨c0, rfl⟩ | ⟨c0, hc⟩ + · exact Or.inl (Or.inl hc) + · exact Or.inr (Or.inl ⟨q, (END.ntf c0).1, by rw [vq c0]; omega, by rw [vq c0]; omega⟩) + · exact Or.inr (Or.inr (Or.inr ⟨h, hRp c0, by rw [vnext _ (hRp c0)]; omega, by rw [vnext _ (hRp c0)]; omega⟩)) + · have hy := (END.yT c0).1 + have hfit := (tH yf hy).fits + have hv : ∀ K : U64, K.val < 4096 → (wa yf K).val = yf.val + K.val := fun K hK => by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have key : ∀ K : U64, 224 ≤ K.val → K.val < 4096 → K.val ≠ 3664 → K.val ≠ 3672 → c = wa yf K → + TcbStepWA Z.T c := by + intro K k1 k2 k3 k4 e + subst e + refine ⟨yf, hy, by rw [hv K k2]; omega, by rw [hv K k2]; omega, fun x => ?_, fun x => ?_⟩ + · have := congrArg UScalar.val x + rw [hv K k2, hv _ (by simp)] at this + simp at this; omega + · unfold runVcpuA at x + have := congrArg UScalar.val x + rw [hv K k2, hv _ (by simp)] at this + simp at this; omega + refine Or.inl (Or.inr ?_) + rcases hc with (e | e | e) | e + · exact key 256#u64 (by simp) (by simp) (by simp) (by simp) e + · exact key 264#u64 (by simp) (by simp) (by simp) (by simp) e + · exact key 272#u64 (by simp) (by simp) (by simp) (by simp) e + · exact key 3472#u64 (by simp) (by simp) (by simp) (by simp) e + have kb : L.ksb s' = L.ksb s := w.1 + -- the TCB words + have tw : ∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → + (K.val ≠ 3464 ∧ K.val ≠ 3480 ∧ K.val ≠ 3488 ∧ K.val ≠ 3592) → + (yf ≠ 0#u64 → u = yf → K.val ≠ 256 ∧ K.val ≠ 264 ∧ K.val ≠ 272 ∧ K.val ≠ 3472) → + L.mem s' (wa u K) = L.mem s (wa u K) := by + intro u hu K k1 k2 k3 k4 + refine END.wr.mem_eq (field_aligned (tH u hu) k1 k2) ?_ + rintro (c | ⟨c0, c⟩ | ⟨c0, c⟩ | ⟨c0, c⟩) + · exact J.tcbWord_offMA hu k1 k3 c + · exact J.ipc_ne_tcbA hu k1 (Or.inl ⟨q, (END.ntf c0).1, by rw [vq c0]; omega, by rw [vq c0]; omega⟩) c + · exact J.ipc_ne_tcbA hu k1 (Or.inr (Or.inr ⟨h, hRp c0, by rw [vnext _ (hRp c0)]; omega, + by rw [vnext _ (hRp c0)]; omega⟩)) c + · have hy := (END.yT c0).1 + have ne : ∀ K' : U64, K'.val < 4096 → (K'.val = 256 ∨ K'.val = 264 ∨ K'.val = 272 ∨ K'.val = 3472) → + wa u K ≠ wa yf K' := by + intro K' h4 h5 + refine field_ne (tH u hu) (tH yf hy) k1 h4 (fun e => ?_) + have := k4 c0 e + omega + rcases c with (c | c | c) | c + · exact ne 256#u64 (by simp) (by simp) c + · exact ne 264#u64 (by simp) (by simp) c + · exact ne 272#u64 (by simp) (by simp) c + · exact ne 3472#u64 (by simp) (by simp) c + have twS : ∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → + (K.val ≠ 256 ∧ K.val ≠ 264 ∧ K.val ≠ 272 ∧ K.val ≠ 3464 ∧ K.val ≠ 3472 ∧ K.val ≠ 3480 ∧ K.val ≠ 3488 ∧ + K.val ≠ 3592) → L.mem s' (wa u K) = L.mem s (wa u K) := + fun u hu K k1 k2 k3 => tw u hu K k1 k2 ⟨by omega, by omega, by omega, by omega⟩ + (fun _ _ => ⟨by omega, by omega, by omega, by omega⟩) + -- notification words but the unbound `SC` word + have nw : ∀ r ∈ N, ∀ K : U64, K.val < 40 → K.val % 8 = 0 → ¬ (q ≠ 0#u64 ∧ r = q ∧ K.val = 32) → + L.mem s' (wa r K) = L.mem s (wa r K) := by + intro r hr K k1 k2 k3 + have hna := hP.n.ntfn r hr + have hv : (wa r K).val = r.val + K.val := Notif.nfield_val hna (by omega) + have hw : IpcObjWA N E Rp (wa r K) := Or.inl ⟨r, hr, by omega, by omega⟩ + refine END.wr.mem_eq (Notif.nfield_aligned hna (by omega) k2) ?_ + rintro (c | ⟨c0, c⟩ | ⟨c0, c⟩ | ⟨c0, c⟩) + · exact J.ipc_offMA hw c + · have e := congrArg UScalar.val c + rw [hv, vq c0] at e + have a1 := hna.align + have a2 := (hP.n.ntfn q (END.ntf c0).1).align + exact k3 ⟨c0, UScalar.eq_of_val_eq (by omega), by omega⟩ + · have e := congrArg UScalar.val c + rw [hv, vnext _ (hRp c0)] at e + have := hP.nr r hr h (hRp c0) + omega + · have hy := (END.yT c0).1 + rcases c with (c | c | c) | c + · exact J.ipc_ne_tcbA hy (K := 256#u64) (by simp) hw c.symm + · exact J.ipc_ne_tcbA hy (K := 264#u64) (by simp) hw c.symm + · exact J.ipc_ne_tcbA hy (K := 272#u64) (by simp) hw c.symm + · exact J.ipc_ne_tcbA hy (K := 3472#u64) (by simp) hw c.symm + -- the endpoint footprint but the yielder's three registers + let Rg : U64 → Prop := fun c => yf ≠ 0#u64 ∧ ScConsumedRegsA yf c + have ef : ∀ c, Aligned c → EFoot Z.T E Rp Bf c → ¬ Rg c → L.mem s' c = L.mem s c := by + intro c al hf hr + have offN : ∀ r ∈ N, ¬ (r.val ≤ c.val ∧ c.val < r.val + 64) := by + intro r hr' hc + rcases hf with ⟨e, he, x⟩ | ⟨rr, hrr, x⟩ | ⟨t, ht, x⟩ | x + · have := hP.ne r hr' e he; omega + · have := hP.nr r hr' rr hrr; omega + · have := hP.n.disj r hr' t ht + have : t.val ≤ c.val ∧ c.val < t.val + 4096 := by + rcases x with x | x + · exact ⟨x.1, by have := TF.range x; omega⟩ + · unfold TL at x; omega + omega + · have := hP.nb r hr' c x; omega + refine END.wr.mem_eq al ?_ + rintro (x | ⟨c0, x⟩ | ⟨c0, x⟩ | ⟨c0, x⟩) + · exact endpFootOffWrite J.sr.toSchedSRN J.ipcPlace hf x + · exact offN q (END.ntf c0).1 (by rw [x, vq c0]; omega) + · have v : c.val = h.val + 16 := by rw [x, vnext _ (hRp c0)] + obtain ⟨h32, -, -⟩ := hP.e.rep h (hRp c0) + rcases hf with ⟨e, he, y⟩ | ⟨rr, hrr, y⟩ | ⟨t, ht, y⟩ | y + · have := hP.e.er e he h (hRp c0); omega + · have := (hP.e.rep rr hrr).1; omega + · have := hP.e.rt h (hRp c0) t ht + have : t.val ≤ c.val ∧ c.val < t.val + 4096 := by + rcases y with y | y + · exact ⟨y.1, by have := TF.range y; omega⟩ + · unfold TL at y; omega + omega + · have := hP.e.br c y h (hRp c0); omega + · rcases x with x | x + · exact hr ⟨c0, x⟩ + · have hy := (END.yT c0).1 + have v : c.val = yf.val + 3472 := by + have hfit := (tH yf hy).fits + rw [x] + show (wa yf 3472#u64).val = _ + rw [wa_valm]; simp; omega + rcases hf with ⟨e, he, y⟩ | ⟨rr, hrr, y⟩ | ⟨t, ht, y⟩ | y + · have := hP.e.et e he yf hy; omega + · have := hP.e.rt rr hrr yf hy; omega + · have r : t.val ≤ c.val ∧ c.val < t.val + 4096 := by + rcases y with y | y + · exact ⟨y.1, by have := TF.range y; omega⟩ + · unfold TL at y; omega + by_cases e : t = yf + · subst e + rcases y with y | y + · have := y.2 + rw [v, Nat.add_sub_cancel_left] at this + unfold TOffN at this + omega + · unfold TL at y; omega + · have := Endp.tcb_disj hP.e ht hy (Ne.symm e); omega + · have := hP.e.bt c y yf hy; omega + -- spec TCB keeps + have TK : ∀ r x, σ.kheap r = some (.tcb x) → ∃ y, σ'.kheap r = some (.tcb y) ∧ y.tcbState = x.tcbState ∧ + y.tcbIpcframe = x.tcbIpcframe ∧ y.tcbIpcBuffer = x.tcbIpcBuffer ∧ y.tcbFault = x.tcbFault ∧ + y.tcbPriority = x.tcbPriority ∧ y.tcbBoundNotification = x.tcbBoundNotification := by + intro r x hx' + obtain ⟨y, o, hy, c1, c2⟩ := END.tcb r x hx' + refine ⟨y, hy, ?_⟩ + by_cases c : yf ≠ 0#u64 ∧ r = specPtr yf + · rw [c1 c]; exact ⟨rfl, rfl, rfl, rfl, rfl, rfl⟩ + · rw [c2 c]; exact ⟨rfl, rfl, rfl, rfl, rfl, rfl⟩ + have E' : EpStateRel Z.T E Rp Bf (L.mem s') σ' := by + have rk : ∀ r x, σ.kheap r = some (.reply x) → ∃ y, σ'.kheap r = some (.reply y) ∧ y.replyTcb = x.replyTcb := by + intro r x hx' + obtain ⟨o, ho, -⟩ := END.re r x hx' + exact ⟨_, ho, rfl⟩ + by_cases c0 : yf = 0#u64 + · refine J.ep.frameX hP.e (fun c al hc => ef c al hc.1 fun x => x.1 c0) + ⟨END.ep, rk, fun r x hx' => ?_, F.2.2⟩ + obtain ⟨y, o, hy, -, c2⟩ := END.tcb r x hx' + refine ⟨y, hy, ?_⟩ + rw [c2 fun c => c.1 c0] + exact ⟨rfl, rfl, rfl, rfl, rfl⟩ + have hy := (END.yT c0).1 + have hta := tH yf hy + obtain ⟨x0, hx0, rel0⟩ := J.ep.tcb yf hy + obtain ⟨y, o, hyk, c1, -⟩ := END.tcb _ _ hx0 + have ey := c1 ⟨c0, rfl⟩ + haveI : ∀ c, Decidable (ScConsumedRegsA yf c) := fun c => by unfold ScConsumedRegsA; infer_instance + let y0 : Tcb := { y with tcbArch := x0.tcbArch } + let σA : Fl4SpecA64.State Unit := updK σ' (specPtr yf) (.tcb y0) + let mA : U64 → U64 := fun c => if ScConsumedRegsA yf c then L.mem s c else L.mem s' c + have mAp : ∀ c, ScConsumedRegsA yf c → mA c = L.mem s c := fun c h => by simp [mA, h] + have mAn : ∀ c, ¬ ScConsumedRegsA yf c → mA c = L.mem s' c := fun c h => by simp [mA, h] + have far : ∀ r, r ≠ specPtr yf → σA.kheap r = σ'.kheap r := fun r hr => by + change Function.update σ'.kheap (specPtr yf) _ r = _ + rw [Function.update_of_ne hr] + have ne : ∀ r o', σ.kheap r = some o' → (∀ z, o' ≠ .tcb z) → r ≠ specPtr yf := fun r o' h' hn e => by + rw [e, hx0] at h'; cases h'; exact hn _ rfl + have R1 : EpStateRel Z.T E Rp Bf mA σA := by + refine J.ep.frameX hP.e (fun c al hc => ?_) ⟨fun r x hx' => ?_, fun r x hx' => ?_, fun r x hx' => ?_, F.2.2⟩ + · by_cases cr : ScConsumedRegsA yf c + · exact mAp c cr + · exact (mAn c cr).trans (ef c al hc.1 fun x => cr x.2) + · rw [far r (ne r _ hx' (fun _ e => by cases e))]; exact END.ep r x hx' + · rw [far r (ne r _ hx' (fun _ e => by cases e))]; exact rk r x hx' + · by_cases er : r = specPtr yf + · subst er + rw [hx0] at hx' + cases hx' + refine ⟨y0, Function.update_self _ _ _, ?_⟩ + simp only [y0, ey] + refine ⟨?_, ?_, ?_, ?_, ?_⟩ <;> first | rfl | trivial + · obtain ⟨z, o', hz, -, c2⟩ := END.tcb r x hx' + refine ⟨z, (far r er).trans hz, ?_⟩ + rw [c2 fun c => er c.2] + exact ⟨rfl, rfl, rfl, rfl, rfl⟩ + have rv : ∀ i : Fl4SpecA64.Register, (Endp.regA yf (regIdxA i)).val = yf.val + 256 + 8 * (regIdxA i).val := + fun i => regA_val hta (regIdxA_lt i) + have kv : ∀ K : U64, K.val < 4096 → (wa yf K).val = yf.val + K.val := fun K hK => by + have := hta.fits + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have inT : ∀ c, ScConsumedRegsA yf c → TF yf c := by + intro c hc + have key : ∀ K : U64, K.val = 256 ∨ K.val = 264 ∨ K.val = 272 → c = wa yf K → TF yf c := by + intro K hK e + have v := kv K (by omega) + rw [e] + refine ⟨by omega, ?_⟩ + rw [v, Nat.add_sub_cancel_left] + unfold TOffN + omega + rcases hc with e | e | e + · exact key _ (by simp) e + · exact key _ (by simp) e + · exact key _ (by simp) e + have AG : Agree mA (L.mem s') (fun c => EFoot Z.T E Rp Bf c ∧ ¬ TF yf c) := fun c _ hc => + (mAn c fun cr => hc.2 (inT c cr)).symm + have offR : ∀ K : U64, K.val < 4096 → 552 ≤ K.val → ¬ ScConsumedRegsA yf (wa yf K) := by + intro K k1 k2 cr + have no : ∀ K' : U64, K'.val < 552 → wa yf K ≠ wa yf K' := fun K' h' e => by + have := congrArg UScalar.val e + rw [kv K k1, kv K' (by omega)] at this + omega + rcases cr with e | e | e + · exact no _ (by simp) e + · exact no _ (by simp) e + · exact no _ (by simp) e + have same : ∀ c : U64, ∀ K : U64, K.val < 4096 → 552 ≤ K.val → c.val = yf.val + K.val → L.mem s' c = mA c := by + intro c K k1 k2 e + have : c = wa yf K := UScalar.eq_of_val_eq (e.trans (kv K k1).symm) + rw [this] + exact (mAn _ (offR K k1 k2)).symm + obtain ⟨z, hz, rel1⟩ := R1.tcb yf hy + have ez : z = y0 := by + rw [show σA.kheap (specPtr yf) = some (.tcb y0) from Function.update_self _ _ _] at hz + cases hz; rfl + subst ez + have X : TcbRelE Rp (L.mem s') yf y := by + refine rel1.with_regsA hta (fun c al hc => ?_) rfl (fun i => ?_) + · refine (mAn c fun cr => ?_).symm + have key : ∀ K : U64, K.val = 256 ∨ K.val = 264 ∨ K.val = 272 → c ≠ wa yf K := by + intro K hK e + have v := kv K (by omega) + rw [← e] at v + have := hc.2 + omega + rcases cr with e | e | e + · exact key _ (by simp) e + · exact key _ (by simp) e + · exact key _ (by simp) e + · have at_ : ∀ K : U64, K.val < 4096 → (regIdxA i).val * 8 + 256 = K.val → Endp.regA yf (regIdxA i) = wa yf K := + fun K k1 k2 => UScalar.eq_of_val_eq (by rw [rv, kv K k1]; omega) + obtain ⟨-, v0, v1, v2⟩ := END.yT c0 + rw [ey] + simp only [yieldDoneA, yieldRegA, archTcbSetRegisters, archTcbGetRegisters, archTcbContextGet, + archTcbContextSet, badge_register, msg_info_register, badgeRegister, msgInfoRegister] + by_cases i0 : i = .x0 + · subst i0 + rw [at_ 256#u64 (by simp) (by simp [regIdxA]), + Function.update_of_ne (by decide), Function.update_of_ne (by decide), Function.update_self, v0] + rfl + by_cases ix1 : i = .x1 + · subst ix1 + rw [at_ 264#u64 (by simp) (by simp [regIdxA]), Function.update_self] + exact v1 + by_cases ix2 : i = .x2 + · subst ix2 + rw [at_ 272#u64 (by simp) (by simp [regIdxA]), Function.update_of_ne (by decide), Function.update_self] + exact v2 + rw [Function.update_of_ne ix1, Function.update_of_ne ix2, Function.update_of_ne i0] + have n0 : (regIdxA i).val ≠ 0 ∧ (regIdxA i).val ≠ 1 ∧ (regIdxA i).val ≠ 2 := by + revert i0 ix1 ix2 + cases i <;> simp [regIdxA] + have lt := regIdxA_lt i + have off : ¬ ScConsumedRegsA yf (Endp.regA yf (regIdxA i)) := by + intro cr + have no : ∀ K : U64, K.val = 256 ∨ K.val = 264 ∨ K.val = 272 → Endp.regA yf (regIdxA i) ≠ wa yf K := by + intro K hK e + have := congrArg UScalar.val e + rw [rv, kv K (by omega)] at this + omega + rcases cr with e | e | e + · exact no _ (by simp) e + · exact no _ (by simp) e + · exact no _ (by simp) e + have keep : L.mem s' (Endp.regA yf (regIdxA i)) = L.mem s (Endp.regA yf (regIdxA i)) := + ef _ (a_reg hta i).1 (Or.inr (Or.inr (Or.inl ⟨yf, hy, Or.inl (a_reg hta i).2⟩))) fun x => off x.2 + rw [keep] + exact rel0.regs i + have ST : L.mem s' (stA yf) = mA (stA yf) := same _ 3328#u64 (by simp) (by simp) (by rw [val_st hta]; simp) + have BL : L.mem s' (tBlockA yf) = mA (tBlockA yf) := same _ 3336#u64 (by simp) (by simp) + (by rw [val_blk hta]; simp) + have U := R1.upd_tcb hP.e hy y AG (Or.inr ⟨ST, BL⟩) X + have eσ : updK σA (specPtr yf) (.tcb y) = σ' := by + have : Function.update (Function.update σ'.kheap (specPtr yf) (some (.tcb y0))) (specPtr yf) + (some (.tcb y)) = σ'.kheap := by + rw [Function.update_idem, ← hyk, Function.update_eq_self] + show ({ σ' with kheap := _ } : Fl4SpecA64.State Unit) = σ' + rw [show Function.update σA.kheap (specPtr yf) (some (.tcb y)) = σ'.kheap from this] + rwa [eσ] at U + have ntfn : NtfnStateRel N Z.T (L.mem s') σ' := by + refine ⟨fun r hr => ?_, fun u hu => ?_⟩ + · obtain ⟨x, hx', rel⟩ := J.ntfn.ntfn r hr + obtain ⟨o, ho, o1, o2⟩ := END.nt _ _ hx' + have hna := hP.n.ntfn r hr + have g : ∀ K : U64, K.val < 32 → K.val % 8 = 0 → L.mem s' (wa r K) = L.mem s (wa r K) := + fun K k1 k2 => nw r hr K (by omega) k2 (fun c => by omega) + have bd : L.mem s' (nBoundA r) = L.mem s (nBoundA r) := by + rw [Notif.nBoundA_eq]; exact g _ (by simp) (by simp) + refine ⟨_, ho, ?_, ?_, ?_, ?_⟩ + · refine ntfnObjRel_frame4A rel.obj (by rw [Notif.epHeadA_eq]; exact g _ (by simp) (by simp)) + (by rw [Notif.epTSA_eq]; exact g _ (by simp) (by simp)) + (by rw [Notif.nBadgeA_eq]; exact g _ (by simp) (by simp)) fun y hy hm => ?_ + obtain ⟨hyT, -, -⟩ := rel.obj.blocked hm + have hya := hP.e.tcb y hyT + refine ⟨?_, ?_, ?_, ?_⟩ + · have e : stA y = wa y 3328#u64 := by rw [stA_eq] + exact e ▸ twS y hyT 3328#u64 (by simp) (by simp) (by simp) + · rw [Notif.tBlockA_eq]; exact twS y hyT _ (by simp) (by simp) (by simp) + · rw [Notif.prevEp_eq]; exact twS y hyT _ (by simp) (by simp) (by simp) + · rw [Notif.nextEp_eq]; exact twS y hyT _ (by simp) (by simp) (by simp) + · change x.ntfnBoundTcb = _ + rw [bd]; exact rel.bound + · rw [bd]; exact rel.bound_mem + · change o = optRef (L.mem s' (nScA r)) + by_cases c : q ≠ 0#u64 ∧ specPtr r = specPtr q + · have e : r = q := specPtr_inj c.2 + rw [o1 c, e, show nScA q = nScSc q from rfl, (END.ntf c.1).2] + rfl + · have keep : L.mem s' (nScA r) = L.mem s (nScA r) := by + rw [Notif.nScA_eq] + exact nw r hr _ (by simp) (by simp) (fun x => c ⟨x.1, by rw [x.2.1]⟩) + rw [o2 c, keep]; exact rel.sc + · obtain ⟨x, hx', rel⟩ := J.ntfn.tcb u hu + obtain ⟨y, o, hy, c1, c2⟩ := END.tcb _ _ hx' + refine ⟨y, hy, ?_⟩ + have hst : L.mem s' (stA u) = L.mem s (stA u) := by + rw [show stA u = wa u 3328#u64 from by rw [stA_eq]] + exact twS u hu _ (by simp) (by simp) (by simp) + have hbl : L.mem s' (tBlockA u) = L.mem s (tBlockA u) := by + rw [Notif.tBlockA_eq]; exact twS u hu _ (by simp) (by simp) (by simp) + have hpr : L.mem s' (prioA u) = L.mem s (prioA u) := by + rw [Notif.prioA_eq']; exact twS u hu _ (by simp) (by simp) (by simp) + obtain ⟨y', hy', es, -, -, -, ep, -⟩ := TK _ _ hx' + rw [hy] at hy' + cases hy' + refine ⟨by rw [hst, es]; exact rel.st, fun k hk' => by rw [hbl]; exact rel.blk k (es ▸ hk'), ?_, + by rw [hpr, ep]; exact rel.prio⟩ + by_cases c : yf ≠ 0#u64 ∧ specPtr u = specPtr yf + · have e : u = yf := specPtr_inj c.2 + rw [c1 c, e, badgeRegA_eq, (END.yT c.1).2.1] + simp only [badgeOf, yieldDoneA, yieldRegA, archTcbSetRegisters, archTcbGetRegisters, archTcbContextGet, + archTcbContextSet, badge_register, msg_info_register, badgeRegister, msgInfoRegister] + rw [Function.update_of_ne (by decide), Function.update_of_ne (by decide), Function.update_self] + rfl + · have keep : L.mem s' (badgeRegA u) = L.mem s (badgeRegA u) := by + rw [badgeRegA_eq] + exact tw u hu 256#u64 (by simp) (by simp) (by simp) (fun h0 e => absurd ⟨h0, by rw [e]⟩ c) + rw [c2 c, keep]; exact rel.badge + have sq : SchedQueuesInv st H.tcbs Z.Cs H.omega H s' Q' R' := by + obtain ⟨Q0, R0, h0'⟩ := bk.1 + exact SchedQueuesInv.reQA h0' (by rw [J.tcbs]; exact H5.core) + have poolOff : PoolOff st H Z.Cs OS s' := fun i => by + have h0' := J.poolOff i + rw [vspace.asid_table.eq] at h0' ⊢ + have v := AtOff.v i + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.ent.ksw + have ev : (wa (L.ksb s) (AtOff i)).val = (L.ksb s).val + (AtOff i).val := by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have e0 : L.mem s' (wa (L.ksb s') (AtOff i)) = L.mem s (wa (L.ksb s) (AtOff i)) := by + rw [kb] + refine w.mem_eq ?_ (J.asid_offStepNA (by omega) (by omega)) + have h32 := J.j.1.ksbAl + show (wa (L.ksb s) (AtOff i)).val % 8 = 0 + omega + rw [e0] + refine (WP.spec_ok _).2 fun hne asid => ?_ + have x := (WP.spec_ok _).1 h0' hne asid + exact ⟨x.al, x.om, x.sg, x.os, RegionOffNodes.ksb kb x.ks⟩ + have buf : BufCover st Z.T Bf s' σ' := + J.bufCover_wA END.wr (fun u hu K hK x => by + have k4 : K.val < 4096 := by omega + rcases x with x | ⟨c0, x⟩ | ⟨c0, x⟩ | ⟨c0, x⟩ + · exact J.tcbWord_offMA hu k4 (by omega) x + · exact J.ipc_ne_tcbA hu k4 (Or.inl ⟨q, (END.ntf c0).1, by rw [vq c0]; omega, by rw [vq c0]; omega⟩) x + · exact J.ipc_ne_tcbA hu k4 (Or.inr (Or.inr ⟨h, hRp c0, by rw [vnext _ (hRp c0)]; omega, + by rw [vnext _ (hRp c0)]; omega⟩)) x + · have hy := (END.yT c0).1 + have no : ∀ K' : U64, K'.val < 4096 → 224 ≤ K'.val → K'.val ≠ 3512 → wa u K ≠ wa yf K' := + fun K' h4 h5 h6 => field_ne (tH u hu) (tH yf hy) k4 h4 (fun _ => by omega) + rcases x with (x | x | x) | x + · exact no _ (by simp) (by simp) (by simp) x + · exact no _ (by simp) (by simp) (by simp) x + · exact no _ (by simp) (by simp) (by simp) x + · exact no _ (by simp) (by simp) (by simp) x) + (fun r y h' => by + obtain ⟨y2, hy, -, f, g, -⟩ := TK r y h' + exact ⟨y2, hy, f, g⟩) + have bufAl : UtBufAlA Z.T σ' := fun u hu y hy => by + obtain ⟨x0, hx0, -⟩ := J.ntfn.tcb u hu + obtain ⟨y', hy', -, -, e, -⟩ := TK _ _ hx0 + rw [hy] at hy'; cases hy' + rw [e]; exact J.bufAl u hu x0 hx0 + have idle : IdleNoFaultA m σ' := fun c hc' => by + obtain ⟨x0, hx0, hf⟩ := J.idleNoFault c hc' + obtain ⟨y, hy, -, -, -, g, -⟩ := TK _ _ hx0 + exact ⟨y, hy, g.trans hf⟩ + have scCov : ScCovA Z.C m σ' := fun r y k h' => by + obtain ⟨y', hy⟩ := F.2.1 r y k h' + exact J.scCov r y' k hy + have rv : RunVcpuInv st Z.T s' := J.runVcpu.rvSt fun y hy => + ⟨by rw [show stA y = wa y 3328#u64 from by rw [stA_eq]] + exact twS y hy _ (by simp) (by simp) (by simp), + twS y hy 3672#u64 (by simp) (by simp) (by simp)⟩ + have hvr : ∀ sc ∈ Z.C, ∀ r ∈ stack' sc, RBlk st s' r := by + refine J.vr_specA bk hsub (fun sc _ r _ c _ _ x o hx' _ => ?_) E' (fun c hc => ?_) + · obtain ⟨y, hy, es, -⟩ := TK _ _ hx' + exact ⟨y, hy, es⟩ + · rw [show tReplyA c = wa c 3360#u64 from rfl] + exact twS c hc _ (by simp) (by simp) (by simp) + have j := J.j57_stepNWA w σ' + have bq : BlkQueuedA Z.T E N (L.mem s') σ' := by + obtain ⟨xh, kh, hxh⟩ := hpk + have M : BlkMonoA P σ σ' := by + refine ⟨fun u x hx => ?_, fun r y _ hy => END.ep r y hy, fun r y _ hy => ?_⟩ + · obtain ⟨y, hy, es, -⟩ := TK u x hx + exact ⟨y, hy, Or.inl es⟩ + · obtain ⟨o, ho, -⟩ := END.nt r y hy + exact ⟨_, ho, rfl⟩ + refine J.blkQ.finA M (fun u hu => ?_) (fun u hu => ?_) (fun y hy => ?_) (fun y q' hn => ?_) + · obtain ⟨x0, hx0, -⟩ := J.ntfn.tcb u hu + exact ⟨x0, hx0⟩ + · rw [Notif.tBlockA_eq]; exact twS u hu _ (by simp) (by simp) (by simp) + · rw [hxh] at hy; cases hy + · rw [hxh] at hn; cases hn + have J' := JStage3TA.of_schedWrSN (blkQ := bq) J (stack' := stack') hsub (Q := Q') (Rl := R') + (curOf := Z.curOf) (curScOf := Z.curScOf) (s' := s') (a' := a) (σ' := σ') w F.1 F.2.1 Qk F.2.2 J.j.1.ar + (fun _ _ => rfl) bufAl (J.ent_stepAcA w bk) sq poolOff H5 scCov E' ntfn reply buf idle END.mcp END.yield + inv.sinv inv.sidle rv hvr nsw inv.sact j.1 (by rw [hQ7]; exact j.2) + exact ⟨J', w, J.sigW_osKeepA w, twS⟩ + +set_option maxHeartbeats 3200000 in +/-- **The record across the final SC finaliser** (module docstring). -/ +theorem JStage3TA.scFin_recA {ipa : U64} + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7LA st (J7R2A st ar ipa)) s a σ m) + {p b : U64} (hp : p ∈ Z.C) (hni : specPtr p ≠ m.idleScOf m.curCore) + (hA : P0.ArchTotal ar) (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (h1 : fl4A64Platform.numNodes = 1) (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) : + objects.finalise_cap_object st ar s a (.SchedContext p b) true ⦃ r => + r.1 = (.Null, .Null) ∧ r.2.2 = a ∧ ∃ σ1 σ2 σ3 σ4 σ' Q R Ac' stack', + ((), σ1) ∈ (schedContextUnbindAllTcbs (Z := Unit) (specPtr p) σ).1 ∧ + ((), σ2) ∈ (schedContextUnbindNtfn (Z := Unit) (specPtr p) σ1).1 ∧ + ((), σ3) ∈ (schedContextUnbindReply (Z := Unit) (specPtr p) σ2).1 ∧ + ((), σ4) ∈ (schedContextUnbindYieldFrom (Z := Unit) (specPtr p) σ3).1 ∧ + ((), σ') ∈ (schedContextSetInactive (Z := Unit) (specPtr p) σ4).1 ∧ + JStage3TA st AR { Z with Q := Q, R := R } H OS U Ac' Pp Tb Lv ia ie N E Rp Bf stack' κ n (J5A st) + (J7LA st (J7R2A st ar ipa)) r.2.1 a σ' m ∧ + OSKeep st OS s r.2.1 ∧ L.ksb r.2.1 = L.ksb s ∧ L.cpu r.2.1 = L.cpu s ∧ SchedFrameA σ σ' ∧ + (∀ t ∈ Z.T, L.mem r.2.1 (wa t 3664#u64) = L.mem s (wa t 3664#u64)) ∧ + Std6KeepA st Z H OS Bf s σ r.2.1 σ' ⦄ := by + have hpS : p ∈ H.scs := by rw [J.scs]; exact hp + have hp0 := J.scNZ p hp + have hcc := J.curCore_lt hCs + have hnm : scNameA m p = specPtr p := if_neg hni + have hni' : scNameA m p ≠ idleScPtr := by rw [hnm]; exact J.sr.sch.scOff p hp hni + obtain ⟨xs, ns, hk, hx⟩ := J.sr.scr.sc p hp + have hna : L.mem s (scTcbA p) ≠ 0#u64 → L.actionW s ≠ L.mem s (scTcbA p) := by + intro h0 e + have ht : L.mem s (scTcbA p) ∈ Z.T := by rw [← J.tcbs]; exact J.blkA.2.2.1.2.2.yt.1 p hpS h0 + refine J.noSwitch (specPtr (L.mem s (scTcbA p))) ?_ + rw [J.sr.sch.act, e] + unfold actOfA + rw [if_neg h0, if_neg] + intro c + have := (J.sr.core.tcbs _ ht).align + rw [c] at this + simp at this + have hidle : L.mem s (scTcbA p) ≠ 0#u64 → L.idleW s ≠ L.mem s (scTcbA p) := by + intro h0 e + have hxT : xs.scTcb = some σ.idleThread := by + rw [J.sr.sch.idle, e, ← optRefS_ne h0, scTcbA_eqSc] + exact hx.tcb + exact hni' (J.sidle.f5 _ xs ns hk hxT) + refine WP.spec_mono (WP.spec_and + (Fl4ProofsA64.P1.K7.fin_sc.blk (ar := ar) hA s a p b true J.blkA hpS hp0 hna hidle) + (J.scFin_schedA (b := b) hp hni hA hCs h1 hmax htu)) ?_ + rintro ⟨caps, s', a'⟩ ⟨⟨Ac', -, bk, -⟩, e0, ha, σ1, σ2, σ3, σ4, σ', Q', R', m1, m2, m3, m4, m5, H5, inv, nsw, F, + q, h, yf, d, END⟩ + dsimp only at e0 ha H5 bk END ⊢ + have hP := J.iplaced + have tH : ∀ t ∈ Z.T, TcbAddr t := fun t ht => J.sr.core.tcbs t ht + have hRp : h ≠ 0#u64 → h ∈ Rp := fun c => (END.rep c).1 + have vnext : ∀ r ∈ Rp, (rNextA r).val = r.val + 16 := fun r hr => (replyNextPlaceA (Z := Z) hP.e hr).2 + have vq : q ≠ 0#u64 → (nScSc q).val = q.val + 32 := fun c => by + have e : nScSc q = Notif.nScA q := rfl + rw [e, Notif.nScA_eq, Notif.nfield_val (hP.n.ntfn q (END.ntf c).1) (by simp)]; simp + have hsub : ∀ c, ∀ r ∈ Function.update stack p [] c, r ∈ stack c := by + intro c r hr + by_cases e : c = p + · subst e; rw [Function.update_self] at hr; cases hr + · rwa [Function.update_of_ne e] at hr + have reply : ReplyStackRelA (L.mem s') Rp Z.C σ' (scNameA m) (Function.update stack p []) := by + have rw8 : ∀ r ∈ Rp, ∀ c : U64, c.val = r.val + 8 ∨ (c.val = r.val + 16 ∧ ¬ (h ≠ 0#u64 ∧ r = h)) → + Aligned c → L.mem s' c = L.mem s c := by + intro r hr c hc al + obtain ⟨r32, -, -⟩ := hP.e.rep r hr + have hw : IpcObjWA N E Rp c := Or.inr (Or.inr ⟨r, hr, by omega, by omega⟩) + refine END.wr.mem_eq al ?_ + rintro (x | ⟨c0, x⟩ | ⟨c0, x⟩ | ⟨c0, x⟩) + · exact J.ipc_offMA hw x + · have v : c.val = q.val + 32 := by rw [x, vq c0] + have := hP.nr q (END.ntf c0).1 r hr + omega + · have v : c.val = h.val + 16 := by rw [x, vnext _ (hRp c0)] + obtain ⟨h32, -, -⟩ := hP.e.rep h (hRp c0) + rcases hc with hc | ⟨hc, hn⟩ + · omega + · exact hn ⟨c0, UScalar.eq_of_val_eq (by omega)⟩ + · have hy := (END.yT c0).1 + rcases x with (x | x | x) | x + · exact J.ipc_ne_tcbA hy (K := 256#u64) (by simp) hw x.symm + · exact J.ipc_ne_tcbA hy (K := 264#u64) (by simp) hw x.symm + · exact J.ipc_ne_tcbA hy (K := 272#u64) (by simp) hw x.symm + · exact J.ipc_ne_tcbA hy (K := 3472#u64) (by simp) hw x.symm + have al : ∀ r ∈ Rp, ∀ c : U64, c.val = r.val + 8 ∨ c.val = r.val + 16 → Aligned c := by + intro r hr c hc + obtain ⟨r32, -, -⟩ := hP.e.rep r hr + show c.val % 8 = 0 + omega + have vprev : ∀ r ∈ Rp, (rPrevA r).val = r.val + 8 := fun r hr => (replyPrevPlaceA (Z := Z) hP.e hr).2 + obtain ⟨x, k, hxk, hhd⟩ := END.head + have hh : (stack p).head?.map specPtr = optRefS h := by + obtain ⟨⟨y, k', hy, hl⟩, -⟩ := J.reply.stackRel p hp + rw [hxk] at hy + cases hy + rw [← hhd, hl, List.head?_map] + refine ReplyStackRelA.unbindHeadA J.reply (fun d hd => replyPointerNonzeroA (Z := Z) hP.e hd) + (fun d hd => by have := (hP.e.rep d hd).1; omega) hp hh + (fun d hd => rw8 d hd _ (Or.inl (vprev d hd)) (al d hd _ (Or.inl (vprev d hd)))) + (fun d hd c => rw8 d hd _ (Or.inr ⟨vnext d hd, c⟩) (al d hd _ (Or.inr (vnext d hd)))) + (fun c => (END.rep c).2) ?_ ?_ (fun d hd => ?_) (fun d hd x' hx' => ?_) + · obtain ⟨y, hy, e, -⟩ := END.sc _ _ _ hxk + exact ⟨y, k, hy, e rfl⟩ + · intro c hc ne x' k' hx' + obtain ⟨y, hy, -, e⟩ := END.sc _ _ _ hx' + exact ⟨y, k', hy, e fun x => ne (J.sr.scp.inj c hc p hp x)⟩ + · obtain ⟨x', hx', -⟩ := J.ep.reply d hd + exact ⟨x', hx'⟩ + · exact END.re _ _ hx' + obtain ⟨xh, kh, hxh, -⟩ := END.head + have Qk : QKeepA σ σ' := by + refine ((((Sched.qk_of m1).trans (Sched.qk_of m2)).trans (Sched.qk_of m3)).trans (Sched.qk_of m4)).trans + (Sched.setInactive_qA m5 fun y k e => ?_) + obtain ⟨⟨x', n', hx', hl⟩, -⟩ := reply.stackRel p hp + rw [hnm, e] at hx' + cases hx' + rw [hl, Function.update_self] + rfl + obtain ⟨J', w, osk, twS⟩ := J.rec_scEndA bk H5 inv nsw F Qk END.toG hsub reply ⟨xh, kh, hxh⟩ (fun _ _ => rfl) + exact ⟨e0, ha, σ1, σ2, σ3, σ4, σ', Q', R', Ac', _, m1, m2, m3, m4, m5, J', osk, w.1, w.2.1, F, + fun t ht => twS t ht 3664#u64 (by simp) (by simp) (by simp), + fun {_} sub h => Std6GA.schedStepA J h sub w (J.sigW_osKeepA w) F⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelScSpecA.lean b/proofs/Fl4ProofsA64/P3/DelScSpecA.lean new file mode 100644 index 000000000..124e11a46 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelScSpecA.lean @@ -0,0 +1,342 @@ +/- +P3 (AArch64, delete hooks W4, SC finaliser): **`SInvXA ∧ SIdleA ∧ SActA` and "no pending switch" across the five spec +steps of a final SC cap's finalisation**, on the spec definitions alone. + +* `unbindAllTcbs_stepA` (`sched_context_unbind_all_tcbs`, an SC other than `idle_sc_ptr`), +* `scNtfnUnlink_refKA` (the state after `sched_context_unbind_ntfn`), +* `scUnbRep_invA` (the state after `sched_context_unbind_reply`), +* `completeYieldTo_refKA` (`complete_yield_to` of the yielder), +* `LoopInvA.inactScA` (the state after `sched_context_set_inactive`). + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.ReplyRemoveInvA +import Fl4ProofsA64.P3.DelScInactA +import Fl4ProofsA64.P3.ScUnbRepA +import Fl4ProofsA64.P3.ScNtfnLinkA +import Fl4ProofsA64.P3.ScYieldCompleteA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open Fl4Spec Fl4SpecA64 Fl4ProofsA64.P3.Sched + +noncomputable section + +/-- A thread bound to an SC other than `idle_sc_ptr` is not the idle thread. -/ +theorem LoopInvA.boundNotIdle {σ : State Unit} {m : McState} {n : Nat} (h : LoopInvA σ m n) {p t : ObjRef} + {x : SchedContext} {k : Nat} (hk : σ.kheap p = some (.schedContext x k)) (hp : p ≠ idleScPtr) + (ht : x.scTcb = some t) : t ≠ σ.idleThread := fun e => hp (h.sidle.f5 p x k hk (e ▸ ht)) + +/-- **`sched_context_unbind_all_tcbs`** of an SC other than `idle_sc_ptr`. -/ +theorem unbindAllTcbs_stepA {σ : State Unit} {m : McState} {n : Nat} (h : LoopInvA σ m n) {p : ObjRef} + {x : SchedContext} {k : Nat} (hk : σ.kheap p = some (.schedContext x k)) (hp : p ≠ idleScPtr) + {r : Unit × State Unit} (hr : r ∈ (schedContextUnbindAllTcbs (Z := Unit) p σ).1) : RStepA m n σ r.2 := by + unfold schedContextUnbindAllTcbs at hr + rw [nd_bind_oneSc (getSchedContext_eqA hk)] at hr + unfold Fl4Spec.NonDet.when at hr + split_ifs at hr with hc + swap + · have : r = ((), σ) := hr + rw [this] + exact RStepA.refl h + obtain ⟨t, ht⟩ := Option.ne_none_iff_exists'.1 hc.1 + have hti : t ≠ σ.idleThread := h.boundNotIdle hk hp ht + unfold schedContextUnbindTcb at hr + rw [nd_bind_oneSc (getSchedContext_eqA hk), ht, + nd_bind_oneSc (show Fl4Spec.NonDet.assertOpt (σ := State Unit) (some t) σ = ({(t, σ)}, False) from rfl), + nd_gets_bindSc] at hr + obtain ⟨a, ha, hr⟩ := mem_bindA.1 hr + have A : RStepA m n σ a.2 := by + unfold Fl4Spec.NonDet.when at ha + split_ifs at ha + · exact RStepA.of_queue (h.reschedule ha) (rescheduleRequired_memA ha).1 (rescheduleRequired_memA ha).2 + · have : a = ((), σ) := ha + rw [this] + exact RStepA.refl h + obtain ⟨b, hb, hr⟩ := mem_bindA.1 hr + have B := A.trans (tcbSchedDeq_stepA t A.inv hb) + obtain ⟨c, hc', hr⟩ := mem_bindA.1 hr + have C := B.trans (tcbReleaseRemove_stepA t B.inv hc') + obtain ⟨d, hd, hr⟩ := mem_bindA.1 hr + have D : RStepA m n σ d.2 := by + unfold setTcbObjRef at hd + obtain ⟨z, hz, ez⟩ := threadSet_memA _ _ hd + rw [ez] + exact C.trans (RStepA.tcbSc C.inv hz (by rw [C.idle]; exact hti) none (fun h0 => by cases h0)) + unfold setScObjRef at hr + obtain ⟨y, j, hy, ey⟩ := updateSchedContext_memA _ _ hr + rw [ey] + exact D.trans (RStepA.updSc D.inv hy hp rfl rfl rfl (Or.inr (fun h => by cases h))) + +/-- The state after `sched_context_unbind_ntfn` differs from the one before in a notification and an SC link. -/ +theorem scNtfnUnlink_refKA {σ : State Unit} {p q : ObjRef} {xs : SchedContext} {ns : Nat} {xn : Notification} + (hs : σ.kheap p = some (.schedContext xs ns)) (hn : σ.kheap q = some (.notification xn)) (b : Bool) : + RefK σ (scNtfnLinkStA σ p q xs ns xn b) := by + refine ⟨rfl, fun r => ?_, fun g => ?_⟩ + rotate_left + · have g1 := g.updNs q (.notification { xn with ntfnSc := if b then some p else none }) (fun _ _ e => by cases e) + have pq : p ≠ q := fun e => by rw [e] at hs; rw [hs] at hn; cases hn + exact g1.updSc (p := p) (y := xs) (y' := { xs with scNtfn := if b then some q else none }) (n := ns) + (by show Function.update σ.kheap q _ p = _; rw [Function.update_of_ne pq]; exact hs) rfl id + show RefObjA (σ.kheap r) (Function.update (Function.update σ.kheap q (some (.notification + { xn with ntfnSc := if b then some p else none }))) p (some (.schedContext + { xs with scNtfn := if b then some q else none } ns)) r) + by_cases e : r = p + · subst e; rw [Function.update_self, hs]; exact .sc _ _ _ ⟨rfl, rfl, rfl, id⟩ + · rw [Function.update_of_ne e] + by_cases e' : r = q + · subst e'; rw [Function.update_self, hn]; exact .ntfn _ _ + · rw [Function.update_of_ne e']; exact .same _ + +/-- **`sched_context_unbind_reply`** of an SC other than `idle_sc_ptr` keeps the invariants. -/ +theorem scUnbRep_invA {σ : State Unit} {m : McState} {n : Nat} (h : LoopInvA σ m n) {p : ObjRef} + {xs : SchedContext} {ns : Nat} (hk : σ.kheap p = some (.schedContext xs ns)) (hp : p ≠ idleScPtr) + (hkr : ∀ R, xs.scReplies.head? = some R → ∃ xr, σ.kheap R = some (.reply xr)) : + LoopInvA (unbRepStA σ p xs ns) m n ∧ + (unbRepStA σ p xs ns).schedulerAction = σ.schedulerAction := by + cases hl : xs.scReplies with + | nil => rw [unbRepStA_nil hl]; exact ⟨h, rfl⟩ + | cons R l => + obtain ⟨xr, hr⟩ := hkr R (by rw [hl]; rfl) + have ne : p ≠ R := fun e => by rw [e, hr] at hk; cases hk + rw [unbRepStA_cons hl, rscA_of hr] + have h1 : LoopInvA (updKSc σ R (.reply { xr with replySc := none })) m n := by + refine h.refK ⟨rfl, fun q => ?_, fun g => g.updNs R _ fun _ _ e => by cases e⟩ + show RefObjA (σ.kheap q) (Function.update σ.kheap R (some (.reply { xr with replySc := none })) q) + by_cases e : q = R + · subst e; rw [Function.update_self, hr]; exact .reply _ _ + · rw [Function.update_of_ne e]; exact .same _ + have hk1 : (updKSc σ R (.reply { xr with replySc := none })).kheap p = some (.schedContext xs ns) := by + rw [updKSc_kheap, Function.update_of_ne ne]; exact hk + exact ⟨(RStepA.updSc h1 (y := { xs with scReplies := [] }) hk1 hp rfl rfl rfl (Or.inl rfl)).inv, rfl⟩ + +/-- `sched_context_update_consumed` writes the SC's consumed time. -/ +theorem updateConsumed_refKA (p : ObjRef) {σ : State Unit} {r : Time × State Unit} + (hr : r ∈ (schedContextUpdateConsumed (Z := Unit) p σ).1) : RefK σ r.2 := by + unfold schedContextUpdateConsumed at hr + obtain ⟨sc, -, hr⟩ := eqPres_mem (getSchedContext_eqPres p) hr + obtain ⟨c, -, hr⟩ := eqPres_mem (PresR.pure _) hr + have upd : ∀ (v : Ticks) {w : Unit × State Unit}, + w ∈ (setScObjRef (Z := Unit) scConsumedUpdate p v σ).1 → RefK σ w.2 := by + intro v w hw + unfold setScObjRef at hw + obtain ⟨y, j, hy, ey⟩ := updateSchedContext_memA _ _ hw + rw [ey] + exact RefK.updSc hy ⟨rfl, rfl, rfl, id⟩ id + split_ifs at hr + all_goals + obtain ⟨a, ha, hr⟩ := mem_bindA.1 hr + have : r.2 = a.2 := by + have e : r = (_, a.2) := hr + rw [e] + rw [this] + exact upd _ ha + +/-- `sched_context_cancel_yield_to` writes the two yield links. -/ +theorem cancelYieldTo_refKA (t : ObjRef) {σ : State Unit} {r : Unit × State Unit} + (hr : r ∈ (schedContextCancelYieldTo (Z := Unit) t σ).1) : RefK σ r.2 := by + unfold schedContextCancelYieldTo at hr + have hg : PresR (@Eq (State Unit)) (getTcbObjRef (Z := Unit) Tcb.tcbYieldTo t) := by + unfold getTcbObjRef; exact threadGet_eqPres _ t + obtain ⟨yt, -, hr⟩ := eqPres_mem hg hr + cases yt with + | none => + have : r = ((), σ) := hr + rw [this] + exact RefK.refl σ + | some p => + change r ∈ ((setScObjRef scYieldFromUpdate p none >>= fun _ => setTcbObjRef tcbYieldToUpdate t none) σ).1 at hr + obtain ⟨a, ha, hr⟩ := mem_bindA.1 hr + have A : RefK σ a.2 := by + unfold setScObjRef at ha + obtain ⟨y, j, hy, ey⟩ := updateSchedContext_memA _ _ ha + rw [ey] + exact RefK.updSc hy ⟨rfl, rfl, rfl, id⟩ id + unfold setTcbObjRef at hr + obtain ⟨z, hz, ez⟩ := threadSet_memA _ _ hr + refine A.trans ?_ + rw [ez] + refine ⟨rfl, fun q => ?_, fun g => g.updNs t _ fun _ _ e => by cases e⟩ + show RefObjA (a.2.kheap q) (Function.update a.2.kheap t _ q) + by_cases e : q = t + · subst e; rw [Function.update_self, hz]; exact .tcb _ _ ⟨rfl, rfl, rfl, rfl, Or.inl rfl⟩ + · rw [Function.update_of_ne e]; exact .same _ + +/-- **`complete_yield_to`** of a thread yielding to `p`: only the consumed time, the thread's reply registers and the +two yield links change. -/ +theorem completeYieldTo_refKA {σ : State Unit} {p q : ObjRef} {x : Tcb} {y : SchedContext} {n : Nat} + (hx : σ.kheap q = some (.tcb x)) (hyt : x.tcbYieldTo = some p) (hk : σ.kheap p = some (.schedContext y n)) + {r : Unit × State Unit} (hr : r ∈ (completeYieldTo (Z := Unit) q σ).1) : RefK σ r.2 := by + have ne : q ≠ p := fun e => by rw [e, hk] at hx; cases hx + unfold completeYieldTo getTcbObjRef at hr + rw [nd_bind_oneSc (threadGet_eqSc _ hx), hyt] at hr + change r ∈ ((setConsumed p q >>= fun _ => schedContextCancelYieldTo q) σ).1 at hr + unfold setConsumed at hr + rw [nd_bind_assocSc] at hr + obtain ⟨c, hc, hr⟩ := mem_bindA.1 hr + have C : RefK σ c.2 := updateConsumed_refKA p hc + obtain ⟨tm, y', -, hup⟩ := yieldConsumed_viewA hk + rw [hup] at hc + have ec : c = (tm, updKSc σ p (.schedContext y' n)) := hc + subst ec + dsimp only at hr C + have hz : (updKSc σ p (.schedContext y' n)).kheap q = some (.tcb x) := by + rw [updKSc_kheap, Function.update_of_ne ne]; exact hx + obtain ⟨z, hz', hrep⟩ : ∃ z : Tcb, TcbEqLA x z ∧ + replyFromKernel (Z := Unit) q (0, wordsFromTime tm) (updKSc σ p (.schedContext y' n)) = + ({((), updKSc (updKSc σ p (.schedContext y' n)) q (.tcb z))}, False) := + ⟨yieldRegA (yieldRegA (yieldRegA x badge_register 0) .x2 (ucast 64 tm)) msg_info_register + (messageInfoToData ⟨natToLen 1, 0, 0, 0⟩), ⟨rfl, rfl, rfl, rfl, Or.inl rfl⟩, yield_reply_wordA hz _⟩ + rw [nd_bind_oneSc hrep] at hr + refine C.trans (RefK.trans ?_ (cancelYieldTo_refKA q hr)) + refine ⟨rfl, fun u => ?_, fun g => g.updNs q _ fun _ _ e => by cases e⟩ + show RefObjA ((updKSc σ p (.schedContext y' n)).kheap u) + (Function.update (updKSc σ p (.schedContext y' n)).kheap q (some (.tcb z)) u) + by_cases e : u = q + · subst e; rw [Function.update_self, hz]; exact .tcb _ _ hz' + · rw [Function.update_of_ne e]; exact .same _ + +/-- **The invariants across `sched_context_set_inactive`**: the SC at `p` keeps its thread and replies and is no +longer active. -/ +theorem LoopInvA.inactScA {σ : State Unit} {m : McState} {n : Nat} (h : LoopInvA σ m n) {p : ObjRef} + {x : SchedContext} {k : Nat} (hk : σ.kheap p = some (.schedContext x k)) (hpi : p ≠ idleScPtr) : + LoopInvA (updKSc σ p (.schedContext (inactScA x) k)) m n := by + obtain ⟨σ', eσ'⟩ : ∃ σ', σ' = updKSc σ p (.schedContext (Sched.inactScA x) k) := ⟨_, rfl⟩ + rw [← eσ'] + have far : ∀ q, q ≠ p → σ'.kheap q = σ.kheap q := fun q e => by + rw [eσ', updKSc_kheap, Function.update_of_ne e] + have here : σ'.kheap p = some (.schedContext (Sched.inactScA x) k) := by + rw [eσ', updKSc_kheap, Function.update_self] + have hcur : σ'.curThread = σ.curThread := by rw [eσ']; rfl + have hidle : σ'.idleThread = σ.idleThread := by rw [eσ']; rfl + have hcsc : σ'.curSc = σ.curSc := by rw [eσ']; rfl + have hrq : σ'.readyQueues = σ.readyQueues := by rw [eσ']; rfl + have hrls : σ'.releaseQueue = σ.releaseQueue := by rw [eσ']; rfl + have hact : σ'.schedulerAction = σ.schedulerAction := by rw [eσ']; rfl + have hnode : σ'.interruptIrqNode = σ.interruptIrqNode := by rw [eσ']; rfl + have tcbF : ∀ q y, σ.kheap q = some (.tcb y) → σ'.kheap q = some (.tcb y) := fun q y e => by + have : q ≠ p := fun c => by rw [c, hk] at e; cases e + rw [far q this]; exact e + have tcbB : ∀ q y, σ'.kheap q = some (.tcb y) → σ.kheap q = some (.tcb y) := fun q y e => by + have : q ≠ p := fun c => by rw [c, here] at e; cases e + rw [far q this] at e; exact e + have scF : ∀ q a j, σ.kheap q = some (.schedContext a j) → ∃ b, σ'.kheap q = some (.schedContext b j) ∧ + b.scTcb = a.scTcb ∧ b.scReplies = a.scReplies := fun q a j e => by + by_cases c : q = p + · subst c; rw [hk] at e; cases e; exact ⟨_, here, rfl, rfl⟩ + · exact ⟨a, by rw [far q c]; exact e, rfl, rfl⟩ + have scB : ∀ q b j, σ'.kheap q = some (.schedContext b j) → ∃ a, σ.kheap q = some (.schedContext a j) ∧ + b.scTcb = a.scTcb ∧ b.scReplies = a.scReplies ∧ (scActive b = true → b = a) := fun q b j e => by + by_cases c : q = p + · subst c; rw [here] at e; cases e + exact ⟨x, hk, rfl, rfl, fun a => by simp [scActive, activeSc, Sched.inactScA] at a⟩ + · rw [far q c] at e; exact ⟨b, e, rfl, rfl, fun _ => rfl⟩ + have hct : ∀ j q, capTableAt j q σ' ↔ capTableAt j q σ := fun j q => by + unfold capTableAt + by_cases c : q = p + · subst c; rw [here, hk]; simp + · rw [far q c] + have capB : ∀ q o b c, σ'.kheap q = some o → capOf o b = some c → + ∃ o0, σ.kheap q = some o0 ∧ capOf o0 b = some c := fun q o b c e hc => by + by_cases d : q = p + · subst d; rw [here] at e; cases e; cases hc + · rw [far q d] at e; exact ⟨o, e, hc⟩ + have specCap : ∀ sl, specCapAtA σ' sl = specCapAtA σ sl := fun sl => by + unfold specCapAtA + by_cases c : sl.1 = p + · rw [c, here, hk] + · rw [far _ c] + have CF := CurFactsA.back (σ := σ) (σ' := σ') ⟨h.sinv.zc, h.sinv.rlsRun, h.sinv.curScReady⟩ + (fun t ht => by rw [hrls] at ht; exact ht) (by rw [eσ']; rfl) hcsc (by rw [eσ']; rfl) + (fun q y e => ⟨y, tcbB _ _ e, rfl⟩) + (fun q x e _ => ⟨x, tcbF _ _ e, id⟩) + (fun y k e => by + obtain ⟨a, ha, -, -, fa⟩ := scB _ y k e + refine ⟨a, ha, fun act => ?_⟩ + have := fa act + subst this + exact ⟨act, id⟩) + have S := h.sinv + refine + { sinv := + { mc := S.mc + view := by unfold McView; rw [hidle]; exact S.view + curTcb := by + obtain ⟨a, ha⟩ := S.curTcb + exact ⟨a, by rw [hcur]; exact tcbF _ _ ha⟩ + idleTcb := by + obtain ⟨a, ha, hs⟩ := S.idleTcb + exact ⟨a, by rw [hidle]; exact tcbF _ _ ha, hs⟩ + onlyIdle := fun t y hy hs => S.onlyIdle t y (tcbB _ _ hy) hs + idleSc := by + obtain ⟨a, y, j, h1, h2, h3, h4⟩ := S.idleSc + obtain ⟨y', h3', et, -⟩ := scF _ _ _ h3 + exact ⟨a, y', j, by rw [hidle]; exact tcbF _ _ h1, h2, h3', by rw [hidle, et]; exact h4⟩ + curSc := by + obtain ⟨y, j, hy⟩ := S.curSc + obtain ⟨y', h', -⟩ := scF _ _ _ hy + exact ⟨y', j, by rw [hcsc]; exact h'⟩ + ctQ := fun ha d q => by rw [hcur, hrq]; exact S.ctQ (hact.symm.trans ha) d q + idleQ := fun d q => by rw [hrq]; exact S.idleQ d q + rq := fun d q t ht => by + rw [hrq] at ht + obtain ⟨a, ha, hr⟩ := S.rq d q t ht + exact ⟨a, tcbF _ _ ha, hr⟩ + rls := fun t ht => by + rw [hrls] at ht + obtain ⟨a, ha⟩ := S.rls t ht + exact ⟨a, tcbF _ _ ha⟩ + rpos := fun q y j hy a => by + obtain ⟨y0, hy0, -, -, e⟩ := scB q y j hy + have e' := e a + subst e' + exact S.rpos q y j hy0 a + zc := CF.1 + rlsRun := CF.2.1 + curScReady := CF.2.2 + faultSt := fun t y hy f => S.faultSt t y (tcbB _ _ hy) f + irqN := by + obtain ⟨⟨⟨hinj, hcn⟩, hdom⟩, hsl, href⟩ := S.irqN + refine ⟨⟨⟨?_, fun irq hi => ?_⟩, fun irq hi j hj => ?_⟩, fun irq hi o b c ho hc => ?_, + fun q o b c ho hc => ?_⟩ + · rw [hnode]; exact hinj + · rw [hnode]; exact (hct _ _).2 (hcn irq hi) + · rw [hnode] at hj; exact hdom irq hi j ((hct _ _).1 hj) + · rw [hnode] at ho + obtain ⟨o0, h0, hc0⟩ := capB _ _ _ _ ho hc + exact hsl irq hi o0 b c h0 hc0 + · rw [hnode] + obtain ⟨o0, h0, hc0⟩ := capB _ _ _ _ ho hc + exact href q o0 b c h0 hc0 } + sidle := + { f3 := fun c hc => by + obtain ⟨a, ha, hs⟩ := h.sidle.f3 c hc + exact ⟨a, tcbF _ _ ha, hs⟩ + f4 := fun y j hy => by + obtain ⟨a, ha, -, er, -⟩ := scB _ y j hy + rw [er]; exact h.sidle.f4 a j ha + f5 := fun q y j hy he => by + obtain ⟨a, ha, et, -⟩ := scB q y j hy + rw [hidle, et] at he + exact h.sidle.f5 q a j ha he + f6 := h.sidle.f6.of_eq specCap + f7 := fun y j hy => by + rw [far _ (Ne.symm hpi)] at hy + rw [show σ'.curTime = σ.curTime by rw [eσ']; rfl] + exact h.sidle.f7 y j hy + idleSlot := fun c hc => by + obtain ⟨a, ha, -⟩ := h.sidle.f3 c hc + exact (idleVtSameA (by rw [ha]; exact tcbF _ _ ha) (h.sidle.idleSlot c hc) (h.sidle.idleBound c hc)).1 + idleBound := fun c hc => by + obtain ⟨a, ha, -⟩ := h.sidle.f3 c hc + exact (idleVtSameA (by rw [ha]; exact tcbF _ _ ha) (h.sidle.idleSlot c hc) (h.sidle.idleBound c hc)).2 } + sact := + { f1 := fun u eu => by + rw [hact] at eu; rw [hcur, hidle]; exact h.sact.f1 u eu + f2 := by + obtain ⟨a, ha, hs⟩ := h.sact.f2 + exact ⟨a, by rw [hcur]; exact tcbF _ _ ha, hs⟩ } } + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelScStallA.lean b/proofs/Fl4ProofsA64/P3/DelScStallA.lean new file mode 100644 index 000000000..3574892f9 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelScStallA.lean @@ -0,0 +1,112 @@ +/- +P3 (AArch64, delete hooks W4, SC finaliser): **`remote_tcb_stall` of a bound thread of this core stalls nothing, with +or without a bound VCPU** (AArch64's stall also looks at a bound VCPU's `RUN_CALLER`), and `unbind_all_tcbs` with +that hypothesis. + +* `stall_localA`: the thread, and a bound VCPU's run caller, are on this core. +* `JStage3TA.stall_singleA`: at one core ([S] `h1`) the record gives it for every TCB (`vcRun`, `onCore_single`). +* `sc_unbind_all_tcbs_stA`: `P3/ScUnbAllA`'s `sc_unbind_all_tcbs_localA` with the stall as a hypothesis. + +Premises: [P] `hCs`; [S] `h1`. New module. +-/ +import Fl4ProofsA64.P3.ScUnbAllA +import Fl4ProofsA64.P3.DelNtfnRecA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Sched + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + +/-- **The stall of a thread of this core stalls nothing** (module docstring). -/ +theorem stall_localA (a : A) {s : S} {t : U64} (hT : TcbAddr t) (haff : L.mem s (affA t) = L.cpu s) + (hrun : ∀ v, L.mem s (wa t 3664#u64) = v → v ≠ 0#u64 → + L.mem s (wa v 24#u64) = 0#u64 ∨ L.mem s (affA (L.mem s (wa v 24#u64))) = L.cpu s) : + smp.remote_tcb_stall st ar s a t = ok (s, a) := by + have e1 : obj.Tcb.affinity st t s = ok (L.cpu s) := by + rw [eq_of_spec_fd (obj.Tcb.affinity.spec (L := L) s t), haff] + have eb : vcpu.bound st s t = ok (L.mem s (wa t 3664#u64)) := by + unfold vcpu.bound + rw [eq_of_spec_fd Fl4ProofsA64.P0.Consts.layout.tcb.VCPU.spec] + simp only [bind_ok, lift, L.read_eq] + have eR : vcpu.RUN_CALLER = 24#u64 := by unfold vcpu.RUN_CALLER; rfl + unfold smp.remote_tcb_stall + rw [e1, bind_ok, L.cur_cpu_eq s, bind_ok] + rw [if_neg (by simp)] + simp only [bind_ok, uncurry_apply_pair] + rw [eb, bind_ok] + by_cases hv : L.mem s (wa t 3664#u64) = 0#u64 + · rw [if_neg (by simp [hv])] + rw [if_pos (by simpa using hv), eR] + simp only [lift, bind_ok, L.read_eq] + by_cases hc : L.mem s (wa (L.mem s (wa t 3664#u64)) 24#u64) = 0#u64 + · rw [if_neg (by simp [hc])] + rw [if_pos (by simpa using hc)] + have haff2 := (hrun _ rfl hv).resolve_left hc + rw [eq_of_spec_fd (obj.Tcb.affinity.spec (L := L) s _), bind_ok, haff2, L.cur_cpu_eq s, bind_ok, + if_neg (by simp)] + +/-- **At one core the record gives the stall for every TCB** (module docstring). -/ +theorem JStage3TA.stall_singleA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} + {κ : U64 → U64} {n : Nat} {J5 J7 : JSlotA S} [J7PpA st J7] {s : S} {a : A} {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < Fl4SpecA64.fl4A64Platform.numNodes) + (h1 : Fl4SpecA64.fl4A64Platform.numNodes = 1) {t : U64} (ht : t ∈ Z.T) : + smp.remote_tcb_stall st ar s a t = ok (s, a) := by + refine stall_localA a (J.sr.core.tcbs t ht) (J.onCore_single hCs h1 ht) fun v ev hv => ?_ + have vV : v ∈ Z.V := by + rcases J.vcRun.tcb t ht with h | h + · exact absurd (ev ▸ h) hv + · exact ev ▸ h + rcases J.vcRun.vcpu v vV with h | h + · exact Or.inl h + · exact Or.inr (J.onCore_single hCs h1 h) + +/-- **`unbind_all_tcbs`** for a non-idle SC whose bound thread's stall does nothing (`P3/ScUnbAllA`). -/ +theorem sc_unbind_all_tcbs_stA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} + {σ : Fl4SpecA64.State Unit} (H : SchedSRA st Z m κ s σ) (hst : StEqA Z.T (L.mem s) σ) (a : A) + {sc : U64} (hsc : sc ∈ Z.C) (hsc0 : sc ≠ 0#u64) (hni : scNameA m sc ≠ Fl4SpecA64.idleScPtr) + (hbind : ∀ t, L.mem s (scTcbA sc) = t → t ≠ 0#u64 → + t ∈ Z.T ∧ L.mem s (scA t) = sc ∧ L.mem s (affA t) = L.cpu s ∧ + smp.remote_tcb_stall st ar s a t = ok (s, a)) : + schedcontext.unbind_all_tcbs st ar s a sc ⦃ r => r.2 = a ∧ ∃ σ' Q' R', + Fl4SpecA64.schedContextUnbindAllTcbs (Z := Unit) (scNameA m sc) σ = ({((), σ')}, False) ∧ + SchedSRA st { Z with Q := Q', R := R' } m κ r.1 σ' ∧ MFrA (st := st) Z m s r.1 σ σ' ⦄ := by + obtain ⟨xs, ns, hk, hx⟩ := H.scr.sc sc hsc + obtain ⟨t, et⟩ : ∃ t, L.mem s (scTcbA sc) = t := ⟨_, rfl⟩ + have hxT : xs.scTcb = optRefS t := by + rw [← et, scTcbA_eqSc] + exact hx.tcb + unfold schedcontext.unbind_all_tcbs + refine WP.spec_bind (obj.Sc.tcb.spec s sc) ?_ + rintro i rfl + rw [et] + by_cases ht0 : t = 0#u64 + · rw [if_neg (by simp [ht0]), WP.spec_ok] + refine ⟨rfl, σ, Z.Q, Z.R, ?_, H, MFrA.refl Z m s σ⟩ + exact schedContextUnbindAllTcbs_noneA hk (by rw [hxT, ht0, optRefS_zero]) + · rw [if_pos (by simpa using ht0)] + obtain ⟨ht, hts, hon, hstall⟩ := hbind t et ht0 + rw [hstall, bind_ok] + refine WP.spec_bind (sc_unbind_tcb_localA H hst hsc hsc0 ht et hts hon) ?_ + rintro s' ⟨σ', Q', R', hu, H', F⟩ + rw [WP.spec_ok] + refine ⟨rfl, σ', Q', R', ?_, H', F⟩ + unfold Fl4SpecA64.schedContextUnbindAllTcbs + rw [nd_bind_oneSc (getSchedContext_eqA hk), hxT, optRefS_ne ht0] + unfold Fl4Spec.NonDet.when + rw [if_pos ⟨by simp, hni⟩] + exact hu + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelSidleStepA.lean b/proofs/Fl4ProofsA64/P3/DelSidleStepA.lean new file mode 100644 index 000000000..49223e82a --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelSidleStepA.lean @@ -0,0 +1,238 @@ +/- +P3 (AArch64, delete hooks; entry record edit F1-F6, `SIdleA` / `SActA`): **the new record fields across the loop's +slot steps**. The swap, the emptying and the zombie write only move caps between slots or write a null / zombie cap, +so no idle-thread or idle-SC cap appears (`SIdleA.slotA`). `SActA`'s keep is `P3/DelSactSlotA`. No named premise; new module. +-/ +import Fl4ProofsA64.P3.Stage3CapFrameA +import Fl4ProofsA64.P3.Stage3FBootA +import Fl4ProofsA64.P3.CnodeDelStepA +import Fl4ProofsA64.P3.DelStepSpecA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open Fl4SpecA64 Fl4Spec + +/-- The caps of the new state's objects are old caps of the same slot or satisfy `P`; CNode well-formedness is +kept. -/ +def CapProvA (P : Cap → Prop) (σ σ' : State Unit) : Prop := + WfKeepA σ σ' ∧ ∀ q o', σ'.kheap q = some o' → ∀ b a, capOf o' b = some a → + P a ∨ ∃ o, σ.kheap q = some o ∧ capOf o b = some a + +theorem CapProvA.of_kheap {P : Cap → Prop} {σ σ' : State Unit} (h : σ'.kheap = σ.kheap) : CapProvA P σ σ' := + ⟨WfKeepA.of_kheap h, fun q o' hq b a hc => Or.inr ⟨o', by rw [← h]; exact hq, hc⟩⟩ + +theorem CapProvA.trans {P : Cap → Prop} {σ1 σ2 σ3 : State Unit} (h1 : CapProvA P σ1 σ2) (h2 : CapProvA P σ2 σ3) : + CapProvA P σ1 σ3 := + ⟨h1.1.trans h2.1, fun q o3 hq b a hc => by + rcases h2.2 q o3 hq b a hc with hp | ⟨o2, hq2, hc2⟩ + · exact Or.inl hp + · exact h1.2 q o2 hq2 b a hc2⟩ + +theorem setCap_provA' {P : Cap → Prop} {c : Cap} (hc : P c) (cte : CslotPtr) {σ : State Unit} {r : Unit × State Unit} + (hr : r ∈ (setCap (Z := Unit) c cte σ).1) : CapProvA P σ r.2 := by + obtain ⟨o, o', hk, hσ, hcap, hw⟩ := setCap_memA (p := cte.1) (cref := cte.2) hr + have k2 : r.2.kheap = Function.update σ.kheap cte.1 (some o') := by rw [hσ] + refine ⟨fun q n => ?_, fun q x hq b a ha => ?_⟩ + · by_cases e : q = cte.1 + · subst e + rw [k2, Function.update_self, hk] + constructor + · rintro ⟨cs, e1, w⟩ + obtain ⟨cs0, e0, w0⟩ := (hw n).1 ⟨cs, Option.some.inj e1, w⟩ + exact ⟨cs0, by rw [e0], w0⟩ + · rintro ⟨cs, e1, w⟩ + obtain ⟨cs0, e0, w0⟩ := (hw n).2 ⟨cs, Option.some.inj e1, w⟩ + exact ⟨cs0, by rw [e0], w0⟩ + · rw [k2, Function.update_of_ne e] + · by_cases e : q = cte.1 + · subst e + rw [k2, Function.update_self] at hq + cases hq + rcases hcap b a ha with ⟨-, ea⟩ | h + · exact Or.inl (ea ▸ hc) + · exact Or.inr ⟨o, hk, h⟩ + · rw [k2, Function.update_of_ne e] at hq + exact Or.inr ⟨x, hq, ha⟩ + +/-- **A spec cap of a slot after a slot-only step**: `P`, or the slot's old cap, given that provenance for the slot's +object. -/ +theorem specCapAtA_provAtA {σ σ' : State Unit} (HS : SpecSlotA σ σ') (W : WfKeepA σ σ') {P : Prop} {sl : CslotPtr} + {a : Cap} (C : ∀ o', σ'.kheap sl.1 = some o' → capOf o' sl.2 = some a → + P ∨ ∃ o, σ.kheap sl.1 = some o ∧ capOf o sl.2 = some a) + (hs : specCapAtA σ' sl = some a) : P ∨ specCapAtA σ sl = some a := by + obtain ⟨o', k1, hc⟩ := specCapAtA_of_capOf hs + rcases C o' k1 hc with hp | ⟨o, k0, h0⟩ + · exact Or.inl hp + right + rcases HS.kh sl.1 with e' | ⟨r, cs, cs', e1, e2⟩ | ⟨y, y', e1, e2, -⟩ + · unfold specCapAtA at hs ⊢; rwa [← e'] + · rw [k0] at e1; rw [k1] at e2; cases e1; cases e2 + unfold specCapAtA at hs ⊢ + rw [k1] at hs; rw [k0] + simp only at hs ⊢ + split at hs + case isFalse => cases hs + case isTrue hwf => + obtain ⟨cs2, e3, w3⟩ := (W sl.1 r).1 ⟨cs', k1, hwf⟩ + rw [k0] at e3; cases e3 + rw [if_pos w3] + exact h0 + · rw [k0] at e1; rw [k1] at e2; cases e1; cases e2 + unfold specCapAtA + rw [k0] + exact h0 + +/-- **F3–F6 across a slot-only spec step** with cap provenance. -/ +theorem SIdleA.provA {σ σ' : State Unit} {m : McState} {n : Nat} (h : SIdleA σ m n) (HS : SpecSlotA σ σ') + (C : CapProvA (NoIdleCapV m n) σ σ') (hv : IdleSlotNewA σ σ' m n) : SIdleA σ' m n := by + refine h.slotA HS (fun sl a hs => ?_) hv + rcases specCapAtA_provAtA HS C.1 (P := NoIdleCapV m n a) (fun o' k1 hc => C.2 sl.1 o' k1 sl.2 a hc) hs with g | g + · exact Or.inr g + · exact Or.inl g + +/-- The cap of one slot after a step is the slot's old one; CNode well-formedness is kept. -/ +def SlotProvA (S : CslotPtr) (σ σ' : State Unit) : Prop := + WfKeepA σ σ' ∧ ∀ o', σ'.kheap S.1 = some o' → ∀ a, capOf o' S.2 = some a → + ∃ o, σ.kheap S.1 = some o ∧ capOf o S.2 = some a + +theorem SlotProvA.of_kheap {S : CslotPtr} {σ σ' : State Unit} (h : σ'.kheap = σ.kheap) : SlotProvA S σ σ' := + ⟨WfKeepA.of_kheap h, fun o' hq a hc => ⟨o', by rw [← h]; exact hq, hc⟩⟩ + +theorem SlotProvA.trans {S : CslotPtr} {σ1 σ2 σ3 : State Unit} (h1 : SlotProvA S σ1 σ2) (h2 : SlotProvA S σ2 σ3) : + SlotProvA S σ1 σ3 := + ⟨h1.1.trans h2.1, fun o3 hq a hc => by + obtain ⟨o2, hq2, hc2⟩ := h2.2 o3 hq a hc + exact h1.2 o2 hq2 a hc2⟩ + +theorem setCap_slotProvA {S cte : CslotPtr} (ne : cte ≠ S) {c : Cap} {σ : State Unit} {r : Unit × State Unit} + (hr : r ∈ (setCap (Z := Unit) c cte σ).1) : SlotProvA S σ r.2 := by + refine ⟨(setCap_provA' (P := fun _ => True) trivial cte hr).1, fun x hq a ha => ?_⟩ + obtain ⟨o, o', hk, hσ, hcap, -⟩ := setCap_memA (p := cte.1) (cref := cte.2) hr + have k2 : r.2.kheap = Function.update σ.kheap cte.1 (some o') := by rw [hσ] + by_cases e : S.1 = cte.1 + · rw [k2, e, Function.update_self] at hq + cases hq + rcases hcap _ _ ha with ⟨eb, -⟩ | g + · exact absurd (Prod.ext e eb).symm ne + · exact ⟨o, by rw [e]; exact hk, g⟩ + · rw [k2, Function.update_of_ne e] at hq + exact ⟨x, hq, ha⟩ + +theorem IdleSlotNewA.of_slotProvA {σ σ' : State Unit} {m : McState} {n : Nat} (HS : SpecSlotA σ σ') + (C : ∀ c < n, SlotProvA (m.idleThreadOf c, tcbCnodeIndex 1) σ σ') : IdleSlotNewA σ σ' m n := fun c hc a ha => by + rcases specCapAtA_provAtA HS (C c hc).1 (P := False) (fun o' k1 h => Or.inr ((C c hc).2 o' k1 a h)) ha with g | g + · exact g.elim + · exact Or.inl g + +/-- A slot at a list of `false` bits is no thread's slot 1. -/ +theorem replicate_ne_idx1A (q t : ObjRef) (k : Nat) : (q, List.replicate k false) ≠ ((t, tcbCnodeIndex 1) : CslotPtr) := by + intro e + have e2 : List.replicate k false = tcbCnodeIndex 1 := congrArg Prod.snd e + have h : true ∈ tcbCnodeIndex 1 := by decide + rw [← e2] at h + simp at h + +/-- A slot holding a non-null FL4 cap is no idle thread's slot 1. -/ +theorem SIdleA.neIdleSlotA {σ : State Unit} {m : McState} {n : Nat} (h : SIdleA σ m n) {th : TypedHeapA} + {mem : U64 → U64} (hR : StateRelA th mem σ) {sl : CslotPtr} (hA : AbsSlotA th sl) + (hc : capAtA mem (cslotAddrA sl) ≠ .Null) : ∀ c < n, sl ≠ (m.idleThreadOf c, tcbCnodeIndex 1) := + fun c hc' e => specCap_nnA hR hA hc (by rw [e]; exact h.idleSlotCapA hc') + +/-- `SIdleA.neIdleSlotA` at the FL4 state's slot read. -/ +theorem SIdleA.neIdleSlotCapA {S : Type} {st : fl4_core_a64.machine.Store S} [L : Fl4ProofsA64.P1.LawfulStore st] {s : S} + {σ : State Unit} {m : McState} {n : Nat} (h : SIdleA σ m n) {th : TypedHeapA} (hR : StateRelA th (L.mem s) σ) + {sl : CslotPtr} (hA : AbsSlotA th sl) (hc : Fl4ProofsA64.P1.W7.R.ST.capA st s (cslotAddrA sl) ≠ .Null) : + ∀ c < n, sl ≠ (m.idleThreadOf c, tcbCnodeIndex 1) := + h.neIdleSlotA hR hA hc + +/-- A cap read from a slot is not an idle cap. -/ +theorem SIdleA.readA {σ σ' : State Unit} {m : McState} {n : Nat} (h : SIdleA σ m n) {sl : CslotPtr} {c : Cap} + (hr : (c, σ') ∈ (getCap (Z := Unit) sl σ).1) : NoIdleCapV m n c := by + have e := getCap_specAtA hr + exact ⟨fun i hi ec => (h.f6 sl).1 i hi (ec ▸ e), fun b ec => (h.f6 sl).2.1 b (ec ▸ e), (h.f6 sl).2.2 c e⟩ + +theorem SIdleA.swapForDeleteA {σ σ' : State Unit} {m : McState} {n : Nat} (h : SIdleA σ m n) (HS : SpecSlotA σ σ') + {s1 s2 : CslotPtr} (n1' : ∀ c < n, s1 ≠ (m.idleThreadOf c, tcbCnodeIndex 1)) + (n2' : ∀ c < n, s2 ≠ (m.idleThreadOf c, tcbCnodeIndex 1)) + (hr : ((), σ') ∈ (capSwapForDelete (Z := Unit) s1 s2 σ).1) : SIdleA σ' m n := by + suffices g : CapProvA (NoIdleCapV m n) σ σ' ∧ ∀ c < n, SlotProvA (m.idleThreadOf c, tcbCnodeIndex 1) σ σ' from + h.provA HS g.1 (IdleSlotNewA.of_slotProvA HS g.2) + unfold capSwapForDelete NonDet.when at hr + split_ifs at hr with hne + swap + · have e : σ'.kheap = σ.kheap := by rw [show σ' = σ from congrArg Prod.snd (show ((), σ') = ((), σ) from hr)] + exact ⟨CapProvA.of_kheap e, fun _ _ => SlotProvA.of_kheap e⟩ + obtain ⟨x1, hx1, hr⟩ := mem_bindA.1 hr + have e1 : x1.2 = σ := getCap_stateA s1 σ x1 hx1 + obtain ⟨x2, hx2, hr⟩ := mem_bindA.1 hr + have e2 : x2.2 = σ := (getCap_stateA s2 x1.2 x2 hx2).trans e1 + have n1 : NoIdleCapV m n x1.1 := h.readA hx1 + have n2 : NoIdleCapV m n x2.1 := by + rw [e1] at hx2 + exact h.readA hx2 + rw [e2] at hr + unfold capSwap at hr + obtain ⟨a, ha, hr⟩ := mem_bindA.1 hr + have pa := setCap_provA' (P := NoIdleCapV m n) n2 _ ha + obtain ⟨b, hb, hr⟩ := mem_bindA.1 hr + have pb := setCap_provA' (P := NoIdleCapV m n) n1 _ hb + suffices k : σ'.kheap = b.2.kheap from + ⟨(pa.trans pb).trans (CapProvA.of_kheap k), fun c hc => by + refine ((setCap_slotProvA ?_ ha).trans (setCap_slotProvA ?_ hb)).trans (SlotProvA.of_kheap k) + all_goals first | exact n1' c hc | exact n2' c hc⟩ + have key : NdK (do + let slot1P ← NonDet.gets fun s : State Unit => s.cdt s1 + let slot2P ← NonDet.gets fun s : State Unit => s.cdt s2 + let cdt ← NonDet.gets fun s : State Unit => s.cdt + let cdt' ← pure (fun k => if cdt k = some s1 then some s2 + else if cdt k = some s2 then some s1 else cdt k) + setCdt (Function.update (Function.update cdt' s1 (cdt' s2)) s2 (cdt' s1)) + doExtendedOp (capSwapExt s1 s2 slot1P slot2P) + let isOriginal ← NonDet.gets fun s : State Unit => s.isOriginalCap + setOriginal s1 (isOriginal s2) + setOriginal s2 (isOriginal s1)) := by + repeat' first + | exact ndk_pure _ | exact ndk_pure' _ | exact ndk_gets _ | exact ndk_setCdt _ + | exact ndk_doExtendedOp _ | exact ndk_setOriginal _ _ + | (refine NdK.bind ?_ (fun _ => ?_)) + exact (key _ _ hr).1 + +theorem SIdleA.emptySlotA {σ σ' : State Unit} {m : McState} {n : Nat} (h : SIdleA σ m n) (HS : SpecSlotA σ σ') + {sl : CslotPtr} (hr : ((), σ') ∈ (emptySlot (Z := Unit) sl .nullCap σ).1) : SIdleA σ' m n := by + suffices g : CapProvA (· = .nullCap) σ σ' by + refine h.provA HS ⟨g.1, fun q o' hq b a hc => (g.2 q o' hq b a hc).imp_left fun (e : a = .nullCap) => by + rw [e]; exact NoIdleCapV.null⟩ fun c _ a ha => ?_ + rcases specCapAtA_provAtA HS g.1 (P := a = .nullCap) (fun o' k1 hc => g.2 _ o' k1 _ a hc) ha with e | e + · exact Or.inr e + · exact Or.inl e + unfold emptySlot at hr + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + have ex : x.2 = σ := getCap_stateA sl σ x hx + rw [ex] at hr + by_cases hc : x.1 = .nullCap + · rw [if_pos hc] at hr + exact CapProvA.of_kheap (by rw [show σ' = σ from congrArg Prod.snd (show ((), σ') = ((), σ) from hr)]) + rw [if_neg hc] at hr + have step : ∀ {α β : Type} {f : NonDet (State Unit) α} {g : α → NonDet (State Unit) β} {s : State Unit} + {y : β × State Unit}, NdK f → y ∈ ((f >>= g) s).1 → ∃ z : α × State Unit, z.2.kheap = s.kheap ∧ + y ∈ (g z.1 z.2).1 := fun hf hy => by + obtain ⟨z, hz, hy⟩ := mem_bindA.1 hy + exact ⟨z, (hf _ z hz).1, hy⟩ + obtain ⟨z1, k1, hr⟩ := step (ndk_gets _) hr + obtain ⟨z2, k2, hr⟩ := step (ndk_gets _) hr + obtain ⟨z3, k3, hr⟩ := step (ndk_pure' _) hr + obtain ⟨z4, k4, hr⟩ := step (ndk_setCdt _) hr + obtain ⟨z5, k5, hr⟩ := step (ndk_doExtendedOp _) hr + obtain ⟨z6, k6, hr⟩ := step (ndk_setOriginal _ _) hr + obtain ⟨z7, hz7, hr⟩ := mem_bindA.1 hr + have p7 := setCap_provA' (P := (· = .nullCap)) rfl _ hz7 + have k8 := (ndk_postCapDeletion _ _ _ hr).1 + have k0 : z6.2.kheap = σ.kheap := k6.trans (k5.trans (k4.trans (k3.trans (k2.trans k1)))) + exact (CapProvA.of_kheap k0).trans (p7.trans (CapProvA.of_kheap k8)) + +theorem SIdleA.setCapZombieA {σ σ' : State Unit} {m : McState} {n : Nat} (h : SIdleA σ m n) (HS : SpecSlotA σ σ') + {sl : CslotPtr} {c : Cap} (hc : NoIdleCapV m n c) (ne : ∀ i < n, sl ≠ (m.idleThreadOf i, tcbCnodeIndex 1)) + (hr : ((), σ') ∈ (setCap (Z := Unit) c sl σ).1) : SIdleA σ' m n := + h.provA HS (setCap_provA' hc sl hr) (IdleSlotNewA.of_slotProvA HS fun i hi => setCap_slotProvA (ne i hi) hr) + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelSinvLoopA.lean b/proofs/Fl4ProofsA64/P3/DelSinvLoopA.lean new file mode 100644 index 000000000..0a261300f --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelSinvLoopA.lean @@ -0,0 +1,1116 @@ +/- +P3 (aarch64, delete hooks W3, contract §47 / §51): **`SInvXA ∧ SIdleA ∧ SActA` across the spec's +`cancel_all_signals`**, on the spec definitions alone (no concrete program). + +* `RefK σ σ'`: a step that only replaces notifications, or SCs' refill lists keeping non-emptiness (the notification + dequeue and the sporadic `refill_unblock_check`; the latter is the "refill check keeps `rpos`" lemma of §51). +* `QueueE P σ σ'`: a step that only writes the scheduler action and enqueues threads satisfying `P` into ready + queues (`possible_switch_to`, `reschedule_required`). +* `cancelAllSignals_invA`: every outcome keeps `LoopInvA` (the three invariants), given that the queued threads are + distinct TCBs blocked on a notification (from `NtfnStateRel` at the hook). + +No named premise; new module. +-/ +import Fl4ProofsA64.P3.NtfnCancelAllA +import Fl4ProofsA64.P3.RestartA +import Fl4ProofsA64.P3.Stage3RestartA +import Fl4ProofsA64.P3.ScSwitchA +import Fl4ProofsA64.P3.SpecFrA +import Fl4ProofsA64.P3.SpecFrameCalcA +import Fl4ProofsA64.P3.Stage3TA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open Fl4Spec Fl4SpecA64 Fl4ProofsA64.P3.Sched + +noncomputable section + +/-! ## The two spec step shapes -/ + +/-- The TCB fields the three invariants read. -/ +def TcbEqLA (x y : Tcb) : Prop := + y.tcbState = x.tcbState ∧ y.tcbSchedContext = x.tcbSchedContext ∧ tcbCnodeMap y = tcbCnodeMap x ∧ + y.tcbArch.tcbVcpu = x.tcbArch.tcbVcpu ∧ (y.tcbFault = x.tcbFault ∨ y.tcbFault = none) + +/-- The SC fields the three invariants read (a non-empty refill list stays non-empty). -/ +def ScEqLA (x y : SchedContext) : Prop := + y.scTcb = x.scTcb ∧ y.scReplies = x.scReplies ∧ y.scRefillMax = x.scRefillMax ∧ + (x.scRefills ≠ [] → y.scRefills ≠ []) + +/-- The object changes of the finaliser's unlinks, dequeue and refill check. -/ +inductive RefObjA : Option KernelObject → Option KernelObject → Prop + | same (o : Option KernelObject) : RefObjA o o + | ntfn (a b : Notification) : RefObjA (some (.notification a)) (some (.notification b)) + | tcb (x y : Tcb) (h : TcbEqLA x y) : RefObjA (some (.tcb x)) (some (.tcb y)) + | sc (x y : SchedContext) (k : Nat) (h : ScEqLA x y) : + RefObjA (some (.schedContext x k)) (some (.schedContext y k)) + | ep (a b : Endpoint) : RefObjA (some (.endpoint a)) (some (.endpoint b)) + | reply (a b : Reply) : RefObjA (some (.reply a)) (some (.reply b)) + +theorem RefObjA.trans {a b c : Option KernelObject} (h1 : RefObjA a b) (h2 : RefObjA b c) : RefObjA a c := by + cases h1 with + | same => exact h2 + | ntfn x y => + cases h2 with + | same => exact .ntfn _ _ + | ntfn _ z => exact .ntfn _ _ + | tcb x y h => + cases h2 with + | same => exact .tcb _ _ h + | tcb _ z h' => + exact .tcb _ _ ⟨h'.1.trans h.1, h'.2.1.trans h.2.1, h'.2.2.1.trans h.2.2.1, h'.2.2.2.1.trans h.2.2.2.1, + by rcases h'.2.2.2.2 with e1 | e1 + · rcases h.2.2.2.2 with e2 | e2 + · exact Or.inl (e1.trans e2) + · exact Or.inr (e1.trans e2) + · exact Or.inr e1⟩ + | sc x y k h => + cases h2 with + | same => exact .sc _ _ _ h + | sc _ z _ h' => + exact .sc _ _ _ ⟨h'.1.trans h.1, h'.2.1.trans h.2.1, h'.2.2.1.trans h.2.2.1, fun e => h'.2.2.2 (h.2.2.2 e)⟩ + | ep a b => + cases h2 with + | same => exact .ep _ _ + | ep _ z => exact .ep _ _ + | reply a b => + cases h2 with + | same => exact .reply _ _ + | reply _ z => exact .reply _ _ + +/-- A TCB of the new state is an old TCB (or the object is unchanged). -/ +theorem RefObjA.tcbF {a b : Option KernelObject} (h : RefObjA a b) {x : Tcb} (e : a = some (.tcb x)) : + ∃ y, b = some (.tcb y) ∧ TcbEqLA x y := by + cases h with + | same => exact ⟨x, e, rfl, rfl, rfl, rfl, Or.inl rfl⟩ + | ntfn => cases e + | tcb x' y h => cases e; exact ⟨y, rfl, h⟩ + | sc => cases e + | ep => cases e + | reply => cases e + +theorem RefObjA.tcbB {a b : Option KernelObject} (h : RefObjA a b) {y : Tcb} (e : b = some (.tcb y)) : + ∃ x, a = some (.tcb x) ∧ TcbEqLA x y := by + cases h with + | same => exact ⟨y, e, rfl, rfl, rfl, rfl, Or.inl rfl⟩ + | ntfn => cases e + | tcb x y' h => cases e; exact ⟨x, rfl, h⟩ + | sc => cases e + | ep => cases e + | reply => cases e + +theorem RefObjA.scF {a b : Option KernelObject} (h : RefObjA a b) {x : SchedContext} {k : Nat} + (e : a = some (.schedContext x k)) : ∃ y, b = some (.schedContext y k) ∧ ScEqLA x y := by + cases h with + | same => exact ⟨x, e, rfl, rfl, rfl, id⟩ + | ntfn => cases e + | tcb => cases e + | sc x' y k' h => cases e; exact ⟨y, rfl, h⟩ + | ep => cases e + | reply => cases e + +theorem RefObjA.scB {a b : Option KernelObject} (h : RefObjA a b) {y : SchedContext} {k : Nat} + (e : b = some (.schedContext y k)) : ∃ x, a = some (.schedContext x k) ∧ ScEqLA x y := by + cases h with + | same => exact ⟨y, e, rfl, rfl, rfl, id⟩ + | ntfn => cases e + | tcb => cases e + | sc x y' k' h => cases e; exact ⟨x, rfl, h⟩ + | ep => cases e + | reply => cases e + +/-- (record edit #8b) `idle_sc_ptr`'s head refill is ready. -/ +def IdleRdyA (σ : State Unit) : Prop := + ∀ y n, σ.kheap idleScPtr = some (.schedContext y n) → (refillHd y).rTime ≤ σ.curTime + +/-- An update of an object that leaves an SC with an unready head nowhere new at `idle_sc_ptr`. -/ +theorem IdleRdyA.updA {σ : State Unit} (h : IdleRdyA σ) (p : ObjRef) (o : KernelObject) + (ho : p = idleScPtr → ∀ y n, o = .schedContext y n → (refillHd y).rTime ≤ σ.curTime) : + IdleRdyA { σ with kheap := Function.update σ.kheap p (some o) } := fun y n g => by + change Function.update σ.kheap p (some o) idleScPtr = _ at g + change (refillHd y).rTime ≤ σ.curTime + by_cases e : idleScPtr = p + · rw [e, Function.update_self] at g + exact ho e.symm y n (Option.some.inj g) + · rw [Function.update_of_ne e] at g + exact h y n g + +theorem refillReady_iffA {t : Time} {r : Refill} : refillReady t r = true ↔ r.rTime ≤ t := by + simp [refillReady, refillReady'] + +/-- (record edit #8b-C2, item 11) Both ready-head facts: `idle_sc_ptr`'s and the current SC's (`CurRdyA`). -/ +def RdyA (σ : State Unit) : Prop := IdleRdyA σ ∧ CurRdyA σ + +/-- (item 11) The two ready-head facts at a state: the idle SC's from `SIdleA.f7`, the current SC's from `SInvXA`. -/ +theorem _root_.Fl4ProofsA64.P3.SIdleA.rdyA {σ : State Unit} {m : McState} {n : Nat} (h : SIdleA σ m n) (g : SInvXA σ m n) : RdyA σ := + ⟨fun y k hy => (h.f7 y k hy).2, g.curScReady⟩ + +/-- An update of an object to a non-SC object keeps both. -/ +theorem RdyA.updNs {σ : State Unit} (h : RdyA σ) (p : ObjRef) (o : KernelObject) + (hn : ∀ y n, o ≠ .schedContext y n) : RdyA { σ with kheap := Function.update σ.kheap p (some o) } := by + refine ⟨h.1.updA p o (fun _ y n e => absurd e (hn y n)), fun hc y k hy a => ?_⟩ + change Function.update σ.kheap p (some o) σ.curSc = _ at hy + by_cases e : σ.curSc = p + · rw [e, Function.update_self] at hy + exact absurd (Option.some.inj hy) (hn y k) + · rw [Function.update_of_ne e] at hy + exact h.2 hc y k hy a + +/-- An update of an SC that keeps its activity and keeps a ready head ready keeps both. -/ +theorem RdyA.updSc {σ : State Unit} (h : RdyA σ) {p : ObjRef} {y y' : SchedContext} {n : Nat} + (hk : σ.kheap p = some (.schedContext y n)) (hm : y'.scRefillMax = y.scRefillMax) + (hr : (refillHd y).rTime ≤ σ.curTime → (refillHd y').rTime ≤ σ.curTime) : + RdyA { σ with kheap := Function.update σ.kheap p (some (.schedContext y' n)) } := by + refine ⟨h.1.updA p _ fun ep z k e => by cases e; exact hr (h.1 y n (by rw [← ep]; exact hk)), fun hc y1 k hy a => ?_⟩ + change Function.update σ.kheap p (some (.schedContext y' n)) σ.curSc = _ at hy + by_cases e : σ.curSc = p + · rw [e, Function.update_self] at hy + obtain ⟨rfl, rfl⟩ : y' = y1 ∧ n = k := by + have := Option.some.inj hy + injection this with h1 h2 + exact ⟨h1, h2⟩ + have a0 : scActive y = true := by + have : scActive y' = scActive y := by show activeSc _ = activeSc _; rw [hm] + rw [← this]; exact a + have h0 := h.2 hc y n (by rw [e]; exact hk) a0 + exact refillReady_iffA.2 (hr (refillReady_iffA.1 h0)) + · rw [Function.update_of_ne e] at hy + exact h.2 hc y1 k hy a + +/-- `RdyA` across a step keeping the kernel heap, the time, the consumed time and the current SC. -/ +theorem RdyA.ofFrameA {σ σ' : State Unit} (hk : σ'.kheap = σ.kheap) (ht : σ'.curTime = σ.curTime) + (hc : σ'.consumedTime = σ.consumedTime) (hs : σ'.curSc = σ.curSc) (g : RdyA σ) : RdyA σ' := + ⟨fun y n h => by rw [ht]; exact g.1 y n (by rw [← hk]; exact h), fun hne y k hy a => by + rw [hc] at hne + rw [hs, hk] at hy + rw [ht] + exact g.2 hne y k hy a⟩ + +/-- Only notifications and refill lists change (and the timer flag); (record edit #8b) a ready `idle_sc_ptr` head +stays ready; (item 11) so does the current SC's, with time consumed. -/ +def RefK (σ σ' : State Unit) : Prop := + σ' = { σ with kheap := σ'.kheap, reprogramTimer := σ'.reprogramTimer } ∧ (∀ p, RefObjA (σ.kheap p) (σ'.kheap p)) ∧ + (RdyA σ → RdyA σ') + +theorem RefK.refl (σ : State Unit) : RefK σ σ := ⟨rfl, fun _ => .same _, id⟩ + +theorem RefK.trans {σ1 σ2 σ3 : State Unit} (h1 : RefK σ1 σ2) (h2 : RefK σ2 σ3) : RefK σ1 σ3 := by + refine ⟨?_, fun p => (h1.2.1 p).trans (h2.2.1 p), fun g => h2.2.2 (h1.2.2 g)⟩ + have a := h1.1 + have b := h2.1 + rw [a] at b + exact b + +instance : PreO RefK := ⟨RefK.refl, RefK.trans⟩ + +instance : PreO (@Eq (State Unit)) := ⟨fun _ => rfl, fun h1 h2 => h1.trans h2⟩ + +/-- Only the scheduler action and the ready queues change; every new queue member is old or satisfies `P`. -/ +def QueueE (P : ObjRef → Prop) (σ σ' : State Unit) : Prop := + σ' = { σ with readyQueues := σ'.readyQueues, schedulerAction := σ'.schedulerAction } ∧ + ∀ d p y, y ∈ σ'.readyQueues d p → y ∈ σ.readyQueues d p ∨ P y + +/-! ## Reads -/ + +theorem updateSchedContext_memA (p : ObjRef) (f : SchedContext → SchedContext) {σ : State Unit} + {r : Unit × State Unit} (hr : r ∈ (updateSchedContext (Z := Unit) p f σ).1) : + ∃ y n, σ.kheap p = some (.schedContext y n) ∧ + r.2 = { σ with kheap := Function.update σ.kheap p (some (.schedContext (f y) n)) } := by + obtain ⟨y1, hy1, h⟩ := hr + have hy1' : y1 ∈ (NonDet.assertOpt (σ.kheap p) σ).1 := hy1 + cases hk : σ.kheap p with + | none => rw [hk] at hy1'; exact absurd hy1' (Set.notMem_empty _) + | some ko => + rw [hk] at hy1' + have e1 : y1 = (ko, σ) := hy1' + subst e1 + cases ko with + | schedContext sc n => + obtain ⟨ko', -, hr2⟩ := setObject_memA h + exact ⟨sc, n, rfl, hr2⟩ + | _ => exact absurd h (Set.notMem_empty _) + +macro "eqpres" : tactic => `(tactic| + repeat' (first + | exact PresR.pure _ + | exact PresR.gets _ + | exact PresR.getsThe _ + | exact PresR.assertOpt _ + | exact PresR.assert _ + | exact PresR.fail + | refine PresR.bind_pure _ _ ?_ + | refine PresR.bind ?_ (fun _ => ?_) + | dsimp only + | split)) + +theorem getObject_eqPres (p : ObjRef) : PresR (@Eq (State Unit)) (getObject (Z := Unit) p) := by + unfold getObject; exact PresR.getsThe _ + +theorem getSchedContext_eqPres (p : ObjRef) : PresR (@Eq (State Unit)) (getSchedContext (Z := Unit) p) := by + unfold getSchedContext + refine PresR.bind (getObject_eqPres p) (fun _ => ?_) + eqpres + +theorem threadGet_eqPres {α : Type} (f : Tcb → α) (t : ObjRef) : + PresR (@Eq (State Unit)) (threadGet (Z := Unit) f t) := by + unfold threadGet; eqpres + +theorem getScRefillReady_eqPres (p : ObjRef) : PresR (@Eq (State Unit)) (getScRefillReady (Z := Unit) p) := by + unfold getScRefillReady; exact PresR.getsThe _ + +theorem getScRefillSufficient_eqPres (p : ObjRef) (u : Ticks) : + PresR (@Eq (State Unit)) (getScRefillSufficient (Z := Unit) p u) := by + unfold getScRefillSufficient; exact PresR.getsThe _ + +theorem eqPres_mem {α β : Type} {m : NonDet (State Unit) α} (h : PresR (@Eq (State Unit)) m) + {f : α → NonDet (State Unit) β} {σ : State Unit} {r : β × State Unit} (hr : r ∈ ((m >>= f) σ).1) : + ∃ a, (a, σ) ∈ (m σ).1 ∧ r ∈ (f a σ).1 := by + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + have e : σ = x.2 := h σ x hx + refine ⟨x.1, ?_, ?_⟩ + · rw [show (x.1, σ) = x from Prod.ext rfl e]; exact hx + · rw [← e] at hr; exact hr + +/-! ## The refill check -/ + +theorem RefK.updSc {σ : State Unit} {p : ObjRef} {y y' : SchedContext} {n : Nat} + (hk : σ.kheap p = some (.schedContext y n)) (h : ScEqLA y y') + (hr : (refillHd y).rTime ≤ σ.curTime → (refillHd y').rTime ≤ σ.curTime) : + RefK σ { σ with kheap := Function.update σ.kheap p (some (.schedContext y' n)) } := by + refine ⟨rfl, fun q => ?_, fun g => g.updSc hk h.2.2.1 hr⟩ + show RefObjA (σ.kheap q) (Function.update σ.kheap p (some (.schedContext y' n)) q) + by_cases e : q = p + · subst e; rw [Function.update_self, hk]; exact .sc _ _ _ h + · rw [Function.update_of_ne e]; exact .same _ + +theorem updateRefillHd_refK (p : ObjRef) (f : Refill → Refill) {σ : State Unit} + (hf : ∀ x : Refill, x.rTime ≤ σ.curTime → (f x).rTime ≤ σ.curTime) {r : Unit × State Unit} + (hr : r ∈ (updateRefillHd (Z := Unit) p f σ).1) : RefK σ r.2 := by + unfold updateRefillHd at hr + obtain ⟨y, n, hk, e⟩ := updateSchedContext_memA p _ hr + rw [e] + exact RefK.updSc hk ⟨rfl, rfl, rfl, fun _ => List.cons_ne_nil _ _⟩ (hf _) + +/-- Popping the head and rewriting the new head keeps a non-empty refill list (it ends non-empty). -/ +theorem mergeOverlappingRefills_refK (p : ObjRef) : PresR RefK (mergeOverlappingRefills (Z := Unit) p) := by + intro σ r hr + unfold mergeOverlappingRefills refillPopHead at hr + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + obtain ⟨hd, hhd, hx⟩ := mem_bindA.1 hx + have ehd : σ = hd.2 := by + have : PresR (@Eq (State Unit)) (getRefillHead (Z := Unit) p) := by + unfold getRefillHead; exact PresR.getsThe _ + exact this σ hd hhd + rw [← ehd] at hx + obtain ⟨u, hu, hx⟩ := mem_bindA.1 hx + have ex : x.2 = u.2 := by + have : x = (hd.1, u.2) := hx + rw [this] + obtain ⟨y, n, hk, e1⟩ := updateSchedContext_memA p _ hu + rw [ex] at hr + unfold updateRefillHd at hr + obtain ⟨y', n', hk', e2⟩ := updateSchedContext_memA p _ hr + rw [e1] at hk' e2 + simp only [Function.update_self] at hk' + injection hk' with hk' + injection hk' with hy hn + subst hn + rw [e2] + simp only [Function.update_idem] + rw [← hy] + have ehv : hd.1 = refillHd y := by + by_cases ne : y.scRefills = [] + · exfalso + have rn : readRefillHead (Z := Unit) p σ = none := by + show Option.bind (readSchedContext p σ) (fun sc => Option.bind (Fl4Spec.Lookup.oassert (sc.scRefills ≠ []) σ) + (fun _ => Fl4Spec.Lookup.oreturn (refillHd sc) σ)) = _ + rw [Sched.readSchedContext_eqA hk] + simp [Fl4Spec.Lookup.oassert, ne, Fl4Spec.Lookup.ofail] + have g := hhd + simp [getRefillHead, Fl4Spec.NonDet.getsThe, rn, Fl4Spec.NonDet.assertOpt, Fl4Spec.NonDet.fail] at g + · have g := hhd + rw [Sched.getRefillHead_eqA hk ne] at g + exact congrArg Prod.fst g + have ex1 : x.1 = hd.1 := by + have t : x = (hd.1, u.2) := hx + rw [t] + refine RefK.updSc hk ⟨rfl, rfl, rfl, fun _ => List.cons_ne_nil _ _⟩ (fun g => ?_) + show x.1.rTime ≤ σ.curTime + rw [ex1, ehv]; exact g + +theorem refillHeadOverlappingLoop_refK (p : ObjRef) : + PresR RefK (refillHeadOverlappingLoop (Z := Unit) p) := by + unfold refillHeadOverlappingLoop + exact PresR.whileLoop (fun _ => mergeOverlappingRefills_refK p) _ + +theorem eqPres_refK {α : Type} {m : NonDet (State Unit) α} (h : PresR (@Eq (State Unit)) m) : PresR RefK m := + fun s x hx => by rw [← h s x hx]; exact RefK.refl s + +theorem refillUnblockCheck_refK (p : ObjRef) : PresR RefK (refillUnblockCheck (Z := Unit) p) := by + unfold refillUnblockCheck + refine PresR.bind (eqPres_refK ?_) (fun _ => PresR.bind (eqPres_refK (getScRefillReady_eqPres p)) (fun _ => ?_)) + · unfold isRoundRobin + exact PresR.bind (getSchedContext_eqPres p) (fun _ => PresR.pure _) + refine PresR.when ?_ + intro σ r hr + obtain ⟨ct, hct, hr⟩ := eqPres_mem (PresR.gets _) hr + have ect : ct = σ.curTime := congrArg Prod.fst (show (ct, σ) = (σ.curTime, σ) from hct) + obtain ⟨u, hu, hr⟩ := mem_bindA.1 hr + have R1 : RefK σ u.2 := updateRefillHd_refK _ _ (fun _ _ => by rw [ect]; exact BitVec.le_refl _) hu + have rest : PresR RefK (NonDet.modify (fun s : State Unit => { s with reprogramTimer := true }) >>= fun _ => + refillHeadOverlappingLoop (Z := Unit) p) := + PresR.bind (PresR.modify _ (fun σ => ⟨rfl, fun _ => .same _, id⟩)) (fun _ => refillHeadOverlappingLoop_refK p) + exact R1.trans (rest u.2 r hr) + +theorem ifCondRefillUnblockCheck_refK (o : Option ObjRef) (a b : Option Bool) : + PresR RefK (ifCondRefillUnblockCheck (Z := Unit) o a b) := by + unfold ifCondRefillUnblockCheck + cases o with + | none => exact PresR.pure _ + | some p => + show PresR RefK (_ >>= _) + refine PresR.bind (eqPres_refK (getSchedContext_eqPres p)) (fun _ => ?_) + refine PresR.bind (PresR.gets _) (fun _ => PresR.bind (PresR.pure _) (fun _ => ?_)) + exact PresR.when (PresR.when (refillUnblockCheck_refK p)) + +/-! ## The scheduler steps -/ + +theorem tcbSchedEnqueue_memA {a b : ObjRef} {q : List ObjRef} (h : b ∈ tcbSchedEnqueue a q) : b = a ∨ b ∈ q := by + unfold tcbSchedEnqueue at h + split_ifs at h + · exact Or.inr h + · exact (List.mem_cons.1 h) + +theorem tcbSchedAction_enq_memA (t : ObjRef) {σ : State Unit} {r : Unit × State Unit} + (hr : r ∈ (tcbSchedAction (Z := Unit) tcbSchedEnqueue t σ).1) : + QueueE (fun y => y = t) σ r.2 ∧ r.2.schedulerAction = σ.schedulerAction := by + unfold tcbSchedAction at hr + obtain ⟨d, -, hr⟩ := eqPres_mem (threadGet_eqPres _ t) hr + obtain ⟨pr, -, hr⟩ := eqPres_mem (threadGet_eqPres _ t) hr + have hq : PresR (@Eq (State Unit)) (getTcbQueue (Z := Unit) d pr) := by unfold getTcbQueue; eqpres + obtain ⟨q, hq', hr⟩ := eqPres_mem hq hr + have eq : q = σ.readyQueues d pr := by + unfold getTcbQueue at hq' + obtain ⟨z, hz, hq'⟩ := mem_bindA.1 hq' + have ez : z = (σ.readyQueues, σ) := hz + subst ez + have := congrArg Prod.fst (show (q, σ) = (σ.readyQueues d pr, σ) from hq') + exact this + obtain ⟨q', hq'', hr⟩ := mem_bindA.1 hr + have eq' : q' = (tcbSchedEnqueue t q, σ) := hq'' + subst eq' + have er : r = ((), { σ with readyQueues := fun d' p => + if d' = d ∧ p = pr then tcbSchedEnqueue t q else σ.readyQueues d' p }) := hr + rw [er] + refine ⟨⟨rfl, fun d' p y hy => ?_⟩, rfl⟩ + simp only at hy + split_ifs at hy with h + · rcases tcbSchedEnqueue_memA hy with e | e + · exact Or.inr e + · rw [eq] at e; rw [h.1, h.2]; exact Or.inl e + · exact Or.inl hy + +theorem QueueE.refl (P : ObjRef → Prop) (σ : State Unit) : QueueE P σ σ := ⟨rfl, fun _ _ _ h => Or.inl h⟩ + +theorem QueueE.trans {P : ObjRef → Prop} {σ1 σ2 σ3 : State Unit} (h1 : QueueE P σ1 σ2) (h2 : QueueE P σ2 σ3) : + QueueE P σ1 σ3 := by + refine ⟨?_, fun d p y hy => ?_⟩ + · have a := h1.1 + have b := h2.1 + rw [a] at b + exact b + · rcases h2.2 d p y hy with h | h + · exact h1.2 d p y h + · exact Or.inr h + +theorem QueueE.mono {P Q : ObjRef → Prop} {σ σ' : State Unit} (h : QueueE P σ σ') (hpq : ∀ y, P y → Q y) : + QueueE Q σ σ' := + ⟨h.1, fun d p y hy => (h.2 d p y hy).imp id (hpq y)⟩ + +theorem setSchedulerAction_memA (a : SchedulerAction) {σ : State Unit} {r : Unit × State Unit} + (hr : r ∈ (setSchedulerAction (Z := Unit) a σ).1) : + QueueE (fun _ => False) σ r.2 ∧ r.2.schedulerAction = a := by + have er : r = ((), { σ with schedulerAction := a }) := hr + rw [er] + exact ⟨⟨rfl, fun _ _ _ h => Or.inl h⟩, rfl⟩ + +/-- The thread a pending `switchThread` would enqueue. -/ +def PendA (σ : State Unit) (y : ObjRef) : Prop := σ.schedulerAction = .switchThread y ∧ schedulable y σ = true + +/-- **`reschedule_required`** enqueues at most the pending schedulable thread and sets `chooseNewThread`. -/ +theorem rescheduleRequired_memA {σ : State Unit} {r : Unit × State Unit} + (hr : r ∈ (rescheduleRequired (Z := Unit) σ).1) : + QueueE (PendA σ) σ r.2 ∧ r.2.schedulerAction = .chooseNewThread := by + unfold rescheduleRequired at hr + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + have ex : x = (σ.schedulerAction, σ) := hx + subst ex + have noswitch : r ∈ (setSchedulerAction (Z := Unit) .chooseNewThread σ).1 → + QueueE (PendA σ) σ r.2 ∧ r.2.schedulerAction = .chooseNewThread := fun h => by + have h2 := setSchedulerAction_memA _ h + exact ⟨h2.1.mono fun _ h => h.elim, h2.2⟩ + cases e : σ.schedulerAction with + | resumeCurThread => + simp only [e] at hr + exact noswitch hr + | chooseNewThread => + simp only [e] at hr + exact noswitch hr + | switchThread t => + simp only [e] at hr + obtain ⟨z, hz, hr⟩ := mem_bindA.1 hr + have ez : z = (schedulable t σ, σ) := hz + subst ez + obtain ⟨u, hu, hr⟩ := mem_bindA.1 hr + have h2 := setSchedulerAction_memA _ hr + have h1 : QueueE (PendA σ) σ u.2 := by + unfold NonDet.when at hu + by_cases hs : schedulable t σ = true + · rw [if_pos hs] at hu + obtain ⟨o, -, hu⟩ := eqPres_mem (threadGet_eqPres _ t) hu + obtain ⟨scp, -, hu⟩ := eqPres_mem (PresR.assertOpt o) hu + obtain ⟨b1, -, hu⟩ := eqPres_mem (getScRefillSufficient_eqPres scp 0) hu + obtain ⟨b2, -, hu⟩ := eqPres_mem (getScRefillReady_eqPres scp) hu + obtain ⟨_, -, hu⟩ := eqPres_mem (PresR.assert _) hu + exact (tcbSchedAction_enq_memA t hu).1.mono fun y ey => by subst ey; exact ⟨e, hs⟩ + · rw [if_neg hs] at hu + have : u = ((), σ) := hu + rw [this]; exact QueueE.refl _ _ + exact ⟨h1.trans (h2.1.mono fun _ h => h.elim), h2.2⟩ + +/-- **`possible_switch_to`**: enqueues at most its target and the pending thread; the new action is the old one, +`chooseNewThread`, or a switch to the target. -/ +theorem possibleSwitchTo_memA (t : ObjRef) {σ : State Unit} {r : Unit × State Unit} + (hr : r ∈ (possibleSwitchTo (Z := Unit) t σ).1) : + QueueE (fun y => y = t ∨ PendA σ y) σ r.2 ∧ + (r.2.schedulerAction = σ.schedulerAction ∨ r.2.schedulerAction = .chooseNewThread ∨ + r.2.schedulerAction = .switchThread t) := by + unfold possibleSwitchTo getTcbObjRef at hr + obtain ⟨scOpt, -, hr⟩ := eqPres_mem (threadGet_eqPres _ t) hr + obtain ⟨inq, -, hr⟩ := eqPres_mem (PresR.gets _) hr + unfold NonDet.when at hr + split_ifs at hr with hg + swap + · have : r = ((), σ) := hr + rw [this]; exact ⟨QueueE.refl _ _, Or.inl rfl⟩ + obtain ⟨cd, -, hr⟩ := eqPres_mem (PresR.gets _) hr + obtain ⟨td, -, hr⟩ := eqPres_mem (threadGet_eqPres _ t) hr + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + have ex : x = (σ.schedulerAction, σ) := hx + subst ex + simp only at hr + by_cases h1 : td ≠ cd + · rw [if_pos h1] at hr + obtain ⟨q, e⟩ := tcbSchedAction_enq_memA t hr + exact ⟨q.mono fun _ h => Or.inl h, Or.inl e⟩ + rw [if_neg h1] at hr + by_cases h2 : σ.schedulerAction ≠ .resumeCurThread + · rw [if_pos h2] at hr + obtain ⟨u, hu, hr⟩ := mem_bindA.1 hr + obtain ⟨q1, e1⟩ := rescheduleRequired_memA hu + obtain ⟨q2, e2⟩ := tcbSchedAction_enq_memA t hr + exact ⟨(q1.mono fun _ h => Or.inr h).trans (q2.mono fun _ h => Or.inl h), Or.inr (Or.inl (e2.trans e1))⟩ + · rw [if_neg h2] at hr + obtain ⟨q, e⟩ := setSchedulerAction_memA _ hr + exact ⟨q.mono fun _ h => h.elim, Or.inr (Or.inr e)⟩ + +/-! ## The invariants -/ + +/-- The three spec invariants the notification finaliser's loop carries. -/ +structure LoopInvA (σ : State Unit) (m : McState) (n : Nat) : Prop where + sinv : SInvXA σ m n + sidle : SIdleA σ m n + sact : SActA σ + +theorem LoopInvA.refK {σ σ' : State Unit} {m : McState} {n : Nat} (h : LoopInvA σ m n) (R : RefK σ σ') : + LoopInvA σ' m n := by + have e := R.1 + have hcur : σ'.curThread = σ.curThread := by rw [e] + have hidle : σ'.idleThread = σ.idleThread := by rw [e] + have hcsc : σ'.curSc = σ.curSc := by rw [e] + have hrq : σ'.readyQueues = σ.readyQueues := by rw [e] + have hrls : σ'.releaseQueue = σ.releaseQueue := by rw [e] + have hact : σ'.schedulerAction = σ.schedulerAction := by rw [e] + have hnode : σ'.interruptIrqNode = σ.interruptIrqNode := by rw [e] + have htime : σ'.curTime = σ.curTime := by rw [e] + have tcbF : ∀ p x, σ.kheap p = some (.tcb x) → ∃ y, σ'.kheap p = some (.tcb y) ∧ TcbEqLA x y := + fun p x hp => (R.2.1 p).tcbF hp + have scF : ∀ p y k, σ.kheap p = some (.schedContext y k) → + ∃ y', σ'.kheap p = some (.schedContext y' k) ∧ ScEqLA y y' := fun p y k hp => (R.2.1 p).scF hp + have scB : ∀ p y' k, σ'.kheap p = some (.schedContext y' k) → ∃ y, σ.kheap p = some (.schedContext y k) ∧ + ScEqLA y y' := fun p y' k hp => (R.2.1 p).scB hp + have hct : ∀ k p, capTableAt k p σ' ↔ capTableAt k p σ := fun k p => by + have hR := R.2.1 p + unfold capTableAt + generalize σ.kheap p = a at hR ⊢ + generalize σ'.kheap p = b at hR ⊢ + cases hR <;> simp + have capB : ∀ p o' b c, σ'.kheap p = some o' → capOf o' b = some c → + ∃ o, σ.kheap p = some o ∧ capOf o b = some c := fun p o' b c hp hc => by + have hR := R.2.1 p + rw [hp] at hR + generalize e : σ.kheap p = a at hR + cases hR with + | same => exact ⟨o', rfl, hc⟩ + | ntfn => cases hc + | tcb x y h => exact ⟨_, rfl, by simpa [capOf, h.2.2.1] using hc⟩ + | sc => cases hc + | ep => cases hc + | reply => cases hc + have specCap : ∀ sl, specCapAtA σ' sl = specCapAtA σ sl := fun sl => by + have hR := R.2.1 sl.1 + unfold specCapAtA + generalize σ.kheap sl.1 = a at hR ⊢ + generalize σ'.kheap sl.1 = b at hR ⊢ + cases hR with + | same => rfl + | ntfn => rfl + | tcb x y h => simp only [h.2.2.1] + | sc => rfl + | ep => rfl + | reply => rfl + have RD : RdyA σ' := R.2.2 ⟨fun z j g => (h.sidle.f7 z j g).2, h.sinv.curScReady⟩ + have hcons : σ'.consumedTime = σ.consumedTime := by rw [e] + have S := h.sinv + refine + { sinv := + { mc := S.mc + view := by unfold McView; rw [hidle]; exact S.view + curTcb := by + obtain ⟨x, hx⟩ := S.curTcb + obtain ⟨y, hy, -⟩ := tcbF _ _ hx + exact ⟨y, by rw [hcur]; exact hy⟩ + idleTcb := by + obtain ⟨x, hx, hs⟩ := S.idleTcb + obtain ⟨y, hy, e⟩ := tcbF _ _ hx + exact ⟨y, by rw [hidle]; exact hy, e.1.trans hs⟩ + onlyIdle := fun t y hy hs => by + obtain ⟨x, hx, e⟩ := (R.2.1 t).tcbB hy + exact S.onlyIdle t x hx (e.1.symm.trans hs) + idleSc := by + obtain ⟨x, y, k, h1, h2, h3, h4⟩ := S.idleSc + obtain ⟨x', h1', ex⟩ := tcbF _ _ h1 + obtain ⟨y', h3', ey⟩ := scF _ _ _ h3 + exact ⟨x', y', k, by rw [hidle]; exact h1', ex.2.1.trans h2, h3', by rw [hidle]; exact ey.1.trans h4⟩ + curSc := by + obtain ⟨y, k, hy⟩ := S.curSc + obtain ⟨y', h', -⟩ := scF _ _ _ hy + exact ⟨y', k, by rw [hcsc]; exact h'⟩ + ctQ := fun ha d p => by rw [hcur, hrq]; exact S.ctQ (hact.symm.trans ha) d p + idleQ := fun d p => by rw [hrq]; exact S.idleQ d p + rq := fun d p t ht => by + rw [hrq] at ht + obtain ⟨x, hx, hr⟩ := S.rq d p t ht + obtain ⟨y, hy, e⟩ := tcbF _ _ hx + exact ⟨y, hy, by rw [e.1]; exact hr⟩ + rls := fun t ht => by + rw [hrls] at ht + obtain ⟨x, hx⟩ := S.rls t ht + obtain ⟨y, hy, -⟩ := tcbF _ _ hx + exact ⟨y, hy⟩ + rpos := fun p y k hy a => by + obtain ⟨y0, hy0, e⟩ := scB p y k hy + exact e.2.2.2 (S.rpos p y0 k hy0 (by unfold scActive at a ⊢; rw [← e.2.2.1]; exact a)) + zc := ZeroConsA.back S.zc (fun t ht => by rw [hrls] at ht; exact ht) hcons hcsc + (fun t x' h0 => by obtain ⟨x, hx, ee⟩ := (R.2.1 t).tcbB h0; exact ⟨x, hx, ee.2.1.symm⟩) + rlsRun := RlsRunA.back S.rlsRun (fun t ht => by rw [hrls] at ht; exact ht) + (fun t x hx _ => by obtain ⟨y, hy, ee⟩ := tcbF t x hx; exact ⟨y, hy, fun r => by rw [ee.1]; exact r⟩) + curScReady := RD.2 + faultSt := fun t x' h0 f => by + obtain ⟨x, hx, ee⟩ := (R.2.1 t).tcbB h0 + rcases ee.2.2.2.2 with ef | ef + · rw [ee.1]; exact S.faultSt t x hx (by rw [← ef]; exact f) + · exact absurd ef f + irqN := by + obtain ⟨⟨⟨hinj, hcn⟩, hdom⟩, hsl, href⟩ := S.irqN + refine ⟨⟨⟨?_, fun irq hi => ?_⟩, fun irq hi k hk => ?_⟩, fun irq hi o b c ho hc => ?_, + fun p o b c ho hc => ?_⟩ + · rw [hnode]; exact hinj + · rw [hnode]; exact (hct _ _).2 (hcn irq hi) + · rw [hnode] at hk; exact hdom irq hi k ((hct _ _).1 hk) + · rw [hnode] at ho + obtain ⟨o0, h0, hc0⟩ := capB _ _ _ _ ho hc + exact hsl irq hi o0 b c h0 hc0 + · rw [hnode] + obtain ⟨o0, h0, hc0⟩ := capB _ _ _ _ ho hc + exact href p o0 b c h0 hc0 } + sidle := + { f3 := fun c hc => by + obtain ⟨x, hx, hs⟩ := h.sidle.f3 c hc + obtain ⟨y, hy, e⟩ := tcbF _ _ hx + exact ⟨y, hy, e.1.trans hs⟩ + f4 := fun y k hy => by + obtain ⟨y0, hy0, e⟩ := scB _ y k hy + rw [e.2.1]; exact h.sidle.f4 y0 k hy0 + f5 := fun p y k hy he => by + obtain ⟨y0, hy0, e⟩ := scB p y k hy + rw [e.1, hidle] at he + exact h.sidle.f5 p y0 k hy0 he + f6 := h.sidle.f6.of_eq specCap + f7 := fun y k hy => by + obtain ⟨y0, hy0, e⟩ := scB _ y k hy + have a := h.sidle.f7 y0 k hy0 + refine ⟨?_, (RD.1) y k hy⟩ + have a1 := a.1 + unfold scActive at a1 ⊢ + rw [e.2.2.1]; exact a1 + idleSlot := fun c hc => by + obtain ⟨x, hx, -⟩ := h.sidle.f3 c hc + obtain ⟨y, hy, e⟩ := tcbF _ _ hx + exact (idleVtKeepA hx hy (Option.some.inj (show some y.tcbVtable = some x.tcbVtable from + congrFun e.2.2.1 (tcbCnodeIndex 1))) e.2.2.2.1 (h.sidle.idleSlot c hc) (h.sidle.idleBound c hc)).1 + idleBound := fun c hc => by + obtain ⟨x, hx, -⟩ := h.sidle.f3 c hc + obtain ⟨y, hy, e⟩ := tcbF _ _ hx + exact (idleVtKeepA hx hy (Option.some.inj (show some y.tcbVtable = some x.tcbVtable from + congrFun e.2.2.1 (tcbCnodeIndex 1))) e.2.2.2.1 (h.sidle.idleSlot c hc) (h.sidle.idleBound c hc)).2 } + sact := + { f1 := fun u eu => by + rw [hact] at eu; rw [hcur, hidle]; exact h.sact.f1 u eu + f2 := by + obtain ⟨x, hx, hs⟩ := h.sact.f2 + obtain ⟨y, hy, e⟩ := tcbF _ _ hx + exact ⟨y, by rw [hcur]; exact hy, by rw [e.1]; exact hs⟩ } } + +/-- A thread that may be enqueued: not current, no core's idle thread, a runnable TCB. -/ +def EnqOkA (σ : State Unit) (m : McState) (n : Nat) (y : ObjRef) : Prop := + y ≠ σ.curThread ∧ (∀ c < n, m.idleThreadOf c ≠ y) ∧ ∃ x, σ.kheap y = some (.tcb x) ∧ runnable x.tcbState = true + +/-- A pending thread may be enqueued (F1 and F3). -/ +theorem LoopInvA.pendOk {σ : State Unit} {m : McState} {n : Nat} (h : LoopInvA σ m n) {y : ObjRef} + (p : PendA σ y) : EnqOkA σ m n y := by + obtain ⟨ha, hs⟩ := p + have hp := (Sched.schedulable_partsA hs).1 + have hx : ∃ x, σ.kheap y = some (.tcb x) := by + unfold schedulable at hs + simp only [decide_eq_true_eq] at hs + have h1 := hs.1 + cases hk : σ.kheap y with + | none => simp [optPred, optMapSome, optMap, tcbsOfKh, hk] at h1 + | some o => + cases o with + | tcb x => exact ⟨x, rfl⟩ + | _ => simp [optPred, optMapSome, optMap, tcbsOfKh, tcbOf, hk] at h1 + obtain ⟨x, hx⟩ := hx + refine ⟨(h.sact.f1 y ha).1, fun c hc e => ?_, x, hx, hp x hx⟩ + obtain ⟨z, hz, hz'⟩ := h.sidle.f3 c hc + rw [e, hx] at hz + have := KernelObject.tcb.inj (Option.some.inj hz) + subst this + have := hp x hx + rw [hz'] at this + cases this + +/-- **The three invariants across a queue / action step** (`ctQ`, `idleQ`, `rq`, F1). -/ +theorem LoopInvA.queueE {P : ObjRef → Prop} {σ σ' : State Unit} {m : McState} {n : Nat} (h : LoopInvA σ m n) + (Q : QueueE P σ σ') (hP : ∀ y, P y → EnqOkA σ m n y) + (hA : ∀ u, σ'.schedulerAction = .switchThread u → + σ.schedulerAction = .switchThread u ∨ (u ≠ σ.curThread ∧ u ≠ σ.idleThread)) + (hR : σ'.schedulerAction = .resumeCurThread → σ.schedulerAction = .resumeCurThread) : LoopInvA σ' m n := by + have e := Q.1 + have hk : σ'.kheap = σ.kheap := by rw [e] + have hcur : σ'.curThread = σ.curThread := by rw [e] + have hidle : σ'.idleThread = σ.idleThread := by rw [e] + have hcsc : σ'.curSc = σ.curSc := by rw [e] + have hrls : σ'.releaseQueue = σ.releaseQueue := by rw [e] + have hnode : σ'.interruptIrqNode = σ.interruptIrqNode := by rw [e] + have hct : ∀ k p, capTableAt k p σ' ↔ capTableAt k p σ := fun k p => by + unfold capTableAt; rw [hk] + have S := h.sinv + refine + { sinv := + { mc := S.mc + view := by unfold McView; rw [hidle]; exact S.view + curTcb := by rw [hk, hcur]; exact S.curTcb + idleTcb := by rw [hk, hidle]; exact S.idleTcb + onlyIdle := by rw [hk]; exact S.onlyIdle + idleSc := by rw [hk, hidle]; exact S.idleSc + curSc := by rw [hk, hcsc]; exact S.curSc + ctQ := fun hres d p hm => by + rw [hcur] at hm + rcases Q.2 d p _ hm with a | a + · exact S.ctQ (hR hres) d p a + · exact (hP _ a).1 rfl + idleQ := fun d p c hc hm => by + rcases Q.2 d p _ hm with a | a + · exact S.idleQ d p c hc a + · exact (hP _ a).2.1 c hc rfl + rq := fun d p t hm => by + rw [hk] + rcases Q.2 d p t hm with a | a + · exact S.rq d p t a + · exact (hP _ a).2.2 + rls := fun t ht => by rw [hrls] at ht; rw [hk]; exact S.rls t ht + rpos := by rw [hk]; exact S.rpos + zc := ZeroConsA.back S.zc (fun t ht => by rw [hrls] at ht; exact ht) (by rw [e]) hcsc + (fun t x' h0 => ⟨x', by rw [hk] at h0; exact h0, rfl⟩) + rlsRun := RlsRunA.back S.rlsRun (fun t ht => by rw [hrls] at ht; exact ht) + (fun t x hx _ => ⟨x, by rw [hk]; exact hx, id⟩) + curScReady := fun hc y k hy a => by + rw [hk, hcsc] at hy + have htm : σ'.curTime = σ.curTime := by rw [e] + have hcc : σ'.consumedTime = σ.consumedTime := by rw [e] + rw [htm] + exact S.curScReady (by rw [← hcc]; exact hc) y k hy a + faultSt := fun t x h0 f => S.faultSt t x (by rw [hk] at h0; exact h0) f + irqN := by + obtain ⟨⟨⟨hinj, hcn⟩, hdom⟩, hsl, href⟩ := S.irqN + refine ⟨⟨⟨?_, fun irq hi => ?_⟩, fun irq hi k hk' => ?_⟩, fun irq hi o b c ho hc => ?_, + fun p o b c ho hc => ?_⟩ + · rw [hnode]; exact hinj + · rw [hnode]; exact (hct _ _).2 (hcn irq hi) + · rw [hnode] at hk'; exact hdom irq hi k ((hct _ _).1 hk') + · rw [hk, hnode] at ho; exact hsl irq hi o b c ho hc + · rw [hnode]; rw [hk] at ho; exact href p o b c ho hc } + sidle := h.sidle.congr hk hidle (by rw [e]; exact BitVec.le_refl _) + sact := + { f1 := fun u eu => by + rw [hcur, hidle] + rcases hA u eu with a | a + · exact h.sact.f1 u a + · exact a + f2 := by rw [hk, hcur]; exact h.sact.f2 } } + +theorem LoopInvA.reschedule {σ : State Unit} {m : McState} {n : Nat} (h : LoopInvA σ m n) + {r : Unit × State Unit} (hr : r ∈ (rescheduleRequired (Z := Unit) σ).1) : LoopInvA r.2 m n := by + obtain ⟨q, e⟩ := rescheduleRequired_memA hr + refine h.queueE q (fun y p => h.pendOk p) (fun u eu => ?_) (fun ha => ?_) + · rw [e] at eu; cases eu + · rw [e] at ha; cases ha + +theorem LoopInvA.possibleSwitch {σ : State Unit} {m : McState} {n : Nat} (h : LoopInvA σ m n) {t : ObjRef} + (ht : EnqOkA σ m n t) (hti : t ≠ σ.idleThread) + {r : Unit × State Unit} (hr : r ∈ (possibleSwitchTo (Z := Unit) t σ).1) : + LoopInvA r.2 m n ∧ r.2.kheap = σ.kheap := by + obtain ⟨q, e⟩ := possibleSwitchTo_memA t hr + refine ⟨h.queueE q (fun y p => ?_) (fun u eu => ?_) (fun ha => ?_), by rw [q.1]⟩ + · rcases p with p | p + · rw [p]; exact ht + · exact h.pendOk p + · rcases e with e | e | e + · exact Or.inl (e.symm.trans eu) + · rw [e] at eu; cases eu + · rw [e] at eu; cases eu; exact Or.inr ⟨ht.1, hti⟩ + · rcases e with e | e | e + · exact e.symm.trans ha + · rw [e] at ha; cases ha + · rw [e] at ha; cases ha + +/-! ## One dequeue-and-restart, and the loop -/ + +/-- A thread blocked on a notification. -/ +def NtfnBlockedA (σ : State Unit) (t : ObjRef) : Prop := + ∃ x o, σ.kheap t = some (.tcb x) ∧ x.tcbState = .blockedOnNotification o + +theorem tcbNtfnDequeue_memA {σ : State Unit} {t p : ObjRef} {y : Notification} + (hk : σ.kheap p = some (.notification y)) {r : Unit × State Unit} + (hr : r ∈ (tcbNtfnDequeue (Z := Unit) t p σ).1) : + ∃ y', r.2 = { σ with kheap := Function.update σ.kheap p (some (.notification y')) } := by + unfold tcbNtfnDequeue at hr + rw [Notif.nd_bind_one (Notif.getNotification_eq hk)] at hr + obtain ⟨q, hq, hr⟩ := mem_bindA.1 hr + have eq : q = (ntfnQueueOf y, σ) := hq + subst eq + obtain ⟨_, -, hr⟩ := eqPres_mem (PresR.assert _) hr + obtain ⟨q', hq', hr⟩ := mem_bindA.1 hr + have eq' : q' = (removeAll t (ntfnQueueOf y), σ) := hq' + subst eq' + obtain ⟨z, hz, hr⟩ := mem_bindA.1 hr + have ez : z.2 = σ := by + have : z = (_, σ) := hz + rw [this] + rw [ez] at hr + unfold setNotification setSimpleKo at hr + obtain ⟨_, -, hr⟩ := eqPres_mem (getObject_eqPres p) hr + obtain ⟨_, -, hr⟩ := eqPres_mem (PresR.assert _) hr + obtain ⟨_, -, hr⟩ := eqPres_mem (PresR.assert _) hr + obtain ⟨ko, -, e⟩ := setObject_memA hr + exact ⟨_, e⟩ + +/-- **`remove_and_restart_ntfn_queued_thread` keeps the invariants**, the notification object, and every other TCB. -/ +theorem LoopInvA.removeAndRestart {σ : State Unit} {m : McState} {n : Nat} (h : LoopInvA σ m n) {t p : ObjRef} + {y : Notification} (hk : σ.kheap p = some (.notification y)) (hb : NtfnBlockedA σ t) + {r : Unit × State Unit} (hr : r ∈ (removeAndRestartNtfnQueuedThread (Z := Unit) t p σ).1) : + LoopInvA r.2 m n ∧ (∃ y', r.2.kheap p = some (.notification y')) ∧ + ∀ u, u ≠ t → NtfnBlockedA σ u → NtfnBlockedA r.2 u := by + obtain ⟨x, o, hx, hs⟩ := hb + unfold removeAndRestartNtfnQueuedThread at hr + obtain ⟨d, hd, hr⟩ := mem_bindA.1 hr + obtain ⟨y', e1⟩ := tcbNtfnDequeue_memA hk hd + have R1 : RefK σ d.2 := by + rw [e1] + refine ⟨rfl, fun q => ?_, fun g => g.updNs p _ fun _ _ e => by cases e⟩ + show RefObjA (σ.kheap q) (Function.update σ.kheap p (some (.notification y')) q) + by_cases e : q = p + · subst e; rw [Function.update_self, hk]; exact .ntfn _ _ + · rw [Function.update_of_ne e]; exact .same _ + have h1 := h.refK R1 + have tp : t ≠ p := fun e => by rw [e, hk] at hx; cases hx + have hx1 : d.2.kheap t = some (.tcb x) := by rw [e1]; exact (Function.update_of_ne tp _ _).trans hx + have hk1 : d.2.kheap p = some (.notification y') := by rw [e1]; exact Function.update_self _ _ _ + have u1 : ∀ u, u ≠ t → NtfnBlockedA σ u → NtfnBlockedA d.2 u := fun u _ ⟨xu, ou, hu, su⟩ => by + have up : u ≠ p := fun e => by rw [e, hk] at hu; cases hu + exact ⟨xu, ou, by rw [e1]; exact (Function.update_of_ne up _ _).trans hu, su⟩ + -- the waiter is neither current nor idle + have tcur : t ≠ d.2.curThread := fun e => by + obtain ⟨z, hz, hz'⟩ := h1.sact.f2 + rw [← e, hx1] at hz + have := KernelObject.tcb.inj (Option.some.inj hz) + subst this + rw [hs] at hz' + simp at hz' + have tidle : t ≠ d.2.idleThread := fun e => by + obtain ⟨z, hz, hz'⟩ := h1.sinv.idleTcb + rw [← e, hx1] at hz + have := KernelObject.tcb.inj (Option.some.inj hz) + subst this + rw [hs] at hz' + cases hz' + have tidles : ∀ c < n, m.idleThreadOf c ≠ t := fun c hc e => by + obtain ⟨z, hz, hz'⟩ := h1.sidle.f3 c hc + rw [e, hx1] at hz + have := KernelObject.tcb.inj (Option.some.inj hz) + subst this + rw [hs] at hz' + cases hz' + obtain ⟨s, hsm, hr⟩ := mem_bindA.1 hr + rw [Notif.setThreadState_eq .restart hx1] at hsm + have es : s = ((), Notif.stsAct (Notif.updK d.2 t (.tcb { x with tcbState := .restart })) t) := hsm + have eact : Notif.stsAct (Notif.updK d.2 t (.tcb { x with tcbState := .restart })) t = + stUpdA d.2 t x .restart := by + unfold Notif.stsAct + rw [if_neg (fun h => tcur h.1)] + rfl + rw [es, eact] at hr + have h2 : LoopInvA (stUpdA d.2 t x .restart) m n := + { sinv := h1.sinv.restartA hx1 tidle rfl (fun e => by cases e) + (FaultStOkA.none h1.sinv.faultSt hx1 (by rw [hs]; rintro (⟨_, _, h⟩ | ⟨_, h⟩ | h) <;> cases h)) + sidle := h1.sidle.stUpdA hx1 tidles + sact := + { f1 := fun u e => h1.sact.f1 u e + f2 := by + obtain ⟨z, hz, hz'⟩ := h1.sact.f2 + exact ⟨z, (Function.update_of_ne tcur.symm _ _).trans hz, hz'⟩ } } + have hx2 : (stUpdA d.2 t x .restart).kheap t = some (.tcb { x with tcbState := .restart }) := + Function.update_self _ _ _ + have pt : p ≠ t := tp.symm + have hk2 : (stUpdA d.2 t x .restart).kheap p = some (.notification y') := + (Function.update_of_ne pt _ _).trans hk1 + have u2 : ∀ u, u ≠ t → NtfnBlockedA σ u → NtfnBlockedA (stUpdA d.2 t x .restart) u := fun u ut hu => by + obtain ⟨xu, ou, hu', su⟩ := u1 u ut hu + exact ⟨xu, ou, (Function.update_of_ne ut _ _).trans hu', su⟩ + obtain ⟨scOpt, -, hr⟩ := eqPres_mem (threadGet_eqPres _ t) hr + obtain ⟨c, hc, hr⟩ := mem_bindA.1 hr + have R3 : RefK (stUpdA d.2 t x .restart) c.2 := ifCondRefillUnblockCheck_refK _ _ _ _ c hc + have h3 := h2.refK R3 + have tcbKeep : ∀ u z, (stUpdA d.2 t x .restart).kheap u = some (.tcb z) → + ∃ z', c.2.kheap u = some (.tcb z') ∧ TcbEqLA z z' := fun u z hu => (R3.2.1 u).tcbF hu + obtain ⟨x3, hx3, ex3⟩ := tcbKeep _ _ hx2 + have cur3 : c.2.curThread = d.2.curThread := by rw [R3.1]; rfl + have idle3 : c.2.idleThread = d.2.idleThread := by rw [R3.1]; rfl + have ok3 : EnqOkA c.2 m n t := ⟨by rw [cur3]; exact tcur, tidles, x3, hx3, by rw [ex3.1]; rfl⟩ + obtain ⟨h4, hk4⟩ := h3.possibleSwitch ok3 (by rw [idle3]; exact tidle) hr + have hk3 : ∃ y'', c.2.kheap p = some (.notification y'') := by + have hR := R3.2.1 p + rw [hk2] at hR + generalize c.2.kheap p = b at hR ⊢ + cases hR with + | same => exact ⟨y', rfl⟩ + | ntfn _ b => exact ⟨b, rfl⟩ + refine ⟨h4, by rw [hk4]; exact hk3, fun u ut hu => ?_⟩ + obtain ⟨xu, ou, hu', su⟩ := u2 u ut hu + obtain ⟨xu', hu3, eu⟩ := tcbKeep _ _ hu' + exact ⟨xu', ou, by rw [hk4]; exact hu3, eu.1.trans su⟩ + +/-- The loop over the queue. -/ +theorem LoopInvA.ntfnLoop {m : McState} {n : Nat} {p : ObjRef} (q : List ObjRef) : + ∀ {σ : State Unit} {y : Notification}, LoopInvA σ m n → σ.kheap p = some (.notification y) → q.Nodup → + (∀ t ∈ q, NtfnBlockedA σ t) → ∀ r ∈ (NonDet.mapMx (fun t => removeAndRestartNtfnQueuedThread (Z := Unit) t p) + q σ).1, LoopInvA r.2 m n := by + induction q with + | nil => + intro σ y h _ _ _ r hr + have : r = ((), σ) := hr + rw [this]; exact h + | cons t l ih => + intro σ y h hk hnd hb r hr + obtain ⟨z, hz, hr⟩ := hr + obtain ⟨h1, ⟨y1, hk1⟩, hkeep⟩ := h.removeAndRestart hk (hb t List.mem_cons_self) hz + obtain ⟨hnt, hnd'⟩ := List.nodup_cons.1 hnd + exact ih h1 hk1 hnd' (fun u hu => hkeep u (fun e => hnt (e ▸ hu)) (hb u (List.mem_cons_of_mem _ hu))) r hr + +/-- **`cancel_all_signals` keeps `SInvXA`, `SIdleA` and `SActA`** (module docstring). -/ +theorem cancelAllSignals_invA {σ : State Unit} {m : McState} {n : Nat} {p : ObjRef} {y : Notification} + (h : LoopInvA σ m n) (hk : σ.kheap p = some (.notification y)) + (hq : ∀ q, y.ntfnObj = .waitingNtfn q → q.Nodup ∧ ∀ t ∈ q, NtfnBlockedA σ t) + {r : Unit × State Unit} (hr : r ∈ (cancelAllSignals (Z := Unit) p σ).1) : LoopInvA r.2 m n := by + unfold cancelAllSignals at hr + rw [Notif.nd_bind_one (Notif.getNotification_eq hk)] at hr + cases e : y.ntfnObj with + | waitingNtfn q => + rw [e] at hr + obtain ⟨z, hz, hr⟩ := mem_bindA.1 hr + obtain ⟨hnd, hb⟩ := hq q e + exact (LoopInvA.ntfnLoop q h hk hnd hb z hz).reschedule hr + | idleNtfn => + rw [e] at hr + have : r = ((), σ) := hr + rw [this]; exact h + | activeNtfn b => + rw [e] at hr + have : r = ((), σ) := hr + rw [this]; exact h + +/-! ## The two unlinks and the whole finaliser -/ + +theorem threadSet_refK (f : Tcb → Tcb) (hf : ∀ x, TcbEqLA x (f x)) (t : ObjRef) : + PresR RefK (threadSet (Z := Unit) f t) := by + intro σ r hr + obtain ⟨y1, hy1, h⟩ := hr + have hy1' : y1 ∈ (NonDet.assertOpt (getTcb (Z := Unit) t σ) σ).1 := hy1 + cases hg : getTcb (Z := Unit) t σ with + | none => rw [hg] at hy1'; exact absurd hy1' (Set.notMem_empty _) + | some x => + rw [hg] at hy1' + have e1 : y1 = (x, σ) := hy1' + subst e1 + obtain ⟨ko, hko, hr2⟩ := setObject_memA h + have hx := getTcb_kheapA hg + rw [hr2] + refine ⟨rfl, fun q => ?_, fun g => g.updNs t _ fun _ _ e => by cases e⟩ + show RefObjA (σ.kheap q) (Function.update σ.kheap t (some (.tcb (f x))) q) + by_cases e : q = t + · subst e; rw [Function.update_self, hx]; exact .tcb _ _ (hf x) + · rw [Function.update_of_ne e]; exact .same _ + +theorem updateSchedContext_refK (f : SchedContext → SchedContext) (hf : ∀ x, ScEqLA x (f x)) + (hrf : ∀ x, (f x).scRefills = x.scRefills) (p : ObjRef) : + PresR RefK (updateSchedContext (Z := Unit) p f) := by + intro σ r hr + obtain ⟨y, n, hk, e⟩ := updateSchedContext_memA p f hr + rw [e] + exact RefK.updSc hk (hf y) (fun g => by unfold refillHd at g ⊢; rw [hrf]; exact g) + +theorem setNotification_refK (p : ObjRef) (y' : Notification) : + PresR RefK (setSimpleKo (Z := Unit) KernelObject.notification p y') := by + intro σ r hr + unfold setSimpleKo at hr + obtain ⟨o, ho, hr⟩ := eqPres_mem (getObject_eqPres p) hr + obtain ⟨_, -, hr⟩ := eqPres_mem (PresR.assert _) hr + obtain ⟨_, hpi, hr⟩ := eqPres_mem (PresR.assert _) hr + obtain ⟨ko, hko, e⟩ := setObject_memA hr + have hgo : σ.kheap p = some o := by + have ho' : (o, σ) ∈ (NonDet.assertOpt (σ.kheap p) σ).1 := ho + cases hk : σ.kheap p with + | none => rw [hk] at ho'; exact absurd ho' (Set.notMem_empty _) + | some o' => + rw [hk] at ho' + have := congrArg Prod.fst (show (o, σ) = (o', σ) from ho') + exact congrArg some this.symm + have hn : ∃ a, o = .notification a := by + have h1 := hpi.1 + unfold partialInv at h1 + split_ifs at h1 with hu + · obtain ⟨a, ha, -⟩ := hu + exact ⟨a, ha.symm⟩ + · exact absurd rfl h1 + obtain ⟨a, ea⟩ := hn + rw [e] + refine ⟨rfl, fun q => ?_, fun g => g.updNs p _ fun _ _ e => by cases e⟩ + show RefObjA (σ.kheap q) (Function.update σ.kheap p (some (.notification y')) q) + by_cases eq : q = p + · subst eq; rw [Function.update_self, hgo, ea]; exact .ntfn _ _ + · rw [Function.update_of_ne eq]; exact .same _ + +theorem getNtfnObjRef_eqPres (f : Notification → Option ObjRef) (p : ObjRef) : + PresR (@Eq (State Unit)) (getNtfnObjRef (Z := Unit) f p) := by + unfold getNtfnObjRef getSkObjRef getSimpleKo + refine PresR.bind (PresR.bind (getObject_eqPres p) (fun _ => ?_)) (fun _ => PresR.pure _) + eqpres + +theorem setNtfnObjRef_refK (u : (Option ObjRef → Option ObjRef) → Notification → Notification) (p : ObjRef) + (new : Option ObjRef) : PresR RefK (setNtfnObjRef (Z := Unit) u p new) := by + unfold setNtfnObjRef updateSkObjRef + refine PresR.bind (eqPres_refK ?_) (fun _ => setNotification_refK _ _) + unfold getSimpleKo + refine PresR.bind (getObject_eqPres p) (fun _ => ?_) + eqpres + +theorem unbindMaybeNotification_refK (p : ObjRef) : PresR RefK (unbindMaybeNotification (Z := Unit) p) := by + unfold unbindMaybeNotification + refine PresR.bind (eqPres_refK (getNtfnObjRef_eqPres _ p)) (fun o => ?_) + cases o with + | none => exact PresR.pure _ + | some t => + show PresR RefK (doUnbindNotification (Z := Unit) p t) + unfold doUnbindNotification + refine PresR.bind (setNtfnObjRef_refK _ _ _) (fun _ => ?_) + exact threadSet_refK (tcbBoundNotificationUpdate fun _ => none) (fun _ => ⟨rfl, rfl, rfl, rfl, Or.inl rfl⟩) t + +theorem schedContextMaybeUnbindNtfn_refK (p : ObjRef) : + PresR RefK (schedContextMaybeUnbindNtfn (Z := Unit) p) := by + unfold schedContextMaybeUnbindNtfn + refine PresR.bind (eqPres_refK (getNtfnObjRef_eqPres _ p)) (fun o => ?_) + cases o with + | none => exact PresR.pure _ + | some sc => + show PresR RefK (doUnbindNtfnSc (Z := Unit) p sc) + unfold doUnbindNtfnSc + refine PresR.bind (setNtfnObjRef_refK _ _ _) (fun _ => ?_) + exact updateSchedContext_refK (scNtfnUpdate fun _ => none) (fun _ => ⟨rfl, rfl, rfl, id⟩) (fun _ => rfl) sc + +/-- The queued threads of a related notification are distinct TCBs blocked on a notification. -/ +theorem ntfnQueue_blockedA {N T : Set U64} {mem : U64 → U64} {σ : State Unit} + (hR : Notif.NtfnStateRel N T mem σ) {n : U64} (hn : n ∈ N) {y : Notification} + (hk : σ.kheap (specPtr n) = some (.notification y)) : + ∀ q, y.ntfnObj = .waitingNtfn q → q.Nodup ∧ ∀ t ∈ q, NtfnBlockedA σ t := by + intro q hq + have hobj := (hR.at_ntfn hn hk).obj + rw [hq] at hobj + obtain ⟨-, xs, -, hqx, hQ, hB⟩ := hobj + rw [hqx] + refine ⟨(List.nodup_map_iff (fun a b h => specPtr_inj h)).2 hQ.nodup, fun u hu => ?_⟩ + obtain ⟨t, ht, rfl⟩ := List.mem_map.1 hu + obtain ⟨x, hx, hxt⟩ := hR.tcb t (hQ.mem t ht) + obtain ⟨o, ho⟩ : ∃ o, x.tcbState = .blockedOnNotification o := by + have := hxt.st + rw [(hB t ht).1] at this + exact Notif.tsCode_six' this.symm + exact ⟨x, o, hx, ho⟩ + +/-- **The notification finaliser's spec chain keeps the three invariants**: the SC unlink, the bound-TCB unlink and +`cancel_all_signals`. The relation `hR` at the state after the unlinks gives the queue's shape. -/ +theorem LoopInvA.ntfnFinalise {σ σ1 σ2 σ3 : State Unit} {m : McState} {n : Nat} {N T : Set U64} {mem : U64 → U64} + {p : U64} (h : LoopInvA σ m n) (h1 : ((), σ1) ∈ (schedContextMaybeUnbindNtfn (Z := Unit) (specPtr p) σ).1) + (h2 : ((), σ2) ∈ (unbindMaybeNotification (Z := Unit) (specPtr p) σ1).1) + (hR : Notif.NtfnStateRel N T mem σ2) (hp : p ∈ N) + (h3 : ((), σ3) ∈ (cancelAllSignals (Z := Unit) (specPtr p) σ2).1) : LoopInvA σ3 m n := by + have a1 : LoopInvA σ1 m n := h.refK (schedContextMaybeUnbindNtfn_refK _ σ _ h1) + have a2 : LoopInvA σ2 m n := a1.refK (unbindMaybeNotification_refK _ σ1 _ h2) + obtain ⟨y, hk, -⟩ := hR.ntfn p hp + exact cancelAllSignals_invA a2 hk (ntfnQueue_blockedA hR hp hk) h3 + +section RecA +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST + +/-- **`of_schedWrN`'s `sinv`, `sidle` and `sact` after the notification finaliser**, from the per-step record +before it. -/ +theorem JStage3TA.ntfnFin_invA {S A : Type} {st : machine.Store S} [L : LawfulStore st] {AR : A → HwRootA → Prop} + {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} + {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J5 J7 : JSlotA S} + {s : S} {a : A} {σ σ1 σ2 σ3 : State Unit} {m : McState} + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {N' T : Set U64} {mem : U64 → U64} {p : U64} + (h1 : ((), σ1) ∈ (schedContextMaybeUnbindNtfn (Z := Unit) (specPtr p) σ).1) + (h2 : ((), σ2) ∈ (unbindMaybeNotification (Z := Unit) (specPtr p) σ1).1) + (hR : Notif.NtfnStateRel N' T mem σ2) (hp : p ∈ N') + (h3 : ((), σ3) ∈ (cancelAllSignals (Z := Unit) (specPtr p) σ2).1) : + SInvXA σ3 m n ∧ SIdleA σ3 m n ∧ SActA σ3 := by + have h := LoopInvA.ntfnFinalise (m := m) (n := n) ⟨J.sinv, J.sidle, J.sact⟩ h1 h2 hR hp h3 + exact ⟨h.sinv, h.sidle, h.sact⟩ + +end RecA + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelSlotWA.lean b/proofs/Fl4ProofsA64/P3/DelSlotWA.lean new file mode 100644 index 000000000..1162cc999 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelSlotWA.lean @@ -0,0 +1,478 @@ +/- +P3 (AArch64, delete hooks E4, Option T): **the per-step record `JStage3TA` across the deletion loop's slot writes**. + +The loop's swap, zombie write and emptying write only cap-slot words (`W7.CapW H.tcbs {InOS OS} ∅`: `OS` slot words and +the TCBs' seven cap slots below offset 224) with a slot-only spec step (`SpecSlotA`). **`JStage3TA.of_slotW`** carries +every non-cap field of the record across such a write: + +* a written (aligned) word lies in a heap TCB below offset 224 or in an `other` heap object (**`capw_inObjA`**, + from `os_capw`), so it is apart from every SC, endpoint, notification and reply object and from the TCB fields + from 224 (**`capw_apartA`**), and from the node words (`capw_ne_node`); +* the state-block globals the scheduler relation reads are off the slot words by the decode-site footprint + (`hN`, `DelFootA.off`); +* the spec side is `SpecSlotA.sstepA` / `.kviewA` (`P3/IpcXferOrthA`) and TCBs equal up to their caps (`tcbNcA`). + +The fields that read caps (the dead / free / original-cap facts, the IPC buffer slot, `objAlloc`, `idleAnchor`, +`finCov`, `sinv` (the IRQ node), `j5` / `j7`, ...) and the relation `j` are hypotheses, proved per step. + +The reachable VSpace words (`vsOk`: `VrOK` over the narrowed `ent.vs`, D1b) are off the slot words +(`JStage3TA.vsr_offCapWA`): the two temporary hypotheses `hvsW` / `halv` are gone. The new D1b fields `zombieBnd` and +`devUtOff` read caps and are hypotheses, proved per step (`UtZombieA.ofSrcA`, `DevUtOffA.fromA`). +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3VsWriteTA +import Fl4ProofsA64.P3.Stage3SchedWrTA +import Fl4ProofsA64.P3.IpcXferOrthA +import Fl4ProofsA64.P3.DelRecA +import Fl4ProofsA64.P3.DelSactSlotA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-! ## Where a slot word lies -/ + +theorem wa_val_tcbA {H : Heap} (hH : HeapOK H) {t K : U64} (ht : H t = some .tcb) (hK : K.val < 4096) : + (wa t K).val = t.val + K.val := by + have hf := hH.fits t _ ht + simp only [HKind.bits] at hf + simp [core.num.U64.wrapping_add_val_eq, U64.size_def, U64.numBits]; omega + +/-- **An aligned slot word lies in a heap TCB below offset 224 or in an `other` heap object.** -/ +theorem capw_inObjA {H : Heap} {OS : Set U64} (hH : HeapOK H) (hO : OSOK H.tcbs H.omega H.frame OS) {b : U64} + (hb : Aligned b) (h : W7.CapW H.tcbs {b | InOS OS b} ∅ b) : + ∃ p k, H p = some k ∧ InObj p k b ∧ ((k = .tcb ∧ b.val < p.val + 224) ∨ ∃ z, k = .other z) := by + rcases os_capw hO hb h with ⟨t, ht, K, hK, e⟩ | ⟨⟨-, p, k, hp, hk, hin⟩, hF⟩ + · have ht' : H t = some .tcb := ht + have hv := wa_val_tcbA hH ht' (K := K) (by omega) + subst e + exact ⟨t, .tcb, ht', ⟨by omega, by simp only [HKind.bits]; omega⟩, Or.inl ⟨rfl, by omega⟩⟩ + · refine ⟨p, k, hp, hin, Or.inr ?_⟩ + rcases hk with rfl | ⟨z, rfl⟩ | ⟨z, rfl⟩ | ⟨rfl, hw⟩ + · exact absurd ⟨hb, p, .reply, hp, Or.inl rfl, hin⟩ hF + · exact absurd ⟨hb, p, .sc z, hp, Or.inr (Or.inl ⟨z, rfl⟩), hin⟩ hF + · exact ⟨z, rfl⟩ + · exact absurd ⟨hb, p, .ntfn, hp, Or.inr (Or.inr ⟨rfl, hw⟩), hin⟩ hF + +/-- **A slot word is apart from every heap object but a TCB (below 224) or an `other` object.** -/ +theorem capw_apartA {H : Heap} {OS : Set U64} (hH : HeapOK H) (hO : OSOK H.tcbs H.omega H.frame OS) {b : U64} + (hb : Aligned b) (h : W7.CapW H.tcbs {b | InOS OS b} ∅ b) {q : U64} {k' : HKind} (hq : H q = some k') + (hin : InObj q k' b) (hk : (k' ≠ .tcb ∧ ∀ z, k' ≠ .other z) ∨ (k' = .tcb ∧ q.val + 224 ≤ b.val)) : False := by + obtain ⟨p, k, hp, hinp, hkp⟩ := capw_inObjA hH hO hb h + by_cases e : p = q + · subst e + rw [hp] at hq + cases hq + rcases hkp with ⟨rfl, h2⟩ | ⟨z, rfl⟩ + · rcases hk with ⟨h1, -⟩ | ⟨-, h3⟩ + · exact h1 rfl + · omega + · rcases hk with ⟨-, h1⟩ | ⟨h1, -⟩ + · exact h1 z rfl + · cases h1 + · have := hH.disj p k q k' hp hq e + unfold InObj at hinp hin + omega + +/-- The TCB fields from offset 224 are off the slot words. -/ +theorem capw_tcbHiA {H : Heap} {OS : Set U64} (hH : HeapOK H) (hO : OSOK H.tcbs H.omega H.frame OS) {b : U64} + (hb : Aligned b) (h : W7.CapW H.tcbs {b | InOS OS b} ∅ b) {t : U64} (ht : H t = some .tcb) + (h1 : t.val + 224 ≤ b.val) (h2 : b.val < t.val + 4096) : False := + capw_apartA hH hO hb h ht ⟨by omega, by simp only [HKind.bits]; omega⟩ (Or.inr ⟨rfl, h1⟩) + +/-- A word of a heap object of kind SC / endpoint / notification / reply is off the slot words. -/ +theorem capw_objOffA {H : Heap} {OS : Set U64} (hH : HeapOK H) (hO : OSOK H.tcbs H.omega H.frame OS) {b : U64} + (hb : Aligned b) (h : W7.CapW H.tcbs {b | InOS OS b} ∅ b) {q : U64} {k' : HKind} (hq : H q = some k') + (hin : InObj q k' b) (hk : k' = .ep ∨ k' = .ntfn ∨ k' = .reply ∨ ∃ z, k' = .sc z) : False := by + refine capw_apartA hH hO hb h hq hin (Or.inl ⟨?_, fun z e => ?_⟩) + · rintro rfl; rcases hk with h | h | h | ⟨z, h⟩ <;> cases h + · subst e; rcases hk with h | h | h | ⟨z', h⟩ <;> cases h + +/-- An aligned write of slot words, as a write set of aligned slot words (for P1's `W7.blk_of_capw`). -/ +theorem writesIn_alA {s s' : S} {P : U64 → Prop} (w : WritesIn st s s' P) : + WritesIn st s s' (fun b => Aligned b ∧ P b) := + ⟨w.1, w.2.1, fun b hb hn => w.2.2 b hb fun h => hn ⟨hb, h⟩⟩ + +/-- **A VSpace word of the reachable set is no slot word** (`VrOK`: placed in an `other` object, off the `OS` +slots). -/ +theorem JStage3TA.vsr_offCapWA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J5 J7 : JSlotA S} {s : S} {a : A} + {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) {Pt Ub : Set U64} + (vr : VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt) {b : U64} (h : VsR (L.ksb s) Pt b) : + ¬ W7.CapW H.tcbs {b | InOS OS b} ∅ b := by + rintro (⟨t, ht, K, hK, e⟩ | ⟨hb, -⟩) + · obtain ⟨Q, R', q⟩ := J.ent.bxi.1 + have hΩ := vr.om b h + have := (q.omega b hΩ).2.1 t ht + have hv := wa_val_tcbA J.ent.heap (show H t = some .tcb from ht) (K := K) (by omega) + rw [e, hv] at this + omega + · exact vr.os b h hb + +/-- `FootNA` does not read the loop's ghost, queues or hardware root. -/ +theorem footNA_loopA {Z : XSetsA} {G : MdbGhostA} {Q : U64 → U64 → List U64} {R : U64 → List U64} + {hw : HwRootA} {s : S} {b : U64} (h : FootNA (L := L) { Z with G := G, Q := Q, R := R, hw := hw } s b) : + FootNA (L := L) Z s b := by + cases h with + | node c w hc hw => exact .node c b hc hw + | cur w hw => exact .cur b hw + | tcb t w ht hw => exact .tcb t b ht hw + | sc c hc => exact .sc c hc + | vcpu v w hv hw => exact .vcpu v b hv hw + | irq i hi => exact .irq i hi + | cur2 w hw => exact .cur2 b hw + | glob w hw => exact .glob b hw + | tcb2 t w ht hw => exact .tcb2 t b ht hw + | sc2 c hc => exact .sc2 c hc + | vs w hw => exact .vs b hw + | ext w hw => exact .ext b hw + +/-! ## The record across slot writes -/ + +/-- (record edit #8b-B1) The loop's J7 carries `J7PpA`'s facts. -/ +instance J7LA.ppA (R : Set U64 → JSlotA S) : J7PpA st (J7LA st R) := + ⟨fun _ _ _ _ _ _ h => by + obtain ⟨Pt, Ub, vr, pn, arm, capc, vc, df, hicap, link, vcks, r⟩ := h + exact ⟨Pt, Ub, vr, arm, link, vcks⟩⟩ + +set_option maxHeartbeats 1000000 in +/-- The IPC placement at fewer tracked buffers. -/ +theorem Endp.IPlacedA.monoBfA {N T E R : Set U64} {Bf Bf' : Set (U64 × Fl4SpecA64.Word)} + (h : Endp.IPlacedA N T E R Bf) (sub : Bf' ⊆ Bf) : Endp.IPlacedA N T E R Bf' := by + have bw : ∀ w, BufW Bf' w → BufW Bf w := fun w ⟨bp, hbp, rest⟩ => ⟨bp, sub hbp, rest⟩ + exact + { n := h.n + e := + { tcb := h.e.tcb + ep := h.e.ep + rep := h.e.rep + et := h.e.et + rt := h.e.rt + er := h.e.er + balign := fun w g => h.e.balign w (bw w g) + bt := fun w g => h.e.bt w (bw w g) + be := fun w g => h.e.be w (bw w g) + br := fun w g => h.e.br w (bw w g) + binj := fun bp hbp bq hbq => h.e.binj bp (sub hbp) bq (sub hbq) } + ne := h.ne + nr := h.nr + nb := fun q hq w g => h.nb q hq w (bw w g) } + +/-- The scheduler placement at fewer tracked buffers. -/ +theorem Endp.IpcSchedPlacementA.monoBfA {th : TypedHeapA} {N E R : Set U64} {Bf Bf' : Set (U64 × Fl4SpecA64.Word)} + (h : IpcSchedPlacementA th N E R Bf) (sub : Bf' ⊆ Bf) : IpcSchedPlacementA th N E R Bf' := + { notification := h.notification + endpoint := h.endpoint + reply := h.reply + buffer := fun w ⟨bp, hbp, rest⟩ => h.buffer w ⟨bp, sub hbp, rest⟩ } + +/-- **`JStage3TA` across a write of slot words** (module docstring). -/ +theorem JStage3TA.of_slotW {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J5 : JSlotA S} {Rj : Set U64 → JSlotA S} {s s' : S} + {a : A} {σ σ' : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7LA st Rj) s a σ m) + {Bf' : Set (U64 × Fl4SpecA64.Word)} (sub : Bf' ⊆ Bf) + {G' : MdbGhostA} (w : WritesIn st s s' (W7.CapW H.tcbs {b | InOS OS b} ∅)) + (HS : SpecSlotA σ σ') (hm : σ'.machineState = σ.machineState) + (hN : ∀ b, FootNA (L := L) Z s b → ¬ W7.CapW H.tcbs {b | InOS OS b} ∅ b) + (jx : JAtZAX (L := L) AR { Z with G := G' } s' a σ' m) + (sinv : SInvXA σ' m n) + (sidle : SIdleA σ' m n) + (objAlloc : ObjAllocA (st := st) { Z with G := G' } H OS s') + (idleAnchor : IdleAnchorOffA (st := st) OS s') + (bufCover : BufCover st Z.T Bf' s' σ') + (bufNative : BufNativeA Z.T (L.mem s')) + (deadExt : DeadExtA st { Z with G := G' } OS Rp stack s' σ') + (specFree : SpecFreeA st OS s' σ') + (utBlk : UtBlk39A st OS s') + (unmPt : UnmPtSpecA st { Z with G := G' } m OS s' σ') + (specZero : SpecZeroFreeA st OS s' σ') + (origLive : ∀ sl, σ'.isOriginalCap sl = true → AbsSlotA Z.th sl) + (nullOrig : ∀ sl, specCapAtA σ' sl = some .nullCap → σ'.isOriginalCap sl = false) + (frBase : FrBaseApartA st OS s') + (devFr : DevFrDisjA st H OS s' σ') + (finCov : FinCovA Z.th σ') + (zombieNoVcpu : ZombieTcbNoVcpuA st OS s') + (zombieTcb : ZombieTcbTA st H OS s') + (bufOff : BufOffA st { Z with G := G' } OS s' Bf') + (zombieBnd : UtZombieA st OS s') + (devUtOff : DevUtOffA st { Z with G := G' } H OS ia ie s') + (capTy : CapTyA st H OS s') + (ciSc : ∀ u ∈ OS, ∀ p b, capA st s' u = .SchedContext p b → specPtr p ≠ m.idleScOf m.curCore) + (j5 : J5 { Z with G := G' } H OS s' σ' m) + (j7 : J7LA st Rj { Z with G := G' } H OS s' σ' m) : + JStage3TA st AR { Z with G := G' } H OS U Ac Pp Tb Lv ia ie N E Rp Bf' stack κ n J5 (J7LA st Rj) s' a σ' m := by + have hk := w.1 + have hc := w.2.1 + have hH := J.ent.heap + have hO := J.ent.os + have wA := writesIn_alA w + have hB : ∀ b, (Aligned b ∧ W7.CapW H.tcbs {b | InOS OS b} ∅ b) → W7.CapW H.tcbs H.omega H.frame b := + fun b hb => os_capw hO hb.1 hb.2 + obtain ⟨blk, -, -, -, -, fld, nod, -⟩ := W7.blk_of_capw (BXI.blk J.ent.bxi) wA hB + obtain ⟨Qb, Rb, q⟩ := J.ent.bxi.1 + have keep : ∀ b, Aligned b → ¬ W7.CapW H.tcbs {b | InOS OS b} ∅ b → L.mem s' b = L.mem s b := + fun b hb hn => w.mem_eq hb hn + have hT : ∀ t ∈ Z.T, H t = some .tcb := fun t ht => by + have : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + exact this + -- the TCB fields from 224 + have tw : ∀ t ∈ Z.T, ∀ K : U64, 224 ≤ K.val → K.val + 8 ≤ 4096 → K.val % 8 = 0 → + L.mem s' (wa t K) = L.mem s (wa t K) := fun t ht K h0 hK h8 => + fld t (by rw [J.tcbs]; exact ht) K h0 (by omega) h8 + -- the spec side + obtain ⟨SS, NB⟩ := Endp.SpecSlotA.sstepA HS + have kS : ∀ p x k, σ.kheap p = some (.schedContext x k) → σ'.kheap p = some (.schedContext x k) := + fun p x k h => HS.keep h (fun _ _ e => by cases e) (fun _ e => by cases e) + have bS : ∀ p x k, σ'.kheap p = some (.schedContext x k) → σ.kheap p = some (.schedContext x k) := + fun p x k h => HS.back h (fun _ _ e => by cases e) (fun _ e => by cases e) + have bT : ∀ p y, σ'.kheap p = some (.tcb y) → ∃ x, σ.kheap p = some (.tcb x) ∧ tcbNcA y = tcbNcA x := by + intro p y h + rcases HS.kh p with e | ⟨r, cs, cs', -, e2⟩ | ⟨x, x', e1, e2, e3⟩ + · exact ⟨y, e ▸ h, rfl⟩ + · rw [h] at e2; cases e2 + · rw [h] at e2; cases e2; exact ⟨x, e1, e3⟩ + -- the scheduler relation + have sr0 : SchedSRA st Z m κ s' σ' := by + refine J.sr.stable wA (fun b hb hr => ?_) SS NB + have hcw := hB b hb + rcases hr with ⟨c, hcs, o, ho, e⟩ | e | e | ⟨t, ht, K, hK, e⟩ | h + · exact capw_ne_node q hcw hcs ho e + · exact hN b (.glob b (Or.inl e)) hb.2 + · exact hN b (.glob b (Or.inr e)) hb.2 + · have hKv : 224 ≤ K.val ∧ K.val < 4096 := by + rcases hK with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> simp + exact capw_ne_hi q hcw (by rw [J.tcbs]; exact ht) hKv.1 hKv.2 e + · obtain ⟨c, -, z, hz, hin⟩ := J.scFoot_heapA h + have e : H.scBits c = z := by unfold Heap.scBits; rw [hz] + rw [e] at hin + exact capw_objOffA hH hO hb.1 hb.2 hz ⟨hin.1, by simp only [HKind.bits]; exact hin.2⟩ + (Or.inr (Or.inr (Or.inr ⟨z, rfl⟩))) + have sr : SchedSRA st { Z with G := G' } m κ s' σ' := + ⟨{ sr0.toSchedSRN with rest := { sr0.rest with } }, sr0.rd⟩ + -- the IPC relations + have ipcOff : ∀ b, Aligned b → W7.CapW H.tcbs {b | InOS OS b} ∅ b → ¬ IpcObjWA N E Rp b := fun b hb h hi => by + obtain ⟨p, k, hp, hk, hin⟩ := J.ipc_hobjA hi + exact capw_objOffA hH hO hb h hp hin (by rcases hk with rfl | rfl | rfl <;> simp) + have tcbOff : ∀ b, Aligned b → W7.CapW H.tcbs {b | InOS OS b} ∅ b → ∀ t ∈ Z.T, + t.val + 224 ≤ b.val → b.val < t.val + 4096 → False := fun b hb h t ht h1 h2 => + capw_tcbHiA hH hO hb h (hT t ht) h1 h2 + have ef : Endp.FootEq Z.T E Rp Bf (L.mem s) (L.mem s') := by + intro b hb h + refine keep b hb (fun hp => ?_) + rcases h with ⟨e, he, h⟩ | ⟨r, hr, h⟩ | ⟨t, ht, h⟩ | h + · exact ipcOff b hb hp (Or.inr (Or.inl ⟨e, he, by omega, by omega⟩)) + · exact ipcOff b hb hp (Or.inr (Or.inr ⟨r, hr, by omega, by omega⟩)) + · rcases h with ⟨h1, h2⟩ | h + · unfold TOffN at h2; exact tcbOff b hb hp t ht (by omega) (by omega) + · unfold TL at h; exact tcbOff b hb hp t ht (by omega) (by omega) + · rcases hp with ⟨t, ht, K, hK, e⟩ | ⟨hos, -⟩ + · have ht' : t ∈ Z.T := by rw [← J.tcbs]; exact ht + have := J.iplaced.e.bt b h t ht' + have hv := wa_val_tcbA hH (hT t ht') (K := K) (by omega) + rw [e, hv] at this + omega + · exact J.bufOff.os b h hos + have ep : EpStateRel Z.T E Rp Bf (L.mem s') σ' := J.ep.frame J.iplaced.e ef (Endp.SpecSlotA.kviewA HS hm) + have ntfn : NtfnStateRel N Z.T (L.mem s') σ' := by + refine J.ntfn.frame J.iplaced.n (fun b hb h => keep b hb (fun hp => ?_)) + ⟨fun p x h => HS.keep h (fun _ _ e => by cases e) (fun _ e => by cases e), fun p x h => ?_⟩ + · rcases h with ⟨nn, hn, h1, h2⟩ | ⟨t, ht, h⟩ + · exact ipcOff b hb hp (Or.inl ⟨nn, hn, h1, by omega⟩) + · have hta := J.sr.core.tcbs t ht + have fv : ∀ K : U64, K.val < 4096 → (wa t K).val = t.val + K.val := fun K hK => tfield_val hta hK + have inT : t.val + 224 ≤ b.val ∧ b.val < t.val + 4096 := by + rcases h with rfl | rfl | rfl | rfl | rfl | rfl + · rw [stA_eq, fv _ (by simp)]; simp + · unfold tBlockA; rw [fv _ (by simp)]; simp + · rw [badgeRegA_eq, fv _ (by simp)]; simp + · unfold prioA; rw [fv _ (by simp)]; simp + · rw [nextA_val hta]; simp [nextOff] + · rw [prevA_val hta]; simp [prevOff] + exact tcbOff b hb hp t ht inT.1 inT.2 + · obtain ⟨y, hy, e⟩ := HS.tcb h + obtain ⟨h1, -, -, h4, h5, -⟩ := tcbNcA_eq e + exact ⟨y, hy, h1, by unfold badgeOf; rw [h5], h4⟩ + have reply : ReplyStackRelA (L.mem s') Rp Z.C σ' (scNameA m) stack := by + refine J.reply.frame (fun r hr => ?_) (fun c _ x k h => ⟨x, k, kS _ _ _ h, rfl⟩) + (fun r _ x' h => ⟨x', HS.back h (fun _ _ e => by cases e) (fun _ e => by cases e), rfl⟩) + have hr3 := J.iplaced.e.rep r hr + have key : ∀ o : U64, o.val = 8 ∨ o.val = 16 → L.mem s' (wa r o) = L.mem s (wa r o) := by + intro o ho + have hv : (wa r o).val = r.val + o.val := by + simp [core.num.U64.wrapping_add_val_eq, U64.size_def, U64.numBits]; omega + have hal : Aligned (wa r o) := by unfold Aligned; rw [hv]; omega + exact keep _ hal (fun hp => ipcOff _ hal hp (Or.inr (Or.inr ⟨r, hr, by omega, by omega⟩))) + exact ⟨key _ (Or.inr (by simp [layout.reply.NEXT])), key _ (Or.inl (by simp [layout.reply.PREV]))⟩ + have mcp : McpRelA Z.T m (L.mem s') σ' := fun t ht => by + obtain ⟨x, hx, e, hf⟩ := J.mcp t ht + obtain ⟨y, hy, ey⟩ := HS.tcb hx + have hm' : y.tcbMcpriority = x.tcbMcpriority := by have h0 := congrArg Fl4SpecA64.Tcb.tcbMcpriority ey; exact h0 + have hfl : y.tcbFlags = x.tcbFlags := by have h0 := congrArg Fl4SpecA64.Tcb.tcbFlags ey; exact h0 + refine ⟨y, hy, ?_, fun g => (hf g).keep ?_ hfl⟩ + · unfold mcpA at e ⊢ + rw [tw t ht _ (by simp) (by simp) (by simp), hm'] + exact e + · unfold flagsA + exact tw t ht _ (by simp) (by simp) (by simp) + have ye : ∀ t ∈ Z.T, L.mem s' (yieldA t) = L.mem s (yieldA t) := fun t ht => by + unfold yieldA; exact tw t ht _ (by simp) (by simp) (by simp) + have yieldRel : YieldRelA Z.T Z.C m (L.mem s') σ' := by + refine ⟨fun t ht => ?_, fun t ht h => ?_, fun c hcc y k h p hp => ?_⟩ + · obtain ⟨x, hx, e⟩ := J.yieldRel.tcb t ht + obtain ⟨y, hy, ey⟩ := HS.tcb hx + have hy' : y.tcbYieldTo = x.tcbYieldTo := by have h0 := congrArg Fl4SpecA64.Tcb.tcbYieldTo ey; exact h0 + exact ⟨y, hy, by rw [ye t ht, hy']; exact e⟩ + · rw [ye t ht] at h ⊢ + obtain ⟨hC, y, k, hy, e⟩ := J.yieldRel.toSc t ht h + exact ⟨hC, y, k, kS _ _ _ hy, e⟩ + · obtain ⟨t, ht, e1, e2⟩ := J.yieldRel.frm c hcc y k (bS _ _ _ h) p hp + exact ⟨t, ht, e1, by rw [ye t ht]; exact e2⟩ + have idleNoFault : IdleNoFaultA m σ' := fun c hcn => by + obtain ⟨x, hx, e⟩ := J.idleNoFault c hcn + obtain ⟨y, hy, ey⟩ := HS.tcb hx + exact ⟨y, hy, by rw [← e]; have h0 := congrArg Fl4SpecA64.Tcb.tcbFault ey; exact h0⟩ + -- the entry invariants + have vsK : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → L.mem s' b = L.mem s b := fun b hb => + keep b (J.vsOk.al b hb) (J.vsr_offCapWA J.vsOk hb) + have vsT := utVsPt_keepA J.ent.vs hk vsK + have ent : EntryInvA3 st H Z.Cs OS U Ac Pp Tb Lv s' := + { heap := hH + bxi := BlkOK.bxi blk + os := hO + user := J.ent.user.ksb hk + vs := vsT.1 + ksw := by rw [hk]; exact J.ent.ksw } + have sq : SchedQueuesInv st H.tcbs Z.Cs H.omega H s' Z.Q Z.R := + W7.inv_of_capw J.sq wA fun b hb => (hB b hb).elim Or.inr fun h' => Or.inl (Or.inl h'.1) + have poolOff : PoolOff st H Z.Cs OS s' := by + refine poolOff_keepTableA (Z := Z) J.poolOff hk fun i => vsK _ (Or.inr ?_) + obtain ⟨v1, v2, v3⟩ := AtOff.v i + exact ⟨AtOff i, v1, v2, v3, rfl⟩ + exact + { ent := ent + j := jx + tcbs := J.tcbs + scs := J.scs + sq := sq + ap := by rw [show L.ksb s' = L.ksb s from hk]; exact { J.ap with } + pp := J.pp + s1 := J.s1 + ko := by rw [show L.ksb s' = L.ksb s from hk]; exact J.ko + ptH := J.ptH + tabInj := fun i j p h1 h2 => J.tabInj i j p (by rw [← HS.asid]; exact h1) (by rw [← HS.asid]; exact h2) + poolOff := poolOff + objAlloc := objAlloc + idleAnchor := idleAnchor + vsVc := by rw [show L.ksb s' = L.ksb s from hk]; exact J.vsVc + sr := sr + scCov := fun p x k h => J.scCov p x k (bS _ _ _ h) + scSize := fun c hcm x k h => J.scSize c hcm x k (bS _ _ _ h) + ep := { ep := ep.ep, reply := ep.reply, tcb := ep.tcb, buf := fun bp h => ep.buf bp (sub h) } + ntfn := ntfn + reply := reply + bufCover := bufCover + bufNative := bufNative + iplaced := J.iplaced.monoBfA sub + ipcPlace := J.ipcPlace.monoBfA sub + idleNoFault := idleNoFault + mcp := mcp + yieldRel := yieldRel + sinv := sinv + sidle := sidle + sact := J.sact.slotA HS + setsInH := { J.setsInH with } + utIpc := J.utIpc + ipcTh := J.ipcTh + deadExt := deadExt + specFree := specFree + utBlk := utBlk + unmPt := unmPt + tabS1 := fun qq hq => + ⟨fun i hi h => (J.tabS1 qq hq).mem i hi + (footA_schedA (Z := Z) (Q := Z.Q) (Rl := Z.R) (curOf := Z.curOf) (curScOf := Z.curScOf) hk hc + (FootA.monoA (Z := { { Z with G := G' } with S1 := ∅ }) rfl rfl (fun _ h => h) (fun _ h => h) + (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) h)), + (J.tabS1 qq hq).obj⟩ + tabExt := + ⟨fun qq hq i hi h => J.tabExt.mem qq hq i hi + (footA_schedA (Z := extZA Z) (Q := Z.Q) (Rl := Z.R) (curOf := Z.curOf) (curScOf := Z.curScOf) hk hc + (FootA.monoA (Z := extZA { Z with G := G' }) rfl rfl (fun _ h => h) (fun _ h => h) + (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) h)), + J.tabExt.obj⟩ + specZero := specZero + origLive := origLive + nullOrig := nullOrig + thPos := fun p z h => J.thPos p z h + frBase := frBase + devFr := devFr + finCov := finCov + idleAll := J.idleAll.keepA + (fun c hcs => specPtr_inj ((sr.sch.idleOf c hcs).symm.trans (J.sr.sch.idleOf c hcs))) + (fun c hcs => specPtr_inj ((sr.sch.idleScOf c hcs).symm.trans (J.sr.sch.idleScOf c hcs))) + ipcHeapCov := J.ipcHeapCov + zombieNoVcpu := zombieNoVcpu + zombieTcb := zombieTcb + bufOff := bufOff + vsOk := by + unfold UtVsOkA + rw [show L.ksb s' = L.ksb s from hk, vsT.2] + exact J.vsOk + domOK := J.domOK.keepS J.sr.toSchedSRN HS.domk hk hc (fun b hb hd => w.mem_eq hb (fun h => J.capw_domA h hd)) + ipiZero := J.ipiKeepOf w fun o ho h => J.ipi_capwA ho h + bufVs := by + rw [show L.ksb s' = L.ksb s from hk, vsT.2] + exact fun b ⟨bp, hbp, r⟩ => J.bufVs b ⟨bp, sub hbp, r⟩ + bufAl := fun t ht y h => by + obtain ⟨x, hx, e⟩ := bT _ y h + have h0 : y.tcbIpcBuffer = x.tcbIpcBuffer := by + have h1 := congrArg Fl4SpecA64.Tcb.tcbIpcBuffer e + exact h1 + rw [h0]; exact J.bufAl t ht x hx + zombieBnd := zombieBnd + devUtOff := devUtOff + capTy := capTy + scCpu := J.scCpu + scNZ := J.scNZ + runVcpu := J.runVcpu.keep (fun y hy => by rw [stA_eq]; exact tw y hy 3328#u64 (by simp) (by simp) (by simp)) + (fun y hy => tw y hy 3672#u64 (by simp) (by simp) (by simp)) + vr := J.vr_keepA (fun r hr => keep _ (J.rTcbValA hr).2 (fun hp => ipcOff _ (J.rTcbValA hr).2 hp + (Or.inr (Or.inr ⟨r, hr, by have := (J.rTcbValA hr).1; omega, + by have := (J.rTcbValA hr).1; omega⟩)))) + (fun y hy => by rw [stA_eq]; exact tw y hy 3328#u64 (by simp) (by simp) (by simp)) + ciSc := ciSc + noSwitch := fun u e => J.noSwitch u (by rw [HS.act] at e; exact e) + vcRun := J.vcRun.keepA (fun t ht => tw t ht 3664#u64 (by simp) (by simp) (by simp)) (fun v hv => + keep _ (vcAl24A (J.ap.vcAl v hv)) (fun hp => hp.elim (J.ap.vc v hv _ (Or.inr rfl)).2 + (fun hp => (J.ap.vc v hv _ (Or.inr rfl)).1 hp.1))) + (fun v hv => keep v (J.ap.vcAl v hv) (fun hp => hp.elim (J.ap.vc v hv _ (Or.inl rfl)).2 + (fun hp => (J.ap.vc v hv _ (Or.inl rfl)).1 hp.1))) + (fun t ht => tw t ht 3672#u64 (by simp) (by simp) (by simp)) + blkQ := J.blkQ.keepA (fun t ht => tw t ht 3336#u64 (by simp) (by simp) (by simp)) + (fun t _ x' h => Or.inr (by + obtain ⟨x, hx, e⟩ := bT _ x' h + exact ⟨x, hx, (tcbNcA_eq e).1.symm⟩)) + (fun p y h => HS.keep h (fun _ _ e => by cases e) (fun _ e => by cases e)) + (fun p y h => HS.keep h (fun _ _ e => by cases e) (fun _ e => by cases e)) + curVcpuNone := HS.vcur.trans J.curVcpuNone + ipcSorted := J.ipcSorted.of_q (QKeepA.of_slotA HS) + activeReplyScs := J.activeReplyScs.of_q (QKeepA.of_slotA HS) + j5 := j5 + j7 := j7 } + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelSpecA.lean b/proofs/Fl4ProofsA64/P3/DelSpecA.lean new file mode 100644 index 000000000..2056d9a88 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelSpecA.lean @@ -0,0 +1,1006 @@ +/- +P3 (aarch64, A64-P3 DelSpec gen 1): **the spec-side toolkit for CNode Delete, Revoke and CancelBadgedSends** — pure +reasoning about `Fl4SpecA64`, for the S3/S4 refinement of those invocations (the A64 twin of the spec parts of x86's +`P3/CnodeRevoke`, `P3/DelSpec`, `P3/CnodeOther`, `P3/Delete`, `P3/CnodeSpec` and `P3/IpcCancelBadged`). + +This module imports only `Fl4SpecA64`; it has no `StateRelA` / `JAtZA` / `specPtr` / `Fl4CoreA64` coupling. The +spec monad lemmas are restated here (suffix `D`) so the module stays file-disjoint from S3's `LookupA` / `CnodeCapsA`. + +* **Spec monad steps** (`nd_bind_oneD`, `run_bind_okD`, `run_liftE_stD`, `mem_bindE_okD`, …): outcomes of binds. +* **Least fixed points** (x86 `CnodeRevoke.lfp_le_prefixed` / `lfp_mem`, `CnodeOther.lfp_one`): `lfp_le_prefixedA` + (every pre-fixed point bounds the `lfp`), `lfp_memA` (an outcome of `F f` for every pre-fixed point `f` is an + outcome of the `lfp`), `lfp_oneA` (the `lfp` at a point where `F` ignores the recursion). +* **`rec_del` over a finaliser `Fn`** (x86 `DelSpec`, gen 42b49): `recDelFGA Fn` (l4v's `rec_del` equations with `Fn` + for `finalise_cap`); `recDelFGA finaliseCap` is `recDelF` and `recDelFGA fl4FinaliseCap` is `Ext/Finalise`'s + `fl4RecDelF`, by `rfl` (likewise `recDelGA` / `capDeleteGA`). One lemma per branch (`rd_cte_okGA`, + `rd_fin_reduceGA`, `rd_reduce_falseGA`, …), each turning outcomes of the recursive calls into an outcome of the call. +* **`cap_revoke` over a deletion `Dl`** (x86 `CnodeRevoke`, gen 42b62): `capRevokeFGA Dl`; at `capDelete` it is + `capRevokeF`, at `fl4CapDelete` `fl4CapRevokeF`, by `rfl`. Steps `capRevokeGA_step` / `_del_preempt` / + `_pp_preempt` / `_stop`, and `capRevokeGA_none` (no children: the single outcome); `pp_okA` (the preemption + point can return without preempting, the state unchanged). +* **`decode_cnode_invocation` at labels 21–23** (x86 `CnodeSpec.copyTail`'s three arms, in `CnodeSpecA`'s style): + `decode_revokeA` / `decode_deleteA` / `decode_cancelA` (the decode after the prefix, as the target lookup bound to + the arm), their runs (`decode_*_runA`), `cancelArm_runA` (the rights check), and `invokeCnode` / `fl4InvokeCnode` + at the three invocations. +* **`cancel_badged_sends`** (x86 `IpcCancelBadged`'s spec steps): `cancelBadgedSends_sendA` / `_otherA` (the queue + walk, or nothing), the `mapM_x` steps (`mapMx_nil_memA`, `mapMx_cons_memA`), `removeAndRestartBadgedThread_matchA` / + `_skipA`, `restartThreadIfNoFault_okA` / `_faultA`. +* **`fl4FinaliseCap` dispatch** (`Ext/Finalise`): `extKindOfAsid_eqA` and its cases (`.high` / `.stage2` stay, + `.stage1` / no root give `none`); a mapped plain frame → `extUnmapPage .high` (a high window), nothing (a + Stage-2 root) or l4v's `unmap_page`, a mapped Stage-2 frame cap → `extUnmapPage .stage2` (ExtVsA gen 3); a mapped + normal page table → `extUnmapTable` (final, FL4 root) or l4v's `unmap_page_table`; an assigned FL4 root → + `delete_asid`; VCPU / thread caps → FL4's hook then l4v's; every other cap → l4v's `finalise_cap` + (`fl4FinaliseCap_plainA`). + +No named premise ([J] / [P] / [W]): every statement is about the spec alone. New module. +-/ +import Fl4SpecA64 +import Mathlib.Data.Set.Lattice.Bounded +import Mathlib.Data.Set.Lattice.Disjoint +import Mathlib.Data.Set.Lattice.Image +import Mathlib.Data.Set.Lattice.Indexed +import Mathlib.Data.Set.Lattice.Order + +namespace Fl4ProofsA64.P3 +open Classical Fl4Spec Fl4SpecA64 + +noncomputable section + +/-! ## Spec monad steps -/ + +section +variable {σ ε α β : Type} + +theorem nd_bind_applyD (m : NonDet σ α) (f : α → NonDet σ β) (s : σ) : + (m >>= f) s = ({r | ∃ x ∈ (m s).1, r ∈ (f x.1 x.2).1}, (m s).2 ∨ ∃ x ∈ (m s).1, (f x.1 x.2).2) := rfl + +/-- A bind whose first computation has the single outcome `(a, s')`. -/ +theorem nd_bind_oneD {m : NonDet σ α} {s s' : σ} {a : α} (h : m s = ({(a, s')}, False)) (f : α → NonDet σ β) : + (m >>= f) s = f a s' := by + simp only [nd_bind_applyD, h, Set.mem_singleton_iff, exists_eq_left, false_or] + ext <;> simp + +theorem nd_pure_bindD (a : α) (f : α → NonDet σ β) : (pure a >>= f) = f a := by + funext s; exact nd_bind_oneD (m := (pure a : NonDet σ α)) (s := s) (s' := s) rfl f + +theorem ndE_pure_bindD (a : α) (f : α → NonDetE σ ε β) : (pure a >>= f) = f a := by + show ExceptT.mk (pure (Except.ok a) >>= ExceptT.bindCont f) = f a + rw [nd_pure_bindD]; rfl + +theorem ndE_throw_bindD (e : ε) (f : α → NonDetE σ ε β) : (throw e >>= f) = throw e := by + show ExceptT.mk (pure (Except.error e) >>= ExceptT.bindCont f) = ExceptT.mk (pure (Except.error e)) + rw [nd_pure_bindD]; rfl + +theorem run_bind_okD {m : NonDetE σ ε α} {s s' : σ} {a : α} (h : m.run s = ({(.ok a, s')}, False)) + (f : α → NonDetE σ ε β) : (m >>= f).run s = (f a).run s' := by + rw [ExceptT.run_bind, nd_bind_oneD h] + +theorem run_bind_errorD {m : NonDetE σ ε α} {s s' : σ} {e : ε} (h : m.run s = ({(.error e, s')}, False)) + (f : α → NonDetE σ ε β) : (m >>= f).run s = ({(.error e, s')}, False) := by + rw [ExceptT.run_bind, nd_bind_oneD h]; rfl + +theorem liftE_runD (m : NonDet σ α) : (NonDetE.liftE (ε := ε) m).run = (m >>= fun a => NonDet.pure (.ok a)) := by + unfold NonDetE.liftE; rw [ExceptT.run_lift]; rfl + +theorem run_liftE_stD {m : NonDet σ α} {s s' : σ} {a : α} (h : m s = ({(a, s')}, False)) : + (NonDetE.liftE (ε := ε) m).run s = ({(.ok a, s')}, False) := by + rw [liftE_runD, nd_bind_oneD h]; rfl + +theorem run_returnOkD (a : α) (s : σ) : (NonDetE.returnOk a : NonDetE σ ε α).run s = ({(.ok a, s)}, False) := rfl + +theorem run_throwErrorD (e : ε) (s : σ) : + (NonDetE.throwError e : NonDetE σ ε α).run s = ({(.error e, s)}, False) := rfl + +theorem mem_bind_ndD {m : NonDet σ α} {f : α → NonDet σ β} {s : σ} {x : α × σ} {y : β × σ} + (hx : x ∈ (m s).1) (hy : y ∈ (f x.1 x.2).1) : y ∈ ((m >>= f) s).1 := ⟨x, hx, hy⟩ + +theorem mem_bindE_okD {m : NonDetE σ ε α} {f : α → NonDetE σ ε β} {s s' : σ} {a : α} + {y : Except ε β × σ} (hx : (.ok a, s') ∈ (m.run s).1) (hy : y ∈ ((f a).run s').1) : + y ∈ ((m >>= f).run s).1 := by + rw [ExceptT.run_bind]; exact ⟨(.ok a, s'), hx, hy⟩ + +theorem mem_bindE_errD {m : NonDetE σ ε α} {f : α → NonDetE σ ε β} {s s' : σ} {e : ε} + (hx : (.error e, s') ∈ (m.run s).1) : (.error e, s') ∈ ((m >>= f).run s).1 := by + rw [ExceptT.run_bind]; exact ⟨(.error e, s'), hx, rfl⟩ + +theorem mem_of_runD {m : NonDetE σ ε α} {s : σ} {y : Except ε α × σ} (h : m.run s = ({y}, False)) : + y ∈ (m.run s).1 := by rw [h]; rfl + +/-- A lifted computation's outcome. -/ +theorem mem_liftED {m : NonDet σ α} {s s' : σ} {a : α} (h : (a, s') ∈ (m s).1) : + (.ok a, s') ∈ ((NonDetE.liftE (ε := ε) m).run s).1 := by + rw [liftE_runD]; exact mem_bind_ndD h rfl + +end + +/-! ## Least fixed points -/ + +section +variable {σ α ι : Type} + +/-- Every pre-fixed point bounds the least fixed point. -/ +theorem lfp_le_prefixedA (F : (ι → NonDet σ α) → ι → NonDet σ α) (f : ι → NonDet σ α) + (hf : ∀ k t, F f k t ≤ f k t) (i : ι) (s : σ) : NonDet.lfp F i s ≤ f i s := by + let S := {f : ι → σ → Set (α × σ) × Prop | ∀ k t, F f k t ≤ f k t} + have hl : NonDet.lfp F = (sInf S : ι → σ → Set (α × σ) × Prop) := rfl + let f' : ι → σ → Set (α × σ) × Prop := f + have hfS : f' ∈ S := hf + rw [hl] + exact (sInf_le hfS) i s + +/-- An outcome of `F f` at `(i, s)` for every pre-fixed point `f` is an +outcome of the least fixed point. -/ +theorem lfp_memA (F : (ι → NonDet σ α) → ι → NonDet σ α) {i : ι} {s : σ} {x : α × σ} + (h : ∀ f : ι → NonDet σ α, (∀ k t, F f k t ≤ f k t) → x ∈ (F f i s).1) : + x ∈ (NonDet.lfp F i s).1 := by + let S := {f : ι → σ → Set (α × σ) × Prop | ∀ k t, F f k t ≤ f k t} + have hl : NonDet.lfp F = (sInf S : ι → σ → Set (α × σ) × Prop) := rfl + let g : ι → σ → Set (α × σ) × Prop := fun k t => if k = i ∧ t = s then ({x}, False) else ⊥ + have hlow : g ≤ (sInf S : ι → σ → Set (α × σ) × Prop) := by + apply le_sInf + intro f hf k t + by_cases h1 : k = i ∧ t = s + · obtain ⟨rfl, rfl⟩ := h1 + simp only [g, and_self, ↓reduceIte] + refine ⟨?_, fun h => h.elim⟩ + intro y hy + rw [Set.mem_singleton_iff.1 hy] + exact (hf k t).1 (h f hf) + · simp only [g, h1, ↓reduceIte]; exact bot_le + rw [hl] + have := (hlow i s).1 + simp only [g, and_self, ↓reduceIte] at this + exact this rfl + +/-- The least fixed point at a point where `F` does not look at the +recursion. -/ +theorem lfp_oneA (F : (ι → NonDet σ α) → ι → NonDet σ α) (j : ι) (s : σ) + (X : Set (α × σ) × Prop) (hj : ∀ f, F f j s = X) : NonDet.lfp F j s = X := by + let S := {f : ι → σ → Set (α × σ) × Prop | ∀ k t, F f k t ≤ f k t} + have hl : NonDet.lfp F = (sInf S : ι → σ → Set (α × σ) × Prop) := rfl + let fs : ι → NonDet σ α := fun k t => if k = j ∧ t = s then X else ⊤ + have hfsj : fs j s = X := by simp [fs] + have hfs : (fs : ι → σ → Set (α × σ) × Prop) ∈ S := by + intro k t + by_cases h1 : k = j ∧ t = s + · obtain ⟨rfl, rfl⟩ := h1; exact (hj fs).le.trans hfsj.ge + · show F fs k t ≤ (if k = j ∧ t = s then X else ⊤) + simp only [h1, ↓reduceIte]; exact le_top + have hup : (sInf S : ι → σ → Set (α × σ) × Prop) ≤ fs := sInf_le hfs + let g : ι → σ → Set (α × σ) × Prop := fun k t => if k = j ∧ t = s then X else ⊥ + have hlow : g ≤ (sInf S : ι → σ → Set (α × σ) × Prop) := by + apply le_sInf + intro f hf k t + by_cases h1 : k = j ∧ t = s + · obtain ⟨rfl, rfl⟩ := h1; simp only [g, and_self, ↓reduceIte]; exact (hj f).ge.trans (hf _ _) + · simp only [g, h1, ↓reduceIte]; exact bot_le + rw [hl] + apply le_antisymm + · exact (hup j s).trans hfsj.le + · have := hlow j s; simpa [g] using this + +end + +/-! ## `rec_del` over a spec finaliser -/ + +/-- A spec finaliser (`finalise_cap`'s type). -/ +abbrev FinFnA := Cap → Bool → SMonad Unit (Cap × Cap) + +/-- l4v `CSpace_A.rec_del`'s equations (`recDelF`) with the finaliser `Fn` for `finalise_cap` (x86 `recDelFG`). -/ +def recDelFGA (Fn : FinFnA) (recDel : RecDelCall → PMonad Unit (Bool × Cap)) : RecDelCall → PMonad Unit (Bool × Cap) + | .cTEDeleteCall slot exposed => do + let (success, cleanupInfo) ← recDel (.finaliseSlotCall slot exposed) + withoutPreemption <| NonDet.when (exposed ∨ success) <| emptySlot slot cleanupInfo + NonDetE.returnOk undefined + | .finaliseSlotCall slot exposed => do + let cap ← withoutPreemption <| getCap slot + if cap = .nullCap + then NonDetE.returnOk (true, .nullCap) + else do + let isFinal ← withoutPreemption <| isFinalCap cap + let (remainder, cleanupInfo) ← withoutPreemption <| Fn cap isFinal + if capRemoveable remainder slot + then NonDetE.returnOk (true, cleanupInfo) + else if capCyclicZombie remainder slot ∧ ¬exposed + then do + withoutPreemption <| setCap remainder slot + NonDetE.returnOk (false, .nullCap) + else do + withoutPreemption <| setCap remainder slot + let _ ← recDel (.reduceZombieCall remainder slot exposed) + preemptionPoint + recDel (.finaliseSlotCall slot exposed) + | .reduceZombieCall (.zombie ptr bits (n + 1)) slot false => do + let cn ← NonDetE.returnOk (firstCslotOf (.zombie ptr bits (n + 1))) + NonDetE.assertE (cn ≠ slot) + withoutPreemption <| capSwapForDelete cn slot + NonDetE.returnOk undefined + | .reduceZombieCall (.zombie ptr bits (n + 1)) slot true => do + let endSlot ← NonDetE.returnOk (ptr, natToCref (zombieCteBits bits) n) + let _ ← recDel (.cTEDeleteCall endSlot false) + let newCap ← withoutPreemption <| getCap slot + if newCap = .zombie ptr bits (n + 1) + then withoutPreemption <| setCap (.zombie ptr bits n) slot + else NonDetE.assertE (newCap = .nullCap ∨ + isZombie newCap ∧ firstCslotOf newCap = slot + ∧ firstCslotOf (.zombie ptr bits (n + 1)) ≠ slot) + NonDetE.returnOk undefined + | .reduceZombieCall _ _ _ => NonDetE.liftE NonDet.fail + +/-- `recDel` with the finaliser `Fn` (the least fixed point of `recDelFGA Fn`). -/ +def recDelGA (Fn : FinFnA) : RecDelCall → PMonad Unit (Bool × Cap) := + NonDet.lfp (σ := State Unit) (α := Except Unit (Bool × Cap)) fun r c => recDelFGA Fn r c + +/-- `capDelete` with the finaliser `Fn`. -/ +def capDeleteGA (Fn : FinFnA) (slot : CslotPtr) : PMonad Unit Unit := do + let _ ← recDelGA Fn (.cTEDeleteCall slot true) + NonDetE.returnOk () + +theorem recDelFGA_cap : recDelFGA finaliseCap = recDelF (Z := Unit) := rfl +theorem recDelGA_cap : recDelGA finaliseCap = recDel (Z := Unit) := rfl +theorem capDeleteGA_cap : capDeleteGA finaliseCap = capDelete (Z := Unit) := rfl + +/-- At FL4's finaliser, `recDelFGA` is `Ext/Finalise`'s `fl4RecDelF`. -/ +theorem recDelFGA_fl4 : recDelFGA fl4FinaliseCap = fl4RecDelF (Z := Unit) := rfl +theorem recDelGA_fl4 : recDelGA fl4FinaliseCap = fl4RecDel (Z := Unit) := rfl +theorem capDeleteGA_fl4 : capDeleteGA fl4FinaliseCap = fl4CapDelete (Z := Unit) := rfl + +/-- l4v `finalise_slot` is the `FinaliseSlotCall` of `rec_del`. -/ +theorem finaliseSlot_eqA (p : CslotPtr) (e : Bool) : + finaliseSlot (Z := Unit) p e = recDelGA finaliseCap (.finaliseSlotCall p e) := rfl + +/-- An outcome of a `recDelGA Fn` call. -/ +abbrev RDGA (Fn : FinFnA) (c : RecDelCall) (σ : State Unit) (x : Except Unit (Bool × Cap) × State Unit) : Prop := + x ∈ ((recDelGA Fn c).run σ).1 + +abbrev RDFGA (Fn : FinFnA) := + fun (r : RecDelCall → NonDet (State Unit) (Except Unit (Bool × Cap))) (c : RecDelCall) => recDelFGA Fn r c + +theorem recDelGA_run (Fn : FinFnA) (c : RecDelCall) (σ : State Unit) : + (recDelGA Fn c).run σ = NonDet.lfp (RDFGA Fn) c σ := rfl + +/-- Lift an outcome of `recDelFGA Fn f` for every pre-fixed point `f`, given +the recursive outcomes, to an outcome of `recDelGA Fn` (x86 `rd_of_FG`). -/ +theorem rd_of_FGA {Fn : FinFnA} {c : RecDelCall} {σ : State Unit} {x : Except Unit (Bool × Cap) × State Unit} + (h : ∀ f : RecDelCall → NonDet (State Unit) (Except Unit (Bool × Cap)), + (∀ k t, RDFGA Fn f k t ≤ f k t) → (∀ c' σ' y, RDGA Fn c' σ' y → y ∈ (f c' σ').1) → + x ∈ ((recDelFGA Fn f c).run σ).1) : RDGA Fn c σ x := by + show x ∈ (NonDet.lfp (RDFGA Fn) c σ).1 + exact lfp_memA _ fun f hf => h f hf fun c' σ' y hy => (lfp_le_prefixedA _ f hf c' σ').1 hy + +/-- Every outcome of `recDelGA Fn` satisfies `P` when `P` is closed under one +step (an outcome of `recDelFGA Fn f` for `f` whose outcomes satisfy `P`). -/ +theorem rd_inductGA {Fn : FinFnA} (P : RecDelCall → State Unit → Except Unit (Bool × Cap) × State Unit → Prop) + (hstep : ∀ c σ y, y ∈ ((recDelFGA Fn (fun c' σ' => ({z | P c' σ' z}, True)) c).run σ).1 → P c σ y) + {c : RecDelCall} {σ : State Unit} {y : Except Unit (Bool × Cap) × State Unit} (hy : RDGA Fn c σ y) : P c σ y := by + let f : RecDelCall → NonDet (State Unit) (Except Unit (Bool × Cap)) := fun c' σ' => ({z | P c' σ' z}, True) + have hf : ∀ k t, RDFGA Fn f k t ≤ f k t := fun k t => ⟨fun z hz => hstep k t z hz, fun _ => trivial⟩ + exact (lfp_le_prefixedA _ f hf c σ).1 hy + +/-! ### `CTEDeleteCall` -/ + +theorem rd_cte_okGA {Fn : FinFnA} {sl : CslotPtr} {e : Bool} {σ σ1 σ2 : State Unit} {succ : Bool} {cl : Cap} + (hf : RDGA Fn (.finaliseSlotCall sl e) σ (.ok (succ, cl), σ1)) + (he : ((), σ2) ∈ ((NonDet.when (e = true ∨ succ = true) (emptySlot (Z := Unit) sl cl)) σ1).1) : + ∃ v, RDGA Fn (.cTEDeleteCall sl e) σ (.ok v, σ2) := by + refine ⟨undefined, rd_of_FGA fun f _ hl => ?_⟩ + unfold recDelFGA withoutPreemption + refine mem_bindE_okD (hl _ _ _ hf) ?_ + show (Except.ok undefined, σ2) ∈ ((NonDetE.liftE _ >>= fun _ => NonDetE.returnOk undefined).run σ1).1 + exact mem_bindE_okD (s' := σ2) (a := ()) (mem_liftED he) (mem_of_runD (run_returnOkD _ σ2)) + +theorem rd_cte_errGA {Fn : FinFnA} {sl : CslotPtr} {e : Bool} {σ σ1 : State Unit} + (hf : RDGA Fn (.finaliseSlotCall sl e) σ (.error (), σ1)) : RDGA Fn (.cTEDeleteCall sl e) σ (.error (), σ1) := by + refine rd_of_FGA fun f _ hl => ?_ + unfold recDelFGA + exact mem_bindE_errD (hl _ _ _ hf) + +/-- `cap_delete` from `CTEDeleteCall slot True`. -/ +theorem capDeleteGA_ok {Fn : FinFnA} {sl : CslotPtr} {σ σ' : State Unit} {v : Bool × Cap} + (h : RDGA Fn (.cTEDeleteCall sl true) σ (.ok v, σ')) : + (.ok (), σ') ∈ ((capDeleteGA Fn sl).run σ).1 := by + unfold capDeleteGA + exact mem_bindE_okD h (mem_of_runD (run_returnOkD _ σ')) + +theorem capDeleteGA_err {Fn : FinFnA} {sl : CslotPtr} {σ σ' : State Unit} + (h : RDGA Fn (.cTEDeleteCall sl true) σ (.error (), σ')) : + (.error (), σ') ∈ ((capDeleteGA Fn sl).run σ).1 := by + unfold capDeleteGA + exact mem_bindE_errD h + +/-! ### `FinaliseSlotCall` -/ + +theorem rd_fin_nullGA {Fn : FinFnA} {sl : CslotPtr} {e : Bool} {σ : State Unit} + (hg : getCap (Z := Unit) sl σ = ({(.nullCap, σ)}, False)) : + RDGA Fn (.finaliseSlotCall sl e) σ (.ok (true, .nullCap), σ) := by + refine rd_of_FGA fun f _ _ => ?_ + unfold recDelFGA withoutPreemption + refine mem_bindE_okD (mem_of_runD (run_liftE_stD hg)) ?_ + simp only [↓reduceIte] + exact mem_of_runD (run_returnOkD _ σ) + +/-- The common prefix of a round on a non-null cap: read it, its finality, +finalise it (x86 `fin_prefixG`). -/ +theorem fin_prefixGA {Fn : FinFnA} {f : RecDelCall → NonDet (State Unit) (Except Unit (Bool × Cap))} {sl : CslotPtr} + {e : Bool} {σ σ1 : State Unit} {c rem cl : Cap} (hg : getCap (Z := Unit) sl σ = ({(c, σ)}, False)) + (hc : c ≠ .nullCap) (hfc : ((rem, cl), σ1) ∈ (Fn c (decide (isFinalCap' c σ)) σ).1) + {y : Except Unit (Bool × Cap) × State Unit} + (hy : y ∈ ((if capRemoveable rem sl then NonDetE.returnOk (true, cl) + else if capCyclicZombie rem sl ∧ ¬ e = true then do + withoutPreemption <| setCap rem sl + NonDetE.returnOk (false, .nullCap) + else do + withoutPreemption <| setCap rem sl + let _ ← f (.reduceZombieCall rem sl e) + preemptionPoint + f (.finaliseSlotCall sl e) : PMonad Unit (Bool × Cap)).run σ1).1) : + y ∈ ((recDelFGA Fn f (.finaliseSlotCall sl e)).run σ).1 := by + unfold recDelFGA withoutPreemption + refine mem_bindE_okD (mem_of_runD (run_liftE_stD hg)) ?_ + simp only [hc, ↓reduceIte] + refine mem_bindE_okD (mem_of_runD (run_liftE_stD (m := isFinalCap (Z := Unit) c) rfl)) ?_ + exact mem_bindE_okD (a := (rem, cl)) (s' := σ1) (mem_liftED hfc) hy + +theorem rd_fin_removableGA {Fn : FinFnA} {sl : CslotPtr} {e : Bool} {σ σ1 : State Unit} {c rem cl : Cap} + (hg : getCap (Z := Unit) sl σ = ({(c, σ)}, False)) (hc : c ≠ .nullCap) + (hfc : ((rem, cl), σ1) ∈ (Fn c (decide (isFinalCap' c σ)) σ).1) + (hr : capRemoveable rem sl) : + RDGA Fn (.finaliseSlotCall sl e) σ (.ok (true, cl), σ1) := by + refine rd_of_FGA fun f _ _ => fin_prefixGA hg hc hfc ?_ + rw [if_pos hr] + exact mem_of_runD (run_returnOkD _ σ1) + +theorem rd_fin_cyclicGA {Fn : FinFnA} {sl : CslotPtr} {σ σ1 σ2 : State Unit} {c rem cl : Cap} + (hg : getCap (Z := Unit) sl σ = ({(c, σ)}, False)) (hc : c ≠ .nullCap) + (hfc : ((rem, cl), σ1) ∈ (Fn c (decide (isFinalCap' c σ)) σ).1) + (hr : ¬ capRemoveable rem sl) (hcy : capCyclicZombie rem sl) + (hs : setCap (Z := Unit) rem sl σ1 = ({((), σ2)}, False)) : + RDGA Fn (.finaliseSlotCall sl false) σ (.ok (false, .nullCap), σ2) := by + refine rd_of_FGA fun f _ _ => fin_prefixGA hg hc hfc ?_ + rw [if_neg hr, if_pos ⟨hcy, by decide⟩] + exact mem_bindE_okD (mem_of_runD (run_liftE_stD hs)) (mem_of_runD (run_returnOkD _ σ2)) + +/-- The reducing round: set the remainder, reduce the zombie, preemption +point, and then the outcome `y` of the next round. -/ +theorem rd_fin_reduceGA {Fn : FinFnA} {sl : CslotPtr} {e : Bool} {σ σ1 σ2 σ3 σ4 : State Unit} {c rem cl : Cap} + (hg : getCap (Z := Unit) sl σ = ({(c, σ)}, False)) (hc : c ≠ .nullCap) + (hfc : ((rem, cl), σ1) ∈ (Fn c (decide (isFinalCap' c σ)) σ).1) + (hr : ¬ capRemoveable rem sl) (hcy : ¬ (capCyclicZombie rem sl ∧ e = false)) + (hs : setCap (Z := Unit) rem sl σ1 = ({((), σ2)}, False)) {v : Bool × Cap} + (hz : RDGA Fn (.reduceZombieCall rem sl e) σ2 (.ok v, σ3)) + (hp : (.ok (), σ4) ∈ ((preemptionPoint (Z := Unit)).run σ3).1) {y : Except Unit (Bool × Cap) × State Unit} + (hy : RDGA Fn (.finaliseSlotCall sl e) σ4 y) : + RDGA Fn (.finaliseSlotCall sl e) σ y := by + refine rd_of_FGA fun f _ hl => fin_prefixGA hg hc hfc ?_ + rw [if_neg hr, if_neg (fun h => hcy ⟨h.1, by simpa using h.2⟩)] + refine mem_bindE_okD (mem_of_runD (run_liftE_stD hs)) ?_ + exact mem_bindE_okD (hl _ _ _ hz) (mem_bindE_okD hp (hl _ _ _ hy)) + +theorem rd_fin_reduce_errGA {Fn : FinFnA} {sl : CslotPtr} {e : Bool} {σ σ1 σ2 σ3 : State Unit} {c rem cl : Cap} + (hg : getCap (Z := Unit) sl σ = ({(c, σ)}, False)) (hc : c ≠ .nullCap) + (hfc : ((rem, cl), σ1) ∈ (Fn c (decide (isFinalCap' c σ)) σ).1) + (hr : ¬ capRemoveable rem sl) (hcy : ¬ (capCyclicZombie rem sl ∧ e = false)) + (hs : setCap (Z := Unit) rem sl σ1 = ({((), σ2)}, False)) + (hz : RDGA Fn (.reduceZombieCall rem sl e) σ2 (.error (), σ3)) : + RDGA Fn (.finaliseSlotCall sl e) σ (.error (), σ3) := by + refine rd_of_FGA fun f _ hl => fin_prefixGA hg hc hfc ?_ + rw [if_neg hr, if_neg (fun h => hcy ⟨h.1, by simpa using h.2⟩)] + refine mem_bindE_okD (mem_of_runD (run_liftE_stD hs)) ?_ + exact mem_bindE_errD (hl _ _ _ hz) + +theorem rd_fin_pp_errGA {Fn : FinFnA} {sl : CslotPtr} {e : Bool} {σ σ1 σ2 σ3 σ4 : State Unit} {c rem cl : Cap} + (hg : getCap (Z := Unit) sl σ = ({(c, σ)}, False)) (hc : c ≠ .nullCap) + (hfc : ((rem, cl), σ1) ∈ (Fn c (decide (isFinalCap' c σ)) σ).1) + (hr : ¬ capRemoveable rem sl) (hcy : ¬ (capCyclicZombie rem sl ∧ e = false)) + (hs : setCap (Z := Unit) rem sl σ1 = ({((), σ2)}, False)) {v : Bool × Cap} + (hz : RDGA Fn (.reduceZombieCall rem sl e) σ2 (.ok v, σ3)) + (hp : (.error (), σ4) ∈ ((preemptionPoint (Z := Unit)).run σ3).1) : + RDGA Fn (.finaliseSlotCall sl e) σ (.error (), σ4) := by + refine rd_of_FGA fun f _ hl => fin_prefixGA hg hc hfc ?_ + rw [if_neg hr, if_neg (fun h => hcy ⟨h.1, by simpa using h.2⟩)] + refine mem_bindE_okD (mem_of_runD (run_liftE_stD hs)) ?_ + exact mem_bindE_okD (hl _ _ _ hz) (mem_bindE_errD hp) + +/-! ### `ReduceZombieCall` -/ + +/-- Not exposed: swap the zombie into its first slot. -/ +theorem rd_reduce_falseGA {Fn : FinFnA} {ptr : ObjRef} {bits : Option Nat} {n : Nat} {sl : CslotPtr} + {σ σ' : State Unit} (hne : firstCslotOf (.zombie ptr bits (n + 1)) ≠ sl) + (hsw : ((), σ') ∈ (capSwapForDelete (Z := Unit) (firstCslotOf (.zombie ptr bits (n + 1))) sl σ).1) : + ∃ v, RDGA Fn (.reduceZombieCall (.zombie ptr bits (n + 1)) sl false) σ (.ok v, σ') := by + refine ⟨undefined, rd_of_FGA fun f _ _ => ?_⟩ + unfold recDelFGA withoutPreemption + refine mem_bindE_okD (mem_of_runD (run_returnOkD _ σ)) ?_ + refine mem_bindE_okD (a := ()) (s' := σ) ?_ ?_ + · rw [NonDetE.assertE]; exact mem_liftED (m := NonDet.assert _) ⟨hne, rfl⟩ + exact mem_bindE_okD (a := ()) (s' := σ') (mem_liftED hsw) (mem_of_runD (run_returnOkD _ σ')) + +/-- Exposed, the zombie's end slot deleted, the cap in the slot unchanged: +decrement the zombie. -/ +theorem rd_reduce_true_decGA {Fn : FinFnA} {ptr : ObjRef} {bits : Option Nat} {n : Nat} {sl : CslotPtr} + {σ σ1 σ2 : State Unit} {v : Bool × Cap} + (hd : RDGA Fn (.cTEDeleteCall (ptr, natToCref (zombieCteBits bits) n) false) σ (.ok v, σ1)) + (hg : getCap (Z := Unit) sl σ1 = ({(.zombie ptr bits (n + 1), σ1)}, False)) + (hs : setCap (Z := Unit) (.zombie ptr bits n) sl σ1 = ({((), σ2)}, False)) : + ∃ v, RDGA Fn (.reduceZombieCall (.zombie ptr bits (n + 1)) sl true) σ (.ok v, σ2) := by + refine ⟨undefined, rd_of_FGA fun f _ hl => ?_⟩ + unfold recDelFGA withoutPreemption + refine mem_bindE_okD (mem_of_runD (run_returnOkD _ σ)) ?_ + refine mem_bindE_okD (hl _ _ _ hd) ?_ + refine mem_bindE_okD (mem_of_runD (run_liftE_stD hg)) ?_ + simp only [↓reduceIte] + exact mem_bindE_okD (mem_of_runD (run_liftE_stD hs)) (mem_of_runD (run_returnOkD _ σ2)) + +/-- Exposed, the cap in the slot changed: the spec's assertion. -/ +theorem rd_reduce_true_chgGA {Fn : FinFnA} {ptr : ObjRef} {bits : Option Nat} {n : Nat} {sl : CslotPtr} + {σ σ1 : State Unit} {v : Bool × Cap} {nc : Cap} + (hd : RDGA Fn (.cTEDeleteCall (ptr, natToCref (zombieCteBits bits) n) false) σ (.ok v, σ1)) + (hg : getCap (Z := Unit) sl σ1 = ({(nc, σ1)}, False)) (hnc : nc ≠ .zombie ptr bits (n + 1)) + (ha : nc = .nullCap ∨ isZombie nc ∧ firstCslotOf nc = sl ∧ firstCslotOf (.zombie ptr bits (n + 1)) ≠ sl) : + ∃ v, RDGA Fn (.reduceZombieCall (.zombie ptr bits (n + 1)) sl true) σ (.ok v, σ1) := by + refine ⟨undefined, rd_of_FGA fun f _ hl => ?_⟩ + unfold recDelFGA withoutPreemption + refine mem_bindE_okD (mem_of_runD (run_returnOkD _ σ)) ?_ + refine mem_bindE_okD (hl _ _ _ hd) ?_ + refine mem_bindE_okD (mem_of_runD (run_liftE_stD hg)) ?_ + simp only [hnc, ↓reduceIte] + refine mem_bindE_okD (a := ()) (s' := σ1) ?_ (mem_of_runD (run_returnOkD _ σ1)) + rw [NonDetE.assertE]; exact mem_liftED (m := NonDet.assert _) ⟨ha, rfl⟩ + +theorem rd_reduce_true_errGA {Fn : FinFnA} {ptr : ObjRef} {bits : Option Nat} {n : Nat} {sl : CslotPtr} + {σ σ1 : State Unit} + (hd : RDGA Fn (.cTEDeleteCall (ptr, natToCref (zombieCteBits bits) n) false) σ (.error (), σ1)) : + RDGA Fn (.reduceZombieCall (.zombie ptr bits (n + 1)) sl true) σ (.error (), σ1) := by + refine rd_of_FGA fun f _ hl => ?_ + unfold recDelFGA withoutPreemption + refine mem_bindE_okD (mem_of_runD (run_returnOkD _ σ)) ?_ + exact mem_bindE_errD (hl _ _ _ hd) + +/-! ## The preemption point -/ + +/-- **The spec's preemption point can return without preempting**, the state +unchanged (for `Z = Unit`, `work_units_limit_reached` is any boolean and the +extended operations do nothing; x86 `UntypedResetSpec.pp_ok`). -/ +theorem pp_okA (σ : State Unit) : (.ok (), σ) ∈ ((preemptionPoint (Z := Unit)).run σ).1 := by + unfold preemptionPoint + refine mem_bindE_okD (a := ()) (s' := σ) (mem_liftED ?_) ?_ + · unfold doExtendedOp + exact mem_bind_ndD (x := (σ, σ)) rfl (mem_bind_ndD (x := (((), σ), σ)) ⟨rfl, rfl⟩ rfl) + unfold orChoiceE + refine mem_bindE_okD (a := σ) (s' := σ) (mem_liftED rfl) ?_ + refine mem_bindE_okD (a := (false, σ)) (s' := σ) (mem_liftED ⟨⟨trivial, rfl⟩, rfl⟩) ?_ + exact mem_of_runD (run_returnOkD _ σ) + +/-! ## `cap_revoke` over a spec deletion -/ + +theorem selectExt_memA {D : Type} (a : State DetExt → D) {A : Set D} {x : D} (hx : x ∈ A) (σ : State Unit) : + (x, σ) ∈ (selectExt (Z := Unit) a A σ).1 := by + unfold selectExt + refine mem_bind_ndD (x := (σ, σ)) rfl ?_ + refine mem_bind_ndD (x := (x, σ)) ⟨hx, rfl⟩ ?_ + refine mem_bind_ndD (x := ((), σ)) ⟨hx, rfl⟩ ?_ + rfl + +theorem descendantsOf_empty_iffA (p : CslotPtr) (t : Cdt) : + descendantsOf p t = ∅ ↔ ¬ ∃ c, t c = some p := by + constructor + · rintro h ⟨c, hc⟩ + have : c ∈ descendantsOf p t := Relation.TransGen.single hc + rw [h] at this; exact this + · intro h + apply Set.eq_empty_iff_forall_notMem.2 + intro q hq + obtain ⟨b, hb, -⟩ := Relation.TransGen.head'_iff.1 hq + exact h ⟨b, hb⟩ + +/-- l4v `cap_revoke`'s equation (`capRevokeF`) with the deletion `Dl` for +`cap_delete` (x86 `capRevokeFG`). -/ +def capRevokeFGA (Dl : CslotPtr → PMonad Unit Unit) (capRevoke : CslotPtr → PMonad Unit Unit) (slot : CslotPtr) : + PMonad Unit Unit := do + let cap ← withoutPreemption <| getCap slot + let cdt ← withoutPreemption <| NonDet.gets fun s : State Unit => s.cdt + let descendants ← NonDetE.returnOk (descendantsOf slot cdt) + NonDetE.whenE (cap ≠ .nullCap ∧ descendants ≠ ∅) (do + let child ← withoutPreemption <| selectExt (nextRevokeCap slot) descendants + let cap ← withoutPreemption <| getCap child + NonDetE.assertE (cap ≠ .nullCap) + Dl child + preemptionPoint + capRevoke slot) + +/-- `capRevoke` over the deletion `Dl` (the least fixed point of `capRevokeFGA Dl`). -/ +def capRevokeGA (Dl : CslotPtr → PMonad Unit Unit) : CslotPtr → PMonad Unit Unit := + NonDet.lfp (σ := State Unit) (α := Except Unit Unit) fun r c => capRevokeFGA Dl r c + +theorem capRevokeFGA_cap : capRevokeFGA capDelete = capRevokeF (Z := Unit) := rfl +theorem capRevokeGA_cap : capRevokeGA capDelete = capRevoke (Z := Unit) := rfl +/-- At FL4's deletion, `capRevokeFGA` is `Ext/Finalise`'s `fl4CapRevokeF`. -/ +theorem capRevokeFGA_fl4 : capRevokeFGA fl4CapDelete = fl4CapRevokeF (Z := Unit) := rfl +theorem capRevokeGA_fl4 : capRevokeGA fl4CapDelete = fl4CapRevoke (Z := Unit) := rfl + +theorem capRevokeGA_run (Dl : CslotPtr → PMonad Unit Unit) (sl : CslotPtr) (σ : State Unit) : + (capRevokeGA Dl sl).run σ = NonDet.lfp (fun r c => capRevokeFGA Dl r c) sl σ := rfl + +/-- The prefix of a round: the cap and the CDT read, the descendants. -/ +theorem capRevokeFGA_pre (Dl : CslotPtr → PMonad Unit Unit) (f : CslotPtr → NonDet (State Unit) (Except Unit Unit)) + (sl : CslotPtr) (σ : State Unit) {a : Cap} (hg : getCap (Z := Unit) sl σ = ({(a, σ)}, False)) : + (capRevokeFGA Dl f sl).run σ = (NonDetE.whenE (a ≠ .nullCap ∧ descendantsOf sl σ.cdt ≠ ∅) (do + let child ← withoutPreemption <| selectExt (nextRevokeCap sl) (descendantsOf sl σ.cdt) + let cap ← withoutPreemption <| getCap child + NonDetE.assertE (cap ≠ .nullCap) + Dl child + preemptionPoint + f sl) : PMonad Unit Unit).run σ := by + unfold capRevokeFGA withoutPreemption + rw [run_bind_okD (run_liftE_stD hg)] + rw [run_bind_okD (run_liftE_stD (m := NonDet.gets fun s : State Unit => s.cdt) rfl)] + rw [run_bind_okD (run_returnOkD _ σ)] + +/-- The stop case: a null cap, or no children (x86 `capRevokeFG_stop`). -/ +theorem capRevokeFGA_stop (Dl : CslotPtr → PMonad Unit Unit) (f : CslotPtr → NonDet (State Unit) (Except Unit Unit)) + (sl : CslotPtr) (σ : State Unit) {a : Cap} (hg : getCap (Z := Unit) sl σ = ({(a, σ)}, False)) + (hn : a = .nullCap ∨ ¬ ∃ c, σ.cdt c = some sl) : + (capRevokeFGA Dl f sl).run σ = ({(.ok (), σ)}, False) := by + rw [capRevokeFGA_pre Dl f sl σ hg] + have : ¬ (a ≠ .nullCap ∧ descendantsOf sl σ.cdt ≠ ∅) := by + rintro ⟨h1, h2⟩ + rcases hn with h | h + · exact h1 h + · exact h2 ((descendantsOf_empty_iffA sl σ.cdt).2 h) + simp only [NonDetE.whenE] + rw [if_neg this] + rfl + +/-- The step: delete a child `child`, then the preemption point, then the +rest (x86 `capRevokeFG_step`). -/ +theorem capRevokeFGA_step (Dl : CslotPtr → PMonad Unit Unit) (f : CslotPtr → NonDet (State Unit) (Except Unit Unit)) + (sl child : CslotPtr) (σ : State Unit) {a ac : Cap} (hg : getCap (Z := Unit) sl σ = ({(a, σ)}, False)) + (ha : a ≠ .nullCap) (hch : child ∈ descendantsOf sl σ.cdt) + (hgc : getCap (Z := Unit) child σ = ({(ac, σ)}, False)) (hac : ac ≠ .nullCap) {y : Except Unit Unit × State Unit} + (hy : y ∈ ((Dl child >>= fun _ => preemptionPoint >>= fun _ => f sl).run σ).1) : + y ∈ ((capRevokeFGA Dl f sl).run σ).1 := by + rw [capRevokeFGA_pre Dl f sl σ hg] + have hne : descendantsOf sl σ.cdt ≠ ∅ := fun h => by rw [h] at hch; exact hch + simp only [NonDetE.whenE] + rw [if_pos ⟨ha, hne⟩] + refine mem_bindE_okD (a := child) (s' := σ) (mem_liftED (selectExt_memA _ hch σ)) ?_ + refine mem_bindE_okD (mem_of_runD (run_liftE_stD hgc)) ?_ + refine mem_bindE_okD (a := ()) (s' := σ) ?_ hy + rw [NonDetE.assertE]; exact mem_liftED (m := NonDet.assert _) ⟨hac, rfl⟩ + +/-- **One round of `cap_revoke`** over `Dl` (x86 `capRevokeG_step`): from the +deletion of a child and a preemption point that do not preempt, an outcome of +the rest is one of the whole's. -/ +theorem capRevokeGA_step {Dl : CslotPtr → PMonad Unit Unit} {sl child : CslotPtr} {σ σd σp : State Unit} {a ac : Cap} + (hg : getCap (Z := Unit) sl σ = ({(a, σ)}, False)) (ha : a ≠ .nullCap) + (hch : child ∈ descendantsOf sl σ.cdt) (hgc : getCap (Z := Unit) child σ = ({(ac, σ)}, False)) + (hac : ac ≠ .nullCap) (hd : (.ok (), σd) ∈ ((Dl child).run σ).1) + (hp : (.ok (), σp) ∈ ((preemptionPoint (Z := Unit)).run σd).1) {x : Except Unit Unit × State Unit} + (hx : x ∈ ((capRevokeGA Dl sl).run σp).1) : x ∈ ((capRevokeGA Dl sl).run σ).1 := by + rw [capRevokeGA_run] + refine lfp_memA _ fun f hf => capRevokeFGA_step Dl f sl child σ hg ha hch hgc hac ?_ + refine mem_bindE_okD hd (mem_bindE_okD hp ?_) + exact (lfp_le_prefixedA _ f hf sl σp).1 hx + +/-- The deletion of a child preempts. -/ +theorem capRevokeGA_del_preempt {Dl : CslotPtr → PMonad Unit Unit} {sl child : CslotPtr} {σ σd : State Unit} + {a ac : Cap} (hg : getCap (Z := Unit) sl σ = ({(a, σ)}, False)) (ha : a ≠ .nullCap) + (hch : child ∈ descendantsOf sl σ.cdt) (hgc : getCap (Z := Unit) child σ = ({(ac, σ)}, False)) + (hac : ac ≠ .nullCap) (hd : (.error (), σd) ∈ ((Dl child).run σ).1) : + (.error (), σd) ∈ ((capRevokeGA Dl sl).run σ).1 := by + rw [capRevokeGA_run] + exact lfp_memA _ fun f _ => capRevokeFGA_step Dl f sl child σ hg ha hch hgc hac (mem_bindE_errD hd) + +/-- The preemption point after the deletion of a child preempts. -/ +theorem capRevokeGA_pp_preempt {Dl : CslotPtr → PMonad Unit Unit} {sl child : CslotPtr} {σ σd σp : State Unit} + {a ac : Cap} (hg : getCap (Z := Unit) sl σ = ({(a, σ)}, False)) (ha : a ≠ .nullCap) + (hch : child ∈ descendantsOf sl σ.cdt) (hgc : getCap (Z := Unit) child σ = ({(ac, σ)}, False)) + (hac : ac ≠ .nullCap) (hd : (.ok (), σd) ∈ ((Dl child).run σ).1) + (hp : (.error (), σp) ∈ ((preemptionPoint (Z := Unit)).run σd).1) : + (.error (), σp) ∈ ((capRevokeGA Dl sl).run σ).1 := by + rw [capRevokeGA_run] + exact lfp_memA _ fun f _ => + capRevokeFGA_step Dl f sl child σ hg ha hch hgc hac (mem_bindE_okD hd (mem_bindE_errD hp)) + +/-- `cap_revoke` stops at a null cap or a slot without children. -/ +theorem capRevokeGA_stop {Dl : CslotPtr → PMonad Unit Unit} {sl : CslotPtr} {σ : State Unit} {a : Cap} + (hg : getCap (Z := Unit) sl σ = ({(a, σ)}, False)) (hn : a = .nullCap ∨ ¬ ∃ c, σ.cdt c = some sl) : + (capRevokeGA Dl sl).run σ = ({(.ok (), σ)}, False) := by + rw [capRevokeGA_run] + exact lfp_oneA (fun r c => capRevokeFGA Dl r c) sl σ _ fun f => capRevokeFGA_stop Dl f sl σ hg hn + +/-- **`cap_revoke` of a slot without children** does nothing (x86 +`CnodeOther.capRevoke_none`). -/ +theorem capRevokeGA_none {Dl : CslotPtr → PMonad Unit Unit} {sl : CslotPtr} {σ : State Unit} {a : Cap} + (hg : getCap (Z := Unit) sl σ = ({(a, σ)}, False)) (hn : ¬ ∃ c, σ.cdt c = some sl) : + (capRevokeGA Dl sl).run σ = ({(.ok (), σ)}, False) := + capRevokeGA_stop hg (Or.inr hn) + +/-! ## `decode_cnode_invocation` at labels 21–23 (Revoke, Delete, CancelBadgedSends) + +After the common prefix (the label range, at least two words, the target lookup), the three arms of x86 +`CnodeSpec.copyTail` (and S3's `CnodeSpecA.copyTailA`), stated against `decodeCnodeInvocation` itself. A short +message (fewer than two words) is S3's `decode_shortA` for every label in 21..28. -/ + +/-- The CancelBadgedSends arm after the target lookup: the target cap, which +needs full rights (`has_cancel_send_rights`). -/ +def cancelArmA (d : CslotPtr) : SEMonad Unit CnodeInvocation := do + let cap ← NonDetE.liftE <| getCap d + NonDetE.unlessE (hasCancelSendRights cap) <| NonDetE.throwError .illegalOperation + NonDetE.returnOk <| .cancelBadgedSendsCall cap + +section +variable (label : Data) (cap : Cap) (excaps : List Cap) (x0 x1 : Data) (rest : List Data) + +theorem decode_revokeA (hg : genInvocationType label = .cnodeRevoke) : + decodeCnodeInvocation (Z := Unit) label (x0 :: x1 :: rest) cap excaps = + (lookupTargetSlot cap (dataToCptr x0) (dataToNat x1) >>= fun d => + NonDetE.returnOk (CnodeInvocation.revokeCall d)) := by + have h1 : GenInvocationLabels.cnodeRevoke ∈ GenInvocationLabels.uptoEnum .cnodeRevoke .cnodeRotate := by decide + have h2 : GenInvocationLabels.cnodeRevoke ∉ GenInvocationLabels.uptoEnum .cnodeCopy .cnodeMutate := by decide + have hl : ¬ (rest.length + 1 + 1 < 2) := by omega + unfold decodeCnodeInvocation + simp only [hg] + simp [NonDetE.unlessE, hg, h1, h2, hl, NonDetE.whenE, NonDetE.returnOk, the, ndE_pure_bindD] + +theorem decode_deleteA (hg : genInvocationType label = .cnodeDelete) : + decodeCnodeInvocation (Z := Unit) label (x0 :: x1 :: rest) cap excaps = + (lookupTargetSlot cap (dataToCptr x0) (dataToNat x1) >>= fun d => + NonDetE.returnOk (CnodeInvocation.deleteCall d)) := by + have h1 : GenInvocationLabels.cnodeDelete ∈ GenInvocationLabels.uptoEnum .cnodeRevoke .cnodeRotate := by decide + have h2 : GenInvocationLabels.cnodeDelete ∉ GenInvocationLabels.uptoEnum .cnodeCopy .cnodeMutate := by decide + have hl : ¬ (rest.length + 1 + 1 < 2) := by omega + unfold decodeCnodeInvocation + simp only [hg] + simp [NonDetE.unlessE, hg, h1, h2, hl, NonDetE.whenE, NonDetE.returnOk, the, ndE_pure_bindD] + +theorem decode_cancelA (hg : genInvocationType label = .cnodeCancelBadgedSends) : + decodeCnodeInvocation (Z := Unit) label (x0 :: x1 :: rest) cap excaps = + (lookupTargetSlot cap (dataToCptr x0) (dataToNat x1) >>= cancelArmA) := by + have h1 : GenInvocationLabels.cnodeCancelBadgedSends ∈ GenInvocationLabels.uptoEnum .cnodeRevoke .cnodeRotate := by decide + have h2 : GenInvocationLabels.cnodeCancelBadgedSends ∉ GenInvocationLabels.uptoEnum .cnodeCopy .cnodeMutate := by decide + have hl : ¬ (rest.length + 1 + 1 < 2) := by omega + unfold decodeCnodeInvocation cancelArmA + simp only [hg] + simp [NonDetE.unlessE, hg, h1, h2, hl, NonDetE.whenE, NonDetE.returnOk, the, ndE_pure_bindD] + +variable (σ : State Unit) {r : Except SyscallError CslotPtr} + +/-- Revoke's decode run, from the target lookup's single outcome. -/ +theorem decode_revoke_runA (hg : genInvocationType label = .cnodeRevoke) + (hl : (lookupTargetSlot (Z := Unit) cap (dataToCptr x0) (dataToNat x1)).run σ = ({(r, σ)}, False)) : + (decodeCnodeInvocation (Z := Unit) label (x0 :: x1 :: rest) cap excaps).run σ = + ({(r.map CnodeInvocation.revokeCall, σ)}, False) := by + rw [decode_revokeA label cap excaps x0 x1 rest hg] + cases r with + | error e => exact run_bind_errorD hl _ + | ok d => rw [run_bind_okD hl]; rfl + +/-- Delete's decode run, from the target lookup's single outcome. -/ +theorem decode_delete_runA (hg : genInvocationType label = .cnodeDelete) + (hl : (lookupTargetSlot (Z := Unit) cap (dataToCptr x0) (dataToNat x1)).run σ = ({(r, σ)}, False)) : + (decodeCnodeInvocation (Z := Unit) label (x0 :: x1 :: rest) cap excaps).run σ = + ({(r.map CnodeInvocation.deleteCall, σ)}, False) := by + rw [decode_deleteA label cap excaps x0 x1 rest hg] + cases r with + | error e => exact run_bind_errorD hl _ + | ok d => rw [run_bind_okD hl]; rfl + +/-- The CancelBadgedSends arm's run: the target cap with full rights, or +`IllegalOperation`. -/ +theorem cancelArm_runA (d : CslotPtr) {c : Cap} (hc : getCap (Z := Unit) d σ = ({(c, σ)}, False)) : + (cancelArmA d).run σ = ({(if hasCancelSendRights c then .ok (.cancelBadgedSendsCall c) + else .error .illegalOperation, σ)}, False) := by + unfold cancelArmA + rw [run_bind_okD (run_liftE_stD hc)] + by_cases h : hasCancelSendRights c + · simp only [NonDetE.unlessE, h, ↓reduceIte, ndE_pure_bindD]; rfl + · simp only [NonDetE.unlessE, h, ↓reduceIte, NonDetE.throwError, ndE_throw_bindD]; rfl + +/-- CancelBadgedSends's decode run: the lookup's error, or the arm at the +target slot. -/ +theorem decode_cancel_runA (hg : genInvocationType label = .cnodeCancelBadgedSends) {d : CslotPtr} + (hl : (lookupTargetSlot (Z := Unit) cap (dataToCptr x0) (dataToNat x1)).run σ = ({(.ok d, σ)}, False)) : + (decodeCnodeInvocation (Z := Unit) label (x0 :: x1 :: rest) cap excaps).run σ = (cancelArmA d).run σ := by + rw [decode_cancelA label cap excaps x0 x1 rest hg, run_bind_okD hl] + +theorem decode_cancel_errA (hg : genInvocationType label = .cnodeCancelBadgedSends) {e : SyscallError} + (hl : (lookupTargetSlot (Z := Unit) cap (dataToCptr x0) (dataToNat x1)).run σ = ({(.error e, σ)}, False)) : + (decodeCnodeInvocation (Z := Unit) label (x0 :: x1 :: rest) cap excaps).run σ = ({(.error e, σ)}, False) := by + rw [decode_cancelA label cap excaps x0 x1 rest hg, run_bind_errorD hl] + +end + +/-! ### `invoke_cnode` at the three invocations -/ + +theorem fl4InvokeCnode_revokeA (d : CslotPtr) : + fl4InvokeCnode (Z := Unit) (.revokeCall d) = capRevokeGA fl4CapDelete d := rfl + +theorem fl4InvokeCnode_deleteA (d : CslotPtr) : + fl4InvokeCnode (Z := Unit) (.deleteCall d) = capDeleteGA fl4FinaliseCap d := rfl + +theorem fl4InvokeCnode_cancelA (c : Cap) : + fl4InvokeCnode (Z := Unit) (.cancelBadgedSendsCall c) = invokeCnode (.cancelBadgedSendsCall c) := rfl + +theorem invokeCnode_revokeA (d : CslotPtr) : invokeCnode (Z := Unit) (.revokeCall d) = capRevokeGA capDelete d := rfl + +theorem invokeCnode_deleteA (d : CslotPtr) : + invokeCnode (Z := Unit) (.deleteCall d) = capDeleteGA finaliseCap d := rfl + +/-- CancelBadgedSends with badge 0 does nothing. -/ +theorem invokeCnode_cancel_zeroA (ep : ObjRef) (R : CapRights) (σ : State Unit) : + (invokeCnode (Z := Unit) (.cancelBadgedSendsCall (.endpointCap ep 0 R))).run σ = ({(.ok (), σ)}, False) := by + show (NonDetE.liftE (NonDet.when ((0 : Badge) ≠ 0) (cancelBadgedSends (Z := Unit) ep 0))).run σ = _ + unfold NonDet.when + rw [if_neg (fun h => h rfl)] + exact run_liftE_stD rfl + +/-- CancelBadgedSends with a non-zero badge is the spec's +`cancel_badged_sends`, without preemption. -/ +theorem invokeCnode_cancel_nzA (ep : ObjRef) {b : Badge} (R : CapRights) (hb : b ≠ 0) : + invokeCnode (Z := Unit) (.cancelBadgedSendsCall (.endpointCap ep b R)) = + NonDetE.liftE (cancelBadgedSends ep b) := by + show NonDetE.liftE (NonDet.when (b ≠ 0) (cancelBadgedSends (Z := Unit) ep b)) = _ + unfold NonDet.when + rw [if_pos hb] + +/-! ## `cancel_badged_sends` -/ + +section +variable {σ σ1 : State Unit} + +/-- A send queue: `remove_and_restart_badged_thread` over it, then +`reschedule_required`. -/ +theorem cancelBadgedSends_sendA {ep : ObjRef} {b : Badge} {q : List ObjRef} + (hg : getEndpoint (Z := Unit) ep σ = ({(.sendEP q, σ)}, False)) : + cancelBadgedSends (Z := Unit) ep b σ = (do + NonDet.mapMx (fun t => removeAndRestartBadgedThread t ep b) q + rescheduleRequired : SMonad Unit Unit) σ := by + unfold cancelBadgedSends + rw [nd_bind_oneD hg] + +/-- Any other endpoint state: nothing. -/ +theorem cancelBadgedSends_otherA {ep : ObjRef} {b : Badge} {e : Endpoint} (hne : ∀ q, e ≠ .sendEP q) + (hg : getEndpoint (Z := Unit) ep σ = ({(e, σ)}, False)) : + cancelBadgedSends (Z := Unit) ep b σ = ({((), σ)}, False) := by + unfold cancelBadgedSends + rw [nd_bind_oneD hg] + cases e with + | sendEP q => exact absurd rfl (hne q) + | idleEP => rfl + | recvEP q => rfl + +theorem mapMx_nil_memA (g : ObjRef → SMonad Unit Unit) : ((), σ) ∈ (NonDet.mapMx g [] σ).1 := rfl + +/-- One step of the queue walk: an outcome of the head's step, then of the rest. -/ +theorem mapMx_cons_memA {g : ObjRef → SMonad Unit Unit} {a : ObjRef} {l : List ObjRef} {z : Unit × State Unit} + (h : ((), σ1) ∈ (g a σ).1) (hz : z ∈ (NonDet.mapMx g l σ1).1) : z ∈ (NonDet.mapMx g (a :: l) σ).1 := + ⟨((), σ1), h, hz⟩ + +theorem mapMx_cons_oneA {g : ObjRef → SMonad Unit Unit} {a : ObjRef} {l : List ObjRef} + (h : g a σ = ({((), σ1)}, False)) : NonDet.mapMx g (a :: l) σ = NonDet.mapMx g l σ1 := + nd_bind_oneD h _ + +/-- A thread blocked with the badge: dequeued, then restarted. -/ +theorem removeAndRestartBadgedThread_matchA {t ep : ObjRef} {b : Badge} {st : ThreadState} + (hst : getThreadState (Z := Unit) t σ = ({(st, σ)}, False)) (hb : blockingIpcBadge st = b) : + removeAndRestartBadgedThread (Z := Unit) t ep b σ = (do + tcbEpDequeue t ep + restartThreadIfNoFault t : SMonad Unit Unit) σ := by + unfold removeAndRestartBadgedThread + rw [nd_bind_oneD hst] + unfold NonDet.when + rw [if_pos hb] + +/-- Another badge: skipped. -/ +theorem removeAndRestartBadgedThread_skipA {t ep : ObjRef} {b : Badge} {st : ThreadState} + (hst : getThreadState (Z := Unit) t σ = ({(st, σ)}, False)) (hb : blockingIpcBadge st ≠ b) : + removeAndRestartBadgedThread (Z := Unit) t ep b σ = ({((), σ)}, False) := by + unfold removeAndRestartBadgedThread + rw [nd_bind_oneD hst] + unfold NonDet.when + rw [if_neg hb]; rfl + +/-- No fault: restart, the sporadic check, the switch. -/ +theorem restartThreadIfNoFault_okA {t : ObjRef} + (hf : threadGet (Z := Unit) Tcb.tcbFault t σ = ({(none, σ)}, False)) : + restartThreadIfNoFault (Z := Unit) t σ = (do + setThreadState t .restart + let scOpt ← getTcbObjRef Tcb.tcbSchedContext t + ifSporadicCurScAssertRefillUnblockCheck scOpt + possibleSwitchTo t : SMonad Unit Unit) σ := by + unfold restartThreadIfNoFault + rw [nd_bind_oneD hf, if_pos rfl] + +/-- A fault: the thread becomes inactive. -/ +theorem restartThreadIfNoFault_faultA {t : ObjRef} {f : Fault} + (hf : threadGet (Z := Unit) Tcb.tcbFault t σ = ({(some f, σ)}, False)) : + restartThreadIfNoFault (Z := Unit) t σ = setThreadState t .inactive σ := by + unfold restartThreadIfNoFault + rw [nd_bind_oneD hf, if_neg (fun h => by cases h)] + +end + +/-! ## `fl4FinaliseCap` dispatch (`Ext/Finalise`) -/ + +/-- `extKindOfAsid`'s result: the root kind, with Stage-1 (l4v's own) as `none`. -/ +def extNormA : Option ExtRootKind → Option ExtRootKind + | some .stage1 => none + | k => k + +theorem extKindOfAsid_eqA (asid : Asid) (σ : State Unit) : + extKindOfAsid (Z := Unit) asid σ = ({(extNormA (asidRootKind σ asid), σ)}, False) := by + unfold extKindOfAsid + rw [nd_bind_oneD (m := NonDet.gets fun s : State Unit => asidRootKind s asid) (s' := σ) rfl] + dsimp only + cases asidRootKind σ asid with + | none => rfl + | some k => cases k <;> rfl + +/-- A Stage-2 VSpace or `ArmHighVSpace` root: its kind. -/ +theorem extKindOfAsid_someA {asid : Asid} {σ : State Unit} {k : ExtRootKind} (h : asidRootKind σ asid = some k) + (hk : k ≠ .stage1) : extKindOfAsid (Z := Unit) asid σ = ({(some k, σ)}, False) := by + rw [extKindOfAsid_eqA, h] + cases k with + | stage1 => exact absurd rfl hk + | stage2 => rfl + | high => rfl + +/-- No root, or an l4v (Stage-1) root: `none`. -/ +theorem extKindOfAsid_noneA {asid : Asid} {σ : State Unit} + (h : asidRootKind σ asid = none ∨ asidRootKind σ asid = some .stage1) : + extKindOfAsid (Z := Unit) asid σ = ({(none, σ)}, False) := by + rw [extKindOfAsid_eqA] + rcases h with h | h <;> rw [h] <;> rfl + +section +variable {σ : State Unit} {asid : Asid} {va : VSpaceRef} {p : ObjRef} + +/-- (ExtVsA gen 3) A mapped plain frame whose ASID names a high window: `extUnmapPage .high` (FL4 `unmap_page`'s +`ROOT_HIGH` walk), final or not. -/ +theorem fl4FinaliseCap_frame_hiA {r : CapRights} {sz : VMPageSize} {dev final : Bool} + (hk : extKindOfAsid (Z := Unit) asid σ = ({(some .high, σ)}, False)) : + fl4FinaliseCap (Z := Unit) (.archObjectCap (.frameCap p r sz dev (some (asid, va)))) final σ = (do + extUnmapPage .high sz asid va p + return (Cap.nullCap, Cap.nullCap) : SMonad Unit (Cap × Cap)) σ := by + simp only [fl4FinaliseCap] + rw [nd_bind_oneD hk] + all_goals rfl + +/-- (ExtVsA gen 3) A mapped plain frame whose ASID names a Stage-2 root: nothing (FL4 `unmap_page` finds no root). -/ +theorem fl4FinaliseCap_frame_s2A {r : CapRights} {sz : VMPageSize} {dev final : Bool} + (hk : extKindOfAsid (Z := Unit) asid σ = ({(some .stage2, σ)}, False)) : + fl4FinaliseCap (Z := Unit) (.archObjectCap (.frameCap p r sz dev (some (asid, va)))) final σ = + ({((Cap.nullCap, Cap.nullCap), σ)}, False) := by + simp only [fl4FinaliseCap] + rw [nd_bind_oneD hk] + all_goals rfl + +/-- (ExtVsA gen 3) A mapped Stage-2 frame cap (`stage2FrameCap`): `extUnmapPage .stage2` (FL4 `ept::unmap`), final or +not. -/ +theorem fl4FinaliseCap_s2frameA {r : CapRights} {sz : VMPageSize} {dev final : Bool} : + fl4FinaliseCap (Z := Unit) (.archObjectCap (.ext (.stage2FrameCap p r sz dev (some (asid, va))))) final = (do + extUnmapPage .stage2 sz asid va p + return (Cap.nullCap, Cap.nullCap) : SMonad Unit (Cap × Cap)) := rfl + +/-- A mapped frame in an l4v root: l4v's `unmap_page`. -/ +theorem fl4FinaliseCap_frame_l4vA {r : CapRights} {sz : VMPageSize} {dev final : Bool} + (hk : extKindOfAsid (Z := Unit) asid σ = ({(none, σ)}, False)) : + fl4FinaliseCap (Z := Unit) (.archObjectCap (.frameCap p r sz dev (some (asid, va)))) final σ = (do + unmapPage sz asid va p + return (Cap.nullCap, Cap.nullCap) : SMonad Unit (Cap × Cap)) σ := by + simp only [fl4FinaliseCap] + rw [nd_bind_oneD hk] + cases final <;> rfl + +/-- The final cap of a mapped page table in an FL4 root: `extUnmapTable` (FL4 +`vspace::unmap_page_table`). -/ +theorem fl4FinaliseCap_pt_extA {k : ExtRootKind} (hk : extKindOfAsid (Z := Unit) asid σ = ({(some k, σ)}, False)) : + fl4FinaliseCap (Z := Unit) (.archObjectCap (.pageTableCap p .normalPtT (some (asid, va)))) true σ = (do + extUnmapTable k asid va p + return (Cap.nullCap, Cap.nullCap) : SMonad Unit (Cap × Cap)) σ := by + simp only [fl4FinaliseCap] + rw [nd_bind_oneD hk] + rfl + +/-- The final cap of a mapped page table in an l4v root: l4v's `unmap_page_table`. -/ +theorem fl4FinaliseCap_pt_l4vA (hk : extKindOfAsid (Z := Unit) asid σ = ({(none, σ)}, False)) : + fl4FinaliseCap (Z := Unit) (.archObjectCap (.pageTableCap p .normalPtT (some (asid, va)))) true σ = (do + unmapPageTable asid va p + return (Cap.nullCap, Cap.nullCap) : SMonad Unit (Cap × Cap)) σ := by + simp only [fl4FinaliseCap] + rw [nd_bind_oneD hk] + rfl + +/-- A non-final mapped page table cap: nothing, in either kind of root. -/ +theorem fl4FinaliseCap_pt_nfA {k : Option ExtRootKind} + (hk : extKindOfAsid (Z := Unit) asid σ = ({(k, σ)}, False)) : + fl4FinaliseCap (Z := Unit) (.archObjectCap (.pageTableCap p .normalPtT (some (asid, va)))) false σ = + ({((Cap.nullCap, Cap.nullCap), σ)}, False) := by + simp only [fl4FinaliseCap] + rw [nd_bind_oneD hk] + cases k <;> rfl + +/-- The final cap of an assigned Stage-2 VSpace / `ArmHighVSpace`: l4v's `delete_asid`. -/ +theorem fl4FinaliseCap_root_finalA {c : ExtArchCap} (hm : c.mapData = some asid) : + fl4FinaliseCap (Z := Unit) (.archObjectCap (.ext c)) true = (do + deleteAsid asid c.objRef + return (Cap.nullCap, Cap.nullCap) : SMonad Unit (Cap × Cap)) := by + cases c with + | stage2VSpaceCap q m => simp only [ExtArchCap.mapData] at hm; subst hm; rfl + | highVSpaceCap q m => simp only [ExtArchCap.mapData] at hm; subst hm; rfl + | stage2FrameCap => simp [ExtArchCap.mapData] at hm + +/-- Any other Stage-2 VSpace / `ArmHighVSpace` cap: nothing (ExtVsA gen 3: a root cap, `isRoot`). -/ +theorem fl4FinaliseCap_root_otherA {c : ExtArchCap} {final : Bool} (hr : c.isRoot = true) + (h : c.mapData = none ∨ final = false) : + fl4FinaliseCap (Z := Unit) (.archObjectCap (.ext c)) final = return (Cap.nullCap, Cap.nullCap) := by + cases c with + | stage2FrameCap => cases hr + | stage2VSpaceCap q m => + simp only [ExtArchCap.mapData] at h + rcases h with h | h + · subst h; rfl + · subst h; cases m <;> rfl + | highVSpaceCap q m => + simp only [ExtArchCap.mapData] at h + rcases h with h | h + · subst h; rfl + · subst h; cases m <;> rfl + +end + +/-- A VCPU cap: FL4's end of the run when final, then l4v's. -/ +theorem fl4FinaliseCap_vcpuA (v : ObjRef) (final : Bool) : + fl4FinaliseCap (Z := Unit) (.archObjectCap (.vcpuCap v)) final = (do + NonDet.when final <| fl4VcpuEndRun v + finaliseCap (.archObjectCap (.vcpuCap v)) final : SMonad Unit (Cap × Cap)) := rfl + +/-- A thread cap: FL4's `cancel_run` and bound-VCPU `end_run` when final, then l4v's. -/ +theorem fl4FinaliseCap_threadA (t : ObjRef) (final : Bool) : + fl4FinaliseCap (Z := Unit) (.threadCap t) final = (do + NonDet.when final <| do + fl4CancelRun t + let vOpt ← archThreadGet ArchTcb.tcbVcpu t + match vOpt with + | some v => fl4VcpuEndRun v + | none => pure () + finaliseCap (.threadCap t) final : SMonad Unit (Cap × Cap)) := rfl + +/-- The caps `fl4FinaliseCap` treats differently from l4v's `finalise_cap`. -/ +def Fl4HookCapA : Cap → Prop + | .archObjectCap (.frameCap _ _ _ _ (some _)) => True + | .archObjectCap (.pageTableCap _ .normalPtT (some _)) => True + | .archObjectCap (.ext _) => True + | .archObjectCap (.vcpuCap _) => True + | .threadCap _ => True + | _ => False + +/-- **Every other cap is finalised by l4v's `finalise_cap`.** -/ +theorem fl4FinaliseCap_plainA {c : Cap} (h : ¬ Fl4HookCapA c) (final : Bool) : + fl4FinaliseCap (Z := Unit) c final = finaliseCap c final := by + cases c with + | threadCap t => exact (h trivial).elim + | archObjectCap a => + cases a with + | frameCap p r sz d m => + cases m with + | none => rfl + | some m => exact (h trivial).elim + | pageTableCap p t m => + cases t <;> cases m + · rfl + · rfl + · rfl + · exact (h trivial).elim + | ext c => exact (h trivial).elim + | vcpuCap v => exact (h trivial).elim + | asidPoolCap _ _ => rfl + | asidControlCap => rfl + | sgiSignalCap _ _ => rfl + | smcCap _ => rfl + | _ => rfl + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelStepAsmA.lean b/proofs/Fl4ProofsA64/P3/DelStepAsmA.lean new file mode 100644 index 000000000..be7766a7f --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelStepAsmA.lean @@ -0,0 +1,799 @@ +/- +P3 (AArch64, delete hooks E4): **the deletion loop's three slot-step hooks for the loop record `DelXA6`**. + +* **`swapHyp_delA`**: `SwapHypA` at `DelJWQA s0 (DelXA6 … (J5A) (J7LA (J7R2A ipa)))`. +* **`zombieHyp_delA`**: `ZombieHypA` at the same invariant, with `Rz := DelRzA`. +* **`emptyHyp_delA`**: `EmptyHypA`, from `Wx` (`J7LA J7R2tA`) and `Rd := DelRdXA … (RvVcA OS Rv)` + (**`emptyDead_delA`**: the dead-object clauses after the emptying, typed namers; `RvVcA` is the VCPU clause S4's + `Rv` carries). + +Each is `JStage3TA.of_slotW` with the step lemmas of the `Del*KeepA` / `DelJ?StepA` / `DelStepSpecA` files. + +The VSpace words are off the slot words by the narrowed `ent.vs` / `vsOk` (D1b), inside `of_slotW`: no temporary +hypothesis. The step's untyped and zombie caps keep `devUtOff` / `zombieBnd`. No named premise. New module. +-/ +import Fl4ProofsA64.P3.DelSidleStepA +import Fl4ProofsA64.P3.DelBufKeepA +import Fl4ProofsA64.P3.DelCtOffA +import Fl4ProofsA64.P3.DelStepSpecA +import Fl4ProofsA64.P3.DelJ5StepA +import Fl4ProofsA64.P3.DelRecA +import Fl4ProofsA64.P3.DelRec6A +import Fl4ProofsA64.P3.IpcXferLoopA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## The spec steps keep the machine state -/ + +theorem capSwapForDelete_msA (s1 s2 : CslotPtr) : NdMsA (capSwapForDelete (Z := Unit) s1 s2) := by + have hG : ∀ sl, NdMsA (getCap (Z := Unit) sl) := getCap_msA + unfold capSwapForDelete NonDet.when capSwap setCdt setOriginal doExtendedOp + repeat' first + | exact NdMsA.pure _ | exact hG _ | exact setCap_msA _ _ | exact NdMsA.gets _ | exact NdMsA.get + | exact NdMsA.assert _ | exact NdMsA.selectF _ | exact NdMsA.modify (fun _ => rfl) + | exact NdMsA.getPut (fun _ => rfl) + | refine NdMsA.bind ?_ (fun _ => ?_) + | split + +theorem emptySlot_msA (sl : CslotPtr) : NdMsA (emptySlot (Z := Unit) sl .nullCap) := by + have hG : ∀ sl, NdMsA (getCap (Z := Unit) sl) := getCap_msA + unfold emptySlot postCapDeletion setCdt setOriginal doExtendedOp + repeat' first + | exact NdMsA.pure _ | exact hG _ | exact setCap_msA _ _ | exact NdMsA.gets _ | exact NdMsA.get + | exact NdMsA.assert _ | exact NdMsA.selectF _ | exact NdMsA.modify (fun _ => rfl) + | exact NdMsA.getPut (fun _ => rfl) + | refine NdMsA.bind ?_ (fun _ => ?_) + | split + +section +variable {m : Fl4SpecA64.McState} +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Pp Tb : Set U64} {Lv : U64 → Nat} {ia ie : Nat} + {N E Rp : Set U64} {κ : U64 → U64} {n : Nat} {ipa : U64} {s0 : S} + +/-- The record's footprint facts at a ghost, queues and hardware root. -/ +theorem delFoot_loopA {T Ω Sg : Set U64} (F : DelFootA st Z T Ω Sg OS s0) (G : MdbGhostA) + (Q : U64 → U64 → List U64) (R : U64 → List U64) (hw : HwRootA) : + DelFootA st { Z with G := G, Q := Q, R := R, hw := hw } T Ω Sg OS s0 := + ⟨F.os, fun b h => F.off b (footNA_loopA h), fun b h => F.al b (footNA_loopA h)⟩ + +/-- The `HighVSpace` roots of the `OS` caps are in `Z.Y`. -/ +theorem hiY_ofA {a : A} {s : S} {σ : State Unit} {m : McState} {G : List (List U64)} {Z' : XSetsA} + (D : FinStA st Z'.th OS G s σ) (J : JAtZAX (L := L) AR Z' s a σ m) : + ∀ y ∈ OS, ∀ q mp ad, capA st s y = .HighVSpace q mp ad → q ∈ Z'.Y := by + intro y hy q mp ad e + by_cases h : IsSlotA Z'.th y + · exact (J.capObj h).2.2.1 q mp ad e + · rw [D.nul y hy h] at e; cases e + +/-- A TCB's buffer slot is an `OS` slot. -/ +theorem sbWord_osA {T : Set U64} (ts : TcbSlotsOS T OS) {t : U64} (ht : t ∈ T) : sbWordA t ∈ OS := by + have e : sbWordA t = slotIdx t 2#u64 := by + first + | rfl + | (unfold sbWordA slotIdx layout.tcb.SLOT_BUFFER layout.cte.SIZE; rfl) + rw [e] + exact ts t ht _ (by decide) + +/-- The TCB fields the buffer and dead-object clauses read. -/ +theorem ibW_keptA {s r : S} {a : A} {σ : State Unit} {m : McState} {Z' : XSetsA} {Ac : Set U64} + (J : JEntTA st AR Z' H OS U Ac Pp Tb Lv ia ie s a σ m) + (w : WritesIn st s r (W7.CapW H.tcbs {b | InOS OS b} ∅)) : + (∀ t ∈ Z'.T, L.mem r (ibWordA t) = L.mem s (ibWordA t)) ∧ + (∀ t ∈ Z'.T, L.mem r (wa t 3664#u64) = L.mem s (wa t 3664#u64)) ∧ + (∀ t ∈ H.tcbs, L.mem r (wa t 3664#u64) = L.mem s (wa t 3664#u64)) := by + have k := tcbFld_keptA J w + refine ⟨fun t ht => ?_, fun t ht => ?_, fun t ht => k t ht _ (by simp) (by simp) (by simp)⟩ + · exact k t (by rw [J.tcbs]; exact ht) 3512#u64 (by simp) (by simp) (by simp) + · exact k t (by rw [J.tcbs]; exact ht) _ (by simp) (by simp) (by simp) + +variable (st) in +/-- **What a slot-word step of the loop must keep of a further clause `Φ`** of the loop record (`DelXPA`). -/ +def SlotKeepPA (AR : A → HwRootA → Prop) (Z : XSetsA) (H : Heap) (OS U Pp Tb : Set U64) (Lv : U64 → Nat) (ia ie : Nat) + (N E Rp : Set U64) (κ : U64 → U64) (n : Nat) (Φ : Set (U64 × Fl4SpecA64.Word) → S → State Unit → Prop) : Prop := + ∀ {G : MdbGhostA} {Q : U64 → U64 → List U64} {R : U64 → List U64} {hw : HwRootA} {Ac : Set U64} + {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {m : McState} {R7 : Set U64 → JSlotA S} {s s' : S} + {a : A} {σ σ' : State Unit}, + JStage3TA st AR { Z with G := G, Q := Q, R := R, hw := hw } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n + (J5A st) (J7LA st R7) s a σ m → Φ Bf s σ → WritesIn st s s' (W7.CapW H.tcbs {b | InOS OS b} ∅) → + SpecSlotA σ σ' → + (∀ y ∈ OS, capA st s' y = .Null ∨ (∃ p t c, capA st s' y = .Zombie p t c) ∨ + ∃ z ∈ OS, capA st s' y = capA st s z) → + DelInvA st Z.th s' → σ'.machineState = σ.machineState → Φ Bf s' σ' + +/-- `Std6A` is kept by the loop's slot-word steps. -/ +theorem slotKeep_std6A : SlotKeepPA st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n (Std6GA st Ag Z H OS) := + fun J h w HS caps zwf ms => + have h' := Std6GA.slotStepA J (h.ghostA _ _ _ _) w HS caps zwf (by rw [ms]) + ⟨h'.utWin, h'.devWin, h'.vcBack, h'.runBack, h'.vcSnap, h'.vcOS, h'.userFr, h'.frTh, h'.bfSpec, h'.vcSym1, h'.vcSym2, h'.heapWin, + h'.coresAll, h'.zwf, h'.frOff, h'.frAg⟩ + +set_option maxHeartbeats 1000000 in +/-- `Std6A` is kept when tracked buffers are dropped. -/ +theorem std6A_monoA : ∀ (Bf Bf' : Set (U64 × Fl4SpecA64.Word)) (s : S) (σ : State Unit), Bf' ⊆ Bf → + Std6GA st Ag Z H OS Bf s σ → Std6GA st Ag Z H OS Bf' s σ := + fun _ _ _ _ sub h => { h with bfSpec := fun bp hbp => h.bfSpec bp (sub hbp) } + +/-- **A zombie swapped into its first slot keeps the loop invariant** (with `Φ`). -/ +theorem swapHyp_delPA {Φ : Set (U64 × Fl4SpecA64.Word) → S → State Unit → Prop} + (hΦ : SlotKeepPA st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n Φ) (F : DelFootA st Z H.tcbs H.omega H.frame OS s0) : + SwapHypA st H.tcbs Z.th OS + (DelJWQA st s0 (DelXPA st Φ AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st (J7R2A st ar ipa)))) := by + intro s s' a σ σ' G sl p zt k hW h1 D D' w HS hm hA hz hne hcap M + obtain ⟨hcore, ⟨Q, R, hw, Ac, Bf, stack, J, X⟩, -, hZT, hBH, hCT, hFx⟩ := hW + have hx : cslotAddrA sl ∈ OS := D.mem_slot hA + have hzok := D.zw _ (hA.isSlot D.place) p zt k hz + have hAp : AbsSlotA Z.th (homeSlA p zt) := hzok.absSlot + have hpa : cslotAddrA (homeSlA p zt) = p := hzok.addr + have hp : p ∈ OS := by rw [← hpa]; exact D.mem_slot hAp + have kb : L.ksb s = L.ksb s0 := hcore.1 + have jx : JAtZAX (L := L) AR { Z with G := swGA G p (cslotAddrA sl), Q := Q, R := R, hw := hw } s' a σ' m := by + refine (delFoot_loopA F G Q R hw).slotWrites hcore J.j w HS D'.rel D'.cdt fun y hy => ?_ + show CapObjOkA _ (capA st s' y) + rw [hcap y (D.sub y hy)] + split_ifs + · have h := hAp.isSlot D.place + rw [hpa] at h + exact J.j.capObj h + · exact J.j.capObj (hA.isSlot D.place) + · exact J.j.capObj hy + have hms : σ'.machineState = σ.machineState := capSwapForDelete_msA _ _ σ _ hm + have fr := CapFromA.of_swap (st := st) (s := s) (s' := s') hx hp hcap + have tc := CapToA.of_swap (st := st) (s := s) (s' := s') hx hp hcap + obtain ⟨ibk, tw, twH⟩ := ibW_keptA J.toJEntTA w + have hT : ∀ t ∈ Z.T, Z.th t = some .tcb := fun t ht => (J.j.1.thT t).2 ht + have hxB : cslotAddrA sl ∉ BufS H.tcbs := not_bufS_of_capA hBH hx (by rw [hz]; exact not_bufCap_zombieA) + have hpB : p ∉ BufS H.tcbs := by + rcases hzok with ⟨-, hq, -⟩ | ⟨-, hq, -⟩ + · exact base_not_bufSA D.place (fun t ht => hT t (by rw [← J.tcbs]; exact ht)) hq + · exact base_not_bufSA D.place (fun t ht => hT t (by rw [← J.tcbs]; exact ht)) hq + obtain ⟨Pt, Ub, vr, -, arm, -, -, -, -, link, -, -⟩ := id J.j7 + have ts : TcbSlotsOS H.tcbs OS := arm.case.ts + have bufkE : ∀ t ∈ Z.T, capA st s' (sbWordA t) = capA st s (sbWordA t) := by + intro t ht + have ht' : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + have hb : sbWordA t ∈ BufS H.tcbs := ⟨t, ht', rfl⟩ + rw [hcap _ (sbWord_osA ts ht'), if_neg (fun e => hxB (by rw [← e]; exact hb)), + if_neg (fun e => hpB (by rw [← e]; exact hb))] + have bufk : ∀ t ∈ Z.T, capA st s' (sbWordA t) = capA st s (sbWordA t) ∨ capA st s' (sbWordA t) = .Null := + fun t ht => Or.inl (bufkE t ht) + have mem : VsKeep st (L.ksb s) Pt s s' := vsKeep_capwA J.ent.heap ts vr w + have hQ : ∀ x, x = homeSlA p zt ∨ x = sl → AbsSlotA Z.th x := by + rintro x (rfl | rfl) + · exact hAp + · exact hA + have P := capSwapForDelete_provA hm + have live : ∀ x, σ'.isOriginalCap x = true → AbsSlotA Z.th x := origLive_stepA P hQ J.origLive + have hne' : homeSlA p zt ≠ sl := fun e => hne (by rw [← hpa, e]) + have hcapS : ∀ y ∈ OS, capA st s' y = if y = cslotAddrA (homeSlA p zt) then capA st s (cslotAddrA sl) + else if y = cslotAddrA sl then capA st s (cslotAddrA (homeSlA p zt)) else capA st s y := by + intro y hy + rw [hcap y hy, hpa] + by_cases e1 : y = cslotAddrA sl + · rw [if_pos e1, if_neg (fun e => hne (e.symm.trans e1)), if_pos e1] + · rw [if_neg e1] + by_cases e2 : y = p + · rw [if_pos e2, if_pos e2] + · rw [if_neg e2, if_neg e2, if_neg e1] + have hcapP : ∀ y ∈ OS, capA st s' y = if y = p then capA st s (cslotAddrA sl) + else if y = cslotAddrA sl then capA st s p else capA st s y := by + intro y hy + have := hcapS y hy + rwa [hpa] at this + have j5' := J5A.delSwapA (Z := { Z with G := G, Q := Q, R := R, hw := hw }) (m' := m) J.j5 D D' hp hx hcapP M w.1 + (irqSt_keptA J.ent.heap ts arm.kb D.place J.ent.ksw w) + have J' := JStage3TA.of_slotW J (Set.Subset.refl _) w HS hms + (fun b h => F.off b (footNA_coreA (G := Z.G) hcore (footNA_loopA h))) + (G' := swGA G p (cslotAddrA sl)) jx + (J.sinv.capSwapForDelete (by + have h := offNode_homeA (σ := σ) J.j.1.irq (by have := hCT _ hx; rwa [hz] at this) h1 + exact h) + (offNode_slotA D.place D.rel J.j.1.irq J.sinv.irqN hA hz) hm) + (J.sidle.swapForDeleteA HS (s1 := homeSlA p zt) (s2 := sl) + (fun c _ => replicate_ne_idx1A (specPtr p) (m.idleThreadOf c) _) + (J.sidle.neIdleSlotCapA (st := st) J.j.1.st hA (by rw [hz]; exact fun e => by cases e)) hm) + (J.objAlloc.fromA fr (fun _ _ h => h)) + (J.idleAnchor.fromA fr) + (BufCover.stepA J.bufCover D.rel D'.rel hT HS ibk bufk) + (BufNativeA.stepA J.bufNative bufk) + (J.deadExt.stepA HS tw (vcRunW_keptA J.toJEntTA w) (DeadBackA.of_capTo tc)) + (specFree_stepA J.specFree P D.rel hQ fr HS) + (J.utBlk.fromA fr) + (J.unmPt.stepA fr w.1 w.2.1 mem link HS) + (J.specZero.fromA fr (by rw [hms])) + live + (nullOrig_swapA hAp hA hne' hm D.rel D'.rel D.place D.sub hcapS live J.nullOrig) + D'.fr + (J.devFr.fromA fr (fun _ _ _ h => HS.keep h (fun _ _ e => by cases e) (fun _ e => by cases e))) + D'.cov + (zombieNoVcpu_keepA J.zombieNoVcpu J.zombieTcb twH (fun u hu q j e => by + rw [hcap u hu] at e + split_ifs at e + · exact ⟨p, hp, e⟩ + · exact ⟨_, hx, e⟩ + · exact ⟨u, hu, e⟩)) + (fun u hu q j e => by + rw [hcap u hu] at e + split_ifs at e + · exact J.zombieTcb p hp q j e + · exact J.zombieTcb _ hx q j e + · exact J.zombieTcb u hu q j e) + (BufOffA.fromA (Z := { Z with G := G, Q := Q, R := R, hw := hw }) fr w.1 rfl rfl rfl rfl rfl rfl J.bufOff + (fun bp h => (J.bufOff.pin bp h).keepC rfl rfl fun t ht _ => bufkE t ht)) + (J.zombieBnd.ofSrcA (fun u hu q j z e => by + rw [hcap u hu] at e + split_ifs at e + · exact ⟨p, hp, e⟩ + · exact ⟨_, hx, e⟩ + · exact ⟨u, hu, e⟩)) + (J.devUtOff.fromA (Z := { Z with G := G, Q := Q, R := R, hw := hw }) fr w.1 (fun _ _ h => h) rfl) + (fun u hu => by + rw [hcap u hu] + split_ifs + · exact J.capTy p hp + · exact J.capTy _ hx + · exact J.capTy u hu) + (fun u hu q b e => by + rw [hcap u hu] at e + split_ifs at e + · exact J.ciSc p hp q b e + · exact J.ciSc _ hx q b e + · exact J.ciSc u hu q b e) + j5' + (J.j7.swapA J.ent.heap w hx hp hcap (hiY_ofA D.toFinStA J.j)) + have X' : Φ Bf s' σ' := hΦ J X w HS (fun y hy => Or.inr (Or.inr (by + rw [hcap y hy] + split_ifs + exacts [⟨p, hp, rfl⟩, ⟨_, hx, rfl⟩, ⟨y, hy, rfl⟩]))) D'.zw hms + exact ⟨hcore.writes w, ⟨Q, R, hw, Ac, Bf, stack, J', X'⟩, j5'.1.1.parentAdj, + zombieNoVcpu_keepA hZT J.zombieTcb twH (fun u hu q j e => by + rw [hcap u hu] at e + split_ifs at e + · exact ⟨p, hp, e⟩ + · exact ⟨_, hx, e⟩ + · exact ⟨u, hu, e⟩), + bufHold_swapA hBH hxB hpB hcap, ctOff_swapA hCT w.1 hx hp hcap, by + have sx := hA.isSlot D.place + have sp : IsSlotA Z.th p := hpa ▸ hAp.isSlot D.place + have nx : ∀ t ∈ Z.T, cslotAddrA sl ≠ slotIdx t layout.tcb.SLOT_FAULT_HANDLER := + hFx.ne_fhA st (by + rw [hz] + rintro (e | ⟨_, _, _, _, _, _, e, -⟩) <;> cases e) + have np : ∀ t ∈ Z.T, p ≠ slotIdx t layout.tcb.SLOT_FAULT_HANDLER := fun t ht => by + rcases hzok with ⟨-, hq, -⟩ | ⟨-, hq, -⟩ + · exact base_ne_fhA D.place (hT t ht) hq + · exact base_ne_fhA D.place (hT t ht) hq + refine hFx.stepA st fun y hy => ?_ + have e := hcap y (D.sub y hy) + by_cases e1 : y = cslotAddrA sl + · rw [if_pos e1] at e + exact Or.inr (Or.inr ⟨e1 ▸ nx, Or.inl ⟨p, sp, e⟩⟩) + · rw [if_neg e1] at e + by_cases e2 : y = p + · rw [if_pos e2] at e + exact Or.inr (Or.inr ⟨e2 ▸ np, Or.inl ⟨_, sx, e⟩⟩) + · rw [if_neg e2] at e + exact Or.inl e⟩ + +set_option maxHeartbeats 1000000 in +/-- **A zombie swapped into its first slot keeps the loop invariant.** -/ +theorem swapHyp_delA (F : DelFootA st Z H.tcbs H.omega H.frame OS s0) : + SwapHypA st H.tcbs Z.th OS + (DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st (J7R2A st ar ipa)))) := + swapHyp_delPA slotKeep_std6A F + +/-- **A zombie written over the slot keeps the loop invariant** (with `Φ`). -/ +theorem zombieHyp_delPA {Φ : Set (U64 × Fl4SpecA64.Word) → S → State Unit → Prop} + (hΦ : SlotKeepPA st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n Φ) (F : DelFootA st Z H.tcbs H.omega H.frame OS s0) : + ZombieHypA st H.tcbs Z.th OS + (DelJWQA st s0 (DelXPA st Φ AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st (J7R2A st ar ipa)))) + (DelRzA st) := by + intro s s' a σ σ' G sl p zt k hW hRz hzz hnz D D' w HS hset hA hn hc hcap M + obtain ⟨hcore, ⟨Q, R, hw, Ac, Bf, stack, J, X⟩, -, hZT, hBH, hCT, hFx⟩ := hW + have hx : cslotAddrA sl ∈ OS := D.mem_slot hA + have hxs := hA.isSlot D.place + have kb : L.ksb s = L.ksb s0 := hcore.1 + have jx : JAtZAX (L := L) AR { Z with G := G, Q := Q, R := R, hw := hw } s' a σ' m := by + refine (delFoot_loopA F G Q R hw).slotWrites hcore J.j w HS D'.rel D'.cdt fun y hy => ?_ + show CapObjOkA _ (capA st s' y) + by_cases e : y = cslotAddrA sl + · rw [e, hc] + exact ⟨fun _ h => (by cases h), fun _ _ _ _ _ h => (by cases h), fun _ _ _ h => (by cases h), + fun _ _ _ h => (by cases h), trivial⟩ + · rw [hcap y (D.sub y hy) e]; exact J.j.capObj hy + have hr : ((), σ') ∈ (setCap (Z := Unit) (absCapA (.Zombie p zt k)) sl σ).1 := by rw [hset]; rfl + have hms : σ'.machineState = σ.machineState := setCap_msA _ _ σ _ hr + have fr := CapFromA.of_zombie (st := st) (OS := OS) (s := s) (s' := s') hc hcap + obtain ⟨ibk, tw, twH⟩ := ibW_keptA J.toJEntTA w + have hT : ∀ t ∈ Z.T, Z.th t = some .tcb := fun t ht => (J.j.1.thT t).2 ht + have cont : ContCapA (capA st s (cslotAddrA sl)) := by + by_cases h : IsZombieA (capA st s (cslotAddrA sl)) + · exact Or.inl h + · exact Or.inr (hnz h) + have hxB : cslotAddrA sl ∉ BufS H.tcbs := not_bufS_of_capA hBH hx (by + rcases cont with ⟨_, _, _, e⟩ | ⟨_, e⟩ | ⟨_, _, _, _, e⟩ + · rw [e]; exact not_bufCap_zombieA + · exact not_bufCap_tcbCnA (Or.inl ⟨_, e⟩) + · exact not_bufCap_tcbCnA (Or.inr ⟨_, _, _, _, e⟩)) + obtain ⟨Pt, Ub, vr, -, arm, -, -, -, -, link, -, -⟩ := id J.j7 + have ts : TcbSlotsOS H.tcbs OS := arm.case.ts + have bufkE : ∀ t ∈ Z.T, capA st s' (sbWordA t) = capA st s (sbWordA t) := by + intro t ht + have ht' : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + have hb : sbWordA t ∈ BufS H.tcbs := ⟨t, ht', rfl⟩ + exact hcap _ (sbWord_osA ts ht') (fun e => hxB (by rw [← e]; exact hb)) + have bufk : ∀ t ∈ Z.T, capA st s' (sbWordA t) = capA st s (sbWordA t) ∨ capA st s' (sbWordA t) = .Null := + fun t ht => Or.inl (bufkE t ht) + have mem : VsKeep st (L.ksb s) Pt s s' := vsKeep_capwA J.ent.heap ts vr w + have zok : ZOKA Z.th p zt k := D'.zw _ hxs p zt k hc + have cv : CapValidA Z.th (capA st s (cslotAddrA sl)) := D.cv _ hx + have same : ∀ p' zt' n', capA st s (cslotAddrA sl) = .Zombie p' zt' n' → p' = p ∧ zt' = zt ∧ k.val ≤ n'.val := by + intro p' zt' n' e + obtain ⟨a1, a2, a3⟩ := hzz p' zt' n' e + exact ⟨a1, a2, by rcases a3 with h | ⟨h, -⟩ <;> omega⟩ + have ptr : ¬ IsZombieA (capA st s (cslotAddrA sl)) → + ptrA (capA st s' (cslotAddrA sl)) = ptrA (capA st s (cslotAddrA sl)) := fun h => (M.cover h).ptr + have cov : ∀ kd q, NamesDA kd q (capA st s (cslotAddrA sl)) → NamesDA kd q (capA st s' (cslotAddrA sl)) := by + intro kd q hn + rw [hc] + cases kd + · have e : capA st s (cslotAddrA sl) = .Vcpu q := hn + rcases cont with ⟨_, _, _, h⟩ | ⟨_, h⟩ | ⟨_, _, _, _, h⟩ <;> (rw [e] at h; cases h) + · obtain ⟨g, e⟩ := hn + rcases cont with ⟨_, _, _, h⟩ | ⟨_, h⟩ | ⟨_, _, _, _, h⟩ <;> (rw [e] at h; cases h) + · obtain ⟨b, e⟩ := hn + rcases cont with ⟨_, _, _, h⟩ | ⟨_, h⟩ | ⟨_, _, _, _, h⟩ <;> (rw [e] at h; cases h) + · rcases hn with e | ⟨n', e⟩ + · have nz : ¬ IsZombieA (capA st s (cslotAddrA sl)) := by + rintro ⟨_, _, _, h⟩; rw [e] at h; cases h + have ep : p = q := by + have := ptr nz + rwa [hc, ptrA_zombie, e, ptrA_thA] at this + have hq : Z.th p = some .tcb := by + have := cv + rw [e] at this + rw [ep]; exact this + have ez : zt = capability.ZOMBIE_TCB := by + rcases zok with ⟨h65, -, -⟩ | ⟨-, h, -⟩ + · apply UScalar.eq_of_val_eq + rw [h65]; first | rfl | decide | (unfold capability.ZOMBIE_TCB; rfl) + · rw [hq] at h; cases h + exact Or.inr ⟨k, by rw [ep, ez]⟩ + · obtain ⟨rfl, rfl, -⟩ := same _ _ _ e + exact Or.inr ⟨k, rfl⟩ + have eqp : ¬ IsZombieA (capA st s (cslotAddrA sl)) → p = ptrA (capA st s (cslotAddrA sl)) := by + intro h + have := ptr h + rwa [hc, ptrA_zombie] at this + have vc : zt = capability.ZOMBIE_TCB → L.mem s (wa p 3664#u64) = 0#u64 := by + intro hzt + by_cases h : IsZombieA (capA st s (cslotAddrA sl)) + · obtain ⟨p', zt', n', e⟩ := h + obtain ⟨rfl, rfl, -⟩ := same p' zt' n' e + exact hZT _ hx p' n' (by rw [e, hzt]) + · rcases hnz h with ⟨q, e⟩ | ⟨q, rb, gs, g, e⟩ + · have ep : p = q := by rw [eqp h, e, ptrA_thA] + rw [ep] + exact hRz q e q e + · have ep : p = q := by rw [eqp h, e, ptrA_cnA] + rw [e] at cv + exact absurd hzt (cnode_not_tcbZombieA zok cv.1 ep) + have ztb : ZombieTcbTA st H OS s' := by + intro u hu q j e + by_cases eu : u = cslotAddrA sl + · rw [eu, hc] at e + injection e with e1 e2 e3 + rw [e1, e2, e3] at zok + rcases zok with ⟨-, hq, -⟩ | ⟨le, -, -⟩ + · rw [J.tcbs]; exact (J.j.1.thT q).1 hq + · have h65 : (capability.ZOMBIE_TCB : U64).val = 65 := by + first | rfl | decide | (unfold capability.ZOMBIE_TCB; rfl) + omega + · rw [hcap u hu eu] at e; exact J.zombieTcb u hu q j e + have old5 : IsTcbCnA (capA st s (cslotAddrA sl)) ∨ ∃ n', capA st s (cslotAddrA sl) = .Zombie p zt n' := by + by_cases h : IsZombieA (capA st s (cslotAddrA sl)) + · obtain ⟨p', zt', n', e⟩ := h + obtain ⟨rfl, rfl, -⟩ := same p' zt' n' e + exact Or.inr ⟨n', e⟩ + · exact Or.inl (hnz h) + have old7 : (∃ t, capA st s (cslotAddrA sl) = .Thread t) ∨ (∃ c rb gs g, capA st s (cslotAddrA sl) = .CNode c rb gs g) ∨ + IsZombieA (capA st s (cslotAddrA sl)) := by + rcases cont with h | h | h + · exact Or.inr (Or.inr h) + · exact Or.inl h + · exact Or.inr (Or.inl h) + have P : CdtProvA σ σ' (fun _ => False) := + CdtProvA.of_eq (setCap_cdtA _ _ σ _ hr).1 (setCap_cdtA _ _ σ _ hr).2 + have hQ : ∀ x : CslotPtr, False → AbsSlotA Z.th x := fun _ h => h.elim + have live : ∀ x, σ'.isOriginalCap x = true → AbsSlotA Z.th x := origLive_stepA P hQ J.origLive + have j5' := J5A.zombieA (Z := { Z with G := G, Q := Q, R := R, hw := hw }) (m' := m) J.j5 D D' hx hn hc hcap old5 M + w.1 (irqSt_keptA J.ent.heap ts arm.kb D.place J.ent.ksw w) + have zv : ZombieTcbNoVcpuA st OS s' := zombieNoVcpu_zombieA J.zombieNoVcpu ztb twH hx hc hcap vc + have zbnd : UtZombieA st OS s' := by + intro u hu q j z e + by_cases eu : u = cslotAddrA sl + · rw [eu, hc] at e + injection e with e1 e2 e3 + subst e1 e2 e3 + have h65 : (capability.ZOMBIE_TCB : U64).val = 65 := by + first | rfl | decide | (unfold capability.ZOMBIE_TCB; rfl) + rcases zok with ⟨h65', -, h7⟩ | ⟨le, hq, hn⟩ + · refine ⟨fun _ => ?_, fun hne => absurd (UScalar.eq_of_val_eq (by rw [h65', h65])) hne⟩ + unfold tcbSlotsA at h7; omega + · refine ⟨fun hT => ?_, fun hne => ?_⟩ + · rw [hT] at le; omega + · refine ⟨?_, hn⟩ + by_cases hz' : IsZombieA (capA st s (cslotAddrA sl)) + · obtain ⟨p', zt', n', e⟩ := hz' + obtain ⟨a1, a2, -⟩ := same p' zt' n' e + have := (J.zombieBnd _ hx p' zt' n' e).2 (by rw [a2]; exact hne) + rw [a2] at this; exact this.1 + · rcases hnz hz' with ⟨q0, e⟩ | ⟨q0, rb, gs, g, e⟩ + · have ep : p = q0 := by rw [eqp hz', e, ptrA_thA] + have hqT : Z.th p = some .tcb := by + have := cv; rw [e] at this; rw [ep]; exact this + rw [hqT] at hq; cases hq + · have ep : p = q0 := by rw [eqp hz', e, ptrA_cnA] + have wf : CapWfA (.CNode q0 rb gs g) := e ▸ D.toFinStA.wf hx + obtain ⟨hrb, -, -⟩ := wf + rw [e] at cv + have h1 : Z.th q0 = some (.cnode rb.val) := cv.1 + rw [← ep, hq] at h1 + have h2 : zt.val = rb.val := ObjKindA.cnode.inj (Option.some.inj h1) + omega + · rw [hcap u hu eu] at e; exact J.zombieBnd u hu q j z e + have J' := JStage3TA.of_slotW J (Set.Subset.refl _) w HS hms + (fun b h => F.off b (footNA_coreA (G := Z.G) hcore (footNA_loopA h))) + (G' := G) jx + (sinv_zombieA J.sinv D.place D.rel J.j.1.irq hA cont hr) + (J.sidle.setCapZombieA HS ⟨fun _ _ e => (by simp [absCapA] at e), fun _ e => (by simp [absCapA] at e), fun _ _ _ e => (by simp [absCapA] at e)⟩ + (J.sidle.neIdleSlotCapA (st := st) J.j.1.st hA (by + rcases cont with ⟨_, _, _, h⟩ | ⟨_, h⟩ | ⟨_, _, _, _, h⟩ <;> (rw [h]; exact fun e => by cases e))) hr) + (J.objAlloc.fromA fr (fun _ _ h => h)) + (J.idleAnchor.fromA fr) + (BufCover.stepA J.bufCover D.rel D'.rel hT HS ibk bufk) + (BufNativeA.stepA J.bufNative bufk) + (J.deadExt.stepA HS tw (vcRunW_keptA J.toJEntTA w) (DeadBackA.of_zombie cov hcap)) + (specFree_stepA J.specFree P D.rel hQ fr HS) + (J.utBlk.fromA fr) + (J.unmPt.stepA fr w.1 w.2.1 mem link HS) + (J.specZero.fromA fr (by rw [hms])) + live + (nullOrig_zombieA hA hset D.rel D'.rel D.place D.sub (by rw [hc]; intro h; cases h) hcap live J.nullOrig) + D'.fr + (J.devFr.fromA fr (fun _ _ _ h => HS.keep h (fun _ _ e => by cases e) (fun _ e => by cases e))) + D'.cov + zv + ztb + (BufOffA.fromA (Z := { Z with G := G, Q := Q, R := R, hw := hw }) fr w.1 rfl rfl rfl rfl rfl rfl J.bufOff + (fun bp h => (J.bufOff.pin bp h).keepC rfl rfl fun t ht _ => bufkE t ht)) + zbnd + (J.devUtOff.fromA (Z := { Z with G := G, Q := Q, R := R, hw := hw }) fr w.1 (fun _ _ h => h) rfl) + (fun u hu => by + by_cases eu : u = cslotAddrA sl + · rw [eu, hc] + exact ⟨fun _ _ _ _ _ _ h => (by cases h), fun _ _ _ _ h => (by cases h), fun _ _ h => (by cases h), + fun _ _ h => (by cases h), fun _ h => (by cases h)⟩ + · rw [hcap u hu eu]; exact J.capTy u hu) + (fun u hu q b e => by + by_cases eu : u = cslotAddrA sl + · rw [eu, hc] at e; cases e + · rw [hcap u hu eu] at e; exact J.ciSc u hu q b e) + j5' + (J.j7.zombieA J.ent.heap w hx hc hcap old7 (hiY_ofA D.toFinStA J.j)) + have X' : Φ Bf s' σ' := hΦ J X w HS (fun y hy => by + by_cases e : y = cslotAddrA sl + · exact Or.inr (Or.inl ⟨p, zt, k, by rw [e]; exact hc⟩) + · exact Or.inr (Or.inr ⟨y, hy, hcap y hy e⟩)) D'.zw hms + exact ⟨hcore.writes w, ⟨Q, R, hw, Ac, Bf, stack, J', X'⟩, j5'.1.1.parentAdj, zv, + bufHold_zombieA hBH hxB hcap, + ctOff_zombieA hCT w.1 D.place hx hc zok cv same hnz ptr hcap, by + have nx : ∀ t ∈ Z.T, cslotAddrA sl ≠ slotIdx t layout.tcb.SLOT_FAULT_HANDLER := + hFx.ne_fhA st (by + rintro (e | ⟨_, _, _, _, _, _, e, -⟩) <;> + rcases cont with ⟨_, _, _, h⟩ | ⟨_, h⟩ | ⟨_, _, _, _, h⟩ <;> (rw [e] at h; cases h)) + refine hFx.stepA st fun y hy => ?_ + by_cases e1 : y = cslotAddrA sl + · refine Or.inr (Or.inr ⟨e1 ▸ nx, Or.inr fun ptr asid va h => ?_⟩) + rw [e1, hc] at h + cases h + · exact Or.inl (hcap y (D.sub y hy) e1)⟩ + +/-- **A zombie written over the slot keeps the loop invariant.** -/ +theorem zombieHyp_delA (F : DelFootA st Z H.tcbs H.omega H.frame OS s0) : + ZombieHypA st H.tcbs Z.th OS + (DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st (J7R2A st ar ipa)))) + (DelRzA st) := + zombieHyp_delPA slotKeep_std6A F + +/-! ## The emptying -/ + +/-- **A capability that is not the last** leaves another `OS` slot with a range key cap of the same base and kind +(`isFinalCap_iffA`, `KindA`, `FrBaseApartA`). -/ +theorem other_of_nonfinalA {th : TypedHeapA} {G : List (List U64)} {s : S} {σ : State Unit} {sl : CslotPtr} + {c : capability.Cap} (F : FinStA st th OS G s σ) (hsl : AbsSlotA th sl) (hc : capA st s (cslotAddrA sl) = c) + (hk : RngKeyA c) (hn : ¬ isFinalCap' (absCapA c) σ) : + ∃ w ∈ OS, w ≠ cslotAddrA sl ∧ RngKeyA (capA st s w) ∧ ptrA (capA st s w) = ptrA c ∧ + kindA (capA st s w) = kindA c := by + have hx : cslotAddrA sl ∈ OS := F.sub _ (hsl.isSlot F.place) + have hwf : CapWfA c := hc ▸ F.wf hx + have habs : isFinalCap' (absCapA c) σ ↔ + ∀ w ∈ OS, w ≠ cslotAddrA sl → ¬ (RngKeyA (capA st s w) ∧ ptrA (capA st s w) = ptrA c) := by + refine isFinalCap_iffA F hsl hc (fun d => RngKeyA d ∧ ptrA d = ptrA c) (fun y hy => ?_) ⟨hk, rfl⟩ + (fun h => RngKeyA.not_null h.1) + rw [genRefs_keyA hk hwf, overlap_objA (F.wf hy), specPtr_iff] + refine ⟨fun ⟨h, e⟩ => ?_, fun ⟨h, e⟩ => ⟨Or.inl h, e⟩⟩ + rcases h with h | h + · exact ⟨h, e⟩ + · exact absurd (hc.symm ▸ e.symm : ptrA (capA st s (cslotAddrA sl)) = ptrA (capA st s y)) + (F.fr y hy _ hx h (hc.symm ▸ hk)) + rw [habs] at hn + simp only [not_forall, not_not] at hn + obtain ⟨w, hw, ne, kw, pw⟩ := hn + refine ⟨w, hw, ne, kw, pw, ?_⟩ + have := F.kind.kind w hw _ hx kw (hc.symm ▸ hk) (by rw [hc]; exact pw) + rwa [hc] at this + +theorem rngKey_asidPoolA (p b : U64) : RngKeyA (.AsidPool p b) := by + have h := physA_eq (.AsidPool p b) + simp only [objects.cap_is_physical] at h + exact ⟨(okinj h).symm, rfl, rfl⟩ + +theorem ptrA_asidPoolA (p b : U64) : ptrA (.AsidPool p b) = p := by + have h := ptrA_eq (.AsidPool p b) + simp only [capability.Cap.ptr] at h + exact (okinj h).symm + +theorem of_kindA_asidPoolA {c : capability.Cap} (h : kindA c = 21) : ∃ p b, c = .AsidPool p b := by + cases c <;> simp only [kindA] at h <;> first | exact ⟨_, _, rfl⟩ | omega + +/-- **A01 P1's `PoolEm` at the emptied slot**, from `Rd`'s ASID-table clause (a final pool) or another holder. -/ +theorem poolEm_ofA {a : A} {th : TypedHeapA} {G : List (List U64)} {s : S} {σ : State Unit} {sl : CslotPtr} + {Z' : XSetsA} {m : McState} {Rv : S → A → State Unit → MdbGhostA → U64 → Prop} {G' : MdbGhostA} + (F : FinStA st th OS G s σ) (hsl : AbsSlotA th sl) (J : JAtZAX (L := L) AR Z' s a σ m) + (R : DelRdA st Rv s a σ G' (cslotAddrA sl)) : PoolEm st OS s (cslotAddrA sl) := by + classical + intro p b e + by_cases hf : isFinalCap' (absCapA (capA st s (cslotAddrA sl))) σ + · refine Or.inl fun j hj => ?_ + by_contra h0 + have hi : (j &&& 127#u64).val < 128 := by + have : (j &&& 127#u64).val = j.val % 128 := by + exact and_maskA j 7 _ rfl + omega + have ht := J.1.vs.table _ hi + have ea : atAF (L.ksb s) (j &&& 127#u64) = wa (L.ksb s) (AtOff j) := rfl + rw [ea, hj] at ht + have : optPtrA p = some (specPtr p) := by + unfold optPtrA; rw [if_neg h0] + rw [this] at ht + exact R.2.1 p b e hf _ ht + · obtain ⟨w, hw, ne, -, pw, kw⟩ := other_of_nonfinalA F hsl e (rngKey_asidPoolA p b) (e ▸ hf) + obtain ⟨q, b', eq⟩ := of_kindA_asidPoolA (kw.trans rfl) + rw [eq, ptrA_asidPoolA, ptrA_asidPoolA] at pw + exact Or.inr ⟨w, hw, ne, b', by rw [eq, pw]⟩ + +variable (st) in +/-- **The VCPU clause of `Rd`** (Architect ruling 2026-12-03, contract §35; producer S4, as part of its `Rv`): at a +VCPU slot the spec VCPU has no TCB, or another `OS` slot holds a cap naming it. -/ +def RvVcA (OS : Set U64) (Rv : S → A → State Unit → MdbGhostA → U64 → Prop) : + S → A → State Unit → MdbGhostA → U64 → Prop := fun s a σ G x => + (∀ v, capA st s x = .Vcpu v → + ((∀ y, σ.kheap (specPtr v) = some (.archObj (.vcpu y)) → y.vcpuTcb = none) ∧ + L.mem s (wa v 24#u64) = 0#u64) ∨ + ∃ w ∈ OS, w ≠ x ∧ capA st s w = .Vcpu v) ∧ Rv s a σ G x + +theorem rngKey_replyA (p : U64) (g : Bool) : RngKeyA (.Reply p g) := by + have h := physA_eq (.Reply p g) + simp only [objects.cap_is_physical] at h + exact ⟨(okinj h).symm, rfl, rfl⟩ + +theorem ptrA_replyA (p : U64) (g : Bool) : ptrA (.Reply p g) = p := by + have h := ptrA_eq (.Reply p g) + simp only [capability.Cap.ptr] at h + exact (okinj h).symm + +theorem of_kindA_replyA {c : capability.Cap} (h : kindA c = 4) : ∃ p g, c = .Reply p g := by + cases c <;> simp only [kindA] at h <;> first | exact ⟨_, _, rfl⟩ | omega + +set_option maxHeartbeats 1000000 in +/-- **`deadExt` after the emptying** (module docstring): an object the removed cap names is dead afterwards only if +that cap was its last typed namer, and then `Rd` / `ZombieTcbNoVcpuA` give the clause. -/ +theorem emptyDead_delA {Rv : S → A → State Unit → MdbGhostA → U64 → Prop} {s s' : S} {a : A} {σ σ' : State Unit} + {G : MdbGhostA} {sl : CslotPtr} {Q : U64 → U64 → List U64} {R : U64 → List U64} {hw : HwRootA} {Ac : Set U64} + {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {m : McState} + (J : JStage3TA st AR { Z with G := G, Q := Q, R := R, hw := hw } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n + (J5A st) (J7LA st J7R2tA) s a σ m) + (hRd : DelRdA st (RvVcA st OS Rv) s a σ G (cslotAddrA sl)) (hTh : RdThA st OS s (cslotAddrA sl)) + (D : DStateA st Z.th OS s σ G) (hZT : ZombieTcbNoVcpuA st OS s) (hA : AbsSlotA Z.th sl) + (w : WritesIn st s s' (W7.CapW H.tcbs {b | InOS OS b} ∅)) (HS : SpecSlotA σ σ') + (hcap : ∀ y ∈ OS, y ≠ cslotAddrA sl → capA st s' y = capA st s y) : + DeadExtA st { Z with G := remGA G (cslotAddrA sl), Q := Q, R := R, hw := hw } OS Rp stack s' σ' := by + classical + obtain ⟨-, tw, -⟩ := ibW_keptA J.toJEntTA w + have hx : cslotAddrA sl ∈ OS := D.mem_slot hA + have surv : ∀ (k : DKindA) (p y : U64), y ∈ OS → y ≠ cslotAddrA sl → NamesDA k p (capA st s y) → + DeadA st OS k s' p → False := fun k p y hy ne n d => d y hy (by rw [hcap y hy ne]; exact n) + refine J.deadExt.emptyA (G' := remGA G (cslotAddrA sl)) HS tw (vcRunW_keptA J.toJEntTA w) hcap + (fun v _ n d y e => ?_) (fun q _ n d c hc => ?_) + (fun i p e n d => ?_) (fun t ht n d => ?_) (fun v _ n d => ?_) (fun q hq n d y e => ?_) + · have n' : capA st s (cslotAddrA sl) = .Vcpu v := n + rcases (hRd.2.2 v n').1 v n' with h | ⟨z, hz, ne, ez⟩ + · exact h.1 y e + · exact (surv .vcpu v z hz ne ez d).elim + · obtain ⟨g, n⟩ := n + by_cases hf : isFinalCap' (absCapA (capA st s (cslotAddrA sl))) σ + · have clean := (hRd.1 q g n hf).1 + intro hq + obtain ⟨⟨x, k, hk, er⟩, -⟩ := J.reply.stackRel c hc + exact clean _ x k hk (by rw [er]; exact List.mem_map.2 ⟨q, hq, rfl⟩) + · obtain ⟨z, hz, ne, -, pz, kz⟩ := other_of_nonfinalA D.toFinStA hA n (rngKey_replyA q g) (n ▸ hf) + obtain ⟨q', g', ez⟩ := of_kindA_replyA (kz.trans rfl) + rw [ez, ptrA_replyA, ptrA_replyA] at pz + exact (surv .reply q z hz ne ⟨g', by rw [ez, pz]⟩ d).elim + · obtain ⟨b, n⟩ := n + by_cases hf : isFinalCap' (absCapA (capA st s (cslotAddrA sl))) σ + · exact hRd.2.1 p b n hf i e + · obtain ⟨z, hz, ne, -, pz, kz⟩ := other_of_nonfinalA D.toFinStA hA n (rngKey_asidPoolA p b) (n ▸ hf) + obtain ⟨q', b', ez⟩ := of_kindA_asidPoolA (kz.trans rfl) + rw [ez, ptrA_asidPoolA, ptrA_asidPoolA] at pz + exact surv .pool p z hz ne ⟨b', by rw [ez, pz]⟩ d + · rcases n with n | ⟨j, n⟩ + · obtain ⟨z, hz, ne, nz⟩ := hTh t n + exact (surv .tcb t z hz ne nz d).elim + · exact hZT _ hx t j n + · have n' : capA st s (cslotAddrA sl) = .Vcpu v := n + rcases (hRd.2.2 v n').1 v n' with h | ⟨z, hz, ne, ez⟩ + · exact h.2 + · exact (surv .vcpu v z hz ne ez d).elim + · obtain ⟨g, n⟩ := n + by_cases hf : isFinalCap' (absCapA (capA st s (cslotAddrA sl))) σ + · have clean := (hRd.1 q g n hf).2 + obtain ⟨x, hk, er⟩ := J.ep.reply q hq + rw [e] at hk + cases hk + rw [er, clean] + rfl + · obtain ⟨z, hz, ne, -, pz, kz⟩ := other_of_nonfinalA D.toFinStA hA n (rngKey_replyA q g) (n ▸ hf) + obtain ⟨q', g', ez⟩ := of_kindA_replyA (kz.trans rfl) + rw [ez, ptrA_replyA, ptrA_replyA] at pz + exact (surv .reply q z hz ne ⟨g', by rw [ez, pz]⟩ d).elim + +set_option maxHeartbeats 1000000 in +/-- **A ready non-null slot emptied keeps the loop invariant** and restores R2 over every slot (with `Φ`). -/ +theorem emptyHyp_delPA {Rv : S → A → State Unit → MdbGhostA → U64 → Prop} + {Φ : Set (U64 × Fl4SpecA64.Word) → S → State Unit → Prop} + (hΦ : SlotKeepPA st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n Φ) + (hmono : ∀ (Bf Bf' : Set (U64 × Fl4SpecA64.Word)) (s : S) (σ : State Unit), Bf' ⊆ Bf → Φ Bf s σ → Φ Bf' s σ) + (F : DelFootA st Z H.tcbs H.omega H.frame OS s0) : + EmptyHypA st H.tcbs Z.th OS + (DelJWQA st s0 (DelXPA st Φ AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st (J7R2A st ar ipa)))) + (DelJWQA st s0 (DelXPA st Φ AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st J7R2tA))) + (DelRdXA st ar Z H OS ipa (RvVcA st OS Rv)) := by + intro s s' a σ σ' G sl hW hRd D D' w HS hm hA hn h0 hcap M + obtain ⟨hcore, ⟨Q, R, hw, Ac, Bf, stack, J, X⟩, -, hZT, hBH, hCT, hFx⟩ := hW + obtain ⟨⟨hRdA, hTh⟩, m', j7x⟩ := hRd + have dead := emptyDead_delA J hRdA hTh D hZT hA w HS hcap + have hx : cslotAddrA sl ∈ OS := D.mem_slot hA + have kb : L.ksb s = L.ksb s0 := hcore.1 + have jx : JAtZAX (L := L) AR { Z with G := remGA G (cslotAddrA sl), Q := Q, R := R, hw := hw } s' a σ' m := by + refine (delFoot_loopA F G Q R hw).slotWrites hcore J.j w HS D'.rel D'.cdt fun y hy => ?_ + show CapObjOkA _ (capA st s' y) + by_cases e : y = cslotAddrA sl + · rw [e, h0]; exact CapObjOkA.null _ + · rw [hcap y (D.sub y hy) e]; exact J.j.capObj hy + have hms : σ'.machineState = σ.machineState := emptySlot_msA _ σ _ hm + have fr := CapFromA.of_empty (st := st) (OS := OS) (s := s) (s' := s') h0 hcap + obtain ⟨ibk, -, twH⟩ := ibW_keptA J.toJEntTA w + have hT : ∀ t ∈ Z.T, Z.th t = some .tcb := fun t ht => (J.j.1.thT t).2 ht + have j7x' : J7LA st (J7R2xA st ar ipa (cslotAddrA sl)) { Z with G := G, Q := Q, R := R, hw := hw } H OS s σ m := j7x + obtain ⟨Pt, Ub, vr, -, arm, capc, -, -, -, link, -, -⟩ := id j7x' + have ts : TcbSlotsOS H.tcbs OS := arm.case.ts + have bufk : ∀ t ∈ Z.T, capA st s' (sbWordA t) = capA st s (sbWordA t) ∨ capA st s' (sbWordA t) = .Null := by + intro t ht + have ht' : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + by_cases e : sbWordA t = cslotAddrA sl + · exact Or.inr (by rw [e]; exact h0) + · exact Or.inl (hcap _ (sbWord_osA ts ht') e) + have mem : VsKeep st (L.ksb s) Pt s s' := vsKeep_capwA J.ent.heap ts vr w + have P : CdtProvA σ σ' (fun _ => False) := emptySlot_provA hm + have hQ : ∀ x : CslotPtr, False → AbsSlotA Z.th x := fun _ h => h.elim + have live : ∀ x, σ'.isOriginalCap x = true → AbsSlotA Z.th x := origLive_stepA P hQ J.origLive + have src : ∀ u ∈ OS, ∀ q j, capA st s' u = .Zombie q capability.ZOMBIE_TCB j → + ∃ z ∈ OS, capA st s z = .Zombie q capability.ZOMBIE_TCB j := by + intro u hu q j e + by_cases eu : u = cslotAddrA sl + · rw [eu, h0] at e; cases e + · exact ⟨u, hu, by rw [← hcap u hu eu]; exact e⟩ + have zv : ZombieTcbNoVcpuA st OS s' := zombieNoVcpu_keepA J.zombieNoVcpu J.zombieTcb twH src + have j5' := J5A.emptyA (Z := { Z with G := G, Q := Q, R := R, hw := hw }) (m' := m) J.j5 D D' hx hn hcap M w.1 + (irqSt_keptA J.ent.heap ts arm.kb D.place J.ent.ksw w) + have pe : PoolEm st OS s (cslotAddrA sl) := poolEm_ofA D.toFinStA hA J.j hRdA + have j7' := j7x'.emptyA (G' := remGA G (cslotAddrA sl)) (σ' := σ') (m' := m) J.ent.heap w h0 hcap pe + (hiY_ofA D.toFinStA J.j) + -- (12′) a nulled buffer slot unpins its buffer: the tracked set shrinks to the pinned buffers + have sbO : ∀ t ∈ Z.T, sbWordA t ∈ OS := fun t ht => sbWord_osA ts (by rw [J.tcbs]; exact ht) + have cov' := BufCover.pinA J.ent.heap capc.2 (BufCover.stepA J.bufCover D.rel D'.rel hT HS ibk bufk) sbO bufk + J.bufOff.pin + have sub' : {bp | bp ∈ Bf ∧ BufPinA st Z.T OS s' bp.1} ⊆ Bf := fun _ h => h.1 + have bufOff0 : BufOffA st { Z with G := remGA G (cslotAddrA sl), Q := Q, R := R, hw := hw } OS s' + {bp | bp ∈ Bf ∧ BufPinA st Z.T OS s' bp.1} := by + have bw : ∀ b, BufW {bp | bp ∈ Bf ∧ BufPinA st Z.T OS s' bp.1} b → BufW Bf b := + fun b ⟨bp, h, r⟩ => ⟨bp, h.1, r⟩ + exact ⟨fun b h => J.bufOff.os b (bw b h), fun b h => by rw [w.1]; exact J.bufOff.vs b (bw b h), + fun b h p hp => J.bufOff.page b (bw b h) p (SchedPageA.fromA fr rfl rfl rfl rfl rfl hp), + fun b h => J.bufOff.vcw b (bw b h), fun bp h => h.2⟩ + have J' := JStage3TA.of_slotW (Rj := J7R2tA) J sub' w HS hms + (fun b h => F.off b (footNA_coreA (G := Z.G) hcore (footNA_loopA h))) + (G' := remGA G (cslotAddrA sl)) jx + (J.sinv.emptySlot (r := ((), σ')) hm) + (J.sidle.emptySlotA HS hm) + (J.objAlloc.fromA fr (fun _ _ h => h)) + (J.idleAnchor.fromA fr) + cov' + (BufNativeA.stepA J.bufNative bufk) + dead + (specFree_stepA J.specFree P D.rel hQ fr HS) + (J.utBlk.fromA fr) + (J.unmPt.stepA fr w.1 w.2.1 mem link HS) + (J.specZero.fromA fr (by rw [hms])) + live + (nullOrig_emptyA hA hm D.rel D'.rel D.place D.sub hn hcap live J.nullOrig) + D'.fr + (J.devFr.fromA fr (fun _ _ _ h => HS.keep h (fun _ _ e => by cases e) (fun _ e => by cases e))) + D'.cov + zv + (fun u hu q j e => by + obtain ⟨z, hz, ez⟩ := src u hu q j e + exact J.zombieTcb z hz q j ez) + bufOff0 + (J.zombieBnd.ofSrcA (fun u hu q j z e => by + by_cases eu : u = cslotAddrA sl + · rw [eu, h0] at e; cases e + · exact ⟨u, hu, (hcap u hu eu).symm.trans e⟩)) + (J.devUtOff.fromA (Z := { Z with G := G, Q := Q, R := R, hw := hw }) fr w.1 (fun _ _ h => h) rfl) + (fun u hu => by + by_cases eu : u = cslotAddrA sl + · rw [eu, h0] + exact ⟨fun _ _ _ _ _ _ h => (by cases h), fun _ _ _ _ h => (by cases h), fun _ _ h => (by cases h), + fun _ _ h => (by cases h), fun _ h => (by cases h)⟩ + · rw [hcap u hu eu]; exact J.capTy u hu) + (fun u hu q b e => by + by_cases eu : u = cslotAddrA sl + · rw [eu, h0] at e; cases e + · rw [hcap u hu eu] at e; exact J.ciSc u hu q b e) + j5' + (j7'.mapR fun _ _ => trivial) + have X' : Φ Bf s' σ' := hΦ J X w HS (fun y hy => by + by_cases e : y = cslotAddrA sl + · exact Or.inl (by rw [e]; exact h0) + · exact Or.inr (Or.inr ⟨y, hy, hcap y hy e⟩)) D'.zw hms + exact ⟨hcore.writes w, ⟨Q, R, hw, Ac, _, stack, { J' with j7 := j7' }, hmono _ _ _ _ sub' X'⟩, j5'.1.1.parentAdj, + zombieNoVcpu_keepA hZT J.zombieTcb twH src, bufHold_emptyA hBH h0 hcap, ctOff_emptyA hCT w.1 h0 hcap, + hFx.stepA st fun y hy => by + by_cases e1 : y = cslotAddrA sl + · exact Or.inr (Or.inl (by rw [e1]; exact h0)) + · exact Or.inl (hcap y (D.sub y hy) e1)⟩ + +/-- **A ready non-null slot emptied keeps the loop invariant** and restores R2 over every slot. -/ +theorem emptyHyp_delA {Rv : S → A → State Unit → MdbGhostA → U64 → Prop} + (F : DelFootA st Z H.tcbs H.omega H.frame OS s0) : + EmptyHypA st H.tcbs Z.th OS + (DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st (J7R2A st ar ipa)))) + (DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st J7R2tA))) + (DelRdXA st ar Z H OS ipa (RvVcA st OS Rv)) := + emptyHyp_delPA slotKeep_std6A std6A_monoA F + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelStepKeepA.lean b/proofs/Fl4ProofsA64/P3/DelStepKeepA.lean new file mode 100644 index 000000000..6013d3fc5 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelStepKeepA.lean @@ -0,0 +1,250 @@ +/- +P3 (AArch64, delete hooks E4): **the unmapped-object, dead-object and zombie-VCPU fields of the per-step record across +the loop's three slot steps**. + +* **`UnmPtSpecA.stepA`**: when every cap after is null, a zombie or a cap from before, the unmapped VSpace objects stay + unmapped (their words are `VsR` words, kept by the slot write; their spec objects are not CNodes or TCBs). +* **`tcbFld_keptA`**: the TCB fields from 224 are kept by the slot write. +* **`DeadBackA`**, `DeadBackA.of_capTo`, `DeadBackA.of_zombie`: no object dies across a swap or a zombie write + (typed namers, contract §35). +* **`DeadExtA.stepA`** (`deadExt` when no object dies) and **`DeadExtA.emptyA`** (`deadExt` at the emptying, given the + four facts at an object the removed cap names). +* **`zombieNoVcpu_keepA`** (swap, emptying) and **`zombieNoVcpu_zombieA`** (zombie write). + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.DelSlotWA +import Fl4ProofsA64.P3.DelJ7StepA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 Fl4ProofsA64.P3.Endp + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {Z : XSetsA} {H : Heap} {OS : Set U64} {s r : S} {σ σ' : State Unit} {m : McState} {G' : MdbGhostA} + +/-! ## The unmapped objects -/ + +theorem ptW_peA {Pt : Set U64} {p i : U64} (hp : p ∈ Pt) (hi : i.val < 512) : PtW Pt (peAF p i) := by + have e8 : (8#u64 : U64).val = 8 := rfl + have hm : (core.num.U64.wrapping_mul i 8#u64).val = i.val * 8 := by + simp only [core.num.U64.wrapping_mul_val_eq, U64.size_def, U64.numBits, e8] + have hs : UScalar.size UScalarTy.U64 = 18446744073709551616 := by + first | rfl | decide | (simp [U64.size_def, U64.numBits]; done) | + (simp [UScalar.size, UScalarTy.numBits, U64.numBits]; done) | (simp [U64.size, U64.numBits]; done) + simp only [hs]; omega + exact ⟨p, hp, core.num.U64.wrapping_mul i 8#u64, by omega, by omega, rfl⟩ + +/-- **J9 across a slot-only step** whose caps after are null, zombies or caps from before. -/ +theorem UnmPtSpecA.stepA {Pt : Set U64} (h : UnmPtSpecA st Z m OS s σ) + (fr : CapFromA st OS s r) (hk : L.ksb r = L.ksb s) (hc : L.cpu r = L.cpu s) + (mem : VsKeep st (L.ksb s) Pt s r) (link : ∀ p, SchedPageA st Z OS s p → p ∈ Pt) + (HS : SpecSlotA σ σ') : UnmPtSpecA st { Z with G := G' } m OS r σ' := by + have word : ∀ p i : U64, p ∈ Pt → i.val < 512 → L.mem r (peAF p i) = L.mem s (peAF p i) := + fun p i hp hi => mem _ (Or.inl (ptW_peA hp hi)) + have obj : ∀ {p : ObjRef} {o : ArchKernelObj}, σ.kheap p = some (.archObj o) → σ'.kheap p = some (.archObj o) := + fun e => HS.keep e (fun _ _ h => by cases h) (fun _ h => by cases h) + have pg : ∀ {u : U64}, u ∈ OS → ∀ p : U64, + ((∃ m a v, capA st s u = .PageTable p m a v) ∨ (∃ m a, capA st s u = .Pml4 p m a) ∨ + (∃ a v, capA st s u = .Ept p 0#u64 false a v) ∨ + (∃ q a v, capA st s u = .Ept q 0#u64 false a v ∧ p = wa q 4096#u64) ∨ + ∃ y a, capA st s u = .HighVSpace y false a ∧ p = wa y 4096#u64) → p ∈ Pt := by + intro u hu p h + refine link p ?_ + unfold SchedPageA + exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ⟨u, hu, h⟩)))))) + have tab : ∀ ty p, p ∈ Pt → UnmTabA (L := L) st Z m s σ ty p → + UnmTabA (L := L) st { Z with G := G' } m r σ' ty p := by + intro ty p hp U + obtain ⟨t, e, ety, inv⟩ := U.spec + refine ⟨fun i hi => by rw [word p i hp hi]; exact U.inv i hi, ⟨t, obj e, ety, inv⟩, + ⟨fun i hi h => U.off.mem i hi + (footA_schedA (Z := Z) (Q := Z.Q) (Rl := Z.R) (curOf := Z.curOf) (curScOf := Z.curScOf) hk hc + (FootA.monoA (Z := { { Z with G := G' } with S1 := ∅ }) rfl rfl (fun _ h => h) (fun _ h => h) + (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) h)), + U.off.obj⟩⟩ + have src : ∀ u ∈ OS, ∀ c, capA st r u = c → c ≠ .Null → ¬ IsZombieA c → ∃ z ∈ OS, capA st s z = c := + fun u hu c e hn hz => fr.src hu e hn hz + refine { pt := ?_, vs := ?_, s2 := ?_, hv := ?_ } + · intro u hu p asid va e + obtain ⟨z, hz, ez⟩ := src u hu _ e (by intro h'; cases h') (by rintro ⟨_, _, _, h'⟩; cases h') + exact tab _ p (pg hz p (Or.inl ⟨_, _, _, ez⟩)) (h.pt z hz p asid va ez) + · intro u hu p asid e + obtain ⟨z, hz, ez⟩ := src u hu _ e (by intro h'; cases h') (by rintro ⟨_, _, _, h'⟩; cases h') + exact tab _ p (pg hz p (Or.inr (Or.inl ⟨_, _, ez⟩))) (h.vs z hz p asid ez) + · intro u hu p asid va e + obtain ⟨z, hz, ez⟩ := src u hu _ e (by intro h'; cases h') (by rintro ⟨_, _, _, h'⟩; cases h') + have U := h.s2 z hz p asid va ez + obtain ⟨es, e1, inv⟩ := U.spec + refine ⟨fun i hi => ?_, es, obj e1, inv⟩ + by_cases h5 : i.val < 512 + · rw [word p i (pg hz p (Or.inr (Or.inr (Or.inl ⟨_, _, ez⟩)))) h5]; exact U.inv i hi + · obtain ⟨j, hj, ej⟩ := peAF_hiA p i (by omega) hi + have hp : wa p 4096#u64 ∈ Pt := pg hz _ (Or.inr (Or.inr (Or.inr (Or.inl ⟨p, _, _, ez, rfl⟩)))) + have := U.inv i hi + rw [ej] at this ⊢ + rw [word _ j hp hj]; exact this + · intro u hu p asid e + obtain ⟨z, hz, ez⟩ := src u hu _ e (by intro h'; cases h') (by rintro ⟨_, _, _, h'⟩; cases h') + have U := h.hv z hz p asid ez + obtain ⟨l, e1, inv⟩ := U.spec + have hp : wa p 4096#u64 ∈ Pt := pg hz _ (Or.inr (Or.inr (Or.inr (Or.inr ⟨p, _, ez, rfl⟩)))) + exact ⟨fun i hi => by rw [word _ i hp hi]; exact U.inv i hi, l, obj e1, inv⟩ + +/-! ## The dead objects -/ + +variable (st) in +/-- An object dead after the step (as an object of some kind) was dead before. -/ +def DeadBackA (OS : Set U64) (s r : S) : Prop := ∀ k p, DeadA st OS k r p → DeadA st OS k s p + +theorem DeadBackA.of_capTo (h : CapToA st OS s r) : DeadBackA st OS s r := fun _ _ hd => DeadA.backA h hd + +/-- The zombie write at `x`: the new cap names (typed) what the old one did. -/ +theorem DeadBackA.of_zombie {x : U64} + (cov : ∀ k p, NamesDA k p (capA st s x) → NamesDA k p (capA st r x)) + (hcap : ∀ y ∈ OS, y ≠ x → capA st r y = capA st s y) : DeadBackA st OS s r := fun k p hd u hu hn => by + by_cases e : u = x + · subst e; exact hd u hu (cov k p hn) + · exact hd u hu (by rw [hcap u hu e]; exact hn) + +variable {Rp : Set U64} {stack : U64 → List U64} + +/-- **`deadExt` across a slot-only step**, from the four facts at the objects dead after. -/ +theorem DeadExtA.ofA (HS : SpecSlotA σ σ') (tw : ∀ t ∈ Z.T, L.mem r (wa t 3664#u64) = L.mem s (wa t 3664#u64)) + (hv : ∀ v ∈ Z.V, DeadA st OS .vcpu r v → ∀ x, σ.kheap (specPtr v) = some (.archObj (.vcpu x)) → x.vcpuTcb = none) + (hr : ∀ q ∈ Rp, DeadA st OS .reply r q → ∀ c ∈ Z.C, q ∉ stack c) + (ha : ∀ i p, σ.archState.armAsidTable i = some (specPtr p) → ¬ DeadA st OS .pool r p) + (ht : ∀ t ∈ Z.T, DeadA st OS .tcb r t → L.mem s (wa t 3664#u64) = 0#u64) + (vw : ∀ v ∈ Z.V, L.mem r (wa v 24#u64) = L.mem s (wa v 24#u64)) + (hd24 : ∀ v ∈ Z.V, DeadA st OS .vcpu r v → L.mem s (wa v 24#u64) = 0#u64) + (hrt : ∀ q ∈ Rp, DeadA st OS .reply r q → ∀ y, σ.kheap (specPtr q) = some (.reply y) → y.replyTcb = none) : + DeadExtA st { Z with G := G' } OS Rp stack r σ' where + vcpu v hvm hd x e := hv v hvm hd x (HS.back e (fun _ _ h => by cases h) (fun _ h => by cases h)) + reply := hr + asid i p e := ha i p (by rw [← HS.asid]; exact e) + tvcpu t htm hd := (tw t htm).trans (ht t htm hd) + vcDeadRun v hvm hd := (vw v hvm).trans (hd24 v hvm hd) + replyTcb q hq hd y e := hrt q hq hd y (HS.back e (fun _ _ h => by cases h) (fun _ h => by cases h)) + +/-- No object dies (swap, zombie write). -/ +theorem DeadExtA.stepA (o : DeadExtA st Z OS Rp stack s σ) (HS : SpecSlotA σ σ') + (tw : ∀ t ∈ Z.T, L.mem r (wa t 3664#u64) = L.mem s (wa t 3664#u64)) + (vw : ∀ v ∈ Z.V, L.mem r (wa v 24#u64) = L.mem s (wa v 24#u64)) (h : DeadBackA st OS s r) : + DeadExtA st { Z with G := G' } OS Rp stack r σ' := + DeadExtA.ofA HS tw (fun v hv hd => o.vcpu v hv (h _ _ hd)) + (fun q hq hd => o.reply q hq (h _ _ hd)) + (fun i p e hd => o.asid i p e (h _ _ hd)) + (fun t ht hd => o.tvcpu t ht (h _ _ hd)) vw (fun v hv hd => o.vcDeadRun v hv (h _ _ hd)) + (fun q hq hd => o.replyTcb q hq (h _ _ hd)) + +/-- An object dead after the emptying of `x` was dead before, or was named by `x`'s cap. -/ +theorem DeadA.of_emptyA {x p : U64} {k : DKindA} (hcap : ∀ y ∈ OS, y ≠ x → capA st r y = capA st s y) + (hd : DeadA st OS k r p) : DeadA st OS k s p ∨ NamesDA k p (capA st s x) := by + by_cases h : NamesDA k p (capA st s x) + · exact Or.inr h + refine Or.inl fun u hu hn => ?_ + by_cases ex : u = x + · subst ex; exact h hn + · exact hd u hu (by rw [hcap u hu ex]; exact hn) + +/-- **`deadExt` at the emptying of `x`**: only an object `x`'s cap names can die. -/ +theorem DeadExtA.emptyA {x : U64} (o : DeadExtA st Z OS Rp stack s σ) (HS : SpecSlotA σ σ') + (tw : ∀ t ∈ Z.T, L.mem r (wa t 3664#u64) = L.mem s (wa t 3664#u64)) + (vw : ∀ v ∈ Z.V, L.mem r (wa v 24#u64) = L.mem s (wa v 24#u64)) + (hcap : ∀ y ∈ OS, y ≠ x → capA st r y = capA st s y) + (hv : ∀ v ∈ Z.V, NamesDA .vcpu v (capA st s x) → DeadA st OS .vcpu r v → + ∀ y, σ.kheap (specPtr v) = some (.archObj (.vcpu y)) → y.vcpuTcb = none) + (hr : ∀ q ∈ Rp, NamesDA .reply q (capA st s x) → DeadA st OS .reply r q → ∀ c ∈ Z.C, q ∉ stack c) + (ha : ∀ i p, σ.archState.armAsidTable i = some (specPtr p) → NamesDA .pool p (capA st s x) → + ¬ DeadA st OS .pool r p) + (ht : ∀ t ∈ Z.T, NamesDA .tcb t (capA st s x) → DeadA st OS .tcb r t → L.mem s (wa t 3664#u64) = 0#u64) + (hd24 : ∀ v ∈ Z.V, NamesDA .vcpu v (capA st s x) → DeadA st OS .vcpu r v → L.mem s (wa v 24#u64) = 0#u64) + (hrt : ∀ q ∈ Rp, NamesDA .reply q (capA st s x) → DeadA st OS .reply r q → + ∀ y, σ.kheap (specPtr q) = some (.reply y) → y.replyTcb = none) : + DeadExtA st { Z with G := G' } OS Rp stack r σ' := by + refine DeadExtA.ofA HS tw (fun v hvm hd => ?_) (fun q hq hd => ?_) (fun i p e hd => ?_) (fun t htm hd => ?_) vw + (fun v hvm hd => ?_) (fun q hq hd => ?_) + · rcases DeadA.of_emptyA hcap hd with h | n + · exact o.vcpu v hvm h + · exact hv v hvm n hd + · rcases DeadA.of_emptyA hcap hd with h | n + · exact o.reply q hq h + · exact hr q hq n hd + · rcases DeadA.of_emptyA hcap hd with h | n + · exact o.asid i p e h + · exact ha i p e n hd + · rcases DeadA.of_emptyA hcap hd with h | n + · exact o.tvcpu t htm h + · exact ht t htm n hd + · rcases DeadA.of_emptyA hcap hd with h | n + · exact o.vcDeadRun v hvm h + · exact hd24 v hvm n hd + · rcases DeadA.of_emptyA hcap hd with h | n + · exact o.replyTcb q hq h + · exact hrt q hq n hd + +/-! ## TCB zombies have no VCPU -/ + +/-- Swap and emptying: a zombie cap after was a cap before. -/ +theorem zombieNoVcpu_keepA (o : ZombieTcbNoVcpuA st OS s) (zt : ZombieTcbTA st H OS s) + (tw : ∀ t ∈ H.tcbs, L.mem r (wa t 3664#u64) = L.mem s (wa t 3664#u64)) + (src : ∀ u ∈ OS, ∀ p n, capA st r u = .Zombie p capability.ZOMBIE_TCB n → + ∃ z ∈ OS, capA st s z = .Zombie p capability.ZOMBIE_TCB n) : + ZombieTcbNoVcpuA st OS r := fun u hu p n e => by + obtain ⟨z, hz, ez⟩ := src u hu p n e + exact (tw p (zt z hz p n ez)).trans (o z hz p n ez) + +/-- The zombie write at `x`: a new TCB zombie replaces a TCB zombie of the same TCB or a thread with no VCPU. -/ +theorem zombieNoVcpu_zombieA {x q zt n : U64} (o : ZombieTcbNoVcpuA st OS s) (ztb : ZombieTcbTA st H OS r) + (tw : ∀ t ∈ H.tcbs, L.mem r (wa t 3664#u64) = L.mem s (wa t 3664#u64)) (hx : x ∈ OS) + (new : capA st r x = .Zombie q zt n) (hcap : ∀ y ∈ OS, y ≠ x → capA st r y = capA st s y) + (old : zt = capability.ZOMBIE_TCB → L.mem s (wa q 3664#u64) = 0#u64) : + ZombieTcbNoVcpuA st OS r := fun u hu p k e => by + refine (tw p (ztb u hu p k e)).trans ?_ + by_cases ex : u = x + · subst ex + rw [new] at e + injection e with e1 e2 e3 + subst e1 + exact old e2 + · rw [hcap u hu ex] at e + exact o u hu p k e + +end + +/-! ## The TCB fields -/ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} {ia ie : Nat} + {s r : S} {a : A} {σ : State Unit} {m : McState} + +/-- The VCPU `RUN_CALLER` words are kept by a write into the slot words (`SlotApA.vc`). -/ +theorem vcRunW_keptA (J : JEntTA st AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + (w : WritesIn st s r (W7.CapW H.tcbs {b | InOS OS b} ∅)) : + ∀ v ∈ Z.V, L.mem r (wa v 24#u64) = L.mem s (wa v 24#u64) := fun v hv => + w.mem_eq (vcAl24A (J.ap.vcAl v hv)) (fun hp => hp.elim (J.ap.vc v hv _ (Or.inr rfl)).2 + (fun hp => (J.ap.vc v hv _ (Or.inr rfl)).1 hp.1)) + +/-- The TCB fields from 224 are kept by a write into the slot words. -/ +theorem tcbFld_keptA (J : JEntTA st AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + (w : WritesIn st s r (W7.CapW H.tcbs {b | InOS OS b} ∅)) : + ∀ t ∈ H.tcbs, ∀ K : U64, 224 ≤ K.val → K.val + 8 ≤ 4096 → K.val % 8 = 0 → + L.mem r (wa t K) = L.mem s (wa t K) := by + have wA := writesIn_alA w + have hB : ∀ b, (Aligned b ∧ W7.CapW H.tcbs {b | InOS OS b} ∅ b) → W7.CapW H.tcbs H.omega H.frame b := + fun b hb => os_capw J.ent.os hb.1 hb.2 + obtain ⟨-, -, -, -, -, fld, -, -⟩ := W7.blk_of_capw (BXI.blk J.ent.bxi) wA hB + exact fun t ht K h0 hK h8 => fld t ht K h0 (by omega) h8 + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelStepMdbA.lean b/proofs/Fl4ProofsA64/P3/DelStepMdbA.lean new file mode 100644 index 000000000..e2a8dcd2c --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelStepMdbA.lean @@ -0,0 +1,51 @@ +/- +P3 (AArch64, delete hooks E4; Architect Option H): **the MDB words across the deletion loop's slot steps** as the +three hook hypotheses of `P3/CnodeDelHookA` receive them: **`SwapMdbA`** (links and flags renamed by the +transposition), **`EmpMdbA`** (`EmpCtxA`'s `hrev`, `hfb`, `hnext`) and **`ZombMdbA`** (links and flags kept; the +written zombie covers a replaced thread / CNode object). Definitions only. No named premise. New module. +-/ +import Fl4ProofsA64.P3.CnodeFinalA +import Fl4ProofsA64.P1.MdbSemA +import Fl4ProofsA64.P1.MdbTreeA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +variable (st) in +/-- The swap's MDB words: links and flags renamed by the transposition of `x` and `p`. -/ +def SwapMdbA (G : List (List U64)) (x p : U64) (s r : S) : Prop := + ∀ a ∈ G.flatten, NxA st r (Equiv.swap x p a) = Equiv.swap x p (NxA st s a) ∧ + Fl4ProofsA64.P1.W7.R.ST.revA st r (Equiv.swap x p a) = Fl4ProofsA64.P1.W7.R.ST.revA st s a ∧ + fbA st r (Equiv.swap x p a) = fbA st s a + +variable (st) in +/-- The emptying's MDB words (`EmpCtxA`'s `hrev`, `hfb`, `hnext`). -/ +structure EmpMdbA (G : List (List U64)) (x : U64) (s r : S) : Prop where + rev : ∀ a ∈ G.flatten, a ≠ x → Fl4ProofsA64.P1.W7.R.ST.revA st r a = Fl4ProofsA64.P1.W7.R.ST.revA st s a + fb : ∀ a ∈ G.flatten, a ≠ x → fbA st r a = + if NxA st s x ≠ 0#u64 ∧ a = NxA st s x then (fbA st s a || fbA st s x) else fbA st s a + next : ∀ a ∈ G.flatten, a ≠ x → NxA st r a = + if L.mem s (wa x 16#u64) ≠ 0#u64 ∧ a = L.mem s (wa x 16#u64) then NxA st s x else NxA st s a + +variable (st) in +/-- The zombie write's MDB words, and the zombie covers a replaced thread / CNode object. -/ +structure ZombMdbA (G : List (List U64)) (x : U64) (s r : S) : Prop where + next : ∀ a ∈ G.flatten, NxA st r a = NxA st s a + rev : ∀ a ∈ G.flatten, Fl4ProofsA64.P1.W7.R.ST.revA st r a = Fl4ProofsA64.P1.W7.R.ST.revA st s a + fb : ∀ a ∈ G.flatten, fbA st r a = fbA st s a + cover : ¬ IsZombieA (capA st s x) → UA (capA st r x) (capA st s x) + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelStepSpecA.lean b/proofs/Fl4ProofsA64/P3/DelStepSpecA.lean new file mode 100644 index 000000000..2d9eebef0 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelStepSpecA.lean @@ -0,0 +1,486 @@ +/- +P3 (AArch64, delete hooks E4): **the CDT and original-cap fields of the per-step record across the loop's three slot +steps** (the spec's `empty_slot`, `cap_swap_for_delete` and the zombie `set_cap`). + +* **`emptySlot_cdtA`** / **`capSwapForDelete_cdtA`**: every outcome's `cdt` and `isOriginalCap`; +* **`CdtProvA`**: the slot names and original flags after come from before or from the step's own slots, with + `emptySlot_provA` / `capSwapForDelete_provA` / `CdtProvA.of_eq`; +* **`origLive_stepA`**, **`specFree_stepA`**, **`nullOrig_emptyA`**, **`nullOrig_swapA`**, **`nullOrig_zombieA`**: the + record's `origLive`, `specFree` and `nullOrig` after each step. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.DelCapKeepA +import Fl4ProofsA64.P3.CnodeDelZombieA +import Fl4ProofsA64.P3.Stage3A + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 + +noncomputable section + +/-! ## Computations that keep the CDT and the original flags -/ + +/-- Every outcome of `f` keeps `cdt` and `isOriginalCap`. -/ +def NdCdtA {α : Type} (f : NonDet (State Unit) α) : Prop := + ∀ σ r, r ∈ (f σ).1 → r.2.cdt = σ.cdt ∧ r.2.isOriginalCap = σ.isOriginalCap + +theorem NdCdtA.bind {α β : Type} {m : NonDet (State Unit) α} {f : α → NonDet (State Unit) β} (hm : NdCdtA m) + (hf : ∀ a, NdCdtA (f a)) : NdCdtA (m >>= f) := fun σ r hr => by + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + have h1 := hm σ x hx + have h2 := hf x.1 x.2 r hr + exact ⟨h2.1.trans h1.1, h2.2.trans h1.2⟩ + +theorem NdCdtA.ro {α : Type} {f : NonDet (State Unit) α} (h : ∀ σ r, r ∈ (f σ).1 → r.2 = σ) : NdCdtA f := + fun σ r hr => by rw [h σ r hr]; exact ⟨rfl, rfl⟩ + +theorem getObject_cdtA (p : ObjRef) : NdCdtA (getObject (Z := Unit) p) := + NdCdtA.ro fun _ _ h => (getObject_mem h).1 + +theorem setObject_cdtA (p : ObjRef) (o : KernelObject) : NdCdtA (setObject (Z := Unit) p o) := + fun σ r hr => by + unfold setObject at hr + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + have e1 := (getObject_mem hx).1 + obtain ⟨y, hy, hr⟩ := mem_bindA.1 hr + obtain ⟨-, rfl⟩ := hy + obtain ⟨z, hz, hr⟩ := mem_bindA.1 hr + obtain rfl : z = (x.2, x.2) := hz + obtain rfl : r = ((), { x.2 with kheap := Function.update x.2.kheap p (some o) }) := hr + rw [← e1]; exact ⟨rfl, rfl⟩ + +theorem NdCdtA.fail {α : Type} : NdCdtA (NonDet.fail : NonDet (State Unit) α) := + fun _ _ h => absurd h (Set.notMem_empty _) + +theorem NdCdtA.pure {α : Type} (a : α) : NdCdtA (Pure.pure a : NonDet (State Unit) α) := + NdCdtA.ro fun σ r h => by obtain rfl : r = (a, σ) := h; rfl + +theorem setCap_cdtA (c : Cap) (sl : CslotPtr) : NdCdtA (setCap (Z := Unit) c sl) := by + obtain ⟨oref, cref⟩ := sl + intro σ r hr + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + obtain ⟨e1, -⟩ := getObject_mem hx + obtain ⟨obj, σ1⟩ := x + simp only at e1 hr + subst e1 + have key : ∀ o, r ∈ ((Pure.pure o >>= fun o' => setObject (Z := Unit) oref o') σ1).1 → + r.2.cdt = σ1.cdt ∧ r.2.isOriginalCap = σ1.isOriginalCap := by + intro o h + obtain ⟨y, hy, h⟩ := mem_bindA.1 h + obtain rfl : y = (o, σ1) := hy + exact setObject_cdtA oref o σ1 r h + have kfail : ∀ f : KernelObject → NonDet (State Unit) Unit, r ∈ ((NonDet.fail >>= f) σ1).1 → False := by + intro f h + obtain ⟨y, hy, -⟩ := mem_bindA.1 h + exact absurd hy (Set.notMem_empty _) + cases obj with + | cnode sz cn => + simp only at hr + split_ifs at hr + · exact key _ hr + · exact (kfail _ hr).elim + | tcb t => + simp only at hr + split_ifs at hr <;> + first + | exact (kfail _ hr).elim + | exact key _ hr + | _ => + try simp only at hr + exact (kfail _ hr).elim + +theorem doExtendedOp_cdtA (eop : DetExtMonad Unit) : NdCdtA (doExtendedOp (Z := Unit) eop) := + fun σ r hr => by + unfold doExtendedOp at hr + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + obtain rfl : x = (σ, σ) := hx + obtain ⟨y, hy, hr⟩ := mem_bindA.1 hr + have e : y.2 = σ := hy.2 + obtain rfl : r = ((), { y.2 with exst := y.1.2.exst }) := hr + rw [e]; exact ⟨rfl, rfl⟩ + +/-- `get_cap`'s outcome: the state kept, the cap the slot's spec cap. -/ +theorem getCap_outA {σ σ' : State Unit} {sl : CslotPtr} {c : Cap} (hr : (c, σ') ∈ (getCap (Z := Unit) sl σ).1) : + σ' = σ ∧ specCapAtA σ sl = some c := by + obtain ⟨oref, cref⟩ := sl + unfold getCap at hr + obtain ⟨⟨o, σ1⟩, hx, hr⟩ := mem_bindA.1 hr + obtain ⟨e1, hk⟩ := getObject_mem hx + dsimp only at e1 hk hr + subst σ1 + have fin : ∀ (caps : _ → Option Cap) (σ2 : State Unit), σ2 = σ → + (c, σ') ∈ (NonDet.assertOpt (caps cref) σ2).1 → σ' = σ ∧ caps cref = some c := by + intro caps σ2 e h + cases hc : caps cref with + | none => rw [hc] at h; exact absurd h (Set.notMem_empty _) + | some d => + rw [hc] at h + have h' : (c, σ') = (d, σ2) := h + cases h' + exact ⟨e, rfl⟩ + unfold specCapAtA + dsimp only + rw [hk] + cases o with + | cnode sz cn => + dsimp only at hr ⊢ + obtain ⟨z, hz, hr⟩ := mem_bindA.1 hr + obtain ⟨hw, rfl⟩ := hz + obtain ⟨y, hy, hr⟩ := mem_bindA.1 hr + obtain rfl : y = (cn, σ) := hy + rw [if_pos hw] + exact fin cn σ rfl hr + | tcb t => + dsimp only at hr ⊢ + obtain ⟨y, hy, hr⟩ := mem_bindA.1 hr + obtain rfl : y = (tcbCnodeMap t, σ) := hy + exact fin (tcbCnodeMap t) σ rfl hr + | _ => + dsimp only at hr + obtain ⟨y, hy, -⟩ := mem_bindA.1 hr + exact absurd hy (Set.notMem_empty _) + +theorem getCap_stateA (sl : CslotPtr) : ∀ σ r, r ∈ (getCap (Z := Unit) sl σ).1 → r.2 = σ := + fun _ _ hr => (getCap_outA hr).1 + +theorem getCap_specAtA {σ σ' : State Unit} {sl : CslotPtr} {c : Cap} (hr : (c, σ') ∈ (getCap (Z := Unit) sl σ).1) : + specCapAtA σ sl = some c := (getCap_outA hr).2 + +/-! ## The outcomes -/ + +/-- **`empty_slot slot NullCap`'s CDT and original flags.** -/ +theorem emptySlot_cdtA {σ σ' : State Unit} {sl : CslotPtr} + (h : ((), σ') ∈ (emptySlot (Z := Unit) sl .nullCap σ).1) : + (σ'.cdt = σ.cdt ∧ σ'.isOriginalCap = σ.isOriginalCap ∧ (Cap.nullCap, σ) ∈ (getCap (Z := Unit) sl σ).1) ∨ + (σ'.cdt = Function.update (fun p => if σ.cdt p = some sl then σ.cdt sl else σ.cdt p) sl none ∧ + σ'.isOriginalCap = Function.update σ.isOriginalCap sl false) := by + unfold emptySlot at h + obtain ⟨x, hx, h⟩ := mem_bindA.1 h + have ex : x.2 = σ := by + have := getCap_stateA sl σ x hx + exact this + obtain ⟨c, σ1⟩ := x + dsimp only at ex h + subst σ1 + by_cases hc : c = .nullCap + · rw [if_pos hc] at h + obtain rfl : σ' = σ := congrArg Prod.snd (show ((), σ') = ((), σ) from h) + subst hc + exact Or.inl ⟨rfl, rfl, hx⟩ + rw [if_neg hc] at h + right + obtain ⟨a, ha, h⟩ := mem_bindA.1 h + obtain rfl : a = (σ.cdt sl, σ) := ha + obtain ⟨b, hb, h⟩ := mem_bindA.1 h + obtain rfl : b = (σ.cdt, σ) := hb + obtain ⟨d, hd, h⟩ := mem_bindA.1 h + obtain rfl : d = (σ.cdt sl, σ) := hd + obtain ⟨e, he, h⟩ := mem_bindA.1 h + obtain ⟨g, hg, he⟩ := mem_bindA.1 he + obtain rfl : g = (σ, σ) := hg + obtain rfl : e = ((), { σ with cdt := Function.update (fun p => if σ.cdt p = some sl then σ.cdt sl else σ.cdt p) sl none }) := he + obtain ⟨f, hf, h⟩ := mem_bindA.1 h + have kf := doExtendedOp_cdtA _ _ _ hf + obtain ⟨i, hi, h⟩ := mem_bindA.1 h + unfold setOriginal at hi + obtain ⟨j, hj, hi⟩ := mem_bindA.1 hi + obtain rfl : j = (f.2.isOriginalCap, f.2) := hj + obtain rfl : i = ((), { f.2 with isOriginalCap := Function.update f.2.isOriginalCap sl false }) := hi + obtain ⟨k, hk, h⟩ := mem_bindA.1 h + have kk := setCap_cdtA _ _ _ _ hk + obtain rfl : σ' = k.2 := congrArg Prod.snd (show ((), σ') = ((), k.2) from h) + refine ⟨kk.1.trans kf.1, kk.2.trans ?_⟩ + show Function.update f.2.isOriginalCap sl false = _ + rw [kf.2] + +/-- The CDT after `cap_swap` of `s1` and `s2`: children, then parents, renamed. -/ +def swapKidsA (c : Cdt) (s1 s2 : CslotPtr) : Cdt := fun n => + if c n = some s1 then some s2 else if c n = some s2 then some s1 else c n + +def swapCdtA (c : Cdt) (s1 s2 : CslotPtr) : Cdt := + Function.update (Function.update (swapKidsA c s1 s2) s1 (swapKidsA c s1 s2 s2)) s2 (swapKidsA c s1 s2 s1) + +/-- **`cap_swap`'s CDT and original flags.** -/ +theorem capSwap_cdtA {σ σ' : State Unit} {c1 c2 : Cap} {s1 s2 : CslotPtr} + (h : ((), σ') ∈ (capSwap (Z := Unit) c1 s1 c2 s2 σ).1) : + σ'.cdt = swapCdtA σ.cdt s1 s2 ∧ + σ'.isOriginalCap = Function.update (Function.update σ.isOriginalCap s1 (σ.isOriginalCap s2)) s2 + (σ.isOriginalCap s1) := by + unfold capSwap at h + obtain ⟨a, ha, h⟩ := mem_bindA.1 h + have ka := setCap_cdtA _ _ _ _ ha + obtain ⟨b, hb, h⟩ := mem_bindA.1 h + have kb := setCap_cdtA _ _ _ _ hb + have e1 : b.2.cdt = σ.cdt := kb.1.trans ka.1 + have e2 : b.2.isOriginalCap = σ.isOriginalCap := kb.2.trans ka.2 + obtain ⟨c, hc, h⟩ := mem_bindA.1 h + obtain rfl : c = (b.2.cdt s1, b.2) := hc + obtain ⟨d, hd, h⟩ := mem_bindA.1 h + obtain rfl : d = (b.2.cdt s2, b.2) := hd + obtain ⟨e, he, h⟩ := mem_bindA.1 h + obtain rfl : e = (b.2.cdt, b.2) := he + obtain ⟨f, hf, h⟩ := mem_bindA.1 h + obtain rfl : f = (swapKidsA b.2.cdt s1 s2, b.2) := hf + obtain ⟨g, hg, h⟩ := mem_bindA.1 h + obtain ⟨g0, hg0, hg⟩ := mem_bindA.1 hg + obtain rfl : g0 = (b.2, b.2) := hg0 + obtain rfl : g = ((), { b.2 with cdt := swapCdtA b.2.cdt s1 s2 }) := hg + obtain ⟨i, hi, h⟩ := mem_bindA.1 h + have ki := doExtendedOp_cdtA _ _ _ hi + obtain ⟨j, hj, h⟩ := mem_bindA.1 h + obtain rfl : j = (i.2.isOriginalCap, i.2) := hj + obtain ⟨k, hk, h⟩ := mem_bindA.1 h + unfold setOriginal at hk h + obtain ⟨k0, hk0, hk⟩ := mem_bindA.1 hk + obtain rfl : k0 = (i.2.isOriginalCap, i.2) := hk0 + obtain rfl : k = ((), { i.2 with isOriginalCap := Function.update i.2.isOriginalCap s1 (i.2.isOriginalCap s2) }) := hk + obtain ⟨l0, hl0, h⟩ := mem_bindA.1 h + obtain rfl : l0 = (Function.update i.2.isOriginalCap s1 (i.2.isOriginalCap s2), { i.2 with isOriginalCap := Function.update i.2.isOriginalCap s1 (i.2.isOriginalCap s2) }) := hl0 + obtain rfl : σ' = { i.2 with isOriginalCap := Function.update (Function.update i.2.isOriginalCap s1 (i.2.isOriginalCap s2)) s2 (i.2.isOriginalCap s1) } := + congrArg Prod.snd h + constructor + · show i.2.cdt = _ + rw [ki.1] + show swapCdtA b.2.cdt s1 s2 = _ + rw [e1] + · show Function.update (Function.update i.2.isOriginalCap s1 (i.2.isOriginalCap s2)) s2 (i.2.isOriginalCap s1) = _ + rw [ki.2] + show Function.update (Function.update b.2.isOriginalCap s1 (b.2.isOriginalCap s2)) s2 (b.2.isOriginalCap s1) = _ + rw [e2] + +/-! ## Provenance -/ + +/-- The CDT names and original flags after come from before or from the slots `Q`. -/ +structure CdtProvA (σ σ' : State Unit) (Q : CslotPtr → Prop) : Prop where + cdt : ∀ x y, σ'.cdt x = some y → + (Q x ∨ ∃ y', σ.cdt x = some y') ∧ (Q y ∨ ∃ z, σ.cdt z = some y) + orig : ∀ x, σ'.isOriginalCap x = true → Q x ∨ σ.isOriginalCap x = true + +theorem CdtProvA.of_eq {σ σ' : State Unit} {Q : CslotPtr → Prop} (h1 : σ'.cdt = σ.cdt) + (h2 : σ'.isOriginalCap = σ.isOriginalCap) : CdtProvA σ σ' Q := + ⟨fun x y h => by rw [h1] at h; exact ⟨Or.inr ⟨y, h⟩, Or.inr ⟨x, h⟩⟩, + fun x h => by rw [h2] at h; exact Or.inr h⟩ + +theorem emptySlot_provA {σ σ' : State Unit} {sl : CslotPtr} + (h : ((), σ') ∈ (emptySlot (Z := Unit) sl .nullCap σ).1) : CdtProvA σ σ' (fun _ => False) := by + rcases emptySlot_cdtA h with ⟨h1, h2, -⟩ | ⟨h1, h2⟩ + · exact CdtProvA.of_eq h1 h2 + refine ⟨fun x y hx => ?_, fun x hx => ?_⟩ + · rw [h1] at hx + by_cases e : x = sl + · rw [e, Function.update_self] at hx; cases hx + rw [Function.update_of_ne e] at hx + by_cases e2 : σ.cdt x = some sl + · rw [if_pos e2] at hx + exact ⟨Or.inr ⟨sl, e2⟩, Or.inr ⟨sl, hx⟩⟩ + · rw [if_neg e2] at hx + exact ⟨Or.inr ⟨y, hx⟩, Or.inr ⟨x, hx⟩⟩ + · rw [h2] at hx + by_cases e : x = sl + · rw [e, Function.update_self] at hx; cases hx + rw [Function.update_of_ne e] at hx + exact Or.inr hx + +theorem capSwapForDelete_provA {σ σ' : State Unit} {s1 s2 : CslotPtr} + (h : ((), σ') ∈ (capSwapForDelete (Z := Unit) s1 s2 σ).1) : CdtProvA σ σ' (fun x => x = s1 ∨ x = s2) := by + unfold capSwapForDelete NonDet.when at h + by_cases hne : s1 ≠ s2 + swap + · rw [if_neg hne] at h + obtain rfl : σ' = σ := congrArg Prod.snd (show ((), σ') = ((), σ) from h) + exact CdtProvA.of_eq rfl rfl + rw [if_pos hne] at h + obtain ⟨x1, hx1, h⟩ := mem_bindA.1 h + have e1 : x1.2 = σ := getCap_stateA s1 σ x1 hx1 + obtain ⟨x2, hx2, h⟩ := mem_bindA.1 h + have e2 : x2.2 = σ := (getCap_stateA s2 x1.2 x2 hx2).trans e1 + rw [e2] at h + obtain ⟨hc, ho⟩ := capSwap_cdtA h + -- a value of the renamed map is a slot of the step or an old parent + have val : ∀ n y, swapKidsA σ.cdt s1 s2 n = some y → (∃ y', σ.cdt n = some y') ∧ ((y = s1 ∨ y = s2) ∨ ∃ z, σ.cdt z = some y) := by + intro n y hn + unfold swapKidsA at hn + by_cases a : σ.cdt n = some s1 + · rw [if_pos a] at hn; cases hn; exact ⟨⟨_, a⟩, Or.inl (Or.inr rfl)⟩ + rw [if_neg a] at hn + by_cases b : σ.cdt n = some s2 + · rw [if_pos b] at hn; cases hn; exact ⟨⟨_, b⟩, Or.inl (Or.inl rfl)⟩ + rw [if_neg b] at hn + exact ⟨⟨_, hn⟩, Or.inr ⟨n, hn⟩⟩ + refine ⟨fun x y hx => ?_, fun x hx => ?_⟩ + · rw [hc] at hx + unfold swapCdtA at hx + by_cases a : x = s2 + · rw [a, Function.update_self] at hx + exact ⟨Or.inl (Or.inr a), (val _ _ hx).2⟩ + rw [Function.update_of_ne a] at hx + by_cases b : x = s1 + · rw [b, Function.update_self] at hx + exact ⟨Or.inl (Or.inl b), (val _ _ hx).2⟩ + rw [Function.update_of_ne b] at hx + exact ⟨Or.inr (val _ _ hx).1, (val _ _ hx).2⟩ + · by_cases a : x = s2 + · exact Or.inl (Or.inr a) + by_cases b : x = s1 + · exact Or.inl (Or.inl b) + rw [ho, Function.update_of_ne a, Function.update_of_ne b] at hx + exact Or.inr hx + +/-! ## The record's fields -/ + +theorem origLive_stepA {th : TypedHeapA} {σ σ' : State Unit} {Q : CslotPtr → Prop} (P : CdtProvA σ σ' Q) + (hQ : ∀ x, Q x → AbsSlotA th x) (o : ∀ sl, σ.isOriginalCap sl = true → AbsSlotA th sl) : + ∀ sl, σ'.isOriginalCap sl = true → AbsSlotA th sl := fun sl h => + (P.orig sl h).elim (hQ sl) (o sl) + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {th : TypedHeapA} {OS : Set U64} {s s' : S} {σ σ' : State Unit} + +/-- A typed slot's object is in the spec heap. -/ +theorem absSlot_kheapA {mem : U64 → U64} (hR : StateRelA th mem σ) {sl : CslotPtr} (hs : AbsSlotA th sl) : + σ.kheap sl.1 ≠ none := by + obtain ⟨c, hg, -⟩ := hR.getAbsA hs + intro h + have := getCap_specAtA hg + unfold specCapAtA at this + rw [h] at this + cases this + +theorem specFree_stepA {Q : CslotPtr → Prop} (F : SpecFreeA st OS s σ) (P : CdtProvA σ σ' Q) + (hR : StateRelA th (L.mem s) σ) (hQ : ∀ x, Q x → AbsSlotA th x) (fr : CapFromA st OS s s') + (HS : SpecSlotA σ σ') : SpecFreeA st OS s' σ' := by + refine ⟨SpecFreeA.heapFromA fr HS F, fun x y h p hp => ?_, fun x h p hp => ?_⟩ + · have hp0 := UtFreeA.fromA fr hp + have slot : ∀ q : CslotPtr, Q q → q.1 ≠ specPtr p := fun q hq e => + absSlot_kheapA hR (hQ q hq) (by rw [e]; exact F.heap p hp0) + obtain ⟨hx, hy⟩ := P.cdt x y h + refine ⟨?_, ?_⟩ + · rcases hx with hq | ⟨y', e⟩ + · exact slot x hq + · exact (F.cdt x y' e p hp0).1 + · rcases hy with hq | ⟨z, e⟩ + · exact slot y hq + · exact (F.cdt z y e p hp0).2 + · have hp0 := UtFreeA.fromA fr hp + rcases P.orig x h with hq | e + · exact fun e => absSlot_kheapA hR (hQ x hq) (by rw [e]; exact F.heap p hp0) + · exact F.orig x e p hp0 + +/-- A typed slot whose spec cap is null holds `Null`, and conversely. -/ +theorem specNull_iffA {mem : U64 → U64} (hR : StateRelA th mem σ) {sl : CslotPtr} (hs : AbsSlotA th sl) : + specCapAtA σ sl = some .nullCap ↔ capAtA mem (cslotAddrA sl) = .Null := by + obtain ⟨c, hg, hrel⟩ := hR.getAbsA hs + rw [getCap_specAtA hg] + constructor + · intro h + by_contra hn + exact hrel.ne_null hn (Option.some.inj h) + · intro h + rw [h] at hrel + have e := hrel.2 + change absCapA .Null = c at e + rw [← e]; rfl + +/-- A typed spec slot that is original after the step, with a null spec cap, holds `Null`. -/ +theorem nullOrig_emptyA {sl : CslotPtr} (hs : AbsSlotA th sl) + (h : ((), σ') ∈ (emptySlot (Z := Unit) sl .nullCap σ).1) + (hR : StateRelA th (L.mem s) σ) (hR' : StateRelA th (L.mem s') σ') (hP : PlaceA th (L.ksb s)) + (hsub : ∀ y, IsSlotA th y → y ∈ OS) (hn : capA st s (cslotAddrA sl) ≠ .Null) + (hcap : ∀ y ∈ OS, y ≠ cslotAddrA sl → capA st s' y = capA st s y) + (live : ∀ x, σ'.isOriginalCap x = true → AbsSlotA th x) + (o : ∀ x, specCapAtA σ x = some .nullCap → σ.isOriginalCap x = false) : + ∀ x, specCapAtA σ' x = some .nullCap → σ'.isOriginalCap x = false := by + intro x hx + by_contra ho + rw [Bool.not_eq_false] at ho + have ax := live x ho + rcases emptySlot_cdtA h with ⟨-, -, hg⟩ | ⟨-, h2⟩ + · obtain ⟨c, hg', hrel⟩ := hR.getAbsA hs + have e : some c = some Cap.nullCap := (getCap_specAtA hg').symm.trans (getCap_specAtA hg) + exact hrel.ne_null hn (Option.some.inj e) + have ne : x ≠ sl := fun e => by + rw [h2, e, Function.update_self] at ho; cases ho + rw [h2, Function.update_of_ne ne] at ho + have hxO := hsub _ (ax.isSlot hP) + have c' : capA st s' (cslotAddrA x) = .Null := (specNull_iffA hR' ax).1 hx + have ane : cslotAddrA x ≠ cslotAddrA sl := fun e => ne (cslotAddrA_inj hP ax hs e) + rw [hcap _ hxO ane] at c' + rw [o x ((specNull_iffA hR ax).2 c')] at ho + cases ho + +theorem nullOrig_zombieA {a : Cap} {sl : CslotPtr} (hs : AbsSlotA th sl) + (hset : setCap (Z := Unit) a sl σ = ({((), σ')}, False)) + (hR : StateRelA th (L.mem s) σ) (hR' : StateRelA th (L.mem s') σ') (hP : PlaceA th (L.ksb s)) + (hsub : ∀ y, IsSlotA th y → y ∈ OS) (hn : capA st s' (cslotAddrA sl) ≠ .Null) + (hcap : ∀ y ∈ OS, y ≠ cslotAddrA sl → capA st s' y = capA st s y) + (live : ∀ x, σ'.isOriginalCap x = true → AbsSlotA th x) + (o : ∀ x, specCapAtA σ x = some .nullCap → σ.isOriginalCap x = false) : + ∀ x, specCapAtA σ' x = some .nullCap → σ'.isOriginalCap x = false := by + intro x hx + by_contra ho + rw [Bool.not_eq_false] at ho + have ax := live x ho + have h2 := (setCap_cdtA a sl σ ((), σ') (by rw [hset]; rfl)).2 + rw [h2] at ho + have c' : capA st s' (cslotAddrA x) = .Null := (specNull_iffA hR' ax).1 hx + have ane : cslotAddrA x ≠ cslotAddrA sl := fun e => hn (e ▸ c') + rw [hcap _ (hsub _ (ax.isSlot hP)) ane] at c' + rw [o x ((specNull_iffA hR ax).2 c')] at ho + cases ho + +theorem nullOrig_swapA {s1 s2 : CslotPtr} (h1 : AbsSlotA th s1) (h2 : AbsSlotA th s2) (hne : s1 ≠ s2) + (h : ((), σ') ∈ (capSwapForDelete (Z := Unit) s1 s2 σ).1) + (hR : StateRelA th (L.mem s) σ) (hR' : StateRelA th (L.mem s') σ') (hP : PlaceA th (L.ksb s)) + (hsub : ∀ y, IsSlotA th y → y ∈ OS) + (hcap : ∀ y ∈ OS, capA st s' y = if y = cslotAddrA s1 then capA st s (cslotAddrA s2) + else if y = cslotAddrA s2 then capA st s (cslotAddrA s1) else capA st s y) + (live : ∀ x, σ'.isOriginalCap x = true → AbsSlotA th x) + (o : ∀ x, specCapAtA σ x = some .nullCap → σ.isOriginalCap x = false) : + ∀ x, specCapAtA σ' x = some .nullCap → σ'.isOriginalCap x = false := by + intro x hx + by_contra ho + rw [Bool.not_eq_false] at ho + have ax := live x ho + have eo : σ'.isOriginalCap = Function.update (Function.update σ.isOriginalCap s1 (σ.isOriginalCap s2)) s2 + (σ.isOriginalCap s1) := by + unfold capSwapForDelete NonDet.when at h + rw [if_pos hne] at h + obtain ⟨x1, hx1, h⟩ := mem_bindA.1 h + have e1 : x1.2 = σ := getCap_stateA s1 σ x1 hx1 + obtain ⟨x2, hx2, h⟩ := mem_bindA.1 h + have e2 : x2.2 = σ := (getCap_stateA s2 x1.2 x2 hx2).trans e1 + rw [e2] at h + exact (capSwap_cdtA h).2 + have c' : capA st s' (cslotAddrA x) = .Null := (specNull_iffA hR' ax).1 hx + rw [hcap _ (hsub _ (ax.isSlot hP))] at c' + have old : ∀ q, AbsSlotA th q → capA st s (cslotAddrA q) = .Null → σ.isOriginalCap q = false := + fun q aq c => o q ((specNull_iffA hR aq).2 c) + rw [eo] at ho + by_cases a : x = s2 + · subst a + have ane : cslotAddrA x ≠ cslotAddrA s1 := fun e => hne (cslotAddrA_inj hP ax h1 e).symm + rw [if_neg ane, if_pos rfl] at c' + rw [Function.update_self, old s1 h1 c'] at ho + cases ho + rw [Function.update_of_ne a] at ho + by_cases b : x = s1 + · subst b + rw [if_pos rfl] at c' + rw [Function.update_self, old s2 h2 c'] at ho + cases ho + rw [Function.update_of_ne b] at ho + have n1 : cslotAddrA x ≠ cslotAddrA s1 := fun e => b (cslotAddrA_inj hP ax h1 e) + have n2 : cslotAddrA x ≠ cslotAddrA s2 := fun e => a (cslotAddrA_inj hP ax h2 e) + rw [if_neg n1, if_neg n2] at c' + rw [old x ax c'] at ho + cases ho + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelSuspendA.lean b/proofs/Fl4ProofsA64/P3/DelSuspendA.lean new file mode 100644 index 000000000..e6b96c2e6 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelSuspendA.lean @@ -0,0 +1,518 @@ +/- +P3 (AArch64, delete hooks, slice "record-level `sched.suspend`"): **`sched.suspend` at the Stage-3 record**, for a +thread running no VCPU (`RUN_VCPU = 0`). Shared by `finHookThreadA` and the `TCB_Suspend` arm. + +* `JStage3TA.bufXOffA`: the buffer words are extra words off the footprint (`JStage3A.bufXOffA` over the per-step + record). +* `suspend_stepA`: `suspend_refinesA` and `suspend_footA` at one outcome. +* `JStage3TA.suspend_recA`: the record rebuilt after `suspend` (every field). The spec-side facts come from + `DelSuspendSpecA`; `BufCover`, `BlkQueuedA`, `RBlk` on the stacks, J5 and J7 are proved here. `suspend` writes no + buffer word (`suspend_footA` at `Bf = ∅`). +* `JStage3TA.susX_keepA`: the `DelXA` fields besides the record, across `suspend`. +* `JStage3TA.notIdle_of_capA`: the caller premises `hti` / `hidles` from a thread cap (`NoIdleCapA`). + +Caller premises: `hQ7` (J7 does not read the ghost queues; `rfl` for the concrete slots), `hti`, `hidles` (a thread that is no core's idle thread) and `X.runW` (`RUN_VCPU = 0`; the live +`cancel_run` arm is not handled). The scheduler action word is not `t` by the +record's `noSwitch`. [S] and [P] platform premises are `suspend_refinesA`'s. +-/ +import Fl4ProofsA64.P3.Stage3QKeepPresA +import Fl4ProofsA64.P3.DelSuspendSpecA +import Fl4ProofsA64.P3.IpcScReplyA +import Fl4ProofsA64.P1.MdbIpcAl +import Fl4ProofsA64.P1.MdbRegRL +import Fl4ProofsA64.P1.QueueFoot8T +import Fl4ProofsA64.P3.Stage3TcbSuspFrA +import Fl4ProofsA64.P3.TcbRegsContA +import Fl4ProofsA64.P3.DelNtfnRecA +import Fl4ProofsA64.P3.DelRecA +import Fl4ProofsA64.P3.DelCtOffA +import Fl4ProofsA64.P3.Stage3SchedWrSTA +import Fl4ProofsA64.P3.Stage3BufWrA +import Fl4ProofsA64.P3.Stage3SchedWrNTA +import Fl4ProofsA64.P3.DelNtfnLoopFrA +import Fl4ProofsA64.P3.DelNtfnEntA +import Fl4ProofsA64.P3.Stage3J5A +import Fl4ProofsA64.P3.Stage3PreemptTA +import Fl4ProofsA64.P1.QueueFinRvSt +import Fl4ProofsA64.P1.QueueFinal +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 Fl4Spec + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- A buffer word is not a word of a typed slot. -/ +theorem JStage3TA.buf_slot_offA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) {y b : U64} + (hy : IsSlotA Z.th y) (y1 : y.val ≤ b.val) (y2 : b.val < y.val + 32) : ¬ BufW Bf b := by + intro hw + have P := J.sr.place + obtain ⟨e, len, he, e1, e2⟩ := J.ipcPlace.buffer b hw + obtain ⟨p, k, hk, p1, p2, hkind⟩ : ∃ p k, Z.th p = some k ∧ p.val ≤ y.val ∧ y.val + 32 ≤ p.val + k.size ∧ + k ≠ .other len := by + obtain ⟨p, ⟨r, hr, i, hi, rfl⟩ | ⟨ht, i, hi, rfl⟩⟩ := hy + · obtain ⟨k, hk, hs, hv⟩ := slotVA_of P hr (j := i) (by rw [cnode_size]; omega) + exact ⟨p, k, hk, by omega, by omega, fun e' => by rw [e', hr] at hk; cases hk⟩ + · obtain ⟨k, hk, hs, hv⟩ := tcb_slotV P ht hi + have : i < 7 := hi + exact ⟨p, k, hk, by omega, by omega, fun e' => by rw [e', ht] at hk; cases hk⟩ + have hne : p ≠ e := fun h => hkind (by subst h; exact Option.some.inj (hk.symm.trans he)) + have := P.apart p e k (.other len) hk he hne + have hsz : ObjKindA.size (.other len) = len := rfl + omega + +/-- A buffer word is no VCPU-link word of a TCB. -/ +theorem JStage3TA.buf_link_offA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) {t K : U64} (ht : t ∈ Z.T) + (hK : K = 3664#u64 ∨ K = 3672#u64) : ¬ BufW Bf (wa t K) := by + intro hw + have P := J.sr.place + obtain ⟨e, len, he, e1, e2⟩ := J.ipcPlace.buffer _ hw + have hth : Z.th t = some .tcb := (J.sr.thT t).2 ht + have hfit := (J.sr.core.tcbs t ht).fits + have hKv : K.val = 3664 ∨ K.val = 3672 := by rcases hK with rfl | rfl <;> simp + have hv : (wa t K).val = t.val + K.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have hne : t ≠ e := fun h => by + subst h + rw [hth] at he + cases he + have := P.apart t e .tcb (.other len) hth he hne + have hsz : ObjKindA.size (.other len) = len := rfl + have hsz' : ObjKindA.size .tcb = 4096 := rfl + omega + +/-- A buffer word is off the kernel-state block. -/ +theorem JStage3TA.buf_ksA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) {b : U64} (h : BufW Bf b) : + b.val < (L.ksb s).val ∨ (L.ksb s).val + ksSizeA ≤ b.val := by + obtain ⟨e, len, he, e1, e2⟩ := J.ipcPlace.buffer b h + have P := J.sr.place + have := P.ks e (.other len) he (fun hn => by have := P.irqK he hn; cases this) + have hsz : ObjKindA.size (.other len) = len := rfl + omega + +/-- The buffer words are extra words off the footprint. -/ +theorem JStage3TA.bufXOffA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + : XOffA (st := st) Z H OS s (BufW Bf) where + os := J.bufOff.os + slot := fun _ _ hy h1 h2 => J.buf_slot_offA hy h1 h2 + link := fun _ ht _ hK => J.buf_link_offA ht hK + irq := fun i hi h => by + have hv := irqWordA_val J.sr.ksFit hi + have hksz : ksSizeA = 788736 := rfl + have := J.buf_ksA h + omega + vs := fun b h hw => J.bufOff.vs b hw h + page := fun p hp o o1 o2 hw => J.bufOff.page _ hw p hp o o1 o2 rfl + vcw := fun v hv o ho hw => J.bufOff.vcw _ hw v hv o (by rcases ho with h | h <;> omega) + (by rcases ho with h | h <;> omega) rfl + dom := fun b hd hw => by + have hksz : ksSizeA = 788736 := rfl + have := J.buf_ksA hw + unfold DomW at hd + omega + ipi := fun o ho hw => by + have hksz : ksSizeA = 788736 := rfl + obtain ⟨hv, hcl⟩ := J.sr.toSchedSRN.ipi_val ho + have := J.buf_ksA hw + omega + +/-- **(D12) J10 across a step changing user memory only inside typed `other` objects**: such an object is off every +free untyped part (`ObjAllocA`). -/ +theorem JStage3TA.specZero_of_bufA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) {σ' : Fl4SpecA64.State Unit} + (hum : ∀ x, σ'.machineState.underlyingMemory x ≠ σ.machineState.underlyingMemory x → + ∃ p len, Z.th p = some (.other len) ∧ p.val ≤ (fl4Ptr x).val ∧ (fl4Ptr x).val < p.val + len) : + ∀ x : Fl4SpecA64.Word, UtFreeA st OS s (fun d => d = false) (fl4Ptr x).val → + σ'.machineState.underlyingMemory x = σ.machineState.underlyingMemory x := by + intro x hf + by_contra hne + obtain ⟨p, len, hp, h1, h2⟩ := hum x hne + obtain ⟨u, hu, q, b, fi, d, hc, -, f1, f2⟩ := hf + have hsz : ObjKindA.size (.other len) = len := rfl + have hO := J.objAlloc p len (Or.inr (Or.inr (Or.inl ⟨.other len, hp, hsz.symm⟩))) u hu q b fi d hc + omega + +end + +/-- **`sched.suspend`: refinement, write set and spec frame at one outcome** (`suspend_refinesA` and +`suspend_footA`; the spec outcome is a singleton). -/ +theorem suspend_stepA {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + (hP : IPlacedA N Z.T E R Bf) (P : IpcSchedPlacementA Z.th N E R Bf) + (hc : m.curCore < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (s : S) (a : A) (σ : State Unit) {stack : U64 → List U64} + (H : IpcSchedRelA st Z m κ s σ) (RN : NtfnStateRel N Z.T (L.mem s) σ) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + (M : McpRelA Z.T m (L.mem s) σ) (Y : YieldRelA Z.T Z.C m (L.mem s) σ) (hC0 : ∀ c ∈ Z.C, c ≠ 0#u64) + {t : U64} (ht : t ∈ Z.T) {x : Fl4SpecA64.Tcb} (X : TcbSuspendReadyA st Z m N E R s σ t x) : + sched.suspend st ar s a t ⦃ out => out.2 = a ∧ ∃ σ' stack', + (fl4CancelRun (Z := Unit) (specPtr t) >>= fun _ => Fl4SpecA64.suspend (specPtr t)) σ = + ({((), σ')}, False) ∧ + WritesIn st s out.1 (SchedStepNWA (st := st) N E R Z m σ s) ∧ + SchedFrameA σ σ' ∧ + NtfnStateRel N Z.T (L.mem out.1) σ' ∧ IpcSchedRelA st Z m κ out.1 σ' ∧ + EpStateRel Z.T E R Bf (L.mem out.1) σ' ∧ + ReplyStackRelA (L.mem out.1) R Z.C σ' (scNameA m) stack' ∧ + McpRelA Z.T m (L.mem out.1) σ' ∧ YieldRelA Z.T Z.C m (L.mem out.1) σ' ∧ + ∃ y, σ'.kheap (specPtr t) = some (.tcb y) ∧ y.tcbState = .inactive ⦄ := by + have PE0 : EPlaced Z.T E R (∅ : Set (U64 × Fl4SpecA64.Word)) := + { hP.e with + balign := fun _ ⟨_, h, _⟩ => h.elim + bt := fun _ ⟨_, h, _⟩ => h.elim + be := fun _ ⟨_, h, _⟩ => h.elim + br := fun _ ⟨_, h, _⟩ => h.elim + binj := fun _ h => h.elim } + have hP0 : IPlacedA N Z.T E R (∅ : Set (U64 × Fl4SpecA64.Word)) := + ⟨hP.n, PE0, hP.ne, hP.nr, fun _ _ _ ⟨_, h, _⟩ => h.elim⟩ + have P0' : IpcSchedPlacementA Z.th N E R (∅ : Set (U64 × Fl4SpecA64.Word)) := + ⟨P.notification, P.endpoint, P.reply, fun _ ⟨_, h, _⟩ => h.elim⟩ + have HE0 : EpStateRel Z.T E R (∅ : Set (U64 × Fl4SpecA64.Word)) (L.mem s) σ := + ⟨HE.ep, HE.reply, HE.tcb, fun _ h => h.elim⟩ + refine WP.spec_mono (WP.spec_and + (suspend_refinesA (st := st) (ar := ar) hP P hc hmb hmax htu s a σ H RN HE HS M Y hC0 ht X.tcb X.reply X.ep + X.ntfn X.rep X.runW X.runS X.on) + (suspend_footA (st := st) (ar := ar) hP0 P0' hc hmb hmax htu s a σ H RN HE0 HS M Y hC0 ht X.tcb X.reply X.ep + X.ntfn X.rep X.runW X.runS X.on)) ?_ + rintro out ⟨⟨ha, σ1, stk, h1, rn, rel, ep, rs, mcp, yr, -, hy⟩, σ2, h2, w, F⟩ + have e : σ2 = σ1 := + congrArg Prod.snd (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst (h2.symm.trans h1))) + subst e + exact ⟨ha, σ2, stk, h1, w.mono fun _ h => h.resolve_right fun ⟨_, h, _⟩ => h.elim, F, rn, rel, ep, rs, mcp, yr, hy⟩ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} [J7PpA st J7] {s s' : S} {a : A} {σ σ' : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} + +/-- A thread a slot names by a thread cap is no core's idle thread (`NoIdleCapA`, F6): `suspend_recA`'s `hti` and +`hidles`. -/ +theorem JStage3TA.notIdle_of_capA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (hcur : m.curCore < n) {t : U64} {sl : Fl4SpecA64.CslotPtr} + (h : specCapAtA σ sl = some (.threadCap (specPtr t))) : + specPtr t ≠ σ.idleThread ∧ ∀ c < n, m.idleThreadOf c ≠ specPtr t := by + have hidles : ∀ c < n, m.idleThreadOf c ≠ specPtr t := fun c hc e => (J.sidle.f6 sl).1 c hc (e ▸ h) + refine ⟨fun e => hidles m.curCore hcur ?_, hidles⟩ + rw [e, J.sr.sch.idle, J.sr.sch.core, J.sr.sch.idleOf _ J.sr.sch.cpuCs] + rfl + +/-- **The deletion loop's other record fields across `suspend`** (`DelXA`'s `ParentAdjA`, `ZombieTcbNoVcpuA`, +`BufHold`, `CtOff`, `DelCapFxA`; `blkQ` is a record field), from `suspend_recA`'s write set and `OSKeep`. `lk` is the +loop state's `DStateA.lk`. -/ +theorem JStage3TA.susX_keepA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + {G : MdbGhostA} (lk : MdbLk st OS G.chains s) + (w : WritesIn st s s' (SchedStepNWA (st := st) N E Rp Z m σ s)) (osk : OSKeep st OS s s') + (hPA : ParentAdjA st G.chains G.par s) (hZT : ZombieTcbNoVcpuA st OS s) + (hBH : Fl4ProofsA64.P1.W7.R.ST.BufHold st H OS s) + (hCT : Fl4ProofsA64.P1.W7.R.ST.CtOff st (L.ksb s) OS s) (hFx : DelCapFxA st Z H s) : + ParentAdjA st G.chains G.par s' ∧ ZombieTcbNoVcpuA st OS s' ∧ Fl4ProofsA64.P1.W7.R.ST.BufHold st H OS s' ∧ + Fl4ProofsA64.P1.W7.R.ST.CtOff st (L.ksb s') OS s' ∧ DelCapFxA st Z H s' := by + have caps : ∀ y ∈ OS, Fl4ProofsA64.P1.W7.R.ST.capA st s' y = Fl4ProofsA64.P1.W7.R.ST.capA st s y := + fun y hy => osk.capA hy + refine ⟨ParentAdjA.keepOS lk osk hPA, ZombieTcbNoVcpuA.keepA caps J.zombieTcb (fun u hu => ?_) hZT, + bufHold_sameA st hBH caps, ctOff_sameA st hCT w.1 caps, hFx.keepA st J.j5.2.1 caps⟩ + have huT : u ∈ Z.T := J.tcbs ▸ hu + have hfit := (J.sr.core.tcbs u huT).fits + have hv : (wa u 3664#u64).val = u.val + 3664 := by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by simp; omega) + refine w.mem_eq (field_aligned (J.sr.core.tcbs u huT) (by simp) (by simp)) (fun h => ?_) + rcases h with (hm | ⟨v, hv', h1, h2, h3, -⟩) | hipc + · exact J.tcbWord_offMA huT (by simp) (by simp) hm + · by_cases e : v = u + · subst e; exact h3 rfl + · have := J.sr.place.apart v u .tcb .tcb ((J.sr.thT v).2 hv') ((J.sr.thT u).2 huT) e + have hsz : ObjKindA.size .tcb = 4096 := rfl + omega + · exact J.ipc_off_hobjA (v := u) (kv := .tcb) hu (by simp) + ⟨by omega, by simp only [HKind.bits]; omega⟩ hipc + +/-- **`BufCover` across `suspend`**: the slot words by the scheduler write set, the `IPC_BUFFER` words by A64 P1's +footprint. -/ +theorem JStage3BA.bufCover_susA + (J : JStage3BA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (w : WritesIn st s s' (SchedStepNWA (st := st) N E Rp Z m σ s)) + (f : Fl4ProofsA64.P1.K8.FootD st H.tcbs Z.Cs H.frame H.scs H.scBits (EH H) s s') + (K : SStepA σ σ') : BufCover st Z.T Bf s' σ' := by + refine J.bufCover_wA (w.and f) (fun u hu K0 hK h => ?_) (fun p x hx => ?_) + swap + · obtain ⟨y, hy, e, -⟩ := K.tcb p x hx + exact ⟨y, hy, e.2, e.1⟩ + have htH : u ∈ H.tcbs := by rw [J.tcbs]; exact hu + rcases hK with hK | hK + swap + · have eK : K0 = 3512#u64 := UScalar.eq_of_val_eq (by rw [hK]; rfl) + subst eK + exact Fl4ProofsA64.P1.ib_not_blk J.blkA htH h.2 + have hfit := (J.sr.core.tcbs u hu).fits + have hv : (wa u K0).val = u.val + K0.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + rcases h.1 with (hm | ⟨v, hv', h1, h2, -⟩) | hipc + · exact J.tcbWord_offMA hu (by omega) (by omega) hm + · by_cases e : v = u + · subst e; omega + · have := J.sr.place.apart v u .tcb .tcb ((J.sr.thT v).2 hv') ((J.sr.thT u).2 hu) e + have hsz : ObjKindA.size .tcb = 4096 := rfl + omega + · exact J.ipc_off_hobjA (v := u) (kv := .tcb) htH (by simp) ⟨by omega, by simp only [HKind.bits]; omega⟩ hipc + +/-- **`vr` after `suspend`**: a reply left on a stack is not `t`'s (it was unlinked), and every other thread keeps +its state and `TCB.REPLY` word. -/ +theorem JStage3BA.vr_susA + (J : JStage3BA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (blk' : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s') + {t : U64} {x : Fl4SpecA64.Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + (K : BlkOthA (specPtr t) σ σ') (oth : Fl4ProofsA64.P1.OthF st H.tcbs t s s') + {stack' : U64 → List U64} (hsub : ∀ c, ∀ r ∈ stack' c, r ∈ stack c) + (hnt : ∀ sc ∈ Z.C, ∀ r ∈ stack' sc, x.tcbState = .blockedOnReply (specPtr r) → False) + (rs : ReplyStackRelA (L.mem s') Rp Z.C σ' (scNameA m) stack') + (E' : EpStateRel Z.T E Rp Bf (L.mem s') σ') : + ∀ sc ∈ Z.C, ∀ r ∈ stack' sc, RBlk st s' r := by + intro sc hc r h + have hr := hsub sc r h + have mR : r ∈ Rp := (J.reply.stackRel sc hc).2.1 r hr + have old := J.vr sc hc r hr + have back := J.blkA.2.2.1.2.1.back r (J.utIpc.reply r mR) old.tcb + obtain ⟨c, ec⟩ : ∃ c, L.mem s (Fl4ProofsA64.P1.rTcbA r) = c := ⟨_, rfl⟩ + have hcH : c ∈ H.tcbs := ec ▸ back.2.1 + have cT : c ∈ Z.T := J.tcbs ▸ hcH + have h5 : (L.mem s (Fl4ProofsA64.P1.stA c)).val = 5 := by + have e := old.blk + rw [ec] at e + rw [e]; unfold sched.TS_BLOCKED_ON_REPLY; rfl + obtain ⟨y, hy, rel⟩ := J.ep.tcb c cT + have h5x : Notif.tsCode y.tcbState = 5 := by rw [← rel.st]; exact h5 + obtain ⟨q, hq⟩ := tsCode_five h5x + have eA : Fl4ProofsA64.P1.replyA c = tReplyA c := by + unfold Fl4ProofsA64.P1.replyA tReplyA; rfl + have er0 : L.mem s (tReplyA c) = r := by rw [← eA, ← ec]; exact back.2.2.1 + have eq : q = specPtr r := by rw [← rel.reply q hq, er0] + have ne : c ≠ t := by + intro e + rw [e] at hy + rw [hk] at hy + cases hy + rw [eq] at hq + exact hnt sc hc r h hq + obtain ⟨y1, hy1, ey⟩ := K.tcb _ _ (fun h => ne (specPtr_inj h)) hy + obtain ⟨y', hy', rel'⟩ := E'.tcb c cT + rw [hy1] at hy' + cases hy' + have e5 : L.mem s' (Fl4ProofsA64.P1.stA c) = sched.TS_BLOCKED_ON_REPLY := by + apply UScalar.eq_of_val_eq + have : (L.mem s' (Fl4ProofsA64.P1.stA c)).val = 5 := by + have := rel'.st + rw [ey, h5x] at this + exact this + rw [this]; unfold sched.TS_BLOCKED_ON_REPLY; rfl + have R' := blk'.2.2.1.2.1.reply c hcH e5 + have er : L.mem s' (Fl4ProofsA64.P1.replyA c) = r := by + rw [eA, show tReplyA c = wa c 3360#u64 from rfl, + oth c hcH ne 3360#u64 (by simp) (by simp) (by simp) (by simp) (by simp) (by simp)] + exact er0 + have et : L.mem s' (Fl4ProofsA64.P1.rTcbA r) = c := by + have := R'.2 + rwa [er] at this + exact ⟨by rw [et, ← ec]; exact old.tcb, by rw [et]; exact e5⟩ + +/-- **`BlkQueuedA` after `suspend`**. -/ +theorem JStage3BA.blkQ_susA + (J : JStage3BA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {t : U64} (hy : ∃ y, σ'.kheap (specPtr t) = some (.tcb y) ∧ y.tcbState = .inactive) + (K : BlkOthA (specPtr t) σ σ') (E' : EpStateRel Z.T E Rp Bf (L.mem s') σ') + (rn : NtfnStateRel N Z.T (L.mem s') σ') : BlkQueuedA Z.T E N (L.mem s') σ' where + ep := fun u hu x' hk e hb => by + have ne : specPtr u ≠ specPtr t := fun h => by + obtain ⟨y, h1, h2⟩ := hy + rw [h, h1] at hk; cases hk + rw [h2] at hb; cases hb + obtain ⟨x, hx, rel⟩ := J.ep.tcb u hu + obtain ⟨z, hz, es⟩ := K.tcb _ _ ne hx + rw [hk] at hz; cases hz + have hb0 : Fl4SpecA64.epBlocked x.tcbState = some e := es ▸ hb + obtain ⟨mE, y, h1, h2⟩ := J.blkQ.ep u hu x hx e hb0 + obtain ⟨y', h1', h2'⟩ := K.ep _ _ _ ne h1 h2 + obtain ⟨x2, hx2, rel'⟩ := E'.tcb u hu + rw [hk] at hx2; cases hx2 + have em : L.mem s' (tBlockA u) = L.mem s (tBlockA u) := + specPtr_inj ((rel'.blk e hb).trans (rel.blk e hb0).symm) + refine ⟨?_, y', h1', h2'⟩ + show L.mem s' (tBlockA u) ∈ E + rw [em]; exact mE + ntfn := fun u hu x' hk p hb => by + have ne : specPtr u ≠ specPtr t := fun h => by + obtain ⟨y, h1, h2⟩ := hy + rw [h, h1] at hk; cases hk + rw [h2] at hb; cases hb + obtain ⟨x, hx, rel⟩ := J.ntfn.tcb u hu + obtain ⟨z, hz, es⟩ := K.tcb _ _ ne hx + rw [hk] at hz; cases hz + have hb0 : x.tcbState = .blockedOnNotification p := es.symm.trans hb + obtain ⟨mN, y, q, h1, h2, h3⟩ := J.blkQ.ntfn u hu x hx p hb0 + obtain ⟨y', q', h1', h2', h3'⟩ := K.ntfn _ _ _ _ ne h1 h2 h3 + obtain ⟨x2, hx2, rel'⟩ := rn.tcb u hu + rw [hk] at hx2; cases hx2 + have em : L.mem s' (Notif.tBlockA u) = L.mem s (Notif.tBlockA u) := + specPtr_inj ((rel'.blk p hb).trans (rel.blk p hb0).symm) + exact ⟨by rw [em]; exact mN, y', q', h1', h2', h3'⟩ + +end + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J7 : JSlotA S} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +set_option maxHeartbeats 2000000 in +/-- **`sched.suspend` at the Stage-3 record** (module docstring). -/ +theorem JStage3TA.suspend_recA [J7PpA st J7] [J7PpKA st J7] + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + (hc : m.curCore < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {t : U64} (ht : t ∈ Z.T) {x : Fl4SpecA64.Tcb} (X : TcbSuspendReadyA st Z m N E Rp s σ t x) + (hQ7 : ∀ Q Rl, J7 { Z with Q := Q, R := Rl } = J7 Z) + (hti : specPtr t ≠ σ.idleThread) (hidles : ∀ c < n, m.idleThreadOf c ≠ specPtr t) : + sched.suspend st ar s a t ⦃ out => out.2 = a ∧ ∃ σ' stack' Q Rl, + (fl4CancelRun (Z := Unit) (specPtr t) >>= fun _ => Fl4SpecA64.suspend (specPtr t)) σ = + ({((), σ')}, False) ∧ + WritesIn st s out.1 (SchedStepNWA (st := st) N E Rp Z m σ s) ∧ + SchedFrameA σ σ' ∧ OSKeep st OS s out.1 ∧ + (∃ y, σ'.kheap (specPtr t) = some (.tcb y) ∧ y.tcbState = .inactive) ∧ + JStage3TA st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack' κ n (J5A st) J7 + out.1 out.2 σ' m ⦄ := by + have htH : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + have hna : L.actionW s ≠ t := by + intro e + have h01 := tcb_ne01Sc (J.sr.core.tcbs t ht) + refine J.noSwitch (specPtr t) ?_ + rw [J.sr.sch.act, e] + unfold actOfA + rw [if_neg h01.1, if_neg h01.2] + refine WP.spec_mono (WP.spec_and + (suspend_stepA (st := st) (ar := ar) (κ := κ) J.iplaced J.ipcPlace hc hmb hmax htu s a σ ⟨Z.Q, Z.R, J.sr⟩ + J.ntfn J.ep J.reply J.mcp J.yieldRel J.scNZ ht X) + (WP.spec_and + (Fl4ProofsA64.P1.K8.sched.suspend.blk (st := st) (ar := ar) s a t J.blkA htH hna X.runW) + (WP.spec_and + (Fl4ProofsA64.P1.sched.suspend.rvi (st := st) (ar := ar) s a t J.blkA htH X.runW + (by rw [J.tcbs]; exact J.runVcpu)) + (Fl4ProofsA64.P1.sched.suspend.othF (st := st) (ar := ar) s a t J.blkA htH X.runW)))) ?_ + rintro ⟨s', a'⟩ ⟨⟨ha, σ', stk', hspec, w, F, rn, rel, ep, rs, mcp, yr, hy⟩, ⟨blk, foot⟩, rv, oth⟩ + simp only at ha + subst a' + rw [J.tcbs] at rv + have kb : L.ksb s' = L.ksb s := w.1 + have osk : OSKeep st OS s s' := J.sigW_osKeepA w + obtain ⟨Q, Rl, sr⟩ := rel + have ent := J.ent_stepNWA w blk + have sq : SchedQueuesInv st H.tcbs Z.Cs H.omega H s' Q Rl := by + obtain ⟨Q0, R0, h0⟩ := blk.1 + exact SchedQueuesInv.reQA h0 (by rw [J.tcbs]; exact sr.core) + have poolOff : PoolOff st H Z.Cs OS s' := fun i => by + have h0 := J.poolOff i + rw [vspace.asid_table.eq] at h0 ⊢ + have v := AtOff.v i + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.ent.ksw + have ev : (wa (L.ksb s) (AtOff i)).val = (L.ksb s).val + (AtOff i).val := by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have e : L.mem s' (wa (L.ksb s') (AtOff i)) = L.mem s (wa (L.ksb s) (AtOff i)) := by + rw [kb] + refine w.mem_eq ?_ (J.asid_offStepNA (by omega) (by omega)) + have h32 := J.j.1.ksbAl + show (wa (L.ksb s) (AtOff i)).val % 8 = 0 + omega + rw [e] + refine (WP.spec_ok _).2 fun hne asid => ?_ + have q := (WP.spec_ok _).1 h0 hne asid + exact ⟨q.al, q.om, q.sg, q.os, RegionOffNodes.ksb kb q.ks⟩ + have j := J.j57_stepNWA w σ' + obtain ⟨HS, SSz, hum⟩ := F + classical + -- the stacks off `C` are unconstrained by the relation; take them empty + obtain ⟨stk, estk⟩ : ∃ f : U64 → List U64, f = fun c => if c ∈ Z.C then stk' c else [] := ⟨_, rfl⟩ + have eC : ∀ c ∈ Z.C, stk c = stk' c := fun c hc => by rw [estk]; exact if_pos hc + have rs : ReplyStackRelA (L.mem s') Rp Z.C σ' (scNameA m) stk := + { stackRel := fun c hc => by rw [eC c hc]; exact rs.stackRel c hc + unique := fun c hc c' hc' r h h' => by + rw [eC c hc] at h; rw [eC c' hc'] at h'; exact rs.unique c hc c' hc' r h h' + owner := fun r hr x hx p => (rs.owner r hr x hx p).trans + (exists_congr fun c => and_congr_right fun hc => by rw [eC c hc]) + detached := fun r hr h => by + have h' : ∀ c ∈ Z.C, r ∉ stk' c := fun c hc => by rw [← eC c hc]; exact h c hc + obtain ⟨a, b, d⟩ := rs.detached r hr h' + refine ⟨a, fun ne => ?_, fun ne => ?_⟩ + · obtain ⟨m1, m2, m3⟩ := b ne + exact ⟨m1, fun c hc => by rw [eC c hc]; exact m2 c hc, m3⟩ + · obtain ⟨m1, m2, m3⟩ := d ne + exact ⟨m1, fun c hc => by rw [eC c hc]; exact m2 c hc, m3⟩ } + have hsus : ((), σ') ∈ (Fl4SpecA64.suspend (Z := Unit) (specPtr t) σ).1 := by + have h0 : ((), σ') ∈ ((fl4CancelRun (Z := Unit) (specPtr t) >>= fun _ => Fl4SpecA64.suspend (specPtr t)) σ).1 := by + rw [hspec]; exact rfl + rwa [Fl4ProofsA64.P3.Notif.nd_bind_one (fl4CancelRun_quietA X.tcb X.runS)] at h0 + have K : SStepA σ σ' := suspend_sstepA (specPtr t) σ _ hsus + have hnq : ∀ d p, specPtr t ∉ σ'.readyQueues d p := fun d p hm => by + obtain ⟨z, hz, hr⟩ := sr.run.1 d p _ hm + obtain ⟨y, hy1, hy2⟩ := hy + rw [hy1] at hz + cases hz + rw [hy2] at hr + cases hr + have inv := suspend_invA (m := m) (n := n) ⟨J.sinv, J.sidle, J.sact⟩ J.noSwitch X.tcb + (fun e' he => (X.ep e' he).2) (fun n' he => (X.ntfn n' he).2.imp fun _ h => h.1) X.rep hti hidles hsus hnq + have hsub : ∀ c, ∀ r ∈ stk c, r ∈ stack c := fun c r hr => by + by_cases hc : c ∈ Z.C + · exact K.stackSubA J.reply rs c hc r hr + · rw [estk] at hr + simp only [if_neg hc] at hr + cases hr + have bufAl : UtBufAlA Z.T σ' := K.utBufAlA J.bufAl fun u hu => by + obtain ⟨x0, hx0, -⟩ := J.ntfn.tcb u hu + exact ⟨x0, hx0⟩ + have idle : IdleNoFaultA m σ' := K.idleNoFaultA J.idleNoFault + have sinv := inv.1.sinv + have sidle := inv.1.sidle + have sact := inv.1.sact + have nsw := inv.2 + have KB : BlkOthA (specPtr t) σ σ' := suspend_blkOthA X.tcb (fun e' he => (X.ep e' he).2) + (fun n' he => (X.ntfn n' he).2.imp fun _ h => h.1) hsus + have buf : BufCover st Z.T Bf s' σ' := J.toBA.bufCover_susA w (Fl4ProofsA64.P1.K8.Foot.d foot) K + have bq : BlkQueuedA Z.T E N (L.mem s') σ' := J.toBA.blkQ_susA hy KB ep rn + have hvr : ∀ sc ∈ Z.C, ∀ r ∈ stk sc, RBlk st s' r := + J.toBA.vr_susA blk X.tcb KB oth hsub (fun sc hc r h hq => by + obtain ⟨⟨z, k, hz, -⟩, -⟩ := J.reply.stackRel sc hc + have hsw := J.reply.scWithReply_some hc (hsub sc r h) (J.scCov.reply _) + obtain ⟨⟨z', k', hz', hl⟩, -⟩ := rs.stackRel sc hc + exact suspend_unlinkedA X.tcb hq hz hsw hsus z' k' hz' (by rw [hl]; exact List.mem_map_of_mem h)) rs ep + have scCov : ScCovA Z.C m σ' := fun r y k h => by + obtain ⟨y', hy'⟩ := SSz r y k h + exact J.scCov r y' k hy' + have J' := JStage3TA.of_schedWrSN (blkQ := bq) J (stack' := stk) hsub (Ac' := Ac) (Q := Q) (Rl := Rl) + (curOf := Z.curOf) (curScOf := Z.curScOf) (s' := s') (a' := a) (σ' := σ') w HS SSz (Sched.qk_of hsus) hum J.j.1.ar (fun _ _ => rfl) + bufAl ent sq poolOff sr scCov ep rn rs buf idle mcp yr sinv sidle rv hvr nsw sact j.1 (by rw [hQ7]; exact j.2) + exact ⟨rfl, σ', stk, Q, Rl, hspec, w, ⟨HS, SSz, hum⟩, osk, hy, J'⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelSuspendLiveA.lean b/proofs/Fl4ProofsA64/P3/DelSuspendLiveA.lean new file mode 100644 index 000000000..0e4320045 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelSuspendLiveA.lean @@ -0,0 +1,121 @@ +/- +P3 (AArch64, delete hooks, contract §90 Step-0 / §93): **`sched.suspend` at the Stage-3 record with a live VCPU run** +(`RUN_VCPU ≠ 0` allowed), the arm `JStage3TA.suspend_recA` leaves out. + +* `fl4CancelRun_detA`: at a record state `fl4CancelRun t` is deterministic (one outcome, the one S4's + `vcpu_cancel_run_stage3TA` names), and keeps the idle thread. +* `JStage3TA.suspend_liveRecA`: FL4's `sched.suspend` first runs `vcpu.cancel_run` + (`vcpu_cancel_run_stage3TA`); at the post-state `RUN_VCPU = 0`, so the second, inner `cancel_run` is a no-op + (`cancel_run_quietA`) and `suspend_recA` applies there (`suspReadyA` gives its readiness). On the spec side + `fl4CancelRun t` at the intermediate state is quiet (`fl4CancelRun_quietA`), so + `(fl4CancelRun t >>= suspend t) σ` is the outcome `suspend_recA` gives at `σ1`. + It also posts `SchedValidA` at the end and, at `J7 = J7LA`, that `Std6A` is kept across both steps. + The writes are stated in two steps: `VcpuWrNoStA` at the entry state, then `SchedStepNWA` at `s1` / `σ1`. + +Premises: [P] `hCs`, `hmb`, `hmax`, `htu`; [S] `h1`; the caller facts of `suspend_recA` (`hQ7`, `hti`, `hidles`) +and S4's `VcpuRec6A` / `SchedValidA` at the entry. New module. +-/ +import Fl4ProofsA64.P3.DelThrSusA +import Fl4ProofsA64.P3.TcbCancelRunA +import Fl4ProofsA64.P3.VcpuDelStepsA +import Fl4ProofsA64.P3.DelRec6A +import Fl4ProofsA64.P3.Stage3DelSvSuspendA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 Fl4ProofsA64.P3.Endp +open Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Vcpu + +local notation "wa" => core.num.U64.wrapping_add + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- **`fl4CancelRun t` at a record state has the one outcome it is given** (and keeps the idle thread). -/ +theorem fl4CancelRun_detA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {t : U64} (ht : t ∈ Z.T) {σ1 : Fl4SpecA64.State Unit} + (hcan : ((), σ1) ∈ (fl4CancelRun (Z := Unit) (specPtr t) σ).1) : + fl4CancelRun (Z := Unit) (specPtr t) σ = ({((), σ1)}, False) ∧ σ1.idleThread = σ.idleThread := by + obtain ⟨xt, hkt, -, hrv⟩ := J.j.2.1.tcb t ht + by_cases hr0 : L.mem s (runVcpuA t) = 0#u64 + · have h := fl4CancelRun_noneA hkt (by rw [hrv, hr0]; unfold optRefA; simp) + rw [h] at hcan + have e : σ1 = σ := congrArg Prod.snd (show ((), σ1) = ((), σ) from hcan) + subst e + exact ⟨h, rfl⟩ + · have hvcV : L.mem s (runVcpuA t) ∈ Z.V := (J.vcRun.run t ht).resolve_left hr0 + obtain ⟨xv, hkv, -, -⟩ := J.j.2.1.obj _ hvcV + have h := fl4CancelRun_someA hkt hkv (by rw [hrv]; unfold optRefA; simp [hr0]) + rw [h] at hcan + have e := congrArg Prod.snd (show ((), σ1) = ((), _) from hcan) + subst e + exact ⟨h, rfl⟩ + +set_option maxHeartbeats 1600000 in +/-- **`sched.suspend` at the record, live VCPU run allowed** (module docstring). -/ +theorem JStage3TA.suspend_liveRecA {ar : machine.Arch A} [J7PpA st J7] [J7PpKA st J7] [J7FrA st J7] + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + (hSV : SchedValidA σ) (R6 : VcpuRec6A st Z OS Bf s σ) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {t : U64} (ht : t ∈ Z.T) + (hQ7 : ∀ Q Rl, J7 { Z with Q := Q, R := Rl } = J7 Z) + (hti : specPtr t ≠ σ.idleThread) (hidles : ∀ c < n, m.idleThreadOf c ≠ specPtr t) : + sched.suspend st ar s a t ⦃ out => out.2 = a ∧ ∃ (s1 : S) (σ1 σ' : Fl4SpecA64.State Unit) + (stack' : U64 → List U64) (Q : U64 → U64 → List U64) (Rl : U64 → List U64), + (fl4CancelRun (Z := Unit) (specPtr t) >>= fun _ => Fl4SpecA64.suspend (specPtr t)) σ = + ({((), σ')}, False) ∧ + ((), σ1) ∈ (fl4CancelRun (Z := Unit) (specPtr t) σ).1 ∧ + WritesIn st s s1 (VcpuWrNoStA st Z OS s) ∧ + WritesIn st s1 out.1 (SchedStepNWA (st := st) N E Rp Z m σ1 s1) ∧ + SchedFrameA σ1 σ' ∧ OSKeep st OS s out.1 ∧ + (∃ y, σ'.kheap (specPtr t) = some (.tcb y) ∧ y.tcbState = .inactive) ∧ + JStage3TA st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack' κ n (J5A st) J7 + out.1 out.2 σ' m ∧ + SchedValidA σ' ∧ + (∀ R7 : Set U64 → JSlotA S, J7 = J7LA st R7 → (∀ y, IsSlotA Z.th y → y ∈ OS) → + Std6A st Z H OS Bf s σ → Std6A st Z H OS Bf out.1 σ') ⦄ := by + have hcr := vcpu_cancel_run_stage3TA (ar := ar) (Clock := fun _ _ => True) J hSV R6 trivial ht + unfold sched.suspend + refine WP.spec_bind hcr ?_ + rintro ⟨s1, a1⟩ ⟨σ1, ha1, hcan, J1, hSV1, R1, -, -, -, hrw1, -, -, -, osk1, w1, -⟩ + dsimp only at ha1 hcan J1 R1 hrw1 osk1 w1 ⊢ + subst a1 + obtain ⟨hdet, hid⟩ := fl4CancelRun_detA J ht hcan + obtain ⟨x1, X⟩ := J1.suspReadyA hCs h1 ht hrw1 + have key := J1.suspend_recA (J.curCore_lt hCs) hmb hmax htu ht X hQ7 (by rw [hid]; exact hti) hidles + unfold sched.suspend at key + rw [cancel_run_quietA s1 a t hrw1, bind_ok] at key + refine WP.spec_mono key ?_ + rintro ⟨s', a'⟩ ⟨ea, σ', stk', Q, Rl, sp, w, F, osk, hy, J'⟩ + have hq1 : fl4CancelRun (Z := Unit) (specPtr t) σ1 = ({((), σ1)}, False) := by + obtain ⟨x', hx', -, hrv⟩ := J1.j.2.1.tcb t ht + exact fl4CancelRun_quietA hx' (by rw [hrv, hrw1]; unfold optRefA; simp) + rw [nd_bind_oneSc hq1] at sp + refine ⟨ea, s1, σ1, σ', stk', Q, Rl, ?_, hcan, w1, w, F, OSKeep.transA osk1 osk, hy, J', ?_, ?_⟩ + · rw [nd_bind_oneSc hdet] + exact sp + · exact suspend_schedValid (specPtr t) σ1 hSV1 ((), σ') (by rw [sp]; rfl) + · intro R7 e sub h + subst e + have capE : ∀ u ∈ OS, capA st s1 u = capA st s u := fun u hu => osk1.capA hu + have h1c : Std6A st Z H OS Bf s1 σ1 := + ⟨UtWinCapA.keepA capE h.utWin, h.devWin.keepA capE, R1.vcBack, R1.runBack, R1.snap, R1.vcOS, R1.userFr, R1.frTh, R1.bfSpec, R1.vcSym1, + R1.vcSym2, h.heapWin, h.coresAll, DelInvA.keepW h.zwf fun y hy => capE y (sub y hy), R1.frOff, + FrAgA.nilA⟩ + exact Std6GA.schedStepA J1 h1c sub w osk F + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelSuspendSpecA.lean b/proofs/Fl4ProofsA64/P3/DelSuspendSpecA.lean new file mode 100644 index 000000000..5ae0b321c --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelSuspendSpecA.lean @@ -0,0 +1,1000 @@ +/- +P3 (AArch64, delete hooks, slice "record-level `sched.suspend`"): **the spec-side post-state facts of l4v +`suspend`**, over every outcome. + +* `SStepA`: the idle thread is kept; a TCB stays a TCB with its IPC buffer, and a fault-free one stays fault-free; + an SC keeps its size and its reply list only shrinks. `suspend_sstepA`: `suspend` is an `SStepA` step. +* `suspend_invA`: `SInvXA ∧ SIdleA ∧ SActA` and "no pending switch" across `suspend`. +* `SStepA.utBufAlA`, `SStepA.idleNoFaultA`, `SStepA.stackSubA`: the record facts read off `SStepA`. + +Hypotheses of `suspend_invA`: the suspended thread is not the idle thread of any core (`hti`, `hidles`; suspending +an idle thread would falsify `SIdleA`), and it is in no ready queue AFTER `suspend` (`hnq`; `SInvXA` does not say +that a thread is queued only at its own domain and priority, so this is read from the post-state heap relation). + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.ReplyRemoveInvA +import Fl4ProofsA64.P3.DelScSpecA +import Fl4ProofsA64.P3.ReplyCancelFootA +import Fl4ProofsA64.P3.DelEpLoopA +import Fl4ProofsA64.P3.Stage3TcbSuspFrA +import Fl4ProofsA64.P3.TcbRegsContA +import Fl4ProofsA64.P3.Stage3D1bA +import Fl4ProofsA64.P3.IpcScGlueA +import Fl4ProofsA64.P3.EpReplyStackA + +namespace Fl4ProofsA64.P3 +open Aeneas Aeneas.Std Fl4Spec Fl4SpecA64 Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Endp + +noncomputable section + +/-! ## The object step -/ + +/-- What `suspend` keeps of TCBs and SCs (module docstring). -/ +structure SStepA (σ σ' : State Unit) : Prop where + idle : σ'.idleThread = σ.idleThread + tcb : ∀ p x, σ.kheap p = some (.tcb x) → ∃ y, σ'.kheap p = some (.tcb y) ∧ (y.tcbIpcBuffer = x.tcbIpcBuffer ∧ y.tcbIpcframe = x.tcbIpcframe) ∧ + (x.tcbFault = none → y.tcbFault = none) + sc : ∀ p x n, σ.kheap p = some (.schedContext x n) → ∃ y, σ'.kheap p = some (.schedContext y n) ∧ + ∀ r ∈ y.scReplies, r ∈ x.scReplies + +theorem SStepA.of_kheap {σ σ' : State Unit} (hi : σ'.idleThread = σ.idleThread) (hk : σ'.kheap = σ.kheap) : + SStepA σ σ' := + ⟨hi, fun _ x h => ⟨x, by rw [hk]; exact h, ⟨rfl, rfl⟩, id⟩, fun _ x _ h => ⟨x, by rw [hk]; exact h, fun _ a => a⟩⟩ + +instance : PreO SStepA := + ⟨fun _ => SStepA.of_kheap rfl rfl, fun {a b c} h1 h2 => by + refine ⟨h2.idle.trans h1.idle, fun p x h => ?_, fun p x n h => ?_⟩ + · obtain ⟨y, hy, e, f⟩ := h1.tcb p x h + obtain ⟨z, hz, e', f'⟩ := h2.tcb p y hy + exact ⟨z, hz, ⟨e'.1.trans e.1, e'.2.trans e.2⟩, fun g => f' (f g)⟩ + · obtain ⟨y, hy, s⟩ := h1.sc p x n h + obtain ⟨z, hz, s'⟩ := h2.sc p y n hy + exact ⟨z, hz, fun r hr => s r (s' r hr)⟩⟩ + +theorem SStepA.updTcb {σ : State Unit} {p : ObjRef} {x y : Tcb} (hk : σ.kheap p = some (.tcb x)) + (hb : y.tcbIpcBuffer = x.tcbIpcBuffer ∧ y.tcbIpcframe = x.tcbIpcframe) (hf : x.tcbFault = none → y.tcbFault = none) : + SStepA σ { σ with kheap := Function.update σ.kheap p (some (.tcb y)) } := by + refine ⟨rfl, fun q z h => ?_, fun q z n h => ?_⟩ + · by_cases e : q = p + · subst e + rw [hk] at h; cases h + exact ⟨y, Function.update_self _ _ _, hb, hf⟩ + · exact ⟨z, by show Function.update σ.kheap p _ q = _; rw [Function.update_of_ne e]; exact h, ⟨rfl, rfl⟩, id⟩ + · have e : q ≠ p := fun e => by rw [e, hk] at h; cases h + exact ⟨z, by show Function.update σ.kheap p _ q = _; rw [Function.update_of_ne e]; exact h, fun _ a => a⟩ + +theorem SStepA.updSc {σ : State Unit} {p : ObjRef} {x y : SchedContext} {k : Nat} + (hk : σ.kheap p = some (.schedContext x k)) (hs : ∀ r ∈ y.scReplies, r ∈ x.scReplies) : + SStepA σ { σ with kheap := Function.update σ.kheap p (some (.schedContext y k)) } := by + refine ⟨rfl, fun q z h => ?_, fun q z n h => ?_⟩ + · have e : q ≠ p := fun e => by rw [e, hk] at h; cases h + exact ⟨z, by show Function.update σ.kheap p _ q = _; rw [Function.update_of_ne e]; exact h, ⟨rfl, rfl⟩, id⟩ + · by_cases e : q = p + · subst e + rw [hk] at h; cases h + exact ⟨y, Function.update_self _ _ _, hs⟩ + · exact ⟨z, by show Function.update σ.kheap p _ q = _; rw [Function.update_of_ne e]; exact h, fun _ a => a⟩ + +theorem SStepA.updOther {σ : State Unit} {p : ObjRef} {o o' : KernelObject} (hk : σ.kheap p = some o) + (ht : ∀ x, o ≠ .tcb x) (hs : ∀ x n, o ≠ .schedContext x n) : + SStepA σ { σ with kheap := Function.update σ.kheap p (some o') } := by + refine ⟨rfl, fun q z h => ?_, fun q z n h => ?_⟩ + · have e : q ≠ p := fun e => by rw [e, hk] at h; exact ht z (Option.some.inj h) + exact ⟨z, by show Function.update σ.kheap p _ q = _; rw [Function.update_of_ne e]; exact h, ⟨rfl, rfl⟩, id⟩ + · have e : q ≠ p := fun e => by rw [e, hk] at h; exact hs z n (Option.some.inj h) + exact ⟨z, by show Function.update σ.kheap p _ q = _; rw [Function.update_of_ne e]; exact h, fun _ a => a⟩ + +/-! ## The primitives -/ + +theorem eq_sstepA {α : Type} {m : NonDet (State Unit) α} (h : PresR (@Eq (State Unit)) m) : PresR SStepA m := + fun σ r hr => by rw [← h σ r hr]; exact PreO.refl σ + +theorem threadSet_sstepA (f : Tcb → Tcb) (t : ObjRef) + (hf : ∀ x, ((f x).tcbIpcBuffer = x.tcbIpcBuffer ∧ (f x).tcbIpcframe = x.tcbIpcframe) ∧ + (x.tcbFault = none → (f x).tcbFault = none)) : + PresR SStepA (threadSet (Z := Unit) f t) := fun σ r hr => by + obtain ⟨x, hx, e⟩ := threadSet_memA f t hr + rw [e] + exact SStepA.updTcb hx (hf x).1 (hf x).2 + +theorem updateSchedContext_sstepA (p : ObjRef) (f : SchedContext → SchedContext) + (hf : ∀ x, ∀ r ∈ (f x).scReplies, r ∈ x.scReplies) : PresR SStepA (updateSchedContext (Z := Unit) p f) := + fun σ r hr => by + obtain ⟨y, n, hy, e⟩ := updateSchedContext_memA p f hr + rw [e] + exact SStepA.updSc hy (hf y) + +theorem setSimpleKo_sstepA {α : Type} [Nonempty α] (C : α → KernelObject) (p : ObjRef) (v : α) + (ht : ∀ a x, C a ≠ .tcb x) (hs : ∀ a x n, C a ≠ .schedContext x n) : + PresR SStepA (setSimpleKo (Z := Unit) C p v) := fun σ r hr => by + unfold setSimpleKo at hr + obtain ⟨o, ho, hr⟩ := eqPres_mem (getObject_eqPres p) hr + obtain ⟨_, -, hr⟩ := eqPres_mem (PresR.assert _) hr + obtain ⟨_, hpi, hr⟩ := eqPres_mem (PresR.assert _) hr + obtain ⟨ko, -, e⟩ := setObject_memA hr + have hgo : σ.kheap p = some o := by + have ho' : (o, σ) ∈ (NonDet.assertOpt (σ.kheap p) σ).1 := ho + cases hk : σ.kheap p with + | none => rw [hk] at ho'; exact absurd ho' (Set.notMem_empty _) + | some o' => + rw [hk] at ho' + have := congrArg Prod.fst (show (o, σ) = (o', σ) from ho') + exact congrArg some this.symm + have hn : ∃ a, C a = o := by + have h1 := hpi.1 + unfold partialInv at h1 + split_ifs at h1 with hu + · obtain ⟨a, ha, -⟩ := hu + exact ⟨a, ha⟩ + · exact absurd rfl h1 + obtain ⟨a, ea⟩ := hn + rw [e] + exact SStepA.updOther hgo (fun x h => ht a x (ea.trans h)) (fun x n h => hs a x n (ea.trans h)) + +theorem getSimpleKo_sstepA {α : Type} [Nonempty α] (C : α → KernelObject) (p : ObjRef) : + PresR SStepA (getSimpleKo (Z := Unit) C p) := by + refine eq_sstepA ?_ + unfold getSimpleKo + refine PresR.bind (getObject_eqPres p) (fun _ => ?_) + eqpres + +theorem getThreadState_sstepA (t : ObjRef) : PresR SStepA (getThreadState (Z := Unit) t) := by + unfold getThreadState + exact eq_sstepA (threadGet_eqPres _ t) + +theorem setThreadState_sstepA (t : ObjRef) (ts : ThreadState) : + PresR SStepA (setThreadState (Z := Unit) t ts) := by + unfold setThreadState + refine PresR.bind (threadSet_sstepA _ t fun _ => ⟨⟨rfl, rfl⟩, id⟩) (fun _ σ r hr => ?_) + rw [setThreadStateAct_eq] at hr + have e : r = ((), stsAct σ t) := hr + rw [e] + exact SStepA.of_kheap (by unfold stsAct; split_ifs <;> rfl) (stsAct_kheap σ t) + +theorem replyUnlinkTcb_sstepA (t rp : ObjRef) : PresR SStepA (replyUnlinkTcb (Z := Unit) t rp) := by + unfold replyUnlinkTcb + refine PresR.bind (eq_sstepA (getReply_eqPres rp)) (fun _ => ?_) + refine PresR.bind (eq_sstepA (PresR.assert _)) (fun _ => ?_) + refine PresR.bind (getThreadState_sstepA t) (fun _ => ?_) + refine PresR.bind (eq_sstepA (PresR.assert _)) (fun _ => ?_) + refine PresR.bind ?_ (fun _ => setThreadState_sstepA _ _) + unfold setReplyObjRef updateSkObjRef + exact PresR.bind (getSimpleKo_sstepA _ _) (fun _ => + setSimpleKo_sstepA _ _ _ (fun _ _ h => by cases h) (fun _ _ _ h => by cases h)) + +theorem tcbEpDequeue_sstepA (t p : ObjRef) : PresR SStepA (tcbEpDequeue (Z := Unit) t p) := by + unfold tcbEpDequeue + refine PresR.bind (getSimpleKo_sstepA _ _) (fun _ => ?_) + refine PresR.bind (eq_sstepA (PresR.pure _)) (fun _ => ?_) + refine PresR.bind (eq_sstepA (PresR.assert _)) (fun _ => ?_) + refine PresR.bind (eq_sstepA (PresR.pure _)) (fun _ => ?_) + refine PresR.bind (eq_sstepA (PresR.pure _)) (fun _ => ?_) + exact setSimpleKo_sstepA _ _ _ (fun _ _ h => by cases h) (fun _ _ _ h => by cases h) + +theorem tcbNtfnDequeue_sstepA (t p : ObjRef) : PresR SStepA (tcbNtfnDequeue (Z := Unit) t p) := by + unfold tcbNtfnDequeue + refine PresR.bind (getSimpleKo_sstepA _ _) (fun _ => ?_) + refine PresR.bind (eq_sstepA (PresR.pure _)) (fun _ => ?_) + refine PresR.bind (eq_sstepA (PresR.assert _)) (fun _ => ?_) + refine PresR.bind (eq_sstepA (PresR.pure _)) (fun _ => ?_) + refine PresR.bind (eq_sstepA (PresR.pure _)) (fun _ => ?_) + exact setSimpleKo_sstepA _ _ _ (fun _ _ h => by cases h) (fun _ _ _ h => by cases h) + +theorem replyRemoveTcb_sstepA (t rp : ObjRef) : PresR SStepA (replyRemoveTcb (Z := Unit) t rp) := by + unfold replyRemoveTcb + refine PresR.bind (getThreadState_sstepA t) (fun _ => ?_) + refine PresR.bind (eq_sstepA (PresR.assert _)) (fun _ => ?_) + refine PresR.bind (eq_sstepA (PresR.gets _)) (fun so => ?_) + cases so with + | none => exact replyUnlinkTcb_sstepA _ _ + | some p => + dsimp only + refine PresR.bind (eq_sstepA ?_) (fun _ => ?_) + · unfold NonDet.liftM + refine PresR.bind (getSchedContext_eqPres p) (fun _ => ?_) + eqpres + refine PresR.bind (eq_sstepA ?_) (fun _ => ?_) + · unfold getReplyObjRef getSkObjRef getSimpleKo + refine PresR.bind (PresR.bind (getObject_eqPres _) (fun _ => by eqpres)) (fun _ => ?_) + eqpres + refine PresR.bind (eq_sstepA (PresR.assert _)) (fun _ => ?_) + refine PresR.bind (updateSchedContext_sstepA _ _ fun x r hr => ?_) (fun _ => ?_) + · exact (List.takeWhile_sublist _).subset hr + refine PresR.bind ?_ (fun _ => replyUnlinkTcb_sstepA _ _) + unfold setReplyObjRef updateSkObjRef + exact PresR.bind (getSimpleKo_sstepA _ _) (fun _ => + setSimpleKo_sstepA _ _ _ (fun _ _ h => by cases h) (fun _ _ _ h => by cases h)) + +theorem cancelIpc_sstepA (t : ObjRef) : PresR SStepA (cancelIpc (Z := Unit) t) := by + have blk : ∀ (ts : ThreadState) (ro : Option ObjRef), PresR SStepA (blockedCancelIpc (Z := Unit) ts t ro) := by + intro ts ro + unfold blockedCancelIpc getBlockingObject + refine PresR.bind (eq_sstepA (PresR.assertOpt _)) (fun _ => ?_) + refine PresR.bind (tcbEpDequeue_sstepA _ _) (fun _ => ?_) + cases ro with + | none => + show PresR SStepA (setThreadState t .inactive) + exact setThreadState_sstepA _ _ + | some r => + show PresR SStepA (replyUnlinkTcb t r >>= fun _ => setThreadState t .inactive) + exact PresR.bind (replyUnlinkTcb_sstepA _ _) (fun _ => setThreadState_sstepA _ _) + unfold cancelIpc + refine PresR.bind (getThreadState_sstepA t) (fun ts => ?_) + refine PresR.bind (threadSet_sstepA _ t fun _ => ⟨⟨rfl, rfl⟩, fun _ => rfl⟩) (fun _ => ?_) + cases ts <;> dsimp only <;> try exact PresR.pure _ + all_goals first + | exact blk _ _ + | exact replyRemoveTcb_sstepA _ _ + | exact PresR.bind (tcbNtfnDequeue_sstepA _ _) (fun _ => setThreadState_sstepA _ _) + +theorem asUser_memA {α : Type} (t : ObjRef) (f : UserMonad α) {σ : State Unit} {r : α × State Unit} + (hr : r ∈ (asUser (Z := Unit) t f σ).1) : + ∃ x uc, σ.kheap t = some (.tcb x) ∧ r.2 = { σ with kheap := (Function.update σ.kheap t + (some (.tcb { x with tcbArch := archTcbContextSet uc x.tcbArch }))) } := by + unfold asUser at hr + obtain ⟨y1, hy1, hr⟩ := hr + have hy1' : y1 ∈ (NonDet.assertOpt (getTcb (Z := Unit) t σ) σ).1 := hy1 + cases hg : getTcb (Z := Unit) t σ with + | none => rw [hg] at hy1'; exact absurd hy1' (Set.notMem_empty _) + | some x => + rw [hg] at hy1' + have e1 : y1 = (x, σ) := hy1' + subst e1 + obtain ⟨uc, huc, hr⟩ := eqPres_mem (PresR.pure _) hr + obtain ⟨w, hw, hr⟩ := mem_bindA.1 hr + have ew : w.2 = σ := hw.2 + rw [ew] at hr + obtain ⟨nt, hnt, hr⟩ := eqPres_mem (PresR.pure _) hr + have en : nt = { x with tcbArch := archTcbContextSet w.1.2 x.tcbArch } := congrArg Prod.fst hnt + obtain ⟨z, hz, hr⟩ := mem_bindA.1 hr + obtain ⟨ko, -, ez⟩ := setObject_memA hz + have er : r.2 = z.2 := congrArg Prod.snd (show r = (w.1.1, z.2) from hr) + exact ⟨x, w.1.2, getTcb_kheapA hg, by rw [er, ez, en]⟩ + +theorem updateRestartPc_sstepA (t : ObjRef) : PresR SStepA (updateRestartPc (Z := Unit) t) := fun σ r hr => by + unfold updateRestartPc at hr + obtain ⟨x, uc, hx, e⟩ := asUser_memA t _ hr + rw [e] + exact SStepA.updTcb hx ⟨rfl, rfl⟩ id + +theorem updateRestartPc_refKA (t : ObjRef) {σ : State Unit} {r : Unit × State Unit} + (hr : r ∈ (updateRestartPc (Z := Unit) t σ).1) : RefK σ r.2 := by + unfold updateRestartPc at hr + obtain ⟨x, uc, hx, e⟩ := asUser_memA t _ hr + rw [e] + refine ⟨rfl, fun q => ?_, fun g => g.updNs t _ fun _ _ e => by cases e⟩ + show RefObjA (σ.kheap q) (Function.update σ.kheap t _ q) + by_cases e : q = t + · subst e; rw [Function.update_self, hx]; exact .tcb _ _ ⟨rfl, rfl, rfl, rfl, Or.inl rfl⟩ + · rw [Function.update_of_ne e]; exact .same _ + +theorem tcbSchedAction_sstepA (f : ObjRef → List ObjRef → List ObjRef) (t : ObjRef) : + PresR SStepA (tcbSchedAction (Z := Unit) f t) := by + unfold tcbSchedAction + refine PresR.bind (eq_sstepA (threadGet_eqPres _ t)) (fun _ => ?_) + refine PresR.bind (eq_sstepA (threadGet_eqPres _ t)) (fun _ => ?_) + refine PresR.bind (eq_sstepA (by unfold getTcbQueue; eqpres)) (fun _ => ?_) + refine PresR.bind (eq_sstepA (PresR.pure _)) (fun _ σ r hr => ?_) + have e : r = ((), _) := hr + rw [e] + exact SStepA.of_kheap rfl rfl + +theorem tcbReleaseRemove_sstepA (t : ObjRef) : PresR SStepA (tcbReleaseRemove (Z := Unit) t) := by + unfold tcbReleaseRemove + refine PresR.bind (eq_sstepA (PresR.gets _)) (fun _ => ?_) + refine PresR.bind (fun σ r hr => ?_) (fun _ => ?_) + · unfold NonDet.when at hr + split_ifs at hr + · have e : r = ((), { σ with reprogramTimer := true }) := hr + rw [e]; exact SStepA.of_kheap rfl rfl + · have e : r = ((), σ) := hr + rw [e]; exact PreO.refl σ + refine PresR.bind (eq_sstepA (PresR.pure _)) (fun _ σ r hr => ?_) + have e : r = ((), _) := hr + rw [e] + exact SStepA.of_kheap rfl rfl + +theorem cancelYieldTo_sstepA (t : ObjRef) : PresR SStepA (schedContextCancelYieldTo (Z := Unit) t) := by + unfold schedContextCancelYieldTo + refine PresR.bind (eq_sstepA (by unfold getTcbObjRef; exact threadGet_eqPres _ t)) (fun yt => ?_) + cases yt with + | none => exact PresR.pure _ + | some p => + show PresR SStepA (setScObjRef scYieldFromUpdate p none >>= fun _ => setTcbObjRef tcbYieldToUpdate t none) + refine PresR.bind ?_ (fun _ => ?_) + · unfold setScObjRef + exact updateSchedContext_sstepA _ _ fun _ _ h => h + · unfold setTcbObjRef + exact threadSet_sstepA _ t fun _ => ⟨⟨rfl, rfl⟩, id⟩ + +/-- **`suspend` is an `SStepA` step**, at every outcome. -/ +theorem suspend_sstepA (t : ObjRef) : PresR SStepA (Fl4SpecA64.suspend (Z := Unit) t) := by + unfold Fl4SpecA64.suspend + refine PresR.bind (cancelIpc_sstepA t) (fun _ => ?_) + refine PresR.bind (getThreadState_sstepA t) (fun _ => ?_) + refine PresR.bind (fun σ r hr => ?_) (fun _ => ?_) + · unfold NonDet.when at hr + split_ifs at hr + · exact updateRestartPc_sstepA t σ r hr + · have e : r = ((), σ) := hr + rw [e]; exact PreO.refl σ + refine PresR.bind (tcbSchedAction_sstepA _ t) (fun _ => ?_) + refine PresR.bind (tcbReleaseRemove_sstepA t) (fun _ => ?_) + exact PresR.bind (cancelYieldTo_sstepA t) (fun _ => setThreadState_sstepA t _) + +/-! ## What `suspend` keeps of the other threads' queueing -/ + +/-- Every TCB other than `t` keeps its state, and stays queued on the endpoints and notifications it was queued on. -/ +structure BlkOthA (t : ObjRef) (σ σ' : State Unit) : Prop where + tcb : ∀ u x, u ≠ t → σ.kheap u = some (.tcb x) → ∃ y, σ'.kheap u = some (.tcb y) ∧ y.tcbState = x.tcbState + ep : ∀ r y u, u ≠ t → σ.kheap r = some (.endpoint y) → u ∈ y.epQueue → + ∃ y', σ'.kheap r = some (.endpoint y') ∧ u ∈ y'.epQueue + ntfn : ∀ r y q u, u ≠ t → σ.kheap r = some (.notification y) → y.ntfnObj = .waitingNtfn q → u ∈ q → + ∃ y' q', σ'.kheap r = some (.notification y') ∧ y'.ntfnObj = .waitingNtfn q' ∧ u ∈ q' + +theorem BlkOthA.of_kheap {t : ObjRef} {σ σ' : State Unit} (hk : σ'.kheap = σ.kheap) : BlkOthA t σ σ' := + ⟨fun _ x _ h => ⟨x, by rw [hk]; exact h, rfl⟩, fun _ y _ _ h m => ⟨y, by rw [hk]; exact h, m⟩, + fun _ y q _ _ h e m => ⟨y, q, by rw [hk]; exact h, e, m⟩⟩ + +instance (t : ObjRef) : PreO (BlkOthA t) := + ⟨fun _ => BlkOthA.of_kheap rfl, fun {a b c} h1 h2 => by + refine ⟨fun u x ne h => ?_, fun r y u ne h m => ?_, fun r y q u ne h e m => ?_⟩ + · obtain ⟨y, hy, e⟩ := h1.tcb u x ne h + obtain ⟨z, hz, e'⟩ := h2.tcb u y ne hy + exact ⟨z, hz, e'.trans e⟩ + · obtain ⟨y1, hy1, m1⟩ := h1.ep r y u ne h m + exact h2.ep r y1 u ne hy1 m1 + · obtain ⟨y1, q1, hy1, e1, m1⟩ := h1.ntfn r y q u ne h e m + exact h2.ntfn r y1 q1 u ne hy1 e1 m1⟩ + +/-- One object replaced. -/ +theorem BlkOthA.updA {t p : ObjRef} {σ : State Unit} {o0 o : KernelObject} (hp : σ.kheap p = some o0) + (ht : ∀ x, o0 = .tcb x → p = t) + (he : ∀ y, o0 = .endpoint y → ∃ y', o = .endpoint y' ∧ ∀ u, u ≠ t → u ∈ y.epQueue → u ∈ y'.epQueue) + (hn : ∀ y, o0 = .notification y → ∃ y', o = .notification y' ∧ ∀ q u, u ≠ t → y.ntfnObj = .waitingNtfn q → + u ∈ q → ∃ q', y'.ntfnObj = .waitingNtfn q' ∧ u ∈ q') : + BlkOthA t σ { σ with kheap := Function.update σ.kheap p (some o) } := by + refine ⟨fun u x ne h => ?_, fun r y u ne h m => ?_, fun r y q u ne h e m => ?_⟩ + · have c : u ≠ p := fun c => ne (c.trans (ht x (Option.some.inj ((c ▸ hp).symm.trans h)))) + exact ⟨x, (Function.update_of_ne c _ _).trans h, rfl⟩ + · by_cases c : r = p + · subst c + obtain ⟨y', rfl, f⟩ := he y (Option.some.inj (hp.symm.trans h)) + exact ⟨y', Function.update_self _ _ _, f u ne m⟩ + · exact ⟨y, (Function.update_of_ne c _ _).trans h, m⟩ + · by_cases c : r = p + · subst c + obtain ⟨y', rfl, f⟩ := hn y (Option.some.inj (hp.symm.trans h)) + obtain ⟨q', e', m'⟩ := f q u ne e m + exact ⟨y', q', Function.update_self _ _ _, e', m'⟩ + · exact ⟨y, q, (Function.update_of_ne c _ _).trans h, e, m⟩ + +theorem eq_blkOthA {t : ObjRef} {α : Type} {m : NonDet (State Unit) α} (h : PresR (@Eq (State Unit)) m) : + PresR (BlkOthA t) m := fun σ r hr => by rw [← h σ r hr]; exact PreO.refl σ + +theorem threadSet_blkOthA (t : ObjRef) (f : Tcb → Tcb) : PresR (BlkOthA t) (threadSet (Z := Unit) f t) := + fun σ r hr => by + obtain ⟨x, hx, e⟩ := threadSet_memA f t hr + rw [e] + exact BlkOthA.updA hx (fun _ _ => rfl) (fun _ h => by cases h) (fun _ h => by cases h) + +theorem setThreadState_blkOthA (t : ObjRef) (ts : ThreadState) : + PresR (BlkOthA t) (setThreadState (Z := Unit) t ts) := by + unfold setThreadState + refine PresR.bind (threadSet_blkOthA t _) (fun _ σ r hr => ?_) + rw [setThreadStateAct_eq] at hr + have e : r = ((), stsAct σ t) := hr + rw [e] + exact BlkOthA.of_kheap (stsAct_kheap σ t) + +theorem setReply_blkOthA (t p : ObjRef) (z : Reply) : + PresR (BlkOthA t) (setSimpleKo (Z := Unit) KernelObject.reply p z) := fun σ r hr => by + obtain ⟨y, hy, e⟩ := setSimpleKo_memA _ _ _ hr + rw [e] + exact BlkOthA.updA hy (fun _ h => by cases h) (fun _ h => by cases h) (fun _ h => by cases h) + +theorem updateSchedContext_blkOthA (t p : ObjRef) (f : SchedContext → SchedContext) : + PresR (BlkOthA t) (updateSchedContext (Z := Unit) p f) := fun σ r hr => by + obtain ⟨y, n, hy, e⟩ := updateSchedContext_memA p f hr + rw [e] + exact BlkOthA.updA hy (fun _ h => by cases h) (fun _ h => by cases h) (fun _ h => by cases h) + +theorem setReplyObjRef_blkOthA (t rp : ObjRef) (f : (Option ObjRef → Option ObjRef) → Reply → Reply) + (o : Option ObjRef) : PresR (BlkOthA t) (setReplyObjRef (Z := Unit) f rp o) := by + unfold setReplyObjRef updateSkObjRef + refine PresR.bind (eq_blkOthA ?_) (fun _ => setReply_blkOthA _ _ _) + unfold getSimpleKo + refine PresR.bind (getObject_eqPres _) (fun _ => ?_) + eqpres + +theorem replyUnlinkTcb_blkOthA (t rp : ObjRef) : PresR (BlkOthA t) (replyUnlinkTcb (Z := Unit) t rp) := by + unfold replyUnlinkTcb + refine PresR.bind (eq_blkOthA (getReply_eqPres rp)) (fun _ => ?_) + refine PresR.bind (eq_blkOthA (PresR.assert _)) (fun _ => ?_) + refine PresR.bind (eq_blkOthA (by unfold getThreadState; exact threadGet_eqPres _ t)) (fun _ => ?_) + refine PresR.bind (eq_blkOthA (PresR.assert _)) (fun _ => ?_) + exact PresR.bind (setReplyObjRef_blkOthA _ _ _ _) (fun _ => setThreadState_blkOthA _ _) + +theorem replyRemoveTcb_blkOthA (t rp : ObjRef) : PresR (BlkOthA t) (replyRemoveTcb (Z := Unit) t rp) := by + unfold replyRemoveTcb + refine PresR.bind (eq_blkOthA (by unfold getThreadState; exact threadGet_eqPres _ t)) (fun _ => ?_) + refine PresR.bind (eq_blkOthA (PresR.assert _)) (fun _ => ?_) + refine PresR.bind (eq_blkOthA (PresR.gets _)) (fun so => ?_) + cases so with + | none => exact replyUnlinkTcb_blkOthA _ _ + | some p => + dsimp only + refine PresR.bind (eq_blkOthA ?_) (fun _ => ?_) + · unfold NonDet.liftM + refine PresR.bind (getSchedContext_eqPres p) (fun _ => ?_) + eqpres + refine PresR.bind (eq_blkOthA ?_) (fun _ => ?_) + · unfold getReplyObjRef getSkObjRef getSimpleKo + refine PresR.bind (PresR.bind (getObject_eqPres _) (fun _ => by eqpres)) (fun _ => ?_) + eqpres + refine PresR.bind (eq_blkOthA (PresR.assert _)) (fun _ => ?_) + refine PresR.bind (updateSchedContext_blkOthA _ _ _) (fun _ => ?_) + exact PresR.bind (setReplyObjRef_blkOthA _ _ _ _) (fun _ => replyUnlinkTcb_blkOthA _ _) + +theorem cancelIpc_blkOthA {σ : State Unit} {t : ObjRef} {x : Tcb} (hk : σ.kheap t = some (.tcb x)) + (hE : ∀ e', epBlocked x.tcbState = some e' → ∃ z, σ.kheap e' = some (.endpoint z) ∧ t ∈ z.epQueue) + (hN : ∀ n', x.tcbState = .blockedOnNotification n' → ∃ z, σ.kheap n' = some (.notification z)) + {r : Unit × State Unit} (hr : r ∈ (cancelIpc (Z := Unit) t σ).1) : BlkOthA t σ r.2 := by + have hf : BlkOthA t σ (updK σ t (.tcb { x with tcbFault := none })) := + BlkOthA.updA hk (fun _ _ => rfl) (fun _ h => by cases h) (fun _ h => by cases h) + unfold cancelIpc at hr + rw [nd_bind_one (getThreadState_eq hk), nd_bind_one (threadSet_eq _ hk)] at hr + have far : ∀ q, q ≠ t → (updK σ t (.tcb { x with tcbFault := none })).kheap q = σ.kheap q := fun q c => by + rw [updK_kheap, Function.update_of_ne c] + generalize updK σ t (.tcb { x with tcbFault := none }) = σ1 at hr hf far + refine PreO.trans hf ?_ + have ep : ∀ e (ts : ThreadState) (ro : Option ObjRef), epBlocked x.tcbState = some e → + (∀ y, y ∈ (getBlockingObject (Z := Unit) ts σ1).1 → y = (e, σ1)) → + r ∈ (blockedCancelIpc (Z := Unit) ts t ro σ1).1 → BlkOthA t σ1 r.2 := by + intro e ts ro hb hg h + obtain ⟨z, hz, hq⟩ := hE e hb + have het : e ≠ t := fun c => by rw [c, hk] at hz; cases hz + have hz1 : σ1.kheap e = some (.endpoint z) := (far e het).trans hz + unfold blockedCancelIpc at h + obtain ⟨y1, hy1, h⟩ := mem_bindA.1 h + have e1 := hg y1 hy1 + subst e1 + obtain ⟨y2, hy2, h⟩ := mem_bindA.1 h + rw [tcbEpDequeue_eq (Z := Unit) hz1 hq] at hy2 + have e2 : y2 = ((), updK σ1 e (.endpoint (epDeq z t))) := hy2 + subst e2 + have d : BlkOthA t σ1 (updK σ1 e (.endpoint (epDeq z t))) := + BlkOthA.updA hz1 (fun _ h => by cases h) (fun y h => by + cases h + refine ⟨_, rfl, fun u ne m => ?_⟩ + rw [epDeq_queue] + unfold removeAll + exact List.mem_filter.2 ⟨m, decide_eq_true (Ne.symm ne)⟩) (fun _ h => by cases h) + refine PreO.trans d ?_ + cases ro with + | none => exact setThreadState_blkOthA t _ _ _ h + | some rp => exact (PresR.bind (replyUnlinkTcb_blkOthA t rp) fun _ => setThreadState_blkOthA t _) _ _ h + cases hs : x.tcbState with + | blockedOnSend e d => + rw [hs] at hr + dsimp only at hr + exact ep e (.blockedOnSend e d) none (by rw [hs]; rfl) (fun _ h => h) hr + | blockedOnReceive e ro d => + rw [hs] at hr + dsimp only at hr + exact ep e (.blockedOnReceive e ro d) ro (by rw [hs]; rfl) (fun _ h => h) hr + | blockedOnNotification p => + rw [hs] at hr + change r ∈ (cancelSignal (Z := Unit) t p σ1).1 at hr + unfold cancelSignal at hr + obtain ⟨d, hd, hr⟩ := mem_bindA.1 hr + obtain ⟨z, hz⟩ := hN p hs + have pt : p ≠ t := fun c => by rw [c, hk] at hz; cases hz + have hz1 : σ1.kheap p = some (.notification z) := (far p pt).trans hz + refine PreO.trans ?_ (setThreadState_blkOthA t _ _ _ hr) + unfold tcbNtfnDequeue at hd + rw [Notif.nd_bind_one (Notif.getNotification_eq hz1)] at hd + obtain ⟨q, hq, hd⟩ := mem_bindA.1 hd + have eq : q = (ntfnQueueOf z, σ1) := hq + subst eq + obtain ⟨_, -, hd⟩ := eqPres_mem (PresR.assert _) hd + obtain ⟨q', hq', hd⟩ := mem_bindA.1 hd + have eq' : q' = (removeAll t (ntfnQueueOf z), σ1) := hq' + subst eq' + obtain ⟨n', hn', hd⟩ := mem_bindA.1 hd + have en : n' = (ntfnObjUpdate (fun _ => match removeAll t (ntfnQueueOf z) with + | [] => Ntfn.idleNtfn + | _ => Ntfn.waitingNtfn (removeAll t (ntfnQueueOf z))) z, σ1) := hn' + subst en + obtain ⟨y0, -, e⟩ := setSimpleKo_memA _ _ _ hd + rw [e] + refine BlkOthA.updA hz1 (fun _ h => by cases h) (fun _ h => by cases h) (fun y h => ?_) + cases h + refine ⟨_, rfl, fun q u ne hw m => ?_⟩ + have eq : ntfnQueueOf z = q := by unfold ntfnQueueOf; rw [hw]; rfl + have mm : u ∈ removeAll t (ntfnQueueOf z) := by + rw [eq]; unfold removeAll + exact List.mem_filter.2 ⟨m, decide_eq_true (Ne.symm ne)⟩ + refine ⟨removeAll t (ntfnQueueOf z), ?_, mm⟩ + show (match removeAll t (ntfnQueueOf z) with + | [] => Ntfn.idleNtfn + | _ => Ntfn.waitingNtfn (removeAll t (ntfnQueueOf z))) = _ + split + · next c => rw [c] at mm; cases mm + · rfl + | blockedOnReply rp => + rw [hs] at hr + exact replyRemoveTcb_blkOthA t rp _ _ hr + | _ => + rw [hs] at hr + have er : r = ((), σ1) := hr + rw [er]; exact PreO.refl σ1 + +/-- The steps of `suspend` after `cancel_ipc`. -/ +def suspendTailA (t : ObjRef) : SMonad Unit Unit := do + let state ← getThreadState (Z := Unit) t + NonDet.when (state = .running) (updateRestartPc t) + tcbSchedAction Fl4SpecA64.tcbSchedDequeue t + tcbReleaseRemove t + schedContextCancelYieldTo t + setThreadState t .inactive + +theorem suspend_tailEqA (t : ObjRef) : + Fl4SpecA64.suspend (Z := Unit) t = (cancelIpc t >>= fun _ => suspendTailA t) := rfl + +theorem suspendTail_sstepA (t : ObjRef) : PresR SStepA (suspendTailA t) := by + unfold suspendTailA + refine PresR.bind (getThreadState_sstepA t) (fun _ => ?_) + refine PresR.bind (fun σ r hr => ?_) (fun _ => ?_) + · unfold NonDet.when at hr + split_ifs at hr + · exact updateRestartPc_sstepA t σ r hr + · have e : r = ((), σ) := hr + rw [e]; exact PreO.refl σ + refine PresR.bind (tcbSchedAction_sstepA _ t) (fun _ => ?_) + refine PresR.bind (tcbReleaseRemove_sstepA t) (fun _ => ?_) + exact PresR.bind (cancelYieldTo_sstepA t) (fun _ => setThreadState_sstepA t _) + +theorem suspendTail_blkOthA (t : ObjRef) : PresR (BlkOthA t) (suspendTailA t) := by + unfold suspendTailA + refine PresR.bind (eq_blkOthA (by unfold getThreadState; exact threadGet_eqPres _ t)) (fun _ => ?_) + refine PresR.bind (fun σ r hr => ?_) (fun _ => ?_) + · unfold NonDet.when at hr + split_ifs at hr + · unfold updateRestartPc at hr + obtain ⟨x, uc, hx, e⟩ := asUser_memA t _ hr + rw [e] + exact BlkOthA.updA hx (fun _ _ => rfl) (fun _ h => by cases h) (fun _ h => by cases h) + · have e : r = ((), σ) := hr + rw [e]; exact PreO.refl σ + refine PresR.bind ?_ (fun _ => ?_) + · show PresR (BlkOthA t) (tcbSchedAction (Z := Unit) _ t) + unfold tcbSchedAction + refine PresR.bind (eq_blkOthA (threadGet_eqPres _ t)) (fun _ => ?_) + refine PresR.bind (eq_blkOthA (threadGet_eqPres _ t)) (fun _ => ?_) + refine PresR.bind (eq_blkOthA (by unfold getTcbQueue; eqpres)) (fun _ => ?_) + refine PresR.bind (eq_blkOthA (PresR.pure _)) (fun _ σ r hr => ?_) + have e : r = ((), _) := hr + rw [e] + exact BlkOthA.of_kheap rfl + refine PresR.bind ?_ (fun _ => ?_) + · unfold tcbReleaseRemove + refine PresR.bind (eq_blkOthA (PresR.gets _)) (fun _ => ?_) + refine PresR.bind (fun σ r hr => ?_) (fun _ => ?_) + · unfold NonDet.when at hr + split_ifs at hr + · have e : r = ((), { σ with reprogramTimer := true }) := hr + rw [e]; exact BlkOthA.of_kheap rfl + · have e : r = ((), σ) := hr + rw [e]; exact PreO.refl σ + refine PresR.bind (eq_blkOthA (PresR.pure _)) (fun _ σ r hr => ?_) + have e : r = ((), _) := hr + rw [e] + exact BlkOthA.of_kheap rfl + refine PresR.bind ?_ (fun _ => setThreadState_blkOthA t _) + unfold schedContextCancelYieldTo + refine PresR.bind (eq_blkOthA (by unfold getTcbObjRef; exact threadGet_eqPres _ t)) (fun yt => ?_) + cases yt with + | none => exact PresR.pure _ + | some p => + show PresR (BlkOthA t) (setScObjRef scYieldFromUpdate p none >>= fun _ => setTcbObjRef tcbYieldToUpdate t none) + refine PresR.bind ?_ (fun _ => ?_) + · unfold setScObjRef + exact updateSchedContext_blkOthA _ _ _ + · unfold setTcbObjRef + exact threadSet_blkOthA t _ + +theorem suspend_blkOthA {σ : State Unit} {t : ObjRef} {x : Tcb} (hk : σ.kheap t = some (.tcb x)) + (hE : ∀ e', epBlocked x.tcbState = some e' → ∃ z, σ.kheap e' = some (.endpoint z) ∧ t ∈ z.epQueue) + (hN : ∀ n', x.tcbState = .blockedOnNotification n' → ∃ z, σ.kheap n' = some (.notification z)) + {r : Unit × State Unit} (hr : r ∈ (Fl4SpecA64.suspend (Z := Unit) t σ).1) : BlkOthA t σ r.2 := by + rw [suspend_tailEqA] at hr + obtain ⟨a, ha, hr⟩ := mem_bindA.1 hr + exact PreO.trans (cancelIpc_blkOthA hk hE hN ha) (suspendTail_blkOthA t _ _ hr) + +/-- **The reply of a thread blocked on reply is off its SC's reply list after `suspend`** (`p` the SC +`sc_with_reply` finds). -/ +theorem suspend_unlinkedA {σ : State Unit} {t rp p : ObjRef} {x : Tcb} (hk : σ.kheap t = some (.tcb x)) + (hs : x.tcbState = .blockedOnReply rp) {y : SchedContext} {n : Nat} + (hp : σ.kheap p = some (.schedContext y n)) (hsw : scWithReply rp σ = some p) + {r : Unit × State Unit} (hr : r ∈ (Fl4SpecA64.suspend (Z := Unit) t σ).1) : + ∀ y' k, r.2.kheap p = some (.schedContext y' k) → rp ∉ y'.scReplies := by + rw [suspend_tailEqA] at hr + obtain ⟨a, ha, hr⟩ := mem_bindA.1 hr + have K := suspendTail_sstepA t _ _ hr + have pt : p ≠ t := fun c => by rw [c, hk] at hp; cases hp + unfold cancelIpc at ha + rw [nd_bind_one (getThreadState_eq hk), nd_bind_one (threadSet_eq _ hk)] at ha + have hk1 : (updK σ t (.tcb { x with tcbFault := none })).kheap t = some (.tcb { x with tcbFault := none }) := by + rw [updK_kheap, Function.update_self] + have hsw1 : scWithReply rp (updK σ t (.tcb { x with tcbFault := none })) = some p := by + have e : ∀ q (z : SchedContext) k, (updK σ t (.tcb { x with tcbFault := none })).kheap q = + some (.schedContext z k) ↔ σ.kheap q = some (.schedContext z k) := fun q z k => by + by_cases c : q = t + · subst c + rw [hk1, hk] + exact ⟨fun h => (by cases h), fun h => (by cases h)⟩ + · rw [updK_kheap, Function.update_of_ne c] + rw [← hsw] + exact congrArg (fun f => partialInv f true) (funext fun q => decide_eq_decide.2 + (exists_congr fun z => exists_congr fun k => and_congr_left' (e q z k))) + generalize updK σ t (.tcb { x with tcbFault := none }) = σ1 at ha hk1 hsw1 + rw [hs] at ha + change a ∈ (replyRemoveTcb (Z := Unit) t rp σ1).1 at ha + have hs1 : ({ x with tcbFault := none } : Tcb).tcbState = .blockedOnReply rp := hs + unfold replyRemoveTcb at ha + rw [nd_bind_one (getThreadState_eq hk1)] at ha + obtain ⟨_, -, ha⟩ := eqPres_mem (PresR.assert _) ha + obtain ⟨so, hso, ha⟩ := mem_bindA.1 ha + have eso : so = (scWithReply rp σ1, σ1) := hso + subst eso + rw [hsw1] at ha + dsimp only at ha + obtain ⟨l, -, ha⟩ := eqPres_mem (show PresR (@Eq (State Unit)) + (NonDet.liftM SchedContext.scReplies (getSchedContext (Z := Unit) p)) by + unfold NonDet.liftM + refine PresR.bind (getSchedContext_eqPres p) (fun _ => ?_) + eqpres) ha + obtain ⟨rs, -, ha⟩ := eqPres_mem (show PresR (@Eq (State Unit)) + (getReplyObjRef (Z := Unit) Reply.replySc rp) by + unfold getReplyObjRef getSkObjRef getSimpleKo + refine PresR.bind (PresR.bind (getObject_eqPres _) (fun _ => by eqpres)) (fun _ => ?_) + eqpres) ha + obtain ⟨_, -, ha⟩ := eqPres_mem (PresR.assert _) ha + obtain ⟨b, hb, ha⟩ := mem_bindA.1 ha + obtain ⟨z, k, -, eb⟩ := updateSchedContext_memA _ _ hb + have K2 : SStepA b.2 a.2 := by + refine (PresR.bind ?_ fun _ => replyUnlinkTcb_sstepA t rp) _ _ ha + unfold setReplyObjRef updateSkObjRef + exact PresR.bind (getSimpleKo_sstepA _ _) (fun _ => + setSimpleKo_sstepA _ _ _ (fun _ _ h => by cases h) (fun _ _ _ h => by cases h)) + have K3 : SStepA b.2 r.2 := PreO.trans K2 K + have hb2 : b.2.kheap p = some (.schedContext + (scRepliesUpdate (List.takeWhile fun d => decide (d ≠ rp)) z) k) := by + rw [eb]; exact Function.update_self _ _ _ + obtain ⟨w, hw, sub⟩ := K3.sc _ _ _ hb2 + intro y' k' hy' hm + rw [hw] at hy' + cases hy' + have h2 : rp ∈ List.takeWhile (fun d => decide (d ≠ rp)) z.scReplies := sub _ hm + have h3 := List.mem_takeWhile_imp h2 + simp at h3 + +/-! ## The record facts read off the step -/ + +theorem SStepA.utBufAlA {σ σ' : State Unit} (K : SStepA σ σ') {T : Set U64} (h : UtBufAlA T σ) + (htcb : ∀ t ∈ T, ∃ x, σ.kheap (specPtr t) = some (.tcb x)) : UtBufAlA T σ' := fun t ht y hy => by + obtain ⟨x, hx⟩ := htcb t ht + obtain ⟨y', hy', e, -⟩ := K.tcb _ _ hx + rw [hy] at hy'; cases hy' + rw [e.1]; exact h t ht x hx + +theorem SStepA.idleNoFaultA {σ σ' : State Unit} (K : SStepA σ σ') {m : McState} (h : IdleNoFaultA m σ) : + IdleNoFaultA m σ' := fun c hc => by + obtain ⟨x, hx, hf⟩ := h c hc + obtain ⟨y, hy, -, g⟩ := K.tcb _ _ hx + exact ⟨y, hy, g hf⟩ + +/-- The new reply stacks are sub-lists of the old. -/ +theorem SStepA.stackSubA {σ σ' : State Unit} (K : SStepA σ σ') {mem mem' : U64 → U64} {R C : Set U64} + {scRef : U64 → ObjRef} {stack stack' : U64 → List U64} (h : ReplyStackRelA mem R C σ scRef stack) + (h' : ReplyStackRelA mem' R C σ' scRef stack') : ∀ c ∈ C, ∀ r ∈ stack' c, r ∈ stack c := fun c hc r hr => by + obtain ⟨⟨x, n, hx, ex⟩, -⟩ := h.stackRel c hc + obtain ⟨⟨y, k, hy, ey⟩, -⟩ := h'.stackRel c hc + obtain ⟨y', hy', sub⟩ := K.sc _ _ _ hx + rw [hy] at hy'; cases hy' + have := sub (specPtr r) (by rw [ey]; exact List.mem_map_of_mem hr) + rw [ex] at this + obtain ⟨r0, h0, e⟩ := List.mem_map.1 this + rw [← specPtr_inj e]; exact h0 + +/-! ## The three invariants and no pending switch -/ + +/-- The invariants and "no pending switch", from one state to another. -/ +def InvStepA (m : McState) (n : Nat) (σ σ' : State Unit) : Prop := + LoopInvA σ m n → NoSwA σ → LoopInvA σ' m n ∧ NoSwA σ' + +theorem RStepA.invStepA {m : McState} {n : Nat} {σ σ' : State Unit} (S : RStepA m n σ σ') (ns : NoSwA σ) : + LoopInvA σ' m n ∧ NoSwA σ' := ⟨S.inv, S.ns ns⟩ + +theorem RefK.noSwA {σ σ' : State Unit} (R : RefK σ σ') (ns : NoSwA σ) : NoSwA σ' := fun u => by + have e : σ'.schedulerAction = σ.schedulerAction := by rw [R.1] + rw [e]; exact ns u + +/-- Setting a thread that is not idle and in no ready queue `Inactive`. -/ +theorem setInactive_invA {σ : State Unit} {m : McState} {n : Nat} {t : ObjRef} {x : Tcb} + (h : LoopInvA σ m n) (ns : NoSwA σ) (hk : σ.kheap t = some (.tcb x)) (hti : t ≠ σ.idleThread) + (hidles : ∀ c < n, m.idleThreadOf c ≠ t) (hnq : ∀ d p, t ∉ σ.readyQueues d p) (hnr : t ∉ σ.releaseQueue) : + ∃ σ', setThreadState (Z := Unit) t .inactive σ = ({((), σ')}, False) ∧ LoopInvA σ' m n ∧ NoSwA σ' := by + refine ⟨_, setThreadState_eq _ hk, ?_⟩ + have h1 : LoopInvA (stUpdA σ t x .inactive) m n := + { sinv := h.sinv.stateA hk hti (Or.inr hnq) (Or.inr hnr) (fun e => by cases e) (fun _ => Or.inr (Or.inr rfl)) + sidle := h.sidle.stUpdA hk hidles + sact := + { f1 := fun u e => h.sact.f1 u e + f2 := by + by_cases e : σ.curThread = t + · refine ⟨{ x with tcbState := .inactive }, ?_, Or.inr (Or.inr (Or.inl rfl))⟩ + show Function.update σ.kheap t _ σ.curThread = _ + rw [e, Function.update_self] + · obtain ⟨z, hz, hz'⟩ := h.sact.f2 + exact ⟨z, (Function.update_of_ne e _ _).trans hz, hz'⟩ } } + have e0 : updK σ t (.tcb { x with tcbState := .inactive }) = stUpdA σ t x .inactive := rfl + rw [e0] + unfold stsAct + split_ifs + · refine ⟨h1.queueE (P := fun _ => False) ⟨rfl, fun _ _ _ a => Or.inl a⟩ (fun _ f => f.elim) + (fun _ e => by cases e) (fun ha => by cases ha), fun _ e => by cases e⟩ + · exact ⟨h1, ns⟩ + +/-- A thread in a blocked state is neither current nor idle. -/ +theorem LoopInvA.offBlockedA {σ : State Unit} {m : McState} {n : Nat} (h : LoopInvA σ m n) {t : ObjRef} {x : Tcb} + (hk : σ.kheap t = some (.tcb x)) + (hb : x.tcbState ≠ .running ∧ x.tcbState ≠ .restart ∧ x.tcbState ≠ .inactive ∧ + x.tcbState ≠ .idleThreadState) : OffCurA σ m n t := by + refine ⟨fun e => ?_, fun e => ?_, fun c hc e => ?_⟩ + · obtain ⟨z, hz, hz'⟩ := h.sact.f2 + rw [← e, hk] at hz + cases hz + rcases hz' with a | a | a | a + · exact hb.1 a + · exact hb.2.1 a + · exact hb.2.2.1 a + · exact hb.2.2.2 a + · obtain ⟨z, hz, hz'⟩ := h.sinv.idleTcb + rw [← e, hk] at hz + cases hz + exact hb.2.2.2 hz' + · obtain ⟨z, hz, hz'⟩ := h.sidle.f3 c hc + rw [e, hk] at hz + cases hz + exact hb.2.2.2 hz' + +/-- **`reply_remove_tcb`** keeps the invariants. -/ +theorem replyRemoveTcb_invA {σ : State Unit} {m : McState} {n : Nat} {t rp : ObjRef} {x : Tcb} + (h : LoopInvA σ m n) (hk : σ.kheap t = some (.tcb x)) (hst : x.tcbState = .blockedOnReply rp) + {r : Unit × State Unit} (hr : r ∈ (replyRemoveTcb (Z := Unit) t rp σ).1) (ns : NoSwA σ) : + LoopInvA r.2 m n ∧ NoSwA r.2 := by + have off : OffCurA σ m n t := h.offBlockedA hk (by rw [hst]; refine ⟨?_, ?_, ?_, ?_⟩ <;> (intro e; cases e)) + unfold replyRemoveTcb at hr + obtain ⟨ts, -, hr⟩ := eqPres_mem (by unfold getThreadState; exact threadGet_eqPres _ t) hr + obtain ⟨_, -, hr⟩ := eqPres_mem (PresR.assert _) hr + obtain ⟨so, hso, hr⟩ := eqPres_mem (PresR.gets _) hr + have eso : so = scWithReply rp σ := congrArg Prod.fst (show (so, σ) = (scWithReply rp σ, σ) from hso) + have fin : ∀ τ : State Unit, RStepA m n σ τ → ∀ w ∈ (replyUnlinkTcb (Z := Unit) t rp τ).1, + LoopInvA w.2 m n ∧ NoSwA w.2 := by + intro τ S w hw + obtain ⟨y, hy, es⟩ := S.st _ _ hk + have oy : OffCurA τ m n t := ⟨by rw [S.cur]; exact off.cur, by rw [S.idle]; exact off.idle, off.idles⟩ + obtain ⟨I, -, -, -, -, nsw⟩ := replyUnlinkTcb_stepA S.inv hy (by rw [es, hst]; rfl) oy hw + exact ⟨I, nsw (S.ns ns)⟩ + cases so with + | none => exact fin σ (RStepA.refl h) r hr + | some p => + dsimp only at hr + have hp : p ≠ idleScPtr := by + intro ep + have hw := eso.symm + unfold scWithReply partialInv at hw + split_ifs at hw with hun + have hsp := (Classical.choose_spec hun).1 + have eThe := Option.some.inj hw + unfold The at eThe + rw [dif_pos hun] at eThe + have hsat : ∃ sc k, σ.kheap p = some (.schedContext sc k) ∧ rp ∈ sc.scReplies := by + rw [← eThe] + simpa only [decide_eq_true_eq] using hsp + obtain ⟨sc, k, hsc, hm⟩ := hsat + rw [ep] at hsc + rw [h.sidle.f4 sc k hsc] at hm + cases hm + have hg : PresR (@Eq (State Unit)) + (NonDet.liftM SchedContext.scReplies (getSchedContext (Z := Unit) p)) := by + unfold NonDet.liftM + refine PresR.bind (getSchedContext_eqPres p) (fun _ => ?_) + eqpres + obtain ⟨l, -, hr⟩ := eqPres_mem hg hr + have hg2 : PresR (@Eq (State Unit)) (getReplyObjRef (Z := Unit) Reply.replySc rp) := by + unfold getReplyObjRef getSkObjRef getSimpleKo + refine PresR.bind (PresR.bind (getObject_eqPres _) (fun _ => by eqpres)) (fun _ => ?_) + eqpres + obtain ⟨rs, -, hr⟩ := eqPres_mem hg2 hr + obtain ⟨_, -, hr⟩ := eqPres_mem (PresR.assert _) hr + obtain ⟨a, ha, hr⟩ := mem_bindA.1 hr + obtain ⟨y, k, hy, ea⟩ := updateSchedContext_memA _ _ ha + have A : RStepA m n σ a.2 := by + rw [ea] + exact RStepA.updSc h hy hp rfl rfl rfl (Or.inl rfl) + obtain ⟨b, hb, hr⟩ := mem_bindA.1 hr + have B := setReplyObjRef_stepA _ _ _ A.inv hb + exact fin b.2 (A.trans B) r hr + +/-- **`cancel_ipc`** keeps the invariants and starts no switch, in every thread state. -/ +theorem cancelIpc_invA {σ : State Unit} {m : McState} {n : Nat} {t : ObjRef} {x : Tcb} + (h : LoopInvA σ m n) (ns : NoSwA σ) (hk : σ.kheap t = some (.tcb x)) + (hE : ∀ e', epBlocked x.tcbState = some e' → ∃ z, σ.kheap e' = some (.endpoint z) ∧ t ∈ z.epQueue) + (hN : ∀ n', x.tcbState = .blockedOnNotification n' → ∃ z, σ.kheap n' = some (.notification z)) + (hrep : RepOKA σ t) + {r : Unit × State Unit} (hr : r ∈ (cancelIpc (Z := Unit) t σ).1) : LoopInvA r.2 m n ∧ NoSwA r.2 := by + let σ1 := updK σ t (.tcb { x with tcbFault := none }) + have hk1 : σ1.kheap t = some (.tcb { x with tcbFault := none }) := by + simp only [σ1, updK_kheap, Function.update_self] + have R1 : RefK σ σ1 := by + refine ⟨rfl, fun q => ?_, fun g => g.updNs t _ fun _ _ e => by cases e⟩ + show RefObjA (σ.kheap q) (Function.update σ.kheap t (some (.tcb { x with tcbFault := none })) q) + by_cases e : q = t + · subst e; rw [Function.update_self, hk]; exact .tcb _ _ ⟨rfl, rfl, rfl, rfl, Or.inr rfl⟩ + · rw [Function.update_of_ne e]; exact .same _ + have h1 := h.refK R1 + have ns1 : NoSwA σ1 := ns + have ep_arm : ∀ e, epBlocked x.tcbState = some e → + (x.tcbState ≠ .running ∧ x.tcbState ≠ .restart ∧ x.tcbState ≠ .inactive ∧ x.tcbState ≠ .idleThreadState ∧ + runnable x.tcbState = false) → + ∀ ro : Option ObjRef, ((∃ e d, x.tcbState = .blockedOnSend e d ∧ ro = none) ∨ + (∃ e d, x.tcbState = .blockedOnReceive e ro d)) → ro = none → LoopInvA r.2 m n ∧ NoSwA r.2 := by + intro e hb hs ro hcase hro + obtain ⟨z, hkz, hq⟩ := hE e hb + have het : e ≠ t := by intro c; rw [c, hk] at hkz; cases hkz + have hkz1 : σ1.kheap e = some (.endpoint z) := by + simp only [σ1, updK_kheap, Function.update_of_ne het]; exact hkz + have hdq := tcbEpDequeue_eq (Z := Unit) hkz1 hq + let σ2 := updK σ1 e (.endpoint (epDeq z t)) + have hk2 : σ2.kheap t = some (.tcb { x with tcbFault := none }) := by + simp only [σ2, updK_kheap, Function.update_of_ne het.symm]; exact hk1 + have h2 : LoopInvA σ2 m n := h1.updEp hkz1 + have o2 : OffCurA σ2 m n t := h2.offBlockedA hk2 ⟨hs.1, hs.2.1, hs.2.2.1, hs.2.2.2.1⟩ + have eq := cancelIpc_blocked hk ro hcase (by rw [hro]; exact bci_none hb hdq hk2) + rw [eq] at hr + have er : r = ((), _) := hr + rw [er] + refine ⟨h2.setBlk hk2 hs.2.2.2.2 o2 (Or.inr rfl) (fun e => by cases e), ?_⟩ + show NoSwA (stsAct _ t) + unfold stsAct + rw [if_neg (fun c => o2.cur c.1)] + exact ns + cases hs : x.tcbState with + | blockedOnSend e d => + exact ep_arm e (by rw [hs]; rfl) (by rw [hs]; refine ⟨?_, ?_, ?_, ?_, rfl⟩ <;> (intro c; cases c)) + none (Or.inl ⟨e, d, hs, rfl⟩) rfl + | blockedOnReceive e ro d => + cases ro with + | none => + exact ep_arm e (by rw [hs]; rfl) (by rw [hs]; refine ⟨?_, ?_, ?_, ?_, rfl⟩ <;> (intro c; cases c)) + none (Or.inr ⟨e, d, hs⟩) rfl + | some rp => + obtain ⟨z, hkz, hq⟩ := hE e (by rw [hs]; rfl) + obtain ⟨xr, hkr, hback⟩ := hrep x hk e rp d hs + obtain ⟨σ', hsp, -, -, hinv, hns⟩ := cancelRecv_specA hk hkz hq hkr hback hs + rw [hsp] at hr + have er : r = ((), σ') := hr + rw [er] + have o1 : OffCurA σ1 m n t := h1.offBlockedA hk1 + (by show x.tcbState ≠ _ ∧ x.tcbState ≠ _ ∧ x.tcbState ≠ _ ∧ x.tcbState ≠ _ + rw [hs]; refine ⟨?_, ?_, ?_, ?_⟩ <;> (intro c; cases c)) + exact ⟨hinv m n h1 o1, hns ns⟩ + | blockedOnNotification p => + unfold cancelIpc at hr + rw [nd_bind_one (getThreadState_eq hk), nd_bind_one (threadSet_eq _ hk), + show updK σ t (.tcb { x with tcbFault := none }) = σ1 from rfl, hs] at hr + change r ∈ (cancelSignal (Z := Unit) t p σ1).1 at hr + unfold cancelSignal at hr + obtain ⟨d, hd, hr⟩ := mem_bindA.1 hr + have o1 : OffCurA σ1 m n t := h1.offBlockedA hk1 + (by show x.tcbState ≠ _ ∧ x.tcbState ≠ _ ∧ x.tcbState ≠ _ ∧ x.tcbState ≠ _ + rw [hs]; refine ⟨?_, ?_, ?_, ?_⟩ <;> (intro c; cases c)) + obtain ⟨y, hy0⟩ := hN p hs + have pt : p ≠ t := fun c => by rw [c, hk] at hy0; cases hy0 + have hy : σ1.kheap p = some (.notification y) := by + simp only [σ1, updK_kheap, Function.update_of_ne pt]; exact hy0 + obtain ⟨y', e1⟩ := tcbNtfnDequeue_memA hy hd + have R2 : RefK σ1 d.2 := by + rw [e1] + refine ⟨rfl, fun q => ?_, fun g => g.updNs p _ fun _ _ e => by cases e⟩ + show RefObjA (σ1.kheap q) (Function.update σ1.kheap p (some (.notification y')) q) + by_cases c : q = p + · subst c; rw [Function.update_self, hy]; exact .ntfn _ _ + · rw [Function.update_of_ne c]; exact .same _ + have h2 := h1.refK R2 + have hk2 : d.2.kheap t = some (.tcb { x with tcbFault := none }) := by + rw [e1]; exact (Function.update_of_ne pt.symm _ _).trans hk1 + have o2 : OffCurA d.2 m n t := + ⟨by rw [R2.1]; exact o1.cur, by rw [R2.1]; exact o1.idle, o1.idles⟩ + rw [setThreadState_eq _ hk2] at hr + have er : r = ((), stsAct (updK d.2 t (.tcb { x with tcbFault := none, tcbState := .inactive })) t) := hr + rw [er] + refine ⟨h2.setBlk hk2 (by show runnable x.tcbState = false; rw [hs]; rfl) o2 (Or.inr rfl) (fun e => by cases e), ?_⟩ + show NoSwA (stsAct _ t) + unfold stsAct + rw [if_neg (fun c => o2.cur c.1)] + exact R2.noSwA ns1 + | blockedOnReply rp => + unfold cancelIpc at hr + rw [nd_bind_one (getThreadState_eq hk), nd_bind_one (threadSet_eq _ hk), + show updK σ t (.tcb { x with tcbFault := none }) = σ1 from rfl, hs] at hr + exact replyRemoveTcb_invA h1 hk1 hs hr ns1 + | _ => + unfold cancelIpc at hr + rw [nd_bind_one (getThreadState_eq hk), nd_bind_one (threadSet_eq _ hk), + show updK σ t (.tcb { x with tcbFault := none }) = σ1 from rfl, hs] at hr + have er : r = ((), σ1) := hr + rw [er]; exact ⟨h1, ns1⟩ + +/-- **`SInvXA ∧ SIdleA ∧ SActA` and no pending switch across `suspend`**, at every outcome. -/ +theorem suspend_invA {σ : State Unit} {m : McState} {n : Nat} {t : ObjRef} {x : Tcb} + (h : LoopInvA σ m n) (ns : NoSwA σ) (hk : σ.kheap t = some (.tcb x)) + (hE : ∀ e', epBlocked x.tcbState = some e' → ∃ z, σ.kheap e' = some (.endpoint z) ∧ t ∈ z.epQueue) + (hN : ∀ n', x.tcbState = .blockedOnNotification n' → ∃ z, σ.kheap n' = some (.notification z)) + (hrep : RepOKA σ t) (hti : t ≠ σ.idleThread) (hidles : ∀ c < n, m.idleThreadOf c ≠ t) + {r : Unit × State Unit} (hr : r ∈ (Fl4SpecA64.suspend (Z := Unit) t σ).1) + (hnq : ∀ d p, t ∉ r.2.readyQueues d p) : LoopInvA r.2 m n ∧ NoSwA r.2 := by + unfold Fl4SpecA64.suspend at hr + obtain ⟨a, ha, hr⟩ := mem_bindA.1 hr + have I1 := cancelIpc_invA h ns hk hE hN hrep ha + have K1 := cancelIpc_sstepA t σ a ha + obtain ⟨st, -, hr⟩ := eqPres_mem (by unfold getThreadState; exact threadGet_eqPres _ t) hr + obtain ⟨b, hb, hr⟩ := mem_bindA.1 hr + have W : (LoopInvA b.2 m n ∧ NoSwA b.2) ∧ SStepA a.2 b.2 := by + unfold NonDet.when at hb + split_ifs at hb + · have R := updateRestartPc_refKA t hb + exact ⟨⟨I1.1.refK R, R.noSwA I1.2⟩, updateRestartPc_sstepA t _ _ hb⟩ + · have e : b = ((), a.2) := hb + rw [e]; exact ⟨I1, PreO.refl _⟩ + obtain ⟨c, hc, hr⟩ := mem_bindA.1 hr + have I3 := (tcbSchedDeq_stepA t W.1.1 hc).invStepA W.1.2 + have K3 := tcbSchedAction_sstepA _ t _ _ hc + obtain ⟨d, hd, hr⟩ := mem_bindA.1 hr + have I4 := (tcbReleaseRemove_stepA t I3.1 hd).invStepA I3.2 + have K4 := tcbReleaseRemove_sstepA t _ _ hd + obtain ⟨e, he, hr⟩ := mem_bindA.1 hr + have R5 := cancelYieldTo_refKA t he + have K5 := cancelYieldTo_sstepA t _ _ he + have I5 := I4.1.refK R5 + have ns5 := R5.noSwA I4.2 + have K : SStepA σ e.2 := PreO.trans K1 (PreO.trans W.2 (PreO.trans K3 (PreO.trans K4 K5))) + obtain ⟨y, hy, -⟩ := K.tcb _ _ hk + have eq := setThreadState_eq (Z := Unit) .inactive hy + have hq : r.2.readyQueues = e.2.readyQueues := by + rw [eq] at hr + have er : r = ((), _) := hr + rw [er] + show (stsAct _ t).readyQueues = _ + unfold stsAct + split_ifs <;> rfl + obtain ⟨σ', e', I6, ns6⟩ := setInactive_invA I5 ns5 hy (by rw [K.idle]; exact hti) hidles + (fun d p => by rw [← hq]; exact hnq d p) (by + obtain ⟨bb, hbb⟩ := tcbReleaseRemove_memA t hd + have e5 : e.2.releaseQueue = d.2.releaseQueue := by rw [R5.1] + rw [e5, hbb] + show t ∉ tcbSchedDequeue t _ + unfold tcbSchedDequeue + simp) + rw [e'] at hr + have er : r = ((), σ') := hr + rw [er] + exact ⟨I6, ns6⟩ + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelSwapMdbA.lean b/proofs/Fl4ProofsA64/P3/DelSwapMdbA.lean new file mode 100644 index 000000000..33c43c207 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelSwapMdbA.lean @@ -0,0 +1,305 @@ +/- +P3 (AArch64, delete hooks E4; Architect Option H): **the MDB words across the loop's `cdt::swap`**, exported for the +twin hook hypotheses (`P3/DelStepMdbA`'s `SwapMdbA`): + +* **`cdt_swap_flagsA`**: bits 48/49 (`MDB_REVOCABLE` / `MDB_FIRST_BADGED`) move with the transposition; +* **`cdt_swap_mdbWA`**: `SwapMdbA`, from it and A64 P1's `cdt.swap.lrN` (the links). + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.DelStepMdbA +import Fl4ProofsA64.P1.MdbNull +import Fl4ProofsA64.P1.MdbSwap +import Fl4ProofsA64.P1.MdbTreeU +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P0.Consts Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-- The two MDB flag bits. -/ +abbrev FLGA : U64 := RVB ||| Fl4ProofsA64.P1.W7.R.ST.FB + +theorem u64_and_assocA (a b c : U64) : a &&& b &&& c = a &&& (b &&& c) := by + rw [UScalar.eq_equiv_bv_eq] + simp only [UScalar.bv_and, BitVec.and_assoc] + +theorem flg_ptrA : FLGA &&& obj.MDB_PTR_MASK = 0#u64 := by unfold obj.MDB_PTR_MASK; rfl + +/-- A link write keeps the flag bits. -/ +theorem flg_linkA (w n : U64) : + ((w &&& ~~~ obj.MDB_PTR_MASK) ||| (n &&& obj.MDB_PTR_MASK)) &&& FLGA = w &&& FLGA := + Fl4ProofsA64.P1.W7.R.ST.ptr_bits _ _ _ _ flg_ptrA + +theorem bit_choiceA (v B : U64) (k : Nat) (hB : B.bv = BitVec.twoPow 64 k) (hk : k < 64) : + (if ((v &&& B) != 0#u64) = true then B else 0#u64) = v &&& B := by + have h : (v &&& B).bv = if v.bv.getLsbD k then B.bv else 0#64 := by + have e1 : (v &&& B).bv = v.bv &&& BitVec.twoPow 64 k := + (UScalar.bv_and v B).trans (congrArg (fun x : BitVec 64 => v.bv &&& x) hB) + have e2 : (if v.bv.getLsbD k then B.bv else 0#64) = (if v.bv.getLsbD k then BitVec.twoPow 64 k else 0#64) := + congrArg (fun x : BitVec 64 => if v.bv.getLsbD k then x else 0#64) hB + exact e1.trans ((BitVec.and_twoPow v.bv k).trans e2.symm) + have hne : B.bv ≠ 0#64 := by + intro e + have := congrArg (fun x : BitVec 64 => x.getLsbD k) (hB.symm.trans e) + simp only [BitVec.getLsbD_twoPow, BitVec.getLsbD_zero] at this + simp [hk] at this + cases hb : v.bv.getLsbD k + · rw [hb] at h + have e0 : v &&& B = 0#u64 := by rw [UScalar.eq_equiv_bv_eq]; exact h + rw [e0]; simp + · rw [hb] at h + have eB : v &&& B = B := by rw [UScalar.eq_equiv_bv_eq]; exact h + have nz : B ≠ 0#u64 := fun e => hne (by rw [e]; rfl) + rw [eB, if_pos (by simpa using nz)] + +theorem flg_writeA (w n v : U64) : + ((((w &&& ~~~ (obj.MDB_PTR_MASK ||| RVB ||| Fl4ProofsA64.P1.W7.R.ST.FB)) ||| (n &&& obj.MDB_PTR_MASK)) ||| + (v &&& RVB)) ||| (v &&& Fl4ProofsA64.P1.W7.R.ST.FB)) &&& FLGA = v &&& FLGA := by + have d1 : RVB &&& obj.MDB_PTR_MASK = 0#u64 := rvbc + have d2 : Fl4ProofsA64.P1.W7.R.ST.FB &&& obj.MDB_PTR_MASK = 0#u64 := fbc1 + rw [UScalar.eq_equiv_bv_eq] at d1 d2 ⊢ + apply BitVec.eq_of_getLsbD_eq; intro i hi + have e1 := congrArg (fun x : BitVec 64 => x.getLsbD i) d1 + have e2 := congrArg (fun x : BitVec 64 => x.getLsbD i) d2 + have z : (0#u64 : U64).bv.getLsbD i = false := by + have : (0#u64 : U64).bv = 0#64 := rfl + rw [this]; simp + simp only [UScalar.bv_and, BitVec.getLsbD_and, z] at e1 e2 + have hi' : i < UScalarTy.U64.numBits := hi + simp only [FLGA, UScalar.bv_and, UScalar.bv_or, UScalar.bv_not, BitVec.getLsbD_and, BitVec.getLsbD_or, + BitVec.getLsbD_not] + revert e1 e2 + cases (RVB : U64).bv.getLsbD i <;> cases (Fl4ProofsA64.P1.W7.R.ST.FB : U64).bv.getLsbD i <;> + cases (obj.MDB_PTR_MASK : U64).bv.getLsbD i <;> simp [hi'] + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {T Ω Sg OS : Set U64} + +/-- `set_next_flags` with the flags read from a `NEXT` word `v`: the written word's flags are `v`'s. -/ +theorem set_nf_flgA (s : S) (x n v : U64) : + obj.Slot.set_next_flags st x s n ((v &&& RVB) != 0#u64) ((v &&& Fl4ProofsA64.P1.W7.R.ST.FB) != 0#u64) + ⦃ s' => ∃ w, s' = L.wr s (wa x 24#u64) w ∧ w &&& FLGA = v &&& FLGA ⦄ := by + have hr := bit_choiceA v RVB 48 (by decide) (by decide) + have hf := bit_choiceA v Fl4ProofsA64.P1.W7.R.ST.FB 49 (by decide) (by decide) + have h : obj.Slot.set_next_flags st x s n ((v &&& RVB) != 0#u64) ((v &&& Fl4ProofsA64.P1.W7.R.ST.FB) != 0#u64) + = ok (L.wr s (wa x 24#u64) ((((L.mem s (wa x 24#u64) &&& + ~~~ (obj.MDB_PTR_MASK ||| RVB ||| Fl4ProofsA64.P1.W7.R.ST.FB)) ||| (n &&& obj.MDB_PTR_MASK)) ||| + (v &&& RVB)) ||| (v &&& Fl4ProofsA64.P1.W7.R.ST.FB))) := by + have er : (if ((v &&& RVB) != 0#u64) = true then ok RVB else ok 0#u64 : Result U64) = ok (v &&& RVB) := by + rw [← apply_ite ok, hr] + have ef : (if ((v &&& Fl4ProofsA64.P1.W7.R.ST.FB) != 0#u64) = true then ok Fl4ProofsA64.P1.W7.R.ST.FB else ok 0#u64 : + Result U64) = ok (v &&& Fl4ProofsA64.P1.W7.R.ST.FB) := by + rw [← apply_ite ok, hf] + unfold obj.Slot.set_next_flags obj.Slot.next_word + simp only [lift, bind_ok, cte_next, L.read_eq, rvb_val, fb_val] + rw [er, ef] + simp only [bind_ok, L.write_eq] + rw [h, WP.spec_ok] + exact ⟨_, rfl, flg_writeA _ _ _⟩ + +theorem w24_linkA (hO : OSOK T Ω Sg OS) (s : S) {p y : U64} (hp : p ∈ OS) (hy : y ∈ OS) (n : U64) : + L.mem (L.wr s (wa p 24#u64) ((L.mem s (wa p 24#u64) &&& ~~~ obj.MDB_PTR_MASK) ||| (n &&& obj.MDB_PTR_MASK))) + (wa y 24#u64) &&& FLGA = L.mem s (wa y 24#u64) &&& FLGA := by + by_cases e : y = p + · subst e + rw [w24_self hO _ hy, flg_linkA] + · rw [w24_wr24 hO _ hp hy e] + +set_option maxHeartbeats 4000000 in +/-- **`swap` and bits 48 / 49** (cdt.rs:67–94; the proof follows A64 P1's `cdt.swap.rvb`). -/ +theorem cdt_swap_flagsA (hO : OSOK T Ω Sg OS) {s : S} {pv nx : U64 → U64} (hL : LR st OS s pv nx) {x1 x2 : U64} + {c1 c2 : capability.Cap} (hx1 : x1 ∈ OS) (hx2 : x2 ∈ OS) (hne : x1 ≠ x2) (hs1 : obj.sext48 x1 = ok x1) + (hs2 : obj.sext48 x2 = ok x2) (hw1 : CapRt.EncWf c1) (hw2 : CapRt.EncWf c2) + (hp1 : pv x1 ≠ 0#u64 → pv x1 ∈ OS) (hn1 : nx x1 ≠ 0#u64 → nx x1 ∈ OS) + (hp2 : pv x2 ≠ 0#u64 → pv x2 ∈ OS) (hn2 : nx x2 ≠ 0#u64 → nx x2 ∈ OS) : + cdt.swap st s c1 x1 c2 x2 ⦃ s' => ∀ y ∈ OS, + L.mem s' (wa (swp x1 x2 y) 24#u64) &&& FLGA = L.mem s (wa y 24#u64) &&& FLGA ⦄ := by + have hS : P0.StoreTotal st := L.total + obtain ⟨a1, b1, he1, -⟩ := CapRt.decode_encode c1 hw1 + obtain ⟨a2, b2, he2, -⟩ := CapRt.decode_encode c2 hw2 + unfold cdt.swap + rw [set_cap_eq he2, bind_ok, set_cap_eq he1, bind_ok] + have L2 : LR st OS (L.wr (L.wr (L.wr (L.wr s x1 a2) (wa x1 8#u64) b2) x2 a1) (wa x2 8#u64) b1) pv nx := + (hL.wr2 hO hx1).wr2 hO hx2 + have K2 : ∀ y ∈ OS, L.mem (L.wr (L.wr (L.wr (L.wr s x1 a2) (wa x1 8#u64) b2) x2 a1) (wa x2 8#u64) b1) + (wa y 24#u64) = L.mem s (wa y 24#u64) := fun y hy => + ((set_cap_links hO hx2 y hy).2).trans ((set_cap_links hO hx1 y hy).2) + generalize L.wr (L.wr (L.wr (L.wr s x1 a2) (wa x1 8#u64) b2) x2 a1) (wa x2 8#u64) b1 = s2 at L2 K2 ⊢ + rw [slot_prev_eq s2 x1, bind_ok, (L2 x1 hx1).1, next_eq, bind_ok, (L2 x1 hx1).2, revocable_eqA, bind_ok, + slot_fb_eqA, bind_ok] + have efb : fbA st s2 x1 = ((L.mem s (wa x1 24#u64) &&& Fl4ProofsA64.P1.W7.R.ST.FB) != 0#u64) := by + show ((L.mem s2 (wa x1 24#u64) &&& Fl4ProofsA64.P1.W7.R.ST.FB) != 0#u64) = _ + rw [K2 x1 hx1] + have erv : ((L.mem s2 (wa x1 24#u64) &&& RVB) != 0#u64) = ((L.mem s (wa x1 24#u64) &&& RVB) != 0#u64) := by + rw [K2 x1 hx1] + rw [efb, erv] + simp only [obj.Slot.is_null, bind_ok] + -- stage 3: `P1.NEXT := x2` + refine WP.spec_bind (Pₘ := fun s3 => LR st OS s3 pv (LkAbs.s3 0#u64 pv nx x1 x2) ∧ + ∀ y ∈ OS, L.mem s3 (wa y 24#u64) &&& FLGA = L.mem s (wa y 24#u64) &&& FLGA) ?_ fun s3 h3 => ?_ + · split + · rename_i hp0 + have hP0 : pv x1 = 0#u64 := by simpa using hp0 + refine (WP.spec_ok _).2 ⟨L2.congr (fun _ _ => rfl) fun z _ => ?_, fun y hy => by rw [K2 y hy]⟩ + unfold LkAbs.s3; simp [hP0] + · rename_i hp0 + have hP0 : pv x1 ≠ 0#u64 := by simpa using hp0 + rw [set_next_eq, WP.spec_ok] + refine ⟨(L2.nw hO (hp1 hP0) (link_bits _ _) hs2).congr (fun _ _ => rfl) fun z _ => ?_, + fun y hy => by rw [w24_linkA hO s2 (hp1 hP0) hy x2, K2 y hy]⟩ + unfold LkAbs.s3; by_cases e : z = pv x1 <;> simp [e, hP0] + obtain ⟨L3, B3⟩ := h3 + -- stage 4: `N1.PREV := x2` + refine WP.spec_bind (Pₘ := fun s4 => LR st OS s4 (LkAbs.s4 0#u64 pv nx x1 x2) (LkAbs.s3 0#u64 pv nx x1 x2) ∧ + ∀ y ∈ OS, L.mem s4 (wa y 24#u64) &&& FLGA = L.mem s (wa y 24#u64) &&& FLGA) ?_ fun s4 h4 => ?_ + · split + · rename_i hn0 + have hN0 : nx x1 = 0#u64 := by simpa using hn0 + refine (WP.spec_ok _).2 ⟨L3.congr (fun z _ => ?_) (fun _ _ => rfl), B3⟩ + unfold LkAbs.s4; simp [hN0] + · rename_i hn0 + have hN0 : nx x1 ≠ 0#u64 := by simpa using hn0 + rw [set_prev_eq, WP.spec_ok] + refine ⟨(L3.prev hO (hn1 hN0) x2).congr (fun z _ => ?_) (fun _ _ => rfl), + fun y hy => by rw [w24_wr16 hO s3 (hn1 hN0) hy]; exact B3 y hy⟩ + unfold LkAbs.s4; by_cases e : z = nx x1 <;> simp [e, hN0] + obtain ⟨L4, B4⟩ := h4 + have hP2 : LkAbs.s4 0#u64 pv nx x1 x2 x2 ≠ 0#u64 → LkAbs.s4 0#u64 pv nx x1 x2 x2 ∈ OS := by + unfold LkAbs.s4; split + · exact fun _ => hx2 + · exact hp2 + have hN2 : LkAbs.s3 0#u64 pv nx x1 x2 x2 ≠ 0#u64 → LkAbs.s3 0#u64 pv nx x1 x2 x2 ∈ OS := by + unfold LkAbs.s3; split + · exact fun _ => hx2 + · exact hn2 + rw [slot_prev_eq s4 x2, bind_ok, (L4 x2 hx2).1, next_eq, bind_ok, (L4 x2 hx2).2, revocable_eqA, bind_ok, + slot_fb_eqA, bind_ok] + have ev : ∀ B : U64, FLGA &&& B = B → + L.mem s4 (wa x2 24#u64) &&& B = L.mem s (wa x2 24#u64) &&& B := fun B hB => by + rw [← hB, ← u64_and_assocA, ← u64_and_assocA, B4 x2 hx2] + have erv2 : ((L.mem s4 (wa x2 24#u64) &&& RVB) != 0#u64) = ((L.mem s (wa x2 24#u64) &&& RVB) != 0#u64) := by + rw [ev RVB (by rfl)] + have efb2 : fbA st s4 x2 = ((L.mem s (wa x2 24#u64) &&& Fl4ProofsA64.P1.W7.R.ST.FB) != 0#u64) := by + show ((L.mem s4 (wa x2 24#u64) &&& Fl4ProofsA64.P1.W7.R.ST.FB) != 0#u64) = _ + rw [ev Fl4ProofsA64.P1.W7.R.ST.FB (by rfl)] + rw [erv2, efb2] + -- stages 5–8: slot 1 and slot 2 get each other's flags + rw [set_prev_eq s4 x1 _, bind_ok] + refine WP.spec_bind (set_nf_flgA _ x1 _ _) ?_ + rintro s6 ⟨w6, rfl, hw6⟩ + rw [set_prev_eq _ x2 _, bind_ok] + refine WP.spec_bind (set_nf_flgA _ x2 _ _) ?_ + rintro s8 ⟨w8, rfl, hw8⟩ + have B8 : ∀ y ∈ OS, L.mem (L.wr (L.wr (L.wr (L.wr s4 (wa x1 16#u64) + (LkAbs.s4 0#u64 pv nx x1 x2 x2)) (wa x1 24#u64) w6) (wa x2 16#u64) (pv x1)) (wa x2 24#u64) w8) + (wa (swp x1 x2 y) 24#u64) &&& FLGA = L.mem s (wa y 24#u64) &&& FLGA := fun y hy => by + by_cases e1 : y = x1 + · rw [show swp x1 x2 y = x2 by simp [swp, e1], w24_self hO _ hx2, hw8, e1] + by_cases e2 : y = x2 + · rw [show swp x1 x2 y = x1 by simp [swp, e2, hne.symm], w24_wr24 hO _ hx2 hx1 hne, + w24_wr16 hO _ hx2 hx1, w24_self hO _ hx1, hw6, e2] + rw [show swp x1 x2 y = y by simp [swp, e1, e2], w24_wr24 hO _ hx2 hy e2, w24_wr16 hO _ hx2 hy, + w24_wr24 hO _ hx1 hy e1, w24_wr16 hO _ hx1 hy] + exact B4 y hy + generalize L.wr (L.wr (L.wr (L.wr s4 (wa x1 16#u64) (LkAbs.s4 0#u64 pv nx x1 x2 x2)) (wa x1 24#u64) w6) + (wa x2 16#u64) (pv x1)) (wa x2 24#u64) w8 = s8 at B8 ⊢ + try simp only [obj.Slot.is_null, bind_ok] + have sw : ∀ y ∈ OS, swp x1 x2 y ∈ OS := fun y hy => swp_os hx1 hx2 hy + -- stage 9: `P2.NEXT := x1` + refine WP.spec_bind (Pₘ := fun s9 => ∀ y ∈ OS, L.mem s9 (wa y 24#u64) &&& FLGA = L.mem s8 (wa y 24#u64) &&& FLGA) + ?_ fun s9 h9 => ?_ + · split + · exact (WP.spec_ok _).2 fun _ _ => rfl + · rename_i hp0 + have hP0 : LkAbs.s4 0#u64 pv nx x1 x2 x2 ≠ 0#u64 := by simpa using hp0 + rw [set_next_eq, WP.spec_ok] + exact fun y hy => w24_linkA hO s8 (hP2 hP0) hy x1 + have fin : ∀ s10 : S, (∀ y ∈ OS, L.mem s10 (wa y 24#u64) = L.mem s9 (wa y 24#u64)) → + ∀ y ∈ OS, L.mem s10 (wa (swp x1 x2 y) 24#u64) &&& FLGA = L.mem s (wa y 24#u64) &&& FLGA := + fun s10 h10 y hy => by rw [h10 _ (sw y hy), h9 _ (sw y hy)]; exact B8 y hy + -- stage 10: `N2.PREV := x1` + split + · exact (WP.spec_ok _).2 (fin s9 fun _ _ => rfl) + · rename_i hn0 + have hN0 : LkAbs.s3 0#u64 pv nx x1 x2 x2 ≠ 0#u64 := by simpa using hn0 + rw [set_prev_eq, WP.spec_ok] + exact fin _ fun y hy => w24_wr16 hO s9 (hN2 hN0) hy x1 + +/-- **`SwapMdbA` along `cdt::swap`** (module docstring; the hypotheses of `cdt.swap.lrN`). -/ +theorem cdt_swap_mdbWA (hO : OSOK T Ω Sg OS) (h0 : ∀ y ∈ OS, y ≠ 0#u64) {G : List (List U64)} {s : S} + (hM : MdbLk st OS G s) (hN : NullLk st OS s) {x1 x2 : U64} {c1 c2 : capability.Cap} (hx1 : x1 ∈ OS) + (hx2 : x2 ∈ G.flatten) (hne : x1 ≠ x2) (hs1 : obj.sext48 x1 = ok x1) (hs2 : obj.sext48 x2 = ok x2) + (hc1 : capA st s x1 = c1) (hc2 : capA st s x2 = c2) (hw1 : CapRt.EncWf c1) (hw2 : CapRt.EncWf c2) : + cdt.swap st s c1 x1 c2 x2 ⦃ s' => SwapMdbA st G x1 x2 s s' ⦄ := by + have inOS : ∀ y ∈ G.flatten, y ∈ OS := fun y hy => ((hM.members y).1 hy).1 + have F := hM.lkF h0 + have o2 := inOS x2 hx2 + have nm : ∀ y ∈ OS, y ∉ G.flatten → capA st s y = .Null := fun y hy hn => by + by_contra h; exact hn ((hM.members y).2 ⟨hy, h⟩) + have N1 : x1 ∈ G.flatten ∨ (L.mem s (wa x1 16#u64) = 0#u64 ∧ NxA st s x1 = 0#u64) := by + by_cases m : x1 ∈ G.flatten + · exact Or.inl m + · exact Or.inr (hN x1 hx1 (nm x1 hx1 m)) + have hp1 : L.mem s (wa x1 16#u64) ≠ 0#u64 → L.mem s (wa x1 16#u64) ∈ OS := fun h => by + rcases N1 with m | ⟨e, -⟩ + · exact inOS _ (F.pvm _ m h) + · exact absurd e h + have hn1 : NxA st s x1 ≠ 0#u64 → NxA st s x1 ∈ OS := fun h => by + rcases N1 with m | ⟨-, e⟩ + · exact inOS _ (F.nxm _ m h) + · exact absurd e h + have r1 : x1 ∉ G.flatten → ∀ y, y ∈ G.flatten → NxA st s y ≠ x1 ∧ L.mem s (wa y 16#u64) ≠ x1 := + fun n1 y hy => ⟨fun e => n1 (by rw [← e]; exact F.nxm y hy (by rw [e]; exact h0 x1 hx1)), + fun e => n1 (by rw [← e]; exact F.pvm y hy (by rw [e]; exact h0 x1 hx1))⟩ + have key : ∀ y ∈ G.flatten, + LkAbs.sN 0#u64 (fun z => L.mem s (wa z 16#u64)) (NxA st s) x1 x2 (LkAbs.tr x1 x2 y) = + LkAbs.tr x1 x2 (NxA st s y) := by + intro y hy + by_cases m1 : x1 ∈ G.flatten + · exact (LkAbs.sw_ok F m1 hx2 hne hy).1 + · obtain ⟨p1, q1⟩ := hN x1 hx1 (nm x1 hx1 m1) + obtain ⟨a, -, -, -⟩ := LkAbs.sw_null F m1 (h0 x1 hx1) p1 q1 (r1 m1) hx2 hy + exact a + have e : ∀ a, Equiv.swap x1 x2 a = swp x1 x2 a := fun a => by rw [Equiv.swap_apply_def]; rfl + refine WP.spec_mono (WP.spec_and (cdt.swap.lrN hO h0 hM hN hx1 hx2 hne hs1 hs2 hc1 hc2) + (cdt_swap_flagsA hO (LR.self s) hx1 o2 hne hs1 hs2 hw1 hw2 hp1 hn1 + (fun h => inOS _ (F.pvm _ hx2 h)) (fun h => inOS _ (F.nxm _ hx2 h)))) ?_ + rintro s' ⟨⟨hLR, -⟩, hf⟩ a ha + have haO := inOS a ha + have aO : swp x1 x2 a ∈ OS := swp_os hx1 o2 haO + have fl := hf a haO + have ex : ∀ B : U64, FLGA &&& B = B → + L.mem s' (wa (swp x1 x2 a) 24#u64) &&& B = L.mem s (wa a 24#u64) &&& B := fun B hB => by + rw [← hB, ← u64_and_assocA, ← u64_and_assocA, fl] + rw [e, e] + refine ⟨?_, ?_, ?_⟩ + · rw [(hLR _ aO).2, swp_tr, swp_tr]; exact key a ha + · show ((L.mem s' (wa (swp x1 x2 a) 24#u64) &&& RVB) != 0#u64) = ((L.mem s (wa a 24#u64) &&& RVB) != 0#u64) + rw [ex RVB (by rfl)] + · show ((L.mem s' (wa (swp x1 x2 a) 24#u64) &&& Fl4ProofsA64.P1.W7.R.ST.FB) != 0#u64) = + ((L.mem s (wa a 24#u64) &&& Fl4ProofsA64.P1.W7.R.ST.FB) != 0#u64) + rw [ex Fl4ProofsA64.P1.W7.R.ST.FB (by rfl)] + +/-- **`SwapMdbA` along `cdt::cap_swap_for_delete`** (`x2` a member, `x1` any other `OS` slot). -/ +theorem cdt_cap_swap_for_delete_mdbWA (hO : OSOK T Ω Sg OS) (h0 : ∀ y ∈ OS, y ≠ 0#u64) {G : List (List U64)} + {s : S} (hM : MdbLk st OS G s) (hN : NullLk st OS s) {x1 x2 : U64} (hx1 : x1 ∈ OS) (hx2 : x2 ∈ G.flatten) + (hne : x1 ≠ x2) (hs1 : obj.sext48 x1 = ok x1) (hs2 : obj.sext48 x2 = ok x2) : + cdt.cap_swap_for_delete st s x1 x2 ⦃ s' => SwapMdbA st G x1 x2 s s' ⦄ := by + unfold cdt.cap_swap_for_delete + simp only [obj.Slot.Insts.CoreCmpPartialEqSlot.eq, bind_ok] + split + · rename_i heq + exact absurd (by simpa using heq) hne + rw [capA_eq, bind_ok, capA_eq, bind_ok] + exact cdt_swap_mdbWA hO h0 hM hN hx1 hx2 hne hs1 hs2 rfl rfl (CapRt.decodeF_encWf _ _) + (CapRt.decodeF_encWf _ _) + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelSysKeepA.lean b/proofs/Fl4ProofsA64/P3/DelSysKeepA.lean new file mode 100644 index 000000000..8248003fa --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelSysKeepA.lean @@ -0,0 +1,110 @@ +/- +P3 (AArch64, delete hooks W1): **`SysL` across a `DFB` step without the symmetry record.** A64 P1's +`SysL.dfTX` (MdbLiveX) proves `SysL ∧ SymL` and takes `SymL` only for the second half; the deletion loop's record +carries no `SymL`, so the `SysL` half is restated here (proof copied, same hypotheses minus `SymL`). +**`SysL.ptW_noSymA`** is MdbVsMapA's `PtW.sysK` for it. No named premise; new module. +-/ +import Fl4ProofsA64.P1.MdbVsMapA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P1 +open fl4_core_a64 Fl4ProofsA64.P0.Consts Fl4Proofs.P3 + +section +variable {S : Type _} {st : machine.Store S} [L : LawfulStore st] {H : Heap} +variable {T Cs Ω Sg Rp Scs Nt : Set U64} {Z : U64 → Nat} {Ac : Set U64} + +local notation "wa" => core.num.U64.wrapping_add + +namespace W7 +namespace R +namespace ST + +variable {OS : Set U64} {ia ie : Nat} + +set_option maxHeartbeats 1000000 in +theorem SysL.dfTX_noSymA {Xq : Set U64} {Ac : Set U64} {Ko : Set Nat} {Bw : U64 → Prop} {s s' : S} (hH : HeapOK H) + (hO : OSOK H.tcbs H.omega H.frame OS) + (hB : BlkOKX st H.tcbs Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac Xq s) + (hrun : Runnable (L.mem s (stA (L.curW s))) ∨ LvF st H.tcbs Cs H.replies H.scs H.ntfns LvX.none s s') + (hKo : ∀ o : U64, o.val ∈ Ko → o.val < 224 → ∀ y ∈ OS, + ¬ (y.val ≤ (wa (L.ksb s) o).val ∧ (wa (L.ksb s) o).val < y.val + 32)) + (hBw : ∀ b, Bw b → (b ∈ H.omega ∧ b ∉ H.frame) ∨ ∃ y ∈ H.tcbs, b = wa y 3512#u64) + (hBo : ∀ x ∈ OS, ∀ b, x.val ≤ b.val → b.val < x.val + 32 → ¬ Bw b) + (B : SysL st H Cs OS ia ie s) (d : DFB st H Cs (L.curW s) Ko Bw s s') + (hI : IpcAl st H s') : + SysL st H Cs OS ia ie s' := by + have h := hB + obtain ⟨Q, R, q⟩ := h.1 + have hSg := h.2.2.2.2 + have hX := h.2.2.1 + -- every `OS` word kept + have k : OSKeep st OS s s' := OSKeep.of_in hO d.w fun x hx b hb h1 h2 hF => by + rcases hF with (hk | ⟨o, ho, hl, -, e⟩) | hw + · exact kw7_not_os (Scs := ∅) (Z := H.scBits) (Es := ∅) q hSg.sub + (fun _ _ c hc => absurd hc (Set.notMem_empty c)) (fun e he => absurd he (Set.notMem_empty e)) hx hb h1 h2 + (K8.KW.to7 hk) + · exact hKo o ho hl x hx (by rw [← e]; exact ⟨h1, h2⟩) + · exact hBo x hx b h1 h2 hw + -- `SysZ` + obtain ⟨⟨⟨hS, hR, hV, hZ, hC⟩, hz, hK⟩, hfr, hcz, hur⟩ := B.sys + have u := UBm.keepO k + have hoff := HW.off (Vc := ∅) (Ub := ∅) hH hV hK q + (fun b hb => by obtain ⟨v, hv, -⟩ := hb; exact absurd hv (Set.notMem_empty v)) + (fun b hb => absurd hb (Set.notMem_empty b)) + have sys' : SysZ st H Cs OS s' := ⟨⟨⟨MdbST.keepO hO hS k, UtRev.keep hR k, ValidUntypedA.keepO hV k, + UtZeroA.writes hZ d.w (fun y0 hy0 p b i hc w hw => hoff w (hw.elim DfW.hw fun hw => + (hBw w hw).elim (fun h => hw_omega h.1) fun ⟨y, hy, e⟩ => Or.inl ⟨y, hy, 3512#u64, by simp, e⟩) + y0 hy0 p b i hc) u, CoverA.keepO hC k d.w.1⟩, + ZombiesFinal.recap hz fun _ hy => Or.inl (k.capA hy), KsOffA.of_ub hK d.w.1 u⟩, + FrameOK.keepO hfr k, CoverZ.keepO hcz k d.w.1, UtRV.keepO hur k⟩ + -- the liveness words + have he : ∀ e, (H e = some .ep ∨ H e = some .ntfn) → L.mem s' (epTSA e) = L.mem s (epTSA e) := + ep_keptX h d.w fun b _ hb => by + rcases hb with ((⟨y, hy, K, -, hK, -, -, rfl⟩ | ⟨c, hc, o, ho, -, rfl⟩ | hb | ⟨c, hc, -⟩ | ⟨e, he, -⟩) | + ⟨o, -, hl, h8, rfl⟩) | hw + · exact Or.inr (Or.inl ⟨y, hy, K, hK, rfl⟩) + · exact Or.inr (Or.inr (Or.inl ⟨c, hc, o, ho, s, rfl, rfl⟩)) + · exact Or.inl (hSg.sub b hb) + · exact absurd hc (Set.notMem_empty c) + · exact absurd he (Set.notMem_empty e) + · exact Or.inl (hSg.ksΩ o (by omega) h8) + · exact (hBw b hw).elim (fun h => Or.inl h.1) fun ⟨y, hy, e⟩ => + Or.inr (Or.inl ⟨y, hy, 3512#u64, by simp, e⟩) + have g : LiveAg st H Cs s s' := + hrun.elim (fun hr => LiveAg.ofLvC d.lv he (runnable_ne_inactive hr)) fun l => LiveAg.ofLv l he + have bk : BorK st H.tcbs s s' := hrun.elim (fun hr => BorK.of_eq _ (runnable_ne_reply hr) fun t ht htc => by + rw [stA_eq] + exact d.lv.t t ht 3328#u64 (by unfold LvO; simp) fun hx => htc (show t = L.curW s ∧ _ from hx).1) + fun l => BorK.of_lv l fun _ _ h => h.elim + have hz' : DbgTz st H.tcbs Cs s := ⟨hX.2.2.tm.dn, hX.2.2.tm.dp, hX.2.2.tm.dh⟩ + refine ⟨sys', B.ln.ag g (NamesKept.keepO k), B.vr.ag g hX.2.1 bk, B.ly.of_ub g.ksb g.idle (g.idleSc q.idleT) u, + B.cn.keepO k, + fun x hx hb => by rw [k.capA hx]; exact B.bh x hx hb, (B.ci.keepO k).idleMv g.idle (g.idleSc q.idleT), + B.sc.keepO k, CtOff.ks g.ksb (CapInv.keepO B.ct k), FrOff.ks g.ksb (CapInv.keepO B.fo k), + fun x hx hb => by rw [k.capA hx]; exact B.ep x hx hb, hI⟩ + +/-- **`SysL` and every `OS` word across a `PtW Pt` write step, without `SymL`.** -/ +theorem SysL.ptW_noSymA {Pt : Set U64} {Vc' Ub' : Set U64} {s s' : S} {Ac0 : Set U64} (hH : HeapOK H) + (hO : OSOK H.tcbs H.omega H.frame OS) (hvr : VrOK H.omega H.frame H.scs H.scBits OS Vc' Ub' (L.ksb s) Pt) + (B : SysL st H Cs OS ia ie s) + (hB : BlkOK st H.tcbs Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac0 s) + (w : WritesIn st s s' (PtW Pt)) : SysL st H Cs OS ia ie s' ∧ OSKeep st OS s s' := by + obtain ⟨Q, Rq, q⟩ := hB.1 + have hBw : ∀ b, PtW Pt b → b ∈ H.omega ∧ b ∉ H.frame := fun b hb => + ⟨hvr.om b (Or.inl hb), hvr.sg b (Or.inl hb)⟩ + have C := LvC.of_blk hB + have d : DFB st H Cs (L.curW s) ∅ (PtW Pt) s s' := DFB.ofBw C w hBw + have hS := SysL.dfTX_noSymA hH hO hB.bxi.blk (Or.inr (LvF.ofCwN C w fun b _ hb => Or.inl (Or.inr (hBw b hb)))) + (fun _ h => absurd h (Set.notMem_empty _)) (fun b h => Or.inl (hBw b h)) + (fun x hx b h1 h2 hv => hvr.os b (Or.inl hv) ⟨x, hx, h1, h2⟩) B d + (B.ipc.keep (IbK.of_w q.tcbs w fun y hy hb => ib_out q hy (q.omega _ (hBw _ hb).1) rfl)) + exact ⟨hS, OSKeep.of_in hO w fun x hx b _ h1 h2 hF => hvr.os b (Or.inl hF) ⟨x, hx, h1, h2⟩⟩ + +end ST +end R +end W7 + +end + +end Fl4ProofsA64.P1 diff --git a/proofs/Fl4ProofsA64/P3/DelTabKeepA.lean b/proofs/Fl4ProofsA64/P3/DelTabKeepA.lean new file mode 100644 index 000000000..cd431d82b --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelTabKeepA.lean @@ -0,0 +1,274 @@ +/- +P3 (AArch64, delete hooks W2): **the deletion loop's record across an ASID-table-word clear** (`delete_asid_pool`'s +write), the table-word sibling of `P3/DelPoolKeepA`'s `delVs_zeroKeepHwA`. Generated by `handoff/tools/gen_deltab.py` +from that proof, with: + +* `SysL.vsW_noSymA`: `SysL.ptW_noSymA` for any write set of `VsR` words (`SysL.ptW_noSymA`'s proof, generalised); +* `PoolOff.ofTabZ`, `PoolNamed.ofTabZ`, `UbPoolT.ofTabZ`: A64 P1's `ofTab` / `UbPoolT.keep` for a step that keeps or zeroes + each ASID-table word (a zero entry names no pool); +* **`delTab_zeroKeepHwA`**: after FL4's write of `0` into ASID-table word `i`, a spec step with `JAtZAX`, `VsSpecA` and + `ClrViewA`, the loop's `DStateA`, `JStage3TA` (the J7 slot's R2 part `Rj` free: the caller proves it after the write), + `ParentAdjA` and `ZombieTcbNoVcpuA` hold again. `TabKeep` does NOT hold here. + +Named premise: `BufOffTabA` is not needed (`vsW_asid_bufA`); lemma hypotheses: the +windows' words are off the ASID table (`hYAs`). New module; no existing statement changed. +-/ +import Fl4ProofsA64.P3.DelPoolKeepA +import Fl4ProofsA64.P3.DelAsidPoolA +import Fl4ProofsA64.P1.S1aUbRt +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P1 +open fl4_core_a64 Fl4ProofsA64.P0.Consts Fl4Proofs.P3 + +section +variable {S : Type _} {st : machine.Store S} [L : LawfulStore st] {H : Heap} +variable {T Cs Ω Sg Rp Scs Nt : Set U64} {Z : U64 → Nat} {Ac : Set U64} + +local notation "wa" => core.num.U64.wrapping_add + +namespace W7 +namespace R +namespace ST + +variable {OS : Set U64} {ia ie : Nat} + +/-- **`SysL` and every `OS` word across a step writing only `VsR` words.** -/ +theorem SysL.vsW_noSymA {P : U64 → Prop} {Pt : Set U64} {Vc' Ub' : Set U64} {s s' : S} {Ac0 : Set U64} + (hP : ∀ b, P b → VsR (L.ksb s) Pt b) (hH : HeapOK H) + (hO : OSOK H.tcbs H.omega H.frame OS) (hvr : VrOK H.omega H.frame H.scs H.scBits OS Vc' Ub' (L.ksb s) Pt) + (B : SysL st H Cs OS ia ie s) + (hB : BlkOK st H.tcbs Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac0 s) + (w : WritesIn st s s' P) : SysL st H Cs OS ia ie s' ∧ OSKeep st OS s s' := by + obtain ⟨Q, Rq, q⟩ := hB.1 + have hBw : ∀ b, P b → b ∈ H.omega ∧ b ∉ H.frame := fun b hb => + ⟨hvr.om b (hP b hb), hvr.sg b (hP b hb)⟩ + have C := LvC.of_blk hB + have d : DFB st H Cs (L.curW s) ∅ P s s' := DFB.ofBw C w hBw + have hS := SysL.dfTX_noSymA hH hO hB.bxi.blk (Or.inr (LvF.ofCwN C w fun b _ hb => Or.inl (Or.inr (hBw b hb)))) + (fun _ h => absurd h (Set.notMem_empty _)) (fun b h => Or.inl (hBw b h)) + (fun x hx b h1 h2 hv => hvr.os b (hP b hv) ⟨x, hx, h1, h2⟩) B d + (B.ipc.keep (IbK.of_w q.tcbs w fun y hy hb => ib_out q hy (q.omega _ (hBw _ hb).1) rfl)) + exact ⟨hS, OSKeep.of_in hO w fun x hx b _ h1 h2 hF => hvr.os b (hP b hF) ⟨x, hx, h1, h2⟩⟩ + +variable {s s' : S} + +theorem PoolOff.ofTabZ {Cs : Set U64} (hk : L.ksb s' = L.ksb s) + (ht : ∀ j, L.mem s' (wa (L.ksb s') (AtOff j)) = L.mem s (wa (L.ksb s) (AtOff j)) ∨ + L.mem s' (wa (L.ksb s') (AtOff j)) = 0#u64) (h : PoolOff st H Cs OS s) : PoolOff st H Cs OS s' := fun j => by + have h0 := h j + rw [vspace.asid_table.eq] at h0 ⊢ + rcases ht j with e | e + · rw [e] + refine (WP.spec_ok _).2 fun hne asid => ?_ + have w := (WP.spec_ok _).1 h0 hne asid + exact ⟨w.al, w.om, w.sg, w.os, RegionOffNodes.ksb hk w.ks⟩ + · rw [e] + exact (WP.spec_ok _).2 fun hne => absurd rfl hne + +theorem PoolNamed.ofTabZ (ht : ∀ j, L.mem s' (wa (L.ksb s') (AtOff j)) = L.mem s (wa (L.ksb s) (AtOff j)) ∨ + L.mem s' (wa (L.ksb s') (AtOff j)) = 0#u64) (k : OSKeep st OS s s') (h : PoolNamed st OS s) : + PoolNamed st OS s' := fun j => by + have h0 := h j + rw [vspace.asid_table.eq] at h0 ⊢ + rcases ht j with e | e + · rw [e] + refine (WP.spec_ok _).2 fun hne => ?_ + obtain ⟨y, hy, b, hcy⟩ := (WP.spec_ok _).1 h0 hne + exact ⟨y, hy, b, by rw [k.capA hy]; exact hcy⟩ + · rw [e] + exact (WP.spec_ok _).2 fun hne => absurd rfl hne + +theorem UbPoolT.ofTabZ {Ub : Set U64} + (ht : ∀ j, L.mem s' (wa (L.ksb s') (AtOff j)) = L.mem s (wa (L.ksb s) (AtOff j)) ∨ + L.mem s' (wa (L.ksb s') (AtOff j)) = 0#u64) (h : UbPoolT st Ub s) : UbPoolT st Ub s' := fun j => by + rcases ht j with e | e + · rw [e]; exact h j + · rw [e]; exact fun hne => absurd rfl hne + +end ST +end R +end W7 + +end + +end Fl4ProofsA64.P1 + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + + + +/-- **J7 across an ASID-table-word clear**, the R2 part `Rj` mapped to `Rj'` by the caller. -/ +theorem J7LA.ofTabZA {Z : XSetsA} {H : Heap} {OS : Set U64} {Rj Rj' : Set U64 → JSlotA S} {s s' : S} + {σ σ' : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} {hw' : HwRootA} + (h : J7LA st Rj Z H OS s σ m) (kb : L.ksb s' = L.ksb s) + (ht : ∀ j, L.mem s' (wa (L.ksb s') (AtOff j)) = L.mem s (wa (L.ksb s) (AtOff j)) ∨ + L.mem s' (wa (L.ksb s') (AtOff j)) = 0#u64) + (ht0 : ∀ j, L.mem s' (wa (L.ksb s) (AtOff j)) = L.mem s (wa (L.ksb s) (AtOff j)) ∨ + L.mem s' (wa (L.ksb s) (AtOff j)) = 0#u64) + (osk : OSKeep st OS s s') + (hdr : ∀ y ∈ OS, ∀ q mp ad, capA st s y = .HighVSpace q mp ad → + L.mem s' (wa q 4088#u64) = L.mem s (wa q 4088#u64)) + (R' : ∀ Pt Ub : Set U64, VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt → + (∀ q, SchedPageA st Z OS s q → q ∈ Pt) → Rj Pt Z H OS s σ m → Rj' Pt { Z with hw := hw' } H OS s' σ' m) : + J7LA st Rj' { Z with hw := hw' } H OS s' σ' m := by + obtain ⟨Pt, Ub, vr, pn, arm, capc, vc, df, hicap, link, vcks, fu, ual, vcu, R⟩ := h + have capE : ∀ u ∈ OS, capA st s' u = capA st s u := fun u hu => osk.capA hu + refine ⟨Pt, Ub, by rw [kb]; exact vr, PoolNamed.ofTabZ ht osk pn, ?_, ⟨UbFr.ofOSK osk capc.1, FrameOK.ofOSK osk capc.2⟩, + CapInv.keepO vc osk, CapInv.keepO df osk, HiCap511.keep hicap capE hdr, fun q hq => link q ?_, + by rw [kb]; exact vcks, FrUb.keepO fu osk, ual, vcu, R' Pt Ub vr link R⟩ + · exact ⟨⟨arm.case.slot, arm.case.ts, PoolOff.ofTabZ kb ht arm.case.po, + PoolObj.of_tab kb ht0 arm.case.pobj⟩, + KsBridge.ofKsb kb arm.kb, arm.nt, UbPoolT.ofTabZ ht arm.pool, arm.vh⟩ + · unfold SchedPageA at hq ⊢ + refine hq.imp_right (·.imp_right (·.imp_right (·.imp_right (·.imp_right (·.imp_right (·.imp_right ?_)))))) + rintro ⟨u, hu, h⟩ + refine ⟨u, hu, ?_⟩ + rw [capE u hu] at h + exact h + +set_option maxHeartbeats 1000000 in +/-- **The loop record across an ASID-table-word clear** (module docstring). -/ +theorem delTab_zeroKeepHwA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {Rj Rj0 : Set U64 → JSlotA S} {hw' : HwRootA} {s : S} {a a' : A} + {σ σ' : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7LA st Rj) s a σ m) + (J0 : J7LA st Rj0 Z H OS s σ m) + (D : DStateA st Z.th OS s σ Z.G) (hPA : ParentAdjA st Z.G.chains Z.G.par s) (hZT : ZombieTcbNoVcpuA st OS s) + {i : U64} (hi : i.val < 128) + (hYAs : ∀ y ∈ Z.Y, ∀ j : U64, j.val < 512 → ¬ AsW (L.ksb s) (peAF y j)) + (jx : JAtZAX (L := L) AR { Z with hw := hw' } (L.wr s (atAF (L.ksb s) i) 0#u64) a' σ' m) + (HS : VsSpecA σ σ') (HC : ClrViewA σ σ') + (R' : ∀ Pt Ub : Set U64, VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt → + (∀ q, SchedPageA st Z OS s q → q ∈ Pt) → Rj Pt Z H OS s σ m → + Rj Pt { Z with hw := hw' } H OS (L.wr s (atAF (L.ksb s) i) 0#u64) σ' m) : + DStateA st Z.th OS (L.wr s (atAF (L.ksb s) i) 0#u64) σ' Z.G ∧ + JStage3TA st AR { Z with hw := hw' } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7LA st Rj) + (L.wr s (atAF (L.ksb s) i) 0#u64) a' σ' m ∧ + ParentAdjA st Z.G.chains Z.G.par (L.wr s (atAF (L.ksb s) i) 0#u64) ∧ + ZombieTcbNoVcpuA st OS (L.wr s (atAF (L.ksb s) i) 0#u64) ∧ OSKeep st OS s (L.wr s (atAF (L.ksb s) i) 0#u64) ∧ + (∀ Rj' : Set U64 → JSlotA S, (∀ Pt Ub : Set U64, VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt → + (∀ q, SchedPageA st Z OS s q → q ∈ Pt) → Rj0 Pt Z H OS s σ m → + Rj' Pt { Z with hw := hw' } H OS (L.wr s (atAF (L.ksb s) i) 0#u64) σ' m) → + J7LA st Rj' { Z with hw := hw' } H OS (L.wr s (atAF (L.ksb s) i) 0#u64) σ' m) := by + have hbf : ¬ BufW Bf (atAF (L.ksb s) i) := + vsW_asid_bufA J.j.1.place J.ipcPlace J.sr.ksFit hi + obtain ⟨Pt, Ub, vr, pn, arm, capc, vc, df, hicap, link, vcks, fu, ual, vcu, R⟩ := J.j7 + have vsr : VsR (L.ksb s) Pt (atAF (L.ksb s) i) := vsW_asidA hi + have hal : Aligned (atAF (L.ksb s) i) := vr.al _ vsr + have kb : L.ksb (L.wr s (atAF (L.ksb s) i) 0#u64) = L.ksb s := L.ksb_wr _ _ _ + have w1 : WritesIn st s (L.wr s (atAF (L.ksb s) i) 0#u64) (fun x => x = atAF (L.ksb s) i) := writesIn_wr hal + have hP : ∀ Pt' Ub' : Set U64, VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub' (L.ksb s) Pt' → + (∀ q, SchedPageA st Z OS s q → q ∈ Pt') → ∀ b, b = atAF (L.ksb s) i → VsR (L.ksb s) Pt' b := + fun Pt' _ _ link' b hb => hb ▸ vsW_asidA hi + have om : atAF (L.ksb s) i ∈ H.omega ∧ atAF (L.ksb s) i ∉ H.frame := ⟨vr.om _ vsr, vr.sg _ vsr⟩ + -- A64 P1's invariants + have osk := OSKeep.of_in J.ent.os w1 fun x hx b _ h1 h2 hF => vr.os b (hF ▸ vsr) ⟨x, hx, h1, h2⟩ + have hB := (blk_capw_wr (v := 0#u64) (BXI.blk J.ent.bxi) hal (Or.inr om)).bxi + have vsZ : VsZ st (L.ksb s) (utVsPtA st Pp Tb Lv s) s (L.wr s (atAF (L.ksb s) i) 0#u64) := + VsZ.wr0 (fun b hb => J.vsOk.al b hb) hal + have vsT := utVsZero_keepA J.ent.vs kb vsZ + have vsOk' := utVsOk_zeroA J.ent.vs kb vsZ J.vsOk + have ent : EntryInvA3 st H Z.Cs OS U Ac Pp Tb Lv (L.wr s (atAF (L.ksb s) i) 0#u64) := + ⟨J.ent.heap, hB, J.ent.os, J.ent.user.ksb kb, vsT.1, by rw [kb]; exact J.ent.ksw⟩ + have sq : SchedQueuesInv st H.tcbs Z.Cs H.omega H (L.wr s (atAF (L.ksb s) i) 0#u64) Z.Q Z.R := + inv_of_capw J.sq w1 (fun b hb => Or.inl (Or.inl (by rw [hb]; exact om.1))) + have ht : ∀ j : U64, L.mem (L.wr s (atAF (L.ksb s) i) 0#u64) (wa (L.ksb (L.wr s (atAF (L.ksb s) i) 0#u64)) (AtOff j)) = + L.mem s (wa (L.ksb s) (AtOff j)) ∨ + L.mem (L.wr s (atAF (L.ksb s) i) 0#u64) (wa (L.ksb (L.wr s (atAF (L.ksb s) i) 0#u64)) (AtOff j)) = 0#u64 := by + intro j + rw [kb] + by_cases e : wa (L.ksb s) (AtOff j) = atAF (L.ksb s) i + · exact Or.inr (by rw [e]; exact L.mem_wr_same hal) + · exact Or.inl (L.mem_wr_ne hal (vr.al _ (Or.inr (asW_at _ j))) (fun h => e (UScalar.eq_of_val_eq h).symm)) + have ht0 : ∀ j : U64, L.mem (L.wr s (atAF (L.ksb s) i) 0#u64) (wa (L.ksb s) (AtOff j)) = + L.mem s (wa (L.ksb s) (AtOff j)) ∨ + L.mem (L.wr s (atAF (L.ksb s) i) 0#u64) (wa (L.ksb s) (AtOff j)) = 0#u64 := fun j => by + have e := ht j + rwa [kb] at e + have poolOff : PoolOff st H Z.Cs OS (L.wr s (atAF (L.ksb s) i) 0#u64) := PoolOff.ofTabZ kb ht J.poolOff + -- the unmapped objects, J5, J7 + have al : ∀ q, SchedPageA st Z OS s q → ∀ j : U64, j.val < 512 → Aligned (peAF q j) := + fun q hq j hj => vr.al _ (vsW_pageA link hq hj) + have unm : UnmPtSpecA st { Z with hw := hw' } m OS (L.wr s (atAF (L.ksb s) i) 0#u64) σ' := (J.unmPt.zeroViewA hal osk al HC).hwA (hw := hw') + have j5 := J.j5_vsWriteA w1 hP hw' σ' + have capE : ∀ u ∈ OS, capA st (L.wr s (atAF (L.ksb s) i) 0#u64) u = capA st s u := fun u hu => osk.capA hu + have hdr : ∀ y ∈ OS, ∀ q mp ad, capA st s y = .HighVSpace q mp ad → + L.mem (L.wr s (atAF (L.ksb s) i) 0#u64) (wa q 4088#u64) = L.mem s (wa q 4088#u64) := by + intro y hy q mq ad hq + have hys : IsSlotA Z.th y := D.isSlot_of hy (by rw [hq]; exact fun h => by cases h) + have hqY : q ∈ Z.Y := by + have := J.j.capObj hys + rw [capAtA_eq, hq] at this + exact this.2.2.1 q mq ad rfl + have e511 : core.num.U64.wrapping_mul 511#u64 8#u64 = 4088#u64 := + UScalar.eq_of_val_eq (by rw [wm8_valA (by decide)]; rfl) + have hne : wa q 4088#u64 ≠ atAF (L.ksb s) i := fun h => by + apply hYAs q hqY 511#u64 (by decide) + have : peAF q 511#u64 = atAF (L.ksb s) i := by unfold peAF; rw [e511]; exact h + rw [this]; exact atAF_asWA hi + have hqal : Aligned (wa q 4088#u64) := by + have := J.j.1.vs.yal q hqY + have e : (wa q 4088#u64).val = q.val + 4088 := by + rw [wa_valP, show (4088#u64 : U64).val = 4088 from rfl] + exact Nat.mod_eq_of_lt (by have hlt : q.val < 2 ^ 64 := q.bv.isLt; omega) + show (wa q 4088#u64).val % 8 = 0 + rw [e]; omega + exact L.mem_wr_ne hal hqal (fun h => hne (UScalar.eq_of_val_eq h).symm) + have j7 : J7LA st Rj { Z with hw := hw' } H OS (L.wr s (atAF (L.ksb s) i) 0#u64) σ' m := + J7LA.ofTabZA J.j7 kb ht ht0 osk hdr R' + have J' := J.of_vsWrite (hw := hw') (a' := a') (σ' := σ') w1 hP (fun b hb => hb ▸ hbf) HS (fun p dev pg h => HC.data h) jx ent vsOk' (bufVs_subA J.bufVs kb vsT.2) sq poolOff + unm j5 j7 + -- the deletion's related states + have tree := D.tree.keepO osk + have hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st (L.wr s (atAF (L.ksb s) i) 0#u64) y = .Null := + fun y hy hn => (capE y hy).trans (D.nul y hy hn) + have site : DelSiteA st Z.th OS (L.wr s (atAF (L.ksb s) i) 0#u64) σ' Z.G.chains := + { cov := ⟨fun q r cs hk hw => D.cov.cnode q r cs (HS.back hk (fun _ e => by cases e)) hw, + fun q x hk => D.cov.tcb q x (HS.back hk (fun _ e => by cases e))⟩ + fr := fun y hy z hz h1 h2 => by + rw [capE y hy] at h1; rw [capE z hz] at h2; rw [capE y hy, capE z hz] + exact D.fr y hy z hz h1 h2 + irq := Fl4ProofsA64.P3.IrqRunA.keepIrq D.irq fun y hy _ => by rw [capE y ((D.lk.members y).1 hy).1] + zw := fun y hy q zt k hc => by + rw [capE y (D.sub y hy)] at hc + exact D.zw y hy q zt k hc } + have D' : DStateA st Z.th OS (L.wr s (atAF (L.ksb s) i) 0#u64) σ' Z.G := + DStateA.ofTA (Z := { Z with hw := hw' }) J'.toJEntTA (D.mdbS.keep osk) (ZombiesFinal.recap D.zf fun y hy => Or.inl (capE y hy)) tree D.sub hnul site + have hZT' : ZombieTcbNoVcpuA st OS (L.wr s (atAF (L.ksb s) i) 0#u64) := by + intro u hu q k hc + rw [capE u hu] at hc + have old := hZT u hu q k hc + have hs : IsSlotA Z.th u := by + by_contra hn + rw [D.nul u hu hn] at hc; cases hc + have htcb : Z.th q = some .tcb := by + rcases D.zw u hs _ _ _ hc with ⟨-, h, -⟩ | ⟨h, -⟩ + · exact h + · exact absurd h (by simp [capability.ZOMBIE_TCB]) + have hT : q ∈ Z.T := (J.j.1.thT q).1 htcb + have hta := J.sr.core.tcbs q hT + have hv : (wa q 3664#u64).val = q.val + 3664 := field_val hta (K := 3664#u64) (by simp) + have hH : q ∈ H.tcbs := by rw [J.tcbs]; exact hT + obtain ⟨Qb, Rb, qq⟩ := J.ent.bxi.1 + have off := (qq.omega _ om.1).2.1 q hH + rw [L.mem_wr_ne hal (field_aligned (K := 3664#u64) hta (by simp) (by simp)) (by omega)] + exact old + exact ⟨D', J', ParentAdjA.keepOS D.lk osk hPA, hZT', osk, + fun Rj' R'' => J7LA.ofTabZA J0 kb ht ht0 osk hdr R''⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelThrFpuA.lean b/proofs/Fl4ProofsA64/P3/DelThrFpuA.lean new file mode 100644 index 000000000..54a895d40 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelThrFpuA.lean @@ -0,0 +1,395 @@ +/- +P3 (AArch64, delete hooks, contract §78): **the spec's `fpu_release` at the record** (Thread finaliser, step 4b). +The concrete `fpu_release` writes no kernel word; the spec's `fpuRelease t` may run `switchLocalFpuOwner none`, which +changes `t`'s saved FPU state and `tcbCurFpu`, the machine's FPU bits and `armCurrentFpuOwner`. No relation of the +record reads any of these. + +* `FpuStepA`: the spec-only step. `fpuRelease_memA`: `fpuRelease` has an outcome that is such a step. +* `JStage3TA.fpu_recA`: the record across it, at any arch state related to the same hardware root. + +No premise. +-/ +import Fl4ProofsA64.P3.DelThrSusA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 Fl4ProofsA64.P3.Endp +open Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Notif + +noncomputable section + +/-- A TCB with its saved FPU state replaced and `tcbCurFpu` cleared. -/ +def fpuTcbA (f : FpuState) (x : Fl4SpecA64.Tcb) : Fl4SpecA64.Tcb := + { x with tcbArch := { x.tcbArch with tcbContext := { x.tcbArch.tcbContext with userFpuState := f }, + tcbCurFpu := false } } + +/-- **A spec step that changes only FPU data** (module docstring). -/ +structure FpuStepA (σ σ' : State Unit) : Prop where + eq : σ' = { σ with kheap := σ'.kheap, archState := σ'.archState, machineState := σ'.machineState } + arch : σ'.archState = { σ.archState with armCurrentFpuOwner := σ'.archState.armCurrentFpuOwner } + um : σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory + time : σ'.machineState.timeState = σ.machineState.timeState + last : σ'.machineState.lastMachineTime = σ.machineState.lastMachineTime + kh : ∀ p, σ'.kheap p = σ.kheap p ∨ + ∃ x f, σ.kheap p = some (.tcb x) ∧ σ'.kheap p = some (.tcb (fpuTcbA f x)) + +namespace FpuStepA +variable {σ σ' : State Unit} (F : FpuStepA σ σ') +include F + +theorem get {p : ObjRef} {o : KernelObject} (h : σ.kheap p = some o) (ht : ∀ x, o ≠ .tcb x) : + σ'.kheap p = some o := by + rcases F.kh p with e | ⟨x, f, e1, -⟩ + · rw [e]; exact h + · rw [h] at e1; exact absurd (Option.some.inj e1) (ht x) + +theorem back {p : ObjRef} {o : KernelObject} (h : σ'.kheap p = some o) (ht : ∀ x, o ≠ .tcb x) : + σ.kheap p = some o := by + rcases F.kh p with e | ⟨x, f, -, e2⟩ + · rw [← e]; exact h + · rw [h] at e2; exact absurd (Option.some.inj e2) (ht _) + +theorem tcb {p : ObjRef} {x : Fl4SpecA64.Tcb} (h : σ.kheap p = some (.tcb x)) : + ∃ y, σ'.kheap p = some (.tcb y) ∧ (y = x ∨ ∃ f, y = fpuTcbA f x) := by + rcases F.kh p with e | ⟨x', f, e1, e2⟩ + · exact ⟨x, by rw [e]; exact h, Or.inl rfl⟩ + · rw [h] at e1; cases e1; exact ⟨_, e2, Or.inr ⟨f, rfl⟩⟩ + +theorem tcbBack {p : ObjRef} {y : Fl4SpecA64.Tcb} (h : σ'.kheap p = some (.tcb y)) : + ∃ x, σ.kheap p = some (.tcb x) ∧ (y = x ∨ ∃ f, y = fpuTcbA f x) := by + rcases F.kh p with e | ⟨x, f, e1, e2⟩ + · exact ⟨y, by rw [← e]; exact h, Or.inl rfl⟩ + · rw [h] at e2; cases e2; exact ⟨x, e1, Or.inr ⟨f, rfl⟩⟩ + +theorem qkeep : QKeepA σ σ' := by + refine ⟨fun p y h => ?_, fun _ x h => ⟨x, F.back h (fun _ e => by cases e), List.Sublist.refl _⟩, + fun _ x h => ⟨x, F.back h (fun _ e => by cases e), List.Sublist.refl _⟩, + fun _ y k h => ⟨y, k, F.back h (fun _ e => by cases e), fun r => ⟨r, id⟩⟩, + fun _ y h => ⟨y, F.back h (fun _ e => by cases e), id⟩, + ⟨congrArg (·.domainIndex) F.eq, congrArg (·.domainStartIndex) F.eq, congrArg (·.domainList) F.eq, (by rw [F.arch])⟩⟩ + obtain ⟨x, hx, e⟩ := F.tcbBack h + rcases e with rfl | ⟨f, rfl⟩ + · exact ⟨_, hx, rfl⟩ + · exact ⟨x, hx, rfl⟩ + +theorem sched : SpecSchedA σ σ' where + kh p := by + rcases F.kh p with e | ⟨x, f, e1, e2⟩ + · exact Or.inl e + · exact Or.inr (Or.inl ⟨x, _, e1, e2, rfl, rfl, rfl⟩) + irqS := by rw [F.eq] + irqN := by rw [F.eq] + asid := by rw [F.arch] + vcur := by rw [F.arch] + cdt := by rw [F.eq] + orig := by rw [F.eq] + +theorem ssz : SSzA σ σ' := fun _ y _ h => ⟨y, F.back h (fun _ e => by cases e)⟩ + +theorem sstep : Sched.SStepA σ σ' where + fix := by constructor <;> rw [F.eq] + act := by rw [F.eq] + rq := by rw [F.eq] + rel := by rw [F.eq] + sc _ _ _ h := (F.get h (fun _ e => by cases e)).trans h.symm + tcb _ x h := by + obtain ⟨y, hy, e | ⟨f, e⟩⟩ := F.tcb h <;> subst y <;> exact ⟨_, hy, rfl, rfl, rfl⟩ + scBack _ _ _ h := (F.back h (fun _ e => by cases e)).trans h.symm + tcbBack _ _ h := let ⟨x, hx, _⟩ := F.tcbBack h; ⟨x, hx⟩ + ntfnBack _ y h := ⟨y, F.back h (fun _ e => by cases e), rfl⟩ + +theorem noBlock : NoBlockESc σ σ' := fun p x y hx hy => by + obtain ⟨y', hy', e⟩ := F.tcb hx + rw [hy] at hy'; cases hy' + rcases e with e | ⟨f, e⟩ <;> subst y <;> exact Or.inl ⟨rfl, id⟩ + +theorem irqA (h : SpecIrqA σ) : SpecIrqA σ' := by + have enode : σ'.interruptIrqNode = σ.interruptIrqNode := by rw [F.eq] + have hct : ∀ n p, capTableAt n p σ' ↔ capTableAt n p σ := fun n p => by + unfold capTableAt + rcases F.kh p with e | ⟨x, f, e1, e2⟩ + · rw [e] + · simp [e1, e2] + have cap : ∀ p o b c, σ'.kheap p = some o → capOf o b = some c → + ∃ o0, σ.kheap p = some o0 ∧ capOf o0 b = some c := fun p o b c e hc => by + rcases F.kh p with e' | ⟨x, f, e1, e2⟩ + · exact ⟨o, by rw [← e']; exact e, hc⟩ + · rw [e2] at e; cases e; exact ⟨_, e1, hc⟩ + obtain ⟨⟨⟨hinj, hcn⟩, hdom⟩, hsl, href⟩ := h + refine ⟨⟨⟨by rw [enode]; exact hinj, fun irq hi => by rw [enode]; exact (hct _ _).2 (hcn irq hi)⟩, + fun irq hi k hk => hdom irq hi k ((hct _ _).1 (by rw [enode] at hk; exact hk))⟩, + fun irq hi o b c e hc => ?_, fun p o b c e hc => ?_⟩ + · obtain ⟨o0, e0, h0⟩ := cap _ o b c e hc + exact hsl irq hi o0 b c (by rw [enode] at e0; exact e0) h0 + · obtain ⟨o0, e0, h0⟩ := cap p o b c e hc + rw [enode] + exact href p o0 b c e0 h0 + +end FpuStepA + +private theorem mop_memA {α : Type} {mop : NonDet MachineState α} {τ : State Unit} {r : α} {ms' : MachineState} + (h : (r, ms') ∈ (mop τ.machineState).1) : + (r, { τ with machineState := ms' }) ∈ (doMachineOp (Z := Unit) mop τ).1 := + ⟨(τ.machineState, τ), rfl, ((r, ms'), τ), ⟨h, rfl⟩, ((), { τ with machineState := ms' }), rfl, rfl⟩ + +private theorem lift_memA (g : MachineRestMonad Unit) (ms : MachineState) : + ∃ r, ((), { ms with machineStateRest := r }) ∈ (machineOpLift g ms).1 := by + have ne : ((ignoreFailure g) ms.machineStateRest).1.Nonempty := by + unfold ignoreFailure + split + · exact Set.singleton_nonempty _ + · exact Set.nonempty_iff_ne_empty.2 ‹_› + obtain ⟨⟨u, r⟩, hr⟩ := ne + exact ⟨r, (ms.machineStateRest, ms), rfl, (((), r), ms), ⟨hr, rfl⟩, ((), { ms with machineStateRest := r }), rfl, rfl⟩ + +private theorem setTcb_memA {σ : State Unit} {t : ObjRef} {x y : Fl4SpecA64.Tcb} (hk : σ.kheap t = some (.tcb x)) : + ((), updKSc σ t (.tcb y)) ∈ (setObject (Z := Unit) t (.tcb y) σ).1 := by + rw [setObject_eqSc (o' := .tcb y) hk rfl]; exact rfl + +private theorem getTcb_memA {σ : State Unit} {t : ObjRef} {x : Fl4SpecA64.Tcb} (hk : σ.kheap t = some (.tcb x)) : + (x, σ) ∈ (NonDet.getsThe (getTcb (Z := Unit) t) σ).1 := by + rw [Notif.getTcb_eq hk]; exact rfl + +/-- **The spec's `fpu_release` has an outcome that is an `FpuStepA`.** -/ +theorem fpuRelease_memA {σ : State Unit} {t : ObjRef} {x : Fl4SpecA64.Tcb} (hk : σ.kheap t = some (.tcb x)) : + ∃ σ', ((), σ') ∈ (fpuRelease (Z := Unit) t σ).1 ∧ FpuStepA σ σ' := by + by_cases h : σ.archState.armCurrentFpuOwner = some t + swap + · refine ⟨σ, ?_, ⟨rfl, rfl, rfl, rfl, rfl, fun _ => Or.inl rfl⟩⟩ + refine ⟨(σ.archState.armCurrentFpuOwner, σ), rfl, ?_⟩ + show ((), σ) ∈ (NonDet.when (σ.archState.armCurrentFpuOwner = some t) (switchLocalFpuOwner none) σ).1 + unfold NonDet.when + rw [if_neg h] + exact rfl + -- `enable_fpu` + obtain ⟨r1, h1⟩ := lift_memA enableFpuImpl σ.machineState + obtain ⟨ms1, e1⟩ : ∃ ms : MachineState, ms = { σ.machineState with machineStateRest := r1, fpuEnabled := true } := + ⟨_, rfl⟩ + have m1 : ((), ms1) ∈ (enableFpu σ.machineState).1 := by + subst e1 + exact ⟨((), _), h1, rfl⟩ + have M1 := mop_memA (τ := σ) m1 + obtain ⟨σ1, s1⟩ : ∃ τ : State Unit, τ = { σ with machineState := ms1 } := ⟨_, rfl⟩ + rw [← s1] at M1 + -- `save_fpu_state` + have en : ms1.fpuEnabled = true := by rw [e1] + have rd : (ms1.fpuState, σ1) ∈ (doMachineOp (Z := Unit) readFpuState σ1).1 := by + have e : σ1.machineState = ms1 := by rw [s1] + have m : (ms1.fpuState, ms1) ∈ (readFpuState σ1.machineState).1 := by + rw [e]; exact ⟨((), ms1), ⟨en, rfl⟩, rfl⟩ + have es : ({ σ1 with machineState := ms1 } : State Unit) = σ1 := by rw [s1] + have := mop_memA (τ := σ1) m + rwa [es] at this + have k1 : σ1.kheap t = some (.tcb x) := by rw [s1]; exact hk + obtain ⟨x2, ex2⟩ : ∃ y : Fl4SpecA64.Tcb, y = + { x with tcbArch := archTcbContextSet ⟨ms1.fpuState, x.tcbArch.tcbContext.userRegs⟩ x.tcbArch } := ⟨_, rfl⟩ + obtain ⟨σ2, s2⟩ : ∃ τ : State Unit, τ = updKSc σ1 t (.tcb x2) := ⟨_, rfl⟩ + have us : ((), σ2) ∈ (asUser (Z := Unit) t (setFPUState ms1.fpuState) σ1).1 := by + refine ⟨(x, σ1), getTcb_memA k1, (archTcbContextGet x.tcbArch, σ1), rfl, + (((), ⟨ms1.fpuState, x.tcbArch.tcbContext.userRegs⟩), σ1), ⟨rfl, rfl⟩, (x2, σ1), ?_, ((), σ2), ?_, rfl⟩ + · rw [ex2]; exact rfl + · rw [s2]; exact setTcb_memA k1 + have sv : ((), σ2) ∈ (saveFpuState (Z := Unit) t σ1).1 := ⟨(ms1.fpuState, σ1), rd, us⟩ + -- `disable_fpu` + obtain ⟨r3, h3⟩ := lift_memA disableFpuImpl σ2.machineState + obtain ⟨ms3, e3⟩ : ∃ ms : MachineState, ms = { σ2.machineState with machineStateRest := r3, fpuEnabled := false } := + ⟨_, rfl⟩ + have m3 : ((), ms3) ∈ (disableFpu σ2.machineState).1 := by + subst e3 + exact ⟨((), _), h3, rfl⟩ + have M3 := mop_memA (τ := σ2) m3 + obtain ⟨σ3, s3⟩ : ∃ τ : State Unit, τ = { σ2 with machineState := ms3 } := ⟨_, rfl⟩ + rw [← s3] at M3 + -- `set_arm_current_fpu_owner none` + have own : σ3.archState.armCurrentFpuOwner = some t := by rw [s3, s2, s1]; exact h + have k3 : σ3.kheap t = some (.tcb x2) := by rw [s3, s2]; simp [updKSc] + obtain ⟨x4, ex4⟩ : ∃ y : Fl4SpecA64.Tcb, y = { x2 with tcbArch := { x2.tcbArch with tcbCurFpu := false } } := + ⟨_, rfl⟩ + obtain ⟨σ4, s4⟩ : ∃ τ : State Unit, τ = updKSc σ3 t (.tcb x4) := ⟨_, rfl⟩ + have at4 : ((), σ4) ∈ (archThreadSet (Z := Unit) (fun a => { a with tcbCurFpu := false }) t σ3).1 := by + refine ⟨(x2, σ3), getTcb_memA k3, ?_⟩ + rw [s4, ex4]; exact setTcb_memA k3 + obtain ⟨σ5, s5⟩ : ∃ τ : State Unit, τ = { σ4 with archState := { σ4.archState with armCurrentFpuOwner := none } } := + ⟨_, rfl⟩ + have sa : ((), σ5) ∈ (setArmCurrentFpuOwner (Z := Unit) none σ3).1 := by + refine ⟨(some t, σ3), by rw [← own]; exact rfl, ((), σ4), at4, ((), σ5), by rw [s5]; exact rfl, rfl⟩ + have sw : ((), σ5) ∈ (switchLocalFpuOwner (Z := Unit) none σ).1 := + ⟨(some t, σ), by rw [← h]; exact rfl, ((), σ1), M1, ((), σ2), sv, ((), σ3), M3, sa⟩ + refine ⟨σ5, ?_, ?_⟩ + · refine ⟨(σ.archState.armCurrentFpuOwner, σ), rfl, ?_⟩ + show ((), σ5) ∈ (NonDet.when (σ.archState.armCurrentFpuOwner = some t) (switchLocalFpuOwner none) σ).1 + unfold NonDet.when + rw [if_pos h] + exact sw + have ek : σ5.kheap = Function.update σ.kheap t (some (.tcb (fpuTcbA ms1.fpuState x))) := by + have ex : x4 = fpuTcbA ms1.fpuState x := by rw [ex4, ex2]; rfl + rw [s5, s4, ← ex, s3, s2, s1] + simp [updKSc] + have em : σ5.machineState = ms3 := by rw [s5, s4, s3]; rfl + have m2 : σ2.machineState = ms1 := by rw [s2, s1]; rfl + refine ⟨by rw [s5, s4, s3, s2, s1]; rfl, by rw [s5, s4, s3, s2, s1]; rfl, by rw [em, e3, m2, e1], + by rw [em, e3, m2, e1], by rw [em, e3, m2, e1], fun p => ?_⟩ + rw [ek] + by_cases e : p = t + · subst e + exact Or.inr ⟨x, _, hk, Function.update_self ..⟩ + · exact Or.inl (Function.update_of_ne e ..) + +variable {S A : Type} {st : machine.Store S} [L : LawfulStore st] +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J7 : JSlotA S} {s : S} {a : A} {σ σ' : State Unit} {m : McState} + +/-- **The record across an `FpuStepA`**, at any arch state related to the same hardware root (module docstring). -/ +theorem JStage3TA.fpu_recA [J7PpA st J7] + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + (F : FpuStepA σ σ') {a' : A} (har : AR a' Z.hw) (j7 : J7 Z H OS s σ' m) : + JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a' σ' m := by + have HS := F.sched + have ntcb : ∀ {p : ObjRef} {o : KernelObject}, σ.kheap p = some o → (∀ x, o ≠ .tcb x) → σ'.kheap p = some o := + fun h ht => F.get h ht + have eidle : σ'.idleThread = σ.idleThread := by rw [F.eq] + have ecur : σ'.curThread = σ.curThread := by rw [F.eq] + have ecsc : σ'.curSc = σ.curSc := by rw [F.eq] + have eact : σ'.schedulerAction = σ.schedulerAction := by rw [F.eq] + have erq : σ'.readyQueues = σ.readyQueues := by rw [F.eq] + have erl : σ'.releaseQueue = σ.releaseQueue := by rw [F.eq] + have enode : σ'.interruptIrqNode = σ.interruptIrqNode := by rw [F.eq] + have sr : SchedSRA st Z m κ s σ' := + J.sr.stable (W := fun _ => False) (WritesIn.refl _ _) (fun _ h => h.elim) F.sstep F.noBlock + have scCov : ScCovA Z.C m σ' := fun p x k h => J.scCov p x k (F.back h (fun _ e => by cases e)) + have bufAl : UtBufAlA Z.T σ' := fun t ht y hy => by + obtain ⟨x, hx, e | ⟨f, e⟩⟩ := F.tcbBack hy <;> subst y <;> exact J.bufAl t ht x hx + have ep : EpStateRel Z.T E Rp Bf (L.mem s) σ' := by + refine ⟨fun e he => ?_, fun r hr => ?_, fun t ht => ?_, fun bp hb k hk => by rw [F.um]; exact J.ep.buf bp hb k hk⟩ + · obtain ⟨x, hx, h⟩ := J.ep.ep e he + exact ⟨x, ntcb hx (fun _ e => by cases e), h⟩ + · obtain ⟨x, hx, h⟩ := J.ep.reply r hr + exact ⟨x, ntcb hx (fun _ e => by cases e), h⟩ + · obtain ⟨x, hx, h⟩ := J.ep.tcb t ht + obtain ⟨y, hy, e | ⟨f, e⟩⟩ := F.tcb hx <;> subst e + · exact ⟨_, hy, h⟩ + · exact ⟨_, hy, ⟨h.st, h.blk, h.send, h.recv, h.reply, h.regs, h.prio, h.fault0, h.faultT, h.bound, h.faultR⟩⟩ + have ntfn : NtfnStateRel N Z.T (L.mem s) σ' := by + refine ⟨fun p hp => ?_, fun t ht => ?_⟩ + · obtain ⟨x, hx, h⟩ := J.ntfn.ntfn p hp + exact ⟨x, ntcb hx (fun _ e => by cases e), h⟩ + · obtain ⟨x, hx, h⟩ := J.ntfn.tcb t ht + obtain ⟨y, hy, e | ⟨f, e⟩⟩ := F.tcb hx <;> subst e + · exact ⟨_, hy, h⟩ + · exact ⟨_, hy, ⟨h.st, h.blk, h.badge, h.prio⟩⟩ + have reply : ReplyStackRelA (L.mem s) Rp Z.C σ' (scNameA m) stack := + { stackRel := fun c hc => by + obtain ⟨⟨x, k, hx, e⟩, h⟩ := J.reply.stackRel c hc + exact ⟨⟨x, k, ntcb hx (fun _ e => by cases e), e⟩, h⟩ + unique := J.reply.unique + owner := fun r hr x hx p => J.reply.owner r hr x (F.back hx (fun _ e => by cases e)) p + detached := J.reply.detached } + have buf : BufCover st Z.T Bf s σ' := fun t ht y b sb w hy hb hw => by + obtain ⟨x, hx, e | ⟨f, e⟩⟩ := F.tcbBack hy <;> subst y <;> exact J.bufCover t ht x b sb w hx hb hw + have idle : IdleNoFaultA m σ' := fun c hc => by + obtain ⟨x, hx, h⟩ := J.idleNoFault c hc + obtain ⟨y, hy, e | ⟨f, e⟩⟩ := F.tcb hx <;> subst y <;> exact ⟨_, hy, h⟩ + have mcp : McpRelA Z.T m (L.mem s) σ' := fun t ht => by + obtain ⟨x, hx, h, hf⟩ := J.mcp t ht + obtain ⟨y, hy, e | ⟨f, e⟩⟩ := F.tcb hx <;> subst y <;> exact ⟨_, hy, h, hf⟩ + have yr : YieldRelA Z.T Z.C m (L.mem s) σ' := by + refine ⟨fun t ht => ?_, fun t ht hne => ?_, fun c hc y k hy p hp => + J.yieldRel.frm c hc y k (F.back hy (fun _ e => by cases e)) p hp⟩ + · obtain ⟨x, hx, h⟩ := J.yieldRel.tcb t ht + obtain ⟨y, hy, e | ⟨f, e⟩⟩ := F.tcb hx <;> subst y <;> exact ⟨_, hy, h⟩ + · obtain ⟨hC, y, k, hy, h⟩ := J.yieldRel.toSc t ht hne + exact ⟨hC, y, k, ntcb hy (fun _ e => by cases e), h⟩ + have tcbS : ∀ p x, σ.kheap p = some (.tcb x) → ∃ y, σ'.kheap p = some (.tcb y) ∧ y.tcbState = x.tcbState ∧ + y.tcbSchedContext = x.tcbSchedContext := fun p x hx => by + obtain ⟨y, hy, e | ⟨f, e⟩⟩ := F.tcb hx <;> subst y <;> exact ⟨_, hy, rfl, rfl⟩ + have tcbB : ∀ p y, σ'.kheap p = some (.tcb y) → ∃ x, σ.kheap p = some (.tcb x) ∧ y.tcbState = x.tcbState := + fun p y hy => by obtain ⟨x, hx, e | ⟨f, e⟩⟩ := F.tcbBack hy <;> subst y <;> exact ⟨_, hx, rfl⟩ + have CF := CurFactsA.back (σ := σ) (σ' := σ') ⟨J.sinv.zc, J.sinv.rlsRun, J.sinv.curScReady⟩ + (fun t ht => by rw [erl] at ht; exact ht) (by rw [F.eq]) ecsc (by rw [F.eq]) + (fun p y hy => by obtain ⟨x, hx, e | ⟨f, e⟩⟩ := F.tcbBack hy <;> subst y <;> exact ⟨x, hx, rfl⟩) + (fun p x hx _ => by + obtain ⟨y, hy, e, -⟩ := tcbS p x hx + exact ⟨y, hy, fun r => by rw [e]; exact r⟩) + (fun y k hy => ⟨y, F.back hy (fun _ e => by cases e), fun a => ⟨a, id⟩⟩) + have sinv : SInvXA σ' m n := + { mc := J.sinv.mc + view := by unfold McView; rw [eidle]; exact J.sinv.view + curTcb := by + obtain ⟨x, hx⟩ := J.sinv.curTcb + obtain ⟨y, hy, -⟩ := tcbS _ x hx + exact ⟨y, by rw [ecur]; exact hy⟩ + idleTcb := by + obtain ⟨x, hx, h⟩ := J.sinv.idleTcb + obtain ⟨y, hy, e, -⟩ := tcbS _ x hx + exact ⟨y, by rw [eidle]; exact hy, e.trans h⟩ + onlyIdle := fun t y hy h => by + obtain ⟨x, hx, e⟩ := tcbB t y hy + exact J.sinv.onlyIdle t x hx (e ▸ h) + idleSc := by + obtain ⟨x, y, k, hx, h1, h2, h3⟩ := J.sinv.idleSc + obtain ⟨x', hx', -, e⟩ := tcbS _ x hx + exact ⟨x', y, k, by rw [eidle]; exact hx', e.trans h1, ntcb h2 (fun _ e => by cases e), by rw [eidle]; exact h3⟩ + curSc := by + obtain ⟨y, k, hy⟩ := J.sinv.curSc + exact ⟨y, k, by rw [ecsc]; exact ntcb hy (fun _ e => by cases e)⟩ + ctQ := fun ha => by rw [ecur, erq]; exact J.sinv.ctQ (eact.symm.trans ha) + idleQ := by rw [erq]; exact J.sinv.idleQ + rq := fun d p t ht => by + rw [erq] at ht + obtain ⟨x, hx, h⟩ := J.sinv.rq d p t ht + obtain ⟨y, hy, e, -⟩ := tcbS _ x hx + exact ⟨y, hy, by rw [e]; exact h⟩ + rls := fun t ht => by + rw [erl] at ht + obtain ⟨x, hx⟩ := J.sinv.rls t ht + obtain ⟨y, hy, -⟩ := tcbS _ x hx + exact ⟨y, hy⟩ + rpos := fun p y k hy => J.sinv.rpos p y k (F.back hy (fun _ e => by cases e)) + zc := CF.1 + rlsRun := CF.2.1 + curScReady := CF.2.2 + faultSt := fun p y hy fl => by + obtain ⟨x, hx, e | ⟨f, e⟩⟩ := F.tcbBack hy <;> subst y <;> exact J.sinv.faultSt p x hx fl + irqN := F.irqA J.sinv.irqN } + have idleVt : ∀ c < n, vtCapA σ' (m.idleThreadOf c) = some .nullCap ∧ vcpuOfA σ' (m.idleThreadOf c) = none := by + intro c hc + obtain ⟨x, hx, -⟩ := J.sidle.f3 c hc + obtain ⟨y, hy, e | ⟨f, e⟩⟩ := F.tcb hx <;> subst y <;> + exact idleVtKeepA hx hy rfl rfl (J.sidle.idleSlot c hc) (J.sidle.idleBound c hc) + have sidle : SIdleA σ' m n := + { f3 := fun c hc => by + obtain ⟨x, hx, h⟩ := J.sidle.f3 c hc + obtain ⟨y, hy, e, -⟩ := tcbS _ x hx + exact ⟨y, hy, e.trans h⟩ + f4 := fun y k hy => J.sidle.f4 y k (F.back hy (fun _ e => by cases e)) + f5 := fun p y k hy h => J.sidle.f5 p y k (F.back hy (fun _ e => by cases e)) (by rw [← eidle]; exact h) + f6 := J.sidle.f6.of_eq HS.specCapAt + f7 := fun y k hy => by + rw [show σ'.curTime = σ.curTime by rw [F.eq]] + exact J.sidle.f7 y k (F.back hy (fun _ e => by cases e)) + idleSlot := fun c hc => (idleVt c hc).1 + idleBound := fun c hc => (idleVt c hc).2 } + have sact : SActA σ' := + { f1 := fun u hu => by rw [ecur, eidle]; exact J.sact.f1 u (eact ▸ hu) + f2 := by + obtain ⟨x, hx, h⟩ := J.sact.f2 + obtain ⟨y, hy, e, -⟩ := tcbS _ x hx + exact ⟨y, by rw [ecur]; exact hy, by rw [e]; exact h⟩ } + have blkQ : BlkQueuedA Z.T E N (L.mem s) σ' := by + refine ⟨fun t ht y hy e he => ?_, fun t ht y hy p hp => ?_⟩ + · obtain ⟨x, hx, es⟩ := tcbB _ y hy + obtain ⟨h1, z, h2, h3⟩ := J.blkQ.ep t ht x hx e (es ▸ he) + exact ⟨h1, z, ntcb h2 (fun _ e => by cases e), h3⟩ + · obtain ⟨x, hx, es⟩ := tcbB _ y hy + obtain ⟨h1, z, q, h2, h3⟩ := J.blkQ.ntfn t ht x hx p (es ▸ hp) + exact ⟨h1, z, q, ntcb h2 (fun _ e => by cases e), h3⟩ + exact JStage3TA.of_schedWrSN J (stack' := stack) (fun _ _ h => h) (Ac' := Ac) (Q := Z.Q) (Rl := Z.R) + (curOf := Z.curOf) (curScOf := Z.curScOf) (s' := s) (a' := a') (σ' := σ') (WritesIn.refl _ _) HS F.ssz F.qkeep F.um har + (fun _ _ => rfl) bufAl J.ent J.sq J.poolOff sr scCov ep ntfn reply buf idle mcp yr sinv sidle J.runVcpu J.vr + (fun u => by rw [eact]; exact J.noSwitch u) sact (blkQ := blkQ) J.j5 j7 + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelThrFrontA.lean b/proofs/Fl4ProofsA64/P3/DelThrFrontA.lean new file mode 100644 index 000000000..3ed3196cc --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelThrFrontA.lean @@ -0,0 +1,106 @@ +/- +P3 (AArch64, delete hooks W3, contract §72): **the Thread finaliser's first three calls** — `smp.remote_tcb_stall` +(a no-op under [S] `numNodes = 1`), `vcpu.detach` and `notification.unbind_notification` — refine +`fl4VcpuDetach t` followed by `unbindNotification t`, and `JStage3TA` holds again. + +Premises: [S] `h1`; [P] `hCs`; [P] `hDet`, the conclusion of S4's frozen `vcpu_detach_stage3TA` +(`handoff/s4-detach-stmt.lean`) at `(s, a, σ, t)`, verbatim. That theorem's own premises (among them `SchedValidA σ`, +G1's F8, and `hClFlushState`) are the caller's. +-/ +import Fl4ProofsA64.P3.DelThrUnbindA +import Fl4ProofsA64.P3.Stage3TA +import Fl4ProofsA64.P3.Stage3J5A +import Fl4ProofsA64.P3.Stage3PreemptTA +import Fl4ProofsA64.P3.Stage3SchedValidA +import Fl4ProofsA64.P3.VcpuPremisesA +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3.Vcpu +open Fl4Spec Fl4SpecA64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST +open Fl4ProofsA64.P3 Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-- **The Thread finaliser's front** (module docstring). -/ +theorem thrFront_recA {Ag : Set Aeneas.Std.U64} + {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + {AR : A → HwRootA → Prop} {Clock : A → Fl4SpecA64.MachineState → Prop} + {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J7 : JSlotA S} + [J7PpA st J7] [J7PpKA st J7] + {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < Fl4SpecA64.fl4A64Platform.numNodes) + (h1 : Fl4SpecA64.fl4A64Platform.numNodes = 1) + {t : U64} (ht : t ∈ Z.T) + (hDet : vcpu.detach st ar s a t ⦃ res => + ∃ (Q' : U64 → U64 → List U64) (R' : U64 → List U64) (hw' : HwRootA) (σ' : Fl4SpecA64.State Unit), + ((), σ') ∈ (fl4VcpuDetach (specPtr t) σ).1 ∧ + JStage3TA st AR { Z with Q := Q', R := R', hw := hw' } H OS U Ac Pp Tb Lv ia ie + N E Rp Bf stack κ n (J5A st) J7 res.1 res.2 σ' m ∧ + SchedValidA σ' ∧ + VcSym1A st Z.T res.1 ∧ VcSym2A st Z.V res.1 ∧ + Clock res.2 σ'.machineState ∧ + -- Cleared VCPU / run-link words on `t` and any previously bound VCPU `v₀` + L.mem res.1 (wa t 3664#u64) = 0#u64 ∧ + L.mem res.1 (runVcpuA t) = 0#u64 ∧ + (L.mem s (wa t 3664#u64) ≠ 0#u64 → + L.mem res.1 (L.mem s (wa t 3664#u64)) = 0#u64 ∧ + L.mem res.1 (wa (L.mem s (wa t 3664#u64)) 24#u64) = 0#u64) ∧ + -- Concrete frame guarantees needed by the rest of `finalise_cap_object (.Thread t) true` + L.ksb res.1 = L.ksb s ∧ L.cpu res.1 = L.cpu s ∧ + (∀ y ∈ OS, capA st res.1 y = capA st s y) ∧ + L.mem res.1 (boundA t) = L.mem s (boundA t) ∧ + L.mem res.1 (scA t) = L.mem s (scA t) ∧ + L.mem res.1 (stA t) = L.mem s (stA t) ∧ + -- Spec-side frame on `t` (other than `tcbVcpu` / `tcbRunVcpu`) + (∀ x, σ.kheap (specPtr t) = some (.tcb x) → + ∃ x', σ'.kheap (specPtr t) = some (.tcb x') ∧ + x'.tcbArch.tcbVcpu = none ∧ x'.tcbExt.tcbRunVcpu = none ∧ + x'.tcbBoundNotification = x.tcbBoundNotification ∧ + x'.tcbSchedContext = x.tcbSchedContext ∧ + x'.tcbState = x.tcbState) ∧ + Std6GA st Ag Z H OS Bf res.1 σ' ⦄) : + (do + let (s1, a1) ← smp.remote_tcb_stall st ar s a t + let (s2, a2) ← vcpu.detach st ar s1 a1 t + let s3 ← notification.unbind_notification st s2 t + ok (s3, a2)) ⦃ r => + ∃ (Q' : U64 → U64 → List U64) (R' : U64 → List U64) (hw' : HwRootA) (σ1 σ2 : Fl4SpecA64.State Unit), + ((), σ1) ∈ (fl4VcpuDetach (specPtr t) σ).1 ∧ + unbindNotification (Z := Unit) (specPtr t) σ1 = ({((), σ2)}, False) ∧ + JStage3TA st AR { Z with Q := Q', R := R', hw := hw' } H OS U Ac Pp Tb Lv ia ie + N E Rp Bf stack κ n (J5A st) J7 r.1 r.2 σ2 m ∧ + SchedValidA σ1 ∧ Clock r.2 σ2.machineState ∧ σ2.machineState = σ1.machineState ∧ + L.ksb r.1 = L.ksb s ∧ L.cpu r.1 = L.cpu s ∧ + (∀ y ∈ OS, capA st r.1 y = capA st s y) ∧ + L.mem r.1 (wa t 3664#u64) = 0#u64 ∧ L.mem r.1 (runVcpuA t) = 0#u64 ∧ + L.mem r.1 (scA t) = L.mem s (scA t) ∧ L.mem r.1 (stA t) = L.mem s (stA t) ∧ + (∀ R7 : Set U64 → JSlotA S, J7 = J7LA st R7 → (∀ y, IsSlotA Z.th y → y ∈ OS) → Std6GA st Ag Z H OS Bf r.1 σ2) ⦄ := by + rw [J.stall_singleA hCs h1 ht, bind_ok] + refine WP.spec_bind hDet ?_ + rintro ⟨s2, a2⟩ ⟨Q', R', hw', σ1, m1, J1, sv, -, -, cl, v0, rv0, -, kb, cpu, caps, -, sc, sta, -, h6⟩ + dsimp only at J1 cl v0 rv0 kb cpu caps sc sta ⊢ + refine WP.spec_bind (J1.thrUnbind_recA (t := t) ht) ?_ + rintro s3 ⟨σ2, sp, J2, w, -, osk, -, -, rk, tw, k6⟩ + rw [WP.spec_ok] + have e3664 : L.mem s3 (wa t 3664#u64) = L.mem s2 (wa t 3664#u64) := + tw t ht _ (by simp) (by simp) (by simp) + have erv : L.mem s3 (runVcpuA t) = L.mem s2 (runVcpuA t) := by + unfold runVcpuA; exact tw t ht _ (by simp) (by simp) (by simp) + have esc : L.mem s3 (scA t) = L.mem s2 (scA t) := by + unfold Fl4ProofsA64.P1.scA; exact tw t ht _ (by simp) (by simp) (by simp) + have est : L.mem s3 (stA t) = L.mem s2 (stA t) := by + have h : stA t = wa t 3328#u64 := by rw [stA_eq] + rw [h]; exact tw t ht _ (by simp) (by simp) (by simp) + have em : σ2.machineState = σ1.machineState := by rw [rk.1] + exact ⟨Q', R', hw', σ1, σ2, m1, sp, J2, sv, em ▸ cl, em, w.1.trans kb, w.2.1.trans cpu, + fun y hy => (osk.capA hy).trans (caps y hy), e3664.trans v0, erv.trans rv0, esc.trans sc, est.trans sta, + fun R7 e sub => (k6 R7 e sub (h6.ghostA Z.G Q' R' hw')).ghostA Z.G Z.Q Z.R Z.hw⟩ + +end + +end Fl4ProofsA64.P3.Vcpu diff --git a/proofs/Fl4ProofsA64/P3/DelThrHookA.lean b/proofs/Fl4ProofsA64/P3/DelThrHookA.lean new file mode 100644 index 000000000..7db046e41 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelThrHookA.lean @@ -0,0 +1,562 @@ +/- +P3 (AArch64, delete hooks, contract §78): **the Thread finaliser hook** `finHookThreadA : FinHookKA .thread` at +`W ∧ DelPpTimeA ∧ SchedValidA`. A non-final cap is a no-op. A final one runs `thrFront_recA` (stall, `vcpu.detach`, +`unbind_notification`), `thrMid_recA` (timer, SC, `suspend`), `debugRm_recA`, then `fpu_release` (`fpu_recA`). The spec +side moves the VCPU dissociation to `prepareThreadDelete` (`hComm`). + +Premises. +* [P] `hA`, `hCs`, `hmb`, `hmax`, `htu`, `Da`, `hP`, `hAK`, `hVR`; [S] `h1`. +* Temporary, from other lines (stated, not proved here): + - `hDetS`: S4's updated frozen `vcpu_detach_stage3TA` at this J7, with `VcpuStateSnapA` / `UserFrSpecA` (record + edit #6) and `hFrOff`, and no `[J7FrA]` binder; + - `hComm`, `hKeepB`: S4's `fl4Dissociate_comm_tcbFinA` and `fl4VcpuEndRun_keeps_bindA`; +The record edit #6 facts the detach takes are `W`'s `Std6A` (`DelXA6`), carried to the post-state. +New module. +-/ +import Fl4ProofsA64.P3.DelThrFrontA +import Fl4ProofsA64.P3.DelThrSusA +import Fl4ProofsA64.P3.DelThrFpuA +import Fl4ProofsA64.P3.DelScHookA +import Fl4ProofsA64.P3.DelClockA +import Fl4ProofsA64.P3.DelRdThA +import Fl4ProofsA64.P3.DelThrTimeA +import Fl4ProofsA64.P3.Stage3DelSvThrA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 Fl4ProofsA64.P3.Endp +open Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Vcpu + +local notation "wa" => core.num.U64.wrapping_add + +noncomputable section + +private theorem thrBindAssocA {α β γ : Type} (r : Result α) (f : α → Result β) (g : β → Result γ) : + Std.bind (Std.bind r f) g = Std.bind r (fun x => Std.bind (f x) g) := by + cases r with + | ret x => simp only [bind_ok] + | vis i k => simp only [bind_vis]; congr 1; funext y; cases i; exact PEmpty.elim y + | div => simp only [bind_div] + +private theorem archGet_memA {α : Type} {f : ArchTcb → α} {t : ObjRef} {σ : State Unit} {r : α × State Unit} + (h : r ∈ (archThreadGet (Z := Unit) f t σ).1) : + ∃ x, σ.kheap t = some (.tcb x) ∧ r = (f x.tcbArch, σ) := by + obtain ⟨y, hy, hr⟩ := h + have hy' : y ∈ (NonDet.assertOpt (getTcb (Z := Unit) t σ) σ).1 := hy + cases hk : σ.kheap t with + | none => + have e : getTcb (Z := Unit) t σ = none := by simp [getTcb, readObject, hk] + rw [e] at hy'; exact absurd hy' (Set.notMem_empty _) + | some o => + cases o with + | tcb x => + have e : getTcb (Z := Unit) t σ = some x := by simp [getTcb, readObject, hk] + rw [e] at hy' + have : y = (x, σ) := hy' + subst this + exact ⟨x, rfl, hr⟩ + | _ => + have e : getTcb (Z := Unit) t σ = none := by simp [getTcb, readObject, hk] + rw [e] at hy'; exact absurd hy' (Set.notMem_empty _) + +private theorem archGet_ofA {α : Type} (f : ArchTcb → α) {t : ObjRef} {σ : State Unit} {x : Fl4SpecA64.Tcb} + (hk : σ.kheap t = some (.tcb x)) : (f x.tcbArch, σ) ∈ (archThreadGet (Z := Unit) f t σ).1 := + ⟨(x, σ), by rw [Notif.getTcb_eq hk]; exact rfl, rfl⟩ + +private theorem whenT_memA {f : SMonad Unit Unit} {τ τ' : State Unit} (h : ((), τ') ∈ (f τ).1) : + ((), τ') ∈ (NonDet.when (true = true) f τ).1 := by + unfold NonDet.when; rw [if_pos rfl]; exact h + +/-- `fl4CancelRun t` keeps `t`'s arch TCB. -/ +private theorem cancelRun_archA {t : ObjRef} {σ σ' : State Unit} {x : Fl4SpecA64.Tcb} + (hk : σ.kheap t = some (.tcb x)) (h : ((), σ') ∈ (fl4CancelRun (Z := Unit) t σ).1) : + ∃ x', σ'.kheap t = some (.tcb x') ∧ x'.tcbArch = x.tcbArch := by + cases hr : x.tcbExt.tcbRunVcpu with + | none => + rw [fl4CancelRun_quietA hk hr] at h + obtain ⟨-, rfl⟩ := Prod.mk.inj (show ((), σ') = ((), σ) from h) + exact ⟨x, hk, rfl⟩ + | some vc => + unfold fl4CancelRun getRunVcpu at h + rw [nd_bind_oneSc (threadGet_eqSc _ hk), hr] at h + obtain ⟨⟨_, σ1⟩, h1, h2⟩ := mem_bindA.1 h + unfold fl4VcpuExtUpdate at h1 + obtain ⟨⟨v, σg⟩, hg, h3⟩ := mem_bindA.1 h1 + have hg' : (v, σg) ∈ (NonDet.assertOpt (vcpusOf (Z := Unit) σ vc) σ).1 := by + unfold getVcpu NonDet.getsMap at hg + rwa [Fl4ProofsA64.P3.Notif.nd_bind_one' (show NonDet.gets (vcpusOf (Z := Unit)) σ = + ({(vcpusOf σ, σ)}, False) from rfl)] at hg + cases hv : vcpusOf (Z := Unit) σ vc with + | none => rw [hv] at hg'; exact absurd hg' (Set.notMem_empty _) + | some y => + rw [hv] at hg' + obtain ⟨-, e⟩ := Prod.mk.inj (show (v, σg) = (y, σ) from hg') + rw [e] at h3 + have hne : t ≠ vc := fun e' => by + subst e' + simp [vcpusOf, optMap, aobjsOf, aobjOf, hk] at hv + obtain ⟨_, -, e1⟩ := setObject_mem h3 + have hk1 : σ1.kheap t = some (.tcb x) := by + rw [show σ1 = _ from e1]; exact (Function.update_of_ne hne _ _).trans hk + obtain ⟨x1, hx1, e2⟩ := threadSet_memA _ _ h2 + rw [hk1] at hx1 + cases hx1 + rw [show σ' = _ from e2] + exact ⟨_, Function.update_self _ _ _, rfl⟩ + +/-- `fl4VcpuDetach` keeps the time fields. -/ +private theorem detach_tmA (t : ObjRef) : NdTmA (fl4VcpuDetach t) := by + unfold fl4VcpuDetach fl4VcpuUnbind + refine NdTmA.bind (fl4CancelRun_tmA t) fun _ => NdTmA.bind (by unfold archThreadGet; tm) fun o => ?_ + cases o with + | none => exact NdTmA.pure _ + | some v => + refine NdTmA.bind (fl4VcpuEndRun_tmA v) fun _ => NdTmA.bind (by unfold getVcpu; tm) fun x => ?_ + cases x.vcpuTcb with + | none => exact NdTmA.pure _ + | some u => unfold dissociateVcpuTcb; tm + +/-- **The spec side of the final Thread finaliser** (module docstring). -/ +private theorem thrSpecA {t : ObjRef} {σ σ1 σ2 σ5 σ6 σ7 : State Unit} + (hDet : ((), σ1) ∈ (fl4VcpuDetach t σ).1) + (hU : unbindNotification (Z := Unit) t σ1 = ({((), σ2)}, False)) + (hSc : ((), σ5) ∈ (unbindFromSc (Z := Unit) t σ2).1) + (hSus : ((), σ6) ∈ (Fl4SpecA64.suspend (Z := Unit) t σ5).1) + (hFpu : ((), σ7) ∈ (fpuRelease (Z := Unit) t σ6).1) + (hv6 : ∀ σc xc, ((), σc) ∈ (fl4CancelRun (Z := Unit) t σ).1 → σc.kheap t = some (.tcb xc) → + xc.tcbArch.tcbVcpu = none → ∃ y, σ6.kheap t = some (.tcb y) ∧ y.tcbArch.tcbVcpu = none) + (hLink : ∀ v σc σe xc, ((), σc) ∈ (fl4CancelRun (Z := Unit) t σ).1 → σc.kheap t = some (.tcb xc) → + xc.tcbArch.tcbVcpu = some v → ((), σe) ∈ (fl4VcpuEndRun (Z := Unit) v σc).1 → + (∃ xt, σe.kheap t = some (.tcb xt) ∧ xt.tcbArch.tcbVcpu = some v) ∧ + ∃ xv, σe.kheap v = some (.archObj (.vcpu xv)) ∧ xv.vcpuTcb = some t) + (hComm : ∀ (τ τ1 τ2 : State Unit) (t v : ObjRef), + (∃ xt, τ.kheap t = some (.tcb xt) ∧ xt.tcbArch.tcbVcpu = some v) → + (∃ xv, τ.kheap v = some (.archObj (.vcpu xv)) ∧ xv.vcpuTcb = some t) → + ((), τ1) ∈ (dissociateVcpuTcb (Z := Unit) v t τ).1 → + ((), τ2) ∈ (((do unbindNotification t; unbindFromSc t; suspend t) : SMonad Unit Unit) τ1).1 → + ((), τ2) ∈ (((do unbindNotification t; unbindFromSc t; suspend t; dissociateVcpuTcb v t) : + SMonad Unit Unit) τ).1) : + ((Fl4SpecA64.Cap.zombie t none fl4TcbSlotCount, Fl4SpecA64.Cap.nullCap), σ7) ∈ + (fl4FinaliseCap (Z := Unit) (.threadCap t) true σ).1 := by + have fin : ∀ (τ τu τs τm τp : State Unit), ((), τu) ∈ (unbindNotification (Z := Unit) t τ).1 → + ((), τs) ∈ (unbindFromSc (Z := Unit) t τu).1 → ((), τm) ∈ (Fl4SpecA64.suspend (Z := Unit) t τs).1 → + ((), τp) ∈ (prepareThreadDelete (Z := Unit) t τm).1 → + ((Fl4SpecA64.Cap.zombie t none fl4TcbSlotCount, Fl4SpecA64.Cap.nullCap), τp) ∈ + (finaliseCap (Z := Unit) (.threadCap t) true τ).1 := fun τ τu τs τm τp h1 h2 h3 h4 => by + simp only [finaliseCap] + exact mem_bindA.2 ⟨((), τu), whenT_memA h1, mem_bindA.2 ⟨((), τs), whenT_memA h2, + mem_bindA.2 ⟨((), τm), whenT_memA h3, mem_bindA.2 ⟨((), τp), whenT_memA h4, rfl⟩⟩⟩⟩ + have hU' : ((), σ2) ∈ (unbindNotification (Z := Unit) t σ1).1 := by rw [hU]; exact rfl + unfold fl4VcpuDetach at hDet + obtain ⟨⟨_, σc⟩, hc, hDet⟩ := mem_bindA.1 hDet + obtain ⟨⟨vo, σc'⟩, hg, hD2⟩ := mem_bindA.1 hDet + obtain ⟨xc, hkc, e⟩ := archGet_memA hg + obtain ⟨rfl, rfl⟩ := Prod.mk.inj e + dsimp only at hD2 + simp only [fl4FinaliseCap] + cases hv : xc.tcbArch.tcbVcpu with + | none => + rw [hv] at hD2 + have e1 : σ1 = σc' := (Prod.mk.inj (show ((), σ1) = ((), σc') from hD2)).2 + subst e1 + obtain ⟨y, hy, hyv⟩ := hv6 _ xc hc hkc hv + have hp : ((), σ7) ∈ (prepareThreadDelete (Z := Unit) t σ6).1 := by + unfold prepareThreadDelete + refine mem_bindA.2 ⟨_, archGet_ofA _ hy, ?_⟩ + dsimp only + rw [hyv] + dsimp only + first + | exact hFpu + | exact mem_bindA.2 ⟨((), σ6), rfl, hFpu⟩ + refine mem_bindA.2 ⟨((), σ1), whenT_memA ?_, fin _ _ _ _ _ hU' hSc hSus hp⟩ + refine mem_bindA.2 ⟨((), σ1), hc, mem_bindA.2 ⟨_, archGet_ofA _ hkc, ?_⟩⟩ + simp only [hv] + exact rfl + | some v => + rw [hv] at hD2 + unfold fl4VcpuUnbind at hD2 + obtain ⟨⟨_, σe⟩, he, hD2⟩ := mem_bindA.1 hD2 + obtain ⟨htv, xv, hkv, hvt⟩ := hLink v _ _ xc hc hkc hv he + have gv : getVcpu (Z := Unit) v σe = ({(xv, σe)}, False) := by + have h0 : vcpusOf σe v = some xv := by simp only [vcpusOf, optMap, aobjsOf, hkv]; rfl + unfold getVcpu NonDet.getsMap + show NonDet.bind (NonDet.gets vcpusOf) (fun m => NonDet.assertOpt (m v)) σe = _ + rw [Fl4ProofsA64.P3.Notif.nd_bind_one' (show NonDet.gets (vcpusOf (Z := Unit)) σe = ({(vcpusOf σe, σe)}, False) from rfl), + h0] + rfl + obtain ⟨⟨xv', σg⟩, hg', hD3⟩ := mem_bindA.1 hD2 + rw [gv] at hg' + obtain ⟨e1, e2⟩ := Prod.mk.inj (show (xv', σg) = (xv, σe) from hg') + rw [e1, e2] at hD3 + simp only [hvt] at hD3 + have h2 : ((), σ6) ∈ (((do unbindNotification t; unbindFromSc t; suspend t) : SMonad Unit Unit) σ1).1 := + mem_bindA.2 ⟨((), σ2), hU', mem_bindA.2 ⟨((), σ5), hSc, hSus⟩⟩ + have c := hComm _ _ _ t v htv ⟨xv, hkv, hvt⟩ hD3 h2 + obtain ⟨⟨_, σu⟩, c1, c⟩ := mem_bindA.1 c + obtain ⟨⟨_, σs⟩, c2, c⟩ := mem_bindA.1 c + obtain ⟨⟨_, σm⟩, c3, c4⟩ := mem_bindA.1 c + have hp : ((), σ7) ∈ (prepareThreadDelete (Z := Unit) t σm).1 := by + have c4' := c4 + unfold dissociateVcpuTcb at c4' + obtain ⟨⟨o, τ⟩, g1, c5⟩ := mem_bindA.1 c4' + obtain ⟨xm, hkm, e⟩ := archGet_memA g1 + obtain ⟨rfl, rfl⟩ := Prod.mk.inj e + obtain ⟨⟨w, τ'⟩, -, c6⟩ := mem_bindA.1 c5 + obtain ⟨⟨_, τ''⟩, hset, -⟩ := mem_bindA.1 c6 + have hm : xm.tcbArch.tcbVcpu = some v := hset.1.1 + unfold prepareThreadDelete + refine mem_bindA.2 ⟨_, archGet_ofA _ hkm, ?_⟩ + simp only [hm] + exact mem_bindA.2 ⟨((), σ6), c4, hFpu⟩ + refine mem_bindA.2 ⟨((), σe), whenT_memA ?_, fin _ _ _ _ _ c1 c2 c3 hp⟩ + refine mem_bindA.2 ⟨((), _), hc, mem_bindA.2 ⟨_, archGet_ofA _ hkc, ?_⟩⟩ + simp only [hv] + exact he + +section +variable {m : Fl4SpecA64.McState} +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Pp Tb : Set U64} {Lv : U64 → Nat} {ia ie : Nat} + {N E Rp : Set U64} {κ : U64 → U64} {n : Nat} {Clock : A → Fl4SpecA64.MachineState → Prop} + +variable {Ag : Set U64} + +set_option maxHeartbeats 3200000 in +/-- **The Thread finaliser hook** (module docstring). -/ +theorem finHookThreadA {ipa : U64} (s0 : S) + (Rv : S → A → State Unit → MdbGhostA → U64 → Prop) + (hA : P0.ArchTotal ar) (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (Da : DelArchA st ar AR Clock) + (hP : VcpuPlatAgreeA ar) (hAK : VcpuArchKeepsA ar AR) (hVR : VcpuVmRootA st ar AR) + (hDetS : ∀ {Z' : XSetsA} {Ac : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} + {s : S} {a : A} {σ : State Unit} {m : McState} + (_ : JStage3TA st AR Z' H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7LA st (J7R2A st ar ipa)) s a σ m) + (_ : SchedValidA σ) (_ : VcpuStateSnapA Z'.V (L.mem s) σ) (_ : VcSym1A st Z'.T s) (_ : VcSym2A st Z'.V s) + (_ : ∀ v ∈ Z'.V, L.mem s v ≠ 0#u64 → L.mem s (wa (L.mem s v) 3664#u64) = v) + (_ : ∀ u ∈ Z'.T, L.mem s (runVcpuA u) ≠ 0#u64 → L.mem s (wa (L.mem s (runVcpuA u)) 24#u64) = u) + (_ : ∀ b, VcW Z'.V b → ¬ InOS OS b) (_ : FrOff st Z'.Cs OS (L.ksb s) s) (_ : UserFrSpecA st OS s σ) (_ : FrThA st Z'.th OS s) (_ : ∀ bp ∈ Bf, bp.2 = specPtr bp.1) + (_ : Clock a σ.machineState) + (_ : ∀ s' a' ms (u : U64), Clock a' ms → vspace.set_vm_root st ar s' a' u ⦃ r => Clock r.2 ms ⦄) + (_ : ∀ a' ms (vc : U64), Clock a' ms → ar.vcpu_flush a' vc ⦃ a'' => Clock a'' ms ⦄) + (_ : ∀ a' ms (vc om : U64), Clock a' ms → ar.vcpu_flush_state a' vc om ⦃ a'' => Clock a'' ms ⦄) + (_ : ∀ a' ms ms', Clock a' ms → ms'.timeState = ms.timeState → + ms'.lastMachineTime = ms.lastMachineTime → Clock a' ms') + (_ : VcpuPlatAgreeA ar) (_ : VcpuArchKeepsA ar AR) (_ : VcpuVmRootA st ar AR) + (_ : ∀ c ∈ Z'.Cs, c.val < fl4A64Platform.numNodes) (_ : fl4A64Platform.numNodes = 1) + {t : U64} (_ : t ∈ Z'.T), + vcpu.detach st ar s a t ⦃ res => + ∃ (Q' : U64 → U64 → List U64) (R' : U64 → List U64) (hw' : HwRootA) (σ' : State Unit), + ((), σ') ∈ (fl4VcpuDetach (specPtr t) σ).1 ∧ + JStage3TA st AR { Z' with Q := Q', R := R', hw := hw' } H OS U Ac Pp Tb Lv ia ie + N E Rp Bf stack κ n (J5A st) (J7LA st (J7R2A st ar ipa)) res.1 res.2 σ' m ∧ + SchedValidA σ' ∧ + VcpuStateSnapA Z'.V (L.mem res.1) σ' ∧ + VcSym1A st Z'.T res.1 ∧ VcSym2A st Z'.V res.1 ∧ + (∀ v ∈ Z'.V, L.mem res.1 v ≠ 0#u64 → L.mem res.1 (wa (L.mem res.1 v) 3664#u64) = v) ∧ + (∀ u ∈ Z'.T, L.mem res.1 (runVcpuA u) ≠ 0#u64 → + L.mem res.1 (wa (L.mem res.1 (runVcpuA u)) 24#u64) = u) ∧ + UserFrSpecA st OS res.1 σ' ∧ + Clock res.2 σ'.machineState ∧ + σ'.machineState.timeState = σ.machineState.timeState ∧ + σ'.machineState.lastMachineTime = σ.machineState.lastMachineTime ∧ + L.mem res.1 (wa t 3664#u64) = 0#u64 ∧ + L.mem res.1 (runVcpuA t) = 0#u64 ∧ + (L.mem s (wa t 3664#u64) ≠ 0#u64 → + L.mem res.1 (L.mem s (wa t 3664#u64)) = 0#u64 ∧ + L.mem res.1 (wa (L.mem s (wa t 3664#u64)) 24#u64) = 0#u64) ∧ + L.ksb res.1 = L.ksb s ∧ L.cpu res.1 = L.cpu s ∧ OSKeep st OS s res.1 ∧ + (∀ y ∈ OS, capA st res.1 y = capA st s y) ∧ + L.mem res.1 (boundA t) = L.mem s (boundA t) ∧ + L.mem res.1 (scA t) = L.mem s (scA t) ∧ + L.mem res.1 (stA t) = L.mem s (stA t) ∧ + (∀ x, σ.kheap (specPtr t) = some (.tcb x) → + ∃ x', σ'.kheap (specPtr t) = some (.tcb x') ∧ + x'.tcbArch.tcbVcpu = none ∧ x'.tcbExt.tcbRunVcpu = none ∧ + x'.tcbBoundNotification = x.tcbBoundNotification ∧ + x'.tcbSchedContext = x.tcbSchedContext ∧ + x'.tcbState = x.tcbState) ∧ + FrKeepA st OS s σ res.1 σ' ⦄) + (hComm : ∀ (τ τ1 τ2 : State Unit) (t v : ObjRef), + (∃ xt, τ.kheap t = some (.tcb xt) ∧ xt.tcbArch.tcbVcpu = some v) → + (∃ xv, τ.kheap v = some (.archObj (.vcpu xv)) ∧ xv.vcpuTcb = some t) → + ((), τ1) ∈ (dissociateVcpuTcb (Z := Unit) v t τ).1 → + ((), τ2) ∈ (((do unbindNotification t; unbindFromSc t; suspend t) : SMonad Unit Unit) τ1).1 → + ((), τ2) ∈ (((do unbindNotification t; unbindFromSc t; suspend t; dissociateVcpuTcb v t) : + SMonad Unit Unit) τ).1) + (hKeepB : ∀ (σ σc σe : State Unit) (t v : ObjRef) (xc : Fl4SpecA64.Tcb), + (∃ xv, σ.kheap v = some (.archObj (.vcpu xv)) ∧ xv.vcpuTcb = some t) → + ((), σc) ∈ (fl4CancelRun (Z := Unit) t σ).1 → σc.kheap t = some (.tcb xc) → xc.tcbArch.tcbVcpu = some v → + ((), σe) ∈ (fl4VcpuEndRun (Z := Unit) v σc).1 → + (∃ xt, σe.kheap t = some (.tcb xt) ∧ xt.tcbArch.tcbVcpu = some v) ∧ + (∃ xv, σe.kheap v = some (.archObj (.vcpu xv)) ∧ xv.vcpuTcb = some t)) : + FinHookKA st .thread ar Z.th OS fl4FinaliseCap + (fun s a σ G => (DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) + (J7LA st (J7R2A st ar ipa))) s a σ G ∧ DelPpTimeA Clock a σ) ∧ SchedValidA σ) + (fun s a σ G => DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) + (J7LA st J7R2tA)) s a σ G ∧ DelPpTimeA Clock a σ) + (DelRdXA st ar Z H OS ipa Rv) (DelRzA st) := by + intro s a σ G sl c ac f D hW hsl hc hk hg hac hf + obtain ⟨t, rfl⟩ : ∃ t, c = .Thread t := by + cases c <;> simp [finKindA] at hk + exact ⟨_, rfl⟩ + have eac : ac = .threadCap (specPtr t) := (show absCapA (.Thread t) = ac from hac).symm + obtain ⟨⟨hW0, hTime⟩, hSVσ⟩ := hW + obtain ⟨hcore, ⟨Q, R, hw, Ac, Bf, stack, J, X⟩, hPA, hZT, hBH, hCT, hFx⟩ := hW0 + have hOSx : cslotAddrA sl ∈ OS := D.mem_slot hsl + have unread : ¬ S1aR2Reads (capability.Cap.Thread t) := fun h => h + rw [fin_cap_eq_obj (by simp)] + cases f + · have e : objects.finalise_cap_object st ar s a (.Thread t) false = + ok ((capability.Cap.Null, capability.Cap.Null), s, a) := rfl + have hnoop : ((Fl4SpecA64.Cap.nullCap, Fl4SpecA64.Cap.nullCap), σ) ∈ + (fl4FinaliseCap (Z := Unit) ac false σ).1 := by + have skip : ∀ f : SMonad Unit Unit, ((), σ) ∈ (NonDet.when (false = true) f σ).1 := fun f => by + unfold NonDet.when + rw [if_neg (by simp)] + exact rfl + rw [eac] + simp only [fl4FinaliseCap, finaliseCap] + exact mem_bindA.2 ⟨((), σ), skip _, mem_bindA.2 ⟨((), σ), skip _, mem_bindA.2 ⟨((), σ), skip _, + mem_bindA.2 ⟨((), σ), skip _, mem_bindA.2 ⟨((), σ), skip _, rfl⟩⟩⟩⟩⟩ + rw [e, WP.spec_ok] + refine ⟨rfl, trivial, trivial, σ, G, ⟨false, hf trivial, hnoop⟩, D, hc, fun y _ _ => Or.inl rfl, ?_, ?_, + fun _ _ _ h => by cases h⟩ + · exact ⟨fun h => absurd (Or.inl rfl) h, fun _ => ⟨⟨hcore, ⟨Q, R, hw, Ac, Bf, stack, + J.mapJ7A fun _ _ => trivial, X⟩, hPA, hZT, hBH, hCT, hFx⟩, hTime⟩⟩ + · exact fun _ => ⟨⟨delRdA_other (fun r g h => by rw [hc] at h; cases h) (fun r g h => by rw [hc] at h; cases h) + (fun v h => by rw [hc] at h; cases h), rdThA_of_nonfinalA D.toFinStA hsl fun t' e h => by + rw [hc] at e; cases e + have e' : absCapA (.Thread t) = ac := hac + rw [e'] at h + exact absurd ((hf trivial).2 h) (by simp)⟩, m, J.j7.pinUnreadA (by rw [hc]; exact unread)⟩ + · -- final + have hf := hf trivial + -- the thread is a TCB of the record, and not an idle thread + have hslot : IsSlotA Z.th (cslotAddrA sl) := by + by_contra h + have := D.nul _ hOSx h + rw [hc] at this + cases this + have ht : t ∈ Z.T := by + have h0 := D.cv _ hOSx + rw [hc] at h0 + exact (J.j.1.thT t).1 h0 + have hcap : specCapAtA σ sl = some (.threadCap (specPtr t)) := by + rw [← eac]; exact (getCap_outA (c := ac) (σ' := σ) (by rw [hg]; exact rfl)).2 + have hidles : ∀ c < n, m.idleThreadOf c ≠ specPtr t := fun c hc' e => + (J.sidle.f6 sl).1 c hc' (e ▸ hcap) + have hCl := hTime.2.1 + have hSym1 := X.vcSym1 + have hDetN := hDetS J hSVσ X.vcSnap X.vcSym1 X.vcSym2 X.vcBack X.runBack X.vcOS X.frOff X.userFr X.frTh X.bfSpec hCl + (fun s' a' ms u h => Da.clockVm s' a' ms u h) Da.clockVcpuFlush Da.clockVcpuFlushState Da.clockFr hP hAK hVR + hCs h1 ht + have hDet : vcpu.detach st ar s a t ⦃ res => + ∃ (Q' : U64 → U64 → List U64) (R' : U64 → List U64) (hw' : HwRootA) (σ' : State Unit), + ((), σ') ∈ (fl4VcpuDetach (specPtr t) σ).1 ∧ + JStage3TA st AR { ({ Z with G := G, Q := Q, R := R, hw := hw } : XSetsA) with Q := Q', R := R', hw := hw' } + H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7LA st (J7R2A st ar ipa)) res.1 res.2 σ' m ∧ + SchedValidA σ' ∧ + VcSym1A st Z.T res.1 ∧ VcSym2A st Z.V res.1 ∧ + Clock res.2 σ'.machineState ∧ + L.mem res.1 (wa t 3664#u64) = 0#u64 ∧ + L.mem res.1 (runVcpuA t) = 0#u64 ∧ + (L.mem s (wa t 3664#u64) ≠ 0#u64 → + L.mem res.1 (L.mem s (wa t 3664#u64)) = 0#u64 ∧ + L.mem res.1 (wa (L.mem s (wa t 3664#u64)) 24#u64) = 0#u64) ∧ + L.ksb res.1 = L.ksb s ∧ L.cpu res.1 = L.cpu s ∧ + (∀ y ∈ OS, capA st res.1 y = capA st s y) ∧ + L.mem res.1 (boundA t) = L.mem s (boundA t) ∧ + L.mem res.1 (scA t) = L.mem s (scA t) ∧ + L.mem res.1 (stA t) = L.mem s (stA t) ∧ + (∀ x, σ.kheap (specPtr t) = some (.tcb x) → + ∃ x', σ'.kheap (specPtr t) = some (.tcb x') ∧ + x'.tcbArch.tcbVcpu = none ∧ x'.tcbExt.tcbRunVcpu = none ∧ + x'.tcbBoundNotification = x.tcbBoundNotification ∧ + x'.tcbSchedContext = x.tcbSchedContext ∧ + x'.tcbState = x.tcbState) ∧ + Std6GA st Ag { Z with G := G, Q := Q, R := R, hw := hw } H OS Bf res.1 σ' ⦄ := WP.spec_mono hDetN (by + rintro res ⟨Q', R', hw', σ', m1, J1, sv, snap, v1, v2, vb, rb, uf, cl, -, -, c1, c2, c3, kb, cp, k, caps, bd, sc, sta, sp, fk⟩ + exact ⟨Q', R', hw', σ', m1, J1, sv, v1, v2, cl, c1, c2, c3, kb, cp, caps, bd, sc, sta, sp, + { utWin := X.utWin.keepA fun u hu => k.capA hu + devWin := X.devWin.keepA fun u hu => k.capA hu + vcBack := vb, runBack := rb, vcSnap := snap, vcOS := X.vcOS, userFr := uf, bfSpec := X.bfSpec, + frTh := X.frTh.keepA (fun u hu => k.capA hu) fun _ _ e => e + vcSym1 := v1, vcSym2 := v2, heapWin := X.heapWin, coresAll := X.coresAll + zwf := fun y hy p zt c e => X.zwf y hy p zt c ((k.capA (D.sub y hy)).symm.trans e) + frOff := FrOff.ks kb (CapInv.keepO X.frOff k) + frAg := FrAgA.keepP X.frAg fk }⟩) + have hOskD : vcpu.detach st ar s a t ⦃ r => OSKeep st OS s r.1 ⦄ := WP.spec_mono hDetN (by + rintro res ⟨_, _, _, _, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -, k, -⟩ + exact k) + have prog : objects.finalise_cap_object st ar s a (.Thread t) true = + Std.bind (do + let (s1, a1) ← smp.remote_tcb_stall st ar s a t + let (s2, a2) ← vcpu.detach st ar s1 a1 t + let s3 ← notification.unbind_notification st s2 t + ok (s3, a2)) (fun p => + Std.bind (do + let s4 ← timer.remove st p.1 t + let r ← (do + let i ← obj.Tcb.sc st t s4 + let b ← obj.Sc.is_null i + if b then ok (s4, p.2) else do + let s6 ← schedcontext.unbind_tcb st s4 i + let i1 ← obj.Sc.yield_from st i s6 + let b1 ← obj.Tcb.is_null i1 + if b1 then ok (s6, p.2) else schedcontext.complete_yield_to st ar s6 p.2 i1) + sched.suspend st ar r.1 r.2 t) (fun q => + Std.bind (objects.debug_remove st q.1 t) (fun s7 => + Std.bind (ar.fpu_release q.2 t) (fun a5 => + ok ((capability.Cap.Zombie t capability.ZOMBIE_TCB layout.tcb.NUM_SLOTS, capability.Cap.Null), s7, a5))))) := by + have bc : ∀ {α β : Type} (x : Result α) (f g : α → Result β), (∀ y, f y = g y) → + Std.bind x f = Std.bind x g := fun x f g h => by rw [funext h] + unfold objects.finalise_cap_object + simp only [↓reduceIte, Bind.bind] + repeat' first + | rfl + | refine bc _ _ _ (fun ⟨_, _⟩ => ?_) + | refine bc _ _ _ (fun _ => ?_) + | simp only [thrBindAssocA, bind_ok, tcb_slot0A, WP.uncurry'_pair, uncurry_apply_pair] + rw [prog] + have hFront := Vcpu.thrFront_recA J hCs h1 ht hDet + refine WP.spec_bind (WP.spec_and hFront (show (do + let (s1, a1) ← smp.remote_tcb_stall st ar s a t + let (s2, a2) ← vcpu.detach st ar s1 a1 t + let s3 ← notification.unbind_notification st s2 t + ok (s3, a2)) ⦃ p => OSKeep st OS s p.1 ⦄ by + rw [J.stall_singleA hCs h1 ht, bind_ok] + refine WP.spec_bind (WP.spec_and hDet hOskD) ?_ + rintro ⟨s2, a2⟩ ⟨⟨Q', R', hw', σ1, -, J1, -⟩, k1⟩ + refine WP.spec_bind (J1.thrUnbind_recA (t := t) ht) ?_ + rintro s3 ⟨σ2, -, -, -, -, k2, -⟩ + rw [WP.spec_ok] + exact OSKeep.transA k1 k2)) ?_ + rintro ⟨s3, a2⟩ ⟨⟨Q2, R2, hw2, σ1, σ2, m1, sp2, J2, -, cl2, ems, kb2, cpu2, caps2, v0, rv0, -, -, k62⟩, osk2⟩ + dsimp only at m1 sp2 J2 cl2 ems kb2 cpu2 caps2 v0 rv0 osk2 ⊢ + have hti : specPtr t ≠ σ2.idleThread := fun e => by + refine hidles m.curCore J.sinv.mc.curCore_lt ?_ + rw [e, J2.sr.sch.idle, J2.sr.sch.core, J2.sr.sch.idleOf _ J2.sr.sch.cpuCs] + rfl + have hQ7 : ∀ Q' Rl', J7LA st (J7R2A st ar ipa) { ({ ({ Z with G := G, Q := Q, R := R, hw := hw } : XSetsA) + with Q := Q2, R := R2, hw := hw2 } : XSetsA) with Q := Q', R := Rl' } = + J7LA st (J7R2A st ar ipa) { ({ Z with G := G, Q := Q, R := R, hw := hw } : XSetsA) + with Q := Q2, R := R2, hw := hw2 } := fun _ _ => rfl + refine WP.spec_bind (J2.thrMid_recA hQ7 hA hCs h1 hmb hmax htu ht hti hidles rv0) ?_ + rintro ⟨s6, a4⟩ ⟨ea, σ5, σ6, Q6, R6, Ac6, stack6, m5, F5, sp6, J6, osk6, kb6, cpu6, -, hy6, vk6, k66⟩ + dsimp only at ea J6 osk6 kb6 cpu6 vk6 ⊢ + subst a4 + refine WP.spec_bind (J6.debugRm_recA ht) ?_ + rintro s7 ⟨J7', w7, osk7, tk7⟩ + refine WP.spec_bind (WP.spec_and (Da.fpuRelease a2 _ t J7'.j.1.ar) (Da.clockFpu a2 _ t cl2)) ?_ + rintro a5 ⟨har5, hcl5⟩ + rw [WP.spec_ok] + have z6 : L.mem s6 (wa t 3664#u64) = 0#u64 := (vk6 t ht).trans v0 + have z7 : L.mem s7 (wa t 3664#u64) = 0#u64 := + (tk7 t ht 3664#u64 (by simp) (by simp) (Or.inr (by simp))).trans z6 + -- no run link after the SC step, so `fl4CancelRun` there is a no-op + obtain ⟨x2, hx2, -, hr2⟩ := J2.j.2.1.tcb t ht + obtain ⟨x5, hx5, k5⟩ := F5.1.tcb hx2 + have r5 : x5.tcbExt.tcbRunVcpu = none := by + rw [k5.2.2, hr2, rv0]; simp [optRefA] + have hsus : ((), σ6) ∈ (Fl4SpecA64.suspend (Z := Unit) (specPtr t) σ5).1 := by + have h0 : ((), σ6) ∈ ((fl4CancelRun (Z := Unit) (specPtr t) >>= fun _ => Fl4SpecA64.suspend (specPtr t)) σ5).1 := by + rw [sp6]; exact rfl + rwa [Fl4ProofsA64.P3.Notif.nd_bind_one (fl4CancelRun_quietA hx5 r5)] at h0 + obtain ⟨y6, hk6, -⟩ := hy6 + obtain ⟨σ7, hfpu, F7⟩ := fpuRelease_memA hk6 + have J8 := J7'.fpu_recA F7 har5 J7'.j7 + have X6 := k66 _ rfl D.sub ((k62 _ rfl D.sub).ghostA G Q2 R2 hw2) + have X7 := Std6GA.schedStepA J6 (X6.ghostA G Q6 R6 hw2) D.sub w7 osk7 ⟨SpecSchedA.refl _, SSzA.refl _, rfl⟩ + have X8 := Std6GA.keepA X7 J7'.vcRun D.sub (OSKeep.reflA _) rfl (fun _ _ _ _ _ => rfl) (fun _ _ => rfl) + (fun _ _ => rfl) (fun _ _ _ e => F7.back e (fun _ c => by cases c)) + (fun _ _ _ e => F7.get e (fun _ c => by cases c)) + (fun h => FrAgA.stepA h (fun _ x => x) (fun _ _ _ => rfl) F7.um) + have osk : OSKeep st OS s s7 := OSKeep.transA osk2 (OSKeep.transA osk6 osk7) + have caps : ∀ y ∈ OS, capA st s7 y = capA st s y := fun y hy => osk.capA hy + have kb : L.ksb s7 = L.ksb s := w7.1.trans (kb6.trans kb2) + have hmem : ((Fl4SpecA64.Cap.zombie (specPtr t) none fl4TcbSlotCount, Fl4SpecA64.Cap.nullCap), σ7) ∈ + (fl4FinaliseCap (Z := Unit) ac true σ).1 := by + rw [eac] + have hLinkσ : ∀ v σc σe xc, ((), σc) ∈ (fl4CancelRun (Z := Unit) (specPtr t) σ).1 → + σc.kheap (specPtr t) = some (.tcb xc) → xc.tcbArch.tcbVcpu = some v → + ((), σe) ∈ (fl4VcpuEndRun (Z := Unit) v σc).1 → + (∃ xt, σe.kheap (specPtr t) = some (.tcb xt) ∧ xt.tcbArch.tcbVcpu = some v) ∧ + ∃ xv, σe.kheap v = some (.archObj (.vcpu xv)) ∧ xv.vcpuTcb = some (specPtr t) := by + intro v σc σe xc hc1 hk1 hv1 he1 + refine hKeepB _ _ _ _ _ _ ?_ hc1 hk1 hv1 he1 + -- the record's back link: `t`'s VCPU names `t` + obtain ⟨x0, hx0, hb0, -⟩ := J.j.2.1.tcb t ht + obtain ⟨x', hx', ea⟩ := cancelRun_archA hx0 hc1 + rw [hk1] at hx' + cases hx' + rw [ea, hb0] at hv1 + have hne : L.mem s (wa t 3664#u64) ≠ 0#u64 := fun e => by rw [e] at hv1; simp [optRefA] at hv1 + have ev : v = specPtr (L.mem s (wa t 3664#u64)) := by + rw [show optRefA (L.mem s (wa t 3664#u64)) = some (specPtr (L.mem s (wa t 3664#u64))) by + simp [optRefA, hne]] at hv1 + exact (Option.some.inj hv1).symm + have hV : L.mem s (wa t 3664#u64) ∈ Z.V := (J.vcRun.tcb t ht).resolve_left hne + obtain ⟨y, hy, hyt, -⟩ := J.j.2.1.obj _ hV + have e0 : L.mem s (L.mem s (wa t 3664#u64)) = t := hSym1 t ht hne + refine ⟨y, ev ▸ hy, ?_⟩ + have ht0 : t ≠ 0#u64 := J.sr.place.nz t _ ((J.sr.thT t).2 ht) + rw [hyt, e0] + simp [optRefA, ht0] + refine thrSpecA m1 sp2 m5 hsus hfpu (fun σc xc _ _ _ => ?_) hLinkσ hComm + obtain ⟨x6, hx6, hv6, -⟩ := J6.j.2.1.tcb t ht + exact ⟨x6, hx6, by rw [hv6, z6]; simp [optRefA]⟩ + have D' : DStateA st Z.th OS s7 σ7 G := DStateA.keepOSA (Z := { Z with G := G, Q := Q6, R := R6, hw := hw2 }) D J8 osk + have eS : capability.Cap.Zombie t capability.ZOMBIE_TCB layout.tcb.NUM_SLOTS = + capability.Cap.Zombie t capability.ZOMBIE_TCB 7#u64 := by + unfold layout.tcb.NUM_SLOTS; rfl + have hTime' : DelPpTimeA Clock a5 σ7 := by + obtain ⟨e1, e2, e3, ek⟩ := finTimeSpec_threadA (.Thread t) ac true σ _ σ7 rfl hac hmem + obtain ⟨-, d2, d1, -⟩ := detach_tmA _ _ _ m1 + have em : σ2.machineState.timeState = σ.machineState.timeState ∧ + σ2.machineState.lastMachineTime = σ.machineState.lastMachineTime := by + rw [ems]; exact ⟨d1, d2⟩ + refine ⟨?_, Da.clockFr _ _ _ hcl5 (e3.trans em.1.symm) (e2.trans em.2.symm), ?_, + hTime.2.2.2.keepA ek (by rw [e1]; exact BitVec.le_refl _)⟩ + · have h0 := hTime.1 + unfold ValidMachineTimeA at h0 ⊢ + rw [e1, e2]; exact h0 + · unfold CounterStepA rawNextA + rw [e2, e3]; exact hTime.2.2.1 + have Zt : ZombieTcbNoVcpuA st OS s7 := J8.zombieNoVcpu + have hPA' : ParentAdjA st G.chains G.par s7 := ParentAdjA.keepOS D.lk osk hPA + have W' : DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) + (J7LA st (J7R2A st ar ipa))) s7 a5 σ7 G := + ⟨⟨kb.trans hcore.1, (w7.2.1.trans (cpu6.trans cpu2)).trans hcore.2⟩, ⟨Q6, R6, hw2, Ac6, Bf, stack6, J8, X8.ghostA Z.G Z.Q Z.R Z.hw⟩, hPA', Zt, + bufHold_sameA st hBH caps, ctOff_sameA st hCT kb caps, hFx.keepA st D.sub caps⟩ + have hrm : ¬ RmOKA (capability.Cap.Zombie t capability.ZOMBIE_TCB 7#u64) (cslotAddrA sl) := by + rintro (h | ⟨p, zt, k, e, h⟩) + · cases h + · cases e + rcases h with h | ⟨h, -⟩ <;> simp at h + rw [eS] + have hc' : capA st s7 (cslotAddrA sl) = .Thread t := (caps _ hOSx).trans hc + have ez : capability.ZOMBIE_TCB = 65#u64 := by unfold capability.ZOMBIE_TCB; rfl + have ea : absCapA (.Zombie t capability.ZOMBIE_TCB 7#u64) = + Fl4SpecA64.Cap.zombie (specPtr t) none fl4TcbSlotCount := by + rw [ez]; simp [absCapA, zombieBitsA, fl4TcbSlotCount] + have hmemA : ((absCapA (.Zombie t capability.ZOMBIE_TCB 7#u64), absCapA .Null), σ7) ∈ + (fl4FinaliseCap (Z := Unit) ac true σ).1 := by rw [ea]; exact hmem + refine ⟨rfl, trivial, trivial, σ7, G, ⟨true, hf, hmemA⟩, D', hc', + fun y hy _ => Or.inl (caps y hy), ⟨fun _ => ⟨⟨W', hTime'⟩, threadFin_schedValid (specPtr t) true σ ⟨hSVσ, rqDomPrio_of_schedA J.sr⟩ (_, σ7) (eac ▸ hmem)⟩, + fun h => absurd h hrm⟩, fun h => absurd h hrm, + fun _ _ _ _ t' e => ?_⟩ + rw [hc'] at e + cases e + exact z7 + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelThrScA.lean b/proofs/Fl4ProofsA64/P3/DelThrScA.lean new file mode 100644 index 000000000..594f01ffe --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelThrScA.lean @@ -0,0 +1,401 @@ +/- +P3 (AArch64, delete hooks W3, contract §77): **the Thread finaliser's SC unbinding at the record** (slice 2, step 2). +After `timer.remove`, `finalise_cap_object (.Thread t) true` reads `t`'s SC word. If it is nonzero, the program +runs `schedcontext.unbind_tcb`, reads the SC's yielder, and runs `complete_yield_to` when there is one. This refines +l4v's `unbind_from_sc t` (`schedContextUnbindTcb` of the bound SC, then `complete_yield_to` of its yielder), and +`JStage3TA` holds again. + +* `unbindTcb_stepA` / `unbindTcb_repA`: `sched_context_unbind_tcb` keeps the loop invariant and every SC's reply list + (the bodies of `unbindAllTcbs_stepA` / `unbindAllTcbs_repA` without the outer `when`). +* `unbindFromSc_memA`: the spec outcome of `unbind_from_sc` from its two steps. +* `JStage3TA.thrSc_recA`: the record after the fragment. The end state is an `ScEndGA` with no notification and no + reply touched, and `DelScRecA`'s `JStage3TA.rec_scEndA` rebuilds the record. + +Premises: [P] `hA`, `hCs`, `hmax`, `htu`; [S] `h1`. Caller hypotheses: `hti` (`t` is not the idle thread, from +`JStage3TA.notIdle_of_capA`) and `hQ7` (J7 does not read the ghost queues). +-/ +import Fl4ProofsA64.P3.DelScRecA +import Fl4ProofsA64.P3.DelThrTimerA +import Fl4ProofsA64.P3.ScUnbAllA +import Fl4ProofsA64.P1.QueueFinal +import Fl4ProofsA64.P1.QueueFinSc +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 Fl4ProofsA64.P3.Endp +open Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Notif + +local notation "wa" => core.num.U64.wrapping_add + +noncomputable section + +/-- **`sched_context_unbind_tcb`** of an SC other than `idle_sc_ptr` (`unbindAllTcbs_stepA`'s body). -/ +theorem unbindTcb_stepA {σ : State Unit} {m : McState} {n : Nat} (h : LoopInvA σ m n) {p t : ObjRef} + {x : SchedContext} {k : Nat} (hk : σ.kheap p = some (.schedContext x k)) (hp : p ≠ idleScPtr) + (ht : x.scTcb = some t) {r : Unit × State Unit} (hr : r ∈ (schedContextUnbindTcb (Z := Unit) p σ).1) : + RStepA m n σ r.2 := by + have hti : t ≠ σ.idleThread := h.boundNotIdle hk hp ht + unfold schedContextUnbindTcb at hr + rw [nd_bind_oneSc (Sched.getSchedContext_eqA hk), ht, + nd_bind_oneSc (show Fl4Spec.NonDet.assertOpt (σ := State Unit) (some t) σ = ({(t, σ)}, False) from rfl), + nd_gets_bindSc] at hr + obtain ⟨a, ha, hr⟩ := mem_bindA.1 hr + have A : RStepA m n σ a.2 := by + unfold Fl4Spec.NonDet.when at ha + split_ifs at ha + · exact RStepA.of_queue (h.reschedule ha) (rescheduleRequired_memA ha).1 (rescheduleRequired_memA ha).2 + · have : a = ((), σ) := ha + rw [this] + exact RStepA.refl h + obtain ⟨b, hb, hr⟩ := mem_bindA.1 hr + have B := A.trans (tcbSchedDeq_stepA t A.inv hb) + obtain ⟨c, hc', hr⟩ := mem_bindA.1 hr + have C := B.trans (tcbReleaseRemove_stepA t B.inv hc') + obtain ⟨d, hd, hr⟩ := mem_bindA.1 hr + have D : RStepA m n σ d.2 := by + unfold setTcbObjRef at hd + obtain ⟨z, hz, ez⟩ := threadSet_memA _ _ hd + rw [ez] + exact C.trans (RStepA.tcbSc C.inv hz (by rw [C.idle]; exact hti) none (fun h0 => by cases h0)) + unfold setScObjRef at hr + obtain ⟨y, j, hy, ey⟩ := updateSchedContext_memA _ _ hr + rw [ey] + exact D.trans (RStepA.updSc D.inv hy hp rfl rfl rfl (Or.inr (fun h => by cases h))) + +/-- `sched_context_unbind_tcb` keeps every SC's reply list (`unbindAllTcbs_repA`'s body). -/ +theorem unbindTcb_repA {σ : State Unit} {p t : ObjRef} {x : SchedContext} {k : Nat} + (hk : σ.kheap p = some (.schedContext x k)) (ht : x.scTcb = some t) {r : Unit × State Unit} + (hr : r ∈ (schedContextUnbindTcb (Z := Unit) p σ).1) : + ∀ q y n, σ.kheap q = some (.schedContext y n) → + ∃ y', r.2.kheap q = some (.schedContext y' n) ∧ y'.scReplies = y.scReplies := by + intro q z n hq + unfold schedContextUnbindTcb at hr + rw [nd_bind_oneSc (Sched.getSchedContext_eqA hk), ht, + nd_bind_oneSc (show Fl4Spec.NonDet.assertOpt (σ := State Unit) (some t) σ = ({(t, σ)}, False) from rfl), + nd_gets_bindSc] at hr + obtain ⟨a, ha, hr⟩ := mem_bindA.1 hr + have A : a.2.kheap = σ.kheap := by + unfold Fl4Spec.NonDet.when at ha + split_ifs at ha + · have e := (rescheduleRequired_memA ha).1.1 + rw [e] + · have : a = ((), σ) := ha + rw [this] + obtain ⟨b, hb, hr⟩ := mem_bindA.1 hr + have B : b.2.kheap = σ.kheap := (tcbSchedDeq_kheapA t hb).trans A + obtain ⟨c, hc', hr⟩ := mem_bindA.1 hr + have C : c.2.kheap = σ.kheap := (tcbReleaseRemove_kheapA t hc').trans B + obtain ⟨d, hd, hr⟩ := mem_bindA.1 hr + unfold setTcbObjRef at hd + obtain ⟨zt, hzt, ez⟩ := threadSet_memA _ _ hd + unfold setScObjRef at hr + obtain ⟨y, j, hy, ey⟩ := updateSchedContext_memA _ _ hr + have D : d.2.kheap q = some (.schedContext z n) := by + have ne : q ≠ t := fun e => by rw [e, ← C, hzt] at hq; cases hq + rw [ez] + change Function.update c.2.kheap t _ q = _ + rw [Function.update_of_ne ne, C]; exact hq + rw [ey] + by_cases e : q = p + · subst e + rw [D] at hy; cases hy + exact ⟨scTcbUpdate (fun _ => none) z, by change Function.update d.2.kheap q _ q = _; rw [Function.update_self], rfl⟩ + · exact ⟨z, by change Function.update d.2.kheap p _ q = _; rw [Function.update_of_ne e]; exact D, rfl⟩ + +/-- `unbind_from_sc t` from its two steps, for a thread bound to `P`. -/ +theorem unbindFromSc_memA {σ σ1 σ' : State Unit} {t P : ObjRef} {x : Tcb} {y : SchedContext} {k : Nat} + (hx : σ.kheap t = some (.tcb x)) (hs : x.tcbSchedContext = some P) + (h1 : ((), σ1) ∈ (schedContextUnbindTcb (Z := Unit) P σ).1) + (hk : σ1.kheap P = some (.schedContext y k)) + (h2 : ((), σ') ∈ (NonDet.maybeM (completeYieldTo (Z := Unit)) y.scYieldFrom σ1).1) : + ((), σ') ∈ (unbindFromSc (Z := Unit) t σ).1 := by + have e1 : getTcbObjRef (Z := Unit) Tcb.tcbSchedContext t σ = ({(some P, σ)}, False) := by + unfold getTcbObjRef + rw [threadGet_eqA _ hx, hs] + unfold unbindFromSc + rw [nd_bind_oneSc e1] + refine mem_bindA.2 ⟨((), σ1), h1, ?_⟩ + show ((), σ') ∈ ((getSchedContext (Z := Unit) P >>= fun sc => + NonDet.maybeM (completeYieldTo (Z := Unit)) sc.scYieldFrom) σ1).1 + rw [nd_bind_oneSc (Sched.getSchedContext_eqA hk)] + exact h2 + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J7 : JSlotA S} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +set_option maxHeartbeats 3200000 in +/-- **The Thread finaliser's SC unbinding at the record** (module docstring). -/ +theorem JStage3TA.thrSc_recA {ar : machine.Arch A} [J7PpA st J7] [J7PpKA st J7] + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + (hQ7 : ∀ Q Rl, J7 { Z with Q := Q, R := Rl } = J7 Z) + (hA : P0.ArchTotal ar) (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (h1 : fl4A64Platform.numNodes = 1) (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {t : U64} (ht : t ∈ Z.T) (hti : specPtr t ≠ σ.idleThread) : + (do + let i ← obj.Tcb.sc st t s + let b ← obj.Sc.is_null i + if b then ok (s, a) else do + let s6 ← schedcontext.unbind_tcb st s i + let i1 ← obj.Sc.yield_from st i s6 + let b1 ← obj.Tcb.is_null i1 + if b1 then ok (s6, a) else schedcontext.complete_yield_to st ar s6 a i1) ⦃ r => + r.2 = a ∧ ∃ (σ' : Fl4SpecA64.State Unit) (Q : U64 → U64 → List U64) (R : U64 → List U64) (Ac' : Set U64) + (yf : U64) (d : Fl4SpecA64.Data), + ((), σ') ∈ (unbindFromSc (Z := Unit) (specPtr t) σ).1 ∧ + JStage3TA st AR { Z with Q := Q, R := R } H OS U Ac' Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 + r.1 a σ' m ∧ + WritesIn st s r.1 (SchedStepNWA (st := st) N E Rp Z m σ s) ∧ OSKeep st OS s r.1 ∧ SchedFrameA σ σ' ∧ + σ'.idleThread = σ.idleThread ∧ ScEndGA st Z N Rp m s r.1 σ σ' 0#u64 0#u64 yf d ∧ + (∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → + (K.val ≠ 256 ∧ K.val ≠ 264 ∧ K.val ≠ 272 ∧ K.val ≠ 3464 ∧ K.val ≠ 3472 ∧ K.val ≠ 3480 ∧ K.val ≠ 3488 ∧ + K.val ≠ 3592) → L.mem r.1 (wa u K) = L.mem s (wa u K)) ⦄ := by + have htH : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + have L0 : LoopInvA σ m n := ⟨J.sinv, J.sidle, J.sact⟩ + have hst : StEqA Z.T (L.mem s) σ := by + intro u hu x hxt + obtain ⟨y, hy, rel⟩ := J.ep.tcb u hu + rw [hxt] at hy; cases hy + rw [← Notif.tsCode_eqA]; exact rel.st + obtain ⟨x, hx, hxs⟩ := J.sr.sch.tcb t ht + refine WP.spec_bind (obj.Tcb.sc.spec s t) ?_ + rintro c rfl + refine WP.spec_bind (obj.Sc.is_null.spec _) ?_ + rintro b rfl + by_cases h0 : L.mem s (scA t) = 0#u64 + · -- no SC: nothing happens + rw [if_pos (by simpa using h0), WP.spec_ok] + have hn : x.tcbSchedContext = none := by rw [hxs, h0]; unfold optScA; rw [if_pos rfl] + have e1 : getTcbObjRef (Z := Unit) Tcb.tcbSchedContext (specPtr t) σ = ({(none, σ)}, False) := by + unfold getTcbObjRef + rw [threadGet_eqA _ hx, hn] + have sp : ((), σ) ∈ (unbindFromSc (Z := Unit) (specPtr t) σ).1 := by + unfold unbindFromSc + rw [nd_bind_oneSc e1] + exact mem_bindA.2 ⟨((), σ), rfl, rfl⟩ + refine ⟨rfl, σ, Z.Q, Z.R, Ac, 0#u64, 0, sp, J, WritesIn.refl _ _, OSKeep.reflA _, + ⟨SpecSchedA.refl _, SSzA.refl _, rfl⟩, rfl, ?_, fun _ _ _ _ _ _ => rfl⟩ + exact ⟨WritesIn.refl _ _, fun c => absurd rfl c, fun c => absurd rfl c, fun c => absurd rfl c, + fun r x hx => ⟨x, x.tcbSchedContext, hx, fun c => absurd rfl c.1, fun _ => rfl⟩, fun _ _ h => h, + fun r x hx => ⟨x.ntfnSc, hx, fun c => absurd rfl c.1, fun _ => rfl⟩, + fun r x hx => ⟨x.replySc, hx, fun c => absurd rfl c.1, fun _ => rfl⟩, J.mcp, J.yieldRel⟩ + · -- `unbind_tcb`, then the yielder + rw [if_neg (by simpa using h0)] + obtain ⟨hcS, htw, -⟩ := J.blkA.2.2.1.2.2.bound t htH h0 + generalize hcdef : L.mem s (scA t) = c at h0 hcS htw hxs ⊢ + have hc : c ∈ Z.C := by rw [← J.scs]; exact hcS + have hc0 : c ≠ 0#u64 := h0 + have hon := J.onCore_single hCs h1 ht + obtain ⟨xs, ns, hk, hxr⟩ := J.sr.scr.sc c hc + have ht0 := (tcb_ne01Sc (J.sr.core.tcbs t ht)).1 + have hxsT : xs.scTcb = some (specPtr t) := by + rw [hxr.tcb, ← scTcbA_eqSc, htw, optRefS_ne ht0] + have hni' : scNameA m c ≠ idleScPtr := by + intro e + obtain ⟨-, y, k, -, -, hk2, hy⟩ := J.sinv.idleSc + rw [e] at hk + rw [hk] at hk2 + simp only [Option.some.injEq, KernelObject.schedContext.injEq] at hk2 + obtain ⟨rfl, -⟩ := hk2 + rw [hxsT] at hy + exact hti (Option.some.inj hy) + have hxsc : x.tcbSchedContext = some (scNameA m c) := by + rw [hxs]; unfold optScA; rw [if_neg h0, scNameA_eq] + have hna : L.mem s (scTcbA c) ≠ 0#u64 → L.actionW s ≠ L.mem s (scTcbA c) := by + intro _ e + rw [htw] at e + have h01 := tcb_ne01Sc (J.sr.core.tcbs t ht) + refine J.noSwitch (specPtr t) ?_ + rw [J.sr.sch.act, e] + unfold actOfA + rw [if_neg h01.1, if_neg h01.2] + have hidle : L.mem s (scTcbA c) ≠ 0#u64 → L.idleW s ≠ L.mem s (scTcbA c) := by + intro _ e + rw [htw] at e + exact hti (by rw [J.sr.sch.idle, e]) + refine WP.spec_bind (WP.spec_and (sc_unbind_tcb_localA J.sr hst hc hc0 ht htw hcdef hon) + (Fl4ProofsA64.P1.schedcontext.unbind_tcb.blk (st := st) s c J.blkA hcS hc0 hna hidle)) ?_ + rintro s1 ⟨⟨σ1, Q1, R1, sp1, H1, F1⟩, blk1, -⟩ + have m1 : ((), σ1) ∈ (schedContextUnbindTcb (Z := Unit) (scNameA m c) σ).1 := by rw [sp1]; exact rfl + have S1 := unbindTcb_stepA L0 hk hni' hxsT m1 + have Rp1 := unbindTcb_repA hk hxsT m1 + -- the relations of the yield step at `s1` + have words : ∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → + (K.val ≠ 3464 ∧ K.val ≠ 3480 ∧ K.val ≠ 3488 ∧ K.val ≠ 3592) → L.mem s1 (wa u K) = L.mem s (wa u K) := + fun u hu K hK h8 hne => F1.wr.mem_eq (field_aligned (J.sr.core.tcbs u hu) hK h8) (J.tcbWord_offMA hu hK hne) + have hst1 : StEqA Z.T (L.mem s1) σ1 := by + intro u hu z hz + obtain ⟨z0, hz0, -⟩ := J.sr.sch.tcb u hu + obtain ⟨y, hy, o, rfl⟩ := F1.keep.tcb _ _ hz0 + rw [hz] at hy; cases hy + have e : stA u = wa u 3328#u64 := by rw [stA_eq] + rw [e, words u hu 3328#u64 (by simp) (by simp) (by simp), ← e] + exact hst u hu z0 hz0 + obtain ⟨M1, Y1⟩ := yieldFrameA (mem' := L.mem s1) (σ' := σ1) J.mcp J.yieldRel + (fun u hu => ⟨words u hu 3440#u64 (by simp) (by simp) (by simp), words u hu 3456#u64 (by simp) (by simp) (by simp)⟩) + (fun u hu => words u hu 3472#u64 (by simp) (by simp) (by simp)) + (fun u _ z hz => by + obtain ⟨y, hy, o, rfl⟩ := F1.keep.tcb _ _ hz + exact ⟨_, hy, rfl, rfl, rfl⟩) + (fun c hc => by + obtain ⟨z, k, hz, -⟩ := J.sr.scr.sc c hc + obtain ⟨y, hy, e, -⟩ := S1.sc _ _ _ hz + exact ⟨z, k, y, hz, hy, e⟩) + obtain ⟨x1, n1, hk1, hx1⟩ := H1.scr.sc c hc + have hyw : x1.scYieldFrom = optRefS (L.mem s1 (scYieldFromA c)) := by + have e : scYieldFromA c = scWA c 56#u64 := by unfold scYieldFromA scWA layout.sc.YIELD_FROM; rfl + rw [e]; exact hx1.yieldFrom + have F1' : SchedFrameA σ σ1 := + (inferInstance : SPresInst (schedContextUnbindTcb (Z := Unit) (scNameA m c))).pres σ ((), σ1) m1 + -- the TCB and object changes of `unbind_tcb` + have tcb1 : ∀ r z, σ.kheap r = some (.tcb z) → ∃ o, σ1.kheap r = some (.tcb { z with tcbSchedContext := o }) := + fun r z h => by + obtain ⟨y, hy, o, rfl⟩ := F1.keep.tcb r z h + exact ⟨o, hy⟩ + have obj1 : ∀ r o, σ.kheap r = some o → (∀ y n, o ≠ .schedContext y n) → (∀ x, o ≠ .tcb x) → + σ1.kheap r = some o := F1.keep.obj + refine WP.spec_bind (obj.Sc.yield_from.val s1 c) ?_ + rintro yf rfl + refine WP.spec_bind (obj.Tcb.is_null.spec _) ?_ + rintro bb rfl + by_cases hy0 : L.mem s1 (scYieldFromA c) = 0#u64 + · -- no yielder + rw [if_pos (by simpa using hy0), WP.spec_ok] + have hn : x1.scYieldFrom = none := by rw [hyw, hy0, optRefS_zero] + have mem : ((), σ1) ∈ (unbindFromSc (Z := Unit) (specPtr t) σ).1 := + unbindFromSc_memA hx hxsc m1 hk1 (by rw [hn]; exact rfl) + have END : ScEndGA st Z N Rp m s s1 σ σ1 0#u64 0#u64 0#u64 0 := + ⟨F1.wr.mono fun _ h => Or.inl h, fun c => absurd rfl c, fun c => absurd rfl c, fun c => absurd rfl c, + fun r z h => by + obtain ⟨o, ho⟩ := tcb1 r z h + exact ⟨_, o, ho, fun c => absurd rfl c.1, fun _ => rfl⟩, + fun r z h => obj1 r _ h (fun _ _ e => by cases e) (fun _ e => by cases e), + fun r z h => ⟨z.ntfnSc, obj1 r _ h (fun _ _ e => by cases e) (fun _ e => by cases e), + fun c => absurd rfl c.1, fun _ => rfl⟩, + fun r z h => ⟨z.replySc, obj1 r _ h (fun _ _ e => by cases e) (fun _ e => by cases e), + fun c => absurd rfl c.1, fun _ => rfl⟩, M1, Y1⟩ + have reply : ReplyStackRelA (L.mem s1) Rp Z.C σ1 (scNameA m) stack := by + refine J.reply.frame (fun r hr => ?_) (fun c _ z k hz => ?_) (fun r hr z hz => ?_) + · obtain ⟨r32, -, rfit⟩ := J.iplaced.e.rep r hr + have one : ∀ o : U64, o.val < 32 → o.val % 8 = 0 → L.mem s1 (wa r o) = L.mem s (wa r o) := by + intro o o1 o2 + have v : (wa r o).val = r.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + refine F1.wr.mem_eq ?_ (J.ipc_offMA (Or.inr (Or.inr ⟨r, hr, by omega, by omega⟩))) + show (wa r o).val % 8 = 0 + omega + have n16 : (layout.reply.NEXT).val = 16 := by unfold layout.reply.NEXT; rfl + have p8 : (layout.reply.PREV).val = 8 := by unfold layout.reply.PREV; rfl + exact ⟨one _ (by omega) (by omega), one _ (by omega) (by omega)⟩ + · obtain ⟨y, hy, e⟩ := Rp1 _ _ _ hz + exact ⟨y, k, hy, e⟩ + · obtain ⟨z0, hz0, -⟩ := J.ep.reply r hr + have := obj1 _ _ hz0 (fun _ _ e => by cases e) (fun _ e => by cases e) + rw [hz] at this; cases this + exact ⟨_, hz0, rfl⟩ + have nsw : NoSwA σ1 := S1.ns fun u c => J.noSwitch u c + obtain ⟨J', w, osk, twS⟩ := J.rec_scEndA blk1 H1 S1.inv nsw F1' (Sched.qk_of mem) END (fun _ _ h => h) reply + ⟨xs, ns, hk⟩ hQ7 + exact ⟨rfl, σ1, Q1, R1, _, 0#u64, 0, mem, J', w, osk, F1', S1.idle, END, twS⟩ + · -- `complete_yield_to` of the yielder + rw [if_neg (by simpa using hy0)] + have hyf : x1.scYieldFrom = some (specPtr (L.mem s1 (scYieldFromA c))) := by rw [hyw, optRefS_ne hy0] + obtain ⟨u, hu, eu, hyu⟩ := Y1.frm c hc x1 n1 hk1 _ hyf + have eyu : u = L.mem s1 (scYieldFromA c) := specPtr_inj eu + generalize hyfd : L.mem s1 (scYieldFromA c) = yf at hy0 hyf eyu + subst eyu + have huH : u ∈ H.tcbs := by rw [J.tcbs]; exact hu + refine WP.spec_mono (WP.spec_and (yield_complete_liveWithA H1 hst1 M1 Y1 a hc J.scNZ hy0 hyfd + maxTicksToUs ticksToUs hmax (htu a)) + (Fl4ProofsA64.P1.schedcontext.complete_yield_to.blkS (st := st) hA s1 a u c blk1 (fun _ => huH) hcS)) ?_ + rintro ⟨s2, a2⟩ ⟨⟨ea2, σ2, -, H2, -, M2, Y2, -, -, -, -, scs, wr2, ev, far, d, done, v0, v1, v2⟩, blk2⟩ + simp only [WP.uncurry'_pair] at blk2 + obtain ⟨blk2, -⟩ := blk2 + dsimp only at ea2 H2 M2 Y2 wr2 v0 v1 v2 ⊢ + subst ea2 + rw [completeYieldToWith_eq_l4v] at ev + have m2 : ((), σ2) ∈ (completeYieldTo (Z := Unit) (specPtr u) σ1).1 := by rw [ev]; exact rfl + have mem : ((), σ2) ∈ (unbindFromSc (Z := Unit) (specPtr t) σ).1 := + unbindFromSc_memA hx hxsc m1 hk1 (by rw [hyf]; exact m2) + obtain ⟨zy, hzy0, hzy⟩ := Y1.tcb u hu + have hzy' : zy.tcbYieldTo = some (scNameA m c) := by + rw [hzy, hyu]; simp [optScA, hc0, scNameA_eq] + have K2 : RefK σ1 σ2 := completeYieldTo_refKA hzy0 hzy' hk1 m2 + have inv2 : LoopInvA σ2 m n := S1.inv.refK K2 + have nsw : NoSwA σ2 := fun v e => S1.ns J.noSwitch v (by rw [K2.1] at e; exact e) + have F2 : SchedFrameA σ σ2 := + PreO.trans F1' ((inferInstance : SPresInst (completeYieldTo (Z := Unit) (specPtr u))).pres σ1 ((), σ2) m2) + have hne : ∀ r o, σ1.kheap r = some o → (∀ x, o ≠ .tcb x) → (∀ y n, o ≠ .schedContext y n) → + σ2.kheap r = σ1.kheap r := by + intro r o h hn1 hn2 + refine far r (fun e => ?_) (fun e => ?_) + · rw [e, hk1] at h; cases h; exact hn2 _ _ rfl + · rw [e, hzy0] at h; cases h; exact hn1 _ rfl + have END : ScEndGA st Z N Rp m s s2 σ σ2 0#u64 0#u64 u d := by + refine ⟨(F1.wr.trans wr2).mono fun b hb => ?_, fun c => absurd rfl c, fun c => absurd rfl c, + fun _ => ⟨hu, v0, v1, v2⟩, fun r z h => ?_, fun r z h => ?_, fun r z h => ?_, fun r z h => ?_, M2, Y2⟩ + · rcases hb with hb | (hb | hb | hb) + · exact Or.inl hb + · exact Or.inl (mwr_back (Z := Z) (Z' := { Z with Q := Q1, R := R1 }) rfl rfl F1.sz F1.wr.1 hb) + · exact Or.inr (Or.inr (Or.inr ⟨hy0, Or.inl hb⟩)) + · exact Or.inr (Or.inr (Or.inr ⟨hy0, Or.inr hb⟩)) + · obtain ⟨o, ho⟩ := tcb1 r z h + by_cases er : r = specPtr u + · subst er + have ez : zy = { z with tcbSchedContext := o } := by + have e := hzy0.symm.trans ho + simp only [Option.some.injEq, KernelObject.tcb.injEq] at e + exact e + refine ⟨_, o, done _ hzy0, fun _ => ?_, fun c => absurd ⟨hy0, rfl⟩ c⟩ + rw [ez] + rfl + · refine ⟨_, o, (far r ?_ er).trans ho, fun c => absurd c.2 er, fun _ => rfl⟩ + intro e; rw [e, hk1] at ho; cases ho + · have h1' := obj1 r _ h (fun _ _ e => by cases e) (fun _ e => by cases e) + rw [hne r _ h1' (fun _ e => by cases e) (fun _ _ e => by cases e)] + exact h1' + · have h1' := obj1 r _ h (fun _ _ e => by cases e) (fun _ e => by cases e) + exact ⟨z.ntfnSc, by rw [hne r _ h1' (fun _ e => by cases e) (fun _ _ e => by cases e)]; exact h1', + fun c => absurd rfl c.1, fun _ => rfl⟩ + · have h1' := obj1 r _ h (fun _ _ e => by cases e) (fun _ e => by cases e) + exact ⟨z.replySc, by rw [hne r _ h1' (fun _ e => by cases e) (fun _ _ e => by cases e)]; exact h1', + fun c => absurd rfl c.1, fun _ => rfl⟩ + have reply : ReplyStackRelA (L.mem s2) Rp Z.C σ2 (scNameA m) stack := by + refine J.reply.frame (fun r hr => ?_) (fun c _ z k hz => ?_) (fun r hr z hz => ?_) + · obtain ⟨r32, -, rfit⟩ := J.iplaced.e.rep r hr + have one : ∀ o : U64, o.val < 32 → o.val % 8 = 0 → L.mem s2 (wa r o) = L.mem s (wa r o) := by + intro o o1 o2 + have v : (wa r o).val = r.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have hw : IpcObjWA N E Rp (wa r o) := Or.inr (Or.inr ⟨r, hr, by omega, by omega⟩) + refine END.wr.mem_eq (by show (wa r o).val % 8 = 0; omega) ?_ + rintro (x' | ⟨c0, -⟩ | ⟨c0, -⟩ | ⟨-, x'⟩) + · exact J.ipc_offMA hw x' + · exact c0 rfl + · exact c0 rfl + · rcases x' with (x' | x' | x') | x' + · exact J.ipc_ne_tcbA hu (K := 256#u64) (by simp) hw x'.symm + · exact J.ipc_ne_tcbA hu (K := 264#u64) (by simp) hw x'.symm + · exact J.ipc_ne_tcbA hu (K := 272#u64) (by simp) hw x'.symm + · exact J.ipc_ne_tcbA hu (K := 3472#u64) (by simp) hw x'.symm + have n16 : (layout.reply.NEXT).val = 16 := by unfold layout.reply.NEXT; rfl + have p8 : (layout.reply.PREV).val = 8 := by unfold layout.reply.PREV; rfl + exact ⟨one _ (by omega) (by omega), one _ (by omega) (by omega)⟩ + · obtain ⟨y, hy, e⟩ := Rp1 _ _ _ hz + obtain ⟨y', hy', e'⟩ := (K2.2.1 _).scF hy + exact ⟨y', k, hy', e'.2.1.trans e⟩ + · obtain ⟨z0, hz0, -⟩ := J.ep.reply r hr + obtain ⟨o, ho, -, o2⟩ := END.re _ _ hz0 + rw [hz] at ho + simp only [Option.some.injEq, KernelObject.reply.injEq] at ho + exact ⟨z0, hz0, by rw [ho]; exact o2 (fun c => c.1 rfl)⟩ + obtain ⟨J', w, osk, twS⟩ := J.rec_scEndA blk2 H2 inv2 nsw F2 (Sched.qk_of mem) END (fun _ _ h => h) reply + ⟨xs, ns, hk⟩ hQ7 + exact ⟨rfl, σ2, Q1, R1, _, u, d, mem, J', w, osk, F2, (congrArg (fun τ : Fl4SpecA64.State Unit => τ.idleThread) K2.1).trans S1.idle, END, twS⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelThrSusA.lean b/proofs/Fl4ProofsA64/P3/DelThrSusA.lean new file mode 100644 index 000000000..7b73755eb --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelThrSusA.lean @@ -0,0 +1,152 @@ +/- +P3 (AArch64, delete hooks W3, contract §77): **the Thread finaliser's middle at the record** (slice 2, step 3): after +the front (`Vcpu.thrFront_recA`), `timer.remove t`, the SC unbinding and `sched.suspend t` refine +`unbind_from_sc t` and then `fl4CancelRun t >>= suspend t`, and `JStage3TA` holds again. + +* `JStage3TA.suspReadyA`: `suspend_recA`'s `TcbSuspendReadyA` from the record, given `RUN_VCPU = 0`. The reply case + comes from P1's `ReplyInv0.reply` through `EpStateRel` (`repOKA`'s pattern), the queue cases from `blkQ`, `runS` + from `VcpuCoreA`, and `on` from [S] `numNodes = 1`. +* `JStage3TA.stepNW_vcA`: a `SchedStepNWA` step keeps every TCB's VCPU word (+3664). +* `JStage3TA.thrMid_recA`: `timerRm_recA`, then `thrSc_recA`, then `suspend_recA`. + +Premises: [P] `hA`, `hCs`, `hmb`, `hmax`, `htu`; [S] `h1`. Caller hypotheses: `hti` / `hidles` +(`JStage3TA.notIdle_of_capA` at the finalised thread cap), `hrw` (`RUN_VCPU = 0`, the front's export after +`vcpu.detach`), and `hQ7`. +-/ +import Fl4ProofsA64.P3.DelThrScA +import Fl4ProofsA64.P3.DelSuspendA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 Fl4ProofsA64.P3.Endp +open Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Notif + +local notation "wa" => core.num.U64.wrapping_add + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- **`suspend_recA`'s readiness from the record** (module docstring). -/ +theorem JStage3TA.suspReadyA [J7PpA st J7] + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + {t : U64} (ht : t ∈ Z.T) (hrw : L.mem s (runVcpuA t) = 0#u64) : + ∃ x, TcbSuspendReadyA st Z m N E Rp s σ t x := by + obtain ⟨x, hx, hrel⟩ := J.ep.tcb t ht + have htH : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + refine ⟨x, hx, fun r' hs => ?_, fun e' he => ?_, fun n' hn => ?_, J.repOKA ht, hrw, ?_, + J.onCore_single hCs h1 ht⟩ + · have e1 : L.mem s (Fl4ProofsA64.P1.stA t) = sched.TS_BLOCKED_ON_REPLY := by + apply UScalar.eq_of_val_eq + show (L.mem s (Fl4ProofsA64.P1.stA t)).val = _ + rw [hrel.st, hs]; unfold sched.TS_BLOCKED_ON_REPLY; rfl + obtain ⟨hin, back⟩ := J.blkA.2.2.1.2.1.reply t htH e1 + have hiR : L.mem s (tReplyA t) ∈ Rp := J.ipcHeapCov.reply _ hin + obtain ⟨xr, hkr, hb⟩ := J.ep.reply _ hiR + have er : specPtr (L.mem s (tReplyA t)) = r' := hrel.reply r' hs + have t0 : t ≠ 0#u64 := J.sr.place.nz t _ ((J.sr.thT t).2 ht) + have back' : L.mem s (Endp.rTcbA (L.mem s (tReplyA t))) = t := back + exact ⟨_, hiR, er, xr, by rw [← er]; exact hkr, by rw [hb, back', optRef_ne t0], J.scCov.reply _⟩ + · exact J.blkQ.ep t ht x hx e' he + · obtain ⟨a1, y, q, hy, hq, hm⟩ := J.blkQ.ntfn t ht x hx n' hn + refine ⟨a1, y, hy, ?_⟩ + rw [hq] + simpa only [Fl4SpecA64.Ntfn.ntfnQueue] using hm + · obtain ⟨x', hx', -, hrv⟩ := J.j.2.1.tcb t ht + have e := hx'.symm.trans hx + simp only [Option.some.injEq, KernelObject.tcb.injEq] at e + subst e + rw [hrv, hrw] + simp [optRefA] + +/-- A `SchedStepNWA` step keeps every TCB's VCPU word. -/ +theorem JStage3TA.stepNW_vcA [J7PpA st J7] + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) {s' : S} + (w : WritesIn st s s' (SchedStepNWA (st := st) N E Rp Z m σ s)) {u : U64} (hu : u ∈ Z.T) : + L.mem s' (wa u 3664#u64) = L.mem s (wa u 3664#u64) := by + have huH : u ∈ H.tcbs := by rw [J.tcbs]; exact hu + have hfit := (J.sr.core.tcbs u hu).fits + have hv : (wa u 3664#u64).val = u.val + 3664 := by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by simp; omega) + refine w.mem_eq (field_aligned (J.sr.core.tcbs u hu) (by simp) (by simp)) (fun h => ?_) + rcases h with (hm | ⟨v, hv', h1, h2, h3, -⟩) | hipc + · exact J.tcbWord_offMA hu (by simp) (by simp) hm + · by_cases e : v = u + · subst e; exact h3 rfl + · have := J.sr.place.apart v u .tcb .tcb ((J.sr.thT v).2 hv') ((J.sr.thT u).2 hu) e + have hsz : ObjKindA.size .tcb = 4096 := rfl + omega + · exact J.ipc_off_hobjA (v := u) (kv := .tcb) huH (by simp) + ⟨by omega, by simp only [HKind.bits]; omega⟩ hipc + +set_option maxHeartbeats 3200000 in +/-- **The Thread finaliser's middle at the record** (module docstring). -/ +theorem JStage3TA.thrMid_recA {ar : machine.Arch A} [J7PpA st J7] [J7PpKA st J7] + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + (hQ7 : ∀ Q Rl, J7 { Z with Q := Q, R := Rl } = J7 Z) + (hA : P0.ArchTotal ar) (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {t : U64} (ht : t ∈ Z.T) (hti : specPtr t ≠ σ.idleThread) (hidles : ∀ c < n, m.idleThreadOf c ≠ specPtr t) + (hrw : L.mem s (runVcpuA t) = 0#u64) : + (do + let s4 ← timer.remove st s t + let r ← (do + let i ← obj.Tcb.sc st t s4 + let b ← obj.Sc.is_null i + if b then ok (s4, a) else do + let s6 ← schedcontext.unbind_tcb st s4 i + let i1 ← obj.Sc.yield_from st i s6 + let b1 ← obj.Tcb.is_null i1 + if b1 then ok (s6, a) else schedcontext.complete_yield_to st ar s6 a i1) + sched.suspend st ar r.1 r.2 t) ⦃ r => + r.2 = a ∧ ∃ (σ5 σ6 : Fl4SpecA64.State Unit) (Q : U64 → U64 → List U64) (R : U64 → List U64) + (Ac' : Set U64) (stack' : U64 → List U64), + ((), σ5) ∈ (unbindFromSc (Z := Unit) (specPtr t) σ).1 ∧ SchedFrameA σ σ5 ∧ + (fl4CancelRun (Z := Unit) (specPtr t) >>= fun _ => Fl4SpecA64.suspend (specPtr t)) σ5 = + ({((), σ6)}, False) ∧ + JStage3TA st AR { Z with Q := Q, R := R } H OS U Ac' Pp Tb Lv ia ie N E Rp Bf stack' κ n (J5A st) J7 + r.1 a σ6 m ∧ + OSKeep st OS s r.1 ∧ L.ksb r.1 = L.ksb s ∧ L.cpu r.1 = L.cpu s ∧ SchedFrameA σ σ6 ∧ + (∃ y, σ6.kheap (specPtr t) = some (.tcb y) ∧ y.tcbState = .inactive) ∧ + (∀ u ∈ Z.T, L.mem r.1 (wa u 3664#u64) = L.mem s (wa u 3664#u64)) ∧ + (∀ R7 : Set U64 → JSlotA S, J7 = J7LA st R7 → Std6KeepA st Z H OS Bf s σ r.1 σ6) ⦄ := by + refine WP.spec_bind (J.timerRm_recA ht) ?_ + rintro s4 ⟨J4, w4, osk4, tk4⟩ + refine WP.spec_bind (J4.thrSc_recA hQ7 hA hCs h1 hmax htu ht hti) ?_ + rintro ⟨s5, a5⟩ ⟨ea5, σ5, Q, R, Ac', yf, d, m5, J5', w5, osk5, F5, id5, -, tw5⟩ + dsimp only at ea5 J5' w5 osk5 tw5 ⊢ + subst ea5 + have rw5 : L.mem s5 (runVcpuA t) = 0#u64 := by + have e1 := tk4 t ht 3672#u64 (by simp) (by simp) (Or.inr (by simp)) + have e2 := tw5 t ht 3672#u64 (by simp) (by simp) (by simp) + unfold runVcpuA at hrw ⊢ + rw [e2, e1]; exact hrw + obtain ⟨x5, X⟩ := J5'.suspReadyA hCs h1 ht rw5 + have hQ7' : ∀ Q' Rl', J7 { ({ Z with Q := Q, R := R } : XSetsA) with Q := Q', R := Rl' } = + J7 { Z with Q := Q, R := R } := fun Q' Rl' => (hQ7 Q' Rl').trans (hQ7 Q R).symm + refine WP.spec_mono (J5'.suspend_recA (J.curCore_lt hCs) hmb hmax htu ht X hQ7' (by rw [id5]; exact hti) + hidles) ?_ + rintro ⟨s6, a6⟩ ⟨ea6, σ6, stack', Q', Rl', sp6, w6, F6, osk6, hy6, J6⟩ + dsimp only at ea6 sp6 w6 F6 osk6 hy6 J6 ⊢ + rw [ea6] at J6 + refine ⟨ea6, σ5, σ6, Q', Rl', Ac', stack', m5, F5, sp6, J6, OSKeep.transA osk4 (OSKeep.transA osk5 osk6), + w6.1.trans (w5.1.trans w4.1), w6.2.1.trans (w5.2.1.trans w4.2.1), PreO.trans F5 F6, hy6, fun u hu => ?_, fun R7 e => by + subst e + exact fun {_} sub h => (Std6GA.schedStepA J5' ((Std6GA.schedStepA J4 (Std6GA.schedStepA J h sub w4 osk4 + (⟨SpecSchedA.refl _, SSzA.refl _, rfl⟩)) sub w5 osk5 F5).ghostA Z.G Q R Z.hw) sub w6 osk6 F6).ghostA Z.G Z.Q Z.R Z.hw⟩ + rw [J5'.stepNW_vcA w6 hu, tw5 u hu 3664#u64 (by simp) (by simp) (by simp), + tk4 u hu 3664#u64 (by simp) (by simp) (Or.inr (by simp))] + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelThrTimeA.lean b/proofs/Fl4ProofsA64/P3/DelThrTimeA.lean new file mode 100644 index 000000000..940d11701 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelThrTimeA.lean @@ -0,0 +1,319 @@ +/- +P3 (AArch64, delete hooks, contract §79): **the spec Thread finaliser keeps the time** (`finTimeSpec_threadA : +FinTimeSpecA fl4FinaliseCap .thread`), a spec-only frame over the `threadCap` arm of `Ext/Finalise`. + +`MTmA mop`: every outcome of the machine operation keeps `timeState` and `lastMachineTime`. The tactic `tm` walks a +spec program (`NdTmA` / `MTmA`), unfolding each called operation. No named premise. New module. +-/ +import Fl4ProofsA64.P3.DelTimeSpecA + +namespace Fl4ProofsA64.P3 +open Fl4ProofsA64.P1 Fl4Spec Fl4SpecA64 + +noncomputable section + +set_option maxHeartbeats 1600000 + +/-- **A machine operation keeping the time fields** (module docstring). -/ +def MTmA {α : Type} (m : NonDet MachineState α) : Prop := + ∀ ms r, r ∈ (m ms).1 → r.2.timeState = ms.timeState ∧ r.2.lastMachineTime = ms.lastMachineTime + +section +variable {α β γ : Type} + +theorem MTmA.ro {m : NonDet MachineState α} (h : ∀ ms r, r ∈ (m ms).1 → r.2 = ms) : MTmA m := fun ms r hr => by + rw [h ms r hr]; exact ⟨rfl, rfl⟩ + +theorem MTmA.pure (a : α) : MTmA (Pure.pure a : NonDet MachineState α) := + MTmA.ro fun _ _ h => by rw [Set.mem_singleton_iff.1 h] + +theorem MTmA.bind {m : NonDet MachineState α} {f : α → NonDet MachineState β} (hm : MTmA m) (hf : ∀ a, MTmA (f a)) : + MTmA (m >>= f) := fun ms r hr => by + obtain ⟨x, hx, hr⟩ := (show ∃ x ∈ (m ms).1, r ∈ (f x.1 x.2).1 from hr) + have h1 := hm ms x hx + have h2 := hf x.1 x.2 r hr + exact ⟨h2.1.trans h1.1, h2.2.trans h1.2⟩ + +theorem MTmA.gets (g : MachineState → α) : MTmA (NonDet.gets g) := + MTmA.ro fun _ _ h => by rw [Set.mem_singleton_iff.1 h] + +theorem MTmA.assert (P : Prop) : MTmA (NonDet.assert P : NonDet MachineState Unit) := + MTmA.ro fun _ _ h => by rw [h.2] + +theorem MTmA.stateAssert (P : MachineState → Prop) : MTmA (NonDet.stateAssert P) := + MTmA.ro fun _ _ h => by rw [h.2] + +theorem MTmA.modify {g : MachineState → MachineState} + (h : ∀ ms, (g ms).timeState = ms.timeState ∧ (g ms).lastMachineTime = ms.lastMachineTime) : + MTmA (NonDet.modify g) := fun ms r hr => by rw [Set.mem_singleton_iff.1 hr]; exact h ms + +theorem MTmA.lift (f : MachineRestMonad Unit) : MTmA (machineOpLift f) := by + unfold machineOpLift machineRestLift + refine MTmA.bind (MTmA.gets _) fun _ => MTmA.bind (MTmA.ro fun _ _ h => h.2) fun x => ?_ + exact MTmA.bind (MTmA.modify fun _ => ⟨rfl, rfl⟩) fun _ => MTmA.pure _ + +theorem doMachineOp_mtmA {m : NonDet MachineState α} (h : MTmA m) : NdTmA (doMachineOp (Z := Unit) m) := by + intro σ r hr + unfold doMachineOp at hr + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + rw [Set.mem_singleton_iff.1 hx] at hr + obtain ⟨y, hy, hr⟩ := mem_bindA.1 hr + obtain ⟨hy1, hy2⟩ := hy + obtain ⟨z, hz, hr⟩ := mem_bindA.1 hr + rw [Set.mem_singleton_iff.1 hz] at hr + rw [Set.mem_singleton_iff.1 hr, hy2] + have := h _ _ hy1 + exact tmA_eqA rfl this.2 this.1 rfl rfl rfl rfl + +theorem NdTmA.mapMx {f : α → NonDet (State Unit) β} (h : ∀ a, NdTmA (f a)) : + ∀ l : List α, NdTmA (NonDet.mapMx f l) + | [] => NdTmA.npure _ + | a :: l => NdTmA.nbind (h a) fun _ => NdTmA.mapMx h l + +theorem MTmA.mapM {f : α → NonDet MachineState β} (h : ∀ a, MTmA (f a)) : ∀ l : List α, MTmA (NonDet.mapM f l) + | [] => MTmA.pure _ + | a :: l => MTmA.bind (h a) fun _ => MTmA.bind (MTmA.mapM h l) fun _ => MTmA.pure _ + +theorem MTmA.mapMx {f : α → NonDet MachineState β} (h : ∀ a, MTmA (f a)) : ∀ l : List α, MTmA (NonDet.mapMx f l) + | [] => MTmA.pure _ + | a :: l => MTmA.bind (h a) fun _ => MTmA.mapMx h l + +theorem NdTmA.zipWithMx {f : α → β → NonDet (State Unit) γ} (h : ∀ a b, NdTmA (f a b)) : + ∀ (l : List α) (l' : List β), NdTmA (NonDet.zipWithMx f l l') + | a :: l, b :: l' => NdTmA.nbind (h a b) fun _ => NdTmA.zipWithMx h l l' + | [], _ => by unfold NonDet.zipWithMx; exact NdTmA.npure _ + | _ :: _, [] => by unfold NonDet.zipWithMx; exact NdTmA.npure _ + +end + +/-- (Task 0) **Read a TCB, then run `g` on it**: `g`'s outcomes from a state holding that TCB keep the time. -/ +theorem getTcbThen_tmA {α : Type} (p : ObjRef) (g : Fl4SpecA64.Tcb → NonDet (State Unit) α) + (h : ∀ x σ, σ.kheap p = some (.tcb x) → ∀ r, r ∈ (g x σ).1 → TmA σ r.2) : + NdTmA (NonDet.getsThe (getTcb (Z := Unit) p) >>= g) := fun σ r hr => by + obtain ⟨y1, hy1, h2⟩ := mem_bindA.1 hr + have hy1' : y1 ∈ (NonDet.assertOpt (getTcb (Z := Unit) p σ) σ).1 := hy1 + cases hg : getTcb (Z := Unit) p σ with + | none => rw [hg] at hy1'; exact absurd hy1' (Set.notMem_empty _) + | some x => + rw [hg] at hy1' + have e1 : y1 = (x, σ) := hy1' + subst e1 + exact h x σ (Sched.getTcb_kheapA hg) r h2 + +/-- (Task 0) `thread_set` with an update that keeps the SC binding or clears it. -/ +theorem threadSet_tmA (f : Fl4SpecA64.Tcb → Fl4SpecA64.Tcb) (p : ObjRef) + (hf : ∀ x c, (f x).tcbSchedContext = some c → x.tcbSchedContext = some c) : + NdTmA (threadSet (Z := Unit) f p) := by + unfold threadSet + refine getTcbThen_tmA p _ fun x σ hx r hr => ?_ + obtain ⟨_, -, e⟩ := setObject_mem hr + rw [e]; exact tcbW_tmA hx (hf x) + +theorem archThreadSet_tmA (f : ArchTcb → ArchTcb) (p : ObjRef) : NdTmA (archThreadSet (Z := Unit) f p) := by + unfold archThreadSet + refine getTcbThen_tmA p _ fun x σ hx r hr => ?_ + obtain ⟨_, -, e⟩ := setObject_mem hr + rw [e]; exact tcbW_tmA hx fun _ h => h + +/-- (Task 0) Clearing a TCB's SC binding. -/ +theorem tcbScNone_hfA : ∀ (x : Fl4SpecA64.Tcb) (c : ObjRef), + (tcbSchedContextUpdate (fun _ => none) x).tcbSchedContext = some c → x.tcbSchedContext = some c := by + intro x c h + simp [tcbSchedContextUpdate] at h + +/-- (Task 0) A read-only step before a continuation (the outcome's state is the start state). -/ +theorem preBind_roA {α β : Type} {m : NonDet (State Unit) α} {f : α → NonDet (State Unit) β} {σ : State Unit} + {Q : State Unit → Prop} (hm : ∀ r ∈ (m σ).1, r.2 = σ) (hf : ∀ a, ∀ r ∈ (f a σ).1, Q r.2) : + ∀ r ∈ ((m >>= f) σ).1, Q r.2 := fun r hr => by + obtain ⟨⟨a, s⟩, hy, h2⟩ := mem_bindA.1 hr + have e : s = σ := hm _ hy + subst e + exact hf a r h2 + +theorem preBind_pureA {α β : Type} {a : α} {f : α → NonDet (State Unit) β} {σ : State Unit} + {Q : State Unit → Prop} (hf : ∀ r ∈ (f a σ).1, Q r.2) : ∀ r ∈ ((pure a >>= f) σ).1, Q r.2 := fun r hr => by + obtain ⟨y, hy, hr⟩ := mem_bindA.1 hr + rw [Set.mem_singleton_iff.1 hy] at hr + exact hf r hr + +/-- (Task 0) A step followed by a read-only tail. -/ +theorem preBind_tailA {α β : Type} {m : NonDet (State Unit) α} {f : α → NonDet (State Unit) β} {σ : State Unit} + {Q : State Unit → Prop} (hm : ∀ r ∈ (m σ).1, Q r.2) (hf : ∀ a s, ∀ r ∈ (f a s).1, r.2 = s) : + ∀ r ∈ ((m >>= f) σ).1, Q r.2 := fun r hr => by + obtain ⟨y, hy, hr⟩ := mem_bindA.1 hr + rw [hf y.1 y.2 r hr] + exact hm y hy + +theorem asUser_tmA {α : Type} (p : ObjRef) (f : UserMonad α) : NdTmA (asUser (Z := Unit) p f) := by + unfold asUser + refine getTcbThen_tmA p _ fun x σ hx => ?_ + refine preBind_pureA (Q := fun τ => TmA σ τ) ?_ + refine preBind_roA (fun r h => h.2) fun au => ?_ + obtain ⟨a, uc⟩ := au + refine preBind_pureA (Q := fun τ => TmA σ τ) ?_ + refine preBind_tailA (fun r h => ?_) (fun _ _ r h => by rw [Set.mem_singleton_iff.1 h]) + obtain ⟨_, -, e⟩ := setObject_mem h + rw [e]; exact tcbW_tmA hx fun _ h => h + +/-- (Task 0) A step keeping the time before a continuation that keeps it. -/ +theorem preBind_stepA {α β : Type} {m : NonDet (State Unit) α} {f : α → NonDet (State Unit) β} {σ : State Unit} + (hm : ∀ r ∈ (m σ).1, TmA σ r.2) (hf : ∀ a, NdTmA (f a)) : ∀ r ∈ ((m >>= f) σ).1, TmA σ r.2 := fun r hr => by + obtain ⟨y, hy, h2⟩ := mem_bindA.1 hr + exact tmA_trans (hm y hy) (hf y.1 y.2 r h2) + +/-- (Task 0) `update_sched_context` with an update that keeps an active SC's activity and head refill. -/ +theorem updateSchedContext_tmA (p : ObjRef) (f : SchedContext → SchedContext) + (hf : ∀ y, scActive (f y) = true → scActive y = true ∧ refillHd y = refillHd (f y)) : + NdTmA (updateSchedContext (Z := Unit) p f) := fun σ r hr => by + unfold updateSchedContext at hr + obtain ⟨y1, hy1, hr⟩ := mem_bindA.1 hr + obtain ⟨e1, e2⟩ := getObject_mem hy1 + obtain ⟨o, s1⟩ := y1 + dsimp only at e1 e2 hr + subst e1 + cases o with + | schedContext y n => + obtain ⟨_, -, e⟩ := setObject_mem hr + rw [e]; exact scW_tmA e2 (hf y) + | _ => exact hr.elim + +/-- (Task 0) `tcb_release_remove` only removes from the release queue. -/ +theorem tcbReleaseRemove_tmA (t : ObjRef) : NdTmA (tcbReleaseRemove (Z := Unit) t) := fun σ r hr => by + unfold tcbReleaseRemove at hr + obtain ⟨y1, hy1, hr⟩ := mem_bindA.1 hr + rw [Set.mem_singleton_iff.1 hy1] at hr + obtain ⟨y2, hy2, hr⟩ := mem_bindA.1 hr + have h2 : TmA σ y2.2 ∧ y2.2.releaseQueue = σ.releaseQueue := by + unfold NonDet.when at hy2 + split at hy2 + · rw [Set.mem_singleton_iff.1 hy2]; exact ⟨tmA_eqA rfl rfl rfl rfl rfl rfl rfl, rfl⟩ + · rw [Set.mem_singleton_iff.1 hy2]; exact ⟨tmA_refl σ, rfl⟩ + obtain ⟨y3, hy3, hr⟩ := mem_bindA.1 hr + rw [Set.mem_singleton_iff.1 hy3] at hr + rw [Set.mem_singleton_iff.1 hr] + refine tmA_trans h2.1 ⟨rfl, rfl, rfl, ⟨rfl, rfl, fun u hu => ?_, fun y k h a => ⟨y, k, h, a, rfl⟩, + fun u x h e _ => ⟨x, h, e⟩⟩⟩ + rw [h2.2] + exact (List.mem_filter.1 hu).1 + +macro "tm0" : tactic => `(tactic| repeat' first + | with_reducible exact NdTmA.pure _ | with_reducible exact NdTmA.npure _ | with_reducible exact NdTmA.gets _ | with_reducible exact NdTmA.get + | with_reducible exact NdTmA.assert _ | with_reducible exact NdTmA.assertOpt _ | with_reducible exact NdTmA.getsThe _ | with_reducible exact NdTmA.getsMap _ _ + | with_reducible exact NdTmA.selectF _ | with_reducible exact getObject_tmA _ + | with_reducible refine setObject_tmA _ _ ?_ ?_ + | with_reducible refine NdTmA.modify (fun _ => ?_) | exact tmA_eqA rfl rfl rfl rfl rfl rfl rfl + | (intro _ h; exact KernelObject.noConfusion h) | (intro _ _ h; exact KernelObject.noConfusion h) + | with_reducible refine NdTmA.bind ?_ (fun _ => ?_) | with_reducible refine NdTmA.nbind ?_ (fun _ => ?_) | split) + +theorem threadGet_tmA {α : Type} (f : Fl4SpecA64.Tcb → α) (p : ObjRef) : NdTmA (threadGet (Z := Unit) f p) := by + unfold threadGet getTcb; tm0 + +theorem tcbSchedAction_tmA (g : ObjRef → List ObjRef → List ObjRef) (p : ObjRef) : + NdTmA (tcbSchedAction (Z := Unit) g p) := by + unfold tcbSchedAction getTcbQueue setTcbQueue + refine NdTmA.bind (threadGet_tmA _ _) fun _ => NdTmA.bind (threadGet_tmA _ _) fun _ => ?_ + tm0 + +/-- Walk a spec program (module docstring). -/ +macro "tmstep" : tactic => `(tactic| first + | with_reducible exact NdTmA.pure _ | with_reducible exact NdTmA.npure _ | with_reducible exact NdTmA.gets _ | with_reducible exact NdTmA.get | with_reducible exact NdTmA.assert _ + | with_reducible exact NdTmA.assertOpt _ | with_reducible exact NdTmA.getsThe _ | with_reducible exact NdTmA.getsMap _ _ | with_reducible exact NdTmA.selectF _ + | with_reducible exact NdTmA.fail | with_reducible exact getObject_tmA _ | with_reducible exact setVmRoot_tmA _ + | with_reducible exact tcbReleaseRemove_tmA _ | with_reducible exact archThreadSet_tmA _ _ + | with_reducible exact asUser_tmA _ _ + | with_reducible refine threadSet_tmA _ _ ?_ | with_reducible refine updateSchedContext_tmA _ _ ?_ + | with_reducible refine setObject_tmA _ _ ?_ ?_ + | with_reducible refine NdTmA.modify (fun _ => ?_) | exact tmA_eqA rfl rfl rfl rfl rfl rfl rfl + | with_reducible exact MTmA.pure _ | with_reducible exact MTmA.gets _ | with_reducible exact MTmA.assert _ | with_reducible exact MTmA.stateAssert _ | with_reducible exact MTmA.lift _ + | with_reducible refine MTmA.modify (fun _ => ?_) | exact ⟨rfl, rfl⟩ + | with_reducible refine doMachineOp_mtmA ?_ + | with_reducible refine NdTmA.when ?_ | with_reducible refine NdTmA.mapMx (fun _ => ?_) _ | with_reducible refine NdTmA.mapM (fun _ => ?_) _ | with_reducible refine NdTmA.zipWithMx (fun _ _ => ?_) _ _ + | with_reducible refine MTmA.mapM (fun _ => ?_) _ | with_reducible refine MTmA.mapMx (fun _ => ?_) _ + | with_reducible refine NdTmA.bind ?_ (fun _ => ?_) | with_reducible refine NdTmA.nbind ?_ (fun _ => ?_) | with_reducible refine MTmA.bind ?_ (fun _ => ?_) + | split + | unfold NonDet.maybeM | unfold NonDet.liftM + | with_reducible exact threadGet_tmA _ _ | with_reducible exact tcbSchedAction_tmA _ _ + | unfold getTcb | unfold getRunVcpu | unfold getVcpu | unfold setVcpu | unfold fl4VcpuExtUpdate | unfold setRunVcpu + | unfold fl4CancelRun | unfold getThreadState | unfold dsb | unfold readVCPUHardwareReg | unfold vcpuUpdate + | unfold vcpuSaveReg | unfold getGicVcpuCtrlHcr | unfold vgicUpdate | unfold writeVCPUHardwareReg + | unfold checkExportArchTimer | unfold disableVirtTimer | unfold getGicVcpuCtrlVmcr | unfold getGicVcpuCtrlApr + | unfold getGicVcpuCtrlLr | unfold vgicUpdateLr | unfold vcpuSaveRegRange | unfold vcpuSave + | unfold setGicVcpuCtrlHcr | unfold setSCTLR | unfold writeTCR | unfold setHCR | unfold isb | unfold enableFpuEL01 + | unfold maskInterrupt | unfold vcpuDisable | unfold vcpuInvalidateActive | unfold vcpuFlush | unfold fl4VcpuFlush + | unfold getCap | unfold fl4VcpuGet | unfold storeWord | unfold storeWordOffs | unfold copyRegsToFrame + | unfold getRegister | unfold fl4ReadStateToFrame | unfold setRegister | unfold setMessageInfo + | unfold setSchedulerAction | unfold setThreadStateAct | unfold setThreadState | unfold fl4CompleteRun + | unfold fl4VcpuEndRun | unfold getTcbObjRef | unfold getSimpleKo | unfold setSimpleKo | unfold updateSkObjRef + | unfold setNtfnObjRef | unfold setTcbObjRef | unfold doUnbindNotification | unfold unbindNotification + | unfold getSchedContext | unfold readSchedContext | unfold readRefillHead | unfold readScRefillSufficient + | unfold getScRefillSufficient | unfold readScRefillReady | unfold getScRefillReady | unfold getTcbQueue + | unfold setTcbQueue | unfold rescheduleRequired | unfold setReleaseQueue + | unfold schedContextUnbindTcb + | unfold schedContextUpdateConsumed | unfold lookupIpcBuffer | unfold replyFromKernel + | unfold setConsumed | unfold schedContextCancelYieldTo | unfold completeYieldTo | unfold unbindFromSc + | unfold getBlockingObject | unfold getEndpoint | unfold setEndpoint | unfold tcbEpDequeue | unfold getReply + | unfold setReplyObjRef | unfold replyUnlinkTcb | unfold blockedCancelIpc | unfold getNotification + | unfold setNotification | unfold tcbNtfnDequeue | unfold cancelSignal | unfold getSkObjRef | unfold getReplyObjRef + | unfold replyRemoveTcb | unfold cancelIpc | unfold updateRestartPc | unfold suspend | unfold archThreadGet + | unfold dissociateVcpuTcb | unfold enableFpu | unfold readFpuState | unfold setFPUState + | unfold saveFpuState | unfold disableFpu | unfold getFPUState | unfold writeFpuState | unfold loadFpuState + | unfold setArmCurrentFpuOwner | unfold switchLocalFpuOwner | unfold fpuRelease | unfold prepareThreadDelete + | (intro _ h; exact KernelObject.noConfusion h) | (intro _ _ h; exact KernelObject.noConfusion h) + | (intro _ _ h; exact h) | exact tcbScNone_hfA | (intro _ h; constructor; exact h; rfl)) + +/-- (Task 0) `set_mrs`: the TCB's registers (its SC binding kept), then IPC-buffer words. -/ +macro "tm" : tactic => `(tactic| repeat' tmstep) + +theorem setMrs_tmA (t : ObjRef) (buf : Option ObjRef) (msgs : List Message) : NdTmA (setMrs (Z := Unit) t buf msgs) := by + unfold setMrs + refine getTcbThen_tmA t _ fun x σ hx => ?_ + refine preBind_pureA (Q := fun τ => TmA σ τ) ?_ + refine preBind_pureA (Q := fun τ => TmA σ τ) ?_ + refine preBind_stepA (fun r h => ?_) (fun _ => ?_) + · obtain ⟨_, -, e⟩ := setObject_mem h + rw [e]; exact tcbW_tmA hx fun _ h => h + · tm + +/-- Walk a spec program, `set_mrs` as one step (Task 0). -/ +macro "tmm" : tactic => `(tactic| repeat' (first | exact setMrs_tmA _ _ _ | tmstep)) + +theorem fl4CancelRun_tmA (t : ObjRef) : NdTmA (fl4CancelRun (Z := Unit) t) := by unfold fl4CancelRun; tmm +theorem fl4VcpuEndRun_tmA (v : ObjRef) : NdTmA (fl4VcpuEndRun (Z := Unit) v) := by unfold fl4VcpuEndRun; tmm +theorem unbindNotification_tmA (t : ObjRef) : NdTmA (unbindNotification (Z := Unit) t) := by + unfold unbindNotification; tmm +theorem unbindFromSc_tmA (t : ObjRef) : NdTmA (unbindFromSc (Z := Unit) t) := by unfold unbindFromSc; tmm +theorem suspend_tmA (t : ObjRef) : NdTmA (Fl4SpecA64.suspend (Z := Unit) t) := by unfold Fl4SpecA64.suspend; tmm +theorem prepareThreadDelete_tmA (t : ObjRef) : NdTmA (prepareThreadDelete (Z := Unit) t) := by + unfold prepareThreadDelete; tmm + +/-- **The `threadCap` arm of `fl4FinaliseCap` keeps the time.** -/ +theorem fl4FinaliseCap_thread_tmA (t : ObjRef) (d : Bool) : NdTmA (fl4FinaliseCap (Z := Unit) (.threadCap t) d) := by + have hF : NdTmA (finaliseCap (Z := Unit) (.threadCap t) d) := by + unfold finaliseCap + exact NdTmA.bind (NdTmA.when (unbindNotification_tmA t)) fun _ => + NdTmA.bind (NdTmA.when (unbindFromSc_tmA t)) fun _ => NdTmA.bind (NdTmA.when (suspend_tmA t)) fun _ => + NdTmA.bind (NdTmA.when (prepareThreadDelete_tmA t)) fun _ => NdTmA.pure _ + simp only [fl4FinaliseCap] + refine NdTmA.bind (NdTmA.when ?_) fun _ => hF + refine NdTmA.bind (fl4CancelRun_tmA t) fun _ => NdTmA.bind (by unfold archThreadGet; tm) fun o => ?_ + cases o with + | some v => exact fl4VcpuEndRun_tmA v + | none => exact NdTmA.pure _ + +/-- **`FinTimeSpecA` for the Thread kind** (module docstring). -/ +theorem finTimeSpec_threadA : FinTimeSpecA fl4FinaliseCap .thread := by + intro c ac d σ x σ' hck hac hmem + have hA : ∃ t, ac = .threadCap t := by + rcases hac.eq_or_pt with e | ⟨p', a', v', e⟩ + · subst e + cases c <;> simp [finKindA] at hck + exact ⟨_, rfl⟩ + · subst e + cases c <;> simp [finKindA] at hck + simp [CapEqA, absCapA] at hac + obtain ⟨t, rfl⟩ := hA + exact fl4FinaliseCap_thread_tmA t d σ (x, σ') hmem + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelThrTimerA.lean b/proofs/Fl4ProofsA64/P3/DelThrTimerA.lean new file mode 100644 index 000000000..0f60f1713 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelThrTimerA.lean @@ -0,0 +1,385 @@ +/- +P3 (AArch64, delete hooks W3, contract §77): **the Thread finaliser's `timer.remove` at the record** (slice 2, +step 1). + +FL4's TCB timers have no spec counterpart (`Ext/Timer`: the spec does not keep the timer tree), so `timer.remove t` is +a concrete-only step. A64 P1's `timer.remove.tw` writes only the timer words `TW` (a TCB's words 3528–3575 and the +cores' node words 104 / 112, `TIMER_ROOT` / `TIMER_MIN`). These words are in `SchedStepNWA`, and the record holds +again at the same spec state. + +Step 4's `objects.debug_remove t` is the same kind of step: the debug list has no spec counterpart, and P1's +`debug_remove.x` writes only `DbgW` (`DEBUG_NEXT` / `DEBUG_PREV`, +3576 / +3584, and the nodes' `DEBUG_TCBS`, +88). +Both write sets lie in `TDWA`. + +* `TDWA`, `JStage3TA.tw_*A`: a `TDWA` word misses every word the record reads, except the three node words. +* `JStage3TA.sr_twA`: `SchedSRA` across timer writes. `SchedSRA.stable` does not apply because `SReadA` names every + node word; `SchedSRN.rebuildG` reads only the node words below 56. +* `JStage3TA.rec_tdwA`: the record after a `TDWA` step, with its write set, `OSKeep` and the kept TCB words; + `timerRm_recA` (`timer.remove t`) and `debugRm_recA` (`objects.debug_remove t`) instantiate it. + +No new premise. +-/ +import Fl4ProofsA64.P3.DelThrFrontA +import Fl4ProofsA64.P3.DelRvVrA +import Fl4ProofsA64.P1.QueueTimerSet +import Fl4ProofsA64.P1.RunVcpuKeep +import Fl4ProofsA64.P1.QueueIpc +import Fl4ProofsA64.P1.QueueFinRvSt +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 Fl4ProofsA64.P3.Endp +open Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Notif + +local notation "wa" => core.num.U64.wrapping_add + +noncomputable section + +/-- **The timer and debug-list words**: `timer.remove`'s `TW` and `debug_remove`'s `DbgW` together, a TCB's words +3528–3591 and a core's node words 88 / 104 / 112 (`DEBUG_TCBS`, `TIMER_ROOT`, `TIMER_MIN`). -/ +def TDWA {S : Type} (st : machine.Store S) [L : LawfulStore st] (Z : XSetsA) (s : S) (b : U64) : Prop := + (∃ y ∈ Z.T, ∃ K : U64, 3528 ≤ K.val ∧ K.val < 3592 ∧ b = wa y K) ∨ + ∃ c ∈ Z.Cs, ∃ o : U64, (o.val = 88 ∨ o.val = 104 ∨ o.val = 112) ∧ b = L.nodeAt s c o + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s s' : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- A timer word's value: a TCB's word 3528–3575, or a core's node word 104 / 112. -/ +theorem JStage3TA.tw_valA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) {b : U64} + (hb : TDWA st Z s b) : + (∃ y ∈ Z.T, y.val + 3528 ≤ b.val ∧ b.val < y.val + 3592) ∨ + ((L.ksb s).val + 223488 ≤ b.val ∧ b.val < (L.ksb s).val + 772352 ∧ + ∃ c ∈ Z.Cs, ∃ o : U64, (o.val = 88 ∨ o.val = 104 ∨ o.val = 112) ∧ c.val < 128 ∧ + b.val = (L.ksb s).val + 223488 + c.val * 4288 + o.val) := by + rcases hb with ⟨y, hy, K, k1, k2, rfl⟩ | ⟨c, hc, o, ho, rfl⟩ + · have hfit := (J.sr.core.tcbs y hy).fits + have hv : (wa y K).val = y.val + K.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact Or.inl ⟨y, hy, by omega, by omega⟩ + · obtain ⟨nn, -, hn, hcs⟩ := J.sr.csN + have hcl : c.val < 128 := by have := (hcs c).1 hc; omega + have hv := nodeAt_valSc (s := s) J.sr.ksFit hcl (o := o) (by omega) + exact Or.inr ⟨by omega, by omega, c, hc, o, ho, hcl, hv⟩ + +/-- A timer word is no word of a TCB outside the timer fields. -/ +theorem JStage3TA.tw_tcbA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) {b : U64} + (hb : TDWA st Z s b) {u : U64} (hu : u ∈ Z.T) (h1 : u.val ≤ b.val) + (h2 : b.val < u.val + 3528 ∨ (u.val + 3592 ≤ b.val ∧ b.val < u.val + 4096)) : False := by + have hks := J.sr.tcb_ks hu + have hksz : ksSizeA = 788736 := rfl + rcases J.tw_valA hb with ⟨y, hy, y1, y2⟩ | ⟨k1, k2, -⟩ + · by_cases e : u = y + · subst e; omega + · have := J.sr.place.apart u y .tcb .tcb ((J.sr.thT u).2 hu) ((J.sr.thT y).2 hy) e + have hsz : ObjKindA.size .tcb = 4096 := rfl + omega + · omega + +/-- A timer word of the TCB form, by offset. -/ +theorem JStage3TA.tw_tcbKA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) {u : U64} (hu : u ∈ Z.T) + {K : U64} (hK : K.val < 4096) (hK' : K.val < 3528 ∨ 3592 ≤ K.val) + (hb : TDWA st Z s (wa u K)) : False := by + have hfit := (J.sr.core.tcbs u hu).fits + have hv : (wa u K).val = u.val + K.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact J.tw_tcbA hb hu (by omega) (by omega) + +/-- A timer word is in no typed `other` object (IPC objects, IPC buffers). -/ +theorem JStage3TA.tw_otherA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) {b : U64} + (hb : TDWA st Z s b) {e : U64} {len : Nat} (he : Z.th e = some (.other len)) + (h1 : e.val ≤ b.val) (h2 : b.val < e.val + len) : False := by + have P := J.sr.place + have hsz : ObjKindA.size (.other len) = len := rfl + rcases J.tw_valA hb with ⟨y, hy, y1, y2⟩ | ⟨k1, k2, -⟩ + · have hth : Z.th y = some .tcb := (J.sr.thT y).2 hy + have hne : y ≠ e := fun h => by subst h; rw [hth] at he; cases he + have := P.apart y e .tcb (.other len) hth he hne + have hsz' : ObjKindA.size .tcb = 4096 := rfl + omega + · have := P.ks e (.other len) he (fun hn => by have := P.irqK he hn; cases this) + have hksz : ksSizeA = 788736 := rfl + omega + +theorem JStage3TA.tw_ipcA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) {b : U64} + (hb : TDWA st Z s b) (h : IpcObjWA N E Rp b) : False := by + rcases h with ⟨q, hq, h1, h2⟩ | ⟨q, hq, h1, h2⟩ | ⟨q, hq, h1, h2⟩ + · exact J.tw_otherA hb (J.ipcPlace.notification q hq) h1 h2 + · exact J.tw_otherA hb (J.ipcPlace.endpoint q hq) h1 h2 + · exact J.tw_otherA hb (J.ipcPlace.reply q hq) h1 h2 + +theorem JStage3TA.tw_bufA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) {b : U64} + (hb : TDWA st Z s b) (h : BufW Bf b) : False := by + obtain ⟨e, len, he, e1, e2⟩ := J.ipcPlace.buffer b h + exact J.tw_otherA hb he e1 e2 + +theorem JStage3TA.tw_scA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) {b : U64} + (hb : TDWA st Z s b) (h : ScFootA Z.C m σ b) : False := by + have hksz : ksSizeA = 788736 := rfl + rcases J.tw_valA hb with ⟨y, hy, y1, y2⟩ | ⟨k1, k2, -⟩ + · have := J.sr.scp.th_out h ((J.sr.thT y).2 hy) + have hsz : ObjKindA.size .tcb = 4096 := rfl + omega + · have := J.sr.scp.ks_out h + omega + +/-- A timer word is no other node word. -/ +theorem JStage3TA.tw_nodeA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) {b : U64} + (hb : TDWA st Z s b) {c o : U64} (hc : c ∈ Z.Cs) (ho : o.val < 4288) (h0 : o.val ≠ 88) + (h1 : o.val ≠ 104) (h2 : o.val ≠ 112) (e : b = L.nodeAt s c o) : False := by + obtain ⟨nn, -, hn, hcs⟩ := J.sr.csN + have hcl : c.val < 128 := by have := (hcs c).1 hc; omega + have hv := nodeAt_valSc (s := s) J.sr.ksFit hcl ho + rw [← e] at hv + have hksz : ksSizeA = 788736 := rfl + rcases J.tw_valA hb with ⟨y, hy, y1, y2⟩ | ⟨k1, k2, c', hc', o', ho', hcl', hv'⟩ + · have := J.sr.tcb_ks hy + omega + · omega + +/-- A timer word is not in the global words below the nodes. -/ +theorem JStage3TA.tw_ksLowA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) {b : U64} + (hb : TDWA st Z s b) (h1 : (L.ksb s).val ≤ b.val) + (h2 : b.val < (L.ksb s).val + 223488) : False := by + have hksz : ksSizeA = 788736 := rfl + rcases J.tw_valA hb with ⟨y, hy, y1, y2⟩ | ⟨k1, k2, -⟩ + · have := J.sr.tcb_ks hy + omega + · omega + +/-- **`SchedSRA` across timer writes** (module docstring). -/ +theorem JStage3TA.sr_twA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (w : WritesIn st s s' (TDWA st Z s)) : SchedSRA st Z m κ s' σ := by + have H := J.sr + have kp : ∀ b, Aligned b → (TDWA st Z s b → False) → L.mem s' b = L.mem s b := + fun b hb hn => w.mem_eq hb hn + have tK : ∀ t ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → (K.val < 3528 ∨ 3592 ≤ K.val) → + L.mem s' (wa t K) = L.mem s (wa t K) := fun t ht K h1 h8 h2 => + kp _ (field_aligned (H.core.tcbs t ht) h1 h8) (J.tw_tcbKA ht h1 h2) + have nK : ∀ c ∈ Z.Cs, ∀ o : U64, o.val < 4288 → o.val % 8 = 0 → o.val ≠ 88 → o.val ≠ 104 → o.val ≠ 112 → + L.mem s' (L.nodeAt s c o) = L.mem s (L.nodeAt s c o) := + fun c hc o ho h8 h0 h1 h2 => kp _ (nodeAt_aligned (H.core.nodes c hc) ho h8) fun hb => + J.tw_nodeA hb hc ho h0 h1 h2 rfl + have hkb := H.ksb_al + have hf := H.ksFit + have gK : ∀ K : U64, (K = 8#u64 ∨ K = 16#u64) → L.mem s' (wa (L.ksb s) K) = L.mem s (wa (L.ksb s) K) := by + intro K hK + have hK' : K.val = 8 ∨ K.val = 16 := by rcases hK with rfl | rfl <;> simp + have hv : (wa (L.ksb s) K).val = (L.ksb s).val + K.val := by rw [wa_valm]; unfold ksSizeA at hf; omega + exact kp _ (by unfold Aligned; rw [hv]; omega) fun hb => J.tw_ksLowA hb (by omega) (by omega) + have scK : ∀ b, Aligned b → ScFootA Z.C m σ b → L.mem s' b = L.mem s b := + fun b hb hs => kp b hb fun h => J.tw_scA h hs + have hcore : SchedCoreA st Z.T Z.Cs s' Z.Q Z.R := + H.core.agree w.1 (fun y hy => ⟨tK y hy 3480#u64 (by simp) (by simp) (by simp), + tK y hy 3488#u64 (by simp) (by simp) (by simp)⟩) + (fun c hc o h1 h2 h3 => nK c hc o h1 h2 (by omega) (by omega) (by omega)) + (fun y hy => tK y hy 3592#u64 (by simp) (by simp) (by simp)) + (fun y hy => tK y hy 3520#u64 (by simp) (by simp) (by simp)) + (fun y hy => tK y hy 3448#u64 (by simp) (by simp) (by simp)) + have e3464 : ∀ t ∈ Z.T, L.mem s' (scA t) = L.mem s (scA t) := + fun t ht => tK t ht 3464#u64 (by simp) (by simp) (by simp) + refine ⟨H.toSchedSRN.rebuildG (Q' := Z.Q) (R' := Z.R) w.1 w.2.1 + (fun c hc o h1 h2 _ _ => nK c hc o (by omega) h2 (by omega) (by omega) (by omega)) gK + (fun t ht K hK => by + rcases hK with rfl | rfl | rfl + · exact tK t ht _ (by simp) (by simp) (by simp) + · exact tK t ht _ (by simp) (by simp) (by simp) + · exact tK t ht _ (by simp) (by simp) (by simp)) + (H.scr.frame scK (fun _ _ => rfl)) H.scp (fun t ht x hx => ⟨x, hx, rfl, rfl⟩) + (fun t ht => by + obtain ⟨x, h1, h3⟩ := H.sch.tcb t ht + exact ⟨x, h1, by rw [e3464 t ht]; exact h3⟩) + (fun t ht h => by + rw [e3464 t ht] at h ⊢ + exact H.rest.scMem t ht h) + (SFixA.refl σ) ?_ ?_ H.actOn H.inv hcore H.rest.rq H.rest.rqDom H.rest.rel H.run + (fun c hc => scK _ (H.scCoreFoot hc).1 (H.scCoreFoot hc).2), H.rd⟩ + · rw [nK _ H.sch.cpuCs layout.node.REPROGRAM (by simp [layout.node.REPROGRAM]) + (by simp [layout.node.REPROGRAM]) (by simp [layout.node.REPROGRAM]) (by simp [layout.node.REPROGRAM]) (by simp [layout.node.REPROGRAM])] + exact H.rest.repr + · rw [nK _ H.sch.cpuCs layout.node.SCHED_ACTION (by simp [layout.node.SCHED_ACTION]) + (by simp [layout.node.SCHED_ACTION]) (by simp [layout.node.SCHED_ACTION]) (by simp [layout.node.SCHED_ACTION]) (by simp [layout.node.SCHED_ACTION])] + exact H.sch.act + +set_option maxHeartbeats 1000000 in +/-- **The record across a step writing only `TDWA` words** (`timer.remove`, `debug_remove`), given P1's `BlkOK` and +the run-VCPU words at the end. The spec state is unchanged. -/ +theorem JStage3TA.rec_tdwA [J7PpA st J7] [J7PpKA st J7] + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) {s' : S} + (blk' : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s') + (rv : RvSt st Z.T s s') (w0 : WritesIn st s s' (TDWA st Z s)) : + JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s' a σ m ∧ + WritesIn st s s' (SchedStepNWA (st := st) N E Rp Z m σ s) ∧ OSKeep st OS s s' ∧ + (∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → (K.val < 3528 ∨ 3592 ≤ K.val) → + L.mem s' (wa u K) = L.mem s (wa u K)) := by + have hP := J.iplaced + have kp : ∀ b, Aligned b → (TDWA st Z s b → False) → L.mem s' b = L.mem s b := + fun b hb hn => w0.mem_eq hb hn + have tK : ∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → (K.val < 3528 ∨ 3592 ≤ K.val) → + L.mem s' (wa u K) = L.mem s (wa u K) := fun u hu K h1 h8 h2 => + kp _ (field_aligned (J.sr.core.tcbs u hu) h1 h8) (J.tw_tcbKA hu h1 h2) + have w : WritesIn st s s' (SchedStepNWA (st := st) N E Rp Z m σ s) := w0.mono fun b hb => by + rcases hb with ⟨y, hy, K, k1, k2, rfl⟩ | ⟨c, hc, o, ho, rfl⟩ + · have hfit := (J.sr.core.tcbs y hy).fits + have hv : ∀ K' : U64, K'.val < 4096 → (wa y K').val = y.val + K'.val := fun K' h => by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have v1 := hv K (by omega) + have v2 := hv 3664#u64 (by simp) + have v3 := hv 3672#u64 (by simp) + have c1 : (3664#u64 : U64).val = 3664 := by simp + have c2 : (3672#u64 : U64).val = 3672 := by simp + refine Or.inl (Or.inr ⟨y, hy, by omega, by omega, fun e => ?_, fun e => ?_⟩) + · have := congrArg UScalar.val e + omega + · unfold runVcpuA at e + have := congrArg UScalar.val e + omega + · exact Or.inl (Or.inl (Or.inr (Or.inl (J.sr.node_sks hc (by omega))))) + have kb : L.ksb s' = L.ksb s := w.1 + have H2 : SchedSRA st Z m κ s' σ := J.sr_twA w0 + have rv' : RunVcpuInv st Z.T s' := J.runVcpu.rvSt rv + have sq : SchedQueuesInv st H.tcbs Z.Cs H.omega H s' Z.Q Z.R := by + obtain ⟨Q0, R0, h⟩ := blk'.1 + exact SchedQueuesInv.reQA h (by rw [J.tcbs]; exact H2.core) + have poolOff : PoolOff st H Z.Cs OS s' := fun i => by + have h0 := J.poolOff i + rw [vspace.asid_table.eq] at h0 ⊢ + have v := AtOff.v i + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.ent.ksw + have ev : (wa (L.ksb s) (AtOff i)).val = (L.ksb s).val + (AtOff i).val := by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have e : L.mem s' (wa (L.ksb s') (AtOff i)) = L.mem s (wa (L.ksb s) (AtOff i)) := by + rw [kb] + refine w.mem_eq ?_ (J.asid_offStepNA (by omega) (by omega)) + have h32 := J.j.1.ksbAl + show (wa (L.ksb s) (AtOff i)).val % 8 = 0 + omega + rw [e] + refine (WP.spec_ok _).2 fun hne asid => ?_ + have q := (WP.spec_ok _).1 h0 hne asid + exact ⟨q.al, q.om, q.sg, q.os, RegionOffNodes.ksb kb q.ks⟩ + have ep : EpStateRel Z.T E Rp Bf (L.mem s') σ := by + refine J.ep.frame hP.e (fun b hb hf => kp b hb fun htw => ?_) + ⟨fun p x hk => hk, fun p x hk => ⟨x, hk, rfl⟩, fun p x hk => ⟨x, hk, rfl, rfl, rfl, rfl, rfl⟩, rfl⟩ + rcases hf with ⟨e, he, h⟩ | ⟨r, hr, h⟩ | ⟨u, hu, ⟨h1, h2⟩ | h⟩ | h + · exact J.tw_ipcA htw (Or.inr (Or.inl ⟨e, he, by omega, by omega⟩)) + · exact J.tw_ipcA htw (Or.inr (Or.inr ⟨r, hr, by omega, by omega⟩)) + · unfold TOffN at h2 + exact J.tw_tcbA htw hu h1 (Or.inl (by omega)) + · unfold TL at h + exact J.tw_tcbA htw hu (by omega) (Or.inl (by omega)) + · exact J.tw_bufA htw h + have ntfn : NtfnStateRel N Z.T (L.mem s') σ := by + refine J.ntfn.frame hP.n (fun b hb hf => kp b hb fun htw => ?_) + ⟨fun p x hk => hk, fun p x hk => ⟨x, hk, rfl, rfl, rfl⟩⟩ + rcases hf with ⟨q, hq, h1, h2⟩ | ⟨u, hu, e⟩ + · exact J.tw_ipcA htw (Or.inl ⟨q, hq, h1, by omega⟩) + · rcases e with rfl | rfl | rfl | rfl | rfl | rfl + · rw [stA_eq] at htw; exact J.tw_tcbKA hu (by simp) (Or.inl (by simp)) htw + · rw [tBlockA_eq] at htw; exact J.tw_tcbKA hu (by simp) (Or.inl (by simp)) htw + · rw [badgeRegA_eq] at htw; exact J.tw_tcbKA hu (by simp) (Or.inl (by simp)) htw + · rw [prioA_eq'] at htw; exact J.tw_tcbKA hu (by simp) (Or.inl (by simp)) htw + · rw [nextEp_eq] at htw; exact J.tw_tcbKA hu (by simp) (Or.inl (by simp)) htw + · rw [prevEp_eq] at htw; exact J.tw_tcbKA hu (by simp) (Or.inl (by simp)) htw + have reply : ReplyStackRelA (L.mem s') Rp Z.C σ (scNameA m) stack := by + refine J.reply.frame (fun r hr => ?_) (fun c _ x n hk => ⟨x, n, hk, rfl⟩) (fun r _ x hk => ⟨x, hk, rfl⟩) + obtain ⟨r32, -, rfit⟩ := hP.e.rep r hr + have one : ∀ o : U64, o.val < 32 → o.val % 8 = 0 → L.mem s' (wa r o) = L.mem s (wa r o) := by + intro o o1 o2 + have v : (wa r o).val = r.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + refine kp _ ?_ fun htw => J.tw_ipcA htw (Or.inr (Or.inr ⟨r, hr, by omega, by omega⟩)) + show (wa r o).val % 8 = 0 + omega + have n16 : (layout.reply.NEXT).val = 16 := by unfold layout.reply.NEXT; rfl + have p8 : (layout.reply.PREV).val = 8 := by unfold layout.reply.PREV; rfl + exact ⟨one _ (by omega) (by omega), one _ (by omega) (by omega)⟩ + have buf : BufCover st Z.T Bf s' σ := + J.bufCover_wA w0 (fun u hu K hK htw => J.tw_tcbKA hu (by omega) (Or.inl (by omega)) htw) + (fun q z h => ⟨z, h, rfl, rfl⟩) + have mcp : McpRelA Z.T m (L.mem s') σ := fun u hu => by + obtain ⟨x, hx, he, hf⟩ := J.mcp u hu + have e : L.mem s' (mcpA u) = L.mem s (mcpA u) := tK u hu 3440#u64 (by simp) (by simp) (by simp) + have ef : L.mem s' (flagsA u) = L.mem s (flagsA u) := tK u hu 3456#u64 (by simp) (by simp) (by simp) + exact ⟨x, hx, by rw [e]; exact he, fun g => (hf g).keep ef rfl⟩ + have yr : YieldRelA Z.T Z.C m (L.mem s') σ := by + have k : ∀ u ∈ Z.T, L.mem s' (yieldA u) = L.mem s (yieldA u) := + fun u hu => tK u hu 3472#u64 (by simp) (by simp) (by simp) + refine ⟨fun u hu => ?_, fun u hu hnz => ?_, fun c hc y n hy p hp => ?_⟩ + · obtain ⟨x, hx, he⟩ := J.yieldRel.tcb u hu + exact ⟨x, hx, by rw [k u hu]; exact he⟩ + · rw [k u hu] at hnz ⊢ + exact J.yieldRel.toSc u hu hnz + · obtain ⟨u, hu, e1, e2⟩ := J.yieldRel.frm c hc y n hy p hp + exact ⟨u, hu, e1, by rw [k u hu]; exact e2⟩ + have hvr : ∀ sc ∈ Z.C, ∀ q ∈ stack sc, RBlk st s' q := + J.vr_specA blk' (fun _ _ h => h) (fun _ _ _ _ _ _ _ x0 _ hx0 _ => ⟨x0, hx0, rfl⟩) ep + (fun c hc => tK c hc 3360#u64 (by simp) (by simp) (by simp)) + have bq : BlkQueuedA Z.T E N (L.mem s') σ := + J.blkQ.keepA (fun u hu => by rw [Notif.tBlockA_eq]; exact tK u hu 3336#u64 (by simp) (by simp) (by simp)) + (fun u hu x' hx' => Or.inr ⟨x', hx', rfl⟩) (fun _ _ h => h) (fun _ _ h => h) + have j := J.j57_stepNWA w σ + exact ⟨JStage3TA.of_schedWrN (blkQ := bq) J (Q := Z.Q) (Rl := Z.R) (curOf := Z.curOf) (curScOf := Z.curScOf) + (s' := s') (a' := a) (σ' := σ) w (SpecSchedA.refl _) (SSzA.refl _) (QKeepA.refl _) rfl J.j.1.ar (fun _ _ => rfl) J.bufAl + (J.ent_stepNWA w blk') sq poolOff H2 J.scCov ep ntfn reply buf J.idleNoFault mcp yr J.sinv J.sidle rv' hvr + J.noSwitch J.sact j.1 j.2, + w, J.sigW_osKeepA w, tK⟩ + + +/-- **The record across `timer.remove t`** (module docstring). The spec state is unchanged. -/ +theorem JStage3TA.timerRm_recA [J7PpA st J7] [J7PpKA st J7] + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + {t : U64} (ht : t ∈ Z.T) : + timer.remove st s t ⦃ s' => + JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s' a σ m ∧ + WritesIn st s s' (SchedStepNWA (st := st) N E Rp Z m σ s) ∧ OSKeep st OS s s' ∧ + (∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → (K.val < 3528 ∨ 3592 ≤ K.val) → + L.mem s' (wa u K) = L.mem s (wa u K)) ⦄ := by + have blk := J.blkA + have htH : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + refine WP.spec_mono (WP.spec_and (Fl4ProofsA64.P1.timer.remove.tw (st := st) s t blk.tinv htH) + (Fl4ProofsA64.P1.timer.remove.rvst (st := st) s t blk.tinv htH)) ?_ + rintro s' ⟨⟨blk', w0⟩, -, rv⟩ + rw [J.tcbs] at w0 rv + exact J.rec_tdwA blk' rv (w0.mono fun b hb => by + rcases hb with ⟨y, hy, K, k1, k2, e⟩ | ⟨c, hc, o, ho, e⟩ + · exact Or.inl ⟨y, hy, K, k1, by omega, e⟩ + · exact Or.inr ⟨c, hc, o, by omega, e⟩) + +/-- **The record across `objects.debug_remove t`** (slice 2, step 4): the debug list has no spec counterpart, and +`debug_remove` writes only `DbgW` words (`DEBUG_NEXT` +3576, `DEBUG_PREV` +3584, the nodes' `DEBUG_TCBS` +88). -/ +theorem JStage3TA.debugRm_recA [J7PpA st J7] [J7PpKA st J7] + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + {t : U64} (ht : t ∈ Z.T) : + objects.debug_remove st s t ⦃ s' => + JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s' a σ m ∧ + WritesIn st s s' (SchedStepNWA (st := st) N E Rp Z m σ s) ∧ OSKeep st OS s s' ∧ + (∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → (K.val < 3528 ∨ 3592 ≤ K.val) → + L.mem s' (wa u K) = L.mem s (wa u K)) ⦄ := by + have blk := J.blkA + have htH : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + refine WP.spec_mono (WP.spec_and (Fl4ProofsA64.P1.objects.debug_remove.x (st := st) (X := ∅) s t blk htH) + (Fl4ProofsA64.P1.objects.debug_remove.rvst (st := st) s t blk htH)) ?_ + rintro s' ⟨⟨blk', -, w0⟩, rv⟩ + rw [J.tcbs] at w0 rv + have h88 : layout.node.DEBUG_TCBS = 88#u64 := by simp [layout.node.DEBUG_TCBS] + exact J.rec_tdwA blk' rv (w0.mono fun b hb => by + rcases hb with ⟨y, hy, e | e⟩ | ⟨c, hc, e⟩ + · exact Or.inl ⟨y, hy, 3576#u64, by simp, by simp, e⟩ + · exact Or.inl ⟨y, hy, 3584#u64, by simp, by simp, e⟩ + · exact Or.inr ⟨c, hc, layout.node.DEBUG_TCBS, Or.inl (by rw [h88]; simp), e⟩) + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelThrUnbindA.lean b/proofs/Fl4ProofsA64/P3/DelThrUnbindA.lean new file mode 100644 index 000000000..533d13c71 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelThrUnbindA.lean @@ -0,0 +1,326 @@ +/- +P3 (AArch64, delete hooks W3, contract §72): **the Thread finaliser, first stages**. + +* `JStage3TA.thrUnbind_recA`: `notification.unbind_notification t` (the TCB side of the unbinding) refines + `unbindNotification t` and `JStage3TA` holds again at the same ghosts. P1's `BndS` (`BlkOK`) gives the bound + notification's back link, so the program is `unbind_maybe_notification` of it (`ntfnBoundUnlinkA`). +* `JStage3TA.thrFront_recA`: the finaliser's first three calls: `remote_tcb_stall` (a no-op under [S] + `numNodes = 1`), `vcpu.detach` and `unbind_notification`. + +Premises: [S] `h1` (`numNodes = 1`), [P] `hCs`; [P] `hDet`, the conclusion of S4's frozen `vcpu_detach_stage3TA` +(`handoff/s4-detach-stmt.lean`) at `(s, a, σ, t)`, verbatim. Its own premises (among them `SchedValidA σ`, G1's F8) +are the caller's. +-/ +import Fl4ProofsA64.P3.Stage3QKeepPresA +import Fl4ProofsA64.P3.DelNtfnRecA +import Fl4ProofsA64.P3.DelScStallA +import Fl4ProofsA64.P3.TcbInvokeNtfnA +import Fl4ProofsA64.P3.Stage3SchedValidA +import Fl4ProofsA64.P3.VcpuPremisesA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 Fl4ProofsA64.P3.Endp +open Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Notif + +local notation "wa" => core.num.U64.wrapping_add + +noncomputable section + +/-- TCB-side and notification-side unbinding are the same spec step when the two links name each other. -/ +theorem unbindNotification_eq_maybeA {σ : Fl4SpecA64.State Unit} {n t : ObjRef} {x : Notification} {y : Tcb} + (hk : σ.kheap n = some (.notification x)) (ht : σ.kheap t = some (.tcb y)) (hb : x.ntfnBoundTcb = some t) + (hf : y.tcbBoundNotification = some n) : + unbindNotification (Z := Unit) t σ = unbindMaybeNotification (Z := Unit) n σ := by + have e1 : getTcbObjRef (Z := Unit) Tcb.tcbBoundNotification t σ = ({(some n, σ)}, False) := by + unfold getTcbObjRef + rw [threadGet_eqA _ ht, hf] + have e2 : getNtfnObjRef (Z := Unit) Notification.ntfnBoundTcb n σ = ({(some t, σ)}, False) := by + unfold getNtfnObjRef getSkObjRef + rw [nd_bind_oneSc (getSimpleKo_ntfnSc hk), hb] + rfl + unfold unbindNotification unbindMaybeNotification + rw [nd_bind_oneSc e1, nd_bind_oneSc e2] + simp only [NonDet.maybeM] + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J7 : JSlotA S} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + [J7PpA st J7] [J7PpKA st J7] + +set_option maxHeartbeats 1000000 in +/-- **The record across `unbind_notification t`** (module docstring). The write set is the notification's bound +word and the thread's. -/ +theorem JStage3TA.thrUnbind_recA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + {t : U64} (ht : t ∈ Z.T) : + notification.unbind_notification st s t ⦃ s' => ∃ σ', + unbindNotification (Z := Unit) (specPtr t) σ = ({((), σ')}, False) ∧ + JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s' a σ' m ∧ + WritesIn st s s' (fun b => b = ntfnBtA (L.mem s (boundA t)) ∨ b = boundA t) ∧ + (L.mem s (boundA t) ≠ 0#u64 → L.mem s (boundA t) ∈ N) ∧ + OSKeep st OS s s' ∧ SchedFrameA σ σ' ∧ TcbFinKeepA σ σ' ∧ RefK σ σ' ∧ + (∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → K.val ≠ 3368 → L.mem s' (wa u K) = L.mem s (wa u K)) ∧ + (∀ R7 : Set U64 → JSlotA S, J7 = J7LA st R7 → Std6KeepA st Z H OS Bf s σ s' σ') ⦄ := by + have hP := J.iplaced + have htT : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + have blk := J.blkA + have bnd : L.mem s (boundA t) ≠ 0#u64 → L.mem s (boundA t) ∈ H.ntfns := blk.2.2.1.2.2.bnd t htT + have bnd' : L.mem s (boundA t) ≠ 0#u64 → L.mem s (boundA t) ∈ N := fun h => + J.ipcHeapCov.ntfn _ (bnd h) + obtain ⟨y, hy, hrel⟩ := J.ep.tcb t ht + by_cases h0 : L.mem s (boundA t) = 0#u64 + · have e : notification.unbind_notification st s t = ok s := by + unfold notification.unbind_notification obj.Tcb.bound_ntfn + simp only [eq_of_spec_fd Fl4ProofsA64.P0.Consts.layout.tcb.BOUND_NTFN.spec, bind_ok, lift, L.read_eq] + change (if (L.mem s (boundA t)) != 0#u64 then _ else ok s) = ok s + rw [h0] + rfl + have hb : y.tcbBoundNotification = none := by + have : tBoundA t = boundA t := rfl + rw [hrel.bound, this, h0, Notif.optRef_zero] + have sp : unbindNotification (Z := Unit) (specPtr t) σ = ({((), σ)}, False) := by + have e1 : getTcbObjRef (Z := Unit) Tcb.tcbBoundNotification (specPtr t) σ = ({(none, σ)}, False) := by + unfold getTcbObjRef + rw [threadGet_eqA _ hy, hb] + unfold unbindNotification + rw [nd_bind_oneSc e1] + rfl + rw [e, WP.spec_ok] + exact ⟨σ, sp, J, WritesIn.refl _ _, bnd', OSKeep.reflA _, ⟨SpecSchedA.refl _, SSzA.refl _, rfl⟩, + fun _ z h => ⟨z, h, rfl, rfl, rfl⟩, RefK.refl _, fun _ _ _ _ _ _ => rfl, fun _ _ {_} _ h => h⟩ + · set p := L.mem s (boundA t) with hpdef + have hpN : p ∈ N := bnd' h0 + have hpH : p ∈ H.ntfns := bnd h0 + have back : L.mem s (ntfnBtA p) = t := blk.2.2.1.2.2.bs.tb t htT h0 + have eB : ntfnBtA p = Notif.nBoundA p := rfl + have hth := (J.sr.thT t).2 ht + have t0 : t ≠ 0#u64 := J.sr.place.nz t _ hth + obtain ⟨x, hk, hnrel⟩ := J.ntfn.ntfn p hpN + have back' : L.mem s (Notif.nBoundA p) = t := by rw [← eB]; exact back + have hxb : x.ntfnBoundTcb = some (specPtr t) := by rw [hnrel.bound, back']; exact Notif.optRef_ne t0 + have hyb : y.tcbBoundNotification = some (specPtr p) := by + have : tBoundA t = boundA t := rfl + rw [hrel.bound, this, ← hpdef]; exact Notif.optRef_ne h0 + have eun : notification.unbind_notification st s t = + ok (L.wr (L.wr s (ntfnBtA p) 0#u64) (boundA t) 0#u64) := + tcbUnbindNtfnWritesA s t p h0 hpdef.symm + obtain ⟨s2, σ2, ev2, sp2, w2, R2, E2, F2, K2, T2, S2, B2, q2⟩ := ntfnBoundUnlinkA (st := st) J.ntfn J.ep hP hpN + have hs2 : s2 = L.wr (L.wr s (ntfnBtA p) 0#u64) (boundA t) 0#u64 := by + obtain ⟨y0, hy0, -⟩ := J.ntfn.tcb t ht + obtain ⟨e1, -⟩ := irqUnbindNotificationTcb_someA (st := st) hk hy0 t0 hxb back' + rw [ev2] at e1 + exact ok_inj_ff e1 + have w' : WritesIn st s s2 (fun b => b = ntfnBtA p ∨ b = boundA t) := by + have aB : Aligned (boundA t) := (a_bound (hP.e.tcb t ht)).1 + have hna := hP.n.ntfn p hpN + have aN : Aligned (ntfnBtA p) := by + rw [eB, Notif.nBoundA_eq]; exact Notif.nfield_aligned hna (by simp) (by simp) + subst hs2 + refine ⟨by rw [L.ksb_wr, L.ksb_wr], by rw [L.cpu_wr, L.cpu_wr], fun b hb hn => ?_⟩ + rw [L.mem_wr_ne aB hb (fun h => hn (Or.inr (UScalar.eq_of_val_eq h).symm)), + L.mem_wr_ne aN hb (fun h => hn (Or.inl (UScalar.eq_of_val_eq h).symm))] + have sp : unbindNotification (Z := Unit) (specPtr t) σ = ({((), σ2)}, False) := by + rw [unbindNotification_eq_maybeA hk hy hxb hyb]; exact sp2 + have hc2 : ((), σ2) ∈ (unbindMaybeNotification (Z := Unit) (specPtr p) σ).1 := by rw [sp2]; exact rfl + rw [eun, WP.spec_ok, ← hs2] + refine ⟨σ2, sp, ?_, w', bnd', ?_⟩ + all_goals + have wF : WritesIn st s s2 (NtfnFinW st Z m σ s ∅ p) := w'.mono fun b hb => by + rcases hb with h | h + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr (Or.inl ⟨t, ht, h⟩)) + have w : WritesIn st s s2 (SchedStepNWA (st := st) N E Rp Z m σ s) := + wF.mono fun _ h => J.finW_stepNA hpN (Set.empty_subset _) h + swap + · refine ⟨J.sigW_osKeepA w, F2, T2, unbindMaybeNotification_refK (specPtr p) σ ((), σ2) hc2, + fun u hu K k1 k2 k3 => w'.mem_eq (field_aligned (J.sr.core.tcbs u hu) k1 k2) ?_, + fun R7 e => by subst e; exact fun {_} sub h => Std6GA.schedStepA J h sub w (J.sigW_osKeepA w) F2⟩ + have hfu := (J.sr.core.tcbs u hu).fits + have hft := (J.sr.core.tcbs t ht).fits + have hv : (wa u K).val = u.val + K.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + rintro (e | e) + · have hna := hP.n.ntfn p hpN + have vN : (ntfnBtA p).val = p.val + 24 := by + rw [eB, Notif.nBoundA_eq, Notif.nfield_val hna (by simp)]; simp + have := hP.n.disj p hpN u hu + have h' := congrArg (fun w : U64 => w.val) e + simp only [hv, vN] at h' + omega + · have v : (boundA t).val = t.val + 3368 := by + have e : boundA t = wa t 3368#u64 := rfl + rw [e, wa_valm, show (3368#u64 : U64).val = 3368 by simp]; exact Nat.mod_eq_of_lt (by omega) + have h' := congrArg (fun w : U64 => w.val) e + simp only [hv, v] at h' + by_cases eu : u = t + · subst eu; omega + · have := J.sr.place.apart u t .tcb .tcb ((J.sr.thT u).2 hu) hth eu + have hsz : ObjKindA.size .tcb = 4096 := rfl + omega + have blk' : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s2 := by + have h := Fl4ProofsA64.P1.notification.unbind_notification.blk (st := st) s t blk htT + rw [eun, WP.spec_ok, ← hs2] at h + exact h + have rv' : RunVcpuInv st Z.T s2 := by + have h := Fl4ProofsA64.P1.notification.unbind_notification.rvst (st := st) s t blk htT + rw [eun, WP.spec_ok, ← hs2, J.tcbs] at h + exact J.runVcpu.rvSt h + have H2 : SchedSRA st Z m κ s2 σ2 := by + refine J.sr.stable w' (fun b hb => ?_) S2 B2 + have hna := hP.n.ntfn p hpN + rcases hb with rfl | rfl + · have v := J.ntfnLink_ipcA hpN (b := ntfnBtA p) (Or.inr rfl) + rintro (⟨c, hc', o, ho, e⟩ | e | e | ⟨u, hu, K, hK, e⟩ | h) + · have hn := J.sr.node_in hc' ho + have hk := J.ipc_ksA (b := ntfnBtA p) (Or.inl ⟨p, hpN, v.1, by omega⟩) + rw [← e] at hn + omega + · have hk := J.ipc_ksA (b := ntfnBtA p) (Or.inl ⟨p, hpN, v.1, by omega⟩) + have hfit := J.sr.ksFit + have : (wa (L.ksb s) 8#u64).val = (L.ksb s).val + 8 := by + rw [wa_valm, show (8#u64 : U64).val = 8 by simp]; unfold ksSizeA at hfit; exact Nat.mod_eq_of_lt (by omega) + rw [e, this] at hk + unfold ksSizeA at hk; omega + · have hk := J.ipc_ksA (b := ntfnBtA p) (Or.inl ⟨p, hpN, v.1, by omega⟩) + have hfit := J.sr.ksFit + have : (wa (L.ksb s) 16#u64).val = (L.ksb s).val + 16 := by + rw [wa_valm, show (16#u64 : U64).val = 16 by simp]; unfold ksSizeA at hfit; exact Nat.mod_eq_of_lt (by omega) + rw [e, this] at hk + unfold ksSizeA at hk; omega + · have huH : H u = some .tcb := by + have : u ∈ H.tcbs := by rw [J.tcbs]; exact hu + exact this + have k4 : K.val < 4096 := by rcases hK with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> simp + have hfit := (J.sr.core.tcbs u hu).fits + have hv : (wa u K).val = u.val + K.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact J.ipc_off_hobjA (v := u) (kv := .tcb) huH (by simp) + ⟨by rw [e, hv]; omega, by rw [e, hv]; simp only [HKind.bits]; omega⟩ (Or.inl ⟨p, hpN, v.1, by omega⟩) + · have := J.sr.scp.th_out h (J.ipcPlace.notification p hpN) + simp only [ObjKindA.size] at this + omega + · have hfit := (J.sr.core.tcbs t ht).fits + have v : (boundA t).val = t.val + 3368 := by + have e : boundA t = wa t 3368#u64 := rfl + rw [e, wa_valm, show (3368#u64 : U64).val = 3368 by simp]; exact Nat.mod_eq_of_lt (by omega) + have hks := J.sr.tcb_ks ht + have hsz : ObjKindA.size .tcb = 4096 := rfl + rintro (⟨c, hc', o, ho, e⟩ | e | e | ⟨u, hu, K, hK, e⟩ | h) + · have hn := J.sr.node_in hc' ho + rw [← e, v] at hn + unfold ksSizeA at hks hn; omega + · have hfit' := J.sr.ksFit + have : (wa (L.ksb s) 8#u64).val = (L.ksb s).val + 8 := by + rw [wa_valm, show (8#u64 : U64).val = 8 by simp]; unfold ksSizeA at hfit'; exact Nat.mod_eq_of_lt (by omega) + have h' := congrArg (fun w : U64 => w.val) e + simp only [v, this] at h' + unfold ksSizeA at hks; omega + · have hfit' := J.sr.ksFit + have : (wa (L.ksb s) 16#u64).val = (L.ksb s).val + 16 := by + rw [wa_valm, show (16#u64 : U64).val = 16 by simp]; unfold ksSizeA at hfit'; exact Nat.mod_eq_of_lt (by omega) + have h' := congrArg (fun w : U64 => w.val) e + simp only [v, this] at h' + unfold ksSizeA at hks; omega + · have k4 : K.val < 4096 := by rcases hK with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> simp + have ne : K.val ≠ 3368 := by rcases hK with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> simp + have hft := (J.sr.core.tcbs u hu).fits + have hv : (wa u K).val = u.val + K.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have h' := congrArg (fun w : U64 => w.val) e + simp only [v, hv] at h' + by_cases eu : u = t + · subst eu; omega + · have := J.sr.place.apart u t .tcb .tcb ((J.sr.thT u).2 hu) hth eu + omega + · have := J.sr.scp.th_out h hth + omega + have tw : ∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → + (K.val ≠ 3328 ∧ K.val ≠ 3368 ∧ K.val ≠ 3464 ∧ K.val ≠ 3480 ∧ K.val ≠ 3488 ∧ K.val ≠ 3496 ∧ + K.val ≠ 3504 ∧ K.val ≠ 3592) → L.mem s2 (wa u K) = L.mem s (wa u K) := + fun u hu K h1 h2 h3 => wF.mem_eq (field_aligned (J.sr.core.tcbs u hu) h1 h2) + (J.tcbWord_offFinA hpN (Set.empty_subset _) hu h1 h3) + have sq : SchedQueuesInv st H.tcbs Z.Cs H.omega H s2 Z.Q Z.R := by + obtain ⟨Q0, R0, h⟩ := blk'.1 + exact SchedQueuesInv.reQA h (by rw [J.tcbs]; exact H2.core) + have kb : L.ksb s2 = L.ksb s := w.1 + have poolOff : PoolOff st H Z.Cs OS s2 := fun i => by + have h0 := J.poolOff i + rw [vspace.asid_table.eq] at h0 ⊢ + have v := AtOff.v i + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.ent.ksw + have ev : (wa (L.ksb s) (AtOff i)).val = (L.ksb s).val + (AtOff i).val := by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have e : L.mem s2 (wa (L.ksb s2) (AtOff i)) = L.mem s (wa (L.ksb s) (AtOff i)) := by + rw [kb] + refine w.mem_eq ?_ (J.asid_offStepNA (by omega) (by omega)) + have h32 := J.j.1.ksbAl + show (wa (L.ksb s) (AtOff i)).val % 8 = 0 + omega + rw [e] + refine (WP.spec_ok _).2 fun hne asid => ?_ + have q := (WP.spec_ok _).1 h0 hne asid + exact ⟨q.al, q.om, q.sg, q.os, RegionOffNodes.ksb kb q.ks⟩ + have U : RefK σ σ2 := unbindMaybeNotification_refK (specPtr p) σ ((), σ2) hc2 + have K := K2 + have TK := T2 + have reply : ReplyStackRelA (L.mem s2) Rp Z.C σ2 (scNameA m) stack := by + refine J.reply.ntfnFr K fun r hr => ?_ + obtain ⟨r32, -, rfit⟩ := hP.e.rep r hr + have one : ∀ o : U64, o.val < 32 → o.val % 8 = 0 → L.mem s2 (wa r o) = L.mem s (wa r o) := by + intro o o1 o2 + have v : (wa r o).val = r.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + refine wF.mem_eq ?_ (J.reply_offFinA hpN (Set.empty_subset _) hr (by omega) (by omega)) + show (wa r o).val % 8 = 0 + omega + have n16 : (layout.reply.NEXT).val = 16 := by unfold layout.reply.NEXT; rfl + have p8 : (layout.reply.PREV).val = 8 := by unfold layout.reply.PREV; rfl + exact ⟨one _ (by omega) (by omega), one _ (by omega) (by omega)⟩ + have mcp : McpRelA Z.T m (L.mem s2) σ2 := + J.mcp.ntfnFr K fun u hu => ⟨tw u hu 3440#u64 (by simp) (by simp) (by simp), + tw u hu 3456#u64 (by simp) (by simp) (by simp)⟩ + have yr : YieldRelA Z.T Z.C m (L.mem s2) σ2 := + J.yieldRel.ntfnFr K fun u hu => tw u hu 3472#u64 (by simp) (by simp) (by simp) + have buf : BufCover st Z.T Bf s2 σ2 := + J.bufCover_wA wF (fun u hu K hK => J.tcbWord_offFinA hpN (Set.empty_subset _) hu (by omega) (by omega)) + (fun q z h => by + obtain ⟨z', hz, -, b, c⟩ := TK q z h + exact ⟨z', hz, b, c⟩) + have bufAl : UtBufAlA Z.T σ2 := fun u hu z hz => by + obtain ⟨x0, hx0, -⟩ := J.ntfn.tcb u hu + obtain ⟨z', hz', -, -, e⟩ := TK _ _ hx0 + rw [hz] at hz'; cases hz' + rw [e]; exact J.bufAl u hu x0 hx0 + have idle : IdleNoFaultA m σ2 := fun c hc' => by + obtain ⟨x0, hx0, hf⟩ := J.idleNoFault c hc' + obtain ⟨z, hz, e, -⟩ := TK _ _ hx0 + exact ⟨z, hz, e.trans hf⟩ + have scCov : ScCovA Z.C m σ2 := fun q z k h => by + obtain ⟨z', hz⟩ := F2.2.1 q z k h + exact J.scCov q z' k hz + have inv : LoopInvA σ2 m n := LoopInvA.refK ⟨J.sinv, J.sidle, J.sact⟩ U + have j := J.j57_stepNWA w σ2 + have nsw : NoSwA σ2 := fun u c => J.noSwitch u (by rw [U.1] at c; exact c) + have hvr : ∀ sc ∈ Z.C, ∀ q ∈ stack sc, RBlk st s2 q := by + refine J.vr_specA blk' (fun _ _ h => h) (fun sc _ q _ c _ _ x0 o hx0 _ => ?_) E2 + (fun c hc => tw c hc 3360#u64 (by simp) (by simp) (by simp)) + obtain ⟨z, hz, k⟩ := (U.2.1 (specPtr c)).tcbF hx0 + exact ⟨z, hz, k.1⟩ + have bq : BlkQueuedA Z.T E N (L.mem s2) σ2 := by + have M := unbindMaybeNotification_blkA (specPtr p) σ _ hc2 + refine J.blkQ.finA M (fun u hu => ?_) (fun u hu => ?_) (fun z hz => ?_) (fun z q hz ho r hr => ?_) + · obtain ⟨z, hz, -⟩ := J.ntfn.tcb u hu + exact ⟨z, hz⟩ + · rw [Notif.tBlockA_eq]; exact tw u hu 3336#u64 (by simp) (by simp) (by simp) + · rw [hk] at hz; cases hz + · rw [hk] at hz; cases hz + obtain ⟨x', hx', e⟩ := q2 x hk + exact Or.inr ⟨x', q, hx', e.trans ho, hr⟩ + exact JStage3TA.of_schedWrN (blkQ := bq) J (Q := Z.Q) (Rl := Z.R) (curOf := Z.curOf) (curScOf := Z.curScOf) + (s' := s2) (a' := a) (σ' := σ2) w F2.1 F2.2.1 (Sched.qk_of hc2) F2.2.2 J.j.1.ar (fun _ _ => rfl) bufAl (J.ent_stepNWA w blk') + sq poolOff H2 scCov E2 R2 reply buf idle mcp yr inv.sinv inv.sidle rv' hvr nsw inv.sact j.1 j.2 + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelTimeSpecA.lean b/proofs/Fl4ProofsA64/P3/DelTimeSpecA.lean new file mode 100644 index 000000000..12f8703e1 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelTimeSpecA.lean @@ -0,0 +1,398 @@ +/- +P3 (AArch64, delete hooks, contract §39): **the VSpace finalisers keep the spec's time** (`FinTimeSpecA` for the six +VSpace kinds; `FinHookKA.timeA`'s spec side). + +* `TmA σ σ'`: the current time and the machine's last time and time-oracle position are kept, and (Task 0) the + current SC's entry facts (`CurKeepA`: a written object is no TCB or SC, or a TCB / SC write that keeps the SC + binding / activity and head refill, `setObject_tmA`, `tcbW_tmA`); **`NdTmA m`**: every + outcome of `m` is a `TmA` step (no failure claim, unlike `NdOkA`), with its closure lemmas (`bind`, the `NonDetE` + `run` forms, `catch`, `mapM`) and `NdTmA.ofOk` from an `NdOkA` relation that implies `TmA` (`VmA`). +* `NdTmA` of the spec's VSpace operations (`unmapPage`, `unmapPageTable`, `deleteAsid`, `deleteAsidPool`, + `extUnmapPage`, `extUnmapTable`, `setVmRoot`), **`fl4FinaliseCap_tmA`** on every arch cap but a VCPU's, and + **`finTimeSpec_vsA`**: `FinTimeSpecA fl4FinaliseCap k` for the six VSpace kinds. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.DelPpLiftA +import Fl4ProofsA64.P3.DelAsidSpecA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4Spec Fl4SpecA64 + +noncomputable section + +/-- **A step keeping the spec's time** (module docstring). -/ +def TmA (σ σ' : State Unit) : Prop := + σ'.curTime = σ.curTime ∧ σ'.machineState.lastMachineTime = σ.machineState.lastMachineTime ∧ + σ'.machineState.timeState = σ.machineState.timeState ∧ CurKeepA σ σ' + +theorem tmA_refl (σ : State Unit) : TmA σ σ := ⟨rfl, rfl, rfl, CurKeepA.refl σ⟩ + +theorem tmA_trans {a b c : State Unit} (h1 : TmA a b) (h2 : TmA b c) : TmA a c := + ⟨h2.1.trans h1.1, h2.2.1.trans h1.2.1, h2.2.2.1.trans h1.2.2.1, h1.2.2.2.trans h2.2.2.2⟩ + +/-- A step keeping the time fields, the heap, the current SC, the consumed time and the release queue. -/ +theorem tmA_eqA {σ σ' : State Unit} (h1 : σ'.curTime = σ.curTime) + (h2 : σ'.machineState.lastMachineTime = σ.machineState.lastMachineTime) + (h3 : σ'.machineState.timeState = σ.machineState.timeState) (hk : σ'.kheap = σ.kheap) + (hc : σ'.curSc = σ.curSc) (hn : σ'.consumedTime = σ.consumedTime) (hr : σ'.releaseQueue = σ.releaseQueue) : + TmA σ σ' := ⟨h1, h2, h3, CurKeepA.of_eq hk hc hn hr⟩ + +/-- (Task 0) **A heap write of a TCB** whose SC binding, if any, was the old TCB's. -/ +theorem tcbW_tmA {σ : State Unit} {p : ObjRef} {x x' : Tcb} (hx : σ.kheap p = some (.tcb x)) + (hf : ∀ c, x'.tcbSchedContext = some c → x.tcbSchedContext = some c) : + TmA σ { σ with kheap := Function.update σ.kheap p (some (.tcb x')) } := by + refine ⟨rfl, rfl, rfl, ⟨rfl, rfl, fun _ h => h, fun y k h a => ?_, fun t x1 h e _ => ?_⟩⟩ + · dsimp only at h + by_cases e : σ.curSc = p + · rw [e, Function.update_self] at h; cases h + · rw [Function.update_of_ne e] at h; exact ⟨y, k, h, a, rfl⟩ + · dsimp only at h + by_cases e' : t = p + · subst e' + rw [Function.update_self] at h + cases h + exact ⟨x, hx, hf _ e⟩ + · rw [Function.update_of_ne e'] at h; exact ⟨x1, h, e⟩ + +/-- (Task 0) **A heap write of an SC** whose activity, if active, and head refill were the old SC's. -/ +theorem scW_tmA {σ : State Unit} {p : ObjRef} {y : SchedContext} {n : Nat} {y' : SchedContext} + (hy : σ.kheap p = some (.schedContext y n)) + (hf : scActive y' = true → scActive y = true ∧ refillHd y = refillHd y') : + TmA σ { σ with kheap := Function.update σ.kheap p (some (.schedContext y' n)) } := by + refine ⟨rfl, rfl, rfl, ⟨rfl, rfl, fun _ h => h, fun y1 k h a => ?_, fun t x1 h e _ => ?_⟩⟩ + · dsimp only at h + by_cases e : σ.curSc = p + · rw [e, Function.update_self] at h + cases h + rw [e] + exact ⟨y, n, hy, (hf a).1, (hf a).2⟩ + · rw [Function.update_of_ne e] at h; exact ⟨y1, k, h, a, rfl⟩ + · dsimp only at h + by_cases e' : t = p + · subst e'; rw [Function.update_self] at h; cases h + · rw [Function.update_of_ne e'] at h; exact ⟨x1, h, e⟩ + +/-- **Every outcome keeps the time** (module docstring). -/ +def NdTmA {α : Type} (m : NonDet (State Unit) α) : Prop := ∀ σ r, r ∈ (m σ).1 → TmA σ r.2 + +section +variable {α β ε : Type} + +theorem NdTmA.ro {m : NonDet (State Unit) α} (h : ∀ σ r, r ∈ (m σ).1 → r.2 = σ) : NdTmA m := fun σ r hr => by + rw [h σ r hr]; exact tmA_refl σ + +theorem NdTmA.pure (a : α) : NdTmA (Pure.pure a : NonDet (State Unit) α) := + NdTmA.ro fun _ _ h => by rw [Set.mem_singleton_iff.1 h] + +theorem NdTmA.npure (a : α) : NdTmA (NonDet.pure a : NonDet (State Unit) α) := NdTmA.pure a + +theorem NdTmA.bind {m : NonDet (State Unit) α} {f : α → NonDet (State Unit) β} (hm : NdTmA m) + (hf : ∀ a, NdTmA (f a)) : NdTmA (m >>= f) := fun σ r hr => by + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + exact tmA_trans (hm σ x hx) (hf x.1 x.2 r hr) + +theorem NdTmA.nbind {m : NonDet (State Unit) α} {f : α → NonDet (State Unit) β} (hm : NdTmA m) + (hf : ∀ a, NdTmA (f a)) : NdTmA (NonDet.bind m f) := NdTmA.bind hm hf + +theorem NdTmA.fail : NdTmA (NonDet.fail : NonDet (State Unit) α) := fun _ _ h => h.elim + +theorem NdTmA.gets (g : State Unit → α) : NdTmA (NonDet.gets g) := + NdTmA.ro fun _ _ h => by rw [Set.mem_singleton_iff.1 h] + +theorem NdTmA.get : NdTmA (NonDet.get : NonDet (State Unit) (State Unit)) := + NdTmA.ro fun _ _ h => by rw [Set.mem_singleton_iff.1 h] + +theorem NdTmA.assert (P : Prop) : NdTmA (NonDet.assert P : NonDet (State Unit) Unit) := + NdTmA.ro fun _ _ h => by rw [h.2] + +theorem NdTmA.assertOpt (o : Option α) : NdTmA (NonDet.assertOpt o : NonDet (State Unit) α) := by + cases o + · exact NdTmA.fail + · exact NdTmA.npure _ + +theorem NdTmA.getsThe (g : State Unit → Option α) : NdTmA (NonDet.getsThe g) := fun σ r hr => + NdTmA.assertOpt (g σ) σ r hr + +theorem NdTmA.getsMap {γ : Type} (g : State Unit → γ → Option α) (p : γ) : NdTmA (NonDet.getsMap g p) := + NdTmA.nbind (NdTmA.gets _) fun _ => NdTmA.assertOpt _ + +theorem NdTmA.selectF (S : Set α × Prop) : NdTmA (NonDet.selectF S : NonDet (State Unit) α) := + NdTmA.ro fun _ _ h => h.2 + +theorem NdTmA.modify {g : State Unit → State Unit} (h : ∀ σ, TmA σ (g σ)) : NdTmA (NonDet.modify g) := + fun σ r hr => by rw [Set.mem_singleton_iff.1 hr]; exact h σ + +theorem NdTmA.when {P : Prop} {inst : Decidable P} {m : NonDet (State Unit) Unit} (h : NdTmA m) : NdTmA (NonDet.when P m) := by + unfold NonDet.when; split + · exact h + · exact NdTmA.npure _ + +theorem NdTmA.mapM {f : α → NonDet (State Unit) β} (h : ∀ a, NdTmA (f a)) : + ∀ l : List α, NdTmA (NonDet.mapM f l) + | [] => NdTmA.npure _ + | a :: l => NdTmA.nbind (h a) fun _ => NdTmA.nbind (NdTmA.mapM h l) fun _ => NdTmA.npure _ + +theorem NdTmA.ofOk {Q : State Unit → State Unit → Prop} (hQ : ∀ a b, Q a b → TmA a b) + {m : NonDet (State Unit) α} (h : NdOkA Q m) : NdTmA m := fun σ r hr => hQ _ _ ((h σ).2.2 r hr) + +theorem VmA.tmA {σ σ' : State Unit} (h : VmA σ σ') : TmA σ σ' := by + obtain ⟨mr, am, vt, nv, rfl⟩ := h; exact tmA_eqA rfl rfl rfl rfl rfl rfl rfl + +/-! ### The `NonDetE` forms (on `run`) -/ + +theorem NdTmA.liftE {m : NonDet (State Unit) α} (h : NdTmA m) : NdTmA (NonDetE.liftE m : NonDetE (State Unit) ε α).run := by + unfold NonDetE.liftE + rw [ExceptT.run_lift] + exact NdTmA.bind h fun _ => NdTmA.pure _ + +theorem NdTmA.runBind {x : NonDetE (State Unit) ε α} {f : α → NonDetE (State Unit) ε β} (hx : NdTmA x.run) + (hf : ∀ a, NdTmA (f a).run) : NdTmA (x >>= f).run := by + rw [ExceptT.run_bind] + refine NdTmA.bind hx fun r => ?_ + cases r with + | error e => exact NdTmA.pure _ + | ok a => exact hf a + +theorem NdTmA.runPure (a : α) : NdTmA (Pure.pure a : NonDetE (State Unit) ε α).run := by + rw [ExceptT.run_pure]; exact NdTmA.pure _ + +theorem NdTmA.returnOk (a : α) : NdTmA (NonDetE.returnOk a : NonDetE (State Unit) ε α).run := NdTmA.runPure a + +theorem NdTmA.throwError (e : ε) : NdTmA (NonDetE.throwError e : NonDetE (State Unit) ε α).run := by + unfold NonDetE.throwError + show NdTmA (ExceptT.run (throw e : ExceptT ε (NonDet (State Unit)) α)) + exact NdTmA.pure _ + +theorem NdTmA.throwOpt (e : ε) (o : Option α) : NdTmA (NonDetE.throwOpt e o : NonDetE (State Unit) ε α).run := by + cases o + · exact NdTmA.throwError e + · exact NdTmA.runPure _ + +theorem NdTmA.unlessE {P : Prop} {inst : Decidable P} {m : NonDetE (State Unit) ε Unit} (h : NdTmA m.run) : + NdTmA (NonDetE.unlessE P m).run := by + unfold NonDetE.unlessE; split + · exact NdTmA.runPure _ + · exact h + +theorem NdTmA.whenE {P : Prop} {inst : Decidable P} {m : NonDetE (State Unit) ε Unit} (h : NdTmA m.run) : + NdTmA (NonDetE.whenE P m).run := by + unfold NonDetE.whenE; split + · exact h + · exact NdTmA.runPure _ + +theorem NdTmA.catch {m : NonDetE (State Unit) ε α} {h : ε → NonDet (State Unit) α} (hm : NdTmA m.run) + (hh : ∀ e, NdTmA (h e)) : NdTmA (NonDetE.catch m h) := by + unfold NonDetE.catch + refine NdTmA.nbind hm fun r => ?_ + cases r with + | error e => exact hh e + | ok a => exact NdTmA.npure a + +end + +/-! ## The spec's VSpace operations -/ + +theorem getObject_tmA (p : ObjRef) : NdTmA (getObject (Z := Unit) p) := NdTmA.ro fun _ _ h => (getObject_mem h).1 + +/-- (Task 0: a write of no TCB and no SC.) -/ +theorem setObject_tmA (p : ObjRef) (o : KernelObject) (ht : ∀ x, o ≠ .tcb x) (hs : ∀ y n, o ≠ .schedContext y n) : + NdTmA (setObject (Z := Unit) p o) := fun σ r hr => by + obtain ⟨_, -, h⟩ := setObject_mem hr; rw [h] + refine ⟨rfl, rfl, rfl, ⟨rfl, rfl, fun _ h => h, fun y k h a => ?_, fun t x1 h e _ => ?_⟩⟩ + · dsimp only at h + by_cases e : σ.curSc = p + · rw [e, Function.update_self] at h; exact absurd (Option.some.inj h) (hs y k) + · rw [Function.update_of_ne e] at h; exact ⟨y, k, h, a, rfl⟩ + · dsimp only at h + by_cases e' : t = p + · subst e'; rw [Function.update_self] at h; exact absurd (Option.some.inj h) (ht x1) + · rw [Function.update_of_ne e'] at h; exact ⟨x1, h, e⟩ + +macro "tm_auto" : tactic => `(tactic| repeat' first + | exact NdTmA.pure _ | exact NdTmA.npure _ | exact NdTmA.gets _ | exact NdTmA.get | exact NdTmA.assert _ + | exact NdTmA.assertOpt _ | exact NdTmA.getsThe _ | exact NdTmA.getsMap _ _ | exact NdTmA.selectF _ + | exact NdTmA.fail | exact getObject_tmA _ | exact setObject_tmA _ _ (fun _ h => nomatch h) (fun _ _ h => nomatch h) + | exact NdTmA.runPure _ | exact NdTmA.returnOk _ | exact NdTmA.throwError _ | exact NdTmA.throwOpt _ _ + | exact NdTmA.modify (fun _ => tmA_eqA rfl rfl rfl rfl rfl rfl rfl) + | refine NdTmA.liftE ?_ | refine NdTmA.unlessE ?_ | refine NdTmA.whenE ?_ | refine NdTmA.when ?_ + | refine NdTmA.catch ?_ (fun _ => ?_) + | refine NdTmA.runBind ?_ (fun _ => ?_) + | refine NdTmA.bind ?_ (fun _ => ?_) | refine NdTmA.nbind ?_ (fun _ => ?_) + | split) + +theorem doMachineOp_tmA (g : MachineRestMonad Unit) : NdTmA (doMachineOp (Z := Unit) (machineOpLift g)) := + NdTmA.ofOk (fun _ _ h => VmA.tmA h) (doMachineOp_vmA g) + +theorem invalidateTlbByAsid_tmA (asid : Asid) : NdTmA (invalidateTlbByAsid (Z := Unit) asid) := + NdTmA.ofOk (fun _ _ h => VmA.tmA h) (invalidateTlbByAsid_vmA asid) + +theorem invalidateAsidEntry_tmA (asid : Asid) : NdTmA (invalidateAsidEntry (Z := Unit) asid) := + NdTmA.ofOk (fun _ _ h => VmA.tmA h) (invalidateAsidEntry_vmA asid) + +theorem invalidateTlbByAsidVa_tmA (asid : Asid) (v : VSpaceRef) : NdTmA (invalidateTlbByAsidVa (Z := Unit) asid v) := by + unfold invalidateTlbByAsidVa + refine NdTmA.bind (NdTmA.ofOk (fun _ _ h => VmA.tmA h) (loadVmid_vmA asid)) fun mv => ?_ + cases mv with + | none => exact NdTmA.pure _ + | some vmid => exact doMachineOp_tmA _ + +theorem findVspaceForAsid_tmA (asid : Asid) : NdTmA (findVspaceForAsid (Z := Unit) asid).run := + NdTmA.ofOk (fun _ _ h => VmA.tmA h) (findVspaceForAsid_vmA asid) + +theorem getCap_tmA (sl : CslotPtr) : NdTmA (getCap (Z := Unit) sl) := by + obtain ⟨oref, cref⟩ := sl + unfold getCap + tm_auto + +theorem setVmRoot_tmA (t : ObjRef) : NdTmA (setVmRoot (Z := Unit) t) := by + unfold setVmRoot + refine NdTmA.bind (NdTmA.pure _) fun _ => NdTmA.bind (getCap_tmA _) fun c => ?_ + refine NdTmA.catch ?_ fun _ => NdTmA.ofOk (fun _ _ h => VmA.tmA h) setGlobalUserVspace_vmA + split + · refine NdTmA.runBind (findVspaceForAsid_tmA _) fun _ => NdTmA.runBind ?_ fun _ => + NdTmA.liftE (NdTmA.ofOk (fun _ _ h => VmA.tmA h) (armContextSwitch_vmA _ _)) + exact NdTmA.whenE (NdTmA.throwError _) + · exact NdTmA.throwError _ + +theorem setAsidPool_tmA (p : ObjRef) (pool : AsidPool) : NdTmA (setAsidPool (Z := Unit) p pool) := by + unfold setAsidPool; tm_auto + +theorem storePte_tmA (t : PtType) (p : ObjRef) (pte : PTE) : NdTmA (storePte (Z := Unit) t p pte) := by + unfold storePte setPt; tm_auto + +theorem ptLookupFromLevelN_tmA : ∀ (n : Nat) (lv : VmLevel) (p : ObjRef) (v : VSpaceRef) (t : ObjRef), + NdTmA (ptLookupFromLevelN (Z := Unit) n lv p v t).run + | 0, lv, p, v, t => by unfold ptLookupFromLevelN; tm_auto + | n + 1, lv, p, v, t => by + unfold ptLookupFromLevelN + tm_auto + all_goals exact ptLookupFromLevelN_tmA n _ _ _ _ + +theorem deleteAsid_tmA (asid : Asid) (pt : ObjRef) : NdTmA (deleteAsid (Z := Unit) asid pt) := by + unfold deleteAsid + refine NdTmA.bind (NdTmA.gets _) fun o => ?_ + split + · exact NdTmA.pure _ + · refine NdTmA.bind (NdTmA.getsMap _ _) fun _ => NdTmA.when ?_ + exact NdTmA.bind (invalidateTlbByAsid_tmA _) fun _ => NdTmA.bind (invalidateAsidEntry_tmA _) fun _ => + NdTmA.bind (NdTmA.getsMap _ _) fun _ => NdTmA.bind (NdTmA.pure _) fun _ => + NdTmA.bind (setAsidPool_tmA _ _) fun _ => NdTmA.bind (NdTmA.gets _) fun _ => setVmRoot_tmA _ + +theorem deleteAsidPool_tmA (base : Asid) (ptr : ObjRef) : NdTmA (deleteAsidPool (Z := Unit) base ptr) := by + unfold deleteAsidPool + refine NdTmA.bind (NdTmA.assert _) fun _ => NdTmA.bind (NdTmA.gets _) fun _ => NdTmA.when ?_ + refine NdTmA.bind (NdTmA.getsMap _ _) fun _ => NdTmA.bind (NdTmA.mapM (fun _ => NdTmA.when ?_) _) fun _ => + NdTmA.bind (NdTmA.pure _) fun _ => NdTmA.bind (NdTmA.modify fun _ => tmA_eqA rfl rfl rfl rfl rfl rfl rfl) fun _ => + NdTmA.bind (NdTmA.gets _) fun _ => setVmRoot_tmA _ + exact NdTmA.bind (invalidateTlbByAsid_tmA _) fun _ => invalidateAsidEntry_tmA _ + +theorem unmapPageTable_tmA (asid : Asid) (v : VSpaceRef) (pt : ObjRef) : + NdTmA (unmapPageTable (Z := Unit) asid v pt) := by + unfold unmapPageTable + refine NdTmA.catch ?_ fun _ => NdTmA.pure _ + refine NdTmA.runBind (findVspaceForAsid_tmA _) fun _ => NdTmA.runBind ?_ fun x => ?_ + · unfold ptLookupFromLevel; exact ptLookupFromLevelN_tmA _ _ _ _ _ + · obtain ⟨sl, lv⟩ := x + exact NdTmA.runBind (NdTmA.liftE (storePte_tmA _ _ _)) fun _ => + NdTmA.runBind (NdTmA.liftE (doMachineOp_tmA _)) fun _ => NdTmA.liftE (invalidateTlbByAsid_tmA _) + +theorem unmapPage_tmA (sz : VMPageSize) (asid : Asid) (v : VSpaceRef) (p : ObjRef) : + NdTmA (unmapPage (Z := Unit) sz asid v p) := by + unfold unmapPage + refine NdTmA.catch ?_ fun _ => NdTmA.pure _ + refine NdTmA.runBind (findVspaceForAsid_tmA _) fun _ => NdTmA.runBind (NdTmA.liftE (NdTmA.getsThe _)) fun x => ?_ + obtain ⟨lv, sl⟩ := x + exact NdTmA.runBind (NdTmA.unlessE (NdTmA.throwError _)) fun _ => + NdTmA.runBind (NdTmA.liftE (NdTmA.getsMap _ _)) fun _ => + NdTmA.runBind (NdTmA.unlessE (NdTmA.throwError _)) fun _ => + NdTmA.runBind (NdTmA.liftE (storePte_tmA _ _ _)) fun _ => + NdTmA.runBind (NdTmA.liftE (doMachineOp_tmA _)) fun _ => NdTmA.liftE (invalidateTlbByAsidVa_tmA _ _) + +theorem storeExtSlot_tmA (sl : ExtSlot) (pte : PTE) : NdTmA (storeExtSlot (Z := Unit) sl pte) := by + cases sl with + | root r i => unfold storeExtSlot; tm_auto + | pt p => exact storePte_tmA _ _ _ + +theorem findExtRoot_tmA (k : ExtRootKind) (asid : Asid) : NdTmA (findExtRoot (Z := Unit) k asid).run := by + unfold findExtRoot findAnyRootForAsid; tm_auto + +theorem extUnmapPage_tmA (k : ExtRootKind) (sz : VMPageSize) (asid : Asid) (v : VSpaceRef) (p : ObjRef) : + NdTmA (extUnmapPage (Z := Unit) k sz asid v p) := by + unfold extUnmapPage + refine NdTmA.catch ?_ fun _ => NdTmA.pure _ + refine NdTmA.runBind (NdTmA.unlessE (NdTmA.throwError _)) fun _ => NdTmA.runBind (findExtRoot_tmA _ _) fun _ => ?_ + exact NdTmA.runBind (NdTmA.liftE (NdTmA.gets _)) fun _ => NdTmA.runBind (NdTmA.unlessE (NdTmA.throwError _)) fun _ => + NdTmA.runBind (NdTmA.liftE (NdTmA.gets _)) fun _ => NdTmA.runBind (NdTmA.unlessE (NdTmA.throwError _)) fun _ => + NdTmA.liftE (storeExtSlot_tmA _ _) + +theorem extUnmapTable_tmA (k : ExtRootKind) (asid : Asid) (v : VSpaceRef) (pt : ObjRef) : + NdTmA (extUnmapTable (Z := Unit) k asid v pt) := by + unfold extUnmapTable + refine NdTmA.catch ?_ fun _ => NdTmA.pure _ + refine NdTmA.runBind (findExtRoot_tmA _ _) fun _ => NdTmA.runBind (NdTmA.liftE (NdTmA.gets _)) fun o => ?_ + cases o with + | some sl => exact NdTmA.liftE (storeExtSlot_tmA _ _) + | none => exact NdTmA.returnOk _ + +theorem extKindOfAsid_tmA (asid : Asid) : NdTmA (extKindOfAsid (Z := Unit) asid) := by + unfold extKindOfAsid; tm_auto + +/-- **`archFinaliseCap` keeps the time** on every arch cap but a VCPU's. -/ +theorem archFinaliseCap_tmA (c : ArchCap) (hc : ∀ v, c ≠ .vcpuCap v) (d : Bool) : + NdTmA (archFinaliseCap (Z := Unit) c d) := by + unfold archFinaliseCap + split + · exact NdTmA.bind (deleteAsidPool_tmA _ _) fun _ => NdTmA.pure _ + · exact NdTmA.bind (deleteAsid_tmA _ _) fun _ => NdTmA.pure _ + · exact NdTmA.bind (unmapPageTable_tmA _ _ _) fun _ => NdTmA.pure _ + · exact NdTmA.bind (unmapPage_tmA _ _ _ _) fun _ => NdTmA.pure _ + all_goals first | exact NdTmA.pure _ | exact absurd rfl (hc _) | (exfalso; simp_all) + +/-- **`fl4FinaliseCap` keeps the time** on every arch cap but a VCPU's (module docstring). -/ +theorem fl4FinaliseCap_tmA (c : ArchCap) (hc : ∀ v, c ≠ .vcpuCap v) (d : Bool) : + NdTmA (fl4FinaliseCap (Z := Unit) (.archObjectCap c) d) := by + have hF : NdTmA (finaliseCap (Z := Unit) (.archObjectCap c) d) := by + unfold finaliseCap; exact archFinaliseCap_tmA c hc d + unfold fl4FinaliseCap + split + all_goals first + | exact hF + | (refine NdTmA.bind (extKindOfAsid_tmA _) fun k => ?_; split + all_goals first + | exact hF + | exact NdTmA.pure _ + | exact NdTmA.bind (extUnmapPage_tmA _ _ _ _ _) fun _ => NdTmA.pure _ + | (split + · exact NdTmA.bind (extUnmapTable_tmA _ _ _ _) fun _ => NdTmA.pure _ + · exact NdTmA.pure _)) + | exact NdTmA.bind (extUnmapPage_tmA _ _ _ _ _) fun _ => NdTmA.pure _ + | (split + · split + · exact NdTmA.bind (deleteAsid_tmA _ _) fun _ => NdTmA.pure _ + · exact NdTmA.pure _ + · exact NdTmA.pure _) + | (exfalso; simp_all) + + +/-- **`FinTimeSpecA` for the six VSpace kinds** (module docstring). -/ +theorem finTimeSpec_vsA {k : FinKindA} + (hk : k = .frame ∨ k = .pageTable ∨ k = .pml4 ∨ k = .highVSpace ∨ k = .ept ∨ k = .asidPool) : + FinTimeSpecA fl4FinaliseCap k := by + intro c ac d σ x σ' hck hac hmem + have hA : ∃ y, ac = .archObjectCap y ∧ ∀ v, y ≠ .vcpuCap v := by + rcases hac.eq_or_pt with e | ⟨p', a', v', e⟩ + · subst e + rcases hk with rfl | rfl | rfl | rfl | rfl | rfl <;> cases c <;> simp [finKindA] at hck <;> + first + | exact ⟨_, rfl, fun v h => by cases h⟩ + | exact ⟨_, rfl, fun v h => by unfold absFrameE at h; split at h <;> cases h⟩ + · exact ⟨_, e, fun v h => by cases h⟩ + obtain ⟨y, rfl, hy⟩ := hA + exact fl4FinaliseCap_tmA y hy d σ (x, σ') hmem + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelUnmapLeafA.lean b/proofs/Fl4ProofsA64/P3/DelUnmapLeafA.lean new file mode 100644 index 000000000..54ea46db5 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelUnmapLeafA.lean @@ -0,0 +1,392 @@ +/- +P3 (AArch64, delete hooks W1): **the final page-table unmap with what the deletion loop reads**. +`P3/FinVsXA`'s `fin_ptJXA` leaves FL4's flush and the written word unnamed. **`unmap_pt_leafA`** (every root kind) +gives, at FL4's returned state and arch state, a spec outcome `σ'` of the finaliser with `JAtZAX`, `VsSpecA`, `ClrA` +(only entries invalidated), and the written word as entry `i` of a table page of the relation (or nothing written). + +* **`OutRA R`**: every outcome of a spec operation is `R`-related to its start (`R` a preorder), closed under the + monad operations; **`unmapPageTable_clrA`** / **`extSlot_clrA`**. + +Named premises: `ArchKeepsA` [P], `S2IpaE` [P] (as the unmap tops). Decode-site hypotheses: `TabOffA`, +`ExtTabOffA`, `OffAsE`. New module. +-/ +import Fl4ProofsA64.P3.DelZeroKeepA +import Fl4ProofsA64.P3.FinVsXA +import Fl4ProofsA64.P3.VsMapJA +import Fl4ProofsA64.P3.VsTabJA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4Spec Fl4SpecA64 + +noncomputable section + +theorem ClrA.trans {σ1 σ2 σ3 : State Unit} (h1 : ClrA σ1 σ2) (h2 : ClrA σ2 σ3) : ClrA σ1 σ3 := by + intro p + rcases h1 p with k1 | ⟨t, t', e0, e1, ty, en⟩ | ⟨es, es', e0, e1, en⟩ | ⟨l, l', e0, e1, en⟩ + · have := h2 p + rw [k1] at this + exact this + · obtain ⟨t'', e2, ty2, en2⟩ := h2.pt e1 + refine Or.inr (Or.inl ⟨t, t'', e0, e2, ty2.trans ty, fun i => ?_⟩) + rcases en2 i with q | q + · rw [q]; exact en i + · exact Or.inr q + · obtain ⟨es'', e2, en2⟩ := h2.s2 e1 + refine Or.inr (Or.inr (Or.inl ⟨es, es'', e0, e2, fun i => ?_⟩)) + rcases en2 i with q | q + · rw [q]; exact en i + · exact Or.inr q + · obtain ⟨l'', e2, en2⟩ := h2.hv e1 + refine Or.inr (Or.inr (Or.inr ⟨l, l'', e0, e2, fun i => ?_⟩)) + rcases en2 i with q | q + · rw [q]; exact en i + · exact Or.inr q + +/-! ## Outcomes related to the start -/ + +def OutRA (R : State Unit → State Unit → Prop) {α : Type} (m : NonDet (State Unit) α) : Prop := + ∀ σ r, r ∈ (m σ).1 → R σ r.2 + +section out +variable {R : State Unit → State Unit → Prop} {α β ε : Type} + +theorem OutRA.bind (T : ∀ {a b c}, R a b → R b c → R a c) {m : NonDet (State Unit) α} + {f : α → NonDet (State Unit) β} (hm : OutRA R m) (hf : ∀ a, OutRA R (f a)) : OutRA R (m >>= f) := fun σ r hr => by + obtain ⟨x, hx, hr⟩ := hr + exact T (hm σ x hx) (hf x.1 x.2 r hr) + +theorem OutRA.one (F : ∀ a, R a a) {m : NonDet (State Unit) α} (h : ∀ σ, ∃ v, m σ = ({(v, σ)}, False)) : + OutRA R m := fun σ r hr => by + obtain ⟨v, e⟩ := h σ + rw [e] at hr + obtain rfl : r = (v, σ) := hr + exact F σ + +theorem OutRA.assertOpt (F : ∀ a, R a a) (o : Option α) : OutRA R (NonDet.assertOpt o : NonDet (State Unit) α) := by + cases o with + | none => exact fun _ _ h => absurd h (Set.notMem_empty _) + | some a => exact OutRA.one F fun _ => ⟨a, rfl⟩ + +theorem OutRA.ebind (T : ∀ {a b c}, R a b → R b c → R a c) {m : NonDetE (State Unit) ε α} + {f : α → NonDetE (State Unit) ε β} (hm : OutRA R m.run) (hf : ∀ a, OutRA R (f a).run) : + OutRA R (m >>= f).run := fun σ r hr => by + obtain ⟨⟨e | a, σ1⟩, hx, hr⟩ := hr + · obtain rfl : r = (.error e, σ1) := hr + exact hm σ _ hx + · exact T (hm σ _ hx) (hf a σ1 r hr) + +theorem OutRA.liftE {m : NonDet (State Unit) α} (hm : OutRA R m) : OutRA R (NonDetE.liftE (ε := ε) m).run := + fun σ r hr => by + obtain ⟨x, hx, hr⟩ := hr + obtain rfl : r = (.ok x.1, x.2) := hr + exact hm σ x hx + +theorem OutRA.catch (T : ∀ {a b c}, R a b → R b c → R a c) {m : NonDetE (State Unit) ε α} + {h : ε → NonDet (State Unit) α} (hm : OutRA R m.run) (hh : ∀ e, OutRA R (h e)) : + OutRA R (NonDetE.catch m h) := fun σ r hr => by + obtain ⟨⟨e | a, σ1⟩, hx, hr⟩ := hr + · exact T (hm σ _ hx) (hh e σ1 r hr) + · obtain rfl : r = (a, σ1) := hr + exact hm σ _ hx + +end out + +/-! ## `ClrA` of the unmaps -/ + +local notation "CT" => fun {a b c} (h1 : ClrA a b) (h2 : ClrA b c) => ClrA.trans h1 h2 + +theorem gets_clrA {α : Type} (g : State Unit → α) : OutRA ClrA (NonDet.gets g) := + OutRA.one ClrA.refl fun _ => ⟨_, rfl⟩ + +theorem getsThe_clrA {α : Type} (g : State Unit → Option α) : OutRA ClrA (NonDet.getsThe g) := fun σ r hr => + OutRA.assertOpt ClrA.refl (g σ) σ r hr + +theorem throwE_clrA {α ε : Type} (e : ε) : OutRA ClrA (NonDetE.throwError e : NonDetE (State Unit) ε α).run := + OutRA.one ClrA.refl fun _ => ⟨_, rfl⟩ + +theorem retOk_clrA {α ε : Type} (a : α) : OutRA ClrA (NonDetE.returnOk a : NonDetE (State Unit) ε α).run := + OutRA.one ClrA.refl fun _ => ⟨_, rfl⟩ + +theorem unlessE_clrA {ε : Type} {P : Prop} [Decidable P] {m : NonDetE (State Unit) ε Unit} (hm : OutRA ClrA m.run) : + OutRA ClrA (NonDetE.unlessE P m).run := by + unfold NonDetE.unlessE + split + · exact OutRA.one ClrA.refl fun _ => ⟨_, rfl⟩ + · exact hm + +theorem throwOpt_clrA {α ε : Type} (e : ε) (o : Option α) : + OutRA ClrA (NonDetE.throwOpt e o : NonDetE (State Unit) ε α).run := by + cases o with + | none => exact OutRA.one ClrA.refl fun _ => ⟨_, rfl⟩ + | some a => exact OutRA.one ClrA.refl fun _ => ⟨_, rfl⟩ + +/-- Replacing one VSpace object by a `ClrObjA` successor. -/ +theorem clr_updA (σ : State Unit) (p0 : ObjRef) (o : KernelObject) + (h : ClrObjA (σ.kheap p0) (some o)) : ClrA σ { σ with kheap := Function.update σ.kheap p0 (some o) } := by + intro p + show ClrObjA (σ.kheap p) (Function.update σ.kheap p0 (some o) p) + by_cases e : p = p0 + · subst e; rw [Function.update_self]; exact h + · rw [Function.update_of_ne e]; exact Or.inl rfl + +theorem clr_ptUpdA (t : PT) (idx : Word) : + ptType (ptUpd t idx .invalidPTE) = ptType t ∧ + ∀ i, ptApply (ptUpd t idx .invalidPTE) i = ptApply t i ∨ ptApply (ptUpd t idx .invalidPTE) i = .invalidPTE := by + cases t with + | normalPT f => + refine ⟨rfl, fun i => ?_⟩ + simp only [ptUpd, ptApply, Function.update_apply] + split + · exact Or.inr rfl + · exact Or.inl rfl + | vsRootPT f => + refine ⟨rfl, fun i => ?_⟩ + simp only [ptUpd, ptApply, Function.update_apply] + split + · exact Or.inr rfl + · exact Or.inl rfl + +theorem storePte_clrA (ptT : PtType) (p : ObjRef) : OutRA ClrA (storePte (Z := Unit) ptT p .invalidPTE) := by + have look : ∀ (b : ObjRef) (σ : State Unit) (x : PT × State Unit), x ∈ (NonDet.getsMap ptsOf b σ).1 → + x.2 = σ ∧ ptsOf σ b = some x.1 := by + intro b σ x hx + obtain ⟨y, hy, hx⟩ := hx + obtain rfl : y = (ptsOf σ, σ) := hy + change x ∈ (NonDet.assertOpt (ptsOf σ b) σ).1 at hx + cases e : ptsOf σ b with + | none => rw [e] at hx; exact absurd hx (Set.notMem_empty _) + | some t => + rw [e] at hx + obtain rfl : x = (t, σ) := hx + exact ⟨rfl, rfl⟩ + intro σ r hr + unfold storePte setPt setObject getObject at hr + obtain ⟨x1, ⟨-, rfl⟩, hr⟩ := hr + obtain ⟨x2, hx2, hr⟩ := hr + obtain rfl : x2 = (tableBase ptT p, σ) := hx2 + obtain ⟨x3, hx3, hr⟩ := hr + obtain ⟨e3, h3⟩ := look _ _ _ hx3 + rw [e3] at hr + have hk := ptsOf_someX h3 + obtain ⟨x4, ⟨-, rfl⟩, hr⟩ := hr + obtain ⟨x5, hx5, hr⟩ := hr + obtain ⟨e5, h5⟩ := look _ _ _ hx5 + rw [e5] at hr + obtain ⟨x6, hx6, hr⟩ := hr + have e6 : x6.2 = σ := by + change x6 ∈ (NonDet.assertOpt (readObject (tableBase ptT p) σ) σ).1 at hx6 + cases e : readObject (tableBase ptT p) σ with + | none => rw [e] at hx6; exact absurd hx6 (Set.notMem_empty _) + | some o => + rw [e] at hx6 + obtain rfl : x6 = (o, σ) := hx6 + rfl + rw [e6] at hr + obtain ⟨x7, ⟨-, rfl⟩, hr⟩ := hr + obtain ⟨x8, hx8, hr⟩ := hr + obtain rfl : x8 = (σ, σ) := hx8 + obtain rfl : r = ((), _) := hr + refine clr_updA σ _ _ (Or.inr (Or.inl ⟨_, _, hk, rfl, ?_⟩)) + exact clr_ptUpdA _ _ + +theorem doMachineOp_clrA {mop : NonDet MachineState Unit} (h : RestOnlyA mop) : + OutRA ClrA (doMachineOp (Z := Unit) mop) := by + intro σ r hr + have := doMachineOp_vsA h σ r hr + have e : r.2.kheap = σ.kheap := by + unfold doMachineOp at hr + obtain ⟨y, hy, hr⟩ := hr + obtain rfl : y = (σ.machineState, σ) := hy + obtain ⟨z, hz, hr⟩ := hr + obtain ⟨-, ez⟩ := hz + obtain ⟨w, hw, hr⟩ := hr + obtain rfl : w = ((), { z.2 with machineState := z.1.2 }) := hw + obtain rfl : r = (z.1.1, { z.2 with machineState := z.1.2 }) := hr + show z.2.kheap = σ.kheap + rw [ez] + intro p + rw [e]; exact Or.inl rfl + +theorem liftOp_clrA (g : MachineRestMonad Unit) : OutRA ClrA (doMachineOp (Z := Unit) (machineOpLift g)) := + doMachineOp_clrA fun ms x hx => (machineOpLift_restA g ms).2.2 x hx + +theorem invTlb_clrA (asid : Asid) : OutRA ClrA (invalidateTlbByAsid (Z := Unit) asid) := by + unfold invalidateTlbByAsid loadVmid + refine OutRA.bind CT (OutRA.bind CT (gets_clrA _) fun _ => OutRA.one ClrA.refl fun _ => ⟨_, rfl⟩) fun o => ?_ + cases o with + | none => exact OutRA.one ClrA.refl fun _ => ⟨_, rfl⟩ + | some _ => exact liftOp_clrA _ + +theorem findVs_clrA (asid : Asid) : OutRA ClrA (findVspaceForAsid (Z := Unit) asid).run := by + unfold findVspaceForAsid + exact OutRA.ebind CT (OutRA.liftE (gets_clrA _)) fun _ => OutRA.ebind CT (OutRA.liftE (gets_clrA _)) fun o => + throwOpt_clrA _ o + +theorem ptLookupN_clrA : ∀ (n : Nat) (level : VmLevel) (pt : ObjRef) (v : VSpaceRef) (t : ObjRef), + OutRA ClrA (ptLookupFromLevelN (Z := Unit) n level pt v t).run + | 0, level, pt, v, t => by + rw [ptLookupFromLevelN] + exact OutRA.ebind CT (unlessE_clrA (throwE_clrA _)) fun _ => throwE_clrA _ + | n + 1, level, pt, v, t => by + rw [ptLookupFromLevelN] + refine OutRA.ebind CT (unlessE_clrA (throwE_clrA _)) fun _ => ?_ + refine OutRA.ebind CT (retOk_clrA _) fun _ => ?_ + refine OutRA.ebind CT (OutRA.liftE (getsThe_clrA _)) fun _ => ?_ + refine OutRA.ebind CT (unlessE_clrA (throwE_clrA _)) fun _ => ?_ + refine OutRA.ebind CT (retOk_clrA _) fun ptr => ?_ + by_cases hp : ptr = t + · rw [if_pos hp]; exact retOk_clrA _ + · rw [if_neg hp]; exact ptLookupN_clrA n _ _ _ _ + +/-- **l4v's `unmap_page_table` only invalidates an entry.** -/ +theorem unmapPageTable_clrA (asid : Asid) (v : VSpaceRef) (pt : ObjRef) : + OutRA ClrA (unmapPageTable (Z := Unit) asid v pt) := by + unfold unmapPageTable ptLookupFromLevel + refine OutRA.catch CT ?_ fun _ => OutRA.one ClrA.refl fun _ => ⟨_, rfl⟩ + refine OutRA.ebind CT (findVs_clrA _) fun _ => ?_ + refine OutRA.ebind CT (ptLookupN_clrA _ _ _ _ _) fun x => ?_ + obtain ⟨slot, level⟩ := x + refine OutRA.ebind CT (OutRA.liftE (storePte_clrA _ _)) fun _ => ?_ + refine OutRA.ebind CT (OutRA.liftE (liftOp_clrA _)) fun _ => ?_ + exact OutRA.liftE (invTlb_clrA _) + +/-- **A VSpace slot's invalidation is a `VsSpecA` and `ClrA` step.** -/ +theorem extSlot_clrA (σ : State Unit) (sl : ExtSlot) : + VsSpecA σ (extSlotWriteX σ sl .invalidPTE) ∧ ClrA σ (extSlotWriteX σ sl .invalidPTE) := by + have upd : ∀ (p0 : ObjRef) (a a' : ArchKernelObj), σ.kheap p0 = some (.archObj a) → + (∀ v, a ≠ .vcpu v) → (∀ v, a' ≠ .vcpu v) → ClrObjA (σ.kheap p0) (some (.archObj a')) → + VsSpecA σ { σ with kheap := Function.update σ.kheap p0 (some (.archObj a')) } ∧ + ClrA σ { σ with kheap := Function.update σ.kheap p0 (some (.archObj a')) } := by + intro p0 a a' e n n' c + refine ⟨⟨rfl, fun p => ?_, fun _ _ h => h⟩, clr_updA σ p0 _ c⟩ + show Function.update σ.kheap p0 (some (.archObj a')) p = σ.kheap p ∨ _ + by_cases h : p = p0 + · subst h + exact Or.inr ⟨a, a', e, Function.update_self .., n, n'⟩ + · exact Or.inl (Function.update_of_ne h ..) + have keep : VsSpecA σ { σ with kheap := σ.kheap } ∧ ClrA σ { σ with kheap := σ.kheap } := + ⟨VsSpecA.refl σ, ClrA.refl σ⟩ + unfold extSlotWriteX + cases sl with + | root r i => + simp only [extSlotKhX] + cases ho : extObjAt σ r with + | none => exact keep + | some o => + have e : σ.kheap r = some (.archObj (.ext o)) := by + unfold extObjAt at ho + split at ho + · rename_i o' h + cases ho; exact h + · cases ho + refine upd r (.ext o) (.ext (o.setEntry i .invalidPTE)) e (fun _ h => by cases h) (fun _ h => by cases h) ?_ + rw [e] + cases o with + | stage2Root es => + refine Or.inr (Or.inr (Or.inl ⟨es, _, rfl, rfl, fun j => ?_⟩)) + try dsimp only + split + · exact Or.inr rfl + · exact Or.inl rfl + | highVSpace l => + refine Or.inr (Or.inr (Or.inr ⟨l, _, rfl, rfl, fun j => ?_⟩)) + try dsimp only + split + · exact Or.inr rfl + · exact Or.inl rfl + | pt p => + simp only [extSlotKhX] + split + · rename_i t e + refine upd _ (.pageTable t) (.pageTable (ptUpd t (tableIndex .normalPtT p) .invalidPTE)) e + (fun _ h => by cases h) (fun _ h => by cases h) ?_ + rw [e] + exact Or.inr (Or.inl ⟨_, _, rfl, rfl, clr_ptUpdA _ _⟩) + · exact keep + +/-! ## The leaf -/ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {s : S} {a : A} {σ : State Unit} {m : McState} + +/-- A flush keeps the arch relation. -/ +theorem flushE_arKeepA {ar : machine.Arch A} (K : ArchKeepsA ar AR) {a : A} {fa : Result A} (h : FlushE ar a fa) + {hw : HwRootA} (ha : AR a hw) : fa ⦃ a' => AR a' hw ⦄ := by + rcases h with rfl | ⟨x, y, rfl⟩ | ⟨x, rfl⟩ | ⟨y, rfl⟩ | ⟨y, rfl⟩ + · exact (WP.spec_ok _).2 ha + · exact K.invS2Page a hw x y ha + · exact K.invS2Vmid a hw x ha + · exact K.invHiPage a hw y ha + · exact K.invHiVs a hw y ha + +/-- The table page a written word lies in. -/ +def VsPageA (Z : XSetsA) (p : U64) : Prop := + p ∈ Z.S1 ∨ p ∈ S2PgE Z.X Z.T2 ∨ ∃ y ∈ Z.Y, p = core.num.U64.wrapping_add y 4096#u64 + +/-- **The leaf's post** (module docstring). -/ +def UnmapLeafPostA (AR : A → HwRootA → Prop) (Z : XSetsA) (m : McState) (s : S) (a : A) (σ : State Unit) + (W : Set ((Cap × Cap) × State Unit)) (r : S × A) : Prop := + ∃ σ', ((Cap.nullCap, Cap.nullCap), σ') ∈ W ∧ JAtZAX (L := L) AR Z r.1 r.2 σ' m ∧ VsSpecA σ σ' ∧ ClrA σ σ' ∧ + (r = (s, a) ∧ σ' = σ ∨ ∃ p i : U64, i.val < 512 ∧ VsPageA Z p ∧ r.1 = L.wr s (peAF p i) 0#u64) + +/-- **The final mapped page table cap's unmap** (module docstring). -/ +theorem unmap_pt_leafA {ar : machine.Arch A} (K : ArchKeepsA ar AR) (J : JAtZAX (L := L) AR Z s a σ m) + (hfa : ∀ w, FootA (L := L) Z s w → Aligned w) (hTO : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s q) + (hEO : ExtTabOffA (L := L) Z m s) (hK : Aligned (L.ksb s)) (hT : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s)) + (hIpa : S2IpaE ar a) {asid : U64} (h16 : asid.val < 65536) (va pt : U64) : + vspace.unmap_page_table st ar s a asid va pt ⦃ r => UnmapLeafPostA (L := L) AR Z m s a σ + (fl4FinaliseCap (Z := Unit) + (.archObjectCap (.pageTableCap (specPtr pt) .normalPtT (some (BitVec.ofNat 16 asid.val, va.bv)))) true σ).1 r ⦄ := by + have har : AR a Z.hw := J.1.ar + have same : ∀ W : Set ((Cap × Cap) × State Unit), ((Cap.nullCap, Cap.nullCap), σ) ∈ W → + UnmapLeafPostA (L := L) AR Z m s a σ W (s, a) := + fun W h => ⟨σ, h, J, VsSpecA.refl σ, ClrA.refl σ, Or.inl ⟨rfl, rfl⟩⟩ + rcases unmap_page_tableXGE J.1.vs J.1.ext hK hT J.2.2.1.obj hIpa h16 va pt with + ⟨-, hF, hU⟩ | ⟨k, fk, -, hk, hF, hU⟩ + · rw [hF] + obtain ⟨-, hm⟩ := finRetA (unmapPageTable (Z := Unit) (BitVec.ofNat 16 asid.val) va.bv (specPtr pt)) + (Cap.nullCap, Cap.nullCap) σ + rcases UnmapRelA.extE J.1.vs J.1.ext hU with + ⟨hu, hW⟩ | ⟨slot, ⟨p, hp, i, hi, rfl⟩, hu, ⟨-, ⟨x0, hx0⟩, h3⟩, h4⟩ + · rw [hu, WP.spec_ok] + exact same _ ((hm _).2 ⟨((), σ), by rw [hW]; rfl, rfl⟩) + · rw [hu] + refine WP.spec_bind (K.invAsid a Z.hw asid har) fun a1 h1 => (WP.spec_ok _).2 ?_ + have J' := (J.of_s1ClearA hfa hTO hp hi (unmapPageTable_kmA _ _ _ σ x0 hx0) (h3 x0 hx0).2 (h3 x0 hx0).1 + (h4 x0 hx0)).of_arA h1 + exact ⟨x0.2, (hm _).2 ⟨x0, hx0, rfl⟩, J', unmapPageTable_vsA _ _ _ σ x0 hx0, + unmapPageTable_clrA _ _ _ σ x0 hx0, Or.inr ⟨p, i, hi, Or.inl hp, rfl⟩⟩ + · rw [hF] + rw [extUnmapTable_runX] at hU + have cs : VsSpecA σ (extUnmapTableStX σ k (BitVec.ofNat 16 asid.val) va.bv (specPtr pt)) ∧ + ClrA σ (extUnmapTableStX σ k (BitVec.ofNat 16 asid.val) va.bv (specPtr pt)) := by + unfold extUnmapTableStX + split + · exact extSlot_clrA σ _ + · exact ⟨VsSpecA.refl σ, ClrA.refl σ⟩ + rcases hU with ⟨hu, hW⟩ | ⟨slot, σ', hu, hW, hR, hE, hw⟩ + · have e := congrArg Prod.fst hW + simp only [Set.singleton_eq_singleton_iff, Prod.mk.injEq, true_and] at e + rw [hu, WP.spec_ok] + exact same _ (by rw [e]; rfl) + · have e := congrArg Prod.fst hW + simp only [Set.singleton_eq_singleton_iff, Prod.mk.injEq, true_and] at e + subst e + obtain ⟨fa, hfl, hfk⟩ := hk (L.wr s slot 0#u64) + rw [hu, hfk] + refine WP.spec_bind (flushE_arKeepA K hfl har) fun a1 h1 => (WP.spec_ok _).2 ?_ + refine ⟨_, rfl, (J.of_extClearA hfa hTO hEO hw hR hE).of_arA h1, cs.1, cs.2, Or.inr ?_⟩ + obtain ⟨⟨q, j, hj, rfl, -, hq⟩, -⟩ := hw + refine ⟨q, j, hj, ?_, rfl⟩ + rcases hq with h | h | h + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr h) + · exact Or.inl h + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelUnmapSpecA.lean b/proofs/Fl4ProofsA64/P3/DelUnmapSpecA.lean new file mode 100644 index 000000000..6080ff8a6 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelUnmapSpecA.lean @@ -0,0 +1,252 @@ +/- +P3 (AArch64, delete hooks W1): **l4v's `unmap_page_table` and `unmap_page` are `VsSpecA` steps**: every outcome +changes only non-VCPU arch objects (the written table) and the machine's opaque part; the user memory, the ASID table +and every other field are kept. This is the spec premise of `JStage3TA.of_vsWrite` for the Frame and PageTable hooks +(`FinVsJA`'s `KmA` also lets the user memory and any object change, which is too weak for that lift). + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3VsWriteA +import Fl4ProofsA64.P3.FinVsJA +import Fl4ProofsA64.P3.ExtVsSpecA + +namespace Fl4ProofsA64.P3 +open Fl4Spec Fl4SpecA64 + +noncomputable section + +theorem VsSpecA.refl (σ : State Unit) : VsSpecA σ σ := ⟨rfl, fun _ => Or.inl rfl, fun _ _ h => h⟩ + +theorem VsSpecA.trans {σ1 σ2 σ3 : State Unit} (h1 : VsSpecA σ1 σ2) (h2 : VsSpecA σ2 σ3) : VsSpecA σ1 σ3 := by + refine ⟨?_, fun p => ?_, fun i p h => h1.asid i p (h2.asid i p h)⟩ + · conv => lhs; rw [h2.eq] + generalize σ3.kheap = k3 + generalize σ3.machineState.machineStateRest = r3 + generalize σ3.archState.armAsidTable = t3 + generalize σ3.archState.armAsidMap = a3 + generalize σ3.archState.armVmidTable = v3 + generalize σ3.archState.armNextVmid = n3 + rw [h1.eq] + · rcases h2.kh p with e2 | ⟨a, a', e1, e2, n1, n2⟩ + · rcases h1.kh p with e1 | ⟨b, b', f1, f2, m1, m2⟩ + · exact Or.inl (e2.trans e1) + · exact Or.inr ⟨b, b', f1, e2.trans f2, m1, m2⟩ + · rcases h1.kh p with f | ⟨b, b', f1, f2, m1, m2⟩ + · exact Or.inr ⟨a, a', f.symm.trans e1, e2, n1, n2⟩ + · exact Or.inr ⟨b, a', f1, e2, m1, n2⟩ + +/-- Every outcome is a `VsSpecA` step from its start. -/ +def OutVsA {α : Type} (m : NonDet (State Unit) α) : Prop := ∀ σ r, r ∈ (m σ).1 → VsSpecA σ r.2 + +section out +variable {α β ε : Type} + +theorem OutVsA.bind {m : NonDet (State Unit) α} {f : α → NonDet (State Unit) β} (hm : OutVsA m) + (hf : ∀ a, OutVsA (f a)) : OutVsA (m >>= f) := fun σ r hr => by + obtain ⟨x, hx, hr⟩ := hr + exact (hm σ x hx).trans (hf x.1 x.2 r hr) + +theorem OutVsA.one {m : NonDet (State Unit) α} (h : ∀ σ, ∃ v, m σ = ({(v, σ)}, False)) : OutVsA m := fun σ r hr => by + obtain ⟨v, e⟩ := h σ + rw [e] at hr + obtain rfl : r = (v, σ) := hr + exact VsSpecA.refl σ + +theorem OutVsA.pure (a : α) : OutVsA (Pure.pure a : NonDet (State Unit) α) := OutVsA.one fun _ => ⟨a, rfl⟩ + +theorem OutVsA.gets (g : State Unit → α) : OutVsA (NonDet.gets g) := OutVsA.one fun _ => ⟨_, rfl⟩ + +theorem OutVsA.assertOpt (o : Option α) : OutVsA (NonDet.assertOpt o : NonDet (State Unit) α) := by + cases o with + | none => exact fun _ _ h => absurd h (Set.notMem_empty _) + | some a => exact OutVsA.one fun _ => ⟨a, rfl⟩ + +theorem OutVsA.getsThe (g : State Unit → Option α) : OutVsA (NonDet.getsThe g) := fun σ r hr => + OutVsA.assertOpt (g σ) σ r hr + +theorem OutVsA.getsMap {γ : Type} (g : State Unit → γ → Option α) (p : γ) : OutVsA (NonDet.getsMap g p) := + OutVsA.bind (OutVsA.gets g) fun x => OutVsA.assertOpt (x p) + +theorem OutVsA.ebind {m : NonDetE (State Unit) ε α} {f : α → NonDetE (State Unit) ε β} (hm : OutVsA m.run) + (hf : ∀ a, OutVsA (f a).run) : OutVsA (m >>= f).run := fun σ r hr => by + obtain ⟨⟨e | a, σ1⟩, hx, hr⟩ := hr + · obtain rfl : r = (.error e, σ1) := hr + exact hm σ _ hx + · exact (hm σ _ hx).trans (hf a σ1 r hr) + +theorem OutVsA.liftE {m : NonDet (State Unit) α} (hm : OutVsA m) : OutVsA (NonDetE.liftE (ε := ε) m).run := + fun σ r hr => by + obtain ⟨x, hx, hr⟩ := hr + obtain rfl : r = (.ok x.1, x.2) := hr + exact hm σ x hx + +theorem OutVsA.retOk (a : α) : OutVsA (NonDetE.returnOk a : NonDetE (State Unit) ε α).run := + OutVsA.one fun _ => ⟨_, rfl⟩ + +theorem OutVsA.throw (e : ε) : OutVsA (NonDetE.throwError e : NonDetE (State Unit) ε α).run := + OutVsA.one fun _ => ⟨_, rfl⟩ + +theorem OutVsA.unlessE {P : Prop} [Decidable P] {m : NonDetE (State Unit) ε Unit} (hm : OutVsA m.run) : + OutVsA (NonDetE.unlessE P m).run := by + unfold NonDetE.unlessE + split + · exact OutVsA.one fun _ => ⟨_, rfl⟩ + · exact hm + +theorem OutVsA.throwOpt (e : ε) (o : Option α) : OutVsA (NonDetE.throwOpt e o : NonDetE (State Unit) ε α).run := by + cases o with + | none => exact OutVsA.one fun _ => ⟨_, rfl⟩ + | some a => exact OutVsA.one fun _ => ⟨_, rfl⟩ + +theorem OutVsA.catch {m : NonDetE (State Unit) ε α} {h : ε → NonDet (State Unit) α} (hm : OutVsA m.run) + (hh : ∀ e, OutVsA (h e)) : OutVsA (NonDetE.catch m h) := fun σ r hr => by + obtain ⟨⟨e | a, σ1⟩, hx, hr⟩ := hr + · exact (hm σ _ hx).trans (hh e σ1 r hr) + · obtain rfl : r = (a, σ1) := hr + exact hm σ _ hx + +end out + +/-! The spec operations. -/ + +theorem storePte_vsA (ptT : PtType) (p : ObjRef) (pte : PTE) : OutVsA (storePte (Z := Unit) ptT p pte) := by + have look : ∀ (b : ObjRef) (σ : State Unit) (x : PT × State Unit), x ∈ (NonDet.getsMap ptsOf b σ).1 → + x.2 = σ ∧ ptsOf σ b = some x.1 := by + intro b σ x hx + obtain ⟨y, hy, hx⟩ := hx + obtain rfl : y = (ptsOf σ, σ) := hy + change x ∈ (NonDet.assertOpt (ptsOf σ b) σ).1 at hx + cases e : ptsOf σ b with + | none => rw [e] at hx; exact absurd hx (Set.notMem_empty _) + | some t => + rw [e] at hx + obtain rfl : x = (t, σ) := hx + exact ⟨rfl, rfl⟩ + intro σ r hr + unfold storePte setPt setObject getObject at hr + obtain ⟨x1, ⟨-, rfl⟩, hr⟩ := hr + obtain ⟨x2, hx2, hr⟩ := hr + obtain rfl : x2 = (tableBase ptT p, σ) := hx2 + obtain ⟨x3, hx3, hr⟩ := hr + obtain ⟨e3, h3⟩ := look _ _ _ hx3 + rw [e3] at hr + have hk := ptsOf_someX h3 + obtain ⟨x4, ⟨-, rfl⟩, hr⟩ := hr + obtain ⟨x5, hx5, hr⟩ := hr + obtain ⟨e5, -⟩ := look _ _ _ hx5 + rw [e5] at hr + obtain ⟨x6, hx6, hr⟩ := hr + have e6 : x6.2 = σ := by + change x6 ∈ (NonDet.assertOpt (readObject (tableBase ptT p) σ) σ).1 at hx6 + cases e : readObject (tableBase ptT p) σ with + | none => rw [e] at hx6; exact absurd hx6 (Set.notMem_empty _) + | some o => + rw [e] at hx6 + obtain rfl : x6 = (o, σ) := hx6 + rfl + rw [e6] at hr + obtain ⟨x7, ⟨-, rfl⟩, hr⟩ := hr + obtain ⟨x8, hx8, hr⟩ := hr + obtain rfl : x8 = (σ, σ) := hx8 + obtain rfl : r = ((), _) := hr + refine ⟨rfl, fun q => ?_, fun _ _ h => h⟩ + by_cases hq : q = tableBase ptT p + · subst hq + refine Or.inr ⟨_, _, hk, Function.update_self .., ?_, ?_⟩ + all_goals + intro v e + cases e + · exact Or.inl (Function.update_of_ne hq ..) + +/-- A machine operation that changes only the opaque part. -/ +def RestOnlyA (mop : NonDet MachineState Unit) : Prop := + ∀ ms, ∀ x ∈ (mop ms).1, ∃ rest, x.2 = { ms with machineStateRest := rest } + +theorem doMachineOp_vsA {mop : NonDet MachineState Unit} (h : RestOnlyA mop) : + OutVsA (doMachineOp (Z := Unit) mop) := by + intro σ r hr + unfold doMachineOp at hr + obtain ⟨y, hy, hr⟩ := hr + obtain rfl : y = (σ.machineState, σ) := hy + obtain ⟨z, hz, hr⟩ := hr + obtain ⟨hz1, ez⟩ := hz + obtain ⟨w, hw, hr⟩ := hr + obtain rfl : w = ((), { z.2 with machineState := z.1.2 }) := hw + obtain rfl : r = (z.1.1, { z.2 with machineState := z.1.2 }) := hr + obtain ⟨rest, e⟩ := h _ _ hz1 + show VsSpecA σ { z.2 with machineState := z.1.2 } + rw [ez, e] + exact ⟨rfl, fun _ => Or.inl rfl, fun _ _ h => h⟩ + +theorem liftOp_vsA (g : MachineRestMonad Unit) : OutVsA (doMachineOp (Z := Unit) (machineOpLift g)) := + doMachineOp_vsA fun ms x hx => (machineOpLift_restA g ms).2.2 x hx + +theorem loadVmid_vsA (asid : Asid) : OutVsA (loadVmid (Z := Unit) asid) := by + unfold loadVmid + exact OutVsA.bind (OutVsA.gets _) fun _ => OutVsA.pure _ + +theorem invTlbVa_vsA (asid : Asid) (v : VSpaceRef) : OutVsA (invalidateTlbByAsidVa (Z := Unit) asid v) := by + unfold invalidateTlbByAsidVa + refine OutVsA.bind (loadVmid_vsA _) fun o => ?_ + cases o with + | none => exact OutVsA.pure _ + | some _ => exact liftOp_vsA _ + +theorem invTlb_vsA (asid : Asid) : OutVsA (invalidateTlbByAsid (Z := Unit) asid) := by + unfold invalidateTlbByAsid + refine OutVsA.bind (loadVmid_vsA _) fun o => ?_ + cases o with + | none => exact OutVsA.pure _ + | some _ => exact liftOp_vsA _ + +theorem findVs_vsA (asid : Asid) : OutVsA (findVspaceForAsid (Z := Unit) asid).run := by + unfold findVspaceForAsid + exact OutVsA.ebind (OutVsA.liftE (OutVsA.gets _)) fun _ => OutVsA.ebind (OutVsA.liftE (OutVsA.gets _)) fun o => + OutVsA.throwOpt _ o + +theorem ptLookupN_vsA : ∀ (n : Nat) (level : VmLevel) (pt : ObjRef) (v : VSpaceRef) (t : ObjRef), + OutVsA (ptLookupFromLevelN (Z := Unit) n level pt v t).run + | 0, level, pt, v, t => by + rw [ptLookupFromLevelN] + exact OutVsA.ebind (OutVsA.unlessE (OutVsA.throw _)) fun _ => OutVsA.throw _ + | n + 1, level, pt, v, t => by + rw [ptLookupFromLevelN] + refine OutVsA.ebind (OutVsA.unlessE (OutVsA.throw _)) fun _ => ?_ + refine OutVsA.ebind (OutVsA.retOk _) fun _ => ?_ + refine OutVsA.ebind (OutVsA.liftE (OutVsA.getsThe _)) fun _ => ?_ + refine OutVsA.ebind (OutVsA.unlessE (OutVsA.throw _)) fun _ => ?_ + refine OutVsA.ebind (OutVsA.retOk _) fun ptr => ?_ + by_cases hp : ptr = t + · rw [if_pos hp]; exact OutVsA.retOk _ + · rw [if_neg hp]; exact ptLookupN_vsA n _ _ _ _ + +/-- **l4v's `unmap_page` is a `VsSpecA` step.** -/ +theorem unmapPage_vsA (sz : VMPageSize) (asid : Asid) (v : VSpaceRef) (p : ObjRef) : + OutVsA (unmapPage (Z := Unit) sz asid v p) := by + unfold unmapPage + refine OutVsA.catch ?_ fun _ => OutVsA.pure _ + refine OutVsA.ebind (findVs_vsA _) fun _ => ?_ + refine OutVsA.ebind (OutVsA.liftE (OutVsA.getsThe _)) fun x => ?_ + obtain ⟨lev, slot⟩ := x + refine OutVsA.ebind (OutVsA.unlessE (OutVsA.throw _)) fun _ => ?_ + refine OutVsA.ebind (OutVsA.liftE (OutVsA.getsMap _ _)) fun _ => ?_ + refine OutVsA.ebind (OutVsA.unlessE (OutVsA.throw _)) fun _ => ?_ + refine OutVsA.ebind (OutVsA.liftE (storePte_vsA _ _ _)) fun _ => ?_ + refine OutVsA.ebind (OutVsA.liftE (liftOp_vsA _)) fun _ => ?_ + exact OutVsA.liftE (invTlbVa_vsA _ _) + +/-- **l4v's `unmap_page_table` is a `VsSpecA` step.** -/ +theorem unmapPageTable_vsA (asid : Asid) (v : VSpaceRef) (pt : ObjRef) : + OutVsA (unmapPageTable (Z := Unit) asid v pt) := by + unfold unmapPageTable ptLookupFromLevel + refine OutVsA.catch ?_ fun _ => OutVsA.pure _ + refine OutVsA.ebind (findVs_vsA _) fun _ => ?_ + refine OutVsA.ebind (ptLookupN_vsA _ _ _ _ _) fun x => ?_ + obtain ⟨slot, level⟩ := x + refine OutVsA.ebind (OutVsA.liftE (storePte_vsA _ _ _)) fun _ => ?_ + refine OutVsA.ebind (OutVsA.liftE (liftOp_vsA _)) fun _ => ?_ + exact OutVsA.liftE (invTlb_vsA _) + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelVaA.lean b/proofs/Fl4ProofsA64/P3/DelVaA.lean new file mode 100644 index 000000000..9896bf1ff --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelVaA.lean @@ -0,0 +1,149 @@ +/- +P3 (AArch64, delete hooks W1): **the spec's page-table unmap reads only bits below 48 of the address**, so the two +abstract forms of a mapped page table cap (`CapEqA`: `va.bv = va'` or `va.bv = unpack48A va'`) finalise alike: +**`fl4FinaliseCap_pt_unpackA`**. No named premise. New module. +-/ +import Fl4ProofsA64.P3.CapRelA +import Fl4ProofsA64.P3.DelSpecA +import Fl4ProofsA64.P3.ExtVsWalkA + +namespace Fl4ProofsA64.P3 +open Fl4Spec Fl4SpecA64 + +noncomputable section + +theorem unpack48A_fieldA (v : BitVec 64) (n k : Nat) (h : n + k ≤ 48) : + ((unpack48A v).toNat >>> n) % 2 ^ k = (v.toNat >>> n) % 2 ^ k := by + unfold unpack48A + split + · rfl + · have hb : ∀ j, j < 48 → (0xffff000000000000 : Nat).testBit j = false := by + intro j hj + have e : (0xffff000000000000 : Nat) % 2 ^ 48 = 0 := by norm_num + have := Nat.testBit_mod_two_pow (0xffff000000000000 : Nat) 48 j + rw [e, Nat.zero_testBit] at this + simpa [hj] using this.symm + rw [BitVec.toNat_or] + refine Nat.eq_of_testBit_eq fun i => ?_ + rw [Nat.testBit_mod_two_pow, Nat.testBit_mod_two_pow, Nat.testBit_shiftRight, Nat.testBit_shiftRight, + Nat.testBit_or] + by_cases hi : i < k + · have e : (0xffff000000000000#64 : BitVec 64).toNat = 0xffff000000000000 := rfl + rw [e, hb (n + i) (by omega), Bool.or_false] + · simp [hi] + +theorem unpack48A_maskA (v : BitVec 64) (n : Nat) (h : n + 9 ≤ 48) : + ((unpack48A v) >>> n) &&& (511#64) = (v >>> n) &&& (511#64) := by + apply BitVec.eq_of_toNat_eq + have e : (511#64 : BitVec 64).toNat = 2 ^ 9 - 1 := rfl + rw [BitVec.toNat_and, BitVec.toNat_and, BitVec.toNat_ushiftRight, BitVec.toNat_ushiftRight, e, + Nat.and_two_pow_sub_one_eq_mod, Nat.and_two_pow_sub_one_eq_mod] + exact unpack48A_fieldA v n 9 h + +theorem ptSlotOffset_unpackA (level : VmLevel) (hl : level.val ≤ 3) (p : ObjRef) (v : BitVec 64) : + ptSlotOffset level p (unpack48A v) = ptSlotOffset level p v := by + have h : ptIndex level (unpack48A v) = ptIndex level v := by + have key : ∀ l : VmLevel, l.val ≤ 3 → ptBitsLeft l + 9 ≤ 48 ∧ mask (ptTranslationBits (levelType l)) = (511#64) := by + decide + unfold ptIndex + rw [(key level hl).2] + exact unpack48A_maskA v _ (key level hl).1 + unfold ptSlotOffset + rw [h] + +theorem ptLookupN_unpackA : ∀ (n : Nat) (level : VmLevel), level.val ≤ 3 → ∀ (pt : ObjRef) (v : BitVec 64) (t : ObjRef), + ptLookupFromLevelN (Z := Unit) n level pt (unpack48A v) t = ptLookupFromLevelN (Z := Unit) n level pt v t + | 0, level, _, pt, v, t => by rw [ptLookupFromLevelN, ptLookupFromLevelN] + | n + 1, level, hl, pt, v, t => by + rw [ptLookupFromLevelN, ptLookupFromLevelN] + by_cases h0 : 0 < level + · have h1 : (level - 1).val ≤ 3 := by + have key : ∀ l : VmLevel, l.val ≤ 3 → 0 < l → (l - 1).val ≤ 3 := by decide + exact key level hl h0 + have ih := fun p => ptLookupN_unpackA n (level - 1) h1 p v t + simp only [ptSlotOffset_unpackA level hl, ih] + · have e : ∀ {α β : Type} (f g : α → NonDetE (State Unit) LookupFailure β), + (NonDetE.throwError .invalidRoot >>= f) = (NonDetE.throwError .invalidRoot >>= g) := by + intro α β f g + apply ExceptT.ext + funext s + rw [ExceptT.run_bind, ExceptT.run_bind] + rw [nd_bind_oneD (m := (NonDetE.throwError .invalidRoot : NonDetE (State Unit) LookupFailure α).run) + (a := .error .invalidRoot) (s' := s) rfl, + nd_bind_oneD (m := (NonDetE.throwError .invalidRoot : NonDetE (State Unit) LookupFailure α).run) + (a := .error .invalidRoot) (s' := s) rfl] + unfold NonDetE.unlessE + rw [if_neg h0] + exact e _ _ + +theorem extNextSlot_unpackA (v : BitVec 64) (pte : PTE) (shift : Nat) (h : shift ≤ 48) (h9 : 9 ≤ shift) : + extNextSlot (unpack48A v) pte shift = extNextSlot v pte shift := by + unfold extNextSlot + have e9 : ptTranslationBits .normalPtT = 9 := by decide + have m9 : mask 9 = (511#64) := by decide + rw [e9, m9, unpack48A_maskA v _ (by omega)] + +theorem extFindTableN_unpackA (s : State Unit) (v : BitVec 64) (pt : ObjRef) : + ∀ (n : Nat) (p : ExtPos), p.shift ≤ 48 → + extFindTableN s (unpack48A v) pt n p = extFindTableN s v pt n p + | 0, _, _ => by rw [extFindTableN, extFindTableN] + | n + 1, p, hp => by + rw [extFindTableN, extFindTableN] + try dsimp only + split + · rfl + · rename_i ht + split + · rfl + · have h9 : 9 ≤ p.shift := by + have ht' : extIsTable (p.slot.read s) p.shift = true := by simpa using ht + unfold extIsTable at ht' + simp only [Bool.and_eq_true, decide_eq_true_eq] at ht' + have : pageBits = 12 := by decide + omega + rw [extNextSlot_unpackA v _ _ hp h9] + exact extFindTableN_unpackA s v pt n _ (by dsimp only; omega) + +theorem extFindTableSlot_unpackA (s : State Unit) (k : ExtRootKind) (r : ObjRef) (v : BitVec 64) (pt : ObjRef) : + extFindTableSlot s k.geom r (unpack48A v) pt = extFindTableSlot s k.geom r v pt := by + have g : ∃ j, k.geom.topShift + j ≤ 48 ∧ k.geom.topMask + 1 = 2 ^ j := by + have s2 : ∃ j, s2Geom.topShift + j ≤ 48 ∧ s2Geom.topMask + 1 = 2 ^ j := by + unfold s2Geom s2GeomOf + split + · exact ⟨10, by decide, by decide⟩ + · split + · exact ⟨5, by decide, by decide⟩ + · exact ⟨9, by decide, by decide⟩ + cases k with + | high => exact ⟨9, by decide, by decide⟩ + | stage1 => exact s2 + | stage2 => exact s2 + obtain ⟨j, h1, h2⟩ := g + have top : extTopPos k.geom r (unpack48A v) = extTopPos k.geom r v := by + unfold extTopPos + rw [h2, unpack48A_fieldA v _ j h1] + unfold extFindTableSlot + rw [top] + exact extFindTableN_unpackA s v pt _ _ (show k.geom.topShift ≤ 48 by omega) + +theorem unmapPageTable_unpackA (asid : Asid) (v : BitVec 64) (pt : ObjRef) : + unmapPageTable (Z := Unit) asid (unpack48A v) pt = unmapPageTable (Z := Unit) asid v pt := by + unfold unmapPageTable ptLookupFromLevel + have h : (maxPtLevel : VmLevel).val ≤ 3 := by decide + simp only [ptLookupN_unpackA _ _ h] + +theorem extUnmapTable_unpackA (k : ExtRootKind) (asid : Asid) (v : BitVec 64) (pt : ObjRef) : + extUnmapTable (Z := Unit) k asid (unpack48A v) pt = extUnmapTable (Z := Unit) k asid v pt := by + unfold extUnmapTable + simp only [extFindTableSlot_unpackA] + +/-- **The finalisation of a mapped page table cap does not see `unpack48A`.** -/ +theorem fl4FinaliseCap_pt_unpackA (p : ObjRef) (asid : Asid) (v : BitVec 64) (final : Bool) : + fl4FinaliseCap (Z := Unit) (.archObjectCap (.pageTableCap p .normalPtT (some (asid, unpack48A v)))) final = + fl4FinaliseCap (Z := Unit) (.archObjectCap (.pageTableCap p .normalPtT (some (asid, v)))) final := by + cases final <;> + simp only [fl4FinaliseCap, finaliseCap, archFinaliseCap, unmapPageTable_unpackA, extUnmapTable_unpackA] + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelVsKeepA.lean b/proofs/Fl4ProofsA64/P3/DelVsKeepA.lean new file mode 100644 index 000000000..b94ebcc54 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelVsKeepA.lean @@ -0,0 +1,153 @@ +/- +P3 (AArch64, delete hooks W1): **what a VSpace word write keeps**, for the premises of `JStage3TA.of_vsWrite`. + +* **`JStage3TA.vsW_osKeepA`**: no `OS` slot word is written. +* **`JStage3TA.j5_vsWriteA`**: the concrete J5 slot (`J5A`: CDT ghost, IRQ handlers, slot sets) at the new state, any + hardware root and spec state: the `j5` premise of the lift. + +The write set is any set of `VsR` words, given as for `of_vsWrite`. No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3VsWriteTA +import Fl4ProofsA64.P3.Stage3J5A +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section zeroTy +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {Pp Tb : Set U64} {Lv : U64 → Nat} {s s' : S} + +/-- **A step that keeps or zeroes every reachable VSpace word keeps the narrowed typing and shrinks the reachable +set** (the ASID table may lose pools: `PoolSet` shrinks; `utVsTyR_keepA` is the case of an equal `PoolSet`). -/ +theorem utVsZero_keepA (h : VsTyR st (L.ksb s) (PoolSet st Pp s) ∅ Tb Lv s) (hk : L.ksb s' = L.ksb s) + (k : VsZ st (L.ksb s) (utVsPtA st Pp Tb Lv s) s s') : + VsTyR st (L.ksb s') (PoolSet st Pp s') ∅ Tb Lv s' ∧ utVsPtA st Pp Tb Lv s' ⊆ utVsPtA st Pp Tb Lv s := by + have hP : PoolSet st Pp s ⊆ utVsPtA st Pp Tb Lv s := Set.subset_union_left + have hT : VsRt st (PoolSet st Pp s) ∅ Tb Lv s ⊆ utVsPtA st Pp Tb Lv s := Set.subset_union_right + have old' : VsTy st (L.ksb s) (PoolSet st Pp s) (VsRt st (PoolSet st Pp s) ∅ Tb Lv s) Lv s' := + h.sub.keepZ hP hT k + -- the entry words are kept or zero + have ent : ∀ o : U64, 772352 ≤ o.val → o.val < 773376 → o.val % 8 = 0 → + L.mem s' (wa (L.ksb s) o) = L.mem s (wa (L.ksb s) o) ∨ L.mem s' (wa (L.ksb s) o) = 0#u64 := + fun o h1 h2 h8 => k _ (Or.inr ⟨o, h1, h2, h8, rfl⟩) + have hps : PoolSet st Pp s' ⊆ PoolSet st Pp s := by + rintro p ⟨hp, o, h1, h2, h8, hm, hn⟩ + rw [hk] at hm + rcases ent o h1 h2 h8 with e | e + · exact ⟨hp, o, h1, h2, h8, e.symm.trans hm, hn⟩ + · exact absurd (hm.symm.trans e) hn + have reach : ∀ p, VsReach st (PoolSet st Pp s') ∅ Lv s' p → VsReach st (PoolSet st Pp s) ∅ Lv s p := by + intro p hr + induction hr with + | hr hR => exact .hr hR + | @root0 q o hq ho h8 hne h0 => + have hqS := hps hq + rcases k _ (Or.inl ⟨q, hP hqS, o, ho, h8, rfl⟩) with e | e + · rw [e] at hne h0 ⊢; exact .root0 hqS ho h8 hne h0 + · exact absurd e hne + | @root2 q o hq ho h8 h2 => + have hqS := hps hq + have hne : L.mem s' (wa q o) ≠ 0#u64 := fun e => by rw [e] at h2; exact absurd h2 (by decide) + rcases k _ (Or.inl ⟨q, hP hqS, o, ho, h8, rfl⟩) with e | e + · rw [e] at h2 ⊢; exact .root2 hqS ho h8 h2 + · exact absurd e hne + | @step q o _ hl ho h8 h3 ih => + have hqR : q ∈ VsRt st (PoolSet st Pp s) ∅ Tb Lv s := ⟨(h.sub.reach (Set.empty_subset _) ih).1, ih⟩ + rcases k _ (Or.inl ⟨q, hT hqR, o, ho, h8, rfl⟩) with e | e + · rw [e] at h3 ⊢; exact .step ih hl ho h8 h3 + · rw [e] at h3; exact absurd h3 (by decide) + rw [hk] + refine ⟨⟨fun o h1 h2 h8 hne => ?_, fun p hp o ho h8 hne => ?_, fun p hp hr hl o ho h8 h3 => ?_⟩, ?_⟩ + · rcases ent o h1 h2 h8 with e | e + · rw [e] at hne ⊢ + exact ⟨(h.tbl o h1 h2 h8 hne).1, o, h1, h2, h8, by rw [hk]; exact e, hne⟩ + · exact absurd e hne + · have hpS := hps hp + obtain ⟨a, b⟩ := old'.pool p hpS o ho h8 hne + exact ⟨fun h0 => by obtain ⟨c, d⟩ := a h0; exact ⟨c.1, d⟩, fun h2 => by obtain ⟨c, d⟩ := b h2; exact ⟨c.1, d⟩⟩ + · have hpR : p ∈ VsRt st (PoolSet st Pp s) ∅ Tb Lv s := ⟨hp, reach p hr⟩ + obtain ⟨a, b⟩ := old'.desc p hpR hl o ho h8 h3 + exact ⟨a.1, b⟩ + · intro p hp + rcases hp with hp | hp + · exact Or.inl (hps hp) + · exact Or.inr ⟨hp.1, reach p hp.2⟩ + +/-- `UtVsOkA` across such a step. -/ +theorem utVsOk_zeroA {H : Heap} {OS V : Set U64} (h : VsTyR st (L.ksb s) (PoolSet st Pp s) ∅ Tb Lv s) + (hk : L.ksb s' = L.ksb s) (k : VsZ st (L.ksb s) (utVsPtA st Pp Tb Lv s) s s') + (o : UtVsOkA st H OS V Pp Tb Lv s) : UtVsOkA st H OS V Pp Tb Lv s' := by + unfold UtVsOkA at o ⊢ + rw [hk] + exact vrOK_monoPtA o (utVsZero_keepA h hk k).2 + +end zeroTy + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 : JSlotA S} {Rj : Set U64 → JSlotA S} {s s' : S} {a : A} {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} {P : U64 → Prop} + +/-- No `OS` slot word is written. -/ +theorem JStage3TA.vsW_osKeepA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7LA st Rj) s a σ m) + (w : WritesIn st s s' P) + (hP : ∀ Pt Ub : Set U64, VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt → + (∀ p, SchedPageA st Z OS s p → p ∈ Pt) → ∀ b, P b → VsR (L.ksb s) Pt b) : OSKeep st OS s s' := by + obtain ⟨Pt, Ub, vr, -, -, -, -, -, -, link, -, -⟩ := J.j7 + intro y hy + have al0 := J.ent.os.al0 hy + have hfit := J.ent.os.fit y hy + have key : ∀ o : U64, o.val < 32 → o.val % 8 = 0 → L.mem s' (wa y o) = L.mem s (wa y o) := by + intro o o1 o2 + have hv : (wa y o).val = y.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact w.mem_eq (J.ent.os.al hy o1 o2) + (fun h => vr.os _ (hP Pt Ub vr link _ h) ⟨y, hy, by omega, by omega⟩) + have e0 : wa y 0#u64 = y := by apply UScalar.eq_of_val_eq; rw [wa_valm]; simp + exact ⟨by have := key 0#u64 (by simp) (by simp); rwa [e0] at this, key 8#u64 (by simp) (by simp), + key 16#u64 (by simp) (by simp), key 24#u64 (by simp) (by simp)⟩ + +/-- **The `j5` premise of `of_vsWrite` at the concrete slot `J5A`.** -/ +theorem JStage3TA.j5_vsWriteA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7LA st Rj) s a σ m) + (w : WritesIn st s s' P) + (hP : ∀ Pt Ub : Set U64, VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt → + (∀ p, SchedPageA st Z OS s p → p ∈ Pt) → ∀ b, P b → VsR (L.ksb s) Pt b) + (hw : HwRootA) (σ' : Fl4SpecA64.State Unit) : J5A st { Z with hw := hw } H OS s' σ' m := by + have osk := J.vsW_osKeepA w hP + obtain ⟨Pt, Ub, vr, -, -, -, -, -, -, link, -, -⟩ := J.j7 + obtain ⟨⟨mdb, irq, act⟩, sub, nul⟩ := J.j5 + have tr := J.j.1.cdt.tree + have osk' : OSKeep st {a | IsSlotA Z.th a} s s' := fun y hy => osk y (sub y hy) + have hk : L.ksb s' = L.ksb s := w.1 + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.ent.ksw + have irqW : ∀ i : U64, i.val < 5376 → + L.mem s' (irqStAddrA (L.ksb s) i) = L.mem s (irqStAddrA (L.ksb s) i) := by + intro i hi + have ho : (wa 224#u64 (core.num.U64.wrapping_mul i 8#u64)).val = 224 + 8 * i.val := by + have h8 : (core.num.U64.wrapping_mul i 8#u64).val = 8 * i.val := by + simp [core.num.U64.wrapping_mul_val_eq, U64.size_def, U64.numBits]; omega + have e : (224#u64 : U64).val = 224 := rfl + rw [wa_valm, h8, e] + exact Nat.mod_eq_of_lt (by omega) + have hv : (irqStAddrA (L.ksb s) i).val = (L.ksb s).val + (224 + 8 * i.val) := by + unfold irqStAddrA + rw [wa_valm, ho]; exact Nat.mod_eq_of_lt (by omega) + have hkb := J.sr.toSchedSRN.ksb_al + refine w.mem_eq (by unfold Aligned; rw [hv]; omega) (fun h => ?_) + exact vr.ks _ (hP Pt Ub vr link _ h) _ (by rw [ho]; omega) rfl + exact ⟨⟨MdbInvA.keepO tr.lk tr.tree mdb osk', irq.keepO tr.lk osk', act.keepO tr.lk osk' hk irqW⟩, sub, + fun y hy hn => (osk.capA hy).trans (nul y hy hn)⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DelZeroKeepA.lean b/proofs/Fl4ProofsA64/P3/DelZeroKeepA.lean new file mode 100644 index 000000000..4b552f330 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DelZeroKeepA.lean @@ -0,0 +1,350 @@ +/- +P3 (AArch64, delete hooks W1): **the deletion loop's record across a VSpace table-word zero**, shared by the +PageTable and Frame finaliser hooks. + +* **`BufOffTabA`** ([J], local until the merge line's D1 `BufOffA`): no tracked IPC-buffer word is a word of a + scheduler-or-VSpace table page. +* **`ClrA σ σ'`**: the spec step only invalidates VSpace entries (what keeps `UnmPtSpecA`). +* **`delVs_zeroKeepA`**: after FL4's zero into word `i` of a table page `p` and a spec step with `JAtZAX`, `VsSpecA` + and `ClrA`, the loop's `DStateA`, `JStage3TA`, `ParentAdjA` and `ZombieTcbNoVcpuA` hold again. The caller supplies + A64 P1's R2 part of `J7` after the write. + +Named premise: `BufOffTabA` [J] (through `hbf`). Lemma hypotheses: the written word is no ASID-table word, the HighVSpace header words are kept. New module. +-/ +import Fl4ProofsA64.P3.DelVsKeepA +import Fl4ProofsA64.P3.DelSysKeepA +import Fl4ProofsA64.P3.DelUnmapSpecA +import Fl4ProofsA64.P3.DelRecA +import Fl4ProofsA64.P3.CnodeDelInvA +import Fl4ProofsA64.P3.CnodeDecDelA +import Fl4ProofsA64.P3.CnodeDelZombieA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## The spec side: entries only become invalid -/ + +/-- One object: kept, or a VSpace object whose entries are each kept or invalid. -/ +def ClrObjA (o o' : Option Fl4SpecA64.KernelObject) : Prop := + o' = o ∨ + (∃ t t', o = some (.archObj (.pageTable t)) ∧ o' = some (.archObj (.pageTable t')) ∧ + Fl4SpecA64.ptType t' = Fl4SpecA64.ptType t ∧ + ∀ i, Fl4SpecA64.ptApply t' i = Fl4SpecA64.ptApply t i ∨ Fl4SpecA64.ptApply t' i = .invalidPTE) ∨ + (∃ es es', o = some (.archObj (.ext (.stage2Root es))) ∧ o' = some (.archObj (.ext (.stage2Root es'))) ∧ + ∀ i, es' i = es i ∨ es' i = .invalidPTE) ∨ + (∃ l l', o = some (.archObj (.ext (.highVSpace l))) ∧ o' = some (.archObj (.ext (.highVSpace l'))) ∧ + ∀ i, l' i = l i ∨ l' i = .invalidPTE) + +def ClrA (σ σ' : Fl4SpecA64.State Unit) : Prop := ∀ p, ClrObjA (σ.kheap p) (σ'.kheap p) + +theorem ClrA.refl (σ : Fl4SpecA64.State Unit) : ClrA σ σ := fun _ => Or.inl rfl + +/-- A clearing step keeps the spec data pages. -/ +theorem ClrA.dataA {σ σ' : Fl4SpecA64.State Unit} (h : ClrA σ σ') (p : Fl4SpecA64.ObjRef) (dev : Bool) + (pg : Fl4SpecA64.VMPageSize) + (e : σ.kheap p = some (.archObj (.dataPage dev pg))) : σ'.kheap p = some (.archObj (.dataPage dev pg)) := by + rcases h p with k | ⟨t0, t', e0, -⟩ | ⟨_, _, e0, -⟩ | ⟨_, _, e0, -⟩ + · rw [k]; exact e + · rw [e] at e0; cases e0 + · rw [e] at e0; cases e0 + · rw [e] at e0; cases e0 + +theorem ClrA.pt {σ σ' : Fl4SpecA64.State Unit} (h : ClrA σ σ') {p : Fl4SpecA64.ObjRef} {t : Fl4SpecA64.PT} + (e : σ.kheap p = some (.archObj (.pageTable t))) : + ∃ t', σ'.kheap p = some (.archObj (.pageTable t')) ∧ Fl4SpecA64.ptType t' = Fl4SpecA64.ptType t ∧ + ∀ i, Fl4SpecA64.ptApply t' i = Fl4SpecA64.ptApply t i ∨ Fl4SpecA64.ptApply t' i = .invalidPTE := by + rcases h p with k | ⟨t0, t', e0, e1, ty, en⟩ | ⟨_, _, e0, -⟩ | ⟨_, _, e0, -⟩ + · exact ⟨t, k.trans e, rfl, fun _ => Or.inl rfl⟩ + · rw [e] at e0 + cases e0 + exact ⟨t', e1, ty, en⟩ + · rw [e] at e0; cases e0 + · rw [e] at e0; cases e0 + +theorem ClrA.s2 {σ σ' : Fl4SpecA64.State Unit} (h : ClrA σ σ') {p : Fl4SpecA64.ObjRef} {es} + (e : σ.kheap p = some (.archObj (.ext (.stage2Root es)))) : + ∃ es', σ'.kheap p = some (.archObj (.ext (.stage2Root es'))) ∧ ∀ i, es' i = es i ∨ es' i = .invalidPTE := by + rcases h p with k | ⟨_, _, e0, -⟩ | ⟨e1', es', e0, e1, en⟩ | ⟨_, _, e0, -⟩ + · exact ⟨es, k.trans e, fun _ => Or.inl rfl⟩ + · rw [e] at e0; cases e0 + · rw [e] at e0 + cases e0 + exact ⟨es', e1, en⟩ + · rw [e] at e0; cases e0 + +theorem ClrA.hv {σ σ' : Fl4SpecA64.State Unit} (h : ClrA σ σ') {p : Fl4SpecA64.ObjRef} {l} + (e : σ.kheap p = some (.archObj (.ext (.highVSpace l)))) : + ∃ l', σ'.kheap p = some (.archObj (.ext (.highVSpace l'))) ∧ ∀ i, l' i = l i ∨ l' i = .invalidPTE := by + rcases h p with k | ⟨_, _, e0, -⟩ | ⟨_, _, e0, -⟩ | ⟨_, l', e0, e1, en⟩ + · exact ⟨l, k.trans e, fun _ => Or.inl rfl⟩ + · rw [e] at e0; cases e0 + · rw [e] at e0; cases e0 + · rw [e] at e0 + cases e0 + exact ⟨l', e1, en⟩ + +/-! ## The record -/ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + +variable (st) in +/-- **[J] No tracked IPC-buffer word is a word of a scheduler-or-VSpace table page** (module docstring). -/ +def BufOffTabA (Z : XSetsA) (OS : Set U64) (Bf : Set (U64 × Fl4SpecA64.Word)) (s : S) : Prop := + ∀ b, BufW Bf b → ∀ p, SchedPageA st Z OS s p → ∀ i : U64, i.val < 512 → b ≠ peAF p i + +/-- A dead descriptor stays dead across a zero write. -/ +theorem dBitA_zeroA : dBitA 0#u64 0 = false := by + unfold dBitA; simp + +/-- **`ParentAdjA` along a step keeping every `OS` slot's words.** -/ +theorem ParentAdjA.keepOS {OS : Set U64} {G : List (List U64)} {par : U64 → Option U64} {s s' : S} + (hM : MdbLk st OS G s) (k : OSKeep st OS s s') (h : ParentAdjA st G par s) : ParentAdjA st G par s' := by + intro u hu hne + have huO : u ∈ OS := ((hM.members u).1 hu).1 + have nx : NxA st s' u = NxA st s u := by unfold NxA; rw [(k u huO).2.2.2] + rw [nx] at hne ⊢ + obtain ⟨b, hb, hd⟩ := h u hu hne + refine ⟨b, ?_, hd⟩ + have hv : NxA st s u ∈ OS := by + obtain ⟨ch, hch, hmem⟩ := List.mem_flatten.1 hu + obtain ⟨i, hi⟩ := List.mem_iff_getElem?.1 hmem + cases hn : ch[i + 1]? with + | some c => + rw [(hM.chain ch hch i u c hi hn).1] + exact ((hM.members c).1 (List.mem_flatten.2 ⟨ch, hch, List.mem_of_getElem? hn⟩)).1 + | none => + have hl : ch.getLast? = some u := by + have hlt : i < ch.length := (List.getElem?_eq_some_iff.1 hi).1 + have hge : ch.length ≤ i + 1 := List.getElem?_eq_none_iff.1 hn + rw [List.getLast?_eq_getElem?, show ch.length - 1 = i by omega] + exact hi + exact absurd (hM.last ch hch u hl) hne + rw [is_mdb_parent_of_eqA] at hb ⊢ + have e : isPMA st s' u (NxA st s u) = isPMA st s u (NxA st s u) := by + unfold isPMA Fl4ProofsA64.P1.W7.R.ST.revA Fl4ProofsA64.P1.W7.R.ST.fbA + rw [k.capA huO, k.capA hv, (k u huO).2.2.2, (k _ hv).2.2.2] + rw [e]; exact hb + +/-- The VSpace objects of the unmapped caps stay unmapped. -/ +theorem UnmPtSpecA.zeroA {Z : XSetsA} {m : Fl4SpecA64.McState} {OS : Set U64} {s : S} {σ σ' : Fl4SpecA64.State Unit} + {x : U64} (h : UnmPtSpecA st Z m OS s σ) (hx : Aligned x) (k : OSKeep st OS s (L.wr s x 0#u64)) + (al : ∀ p, SchedPageA st Z OS s p → ∀ i : U64, i.val < 512 → Aligned (peAF p i)) (HC : ClrA σ σ') : + UnmPtSpecA st Z m OS (L.wr s x 0#u64) σ' := by + have dead : ∀ w, Aligned w → dBitA (L.mem s w) 0 = false → dBitA (L.mem (L.wr s x 0#u64) w) 0 = false := by + intro w hw h0 + rw [L.mem_wr _ _ _ _ hx hw] + split + · exact dBitA_zeroA + · exact h0 + have tab : ∀ ty p, SchedPageA st Z OS s p → UnmTabA (L := L) st Z m s σ ty p → + UnmTabA (L := L) st Z m (L.wr s x 0#u64) σ' ty p := by + intro ty p hp U + obtain ⟨t, e, ety, inv⟩ := U.spec + obtain ⟨t', e', ty', en⟩ := HC.pt e + refine ⟨fun i hi => dead _ (al p hp i hi) (U.inv i hi), ⟨t', e', ty'.trans ety, fun i hi => ?_⟩, + ⟨fun i hi h => U.off.mem i hi + (footA_schedA (Z := Z) (Q := Z.Q) (Rl := Z.R) (curOf := Z.curOf) (curScOf := Z.curScOf) + (L.ksb_wr _ _ _) (L.cpu_wr _ _ _) + (FootA.monoA (Z := { Z with S1 := ∅ }) rfl rfl (fun _ h => h) (fun _ h => h) (fun _ h => h) + (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) h)), U.off.obj⟩⟩ + rcases en i.bv with q | q + · rw [q]; exact inv i hi + · exact q + exact + { pt := fun u hu p asid va hc => by + rw [k.capA hu] at hc + exact tab _ p (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inl ⟨_, _, _, hc⟩⟩))))))) (h.pt u hu p asid va hc) + vs := fun u hu p asid hc => by + rw [k.capA hu] at hc + exact tab _ p (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inl ⟨_, _, hc⟩)⟩))))))) (h.vs u hu p asid hc) + s2 := fun u hu p asid va hc => by + rw [k.capA hu] at hc + have U := h.s2 u hu p asid va hc + obtain ⟨es, e, inv⟩ := U.spec + obtain ⟨es', e', en⟩ := HC.s2 e + refine ⟨fun i hi => ?_, es', e', fun i => ?_⟩ + · by_cases h5 : i.val < 512 + · have hp : SchedPageA st Z OS s p := Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inr (Or.inl ⟨_, _, hc⟩))⟩)))))) + exact dead _ (al p hp i h5) (U.inv i hi) + · obtain ⟨j, hj, ej⟩ := peAF_hiA p i (by omega) hi + have hp : SchedPageA st Z OS s (wa p 4096#u64) := Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inr (Or.inr (Or.inl ⟨p, _, _, hc, rfl⟩)))⟩)))))) + have := U.inv i hi + rw [ej] at this ⊢ + exact dead _ (al _ hp j hj) this + · rcases en i with q | q + · rw [q]; exact inv i + · exact q + hv := fun u hu p asid hc => by + rw [k.capA hu] at hc + have U := h.hv u hu p asid hc + obtain ⟨l, e, inv⟩ := U.spec + obtain ⟨l', e', en⟩ := HC.hv e + have hp : SchedPageA st Z OS s (wa p 4096#u64) := Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inr (Or.inr (Or.inr ⟨p, _, hc, rfl⟩)))⟩)))))) + refine ⟨fun i hi => dead _ (al _ hp i hi) (U.inv i hi), l', e', fun i => ?_⟩ + rcases en i with q | q + · rw [q]; exact inv i + · exact q } + +/-- **`DStateA` at the per-step record** (Option T: `CnodeDecDelA`'s `DStateA.ofEntA` with the MDB facts it read from +`SysL` given: `MdbS` and `ZombiesFinal`). -/ +theorem DStateA.ofTA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (E : JEntTA st AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) (hM : MdbS st OS s) (zf : ZombiesFinal st OS s) + (hT : TreeA st OS Z.G.chains Z.G.par s) (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) (site : DelSiteA st Z.th OS s σ Z.G.chains) : + DStateA st Z.th OS s σ Z.G := by + obtain ⟨G', lk', rng', kind', nl', cls'⟩ := hM + exact + { rel := E.j.1.st + place := E.j.1.place + cov := site.cov + lk := hT.lk + rng := rng'.congr (lk'.chains_iff hT.lk E.ent.os.nz) + kind := kind' + cls := cls' + zf := zf + nz := E.ent.os.nz + sub := hsub + nul := hnul + fr := site.fr + irq := site.irq + cdt := E.j.1.cdt + tree := hT + nl := nl' + zw := site.zw + cv := fun y hy => by + by_cases hs : IsSlotA Z.th y + · exact (E.j.capObj hs).2.2.2.2 + · rw [hnul y hy hs]; exact trivial } + +set_option maxHeartbeats 1000000 in +/-- **The loop record across a table-word zero** (module docstring). -/ +theorem delVs_zeroKeepA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {ipa : U64} {s : S} {a a' : A} + {σ σ' : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7LA st (J7R2A st ar ipa)) s a σ m) + (D : DStateA st Z.th OS s σ Z.G) (hPA : ParentAdjA st Z.G.chains Z.G.par s) (hZT : ZombieTcbNoVcpuA st OS s) + {p i : U64} (hp : SchedPageA st Z OS s p) (hi : i.val < 512) + (hNA : ¬ AsW (L.ksb s) (peAF p i)) (hbf : ¬ BufW Bf (peAF p i)) + (jx : JAtZAX (L := L) AR Z (L.wr s (peAF p i) 0#u64) a' σ' m) (HS : VsSpecA σ σ') (HC : ClrA σ σ') + (hdr : ∀ y ∈ OS, ∀ q mp ad, capA st s y = .HighVSpace q mp ad → + L.mem (L.wr s (peAF p i) 0#u64) (wa q 4088#u64) = L.mem s (wa q 4088#u64)) + (R' : ∀ Pt Ub : Set U64, VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt → + (∀ q, SchedPageA st Z OS s q → q ∈ Pt) → J7R2A st ar ipa Pt Z H OS s σ m → + J7R2A st ar ipa Pt Z H OS (L.wr s (peAF p i) 0#u64) σ' m) : + DStateA st Z.th OS (L.wr s (peAF p i) 0#u64) σ' Z.G ∧ + JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7LA st (J7R2A st ar ipa)) + (L.wr s (peAF p i) 0#u64) a' σ' m ∧ + ParentAdjA st Z.G.chains Z.G.par (L.wr s (peAF p i) 0#u64) ∧ + ZombieTcbNoVcpuA st OS (L.wr s (peAF p i) 0#u64) ∧ OSKeep st OS s (L.wr s (peAF p i) 0#u64) ∧ + (∀ Rj' : Set U64 → JSlotA S, (∀ Pt Ub : Set U64, VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt → + (∀ q, SchedPageA st Z OS s q → q ∈ Pt) → J7R2A st ar ipa Pt Z H OS s σ m → Rj' Pt Z H OS (L.wr s (peAF p i) 0#u64) σ' m) → + J7LA st Rj' Z H OS (L.wr s (peAF p i) 0#u64) σ' m) := by + obtain ⟨Pt, Ub, vr, pn, arm, capc, vc, df, hicap, link, vcks, fu, ual, vcu, R⟩ := J.j7 + have vsr : VsR (L.ksb s) Pt (peAF p i) := vsW_pageA link hp hi + have hal : Aligned (peAF p i) := vr.al _ vsr + have kb : L.ksb (L.wr s (peAF p i) 0#u64) = L.ksb s := L.ksb_wr _ _ _ + have w : WritesIn st s (L.wr s (peAF p i) 0#u64) (PtW Pt) := by + refine (writesIn_wr hal).mono fun x hx => ?_ + subst hx + refine ⟨p, link p hp, core.num.U64.wrapping_mul i 8#u64, ?_, ?_, rfl⟩ + all_goals rw [wm8_valA hi]; omega + have w1 : WritesIn st s (L.wr s (peAF p i) 0#u64) (fun x => x = peAF p i) := writesIn_wr hal + have hP : ∀ Pt' Ub' : Set U64, VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub' (L.ksb s) Pt' → + (∀ q, SchedPageA st Z OS s q → q ∈ Pt') → ∀ b, b = peAF p i → VsR (L.ksb s) Pt' b := + fun Pt' _ _ link' b hb => hb ▸ vsW_pageA link' hp hi + have om : peAF p i ∈ H.omega ∧ peAF p i ∉ H.frame := ⟨vr.om _ vsr, vr.sg _ vsr⟩ + -- A64 P1's invariants + have osk := OSKeep.of_in J.ent.os w fun x hx b _ h1 h2 hF => vr.os b (Or.inl hF) ⟨x, hx, h1, h2⟩ + have hB := (blk_capw_wr (v := 0#u64) (BXI.blk J.ent.bxi) hal (Or.inr om)).bxi + have vsZ : VsZ st (L.ksb s) (utVsPtA st Pp Tb Lv s) s (L.wr s (peAF p i) 0#u64) := + VsZ.wr0 (fun b hb => J.vsOk.al b hb) hal + have vsT := utVsZero_keepA J.ent.vs kb vsZ + have vsOk' := utVsOk_zeroA J.ent.vs kb vsZ J.vsOk + have ent : EntryInvA3 st H Z.Cs OS U Ac Pp Tb Lv (L.wr s (peAF p i) 0#u64) := + ⟨J.ent.heap, hB, J.ent.os, J.ent.user.ksb kb, vsT.1, by rw [kb]; exact J.ent.ksw⟩ + have sq : SchedQueuesInv st H.tcbs Z.Cs H.omega H (L.wr s (peAF p i) 0#u64) Z.Q Z.R := + inv_of_capw J.sq w1 (fun b hb => Or.inl (Or.inl (by rw [hb]; exact om.1))) + have ht := tab_of_w vr w1 (fun b hb => hb ▸ hNA) + have poolOff : PoolOff st H Z.Cs OS (L.wr s (peAF p i) 0#u64) := PoolOff.ofTab kb ht J.poolOff + -- the unmapped objects, J5, J7 + have al : ∀ q, SchedPageA st Z OS s q → ∀ j : U64, j.val < 512 → Aligned (peAF q j) := + fun q hq j hj => vr.al _ (vsW_pageA link hq hj) + have unm : UnmPtSpecA st Z m OS (L.wr s (peAF p i) 0#u64) σ' := J.unmPt.zeroA hal osk al HC + have j5 := J.j5_vsWriteA w1 hP Z.hw σ' + have capE : ∀ u ∈ OS, capA st (L.wr s (peAF p i) 0#u64) u = capA st s u := fun u hu => osk.capA hu + have mk : ∀ Rj' : Set U64 → JSlotA S, (∀ Pt Ub : Set U64, VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt → + (∀ q, SchedPageA st Z OS s q → q ∈ Pt) → J7R2A st ar ipa Pt Z H OS s σ m → Rj' Pt Z H OS (L.wr s (peAF p i) 0#u64) σ' m) → + J7LA st Rj' Z H OS (L.wr s (peAF p i) 0#u64) σ' m := by + intro Rj' h + refine ⟨Pt, Ub, by rw [kb]; exact vr, PoolNamed.ofTab kb ht osk pn, ?_, ⟨UbFr.ofOSK osk capc.1, FrameOK.ofOSK osk capc.2⟩, + CapInv.keepO vc osk, CapInv.keepO df osk, HiCap511.keep hicap capE hdr, fun q hq => link q ?_, + by rw [kb]; exact vcks, FrUb.keepO fu osk, ual, vcu, h Pt Ub vr link R⟩ + · refine ⟨⟨arm.case.slot, arm.case.ts, PoolOff.ofTab kb ht arm.case.po, + PoolObj.of_tab kb (fun j => Or.inl (by have e := ht j; rwa [kb] at e)) arm.case.pobj⟩, + KsBridge.ofKsb kb arm.kb, arm.nt, UbPoolT.keep (w1.mono fun x hx h => hNA (hx ▸ h)) + (fun j => vr.al _ (Or.inr (asW_at _ j))) arm.pool, arm.vh⟩ + · unfold SchedPageA at hq ⊢ + refine hq.imp_right (·.imp_right (·.imp_right (·.imp_right (·.imp_right (·.imp_right (·.imp_right ?_)))))) + rintro ⟨u, hu, h⟩ + refine ⟨u, hu, ?_⟩ + rw [capE u hu] at h + exact h + have j7 := mk (J7R2A st ar ipa) R' + have J' := J.of_vsWrite (hw := Z.hw) (a' := a') (σ' := σ') w1 hP (fun b hb => hb ▸ hbf) HS (HC.dataA) jx ent vsOk' (bufVs_subA J.bufVs kb vsT.2) sq poolOff + unm j5 j7 + -- the deletion's related states + have tree := D.tree.keepO osk + have hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st (L.wr s (peAF p i) 0#u64) y = .Null := + fun y hy hn => (capE y hy).trans (D.nul y hy hn) + have site : DelSiteA st Z.th OS (L.wr s (peAF p i) 0#u64) σ' Z.G.chains := + { cov := ⟨fun q r cs hk hw => D.cov.cnode q r cs (HS.back hk (fun _ e => by cases e)) hw, + fun q x hk => D.cov.tcb q x (HS.back hk (fun _ e => by cases e))⟩ + fr := fun y hy z hz h1 h2 => by + rw [capE y hy] at h1; rw [capE z hz] at h2; rw [capE y hy, capE z hz] + exact D.fr y hy z hz h1 h2 + irq := Fl4ProofsA64.P3.IrqRunA.keepIrq D.irq fun y hy _ => by rw [capE y ((D.lk.members y).1 hy).1] + zw := fun y hy q zt k hc => by + rw [capE y (D.sub y hy)] at hc + exact D.zw y hy q zt k hc } + have D' : DStateA st Z.th OS (L.wr s (peAF p i) 0#u64) σ' Z.G := + DStateA.ofTA J'.toJEntTA (D.mdbS.keep osk) (ZombiesFinal.recap D.zf fun y hy => Or.inl (capE y hy)) tree D.sub hnul site + have hZT' : ZombieTcbNoVcpuA st OS (L.wr s (peAF p i) 0#u64) := by + intro u hu q k hc + rw [capE u hu] at hc + have old := hZT u hu q k hc + have hs : IsSlotA Z.th u := by + by_contra hn + rw [D.nul u hu hn] at hc; cases hc + have htcb : Z.th q = some .tcb := by + rcases D.zw u hs _ _ _ hc with ⟨-, h, -⟩ | ⟨h, -⟩ + · exact h + · exact absurd h (by simp [capability.ZOMBIE_TCB]) + have hT : q ∈ Z.T := (J.j.1.thT q).1 htcb + have hta := J.sr.core.tcbs q hT + have hv : (wa q 3664#u64).val = q.val + 3664 := field_val hta (K := 3664#u64) (by simp) + have hH : q ∈ H.tcbs := by rw [J.tcbs]; exact hT + obtain ⟨Qb, Rb, qq⟩ := J.ent.bxi.1 + have off := (qq.omega _ om.1).2.1 q hH + rw [L.mem_wr_ne hal (field_aligned (K := 3664#u64) hta (by simp) (by simp)) (by omega)] + exact old + exact ⟨D', J', ParentAdjA.keepOS D.lk osk hPA, hZT', osk, mk⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DevFrDisjA.lean b/proofs/Fl4ProofsA64/P3/DevFrDisjA.lean new file mode 100644 index 000000000..fee41f279 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DevFrDisjA.lean @@ -0,0 +1,26 @@ +/- +P3 (AArch64): cross-kind separation for live device-backed frame caps. +[J] The Stage-3 typed frame correspondence supplies this predicate from +boot and preserves it through retype and capability operations. +-/ +import Fl4ProofsA64.P3.PtrA +import Fl4ProofsA64.P1.MdbA64 + +open Aeneas Aeneas.Std + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4SpecA64 + +/-- A live device-backed frame names no live non-frame object. The P1 +heap's `.other` tag also covers CNodes and translation tables, so the +abstract heap identifies data pages among objects tagged `.other`. +The concrete Frame constructor covers both native and Stage-2 caps. -/ +def DevFrDisjA {S : Type} (st : machine.Store S) [LawfulStore st] + (H : Heap) (OS : Set U64) (s : S) (σ : State Unit) : Prop := + ∀ y ∈ OS, ∀ p size rights mapped ept asid va, + capA st s y = .Frame p size rights true mapped ept asid va → + ∀ k, H p = some k → + (∃ bits, k = .other bits) ∧ + ∃ dev pg, σ.kheap (specPtr p) = some (.archObj (.dataPage dev pg)) + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/DomainSetDispatchA.lean b/proofs/Fl4ProofsA64/P3/DomainSetDispatchA.lean new file mode 100644 index 000000000..c7419f4a2 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/DomainSetDispatchA.lean @@ -0,0 +1,274 @@ +/- +P3 (AArch64): DomainSetSet dispatch and ordered validation. +[S] The successful local path uses the A64 one-domain configuration. +The remote stall is kept in the extracted program so a caller must +establish its real post before composing the scheduling update. +-/ +import Fl4ProofsA64.P3.DelSpecA +import Fl4ProofsA64.P3.NtfnSpecA +import Fl4ProofsA64.P3.ScSetStateA +import Fl4ProofsA64.P3.ScYieldQueueA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4SpecA64 + +noncomputable section + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {s : S} {a : A} {len slot buf : U64} {b c d fp : Bool} +variable {extra : Array obj.Slot 3#usize} + +theorem decodeDomain_programA {lbl : U64} : + syscall.decode_invocation st ar s a lbl len slot .Domain b c d fp buf extra = + if fp then ok (.Err (.Sys (.InvalidCapability 0#u64)), s, a) + else domain.decode st ar s a lbl len buf extra := by + simp only [syscall.decode_invocation, cur_thread_ok, bind_ok] + +theorem domainSet_emptyA : + domain.decode st ar s a fl4_abi.label.DomainSetSet 0#u64 buf extra = + ok (.Err (.Sys .TruncatedMessage), s, a) := by + simp [domain.decode, cur_thread_ok, error.truncated, bind_ok] + +theorem domainSet_rangeA {dom : U64} (hl : len ≠ 0#u64) + (hd : msg.syscall_arg st s (L.curW s) buf 0#u64 = ok dom) (hb : dom ≥ layout.NUM_DOMAINS) : + domain.decode st ar s a fl4_abi.label.DomainSetSet len buf extra = + ok (.Err (.Sys (.InvalidArgument 0#u64)), s, a) := by + simp only [domain.decode, cur_thread_ok, bind_ok, if_pos rfl, if_neg hl, hd, if_pos hb, error.invalid_arg, if_true] + +theorem domainSet_noExtraA {dom : U64} (hl : len ≠ 0#u64) + (hd : msg.syscall_arg st s (L.curW s) buf 0#u64 = ok dom) (hb : ¬ dom ≥ layout.NUM_DOMAINS) + (he : Array.index_usize extra 0#usize = ok (0#u64 : obj.Slot)) : + domain.decode st ar s a fl4_abi.label.DomainSetSet len buf extra = + ok (.Err (.Sys .TruncatedMessage), s, a) := by + rw [domain.decode, cur_thread_ok, bind_ok, if_pos rfl, if_neg hl, hd, bind_ok, if_neg hb, he, bind_ok] + simp [obj.Slot.is_null, error.truncated, bind_ok] + +theorem domainSet_wrongExtraA {dom x0 : U64} {cap : capability.Cap} (hl : len ≠ 0#u64) + (hd : msg.syscall_arg st s (L.curW s) buf 0#u64 = ok dom) (hb : ¬ dom ≥ layout.NUM_DOMAINS) + (he : Array.index_usize extra 0#usize = ok x0) (hx : x0 ≠ 0#u64) + (hc : obj.Slot.cap st x0 s = ok cap) (hn : ∀ t, cap ≠ .Thread t) : + domain.decode st ar s a fl4_abi.label.DomainSetSet len buf extra = + ok (.Err (.Sys (.InvalidArgument 1#u64)), s, a) := by + rw [domain.decode, cur_thread_ok, bind_ok, if_pos rfl, if_neg hl, hd, bind_ok, if_neg hb, he, bind_ok] + simp only [obj.Slot.is_null, bind_ok, decide_eq_true_eq, if_neg hx, hc] + cases cap <;> first | exact (hn _ rfl).elim | simp only [error.invalid_arg, bind_ok] + +/-- The accepted branch of the concrete decoder, with the stall, Restart +and scheduling update in their actual order. -/ +theorem domainSet_acceptA {dom x0 t : U64} (hl : len ≠ 0#u64) + (hd : msg.syscall_arg st s (L.curW s) buf 0#u64 = ok dom) (hb : ¬ dom ≥ layout.NUM_DOMAINS) + (he : Array.index_usize extra 0#usize = ok x0) (hx : x0 ≠ 0#u64) + (hc : obj.Slot.cap st x0 s = ok (.Thread t)) : + domain.decode st ar s a fl4_abi.label.DomainSetSet len buf extra = (do + let (s1, a1) ← smp.remote_tcb_stall st ar s a t + let s2 ← sched.set_thread_state st s1 (L.curW s) sched.TS_RESTART + let s3 ← sched.set_domain st s2 t dom + ok (.Ok (), s3, a1)) := by + rw [domain.decode, cur_thread_ok, bind_ok, if_pos rfl, if_neg hl, hd, bind_ok, if_neg hb, he, bind_ok] + simp [obj.Slot.is_null, hx, hc, bind_ok] + +end + +section +variable {σ : State Unit} + +/-- DomainSet has four distinct outcomes before dispatching to setDomain. +Validation of the domain word precedes inspecting the extra capabilities. -/ +theorem domainSet_specA (args : List Data) (caps : List (Cap × CslotPtr)) : + (decodeDomainSet (Z := Unit) args caps).run σ = + ({((match args with + | [] => .error .truncatedMessage + | dom :: _ => + if dom.toNat ≥ numDomains then .error (.invalidArgument 0) + else match caps with + | [] => .error .truncatedMessage + | (.threadCap t, _) :: _ => .ok (.invokeDomainSet t (ucast domainLen dom)) + | _ => .error (.invalidArgument 1)), σ)}, False) := by + cases args with + | nil => + simp only [decodeDomainSet, Fl4Spec.NonDetE.throwError, ndE_throw_bindD] + rfl + | cons dom rest => + by_cases h : dom.toNat ≥ numDomains + · simp only [decodeDomainSet, unat, Fl4Spec.NonDetE.whenE, if_pos h, + Fl4Spec.NonDetE.throwError, ndE_throw_bindD] + rfl + · simp only [decodeDomainSet, unat, Fl4Spec.NonDetE.whenE, if_neg h, + Fl4Spec.NonDetE.returnOk, ndE_pure_bindD] + cases caps with + | nil => + simp only [List.length_nil, if_pos rfl, if_true, Fl4Spec.NonDetE.throwError, ndE_throw_bindD] + rfl + | cons cap caps => + obtain ⟨cap, ptr⟩ := cap + simp only [List.length_cons, Nat.succ_ne_zero, if_false, ndE_pure_bindD] + cases cap <;> rfl + +/-- [S] The one-domain branch needs no VCPU or FPU domain preparation. -/ +theorem invokeDomainSet_sameA {t : ObjRef} {d : Domain} (h : σ.curDomain = d) : + invokeDomain (Z := Unit) (.invokeDomainSet t d) σ = setDomain t d σ := by + change (archPrepareSetDomain t d >>= fun _ => setDomain t d) σ = _ + have prep : archPrepareSetDomain (Z := Unit) t d σ = ({((), σ)}, False) := by + unfold archPrepareSetDomain + rw [Sched.nd_gets_bindSc] + simp [h, Fl4Spec.NonDet.when] + rfl + rw [Sched.nd_bind_oneSc prep] + +end + +open Sched in +/-- Rewriting the existing zero domain is invisible to both stores. +The concrete state itself can change, so retain its aligned-word frame. -/ +private theorem domainWord_zeroA {S : Type} {st : machine.Store S} [L : LawfulStore st] + {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRA st Z m κ s σ) (hs : StEqA Z.T (L.mem s) σ) {t : U64} (ht : t ∈ Z.T) : + obj.Tcb.set_domain st t s 0#u64 ⦃ s' => + threadSetDomain (Z := Unit) (specPtr t) 0 σ = ({((), σ)}, False) ∧ + WritesIn st s s' (fun _ => False) ∧ SchedSRA st Z m κ s' σ ∧ StEqA Z.T (L.mem s') σ ⦄ := by + obtain ⟨x, hk, hd, hw, -⟩ := H.rest.prio t ht + have hspec : threadSetDomain (Z := Unit) (specPtr t) 0 σ = ({((), σ)}, False) := by + unfold threadSetDomain + rw [Notif.threadSet_eq _ hk] + have e : Notif.updK σ (specPtr t) (.tcb { x with tcbDomain := 0 }) = σ := by + rw [← hd] + unfold Notif.updK + rw [show ({ x with tcbDomain := x.tcbDomain } : Tcb) = x from rfl, ← hk, Function.update_eq_self] + rw [e] + have hal : Aligned (domA t) := field_aligned (H.core.tcbs t ht) (by decide) (by decide) + have hf : WritesIn st s (L.wr s (domA t) 0#u64) (fun _ => False) := by + refine ⟨L.ksb_wr _ _ _, L.cpu_wr _ _ _, fun b hb _ => ?_⟩ + rw [L.mem_wr _ _ _ _ hal hb] + split + · next h => rw [← h, hw] + · rfl + unfold obj.Tcb.set_domain + have off : layout.tcb.DOMAIN = ok 3432#u64 := eq_of_spec_fd Fl4ProofsA64.P0.Consts.layout.tcb.DOMAIN.spec + rw [off] + simp only [bind_ok, lift, L.write_eq] + refine (WP.spec_ok _).2 ⟨hspec, hf, ?_, ?_⟩ + · exact H.stableR hf (fun _ h => h.elim) + ⟨SFixA.refl _, rfl, rfl, rfl, fun _ _ _ _ => rfl, fun _ x h => ⟨x, h, rfl, rfl, rfl⟩, + fun _ _ _ _ => rfl, fun _ x h => ⟨x, h⟩, fun _ x h => ⟨x, h, rfl⟩⟩ H.run H.rd + · exact hs.keep (fun u hu => hf.mem_eq (by + rw [stA_eq] + exact field_aligned (H.core.tcbs u hu) (by decide) (by decide)) id) (fun _ x h => ⟨x, h, rfl⟩) + +open Sched in +private theorem domainQueueStatesA {S : Type} {st : machine.Store S} [L : LawfulStore st] + {Z : XSetsA} {m : McState} {κ : U64 → U64} {s s' : S} {σ σ' : State Unit} + (H : SchedSRA st Z m κ s σ) (hs : StEqA Z.T (L.mem s) σ) + (hk : σ'.kheap = σ.kheap) (F : MFrA (st := st) Z m s s' σ σ') : + StEqA Z.T (L.mem s') σ' := + hs.keep (fun t ht => by + rw [stA_eq] + exact yieldWordKeepA H.toSchedSRN H.curT (F.wr.mono fun _ h => Or.inl h) ht (Or.inl rfl)) + (fun _ x h => ⟨x, hk ▸ h, rfl⟩) + +open Sched in +/-- [S] The target is on this core. Dequeue it, write the single domain, +enqueue if schedulable, and reschedule exactly when it is current. -/ +theorem setDomain_localA {S : Type} {st : machine.Store S} [L : LawfulStore st] + {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRA st Z m κ s σ) (hs : StEqA Z.T (L.mem s) σ) + {t : U64} (ht : t ∈ Z.T) (hon : L.mem s (affA t) = L.cpu s) : + sched.set_domain st s t 0#u64 ⦃ s' => ∃ σ' Q', + setDomain (Z := Unit) (specPtr t) 0 σ = ({((), σ')}, False) ∧ + SchedSRA st { Z with Q := Q' } m κ s' σ' ∧ StEqA Z.T (L.mem s') σ' ⦄ := by + have finish {Q₁ : U64 → U64 → List U64} {s₁ : S} {σ₁ : State Unit} + (H₁ : SchedSRA st { Z with Q := Q₁ } m κ s₁ σ₁) (hs₁ : StEqA Z.T (L.mem s₁) σ₁) : + (do + let ct ← sched.cur_thread st s₁ + let same ← obj.Tcb.Insts.CoreCmpPartialEqTcb.eq t ct + if same then sched.reschedule_required st s₁ else ok s₁) ⦃ s' => + ∃ σ' Q', Fl4Spec.NonDet.when (specPtr t = σ.curThread) + (rescheduleRequired (Z := Unit)) σ₁ = ({((), σ')}, False) ∧ + SchedSRA st { Z with Q := Q' } m κ s' σ' ∧ StEqA Z.T (L.mem s') σ' ⦄ := by + have cur : σ.curThread = specPtr (L.curW s₁) := by + exact H.sch.cur.symm.trans (H₁.sch.cur.trans H₁.sch.curThread) + simp only [cur_thread_ok, bind_ok, obj.Tcb.Insts.CoreCmpPartialEqTcb.eq, decide_eq_true_eq, cur] + by_cases h : t = L.curW s₁ + · rw [if_pos h] + rw [Fl4Spec.NonDet.when, if_pos (congrArg specPtr h)] + refine WP.spec_mono (reschedule_required_refinesA H₁ hs₁) ?_ + rintro s₂ ⟨σ₂, Q₂, he, hk, -, hw, H₂⟩ + have hs₂ := domainQueueStatesA H₁ hs₁ hk (MFrA.of_sw (m := m) hk hw) + exact ⟨σ₂, Q₂, he, H₂, hs₂⟩ + · rw [if_neg h, Fl4Spec.NonDet.when, if_neg (fun e => h (specPtr_inj e))] + exact (WP.spec_ok _).2 ⟨σ₁, Q₁, rfl, H₁, hs₁⟩ + unfold sched.set_domain + refine WP.spec_bind (dequeue_refines_localA H ht hon) ?_ + rintro s1 ⟨σ1, hdeq, hk1, -, -, H1, F1⟩ + have hs1 := domainQueueStatesA H hs hk1 F1 + refine WP.spec_bind (domainWord_zeroA H1 hs1 ht) ?_ + rintro s2 ⟨hword, -, H2, hs2⟩ + have hlocal := yieldAffKeepA H H2 ht ht hon + rw [is_schedulable_eqA, bind_ok, schedulable_eqA H2.toSchedSRN ht (H2.relOn ht hlocal) hs2] + have spec : setDomain (Z := Unit) (specPtr t) 0 σ = + (do + Fl4Spec.NonDet.when (schedulable (specPtr t) σ1 = true) + (tcbSchedAction (Z := Unit) tcbSchedEnqueue (specPtr t)) + Fl4Spec.NonDet.when (specPtr t = σ.curThread) (rescheduleRequired (Z := Unit))) σ1 := by + unfold setDomain + rw [nd_gets_bindSc, nd_bind_oneSc hdeq, nd_bind_oneSc hword, nd_gets_bindSc] + by_cases h : schedulable (specPtr t) σ1 = true + · rw [if_pos h, spec] + obtain ⟨hrun, hnrel⟩ := schedulable_partsA h + refine WP.spec_bind (enqueue_refines_localA H2 t ht hlocal + (fun _ => (H2.relOn ht hlocal).2 hnrel) hrun) ?_ + rintro s3 ⟨σ3, henq, hk3, -, -, w3, H3⟩ + have f3 := MFrA.of_sw (m := m) hk3 (w3.mono fun _ h => H2.toSchedSRN.enq_swr ht h) + have hs3 := domainQueueStatesA H2 hs2 hk3 f3 + refine WP.spec_mono (finish H3 hs3) ?_ + rintro s4 ⟨σ4, Q4, he, hS, hT⟩ + refine ⟨σ4, Q4, ?_, hS, hT⟩ + rw [Fl4Spec.NonDet.when, if_pos h, nd_bind_oneSc henq] + exact he + · rw [if_neg h, bind_ok, spec] + refine WP.spec_mono (finish H2 hs2) ?_ + rintro s4 ⟨σ4, Q4, he, hS, hT⟩ + refine ⟨σ4, Q4, ?_, hS, hT⟩ + rw [Fl4Spec.NonDet.when, if_neg h] + rw [nd_bind_oneSc (show Fl4Spec.NonDet.pure () σ1 = ({((), σ1)}, False) from rfl)] + exact he + +open Sched in +/-- [S] Successful DomainSet on the one-domain configuration, with a +local target and a concrete no-op stall. The stall premise also excludes +a guest target that would require stalling a remote RUN_CALLER. -/ +theorem decodeInvocation_domainSet_localA {S A : Type} {st : machine.Store S} {ar : machine.Arch A} + [L : LawfulStore st] {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {a : A} + {σ : State Unit} (H : SchedSRA st Z m κ s σ) (hs : StEqA Z.T (L.mem s) σ) + {len src buf x0 t : U64} {extra : Array obj.Slot 3#usize} {block call donate : Bool} + (ht : t ∈ Z.T) (hon : L.mem s (affA t) = L.cpu s) + (hstall : smp.remote_tcb_stall st ar s a t = ok (s, a)) + {xc : Tcb} (hc : σ.kheap (specPtr (L.curW s)) = some (.tcb xc)) + (hr : runnable xc.tcbState = true) + (hl : len ≠ 0#u64) (hd : msg.syscall_arg st s (L.curW s) buf 0#u64 = ok 0#u64) + (he : Array.index_usize extra 0#usize = ok x0) (hx : x0 ≠ 0#u64) + (hextra : obj.Slot.cap st x0 s = ok (.Thread t)) : + syscall.decode_invocation st ar s a fl4_abi.label.DomainSetSet len src .Domain block call donate + false buf extra ⦃ r => r.1 = .Ok () ∧ r.2.2 = a ∧ ∃ σ1 σ2 Q', + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + invokeDomain (Z := Unit) (.invokeDomainSet (specPtr t) 0) σ1 = ({((), σ2)}, False) ∧ + SchedSRA st {Z with Q := Q'} m κ r.2.1 σ2 ∧ StEqA Z.T (L.mem r.2.1) σ2 ⦄ := by + rw [decodeDomain_programA] + change domain.decode st ar s a fl4_abi.label.DomainSetSet len buf extra ⦃ _ => _ ⦄ + have hb : ¬ (0#u64 : U64) ≥ layout.NUM_DOMAINS := by + rw [layout.NUM_DOMAINS, fl4_abi.config.NUM_DOMAINS] + decide + rw [domainSet_acceptA hl hd hb he hx hextra, hstall, bind_ok] + have code : sched.TS_RESTART.val = tsCodeA .restart := by rw [ts_vals.2.1]; rfl + refine WP.spec_bind (sc_set_runnable_stateA H hs H.curT hc hr (ts := .restart) rfl code) ?_ + rintro s1 ⟨σ1, hR, H1, hs1, -, -⟩ + refine WP.spec_bind (setDomain_localA H1 hs1 ht (yieldAffKeepA H H1 ht ht hon)) ?_ + rintro s2 ⟨σ2, Q', hD, H2, hs2⟩ + exact (WP.spec_ok _).2 ⟨rfl, rfl, σ1, σ2, Q', by simpa only [H.sch.curThread] using hR, + (invokeDomainSet_sameA H1.rest.curDom.1).trans hD, H2, hs2⟩ + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/EmptyA.lean b/proofs/Fl4ProofsA64/P3/EmptyA.lean new file mode 100644 index 000000000..4b7243b6b --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/EmptyA.lean @@ -0,0 +1,745 @@ +/- +P3 (aarch64, A64-P3 S3 gen 4, step 2): **the first joint CSpace leaf: FL4's `cdt::empty_slot` refines l4v +`empty_slot`** (IpcCancel_A, nondeterministic spec, `Z = Unit`) at a CNode slot of the typed heap with no cleanup +cap — the twin of x86's `P3/EmptyRefine` (`empty_slot_refines`), with the pieces it rests on. + +* **Slot indices** (x86 `SetCap`'s `blVal` / `blVal_lt` / `ofBl_eq` / `idx_inj`, `CapRel`'s `ofBl_step` / + `ofBl_foldl_toNat`): `blvA` (a bit list's value), `ofBlA_eq` (`ofBl idx = ofNat (blvA idx)` below 64 bits), + `blvA_inj`. +* **Slots by value** (x86 `IsSlotAt` / `slotOf_inj`, `MdbInsert.cslotAddr_inj`): `SlotVA th y q j` (slot `j` of the + object at `q`), `SlotVA.inj` (from `PlaceA`), `cn_slotV` / `tcb_slotV` / `IsSlotA.v`, **`cslotAddrA_inj`**, + `AbsSlotA.isSlot`, `slotW_isSlot`. +* **The spec's outcomes** (x86 `Lookup.getCap_cnode`, `SetCap.setCap_cnode`, `InsertSpec.doExtendedOp_unit` / + `emptySlot_eq`, as memberships): `getCap_cnode_memA`, `setCap_cnode_memA`, `doExtendedOp_memA`, `emptySlotCdtStateA`, + `emptySlot_memA`. +* **`StateRelA.setCn`** (x86 `StateRel.update_cnode_slot`): `StateRelA` after a CNode slot gets a related cap and + every other slot of `th` keeps its cap. **`CdtRelA.removeA`** (x86 `MdbRemove.CdtRel.remove`): the spec's CDT + after `empty_slot` (the slot's children get its parent, `Mdb.removePar`) against A64 P1's tree after it. +* FL4 side (A64 P1's `cdt.empty_slot.mdbFLG`, `.rvb`, `.tree`, with the ghosts named): `emptySlot_arNullA` + (a null cleanup keeps the arch state), **`emptySlot_treeGA`** (`TreeA` at `remG x G` / `Mdb.removePar x par`, + the other slots' caps and bit 48 kept, the slot's `NEXT` word zero). +* **`empty_slot_refinesA`** (x86 `empty_slot_refines`): from `StateRelA`, `CdtRelA`, `PlaceA` and A64 P1's MDB facts + at a slot set `OS` covering `th`'s slots (null outside them), FL4's `empty_slot` of a non-null CNode slot with a + `Null` cleanup keeps the arch state and ends in a state related (`StateRelA`, `CdtRelA` at the ghost + `remGA G x`) to an outcome of the spec's `empty_slot slot NullCap`, with A64 P1's `TreeA` / `RngOneA64` / `KindA` + at the new ghost. The spec side keeps `SInvXA` (`SInvXA.emptySlot`, `P3/SpecFrA`). Gen 5 (in place, ruling 11:09Z + item 3): its post also states that the spec step is slot-only (`SpecSlotA`, from `SpecSlotA.setCn`) and that every + slot ends null or with its old cap — what `JAtZAX.of_slotWrites` needs (`P3/SlotJA`'s `empty_slot_jA`). + +* (gen 8, ruling 12:48Z item 1) **TCB slots** (x86 `DelAbs`): `setObjA`, `tcbSetA` (`set_cap`'s TCB branch, the `ext` + hook's slots 5 and 6 included), `tcbNcA_setA`, `tcbMap_setA` (x86 `tcbCnodeMap_set`), `getCap_tcb_memA`, + `setCap_tcb_memA` (x86 `setCap_tcb`), `SpecSlotA.setTcbA`, **`StateRelA.setTcbA`** (x86 `StateRel.update_tcb_slot`), + and the slot-generic **`StateRelA.getAbsA`** / **`StateRelA.setAbsA`** (x86 `StateRel.update_abs_slot`): `get_cap` / + `set_cap` of any `AbsSlotA` slot, CNode or TCB, with the CSpace relation after it and a `SpecSlotA` step. + `empty_slot_refinesA` now takes any `AbsSlotA` slot (statement changed: `xs`, `hxs` replace `p`, `r`, `hp`, `idx`, + `hl`). + +Not here (sized in the handoff): a non-null cleanup (`post_cap_deletion` of an IRQ handler). The +non-CSpace part of `JAtZAX` across the step is gen 5's `P3/SlotJA`. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.SpecFrA +import Fl4ProofsA64.P1.MdbTreeU +import Fl4ProofsA64.P1.MdbTreeF +import Fl4ProofsA64.P1.MdbRstZ +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## Slot indices -/ + +/-- The value of a bit list, most significant first (x86 `blVal 0`). -/ +def blvA : List Bool → Nat + | [] => 0 + | b :: l => (if b then 2 ^ l.length else 0) + blvA l + +theorem blvA_lt : ∀ l : List Bool, blvA l < 2 ^ l.length + | [] => by simp [blvA] + | b :: l => by + have := blvA_lt l + simp only [blvA, List.length_cons, pow_succ] + split <;> omega + +theorem blvA_inj : ∀ {l l' : List Bool}, l.length = l'.length → blvA l = blvA l' → l = l' + | [], [], _, _ => rfl + | [], _ :: _, hl, _ => by simp at hl + | _ :: _, [], hl, _ => by simp at hl + | b :: l, b' :: l', hl, h => by + simp only [List.length_cons, Nat.add_right_cancel_iff] at hl + have h1 := blvA_lt l + have h2 := blvA_lt l' + simp only [blvA, hl] at h + rw [hl] at h1 + have hb : b = b' := by cases b <;> cases b' <;> first | rfl | (simp at h; omega) + subst hb + have e : blvA l = blvA l' := by cases b <;> simp at h <;> omega + rw [blvA_inj hl e] + +theorem ofBl_stepA (acc : BitVec 64) (b : Bool) : + ((acc <<< 1) ||| (if b then 1 else 0)) = acc + acc + (if b then 1 else 0) := by + have h0 : acc <<< 1 &&& (if b then (1 : BitVec 64) else 0) = 0#64 := by + ext i hi + cases b <;> simp only [BitVec.getElem_and, BitVec.getElem_shiftLeft] <;> interval_cases i <;> simp + rw [← BitVec.add_eq_or_of_and_eq_zero _ _ h0, BitVec.shiftLeft_eq_mul_twoPow] + congr 1 + rw [show BitVec.twoPow 64 1 = 2#64 from rfl, BitVec.mul_two] + +theorem ofBl_foldA : ∀ (l : List Bool) (acc : BitVec 64), acc.toNat * 2 ^ l.length + blvA l < 2 ^ 64 → + (l.foldl (fun (acc : BitVec 64) b => (acc <<< 1) ||| (if b then (1 : BitVec 64) else 0)) acc).toNat = + acc.toNat * 2 ^ l.length + blvA l + | [], acc, _ => by simp [blvA] + | b :: l, acc, h => by + simp only [List.foldl_cons] + have hp : 1 ≤ 2 ^ l.length := Nat.one_le_two_pow + have e2 : acc.toNat * 2 ^ (b :: l).length + blvA (b :: l) = + (2 * acc.toNat + (if b then 1 else 0)) * 2 ^ l.length + blvA l := by + simp only [blvA, List.length_cons, pow_succ]; cases b <;> simp <;> ring + rw [e2] at h ⊢ + have hlt : 2 * acc.toNat + (if b then 1 else 0) < 2 ^ 64 := by + have : (2 * acc.toNat + (if b then 1 else 0)) * 1 ≤ (2 * acc.toNat + (if b then 1 else 0)) * 2 ^ l.length := + Nat.mul_le_mul_left _ hp + omega + have e : (acc <<< 1 ||| (if b then (1 : BitVec 64) else 0)).toNat = 2 * acc.toNat + (if b then 1 else 0) := by + rw [ofBl_stepA, BitVec.toNat_add, BitVec.toNat_add] + have hc : (if b then (1 : BitVec 64) else 0).toNat = (if b then 1 else 0) := by cases b <;> rfl + rw [hc, Nat.mod_eq_of_lt (by omega : acc.toNat + acc.toNat < 2 ^ 64), Nat.mod_eq_of_lt (by omega)] + omega + rw [ofBl_foldA l _ (by rw [e]; exact h), e] + +/-- **`ofBl` below 64 bits is the list's value** (x86 `ofBl_eq`). -/ +theorem ofBlA_eq {l : List Bool} (hl : l.length ≤ 64) : + (ofBl l : BitVec 64) = BitVec.ofNat 64 (blvA l) := by + have h1 := blvA_lt l + have h2 : 2 ^ l.length ≤ 2 ^ 64 := Nat.pow_le_pow_right (by norm_num) hl + apply BitVec.eq_of_toNat_eq + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)] + unfold ofBl + exact (ofBl_foldA l 0 (by simp; omega)).trans (by simp) + +/-! ## Slots by value -/ + +/-- **Slot `j` of the object at `q`** of `th`, by value (x86 `IsSlotAt` / `slotOf`). -/ +def SlotVA (th : TypedHeapA) (y q : U64) (j : Nat) : Prop := + ∃ k, th q = some k ∧ 32 * (j + 1) ≤ k.size ∧ y.val = q.val + 32 * j + +/-- Two slots at one address are the same slot of the same object (x86 `slotOf_inj`). -/ +theorem SlotVA.inj {th : TypedHeapA} {kb : U64} (hP : PlaceA th kb) {y q q' : U64} {j j' : Nat} + (h : SlotVA th y q j) (h' : SlotVA th y q' j') : q = q' ∧ j = j' := by + obtain ⟨k, hk, hs, hv⟩ := h + obtain ⟨k', hk', hs', hv'⟩ := h' + by_cases e : q = q' + · subst e; exact ⟨rfl, by omega⟩ + · exfalso + rcases hP.apart q q' k k' hk hk' e with h1 | h1 <;> omega + +/-- A slot address by value. -/ +theorem slotAddrA_valN {p : U64} {j : Nat} (h : p.val + 32 * j < 2 ^ 64) : + (slotAddrA (specPtr p) (BitVec.ofNat 64 j)).val = p.val + 32 * j := by + rw [slotAddrA_sp] + show (p.bv + BitVec.ofNat 64 j * 32#64).toNat = _ + have e : p.bv.toNat = p.val := rfl + simp only [BitVec.toNat_add, BitVec.toNat_mul, BitVec.toNat_ofNat, e] + have hj : j % 2 ^ 64 = j := Nat.mod_eq_of_lt (by omega) + rw [hj] + omega + +theorem slotVA_of {th : TypedHeapA} {kb : U64} (hP : PlaceA th kb) {p : U64} {k : ObjKindA} (hp : th p = some k) + {j : Nat} (hj : 32 * (j + 1) ≤ k.size) : SlotVA th (slotAddrA (specPtr p) (BitVec.ofNat 64 j)) p j := + ⟨k, hp, hj, slotAddrA_valN (by have := hP.fit p k hp; omega)⟩ + +theorem cnode_size (r : Nat) : (ObjKindA.cnode r).size = 32 * 2 ^ r := by + show 2 ^ (r + 5) = _; rw [pow_add]; ring + +/-- A CNode of `th` has radix below 60. -/ +theorem cnode_r {th : TypedHeapA} {kb : U64} (hP : PlaceA th kb) {p : U64} {r : Nat} (hp : th p = some (.cnode r)) : + r < 60 := by + have h := hP.fit p _ hp + rw [cnode_size] at h + by_contra hr + have : 2 ^ 60 ≤ 2 ^ r := Nat.pow_le_pow_right (by norm_num) (by omega) + have : (2 : Nat) ^ 64 = 16 * 2 ^ 60 := by norm_num + omega + +/-- **A CNode slot named by an index** is slot `blvA idx` (x86 `slotAddr_ofBl`). -/ +theorem cn_slotV {th : TypedHeapA} {kb : U64} (hP : PlaceA th kb) {p : U64} {r : Nat} (hp : th p = some (.cnode r)) + {idx : List Bool} (hl : idx.length = r) : + slotAddrA (specPtr p) (ofBl idx) = slotAddrA (specPtr p) (BitVec.ofNat 64 (blvA idx)) ∧ blvA idx < 2 ^ r ∧ + SlotVA th (slotAddrA (specPtr p) (ofBl idx)) p (blvA idx) := by + have hr := cnode_r hP hp + have e : slotAddrA (specPtr p) (ofBl idx) = slotAddrA (specPtr p) (BitVec.ofNat 64 (blvA idx)) := by + rw [ofBlA_eq (by omega)] + have hb : blvA idx < 2 ^ r := hl ▸ blvA_lt idx + refine ⟨e, hb, ?_⟩ + rw [e] + exact slotVA_of hP hp (by rw [cnode_size]; omega) + +theorem tcb_slotV {th : TypedHeapA} {kb : U64} (hP : PlaceA th kb) {t : U64} (ht : th t = some .tcb) {i : Nat} + (hi : i < tcbSlotsA) : SlotVA th (slotAddrA (specPtr t) (BitVec.ofNat 64 i)) t i := + slotVA_of hP ht (by show 32 * (i + 1) ≤ 4096; unfold tcbSlotsA at hi; omega) + +/-- A slot of a (sub-)typed heap lies in its object. -/ +theorem ut_ownerA {th th' : TypedHeapA} {kb : U64} (P : PlaceA th kb) (sub : ∀ q k, th' q = some k → th q = some k) + {y : U64} (h : IsSlotA th' y) : ∃ q k, th' q = some k ∧ q.val ≤ y.val ∧ y.val + 32 ≤ q.val + k.size := by + obtain ⟨q, ⟨r, hr, i, hi, rfl⟩ | ⟨ht, i, hi, rfl⟩⟩ := h + · obtain ⟨k, hk, hj, hv⟩ := slotVA_of P (sub _ _ hr) (j := i) (by rw [cnode_size]; omega) + have e : k = .cnode r := Option.some.inj (hk.symm.trans (sub _ _ hr)) + subst e + exact ⟨q, _, hr, by omega, by omega⟩ + · obtain ⟨k, hk, hj, hv⟩ := tcb_slotV P (sub _ _ ht) hi + have e : k = .tcb := Option.some.inj (hk.symm.trans (sub _ _ ht)) + subst e + exact ⟨q, _, ht, by omega, by omega⟩ + +/-- Every slot of `th` is a slot by value. -/ +theorem IsSlotA.v {th : TypedHeapA} {kb : U64} (hP : PlaceA th kb) {y : U64} (h : IsSlotA th y) : + ∃ q j, SlotVA th y q j := by + obtain ⟨p, ⟨r, hr, i, hi, rfl⟩ | ⟨ht, i, hi, rfl⟩⟩ := h + · exact ⟨p, i, slotVA_of hP hr (by rw [cnode_size]; omega)⟩ + · exact ⟨p, i, tcb_slotV hP ht hi⟩ + +/-- (S3 gen 18) **No slot of `th` is at address 0** (`PlaceA.nz`: its object is not, and the slot lies inside it; +gen 16's decode-site hypothesis `hnz`). -/ +theorem PlaceA.slot_ne0 {th : TypedHeapA} {kb : U64} (hP : PlaceA th kb) {y : U64} (h : IsSlotA th y) : + y ≠ 0#u64 := by + obtain ⟨q, j, k, hk, -, hv⟩ := IsSlotA.v hP h + intro e + have h0 : (0#u64 : U64).val = 0 := by simp + rw [e, h0] at hv + exact hP.nz q k hk (UScalar.eq_of_val_eq (by rw [h0]; omega)) + +/-- An abstract slot of `th` is a slot by value: a CNode's at `blvA idx`, a TCB's at its index. -/ +theorem AbsSlotA.v {th : TypedHeapA} {kb : U64} (hP : PlaceA th kb) {sl : CslotPtr} (h : AbsSlotA th sl) : + ∃ p, sl.1 = (specPtr p) ∧ ((th p = some (.cnode sl.2.length) ∧ SlotVA th (cslotAddrA sl) p (blvA sl.2)) ∨ + (th p = some .tcb ∧ ∃ i < tcbSlotsA, sl.2 = tcbCnodeIndex i ∧ SlotVA th (cslotAddrA sl) p i)) := by + obtain ⟨p, e1, hc | ⟨ht, i, hi, e2⟩⟩ := h + · refine ⟨p, e1, Or.inl ⟨hc, ?_⟩⟩ + unfold cslotAddrA; rw [e1]; exact (cn_slotV hP hc rfl).2.2 + · refine ⟨p, e1, Or.inr ⟨ht, i, hi, e2, ?_⟩⟩ + unfold cslotAddrA; rw [e1, e2, ofBl_tcbIdx i (by unfold tcbSlotsA at hi; omega)] + exact tcb_slotV hP ht hi + +/-- **Abstract slots of `th` at one address are equal** (x86 `cslotAddr_inj`). -/ +theorem cslotAddrA_inj {th : TypedHeapA} {kb : U64} (hP : PlaceA th kb) {sl sl' : CslotPtr} (h : AbsSlotA th sl) + (h' : AbsSlotA th sl') (he : cslotAddrA sl = cslotAddrA sl') : sl = sl' := by + obtain ⟨a, idx⟩ := sl + obtain ⟨a', idx'⟩ := sl' + obtain ⟨p, e1, hv⟩ := AbsSlotA.v hP h + obtain ⟨q, e1', hv'⟩ := AbsSlotA.v hP h' + try simp only at e1 e1' hv hv' + rw [he] at hv + rcases hv with ⟨hc, hv⟩ | ⟨ht, i, hi, e2, hv⟩ <;> rcases hv' with ⟨hc', hv'⟩ | ⟨ht', i', hi', e2', hv'⟩ + · obtain ⟨rfl, ej⟩ := hv.inj hP hv' + rw [hc] at hc' + have hl : idx.length = idx'.length := by injection hc' with h1; injection h1 + rw [e1, e1', blvA_inj hl ej] + · obtain ⟨rfl, -⟩ := hv.inj hP hv' + rw [hc] at ht'; cases ht' + · obtain ⟨rfl, -⟩ := hv.inj hP hv' + rw [ht] at hc'; cases hc' + · obtain ⟨rfl, ej⟩ := hv.inj hP hv' + rw [e1, e1', e2, e2', ej] + +/-- An abstract slot's address is a slot of `th`. -/ +theorem AbsSlotA.isSlot {th : TypedHeapA} {kb : U64} (hP : PlaceA th kb) {sl : CslotPtr} (h : AbsSlotA th sl) : + IsSlotA th (cslotAddrA sl) := by + obtain ⟨p, e1, hc | ⟨ht, i, hi, e2⟩⟩ := h + · obtain ⟨e, hb, -⟩ := cn_slotV hP hc rfl + exact ⟨p, Or.inl ⟨_, hc, _, hb, by unfold cslotAddrA; rw [e1, e]⟩⟩ + · refine ⟨p, Or.inr ⟨ht, i, hi, ?_⟩⟩ + unfold cslotAddrA; rw [e1, e2, ofBl_tcbIdx i (by unfold tcbSlotsA at hi; omega)] + +/-- A slot the relation reads is a slot of `th`. -/ +theorem slotW_isSlot {th : TypedHeapA} {kb : U64} (hP : PlaceA th kb) {y : U64} (h : SlotWA th y) : IsSlotA th y := by + rcases h with h | ⟨p, r, hp, idx, hl, rfl⟩ + · exact h + · obtain ⟨e, hb, -⟩ := cn_slotV hP hp hl + exact ⟨p, Or.inl ⟨r, hp, _, hb, e⟩⟩ + +/-! ## The spec's outcomes -/ + +theorem assertOpt_some_memA {α : Type} {o : Option α} {a : α} (h : o = some a) (σ : State Unit) : + (a, σ) ∈ ((NonDet.assertOpt o : NonDet (State Unit) α) σ).1 := by + rw [h]; rfl + +/-- `get_cap` of a CNode slot (x86 `getCap_cnode`). -/ +theorem getCap_cnode_memA {σ : State Unit} {p : ObjRef} {r : Nat} {cs : CnodeContents} {idx : List Bool} {c : Cap} + (hk : σ.kheap p = some (.cnode r cs)) (hwf : wellFormedCnodeN r cs) (hc : cs idx = some c) : + (c, σ) ∈ (getCap (Z := Unit) (p, idx) σ).1 := by + unfold getCap + refine mem_bindA.2 ⟨(.cnode r cs, σ), assertOpt_some_memA hk σ, ?_⟩ + try dsimp only + first + | exact mem_bindA.2 ⟨((), σ), ⟨hwf, rfl⟩, mem_bindA.2 ⟨(cs, σ), rfl, assertOpt_some_memA hc σ⟩⟩ + | exact mem_bindA.2 ⟨(cs, σ), mem_bindA.2 ⟨((), σ), ⟨hwf, rfl⟩, rfl⟩, assertOpt_some_memA hc σ⟩ + +theorem wf_updA {r : Nat} {cs : CnodeContents} {idx : List Bool} (a : Cap) (hwf : wellFormedCnodeN r cs) + (hl : idx.length = r) : wellFormedCnodeN r (Function.update cs idx (some a)) := by + unfold wellFormedCnodeN at hwf ⊢ + rw [dom_updA (by rw [hwf]; exact hl)]; exact hwf + +/-- `set_cap` of a CNode slot (x86 `setCap_cnode`). -/ +theorem setCap_cnode_memA {σ : State Unit} {p : ObjRef} {r : Nat} {cs : CnodeContents} {idx : List Bool} (a : Cap) + (hk : σ.kheap p = some (.cnode r cs)) (hwf : wellFormedCnodeN r cs) (hl : idx.length = r) : + ((), { σ with kheap := Function.update σ.kheap p (some (.cnode r (Function.update cs idx (some a)))) }) ∈ + (setCap (Z := Unit) a (p, idx) σ).1 := by + have hd : idx ∈ dom cs := by rw [hwf]; exact hl + have hty : aType (.cnode r cs) = aType (.cnode r (Function.update cs idx (some a))) := by + simp only [aType, hwf, wf_updA a hwf hl, ↓reduceIte] + unfold setCap + refine mem_bindA.2 ⟨(.cnode r cs, σ), assertOpt_some_memA hk σ, ?_⟩ + simp only [hd, hwf, and_self, ↓reduceIte] + refine mem_bindA.2 ⟨(_, σ), rfl, ?_⟩ + unfold setObject + refine mem_bindA.2 ⟨(.cnode r cs, σ), assertOpt_some_memA hk σ, ?_⟩ + exact mem_bindA.2 ⟨((), σ), ⟨hty, rfl⟩, mem_bindA.2 ⟨(σ, σ), rfl, rfl⟩⟩ + +/-- `do_extended_op` at `Z = Unit` keeps the state (x86 `doExtendedOp_unit`). -/ +theorem doExtendedOp_memA (eop : DetExtMonad Unit) (σ : State Unit) : + ((), σ) ∈ (doExtendedOp (Z := Unit) eop σ).1 := + mem_bindA.2 ⟨(σ, σ), rfl, mem_bindA.2 ⟨(((), σ), σ), ⟨rfl, rfl⟩, rfl⟩⟩ + +/-- The state after `empty_slot`'s CDT and original-flag updates, before its `set_cap` (x86 `emptySlotCdtState`). -/ +def emptySlotCdtStateA (σ : State Unit) (slot : CslotPtr) : State Unit := + { σ with + cdt := Function.update (fun p => if σ.cdt p = some slot then σ.cdt slot else σ.cdt p) slot none, + isOriginalCap := Function.update σ.isOriginalCap slot false } + +/-- **An outcome of `empty_slot slot NullCap`** of a non-null slot (x86 `emptySlot_eq`). -/ +theorem emptySlot_memA {σ σ2 : State Unit} {slot : CslotPtr} {c : Cap} (hg : (c, σ) ∈ (getCap (Z := Unit) slot σ).1) + (hcn : c ≠ .nullCap) (h2 : ((), σ2) ∈ (setCap (Z := Unit) .nullCap slot (emptySlotCdtStateA σ slot)).1) : + ((), σ2) ∈ (emptySlot (Z := Unit) slot .nullCap σ).1 := by + unfold emptySlot + refine mem_bindA.2 ⟨(c, σ), hg, ?_⟩ + dsimp only + rw [if_neg hcn] + refine mem_bindA.2 ⟨(σ.cdt slot, σ), rfl, ?_⟩ + refine mem_bindA.2 ⟨(σ.cdt, σ), rfl, ?_⟩ + refine mem_bindA.2 ⟨(σ.cdt slot, σ), rfl, ?_⟩ + refine mem_bindA.2 ⟨((), _), mem_bindA.2 ⟨(σ, σ), rfl, rfl⟩, ?_⟩ + refine mem_bindA.2 ⟨((), _), doExtendedOp_memA _ _, ?_⟩ + refine mem_bindA.2 ⟨((), emptySlotCdtStateA σ slot), mem_bindA.2 ⟨(_, _), rfl, rfl⟩, ?_⟩ + exact mem_bindA.2 ⟨((), σ2), h2, rfl⟩ + +/-! ## The relations after a CNode slot's update -/ + +theorem CapRelA.ne_null {c : capability.Cap} {a : Cap} (h : CapRelA c a) (hc : c ≠ .Null) : a ≠ .nullCap := by + obtain ⟨hw, heq⟩ := h + cases c with + | PageTable p m asid va => + cases m + · rw [← heq]; simp [absCapA] + · obtain ⟨_, _, rfl, _, _⟩ := CapEqA.pt_mapped_iff.mp heq + simp + | _ => + rw [← heq] + simp_all [absCapA, CapWfA] + +theorem bv_ne {p q : U64} (h : p ≠ q) : specPtr p ≠ specPtr q := fun e => h (specPtr_inj e) + +/-- (gen 5) **A CNode's contents replaced is a slot-only spec step** (`SpecSlotA`). -/ +theorem SpecSlotA.setCn {σ σ' : State Unit} {p : ObjRef} {r : Nat} {cs cs' : CnodeContents} + (hk : σ.kheap p = some (.cnode r cs)) (hk' : σ'.kheap = Function.update σ.kheap p (some (.cnode r cs'))) + (hg : SpecGlobA σ σ') : SpecSlotA σ σ' := by + refine ⟨hg, fun q => ?_⟩ + rw [hk'] + by_cases e : q = p + · subst e; exact Or.inr (Or.inl ⟨r, cs, cs', hk, by simp⟩) + · exact Or.inl (Function.update_of_ne e _ _) + +/-- **`StateRelA` after a CNode slot gets a related cap** and every other slot of `th` keeps its cap (x86 +`StateRel.update_cnode_slot`). -/ +theorem StateRelA.setCn {th : TypedHeapA} {kb : U64} {mem mem' : U64 → U64} {σ σ' : State Unit} + (hR : StateRelA th mem σ) (hP : PlaceA th kb) {p : U64} {r : Nat} (hp : th p = some (.cnode r)) + {cs : CnodeContents} (hk : σ.kheap (specPtr p) = some (.cnode r cs)) {idx : List Bool} (hl : idx.length = r) {a : Cap} + (hca : CapRelA (capAtA mem' (cslotAddrA ((specPtr p), idx))) a) + (hcap : ∀ y, IsSlotA th y → y ≠ cslotAddrA ((specPtr p), idx) → capAtA mem' y = capAtA mem y) + (hk' : σ'.kheap = Function.update σ.kheap (specPtr p) (some (.cnode r (Function.update cs idx (some a))))) : + StateRelA th mem' σ' := by + have hx := (cn_slotV hP hp hl).2.2 + have ne : ∀ y q j, SlotVA th y q j → ¬ (q = p ∧ j = blvA idx) → y ≠ cslotAddrA ((specPtr p), idx) := by + rintro y q j hy hn rfl + exact hn (hy.inj hP hx) + refine ⟨fun q r' hq => ?_, fun t ht => ?_⟩ + · obtain ⟨cs', hk1, hwf, hs⟩ := hR.cnode q r' hq + by_cases e : q = p + · subst e + rw [hp] at hq + obtain rfl : r = r' := by injection hq with h1; injection h1 + rw [hk] at hk1 + obtain rfl : cs = cs' := by injection hk1 with h1; injection h1 + refine ⟨Function.update cs idx (some a), by rw [hk']; simp, wf_updA a hwf hl, fun idx' hl' => ?_⟩ + by_cases ei : idx' = idx + · subst ei; exact ⟨a, by simp, hca⟩ + · rw [Function.update_of_ne ei] + obtain ⟨-, -, hv⟩ := cn_slotV hP hp hl' + have hy := ne _ _ _ hv (fun ⟨_, h2⟩ => ei (blvA_inj (by rw [hl, hl']) h2)) + unfold SlotRelA; rw [hcap _ (slotW_isSlot hP (Or.inr ⟨q, _, hp, idx', hl', rfl⟩)) hy] + exact hs idx' hl' + · refine ⟨cs', by rw [hk', Function.update_of_ne (bv_ne e)]; exact hk1, hwf, fun idx' hl' => ?_⟩ + obtain ⟨-, -, hv⟩ := cn_slotV hP hq hl' + have hy := ne _ _ _ hv (fun ⟨h1, _⟩ => e h1) + unfold SlotRelA; rw [hcap _ (slotW_isSlot hP (Or.inr ⟨q, _, hq, idx', hl', rfl⟩)) hy] + exact hs idx' hl' + · obtain ⟨x, hk1, hs⟩ := hR.tcb t ht + have e : t ≠ p := by rintro rfl; rw [hp] at ht; cases ht + refine ⟨x, by rw [hk', Function.update_of_ne (bv_ne e)]; exact hk1, fun i hi => ?_⟩ + have hy := ne _ _ _ (tcb_slotV hP ht hi) (fun ⟨h1, _⟩ => e h1) + unfold SlotRelA; rw [hcap _ ⟨t, Or.inr ⟨ht, i, hi, rfl⟩⟩ hy] + exact hs i hi + +/-! ## TCB slots (gen 8, ruling 12:48Z item 1) -/ + +/-- The spec state with the object at `p` replaced (`set_object`'s outcome; `MoveA.setCnA` is its CNode instance). -/ +def setObjA (σ : State Unit) (p : ObjRef) (o : KernelObject) : State Unit := + { σ with kheap := Function.update σ.kheap p (some o) } + +/-- The TCB with cap slot `i` (`tcb_cnode_index i`, the `ext` hook's slots 5 and 6 included) set to `a`: `set_cap`'s +TCB branch (x86 `DelAbs.tcbSet`). -/ +def tcbSetA (x : Tcb) (i : Nat) (a : Cap) : Tcb := + if i = 0 then { x with tcbCtable := a } + else if i = 1 then { x with tcbVtable := a } + else if i = 2 then { x with tcbIpcframe := a } + else if i = 3 then { x with tcbFaultHandler := a } + else if i = 4 then { x with tcbTimeoutHandler := a } + else if i = 5 then { x with tcbExt := { x.tcbExt with tcbStage2 := a } } + else { x with tcbExt := { x.tcbExt with tcbHighVSpace := a } } + +/-- Writing a TCB cap slot keeps the non-cap fields. -/ +theorem tcbNcA_setA (x : Tcb) (i : Nat) (a : Cap) : tcbNcA (tcbSetA x i a) = tcbNcA x := by + unfold tcbSetA; split_ifs <;> rfl + +/-- **The TCB's cap map after a slot write** (x86 `tcbCnodeMap_set`). -/ +theorem tcbMap_setA (x : Tcb) {i : Nat} (hi : i < tcbSlotsA) (a : Cap) : + tcbCnodeMap (tcbSetA x i a) = Function.update (tcbCnodeMap x) (tcbCnodeIndex i) (some a) := by + funext b + unfold tcbSlotsA at hi + by_cases hb : b = tcbCnodeIndex i + · subst hb + rw [Function.update_self] + interval_cases i <;> simp [tcbSetA, tcbCnodeMap, tcbIdx_eq, tcbStage2Slot, tcbHighVSpaceSlot] + · rw [Function.update_of_ne hb] + interval_cases i <;> simp [tcbSetA, tcbCnodeMap, Function.update_apply, hb, tcbStage2Slot, tcbHighVSpaceSlot] + +/-- `get_cap` of a TCB slot (x86 `getCap_tcb_*`). -/ +theorem getCap_tcb_memA {σ : State Unit} {t : ObjRef} {x : Tcb} {idx : List Bool} {c : Cap} + (hk : σ.kheap t = some (.tcb x)) (hc : tcbCnodeMap x idx = some c) : + (c, σ) ∈ (getCap (Z := Unit) (t, idx) σ).1 := by + unfold getCap + refine mem_bindA.2 ⟨(.tcb x, σ), assertOpt_some_memA hk σ, ?_⟩ + try dsimp only + first + | exact mem_bindA.2 ⟨(tcbCnodeMap x, σ), rfl, assertOpt_some_memA hc σ⟩ + | exact assertOpt_some_memA hc σ + +/-- **`set_cap` of a TCB slot** (x86 `setCap_tcb`, as a membership). -/ +theorem setCap_tcb_memA {σ : State Unit} {t : ObjRef} {x : Tcb} {i : Nat} (a : Cap) + (hk : σ.kheap t = some (.tcb x)) (hi : i < tcbSlotsA) : + ((), setObjA σ t (.tcb (tcbSetA x i a))) ∈ (setCap (Z := Unit) a (t, tcbCnodeIndex i) σ).1 := by + have hset : ∀ x' : Tcb, ((), setObjA σ t (.tcb x')) ∈ (setObject (Z := Unit) t (.tcb x') σ).1 := fun x' => by + unfold setObject + refine mem_bindA.2 ⟨(.tcb x, σ), assertOpt_some_memA hk σ, ?_⟩ + exact mem_bindA.2 ⟨((), σ), ⟨rfl, rfl⟩, mem_bindA.2 ⟨(σ, σ), rfl, rfl⟩⟩ + unfold tcbSlotsA at hi + have e : ∀ j, j < 8 → (tcbCnodeIndex i = tcbCnodeIndex j ↔ i = j) := fun j hj => tcbIdx_eq (by omega) hj + unfold setCap + refine mem_bindA.2 ⟨(.tcb x, σ), assertOpt_some_memA hk σ, ?_⟩ + simp only [tcbStage2Slot, tcbHighVSpaceSlot, e 0 (by decide), e 1 (by decide), e 2 (by decide), e 3 (by decide), + e 4 (by decide), e 5 (by decide), e 6 (by decide)] + unfold tcbSetA + split_ifs <;> first | omega | exact mem_bindA.2 ⟨(_, σ), rfl, hset _⟩ + +/-- (gen 8) A TCB's cap slot replaced is a slot-only spec step. -/ +theorem SpecSlotA.setTcbA {σ : State Unit} {t : ObjRef} {x : Tcb} (hk : σ.kheap t = some (.tcb x)) (i : Nat) + (a : Cap) : SpecSlotA σ (setObjA σ t (.tcb (tcbSetA x i a))) := by + refine ⟨⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, ⟨rfl, rfl, rfl, rfl⟩⟩, fun q => ?_⟩ + by_cases e : q = t + · subst e; exact Or.inr (Or.inr ⟨x, tcbSetA x i a, hk, by simp [setObjA], tcbNcA_setA x i a⟩) + · exact Or.inl (by simp [setObjA, Function.update_of_ne e]) + +/-- **`StateRelA` after a TCB slot gets a related cap** and every other slot of `th` keeps its cap (x86 +`StateRel.update_tcb_slot`). -/ +theorem StateRelA.setTcbA {th : TypedHeapA} {kb : U64} {mem mem' : U64 → U64} {σ σ' : State Unit} + (hR : StateRelA th mem σ) (hP : PlaceA th kb) {t : U64} (ht : th t = some .tcb) {x : Tcb} + (hk : σ.kheap (specPtr t) = some (.tcb x)) {i : Nat} (hi : i < tcbSlotsA) {a : Cap} + (hca : CapRelA (capAtA mem' (cslotAddrA ((specPtr t), tcbCnodeIndex i))) a) + (hcap : ∀ y, IsSlotA th y → y ≠ cslotAddrA ((specPtr t), tcbCnodeIndex i) → capAtA mem' y = capAtA mem y) + (hk' : σ'.kheap = Function.update σ.kheap (specPtr t) (some (.tcb (tcbSetA x i a)))) : + StateRelA th mem' σ' := by + have hi8 : i < 8 := by unfold tcbSlotsA at hi; omega + have ea : cslotAddrA ((specPtr t), tcbCnodeIndex i) = slotAddrA (specPtr t) (BitVec.ofNat 64 i) := by + unfold cslotAddrA; rw [ofBl_tcbIdx i hi8] + rw [ea] at hca hcap + have hx := tcb_slotV hP ht hi + have ne : ∀ y q j, SlotVA th y q j → ¬ (q = t ∧ j = i) → y ≠ slotAddrA (specPtr t) (BitVec.ofNat 64 i) := by + rintro y q j hy hn rfl + exact hn (hy.inj hP hx) + refine ⟨fun q r' hq => ?_, fun q hq => ?_⟩ + · obtain ⟨cs', hk1, hwf, hs⟩ := hR.cnode q r' hq + have e : q ≠ t := by rintro rfl; rw [ht] at hq; cases hq + refine ⟨cs', by rw [hk', Function.update_of_ne (bv_ne e)]; exact hk1, hwf, fun idx' hl' => ?_⟩ + obtain ⟨-, -, hv⟩ := cn_slotV hP hq hl' + have hy := ne _ _ _ hv (fun ⟨h1, _⟩ => e h1) + unfold SlotRelA; rw [hcap _ (slotW_isSlot hP (Or.inr ⟨q, _, hq, idx', hl', rfl⟩)) hy] + exact hs idx' hl' + · obtain ⟨x', hk1, hs⟩ := hR.tcb q hq + by_cases e : q = t + · subst e + rw [hk] at hk1 + obtain rfl : x = x' := by injection hk1 with h1; injection h1 + refine ⟨tcbSetA x i a, by rw [hk']; simp, fun j hj => ?_⟩ + rw [tcbMap_setA x hi a] + by_cases ej : j = i + · subst ej; rw [Function.update_self]; exact ⟨a, rfl, hca⟩ + · have hne : tcbCnodeIndex j ≠ tcbCnodeIndex i := + fun h => ej ((tcbIdx_eq (by unfold tcbSlotsA at hj; omega) hi8).1 h) + rw [Function.update_of_ne hne] + have hy := ne _ _ _ (tcb_slotV hP hq hj) (fun ⟨_, h2⟩ => ej h2) + unfold SlotRelA; rw [hcap _ ⟨q, Or.inr ⟨hq, j, hj, rfl⟩⟩ hy] + exact hs j hj + · refine ⟨x', by rw [hk', Function.update_of_ne (bv_ne e)]; exact hk1, fun j hj => ?_⟩ + have hy := ne _ _ _ (tcb_slotV hP hq hj) (fun ⟨h1, _⟩ => e h1) + unfold SlotRelA; rw [hcap _ ⟨q, Or.inr ⟨hq, j, hj, rfl⟩⟩ hy] + exact hs j hj + +theorem StateRelA.of_kheap {th : TypedHeapA} {mem : U64 → U64} {σ σ' : State Unit} (h : StateRelA th mem σ) + (hk : σ'.kheap = σ.kheap) : StateRelA th mem σ' where + cnode p r hp := by rw [hk]; exact h.cnode p r hp + tcb t ht := by rw [hk]; exact h.tcb t ht + +/-- **`get_cap` of any slot of the typed heap** (gen 8): an outcome related to the slot's FL4 cap. -/ +theorem StateRelA.getAbsA {th : TypedHeapA} {mem : U64 → U64} {σ : State Unit} (hR : StateRelA th mem σ) + {sl : CslotPtr} (hs : AbsSlotA th sl) : + ∃ c, (c, σ) ∈ (getCap (Z := Unit) sl σ).1 ∧ CapRelA (capAtA mem (cslotAddrA sl)) c := by + obtain ⟨o, idx⟩ := sl + obtain ⟨p, e1, hc | ⟨ht, i, hi, e2⟩⟩ := hs + · simp only at e1 hc; subst e1 + obtain ⟨cs, hk, hwf, hr⟩ := hR.cnode p _ hc + obtain ⟨c, hc', hrel⟩ := hr idx rfl + exact ⟨c, getCap_cnode_memA hk hwf hc', hrel⟩ + · simp only at e1 e2; subst e1 e2 + obtain ⟨x, hk, hr⟩ := hR.tcb p ht + obtain ⟨c, hc', hrel⟩ := hr i hi + refine ⟨c, getCap_tcb_memA hk hc', ?_⟩ + unfold cslotAddrA; rw [ofBl_tcbIdx i (by unfold tcbSlotsA at hi; omega)]; exact hrel + +/-- **`set_cap` of any slot of the typed heap with a related cap** (gen 8, x86 `StateRel.update_abs_slot`): an +outcome that replaces the slot's object only, with the CSpace relation after it and a slot-only spec step. -/ +theorem StateRelA.setAbsA {th : TypedHeapA} {kb : U64} {mem mem' : U64 → U64} {σ : State Unit} + (hR : StateRelA th mem σ) (hP : PlaceA th kb) {sl : CslotPtr} (hs : AbsSlotA th sl) {a : Cap} + (hca : CapRelA (capAtA mem' (cslotAddrA sl)) a) + (hcap : ∀ y, IsSlotA th y → y ≠ cslotAddrA sl → capAtA mem' y = capAtA mem y) : + ∃ o, ((), setObjA σ sl.1 o) ∈ (setCap (Z := Unit) a sl σ).1 ∧ StateRelA th mem' (setObjA σ sl.1 o) ∧ + SpecSlotA σ (setObjA σ sl.1 o) := by + obtain ⟨o, idx⟩ := sl + obtain ⟨p, e1, hc | ⟨ht, i, hi, e2⟩⟩ := hs + · simp only at e1 hc; subst e1 + obtain ⟨cs, hk, hwf, -⟩ := hR.cnode p _ hc + exact ⟨.cnode idx.length (Function.update cs idx (some a)), setCap_cnode_memA a hk hwf rfl, StateRelA.setCn hR hP hc hk rfl hca hcap rfl, + SpecSlotA.setCn hk rfl ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, ⟨rfl, rfl, rfl, rfl⟩⟩⟩ + · simp only at e1 e2; subst e1 e2 + obtain ⟨x, hk, -⟩ := hR.tcb p ht + exact ⟨.tcb (tcbSetA x i a), setCap_tcb_memA a hk hi, StateRelA.setTcbA hR hP ht hk hi hca hcap rfl, SpecSlotA.setTcbA hk i a⟩ + +/-- A spec step that keeps the heap and the globals is slot-only. -/ +theorem SpecSlotA.ofKh {σ σ' : State Unit} (hk : σ'.kheap = σ.kheap) (hg : SpecGlobA σ σ') : SpecSlotA σ σ' := + ⟨hg, fun p => Or.inl (by rw [hk])⟩ + +section cdt +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- (gen 7) No slot is its own `cdt` parent (the ghost tree's `Mdb.not_desc_self`). -/ +theorem CdtRelA.noSelf {th : TypedHeapA} {s : S} {σ : State Unit} {G : MdbGhostA} (hC : CdtRelA st th s σ G) + {sl : CslotPtr} (hsl : AbsSlotA th sl) : σ.cdt sl ≠ some sl := by + intro h + have e := hC.cdt sl hsl + rw [h] at e + have hp : G.par (cslotAddrA sl) = some (cslotAddrA sl) := by rw [← e]; rfl + have T := hC.tree.tree + obtain ⟨ch, hch, hx⟩ := List.mem_flatten.1 (T.par_mem _ _ hp).1 + exact Fl4Proofs.P3.Mdb.not_desc_self (T.seg ch hch) ((List.nodup_flatten.1 T.nodup).1 ch hch) hx + (Relation.TransGen.single hp) + +/-- **The spec's CDT after `empty_slot`** against the ghost tree with the slot removed (x86 `CdtRel.remove`). -/ +theorem CdtRelA.removeA {th : TypedHeapA} {kb : U64} (hP : PlaceA th kb) {s s' : S} {σ σ' : State Unit} + {G : MdbGhostA} (hC : CdtRelA st th s σ G) {chains' : List (List U64)} {xs : CslotPtr} (hxs : AbsSlotA th xs) + (htr : TreeA st {a | IsSlotA th a} chains' (Fl4Proofs.P3.Mdb.removePar (cslotAddrA xs) G.par) s') + (hcdt : σ'.cdt = Function.update (fun p => if σ.cdt p = some xs then σ.cdt xs else σ.cdt p) xs none) + (hio : σ'.isOriginalCap = Function.update σ.isOriginalCap xs false) + (hrev : ∀ sl, AbsSlotA th sl → + revA (L.mem s') (cslotAddrA sl) = if sl = xs then false else revA (L.mem s) (cslotAddrA sl)) : + CdtRelA st th s' σ' ⟨chains', Fl4Proofs.P3.Mdb.removePar (cslotAddrA xs) G.par⟩ := by + have hiff : ∀ sl, AbsSlotA th sl → (σ.cdt sl = some xs ↔ G.par (cslotAddrA sl) = some (cslotAddrA xs)) := by + intro sl hsl + rw [← hC.cdt sl hsl] + constructor + · intro h; rw [h]; rfl + · intro h + obtain ⟨q, hq, hqa⟩ := Option.map_eq_some_iff.1 h + rw [hq, cslotAddrA_inj hP (hC.cdt_slot sl q hq).2 hxs hqa] + refine CdtRelA.mkA htr (fun sl hsl => ?_) (fun sl q hq => ?_) (fun sl hsl => ?_) (fun a b hab => ?_) + (fun sl q hq => ?_) + · show Option.map cslotAddrA (σ'.cdt sl) = Fl4Proofs.P3.Mdb.removePar _ G.par (cslotAddrA sl) + rw [hcdt] + by_cases h1 : sl = xs + · rw [h1]; simp [Fl4Proofs.P3.Mdb.removePar] + · have hne : cslotAddrA sl ≠ cslotAddrA xs := fun e => h1 (cslotAddrA_inj hP hsl hxs e) + simp only [Function.update_of_ne h1, Fl4Proofs.P3.Mdb.removePar, hne, ↓reduceIte] + by_cases h2 : σ.cdt sl = some xs + · rw [if_pos h2, if_pos ((hiff sl hsl).1 h2), hC.cdt xs hxs] + · rw [if_neg h2, if_neg (fun h => h2 ((hiff sl hsl).2 h)), hC.cdt sl hsl] + · rw [hcdt] at hq + by_cases h1 : sl = xs + · rw [h1] at hq; simp at hq + · rw [Function.update_of_ne h1] at hq + by_cases h2 : σ.cdt sl = some xs + · rw [if_pos h2] at hq + exact ⟨(hC.cdt_slot _ _ h2).1, (hC.cdt_slot _ _ hq).2⟩ + · rw [if_neg h2] at hq; exact hC.cdt_slot _ _ hq + · rw [hio, hrev _ hsl] + by_cases h1 : sl = xs + · rw [h1]; simp + · rw [Function.update_of_ne h1, if_neg h1]; exact hC.orig sl hsl + · change Fl4Proofs.P3.Mdb.removePar _ G.par a = some b at hab + unfold Fl4Proofs.P3.Mdb.removePar at hab + by_cases hax : a = cslotAddrA xs + · simp [hax] at hab + · rw [if_neg hax] at hab + by_cases hp : G.par a = some (cslotAddrA xs) + · exact hC.par_slot a _ hp + · rw [if_neg hp] at hab; exact hC.par_slot a b hab + · -- (gen 7) every parent is original: `xs`'s children take `xs`'s parent, which is not `xs` + rw [hcdt] at hq + rw [hio] + by_cases h1 : sl = xs + · rw [h1] at hq; simp at hq + · rw [Function.update_of_ne h1] at hq + by_cases h2 : σ.cdt sl = some xs + · rw [if_pos h2] at hq + have hqx : q ≠ xs := by rintro rfl; exact hC.noSelf hxs hq + rw [Function.update_of_ne hqx]; exact hC.po _ _ hq + · rw [if_neg h2] at hq + have hqx : q ≠ xs := fun e => h2 (e ▸ hq) + rw [Function.update_of_ne hqx]; exact hC.po _ _ hq + +end cdt + +/-! ## FL4's `empty_slot` with a null cleanup -/ + +section fl4 +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- A `Null` cleanup keeps the arch state. -/ +theorem emptySlot_arNullA {A : Type} (ar : machine.Arch A) (s : S) (a : A) (x : U64) : + cdt.empty_slot st ar s a x .Null ⦃ r => r.2 = a ⦄ := by + have hS : P0.StoreTotal st := L.total + unfold cdt.empty_slot + rw [capA_eq, bind_ok] + by_cases hn : capA st s x = .Null + · rw [hn, is_null_t, bind_ok, if_pos rfl]; exact (WP.spec_ok _).2 rfl + rw [is_null_f hn, bind_ok, if_neg (by simp), slot_prev_eq, bind_ok, next_eq, bind_ok] + refine WP.spec_bind (P0.obj.Slot.first_badged.p0 st x s hS) fun fb _ => ?_ + simp only [obj.Slot.is_null, bind_ok] + refine WP.spec_bind (Pₘ := fun _ => True) ?_ fun s1 _ => ?_ + · split + · exact (WP.spec_ok _).2 trivial + · exact P0.obj.Slot.set_next.p0 st _ _ _ hS + refine WP.spec_bind (Pₘ := fun _ => True) ?_ fun s2 _ => ?_ + · split + · exact (WP.spec_ok _).2 trivial + · refine WP.spec_bind (P0.obj.Slot.set_prev.p0 st _ _ _ hS) fun s3 _ => ?_ + refine WP.spec_bind (P0.obj.Slot.first_badged.p0 st _ _ hS) fun nfb _ => ?_ + split <;> exact P0.obj.Slot.set_first_badged.p0 st _ _ _ hS + refine WP.spec_bind (P0.obj.Slot.clear.p0 st x s2 hS) fun s3 _ => ?_ + rw [pcd_null]; exact (WP.spec_ok _).2 rfl + +/-- **`empty_slot` of a non-null `OS` slot with a `Null` cleanup, the ghosts named** (A64 P1's `cdt.empty_slot.tree` +with `remG x G` / `Mdb.removePar x par`, `.mdbFLG`'s caps, `.rvb`'s bit 48, and the slot's `NEXT` word zero). -/ +theorem emptySlot_treeGA {A : Type} {ar : machine.Arch A} {T Ω Sg OS : Set U64} (hO : OSOK T Ω Sg OS) {s : S} + {G : List (List U64)} {par : U64 → Option U64} (hT : TreeA st OS G par s) (hRO : RngOneA64 st G s) + (hK : KindA st OS s) (a : A) {x : U64} (hx : x ∈ OS) (hn : capA st s x ≠ .Null) : + cdt.empty_slot st ar s a x .Null ⦃ r => TreeA st OS (remG x G) (Fl4Proofs.P3.Mdb.removePar x par) r.1 ∧ + RngOneA64 st (remG x G) r.1 ∧ KindA st OS r.1 ∧ (∀ y ∈ OS, y ≠ x → capA st r.1 y = capA st s y) ∧ + capA st r.1 x = .Null ∧ L.mem r.1 (wa x 24#u64) = 0#u64 ∧ + (∀ y ∈ OS, y ≠ x → L.mem r.1 (wa y 24#u64) &&& RVB = L.mem s (wa y 24#u64) &&& RVB) ∧ r.2 = a ⦄ := by + have hpcd : ∀ (s1 : S) (a1 : A), L.ksb s1 = L.ksb s → + objects.post_cap_deletion st ar s1 a1 .Null ⦃ r => WritesIn st s1 r.1 (fun _ => False) ⦄ := + fun s1 a1 _ => by rw [pcd_null]; exact (WP.spec_ok _).2 (WritesIn.refl _ _) + have inOS : ∀ y ∈ G.flatten, y ∈ OS := fun y hy => ((hT.lk.members y).1 hy).1 + have F := hT.lk.lkF hO.nz + have hxG : x ∈ G.flatten := (hT.lk.members x).2 ⟨hx, hn⟩ + refine WP.spec_mono (WP.spec_and (WP.spec_and + (W7.R.ST.cdt.empty_slot.mdbFLG hO hO.nz hT.lk hRO hK hx (fun _ e => e.elim) hpcd) + (W7.R.ST.cdt.empty_slot.rvb hO hx (fun _ e => e.elim) hpcd (fun _ h => inOS _ (F.pvm _ hxG h)) + (fun _ h => inOS _ (F.nxm _ hxG h)))) (emptySlot_arNullA ar s a x)) ?_ + rintro r ⟨⟨⟨⟨G', -, hG2, hM', hRO', hK', hcr, -, hx0, hsk⟩, -, -⟩, hrv⟩, har⟩ + obtain rfl := hG2 hn + refine ⟨⟨hM', by rw [remG_eq]; exact hT.tree.remove (hT.noself x), hT.ut.rem hT.parIn (hT.noself x) hx0 hcr⟩, + hRO', hK', hcr, hx0, ?_, hrv, har⟩ + rcases hsk with hs | ⟨-, h24⟩ + · exact absurd (show capA st s x = .Null by rw [← hx0]; unfold capA; rw [hs.1, hs.2.1]) hn + · exact h24 + +end fl4 + +/-! ## The joint leaf -/ + +/-- The ghost after emptying the slot at `x` (x86 `remG`): `x` erased from the chains (A64 P1's `remG`), its +children given its parent. -/ +def remGA (G : MdbGhostA) (x : U64) : MdbGhostA := ⟨remG x G.chains, Fl4Proofs.P3.Mdb.removePar x G.par⟩ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **FL4's `cdt::empty_slot` refines l4v `empty_slot`** at a non-null slot of the typed heap (gen 8: a CNode's or a +TCB's, `AbsSlotA`) with no cleanup (module docstring; x86 `empty_slot_refines`). -/ +theorem empty_slot_refinesA {A : Type} {ar : machine.Arch A} {T Ω Sg OS : Set U64} {th : TypedHeapA} {kb : U64} + (s : S) (a : A) (σ : State Unit) (G : MdbGhostA) (hR : StateRelA th (L.mem s) σ) (hC : CdtRelA st th s σ G) + (hP : PlaceA th kb) (hO : OSOK T Ω Sg OS) (hT : TreeA st OS G.chains G.par s) (hRO : RngOneA64 st G.chains s) + (hK : KindA st OS s) (hsub : ∀ y, IsSlotA th y → y ∈ OS) + (hnul : ∀ y ∈ OS, ¬ IsSlotA th y → capA st s y = .Null) + {xs : CslotPtr} (hxs : AbsSlotA th xs) (hcn : capAtA (L.mem s) (cslotAddrA xs) ≠ .Null) : + cdt.empty_slot st ar s a (cslotAddrA xs) .Null ⦃ q => q.2 = a ∧ + ∃ σ', ((), σ') ∈ (emptySlot (Z := Unit) xs .nullCap σ).1 ∧ StateRelA th (L.mem q.1) σ' ∧ + CdtRelA st th q.1 σ' (remGA G (cslotAddrA xs)) ∧ + TreeA st OS (remGA G (cslotAddrA xs)).chains (remGA G (cslotAddrA xs)).par q.1 ∧ + RngOneA64 st (remGA G (cslotAddrA xs)).chains q.1 ∧ KindA st OS q.1 ∧ SpecSlotA σ σ' ∧ + ∀ y, IsSlotA th y → capAtA (L.mem q.1) y = .Null ∨ capAtA (L.mem q.1) y = capAtA (L.mem s) y ⦄ := by + have hxS : IsSlotA th (cslotAddrA xs) := hxs.isSlot hP + obtain ⟨ac, hg, hca⟩ := hR.getAbsA hxs + have hacn : ac ≠ .nullCap := hca.ne_null hcn + refine WP.spec_mono (emptySlot_treeGA hO hT hRO hK a (hsub _ hxS) hcn) ?_ + rintro q ⟨hT', hRO', hK', hcr, hx0, hx24, hrv, har⟩ + have hcap : ∀ y, IsSlotA th y → y ≠ cslotAddrA xs → capAtA (L.mem q.1) y = capAtA (L.mem s) y := + fun y hy hne => hcr y (hsub y hy) hne + have hnull : CapRelA (capAtA (L.mem q.1) (cslotAddrA xs)) .nullCap := by + show CapRelA (capA st q.1 _) _ + rw [hx0]; exact CapRelA.null + obtain ⟨o, hm, hR', hS'⟩ := StateRelA.setAbsA (σ := emptySlotCdtStateA σ xs) (hR.of_kheap rfl) hP hxs hnull hcap + refine ⟨har, _, emptySlot_memA hg hacn hm, hR', CdtRelA.removeA hP hC hxs ?_ rfl rfl ?_, hT', hRO', hK', + (SpecSlotA.ofKh (σ := σ) (σ' := emptySlotCdtStateA σ xs) rfl + ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, ⟨rfl, rfl, rfl, rfl⟩⟩).trans hS', fun y hy => ?_⟩ + · refine hT'.sub (fun y hy => hsub y hy) fun y hy hn => ?_ + rw [hcr y hy (fun e => hn (e ▸ hxS))]; exact hnul y hy hn + · intro sl hsl + by_cases e : sl = xs + · subst e; rw [if_pos rfl]; unfold revA; rw [hx24]; rfl + · rw [if_neg e] + have hne : cslotAddrA sl ≠ cslotAddrA xs := fun h => e (cslotAddrA_inj hP hsl hxs h) + have h1 := hrv _ (hsub _ (hsl.isSlot hP)) hne + simp only [RVB] at h1 + unfold revA; rw [h1] + · by_cases e : y = cslotAddrA xs + · exact Or.inl (by rw [e]; exact hx0) + · exact Or.inr (hcap y hy e) + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/EntryA.lean b/proofs/Fl4ProofsA64/P3/EntryA.lean new file mode 100644 index 000000000..1e6ff0fb0 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/EntryA.lean @@ -0,0 +1,405 @@ +/- +P3 (aarch64, A64-P3 S3 gen 2, S4 step 1): **the entry-level combination `SysInvA ∧ JAtZAX`** (Architect's ruling +08:46Z item 5) and the bridges from A64 P1's facts to the `JAtZA` clauses that mirror them. + +* **`SysInvA st H Cs OS U Ac Pp Tb Lv ia ie s`**: A64 P1's invariants at an entry state, unchanged: `ent` (S1b's + `EntryInvA`, which holds `BXI` as `ent.bxi`, `HeapOK`, `OSOK`, `UserOK`, `VsTy`, `ksw`) and `sysl` (A64 P1's + syscall-level `SysL`: `SysZ` with the MDB tree, `LiveNamed`, `ValidReplies`, `A64Layout`, …). +* **`JEntA AR Z H OS … s a σ m`** (x86 `JAtZB` with its `zH` / `P1Inv` beside it): `sys : SysInvA` at the + relation's cores `Z.Cs`, `j : JAtZAX`, `tcbs : H.tcbs = Z.T` and (gen 3, ruling 09:59Z item 1) `scs : H.scs = Z.C` + (the P1 heap's TCBs and SCs are the relation's; `JEntA.scAl`: the SCs are aligned), `sq` (gen 3, the queue + ghosts) and **`ap`** (gen 7, ruling 12:26Z item 1: **`SlotApA`**, the slot apartness facts, folded in as a field: + `irqSt` and the VCPU facts do not follow from `SysInvA` at a general state — `OSOK` / `HeapOK` do not place `OS` + slots off the kernel-state block or off `Z.V`, and S1b's `PlaceA` is not part of the entry relation). + +Bridges (each a `JAtZA` / `FrameA` clause from the P1 fact that mirrors it; x86: `JAtZB.zH`, `JKern.*` from `P1Inv`): +* `bxi_cpuCs`, `bxi_curT`, `bxi_idleT` — `SchedRelA.cpuCs`, `JAtZA.curT` / `idleT` at the current core, from + `SchedQueuesInv` (`cur`, `curT`, `idleT`); +* `bxi_nodes` — the nodes' words aligned (`FootA.aligned`'s `hN`), from `SchedQueuesInv.nodes` (`NodePlaced`); +* `ent_ksFit` — `JAtZA.ksFit`, from `EntryInvA.ksw`; +* `sysl_tree` — A64 P1's MDB tree at `OS` (`SysL.sys.stz`'s `MdbST`), and **`cdtTree_of`** — `CdtRelA.tree` (A64 P1's + `TreeA` at the slots of `th`, ruling item 4) from it by `TreeA.sub`, when `th`'s slots are in `OS` and `OS`'s other + slots are null (the IRQ node's slots, until the IRQ relation names them: S4); +* `place_of` — `PlaceA` from `HeapOK` (alignment, fit, apartness) and S1b's `PlaceA.ks` / an apartness, when every + object of `th` but an IRQ-node slot is an object of `H` of the same size (gen 3: the IRQ-node slots placed from + the block's alignment and fit); +* `JEntA.writesIn` — `JAtZAX` across a P1 step writing off the footprint (`JAtZAX.of_writesIn`), the nodes' alignment + from `BXI`, the SCs' from `JEntA.scs` (gen 3). + +(gen 13, ruling 15:58Z item 1) **`JEntA` gains `pp` (`Z.Pp = Pp`), `s1` (`Z.S1 ⊆ Tb`), `ko` (A64 P1's `HeapKsOff`) +and `ptH` (`PtHA H (Z.Pp ∪ Z.S1)`: every pool / Stage-1 table page inside an `other` object of `H`, the generalised +`PtH` of A64-S1A)**, so the VS leaves' placement hypotheses discharge from `JEntA`: **`JEntA.vsLeafA`** gives `hK` +(`Aligned ksb`, from `JAtZA.ksbAl`) and `hT` / `hA` (no Stage-1 table or pool entry is an ASID-table word, +`ptHA_not_asW`). `JAtZA` / `JAtZAX` unchanged. + +No named premise. New definitions and lemmas only. + +(Architect 21:51Z D1, J1) **`JEntA.objNamed : ObjNamedA` is replaced by `objAlloc : ObjAllocA` (the watermark form: +every joint object is off each `OS` untyped or inside its used prefix) and `idleAnchor : IdleAnchorOffA`**; +`ObjNamedA` and its lemmas are removed. `DeadDrop4KA` ([J], the dead-object drop of a childless 4 KiB untyped) and +`ObjAllocA.disjoint_of_drop` give MakePool's disjointness. +-/ +import Fl4ProofsA64.P3.FrameA +import Fl4ProofsA64.P3.Stage3D1bA +import Fl4ProofsA64.P2.S1bLay +import Fl4ProofsA64.P1.MdbLiveC +import Fl4ProofsA64.P1.MdbRstZ +import Fl4ProofsA64.P1.S1aPlace +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +variable (st) in +/-- **A64 P1's invariants at an entry state** (module docstring). -/ +structure SysInvA (H : Heap) (Cs OS U Ac Pp Tb : Set U64) (Lv : U64 → Nat) (ia ie : Nat) (s : S) : Prop where + ent : EntryInvA3 st H Cs OS U Ac Pp Tb Lv s + sysl : SysL st H Cs OS ia ie s + +/-- **The slot apartness facts at an entry state** (gen 5 as a [J] premise of `P3/SlotJA`; gen 7, ruling 12:26Z +item 1: a field of `JEntA`, so no longer a premise): `OS` slots are off every SC (`ScApart`, A64 P1's `DelS.sca`) +and off the IRQ-state words (`IrqStApart`, `DelS.irqSt`); the VCPUs of `Z.V` are aligned and their `TCB` / +`RUN_CALLER` words lie in no `OS` slot and in no TCB's slot words (`VcOK.os`, `VcTy.w`). x86 carries the same facts +inside its relation (`JAtZ` / `TopPlaced`). -/ +structure SlotApA (Z : XSetsA) (H : Heap) (OS : Set U64) (kb : U64) : Prop where + sca : ScApart H.scs H.scBits OS + irqSt : IrqStApart OS kb + vcAl : ∀ v ∈ Z.V, Aligned v + vc : ∀ v ∈ Z.V, ∀ b, (b = v ∨ b = core.num.U64.wrapping_add v 24#u64) → ¬ InOS OS b ∧ ¬ W7.TLow H.tcbs b + /-- (gen 12, ruling 15:01Z item 3) **The VSpace relation's words lie in no `OS` slot and in no TCB** (the ASID + table, the pools, the Stage-1 tables and the windows' level-1 tables: `VsFootA`; x86: `TopPlaced.disj` for + `VsLay`). As `vc`, a placement fact the entry relation does not otherwise carry (A64 P1's `VsTy` does not place + its tables against the heap). -/ + vs : ∀ b, VsFootA Z.Pp Z.S1 Z.Y kb b → ¬ InOS OS b ∧ ∀ t ∈ H.tcbs, b.val < t.val ∨ t.val + 4096 ≤ b.val + /-- (gen 18, ruling 18:13Z item 4) **The Stage-2 / high-window relation's words lie in no `OS` slot and in no TCB** + (`ExtFootE`), as `vs`. -/ + ext : ∀ b, ExtFootE Z.X Z.Y Z.T2 b → ¬ InOS OS b ∧ ∀ t ∈ H.tcbs, b.val < t.val ∨ t.val + 4096 ≤ b.val + +/-- (gen 13, ruling 15:58Z item 1) **Every page of `P` lies inside an `other` object of the heap** (the generalised +`PtH`, the shape A64-S1A's `S1aPtCol` uses: a 4 KiB page `[p, p + 4096)` inside `[q, q + 2^z)`, so the 8 KiB +`other 13` high windows and Stage-2 roots fit later). At `JEntA` it is stated for the P3 pools and Stage-1 tables +`Z.Pp ∪ Z.S1`. -/ +def PtHA (H : Heap) (P : Set U64) : Prop := + ∀ p ∈ P, ∃ q z, H q = some (.other z) ∧ q.val ≤ p.val ∧ p.val + 4096 ≤ q.val + 2 ^ z + +/-- The retained ASID pools, translation tables, VCPUs, typed-heap objects, P1 heap objects + (covering scheduling contexts `Z.C = H.scs` at size `2 ^ H.scBits c`, endpoints, notifications, + replies, TCBs, and per-core idle TCBs/SCs), and the spec's idle-SC anchor `fl4Ptr idleScPtr`, + with their allocation sizes (frozen S3 gen 40, Architect 10:07Z ruling 1). -/ +def JointObjectA (Z : XSetsA) (H : Heap) (p : U64) (bytes : Nat) : Prop := + (bytes = 4096 ∧ (p ∈ Z.Pp ∨ p ∈ Z.S1 ∨ p ∈ Z.T2 ∨ p ∈ Z.V ∨ p = fl4Ptr Fl4SpecA64.idleScPtr)) ∨ + (bytes = 8192 ∧ (p ∈ Z.X ∨ p ∈ Z.Y)) ∨ + (∃ kind, Z.th p = some kind ∧ bytes = kind.size) ∨ + (∃ k, H p = some k ∧ bytes = 2 ^ k.bits) + +/-- Every SC in `H.scs` (which equals `Z.C` in `JEntA`) is a `JointObjectA` at size `2 ^ H.scBits c`. -/ +theorem JointObjectA.of_sc {Z : XSetsA} {H : Heap} {c : U64} (hc : c ∈ H.scs) : + JointObjectA Z H c (2 ^ H.scBits c) := by + obtain ⟨z, hz⟩ := hc + refine Or.inr (Or.inr (Or.inr ⟨.sc z, hz, ?_⟩)) + simp [Heap.scBits, hz, HKind.bits] + +/-- **J1 (Architect 21:51Z, D1): the retained objects lie in allocated memory** (the watermark form replacing +`ObjNamedA`): every `JointObjectA` object is off each `OS` untyped region or inside its used prefix `[q, q + fi)`. +Unlike a key-cap witness it is kept by a delete (the caps of `OS` only shrink their free part) and re-established by +Retype (new objects are allocated below the new watermark). -/ +def ObjAllocA (Z : XSetsA) (H : Heap) (OS : Set U64) (s : S) : Prop := + ∀ p bytes, JointObjectA Z H p bytes → + ∀ u ∈ OS, ∀ q b fi d, capA st s u = .Untyped q b fi d → + p.val + bytes ≤ q.val ∨ q.val + 2 ^ b.val ≤ p.val ∨ (q.val ≤ p.val ∧ p.val + bytes ≤ q.val + fi.val) + +/-- **J1 (D1): the synthetic idle-SC anchor** (the 4096-byte page at `fl4Ptr idle_sc_ptr`, `scRenA`'s target) lies +off every `OS` untyped region, so no retyped object can take its spec name. -/ +def IdleAnchorOffA (OS : Set U64) (s : S) : Prop := + ∀ u ∈ OS, ∀ q b fi d, capA st s u = .Untyped q b fi d → + (fl4Ptr Fl4SpecA64.idleScPtr).val + 4096 ≤ q.val ∨ q.val + 2 ^ b.val ≤ (fl4Ptr Fl4SpecA64.idleScPtr).val + +/-- Preserving all slot caps preserves the allocation facts. -/ +theorem ObjAllocA.keep {Z : XSetsA} {H : Heap} {OS : Set U64} {s s' : S} + (h : ObjAllocA (st := st) Z H OS s) (hk : OSKeep st OS s s') : ObjAllocA (st := st) Z H OS s' := + fun p bytes hp u hu q b fi d hc => h p bytes hp u hu q b fi d ((hk.capA hu).symm.trans hc) + +theorem IdleAnchorOffA.keep {OS : Set U64} {s s' : S} + (h : IdleAnchorOffA (st := st) OS s) (hk : OSKeep st OS s s') : IdleAnchorOffA (st := st) OS s' := + fun u hu q b fi d hc => h u hu q b fi d ((hk.capA hu).symm.trans hc) + +/-- **`DeadDrop4KA` [J]** (Architect 21:51Z D1; ledger item, producer: the Untyped line's `UtDropA`, after the S1A and +P1-MDB merges): **the dead objects of a childless 4 KiB untyped at `frame` are dropped**, as spec `deleteObjects` +drops them from `kheap` before MakePool's `retype_region`. Stated as its effect on the retained objects: no +`JointObjectA` object lies in the untyped's used prefix `[frame, frame + fi)` (a childless untyped's used prefix +holds only dead objects; the drop removes them from `Z`, `H` and the spec heap). With `ObjAllocA` at the untyped's +own slot it gives MakePool's disjointness (`ObjAllocA.disjoint_of_drop`). -/ +def DeadDrop4KA (Z : XSetsA) (H : Heap) (frame fi : U64) : Prop := + ∀ p bytes, JointObjectA Z H p bytes → frame.val ≤ p.val → p.val + bytes ≤ frame.val + fi.val → False + +/-- MakePool's disjointness from `ObjAllocA` at the untyped's slot and the dead-object drop. -/ +theorem ObjAllocA.disjoint_of_drop {Z : XSetsA} {H : Heap} {OS : Set U64} {s : S} + (h : ObjAllocA (st := st) Z H OS s) {u frame fi : U64} {d : Bool} (hu : u ∈ OS) + (hc : capA st s u = .Untyped frame 12#u64 fi d) (hD : DeadDrop4KA Z H frame fi) + {p : U64} {bytes : Nat} (hp : JointObjectA Z H p bytes) : + p.val + bytes ≤ frame.val ∨ frame.val + 4096 ≤ p.val := by + rcases h p bytes hp u hu frame 12#u64 fi d hc with h1 | h1 | ⟨h1, h2⟩ + · exact Or.inl h1 + · exact Or.inr (by simpa using h1) + · exact (hD p bytes hp h1 h2).elim + +/-- **The entry-level relation `SysInvA ∧ JAtZAX`** (module docstring). -/ +structure JEntA (AR : A → HwRootA → Prop) (Z : XSetsA) (H : Heap) (OS U Ac Pp Tb : Set U64) (Lv : U64 → Nat) + (ia ie : Nat) (s : S) (a : A) (σ : Fl4SpecA64.State Unit) (m : Fl4SpecA64.McState) : Prop where + sys : SysInvA st H Z.Cs OS U Ac Pp Tb Lv ia ie s + j : JAtZAX (L := L) AR Z s a σ m + tcbs : H.tcbs = Z.T + /-- (gen 3, ruling 09:59Z item 1) The P1 heap's SCs are the relation's (x86 `JAtZB.zH`'s SC half). -/ + scs : H.scs = Z.C + /-- (gen 3, `SchedRestA`) The relation's queue ghosts are A64 P1's queue lists (`SchedQueuesInv`: FL4's ready / + release queue words hold `Z.Q` / `Z.R`; x86 `SchedCore.ready` / `release`, inside x86's relation). -/ + sq : SchedQueuesInv st H.tcbs Z.Cs H.omega H s Z.Q Z.R + /-- (gen 7, ruling 12:26Z item 1) The slot apartness facts (`SlotApA`; x86: inside `JAtZ` / `TopPlaced`). -/ + ap : SlotApA Z H OS (L.ksb s) + /-- (gen 13, ruling 15:58Z item 1) The relation's ASID pools are A64 P1's (`VsTy`'s `Pp`). -/ + pp : Z.Pp = Pp + /-- (gen 13) The relation's Stage-1 tables are among A64 P1's tables (`VsTy`'s `Tb`; VS gen 2 Q2). -/ + s1 : Z.S1 ⊆ Tb + /-- (gen 13) A64 P1's `HeapKsOff` (S1a fact 1): heap objects off the kernel-state block, `other` ones possibly in + its two static regions (x86 `JKern.hks` / `hksIn`). -/ + ko : HeapKsOff H (L.ksb s) + /-- (gen 13) The pools' and Stage-1 tables' pages lie inside `other` objects of `H` (`PtHA`). -/ + ptH : PtHA H (Z.Pp ∪ Z.S1) + /-- The ASID table is injective on its occupied entries (`SpecVsA.tabInj`). -/ + tabInj : ∀ i j p, σ.archState.armAsidTable i = some p → σ.archState.armAsidTable j = some p → i = j + /-- The live pools' words have P1's `PoolOff` placement. -/ + poolOff : PoolOff st H Z.Cs OS s + /-- (Architect 21:51Z D1, J1) The retained joint objects lie off untyped memory or below its watermark. -/ + objAlloc : ObjAllocA (st := st) Z H OS s + /-- (D1, J1) The synthetic idle-SC anchor is off untyped memory. -/ + idleAnchor : IdleAnchorOffA (st := st) OS s + /-- The VSpace footprint misses the VCPU words (`VrOK.vc` on this relation's pages). -/ + vsVc : ∀ b, VsFootA Z.Pp Z.S1 Z.Y (L.ksb s) b → ¬ VcW Z.V b + +/-- Childlessness excludes overlapping key-cap ranges, including a root whose base precedes the untyped frame. -/ +theorem keyDisjoint_of_childlessA {T Ω Sg OS : Set U64} {G : MdbGhostA} {s : S} + (hO : OSOK T Ω Sg OS) (hTr : TreeA st OS G.chains G.par s) (hRev : UtRev st OS s) + {u q b i : U64} {d : Bool} (hu : u ∈ OS) (hc : capA st s u = .Untyped q b i d) + (he : cdt.ensure_no_children st s u = ok (core.result.Result.Ok ())) + {y : U64} (hy : y ∈ OS) (hk : RngKeyA (capA st s y)) : + DisjRA (capA st s y) q.val (q.val + 2 ^ b.val) := by + have huy : y ≠ u := fun e => by + have h := hk.2.1 + rw [e, hc] at h + cases h + have hut : isUtA (capA st s u) = true := by rw [hc]; rfl + have hnd := cdt.ensure_no_children.noDesc hO hTr hu hc (hRev u hu hut) he + have hdisj : DisjA (capA st s y) (capA st s u) := by + by_contra hn + rcases hTr.ut.drange u hu q b i d hc y hy huy hk.1 hn with hd | hd + · exact hnd y hd + · have bad := hTr.ut.anc y u hd hut + rw [hk.2.1] at bad + cases bad + have hreg := hTr.ut.reg u hu (by rw [hc]; exact physA_ut _ _ _ _) + rw [hc] at hreg + obtain ⟨hbase, htop, _⟩ := ut_rangeA (capA_ut_b hc) hreg + have hpos : 1 ≤ 2 ^ b.val := Nat.one_le_two_pow + rcases hdisj with h | h + · exact Or.inl (by rw [hc, hbase] at h; exact h) + · exact Or.inr (by rw [hc, htop] at h; omega) + +/-- By `VsRelA.table`, a live pool appears at only one high index in the concrete ASID table. -/ +theorem JEntA.poolTable_injA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) {i j p : U64} + (hi : i.val < 128) (hj : j.val < 128) (hp : p ≠ 0#u64) + (ei : L.mem s (atAF (L.ksb s) i) = p) (ej : L.mem s (atAF (L.ksb s) j) = p) : i = j := by + have ti := E.j.1.vs.table i hi + have tj := E.j.1.vs.table j hj + rw [ei, optPtrA, if_neg hp] at ti + rw [ej, optPtrA, if_neg hp] at tj + have e := congrArg BitVec.toNat (E.tabInj _ _ _ ti tj) + simp only [BitVec.toNat_ofNat] at e + change i.val % 128 = j.val % 128 at e + rw [Nat.mod_eq_of_lt hi, Nat.mod_eq_of_lt hj] at e + exact UScalar.eq_of_val_eq e + +/-- A step keeping the state-block base and ASID table carries P1's pool placement. -/ +theorem poolOff_keepTableA {Z : XSetsA} {H : Heap} {OS : Set U64} {s s' : S} + (P : PoolOff st H Z.Cs OS s) (hk : L.ksb s' = L.ksb s) + (h : ∀ i, L.mem s' (core.num.U64.wrapping_add (L.ksb s) (AtOff i)) = + L.mem s (core.num.U64.wrapping_add (L.ksb s) (AtOff i))) : + PoolOff st H Z.Cs OS s' := by + intro i + have hi := P i + rw [vspace.asid_table.eq] at hi ⊢ + rw [hk, h i] + refine (WP.spec_ok _).2 fun hne asid => ?_ + have w := (WP.spec_ok _).1 hi hne asid + exact ⟨w.al, w.om, w.sg, w.os, RegionOffNodes.ksb hk w.ks⟩ + +/-! ## Bridges from `BXI` -/ + +section bxi +variable {T Cs Ω Sg Rp Scs Nt Ac : Set U64} {H : Heap} {Z0 : U64 → Nat} {s : S} + +theorem bxi_cpuCs (h : BXI st T Cs Ω H Sg Rp Scs Nt Z0 Ac s) : L.cpu s ∈ Cs := by + obtain ⟨⟨Q, R, hq⟩, -⟩ := h; exact hq.cur + +theorem bxi_curT (h : BXI st T Cs Ω H Sg Rp Scs Nt Z0 Ac s) : + L.mem s (L.nodeAt s (L.cpu s) layout.node.CUR_THREAD) ∈ T := by + obtain ⟨⟨Q, R, hq⟩, -⟩ := h; exact hq.curT + +theorem bxi_idleT (h : BXI st T Cs Ω H Sg Rp Scs Nt Z0 Ac s) : + L.mem s (L.nodeAt s (L.cpu s) layout.node.IDLE_THREAD) ∈ T := by + obtain ⟨⟨Q, R, hq⟩, -⟩ := h; exact hq.idleT + +theorem bxi_nodes (h : BXI st T Cs Ω H Sg Rp Scs Nt Z0 Ac s) : + ∀ c ∈ Cs, ∀ o : U64, o.val < 4288 → o.val % 8 = 0 → Aligned (L.nodeAt s c o) := by + obtain ⟨⟨Q, R, hq⟩, -⟩ := h + exact fun c hc o h1 h2 => nodeAt_aligned (hq.nodes c hc) h1 h2 + +end bxi + +/-! ## Bridges from `EntryInvA` and `SysL` -/ + +section ent +variable {H : Heap} {Cs OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} {ia ie : Nat} {s : S} + +theorem ent_ksFit (h : EntryInvA3 st H Cs OS U Ac Pp Tb Lv s) : + (L.ksb s).val + ksSizeA ≤ 2 ^ 64 := h.ksw + +/-- A64 P1's MDB tree at `OS` (`SysL`'s `MdbST`). -/ +theorem sysl_tree (h : SysL st H Cs OS ia ie s) : ∃ G par, TreeA st OS G par s := by + obtain ⟨G, par, lk, -, -, -, -, tr, ut⟩ := h.sys.stz.1.1 + exact ⟨G, par, ⟨lk, tr, ut⟩⟩ + +/-- **`CdtRelA.tree` from A64 P1's tree** (module docstring). -/ +theorem cdtTree_of {th : TypedHeapA} {G : List (List U64)} {par : U64 → Option U64} (h : TreeA st OS G par s) + (hs : ∀ a, IsSlotA th a → a ∈ OS) (hn : ∀ x ∈ OS, ¬ IsSlotA th x → capA st s x = .Null) : + TreeA st {a | IsSlotA th a} G par s := + h.sub (fun a ha => hs a ha) fun x hx hn' => hn x hx hn' + +end ent + +/-- **`PlaceA` from `HeapOK`** (module docstring): every object of `th` but an IRQ-node slot is an object of `H` of +the same size and apart from the kernel-state block (S1b's `PlaceA.ks` gives it for an object of `H` that is not +`other`), and the IRQ-node slots are one-slot CNodes of `th` (gen 3: `PlaceA.ks` relaxed, `PlaceA.irq`; their +alignment, fit and apartness come from the block's); (S3 gen 18) no object of `th` is at 0 (`hnz`, `PlaceA.nz`). -/ +theorem place_of {th : TypedHeapA} {H : Heap} {kb : U64} (hH : HeapOK H) + (hth : ∀ p k, th p = some k → ¬ IrqNodeA kb p → ∃ hk, H p = some hk ∧ k.size = 2 ^ hk.bits) + (hks : ∀ p k, th p = some k → ¬ IrqNodeA kb p → p.val + k.size ≤ kb.val ∨ kb.val + ksSizeA ≤ p.val) + (hirq : ∀ i : U64, i.val < 5376 → th (irqSlotA kb i) = some (.cnode 0)) + (hkb : kb.val % 32 = 0) (hkf : kb.val + ksSizeA ≤ 2 ^ 64) (hnz : ∀ p k, th p = some k → p ≠ 0#u64) : + PlaceA th kb := by + have hI : ∀ p k, th p = some k → IrqNodeA kb p → k.size = 32 ∧ ∃ i : U64, i.val < 5376 ∧ + p.val = kb.val + 43232 + 32 * i.val := by + rintro p k hp ⟨i, hi, rfl⟩ + rw [hirq i hi] at hp + cases hp + exact ⟨rfl, i, hi, irqSlotA_val hkf hi⟩ + have hf : ksSizeA = 788736 := rfl + refine ⟨fun p k hp => ?_, fun p k hp => ?_, fun p q kp kq hp hq hne => ?_, hks, hirq, hnz⟩ + · by_cases hn : IrqNodeA kb p + · obtain ⟨e, i, hi, hv⟩ := hI p k hp hn + rw [e, hv]; omega + · obtain ⟨hk, e, hs⟩ := hth p k hp hn + rw [hs]; exact hH.align p hk e + · by_cases hn : IrqNodeA kb p + · obtain ⟨e, i, hi, hv⟩ := hI p k hp hn + rw [e, hv]; omega + · obtain ⟨hk, e, hs⟩ := hth p k hp hn + rw [hs]; exact hH.fits p hk e + · have hv : p.val ≠ q.val := fun e => hne (UScalar.eq_of_val_eq e) + by_cases hnp : IrqNodeA kb p <;> by_cases hnq : IrqNodeA kb q + · obtain ⟨e, i, hi, h1⟩ := hI p kp hp hnp + obtain ⟨e', j, hj, h2⟩ := hI q kq hq hnq + rw [e, e']; omega + · obtain ⟨e, i, hi, h1⟩ := hI p kp hp hnp + have := hks q kq hq hnq + rw [e]; omega + · obtain ⟨e', j, hj, h2⟩ := hI q kq hq hnq + have := hks p kp hp hnp + rw [e']; omega + · obtain ⟨hk, e, hs⟩ := hth p kp hp hnp + obtain ⟨hk', e', hs'⟩ := hth q kq hq hnq + rw [hs, hs']; exact hH.disj p hk q hk' e e' hne + +/-- S1b's `PlaceA.ks` for an object of `th` whose `H` object is not `other`. -/ +theorem ks_of_s1b {H : Heap} {OS U Pp : Set U64} {s : S} + (hP : Fl4ProofsA64.P2.S1b.PlaceA st H OS U Pp s) {p : U64} {k : ObjKindA} {hk : HKind} + (e : H p = some hk) (hs : k.size = 2 ^ hk.bits) (ho : ∀ z, hk ≠ .other z) : + p.val + k.size ≤ (L.ksb s).val ∨ (L.ksb s).val + ksSizeA ≤ p.val := by + rw [hs]; exact hP.ks p hk e ho + +/-! ## (gen 13) The VSpace leaves' placement hypotheses from `JEntA` -/ + +/-- **A page inside an `other` object is off the ASID table** (`HeapKsOff`: an `other` object inside the block is in +the globals or inside the 1 KiB table, too small for a 4 KiB page): no entry of the page is an `AsW` word. -/ +theorem ptHA_not_asW {H : Heap} {kb : U64} {P : Set U64} (hH : HeapOK H) (hK : HeapKsOff H kb) + (hw : kb.val + 788736 ≤ 2 ^ 64) (hP : PtHA H P) {p : U64} (hp : p ∈ P) {i : U64} (hi : i.val < 512) : + ¬ AsW kb (peAF p i) := by + obtain ⟨q, z, hq, h1, h2⟩ := hP p hp + have hf := hH.fits q _ hq + simp only [HKind.bits] at hf + have hpv := peAF_valA (p := p) (by omega) hi + rintro ⟨o, o1, o2, -, e⟩ + have : (peAF p i).val = kb.val + o.val := by + rw [e, wa_valm]; exact Nat.mod_eq_of_lt (by omega) + rcases hK.2 q z hq with h | h | h | h <;> omega + +/-- **The kernel-state block is aligned** (`JAtZA.place`: the first IRQ-node slot is a 32-byte CNode at +`ksb + 43232`). -/ +theorem JAtZA.ksbAl {AR : A → HwRootA → Prop} {Z : XSetsA} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} (J : JAtZA (L := L) AR Z s a σ m) : (L.ksb s).val % 32 = 0 := by + have h0 : (0#u64 : U64).val < 5376 := by simp + have h1 := J.place.align _ _ (J.place.irq 0#u64 h0) + rw [irqSlotA_val J.ksFit h0, show ObjKindA.size (.cnode 0) = 32 from rfl, + show (0#u64 : U64).val = 0 from rfl] at h1 + omega + +/-- **The VS leaves' `hK` / `hT` (and so `hA`) from `JEntA`** (gen 13, ruling 15:58Z item 1): the block is aligned, +and no entry of a Stage-1 table of `Z.S1` (nor of a pool of `Z.Pp`) is an ASID-table word. These are exactly the +hypotheses `hK`, `hT` of `unmap_pageA` / `unmap_page_tableA` and `hA`, `hK` of `write_storePteA` / +`write_clearPteA` at `Tb := Z.S1`. -/ +theorem JEntA.vsLeafA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) : + Aligned (L.ksb s) ∧ (∀ p ∈ Z.S1, ∀ i : U64, i.val < 512 → ¬ AsW (L.ksb s) (peAF p i)) ∧ + (∀ p ∈ Z.Pp, ∀ i : U64, i.val < 512 → ¬ AsW (L.ksb s) (peAF p i)) := by + have h32 := J.j.1.ksbAl + refine ⟨show (L.ksb s).val % 8 = 0 by omega, fun p hp i hi => ?_, fun p hp i hi => ?_⟩ + · exact ptHA_not_asW J.sys.ent.heap J.ko J.sys.ent.ksw J.ptH (Or.inr hp) hi + · exact ptHA_not_asW J.sys.ent.heap J.ko J.sys.ent.ksw J.ptH (Or.inl hp) hi + +/-! ## The entry-level frame -/ + +/-- (gen 3) **The relation's SCs are aligned**: they are `H`'s (`JEntA.scs`), whose SCs are at least 128 bytes +(`HeapOK.scmin`) and aligned to their size. -/ +theorem JEntA.scAl {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) : ∀ c ∈ Z.C, Aligned c := by + intro c hc + rw [← J.scs] at hc + obtain ⟨z, hz⟩ := hc + have h1 := J.sys.ent.heap.align c _ hz + have h2 := J.sys.ent.heap.scmin c z hz + simp only [HKind.bits] at h1 + have : 8 ∣ 2 ^ z := ⟨2 ^ (z - 3), by rw [show z = 3 + (z - 3) by omega, pow_add]; simp⟩ + exact Nat.mod_eq_zero_of_dvd (Nat.dvd_trans this (Nat.dvd_of_mod_eq_zero h1)) + +/-- **`JEntA`'s relation across a P1 step writing off the footprint** (module docstring), the nodes' alignment from +`BXI`, the SCs' from `JEntA.scs` (gen 3: the SC hypothesis is gone). -/ +theorem JEntA.writesIn {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {s s' : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) {W : U64 → Prop} (hw : WritesIn st s s' W) + (hW : ∀ b, W b → ¬ FootA (L := L) Z s b) (hV : ∀ v ∈ Z.V, Aligned v) : + JAtZAX (L := L) AR Z s' a σ m := + J.j.of_writesIn hw hW (FootA.aligned J.j.1 (bxi_nodes J.sys.ent.bxi) J.scAl hV) + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/EpBufA.lean b/proofs/Fl4ProofsA64/P3/EpBufA.lean new file mode 100644 index 000000000..3584f392a --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/EpBufA.lean @@ -0,0 +1,395 @@ +/- +P3 (aarch64, A64-P3-IPC gen 4): **the IPC-buffer half** of the endpoint leaves — the A64 port of x86's `P3/EpBytes`, +the store half of `P3/EpBufSpec`, the buffer half of `P3/EpMrs` (`setMrs_eq`) and `P3/IpcBuf`'s `lookup_buf_full` / +`BufCover`, so that FrontEndExtraA's decode-site view `BufViewA` follows from `EpStateRel` (`lookup_buf_view`). + +* Spec bytes (x86 `EpBytes`, verbatim on `Fl4SpecA64`): `storeMem` (AArch64 `storeWord` on the byte memory), + `load_store_same`, `load_store_other`. +* Spec stores (x86 `EpBufSpec`): `setUm`, `storeWord_eq`, `storeWordOffs_eq`, `inUserFrame_updK`. +* **`setMrs_eq`** (x86 `EpMrs.setMrs_eq`): l4v `set_mrs rcv (Some p) ms` on a TCB, aligned buffer words in user frames, + has the one outcome `mrState` (registers `mrRegs`, buffer `mrMem` of `mrPairs`), not failing. +* The spec's buffer lookup: `ipcBufOfA` (l4v AArch64 `lookup_ipc_buffer` as a function of the TCB: slot 2's frame cap, + **not device** (the AArch64 spec's `frameCap … false` pattern; x86's `ipcBufOf` did not test it, whence x86's + `IpcBufCapOK.dev`), read-write, or read-only for a sender), **`lookupIpcBuffer_eqA`** (one outcome). +* FL4's lookup: **`lookup_buf_none`**: FL4's `msg::lookup_ipc_buffer` of a TCB of `th` returns 0 whenever the spec finds + no buffer (from `StateRelA`'s slot 2 and `absCapA`: the same frame / device / rights tests). +* **`BufCover`** (x86 `IpcBuf.BufCover`; ghost placement of `Bf`): for a TCB of `T` whose spec buffer is `sb`, the + word FL4's lookup returns is tracked with it, `(w, sb) ∈ Bf`, and is nonzero. (x86 derives "nonzero" from + `IpcBufCapOK`'s `p ≠ 0` and `SlotsValid`'s frame alignment; A64's `CapValidA` / `CapWfA` carry neither, so it is + part of the placement; see the handoff.) +* **`lookup_buf_full`** (x86 `lookup_buf_full`): FL4's word is 0 exactly when the spec finds no buffer, and is tracked + with the spec's buffer otherwise; **`lookup_buf_view`**: hence `BufViewA` (FrontEndExtraA's decode-site view) at + FL4's lookup and the spec's (`EpStateRel.bufView`, `bufViewA_none`). + +Hypotheses: `StateRelA`, `th t = some .tcb`, `t ∈ T`, `BufCover` (placement), `BufNativeA`, alignment / `inUserFrame` of the stored +words (case hypotheses of `setMrs_eq`, as x86). No named premise. +-/ +import Fl4ProofsA64.P3.EpMrsA +import Mathlib.Tactic.IntervalCases +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp + +noncomputable section + +/-! ## The bytes (x86 `EpBytes`) -/ + +section Bytes +open Fl4SpecA64 + +/-- The byte memory after AArch64 `storeWord p w`. -/ +def storeMem (um : Word → Word8) (p w : Word) : Word → Word8 := + (intUpto 0 7).foldl + (fun m i => Function.update m (p + BitVec.ofInt 64 i) ((wordRsplit w : List Word8).getD (7 - i.toNat) 0)) um + +theorem intUpto_07 : intUpto 0 7 = [0, 1, 2, 3, 4, 5, 6, 7] := by decide + +theorem wordRsplit_eq (w : Word) : (wordRsplit w : List Word8) = + [(w >>> 56).setWidth 8, (w >>> 48).setWidth 8, (w >>> 40).setWidth 8, (w >>> 32).setWidth 8, + (w >>> 24).setWidth 8, (w >>> 16).setWidth 8, (w >>> 8).setWidth 8, (w >>> 0).setWidth 8] := by + unfold wordRsplit + rw [show (64 + 8 - 1) / 8 = 8 from rfl, show List.range 8 = [0, 1, 2, 3, 4, 5, 6, 7] from rfl] + rfl + +theorem load_store_same (um : Word → Word8) (p w : Word) : loadWordAt (storeMem um p w) p = w := by + unfold storeMem loadWordAt + rw [intUpto_07, wordRsplit_eq] + simp (config := { decide := true }) only [List.foldl_cons, List.foldl_nil, List.map_cons, List.map_nil, + Function.update_apply, add_right_inj, ↓reduceIte] + simp only [Int.reduceToNat, Nat.reduceSub, List.getD_eq_getElem?_getD, List.getElem?_cons_succ, + List.getElem?_cons_zero, Option.getD_some] + unfold wordRcat + simp only [List.foldl_cons, List.foldl_nil] + apply BitVec.eq_of_getLsbD_eq + intro i hi + simp only [BitVec.getLsbD_or, BitVec.getLsbD_shiftLeft, BitVec.getLsbD_setWidth, BitVec.getLsbD_ushiftRight] + interval_cases i <;> simp + +theorem addr_ne {p q x y : Word} (hp : p.toNat % 8 = 0) (hq : q.toNat % 8 = 0) (hne : q ≠ p) + (hx : x.toNat < 8) (hy : y.toNat < 8) : q + x ≠ p + y := by + intro e + have h := congrArg BitVec.toNat e + rw [BitVec.toNat_add, BitVec.toNat_add] at h + have h1 := p.isLt; have h2 := q.isLt + have h3 : q.toNat + x.toNat < 2 ^ 64 := by omega + have h4 : p.toNat + y.toNat < 2 ^ 64 := by omega + rw [Nat.mod_eq_of_lt h3, Nat.mod_eq_of_lt h4] at h + exact hne (BitVec.eq_of_toNat_eq (by omega)) + +theorem storeMem_other (um : Word → Word8) (p w a : Word) (h : ∀ y : Word, y.toNat < 8 → a ≠ p + y) : + storeMem um p w a = um a := by + unfold storeMem + rw [intUpto_07] + simp only [List.foldl_cons, List.foldl_nil] + rw [Function.update_of_ne (h _ (by decide)), Function.update_of_ne (h _ (by decide)), + Function.update_of_ne (h _ (by decide)), Function.update_of_ne (h _ (by decide)), + Function.update_of_ne (h _ (by decide)), Function.update_of_ne (h _ (by decide)), + Function.update_of_ne (h _ (by decide)), Function.update_of_ne (h _ (by decide))] + +theorem load_store_other (um : Word → Word8) {p q : Word} (w : Word) (hp : p.toNat % 8 = 0) (hq : q.toNat % 8 = 0) + (hne : q ≠ p) : loadWordAt (storeMem um p w) q = loadWordAt um q := by + unfold loadWordAt + congr 1 + apply List.map_congr_left + intro i hi + rw [intUpto_07] at hi + refine storeMem_other um p w _ fun y hy => addr_ne hp hq hne ?_ hy + simp only [List.mem_cons, List.not_mem_nil, or_false] at hi + rcases hi with rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> decide + +end Bytes + +/-! ## The spec's stores (x86 `EpBufSpec`) and `set_mrs` (x86 `EpMrs`) -/ + +section Stores +open Fl4SpecA64 Fl4ProofsA64.P3.Notif + +/-- The state with user memory `um`. -/ +def setUm (σ : State Unit) (um : Word → Word8) : State Unit := + { σ with machineState := { σ.machineState with underlyingMemory := um } } + +theorem storeWord_eq (ms : MachineState) (a w : Word) (h : a.toNat % 8 = 0) : + storeWord a w ms = ({((), { ms with underlyingMemory := storeMem ms.underlyingMemory a w })}, False) := by + unfold storeWord + rw [nd_bind_one (assert_trueA (aligned_maskA h) ms)] + rfl + +theorem storeWordOffs_eq (σ : State Unit) (p : ObjRef) (x : Nat) (v : Word) (h : (bufAddr p x).toNat % 8 = 0) + (hu : inUserFrame (bufAddr p x) σ) : + storeWordOffs (Z := Unit) p x v σ = ({((), setUm σ (storeMem σ.machineState.underlyingMemory (bufAddr p x) v))}, + False) := by + unfold storeWordOffs doMachineOp + unfold bufAddr at h hu ⊢ + rw [nd_bind_one (show (Fl4Spec.NonDet.get : Fl4Spec.NonDet (State Unit) (State Unit)) σ = ({(σ, σ)}, False) from rfl), + nd_bind_one (assert_trueA hu σ), nd_gets_bind, storeWord_eq _ _ _ h, nd_bind_one (selectF_oneA _ σ)] + dsimp only + rw [nd_bind_one (show Fl4Spec.NonDet.modify (fun state : State Unit => _) σ = _ from rfl)] + rfl + +/-- A user frame stays one when a kernel object is replaced by one of the same kind (here: anything that is not a +data page). -/ +theorem inUserFrame_updK {σ : State Unit} {p : ObjRef} {o o' : KernelObject} (hk : σ.kheap p = some o) + (ho : ∀ sz, o ≠ .archObj (.dataPage false sz)) (a : Word) (h : inUserFrame a σ) : inUserFrame a (updK σ p o') := by + obtain ⟨sz, hsz⟩ := h + refine ⟨sz, ?_⟩ + have hne : (a &&& ~~~(mask (pageBitsForSize sz))) ≠ p := by + intro e; rw [e, hk] at hsz; cases hsz; exact ho sz rfl + simp [updK, hne, hsz] + +/-- User memory after storing `ps` into the buffer at `p`. -/ +def mrMem (um : Word → Word8) (p : Word) : List (Nat × Word) → (Word → Word8) + | [] => um + | (x, v) :: ps => mrMem (storeMem um (bufAddr p x) v) p ps + +/-- The state after `set_mrs rcv (Some p) ms`. -/ +def mrState (σ : State Unit) (rcv : ObjRef) (x : Tcb) (p : ObjRef) (ms : List Word) : State Unit := + setUm (updK σ rcv (.tcb (withRegs x (mrRegs x.tcbArch.tcbContext.userRegs ms)))) + (mrMem σ.machineState.underlyingMemory p (mrPairs ms)) + +theorem mrMem_snoc (um : Word → Word8) (p : Word) : + ∀ (ps : List (Nat × Word)) (x : Nat) (v : Word), mrMem um p (ps ++ [(x, v)]) = storeMem (mrMem um p ps) (bufAddr p x) v + | [], x, v => rfl + | (y, w) :: ps, x, v => mrMem_snoc _ p ps x v + +theorem zipWithMx_store (p : ObjRef) : ∀ (xs : List Nat) (ys : List Word) (σ : State Unit), + (∀ q ∈ xs.zip ys, (bufAddr p q.1).toNat % 8 = 0 ∧ inUserFrame (bufAddr p q.1) σ) → + Fl4Spec.NonDet.zipWithMx (fun x v => storeWordOffs (Z := Unit) p x v) xs ys σ = + ({((), setUm σ (mrMem σ.machineState.underlyingMemory p (xs.zip ys)))}, False) + | [], _, σ, _ => by + show (pure () : Fl4Spec.NonDet (State Unit) Unit) σ = _ + rw [nd_pure_apply']; rfl + | _ :: _, [], σ, _ => by + show (pure () : Fl4Spec.NonDet (State Unit) Unit) σ = _ + rw [nd_pure_apply']; rfl + | x :: xs, y :: ys, σ, h => by + obtain ⟨h1, h2⟩ := h (x, y) (by simp) + unfold Fl4Spec.NonDet.zipWithMx + rw [nd_bind_one' (storeWordOffs_eq σ p x y h1 h2)] + rw [zipWithMx_store p xs ys _ fun q hq => by + obtain ⟨g1, g2⟩ := h q (by simp [hq]); exact ⟨g1, g2⟩] + rfl + +/-- **`set_mrs` with an IPC buffer** (x86 `setMrs_eq`): one outcome, no failure. -/ +theorem setMrs_eq {σ : State Unit} {rcv : ObjRef} {x : Tcb} (hk : σ.kheap rcv = some (.tcb x)) (p : ObjRef) + (ms : List Word) (hal : ∀ q ∈ mrPairs ms, (bufAddr p q.1).toNat % 8 = 0 ∧ inUserFrame (bufAddr p q.1) σ) : + setMrs (Z := Unit) rcv (some p) ms σ = ({(natToLen (min ms.length msgMaxLength), mrState σ rcv x p ms)}, False) := by + unfold setMrs + rw [nd_bind_one (getTcb_eq hk), nd_pure_bind', nd_pure_bind'] + erw [nd_bind_one (setObject_eq (o' := .tcb (withRegs x (mrRegs x.tcbArch.tcbContext.userRegs ms))) hk rfl)] + rw [nd_pure_bind'] + dsimp only + rw [nd_bind_one (zipWithMx_store p _ _ _ (fun q hq => by + obtain ⟨g1, g2⟩ := hal q hq + exact ⟨g1, inUserFrame_updK hk (fun _ h => by cases h) _ g2⟩))] + rfl + +end Stores + +/-! ## The buffer lookup (x86 `IpcBuf`) -/ + +section SpecLookup +open Fl4SpecA64 Fl4ProofsA64.P3.Notif Classical + +/-- **The spec's IPC buffer of a TCB** (l4v AArch64 `lookup_ipc_buffer`, as a function of the TCB: slot 2). -/ +def ipcBufOfA (b : Bool) (x : Tcb) : Option ObjRef := + match x.tcbIpcframe with + | .archObjectCap (.frameCap p R vms false _) => + if vmReadWrite ⊆ R ∨ vmReadOnly ⊆ R ∧ ¬b = true then some (p + (x.tcbIpcBuffer &&& mask (pageBitsForSize vms))) + else none + | _ => none + +theorem tcbCnodeMap_twoA (x : Tcb) : tcbCnodeMap x (tcbCnodeIndex 2) = some x.tcbIpcframe := by + simp [tcbCnodeMap, tcbIdx_eq, tcbStage2Slot, tcbHighVSpaceSlot] + +/-- `get_cap` of a TCB slot, one outcome. -/ +theorem getCap_tcb_eqA {σ : State Unit} {t : ObjRef} {x : Tcb} {idx : List Bool} {c : Cap} + (hk : σ.kheap t = some (.tcb x)) (hc : tcbCnodeMap x idx = some c) : + getCap (Z := Unit) (t, idx) σ = ({(c, σ)}, False) := by + unfold getCap + dsimp only + rw [nd_bind_one (getObject_eq hk)] + dsimp only + erw [nd_pure_bind'] + show Fl4Spec.NonDet.assertOpt (tcbCnodeMap x idx) σ = _ + rw [hc]; rfl + +/-- **l4v `lookup_ipc_buffer` has one outcome**, `ipcBufOfA`. -/ +theorem lookupIpcBuffer_eqA {σ : State Unit} {t : ObjRef} {x : Tcb} (hk : σ.kheap t = some (.tcb x)) (b : Bool) : + lookupIpcBuffer (Z := Unit) b t σ = ({(ipcBufOfA b x, σ)}, False) := by + unfold lookupIpcBuffer + rw [nd_bind_one (threadGet_eq _ hk), nd_pure_bind', nd_bind_one (getCap_tcb_eqA hk (tcbCnodeMap_twoA x))] + unfold ipcBufOfA + cases hc : x.tcbIpcframe <;> try rfl + rename_i a + cases a <;> try rfl + rename_i p R vms d md + cases d <;> try rfl + dsimp only + split_ifs <;> rfl + +end SpecLookup + +section Lookup +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Classical +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +theorem tcb_slot_bufferA (t : U64) : + obj.Tcb.slot t layout.tcb.SLOT_BUFFER = ok (slotAddrA (specPtr t) (BitVec.ofNat 64 2)) := by + rw [slotAddrA_sp] + unfold obj.Tcb.slot + simp only [lift, bind_ok] + congr 1 + rw [UScalar.eq_equiv_bv_eq] + simp [layout.cte.SIZE, layout.tcb.SLOT_BUFFER] + +theorem ipc_buffer_okA (s : S) (t : U64) : obj.Tcb.ipc_buffer st t s ⦃ _ => True ⦄ := by + unfold obj.Tcb.ipc_buffer layout.tcb.IPC_BUFFER; step* + +theorem frame_off_okA (sz : capability.FrameSize) (p w : U64) : + (do + let bits ← capability.FrameSize.bits sz + let i ← lift (bits &&& 63#u64) + let i1 ← 1#u64 <<< i + let i2 ← lift (core.num.U64.wrapping_sub i1 1#u64) + let i3 ← lift (w &&& i2) + ok (core.num.U64.wrapping_add p i3)) ⦃ _ => True ⦄ := by + cases sz <;> simp only [capability.FrameSize.bits, bind_ok] <;> step* + +theorem vmRightsOfA_ro {r : U64} (h : r.val = 2 ∨ r.val = 3) : Fl4SpecA64.vmReadOnly ⊆ vmRightsOfA r := by + unfold vmRightsOfA Fl4SpecA64.vmReadOnly Fl4SpecA64.vmReadWrite + rcases h with h | h <;> simp [h, Set.subset_def] + +theorem vmRightsOfA_rw {r : U64} (h : r.val = 3) : Fl4SpecA64.vmReadWrite ⊆ vmRightsOfA r := by + unfold vmRightsOfA; simp [h] + +/-- A TCB's IPC-buffer frame cap is native. FL4 clears `is_ept` when deriving the cap for this slot. -/ +def BufNativeA (T : Set U64) (mem : U64 → U64) : Prop := + ∀ t ∈ T, match capAtA mem (slotAddrA (specPtr t) (BitVec.ofNat 64 2)) with + | .Frame _ _ _ _ _ ept _ _ => ept = false + | _ => True + +theorem bufSlot_eqA (t : U64) : slotAddrA (specPtr t) (BitVec.ofNat 64 2) = sbWordA t := by + have h := tcb_slot_bufferA t + have h' : obj.Tcb.slot t layout.tcb.SLOT_BUFFER = ok (sbWordA t) := by + simp [obj.Tcb.slot, lift, sbWordA] + exact ok_inj_ff (h.symm.trans h') + +/-- Keeping the two cap words of slot 2 keeps its native-frame invariant. -/ +theorem BufNativeA.frame {T : Set U64} {mem mem' : U64 → U64} (h : BufNativeA T mem) + (hcap : ∀ t ∈ T, mem' (sbWordA t) = mem (sbWordA t) ∧ + mem' (core.num.U64.wrapping_add (sbWordA t) 8#u64) = + mem (core.num.U64.wrapping_add (sbWordA t) 8#u64)) : + BufNativeA T mem' := by + intro t ht + obtain ⟨h0, h8⟩ := hcap t ht + have hn := h t ht + rw [bufSlot_eqA] at hn ⊢ + unfold capAtA at hn ⊢ + rw [h0, h8] + exact hn + +/-- **FL4's buffer word is 0 when the spec finds no buffer** (the frame / device / rights tests of both lookups agree +through `absCapA`). -/ +theorem lookup_buf_none {th : TypedHeapA} {T : Set U64} (s : S) (σ : Fl4SpecA64.State Unit) (hR : StateRelA th (L.mem s) σ) + (hNative : BufNativeA T (L.mem s)) {t : U64} (ht : th t = some .tcb) (htT : t ∈ T) + {x : Fl4SpecA64.Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) (b : Bool) : + msg.lookup_ipc_buffer st s b t ⦃ w => ipcBufOfA b x = none → w = 0#u64 ⦄ := by + obtain ⟨x', hk', hs⟩ := hR.tcb t ht + rw [hk] at hk'; cases hk' + obtain ⟨ac, hac, hca⟩ := hs 2 (by decide) + rw [tcbCnodeMap_twoA] at hac + cases hac + obtain ⟨-, hab⟩ := hca + unfold msg.lookup_ipc_buffer + refine WP.spec_bind (ipc_buffer_okA s t) fun wv _ => ?_ + rw [tcb_slot_bufferA, bind_ok, Fl4ProofsA64.P1.W7.R.ST.capA_eq, bind_ok] + have hz : (ok 0#u64 : Result U64) ⦃ w => ipcBufOfA b x = none → w = 0#u64 ⦄ := by + rw [WP.spec_ok]; exact fun _ => rfl + have hsm : ∀ sz p, ipcBufOfA b x ≠ none → (do + let bits ← capability.FrameSize.bits sz + let i ← lift (bits &&& 63#u64) + let i1 ← 1#u64 <<< i + let i2 ← lift (core.num.U64.wrapping_sub i1 1#u64) + let i3 ← lift (wv &&& i2) + ok (core.num.U64.wrapping_add p i3)) ⦃ w => ipcBufOfA b x = none → w = 0#u64 ⦄ := + fun sz p hn => WP.spec_mono (frame_off_okA sz p wv) fun _ _ h => absurd h hn + have hNativeT := hNative t htT + rw [capAtA_eq] at hNativeT hab + generalize Fl4ProofsA64.P1.W7.R.ST.capA st s (slotAddrA (specPtr t) (BitVec.ofNat 64 2)) = c at hab hNativeT ⊢ + cases c with + | Frame p sz r dev m ept asid va => + have hept : ept = false := hNativeT + subst ept + replace hab : absCapA (.Frame p sz r dev m false asid va) = x.tcbIpcframe := hab + simp only [absCapA, absFrameE, Bool.false_eq_true, ↓reduceIte] at hab + cases dev with + | true => simp only [↓reduceIte]; exact hz + | false => + simp only [Bool.false_eq_true, ↓reduceIte] + have hsp : ipcBufOfA b x = if Fl4SpecA64.vmReadWrite ⊆ vmRightsOfA r ∨ + Fl4SpecA64.vmReadOnly ⊆ vmRightsOfA r ∧ ¬ b = true then some (specPtr p + (x.tcbIpcBuffer &&& + Fl4SpecA64.mask (Fl4SpecA64.pageBitsForSize (frameSizeOfA sz)))) else none := by + unfold ipcBufOfA; rw [← hab] + by_cases h3 : r = capability.VM_READ_WRITE + · rw [if_pos h3] + have hr3 : r.val = 3 := by rw [h3]; unfold capability.VM_READ_WRITE; rfl + exact hsm sz p (by rw [hsp, if_pos (Or.inl (vmRightsOfA_rw hr3))]; simp) + · rw [if_neg h3] + cases b with + | true => simp only [↓reduceIte]; exact hz + | false => + simp only [Bool.false_eq_true, ↓reduceIte] + by_cases h2 : r = capability.VM_READ_ONLY + · rw [if_pos h2] + have hr2 : r.val = 2 := by rw [h2]; unfold capability.VM_READ_ONLY; rfl + exact hsm sz p (by rw [hsp, if_pos (Or.inr ⟨vmRightsOfA_ro (Or.inl hr2), by simp⟩)]; simp) + · rw [if_neg h2]; exact hz + | _ => exact hz + +end Lookup + +section Cover +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif +variable {S : Type} (st : machine.Store S) [L : LawfulStore st] + +/-- **The tracked buffers cover the IPC buffers** (x86 `IpcBuf.BufCover`; a ghost-set placement fact of `Bf`, as +`EPlaced`): for a TCB of `T` whose spec buffer (for `b`) is `sb`, the word FL4's lookup returns is tracked with it and +is not 0. -/ +def BufCover (T : Set U64) (Bf : Set (U64 × Fl4SpecA64.Word)) (s : S) (σ : Fl4SpecA64.State Unit) : Prop := + ∀ t ∈ T, ∀ x b sb w, σ.kheap (specPtr t) = some (.tcb x) → ipcBufOfA b x = some sb → + msg.lookup_ipc_buffer st s b t = ok w → (w, sb) ∈ Bf ∧ w ≠ 0#u64 + +variable {st} + +/-- **`lookup_buf_full`** (x86's): FL4's buffer word is 0 exactly when the spec finds no buffer, and otherwise tracked +with the spec's buffer. -/ +theorem lookup_buf_full {th : TypedHeapA} {T : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} (s : S) + (σ : Fl4SpecA64.State Unit) (hR : StateRelA th (L.mem s) σ) {t : U64} (ht : th t = some .tcb) (htT : t ∈ T) + {x : Fl4SpecA64.Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) (hC : BufCover st T Bf s σ) (hNative : BufNativeA T (L.mem s)) (b : Bool) : + msg.lookup_ipc_buffer st s b t ⦃ w => (w = 0#u64 ↔ ipcBufOfA b x = none) ∧ + ∀ sb, ipcBufOfA b x = some sb → (w, sb) ∈ Bf ⦄ := by + obtain ⟨w, hw, hP⟩ := spec_existsA (lookup_buf_none s σ hR hNative ht htT hk b) + rw [hw, WP.spec_ok] + cases ho : ipcBufOfA b x with + | none => exact ⟨⟨fun _ => rfl, fun _ => hP ho⟩, fun _ h => by cases h⟩ + | some sb => + obtain ⟨hm, h0⟩ := hC t htT x b sb w hk ho hw + exact ⟨⟨fun e => absurd e h0, fun e => by cases e⟩, fun sb' e => by cases e; exact hm⟩ + +/-- **`BufViewA` from the endpoint relation** (FrontEndExtraA's decode-site view, gen 2 open item): at FL4's lookup +of a TCB of `T` and the spec's (`lookupIpcBuffer_eqA`). -/ +theorem lookup_buf_view {th : TypedHeapA} {T E R : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} (s : S) + (σ : Fl4SpecA64.State Unit) (hR : StateRelA th (L.mem s) σ) (hE : EpStateRel T E R Bf (L.mem s) σ) {t : U64} + (ht : th t = some .tcb) (htT : t ∈ T) {x : Fl4SpecA64.Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hC : BufCover st T Bf s σ) (hNative : BufNativeA T (L.mem s)) (b : Bool) : + msg.lookup_ipc_buffer st s b t ⦃ w => BufViewA (L.mem s) σ.machineState.underlyingMemory w (ipcBufOfA b x) ⦄ := by + refine WP.spec_mono (lookup_buf_full s σ hR ht htT hk hC hNative b) fun w ⟨hz, hm⟩ => ?_ + cases ho : ipcBufOfA b x with + | none => rw [hz.2 ho]; exact bufViewA_none _ _ + | some sb => exact hE.bufView (hm sb ho) (fun e => by have := hz.1 e; rw [ho] at this; cases this) + +end Cover + +end + +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/EpCancelA.lean b/proofs/Fl4ProofsA64/P3/EpCancelA.lean new file mode 100644 index 000000000..68c921747 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/EpCancelA.lean @@ -0,0 +1,814 @@ +/- +P3 (aarch64, A64-P3-IPC gen 3): **`cancel_badged_sends` refines `Fl4SpecA64.cancelBadgedSends`** (`Z = Unit`; the +CNode CancelBadgedSends invocation's IPC step, CNode label 26) over `EpStateRel` and the endpoint scheduler interface +`SchedOpsEA` (`P3/SchedOpsA`). FL4's AArch64 `cancel_badged_sends`, its loop, `restart_thread_if_no_fault`, +`set_thread_state` and `ep_dequeue` are the x86 text (extraction diffed). Ported from x86: + +* `P3/EpSched` (`write_state_rel`, `set_thread_state_rel`, `TcbRelE.with_state`, `KView.stsAct'`, `tsCode_four`); +* `P3/EpDeq` (`ep_dequeue_rel`: `ep_dequeue` of a queued thread refines `tcb_ep_dequeue`, single outcome, no TCB + field changed; `dequeued_not_queued`, `epDeq_not_mem`); +* `P3/IpcCancelBadged`'s loop on `EpStateRel ∧ SR` instead of x86's `IRel`: `restart_rel` + (`restart_thread_if_no_fault`: the fault word read against `tcbFault`, the state word, then + `SchedOpsEA.restartTail`; or `Inactive` for a faulted thread), `badged_loop` (by the queue still to visit, the + spec's outcomes from it included in the whole's, `mapMx`; FL4's fuel `WALK_FUEL` exceeds any queue of distinct TCB + addresses), **`cancel_badged_rel`** (then `SchedOpsEA.reschedule`). + +The scheduler facts x86 carried in its loop invariant (`ScBack`, `ActReady`, `MemOKE` of the queued threads) are +`SR`'s business here (they are what `SchedOpsEA.restartTail` / `.reschedule` need). Result: every FL4 outcome is +related (`EpStateRel ∧ SR`) to one of the spec's (as x86's `cancel_badged_irel`). Hypotheses: `EPlaced`, `e ∈ E`, the +interface; no named premise. + +(gen 4, ruling item 1) **The spec does not fail**: `restart_*` / `restart_rel` also give `¬ failed` of +`restart_thread_if_no_fault` (from `SchedOpsEA.restartTail`'s, or the faulted branch's single outcome); `BInv` carries +"the whole loop fails only if the rest from here does" (`mapMx_cons_fail`: each step has one outcome, `P3/SpecDetA`'s +`det_removeAndRestartBadged`, and does not fail; `rrb_deq_eq` / `rrb_keep_eq`), so `badged_loop` and +**`cancel_badged_rel`** state `¬ (cancelBadgedSends …).2` (the reschedule's by `Det.detAt_bind_fail`). + +(gen 5, ruling Q1) `SchedOpsEA.stable` takes `NoBlockE`: `set_thread_state_rel` takes the step's `NoBlockE` (`hNB`), +`ep_dequeue_rel` gives it by `NoBlockE.updO` (no TCB changes), and `restart_spine` / `restart_rel` take the restarted +thread's state blocked on an endpoint (`hblk : inClass ∧ ¬ runnable`, from the queue): `Restart` without a fault is +`NoBlockE`'s branch 2 (`inClassR` with no fault), `Inactive` with one its branch 3. For `cancel_ipc` +(`P3/EpCancelIpcA`): `set_thread_state_from` (one `stable` step for endpoint writes since an earlier state; +`set_thread_state_rel` is its instance) and `ep_dequeue_raw` (`ep_dequeue_rel` without `SR`: its writes and spec step). + +(gen 6, ruling (a)) `restart_spine` / **`restart_relO`** take `RestartOK` (not runnable, not idle, and without a +fault `inClassR` or `RelIfBound`) instead of `hblk`; `restart_rel` (statement unchanged) is `restart_relO` with +`RestartOK.of_blk`. `KeepO` also keeps the replies not pointing to the restarted thread (`KeepO.trans`, `.of_kview`, +`.updE`); `RelIfBound.keep`. + +(gen 7, ruling item 3) `restart_from_state` / `restart_cut` / `restart_relO` / `restart_rel` take the restarted +thread on the current core (`hon`, `SchedOpsEA.restartTail`'s precondition; carried across the state word by +`SchedOpsEA.coreKeep`), `BInv` carries it for the threads still to visit, and `cancel_badged_rel` takes it for +every thread queued on the endpoint. + +Kernel cost: the restart (`restart_spine` / `restart_cut` / `restart_cutI` / `restart_from_state`) and the loop's +dequeue step (`badged_deq`, `rrb_deq_mem`, `epDeq_send_rm`) are separate declarations: checked inside one proof +(as x86's), the kernel check ran away in memory (> 20 GiB). +-/ +import Fl4ProofsA64.P3.SchedOpsA +import Fl4ProofsA64.P3.EpRegsA +import Fl4ProofsA64.P3.SpecDetA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif + +noncomputable section + +section +open Fl4SpecA64 Fl4Spec +variable {Z : Type} + +theorem stsAct_ms (σ : State Z) (t : ObjRef) : (stsAct σ t).machineState = σ.machineState := by + unfold stsAct; split_ifs <;> rfl + +theorem KView.stsAct' (σ : State Z) (t : ObjRef) : KView σ (stsAct σ t) := by + refine ⟨fun p x h => ?_, fun p x h => ⟨x, ?_, rfl⟩, fun p x h => ⟨x, ?_, rfl, rfl, rfl, rfl, rfl⟩, by rw [stsAct_ms]⟩ <;> + rw [stsAct_kheap] <;> exact h + +theorem tsCode_four {ts : ThreadState} (h : tsCode ts = 4) : ∃ e d, ts = .blockedOnSend e d := by + rcases ts with _ | _ | _ | _ | _ | _ | _ | _ | ⟨⟨⟩⟩ <;> simp [tsCode] at h ⊢ + +theorem ep_state_vals : obj.EP_IDLE = 0#u64 ∧ obj.EP_SEND = 1#u64 ∧ obj.EP_RECV = 2#u64 := by + unfold obj.EP_IDLE obj.EP_SEND obj.EP_RECV; exact ⟨rfl, rfl, rfl⟩ + +end + +/-! ## The state word -/ + +section +variable {T E R : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + +/-- A related TCB with a new state whose payload the relation ignores, from memory that agrees on all of its fields +but the state word. -/ +theorem TcbRelE.with_state {m m' : U64 → U64} {t : U64} {x : Fl4SpecA64.Tcb} (ht : TcbAddr t) + (h : TcbRelE R m t x) (ts : Fl4SpecA64.ThreadState) (hts : tsCode ts < 3) + (hst : (m' (stA t)).val = tsCode ts) (hA : Agree m m' (fun b => TF t b ∧ b.val ≠ (stA t).val)) : + TcbRelE R m' t { x with tcbState := ts } := by + have g : ∀ {b}, Aligned b ∧ TF t b → b.val ≠ (stA t).val → m' b = m b := fun hb hne => hA _ hb.1 ⟨hb.2, hne⟩ + have v := val_st ht + refine ⟨hst, fun e he => ?_, fun e d he => ?_, fun e r d he => ?_, fun r he => ?_, fun r => ?_, ?_, ?_, ?_, ?_, ?_⟩ + · rcases ts with _ | _ | _ | _ | _ | _ | _ | _ | ⟨⟨⟩⟩ <;> simp [tsCode, Fl4SpecA64.epBlocked] at hts he + · subst he; simp [tsCode] at hts + · subst he; simp [tsCode] at hts + · subst he; simp [tsCode] at hts + · rw [g (a_reg ht r) (by rw [regA_val ht (regIdxA_lt r), v]; have := regIdxA_lt r; omega)]; exact h.regs r + · rw [g (a_prio ht) (by rw [val_prio ht, v]; omega)]; exact h.prio + · rw [g (a_fault ht) (by rw [val_fault ht, v]; omega)]; exact h.fault0 + · rw [g (a_fault ht) (by rw [val_fault ht, v]; omega)]; exact h.faultT + · rw [g (a_bound ht) (by rw [val_bound ht, v]; omega)]; exact h.bound + · exact h.faultR.congr (g (a_fault ht) (by rw [val_fault ht, v]; omega)) (g (a_fw0 ht) (by rw [val_fw0 ht, v]; omega)) + (g (a_fw1 ht) (by rw [val_fw1 ht, v]; omega)) (g (a_lf ht) (by rw [val_lf ht, v]; omega)) + (g (a_lf0 ht) (by rw [val_lf0 ht, v]; omega)) (g (a_lf1 ht) (by rw [val_lf1 ht, v]; omega)) + (g (a_fw2 ht) (by rw [val_fw2 ht, v]; omega)) + +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {SR : S → Fl4SpecA64.State Unit → Prop} + +/-- Writing the state word of a TCB in no endpoint queue: related to the spec's TCB with the new (unblocked) +state. -/ +theorem write_state_rel (hP : EPlaced T E R Bf) (s : S) (σ : Fl4SpecA64.State Unit) + (hR : EpStateRel T E R Bf (L.mem s) σ) {t : U64} (ht : t ∈ T) {x : Fl4SpecA64.Tcb} + (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) + (ts : Fl4SpecA64.ThreadState) (hts : tsCode ts < 3) (code : U64) (hcode : code.val = tsCode ts) : + EpStateRel T E R Bf (L.mem (L.wr s (stA t) code)) (updK σ (specPtr t) (.tcb { x with tcbState := ts })) ∧ + WritesIn st s (L.wr s (stA t) code) (EWrite T E R Bf) ∧ + EpStep σ (updK σ (specPtr t) (.tcb { x with tcbState := ts })) := by + have hta := hP.tcb t ht + have hx := hR.at_tcb ht hk + have ha := a_st hta + have hag := agree_wr (L := L) (s := s) (v := code) ha.1 + refine ⟨hR.upd_tcb hP ht _ (hag.mono fun b hb => tf_ne_of hb.2 ha.2) (Or.inl hq) + (hx.with_state hta ts hts (by rw [rd_wr_eq ha.1]; exact hcode) (hag.mono fun b hb => hb.2)), ?_, + EpStep.tcb hk ts x.tcbArch⟩ + refine (writesIn_wr ha.1).mono fun b hb => ?_ + subst hb + refine Or.inr (Or.inr (Or.inl ⟨t, ht, Or.inl ⟨ha.2.1, ?_⟩⟩)) + rw [val_st hta]; unfold TWOffN; omega + +/-- (gen 5) Two endpoint write sets compose. -/ +theorem WritesIn.ew {s1 s2 s3 : S} (h1 : WritesIn st s1 s2 (EWrite T E R Bf)) (h2 : WritesIn st s2 s3 (EWrite T E R Bf)) : + WritesIn st s1 s3 (EWrite T E R Bf) := + (h1.trans h2).mono fun _ h => h.elim id id + +/-- (gen 5) **`set_thread_state t ts`** after endpoint writes / spec steps from `(s0, σ0)`, with one `stable` step for +the whole (ruling Q4b: `cancel_ipc`'s fault clear, dequeue and state write before `schedule_tcb`). -/ +theorem set_thread_state_from (hO : SchedOpsEA st T E R Bf SR) (hP : EPlaced T E R Bf) {s0 : S} + {σ0 : Fl4SpecA64.State Unit} (hSR0 : SR s0 σ0) (s : S) (σ : Fl4SpecA64.State Unit) + (hR : EpStateRel T E R Bf (L.mem s) σ) (hW0 : WritesIn st s0 s (EWrite T E R Bf)) (hS0 : EpStep σ0 σ) + {t : U64} (ht : t ∈ T) {x : Fl4SpecA64.Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) + (ts : Fl4SpecA64.ThreadState) (hts : tsCode ts < 3) (code : U64) (hcode : code.val = tsCode ts) + (hNB : NoBlockE σ0 (updK σ (specPtr t) (.tcb { x with tcbState := ts }))) : + sched.set_thread_state st s t code ⦃ s' => + EpStateRel T E R Bf (L.mem s') (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := ts })) (specPtr t)) ∧ + SR s' (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := ts })) (specPtr t)) ⦄ := by + unfold sched.set_thread_state + step with obj.Tcb.set_state.spec as ⟨s1, hs1⟩ + subst hs1 + obtain ⟨hR1, hW1, hS1⟩ := write_state_rel hP s σ hR ht hk hq ts hts code hcode + refine WP.spec_mono (hO.scheduleTcb _ _ t hR1 (hO.stable _ _ _ _ hSR0 (WritesIn.ew hW0 hW1) (hS0.trans hS1) hNB) ht) ?_ + rintro s2 ⟨hf, hS⟩ + exact ⟨hR1.frame hP hf (KView.stsAct' _ _), hS⟩ + +/-- **`set_thread_state t ts`** for a thread in no endpoint queue and a state that is `Inactive`, `Running` or +`Restart`. -/ +theorem set_thread_state_rel (hO : SchedOpsEA st T E R Bf SR) (hP : EPlaced T E R Bf) (s : S) + (σ : Fl4SpecA64.State Unit) (hR : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) {t : U64} (ht : t ∈ T) + {x : Fl4SpecA64.Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) + (ts : Fl4SpecA64.ThreadState) (hts : tsCode ts < 3) (code : U64) (hcode : code.val = tsCode ts) + (hNB : NoBlockE σ (updK σ (specPtr t) (.tcb { x with tcbState := ts }))) : + sched.set_thread_state st s t code ⦃ s' => + EpStateRel T E R Bf (L.mem s') (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := ts })) (specPtr t)) ∧ + SR s' (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := ts })) (specPtr t)) ⦄ := + set_thread_state_from hO hP hSR s σ hR (WritesIn.refl _ _) (EpStep.refl _) ht hk hq ts hts code hcode hNB + +/-! ## The dequeue -/ + +/-- The TCB fields of all TCBs. -/ +def AllTF (T : Set U64) (b : U64) : Prop := ∃ y ∈ T, TF y b + +/-- (gen 5) `ep_dequeue` of a queued thread against `tcb_ep_dequeue`, its writes and spec step (no `SR`). -/ +theorem ep_dequeue_raw (hP : EPlaced T E R Bf) (s : S) (σ : Fl4SpecA64.State Unit) + (hR : EpStateRel T E R Bf (L.mem s) σ) {e : U64} (he : e ∈ E) {x : Fl4SpecA64.Endpoint} + (hk : σ.kheap (specPtr e) = some (.endpoint x)) {d : U64} (hd : specPtr d ∈ x.epQueue) : + endpoint.ep_dequeue st s e d ⦃ s' => + Fl4SpecA64.tcbEpDequeue (Z := Unit) (specPtr d) (specPtr e) σ = + ({((), updK σ (specPtr e) (.endpoint (epDeq x (specPtr d))))}, False) ∧ + EpStateRel T E R Bf (L.mem s') (updK σ (specPtr e) (.endpoint (epDeq x (specPtr d)))) ∧ + WritesIn st s s' (EWrite T E R Bf) ∧ EpStep σ (updK σ (specPtr e) (.endpoint (epDeq x (specPtr d)))) ∧ + Agree (L.mem s) (L.mem s') (AllTF T) ⦄ := by + have hx := hR.at_ep he hk + obtain ⟨xs, hQ, hxs, hB⟩ := hx.queue + have hdx : d ∈ xs := by + rw [hxs, List.mem_map] at hd + obtain ⟨y, hy, ey⟩ := hd + rw [← specPtr_inj ey]; exact hy + refine WP.spec_mono (ep_dequeue_full hP s he d xs hQ hdx) ?_ + rintro s' ⟨hQ', hst', hA, hW, -, -⟩ + have hfield : Agree (L.mem s) (L.mem s') (AllTF T) := by + intro b hb ⟨y, hy, hf⟩ + refine hA b hb ⟨fun hw => ?_, fun ⟨w, hw, hl⟩ => ?_⟩ + · have := hf.range; have := hP.et e he y hy; unfold EW at hw; omega + · have hwT := hQ.mem w hw + by_cases ew : w = y + · subst ew; exact hl.not_tf hf + · exact notTF_tcb hP hy hwT ew (Or.inr hl) hf + refine ⟨tcbEpDequeue_eq hk hd, ?_, hW, EpStep.ep hk, hfield⟩ + refine hR.upd_ep hP he _ (hA.mono fun b ⟨h1, h2, h3⟩ => ⟨h2, fun ⟨w, hw, hl⟩ => h3 ⟨w, hQ.mem w hw, (hB w hw).1, hl⟩⟩) ?_ + have hrm : Fl4SpecA64.removeAll (specPtr d) x.epQueue = (xs.erase d).map specPtr := by + rw [hxs]; exact removeAll_map hQ.nodup d + have hmem : ∀ y ∈ xs.erase d, L.mem s' (stA y) = L.mem s (stA y) ∧ L.mem s' (tBlockA y) = L.mem s (tBlockA y) := by + intro y hy + have hy' := (List.erase_subset hy) + have hya := hP.tcb y (hQ.mem y hy') + have nst : ¬ EW e (stA y) ∧ ¬ ∃ w ∈ xs, TL w (stA y) := by + refine ⟨fun hw => ?_, fun ⟨w, hw, hl⟩ => ?_⟩ + · have := hP.et e he y (hQ.mem y hy'); unfold EW at hw; rw [val_st hya] at hw; omega + · by_cases ew : w = y + · subst ew; exact hl.not_tf (a_st hya).2 + · have := tcb_disj hP (hQ.mem w hw) (hQ.mem y hy') (Ne.symm ew); unfold TL at hl; rw [val_st hya] at hl; omega + have nbl : ¬ EW e (tBlockA y) ∧ ¬ ∃ w ∈ xs, TL w (tBlockA y) := by + refine ⟨fun hw => ?_, fun ⟨w, hw, hl⟩ => ?_⟩ + · have := hP.et e he y (hQ.mem y hy'); unfold EW at hw; rw [val_blk hya] at hw; omega + · by_cases ew : w = y + · subst ew; exact hl.not_tf (a_blk hya).2 + · have := tcb_disj hP (hQ.mem w hw) (hQ.mem y hy') (Ne.symm ew); unfold TL at hl; rw [val_blk hya] at hl; omega + exact ⟨hA _ (a_st hya).1 nst, hA _ (a_blk hya).1 nbl⟩ + unfold epDeq + rw [hrm] + by_cases hemp : xs.erase d = [] + · rw [hemp] + refine ⟨by rw [hst', if_pos hemp], ?_⟩ + rw [← hemp]; exact hQ' + · have hne : (xs.erase d).map specPtr ≠ [] := by simpa using hemp + obtain ⟨h, tl, hht⟩ := List.exists_cons_of_ne_nil hne + rw [hht] + simp only + rw [← hht] + cases x with + | idleEP => simp [Fl4SpecA64.Endpoint.epQueue] at hd + | sendEP q => + obtain ⟨hx1, xs', -, hq', hQ'', h3⟩ := hx + have e' : xs' = xs := hQ''.unique hP.tcb hQ + subst e' + refine ⟨by rw [hst', if_neg hemp]; exact hx1, xs'.erase d, hemp, rfl, hQ', fun y hy => ?_⟩ + obtain ⟨c1, c2⟩ := h3 y (List.erase_subset hy) + exact ⟨by rw [(hmem y hy).1]; exact c1, by rw [(hmem y hy).2]; exact c2⟩ + | recvEP q => + obtain ⟨hx1, xs', -, hq', hQ'', h3⟩ := hx + have e' : xs' = xs := hQ''.unique hP.tcb hQ + subst e' + refine ⟨by rw [hst', if_neg hemp]; exact hx1, xs'.erase d, hemp, rfl, hQ', fun y hy => ?_⟩ + obtain ⟨c1, c2⟩ := h3 y (List.erase_subset hy) + exact ⟨by rw [(hmem y hy).1]; exact c1, by rw [(hmem y hy).2]; exact c2⟩ + +/-- **`ep_dequeue` of a queued thread refines `tcb_ep_dequeue`** (x86 `P3/EpDeq`). -/ +theorem ep_dequeue_rel (hO : SchedOpsEA st T E R Bf SR) (hP : EPlaced T E R Bf) (s : S) (σ : Fl4SpecA64.State Unit) + (hR : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) {e : U64} (he : e ∈ E) {x : Fl4SpecA64.Endpoint} + (hk : σ.kheap (specPtr e) = some (.endpoint x)) {d : U64} (hd : specPtr d ∈ x.epQueue) : + endpoint.ep_dequeue st s e d ⦃ s' => + Fl4SpecA64.tcbEpDequeue (Z := Unit) (specPtr d) (specPtr e) σ = + ({((), updK σ (specPtr e) (.endpoint (epDeq x (specPtr d))))}, False) ∧ + EpStateRel T E R Bf (L.mem s') (updK σ (specPtr e) (.endpoint (epDeq x (specPtr d)))) ∧ + SR s' (updK σ (specPtr e) (.endpoint (epDeq x (specPtr d)))) ∧ + Agree (L.mem s) (L.mem s') (AllTF T) ⦄ := + WP.spec_mono (ep_dequeue_raw hP s σ hR he hk hd) fun _ ⟨h1, h2, hW, hS, h5⟩ => + ⟨h1, h2, hO.stable _ _ _ _ hSR hW hS (NoBlockE.updO _ _ fun _ h => by cases h), h5⟩ + +/-- After its dequeue, a thread blocked on `e` is in no endpoint queue. -/ +theorem dequeued_not_queued {m : U64 → U64} {σ : Fl4SpecA64.State Unit} (hR : EpStateRel T E R Bf m σ) + {e d : U64} (he : e ∈ E) (hb : m (tBlockA d) = e) {x : Fl4SpecA64.Endpoint} + (hk : σ.kheap (specPtr e) = some (.endpoint x)) (hnd : specPtr d ∉ x.epQueue) : + ∀ e' ∈ E, ∀ z, σ.kheap (specPtr e') = some (.endpoint z) → specPtr d ∉ z.epQueue := by + intro e' he' z hz hm + obtain ⟨-, hb'⟩ := (hR.at_ep he' hz).blocked hm + have : e' = e := hb'.symm.trans hb + subst this + rw [hk] at hz; cases hz + exact hnd hm + +/-! ## The restart + +Split in declarations at the `stsAct` state `set_thread_state` leaves (as `wake_spine` in `P3/NtfnWakeA`): checked as +one term, such proofs ran away in the kernel. -/ + +/-- The spec's restart, unfolded to its tail after the state. -/ +theorem restartThreadIfNoFault_eq {σ : Fl4SpecA64.State Unit} {t : Fl4SpecA64.ObjRef} {x : Fl4SpecA64.Tcb} + (hk : σ.kheap t = some (.tcb x)) (hf : x.tcbFault = none) : + Fl4SpecA64.restartThreadIfNoFault (Z := Unit) t σ = + specRestartTail t (stsAct (updK σ t (.tcb { x with tcbState := .restart })) t) := by + unfold Fl4SpecA64.restartThreadIfNoFault + rw [nd_bind_one (threadGet_eq _ hk)] + show (if x.tcbFault = none then (_ : Fl4SpecA64.SMonad Unit Unit) else _) σ = _ + rw [if_pos hf, nd_bind_one (setThreadState_eq .restart hk)] + rfl + +/-- The spec's restart of a faulted thread. -/ +theorem restartThreadIfNoFault_fault {σ : Fl4SpecA64.State Unit} {t : Fl4SpecA64.ObjRef} {x : Fl4SpecA64.Tcb} + (hk : σ.kheap t = some (.tcb x)) (hf : x.tcbFault ≠ none) : + Fl4SpecA64.restartThreadIfNoFault (Z := Unit) t σ = + ({((), stsAct (updK σ t (.tcb { x with tcbState := .inactive })) t)}, False) := by + unfold Fl4SpecA64.restartThreadIfNoFault + rw [nd_bind_one (threadGet_eq _ hk)] + simp only [hf, ↓reduceIte] + exact setThreadState_eq .inactive hk + +/-- (gen 4) The other TCBs keep their thread states (for `cancel_all_ipc`'s loop); (gen 6) and the replies not +pointing to `t` keep their TCB (for `RepOKA` of the threads still queued). -/ +def KeepO (σ σ' : Fl4SpecA64.State Unit) (t : Fl4SpecA64.ObjRef) : Prop := + (∀ p x, p ≠ t → σ.kheap p = some (.tcb x) → ∃ y, σ'.kheap p = some (.tcb y) ∧ y.tcbState = x.tcbState) ∧ + (∀ p x, σ.kheap p = some (.reply x) → x.replyTcb ≠ some t → + ∃ y, σ'.kheap p = some (.reply y) ∧ y.replyTcb = x.replyTcb) + +theorem KeepO.trans {σ1 σ2 σ3 : Fl4SpecA64.State Unit} {t : Fl4SpecA64.ObjRef} (h1 : KeepO σ1 σ2 t) + (h2 : KeepO σ2 σ3 t) : KeepO σ1 σ3 t := by + refine ⟨fun p x hp hx => ?_, fun p x hx hn => ?_⟩ + · obtain ⟨y, hy, e1⟩ := h1.1 p x hp hx + obtain ⟨z, hz, e2⟩ := h2.1 p y hp hy + exact ⟨z, hz, e2.trans e1⟩ + · obtain ⟨y, hy, e1⟩ := h1.2 p x hx hn + obtain ⟨z, hz, e2⟩ := h2.2 p y hy (by rw [e1]; exact hn) + exact ⟨z, hz, e2.trans e1⟩ + +theorem KeepO.of_kview {σ σ' : Fl4SpecA64.State Unit} (h : KView σ σ') (t : Fl4SpecA64.ObjRef) : KeepO σ σ' t := + ⟨fun p x _ hx => by obtain ⟨y, hy, e, -⟩ := h.2.2.1 p x hx; exact ⟨y, hy, e⟩, + fun p x hx _ => h.2.1 p x hx⟩ + +/-- (gen 6) Writing an endpoint keeps the TCBs and replies. -/ +theorem KeepO.updE {σ : Fl4SpecA64.State Unit} {q : Fl4SpecA64.ObjRef} {z : Fl4SpecA64.Endpoint} + (hq : σ.kheap q = some (.endpoint z)) (z' : Fl4SpecA64.Endpoint) (t : Fl4SpecA64.ObjRef) : + KeepO σ (updK σ q (.endpoint z')) t := by + refine ⟨fun p x _ hx => ⟨x, ?_, rfl⟩, fun p x hx _ => ⟨x, ?_, rfl⟩⟩ + · have hpq : p ≠ q := fun e => by rw [e, hq] at hx; cases hx + rw [updK_kheap, Function.update_of_ne hpq, hx] + · have hpq : p ≠ q := fun e => by rw [e, hq] at hx; cases hx + rw [updK_kheap, Function.update_of_ne hpq, hx] + +/-- (gen 6) `RelIfBound` only reads the TCB's SC pointer, the SC objects, the release queue and the time. -/ +theorem RelIfBound.keep {σ σ' : Fl4SpecA64.State Unit} {p : Fl4SpecA64.ObjRef} (h : RelIfBound σ p) + (hrq : σ'.releaseQueue = σ.releaseQueue) (hct : σ'.curTime = σ.curTime) + (hp : ∀ x', σ'.kheap p = some (.tcb x') → ∃ x, σ.kheap p = some (.tcb x) ∧ + x.tcbSchedContext = x'.tcbSchedContext) + (hsc : ∀ q y n, σ'.kheap q = some (.schedContext y n) → σ.kheap q = some (.schedContext y n)) : + RelIfBound σ' p := by + intro x' scp y n hx' hs hy hact hnr + obtain ⟨x, hx, e⟩ := hp x' hx' + have := h x scp y n hx (e.trans hs) (hsc _ _ _ hy) hact (by rw [← hrq]; exact hnr) + unfold released at this ⊢ + rw [hct]; exact this + +/-- (gen 6, ruling (a)) **What the restart of `restart_thread_if_no_fault` needs of the thread**: not runnable, not +idle (the faulted branch: `NoBlockE`'s branch 3), and, without a fault, `inClassR` or a released SC (branch 2). An +endpoint-blocked thread has it (`RestartOK.of_blk`); so has a receiver that `reply_unlink` made `Inactive`, from +`SchedOpsEA.rd` before the unlink (`P3/EpCancelAllA`). -/ +def RestartOK (σ : Fl4SpecA64.State Unit) (p : Fl4SpecA64.ObjRef) (x : Fl4SpecA64.Tcb) : Prop := + Fl4SpecA64.runnable x.tcbState = false ∧ x.tcbState ≠ .idleThreadState ∧ + (x.tcbFault = none → inClassR x.tcbState x.tcbFault ∨ RelIfBound σ p) + +theorem RestartOK.of_blk {σ : Fl4SpecA64.State Unit} {p : Fl4SpecA64.ObjRef} {x : Fl4SpecA64.Tcb} + (h : inClass x.tcbState ∧ Fl4SpecA64.runnable x.tcbState = false) : RestartOK σ p x := + ⟨h.2, h.1.ne_idle h.2, fun hf => Or.inl (inClassR_nf h.1 fun b e => by rw [hf] at e; cases e)⟩ + +/-- The restart tail from the state `σ1` that `set_thread_state t Restart` leaves. -/ +theorem restart_from_state (hO : SchedOpsEA st T E R Bf SR) (hP : EPlaced T E R Bf) (s1 : S) + (σ σ1 : Fl4SpecA64.State Unit) (hR1 : EpStateRel T E R Bf (L.mem s1) σ1) (hS1 : SR s1 σ1) {t : U64} (ht : t ∈ T) + {x : Fl4SpecA64.Tcb} (hk1 : σ1.kheap (specPtr t) = some (.tcb { x with tcbState := .restart })) + (hq1 : ∀ e ∈ E, ∀ z, σ1.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) + (hkeep : ∀ p y, σ.kheap p = some (.endpoint y) → σ1.kheap p = some (.endpoint y)) + (hkT : KeepO σ σ1 (specPtr t)) (hon : L.mem s1 (affA t) = L.cpu s1) (hNQ : NotQueued σ1 (specPtr t)) : + rustRestartTail st s1 t ⦃ s' => ¬ (specRestartTail (specPtr t) σ1).2 ∧ + ∃ σ', ((), σ') ∈ (specRestartTail (specPtr t) σ1).1 ∧ + EpStateRel T E R Bf (L.mem s') σ' ∧ SR s' σ' ∧ + (∀ p y, σ.kheap p = some (.endpoint y) → σ'.kheap p = some (.endpoint y)) ∧ KeepO σ σ' (specPtr t) ⦄ := by + refine WP.spec_mono (hO.restartTail s1 σ1 t _ hR1 hS1 ht hk1 (Or.inr rfl) hq1 hon hNQ) ?_ + rintro s2 ⟨hnf, σ', hm, hf2, hv, hS2⟩ + exact ⟨hnf, σ', hm, hR1.frame hP hf2 hv, hS2, fun p y hy => hv.1 _ _ (hkeep p y hy), hkT.trans (KeepO.of_kview hv _)⟩ + +/-- The facts at the state `set_thread_state t ts` leaves, for a TCB in no endpoint queue. -/ +theorem sts_facts {σ : Fl4SpecA64.State Unit} {t : U64} {x : Fl4SpecA64.Tcb} + (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) + (ts : Fl4SpecA64.ThreadState) : + (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := ts })) (specPtr t)).kheap (specPtr t) = + some (.tcb { x with tcbState := ts }) ∧ + (∀ e ∈ E, ∀ z, (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := ts })) (specPtr t)).kheap (specPtr e) = + some (.endpoint z) → specPtr t ∉ z.epQueue) ∧ + (∀ p y, σ.kheap p = some (.endpoint y) → + (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := ts })) (specPtr t)).kheap p = some (.endpoint y)) ∧ + KeepO σ (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := ts })) (specPtr t)) (specPtr t) := by + have hkp : ∀ p y, σ.kheap p = some (.endpoint y) → p ≠ specPtr t := fun p y hy e => by + rw [e, hk] at hy; cases hy + refine ⟨by rw [stsAct_kheap, updK_kheap, Function.update_self], fun e he z hz => ?_, fun p y hy => ?_, + fun p z hp hz => ⟨z, by rw [stsAct_kheap, updK_kheap, Function.update_of_ne hp, hz], rfl⟩, + fun p z hz _ => ⟨z, ?_, rfl⟩⟩ + · rw [stsAct_kheap, updK_kheap] at hz + by_cases hne : specPtr e = specPtr t + · rw [hne, Function.update_self] at hz; cases hz + · rw [Function.update_of_ne hne] at hz; exact hq e he z hz + · rw [stsAct_kheap, updK_kheap, Function.update_of_ne (hkp p y hy), hy] + · have hp : p ≠ specPtr t := fun e => by rw [e, hk] at hz; cases hz + rw [stsAct_kheap, updK_kheap, Function.update_of_ne hp, hz] + +/-- The restart up to the state word, the rest a parameter. -/ +theorem restart_spine (hO : SchedOpsEA st T E R Bf SR) (hP : EPlaced T E R Bf) (s : S) (σ : Fl4SpecA64.State Unit) + (hR : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) {t : U64} (ht : t ∈ T) {x : Fl4SpecA64.Tcb} + (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) + (hblk : RestartOK σ (specPtr t) x) + (hcut : x.tcbFault = none → ∀ s1, EpStateRel T E R Bf (L.mem s1) + (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := .restart })) (specPtr t)) → + SR s1 (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := .restart })) (specPtr t)) → + rustRestartTail st s1 t ⦃ s' => ¬ (Fl4SpecA64.restartThreadIfNoFault (Z := Unit) (specPtr t) σ).2 ∧ + ∃ σ', ((), σ') ∈ (Fl4SpecA64.restartThreadIfNoFault (Z := Unit) (specPtr t) σ).1 ∧ + EpStateRel T E R Bf (L.mem s') σ' ∧ SR s' σ' ∧ + (∀ p y, σ.kheap p = some (.endpoint y) → σ'.kheap p = some (.endpoint y)) ∧ KeepO σ σ' (specPtr t) ⦄) + (hcutI : x.tcbFault ≠ none → ∀ s1, EpStateRel T E R Bf (L.mem s1) + (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := .inactive })) (specPtr t)) → + SR s1 (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := .inactive })) (specPtr t)) → + ¬ (Fl4SpecA64.restartThreadIfNoFault (Z := Unit) (specPtr t) σ).2 ∧ ∃ σ', ((), σ') ∈ (Fl4SpecA64.restartThreadIfNoFault (Z := Unit) (specPtr t) σ).1 ∧ + EpStateRel T E R Bf (L.mem s1) σ' ∧ SR s1 σ' ∧ + (∀ p y, σ.kheap p = some (.endpoint y) → σ'.kheap p = some (.endpoint y)) ∧ KeepO σ σ' (specPtr t)) : + endpoint.restart_thread_if_no_fault st s t ⦃ s' => ¬ (Fl4SpecA64.restartThreadIfNoFault (Z := Unit) (specPtr t) σ).2 ∧ + ∃ σ', ((), σ') ∈ (Fl4SpecA64.restartThreadIfNoFault (Z := Unit) (specPtr t) σ).1 ∧ + EpStateRel T E R Bf (L.mem s') σ' ∧ SR s' σ' ∧ + (∀ p y, σ.kheap p = some (.endpoint y) → σ'.kheap p = some (.endpoint y)) ∧ KeepO σ σ' (specPtr t) ⦄ := by + have hx := hR.at_tcb ht hk + unfold endpoint.restart_thread_if_no_fault + step with obj.Tcb.fault_type.spec as ⟨i, hi⟩ + by_cases h0 : i = fl4_abi.consts.fault.NullFault + · rw [if_pos h0] + have hf : x.tcbFault = none := hx.fault0.1 (by rw [← hi, h0]; unfold fl4_abi.consts.fault.NullFault; rfl) + have hcode : sched.TS_RESTART.val = tsCode .restart := by unfold sched.TS_RESTART; rfl + refine WP.spec_bind (set_thread_state_rel hO hP s σ hR hSR ht hk hq .restart (by decide) sched.TS_RESTART + hcode (NoBlockE.updT hk _ (Or.inr (Or.inl ⟨rfl, hblk.2.1, hblk.2.2 hf⟩)))) ?_ + rintro s1 ⟨hR1, hS1⟩ + exact hcut hf s1 hR1 hS1 + · rw [if_neg h0] + have hf : x.tcbFault ≠ none := fun e => h0 (by rw [hi, hx.fault0.2 e]; unfold fl4_abi.consts.fault.NullFault; rfl) + have hcode : sched.TS_INACTIVE.val = tsCode .inactive := by unfold sched.TS_INACTIVE; rfl + refine WP.spec_mono (set_thread_state_rel hO hP s σ hR hSR ht hk hq .inactive (by decide) sched.TS_INACTIVE + hcode (NoBlockE.updT hk _ (Or.inr (Or.inr ⟨rfl, hblk.1, hblk.2.1⟩)))) ?_ + rintro s1 ⟨hR1, hS1⟩ + exact hcutI hf s1 hR1 hS1 + +/-- The no-fault branch after the state word. -/ +theorem restart_cut (hO : SchedOpsEA st T E R Bf SR) (hP : EPlaced T E R Bf) {σ : Fl4SpecA64.State Unit} {t : U64} + (ht : t ∈ T) {x : Fl4SpecA64.Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) + {s0 : S} (hSR0 : SR s0 σ) (hon : L.mem s0 (affA t) = L.cpu s0) + (hnr : Fl4SpecA64.runnable x.tcbState = false) (hf : x.tcbFault = none) (s1 : S) (hR1 : EpStateRel T E R Bf (L.mem s1) + (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := .restart })) (specPtr t))) + (hS1 : SR s1 (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := .restart })) (specPtr t))) : + rustRestartTail st s1 t ⦃ s' => ¬ (Fl4SpecA64.restartThreadIfNoFault (Z := Unit) (specPtr t) σ).2 ∧ + ∃ σ', ((), σ') ∈ (Fl4SpecA64.restartThreadIfNoFault (Z := Unit) (specPtr t) σ).1 ∧ + EpStateRel T E R Bf (L.mem s') σ' ∧ SR s' σ' ∧ + (∀ p y, σ.kheap p = some (.endpoint y) → σ'.kheap p = some (.endpoint y)) ∧ KeepO σ σ' (specPtr t) ⦄ := by + obtain ⟨hk1, hq1, hkeep, hkT⟩ := sts_facts hk hq .restart + rw [restartThreadIfNoFault_eq hk hf] + exact restart_from_state hO hP s1 σ _ hR1 hS1 ht hk1 hq1 hkeep hkT ((hO.coreKeep _ _ _ _ hSR0 hS1).onCore ht hon) + ((hO.notQueued s0 σ t x hSR0 hk hnr).sts _ _ _) + +/-- The faulted branch after the state word. -/ +theorem restart_cutI {σ : Fl4SpecA64.State Unit} {t : U64} {x : Fl4SpecA64.Tcb} + (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) + (hf : x.tcbFault ≠ none) (s1 : S) (hR1 : EpStateRel T E R Bf (L.mem s1) + (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := .inactive })) (specPtr t))) + (hS1 : SR s1 (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := .inactive })) (specPtr t))) : + ¬ (Fl4SpecA64.restartThreadIfNoFault (Z := Unit) (specPtr t) σ).2 ∧ ∃ σ', ((), σ') ∈ (Fl4SpecA64.restartThreadIfNoFault (Z := Unit) (specPtr t) σ).1 ∧ + EpStateRel T E R Bf (L.mem s1) σ' ∧ SR s1 σ' ∧ + (∀ p y, σ.kheap p = some (.endpoint y) → σ'.kheap p = some (.endpoint y)) ∧ KeepO σ σ' (specPtr t) := by + obtain ⟨-, -, hkeep, hkT⟩ := sts_facts hk hq .inactive + rw [restartThreadIfNoFault_fault hk hf] + exact ⟨fun h => h, _, rfl, hR1, hS1, hkeep, hkT⟩ + +/-- (gen 6) **`restart_thread_if_no_fault`** of a TCB in no endpoint queue with `RestartOK` refines +`restart_thread_if_no_fault`, keeping the endpoints (and `KeepO`). -/ +theorem restart_relO (hO : SchedOpsEA st T E R Bf SR) (hP : EPlaced T E R Bf) (s : S) (σ : Fl4SpecA64.State Unit) + (hR : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) {t : U64} (ht : t ∈ T) {x : Fl4SpecA64.Tcb} + (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) + (hblk : RestartOK σ (specPtr t) x) (hon : L.mem s (affA t) = L.cpu s) : + endpoint.restart_thread_if_no_fault st s t ⦃ s' => ¬ (Fl4SpecA64.restartThreadIfNoFault (Z := Unit) (specPtr t) σ).2 ∧ + ∃ σ', ((), σ') ∈ (Fl4SpecA64.restartThreadIfNoFault (Z := Unit) (specPtr t) σ).1 ∧ + EpStateRel T E R Bf (L.mem s') σ' ∧ SR s' σ' ∧ + (∀ p y, σ.kheap p = some (.endpoint y) → σ'.kheap p = some (.endpoint y)) ∧ KeepO σ σ' (specPtr t) ⦄ := + restart_spine hO hP s σ hR hSR ht hk hq hblk (restart_cut hO hP ht hk hq hSR hon hblk.1) (restart_cutI hk hq) + +/-- **`restart_thread_if_no_fault`** of a TCB in no endpoint queue refines `restart_thread_if_no_fault`, keeping the +endpoints. -/ +theorem restart_rel (hO : SchedOpsEA st T E R Bf SR) (hP : EPlaced T E R Bf) (s : S) (σ : Fl4SpecA64.State Unit) + (hR : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) {t : U64} (ht : t ∈ T) {x : Fl4SpecA64.Tcb} + (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) + (hblk : inClass x.tcbState ∧ Fl4SpecA64.runnable x.tcbState = false) (hon : L.mem s (affA t) = L.cpu s) : + endpoint.restart_thread_if_no_fault st s t ⦃ s' => ¬ (Fl4SpecA64.restartThreadIfNoFault (Z := Unit) (specPtr t) σ).2 ∧ + ∃ σ', ((), σ') ∈ (Fl4SpecA64.restartThreadIfNoFault (Z := Unit) (specPtr t) σ).1 ∧ + EpStateRel T E R Bf (L.mem s') σ' ∧ SR s' σ' ∧ + (∀ p y, σ.kheap p = some (.endpoint y) → σ'.kheap p = some (.endpoint y)) ∧ KeepO σ σ' (specPtr t) ⦄ := + restart_relO hO hP s σ hR hSR ht hk hq (RestartOK.of_blk hblk) hon + +end + +/-! ## The loop -/ + +section Loop +open Fl4SpecA64 in +/-- One step of the spec's loop. -/ +abbrev rrb (e b : U64) (t : ObjRef) : SMonad Unit Unit := removeAndRestartBadgedThread t (specPtr e) b.bv + +theorem mapMx_cons_mem {σ' σ'' : Fl4SpecA64.State Unit} {g : Fl4SpecA64.ObjRef → Fl4SpecA64.SMonad Unit Unit} + {a : Fl4SpecA64.ObjRef} {l : List Fl4SpecA64.ObjRef} (h : ((), σ'') ∈ (g a σ').1) : + ∀ z, z ∈ (Fl4Spec.NonDet.mapMx g l σ'').1 → z ∈ (Fl4Spec.NonDet.mapMx g (a :: l) σ').1 := + fun z hz => ⟨((), σ''), h, hz⟩ + +theorem epDeq_send_queue {y : Fl4SpecA64.Endpoint} (hnr : ∀ q, y ≠ .recvEP q) (t : Fl4SpecA64.ObjRef) : + (epDeq y t).epQueue = Fl4SpecA64.removeAll t y.epQueue ∧ ∀ q, epDeq y t ≠ .recvEP q := by + unfold epDeq + split + · rename_i h; exact ⟨by rw [h]; rfl, fun q e => by cases e⟩ + · cases y with + | idleEP => rename_i h; exact absurd rfl h + | sendEP q => exact ⟨rfl, fun q' e => by cases e⟩ + | recvEP q => exact absurd rfl (hnr q) + +theorem len_le_max (l : List U64) (hnd : l.Nodup) (h0 : ∀ x ∈ l, x ≠ 0#u64) : l.length ≤ 2 ^ 64 - 1 := by + have hnd' : (l.map (fun x : U64 => x.val)).Nodup := hnd.map (fun a b h => UScalar.eq_of_val_eq h) + calc l.length = (l.map (fun x : U64 => x.val)).toFinset.card := by + rw [List.toFinset_card_of_nodup hnd', List.length_map] + _ ≤ (Finset.Icc 1 (2 ^ 64 - 1)).card := by + apply Finset.card_le_card + intro v hv + simp only [List.mem_toFinset, List.mem_map] at hv + obtain ⟨x, hx, rfl⟩ := hv + have h1 : x.val ≠ 0 := fun e => h0 x hx (UScalar.eq_of_val_eq (by simpa using e)) + have h2 := x.hBounds + simp only [Finset.mem_Icc] + constructor <;> scalar_tac + _ = 2 ^ 64 - 1 := by simp + +end Loop + +section Loop2 +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {T E R : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {SR : S → Fl4SpecA64.State Unit → Prop} + +variable (st T E R Bf SR) in +/-- The loop's invariant: the queue still to visit `rest` (after the kept `kept`), FL4's cursor at its head, the +spec's outcomes from it among the whole loop's; (gen 7) the threads still to visit on the current core. -/ +def BInv (e b : U64) (σ0 : Fl4SpecA64.State Unit) (q0 : List Fl4SpecA64.ObjRef) (x : S × U64 × U64) : Prop := + ∃ σ' y, ∃ kept rest : List U64, EpStateRel T E R Bf (L.mem x.1) σ' ∧ SR x.1 σ' ∧ + σ'.kheap (specPtr e) = some (.endpoint y) ∧ + y.epQueue = (kept ++ rest).map specPtr ∧ (∀ q, y ≠ .recvEP q) ∧ x.2.1 = rest.headD 0#u64 ∧ + rest.length ≤ x.2.2.val ∧ (∀ t ∈ rest, t ∈ T → L.mem x.1 (affA t) = L.cpu x.1) ∧ + (∀ z, z ∈ (Fl4Spec.NonDet.mapMx (rrb e b) (rest.map specPtr) σ').1 → + z ∈ (Fl4Spec.NonDet.mapMx (rrb e b) q0 σ0).1) ∧ + ((Fl4Spec.NonDet.mapMx (rrb e b) q0 σ0).2 → (Fl4Spec.NonDet.mapMx (rrb e b) (rest.map specPtr) σ').2) + +/-- (gen 4) The whole loop fails only where its rest from a step's one outcome fails (`Det`). -/ +theorem mapMx_cons_fail {σ' σ'' : Fl4SpecA64.State Unit} {e b : U64} {a : Fl4SpecA64.ObjRef} + {l : List Fl4SpecA64.ObjRef} (h0 : ¬ (rrb e b a σ').2) (h : ((), σ'') ∈ (rrb e b a σ').1) : + (Fl4Spec.NonDet.mapMx (rrb e b) (a :: l) σ').2 → (Fl4Spec.NonDet.mapMx (rrb e b) l σ'').2 := + fun hf => Det.detAt_bind_fail ((Det.det_removeAndRestartBadged a _ _).at σ') h0 h hf + +/-- (gen 4) The spec's step on a matching thread, as the restart from the dequeued state. -/ +theorem rrb_deq_eq {σ' σd : Fl4SpecA64.State Unit} {t e : Fl4SpecA64.ObjRef} {bd : Fl4SpecA64.Badge} + {xt : Fl4SpecA64.Tcb} {d : Fl4SpecA64.SenderPayload} (hkt : σ'.kheap t = some (.tcb xt)) + (hst : xt.tcbState = .blockedOnSend e d) (hb : d.senderBadge = bd) + (hdq : Fl4SpecA64.tcbEpDequeue (Z := Unit) t e σ' = ({((), σd)}, False)) : + Fl4SpecA64.removeAndRestartBadgedThread (Z := Unit) t e bd σ' = Fl4SpecA64.restartThreadIfNoFault t σd := by + unfold Fl4SpecA64.removeAndRestartBadgedThread + rw [nd_bind_one (getThreadState_eq hkt), hst] + have hb' : Fl4SpecA64.blockingIpcBadge (.blockedOnSend e d) = bd := by rw [Fl4SpecA64.blockingIpcBadge, hb] + simp only [Fl4Spec.NonDet.when, hb', ↓reduceIte] + exact nd_bind_one hdq _ + +/-- (gen 4) The spec's step on a thread whose badge does not match, as an equation. -/ +theorem rrb_keep_eq {σ' : Fl4SpecA64.State Unit} {t e : Fl4SpecA64.ObjRef} {bd : Fl4SpecA64.Badge} + {xt : Fl4SpecA64.Tcb} {d : Fl4SpecA64.SenderPayload} (hkt : σ'.kheap t = some (.tcb xt)) + (hst : xt.tcbState = .blockedOnSend e d) (hb : d.senderBadge ≠ bd) : + Fl4SpecA64.removeAndRestartBadgedThread (Z := Unit) t e bd σ' = ({((), σ')}, False) := by + unfold Fl4SpecA64.removeAndRestartBadgedThread + rw [nd_bind_one (getThreadState_eq hkt), hst] + have hb' : Fl4SpecA64.blockingIpcBadge (.blockedOnSend e d) ≠ bd := by rw [Fl4SpecA64.blockingIpcBadge]; exact hb + simp only [Fl4Spec.NonDet.when, hb', ↓reduceIte] + rfl + +/-- The spec's step on a thread whose badge matches: dequeued, then restarted. -/ +theorem rrb_deq_mem {σ' σd σ'' : Fl4SpecA64.State Unit} {t e : Fl4SpecA64.ObjRef} {bd : Fl4SpecA64.Badge} + {xt : Fl4SpecA64.Tcb} {d : Fl4SpecA64.SenderPayload} (hkt : σ'.kheap t = some (.tcb xt)) + (hst : xt.tcbState = .blockedOnSend e d) (hb : d.senderBadge = bd) + (hdq : Fl4SpecA64.tcbEpDequeue (Z := Unit) t e σ' = ({((), σd)}, False)) + (hres : ((), σ'') ∈ (Fl4SpecA64.restartThreadIfNoFault (Z := Unit) t σd).1) : + ((), σ'') ∈ (Fl4SpecA64.removeAndRestartBadgedThread (Z := Unit) t e bd σ').1 := by + unfold Fl4SpecA64.removeAndRestartBadgedThread + rw [nd_bind_one (getThreadState_eq hkt), hst] + have hb' : Fl4SpecA64.blockingIpcBadge (.blockedOnSend e d) = bd := by rw [Fl4SpecA64.blockingIpcBadge, hb] + simp only [Fl4Spec.NonDet.when, hb', ↓reduceIte] + exact ⟨((), σd), by rw [hdq]; rfl, hres⟩ + +/-- The spec's step on a thread whose badge does not match: nothing. -/ +theorem rrb_keep_mem {σ' : Fl4SpecA64.State Unit} {t e : Fl4SpecA64.ObjRef} {bd : Fl4SpecA64.Badge} + {xt : Fl4SpecA64.Tcb} {d : Fl4SpecA64.SenderPayload} (hkt : σ'.kheap t = some (.tcb xt)) + (hst : xt.tcbState = .blockedOnSend e d) (hb : d.senderBadge ≠ bd) : + ((), σ') ∈ (Fl4SpecA64.removeAndRestartBadgedThread (Z := Unit) t e bd σ').1 := by + unfold Fl4SpecA64.removeAndRestartBadgedThread + rw [nd_bind_one (getThreadState_eq hkt), hst] + have hb' : Fl4SpecA64.blockingIpcBadge (.blockedOnSend e d) ≠ bd := by rw [Fl4SpecA64.blockingIpcBadge]; exact hb + simp only [Fl4Spec.NonDet.when, hb', ↓reduceIte] + rfl + +/-- The endpoint after dequeuing `t0` from the send queue `kept ++ t0 :: rest'`. -/ +theorem epDeq_send_rm {y : Fl4SpecA64.Endpoint} (hnr : ∀ q, y ≠ .recvEP q) {kept rest' : List U64} {t0 : U64} + (hq : y.epQueue = (kept ++ t0 :: rest').map specPtr) (hnd : (kept ++ t0 :: rest').Nodup) : + (epDeq y (specPtr t0)).epQueue = (kept ++ rest').map specPtr ∧ specPtr t0 ∉ (epDeq y (specPtr t0)).epQueue ∧ + ∀ q, epDeq y (specPtr t0) ≠ .recvEP q := by + obtain ⟨hdqq, hdnr⟩ := epDeq_send_queue hnr (specPtr t0) + have hrm : Fl4SpecA64.removeAll (specPtr t0) y.epQueue = (kept ++ rest').map specPtr := by + rw [hq, removeAll_map hnd] + congr 1 + have hnk : t0 ∉ kept := fun h => by + have := List.nodup_append.1 hnd + exact this.2.2 _ h _ (by simp) rfl + rw [List.erase_append_right _ hnk, List.erase_cons_head] + refine ⟨by rw [hdqq, hrm], ?_, hdnr⟩ + rw [hdqq, hrm, List.mem_map] + rintro ⟨z, hz, ez⟩ + have : z = t0 := specPtr_inj ez + subst this + have := List.nodup_append.1 hnd + rcases List.mem_append.1 hz with h | h + · exact this.2.2 _ h _ (by simp) rfl + · exact (List.nodup_cons.1 this.2.1).1 h + +/-- **One dequeue-and-restart step of the loop**, re-establishing the invariant (a separate declaration from the +loop, see `restart_spine`). -/ +theorem badged_deq (hO : SchedOpsEA st T E R Bf SR) (hP : EPlaced T E R Bf) {e b : U64} (he : e ∈ E) + (σ0 : Fl4SpecA64.State Unit) (q0 : List Fl4SpecA64.ObjRef) {s : S} {σ' : Fl4SpecA64.State Unit} + {y : Fl4SpecA64.Endpoint} {kept rest' : List U64} {t0 nx fuel1 : U64} + (hR : EpStateRel T E R Bf (L.mem s) σ') (hSR : SR s σ') (hky : σ'.kheap (specPtr e) = some (.endpoint y)) + (hq : y.epQueue = (kept ++ t0 :: rest').map specPtr) (hnr : ∀ q, y ≠ .recvEP q) + (hQ : QueueRel (L.mem s) T .Ep (qOf (L.mem s) e) (kept ++ t0 :: rest')) (ht0T : t0 ∈ T) {xt : Fl4SpecA64.Tcb} + (hkt : σ'.kheap (specPtr t0) = some (.tcb xt)) {d : Fl4SpecA64.SenderPayload} + (hst : xt.tcbState = .blockedOnSend (specPtr e) d) (hbe : L.mem s (tBlockA t0) = e) (hb : d.senderBadge = b.bv) + (hnx : nx = rest'.headD 0#u64) (hlen : rest'.length ≤ fuel1.val) + (hon : ∀ t ∈ t0 :: rest', t ∈ T → L.mem s (affA t) = L.cpu s) + (hinc : ∀ z, z ∈ (Fl4Spec.NonDet.mapMx (rrb e b) ((t0 :: rest').map specPtr) σ').1 → + z ∈ (Fl4Spec.NonDet.mapMx (rrb e b) q0 σ0).1) + (hfail : (Fl4Spec.NonDet.mapMx (rrb e b) q0 σ0).2 → + (Fl4Spec.NonDet.mapMx (rrb e b) ((t0 :: rest').map specPtr) σ').2) : + (do + let s1 ← endpoint.ep_dequeue st s e t0 + let s2 ← endpoint.restart_thread_if_no_fault st s1 t0 + ok (ControlFlow.cont (s2, nx, fuel1) : ControlFlow (S × U64 × U64) S)) ⦃ r => + ∃ x' : S × U64 × U64, r = (ControlFlow.cont x' : ControlFlow (S × U64 × U64) S) ∧ BInv st T E R Bf SR e b σ0 q0 x' ∧ x'.2.2 = fuel1 ⦄ := by + have hd : specPtr t0 ∈ y.epQueue := by rw [hq]; simp + refine WP.spec_bind (ep_dequeue_rel hO hP s σ' hR hSR he hky hd) ?_ + rintro s1 ⟨hdq, hR1, hS1, hA1⟩ + have hte : specPtr t0 ≠ specPtr e := by intro h; rw [h, hky] at hkt; cases hkt + have hktd : (updK σ' (specPtr e) (.endpoint (epDeq y (specPtr t0)))).kheap (specPtr t0) = some (.tcb xt) := by + rw [updK_kheap, Function.update_of_ne hte, hkt] + have hkyd : (updK σ' (specPtr e) (.endpoint (epDeq y (specPtr t0)))).kheap (specPtr e) = + some (.endpoint (epDeq y (specPtr t0))) := by rw [updK_kheap, Function.update_self] + have hta := hP.tcb t0 ht0T + have hb1 : L.mem s1 (tBlockA t0) = e := (hA1 _ (a_blk hta).1 ⟨t0, ht0T, (a_blk hta).2⟩).trans hbe + obtain ⟨hrq, hnq, hdnr⟩ := epDeq_send_rm hnr hq hQ.nodup + have hq1 := dequeued_not_queued hR1 he hb1 hkyd hnq + refine WP.spec_bind (restart_rel hO hP s1 _ hR1 hS1 ht0T hktd hq1 + ⟨Or.inr (Or.inr (Or.inl ⟨_, _, hst⟩)), by rw [hst]; rfl⟩ + ((hO.coreKeep _ _ _ _ hSR hS1).onCore ht0T (hon t0 List.mem_cons_self ht0T))) ?_ + rintro s2 ⟨hnf, σ'', hres, hR2, hS2, hkeep, -⟩ + rw [WP.spec_ok] + have heq : rrb e b (specPtr t0) σ' = _ := rrb_deq_eq hkt hst hb hdq + exact ⟨_, rfl, ⟨σ'', epDeq y (specPtr t0), kept, rest', hR2, hS2, hkeep _ _ hkyd, hrq, hdnr, hnx, hlen, + fun t ht hT => (hO.coreKeep _ _ _ _ hSR hS2).onCore hT (hon t (List.mem_cons_of_mem _ ht) hT), + fun z hz => hinc z (mapMx_cons_mem (g := rrb e b) (l := rest'.map specPtr) (rrb_deq_mem hkt hst hb hdq hres) z hz), + fun hw => mapMx_cons_fail (l := rest'.map specPtr) (by rw [heq]; exact hnf) (by rw [heq]; exact hres) + (hfail hw)⟩, rfl⟩ + +set_option maxHeartbeats 4000000 in +/-- **The loop of `cancel_badged_sends`** on `EpStateRel ∧ SR`. -/ +theorem badged_loop (hO : SchedOpsEA st T E R Bf SR) (hP : EPlaced T E R Bf) + {e b : U64} (he : e ∈ E) (σ0 : Fl4SpecA64.State Unit) (q0 : List Fl4SpecA64.ObjRef) (x0 : S × U64 × U64) + (h0 : BInv st T E R Bf SR e b σ0 q0 x0) : + endpoint.cancel_badged_sends_loop st x0.1 e b x0.2.1 x0.2.2 ⦃ s' => ¬ (Fl4Spec.NonDet.mapMx (rrb e b) q0 σ0).2 ∧ ∃ σf, + ((), σf) ∈ (Fl4Spec.NonDet.mapMx (rrb e b) q0 σ0).1 ∧ EpStateRel T E R Bf (L.mem s') σf ∧ SR s' σf ⦄ := by + unfold endpoint.cancel_badged_sends_loop + apply loop.spec_decr_nat (measure := fun x : S × U64 × U64 => x.2.2.val) + (inv := BInv st T E R Bf SR e b σ0 q0) _ _ x0 ?_ h0 + rintro ⟨s, t, fuel⟩ ⟨σ', y, kept, rest, hR, hSR, hky, hq, hnr, ht, hlen, hon, hinc, hfail⟩ + simp only at hR hSR hky hq hnr ht hlen hon hinc hfail ⊢ + have hEP := hR.at_ep he hky + obtain ⟨xs, hQ, hxs, hB⟩ := hEP.queue + have hxs' : xs = kept ++ rest := by + rw [hq] at hxs + exact (List.map_injective_iff.2 (fun a b h => specPtr_inj h) hxs).symm + subst hxs' + unfold endpoint.cancel_badged_sends_loop.body + step with obj.Tcb.is_null.spec as ⟨bn, hbn⟩ + cases rest with + | nil => + simp only [List.headD_nil] at ht + subst ht + simp only [hbn, decide_true, ↓reduceIte, WP.spec_ok] + exact ⟨fun h => hfail h, σ', hinc _ rfl, hR, hSR⟩ + | cons t0 rest' => + simp only [List.headD_cons] at ht + subst t + have ht0T : t0 ∈ T := hQ.mem t0 (by simp) + have ht00 : t0 ≠ 0#u64 := hQ.ne0 hP.tcb t0 (by simp) + simp only [hbn, ht00, decide_false, Bool.false_eq_true, ↓reduceIte] + have hf0 : fuel > 0#u64 := by + simp only [List.length_cons] at hlen + show (0#u64 : U64).val < fuel.val; simp; omega + simp only [hf0, ↓reduceIte] + step as ⟨fuel1, hfuel1⟩ + -- the successor + have hseg := hQ.seg + rw [Seg_mid] at hseg + obtain ⟨-, -, hnext, -⟩ := hseg + unfold tcbqueue.next_of + step with tcbqueue.next.spec as ⟨nx, hnx⟩ + try simp only [bind_ok] + -- the thread's state + obtain ⟨hbe, hcode⟩ := hB t0 (by simp) + obtain ⟨xt, hkt, hTRE⟩ := hR.tcb t0 ht0T + have h4 : (L.mem s (stA t0)).val = 4 := by + cases y with + | idleEP => simp [Fl4SpecA64.Endpoint.epQueue] at hq + | recvEP q => exact absurd rfl (hnr q) + | sendEP q => + obtain ⟨-, ys, -, hys, hQ', hqo⟩ := hEP + have := hQ'.unique hP.tcb hQ + rw [this] at hqo + exact (hqo t0 (by simp)).1 + obtain ⟨e', d, hst⟩ : ∃ e' d, xt.tcbState = .blockedOnSend e' d := by + have := hTRE.st; rw [h4] at this + exact tsCode_four this.symm + have he' : e' = specPtr e := by + rw [← hTRE.blk e' (by simp [hst, Fl4SpecA64.epBlocked]), hbe] + subst he' + have hbdg : (L.mem s (tBadgeA t0)).bv = d.senderBadge := (hTRE.send _ d hst).1 + step with obj.Tcb.block_badge.spec as ⟨i, hi⟩ + subst hi + by_cases hm : L.mem s (tBadgeA t0) = b + · -- dequeued and restarted + simp only [hm, ↓reduceIte] + have hb' : d.senderBadge = b.bv := by rw [← hbdg, hm] + refine WP.spec_mono (badged_deq hO hP he σ0 q0 hR hSR hky hq hnr hQ ht0T hkt hst hbe hb' (by rw [hnx, hnext]) + (by rw [hfuel1]; simp only [List.length_cons] at hlen; scalar_tac) hon hinc hfail) ?_ + rintro r ⟨x', rfl, hI, hf⟩ + exact ⟨hI, by rw [hf, hfuel1]; scalar_tac⟩ + · -- kept + simp only [hm, ↓reduceIte, WP.spec_ok] + have hkq : rrb e b (specPtr t0) σ' = ({((), σ')}, False) := + rrb_keep_eq hkt hst (fun h => hm (eq_of_bv (hbdg.trans h))) + refine ⟨⟨σ', y, kept ++ [t0], rest', hR, hSR, hky, by rw [hq]; simp, hnr, by rw [hnx, hnext], ?_, + fun t ht hT => hon t (List.mem_cons_of_mem _ ht) hT, ?_, ?_⟩, ?_⟩ + · rw [hfuel1]; simp only [List.length_cons] at hlen; scalar_tac + · exact fun z hz => hinc z (mapMx_cons_mem (g := rrb e b) (l := rest'.map specPtr) + (rrb_keep_mem hkt hst (fun h => hm (eq_of_bv (hbdg.trans h)))) z hz) + · exact fun hw => mapMx_cons_fail (l := rest'.map specPtr) (by rw [hkq]; exact fun h => h) + (by rw [hkq]; rfl) (hfail hw) + · rw [hfuel1]; scalar_tac + +set_option maxHeartbeats 4000000 in +/-- **`cancel_badged_sends` refines `cancel_badged_sends`** on `EpStateRel ∧ SR`: every FL4 outcome is related to +one of the spec's. -/ +theorem cancel_badged_rel (hO : SchedOpsEA st T E R Bf SR) (hP : EPlaced T E R Bf) + (s : S) (σ : Fl4SpecA64.State Unit) (hR : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) {e : U64} (he : e ∈ E) + (b : U64) (hon : ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → ∀ t ∈ T, specPtr t ∈ z.epQueue → + L.mem s (affA t) = L.cpu s) : + endpoint.cancel_badged_sends st s e b ⦃ s' => ¬ (Fl4SpecA64.cancelBadgedSends (Z := Unit) (specPtr e) b.bv σ).2 ∧ + ∃ σ', ((), σ') ∈ (Fl4SpecA64.cancelBadgedSends (Z := Unit) (specPtr e) b.bv σ).1 ∧ + EpStateRel T E R Bf (L.mem s') σ' ∧ SR s' σ' ⦄ := by + obtain ⟨y, hky, hEP⟩ := hR.ep e he + unfold endpoint.cancel_badged_sends + step with obj.Ep.state.spec as ⟨i, hi⟩ + rw [epState_eq] at hi + have hsp : Fl4SpecA64.cancelBadgedSends (Z := Unit) (specPtr e) b.bv σ = (match y with + | .sendEP queue => (do + Fl4Spec.NonDet.mapMx (fun t => Fl4SpecA64.removeAndRestartBadgedThread t (specPtr e) b.bv) queue + Fl4SpecA64.rescheduleRequired) + | _ => return ()) σ := by + unfold Fl4SpecA64.cancelBadgedSends + rw [nd_bind_one (getEndpoint_eq hky)] + rfl + cases y with + | idleEP => + have h0 : i ≠ obj.EP_SEND := by rw [hi, hEP.1, ep_state_vals.2.1]; decide + simp only [h0, bne_iff_ne, ne_eq, not_false_eq_true, ↓reduceIte, WP.spec_ok] + exact ⟨by rw [hsp]; exact fun h => h, σ, by rw [hsp]; rfl, hR, hSR⟩ + | recvEP q => + have h0 : i ≠ obj.EP_SEND := by rw [hi, hEP.1, ep_state_vals.2.1]; decide + simp only [h0, bne_iff_ne, ne_eq, not_false_eq_true, ↓reduceIte, WP.spec_ok] + exact ⟨by rw [hsp]; exact fun h => h, σ, by rw [hsp]; rfl, hR, hSR⟩ + | sendEP q => + have h0 : i = obj.EP_SEND := by rw [hi, hEP.1, ep_state_vals.2.1] + simp only [h0, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + obtain ⟨-, xs, -, hqx, hQ, -⟩ := hEP + step with endpoint.get_queue.spec as ⟨qq, hqq⟩ + have hfuel : layout.WALK_FUEL.val = 2 ^ 64 - 1 := by simp [layout.WALK_FUEL, U64.rMax] + have hI0 : BInv st T E R Bf SR e b σ q (s, qq.head, layout.WALK_FUEL) := + ⟨σ, .sendEP q, [], xs, hR, hSR, hky, by rw [hqx]; rfl, (fun q' e' => by cases e'), + by rw [hqq, epQ_eq, hQ.head], by rw [hfuel]; exact len_le_max xs hQ.nodup (hQ.ne0 hP.tcb), + fun t ht hT => hon _ hky t hT (by show specPtr t ∈ q; rw [hqx]; exact List.mem_map.2 ⟨t, ht, rfl⟩), + fun z hz => by rw [hqx]; exact hz, fun h => by rw [← hqx]; exact h⟩ + refine WP.spec_bind (badged_loop hO hP he σ q _ hI0) ?_ + rintro s1 ⟨hnf, σf, hmf, hRf, hSf⟩ + refine WP.spec_mono (hO.reschedule s1 σf hRf hSf) ?_ + rintro s2 ⟨hnr2, σr, hrs, hf2, hv2, hS2⟩ + refine ⟨?_, σr, ?_, hRf.frame hP hf2 hv2, hS2⟩ + · rw [hsp] + exact fun h => hnr2 (Det.detAt_bind_fail ((Det.det_mapMx (fun a => Det.det_removeAndRestartBadged a _ _) q).at σ) + hnf hmf h) + rw [hsp] + exact ⟨((), σf), hmf, hrs⟩ + +end Loop2 + +end + +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/EpCancelAllA.lean b/proofs/Fl4ProofsA64/P3/EpCancelAllA.lean new file mode 100644 index 000000000..e3f9dd104 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/EpCancelAllA.lean @@ -0,0 +1,416 @@ +/- +P3 (aarch64, A64-P3-IPC gen 4, ruling item 3): **`cancel_all_ipc` refines `Fl4SpecA64.cancelAllIpc`** (`Z = Unit`; an +endpoint's finalisation) over `EpStateRel` and the endpoint scheduler interface `SchedOpsEA`, the spec not failing +(x86 `P3/IpcCancelAllIpc`'s loop, on `EpStateRel ∧ SR` as gen 3's `cancel_badged_rel`). FL4's AArch64 +`cancel_all_ipc` and its loop are the x86 text. + +FL4 walks the endpoint's queue from its head; each step reads the successor, dequeues the thread (`ep_dequeue`), reads +its state word and, for a receiver, its reply word (unlinking a nonzero one), then restarts it +(`restart_thread_if_no_fault`, EpCancelA's `restart_rel`). The spec runs `remove_and_restart_ep_queued_thread` over the +queue (`mapM_x`), then `reschedule_required`. + +* `rra_of` / `rra_eq` / `rra_reply` (the spec's step from the dequeued state: the reply unlink if any, then the + restart), `AInv` (the loop invariant: the queue still to visit, each with `RepOKA`, the spec's outcomes from it among + the whole's, and the whole failing only if the rest does, as gen 4's `BInv`), `all_tail` (the restart from the + state the dequeue / unlink left, split at the `stsAct` state inside `restart_relO`), `unlink_ok` / `all_reply` (the + receiver with a reply object), `all_deq` (one step), `all_loop`, **`cancel_all_rel`**. + +(gen 6, ruling (a)) **Receivers holding a reply object.** FL4's `reply_unlink` (EpCancelIpcA's `reply_unlink_rel`) +against the spec's `reply_unlink_tcb`, then `restart_thread_if_no_fault` of the now `Inactive` thread: its +`Inactive → Restart` is `NoBlockE`'s branch 2 from `RelIfBound`, which `SchedOpsEA.rd` gives at the dequeued state +(the receiver still `BlockedOnReceive`, in `inClassR`) and `RelIfBound.keep` carries across the reply word and the +state word (`unlink_ok`; EpCancelA's `RestartOK`). `KeepO` (now also: replies not pointing to the restarted thread +keep their TCB) carries `RepOKA` of the threads still queued. + +**Case hypothesis** `hrep` (gen 6: replaces gen 4's "no reply object"; not a named premise): every thread queued on +the endpoint has `RepOKA` (l4v `sym_refs` for `reply_tcb`, the same spec-level fact as `cancel_ipc_queued`'s). Other +hypotheses: `EPlaced`, `e ∈ E`, the interface. `blk_of_code`: a queued thread's state is blocked on an endpoint. +(gen 7, ruling item 3) `cancel_all_rel` takes `hon` (every queued thread on the current core, `SchedOpsEA.restartTail`'s +precondition); `AInv` carries it for the threads still to visit (`SchedOpsEA.coreKeep` across each step). +-/ +import Fl4ProofsA64.P3.EpCancelIpcA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif + +noncomputable section + +section Spec +open Fl4SpecA64 Classical + +/-- One step of the spec's loop. -/ +abbrev rra (e : U64) (t : ObjRef) : SMonad Unit Unit := removeAndRestartEpQueuedThread t (specPtr e) + +/-- (gen 5) A queued thread's state (code 3 or 4) is blocked on an endpoint: in `inClass`, not runnable. -/ +theorem blk_of_code {ts : ThreadState} (h : tsCode ts = 3 ∨ tsCode ts = 4) : inClass ts ∧ runnable ts = false := by + rcases h with h | h + · obtain ⟨e, r, d, rfl⟩ := tsCode_three h; exact ⟨Or.inr (Or.inr (Or.inr ⟨e, r, d, rfl⟩)), rfl⟩ + · obtain ⟨e, d, rfl⟩ := tsCode_four h; exact ⟨Or.inr (Or.inr (Or.inl ⟨e, d, rfl⟩)), rfl⟩ + +/-- The spec's step from the dequeued state: the reply unlink (if any), then the restart. -/ +theorem rra_of {σ' σd σ4 : State Unit} {t e : ObjRef} {xt : Tcb} {ro : Option ObjRef} + (hdq : tcbEpDequeue (Z := Unit) t e σ' = ({((), σd)}, False)) (hktd : σd.kheap t = some (.tcb xt)) + (hro : (if isBlockedOnReceive xt.tcbState then replyObject xt.tcbState else none) = ro) + (hun : Fl4Spec.NonDet.maybeM (replyUnlinkTcb (Z := Unit) t) ro σd = ({((), σ4)}, False)) : + removeAndRestartEpQueuedThread (Z := Unit) t e σ' = restartThreadIfNoFault t σ4 := by + unfold removeAndRestartEpQueuedThread + rw [nd_bind_one hdq] + try dsimp only + rw [nd_bind_one (getThreadState_eq hktd)] + try dsimp only + rw [nd_pure_bind'] + try dsimp only + rw [hro] + exact nd_bind_one hun _ + +/-- **The spec's step on a thread with no reply object**: the restart from the dequeued state. -/ +theorem rra_eq {σ' σd : State Unit} {t e : ObjRef} {xt : Tcb} + (hdq : tcbEpDequeue (Z := Unit) t e σ' = ({((), σd)}, False)) (hktd : σd.kheap t = some (.tcb xt)) + (hrep : replyObject xt.tcbState = none) : + removeAndRestartEpQueuedThread (Z := Unit) t e σ' = restartThreadIfNoFault t σd := + rra_of hdq hktd (by split; exact hrep; rfl) rfl + +/-- (gen 6) **The spec's step on a receiver with a reply object** (`reply_unlink_tcb`, then the restart). -/ +theorem rra_reply {σ' σd σ4 : State Unit} {t e r : ObjRef} {xt : Tcb} + (hdq : tcbEpDequeue (Z := Unit) t e σ' = ({((), σd)}, False)) (hktd : σd.kheap t = some (.tcb xt)) + {ep : ObjRef} {d : ReceiverPayload} (hst : xt.tcbState = .blockedOnReceive ep (some r) d) + (hun : replyUnlinkTcb (Z := Unit) t r σd = ({((), σ4)}, False)) : + removeAndRestartEpQueuedThread (Z := Unit) t e σ' = restartThreadIfNoFault t σ4 := + rra_of (ro := some r) hdq hktd (by rw [if_pos ⟨ep, some r, d, hst⟩, hst]; try rfl) + (show Fl4Spec.NonDet.maybeM (replyUnlinkTcb (Z := Unit) t) (some r) σd = _ from hun) + +/-- (gen 6) Unlinking the reply that points to `t` keeps the other TCBs and replies. -/ +theorem KeepO.updR {σ : State Unit} {q : ObjRef} {xr : Reply} (hq : σ.kheap q = some (.reply xr)) {t : ObjRef} + (hb : xr.replyTcb = some t) (o : Reply) : KeepO σ (updK σ q (.reply o)) t := by + refine ⟨fun p x _ hx => ⟨x, ?_, rfl⟩, fun p x hx hn => ⟨x, ?_, rfl⟩⟩ + · have hpq : p ≠ q := fun e => by rw [e, hq] at hx; cases hx + rw [updK_kheap, Function.update_of_ne hpq, hx] + · have hpq : p ≠ q := fun e => by + rw [e, hq] at hx; injection hx with hx; injection hx with hx; exact hn (hx ▸ hb) + rw [updK_kheap, Function.update_of_ne hpq, hx] + +end Spec + +section Loop +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {T E R : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {SR : S → Fl4SpecA64.State Unit → Prop} + +variable (st T E R Bf SR) in +/-- The loop's invariant: the queue still to visit `rest`, FL4's cursor at its head, (gen 6) each of them with +`RepOKA`, the spec's outcomes from it among the whole loop's, the whole failing only if the rest does; (gen 7) the +threads still to visit on the current core. -/ +def AInv (e : U64) (σ0 : Fl4SpecA64.State Unit) (q0 : List Fl4SpecA64.ObjRef) (x : S × U64 × U64) : Prop := + ∃ σ' y, ∃ rest : List U64, EpStateRel T E R Bf (L.mem x.1) σ' ∧ SR x.1 σ' ∧ + σ'.kheap (specPtr e) = some (.endpoint y) ∧ y.epQueue = rest.map specPtr ∧ x.2.1 = rest.headD 0#u64 ∧ + rest.length ≤ x.2.2.val ∧ (∀ t ∈ rest, RepOKA σ' (specPtr t)) ∧ + (∀ t ∈ rest, t ∈ T → L.mem x.1 (affA t) = L.cpu x.1) ∧ + (∀ z, z ∈ (Fl4Spec.NonDet.mapMx (rra e) (rest.map specPtr) σ').1 → + z ∈ (Fl4Spec.NonDet.mapMx (rra e) q0 σ0).1) ∧ + ((Fl4Spec.NonDet.mapMx (rra e) q0 σ0).2 → (Fl4Spec.NonDet.mapMx (rra e) (rest.map specPtr) σ').2) + +/-- The restart of the dequeued head from the state `σr` the dequeue (and the reply unlink) left, re-establishing the +invariant (a separate declaration, see `restart_spine`; gen 6: from any such `σr` with `RestartOK`). -/ +theorem all_tail (hO : SchedOpsEA st T E R Bf SR) (hP : EPlaced T E R Bf) {e : U64} (he : e ∈ E) + (σ0 : Fl4SpecA64.State Unit) (q0 : List Fl4SpecA64.ObjRef) {s s1 : S} {σ' σr : Fl4SpecA64.State Unit} + {y : Fl4SpecA64.Endpoint} {rest' : List U64} {t0 nx fuel1 : U64} + (hR : EpStateRel T E R Bf (L.mem s) σ') (hky : σ'.kheap (specPtr e) = some (.endpoint y)) + (hq : y.epQueue = (t0 :: rest').map specPtr) (hnd : (t0 :: rest').Nodup) (ht0T : t0 ∈ T) + (hRr : EpStateRel T E R Bf (L.mem s1) σr) (hSr : SR s1 σr) {xr : Fl4SpecA64.Tcb} + (hktr : σr.kheap (specPtr t0) = some (.tcb xr)) (hok : RestartOK σr (specPtr t0) xr) + (hqr : ∀ e' ∈ E, ∀ z, σr.kheap (specPtr e') = some (.endpoint z) → specPtr t0 ∉ z.epQueue) + (hkyr : σr.kheap (specPtr e) = some (.endpoint (epDeq y (specPtr t0)))) (hkTr : KeepO σ' σr (specPtr t0)) + (hrr : Fl4SpecA64.removeAndRestartEpQueuedThread (Z := Unit) (specPtr t0) (specPtr e) σ' = + Fl4SpecA64.restartThreadIfNoFault (specPtr t0) σr) + (hrep : ∀ t ∈ t0 :: rest', RepOKA σ' (specPtr t)) + (hnx : nx = rest'.headD 0#u64) (hlen : rest'.length ≤ fuel1.val) + (hon : ∀ t ∈ t0 :: rest', t ∈ T → L.mem s1 (affA t) = L.cpu s1) + (hinc : ∀ z, z ∈ (Fl4Spec.NonDet.mapMx (rra e) ((t0 :: rest').map specPtr) σ').1 → + z ∈ (Fl4Spec.NonDet.mapMx (rra e) q0 σ0).1) + (hfail : (Fl4Spec.NonDet.mapMx (rra e) q0 σ0).2 → + (Fl4Spec.NonDet.mapMx (rra e) ((t0 :: rest').map specPtr) σ').2) : + (do + let s3 ← endpoint.restart_thread_if_no_fault st s1 t0 + ok (ControlFlow.cont (s3, nx, fuel1) : ControlFlow (S × U64 × U64) S)) ⦃ r => + ∃ x' : S × U64 × U64, r = (ControlFlow.cont x' : ControlFlow (S × U64 × U64) S) ∧ + AInv st T E R Bf SR e σ0 q0 x' ∧ x'.2.2 = fuel1 ⦄ := by + have hrq : (epDeq y (specPtr t0)).epQueue = rest'.map specPtr := by + rw [epDeq_queue, hq, removeAll_map hnd, List.erase_cons_head] + refine WP.spec_bind (restart_relO hO hP s1 σr hRr hSr ht0T hktr hqr hok (hon t0 List.mem_cons_self ht0T)) ?_ + rintro s2 ⟨hnf, σ'', hres, hR2, hS2, hkeep, hkT⟩ + rw [WP.spec_ok] + refine ⟨_, rfl, ⟨σ'', epDeq y (specPtr t0), rest', hR2, hS2, hkeep _ _ hkyr, hrq, hnx, hlen, ?_, + fun t ht hT => (hO.coreKeep _ _ _ _ hSr hS2).onCore hT (hon t (List.mem_cons_of_mem _ ht) hT), + fun z hz => hinc z (mapMx_cons_mem (g := rra e) (l := rest'.map specPtr) + (by simp only [rra]; rw [hrr]; exact hres) z hz), + fun hw => Det.detAt_bind_fail ((Det.det_removeAndRestartEp _ _).at σ') + (by rw [hrr]; exact hnf) + (by rw [hrr]; exact hres) (hfail hw)⟩, rfl⟩ + intro t ht + have htt : t ≠ t0 := fun e' => (List.nodup_cons.1 hnd).1 (e' ▸ ht) + have htT : t ∈ T := (hR.at_ep he hky).blocked (by rw [hq]; exact List.mem_map_of_mem (List.mem_cons_of_mem _ ht)) |>.1 + obtain ⟨z, hz, -⟩ := hR.tcb t htT + exact (hrep t (List.mem_cons_of_mem _ ht)).of_keepO (hkTr.trans hkT) (fun h => htt (specPtr_inj h)) hz + +/-- (gen 6, ruling (a)) After `reply_unlink_tcb` the receiver (now `Inactive`) still has `RelIfBound` (the SCs, its +SC pointer, the release queue and the time are unchanged), so its restart has `RestartOK`. -/ +theorem unlink_ok {σd : Fl4SpecA64.State Unit} {t i : U64} {xt : Fl4SpecA64.Tcb} {xr : Fl4SpecA64.Reply} + (hktd : σd.kheap (specPtr t) = some (.tcb xt)) (hrel : RelIfBound σd (specPtr t)) : + RestartOK (stsAct (updK (updK σd (specPtr i) (.reply { xr with replyTcb := none })) (specPtr t) + (.tcb { xt with tcbState := .inactive })) (specPtr t)) (specPtr t) { xt with tcbState := .inactive } := by + refine ⟨rfl, (fun h => by cases h), fun _ => Or.inr ?_⟩ + refine RelIfBound.keep hrel (by rw [stsAct_releaseQueue]; rfl) (by rw [stsAct_curTime]; rfl) (fun x' hx' => ?_) + (fun q y' n hq' => ?_) + · rw [stsAct_kheap, updK_kheap, Function.update_self] at hx' + injection hx' with hx'; injection hx' with hx'; subst hx'; exact ⟨xt, hktd, rfl⟩ + · rw [stsAct_kheap, updK_kheap, updK_kheap, Function.update_apply, Function.update_apply] at hq' + split_ifs at hq' <;> first | exact hq' | cases hq' + +/-- (gen 6, ruling (a)) **The restart of a receiver after `reply_unlink`**, from the `stsAct` state the unlink leaves +(a separate declaration): `SchedOpsEA.rd` before the unlink (the receiver in `inClassR`) gives `RelIfBound`, carried +across the reply word and the state word (`RelIfBound.keep`), so `Inactive → Restart` is `NoBlockE`'s branch 2. -/ +theorem all_reply (hO : SchedOpsEA st T E R Bf SR) (hP : EPlaced T E R Bf) {e : U64} (he : e ∈ E) + (σ0 : Fl4SpecA64.State Unit) (q0 : List Fl4SpecA64.ObjRef) {s s3 : S} {σ' : Fl4SpecA64.State Unit} + {y : Fl4SpecA64.Endpoint} {rest' : List U64} {t0 nx fuel1 : U64} + (hR : EpStateRel T E R Bf (L.mem s) σ') (hky : σ'.kheap (specPtr e) = some (.endpoint y)) + (hq : y.epQueue = (t0 :: rest').map specPtr) (hnd : (t0 :: rest').Nodup) (ht0T : t0 ∈ T) + {xt : Fl4SpecA64.Tcb} (hkt : σ'.kheap (specPtr t0) = some (.tcb xt)) {ep : Fl4SpecA64.ObjRef} + {d : Fl4SpecA64.ReceiverPayload} {i : U64} (hst : xt.tcbState = .blockedOnReceive ep (some (specPtr i)) d) + {xr : Fl4SpecA64.Reply} (hkr : σ'.kheap (specPtr i) = some (.reply xr)) (hback : xr.replyTcb = some (specPtr t0)) + (hdq : Fl4SpecA64.tcbEpDequeue (Z := Unit) (specPtr t0) (specPtr e) σ' = + ({((), updK σ' (specPtr e) (.endpoint (epDeq y (specPtr t0))))}, False)) + (hrel : RelIfBound (updK σ' (specPtr e) (.endpoint (epDeq y (specPtr t0)))) (specPtr t0)) + (hq1 : ∀ e' ∈ E, ∀ z, (updK σ' (specPtr e) (.endpoint (epDeq y (specPtr t0)))).kheap (specPtr e') = + some (.endpoint z) → specPtr t0 ∉ z.epQueue) + (hR3 : EpStateRel T E R Bf (L.mem s3) (stsAct (updK (updK (updK σ' (specPtr e) (.endpoint (epDeq y (specPtr t0)))) + (specPtr i) (.reply { xr with replyTcb := none })) (specPtr t0) (.tcb { xt with tcbState := .inactive })) (specPtr t0))) + (hS3 : SR s3 (stsAct (updK (updK (updK σ' (specPtr e) (.endpoint (epDeq y (specPtr t0)))) + (specPtr i) (.reply { xr with replyTcb := none })) (specPtr t0) (.tcb { xt with tcbState := .inactive })) (specPtr t0))) + (hrep : ∀ t ∈ t0 :: rest', RepOKA σ' (specPtr t)) + (hnx : nx = rest'.headD 0#u64) (hlen : rest'.length ≤ fuel1.val) + (hon : ∀ t ∈ t0 :: rest', t ∈ T → L.mem s3 (affA t) = L.cpu s3) + (hinc : ∀ z, z ∈ (Fl4Spec.NonDet.mapMx (rra e) ((t0 :: rest').map specPtr) σ').1 → + z ∈ (Fl4Spec.NonDet.mapMx (rra e) q0 σ0).1) + (hfail : (Fl4Spec.NonDet.mapMx (rra e) q0 σ0).2 → + (Fl4Spec.NonDet.mapMx (rra e) ((t0 :: rest').map specPtr) σ').2) : + (do + let s4 ← endpoint.restart_thread_if_no_fault st s3 t0 + ok (ControlFlow.cont (s4, nx, fuel1) : ControlFlow (S × U64 × U64) S)) ⦃ r => + ∃ x' : S × U64 × U64, r = (ControlFlow.cont x' : ControlFlow (S × U64 × U64) S) ∧ + AInv st T E R Bf SR e σ0 q0 x' ∧ x'.2.2 = fuel1 ⦄ := by + have hte : specPtr t0 ≠ specPtr e := by intro h; rw [h, hky] at hkt; cases hkt + have hie : specPtr i ≠ specPtr e := by intro h; rw [h, hky] at hkr; cases hkr + have hit : specPtr i ≠ specPtr t0 := by intro h; rw [h, hkt] at hkr; cases hkr + have hktd : (updK σ' (specPtr e) (.endpoint (epDeq y (specPtr t0)))).kheap (specPtr t0) = some (.tcb xt) := by + rw [updK_kheap, Function.update_of_ne hte, hkt] + have hkrd : (updK σ' (specPtr e) (.endpoint (epDeq y (specPtr t0)))).kheap (specPtr i) = some (.reply xr) := by + rw [updK_kheap, Function.update_of_ne hie, hkr] + have hk3 : (updK (updK σ' (specPtr e) (.endpoint (epDeq y (specPtr t0)))) (specPtr i) + (.reply { xr with replyTcb := none })).kheap (specPtr t0) = some (.tcb xt) := by + rw [updK_kheap, Function.update_of_ne hit.symm, hktd] + obtain ⟨hk4, hq4, hkeep4, hkT4⟩ := sts_facts hk3 (notq_updR (E := E) hq1 i { xr with replyTcb := none }) .inactive + exact all_tail hO hP he σ0 q0 hR hky hq hnd ht0T hR3 hS3 hk4 (unlink_ok (i := i) (xr := xr) hktd hrel) hq4 + (hkeep4 _ _ (by rw [updK_kheap, Function.update_of_ne hie.symm, updK_kheap, Function.update_self])) + (((KeepO.updE hky _ _).trans (KeepO.updR hkrd hback _)).trans hkT4) + (rra_reply hdq hktd hst (replyUnlinkTcb_eq hkrd hback hktd hst)) hrep hnx hlen hon hinc hfail + +/-- **One dequeue-and-restart step of the loop** (FL4's step after the successor read; gen 6: a receiver holding a +reply object too, `RepOKA`). -/ +theorem all_deq (hO : SchedOpsEA st T E R Bf SR) (hP : EPlaced T E R Bf) {e : U64} (he : e ∈ E) + (σ0 : Fl4SpecA64.State Unit) (q0 : List Fl4SpecA64.ObjRef) {s : S} {σ' : Fl4SpecA64.State Unit} + {y : Fl4SpecA64.Endpoint} {rest' : List U64} {t0 nx fuel1 : U64} + (hR : EpStateRel T E R Bf (L.mem s) σ') (hSR : SR s σ') (hky : σ'.kheap (specPtr e) = some (.endpoint y)) + (hq : y.epQueue = (t0 :: rest').map specPtr) + (hQ : QueueRel (L.mem s) T .Ep (qOf (L.mem s) e) (t0 :: rest')) (ht0T : t0 ∈ T) {xt : Fl4SpecA64.Tcb} + (hkt : σ'.kheap (specPtr t0) = some (.tcb xt)) (hbe : L.mem s (tBlockA t0) = e) + (hc : tsCode xt.tcbState = 3 ∨ tsCode xt.tcbState = 4) + (hrep : ∀ t ∈ t0 :: rest', RepOKA σ' (specPtr t)) + (hnx : nx = rest'.headD 0#u64) (hlen : rest'.length ≤ fuel1.val) + (hon : ∀ t ∈ t0 :: rest', t ∈ T → L.mem s (affA t) = L.cpu s) + (hinc : ∀ z, z ∈ (Fl4Spec.NonDet.mapMx (rra e) ((t0 :: rest').map specPtr) σ').1 → + z ∈ (Fl4Spec.NonDet.mapMx (rra e) q0 σ0).1) + (hfail : (Fl4Spec.NonDet.mapMx (rra e) q0 σ0).2 → + (Fl4Spec.NonDet.mapMx (rra e) ((t0 :: rest').map specPtr) σ').2) : + (do + let s1 ← endpoint.ep_dequeue st s e t0 + let i ← obj.Tcb.state st t0 s1 + let s2 ← + if i = sched.TS_BLOCKED_ON_RECV + then + do + let i1 ← obj.Tcb.reply_obj st t0 s1 + if i1 != 0#u64 + then ipc.reply_unlink st s1 i1 t0 + else ok s1 + else ok s1 + let s3 ← endpoint.restart_thread_if_no_fault st s2 t0 + ok (ControlFlow.cont (s3, nx, fuel1) : ControlFlow (S × U64 × U64) S)) ⦃ r => + ∃ x' : S × U64 × U64, r = (ControlFlow.cont x' : ControlFlow (S × U64 × U64) S) ∧ + AInv st T E R Bf SR e σ0 q0 x' ∧ x'.2.2 = fuel1 ⦄ := by + have hd : specPtr t0 ∈ y.epQueue := by rw [hq]; simp + refine WP.spec_bind (ep_dequeue_rel hO hP s σ' hR hSR he hky hd) ?_ + rintro s1 ⟨hdq, hR1, hS1, hA1⟩ + have hte : specPtr t0 ≠ specPtr e := by intro h; rw [h, hky] at hkt; cases hkt + have hktd : (updK σ' (specPtr e) (.endpoint (epDeq y (specPtr t0)))).kheap (specPtr t0) = some (.tcb xt) := by + rw [updK_kheap, Function.update_of_ne hte, hkt] + have hkyd : (updK σ' (specPtr e) (.endpoint (epDeq y (specPtr t0)))).kheap (specPtr e) = + some (.endpoint (epDeq y (specPtr t0))) := by rw [updK_kheap, Function.update_self] + have hta := hP.tcb t0 ht0T + have hb1 : L.mem s1 (tBlockA t0) = e := (hA1 _ (a_blk hta).1 ⟨t0, ht0T, (a_blk hta).2⟩).trans hbe + have hx1 := hR1.at_tcb ht0T hktd + have hq1 := dequeued_not_queued hR1 he hb1 hkyd (epDeq_not_mem hq hQ.nodup t0) + have hblk := blk_of_code hc + have hcont := fun (hro : Fl4SpecA64.replyObject xt.tcbState = none) => + all_tail hO hP he σ0 q0 hR hky hq hQ.nodup ht0T hR1 hS1 hktd (RestartOK.of_blk hblk) hq1 hkyd + (KeepO.updE hky _ _) (rra_eq hdq hktd hro) hrep hnx hlen + (fun t ht hT => (hO.coreKeep _ _ _ _ hSR hS1).onCore hT (hon t ht hT)) hinc hfail + step with obj.Tcb.state.spec as ⟨i, hi⟩ + by_cases hr : i = sched.TS_BLOCKED_ON_RECV + · simp only [hr, ↓reduceIte] + have h3 : tsCode xt.tcbState = 3 := by + rw [← hx1.st, ← hi, hr]; unfold sched.TS_BLOCKED_ON_RECV; rfl + obtain ⟨e', r', d, hst⟩ := tsCode_three h3 + have hrec := hx1.recv e' r' d hst + step with obj.Tcb.reply_obj.spec as ⟨i1, hi1⟩ + have hi1' : i1 = L.mem s1 (tReplyA t0) := hi1 + by_cases hi0 : i1 = 0#u64 + · have hro : r' = none := by rw [← hrec.1, ← hi1', hi0, optRef_zero] + simp only [hi0, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte, bind_ok] + exact hcont (by rw [hst, hro]; rfl) + · have hro : r' = some (specPtr i1) := by rw [← hrec.1, ← hi1', optRef_ne hi0] + subst hro + have hiR : i1 ∈ R := by rw [hi1']; exact hrec.2 (by simp) + obtain ⟨xr, hkr, hback⟩ := hrep t0 (by simp) xt hkt _ _ d hst + have hie : specPtr i1 ≠ specPtr e := by intro h; rw [h, hky] at hkr; cases hkr + have hkr1 : (updK σ' (specPtr e) (.endpoint (epDeq y (specPtr t0)))).kheap (specPtr i1) = some (.reply xr) := by + rw [updK_kheap, Function.update_of_ne hie, hkr] + have hrel := hO.rd _ _ _ _ hS1 hktd (Or.inr (Or.inr (Or.inr ⟨_, _, _, hst⟩))) + have hnz : (i1 != 0#u64) = true := by simpa using hi0 + simp only [hnz, ↓reduceIte] + refine WP.spec_bind (reply_unlink_rel hO hP hS1 hR1 (WritesIn.refl _ _) (EpStep.refl _) ht0T hktd hktd + (OnlyT.refl _ _) ⟨hblk.2, hblk.1.ne_idle hblk.2⟩ hq1 hiR hkr1) ?_ + rintro s3 ⟨hR3, hS3⟩ + exact all_reply hO hP he σ0 q0 hR hky hq hQ.nodup ht0T hkt hst hkr hback hdq hrel hq1 hR3 hS3 hrep hnx hlen + (fun t ht hT => (hO.coreKeep _ _ _ _ hSR hS3).onCore hT (hon t ht hT)) hinc hfail + · simp only [hr, ↓reduceIte, bind_ok] + refine hcont ?_ + rcases hc with h3 | h4 + · exact absurd (UScalar.eq_of_val_eq (by rw [hi, hx1.st, h3]; unfold sched.TS_BLOCKED_ON_RECV; rfl)) hr + · obtain ⟨e', d, hst⟩ := tsCode_four h4; rw [hst]; rfl + +/-- The loop's cursor at a nonempty queue: a TCB of `T`, not null, with fuel left. -/ +theorem cursor_facts (hP : EPlaced T E R Bf) {m : U64 → U64} {q : tcbqueue.Queue} {t0 fuel : U64} {rest' : List U64} + (hQ : QueueRel m T .Ep q (t0 :: rest')) (hlen : (t0 :: rest').length ≤ fuel.val) : + t0 ∈ T ∧ t0 ≠ 0#u64 ∧ fuel > 0#u64 := by + refine ⟨hQ.mem t0 (by simp), hQ.ne0 hP.tcb t0 (by simp), ?_⟩ + simp only [List.length_cons] at hlen + show (0#u64 : U64).val < fuel.val; simp; omega + +set_option maxHeartbeats 4000000 in +/-- **The loop of `cancel_all_ipc`** on `EpStateRel ∧ SR`. -/ +theorem all_loop (hO : SchedOpsEA st T E R Bf SR) (hP : EPlaced T E R Bf) + {e : U64} (he : e ∈ E) (σ0 : Fl4SpecA64.State Unit) (q0 : List Fl4SpecA64.ObjRef) (x0 : S × U64 × U64) + (h0 : AInv st T E R Bf SR e σ0 q0 x0) : + endpoint.cancel_all_ipc_loop st x0.1 e x0.2.1 x0.2.2 ⦃ s' => ¬ (Fl4Spec.NonDet.mapMx (rra e) q0 σ0).2 ∧ + ∃ σf, ((), σf) ∈ (Fl4Spec.NonDet.mapMx (rra e) q0 σ0).1 ∧ EpStateRel T E R Bf (L.mem s') σf ∧ SR s' σf ⦄ := by + unfold endpoint.cancel_all_ipc_loop + apply loop.spec_decr_nat (measure := fun x : S × U64 × U64 => x.2.2.val) + (inv := AInv st T E R Bf SR e σ0 q0) _ _ x0 ?_ h0 + rintro ⟨s, t, fuel⟩ ⟨σ', y, rest, hR, hSR, hky, hq, ht, hlen, hrep, hon, hinc, hfail⟩ + simp only at hR hSR hky hq ht hlen hrep hon hinc hfail ⊢ + have hEP := hR.at_ep he hky + obtain ⟨xs, hQ, hxs, hB⟩ := hEP.queue + have hxs' : xs = rest := by + rw [hq] at hxs + exact (List.map_injective_iff.2 (fun a b h => specPtr_inj h) hxs).symm + subst hxs' + unfold endpoint.cancel_all_ipc_loop.body + step with obj.Tcb.is_null.spec as ⟨bn, hbn⟩ + cases xs with + | nil => + simp only [List.headD_nil] at ht + subst ht + simp only [hbn, decide_true, ↓reduceIte, WP.spec_ok] + exact ⟨fun h => hfail h, σ', hinc _ rfl, hR, hSR⟩ + | cons t0 rest' => + simp only [List.headD_cons] at ht + subst t + obtain ⟨ht0T, ht00, hf0⟩ := cursor_facts hP hQ hlen + simp only [hbn, ht00, decide_false, Bool.false_eq_true, ↓reduceIte, hf0] + step as ⟨fuel1, hfuel1⟩ + have hseg := hQ.seg + rw [show t0 :: rest' = [] ++ t0 :: rest' from rfl, Seg_mid] at hseg + obtain ⟨-, -, hnext, -⟩ := hseg + unfold tcbqueue.next_of + step with tcbqueue.next.spec as ⟨nx, hnx⟩ + try simp only [bind_ok] + obtain ⟨hbe, hc⟩ := hB t0 (by simp) + obtain ⟨xt, hkt, hxt⟩ := hR.tcb t0 ht0T + refine WP.spec_mono (all_deq hO hP he σ0 q0 hR hSR hky hq hQ ht0T hkt hbe (by rw [← hxt.st]; exact hc) hrep (by rw [hnx, hnext]) + (by rw [hfuel1]; simp only [List.length_cons] at hlen; scalar_tac) hon hinc hfail) ?_ + rintro r ⟨x', rfl, hI, hf⟩ + exact ⟨hI, by rw [hf, hfuel1]; scalar_tac⟩ + +set_option maxHeartbeats 4000000 in +/-- **`cancel_all_ipc` refines `cancel_all_ipc`** on `EpStateRel ∧ SR`, the spec not failing, for an endpoint each of +whose queued threads has `RepOKA` (`hrep`; gen 6: receivers holding a reply object included). -/ +theorem cancel_all_rel (hO : SchedOpsEA st T E R Bf SR) (hP : EPlaced T E R Bf) + (s : S) (σ : Fl4SpecA64.State Unit) (hR : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) {e : U64} (he : e ∈ E) + (hrep : ∀ y, σ.kheap (specPtr e) = some (.endpoint y) → ∀ t ∈ y.epQueue, RepOKA σ t) + (hon : ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → ∀ t ∈ T, specPtr t ∈ z.epQueue → + L.mem s (affA t) = L.cpu s) : + endpoint.cancel_all_ipc st s e ⦃ s' => ¬ (Fl4SpecA64.cancelAllIpc (Z := Unit) (specPtr e) σ).2 ∧ + ∃ σ', ((), σ') ∈ (Fl4SpecA64.cancelAllIpc (Z := Unit) (specPtr e) σ).1 ∧ + EpStateRel T E R Bf (L.mem s') σ' ∧ SR s' σ' ⦄ := by + obtain ⟨y, hky, hEP⟩ := hR.ep e he + unfold endpoint.cancel_all_ipc + step with obj.Ep.state.spec as ⟨i, hi⟩ + rw [epState_eq] at hi + by_cases hid : y = .idleEP + · subst hid + have h0 : i = obj.EP_IDLE := by rw [hi, hEP.1, ep_state_vals.1] + have hsp : Fl4SpecA64.cancelAllIpc (Z := Unit) (specPtr e) σ = ({((), σ)}, False) := by + unfold Fl4SpecA64.cancelAllIpc + rw [nd_bind_one (getEndpoint_eq hky)] + rfl + simp only [h0, ↓reduceIte, WP.spec_ok] + exact ⟨by rw [hsp]; exact fun h => h, σ, by rw [hsp]; rfl, hR, hSR⟩ + have hsp : Fl4SpecA64.cancelAllIpc (Z := Unit) (specPtr e) σ = + (Fl4Spec.NonDet.mapMx (rra e) y.epQueue >>= fun _ => Fl4SpecA64.rescheduleRequired) σ := by + unfold Fl4SpecA64.cancelAllIpc + rw [nd_bind_one (getEndpoint_eq hky)] + cases y with + | idleEP => exact absurd rfl hid + | sendEP q => exact nd_pure_bind' _ _ _ + | recvEP q => exact nd_pure_bind' _ _ _ + have h0 : i ≠ obj.EP_IDLE := by + rw [hi] + cases y with + | idleEP => exact absurd rfl hid + | sendEP q => rw [hEP.1, ep_state_vals.1]; decide + | recvEP q => rw [hEP.1, ep_state_vals.1]; decide + simp only [h0, ↓reduceIte] + obtain ⟨xs, hQ, hqx, -⟩ := hEP.queue + step with endpoint.get_queue.spec as ⟨qq, hqq⟩ + have hfuel : layout.WALK_FUEL.val = 2 ^ 64 - 1 := by simp [layout.WALK_FUEL, U64.rMax] + have hI0 : AInv st T E R Bf SR e σ y.epQueue (s, qq.head, layout.WALK_FUEL) := + ⟨σ, y, xs, hR, hSR, hky, hqx, by rw [hqq, epQ_eq, hQ.head], by rw [hfuel]; exact len_le_max xs hQ.nodup (hQ.ne0 hP.tcb), + fun t ht => hrep y hky _ (by rw [hqx]; exact List.mem_map_of_mem ht), + fun t ht hT => hon y hky t hT (by rw [hqx]; exact List.mem_map_of_mem ht), + fun z hz => by rw [hqx]; exact hz, fun h => by rw [← hqx]; exact h⟩ + refine WP.spec_bind (all_loop hO hP he σ y.epQueue _ hI0) ?_ + rintro s1 ⟨hnf, σf, hmf, hRf, hSf⟩ + refine WP.spec_mono (hO.reschedule s1 σf hRf hSf) ?_ + rintro s2 ⟨hnr2, σr, hrs, hf2, hv2, hS2⟩ + refine ⟨?_, σr, ?_, hRf.frame hP hf2 hv2, hS2⟩ + · rw [hsp] + exact fun h => hnr2 (Det.detAt_bind_fail + ((Det.det_mapMx (fun a => Det.det_removeAndRestartEp a _) y.epQueue).at σ) hnf hmf h) + rw [hsp] + exact ⟨((), σf), hmf, hrs⟩ + +end Loop + +end + +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/EpCancelBadgedTopA.lean b/proofs/Fl4ProofsA64/P3/EpCancelBadgedTopA.lean new file mode 100644 index 000000000..50d665ffc --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/EpCancelBadgedTopA.lean @@ -0,0 +1,77 @@ +/- +P3 (aarch64, A64-P3-IPC gen 4, ruling item 4): **the CNode CancelBadgedSends step** (label 26 of the A64 front end's +numbering, `.cnodeCancelBadgedSends`), as a standalone adapter in the shape of x86's `XCancelBadgedHyp` +(`P3/IpcCancelBadged.xCancelBadged`), on `EpStateRel ∧ SR` over `SchedOpsEA`: FL4 sets the caller `Restart`, then runs +`cancel_badged_sends`; the spec's `cancel_badged_sends` runs from the state after its own restart of the caller. + +* `not_ep_queued` (x86 `IpcRel.not_ep_queued`): a TCB whose state code is below 3 (`Inactive` / `Running` / `Restart`) + is in no endpoint queue (queued threads carry code 3 or 4, `EpObjRel.queue`). +* **`restart_cancel_badged_rel`**: the restart, then gen 4's `cancel_badged_rel` (spec not failing). + +Not wired into `JAtZA` / `CnodeFrontA` (ruling: held until the coordinated S3/IPC merge); the wiring then instantiates +`SR` with `JAtZA`'s scheduler part and takes the caller running (`hrun`, gen 5: `Running`, for `NoBlockE`) from the +decode site, as x86's `cn_cancel_topX`. Hypotheses: `EPlaced`, the caller in `T`, the endpoint in `E`, `hrun`, the spec's restart outcome +`hst`; (gen 7) the endpoint's queued threads on the current core (`hon`, for `cancel_badged_rel`); the interface. No +named premise. +-/ +import Fl4ProofsA64.P3.EpCancelA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif + +noncomputable section + +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {T E R : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {SR : S → Fl4SpecA64.State Unit → Prop} + +/-- A TCB with a state code below 3 is in no endpoint queue (x86 `IpcRel.not_ep_queued`). -/ +theorem not_ep_queued {m : U64 → U64} {σ : Fl4SpecA64.State Unit} (hR : EpStateRel T E R Bf m σ) {t : U64} + (ht : t ∈ T) {x : Fl4SpecA64.Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) (hrun : tsCode x.tcbState < 3) : + ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue := by + intro e he z hz hm + obtain ⟨xs, -, hxs, hB⟩ := (hR.at_ep he hz).queue + rw [hxs, List.mem_map] at hm + obtain ⟨y, hy, ey⟩ := hm + have := specPtr_inj ey + subst this + have hst := (hR.at_tcb ht hk).st + rcases (hB y hy).2 with h | h <;> omega + +set_option maxHeartbeats 4000000 in +/-- **CancelBadgedSends's IPC step** (x86 `xCancelBadged`'s leaf): FL4's restart of the caller then +`cancel_badged_sends p b`, against the spec's `cancel_badged_sends` from the state `σ1` after the spec's restart; the +spec does not fail. -/ +theorem restart_cancel_badged_rel (hO : SchedOpsEA st T E R Bf SR) (hP : EPlaced T E R Bf) (s : S) + (σ : Fl4SpecA64.State Unit) (hR : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) {ct : U64} (hct : ct ∈ T) + {xt : Fl4SpecA64.Tcb} (hkt : σ.kheap (specPtr ct) = some (.tcb xt)) (hrun : xt.tcbState = .running) + {p : U64} (hp : p ∈ E) (b : U64) {σ1 : Fl4SpecA64.State Unit} + (hst : Fl4SpecA64.setThreadState (Z := Unit) (specPtr ct) .restart σ = ({((), σ1)}, False)) + (hon : ∀ z, σ.kheap (specPtr p) = some (.endpoint z) → ∀ t ∈ T, specPtr t ∈ z.epQueue → + L.mem s (affA t) = L.cpu s) : + (do + let s1 ← sched.set_thread_state st s ct sched.TS_RESTART + endpoint.cancel_badged_sends st s1 p b) ⦃ s' => + ¬ (Fl4SpecA64.cancelBadgedSends (Z := Unit) (specPtr p) b.bv σ1).2 ∧ + ∃ σ', ((), σ') ∈ (Fl4SpecA64.cancelBadgedSends (Z := Unit) (specPtr p) b.bv σ1).1 ∧ + EpStateRel T E R Bf (L.mem s') σ' ∧ SR s' σ' ⦄ := by + rw [setThreadState_eq .restart hkt] at hst + have hσ1 : σ1 = stsAct (updK σ (specPtr ct) (.tcb { xt with tcbState := .restart })) (specPtr ct) := + (congrArg Prod.snd (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst hst))).symm + subst hσ1 + have hcode : sched.TS_RESTART.val = tsCode .restart := by unfold sched.TS_RESTART; rfl + refine WP.spec_bind (set_thread_state_rel hO hP s σ hR hSR hct hkt + (not_ep_queued hR hct hkt (by rw [hrun]; decide)) .restart (by decide) sched.TS_RESTART hcode + (NoBlockE.updT hkt _ (Or.inr (Or.inl ⟨rfl, (by rw [hrun]; exact fun h => by cases h), + Or.inl (Or.inl (by rw [hrun]; rfl))⟩)))) ?_ + rintro s1 ⟨hR1, hS1⟩ + refine cancel_badged_rel hO hP s1 _ hR1 hS1 hp b fun z hz t ht hm => + (hO.coreKeep _ _ _ _ hSR hS1).onCore ht (hon z ?_ t ht hm) + rw [stsAct_kheap, updK_kheap] at hz + by_cases hpc : specPtr p = specPtr ct + · rw [hpc, Function.update_self] at hz; cases hz + · rwa [Function.update_of_ne hpc] at hz + +end + +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/EpCancelIpcA.lean b/proofs/Fl4ProofsA64/P3/EpCancelIpcA.lean new file mode 100644 index 000000000..2d325a680 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/EpCancelIpcA.lean @@ -0,0 +1,453 @@ +/- +P3 (aarch64, A64-P3-IPC gen 5, ruling item 4): **`cancel_ipc` of a thread queued on an endpoint refines +`Fl4SpecA64.cancelIpc`** (`Z = Unit`) over `EpStateRel` and the endpoint scheduler interface `SchedOpsEA` (x86 +`P3/IpcCancel`'s `cancel_ipc_recvI`, extended to the send arm). FL4's AArch64 `cancel_ipc` (ipc.rs) is the x86 text. + +FL4 clears the fault word (`clear_fault`), reads the state word, and for a thread blocked on a send / receive reads +its block object, dequeues it (`ep_dequeue`), for a receiver with a reply object unlinks it (`reply_unlink`: the +reply's TCB word, then `set_thread_state Inactive`), then sets it `Inactive` (`set_thread_state`). The spec +(IpcCancel_A `cancel_ipc`) clears `tcb_fault`, then runs `blocked_cancel_ipc` (`tcb_ep_dequeue`, `reply_unlink_tcb`, +`set_thread_state Inactive`). + +**One `stable` step (ruling Q4b).** The fault clear, the dequeue (and the reply's TCB word) and the state word are +all written before the first `schedule_tcb`: `SchedOpsEA.stable` is applied once, from the state before the fault +clear to the state after the state word (`set_thread_state_from`), with `NoBlockE`'s branch 3 (`BlockedOnSend` / +`BlockedOnReceive` → `Inactive`; the fault clear of a sender alone is never presented to `stable`). A receiver with a +reply object is set `Inactive` a second time (blocked_cancel_ipc's own `set_thread_state`): branch 1. + +* spec: **`RepOKA`** (ruling Q3, x86 `SpecFinOutEp.RepOK`: a receiver's reply object points back to it), + `replyUnlinkTcb_eq`, `cancelIpc_blocked`, `bci_none`, `bci_reply`, `epDeq_not_mem`; +* FL4: `TcbRelE.with_fault`, `clear_fault_rel` (the fault word, in `EWrite` since gen 5), `OnlyT` (the step changes + no other TCB, for `NoBlockE.only`), `final_inactive`, `reply_second` / `reply_arm` (split at the `stsAct` state + `reply_unlink` leaves), `cancel_arm`, **`cancel_ipc_queued`**: for `t ∈ T` queued on `e ∈ E` (the case / + placement hypotheses `he`, `hkz`, `hq`, as `ep_dequeue_rel`'s) and `RepOKA σ t`, + `ipc.cancel_ipc st ar s a t ⦃ r => r.2 = a ∧ ∃ σ', cancelIpc (specPtr t) σ = {((), σ')} ∧ EpStateRel … ∧ SR … ⦄`. + +(gen 6) `reply_unlink_rel` (`reply_unlink` alone, factored out of `reply_arm`; `P3/EpCancelAllA` uses it, and now +imports this module: `epDeq_queue`, `tsCode_three` moved here), `notq_updR`, `RepOKA.of_keepO`. + +Not covered (gen 5): the `BlockedOnNotification` arm (`cancel_signal`, on `NtfnStateRel`; its fault clear is +outside `NWrite` / `NtfnStep`, `SchedHypsA.stable` cannot take it) and the `BlockedOnReply` arm (`reply_remove_tcb`, +the SC reply stack); and the threads in no IPC state (only the fault clear). No named premise. +-/ +import Fl4ProofsA64.P3.EpCancelA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif + +noncomputable section + +section Spec +open Fl4SpecA64 + +/-- (gen 6: moved here from `P3/EpCancelAllA`, which now imports this module.) -/ +theorem epDeq_queue (y : Endpoint) (t : ObjRef) : (epDeq y t).epQueue = removeAll t y.epQueue := by + unfold epDeq + split + · rename_i h; rw [h]; rfl + · cases y with + | idleEP => rename_i h; exact absurd rfl h + | sendEP q => rfl + | recvEP q => rfl + +theorem tsCode_three {ts : ThreadState} (h : tsCode ts = 3) : ∃ e r d, ts = .blockedOnReceive e r d := by + rcases ts with _ | _ | _ | _ | _ | _ | _ | _ | ⟨⟨⟩⟩ <;> simp [tsCode] at h ⊢ + +/-- (ruling Q3) **A receiver's reply object points back to it** (l4v `sym_refs` for `reply_tcb`; x86 +`SpecFinOutEp.RepOK`): a spec-level fact of the thread `t`. -/ +def RepOKA (σ : State Unit) (t : ObjRef) : Prop := + ∀ x, σ.kheap t = some (.tcb x) → ∀ ep r d, x.tcbState = .blockedOnReceive ep (some r) d → + ∃ xr, σ.kheap r = some (.reply xr) ∧ xr.replyTcb = some t + +/-- `reply_unlink_tcb t r` of a receiver whose reply points back to it. -/ +theorem replyUnlinkTcb_eq {σ : State Unit} {t r : ObjRef} {xr : Reply} {x : Tcb} + (hkr : σ.kheap r = some (.reply xr)) (hback : xr.replyTcb = some t) (hkt : σ.kheap t = some (.tcb x)) + {ep : ObjRef} {d : ReceiverPayload} (hst : x.tcbState = .blockedOnReceive ep (some r) d) : + replyUnlinkTcb (Z := Unit) t r σ = ({((), stsAct (updK (updK σ r (.reply { xr with replyTcb := none })) t + (.tcb { x with tcbState := .inactive })) t)}, False) := by + have hrt : r ≠ t := by intro h; rw [h, hkt] at hkr; cases hkr + have hkt' : (updK σ r (.reply { xr with replyTcb := none })).kheap t = some (.tcb x) := by + rw [updK_kheap, Function.update_of_ne hrt.symm, hkt] + unfold replyUnlinkTcb + rw [nd_bind_one (getReply_eq hkr), nd_bind_one (assert_true hback σ), nd_bind_one (getThreadState_eq hkt), + nd_bind_one (assert_true (Or.inr ⟨ep, d, hst⟩) σ), nd_bind_one (setReplyTcb_eq none hkr)] + exact setThreadState_eq .inactive hkt' + +/-- `cancel_ipc` of a thread blocked on a send or a receive: the fault clear, then `blocked_cancel_ipc`. -/ +theorem cancelIpc_blocked {σ σ' : State Unit} {t : ObjRef} {x : Tcb} (hk : σ.kheap t = some (.tcb x)) + (ro : Option ObjRef) + (hcase : (∃ e d, x.tcbState = .blockedOnSend e d ∧ ro = none) ∨ (∃ e d, x.tcbState = .blockedOnReceive e ro d)) + (h : blockedCancelIpc (Z := Unit) x.tcbState t ro (updK σ t (.tcb { x with tcbFault := none })) = + ({((), σ')}, False)) : + cancelIpc (Z := Unit) t σ = ({((), σ')}, False) := by + unfold cancelIpc + rw [nd_bind_one (getThreadState_eq hk), nd_bind_one (threadSet_eq _ hk)] + rcases hcase with ⟨e, d, hs, rfl⟩ | ⟨e, d, hs⟩ <;> rw [hs] at h ⊢ <;> exact h + +/-- `blocked_cancel_ipc` without a reply object. -/ +theorem bci_none {σ1 σ2 : State Unit} {t e : ObjRef} {ts : ThreadState} {x1 : Tcb} (hb : epBlocked ts = some e) + (hdq : tcbEpDequeue (Z := Unit) t e σ1 = ({((), σ2)}, False)) (hk2 : σ2.kheap t = some (.tcb x1)) : + blockedCancelIpc (Z := Unit) ts t none σ1 = + ({((), stsAct (updK σ2 t (.tcb { x1 with tcbState := .inactive })) t)}, False) := by + unfold blockedCancelIpc getBlockingObject + rw [nd_bind_one (show Fl4Spec.NonDet.assertOpt (σ := State Unit) (epBlocked ts) σ1 = ({(e, σ1)}, False) by rw [hb]; rfl), + nd_bind_one hdq] + try dsimp only + first + | exact setThreadState_eq _ hk2 + | (rw [nd_pure_bind']; exact setThreadState_eq _ hk2) + +/-- `blocked_cancel_ipc` with a reply object. -/ +theorem bci_reply {σ1 σ2 σ4 : State Unit} {t e r : ObjRef} {ts : ThreadState} {x4 : Tcb} + (hb : epBlocked ts = some e) (hdq : tcbEpDequeue (Z := Unit) t e σ1 = ({((), σ2)}, False)) + (hun : replyUnlinkTcb (Z := Unit) t r σ2 = ({((), σ4)}, False)) (hk4 : σ4.kheap t = some (.tcb x4)) : + blockedCancelIpc (Z := Unit) ts t (some r) σ1 = + ({((), stsAct (updK σ4 t (.tcb { x4 with tcbState := .inactive })) t)}, False) := by + unfold blockedCancelIpc getBlockingObject + rw [nd_bind_one (show Fl4Spec.NonDet.assertOpt (σ := State Unit) (epBlocked ts) σ1 = ({(e, σ1)}, False) by rw [hb]; rfl), + nd_bind_one hdq] + try dsimp only + rw [nd_bind_one hun] + exact setThreadState_eq _ hk4 + +/-- The dequeued thread is not in the endpoint's queue after. -/ +theorem epDeq_not_mem {z : Endpoint} {xs : List U64} (hq : z.epQueue = xs.map specPtr) (hnd : xs.Nodup) (t : U64) : + specPtr t ∉ (epDeq z (specPtr t)).epQueue := by + rw [epDeq_queue, hq, removeAll_map hnd, List.mem_map] + rintro ⟨y, hy, ey⟩ + have : y = t := specPtr_inj ey + subst this + exact hnd.not_mem_erase hy + +/-- (gen 5) The step changes no TCB but the one at `p`. -/ +def OnlyT (σ σ' : State Unit) (p : ObjRef) : Prop := + ∀ q, q ≠ p → ∀ x' y', σ.kheap q = some (.tcb x') → σ'.kheap q = some (.tcb y') → y' = x' + +theorem OnlyT.refl (σ : State Unit) (p : ObjRef) : OnlyT σ σ p := fun q _ x' y' h1 h2 => by + rw [h1] at h2; injection h2 with h2; injection h2 with h2; exact h2.symm + +theorem OnlyT.updO {σ σ' : State Unit} {p : ObjRef} (h : OnlyT σ σ' p) (q : ObjRef) {o : KernelObject} + (ho : ∀ x, o ≠ .tcb x) : OnlyT σ (updK σ' q o) p := by + intro q' hq' x' y' h1 h2 + by_cases e : q' = q + · subst e; rw [updK_kheap, Function.update_self] at h2; injection h2 with h2; exact absurd h2 (ho y') + · rw [updK_kheap, Function.update_of_ne e] at h2; exact h q' hq' x' y' h1 h2 + +theorem OnlyT.updP {σ σ' : State Unit} {p : ObjRef} (h : OnlyT σ σ' p) (y : Tcb) : OnlyT σ (updK σ' p (.tcb y)) p := by + intro q' hq' x' y' h1 h2 + rw [updK_kheap, Function.update_of_ne hq'] at h2; exact h q' hq' x' y' h1 h2 + +/-- (gen 6) `RepOKA` of a thread `p ≠ t` across a step that keeps the other TCBs' states and the replies not +pointing to `t` (`KeepO`). -/ +theorem RepOKA.of_keepO {σ σ' : State Unit} {p t : ObjRef} (h : RepOKA σ p) (hk : KeepO σ σ' t) (hpt : p ≠ t) + {x : Tcb} (hx : σ.kheap p = some (.tcb x)) : RepOKA σ' p := by + intro x' hx' ep r d hs + obtain ⟨y, hy, e⟩ := hk.1 p x hpt hx + rw [hx'] at hy; injection hy with hy; injection hy with hy; subst hy + obtain ⟨xr, hkr, hb⟩ := h x hx ep r d (e.symm.trans hs) + obtain ⟨yr, hyr, e2⟩ := hk.2 r xr hkr (by rw [hb]; exact fun h' => hpt (Option.some.inj h')) + exact ⟨yr, hyr, e2.trans hb⟩ + +end Spec + +section +variable {T E R : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + +/-- A related TCB with its fault cleared, from memory that agrees on all of its fields but the fault-type word, which +is 0. -/ +theorem TcbRelE.with_fault {m m' : U64 → U64} {t : U64} {x : Fl4SpecA64.Tcb} (ht : TcbAddr t) + (h : TcbRelE R m t x) (h0 : m' (tFaultA t) = 0#u64) + (hA : Agree m m' (fun b => TF t b ∧ b.val ≠ (tFaultA t).val)) : + TcbRelE R m' t { x with tcbFault := none } := by + have g : ∀ {b}, Aligned b ∧ TF t b → b.val ≠ (tFaultA t).val → m' b = m b := fun hb hne => hA _ hb.1 ⟨hb.2, hne⟩ + have v := val_fault ht + refine ⟨?_, fun e he => ?_, fun e d he => ?_, fun e r d he => ?_, fun r he => ?_, fun r => ?_, ?_, + ⟨fun _ => rfl, fun _ => h0⟩, ⟨fun e => (by rw [h0] at e; cases e), fun ⟨b, e⟩ => (by cases e)⟩, ?_, ?_⟩ + · rw [g (a_st ht) (by rw [val_st ht, v]; omega)]; exact h.st + · rw [g (a_blk ht) (by rw [val_blk ht, v]; omega)]; exact h.blk e he + · rw [g (a_badge ht) (by rw [val_badge ht, v]; omega), g (a_flags ht) (by rw [val_flags ht, v]; omega)] + exact h.send e d he + · rw [g (a_reply ht) (by rw [val_reply ht, v]; omega)]; exact h.recv e r d he + · rw [g (a_reply ht) (by rw [val_reply ht, v]; omega)]; exact h.reply r he + · rw [g (a_reg ht r) (by rw [regA_val ht (regIdxA_lt r), v]; have := regIdxA_lt r; omega)]; exact h.regs r + · rw [g (a_prio ht) (by rw [val_prio ht, v]; omega)]; exact h.prio + · rw [g (a_bound ht) (by rw [val_bound ht, v]; omega)]; exact h.bound + · show m' (tFaultA t) = 0#u64 + exact h0 + +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + {SR : S → Fl4SpecA64.State Unit → Prop} + +/-- **`clear_fault`** (the fault-type word, in `EWrite` since gen 5): related to the spec's TCB with no fault. -/ +theorem clear_fault_rel (hP : EPlaced T E R Bf) (s : S) (σ : Fl4SpecA64.State Unit) + (hR : EpStateRel T E R Bf (L.mem s) σ) {t : U64} (ht : t ∈ T) {x : Fl4SpecA64.Tcb} + (hk : σ.kheap (specPtr t) = some (.tcb x)) : + EpStateRel T E R Bf (L.mem (L.wr s (tFaultA t) 0#u64)) (updK σ (specPtr t) (.tcb { x with tcbFault := none })) ∧ + WritesIn st s (L.wr s (tFaultA t) 0#u64) (EWrite T E R Bf) ∧ + EpStep σ (updK σ (specPtr t) (.tcb { x with tcbFault := none })) ∧ + Agree (L.mem s) (L.mem (L.wr s (tFaultA t) 0#u64)) (fun b => b.val ≠ (tFaultA t).val) := by + have hta := hP.tcb t ht + have hx := hR.at_tcb ht hk + have ha := a_fault hta + have hag := agree_wr (L := L) (s := s) (v := 0#u64) ha.1 + have vf := val_fault hta + refine ⟨hR.upd_tcb hP ht _ (hag.mono fun b hb => tf_ne_of hb.2 ha.2) + (Or.inr ⟨hag _ (a_st hta).1 (by dsimp only; rw [val_st hta, vf]; omega), hag _ (a_blk hta).1 (by dsimp only; rw [val_blk hta, vf]; omega)⟩) + (hx.with_fault hta (rd_wr_eq ha.1) (hag.mono fun b hb => hb.2)), ?_, EpStep.tcbF hk x.tcbState x.tcbArch none, hag⟩ + refine (writesIn_wr ha.1).mono fun b hb => ?_ + subst hb + refine Or.inr (Or.inr (Or.inl ⟨t, ht, Or.inl ⟨ha.2.1, ?_⟩⟩)) + rw [vf]; unfold TWOffN; omega + +/-- **The final `set_thread_state t Inactive`** after endpoint writes from `(s0, σ0)` that changed no other TCB, of a +thread that was blocked (not runnable, not idle) at `σ0`: one `stable` step (`NoBlockE` branch 3). -/ +theorem final_inactive (hO : SchedOpsEA st T E R Bf SR) (hP : EPlaced T E R Bf) {s0 : S} + {σ0 : Fl4SpecA64.State Unit} (hSR0 : SR s0 σ0) (s : S) (σ : Fl4SpecA64.State Unit) + (hR : EpStateRel T E R Bf (L.mem s) σ) (hW0 : WritesIn st s0 s (EWrite T E R Bf)) (hS0 : EpStep σ0 σ) + {t : U64} (ht : t ∈ T) {x0 x : Fl4SpecA64.Tcb} (hk0 : σ0.kheap (specPtr t) = some (.tcb x0)) + (hk : σ.kheap (specPtr t) = some (.tcb x)) (hO0 : OnlyT σ0 σ (specPtr t)) + (hblk : Fl4SpecA64.runnable x0.tcbState = false ∧ x0.tcbState ≠ .idleThreadState) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) : + sched.set_thread_state st s t sched.TS_INACTIVE ⦃ s' => + EpStateRel T E R Bf (L.mem s') (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := .inactive })) (specPtr t)) ∧ + SR s' (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := .inactive })) (specPtr t)) ⦄ := + set_thread_state_from hO hP hSR0 s σ hR hW0 hS0 ht hk hq .inactive (by decide) sched.TS_INACTIVE + (by unfold sched.TS_INACTIVE; rfl) + (NoBlockE.only hk0 (by rw [updK_kheap, Function.update_self]) (hO0.updP _) (Or.inr (Or.inr ⟨rfl, hblk⟩))) + +/-- The receive arm's second `set_thread_state t Inactive` (blocked_cancel_ipc's own), from the state `reply_unlink` +leaves (a separate declaration at that `stsAct` state). -/ +theorem reply_second (hO : SchedOpsEA st T E R Bf SR) (hP : EPlaced T E R Bf) (a : A) {s4 : S} + {σ3 : Fl4SpecA64.State Unit} {t : U64} (ht : t ∈ T) {x3 : Fl4SpecA64.Tcb} + (hk3 : σ3.kheap (specPtr t) = some (.tcb x3)) + (hq3 : ∀ e ∈ E, ∀ z, σ3.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) + (hR4 : EpStateRel T E R Bf (L.mem s4) (stsAct (updK σ3 (specPtr t) (.tcb { x3 with tcbState := .inactive })) + (specPtr t))) + (hS4 : SR s4 (stsAct (updK σ3 (specPtr t) (.tcb { x3 with tcbState := .inactive })) (specPtr t))) : + (do + let s5 ← sched.set_thread_state st s4 t sched.TS_INACTIVE + ok (s5, a)) ⦃ r => r.2 = a ∧ EpStateRel T E R Bf (L.mem r.1) + (stsAct (updK (stsAct (updK σ3 (specPtr t) (.tcb { x3 with tcbState := .inactive })) (specPtr t)) (specPtr t) + (.tcb { { x3 with tcbState := .inactive } with tcbState := .inactive })) (specPtr t)) ∧ + SR r.1 (stsAct (updK (stsAct (updK σ3 (specPtr t) (.tcb { x3 with tcbState := .inactive })) (specPtr t)) (specPtr t) + (.tcb { { x3 with tcbState := .inactive } with tcbState := .inactive })) (specPtr t)) ⦄ := by + obtain ⟨hk4, hq4, -, -⟩ := sts_facts hk3 hq3 .inactive + refine WP.spec_bind (set_thread_state_rel hO hP s4 _ hR4 hS4 ht hk4 hq4 .inactive (by decide) sched.TS_INACTIVE + (by unfold sched.TS_INACTIVE; rfl) (NoBlockE.updT hk4 _ (Or.inl ⟨rfl, id⟩))) ?_ + rintro s5 ⟨hR5, hS5⟩ + rw [WP.spec_ok] + exact ⟨rfl, hR5, hS5⟩ + +/-- (gen 6) Writing a reply keeps a thread out of the endpoint queues. -/ +theorem notq_updR {σ : Fl4SpecA64.State Unit} {t : U64} + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) (i : U64) + (o : Fl4SpecA64.Reply) : + ∀ e ∈ E, ∀ z, (updK σ (specPtr i) (.reply o)).kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue := by + intro e he z hz + have hne : specPtr e ≠ specPtr i := by + intro h; rw [h, updK_kheap, Function.update_self] at hz; cases hz + rw [updK_kheap, Function.update_of_ne hne] at hz + exact hq e he z hz + +/-- (gen 6) **`reply_unlink` of a receiver whose reply points back to it**, after endpoint writes from `(s0, σ0)` that +changed no other TCB: the reply's TCB word, then `set_thread_state Inactive` (one `stable` step from `(s0, σ0)`). -/ +theorem reply_unlink_rel (hO : SchedOpsEA st T E R Bf SR) (hP : EPlaced T E R Bf) {s0 : S} + {σ0 : Fl4SpecA64.State Unit} (hSR0 : SR s0 σ0) {s2 : S} {σ2 : Fl4SpecA64.State Unit} + (hR2 : EpStateRel T E R Bf (L.mem s2) σ2) (hW02 : WritesIn st s0 s2 (EWrite T E R Bf)) (hS02 : EpStep σ0 σ2) + {t : U64} (ht : t ∈ T) {x0 x1 : Fl4SpecA64.Tcb} (hk0 : σ0.kheap (specPtr t) = some (.tcb x0)) + (hk2 : σ2.kheap (specPtr t) = some (.tcb x1)) (hO2 : OnlyT σ0 σ2 (specPtr t)) + (hblk : Fl4SpecA64.runnable x0.tcbState = false ∧ x0.tcbState ≠ .idleThreadState) + (hq2 : ∀ e ∈ E, ∀ z, σ2.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) + {i : U64} (hiR : i ∈ R) {xr : Fl4SpecA64.Reply} (hkr2 : σ2.kheap (specPtr i) = some (.reply xr)) : + ipc.reply_unlink st s2 i t ⦃ s3 => + EpStateRel T E R Bf (L.mem s3) (stsAct (updK (updK σ2 (specPtr i) (.reply { xr with replyTcb := none })) + (specPtr t) (.tcb { x1 with tcbState := .inactive })) (specPtr t)) ∧ + SR s3 (stsAct (updK (updK σ2 (specPtr i) (.reply { xr with replyTcb := none })) + (specPtr t) (.tcb { x1 with tcbState := .inactive })) (specPtr t)) ⦄ := by + have hit : specPtr i ≠ specPtr t := by intro h; rw [h, hk2] at hkr2; cases hkr2 + have hal := (a_rtcb hP hiR).1 + have hag := agree_wr (L := L) (s := s2) (v := 0#u64) hal + have hR3 : EpStateRel T E R Bf (L.mem (L.wr s2 (rTcbA i) 0#u64)) + (updK σ2 (specPtr i) (.reply { xr with replyTcb := none })) := + hR2.upd_reply hP hiR _ (hag.mono fun b hb => by rw [rTcbA_val]; exact hb.2) (by rw [rd_wr_eq hal, optRef_zero]) + have hW3 : WritesIn st s2 (L.wr s2 (rTcbA i) 0#u64) (EWrite T E R Bf) := + (writesIn_wr hal).mono fun b hb => by subst hb; exact Or.inr (Or.inl ⟨i, hiR, rTcbA_val i⟩) + have hk3 : (updK σ2 (specPtr i) (.reply { xr with replyTcb := none })).kheap (specPtr t) = some (.tcb x1) := by + rw [updK_kheap, Function.update_of_ne hit.symm, hk2] + have hq3 := notq_updR (E := E) hq2 i { xr with replyTcb := none } + unfold ipc.reply_unlink + step with obj.Reply.set_tcb.spec as ⟨s3, hs3⟩ + subst hs3 + exact final_inactive hO hP hSR0 _ _ hR3 (WritesIn.ew hW02 hW3) (hS02.trans (EpStep.reply hkr2 none)) + ht hk0 hk3 (hO2.updO _ fun _ h => by cases h) hblk hq3 + +/-- The receive arm with a reply object, after the dequeue: `reply_unlink` (`reply_unlink_rel`), then the second +`set_thread_state Inactive`. -/ +theorem reply_arm (hO : SchedOpsEA st T E R Bf SR) (hP : EPlaced T E R Bf) (a : A) {s0 : S} + {σ0 : Fl4SpecA64.State Unit} (hSR0 : SR s0 σ0) {s2 : S} {σ2 : Fl4SpecA64.State Unit} + (hR2 : EpStateRel T E R Bf (L.mem s2) σ2) (hW02 : WritesIn st s0 s2 (EWrite T E R Bf)) (hS02 : EpStep σ0 σ2) + {t : U64} (ht : t ∈ T) {x0 x1 : Fl4SpecA64.Tcb} (hk0 : σ0.kheap (specPtr t) = some (.tcb x0)) + (hk2 : σ2.kheap (specPtr t) = some (.tcb x1)) (hO2 : OnlyT σ0 σ2 (specPtr t)) + (hblk : Fl4SpecA64.runnable x0.tcbState = false ∧ x0.tcbState ≠ .idleThreadState) + (hq2 : ∀ e ∈ E, ∀ z, σ2.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) + {i : U64} (hiR : i ∈ R) {xr : Fl4SpecA64.Reply} (hkr2 : σ2.kheap (specPtr i) = some (.reply xr)) + (hback : xr.replyTcb = some (specPtr t)) {ep : Fl4SpecA64.ObjRef} {d : Fl4SpecA64.ReceiverPayload} + (hst1 : x1.tcbState = .blockedOnReceive ep (some (specPtr i)) d) : + (do + let s3 ← ipc.reply_unlink st s2 i t + let s4 ← sched.set_thread_state st s3 t sched.TS_INACTIVE + ok (s4, a)) ⦃ r => r.2 = a ∧ ∃ σ4 x4, + Fl4SpecA64.replyUnlinkTcb (Z := Unit) (specPtr t) (specPtr i) σ2 = ({((), σ4)}, False) ∧ + σ4.kheap (specPtr t) = some (.tcb x4) ∧ + EpStateRel T E R Bf (L.mem r.1) (stsAct (updK σ4 (specPtr t) (.tcb { x4 with tcbState := .inactive })) (specPtr t)) ∧ + SR r.1 (stsAct (updK σ4 (specPtr t) (.tcb { x4 with tcbState := .inactive })) (specPtr t)) ⦄ := by + have hit : specPtr i ≠ specPtr t := by intro h; rw [h, hk2] at hkr2; cases hkr2 + have hk3 : (updK σ2 (specPtr i) (.reply { xr with replyTcb := none })).kheap (specPtr t) = some (.tcb x1) := by + rw [updK_kheap, Function.update_of_ne hit.symm, hk2] + have hq3 := notq_updR (E := E) hq2 i { xr with replyTcb := none } + refine WP.spec_bind (reply_unlink_rel hO hP hSR0 hR2 hW02 hS02 ht hk0 hk2 hO2 hblk hq2 hiR hkr2) ?_ + rintro s4 ⟨hR4, hS4⟩ + refine WP.spec_mono (reply_second hO hP a ht hk3 hq3 hR4 hS4) ?_ + rintro r ⟨h1, h2, h3⟩ + exact ⟨h1, _, _, replyUnlinkTcb_eq hkr2 hback hk2 hst1, by rw [stsAct_kheap, updK_kheap, Function.update_self], + h2, h3⟩ + +/-- **The send / receive arm of `cancel_ipc`** after the fault clear and the state read `w`. -/ +theorem cancel_arm (hO : SchedOpsEA st T E R Bf SR) (hP : EPlaced T E R Bf) (s : S) (a : A) + (σ : Fl4SpecA64.State Unit) (hR : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) {t : U64} (ht : t ∈ T) + {x : Fl4SpecA64.Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) {e : U64} (he : e ∈ E) + {z : Fl4SpecA64.Endpoint} (hkz : σ.kheap (specPtr e) = some (.endpoint z)) (hq : specPtr t ∈ z.epQueue) + (hrep : RepOKA σ (specPtr t)) + (hR1 : EpStateRel T E R Bf (L.mem (L.wr s (tFaultA t) 0#u64)) (updK σ (specPtr t) (.tcb { x with tcbFault := none }))) + (hW1 : WritesIn st s (L.wr s (tFaultA t) 0#u64) (EWrite T E R Bf)) + (hS1 : EpStep σ (updK σ (specPtr t) (.tcb { x with tcbFault := none }))) + (hb1 : L.mem (L.wr s (tFaultA t) 0#u64) (tBlockA t) = e) {w : U64} + (hcase : (∃ d, x.tcbState = .blockedOnSend (specPtr e) d ∧ w ≠ sched.TS_BLOCKED_ON_RECV) ∨ + (∃ ro d, x.tcbState = .blockedOnReceive (specPtr e) ro d ∧ w = sched.TS_BLOCKED_ON_RECV)) : + (do + let ep ← obj.Tcb.block_obj st t (L.wr s (tFaultA t) 0#u64) + let s2 ← endpoint.ep_dequeue st (L.wr s (tFaultA t) 0#u64) ep t + let s3 ← + if w = sched.TS_BLOCKED_ON_RECV + then + do + let i ← obj.Tcb.reply_obj st t s2 + if i != 0#u64 + then ipc.reply_unlink st s2 i t + else ok s2 + else ok s2 + let s4 ← sched.set_thread_state st s3 t sched.TS_INACTIVE + ok (s4, a)) ⦃ r => r.2 = a ∧ ∃ σ', Fl4SpecA64.cancelIpc (Z := Unit) (specPtr t) σ = ({((), σ')}, False) ∧ + EpStateRel T E R Bf (L.mem r.1) σ' ∧ SR r.1 σ' ⦄ := by + have hta := hP.tcb t ht + have hte : specPtr e ≠ specPtr t := by intro h; rw [h, hk] at hkz; cases hkz + have hkz1 : (updK σ (specPtr t) (.tcb { x with tcbFault := none })).kheap (specPtr e) = some (.endpoint z) := by + rw [updK_kheap, Function.update_of_ne hte, hkz] + obtain ⟨xs, hQ, hxs, -⟩ := (hR.at_ep he hkz).queue + have hblk : Fl4SpecA64.runnable x.tcbState = false ∧ x.tcbState ≠ .idleThreadState := by + rcases hcase with ⟨d, hs, -⟩ | ⟨ro, d, hs, -⟩ <;> rw [hs] <;> exact ⟨rfl, fun h => by cases h⟩ + step with obj.Tcb.block_obj.spec as ⟨ep, hep⟩ + have hep' : ep = e := hep.trans hb1 + subst hep' + refine WP.spec_bind (ep_dequeue_raw hP _ _ hR1 he hkz1 hq) ?_ + rintro s2 ⟨hdq, hR2, hW2, hS2, hA2⟩ + have hk2 : (updK (updK σ (specPtr t) (.tcb { x with tcbFault := none })) (specPtr ep) + (.endpoint (epDeq z (specPtr t)))).kheap (specPtr t) = some (.tcb { x with tcbFault := none }) := by + rw [updK_kheap, Function.update_of_ne hte.symm, updK_kheap, Function.update_self] + have hkz2 : (updK (updK σ (specPtr t) (.tcb { x with tcbFault := none })) (specPtr ep) + (.endpoint (epDeq z (specPtr t)))).kheap (specPtr ep) = some (.endpoint (epDeq z (specPtr t))) := by + rw [updK_kheap, Function.update_self] + have hb2 : L.mem s2 (tBlockA t) = ep := (hA2 _ (a_blk hta).1 ⟨t, ht, (a_blk hta).2⟩).trans hb1 + have hq2 := dequeued_not_queued hR2 he hb2 hkz2 (epDeq_not_mem hxs hQ.nodup t) + have hO2 : OnlyT σ (updK (updK σ (specPtr t) (.tcb { x with tcbFault := none })) (specPtr ep) + (.endpoint (epDeq z (specPtr t)))) (specPtr t) := + ((OnlyT.refl σ _).updP _).updO _ fun _ h => by cases h + have hW02 := WritesIn.ew hW1 hW2 + have hS02 := hS1.trans hS2 + rcases hcase with ⟨d, hs, hwr⟩ | ⟨ro, d, hs, hwr⟩ + · simp only [hwr, ↓reduceIte, bind_ok] + refine WP.spec_bind (final_inactive hO hP hSR _ _ hR2 hW02 hS02 ht hk hk2 hO2 hblk hq2) ?_ + rintro s4 ⟨hR4, hS4⟩ + rw [WP.spec_ok] + exact ⟨rfl, _, cancelIpc_blocked hk none (Or.inl ⟨_, d, hs, rfl⟩) (bci_none (by rw [hs]; rfl) hdq hk2), hR4, hS4⟩ + · simp only [hwr, ↓reduceIte] + have hx2 := hR2.at_tcb ht hk2 + have hrec := hx2.recv (specPtr ep) ro d hs + step with obj.Tcb.reply_obj.spec as ⟨i, hi⟩ + have hi' : i = L.mem s2 (tReplyA t) := hi + by_cases hi0 : i = 0#u64 + · have hro : ro = none := by rw [← hrec.1, ← hi', hi0, optRef_zero] + subst hro + simp only [hi0, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte, bind_ok] + refine WP.spec_bind (final_inactive hO hP hSR _ _ hR2 hW02 hS02 ht hk hk2 hO2 hblk hq2) ?_ + rintro s4 ⟨hR4, hS4⟩ + rw [WP.spec_ok] + exact ⟨rfl, _, cancelIpc_blocked hk none (Or.inr ⟨_, d, hs⟩) (bci_none (by rw [hs]; rfl) hdq hk2), hR4, hS4⟩ + · have hro : ro = some (specPtr i) := by rw [← hrec.1, ← hi', optRef_ne hi0] + subst hro + have hiR : i ∈ R := by rw [hi']; exact hrec.2 (by simp) + obtain ⟨xr, hkr, hback⟩ := hrep x hk _ _ d hs + have hit : specPtr i ≠ specPtr t := by intro h; rw [h, hk] at hkr; cases hkr + have hie : specPtr i ≠ specPtr ep := by intro h; rw [h, hkz] at hkr; cases hkr + have hkr2 : (updK (updK σ (specPtr t) (.tcb { x with tcbFault := none })) (specPtr ep) + (.endpoint (epDeq z (specPtr t)))).kheap (specPtr i) = some (.reply xr) := by + rw [updK_kheap, Function.update_of_ne hie, updK_kheap, Function.update_of_ne hit, hkr] + have hnz : (i != 0#u64) = true := by simpa using hi0 + simp only [hnz, ↓reduceIte] + refine WP.spec_mono (reply_arm hO hP a hSR hR2 hW02 hS02 ht hk hk2 hO2 hblk hq2 hiR hkr2 hback hs) ?_ + rintro r ⟨h1, σ4, x4, hun, hk4, hR5, hS5⟩ + exact ⟨h1, _, cancelIpc_blocked hk _ (Or.inr ⟨_, d, hs⟩) (bci_reply (by rw [hs]; rfl) hdq hun hk4), hR5, hS5⟩ + +set_option maxHeartbeats 4000000 in +/-- **`cancel_ipc` of a thread queued on an endpoint refines `cancel_ipc`** (send / receive arms; one spec outcome), +from `RepOKA` for the thread (ruling Q3) and the case / placement hypotheses `he` / `hkz` / `hq`. -/ +theorem cancel_ipc_queued (hO : SchedOpsEA st T E R Bf SR) (hP : EPlaced T E R Bf) (s : S) (a : A) + (σ : Fl4SpecA64.State Unit) (hR : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) {t : U64} (ht : t ∈ T) + {x : Fl4SpecA64.Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) {e : U64} (he : e ∈ E) + {z : Fl4SpecA64.Endpoint} (hkz : σ.kheap (specPtr e) = some (.endpoint z)) (hq : specPtr t ∈ z.epQueue) + (hrep : RepOKA σ (specPtr t)) : + ipc.cancel_ipc st ar s a t ⦃ r => r.2 = a ∧ ∃ σ', Fl4SpecA64.cancelIpc (Z := Unit) (specPtr t) σ = ({((), σ')}, False) ∧ + EpStateRel T E R Bf (L.mem r.1) σ' ∧ SR r.1 σ' ⦄ := by + have hta := hP.tcb t ht + obtain ⟨xs, -, hxs, hB⟩ := (hR.at_ep he hkz).queue + have htx : t ∈ xs := by + rw [hxs, List.mem_map] at hq + obtain ⟨y, hy, ey⟩ := hq + rw [← specPtr_inj ey]; exact hy + obtain ⟨hbe, hc⟩ := hB t htx + have hx := hR.at_tcb ht hk + obtain ⟨hR1, hW1, hS1, hag⟩ := clear_fault_rel hP s σ hR ht hk + have vf := val_fault hta + have hb1 : L.mem (L.wr s (tFaultA t) 0#u64) (tBlockA t) = e := + (hag _ (a_blk hta).1 (by dsimp only; rw [val_blk hta, vf]; omega)).trans hbe + have hblkE : ∀ e', Fl4SpecA64.epBlocked x.tcbState = some e' → e' = specPtr e := fun e' h => by + rw [← hx.blk e' h, hbe] + unfold ipc.cancel_ipc faults.clear_fault + step with obj.Tcb.set_fault_type.spec as ⟨s1, hs1⟩ + have e1 : s1 = L.wr s (tFaultA t) 0#u64 := by rw [hs1]; unfold fl4_abi.consts.fault.NullFault; rfl + subst e1 + step with obj.Tcb.state.spec as ⟨w, hw⟩ + have hw' : w.val = tsCode x.tcbState := by + rw [hw, hag _ (a_st hta).1 (by dsimp only; rw [val_st hta, vf]; omega)]; exact hx.st + rcases hc with h3 | h4 + · obtain ⟨e', ro, d, hs⟩ := tsCode_three (hx.st.symm.trans h3) + have he' : e' = specPtr e := hblkE e' (by rw [hs]; rfl) + subst he' + have hwR : w = sched.TS_BLOCKED_ON_RECV := UScalar.eq_of_val_eq (by rw [hw', hs, tsVals.2.2.1]; rfl) + have hwS : w ≠ sched.TS_BLOCKED_ON_SEND := by rw [hwR, tsVals.2.2.1, tsVals.2.2.2.1]; decide + rw [if_neg hwS, if_pos hwR] + exact cancel_arm hO hP s a σ hR hSR ht hk he hkz hq hrep hR1 hW1 hS1 hb1 (Or.inr ⟨ro, d, hs, hwR⟩) + · obtain ⟨e', d, hs⟩ := tsCode_four (hx.st.symm.trans h4) + have he' : e' = specPtr e := hblkE e' (by rw [hs]; rfl) + subst he' + have hwS : w = sched.TS_BLOCKED_ON_SEND := UScalar.eq_of_val_eq (by rw [hw', hs, tsVals.2.2.2.1]; rfl) + have hwR : w ≠ sched.TS_BLOCKED_ON_RECV := by rw [hwS, tsVals.2.2.1, tsVals.2.2.2.1]; decide + rw [if_pos hwS] + exact cancel_arm hO hP s a σ hR hSR ht hk he hkz hq hrep hR1 hW1 hS1 hb1 (Or.inl ⟨d, hs, hwR⟩) + +end + +end + +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/EpCopyMrsA.lean b/proofs/Fl4ProofsA64/P3/EpCopyMrsA.lean new file mode 100644 index 000000000..951095d4c --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/EpCopyMrsA.lean @@ -0,0 +1,530 @@ +/- +P3 (aarch64, A64-P3-IPC gen 8, ruling item 2): **FL4's `copy_mrs` against l4v `copy_mrs`** (x86 `P3/EpXferSpec`'s +`copyRegsTcb` / `copy_regs_eq` / `copyMrs_noRecvBuf`, `P3/EpBufSpec`'s `copyBufMem` / `mapM_copy` / `copyMrs_bufs` / +`copyMrs_noSendBuf`, `P3/EpXfer`'s `copy_mrs_loop0_spec` / `copy_mrs_norecv` and `P3/EpXferBuf`'s +`copy_mrs_loop1_rel` / `loop0_rel` / `copy_mrs_rel`, ported to the A64 `EpStateRel` / `EpBufA` shapes; FL4's AArch64 +`msg::copy_mrs` is the x86 text with `X2..X5` as the inline registers, extraction diffed). + +* Spec side: `copyRegsTcbA` (the receiver with the sender's registers `rs`), **`copy_regs_eqA`** (the register loop: + one outcome), `copyBufMemA` / `bufIdxA` / **`mapM_copyA`** (the buffer loop: one outcome, aligned words, the + receiver's words in user frames), **`copyMrs_bufsA`**, **`copyMrs_noSendBufA`**, **`copyMrs_noRecvBufA`**. +* FL4: **`copy_mrs_loop0_specA`** (the first `min n 4` message registers of the sender go to the receiver, nothing + else written but those registers), **`copy_mrs_norecvA`** (no receive buffer: the first loop only), + `TcbRelE.with_regsA`, **`loop0_relA`** (the relation after the first loop, against `copyRegsTcbA`), + **`copy_mrs_loop1_relA`** (the buffer words `5 .. n`, both buffers tracked: `bs ∈ Bf`, `br ∈ Bf`, against + `copyBufMemA`), **`copy_mrs_relA`** (with a receive buffer: one spec outcome, related, `SR` kept). + +Hypotheses: `EPlaced`, `snd ∈ T`, `rcv ∈ T`, `snd ≠ rcv`, the buffers tracked (`bs ∈ Bf`, `br ∈ Bf`, nonzero: the +`BufCover` / `lookup_buf_full` facts at the decode site), the receiver's buffer words in user frames (`hu`, as x86's +`copy_mrs_rel`), `n ≤ 120`; the interface `SchedOpsEA` (`stable`). No named premise. +-/ +import Fl4ProofsA64.P3.EpSetMrA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif + +noncomputable section + +/-! ## The spec's `copy_mrs` -/ + +section Spec +open Fl4SpecA64 + +theorem updK_selfA {σ : State Unit} {p : ObjRef} {o : KernelObject} (hk : σ.kheap p = some o) : updK σ p o = σ := by + unfold updK + rw [show Function.update σ.kheap p (some o) = σ.kheap from by rw [← hk, Function.update_eq_self]] + +/-- The receiver `x` with the registers `rs` taken from `vals` (x86 `copyRegsTcb`). -/ +def copyRegsTcbA (x : Tcb) (vals : Register → Word) (rs : List Register) : Tcb := + rs.foldl (fun acc r => setReg acc r (vals r)) x + +theorem copyRegsTcbA_regs (x : Tcb) (vals : Register → Word) : + ∀ rs : List Register, ∀ r, (copyRegsTcbA x vals rs).tcbArch.tcbContext.userRegs r = + if r ∈ rs then vals r else x.tcbArch.tcbContext.userRegs r + | [], r => by simp [copyRegsTcbA] + | r' :: rs, r => by + show (copyRegsTcbA (setReg x r' (vals r')) vals rs).tcbArch.tcbContext.userRegs r = _ + rw [copyRegsTcbA_regs _ vals rs r] + by_cases h : r ∈ rs + · rw [if_pos h, if_pos (List.mem_cons_of_mem _ h)] + · rw [if_neg h, setReg_regs] + by_cases e : r = r' + · subst e; rw [Function.update_self, if_pos List.mem_cons_self] + · rw [Function.update_of_ne e, if_neg (by simp [e, h])] + +theorem copyRegsTcbA_other (x : Tcb) (vals : Register → Word) : + ∀ rs : List Register, copyRegsTcbA x vals rs = { x with tcbArch := (copyRegsTcbA x vals rs).tcbArch } + | [] => rfl + | r' :: rs => by + show copyRegsTcbA (setReg x r' (vals r')) vals rs = _ + rw [copyRegsTcbA_other _ vals rs] + rfl + +/-- Copying the registers `rs` from the sender to the receiver (x86 `copy_regs_eq`). -/ +theorem copy_regs_eqA {σ : State Unit} {snd rcv : ObjRef} {xs xr : Tcb} (hne : snd ≠ rcv) + (hks : σ.kheap snd = some (.tcb xs)) (hkr : σ.kheap rcv = some (.tcb xr)) : + ∀ rs : List Register, + Fl4Spec.NonDet.mapM (fun r => do + let v ← asUser (Z := Unit) snd <| getRegister r + asUser rcv <| setRegister r v) rs σ = + ({(rs.map fun _ => (), updK σ rcv (.tcb (copyRegsTcbA xr xs.tcbArch.tcbContext.userRegs rs)))}, False) + | [] => by + show (pure [] : Fl4Spec.NonDet (State Unit) (List Unit)) σ = ({([], updK σ rcv (.tcb xr))}, False) + rw [nd_pure_apply', updK_selfA hkr] + | r :: rs => by + unfold Fl4Spec.NonDet.mapM + rw [nd_bind_one' (show (do + let v ← asUser (Z := Unit) snd <| getRegister r + asUser rcv <| setRegister r v) σ = ({((), updK σ rcv (.tcb (setReg xr r (xs.tcbArch.tcbContext.userRegs r))))}, + False) by + rw [nd_bind_one (asUser_getRegister_eqA hks r), asUser_setRegister_eq _ _ hkr])] + have hks' : (updK σ rcv (.tcb (setReg xr r (xs.tcbArch.tcbContext.userRegs r)))).kheap snd = some (.tcb xs) := by + simp [hne, hks] + have hkr' : (updK σ rcv (.tcb (setReg xr r (xs.tcbArch.tcbContext.userRegs r)))).kheap rcv = + some (.tcb (setReg xr r (xs.tcbArch.tcbContext.userRegs r))) := by simp + rw [nd_bind_one' (copy_regs_eqA hne hks' hkr' rs)] + show ({_}, False) = _ + simp only [updK, Function.update_idem] + rfl + +/-- User memory after copying the buffer words `xs` from `sb` to `rb` (x86 `copyBufMem`). -/ +def copyBufMemA (um : Word → Word8) (sb rb : Word) : List Nat → (Word → Word8) + | [] => um + | x :: xs => copyBufMemA (storeMem um (bufAddr rb x) (loadWordAt um (bufAddr sb x))) sb rb xs + +/-- The buffer words `copy_mrs` copies (x86 `bufIdx`). -/ +def bufIdxA (n : LengthType) : List Nat := + List.range' (msg_registers.length + 1) (unat n + 1 - (msg_registers.length + 1)) + +theorem copyBufMemA_snoc (um : Word → Word8) (sb rb : Word) : + ∀ (xs : List Nat) (x : Nat), copyBufMemA um sb rb (xs ++ [x]) = + storeMem (copyBufMemA um sb rb xs) (bufAddr rb x) (loadWordAt (copyBufMemA um sb rb xs) (bufAddr sb x)) + | [], x => rfl + | y :: xs, x => copyBufMemA_snoc _ sb rb xs x + +/-- The buffer loop of `copy_mrs` (x86 `mapM_copy`). -/ +theorem mapM_copyA (sb rb : Word) : ∀ (xs : List Nat) (σ : State Unit), + (∀ x ∈ xs, (bufAddr sb x).toNat % 8 = 0 ∧ (bufAddr rb x).toNat % 8 = 0 ∧ inUserFrame (bufAddr rb x) σ) → + Fl4Spec.NonDet.mapM (fun x => do + let v ← loadWordOffs (Z := Unit) sb x + storeWordOffs rb x v) xs σ = + ({(xs.map fun _ => (), setUm σ (copyBufMemA σ.machineState.underlyingMemory sb rb xs))}, False) + | [], σ, _ => by + show (pure [] : Fl4Spec.NonDet (State Unit) (List Unit)) σ = _ + rw [nd_pure_apply']; rfl + | x :: xs, σ, h => by + obtain ⟨h1, h2, h3⟩ := h x (by simp) + unfold Fl4Spec.NonDet.mapM + rw [nd_bind_one' (show (do + let v ← loadWordOffs (Z := Unit) sb x + storeWordOffs rb x v) σ = ({((), setUm σ (storeMem σ.machineState.underlyingMemory (bufAddr rb x) + (loadWordAt σ.machineState.underlyingMemory (bufAddr sb x))))}, False) by + rw [nd_bind_one (loadWordOffs_eqA σ sb x h1)] + exact storeWordOffs_eq σ rb x _ h2 h3)] + rw [nd_bind_one' (mapM_copyA sb rb xs _ fun y hy => by + obtain ⟨g1, g2, g3⟩ := h y (by simp [hy]) + exact ⟨g1, g2, g3⟩)] + rfl + +/-- **`copy_mrs` with both IPC buffers** (x86 `copyMrs_bufs`). -/ +theorem copyMrs_bufsA {σ : State Unit} {snd rcv : ObjRef} {xs xr : Tcb} (hne : snd ≠ rcv) + (hks : σ.kheap snd = some (.tcb xs)) (hkr : σ.kheap rcv = some (.tcb xr)) (sb rb : ObjRef) (n : LengthType) + (h : ∀ x ∈ bufIdxA n, (bufAddr sb x).toNat % 8 = 0 ∧ (bufAddr rb x).toNat % 8 = 0 ∧ + inUserFrame (bufAddr rb x) σ) : + copyMrs (Z := Unit) snd (some sb) rcv (some rb) n σ = + ({(min n (natToLen ((msg_registers.take (unat n)).length + ((bufIdxA n).map fun _ => ()).length)), + setUm (updK σ rcv (.tcb (copyRegsTcbA xr xs.tcbArch.tcbContext.userRegs (msg_registers.take (unat n))))) + (copyBufMemA σ.machineState.underlyingMemory sb rb (bufIdxA n)))}, False) := by + unfold copyMrs + rw [nd_pure_bind', nd_bind_one (copy_regs_eqA hne hks hkr _)] + dsimp only + rw [show List.range' (msg_registers.length + 1) (unat n + 1 - (msg_registers.length + 1)) = bufIdxA n from rfl] + rw [nd_bind_one (mapM_copyA sb rb (bufIdxA n) (updK σ rcv (.tcb (copyRegsTcbA xr xs.tcbArch.tcbContext.userRegs + (msg_registers.take (unat n))))) (fun x hx => by + obtain ⟨g1, g2, g3⟩ := h x hx + exact ⟨g1, g2, inUserFrame_updK hkr (fun _ h => by cases h) _ g3⟩))] + rfl + +/-- `copy_mrs` without a send buffer (x86 `copyMrs_noSendBuf`). -/ +theorem copyMrs_noSendBufA {σ : State Unit} {snd rcv : ObjRef} {xs xr : Tcb} (hne : snd ≠ rcv) + (hks : σ.kheap snd = some (.tcb xs)) (hkr : σ.kheap rcv = some (.tcb xr)) (rbuf : Option ObjRef) + (n : LengthType) : + copyMrs (Z := Unit) snd none rcv rbuf n σ = + ({(min n (natToLen ((msg_registers.take (unat n)).length + ([] : List Word).length)), + updK σ rcv (.tcb (copyRegsTcbA xr xs.tcbArch.tcbContext.userRegs (msg_registers.take (unat n)))))}, False) := by + unfold copyMrs + rw [nd_pure_bind', nd_bind_one (copy_regs_eqA hne hks hkr _)] + cases rbuf <;> (dsimp only; rw [nd_pure_bind']; rfl) + +/-- `copy_mrs` without a receive buffer (x86 `copyMrs_noRecvBuf`). -/ +theorem copyMrs_noRecvBufA {σ : State Unit} {snd rcv : ObjRef} {xs xr : Tcb} (hne : snd ≠ rcv) + (hks : σ.kheap snd = some (.tcb xs)) (hkr : σ.kheap rcv = some (.tcb xr)) (sbuf : Option ObjRef) + (n : LengthType) : + copyMrs (Z := Unit) snd sbuf rcv none n σ = + ({(min n (natToLen ((msg_registers.take (unat n)).length + ([] : List Word).length)), + updK σ rcv (.tcb (copyRegsTcbA xr xs.tcbArch.tcbContext.userRegs (msg_registers.take (unat n)))))}, False) := by + unfold copyMrs + rw [nd_pure_bind', nd_bind_one (copy_regs_eqA hne hks hkr _)] + cases sbuf <;> (dsimp only; rw [nd_pure_bind']; rfl) + +end Spec + +/-! ## FL4's first loop: the inline message registers -/ + +section Loop0 +open Fl4SpecA64 + +theorem take_succ_memA (i : Nat) (hi : i < 4) (r : Register) : + r ∈ msg_registers.take (i + 1) ↔ r ∈ msg_registers.take i ∨ r = mrIdx i := by + rcases (show i = 0 ∨ i = 1 ∨ i = 2 ∨ i = 3 by omega) with rfl | rfl | rfl | rfl <;> + simp [msg_registers, msgRegisters, Register.x2ToX5, mrIdx] <;> tauto + +theorem take_fourA (k : Nat) (hk : 4 ≤ k) : msg_registers.take k = msg_registers := by + apply List.take_of_length_le; simp [msg_registers, msgRegisters, Register.x2ToX5]; omega + +theorem set_reg_eqA {S : Type} {st : machine.Store S} [L : LawfulStore st] (s : S) (t i v : U64) : + obj.Tcb.set_reg st t s i v = ok (L.wr s (regA t i) v) := by + unfold obj.Tcb.set_reg + simp only [lift, bind_ok, L.write_eq, regA] + +/-- A message register word of the receiver at `rcv` (x86 `RcvMR`). -/ +def RcvMRA (rcv b : U64) : Prop := ∃ r ∈ msg_registers, b.val = (regA rcv (regIdxA r)).val + +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {T E R : Set U64} {Bf : Set (U64 × Word)} + +/-- **The first loop of `copy_mrs`** (x86 `copy_mrs_loop0_spec`): the first `min n 4` message registers of the +sender go to the receiver; nothing else changes. -/ +theorem copy_mrs_loop0_specA (hP : EPlaced T E R Bf) (s : S) {snd rcv : U64} (hs : snd ∈ T) (hr : rcv ∈ T) + (hne : snd ≠ rcv) (n : U64) : + msg.copy_mrs_loop0 st s snd rcv n 0#u64 ⦃ r => r.2.val = min n.val 4 ∧ + (∀ reg, L.mem r.1 (regA rcv (regIdxA reg)) = + if reg ∈ msg_registers.take n.val then L.mem s (regA snd (regIdxA reg)) + else L.mem s (regA rcv (regIdxA reg))) ∧ + Agree (L.mem s) (L.mem r.1) (fun b => ¬ RcvMRA rcv b) ∧ WritesIn st s r.1 (EWrite T E R Bf) ⦄ := by + have hsa := hP.tcb snd hs + have hra := hP.tcb rcv hr + have hd := tcb_disj hP hr hs hne + unfold msg.copy_mrs_loop0 + apply loop.spec_decr_nat (measure := fun x => 4 - x.2.val) + (inv := fun x => x.2.val ≤ 4 ∧ x.2.val ≤ n.val ∧ + (∀ reg, L.mem x.1 (regA rcv (regIdxA reg)) = + if reg ∈ msg_registers.take x.2.val then L.mem s (regA snd (regIdxA reg)) + else L.mem s (regA rcv (regIdxA reg))) ∧ + Agree (L.mem s) (L.mem x.1) (fun b => ¬ RcvMRA rcv b) ∧ WritesIn st s x.1 (EWrite T E R Bf)) + · rintro ⟨s', i⟩ ⟨hi4, hin, hregs, hag, hw⟩ + unfold msg.copy_mrs_loop0.body + simp only at hi4 hin hregs hag hw ⊢ + by_cases h1 : i < n + · simp only [h1, ↓reduceIte] + by_cases h2 : i < msg.N_MSG_REGISTERS + · have hi : i.val < 4 := by + have : msg.N_MSG_REGISTERS.val = 4 := by unfold msg.N_MSG_REGISTERS; rfl + have := (gt_iff_val).1 h2; omega + simp only [h2, ↓reduceIte] + rw [msg_register_regIdxA hi, bind_ok, tcb_reg_eqA, bind_ok, set_reg_eqA, bind_ok, ← regA_eq_regWA] + step as ⟨i2, hi2⟩ + have hsnd : ∀ reg, L.mem s' (regA snd (regIdxA reg)) = L.mem s (regA snd (regIdxA reg)) := by + intro reg + refine hag _ (a_reg hsa reg).1 ?_ + rintro ⟨r', -, hv'⟩ + rw [regA_val hsa (regIdxA_lt reg), regA_val hra (regIdxA_lt r')] at hv' + have := regIdxA_lt reg; have := regIdxA_lt r'; omega + have ha := a_reg hra (mrIdx i.val) + have hi2v : i2.val = i.val + 1 := by simpa using hi2 + refine ⟨by omega, by have := (gt_iff_val).1 h1; omega, fun reg => ?_, ?_, ?_, by omega⟩ + · rw [hi2v] + by_cases e : reg = mrIdx i.val + · subst e + rw [show Fl4SpecA64.msgRegisters.getD i.val .x5 = mrIdx i.val from rfl, rd_wr_eq ha.1, + if_pos ((take_succ_memA _ hi _).2 (Or.inr rfl)), hsnd] + · rw [show Fl4SpecA64.msgRegisters.getD i.val .x5 = mrIdx i.val from rfl, + rd_wr_ne ha.1 (a_reg hra reg).1 (fun h => regA_ne hra e (congrArg UScalar.val h).symm), hregs reg] + by_cases hm : reg ∈ msg_registers.take i.val + · rw [if_pos hm, if_pos ((take_succ_memA _ hi _).2 (Or.inl hm))] + · rw [if_neg hm, if_neg (fun h => ((take_succ_memA _ hi _).1 h).elim hm e)] + · exact fun b hb hn => (agree_wr (L := L) (s := s') (v := L.mem s' (regA snd (regIdxA (mrIdx i.val)))) ha.1 + b hb fun h => hn ⟨_, mrIdx_mem i.val, h⟩).trans (hag b hb hn) + · exact (hw.trans (wr_ewrite hr (twf_reg hra _) ha.1 _ _)).mono fun b hb => by rcases hb with h | h <;> exact h + · simp only [h2, ↓reduceIte, WP.spec_ok] + have : msg.N_MSG_REGISTERS.val = 4 := by unfold msg.N_MSG_REGISTERS; rfl + have hi : i.val = 4 := by + have h2' : ¬ i.val < 4 := fun h => h2 ((gt_iff_val).2 (by rw [this]; exact h)) + omega + have hn4 : 4 ≤ n.val := by have := (gt_iff_val).1 h1; omega + refine ⟨by omega, fun reg => ?_, hag, hw⟩ + rw [hregs reg, hi, take_fourA 4 (le_refl _), take_fourA n.val hn4] + · simp only [h1, ↓reduceIte, WP.spec_ok] + have hni : i.val = n.val := by + have : ¬ i.val < n.val := fun h => h1 ((gt_iff_val).2 h) + omega + refine ⟨by omega, fun reg => ?_, hag, hw⟩ + rw [hregs reg, hni] + · refine ⟨by simp, by simp, fun reg => by simp, Agree.rfl', WritesIn.refl _ _⟩ + +/-- `copy_mrs` without a receive buffer: the first loop only (x86 `copy_mrs_norecv`). -/ +theorem copy_mrs_norecvA (hP : EPlaced T E R Bf) (s : S) {snd rcv : U64} (hs : snd ∈ T) (hr : rcv ∈ T) + (hne : snd ≠ rcv) (sb n : U64) : + msg.copy_mrs st s snd sb rcv 0#u64 n ⦃ r => r.1.val = min n.val 4 ∧ + (∀ reg, L.mem r.2 (regA rcv (regIdxA reg)) = + if reg ∈ msg_registers.take n.val then L.mem s (regA snd (regIdxA reg)) + else L.mem s (regA rcv (regIdxA reg))) ∧ + Agree (L.mem s) (L.mem r.2) (fun b => ¬ RcvMRA rcv b) ∧ WritesIn st s r.2 (EWrite T E R Bf) ⦄ := by + unfold msg.copy_mrs + refine WP.spec_bind (copy_mrs_loop0_specA (L := L) hP s hs hr hne n) ?_ + rintro ⟨s1, i⟩ h + simp only [↓reduceIte] + show (ok (i, s1)) ⦃ _ ⦄ + simp only [WP.spec_ok] + exact h + +/-- A TCB whose register words changed and nothing else of its fields (x86 `TcbRelE.with_regs`; the A64 register +words are `t + 256 .. t + 544`). -/ +theorem TcbRelE.with_regsA {m m' : U64 → U64} {t : U64} {x x' : Tcb} (ht : TcbAddr t) + (h : TcbRelE R m t x) (hA : Agree m m' (fun b => TF t b ∧ (b.val < t.val + 256 ∨ t.val + 552 ≤ b.val))) + (hx : x' = { x with tcbArch := x'.tcbArch }) + (hregs : ∀ r, (m' (regA t (regIdxA r))).bv = x'.tcbArch.tcbContext.userRegs r) : TcbRelE R m' t x' := by + have g : ∀ {b}, Aligned b ∧ TF t b → (b.val < t.val + 256 ∨ t.val + 552 ≤ b.val) → m' b = m b := + fun hb hv => hA _ hb.1 ⟨hb.2, hv⟩ + rw [hx] + refine ⟨?_, fun e he => ?_, fun e d he => ?_, fun e rr d he => ?_, fun rr he => ?_, fun r => ?_, ?_, ?_, ?_, ?_, ?_⟩ + · rw [g (a_st ht) (by rw [val_st ht]; omega)]; exact h.st + · rw [g (a_blk ht) (by rw [val_blk ht]; omega)]; exact h.blk e he + · rw [g (a_badge ht) (by rw [val_badge ht]; omega), g (a_flags ht) (by rw [val_flags ht]; omega)] + exact h.send e d he + · rw [g (a_reply ht) (by rw [val_reply ht]; omega)]; exact h.recv e rr d he + · rw [g (a_reply ht) (by rw [val_reply ht]; omega)]; exact h.reply rr he + · rw [← hx]; exact hregs r + · rw [g (a_prio ht) (by rw [val_prio ht]; omega)]; exact h.prio + · rw [g (a_fault ht) (by rw [val_fault ht]; omega)]; exact h.fault0 + · rw [g (a_fault ht) (by rw [val_fault ht]; omega)]; exact h.faultT + · rw [g (a_bound ht) (by rw [val_bound ht]; omega)]; exact h.bound + · exact h.faultR.congr (g (a_fault ht) (by rw [val_fault ht]; omega)) (g (a_fw0 ht) (by rw [val_fw0 ht]; omega)) + (g (a_fw1 ht) (by rw [val_fw1 ht]; omega)) (g (a_lf ht) (by rw [val_lf ht]; omega)) + (g (a_lf0 ht) (by rw [val_lf0 ht]; omega)) (g (a_lf1 ht) (by rw [val_lf1 ht]; omega)) + (g (a_fw2 ht) (by rw [val_fw2 ht]; omega)) + +variable {SR : S → State Unit → Prop} + +/-- The receiver after the first loop, as a spec step that blocks no thread. -/ +theorem copyRegs_step {σ : State Unit} {p : ObjRef} {xr : Tcb} (hkr : σ.kheap p = some (.tcb xr)) + (vals : Register → Word) (rs : List Register) : + EpStep σ (updK σ p (.tcb (copyRegsTcbA xr vals rs))) ∧ NoBlockE σ (updK σ p (.tcb (copyRegsTcbA xr vals rs))) := by + have hx1 := copyRegsTcbA_other xr vals rs + have hst0 := congrArg Tcb.tcbState hx1 + have hst : (copyRegsTcbA xr vals rs).tcbState = xr.tcbState := hst0 + refine ⟨?_, NoBlockE.tcbK hkr _ (by rw [hst]; exact hx1) hst⟩ + have h := EpStep.tcb hkr xr.tcbState (copyRegsTcbA xr vals rs).tcbArch + rwa [← copyRegsTcbA_other] at h + +/-- **The relation after the first `copy_mrs` loop** (x86 `loop0_rel`). -/ +theorem loop0_relA (hO : SchedOpsEA st T E R Bf SR) (hP : EPlaced T E R Bf) {s s1 : S} {σ : State Unit} + (hR : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) {snd rcv : U64} (hs : snd ∈ T) (hr : rcv ∈ T) + {xs xr : Tcb} (hks : σ.kheap (specPtr snd) = some (.tcb xs)) (hkr : σ.kheap (specPtr rcv) = some (.tcb xr)) + (k : Nat) + (hregs : ∀ reg, L.mem s1 (regA rcv (regIdxA reg)) = + if reg ∈ msg_registers.take k then L.mem s (regA snd (regIdxA reg)) + else L.mem s (regA rcv (regIdxA reg))) + (hag : Agree (L.mem s) (L.mem s1) (fun b => ¬ RcvMRA rcv b)) (hw : WritesIn st s s1 (EWrite T E R Bf)) : + EpStateRel T E R Bf (L.mem s1) + (updK σ (specPtr rcv) (.tcb (copyRegsTcbA xr xs.tcbArch.tcbContext.userRegs (msg_registers.take k)))) ∧ + SR s1 (updK σ (specPtr rcv) (.tcb (copyRegsTcbA xr xs.tcbArch.tcbContext.userRegs (msg_registers.take k)))) := by + have hra := hP.tcb rcv hr + have hxs := hR.at_tcb hs hks + have hxr := hR.at_tcb hr hkr + set x1 := copyRegsTcbA xr xs.tcbArch.tcbContext.userRegs (msg_registers.take k) with hx1 + have hnotreg : ∀ b, TF rcv b → (b.val < rcv.val + 256 ∨ rcv.val + 552 ≤ b.val) → ¬ RcvMRA rcv b := by + rintro b - hv' ⟨r, -, hb⟩ + rw [regA_val hra (regIdxA_lt r)] at hb; have := regIdxA_lt r; omega + have hR1 : EpStateRel T E R Bf (L.mem s1) (updK σ (specPtr rcv) (.tcb x1)) := by + refine hR.upd_tcb hP hr x1 (hag.mono fun b hb h' => hb.2 ?_) + (Or.inr ⟨hag _ (a_st hra).1 ?_, hag _ (a_blk hra).1 ?_⟩) + (hxr.with_regsA hra (hag.mono fun b hb => hnotreg b hb.1 hb.2) (copyRegsTcbA_other _ _ _) fun r => ?_) + · obtain ⟨r', -, hb'⟩ := h' + exact TF.of_val (a_reg hra r').2 hb' + · exact hnotreg _ (a_st hra).2 (by rw [val_st hra]; omega) + · exact hnotreg _ (a_blk hra).2 (by rw [val_blk hra]; omega) + · rw [hregs r, hx1, copyRegsTcbA_regs] + split + · exact hxs.regs r + · exact hxr.regs r + obtain ⟨hS1, hN1⟩ := copyRegs_step hkr xs.tcbArch.tcbContext.userRegs (msg_registers.take k) + exact ⟨hR1, hO.stable _ _ _ _ hSR hw hS1 hN1⟩ + +end Loop0 + +/-! ## FL4's second loop: the buffer words -/ + +section Loop1 +open Fl4SpecA64 +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {T E R : Set U64} {Bf : Set (U64 × Word)} + {SR : S → State Unit → Prop} + +theorem range'_snoc5A (k : Nat) : List.range' 5 (k + 1) = List.range' 5 k ++ [5 + k] := by + rw [List.range'_concat]; simp + +/-- **The second loop of `copy_mrs`** (x86 `copy_mrs_loop1_rel`): the buffer words `5 .. n`, both buffers tracked. -/ +theorem copy_mrs_loop1_relA (hO : SchedOpsEA st T E R Bf SR) (hP : EPlaced T E R Bf) (s : S) (σ : State Unit) + (hR : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) {bs br : U64 × Word} (hbs : bs ∈ Bf) (hbr : br ∈ Bf) + (n i0 : U64) (hn : n.val ≤ 120) (hi0 : i0.val = min n.val 4) : + msg.copy_mrs_loop1 st s bs.1 br.1 n i0 ⦃ r => r.1.val = n.val ∧ + EpStateRel T E R Bf (L.mem r.2) + (setUm σ (copyBufMemA σ.machineState.underlyingMemory bs.2 br.2 (List.range' 5 (n.val - 4)))) ∧ + SR r.2 (setUm σ (copyBufMemA σ.machineState.underlyingMemory bs.2 br.2 (List.range' 5 (n.val - 4)))) ∧ + Agree (L.mem s) (L.mem r.2) (fun b => ¬ BufW Bf b) ∧ WritesIn st s r.2 (EWrite T E R Bf) ⦄ := by + unfold msg.copy_mrs_loop1 + apply loop.spec_decr_nat (measure := fun x => n.val - x.2.val) + (inv := fun x => i0.val ≤ x.2.val ∧ x.2.val ≤ n.val ∧ + EpStateRel T E R Bf (L.mem x.1) + (setUm σ (copyBufMemA σ.machineState.underlyingMemory bs.2 br.2 (List.range' 5 (x.2.val - 4)))) ∧ + SR x.1 (setUm σ (copyBufMemA σ.machineState.underlyingMemory bs.2 br.2 (List.range' 5 (x.2.val - 4)))) ∧ + Agree (L.mem s) (L.mem x.1) (fun b => ¬ BufW Bf b) ∧ WritesIn st s x.1 (EWrite T E R Bf)) + · rintro ⟨s', i⟩ ⟨h1, h2, hR', hS', hA', hW'⟩ + unfold msg.copy_mrs_loop1.body + simp only at h1 h2 hR' hS' hA' hW' ⊢ + by_cases hc1 : i < n + · have hlt : i.val < n.val := (gt_iff_val).1 hc1 + have h4 : 4 ≤ i.val := by omega + have hmax : msg.MSG_MAX_LENGTH.val = 120 := by unfold msg.MSG_MAX_LENGTH; rfl + have hc2 : i < msg.MSG_MAX_LENGTH := (gt_iff_val).2 (by rw [hmax]; omega) + simp only [hc1, hc2, ↓reduceIte] + step as ⟨i1, hi1⟩ + rw [buf_get_eqA, ← bufW_eq_bufWA, bind_ok, buf_set_eq, bind_ok, WP.spec_ok] + have hi1v : i1.val = i.val + 1 := by simpa using hi1 + have hk : i1.val < 128 := by omega + set um' := copyBufMemA σ.machineState.underlyingMemory bs.2 br.2 (List.range' 5 (i.val - 4)) + have hwr : BufW Bf (bufW br.1 i1) := ⟨br, hbr, i1, hk, rfl⟩ + have hal := hP.balign _ hwr + have hag := agree_wr (L := L) (s := s') (v := L.mem s' (bufW bs.1 i1)) hal + have hR1 := hR'.upd_buf hP hbr hk (L.mem s' (bufW bs.1 i1)) + (fun b hb hne => hag b hb (val_ne_of_ne hne)) (rd_wr_eq hal) + have hvb : (L.mem s' (bufW bs.1 i1)).bv = loadWordAt um' (bufAddr bs.2 i1.val) := (hR'.buf bs hbs i1 hk).1 + have hlist : List.range' 5 (i1.val - 4) = List.range' 5 (i.val - 4) ++ [i1.val] := by + rw [show i1.val - 4 = (i.val - 4) + 1 by omega, range'_snoc5A]; congr 2; omega + have hmem : copyBufMemA σ.machineState.underlyingMemory bs.2 br.2 (List.range' 5 (i1.val - 4)) = + storeMem um' (bufAddr br.2 i1.val) (L.mem s' (bufW bs.1 i1)).bv := by + rw [hlist, copyBufMemA_snoc, hvb] + have hW : WritesIn st s' (L.wr s' (bufW br.1 i1) (L.mem s' (bufW bs.1 i1))) (EWrite T E R Bf) := + (writesIn_wr hal).mono fun b hb => by rw [hb]; exact Or.inr (Or.inr (Or.inr hwr)) + dsimp only + refine ⟨by omega, by omega, ?_, ?_, ?_, (hW'.trans hW).mono fun b h => h.elim id id, by omega⟩ + · rw [hmem, ← setUm_setUmA σ um']; rw [setUm_umA] at hR1; exact hR1 + · rw [hmem, ← setUm_setUmA σ um'] + exact hO.stable _ _ _ _ hS' hW (EpStep.setUmA _ _) (NoBlockE.setUmA _ _) + · exact fun b hb hn => (hag b hb fun h => hn (by rw [UScalar.eq_of_val_eq h]; exact hwr)).trans (hA' b hb hn) + · simp only [hc1, ↓reduceIte, WP.spec_ok] + have : i.val = n.val := by have : ¬ i.val < n.val := fun h => hc1 ((gt_iff_val).2 h); omega + refine ⟨this, ?_, ?_, hA', hW'⟩ <;> rw [← this] <;> assumption + · dsimp only + refine ⟨le_refl _, by omega, ?_, ?_, Agree.rfl', WritesIn.refl s _⟩ + · rw [show i0.val - 4 = 0 by omega]; exact hR + · rw [show i0.val - 4 = 0 by omega]; exact hSR + +end Loop1 + +/-! ## `copy_mrs` with a receive buffer -/ + +section Copy +open Fl4SpecA64 +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {T E R : Set U64} {Bf : Set (U64 × Word)} + {SR : S → State Unit → Prop} + +theorem xfer_lenA {i : U64} {Ln : LengthType} (hi : i.val = min Ln.toNat 4) : + i.bv = min Ln (natToLen ((msg_registers.take (unat Ln)).length + ([] : List Word).length)) := by + have hl : (msg_registers.take (unat Ln)).length + ([] : List Word).length = min Ln.toNat 4 := by + simp [msg_registers, msgRegisters, Register.x2ToX5, unat] + rw [hl] + have hle : natToLen (min Ln.toNat 4) ≤ Ln := by + rw [BitVec.le_def] + unfold natToLen + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)] + omega + have key : i.bv = natToLen (min Ln.toNat 4) := by + apply BitVec.eq_of_toNat_eq + unfold natToLen + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)] + exact hi + rw [BitVec.min_def] + by_cases hc : Ln ≤ natToLen (min Ln.toNat 4) + · rw [if_pos hc]; rw [key]; exact (BitVec.le_antisymm hc hle).symm + · rw [if_neg hc]; exact key + +theorem xfer_len_bufA {n : U64} (hn : n.val ≤ 120) : + n.bv = min n.bv (natToLen ((msg_registers.take (unat n.bv)).length + + ((bufIdxA n.bv).map fun _ => ()).length)) := by + have hl : (msg_registers.take (unat n.bv)).length + ((bufIdxA n.bv).map fun _ => ()).length = n.bv.toNat := by + simp [msg_registers, msgRegisters, Register.x2ToX5, unat, bufIdxA] + have : n.bv.toNat = n.val := rfl + omega + rw [hl, BitVec.min_def] + have : natToLen n.bv.toNat = n.bv := by unfold natToLen; simp + rw [this, if_pos (BitVec.le_refl _)] + +theorem bufIdxA_eq (n : U64) : bufIdxA n.bv = List.range' 5 (n.val - 4) := by + unfold bufIdxA + simp only [msg_registers, msgRegisters, Register.x2ToX5, List.length_cons, List.length_nil, unat] + congr 1 + +/-- The spec's buffer word `x` is aligned when word 0 is. -/ +theorem bufAddr_align {p : Word} (h0 : (bufAddr p 0).toNat % 8 = 0) (x : Nat) (hx : x < 128) : + (bufAddr p x).toNat % 8 = 0 := by + simp only [bufAddr, Nat.zero_mul, BitVec.ofNat_eq_ofNat, BitVec.add_zero] at h0 + unfold bufAddr + rw [BitVec.toNat_add, BitVec.toNat_ofNat] + simp only [wordSize] + omega + +/-- **`copy_mrs` with a receive buffer** (x86 `copy_mrs_rel`): one spec outcome, the receiver's registers and buffer +as l4v's, the relation and `SR` kept, nothing else written but the receiver's message registers and buffer words. -/ +theorem copy_mrs_relA (hO : SchedOpsEA st T E R Bf SR) (hP : EPlaced T E R Bf) (s : S) (σ : State Unit) + (hR : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) {snd rcv : U64} (hs : snd ∈ T) (hr : rcv ∈ T) + (hne : snd ≠ rcv) {xs xr : Tcb} (hks : σ.kheap (specPtr snd) = some (.tcb xs)) + (hkr : σ.kheap (specPtr rcv) = some (.tcb xr)) {br : U64 × Word} (hbr : br ∈ Bf) (hbr0 : br.1 ≠ 0#u64) + (hu : ∀ k, k < 128 → inUserFrame (bufAddr br.2 k) σ) {sw : U64} {sb : Option ObjRef} + (hsw : (sb = none ∧ sw = 0#u64) ∨ (∃ bs ∈ Bf, sb = some bs.2 ∧ sw = bs.1 ∧ bs.1 ≠ 0#u64)) + (n : U64) (hn : n.val ≤ 120) : + msg.copy_mrs st s snd sw rcv br.1 n ⦃ r => ∃ σc, copyMrs (Z := Unit) (specPtr snd) sb (specPtr rcv) (some br.2) + n.bv σ = ({(r.1.bv, σc)}, False) ∧ + σc.kheap = Function.update σ.kheap (specPtr rcv) (some (.tcb (copyRegsTcbA xr xs.tcbArch.tcbContext.userRegs + (msg_registers.take n.val)))) ∧ + EpStateRel T E R Bf (L.mem r.2) σc ∧ SR r.2 σc ∧ + Agree (L.mem s) (L.mem r.2) (fun b => ¬ RcvMRA rcv b ∧ ¬ BufW Bf b) ∧ r.1.val ≤ 120 ∧ + WritesIn st s r.2 (EWrite T E R Bf) ∧ EpStep σ σc ⦄ := by + have hne' : specPtr snd ≠ specPtr rcv := fun h => hne (specPtr_inj h) + unfold msg.copy_mrs + refine WP.spec_bind (copy_mrs_loop0_specA (L := L) hP s hs hr hne n) ?_ + rintro ⟨s1, i⟩ ⟨hi, hregs, hag, hw⟩ + obtain ⟨hR1, hS1⟩ := loop0_relA hO hP hR hSR hs hr hks hkr n.val hregs hag hw + have hun : unat n.bv = n.val := rfl + set σ1 := updK σ (specPtr rcv) (.tcb (copyRegsTcbA xr xs.tcbArch.tcbContext.userRegs (msg_registers.take n.val))) + have hE1 : EpStep σ σ1 := (copyRegs_step hkr _ _).1 + simp only [hbr0, ↓reduceIte] + rcases hsw with ⟨rfl, rfl⟩ | ⟨bs, hbs, rfl, rfl, hsw0⟩ + · simp only [↓reduceIte] + show (ok (i, s1)) ⦃ _ ⦄ + simp only [WP.spec_ok] + refine ⟨σ1, ?_, rfl, hR1, hS1, hag.mono fun b hb => hb.1, by rw [hi]; omega, hw, hE1⟩ + rw [copyMrs_noSendBufA hne' hks hkr _ _, hun] + congr + exact (xfer_lenA (by rw [hi]; rfl)).symm + · simp only [hsw0, ↓reduceIte] + refine WP.spec_mono (copy_mrs_loop1_relA hO hP s1 σ1 hR1 hS1 hbs hbr n i hn hi) ?_ + rintro ⟨n2, s2⟩ ⟨hn2, hR2, hS2, hA2, hW2⟩ + refine ⟨_, ?_, rfl, hR2, hS2, fun b hb hn' => (hA2 b hb hn'.2).trans (hag b hb hn'.1), + by rw [hn2]; exact hn, (hw.trans hW2).mono fun b h => h.elim id id, hE1.trans (EpStep.setUmA _ _)⟩ + rw [copyMrs_bufsA hne' hks hkr bs.2 br.2 n.bv ?_] + · have e : n2 = n := UScalar.eq_of_val_eq hn2 + subst e + rw [← xfer_len_bufA hn, bufIdxA_eq, hun] + rfl + · intro x hx + rw [bufIdxA_eq] at hx + have hx' : x < 128 := by simp at hx; omega + exact ⟨bufAddr_align (hR.buf bs hbs 0#u64 (by decide)).2 x hx', + bufAddr_align (hR.buf br hbr 0#u64 (by decide)).2 x hx', hu x hx'⟩ + +end Copy + +end + +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/EpFaultCapA.lean b/proofs/Fl4ProofsA64/P3/EpFaultCapA.lean new file mode 100644 index 000000000..701065686 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/EpFaultCapA.lean @@ -0,0 +1,255 @@ +/- +P3 (aarch64, A64-P3-IPC gen 9, ruling item 2): **the cap-fault message of `set_mrs_fault`** (x86 `P3/EpFault`'s +`makeFaultMsg_cap`, `decode_rel`, `set_mrs_lookup_failure_rel`, `snd_word_eq`, `set_mrs_fault_cap` and +`P3/EpFaultNone`'s `…N` twins, ported once for both receivers: with a tracked IPC buffer (gen 7's `MrInvA`) and without +one (`EpSetMrsNA.MrInvNA`)). + +* `SetMrLawA I rcv buf g`: `set_mr` of the next word keeps the message invariant `I`, returning `g (i + 1)`; + **`setMrLaw_buf`** (`MrInvA`, `g = id`) and **`setMrLaw_nobuf`** (`MrInvNA`, `g = min · 4`). +* Spec: `capFaultMsgA`, **`makeFaultMsg_capA`** (one outcome). +* FL4: `fl4LfMsgA`, **`decode_relA`** (`LookupFault::decode` of `LfRel`-related words gives l4v's + `msg_from_lookup_failure`), **`set_mrs_lookup_failure_genA`**, `snd_word_eqA` (the sender's TCB words are kept while + the message is written), **`set_mrs_fault_capA`**: FL4's `set_mrs_fault` of a cap fault writes l4v's + `make_fault_msg` words under `I`, count `g |msg|`. + +Hypotheses: `EPlaced`, `snd rcv ∈ T`, `snd ≠ rcv`, the sender's cap fault; the invariant's law and start. No named +premise. +-/ +import Fl4ProofsA64.P3.EpSetMrsNA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif + +noncomputable section + +section Spec +open Fl4SpecA64 + +/-- The fault message of a cap fault (l4v `make_fault_msg`). -/ +def capFaultMsgA (pc : Word) (c : ObjRef) (rp : Bool) (lf : LookupFailure) : List Word := + pc :: c :: (if rp then 1 else 0) :: msgFromLookupFailure lf + +theorem makeFaultMsg_capA {σ : State Unit} {t : ObjRef} {x : Tcb} (hk : σ.kheap t = some (.tcb x)) + (c : ObjRef) (rp : Bool) (lf : LookupFailure) : + makeFaultMsg (Z := Unit) (.capFault c rp lf) t σ = + ({((1, capFaultMsgA (x.tcbArch.tcbContext.userRegs .faultIP) c rp lf), σ)}, False) := by + rw [makeFaultMsg] + rw [show getRestartPC = getRegister .faultIP from rfl, nd_bind_one (asUser_getRegister_eqA hk .faultIP)] + rfl + +end Spec + +section Fl4 +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {T E R : Set U64} + {Bf : Set (U64 × Fl4SpecA64.Word)} {SR : S → Fl4SpecA64.State Unit → Prop} + +variable (st) in +/-- `set_mr` of the next word keeps the message invariant `I` of the receiver `rcv` (buffer word `buf`), returning +`g (i + 1)`. -/ +def SetMrLawA (I : S → List Fl4SpecA64.Word → Prop) (rcv buf : U64) (g : Nat → Nat) : Prop := + ∀ s ms, I s ms → ∀ i : U64, i.val = ms.length → i.val < 120 → ∀ v : U64, + msg.set_mr st s rcv buf i v ⦃ r => r.1.val = g (i.val + 1) ∧ I r.2 (ms ++ [v.bv]) ⦄ + +theorem setMrLaw_buf (hO : SchedOpsE0A st T E R Bf SR) (hP : EPlaced T E R Bf) {s0 : S} + {σ0 : Fl4SpecA64.State Unit} {rcv : U64} (hr : rcv ∈ T) (x0 : Fl4SpecA64.Tcb) {bp : U64 × Fl4SpecA64.Word} + (hb : bp ∈ Bf) (hb0 : bp.1 ≠ 0#u64) : + SetMrLawA st (MrInvA (L := L) T E R Bf SR s0 σ0 rcv x0 bp.2) rcv bp.1 id := + fun _ _ hI i hi hi' v => set_mr_inv hO hP hr hb hb0 hI i hi hi' v + +theorem setMrLaw_nobuf (hO : SchedOpsE0A st T E R Bf SR) (hP : EPlaced T E R Bf) {s0 : S} + {σ0 : Fl4SpecA64.State Unit} {rcv : U64} (hr : rcv ∈ T) (x0 : Fl4SpecA64.Tcb) : + SetMrLawA st (MrInvNA (L := L) T E R Bf SR s0 σ0 rcv x0) rcv 0#u64 (fun n => min n 4) := + fun _ _ hI i hi _ v => set_mr_invNA hO hP hr hI i hi v + +/-- The words of a lookup failure, as FL4 writes them. -/ +def fl4LfMsgA : error.LookupFault → List U64 + | .InvalidRoot => [1#u64] + | .MissingCapability bl => [2#u64, bl] + | .DepthMismatch bf bl => [3#u64, bl, bf] + | .GuardMismatch g bl gs => [4#u64, bl, g, gs] + +theorem bv_ofNat_valA (x : U64) {n : Nat} (h : x.val = n) : x.bv = BitVec.ofNat 64 n := by + apply BitVec.eq_of_toNat_eq + rw [BitVec.toNat_ofNat] + show x.val = n % 2 ^ 64 + rw [← h, Nat.mod_eq_of_lt (by have := x.hBounds; simpa [U64.size_def, U64.numBits] using this)] + +/-- `LookupFault::decode` of related words gives l4v's `msg_from_lookup_failure` (x86 `decode_rel`). -/ +theorem decode_relA {lt l0 l1 : U64} {lf : Fl4SpecA64.LookupFailure} (h : LfRel lt l0 l1 lf) : + error.LookupFault.decode lt l0 l1 ⦃ v => (fl4LfMsgA v).map UScalar.bv = Fl4SpecA64.msgFromLookupFailure lf ⦄ := by + unfold error.LookupFault.decode + cases lf with + | invalidRoot => + have : lt = 1#u64 := h + subst this + simp only [show (1#u64 : U64) ≠ 2#u64 by decide, show (1#u64 : U64) ≠ 3#u64 by decide, + show (1#u64 : U64) ≠ 4#u64 by decide, ↓reduceIte, WP.spec_ok] + rfl + | missingCapability n => + obtain ⟨rfl, h0⟩ := h + simp only [↓reduceIte, WP.spec_ok, fl4LfMsgA, List.map_cons, List.map_nil, Fl4SpecA64.msgFromLookupFailure] + simp only [List.cons.injEq, and_true] + exact ⟨rfl, bv_ofNat_valA l0 h0⟩ + | depthMismatch bl bf => + obtain ⟨rfl, h0, h1⟩ := h + simp only [show (3#u64 : U64) ≠ 2#u64 by decide, ↓reduceIte, WP.spec_ok, fl4LfMsgA, List.map_cons, List.map_nil, + Fl4SpecA64.msgFromLookupFailure] + simp only [List.cons.injEq, and_true] + exact ⟨rfl, bv_ofNat_valA l1 h1, bv_ofNat_valA l0 h0⟩ + | guardMismatch bl g => + obtain ⟨rfl, h0, h1, h2⟩ := h + simp only [show (4#u64 : U64) ≠ 2#u64 by decide, show (4#u64 : U64) ≠ 3#u64 by decide, ↓reduceIte] + step as ⟨i, hi, hi'⟩ + simp only [lift, bind_ok, WP.spec_ok, fl4LfMsgA, List.map_cons, List.map_nil, Fl4SpecA64.msgFromLookupFailure, + List.cons.injEq, and_true] + refine ⟨rfl, bv_ofNat_valA i (by rw [hi]; exact h1), h0, bv_ofNat_valA _ ?_⟩ + show (l1 &&& 255#u64).bv.toNat = _ + rw [UScalar.bv_and, BitVec.toNat_and, show (UScalar.bv (255#u64 : U64)).toNat = 2 ^ 8 - 1 from rfl, + Nat.and_two_pow_sub_one_eq_mod] + exact h2 + +theorem wadd_valA (x : U64) (k : Nat) (hk : x.val + k < 2 ^ 64) (K : U64) (hK : K.val = k) : + (core.num.U64.wrapping_add x K).val = x.val + k := by + rw [core.num.U64.wrapping_add_val_eq, hK]; simp [U64.size_def, U64.numBits]; omega + +/-- **`set_mrs_lookup_failure`**: the lookup-failure words appended under `I` (x86 `set_mrs_lookup_failure_rel` / +`_relN`). -/ +theorem set_mrs_lookup_failure_genA {I : S → List Fl4SpecA64.Word → Prop} {rcv buf : U64} {g : Nat → Nat} + (hset : SetMrLawA st I rcv buf g) {s : S} {ms : List Fl4SpecA64.Word} (hI : I s ms) (off : U64) + (hoff : off.val = ms.length) (hlen : ms.length + 4 ≤ 120) (lfv : error.LookupFault) : + faults.set_mrs_lookup_failure st s rcv buf lfv off ⦃ r => r.1.val = g (ms.length + (fl4LfMsgA lfv).length) ∧ + I r.2 (ms ++ ((fl4LfMsgA lfv).map UScalar.bv : List Fl4SpecA64.Word)) ⦄ := by + unfold faults.set_mrs_lookup_failure error.LookupFault.type_code + cases lfv with + | InvalidRoot => + simp only [bind_ok] + refine WP.spec_bind (hset _ _ hI off hoff (by omega) _) ?_ + rintro ⟨i1, s1⟩ ⟨h1, h2⟩ + show (ok (i1, s1)) ⦃ _ ⦄ + simp only [WP.spec_ok] + refine ⟨by simp only at h1; rw [h1, hoff]; rfl, ?_⟩ + simpa [fl4LfMsgA, fl4_abi.consts.lookup.InvalidRoot] using h2 + | MissingCapability bl => + simp only [bind_ok] + refine WP.spec_bind (hset _ _ hI off hoff (by omega) _) ?_ + rintro ⟨i1, s1⟩ ⟨h1, h2⟩ + try simp only [lift, bind_ok] + refine WP.spec_mono (hset _ _ h2 (core.num.U64.wrapping_add off 1#u64) + (by rw [wadd_valA off 1 (by omega) _ rfl]; simp; omega) (by rw [wadd_valA off 1 (by omega) _ rfl]; omega) bl) ?_ + rintro ⟨i2, s2⟩ ⟨g1, g2⟩ + refine ⟨by simp only at g1 ⊢; rw [g1, wadd_valA off 1 (by omega) _ rfl, hoff]; rfl, ?_⟩ + simpa [fl4LfMsgA, fl4_abi.consts.lookup.MissingCapability] using g2 + | DepthMismatch bf bl => + simp only [bind_ok] + refine WP.spec_bind (hset _ _ hI off hoff (by omega) _) ?_ + rintro ⟨i1, s1⟩ ⟨h1, h2⟩ + try simp only [lift, bind_ok] + refine WP.spec_bind (hset _ _ h2 (core.num.U64.wrapping_add off 1#u64) + (by rw [wadd_valA off 1 (by omega) _ rfl]; simp; omega) (by rw [wadd_valA off 1 (by omega) _ rfl]; omega) bl) ?_ + rintro ⟨i2, s2⟩ ⟨g1, g2⟩ + try simp only [lift, bind_ok] + refine WP.spec_mono (hset _ _ g2 (core.num.U64.wrapping_add off 2#u64) + (by rw [wadd_valA off 2 (by omega) _ rfl]; simp; omega) (by rw [wadd_valA off 2 (by omega) _ rfl]; omega) bf) ?_ + rintro ⟨i3, s3⟩ ⟨k1, k2⟩ + refine ⟨by simp only at k1 ⊢; rw [k1, wadd_valA off 2 (by omega) _ rfl, hoff]; rfl, ?_⟩ + simpa [fl4LfMsgA, fl4_abi.consts.lookup.DepthMismatch] using k2 + | GuardMismatch gd bl gs => + simp only [bind_ok] + refine WP.spec_bind (hset _ _ hI off hoff (by omega) _) ?_ + rintro ⟨i1, s1⟩ ⟨h1, h2⟩ + try simp only [lift, bind_ok] + refine WP.spec_bind (hset _ _ h2 (core.num.U64.wrapping_add off 1#u64) + (by rw [wadd_valA off 1 (by omega) _ rfl]; simp; omega) (by rw [wadd_valA off 1 (by omega) _ rfl]; omega) bl) ?_ + rintro ⟨i2, s2⟩ ⟨g1, g2⟩ + try simp only [lift, bind_ok] + refine WP.spec_bind (hset _ _ g2 (core.num.U64.wrapping_add off 2#u64) + (by rw [wadd_valA off 2 (by omega) _ rfl]; simp; omega) (by rw [wadd_valA off 2 (by omega) _ rfl]; omega) gd) ?_ + rintro ⟨i3, s3⟩ ⟨k1, k2⟩ + try simp only [lift, bind_ok] + refine WP.spec_mono (hset _ _ k2 (core.num.U64.wrapping_add off 3#u64) + (by rw [wadd_valA off 3 (by omega) _ rfl]; simp; omega) (by rw [wadd_valA off 3 (by omega) _ rfl]; omega) gs) ?_ + rintro ⟨i4, s4⟩ ⟨l1, l2⟩ + refine ⟨by simp only at l1 ⊢; rw [l1, wadd_valA off 3 (by omega) _ rfl, hoff]; rfl, ?_⟩ + simpa [fl4LfMsgA, fl4_abi.consts.lookup.GuardMismatch] using l2 + +/-- The sender's TCB words do not change while the message is written (x86 `snd_word_eq`). -/ +theorem snd_word_eqA {s0 s : S} (hP : EPlaced T E R Bf) {snd rcv : U64} (hs : snd ∈ T) (hr : rcv ∈ T) + (hne : snd ≠ rcv) (hA : Agree (L.mem s0) (L.mem s) (fun b => ¬ RcvRegA rcv b ∧ ¬ BufW Bf b)) {a : U64} + (ha : Aligned a ∧ TF snd a) : L.mem s a = L.mem s0 a := by + refine hA a ha.1 ⟨?_, ?_⟩ + · rintro ⟨r', hr'⟩ + have := tcb_disj hP hr hs hne; have := ha.2.range + rw [regA_val (hP.tcb rcv hr) (regIdxA_lt r')] at hr'; have := regIdxA_lt r'; omega + · rintro ⟨wp, hw, k, hk, he⟩ + have := hP.bt _ ⟨wp, hw, k, hk, he⟩ snd hs; have := ha.2.range; omega + +variable {A : Type} {ar : machine.Arch A} + +open Fl4SpecA64 in +/-- **`set_mrs_fault` of a cap fault** (x86 `set_mrs_fault_cap` / `set_mrs_fault_capN`): l4v's `make_fault_msg` +words, written under the message invariant `I` from `I s []`, the count `g |msg|`. -/ +theorem set_mrs_fault_capA (hP : EPlaced T E R Bf) (s : S) (a : A) (σ : State Unit) + (hR : EpStateRel T E R Bf (L.mem s) σ) {snd rcv : U64} (hs : snd ∈ T) (hr : rcv ∈ T) (hne : snd ≠ rcv) + {xs : Tcb} (hks : σ.kheap (specPtr snd) = some (.tcb xs)) {c : ObjRef} {rp : Bool} {lf : LookupFailure} + (hf : xs.tcbFault = some (.capFault c rp lf)) {I : S → List Word → Prop} {buf : U64} {g : Nat → Nat} + (hset : SetMrLawA st I rcv buf g) (h0 : I s []) + (hag : ∀ s' ms, I s' ms → Agree (L.mem s) (L.mem s') (fun b => ¬ RcvRegA rcv b ∧ ¬ BufW Bf b)) : + faults.set_mrs_fault st ar s a snd rcv buf ⦃ r => + r.1.val = g (capFaultMsgA (xs.tcbArch.tcbContext.userRegs .faultIP) c rp lf).length ∧ + I r.2 (capFaultMsgA (xs.tcbArch.tcbContext.userRegs .faultIP) c rp lf) ⦄ := by + have hsa := hP.tcb snd hs + have hxs := hR.at_tcb hs hks + have hfr := hxs.faultR + unfold FaultRel at hfr + rw [hf] at hfr + obtain ⟨hty, hw0, hw1, hlf⟩ := hfr + have hpc : (L.mem s (regWA snd layout.reg.FAULT_IP)).bv = xs.tcbArch.tcbContext.userRegs .faultIP := by + rw [← regIdxA_faultIP]; exact hxs.regs _ + unfold faults.set_mrs_fault + step with obj.Tcb.fault_type.spec as ⟨ty, hty'⟩ + step with obj.Tcb.fault_w0.spec as ⟨w0, hw0'⟩ + step with obj.Tcb.fault_w1.spec as ⟨w1, hw1'⟩ + step with obj.Tcb.fault_w2.spec as ⟨w2, hw2'⟩ + rw [tcb_reg_eqA, bind_ok] + subst hty' hw0' hw1' hw2' + rw [hty, if_pos (by unfold fl4_abi.consts.fault.CapFault; rfl)] + refine WP.spec_bind (hset _ _ h0 0#u64 rfl (by decide) _) ?_ + rintro ⟨_, s1⟩ ⟨-, hI1⟩ + refine WP.spec_bind (hset _ _ hI1 1#u64 rfl (by decide) _) ?_ + rintro ⟨_, s2⟩ ⟨-, hI2⟩ + refine WP.spec_bind (hset _ _ hI2 2#u64 rfl (by decide) _) ?_ + rintro ⟨_, s3⟩ ⟨-, hI3⟩ + unfold faults.lookup_failure + step with obj.Tcb.lf_type.spec as ⟨lt, hlt⟩ + step with obj.Tcb.lf_w0.spec as ⟨l0, hl0⟩ + step with obj.Tcb.lf_w1.spec as ⟨l1, hl1⟩ + rw [snd_word_eqA hP hs hr hne (hag _ _ hI3) (a_lf hsa)] at hlt + rw [snd_word_eqA hP hs hr hne (hag _ _ hI3) (a_lf0 hsa)] at hl0 + rw [snd_word_eqA hP hs hr hne (hag _ _ hI3) (a_lf1 hsa)] at hl1 + subst hlt hl0 hl1 + refine WP.spec_bind (decode_relA hlf) ?_ + rintro lfv hlfv + refine WP.spec_mono (set_mrs_lookup_failure_genA hset hI3 3#u64 rfl (by simp) lfv) ?_ + rintro ⟨n, s4⟩ ⟨hn, hI4⟩ + have hlist : [(L.mem s (regWA snd layout.reg.FAULT_IP)).bv, (L.mem s (tFw0A snd)).bv, (L.mem s (tFw1A snd)).bv] ++ + ((fl4LfMsgA lfv).map UScalar.bv : List Word) = + capFaultMsgA (xs.tcbArch.tcbContext.userRegs .faultIP) c rp lf := by + rw [hlfv, hpc, hw0, hw1] + unfold capFaultMsgA + cases rp <;> rfl + rw [List.nil_append, List.singleton_append, List.cons_append, List.singleton_append] at hI4 + refine ⟨?_, ?_⟩ + · simp only at hn + rw [hn] + refine congrArg g ?_ + rw [← hlist] + simp <;> omega + · rw [← hlist] + simpa using hI4 + +end Fl4 + +end + +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/EpFaultMoreA.lean b/proofs/Fl4ProofsA64/P3/EpFaultMoreA.lean new file mode 100644 index 000000000..aab0e5537 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/EpFaultMoreA.lean @@ -0,0 +1,608 @@ +/- +P3 (aarch64, A64-P3-IPC gen 10, ruling item 1): **the other arms of `set_mrs_fault`** (x86 `P3/EpFaultUs`'s +`set_mrs_fault_unknown`, `P3/EpFaultMore`'s `copy_mrs_fault_rel` / `set_mrs_fault_user` / `_timeout` / `_vm` and +`P3/EpFaultNone`'s `…N` twins, ported once for both receivers on gen 9's `SetMrLawA`). + +* **`MsgLawA I rcv buf g s`**: `SetMrLawA` with the start `I s []`, the sender-side agreement, and (no buffer) the + extension past the four inline registers; instances **`msgLaw_buf`** (`MrInvA`) and **`msgLaw_nobuf`** (`MrInvNA`). +* Spec: `mapM_getRegisterA`, `asUser_read_eqA`; the messages `usMsgA` / `ueMsgA` / `vmMsgA` and + `makeFaultMsg_unknownA` / `_userA` / `_timeoutNA` (no SC) / `_vmA` / `_vcpuA` / `_vppiA` / `_vgicA` (valid index). +* FL4: `SetMrLawA.regs` / `.bufs` (one `set_mr` as the register / buffer write `copy_mrs_fault` does), + **`copy_mrs_fault_genA`** (both loops, any register list whose FL4 slice is its `regIdxA` image), and the arms + **`set_mrs_fault_unknownA`**, **`_userA`**, **`_timeoutNA`**, **`_vmA`**, **`_vcpuA`**, **`_vppiA`**, **`_vgicA`**: + l4v's `make_fault_msg` words written under `I`, count `g |msg|`. + +Case selectors (no named premise): the arm's fault; `unknownSyscall`: `buf = 0 ∨` the sender's `TRAP_SYSCALLS` flag +clear (`TrapOffA`: FL4's l4h trap extension, not in the spec, `Ext/Tcb` item 3); timeout: no SC (`scA snd = 0`; the SC +case is `EpFaultTmoA`); VGIC maintenance: a valid index (**the invalid-index arm is false**: FL4 sends no word, seL4 +`Arch_setMRs_fault` and l4v send `-1`; reported, not proved). The debug arm is unreachable (`FaultRel'` has no type 4). +-/ +import Fl4ProofsA64.P3.EpFaultCapA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif + +noncomputable section + +section Spec +open Fl4SpecA64 + +theorem mapM_getRegisterA (uc : UserContext) : ∀ rs : List Register, + Fl4Spec.NonDet.mapM getRegister rs uc = ({(rs.map uc.userRegs, uc)}, False) + | [] => rfl + | r :: rs => by + unfold Fl4Spec.NonDet.mapM + rw [nd_bind_one' (show getRegister r uc = ({(uc.userRegs r, uc)}, False) from rfl), + nd_bind_one' (mapM_getRegisterA uc rs)] + rfl + +/-- `as_user` of a computation that only reads the user context (x86 `asUser_read_eq`). -/ +theorem asUser_read_eqA {α : Type} {σ : State Unit} {t : ObjRef} {x : Tcb} (hk : σ.kheap t = some (.tcb x)) + {f : UserMonad α} {a : α} + (hf : f (archTcbContextGet x.tcbArch) = ({(a, archTcbContextGet x.tcbArch)}, False)) : + asUser (Z := Unit) t f σ = ({(a, σ)}, False) := by + unfold asUser + rw [nd_bind_singleA (getTcb_oneA hk), nd_pure_bindA] + rw [nd_bind_singleA (show Fl4Spec.NonDet.selectF (σ := State Unit) (f (archTcbContextGet x.tcbArch)) σ = + ({((a, archTcbContextGet x.tcbArch), σ)}, False) by + rw [hf]; simp only [Fl4Spec.NonDet.selectF] + ext ⟨a', b⟩ <;> simp)] + dsimp only + rw [nd_pure_bindA] + have hx : ({ x with tcbArch := archTcbContextSet (archTcbContextGet x.tcbArch) x.tcbArch } : Tcb) = x := rfl + rw [hx, nd_bind_singleA (setObject_selfA hk)] + rfl + +/-- The fault message of an unknown-syscall fault (l4v `make_fault_msg`). -/ +def usMsgA (regs : Register → Word) (n : Word) : List Word := syscallMessage.map regs ++ [n] + +theorem makeFaultMsg_unknownA {σ : State Unit} {t : ObjRef} {x : Tcb} (hk : σ.kheap t = some (.tcb x)) (n : Word) : + makeFaultMsg (Z := Unit) (.unknownSyscallException n) t σ = + ({((2, usMsgA x.tcbArch.tcbContext.userRegs n), σ)}, False) := by + rw [makeFaultMsg, nd_bind_one (asUser_read_eqA hk (mapM_getRegisterA _ _))] + rfl + +/-- The fault message of a user exception. -/ +def ueMsgA (regs : Register → Word) (e c : Word) : List Word := exceptionMessage.map regs ++ [e, c] + +theorem makeFaultMsg_userA {σ : State Unit} {t : ObjRef} {x : Tcb} (hk : σ.kheap t = some (.tcb x)) (e c : Word) : + makeFaultMsg (Z := Unit) (.userException e c) t σ = + ({((3, ueMsgA x.tcbArch.tcbContext.userRegs e c), σ)}, False) := by + rw [makeFaultMsg, nd_bind_one (asUser_read_eqA hk (mapM_getRegisterA _ _))] + rfl + +theorem makeFaultMsg_timeoutNA {σ : State Unit} {t : ObjRef} {x : Tcb} (hk : σ.kheap t = some (.tcb x)) + (hsc : x.tcbSchedContext = none) (b : Word) : + makeFaultMsg (Z := Unit) (.timeout b) t σ = ({((5, [b]), σ)}, False) := by + rw [makeFaultMsg] + show (Fl4Spec.NonDet.getsThe _ >>= _) σ = _ + rw [nd_bind_one (getTcb_eq hk), hsc] + rfl + +/-- The fault message of a VM fault. -/ +def vmMsgA (pc a : Word) (d : List Word) : List Word := pc :: a :: d + +theorem makeFaultMsg_vmA {σ : State Unit} {t : ObjRef} {x : Tcb} (hk : σ.kheap t = some (.tcb x)) + (a : VSpaceRef) (d : List Word) : + makeFaultMsg (Z := Unit) (.archFault (.vmFault a d)) t σ = + ({((6, vmMsgA (x.tcbArch.tcbContext.userRegs .faultIP) a d), σ)}, False) := by + rw [makeFaultMsg, makeArchFaultMsg] + rw [show getRestartPC = getRegister .faultIP from rfl, nd_bind_one (asUser_getRegister_eqA hk .faultIP)] + rfl + +theorem makeFaultMsg_vcpuA (σ : State Unit) (t : ObjRef) (h : Data) : + makeFaultMsg (Z := Unit) (.archFault (.vcpuFault h)) t σ = ({((8, [h]), σ)}, False) := rfl + +theorem makeFaultMsg_vppiA (σ : State Unit) (t : ObjRef) (irq : Irq) : + makeFaultMsg (Z := Unit) (.archFault (.vppiEvent irq)) t σ = ({((9, [ucast 64 irq]), σ)}, False) := rfl + +theorem makeFaultMsg_vgicA (σ : State Unit) (t : ObjRef) (i : Data) : + makeFaultMsg (Z := Unit) (.archFault (.vgicMaintenance (some i))) t σ = ({((7, [i]), σ)}, False) := by + rw [makeFaultMsg, makeArchFaultMsg, nd_pure_bind'] + rfl + +end Spec + +/-! ## FL4's register lists -/ + +theorem syscall_slice_valA : + (Array.to_slice faults.SYSCALL_MESSAGE).val = Fl4SpecA64.syscallMessage.map regIdxA := by + unfold faults.SYSCALL_MESSAGE layout.reg.X0 layout.reg.X1 layout.reg.X2 layout.reg.X3 layout.reg.X4 + layout.reg.X5 layout.reg.X6 layout.reg.X7 layout.reg.FAULT_IP layout.reg.SP_EL0 layout.reg.ELR_EL1 + layout.reg.SPSR_EL1 + rfl + +theorem exc_slice_valA : + (Array.to_slice faults.EXCEPTION_MESSAGE).val = Fl4SpecA64.exceptionMessage.map regIdxA := by + unfold faults.EXCEPTION_MESSAGE layout.reg.FAULT_IP layout.reg.SP_EL0 layout.reg.SPSR_EL1 + rfl + +theorem us_lt_iffA {a b : Usize} : a < b ↔ a.val < b.val := by simp [UScalar.lt_equiv] + +theorem cast_usA (i : Usize) (h : i.val < 2 ^ 32) : (UScalar.cast .U64 i).val = i.val := by + rw [UScalar.cast_val_eq]; exact Nat.mod_eq_of_lt (by simp; omega) + +theorem take_succ_mapA {α β : Type} (f : α → β) (l : List α) (k : Nat) (hk : k < l.length) : + (l.take (k + 1)).map f = (l.take k).map f ++ [f l[k]] := by + simp only [List.take_succ, List.getElem?_eq_getElem hk, Option.toList_some, List.map_append, List.map_cons, + List.map_nil] + +section Fl4 +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {T E R : Set U64} + {Bf : Set (U64 × Fl4SpecA64.Word)} {SR : S → Fl4SpecA64.State Unit → Prop} + +/-- `set_mr` of an inline register, as the register write `copy_mrs_fault` does. -/ +theorem SetMrLawA.regs {I : S → List Fl4SpecA64.Word → Prop} {rcv buf : U64} {g : Nat → Nat} + (hset : SetMrLawA st I rcv buf g) {s : S} {ms : List Fl4SpecA64.Word} (hI : I s ms) (i : U64) + (hi : i.val = ms.length) (h4 : i.val < 4) (v : U64) : + I (L.wr s (regA rcv (regIdxA (mrIdx i.val))) v) (ms ++ [v.bv]) := by + have h := hset s ms hI i hi (by omega) v + rw [set_mr_reg s rcv buf i v h4, WP.spec_ok] at h + exact h.2 + +/-- `set_mr` past the inline registers, as the buffer write `copy_mrs_fault` does. -/ +theorem SetMrLawA.bufs {I : S → List Fl4SpecA64.Word → Prop} {rcv buf : U64} {g : Nat → Nat} + (hset : SetMrLawA st I rcv buf g) (hb : buf ≠ 0#u64) {s : S} {ms : List Fl4SpecA64.Word} (hI : I s ms) (i : U64) + (hi : i.val = ms.length) (h4 : 4 ≤ i.val) (h120 : i.val < 120) (v : U64) : + I (L.wr s (bufW buf (core.num.U64.wrapping_add i 1#u64)) v) (ms ++ [v.bv]) := by + have h := hset s ms hI i hi h120 v + rw [set_mr_buf s rcv buf i v h4 hb, WP.spec_ok] at h + exact h.2 + +variable (st Bf) in +/-- The law of a message invariant `I` from the state `s` (gen 10): `SetMrLawA`, the start, the sender side kept, +and (no buffer) nothing past the four inline registers. -/ +structure MsgLawA (I : S → List Fl4SpecA64.Word → Prop) (rcv buf : U64) (g : Nat → Nat) (s : S) : Prop where + set : SetMrLawA st I rcv buf g + ext : buf = 0#u64 → ∀ s' ms ys, I s' ms → 4 ≤ ms.length → I s' (ms ++ ys) + start : I s [] + agree : ∀ s' ms, I s' ms → Agree (L.mem s) (L.mem s') (fun b => ¬ RcvRegA rcv b ∧ ¬ BufW Bf b) + +theorem msgLaw_buf (hO : SchedOpsE0A st T E R Bf SR) (hP : EPlaced T E R Bf) {s : S} {σ : Fl4SpecA64.State Unit} + (hR : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) {rcv : U64} (hr : rcv ∈ T) {xr : Fl4SpecA64.Tcb} + (hkr : σ.kheap (specPtr rcv) = some (.tcb xr)) {bp : U64 × Fl4SpecA64.Word} (hb : bp ∈ Bf) (hb0 : bp.1 ≠ 0#u64) : + MsgLawA st Bf (MrInvA (L := L) T E R Bf SR s σ rcv xr bp.2) rcv bp.1 id s := + ⟨setMrLaw_buf hO hP hr xr hb hb0, fun h => absurd h hb0, MrInvA.nil hkr bp.2 hR hSR, fun _ _ hI => hI.2.2⟩ + +theorem msgLaw_nobuf (hO : SchedOpsE0A st T E R Bf SR) (hP : EPlaced T E R Bf) {s : S} {σ : Fl4SpecA64.State Unit} + (hR : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) {rcv : U64} (hr : rcv ∈ T) {xr : Fl4SpecA64.Tcb} + (hkr : σ.kheap (specPtr rcv) = some (.tcb xr)) : + MsgLawA st Bf (MrInvNA (L := L) T E R Bf SR s σ rcv xr) rcv 0#u64 (fun n => min n 4) s := + ⟨setMrLaw_nobuf hO hP hr xr, fun _ _ _ ys hI h => hI.append_ge h ys, MrInvNA.nil hR hSR hkr, + fun _ _ hI => hI.2.2⟩ + +variable {I : S → List Fl4SpecA64.Word → Prop} {buf : U64} {g : Nat → Nat} + +/-- The first loop of `copy_mrs_fault` (x86 `copy_mrs_fault_loop0_rel`): message words `0 .. min |rl| 4`. -/ +theorem copy_mrs_fault_loop0_genA (hP : EPlaced T E R Bf) {snd rcv : U64} (hs : snd ∈ T) (hr : rcv ∈ T) + (hne : snd ≠ rcv) {s0 : S} (hM : MsgLawA st Bf I rcv buf g s0) {regs : Fl4SpecA64.Register → Fl4SpecA64.Word} + (hreg : ∀ r, (L.mem s0 (regA snd (regIdxA r))).bv = regs r) {sl : Slice U64} {rl : List Fl4SpecA64.Register} + (hsl : sl.val = rl.map regIdxA) (hn : rl.length ≤ 120) : + faults.copy_mrs_fault_loop0 st s0 snd rcv sl 0#usize ⦃ r => + r.2.val = min rl.length 4 ∧ I r.1 ((rl.take r.2.val).map regs) ⦄ := by + have hN : msg.N_MSG_REGISTERS.val = 4 := by unfold msg.N_MSG_REGISTERS; rfl + have hlen : (Slice.len sl).val = rl.length := by simp [Slice.len, hsl] + have hsa := hP.tcb snd hs + unfold faults.copy_mrs_fault_loop0 + apply loop.spec_decr_nat (measure := fun x => 4 - x.2.val) + (inv := fun x => x.2.val ≤ min rl.length 4 ∧ I x.1 ((rl.take x.2.val).map regs)) + · rintro ⟨s', i⟩ ⟨hi4, hI⟩ + unfold faults.copy_mrs_fault_loop0.body + simp only at hi4 hI ⊢ + by_cases h1 : i < Slice.len sl + · have hin : i.val < rl.length := by have := us_lt_iffA.1 h1; omega + simp only [h1, ↓reduceIte] + step as ⟨i2, hi2⟩ + have hi2v : i2.val = i.val := by rw [hi2]; exact cast_usA i (by omega) + by_cases h2 : i2 < msg.N_MSG_REGISTERS + · have hi : i.val < 4 := by have := (gt_iff_val).1 h2; omega + simp only [h2, ↓reduceIte] + step as ⟨r, hr'⟩ + rw [tcb_reg_eqA, bind_ok] + step as ⟨i4, hi4'⟩ + have hi4v : i4.val = i.val := by rw [hi4']; exact cast_usA i (by omega) + rw [msg_register_regIdxA (show i4.val < 4 by omega), bind_ok, set_reg_eqA, bind_ok] + have hlen' : ((rl.take i.val).map regs).length = i.val := by simp; omega + have hI1 := hM.set.regs hI i4 (by rw [hi4v, hlen']) (by omega) (L.mem s' (regWA snd r)) + step as ⟨i6, hi6⟩ + have hi6v : i6.val = i.val + 1 := by simpa using hi6 + have hr2 : r = regIdxA (rl[i.val]'hin) := by rw [hr']; simp [hsl] + have hvb : (L.mem s' (regWA snd r)).bv = regs (rl[i.val]'hin) := by + rw [hr2, ← regA_eq_regWA, snd_word_eqA hP hs hr hne (hM.agree _ _ hI) (a_reg hsa _), hreg] + refine ⟨by omega, ?_, by omega⟩ + rw [hi6v, take_succ_mapA regs rl i.val hin, ← hvb] + exact hI1 + · simp only [h2, ↓reduceIte, WP.spec_ok] + have hi : i.val = 4 := by + have h2' : ¬ i.val < 4 := fun h => h2 ((gt_iff_val).2 (by rw [hN]; omega)) + omega + exact ⟨by omega, hI⟩ + · simp only [h1, ↓reduceIte, WP.spec_ok] + have : ¬ i.val < rl.length := fun h => h1 (us_lt_iffA.2 (by omega)) + exact ⟨by omega, hI⟩ + · refine ⟨by simp, ?_⟩ + simpa using hM.start + +/-- The second loop of `copy_mrs_fault` (x86 `copy_mrs_fault_loop1_rel`): message words `4 .. |rl|`, in the buffer. -/ +theorem copy_mrs_fault_loop1_genA (hP : EPlaced T E R Bf) {snd rcv : U64} (hs : snd ∈ T) (hr : rcv ∈ T) + (hne : snd ≠ rcv) {s0 : S} (hM : MsgLawA st Bf I rcv buf g s0) (hb : buf ≠ 0#u64) + {regs : Fl4SpecA64.Register → Fl4SpecA64.Word} (hreg : ∀ r, (L.mem s0 (regA snd (regIdxA r))).bv = regs r) + {sl : Slice U64} {rl : List Fl4SpecA64.Register} (hsl : sl.val = rl.map regIdxA) (hn : rl.length ≤ 120) + (s1 : S) (i0 : Usize) (hi0 : i0.val = min rl.length 4) (hI : I s1 ((rl.take i0.val).map regs)) : + faults.copy_mrs_fault_loop1 st s1 snd sl buf i0 ⦃ s2 => I s2 (rl.map regs) ⦄ := by + have hlen : (Slice.len sl).val = rl.length := by simp [Slice.len, hsl] + have hsa := hP.tcb snd hs + unfold faults.copy_mrs_fault_loop1 + apply loop.spec_decr_nat (measure := fun x => rl.length - x.2.val) + (inv := fun x => min rl.length 4 ≤ x.2.val ∧ x.2.val ≤ rl.length ∧ I x.1 ((rl.take x.2.val).map regs)) + · rintro ⟨s', i⟩ ⟨h4, hle, hI'⟩ + unfold faults.copy_mrs_fault_loop1.body + simp only at h4 hle hI' ⊢ + by_cases h1 : i < Slice.len sl + · have hi : i.val < rl.length := by have := us_lt_iffA.1 h1; omega + simp only [h1, ↓reduceIte] + step as ⟨r, hr'⟩ + rw [tcb_reg_eqA, bind_ok] + step as ⟨i3, hi3⟩ + have h3v : i3.val = i.val := by rw [hi3]; exact cast_usA i (by omega) + step as ⟨i4, hi4⟩ + subst hi4 + rw [buf_set_eq, bind_ok] + have hlen' : ((rl.take i.val).map regs).length = i.val := by simp; omega + have hI3 := hM.set.bufs hb hI' i3 (by rw [h3v, hlen']) (by omega) (by omega) (L.mem s' (regWA snd r)) + step as ⟨i5, hi5⟩ + have hi5v : i5.val = i.val + 1 := by simpa using hi5 + have hr2 : r = regIdxA (rl[i.val]'hi) := by rw [hr']; simp [hsl] + have hvb : (L.mem s' (regWA snd r)).bv = regs (rl[i.val]'hi) := by + rw [hr2, ← regA_eq_regWA, snd_word_eqA hP hs hr hne (hM.agree _ _ hI') (a_reg hsa _), hreg] + refine ⟨by omega, by omega, ?_, by omega⟩ + rw [hi5v, take_succ_mapA regs rl i.val hi, ← hvb] + exact hI3 + · simp only [h1, ↓reduceIte, WP.spec_ok] + have hi : i.val = rl.length := by + have : ¬ i.val < rl.length := fun h => h1 (us_lt_iffA.2 (by omega)) + omega + rw [hi, List.take_of_length_le (le_refl _)] at hI' + exact hI' + · simp only [hi0] + exact ⟨le_refl _, by omega, hi0 ▸ hI⟩ + +/-- **`copy_mrs_fault`** of the sender's registers `rl` (x86 `copy_mrs_fault_rel` and its no-buffer twin): +message words `0 .. |rl|` under `I`. -/ +theorem copy_mrs_fault_genA (hP : EPlaced T E R Bf) {snd rcv : U64} (hs : snd ∈ T) (hr : rcv ∈ T) + (hne : snd ≠ rcv) {s : S} (hM : MsgLawA st Bf I rcv buf g s) {regs : Fl4SpecA64.Register → Fl4SpecA64.Word} + (hreg : ∀ r, (L.mem s (regA snd (regIdxA r))).bv = regs r) {sl : Slice U64} {rl : List Fl4SpecA64.Register} + (hsl : sl.val = rl.map regIdxA) (hn : rl.length ≤ 120) : + faults.copy_mrs_fault st s snd rcv sl buf ⦃ s2 => I s2 (rl.map regs) ⦄ := by + unfold faults.copy_mrs_fault + refine WP.spec_bind (copy_mrs_fault_loop0_genA hP hs hr hne hM hreg hsl hn) ?_ + rintro ⟨s1, i⟩ ⟨hi, hI⟩ + by_cases hb : buf = 0#u64 + · have hb' : (buf != 0#u64) = false := by simp [hb] + simp only [hb', Bool.false_eq_true, ↓reduceIte] + show (ok s1 : Result S) ⦃ fun s2 => I s2 (rl.map regs) ⦄ + rw [WP.spec_ok] + rw [hi] at hI + by_cases h4 : rl.length ≤ 4 + · rw [Nat.min_eq_left h4, List.take_of_length_le (le_refl _)] at hI; exact hI + · rw [Nat.min_eq_right (by omega)] at hI + have := hM.ext hb _ _ ((rl.drop 4).map regs) hI (by simp; omega) + rw [← List.map_append, List.take_append_drop] at this + exact this + · have hb' : (buf != 0#u64) = true := by simpa using hb + simp only [hb', ↓reduceIte] + exact copy_mrs_fault_loop1_genA hP hs hr hne hM hb hreg hsl hn s1 i hi hI + +/-! ## The arms of `set_mrs_fault` -/ + +/-- The sender's TCB flags word (`tcb::FLAGS`, `+3456`). -/ +def tTflA (t : U64) : U64 := core.num.U64.wrapping_add t 3456#u64 + +theorem tcb_flags_eqA (s : S) (t : U64) : obj.Tcb.flags st t s ⦃ v => v = L.mem s (tTflA t) ⦄ := by + unfold obj.Tcb.flags; step*; simp_all [tTflA] + +/-- The sender's flags word while the message is written. -/ +theorem snd_flags_eqA {s0 s : S} (hP : EPlaced T E R Bf) {snd rcv : U64} (hs : snd ∈ T) (hr : rcv ∈ T) + (hne : snd ≠ rcv) (hA : Agree (L.mem s0) (L.mem s) (fun b => ¬ RcvRegA rcv b ∧ ¬ BufW Bf b)) : + L.mem s (tTflA snd) = L.mem s0 (tTflA snd) := by + have hsa := hP.tcb snd hs + have hv : (tTflA snd).val = snd.val + 3456 := tw_val hsa (by decide) + have := hsa.align; have := hsa.fits + refine hA _ (by unfold Aligned; rw [hv]; omega) ⟨?_, ?_⟩ + · rintro ⟨r', hr'⟩ + have := tcb_disj hP hr hs hne + rw [regA_val (hP.tcb rcv hr) (regIdxA_lt r')] at hr'; have := regIdxA_lt r'; omega + · rintro ⟨wp, hw, k, hk, he⟩ + have := hP.bt _ ⟨wp, hw, k, hk, he⟩ snd hs; omega + +/-- The case selector of an unknown-syscall fault: no receive buffer, or the sender's `TRAP_SYSCALLS` flag clear +(FL4's l4h trap extension writes 24 more words and latches `FAULT_W1`; the spec has no such flag). -/ +def TrapOffA (s : S) (snd buf : U64) : Prop := + buf = 0#u64 ∨ (L.mem s (tTflA snd) &&& fl4_abi.consts.tcb_flag.TRAP_SYSCALLS) = 0#u64 + +theorem trap_extra_mrs_offA (s : S) (snd buf n : U64) + (h : buf = 0#u64 ∨ (L.mem s (tTflA snd) &&& fl4_abi.consts.tcb_flag.TRAP_SYSCALLS) = 0#u64) : + faults.trap_extra_mrs st s snd buf n = ok (n, s) := by + unfold faults.trap_extra_mrs + by_cases hb : buf = 0#u64 + · rw [if_pos hb] + · rw [if_neg hb] + by_cases hn : (n != faults.TRAP_EXTRA_FIRST) = true + · rw [if_pos hn] + · rw [if_neg hn] + rw [eq_of_spec_fd (tcb_flags_eqA (L := L) s snd)] + simp only [lift, bind_ok, h.resolve_left hb, ↓reduceIte] + +variable {A : Type} {ar : machine.Arch A} + +open Fl4SpecA64 in +/-- **`set_mrs_fault` of an unknown-syscall fault** (x86 `set_mrs_fault_unknown`). -/ +theorem set_mrs_fault_unknownA (hP : EPlaced T E R Bf) (s : S) (a : A) (σ : State Unit) + (hR : EpStateRel T E R Bf (L.mem s) σ) {snd rcv : U64} (hs : snd ∈ T) (hr : rcv ∈ T) (hne : snd ≠ rcv) + {xs : Tcb} (hks : σ.kheap (specPtr snd) = some (.tcb xs)) {n : Word} + (hf : xs.tcbFault = some (.unknownSyscallException n)) (hM : MsgLawA st Bf I rcv buf g s) + (htr : TrapOffA (L := L) s snd buf) : + faults.set_mrs_fault st ar s a snd rcv buf ⦃ r => + r.1.val = g (usMsgA xs.tcbArch.tcbContext.userRegs n).length ∧ + I r.2 (usMsgA xs.tcbArch.tcbContext.userRegs n) ⦄ := by + have hxs := hR.at_tcb hs hks + have hfr := hxs.faultR + unfold FaultRel at hfr + rw [hf] at hfr + obtain ⟨hty, hw0⟩ := hfr + unfold faults.set_mrs_fault + step with obj.Tcb.fault_type.spec as ⟨ty, hty'⟩ + step with obj.Tcb.fault_w0.spec as ⟨w0, hw0'⟩ + step with obj.Tcb.fault_w1.spec as ⟨w1, hw1'⟩ + step with obj.Tcb.fault_w2.spec as ⟨w2, hw2'⟩ + rw [tcb_reg_eqA, bind_ok] + subst hty' hw0' hw1' hw2' + rw [hty, if_neg (by unfold fl4_abi.consts.fault.CapFault; decide), + if_pos (by unfold fl4_abi.consts.fault.UnknownSyscall; rfl)] + simp only [lift, bind_ok] + refine WP.spec_bind (copy_mrs_fault_genA hP hs hr hne hM (fun r => hxs.regs r) syscall_slice_valA + (by decide)) ?_ + rintro s2 hI2 + refine WP.spec_bind (hM.set _ _ hI2 12#u64 (by simp [syscallMessage, Register.x0ToX7]) (by decide) + (L.mem s (tFw0A snd))) ?_ + rintro ⟨k, s3⟩ ⟨hk, hI3⟩ + rw [hw0] at hI3 + show (faults.trap_extra_mrs st s3 snd buf k) ⦃ _ ⦄ + rw [trap_extra_mrs_offA s3 snd buf k (htr.imp id fun h => by rw [snd_flags_eqA hP hs hr hne (hM.agree _ _ hI3)]; exact h), + WP.spec_ok] + refine ⟨?_, hI3⟩ + rw [hk]; rfl + +open Fl4SpecA64 in +/-- **`set_mrs_fault` of a user exception** (x86 `set_mrs_fault_user`). -/ +theorem set_mrs_fault_userA (hP : EPlaced T E R Bf) (s : S) (a : A) (σ : State Unit) + (hR : EpStateRel T E R Bf (L.mem s) σ) {snd rcv : U64} (hs : snd ∈ T) (hr : rcv ∈ T) (hne : snd ≠ rcv) + {xs : Tcb} (hks : σ.kheap (specPtr snd) = some (.tcb xs)) {e c : Word} + (hf : xs.tcbFault = some (.userException e c)) (hM : MsgLawA st Bf I rcv buf g s) : + faults.set_mrs_fault st ar s a snd rcv buf ⦃ r => + r.1.val = g (ueMsgA xs.tcbArch.tcbContext.userRegs e c).length ∧ + I r.2 (ueMsgA xs.tcbArch.tcbContext.userRegs e c) ⦄ := by + have hxs := hR.at_tcb hs hks + have hfr := hxs.faultR + unfold FaultRel at hfr + rw [hf] at hfr + obtain ⟨hty, hw0, hw1⟩ := hfr + unfold faults.set_mrs_fault + step with obj.Tcb.fault_type.spec as ⟨ty, hty'⟩ + step with obj.Tcb.fault_w0.spec as ⟨w0, hw0'⟩ + step with obj.Tcb.fault_w1.spec as ⟨w1, hw1'⟩ + step with obj.Tcb.fault_w2.spec as ⟨w2, hw2'⟩ + rw [tcb_reg_eqA, bind_ok] + subst hty' hw0' hw1' hw2' + rw [hty, if_neg (by unfold fl4_abi.consts.fault.CapFault; decide), + if_neg (by unfold fl4_abi.consts.fault.UnknownSyscall; decide), + if_pos (by unfold fl4_abi.consts.fault.UserException; rfl)] + simp only [lift, bind_ok] + refine WP.spec_bind (copy_mrs_fault_genA hP hs hr hne hM (fun r => hxs.regs r) exc_slice_valA + (by decide)) ?_ + rintro s2 hI2 + refine WP.spec_bind (hM.set _ _ hI2 3#u64 (by simp [exceptionMessage]) (by decide) (L.mem s (tFw0A snd))) ?_ + rintro ⟨k3, s3⟩ ⟨-, hI3⟩ + refine WP.spec_mono (hM.set _ _ hI3 4#u64 (by simp [exceptionMessage]) (by decide) (L.mem s (tFw1A snd))) ?_ + rintro ⟨k4, s4⟩ ⟨hk4, hI4⟩ + rw [hw0, hw1, List.append_assoc] at hI4 + exact ⟨by rw [hk4]; rfl, hI4⟩ + +open Fl4SpecA64 in +/-- The timeout arm of `set_mrs_fault`, by the sender's SC word (shared by `set_mrs_fault_timeoutNA` and +`EpFaultTmoA`). -/ +theorem set_mrs_fault_tmo_specA (s : S) (a : A) {snd rcv buf : U64} {P : U64 × S → Prop} + (hty : L.mem s (tFaultA snd) = 5#u64) + (h0 : L.mem s (scA snd) = 0#u64 → msg.set_mr st s rcv buf 0#u64 (L.mem s (tFw0A snd)) ⦃ P ⦄) + (h1 : L.mem s (scA snd) ≠ 0#u64 → (do + let (consumed, s1) ← schedcontext.update_consumed st ar s a (L.mem s (scA snd)) + let (len, s2) ← msg.set_mr st s1 rcv buf 0#u64 (L.mem s (tFw0A snd)) + msg.set_mr st s2 rcv buf len consumed) ⦃ P ⦄) : + faults.set_mrs_fault st ar s a snd rcv buf ⦃ P ⦄ := by + unfold faults.set_mrs_fault + step with obj.Tcb.fault_type.spec as ⟨ty, hty'⟩ + step with obj.Tcb.fault_w0.spec as ⟨w0, hw0'⟩ + step with obj.Tcb.fault_w1.spec as ⟨w1, hw1'⟩ + step with obj.Tcb.fault_w2.spec as ⟨w2, hw2'⟩ + rw [tcb_reg_eqA, bind_ok] + subst hty' hw0' hw1' hw2' + rw [hty, if_neg (by unfold fl4_abi.consts.fault.CapFault; decide), + if_neg (by unfold fl4_abi.consts.fault.UnknownSyscall; decide), + if_neg (by unfold fl4_abi.consts.fault.UserException; decide), + if_pos (by unfold fl4_abi.consts.fault.Timeout; rfl)] + step with obj.Tcb.sc.spec as ⟨i, hi⟩ + subst hi + rw [eq_of_spec_fd (obj.Sc.is_null.spec _), bind_ok] + by_cases hz : L.mem s (scA snd) = 0#u64 + · simp only [hz, decide_true, ↓reduceIte] + exact h0 hz + · simp only [decide_eq_false hz, Bool.false_eq_true, ↓reduceIte] + exact h1 hz + +open Fl4SpecA64 in +/-- **`set_mrs_fault` of a timeout fault of a sender without SC** (x86 `set_mrs_fault_timeout`). -/ +theorem set_mrs_fault_timeoutNA (hP : EPlaced T E R Bf) (s : S) (a : A) (σ : State Unit) + (hR : EpStateRel T E R Bf (L.mem s) σ) {snd rcv : U64} (hs : snd ∈ T) + {xs : Tcb} (hks : σ.kheap (specPtr snd) = some (.tcb xs)) {b : Word} + (hf : xs.tcbFault = some (.timeout b)) (hsc : L.mem s (scA snd) = 0#u64) (hM : MsgLawA st Bf I rcv buf g s) : + faults.set_mrs_fault st ar s a snd rcv buf ⦃ r => r.1.val = g [b].length ∧ I r.2 [b] ⦄ := by + have hxs := hR.at_tcb hs hks + have hfr := hxs.faultR + unfold FaultRel at hfr + rw [hf] at hfr + obtain ⟨hty, hw0⟩ := hfr + refine set_mrs_fault_tmo_specA s a hty (fun _ => ?_) (fun h => absurd hsc h) + refine WP.spec_mono (hM.set _ _ hM.start 0#u64 rfl (by decide) (L.mem s (tFw0A snd))) ?_ + rintro ⟨k, s1⟩ ⟨hk, hI⟩ + rw [hw0, List.nil_append] at hI + exact ⟨by rw [hk]; rfl, hI⟩ + +open Fl4SpecA64 in +/-- **`set_mrs_fault` of a VM fault** (x86 `set_mrs_fault_vm`). -/ +theorem set_mrs_fault_vmA (hP : EPlaced T E R Bf) (s : S) (a : A) (σ : State Unit) + (hR : EpStateRel T E R Bf (L.mem s) σ) {snd rcv : U64} (hs : snd ∈ T) + {xs : Tcb} (hks : σ.kheap (specPtr snd) = some (.tcb xs)) {va : VSpaceRef} {d : List Word} + (hf : xs.tcbFault = some (.archFault (.vmFault va d))) (hM : MsgLawA st Bf I rcv buf g s) : + faults.set_mrs_fault st ar s a snd rcv buf ⦃ r => + r.1.val = g (vmMsgA (xs.tcbArch.tcbContext.userRegs .faultIP) va d).length ∧ + I r.2 (vmMsgA (xs.tcbArch.tcbContext.userRegs .faultIP) va d) ⦄ := by + have hxs := hR.at_tcb hs hks + have hfr := hxs.faultR + unfold FaultRel at hfr + rw [hf] at hfr + obtain ⟨hty, hw0, hd⟩ := hfr + have hpc : (L.mem s (regWA snd layout.reg.FAULT_IP)).bv = xs.tcbArch.tcbContext.userRegs .faultIP := by + rw [← regIdxA_faultIP]; exact hxs.regs _ + unfold faults.set_mrs_fault + step with obj.Tcb.fault_type.spec as ⟨ty, hty'⟩ + step with obj.Tcb.fault_w0.spec as ⟨w0, hw0'⟩ + step with obj.Tcb.fault_w1.spec as ⟨w1, hw1'⟩ + step with obj.Tcb.fault_w2.spec as ⟨w2, hw2'⟩ + rw [tcb_reg_eqA, bind_ok] + subst hty' hw0' hw1' hw2' + rw [hty, if_neg (by unfold fl4_abi.consts.fault.CapFault; decide), + if_neg (by unfold fl4_abi.consts.fault.UnknownSyscall; decide), + if_neg (by unfold fl4_abi.consts.fault.UserException; decide), + if_neg (by unfold fl4_abi.consts.fault.Timeout; decide), + if_neg (by unfold fl4_abi.consts.fault.DebugException; decide), + if_pos (by unfold fl4_abi.consts.fault.VMFault; rfl)] + refine WP.spec_bind (hM.set _ _ hM.start 0#u64 rfl (by decide) _) ?_ + rintro ⟨_, s1⟩ ⟨-, hI1⟩ + refine WP.spec_bind (hM.set _ _ hI1 1#u64 rfl (by decide) _) ?_ + rintro ⟨_, s2⟩ ⟨-, hI2⟩ + refine WP.spec_bind (hM.set _ _ hI2 2#u64 rfl (by decide) _) ?_ + rintro ⟨_, s3⟩ ⟨-, hI3⟩ + refine WP.spec_mono (hM.set _ _ hI3 3#u64 rfl (by decide) _) ?_ + rintro ⟨k, s4⟩ ⟨hk, hI4⟩ + have hl : [(L.mem s (regWA snd layout.reg.FAULT_IP)).bv] ++ [(L.mem s (tFw0A snd)).bv] ++ + [(L.mem s (tFw2A snd)).bv] ++ [(L.mem s (tFw1A snd)).bv] = + vmMsgA (xs.tcbArch.tcbContext.userRegs .faultIP) va d := by + rw [hpc, hw0, hd]; rfl + rw [List.nil_append, hl] at hI4 + exact ⟨by rw [hk, ← hl]; rfl, hI4⟩ + +open Fl4SpecA64 in +/-- The arms writing the one word `w0` at index 0 (`VCPUFault`, `VPPIEvent`, `VGICMaintenance` with an index). -/ +theorem set_mr_w0A {s : S} (hM : MsgLawA st Bf I rcv buf g s) {snd : U64} {w : Word} + (hw : (L.mem s (tFw0A snd)).bv = w) : + msg.set_mr st s rcv buf 0#u64 (L.mem s (tFw0A snd)) ⦃ r => r.1.val = g [w].length ∧ I r.2 [w] ⦄ := + WP.spec_mono (hM.set _ _ hM.start 0#u64 rfl (by decide) _) fun _ ⟨hk, hI⟩ => by + rw [List.nil_append, hw] at hI; exact ⟨by rw [hk]; rfl, hI⟩ + +open Fl4SpecA64 in +/-- **`set_mrs_fault` of a VCPU fault**. -/ +theorem set_mrs_fault_vcpuA (s : S) (a : A) (σ : State Unit) + (hR : EpStateRel T E R Bf (L.mem s) σ) {snd rcv : U64} (hs : snd ∈ T) + {xs : Tcb} (hks : σ.kheap (specPtr snd) = some (.tcb xs)) {h : Data} + (hf : xs.tcbFault = some (.archFault (.vcpuFault h))) (hM : MsgLawA st Bf I rcv buf g s) : + faults.set_mrs_fault st ar s a snd rcv buf ⦃ r => r.1.val = g [h].length ∧ I r.2 [h] ⦄ := by + have hfr := (hR.at_tcb hs hks).faultR + unfold FaultRel at hfr + rw [hf] at hfr + obtain ⟨hty, hw0⟩ := hfr + unfold faults.set_mrs_fault + step with obj.Tcb.fault_type.spec as ⟨ty, hty'⟩ + step with obj.Tcb.fault_w0.spec as ⟨w0, hw0'⟩ + step with obj.Tcb.fault_w1.spec as ⟨w1, hw1'⟩ + step with obj.Tcb.fault_w2.spec as ⟨w2, hw2'⟩ + rw [tcb_reg_eqA, bind_ok] + subst hty' hw0' hw1' hw2' + rw [hty, if_neg (by unfold fl4_abi.consts.fault.CapFault; decide), + if_neg (by unfold fl4_abi.consts.fault.UnknownSyscall; decide), + if_neg (by unfold fl4_abi.consts.fault.UserException; decide), + if_neg (by unfold fl4_abi.consts.fault.Timeout; decide), + if_neg (by unfold fl4_abi.consts.fault.DebugException; decide), + if_neg (by unfold fl4_abi.consts.fault.VMFault; decide), + if_pos (by unfold fl4_abi.consts.fault.VCPUFault; rfl)] + exact set_mr_w0A hM hw0 + +open Fl4SpecA64 in +/-- **`set_mrs_fault` of a VGIC maintenance fault with a valid index**. -/ +theorem set_mrs_fault_vgicA (s : S) (a : A) (σ : State Unit) + (hR : EpStateRel T E R Bf (L.mem s) σ) {snd rcv : U64} (hs : snd ∈ T) + {xs : Tcb} (hks : σ.kheap (specPtr snd) = some (.tcb xs)) {i : Data} + (hf : xs.tcbFault = some (.archFault (.vgicMaintenance (some i)))) (hM : MsgLawA st Bf I rcv buf g s) : + faults.set_mrs_fault st ar s a snd rcv buf ⦃ r => r.1.val = g [i].length ∧ I r.2 [i] ⦄ := by + have hfr := (hR.at_tcb hs hks).faultR + unfold FaultRel at hfr + rw [hf] at hfr + obtain ⟨hty, hw1, hw0⟩ := hfr + unfold faults.set_mrs_fault + step with obj.Tcb.fault_type.spec as ⟨ty, hty'⟩ + step with obj.Tcb.fault_w0.spec as ⟨w0, hw0'⟩ + step with obj.Tcb.fault_w1.spec as ⟨w1, hw1'⟩ + step with obj.Tcb.fault_w2.spec as ⟨w2, hw2'⟩ + rw [tcb_reg_eqA, bind_ok] + subst hty' hw0' hw1' hw2' + rw [hty, if_neg (by unfold fl4_abi.consts.fault.CapFault; decide), + if_neg (by unfold fl4_abi.consts.fault.UnknownSyscall; decide), + if_neg (by unfold fl4_abi.consts.fault.UserException; decide), + if_neg (by unfold fl4_abi.consts.fault.Timeout; decide), + if_neg (by unfold fl4_abi.consts.fault.DebugException; decide), + if_neg (by unfold fl4_abi.consts.fault.VMFault; decide), + if_neg (by unfold fl4_abi.consts.fault.VCPUFault; decide), + if_pos (by unfold fl4_abi.consts.fault.VGICMaintenance; rfl), hw1] + simp only [show ((1#u64 : U64) != 0#u64) = true by decide, ↓reduceIte] + exact set_mr_w0A hM hw0 + +open Fl4SpecA64 in +/-- **`set_mrs_fault` of a virtual PPI event**. -/ +theorem set_mrs_fault_vppiA (s : S) (a : A) (σ : State Unit) + (hR : EpStateRel T E R Bf (L.mem s) σ) {snd rcv : U64} (hs : snd ∈ T) + {xs : Tcb} (hks : σ.kheap (specPtr snd) = some (.tcb xs)) {irq : Irq} + (hf : xs.tcbFault = some (.archFault (.vppiEvent irq))) (hM : MsgLawA st Bf I rcv buf g s) : + faults.set_mrs_fault st ar s a snd rcv buf ⦃ r => + r.1.val = g [ucast 64 irq].length ∧ I r.2 [ucast 64 irq] ⦄ := by + have hfr := (hR.at_tcb hs hks).faultR + unfold FaultRel at hfr + rw [hf] at hfr + obtain ⟨hty, hw0⟩ := hfr + have hw : (L.mem s (tFw0A snd)).bv = ucast 64 irq := by + apply BitVec.eq_of_toNat_eq + have hlt : irq.toNat < 2 ^ 13 := irq.isLt + rw [BitVec.toNat_setWidth, Nat.mod_eq_of_lt (by omega)] + exact hw0 + unfold faults.set_mrs_fault + step with obj.Tcb.fault_type.spec as ⟨ty, hty'⟩ + step with obj.Tcb.fault_w0.spec as ⟨w0, hw0'⟩ + step with obj.Tcb.fault_w1.spec as ⟨w1, hw1'⟩ + step with obj.Tcb.fault_w2.spec as ⟨w2, hw2'⟩ + rw [tcb_reg_eqA, bind_ok] + subst hty' hw0' hw1' hw2' + rw [hty, if_neg (by unfold fl4_abi.consts.fault.CapFault; decide), + if_neg (by unfold fl4_abi.consts.fault.UnknownSyscall; decide), + if_neg (by unfold fl4_abi.consts.fault.UserException; decide), + if_neg (by unfold fl4_abi.consts.fault.Timeout; decide), + if_neg (by unfold fl4_abi.consts.fault.DebugException; decide), + if_neg (by unfold fl4_abi.consts.fault.VMFault; decide), + if_neg (by unfold fl4_abi.consts.fault.VCPUFault; decide), + if_neg (by unfold fl4_abi.consts.fault.VGICMaintenance; decide), + if_pos (by unfold fl4_abi.consts.fault.VPPIEvent; rfl)] + exact set_mr_w0A hM hw + +end Fl4 + +end + +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/EpFaultReplyA.lean b/proofs/Fl4ProofsA64/P3/EpFaultReplyA.lean new file mode 100644 index 000000000..32a5a758a --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/EpFaultReplyA.lean @@ -0,0 +1,650 @@ +/- +P3 (AArch64): **the handler's reply to a fault** — FL4's `faults::handle_fault_reply` against l4v's +`handle_fault_reply` applied to the replier's message (x86 `P3/IpcFaultReply`). + +* `copy_mrs_fault_reply_relA`: the receiver's registers get the sanitised message words (registers first, then the + replier's IPC buffer), and only those register words are written (`RcvRegsW`). +* `handle_fault_reply_regsA`: the program result and the spec's payload (`getMessageInfo`, `lookupIpcBuffer`, + `getMrs`, `handleFaultReply`) agree on the restart flag, and on the new state: the receiver's `tcbArch` only. + +The spec handler is FL4's `fl4HandleFaultReply` (`Fl4SpecA64/Ext/FaultReply`): a VCPU-bound receiver and the +whole `Timeout` reply are covered. [J] `hvc`: the receiver's spec VCPU link is set iff its VCPU word is (the record's +`VcpuCoreA.tcb`). Case selector (no named premise), needed because FL4 and the spec differ outside it: +* [S] UnknownSyscall: the reply is shorter than `TRAP_FULL_LENGTH`, or the fault was not sent in the long form + (`fault_w1 = 0`): FL4's trap-syscalls extension writes more registers (DESIGN.md §9). +The debug fault is unreachable (`FaultRel'` has no type 4). +-/ +import Fl4ProofsA64.P3.EpSendRecvA +import Fl4ProofsA64.P3.TcbInvokeRegsA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif + +noncomputable section + +section Spec +open Fl4SpecA64 + +/-- The receiver with the register writes `l`, in order. -/ +def faultRegsA (x : Tcb) (l : List (Register × Word)) : Tcb := l.foldl (fun y p => setReg y p.1 p.2) x + +/-- The same writes on a user context. -/ +def faultUcA (u : UserContext) (l : List (Register × Word)) : UserContext := l.foldl (fun v p => setRegUc v p.1 p.2) u + +/-- The writes of a fault reply: each register of `rl` with the sanitised message word. -/ +def faultPairsA (g : Bool) (rl : List Register) (V : List Word) : List (Register × Word) := + (rl.zip V).map fun p => (p.1, fl4SanitiseRegister g p.1 p.2) + +theorem faultRegsA_eq : ∀ (l : List (Register × Word)) (x : Tcb), + faultRegsA x l = { x with tcbArch := archTcbContextSet (faultUcA x.tcbArch.tcbContext l) x.tcbArch } + | [], x => rfl + | p :: l, x => by + show faultRegsA (setReg x p.1 p.2) l = _ + rw [faultRegsA_eq l] + rfl + +theorem faultRegsA_arch (l : List (Register × Word)) (x : Tcb) : + faultRegsA x l = { x with tcbArch := (faultRegsA x l).tcbArch } := by rw [faultRegsA_eq] + +theorem faultRegsA_take_succ (x : Tcb) (l : List (Register × Word)) (i : Nat) (hi : i < l.length) : + faultRegsA x (l.take (i + 1)) = setReg (faultRegsA x (l.take i)) l[i].1 l[i].2 := by + rw [List.take_succ_eq_append_getElem hi] + unfold faultRegsA + rw [List.foldl_append] + rfl + +theorem zipWithMx_setRegA (b : Bool) : ∀ (rl : List Register) (V : List Word) (u : UserContext), + Fl4Spec.NonDet.zipWithMx (fun r v => setRegister r <| fl4SanitiseRegister b r v) rl V u = + ({((), faultUcA u ((rl.zip V).map fun p => (p.1, fl4SanitiseRegister b p.1 p.2)))}, False) + | [], _, _ => rfl + | _ :: _, [], _ => rfl + | r :: rl, v :: V, u => by + unfold Fl4Spec.NonDet.zipWithMx + rw [nd_bind_one' (show setRegister r (fl4SanitiseRegister b r v) u = + ({((), setRegUc u r (fl4SanitiseRegister b r v))}, False) from rfl), zipWithMx_setRegA b rl V] + rfl + +/-- `as_user` of a deterministic computation on the user context. -/ +theorem asUser_writeA {σ : State Unit} {t : ObjRef} {x : Tcb} (hk : σ.kheap t = some (.tcb x)) + {f : UserMonad Unit} {u : UserContext} (hf : f (archTcbContextGet x.tcbArch) = ({((), u)}, False)) : + asUser (Z := Unit) t f σ = ({((), updK σ t (.tcb { x with tcbArch := archTcbContextSet u x.tcbArch }))}, False) := by + unfold asUser + show (Fl4Spec.NonDet.getsThe _ >>= _) σ = _ + rw [nd_bind_one (getTcb_eq hk)] + show ((pure _ : Fl4Spec.NonDet (State Unit) UserContext) >>= _) σ = _ + rw [nd_pure_bind'] + show (Fl4Spec.NonDet.selectF _ >>= _) σ = _ + rw [nd_bind_one (show Fl4Spec.NonDet.selectF (σ := State Unit) (f (archTcbContextGet x.tcbArch)) σ = + ({(((), u), σ)}, False) by + rw [hf]; simp only [Fl4Spec.NonDet.selectF] + ext ⟨a', c⟩ <;> simp)] + show ((pure _ : Fl4Spec.NonDet (State Unit) Tcb) >>= _) σ = _ + rw [nd_pure_bind'] + exact (nd_bind_one (setObject_eq (o' := KernelObject.tcb { x with tcbArch := archTcbContextSet u x.tcbArch }) + hk rfl) _).trans rfl + +/-- The register arm of `fl4HandleFaultReply`, for a receiver whose VCPU link is set iff `g`. -/ +theorem hfr_zipA {σ : State Unit} {t : ObjRef} {x : Tcb} (hk : σ.kheap t = some (.tcb x)) + {g : Bool} (hv : decide (x.tcbArch.tcbVcpu ≠ none) = g) (rl : List Register) (V : List Word) (label : Data) : + (do + let g ← archGetSanitiseRegisterInfo t + asUser (Z := Unit) t <| Fl4Spec.NonDet.zipWithMx + (fun r v => setRegister r <| fl4SanitiseRegister g r v) rl V + return decide (label = 0) : SMonad Unit Bool) σ = + ({(decide (label = 0), updK σ t (.tcb (faultRegsA x (faultPairsA g rl V))))}, False) := by + have hg : archGetSanitiseRegisterInfo (Z := Unit) t σ = ({(g, σ)}, False) := by + unfold archGetSanitiseRegisterInfo + rw [nd_bind_singleA (show archThreadGet (Z := Unit) ArchTcb.tcbVcpu t σ = ({(x.tcbArch.tcbVcpu, σ)}, False) by + unfold archThreadGet + rw [nd_bind_singleA (getTcb_oneA hk)] + rfl)] + subst hv + rfl + rw [nd_bind_singleA hg, nd_bind_one (asUser_writeA hk (zipWithMx_setRegA g rl V _)), faultRegsA_eq] + rfl + +end Spec + +theorem timeout_slice_valA : + (Array.to_slice faults.TIMEOUT_REPLY_MESSAGE).val = Fl4SpecA64.fl4TimeoutMessage.map regIdxA := by + unfold faults.TIMEOUT_REPLY_MESSAGE layout.reg.X0 layout.reg.X1 layout.reg.X2 layout.reg.X3 layout.reg.X4 + layout.reg.X5 layout.reg.X6 layout.reg.X7 layout.reg.X8 layout.reg.X9 layout.reg.X10 layout.reg.X11 + layout.reg.X12 layout.reg.X13 layout.reg.X14 layout.reg.X15 layout.reg.X16 layout.reg.X17 layout.reg.X18 + layout.reg.X19 layout.reg.X20 layout.reg.X21 layout.reg.X22 layout.reg.X23 layout.reg.X24 layout.reg.X25 + layout.reg.X26 layout.reg.X27 layout.reg.X28 layout.reg.X29 layout.reg.X30 layout.reg.FAULT_IP + layout.reg.SP_EL0 layout.reg.SPSR_EL1 + rfl + +/-- The receiver's register words of `rl`. -/ +def RcvRegsW (rcv : U64) (rl : List Fl4SpecA64.Register) (b : U64) : Prop := ∃ r ∈ rl, b = regA rcv (regIdxA r) + +/-- The receiver's VCPU word (`tcb::VCPU`). -/ +def tVcpuA (t : U64) : U64 := core.num.U64.wrapping_add t 3664#u64 + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {T E R : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {SR : S → Fl4SpecA64.State Unit → Prop} + +/-- FL4's `sanitise_for_tcb` is the spec's `fl4SanitiseRegister` at the bound-VCPU word. -/ +theorem sanitise_for_tcb_gA (s : S) (t v : U64) (r : Fl4SpecA64.Register) : + ∃ w : U64, regs.sanitise_for_tcb st s t (regIdxA r) v = ok w ∧ + w.bv = Fl4SpecA64.fl4SanitiseRegister (L.mem s (tVcpuA t) != 0#u64) r v.bv := + ⟨_, Fl4ProofsA64.P3.tcbSanitiseRegister_eqA s t r v _ + (fun _ => ⟨_, eq_of_spec_fd (vcpu.bound.spec (L := L) s t), rfl⟩), rfl⟩ + +/-- The loop invariant of `copy_mrs_fault_reply`: the first `i` writes of `ps` done from `s0`. -/ +def FrInvA (T E R : Set U64) (Bf : Set (U64 × Fl4SpecA64.Word)) (SR : S → Fl4SpecA64.State Unit → Prop) (s0 : S) + (σ : Fl4SpecA64.State Unit) (rcv : U64) (xr : Fl4SpecA64.Tcb) (rl : List Fl4SpecA64.Register) + (ps : List (Fl4SpecA64.Register × Fl4SpecA64.Word)) (i : Nat) (s : S) : Prop := + EpStateRel T E R Bf (L.mem s) (updK σ (specPtr rcv) (.tcb (faultRegsA xr (ps.take i)))) ∧ + SR s (updK σ (specPtr rcv) (.tcb (faultRegsA xr (ps.take i)))) ∧ + WritesIn st s0 s (RcvRegsW rcv rl) ∧ WritesIn st s0 s (EWrite T E R Bf) + +theorem FrInvA.init {s : S} {σ : Fl4SpecA64.State Unit} (hR : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) + {rcv : U64} {xr : Fl4SpecA64.Tcb} (hkr : σ.kheap (specPtr rcv) = some (.tcb xr)) + (rl : List Fl4SpecA64.Register) (ps : List (Fl4SpecA64.Register × Fl4SpecA64.Word)) : + FrInvA (L := L) T E R Bf SR s σ rcv xr rl ps 0 s := by + have h : updK σ (specPtr rcv) (.tcb (faultRegsA xr (ps.take 0))) = σ := updK_selfA hkr + unfold FrInvA + rw [h] + exact ⟨hR, hSR, WritesIn.refl _ _, WritesIn.refl _ _⟩ + +theorem FrInvA.step (hO : SchedOpsEA st T E R Bf SR) (hP : EPlaced T E R Bf) {s0 s : S} + {σ : Fl4SpecA64.State Unit} {rcv : U64} (hr : rcv ∈ T) {xr : Fl4SpecA64.Tcb} {rl : List Fl4SpecA64.Register} + {ps : List (Fl4SpecA64.Register × Fl4SpecA64.Word)} {i : Nat} (h : FrInvA (L := L) T E R Bf SR s0 σ rcv xr rl ps i s) + (hi : i < ps.length) (hm : ps[i].1 ∈ rl) {w : U64} (hw : w.bv = ps[i].2) : + FrInvA (L := L) T E R Bf SR s0 σ rcv xr rl ps (i + 1) (L.wr s (regA rcv (regIdxA ps[i].1)) w) := by + obtain ⟨hR, hSR, hW, hE⟩ := h + have hta := hP.tcb rcv hr + have hk : (updK σ (specPtr rcv) (.tcb (faultRegsA xr (ps.take i)))).kheap (specPtr rcv) = + some (.tcb (faultRegsA xr (ps.take i))) := by simp + obtain ⟨hR1, hW1, hS1⟩ := write_reg_rel hP s _ hR hr hk ps[i].1 w + have hupd : updK (updK σ (specPtr rcv) (.tcb (faultRegsA xr (ps.take i)))) (specPtr rcv) + (.tcb (setReg (faultRegsA xr (ps.take i)) ps[i].1 w.bv)) = + updK σ (specPtr rcv) (.tcb (faultRegsA xr (ps.take (i + 1)))) := by + rw [faultRegsA_take_succ xr ps i hi, hw] + unfold updK + simp only [Function.update_idem] + have hN : Fl4ProofsA64.P3.Endp.NoBlockE (updK σ (specPtr rcv) (.tcb (faultRegsA xr (ps.take i)))) + (updK (updK σ (specPtr rcv) (.tcb (faultRegsA xr (ps.take i)))) (specPtr rcv) + (.tcb (setReg (faultRegsA xr (ps.take i)) ps[i].1 w.bv))) := + NoBlockE.tcbK hk _ rfl rfl + have hS' := hO.stable _ _ _ _ hSR hW1 hS1 hN + rw [hupd] at hR1 hS' + have hn : WritesIn st s (L.wr s (regA rcv (regIdxA ps[i].1)) w) (RcvRegsW rcv rl) := + (writesIn_wr (a_reg hta ps[i].1).1).mono fun c hc => ⟨_, hm, hc⟩ + exact ⟨hR1, hS', (hW.trans hn).mono fun _ h => h.elim id id, (hE.trans hW1).mono fun _ h => h.elim id id⟩ + +/-- The receiver's VCPU word and fault words are not register words. -/ +theorem rcvRegs_keepA (hP : EPlaced T E R Bf) {rcv : U64} (hr : rcv ∈ T) {rl : List Fl4SpecA64.Register} {s s' : S} + (hW : WritesIn st s s' (RcvRegsW rcv rl)) {K : U64} (hK : K.val < 4096) (hK8 : K.val % 8 = 0) + (hoff : K.val < 256 ∨ 552 ≤ K.val) : + L.mem s' (core.num.U64.wrapping_add rcv K) = L.mem s (core.num.U64.wrapping_add rcv K) := by + have hta := hP.tcb rcv hr + refine hW.2.2 _ (field_aligned hta hK hK8) ?_ + rintro ⟨r, -, e⟩ + have h1 := congrArg UScalar.val e + have hfit := hta.fits + rw [wadd_valA rcv K.val (by omega) K rfl, regA_val hta (regIdxA_lt r)] at h1 + have := regIdxA_lt r + omega + +/-- The data the two loops need: the replier's message `V` and the pair list. -/ +structure FrDataA (rl : List Fl4SpecA64.Register) (V : List Fl4SpecA64.Word) (n : Nat) : Prop where + nrl : n ≤ rl.length + small : rl.length < 64 + +theorem faultPairsA_get {g : Bool} {rl : List Fl4SpecA64.Register} {V : List Fl4SpecA64.Word} {i : Nat} (h1 : i < rl.length) + (h2 : i < V.length) : ∃ h : i < (faultPairsA g rl V).length, + (faultPairsA g rl V)[i] = (rl[i], Fl4SpecA64.fl4SanitiseRegister g rl[i] V[i]) := by + have h : i < (faultPairsA g rl V).length := by simp [faultPairsA]; omega + exact ⟨h, by simp [faultPairsA]⟩ + +/-- One sanitised register write of the loops, from the message word `v`. -/ +theorem FrInvA.write (hO : SchedOpsEA st T E R Bf SR) (hP : EPlaced T E R Bf) {s0 s : S} + {σ : Fl4SpecA64.State Unit} {rcv : U64} (hr : rcv ∈ T) {xr : Fl4SpecA64.Tcb} {rl : List Fl4SpecA64.Register} + {V : List Fl4SpecA64.Word} {i : Nat} + {g : Bool} (h : FrInvA (L := L) T E R Bf SR s0 σ rcv xr rl (faultPairsA g rl V) i s) (hi : i < rl.length) + (hiV : i < V.length) (hv0 : (L.mem s0 (tVcpuA rcv) != 0#u64) = g) {v : U64} (hv : v.bv = V[i]) : + ∃ w : U64, regs.sanitise_for_tcb st s rcv (regIdxA rl[i]) v = ok w ∧ + FrInvA (L := L) T E R Bf SR s0 σ rcv xr rl (faultPairsA g rl V) (i + 1) (L.wr s (regA rcv (regIdxA rl[i])) w) := by + have h0 : (L.mem s (tVcpuA rcv) != 0#u64) = g := by + rw [show L.mem s (tVcpuA rcv) = L.mem s0 (tVcpuA rcv) from + rcvRegs_keepA hP hr h.2.2.1 (K := 3664#u64) (by simp) (by simp) (by simp)] + exact hv0 + obtain ⟨w, hw, hb⟩ := sanitise_for_tcb_gA (st := st) (L := L) s rcv v rl[i] + rw [h0] at hb + obtain ⟨hlt, hp⟩ := faultPairsA_get (g := g) hi hiV + have hs := FrInvA.step hO hP hr h hlt (by rw [hp]; exact List.getElem_mem _) (w := w) + (by rw [hp, hb, hv]) + rw [hp] at hs + exact ⟨w, hw, hs⟩ + +/-- **`copy_mrs_fault_reply`** (x86 `cmfr_top`): the first `n = min len |rl|` registers of `rl` get the sanitised +words of the replier's message `V` (l4v `get_mrs`); without a replier buffer, the first `min n 4`. -/ +theorem copy_mrs_fault_reply_relA (hO : SchedOpsEA st T E R Bf SR) (hP : EPlaced T E R Bf) {th : TypedHeapA} + (s : S) (σ : Fl4SpecA64.State Unit) (hR : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) + (hSt : StateRelA th (L.mem s) σ) (hC : BufCover st T Bf s σ) (hNative : BufNativeA T (L.mem s)) + {rcv snd : U64} (hr : rcv ∈ T) (hs : snd ∈ T) (hne : snd ≠ rcv) (hts : th snd = some .tcb) + {xs xr : Fl4SpecA64.Tcb} (hks : σ.kheap (specPtr snd) = some (.tcb xs)) + (hkr : σ.kheap (specPtr rcv) = some (.tcb xr)) {g : Bool} (hv0 : (L.mem s (tVcpuA rcv) != 0#u64) = g) + {sl : Slice U64} {rl : List Fl4SpecA64.Register} (hsl : sl.val = rl.map regIdxA) (hn : rl.length < 64) + (len : U64) (hlen : len.val ≤ 120) : + faults.copy_mrs_fault_reply st s snd rcv sl len ⦃ s' => + EpStateRel T E R Bf (L.mem s') (updK σ (specPtr rcv) (.tcb (faultRegsA xr (faultPairsA g rl + (specMrsA xs.tcbArch.tcbContext.userRegs (ipcBufOfA false xs) σ.machineState.underlyingMemory len.val))))) ∧ + SR s' (updK σ (specPtr rcv) (.tcb (faultRegsA xr (faultPairsA g rl + (specMrsA xs.tcbArch.tcbContext.userRegs (ipcBufOfA false xs) σ.machineState.underlyingMemory len.val))))) ∧ + WritesIn st s s' (RcvRegsW rcv rl) ∧ WritesIn st s s' (EWrite T E R Bf) ⦄ := by + set V := specMrsA xs.tcbArch.tcbContext.userRegs (ipcBufOfA false xs) σ.machineState.underlyingMemory len.val with hV + set ps := faultPairsA g rl V with hpsdef + have hslen : (Slice.len sl).val = rl.length := by simp [Slice.len, hsl] + have hcast : (UScalar.cast .U64 (Slice.len sl)).val = rl.length := by + rw [cast_usA _ (by omega), hslen] + have hN : msg.N_MSG_REGISTERS.val = 4 := by unfold msg.N_MSG_REGISTERS; rfl + set m := min len.val rl.length with hm + have hps : ps.length = min rl.length V.length := by simp [ps, faultPairsA] + have hVlen : V.length = min len.val (match ipcBufOfA false xs with | none => 4 | some _ => 120) := + specMrsA_length _ _ _ _ + have hsndx : ∀ (i : Nat) (s' : S), FrInvA (L := L) T E R Bf SR s σ rcv xr rl ps i s' → + TcbRelE R (L.mem s') snd xs := fun i s' hI => by + refine hI.1.at_tcb hs ?_ + have : specPtr snd ≠ specPtr rcv := fun h => hne (specPtr_inj h) + simpa [this] using hks + -- the end: `i` writes done and `ps` has no more than `i` + have fin : ∀ (i : Nat) (s' : S), FrInvA (L := L) T E R Bf SR s σ rcv xr rl ps i s' → ps.length ≤ i → + EpStateRel T E R Bf (L.mem s') (updK σ (specPtr rcv) (.tcb (faultRegsA xr ps))) ∧ + SR s' (updK σ (specPtr rcv) (.tcb (faultRegsA xr ps))) ∧ + WritesIn st s s' (RcvRegsW rcv rl) ∧ WritesIn st s s' (EWrite T E R Bf) := fun i s' hI hle => by + unfold FrInvA at hI + rwa [List.take_of_length_le hle] at hI + unfold faults.copy_mrs_fault_reply + simp only [lift, bind_ok] + have hnn : ∃ nn : Usize, (if len < UScalar.cast .U64 (Slice.len sl) then ok (UScalar.cast .Usize len) + else ok (Slice.len sl) : Result Usize) = ok nn ∧ nn.val = m := by + by_cases h : len < UScalar.cast .U64 (Slice.len sl) + · have h' : len.val < rl.length := by rw [← hcast]; exact (UScalar.lt_equiv _ _).1 h + refine ⟨_, if_pos h, ?_⟩ + rw [UScalar.cast_val_eq, Nat.mod_eq_of_lt (by + have : (2 : Nat) ^ 32 ≤ 2 ^ UScalarTy.Usize.numBits := Nat.pow_le_pow_right (by norm_num) (by + rcases System.Platform.numBits_eq with h | h <;> simp [h]) + omega)] + omega + · have h' : ¬ len.val < rl.length := fun e => h ((UScalar.lt_equiv _ _).2 (by rw [hcast]; exact e)) + exact ⟨_, if_neg h, by rw [hslen]; omega⟩ + obtain ⟨nn, hnn1, hnn2⟩ := hnn + rw [hnn1, bind_ok] + have hmV : ∀ i, i < m → i < 4 → i < V.length := fun i h1 h4 => by + rw [hVlen]; cases ipcBufOfA false xs <;> simp only <;> omega + have L0 : faults.copy_mrs_fault_reply_loop0 st s snd rcv sl nn 0#usize ⦃ r => + r.2.val = min m 4 ∧ FrInvA (L := L) T E R Bf SR s σ rcv xr rl ps r.2.val r.1 ⦄ := by + unfold faults.copy_mrs_fault_reply_loop0 + apply loop.spec_decr_nat (measure := fun x => 4 - x.2.val) + (inv := fun x => x.2.val ≤ min m 4 ∧ FrInvA (L := L) T E R Bf SR s σ rcv xr rl ps x.2.val x.1) + · rintro ⟨s', i⟩ ⟨hi4, hI⟩ + unfold faults.copy_mrs_fault_reply_loop0.body + simp only at hi4 hI ⊢ + by_cases h1 : i < nn + · have him : i.val < m := by have := us_lt_iffA.1 h1; omega + have hil : i.val < rl.length := by omega + have h1' : i < Slice.len sl := us_lt_iffA.2 (by omega) + simp only [h1, h1', ↓reduceIte, lift, bind_ok] + have hi2v : (UScalar.cast .U64 i).val = i.val := cast_usA i (by omega) + by_cases h2 : UScalar.cast .U64 i < msg.N_MSG_REGISTERS + · have h4 : i.val < 4 := by have := (gt_iff_val).1 h2; omega + simp only [h2, ↓reduceIte] + step as ⟨r, hr'⟩ + have hr2 : r = regIdxA (rl[i.val]'hil) := by rw [hr']; simp [hsl] + rw [msg_register_regIdxA (show (UScalar.cast .U64 i).val < 4 by omega), bind_ok, tcb_reg_eqA, bind_ok] + have hiV := hmV i.val him h4 + have hvb : (L.mem s' (regWA snd (regIdxA (Fl4SpecA64.msgRegisters.getD (UScalar.cast .U64 i).val .x5)))).bv = + V[i.val] := by + rw [← regA_eq_regWA, (hsndx _ _ hI).regs, hi2v] + have := (specMrsA_lo xs.tcbArch.tcbContext.userRegs (ipcBufOfA false xs) + σ.machineState.underlyingMemory len.val i.val h4 (by omega)).2 + rw [← hV, getElem!_pos V _ hiV] at this + exact this.symm + obtain ⟨w, hw, hI1⟩ := FrInvA.write hO hP hr hI hil hiV hv0 hvb + rw [hr2, hw, bind_ok, set_reg_eqA, bind_ok] + step as ⟨i6, hi6⟩ + have hi6v : i6.val = i.val + 1 := by simpa using hi6 + refine ⟨by omega, ?_, by omega⟩ + rw [hi6v]; exact hI1 + · simp only [h2, ↓reduceIte, WP.spec_ok] + have : ¬ i.val < 4 := fun h => h2 ((gt_iff_val).2 (by rw [hN]; omega)) + exact ⟨by omega, hI⟩ + · simp only [h1, ↓reduceIte, WP.spec_ok] + have : ¬ i.val < m := fun h => h1 (us_lt_iffA.2 (by omega)) + exact ⟨by omega, hI⟩ + · exact ⟨by simp, FrInvA.init hR hSR hkr rl ps⟩ + refine WP.spec_bind L0 ?_ + rintro ⟨s1, i2⟩ ⟨hi2, hI⟩ + have hi2 : i2.val = min m 4 := hi2 + have hI : FrInvA (L := L) T E R Bf SR s σ rcv xr rl ps i2.val s1 := hI + show (if i2 < nn then (do + let buf ← msg.lookup_ipc_buffer st s1 false snd + if (buf != 0#u64) = true then faults.copy_mrs_fault_reply_loop1 st s1 rcv sl nn i2 buf else ok s1 : Result S) + else ok s1) ⦃ _ ⦄ + by_cases h2 : i2 < nn + · have h2' : i2.val < m := by have := us_lt_iffA.1 h2; omega + have hi24 : i2.val = 4 := by omega + rw [if_pos h2] + rw [lookup_ipc_buffer_sameA (ibSame_ewA hP hI.2.2.2 hs)] + refine WP.spec_bind (lookup_buf_full s σ hSt hts hs hks hC hNative false) ?_ + rintro buf ⟨hz, hmem⟩ + rcases buf_caseA hz hmem with ⟨hnone, rfl⟩ | ⟨bp, hbp, hsb, rfl, hb0⟩ + · simp only [bne_self_eq_false, Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + refine fin i2.val s1 hI ?_ + rw [hps, hVlen, hnone]; simp only; omega + · have hb' : (bp.1 != 0#u64) = true := by simpa using hb0 + simp only [hb', ↓reduceIte] + have hVl : V.length = min len.val 120 := by rw [hVlen, hsb] + unfold faults.copy_mrs_fault_reply_loop1 + apply loop.spec_decr_nat (measure := fun x => m - x.2.val) + (inv := fun x => 4 ≤ x.2.val ∧ x.2.val ≤ m ∧ FrInvA (L := L) T E R Bf SR s σ rcv xr rl ps x.2.val x.1) + · rintro ⟨s', i⟩ ⟨h4, hle, hI'⟩ + unfold faults.copy_mrs_fault_reply_loop1.body + simp only at h4 hle hI' ⊢ + by_cases h1 : i < nn + · have him : i.val < m := by have := us_lt_iffA.1 h1; omega + have hil : i.val < rl.length := by omega + have h1' : i < Slice.len sl := us_lt_iffA.2 (by omega) + simp only [h1, h1', ↓reduceIte] + step as ⟨r, hr'⟩ + have hr2 : r = regIdxA (rl[i.val]'hil) := by rw [hr']; simp [hsl] + simp only [lift, bind_ok] + have hi2v : (UScalar.cast .U64 i).val = i.val := cast_usA i (by omega) + have h3v : (core.num.U64.wrapping_add (UScalar.cast .U64 i) 1#u64).val = i.val + 1 := by + rw [wadd_valA _ 1 (by omega) _ rfl, hi2v] + rw [buf_get_eqA, bind_ok] + have hiV : i.val < V.length := by omega + have hvb : (L.mem s' (bufWA bp.1 (core.num.U64.wrapping_add (UScalar.cast .U64 i) 1#u64))).bv = + V[i.val] := by + rw [← bufW_eq_bufWA, (hI'.1.buf bp hbp _ (by omega)).1, h3v] + show loadWordAt σ.machineState.underlyingMemory (bufAddr bp.2 (i.val + 1)) = _ + have := (specMrsA_hi xs.tcbArch.tcbContext.userRegs bp.2 σ.machineState.underlyingMemory len.val i.val h4 + (by omega) (by omega)).2 + rw [← hsb, ← hV, getElem!_pos V _ hiV] at this + exact this.symm + obtain ⟨w, hw, hI1⟩ := FrInvA.write hO hP hr hI' hil hiV hv0 hvb + rw [hr2, hw, bind_ok, set_reg_eqA, bind_ok] + step as ⟨i5, hi5⟩ + have hi5v : i5.val = i.val + 1 := by simpa using hi5 + refine ⟨by omega, by omega, ?_, by omega⟩ + rw [hi5v]; exact hI1 + · simp only [h1, ↓reduceIte, WP.spec_ok] + have : ¬ i.val < m := fun h => h1 (us_lt_iffA.2 (by omega)) + refine fin i.val s' hI' ?_ + rw [hps]; omega + · exact ⟨by omega, by omega, hI⟩ + · rw [if_neg h2, WP.spec_ok] + have : ¬ i2.val < m := fun h => h2 (us_lt_iffA.2 (by omega)) + refine fin i2.val s1 hI ?_ + rw [hps]; omega + +theorem trap_full_lengthA : faults.TRAP_FULL_LENGTH = ok 37#u64 := by + unfold faults.TRAP_FULL_LENGTH faults.TRAP_EXTRA_FIRST + simp only [lift, bind_ok] + have h : (UScalar.cast .U64 (Slice.len (Array.to_slice faults.TRAP_EXTRA_MESSAGE))).val = 24 := by + rw [cast_usA _ (by simp [faults.TRAP_EXTRA_MESSAGE])] + simp [faults.TRAP_EXTRA_MESSAGE] + obtain ⟨z, hz, hzv⟩ := spec_existsA (U64.add_spec (x := 13#u64) + (y := UScalar.cast .U64 (Slice.len (Array.to_slice faults.TRAP_EXTRA_MESSAGE))) (by + rw [h]; simp [U64.max_def, U64.numBits])) + rw [hz] + congr 1 + apply UScalar.eq_of_val_eq + rw [hzv, h]; rfl + +/-- The scope selector of a fault reply (module docstring): FL4's long-form unknown syscall. `len` is the reply's +message length. -/ +structure FaultReplyOKA (s : S) (rcv : U64) (len : Nat) (f : Fl4SpecA64.Fault) : Prop where + trap : (∃ n, f = .unknownSyscallException n) → len < 37 ∨ L.mem s (tFw1A rcv) = 0#u64 + +/-- The payload of a fault reply: the replier's message, then `fl4HandleFaultReply`. -/ +def faultPayloadA (sender receiver : Fl4SpecA64.ObjRef) (fault : Fl4SpecA64.Fault) : + Fl4SpecA64.SMonad Unit Bool := do + let mi ← Fl4SpecA64.getMessageInfo sender + let buf ← Fl4SpecA64.lookupIpcBuffer false sender + let mrs ← Fl4SpecA64.getMrs sender buf mi + Fl4SpecA64.fl4HandleFaultReply fault receiver mi.miLabel mrs + +/-- **`handle_fault_reply` refines the FL4 payload**: same restart flag; the spec changes the receiver's `tcbArch` +only; FL4 writes only registers of the receiver, and `a` is unchanged. -/ +theorem handle_fault_reply_regsA {A : Type} {ar : machine.Arch A} (hO : SchedOpsEA st T E R Bf SR) + (hP : EPlaced T E R Bf) {th : TypedHeapA} (s : S) (a : A) (σ : Fl4SpecA64.State Unit) + (hR : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) (hSt : StateRelA th (L.mem s) σ) + (hC : BufCover st T Bf s σ) (hNative : BufNativeA T (L.mem s)) + {rcv snd : U64} (hr : rcv ∈ T) (hs : snd ∈ T) (hne : snd ≠ rcv) (hts : th snd = some .tcb) + {xs xr : Fl4SpecA64.Tcb} (hks : σ.kheap (specPtr snd) = some (.tcb xs)) + (hkr : σ.kheap (specPtr rcv) = some (.tcb xr)) {f : Fl4SpecA64.Fault} (hf : xr.tcbFault = some f) + (hok : FaultReplyOKA (L := L) s rcv + (Fl4SpecA64.unat (Fl4SpecA64.dataToMessageInfo + (xs.tcbArch.tcbContext.userRegs Fl4SpecA64.msg_info_register)).miLength) f) + (hvc : xr.tcbArch.tcbVcpu ≠ none ↔ L.mem s (tVcpuA rcv) ≠ 0#u64) : + faults.handle_fault_reply st ar s a rcv snd ⦃ res => res.2.2 = a ∧ ∃ x', + (∃ uc, x' = { xr with tcbArch := Fl4SpecA64.archTcbContextSet uc xr.tcbArch }) ∧ + faultPayloadA (specPtr snd) (specPtr rcv) f σ = ({(res.1, updK σ (specPtr rcv) (.tcb x'))}, False) ∧ + EpStateRel T E R Bf (L.mem res.2.1) (updK σ (specPtr rcv) (.tcb x')) ∧ + SR res.2.1 (updK σ (specPtr rcv) (.tcb x')) ∧ + WritesIn st s res.2.1 (fun b => ∃ r : Fl4SpecA64.Register, b = regA rcv (regIdxA r)) ∧ + WritesIn st s res.2.1 (EWrite T E R Bf) ⦄ := by + have hxs := hR.at_tcb hs hks + have hxr := hR.at_tcb hr hkr + set w := xs.tcbArch.tcbContext.userRegs Fl4SpecA64.msg_info_register with hwdef + set V := specMrsA xs.tcbArch.tcbContext.userRegs (ipcBufOfA false xs) σ.machineState.underlyingMemory + (Fl4SpecA64.unat (Fl4SpecA64.dataToMessageInfo w).miLength) with hVdef + have hwmi : (L.mem s (regWA snd layout.reg.MSG_INFO)).bv = w := by + have h : layout.reg.MSG_INFO = regIdxA Fl4SpecA64.msg_info_register := by + unfold layout.reg.MSG_INFO layout.reg.X1; rfl + rw [← regA_eq_regWA, h]; exact hxs.regs _ + have hpay : ∀ (res : Bool) (σ' : Fl4SpecA64.State Unit), + Fl4SpecA64.fl4HandleFaultReply f (specPtr rcv) (Fl4SpecA64.dataToMessageInfo w).miLabel V σ = + ({(res, σ')}, False) → + faultPayloadA (specPtr snd) (specPtr rcv) f σ = ({(res, σ')}, False) := by + intro res σ' h + have h1 : Fl4SpecA64.getMessageInfo (Z := Unit) (specPtr snd) σ = + ({(Fl4SpecA64.dataToMessageInfo w, σ)}, False) := by + unfold Fl4SpecA64.getMessageInfo + rw [nd_bind_singleA (asUser_getRegister_eqA hks _)] + rfl + unfold faultPayloadA + rw [nd_bind_singleA h1, nd_bind_singleA (show Fl4SpecA64.lookupIpcBuffer (Z := Unit) false (specPtr snd) σ = + ({(ipcBufOfA false xs, σ)}, False) from lookupIpcBuffer_eqA hks false), + nd_bind_singleA (getMrs_eqA hks (ipcBufOfA false xs) (fun sb hsb k hk => by + obtain ⟨w', hz, hmem⟩ := spec_existsA (lookup_buf_full s σ hSt hts hs hks hC hNative false) + obtain ⟨-, hmem⟩ := hmem + have hbp := hmem sb hsb + exact bufAddr_align (hR.buf (w', sb) hbp 0#u64 (by decide)).2 k hk) _)] + exact h + have idle : (res : Bool) → Fl4SpecA64.fl4HandleFaultReply f (specPtr rcv) + (Fl4SpecA64.dataToMessageInfo w).miLabel V σ = ({(res, σ)}, False) → + ∃ x', (∃ uc, x' = { xr with tcbArch := Fl4SpecA64.archTcbContextSet uc xr.tcbArch }) ∧ + faultPayloadA (specPtr snd) (specPtr rcv) f σ = ({(res, updK σ (specPtr rcv) (.tcb x'))}, False) ∧ + EpStateRel T E R Bf (L.mem s) (updK σ (specPtr rcv) (.tcb x')) ∧ + SR s (updK σ (specPtr rcv) (.tcb x')) ∧ + WritesIn st s s (fun b => ∃ r : Fl4SpecA64.Register, b = regA rcv (regIdxA r)) ∧ + WritesIn st s s (EWrite T E R Bf) := fun res h => by + refine ⟨xr, ⟨xr.tcbArch.tcbContext, rfl⟩, ?_⟩ + rw [updK_selfA hkr] + exact ⟨hpay res σ h, hR, hSR, WritesIn.refl _ _, WritesIn.refl _ _⟩ + have hfr := hxr.faultR + unfold FaultRel at hfr + rw [hf] at hfr + unfold faults.handle_fault_reply + refine WP.spec_bind (msg_info_specA (L := L) s snd) ?_ + rintro tag ⟨hlab, hlen, -, hle⟩ + rw [hwmi] at hlab hlen + have hlab0 : decide (tag.label = 0#u64) = decide ((Fl4SpecA64.dataToMessageInfo w).miLabel = 0) := by + have hml : (Fl4SpecA64.dataToMessageInfo w).miLabel = tag.label.bv := by + show (w >>> 12) &&& Fl4SpecA64.mask Fl4SpecA64.msgLabelBits = _ + rw [label_maskA, ← hlab] + refine decide_eq_decide.2 ⟨fun h => ?_, fun h => ?_⟩ + · rw [hml, h]; rfl + · apply UScalar.eq_of_val_eq + show tag.label.bv.toNat = 0 + rw [← hml, h]; rfl + have hlv : tag.length.val = Fl4SpecA64.unat (Fl4SpecA64.dataToMessageInfo w).miLength := by + show tag.length.bv.toNat = _; rw [hlen] + step with obj.Tcb.fault_type.spec as ⟨ty, hty⟩ + -- the register arms + set g := (L.mem s (tVcpuA rcv) != 0#u64) with hg + have hgx : decide (xr.tcbArch.tcbVcpu ≠ none) = g := by + rw [hg] + by_cases h : L.mem s (tVcpuA rcv) = 0#u64 + · have hn : ¬ xr.tcbArch.tcbVcpu ≠ none := fun e => hvc.1 e h + simp [h, not_not.1 hn] + · simp [h, hvc.2 h] + have copy : ∀ (sl : Slice U64) (rl : List Fl4SpecA64.Register), sl.val = rl.map regIdxA → rl.length < 64 → + (Fl4SpecA64.fl4HandleFaultReply f (specPtr rcv) (Fl4SpecA64.dataToMessageInfo w).miLabel V σ = + ({(decide ((Fl4SpecA64.dataToMessageInfo w).miLabel = 0), + updK σ (specPtr rcv) (.tcb (faultRegsA xr (faultPairsA g rl V))))}, False)) → + faults.copy_mrs_fault_reply st s snd rcv sl tag.length ⦃ s2 => + ∃ x', (∃ uc, x' = { xr with tcbArch := Fl4SpecA64.archTcbContextSet uc xr.tcbArch }) ∧ + faultPayloadA (specPtr snd) (specPtr rcv) f σ = + ({(decide (tag.label = 0#u64), updK σ (specPtr rcv) (.tcb x'))}, False) ∧ + EpStateRel T E R Bf (L.mem s2) (updK σ (specPtr rcv) (.tcb x')) ∧ + SR s2 (updK σ (specPtr rcv) (.tcb x')) ∧ + WritesIn st s s2 (fun b => ∃ r : Fl4SpecA64.Register, b = regA rcv (regIdxA r)) ∧ + WritesIn st s s2 (EWrite T E R Bf) ⦄ := by + intro sl rl hsl hl hspec + refine WP.spec_mono (copy_mrs_fault_reply_relA (L := L) hO hP s σ hR hSR hSt hC hNative hr hs hne hts hks hkr + hg.symm hsl hl tag.length hle) ?_ + intro s2 h + rw [hlv, ← hVdef] at h + obtain ⟨h1, h2, h3, h4⟩ := h + refine ⟨_, ⟨_, faultRegsA_eq _ _⟩, by rw [hlab0]; exact hpay _ _ hspec, h1, h2, + h3.mono fun b hb => ?_, h4⟩ + obtain ⟨r, -, e⟩ := hb + exact ⟨r, e⟩ + have hspecR : ∀ rl : List Fl4SpecA64.Register, + (do + let g' ← Fl4SpecA64.archGetSanitiseRegisterInfo (specPtr rcv) + Fl4SpecA64.asUser (Z := Unit) (specPtr rcv) <| Fl4Spec.NonDet.zipWithMx + (fun r v => Fl4SpecA64.setRegister r <| Fl4SpecA64.fl4SanitiseRegister g' r v) rl V + return decide ((Fl4SpecA64.dataToMessageInfo w).miLabel = 0) : Fl4SpecA64.SMonad Unit Bool) σ = + ({(decide ((Fl4SpecA64.dataToMessageInfo w).miLabel = 0), + updK σ (specPtr rcv) (.tcb (faultRegsA xr (faultPairsA g rl V))))}, False) := + fun rl => hfr_zipA hkr hgx rl V _ + cases f with + | capFault c rp lf => + obtain ⟨h1, -⟩ := hfr + rw [hty, h1, if_pos (by unfold fl4_abi.consts.fault.CapFault; rfl), WP.spec_ok] + exact ⟨rfl, idle true rfl⟩ + | unknownSyscallException n => + obtain ⟨h1, -⟩ := hfr + rw [hty, h1, if_neg (by unfold fl4_abi.consts.fault.CapFault; decide), + if_pos (by unfold fl4_abi.consts.fault.UnknownSyscall; rfl)] + simp only [lift, bind_ok] + refine WP.spec_bind (copy _ _ syscall_slice_valA (by decide) (hspecR _)) ?_ + rintro s2 ⟨x', hx', hp, h1', h2', h3', h4'⟩ + have htrap : faults.trap_extra_reply st s2 snd rcv tag.length = ok s2 := by + unfold faults.trap_extra_reply + rw [trap_full_lengthA, bind_ok] + by_cases hl : tag.length < 37#u64 + · rw [if_pos hl] + · rw [if_neg hl] + have h0 : L.mem s (tFw1A rcv) = 0#u64 := by + rcases hok.trap ⟨n, rfl⟩ with h | h + · exact absurd ((UScalar.lt_equiv _ _).2 (by rw [hlv]; exact h)) hl + · exact h + have h0' : L.mem s2 (tFw1A rcv) = 0#u64 := by + have hta := hP.tcb rcv hr + rw [← h0] + refine h3'.2.2 _ (a_fw1 hta).1 ?_ + rintro ⟨r, e⟩ + have h5 := congrArg UScalar.val e + rw [val_fw1 hta, regA_val hta (regIdxA_lt r)] at h5 + have := regIdxA_lt r + omega + rw [eq_of_spec_fd (obj.Tcb.fault_w1.spec (st := st) s2 rcv), bind_ok, if_pos (show _ = 0#u64 from h0')] + rw [htrap, bind_ok, WP.spec_ok] + exact ⟨rfl, x', hx', hp, h1', h2', h3', h4'⟩ + | userException e c => + obtain ⟨h1, -⟩ := hfr + rw [hty, h1, if_neg (by unfold fl4_abi.consts.fault.CapFault; decide), + if_neg (by unfold fl4_abi.consts.fault.UnknownSyscall; decide), + if_pos (by unfold fl4_abi.consts.fault.UserException; rfl)] + simp only [lift, bind_ok] + refine WP.spec_bind (copy _ _ exc_slice_valA (by decide) (hspecR _)) ?_ + intro s2 h + rw [WP.spec_ok] + exact ⟨rfl, h⟩ + | timeout b => + obtain ⟨h1, -⟩ := hfr + rw [hty, h1, if_neg (by unfold fl4_abi.consts.fault.CapFault; decide), + if_neg (by unfold fl4_abi.consts.fault.UnknownSyscall; decide), + if_neg (by unfold fl4_abi.consts.fault.UserException; decide), + if_pos (by unfold fl4_abi.consts.fault.Timeout; rfl)] + simp only [lift, bind_ok] + refine WP.spec_bind (copy _ _ timeout_slice_valA (by decide) (hspecR _)) ?_ + intro s2 h + rw [WP.spec_ok] + exact ⟨rfl, h⟩ + | archFault af => + have hdo : ∀ k : U64, ty = k → k ≠ fl4_abi.consts.fault.CapFault → k ≠ fl4_abi.consts.fault.UnknownSyscall → + k ≠ fl4_abi.consts.fault.UserException → k ≠ fl4_abi.consts.fault.Timeout → + k ≠ fl4_abi.consts.fault.DebugException → + (if ty = fl4_abi.consts.fault.CapFault then ok (true, s, a) + else if ty = fl4_abi.consts.fault.UnknownSyscall then + (do + let s1 ← lift (Array.to_slice faults.SYSCALL_MESSAGE) + let s2 ← faults.copy_mrs_fault_reply st s snd rcv s1 tag.length + let s3 ← faults.trap_extra_reply st s2 snd rcv tag.length + ok (decide (tag.label = 0#u64), s3, a)) + else if ty = fl4_abi.consts.fault.UserException then + (do + let s1 ← lift (Array.to_slice faults.EXCEPTION_MESSAGE) + let s2 ← faults.copy_mrs_fault_reply st s snd rcv s1 tag.length + ok (decide (tag.label = 0#u64), s2, a)) + else if ty = fl4_abi.consts.fault.Timeout then + (do + let s1 ← lift (Array.to_slice faults.TIMEOUT_REPLY_MESSAGE) + let s2 ← faults.copy_mrs_fault_reply st s snd rcv s1 tag.length + ok (decide (tag.label = 0#u64), s2, a)) + else if ty = fl4_abi.consts.fault.DebugException then + (do + let i ← obj.Tcb.fault_w0 st rcv s + if i != debug.SINGLE_STEP + then ok (decide (tag.label = 0#u64), s, a) + else + let n_instrs ← + if tag.length < 1#u64 + then ok 1#u64 + else + do + let i1 ← msg.msg_register 0#u64 + obj.Tcb.reg st snd s i1 + let (_, s1, a1) ← debug.configure_single_stepping st ar s a rcv 0#u64 n_instrs true + ok (decide (tag.label = 0#u64), s1, a1)) + else ok (true, s, a)) = ok (true, s, a) := by + intro k hk h1 h2 h3 h4 h5 + subst hk + rw [if_neg h1, if_neg h2, if_neg h3, if_neg h4, if_neg h5] + have hk : ty = L.mem s (tFaultA rcv) := hty + have h6 : L.mem s (tFaultA rcv) = 6#u64 ∨ L.mem s (tFaultA rcv) = 7#u64 ∨ L.mem s (tFaultA rcv) = 8#u64 ∨ + L.mem s (tFaultA rcv) = 9#u64 := by + cases af with + | vmFault va d => exact Or.inl hfr.1 + | vgicMaintenance o => exact Or.inr (Or.inl hfr.1) + | vcpuFault h => exact Or.inr (Or.inr (Or.inl hfr.1)) + | vppiEvent irq => exact Or.inr (Or.inr (Or.inr hfr.1)) + have hgo : ∀ k : U64, ty = k → (k = 6#u64 ∨ k = 7#u64 ∨ k = 8#u64 ∨ k = 9#u64) → + k ≠ fl4_abi.consts.fault.CapFault ∧ k ≠ fl4_abi.consts.fault.UnknownSyscall ∧ + k ≠ fl4_abi.consts.fault.UserException ∧ k ≠ fl4_abi.consts.fault.Timeout ∧ + k ≠ fl4_abi.consts.fault.DebugException := by + intro k _ h + unfold fl4_abi.consts.fault.CapFault fl4_abi.consts.fault.UnknownSyscall fl4_abi.consts.fault.UserException + fl4_abi.consts.fault.Timeout fl4_abi.consts.fault.DebugException + rcases h with rfl | rfl | rfl | rfl <;> decide + obtain ⟨n1, n2, n3, n4, n5⟩ := hgo ty rfl (by rw [hk]; exact h6) + rw [hdo ty rfl n1 n2 n3 n4 n5, WP.spec_ok] + exact ⟨rfl, idle true rfl⟩ + +end + +end + +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/EpFaultTmoA.lean b/proofs/Fl4ProofsA64/P3/EpFaultTmoA.lean new file mode 100644 index 000000000..047de04ff --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/EpFaultTmoA.lean @@ -0,0 +1,228 @@ +/- +P3 (aarch64, A64-P3-IPC gen 10, Architect 00:51Z `update_consumed` contract): **the timeout fault of a sender +with a scheduling context** (x86 `P3/IpcTmo`'s `makeFaultMsg_timeoutSC` / `schedContextUpdateConsumed_eq`, on the +endpoint relation). + +* **`TimeoutScOpA`** (composition op, not a premise; MCS discharges it): FL4's `schedcontext::update_consumed` of the + nonzero SC word of a timeout-faulted `t ∈ T` with caller-local `NotIdle t` under `EpStateRel ∧ SR` has one outcome for + `sched_context_update_consumed`, keeps the footprint (`FootEq`), `KView σ σ'` and `SR`. +* Spec: `getSchedContext_eqA`, `updateSchedContext_eqA`, **`updateConsumed_oneA`** (an SC object: one outcome, only + that SC changed), `nd_one_injA`, **`makeFaultMsg_timeoutScA`**. +* FL4: **`set_mrs_fault_timeoutScA`** (the arm, on a family of message laws from the state after `update_consumed`) + and **`do_fault_transfer_timeoutScA`** (with or without a receive buffer, l4v `do_fault_transfer`'s one outcome; + the kheap kept but at the receiver and the SC). + +Case selectors / placement: the sender's timeout fault, its SC (`tcbSchedContext = some (specPtr w)`, `w` its FL4 SC +word, nonzero), the SC object at `w` (`hky`). No named premise. +-/ +import Fl4ProofsA64.P3.EpFaultXferA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif + +noncomputable section + +section Spec +open Fl4SpecA64 + +theorem getSchedContext_eqA {σ : State Unit} {p : ObjRef} {y : SchedContext} {n : Nat} + (hk : σ.kheap p = some (.schedContext y n)) : getSchedContext (Z := Unit) p σ = ({(y, σ)}, False) := by + unfold getSchedContext + rw [nd_bind_one (getObject_eq hk)] + rfl + +theorem updateSchedContext_eqA {σ : State Unit} {p : ObjRef} {y : SchedContext} {n : Nat} + (hk : σ.kheap p = some (.schedContext y n)) (f : SchedContext → SchedContext) : + updateSchedContext (Z := Unit) p f σ = ({((), updK σ p (.schedContext (f y) n))}, False) := by + unfold updateSchedContext + rw [nd_bind_one (getObject_eq hk)] + exact setObject_eq hk rfl + +/-- `sched_context_update_consumed` of an SC object: one outcome, only that SC changed. -/ +theorem updateConsumed_oneA {σ : State Unit} {p : ObjRef} {y : SchedContext} {n : Nat} + (hk : σ.kheap p = some (.schedContext y n)) : + ∃ v y', schedContextUpdateConsumed (Z := Unit) p σ = ({(v, updK σ p (.schedContext y' n))}, False) := by + unfold schedContextUpdateConsumed + rw [nd_bind_one (getSchedContext_eqA hk), nd_pure_bind'] + by_cases h : maxTicksToUs ≤ y.scConsumed + · rw [if_pos h] + exact ⟨_, _, by rw [nd_bind_one (updateSchedContext_eqA hk _)]; rfl⟩ + · rw [if_neg h] + exact ⟨_, _, by rw [nd_bind_one (updateSchedContext_eqA hk _)]; rfl⟩ + +theorem nd_one_injA {α β : Type} {a c : α} {b d : β} + (h : (({(a, b)} : Set (α × β)), False) = ({(c, d)}, False)) : a = c ∧ b = d := by + have := congrArg Prod.fst h + simp only [Set.singleton_eq_singleton_iff, Prod.mk.injEq] at this + exact this + +/-- The fault message of a timeout of a sender with an SC (x86 `makeFaultMsg_timeoutSC`). -/ +theorem makeFaultMsg_timeoutScA {σ σu : State Unit} {t sc : ObjRef} {x : Tcb} (hk : σ.kheap t = some (.tcb x)) + (hsc : x.tcbSchedContext = some sc) {cv : Time} + (hu : schedContextUpdateConsumed (Z := Unit) sc σ = ({(cv, σu)}, False)) (b : Word) : + makeFaultMsg (Z := Unit) (.timeout b) t σ = ({((5, [b, cv]), σu)}, False) := by + rw [makeFaultMsg] + show (Fl4Spec.NonDet.getsThe _ >>= _) σ = _ + rw [nd_bind_one (getTcb_eq hk), hsc] + show (schedContextUpdateConsumed sc >>= _) σ = _ + rw [nd_bind_one hu] + simp [wordsFromTime, ucast] + +end Spec + +section Fl4 +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {T E R : Set U64} + {Bf : Set (U64 × Fl4SpecA64.Word)} {SR : S → Fl4SpecA64.State Unit → Prop} + +variable (st) in +open Fl4SpecA64 in +/-- **The `update_consumed` op of a timeout message** (Architect 00:51Z; a composition interface, MCS discharges +it): FL4's `update_consumed` of the nonzero SC word of `t ∈ T` refines l4v `sched_context_update_consumed` (one +outcome `(v.bv, σ')`), keeping the endpoint footprint, `KView` and `SR`. -/ +structure TimeoutScOpA {A : Type} (ar : machine.Arch A) (T E R : Set U64) (Bf : Set (U64 × Word)) + (SR : S → State Unit → Prop) (NotIdle : U64 → Prop) : Prop where + upd : ∀ (s : S) (a : A) (σ : State Unit) (t : U64), EpStateRel T E R Bf (L.mem s) σ → SR s σ → t ∈ T → + NotIdle t → + L.mem s (scA t) ≠ 0#u64 → + schedcontext.update_consumed st ar s a (L.mem s (scA t)) ⦃ r => ∃ σ', + schedContextUpdateConsumed (Z := Unit) (specPtr (L.mem s (scA t))) σ = ({(r.1.bv, σ')}, False) ∧ + FootEq T E R Bf (L.mem s) (L.mem r.2) ∧ KView σ σ' ∧ SR r.2 σ' ∧ + (∀ y n, σ.kheap (specPtr (L.mem s (scA t))) = some (.schedContext y n) → WritesIn st s r.2 + (fun b => (L.mem s (scA t)).val ≤ b.val ∧ b.val < (L.mem s (scA t)).val + 2 ^ (n + minSchedContextBits))) ⦄ + +variable {A : Type} {ar : machine.Arch A} {NotIdle : U64 → Prop} + +open Fl4SpecA64 in +/-- **`set_mrs_fault` of a timeout of a sender with an SC**: `update_consumed`, then `[badge, consumed]` written +under the message law `J s1 σ1` of the state after it. -/ +theorem set_mrs_fault_timeoutScA (hTO : TimeoutScOpA st ar T E R Bf SR NotIdle) (hP : EPlaced T E R Bf) (s : S) (a : A) (σ : State Unit) + (hR : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) {snd rcv : U64} (hs : snd ∈ T) {xs : Tcb} + (hks : σ.kheap (specPtr snd) = some (.tcb xs)) {b : Word} (hf : xs.tcbFault = some (.timeout b)) + (hni : NotIdle snd) (hsc0 : L.mem s (scA snd) ≠ 0#u64) {J : S → State Unit → S → List Word → Prop} {buf : U64} {g : Nat → Nat} + (hg1 : g 1 = 1) + (hJ : ∀ s1 σ1 v, schedContextUpdateConsumed (Z := Unit) (specPtr (L.mem s (scA snd))) σ = ({(v, σ1)}, False) → + EpStateRel T E R Bf (L.mem s1) σ1 → SR s1 σ1 → MsgLawA st Bf (J s1 σ1) rcv buf g s1) : + faults.set_mrs_fault st ar s a snd rcv buf ⦃ r => ∃ s1 σ1 v, + schedContextUpdateConsumed (Z := Unit) (specPtr (L.mem s (scA snd))) σ = ({(v, σ1)}, False) ∧ + FootEq T E R Bf (L.mem s) (L.mem s1) ∧ EpStateRel T E R Bf (L.mem s1) σ1 ∧ SR s1 σ1 ∧ + (∀ y n, σ.kheap (specPtr (L.mem s (scA snd))) = some (.schedContext y n) → WritesIn st s s1 + (fun b => (L.mem s (scA snd)).val ≤ b.val ∧ + b.val < (L.mem s (scA snd)).val + 2 ^ (n + minSchedContextBits))) ∧ + r.1.val = g 2 ∧ J s1 σ1 r.2 [b, v] ⦄ := by + have hxs := hR.at_tcb hs hks + have hfr := hxs.faultR + unfold FaultRel at hfr + rw [hf] at hfr + obtain ⟨hty, hw0⟩ := hfr + refine set_mrs_fault_tmo_specA s a hty (fun h => absurd h hsc0) (fun _ => ?_) + refine WP.spec_bind (hTO.upd s a σ snd hR hSR hs hni hsc0) ?_ + rintro ⟨cv, s1⟩ ⟨σ1, heq, hF, hK, hS1, hWs⟩ + have hR1 := hR.frame hP hF hK + have hM := hJ s1 σ1 cv.bv heq hR1 hS1 + refine WP.spec_bind (hM.set _ _ hM.start 0#u64 rfl (by decide) (L.mem s (tFw0A snd))) ?_ + rintro ⟨len, s2⟩ ⟨hl, hI2⟩ + have hl' : len.val = 1 := hl.trans hg1 + refine WP.spec_mono (hM.set _ _ hI2 len (by rw [hl']; rfl) (by rw [hl']; decide) cv) ?_ + rintro ⟨k, s3⟩ ⟨hk, hI3⟩ + refine ⟨s1, σ1, cv.bv, heq, hF, hR1, hS1, hWs, ?_, ?_⟩ + · rw [hk, hl'] + · rw [hw0] at hI3; simpa using hI3 + +open Fl4SpecA64 in +/-- **`do_fault_transfer` of a timeout of a sender with an SC**, with or without a receive buffer: l4v +`do_fault_transfer`'s one outcome (`update_consumed`, then `[badge, consumed]`); the kheap kept but at the receiver +and the SC, the writes in `EWrite` after `update_consumed`. -/ +theorem do_fault_transfer_timeoutScA (hTO : TimeoutScOpA st ar T E R Bf SR NotIdle) (hO : SchedOpsE0A st T E R Bf SR) + (hP : EPlaced T E R Bf) (s : S) (a : A) (σ : State Unit) (hR : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) + {snd rcv : U64} (hs : snd ∈ T) (hr : rcv ∈ T) (hne : snd ≠ rcv) {xs xr : Tcb} + (hks : σ.kheap (specPtr snd) = some (.tcb xs)) (hkr : σ.kheap (specPtr rcv) = some (.tcb xr)) {b : Word} + (hf : xs.tcbFault = some (.timeout b)) (hsc : xs.tcbSchedContext = some (specPtr (L.mem s (scA snd)))) + (hni : NotIdle snd) (hsc0 : L.mem s (scA snd) ≠ 0#u64) {y : SchedContext} {n : Nat} + (hky : σ.kheap (specPtr (L.mem s (scA snd))) = some (.schedContext y n)) + {rbw : U64} {rbo : Option ObjRef} + (hrw : (rbo = none ∧ rbw = 0#u64) ∨ (∃ br ∈ Bf, rbo = some br.2 ∧ rbw = br.1 ∧ br.1 ≠ 0#u64)) (badge : U64) : + ipc.do_fault_transfer st ar s a badge snd rcv rbw ⦃ s' => ∃ σ', + doFaultTransfer (Z := Unit) badge.bv (specPtr snd) (specPtr rcv) rbo σ = ({((), σ')}, False) ∧ + EpStateRel T E R Bf (L.mem s') σ' ∧ SR s' σ' ∧ + (∃ s1, FootEq T E R Bf (L.mem s) (L.mem s1) ∧ WritesIn st s1 s' (EWrite T E R Bf) ∧ + WritesIn st s s1 (fun b => (L.mem s (scA snd)).val ≤ b.val ∧ + b.val < (L.mem s (scA snd)).val + 2 ^ (n + minSchedContextBits)) ∧ + Agree (L.mem s1) (L.mem s') (fun b => ¬ RcvRegA rcv b ∧ ¬ BufW Bf b)) ∧ + (σ'.readyQueues = σ.readyQueues ∧ σ'.releaseQueue = σ.releaseQueue ∧ σ'.idleThread = σ.idleThread ∧ + σ'.schedulerAction = σ.schedulerAction) ∧ + (∃ y', σ'.kheap (specPtr (L.mem s (scA snd))) = some (.schedContext y' n)) ∧ + (∀ p, p ≠ specPtr rcv → p ≠ specPtr (L.mem s (scA snd)) → σ'.kheap p = σ.kheap p) ∧ + ∃ x', σ'.kheap (specPtr rcv) = some (.tcb x') ∧ x' = { xr with tcbArch := x'.tcbArch } ⦄ := by + have hsa := hP.tcb snd hs + obtain ⟨v0, y', hU⟩ := updateConsumed_oneA hky + have hσ1 : ∀ v σ', schedContextUpdateConsumed (Z := Unit) (specPtr (L.mem s (scA snd))) σ = ({(v, σ')}, False) → + v = v0 ∧ σ' = updK σ (specPtr (L.mem s (scA snd))) (.schedContext y' n) := + fun v σ' h => nd_one_injA (h.symm.trans hU) + have hrsc : specPtr rcv ≠ specPtr (L.mem s (scA snd)) := fun e => by rw [e, hky] at hkr; cases hkr + have hkr1 : (updK σ (specPtr (L.mem s (scA snd))) (.schedContext y' n)).kheap (specPtr rcv) = some (.tcb xr) := by + rw [updK_kheap, Function.update_of_ne hrsc, hkr] + have hmsg := makeFaultMsg_timeoutScA hks hsc hU b + have hfr := (hR.at_tcb hs hks).faultR + unfold FaultRel at hfr + rw [hf] at hfr + have hlab : (L.mem s (tFaultA snd)).bv &&& mask msgLabelBits = 5 := by rw [hfr.1, mask52A]; decide + have hfoot : EFoot T E R Bf (tFaultA snd) := Or.inr (Or.inr (Or.inl ⟨snd, hs, Or.inl (a_fault hsa).2⟩)) + rcases hrw with ⟨rfl, rfl⟩ | ⟨br, hbr, rfl, rfl, hbr0⟩ + · have harm := set_mrs_fault_timeoutScA (ar := ar) hTO hP s a σ hR hSR hs hks hf hni hsc0 (rcv := rcv) + (buf := 0#u64) (J := fun s1 σ1 s' ms => MrInvNA (L := L) T E R Bf SR s1 σ1 rcv xr s' ms ∧ + WritesIn st s1 s' (EWrite T E R Bf)) (g := fun n => min n 4) rfl (fun s1 σ1 v heq hR1 hS1 => by + obtain ⟨-, rfl⟩ := hσ1 _ _ heq + exact (msgLaw_nobuf hO hP hR1 hS1 hr hkr1).withW hP hr (Or.inl rfl)) + refine WP.spec_mono (do_fault_transfer_tailA (Q := fun s1 => FootEq T E R Bf (L.mem s) (L.mem s1) ∧ + WritesIn st s s1 (fun b => (L.mem s (scA snd)).val ≤ b.val ∧ + b.val < (L.mem s (scA snd)).val + 2 ^ (n + minSchedContextBits))) + (σm := mrStateNA (updK σ (specPtr (L.mem s (scA snd))) (.schedContext y' n)) (specPtr rcv) xr [b, v0]) + hO hP s a hs hr hne (mrStateNA_kheap _ _ _ _) hlab (n := 2) (by decide) + (WP.spec_mono harm fun r ⟨s1, σ1, v, heq, hF, _, _, hWs, hk, hJ⟩ => by + obtain ⟨rfl, rfl⟩ := hσ1 _ _ heq + exact ⟨hk, hJ.1.1, hJ.1.2.1, s1, ⟨hF, hWs y n hky⟩, hF _ (a_fault hsa).1 hfoot, hJ.1.2.2, hJ.2⟩) badge) ?_ + rintro s' ⟨h1, h2, ⟨s1, ⟨hF, hWs'⟩, hAg, h4⟩, -⟩ + refine ⟨_, ?_, h1, h2, ⟨s1, hF, h4, hWs', hAg⟩, ⟨rfl, rfl, rfl, rfl⟩, ⟨y', ?_⟩, fun q hq hq' => ?_, _, + by rw [updK_kheap, Function.update_self], faultTcbA_with _ _ _ _ _⟩ + · rw [doFaultTransfer_eqNA hks hkr1 hf hmsg badge.bv] + rfl + · rw [updK_kheap, Function.update_of_ne hrsc.symm] + unfold mrStateNA + rw [updK_kheap, Function.update_of_ne hrsc.symm, updK_kheap, Function.update_self] + · rw [updK_kheap, Function.update_of_ne hq] + unfold mrStateNA + rw [updK_kheap, Function.update_of_ne hq, updK_kheap, Function.update_of_ne hq'] + · have harm := set_mrs_fault_timeoutScA (ar := ar) hTO hP s a σ hR hSR hs hks hf hni hsc0 (rcv := rcv) + (buf := br.1) (J := fun s1 σ1 s' ms => MrInvA (L := L) T E R Bf SR s1 σ1 rcv xr br.2 s' ms ∧ + WritesIn st s1 s' (EWrite T E R Bf)) (g := id) rfl (fun s1 σ1 v heq hR1 hS1 => by + obtain ⟨-, rfl⟩ := hσ1 _ _ heq + exact (msgLaw_buf hO hP hR1 hS1 hr hkr1 hbr hbr0).withW hP hr (Or.inr ⟨br, hbr, rfl⟩)) + have hal : ∀ q ∈ mrPairs [b, v0], (bufAddr br.2 q.1).toNat % 8 = 0 ∧ + inUserFrame (bufAddr br.2 q.1) (updK σ (specPtr (L.mem s (scA snd))) (.schedContext y' n)) := by + intro q hq; simp [mrPairs] at hq + refine WP.spec_mono (do_fault_transfer_tailA (Q := fun s1 => FootEq T E R Bf (L.mem s) (L.mem s1) ∧ + WritesIn st s s1 (fun b => (L.mem s (scA snd)).val ≤ b.val ∧ + b.val < (L.mem s (scA snd)).val + 2 ^ (n + minSchedContextBits))) + (σm := mrState (updK σ (specPtr (L.mem s (scA snd))) (.schedContext y' n)) (specPtr rcv) xr br.2 [b, v0]) + hO hP s a hs hr hne (mrState_kheap _ _ _ _ _) hlab (n := 2) (by decide) + (WP.spec_mono harm fun r ⟨s1, σ1, v, heq, hF, _, _, hWs, hk, hJ⟩ => by + obtain ⟨rfl, rfl⟩ := hσ1 _ _ heq + exact ⟨hk, hJ.1.1, hJ.1.2.1, s1, ⟨hF, hWs y n hky⟩, hF _ (a_fault hsa).1 hfoot, hJ.1.2.2, hJ.2⟩) badge) ?_ + rintro s' ⟨h1, h2, ⟨s1, ⟨hF, hWs'⟩, hAg, h4⟩, -⟩ + refine ⟨_, ?_, h1, h2, ⟨s1, hF, h4, hWs', hAg⟩, ⟨rfl, rfl, rfl, rfl⟩, ⟨y', ?_⟩, fun q hq hq' => ?_, _, + by rw [updK_kheap, Function.update_self], faultTcbA_with _ _ _ _ _⟩ + · rw [doFaultTransfer_eqA hks hkr1 hf hmsg br.2 hal badge.bv] + rfl + · rw [updK_kheap, Function.update_of_ne hrsc.symm] + show (updK _ (specPtr rcv) _).kheap _ = _ + rw [updK_kheap, Function.update_of_ne hrsc.symm, updK_kheap, Function.update_self] + · rw [updK_kheap, Function.update_of_ne hq] + show (updK _ (specPtr rcv) _).kheap q = _ + rw [updK_kheap, Function.update_of_ne hq, updK_kheap, Function.update_of_ne hq'] + +end Fl4 + +end + +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/EpFaultXferA.lean b/proofs/Fl4ProofsA64/P3/EpFaultXferA.lean new file mode 100644 index 000000000..81f6141b0 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/EpFaultXferA.lean @@ -0,0 +1,325 @@ +/- +P3 (aarch64, A64-P3-IPC gen 10, ruling item 2): **FL4's `do_fault_transfer` and the fault branch of +`do_ipc_transfer`** (x86 `P3/EpFault`'s `do_fault_transfer_full` / `_gen`, `P3/EpFaultMore`'s `FaultOK` / +`do_fault_transfer_rel` / `do_ipc_transfer_fault` and `P3/EpFaultNone`'s twins, one proof for both receivers). + +* `set_mr_writesA`, **`MsgLawA.withW`**: the message invariant with the writes kept in `EWrite` (`WritesIn`). +* `faultTcbA`; **`do_fault_transfer_tailA`**: after `set_mrs_fault` has written `ms` under a state `σm` (the spec's + state after `set_mrs`), the label (the fault type word), `set_msg_info` and the badge give + `updK σm rcv (faultTcbA …)`, `EpStateRel ∧ SR` kept. +* **`do_fault_transfer_msgA`**: one fault message (`make_fault_msg`'s outcome, the arm's `set_mrs_fault` on every + `MsgLawA`), with or without a receive buffer, against l4v `do_fault_transfer` (`doFaultTransfer_eqA` / `_eqNA`). +* **`FaultOKA`** (case selectors) and **`do_fault_transfer_relA`**: every fault kind but a timeout of a sender with + an SC (`EpFaultTmoA`) and a VGIC maintenance fault without an index (FL4 differs from seL4, see `EpFaultMoreA`). +* **`do_ipc_transfer_faultA`** (the fault branch) and **`do_ipc_transfer_anyA`** (both branches of + `do_ipc_transfer`, gen 9's `do_ipc_transfer_relA` for the other): one spec outcome, the post of + `do_ipc_transfer_relA`. + +Hypotheses: as `do_ipc_transfer_relA` (`PlaceA` / `StateRelA` / `BufCover` for the receive-buffer lookup, `hu`), the +sender's fault and `FaultOKA` (case selectors). No named premise. +-/ +import Fl4ProofsA64.P3.EpFaultMoreA +import Fl4ProofsA64.P3.EpXferA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif + +noncomputable section + +section Fl4 +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {T E R : Set U64} + {Bf : Set (U64 × Fl4SpecA64.Word)} {SR : S → Fl4SpecA64.State Unit → Prop} + +/-- `set_mr` writes inside `EWrite` (the receiver's registers or a tracked buffer word). -/ +theorem set_mr_writesA (hP : EPlaced T E R Bf) {rcv : U64} (hr : rcv ∈ T) {buf : U64} + (hbw : buf = 0#u64 ∨ ∃ bp ∈ Bf, buf = bp.1) (s : S) (i : U64) (hi : i.val < 120) (v : U64) : + ∃ r, msg.set_mr st s rcv buf i v = ok r ∧ WritesIn st s r.2 (EWrite T E R Bf) := by + have hra := hP.tcb rcv hr + by_cases h4 : i.val < 4 + · refine ⟨_, set_mr_reg s rcv buf i v h4, ?_⟩ + have ha := a_reg hra (mrIdx i.val) + exact (writesIn_wr ha.1).mono fun b e => by + rw [e]; exact Or.inr (Or.inr (Or.inl ⟨rcv, hr, Or.inl (twf_reg hra _)⟩)) + · rcases hbw with rfl | ⟨bp, hb, rfl⟩ + · exact ⟨_, set_mr_nobuf s rcv i v (by omega), WritesIn.refl _ _⟩ + · by_cases hb0 : bp.1 = 0#u64 + · rw [hb0]; exact ⟨_, set_mr_nobuf s rcv i v (by omega), WritesIn.refl _ _⟩ + · refine ⟨_, set_mr_buf s rcv bp.1 i v (by omega) hb0, ?_⟩ + have hk : (core.num.U64.wrapping_add i 1#u64).val < 128 := by + simp [core.num.U64.wrapping_add_val_eq, U64.size_def, U64.numBits]; omega + have hw : BufW Bf (bufW bp.1 (core.num.U64.wrapping_add i 1#u64)) := ⟨bp, hb, _, hk, rfl⟩ + exact (writesIn_wr (hP.balign _ hw)).mono fun b e => by rw [e]; exact Or.inr (Or.inr (Or.inr hw)) + +/-- A message invariant with the writes kept in `EWrite` since its start. -/ +theorem MsgLawA.withW (hP : EPlaced T E R Bf) {I : S → List Fl4SpecA64.Word → Prop} {rcv buf : U64} + {g : Nat → Nat} {s : S} (hM : MsgLawA st Bf I rcv buf g s) (hr : rcv ∈ T) + (hbw : buf = 0#u64 ∨ ∃ bp ∈ Bf, buf = bp.1) : + MsgLawA st Bf (fun s' ms => I s' ms ∧ WritesIn st s s' (EWrite T E R Bf)) rcv buf g s := by + refine ⟨fun s' ms hI i hi hi' v => ?_, fun hb s' ms ys hI h4 => ⟨hM.ext hb s' ms ys hI.1 h4, hI.2⟩, + ⟨hM.start, WritesIn.refl _ _⟩, fun s' ms hI => hM.agree s' ms hI.1⟩ + obtain ⟨r, he, hw⟩ := set_mr_writesA (L := L) hP hr hbw s' i hi' v + have h := hM.set s' ms hI.1 i hi hi' v + rw [he, WP.spec_ok] at h ⊢ + exact ⟨h.1, h.2, hI.2.trans1 hw⟩ + +open Fl4SpecA64 in +/-- The receiver after a fault message `ms` of label `label` and length `n` and the badge `b`. -/ +def faultTcbA (xr : Tcb) (ms : List Word) (label : Data) (n : Nat) (b : Word) : Tcb := + setReg (setReg (withRegs xr (mrRegs xr.tcbArch.tcbContext.userRegs ms)) msg_info_register + (messageInfoToData ⟨natToLen n, 0, 0, label⟩)) badge_register b + +theorem faultTcbA_with (xr : Fl4SpecA64.Tcb) (ms : List Fl4SpecA64.Word) (label : Fl4SpecA64.Data) (n : Nat) + (b : Fl4SpecA64.Word) : faultTcbA xr ms label n b = { xr with tcbArch := (faultTcbA xr ms label n b).tcbArch } := + setReg_withA (setReg_withA rfl _ _) _ _ + +variable {A : Type} {ar : machine.Arch A} + +open Fl4SpecA64 in +/-- **The tail of `do_fault_transfer`** (x86 `do_fault_transfer_full`): after `set_mrs_fault` has written `ms` +(length `n`) under the spec state `σm`, the label and the badge. -/ +theorem do_fault_transfer_tailA (hO : SchedOpsE0A st T E R Bf SR) (hP : EPlaced T E R Bf) (s : S) (a : A) + {snd rcv : U64} (hs : snd ∈ T) (hr : rcv ∈ T) (hne : snd ≠ rcv) {Q : S → Prop} {σm : State Unit} {xr : Tcb} + {ms : List Word} + (hkσm : σm.kheap (specPtr rcv) = some (.tcb (withRegs xr (mrRegs xr.tcbArch.tcbContext.userRegs ms)))) + {label : Data} + (hlab : (L.mem s (tFaultA snd)).bv &&& mask msgLabelBits = label) {n : Nat} (hn : n < 128) {buf : U64} + (hset : faults.set_mrs_fault st ar s a snd rcv buf ⦃ r => r.1.val = n ∧ EpStateRel T E R Bf (L.mem r.2) σm ∧ + SR r.2 σm ∧ ∃ s0, Q s0 ∧ L.mem s0 (tFaultA snd) = L.mem s (tFaultA snd) ∧ + Agree (L.mem s0) (L.mem r.2) (fun b => ¬ RcvRegA rcv b ∧ ¬ BufW Bf b) ∧ + WritesIn st s0 r.2 (EWrite T E R Bf) ⦄) (badge : U64) : + ipc.do_fault_transfer st ar s a badge snd rcv buf ⦃ s' => + EpStateRel T E R Bf (L.mem s') (updK σm (specPtr rcv) (.tcb (faultTcbA xr ms label n badge.bv))) ∧ + SR s' (updK σm (specPtr rcv) (.tcb (faultTcbA xr ms label n badge.bv))) ∧ + (∃ s0, Q s0 ∧ Agree (L.mem s0) (L.mem s') (fun b => ¬ RcvRegA rcv b ∧ ¬ BufW Bf b) ∧ + WritesIn st s0 s' (EWrite T E R Bf)) ∧ + EpStep σm (updK σm (specPtr rcv) (.tcb (faultTcbA xr ms label n badge.bv))) ⦄ := by + have hra := hP.tcb rcv hr + have hsa := hP.tcb snd hs + unfold ipc.do_fault_transfer + refine WP.spec_bind hset ?_ + rintro ⟨sent, s1⟩ ⟨hn1, hR1, hS1, s0, hQ, h0f, hA1, hW1⟩ + step with obj.Tcb.fault_type.spec as ⟨ft, hft⟩ + rw [snd_word_eqA hP hs hr hne hA1 (a_fault hsa), h0f] at hft + subst hft + unfold msginfo.MessageInfo.new + rw [bind_ok] + refine WP.spec_bind (set_msg_info_specA s1 rcv _ sent (by have : sent.val = n := hn1; omega)) ?_ + rintro s2 ⟨v, rfl, hv⟩ + rw [set_reg_eqA, WP.spec_ok] + obtain ⟨hR2, hW2, hS2⟩ := write_reg_rel hP s1 _ hR1 hr hkσm msg_info_register v + rw [regIdxA_msgInfo] at hR2 hW2 + have hk2 : (updK σm (specPtr rcv) (.tcb (setReg (withRegs xr (mrRegs xr.tcbArch.tcbContext.userRegs ms)) + msg_info_register v.bv))).kheap (specPtr rcv) = + some (.tcb (setReg (withRegs xr (mrRegs xr.tcbArch.tcbContext.userRegs ms)) msg_info_register v.bv)) := by + simp + obtain ⟨hR3, hW3, hS3⟩ := write_reg_rel hP _ _ hR2 hr hk2 badge_register badge + rw [regIdxA_badge] at hR3 hW3 + have hSR2 := hO.stable _ _ _ _ hS1 hW2 hS2 (NoBlockE.tcbK hkσm _ rfl rfl) + have hSR3 := hO.stable _ _ _ _ hSR2 hW3 hS3 (NoBlockE.tcbK hk2 _ rfl rfl) + have hvx : v.bv = messageInfoToData ⟨natToLen n, 0, 0, label⟩ := by + rw [hv, hlab, bv_ofNat_valA sent hn1]; rfl + have hfin : updK (updK σm (specPtr rcv) (.tcb (setReg (withRegs xr (mrRegs xr.tcbArch.tcbContext.userRegs ms)) + msg_info_register v.bv))) (specPtr rcv) (.tcb (setReg (setReg (withRegs xr + (mrRegs xr.tcbArch.tcbContext.userRegs ms)) msg_info_register v.bv) badge_register badge.bv)) = + updK σm (specPtr rcv) (.tcb (faultTcbA xr ms label n badge.bv)) := by + simp only [updK, Function.update_idem, faultTcbA]; rw [hvx] + rw [hfin] at hR3 hSR3 hS3 + refine ⟨hR3, hSR3, ⟨s0, hQ, ?_, (hW1.trans (hW2.trans hW3)).mono fun b h => by rcases h with h | h | h <;> exact h⟩, + hS2.trans hS3⟩ + intro b hb hn' + rw [← regIdxA_badge, ← regIdxA_msgInfo, + rd_wr_ne (a_reg hra badge_register).1 hb (fun h => hn'.1 ⟨badge_register, by rw [← h]⟩), + rd_wr_ne (a_reg hra msg_info_register).1 hb (fun h => hn'.1 ⟨msg_info_register, by rw [← h]⟩)] + exact hA1 b hb hn' + +/-- The post of a transfer to `rcv` from `(s, σ)` (`do_ipc_transfer_relA`'s). -/ +def XferPostA (T E R : Set U64) (Bf : Set (U64 × Fl4SpecA64.Word)) (SR : S → Fl4SpecA64.State Unit → Prop) (s : S) + (σ : Fl4SpecA64.State Unit) (rcv : U64) (xr : Fl4SpecA64.Tcb) (s' : S) (σ' : Fl4SpecA64.State Unit) : Prop := + EpStateRel T E R Bf (L.mem s') σ' ∧ SR s' σ' ∧ + Agree (L.mem s) (L.mem s') (fun b => ¬ RcvRegA rcv b ∧ ¬ BufW Bf b) ∧ WritesIn st s s' (EWrite T E R Bf) ∧ + EpStep σ σ' ∧ (∀ p, p ≠ specPtr rcv → σ'.kheap p = σ.kheap p) ∧ + ∃ x', σ'.kheap (specPtr rcv) = some (.tcb x') ∧ x' = { xr with tcbArch := x'.tcbArch } + +open Fl4SpecA64 in +/-- **`do_fault_transfer` of one fault message** (x86 `do_fault_transfer_gen` and its no-buffer twin): with or +without a receive buffer, l4v `do_fault_transfer`'s one outcome. -/ +theorem do_fault_transfer_msgA (hO : SchedOpsEA st T E R Bf SR) (hP : EPlaced T E R Bf) (s : S) (a : A) + (σ : State Unit) (hR : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) {snd rcv : U64} (hs : snd ∈ T) + (hr : rcv ∈ T) (hne : snd ≠ rcv) {xs xr : Tcb} (hks : σ.kheap (specPtr snd) = some (.tcb xs)) + (hkr : σ.kheap (specPtr rcv) = some (.tcb xr)) {f : Fault} (hf : xs.tcbFault = some f) {label : Data} + {ms : List Word} (hmsg : makeFaultMsg (Z := Unit) f (specPtr snd) σ = ({((label, ms), σ)}, False)) + (hlen : ms.length ≤ 120) (hlab : (L.mem s (tFaultA snd)).bv &&& mask msgLabelBits = label) + {rbw : U64} {rbo : Option ObjRef} + (hrw : (rbo = none ∧ rbw = 0#u64) ∨ (∃ br ∈ Bf, rbo = some br.2 ∧ rbw = br.1 ∧ br.1 ≠ 0#u64)) + (harm : ∀ (I : S → List Word → Prop) (g : Nat → Nat), MsgLawA st Bf I rcv rbw g s → + faults.set_mrs_fault st ar s a snd rcv rbw ⦃ r => r.1.val = g ms.length ∧ I r.2 ms ⦄) + (hu : ∀ rp, rbo = some rp → ∀ k, k < 128 → inUserFrame (bufAddr rp k) σ) (badge : U64) : + ipc.do_fault_transfer st ar s a badge snd rcv rbw ⦃ s' => ∃ σ', + doFaultTransfer (Z := Unit) badge.bv (specPtr snd) (specPtr rcv) rbo σ = ({((), σ')}, False) ∧ + XferPostA (L := L) T E R Bf SR s σ rcv xr s' σ' ⦄ := by + rcases hrw with ⟨rfl, rfl⟩ | ⟨br, hbr, rfl, rfl, hbr0⟩ + · have hM := (msgLaw_nobuf (L := L) hO.to0 hP hR hSR hr hkr).withW hP hr (Or.inl rfl) + refine WP.spec_mono (do_fault_transfer_tailA (Q := fun s0 => s0 = s) (σm := mrStateNA σ (specPtr rcv) xr ms) hO.to0 hP s a hs hr hne + (mrStateNA_kheap _ _ _ _) hlab (n := min ms.length 4) (by omega) + (WP.spec_mono (harm _ _ hM) fun r ⟨h1, h2, h3⟩ => ⟨h1, h2.1, h2.2.1, s, rfl, rfl, h2.2.2, h3⟩) badge) ?_ + rintro s' ⟨h1, h2, ⟨_, rfl, h3, h4⟩, h5⟩ + refine ⟨_, ?_, h1, h2, h3, h4, (show EpStep σ (mrStateNA σ (specPtr rcv) xr ms) from EpStep.tcb hkr xr.tcbState _).trans h5, fun q hq => ?_, + _, by rw [updK_kheap, Function.update_self], faultTcbA_with _ _ _ _ _⟩ + · rw [doFaultTransfer_eqNA hks hkr hf hmsg badge.bv, Nat.min_comm]; rfl + · rw [updK_kheap, Function.update_of_ne hq]; unfold mrStateNA; rw [updK_kheap, Function.update_of_ne hq] + · have hM := (msgLaw_buf (L := L) hO.to0 hP hR hSR hr hkr hbr hbr0).withW hP hr (Or.inr ⟨br, hbr, rfl⟩) + have hal : ∀ q ∈ mrPairs ms, (bufAddr br.2 q.1).toNat % 8 = 0 ∧ inUserFrame (bufAddr br.2 q.1) σ := by + intro q hq + have := (List.of_mem_zip hq).1 + simp at this + exact ⟨bufAddr_align (hR.buf br hbr 0#u64 (by decide)).2 _ (by omega), hu _ rfl _ (by omega)⟩ + refine WP.spec_mono (do_fault_transfer_tailA (Q := fun s0 => s0 = s) (σm := mrState σ (specPtr rcv) xr br.2 ms) hO.to0 hP s a hs hr hne + (mrState_kheap _ _ _ _ _) hlab (n := ms.length) (by omega) + (WP.spec_mono (harm _ _ hM) fun r ⟨h1, h2, h3⟩ => ⟨h1, h2.1, h2.2.1, s, rfl, rfl, h2.2.2, h3⟩) badge) ?_ + rintro s' ⟨h1, h2, ⟨_, rfl, h3, h4⟩, h5⟩ + refine ⟨_, ?_, h1, h2, h3, h4, (show EpStep σ (mrState σ (specPtr rcv) xr br.2 ms) from + (EpStep.tcb hkr xr.tcbState _).trans (EpStep.setUmA _ _)).trans h5, + fun q hq => ?_, _, by rw [updK_kheap, Function.update_self], faultTcbA_with _ _ _ _ _⟩ + · rw [doFaultTransfer_eqA hks hkr hf hmsg br.2 hal badge.bv, + Nat.min_eq_left (show ms.length ≤ msgMaxLength by unfold msgMaxLength; omega)] + rfl + · rw [updK_kheap, Function.update_of_ne hq] + show (updK σ (specPtr rcv) _).kheap q = _ + rw [updK_kheap, Function.update_of_ne hq] + +/-- The fault kinds whose transfer is proved here (case selectors, x86 `FaultOK`): an unknown syscall of a sender +not trapping syscalls or into a receiver without a buffer; a timeout of a sender without SC (the SC case is +`EpFaultTmoA`); a VGIC maintenance fault with an index (without one, FL4 sends no word: seL4 and l4v send `-1`). -/ +def FaultOKA (s : S) (snd : U64) (rbo : Option Fl4SpecA64.ObjRef) (x : Fl4SpecA64.Tcb) : Fl4SpecA64.Fault → Prop + | .unknownSyscallException _ => + rbo = none ∨ (L.mem s (tTflA snd) &&& fl4_abi.consts.tcb_flag.TRAP_SYSCALLS) = 0#u64 + | .timeout _ => x.tcbSchedContext = none ∧ L.mem s (scA snd) = 0#u64 + | .archFault (.vgicMaintenance none) => False + | _ => True + +open Fl4SpecA64 in +/-- **`do_fault_transfer`** of every fault in `FaultOKA` (x86 `do_fault_transfer_rel`), with or without a receive +buffer. -/ +theorem do_fault_transfer_relA (hO : SchedOpsEA st T E R Bf SR) (hP : EPlaced T E R Bf) (s : S) (a : A) + (σ : State Unit) (hR : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) {snd rcv : U64} (hs : snd ∈ T) + (hr : rcv ∈ T) (hne : snd ≠ rcv) {xs xr : Tcb} (hks : σ.kheap (specPtr snd) = some (.tcb xs)) + (hkr : σ.kheap (specPtr rcv) = some (.tcb xr)) {f : Fault} (hf : xs.tcbFault = some f) + {rbw : U64} {rbo : Option ObjRef} (hok : FaultOKA (L := L) s snd rbo xs f) + (hrw : (rbo = none ∧ rbw = 0#u64) ∨ (∃ br ∈ Bf, rbo = some br.2 ∧ rbw = br.1 ∧ br.1 ≠ 0#u64)) + (hu : ∀ rp, rbo = some rp → ∀ k, k < 128 → inUserFrame (bufAddr rp k) σ) (badge : U64) : + ipc.do_fault_transfer st ar s a badge snd rcv rbw ⦃ s' => ∃ σ', + doFaultTransfer (Z := Unit) badge.bv (specPtr snd) (specPtr rcv) rbo σ = ({((), σ')}, False) ∧ + XferPostA (L := L) T E R Bf SR s σ rcv xr s' σ' ⦄ := by + have hfr := (hR.at_tcb hs hks).faultR + unfold FaultRel at hfr + rw [hf] at hfr + have go := fun {label : Data} {ms : List Word} + (hmsg : makeFaultMsg (Z := Unit) f (specPtr snd) σ = ({((label, ms), σ)}, False)) (hlen : ms.length ≤ 120) + (hlab : (L.mem s (tFaultA snd)).bv &&& mask msgLabelBits = label) + (harm : ∀ (I : S → List Word → Prop) (g : Nat → Nat), MsgLawA st Bf I rcv rbw g s → + faults.set_mrs_fault st ar s a snd rcv rbw ⦃ r => r.1.val = g ms.length ∧ I r.2 ms ⦄) => + do_fault_transfer_msgA (ar := ar) hO hP s a σ hR hSR hs hr hne hks hkr hf hmsg hlen hlab hrw harm hu badge + cases f with + | capFault c rp lf => + exact go (makeFaultMsg_capA hks c rp lf) + (by cases lf <;> simp [capFaultMsgA, msgFromLookupFailure]) (by rw [hfr.1, mask52A]; decide) + fun I g hM => set_mrs_fault_capA hP s a σ hR hs hr hne hks hf hM.set hM.start hM.agree + | unknownSyscallException n => + have htr : TrapOffA (L := L) s snd rbw := by + rcases hok with h | h + · left; rcases hrw with ⟨_, h2⟩ | ⟨br, _, h1, _, _⟩ + · exact h2 + · rw [h] at h1; cases h1 + · exact Or.inr h + exact go (makeFaultMsg_unknownA hks n) (by simp [usMsgA, syscallMessage, Register.x0ToX7]) + (by rw [hfr.1, mask52A]; decide) + fun I g hM => set_mrs_fault_unknownA hP s a σ hR hs hr hne hks hf hM htr + | userException e c => + exact go (makeFaultMsg_userA hks e c) (by simp [ueMsgA, exceptionMessage]) (by rw [hfr.1, mask52A]; decide) + fun I g hM => set_mrs_fault_userA hP s a σ hR hs hr hne hks hf hM + | timeout b => + exact go (makeFaultMsg_timeoutNA hks hok.1 b) (by simp) (by rw [hfr.1, mask52A]; decide) + fun I g hM => set_mrs_fault_timeoutNA hP s a σ hR hs hks hf hok.2 hM + | archFault af => + cases af with + | vmFault va d => + exact go (makeFaultMsg_vmA hks va d) (by rw [hfr.2.2]; simp [vmMsgA]) (by rw [hfr.1, mask52A]; decide) + fun I g hM => set_mrs_fault_vmA hP s a σ hR hs hks hf hM + | vcpuFault h => + exact go (makeFaultMsg_vcpuA σ _ h) (by simp) (by rw [hfr.1, mask52A]; decide) + fun I g hM => set_mrs_fault_vcpuA s a σ hR hs hks hf hM + | vppiEvent irq => + exact go (makeFaultMsg_vppiA σ _ irq) (by simp) (by rw [hfr.1, mask52A]; decide) + fun I g hM => set_mrs_fault_vppiA s a σ hR hs hks hf hM + | vgicMaintenance o => + cases o with + | none => exact (show False from hok).elim + | some i => + exact go (makeFaultMsg_vgicA σ _ i) (by simp) (by rw [hfr.1, mask52A]; decide) + fun I g hM => set_mrs_fault_vgicA s a σ hR hs hks hf hM + +open Fl4SpecA64 in +/-- **`do_ipc_transfer` of a faulted sender refines l4v `do_ipc_transfer`** (x86 `do_ipc_transfer_fault`, both +receivers): its `do_fault_transfer` branch. -/ +theorem do_ipc_transfer_faultA (hO : SchedOpsEA st T E R Bf SR) (hP : EPlaced T E R Bf) {th : TypedHeapA} + (s : S) (a : A) (σ : State Unit) (hR : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) + (hSt : StateRelA th (L.mem s) σ) (hC : BufCover st T Bf s σ) + (hNative : BufNativeA T (L.mem s)) + {snd rcv : U64} (hs : snd ∈ T) (hr : rcv ∈ T) (hne : snd ≠ rcv) (htr : th rcv = some .tcb) + {xs xr : Tcb} (hks : σ.kheap (specPtr snd) = some (.tcb xs)) (hkr : σ.kheap (specPtr rcv) = some (.tcb xr)) + {f : Fault} (hf : xs.tcbFault = some f) (hok : FaultOKA (L := L) s snd (ipcBufOfA true xr) xs f) + (hu : ∀ rp, ipcBufOfA true xr = some rp → ∀ k, k < 128 → inUserFrame (bufAddr rp k) σ) + (ep badge : U64) (grant : Bool) (epo : Option ObjRef) : + ipc.do_ipc_transfer st ar s a snd ep badge grant rcv ⦃ s' => ∃ σ', + doIpcTransfer (Z := Unit) (specPtr snd) epo badge.bv grant (specPtr rcv) σ = ({((), σ')}, False) ∧ + XferPostA (L := L) T E R Bf SR s σ rcv xr s' σ' ⦄ := by + have hxs := hR.at_tcb hs hks + have hspec : doIpcTransfer (Z := Unit) (specPtr snd) epo badge.bv grant (specPtr rcv) σ = + doFaultTransfer (Z := Unit) badge.bv (specPtr snd) (specPtr rcv) (ipcBufOfA true xr) σ := by + unfold doIpcTransfer + rw [nd_bind_one (lookupIpcBuffer_eqA hkr true), nd_bind_one (threadGet_eq _ hks), hf] + unfold ipc.do_ipc_transfer + refine WP.spec_bind (lookup_buf_full s σ hSt htr hr hkr hC hNative true) ?_ + rintro rbw ⟨hrw0, hrwB⟩ + step with obj.Tcb.fault_type.spec as ⟨ft, hft⟩ + have hft0 : ¬ ft = fl4_abi.consts.fault.NullFault := by + intro h + have : L.mem s (tFaultA snd) = 0#u64 := by rw [← hft, h]; unfold fl4_abi.consts.fault.NullFault; rfl + rw [hxs.fault0.1 this] at hf; cases hf + rw [if_neg hft0] + refine WP.spec_mono (do_fault_transfer_relA hO hP s a σ hR hSR hs hr hne hks hkr hf hok + (buf_caseA hrw0 hrwB) hu badge) ?_ + rintro s' ⟨σ', h1, h2⟩ + exact ⟨σ', hspec.trans h1, h2⟩ + +open Fl4SpecA64 in +/-- **FL4's `do_ipc_transfer` refines l4v `do_ipc_transfer`, both branches** (gen 9's `do_ipc_transfer_relA` for a +sender without fault, `do_ipc_transfer_faultA` for a faulted one). -/ +theorem do_ipc_transfer_anyA (hO : SchedOpsEA st T E R Bf SR) (hP : EPlaced T E R Bf) {th : TypedHeapA} {kb : U64} + (hPl : PlaceA th kb) (s : S) (a : A) (σ : State Unit) (hR : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) + (hSt : StateRelA th (L.mem s) σ) (hV : SlotsValidA th (L.mem s)) (hC : BufCover st T Bf s σ) + (hNative : BufNativeA T (L.mem s)) + {snd rcv : U64} (hs : snd ∈ T) (hr : rcv ∈ T) (hne : snd ≠ rcv) (hts : th snd = some .tcb) + (htr : th rcv = some .tcb) {xs xr : Tcb} (hks : σ.kheap (specPtr snd) = some (.tcb xs)) + (hkr : σ.kheap (specPtr rcv) = some (.tcb xr)) + (hu : ∀ rp, ipcBufOfA true xr = some rp → ∀ k, k < 128 → inUserFrame (bufAddr rp k) σ) + (hoff : ∀ y, IsSlotA th y → ¬ EWrite T E R Bf y ∧ ¬ EWrite T E R Bf (core.num.U64.wrapping_add y 8#u64)) + (ep badge : U64) (grant : Bool) + (hcase : (xs.tcbFault = none ∧ (grant = false ∨ ipcBufOfA false xs = none ∨ + unat (dataToMessageInfo (xs.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0)) ∨ + ∃ f, xs.tcbFault = some f ∧ FaultOKA (L := L) s snd (ipcBufOfA true xr) xs f) + (epo : Option ObjRef) : + ipc.do_ipc_transfer st ar s a snd ep badge grant rcv ⦃ s' => ∃ σ', + doIpcTransfer (Z := Unit) (specPtr snd) epo badge.bv grant (specPtr rcv) σ = ({((), σ')}, False) ∧ + XferPostA (L := L) T E R Bf SR s σ rcv xr s' σ' ⦄ := by + rcases hcase with ⟨hf, hcaps⟩ | ⟨f, hf, hok⟩ + · exact do_ipc_transfer_relA hO hP hPl s a σ hR hSR hSt hV hC hNative hs hr hne hts htr hks hkr hf hu hoff ep badge grant + hcaps epo + · exact do_ipc_transfer_faultA hO hP s a σ hR hSR hSt hC hNative hs hr hne htr hks hkr hf hok hu ep badge grant epo + +end Fl4 + +end + +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/EpFootHelpA.lean b/proofs/Fl4ProofsA64/P3/EpFootHelpA.lean new file mode 100644 index 000000000..4d03f5e72 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/EpFootHelpA.lean @@ -0,0 +1,146 @@ +/- +P3 (aarch64, A64-P3 Stage-3 integration, D8/T1 step 2): **the footprint twins of the shared state-word helpers**. + +`set_thread_state_from` / `set_thread_state_rel` / `final_inactive` / `reply_unlink_rel` (`P3/EpCancelA`, +`P3/EpCancelIpcA`, ratified, many users) drop what they write. These siblings add `FpA` (`P3/SchedFootA`): the words +written are endpoint-code words or scheduler words of the entry state. The ratified statements are unchanged. + +* `ssz_updK_tcb`, `ssz_updK_reply`, `ssz_of_epStep`: the SC sizes are kept by the IPC spec steps. +* **`set_thread_state_fromF`**, `set_thread_state_relF`, `final_inactiveF`, **`reply_unlink_relF`**. + +No named premise. +-/ +import Fl4ProofsA64.P3.EpCancelIpcA +import Fl4ProofsA64.P3.SchedFootA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched + +noncomputable section + +section SSz + +theorem ssz_updK_tcb (σ : Fl4SpecA64.State Unit) (p : Fl4SpecA64.ObjRef) (x : Fl4SpecA64.Tcb) : + SSzA σ (updK σ p (.tcb x)) := fun q y n h => by + rw [updK_kheap] at h + by_cases e : q = p + · subst e; rw [Function.update_self] at h; cases h + · rw [Function.update_of_ne e] at h; exact ⟨y, h⟩ + +theorem ssz_updK_reply (σ : Fl4SpecA64.State Unit) (p : Fl4SpecA64.ObjRef) (x : Fl4SpecA64.Reply) : + SSzA σ (updK σ p (.reply x)) := fun q y n h => by + rw [updK_kheap] at h + by_cases e : q = p + · subst e; rw [Function.update_self] at h; cases h + · rw [Function.update_of_ne e] at h; exact ⟨y, h⟩ + +theorem ssz_of_epStep {σ σ' : Fl4SpecA64.State Unit} (h : EpStep σ σ') : SSzA σ σ' := fun p y n hp => by + rcases h.2.2 p with e | ⟨x, y', h1, h2⟩ | ⟨x, y', h1, h2, -⟩ | ⟨x, y', h1, h2, -⟩ + · exact ⟨y, by rw [← e]; exact hp⟩ + · rw [h2] at hp; cases hp + · rw [h2] at hp; cases hp + · rw [h2] at hp; cases hp + +theorem ssz_stsAct (σ : Fl4SpecA64.State Unit) (t : Fl4SpecA64.ObjRef) : SSzA σ (stsAct σ t) := + SSzA.of_kheap (stsAct_kheap σ t) + +end SSz + +section Fl4 +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {T E R : Set U64} + {Bf : Set (U64 × Fl4SpecA64.Word)} {SR : S → Fl4SpecA64.State Unit → Prop} + {Wr : Fl4SpecA64.State Unit → S → U64 → Prop} + +/-- `set_thread_state_from` with its footprint (from the call's own entry state `(s, σ)`). -/ +theorem set_thread_state_fromF (hO : SchedOpsE0A st T E R Bf SR) (hF : SchedFootE0A st T E R Bf SR Wr) + (hP : EPlaced T E R Bf) {s0 : S} + {σ0 : Fl4SpecA64.State Unit} (hSR0 : SR s0 σ0) (s : S) (σ : Fl4SpecA64.State Unit) + (hR : EpStateRel T E R Bf (L.mem s) σ) (hW0 : WritesIn st s0 s (EWrite T E R Bf)) (hS0 : EpStep σ0 σ) + {t : U64} (ht : t ∈ T) {x : Fl4SpecA64.Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) + (ts : Fl4SpecA64.ThreadState) (hts : tsCode ts < 3) (code : U64) (hcode : code.val = tsCode ts) + (hNB : NoBlockE σ0 (updK σ (specPtr t) (.tcb { x with tcbState := ts }))) : + sched.set_thread_state st s t code ⦃ s' => + EpStateRel T E R Bf (L.mem s') (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := ts })) (specPtr t)) ∧ + SR s' (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := ts })) (specPtr t)) ∧ + FpA st T E R Bf Wr σ s s' ⦄ := by + unfold sched.set_thread_state + step with obj.Tcb.set_state.spec as ⟨s1, hs1⟩ + subst hs1 + obtain ⟨hR1, hW1, hS1⟩ := write_state_rel hP s σ hR ht hk hq ts hts code hcode + have hSR1 := hO.stable _ _ _ _ hSR0 (WritesIn.ew hW0 hW1) (hS0.trans hS1) hNB + refine WP.spec_mono (WP.spec_and (hO.scheduleTcb _ _ t hR1 hSR1 ht) (hF.scheduleTcb _ _ t hR1 hSR1 ht)) ?_ + rintro s2 ⟨⟨hf, hS⟩, hw⟩ + exact ⟨hR1.frame hP hf (KView.stsAct' _ _), hS, + FpA.trans0 hF (FpA.of_ew hW1) (ssz_updK_tcb σ _ _) (FpA.of_wr hw)⟩ + +theorem set_thread_state_relF (hO : SchedOpsEA st T E R Bf SR) (hF : SchedFootEA st T E R Bf SR Wr) + (hP : EPlaced T E R Bf) (s : S) + (σ : Fl4SpecA64.State Unit) (hR : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) {t : U64} (ht : t ∈ T) + {x : Fl4SpecA64.Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) + (ts : Fl4SpecA64.ThreadState) (hts : tsCode ts < 3) (code : U64) (hcode : code.val = tsCode ts) + (hNB : NoBlockE σ (updK σ (specPtr t) (.tcb { x with tcbState := ts }))) : + sched.set_thread_state st s t code ⦃ s' => + EpStateRel T E R Bf (L.mem s') (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := ts })) (specPtr t)) ∧ + SR s' (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := ts })) (specPtr t)) ∧ + FpA st T E R Bf Wr σ s s' ⦄ := + set_thread_state_fromF hO.to0 hF.to0 hP hSR s σ hR (WritesIn.refl _ _) (EpStep.refl _) ht hk hq ts hts code hcode hNB + +theorem final_inactiveF (hO : SchedOpsE0A st T E R Bf SR) (hF : SchedFootE0A st T E R Bf SR Wr) + (hP : EPlaced T E R Bf) {s0 : S} + {σ0 : Fl4SpecA64.State Unit} (hSR0 : SR s0 σ0) (s : S) (σ : Fl4SpecA64.State Unit) + (hR : EpStateRel T E R Bf (L.mem s) σ) (hW0 : WritesIn st s0 s (EWrite T E R Bf)) (hS0 : EpStep σ0 σ) + {t : U64} (ht : t ∈ T) {x0 x : Fl4SpecA64.Tcb} (hk0 : σ0.kheap (specPtr t) = some (.tcb x0)) + (hk : σ.kheap (specPtr t) = some (.tcb x)) (hO0 : OnlyT σ0 σ (specPtr t)) + (hblk : Fl4SpecA64.runnable x0.tcbState = false ∧ x0.tcbState ≠ .idleThreadState) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) : + sched.set_thread_state st s t sched.TS_INACTIVE ⦃ s' => + EpStateRel T E R Bf (L.mem s') (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := .inactive })) (specPtr t)) ∧ + SR s' (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := .inactive })) (specPtr t)) ∧ + FpA st T E R Bf Wr σ s s' ⦄ := + set_thread_state_fromF hO hF hP hSR0 s σ hR hW0 hS0 ht hk hq .inactive (by decide) sched.TS_INACTIVE + (by unfold sched.TS_INACTIVE; rfl) + (NoBlockE.only hk0 (by rw [updK_kheap, Function.update_self]) (hO0.updP _) (Or.inr (Or.inr ⟨rfl, hblk⟩))) + +/-- `reply_unlink_rel` with its footprint (from the call's entry state `(s2, σ2)`). -/ +theorem reply_unlink_relF (hO : SchedOpsE0A st T E R Bf SR) (hF : SchedFootE0A st T E R Bf SR Wr) + (hP : EPlaced T E R Bf) {s0 : S} + {σ0 : Fl4SpecA64.State Unit} (hSR0 : SR s0 σ0) {s2 : S} {σ2 : Fl4SpecA64.State Unit} + (hR2 : EpStateRel T E R Bf (L.mem s2) σ2) (hW02 : WritesIn st s0 s2 (EWrite T E R Bf)) (hS02 : EpStep σ0 σ2) + {t : U64} (ht : t ∈ T) {x0 x1 : Fl4SpecA64.Tcb} (hk0 : σ0.kheap (specPtr t) = some (.tcb x0)) + (hk2 : σ2.kheap (specPtr t) = some (.tcb x1)) (hO2 : OnlyT σ0 σ2 (specPtr t)) + (hblk : Fl4SpecA64.runnable x0.tcbState = false ∧ x0.tcbState ≠ .idleThreadState) + (hq2 : ∀ e ∈ E, ∀ z, σ2.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) + {i : U64} (hiR : i ∈ R) {xr : Fl4SpecA64.Reply} (hkr2 : σ2.kheap (specPtr i) = some (.reply xr)) : + ipc.reply_unlink st s2 i t ⦃ s3 => + EpStateRel T E R Bf (L.mem s3) (stsAct (updK (updK σ2 (specPtr i) (.reply { xr with replyTcb := none })) + (specPtr t) (.tcb { x1 with tcbState := .inactive })) (specPtr t)) ∧ + SR s3 (stsAct (updK (updK σ2 (specPtr i) (.reply { xr with replyTcb := none })) + (specPtr t) (.tcb { x1 with tcbState := .inactive })) (specPtr t)) ∧ + FpA st T E R Bf Wr σ2 s2 s3 ⦄ := by + have hit : specPtr i ≠ specPtr t := by intro h; rw [h, hk2] at hkr2; cases hkr2 + have hal := (a_rtcb hP hiR).1 + have hag := agree_wr (L := L) (s := s2) (v := 0#u64) hal + have hR3 : EpStateRel T E R Bf (L.mem (L.wr s2 (rTcbA i) 0#u64)) + (updK σ2 (specPtr i) (.reply { xr with replyTcb := none })) := + hR2.upd_reply hP hiR _ (hag.mono fun b hb => by rw [rTcbA_val]; exact hb.2) (by rw [rd_wr_eq hal, optRef_zero]) + have hW3 : WritesIn st s2 (L.wr s2 (rTcbA i) 0#u64) (EWrite T E R Bf) := + (writesIn_wr hal).mono fun b hb => by subst hb; exact Or.inr (Or.inl ⟨i, hiR, rTcbA_val i⟩) + have hk3 : (updK σ2 (specPtr i) (.reply { xr with replyTcb := none })).kheap (specPtr t) = some (.tcb x1) := by + rw [updK_kheap, Function.update_of_ne hit.symm, hk2] + have hq3 := notq_updR (E := E) hq2 i { xr with replyTcb := none } + unfold ipc.reply_unlink + step with obj.Reply.set_tcb.spec as ⟨s3, hs3⟩ + subst hs3 + refine WP.spec_mono (final_inactiveF hO hF hP hSR0 _ _ hR3 (WritesIn.ew hW02 hW3) + (hS02.trans (EpStep.reply hkr2 none)) ht hk0 hk3 (hO2.updO _ fun _ h => by cases h) hblk hq3) ?_ + rintro s4 ⟨h1, h1', h2⟩ + exact ⟨h1, h1', FpA.trans0 hF (FpA.of_ew hW3) (ssz_updK_reply σ2 _ _) h2⟩ + +end Fl4 + +end + +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/EpMrsA.lean b/proofs/Fl4ProofsA64/P3/EpMrsA.lean new file mode 100644 index 000000000..8b93cf25e --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/EpMrsA.lean @@ -0,0 +1,162 @@ +/- +P3 (aarch64, A64-P3-IPC gen 2): **the inline message registers** (`X2..X5`): the spec's register file after a +message (x86 `P3/EpMrs`'s register half) and FL4's `set_mr` on them. + +* Spec side (x86's, at l4v AArch64's `msgRegisters = [X2 .. X5]`): `mrIdx i` (`msgRegisters[i]`, default `X5` as in + `FrontEndExtraA`'s `RegViewA`), `mrRegs ctx ms` (l4v `set_mrs`'s `new_regs`), `mrRegs_mr` / `mrRegs_other`, the snoc + lemmas (`mrRegs_snoc_lt` / `_ge`: one more word changes one register or none), `mrPairs` (the buffer words + `set_mrs` stores, `zipWithM_x store_word_offs [5 ..] (drop 4 ms)`) with `mrPairs_snoc_lt` / `_ge`. +* FL4 side: **`set_mr_reg`**: for `i < 4`, `msg::set_mr` writes register `regIdxA (mrIdx i)` (`msg_register_regIdxA`) + and returns `i + 1`; **`set_mr_rel`**: that write keeps `EpStateRel` against the spec TCB with `setReg (mrIdx i)` + (`write_reg_rel`). +x86's `setMrs_eq` / `mrMem` (the buffer half: `storeWordOffs`, `inUserFrame`) come with the A64 port of `EpBytes` / +`EpBufSpec` (gen 3). No named premise. No `Fl4SpecA64` edit. +-/ +import Fl4ProofsA64.P3.EpRegsA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp + +noncomputable section + +section Spec +open Fl4SpecA64 Classical + +/-- The `i`-th message register (l4v AArch64 `msgRegisters`, `[X2 .. X5]`; x86 `mrIdx`). -/ +def mrIdx (i : Nat) : Register := msgRegisters.getD i .x5 + +theorem mrIdx_mem (i : Nat) : mrIdx i ∈ msg_registers := by + unfold mrIdx msg_registers + rw [List.getD_eq_getElem?_getD] + cases h : msgRegisters[i]? with + | none => simp [msgRegisters, Register.x2ToX5] + | some r => rw [Option.getD_some]; exact List.mem_of_getElem? h + +/-- The receiver's registers after the message `ms` (l4v `set_mrs`). -/ +def mrRegs (ctx : Register → Word) (ms : List Word) : Register → Word := fun reg => + if reg ∈ msg_registers.take ms.length then the ms[theIndex msg_registers reg]? else ctx reg + +/-- The buffer words `set_mrs` stores. -/ +def mrPairs (ms : List Word) : List (Nat × Word) := (List.range' 5 116).zip (ms.drop 4) + +/-- The receiver TCB with the registers `regs`. -/ +def withRegs (x : Tcb) (regs : Register → Word) : Tcb := + { x with tcbArch := archTcbSetRegisters regs x.tcbArch } + +theorem theIndex_mr : ∀ j, j < 4 → theIndex msg_registers (mrIdx j) = j := by + intro j hj + rcases (show j = 0 ∨ j = 1 ∨ j = 2 ∨ j = 3 by omega) with rfl | rfl | rfl | rfl <;> rfl + +theorem mr_cases (reg : Register) : (∃ j, j < 4 ∧ reg = mrIdx j) ∨ reg ∉ msg_registers := by + cases reg + all_goals first + | (right; decide) + | (left; exact ⟨0, by omega, rfl⟩) + | (left; exact ⟨1, by omega, rfl⟩) + | (left; exact ⟨2, by omega, rfl⟩) + | (left; exact ⟨3, by omega, rfl⟩) + +theorem mrIdx_mem_take {j k : Nat} (hj : j < 4) : mrIdx j ∈ msg_registers.take k ↔ j < k := by + rcases (show j = 0 ∨ j = 1 ∨ j = 2 ∨ j = 3 by omega) with rfl | rfl | rfl | rfl <;> + rcases (show k = 0 ∨ k = 1 ∨ k = 2 ∨ k = 3 ∨ 4 ≤ k by omega) with rfl | rfl | rfl | rfl | hk <;> + first | decide | (rw [List.take_of_length_le (by simp [msg_registers, msgRegisters, Register.x2ToX5]; omega)]; simp [mrIdx, + msg_registers, msgRegisters, Register.x2ToX5]; omega) + +theorem mrRegs_mr (ctx : Register → Word) (ms : List Word) {j : Nat} (hj : j < 4) : + mrRegs ctx ms (mrIdx j) = if j < ms.length then ms.getD j 0 else ctx (mrIdx j) := by + unfold mrRegs + rw [theIndex_mr j hj] + by_cases h : j < ms.length + · rw [if_pos ((mrIdx_mem_take hj).2 h), if_pos h, List.getElem?_eq_getElem h] + simp [the, List.getD_eq_getElem?_getD, List.getElem?_eq_getElem h] + · rw [if_neg (fun h' => h ((mrIdx_mem_take hj).1 h')), if_neg h] + +theorem mrRegs_other (ctx : Register → Word) (ms : List Word) {reg : Register} (h : reg ∉ msg_registers) : + mrRegs ctx ms reg = ctx reg := by + unfold mrRegs + rw [if_neg (fun h' => h (List.mem_of_mem_take h'))] + +/-- Appending a word to a message of fewer than four words: one more register. -/ +theorem mrRegs_snoc_lt (ctx : Register → Word) (ms : List Word) (v : Word) (h : ms.length < 4) : + mrRegs ctx (ms ++ [v]) = Function.update (mrRegs ctx ms) (mrIdx ms.length) v := by + funext reg + rcases mr_cases reg with ⟨j, hj, rfl⟩ | hr + · rw [mrRegs_mr _ _ hj] + by_cases e : j = ms.length + · subst e; simp + · rw [Function.update_of_ne (fun h' => e (by + have := congrArg (theIndex msg_registers) h'; rwa [theIndex_mr _ hj, theIndex_mr _ h] at this)), + mrRegs_mr _ _ hj] + simp only [List.length_append, List.length_singleton] + by_cases hl : j < ms.length + · rw [if_pos (by omega), if_pos hl, List.getD_append _ _ _ _ hl] + · rw [if_neg (by omega), if_neg hl] + · rw [mrRegs_other _ _ hr, Function.update_of_ne (fun h' => hr (by rw [h']; exact mrIdx_mem _)), + mrRegs_other _ _ hr] + +/-- Appending a word to a message of at least four words: the registers stay. -/ +theorem mrRegs_snoc_ge (ctx : Register → Word) (ms : List Word) (v : Word) (h : 4 ≤ ms.length) : + mrRegs ctx (ms ++ [v]) = mrRegs ctx ms := by + funext reg + rcases mr_cases reg with ⟨j, hj, rfl⟩ | hr + · rw [mrRegs_mr _ _ hj, mrRegs_mr _ _ hj, List.getD_append _ _ _ _ (by omega)] + simp only [List.length_append, List.length_singleton] + rw [if_pos (by omega), if_pos (by omega)] + · rw [mrRegs_other _ _ hr, mrRegs_other _ _ hr] + +theorem mrPairs_snoc_lt (ms : List Word) (v : Word) (h : ms.length < 4) : mrPairs (ms ++ [v]) = mrPairs ms := by + unfold mrPairs + rw [List.drop_eq_nil_of_le (by simp; omega), List.drop_eq_nil_of_le (by omega)] + +theorem zip_range'_snoc {α : Type} (v : α) : ∀ (l : List α) (a n : Nat), l.length < n → + (List.range' a n).zip (l ++ [v]) = (List.range' a n).zip l ++ [(a + l.length, v)] + | [], a, n, h => by + obtain ⟨k, rfl⟩ : ∃ k, n = k + 1 := ⟨n - 1, by omega⟩ + simp [List.range'_succ] + | x :: l, a, n, h => by + obtain ⟨k, rfl⟩ : ∃ k, n = k + 1 := ⟨n - 1, by omega⟩ + simp only [List.range'_succ, List.cons_append, List.zip_cons_cons, List.length_cons] + rw [zip_range'_snoc v l (a + 1) k (by simp at h; omega)] + simp; omega + +theorem mrPairs_snoc_ge (ms : List Word) (v : Word) (h : 4 ≤ ms.length) (h' : ms.length < 120) : + mrPairs (ms ++ [v]) = mrPairs ms ++ [(ms.length + 1, v)] := by + unfold mrPairs + rw [List.drop_append_of_le_length h, zip_range'_snoc v _ 5 116 (by simp; omega)] + simp; omega + +end Spec + +/-! ## FL4's `set_mr` on a message register -/ + +section Fl4 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **`set_mr` of an inline message register** (`i < 4`): one register word, `regIdxA (mrIdx i)`. -/ +theorem set_mr_reg (s : S) (t buf i v : U64) (hi : i.val < 4) : + msg.set_mr st s t buf i v = + ok (core.num.U64.wrapping_add i 1#u64, L.wr s (regA t (regIdxA (mrIdx i.val))) v) := by + have hN : msg.N_MSG_REGISTERS.val = 4 := by unfold msg.N_MSG_REGISTERS; rfl + have h4 : i < msg.N_MSG_REGISTERS := by scalar_tac + unfold msg.set_mr + rw [if_pos h4, msg_register_regIdxA hi, bind_ok] + unfold obj.Tcb.set_reg + simp only [lift, bind_ok, L.write_eq, regA, mrIdx] + +/-- **`set_mr` of an inline message register keeps the endpoint relation**, against the spec TCB with that register +set (`setReg (mrIdx i)`). -/ +theorem set_mr_rel {T E R : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} (hP : EPlaced T E R Bf) (s : S) + (σ : Fl4SpecA64.State Unit) (hR : EpStateRel T E R Bf (L.mem s) σ) {t : U64} (ht : t ∈ T) + {x : Fl4SpecA64.Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) (buf i v : U64) (hi : i.val < 4) : + msg.set_mr st s t buf i v ⦃ r => r.1 = core.num.U64.wrapping_add i 1#u64 ∧ + EpStateRel T E R Bf (L.mem r.2) (updK σ (specPtr t) (.tcb (setReg x (mrIdx i.val) v.bv))) ∧ + WritesIn st s r.2 (EWrite T E R Bf) ∧ EpStep σ (updK σ (specPtr t) (.tcb (setReg x (mrIdx i.val) v.bv))) ⦄ := by + rw [set_mr_reg s t buf i v hi, WP.spec_ok] + exact ⟨rfl, write_reg_rel hP s σ hR ht hk (mrIdx i.val) v⟩ + +end Fl4 + +end + +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/EpQueueA.lean b/proofs/Fl4ProofsA64/P3/EpQueueA.lean new file mode 100644 index 000000000..406b8d394 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/EpQueueA.lean @@ -0,0 +1,139 @@ +/- +P3 (aarch64, A64-P3-IPC gen 2, ported from x86 `P3/EpQueue`), endpoints: `ep_append` and `ep_dequeue` with their +frames. + +A64 P1 (`QueueEp`, the x86 text) proves that they refine the priority-ordered insertion and `List.erase` on the +queue; here the same proofs with what they leave alone: everything but the endpoint's two words and the `Ep` links +of the TCBs involved (`ep_append_full`, `ep_dequeue_full`). FL4's AArch64 `ep_append` / `ep_dequeue` are the x86 +text (extraction diffed). No change from x86 beyond the renaming. +-/ +import Fl4ProofsA64.P3.EpWriteA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif + +noncomputable section + +section +variable {S : Type _} {st : machine.Store S} [L : LawfulStore st] {T E R : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + +omit [L : LawfulStore st] in +/-- What the queue operations on `e` leave alone, for the TCBs `W` whose +links they write. -/ +def QAgree (T : Set U64) (e : U64) (W : List U64) (m m' : U64 → U64) : Prop := + Agree m m' (fun b => ¬ EW e b ∧ ¬ ∃ w ∈ W, TL w b) + +theorem framed_agree (hP : EPlaced T E R Bf) {W : List U64} (hW : ∀ w ∈ W, w ∈ T) {s s1 : S} + (hf : Framed st .Ep W s s1) : + Agree (L.mem s) (L.mem s1) (fun b => ¬ ∃ w ∈ W, TL w b) ∧ WritesIn st s s1 (fun b => ∃ w ∈ W, TL w b) := by + have hWa : ∀ w ∈ W, TcbAddr w := fun w hw => hP.tcb w (hW w hw) + have key : ∀ b : U64, Aligned b → (¬ ∃ w ∈ W, TL w b) → L.mem s1 b = L.mem s b := by + intro b hb hn + refine hf.2.2 b hb fun w hw => ⟨fun e => hn ⟨w, hw, ?_⟩, fun e => hn ⟨w, hw, ?_⟩⟩ + · subst e; exact (a_next (hWa w hw)).2 + · subst e; exact (a_prev (hWa w hw)).2 + exact ⟨key, hf.1, hf.2.1, key⟩ + +/-- `set_queue` on the endpoint `e`. -/ +theorem set_queue_agree (hP : EPlaced T E R Bf) {e : U64} (he : e ∈ E) (s : S) (h v : U64) : + let s' := L.wr (L.wr s (epHeadA e) h) (epTSA e) v + Agree (L.mem s) (L.mem s') (fun b => ¬ EW e b) ∧ WritesIn st s s' (EW e) := by + intro s' + obtain ⟨ah, at'⟩ := ep_aligned (hP.epPlaced he) + have key : ∀ b : U64, Aligned b → ¬ EW e b → L.mem s' b = L.mem s b := by + intro b hb hn + unfold EW at hn + show L.mem (L.wr (L.wr s (epHeadA e) h) (epTSA e) v) b = _ + rw [L.mem_wr_ne at' hb (by rw [val_ts hP he]; omega), L.mem_wr_ne ah hb (by rw [val_head]; omega)] + exact ⟨key, by simp only [s', LawfulStore.ksb_wr], by simp only [s', LawfulStore.cpu_wr], key⟩ + +theorem qagree_of (hP : EPlaced T E R Bf) {e : U64} (he : e ∈ E) {W : List U64} (hW : ∀ w ∈ W, w ∈ T) + {s s1 : S} (hf : Framed st .Ep W s s1) (h v : U64) : + QAgree T e W (L.mem s) (L.mem (L.wr (L.wr s1 (epHeadA e) h) (epTSA e) v)) ∧ + WritesIn st s (L.wr (L.wr s1 (epHeadA e) h) (epTSA e) v) (EWrite T E R Bf) := by + obtain ⟨f1, f2⟩ := framed_agree hP hW hf + obtain ⟨g1, g2⟩ := set_queue_agree (st := st) hP he s1 h v + refine ⟨fun b hb ⟨h1, h2⟩ => (g1 b hb h1).trans (f1 b hb h2), (f2.trans g2).mono fun b hb => ?_⟩ + rcases hb with ⟨w, hw, hl⟩ | hb + · exact Or.inr (Or.inr (Or.inl ⟨w, hW w hw, Or.inr hl⟩)) + · exact Or.inl ⟨e, he, hb⟩ + +/-- `ep_dequeue` of a member, with its frame. -/ +theorem ep_dequeue_full (hP : EPlaced T E R Bf) (s : S) {e : U64} (he : e ∈ E) (t : obj.Tcb) (xs : List U64) + (h : QueueRel (L.mem s) T .Ep (qOf (L.mem s) e) xs) (ht : t ∈ xs) : + endpoint.ep_dequeue st s e t ⦃ s' => + QueueRel (L.mem s') T .Ep (qOf (L.mem s') e) (xs.erase t) ∧ + stOf (L.mem s') e = (if xs.erase t = [] then 0#u64 else stOf (L.mem s) e) ∧ + QAgree T e xs (L.mem s) (L.mem s') ∧ WritesIn st s s' (EWrite T E R Bf) ∧ + L.mem s' (nextA .Ep t) = 0#u64 ∧ L.mem s' (prevA .Ep t) = 0#u64 ⦄ := by + have hE := hP.epPlaced he + have hT := hP.tcb + unfold endpoint.ep_dequeue + step* + subst q_post + refine WP.spec_bind (tcbqueue.remove.rel s .Ep _ t xs hT h ht) ?_ + rintro ⟨q1, s1⟩ h1 + simp only [WP.uncurry'_pair] at h1 + obtain ⟨hr1, hf, hn0, hp0⟩ := h1 + have hW : ∀ w ∈ xs, w ∈ T := h.mem + have hta : TcbAddr t := hP.tcb t (h.mem t ht) + have hlinks : ∀ (s2 : S) (hv : U64), s2 = L.wr (L.wr s1 (epHeadA e) q1.head) (epTSA e) hv → + L.mem s2 (nextA .Ep t) = 0#u64 ∧ L.mem s2 (prevA .Ep t) = 0#u64 := by + intro s2 hv hs2 + obtain ⟨g1, -⟩ := set_queue_agree (L := L) hP he s1 q1.head hv + rw [← hs2] at g1 + have ne1 : ¬ EW e (nextA .Ep t) := by + unfold EW; rw [val_next hta]; have := hP.et e he t (h.mem t ht); omega + have ne2 : ¬ EW e (prevA .Ep t) := by + unfold EW; rw [val_prev hta]; have := hP.et e he t (h.mem t ht); omega + exact ⟨(g1 _ (a_next hta).1 ne1).trans hn0, (g1 _ (a_prev hta).1 ne2).trans hp0⟩ + step* + · subst s'_post + have hemp : xs.erase t = [] := (hr1.empty_iff hT).1 (by simpa [b_post] using ‹b = true›) + obtain ⟨h1, h2⟩ := ep_set_queue (state := obj.EP_IDLE) hT hE hr1 + obtain ⟨q1', q2'⟩ := qagree_of hP he hW hf q1.head ((q1.end &&& ~~~3#u64) ||| (obj.EP_IDLE &&& 3#u64)) + refine ⟨h1, ?_, q1', q2', hlinks _ _ rfl⟩ + rw [← epState_eq, h2]; simp only [hemp, ↓reduceIte] + unfold obj.EP_IDLE + exact UScalar.eq_of_val_eq (by simp) + · subst s'_post + have hne : xs.erase t ≠ [] := by + rw [Ne, ← hr1.empty_iff hT]; simpa [b_post] using ‹¬b = true› + obtain ⟨h1, h2⟩ := ep_set_queue (state := st) hT hE hr1 + obtain ⟨q1', q2'⟩ := qagree_of hP he hW hf q1.head ((q1.end &&& ~~~3#u64) ||| (st &&& 3#u64)) + refine ⟨h1, ?_, q1', q2', hlinks _ _ rfl⟩ + rw [← epState_eq, h2]; simp only [hne, ↓reduceIte]; rw [st_post] + apply eq_of_bv + simp only [LawfulStore.epState, UScalar.bv_and, BitVec.and_assoc, BitVec.and_self, stOf] + +/-- `ep_append`, with its frame. -/ +theorem ep_append_full (hP : EPlaced T E R Bf) (s : S) {e : U64} (he : e ∈ E) (t : obj.Tcb) (state : U64) + (xs : List U64) (h : QueueRel (L.mem s) T .Ep (qOf (L.mem s) e) xs) (ht : t ∈ T) (hnt : t ∉ xs) : + endpoint.ep_append st s e t state ⦃ s' => + QueueRel (L.mem s') T .Ep (qOf (L.mem s') e) + (insPrio (fun y => L.mem s (prioA y)) (L.mem s (prioA t)) t xs) ∧ + stOf (L.mem s') e = state &&& 3#u64 ∧ QAgree T e (t :: xs) (L.mem s) (L.mem s') ∧ + WritesIn st s s' (EWrite T E R Bf) ⦄ := by + have hE := hP.epPlaced he + have hT := hP.tcb + unfold endpoint.ep_append + step* + subst q_post + refine WP.spec_bind (tcbqueue.append_by_prio.rel s .Ep _ t xs hT h ht hnt) ?_ + rintro ⟨q1, s1⟩ h1 + simp only [WP.uncurry'_pair] at h1 + obtain ⟨hr1, hf⟩ := h1 + have hW : ∀ w ∈ t :: xs, w ∈ T := by + intro w hw; rcases List.mem_cons.1 hw with rfl | hw; exacts [ht, h.mem w hw] + step* + subst_vars + obtain ⟨h1, h2⟩ := ep_set_queue (state := state) hT hE hr1 + obtain ⟨q1', q2'⟩ := qagree_of hP he hW hf q1.head ((q1.end &&& ~~~3#u64) ||| (state &&& 3#u64)) + exact ⟨h1, by rw [← epState_eq, h2], q1', q2'⟩ + +end + +end + +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/EpRecvBlkA.lean b/proofs/Fl4ProofsA64/P3/EpRecvBlkA.lean new file mode 100644 index 000000000..b0a7a898f --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/EpRecvBlkA.lean @@ -0,0 +1,284 @@ +/- +P3 (aarch64, A64-P3-IPC gen 13): **`receive_ipc`'s blocking paths refine l4v `receive_ipc_blocked`** (`Z = Unit`; +x86 `P3/EpRecv` `receive_ipc_block` / `receive_ipc_poll`): FL4's `wait_for_sender` on an idle endpoint or one with +waiting receivers. + +* **`block_recv_writesA`** (the three TCB words of a blocking receive: state, block object, reply object) and + **`block_recv_relA`**: they relate to the spec's TCB `BlockedOnReceive e reply ⦇receiverCanGrant := false⦈` + (`TcbRelE.restateA`, the reply word against `reply` by `optRef`), `SR` kept by `SchedOpsEA.block` (the receiver, the + current thread, blocks: `SndRunA`, `RelIfBound` by `SchedOpsEA.rd`); +* **`recv_append_fromA`**: `ep_append_relA` with `isRecv = true` from any later related state where the endpoint, + the priorities and the receiver's state / block words are the entry's or the written ones (sortedness carried by + `sorted_transferA`); +* **`wait_for_sender_blockA`**: the three words, `schedule_tcb` ↔ `set_thread_state_act`, the reply object's TCB word + if any (`set_reply_obj_ref reply_tcb_update r (Some t)`, one `SchedOpsEA.stable` step), then `ep_append`: one spec + outcome of `receive_ipc_blocked True`, related; +* **`wait_for_sender_pollA`**: a polling receive writes the badge register 0 (`do_nbrecv_failed_transfer`, + `write_reg_rel`, one `stable` step). + +Case selectors / placement (no named premise): `EPlaced`, `SndRunA` (the receiver is the running thread), `CanBlockA +true` (the endpoint is idle or has receivers), `hsort` (P1 `PrioSorted`), the reply argument (`optRef rv = reply`, +`rv ∈ R` if any). +-/ +import Fl4ProofsA64.P3.EpSendBlkA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {T E R : Set U64} + {Bf : Set (U64 × Fl4SpecA64.Word)} {SR : S → Fl4SpecA64.State Unit → Prop} + {Wr : Fl4SpecA64.State Unit → S → U64 → Prop} + +/-- The payload of a blocking receive (`receive_ipc_blocked`: `⦇receiverCanGrant = False⦈`). -/ +abbrev rcvPl0A : Fl4SpecA64.ReceiverPayload := { receiverCanGrant := false } + +/-- The three writes of a blocking receive. -/ +theorem block_recv_writesA (hP : EPlaced T E R Bf) (s : S) {t : U64} (ht : t ∈ T) (e rv : U64) : + let s3 := L.wr (L.wr (L.wr s (stA t) sched.TS_BLOCKED_ON_RECV) (tBlockA t) e) (tReplyA t) rv + Agree (L.mem s) (L.mem s3) (fun b => b.val ≠ t.val + 3328 ∧ b.val ≠ t.val + 3336 ∧ b.val ≠ t.val + 3360) ∧ + L.mem s3 (stA t) = sched.TS_BLOCKED_ON_RECV ∧ L.mem s3 (tBlockA t) = e ∧ L.mem s3 (tReplyA t) = rv ∧ + WritesIn st s s3 (EWrite T E R Bf) := by + intro s3 + have hta := hP.tcb t ht + have a1 := (a_st hta).1; have a2 := (a_blk hta).1; have a3 := (a_reply hta).1 + have v1 := val_st hta; have v2 := val_blk hta; have v3 := val_reply hta + have g1 := agree_wr2A (L := L) (s := s) (v := sched.TS_BLOCKED_ON_RECV) (w := e) a1 a2 + have g2 := agree_wr (L := L) (s := L.wr (L.wr s (stA t) sched.TS_BLOCKED_ON_RECV) (tBlockA t) e) (v := rv) a3 + refine ⟨(g1.mono fun b hb => ?_).trans (g2.mono fun b hb => ?_), ?_, ?_, ?_, ?_⟩ + · rw [v1, v2]; omega + · rw [v3]; omega + · show L.mem (L.wr (L.wr (L.wr s _ _) _ _) _ _) _ = _ + rw [L.mem_wr_ne a3 a1 (by omega), L.mem_wr_ne a2 a1 (by omega), L.mem_wr_same a1] + · show L.mem (L.wr (L.wr (L.wr s _ _) _ _) _ _) _ = _ + rw [L.mem_wr_ne a3 a2 (by omega), L.mem_wr_same a2] + · exact L.mem_wr_same a3 + · refine ((wr_ewrite (E := E) (R := R) (Bf := Bf) ht (twf_ofA 3328 v1 (by unfold TWOffN; omega)) a1 s _).trans + ((wr_ewrite (E := E) (R := R) (Bf := Bf) ht (twf_ofA 3336 v2 (by unfold TWOffN; omega)) a2 _ _).trans + (wr_ewrite (E := E) (R := R) (Bf := Bf) ht (twf_ofA 3360 v3 (by unfold TWOffN; omega)) a3 _ _))).mono + fun b hb => ?_ + rcases hb with hb | hb | hb <;> exact hb + +/-- The blocking receive's three TCB words: related to the spec's TCB `BlockedOnReceive e rep rp`, and `SR` kept by +`SchedOpsEA.block` (the receiver, the current thread, blocks; `RelIfBound` by `SchedOpsEA.rd`). -/ +theorem block_recv_relA (hO : SchedOpsEA st T E R Bf SR) (hP : EPlaced T E R Bf) (s : S) + (σ : Fl4SpecA64.State Unit) (hR : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) + {t : U64} (ht : t ∈ T) {xt : Fl4SpecA64.Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hsr : SndRunA σ (specPtr t) xt) (e rv : U64) {rep : Option Fl4SpecA64.ObjRef} (hrv : optRef rv = rep) + (hrR : rep ≠ none → rv ∈ R) (rp : Fl4SpecA64.ReceiverPayload) : + EpStateRel T E R Bf (L.mem (L.wr (L.wr (L.wr s (stA t) sched.TS_BLOCKED_ON_RECV) (tBlockA t) e) (tReplyA t) rv)) + (updK σ (specPtr t) (.tcb { xt with tcbState := .blockedOnReceive (specPtr e) rep rp })) ∧ + SR (L.wr (L.wr (L.wr s (stA t) sched.TS_BLOCKED_ON_RECV) (tBlockA t) e) (tReplyA t) rv) + (updK σ (specPtr t) (.tcb { xt with tcbState := .blockedOnReceive (specPtr e) rep rp })) := by + have hxt := hR.at_tcb ht hkt + have hta := hP.tcb t ht + obtain ⟨hrun, hNQ, hsane, hni⟩ := hsr + have hq := not_queued_codeA hR ht hkt (runnable_codeA hrun) + have hRB : RelIfBound σ (specPtr t) := hO.rd _ _ _ _ hSR hkt (Or.inl hrun) + obtain ⟨hA3, r1, r2, r3, hW3⟩ := block_recv_writesA (L := L) (E := E) (R := R) (Bf := Bf) hP s ht e rv + have v1 := val_st hta; have v2 := val_blk hta; have v3 := val_reply hta + refine ⟨?_, hO.block _ _ _ _ t hSR hNQ hRB hsane hni hW3 (EpStep.tcb hkt _ xt.tcbArch) + fun p x' y' h1 h2 hp => ?_⟩ + · refine hR.upd_tcb hP ht _ (hA3.mono fun b hb => ?_) (Or.inl hq) ?_ + · have n1 : b.val ≠ (stA t).val := fun h => hb.2 (TF.of_val (a_st hta).2 h) + have n2 : b.val ≠ (tBlockA t).val := fun h => hb.2 (TF.of_val (a_blk hta).2 h) + have n3 : b.val ≠ (tReplyA t).val := fun h => hb.2 (TF.of_val (a_reply hta).2 h) + omega + refine hxt.restateA hta (hA3.mono fun b hb => ?_) rfl rfl rfl rfl ?_ ?_ ?_ ?_ ?_ + · have := hb.2; omega + · rw [r1]; unfold sched.TS_BLOCKED_ON_RECV; rfl + · intro e' h + simp only [Fl4SpecA64.epBlocked, Option.some.injEq] at h + subst h; rw [r2] + · intro e' d'' h; cases h + · intro e' r d'' h + simp only [Fl4SpecA64.ThreadState.blockedOnReceive.injEq] at h + obtain ⟨-, rfl, -⟩ := h + exact ⟨by rw [r3, hrv], fun h => by rw [r3]; exact hrR h⟩ + · intro r h; cases h + · rw [updK_kheap, Function.update_of_ne hp, h1] at h2 + injection h2 with h2; injection h2 with h2; subst h2; exact ⟨rfl, id⟩ + +/-- **`ep_append` of a blocking receiver** from a later related state (`ep_append_relA` with `isRecv = true`; the +queue's sortedness carried from the entry state by `sorted_transferA`). -/ +theorem recv_append_fromA (hO : SchedOpsEA st T E R Bf SR) (hP : EPlaced T E R Bf) {s s5 : S} + {σ σ5 : Fl4SpecA64.State Unit} (hR : EpStateRel T E R Bf (L.mem s) σ) {t : U64} (ht : t ∈ T) {e : U64} + (he : e ∈ E) {x : Fl4SpecA64.Endpoint} (hk : σ.kheap (specPtr e) = some (.endpoint x)) (hc : CanBlockA true x) + (htq : specPtr t ∉ x.epQueue) + (hsort : ∀ xs, QueueRel (L.mem s) T .Ep (qOf (L.mem s) e) xs → PrioSorted (fun y => L.mem s (prioA y)) xs) + (hR5 : EpStateRel T E R Bf (L.mem s5) σ5) (hS5 : SR s5 σ5) {x5 : Fl4SpecA64.Tcb} + (hkt5 : σ5.kheap (specPtr t) = some (.tcb x5)) (hk5 : σ5.kheap (specPtr e) = some (.endpoint x)) + (hpr : ∀ y ∈ T, L.mem s5 (prioA y) = L.mem s (prioA y)) (hst : L.mem s5 (stA t) = sched.TS_BLOCKED_ON_RECV) + (hb : L.mem s5 (tBlockA t) = e) : + endpoint.ep_append st s5 e t obj.EP_RECV ⦃ s' => + ∃ σ', Fl4SpecA64.tcbEpAppend (Z := Unit) (specPtr t) (specPtr e) true σ5 = ({((), σ')}, False) ∧ + EpStateRel T E R Bf (L.mem s') σ' ∧ SR s' σ' ∧ WritesIn st s5 s' (EWrite T E R Bf) ⦄ := + ep_append_relA hO hP s5 σ5 hR5 hS5 ht hkt5 he hk5 true hc htq + (by rw [hst]; unfold sched.TS_BLOCKED_ON_RECV; rfl) hb + (sorted_transferA hP.tcb (hR.at_ep he hk) (hR5.at_ep he hk5) hpr hsort) + +/-- **Blocking receive** on an idle endpoint or one with waiting receivers (x86 `receive_ipc_block`): the three TCB +words (`block_recv_relA`), `schedule_tcb`, the reply object's TCB word if any, then `recv_append_fromA`; one outcome +of l4v `receive_ipc_blocked True`. -/ +theorem wait_for_sender_blockA (hO : SchedOpsEA st T E R Bf SR) (hF : SchedFootEA st T E R Bf SR Wr) + (hP : EPlaced T E R Bf) (s : S) + (σ : Fl4SpecA64.State Unit) (hR : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) + {t : U64} (ht : t ∈ T) {xt : Fl4SpecA64.Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hsr : SndRunA σ (specPtr t) xt) + {e : U64} (he : e ∈ E) {x : Fl4SpecA64.Endpoint} (hk : σ.kheap (specPtr e) = some (.endpoint x)) + (hc : CanBlockA true x) + (hsort : ∀ xs, QueueRel (L.mem s) T .Ep (qOf (L.mem s) e) xs → PrioSorted (fun y => L.mem s (prioA y)) xs) + (rv : U64) {rep : Option Fl4SpecA64.ObjRef} (hrv : optRef rv = rep) (hrR : rep ≠ none → rv ∈ R) : + endpoint.wait_for_sender st s t e true rv ⦃ s' => ∃ σ', + Fl4SpecA64.receiveIpcBlocked (Z := Unit) true (specPtr t) (specPtr e) rep σ = ({((), σ')}, False) ∧ + EpStateRel T E R Bf (L.mem s') σ' ∧ SR s' σ' ∧ FpA st T E R Bf Wr σ s s' ⦄ := by + have hta := hP.tcb t ht + have htq : specPtr t ∉ x.epQueue := not_queued_codeA hR ht hkt (runnable_codeA hsr.1) e he x hk + have hnt : specPtr e ≠ specPtr t := kheap_ne hk hkt (fun _ h1 h2 => by subst h1; cases h2) + unfold endpoint.wait_for_sender + simp only [↓reduceIte] + step with obj.Tcb.set_state.spec as ⟨s1, hs1⟩ + step with obj.Tcb.set_block_obj.spec as ⟨s2, hs2⟩ + step with obj.Tcb.set_reply_obj.spec as ⟨s3, hs3⟩ + subst hs1 hs2 hs3 + obtain ⟨hA3, r1, r2, -, hW3⟩ := block_recv_writesA (L := L) (E := E) (R := R) (Bf := Bf) hP s ht e rv + obtain ⟨hR1, hSR1⟩ := block_recv_relA hO hP s σ hR hSR ht hkt hsr e rv hrv hrR rcvPl0A + refine WP.spec_bind (WP.spec_and (hO.scheduleTcb _ _ t hR1 hSR1 ht) (hF.scheduleTcb _ _ t hR1 hSR1 ht)) ?_ + rintro s4 ⟨⟨hf4, hS4⟩, hw4⟩ + have hfp4 : FpA st T E R Bf Wr σ _ s4 := + FpA.trans hF (FpA.of_ew hW3) (ssz_updK_tcb σ (specPtr t) _) (FpA.of_wr hw4) + have hz4 : SSzA σ (stsAct (updK σ (specPtr t) + (.tcb { xt with tcbState := .blockedOnReceive (specPtr e) rep rcvPl0A })) (specPtr t)) := + (ssz_updK_tcb σ (specPtr t) _).trans (ssz_stsAct _ _) + have hR4 : EpStateRel T E R Bf (L.mem s4) + (stsAct (updK σ (specPtr t) (.tcb { xt with tcbState := .blockedOnReceive (specPtr e) rep rcvPl0A })) + (specPtr t)) := hR1.frame hP hf4 (KView.stsAct' _ _) + have hk4 : (stsAct (updK σ (specPtr t) (.tcb { xt with tcbState := .blockedOnReceive (specPtr e) rep rcvPl0A })) + (specPtr t)).kheap (specPtr e) = some (.endpoint x) := by + rw [stsAct_kheap, updK_kheap, Function.update_of_ne hnt, hk] + have hkt4 : (stsAct (updK σ (specPtr t) (.tcb { xt with tcbState := .blockedOnReceive (specPtr e) rep rcvPl0A })) + (specPtr t)).kheap (specPtr t) = + some (.tcb { xt with tcbState := .blockedOnReceive (specPtr e) rep rcvPl0A }) := by + rw [stsAct_kheap, updK_kheap, Function.update_self] + have hpr4 : ∀ y ∈ T, L.mem s4 (prioA y) = L.mem s (prioA y) := by + intro y hy + have hya := hP.tcb y hy + rw [hf4 _ (a_prio hya).1 (Or.inr (Or.inr (Or.inl ⟨y, hy, Or.inl (a_prio hya).2⟩)))] + refine hA3 _ (a_prio hya).1 ?_ + have := val_prio hya + by_cases eyt : y = t + · subst eyt; omega + · have := tcb_disj hP ht hy eyt; omega + have hst4 : L.mem s4 (stA t) = sched.TS_BLOCKED_ON_RECV := by + rw [hf4 _ (a_st hta).1 (Or.inr (Or.inr (Or.inl ⟨t, ht, Or.inl (a_st hta).2⟩))), r1] + have hb4 : L.mem s4 (tBlockA t) = e := by + rw [hf4 _ (a_blk hta).1 (Or.inr (Or.inr (Or.inl ⟨t, ht, Or.inl (a_blk hta).2⟩))), r2] + cases rep with + | none => + have h0 : rv = 0#u64 := optRef_none hrv + subst h0 + simp only [bne_self_eq_false, Bool.false_eq_true, ↓reduceIte, bind_ok] + have hspec : Fl4SpecA64.receiveIpcBlocked (Z := Unit) true (specPtr t) (specPtr e) none σ = + Fl4SpecA64.tcbEpAppend (Z := Unit) (specPtr t) (specPtr e) true (stsAct (updK σ (specPtr t) + (.tcb { xt with tcbState := .blockedOnReceive (specPtr e) none rcvPl0A })) + (specPtr t)) := by + unfold Fl4SpecA64.receiveIpcBlocked + simp only [↓reduceIte] + rw [nd_bind_one (setThreadState_eq _ hkt)] + exact nd_pure_bind' () (fun _ => Fl4SpecA64.tcbEpAppend (Z := Unit) (specPtr t) (specPtr e) true) _ + refine WP.spec_mono (recv_append_fromA hO hP hR ht he hk hc htq hsort hR4 hS4 hkt4 hk4 hpr4 hst4 hb4) ?_ + rintro s' ⟨σ', h1, h2, h3, hW6⟩ + exact ⟨σ', hspec.trans h1, h2, h3, FpA.trans hF hfp4 hz4 (FpA.of_ew hW6)⟩ + | some r => + obtain ⟨hw0, hwr⟩ := optRef_some hrv + subst hwr + have hrR' := hrR (by simp) + have hne : (rv != 0#u64) = true := by simpa using hw0 + simp only [hne, ↓reduceIte] + have hB : (do + let s6 ← obj.Reply.set_tcb st rv s4 t + ok (s6, t)) ⦃ r => r = (L.wr s4 (rTcbA rv) t, t) ⦄ := + WP.spec_bind (obj.Reply.set_tcb.spec s4 rv t) fun s6 h6 => by rw [WP.spec_ok, h6] + refine WP.spec_bind hB ?_ + rintro ⟨s5, t5⟩ h5 + simp only [Prod.mk.injEq] at h5 + obtain ⟨rfl, ht5⟩ := h5 + subst t5 + have hal := (a_rtcb hP hrR').1 + have hag := agree_wr (L := L) (s := s4) (v := t) hal + obtain ⟨xr, hkr4, -⟩ := hR4.reply rv hrR' + have htne : t ≠ 0#u64 := fun h => hta.ne0 (by rw [h]; rfl) + have hR5 := hR4.upd_reply hP hrR' { xr with replyTcb := some (specPtr t) } + (hag.mono fun b hb => by rw [rTcbA_val]; exact hb.2) (by rw [rd_wr_eq hal, optRef_ne htne]) + have hW5 : WritesIn st s4 (L.wr s4 (rTcbA rv) t) (EWrite T E R Bf) := + (writesIn_wr hal).mono fun b hb => by subst hb; exact Or.inr (Or.inl ⟨rv, hrR', rTcbA_val rv⟩) + have hS5 := hO.stable _ _ _ _ hS4 hW5 (EpStep.reply hkr4 (some (specPtr t))) + (NoBlockE.updO _ _ fun _ h => by cases h) + have hrt' : specPtr rv ≠ specPtr t := kheap_ne hkr4 hkt4 (fun _ h1 h2 => by subst h1; cases h2) + have hre' : specPtr rv ≠ specPtr e := kheap_ne hkr4 hk4 (fun _ h1 h2 => by subst h1; cases h2) + have hspec : Fl4SpecA64.receiveIpcBlocked (Z := Unit) true (specPtr t) (specPtr e) (some (specPtr rv)) σ = + Fl4SpecA64.tcbEpAppend (Z := Unit) (specPtr t) (specPtr e) true (updK (stsAct (updK σ (specPtr t) + (.tcb { xt with tcbState := .blockedOnReceive (specPtr e) (some (specPtr rv)) rcvPl0A })) + (specPtr t)) (specPtr rv) (.reply { xr with replyTcb := some (specPtr t) })) := by + unfold Fl4SpecA64.receiveIpcBlocked + simp only [↓reduceIte] + rw [nd_bind_one (setThreadState_eq _ hkt)] + exact nd_bind_one (setReplyTcb_eq (some (specPtr t)) hkr4) + (fun _ => Fl4SpecA64.tcbEpAppend (Z := Unit) (specPtr t) (specPtr e) true) + refine WP.spec_mono (recv_append_fromA hO hP hR ht he hk hc htq hsort hR5 hS5 + (x5 := { xt with tcbState := .blockedOnReceive (specPtr e) (some (specPtr rv)) rcvPl0A }) ?_ ?_ ?_ ?_ ?_) ?_ + · rw [updK_kheap, Function.update_of_ne hrt'.symm]; exact hkt4 + · rw [updK_kheap, Function.update_of_ne hre'.symm]; exact hk4 + · intro y hy + have hya := hP.tcb y hy + rw [hag _ (a_prio hya).1 (by + beta_reduce; try rw [rTcbA_val] + rw [val_prio hya]; have := hP.rt rv hrR' y hy; omega)] + exact hpr4 y hy + · rw [hag _ (a_st hta).1 (by + beta_reduce; try rw [rTcbA_val] + rw [val_st hta]; have := hP.rt rv hrR' t ht; omega)] + exact hst4 + · rw [hag _ (a_blk hta).1 (by + beta_reduce; try rw [rTcbA_val] + rw [val_blk hta]; have := hP.rt rv hrR' t ht; omega)] + exact hb4 + · rintro s' ⟨σ', h1, h2, h3, hW6⟩ + have hfp5 := FpA.trans hF hfp4 hz4 (FpA.of_ew (σ := stsAct (updK σ (specPtr t) (.tcb { xt with + tcbState := .blockedOnReceive (specPtr e) (some (specPtr rv)) rcvPl0A })) (specPtr t)) hW5) + have hz5 := hz4.trans (ssz_updK_reply (stsAct (updK σ (specPtr t) (.tcb { xt with + tcbState := .blockedOnReceive (specPtr e) (some (specPtr rv)) rcvPl0A })) (specPtr t)) (specPtr rv) + { xr with replyTcb := some (specPtr t) }) + exact ⟨σ', hspec.trans h1, h2, h3, FpA.trans hF hfp5 hz5 (FpA.of_ew (σ := updK (stsAct (updK σ (specPtr t) + (.tcb { xt with tcbState := .blockedOnReceive (specPtr e) (some (specPtr rv)) rcvPl0A })) (specPtr t)) + (specPtr rv) (.reply { xr with replyTcb := some (specPtr t) })) hW6)⟩ + +/-- **Polling receive** on an idle endpoint or one with waiting receivers (x86 `receive_ipc_poll`): the badge register +becomes 0 (`do_nbrecv_failed_transfer`), one outcome. -/ +theorem wait_for_sender_pollA (hO : SchedOpsEA st T E R Bf SR) (hP : EPlaced T E R Bf) (s : S) + (σ : Fl4SpecA64.State Unit) (hR : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) + {t : U64} (ht : t ∈ T) {xt : Fl4SpecA64.Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (e rv : U64) (rep : Option Fl4SpecA64.ObjRef) : + endpoint.wait_for_sender st s t e false rv ⦃ s' => ∃ σ', + Fl4SpecA64.receiveIpcBlocked (Z := Unit) false (specPtr t) (specPtr e) rep σ = ({((), σ')}, False) ∧ + EpStateRel T E R Bf (L.mem s') σ' ∧ SR s' σ' ∧ FpA st T E R Bf Wr σ s s' ⦄ := by + unfold endpoint.wait_for_sender + simp only [Bool.false_eq_true, ↓reduceIte] + refine WP.spec_mono (obj.Tcb.set_reg.spec s t _ _) ?_ + rintro s1 rfl + rw [← regIdxA_badge] + obtain ⟨hR1, hW1, hE1⟩ := write_reg_rel hP s σ hR ht hkt Fl4SpecA64.badge_register 0#u64 + refine ⟨_, ?_, hR1, hO.stable _ _ _ _ hSR hW1 hE1 (NoBlockE.tcbK hkt _ rfl rfl), FpA.of_ew hW1⟩ + unfold Fl4SpecA64.receiveIpcBlocked Fl4SpecA64.doNbrecvFailedTransfer + simp only [Bool.false_eq_true, ↓reduceIte] + rw [nd_bind_one (asUser_setRegister_eq _ _ hkt)] + rfl + +end + +end + +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/EpRecvBndA.lean b/proofs/Fl4ProofsA64/P3/EpRecvBndA.lean new file mode 100644 index 000000000..968fb2a3f --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/EpRecvBndA.lean @@ -0,0 +1,616 @@ +/- +P3 (aarch64, A64-P3-IPC gen 14): **`receive_ipc` with a bound notification and with a reply object still linked to +a thread** (closes `P3/EpRecvTopA`'s two scope limits; x86 `P3/XTopJointRecvIpcBound` / `XTopJointRecvIpcAct` and +`P3/EpRecv`'s `take_reply`, without the joint-invariant layer). + +* FL4: `receive_ipc` is `take_reply` then **`rcvBodyA`** (the bound-notification check, then `P3/EpRecvTopA`'s + `rcvDispA`) (`receive_ipc_eqA`, by unfolding). Spec: l4v `receive_ipc` is its reply-cap prefix then + **`specRecvBodyA`** (l4v `receive_ipc` after `reply` is computed, verbatim; `receiveIpc_bodyA` / + `receiveIpc_cancelA`), whose endpoint arm is `specDispA` (`specRecvSendA` / `receive_ipc_blocked`). +* `take_reply`: **`take_reply_plainA`** (a null cap, a reply linked to no thread or to the receiver itself: no write) + and **`take_reply_cancelA`** (a reply linked to another thread `i`: `cancel_ipc i`, IpcCancelA's `cancel_ipc_rel`, + both IPC relations after; the case selector `CancelOKA`: `i ∈ T`, not `BlockedOnReply` (deferred), `cancel_ipc_rel`'s + `hE` / `hN` / `RepOKA`). +* The bound notification: none, or not active with `maybe_return_sc` a no-op when blocking (**`BndOKA`**: + `maybe_return_noopA` / `maybeReturnSc_noopA`, x86 `maybe_return_noop` / `maybeReturnSc_noop`), then the dispatch + (**`rcv_body_sendA`**, **`rcv_body_waitA`**); or active: `complete_signal` (**`rcv_body_completeA`**, via + NtfnBlockA's `complete_signal_of_frame`; the endpoint relation across the badge-register and notification-state + writes by `rsPre_epRelA` and across the scheduler tail by the caller's frame proof). +* **`receive_ipc_relA`**: `take_reply` then any body related at the state after it (no cancel: the entry state; + cancel: `cancel_ipc`'s single outcome with `NtfnStateRel ∧ EpStateRel ∧ SR`), against l4v `receive_ipc`. + +Interfaces (composition, no named premise): `SchedHypsA` / `SchedOpsA` / `SchedOpsEA` for `cancel_ipc_rel`; for +`complete_signal`, a local proof that the scheduler tail preserves the endpoint relation. Case selectors: `CancelOKA`, `BndOKA` (the +`maybe_return_sc` no-op, read on both sides: the TCB's SC word is `SR`'s), the active receive's `hNQ` / `hRun` / `hon` +/ `hdl` (NtfnBlockA's). **Stop-first (not covered):** `maybe_return_sc` that returns the notification's SC (the +receiver's SC is the notification's): `SchedOpsA.returnSc` is stated after a block (`BlockStep`: the thread not +runnable) and does not apply to the running receiver; a contract for it needs a ruling. +-/ +import Fl4ProofsA64.P3.EpRecvTopA +import Fl4ProofsA64.P3.IpcCancelA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched + +noncomputable section + +/-! ## FL4's body of `receive_ipc` -/ + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +variable (st ar) in +/-- `receive_ipc` after `take_reply` (endpoint.rs `receive_ipc`, extraction text): the bound notification, then +`complete_signal` or (`maybe_return_sched_context` and) the endpoint dispatch `rcvDispA`. -/ +def rcvBodyA (s : S) (a : A) (t e : U64) (blk : Bool) (rv : U64) : Result (S × A) := do + let i ← obj.Tcb.bound_ntfn st t s + if i != 0#u64 + then + let i1 ← obj.Ntfn.state st i s + if i1 = obj.NTFN_ACTIVE + then notification.complete_signal st ar s a i t + else + let (s2, a2) ← + if i != 0#u64 + then + do + let t' ← + if blk + then notification.maybe_return_sched_context st s i t + else ok s + ok (t', a) + else ok (s, a) + rcvDispA st ar s2 a2 t e blk rv + else + let (s2, a2) ← + if i != 0#u64 + then + do + let t' ← + if blk + then notification.maybe_return_sched_context st s i t + else ok s + ok (t', a) + else ok (s, a) + rcvDispA st ar s2 a2 t e blk rv + +theorem receive_ipc_eqA (s : S) (a : A) (t e cb : U64) (c1 c2 c3 c4 blk : Bool) (rc : capability.Cap) : + endpoint.receive_ipc st ar s a t (.Endpoint e cb c1 c2 c3 c4) blk rc = (do + let (rv, s1, a1) ← endpoint.take_reply st ar s a t rc + rcvBodyA st ar s1 a1 t e blk rv) := by + unfold endpoint.receive_ipc + simp only [capability.Cap.ptr, bind_tc_ok, bind_ok, Bind.bind] + rfl + +/-- `ok x` then `f`. -/ +theorem spec_ok_bindA {α β : Type} {x : α} {f : α → Result β} {P : β → Prop} (h : f x ⦃ P ⦄) : + (ok x >>= f) ⦃ P ⦄ := + WP.spec_bind ((WP.spec_ok _).2 rfl) fun y e => by subst e; exact h + +theorem tcb_ne_eqA (a b : obj.Tcb) : + core.cmp.PartialEq.ne.trait_default obj.Tcb.Insts.CoreCmpPartialEqTcb a b = ok (decide (a ≠ b)) := by + unfold core.cmp.PartialEq.ne.trait_default core.cmp.PartialEq.ne.default + simp [obj.Tcb.Insts.CoreCmpPartialEqTcb.eq] + exact ⟨fun h => h ▸ rfl, UScalar.eq_of_val_eq⟩ + +/-- FL4's `maybe_return_sched_context` when the notification has no SC or another SC than the thread's: nothing +(x86 `Sched.maybe_return_noop`). -/ +theorem maybe_return_noopA {s : S} {n t : U64} + (h : L.mem s (nScA n) = 0#u64 ∨ L.mem s (nScA n) ≠ L.mem s (scA t)) : + notification.maybe_return_sched_context st s n t ⦃ s' => s' = s ⦄ := by + unfold notification.maybe_return_sched_context + step with obj.Ntfn.sc.spec as ⟨i, hi⟩ + step with obj.Sc.is_null.spec as ⟨b, hb⟩ + by_cases h0 : L.mem s (nScA n) = 0#u64 + · simp [hb, hi, h0, WP.spec_ok] + · rw [if_neg (by simp [hb, hi, h0])] + step with obj.Tcb.sc.spec as ⟨i1, hi1⟩ + rw [if_neg (by rw [hi, hi1]; exact h.resolve_left h0), WP.spec_ok] + +/-- The body without a bound notification: the dispatch. -/ +theorem rcv_body_noneA {s : S} {a : A} {t e : U64} {blk : Bool} {rv : U64} (h0 : L.mem s (tBoundA t) = 0#u64) + {P : S × A → Prop} (h : rcvDispA st ar s a t e blk rv ⦃ P ⦄) : rcvBodyA st ar s a t e blk rv ⦃ P ⦄ := by + unfold rcvBodyA + step with obj.Tcb.bound_ntfn.spec as ⟨i, hi⟩ + simp only [hi, h0, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte, bind_tc_ok, bind_ok, Bind.bind] + exact h + +/-- The body with a bound notification `n` that is not active (`maybe_return_sched_context` a no-op when blocking): +the dispatch. -/ +theorem rcv_body_inactA {s : S} {a : A} {t e n : U64} {blk : Bool} {rv : U64} (hn0 : n ≠ 0#u64) + (hb : L.mem s (tBoundA t) = n) (hst : stOf (L.mem s) n ≠ obj.NTFN_ACTIVE) + (hwn : blk = true → L.mem s (nScA n) = 0#u64 ∨ L.mem s (nScA n) ≠ L.mem s (scA t)) + {P : S × A → Prop} (h : rcvDispA st ar s a t e blk rv ⦃ P ⦄) : rcvBodyA st ar s a t e blk rv ⦃ P ⦄ := by + unfold rcvBodyA + step with obj.Tcb.bound_ntfn.spec as ⟨i, hi⟩ + rw [hi, hb, if_pos (by simpa using hn0)] + step with obj.Ntfn.state.spec as ⟨i1, hi1⟩ + rw [hi1, epState_eq, if_neg hst] + simp only [show (n != 0#u64) = true by simpa using hn0, ↓reduceIte] + cases blk with + | false => + simp only [Bool.false_eq_true, ↓reduceIte, bind_tc_ok, bind_ok, Bind.bind] + exact h + | true => + have hm : (do + let t' ← notification.maybe_return_sched_context st s n t + ok (t', a)) ⦃ r => r = (s, a) ⦄ := + WP.spec_bind (maybe_return_noopA (hwn rfl)) fun s' h' => by rw [h']; exact (WP.spec_ok _).2 rfl + have key : (do + let (s2, a2) ← (do + let t' ← notification.maybe_return_sched_context st s n t + ok (t', a)) + rcvDispA st ar s2 a2 t e true rv) ⦃ P ⦄ := by + refine WP.spec_bind hm ?_ + rintro ⟨s2, a2⟩ hq + rw [Prod.mk.injEq] at hq + rw [hq.1, hq.2] + exact h + simpa only [↓reduceIte, bind_tc_ok, bind_ok, Bind.bind] using key + +/-- The body with an active bound notification `n`: `complete_signal`. -/ +theorem rcv_body_actA {s : S} {a : A} {t e n : U64} {blk : Bool} {rv : U64} (hn0 : n ≠ 0#u64) + (hb : L.mem s (tBoundA t) = n) (hst : stOf (L.mem s) n = obj.NTFN_ACTIVE) + {P : S × A → Prop} (h : notification.complete_signal st ar s a n t ⦃ P ⦄) : rcvBodyA st ar s a t e blk rv ⦃ P ⦄ := by + unfold rcvBodyA + step with obj.Tcb.bound_ntfn.spec as ⟨i, hi⟩ + rw [hi, hb, if_pos (by simpa using hn0)] + step with obj.Ntfn.state.spec as ⟨i1, hi1⟩ + rw [hi1, epState_eq, if_pos hst] + exact h + +end + +/-! ## The spec's body of `receive_ipc` -/ + +section Spec +open Fl4SpecA64 Fl4Spec +open Classical + +/-- **l4v `receive_ipc` after the reply cap** (`Ipc_A.receive_ipc` from `ep ← get_endpoint epptr`), verbatim but for +the `SendEP` arm, which is EpRecvSendA's verbatim `specRecvSendA`. -/ +def specRecvBodyA (thread epptr : ObjRef) (isBlocking : Bool) (reply : Option ObjRef) : SMonad Unit Unit := do + let ep ← getEndpoint epptr + let ntfnptr ← getTcbObjRef Tcb.tcbBoundNotification thread + let ntfn ← match ntfnptr with + | none => pure defaultNotification + | some p => getNotification p + if ntfnptr ≠ none ∧ isActive ntfn + then + completeSignal (the ntfnptr) thread + else do + NonDet.when (ntfnptr ≠ none ∧ isBlocking) <| maybeReturnSc (the ntfnptr) thread + match ep with + | .sendEP q => specRecvSendA thread epptr reply q + | _ => receiveIpcBlocked isBlocking thread epptr reply + +/-- The endpoint arm of `specRecvBodyA`. -/ +def specDispA (thread epptr : ObjRef) (isBlocking : Bool) (reply : Option ObjRef) (x : Endpoint) : + SMonad Unit Unit := + match x with + | .sendEP q => specRecvSendA thread epptr reply q + | _ => receiveIpcBlocked isBlocking thread epptr reply + +theorem specDisp_waitA {t e : ObjRef} {blk : Bool} {rep : Option ObjRef} {x : Endpoint} (hc : CanBlockA true x) : + specDispA t e blk rep x = receiveIpcBlocked blk t e rep := by + cases x with + | idleEP => rfl + | recvEP q => rfl + | sendEP q => simp [CanBlockA] at hc + +/-- The spec's reply cap without a cancel: a null cap, or a reply linked to no thread or to the receiver. -/ +def SpecReplyPlainA (σ : State Unit) (t : ObjRef) (rc' : Cap) (reply : Option ObjRef) : Prop := + (rc' = .nullCap ∧ reply = none) ∨ + (∃ r rr x, rc' = .replyCap r rr ∧ reply = some r ∧ σ.kheap r = some (.reply x) ∧ + (x.replyTcb = none ∨ x.replyTcb = some t)) + +theorem the_someA (y : ObjRef) : the (some y) = y := rfl + +theorem nd_when_true_bindA {σ' β : Type} {P : Prop} [Decidable P] (h : P) (m : NonDet σ' Unit) + (f : Unit → NonDet σ' β) (s : σ') : (NonDet.when P m >>= f) s = (m >>= f) s := by + unfold NonDet.when; rw [if_pos h] + +/-- **l4v `receive_ipc` without a cancel in the reply prefix** is its body at the same state. -/ +theorem receiveIpc_bodyA {σ : State Unit} {t e : ObjRef} (b : Badge) (rights : CapRights) (blk : Bool) + {rc' : Cap} {reply : Option ObjRef} (hrc : SpecReplyPlainA σ t rc' reply) : + receiveIpc (Z := Unit) t (.endpointCap e b rights) blk rc' σ = specRecvBodyA t e blk reply σ := by + unfold receiveIpc + dsimp only + rw [nd_pure_bind'] + rcases hrc with ⟨rfl, rfl⟩ | ⟨r, rr, xr, rfl, rfl, hkr, hr | hr⟩ + · dsimp only + rw [nd_pure_bind'] + rfl + · dsimp only + rw [nd_bind_one (getReplyTcb_eq hkr)] + simp only [hr] + rw [nd_when_false_bindA (by simp [hr, the_someA]), nd_pure_bind'] + rfl + · dsimp only + rw [nd_bind_one (getReplyTcb_eq hkr)] + simp only [hr] + rw [nd_when_false_bindA (by simp [hr, the_someA]), nd_pure_bind'] + rfl + +/-- **l4v `receive_ipc` with a reply linked to another thread `i`**: `cancel_ipc i` (one outcome `σ1`), then the +body at `σ1`. -/ +theorem receiveIpc_cancelA {σ σ1 : State Unit} {t e : ObjRef} (b : Badge) (rights : CapRights) (blk : Bool) + {r : ObjRef} {rr : CapRights} {xr : Reply} {i : ObjRef} (hkr : σ.kheap r = some (.reply xr)) + (hr : xr.replyTcb = some i) (hit : i ≠ t) (hc : cancelIpc (Z := Unit) i σ = ({((), σ1)}, False)) : + receiveIpc (Z := Unit) t (.endpointCap e b rights) blk (.replyCap r rr) σ = specRecvBodyA t e blk (some r) σ1 := by + unfold receiveIpc + dsimp only + rw [nd_pure_bind'] + dsimp only + rw [nd_bind_one (getReplyTcb_eq hkr)] + simp only [hr] + rw [nd_when_true_bindA (by simp [hr, hit, the_someA]), show the (some i) = i from rfl, nd_bind_one hc, nd_pure_bind'] + rfl + +theorem getNtfnSc_eqA {σ : State Unit} {p : ObjRef} {x : Notification} (hk : σ.kheap p = some (.notification x)) : + getNtfnObjRef (Z := Unit) Notification.ntfnSc p σ = ({(x.ntfnSc, σ)}, False) := by + show getSkObjRef KernelObject.notification Notification.ntfnSc p σ = _ + unfold getSkObjRef + rw [nd_bind_one (getNotification_eq hk)]; rfl + +/-- The spec's `maybe_return_sc` when the notification's SC is not the thread's: nothing (x86 +`maybeReturnSc_noop`). -/ +theorem maybeReturnSc_noopA {σ : State Unit} {n t : ObjRef} {xn : Notification} {xt : Tcb} + (hkn : σ.kheap n = some (.notification xn)) (hkt : σ.kheap t = some (.tcb xt)) + (h : ¬ (xn.ntfnSc = xt.tcbSchedContext ∧ xn.ntfnSc ≠ none)) : + maybeReturnSc (Z := Unit) n t σ = ({((), σ)}, False) := by + unfold maybeReturnSc getTcbObjRef + rw [nd_bind_one (getNtfnSc_eqA hkn), nd_bind_one (threadGet_eq _ hkt), NonDet.when, if_neg h] + rfl + +/-- The spec's body with no bound notification, or a bound one that is not active (and whose SC is not the thread's +when blocking): the endpoint arm. -/ +theorem specBody_dispA {σ : State Unit} {t e : ObjRef} {xt : Tcb} (hkt : σ.kheap t = some (.tcb xt)) + {x : Endpoint} (hk : σ.kheap e = some (.endpoint x)) (blk : Bool) (reply : Option ObjRef) + (hb : xt.tcbBoundNotification = none ∨ ∃ n xn, xt.tcbBoundNotification = some n ∧ + σ.kheap n = some (.notification xn) ∧ isActive xn = false ∧ + (blk = true → ¬ (xn.ntfnSc = xt.tcbSchedContext ∧ xn.ntfnSc ≠ none))) : + specRecvBodyA t e blk reply σ = specDispA t e blk reply x σ := by + unfold specRecvBodyA + rw [nd_bind_one (getEndpoint_eq hk), nd_bind_one (getTcbObjRef_eq _ hkt)] + rcases hb with hb | ⟨n, xn, hb, hkn, hna, hnr⟩ + · simp only [hb] + rw [nd_pure_bind'] + rw [if_neg (by simp : ¬ ((none : Option ObjRef) ≠ none ∧ isActive defaultNotification = true))] + rw [nd_when_false_bindA (by simp [hb])] + cases x <;> rfl + · simp only [hb] + rw [nd_bind_one (getNotification_eq hkn), if_neg (by simp [hb, hna])] + cases blk with + | false => + rw [nd_when_false_bindA (by simp)] + cases x <;> rfl + | true => + rw [nd_when_true_bindA (by simp [hb]), show the (some n) = n from rfl, + nd_bind_one (maybeReturnSc_noopA hkn hkt (hnr rfl))] + cases x <;> rfl + +/-- The spec's body with an active bound notification: `complete_signal`. -/ +theorem specBody_actA {σ : State Unit} {t e : ObjRef} {xt : Tcb} (hkt : σ.kheap t = some (.tcb xt)) + {x : Endpoint} (hk : σ.kheap e = some (.endpoint x)) (blk : Bool) (reply : Option ObjRef) + {n : ObjRef} {xn : Notification} {b0 : Badge} (hb : xt.tcbBoundNotification = some n) + (hkn : σ.kheap n = some (.notification xn)) (hact : xn.ntfnObj = .activeNtfn b0) : + specRecvBodyA t e blk reply σ = completeSignal n t σ := by + unfold specRecvBodyA + rw [nd_bind_one (getEndpoint_eq hk), nd_bind_one (getTcbObjRef_eq _ hkt)] + simp only [hb] + rw [nd_bind_one (getNotification_eq hkn), if_pos (by simp [hb, isActive, hact])] + rfl + +end Spec + +/-! ## Relations -/ + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] {N T E R : Set U64} + {Bf : Set (U64 × Fl4SpecA64.Word)} {SR : S → Fl4SpecA64.State Unit → Prop} {DL : S → U64 → Prop} + {Wr : Fl4SpecA64.State Unit → S → U64 → Prop} + +variable (st) in +/-- The post of a `receive_ipc` step against the spec program `m` from `σ`: the spec does not fail and has a related +outcome. -/ +def RcvPostA (T E R : Set U64) (Bf : Set (U64 × Fl4SpecA64.Word)) (SR : S → Fl4SpecA64.State Unit → Prop) + (m : Fl4SpecA64.SMonad Unit Unit) (σ : Fl4SpecA64.State Unit) (r : S × A) : Prop := + ¬ (m σ).2 ∧ ∃ σ', ((), σ') ∈ (m σ).1 ∧ EpStateRel T E R Bf (L.mem r.1) σ' ∧ SR r.1 σ' + +/-- (case selector) **The bound notification for the dispatch**: none, or `n ∈ N` not active and, when blocking, +with an SC that is not the receiver's, read on both sides (the TCB's SC word is `SR`'s). -/ +def BndOKA (N : Set U64) (m : U64 → U64) (σ : Fl4SpecA64.State Unit) (t : U64) (xt : Fl4SpecA64.Tcb) + (blk : Bool) : Prop := + xt.tcbBoundNotification = none ∨ ∃ n ∈ N, xt.tcbBoundNotification = some (specPtr n) ∧ + ∀ xn, σ.kheap (specPtr n) = some (.notification xn) → Fl4SpecA64.isActive xn = false ∧ + (blk = true → ¬ (xn.ntfnSc = xt.tcbSchedContext ∧ xn.ntfnSc ≠ none) ∧ + (m (nScA n) = 0#u64 ∨ m (nScA n) ≠ m (scA t))) + +theorem stOf_not_activeA {m : U64 → U64} {n : U64} {o : Fl4SpecA64.Ntfn} (h : NtfnObjRel m T n o) + (ho : ∀ b, o ≠ .activeNtfn b) : stOf m n ≠ obj.NTFN_ACTIVE := by + rw [ntfn_consts.2.2] + cases o with + | idleNtfn => rw [h.1]; decide + | waitingNtfn q => rw [h.1]; decide + | activeNtfn b => exact absurd rfl (ho b) + +theorem bound_wordA {m : U64 → U64} {t n : U64} {xt : Fl4SpecA64.Tcb} (hx : TcbRelE R m t xt) + (hb : xt.tcbBoundNotification = some (specPtr n)) : n ≠ 0#u64 ∧ m (tBoundA t) = n := by + obtain ⟨h0, hp⟩ := optRef_some (hx.bound.symm.trans hb) + have e := specPtr_inj hp + exact ⟨e ▸ h0, e⟩ + +/-- FL4's body from `BndOKA`: the dispatch. -/ +theorem rcv_body_dispFlA {s : S} {a : A} {σ : Fl4SpecA64.State Unit} (hRN : NtfnStateRel N T (L.mem s) σ) + (hRE : EpStateRel T E R Bf (L.mem s) σ) {t : U64} (ht : t ∈ T) {xt : Fl4SpecA64.Tcb} + (hkt : σ.kheap (specPtr t) = some (.tcb xt)) {e : U64} {blk : Bool} {rv : U64} + (hbnd : BndOKA N (L.mem s) σ t xt blk) {P : S × A → Prop} (h : rcvDispA st ar s a t e blk rv ⦃ P ⦄) : + rcvBodyA st ar s a t e blk rv ⦃ P ⦄ := by + have hxt := hRE.at_tcb ht hkt + rcases hbnd with hb | ⟨n, hn, hb, hx⟩ + · exact rcv_body_noneA (optRef_none (hxt.bound.symm.trans hb)) h + · obtain ⟨hn0, hmn⟩ := bound_wordA hxt hb + obtain ⟨xn, hkn, hxn⟩ := hRN.ntfn n hn + obtain ⟨hna, hw⟩ := hx xn hkn + refine rcv_body_inactA hn0 hmn (stOf_not_activeA hxn.obj fun b e => ?_) (fun hb' => (hw hb').2) h + simp [Fl4SpecA64.isActive, e] at hna + +/-- The spec's body from `BndOKA`: the endpoint arm. -/ +theorem bndOK_specA {s : S} {σ : Fl4SpecA64.State Unit} {t : U64} {xt : Fl4SpecA64.Tcb} + (hkt : σ.kheap (specPtr t) = some (.tcb xt)) {e : U64} {x : Fl4SpecA64.Endpoint} + (hk : σ.kheap (specPtr e) = some (.endpoint x)) {blk : Bool} (hRN : NtfnStateRel N T (L.mem s) σ) + (hbnd : BndOKA N (L.mem s) σ t xt blk) (reply : Option Fl4SpecA64.ObjRef) : + specRecvBodyA (specPtr t) (specPtr e) blk reply σ = specDispA (specPtr t) (specPtr e) blk reply x σ := by + refine specBody_dispA hkt hk blk reply ?_ + rcases hbnd with hb | ⟨n, hn, hb, hx⟩ + · exact Or.inl hb + · obtain ⟨xn, hkn, -⟩ := hRN.ntfn n hn + exact Or.inr ⟨_, xn, hb, hkn, (hx xn hkn).1, fun hb' => ((hx xn hkn).2 hb').1⟩ + +open Fl4SpecA64 in +/-- **The body on a waiting sender** (`BndOKA`, then `rcv_disp_sendA`). -/ +theorem rcv_body_sendA (hO : SchedOpsEA st T E R Bf SR) (hF : SchedFootEA st T E R Bf SR Wr) + (hP : EPlaced T E R Bf) {th : TypedHeapA} {kb : U64} (hPl : PlaceA th kb) + (s : S) (a : A) (σ : State Unit) (hRN : NtfnStateRel N T (L.mem s) σ) (hR : EpStateRel T E R Bf (L.mem s) σ) + (hSR : SR s σ) + (hSt : StateRelA th (L.mem s) σ) (hV : SlotsValidA th (L.mem s)) (hC : BufCover st T Bf s σ) + (hNative : BufNativeA T (L.mem s)) + (hoff : ∀ y, IsSlotA th y → ¬ EWrite T E R Bf y ∧ ¬ EWrite T E R Bf (core.num.U64.wrapping_add y 8#u64)) + {t : U64} (ht : t ∈ T) (hth : th t = some .tcb) {xt : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hnb : tsCode xt.tcbState ≠ 4) + {e sd : U64} (he : e ∈ E) {q : List ObjRef} + (hk : σ.kheap (specPtr e) = some (.endpoint (.sendEP (specPtr sd :: q)))) + (hsh : th sd = some .tcb) {xs : Tcb} (hks : σ.kheap (specPtr sd) = some (.tcb xs)) + (hu : ∀ rp, ipcBufOfA true xt = some rp → ∀ k, k < 128 → inUserFrame (bufAddr rp k) σ) + (blk : Bool) (rv : U64) (hbnd : BndOKA N (L.mem s) σ t xt blk) + (hcase : ∀ e' dd, xs.tcbState = .blockedOnSend e' dd → + (xs.tcbFault = none ∧ (dd.senderCanGrant = false ∨ ipcBufOfA false xs = none ∨ + unat (dataToMessageInfo (xs.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0)) ∨ + ∃ f, xs.tcbFault = some f ∧ FaultOKA (L := L) s sd (ipcBufOfA true xt) xs f) + (hbr : ∀ e' dd, xs.tcbState = .blockedOnSend e' dd → + (dd.senderIsCall = false ∧ xs.tcbFault = none ∧ L.mem s (affA sd) = L.cpu s) ∨ + ((dd.senderIsCall = true ∨ xs.tcbFault ≠ none) ∧ + ((dd.senderCanGrant = false ∧ dd.senderCanGrantReply = false) ∨ optRef rv = none))) : + rcvBodyA st ar s a t e blk rv ⦃ fun r => RcvPostA st T E R Bf SR + (specRecvBodyA (specPtr t) (specPtr e) blk (optRef rv)) σ r ∧ FpA st T E R Bf Wr σ s r.1 ⦄ := by + have hsp := bndOK_specA hkt hk hRN hbnd (optRef rv) + refine WP.spec_mono (rcv_body_dispFlA hRN hR ht hkt hbnd + (rcv_disp_sendA hO hF hP hPl s a σ hR hSR hSt hV hC hNative hoff ht hth hkt hnb he hk hsh hks hu blk rv hcase hbr)) ?_ + rintro r ⟨-, h1, σ', h2, h3, h4, h5⟩ + unfold RcvPostA + rw [hsp] + exact ⟨⟨h1, σ', h2, h3, h4⟩, h5⟩ + +/-- **The body with no waiting sender** (`BndOKA`, then `rcv_disp_waitA`). -/ +theorem rcv_body_waitA (hO : SchedOpsEA st T E R Bf SR) (hF : SchedFootEA st T E R Bf SR Wr) + (hP : EPlaced T E R Bf) (s : S) (a : A) + (σ : Fl4SpecA64.State Unit) (hRN : NtfnStateRel N T (L.mem s) σ) (hR : EpStateRel T E R Bf (L.mem s) σ) + (hSR : SR s σ) {t : U64} (ht : t ∈ T) {xt : Fl4SpecA64.Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + {e : U64} (he : e ∈ E) {x : Fl4SpecA64.Endpoint} (hk : σ.kheap (specPtr e) = some (.endpoint x)) + (hc : CanBlockA true x) (rv : U64) (hrR : optRef rv ≠ none → rv ∈ R) (blk : Bool) + (hbnd : BndOKA N (L.mem s) σ t xt blk) + (hblk : blk = true → SndRunA σ (specPtr t) xt ∧ + ∀ xs, QueueRel (L.mem s) T .Ep (qOf (L.mem s) e) xs → PrioSorted (fun y => L.mem s (prioA y)) xs) : + rcvBodyA st ar s a t e blk rv ⦃ fun r => RcvPostA st T E R Bf SR + (specRecvBodyA (specPtr t) (specPtr e) blk (optRef rv)) σ r ∧ FpA st T E R Bf Wr σ s r.1 ⦄ := by + have hsp := bndOK_specA hkt hk hRN hbnd (optRef rv) + refine WP.spec_mono (rcv_body_dispFlA hRN hR ht hkt hbnd + (rcv_disp_waitA hO hF hP s a σ hR hSR ht hkt he hk hc rv hrR blk hblk)) ?_ + rintro r ⟨-, σ', h1, h3, h4, h5⟩ + unfold RcvPostA + rw [hsp, specDisp_waitA hc, h1] + exact ⟨⟨fun h => h, σ', Set.mem_singleton _, h3, h4⟩, h5⟩ + +/-- **The endpoint relation across the active receive's two writes** (the badge register: EpRegsA's +`write_reg_rel`; the notification's state word: `EpStateRel.frameX`, a notification word is in no `EKeep`). -/ +theorem rsPre_epRelA (hP : IPlacedA N T E R Bf) {s : S} {σ : Fl4SpecA64.State Unit} + (hRN : NtfnStateRel N T (L.mem s) σ) (hRE : EpStateRel T E R Bf (L.mem s) σ) {t : U64} (ht : t ∈ T) + {n : U64} (hn : n ∈ N) {x : Fl4SpecA64.Notification} (hk : σ.kheap (specPtr n) = some (.notification x)) + {b0 : Fl4SpecA64.Badge} (hact : x.ntfnObj = .activeNtfn b0) {xt : Fl4SpecA64.Tcb} + (hkt : σ.kheap (specPtr t) = some (.tcb xt)) : + EpStateRel T E R Bf (L.mem (rsPreA st s t n)) (rsPreσA σ (specPtr t) (specPtr n) xt x b0) := by + have hb0 : (L.mem s (nBadgeA n)).bv = b0 := by + have := (hRN.at_ntfn hn hk).obj; rw [hact] at this; exact this.2.2 + obtain ⟨hR1, -, -⟩ := write_reg_rel hP.e s σ hRE ht hkt Fl4SpecA64.badge_register (L.mem s (nBadgeA n)) + rw [← badgeRegA_eq_regA, hb0] at hR1 + have hnt : specPtr n ≠ specPtr t := by intro e; rw [e, hkt] at hk; cases hk + have hk1 : (updK σ (specPtr t) (.tcb (setReg xt Fl4SpecA64.badge_register b0))).kheap (specPtr n) = + some (.notification x) := by rw [updK_kheap, Function.update_of_ne hnt, hk] + have hW := writesIn_wr (st := st) (s := L.wr s (badgeRegA t) (L.mem s (nBadgeA n))) + (v := (L.mem (L.wr s (badgeRegA t) (L.mem s (nBadgeA n))) (epTSA n) &&& ~~~3#u64) ||| + (obj.NTFN_IDLE &&& 3#u64)) (foot_ts hP.n hn).1 + exact hR1.frameX hP.e (fun b hb hkp => hW.2.2 b hb fun e => (ek_ntfn hP hn (xs := []) (by simp) hkp).2.2 e) + (ekview_updN hk1 _) + +/-- The active bound notification's `complete_signal` with a caller-local endpoint frame. -/ +theorem complete_signal_relA (hH : SchedHypsA st N T SR) + (hO : SchedOpsA st ar N T SR DL) + (tail : CompleteTailFrameA st ar N T SR DL (fun s σ => EpStateRel T E R Bf (L.mem s) σ)) + (hP : IPlacedA N T E R Bf) + (s : S) (a : A) (σ : Fl4SpecA64.State Unit) (hRN : NtfnStateRel N T (L.mem s) σ) + (hRE : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) {t : U64} (ht : t ∈ T) {n : U64} (hn : n ∈ N) + {x : Fl4SpecA64.Notification} (hk : σ.kheap (specPtr n) = some (.notification x)) {b0 : Fl4SpecA64.Badge} + (hact : x.ntfnObj = .activeNtfn b0) (hNQ : NotQueued σ (specPtr t)) + (hRun : ∀ x, σ.kheap (specPtr t) = some (.tcb x) → Fl4SpecA64.runnable x.tcbState = true) + (hon : L.mem s (affA t) = L.cpu s) (hdl : DL s n) : + notification.complete_signal st ar s a n t ⦃ RcvPostA st T E R Bf SR + (Fl4SpecA64.completeSignal (specPtr n) (specPtr t)) σ ⦄ := by + obtain ⟨xt, hkt, -⟩ := hRN.tcb t ht + obtain ⟨-, -, -, hW, hS⟩ := recv_active_prefix (st := st) hP.n hRN ht hn hk hact hkt + have hnt : specPtr n ≠ specPtr t := by intro e; rw [e, hkt] at hk; cases hk + refine WP.spec_mono (complete_signal_of_frame hO tail hP.n s a σ hRN hSR ht hn hk hact hkt hNQ hRun hon hdl + (hH.stable _ _ _ _ hSR hW hS (NoBlockE.recvPrefix hkt hnt _ _ _)) + (rsPre_epRelA hP hRN hRE ht hn hk hact hkt)) ?_ + rintro r ⟨h1, σ', h2, -, h3, h4⟩ + exact ⟨h1, σ', h2, h4, h3⟩ + +/-- **The body with an active bound notification**: `complete_signal` (`complete_signal_relA`). -/ +theorem rcv_body_completeA (hH : SchedHypsA st N T SR) + (hO : SchedOpsA st ar N T SR DL) + (tail : CompleteTailFrameA st ar N T SR DL (fun s σ => EpStateRel T E R Bf (L.mem s) σ)) + (hP : IPlacedA N T E R Bf) + (s : S) (a : A) (σ : Fl4SpecA64.State Unit) (hRN : NtfnStateRel N T (L.mem s) σ) + (hRE : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) {t : U64} (ht : t ∈ T) {xt : Fl4SpecA64.Tcb} + (hkt : σ.kheap (specPtr t) = some (.tcb xt)) {n : U64} (hn : n ∈ N) + (hb : xt.tcbBoundNotification = some (specPtr n)) {x : Fl4SpecA64.Notification} + (hk : σ.kheap (specPtr n) = some (.notification x)) {b0 : Fl4SpecA64.Badge} + (hact : x.ntfnObj = .activeNtfn b0) (hNQ : NotQueued σ (specPtr t)) + (hRun : ∀ x, σ.kheap (specPtr t) = some (.tcb x) → Fl4SpecA64.runnable x.tcbState = true) + (hon : L.mem s (affA t) = L.cpu s) (hdl : DL s n) (e : U64) {xe : Fl4SpecA64.Endpoint} + (hke : σ.kheap (specPtr e) = some (.endpoint xe)) (blk : Bool) (rv : U64) : + rcvBodyA st ar s a t e blk rv ⦃ RcvPostA st T E R Bf SR (specRecvBodyA (specPtr t) (specPtr e) blk (optRef rv)) σ ⦄ := by + obtain ⟨hn0, hmn⟩ := bound_wordA (hRE.at_tcb ht hkt) hb + have hsp := specBody_actA hkt hke blk (optRef rv) hb hk hact + refine WP.spec_mono (rcv_body_actA hn0 hmn (stOf_active (hRN.at_ntfn hn hk).obj hact) + (complete_signal_relA hH hO tail hP s a σ hRN hRE hSR ht hn hk hact hNQ hRun hon hdl)) ?_ + intro r hr + unfold RcvPostA at hr ⊢ + rw [hsp] + exact hr + +/-! ## `take_reply` and `receive_ipc` -/ + +/-- FL4's reply cap `rc` and the spec's `rc'` name the reply `rv` (0 for none), a reply object in `R` (x86 +`ReplyArg` without the unlinked condition). -/ +def ReplyCapA (R : Set U64) (rc : capability.Cap) (rc' : Fl4SpecA64.Cap) (rv : U64) : Prop := + (rc = .Null ∧ rc' = .nullCap ∧ rv = 0#u64) ∨ + (∃ b r', rc = .Reply rv b ∧ rc' = .replyCap (specPtr rv) r' ∧ rv ∈ R) + +/-- (case selector) `cancel_ipc_rel`'s hypotheses on the thread `i` a reply is still linked to: tracked, not +`BlockedOnReply` (deferred), its endpoint / notification holding it, `RepOKA`. -/ +def CancelOKA (N T E : Set U64) (m : U64 → U64) (σ : Fl4SpecA64.State Unit) (i : U64) : Prop := + i ∈ T ∧ ∃ x, σ.kheap (specPtr i) = some (.tcb x) ∧ (∀ r, x.tcbState ≠ .blockedOnReply r) ∧ + (∀ e', Fl4SpecA64.epBlocked x.tcbState = some e' → m (tBlockA i) ∈ E ∧ + ∃ z, σ.kheap e' = some (.endpoint z) ∧ specPtr i ∈ z.epQueue) ∧ + (∀ n', x.tcbState = .blockedOnNotification n' → m (tBlockA i) ∈ N ∧ + ∃ z, σ.kheap n' = some (.notification z) ∧ specPtr i ∈ z.ntfnObj.ntfnQueue) ∧ + RepOKA σ (specPtr i) + +/-- `take_reply` without a cancel: a null cap, or a reply linked to no thread or to the receiver. -/ +theorem take_reply_plainA {s : S} {a : A} {t : U64} {rc : capability.Cap} {rc' : Fl4SpecA64.Cap} {rv : U64} + (hrc : ReplyCapA R rc rc' rv) (h : ∀ b, rc = .Reply rv b → L.mem s (rTcbA rv) = 0#u64 ∨ L.mem s (rTcbA rv) = t) : + endpoint.take_reply st ar s a t rc ⦃ r => r = (rv, s, a) ⦄ := by + rcases hrc with ⟨rfl, -, rfl⟩ | ⟨b, r', rfl, -, -⟩ + · simp only [endpoint.take_reply]; rw [WP.spec_ok] + · simp only [endpoint.take_reply] + step with obj.Reply.tcb.spec as ⟨i, hi⟩ + step with obj.Tcb.is_null.spec as ⟨bn, hbn⟩ + subst hi + by_cases h0 : L.mem s (rTcbA rv) = 0#u64 + · simp only [hbn, h0, decide_true, ↓reduceIte]; rw [WP.spec_ok] + · have hit : L.mem s (rTcbA rv) = t := (h b rfl).resolve_left h0 + simp only [hbn, h0, decide_false, Bool.false_eq_true, ↓reduceIte] + rw [tcb_ne_eqA] + apply spec_ok_bindA + simp only [hit, ne_eq, not_true_eq_false, decide_false, Bool.false_eq_true, ↓reduceIte] + rw [WP.spec_ok] + +/-- **`take_reply` of a reply linked to another thread `i`**: `cancel_ipc i` (`cancel_ipc_rel`), both IPC relations +and `SR` after, against the spec's single outcome. -/ +theorem take_reply_cancelA (hH : SchedHypsA st N T SR) (hO : SchedOpsA st ar N T SR DL) + (hOE : SchedOpsEA st T E R Bf SR) (hP : IPlacedA N T E R Bf) (s : S) (a : A) (σ : Fl4SpecA64.State Unit) + (hRN : NtfnStateRel N T (L.mem s) σ) (hRE : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) {t rv : U64} {b : Bool} + (hi0 : L.mem s (rTcbA rv) ≠ 0#u64) (hit : L.mem s (rTcbA rv) ≠ t) + (hc : CancelOKA N T E (L.mem s) σ (L.mem s (rTcbA rv))) : + endpoint.take_reply st ar s a t (.Reply rv b) ⦃ r => r.1 = rv ∧ r.2.2 = a ∧ ∃ σ1, + Fl4SpecA64.cancelIpc (Z := Unit) (specPtr (L.mem s (rTcbA rv))) σ = ({((), σ1)}, False) ∧ + NtfnStateRel N T (L.mem r.2.1) σ1 ∧ EpStateRel T E R Bf (L.mem r.2.1) σ1 ∧ SR r.2.1 σ1 ⦄ := by + obtain ⟨hiT, x, hki, hnr, hE, hN, hrep⟩ := hc + simp only [endpoint.take_reply] + step with obj.Reply.tcb.spec as ⟨i, hi⟩ + step with obj.Tcb.is_null.spec as ⟨bn, hbn⟩ + subst hi + simp only [hbn, hi0, decide_false, Bool.false_eq_true, ↓reduceIte] + rw [tcb_ne_eqA] + apply spec_ok_bindA + simp only [hit, ne_eq, not_false_eq_true, decide_true, ↓reduceIte] + refine WP.spec_bind (cancel_ipc_rel hH hO.scheduleTcb hOE hP s a σ hRN hRE hSR hiT hki hnr hE hN hrep) ?_ + rintro ⟨s1, a1⟩ ⟨ha, σ1, h1, h2, h3, h4⟩ + exact (WP.spec_ok _).2 ⟨rfl, ha, σ1, h1, h2, h3, h4⟩ + +/-- **`receive_ipc` refines l4v `receive_ipc`** for any reply cap (`ReplyCapA`): `take_reply` (no write, or +`cancel_ipc` of the reply's other thread under `CancelOKA`), then a body related at the state after it — `hbody0` +at the entry state when nothing is cancelled, `hbody1` at `cancel_ipc`'s outcome (with both IPC relations and `SR` +there) — as `rcv_body_sendA` / `rcv_body_waitA` / `rcv_body_completeA` give. -/ +theorem receive_ipc_relA (hH : SchedHypsA st N T SR) (hO : SchedOpsA st ar N T SR DL) + (hOE : SchedOpsEA st T E R Bf SR) (hP : IPlacedA N T E R Bf) (s : S) (a : A) (σ : Fl4SpecA64.State Unit) + (hRN : NtfnStateRel N T (L.mem s) σ) (hRE : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) + {t : U64} (ht : t ∈ T) (e cb : U64) (c1 c2 c3 c4 : Bool) (blk : Bool) {rc : capability.Cap} + {rc' : Fl4SpecA64.Cap} {rv : U64} (hrc : ReplyCapA R rc rc' rv) (b : Fl4SpecA64.Badge) + (rights : Fl4SpecA64.CapRights) + (hbody0 : (rv = 0#u64 ∨ L.mem s (rTcbA rv) = 0#u64 ∨ L.mem s (rTcbA rv) = t) → + rcvBodyA st ar s a t e blk rv ⦃ RcvPostA st T E R Bf SR + (specRecvBodyA (specPtr t) (specPtr e) blk (optRef rv)) σ ⦄) + (hcan : rv ≠ 0#u64 → L.mem s (rTcbA rv) ≠ 0#u64 → L.mem s (rTcbA rv) ≠ t → + CancelOKA N T E (L.mem s) σ (L.mem s (rTcbA rv))) + (hbody1 : ∀ s1 a1 σ1, Fl4SpecA64.cancelIpc (Z := Unit) (specPtr (L.mem s (rTcbA rv))) σ = ({((), σ1)}, False) → + NtfnStateRel N T (L.mem s1) σ1 → EpStateRel T E R Bf (L.mem s1) σ1 → SR s1 σ1 → + rcvBodyA st ar s1 a1 t e blk rv ⦃ RcvPostA st T E R Bf SR + (specRecvBodyA (specPtr t) (specPtr e) blk (optRef rv)) σ1 ⦄) : + endpoint.receive_ipc st ar s a t (.Endpoint e cb c1 c2 c3 c4) blk rc ⦃ RcvPostA st T E R Bf SR + (Fl4SpecA64.receiveIpc (specPtr t) (.endpointCap (specPtr e) b rights) blk rc') σ ⦄ := by + have ht0 : t ≠ 0#u64 := fun h => (hP.n.tcb t ht).ne0 (by rw [h]; rfl) + rw [receive_ipc_eqA] + rcases hrc with ⟨rfl, rfl, rfl⟩ | ⟨b', r', rfl, rfl, hr⟩ + · have hsp := receiveIpc_bodyA (t := specPtr t) (e := specPtr e) b rights blk + (σ := σ) (Or.inl ⟨rfl, optRef_zero⟩) + refine WP.spec_bind (take_reply_plainA (R := R) (rc' := .nullCap) (Or.inl ⟨rfl, rfl, rfl⟩ : ReplyCapA R .Null .nullCap 0#u64) (fun _ h => by cases h)) ?_ + rintro r rfl + refine WP.spec_mono (hbody0 (Or.inl rfl)) ?_ + intro r hr + unfold RcvPostA at hr ⊢ + rw [hsp]; exact hr + · have hrv0 : rv ≠ 0#u64 := fun h => (hP.e.rep rv hr).2.1 (by rw [h]; rfl) + obtain ⟨xr, hkr, hxr⟩ := hRE.reply rv hr + by_cases hnc : L.mem s (rTcbA rv) = 0#u64 ∨ L.mem s (rTcbA rv) = t + · have hsp := receiveIpc_bodyA (t := specPtr t) (e := specPtr e) b rights blk (σ := σ) + (reply := optRef rv) (Or.inr ⟨specPtr rv, r', xr, rfl, optRef_ne hrv0, hkr, by + rcases hnc with h | h + · exact Or.inl (by rw [hxr, h, optRef_zero]) + · exact Or.inr (by rw [hxr, h, optRef_ne ht0])⟩) + refine WP.spec_bind (take_reply_plainA (Or.inr ⟨b', r', rfl, rfl, hr⟩) (fun _ _ => hnc)) ?_ + rintro r rfl + refine WP.spec_mono (hbody0 (Or.inr hnc)) ?_ + intro r hr + unfold RcvPostA at hr ⊢ + rw [hsp]; exact hr + · have hi0 : L.mem s (rTcbA rv) ≠ 0#u64 := fun h => hnc (Or.inl h) + have hit : L.mem s (rTcbA rv) ≠ t := fun h => hnc (Or.inr h) + refine WP.spec_bind (take_reply_cancelA hH hO hOE hP s a σ hRN hRE hSR hi0 hit (hcan hrv0 hi0 hit)) ?_ + rintro ⟨rv', s1, a1⟩ ⟨hrv, -, σ1, hc, h1, h2, h3⟩ + dsimp only at hrv ⊢ + subst hrv + have hsp := receiveIpc_cancelA (t := specPtr t) (e := specPtr e) (rr := r') b rights blk hkr + (hxr.trans (optRef_ne hi0)) (fun h => hit (specPtr_inj h)) hc + refine WP.spec_mono (hbody1 s1 a1 σ1 hc h1 h2 h3) ?_ + intro r hr + unfold RcvPostA at hr ⊢ + rw [hsp, ← optRef_ne hrv0]; exact hr + +end + +end + +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/EpRecvSendA.lean b/proofs/Fl4ProofsA64/P3/EpRecvSendA.lean new file mode 100644 index 000000000..a369ce63f --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/EpRecvSendA.lean @@ -0,0 +1,433 @@ +/- +P3 (aarch64, A64-P3-IPC gen 13): **`receive_ipc`'s send-queue branch refines l4v `receive_ipc`'s `SendEP` arm** +(`Z = Unit`; x86 `P3/EpRecvSend` `receive_ipc_send_gen`, `P3/EpRecvX` `receive_ipc_cfInactive_gen`): FL4's +`receive_from_sender` dequeues the head sender (`ep_dequeue_raw`), reads its badge and flags words (the `BlockedOnSend` +payload, `TcbRelE.send`), transfers the message (gen 10's `do_ipc_transfer_anyA`, a faulted or non-faulted sender, +both buffers), runs the sporadic unblock of the sender while it is still blocked (`SchedOpsEA.unblockSporadic`, gen 13), +then either sets it running and switches to it (`SchedOpsEA.switchTo`, gen 13) or, for a call or a faulted sender +without a reply push, sets it `Inactive` (`NoBlockE`'s branch 3). + +* Spec: **`specRecvSendA`** (l4v `receive_ipc`'s `SendEP q` arm after the notification check, verbatim, so the + `receive_ipc` prefix (gen 14) reduces to it), `flags_grantA` / `flags_grantReplyA` / `flags_callA` (x86 `flags_grant` + / `flags_call`), **`recvSend_runA`** (x86 `receiveIpc_send_eq`: the arm is `possible_switch_to sender` after the sender + runs) and **`recvSend_inactA`** (x86 `receiveIpc_cfInactive_eq` / `receiveIpc_sendCallInactive_eq`: the sender becomes + `Inactive`, one outcome). +* FL4: **`recv_run_tailA`** (the non-call arm after the fault word: `set_thread_state sender Running`, `NoBlockE` + branch 2 by `inClassR` of `BlockedOnSend` without fault, then `SchedOpsEA.switchTo`, a separate declaration) and + **`receive_from_senderA`**. + +Hypotheses (case selectors / placement, no named premise): the transfer's (`PlaceA` / `StateRelA` / `SlotsValidA` / +`BufCover` / `hoff` / `hu`, gen 9–10), `hcase` (`do_ipc_transfer_anyA`'s, on the sender's payload), the receiver not +blocked on send (`hnb`), the reply argument (`optRef rv = rep`), and the branch selector `hbr`: a non-call sender without +fault on the current core (`hon`, `switchTo`'s local-core precondition), or a call / faulted sender without a reply +push (no grant and no grant-reply, or no reply object). Not covered: `reply_push` (the grant + reply arm, with +`can_donate` read from the sender's SC word), an MCS contract (ruling Q3). +-/ +import Fl4ProofsA64.P3.EpSendRecvA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched + +noncomputable section + +theorem flags_grantA (d : Fl4SpecA64.SenderPayload) : + ((flagsOf d &&& endpoint.FLAG_GRANT) != 0#u64) = d.senderCanGrant := by + unfold flagsOf endpoint.FLAG_GRANT + cases d.senderCanGrant <;> cases d.senderCanGrantReply <;> cases d.senderIsCall <;> decide + +theorem flags_grantReplyA (d : Fl4SpecA64.SenderPayload) : + ((flagsOf d &&& endpoint.FLAG_GRANT_REPLY) != 0#u64) = d.senderCanGrantReply := by + unfold flagsOf endpoint.FLAG_GRANT_REPLY + cases d.senderCanGrant <;> cases d.senderCanGrantReply <;> cases d.senderIsCall <;> decide + +theorem flags_callA (d : Fl4SpecA64.SenderPayload) : + ((flagsOf d &&& endpoint.FLAG_CALL) != 0#u64) = d.senderIsCall := by + unfold flagsOf endpoint.FLAG_CALL + cases d.senderCanGrant <;> cases d.senderCanGrantReply <;> cases d.senderIsCall <;> decide + +/-! ## The spec side -/ + +section Spec +open Fl4SpecA64 Fl4Spec +open Classical + +/-- **l4v `receive_ipc`'s `SendEP q` arm** (`Ipc_A.receive_ipc`, after the bound-notification check), verbatim. -/ +def specRecvSendA (thread epptr : ObjRef) (reply : Option ObjRef) (q : List ObjRef) : SMonad Unit Unit := do + NonDet.assert (q ≠ []) + let sender ← pure (the q.head?) + tcbEpDequeue sender epptr + let senderState ← getThreadState sender + let data ← match senderState with + | .blockedOnSend _ref data => pure data + | _ => NonDet.fail + doIpcTransfer sender (some epptr) + data.senderBadge data.senderCanGrant + thread + let scPtr ← getTcbObjRef Tcb.tcbSchedContext sender + ifSporadicCurScAssertRefillUnblockCheck scPtr + let fault ← threadGet Tcb.tcbFault sender + if data.senderIsCall ∨ fault ≠ none + then + if (data.senderCanGrant ∨ data.senderCanGrantReply) ∧ reply ≠ none + then do + let senderSc ← getTcbObjRef Tcb.tcbSchedContext sender + let donate ← pure (decide (senderSc ≠ none ∧ ¬(fault.elim false isTimeoutFault = true))) + replyPush sender thread (the reply) donate + else setThreadState sender .inactive + else do + setThreadState sender .running + possibleSwitchTo sender + +/-- **The arm for a non-call sender without fault** (x86 `receiveIpc_send_eq`): from the transfer's and the sporadic +check's single outcomes, the sender runs, then `possible_switch_to`. -/ +theorem recvSend_runA {σ : State Unit} {t e sd : ObjRef} {q : List ObjRef} (rep : Option ObjRef) + (hk : σ.kheap e = some (.endpoint (.sendEP (sd :: q)))) {xs : Tcb} (hks : σ.kheap sd = some (.tcb xs)) + (hse : sd ≠ e) {e' : ObjRef} {dd : SenderPayload} (hst : xs.tcbState = .blockedOnSend e' dd) + {σ2 σ3 : State Unit} + (hT : doIpcTransfer (Z := Unit) sd (some e) dd.senderBadge dd.senderCanGrant t + (updK σ e (.endpoint (epDeq (.sendEP (sd :: q)) sd))) = ({((), σ2)}, False)) + (hks2 : σ2.kheap sd = some (.tcb xs)) + (hA : ifSporadicCurScAssertRefillUnblockCheck (Z := Unit) xs.tcbSchedContext σ2 = ({((), σ3)}, False)) + {xs3 : Tcb} (hks3 : σ3.kheap sd = some (.tcb xs3)) (hcall : dd.senderIsCall = false) + (hf3 : xs3.tcbFault = none) : + specRecvSendA t e rep (sd :: q) σ = + possibleSwitchTo sd (stsAct (updK σ3 sd (.tcb { xs3 with tcbState := .running })) sd) := by + have hks1 : (updK σ e (.endpoint (epDeq (.sendEP (sd :: q)) sd))).kheap sd = some (.tcb xs) := by + rw [updK_kheap, Function.update_of_ne hse, hks] + unfold specRecvSendA + rw [nd_bind_one (assert_true (by simp) σ), nd_pure_bind'] + rw [show the (sd :: q).head? = sd from rfl, nd_bind_one (tcbEpDequeue_eq hk (by simp [Endpoint.epQueue])), + nd_bind_one (getThreadState_eq hks1), hst] + dsimp only + rw [nd_pure_bind', nd_bind_one hT, nd_bind_one (getTcbObjRef_eq _ hks2), nd_bind_one hA, + nd_bind_one (threadGet_eq _ hks3), if_neg (by rw [hcall, hf3]; simp), nd_bind_one (setThreadState_eq _ hks3)] + +/-- **The arm for a call or a faulted sender without a reply push** (x86 `receiveIpc_cfInactive_eq`): the sender +becomes `Inactive`, one outcome. -/ +theorem recvSend_inactA {σ : State Unit} {t e sd : ObjRef} {q : List ObjRef} (rep : Option ObjRef) + (hk : σ.kheap e = some (.endpoint (.sendEP (sd :: q)))) {xs : Tcb} (hks : σ.kheap sd = some (.tcb xs)) + (hse : sd ≠ e) {e' : ObjRef} {dd : SenderPayload} (hst : xs.tcbState = .blockedOnSend e' dd) + {σ2 σ3 : State Unit} + (hT : doIpcTransfer (Z := Unit) sd (some e) dd.senderBadge dd.senderCanGrant t + (updK σ e (.endpoint (epDeq (.sendEP (sd :: q)) sd))) = ({((), σ2)}, False)) + (hks2 : σ2.kheap sd = some (.tcb xs)) + (hA : ifSporadicCurScAssertRefillUnblockCheck (Z := Unit) xs.tcbSchedContext σ2 = ({((), σ3)}, False)) + {xs3 : Tcb} (hks3 : σ3.kheap sd = some (.tcb xs3)) + (hcf : dd.senderIsCall = true ∨ xs3.tcbFault ≠ none) + (hnp : (dd.senderCanGrant = false ∧ dd.senderCanGrantReply = false) ∨ rep = none) : + specRecvSendA t e rep (sd :: q) σ = + ({((), stsAct (updK σ3 sd (.tcb { xs3 with tcbState := .inactive })) sd)}, False) := by + have hks1 : (updK σ e (.endpoint (epDeq (.sendEP (sd :: q)) sd))).kheap sd = some (.tcb xs) := by + rw [updK_kheap, Function.update_of_ne hse, hks] + have hn : ¬ ((dd.senderCanGrant = true ∨ dd.senderCanGrantReply = true) ∧ rep ≠ none) := by + rintro ⟨h1, h2⟩ + rcases hnp with ⟨h3, h4⟩ | h3 + · rw [h3, h4] at h1; simp at h1 + · exact h2 h3 + unfold specRecvSendA + rw [nd_bind_one (assert_true (by simp) σ), nd_pure_bind'] + rw [show the (sd :: q).head? = sd from rfl, nd_bind_one (tcbEpDequeue_eq hk (by simp [Endpoint.epQueue])), + nd_bind_one (getThreadState_eq hks1), hst] + dsimp only + rw [nd_pure_bind', nd_bind_one hT, nd_bind_one (getTcbObjRef_eq _ hks2), nd_bind_one hA, + nd_bind_one (threadGet_eq _ hks3), if_pos hcf, if_neg hn, setThreadState_eq _ hks3] + +end Spec + +/-! ## FL4 -/ + +section Fl4 +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] {T E R : Set U64} + {Bf : Set (U64 × Fl4SpecA64.Word)} {SR : S → Fl4SpecA64.State Unit → Prop} + {Wr : Fl4SpecA64.State Unit → S → U64 → Prop} + +/-- **The non-call arm of `receive_from_sender`** after the fault word (a separate declaration): the sender (blocked on +send, no fault, so in `inClassR`: `NoBlockE`'s branch 2) runs, then `SchedOpsEA.switchTo` (local core by `coreKeep`, +in no scheduler queue by `SchedOpsEA.notQueued` before and `NotQueued.sts`). -/ +theorem recv_run_tailA (hO : SchedOpsEA st T E R Bf SR) (hF : SchedFootEA st T E R Bf SR Wr) + (hP : EPlaced T E R Bf) (a : A) {s s3 : S} + {σ σ3 : Fl4SpecA64.State Unit} (hSR : SR s σ) {sd : U64} (hsT : sd ∈ T) (hon : L.mem s (affA sd) = L.cpu s) + {xs3 : Fl4SpecA64.Tcb} (hR3 : EpStateRel T E R Bf (L.mem s3) σ3) (hS3 : SR s3 σ3) + (hks3 : σ3.kheap (specPtr sd) = some (.tcb xs3)) {e' : Fl4SpecA64.ObjRef} {dd : Fl4SpecA64.SenderPayload} + (hst3 : xs3.tcbState = .blockedOnSend e' dd) (hf3 : xs3.tcbFault = none) + (hq3 : ∀ e ∈ E, ∀ z, σ3.kheap (specPtr e) = some (.endpoint z) → specPtr sd ∉ z.epQueue) + {σb : Fl4SpecA64.State Unit} {sb : S} (hpre : FpA st T E R Bf Wr σb sb s3) (hzb : SSzA σb σ3) : + (do + let s4 ← sched.set_thread_state st s3 sd sched.TS_RUNNING + let s5 ← sched.possible_switch_to st s4 sd + ok (s5, a)) ⦃ r => r.2 = a ∧ + ¬ (Fl4SpecA64.possibleSwitchTo (Z := Unit) (specPtr sd) + (stsAct (updK σ3 (specPtr sd) (.tcb { xs3 with tcbState := .running })) (specPtr sd))).2 ∧ + ∃ σ', ((), σ') ∈ (Fl4SpecA64.possibleSwitchTo (Z := Unit) (specPtr sd) + (stsAct (updK σ3 (specPtr sd) (.tcb { xs3 with tcbState := .running })) (specPtr sd))).1 ∧ + EpStateRel T E R Bf (L.mem r.1) σ' ∧ SR r.1 σ' ∧ FpA st T E R Bf Wr σb sb r.1 ⦄ := by + have hnr : Fl4SpecA64.runnable xs3.tcbState = false := by rw [hst3]; rfl + have hcls : inClassR xs3.tcbState xs3.tcbFault := + Or.inr (Or.inr (Or.inl ⟨e', dd, hst3, fun b h => by rw [hf3] at h; cases h⟩)) + refine WP.spec_bind (set_thread_state_relF hO hF hP s3 σ3 hR3 hS3 hsT hks3 hq3 .running (by decide) + sched.TS_RUNNING (by unfold sched.TS_RUNNING; rfl) + (NoBlockE.updT hks3 _ (Or.inr (Or.inl ⟨rfl, (by rw [hst3]; exact fun h => by cases h), Or.inl hcls⟩)))) ?_ + rintro s4 ⟨hR4, hS4, hfp4⟩ + obtain ⟨hk4, hq4, -, -⟩ := sts_facts hks3 hq3 .running + have hcore := (hO.coreKeep _ _ _ _ hSR hS4).onCore hsT hon + have hnq : NotQueued (stsAct (updK σ3 (specPtr sd) (.tcb { xs3 with tcbState := .running })) (specPtr sd)) + (specPtr sd) := (hO.notQueued _ _ sd xs3 hS3 hks3 hnr).sts _ _ _ + refine WP.spec_bind (WP.spec_and (hO.switchTo s4 _ sd _ hR4 hS4 hsT hk4 rfl hq4 hcore hnq) + (hF.switchTo s4 _ sd _ hR4 hS4 hsT hk4 rfl hq4 hcore hnq)) ?_ + rintro s5 ⟨⟨hnf, σ', hm, hf, hv, hS5⟩, hw⟩ + rw [WP.spec_ok] + exact ⟨rfl, hnf, σ', hm, hR4.frame hP hf hv, hS5, + FpA.trans hF (FpA.trans hF hpre hzb hfp4) (hzb.trans ((ssz_updK_tcb _ _ _).trans (ssz_stsAct _ _))) + (FpA.of_wr hw)⟩ + +open Fl4SpecA64 in +/-- The receive-from-sender program after the sender's sporadic check. -/ +def recvSendAfterCheckA (s3 : S) (a : A) (t sd rv : U64) (dd : SenderPayload) : Result (S × A) := do + let ftype ← obj.Tcb.fault_type st sd s3 + let inactive := do + let s4 ← sched.set_thread_state st s3 sd sched.TS_INACTIVE + ok (s4, a) + let push := do + let sc ← obj.Tcb.sc st sd s3 + let donate ← if sc != 0#u64 then ok (ftype != fl4_abi.consts.fault.Timeout) else ok false + ipc.reply_push st ar s3 a sd t rv donate + let withReply := if rv != 0#u64 then push else inactive + let callTail := if dd.senderCanGrant then withReply else + if dd.senderCanGrantReply then withReply else inactive + if dd.senderIsCall then callTail + else if ftype != fl4_abi.consts.fault.NullFault then callTail + else + let s4 ← sched.set_thread_state st s3 sd sched.TS_RUNNING + let s5 ← sched.possible_switch_to st s4 sd + ok (s5, a) + +open Fl4SpecA64 in +/-- The receive-from-sender program after transfer, beginning with the sender's sporadic check. -/ +def recvSendAfterTransferA (s2 : S) (a : A) (t sd rv : U64) (dd : SenderPayload) : Result (S × A) := do + let s3 ← endpoint.unblock_sporadic st s2 sd + recvSendAfterCheckA (st := st) (ar := ar) s3 a t sd rv dd + +open Fl4SpecA64 in +/-- Share the dequeue and transfer with the endpoint reply-push proof. -/ +theorem receive_from_senderContA (hO : SchedOpsEA st T E R Bf SR) + (hP : EPlaced T E R Bf) {th : TypedHeapA} {kb : U64} (hPl : PlaceA th kb) + (s : S) (a : A) (σ : State Unit) (hR : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) + (hSt : StateRelA th (L.mem s) σ) (hV : SlotsValidA th (L.mem s)) (hC : BufCover st T Bf s σ) + (hNative : BufNativeA T (L.mem s)) + (hoff : ∀ y, IsSlotA th y → ¬ EWrite T E R Bf y ∧ ¬ EWrite T E R Bf (core.num.U64.wrapping_add y 8#u64)) + {t : U64} (ht : t ∈ T) (hth : th t = some .tcb) {xt : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hnb : tsCode xt.tcbState ≠ 4) + {e sd : U64} (he : e ∈ E) {q : List ObjRef} + (hk : σ.kheap (specPtr e) = some (.endpoint (.sendEP (specPtr sd :: q)))) + (hsh : th sd = some .tcb) {xs : Tcb} (hks : σ.kheap (specPtr sd) = some (.tcb xs)) + (hu : ∀ rp, ipcBufOfA true xt = some rp → ∀ k, k < 128 → inUserFrame (bufAddr rp k) σ) + (rv : U64) + (hcase : ∀ e' dd, xs.tcbState = .blockedOnSend e' dd → + (xs.tcbFault = none ∧ (dd.senderCanGrant = false ∨ ipcBufOfA false xs = none ∨ + unat (dataToMessageInfo (xs.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0)) ∨ + ∃ f, xs.tcbFault = some f ∧ FaultOKA (L := L) s sd (ipcBufOfA true xt) xs f) + {Post : S × A → Prop} + (hFinish : ∀ {s2 : S} {σ2 : State Unit} {dd : SenderPayload}, + sd ∈ T → t ≠ sd → specPtr sd ≠ specPtr e → + xs.tcbState = .blockedOnSend (specPtr e) dd → + EpStateRel T E R Bf (L.mem s2) σ2 → SR s2 σ2 → + WritesIn st s s2 (EWrite T E R Bf) → EpStep σ σ2 → + σ2.kheap (specPtr sd) = some (.tcb xs) → + (∀ e' ∈ E, ∀ z, σ2.kheap (specPtr e') = some (.endpoint z) → specPtr sd ∉ z.epQueue) → + (doIpcTransfer (Z := Unit) (specPtr sd) (some (specPtr e)) dd.senderBadge dd.senderCanGrant (specPtr t) + (updK σ (specPtr e) (.endpoint (epDeq (.sendEP (specPtr sd :: q)) (specPtr sd)))) = + ({((), σ2)}, False)) → + recvSendAfterTransferA (st := st) (ar := ar) s2 a t sd rv dd ⦃ Post ⦄) : + endpoint.receive_from_sender st ar s a t e rv ⦃ Post ⦄ := by + obtain ⟨-, xs0, hxs0, hq, hQ, hB⟩ := hR.at_ep he hk + obtain ⟨sd', xs', rfl⟩ := List.exists_cons_of_ne_nil hxs0 + simp only [List.map_cons, List.cons.injEq] at hq + obtain ⟨hd', hq'⟩ := hq + have hdd : sd = sd' := specPtr_inj hd' + subst hdd + have hsT : sd ∈ T := hQ.mem sd (by simp) + have hsa := hP.tcb sd hsT + have hxs := hR.at_tcb hsT hks + have hxt := hR.at_tcb ht hkt + have hs4 : (L.mem s (stA sd)).val = 4 := (hB sd (by simp)).1 + have hsb : L.mem s (tBlockA sd) = e := (hB sd (by simp)).2 + obtain ⟨e', dd, hstd⟩ := tsCode_four (hxs.st.symm.trans hs4) + have he' : e' = specPtr e := by + have := hxs.blk e' (by rw [hstd]; rfl); rw [hsb] at this; exact this.symm + subst he' + obtain ⟨hbadge, hflags⟩ := hxs.send _ _ hstd + have hts : t ≠ sd := by intro h; subst h; exact hnb (hxt.st.symm.trans hs4) + have hse : specPtr sd ≠ specPtr e := kheap_ne hks hk (fun _ h1 h2 => by subst h1; cases h2) + have hte : specPtr t ≠ specPtr e := kheap_ne hkt hk (fun _ h1 h2 => by subst h1; cases h2) + have hts' : specPtr t ≠ specPtr sd := fun h => hts (specPtr_inj h) + -- FL4: the queue's head and the dequeue + unfold endpoint.receive_from_sender + step with endpoint.get_queue.spec as ⟨q0, hq0⟩ + have hhead : q0.head = sd := by rw [hq0, epQ_eq]; exact hQ.head + rw [hhead] + have hd : specPtr sd ∈ (Endpoint.sendEP (specPtr sd :: q)).epQueue := by simp [Endpoint.epQueue] + refine WP.spec_bind (ep_dequeue_raw hP s σ hR he hk hd) ?_ + rintro s1 ⟨-, hR1, hW1, hE1, hA1⟩ + have hS1 := hO.stable _ _ _ _ hSR hW1 hE1 (NoBlockE.updO _ _ fun _ h => by cases h) + have hkt1 : (updK σ (specPtr e) (.endpoint (epDeq (.sendEP (specPtr sd :: q)) (specPtr sd)))).kheap (specPtr t) = + some (.tcb xt) := by rw [updK_kheap, Function.update_of_ne hte, hkt] + have hks1 : (updK σ (specPtr e) (.endpoint (epDeq (.sendEP (specPtr sd :: q)) (specPtr sd)))).kheap (specPtr sd) = + some (.tcb xs) := by rw [updK_kheap, Function.update_of_ne hse, hks] + -- the sender's payload words + have hbw : L.mem s1 (tBadgeA sd) = L.mem s (tBadgeA sd) := hA1 _ (a_badge hsa).1 ⟨sd, hsT, (a_badge hsa).2⟩ + have hfw : L.mem s1 (tFlagsA sd) = L.mem s (tFlagsA sd) := hA1 _ (a_flags hsa).1 ⟨sd, hsT, (a_flags hsa).2⟩ + step with obj.Tcb.block_badge.spec as ⟨bdg, hbdg⟩ + step with obj.Tcb.block_flags.spec as ⟨fl, hfl⟩ + simp only [lift, bind_ok] + have hg : (fl &&& endpoint.FLAG_GRANT != 0#u64) = dd.senderCanGrant := by + rw [hfl, hfw, hflags]; exact flags_grantA dd + have hgr : (fl &&& endpoint.FLAG_GRANT_REPLY != 0#u64) = dd.senderCanGrantReply := by + rw [hfl, hfw, hflags]; exact flags_grantReplyA dd + have hc : (fl &&& endpoint.FLAG_CALL != 0#u64) = dd.senderIsCall := by + rw [hfl, hfw, hflags]; exact flags_callA dd + have hbb : bdg.bv = dd.senderBadge := by rw [hbdg, hbw]; exact hbadge + rw [hg, hgr, hc] + -- the transfer's placement at the dequeued state + have hmem : ∀ y, IsSlotA th y → L.mem s1 y = L.mem s y ∧ + L.mem s1 (core.num.U64.wrapping_add y 8#u64) = L.mem s (core.num.U64.wrapping_add y 8#u64) := by + intro y hy + have hal : Aligned y := by have := (IsSlotA.al32 hPl hy).1; unfold Aligned; omega + exact ⟨hW1.2.2 y hal (hoff y hy).1, hW1.2.2 _ (wa_al hal (by simp)) (hoff y hy).2⟩ + have hSt1 := hSt.updEpA hPl hmem hk (epDeq (.sendEP (specPtr sd :: q)) (specPtr sd)) + have hV1 : SlotsValidA th (L.mem s1) := slotsValid_xferA hV hmem + have hC1 : BufCover st T Bf s1 (updK σ (specPtr e) (.endpoint (epDeq (.sendEP (specPtr sd :: q)) (specPtr sd)))) := + BufCover.ewA hP hC hW1 fun u _ x hx => by + have hne : specPtr u ≠ specPtr e := fun h => by + rw [h, updK_kheap, Function.update_self] at hx; cases hx + rwa [updK_kheap, Function.update_of_ne hne] at hx + have hu1 : ∀ rp, ipcBufOfA true xt = some rp → ∀ k, k < 128 → + inUserFrame (bufAddr rp k) (updK σ (specPtr e) (.endpoint (epDeq (.sendEP (specPtr sd :: q)) (specPtr sd)))) := + fun rp hrp k hk' => inUserFrame_updK hk (fun _ h => by cases h) _ (hu rp hrp k hk') + -- the transfer + refine WP.spec_bind (do_ipc_transfer_anyA hO hP hPl s1 a _ hR1 hS1 hSt1 hV1 hC1 (hNative.ewA hP hW1) hsT ht (Ne.symm hts) hsh hth hks1 + hkt1 hu1 hoff e bdg dd.senderCanGrant + ((hcase _ _ hstd).imp id fun ⟨f, hf, hok⟩ => ⟨f, hf, FaultOKA.ewA hP hW1 hsT hok⟩) (some (specPtr e))) ?_ + rintro s2 ⟨σ2, hT, hpost⟩ + obtain ⟨hR2, hS2, -, hW2, hE2, hfr2, x2, hkt2, -⟩ := hpost + rw [hbb] at hT + have hks2 : σ2.kheap (specPtr sd) = some (.tcb xs) := by rw [hfr2 _ (Ne.symm hts'), hks1] + have hk1e : (updK σ (specPtr e) (.endpoint (epDeq (.sendEP (specPtr sd :: q)) (specPtr sd)))).kheap (specPtr e) = + some (.endpoint (epDeq (.sendEP (specPtr sd :: q)) (specPtr sd))) := by rw [updK_kheap, Function.update_self] + have hsb1 : L.mem s1 (tBlockA sd) = e := by rw [hA1 _ (a_blk hsa).1 ⟨sd, hsT, (a_blk hsa).2⟩, hsb] + have hq1 := dequeued_not_queued hR1 he hsb1 hk1e + (epDeq_not_mem (z := .sendEP (specPtr sd :: q)) (xs := sd :: xs') (by rw [hq']; rfl) hQ.nodup sd) + have hq2 : ∀ e'' ∈ E, ∀ z, σ2.kheap (specPtr e'') = some (.endpoint z) → specPtr sd ∉ z.epQueue := by + intro e'' he'' z hz + have hne : specPtr e'' ≠ specPtr t := by intro h; rw [h, hkt2] at hz; cases hz + exact hq1 e'' he'' z (by rwa [hfr2 _ hne] at hz) + exact hFinish hsT hts hse hstd hR2 hS2 (WritesIn.ew hW1 hW2) (hE1.trans hE2) hks2 hq2 hT + +open Fl4SpecA64 in +/-- **`receive_from_sender` refines l4v `receive_ipc`'s `SendEP` arm** (x86 `receive_ipc_send_gen` / +`receive_ipc_cfInactive_gen`): one spec outcome related (`EpStateRel ∧ SR`), the spec not failing. Both IPC buffers and +a faulted or non-faulted sender (`hcase`); branches (`hbr`, case selectors): a non-call sender without fault on the +current core runs; a call or faulted sender without a reply push becomes `Inactive`. -/ +theorem receive_from_senderA (hO : SchedOpsEA st T E R Bf SR) (hF : SchedFootEA st T E R Bf SR Wr) + (hP : EPlaced T E R Bf) {th : TypedHeapA} {kb : U64} (hPl : PlaceA th kb) + (s : S) (a : A) (σ : State Unit) (hR : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) + (hSt : StateRelA th (L.mem s) σ) (hV : SlotsValidA th (L.mem s)) (hC : BufCover st T Bf s σ) + (hNative : BufNativeA T (L.mem s)) + (hoff : ∀ y, IsSlotA th y → ¬ EWrite T E R Bf y ∧ ¬ EWrite T E R Bf (core.num.U64.wrapping_add y 8#u64)) + {t : U64} (ht : t ∈ T) (hth : th t = some .tcb) {xt : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hnb : tsCode xt.tcbState ≠ 4) + {e sd : U64} (he : e ∈ E) {q : List ObjRef} + (hk : σ.kheap (specPtr e) = some (.endpoint (.sendEP (specPtr sd :: q)))) + (hsh : th sd = some .tcb) {xs : Tcb} (hks : σ.kheap (specPtr sd) = some (.tcb xs)) + (hu : ∀ rp, ipcBufOfA true xt = some rp → ∀ k, k < 128 → inUserFrame (bufAddr rp k) σ) + (rv : U64) {rep : Option ObjRef} (hrv : optRef rv = rep) + (hcase : ∀ e' dd, xs.tcbState = .blockedOnSend e' dd → + (xs.tcbFault = none ∧ (dd.senderCanGrant = false ∨ ipcBufOfA false xs = none ∨ + unat (dataToMessageInfo (xs.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0)) ∨ + ∃ f, xs.tcbFault = some f ∧ FaultOKA (L := L) s sd (ipcBufOfA true xt) xs f) + (hbr : ∀ e' dd, xs.tcbState = .blockedOnSend e' dd → + (dd.senderIsCall = false ∧ xs.tcbFault = none ∧ L.mem s (affA sd) = L.cpu s) ∨ + ((dd.senderIsCall = true ∨ xs.tcbFault ≠ none) ∧ + ((dd.senderCanGrant = false ∧ dd.senderCanGrantReply = false) ∨ rep = none))) : + endpoint.receive_from_sender st ar s a t e rv ⦃ r => r.2 = a ∧ + ¬ (specRecvSendA (specPtr t) (specPtr e) rep (specPtr sd :: q) σ).2 ∧ + ∃ σ', ((), σ') ∈ (specRecvSendA (specPtr t) (specPtr e) rep (specPtr sd :: q) σ).1 ∧ + EpStateRel T E R Bf (L.mem r.1) σ' ∧ SR r.1 σ' ∧ FpA st T E R Bf Wr σ s r.1 ⦄ := by + refine receive_from_senderContA hO hP hPl s a σ hR hSR hSt hV hC hNative hoff ht hth hkt hnb + he hk hsh hks hu rv hcase ?_ + intro s2 σ2 dd hsT hts hse hstd hR2 hS2 hWf hE hks2 hq2 hT + unfold recvSendAfterTransferA recvSendAfterCheckA + -- the sporadic unblock of the sender, still blocked + have hnr : runnable xs.tcbState = false := by rw [hstd]; rfl + have hnq2 := hO.notQueued _ _ sd xs hS2 hks2 hnr + refine WP.spec_bind (WP.spec_and (hO.unblockSporadic s2 σ2 sd xs hR2 hS2 hsT hks2 hnr hnq2) + (hF.unblockSporadic s2 σ2 sd xs hR2 hS2 hsT hks2 hnr hnq2)) ?_ + rintro s3 ⟨⟨σ3, hA, hf3, hv3, hS3⟩, hw3⟩ + have hz3 : SSzA σ σ3 := (ssz_of_epStep hE).trans (hF.unblockSz _ _ _ hA) + have hpre3 : FpA st T E R Bf Wr σ s s3 := FpA.trans hF (FpA.of_ew hWf) (ssz_of_epStep hE) (FpA.of_wr hw3) + have hR3 := hR2.frame hP hf3 hv3 + obtain ⟨xs3, hks3, hs3st, -, -, hs3f, -⟩ := hv3.2.2.1 _ _ hks2 + have hq3 : ∀ e'' ∈ E, ∀ z, σ3.kheap (specPtr e'') = some (.endpoint z) → specPtr sd ∉ z.epQueue := by + intro e'' he'' z hz + obtain ⟨z2, hz2, -⟩ := hR2.ep e'' he'' + rw [hv3.1 _ _ hz2] at hz + injection hz with hz; injection hz with hz; subst hz + exact hq2 e'' he'' z2 hz2 + have hxs3 := hR3.at_tcb hsT hks3 + have hst3 : xs3.tcbState = .blockedOnSend (specPtr e) dd := hs3st.trans hstd + step with obj.Tcb.fault_type.spec as ⟨ft, hft⟩ + rcases hbr _ _ hstd with ⟨hcall, hfs, hon⟩ | ⟨hcf, hnp⟩ + · -- a non-call sender without fault runs + have hf3' : xs3.tcbFault = none := hs3f.trans hfs + have hft0 : ft = fl4_abi.consts.fault.NullFault := by + rw [hft, hxs3.fault0.2 hf3']; unfold fl4_abi.consts.fault.NullFault; rfl + simp only [hcall, hft0, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + rw [recvSend_runA rep hk hks hse hstd hT hks2 hA hks3 hcall hf3'] + exact recv_run_tailA hO hF hP a hSR hsT hon hR3 hS3 hks3 hst3 hf3' hq3 hpre3 hz3 + · -- a call or a faulted sender without a reply push becomes inactive + have hcf3 : dd.senderIsCall = true ∨ xs3.tcbFault ≠ none := hcf.imp id fun h => by rw [hs3f]; exact h + rw [recvSend_inactA rep hk hks hse hstd hT hks2 hA hks3 hcf3 hnp] + have hI := set_thread_state_relF hO hF hP s3 σ3 hR3 hS3 hsT hks3 hq3 .inactive (by decide) sched.TS_INACTIVE + (by unfold sched.TS_INACTIVE; rfl) + (NoBlockE.updT hks3 _ (Or.inr (Or.inr ⟨rfl, (by rw [hst3]; rfl), (by rw [hst3]; exact fun h => by cases h)⟩))) + have htail : (do + let s4 ← sched.set_thread_state st s3 sd sched.TS_INACTIVE + ok (s4, a)) ⦃ r => r.2 = a ∧ + ¬ (({((), stsAct (updK σ3 (specPtr sd) (.tcb { xs3 with tcbState := .inactive })) (specPtr sd))}, False) : + Set (Unit × State Unit) × Prop).2 ∧ + ∃ σ', ((), σ') ∈ (({((), stsAct (updK σ3 (specPtr sd) (.tcb { xs3 with tcbState := .inactive })) + (specPtr sd))}, False) : Set (Unit × State Unit) × Prop).1 ∧ + EpStateRel T E R Bf (L.mem r.1) σ' ∧ SR r.1 σ' ∧ FpA st T E R Bf Wr σ s r.1 ⦄ := + WP.spec_bind hI fun s4 h4 => by + rw [WP.spec_ok] + obtain ⟨h4a, h4b, h4c⟩ := h4 + exact ⟨rfl, not_false, _, Set.mem_singleton _, h4a, h4b, FpA.trans hF hpre3 hz3 h4c⟩ + have hrv0 : rep = none → rv = 0#u64 := fun h => optRef_none (hrv.trans h) + cases hcl : dd.senderIsCall with + | true => + simp only [↓reduceIte] + rcases hnp with ⟨hg0, hgr0⟩ | hrn + · simp only [hg0, hgr0, Bool.false_eq_true, ↓reduceIte] + exact htail + · simp only [hrv0 hrn, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte, ite_self] + exact htail + | false => + have hfn : xs3.tcbFault ≠ none := hcf3.resolve_left (by rw [hcl]; simp) + have hb1 : (ft != fl4_abi.consts.fault.NullFault) = true := by + rw [bne_iff_ne, hft] + intro h + apply hfn + apply hxs3.fault0.1 + rw [h]; unfold fl4_abi.consts.fault.NullFault; rfl + simp only [Bool.false_eq_true, hb1, ↓reduceIte] + rcases hnp with ⟨hg0, hgr0⟩ | hrn + · simp only [hg0, hgr0, Bool.false_eq_true, ↓reduceIte] + exact htail + · simp only [hrv0 hrn, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte, ite_self] + exact htail + +end Fl4 + +end + +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/EpRecvTopA.lean b/proofs/Fl4ProofsA64/P3/EpRecvTopA.lean new file mode 100644 index 000000000..6137a07d4 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/EpRecvTopA.lean @@ -0,0 +1,301 @@ +/- +P3 (aarch64, A64-P3-IPC gen 13): **`receive_ipc` without a bound notification refines l4v `receive_ipc`** (`Z = Unit`; +x86 `P3/EpRecv`'s preamble `receive_ipc_pre` / `receiveIpc_noSend` and `P3/EpRecvSend`'s `receiveIpc_send_eq`): the +reply cap (`take_reply` of a null cap or of a reply object linked to no thread), the bound-notification word (0), then +the endpoint's state word, against the spec's prefix down to `case ep of SendEP q ⇒ … | _ ⇒ receive_ipc_blocked`. + +* `ReplyArgA` / `SpecReplyArgA` (x86 `ReplyArg` / `SpecReplyArg`), `replyArg_specA`; +* **`receive_ipc_preA`** (FL4's preamble), **`receiveIpc_epA`** (the spec's: the `SendEP` arm is gen 13's + `specRecvSendA`), `receiveIpc_sendA` / `receiveIpc_waitA`; +* **`receive_ipc_sendA`** (a waiting sender: `receive_from_senderA`) and **`receive_ipc_waitA`** (no waiting sender: + `wait_for_sender_blockA` / `wait_for_sender_pollA`). + +Case selectors (no named premise): the receiver without a bound notification (`hbn`; the bound-notification prefix, +`complete_signal` / `maybe_return_sc`, is gen 14's), the reply cap as `ReplyArgA` (a reply object still linked to a +thread needs `take_reply`'s `cancel_ipc`, gen 14), and the selectors of the arms. +-/ +import Fl4ProofsA64.P3.EpRecvSendA +import Fl4ProofsA64.P3.EpRecvBlkA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched + +noncomputable section + +/-- FL4's reply cap `rc` and the spec's `rc'` name the reply `rv` (0 for none), a reply object in `R` linked to no +thread (x86 `ReplyArg`). -/ +def ReplyArgA (R : Set U64) (m : U64 → U64) (rc : capability.Cap) (rc' : Fl4SpecA64.Cap) (rv : U64) : Prop := + (rc = .Null ∧ rc' = .nullCap ∧ rv = 0#u64) ∨ + (∃ b r', rc = .Reply rv b ∧ rc' = .replyCap (specPtr rv) r' ∧ rv ∈ R ∧ m (rTcbA rv) = 0#u64) + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] {T E R : Set U64} + {Bf : Set (U64 × Fl4SpecA64.Word)} + +variable (st ar) in +/-- (gen 14) **`receive_ipc`'s endpoint dispatch** (the program after the bound-notification check): the state word, +then `wait_for_sender` or `receive_from_sender`. -/ +def rcvDispA (s : S) (a : A) (t e : U64) (blk : Bool) (rv : U64) : Result (S × A) := do + let st1 ← obj.Ep.state st e s + if st1 = obj.EP_IDLE then do + let s3 ← endpoint.wait_for_sender st s t e blk rv + ok (s3, a) + else if st1 = obj.EP_RECV then do + let s3 ← endpoint.wait_for_sender st s t e blk rv + ok (s3, a) + else endpoint.receive_from_sender st ar s a t e rv + +/-- **What `receive_ipc` does before looking at the endpoint**, without a bound notification and with a reply cap as in +`ReplyArgA` (x86 `receive_ipc_pre`). -/ +theorem receive_ipc_preA (s : S) (a : A) (t e cb : U64) (c1 c2 c3 c4 blk : Bool) (rc : capability.Cap) + (rc' : Fl4SpecA64.Cap) (rv : U64) (hrc : ReplyArgA R (L.mem s) rc rc' rv) (hbound : L.mem s (tBoundA t) = 0#u64) + {P : S × A → Prop} (h : rcvDispA st ar s a t e blk rv ⦃ P ⦄) : + endpoint.receive_ipc st ar s a t (.Endpoint e cb c1 c2 c3 c4) blk rc ⦃ P ⦄ := by + unfold endpoint.receive_ipc + simp only [capability.Cap.ptr, bind_tc_ok] + rcases hrc with ⟨rfl, -, rfl⟩ | ⟨b, r', rfl, -, -, h0⟩ + · simp only [endpoint.take_reply, bind_tc_ok] + step with obj.Tcb.bound_ntfn.spec as ⟨i, hi⟩ + simp only [hi, hbound, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + simp only [bind_tc_ok, bind_ok, Bind.bind] + exact h + · simp only [endpoint.take_reply] + step with obj.Reply.tcb.spec as ⟨i, hi⟩ + step with obj.Tcb.is_null.spec as ⟨bn, hbn⟩ + simp only [hbn, hi, h0, decide_true, ↓reduceIte, bind_tc_ok] + step with obj.Tcb.bound_ntfn.spec as ⟨j, hj⟩ + simp only [hj, hbound, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + simp only [bind_tc_ok, bind_ok, Bind.bind] + exact h + +end + +/-! ## The spec's preamble -/ + +section Spec +open Fl4SpecA64 Fl4Spec + +/-- The spec's reply cap `rc'` names `reply`, a reply object linked to no thread (x86 `SpecReplyArg`). -/ +def SpecReplyArgA (σ : State Unit) (rc' : Cap) (reply : Option ObjRef) : Prop := + (rc' = .nullCap ∧ reply = none) ∨ + (∃ r rr x, rc' = .replyCap r rr ∧ reply = some r ∧ σ.kheap r = some (.reply x) ∧ x.replyTcb = none) + +/-- **The spec's `receive_ipc` without a bound notification**, down to the endpoint's case split (x86 +`receiveIpc_noSend` / `receiveIpc_send_eq`'s prefix): the `SendEP` arm is `specRecvSendA`. -/ +theorem receiveIpc_epA {σ : State Unit} {t e : ObjRef} (b : Badge) (rights : CapRights) (blk : Bool) + {rc' : Cap} {reply : Option ObjRef} (hrc : SpecReplyArgA σ rc' reply) {xt : Tcb} + (hkt : σ.kheap t = some (.tcb xt)) (hb : xt.tcbBoundNotification = none) {x : Endpoint} + (hk : σ.kheap e = some (.endpoint x)) : + receiveIpc (Z := Unit) t (.endpointCap e b rights) blk rc' σ = + (match x with + | .sendEP q => specRecvSendA t e reply q + | _ => receiveIpcBlocked blk t e reply) σ := by + unfold receiveIpc + dsimp only + rw [nd_pure_bind'] + rcases hrc with ⟨rfl, rfl⟩ | ⟨r, rr, xr, rfl, rfl, hkr, hr⟩ + · dsimp only + rw [nd_pure_bind', nd_bind_one (getEndpoint_eq hk), nd_bind_one (getTcbObjRef_eq _ hkt)] + simp only [hb] + rw [nd_pure_bind'] + rw [if_neg (by simp : ¬ ((none : Option ObjRef) ≠ none ∧ isActive defaultNotification = true))] + rw [nd_when_false_bindA (by simp [hb])] + cases x <;> rfl + · dsimp only + rw [nd_bind_one (getReplyTcb_eq hkr)] + simp only [hr] + rw [nd_when_false_bindA (by simp [hr])] + rw [nd_pure_bind', nd_bind_one (getEndpoint_eq hk), nd_bind_one (getTcbObjRef_eq _ hkt)] + simp only [hb] + rw [nd_pure_bind'] + rw [if_neg (by simp : ¬ ((none : Option ObjRef) ≠ none ∧ isActive defaultNotification = true))] + rw [nd_when_false_bindA (by simp [hb])] + cases x <;> rfl + +theorem receiveIpc_sendA {σ : State Unit} {t e : ObjRef} (b : Badge) (rights : CapRights) (blk : Bool) + {rc' : Cap} {reply : Option ObjRef} (hrc : SpecReplyArgA σ rc' reply) {xt : Tcb} + (hkt : σ.kheap t = some (.tcb xt)) (hb : xt.tcbBoundNotification = none) {q : List ObjRef} + (hk : σ.kheap e = some (.endpoint (.sendEP q))) : + receiveIpc (Z := Unit) t (.endpointCap e b rights) blk rc' σ = specRecvSendA t e reply q σ := + receiveIpc_epA b rights blk hrc hkt hb hk + +theorem receiveIpc_waitA {σ : State Unit} {t e : ObjRef} (b : Badge) (rights : CapRights) (blk : Bool) + {rc' : Cap} {reply : Option ObjRef} (hrc : SpecReplyArgA σ rc' reply) {xt : Tcb} + (hkt : σ.kheap t = some (.tcb xt)) (hb : xt.tcbBoundNotification = none) {x : Endpoint} + (hk : σ.kheap e = some (.endpoint x)) (hc : CanBlockA true x) : + receiveIpc (Z := Unit) t (.endpointCap e b rights) blk rc' σ = receiveIpcBlocked blk t e reply σ := by + rw [receiveIpc_epA b rights blk hrc hkt hb hk] + cases x with + | idleEP => rfl + | recvEP q => rfl + | sendEP q => simp [CanBlockA] at hc + +end Spec + +/-! ## `receive_ipc` -/ + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] {T E R : Set U64} + {Bf : Set (U64 × Fl4SpecA64.Word)} {SR : S → Fl4SpecA64.State Unit → Prop} + {Wr : Fl4SpecA64.State Unit → S → U64 → Prop} + +/-- The spec's reply argument from FL4's (x86 `replyArg_spec`), and the reply in `R` if any. -/ +theorem replyArg_specA (hP : EPlaced T E R Bf) {s : S} {σ : Fl4SpecA64.State Unit} + (hR : EpStateRel T E R Bf (L.mem s) σ) {rc : capability.Cap} {rc' : Fl4SpecA64.Cap} {rv : U64} + (h : ReplyArgA R (L.mem s) rc rc' rv) : + SpecReplyArgA σ rc' (optRef rv) ∧ (optRef rv ≠ none → rv ∈ R) := by + rcases h with ⟨-, rfl, rfl⟩ | ⟨b, r', -, rfl, hr, h0⟩ + · exact ⟨Or.inl ⟨rfl, optRef_zero⟩, fun h => absurd optRef_zero h⟩ + · have hne : rv ≠ 0#u64 := fun h => (hP.rep rv hr).2.1 (by rw [h]; rfl) + obtain ⟨x, hk, hx⟩ := hR.reply rv hr + exact ⟨Or.inr ⟨_, r', x, rfl, optRef_ne hne, hk, by rw [hx, h0, optRef_zero]⟩, fun _ => hr⟩ + +open Fl4SpecA64 in +/-- (gen 14) **The endpoint dispatch on a waiting sender** (`receive_ipc_sendA`'s tail, at any state after the +reply cap and the bound notification): the state word `EP_SEND`, then `receive_from_senderA`. -/ +theorem rcv_disp_sendA (hO : SchedOpsEA st T E R Bf SR) (hF : SchedFootEA st T E R Bf SR Wr) + (hP : EPlaced T E R Bf) {th : TypedHeapA} {kb : U64} (hPl : PlaceA th kb) + (s : S) (a : A) (σ : State Unit) (hR : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) + (hSt : StateRelA th (L.mem s) σ) (hV : SlotsValidA th (L.mem s)) (hC : BufCover st T Bf s σ) + (hNative : BufNativeA T (L.mem s)) + (hoff : ∀ y, IsSlotA th y → ¬ EWrite T E R Bf y ∧ ¬ EWrite T E R Bf (core.num.U64.wrapping_add y 8#u64)) + {t : U64} (ht : t ∈ T) (hth : th t = some .tcb) {xt : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hnb : tsCode xt.tcbState ≠ 4) + {e sd : U64} (he : e ∈ E) {q : List ObjRef} + (hk : σ.kheap (specPtr e) = some (.endpoint (.sendEP (specPtr sd :: q)))) + (hsh : th sd = some .tcb) {xs : Tcb} (hks : σ.kheap (specPtr sd) = some (.tcb xs)) + (hu : ∀ rp, ipcBufOfA true xt = some rp → ∀ k, k < 128 → inUserFrame (bufAddr rp k) σ) + (blk : Bool) (rv : U64) + (hcase : ∀ e' dd, xs.tcbState = .blockedOnSend e' dd → + (xs.tcbFault = none ∧ (dd.senderCanGrant = false ∨ ipcBufOfA false xs = none ∨ + unat (dataToMessageInfo (xs.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0)) ∨ + ∃ f, xs.tcbFault = some f ∧ FaultOKA (L := L) s sd (ipcBufOfA true xt) xs f) + (hbr : ∀ e' dd, xs.tcbState = .blockedOnSend e' dd → + (dd.senderIsCall = false ∧ xs.tcbFault = none ∧ L.mem s (affA sd) = L.cpu s) ∨ + ((dd.senderIsCall = true ∨ xs.tcbFault ≠ none) ∧ + ((dd.senderCanGrant = false ∧ dd.senderCanGrantReply = false) ∨ optRef rv = none))) : + rcvDispA st ar s a t e blk rv ⦃ r => r.2 = a ∧ + ¬ (specRecvSendA (specPtr t) (specPtr e) (optRef rv) (specPtr sd :: q) σ).2 ∧ + ∃ σ', ((), σ') ∈ (specRecvSendA (specPtr t) (specPtr e) (optRef rv) (specPtr sd :: q) σ).1 ∧ + EpStateRel T E R Bf (L.mem r.1) σ' ∧ SR r.1 σ' ∧ FpA st T E R Bf Wr σ s r.1 ⦄ := by + obtain ⟨hst1, -⟩ := hR.at_ep he hk + unfold rcvDispA + step with obj.Ep.state.spec as ⟨st0, hst0⟩ + rw [epState_eq, hst1] at hst0 + subst hst0 + rw [if_neg (by rw [ep_state_vals.1]; decide), if_neg (by rw [ep_state_vals.2.2]; decide)] + exact receive_from_senderA hO hF hP hPl s a σ hR hSR hSt hV hC hNative hoff ht hth hkt hnb he hk hsh hks hu rv rfl hcase hbr + +open Fl4SpecA64 in +/-- **`receive_ipc` from a waiting sender** (x86 `receive_ipc_send_gen`): the preamble, then +`receive_from_senderA` (gen 14: by `rcv_disp_sendA`). -/ +theorem receive_ipc_sendA (hO : SchedOpsEA st T E R Bf SR) (hF : SchedFootEA st T E R Bf SR Wr) + (hP : EPlaced T E R Bf) {th : TypedHeapA} {kb : U64} (hPl : PlaceA th kb) + (s : S) (a : A) (σ : State Unit) (hR : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) + (hSt : StateRelA th (L.mem s) σ) (hV : SlotsValidA th (L.mem s)) (hC : BufCover st T Bf s σ) + (hNative : BufNativeA T (L.mem s)) + (hoff : ∀ y, IsSlotA th y → ¬ EWrite T E R Bf y ∧ ¬ EWrite T E R Bf (core.num.U64.wrapping_add y 8#u64)) + {t : U64} (ht : t ∈ T) (hth : th t = some .tcb) {xt : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hnb : tsCode xt.tcbState ≠ 4) (hbn : xt.tcbBoundNotification = none) + {e sd : U64} (he : e ∈ E) {q : List ObjRef} + (hk : σ.kheap (specPtr e) = some (.endpoint (.sendEP (specPtr sd :: q)))) + (hsh : th sd = some .tcb) {xs : Tcb} (hks : σ.kheap (specPtr sd) = some (.tcb xs)) + (hu : ∀ rp, ipcBufOfA true xt = some rp → ∀ k, k < 128 → inUserFrame (bufAddr rp k) σ) + (cb : U64) (c1 c2 c3 c4 : Bool) {rc : capability.Cap} {rc' : Cap} {rv : U64} + (hrc : ReplyArgA R (L.mem s) rc rc' rv) (b : Badge) (rights : CapRights) (blk : Bool) + (hcase : ∀ e' dd, xs.tcbState = .blockedOnSend e' dd → + (xs.tcbFault = none ∧ (dd.senderCanGrant = false ∨ ipcBufOfA false xs = none ∨ + unat (dataToMessageInfo (xs.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0)) ∨ + ∃ f, xs.tcbFault = some f ∧ FaultOKA (L := L) s sd (ipcBufOfA true xt) xs f) + (hbr : ∀ e' dd, xs.tcbState = .blockedOnSend e' dd → + (dd.senderIsCall = false ∧ xs.tcbFault = none ∧ L.mem s (affA sd) = L.cpu s) ∨ + ((dd.senderIsCall = true ∨ xs.tcbFault ≠ none) ∧ + ((dd.senderCanGrant = false ∧ dd.senderCanGrantReply = false) ∨ optRef rv = none))) : + endpoint.receive_ipc st ar s a t (.Endpoint e cb c1 c2 c3 c4) blk rc ⦃ r => r.2 = a ∧ + ¬ (receiveIpc (Z := Unit) (specPtr t) (.endpointCap (specPtr e) b rights) blk rc' σ).2 ∧ + ∃ σ', ((), σ') ∈ (receiveIpc (Z := Unit) (specPtr t) (.endpointCap (specPtr e) b rights) blk rc' σ).1 ∧ + EpStateRel T E R Bf (L.mem r.1) σ' ∧ SR r.1 σ' ∧ FpA st T E R Bf Wr σ s r.1 ⦄ := by + have hxt := hR.at_tcb ht hkt + have hbound : L.mem s (tBoundA t) = 0#u64 := optRef_none (hxt.bound.symm.trans hbn) + obtain ⟨hsr, -⟩ := replyArg_specA hP hR hrc + rw [receiveIpc_sendA b rights blk hsr hkt hbn hk] + exact receive_ipc_preA s a t e cb c1 c2 c3 c4 blk rc rc' rv hrc hbound + (rcv_disp_sendA hO hF hP hPl s a σ hR hSR hSt hV hC hNative hoff ht hth hkt hnb he hk hsh hks hu blk rv hcase hbr) + +/-- (gen 14) **The endpoint dispatch with no waiting sender** (`receive_ipc_waitA`'s tail, at any state after the +reply cap and the bound notification): `wait_for_sender_blockA` or `wait_for_sender_pollA`; one spec outcome. -/ +theorem rcv_disp_waitA (hO : SchedOpsEA st T E R Bf SR) (hF : SchedFootEA st T E R Bf SR Wr) + (hP : EPlaced T E R Bf) (s : S) (a : A) + (σ : Fl4SpecA64.State Unit) (hR : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) + {t : U64} (ht : t ∈ T) {xt : Fl4SpecA64.Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + {e : U64} (he : e ∈ E) {x : Fl4SpecA64.Endpoint} (hk : σ.kheap (specPtr e) = some (.endpoint x)) + (hc : CanBlockA true x) (rv : U64) (hrR : optRef rv ≠ none → rv ∈ R) (blk : Bool) + (hblk : blk = true → SndRunA σ (specPtr t) xt ∧ + ∀ xs, QueueRel (L.mem s) T .Ep (qOf (L.mem s) e) xs → PrioSorted (fun y => L.mem s (prioA y)) xs) : + rcvDispA st ar s a t e blk rv ⦃ r => r.2 = a ∧ ∃ σ', + Fl4SpecA64.receiveIpcBlocked (Z := Unit) blk (specPtr t) (specPtr e) (optRef rv) σ = ({((), σ')}, False) ∧ + EpStateRel T E R Bf (L.mem r.1) σ' ∧ SR r.1 σ' ∧ FpA st T E R Bf Wr σ s r.1 ⦄ := by + have hx := hR.at_ep he hk + have hwait : (do + let s3 ← endpoint.wait_for_sender st s t e blk rv + ok (s3, a)) ⦃ r => r.2 = a ∧ ∃ σ', + Fl4SpecA64.receiveIpcBlocked (Z := Unit) blk (specPtr t) (specPtr e) (optRef rv) σ = ({((), σ')}, False) ∧ + EpStateRel T E R Bf (L.mem r.1) σ' ∧ SR r.1 σ' ∧ FpA st T E R Bf Wr σ s r.1 ⦄ := by + cases blk with + | false => + refine WP.spec_bind (wait_for_sender_pollA (Wr := Wr) hO hP s σ hR hSR ht hkt e rv (optRef rv)) ?_ + rintro s3 ⟨σ', h1, h2, h3, h4⟩ + rw [WP.spec_ok] + exact ⟨rfl, σ', h1, h2, h3, h4⟩ + | true => + obtain ⟨hrun, hsort⟩ := hblk rfl + refine WP.spec_bind (wait_for_sender_blockA hO hF hP s σ hR hSR ht hkt hrun he hk hc hsort rv rfl hrR) ?_ + rintro s3 ⟨σ', h1, h2, h3, h4⟩ + rw [WP.spec_ok] + exact ⟨rfl, σ', h1, h2, h3, h4⟩ + unfold rcvDispA + step with obj.Ep.state.spec as ⟨st0, hst0⟩ + rw [epState_eq] at hst0 + cases x with + | idleEP => + rw [hx.1] at hst0 + subst hst0 + rw [if_pos ep_state_vals.1.symm] + exact hwait + | recvEP q => + rw [hx.1] at hst0 + subst hst0 + rw [if_neg (by rw [ep_state_vals.1]; decide), if_pos ep_state_vals.2.2.symm] + exact hwait + | sendEP q => simp [CanBlockA] at hc + +/-- **`receive_ipc` with no waiting sender** (x86 `receive_ipc_block` / `receive_ipc_poll`): the preamble, then +`wait_for_sender_blockA` (blocking: the receiver's `SndRunA` and the queue's sortedness) or `wait_for_sender_pollA`; +one spec outcome, related (gen 14: by `rcv_disp_waitA`). -/ +theorem receive_ipc_waitA (hO : SchedOpsEA st T E R Bf SR) (hF : SchedFootEA st T E R Bf SR Wr) + (hP : EPlaced T E R Bf) (s : S) (a : A) + (σ : Fl4SpecA64.State Unit) (hR : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) + {t : U64} (ht : t ∈ T) {xt : Fl4SpecA64.Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hbn : xt.tcbBoundNotification = none) + {e : U64} (he : e ∈ E) {x : Fl4SpecA64.Endpoint} (hk : σ.kheap (specPtr e) = some (.endpoint x)) + (hc : CanBlockA true x) + (cb : U64) (c1 c2 c3 c4 : Bool) {rc : capability.Cap} {rc' : Fl4SpecA64.Cap} {rv : U64} + (hrc : ReplyArgA R (L.mem s) rc rc' rv) (b : Fl4SpecA64.Badge) (rights : Fl4SpecA64.CapRights) (blk : Bool) + (hblk : blk = true → SndRunA σ (specPtr t) xt ∧ + ∀ xs, QueueRel (L.mem s) T .Ep (qOf (L.mem s) e) xs → PrioSorted (fun y => L.mem s (prioA y)) xs) : + endpoint.receive_ipc st ar s a t (.Endpoint e cb c1 c2 c3 c4) blk rc ⦃ r => r.2 = a ∧ ∃ σ', + Fl4SpecA64.receiveIpc (Z := Unit) (specPtr t) (.endpointCap (specPtr e) b rights) blk rc' σ = + ({((), σ')}, False) ∧ EpStateRel T E R Bf (L.mem r.1) σ' ∧ SR r.1 σ' ∧ FpA st T E R Bf Wr σ s r.1 ⦄ := by + have hxt := hR.at_tcb ht hkt + have hbound : L.mem s (tBoundA t) = 0#u64 := optRef_none (hxt.bound.symm.trans hbn) + obtain ⟨hsr, hrR⟩ := replyArg_specA hP hR hrc + rw [receiveIpc_waitA b rights blk hsr hkt hbn hk hc] + exact receive_ipc_preA s a t e cb c1 c2 c3 c4 blk rc rc' rv hrc hbound + (rcv_disp_waitA hO hF hP s a σ hR hSR ht hkt he hk hc rv hrR blk hblk) + +end + +end + +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/EpRegsA.lean b/proofs/Fl4ProofsA64/P3/EpRegsA.lean new file mode 100644 index 000000000..0b0f52e08 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/EpRegsA.lean @@ -0,0 +1,176 @@ +/- +P3 (aarch64, A64-P3-IPC gen 2): **registers of a saved user context, and the front end's two views as projections of +`EpStateRel`** (x86 `P3/EpRegs`, plus the A64 link to S3's `FrontEndExtraA`). + +* `write_reg_rel` (x86's): FL4's `set_reg t (regIdxA r) v` is related to the spec's `as_user t (setRegister r v)` + (`setReg`): one register word changes, the thread state does not; `TcbRelE.set_reg`, `EpStateRel.at_ep` / + `at_tcb` / `at_reply`. +* **The register map is FrontEndExtraA's**: `regA = regWA`, `bufW = bufWA`, `loadWordAt = loadWordAtA`, `bufAddr = + bufAddrA` (all `rfl`). `msg_register_regIdxA`: FL4's `msg::msg_register i` (`X2..X5`) is `regIdxA` of l4v's + `msgRegisters[i]` (`[X2 .. X5]`). +* **`EpStateRel.regView`**: `RegViewA` (the current TCB's message-info and message registers are the spec TCB's) for + every TCB of `T` — a projection of `TcbRelE.regs`. +* **`EpStateRel.bufView`**: `BufViewA` for every tracked buffer `(buf, sb) ∈ Bf` with `buf ≠ 0` — a projection of + `EpStateRel.buf`; `bufViewA_none` for `buf = 0` with no spec buffer. (Which `(buf, sb)` a TCB's `IPC_BUFFER` word and + l4v's `lookup_ipc_buffer` give is x86 `IpcBuf`'s `lookup_buf_full` / `BufCover`, to be ported with the IPC-buffer + caps; the view itself needs nothing else.) + +So `FrontEndExtraA`'s decode-site hypotheses `RegViewA` / `BufViewA` follow from `EpStateRel` once it is a `JAtZA` +projection (with `BufCover`'s placement). No named premise. No `Fl4SpecA64` edit. +-/ +import Fl4ProofsA64.P3.EpQueueA +import Fl4ProofsA64.P3.FrontEndExtraA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif + +noncomputable section + +section +variable {T E R : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + +theorem EpStateRel.at_ep {m : U64 → U64} {σ : Fl4SpecA64.State Unit} (hR : EpStateRel T E R Bf m σ) {e : U64} + (he : e ∈ E) {x : Fl4SpecA64.Endpoint} (hk : σ.kheap (specPtr e) = some (.endpoint x)) : EpObjRel m T e x := by + obtain ⟨y, hky, hy⟩ := hR.ep e he + rw [hk] at hky; cases hky; exact hy + +theorem EpStateRel.at_tcb {m : U64 → U64} {σ : Fl4SpecA64.State Unit} (hR : EpStateRel T E R Bf m σ) {t : U64} + (ht : t ∈ T) {x : Fl4SpecA64.Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) : TcbRelE R m t x := by + obtain ⟨y, hky, hy⟩ := hR.tcb t ht + rw [hk] at hky; cases hky; exact hy + +theorem EpStateRel.at_reply {m : U64 → U64} {σ : Fl4SpecA64.State Unit} (hR : EpStateRel T E R Bf m σ) {r : U64} + (hr : r ∈ R) {x : Fl4SpecA64.Reply} (hk : σ.kheap (specPtr r) = some (.reply x)) : + x.replyTcb = optRef (m (rTcbA r)) := by + obtain ⟨y, hky, hy⟩ := hR.reply r hr + rw [hk] at hky; cases hky; exact hy + +theorem tf_ne_of {t a b : U64} (hb : ¬ TF t b) (ha : TF t a) : b.val ≠ a.val := fun e => hb (ha.of_val e) + +theorem setReg_regs (x : Fl4SpecA64.Tcb) (r : Fl4SpecA64.Register) (v : Fl4SpecA64.Word) : + (setReg x r v).tcbArch.tcbContext.userRegs = Function.update x.tcbArch.tcbContext.userRegs r v := rfl + +theorem regA_ne {t : U64} (ht : TcbAddr t) {r r' : Fl4SpecA64.Register} (h : r' ≠ r) : + (regA t (regIdxA r')).val ≠ (regA t (regIdxA r)).val := by + rw [regA_val ht (regIdxA_lt r'), regA_val ht (regIdxA_lt r)] + intro e + exact h (regIdxA_inj (by omega)) + +/-- A TCB with one register changed. -/ +theorem TcbRelE.set_reg {m m' : U64 → U64} {t : U64} {x : Fl4SpecA64.Tcb} (ht : TcbAddr t) (h : TcbRelE R m t x) + (r : Fl4SpecA64.Register) (v : U64) + (hA : Agree m m' (fun b => TF t b ∧ b.val ≠ (regA t (regIdxA r)).val)) (hv : m' (regA t (regIdxA r)) = v) : + TcbRelE R m' t (setReg x r v.bv) := by + have g : ∀ {b}, Aligned b ∧ TF t b → b.val ≠ (regA t (regIdxA r)).val → m' b = m b := + fun hb hne => hA _ hb.1 ⟨hb.2, hne⟩ + have hr := regA_val ht (regIdxA_lt r) + have := regIdxA_lt r + refine ⟨?_, fun e he => ?_, fun e d he => ?_, fun e rr d he => ?_, fun rr he => ?_, fun r' => ?_, ?_, ?_, ?_, ?_, ?_⟩ + · rw [g (a_st ht) (by rw [val_st ht]; omega)]; exact h.st + · rw [g (a_blk ht) (by rw [val_blk ht]; omega)]; exact h.blk e he + · rw [g (a_badge ht) (by rw [val_badge ht]; omega), g (a_flags ht) (by rw [val_flags ht]; omega)] + exact h.send e d he + · rw [g (a_reply ht) (by rw [val_reply ht]; omega)]; exact h.recv e rr d he + · rw [g (a_reply ht) (by rw [val_reply ht]; omega)]; exact h.reply rr he + · rw [setReg_regs] + by_cases e : r' = r + · subst e; rw [hv]; simp + · rw [g (a_reg ht r') (regA_ne ht e), Function.update_of_ne e]; exact h.regs r' + · rw [g (a_prio ht) (by rw [val_prio ht]; omega)]; exact h.prio + · rw [g (a_fault ht) (by rw [val_fault ht]; omega)]; exact h.fault0 + · rw [g (a_fault ht) (by rw [val_fault ht]; omega)]; exact h.faultT + · rw [g (a_bound ht) (by rw [val_bound ht]; omega)]; exact h.bound + · exact h.faultR.congr (g (a_fault ht) (by rw [val_fault ht]; omega)) (g (a_fw0 ht) (by rw [val_fw0 ht]; omega)) + (g (a_fw1 ht) (by rw [val_fw1 ht]; omega)) (g (a_lf ht) (by rw [val_lf ht]; omega)) + (g (a_lf0 ht) (by rw [val_lf0 ht]; omega)) (g (a_lf1 ht) (by rw [val_lf1 ht]; omega)) + (g (a_fw2 ht) (by rw [val_fw2 ht]; omega)) + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +theorem wr_ewrite {t b : U64} (ht : t ∈ T) (hb : TWF t b) (hal : Aligned b) (s : S) (v : U64) : + WritesIn st s (L.wr s b v) (EWrite T E R Bf) := + (writesIn_wr hal).mono fun c hc => by subst hc; exact Or.inr (Or.inr (Or.inl ⟨t, ht, Or.inl hb⟩)) + +theorem twf_reg {t : U64} (ht : TcbAddr t) (r : Fl4SpecA64.Register) : TWF t (regA t (regIdxA r)) := by + have hr := regA_val ht (regIdxA_lt r) + have := regIdxA_lt r + exact ⟨by omega, by rw [hr]; unfold TWOffN; omega⟩ + +/-- **Writing register `r` of the TCB at `t`.** -/ +theorem write_reg_rel (hP : EPlaced T E R Bf) (s : S) (σ : Fl4SpecA64.State Unit) + (hR : EpStateRel T E R Bf (L.mem s) σ) {t : U64} (ht : t ∈ T) {x : Fl4SpecA64.Tcb} + (hk : σ.kheap (specPtr t) = some (.tcb x)) (r : Fl4SpecA64.Register) (v : U64) : + EpStateRel T E R Bf (L.mem (L.wr s (regA t (regIdxA r)) v)) (updK σ (specPtr t) (.tcb (setReg x r v.bv))) ∧ + WritesIn st s (L.wr s (regA t (regIdxA r)) v) (EWrite T E R Bf) ∧ + EpStep σ (updK σ (specPtr t) (.tcb (setReg x r v.bv))) := by + have hta := hP.tcb t ht + have hx := hR.at_tcb ht hk + have ha := a_reg hta r + have hag := agree_wr (L := L) (s := s) (v := v) ha.1 + refine ⟨hR.upd_tcb hP ht _ (hag.mono fun b hb => tf_ne_of hb.2 ha.2) + (Or.inr ⟨hag _ (a_st hta).1 (by + show (stA t).val ≠ _; rw [val_st hta, regA_val hta (regIdxA_lt r)]; have := regIdxA_lt r; omega), + hag _ (a_blk hta).1 (by + show (tBlockA t).val ≠ _; rw [val_blk hta, regA_val hta (regIdxA_lt r)]; have := regIdxA_lt r; omega)⟩) + (hx.set_reg hta r v (hag.mono fun b hb => hb.2) (L.mem_wr_same ha.1)), wr_ewrite ht (twf_reg hta r) ha.1 s v, + EpStep.tcb hk x.tcbState _⟩ + +end + +/-! ## FrontEndExtraA's views -/ + +theorem regA_eq_regWA (t i : U64) : regA t i = regWA t i := rfl +theorem bufW_eq_bufWA (buf k : U64) : bufW buf k = bufWA buf k := rfl +theorem loadWordAt_eqA : loadWordAt = loadWordAtA := rfl +theorem bufAddr_eqA : bufAddr = bufAddrA := rfl + +theorem ok_eqE {α : Type _} {x y : α} (h : (ok x : Result α) = ok y) : x = y := by + have h2 : (ok x : Result α) ⦃ r => r = x ⦄ := by rw [WP.spec_ok] + rw [h, WP.spec_ok] at h2; exact h2.symm + +/-- FL4's message register `i` (`msg::msg_register`, `X2..X5`) is l4v's `msgRegisters[i]` (`[X2 .. X5]`). -/ +theorem msg_register_regIdxA {i : U64} (hi : i.val < 4) : + msg.msg_register i = ok (regIdxA (Fl4SpecA64.msgRegisters.getD i.val .x5)) := by + have hne : ∀ k : Nat, i.val = k → ∀ j : U64, j.val ≠ k → i ≠ j := fun k hk j hj e => hj (e ▸ hk) + unfold msg.msg_register + rcases (show i.val = 0 ∨ i.val = 1 ∨ i.val = 2 ∨ i.val = 3 by omega) with h | h | h | h + · rw [if_pos (UScalar.eq_of_val_eq (by rw [h]; rfl) : i = 0#u64), h]; unfold layout.reg.X2; rfl + · rw [if_neg (hne 1 h 0#u64 (by decide)), if_pos (UScalar.eq_of_val_eq (by rw [h]; rfl) : i = 1#u64), h] + unfold layout.reg.X3; rfl + · rw [if_neg (hne 2 h 0#u64 (by decide)), if_neg (hne 2 h 1#u64 (by decide)), + if_pos (UScalar.eq_of_val_eq (by rw [h]; rfl) : i = 2#u64), h] + unfold layout.reg.X4; rfl + · rw [if_neg (hne 3 h 0#u64 (by decide)), if_neg (hne 3 h 1#u64 (by decide)), if_neg (hne 3 h 2#u64 (by decide)), h] + unfold layout.reg.X5; rfl + +/-- **The register view of a related TCB** (`RegViewA`'s two conjuncts at its spec TCB). -/ +theorem TcbRelE.regView {m : U64 → U64} {t : U64} {x : Fl4SpecA64.Tcb} (h : TcbRelE R m t x) : + (m (regWA t layout.reg.MSG_INFO)).bv = x.tcbArch.tcbContext.userRegs Fl4SpecA64.msg_info_register ∧ + ∀ i r : U64, i.val < 4 → msg.msg_register i = ok r → + (m (regWA t r)).bv = x.tcbArch.tcbContext.userRegs (Fl4SpecA64.msgRegisters.getD i.val .x5) := by + refine ⟨?_, fun i r hi hr => ?_⟩ + · rw [← regIdxA_msgInfo]; exact h.regs _ + · rw [msg_register_regIdxA hi] at hr + rw [← ok_eqE hr]; exact h.regs _ + +/-- **`RegViewA` is a projection of `EpStateRel`**: at every TCB of `T`. -/ +theorem EpStateRel.regView {m : U64 → U64} {σ : Fl4SpecA64.State Unit} (hR : EpStateRel T E R Bf m σ) {t : U64} + (ht : t ∈ T) : RegViewA m σ t := fun x hk => (hR.at_tcb ht hk).regView + +/-- **`BufViewA` is a projection of `EpStateRel`**: at every tracked buffer with a nonzero FL4 word. -/ +theorem EpStateRel.bufView {Z : Type} {m : U64 → U64} {σ : Fl4SpecA64.State Z} (hR : EpStateRel T E R Bf m σ) + {buf : U64} {sb : Fl4SpecA64.Word} (hb : (buf, sb) ∈ Bf) (h0 : buf ≠ 0#u64) : + BufViewA m σ.machineState.underlyingMemory buf (some sb) := + ⟨⟨fun h => absurd h h0, fun h => by cases h⟩, fun sb' hs k hk => by cases hs; exact hR.buf _ hb k hk⟩ + +/-- No buffer on either side. -/ +theorem bufViewA_none (m : U64 → U64) (um : Fl4SpecA64.Word → Fl4SpecA64.Word8) : BufViewA m um 0#u64 none := + ⟨⟨fun _ => rfl, fun _ => rfl⟩, fun _ h => by cases h⟩ + +end + +end + +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/EpRelA.lean b/proofs/Fl4ProofsA64/P3/EpRelA.lean new file mode 100644 index 000000000..a722656fc --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/EpRelA.lean @@ -0,0 +1,630 @@ +/- +P3 (aarch64, A64-P3-IPC gen 2): **the A64 endpoint relation `EpStateRel`** (x86 `P3/EpRel`, ported). + +FL4's AArch64 endpoint (`layout.rs` `ep`, 16 bytes) has the x86 layout: the queue head and the queue end with the +state (`EP_IDLE` 0, `EP_SEND` 1, `EP_RECV` 2) in bits 0-1. The spec's (`Fl4SpecA64`, l4v Structures_A `endpoint`) +is `IdleEP | SendEP q | RecvEP q`. The AArch64 TCB words are the x86 offsets (`STATE` +3328, `BLOCK_OBJ` +3336, +`BLOCK_BADGE` +3344, `BLOCK_FLAGS` +3352, `REPLY_OBJ` +3360, `BOUND_NTFN` +3368, `FAULT_TYPE` +3376, `FAULT_W0..2` ++3384..+3400, `LF_TYPE` / `LF_W0` / `LF_W1` +3408..+3424, `PRIO` +3448, `EP_NEXT` / `EP_PREV` +3496 / +3504), the +reply's TCB word is +0. + +Changes from x86 beyond the renaming (`fl4_core_a64`, `Fl4SpecA64`, `Fl4ProofsA64`): +* **Objects are named `specPtr p`** (A64-P3 S3 gen 12), as in `NtfnRelA`: `σ.kheap (specPtr e)`, queues + `xs.map specPtr`, the block-object / reply words name `specPtr w`, `optRef` (= JointA's `optRefA`). +* **Registers**: FL4's 37 AArch64 register words `tcb::REGS + 8 i` (`layout.rs` `reg`: `X0..X30` 0–30, `SP_EL0` + 31, `ELR_EL1` 32 (= `NEXT_IP`), `SPSR_EL1` 33, `FAULT_IP` 34, `TPIDR_EL0` 35, `TPIDRRO_EL0` 36) against the 37 + constructors of `Fl4SpecA64.Register` (l4v AARCH64 `register`, the same order; seL4 `registerset.h`: + `NextIP = ELR_EL1 = 32`): **`regIdxA`** / `idxRegA`, `regIdxA_lt` (< 37), `regIdxA_inj`; `regA t i` is the same + term as `FrontEndExtraA`'s `regWA t i` (so `RegViewA` is a projection, P3/EpRegsA). +* **Faults** (`FaultRel'`): FL4's AArch64 fault words (`faults.rs` `set_fault`, `fl4-abi` `fault::*`, MCS numbering + as `Fl4SpecA64.makeArchFaultMsg`): VM fault 6 (`[instruction, fsr]` = l4v's `vmFaultArchData`), VGIC maintenance 7 + (`w1` = index valid, `w0` the index), VCPU fault 8 (`w0` = HSR), VPPI event 9 (`w0` = the IRQ). +* **IPC buffers in `FrontEndExtraA`'s shape**: the ghost set `Bf` holds pairs `(buf, sb)` of FL4's buffer base word + (`Tcb::IPC_BUFFER`'s value) and the spec's buffer pointer; for each, the 128 words `bufW buf k` (= `bufWA`) are the + spec's `loadWordAt um (bufAddr sb k)` (= `loadWordAtA` / `bufAddrA`), aligned — exactly the second conjunct of + `BufViewA` (projection in P3/EpRegsA). x86 tracked single words `(w, p)`; the footprint here is the same set of + words (`BufW`), and `EPlaced`'s buffer clauses are x86's, stated per word. + +Ghost state: `T` the TCB addresses, `E` the endpoints, `R` the reply objects, `Bf` the buffers. `EPlaced` (x86's): +TCBs placed, endpoints 16-byte and replies 32-byte aligned, nonzero, no wrap, pairwise disjoint; buffer words +aligned, off every object, and FL4's / the spec's word addresses in bijection (`binj`). + +The relation (`EpStateRel T E R Bf m σ`, on memory `m`) is x86's: `ep` (`EpObjRel`), `reply` (`reply_tcb` ↔ the +TCB word), `tcb` (`TcbRelE`: state, block object, sender badge / flags, reply word, all 37 registers, priority, +fault, bound notification), `buf`. It reads only the footprint `EFoot` and the spec's state only through `KView` +(`EpStateRel.frame`). + +**How it joins `JAtZA`** (not here; Architect 19:31Z): `E` (and `R`, `Bf`) become `XSetsA` fields at the coordinated +merge, `JAtZA` stores the endpoint objects, block / reply words, the register and IPC-buffer map, and `EpStateRel` +is a projection (with `SchedRelA` / `SchedRestA` for state / priority). This module imports only `NtfnRelA` (P1 +queues, `PtrA`), not `JointA`, so `JointA` can import it. + +No named premise. No `Fl4SpecA64` edit. +-/ +import Fl4ProofsA64.P3.NtfnRelA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif + +noncomputable section + +/-! ## TCB words -/ + +/-- The block-badge word of the TCB at `t` (layout.rs `tcb::BLOCK_BADGE`). -/ +def tBadgeA (t : U64) : U64 := core.num.U64.wrapping_add t 3344#u64 +/-- The block-flags word (`tcb::BLOCK_FLAGS`). -/ +def tFlagsA (t : U64) : U64 := core.num.U64.wrapping_add t 3352#u64 +/-- The reply-object word (`tcb::REPLY_OBJ`). -/ +def tReplyA (t : U64) : U64 := core.num.U64.wrapping_add t 3360#u64 +/-- The bound-notification word (`tcb::BOUND_NTFN`). -/ +def tBoundA (t : U64) : U64 := core.num.U64.wrapping_add t 3368#u64 +/-- The fault-type word (`tcb::FAULT_TYPE`). -/ +def tFaultA (t : U64) : U64 := core.num.U64.wrapping_add t 3376#u64 +/-- The fault words of the TCB at `t` (`tcb::FAULT_W0..2`, `LF_TYPE`, `LF_W0`, `LF_W1`). -/ +def tFw0A (t : U64) : U64 := core.num.U64.wrapping_add t 3384#u64 +def tFw1A (t : U64) : U64 := core.num.U64.wrapping_add t 3392#u64 +def tFw2A (t : U64) : U64 := core.num.U64.wrapping_add t 3400#u64 +def tLfA (t : U64) : U64 := core.num.U64.wrapping_add t 3408#u64 +def tLf0A (t : U64) : U64 := core.num.U64.wrapping_add t 3416#u64 +def tLf1A (t : U64) : U64 := core.num.U64.wrapping_add t 3424#u64 +/-- Register `i` of the saved user context of the TCB at `t` (`Tcb::reg` / `set_reg`; FrontEndExtraA's `regWA`). -/ +def regA (t i : U64) : U64 := + core.num.U64.wrapping_add (core.num.U64.wrapping_add t layout.tcb.REGS) (core.num.U64.wrapping_mul i 8#u64) +/-- The TCB word of the reply object at `r` (`reply::TCB`). -/ +def rTcbA (r : U64) : U64 := core.num.U64.wrapping_add r layout.reply.TCB + +theorem regA_val {t i : U64} (ht : TcbAddr t) (hi : i.val < 37) : (regA t i).val = t.val + 256 + 8 * i.val := by + have := ht.fits + unfold regA layout.tcb.REGS + have hs : UScalar.size UScalarTy.U64 = 2 ^ 64 := by simp [U64.size_def, U64.numBits] + have h1 : (256#u64 : U64).val = 256 := rfl + have h2 : (8#u64 : U64).val = 8 := rfl + rw [core.num.U64.wrapping_add_val_eq, core.num.U64.wrapping_add_val_eq, core.num.U64.wrapping_mul_val_eq, + hs, h1, h2] + have : (i.val * 8) % 2 ^ 64 = i.val * 8 := Nat.mod_eq_of_lt (by omega) + rw [this] + have : (t.val + 256) % 2 ^ 64 = t.val + 256 := Nat.mod_eq_of_lt (by omega) + rw [this, Nat.mod_eq_of_lt (by omega)] + omega + +theorem rTcbA_val (r : U64) : (rTcbA r).val = r.val := by + unfold rTcbA layout.reply.TCB + rw [core.num.U64.wrapping_add_val_eq] + have : (0#u64 : U64).val = 0 := rfl + rw [this, Nat.add_zero] + exact Nat.mod_eq_of_lt (by have := r.hBounds; simpa [U64.size_def, U64.numBits] using this) + +/-! ## Registers -/ + +/-- FL4's register number of a spec register (`arch/aarch64/layout.rs` `reg`; seL4 `registerset.h`). -/ +def regIdxA : Fl4SpecA64.Register → U64 + | .x0 => 0#u64 | .x1 => 1#u64 | .x2 => 2#u64 | .x3 => 3#u64 | .x4 => 4#u64 | .x5 => 5#u64 | .x6 => 6#u64 + | .x7 => 7#u64 | .x8 => 8#u64 | .x9 => 9#u64 | .x10 => 10#u64 | .x11 => 11#u64 | .x12 => 12#u64 + | .x13 => 13#u64 | .x14 => 14#u64 | .x15 => 15#u64 | .x16 => 16#u64 | .x17 => 17#u64 | .x18 => 18#u64 + | .x19 => 19#u64 | .x20 => 20#u64 | .x21 => 21#u64 | .x22 => 22#u64 | .x23 => 23#u64 | .x24 => 24#u64 + | .x25 => 25#u64 | .x26 => 26#u64 | .x27 => 27#u64 | .x28 => 28#u64 | .x29 => 29#u64 | .x30 => 30#u64 + | .spEl0 => 31#u64 | .nextIP => 32#u64 | .spsrEl1 => 33#u64 | .faultIP => 34#u64 | .tpidrEl0 => 35#u64 + | .tpidrroEl0 => 36#u64 + +/-- The inverse of `regIdxA`. -/ +def idxRegA : Nat → Fl4SpecA64.Register + | 0 => .x0 | 1 => .x1 | 2 => .x2 | 3 => .x3 | 4 => .x4 | 5 => .x5 | 6 => .x6 | 7 => .x7 | 8 => .x8 + | 9 => .x9 | 10 => .x10 | 11 => .x11 | 12 => .x12 | 13 => .x13 | 14 => .x14 | 15 => .x15 | 16 => .x16 + | 17 => .x17 | 18 => .x18 | 19 => .x19 | 20 => .x20 | 21 => .x21 | 22 => .x22 | 23 => .x23 | 24 => .x24 + | 25 => .x25 | 26 => .x26 | 27 => .x27 | 28 => .x28 | 29 => .x29 | 30 => .x30 | 31 => .spEl0 + | 32 => .nextIP | 33 => .spsrEl1 | 34 => .faultIP | 35 => .tpidrEl0 | _ => .tpidrroEl0 + +theorem idxRegA_regIdxA (r : Fl4SpecA64.Register) : idxRegA (regIdxA r).val = r := by cases r <;> rfl + +theorem regIdxA_lt (r : Fl4SpecA64.Register) : (regIdxA r).val < 37 := by cases r <;> decide + +theorem regIdxA_inj {r r' : Fl4SpecA64.Register} (h : (regIdxA r).val = (regIdxA r').val) : r = r' := by + rw [← idxRegA_regIdxA r, ← idxRegA_regIdxA r', h] + +theorem regIdxA_badge : regIdxA Fl4SpecA64.badge_register = layout.reg.BADGE := by + unfold layout.reg.BADGE layout.reg.X0; rfl +theorem regIdxA_msgInfo : regIdxA Fl4SpecA64.msg_info_register = layout.reg.MSG_INFO := by + unfold layout.reg.MSG_INFO layout.reg.X1; rfl +/-- l4v's `NextIP` is FL4's `ELR_EL1` (`reg::NEXT_IP`). -/ +theorem regIdxA_nextIP : regIdxA .nextIP = layout.reg.NEXT_IP := by + unfold layout.reg.NEXT_IP layout.reg.ELR_EL1; rfl +theorem regIdxA_faultIP : regIdxA .faultIP = layout.reg.FAULT_IP := by unfold layout.reg.FAULT_IP; rfl + +theorem badgeRegA_eq_regA (t : U64) : badgeRegA t = regA t (regIdxA Fl4SpecA64.badge_register) := by + rw [regIdxA_badge]; rfl + +/-! ## Store specs of the field accessors -/ + +section +variable {S : Type _} {st : machine.Store S} [L : LawfulStore st] + +@[step] theorem obj.Tcb.block_badge.spec (s : S) (t : obj.Tcb) : + obj.Tcb.block_badge st t s ⦃ v => v = L.mem s (tBadgeA t) ⦄ := by + unfold obj.Tcb.block_badge; step*; simp_all [tBadgeA] +@[step] theorem obj.Tcb.set_block_badge.spec (s : S) (t : obj.Tcb) (v : U64) : + obj.Tcb.set_block_badge st t s v ⦃ s' => s' = L.wr s (tBadgeA t) v ⦄ := by + unfold obj.Tcb.set_block_badge; step*; simp_all [tBadgeA] +@[step] theorem obj.Tcb.block_flags.spec (s : S) (t : obj.Tcb) : + obj.Tcb.block_flags st t s ⦃ v => v = L.mem s (tFlagsA t) ⦄ := by + unfold obj.Tcb.block_flags; step*; simp_all [tFlagsA] +@[step] theorem obj.Tcb.set_block_flags.spec (s : S) (t : obj.Tcb) (v : U64) : + obj.Tcb.set_block_flags st t s v ⦃ s' => s' = L.wr s (tFlagsA t) v ⦄ := by + unfold obj.Tcb.set_block_flags; step*; simp_all [tFlagsA] +@[step] theorem obj.Tcb.reply_obj.spec (s : S) (t : obj.Tcb) : + obj.Tcb.reply_obj st t s ⦃ v => v = L.mem s (tReplyA t) ⦄ := by + unfold obj.Tcb.reply_obj; step*; simp_all [tReplyA] +@[step] theorem obj.Tcb.set_reply_obj.spec (s : S) (t : obj.Tcb) (v : U64) : + obj.Tcb.set_reply_obj st t s v ⦃ s' => s' = L.wr s (tReplyA t) v ⦄ := by + unfold obj.Tcb.set_reply_obj; step*; simp_all [tReplyA] +@[step] theorem obj.Tcb.bound_ntfn.spec (s : S) (t : obj.Tcb) : + obj.Tcb.bound_ntfn st t s ⦃ v => v = L.mem s (tBoundA t) ⦄ := by + unfold obj.Tcb.bound_ntfn; step*; simp_all [tBoundA] +@[step] theorem obj.Tcb.fault_type.spec (s : S) (t : obj.Tcb) : + obj.Tcb.fault_type st t s ⦃ v => v = L.mem s (tFaultA t) ⦄ := by + unfold obj.Tcb.fault_type; step*; simp_all [tFaultA] +@[step] theorem obj.Tcb.fault_w0.spec (s : S) (t : obj.Tcb) : + obj.Tcb.fault_w0 st t s ⦃ v => v = L.mem s (tFw0A t) ⦄ := by + unfold obj.Tcb.fault_w0; step*; simp_all [tFw0A] +@[step] theorem obj.Tcb.fault_w1.spec (s : S) (t : obj.Tcb) : + obj.Tcb.fault_w1 st t s ⦃ v => v = L.mem s (tFw1A t) ⦄ := by + unfold obj.Tcb.fault_w1; step*; simp_all [tFw1A] +@[step] theorem obj.Tcb.fault_w2.spec (s : S) (t : obj.Tcb) : + obj.Tcb.fault_w2 st t s ⦃ v => v = L.mem s (tFw2A t) ⦄ := by + unfold obj.Tcb.fault_w2; step*; simp_all [tFw2A] +@[step] theorem obj.Tcb.lf_type.spec (s : S) (t : obj.Tcb) : + obj.Tcb.lf_type st t s ⦃ v => v = L.mem s (tLfA t) ⦄ := by + unfold obj.Tcb.lf_type; step*; simp_all [tLfA] +@[step] theorem obj.Tcb.lf_w0.spec (s : S) (t : obj.Tcb) : + obj.Tcb.lf_w0 st t s ⦃ v => v = L.mem s (tLf0A t) ⦄ := by + unfold obj.Tcb.lf_w0; step*; simp_all [tLf0A] +@[step] theorem obj.Tcb.lf_w1.spec (s : S) (t : obj.Tcb) : + obj.Tcb.lf_w1 st t s ⦃ v => v = L.mem s (tLf1A t) ⦄ := by + unfold obj.Tcb.lf_w1; step*; simp_all [tLf1A] +@[step] theorem obj.Tcb.reg.spec (s : S) (t : obj.Tcb) (i : U64) : + obj.Tcb.reg st t s i ⦃ v => v = L.mem s (regA t i) ⦄ := by + unfold obj.Tcb.reg; step*; simp_all [regA] +@[step] theorem obj.Tcb.set_reg.spec (s : S) (t : obj.Tcb) (i v : U64) : + obj.Tcb.set_reg st t s i v ⦃ s' => s' = L.wr s (regA t i) v ⦄ := by + unfold obj.Tcb.set_reg; step*; simp_all [regA] +@[step] theorem obj.Reply.tcb.spec (s : S) (r : obj.Reply) : + obj.Reply.tcb st r s ⦃ v => v = L.mem s (rTcbA r) ⦄ := by + unfold obj.Reply.tcb; step*; simp_all [rTcbA] +@[step] theorem obj.Reply.set_tcb.spec (s : S) (r : obj.Reply) (v : U64) : + obj.Reply.set_tcb st r s v ⦃ s' => s' = L.wr s (rTcbA r) v ⦄ := by + unfold obj.Reply.set_tcb; step*; simp_all [rTcbA] + +end + +/-! ## IPC-buffer words (FrontEndExtraA's shape) -/ + +section Buf +open Fl4SpecA64 + +/-- FL4's IPC-buffer word `k` at `buf` (`msg::buf_get`; FrontEndExtraA's `bufWA`). -/ +def bufW (buf k : U64) : U64 := core.num.U64.wrapping_add buf (core.num.U64.wrapping_mul k 8#u64) + +/-- l4v's `loadWord` on a byte memory, as a function (FrontEndExtraA's `loadWordAtA`). -/ +def loadWordAt (um : Word → Word8) (p : Word) : Word := + wordRcat ((intUpto 0 7).map fun i => um (p + (7 - BitVec.ofInt 64 i))) + +/-- Word `x` of the spec's buffer at `p` (FrontEndExtraA's `bufAddrA`). -/ +def bufAddr (p : Word) (x : Nat) : Word := p + BitVec.ofNat 64 (x * wordSize) + +/-- A word of a tracked IPC buffer. -/ +abbrev BufW (Bf : Set (U64 × Word)) (a : U64) : Prop := ∃ bp ∈ Bf, ∃ k : U64, k.val < 128 ∧ a = bufW bp.1 k + +end Buf + +/-! ## Placement -/ + +/-- The words of the TCB at `t` the relation reads, as offsets (all but the `Ep` links): state, block object, block +badge, block flags, reply object, bound notification, fault words, priority, and the 37 registers. -/ +def TOffN (k : Nat) : Prop := + k = 3328 ∨ k = 3336 ∨ k = 3344 ∨ k = 3352 ∨ k = 3360 ∨ k = 3368 ∨ k = 3376 ∨ k = 3448 ∨ + (256 ≤ k ∧ k < 552) ∨ k = 3384 ∨ k = 3392 ∨ k = 3400 ∨ k = 3408 ∨ k = 3416 ∨ k = 3424 + +/-- The words of a TCB the endpoint code writes (not the bound notification, the priority and the fault payload +words; (gen 5) the fault-type word `FAULT_TYPE` +3376, which `cancel_ipc`'s `clear_fault` writes). -/ +def TWOffN (k : Nat) : Prop := + k = 3328 ∨ k = 3336 ∨ k = 3344 ∨ k = 3352 ∨ k = 3360 ∨ (256 ≤ k ∧ k < 552) ∨ k = 3376 + +/-- A field word (not a link) of the TCB at `t`. -/ +def TF (t a : U64) : Prop := t.val ≤ a.val ∧ TOffN (a.val - t.val) +/-- A written field word of the TCB at `t`. -/ +def TWF (t a : U64) : Prop := t.val ≤ a.val ∧ TWOffN (a.val - t.val) +/-- An `Ep` link word of the TCB at `t`. -/ +def TL (t a : U64) : Prop := a.val = t.val + 3496 ∨ a.val = t.val + 3504 + +/-- Hypothesis on the ghost sets (module docstring; x86 `EPlaced`, buffer clauses per word of `BufW`). -/ +structure EPlaced (T E R : Set U64) (Bf : Set (U64 × Fl4SpecA64.Word)) : Prop where + tcb : TcbPlaced T + ep : ∀ e ∈ E, e.val % 16 = 0 ∧ e.val ≠ 0 ∧ e.val + 16 ≤ 2 ^ 64 + rep : ∀ r ∈ R, r.val % 32 = 0 ∧ r.val ≠ 0 ∧ r.val + 32 ≤ 2 ^ 64 + et : ∀ e ∈ E, ∀ t ∈ T, t.val + 4096 ≤ e.val ∨ e.val + 16 ≤ t.val + rt : ∀ r ∈ R, ∀ t ∈ T, t.val + 4096 ≤ r.val ∨ r.val + 32 ≤ t.val + er : ∀ e ∈ E, ∀ r ∈ R, r.val + 32 ≤ e.val ∨ e.val + 16 ≤ r.val + balign : ∀ a, BufW Bf a → Aligned a + bt : ∀ a, BufW Bf a → ∀ t ∈ T, a.val < t.val ∨ t.val + 4096 ≤ a.val + be : ∀ a, BufW Bf a → ∀ e ∈ E, a.val < e.val ∨ e.val + 16 ≤ a.val + br : ∀ a, BufW Bf a → ∀ r ∈ R, a.val < r.val ∨ r.val + 32 ≤ a.val + binj : ∀ bp ∈ Bf, ∀ bq ∈ Bf, ∀ k k' : U64, k.val < 128 → k'.val < 128 → + (bufW bp.1 k = bufW bq.1 k' ↔ bufAddr bp.2 k.val = bufAddr bq.2 k'.val) + +theorem EPlaced.epPlaced {T E R : Set U64} {Bf} (hP : EPlaced T E R Bf) {e : U64} (he : e ∈ E) : EpPlaced T e := + ⟨by have := (hP.ep e he).1; omega, (hP.ep e he).2.2, fun t ht => by have := hP.et e he t ht; omega⟩ + +/-! ## The relation -/ + +section Rel +open Fl4SpecA64 + +/-- The block-flags word for a sender payload (endpoint.rs `FLAG_*`). -/ +def flagsOf (d : SenderPayload) : U64 := + ((if d.senderCanGrant then 1#u64 else 0#u64) ||| (if d.senderCanGrantReply then 2#u64 else 0#u64)) ||| + (if d.senderIsCall then 4#u64 else 0#u64) + +/-- A lookup failure, from the lookup-failure words (type, `w0`, `w1`; error.rs `LookupFault::encode`). -/ +def LfRel (lt l0 l1 : U64) : LookupFailure → Prop + | .invalidRoot => lt = 1#u64 + | .missingCapability n => lt = 2#u64 ∧ l0.val = n + | .depthMismatch bl bf => lt = 3#u64 ∧ l0.val = bf ∧ l1.val = bl + | .guardMismatch bl g => lt = 4#u64 ∧ l0.bv = ofBl g ∧ (l1.val >>> 8) = bl ∧ l1.val % 256 = g.length + +/-- A fault, from the fault words (type, `w0`, `w1`, `w2`, and the lookup-failure words; faults.rs `set_fault`, +AArch64 arms). A VM fault's l4v arch data is `[instruction, fsr]` (`handle_vm_fault`), FL4's `w2` and `w1`; a VGIC +maintenance fault's index is `w0` when `w1` (index valid) is 1. -/ +def FaultRel' (ty w0 w1 w2 lt l0 l1 : U64) : Option Fault → Prop + | none => ty = 0#u64 + | some (.capFault a rp lf) => ty = 1#u64 ∧ w0.bv = a ∧ w1 = (if rp then 1#u64 else 0#u64) ∧ LfRel lt l0 l1 lf + | some (.unknownSyscallException n) => ty = 2#u64 ∧ w0.bv = n + | some (.userException e c) => ty = 3#u64 ∧ w0.bv = e ∧ w1.bv = c + | some (.timeout b) => ty = 5#u64 ∧ w0.bv = b + | some (.archFault (.vmFault a d)) => ty = 6#u64 ∧ w0.bv = a ∧ d = [w2.bv, w1.bv] + | some (.archFault (.vgicMaintenance o)) => ty = 7#u64 ∧ + (match o with | none => w1 = 0#u64 | some i => w1 = 1#u64 ∧ w0.bv = i) + | some (.archFault (.vcpuFault h)) => ty = 8#u64 ∧ w0.bv = h + | some (.archFault (.vppiEvent irq)) => ty = 9#u64 ∧ w0.val = irq.toNat + +/-- The fault of the TCB at `t`. -/ +def FaultRel (m : U64 → U64) (t : U64) (f : Option Fault) : Prop := + FaultRel' (m (tFaultA t)) (m (tFw0A t)) (m (tFw1A t)) (m (tFw2A t)) (m (tLfA t)) (m (tLf0A t)) (m (tLf1A t)) f + +/-- The TCB at `t` and the spec's TCB `x`. -/ +structure TcbRelE (R : Set U64) (m : U64 → U64) (t : U64) (x : Tcb) : Prop where + st : (m (stA t)).val = tsCode x.tcbState + blk : ∀ e, epBlocked x.tcbState = some e → specPtr (m (tBlockA t)) = e + send : ∀ e d, x.tcbState = .blockedOnSend e d → (m (tBadgeA t)).bv = d.senderBadge ∧ m (tFlagsA t) = flagsOf d + recv : ∀ e r d, x.tcbState = .blockedOnReceive e r d → optRef (m (tReplyA t)) = r ∧ + (r ≠ none → m (tReplyA t) ∈ R) + reply : ∀ r, x.tcbState = .blockedOnReply r → specPtr (m (tReplyA t)) = r + regs : ∀ r, (m (regA t (regIdxA r))).bv = x.tcbArch.tcbContext.userRegs r + prio : (m (prioA t)).val = x.tcbPriority.toNat + fault0 : m (tFaultA t) = 0#u64 ↔ x.tcbFault = none + faultT : m (tFaultA t) = 5#u64 ↔ ∃ b, x.tcbFault = some (.timeout b) + bound : x.tcbBoundNotification = optRef (m (tBoundA t)) + faultR : FaultRel m t x.tcbFault + +/-- A TCB queued on the endpoint at `e` with state word `k`. -/ +def QueuedOn (m : U64 → U64) (e : U64) (k : Nat) (y : U64) : Prop := (m (stA y)).val = k ∧ m (tBlockA y) = e + +/-- The endpoint at `e` in memory `m` and the spec's endpoint `x`. -/ +def EpObjRel (m : U64 → U64) (T : Set U64) (e : U64) : Endpoint → Prop + | .idleEP => stOf m e = 0#u64 ∧ QueueRel m T .Ep (qOf m e) [] + | .sendEP q => stOf m e = 1#u64 ∧ ∃ xs : List U64, xs ≠ [] ∧ q = xs.map specPtr ∧ + QueueRel m T .Ep (qOf m e) xs ∧ ∀ y ∈ xs, QueuedOn m e 4 y + | .recvEP q => stOf m e = 2#u64 ∧ ∃ xs : List U64, xs ≠ [] ∧ q = xs.map specPtr ∧ + QueueRel m T .Ep (qOf m e) xs ∧ ∀ y ∈ xs, QueuedOn m e 3 y + +/-- The endpoint part of the state relation. -/ +structure EpStateRel {Z : Type} (T E R : Set U64) (Bf : Set (U64 × Word)) (m : U64 → U64) (σ : State Z) : + Prop where + ep : ∀ e ∈ E, ∃ x, σ.kheap (specPtr e) = some (.endpoint x) ∧ EpObjRel m T e x + reply : ∀ r ∈ R, ∃ x, σ.kheap (specPtr r) = some (.reply x) ∧ x.replyTcb = optRef (m (rTcbA r)) + tcb : ∀ t ∈ T, ∃ x, σ.kheap (specPtr t) = some (.tcb x) ∧ TcbRelE R m t x + buf : ∀ bp ∈ Bf, ∀ k : U64, k.val < 128 → + (m (bufW bp.1 k)).bv = loadWordAt σ.machineState.underlyingMemory (bufAddr bp.2 k.val) ∧ + (bufAddr bp.2 k.val).toNat % 8 = 0 + +/-- The words the relation reads. -/ +def EFoot (T E R : Set U64) (Bf : Set (U64 × Word)) (a : U64) : Prop := + (∃ e ∈ E, a.val = e.val ∨ a.val = e.val + 8) ∨ (∃ r ∈ R, a.val = r.val) ∨ + (∃ t ∈ T, TF t a ∨ TL t a) ∨ BufW Bf a + +/-- The words the endpoint code writes. -/ +def EWrite (T E R : Set U64) (Bf : Set (U64 × Word)) (a : U64) : Prop := + (∃ e ∈ E, a.val = e.val ∨ a.val = e.val + 8) ∨ (∃ r ∈ R, a.val = r.val) ∨ + (∃ t ∈ T, TWF t a ∨ TL t a) ∨ BufW Bf a + +theorem EWrite.foot {T E R : Set U64} {Bf} {a : U64} (h : EWrite T E R Bf a) : EFoot T E R Bf a := by + rcases h with h | h | ⟨t, ht, h | h⟩ | h + · exact Or.inl h + · exact Or.inr (Or.inl h) + · refine Or.inr (Or.inr (Or.inl ⟨t, ht, Or.inl ⟨h.1, ?_⟩⟩)) + have := h.2; unfold TWOffN at this; unfold TOffN; omega + · exact Or.inr (Or.inr (Or.inl ⟨t, ht, Or.inr h⟩)) + · exact Or.inr (Or.inr (Or.inr h)) + +/-- What the relation reads of the spec's state: endpoints, the TCB of replies, TCBs' state, registers, priority, +fault and bound notification, and user memory. -/ +def KView {Z : Type} (σ σ' : State Z) : Prop := + (∀ p x, σ.kheap p = some (.endpoint x) → σ'.kheap p = some (.endpoint x)) ∧ + (∀ p x, σ.kheap p = some (.reply x) → ∃ y, σ'.kheap p = some (.reply y) ∧ y.replyTcb = x.replyTcb) ∧ + (∀ p x, σ.kheap p = some (.tcb x) → ∃ y, σ'.kheap p = some (.tcb y) ∧ + y.tcbState = x.tcbState ∧ y.tcbArch.tcbContext.userRegs = x.tcbArch.tcbContext.userRegs ∧ + y.tcbPriority = x.tcbPriority ∧ y.tcbFault = x.tcbFault ∧ y.tcbBoundNotification = x.tcbBoundNotification) ∧ + σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory + +/-- The abstract state `a` with heap `kh` and user memory `um`. -/ +def withKM (a : AbstractState) (kh : ObjRef → Option KernelObject) (um : Word → Word8) : AbstractState := + { a with kheap := kh, machineState := { a.machineState with underlyingMemory := um } } + +/-- What the endpoint code changes in the spec's state: endpoints, the TCB of replies, the state, user context and +(gen 5, `cancel_ipc`'s fault clear) fault of TCBs, and user memory; nothing else. -/ +def EpStep {Z : Type} (σ σ' : State Z) : Prop := + σ'.exst = σ.exst ∧ + σ'.toAbstractState = withKM σ.toAbstractState σ'.kheap σ'.machineState.underlyingMemory ∧ + ∀ p, σ'.kheap p = σ.kheap p ∨ + (∃ x y, σ.kheap p = some (.endpoint x) ∧ σ'.kheap p = some (.endpoint y)) ∨ + (∃ x y, σ.kheap p = some (.reply x) ∧ σ'.kheap p = some (.reply y) ∧ y.replySc = x.replySc) ∨ + (∃ x y, σ.kheap p = some (.tcb x) ∧ σ'.kheap p = some (.tcb y) ∧ + y = { x with tcbState := y.tcbState, tcbArch := y.tcbArch, tcbFault := y.tcbFault }) + +end Rel + +/-! ## Addresses -/ + +/-- Memory that agrees on the words `P`. -/ +def Agree (m m' : U64 → U64) (P : U64 → Prop) : Prop := ∀ a, Aligned a → P a → m' a = m a + +theorem Agree.trans {m₁ m₂ m₃ : U64 → U64} {P : U64 → Prop} (h₁ : Agree m₁ m₂ P) (h₂ : Agree m₂ m₃ P) : + Agree m₁ m₃ P := fun a ha hp => (h₂ a ha hp).trans (h₁ a ha hp) +theorem Agree.mono {m m' : U64 → U64} {P Q : U64 → Prop} (h : Agree m m' P) (hPQ : ∀ a, Q a → P a) : + Agree m m' Q := fun a ha hq => h a ha (hPQ a hq) +theorem Agree.rfl' {m : U64 → U64} {P : U64 → Prop} : Agree m m P := fun _ _ _ => rfl + +theorem tw_val {t K : U64} (ht : TcbAddr t) (hK : K.val < 4096) : + (core.num.U64.wrapping_add t K).val = t.val + K.val := tfield_val ht hK + +/-- A field word at offset `K`: aligned and a field. -/ +theorem tf_addr {t K : U64} (ht : TcbAddr t) (hK : TOffN K.val) (h8 : K.val % 8 = 0) : + Aligned (core.num.U64.wrapping_add t K) ∧ TF t (core.num.U64.wrapping_add t K) := by + have hK' : K.val < 4096 := by unfold TOffN at hK; omega + have := ht.align + refine ⟨?_, ?_, ?_⟩ + · unfold Aligned; rw [tw_val ht hK']; omega + · rw [tw_val ht hK']; omega + · rw [tw_val ht hK', Nat.add_sub_cancel_left]; exact hK + +theorem regA_tf {t i : U64} (ht : TcbAddr t) (hi : i.val < 37) : Aligned (regA t i) ∧ TF t (regA t i) := by + have := ht.align + refine ⟨?_, ?_, ?_⟩ + · unfold Aligned; rw [regA_val ht hi]; omega + · rw [regA_val ht hi]; omega + · rw [regA_val ht hi]; unfold TOffN; omega + +section +variable {t : U64} (ht : TcbAddr t) +include ht +theorem a_st : Aligned (stA t) ∧ TF t (stA t) := by rw [stA_eq]; exact tf_addr ht (by unfold TOffN; simp) (by simp) +theorem a_blk : Aligned (tBlockA t) ∧ TF t (tBlockA t) := by + rw [tBlockA_eq]; exact tf_addr ht (by unfold TOffN; simp) (by simp) +theorem a_badge : Aligned (tBadgeA t) ∧ TF t (tBadgeA t) := tf_addr ht (by unfold TOffN; simp) (by simp) +theorem a_flags : Aligned (tFlagsA t) ∧ TF t (tFlagsA t) := tf_addr ht (by unfold TOffN; simp) (by simp) +theorem a_reply : Aligned (tReplyA t) ∧ TF t (tReplyA t) := tf_addr ht (by unfold TOffN; simp) (by simp) +theorem a_bound : Aligned (tBoundA t) ∧ TF t (tBoundA t) := tf_addr ht (by unfold TOffN; simp) (by simp) +theorem a_fault : Aligned (tFaultA t) ∧ TF t (tFaultA t) := tf_addr ht (by unfold TOffN; simp) (by simp) +theorem a_fw0 : Aligned (tFw0A t) ∧ TF t (tFw0A t) := tf_addr ht (by unfold TOffN; simp) (by simp) +theorem a_fw1 : Aligned (tFw1A t) ∧ TF t (tFw1A t) := tf_addr ht (by unfold TOffN; simp) (by simp) +theorem a_fw2 : Aligned (tFw2A t) ∧ TF t (tFw2A t) := tf_addr ht (by unfold TOffN; simp) (by simp) +theorem a_lf : Aligned (tLfA t) ∧ TF t (tLfA t) := tf_addr ht (by unfold TOffN; simp) (by simp) +theorem a_lf0 : Aligned (tLf0A t) ∧ TF t (tLf0A t) := tf_addr ht (by unfold TOffN; simp) (by simp) +theorem a_lf1 : Aligned (tLf1A t) ∧ TF t (tLf1A t) := tf_addr ht (by unfold TOffN; simp) (by simp) +theorem a_prio : Aligned (prioA t) ∧ TF t (prioA t) := by + rw [prioA_eq']; exact tf_addr ht (by unfold TOffN; simp) (by simp) +theorem a_reg (r : Fl4SpecA64.Register) : Aligned (regA t (regIdxA r)) ∧ TF t (regA t (regIdxA r)) := + regA_tf ht (regIdxA_lt r) +theorem a_next : Aligned (nextA .Ep t) ∧ TL t (nextA .Ep t) := + ⟨nextA_aligned ht .Ep, Or.inl (by rw [nextEp_eq, tw_val ht (by simp)]; rfl)⟩ +theorem a_prev : Aligned (prevA .Ep t) ∧ TL t (prevA .Ep t) := + ⟨prevA_aligned ht .Ep, Or.inr (by rw [prevEp_eq, tw_val ht (by simp)]; rfl)⟩ +end + +theorem val_st {t : U64} (ht : TcbAddr t) : (stA t).val = t.val + 3328 := by rw [stA_eq, tw_val ht (by simp)]; rfl +theorem val_blk {t : U64} (ht : TcbAddr t) : (tBlockA t).val = t.val + 3336 := by + rw [tBlockA_eq, tw_val ht (by simp)]; rfl +theorem val_badge {t : U64} (ht : TcbAddr t) : (tBadgeA t).val = t.val + 3344 := by + unfold tBadgeA; rw [tw_val ht (by simp)]; rfl +theorem val_flags {t : U64} (ht : TcbAddr t) : (tFlagsA t).val = t.val + 3352 := by + unfold tFlagsA; rw [tw_val ht (by simp)]; rfl +theorem val_reply {t : U64} (ht : TcbAddr t) : (tReplyA t).val = t.val + 3360 := by + unfold tReplyA; rw [tw_val ht (by simp)]; rfl +theorem val_bound {t : U64} (ht : TcbAddr t) : (tBoundA t).val = t.val + 3368 := by + unfold tBoundA; rw [tw_val ht (by simp)]; rfl +theorem val_fault {t : U64} (ht : TcbAddr t) : (tFaultA t).val = t.val + 3376 := by + unfold tFaultA; rw [tw_val ht (by simp)]; rfl +theorem val_fw0 {t : U64} (ht : TcbAddr t) : (tFw0A t).val = t.val + 3384 := by + unfold tFw0A; rw [tw_val ht (by simp)]; rfl +theorem val_fw1 {t : U64} (ht : TcbAddr t) : (tFw1A t).val = t.val + 3392 := by + unfold tFw1A; rw [tw_val ht (by simp)]; rfl +theorem val_fw2 {t : U64} (ht : TcbAddr t) : (tFw2A t).val = t.val + 3400 := by + unfold tFw2A; rw [tw_val ht (by simp)]; rfl +theorem val_lf {t : U64} (ht : TcbAddr t) : (tLfA t).val = t.val + 3408 := by + unfold tLfA; rw [tw_val ht (by simp)]; rfl +theorem val_lf0 {t : U64} (ht : TcbAddr t) : (tLf0A t).val = t.val + 3416 := by + unfold tLf0A; rw [tw_val ht (by simp)]; rfl +theorem val_lf1 {t : U64} (ht : TcbAddr t) : (tLf1A t).val = t.val + 3424 := by + unfold tLf1A; rw [tw_val ht (by simp)]; rfl +theorem val_prio {t : U64} (ht : TcbAddr t) : (prioA t).val = t.val + 3448 := by + rw [prioA_eq', tw_val ht (by simp)]; rfl +theorem val_next {t : U64} (ht : TcbAddr t) : (nextA .Ep t).val = t.val + 3496 := by + rw [nextEp_eq, tw_val ht (by simp)]; rfl +theorem val_prev {t : U64} (ht : TcbAddr t) : (prevA .Ep t).val = t.val + 3504 := by + rw [prevEp_eq, tw_val ht (by simp)]; rfl + +/-- The fault relation only reads the fault words. -/ +theorem FaultRel.congr {m m' : U64 → U64} {t : U64} {f : Option Fl4SpecA64.Fault} (h : FaultRel m t f) + (e0 : m' (tFaultA t) = m (tFaultA t)) (e1 : m' (tFw0A t) = m (tFw0A t)) (e2 : m' (tFw1A t) = m (tFw1A t)) + (e3 : m' (tLfA t) = m (tLfA t)) (e4 : m' (tLf0A t) = m (tLf0A t)) (e5 : m' (tLf1A t) = m (tLf1A t)) + (e6 : m' (tFw2A t) = m (tFw2A t)) : + FaultRel m' t f := by + unfold FaultRel; rw [e0, e1, e2, e3, e4, e5, e6]; exact h + +/-- The fault words agree when memory agrees on the fields `P` reaches. -/ +theorem FaultRel.congr' {m m' : U64 → U64} {t : U64} (ht : TcbAddr t) {f : Option Fl4SpecA64.Fault} + (h : FaultRel m t f) (P : Nat → Prop) (hA : ∀ a, Aligned a → P a.val → m' a = m a) + (hP : ∀ k, 3376 ≤ k → k ≤ 3424 → P (t.val + k)) : FaultRel m' t f := + h.congr (hA _ (a_fault ht).1 (by rw [val_fault ht]; exact hP _ (by omega) (by omega))) + (hA _ (a_fw0 ht).1 (by rw [val_fw0 ht]; exact hP _ (by omega) (by omega))) + (hA _ (a_fw1 ht).1 (by rw [val_fw1 ht]; exact hP _ (by omega) (by omega))) + (hA _ (a_lf ht).1 (by rw [val_lf ht]; exact hP _ (by omega) (by omega))) + (hA _ (a_lf0 ht).1 (by rw [val_lf0 ht]; exact hP _ (by omega) (by omega))) + (hA _ (a_lf1 ht).1 (by rw [val_lf1 ht]; exact hP _ (by omega) (by omega))) + (hA _ (a_fw2 ht).1 (by rw [val_fw2 ht]; exact hP _ (by omega) (by omega))) + +section +variable {T E R : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + +theorem val_head (e : U64) : (epHeadA e).val = e.val := epHeadA_val +theorem val_ts (hP : EPlaced T E R Bf) {e : U64} (he : e ∈ E) : (epTSA e).val = e.val + 8 := + epTSA_val (hP.epPlaced he) + +theorem a_head (hP : EPlaced T E R Bf) {e : U64} (he : e ∈ E) : + Aligned (epHeadA e) ∧ EFoot T E R Bf (epHeadA e) := + ⟨(ep_aligned (hP.epPlaced he)).1, Or.inl ⟨e, he, Or.inl (val_head e)⟩⟩ +theorem a_ts (hP : EPlaced T E R Bf) {e : U64} (he : e ∈ E) : Aligned (epTSA e) ∧ EFoot T E R Bf (epTSA e) := + ⟨(ep_aligned (hP.epPlaced he)).2, Or.inl ⟨e, he, Or.inr (val_ts hP he)⟩⟩ +theorem a_rtcb (hP : EPlaced T E R Bf) {r : U64} (hr : r ∈ R) : Aligned (rTcbA r) ∧ EFoot T E R Bf (rTcbA r) := by + refine ⟨?_, Or.inr (Or.inl ⟨r, hr, rTcbA_val r⟩)⟩ + have := (hP.rep r hr).1 + unfold Aligned; rw [rTcbA_val]; omega +/-- A buffer word: aligned, in the footprint. -/ +theorem a_buf (hP : EPlaced T E R Bf) {bp : U64 × Fl4SpecA64.Word} (hb : bp ∈ Bf) {k : U64} (hk : k.val < 128) : + Aligned (bufW bp.1 k) ∧ EFoot T E R Bf (bufW bp.1 k) := + ⟨hP.balign _ ⟨bp, hb, k, hk, rfl⟩, Or.inr (Or.inr (Or.inr ⟨bp, hb, k, hk, rfl⟩))⟩ + +end + +/-! ## Frames -/ + +section Frame +open Fl4SpecA64 +variable {Z : Type} {T E R : Set U64} {Bf : Set (U64 × Word)} + +theorem TcbRelE.frame {m m' : U64 → U64} {t : U64} {x : Tcb} (ht : TcbAddr t) (h : TcbRelE R m t x) + (he : Agree m m' (TF t)) : TcbRelE R m' t x := by + have e_st := he _ (a_st ht).1 (a_st ht).2 + have e_blk := he _ (a_blk ht).1 (a_blk ht).2 + have e_badge := he _ (a_badge ht).1 (a_badge ht).2 + have e_flags := he _ (a_flags ht).1 (a_flags ht).2 + have e_reply := he _ (a_reply ht).1 (a_reply ht).2 + have e_bound := he _ (a_bound ht).1 (a_bound ht).2 + have e_fault := he _ (a_fault ht).1 (a_fault ht).2 + have e_prio := he _ (a_prio ht).1 (a_prio ht).2 + refine ⟨by rw [e_st]; exact h.st, fun e hb => by rw [e_blk]; exact h.blk e hb, + fun e d hs => by rw [e_badge, e_flags]; exact h.send e d hs, + fun e r d hs => by rw [e_reply]; exact h.recv e r d hs, fun r hs => by rw [e_reply]; exact h.reply r hs, + fun r => by rw [he _ (a_reg ht r).1 (a_reg ht r).2]; exact h.regs r, by rw [e_prio]; exact h.prio, + by rw [e_fault]; exact h.fault0, by rw [e_fault]; exact h.faultT, by rw [e_bound]; exact h.bound, + h.faultR.congr e_fault (he _ (a_fw0 ht).1 (a_fw0 ht).2) (he _ (a_fw1 ht).1 (a_fw1 ht).2) + (he _ (a_lf ht).1 (a_lf ht).2) (he _ (a_lf0 ht).1 (a_lf0 ht).2) (he _ (a_lf1 ht).1 (a_lf1 ht).2) + (he _ (a_fw2 ht).1 (a_fw2 ht).2)⟩ + +/-- What an endpoint needs of a member of its queue: state, block object and links. -/ +def MemberEq (m m' : U64 → U64) (y : U64) : Prop := + m' (stA y) = m (stA y) ∧ m' (tBlockA y) = m (tBlockA y) ∧ TLinksEq m m' y + +theorem EpObjRel.frame {m m' : U64 → U64} {e : U64} {o : Endpoint} (h : EpObjRel m T e o) + (h1 : m' (epHeadA e) = m (epHeadA e)) (h2 : m' (epTSA e) = m (epTSA e)) + (hy : ∀ y ∈ T, specPtr y ∈ o.epQueue → MemberEq m m' y) : EpObjRel m' T e o := by + have hq : qOf m' e = qOf m e := by simp only [qOf, h1, h2] + have hs : stOf m' e = stOf m e := by simp only [stOf, h2] + cases o with + | idleEP => + obtain ⟨a, b⟩ := h + exact ⟨hs ▸ a, hq ▸ queueRel_congr_links b (by simp)⟩ + | sendEP q => + obtain ⟨a, xs, hne, rfl, b, c⟩ := h + have hy' : ∀ y ∈ xs, MemberEq m m' y := fun y hyx => + hy y (b.mem y hyx) (by simp only [Endpoint.epQueue]; exact List.mem_map_of_mem hyx) + refine ⟨hs ▸ a, xs, hne, rfl, hq ▸ queueRel_congr_links b (fun y hyx => (hy' y hyx).2.2), fun y hyx => ?_⟩ + obtain ⟨c1, c2⟩ := c y hyx + exact ⟨(hy' y hyx).1 ▸ c1, (hy' y hyx).2.1 ▸ c2⟩ + | recvEP q => + obtain ⟨a, xs, hne, rfl, b, c⟩ := h + have hy' : ∀ y ∈ xs, MemberEq m m' y := fun y hyx => + hy y (b.mem y hyx) (by simp only [Endpoint.epQueue]; exact List.mem_map_of_mem hyx) + refine ⟨hs ▸ a, xs, hne, rfl, hq ▸ queueRel_congr_links b (fun y hyx => (hy' y hyx).2.2), fun y hyx => ?_⟩ + obtain ⟨c1, c2⟩ := c y hyx + exact ⟨(hy' y hyx).1 ▸ c1, (hy' y hyx).2.1 ▸ c2⟩ + +/-- The queue of a related endpoint, whatever its state. -/ +theorem EpObjRel.queue {m : U64 → U64} {e : U64} {o : Endpoint} (h : EpObjRel m T e o) : + ∃ xs, QueueRel m T .Ep (qOf m e) xs ∧ o.epQueue = xs.map specPtr ∧ + ∀ y ∈ xs, m (tBlockA y) = e ∧ ((m (stA y)).val = 3 ∨ (m (stA y)).val = 4) := by + cases o with + | idleEP => exact ⟨[], h.2, rfl, by simp⟩ + | sendEP q => + obtain ⟨-, xs, -, rfl, h2, h3⟩ := h + exact ⟨xs, h2, rfl, fun y hy => ⟨(h3 y hy).2, Or.inr (h3 y hy).1⟩⟩ + | recvEP q => + obtain ⟨-, xs, -, rfl, h2, h3⟩ := h + exact ⟨xs, h2, rfl, fun y hy => ⟨(h3 y hy).2, Or.inl (h3 y hy).1⟩⟩ + +/-- Members of an endpoint queue are TCBs blocked on the endpoint. -/ +theorem EpObjRel.blocked {m : U64 → U64} {e : U64} {o : Endpoint} (h : EpObjRel m T e o) + {y : U64} (hy : specPtr y ∈ o.epQueue) : y ∈ T ∧ m (tBlockA y) = e := by + obtain ⟨xs, h1, h2, h3⟩ := h.queue + rw [h2, List.mem_map] at hy + obtain ⟨z, hz, ez⟩ := hy + have : z = y := specPtr_inj ez + subst this + exact ⟨h1.mem z hz, (h3 z hz).1⟩ + +/-- Memory that agrees on the footprint. -/ +def FootEq (T E R : Set U64) (Bf : Set (U64 × Word)) (m m' : U64 → U64) : Prop := Agree m m' (EFoot T E R Bf) + +/-- The relation only reads the footprint and `KView`. -/ +theorem EpStateRel.frame {m m' : U64 → U64} {σ σ' : State Z} (hR : EpStateRel T E R Bf m σ) + (hP : EPlaced T E R Bf) (hm : FootEq T E R Bf m m') (hk : KView σ σ') : EpStateRel T E R Bf m' σ' := by + refine ⟨fun e he => ?_, fun r hr => ?_, fun t ht => ?_, fun bp hb k hk' => ?_⟩ + · obtain ⟨x, hkx, hx⟩ := hR.ep e he + refine ⟨x, hk.1 _ _ hkx, hx.frame (hm _ (a_head hP he).1 (a_head hP he).2) (hm _ (a_ts hP he).1 (a_ts hP he).2) + fun y hy _ => ?_⟩ + have hya := hP.tcb y hy + exact ⟨hm _ (a_st hya).1 (Or.inr (Or.inr (Or.inl ⟨y, hy, Or.inl (a_st hya).2⟩))), + hm _ (a_blk hya).1 (Or.inr (Or.inr (Or.inl ⟨y, hy, Or.inl (a_blk hya).2⟩))), + hm _ (a_prev hya).1 (Or.inr (Or.inr (Or.inl ⟨y, hy, Or.inr (a_prev hya).2⟩))), + hm _ (a_next hya).1 (Or.inr (Or.inr (Or.inl ⟨y, hy, Or.inr (a_next hya).2⟩)))⟩ + · obtain ⟨x, hkx, hx⟩ := hR.reply r hr + obtain ⟨y, hky, ey⟩ := hk.2.1 _ _ hkx + exact ⟨y, hky, by rw [ey, hx, hm _ (a_rtcb hP hr).1 (a_rtcb hP hr).2]⟩ + · obtain ⟨x, hkx, hx⟩ := hR.tcb t ht + obtain ⟨y, hky, e1, e2, e3, e4, e5⟩ := hk.2.2.1 _ _ hkx + have hta := hP.tcb t ht + have hx' := hx.frame hta (fun a ha hf => hm a ha (Or.inr (Or.inr (Or.inl ⟨t, ht, Or.inl hf⟩)))) + refine ⟨y, hky, ⟨e1 ▸ hx'.st, fun e hb => hx'.blk e (e1 ▸ hb), fun e d hs => hx'.send e d (e1 ▸ hs), + fun e r d hs => hx'.recv e r d (e1 ▸ hs), fun r hs => hx'.reply r (e1 ▸ hs), fun r => e2 ▸ hx'.regs r, + e3 ▸ hx'.prio, e4 ▸ hx'.fault0, e4 ▸ hx'.faultT, e5 ▸ hx'.bound, e4 ▸ hx'.faultR⟩⟩ + · rw [hm _ (a_buf hP hb hk').1 (a_buf hP hb hk').2, hk.2.2.2] + exact hR.buf bp hb k hk' + +theorem KView.refl (σ : State Z) : KView σ σ := + ⟨fun _ _ h => h, fun _ x h => ⟨x, h, rfl⟩, fun _ x h => ⟨x, h, rfl, rfl, rfl, rfl, rfl⟩, rfl⟩ + +theorem KView.trans {σ₁ σ₂ σ₃ : State Z} (h₁ : KView σ₁ σ₂) (h₂ : KView σ₂ σ₃) : KView σ₁ σ₃ := by + refine ⟨fun p x h => h₂.1 p x (h₁.1 p x h), fun p x h => ?_, fun p x h => ?_, h₂.2.2.2.trans h₁.2.2.2⟩ + · obtain ⟨y, hy, e⟩ := h₁.2.1 p x h + obtain ⟨z, hz, f⟩ := h₂.2.1 p y hy + exact ⟨z, hz, f.trans e⟩ + · obtain ⟨y, hy, e1, e2, e3, e4, e5⟩ := h₁.2.2.1 p x h + obtain ⟨z, hz, f1, f2, f3, f4, f5⟩ := h₂.2.2.1 p y hy + exact ⟨z, hz, f1.trans e1, f2.trans e2, f3.trans e3, f4.trans e4, f5.trans e5⟩ + +theorem EpStep.refl (σ : State Z) : EpStep σ σ := ⟨rfl, rfl, fun _ => Or.inl rfl⟩ + +end Frame + +end + +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/EpReplyPushA.lean b/proofs/Fl4ProofsA64/P3/EpReplyPushA.lean new file mode 100644 index 000000000..8887beeb6 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/EpReplyPushA.lean @@ -0,0 +1,156 @@ +/- +P3 (AArch64): the reply link and caller block shared by both `reply_push` branches. +The endpoint scheduler interface supplies only its usual step and block laws. +-/ +import Fl4ProofsA64.P3.EpSendBlkA +import Fl4ProofsA64.P3.EpCancelA +import Fl4ProofsA64.P1.QueueAppend + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4SpecA64 + +noncomputable section + +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {T E R : Set U64} {Bf : Set (U64 × Word)} {SR : S → State Unit → Prop} + +theorem blockOnReplyWrites (P : EPlaced T E R Bf) {s : S} {t r : U64} (ht : t ∈ T) : + let s' := L.wr (L.wr s (stA t) sched.TS_BLOCKED_ON_REPLY) (tReplyA t) r + Agree (L.mem s) (L.mem s') (fun b => EFoot T E R Bf b ∧ ¬ TF t b) ∧ + Agree (L.mem s) (L.mem s') (TStatA t) ∧ + (L.mem s' (stA t)).val = 5 ∧ L.mem s' (tReplyA t) = r ∧ + WritesIn st s s' (EWrite T E R Bf) := by + intro s' + have htA := P.tcb t ht + have hs := a_st htA + have hr := a_reply htA + have vs := val_st htA + have vr := val_reply htA + have ha := agree_wr2A (L := L) (s := s) (v := sched.TS_BLOCKED_ON_REPLY) (w := r) hs.1 hr.1 + refine ⟨ha.mono (fun _ h => ⟨fun eq => h.2 (TF.of_val hs.2 eq), fun eq => h.2 (TF.of_val hr.2 eq)⟩), + ha.mono (fun _ h => by have := h.2; rw [vs, vr]; omega), ?_, L.mem_wr_same hr.1, ?_⟩ + · change (L.mem (L.wr (L.wr s _ _) _ _) _).val = _ + rw [L.mem_wr_ne hr.1 hs.1 (by rw [vr, vs]; omega), L.mem_wr_same hs.1] + unfold sched.TS_BLOCKED_ON_REPLY + rfl + · exact ((wr_ewrite (E := E) (R := R) (Bf := Bf) ht + (twf_ofA 3328 vs (by unfold TWOffN; omega)) hs.1 s _).trans + (wr_ewrite (E := E) (R := R) (Bf := Bf) ht + (twf_ofA 3360 vr (by unfold TWOffN; omega)) hr.1 _ _)).mono (fun _ h => h.elim id id) + +/-- Block the caller on a reply and update the scheduling action. -/ +theorem blockOnReplyA (O : SchedOpsEA st T E R Bf SR) (P : EPlaced T E R Bf) + {s : S} {σ : State Unit} (hR : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) + {t : U64} (ht : t ∈ T) (r : U64) {x : Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hEp : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) + (hQ : NotQueued σ (specPtr t)) (hRB : RelIfBound σ (specPtr t)) + (hAct : ∀ u, σ.schedulerAction = .switchThread u → u ≠ specPtr t) (hIdle : specPtr t ≠ σ.idleThread) : + sched.set_blocked_on_reply st s t r ⦃ s' => + EpStateRel T E R Bf (L.mem s') (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := .blockedOnReply (specPtr r) })) (specPtr t)) ∧ + SR s' (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := .blockedOnReply (specPtr r) })) (specPtr t)) ⦄ := by + unfold sched.set_blocked_on_reply + refine WP.spec_bind (obj.Tcb.set_state.spec (L := L) s t _) ?_ + rintro s1 rfl + refine WP.spec_bind (obj.Tcb.set_reply_obj.wr (L := L) _ t r) ?_ + rintro s2 rfl + obtain ⟨hAgree, hFields, hst, hrep, hW⟩ := blockOnReplyWrites (st := st) P (s := s) (r := r) ht + let y : Tcb := { x with tcbState := .blockedOnReply (specPtr r) } + have hX : TcbRelE R (L.mem (L.wr (L.wr s (stA t) sched.TS_BLOCKED_ON_REPLY) (tReplyA t) r)) t y := by + refine (hR.at_tcb ht hk).restateA (P.tcb t ht) hFields rfl rfl rfl rfl hst ?_ ?_ ?_ ?_ + · intro e h + cases h + · intro e d h + cases h + · intro e r d h + cases h + · intro r' h + cases h + rw [hrep] + have hRel := hR.upd_tcb P ht y hAgree (Or.inl hEp) hX + have hS := O.block _ _ _ _ t hSR hQ hRB hAct hIdle hW (EpStep.tcb hk y.tcbState x.tcbArch) + (fun p x' y' h0 h1 hp => by + rw [updK_kheap, Function.update_of_ne hp, h0] at h1 + cases h1 + exact ⟨rfl, id⟩) + refine WP.spec_mono (O.scheduleTcb _ _ _ hRel hS ht) ?_ + rintro s' ⟨hf, hnew⟩ + exact ⟨hRel.frame P hf (KView.stsAct' _ _), hnew⟩ + +/-- Link the reply to the caller, keeping the scheduler relation. -/ +theorem linkReplyA (O : SchedOpsE0A st T E R Bf SR) (P : EPlaced T E R Bf) + {s : S} {σ : State Unit} (hR : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) + {t r : U64} (ht : t ∈ T) (hr : r ∈ R) {x : Reply} (hk : σ.kheap (specPtr r) = some (.reply x)) : + obj.Reply.set_tcb st r s t ⦃ s' => + EpStateRel T E R Bf (L.mem s') (updK σ (specPtr r) (.reply { x with replyTcb := some (specPtr t) })) ∧ + SR s' (updK σ (specPtr r) (.reply { x with replyTcb := some (specPtr t) })) ⦄ := by + refine WP.spec_mono (obj.Reply.set_tcb.spec (L := L) s r t) ?_ + rintro s' rfl + have ha := a_rtcb P hr + have hA := agree_wr (L := L) (s := s) (v := t) ha.1 + have ht0 : t ≠ 0#u64 := fun h => (P.tcb t ht).ne0 (by rw [h]; rfl) + have hW := (writesIn_wr (st := st) (s := s) (v := t) ha.1).mono + (fun b eq => show EWrite T E R Bf b from Or.inr (Or.inl ⟨r, hr, by rw [eq, rTcbA_val]⟩)) + refine ⟨hR.upd_reply P hr _ (hA.mono (fun b h => by rw [rTcbA_val]; exact h.2)) ?_, + O.stable _ _ _ _ hSR hW (EpStep.reply hk _) (NoBlockE.updO _ _ (by intro y eq; cases eq))⟩ + rw [L.mem_wr_same ha.1, optRef_ne ht0] + +private theorem noDonationPushSpec {σ : State Unit} {t d r : ObjRef} {x y : Tcb} {xr : Reply} + (ht : σ.kheap t = some (.tcb x)) (hd : σ.kheap d = some (.tcb y)) (hr : σ.kheap r = some (.reply xr)) + (ht' : (updK σ r (.reply { xr with replyTcb := some t })).kheap t = some (.tcb x)) : + replyPush (Z := Unit) t d r false σ = + ({((), stsAct (updK (updK σ r (.reply { xr with replyTcb := some t })) t + (.tcb { x with tcbState := .blockedOnReply r })) t)}, False) := by + unfold replyPush + rw [nd_bind_one (getTcbObjRef_eq _ ht), nd_bind_one (getTcbObjRef_eq _ hd), + nd_bind_one (setReplyTcb_eq _ hr), nd_bind_one (setThreadState_eq _ ht')] + simp [Fl4Spec.NonDet.when, Fl4Spec.NonDet.pure] + +/-- Non-donating `reply_push`, including the direct caller block and reply backlink. -/ +theorem reply_push_noDonateA (O : SchedOpsEA st T E R Bf SR) (P : EPlaced T E R Bf) + (s : S) (a : A) (σ : State Unit) (hR : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) + {t d r : U64} (ht : t ∈ T) (hd : d ∈ T) (hr : r ∈ R) {x : Tcb} + (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hEp : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) + (hQ : NotQueued σ (specPtr t)) (hRB : RelIfBound σ (specPtr t)) + (hAct : ∀ u, σ.schedulerAction = .switchThread u → u ≠ specPtr t) (hIdle : specPtr t ≠ σ.idleThread) : + ipc.reply_push st ar s a t d r false ⦃ out => out.2 = a ∧ ∃ σ', + replyPush (Z := Unit) (specPtr t) (specPtr d) (specPtr r) false σ = ({((), σ')}, False) ∧ + EpStateRel T E R Bf (L.mem out.1) σ' ∧ SR out.1 σ' ⦄ := by + obtain ⟨xr, hkr, _⟩ := hR.reply r hr + obtain ⟨xd, hkd, _⟩ := hR.tcb d hd + have htr := kheap_ne hk hkr (by intro o e f; rw [← e] at f; cases f) + let σ1 := updK σ (specPtr r) (.reply { xr with replyTcb := some (specPtr t) }) + have hkt1 : σ1.kheap (specPtr t) = some (.tcb x) := by + simp [σ1, htr, hk] + have hEp1 : ∀ e ∈ E, ∀ z, σ1.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue := by + intro e he z hz + by_cases h : specPtr e = specPtr r + · simp [σ1, h] at hz + · exact hEp e he z (by simpa [σ1, h] using hz) + have hRB1 : RelIfBound σ1 (specPtr t) := + RelIfBound.keep hRB rfl rfl (fun x' hx => by rw [hkt1] at hx; cases hx; exact ⟨x, hk, rfl⟩) + (fun p y n hy => by + by_cases e : p = specPtr r + · simp [σ1, e] at hy + · simpa [σ1, e] using hy) + have hsp := noDonationPushSpec hk hkd hkr hkt1 + unfold ipc.reply_push + refine WP.spec_bind (obj.Tcb.sc.spec (L := L) s t) ?_ + rintro sc _ + refine WP.spec_bind (linkReplyA O.to0 P hR hSR ht hr hkr) ?_ + rintro s1 ⟨hR1, hS1⟩ + refine WP.spec_bind (blockOnReplyA O P hR1 hS1 ht r hkt1 hEp1 hQ hRB1 hAct hIdle) ?_ + rintro s2 ⟨hR2, hS2⟩ + refine WP.spec_bind (obj.Sc.is_null.spec sc) ?_ + rintro b hb + cases b + · simp only [Bool.false_eq_true, ↓reduceIte] + refine WP.spec_bind (obj.Tcb.sc.spec (L := L) s2 d) ?_ + rintro dsc _ + split_ifs <;> exact (WP.spec_ok _).2 ⟨rfl, _, hsp, hR2, hS2⟩ + · exact (WP.spec_ok _).2 ⟨rfl, _, hsp, hR2, hS2⟩ + +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/EpReplyStackA.lean b/proofs/Fl4ProofsA64/P3/EpReplyStackA.lean new file mode 100644 index 000000000..4b5e6671f --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/EpReplyStackA.lean @@ -0,0 +1,38 @@ +/- +P3 (AArch64): IPC's reply NEXT and PREV words, with an abstract name for each SC. + +The stack is newest first. The scheduler relates the SC's REPLY word to its head. +IPC relates the reply words and the abstract reply's SC reference. Detached replies +may remain linked to other detached replies after a cut. No named premises. +-/ +import Fl4ProofsA64.P3.EpRelA +import Fl4ProofsA64.P1.QueueCancel + +open Aeneas Aeneas.Std + +namespace Fl4ProofsA64.P3.Endp +open Fl4ProofsA64.P1 Fl4SpecA64 + +/-- The reply words of one stack, from the head towards older callers. -/ +def ReplyStackWordsA (mem : U64 → U64) (sc : U64) (rs : List U64) : Prop := + ∀ i (hi : i < rs.length), + mem (rPrevA rs[i]) = rs.getD (i + 1) 0#u64 ∧ + mem (rNextA rs[i]) = (match i with | 0 => sc ||| 1#u64 | j + 1 => rs.getD j 0#u64) + +/-- Reply links and their interpretation against the spec's scheduler stacks. -/ +structure ReplyStackRelA (mem : U64 → U64) (R C : Set U64) (σ : State Unit) + (scRef : U64 → ObjRef) (stack : U64 → List U64) : Prop where + stackRel : ∀ sc ∈ C, (∃ x n, σ.kheap (scRef sc) = some (.schedContext x n) ∧ + x.scReplies = (stack sc).map specPtr) ∧ + (∀ r ∈ stack sc, r ∈ R) ∧ (stack sc).Nodup ∧ ReplyStackWordsA mem sc (stack sc) + unique : ∀ sc ∈ C, ∀ sc' ∈ C, ∀ r ∈ stack sc, r ∈ stack sc' → sc = sc' + owner : ∀ r ∈ R, ∀ x, σ.kheap (specPtr r) = some (.reply x) → ∀ p, + x.replySc = some p ↔ ∃ sc ∈ C, scRef sc = p ∧ (stack sc).head? = some r + detached : ∀ r ∈ R, (∀ sc ∈ C, r ∉ stack sc) → + headBit (mem (rNextA r)) = false ∧ + (mem (rNextA r) ≠ 0#u64 → mem (rNextA r) ∈ R ∧ (∀ sc ∈ C, mem (rNextA r) ∉ stack sc) ∧ + mem (rPrevA (mem (rNextA r))) = r) ∧ + (mem (rPrevA r) ≠ 0#u64 → mem (rPrevA r) ∈ R ∧ (∀ sc ∈ C, mem (rPrevA r) ∉ stack sc) ∧ + mem (rNextA (mem (rPrevA r))) = r) + +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/EpSendBlkA.lean b/proofs/Fl4ProofsA64/P3/EpSendBlkA.lean new file mode 100644 index 000000000..35279722d --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/EpSendBlkA.lean @@ -0,0 +1,504 @@ +/- +P3 (aarch64, A64-P3-IPC gen 12): **`send_ipc` on an idle endpoint or one with waiting senders refines l4v +`send_ipc`** (`Z = Unit`; x86 `P3/EpAppend` and `P3/EpBlock`, ported; FL4's AArch64 `send_ipc` / `ep_append` are +the x86 text): + +* `TcbRelE.restateA` (x86 `TcbRelE.restate`): a TCB's relation from its state part and memory that agrees on the + other fields (`TStatA`); +* `CanBlockA` / `epIns_eqA` / `stOf_canBlockA` (x86 `CanBlock` / `epIns_eq` / `stOf_canBlock`) and + **`ep_append_relA`** (x86 `ep_append_rel`): FL4's `ep_append` (`append_by_prio`) of a thread blocked on `e` + refines `tcb_ep_append` (`tcb_append`, equal on priority-sorted queues; P3/NtfnBlockA's `specIns_map`), one + `SchedOpsEA.stable` step (an endpoint write blocks no thread); +* `queue_sameA` / `sorted_transferA` (sortedness carried to a state with the same endpoint and priorities), + `block_send_writesA` (the four TCB words of a blocking send), and **`send_ipc_blockA`**: a blocking send sets the + thread `BlockedOnSend e ⦇badge, canGrant, canGrantReply, isCall⦈` (the gen-12 law **`SchedOpsEA.block`**: + the sender, the current thread, blocks; its scheduler facts `SndRunA`, `RelIfBound` by `SchedOpsEA.rd`), then + `schedule_tcb` ↔ `set_thread_state_act`, then `ep_append_relA`: the endpoint becomes `SendEP`; + **`send_ipc_noblockA`**: a non-blocking send does nothing, as the spec. + +In both the spec has exactly one outcome and does not fail. Case selectors / placement (no named premise): `EPlaced`, +`CanBlockA false` (the endpoint is idle or has senders), `SndRunA` (the sender is the running thread: runnable, in no +scheduler queue, not the switch target, not idle; P3/NtfnBlockA's `hRun` / `hNQ` / `hsane` / `hnidle`), `hsort` (the +queue is priority-sorted: A64 P1's `PrioSorted`, kept by `append_by_prio`). +-/ +import Fl4ProofsA64.P3.EpSendRecvA +import Fl4ProofsA64.P3.NtfnBlockA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched + +noncomputable section + +section +variable {T E R : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + +/-- The fields of a TCB the IPC state does not involve: registers, bound notification, fault, priority. -/ +def TStatA (t b : U64) : Prop := TF t b ∧ (b.val < t.val + 3328 ∨ t.val + 3368 ≤ b.val) + +/-- A TCB's relation, from its state part and memory that agrees on the other fields (x86 `TcbRelE.restate`). -/ +theorem TcbRelE.restateA {m m' : U64 → U64} {t : U64} {x x' : Fl4SpecA64.Tcb} (ht : TcbAddr t) + (h : TcbRelE R m t x) (hA : Agree m m' (TStatA t)) + (e1 : x'.tcbArch.tcbContext.userRegs = x.tcbArch.tcbContext.userRegs) (e2 : x'.tcbPriority = x.tcbPriority) + (e3 : x'.tcbFault = x.tcbFault) (e4 : x'.tcbBoundNotification = x.tcbBoundNotification) + (hst : (m' (stA t)).val = tsCode x'.tcbState) + (hblk : ∀ e, Fl4SpecA64.epBlocked x'.tcbState = some e → specPtr (m' (tBlockA t)) = e) + (hsend : ∀ e d, x'.tcbState = .blockedOnSend e d → (m' (tBadgeA t)).bv = d.senderBadge ∧ + m' (tFlagsA t) = flagsOf d) + (hrecv : ∀ e r d, x'.tcbState = .blockedOnReceive e r d → optRef (m' (tReplyA t)) = r ∧ + (r ≠ none → m' (tReplyA t) ∈ R)) + (hrep : ∀ r, x'.tcbState = .blockedOnReply r → specPtr (m' (tReplyA t)) = r) : + TcbRelE R m' t x' := by + have g : ∀ {b}, Aligned b ∧ TF t b → (b.val < t.val + 3328 ∨ t.val + 3368 ≤ b.val) → m' b = m b := + fun hb hv => hA _ hb.1 ⟨hb.2, hv⟩ + refine ⟨hst, hblk, hsend, hrecv, hrep, fun r => ?_, ?_, ?_, ?_, ?_, ?_⟩ + · rw [g (a_reg ht r) (by rw [regA_val ht (regIdxA_lt r)]; have := regIdxA_lt r; omega), e1]; exact h.regs r + · rw [g (a_prio ht) (by rw [val_prio ht]; omega), e2]; exact h.prio + · rw [g (a_fault ht) (by rw [val_fault ht]; omega), e3]; exact h.fault0 + · rw [g (a_fault ht) (by rw [val_fault ht]; omega), e3]; exact h.faultT + · rw [g (a_bound ht) (by rw [val_bound ht]; omega), e4]; exact h.bound + · rw [e3] + exact h.faultR.congr (g (a_fault ht) (by rw [val_fault ht]; omega)) (g (a_fw0 ht) (by rw [val_fw0 ht]; omega)) + (g (a_fw1 ht) (by rw [val_fw1 ht]; omega)) (g (a_lf ht) (by rw [val_lf ht]; omega)) + (g (a_lf0 ht) (by rw [val_lf0 ht]; omega)) (g (a_lf1 ht) (by rw [val_lf1 ht]; omega)) + (g (a_fw2 ht) (by rw [val_fw2 ht]; omega)) + +/-- The endpoints a thread can block on: idle, or already queued the same way. -/ +def CanBlockA (isRecv : Bool) : Fl4SpecA64.Endpoint → Prop + | .idleEP => True + | .sendEP _ => isRecv = false + | .recvEP _ => isRecv = true + +theorem epIns_eqA {x : Fl4SpecA64.Endpoint} {isRecv : Bool} (h : CanBlockA isRecv x) (q : List Fl4SpecA64.ObjRef) : + Fl4SpecA64.updateEpQueue x q isRecv = if isRecv then .recvEP q else .sendEP q := by + cases x <;> cases isRecv <;> simp [CanBlockA] at h <;> rfl + +theorem stOf_canBlockA {m : U64 → U64} {e : U64} {x : Fl4SpecA64.Endpoint} {isRecv : Bool} (h : EpObjRel m T e x) + (hc : CanBlockA isRecv x) : + ∀ y ∈ x.epQueue, ∃ y', y = specPtr y' ∧ QueuedOn m e (if isRecv then 3 else 4) y' := by + intro y hy + cases x with + | idleEP => simp [Fl4SpecA64.Endpoint.epQueue] at hy + | sendEP q => + simp only [CanBlockA] at hc; subst hc + obtain ⟨-, xs, -, rfl, -, h3⟩ := h + simp only [Fl4SpecA64.Endpoint.epQueue, List.mem_map] at hy + obtain ⟨y', hy', rfl⟩ := hy + exact ⟨y', rfl, h3 y' hy'⟩ + | recvEP q => + simp only [CanBlockA] at hc; subst hc + obtain ⟨-, xs, -, rfl, -, h3⟩ := h + simp only [Fl4SpecA64.Endpoint.epQueue, List.mem_map] at hy + obtain ⟨y', hy', rfl⟩ := hy + exact ⟨y', rfl, h3 y' hy'⟩ + +/-- The queue of a related endpoint is the list of the relation, in any related state where the endpoint is the +same. -/ +theorem queue_sameA {m m' : U64 → U64} {e : U64} {x : Fl4SpecA64.Endpoint} (hT : TcbPlaced T) + (h : EpObjRel m T e x) (h' : EpObjRel m' T e x) {xs : List U64} + (hq : QueueRel m' T .Ep (qOf m' e) xs) : QueueRel m T .Ep (qOf m e) xs := by + obtain ⟨ys, hy1, hy2, -⟩ := h.queue + obtain ⟨zs, hz1, hz2, -⟩ := h'.queue + have e1 : xs = zs := hq.unique hT hz1 + have e2 : zs = ys := List.map_injective_iff.2 (fun a b h => specPtr_inj h) (hz2.symm.trans hy2) + rw [e1, e2]; exact hy1 + +/-- Sortedness carries over to a state with the same endpoint and priorities. -/ +theorem sorted_transferA {m m' : U64 → U64} {e : U64} {x : Fl4SpecA64.Endpoint} (hT : TcbPlaced T) + (h : EpObjRel m T e x) (h' : EpObjRel m' T e x) (hpr : ∀ y ∈ T, m' (prioA y) = m (prioA y)) + (hsort : ∀ xs, QueueRel m T .Ep (qOf m e) xs → PrioSorted (fun y => m (prioA y)) xs) : + ∀ xs, QueueRel m' T .Ep (qOf m' e) xs → PrioSorted (fun y => m' (prioA y)) xs := by + intro xs hq + have hq0 := queue_sameA hT h h' hq + exact prioSorted_congr (hsort xs hq0) fun y hy => hpr y (hq.mem y hy) + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {SR : S → Fl4SpecA64.State Unit → Prop} + +/-- **`ep_append`** of a thread blocked on `e` refines `tcb_ep_append` (x86 `ep_append_rel`). -/ +theorem ep_append_relA0 (hO : SchedOpsE0A st T E R Bf SR) (hP : EPlaced T E R Bf) (s : S) + (σ : Fl4SpecA64.State Unit) (hR : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) {t : U64} (ht : t ∈ T) + {xt : Fl4SpecA64.Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) {e : U64} (he : e ∈ E) + {x : Fl4SpecA64.Endpoint} (hk : σ.kheap (specPtr e) = some (.endpoint x)) (isRecv : Bool) + (hc : CanBlockA isRecv x) (htq : specPtr t ∉ x.epQueue) + (htst : (L.mem s (stA t)).val = if isRecv then 3 else 4) (htb : L.mem s (tBlockA t) = e) + (hsort : ∀ xs, QueueRel (L.mem s) T .Ep (qOf (L.mem s) e) xs → PrioSorted (fun y => L.mem s (prioA y)) xs) : + endpoint.ep_append st s e t (if isRecv then obj.EP_RECV else obj.EP_SEND) ⦃ s' => + ∃ σ', Fl4SpecA64.tcbEpAppend (Z := Unit) (specPtr t) (specPtr e) isRecv σ = ({((), σ')}, False) ∧ + EpStateRel T E R Bf (L.mem s') σ' ∧ SR s' σ' ∧ WritesIn st s s' (EWrite T E R Bf) ⦄ := by + have hx := hR.at_ep he hk + have hxt := hR.at_tcb ht hkt + obtain ⟨xs, hQ, hxs, hB⟩ := hx.queue + have htxs : t ∉ xs := fun h => htq (by rw [hxs]; exact List.mem_map_of_mem h) + have hsorted := hsort xs hQ + refine WP.spec_mono (ep_append_full hP s he t _ xs hQ ht htxs) ?_ + rintro s' ⟨hA1, hA2, hA3, hA4⟩ + set ys := insPrio (fun y => L.mem s (prioA y)) (L.mem s (prioA t)) t xs + -- the spec + have hq : ∀ y ∈ x.epQueue, ∃ z, σ.kheap y = some (.tcb z) ∧ z.tcbPriority = prioOf σ y := by + intro y hy + rw [hxs] at hy + obtain ⟨y', hy'x, rfl⟩ := List.mem_map.1 hy + obtain ⟨z, hz, -⟩ := hR.tcb y' (hQ.mem y' hy'x) + exact ⟨z, hz, by simp [prioOf, hz]⟩ + have hlist : specIns x.epQueue (prioOf σ) xt.tcbPriority (specPtr t) = ys.map specPtr := by + rw [hxs] + exact specIns_map hsorted hxt.prio fun y hy => by + obtain ⟨z, hz, hzr⟩ := hR.tcb y (hQ.mem y hy) + exact ⟨z, hz, hzr.prio⟩ + refine ⟨_, by rw [tcbEpAppend_eq hk hkt (prioOf σ) hq, hlist], ?_, ?_, hA4⟩ + · -- the relation + refine hR.upd_ep hP he _ (hA3.mono fun b ⟨h1, h2, h3⟩ => ⟨h2, fun ⟨w, hw, hl⟩ => h3 ⟨w, ?_, ?_, hl⟩⟩) ?_ + · rcases List.mem_cons.1 hw with rfl | hw + · exact ht + · exact hQ.mem w hw + · rcases List.mem_cons.1 hw with rfl | hw + · exact htb + · exact (hB w hw).1 + rw [epIns_eqA (show CanBlockA isRecv x from hc)] + have hmem : ∀ y ∈ ys, y = t ∨ y ∈ xs := fun y hy => mem_insPrio_sorted hsorted hy + have hyT : ∀ y ∈ ys, y ∈ T := fun y hy => by + rcases hmem y hy with rfl | hy' + · exact ht + · exact hQ.mem y hy' + have hst : ∀ y ∈ ys, QueuedOn (L.mem s') e (if isRecv then 3 else 4) y := by + intro y hy + have hya := hP.tcb y (hyT y hy) + have nst : ¬ EW e (stA y) ∧ ¬ ∃ w ∈ t :: xs, TL w (stA y) := by + refine ⟨fun hw => ?_, fun ⟨w, hw, hl⟩ => ?_⟩ + · have := hP.et e he y (hyT y hy); unfold EW at hw; rw [val_st hya] at hw; omega + · by_cases ew : w = y + · subst ew; exact hl.not_tf (a_st hya).2 + · have hwT : w ∈ T := by + rcases List.mem_cons.1 hw with rfl | hw + · exact ht + · exact hQ.mem w hw + have := tcb_disj hP hwT (hyT y hy) (Ne.symm ew); unfold TL at hl; rw [val_st hya] at hl; omega + have nbl : ¬ EW e (tBlockA y) ∧ ¬ ∃ w ∈ t :: xs, TL w (tBlockA y) := by + refine ⟨fun hw => ?_, fun ⟨w, hw, hl⟩ => ?_⟩ + · have := hP.et e he y (hyT y hy); unfold EW at hw; rw [val_blk hya] at hw; omega + · by_cases ew : w = y + · subst ew; exact hl.not_tf (a_blk hya).2 + · have hwT : w ∈ T := by + rcases List.mem_cons.1 hw with rfl | hw + · exact ht + · exact hQ.mem w hw + have := tcb_disj hP hwT (hyT y hy) (Ne.symm ew); unfold TL at hl; rw [val_blk hya] at hl; omega + rw [QueuedOn, hA3 _ (a_st hya).1 nst, hA3 _ (a_blk hya).1 nbl] + rcases hmem y hy with rfl | hy' + · exact ⟨htst, htb⟩ + · obtain ⟨y'', e1, hq''⟩ := stOf_canBlockA hx hc (specPtr y) (by rw [hxs]; exact List.mem_map_of_mem hy') + have : y = y'' := specPtr_inj e1 + subst this + exact hq'' + have hne : ys ≠ [] := insPrio_ne_nil hsorted + cases isRecv with + | true => + refine ⟨?_, ys, hne, rfl, hA1, fun y hy => by simpa using hst y hy⟩ + rw [hA2]; simp only [↓reduceIte, ep_state_vals]; decide + | false => + refine ⟨?_, ys, hne, rfl, hA1, fun y hy => by simpa using hst y hy⟩ + rw [hA2]; simp only [Bool.false_eq_true, ↓reduceIte, ep_state_vals]; decide + · exact hO.stable _ _ _ _ hSR hA4 (EpStep.ep hk) (NoBlockE.updO _ _ fun _ h => by cases h) + +/-- `ep_append_relA0` from `SchedOpsEA`. -/ +theorem ep_append_relA (hO : SchedOpsEA st T E R Bf SR) (hP : EPlaced T E R Bf) (s : S) + (σ : Fl4SpecA64.State Unit) (hR : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) {t : U64} (ht : t ∈ T) + {xt : Fl4SpecA64.Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) {e : U64} (he : e ∈ E) + {x : Fl4SpecA64.Endpoint} (hk : σ.kheap (specPtr e) = some (.endpoint x)) (isRecv : Bool) + (hc : CanBlockA isRecv x) (htq : specPtr t ∉ x.epQueue) + (htst : (L.mem s (stA t)).val = if isRecv then 3 else 4) (htb : L.mem s (tBlockA t) = e) + (hsort : ∀ xs, QueueRel (L.mem s) T .Ep (qOf (L.mem s) e) xs → PrioSorted (fun y => L.mem s (prioA y)) xs) : + endpoint.ep_append st s e t (if isRecv then obj.EP_RECV else obj.EP_SEND) ⦃ s' => + ∃ σ', Fl4SpecA64.tcbEpAppend (Z := Unit) (specPtr t) (specPtr e) isRecv σ = ({((), σ')}, False) ∧ + EpStateRel T E R Bf (L.mem s') σ' ∧ SR s' σ' ∧ WritesIn st s s' (EWrite T E R Bf) ⦄ := + ep_append_relA0 hO.to0 hP s σ hR hSR ht hkt he hk isRecv hc htq htst htb hsort + +end + +end + +/-! ## The spec side -/ + +section Spec +open Fl4SpecA64 Fl4Spec + +theorem sendIpc_noblockA {σ : State Unit} {e : ObjRef} {x : Endpoint} (hk : σ.kheap e = some (.endpoint x)) + (hc : CanBlockA false x) (call : Bool) (badge : Badge) (g gr d : Bool) (t : ObjRef) : + sendIpc (Z := Unit) false call badge g gr d t e σ = ({((), σ)}, False) := by + unfold sendIpc + rw [nd_bind_one (getEndpoint_eq hk)] + cases x with + | idleEP => rfl + | sendEP q => rfl + | recvEP q => simp [CanBlockA] at hc + +theorem sendIpc_blockA {σ : State Unit} {e : ObjRef} {x : Endpoint} (hk : σ.kheap e = some (.endpoint x)) + (hc : CanBlockA false x) (call : Bool) (badge : Badge) (g gr d : Bool) (t : ObjRef) : + sendIpc (Z := Unit) true call badge g gr d t e σ = sendIpcBlocked t e badge g gr call σ := by + unfold sendIpc + rw [nd_bind_one (getEndpoint_eq hk)] + cases x with + | idleEP => rfl + | sendEP q => rfl + | recvEP q => simp [CanBlockA] at hc + +end Spec + +/-! ## `send_ipc` -/ + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] {T E R : Set U64} + {Bf : Set (U64 × Fl4SpecA64.Word)} {SR : S → Fl4SpecA64.State Unit → Prop} + {Wr : Fl4SpecA64.State Unit → S → U64 → Prop} + +theorem flags_eqA (g gr c : Bool) : + ((if g then endpoint.FLAG_GRANT else 0#u64) ||| (if gr then endpoint.FLAG_GRANT_REPLY else 0#u64)) ||| + (if c then endpoint.FLAG_CALL else 0#u64) = + flagsOf { senderBadge := 0, senderCanGrant := g, senderCanGrantReply := gr, senderIsCall := c } := by + unfold endpoint.FLAG_GRANT endpoint.FLAG_GRANT_REPLY endpoint.FLAG_CALL flagsOf; rfl + +theorem twf_ofA {t b : U64} (k : Nat) (hv : b.val = t.val + k) (hk : TWOffN k) : TWF t b := + ⟨by omega, by rw [hv, Nat.add_sub_cancel_left]; exact hk⟩ + +/-- Two writes to distinct aligned words. -/ +theorem agree_wr2A {s : S} {a b v w : U64} (ha : Aligned a) (hb : Aligned b) : + Agree (L.mem s) (L.mem (L.wr (L.wr s a v) b w)) (fun c => c.val ≠ a.val ∧ c.val ≠ b.val) := + ((agree_wr (L := L) (s := s) (v := v) ha).mono fun _ h => h.1).trans + ((agree_wr (L := L) (s := L.wr s a v) (v := w) hb).mono fun _ h => h.2) + +/-- The four writes of a blocking send. -/ +theorem block_send_writesA (hP : EPlaced T E R Bf) (s : S) {t : U64} (ht : t ∈ T) (e badge f : U64) : + let s4 := L.wr (L.wr (L.wr (L.wr s (stA t) sched.TS_BLOCKED_ON_SEND) (tBlockA t) e) (tBadgeA t) badge) + (tFlagsA t) f + Agree (L.mem s) (L.mem s4) (fun b => b.val ≠ t.val + 3328 ∧ b.val ≠ t.val + 3336 ∧ b.val ≠ t.val + 3344 ∧ + b.val ≠ t.val + 3352) ∧ + L.mem s4 (stA t) = sched.TS_BLOCKED_ON_SEND ∧ L.mem s4 (tBlockA t) = e ∧ L.mem s4 (tBadgeA t) = badge ∧ + L.mem s4 (tFlagsA t) = f ∧ WritesIn st s s4 (EWrite T E R Bf) := by + intro s4 + have hta := hP.tcb t ht + have a1 := (a_st hta).1; have a2 := (a_blk hta).1; have a3 := (a_badge hta).1; have a4 := (a_flags hta).1 + have v1 := val_st hta; have v2 := val_blk hta; have v3 := val_badge hta; have v4 := val_flags hta + have g1 := agree_wr2A (L := L) (s := s) (v := sched.TS_BLOCKED_ON_SEND) (w := e) a1 a2 + have g2 := agree_wr2A (L := L) (s := L.wr (L.wr s (stA t) sched.TS_BLOCKED_ON_SEND) (tBlockA t) e) + (v := badge) (w := f) a3 a4 + refine ⟨(g1.mono fun b hb => ?_).trans (g2.mono fun b hb => ?_), ?_, ?_, ?_, ?_, ?_⟩ + · rw [v1, v2]; omega + · rw [v3, v4]; omega + · show L.mem (L.wr (L.wr (L.wr (L.wr s _ _) _ _) _ _) _ _) _ = _ + rw [L.mem_wr_ne a4 a1 (by omega), L.mem_wr_ne a3 a1 (by omega), L.mem_wr_ne a2 a1 (by omega), + L.mem_wr_same a1] + · show L.mem (L.wr (L.wr (L.wr (L.wr s _ _) _ _) _ _) _ _) _ = _ + rw [L.mem_wr_ne a4 a2 (by omega), L.mem_wr_ne a3 a2 (by omega), L.mem_wr_same a2] + · show L.mem (L.wr (L.wr (L.wr (L.wr s _ _) _ _) _ _) _ _) _ = _ + rw [L.mem_wr_ne a4 a3 (by omega), L.mem_wr_same a3] + · exact L.mem_wr_same a4 + · refine (((wr_ewrite (E := E) (R := R) (Bf := Bf) ht (twf_ofA 3328 v1 (by unfold TWOffN; omega)) a1 s _).trans + (wr_ewrite (E := E) (R := R) (Bf := Bf) ht (twf_ofA 3336 v2 (by unfold TWOffN; omega)) a2 _ _)).trans + ((wr_ewrite (E := E) (R := R) (Bf := Bf) ht (twf_ofA 3344 v3 (by unfold TWOffN; omega)) a3 _ _).trans + (wr_ewrite (E := E) (R := R) (Bf := Bf) ht (twf_ofA 3352 v4 (by unfold TWOffN; omega)) a4 _ _))).mono + fun b hb => ?_ + rcases hb with (hb | hb) | (hb | hb) <;> exact hb + +/-- The payload of a blocking send. -/ +abbrev sndPlA (badge : Fl4SpecA64.Badge) (g gr call : Bool) : Fl4SpecA64.SenderPayload := + { senderBadge := badge, senderCanGrant := g, senderCanGrantReply := gr, senderIsCall := call } + +theorem flag_iteA (b : Bool) (v : U64) : (if b then ok v else ok 0#u64 : Result U64) = ok (if b then v else 0#u64) := by + cases b <;> rfl + +/-- The blocking send's four TCB words: related to the spec's TCB `BlockedOnSend e d'`, and `SR` kept by +`SchedOpsEA.block` (the sender, the current thread, blocks; `RelIfBound` by `SchedOpsEA.rd`). A separate +declaration (kernel cost). -/ +theorem block_send_relA (hO : SchedOpsEA st T E R Bf SR) (hP : EPlaced T E R Bf) (s : S) + (σ : Fl4SpecA64.State Unit) (hR : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) + {t : U64} (ht : t ∈ T) {xt : Fl4SpecA64.Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hsr : SndRunA σ (specPtr t) xt) (e badge f : U64) (d' : Fl4SpecA64.SenderPayload) + (hb : d'.senderBadge = badge.bv) (hfv : f = flagsOf d') : + EpStateRel T E R Bf (L.mem (L.wr (L.wr (L.wr (L.wr s (stA t) sched.TS_BLOCKED_ON_SEND) (tBlockA t) e) + (tBadgeA t) badge) (tFlagsA t) f)) + (updK σ (specPtr t) (.tcb { xt with tcbState := .blockedOnSend (specPtr e) d' })) ∧ + SR (L.wr (L.wr (L.wr (L.wr s (stA t) sched.TS_BLOCKED_ON_SEND) (tBlockA t) e) (tBadgeA t) badge) (tFlagsA t) f) + (updK σ (specPtr t) (.tcb { xt with tcbState := .blockedOnSend (specPtr e) d' })) := by + have hxt := hR.at_tcb ht hkt + have hta := hP.tcb t ht + obtain ⟨hrun, hNQ, hsane, hni⟩ := hsr + have hq := not_queued_codeA hR ht hkt (runnable_codeA hrun) + have hRB : RelIfBound σ (specPtr t) := hO.rd _ _ _ _ hSR hkt (Or.inl hrun) + obtain ⟨hA4, r1, r2, r3, r4, hW4⟩ := block_send_writesA (L := L) hP s ht e badge f + have v1 := val_st hta; have v2 := val_blk hta; have v3 := val_badge hta; have v4 := val_flags hta + refine ⟨?_, hO.block _ _ _ _ t hSR hNQ hRB hsane hni hW4 (EpStep.tcb hkt _ xt.tcbArch) + fun p x' y' h1 h2 hp => ?_⟩ + · refine hR.upd_tcb hP ht _ (hA4.mono fun b hb => ?_) (Or.inl hq) ?_ + · have n1 : b.val ≠ (stA t).val := fun h => hb.2 (TF.of_val (a_st hta).2 h) + have n2 : b.val ≠ (tBlockA t).val := fun h => hb.2 (TF.of_val (a_blk hta).2 h) + have n3 : b.val ≠ (tBadgeA t).val := fun h => hb.2 (TF.of_val (a_badge hta).2 h) + have n4 : b.val ≠ (tFlagsA t).val := fun h => hb.2 (TF.of_val (a_flags hta).2 h) + omega + refine hxt.restateA hta (hA4.mono fun b hb => ?_) rfl rfl rfl rfl ?_ ?_ ?_ ?_ ?_ + · have := hb.2; omega + · rw [r1]; unfold sched.TS_BLOCKED_ON_SEND; rfl + · intro e' h + simp only [Fl4SpecA64.epBlocked, Option.some.injEq] at h + subst h; rw [r2] + · intro e' d'' h + simp only [Fl4SpecA64.ThreadState.blockedOnSend.injEq] at h + obtain ⟨-, rfl⟩ := h + exact ⟨by rw [r3, hb], by rw [r4, hfv]⟩ + · intro e' r d'' h; cases h + · intro r h; cases h + · rw [updK_kheap, Function.update_of_ne hp, h1] at h2 + injection h2 with h2; injection h2 with h2; subst h2; exact ⟨rfl, id⟩ + +/-- FL4's blocking send after the endpoint's state word (a separate declaration): the four words +(`block_send_relA`), `schedule_tcb`, then `ep_append_relA`; against `tcb_ep_append` at the `stsAct` state. -/ +theorem send_block_keyA (hO : SchedOpsEA st T E R Bf SR) (hF : SchedFootEA st T E R Bf SR Wr) + (hP : EPlaced T E R Bf) (s : S) (a : A) + (σ : Fl4SpecA64.State Unit) (hR : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) + {t : U64} (ht : t ∈ T) {xt : Fl4SpecA64.Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hsr : SndRunA σ (specPtr t) xt) + {e : U64} (he : e ∈ E) {x : Fl4SpecA64.Endpoint} (hk : σ.kheap (specPtr e) = some (.endpoint x)) + (hc : CanBlockA false x) + (hsort : ∀ xs, QueueRel (L.mem s) T .Ep (qOf (L.mem s) e) xs → PrioSorted (fun y => L.mem s (prioA y)) xs) + (call : Bool) (badge : U64) (g gr : Bool) : + (do + let s1 ← obj.Tcb.set_state st t s sched.TS_BLOCKED_ON_SEND + let s2 ← obj.Tcb.set_block_obj st t s1 e + let s3 ← obj.Tcb.set_block_badge st t s2 badge + let i ← ok (if g = true then endpoint.FLAG_GRANT else 0#u64) + let i1 ← ok (if gr = true then endpoint.FLAG_GRANT_REPLY else 0#u64) + let i2 ← lift (i ||| i1) + let i3 ← ok (if call = true then endpoint.FLAG_CALL else 0#u64) + let f ← lift (i2 ||| i3) + let s4 ← obj.Tcb.set_block_flags st t s3 f + let s5 ← sched.schedule_tcb st s4 t + let s6 ← endpoint.ep_append st s5 e t obj.EP_SEND + ok (s6, a)) ⦃ r => r.2 = a ∧ + ∃ σ', Fl4SpecA64.tcbEpAppend (Z := Unit) (specPtr t) (specPtr e) false + (stsAct (updK σ (specPtr t) (.tcb { xt with tcbState := .blockedOnSend (specPtr e) (sndPlA badge.bv g gr call) })) + (specPtr t)) = ({((), σ')}, False) ∧ EpStateRel T E R Bf (L.mem r.1) σ' ∧ SR r.1 σ' ∧ + FpA st T E R Bf Wr σ s r.1 ⦄ := by + have hx := hR.at_ep he hk + have hta := hP.tcb t ht + have htq : specPtr t ∉ x.epQueue := not_queued_codeA hR ht hkt (runnable_codeA hsr.1) e he x hk + step with obj.Tcb.set_state.spec as ⟨s1, hs1⟩ + step with obj.Tcb.set_block_obj.spec as ⟨s2, hs2⟩ + step with obj.Tcb.set_block_badge.spec as ⟨s3, hs3⟩ + simp only [lift, bind_ok] + step with obj.Tcb.set_block_flags.spec as ⟨s4, hs4⟩ + subst hs1 hs2 hs3 hs4 + obtain ⟨hA4, r1, r2, -, -, hW4⟩ := block_send_writesA (E := E) (R := R) (Bf := Bf) (L := L) hP s ht e badge + (((if g = true then endpoint.FLAG_GRANT else 0#u64) ||| (if gr = true then endpoint.FLAG_GRANT_REPLY else 0#u64)) + ||| (if call = true then endpoint.FLAG_CALL else 0#u64)) + obtain ⟨hR1, hSR1⟩ := block_send_relA hO hP s σ hR hSR ht hkt hsr e badge + (((if g = true then endpoint.FLAG_GRANT else 0#u64) ||| (if gr = true then endpoint.FLAG_GRANT_REPLY else 0#u64)) + ||| (if call = true then endpoint.FLAG_CALL else 0#u64)) + (sndPlA badge.bv g gr call) rfl + (by rw [flags_eqA]; rfl) + refine WP.spec_bind (WP.spec_and (hO.scheduleTcb _ _ t hR1 hSR1 ht) (hF.scheduleTcb _ _ t hR1 hSR1 ht)) ?_ + rintro s5 ⟨⟨hf5, hS5⟩, hw5⟩ + have hR2 : EpStateRel T E R Bf (L.mem s5) (stsAct (updK σ (specPtr t) + (.tcb { xt with tcbState := .blockedOnSend (specPtr e) (sndPlA badge.bv g gr call) })) (specPtr t)) := + hR1.frame hP hf5 (KView.stsAct' _ _) + have hnt : specPtr e ≠ specPtr t := kheap_ne hk hkt (fun _ h1 h2 => by subst h1; cases h2) + have hk2 : (stsAct (updK σ (specPtr t) (.tcb { xt with tcbState := .blockedOnSend (specPtr e) (sndPlA badge.bv g gr call) })) + (specPtr t)).kheap (specPtr e) = some (.endpoint x) := by + rw [stsAct_kheap, updK_kheap, Function.update_of_ne hnt, hk] + have hkt2 : (stsAct (updK σ (specPtr t) (.tcb { xt with tcbState := .blockedOnSend (specPtr e) (sndPlA badge.bv g gr call) })) + (specPtr t)).kheap (specPtr t) = some (.tcb { xt with tcbState := .blockedOnSend (specPtr e) (sndPlA badge.bv g gr call) }) := by + rw [stsAct_kheap, updK_kheap, Function.update_self] + have hpr : ∀ y ∈ T, L.mem s5 (prioA y) = L.mem s (prioA y) := by + intro y hy + have hya := hP.tcb y hy + rw [hf5 _ (a_prio hya).1 (Or.inr (Or.inr (Or.inl ⟨y, hy, Or.inl (a_prio hya).2⟩)))] + refine hA4 _ (a_prio hya).1 ?_ + have := val_prio hya + by_cases eyt : y = t + · subst eyt; omega + · have := tcb_disj hP ht hy eyt; omega + have hsort5 := sorted_transferA hP.tcb hx (hR2.at_ep he hk2) hpr hsort + have htst : (L.mem s5 (stA t)).val = if false = true then 3 else 4 := by + rw [hf5 _ (a_st hta).1 (Or.inr (Or.inr (Or.inl ⟨t, ht, Or.inl (a_st hta).2⟩))), r1] + unfold sched.TS_BLOCKED_ON_SEND; rfl + have htb : L.mem s5 (tBlockA t) = e := by + rw [hf5 _ (a_blk hta).1 (Or.inr (Or.inr (Or.inl ⟨t, ht, Or.inl (a_blk hta).2⟩))), r2] + have happ := ep_append_relA hO hP s5 _ hR2 hS5 ht hkt2 he hk2 false hc htq htst htb hsort5 + simp only [Bool.false_eq_true, ↓reduceIte] at happ + refine WP.spec_bind happ ?_ + rintro s6 ⟨σ', hσ', hR', hS', hW6⟩ + rw [WP.spec_ok] + have hfp5 : FpA st T E R Bf Wr σ _ s5 := + FpA.trans hF (FpA.of_ew hW4) (ssz_updK_tcb σ (specPtr t) _) (FpA.of_wr hw5) + exact ⟨rfl, σ', hσ', hR', hS', + FpA.trans hF hfp5 ((ssz_updK_tcb σ (specPtr t) _).trans (ssz_stsAct _ _)) + (FpA.of_ew (σ := stsAct (updK σ (specPtr t) + (.tcb { xt with tcbState := .blockedOnSend (specPtr e) (sndPlA badge.bv g gr call) })) (specPtr t)) hW6)⟩ + +/-- **Blocking send** on an idle endpoint or one with waiting senders (x86 `send_ipc_block`): the four TCB words and +`SchedOpsEA.block` (the sender blocks), `schedule_tcb`, then `ep_append_relA` (`send_block_keyA`). -/ +theorem send_ipc_blockA (hO : SchedOpsEA st T E R Bf SR) (hF : SchedFootEA st T E R Bf SR Wr) + (hP : EPlaced T E R Bf) (s : S) (a : A) + (σ : Fl4SpecA64.State Unit) (hR : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) + {t : U64} (ht : t ∈ T) {xt : Fl4SpecA64.Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hsr : SndRunA σ (specPtr t) xt) + {e : U64} (he : e ∈ E) {x : Fl4SpecA64.Endpoint} (hk : σ.kheap (specPtr e) = some (.endpoint x)) + (hc : CanBlockA false x) + (hsort : ∀ xs, QueueRel (L.mem s) T .Ep (qOf (L.mem s) e) xs → PrioSorted (fun y => L.mem s (prioA y)) xs) + (call : Bool) (badge : U64) (g gr d : Bool) : + endpoint.send_ipc st ar s a true call badge g gr d t e ⦃ r => r.2 = a ∧ + ∃ σ', Fl4SpecA64.sendIpc (Z := Unit) true call badge.bv g gr d (specPtr t) (specPtr e) σ = + ({((), σ')}, False) ∧ EpStateRel T E R Bf (L.mem r.1) σ' ∧ SR r.1 σ' ∧ FpA st T E R Bf Wr σ s r.1 ⦄ := by + have hx := hR.at_ep he hk + have hspec : Fl4SpecA64.sendIpc (Z := Unit) true call badge.bv g gr d (specPtr t) (specPtr e) σ = + Fl4SpecA64.tcbEpAppend (specPtr t) (specPtr e) false (stsAct (updK σ (specPtr t) + (.tcb { xt with tcbState := .blockedOnSend (specPtr e) (sndPlA badge.bv g gr call) })) + (specPtr t)) := by + rw [sendIpc_blockA hk hc] + unfold Fl4SpecA64.sendIpcBlocked + rw [nd_bind_one (setThreadState_eq _ hkt)] + rw [hspec] + have key := send_block_keyA hO hF hP s a σ hR hSR ht hkt hsr he hk hc hsort call badge g gr + unfold endpoint.send_ipc + step with obj.Ep.state.spec as ⟨st0, hst0⟩ + subst hst0 + rw [epState_eq] + cases x with + | idleEP => + rw [hx.1, if_pos ep_state_vals.1.symm] + simp only [↓reduceIte, flag_iteA] + exact key + | sendEP q => + rw [hx.1, if_neg (by rw [ep_state_vals.1]; decide), if_pos ep_state_vals.2.1.symm] + simp only [↓reduceIte, flag_iteA] + exact key + | recvEP q => simp [CanBlockA] at hc + +/-- **Non-blocking send** on an idle endpoint or one with waiting senders: nothing happens (x86 +`send_ipc_noblock`). -/ +theorem send_ipc_noblockA (s : S) (a : A) (σ : Fl4SpecA64.State Unit) (hR : EpStateRel T E R Bf (L.mem s) σ) + {e : U64} (he : e ∈ E) {x : Fl4SpecA64.Endpoint} (hk : σ.kheap (specPtr e) = some (.endpoint x)) + (hc : CanBlockA false x) (call : Bool) (badge : U64) (g gr d : Bool) (t : U64) : + endpoint.send_ipc st ar s a false call badge g gr d t e ⦃ r => r = (s, a) ∧ + Fl4SpecA64.sendIpc (Z := Unit) false call badge.bv g gr d (specPtr t) (specPtr e) σ = ({((), σ)}, False) ⦄ := by + have hx := hR.at_ep he hk + have hspec := sendIpc_noblockA hk hc call badge.bv g gr d (specPtr t) + unfold endpoint.send_ipc + step with obj.Ep.state.spec as ⟨st0, hst0⟩ + subst hst0 + rw [epState_eq] + cases x with + | idleEP => + rw [hx.1, if_pos ep_state_vals.1.symm] + simp only [Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + exact ⟨trivial, hspec⟩ + | sendEP q => + rw [hx.1, if_neg (by rw [ep_state_vals.1]; decide), if_pos ep_state_vals.2.1.symm] + simp only [Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + exact ⟨trivial, hspec⟩ + | recvEP q => simp [CanBlockA] at hc + +end + +end + +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/EpSendBlkFpA.lean b/proofs/Fl4ProofsA64/P3/EpSendBlkFpA.lean new file mode 100644 index 000000000..88c0fb4e5 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/EpSendBlkFpA.lean @@ -0,0 +1,337 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2b-1): **the blocking `send_ipc` with its spec outcome and its narrow write set**, +the sibling of `EpSendBlkA.send_ipc_blockA` (whose footprint `FpA` covers the links and block word of every TCB). + +* `blkSendA`: the spec's state after a blocking send (sender `BlockedOnSend`, `set_thread_state_act`, the sender + inserted by priority into the endpoint's queue). +* `BlkSendFpA`: the words FL4 writes: the sender's four blocking words, the endpoint, the `Ep` links of the sender and + of the threads already blocked on the endpoint, scheduler words. +* **`send_ipc_block_genA`**: over any scheduler relation `SR` that the endpoint code keeps (`SchedOpsE0A`), with the + relation after the four words given by the caller. +* **`send_ipc_block_fpA`**: from `SchedSRA` (`IpcSchedRelA`). +* **`send_ipc_block_tmoA`**: from `SchedSRB … t` (the timeout fault send after the charge head: the sender's SC is + exhausted). A sender blocked with a timeout fault is outside `inClassR`, so the relation closes to `SchedSRA`. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.EpSendBlkA +import Fl4ProofsA64.P3.EpSendTmoBA +import Fl4ProofsA64.P3.IpcScPushTopA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +attribute [local irreducible] stsAct updK + +/-- The sender's TCB after a blocking send on `e`. -/ +abbrev blkTcbA (xt : Tcb) (e : ObjRef) (pl : SenderPayload) : Tcb := { xt with tcbState := .blockedOnSend e pl } + +/-- **The spec's state after a blocking send** (module docstring). -/ +def blkSendA (σ : State Unit) (t e : ObjRef) (x : Endpoint) (xt : Tcb) (pl : SenderPayload) : State Unit := + updK (stsAct (updK σ t (.tcb (blkTcbA xt e pl))) t) e + (.endpoint (.sendEP (specIns x.epQueue (prioOf σ) xt.tcbPriority t))) + +/-- The spec's append at the state after `set_thread_state_act`. A separate declaration (kernel cost). -/ +theorem blk_append_specA {σ σ1 : State Unit} {t e : ObjRef} {x : Endpoint} {xt : Tcb} {pl : SenderPayload} + (h1 : σ1 = stsAct (updK σ t (.tcb (blkTcbA xt e pl))) t) (hk : σ.kheap e = some (.endpoint x)) + (hkt : σ.kheap t = some (.tcb xt)) (htq : t ∉ x.epQueue) (hc : CanBlockA false x) + (hmq : ∀ y ∈ x.epQueue, ∃ z, σ.kheap y = some (.tcb z) ∧ z.tcbPriority = prioOf σ y) : + tcbEpAppend (Z := Unit) t e false σ1 = ({((), blkSendA σ t e x xt pl)}, False) := by + have hnt : e ≠ t := fun h => by rw [h, hkt] at hk; cases hk + have hk2 : σ1.kheap e = some (.endpoint x) := by rw [h1, stsAct_kheap, updK_kheap, Function.update_of_ne hnt, hk] + have hkt2 : σ1.kheap t = some (.tcb (blkTcbA xt e pl)) := by rw [h1, stsAct_kheap, updK_kheap, Function.update_self] + rw [tcbEpAppend_eq hk2 hkt2 (prioOf σ) (fun y hy => by + obtain ⟨z, hz, ez⟩ := hmq y hy + refine ⟨z, ?_, ez⟩ + rw [h1, stsAct_kheap, updK_kheap, Function.update_of_ne (show y ≠ t from fun h => htq (by rw [← h]; exact hy))] + exact hz), epIns_eqA hc, h1] + rfl + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] {T E R : Set U64} + {Bf : Set (U64 × Fl4SpecA64.Word)} {SR : S → State Unit → Prop} {Wr : State Unit → S → U64 → Prop} + +variable (st T Wr) in +/-- The write set of a blocking send of `t` on the endpoint `e` (module docstring). -/ +def BlkSendFpA (σ : State Unit) (s : S) (t e : U64) (b : U64) : Prop := + b = stA t ∨ b = tBlockA t ∨ b = tBadgeA t ∨ b = tFlagsA t ∨ EW e b ∨ + (∃ w, (w = t ∨ (w ∈ T ∧ L.mem s (tBlockA w) = e)) ∧ TL w b) ∨ Wr σ s b + +/-- **After the four words**: `schedule_tcb`, then `ep_append`, with the narrow write set. -/ +theorem blkSend_tailA (hO : SchedOpsE0A st T E R Bf SR) (hF : SchedFootE0A st T E R Bf SR Wr) + (hP : EPlaced T E R Bf) {m : U64 → U64} (s4 : S) (a : A) (σ1 σ' : State Unit) + (hR1 : EpStateRel T E R Bf (L.mem s4) σ1) (hSR1 : SR s4 σ1) {t e : U64} (ht : t ∈ T) (he : e ∈ E) + {x : Endpoint} {xt1 : Tcb} (hk : σ1.kheap (specPtr e) = some (.endpoint x)) + (hkt : σ1.kheap (specPtr t) = some (.tcb xt1)) (hc : CanBlockA false x) (htq : specPtr t ∉ x.epQueue) + (htst : (L.mem s4 (stA t)).val = 4) (htb : L.mem s4 (tBlockA t) = e) + (hpr : ∀ y ∈ T, L.mem s4 (prioA y) = m (prioA y)) (hx0 : EpObjRel m T e x) + (hsort : ∀ xs, QueueRel m T .Ep (qOf m e) xs → P1.PrioSorted (fun y => m (prioA y)) xs) + {xs : List U64} (hxs : x.epQueue = xs.map specPtr) + (happ : tcbEpAppend (Z := Unit) (specPtr t) (specPtr e) false (stsAct σ1 (specPtr t)) = ({((), σ')}, False)) : + (do + let s5 ← sched.schedule_tcb st s4 t + let s6 ← endpoint.ep_append st s5 e t obj.EP_SEND + ok (s6, a)) ⦃ r => r.2 = a ∧ EpStateRel T E R Bf (L.mem r.1) σ' ∧ SR r.1 σ' ∧ + WritesIn st s4 r.1 (fun b => Wr σ1 s4 b ∨ EW e b ∨ ∃ w ∈ t :: xs, TL w b) ⦄ := by + have hta := hP.tcb t ht + refine WP.spec_bind (WP.spec_and (hO.scheduleTcb _ _ t hR1 hSR1 ht) (hF.scheduleTcb _ _ t hR1 hSR1 ht)) ?_ + rintro s5 ⟨⟨hf5, hS5⟩, hw5⟩ + have hR2 : EpStateRel T E R Bf (L.mem s5) (stsAct σ1 (specPtr t)) := hR1.frame hP hf5 (KView.stsAct' _ _) + have hk2 : (stsAct σ1 (specPtr t)).kheap (specPtr e) = some (.endpoint x) := by rw [stsAct_kheap]; exact hk + have hkt2 : (stsAct σ1 (specPtr t)).kheap (specPtr t) = some (.tcb xt1) := by rw [stsAct_kheap]; exact hkt + have hpr5 : ∀ y ∈ T, L.mem s5 (prioA y) = m (prioA y) := by + intro y hy + have hya := hP.tcb y hy + rw [hf5 _ (a_prio hya).1 (Or.inr (Or.inr (Or.inl ⟨y, hy, Or.inl (a_prio hya).2⟩)))] + exact hpr y hy + have hsort5 := sorted_transferA hP.tcb hx0 (hR2.at_ep he hk2) hpr5 hsort + have htst5 : (L.mem s5 (stA t)).val = if false = true then 3 else 4 := by + rw [hf5 _ (a_st hta).1 (Or.inr (Or.inr (Or.inl ⟨t, ht, Or.inl (a_st hta).2⟩)))] + exact htst + have htb5 : L.mem s5 (tBlockA t) = e := by + rw [hf5 _ (a_blk hta).1 (Or.inr (Or.inr (Or.inl ⟨t, ht, Or.inl (a_blk hta).2⟩)))] + exact htb + obtain ⟨ys, hQ5, hys, -⟩ := (hR2.at_ep he hk2).queue + have eys : ys = xs := List.map_injective_iff.2 (fun a b h => specPtr_inj h) (hys.symm.trans hxs) + have htys : t ∉ ys := fun h => htq (by rw [hys]; exact List.mem_map_of_mem h) + have happ5 := ep_append_relA0 hO hP s5 _ hR2 hS5 ht hkt2 he hk2 false hc htq htst5 htb5 hsort5 + have hfull := ep_append_full hP s5 he t obj.EP_SEND ys hQ5 ht htys + simp only [Bool.false_eq_true, ↓reduceIte] at happ5 + refine WP.spec_bind (WP.spec_and happ5 hfull) ?_ + rintro s6 ⟨⟨σ2, hσ2, hR', hS', hW6⟩, -, -, hQA, -⟩ + have eσ : σ2 = σ' := (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst (hσ2.symm.trans happ)))).2 + rw [eσ] at hR' hS' + rw [WP.spec_ok] + refine ⟨rfl, hR', hS', WritesIn.trans (st := st) hw5 ⟨hW6.1, hW6.2.1, fun b hb hn => ?_⟩⟩ + rw [← eys] at hn + exact hQA b hb ⟨fun h => hn (Or.inl h), fun h => hn (Or.inr h)⟩ + +/-- **The blocking send** (module docstring). -/ +theorem send_ipc_block_genA (hO : SchedOpsE0A st T E R Bf SR) (hF : SchedFootE0A st T E R Bf SR Wr) + (hP : EPlaced T E R Bf) (s : S) (a : A) (σ : State Unit) (hR : EpStateRel T E R Bf (L.mem s) σ) + {t : U64} (ht : t ∈ T) {xt : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) + {e : U64} (he : e ∈ E) {x : Endpoint} (hk : σ.kheap (specPtr e) = some (.endpoint x)) + (hc : CanBlockA false x) + (hsort : ∀ xs, QueueRel (L.mem s) T .Ep (qOf (L.mem s) e) xs → P1.PrioSorted (fun y => L.mem s (prioA y)) xs) + (call : Bool) (badge : U64) (g gr d : Bool) + (hSR4 : ∀ s4 : S, WritesIn st s s4 (EWrite T E R Bf) → + EpStep σ (updK σ (specPtr t) (.tcb (blkTcbA xt (specPtr e) (sndPlA badge.bv g gr call)))) → + SR s4 (updK σ (specPtr t) (.tcb (blkTcbA xt (specPtr e) (sndPlA badge.bv g gr call))))) : + endpoint.send_ipc st ar s a true call badge g gr d t e ⦃ r => r.2 = a ∧ + sendIpc (Z := Unit) true call badge.bv g gr d (specPtr t) (specPtr e) σ = + ({((), blkSendA σ (specPtr t) (specPtr e) x xt (sndPlA badge.bv g gr call))}, False) ∧ + EpStateRel T E R Bf (L.mem r.1) (blkSendA σ (specPtr t) (specPtr e) x xt (sndPlA badge.bv g gr call)) ∧ + SR r.1 (blkSendA σ (specPtr t) (specPtr e) x xt (sndPlA badge.bv g gr call)) ∧ + WritesIn st s r.1 (BlkSendFpA st T Wr σ s t e) ∧ + SSzA σ (blkSendA σ (specPtr t) (specPtr e) x xt (sndPlA badge.bv g gr call)) ⦄ := by + have hx := hR.at_ep he hk + have hxt := hR.at_tcb ht hkt + have hta := hP.tcb t ht + have v1 := val_st hta; have v2 := val_blk hta; have v3 := val_badge hta; have v4 := val_flags hta + have htq : specPtr t ∉ x.epQueue := hq e he x hk + obtain ⟨xs, hQ, hxs, hB⟩ := hx.queue + have hmq : ∀ y ∈ x.epQueue, ∃ z, σ.kheap y = some (.tcb z) ∧ z.tcbPriority = prioOf σ y := by + intro y hy + rw [hxs] at hy + obtain ⟨y', hy'x, rfl⟩ := List.mem_map.1 hy + obtain ⟨z, hz, -⟩ := hR.tcb y' (hQ.mem y' hy'x) + exact ⟨z, hz, by simp [prioOf, hz]⟩ + have happ : tcbEpAppend (Z := Unit) (specPtr t) (specPtr e) false + (stsAct (updK σ (specPtr t) (.tcb (blkTcbA xt (specPtr e) (sndPlA badge.bv g gr call)))) (specPtr t)) = + ({((), blkSendA σ (specPtr t) (specPtr e) x xt (sndPlA badge.bv g gr call))}, False) := + blk_append_specA rfl hk hkt htq hc hmq + have hspec : sendIpc (Z := Unit) true call badge.bv g gr d (specPtr t) (specPtr e) σ = + ({((), blkSendA σ (specPtr t) (specPtr e) x xt (sndPlA badge.bv g gr call))}, False) := by + rw [sendIpc_blockA hk hc] + unfold Fl4SpecA64.sendIpcBlocked + rw [nd_bind_one (setThreadState_eq _ hkt)] + exact happ + have hz : SSzA σ (blkSendA σ (specPtr t) (specPtr e) x xt (sndPlA badge.bv g gr call)) := by + refine (ssz_updK_tcb σ (specPtr t) (blkTcbA xt (specPtr e) (sndPlA badge.bv g gr call))).trans + ((ssz_stsAct _ (specPtr t)).trans fun p y n h => ?_) + unfold blkSendA at h + rw [updK_kheap] at h + by_cases ep : p = specPtr e + · subst ep; rw [Function.update_self] at h; cases h + · rw [Function.update_of_ne ep] at h; exact ⟨y, h⟩ + have key : (do + let s1 ← obj.Tcb.set_state st t s sched.TS_BLOCKED_ON_SEND + let s2 ← obj.Tcb.set_block_obj st t s1 e + let s3 ← obj.Tcb.set_block_badge st t s2 badge + let i ← ok (if g = true then endpoint.FLAG_GRANT else 0#u64) + let i1 ← ok (if gr = true then endpoint.FLAG_GRANT_REPLY else 0#u64) + let i2 ← lift (i ||| i1) + let i3 ← ok (if call = true then endpoint.FLAG_CALL else 0#u64) + let f ← lift (i2 ||| i3) + let s4 ← obj.Tcb.set_block_flags st t s3 f + let s5 ← sched.schedule_tcb st s4 t + let s6 ← endpoint.ep_append st s5 e t obj.EP_SEND + ok (s6, a)) ⦃ r => r.2 = a ∧ + EpStateRel T E R Bf (L.mem r.1) (blkSendA σ (specPtr t) (specPtr e) x xt (sndPlA badge.bv g gr call)) ∧ + SR r.1 (blkSendA σ (specPtr t) (specPtr e) x xt (sndPlA badge.bv g gr call)) ∧ + WritesIn st s r.1 (BlkSendFpA st T Wr σ s t e) ⦄ := by + step with obj.Tcb.set_state.spec as ⟨s1, hs1⟩ + step with obj.Tcb.set_block_obj.spec as ⟨s2, hs2⟩ + step with obj.Tcb.set_block_badge.spec as ⟨s3, hs3⟩ + simp only [lift, bind_ok] + step with obj.Tcb.set_block_flags.spec as ⟨s4, hs4⟩ + subst hs1 hs2 hs3 + have W := block_send_writesA (E := E) (R := R) (Bf := Bf) (L := L) hP s ht e badge + (((if g = true then endpoint.FLAG_GRANT else 0#u64) ||| (if gr = true then endpoint.FLAG_GRANT_REPLY else 0#u64)) + ||| (if call = true then endpoint.FLAG_CALL else 0#u64)) + simp only at W + rw [← hs4] at W + clear hs4 + obtain ⟨hA4, r1, r2, r3, r4, hW4⟩ := W + have hS1 : EpStep σ (updK σ (specPtr t) (.tcb (blkTcbA xt (specPtr e) (sndPlA badge.bv g gr call)))) := + EpStep.tcb hkt _ xt.tcbArch + have hR1 : EpStateRel T E R Bf (L.mem s4) + (updK σ (specPtr t) (.tcb (blkTcbA xt (specPtr e) (sndPlA badge.bv g gr call)))) := by + refine hR.upd_tcb hP ht _ (hA4.mono fun b hb => ?_) (Or.inl hq) ?_ + · have n1 : b.val ≠ (stA t).val := fun h => hb.2 (TF.of_val (a_st hta).2 h) + have n2 : b.val ≠ (tBlockA t).val := fun h => hb.2 (TF.of_val (a_blk hta).2 h) + have n3 : b.val ≠ (tBadgeA t).val := fun h => hb.2 (TF.of_val (a_badge hta).2 h) + have n4 : b.val ≠ (tFlagsA t).val := fun h => hb.2 (TF.of_val (a_flags hta).2 h) + omega + refine hxt.restateA hta (hA4.mono fun b hb => ?_) rfl rfl rfl rfl ?_ ?_ ?_ ?_ ?_ + · have := hb.2; omega + · rw [r1]; unfold sched.TS_BLOCKED_ON_SEND; rfl + · intro e' h + simp only [Fl4SpecA64.epBlocked, Option.some.injEq] at h + subst h; rw [r2] + · intro e' d'' h + simp only [Fl4SpecA64.ThreadState.blockedOnSend.injEq] at h + obtain ⟨-, rfl⟩ := h + exact ⟨by rw [r3], by rw [r4, flags_eqA]; rfl⟩ + · intro e' r d'' h; cases h + · intro r h; cases h + have hSR1 := hSR4 s4 hW4 hS1 + have hnt : specPtr e ≠ specPtr t := kheap_ne hk hkt (fun _ h1 h2 => by subst h1; cases h2) + have hk1 : (updK σ (specPtr t) (.tcb (blkTcbA xt (specPtr e) (sndPlA badge.bv g gr call)))).kheap (specPtr e) = + some (.endpoint x) := by rw [updK_kheap, Function.update_of_ne hnt, hk] + have hkt1 : (updK σ (specPtr t) (.tcb (blkTcbA xt (specPtr e) (sndPlA badge.bv g gr call)))).kheap (specPtr t) = + some (.tcb (blkTcbA xt (specPtr e) (sndPlA badge.bv g gr call))) := by rw [updK_kheap, Function.update_self] + have htst : (L.mem s4 (stA t)).val = 4 := by rw [r1]; unfold sched.TS_BLOCKED_ON_SEND; rfl + have hpr : ∀ y ∈ T, L.mem s4 (prioA y) = L.mem s (prioA y) := by + intro y hy + have hya := hP.tcb y hy + refine hA4 _ (a_prio hya).1 ?_ + have := val_prio hya + by_cases eyt : y = t + · subst eyt; omega + · have := tcb_disj hP ht hy eyt; omega + have w04 : WritesIn st s s4 (fun b => b = stA t ∨ b = tBlockA t ∨ b = tBadgeA t ∨ b = tFlagsA t) := by + refine ⟨hW4.1, hW4.2.1, fun b hb hn => hA4 b hb ?_⟩ + refine ⟨fun h => hn (Or.inl ?_), fun h => hn (Or.inr (Or.inl ?_)), fun h => hn (Or.inr (Or.inr (Or.inl ?_))), + fun h => hn (Or.inr (Or.inr (Or.inr ?_)))⟩ <;> apply UScalar.eq_of_val_eq <;> omega + refine WP.spec_mono (blkSend_tailA hO hF hP (m := L.mem s) s4 a _ _ hR1 hSR1 ht he hk1 hkt1 hc htq htst r2 hpr + hx hsort hxs happ) ?_ + rintro r ⟨h1, h2, h3, w46⟩ + refine ⟨h1, h2, h3, (w04.trans w46).mono fun b h => ?_⟩ + rcases h with (h | h | h | h) | (h | h | ⟨w, hw, hl⟩) + · exact Or.inl h + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr (Or.inl h)) + · exact Or.inr (Or.inr (Or.inr (Or.inl h))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + (hF.back (ssz_updK_tcb σ _ _) hW4.1 h)))))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inl h)))) + · refine Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨w, ?_, hl⟩))))) + rcases List.mem_cons.1 hw with rfl | hw + · exact Or.inl rfl + · exact Or.inr ⟨hQ.mem w hw, (hB w hw).1⟩ + refine WP.spec_mono (?_ : endpoint.send_ipc st ar s a true call badge g gr d t e ⦃ r => r.2 = a ∧ + EpStateRel T E R Bf (L.mem r.1) (blkSendA σ (specPtr t) (specPtr e) x xt (sndPlA badge.bv g gr call)) ∧ + SR r.1 (blkSendA σ (specPtr t) (specPtr e) x xt (sndPlA badge.bv g gr call)) ∧ + WritesIn st s r.1 (BlkSendFpA st T Wr σ s t e) ⦄) fun r h => ⟨h.1, hspec, h.2.1, h.2.2.1, h.2.2.2, hz⟩ + unfold endpoint.send_ipc + step with obj.Ep.state.spec as ⟨st0, hst0⟩ + subst hst0 + rw [epState_eq] + cases x with + | idleEP => + rw [hx.1, if_pos ep_state_vals.1.symm] + simp only [↓reduceIte, flag_iteA] + exact key + | sendEP q => + rw [hx.1, if_neg (by rw [ep_state_vals.1]; decide), if_pos ep_state_vals.2.1.symm] + simp only [↓reduceIte, flag_iteA] + exact key + | recvEP q => simp [CanBlockA] at hc + +/-- **The blocking send from `SchedSRA`** (module docstring). -/ +theorem send_ipc_block_fpA (hO : SchedOpsEA st T E R Bf SR) (hF : SchedFootEA st T E R Bf SR Wr) + (hP : EPlaced T E R Bf) (s : S) (a : A) (σ : State Unit) (hR : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) + {t : U64} (ht : t ∈ T) {xt : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hsr : SndRunA σ (specPtr t) xt) + {e : U64} (he : e ∈ E) {x : Endpoint} (hk : σ.kheap (specPtr e) = some (.endpoint x)) + (hc : CanBlockA false x) + (hsort : ∀ xs, QueueRel (L.mem s) T .Ep (qOf (L.mem s) e) xs → P1.PrioSorted (fun y => L.mem s (prioA y)) xs) + (call : Bool) (badge : U64) (g gr d : Bool) : + endpoint.send_ipc st ar s a true call badge g gr d t e ⦃ r => r.2 = a ∧ + sendIpc (Z := Unit) true call badge.bv g gr d (specPtr t) (specPtr e) σ = + ({((), blkSendA σ (specPtr t) (specPtr e) x xt (sndPlA badge.bv g gr call))}, False) ∧ + EpStateRel T E R Bf (L.mem r.1) (blkSendA σ (specPtr t) (specPtr e) x xt (sndPlA badge.bv g gr call)) ∧ + SR r.1 (blkSendA σ (specPtr t) (specPtr e) x xt (sndPlA badge.bv g gr call)) ∧ + WritesIn st s r.1 (BlkSendFpA st T Wr σ s t e) ∧ + SSzA σ (blkSendA σ (specPtr t) (specPtr e) x xt (sndPlA badge.bv g gr call)) ⦄ := by + obtain ⟨hrun, hNQ, hsane, hni⟩ := hsr + have hq := not_queued_codeA hR ht hkt (runnable_codeA hrun) + have hRB : RelIfBound σ (specPtr t) := hO.rd _ _ _ _ hSR hkt (Or.inl hrun) + exact send_ipc_block_genA hO.to0 hF.to0 hP s a σ hR ht hkt hq he hk hc hsort call badge g gr d + fun s4 hW4 hS1 => hO.block _ _ _ _ t hSR hNQ hRB hsane hni hW4 hS1 fun p x' y' h1 h2 hp => by + rw [updK_kheap, Function.update_of_ne hp, h1] at h2 + injection h2 with h2; injection h2 with h2; subst h2; exact ⟨rfl, id⟩ + +end + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] {Z : XSetsA} {m : McState} + {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + +/-- **The blocking send of a timeout fault after the charge head** (module docstring): the sender `t` is the excepted +thread of `SchedSRB`; its fault is a timeout. -/ +theorem send_ipc_block_tmoA (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) (s : S) (a : A) + (σ : State Unit) (hR : EpStateRel Z.T E R Bf (L.mem s) σ) {t : U64} (H : SchedSRB st Z m κ s σ (specPtr t)) + (ht : t ∈ Z.T) {xt : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hsr : SndRunA σ (specPtr t) xt) {b0 : Badge} (hft : xt.tcbFault = some (.timeout b0)) + {e : U64} (he : e ∈ E) {x : Endpoint} (hk : σ.kheap (specPtr e) = some (.endpoint x)) + (hc : CanBlockA false x) + (hsort : ∀ xs, QueueRel (L.mem s) Z.T .Ep (qOf (L.mem s) e) xs → P1.PrioSorted (fun y => L.mem s (prioA y)) xs) + (call : Bool) (badge : U64) (g gr d : Bool) : + endpoint.send_ipc st ar s a true call badge g gr d t e ⦃ r => r.2 = a ∧ + sendIpc (Z := Unit) true call badge.bv g gr d (specPtr t) (specPtr e) σ = + ({((), blkSendA σ (specPtr t) (specPtr e) x xt (sndPlA badge.bv g gr call))}, False) ∧ + EpStateRel Z.T E R Bf (L.mem r.1) (blkSendA σ (specPtr t) (specPtr e) x xt (sndPlA badge.bv g gr call)) ∧ + IpcSchedRelA st Z m κ r.1 (blkSendA σ (specPtr t) (specPtr e) x xt (sndPlA badge.bv g gr call)) ∧ + WritesIn st s r.1 (BlkSendFpA st Z.T (fun σ s => MWrA (st := st) Z m σ s) σ s t e) ∧ + SSzA σ (blkSendA σ (specPtr t) (specPtr e) x xt (sndPlA badge.bv g gr call)) ⦄ := by + obtain ⟨hrun, hNQ, -, -⟩ := hsr + have hq := not_queued_codeA hR ht hkt (runnable_codeA hrun) + have O : SchedOpsEA st Z.T E R Bf (IpcSchedRelA st Z m κ) := schedOpsEAOfPlacementA P + have F : SchedFootEA st Z.T E R Bf (IpcSchedRelA st Z m κ) (fun σ s => MWrA (st := st) Z m σ s) := + schedFootEA_of_sched + refine send_ipc_block_genA O.to0 F.to0 PE s a σ hR ht hkt hq he hk hc hsort call badge g gr d + fun s4 hW4 hS1 => ?_ + have hy : (updK σ (specPtr t) (.tcb (blkTcbA xt (specPtr e) (sndPlA badge.bv g gr call)))).kheap (specPtr t) = + some (.tcb (blkTcbA xt (specPtr e) (sndPlA badge.bv g gr call))) := by + rw [updK_kheap, Function.update_self] + refine ⟨Z.Q, Z.R, SchedSRB.blockExA H hW4 (fun _ h => endpWriteOffRead H.toSchedSRN P h) (endpSchedStep hS1) + hNQ (fun u x' y' h1 h2 hu => ?_) hy ?_⟩ + · rw [updK_kheap, Function.update_of_ne hu, h1] at h2 + injection h2 with h2; injection h2 with h2; subst h2; exact ⟨rfl, id⟩ + · rintro (h | ⟨_, h⟩ | ⟨_, _, -, hn⟩ | ⟨_, _, _, h⟩) + · simp [runnable] at h + · cases h + · exact hn b0 hft + · cases h + +end + +end + +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/EpSendFpA.lean b/proofs/Fl4ProofsA64/P3/EpSendFpA.lean new file mode 100644 index 000000000..bcc8c06f8 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/EpSendFpA.lean @@ -0,0 +1,355 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2b-2b): **the narrow write set of `send_ipc` to a waiting receiver**, the sibling of +`EpSendRecvA.send_ipc_recvA`'s footprint `FpA` (whose `EWrite` covers the links and block word of every TCB). + +**`send_ipc_recv_fpA`**: under `send_ipc_recvA`'s hypotheses, FL4 writes only `SendFpA`: the dequeue's and the +transfer's words (`RecvFpA`: the endpoint, the `Ep` links of the threads blocked on it, the receiver's registers, +the IPC buffers), reply objects' thread words, the state words of the sender and the receiver, and scheduler words. +The `*_fpA` helpers are the narrow write sets of `EpSendRecvA`'s steps, also used by the donating arm +(`IpcScSendPushA`). + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.EpSendRecvA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched + +noncomputable section + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] {T E R : Set U64} + {Bf : Set (U64 × Fl4SpecA64.Word)} {SR : S → Fl4SpecA64.State Unit → Prop} + {Wr : Fl4SpecA64.State Unit → S → U64 → Prop} + +variable (st T R Bf Wr) in +/-- The write set of `send_ipc` from `t` to the waiting receiver `d` of the endpoint `e` (module docstring); the +sender's state word only if it may block (`W`: a call or a faulted sender). -/ +def SendFpA (σ : Fl4SpecA64.State Unit) (s : S) (t e d : U64) (W : Prop) (b : U64) : Prop := + RecvFpA st T Bf s e d b ∨ (∃ r ∈ R, b = rTcbA r) ∨ (W ∧ b = stA t) ∨ b = stA d ∨ Wr σ s b + +/-- A later segment (its scheduler words taken at its own entry state) stays inside an earlier write set. -/ +theorem WritesIn.schedA (F : SchedFootEA st T E R Bf SR Wr) {σ σ1 : Fl4SpecA64.State Unit} {s s1 s2 : S} + {Q B : U64 → Prop} (h1 : WritesIn st s s1 Q) (hz : SSzA σ σ1) + (h2 : WritesIn st s1 s2 (fun b => B b ∨ Wr σ1 s1 b)) (hB : ∀ b, B b → Q b) (hW : ∀ b, Wr σ s b → Q b) : + WritesIn st s s2 Q := + (WritesIn.trans (st := st) h1 h2).mono fun b h => + h.elim id fun h => h.elim (hB b) fun h => hW b (F.back hz h1.1 h) + +/-- `set_thread_state_fromF`'s narrow write set: the state word and scheduler words. -/ +theorem set_thread_state_fpA (hO : SchedOpsE0A st T E R Bf SR) (hF : SchedFootE0A st T E R Bf SR Wr) + (hP : EPlaced T E R Bf) {s0 : S} + {σ0 : Fl4SpecA64.State Unit} (hSR0 : SR s0 σ0) (s : S) (σ : Fl4SpecA64.State Unit) + (hR : EpStateRel T E R Bf (L.mem s) σ) (hW0 : WritesIn st s0 s (EWrite T E R Bf)) (hS0 : EpStep σ0 σ) + {t : U64} (ht : t ∈ T) {x : Fl4SpecA64.Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) + (ts : Fl4SpecA64.ThreadState) (hts : tsCode ts < 3) (code : U64) (hcode : code.val = tsCode ts) + (hNB : NoBlockE σ0 (updK σ (specPtr t) (.tcb { x with tcbState := ts }))) : + sched.set_thread_state st s t code ⦃ s' => WritesIn st s s' (fun b => b = stA t ∨ Wr σ s b) ⦄ := by + unfold sched.set_thread_state + step with obj.Tcb.set_state.spec as ⟨s1, hs1⟩ + subst hs1 + obtain ⟨hR1, hW1, hS1⟩ := write_state_rel hP s σ hR ht hk hq ts hts code hcode + have hSR1 := hO.stable _ _ _ _ hSR0 (WritesIn.ew hW0 hW1) (hS0.trans hS1) hNB + have hw1 : WritesIn st s (L.wr s (stA t) code) (fun b => b = stA t) := writesIn_wr (a_st (hP.tcb t ht)).1 + refine WP.spec_mono (hF.scheduleTcb _ _ t hR1 hSR1 ht) fun s2 hw => ?_ + exact (WritesIn.trans (st := st) hw1 hw).mono fun b h => h.imp id (hF.back (ssz_updK_tcb σ _ _) hw1.1) + +/-- `set_thread_state_blockA`'s narrow write set. -/ +theorem set_thread_state_block_fpA (hO : SchedOpsEA st T E R Bf SR) (hF : SchedFootEA st T E R Bf SR Wr) + (hP : EPlaced T E R Bf) (s : S) + (σ : Fl4SpecA64.State Unit) (hR : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) {t : U64} (ht : t ∈ T) + {x : Fl4SpecA64.Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) + (hNQ : NotQueued σ (specPtr t)) (hRB : RelIfBound σ (specPtr t)) + (hsane : ∀ u, σ.schedulerAction = .switchThread u → u ≠ specPtr t) (hni : specPtr t ≠ σ.idleThread) + (ts : Fl4SpecA64.ThreadState) (hts : tsCode ts < 3) (code : U64) (hcode : code.val = tsCode ts) : + sched.set_thread_state st s t code ⦃ s' => WritesIn st s s' (fun b => b = stA t ∨ Wr σ s b) ⦄ := by + unfold sched.set_thread_state + step with obj.Tcb.set_state.spec as ⟨s1, hs1⟩ + subst hs1 + obtain ⟨hR1, hW1, hS1⟩ := write_state_rel hP s σ hR ht hk hq ts hts code hcode + have hSR1 := hO.block _ _ _ _ t hSR hNQ hRB hsane hni hW1 hS1 fun p x' y' h1 h2 hp => by + rw [updK_kheap, Function.update_of_ne hp, h1] at h2 + injection h2 with h2; injection h2 with h2; subst h2; exact ⟨rfl, id⟩ + have hw1 : WritesIn st s (L.wr s (stA t) code) (fun b => b = stA t) := writesIn_wr (a_st (hP.tcb t ht)).1 + refine WP.spec_mono (hF.scheduleTcb _ _ t hR1 hSR1 ht) fun s2 hw => ?_ + exact (WritesIn.trans (st := st) hw1 hw).mono fun b h => h.imp id (hF.back (ssz_updK_tcb σ _ _) hw1.1) + +/-- `send_recv_replyA`'s narrow write set. -/ +theorem send_recv_reply_fpA (hO : SchedOpsE0A st T E R Bf SR) (hF : SchedFootE0A st T E R Bf SR Wr) + (hP : EPlaced T E R Bf) {s1 s2 : S} + {σ1 σ2 : Fl4SpecA64.State Unit} (hS1 : SR s1 σ1) (hR2 : EpStateRel T E R Bf (L.mem s2) σ2) + (hW2 : WritesIn st s1 s2 (EWrite T E R Bf)) (hE2 : EpStep σ1 σ2) {t d : U64} (hdT : d ∈ T) + (hfr2 : ∀ p, p ≠ specPtr d → σ2.kheap p = σ1.kheap p) (htd' : specPtr t ≠ specPtr d) + {xd x2 : Fl4SpecA64.Tcb} (hkd1 : σ1.kheap (specPtr d) = some (.tcb xd)) + (hkd2 : σ2.kheap (specPtr d) = some (.tcb x2)) {e : Fl4SpecA64.ObjRef} {rep : Option Fl4SpecA64.ObjRef} + {dd : Fl4SpecA64.ReceiverPayload} + (hst1 : xd.tcbState = .blockedOnReceive e rep dd) (hst2 : x2.tcbState = .blockedOnReceive e rep dd) + (hrr : ∀ r, rep = some r → ∃ xr, σ2.kheap r = some (.reply xr) ∧ xr.replyTcb = some (specPtr d)) + (hq2 : ∀ e ∈ E, ∀ z, σ2.kheap (specPtr e) = some (.endpoint z) → specPtr d ∉ z.epQueue) + (i : U64) (hi : i = L.mem s2 (tReplyA d)) : + (if (i != 0#u64) = true then ipc.reply_unlink st s2 i d else ok s2) ⦃ s3 => + WritesIn st s2 s3 (fun b => (∃ r ∈ R, b = rTcbA r) ∨ b = stA d ∨ Wr σ2 s2 b) ⦄ := by + subst hi + have hxd2 := hR2.at_tcb hdT hkd2 + obtain ⟨hrw, hrR⟩ := hxd2.recv _ _ _ hst2 + have hO2 : OnlyT σ1 σ2 (specPtr d) := fun q hq x' y' h1 h2 => by + rw [hfr2 q hq, h1] at h2; injection h2 with h2; injection h2 with h2; exact h2.symm + have hblk : Fl4SpecA64.runnable xd.tcbState = false ∧ xd.tcbState ≠ .idleThreadState := by + rw [hst1]; exact ⟨rfl, fun h => by cases h⟩ + cases rep with + | none => + have h0 : L.mem s2 (tReplyA d) = 0#u64 := optRef_none hrw + simp only [h0, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + exact WritesIn.refl _ _ + | some r => + obtain ⟨hw0, hwr⟩ := optRef_some hrw + have hwR := hrR (by simp) + have hne0 : (L.mem s2 (tReplyA d) != 0#u64) = true := by simpa using hw0 + simp only [hne0, ↓reduceIte] + subst hwr + obtain ⟨xr, hkr, -⟩ := hrr _ rfl + generalize L.mem s2 (tReplyA d) = i at hwR hkr ⊢ + have hit : specPtr i ≠ specPtr d := by intro h; rw [h, hkd2] at hkr; cases hkr + have hal := (a_rtcb hP hwR).1 + have hag := agree_wr (L := L) (s := s2) (v := 0#u64) hal + have hR3 : EpStateRel T E R Bf (L.mem (L.wr s2 (rTcbA i) 0#u64)) + (updK σ2 (specPtr i) (.reply { xr with replyTcb := none })) := + hR2.upd_reply hP hwR _ (hag.mono fun b hb => by rw [Endp.rTcbA_val]; exact hb.2) (by rw [rd_wr_eq hal, optRef_zero]) + have hW3 : WritesIn st s2 (L.wr s2 (rTcbA i) 0#u64) (EWrite T E R Bf) := + (writesIn_wr hal).mono fun b hb => by subst hb; exact Or.inr (Or.inl ⟨i, hwR, Endp.rTcbA_val i⟩) + have hk3 : (updK σ2 (specPtr i) (.reply { xr with replyTcb := none })).kheap (specPtr d) = some (.tcb x2) := by + rw [updK_kheap, Function.update_of_ne hit.symm, hkd2] + have hq3 := notq_updR (E := E) hq2 i { xr with replyTcb := none } + unfold ipc.reply_unlink + step with obj.Reply.set_tcb.spec as ⟨s3, hs3⟩ + subst hs3 + have hw1 : WritesIn st s2 (L.wr s2 (rTcbA i) 0#u64) (fun b => b = rTcbA i) := writesIn_wr hal + refine WP.spec_mono (set_thread_state_fpA hO hF hP hS1 _ _ hR3 (WritesIn.ew hW2 hW3) + (hE2.trans (EpStep.reply hkr none)) hdT hk3 hq3 .inactive (by decide) sched.TS_INACTIVE + (by unfold sched.TS_INACTIVE; rfl) + (NoBlockE.only hkd1 (by rw [updK_kheap, Function.update_self]) ((hO2.updO _ fun _ h => by cases h).updP _) + (Or.inr (Or.inr ⟨rfl, hblk⟩)))) fun s4 hw => ?_ + exact (WritesIn.trans (st := st) hw1 hw).mono fun b h => h.elim (fun h => Or.inl ⟨i, hwR, h⟩) + fun h => Or.inr (h.imp id (hF.back (ssz_updK_reply σ2 _ _) hw1.1)) + +/-- `send_tail_fromA`'s narrow write set: scheduler words. -/ +theorem send_tail_fpA (hO : SchedOpsEA st T E R Bf SR) (hF : SchedFootEA st T E R Bf SR Wr) + (a : A) {s s3 s5 : S} {σ σ3 : Fl4SpecA64.State Unit} (hSR : SR s σ) {d : U64} (hdT : d ∈ T) + (hon : L.mem s (affA d) = L.cpu s) {x3 : Fl4SpecA64.Tcb} (hS3 : SR s3 σ3) + (hkd3 : σ3.kheap (specPtr d) = some (.tcb x3)) (hnr : Fl4SpecA64.runnable x3.tcbState = false) + (hq3 : ∀ e ∈ E, ∀ z, σ3.kheap (specPtr e) = some (.endpoint z) → specPtr d ∉ z.epQueue) + (hR5 : EpStateRel T E R Bf (L.mem s5) + (stsAct (updK σ3 (specPtr d) (.tcb { x3 with tcbState := .running })) (specPtr d))) + (hS5 : SR s5 (stsAct (updK σ3 (specPtr d) (.tcb { x3 with tcbState := .running })) (specPtr d))) : + (do + let s6 ← endpoint.unblock_sporadic st s5 d + let s7 ← sched.possible_switch_to st s6 d + ok (s7, a)) ⦃ r => WritesIn st s5 r.1 + (Wr (stsAct (updK σ3 (specPtr d) (.tcb { x3 with tcbState := .running })) (specPtr d)) s5) ⦄ := by + obtain ⟨hk5, hq5, -, -⟩ := sts_facts hkd3 hq3 .running + have hprog : (do + let s6 ← endpoint.unblock_sporadic st s5 d + let s7 ← sched.possible_switch_to st s6 d + ok (s7, a)) = (rustRestartTail st s5 d >>= fun s7 => ok (s7, a)) := by + first + | (simp only [rustRestartTail, bind_assoc]; done) + | (unfold rustRestartTail; simp only [Std.bind_tc_eq, Std.bind_assoc]) + rw [hprog] + have hcore := (hO.coreKeep _ _ _ _ hSR hS5).onCore hdT hon + have hnq : NotQueued (stsAct (updK σ3 (specPtr d) (.tcb { x3 with tcbState := .running })) (specPtr d)) + (specPtr d) := (hO.notQueued _ _ d x3 hS3 hkd3 hnr).sts _ _ _ + refine WP.spec_bind (hF.restartTail s5 _ d _ hR5 hS5 hdT hk5 (Or.inl rfl) hq5 hcore hnq) fun s7 hw => ?_ + rw [WP.spec_ok] + exact hw + +/-- `send_block_tailA`'s narrow write set: the two state words and scheduler words. -/ +theorem send_block_tail_fpA (hO : SchedOpsEA st T E R Bf SR) (hF : SchedFootEA st T E R Bf SR Wr) + (hP : EPlaced T E R Bf) (a : A) {s s3 : S} + {σ σ3 : Fl4SpecA64.State Unit} (hSR : SR s σ) {t d : U64} (ht : t ∈ T) (hdT : d ∈ T) (htd : t ≠ d) + (hon : L.mem s (affA d) = L.cpu s) {xt x3 : Fl4SpecA64.Tcb} (hR3 : EpStateRel T E R Bf (L.mem s3) σ3) + (hS3 : SR s3 σ3) (hkt3 : σ3.kheap (specPtr t) = some (.tcb xt)) (hsr : SndRunA σ3 (specPtr t) xt) + (hkd3 : σ3.kheap (specPtr d) = some (.tcb x3)) (hnr3 : Fl4SpecA64.runnable x3.tcbState = false) + (hni3 : x3.tcbState ≠ .idleThreadState) (hcls3 : inClassR x3.tcbState x3.tcbFault ∨ RelIfBound σ3 (specPtr d)) + (hq3 : ∀ e ∈ E, ∀ z, σ3.kheap (specPtr e) = some (.endpoint z) → specPtr d ∉ z.epQueue) : + (do + let (s4, a1) ← (do + let s5 ← sched.set_thread_state st s3 t sched.TS_INACTIVE + ok (s5, a)) + let s5 ← sched.set_thread_state st s4 d sched.TS_RUNNING + let s6 ← endpoint.unblock_sporadic st s5 d + let s7 ← sched.possible_switch_to st s6 d + ok (s7, a1)) ⦃ r => WritesIn st s3 r.1 (fun b => b = stA t ∨ b = stA d ∨ Wr σ3 s3 b) ⦄ := by + obtain ⟨hrun, hNQ, hsane, hni⟩ := hsr + have hqt := not_queued_codeA hR3 ht hkt3 (runnable_codeA hrun) + have hRB : RelIfBound σ3 (specPtr t) := hO.rd _ _ _ _ hS3 hkt3 (Or.inl hrun) + have hdt' : specPtr d ≠ specPtr t := fun h => htd (specPtr_inj h).symm + have hA : (do + let s5 ← sched.set_thread_state st s3 t sched.TS_INACTIVE + ok (s5, a)) ⦃ r => (EpStateRel T E R Bf (L.mem r.1) + (stsAct (updK σ3 (specPtr t) (.tcb { xt with tcbState := .inactive })) (specPtr t)) ∧ + SR r.1 (stsAct (updK σ3 (specPtr t) (.tcb { xt with tcbState := .inactive })) (specPtr t))) ∧ + WritesIn st s3 r.1 (fun b => b = stA t ∨ Wr σ3 s3 b) ⦄ := + WP.spec_bind (WP.spec_and (set_thread_state_blockA hO hF hP s3 σ3 hR3 hS3 ht hkt3 hqt hNQ hRB hsane hni + .inactive (by decide) sched.TS_INACTIVE (by unfold sched.TS_INACTIVE; rfl)) + (set_thread_state_block_fpA hO hF hP s3 σ3 hR3 hS3 ht hkt3 hqt hNQ hRB hsane hni + .inactive (by decide) sched.TS_INACTIVE (by unfold sched.TS_INACTIVE; rfl))) + fun s5 h5 => by rw [WP.spec_ok]; exact ⟨⟨h5.1.1, h5.1.2.1⟩, h5.2⟩ + refine WP.spec_bind hA ?_ + rintro ⟨s4, a1⟩ ⟨⟨hR4, hS4⟩, hw4⟩ + dsimp only at hR4 hS4 hw4 ⊢ + have hk4 : (stsAct (updK σ3 (specPtr t) (.tcb { xt with tcbState := .inactive })) (specPtr t)).kheap (specPtr d) = + some (.tcb x3) := by rw [stsAct_kheap, updK_kheap, Function.update_of_ne hdt', hkd3] + have hq4 : ∀ e ∈ E, ∀ z, (stsAct (updK σ3 (specPtr t) (.tcb { xt with tcbState := .inactive })) + (specPtr t)).kheap (specPtr e) = some (.endpoint z) → specPtr d ∉ z.epQueue := by + intro e he z hz + rw [stsAct_kheap, updK_kheap] at hz + by_cases hne : specPtr e = specPtr t + · rw [hne, Function.update_self] at hz; cases hz + · rw [Function.update_of_ne hne] at hz; exact hq3 e he z hz + have hcls4 : inClassR x3.tcbState x3.tcbFault ∨ RelIfBound (stsAct (updK σ3 (specPtr t) + (.tcb { xt with tcbState := .inactive })) (specPtr t)) (specPtr d) := + hcls3.imp id fun h => RelIfBound.keep h (by rw [stsAct_releaseQueue]; rfl) (by rw [stsAct_curTime]; rfl) + (fun x' hx' => ⟨x', by rwa [stsAct_kheap, updK_kheap, Function.update_of_ne hdt'] at hx', rfl⟩) + (fun q y n hq' => by + rw [stsAct_kheap, updK_kheap, Function.update_apply] at hq' + split_ifs at hq' <;> first | exact hq' | cases hq') + have hNB := NoBlockE.updT (σ := stsAct (updK σ3 (specPtr t) (.tcb { xt with tcbState := .inactive })) (specPtr t)) + hk4 { x3 with tcbState := .running } (Or.inr (Or.inl ⟨rfl, hni3, hcls4⟩)) + have hz4 : SSzA σ3 (stsAct (updK σ3 (specPtr t) (.tcb { xt with tcbState := .inactive })) (specPtr t)) := + (ssz_updK_tcb _ _ _).trans (ssz_stsAct _ _) + refine WP.spec_bind (WP.spec_and + (set_thread_state_relF hO hF hP s4 _ hR4 hS4 hdT hk4 hq4 .running (by decide) + sched.TS_RUNNING (by unfold sched.TS_RUNNING; rfl) hNB) + (set_thread_state_fpA hO.to0 hF.to0 hP hS4 s4 _ hR4 (WritesIn.refl _ _) (EpStep.refl _) hdT hk4 hq4 .running + (by decide) sched.TS_RUNNING (by unfold sched.TS_RUNNING; rfl) hNB)) ?_ + rintro s5 ⟨⟨hR5, hS5, -⟩, hw5⟩ + have h5 : WritesIn st s3 s5 (fun b => b = stA t ∨ b = stA d ∨ Wr σ3 s3 b) := + WritesIn.schedA hF (hw4.mono fun b h => h.imp id Or.inr) hz4 hw5 (fun _ h => Or.inr (Or.inl h)) + (fun _ h => Or.inr (Or.inr h)) + refine WP.spec_mono (send_tail_fpA hO hF a1 hSR hdT hon hS4 hk4 hnr3 hq4 hR5 hS5) fun r hr => ?_ + exact WritesIn.schedA hF h5 (hz4.trans ((ssz_updK_tcb _ _ _).trans (ssz_stsAct _ _))) + (B := fun _ => False) (hr.mono fun _ h => Or.inr h) (fun _ h => h.elim) (fun _ h => Or.inr (Or.inr h)) + +open Fl4SpecA64 in +/-- **The narrow write set of `send_ipc` to a waiting receiver** (module docstring), under `send_ipc_recvA`'s +hypotheses. -/ +theorem send_ipc_recv_fpA (hO : SchedOpsEA st T E R Bf SR) (hF : SchedFootEA st T E R Bf SR Wr) + (hP : EPlaced T E R Bf) {th : TypedHeapA} {kb : U64} (hPl : PlaceA th kb) + (s : S) (a : A) (σ : State Unit) (hR : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) + (hSt : StateRelA th (L.mem s) σ) (hV : SlotsValidA th (L.mem s)) (hC : BufCover st T Bf s σ) + (hNative : BufNativeA T (L.mem s)) + (hoff : ∀ y, IsSlotA th y → ¬ EWrite T E R Bf y ∧ ¬ EWrite T E R Bf (core.num.U64.wrapping_add y 8#u64)) + {t : U64} (ht : t ∈ T) (hth : th t = some .tcb) {xt : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hnb : tsCode xt.tcbState ≠ 3) + {e d : U64} (he : e ∈ E) {q : List ObjRef} (hk : σ.kheap (specPtr e) = some (.endpoint (.recvEP (specPtr d :: q)))) + (hdh : th d = some .tcb) {xd : Tcb} (hkd : σ.kheap (specPtr d) = some (.tcb xd)) + (hu : ∀ rp, ipcBufOfA true xd = some rp → ∀ k, k < 128 → inUserFrame (bufAddr rp k) σ) + (hrep : RepOKA σ (specPtr d)) (hon : L.mem s (affA d) = L.cpu s) + (badge : U64) (g gr : Bool) (blk call cd : Bool) + (hcase : (xt.tcbFault = none ∧ (g = false ∨ ipcBufOfA false xt = none ∨ + unat (dataToMessageInfo (xt.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0)) ∨ + ∃ f, xt.tcbFault = some f ∧ FaultOKA (L := L) s t (ipcBufOfA true xd) xt f) + (hbr : (call = false ∧ xt.tcbFault = none ∧ cd = false) ∨ + ((call = true ∨ xt.tcbFault ≠ none) ∧ + ((g = false ∧ gr = false) ∨ ∀ e' r dd, xd.tcbState ≠ .blockedOnReceive e' (some r) dd) ∧ + SndRunA σ (specPtr t) xt)) : + endpoint.send_ipc st ar s a blk call badge g gr cd t e + ⦃ r => WritesIn st s r.1 (SendFpA st T R Bf Wr σ s t e d (call = true ∨ xt.tcbFault ≠ none)) ⦄ := by + refine send_ipc_recvContA hO hP hPl s a σ hR hSR hSt hV hC hNative hoff ht hth hkt hnb + he hk hdh hkd hu hrep badge g gr blk call cd hcase ?_ + intro s1 s2 σ2 x2 rep dd σ1 hdT htd hde hS1 hR2 hS2 hWf hE hW2 hE2 hfr2 hkt2 hkd1 hkd2 hstd hst2' hrr hq2 hT hfp + have htd' : specPtr t ≠ specPtr d := fun h => htd (specPtr_inj h) + have hK2 := SchedKeepA.step hE + generalize hW : (call = true ∨ xt.tcbFault ≠ none) = W + have h2 : WritesIn st s s2 (SendFpA st T R Bf Wr σ s t e d W) := hfp.mono fun _ h => Or.inl h + have hz2 : SSzA σ σ2 := ssz_of_epStep hE + unfold sendRecvAfterTransferA sendRecvAfterUnlinkA + step with obj.Tcb.reply_obj.spec as ⟨i, hi⟩ + have hi0 : rep = none → i = 0#u64 := fun h => by + rw [hi]; exact optRef_none (((hR2.at_tcb hdT hkd2).recv _ _ _ hst2').1.trans h) + refine WP.spec_bind (WP.spec_and + (send_recv_replyA hO.to0 hF.to0 hP hS1 hR2 hS2 hW2 hE2 hdT hfr2 htd' hkt2 hkd1 hkd2 hstd hst2' hrr hq2 i hi) + (send_recv_reply_fpA hO.to0 hF.to0 hP hS1 hR2 hW2 hE2 hdT hfr2 htd' hkd1 hkd2 hstd hst2' hrr hq2 i hi)) ?_ + rintro s3 ⟨⟨⟨σ3, x3, hR3, hS3, hU, hkt3, hkd3, hnr3, hni3, hcls3, hq3, hK3, hz3⟩, -⟩, hw3⟩ + have hzσ3 : SSzA σ σ3 := (ssz_of_epStep hE).trans hz3 + have h3 : WritesIn st s s3 (SendFpA st T R Bf Wr σ s t e d W) := + WritesIn.schedA hF h2 hz2 (B := fun b => (∃ r ∈ R, b = rTcbA r) ∨ b = stA d) + (hw3.mono fun _ h => or_assoc.mpr h) + (fun _ h => h.elim (fun h => Or.inr (Or.inl h)) fun h => Or.inr (Or.inr (Or.inr (Or.inl h)))) + (fun _ h => Or.inr (Or.inr (Or.inr (Or.inr h)))) + have hxt3 := hR3.at_tcb ht hkt3 + rcases hbr with ⟨rfl, hft, rfl⟩ | ⟨hcf, hnp, hsr⟩ + · simp only [Bool.false_eq_true, ↓reduceIte] + step with obj.Tcb.fault_type.spec as ⟨i1, hi1⟩ + have hf0 : i1 = fl4_abi.consts.fault.NullFault := by + rw [hi1, hxt3.fault0.2 hft]; unfold fl4_abi.consts.fault.NullFault; rfl + simp only [hf0, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + apply ok_bind_specA + have hNB := NoBlockE.updT (σ := σ3) hkd3 { x3 with tcbState := .running } (Or.inr (Or.inl ⟨rfl, hni3, hcls3⟩)) + refine WP.spec_bind (WP.spec_and + (set_thread_state_relF hO hF hP s3 σ3 hR3 hS3 hdT hkd3 hq3 .running (by decide) + sched.TS_RUNNING (by unfold sched.TS_RUNNING; rfl) hNB) + (set_thread_state_fpA hO.to0 hF.to0 hP hS3 s3 σ3 hR3 (WritesIn.refl _ _) (EpStep.refl _) hdT hkd3 hq3 .running + (by decide) sched.TS_RUNNING (by unfold sched.TS_RUNNING; rfl) hNB)) ?_ + rintro s5 ⟨⟨hR5, hS5, -⟩, hw5⟩ + have h5 : WritesIn st s s5 (SendFpA st T R Bf Wr σ s t e d W) := + WritesIn.schedA hF h3 hzσ3 hw5 (fun _ h => Or.inr (Or.inr (Or.inr (Or.inl h)))) + (fun _ h => Or.inr (Or.inr (Or.inr (Or.inr h)))) + refine WP.spec_mono (send_tail_fpA hO hF a hSR hdT hon hS3 hkd3 hnr3 hq3 hR5 hS5) fun r hr => ?_ + exact WritesIn.schedA hF h5 (hzσ3.trans ((ssz_updK_tcb _ _ _).trans (ssz_stsAct _ _))) + (B := fun _ => False) (hr.mono fun _ h => Or.inr h) (fun _ h => h.elim) + (fun _ h => Or.inr (Or.inr (Or.inr (Or.inr h)))) + · have hsr3 := hsr.keep (hK2.trans hK3) + have hnp' : (g = false ∧ gr = false) ∨ rep = none := hnp.imp id fun h => by + cases rep with + | none => rfl + | some r => exact absurd hstd (h _ r dd) + have hw : W := hW ▸ hcf + have htail : (do + let (s4, a1) ← (do + let s5 ← sched.set_thread_state st s3 t sched.TS_INACTIVE + ok (s5, a)) + let s5 ← sched.set_thread_state st s4 d sched.TS_RUNNING + let s6 ← endpoint.unblock_sporadic st s5 d + let s7 ← sched.possible_switch_to st s6 d + ok (s7, a1)) ⦃ r => WritesIn st s r.1 (SendFpA st T R Bf Wr σ s t e d W) ⦄ := by + refine WP.spec_mono (send_block_tail_fpA hO hF hP a hSR ht hdT htd hon hR3 hS3 hkt3 hsr3 hkd3 hnr3 hni3 + hcls3 hq3) fun r hr => ?_ + refine WritesIn.schedA hF h3 hzσ3 (B := fun b => b = stA t ∨ b = stA d) + (hr.mono fun _ h => or_assoc.mpr h) + (fun _ h => h.elim (fun h => Or.inr (Or.inr (Or.inl ⟨hw, h⟩))) fun h => Or.inr (Or.inr (Or.inr (Or.inl h)))) + (fun _ h => Or.inr (Or.inr (Or.inr (Or.inr h)))) + cases call with + | true => + simp only [↓reduceIte] + rcases hnp' with ⟨rfl, rfl⟩ | hrn + · simp only [Bool.false_eq_true, ↓reduceIte, ite_self] + exact htail + · simp only [hi0 hrn, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + exact htail + | false => + have hfn : xt.tcbFault ≠ none := hcf.resolve_left (by simp) + simp only [Bool.false_eq_true, ↓reduceIte] + step with obj.Tcb.fault_type.spec as ⟨i1, hi1⟩ + have hb1 : (i1 != fl4_abi.consts.fault.NullFault) = true := by + rw [bne_iff_ne, hi1] + intro h + apply hfn + apply hxt3.fault0.1 + rw [h]; unfold fl4_abi.consts.fault.NullFault; rfl + simp only [hb1, ↓reduceIte] + rcases hnp' with ⟨rfl, rfl⟩ | hrn + · simp only [Bool.false_eq_true, ↓reduceIte, ite_self] + exact htail + · simp only [hi0 hrn, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + exact htail + +end + +end + +end Fl4ProofsA64.P3.Endp + diff --git a/proofs/Fl4ProofsA64/P3/EpSendRecvA.lean b/proofs/Fl4ProofsA64/P3/EpSendRecvA.lean new file mode 100644 index 000000000..516abbcb3 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/EpSendRecvA.lean @@ -0,0 +1,839 @@ +/- +P3 (aarch64, A64-P3-IPC gen 11): **`send_ipc` to an endpoint with a waiting receiver refines l4v `send_ipc`** +(`Z = Unit`) for a non-call send without SC donation from a sender without a fault (x86 `P3/EpSend`'s +`send_ipc_recv_gen`, `P3/EpTail`'s `specWakeTailE`): the receiver is dequeued (`ep_dequeue_raw`), the message +transferred (gen 10's `do_ipc_transfer_anyA`, both buffers, at the dequeued state), its reply object (if any) unlinked +(`reply_unlink_rel`), it is set running (`set_thread_state_rel`, the `stsAct` split), then the scheduler tail. + +* Spec: `specSendTailA` (the tail after the receiver runs; x86 `specWakeTailE`), **`test_eq_assertA`** + (`if_sporadic_cur_sc_test_refill_unblock_check` = its `assert` twin: both are `when (guard ∧ sc ≠ cur)`), hence + `specSendTailA_eq` (= `specRestartTail`), **`sendIpc_recv_runA`** (x86 `sendIpc_recv_eq`: l4v `send_ipc` from the + transfer's and the unlink's single outcomes). +* (gen 12, ruling Q1) `RunTailOpA` is gone: the receiver's tail uses the widened `SchedOpsEA.restartTail` + (`Running ∨ Restart`; FL4's tail is `rustRestartTail`, the spec's `specRestartTail` by `specSendTailA_eq`). +* (gen 12) **`sendIpc_recv_blockA`** (x86 `sendIpc_callInactive_eq`: a call / faulted sender without a reply push + becomes `Inactive`), `SchedKeepA` (queues, idle thread, scheduler action kept or choose-new), `SndRunA` (the + sender's scheduler facts), `runnable_codeA`, `FaultOKA.ewA`, `not_queued_codeA`. +* The transfer's placement at the dequeued state: `not_ew_offA` (a TCB word outside the written offsets is no + endpoint write), `ibSame_ewA`, `lookup_ipc_buffer_sameA` (P1's `lookup_ipc_buffer_same` for both buffers), + **`BufCover.ewA`**, **`StateRelA.updEpA`** (an endpoint written, the slot words kept). +* FL4: `SendMidA` (the state after the reply unlink), **`send_recv_replyA`** (the reply word read against the + receiver's `BlockedOnReceive` reply, the unlink if any; the receiver keeps `inClassR` or gains `RelIfBound` from + `SchedOpsEA.rd` before the unlink, so `Running` is `NoBlockE`'s branch 2), **`send_tail_fromA`** (from the `stsAct` + state `set_thread_state dest Running` leaves: `SchedOpsEA.restartTail`), (gen 12) **`set_thread_state_blockA`** + (`SchedOpsEA.block`), **`send_block_tailA`**, and **`send_ipc_recvA`** (gen 11's `send_ipc_recv_runA`, replaced: + both branches, `hcase` / `hbr`). + +Hypotheses (case selectors / placement, no named premise): `EPlaced`, `PlaceA` / `StateRelA` / `SlotsValidA` / +`BufCover` / `hoff` / `hu` (the transfer's, gen 9–10), `hcaps` (no extra caps), the sender not blocked on a receive +(`hnb`) and without fault (`hft`), `do_call = false`, `can_donate = false` (FL4 reads the receiver's SC word, which +`EpStateRel` does not relate), `RepOKA` of the receiver (gen 6), the receiver on the current core (`hon`, gen 7). +(gen 12) The call / faulted-sender branch without a reply push (`hbr`'s second arm: no grant, or the receiver without +a reply object; the sender's `SndRunA`) is covered. Not covered: `reply_push` and the donation branch +(`schedContextDonate`), MCS contracts. +-/ +import Fl4ProofsA64.P3.EpFaultXferA +import Fl4ProofsA64.P3.EpCancelIpcA +import Fl4ProofsA64.P3.EpFootHelpA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched + +noncomputable section + +/-! ## The spec side -/ + +section Spec +open Fl4SpecA64 Fl4Spec + +/-- The spec's tail of `send_ipc` after the receiver runs (x86 `specWakeTailE`). -/ +def specSendTailA (d : ObjRef) : SMonad Unit Unit := do + let destSc ← getTcbObjRef Tcb.tcbSchedContext d + ifSporadicCurScTestRefillUnblockCheck destSc + possibleSwitchTo d + +/-- `if_sporadic_cur_sc_test_refill_unblock_check` is its `assert` twin: both run the unblock check when the SC is +sporadic and not the current one. -/ +theorem test_eq_assertA (o : Option ObjRef) : + ifSporadicCurScTestRefillUnblockCheck (Z := Unit) o = ifSporadicCurScAssertRefillUnblockCheck o := by + cases o with + | none => rfl + | some p => + unfold ifSporadicCurScTestRefillUnblockCheck ifSporadicCurScAssertRefillUnblockCheck ifCondRefillUnblockCheck + simp only [NonDet.maybeM] + congr 1; funext sc; congr 1; funext cur; congr 1; funext g + unfold NonDet.when + by_cases hg : g <;> by_cases hc : p = cur <;> simp [hg, hc] + +theorem specSendTailA_eq (d : ObjRef) : specSendTailA d = specRestartTail d := by + unfold specSendTailA specRestartTail + simp only [test_eq_assertA] + +theorem nd_when_false_bindA {σ' β : Type} {P : Prop} [Decidable P] (h : ¬ P) (m : NonDet σ' Unit) + (f : Unit → NonDet σ' β) (s : σ') : (NonDet.when P m >>= f) s = f () s := by + unfold NonDet.when; rw [if_neg h]; exact nd_pure_bind' () f s + +/-- **The spec's `send_ipc` to a waiting receiver** `d` (non-call, no donation, the sender without a fault), from the +transfer's and the reply unlink's single outcomes (x86 `sendIpc_recv_eq`). -/ +theorem sendIpc_recv_runA {σ : State Unit} {t e d : ObjRef} {q : List ObjRef} + (hk : σ.kheap e = some (.endpoint (.recvEP (d :: q)))) {xd : Tcb} (hkd : σ.kheap d = some (.tcb xd)) + (hde : d ≠ e) {e' : ObjRef} {rep : Option ObjRef} {dd : ReceiverPayload} + (hst : xd.tcbState = .blockedOnReceive e' rep dd) (blk : Bool) (badge : Badge) (g gr : Bool) + {σ2 σ3 : State Unit} + (hT : doIpcTransfer (Z := Unit) t (some e) badge g d (updK σ e (.endpoint (epDeq (.recvEP (d :: q)) d))) = + ({((), σ2)}, False)) + (hU : NonDet.maybeM (replyUnlinkTcb (Z := Unit) d) rep σ2 = ({((), σ3)}, False)) + {xt3 x3 : Tcb} (hkt3 : σ3.kheap t = some (.tcb xt3)) (hf3 : xt3.tcbFault = none) + (hkd3 : σ3.kheap d = some (.tcb x3)) : + sendIpc (Z := Unit) blk false badge g gr false t e σ = + specSendTailA d (stsAct (updK σ3 d (.tcb { x3 with tcbState := .running })) d) := by + have hkd1 : (updK σ e (.endpoint (epDeq (.recvEP (d :: q)) d))).kheap d = some (.tcb xd) := by + rw [updK_kheap, Function.update_of_ne hde, hkd] + unfold sendIpc + rw [nd_bind_one (getEndpoint_eq hk)] + cases blk <;> dsimp only <;> + · rw [nd_bind_one (tcbEpDequeue_eq hk (by simp [Endpoint.epQueue])), nd_bind_one (getThreadState_eq hkd1), hst] + dsimp only + rw [nd_pure_bind', nd_bind_one hT, nd_bind_one hU, nd_bind_one (getTcbObjRef_eq _ hkd3), + nd_bind_one (threadGet_eq _ hkt3), hf3] + simp only [Bool.false_eq_true, ne_eq, not_true_eq_false, or_self, ↓reduceIte, false_and] + rw [nd_when_false_bindA (by simp), nd_bind_one (setThreadState_eq _ hkd3)] + rfl + +/-- (gen 12) **The spec's `send_ipc` to a waiting receiver `d` for a call or a faulted sender without a reply push** +(x86 `sendIpc_callInactive_eq`): the sender becomes `Inactive`, then the receiver runs. -/ +theorem sendIpc_recv_blockA {σ : State Unit} {t e d : ObjRef} {q : List ObjRef} + (hk : σ.kheap e = some (.endpoint (.recvEP (d :: q)))) {xd : Tcb} (hkd : σ.kheap d = some (.tcb xd)) + (hde : d ≠ e) {e' : ObjRef} {rep : Option ObjRef} {dd : ReceiverPayload} + (hst : xd.tcbState = .blockedOnReceive e' rep dd) (blk call : Bool) (badge : Badge) (g gr cd : Bool) + {σ2 σ3 : State Unit} + (hT : doIpcTransfer (Z := Unit) t (some e) badge g d (updK σ e (.endpoint (epDeq (.recvEP (d :: q)) d))) = + ({((), σ2)}, False)) + (hU : NonDet.maybeM (replyUnlinkTcb (Z := Unit) d) rep σ2 = ({((), σ3)}, False)) + {xt3 x3 : Tcb} (hkt3 : σ3.kheap t = some (.tcb xt3)) (hkd3 : σ3.kheap d = some (.tcb x3)) (htd : t ≠ d) + (hcf : call = true ∨ xt3.tcbFault ≠ none) (hnp : (g = false ∧ gr = false) ∨ rep = none) : + sendIpc (Z := Unit) blk call badge g gr cd t e σ = + specSendTailA d (stsAct (updK (stsAct (updK σ3 t (.tcb { xt3 with tcbState := .inactive })) t) d + (.tcb { x3 with tcbState := .running })) d) := by + have hkd1 : (updK σ e (.endpoint (epDeq (.recvEP (d :: q)) d))).kheap d = some (.tcb xd) := by + rw [updK_kheap, Function.update_of_ne hde, hkd] + have hkd4 : (stsAct (updK σ3 t (.tcb { xt3 with tcbState := .inactive })) t).kheap d = some (.tcb x3) := by + rw [stsAct_kheap, updK_kheap, Function.update_of_ne (Ne.symm htd), hkd3] + have hn : ¬ ((g = true ∨ gr = true) ∧ rep ≠ none) := by + rintro ⟨h1, h2⟩ + rcases hnp with ⟨rfl, rfl⟩ | rfl + · simp at h1 + · exact h2 rfl + unfold sendIpc + rw [nd_bind_one (getEndpoint_eq hk)] + cases blk <;> dsimp only <;> + · rw [nd_bind_one (tcbEpDequeue_eq hk (by simp [Endpoint.epQueue])), nd_bind_one (getThreadState_eq hkd1), hst] + dsimp only + rw [nd_pure_bind', nd_bind_one hT, nd_bind_one hU, nd_bind_one (getTcbObjRef_eq _ hkd3), + nd_bind_one (threadGet_eq _ hkt3), if_pos hcf, if_neg hn, nd_bind_one (setThreadState_eq _ hkt3), + nd_bind_one (setThreadState_eq _ hkd4)] + rfl + +/-- (gen 12) **What the IPC steps keep of the scheduler state**: the ready and release queues, the idle thread, and +the scheduler action (or it becomes choose-new, `set_thread_state_act`). -/ +def SchedKeepA (σ σ' : State Unit) : Prop := + σ'.readyQueues = σ.readyQueues ∧ σ'.releaseQueue = σ.releaseQueue ∧ σ'.idleThread = σ.idleThread ∧ + (σ'.schedulerAction = σ.schedulerAction ∨ σ'.schedulerAction = .chooseNewThread) + +theorem SchedKeepA.refl (σ : State Unit) : SchedKeepA σ σ := ⟨rfl, rfl, rfl, Or.inl rfl⟩ + +theorem SchedKeepA.trans {σ1 σ2 σ3 : State Unit} (h1 : SchedKeepA σ1 σ2) (h2 : SchedKeepA σ2 σ3) : + SchedKeepA σ1 σ3 := by + obtain ⟨a1, b1, c1, d1⟩ := h1 + obtain ⟨a2, b2, c2, d2⟩ := h2 + refine ⟨a2.trans a1, b2.trans b1, c2.trans c1, ?_⟩ + rcases d2 with d2 | d2 + · rw [d2]; exact d1 + · exact Or.inr d2 + +theorem SchedKeepA.upd (σ : State Unit) (p : ObjRef) (o : KernelObject) : SchedKeepA σ (updK σ p o) := + ⟨rfl, rfl, rfl, Or.inl rfl⟩ + +theorem SchedKeepA.sts (σ : State Unit) (t : ObjRef) : SchedKeepA σ (stsAct σ t) := by + refine ⟨stsAct_readyQueues σ t, stsAct_releaseQueue σ t, stsAct_idleThread σ t, ?_⟩ + unfold stsAct + split_ifs + · exact Or.inr rfl + · exact Or.inl rfl + +theorem SchedKeepA.step {σ σ' : State Unit} (h : EpStep σ σ') : SchedKeepA σ σ' := + have e := h.2.1 + ⟨by have := congrArg AbstractState.readyQueues e; exact this, + by have := congrArg AbstractState.releaseQueue e; exact this, + by have := congrArg AbstractState.idleThread e; exact this, + Or.inl (by have := congrArg AbstractState.schedulerAction e; exact this)⟩ + +/-- (gen 12) **The scheduler facts of a running sender** (case selectors at the current thread, as `P3/NtfnBlockA`'s +`hRun` / `hNQ` / `hsane` / `hnidle`): runnable, in no scheduler queue, not the switch target, not idle. -/ +def SndRunA (σ : State Unit) (t : ObjRef) (x : Tcb) : Prop := + runnable x.tcbState = true ∧ NotQueued σ t ∧ (∀ u, σ.schedulerAction = .switchThread u → u ≠ t) ∧ + t ≠ σ.idleThread + +theorem SndRunA.keep {σ σ' : State Unit} {t : ObjRef} {x : Tcb} (h : SndRunA σ t x) (hk : SchedKeepA σ σ') : + SndRunA σ' t x := by + obtain ⟨h1, ⟨h2, h3⟩, h4, h5⟩ := h + obtain ⟨k1, k2, k3, k4⟩ := hk + refine ⟨h1, ⟨by rw [k1]; exact h2, by rw [k2]; exact h3⟩, fun u hu => ?_, by rw [k3]; exact h5⟩ + rcases k4 with k4 | k4 <;> rw [k4] at hu + · exact h4 u hu + · cases hu + +theorem runnable_codeA {ts : ThreadState} (h : runnable ts = true) : tsCode ts ≠ 3 ∧ tsCode ts ≠ 4 := by + rcases ts with _ | _ | _ | _ | _ | _ | _ | _ | ⟨⟨⟩⟩ <;> simp [runnable, tsCode] at h ⊢ + +end Spec + +/-! ## The transfer's placement after the dequeue -/ + +section Place +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {T E R : Set U64} + {Bf : Set (U64 × Fl4SpecA64.Word)} + +/-- A TCB word at an offset the endpoint code does not write is no endpoint write. -/ +theorem not_ew_offA (hP : EPlaced T E R Bf) {t K : U64} (ht : t ∈ T) (hK : K.val < 4096) + (hw : ¬ TWOffN K.val) (h1 : K.val ≠ 3496) (h2 : K.val ≠ 3504) : + ¬ EWrite T E R Bf (core.num.U64.wrapping_add t K) := by + have hv : (core.num.U64.wrapping_add t K).val = t.val + K.val := tw_val (hP.tcb t ht) hK + rintro (⟨e, he, h⟩ | ⟨r, hr, h⟩ | ⟨y, hy, h | h⟩ | h) + · have := hP.et e he t ht; omega + · have := hP.rt r hr t ht; omega + · by_cases hyt : y = t + · subst hyt + obtain ⟨-, h2'⟩ := h + rw [hv, show y.val + K.val - y.val = K.val by omega] at h2' + exact hw h2' + · have := tcb_disj hP ht hy hyt + obtain ⟨h1', h2'⟩ := h + unfold TWOffN at h2'; omega + · by_cases hyt : y = t + · subst hyt; unfold TL at h; omega + · have := tcb_disj hP ht hy hyt; unfold TL at h; omega + · have := hP.bt _ h t ht; omega + +/-- The words FL4's buffer lookup reads are no endpoint writes. -/ +theorem ibSame_ewA (hP : EPlaced T E R Bf) {s s' : S} (hW : WritesIn st s s' (EWrite T E R Bf)) {t : U64} + (ht : t ∈ T) : IbSame (L := L) s s' t := by + have hta := hP.tcb t ht + refine ⟨hW.2.2 _ (field_aligned hta (by simp) (by simp)) + (not_ew_offA hP ht (K := 3512#u64) (by simp) (by unfold TWOffN; simp) (by simp) (by simp)), ?_, ?_⟩ + · rw [sbWordA_eq] + exact hW.2.2 _ (field_aligned hta (by simp) (by simp)) + (not_ew_offA hP ht (K := 64#u64) (by simp) (by unfold TWOffN; simp) (by simp) (by simp)) + · rw [sbWordA8_eq] + exact hW.2.2 _ (field_aligned hta (by simp) (by simp)) + (not_ew_offA hP ht (K := 72#u64) (by simp) (by unfold TWOffN; simp) (by simp) (by simp)) + +/-- Endpoint writes preserve the IPC-buffer cap. -/ +theorem BufNativeA.ewA (hP : EPlaced T E R Bf) {s s' : S} + (hNative : BufNativeA T (L.mem s)) (hW : WritesIn st s s' (EWrite T E R Bf)) : + BufNativeA T (L.mem s') := + hNative.frame fun t ht => (ibSame_ewA hP hW ht).2 + +/-- P1's `lookup_ipc_buffer_same` for both buffers. -/ +theorem lookup_ipc_buffer_sameA {s s' : S} {t : U64} (h : IbSame (L := L) s s' t) (b : Bool) : + msg.lookup_ipc_buffer st s' b t = msg.lookup_ipc_buffer st s b t := by + obtain ⟨h1, h2, h3⟩ := h + unfold msg.lookup_ipc_buffer + rw [ipc_buffer_word, ipc_buffer_word, h1] + have hs : obj.Tcb.slot t layout.tcb.SLOT_BUFFER = ok (sbWordA t) := by simp [obj.Tcb.slot, lift, sbWordA] + rw [hs]; simp only [bind_ok] + have hc : obj.Slot.cap st (sbWordA t) s' = obj.Slot.cap st (sbWordA t) s := by + unfold obj.Slot.cap; rw [L.read_pair_eq, L.read_pair_eq, h2, h3] + rw [hc] + +/-- **`BufCover` across endpoint writes** that keep every TCB of the spec. -/ +theorem BufCover.ewA (hP : EPlaced T E R Bf) {s s' : S} {σ σ' : Fl4SpecA64.State Unit} + (hC : BufCover st T Bf s σ) (hW : WritesIn st s s' (EWrite T E R Bf)) + (hk : ∀ t ∈ T, ∀ x, σ'.kheap (specPtr t) = some (.tcb x) → σ.kheap (specPtr t) = some (.tcb x)) : + BufCover st T Bf s' σ' := by + intro t ht x b sb w hx hb hl + rw [lookup_ipc_buffer_sameA (ibSame_ewA hP hW ht)] at hl + exact hC t ht x b sb w (hk t ht x hx) hb hl + +/-- (gen 12) **`FaultOKA` across endpoint writes** (it reads the sender's flags and SC words, no endpoint write). -/ +theorem FaultOKA.ewA (hP : EPlaced T E R Bf) {s s' : S} (hW : WritesIn st s s' (EWrite T E R Bf)) {snd : U64} + (hs : snd ∈ T) {rbo : Option Fl4SpecA64.ObjRef} {x : Fl4SpecA64.Tcb} {f : Fl4SpecA64.Fault} + (h : FaultOKA (L := L) s snd rbo x f) : FaultOKA (L := L) s' snd rbo x f := by + have hta := hP.tcb snd hs + have h1 : L.mem s' (tTflA snd) = L.mem s (tTflA snd) := hW.2.2 _ (field_aligned hta (by simp) (by simp)) + (not_ew_offA hP hs (K := 3456#u64) (by simp) (by unfold TWOffN; simp) (by simp) (by simp)) + have h2 : L.mem s' (scA snd) = L.mem s (scA snd) := hW.2.2 _ (field_aligned hta (by simp) (by simp)) + (not_ew_offA hP hs (K := 3464#u64) (by simp) (by unfold TWOffN; simp) (by simp) (by simp)) + unfold FaultOKA at h ⊢ + rw [h1, h2] + exact h + +/-- (gen 12) A TCB whose state word is neither blocked-on-receive nor blocked-on-send is in no endpoint queue. -/ +theorem not_queued_codeA {m : U64 → U64} {σ : Fl4SpecA64.State Unit} (hR : EpStateRel T E R Bf m σ) {t : U64} + (ht : t ∈ T) {x : Fl4SpecA64.Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hc : tsCode x.tcbState ≠ 3 ∧ tsCode x.tcbState ≠ 4) : + ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue := by + intro e he z hz hm + obtain ⟨xs, -, hq, hb⟩ := (hR.at_ep he hz).queue + rw [hq, List.mem_map] at hm + obtain ⟨y, hy, ey⟩ := hm + have := specPtr_inj ey + subst this + have hst := (hR.at_tcb ht hk).st + rcases (hb y hy).2 with h | h <;> omega + +open Fl4SpecA64 in +/-- **`StateRelA` across an endpoint write** (the slot words kept). -/ +theorem _root_.Fl4ProofsA64.P3.StateRelA.updEpA {th : TypedHeapA} {kb : U64} (hPl : PlaceA th kb) + {mem mem' : U64 → U64} {σ : State Unit} (hR : StateRelA th mem σ) + (hc : ∀ y, IsSlotA th y → mem' y = mem y ∧ + mem' (core.num.U64.wrapping_add y 8#u64) = mem (core.num.U64.wrapping_add y 8#u64)) + {p : ObjRef} {x : Endpoint} (hk : σ.kheap p = some (.endpoint x)) (x' : Endpoint) : + StateRelA th mem' (updK σ p (.endpoint x')) := by + have hR' : StateRelA th mem' σ := hR.congrA fun z hz => by + obtain ⟨h1, h2⟩ := hc z (slotW_isSlot hPl hz) + unfold capAtA; rw [h1, h2] + refine ⟨fun q r hq => ?_, fun t ht => ?_⟩ + · obtain ⟨cs, h1, h2, h3⟩ := hR'.cnode q r hq + have hne : specPtr q ≠ p := fun e => by rw [e, hk] at h1; cases h1 + exact ⟨cs, by rw [updK_kheap, Function.update_of_ne hne, h1], h2, h3⟩ + · obtain ⟨z, h1, h2⟩ := hR'.tcb t ht + have hne : specPtr t ≠ p := fun e => by rw [e, hk] at h1; cases h1 + exact ⟨z, by rw [updK_kheap, Function.update_of_ne hne, h1], h2⟩ + +end Place + +/-! ## FL4 -/ + +section Fl4 +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] {T E R : Set U64} + {Bf : Set (U64 × Fl4SpecA64.Word)} {SR : S → Fl4SpecA64.State Unit → Prop} + {Wr : Fl4SpecA64.State Unit → S → U64 → Prop} + +theorem ok_bind_specA {α β : Type} (x : α) (f : α → Result β) {P : β → Prop} (h : f x ⦃ P ⦄) : + (do let y ← ok x; f y) ⦃ P ⦄ := by + simp only [Bind.bind, bind_ok]; exact h + +variable (T E R Bf SR) in +/-- The state after the reply unlink of `send_ipc` (`σ2` the spec's state after the transfer): related, the sender +kept, the receiver not runnable, not idle, in `inClassR` or with `RelIfBound`, and in no endpoint queue; (gen 12) the +scheduler state kept (`SchedKeepA`). -/ +def SendMidA (t d : U64) (xt : Fl4SpecA64.Tcb) (rep : Option Fl4SpecA64.ObjRef) (σ2 : Fl4SpecA64.State Unit) + (s3 : S) : Prop := + ∃ σ3 x3, EpStateRel T E R Bf (L.mem s3) σ3 ∧ SR s3 σ3 ∧ + Fl4Spec.NonDet.maybeM (Fl4SpecA64.replyUnlinkTcb (Z := Unit) (specPtr d)) rep σ2 = ({((), σ3)}, False) ∧ + σ3.kheap (specPtr t) = some (.tcb xt) ∧ σ3.kheap (specPtr d) = some (.tcb x3) ∧ + Fl4SpecA64.runnable x3.tcbState = false ∧ x3.tcbState ≠ .idleThreadState ∧ + (inClassR x3.tcbState x3.tcbFault ∨ RelIfBound σ3 (specPtr d)) ∧ + (∀ e ∈ E, ∀ z, σ3.kheap (specPtr e) = some (.endpoint z) → specPtr d ∉ z.epQueue) ∧ SchedKeepA σ2 σ3 ∧ + SSzA σ2 σ3 + +open Fl4SpecA64 in +/-- **The reply step of `send_ipc`**: FL4's reply word of the receiver against its `BlockedOnReceive` reply, and the +unlink if any (`reply_unlink_rel`, one `stable` step from the dequeued state `(s1, σ1)`). -/ +theorem send_recv_replyA (hO : SchedOpsE0A st T E R Bf SR) (hF : SchedFootE0A st T E R Bf SR Wr) + (hP : EPlaced T E R Bf) {s1 s2 : S} + {σ1 σ2 : State Unit} (hS1 : SR s1 σ1) (hR2 : EpStateRel T E R Bf (L.mem s2) σ2) (hS2 : SR s2 σ2) + (hW2 : WritesIn st s1 s2 (EWrite T E R Bf)) (hE2 : EpStep σ1 σ2) {t d : U64} (hdT : d ∈ T) + (hfr2 : ∀ p, p ≠ specPtr d → σ2.kheap p = σ1.kheap p) (htd' : specPtr t ≠ specPtr d) {xt xd x2 : Tcb} + (hkt2 : σ2.kheap (specPtr t) = some (.tcb xt)) (hkd1 : σ1.kheap (specPtr d) = some (.tcb xd)) + (hkd2 : σ2.kheap (specPtr d) = some (.tcb x2)) {e : ObjRef} {rep : Option ObjRef} {dd : ReceiverPayload} + (hst1 : xd.tcbState = .blockedOnReceive e rep dd) (hst2 : x2.tcbState = .blockedOnReceive e rep dd) + (hrr : ∀ r, rep = some r → ∃ xr, σ2.kheap r = some (.reply xr) ∧ xr.replyTcb = some (specPtr d)) + (hq2 : ∀ e ∈ E, ∀ z, σ2.kheap (specPtr e) = some (.endpoint z) → specPtr d ∉ z.epQueue) + (i : U64) (hi : i = L.mem s2 (tReplyA d)) : + (if (i != 0#u64) = true then ipc.reply_unlink st s2 i d else ok s2) ⦃ s3 => + SendMidA (L := L) T E R Bf SR t d xt rep σ2 s3 ∧ FpA st T E R Bf Wr σ2 s2 s3 ⦄ := by + subst hi + have hxd2 := hR2.at_tcb hdT hkd2 + obtain ⟨hrw, hrR⟩ := hxd2.recv _ _ _ hst2 + have hO2 : OnlyT σ1 σ2 (specPtr d) := fun q hq x' y' h1 h2 => by + rw [hfr2 q hq, h1] at h2; injection h2 with h2; injection h2 with h2; exact h2.symm + have hblk : runnable xd.tcbState = false ∧ xd.tcbState ≠ .idleThreadState := by + rw [hst1]; exact ⟨rfl, fun h => by cases h⟩ + have hrel2 : RelIfBound σ2 (specPtr d) := hO.rdRecv _ _ _ _ _ _ _ hS2 hkd2 hst2 + cases rep with + | none => + have h0 : L.mem s2 (tReplyA d) = 0#u64 := optRef_none hrw + simp only [h0, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + exact ⟨⟨σ2, x2, hR2, hS2, rfl, hkt2, hkd2, (by rw [hst2]; rfl), (by rw [hst2]; exact fun h => by cases h), + Or.inl (Or.inr (Or.inr (Or.inr ⟨_, _, _, hst2⟩))), hq2, SchedKeepA.refl _, SSzA.refl _⟩, FpA.refl _ _⟩ + | some r => + obtain ⟨hw0, hwr⟩ := optRef_some hrw + have hwR := hrR (by simp) + have hne0 : (L.mem s2 (tReplyA d) != 0#u64) = true := by simpa using hw0 + simp only [hne0, ↓reduceIte] + subst hwr + obtain ⟨xr, hkr, hback⟩ := hrr _ rfl + have hrt : specPtr (L.mem s2 (tReplyA d)) ≠ specPtr t := kheap_ne hkr hkt2 (fun _ h1 h2 => by subst h1; cases h2) + have hrd : specPtr (L.mem s2 (tReplyA d)) ≠ specPtr d := kheap_ne hkr hkd2 (fun _ h1 h2 => by subst h1; cases h2) + refine WP.spec_mono (reply_unlink_relF hO hF hP hS1 hR2 hW2 hE2 hdT hkd1 hkd2 hO2 hblk hq2 hwR hkr) ?_ + rintro s3 ⟨hR3, hS3, hfp⟩ + refine ⟨⟨_, { x2 with tcbState := .inactive }, hR3, hS3, replyUnlinkTcb_eq hkr hback hkd2 hst2, ?_, ?_, rfl, + (fun h => by cases h), Or.inr ?_, ?_, (SchedKeepA.upd _ _ _).trans ((SchedKeepA.upd _ _ _).trans + (SchedKeepA.sts _ _)), (ssz_updK_reply _ _ _).trans ((ssz_updK_tcb _ _ _).trans (ssz_stsAct _ _))⟩, hfp⟩ + · rw [stsAct_kheap, updK_kheap, Function.update_of_ne htd', updK_kheap, Function.update_of_ne hrt.symm, hkt2] + · rw [stsAct_kheap, updK_kheap, Function.update_self] + · refine RelIfBound.keep hrel2 (by rw [stsAct_releaseQueue]; rfl) (by rw [stsAct_curTime]; rfl) + (fun x' hx' => ?_) (fun q y' n hq' => ?_) + · rw [stsAct_kheap, updK_kheap, Function.update_self] at hx' + injection hx' with hx'; injection hx' with hx'; subst hx'; exact ⟨x2, hkd2, rfl⟩ + · rw [stsAct_kheap, updK_kheap, updK_kheap, Function.update_apply, Function.update_apply] at hq' + split_ifs at hq' <;> first | exact hq' | cases hq' + · intro e'' he'' z hz + rw [stsAct_kheap, updK_kheap] at hz + have hne : specPtr e'' ≠ specPtr d := by intro h; rw [h, Function.update_self] at hz; cases hz + rw [Function.update_of_ne hne, updK_kheap] at hz + have hne' : specPtr e'' ≠ specPtr (L.mem s2 (tReplyA d)) := by + intro h; rw [h, Function.update_self] at hz; cases hz + rw [Function.update_of_ne hne'] at hz + exact hq2 e'' he'' z hz + +/-- **The tail of `send_ipc`** from the `stsAct` state `set_thread_state dest Running` leaves (a separate +declaration): `SchedOpsEA.restartTail` (gen 12: widened to `Running`), the receiver on the current core (`coreKeep`) +and in no scheduler queue (`SchedOpsEA.notQueued` before, `NotQueued.sts`). -/ +theorem send_tail_fromA (hO : SchedOpsEA st T E R Bf SR) (hF : SchedFootEA st T E R Bf SR Wr) + (hP : EPlaced T E R Bf) + (a : A) {s s3 s5 : S} {σ σ3 : Fl4SpecA64.State Unit} (hSR : SR s σ) {d : U64} (hdT : d ∈ T) + (hon : L.mem s (affA d) = L.cpu s) {x3 : Fl4SpecA64.Tcb} (hS3 : SR s3 σ3) + (hkd3 : σ3.kheap (specPtr d) = some (.tcb x3)) (hnr : Fl4SpecA64.runnable x3.tcbState = false) + (hq3 : ∀ e ∈ E, ∀ z, σ3.kheap (specPtr e) = some (.endpoint z) → specPtr d ∉ z.epQueue) + (hR5 : EpStateRel T E R Bf (L.mem s5) + (stsAct (updK σ3 (specPtr d) (.tcb { x3 with tcbState := .running })) (specPtr d))) + (hS5 : SR s5 (stsAct (updK σ3 (specPtr d) (.tcb { x3 with tcbState := .running })) (specPtr d))) + {σb : Fl4SpecA64.State Unit} {sb : S} (hpre : FpA st T E R Bf Wr σb sb s5) + (hz : SSzA σb (stsAct (updK σ3 (specPtr d) (.tcb { x3 with tcbState := .running })) (specPtr d))) : + (do + let s6 ← endpoint.unblock_sporadic st s5 d + let s7 ← sched.possible_switch_to st s6 d + ok (s7, a)) ⦃ r => r.2 = a ∧ + ¬ (specSendTailA (specPtr d) (stsAct (updK σ3 (specPtr d) (.tcb { x3 with tcbState := .running })) + (specPtr d))).2 ∧ + ∃ σ', ((), σ') ∈ (specSendTailA (specPtr d) (stsAct (updK σ3 (specPtr d) + (.tcb { x3 with tcbState := .running })) (specPtr d))).1 ∧ + EpStateRel T E R Bf (L.mem r.1) σ' ∧ SR r.1 σ' ∧ FpA st T E R Bf Wr σb sb r.1 ⦄ := by + obtain ⟨hk5, hq5, -, -⟩ := sts_facts hkd3 hq3 .running + have hprog : (do + let s6 ← endpoint.unblock_sporadic st s5 d + let s7 ← sched.possible_switch_to st s6 d + ok (s7, a)) = (rustRestartTail st s5 d >>= fun s7 => ok (s7, a)) := by + first + | (simp only [rustRestartTail, bind_assoc]; done) + | (unfold rustRestartTail; simp only [Std.bind_tc_eq, Std.bind_assoc]) + rw [hprog, specSendTailA_eq] + have hcore := (hO.coreKeep _ _ _ _ hSR hS5).onCore hdT hon + have hnq : NotQueued (stsAct (updK σ3 (specPtr d) (.tcb { x3 with tcbState := .running })) (specPtr d)) + (specPtr d) := (hO.notQueued _ _ d x3 hS3 hkd3 hnr).sts _ _ _ + refine WP.spec_bind (WP.spec_and (hO.restartTail s5 _ d _ hR5 hS5 hdT hk5 (Or.inl rfl) hq5 hcore hnq) + (hF.restartTail s5 _ d _ hR5 hS5 hdT hk5 (Or.inl rfl) hq5 hcore hnq)) ?_ + rintro s7 ⟨⟨hnf, σ', hm, hf, hv, hS7⟩, hw⟩ + rw [WP.spec_ok] + exact ⟨rfl, hnf, σ', hm, hR5.frame hP hf hv, hS7, FpA.trans hF hpre hz (FpA.of_wr hw)⟩ + +/-- (gen 12) **`set_thread_state t ts` blocking a thread** (the sender of a call / faulted send set `Inactive`): the +state word, then `SchedOpsEA.block` (the thread in no scheduler queue, `RelIfBound`, not the switch target, not idle; +only its TCB changes), then `schedule_tcb`. -/ +theorem set_thread_state_blockA (hO : SchedOpsEA st T E R Bf SR) (hF : SchedFootEA st T E R Bf SR Wr) + (hP : EPlaced T E R Bf) (s : S) + (σ : Fl4SpecA64.State Unit) (hR : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) {t : U64} (ht : t ∈ T) + {x : Fl4SpecA64.Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) + (hNQ : NotQueued σ (specPtr t)) (hRB : RelIfBound σ (specPtr t)) + (hsane : ∀ u, σ.schedulerAction = .switchThread u → u ≠ specPtr t) (hni : specPtr t ≠ σ.idleThread) + (ts : Fl4SpecA64.ThreadState) (hts : tsCode ts < 3) (code : U64) (hcode : code.val = tsCode ts) : + sched.set_thread_state st s t code ⦃ s' => + EpStateRel T E R Bf (L.mem s') (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := ts })) (specPtr t)) ∧ + SR s' (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := ts })) (specPtr t)) ∧ + FpA st T E R Bf Wr σ s s' ⦄ := by + unfold sched.set_thread_state + step with obj.Tcb.set_state.spec as ⟨s1, hs1⟩ + subst hs1 + obtain ⟨hR1, hW1, hS1⟩ := write_state_rel hP s σ hR ht hk hq ts hts code hcode + have hSR1 := hO.block _ _ _ _ t hSR hNQ hRB hsane hni hW1 hS1 fun p x' y' h1 h2 hp => by + rw [updK_kheap, Function.update_of_ne hp, h1] at h2 + injection h2 with h2; injection h2 with h2; subst h2; exact ⟨rfl, id⟩ + refine WP.spec_mono (WP.spec_and (hO.scheduleTcb _ _ t hR1 hSR1 ht) (hF.scheduleTcb _ _ t hR1 hSR1 ht)) ?_ + rintro s2 ⟨⟨hf, hS⟩, hw⟩ + exact ⟨hR1.frame hP hf (KView.stsAct' _ _), hS, + FpA.trans hF (FpA.of_ew hW1) (ssz_updK_tcb σ _ _) (FpA.of_wr hw)⟩ + +/-- (gen 12) **The tail of `send_ipc` for a call / faulted sender without a reply push** (from the state after the +reply unlink; a separate declaration): the sender `Inactive` (`set_thread_state_blockA`, its scheduler facts +`SndRunA`, `RelIfBound` by `SchedOpsEA.rd`), then the receiver running and `send_tail_fromA`. -/ +theorem send_block_tailA (hO : SchedOpsEA st T E R Bf SR) (hF : SchedFootEA st T E R Bf SR Wr) + (hP : EPlaced T E R Bf) (a : A) {s s3 : S} + {σ σ3 : Fl4SpecA64.State Unit} (hSR : SR s σ) {t d : U64} (ht : t ∈ T) (hdT : d ∈ T) (htd : t ≠ d) + (hon : L.mem s (affA d) = L.cpu s) {xt x3 : Fl4SpecA64.Tcb} (hR3 : EpStateRel T E R Bf (L.mem s3) σ3) + (hS3 : SR s3 σ3) (hkt3 : σ3.kheap (specPtr t) = some (.tcb xt)) (hsr : SndRunA σ3 (specPtr t) xt) + (hkd3 : σ3.kheap (specPtr d) = some (.tcb x3)) (hnr3 : Fl4SpecA64.runnable x3.tcbState = false) + (hni3 : x3.tcbState ≠ .idleThreadState) (hcls3 : inClassR x3.tcbState x3.tcbFault ∨ RelIfBound σ3 (specPtr d)) + (hq3 : ∀ e ∈ E, ∀ z, σ3.kheap (specPtr e) = some (.endpoint z) → specPtr d ∉ z.epQueue) + {σ4 : Fl4SpecA64.State Unit} + (h4 : σ4 = stsAct (updK σ3 (specPtr t) (.tcb { xt with tcbState := .inactive })) (specPtr t)) + {σb : Fl4SpecA64.State Unit} {sb : S} (hpre : FpA st T E R Bf Wr σb sb s3) (hzb : SSzA σb σ3) : + (do + let (s4, a1) ← (do + let s5 ← sched.set_thread_state st s3 t sched.TS_INACTIVE + ok (s5, a)) + let s5 ← sched.set_thread_state st s4 d sched.TS_RUNNING + let s6 ← endpoint.unblock_sporadic st s5 d + let s7 ← sched.possible_switch_to st s6 d + ok (s7, a1)) ⦃ r => r.2 = a ∧ + ¬ (specSendTailA (specPtr d) (stsAct (updK σ4 (specPtr d) (.tcb { x3 with tcbState := .running })) + (specPtr d))).2 ∧ + ∃ σ', ((), σ') ∈ (specSendTailA (specPtr d) (stsAct (updK σ4 (specPtr d) + (.tcb { x3 with tcbState := .running })) (specPtr d))).1 ∧ + EpStateRel T E R Bf (L.mem r.1) σ' ∧ SR r.1 σ' ∧ FpA st T E R Bf Wr σb sb r.1 ⦄ := by + subst h4 + obtain ⟨hrun, hNQ, hsane, hni⟩ := hsr + have hqt := not_queued_codeA hR3 ht hkt3 (runnable_codeA hrun) + have hRB : RelIfBound σ3 (specPtr t) := hO.rd _ _ _ _ hS3 hkt3 (Or.inl hrun) + have hdt' : specPtr d ≠ specPtr t := fun h => htd (specPtr_inj h).symm + have hA : (do + let s5 ← sched.set_thread_state st s3 t sched.TS_INACTIVE + ok (s5, a)) ⦃ r => r.2 = a ∧ EpStateRel T E R Bf (L.mem r.1) + (stsAct (updK σ3 (specPtr t) (.tcb { xt with tcbState := .inactive })) (specPtr t)) ∧ + SR r.1 (stsAct (updK σ3 (specPtr t) (.tcb { xt with tcbState := .inactive })) (specPtr t)) ∧ + FpA st T E R Bf Wr σ3 s3 r.1 ⦄ := + WP.spec_bind (set_thread_state_blockA hO hF hP s3 σ3 hR3 hS3 ht hkt3 hqt hNQ hRB hsane hni + .inactive (by decide) sched.TS_INACTIVE (by unfold sched.TS_INACTIVE; rfl)) + fun s5 h5 => by rw [WP.spec_ok]; exact ⟨rfl, h5⟩ + refine WP.spec_bind hA ?_ + rintro ⟨s4, a1⟩ ⟨ha1, hR4, hS4, hfp4⟩ + dsimp only at ha1 hR4 hS4 hfp4 ⊢ + subst ha1 + have hk4 : (stsAct (updK σ3 (specPtr t) (.tcb { xt with tcbState := .inactive })) (specPtr t)).kheap (specPtr d) = + some (.tcb x3) := by rw [stsAct_kheap, updK_kheap, Function.update_of_ne hdt', hkd3] + have hq4 : ∀ e ∈ E, ∀ z, (stsAct (updK σ3 (specPtr t) (.tcb { xt with tcbState := .inactive })) + (specPtr t)).kheap (specPtr e) = some (.endpoint z) → specPtr d ∉ z.epQueue := by + intro e he z hz + rw [stsAct_kheap, updK_kheap] at hz + by_cases hne : specPtr e = specPtr t + · rw [hne, Function.update_self] at hz; cases hz + · rw [Function.update_of_ne hne] at hz; exact hq3 e he z hz + have hcls4 : inClassR x3.tcbState x3.tcbFault ∨ RelIfBound (stsAct (updK σ3 (specPtr t) + (.tcb { xt with tcbState := .inactive })) (specPtr t)) (specPtr d) := + hcls3.imp id fun h => RelIfBound.keep h (by rw [stsAct_releaseQueue]; rfl) (by rw [stsAct_curTime]; rfl) + (fun x' hx' => ⟨x', by rwa [stsAct_kheap, updK_kheap, Function.update_of_ne hdt'] at hx', rfl⟩) + (fun q y n hq' => by + rw [stsAct_kheap, updK_kheap, Function.update_apply] at hq' + split_ifs at hq' <;> first | exact hq' | cases hq') + refine WP.spec_bind (set_thread_state_relF hO hF hP s4 _ hR4 hS4 hdT hk4 hq4 .running (by decide) + sched.TS_RUNNING (by unfold sched.TS_RUNNING; rfl) (NoBlockE.updT hk4 _ (Or.inr (Or.inl ⟨rfl, hni3, hcls4⟩)))) ?_ + rintro s5 ⟨hR5, hS5, hfp5⟩ + have hz4 : SSzA σb (stsAct (updK σ3 (specPtr t) (.tcb { xt with tcbState := .inactive })) (specPtr t)) := + hzb.trans ((ssz_updK_tcb _ _ _).trans (ssz_stsAct _ _)) + exact send_tail_fromA hO hF hP a1 hSR hdT hon hS4 hk4 hnr3 hq4 hR5 hS5 + (FpA.trans hF (FpA.trans hF hpre hzb hfp4) hz4 hfp5) + (hz4.trans ((ssz_updK_tcb _ _ _).trans (ssz_stsAct _ _))) + +/-- The send-to-receiver program after unlinking the receiver's reply. -/ +def sendRecvAfterUnlinkA (s3 : S) (a : A) (t d i : U64) (call g gr cd : Bool) : Result (S × A) := do + let (s4, a1) ← + if call + then + if i != 0#u64 + then + if g + then + ipc.reply_push st ar s3 a t + d i cd + else + if gr + then + ipc.reply_push st ar s3 a t + d i cd + else + do + let s5 ← + sched.set_thread_state st s3 t + sched.TS_INACTIVE + ok (s5, a) + else + do + let s5 ← + sched.set_thread_state st s3 t + sched.TS_INACTIVE + ok (s5, a) + else + do + let i1 ← obj.Tcb.fault_type st t s3 + if i1 != fl4_abi.consts.fault.NullFault + then + if i != 0#u64 + then + if g + then + ipc.reply_push st ar s3 a t + d i cd + else + if gr + then + ipc.reply_push st ar s3 a t + d i cd + else + let s5 ← + sched.set_thread_state st s3 t + sched.TS_INACTIVE + ok (s5, a) + else + let s5 ← + sched.set_thread_state st s3 t + sched.TS_INACTIVE + ok (s5, a) + else + if cd + then + let i2 ← obj.Tcb.sc st d s3 + if i2 = 0#u64 + then + let i3 ← obj.Tcb.sc st t s3 + schedcontext.donate st ar s3 a i3 + d + else ok (s3, a) + else ok (s3, a) + let s5 ← + sched.set_thread_state st s4 d sched.TS_RUNNING + let s6 ← endpoint.unblock_sporadic st s5 d + let s7 ← sched.possible_switch_to st s6 d + ok (s7, a1) +/-- The send-to-receiver program after the IPC transfer, beginning with the reply unlink. -/ +def sendRecvAfterTransferA (s2 : S) (a : A) (t d : U64) (call g gr cd : Bool) : Result (S × A) := do + let i ← obj.Tcb.reply_obj st d s2 + let s3 ← + if i != 0#u64 + then ipc.reply_unlink st s2 i d + else ok s2 + sendRecvAfterUnlinkA (st := st) (ar := ar) s3 a t d i call g gr cd + +variable (st T Bf) in +/-- What the dequeue and the IPC transfer of a send to the waiting receiver `d` may write: `e`'s two words, the `Ep` +links of the threads blocked on `e` (at the entry state `s`), `d`'s registers and the IPC buffers. -/ +def RecvFpA (s : S) (e d b : U64) : Prop := + EW e b ∨ (∃ w ∈ T, L.mem s (tBlockA w) = e ∧ TL w b) ∨ RcvRegA d b ∨ BufW Bf b + +open Fl4SpecA64 in +/-- Share the dequeue, transfer and reply-word setup with the endpoint reply-push proof. -/ +theorem send_ipc_recvContA (hO : SchedOpsEA st T E R Bf SR) + (hP : EPlaced T E R Bf) {th : TypedHeapA} {kb : U64} (hPl : PlaceA th kb) + (s : S) (a : A) (σ : State Unit) (hR : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) + (hSt : StateRelA th (L.mem s) σ) (hV : SlotsValidA th (L.mem s)) (hC : BufCover st T Bf s σ) + (hNative : BufNativeA T (L.mem s)) + (hoff : ∀ y, IsSlotA th y → ¬ EWrite T E R Bf y ∧ ¬ EWrite T E R Bf (core.num.U64.wrapping_add y 8#u64)) + {t : U64} (ht : t ∈ T) (hth : th t = some .tcb) {xt : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hnb : tsCode xt.tcbState ≠ 3) + {e d : U64} (he : e ∈ E) {q : List ObjRef} (hk : σ.kheap (specPtr e) = some (.endpoint (.recvEP (specPtr d :: q)))) + (hdh : th d = some .tcb) {xd : Tcb} (hkd : σ.kheap (specPtr d) = some (.tcb xd)) + (hu : ∀ rp, ipcBufOfA true xd = some rp → ∀ k, k < 128 → inUserFrame (bufAddr rp k) σ) + (hrep : RepOKA σ (specPtr d)) + (badge : U64) (g gr : Bool) (blk call cd : Bool) + (hcase : (xt.tcbFault = none ∧ (g = false ∨ ipcBufOfA false xt = none ∨ + unat (dataToMessageInfo (xt.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0)) ∨ + ∃ f, xt.tcbFault = some f ∧ FaultOKA (L := L) s t (ipcBufOfA true xd) xt f) + {Post : S × A → Prop} + (hFinish : ∀ {s1 s2 : S} {σ2 : State Unit} {x2 : Tcb} {rep : Option ObjRef} {dd : ReceiverPayload}, + let σ1 := updK σ (specPtr e) (.endpoint (epDeq (.recvEP (specPtr d :: q)) (specPtr d))) + d ∈ T → t ≠ d → specPtr d ≠ specPtr e → + SR s1 σ1 → EpStateRel T E R Bf (L.mem s2) σ2 → SR s2 σ2 → + WritesIn st s s2 (EWrite T E R Bf) → EpStep σ σ2 → + WritesIn st s1 s2 (EWrite T E R Bf) → EpStep σ1 σ2 → + (∀ p, p ≠ specPtr d → σ2.kheap p = σ1.kheap p) → + σ2.kheap (specPtr t) = some (.tcb xt) → σ1.kheap (specPtr d) = some (.tcb xd) → + σ2.kheap (specPtr d) = some (.tcb x2) → + xd.tcbState = .blockedOnReceive (specPtr e) rep dd → + x2.tcbState = .blockedOnReceive (specPtr e) rep dd → + (∀ r, rep = some r → ∃ xr, σ2.kheap r = some (.reply xr) ∧ xr.replyTcb = some (specPtr d)) → + (∀ e' ∈ E, ∀ z, σ2.kheap (specPtr e') = some (.endpoint z) → specPtr d ∉ z.epQueue) → + (doIpcTransfer (Z := Unit) (specPtr t) (some (specPtr e)) badge.bv g (specPtr d) σ1 = + ({((), σ2)}, False)) → + WritesIn st s s2 (RecvFpA st T Bf s e d) → + sendRecvAfterTransferA (st := st) (ar := ar) s2 a t d call g gr cd ⦃ Post ⦄) : + endpoint.send_ipc st ar s a blk call badge g gr cd t e ⦃ Post ⦄ := by + obtain ⟨hst2, xs, hxs0, hq, hQ, hB⟩ := hR.at_ep he hk + obtain ⟨d', xs', rfl⟩ := List.exists_cons_of_ne_nil hxs0 + simp only [List.map_cons, List.cons.injEq] at hq + obtain ⟨hd', hq'⟩ := hq + have hdd : d = d' := specPtr_inj hd' + subst hdd + have hdT : d ∈ T := hQ.mem d (by simp) + have hda := hP.tcb d hdT + have hxd := hR.at_tcb hdT hkd + have hxt := hR.at_tcb ht hkt + have hd3 : (L.mem s (stA d)).val = 3 := (hB d (by simp)).1 + have hdb : L.mem s (tBlockA d) = e := (hB d (by simp)).2 + obtain ⟨e', rep, dd, hstd⟩ := tsCode_three (hxd.st.symm.trans hd3) + have he' : e' = specPtr e := by + have := hxd.blk e' (by rw [hstd]; rfl); rw [hdb] at this; exact this.symm + subst he' + have htd : t ≠ d := by intro h; subst h; exact hnb (hxt.st.symm.trans hd3) + have hde : specPtr d ≠ specPtr e := kheap_ne hkd hk (fun _ h1 h2 => by subst h1; cases h2) + have hte : specPtr t ≠ specPtr e := kheap_ne hkt hk (fun _ h1 h2 => by subst h1; cases h2) + have htd' : specPtr t ≠ specPtr d := fun h => htd (specPtr_inj h) + -- FL4: the endpoint's state and queue + unfold endpoint.send_ipc + step with obj.Ep.state.spec as ⟨st0, hst0⟩ + rw [epState_eq, hst2] at hst0 + subst hst0 + rw [if_neg (by rw [ep_state_vals.1]; decide), if_neg (by rw [ep_state_vals.2.1]; decide)] + step with endpoint.get_queue.spec as ⟨q0, hq0⟩ + have hhead : q0.head = d := by rw [hq0, epQ_eq]; exact hQ.head + rw [hhead] + -- the dequeue + have hd : specPtr d ∈ (Endpoint.recvEP (specPtr d :: q)).epQueue := by simp [Endpoint.epQueue] + refine WP.spec_bind (WP.spec_and (ep_dequeue_raw hP s σ hR he hk hd) + (ep_dequeue_full hP s he d (d :: xs') hQ (by simp))) ?_ + rintro s1 ⟨⟨-, hR1, hW1, hE1, hA1⟩, -, -, hQA, -⟩ + have hS1 := hO.stable _ _ _ _ hSR hW1 hE1 (NoBlockE.updO _ _ fun _ h => by cases h) + have hkt1 : (updK σ (specPtr e) (.endpoint (epDeq (.recvEP (specPtr d :: q)) (specPtr d)))).kheap (specPtr t) = + some (.tcb xt) := by rw [updK_kheap, Function.update_of_ne hte, hkt] + have hkd1 : (updK σ (specPtr e) (.endpoint (epDeq (.recvEP (specPtr d :: q)) (specPtr d)))).kheap (specPtr d) = + some (.tcb xd) := by rw [updK_kheap, Function.update_of_ne hde, hkd] + -- the transfer's placement at the dequeued state + have hmem : ∀ y, IsSlotA th y → L.mem s1 y = L.mem s y ∧ + L.mem s1 (core.num.U64.wrapping_add y 8#u64) = L.mem s (core.num.U64.wrapping_add y 8#u64) := by + intro y hy + have hal : Aligned y := by have := (IsSlotA.al32 hPl hy).1; unfold Aligned; omega + exact ⟨hW1.2.2 y hal (hoff y hy).1, hW1.2.2 _ (wa_al hal (by simp)) (hoff y hy).2⟩ + have hSt1 := hSt.updEpA hPl hmem hk (epDeq (.recvEP (specPtr d :: q)) (specPtr d)) + have hV1 : SlotsValidA th (L.mem s1) := slotsValid_xferA hV hmem + have hC1 : BufCover st T Bf s1 (updK σ (specPtr e) (.endpoint (epDeq (.recvEP (specPtr d :: q)) (specPtr d)))) := + BufCover.ewA hP hC hW1 fun u _ x hx => by + have hne : specPtr u ≠ specPtr e := fun h => by + rw [h, updK_kheap, Function.update_self] at hx; cases hx + rwa [updK_kheap, Function.update_of_ne hne] at hx + have hu1 : ∀ rp, ipcBufOfA true xd = some rp → ∀ k, k < 128 → + inUserFrame (bufAddr rp k) (updK σ (specPtr e) (.endpoint (epDeq (.recvEP (specPtr d :: q)) (specPtr d)))) := + fun rp hrp k hk' => inUserFrame_updK hk (fun _ h => by cases h) _ (hu rp hrp k hk') + -- the transfer + refine WP.spec_bind (do_ipc_transfer_anyA hO hP hPl s1 a _ hR1 hS1 hSt1 hV1 hC1 (hNative.ewA hP hW1) ht hdT htd hth hdh hkt1 hkd1 + hu1 hoff e badge g (hcase.imp id fun ⟨f, hf, hok⟩ => ⟨f, hf, FaultOKA.ewA hP hW1 ht hok⟩) + (some (specPtr e))) ?_ + rintro s2 ⟨σ2, hT, hpost⟩ + obtain ⟨hR2, hS2, hag2, hW2, hE2, hfr2, x2, hkd2, hx2⟩ := hpost + have hfp : WritesIn st s s2 (RecvFpA st T Bf s e d) := + ⟨hW2.1.trans hW1.1, hW2.2.1.trans hW1.2.1, fun b hb hn => + (hag2 b hb ⟨fun h => hn (Or.inr (Or.inr (Or.inl h))), fun h => hn (Or.inr (Or.inr (Or.inr h)))⟩).trans + (hQA b hb ⟨fun h => hn (Or.inl h), fun ⟨w, hw, hl⟩ => + hn (Or.inr (Or.inl ⟨w, hQ.mem w hw, (hB w hw).2, hl⟩))⟩)⟩ + have hkt2 : σ2.kheap (specPtr t) = some (.tcb xt) := by rw [hfr2 _ htd', hkt1] + have hst2' : x2.tcbState = .blockedOnReceive (specPtr e) rep dd := by rw [hx2]; exact hstd + have hk1e : (updK σ (specPtr e) (.endpoint (epDeq (.recvEP (specPtr d :: q)) (specPtr d)))).kheap (specPtr e) = + some (.endpoint (epDeq (.recvEP (specPtr d :: q)) (specPtr d))) := by rw [updK_kheap, Function.update_self] + have hdb1 : L.mem s1 (tBlockA d) = e := by rw [hA1 _ (a_blk hda).1 ⟨d, hdT, (a_blk hda).2⟩, hdb] + have hq1 := dequeued_not_queued hR1 he hdb1 hk1e + (epDeq_not_mem (z := .recvEP (specPtr d :: q)) (xs := d :: xs') (by rw [hq']; rfl) hQ.nodup d) + have hq2 : ∀ e'' ∈ E, ∀ z, σ2.kheap (specPtr e'') = some (.endpoint z) → specPtr d ∉ z.epQueue := by + intro e'' he'' z hz + have hne : specPtr e'' ≠ specPtr d := by intro h; rw [h, hkd2] at hz; cases hz + exact hq1 e'' he'' z (by rwa [hfr2 _ hne] at hz) + have hrr : ∀ r, rep = some r → ∃ xr, σ2.kheap r = some (.reply xr) ∧ xr.replyTcb = some (specPtr d) := by + intro r hr + obtain ⟨xr, hkr, hb⟩ := hrep xd hkd _ _ _ (by rw [hstd, hr]) + have hre : r ≠ specPtr e := fun h => by rw [h, hk] at hkr; cases hkr + have hrd : r ≠ specPtr d := fun h => by rw [h, hkd] at hkr; cases hkr + exact ⟨xr, by rw [hfr2 _ hrd, updK_kheap, Function.update_of_ne hre, hkr], hb⟩ + exact hFinish hdT htd hde hS1 hR2 hS2 (WritesIn.ew hW1 hW2) (hE1.trans hE2) + hW2 hE2 hfr2 hkt2 hkd1 hkd2 hstd hst2' hrr hq2 hT hfp + +open Fl4SpecA64 in +/-- **`send_ipc` to a waiting receiver refines l4v `send_ipc`** (x86 `send_ipc_recv_gen`): one spec outcome related +(`EpStateRel ∧ SR`), the spec not failing. Both IPC buffers (gen 10's `do_ipc_transfer_anyA`, a faulted or a +non-faulted sender, `hcase`), a receiver with or without a reply object. Branches (`hbr`, case selectors): (gen 11) a +non-call send without donation from a sender without a fault; (gen 12) a call or a faulted sender without a reply push +(no grant, or the receiver without a reply object): the sender becomes `Inactive` (its scheduler facts `SndRunA` at +the entry state). -/ +theorem send_ipc_recvA (hO : SchedOpsEA st T E R Bf SR) (hF : SchedFootEA st T E R Bf SR Wr) + (hP : EPlaced T E R Bf) {th : TypedHeapA} {kb : U64} (hPl : PlaceA th kb) + (s : S) (a : A) (σ : State Unit) (hR : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) + (hSt : StateRelA th (L.mem s) σ) (hV : SlotsValidA th (L.mem s)) (hC : BufCover st T Bf s σ) + (hNative : BufNativeA T (L.mem s)) + (hoff : ∀ y, IsSlotA th y → ¬ EWrite T E R Bf y ∧ ¬ EWrite T E R Bf (core.num.U64.wrapping_add y 8#u64)) + {t : U64} (ht : t ∈ T) (hth : th t = some .tcb) {xt : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hnb : tsCode xt.tcbState ≠ 3) + {e d : U64} (he : e ∈ E) {q : List ObjRef} (hk : σ.kheap (specPtr e) = some (.endpoint (.recvEP (specPtr d :: q)))) + (hdh : th d = some .tcb) {xd : Tcb} (hkd : σ.kheap (specPtr d) = some (.tcb xd)) + (hu : ∀ rp, ipcBufOfA true xd = some rp → ∀ k, k < 128 → inUserFrame (bufAddr rp k) σ) + (hrep : RepOKA σ (specPtr d)) (hon : L.mem s (affA d) = L.cpu s) + (badge : U64) (g gr : Bool) (blk call cd : Bool) + (hcase : (xt.tcbFault = none ∧ (g = false ∨ ipcBufOfA false xt = none ∨ + unat (dataToMessageInfo (xt.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0)) ∨ + ∃ f, xt.tcbFault = some f ∧ FaultOKA (L := L) s t (ipcBufOfA true xd) xt f) + (hbr : (call = false ∧ xt.tcbFault = none ∧ cd = false) ∨ + ((call = true ∨ xt.tcbFault ≠ none) ∧ + ((g = false ∧ gr = false) ∨ ∀ e' r dd, xd.tcbState ≠ .blockedOnReceive e' (some r) dd) ∧ + SndRunA σ (specPtr t) xt)) : + endpoint.send_ipc st ar s a blk call badge g gr cd t e ⦃ r => r.2 = a ∧ + ¬ (sendIpc (Z := Unit) blk call badge.bv g gr cd (specPtr t) (specPtr e) σ).2 ∧ + ∃ σ', ((), σ') ∈ (sendIpc (Z := Unit) blk call badge.bv g gr cd (specPtr t) (specPtr e) σ).1 ∧ + EpStateRel T E R Bf (L.mem r.1) σ' ∧ SR r.1 σ' ∧ FpA st T E R Bf Wr σ s r.1 ⦄ := by + refine send_ipc_recvContA hO hP hPl s a σ hR hSR hSt hV hC hNative hoff ht hth hkt hnb + he hk hdh hkd hu hrep badge g gr blk call cd hcase ?_ + intro s1 s2 σ2 x2 rep dd σ1 hdT htd hde hS1 hR2 hS2 hWf hE hW2 hE2 hfr2 hkt2 hkd1 hkd2 hstd hst2' hrr hq2 hT _ + have htd' : specPtr t ≠ specPtr d := fun h => htd (specPtr_inj h) + have hK2 := SchedKeepA.step hE + unfold sendRecvAfterTransferA sendRecvAfterUnlinkA + -- the reply word and the unlink + step with obj.Tcb.reply_obj.spec as ⟨i, hi⟩ + have hi0 : rep = none → i = 0#u64 := fun h => by + rw [hi]; exact optRef_none (((hR2.at_tcb hdT hkd2).recv _ _ _ hst2').1.trans h) + refine WP.spec_bind (send_recv_replyA hO.to0 hF.to0 hP hS1 hR2 hS2 hW2 hE2 hdT hfr2 htd' hkt2 hkd1 hkd2 hstd hst2' hrr hq2 i hi) + ?_ + rintro s3 ⟨⟨σ3, x3, hR3, hS3, hU, hkt3, hkd3, hnr3, hni3, hcls3, hq3, hK3, hz3⟩, hfp3⟩ + have hzσ3 : SSzA σ σ3 := (ssz_of_epStep hE).trans hz3 + have hpre3 := FpA.trans hF (FpA.of_ew hWf) (ssz_of_epStep hE) hfp3 + have hxt3 := hR3.at_tcb ht hkt3 + rcases hbr with ⟨rfl, hft, rfl⟩ | ⟨hcf, hnp, hsr⟩ + · -- no call, the sender without fault, no donation + simp only [Bool.false_eq_true, ↓reduceIte] + step with obj.Tcb.fault_type.spec as ⟨i1, hi1⟩ + have hf0 : i1 = fl4_abi.consts.fault.NullFault := by + rw [hi1, hxt3.fault0.2 hft]; unfold fl4_abi.consts.fault.NullFault; rfl + simp only [hf0, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + apply ok_bind_specA + -- the receiver runs + refine WP.spec_bind (set_thread_state_relF hO hF hP s3 σ3 hR3 hS3 hdT hkd3 hq3 .running (by decide) + sched.TS_RUNNING (by unfold sched.TS_RUNNING; rfl) (NoBlockE.updT hkd3 _ (Or.inr (Or.inl ⟨rfl, hni3, hcls3⟩)))) + ?_ + rintro s5 ⟨hR5, hS5, hfp5⟩ + rw [sendIpc_recv_runA hk hkd hde hstd blk badge.bv g gr hT hU hkt3 hft hkd3] + exact send_tail_fromA hO hF hP a hSR hdT hon hS3 hkd3 hnr3 hq3 hR5 hS5 (FpA.trans hF hpre3 hzσ3 hfp5) + (hzσ3.trans ((ssz_updK_tcb _ _ _).trans (ssz_stsAct _ _))) + · -- a call or a faulted sender, no reply push: the sender becomes inactive + have hsr3 := hsr.keep (hK2.trans hK3) + have hnp' : (g = false ∧ gr = false) ∨ rep = none := hnp.imp id fun h => by + cases rep with + | none => rfl + | some r => exact absurd hstd (h _ r dd) + rw [sendIpc_recv_blockA hk hkd hde hstd blk call badge.bv g gr cd hT hU hkt3 hkd3 htd' hcf hnp'] + have htail := send_block_tailA hO hF hP a hSR ht hdT htd hon hR3 hS3 hkt3 hsr3 hkd3 hnr3 hni3 hcls3 hq3 rfl hpre3 hzσ3 + cases call with + | true => + simp only [↓reduceIte] + rcases hnp' with ⟨rfl, rfl⟩ | hrn + · simp only [Bool.false_eq_true, ↓reduceIte, ite_self] + exact htail + · simp only [hi0 hrn, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + exact htail + | false => + have hfn : xt.tcbFault ≠ none := hcf.resolve_left (by simp) + simp only [Bool.false_eq_true, ↓reduceIte] + step with obj.Tcb.fault_type.spec as ⟨i1, hi1⟩ + have hb1 : (i1 != fl4_abi.consts.fault.NullFault) = true := by + rw [bne_iff_ne, hi1] + intro h + apply hfn + apply hxt3.fault0.1 + rw [h]; unfold fl4_abi.consts.fault.NullFault; rfl + simp only [hb1, ↓reduceIte] + rcases hnp' with ⟨rfl, rfl⟩ | hrn + · simp only [Bool.false_eq_true, ↓reduceIte, ite_self] + exact htail + · simp only [hi0 hrn, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + exact htail + +end Fl4 + +end + +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/EpSendTmoBA.lean b/proofs/Fl4ProofsA64/P3/EpSendTmoBA.lean new file mode 100644 index 000000000..85db96919 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/EpSendTmoBA.lean @@ -0,0 +1,775 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2b): **the timeout fault send of the current thread after the charge head** +(`SchedSRB … t`: `released_ipc_queues` at every TCB but the sender, whose SC is exhausted). + +The send of a timeout fault of a sender with an SC (`EpFaultXferA.FaultOKA` excludes it: the transfer also writes +the sender's SC, `update_consumed`), from `SchedSRB` (lead ruling, option A): + +* `RdExA.stepA`, `SchedSRB.blockExA`: `RdA.step` / `SchedSRA.block` for the relation excepting `p`; blocking `p` + itself (a state outside `inClassR`) closes back to `SchedSRA`. +* `SRBtA`: `SchedSRB … t` with `t` runnable. **`schedOpsE0_srbA`**, **`schedFootE0_srbA`**, **`timeoutScOp_srbA`**: the + dequeue, the transfer (with `update_consumed`) and the receiver's reply unlink under `SRBtA` (`SchedOpsE0A`, + `SchedFootE0A`, `TimeoutScOpA`). +* `set_thread_state_blockBA` (the `Inactive` branch) and `reply_push_noDonate_BA` (the push branch): the sender + blocked, from `SRBtA` to `IpcSchedRelA`. +* **`send_ipc_recv_tmoBA`**: the send to a waiting receiver (reply push or `Inactive`), with the write set `TmoFpA` (`SendFpA` and the + sender's reply word: no donation, so of the reply object only its thread word). + +Named premises: [P] `hmax`, `htu` (as `ipcSchedOpsA`). New module. +-/ +import Fl4ProofsA64.P3.EpFaultTmoA +import Fl4ProofsA64.P3.EpReplyPushA +import Fl4ProofsA64.P3.IpcScSendPushA +import Fl4ProofsA64.P3.IpcScEndpointPushSpecA +import Fl4ProofsA64.P3.IpcScEndpointPushProgramA +import Fl4ProofsA64.P3.ScCancelYieldBA +import Fl4ProofsA64.P3.TcbRestartTailA +import Fl4ProofsA64.P3.SpecDetA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4SpecA64 + +noncomputable section + +/-- `RdA.step` for `RdExA`. -/ +theorem RdExA.stepA {σ σ' : State Unit} {p : ObjRef} (h : RdExA σ p) (hS : SStepA σ σ') (hb : NoBlockESc σ σ') : + RdExA σ' p := by + intro u y hu hy hcls + obtain ⟨x, hx⟩ := hS.tcbBack u y hy + rcases hb u x y hx hy with ⟨-, hc⟩ | ⟨-, -, hc | hc⟩ | ⟨e, -, -⟩ + · exact (h u x hu hx (hc hcls)).step hS hx + · exact (h u x hu hx hc).step hS hx + · exact hc.step hS hx + · exfalso + rcases hcls with hc | ⟨_, hc⟩ | ⟨_, _, hc, -⟩ | ⟨_, _, _, hc⟩ <;> rw [e] at hc <;> simp [runnable] at hc + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- `SchedSRA.block` from `SchedSRB … p`, blocking `p` itself: `p` outside `inClassR` after, so `SchedSRA`. -/ +theorem SchedSRB.blockExA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s s' : S} {σ σ' : State Unit} + {p : ObjRef} (H : SchedSRB st Z m κ s σ p) {W : U64 → Prop} (hw : WritesIn st s s' W) + (hW : ∀ a, W a → ¬ SReadA st Z m s σ a) (hS : SStepA σ σ') + (hnq : (∀ d q, p ∉ σ.readyQueues d q) ∧ p ∉ σ.releaseQueue) + (hk : ∀ u x y, σ.kheap u = some (.tcb x) → σ'.kheap u = some (.tcb y) → u ≠ p → + y.tcbState = x.tcbState ∧ (inClassRSc y.tcbState y.tcbFault → inClassRSc x.tcbState x.tcbFault)) + {y : Tcb} (hy : σ'.kheap p = some (.tcb y)) (hc : ¬ inClassRSc y.tcbState y.tcbFault) : + SchedSRA st Z m κ s' σ' := by + have k : ∀ u, u ≠ p → (∃ x, σ.kheap u = some (.tcb x) ∧ runnable x.tcbState = true) → + ∃ y, σ'.kheap u = some (.tcb y) ∧ runnable y.tcbState = true := by + rintro u hu ⟨x, hx, hr⟩ + obtain ⟨y, hy, -⟩ := hS.tcb u x hx + exact ⟨y, hy, by rw [(hk u x y hx hy hu).1]; exact hr⟩ + refine (H.stableExBA hw hW hS ⟨fun d q u hu => ?_, fun u hu => ?_⟩ ?_).toA_ncA hy hc + · rw [hS.rq] at hu + exact k u (fun e => hnq.1 d q (e ▸ hu)) (H.run.1 d q u hu) + · rw [hS.rel] at hu + exact k u (fun e => hnq.2 (e ▸ hu)) (H.run.2 u hu) + · intro u y' hu hy' hcls + obtain ⟨x, hx⟩ := hS.tcbBack u y' hy' + exact (H.rdEx u x hu hx ((hk u x y' hx hy' hu).2 hcls)).step hS hx + +end + +end + +end Fl4ProofsA64.P3.Sched + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4SpecA64 Sched + +noncomputable section + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + +/-- One outcome and no failure: the outcome set is that outcome. -/ +theorem nd_singleton_of_detA {σ α : Type} {m : Fl4Spec.NonDet σ α} {s : σ} {x : α × σ} (hd : Det.DetAt m s) + (hf : ¬ (m s).2) (hx : x ∈ (m s).1) : m s = ({x}, False) := + Prod.ext (Set.ext fun y => ⟨fun hy => Set.mem_singleton_iff.2 (hd y x hy hx), + fun hy => (Set.mem_singleton_iff.1 hy) ▸ hx⟩) (eq_false hf) + +theorem det_specSendTailA (d : ObjRef) : Det.Det (specSendTailA d) := by + unfold specSendTailA getTcbObjRef + exact Det.det_bind (Det.det_threadGet _ _) fun _ => + Det.det_bind (Det.det_ifCondRefillUnblockCheck _ _ _) fun _ => Det.det_possibleSwitchTo _ + +variable (st) in +/-- The relation between the dequeue and the sender's block: `SchedSRB … t`, `t` runnable. -/ +def SRBtA (Z : XSetsA) (m : McState) (κ : U64 → U64) (t : U64) (s : S) (σ : State Unit) : Prop := + SchedSRB st Z m κ s σ (specPtr t) ∧ ∀ x, σ.kheap (specPtr t) = some (.tcb x) → runnable x.tcbState = true + +theorem schedOpsE0_srbA (P : IpcSchedPlacementA Z.th N E R Bf) (t : U64) : + SchedOpsE0A st Z.T E R Bf (SRBtA st Z m κ t) := by + refine ⟨?_, ?_, ?_⟩ + · rintro s s' σ σ' ⟨H, hr⟩ hw hs hb + have hS := endpSchedStep hs + refine ⟨H.stableExBA hw (fun _ h => endpWriteOffRead H.toSchedSRN P h) hS (H.run.step hS hb) + (H.rdEx.stepA hS hb), fun y hy => ?_⟩ + obtain ⟨x, hx⟩ := hS.tcbBack _ y hy + exact NoBlockESc.run_keep hb hx hy (hr x hx) + · rintro s σ u hRel ⟨H, hr⟩ _ + refine WP.spec_mono (schedule_tcb_exA H u (fun _ ht _ hx => (hRel.at_tcb ht hx).st)) ?_ + rintro s' ⟨hw, H'⟩ + have swB : MWrA (st := st) Z m σ s (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) := + Or.inr (Or.inl (H.node_sks H.sch.cpuCs (by simp [layout.node.SCHED_ACTION]))) + refine ⟨fun b hb hf => hw.mem_eq hb (fun eq => endpFootOffWrite H.toSchedSRN P hf (eq.symm ▸ swB)), H', + fun y hy => hr y (by rwa [stsAct_kheap] at hy)⟩ + · rintro s σ u x e r d ⟨H, hr⟩ hk hst + have hne : u ≠ specPtr t := fun h => by + subst h + have := hr x hk + rw [hst] at this + cases this + exact H.rdEx u x hne hk (Or.inr (Or.inr (Or.inr ⟨e, r, d, hst⟩))) + +theorem schedFootE0_srbA (t : U64) : + SchedFootE0A st Z.T E R Bf (SRBtA st Z m κ t) (fun σ s => MWrA (st := st) Z m σ s) := by + refine ⟨fun hz hk h => mwr_back rfl rfl hz hk h, ?_⟩ + rintro s σ u hRel ⟨H, -⟩ _ + refine WP.spec_mono (schedule_tcb_exA H u (fun _ ht _ hx => (hRel.at_tcb ht hx).st)) ?_ + rintro s' ⟨hw, -⟩ + have swB : MWrA (st := st) Z m σ s (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) := + Or.inr (Or.inl (H.node_sks H.sch.cpuCs (by simp [layout.node.SCHED_ACTION]))) + exact hw.mono fun b hb => hb ▸ swB + +/-- `timeoutScOpA_of_sched` for `SRBtA` (`SchedSRB.scObjBA`). -/ +theorem timeoutScOp_srbA (P : IpcSchedPlacementA Z.th N E R Bf) (t : U64) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) : + TimeoutScOpA st ar Z.T E R Bf (SRBtA st Z m κ t) + (fun u => ∀ c < fl4A64Platform.numNodes, specPtr u ≠ m.idleThreadOf c) := by + constructor + rintro s a σ u _ ⟨H, hr⟩ hu hni h0 + obtain ⟨x0, hx0, hp0⟩ := H.sch.tcb u hu + have hname : scNameA m (L.mem s (scA u)) = specPtr (L.mem s (scA u)) := + H.inv.scName_of_notIdle hx0 (by rw [hp0]; unfold optScA; rw [if_neg h0]; rfl) hni + have hc := H.rest.scMem u hu h0 + have hup := updateConsumedWith_genA (Rel := fun s' σ' => SchedSRB st Z m κ s' σ' (specPtr t)) H.toSchedSRN a hc + (fun hk hw hx' e1 e2 e3 e4 hcc => H.scObjBA hc hk hw hx' e1 e2 e3 e4 hcc) maxTicksToUs ticksToUs hmax (htu a) + simp only [schedContextUpdateConsumedWith_eq_l4v] at hup + refine WP.spec_mono hup ?_ + rintro r ⟨y, n, v, hky, hspec, _, hw, H'⟩ + have hwk : ∀ y2 n2, σ.kheap (specPtr (L.mem s (scA u))) = some (.schedContext y2 n2) → WritesIn st s r.2 + (fun b => (L.mem s (scA u)).val ≤ b.val ∧ b.val < (L.mem s (scA u)).val + 2 ^ (n2 + minSchedContextBits)) := by + intro y2 n2 h2 + rw [← hname, hky] at h2 + cases h2 + exact hw + let p := scNameA m (L.mem s (scA u)) + let σ' := updKSc σ p (.schedContext { y with scConsumed := v.bv } n) + have hkeep : ∀ q o, σ.kheap q = some o → (∀ z k, o ≠ .schedContext z k) → σ'.kheap q = some o := by + intro q o hq ho + have hne : q ≠ p := by + intro eq + change σ.kheap p = _ at hky + rw [eq, hky] at hq + cases hq + exact ho y n rfl + simp [σ', updKSc, hne, hq] + have hview : KView σ σ' := by + refine ⟨?_, ?_, ?_, rfl⟩ + · exact fun q z hz => hkeep q _ hz (by intro z k e; cases e) + · exact fun q z hz => ⟨z, hkeep q _ hz (by intro z k e; cases e), rfl⟩ + · exact fun q z hz => ⟨z, hkeep q _ hz (by intro z k e; cases e), rfl, rfl, rfl, rfl, rfl⟩ + refine ⟨σ', ?_, ?_, hview, ⟨H', fun z hz => ?_⟩, hwk⟩ + · rw [← hname]; exact hspec + · intro w hwA hwE + exact hw.2.2 w hwA (fun hwSc => endpFootOffWrite H.toSchedSRN P hwE + (Or.inl ⟨_, H.rest.scMem u hu h0, y, n, hky, hwSc⟩)) + · have hz0 : σ.kheap (specPtr t) = some (.tcb z) := by + by_cases e : specPtr t = p + · rw [e] at hz; simp [σ', updKSc] at hz + · simpa [σ', updKSc, e] using hz + exact hr z hz0 + +/-- `set_thread_state_blockA` (with `set_thread_state_block_fpA`'s write set) of the excepted thread `t` set +`Inactive`, from `SchedSRB … t` to `IpcSchedRelA`. -/ +theorem set_thread_state_blockBA (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) (s : S) + (σ : State Unit) (hR : EpStateRel Z.T E R Bf (L.mem s) σ) {t : U64} (H : SchedSRB st Z m κ s σ (specPtr t)) + (ht : t ∈ Z.T) {x : Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) + (hNQ : NotQueued σ (specPtr t)) : + sched.set_thread_state st s t sched.TS_INACTIVE ⦃ s' => + EpStateRel Z.T E R Bf (L.mem s') (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := .inactive })) (specPtr t)) ∧ + IpcSchedRelA st Z m κ s' (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := .inactive })) (specPtr t)) ∧ + WritesIn st s s' (fun b => b = stA t ∨ MWrA (st := st) Z m σ s b) ⦄ := by + let O := schedOpsEAOfPlacementA (st := st) (m := m) (κ := κ) P + have F : SchedFootEA st Z.T E R Bf (IpcSchedRelA st Z m κ) (fun σ s => MWrA (st := st) Z m σ s) := + schedFootEA_of_sched + unfold sched.set_thread_state + step with obj.Tcb.set_state.spec as ⟨s1, hs1⟩ + subst hs1 + obtain ⟨hR1, hW1, hS1⟩ := write_state_rel PE s σ hR ht hk hq .inactive (by decide) sched.TS_INACTIVE + (by unfold sched.TS_INACTIVE; rfl) + have hk1 : (updK σ (specPtr t) (.tcb { x with tcbState := .inactive })).kheap (specPtr t) = + some (.tcb { x with tcbState := .inactive }) := by rw [updK_kheap, Function.update_self] + have hSR1 : IpcSchedRelA st Z m κ (L.wr s (stA t) sched.TS_INACTIVE) + (updK σ (specPtr t) (.tcb { x with tcbState := .inactive })) := + ⟨Z.Q, Z.R, H.blockExA hW1 (fun _ h => endpWriteOffRead H.toSchedSRN P h) (endpSchedStep hS1) hNQ + (fun u x' y' h1 h2 hu => by + rw [updK_kheap, Function.update_of_ne hu, h1] at h2 + injection h2 with h2; injection h2 with h2; subst h2; exact ⟨rfl, id⟩) + hk1 (by simp [inClassRSc, runnable])⟩ + have hw1 : WritesIn st s (L.wr s (stA t) sched.TS_INACTIVE) (fun b => b = stA t) := + writesIn_wr (a_st (PE.tcb t ht)).1 + refine WP.spec_mono (WP.spec_and (O.scheduleTcb _ _ t hR1 hSR1 ht) (F.scheduleTcb _ _ t hR1 hSR1 ht)) ?_ + rintro s2 ⟨⟨hf, hS⟩, hw⟩ + exact ⟨hR1.frame PE hf (KView.stsAct' _ _), hS, + (WritesIn.trans (st := st) hw1 hw).mono fun b h => h.imp id (F.back (ssz_updK_tcb σ _ _) hw1.1)⟩ + +/-- The non-donating `reply_push` of the excepted thread `t`: the reply linked, `t` blocked on it, from `SRBtA … t` to +`IpcSchedRelA`. -/ +theorem reply_push_noDonate_BA (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) (s : S) (a : A) + (σ : State Unit) (hR : EpStateRel Z.T E R Bf (L.mem s) σ) {t d r : U64} (hSR : SRBtA st Z m κ t s σ) + (ht : t ∈ Z.T) (hd : d ∈ Z.T) (hr : r ∈ R) {x : Tcb} {xr : Reply} + (hk : σ.kheap (specPtr t) = some (.tcb x)) (hkr : σ.kheap (specPtr r) = some (.reply xr)) + (hEp : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) + (hQ : NotQueued σ (specPtr t)) : + ipc.reply_push st ar s a t d r false ⦃ out => out.2 = a ∧ ∃ σ', + σ' = stsAct (updK (updK σ (specPtr r) (.reply { xr with replyTcb := some (specPtr t) })) (specPtr t) + (.tcb { x with tcbState := .blockedOnReply (specPtr r) })) (specPtr t) ∧ + replyPush (Z := Unit) (specPtr t) (specPtr d) (specPtr r) false σ = ({((), σ')}, False) ∧ + EpStateRel Z.T E R Bf (L.mem out.1) σ' ∧ IpcSchedRelA st Z m κ out.1 σ' ∧ + WritesIn st s out.1 (fun b => b = rTcbA r ∨ b = stA t ∨ b = tReplyA t ∨ MWrA (st := st) Z m σ s b) ⦄ := by + let O := schedOpsEAOfPlacementA (st := st) (m := m) (κ := κ) P + have F : SchedFootEA st Z.T E R Bf (IpcSchedRelA st Z m κ) (fun σ s => MWrA (st := st) Z m σ s) := + schedFootEA_of_sched + have O0 := schedOpsE0_srbA (st := st) (m := m) (κ := κ) P t + obtain ⟨xd, hkd, _⟩ := hR.tcb d hd + have htr := kheap_ne hk hkr (by intro o e f; rw [← e] at f; cases f) + let σ1 := updK σ (specPtr r) (.reply { xr with replyTcb := some (specPtr t) }) + have hkt1 : σ1.kheap (specPtr t) = some (.tcb x) := by + simp [σ1, htr, hk] + have hEp1 : ∀ e ∈ E, ∀ z, σ1.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue := by + intro e he z hz + by_cases h : specPtr e = specPtr r + · simp [σ1, h] at hz + · exact hEp e he z (by simpa [σ1, h] using hz) + have hsp : replyPush (Z := Unit) (specPtr t) (specPtr d) (specPtr r) false σ = + ({((), stsAct (updK σ1 (specPtr t) (.tcb { x with tcbState := .blockedOnReply (specPtr r) })) (specPtr t))}, + False) := by + unfold replyPush + rw [nd_bind_one (getTcbObjRef_eq _ hk), nd_bind_one (getTcbObjRef_eq _ hkd), + nd_bind_one (setReplyTcb_eq _ hkr), nd_bind_one (setThreadState_eq _ hkt1)] + simp [Fl4Spec.NonDet.when, Fl4Spec.NonDet.pure, σ1] + unfold ipc.reply_push + refine WP.spec_bind (obj.Tcb.sc.spec (L := L) s t) ?_ + rintro sc _ + have haR := a_rtcb PE hr + have hW1 : WritesIn st s (L.wr s (rTcbA r) t) (fun b => b = rTcbA r) := writesIn_wr haR.1 + have hl : obj.Reply.set_tcb st r s t ⦃ s1 => s1 = L.wr s (rTcbA r) t ∧ + EpStateRel Z.T E R Bf (L.mem s1) σ1 ∧ SRBtA st Z m κ t s1 σ1 ⦄ := + WP.spec_mono (WP.spec_and (obj.Reply.set_tcb.spec (L := L) s r t) (linkReplyA O0 PE hR hSR ht hr hkr)) + fun _ h => ⟨h.1, h.2⟩ + refine WP.spec_bind hl ?_ + rintro s1 ⟨hs1, hR1, hS1⟩ + have hz1 : SSzA σ σ1 := ssz_updK_reply _ _ _ + have hk1 : L.ksb s1 = L.ksb s := by rw [hs1]; exact hW1.1 + have hw1 : WritesIn st s s1 (fun b => b = rTcbA r) := by rw [hs1]; exact hW1 + have htA := PE.tcb t ht + have hs := a_st htA + have hra := a_reply htA + have hb : sched.set_blocked_on_reply st s1 t r ⦃ s2 => + EpStateRel Z.T E R Bf (L.mem s2) + (stsAct (updK σ1 (specPtr t) (.tcb { x with tcbState := .blockedOnReply (specPtr r) })) (specPtr t)) ∧ + IpcSchedRelA st Z m κ s2 + (stsAct (updK σ1 (specPtr t) (.tcb { x with tcbState := .blockedOnReply (specPtr r) })) (specPtr t)) ∧ + WritesIn st s1 s2 (fun b => b = stA t ∨ b = tReplyA t ∨ MWrA (st := st) Z m σ1 s1 b) ⦄ := by + unfold sched.set_blocked_on_reply + refine WP.spec_bind (obj.Tcb.set_state.spec (L := L) s1 t _) ?_ + rintro s2 rfl + refine WP.spec_bind (obj.Tcb.set_reply_obj.wr (L := L) _ t r) ?_ + rintro s3 rfl + obtain ⟨hAgree, hFields, hst, hrep, hW⟩ := blockOnReplyWrites (st := st) PE (s := s1) (r := r) ht + have hwn : WritesIn st s1 (L.wr (L.wr s1 (stA t) sched.TS_BLOCKED_ON_REPLY) (tReplyA t) r) + (fun b => b = stA t ∨ b = tReplyA t) := + (WritesIn.trans (st := st) (writesIn_wr (st := st) (s := s1) (v := sched.TS_BLOCKED_ON_REPLY) hs.1) + (writesIn_wr (st := st) (v := r) hra.1)).mono fun _ h => h + let y : Tcb := { x with tcbState := .blockedOnReply (specPtr r) } + have hX : TcbRelE R (L.mem (L.wr (L.wr s1 (stA t) sched.TS_BLOCKED_ON_REPLY) (tReplyA t) r)) t y := by + refine (hR1.at_tcb ht hkt1).restateA (PE.tcb t ht) hFields rfl rfl rfl rfl hst ?_ ?_ ?_ ?_ + · intro e h + cases h + · intro e d h + cases h + · intro e r d h + cases h + · intro r' h + cases h + rw [hrep] + have hRel := hR1.upd_tcb PE ht y hAgree (Or.inl hEp1) hX + have hy : (updK σ1 (specPtr t) (.tcb y)).kheap (specPtr t) = some (.tcb y) := by + rw [updK_kheap, Function.update_self] + have hS : IpcSchedRelA st Z m κ (L.wr (L.wr s1 (stA t) sched.TS_BLOCKED_ON_REPLY) (tReplyA t) r) + (updK σ1 (specPtr t) (.tcb y)) := + ⟨Z.Q, Z.R, hS1.1.blockExA hW (fun _ h => endpWriteOffRead hS1.1.toSchedSRN P h) + (endpSchedStep (EpStep.tcb hkt1 y.tcbState x.tcbArch)) hQ + (fun p x' y' h0 h1 hp => by + rw [updK_kheap, Function.update_of_ne hp, h0] at h1 + cases h1 + exact ⟨rfl, id⟩) hy (by simp [y, inClassRSc, runnable])⟩ + refine WP.spec_mono (WP.spec_and (O.scheduleTcb _ _ _ hRel hS ht) (F.scheduleTcb _ _ _ hRel hS ht)) ?_ + rintro s4 ⟨⟨hf, hnew⟩, hw⟩ + refine ⟨hRel.frame PE hf (KView.stsAct' _ _), hnew, + (WritesIn.trans (st := st) hwn hw).mono fun b h => ?_⟩ + rcases h with (h | h) | h + · exact Or.inl h + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr (F.back (ssz_updK_tcb σ1 _ _) hwn.1 h)) + refine WP.spec_bind hb ?_ + rintro s2 ⟨hR2, hS2, hw2⟩ + have hw02 : WritesIn st s s2 (fun b => b = rTcbA r ∨ b = stA t ∨ b = tReplyA t ∨ MWrA (st := st) Z m σ s b) := + (WritesIn.trans (st := st) hw1 hw2).mono fun b h => by + rcases h with h | h | h | h + · exact Or.inl h + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr (Or.inl h)) + · exact Or.inr (Or.inr (Or.inr (F.back hz1 hk1 h))) + have done : ∀ out : S × A, out = (s2, a) → out.2 = a ∧ ∃ σ', + σ' = stsAct (updK σ1 (specPtr t) (.tcb { x with tcbState := .blockedOnReply (specPtr r) })) (specPtr t) ∧ + replyPush (Z := Unit) (specPtr t) (specPtr d) (specPtr r) false σ = ({((), σ')}, False) ∧ + EpStateRel Z.T E R Bf (L.mem out.1) σ' ∧ IpcSchedRelA st Z m κ out.1 σ' ∧ + WritesIn st s out.1 (fun b => b = rTcbA r ∨ b = stA t ∨ b = tReplyA t ∨ MWrA (st := st) Z m σ s b) := by + rintro _ rfl + exact ⟨rfl, _, rfl, hsp, hR2, hS2, hw02⟩ + refine WP.spec_bind (obj.Sc.is_null.spec sc) ?_ + rintro b hb + cases b + · simp only [Bool.false_eq_true, ↓reduceIte] + refine WP.spec_bind (obj.Tcb.sc.spec (L := L) s2 d) ?_ + rintro dsc _ + split_ifs <;> exact (WP.spec_ok _).2 (done _ rfl) + · exact (WP.spec_ok _).2 (done _ rfl) + +variable (st Z m R Bf) in +/-- The write set of the timeout fault send: `send_ipc_recv_fpA`'s (`SendFpA`, which has the reply object's thread +word) and the sender's reply word. -/ +def TmoFpA (σ : State Unit) (s : S) (t e d b : U64) : Prop := + SendFpA st Z.T R Bf (fun σ s => MWrA (st := st) Z m σ s) σ s t e d True b ∨ b = tReplyA t + +set_option maxHeartbeats 1000000 in +/-- **The timeout fault send to a waiting receiver, from `SchedSRB … t`** (module docstring): the timeout fault sender `t` is excepted from +`released_ipc_queues` until it blocks (reply push or `Inactive`). + +Case selectors: `hf` / `hsc0` (a timeout fault and an SC), `hni` (the SC is not an idle SC), `hon` (the receiver is +on the current core). [P] `hmax`, `htu`. -/ +theorem send_ipc_recv_tmoBA {s : S} {σ : State Unit} (a : A) + (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + {kb : U64} (hPl : PlaceA Z.th kb) (hSt : StateRelA Z.th (L.mem s) σ) + (hC : BufCover st Z.T Bf s σ) (hNative : BufNativeA Z.T (L.mem s)) + (hoff : ∀ y, IsSlotA Z.th y → ¬ EWrite Z.T E R Bf y ∧ + ¬ EWrite Z.T E R Bf (core.num.U64.wrapping_add y 8#u64)) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {t : U64} (H : SchedSRB st Z m κ s σ (specPtr t)) (ht : t ∈ Z.T) {xt : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hRun : SndRunA σ (specPtr t) xt) + {e d : U64} (he : e ∈ E) {q : List ObjRef} + (hk : σ.kheap (specPtr e) = some (.endpoint (.recvEP (specPtr d :: q)))) + (hd : d ∈ Z.T) {xd : Tcb} (hkd : σ.kheap (specPtr d) = some (.tcb xd)) + (hu : ∀ rp, ipcBufOfA true xd = some rp → ∀ k, k < 128 → inUserFrame (bufAddr rp k) σ) + (hrep : RepOKA σ (specPtr d)) (hon : L.mem s (affA d) = L.cpu s) + (badge : U64) (g gr blk : Bool) {bd : Word} (hf : xt.tcbFault = some (.timeout bd)) + (hni : ∀ c < fl4A64Platform.numNodes, specPtr t ≠ m.idleThreadOf c) (hsc0 : L.mem s (scA t) ≠ 0#u64) + (hsc : xt.tcbSchedContext = some (specPtr (L.mem s (scA t)))) {y : SchedContext} {n : Nat} + (hky : σ.kheap (specPtr (L.mem s (scA t))) = some (.schedContext y n)) : + endpoint.send_ipc st ar s a blk false badge g gr false t e ⦃ out => out.2 = a ∧ + ∃ σ', sendIpc (Z := Unit) blk false badge.bv g gr false (specPtr t) (specPtr e) σ = ({((), σ')}, False) ∧ + EpStateRel Z.T E R Bf (L.mem out.1) σ' ∧ IpcSchedRelA st Z m κ out.1 σ' ∧ + WritesIn st s out.1 (TmoFpA st Z m R Bf σ s t e d) ⦄ := by + let O := schedOpsEAOfPlacementA (st := st) (m := m) (κ := κ) P + have F : SchedFootEA st Z.T E R Bf (IpcSchedRelA st Z m κ) (fun σ s => MWrA (st := st) Z m σ s) := + schedFootEA_of_sched + have O0 := schedOpsE0_srbA (st := st) (m := m) (κ := κ) P t + have F0 := schedFootE0_srbA (st := st) (Z := Z) (m := m) (κ := κ) (E := E) (R := R) (Bf := Bf) t + have hTO := timeoutScOp_srbA (st := st) (ar := ar) (m := m) (κ := κ) P t hmax htu + have hSR : SRBtA st Z m κ t s σ := ⟨H, fun x hx => by rw [hkt] at hx; cases hx; exact hRun.1⟩ + have hscT : xt.tcbSchedContext = optScA m (L.mem s (scA t)) := by + obtain ⟨y0, hy0, hs0⟩ := H.sch.tcb t ht + exact (congrArg Tcb.tcbSchedContext (KernelObject.tcb.inj (Option.some.inj (hkt.symm.trans hy0)))).trans hs0 + have hnb : tsCode xt.tcbState ≠ 3 := (runnable_codeA hRun.1).1 + have hth := (H.thT t).2 ht + have hdh := (H.thT d).2 hd + obtain ⟨hst2, xs, hxs0, hq, hQ, hB⟩ := HE.at_ep he hk + obtain ⟨d', xs', rfl⟩ := List.exists_cons_of_ne_nil hxs0 + simp only [List.map_cons, List.cons.injEq] at hq + obtain ⟨hd', hq'⟩ := hq + have hdd : d = d' := specPtr_inj hd' + subst hdd + have hda := PE.tcb d hd + have hxd := HE.at_tcb hd hkd + have hxt := HE.at_tcb ht hkt + have hd3 : (L.mem s (stA d)).val = 3 := (hB d (by simp)).1 + have hdb : L.mem s (tBlockA d) = e := (hB d (by simp)).2 + obtain ⟨e', rep, dd, hstd⟩ := tsCode_three (hxd.st.symm.trans hd3) + have he' : e' = specPtr e := by + have := hxd.blk e' (by rw [hstd]; rfl); rw [hdb] at this; exact this.symm + subst he' + have htd : t ≠ d := by intro h; subst h; exact hnb (hxt.st.symm.trans hd3) + have hde : specPtr d ≠ specPtr e := kheap_ne hkd hk (fun _ h1 h2 => by subst h1; cases h2) + have hte : specPtr t ≠ specPtr e := kheap_ne hkt hk (fun _ h1 h2 => by subst h1; cases h2) + have htd' : specPtr t ≠ specPtr d := fun h => htd (specPtr_inj h) + have hdw : specPtr d ≠ specPtr (L.mem s (scA t)) := fun h => by rw [h, hky] at hkd; cases hkd + have htw : specPtr t ≠ specPtr (L.mem s (scA t)) := fun h => by rw [h, hky] at hkt; cases hkt + have hwe : specPtr (L.mem s (scA t)) ≠ specPtr e := fun h => by rw [h, hk] at hky; cases hky + unfold endpoint.send_ipc + step with obj.Ep.state.spec as ⟨st0, hst0⟩ + rw [epState_eq, hst2] at hst0 + subst hst0 + rw [if_neg (by rw [ep_state_vals.1]; decide), if_neg (by rw [ep_state_vals.2.1]; decide)] + step with endpoint.get_queue.spec as ⟨q0, hq0⟩ + have hhead : q0.head = d := by rw [hq0, epQ_eq]; exact hQ.head + rw [hhead] + -- the dequeue + have hdq : specPtr d ∈ (Endpoint.recvEP (specPtr d :: q)).epQueue := by simp [Endpoint.epQueue] + refine WP.spec_bind (WP.spec_and (ep_dequeue_raw PE s σ HE he hk hdq) + (ep_dequeue_full PE s he d (d :: xs') hQ (by simp))) ?_ + rintro s1 ⟨⟨-, hR1, hW1, hE1, hA1⟩, -, -, hQA, -⟩ + have hS1 := O0.stable _ _ _ _ hSR hW1 hE1 (NoBlockE.updO _ _ fun _ h => by cases h) + set σ1 := updK σ (specPtr e) (.endpoint (epDeq (.recvEP (specPtr d :: q)) (specPtr d))) with hσ1 + have hkt1 : σ1.kheap (specPtr t) = some (.tcb xt) := by rw [hσ1, updK_kheap, Function.update_of_ne hte, hkt] + have hkd1 : σ1.kheap (specPtr d) = some (.tcb xd) := by rw [hσ1, updK_kheap, Function.update_of_ne hde, hkd] + have hky1 : σ1.kheap (specPtr (L.mem s (scA t))) = some (.schedContext y n) := by + rw [hσ1, updK_kheap, Function.update_of_ne hwe, hky] + have hsc1 : L.mem s1 (scA t) = L.mem s (scA t) := + hW1.2.2 _ (field_aligned (PE.tcb t ht) (by simp) (by simp)) + (not_ew_offA PE ht (K := 3464#u64) (by simp) (by unfold TWOffN; simp) (by simp) (by simp)) + have hmem : ∀ z, IsSlotA Z.th z → L.mem s1 z = L.mem s z ∧ + L.mem s1 (core.num.U64.wrapping_add z 8#u64) = L.mem s (core.num.U64.wrapping_add z 8#u64) := by + intro z hz + have hal : Aligned z := by have := (IsSlotA.al32 hPl hz).1; unfold Aligned; omega + exact ⟨hW1.2.2 z hal (hoff z hz).1, hW1.2.2 _ (wa_al hal (by simp)) (hoff z hz).2⟩ + have hSt1 := hSt.updEpA hPl hmem hk (epDeq (.recvEP (specPtr d :: q)) (specPtr d)) + have hC1 : BufCover st Z.T Bf s1 σ1 := + BufCover.ewA PE hC hW1 fun u _ x hx => by + have hne : specPtr u ≠ specPtr e := fun h => by + rw [h, hσ1, updK_kheap, Function.update_self] at hx; cases hx + rwa [hσ1, updK_kheap, Function.update_of_ne hne] at hx + have hu1 : ∀ rp, ipcBufOfA true xd = some rp → ∀ k, k < 128 → inUserFrame (bufAddr rp k) σ1 := + fun rp hrp k hk' => inUserFrame_updK hk (fun _ h => by cases h) _ (hu rp hrp k hk') + -- the transfer + have hfr1 := (hR1.at_tcb ht hkt1).faultR + unfold FaultRel at hfr1 + rw [hf] at hfr1 + have htr : ipc.do_ipc_transfer st ar s1 a t e badge g d ⦃ s2 => ∃ σ2, + doIpcTransfer (Z := Unit) (specPtr t) (some (specPtr e)) badge.bv g (specPtr d) σ1 = ({((), σ2)}, False) ∧ + EpStateRel Z.T E R Bf (L.mem s2) σ2 ∧ SRBtA st Z m κ t s2 σ2 ∧ + (∃ s0, FootEq Z.T E R Bf (L.mem s1) (L.mem s0) ∧ WritesIn st s0 s2 (EWrite Z.T E R Bf) ∧ + WritesIn st s1 s0 (fun b => (L.mem s1 (scA t)).val ≤ b.val ∧ + b.val < (L.mem s1 (scA t)).val + 2 ^ (n + minSchedContextBits)) ∧ + Agree (L.mem s0) (L.mem s2) (fun b => ¬ RcvRegA d b ∧ ¬ BufW Bf b)) ∧ + (σ2.readyQueues = σ1.readyQueues ∧ σ2.releaseQueue = σ1.releaseQueue ∧ σ2.idleThread = σ1.idleThread ∧ + σ2.schedulerAction = σ1.schedulerAction) ∧ + (∃ y', σ2.kheap (specPtr (L.mem s1 (scA t))) = some (.schedContext y' n)) ∧ + (∀ p, p ≠ specPtr d → p ≠ specPtr (L.mem s1 (scA t)) → σ2.kheap p = σ1.kheap p) ∧ + ∃ x', σ2.kheap (specPtr d) = some (.tcb x') ∧ x' = { xd with tcbArch := x'.tcbArch } ⦄ := by + have hspec : doIpcTransfer (Z := Unit) (specPtr t) (some (specPtr e)) badge.bv g (specPtr d) σ1 = + doFaultTransfer (Z := Unit) badge.bv (specPtr t) (specPtr d) (ipcBufOfA true xd) σ1 := by + unfold doIpcTransfer + rw [nd_bind_one (lookupIpcBuffer_eqA hkd1 true), nd_bind_one (threadGet_eq _ hkt1), hf] + unfold ipc.do_ipc_transfer + refine WP.spec_bind (lookup_buf_full s1 σ1 hSt1 hdh hd hkd1 hC1 (hNative.ewA PE hW1) true) ?_ + rintro rbw ⟨hrw0, hrwB⟩ + step with obj.Tcb.fault_type.spec as ⟨ft, hft⟩ + have hft0 : ¬ ft = fl4_abi.consts.fault.NullFault := by + intro h + have : L.mem s1 (tFaultA t) = 0#u64 := by rw [← hft, h]; unfold fl4_abi.consts.fault.NullFault; rfl + rw [(hR1.at_tcb ht hkt1).fault0.1 this] at hf; cases hf + rw [if_neg hft0] + refine WP.spec_mono (do_fault_transfer_timeoutScA hTO O0 PE s1 a σ1 hR1 hS1 ht hd htd hkt1 hkd1 hf + (by rw [hsc1]; exact hsc) hni (by rw [hsc1]; exact hsc0) (by rw [hsc1]; exact hky1) + (buf_caseA hrw0 hrwB) badge) ?_ + rintro s2 ⟨σ2, h1, b1, b2, ⟨s0, c1, c2, c3, c4⟩, ⟨q1, q2, q3, q4⟩, b5, b6, b7⟩ + exact ⟨σ2, hspec.trans h1, b1, b2, ⟨s0, c1, c2, c3, c4⟩, q1, q2, q3, q4, b5, b6, b7⟩ + refine WP.spec_bind htr ?_ + rintro s2 ⟨σ2, hT, hR2, hS2, ⟨s0, -, hW2, hWsc, hAg⟩, ⟨hq1', hq2', hq3', hq4'⟩, ⟨y', hky2⟩, hfr2, x2, hkd2, hx2⟩ + rw [hsc1] at hky2 hfr2 hWsc + have hscF : ∀ b : U64, (L.mem s (scA t)).val ≤ b.val ∧ + b.val < (L.mem s (scA t)).val + 2 ^ (n + minSchedContextBits) → MWrA (st := st) Z m σ s b := by + intro b hb + have hname : scNameA m (L.mem s (scA t)) = specPtr (L.mem s (scA t)) := by + have h := hscT.symm.trans hsc + rw [optScA, if_neg hsc0] at h + exact Option.some.inj h + exact Or.inl ⟨_, H.rest.scMem t ht hsc0, y, n, by rw [hname]; exact hky, hb⟩ + have hfp : WritesIn st s s2 (TmoFpA st Z m R Bf σ s t e d) := by + refine ⟨hW2.1.trans (hWsc.1.trans hW1.1), hW2.2.1.trans (hWsc.2.1.trans hW1.2.1), fun b hb hn => ?_⟩ + have a1 : L.mem s2 b = L.mem s0 b := hAg b hb + ⟨fun h => hn (Or.inl (Or.inl (Or.inr (Or.inr (Or.inl h))))), + fun h => hn (Or.inl (Or.inl (Or.inr (Or.inr (Or.inr h)))))⟩ + have a2 : L.mem s0 b = L.mem s1 b := hWsc.2.2 b hb fun h => hn (Or.inl (Or.inr (Or.inr (Or.inr (Or.inr (hscF b h)))))) + have a3 : L.mem s1 b = L.mem s b := hQA b hb ⟨fun h => hn (Or.inl (Or.inl (Or.inl h))), + fun ⟨w, hw, hl⟩ => hn (Or.inl (Or.inl (Or.inr (Or.inl ⟨w, hQ.mem w hw, (hB w hw).2, hl⟩))))⟩ + exact a1.trans (a2.trans a3) + have hkt2 : σ2.kheap (specPtr t) = some (.tcb xt) := by rw [hfr2 _ htd' htw, hkt1] + have hst2' : x2.tcbState = .blockedOnReceive (specPtr e) rep dd := by rw [hx2]; exact hstd + have hdb1 : L.mem s1 (tBlockA d) = e := by rw [hA1 _ (a_blk hda).1 ⟨d, hd, (a_blk hda).2⟩, hdb] + have hk1e : σ1.kheap (specPtr e) = some (.endpoint (epDeq (.recvEP (specPtr d :: q)) (specPtr d))) := by + rw [hσ1, updK_kheap, Function.update_self] + have hq1 := dequeued_not_queued hR1 he hdb1 hk1e + (epDeq_not_mem (z := .recvEP (specPtr d :: q)) (xs := d :: xs') (by rw [hq']; rfl) hQ.nodup d) + have hq2 : ∀ e'' ∈ E, ∀ z, σ2.kheap (specPtr e'') = some (.endpoint z) → specPtr d ∉ z.epQueue := by + intro e'' he'' z hz + have hne : specPtr e'' ≠ specPtr d := by intro h; rw [h, hkd2] at hz; cases hz + have hnw : specPtr e'' ≠ specPtr (L.mem s (scA t)) := by intro h; rw [h, hky2] at hz; cases hz + exact hq1 e'' he'' z (by rwa [hfr2 _ hne hnw] at hz) + have hrr : ∀ r, rep = some r → ∃ xr, σ2.kheap r = some (.reply xr) ∧ xr.replyTcb = some (specPtr d) := by + intro r hr + obtain ⟨xr, hkr, hb⟩ := hrep xd hkd _ _ _ (by rw [hstd, hr]) + have hre : r ≠ specPtr e := fun h => by rw [h, hk] at hkr; cases hkr + have hrd : r ≠ specPtr d := fun h => by rw [h, hkd] at hkr; cases hkr + have hrw : r ≠ specPtr (L.mem s (scA t)) := fun h => by rw [h, hky] at hkr; cases hkr + exact ⟨xr, by rw [hfr2 _ hrd hrw, hσ1, updK_kheap, Function.update_of_ne hre, hkr], hb⟩ + have hK2 : SchedKeepA σ σ2 := + ⟨hq1'.trans rfl, hq2'.trans rfl, hq3'.trans rfl, Or.inl (hq4'.trans rfl)⟩ + have hz2 : SSzA σ σ2 := by + intro p z k hz + by_cases hpw : p = specPtr (L.mem s (scA t)) + · rw [hpw, hky2] at hz + cases hz + exact ⟨y, by rw [hpw]; exact hky⟩ + have hpd : p ≠ specPtr d := fun h => by rw [h, hkd2] at hz; cases hz + rw [hfr2 p hpd hpw, hσ1, updK_kheap, Function.update_apply] at hz + split_ifs at hz + · cases hz + · exact ⟨z, hz⟩ + -- the reply word and the unlink + change sendRecvAfterTransferA (st := st) (ar := ar) s2 a t d false g gr false ⦃ _ ⦄ + unfold sendRecvAfterTransferA + step with obj.Tcb.reply_obj.spec as ⟨i, hi⟩ + have hi0 : rep = none → i = 0#u64 := fun h => by + rw [hi]; exact optRef_none (((hR2.at_tcb hd hkd2).recv _ _ _ hst2').1.trans h) + refine WP.spec_bind (WP.spec_and + (send_recv_replyA O0 F0 PE hS2 hR2 hS2 (WritesIn.refl _ _) (EpStep.refl _) hd (fun _ _ => rfl) htd' hkt2 hkd2 hkd2 + hst2' hst2' hrr hq2 i hi) + (send_recv_reply_fpA O0 F0 PE hS2 hR2 (WritesIn.refl _ _) (EpStep.refl _) hd (fun _ _ => rfl) htd' hkd2 hkd2 + hst2' hst2' hrr hq2 i hi)) ?_ + rintro s3 ⟨⟨⟨σ3, x3, hR3, hS3, hU, hkt3, hkd3, hnr3, hni3, hcls3, hq3, hK3, hz3⟩, -⟩, hw3⟩ + have hzσ3 : SSzA σ σ3 := hz2.trans hz3 + have hw03 : WritesIn st s s3 (TmoFpA st Z m R Bf σ s t e d) := + (WritesIn.trans (st := st) hfp hw3).mono fun b h => by + rcases h with h | h | h | h + · exact h + · exact Or.inl (Or.inr (Or.inl h)) + · exact Or.inl (Or.inr (Or.inr (Or.inr (Or.inl h)))) + · exact Or.inl (Or.inr (Or.inr (Or.inr (Or.inr (mwr_back rfl rfl hz2 hfp.1 h))))) + have hRun3 := hRun.keep (hK2.trans hK3) + have hf0 : L.mem s3 (tFaultA t) ≠ fl4_abi.consts.fault.NullFault := by + intro h + unfold fl4_abi.consts.fault.NullFault at h + rw [(hR3.at_tcb ht hkt3).fault0.1 h] at hf; cases hf + have hfne : xt.tcbFault ≠ none := by rw [hf]; simp + by_cases hpush : (g = true ∨ gr = true) ∧ rep ≠ none + · -- the reply push + obtain ⟨hg, hrep0⟩ := hpush + obtain ⟨r0, hr0⟩ := Option.ne_none_iff_exists'.1 hrep0 + subst hr0 + obtain ⟨hrw, hrR⟩ := (hR2.at_tcb hd hkd2).recv _ _ _ hst2' + obtain ⟨hi00, hr0eq⟩ := optRef_some hrw + have hiR : i ∈ R := by rw [hi]; exact hrR (by simp) + have hr0i : r0 = specPtr i := by rw [hi]; exact hr0eq.symm + subst hr0i + have hine : i ≠ 0#u64 := by rw [hi]; exact hi00 + obtain ⟨xr, hkr, hback⟩ := hrr _ rfl + have hUe := replyUnlinkTcb_eq hkr hback hkd2 hst2' + have hU' : replyUnlinkTcb (Z := Unit) (specPtr d) (specPtr i) σ2 = ({((), σ3)}, False) := hU + have e3 := (nd_one_injA (hU'.symm.trans hUe)).2 + have hir : specPtr i ≠ specPtr t := kheap_ne hkr hkt2 (fun _ h1 h2 => by subst h1; cases h2) + have hid : specPtr i ≠ specPtr d := kheap_ne hkr hkd2 (fun _ h1 h2 => by subst h1; cases h2) + have hkr3 : σ3.kheap (specPtr i) = some (.reply { xr with replyTcb := none }) := by + rw [e3, stsAct_kheap, updK_kheap, Function.update_of_ne hid, updK_kheap, Function.update_self] + have hx3 : x3.tcbState = .inactive := by + have h := hkd3 + rw [e3, stsAct_kheap, updK_kheap, Function.update_self] at h + cases h + rfl + have hRelD : RelIfBound σ3 (specPtr d) := hcls3.resolve_left fun h => by + rw [hx3] at h + rcases h with h | ⟨_, h⟩ | ⟨_, _, h, _⟩ | ⟨_, _, _, h⟩ + · cases h + all_goals cases h + rw [sendRecvSelectedPushA s3 a t d i false g gr false hine hg (Or.inr hf0)] + have hEp := not_queued_codeA hR3 ht hkt3 (runnable_codeA hRun3.1) + refine WP.spec_bind (reply_push_noDonate_BA P PE s3 a σ3 hR3 hS3 ht hd hiR hkt3 hkr3 hEp hRun3.2.1) ?_ + rintro ⟨s4, a4⟩ ⟨ha4, σ4, e4, hp, hR4, hS4, hw4⟩ + dsimp only at ha4 hR4 hS4 hw4 ⊢ + subst a4 + have hdt : specPtr d ≠ specPtr t := htd'.symm + have hk4 : σ4.kheap (specPtr d) = some (.tcb x3) := by + rw [e4, stsAct_kheap, updK_kheap, Function.update_of_ne hdt, updK_kheap, Function.update_of_ne hid.symm, hkd3] + have hq4 : ∀ e ∈ E, ∀ z, σ4.kheap (specPtr e) = some (.endpoint z) → specPtr d ∉ z.epQueue := by + intro e'' he'' z hz + rw [e4, stsAct_kheap, updK_kheap, Function.update_apply] at hz + split_ifs at hz + · cases hz + rw [updK_kheap, Function.update_apply] at hz + split_ifs at hz + · cases hz + exact hq3 e'' he'' z hz + have hRel4 : RelIfBound σ4 (specPtr d) := by + refine RelIfBound.keep hRelD (by rw [e4, stsAct_releaseQueue]; rfl) (by rw [e4, stsAct_curTime]; rfl) + (fun x' hx' => ⟨x', by rwa [hk4, ← hkd3] at hx', rfl⟩) (fun p z k hz => ?_) + rw [e4, stsAct_kheap, updK_kheap, Function.update_apply] at hz + split_ifs at hz + · cases hz + rw [updK_kheap, Function.update_apply] at hz + split_ifs at hz + · cases hz + exact hz + have hz4 : SSzA σ σ4 := by + refine hzσ3.trans ?_ + rw [e4] + exact (ssz_updK_reply _ _ _).trans ((ssz_updK_tcb _ _ _).trans (ssz_stsAct _ _)) + have hnI : x3.tcbState ≠ .idleThreadState := by rw [hx3]; decide + have hnr4 : runnable x3.tcbState = false := by rw [hx3]; rfl + have hNB := NoBlockE.updT hk4 { x3 with tcbState := .running } (Or.inr (Or.inl ⟨rfl, hnI, Or.inr hRel4⟩)) + have hon4 : L.mem s4 (affA d) = L.cpu s4 := by + obtain ⟨Q4, R4, H4⟩ := hS4 + rw [H.toSchedSRN.aff_eqN H4.toSchedSRN hd hd, H.toSchedSRN.cpu_eqN H4.toSchedSRN, hon] + unfold wakeAfterReplyPushA + refine WP.spec_bind (WP.spec_and + (set_thread_state_relF O F PE s4 σ4 hR4 hS4 hd hk4 hq4 .running (by decide) sched.TS_RUNNING + (by unfold sched.TS_RUNNING; rfl) hNB) + (set_thread_state_fpA O.to0 F.to0 PE hS4 s4 σ4 hR4 (WritesIn.refl _ _) (EpStep.refl _) hd hk4 hq4 .running + (by decide) sched.TS_RUNNING (by unfold sched.TS_RUNNING; rfl) hNB)) ?_ + rintro s5 ⟨⟨hR5, hS5, -⟩, hw5⟩ + have hz5 : SSzA σ (stsAct (updK σ4 (specPtr d) (.tcb { x3 with tcbState := .running })) (specPtr d)) := + hz4.trans ((ssz_updK_tcb _ _ _).trans (ssz_stsAct _ _)) + refine WP.spec_mono (WP.spec_and + (send_tail_fromA O F PE a hS4 hd hon4 hS4 hk4 hnr4 hq4 hR5 hS5 (FpA.refl _ _) hz5) + (send_tail_fpA O F a hS4 hd hon4 hS4 hk4 hnr4 hq4 hR5 hS5)) ?_ + rintro out ⟨⟨ha, hnf, σ', hm, hR', hS', -⟩, hw6⟩ + have spec : sendIpc (Z := Unit) blk false badge.bv g gr false (specPtr t) (specPtr e) σ = + specSendTailA (specPtr d) (stsAct (updK σ4 (specPtr d) (.tcb { x3 with tcbState := .running })) + (specPtr d)) := by + rw [sendIpc_recv_pushSpecA hk hkd hde hstd blk false badge.bv g gr false hT hU' hkt3 hkd3 (Or.inr hfne) hg, + nd_bind_one hp, nd_bind_one (setThreadState_eq _ hk4)] + refine ⟨ha, σ', by rw [spec]; exact nd_singleton_of_detA ((det_specSendTailA _).at _) hnf hm, hR', hS', ?_⟩ + have w4 : WritesIn st s s4 (TmoFpA st Z m R Bf σ s t e d) := + (WritesIn.trans (st := st) hw03 hw4).mono fun b h => by + rcases h with h | h | h | h | h + · exact h + · exact Or.inl (Or.inr (Or.inl ⟨i, hiR, h⟩)) + · exact Or.inl (Or.inr (Or.inr (Or.inl ⟨trivial, h⟩))) + · exact Or.inr h + · exact Or.inl (Or.inr (Or.inr (Or.inr (Or.inr (mwr_back rfl rfl hzσ3 hw03.1 h))))) + have w5 : WritesIn st s s5 (TmoFpA st Z m R Bf σ s t e d) := + (WritesIn.trans (st := st) w4 hw5).mono fun b h => by + rcases h with h | h | h + · exact h + · exact Or.inl (Or.inr (Or.inr (Or.inr (Or.inl h)))) + · exact Or.inl (Or.inr (Or.inr (Or.inr (Or.inr (mwr_back rfl rfl hz4 w4.1 h))))) + exact (WritesIn.trans (st := st) w5 hw6).mono fun b h => h.elim id fun h => + Or.inl (Or.inr (Or.inr (Or.inr (Or.inr (mwr_back rfl rfl hz5 w5.1 h))))) + · -- no push: the sender becomes inactive + have hnp' : (g = false ∧ gr = false) ∨ rep = none := by + by_cases hr : rep = none + · exact Or.inr hr + · refine Or.inl ?_ + cases g with + | true => exact absurd ⟨Or.inl rfl, hr⟩ hpush + | false => + cases gr with + | true => exact absurd ⟨Or.inr rfl, hr⟩ hpush + | false => exact ⟨rfl, rfl⟩ + have spec := sendIpc_recv_blockA hk hkd hde hstd blk false badge.bv g gr false hT hU hkt3 hkd3 htd' + (Or.inr hfne) hnp' + have htail : (do + let (s4, a1) ← (do + let s5 ← sched.set_thread_state st s3 t sched.TS_INACTIVE + ok (s5, a)) + let s5 ← sched.set_thread_state st s4 d sched.TS_RUNNING + let s6 ← endpoint.unblock_sporadic st s5 d + let s7 ← sched.possible_switch_to st s6 d + ok (s7, a1)) ⦃ out => out.2 = a ∧ + ∃ σ', sendIpc (Z := Unit) blk false badge.bv g gr false (specPtr t) (specPtr e) σ = ({((), σ')}, False) ∧ + EpStateRel Z.T E R Bf (L.mem out.1) σ' ∧ IpcSchedRelA st Z m κ out.1 σ' ∧ + WritesIn st s out.1 (TmoFpA st Z m R Bf σ s t e d) ⦄ := by + rw [spec] + have hdt' : specPtr d ≠ specPtr t := htd'.symm + have hqt := not_queued_codeA hR3 ht hkt3 (runnable_codeA hRun3.1) + have hA : (do + let s5 ← sched.set_thread_state st s3 t sched.TS_INACTIVE + ok (s5, a)) ⦃ r => r.2 = a ∧ EpStateRel Z.T E R Bf (L.mem r.1) + (stsAct (updK σ3 (specPtr t) (.tcb { xt with tcbState := .inactive })) (specPtr t)) ∧ + IpcSchedRelA st Z m κ r.1 + (stsAct (updK σ3 (specPtr t) (.tcb { xt with tcbState := .inactive })) (specPtr t)) ∧ + WritesIn st s3 r.1 (fun b => b = stA t ∨ MWrA (st := st) Z m σ3 s3 b) ⦄ := + WP.spec_bind (set_thread_state_blockBA (m := m) (κ := κ) P PE s3 σ3 hR3 hS3.1 ht hkt3 hqt hRun3.2.1) + fun s5 h5 => by rw [WP.spec_ok]; exact ⟨rfl, h5⟩ + refine WP.spec_bind hA ?_ + rintro ⟨s4, a1⟩ ⟨ha1, hR4, hS4, hw4⟩ + dsimp only at ha1 hR4 hS4 hw4 ⊢ + subst ha1 + have hk4 : (stsAct (updK σ3 (specPtr t) (.tcb { xt with tcbState := .inactive })) (specPtr t)).kheap + (specPtr d) = some (.tcb x3) := by rw [stsAct_kheap, updK_kheap, Function.update_of_ne hdt', hkd3] + have hq4 : ∀ e ∈ E, ∀ z, (stsAct (updK σ3 (specPtr t) (.tcb { xt with tcbState := .inactive })) + (specPtr t)).kheap (specPtr e) = some (.endpoint z) → specPtr d ∉ z.epQueue := by + intro e he z hz + rw [stsAct_kheap, updK_kheap] at hz + by_cases hne : specPtr e = specPtr t + · rw [hne, Function.update_self] at hz; cases hz + · rw [Function.update_of_ne hne] at hz; exact hq3 e he z hz + have hcls4 : inClassR x3.tcbState x3.tcbFault ∨ RelIfBound (stsAct (updK σ3 (specPtr t) + (.tcb { xt with tcbState := .inactive })) (specPtr t)) (specPtr d) := + hcls3.imp id fun h => RelIfBound.keep h (by rw [stsAct_releaseQueue]; rfl) (by rw [stsAct_curTime]; rfl) + (fun x' hx' => ⟨x', by rwa [stsAct_kheap, updK_kheap, Function.update_of_ne hdt'] at hx', rfl⟩) + (fun q y n hq' => by + rw [stsAct_kheap, updK_kheap, Function.update_apply] at hq' + split_ifs at hq' <;> first | exact hq' | cases hq') + have hNB := NoBlockE.updT (σ := stsAct (updK σ3 (specPtr t) (.tcb { xt with tcbState := .inactive })) + (specPtr t)) hk4 { x3 with tcbState := .running } (Or.inr (Or.inl ⟨rfl, hni3, hcls4⟩)) + have hz4 : SSzA σ3 (stsAct (updK σ3 (specPtr t) (.tcb { xt with tcbState := .inactive })) (specPtr t)) := + (ssz_updK_tcb _ _ _).trans (ssz_stsAct _ _) + have hon4 : L.mem s4 (affA d) = L.cpu s4 := by + obtain ⟨Q4, R4, H4⟩ := hS4 + rw [H.toSchedSRN.aff_eqN H4.toSchedSRN hd hd, H.toSchedSRN.cpu_eqN H4.toSchedSRN, hon] + refine WP.spec_bind (WP.spec_and + (set_thread_state_relF O F PE s4 _ hR4 hS4 hd hk4 hq4 .running (by decide) + sched.TS_RUNNING (by unfold sched.TS_RUNNING; rfl) hNB) + (set_thread_state_fpA O.to0 F.to0 PE hS4 s4 _ hR4 (WritesIn.refl _ _) (EpStep.refl _) hd hk4 hq4 .running + (by decide) sched.TS_RUNNING (by unfold sched.TS_RUNNING; rfl) hNB)) ?_ + rintro s5 ⟨⟨hR5, hS5, -⟩, hw5⟩ + have hz5 : SSzA (stsAct (updK σ3 (specPtr t) (.tcb { xt with tcbState := .inactive })) (specPtr t)) + (stsAct (updK (stsAct (updK σ3 (specPtr t) (.tcb { xt with tcbState := .inactive })) (specPtr t)) + (specPtr d) (.tcb { x3 with tcbState := .running })) (specPtr d)) := + (ssz_updK_tcb _ _ _).trans (ssz_stsAct _ _) + refine WP.spec_mono (WP.spec_and + (send_tail_fromA O F PE a1 hS4 hd hon4 hS4 hk4 hnr3 hq4 hR5 hS5 (FpA.refl _ _) hz5) + (send_tail_fpA O F a1 hS4 hd hon4 hS4 hk4 hnr3 hq4 hR5 hS5)) ?_ + rintro out ⟨⟨ha, hnf, σ', hm, hR', hS', -⟩, hw6⟩ + refine ⟨ha, σ', nd_singleton_of_detA ((det_specSendTailA _).at _) hnf hm, hR', hS', ?_⟩ + have w4 : WritesIn st s s4 (TmoFpA st Z m R Bf σ s t e d) := + (WritesIn.trans (st := st) hw03 hw4).mono fun b h => by + rcases h with h | h | h + · exact h + · exact Or.inl (Or.inr (Or.inr (Or.inl ⟨trivial, h⟩))) + · exact Or.inl (Or.inr (Or.inr (Or.inr (Or.inr (mwr_back rfl rfl hzσ3 hw03.1 h))))) + have w5 : WritesIn st s s5 (TmoFpA st Z m R Bf σ s t e d) := + (WritesIn.trans (st := st) w4 hw5).mono fun b h => by + rcases h with h | h | h + · exact h + · exact Or.inl (Or.inr (Or.inr (Or.inr (Or.inl h)))) + · exact Or.inl (Or.inr (Or.inr (Or.inr (Or.inr (mwr_back rfl rfl (hzσ3.trans hz4) w4.1 h))))) + exact (WritesIn.trans (st := st) w5 hw6).mono fun b h => h.elim id fun h => + Or.inl (Or.inr (Or.inr (Or.inr (Or.inr (mwr_back rfl rfl ((hzσ3.trans hz4).trans hz5) w5.1 h))))) + unfold sendRecvAfterUnlinkA + simp only [Bool.false_eq_true, ↓reduceIte] + step with obj.Tcb.fault_type.spec as ⟨i1, hi1⟩ + have hb1 : (i1 != fl4_abi.consts.fault.NullFault) = true := by rw [bne_iff_ne, hi1]; exact hf0 + simp only [hb1, ↓reduceIte] + rcases hnp' with ⟨rfl, rfl⟩ | hrn + · simp only [Bool.false_eq_true, ↓reduceIte, ite_self] + exact htail + · simp only [hi0 hrn, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + exact htail + + +end + +end + +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/EpSetMrA.lean b/proofs/Fl4ProofsA64/P3/EpSetMrA.lean new file mode 100644 index 000000000..755c5393b --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/EpSetMrA.lean @@ -0,0 +1,245 @@ +/- +P3 (aarch64, A64-P3-IPC gen 7, ruling item 2): **FL4's `set_mr` against the spec's `set_mrs`, both branches** (x86 +`P3/EpFault`'s `set_mr_rel` / `MrInv` / `set_mr_inv` and `P3/EpXferBuf`'s `EpStateRel.upd_buf`, ported to the A64 +`EpStateRel` / `EpBufA` shapes; FL4's AArch64 `msg::set_mr` / `msg::buf_set` are the x86 text, extraction diffed). + +* Spec side: `mrState_snoc_lt` / `mrState_snoc_ge` (one more message word changes one inline register, or one buffer + word `bufAddr p (k + 1)`: `mrRegs_snoc_*`, `mrPairs_snoc_*`, `mrMem_snoc`), `mrState_kheap`, `EpStep.setUmA`. +* **`EpStateRel.upd_buf`**: FL4's write of a tracked buffer word (`BufW Bf`) and the spec's store of the user word it + stands for keep the relation (`EPlaced.binj` / `balign` / `be` / `br` / `bt`, `load_store_same` / `_other`). +* FL4: `buf_set_eq`; **`set_mr_buf`** (the buffer branch, `4 ≤ i`, `buf ≠ 0`: buffer word `i + 1`) and + `set_mr_nobuf` (no buffer: nothing written, `N_MSG_REGISTERS` returned). +* **`set_mr_step`** (x86 `set_mr_rel`): the next message word `v` of `ms` (`i = |ms| < 120`) to a receiver whose IPC + buffer is tracked (`bp ∈ Bf`, `bp.1 ≠ 0`), from `mrState σ0 rcv x0 bp.2 ms` to `mrState σ0 rcv x0 bp.2 (ms ++ [v])`, + on `EpStateRel ∧ SR` (`SchedOpsEA.stable`: `NoBlockE` — a register or user-memory write changes no thread state), + nothing else written but the receiver's registers and the buffer words (`RcvRegA`, `BufW`); **`MrInvA`** / + **`set_mr_inv`** for callers writing a whole message (`set_mrs_fault`, `reply1`, `copy_mrs`). + +Hypotheses: `EPlaced`, `rcv ∈ T`, `bp ∈ Bf` (the receiver's buffer, placement as gen 2's `Bf`), the length bound; the +interface `SchedOpsEA`. No named premise. `EpBufA` / `BufCover` untouched. +-/ +import Fl4ProofsA64.P3.EpBufA +import Fl4ProofsA64.P3.SchedOpsA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif + +noncomputable section + +/-! ## The spec's `set_mrs`, one word at a time -/ + +section Spec +open Fl4SpecA64 + +theorem setReg_withRegsA (x : Tcb) (R : Register → Word) (r : Register) (v : Word) : + setReg (withRegs x R) r v = withRegs x (Function.update R r v) := rfl + +theorem setUm_setUmA (σ : State Unit) (a b : Word → Word8) : setUm (setUm σ a) b = setUm σ b := rfl + +theorem setUm_umA (σ : State Unit) (a : Word → Word8) : (setUm σ a).machineState.underlyingMemory = a := rfl + +/-- The receiver in `mrState`. -/ +theorem mrState_kheap (σ : State Unit) (rcv : ObjRef) (x : Tcb) (p : ObjRef) (ms : List Word) : + (mrState σ rcv x p ms).kheap rcv = some (.tcb (withRegs x (mrRegs x.tcbArch.tcbContext.userRegs ms))) := by + show (updK σ rcv _).kheap rcv = _ + rw [updK_kheap, Function.update_self] + +/-- One more word, on an inline register (x86 `mrState_snoc_lt`). -/ +theorem mrState_snoc_lt (σ : State Unit) (rcv : ObjRef) (x : Tcb) (p : ObjRef) (ms : List Word) (v : Word) (k : Nat) + (hk : k = ms.length) (h : ms.length < 4) : + updK (mrState σ rcv x p ms) rcv (.tcb (setReg (withRegs x (mrRegs x.tcbArch.tcbContext.userRegs ms)) (mrIdx k) v)) = + mrState σ rcv x p (ms ++ [v]) := by + subst hk + unfold mrState + rw [mrPairs_snoc_lt _ _ h, mrRegs_snoc_lt _ _ _ h, setReg_withRegsA] + simp only [updK, setUm, Function.update_idem] + +/-- One more word, in the buffer (x86 `mrState_snoc_ge`). -/ +theorem mrState_snoc_ge (σ : State Unit) (rcv : ObjRef) (x : Tcb) (p : ObjRef) (ms : List Word) (v : Word) + (h : 4 ≤ ms.length) (h' : ms.length < 120) : + setUm (mrState σ rcv x p ms) (storeMem (mrState σ rcv x p ms).machineState.underlyingMemory + (bufAddr p (ms.length + 1)) v) = mrState σ rcv x p (ms ++ [v]) := by + unfold mrState + rw [mrPairs_snoc_ge _ _ h h', mrRegs_snoc_ge _ _ _ h, mrMem_snoc, setUm_umA, setUm_setUmA] + +/-- Storing user memory is an endpoint step (x86 `EpStep.setUm'`). -/ +theorem EpStep.setUmA (σ : State Unit) (um : Word → Word8) : EpStep σ (setUm σ um) := + ⟨rfl, rfl, fun _ => Or.inl rfl⟩ + +/-- Storing user memory blocks no thread. -/ +theorem NoBlockE.setUmA (σ : State Unit) (um : Word → Word8) : NoBlockE σ (setUm σ um) := + NoBlockE.of fun p x y h1 h2 => by + change σ.kheap p = _ at h2 + rw [h1] at h2 + injection h2 with h2; injection h2 with h2 + subst h2; exact ⟨rfl, rfl⟩ + +end Spec + +/-! ## A buffer word on both sides -/ + +section Buf +open Fl4SpecA64 +variable {T E R : Set U64} {Bf : Set (U64 × Word)} + +/-- An endpoint, reply or TCB word of the relation is no buffer word. -/ +theorem not_bufW (hP : EPlaced T E R Bf) {a : U64} + (ha : (∃ e ∈ E, a.val = e.val ∨ a.val = e.val + 8) ∨ (∃ r ∈ R, a.val = r.val) ∨ (∃ t ∈ T, TF t a ∨ TL t a)) : + ¬ BufW Bf a := by + intro hw + rcases ha with ⟨e, he, h⟩ | ⟨r, hr, h⟩ | ⟨t, ht, h | h⟩ + · have := hP.be a hw e he; omega + · have := hP.br a hw r hr; omega + · have := h.range; have := hP.bt a hw t ht; omega + · have := hP.bt a hw t ht; unfold TL at h; omega + +/-- **Writing a tracked buffer word** on both sides keeps the relation (x86 `EpStateRel.upd_buf`). -/ +theorem EpStateRel.upd_buf {m m' : U64 → U64} {σ : State Unit} (hR : EpStateRel T E R Bf m σ) + (hP : EPlaced T E R Bf) {bp : U64 × Word} (hb : bp ∈ Bf) {k : U64} (hk : k.val < 128) (v : U64) + (hA : Agree m m' (fun a => a ≠ bufW bp.1 k)) (hv : m' (bufW bp.1 k) = v) : + EpStateRel T E R Bf m' (setUm σ (storeMem σ.machineState.underlyingMemory (bufAddr bp.2 k.val) v.bv)) := by + have hw : BufW Bf (bufW bp.1 k) := ⟨bp, hb, k, hk, rfl⟩ + have hm : ∀ a, Aligned a → + ((∃ e ∈ E, a.val = e.val ∨ a.val = e.val + 8) ∨ (∃ r ∈ R, a.val = r.val) ∨ (∃ t ∈ T, TF t a ∨ TL t a)) → + m' a = m a := fun a ha hf => hA a ha fun e => not_bufW hP hf (e ▸ hw) + refine ⟨fun e he => ?_, fun r hr => ?_, fun t ht => ?_, fun bq hq k' hk' => ?_⟩ + · obtain ⟨x, hkx, hx⟩ := hR.ep e he + refine ⟨x, hkx, hx.frame (hm _ (a_head hP he).1 (Or.inl ⟨e, he, Or.inl (val_head e)⟩)) + (hm _ (a_ts hP he).1 (Or.inl ⟨e, he, Or.inr (val_ts hP he)⟩)) fun y hy _ => ?_⟩ + have hya := hP.tcb y hy + exact ⟨hm _ (a_st hya).1 (Or.inr (Or.inr ⟨y, hy, Or.inl (a_st hya).2⟩)), + hm _ (a_blk hya).1 (Or.inr (Or.inr ⟨y, hy, Or.inl (a_blk hya).2⟩)), + hm _ (a_prev hya).1 (Or.inr (Or.inr ⟨y, hy, Or.inr (a_prev hya).2⟩)), + hm _ (a_next hya).1 (Or.inr (Or.inr ⟨y, hy, Or.inr (a_next hya).2⟩))⟩ + · obtain ⟨x, hkx, hx⟩ := hR.reply r hr + exact ⟨x, hkx, by rw [hx, hm _ (a_rtcb hP hr).1 (Or.inr (Or.inl ⟨r, hr, rTcbA_val r⟩))]⟩ + · obtain ⟨x, hkx, hx⟩ := hR.tcb t ht + exact ⟨x, hkx, hx.frame (hP.tcb t ht) fun a ha hf => hm a ha (Or.inr (Or.inr ⟨t, ht, Or.inl hf⟩))⟩ + · refine ⟨?_, (hR.buf bq hq k' hk').2⟩ + show (m' (bufW bq.1 k')).bv = + loadWordAt (storeMem σ.machineState.underlyingMemory (bufAddr bp.2 k.val) v.bv) (bufAddr bq.2 k'.val) + by_cases e : bufW bq.1 k' = bufW bp.1 k + · have e2 := (hP.binj bq hq bp hb k' k hk' hk).1 e + rw [e, e2, hv, load_store_same] + · have e2 : bufAddr bq.2 k'.val ≠ bufAddr bp.2 k.val := fun h => e ((hP.binj bq hq bp hb k' k hk' hk).2 h) + rw [hA _ (hP.balign _ ⟨bq, hq, k', hk', rfl⟩) e, + load_store_other _ _ (hR.buf bp hb k hk).2 (hR.buf bq hq k' hk').2 e2] + exact (hR.buf bq hq k' hk').1 + +end Buf + +/-! ## FL4's `set_mr` -/ + +section Fl4 +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +theorem buf_set_eq (s : S) (w i v : U64) : msg.buf_set st s w i v = ok (L.wr s (bufW w i) v) := by + unfold msg.buf_set bufW + simp only [lift, bind_ok, L.write_eq] + +/-- **`set_mr` past the inline registers, with an IPC buffer** (`4 ≤ i`, `buf ≠ 0`): buffer word `i + 1`. -/ +theorem set_mr_buf (s : S) (t buf i v : U64) (hi : 4 ≤ i.val) (hb : buf ≠ 0#u64) : + msg.set_mr st s t buf i v = ok (core.num.U64.wrapping_add i 1#u64, + L.wr s (bufW buf (core.num.U64.wrapping_add i 1#u64)) v) := by + have hN : msg.N_MSG_REGISTERS.val = 4 := by unfold msg.N_MSG_REGISTERS; rfl + have h4 : ¬ i < msg.N_MSG_REGISTERS := by scalar_tac + have hb' : (buf != 0#u64) = true := by simpa using hb + unfold msg.set_mr + rw [if_neg h4, if_pos hb'] + simp only [lift, bind_ok, buf_set_eq] + +/-- `set_mr` past the inline registers without an IPC buffer: nothing written. -/ +theorem set_mr_nobuf (s : S) (t i v : U64) (hi : 4 ≤ i.val) : + msg.set_mr st s t 0#u64 i v = ok (msg.N_MSG_REGISTERS, s) := by + have hN : msg.N_MSG_REGISTERS.val = 4 := by unfold msg.N_MSG_REGISTERS; rfl + have h4 : ¬ i < msg.N_MSG_REGISTERS := by scalar_tac + unfold msg.set_mr + rw [if_neg h4] + simp + +/-- A register word of the receiver at `rcv` (x86 `RcvReg`). -/ +def RcvRegA (rcv b : U64) : Prop := ∃ r, b.val = (regA rcv (regIdxA r)).val + +variable {T E R : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {SR : S → Fl4SpecA64.State Unit → Prop} + +/-- **`set_mr` of the next message word** (x86 `set_mr_rel`): from `mrState … ms` to `mrState … (ms ++ [v])`, both +branches (inline register for `|ms| < 4`, buffer word `|ms| + 1` after), `SR` kept, only the receiver's registers and +the buffer words written. -/ +theorem set_mr_step (hO : SchedOpsE0A st T E R Bf SR) (hP : EPlaced T E R Bf) (s : S) (σ0 : Fl4SpecA64.State Unit) + {rcv : U64} (hr : rcv ∈ T) (x0 : Fl4SpecA64.Tcb) {bp : U64 × Fl4SpecA64.Word} (hb : bp ∈ Bf) (hb0 : bp.1 ≠ 0#u64) + (ms : List Fl4SpecA64.Word) (hR : EpStateRel T E R Bf (L.mem s) (mrState σ0 (specPtr rcv) x0 bp.2 ms)) + (hSR : SR s (mrState σ0 (specPtr rcv) x0 bp.2 ms)) (i : U64) (hi : i.val = ms.length) (hi' : i.val < 120) + (v : U64) : + msg.set_mr st s rcv bp.1 i v ⦃ r => r.1.val = i.val + 1 ∧ + EpStateRel T E R Bf (L.mem r.2) (mrState σ0 (specPtr rcv) x0 bp.2 (ms ++ [v.bv])) ∧ + SR r.2 (mrState σ0 (specPtr rcv) x0 bp.2 (ms ++ [v.bv])) ∧ + Agree (L.mem s) (L.mem r.2) (fun b => ¬ RcvRegA rcv b ∧ ¬ BufW Bf b) ⦄ := by + have hra := hP.tcb rcv hr + have hks := mrState_kheap σ0 (specPtr rcv) x0 bp.2 ms + have hiv : (core.num.U64.wrapping_add i 1#u64).val = i.val + 1 := by + simp [core.num.U64.wrapping_add_val_eq, U64.size_def, U64.numBits]; omega + by_cases h4 : i.val < 4 + · rw [set_mr_reg s rcv bp.1 i v h4, WP.spec_ok] + obtain ⟨hR1, hW1, hS1⟩ := write_reg_rel hP s _ hR hr hks (mrIdx i.val) v + have hSR1 := hO.stable _ _ _ _ hSR hW1 hS1 (NoBlockE.tcbK hks _ rfl rfl) + rw [mrState_snoc_lt _ _ _ _ _ _ i.val hi (by omega)] at hR1 hSR1 + have ha := a_reg hra (mrIdx i.val) + exact ⟨hiv, hR1, hSR1, fun b hb' hn => agree_wr (L := L) (s := s) (v := v) ha.1 b hb' (fun h => hn.1 ⟨_, h⟩)⟩ + · rw [set_mr_buf s rcv bp.1 i v (by omega) hb0, WP.spec_ok] + have hk : (core.num.U64.wrapping_add i 1#u64).val < 128 := by omega + have hw : BufW Bf (bufW bp.1 (core.num.U64.wrapping_add i 1#u64)) := ⟨bp, hb, _, hk, rfl⟩ + have hal := hP.balign _ hw + have hag := agree_wr (L := L) (s := s) (v := v) hal + have hR1 := hR.upd_buf hP hb hk v (fun b hb' hne => hag b hb' (val_ne_of_ne hne)) (rd_wr_eq hal) + have hW : WritesIn st s (L.wr s (bufW bp.1 (core.num.U64.wrapping_add i 1#u64)) v) (EWrite T E R Bf) := + (writesIn_wr hal).mono fun b e => by rw [e]; exact Or.inr (Or.inr (Or.inr hw)) + have hS1 := hO.stable _ _ _ _ hSR hW (EpStep.setUmA _ _) (NoBlockE.setUmA _ + (storeMem (mrState σ0 (specPtr rcv) x0 bp.2 ms).machineState.underlyingMemory + (bufAddr bp.2 (core.num.U64.wrapping_add i 1#u64).val) v.bv)) + rw [hiv, hi] at hR1 hS1 + rw [mrState_snoc_ge _ _ _ _ _ _ (by omega) (by omega)] at hR1 hS1 + exact ⟨hiv, hR1, hS1, fun b hb' hn => hag b hb' (fun h => hn.2 (by rw [UScalar.eq_of_val_eq h]; exact hw))⟩ + +/-- The invariant of a message being written (x86 `MrInv`): related to `mrState` of the words written so far, `SR`, +and nothing changed but the receiver's registers and the buffer words. -/ +def MrInvA (T E R : Set U64) (Bf : Set (U64 × Fl4SpecA64.Word)) (SR : S → Fl4SpecA64.State Unit → Prop) (s0 : S) + (σ0 : Fl4SpecA64.State Unit) (rcv : U64) (x0 : Fl4SpecA64.Tcb) (p : Fl4SpecA64.ObjRef) (s : S) + (ms : List Fl4SpecA64.Word) : Prop := + EpStateRel T E R Bf (L.mem s) (mrState σ0 (specPtr rcv) x0 p ms) ∧ SR s (mrState σ0 (specPtr rcv) x0 p ms) ∧ + Agree (L.mem s0) (L.mem s) (fun b => ¬ RcvRegA rcv b ∧ ¬ BufW Bf b) + +/-- `set_mr` keeps `MrInvA` with one more word (x86 `set_mr_inv`). -/ +theorem set_mr_inv (hO : SchedOpsE0A st T E R Bf SR) (hP : EPlaced T E R Bf) {s0 : S} {σ0 : Fl4SpecA64.State Unit} + {rcv : U64} (hr : rcv ∈ T) {x0 : Fl4SpecA64.Tcb} {bp : U64 × Fl4SpecA64.Word} (hb : bp ∈ Bf) + (hb0 : bp.1 ≠ 0#u64) {s : S} {ms : List Fl4SpecA64.Word} + (hI : MrInvA (L := L) T E R Bf SR s0 σ0 rcv x0 bp.2 s ms) (i : U64) (hi : i.val = ms.length) + (hi' : i.val < 120) (v : U64) : + msg.set_mr st s rcv bp.1 i v ⦃ r => r.1.val = i.val + 1 ∧ + MrInvA (L := L) T E R Bf SR s0 σ0 rcv x0 bp.2 r.2 (ms ++ [v.bv]) ⦄ := + WP.spec_mono (set_mr_step hO hP s σ0 hr x0 hb hb0 ms hI.1 hI.2.1 i hi hi' v) fun _ ⟨h1, h2, h3, h4⟩ => + ⟨h1, h2, h3, fun b hb' hn => (h4 b hb' hn).trans (hI.2.2 b hb' hn)⟩ + +/-- `MrInvA` with no word written yet, from a receiver at `σ0` (`mrState … [] = σ0`, x86 `mrState_nil`). -/ +theorem mrState_nil {σ : Fl4SpecA64.State Unit} {rcv : Fl4SpecA64.ObjRef} {x : Fl4SpecA64.Tcb} + (hk : σ.kheap rcv = some (.tcb x)) (p : Fl4SpecA64.ObjRef) : mrState σ rcv x p [] = σ := by + have h1 : mrRegs x.tcbArch.tcbContext.userRegs [] = x.tcbArch.tcbContext.userRegs := by + funext reg; simp [mrRegs] + have h2 : withRegs x x.tcbArch.tcbContext.userRegs = x := rfl + have h3 : updK σ rcv (.tcb x) = σ := by + unfold updK + rw [show Function.update σ.kheap rcv (some (.tcb x)) = σ.kheap from by rw [← hk, Function.update_eq_self]] + unfold mrState + rw [h1, h2, h3] + rfl + +theorem MrInvA.nil {s : S} {σ : Fl4SpecA64.State Unit} {rcv : U64} {x : Fl4SpecA64.Tcb} + (hk : σ.kheap (specPtr rcv) = some (.tcb x)) (p : Fl4SpecA64.ObjRef) (hR : EpStateRel T E R Bf (L.mem s) σ) + (hSR : SR s σ) : MrInvA (L := L) T E R Bf SR s σ rcv x p s [] := by + refine ⟨?_, ?_, fun _ _ _ => rfl⟩ <;> rw [mrState_nil hk p] <;> assumption + +end Fl4 + +end + +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/EpSetMrsNA.lean b/proofs/Fl4ProofsA64/P3/EpSetMrsNA.lean new file mode 100644 index 000000000..a2b2786fc --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/EpSetMrsNA.lean @@ -0,0 +1,176 @@ +/- +P3 (aarch64, A64-P3-IPC gen 9, ruling item 2): **messages into a receiver without an IPC buffer** (x86 +`P3/EpFaultNone`'s `mrStateN` / `MrInvN` / `set_mr_invN`, ported to the A64 `EpStateRel` shapes) and **l4v +`do_fault_transfer`** with a given fault message (x86 `EpFault.doFaultTransfer_eq` / `EpFaultNone.doFaultTransfer_eqN`). + +* Spec side: `mrStateNA` (the state after l4v `set_mrs rcv None ms`: the first four words to the message registers, + the rest dropped), **`setMrs_eqNA`** (one outcome, length `min 4 |ms|`), `mrStateNA_nil`, `mrRegs_append_geA`, + `mrStateNA_append_ge`, `mrStateNA_snoc_lt`; **`doFaultTransfer_eqNA`** (no receive buffer) and + **`doFaultTransfer_eqA`** (with one, on `EpBufA.setMrs_eq`'s `mrState`), each from `make_fault_msg`'s outcome. +* FL4: **`MrInvNA`** (x86 `MrInvN`: related to `mrStateNA` of the words written so far, `SR`, nothing changed but the + receiver's registers and the tracked buffer words), `MrInvNA.nil`, `MrInvNA.append_ge`, **`set_mr_invNA`** (FL4's + `set_mr` with `buf = 0`: an inline register for `i < 4`, nothing past them, the count `min (i + 1) 4`). + +The callers (`set_mrs_fault`'s per-fault messages, `do_fault_transfer`) take `MrInvNA` (no buffer) or gen 7's `MrInvA` +(a tracked buffer) as their loop invariant. Hypotheses: `EPlaced`, `rcv ∈ T`, the length bound; the interface +`SchedOpsEA`. No named premise. +-/ +import Fl4ProofsA64.P3.EpXferA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif + +noncomputable section + +/-! ## The spec's `set_mrs rcv None` and `do_fault_transfer` -/ + +section Spec +open Fl4SpecA64 + +/-- The state after `set_mrs rcv None ms` (x86 `mrStateN`). -/ +def mrStateNA (σ : State Unit) (rcv : ObjRef) (x : Tcb) (ms : List Word) : State Unit := + updK σ rcv (.tcb (withRegs x (mrRegs x.tcbArch.tcbContext.userRegs ms))) + +/-- **`set_mrs` without an IPC buffer** (x86 `setMrs_eqN`): one outcome, no failure. -/ +theorem setMrs_eqNA {σ : State Unit} {rcv : ObjRef} {x : Tcb} (hk : σ.kheap rcv = some (.tcb x)) + (ms : List Word) : + setMrs (Z := Unit) rcv none ms σ = ({(natToLen (min 4 ms.length), mrStateNA σ rcv x ms)}, False) := by + unfold setMrs + rw [nd_bind_one (getTcb_eq hk), nd_pure_bind', nd_pure_bind'] + erw [nd_bind_one (setObject_eq (o' := .tcb (withRegs x (mrRegs x.tcbArch.tcbContext.userRegs ms))) hk rfl)] + rw [nd_pure_bind'] + rfl + +theorem mrStateNA_kheap (σ : State Unit) (rcv : ObjRef) (x : Tcb) (ms : List Word) : + (mrStateNA σ rcv x ms).kheap rcv = some (.tcb (withRegs x (mrRegs x.tcbArch.tcbContext.userRegs ms))) := by + unfold mrStateNA; rw [updK_kheap, Function.update_self] + +theorem mrStateNA_nil {σ : State Unit} {rcv : ObjRef} {x : Tcb} (hk : σ.kheap rcv = some (.tcb x)) : + mrStateNA σ rcv x [] = σ := by + have h1 : mrRegs x.tcbArch.tcbContext.userRegs [] = x.tcbArch.tcbContext.userRegs := by + funext reg; simp [mrRegs] + have h2 : withRegs x x.tcbArch.tcbContext.userRegs = x := rfl + unfold mrStateNA + rw [h1, h2, updK_selfA hk] + +theorem mrRegs_append_geA (ctx : Register → Word) (ms : List Word) (h : 4 ≤ ms.length) : + ∀ ys : List Word, mrRegs ctx (ms ++ ys) = mrRegs ctx ms := by + intro ys + induction ys using List.reverseRecOn with + | nil => simp + | append_singleton ys y ih => + rw [← List.append_assoc, mrRegs_snoc_ge _ _ _ (by simp; omega), ih] + +theorem mrStateNA_append_ge (σ : State Unit) (rcv : ObjRef) (x : Tcb) (ms ys : List Word) (h : 4 ≤ ms.length) : + mrStateNA σ rcv x (ms ++ ys) = mrStateNA σ rcv x ms := by + unfold mrStateNA; rw [mrRegs_append_geA _ _ h] + +theorem mrStateNA_snoc_lt (σ : State Unit) (rcv : ObjRef) (x : Tcb) (ms : List Word) (v : Word) (k : Nat) + (hk : k = ms.length) (h : ms.length < 4) : + updK (mrStateNA σ rcv x ms) rcv + (.tcb (setReg (withRegs x (mrRegs x.tcbArch.tcbContext.userRegs ms)) (mrIdx k) v)) = + mrStateNA σ rcv x (ms ++ [v]) := by + subst hk + unfold mrStateNA + rw [mrRegs_snoc_lt _ _ _ h, setReg_withRegsA] + simp only [updK, Function.update_idem] + +/-- **`do_fault_transfer` into a receiver without an IPC buffer**, given the fault message (x86 +`doFaultTransfer_eqN`). -/ +theorem doFaultTransfer_eqNA {σ σm : State Unit} {snd rcv : ObjRef} {xs xr : Tcb} + (hks : σ.kheap snd = some (.tcb xs)) (hkr : σm.kheap rcv = some (.tcb xr)) {f : Fault} + (hf : xs.tcbFault = some f) {label : Data} {ms : List Word} + (hmsg : makeFaultMsg (Z := Unit) f snd σ = ({((label, ms), σm)}, False)) (badge : Data) : + doFaultTransfer (Z := Unit) badge snd rcv none σ = + ({((), updK (mrStateNA σm rcv xr ms) rcv (.tcb (setReg (setReg + (withRegs xr (mrRegs xr.tcbArch.tcbContext.userRegs ms)) msg_info_register + (messageInfoToData ⟨natToLen (min 4 ms.length), 0, 0, label⟩)) badge_register badge)))}, False) := by + unfold doFaultTransfer + rw [nd_bind_one (threadGet_eq _ hks), hf] + dsimp only + rw [nd_pure_bind', nd_bind_one hmsg] + dsimp only + rw [nd_bind_one (setMrs_eqNA hkr ms)] + unfold setMessageInfo + rw [nd_bind_one (asUser_setRegister_eq _ _ (mrStateNA_kheap σm rcv xr ms)), asUser_setRegister_eq (x := setReg + (withRegs xr (mrRegs xr.tcbArch.tcbContext.userRegs ms)) msg_info_register + (messageInfoToData ⟨natToLen (min 4 ms.length), 0, 0, label⟩)) _ _ + (by simp only [updK_kheap, Function.update_self])] + simp only [updK, Function.update_idem] + +/-- **`do_fault_transfer` into a receiver with an IPC buffer**, given the fault message (x86 `doFaultTransfer_eq`). -/ +theorem doFaultTransfer_eqA {σ σm : State Unit} {snd rcv : ObjRef} {xs xr : Tcb} + (hks : σ.kheap snd = some (.tcb xs)) (hkr : σm.kheap rcv = some (.tcb xr)) {f : Fault} + (hf : xs.tcbFault = some f) {label : Data} {ms : List Word} + (hmsg : makeFaultMsg (Z := Unit) f snd σ = ({((label, ms), σm)}, False)) (p : ObjRef) + (hal : ∀ q ∈ mrPairs ms, (bufAddr p q.1).toNat % 8 = 0 ∧ inUserFrame (bufAddr p q.1) σm) (badge : Data) : + doFaultTransfer (Z := Unit) badge snd rcv (some p) σ = + ({((), updK (mrState σm rcv xr p ms) rcv (.tcb (setReg (setReg + (withRegs xr (mrRegs xr.tcbArch.tcbContext.userRegs ms)) msg_info_register + (messageInfoToData ⟨natToLen (min ms.length msgMaxLength), 0, 0, label⟩)) badge_register badge)))}, False) := by + unfold doFaultTransfer + rw [nd_bind_one (threadGet_eq _ hks), hf] + dsimp only + rw [nd_pure_bind', nd_bind_one hmsg] + dsimp only + rw [nd_bind_one (setMrs_eq hkr p ms hal)] + unfold setMessageInfo + rw [nd_bind_one (asUser_setRegister_eq _ _ (mrState_kheap σm rcv xr p ms)), asUser_setRegister_eq (x := setReg + (withRegs xr (mrRegs xr.tcbArch.tcbContext.userRegs ms)) msg_info_register + (messageInfoToData ⟨natToLen (min ms.length msgMaxLength), 0, 0, label⟩)) _ _ + (by simp only [updK_kheap, Function.update_self])] + simp only [updK, Function.update_idem] + +end Spec + +/-! ## FL4's `set_mr` without a buffer -/ + +section Fl4 +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {T E R : Set U64} + {Bf : Set (U64 × Fl4SpecA64.Word)} {SR : S → Fl4SpecA64.State Unit → Prop} + +/-- The invariant of a message being written to a receiver without a buffer (x86 `MrInvN`). -/ +def MrInvNA (T E R : Set U64) (Bf : Set (U64 × Fl4SpecA64.Word)) (SR : S → Fl4SpecA64.State Unit → Prop) (s0 : S) + (σ0 : Fl4SpecA64.State Unit) (rcv : U64) (x0 : Fl4SpecA64.Tcb) (s : S) (ms : List Fl4SpecA64.Word) : Prop := + EpStateRel T E R Bf (L.mem s) (mrStateNA σ0 (specPtr rcv) x0 ms) ∧ SR s (mrStateNA σ0 (specPtr rcv) x0 ms) ∧ + Agree (L.mem s0) (L.mem s) (fun b => ¬ RcvRegA rcv b ∧ ¬ BufW Bf b) + +theorem MrInvNA.nil {s0 : S} {σ0 : Fl4SpecA64.State Unit} (hR : EpStateRel T E R Bf (L.mem s0) σ0) + (hSR : SR s0 σ0) {rcv : U64} {x0 : Fl4SpecA64.Tcb} (hk0 : σ0.kheap (specPtr rcv) = some (.tcb x0)) : + MrInvNA (L := L) T E R Bf SR s0 σ0 rcv x0 s0 [] := by + unfold MrInvNA; rw [mrStateNA_nil hk0]; exact ⟨hR, hSR, Agree.rfl'⟩ + +theorem MrInvNA.append_ge {s0 : S} {σ0 : Fl4SpecA64.State Unit} {rcv : U64} {x0 : Fl4SpecA64.Tcb} {s : S} + {ms : List Fl4SpecA64.Word} (hI : MrInvNA (L := L) T E R Bf SR s0 σ0 rcv x0 s ms) (h : 4 ≤ ms.length) + (ys : List Fl4SpecA64.Word) : MrInvNA (L := L) T E R Bf SR s0 σ0 rcv x0 s (ms ++ ys) := by + unfold MrInvNA at hI ⊢; rw [mrStateNA_append_ge _ _ _ _ _ h]; exact hI + +/-- **`set_mr` of the next message word, without a buffer** (x86 `set_mr_invN`): an inline register for `i < 4`, +nothing written past them; the count is `min (i + 1) 4`. -/ +theorem set_mr_invNA (hO : SchedOpsE0A st T E R Bf SR) (hP : EPlaced T E R Bf) {s0 : S} + {σ0 : Fl4SpecA64.State Unit} {rcv : U64} (hr : rcv ∈ T) {x0 : Fl4SpecA64.Tcb} {s : S} + {ms : List Fl4SpecA64.Word} (hI : MrInvNA (L := L) T E R Bf SR s0 σ0 rcv x0 s ms) (i : U64) + (hi : i.val = ms.length) (v : U64) : + msg.set_mr st s rcv 0#u64 i v ⦃ r => r.1.val = min (i.val + 1) 4 ∧ + MrInvNA (L := L) T E R Bf SR s0 σ0 rcv x0 r.2 (ms ++ [v.bv]) ⦄ := by + have hra := hP.tcb rcv hr + by_cases h4 : i.val < 4 + · rw [set_mr_reg s rcv 0#u64 i v h4, WP.spec_ok] + have hks := mrStateNA_kheap σ0 (specPtr rcv) x0 ms + obtain ⟨hR1, hW1, hS1⟩ := write_reg_rel hP s _ hI.1 hr hks (mrIdx i.val) v + have hSR1 := hO.stable _ _ _ _ hI.2.1 hW1 hS1 (NoBlockE.tcbK hks _ rfl rfl) + rw [mrStateNA_snoc_lt _ _ _ _ _ i.val hi (by omega)] at hR1 hSR1 + have ha := a_reg hra (mrIdx i.val) + refine ⟨?_, hR1, hSR1, fun b hb hn => + (agree_wr (L := L) (s := s) (v := v) ha.1 b hb (fun h => hn.1 ⟨_, h⟩)).trans (hI.2.2 b hb hn)⟩ + simp [core.num.U64.wrapping_add_val_eq, U64.size_def, U64.numBits]; omega + · rw [set_mr_nobuf s rcv i v (by omega), WP.spec_ok] + have hN : msg.N_MSG_REGISTERS.val = 4 := by unfold msg.N_MSG_REGISTERS; rfl + exact ⟨by rw [hN]; omega, hI.append_ge (by omega) _⟩ + +end Fl4 + +end + +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/EpSpecA.lean b/proofs/Fl4ProofsA64/P3/EpSpecA.lean new file mode 100644 index 000000000..e8bbb2d75 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/EpSpecA.lean @@ -0,0 +1,126 @@ +/- +P3 (aarch64, A64-P3-IPC gen 2, ported from x86 `P3/EpSpec`), endpoints: the spec's accessors on related objects. + +On a state where `p` is an endpoint (resp. a reply object, a TCB), `get_endpoint`, `set_endpoint`, `get_reply`, +`set_reply_obj_ref reply_tcb_update` and `tcb_ep_dequeue` of a member have exactly one outcome and do not fail; the +lemmas here compute it (`Fl4SpecA64`, the x86 text). x86's `tcbEpAppend_eq` (it needs the ordered-insertion lemma +`tcbAppend_eq` of x86 `NtfnBlock`) comes with the A64 port of `NtfnBlock` (gen 3). +-/ +import Fl4ProofsA64.P3.EpRelA +import Fl4ProofsA64.P3.NtfnSpecA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open Fl4SpecA64 Fl4Spec Classical Fl4ProofsA64.P3.Notif + +noncomputable section + +variable {Z : Type} + +theorem partialInv_ep (x : Endpoint) : partialInv KernelObject.endpoint (.endpoint x) = some x := by + have hu : ∃! y, KernelObject.endpoint y = .endpoint x := ⟨x, rfl, fun y h => by cases h; rfl⟩ + unfold partialInv The + simp only [hu, ↓reduceIte, ↓reduceDIte] + have h := (Classical.choose_spec hu).1 + injection h with h + rw [h] + +theorem partialInv_reply (x : Reply) : partialInv KernelObject.reply (.reply x) = some x := by + have hu : ∃! y, KernelObject.reply y = .reply x := ⟨x, rfl, fun y h => by cases h; rfl⟩ + unfold partialInv The + simp only [hu, ↓reduceIte, ↓reduceDIte] + have h := (Classical.choose_spec hu).1 + injection h with h + rw [h] + +theorem getEndpoint_eq {σ : State Z} {p : ObjRef} {x : Endpoint} + (hk : σ.kheap p = some (.endpoint x)) : getEndpoint (Z := Z) p σ = ({(x, σ)}, False) := by + show getSimpleKo KernelObject.endpoint p σ = _ + unfold getSimpleKo + show (getObject p >>= _) σ = _ + rw [nd_bind_one (getObject_eq hk)] + show (NonDet.assert _ >>= _) σ = _ + rw [nd_bind_one (show NonDet.assert (σ := State Z) (isSimpleType (.endpoint x)) σ = ({((), σ)}, False) by + simp [NonDet.assert, isSimpleType, aType])] + simp only [partialInv_ep]; rfl + +theorem setEndpoint_eq {σ : State Z} {p : ObjRef} {x : Endpoint} (y : Endpoint) + (hk : σ.kheap p = some (.endpoint x)) : + setEndpoint (Z := Z) p y σ = ({((), updK σ p (.endpoint y))}, False) := by + show setSimpleKo KernelObject.endpoint p y σ = _ + unfold setSimpleKo + show (getObject p >>= _) σ = _ + rw [nd_bind_one (getObject_eq hk)] + show (NonDet.assert _ >>= _) σ = _ + rw [nd_bind_one (show NonDet.assert (σ := State Z) (isSimpleType (.endpoint x)) σ = ({((), σ)}, False) by + simp [NonDet.assert, isSimpleType, aType])] + show (NonDet.assert _ >>= _) σ = _ + rw [nd_bind_one (show NonDet.assert (σ := State Z) + (partialInv KernelObject.endpoint (.endpoint x) ≠ none) σ = ({((), σ)}, False) by + simp [NonDet.assert, partialInv_ep])] + exact setObject_eq hk rfl + +theorem getReply_eq {σ : State Z} {p : ObjRef} {x : Reply} + (hk : σ.kheap p = some (.reply x)) : getReply (Z := Z) p σ = ({(x, σ)}, False) := by + show getSimpleKo KernelObject.reply p σ = _ + unfold getSimpleKo + show (getObject p >>= _) σ = _ + rw [nd_bind_one (getObject_eq hk)] + show (NonDet.assert _ >>= _) σ = _ + rw [nd_bind_one (show NonDet.assert (σ := State Z) (isSimpleType (.reply x)) σ = ({((), σ)}, False) by + simp [NonDet.assert, isSimpleType, aType])] + simp only [partialInv_reply]; rfl + +theorem setReply_eq {σ : State Z} {p : ObjRef} {x : Reply} (y : Reply) + (hk : σ.kheap p = some (.reply x)) : + setReply (Z := Z) p y σ = ({((), updK σ p (.reply y))}, False) := by + show setSimpleKo KernelObject.reply p y σ = _ + unfold setSimpleKo + show (getObject p >>= _) σ = _ + rw [nd_bind_one (getObject_eq hk)] + show (NonDet.assert _ >>= _) σ = _ + rw [nd_bind_one (show NonDet.assert (σ := State Z) (isSimpleType (.reply x)) σ = ({((), σ)}, False) by + simp [NonDet.assert, isSimpleType, aType])] + show (NonDet.assert _ >>= _) σ = _ + rw [nd_bind_one (show NonDet.assert (σ := State Z) + (partialInv KernelObject.reply (.reply x) ≠ none) σ = ({((), σ)}, False) by + simp [NonDet.assert, partialInv_reply])] + exact setObject_eq hk rfl + +theorem getReplyTcb_eq {σ : State Z} {p : ObjRef} {x : Reply} + (hk : σ.kheap p = some (.reply x)) : getReplyTcb (Z := Z) p σ = ({(x.replyTcb, σ)}, False) := by + show (NonDet.liftM _ _) σ = _ + unfold NonDet.liftM + rw [nd_bind_one' (getReply_eq hk)]; rfl + +/-- `set_reply_obj_ref reply_tcb_update`. -/ +theorem setReplyTcb_eq {σ : State Z} {p : ObjRef} {x : Reply} (v : Option ObjRef) + (hk : σ.kheap p = some (.reply x)) : + setReplyObjRef (Z := Z) replyTcbUpdate p v σ = ({((), updK σ p (.reply { x with replyTcb := v }))}, False) := by + show updateSkObjRef KernelObject.reply replyTcbUpdate p v σ = _ + unfold updateSkObjRef + rw [nd_bind_one (getReply_eq hk)] + exact setReply_eq _ hk + +theorem getTcbObjRef_eq {σ : State Z} {p : ObjRef} {x : Tcb} (f : Tcb → Option ObjRef) + (hk : σ.kheap p = some (.tcb x)) : getTcbObjRef (Z := Z) f p σ = ({(f x, σ)}, False) := threadGet_eq f hk + +/-- The endpoint after the dequeue of `t` (l4v `tcb_ep_dequeue`). -/ +def epDeq (x : Endpoint) (t : ObjRef) : Endpoint := + match removeAll t x.epQueue with + | [] => .idleEP + | _ => updateEpQueue x (removeAll t x.epQueue) true + +/-- `tcb_ep_dequeue` of a member. -/ +theorem tcbEpDequeue_eq {σ : State Z} {p t : ObjRef} {x : Endpoint} + (hk : σ.kheap p = some (.endpoint x)) (ht : t ∈ x.epQueue) : + tcbEpDequeue (Z := Z) t p σ = ({((), updK σ p (.endpoint (epDeq x t)))}, False) := by + unfold tcbEpDequeue + rw [nd_bind_one (getEndpoint_eq hk), nd_pure_bind', + nd_bind_one (show NonDet.assert (σ := State Z) (t ∈ x.epQueue) σ = ({((), σ)}, False) by + simp [NonDet.assert, ht]), nd_pure_bind', nd_pure_bind'] + exact setEndpoint_eq _ hk + +end + +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/EpWriteA.lean b/proofs/Fl4ProofsA64/P3/EpWriteA.lean new file mode 100644 index 000000000..f4dd8611d --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/EpWriteA.lean @@ -0,0 +1,317 @@ +/- +P3 (aarch64, A64-P3-IPC gen 2, ported from x86 `P3/EpWrite`), endpoints: updating the relation. + +`EpStateRel.upd_tcb`, `upd_ep`, `upd_reply`: replacing one spec object (at `specPtr p`) keeps the relation when +FL4's memory agrees on the footprint words the other objects read (`EPlaced`: TCBs, endpoints, replies and buffer +words do not overlap). Single word writes give that agreement (`agree_wr`). The spec-step lemmas `EpStep.trans`, +`.ep`, `.reply`, `.tcb`. Changes from x86: `specPtr` naming, the buffer clause per word of `BufW`. +-/ +import Fl4ProofsA64.P3.EpSpecA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif + +noncomputable section + +section +variable {T E R : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + +/-! ## Disjointness -/ + +theorem val_ne_of_ne {x y : U64} (h : x ≠ y) : x.val ≠ y.val := fun e => h (UScalar.eq_of_val_eq e) + +theorem tcb_disj (hP : EPlaced T E R Bf) {t y : U64} (ht : t ∈ T) (hy : y ∈ T) (hne : y ≠ t) : + y.val + 4096 ≤ t.val ∨ t.val + 4096 ≤ y.val := by + have h1 := (hP.tcb t ht).align + have h2 := (hP.tcb y hy).align + have := val_ne_of_ne hne + omega + +theorem ep_disj (hP : EPlaced T E R Bf) {e e' : U64} (he : e ∈ E) (he' : e' ∈ E) (hne : e' ≠ e) : + e'.val + 16 ≤ e.val ∨ e.val + 16 ≤ e'.val := by + have h1 := (hP.ep e he).1 + have h2 := (hP.ep e' he').1 + have := val_ne_of_ne hne + omega + +theorem rep_disj (hP : EPlaced T E R Bf) {r r' : U64} (hr : r ∈ R) (hr' : r' ∈ R) (hne : r' ≠ r) : + r'.val + 32 ≤ r.val ∨ r.val + 32 ≤ r'.val := by + have h1 := (hP.rep r hr).1 + have h2 := (hP.rep r' hr').1 + have := val_ne_of_ne hne + omega + +theorem TF.range {t a : U64} (h : TF t a) : t.val + 256 ≤ a.val ∧ a.val ≤ t.val + 3448 := by + obtain ⟨h1, h2⟩ := h + unfold TOffN at h2; omega + +theorem TWF.tf {t a : U64} (h : TWF t a) : TF t a := by + obtain ⟨h1, h2⟩ := h + refine ⟨h1, ?_⟩ + unfold TWOffN at h2; unfold TOffN; omega + +theorem TF.of_val {t a b : U64} (h : TF t a) (e : b.val = a.val) : TF t b := by + obtain ⟨h1, h2⟩ := h; exact ⟨e ▸ h1, e ▸ h2⟩ + +theorem TL.not_tf {t a : U64} (h : TL t a) : ¬ TF t a := by + rintro ⟨h1, h2⟩; unfold TOffN at h2; unfold TL at h; omega + +/-- A single write agrees on the words at other addresses. -/ +theorem agree_wr {S : Type _} {st : machine.Store S} [L : LawfulStore st] {s : S} {a v : U64} (ha : Aligned a) : + Agree (L.mem s) (L.mem (L.wr s a v)) (fun b => b.val ≠ a.val) := + fun _ hb hne => L.mem_wr_ne ha hb (Ne.symm hne) + +/-! ## Replacing a TCB -/ + +section Upd +open Fl4SpecA64 +variable {Z : Type} + +/-- The words another object of the relation reads are not fields of the TCB at `t`. -/ +theorem notTF_ep (hP : EPlaced T E R Bf) {t e a : U64} (ht : t ∈ T) (he : e ∈ E) + (ha : a.val = e.val ∨ a.val = e.val + 8) : ¬ TF t a := by + intro h; have := h.range; have := hP.et e he t ht; omega +theorem notTF_rep (hP : EPlaced T E R Bf) {t r a : U64} (ht : t ∈ T) (hr : r ∈ R) (ha : a.val = r.val) : + ¬ TF t a := by + intro h; have := h.range; have := hP.rt r hr t ht; omega +theorem notTF_buf (hP : EPlaced T E R Bf) {t : U64} (ht : t ∈ T) {a : U64} (hw : BufW Bf a) : ¬ TF t a := by + intro h; have := h.range; have := hP.bt a hw t ht; omega +theorem notTF_tcb (hP : EPlaced T E R Bf) {t y a : U64} (ht : t ∈ T) (hy : y ∈ T) (hne : y ≠ t) + (ha : TF y a ∨ TL y a) : ¬ TF t a := by + intro h; have := h.range; have := tcb_disj hP ht hy hne + rcases ha with ha | ha + · have := ha.range; omega + · unfold TL at ha; omega + +theorem kheap_ne {σ : State Z} {p q : ObjRef} {o o' : KernelObject} (hp : σ.kheap p = some o) + (hq : σ.kheap q = some o') (h : ∀ x : KernelObject, o = x → o' = x → False) : p ≠ q := by + intro e; subst e; rw [hq] at hp; injection hp with hp; exact h o' hp.symm rfl + +/-- The buffer clause under memory that agrees on the buffer words (the user memory unchanged). -/ +theorem EpStateRel.buf_agree {m m' : U64 → U64} {σ : State Z} (hR : EpStateRel T E R Bf m σ) + (hP : EPlaced T E R Bf) {P : U64 → Prop} (hA : Agree m m' P) (hp : ∀ a, BufW Bf a → P a) + (um : Word → Word8) (hum : um = σ.machineState.underlyingMemory) : + ∀ bp ∈ Bf, ∀ k : U64, k.val < 128 → + (m' (bufW bp.1 k)).bv = loadWordAt um (bufAddr bp.2 k.val) ∧ (bufAddr bp.2 k.val).toNat % 8 = 0 := by + intro bp hb k hk + have hw : BufW Bf (bufW bp.1 k) := ⟨bp, hb, k, hk, rfl⟩ + rw [hA _ (hP.balign _ hw) (hp _ hw), hum] + exact hR.buf bp hb k hk + +/-- Replacing the TCB at `t`: the other objects keep their relation when memory agrees outside `t`'s fields; `t`'s +state and block object may only change if `t` is in no endpoint queue. -/ +theorem EpStateRel.upd_tcb {m m' : U64 → U64} {σ : State Z} (hR : EpStateRel T E R Bf m σ) + (hP : EPlaced T E R Bf) {t : U64} (ht : t ∈ T) (x' : Tcb) + (hA : Agree m m' (fun b => EFoot T E R Bf b ∧ ¬ TF t b)) + (hq : (∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) ∨ + (m' (stA t) = m (stA t) ∧ m' (tBlockA t) = m (tBlockA t))) + (hx : TcbRelE R m' t x') : + EpStateRel T E R Bf m' (updK σ (specPtr t) (.tcb x')) := by + obtain ⟨x, hk, -⟩ := hR.tcb t ht + refine ⟨fun e he => ?_, fun r hr => ?_, fun y hy => ?_, + hR.buf_agree hP hA (fun a hw => ⟨Or.inr (Or.inr (Or.inr hw)), notTF_buf hP ht hw⟩) _ rfl⟩ + · obtain ⟨z, hkz, hz⟩ := hR.ep e he + have hne : specPtr e ≠ specPtr t := kheap_ne hkz hk (fun _ h1 h2 => by subst h1; cases h2) + refine ⟨z, by simp [hne, hkz], hz.frame (hA _ (a_head hP he).1 ⟨(a_head hP he).2, + notTF_ep hP ht he (Or.inl (val_head e))⟩) (hA _ (a_ts hP he).1 ⟨(a_ts hP he).2, + notTF_ep hP ht he (Or.inr (val_ts hP he))⟩) fun y hy hq' => ?_⟩ + have hya := hP.tcb y hy + have hl : TLinksEq m m' y := by + refine ⟨hA _ (a_prev hya).1 ⟨Or.inr (Or.inr (Or.inl ⟨y, hy, Or.inr (a_prev hya).2⟩)), ?_⟩, + hA _ (a_next hya).1 ⟨Or.inr (Or.inr (Or.inl ⟨y, hy, Or.inr (a_next hya).2⟩)), ?_⟩⟩ + · by_cases e' : y = t + · subst e'; exact (a_prev hya).2.not_tf + · exact notTF_tcb hP ht hy e' (Or.inr (a_prev hya).2) + · by_cases e' : y = t + · subst e'; exact (a_next hya).2.not_tf + · exact notTF_tcb hP ht hy e' (Or.inr (a_next hya).2) + by_cases e' : y = t + · subst e' + rcases hq with hq | ⟨e1, e2⟩ + · exact absurd hq' (hq e he z hkz) + · exact ⟨e1, e2, hl⟩ + · refine ⟨hA _ (a_st hya).1 ⟨Or.inr (Or.inr (Or.inl ⟨y, hy, Or.inl (a_st hya).2⟩)), + notTF_tcb hP ht hy e' (Or.inl (a_st hya).2)⟩, + hA _ (a_blk hya).1 ⟨Or.inr (Or.inr (Or.inl ⟨y, hy, Or.inl (a_blk hya).2⟩)), + notTF_tcb hP ht hy e' (Or.inl (a_blk hya).2)⟩, hl⟩ + · obtain ⟨z, hkz, hz⟩ := hR.reply r hr + have hne : specPtr r ≠ specPtr t := kheap_ne hkz hk (fun _ h1 h2 => by subst h1; cases h2) + refine ⟨z, by simp [hne, hkz], ?_⟩ + rw [hz, hA _ (a_rtcb hP hr).1 ⟨(a_rtcb hP hr).2, notTF_rep hP ht hr (rTcbA_val r)⟩] + · by_cases e : y = t + · subst e; exact ⟨x', by simp, hx⟩ + · obtain ⟨z, hkz, hz⟩ := hR.tcb y hy + refine ⟨z, by simp [bv_inj e, hkz], hz.frame (hP.tcb y hy) fun a ha hf => hA a ha + ⟨Or.inr (Or.inr (Or.inl ⟨y, hy, Or.inl hf⟩)), notTF_tcb hP ht hy e (Or.inl hf)⟩⟩ + +/-- The words of the endpoint at `e`. -/ +def EW (e a : U64) : Prop := a.val = e.val ∨ a.val = e.val + 8 + +/-- The `Ep` links of TCBs blocked on `e` (in memory `m`). -/ +def LinkOn (T : Set U64) (m : U64 → U64) (e a : U64) : Prop := ∃ y ∈ T, m (tBlockA y) = e ∧ TL y a + +/-- Replacing the endpoint at `e`: the other objects keep their relation when memory agrees outside `e`'s words +and the links of TCBs blocked on `e`. -/ +theorem EpStateRel.upd_ep {m m' : U64 → U64} {σ : State Z} (hR : EpStateRel T E R Bf m σ) + (hP : EPlaced T E R Bf) {e : U64} (he : e ∈ E) (x' : Endpoint) + (hA : Agree m m' (fun b => EFoot T E R Bf b ∧ ¬ EW e b ∧ ¬ LinkOn T m e b)) + (hx : EpObjRel m' T e x') : + EpStateRel T E R Bf m' (updK σ (specPtr e) (.endpoint x')) := by + obtain ⟨x, hk, -⟩ := hR.ep e he + have hfield : ∀ y ∈ T, ∀ a, Aligned a → TF y a → m' a = m a := by + intro y hy a ha hf + refine hA a ha ⟨Or.inr (Or.inr (Or.inl ⟨y, hy, Or.inl hf⟩)), ?_, ?_⟩ + · intro hw; have := hf.range; have := hP.et e he y hy; unfold EW at hw; omega + · rintro ⟨z, hz, -, hl⟩ + by_cases e' : z = y + · subst e'; exact hl.not_tf hf + · exact notTF_tcb hP hy hz e' (Or.inr hl) hf + refine ⟨fun e' he' => ?_, fun r hr => ?_, fun y hy => ?_, + hR.buf_agree hP hA (fun a hw => ⟨Or.inr (Or.inr (Or.inr hw)), ?_, ?_⟩) _ rfl⟩ + · by_cases ee : e' = e + · subst ee; exact ⟨x', by simp, hx⟩ + · obtain ⟨z, hkz, hz⟩ := hR.ep e' he' + have hd := ep_disj hP he he' ee + have hnl : ∀ a, (a.val = e'.val ∨ a.val = e'.val + 8) → ¬ LinkOn T m e a := by + rintro a ha ⟨y, hy, -, hl⟩ + have := hP.et e' he' y hy; unfold TL at hl; omega + refine ⟨z, by simp [bv_inj ee, hkz], hz.frame + (hA _ (a_head hP he').1 ⟨(a_head hP he').2, by unfold EW; rw [val_head]; omega, + hnl _ (Or.inl (val_head e'))⟩) + (hA _ (a_ts hP he').1 ⟨(a_ts hP he').2, by unfold EW; rw [val_ts hP he']; omega, + hnl _ (Or.inr (val_ts hP he'))⟩) fun y hy hq' => ?_⟩ + have hya := hP.tcb y hy + have hb : m (tBlockA y) = e' := (hz.blocked hq').2 + have hnl' : ∀ a, TL y a → ¬ LinkOn T m e a := by + rintro a hl ⟨y', hy', hb', hl'⟩ + by_cases e2 : y' = y + · subst e2; exact ee (hb.symm.trans hb') + · have := tcb_disj hP hy hy' e2; unfold TL at hl hl'; omega + have hnw : ∀ a, TL y a → ¬ EW e a := by + intro a hl hw; have := hP.et e he y hy; unfold TL at hl; unfold EW at hw; omega + exact ⟨hfield y hy _ (a_st hya).1 (a_st hya).2, hfield y hy _ (a_blk hya).1 (a_blk hya).2, + hA _ (a_prev hya).1 ⟨Or.inr (Or.inr (Or.inl ⟨y, hy, Or.inr (a_prev hya).2⟩)), hnw _ (a_prev hya).2, + hnl' _ (a_prev hya).2⟩, + hA _ (a_next hya).1 ⟨Or.inr (Or.inr (Or.inl ⟨y, hy, Or.inr (a_next hya).2⟩)), hnw _ (a_next hya).2, + hnl' _ (a_next hya).2⟩⟩ + · obtain ⟨z, hkz, hz⟩ := hR.reply r hr + have hne : specPtr r ≠ specPtr e := kheap_ne hkz hk (fun _ h1 h2 => by subst h1; cases h2) + refine ⟨z, by simp [hne, hkz], ?_⟩ + rw [hz, hA _ (a_rtcb hP hr).1 ⟨(a_rtcb hP hr).2, ?_, ?_⟩] + · unfold EW; rw [rTcbA_val]; have := hP.er e he r hr; omega + · rintro ⟨y, hy, -, hl⟩; have := hP.rt r hr y hy; unfold TL at hl; rw [rTcbA_val] at hl; omega + · obtain ⟨z, hkz, hz⟩ := hR.tcb y hy + have hne : specPtr y ≠ specPtr e := kheap_ne hkz hk (fun _ h1 h2 => by subst h1; cases h2) + exact ⟨z, by simp [hne, hkz], hz.frame (hP.tcb y hy) fun a ha hf => hfield y hy a ha hf⟩ + · unfold EW; have := hP.be a hw e he; omega + · rintro ⟨y, hy, -, hl⟩; have := hP.bt a hw y hy; unfold TL at hl; omega + +/-- Replacing the reply object at `r`. -/ +theorem EpStateRel.upd_reply {m m' : U64 → U64} {σ : State Z} (hR : EpStateRel T E R Bf m σ) + (hP : EPlaced T E R Bf) {r : U64} (hr : r ∈ R) (x' : Reply) + (hA : Agree m m' (fun b => EFoot T E R Bf b ∧ b.val ≠ r.val)) + (hx : x'.replyTcb = optRef (m' (rTcbA r))) : + EpStateRel T E R Bf m' (updK σ (specPtr r) (.reply x')) := by + obtain ⟨x, hk, -⟩ := hR.reply r hr + have hfield : ∀ y ∈ T, ∀ a, Aligned a → (TF y a ∨ TL y a) → m' a = m a := by + intro y hy a ha hf + refine hA a ha ⟨Or.inr (Or.inr (Or.inl ⟨y, hy, hf⟩)), ?_⟩ + intro hv; have := hP.rt r hr y hy + rcases hf with hf | hf + · have := hf.range; omega + · unfold TL at hf; omega + refine ⟨fun e he => ?_, fun r' hr' => ?_, fun y hy => ?_, + hR.buf_agree hP hA (fun a hw => ⟨Or.inr (Or.inr (Or.inr hw)), by have := hP.br a hw r hr; omega⟩) _ rfl⟩ + · obtain ⟨z, hkz, hz⟩ := hR.ep e he + have hne : specPtr e ≠ specPtr r := kheap_ne hkz hk (fun _ h1 h2 => by subst h1; cases h2) + have := hP.er e he r hr + refine ⟨z, by simp [hne, hkz], hz.frame + (hA _ (a_head hP he).1 ⟨(a_head hP he).2, by rw [val_head]; omega⟩) + (hA _ (a_ts hP he).1 ⟨(a_ts hP he).2, by rw [val_ts hP he]; omega⟩) fun y hy _ => ?_⟩ + have hya := hP.tcb y hy + exact ⟨hfield y hy _ (a_st hya).1 (Or.inl (a_st hya).2), hfield y hy _ (a_blk hya).1 (Or.inl (a_blk hya).2), + hfield y hy _ (a_prev hya).1 (Or.inr (a_prev hya).2), hfield y hy _ (a_next hya).1 (Or.inr (a_next hya).2)⟩ + · by_cases ee : r' = r + · subst ee; exact ⟨x', by simp, hx⟩ + · obtain ⟨z, hkz, hz⟩ := hR.reply r' hr' + have := rep_disj hP hr hr' ee + refine ⟨z, by simp [bv_inj ee, hkz], ?_⟩ + rw [hz, hA _ (a_rtcb hP hr').1 ⟨(a_rtcb hP hr').2, by rw [rTcbA_val]; omega⟩] + · obtain ⟨z, hkz, hz⟩ := hR.tcb y hy + have hne : specPtr y ≠ specPtr r := kheap_ne hkz hk (fun _ h1 h2 => by subst h1; cases h2) + exact ⟨z, by simp [hne, hkz], hz.frame (hP.tcb y hy) fun a ha hf => hfield y hy a ha (Or.inl hf)⟩ + +/-! ## Spec steps -/ + +theorem EpStep.trans {σ₁ σ₂ σ₃ : State Z} (h₁ : EpStep σ₁ σ₂) (h₂ : EpStep σ₂ σ₃) : EpStep σ₁ σ₃ := by + obtain ⟨a1, b1, c1⟩ := h₁ + obtain ⟨a2, b2, c2⟩ := h₂ + refine ⟨a2.trans a1, ?_, fun p => ?_⟩ + · rw [b2] + rw [show σ₂.toAbstractState = _ from b1] + rfl + · rcases c1 p with e1 | ⟨x, y, hx, hy⟩ | ⟨x, y, hx, hy, f1⟩ | ⟨x, y, hx, hy, f⟩ <;> + rcases c2 p with e2 | ⟨x', y', hx', hy'⟩ | ⟨x', y', hx', hy', f1'⟩ | ⟨x', y', hx', hy', f'⟩ + · exact Or.inl (e2.trans e1) + · rw [e1] at hx'; exact Or.inr (Or.inl ⟨x', y', hx', hy'⟩) + · rw [e1] at hx'; exact Or.inr (Or.inr (Or.inl ⟨x', y', hx', hy', f1'⟩)) + · rw [e1] at hx'; exact Or.inr (Or.inr (Or.inr ⟨x', y', hx', hy', f'⟩)) + · rw [← e2] at hy; exact Or.inr (Or.inl ⟨x, y, hx, hy⟩) + · exact Or.inr (Or.inl ⟨x, y', hx, hy'⟩) + · rw [hy] at hx'; cases hx' + · rw [hy] at hx'; cases hx' + · rw [← e2] at hy; exact Or.inr (Or.inr (Or.inl ⟨x, y, hx, hy, f1⟩)) + · rw [hy] at hx'; cases hx' + · rw [hy] at hx'; cases hx' + exact Or.inr (Or.inr (Or.inl ⟨x, y', hx, hy', f1'.trans f1⟩)) + · rw [hy] at hx'; cases hx' + · rw [← e2] at hy; exact Or.inr (Or.inr (Or.inr ⟨x, y, hx, hy, f⟩)) + · rw [hy] at hx'; cases hx' + · rw [hy] at hx'; cases hx' + · rw [hy] at hx'; cases hx' + refine Or.inr (Or.inr (Or.inr ⟨x, y', hx, hy', ?_⟩)) + rw [f', f] + +/-- Writing an endpoint. -/ +theorem EpStep.ep {σ : State Z} {p : ObjRef} {x y : Endpoint} (hk : σ.kheap p = some (.endpoint x)) : + EpStep σ (updK σ p (.endpoint y)) := by + refine ⟨rfl, rfl, fun q => ?_⟩ + by_cases e : q = p + · subst e; exact Or.inr (Or.inl ⟨x, y, hk, by simp⟩) + · exact Or.inl (by simp [e]) + +/-- Writing the TCB of a reply. -/ +theorem EpStep.reply {σ : State Z} {p : ObjRef} {x : Reply} (hk : σ.kheap p = some (.reply x)) (v : Option ObjRef) : + EpStep σ (updK σ p (.reply { x with replyTcb := v })) := by + refine ⟨rfl, rfl, fun q => ?_⟩ + by_cases e : q = p + · subst e; exact Or.inr (Or.inr (Or.inl ⟨x, { x with replyTcb := v }, hk, by simp, rfl⟩)) + · exact Or.inl (by simp [e]) + +/-- Writing a TCB's state and user context. -/ +theorem EpStep.tcb {σ : State Z} {p : ObjRef} {x : Tcb} (hk : σ.kheap p = some (.tcb x)) (ts : ThreadState) + (a : ArchTcb) : + EpStep σ (updK σ p (.tcb { x with tcbState := ts, tcbArch := a })) := by + refine ⟨rfl, rfl, fun q => ?_⟩ + by_cases e : q = p + · subst e; exact Or.inr (Or.inr (Or.inr ⟨x, { x with tcbState := ts, tcbArch := a }, hk, by simp, rfl⟩)) + · exact Or.inl (by simp [e]) + +/-- (gen 5) Writing a TCB's state, user context and fault (`cancel_ipc`'s fault clear). -/ +theorem EpStep.tcbF {σ : State Z} {p : ObjRef} {x : Tcb} (hk : σ.kheap p = some (.tcb x)) (ts : ThreadState) + (a : ArchTcb) (f : Option Fault) : + EpStep σ (updK σ p (.tcb { x with tcbState := ts, tcbArch := a, tcbFault := f })) := by + refine ⟨rfl, rfl, fun q => ?_⟩ + by_cases e : q = p + · subst e + exact Or.inr (Or.inr (Or.inr ⟨x, { x with tcbState := ts, tcbArch := a, tcbFault := f }, hk, by simp, rfl⟩)) + · exact Or.inl (by simp [e]) + +end Upd + +end + +end + +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/EpXferA.lean b/proofs/Fl4ProofsA64/P3/EpXferA.lean new file mode 100644 index 000000000..d81b08ed1 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/EpXferA.lean @@ -0,0 +1,541 @@ +/- +P3 (aarch64, A64-P3-IPC gen 9, ruling item 1): **FL4's `do_ipc_transfer` against l4v `do_ipc_transfer`** for a sender +without a fault and no extra caps to look up, with or without IPC buffers (x86 `P3/EpXfer`'s +`do_ipc_transfer_regs` and `P3/EpXferBuf2`'s `do_ipc_transfer_buf` in one statement, on gen 8's `copy_mrs_relA`). + +* Spec side (x86 `EpXferSpec` / `EpBufSpec`): `getExtraCptrs_nilA`, `runE_liftE_bindA`, `catch_lookupExtraCaps_nilA` + (no extra caps), `transferCaps_nilA` (no caps: the receive-slot lookup's result is unused, one outcome), + **`doIpcTransfer_xferA`** (one outcome from `copy_mrs`'s and the receive-slot lookup's). +* **The receive-slot lookup** (x86 `EpBufMapHyps.recvSlots`, a premise there): `loadCapTransfer_eqA`, + `emptyOnFailure_oneA`, **`getReceiveSlots_oneA`** — l4v `get_receive_slots t (Some rb)` has one outcome and keeps + the state, at any state `StateRelA`-related with `SlotsValidA` (from `LookupA`'s walk and `FrontEndExtraA`'s + `lookup_slot_refinesA`, through FL4's total lookups); **`StateRelA.xferA`** / `slotsValid_xferA`: `StateRelA` across the copy (the + receiver's user context and words off the cap words of the slots). +* FL4: `and127A`, `to_word_specA`, `set_msg_info_specA`, `lookup_extra_caps_noneA`, `transfer_caps_noextraA`, + `copy_mrs_anyA` (`copy_mrs` with or without a receive buffer), **`do_ipc_transfer_relA`**: both buffer lookups + through **`lookup_buf_full`** (`BufCover`; no x86 `EpBufHyps` / `EpBufMapHyps` premise), the relation and `SR` kept, + one spec outcome, only the receiver's registers and the tracked buffer words written. + +Hypotheses (case / placement, no named premise): `EPlaced`, `PlaceA` / `StateRelA` / `SlotsValidA` (`JAtZA` fields), +`BufCover` (gen 4 placement), the TCBs of `th` and `T`, `snd ≠ rcv`, the sender without fault, no extra caps (`hcaps`, +x86's), `hu` (the receive buffer's words in user frames, gen 8's `copy_mrs_relA` case hypothesis), `hoff` (no IPC +write is a cap word of a slot: the `FootA` disjointness the join supplies for `JAtZAX.of_writesIn`). +-/ +import Fl4ProofsA64.P3.EpCopyMrsA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif + +noncomputable section + +/-! ## The spec's transfer -/ + +section Spec +open Fl4SpecA64 + +theorem getExtraCptrs_nilA {σ : State Unit} {sbuf : Option ObjRef} {mi : MessageInfo} + (h : sbuf = none ∨ unat mi.miExtraCaps = 0) : getExtraCptrs (Z := Unit) sbuf mi σ = ({([], σ)}, False) := by + rcases h with rfl | h + · rfl + · cases sbuf with + | none => rfl + | some b => + unfold getExtraCptrs + dsimp only + rw [h, Nat.add_zero, Nat.sub_self] + unfold Fl4Spec.NonDet.liftM + exact nd_bind_one' (show Fl4Spec.NonDet.mapM (loadWordOffs (Z := Unit) b) [] σ = ({([], σ)}, False) from rfl) _ + +theorem runE_liftE_bindA {ε α β : Type} {m : Fl4Spec.NonDet (State Unit) α} + {f : α → Fl4Spec.NonDetE (State Unit) ε β} {s : State Unit} {a : α} + (h : m s = ({(a, s)}, False)) : ExceptT.run (Fl4Spec.NonDetE.liftE m >>= f) s = ExceptT.run (f a) s := by + show Fl4Spec.NonDet.bind (Fl4Spec.NonDet.bind m (fun a => Fl4Spec.NonDet.pure (Except.ok a))) + (ExceptT.bindCont f) s = _ + rw [nd_bind_one' (show Fl4Spec.NonDet.bind m (fun a => Fl4Spec.NonDet.pure (Except.ok a)) s = + ({(Except.ok a, s)}, False) by rw [nd_bind_one' h]; rfl)] + rfl + +theorem catch_lookupExtraCaps_nilA {σ : State Unit} {snd : ObjRef} {sbuf : Option ObjRef} {mi : MessageInfo} + (h : sbuf = none ∨ unat mi.miExtraCaps = 0) : + Fl4Spec.NonDetE.catch (lookupExtraCaps (Z := Unit) snd sbuf mi) (fun _ => pure []) σ = ({([], σ)}, False) := by + have hg := getExtraCptrs_nilA (σ := σ) h + unfold Fl4Spec.NonDetE.catch lookupExtraCaps + rw [nd_bind_one' (show ExceptT.run (Fl4Spec.NonDetE.liftE (getExtraCptrs sbuf mi) >>= fun cptrs => + Fl4Spec.NonDetE.mapME (fun cptr => capFaultOnFailure (ofBl cptr) false (lookupCapAndSlot snd cptr)) cptrs) σ = + ({(Except.ok [], σ)}, False) by rw [runE_liftE_bindA hg]; rfl)] + rfl + +/-- `transfer_caps` without caps: the message info with no caps, the state kept (x86 `transferCaps_noRecvBuf` / +`transferCaps_recvBuf`). -/ +theorem transferCaps_nilA {σ : State Unit} (mi : MessageInfo) (ep : Option ObjRef) (rcv : ObjRef) + (rbo : Option ObjRef) + (hsl : ∀ rb, rbo = some rb → ∃ sl, getReceiveSlots (Z := Unit) rcv (some rb) σ = ({(sl, σ)}, False)) : + transferCaps (Z := Unit) mi [] ep rcv rbo σ = ({(⟨mi.miLength, 0, 0, mi.miLabel⟩, σ)}, False) := by + cases rbo with + | none => + unfold transferCaps + rw [nd_bind_one (show getReceiveSlots (Z := Unit) rcv none σ = ({([], σ)}, False) from rfl), nd_pure_bind'] + rfl + | some rb => + obtain ⟨sl, hs⟩ := hsl rb rfl + unfold transferCaps + rw [nd_bind_one hs, nd_pure_bind'] + rfl + +/-- **`do_ipc_transfer`** of a sender without fault and no extra caps (x86 `doIpcTransfer_regs` / +`doIpcTransfer_recvBuf`): one outcome, from `copy_mrs`'s and the receive-slot lookup's. -/ +theorem doIpcTransfer_xferA {σ : State Unit} {snd rcv : ObjRef} {xs xr : Tcb} + (hks : σ.kheap snd = some (.tcb xs)) (hkr : σ.kheap rcv = some (.tcb xr)) (hf : xs.tcbFault = none) + (ep : Option ObjRef) (badge : Badge) (grant : Bool) + (hcaps : grant = false ∨ ipcBufOfA false xs = none ∨ + unat (dataToMessageInfo (xs.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0) + {mrs : LengthType} {σc : State Unit} {xc : Tcb} + (hcopy : copyMrs (Z := Unit) snd (ipcBufOfA false xs) rcv (ipcBufOfA true xr) + (dataToMessageInfo (xs.tcbArch.tcbContext.userRegs msg_info_register)).miLength σ = ({(mrs, σc)}, False)) + (hkc : σc.kheap rcv = some (.tcb xc)) + (hsl : ∀ rb, ipcBufOfA true xr = some rb → + ∃ sl, getReceiveSlots (Z := Unit) rcv (some rb) σc = ({(sl, σc)}, False)) : + doIpcTransfer (Z := Unit) snd ep badge grant rcv σ = + ({((), updK σc rcv (.tcb (setReg (setReg xc msg_info_register (messageInfoToData + ⟨mrs, 0, 0, (dataToMessageInfo (xs.tcbArch.tcbContext.userRegs msg_info_register)).miLabel⟩)) + badge_register badge)))}, False) := by + unfold doIpcTransfer + rw [nd_bind_one (lookupIpcBuffer_eqA hkr true), nd_bind_one (threadGet_eq _ hks), hf] + dsimp only + rw [nd_bind_one (lookupIpcBuffer_eqA hks false)] + unfold doNormalTransfer + rw [nd_bind_one (getMessageInfo_eqA hks)] + have hlk : grant = true → Fl4Spec.NonDetE.catch (lookupExtraCaps (Z := Unit) snd (ipcBufOfA false xs) + (dataToMessageInfo (xs.tcbArch.tcbContext.userRegs msg_info_register))) (fun _ => pure []) σ = + ({([], σ)}, False) := by + intro hg + refine catch_lookupExtraCaps_nilA ?_ + rcases hcaps with h | h | h + · rw [hg] at h; cases h + · exact Or.inl h + · exact Or.inr h + cases grant <;> simp only [Bool.false_eq_true, ↓reduceIte] <;> + first | rw [nd_bind_one (hlk rfl)] | rw [nd_pure_bind'] + all_goals + rw [nd_bind_one hcopy, nd_bind_one (transferCaps_nilA _ _ _ _ hsl)] + unfold setMessageInfo + rw [nd_bind_one (asUser_setRegister_eq _ _ hkc)] + rw [asUser_setRegister_eq (x := setReg xc msg_info_register (messageInfoToData + ⟨mrs, 0, 0, (dataToMessageInfo (xs.tcbArch.tcbContext.userRegs msg_info_register)).miLabel⟩)) _ _ + (by simp only [updK_kheap, Function.update_self])] + simp only [updK, Function.update_idem] + +/-- A TCB that differs from `x` in its user context only stays so after a register write. -/ +theorem setReg_withA {x y : Tcb} (h : y = { x with tcbArch := y.tcbArch }) (r : Register) (v : Word) : + setReg y r v = { x with tcbArch := (setReg y r v).tcbArch } := + calc setReg y r v = setReg { x with tcbArch := y.tcbArch } r v := by rw [← h] + _ = _ := rfl + +end Spec + +/-! ## The receive-slot lookup -/ + +section Slots +open Fl4SpecA64 + +theorem dmo_loadWord_eqA (σ : State Unit) (a : Word) (h : a.toNat % 8 = 0) : + doMachineOp (Z := Unit) (loadWord a) σ = ({(loadWordAtA σ.machineState.underlyingMemory a, σ)}, False) := by + unfold doMachineOp + rw [nd_gets_bindA, loadWord_eqA _ _ h, nd_bind_singleA (selectF_oneA _ σ)] + dsimp only + rw [nd_modify_bindA] + rfl + +/-- The spec's cap-transfer words: words 125–127 of the receive buffer (x86 `loadCapTransfer_eq`). -/ +theorem loadCapTransfer_eqA {σ : State Unit} {rb : Word} (hal : ∀ k, k < 128 → (bufAddr rb k).toNat % 8 = 0) : + loadCapTransfer (Z := Unit) rb σ = + ({(⟨dataToCptr (loadWordAt σ.machineState.underlyingMemory (bufAddr rb 125)), + dataToCptr (loadWordAt σ.machineState.underlyingMemory (bufAddr rb 126)), + loadWordAt σ.machineState.underlyingMemory (bufAddr rb 127)⟩, σ)}, False) := by + unfold loadCapTransfer captransferFromWords + rw [nd_pure_bind'] + have e0 : rb + BitVec.ofNat 64 (msgMaxLength + msgMaxExtraCaps + 2) * BitVec.ofNat 64 wordSize = + bufAddr rb 125 := by + unfold bufAddr; congr 1 + have e1 : bufAddr rb 125 + BitVec.ofNat 64 wordSize = bufAddr rb 126 := by + unfold bufAddr; rw [BitVec.add_assoc]; congr 1 + have e2 : bufAddr rb 125 + 2 * BitVec.ofNat 64 wordSize = bufAddr rb 127 := by + unfold bufAddr; rw [BitVec.add_assoc]; congr 1 + rw [e0, e1, e2, nd_bind_one (dmo_loadWord_eqA σ _ (hal 125 (by omega))), + nd_bind_one (dmo_loadWord_eqA σ _ (hal 126 (by omega))), nd_bind_one (dmo_loadWord_eqA σ _ (hal 127 (by omega)))] + rfl + +/-- `empty_on_failure` of a computation with one outcome that keeps the state. -/ +theorem emptyOnFailure_oneA {α F : Type} {m : Fl4Spec.NonDetE (State Unit) F (List α)} {σ : State Unit} + (h : ∃ e, m.run σ = ({(e, σ)}, False)) : ∃ l, emptyOnFailure (Z := Unit) m σ = ({(l, σ)}, False) := by + obtain ⟨e, he⟩ := h + unfold emptyOnFailure Fl4Spec.NonDetE.catch + rw [nd_bind_one' he] + cases e <;> exact ⟨_, rfl⟩ + +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **l4v `get_receive_slots t (Some rb)` has one outcome and keeps the state** (x86 `EpBufMapHyps.recvSlots`, a +premise there), at a state `StateRelA`-related with `SlotsValidA`: the root lookup (`lookup_slot_refinesA`), the +target slot (`lookup_slot_for_cnode_op_refinesA`) and the slot's cap (`StateRelA.slot_capA`) each have one outcome. -/ +theorem getReceiveSlots_oneA {th : TypedHeapA} {kb : U64} (hPl : PlaceA th kb) (s : S) (σ : State Unit) + (hR : StateRelA th (L.mem s) σ) (hV : SlotsValidA th (L.mem s)) {t : U64} (ht : th t = some .tcb) + {rb : Word} (hal : (bufAddr rb 0).toNat % 8 = 0) : + ∃ sl, getReceiveSlots (Z := Unit) (specPtr t) (some rb) σ = ({(sl, σ)}, False) := by + have hal' : ∀ k, k < 128 → (bufAddr rb k).toNat % 8 = 0 := fun k hk => bufAddr_align hal k hk + simp only [getReceiveSlots] + rw [nd_bind_one (loadCapTransfer_eqA hal')] + apply emptyOnFailure_oneA + dsimp only + obtain ⟨r0, -, e0, he0, hrel0⟩ := spec_existsA (lookup_slot_refinesA th s σ hR hV t ht + ⟨loadWordAt σ.machineState.underlyingMemory (bufAddr rb 125)⟩) + have he0' : (lookupSlotForThread (Z := Unit) (specPtr t) + (dataToCptr (loadWordAt σ.machineState.underlyingMemory (bufAddr rb 125)))).run σ = ({(e0, σ)}, False) := he0 + generalize loadWordAt σ.machineState.underlyingMemory (bufAddr rb 125) = w5 at he0' ⊢ + generalize loadWordAt σ.machineState.underlyingMemory (bufAddr rb 126) = w6 + generalize loadWordAt σ.machineState.underlyingMemory (bufAddr rb 127) = w7 + cases e0 with + | error lf => + have h1 : (lookupCap (Z := Unit) (specPtr t) (dataToCptr w5)).run σ = ({(.error lf, σ)}, False) := by + unfold lookupCap; exact run_bind_errorA he0' _ + have hu1 : (unifyFailure (Z := Unit) (lookupCap (Z := Unit) (specPtr t) (dataToCptr w5))).run σ = + ({(.error (), σ)}, False) := by + unfold unifyFailure; rw [run_handleE_errA h1]; rfl + exact ⟨_, run_bind_errorA hu1 _⟩ + | ok v => + obtain ⟨sl, rest⟩ := v + obtain ⟨p, hp1, hp, -⟩ := hrel0.2 + obtain ⟨a, hca, hg⟩ := hR.slot_capA hp1 hp + have hcv : CapValidA th (capAtA (L.mem s) (cslotAddrA sl)) := hV _ (AbsSlotA.isSlot hPl ⟨p, hp1, Or.inl hp⟩) + have h1 : (lookupCap (Z := Unit) (specPtr t) (dataToCptr w5)).run σ = ({(.ok a, σ)}, False) := by + unfold lookupCap; rw [run_bind_okA he0']; exact run_liftEA hg + have hu1 : (unifyFailure (Z := Unit) (lookupCap (Z := Unit) (specPtr t) (dataToCptr w5))).run σ = + ({(.ok a, σ)}, False) := run_handleE_okA h1 + rw [run_bind_okA hu1] + try dsimp only + obtain ⟨r1, -, e1, he1, hrel1⟩ := spec_existsA (lookup_slot_for_cnode_op_refinesA th s σ hR hV false _ a hca hcv + ⟨w6⟩ ⟨w7⟩) + have he1' : (lookupTargetSlot (Z := Unit) a (dataToCptr w6) (unat w7)).run σ = ({(e1, σ)}, False) := he1 + cases e1 with + | error e' => + have hu2 : (unifyFailure (Z := Unit) (lookupTargetSlot (Z := Unit) a (dataToCptr w6) (unat w7))).run σ = + ({(.error (), σ)}, False) := by + unfold unifyFailure; rw [run_handleE_errA he1']; rfl + exact ⟨_, run_bind_errorA hu2 _⟩ + | ok sl2 => + cases r1 with + | Err _ => exact hrel1.elim + | Ok _ => + obtain ⟨-, p2, h21, h22, -⟩ := hrel1 + obtain ⟨a2, -, hg2⟩ := hR.slot_capA h21 h22 + have hu2 : (unifyFailure (Z := Unit) (lookupTargetSlot (Z := Unit) a (dataToCptr w6) (unat w7))).run σ = + ({(.ok sl2, σ)}, False) := run_handleE_okA he1' + rw [run_bind_okA hu2] + try dsimp only + rw [run_bind_okA (run_liftEA hg2)] + try dsimp only + unfold Fl4Spec.NonDetE.whenE + by_cases hn : a2 = .nullCap + · rw [if_neg (fun h => h hn)] + exact ⟨_, (run_bind_okA (show (pure () : Fl4Spec.NonDetE (State Unit) Unit Unit).run σ = + ({(.ok (), σ)}, False) from rfl) _).trans rfl⟩ + · rw [if_pos hn] + exact ⟨_, run_bind_errorA (run_throwErrorA _ σ) _⟩ + +end Slots + +/-! ## `StateRelA` across the copy -/ + +section Frame +open Fl4SpecA64 + +/-- **`StateRelA` across a step that changes a TCB's user context only and no cap word of a slot.** -/ +theorem _root_.Fl4ProofsA64.P3.StateRelA.xferA {th : TypedHeapA} {kb : U64} (hPl : PlaceA th kb) {mem mem' : U64 → U64} + {σ σ' : State Unit} (hR : StateRelA th mem σ) + (hc : ∀ y, IsSlotA th y → mem' y = mem y ∧ + mem' (core.num.U64.wrapping_add y 8#u64) = mem (core.num.U64.wrapping_add y 8#u64)) + {p : ObjRef} {x y : Tcb} (hk : σ.kheap p = some (.tcb x)) (hy : y = { x with tcbArch := y.tcbArch }) + (hσ : σ'.kheap = Function.update σ.kheap p (some (.tcb y))) : StateRelA th mem' σ' := by + have hR' : StateRelA th mem' σ := hR.congrA fun z hz => by + obtain ⟨h1, h2⟩ := hc z (slotW_isSlot hPl hz) + unfold capAtA; rw [h1, h2] + refine ⟨fun q r hq => ?_, fun t ht => ?_⟩ + · obtain ⟨cs, h1, h2, h3⟩ := hR'.cnode q r hq + have hne : specPtr q ≠ p := fun e => by rw [e, hk] at h1; cases h1 + exact ⟨cs, by rw [hσ, Function.update_of_ne hne, h1], h2, h3⟩ + · obtain ⟨z, h1, h2⟩ := hR'.tcb t ht + by_cases e : specPtr t = p + · rw [e, hk] at h1 + cases h1 + refine ⟨y, by rw [hσ, e, Function.update_self], fun i hi => ?_⟩ + rw [hy]; exact h2 i hi + · exact ⟨z, by rw [hσ, Function.update_of_ne e, h1], h2⟩ + +theorem slotsValid_xferA {th : TypedHeapA} {mem mem' : U64 → U64} + (hV : SlotsValidA th mem) + (hc : ∀ y, IsSlotA th y → mem' y = mem y ∧ + mem' (core.num.U64.wrapping_add y 8#u64) = mem (core.num.U64.wrapping_add y 8#u64)) : + SlotsValidA th mem' := fun y hy => by + have h := hV y hy + obtain ⟨h1, h2⟩ := hc y hy + unfold capAtA at h ⊢; rw [h1, h2]; exact h + +/-- A word no IPC write touches is neither a message register of the receiver nor a tracked buffer word. -/ +theorem not_rcv_bufA {T E R : Set U64} {Bf : Set (U64 × Word)} {rcv b : U64} (hr : rcv ∈ T) (hra : TcbAddr rcv) + (h : ¬ EWrite T E R Bf b) : ¬ RcvMRA rcv b ∧ ¬ BufW Bf b := by + refine ⟨fun ⟨r, _, hv⟩ => h ?_, fun hb => h (Or.inr (Or.inr (Or.inr hb)))⟩ + have e : b = regA rcv (regIdxA r) := UScalar.eq_of_val_eq hv + exact Or.inr (Or.inr (Or.inl ⟨rcv, hr, Or.inl (by rw [e]; exact twf_reg hra r)⟩)) + +end Frame + +/-! ## FL4's transfer -/ + +section Fl4 +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +theorem and127A {n : U64} (hn : n.val < 128) : (n &&& 127#u64).bv = n.bv := by + apply BitVec.eq_of_toNat_eq + rw [UScalar.bv_and, BitVec.toNat_and] + have : (UScalar.bv (127#u64 : U64)).toNat = 2 ^ 7 - 1 := rfl + rw [this, Nat.and_two_pow_sub_one_eq_mod] + exact Nat.mod_eq_of_lt hn + +/-- `to_word` of a message info without caps (x86 `to_word_spec`). -/ +theorem to_word_specA (lab n : U64) (hn : n.val < 128) : + msginfo.MessageInfo.to_word { label := lab, caps_unwrapped := 0#u64, extra_caps := 0#u64, length := n } ⦃ v => + v.bv = Fl4SpecA64.messageInfoToData ⟨n.bv, 0, 0, lab.bv &&& Fl4SpecA64.mask Fl4SpecA64.msgLabelBits⟩ ⦄ := by + unfold msginfo.MessageInfo.to_word + step* + simp only [Fl4SpecA64.messageInfoToData, mask52A] + have h8 : i8.bv = n.bv := i8_post1.trans ((UScalar.bv_and n 127#u64).symm.trans (and127A hn)) + have z : UScalar.bv (0#u64 : U64) = 0#64 := rfl + simp only [UScalar.bv_or, i7_post1, i4_post1, i6_post1, i5_post1, i3_post1, i2_post1, i1_post1, i_post1, h8, z, + BitVec.zero_and, BitVec.zero_shiftLeft, BitVec.or_zero] + have z7 : ((0 : BitVec 64) <<< 7) = 0 := by decide + have z9 : ((0 : BitVec 64) <<< 9) = 0 := by decide + have o : ∀ x : BitVec 64, (x ||| (0 : BitVec 64)) = x := fun x => by simp + rw [z7, z9, o, o] + rfl + +/-- `set_msg_info` of a message info without caps (x86 `set_msg_info_spec`). -/ +theorem set_msg_info_specA (s : S) (t : obj.Tcb) (lab n : U64) (hn : n.val < 128) : + msg.set_msg_info st s t { label := lab, caps_unwrapped := 0#u64, extra_caps := 0#u64, length := n } ⦃ s' => + ∃ v : U64, s' = L.wr s (regA t layout.reg.MSG_INFO) v ∧ + v.bv = Fl4SpecA64.messageInfoToData ⟨n.bv, 0, 0, lab.bv &&& Fl4SpecA64.mask Fl4SpecA64.msgLabelBits⟩ ⦄ := by + unfold msg.set_msg_info + refine WP.spec_bind (to_word_specA lab n hn) ?_ + rintro v hv + rw [set_reg_eqA, WP.spec_ok] + exact ⟨v, rfl, hv⟩ + +/-- `lookup_extra_caps` with no buffer or no extra caps finds none (x86 `lookup_extra_caps_none`). -/ +theorem lookup_extra_caps_noneA (s : S) (t buf : U64) (info : msginfo.MessageInfo) + (h : buf = 0#u64 ∨ info.extra_caps = 0#u64) : + ipc.lookup_extra_caps st s t buf info ⦃ r => r = .Ok ipc.NO_EXTRA_CAPS ⦄ := by + unfold ipc.lookup_extra_caps + by_cases hb : buf = 0#u64 + · simp [hb] + · simp only [hb, ↓reduceIte] + have he : info.extra_caps = 0#u64 := h.resolve_left hb + unfold ipc.lookup_extra_caps_loop + apply loop.spec_decr_nat (measure := fun _ => 0) (inv := fun x => x.1 = ipc.NO_EXTRA_CAPS) + · rintro ⟨caps, i⟩ hc + simp only at hc + subst hc + unfold ipc.lookup_extra_caps_loop.body + have : ¬ i < info.extra_caps := by rw [he]; intro h'; have := (gt_iff_val).1 h'; simp at this + simp [this] + · rfl + +/-- `transfer_caps` without extra caps: the message info with no caps, the state unchanged (x86 +`transfer_caps_noextra`). -/ +theorem transfer_caps_noextraA (s : S) (info : msginfo.MessageInfo) (ep : U64) (receiver : obj.Tcb) (rb : U64) : + ipc.transfer_caps st s info ipc.NO_EXTRA_CAPS ep receiver rb ⦃ r => + r = ({ label := info.label, caps_unwrapped := 0#u64, extra_caps := 0#u64, length := info.length }, s) ⦄ := by + unfold ipc.transfer_caps msginfo.MessageInfo.new + rw [bind_ok, exAtA_idx0, bind_ok, exAtA_none 0 (by omega)] + simp [obj.Slot.is_null] + +variable {T E R : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {SR : S → Fl4SpecA64.State Unit → Prop} + +open Fl4SpecA64 in +/-- **`copy_mrs` with or without a receive buffer** (`copy_mrs_relA`, or the first loop only). -/ +theorem copy_mrs_anyA (hO : SchedOpsEA st T E R Bf SR) (hP : EPlaced T E R Bf) (s : S) (σ : State Unit) + (hR : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) {snd rcv : U64} (hs : snd ∈ T) (hr : rcv ∈ T) + (hne : snd ≠ rcv) {xs xr : Tcb} (hks : σ.kheap (specPtr snd) = some (.tcb xs)) + (hkr : σ.kheap (specPtr rcv) = some (.tcb xr)) {rbw : U64} {rbo : Option ObjRef} + (hrw : (rbo = none ∧ rbw = 0#u64) ∨ (∃ br ∈ Bf, rbo = some br.2 ∧ rbw = br.1 ∧ br.1 ≠ 0#u64)) + (hu : ∀ rp, rbo = some rp → ∀ k, k < 128 → inUserFrame (bufAddr rp k) σ) {sw : U64} {sb : Option ObjRef} + (hsw : (sb = none ∧ sw = 0#u64) ∨ (∃ bs ∈ Bf, sb = some bs.2 ∧ sw = bs.1 ∧ bs.1 ≠ 0#u64)) + (n : U64) (hn : n.val ≤ 120) : + msg.copy_mrs st s snd sw rcv rbw n ⦃ r => ∃ σc, copyMrs (Z := Unit) (specPtr snd) sb (specPtr rcv) rbo + n.bv σ = ({(r.1.bv, σc)}, False) ∧ + σc.kheap = Function.update σ.kheap (specPtr rcv) (some (.tcb (copyRegsTcbA xr xs.tcbArch.tcbContext.userRegs + (msg_registers.take n.val)))) ∧ + EpStateRel T E R Bf (L.mem r.2) σc ∧ SR r.2 σc ∧ + Agree (L.mem s) (L.mem r.2) (fun b => ¬ RcvMRA rcv b ∧ ¬ BufW Bf b) ∧ r.1.val ≤ 120 ∧ + WritesIn st s r.2 (EWrite T E R Bf) ∧ EpStep σ σc ⦄ := by + rcases hrw with ⟨rfl, rfl⟩ | ⟨br, hbr, rfl, rfl, hbr0⟩ + · have hne' : specPtr snd ≠ specPtr rcv := fun h => hne (specPtr_inj h) + refine WP.spec_mono (copy_mrs_norecvA (L := L) hP s hs hr hne sw n) ?_ + rintro ⟨i, s1⟩ ⟨hi, hregs, hag, hw⟩ + obtain ⟨hR1, hS1⟩ := loop0_relA hO hP hR hSR hs hr hks hkr n.val hregs hag hw + have hun : unat n.bv = n.val := rfl + refine ⟨updK σ (specPtr rcv) (.tcb (copyRegsTcbA xr xs.tcbArch.tcbContext.userRegs (msg_registers.take n.val))), + ?_, rfl, hR1, hS1, hag.mono fun b hb => hb.1, by show i.val ≤ 120; have : i.val = min n.val 4 := hi; omega, hw, + (copyRegs_step hkr _ _).1⟩ + rw [copyMrs_noRecvBufA hne' hks hkr _ _, hun] + congr + exact (xfer_lenA (by rw [hi]; rfl)).symm + · exact copy_mrs_relA hO hP s σ hR hSR hs hr hne hks hkr hbr hbr0 (hu _ rfl) hsw n hn + +/-- The lookup of a TCB's buffer, as the case split `copy_mrs` takes. -/ +theorem buf_caseA {w : U64} {o : Option Fl4SpecA64.ObjRef} (h0 : w = 0#u64 ↔ o = none) + (hm : ∀ sb, o = some sb → (w, sb) ∈ Bf) : + (o = none ∧ w = 0#u64) ∨ (∃ bs ∈ Bf, o = some bs.2 ∧ w = bs.1 ∧ bs.1 ≠ 0#u64) := by + cases h : o with + | none => exact Or.inl ⟨rfl, h0.2 h⟩ + | some sb => + exact Or.inr ⟨(w, sb), hm sb h, rfl, rfl, fun e => by have := h0.1 e; rw [h] at this; cases this⟩ + +variable {A : Type} {ar : machine.Arch A} + +open Fl4SpecA64 in +/-- **FL4's `do_ipc_transfer` refines l4v `do_ipc_transfer`** for a sender without fault and no extra caps, with or +without IPC buffers (x86 `do_ipc_transfer_regs` / `do_ipc_transfer_buf`; module docstring): one spec outcome, the +relation and `SR` kept, only the receiver's registers and tracked buffer words written, the receiver changed in its +user context only. -/ +theorem do_ipc_transfer_relA (hO : SchedOpsEA st T E R Bf SR) (hP : EPlaced T E R Bf) {th : TypedHeapA} {kb : U64} + (hPl : PlaceA th kb) (s : S) (a : A) (σ : State Unit) (hR : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) + (hSt : StateRelA th (L.mem s) σ) (hV : SlotsValidA th (L.mem s)) (hC : BufCover st T Bf s σ) + (hNative : BufNativeA T (L.mem s)) + {snd rcv : U64} (hs : snd ∈ T) (hr : rcv ∈ T) (hne : snd ≠ rcv) (hts : th snd = some .tcb) + (htr : th rcv = some .tcb) {xs xr : Tcb} (hks : σ.kheap (specPtr snd) = some (.tcb xs)) + (hkr : σ.kheap (specPtr rcv) = some (.tcb xr)) (hf : xs.tcbFault = none) + (hu : ∀ rp, ipcBufOfA true xr = some rp → ∀ k, k < 128 → inUserFrame (bufAddr rp k) σ) + (hoff : ∀ y, IsSlotA th y → ¬ EWrite T E R Bf y ∧ ¬ EWrite T E R Bf (core.num.U64.wrapping_add y 8#u64)) + (ep badge : U64) (grant : Bool) + (hcaps : grant = false ∨ ipcBufOfA false xs = none ∨ + unat (dataToMessageInfo (xs.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0) + (epo : Option ObjRef) : + ipc.do_ipc_transfer st ar s a snd ep badge grant rcv ⦃ s' => ∃ σ', + doIpcTransfer (Z := Unit) (specPtr snd) epo badge.bv grant (specPtr rcv) σ = ({((), σ')}, False) ∧ + EpStateRel T E R Bf (L.mem s') σ' ∧ SR s' σ' ∧ + Agree (L.mem s) (L.mem s') (fun b => ¬ RcvRegA rcv b ∧ ¬ BufW Bf b) ∧ WritesIn st s s' (EWrite T E R Bf) ∧ + EpStep σ σ' ∧ (∀ p, p ≠ specPtr rcv → σ'.kheap p = σ.kheap p) ∧ + ∃ x', σ'.kheap (specPtr rcv) = some (.tcb x') ∧ x' = { xr with tcbArch := x'.tcbArch } ⦄ := by + have hra := hP.tcb rcv hr + have hxs := hR.at_tcb hs hks + have hwmi : (L.mem s (regWA snd layout.reg.MSG_INFO)).bv = xs.tcbArch.tcbContext.userRegs msg_info_register := by + rw [← regIdxA_msgInfo]; exact hxs.regs _ + unfold ipc.do_ipc_transfer + refine WP.spec_bind (lookup_buf_full s σ hSt htr hr hkr hC hNative true) ?_ + rintro rbw ⟨hrw0, hrwB⟩ + step with obj.Tcb.fault_type.spec as ⟨ft, hft⟩ + have hft0 : ft = fl4_abi.consts.fault.NullFault := by + rw [hft, hxs.fault0.2 hf]; unfold fl4_abi.consts.fault.NullFault; rfl + rw [if_pos hft0] + refine WP.spec_bind (lookup_buf_full s σ hSt hts hs hks hC hNative false) ?_ + rintro sw ⟨hsw0, hswB⟩ + unfold ipc.do_normal_transfer + refine WP.spec_bind (msg_info_specA (L := L) s snd) ?_ + rintro tag ⟨hlab, hlen, hext, hle⟩ + have hex : (if grant = true then (do + let r ← ipc.lookup_extra_caps st s snd sw tag + match r with + | core.result.Result.Ok e => ok e + | core.result.Result.Err _ => ok ipc.NO_EXTRA_CAPS) + else ok ipc.NO_EXTRA_CAPS) ⦃ e => e = ipc.NO_EXTRA_CAPS ⦄ := by + cases grant with + | false => simp + | true => + simp only [↓reduceIte] + have hc : sw = 0#u64 ∨ tag.extra_caps = 0#u64 := by + rcases hcaps with h | h | h + · cases h + · exact Or.inl (hsw0.2 h) + · right + have h' : tag.extra_caps.bv.toNat = 0 := by rw [hext, hwmi]; exact h + exact UScalar.eq_of_val_eq h' + refine WP.spec_bind (lookup_extra_caps_noneA s snd sw tag hc) ?_ + rintro r rfl + simp + refine WP.spec_bind hex ?_ + rintro extra rfl + refine WP.spec_bind (copy_mrs_anyA hO hP s σ hR hSR hs hr hne hks hkr (buf_caseA hrw0 hrwB) hu + (buf_caseA hsw0 hswB) tag.length hle) ?_ + rintro ⟨n, s1⟩ ⟨σc, hcopy, hkcf, hRc, hSc, hAc, hn, hWc, hEc⟩ + simp only at hcopy hkcf hRc hSc hAc hn hWc + set x1 := copyRegsTcbA xr xs.tcbArch.tcbContext.userRegs (msg_registers.take tag.length.val) with hx1 + have hkc : σc.kheap (specPtr rcv) = some (.tcb x1) := by rw [hkcf]; simp + refine WP.spec_bind (transfer_caps_noextraA s1 tag ep rcv rbw) ?_ + rintro ⟨out, s2⟩ hout + simp only [Prod.mk.injEq] at hout + obtain ⟨rfl, h2⟩ := hout + subst s2 + show (do + let s3 ← msg.set_msg_info st s1 rcv + { label := tag.label, caps_unwrapped := 0#u64, extra_caps := 0#u64, length := n } + obj.Tcb.set_reg st rcv s3 layout.reg.BADGE badge) ⦃ _ ⦄ + refine WP.spec_bind (set_msg_info_specA s1 rcv tag.label n (by omega)) ?_ + rintro s3 ⟨v, hs3, hv⟩ + rw [set_reg_eqA, WP.spec_ok] + subst hs3 + -- the receive slots, at the state after the copy + have hmem : ∀ y, IsSlotA th y → L.mem s1 y = L.mem s y ∧ + L.mem s1 (core.num.U64.wrapping_add y 8#u64) = L.mem s (core.num.U64.wrapping_add y 8#u64) := by + intro y hy + have hal : Aligned y := by have := (IsSlotA.al32 hPl hy).1; unfold Aligned; omega + exact ⟨hAc y hal (not_rcv_bufA hr hra (hoff y hy).1), + hAc _ (wa_al hal (by simp)) (not_rcv_bufA hr hra (hoff y hy).2)⟩ + have hStc : StateRelA th (L.mem s1) σc := hSt.xferA hPl hmem hkr (copyRegsTcbA_other _ _ _) hkcf + have hVc : SlotsValidA th (L.mem s1) := slotsValid_xferA hV hmem + have hsl : ∀ rb, ipcBufOfA true xr = some rb → + ∃ sl, getReceiveSlots (Z := Unit) (specPtr rcv) (some rb) σc = ({(sl, σc)}, False) := by + intro rb hrb + have hm := hrwB rb hrb + exact getReceiveSlots_oneA hPl s1 σc hStc hVc htr (hR.buf (rbw, rb) hm 0#u64 (by decide)).2 + -- the spec + have hlenw : tag.length.bv = (dataToMessageInfo (xs.tcbArch.tcbContext.userRegs msg_info_register)).miLength := by + rw [hlen, hwmi] + rw [hlenw] at hcopy + have hspec := doIpcTransfer_xferA hks hkr hf epo badge.bv grant hcaps hcopy hkc hsl + -- the two register writes + obtain ⟨hR2, hW2, hS2⟩ := write_reg_rel hP s1 _ hRc hr hkc msg_info_register v + rw [regIdxA_msgInfo] at hR2 hW2 + have hkc2 : (updK σc (specPtr rcv) (.tcb (setReg x1 msg_info_register v.bv))).kheap (specPtr rcv) = + some (.tcb (setReg x1 msg_info_register v.bv)) := by simp + obtain ⟨hR3, hW3, hS3⟩ := write_reg_rel hP _ _ hR2 hr hkc2 badge_register badge + rw [regIdxA_badge] at hR3 hW3 + have hSR2 := hO.stable _ _ _ _ hSc hW2 hS2 (NoBlockE.tcbK hkc _ rfl rfl) + have hSR3 := hO.stable _ _ _ _ hSR2 hW3 hS3 (NoBlockE.tcbK hkc2 _ rfl rfl) + have hvx : v.bv = messageInfoToData ⟨n.bv, 0, 0, + (dataToMessageInfo (xs.tcbArch.tcbContext.userRegs msg_info_register)).miLabel⟩ := by + rw [hv]; congr 2; rw [hlab, hwmi]; rfl + have hfin : updK (updK σc (specPtr rcv) (.tcb (setReg x1 msg_info_register v.bv))) (specPtr rcv) + (.tcb (setReg (setReg x1 msg_info_register v.bv) badge_register badge.bv)) = + updK σc (specPtr rcv) (.tcb (setReg (setReg x1 msg_info_register (messageInfoToData + ⟨n.bv, 0, 0, (dataToMessageInfo (xs.tcbArch.tcbContext.userRegs msg_info_register)).miLabel⟩)) + badge_register badge.bv)) := by + simp only [updK, Function.update_idem]; rw [hvx] + rw [hfin] at hR3 hSR3 hS3 + refine ⟨_, hspec, hR3, hSR3, ?_, (hWc.trans (hW2.trans hW3)).mono fun b h => by + rcases h with h | h | h <;> exact h, hEc.trans (hS2.trans hS3), ?_, ?_⟩ + · intro b hb hn' + have hn1 : ¬ RcvMRA rcv b := fun ⟨r, _, h⟩ => hn'.1 ⟨r, h⟩ + rw [← regIdxA_badge, ← regIdxA_msgInfo, + rd_wr_ne (a_reg hra badge_register).1 hb (fun h => hn'.1 ⟨badge_register, by rw [← h]⟩), + rd_wr_ne (a_reg hra msg_info_register).1 hb (fun h => hn'.1 ⟨msg_info_register, by rw [← h]⟩)] + exact hAc b hb ⟨hn1, hn'.2⟩ + · intro p hp + rw [updK_kheap, Function.update_of_ne hp, hkcf, Function.update_of_ne hp] + · exact ⟨_, by rw [updK_kheap, Function.update_self], + setReg_withA (setReg_withA (copyRegsTcbA_other xr _ _) _ _) _ _⟩ + +end Fl4 + +end + +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/ExtMapFrA.lean b/proofs/Fl4ProofsA64/P3/ExtMapFrA.lean new file mode 100644 index 000000000..6c1a5ff42 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ExtMapFrA.lean @@ -0,0 +1,2169 @@ +/- +P3 (aarch64, A64-P3 S3 gen 39d): **Stage-2, `ArmHighVSpace` and `is_ept` `ARMPageMap`** — FL4's +`vspace::decode_page_map` (`vspace.rs` 731–785), `ept::map_frame` (`ept.rs` 196–245) and +`vspace::decode_high_page_map` (`vspace.rs` 787–852) against `fl4ArchDecodeInvocation` at label 49 +(`ARMPageMap`), `decodeS2PageMap` (`Ext/Stage2`) and `decodeHighPageMap` (`Ext/ArmHighVSpace`), +followed by `set_thread_state Restart` and `performExtVSpace (.map …)`, with `VsTopPostA`. + +* **Ext root checks**: `extRootCapA`, `checkExtRoot_mappedA`, `checkExtRoot_unmappedA`, `ExtRootPostA`, + `find_high_asidA`, `check_high_vspaceA`, `ept_rootA`. +* **Stage-2 leaf descriptor**: `s2LeafCA`, `s2LeafC_factsA`, `make_s2_leaf_specA`, `s2_leaf_relA` + (`ept::make_s2_leaf` against `DescRelE` / `S2AttrE` and `makeUserPte`; kernel-only rights give `0` / + `invalidPTE`). +* **Table/root updates & installs**: `ExtVsRelA.of_ptWriteMapE`, `ExtVsRelA.of_rootWriteMapE`, + `VsRelA.of_hiWriteMapE`, `ept_lookup_slot_mapA`, `lookup_pt_slot_hiA`, `install_s2_leaf_mapA`, + `install_page_hiA`. +* **Arms and combined top**: `s2_map_frame_jA` (`ept::map_frame` vs `decodeS2PageMapAt`), + `hi_map_frame_jA` (`decode_high_page_map` vs `decodeHighPageMap`), `page_map_all_jA` (all frame and + extra-cap combinations for `ARMPageMap`, retiring `NoExtXA` and `e = false` via `CapWfA`). + +Named premises: `ArchKeepsA` [P], `S2IpaE` [P] (standing). No new named premise. +-/ +import Fl4ProofsA64.P3.VsFlushA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## Ext root checks -/ + +section extRoot +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {Pp Tb X Y : Set U64} {Lv : U64 → Nat} {s : S} {σ : State Unit} + +def extRootCapA (k : ExtRootKind) (ptr : ObjRef) (asid : Option Asid) : Cap := + .archObjectCap (.ext (if k = .stage2 then .stage2VSpaceCap ptr asid else .highVSpaceCap ptr asid)) + +theorem checkExtRoot_mappedA {k : ExtRootKind} (hk : k ≠ .stage1) (ptr : ObjRef) (asid : Asid) (arg : Nat) : + (checkExtRoot (Z := Unit) k (extRootCapA k ptr (some asid)) arg).run σ = + ({((match extRootResX σ k asid with + | .error e => .error (.failedLookup false e) + | .ok r => if r = ptr then .ok (ptr, asid) else .error (.invalidCapability arg) : + Except SyscallError (ObjRef × Asid)), σ)}, False) := by + have h1 : (checkExtRoot (Z := Unit) k (extRootCapA k ptr (some asid)) arg) = (do + let r ← lookupErrorOnFailure false (findExtRoot k asid) + NonDetE.whenE (r ≠ ptr) (NonDetE.throwError (.invalidCapability arg)) + NonDetE.returnOk (ptr, asid)) := by + cases k with + | stage1 => exact (hk rfl).elim + | stage2 => rfl + | high => rfl + rw [h1] + rcases h : extRootResX σ k asid with e | r + · exact run_bind_errorA (lefo_errA (by rw [findExtRoot_runX, h])) _ + · rw [run_bind_okA (lefo_okA (by rw [findExtRoot_runX, h]))] + by_cases he : r = ptr + · subst r + simp only [if_pos rfl, NonDetE.whenE, ne_self_iff_false, ↓reduceIte, ndE_pure_bindA] + rfl + · simp only [NonDetE.whenE, ne_eq, he, not_false_eq_true, ↓reduceIte, NonDetE.throwError, ndE_throw_bindA] + rfl + +theorem checkExtRoot_unmappedA (k : ExtRootKind) (ptr : ObjRef) (arg : Nat) : + (checkExtRoot (Z := Unit) k (extRootCapA k ptr none) arg).run σ = + ({(.error (.invalidCapability arg), σ)}, False) := by + cases k <;> rfl + +theorem vspaceForAsid_of_checkExtRootA {k : ExtRootKind} {ptr asid : U64} {mp : Bool} {arg : Nat} + (hk : k ≠ .stage1) + (h : (checkExtRoot (Z := Unit) k (extRootCapA k (specPtr ptr) (asidDataA mp asid)) arg).run σ = + ({(.ok (specPtr ptr, asidOfA asid), σ)}, False)) : + vspaceForAsid (asidOfA asid) σ = some (specPtr ptr) := by + cases mp + · rw [asidDataA, if_neg Bool.false_ne_true, checkExtRoot_unmappedA] at h + have e := congrArg Prod.fst h + simp only [Set.singleton_eq_singleton_iff, Prod.mk.injEq] at e + cases e.1 + · rw [asidDataA, if_pos rfl, checkExtRoot_mappedA hk] at h + rcases hr : extRootResX σ k (asidOfA asid) with e | r + · rw [hr] at h; dsimp only at h + have e' := congrArg Prod.fst h + simp only [Set.singleton_eq_singleton_iff, Prod.mk.injEq] at e' + cases e'.1 + · rw [hr] at h; dsimp only at h + by_cases he : r = specPtr ptr + · subst he; exact (extRootResX_okE hr).1 + · rw [if_neg he] at h + have e' := congrArg Prod.fst h + simp only [Set.singleton_eq_singleton_iff, Prod.mk.injEq] at e' + cases e'.1 + +def ExtRootPostA (k : ExtRootKind) (ptr asid arg : U64) (mp : Bool) (roots : Set U64) + (σ : State Unit) (r : core.result.Result (U64 × U64) error.Exc) : Prop := + let D := (checkExtRoot (Z := Unit) k (extRootCapA k (specPtr ptr) (asidDataA mp asid)) arg.val).run σ + match r with + | .Ok v => v = (ptr, asid) ∧ ptr ∈ roots ∧ asid ≠ 0#u64 ∧ + D = ({(.ok (specPtr ptr, asidOfA asid), σ)}, False) + | .Err e => ∃ e', D = ({(.error e', σ)}, False) ∧ VsErrRelA e e' + +private theorem ept_find_err_loc {asid : U64} (h16 : asid.val < 65536) : + ept.find st s asid ⦃ r => ∀ e, r = .Err e → e = .InvalidRoot ⦄ := by + unfold ept.find + refine WP.spec_bind (find_asid_raw_errA s h16) fun r hr => ?_ + rcases r with w | e + · simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok, lift] + split + · exact (WP.spec_ok _).2 fun e h => by injection h with h; exact h.symm + · exact (WP.spec_ok _).2 fun e h => by cases h + · simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame.from, + bind_tc_ok, WP.spec_ok] + intro e' h + injection h with h + exact hr e rfl |> fun he => h ▸ he + +theorem find_high_asidA + (R : VsRelA Pp Tb Lv X Y (L.mem s) (L.ksb s) σ) + (hX : ∀ r ∈ X, ∃ es, σ.kheap (specPtr r) = some (.archObj (.ext (.stage2Root es)))) + {asid : U64} (h16 : asid.val < 65536) : + vspace.find_high_asid st s asid ⦃ r => + match r with + | .Ok w => w ∈ Y ∧ extRootResX σ .high (asidOfA asid) = .ok (specPtr w) + | .Err e => e = .InvalidRoot ∧ extRootResX σ .high (asidOfA asid) = .error .invalidRoot ⦄ := by + obtain ⟨r, hr, hraw, herr⟩ := spec_ex_fd (WP.spec_and (find_asid_rawA (st := st) R h16) + (find_asid_raw_errA (st := st) s h16)) + unfold vspace.find_high_asid + rw [hr, bind_ok] + rcases r with w | e + · obtain ⟨hs, hk⟩ := rawKindE R hX hraw + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok, lift, rtag, rhigh] + by_cases h2 : tagA w = 2 + · rw [(tagNE w 2 2#u64 rfl).2 h2] + simp only [bne_self_eq_false, Bool.false_eq_true, ↓reduceIte, bind_ok, lift, untagE, WP.spec_ok] + rcases hk with ⟨h0, -⟩ | ⟨h1, -, -⟩ | ⟨-, hy, hk⟩ + · omega + · omega + · exact ⟨hy, extRootResX_okX hs hk⟩ + · have ne : (w &&& 3#u64) ≠ 2#u64 := fun e => h2 ((tagNE w 2 2#u64 rfl).1 e) + rw [if_pos (bne_iff_ne.2 ne)] + refine (WP.spec_ok _).2 ⟨rfl, extRootResX_errX hs ?_⟩ + rcases hk with ⟨-, hk⟩ | ⟨-, -, hk⟩ | ⟨h, -, -⟩ + · rw [hk]; decide + · rw [hk]; decide + · exact (h2 h).elim + · simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame.from, + bind_tc_ok, WP.spec_ok] + have hn : vspaceForAsid (asidOfA asid) σ = none := hraw + exact ⟨herr _ rfl, by simp [extRootResX, hn]⟩ + +private theorem extRootPost_invalidA {k : ExtRootKind} {p asid arg : U64} {roots : Set U64} + {mp : Bool} (h : (checkExtRoot (Z := Unit) k (extRootCapA k (specPtr p) (asidDataA mp asid)) + arg.val).run σ = ({(.error (.invalidCapability arg.val), σ)}, False)) : + ExtRootPostA k p asid arg mp roots σ (.Err (.Sys (.InvalidCapability arg))) := + ⟨_, h, Or.inr ⟨arg, rfl, rfl⟩⟩ + +theorem check_high_vspaceA + (R : VsRelA Pp Tb Lv X Y (L.mem s) (L.ksb s) σ) + (hX : ∀ r ∈ X, ∃ es, σ.kheap (specPtr r) = some (.archObj (.ext (.stage2Root es)))) + (p asid arg : U64) (mp : Bool) (h16 : asid.val < 65536) : + vspace.check_high_vspace st s (.HighVSpace p mp asid) arg ⦃ r => + ExtRootPostA .high p asid arg mp Y σ r ⦄ := by + cases mp + · exact (WP.spec_ok _).2 (extRootPost_invalidA (checkExtRoot_unmappedA .high (specPtr p) arg.val)) + simp only [vspace.check_high_vspace, ↓reduceIte] + refine WP.spec_bind (find_high_asidA R hX h16) fun r hr => ?_ + have hd := checkExtRoot_mappedA (σ := σ) (k := .high) (by decide) (specPtr p) (asidOfA asid) arg.val + rcases r with w | e + · simp only [hr.2] at hd + dsimp only + by_cases he : w = p + · subst w + rw [if_neg (by simp)] + exact (WP.spec_ok _).2 ⟨rfl, hr.1, fun hz => by + subst asid + simp [extRootResX, asidOfA, vspaceForAsid_zeroA] at hr, by simpa only [asidDataA, if_true, if_pos rfl] using hd⟩ + · rw [if_pos (bne_iff_ne.2 he)] + exact (WP.spec_ok _).2 (extRootPost_invalidA (by + simpa only [asidDataA, if_true, if_neg (fun h => he (specPtr_inj h))] using hd)) + · cases hr.1 + exact (WP.spec_ok _).2 ⟨.failedLookup false .invalidRoot, by simpa only [asidDataA, if_true, hr.2] using hd, Or.inl ⟨rfl, trivial⟩⟩ + +theorem ept_rootA + (R : VsRelA Pp Tb Lv X Y (L.mem s) (L.ksb s) σ) + (hX : ∀ r ∈ X, ∃ es, σ.kheap (specPtr r) = some (.archObj (.ext (.stage2Root es)))) + (p asid va arg : U64) (mp : Bool) (h16 : asid.val < 65536) : + ept.root st s (.Ept p 0#u64 mp asid va) arg ⦃ r => ExtRootPostA .stage2 p asid arg mp X σ r ⦄ := by + cases mp + · exact (WP.spec_ok _).2 (extRootPost_invalidA (checkExtRoot_unmappedA .stage2 (specPtr p) arg.val)) + simp only [ept.root, ↓reduceIte] + refine WP.spec_bind (WP.spec_and (ept_findE R hX h16) (ept_find_err_loc (st := st) (s := s) h16)) + fun r ⟨⟨hok, herr⟩, hErr⟩ => ?_ + have hd := checkExtRoot_mappedA (σ := σ) (k := .stage2) (by decide) (specPtr p) (asidOfA asid) arg.val + rcases r with w | e + · obtain ⟨hw, hD⟩ := hok w rfl + rw [show (BitVec.ofNat 16 asid.val) = asidOfA asid from rfl] at hD + simp only [hD] at hd + simp only [core.result.Result.map_err, bind_ok, core.result.Result.Insts.CoreOpsTry.branch] + by_cases he : w = p + · subst w + rw [if_neg (by simp)] + exact (WP.spec_ok _).2 ⟨rfl, hw, fun hz => by + subst asid + simp [extRootResX, asidOfA, vspaceForAsid_zeroA] at hD, by simpa only [asidDataA, if_true, if_pos rfl] using hd⟩ + · rw [if_pos (bne_iff_ne.2 he)] + exact (WP.spec_ok _).2 (extRootPost_invalidA (by + simpa only [asidDataA, if_true, if_neg (fun h => he (specPtr_inj h))] using hd)) + · have hD := herr e rfl + rw [show (BitVec.ofNat 16 asid.val) = asidOfA asid from rfl] at hD + rw [hD] at hd + cases hErr e rfl + simp only [core.result.Result.map_err, bind_ok, ept.root.closure.Insts.CoreOpsFunctionFnOnceTupleLookupFaultExc, + ept.root.closure.Insts.CoreOpsFunctionFnOnceTupleLookupFaultExc.call_once, + core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame.from, bind_tc_ok] + exact (WP.spec_ok _).2 ⟨.failedLookup false .invalidRoot, hd, Or.inl ⟨rfl, trivial⟩⟩ + +end extRoot + +/-! ## Stage-2 leaf descriptor -/ + +section s2Leaf + +/-- The constant (non-address) bits of `ept.make_s2_leaf` for `rw` (true = RW, false = RO), `cache` +(true = normal WB + inner shareable, false = device), `pg` (true = 4 KiB page), `xn` (execute-never). -/ +def s2LeafCA (rw cache pg xn : Bool) : Nat := + (if pg then 3 else 1) ||| (if cache then 60 ||| 768 else 0) ||| + (if rw then 192 else 64) ||| 1024 ||| (if xn then 18014398509481984 else 0) + +theorem s2LeafC_factsA (rw cache pg xn : Bool) : + (s2LeafCA rw cache pg xn &&& 281474976706560 = 0) ∧ + (s2LeafCA rw cache pg xn).testBit 0 = true ∧ + (s2LeafCA rw cache pg xn).testBit 1 = pg ∧ + (s2LeafCA rw cache pg xn / 64) % 4 = (if rw then 3 else 1) ∧ + (s2LeafCA rw cache pg xn / 4) % 16 = (if cache then 15 else 0) ∧ + (s2LeafCA rw cache pg xn).testBit 54 = xn := by + cases rw <;> cases cache <;> cases pg <;> cases xn <;> decide + +theorem make_s2_leaf_zeroA (paddr vm attr shift : U64) (cr : Bool) + (h1 : vm ≠ capability.VM_READ_WRITE) (h2 : vm ≠ capability.VM_READ_ONLY) : + ept.make_s2_leaf paddr vm cr attr shift = ok 0#u64 := by + unfold ept.make_s2_leaf + rw [if_neg h1, if_neg h2] + +set_option maxHeartbeats 1000000 in +theorem make_s2_leaf_valA (paddr vm attr shift : U64) (cr : Bool) + (hvm : vm = capability.VM_READ_WRITE ∨ vm = capability.VM_READ_ONLY) : + ept.make_s2_leaf paddr vm cr attr shift ⦃ e => + e.val = (paddr.val &&& 281474976706560) ||| + s2LeafCA (decide (vm = capability.VM_READ_WRITE)) (attr.bv.getLsbD 0) + (decide (shift = 12#u64)) (attr.bv.getLsbD 2 || !cr) ⦄ := by + have hxn : vspace.exec_never attr cr = ok (attr.bv.getLsbD 2 || !cr) := by + unfold vspace.exec_never + simp only [lift, bind_ok] + rw [and_pow_neA attr 2 (by decide) _ (by unfold fl4_abi.consts.arm_vm_attr.EXECUTE_NEVER; decide)] + cases attr.bv.getLsbD 2 <;> cases cr <;> rfl + have hv : ept.ADDR_MASK = vspace.ADDR_MASK := by unfold ept.ADDR_MASK vspace.ADDR_MASK; rfl + have e12 : ept.PAGE_BITS = 12#u64 := by unfold ept.PAGE_BITS; rfl + have hor : ∀ a b c : U64, (a ||| b) ||| c = a ||| (b ||| c) := fun a b c => + UScalar.eq_of_val_eq (by simp only [UScalar.val_or, Nat.lor_assoc]) + have hne : capability.VM_READ_ONLY ≠ capability.VM_READ_WRITE := by + unfold capability.VM_READ_ONLY capability.VM_READ_WRITE; decide + unfold ept.make_s2_leaf + rcases hvm with rfl | rfl + · rw [if_pos rfl] + simp only [eq_of_spec_fd P0.Consts.ept.S2_AP_RW.spec, hxn, + eq_of_spec_fd P0.Consts.ept.S2_MEMATTR_NORMAL_WB.spec, eq_of_spec_fd P0.Consts.ept.S2_SH_INNER.spec, + eq_of_spec_fd P0.Consts.ept.S2_MEMATTR_DEVICE.spec, eq_of_spec_fd P0.Consts.ept.D_VALID.spec, + eq_of_spec_fd P0.Consts.ept.D_TABLE_OR_PAGE.spec, eq_of_spec_fd P0.Consts.ept.S2_AF.spec, + eq_of_spec_fd P0.Consts.ept.S2_XN.spec, lift, bind_ok, hv, e12, decide_true] + rw [and_pow_neA attr 0 (by decide) _ (by unfold fl4_abi.consts.arm_vm_attr.PAGE_CACHEABLE; decide)] + generalize attr.bv.getLsbD 0 = b0 + generalize (attr.bv.getLsbD 2 || !cr) = xn + by_cases hs : shift = 12#u64 + · simp only [hs, decide_true, ↓reduceIte] + cases b0 <;> cases xn <;> + (simp only [Bool.false_eq_true, ↓reduceIte, bind_ok, hor] + refine (WP.spec_ok _).2 ?_ + rw [UScalar.val_or, UScalar.val_and, Fl4ProofsA64.P0.Consts.vspace.ADDR_MASK.val_eq] + congr 1) + all_goals decide + · simp only [hs, decide_false, ↓reduceIte] + cases b0 <;> cases xn <;> + (simp only [Bool.false_eq_true, ↓reduceIte, bind_ok, hor] + refine (WP.spec_ok _).2 ?_ + rw [UScalar.val_or, UScalar.val_and, Fl4ProofsA64.P0.Consts.vspace.ADDR_MASK.val_eq] + congr 1) + all_goals decide + · rw [if_neg hne, if_pos rfl] + simp only [eq_of_spec_fd P0.Consts.ept.S2_AP_RO.spec, hxn, + eq_of_spec_fd P0.Consts.ept.S2_MEMATTR_NORMAL_WB.spec, eq_of_spec_fd P0.Consts.ept.S2_SH_INNER.spec, + eq_of_spec_fd P0.Consts.ept.S2_MEMATTR_DEVICE.spec, eq_of_spec_fd P0.Consts.ept.D_VALID.spec, + eq_of_spec_fd P0.Consts.ept.D_TABLE_OR_PAGE.spec, eq_of_spec_fd P0.Consts.ept.S2_AF.spec, + eq_of_spec_fd P0.Consts.ept.S2_XN.spec, lift, bind_ok, hv, e12, decide_eq_false hne] + rw [and_pow_neA attr 0 (by decide) _ (by unfold fl4_abi.consts.arm_vm_attr.PAGE_CACHEABLE; decide)] + generalize attr.bv.getLsbD 0 = b0 + generalize (attr.bv.getLsbD 2 || !cr) = xn + by_cases hs : shift = 12#u64 + · simp only [hs, decide_true, ↓reduceIte] + cases b0 <;> cases xn <;> + (simp only [Bool.false_eq_true, ↓reduceIte, bind_ok, hor] + refine (WP.spec_ok _).2 ?_ + rw [UScalar.val_or, UScalar.val_and, Fl4ProofsA64.P0.Consts.vspace.ADDR_MASK.val_eq] + congr 1) + all_goals decide + · simp only [hs, decide_false, ↓reduceIte] + cases b0 <;> cases xn <;> + (simp only [Bool.false_eq_true, ↓reduceIte, bind_ok, hor] + refine (WP.spec_ok _).2 ?_ + rw [UScalar.val_or, UScalar.val_and, Fl4ProofsA64.P0.Consts.vspace.ADDR_MASK.val_eq] + congr 1) + all_goals decide + +theorem s2_leaf_relA {G : U64 → Nat → Prop} {e paddr vm attr : U64} {sz : capability.FrameSize} + {cr : Bool} {R : VMRights} (hcr : cr = decide (Rights.allowRead ∈ R)) + (hvm : vm = capability.VM_READ_WRITE ∨ vm = capability.VM_READ_ONLY) + (he : e.val = (paddr.val &&& 281474976706560) ||| + s2LeafCA (decide (vm = capability.VM_READ_WRITE)) (attr.bv.getLsbD 0) + (decide (psA sz = 12#u64)) (attr.bv.getLsbD 2 || !cr)) + (hal : paddr.val % 2 ^ (psA sz).val = 0) (hpa : paddr.val < 2 ^ 44) : + e ≠ 0#u64 ∧ + DescRelE G (psA sz).val e (makeUserPte paddr.bv (attribsFromWord attr.bv) (vmRightsOfA vm) R (frameSizeOfA sz)) ∧ + S2AttrE (psA sz).val e (makeUserPte paddr.bv (attribsFromWord attr.bv) (vmRightsOfA vm) R (frameSizeOfA sz)) := by + obtain ⟨hC, h0, h1, hap, hma, h54⟩ := s2LeafC_factsA (decide (vm = capability.VM_READ_WRITE)) + (attr.bv.getLsbD 0) (decide (psA sz = 12#u64)) (attr.bv.getLsbD 2 || !cr) + obtain ⟨hy1, hy2⟩ := landM_A paddr.val + have hlow := orC_lowA (C := s2LeafCA (decide (vm = capability.VM_READ_WRITE)) (attr.bv.getLsbD 0) + (decide (psA sz = 12#u64)) (attr.bv.getLsbD 2 || !cr)) hy1 + rw [← he] at hlow + have tb : ∀ i, dBitA e i = e.val.testBit i := fun _ => rfl + have hb0 : dBitA e 0 = true := by rw [tb, he, Nat.testBit_or, tbM_lowA hy1 (by norm_num), h0]; rfl + have hb1 : dBitA e 1 = decide (psA sz = 12#u64) := by + rw [tb, he, Nat.testBit_or, tbM_lowA hy1 (by norm_num), h1]; simp + have h12 : 12 ≤ (psA sz).val := by rcases psA_valA sz with h | h | h <;> omega + have hal12 : paddr.val % 4096 = 0 := by + have hd : (4096 : Nat) ∣ 2 ^ (psA sz).val := by + rw [show (4096 : Nat) = 2 ^ 12 by norm_num]; exact Nat.pow_dvd_pow 2 h12 + rw [← Nat.mod_mod_of_dvd paddr.val hd, hal, Nat.zero_mod] + have hd : dAddrA e = paddr := UScalar.eq_of_val_eq (by + show (e &&& 281474976706560#u64).val = paddr.val + rw [UScalar.val_and, he, show (281474976706560#u64 : U64).val = 281474976706560 from rfl, + orC_MA hy1 hy2 hC, nat_and_M hal12 (by omega)]) + have hapE : apA e = if decide (vm = capability.VM_READ_WRITE) then 3 else 1 := by + unfold apA; rw [← hap]; omega + have hmaE : (e.val / 4) % 16 = if attr.bv.getLsbD 0 then 15 else 0 := by + rw [← hma]; omega + have h54E : dBitA e 54 = (attr.bv.getLsbD 2 || !cr) := by + rw [tb, he, orC_hiA hy2 (by norm_num), h54] + have hne0 : e ≠ 0#u64 := fun hz => by subst hz; simp [dBitA] at hb0 + have hsm : decide (frameSizeOfA sz = .armSmallPage) = decide ((psA sz).val = 12) := by + cases sz <;> decide + have hpg : decide (psA sz = 12#u64) = decide ((psA sz).val = 12) := by + cases sz <;> decide + have hbase : paddr.bv = baseShE e (psA sz).val := by + unfold baseShE + rw [hd] + apply BitVec.eq_of_toNat_eq + have hlt : (psA sz).val < 64 := by rcases psA_valA sz with h | h | h <;> omega + have hpow : 2 ^ (psA sz).val < 2 ^ 64 := Nat.pow_lt_pow_right (by norm_num) hlt + have hm : (~~~(BitVec.ofNat 64 (2 ^ (psA sz).val - 1))).toNat = 2 ^ 64 - 2 ^ (psA sz).val := by + rw [BitVec.toNat_not, BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)] + omega + rw [BitVec.toNat_and, hm] + show paddr.val = paddr.val &&& (2 ^ 64 - 2 ^ (psA sz).val) + apply Nat.eq_of_testBit_eq + intro i + rw [Nat.testBit_and] + by_cases hi : i < (psA sz).val + · have hp0 : paddr.val.testBit i = false := by + have hd : 2 ^ (i + 1) ∣ 2 ^ (psA sz).val := Nat.pow_dvd_pow 2 (by omega) + have hmod : paddr.val % 2 ^ (i + 1) = 0 := by + rw [← Nat.mod_mod_of_dvd paddr.val hd, hal, Nat.zero_mod] + rw [Nat.testBit_eq_decide_div_mod_eq] + have : paddr.val / 2 ^ i % 2 = 0 := by + have : paddr.val = (paddr.val / 2 ^ (i + 1)) * 2 ^ (i + 1) := + (Nat.div_mul_cancel (Nat.dvd_of_mod_eq_zero hmod)).symm + rw [this, Nat.pow_succ, Nat.mul_comm (2 ^ i) 2, ← Nat.mul_assoc, + Nat.mul_div_cancel _ (Nat.two_pow_pos i)] + omega + simp [this] + rw [hp0, Bool.false_and] + · by_cases hi64 : i < 64 + · have h1b : (2 ^ 64 - 2 ^ (psA sz).val).testBit i = true := by + have hsub : 2 ^ 64 - 2 ^ (psA sz).val = (2 ^ (64 - (psA sz).val) - 1) * 2 ^ (psA sz).val := by + rw [Nat.sub_mul, Nat.one_mul, ← Nat.pow_add, Nat.sub_add_cancel (by omega)] + rw [hsub, Nat.testBit_mul_two_pow, decide_eq_true (by omega), Bool.true_and, + Nat.testBit_two_pow_sub_one, decide_eq_true (by omega)] + rw [h1b, Bool.and_true] + · have hp0 : paddr.val.testBit i = false := + Nat.testBit_eq_false_of_lt (lt_of_lt_of_le paddr.bv.isLt (Nat.pow_le_pow_right (by norm_num) (by omega))) + rw [hp0, Bool.false_and] + have hrt : vmRightsOfA vm = s2RightsE e := by + unfold s2RightsE + rw [hapE] + rcases hvm with rfl | rfl + · simp [vmRW_litA, vmRightsOfA] + · have hne : capability.VM_READ_ONLY ≠ capability.VM_READ_WRITE := by + unfold capability.VM_READ_ONLY capability.VM_READ_WRITE; decide + simp [hne, vmRO_litA, vmRightsOfA] + refine ⟨hne0, ?_, ?_⟩ + · unfold makeUserPte + refine ⟨hb0, ?_, hbase, by rw [hd]; exact hpa⟩ + rw [hb1, hpg, decide_eq_true_iff] + · unfold makeUserPte + have hExec : VMAttribute.execute ∈ attribsFromWord attr.bv ↔ attr.bv.getLsbD 2 = false := by + simp [attribsFromWord, Fl4SpecA64.testBit] + have hDev : VMAttribute.device ∈ attribsFromWord attr.bv ↔ attr.bv.getLsbD 0 = false := by + simp [attribsFromWord, Fl4SpecA64.testBit] + refine ⟨hsm, hrt, ?_, ?_, ?_⟩ + · rw [h54E] + by_cases hR : Rights.allowRead ∈ R + · have hcr1 : cr = true := by rw [hcr]; exact decide_eq_true hR + rw [hcr1]; simp [hR, hExec] + · have hcr0 : cr = false := by rw [hcr]; exact decide_eq_false hR + rw [hcr0]; simp [hR] + · rw [hmaE] + have hmem : VMAttribute.device ∈ (attribsFromWord attr.bv \ {VMAttribute.global}) \ + (if Rights.allowRead ∉ R then {VMAttribute.execute} else ∅) ↔ + VMAttribute.device ∈ attribsFromWord attr.bv := by + by_cases hr : Rights.allowRead ∉ R <;> simp [hr] + rw [hmem, hDev] + cases attr.bv.getLsbD 0 <;> simp + · simp + +end s2Leaf + +/-! ## Table/root updates, walks and installs -/ + +section extMap +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {Pp Tb X Y T2 : Set U64} {Lv : U64 → Nat} {Sh2 : U64 → Nat} {s : S} {σ : State Unit} + +theorem ExtVsRelA.of_ptWriteMapE {mem mem' : U64 → U64} {word : U64} {pte : PTE} + (E : ExtVsRelA Pp Tb X Y T2 Sh2 mem σ) + (hY : ∀ y ∈ Y, 8192 ∣ y.val) {t0 i0 : U64} (ht0 : t0 ∈ T2) (hi0 : i0.val < 512) + (hpte : DescRelE (G2E T2 Sh2) (Sh2 t0) word pte ∧ S2AttrE (Sh2 t0) word pte) + (hm : ∀ b, Aligned b → mem' b = if peAF t0 i0 = b then word else mem b) + (hch : ∀ ppn, pte = .pageTablePTE ppn → dTabA word ≠ t0 ∧ + ∀ j : U64, j.val < 512 → dBitA (mem (peAF (dTabA word) j)) 0 = false ∧ + ∀ pt1, σ.kheap (specPtr (dTabA word)) = some (.archObj (.pageTable pt1)) → + Fl4SpecA64.ptApply pt1 j.bv = .invalidPTE) : + ExtVsRelA Pp Tb X Y T2 Sh2 mem' (extSlotWriteX σ (.pt (specPtr (peAF t0 i0))) pte) := by + have ha0 := E.tal t0 ht0 + have ha0' : t0.val % 4096 = 0 := Nat.mod_eq_zero_of_dvd ha0 + obtain ⟨pt0, hk0, hty0, he0⟩ := E.s2pt t0 ht0 + rw [extPtSetE hk0 ha0' hi0] + have hko : ∀ q, q ≠ t0 → (ptSetA σ (specPtr t0) (Fl4SpecA64.ptUpd pt0 i0.bv pte)).kheap (specPtr q) = + σ.kheap (specPtr q) := fun q hq => by rw [ptSetA_kheap, if_neg (fun e => hq (specPtr_inj e))] + have hw : ∀ b, ExtFootE X Y T2 b → b ≠ peAF t0 i0 → mem' b = mem b := fun b hb hne => by + rw [hm b (E.foot_alignedE hY b hb), if_neg (Ne.symm hne)] + have hActS2 : ∀ {r}, ActiveS2RootA Pp X (ptSetA σ (specPtr t0) (Fl4SpecA64.ptUpd pt0 i0.bv pte)) r → + ActiveS2RootA Pp X σ r := fun ha => + ha.of_spec (σ' := ptSetA σ (specPtr t0) (Fl4SpecA64.ptUpd pt0 i0.bv pte)) rfl + fun p hp => hko p (fun e => (E.pgd t0 (Or.inl ht0)).1 (e ▸ Or.inl hp)) + have hReachBack : ∀ {q}, + S2ReachA Pp X T2 Sh2 mem' (ptSetA σ (specPtr t0) (Fl4SpecA64.ptUpd pt0 i0.bv pte)) q → + S2ReachA Pp X T2 Sh2 mem σ q ∨ (∃ ppn, pte = .pageTablePTE ppn ∧ q = dTabA word) := by + intro q hq + induction hq with + | root ha => exact Or.inl (.root (hActS2 ha)) + | @stepRoot r i ha hi h3 ht2 => + have ha' := hActS2 ha + have hi1k : i.val < 1024 := by have := s2GeomNumE.2.2.1; omega + obtain ⟨n1, n2⟩ := E.t2x t0 ht0 r ha'.1 + rw [hw _ (rootFootE ha'.1 (E.xal r ha'.1) hi1k) + (Ne.symm (rootNeE (E.xal r ha'.1) hi1k ha0 hi0 n1 n2))] at h3 ht2 ⊢ + exact Or.inl (.stepRoot ha' hi h3 ht2) + | @stepPt t i _ ht h12 hi h3 ht2 ih => + rcases ih with hpar | ⟨ppn, hpte', rfl⟩ + · by_cases heq : peAF t i = peAF t0 i0 + · obtain ⟨rfl, rfl⟩ := peAF_injA (Nat.mod_eq_zero_of_dvd (E.tal t ht)) ha0' hi hi0 heq + rw [hm _ (peAF_alignedA ha0' hi), if_pos rfl] at h3 ht2 ⊢ + obtain ⟨hb0, hb1⟩ := (and3_bitsA word).1 h3 + cases pte with + | invalidPTE => exact absurd (hpte.1.symm.trans hb0) Bool.false_ne_true + | pageTablePTE ppn => exact Or.inr ⟨ppn, rfl, rfl⟩ + | pagePTE _ _ _ _ => + have := hpte.1.2.1.1 hb1; omega + · rw [hw _ (.s2pt t i ht hi) heq] at h3 ht2 ⊢ + exact Or.inl (.stepPt hpar ht h12 hi h3 ht2) + · subst hpte' + obtain ⟨hdne, hinv⟩ := hch ppn rfl + have hdT2 : dTabA word ∈ T2 := hpte.1.2.2.2.2.2.1 + have heqN : peAF (dTabA word) i ≠ peAF t0 i0 := pgNeE (E.tal _ hdT2) ha0 hi hi0 hdne + rw [hw _ (.s2pt (dTabA word) i hdT2 hi) heqN] at h3 + obtain ⟨hb0, -⟩ := (and3_bitsA _).1 h3 + exact absurd ((hinv i hi).1.symm.trans hb0) Bool.false_ne_true + refine ⟨?_, ?_, E.tsh, ?_, ?_, E.xal, E.tal, E.pgd, E.t2x, E.ypg, E.ypp⟩ + · intro r hr + obtain ⟨es, hk, he⟩ := E.s2root r hr + obtain ⟨n1, n2⟩ := E.t2x t0 ht0 r hr + refine ⟨es, by rw [hko r (Ne.symm n1)]; exact hk, fun ha i hi hmi => ?_⟩ + rw [hw _ (rootFootE hr (E.xal r hr) hi) (Ne.symm (rootNeE (E.xal r hr) hi ha0 hi0 n1 n2))] + exact he (hActS2 ha) i hi hmi + · intro t ht + by_cases htt : t = t0 + · subst htt + refine ⟨Fl4SpecA64.ptUpd pt0 i0.bv pte, by rw [ptSetA_kheap, if_pos rfl], by rw [ptUpd_typeA]; exact hty0, + fun hr j hj => ?_⟩ + rw [ptApply_updA pt0 hi0 hj] + by_cases hji : j = i0 + · subst hji + rw [if_pos rfl, hm _ (peAF_alignedA ha0' hj), if_pos rfl] + exact hpte + · rw [if_neg hji, hw _ (.s2pt t j ht hj) (fun e => hji (peAF_injA ha0' ha0' hj hi0 e).2)] + rcases hReachBack hr with hpar | ⟨ppn, hpte', hdw⟩ + · exact he0 hpar j hj + · exact absurd hdw.symm (hch ppn hpte').1 + · obtain ⟨pt, hk, hty, he⟩ := E.s2pt t ht + refine ⟨pt, by rw [hko t htt]; exact hk, hty, fun hr j hj => ?_⟩ + rw [hw _ (.s2pt t j ht hj) (pgNeE (E.tal t ht) ha0 hj hi0 htt)] + rcases hReachBack hr with hpar | ⟨ppn, hpte', rfl⟩ + · exact he hpar j hj + · obtain ⟨-, hinv⟩ := hch ppn hpte' + rw [(hinv j hj).2 pt hk] + exact ⟨(hinv j hj).1, trivial⟩ + · intro y hy + have hne : y ≠ t0 := fun e => (E.pgd t0 (Or.inl ht0)).2 (e ▸ hy) + rw [hw _ (.wl0 y hy) (pgNeE (al4kE (hY y hy)) ha0 (by decide) hi0 hne)] + exact E.wl0 y hy + · intro y hy i hi + have hne : y ≠ t0 := fun e => (E.pgd t0 (Or.inl ht0)).2 (e ▸ hy) + rw [hw _ (.wlo y i hy hi) (pgNeE (al4kE (hY y hy)) ha0 (by omega) hi0 hne)] + exact E.wlo y hy i hi + +theorem ExtVsRelA.of_rootWriteMapE {mem mem' : U64 → U64} {word : U64} {pte : PTE} + (E : ExtVsRelA Pp Tb X Y T2 Sh2 mem σ) + (hY : ∀ y ∈ Y, 8192 ∣ y.val) {r0 j0 : U64} (hr0 : r0 ∈ X) (hj0 : j0.val < 1024) + (hpte : DescRelE (G2E T2 Sh2) s2Geom.topShift word pte ∧ S2AttrE s2Geom.topShift word pte) + (hm : ∀ b, Aligned b → mem' b = if peAF r0 j0 = b then word else mem b) + (hch : ∀ ppn, pte = .pageTablePTE ppn → + ∀ j : U64, j.val < 512 → dBitA (mem (peAF (dTabA word) j)) 0 = false ∧ + ∀ pt1, σ.kheap (specPtr (dTabA word)) = some (.archObj (.pageTable pt1)) → + Fl4SpecA64.ptApply pt1 j.bv = .invalidPTE) : + ExtVsRelA Pp Tb X Y T2 Sh2 mem' (extSlotWriteX σ (.root (specPtr r0) j0.val) pte) := by + have ha0 := E.xal r0 hr0 + obtain ⟨es0, hk0, he0⟩ := E.s2root r0 hr0 + have hk1 := extRootSetE hk0 j0.val pte + have hko : ∀ q, q ≠ r0 → (extSlotWriteX σ (.root (specPtr r0) j0.val) pte).kheap (specPtr q) = + σ.kheap (specPtr q) := fun q hq => kheap_writeX (fun e => hq (specPtr_inj e)) + have hw : ∀ b, ExtFootE X Y T2 b → b ≠ peAF r0 j0 → mem' b = mem b := fun b hb hne => by + rw [hm b (E.foot_alignedE hY b hb), if_neg (Ne.symm hne)] + have hActS2 : ∀ {r}, ActiveS2RootA Pp X (extSlotWriteX σ (.root (specPtr r0) j0.val) pte) r → + ActiveS2RootA Pp X σ r := fun ha => + ha.of_spec (σ' := extSlotWriteX σ (.root (specPtr r0) j0.val) pte) rfl + fun p hp => hko p (fun e => (E.pgd r0 (Or.inr ⟨r0, hr0, Or.inl rfl⟩)).1 (e ▸ Or.inl hp)) + have hReachBack : ∀ {q}, + S2ReachA Pp X T2 Sh2 mem' (extSlotWriteX σ (.root (specPtr r0) j0.val) pte) q → + S2ReachA Pp X T2 Sh2 mem σ q ∨ (∃ ppn, pte = .pageTablePTE ppn ∧ q = dTabA word) := by + intro q hq + induction hq with + | root ha => exact Or.inl (.root (hActS2 ha)) + | @stepRoot r i ha hi h3 ht2 => + have ha' := hActS2 ha + have hi1k : i.val < 1024 := by have := s2GeomNumE.2.2.1; omega + by_cases heq : peAF r i = peAF r0 j0 + · by_cases hrr : r = r0 + · subst hrr + have hij := peAF_1kInjE ha0 hi1k hj0 heq + subst hij + rw [hm _ (E.foot_alignedE hY _ (rootFootE hr0 ha0 hj0)), if_pos rfl] at h3 ht2 ⊢ + obtain ⟨hb0, hb1⟩ := (and3_bitsA word).1 h3 + cases pte with + | invalidPTE => exact absurd (hpte.1.symm.trans hb0) Bool.false_ne_true + | pageTablePTE ppn => exact Or.inr ⟨ppn, rfl, rfl⟩ + | pagePTE _ _ _ _ => + have h12 : s2Geom.topShift = 12 := hpte.1.2.1.1 hb1 + rcases s2GeomNumE.1 with h | h <;> omega + · exact absurd heq (peAF_rootNeE (E.xal r ha'.1) ha0 hi1k hj0 hrr) + · rw [hw _ (rootFootE ha'.1 (E.xal r ha'.1) hi1k) heq] at h3 ht2 ⊢ + exact Or.inl (.stepRoot ha' hi h3 ht2) + | @stepPt t i _ ht h12 hi h3 ht2 ih => + obtain ⟨n1, n2⟩ := E.t2x t ht r0 hr0 + rw [hw _ (.s2pt t i ht hi) (rootNeE ha0 hj0 (E.tal t ht) hi n1 n2)] at h3 ht2 ⊢ + rcases ih with hpar | ⟨ppn, hpte', rfl⟩ + · exact Or.inl (.stepPt hpar ht h12 hi h3 ht2) + · obtain ⟨hb0, -⟩ := (and3_bitsA _).1 h3 + exact absurd (((hch ppn hpte' i hi).1).symm.trans hb0) Bool.false_ne_true + refine ⟨?_, ?_, E.tsh, ?_, ?_, E.xal, E.tal, E.pgd, E.t2x, E.ypg, E.ypp⟩ + · intro r hr + by_cases hrr : r = r0 + · subst hrr + refine ⟨_, hk1, fun ha i hi hmi => ?_⟩ + show DescRelE _ _ _ (if i.val = j0.val then pte else es0 ⟨i.val, hi⟩) ∧ + S2AttrE _ _ (if i.val = j0.val then pte else es0 ⟨i.val, hi⟩) + by_cases hij : i.val = j0.val + · rw [if_pos hij, hm _ (E.foot_alignedE hY _ (rootFootE hr ha0 hi)), + if_pos (by rw [UScalar.eq_of_val_eq hij])] + exact hpte + · rw [if_neg hij, hw _ (rootFootE hr ha0 hi) (fun e => hij (by rw [peAF_1kInjE ha0 hi hj0 e]))] + exact he0 (hActS2 ha) i hi hmi + · obtain ⟨es, hk, he⟩ := E.s2root r hr + refine ⟨es, by rw [hko r hrr]; exact hk, fun ha i hi hmi => ?_⟩ + rw [hw _ (rootFootE hr (E.xal r hr) hi) (peAF_rootNeE (E.xal r hr) ha0 hi hj0 hrr)] + exact he (hActS2 ha) i hi hmi + · intro t ht + obtain ⟨n1, n2⟩ := E.t2x t ht r0 hr0 + obtain ⟨pt, hk, hty, he⟩ := E.s2pt t ht + refine ⟨pt, by rw [hko t n1]; exact hk, hty, fun hr j hj => ?_⟩ + rw [hw _ (.s2pt t j ht hj) (rootNeE ha0 hj0 (E.tal t ht) hj n1 n2)] + rcases hReachBack hr with hpar | ⟨ppn, hpte', rfl⟩ + · exact he hpar j hj + · have hinv := hch ppn hpte' j hj + rw [hinv.2 pt hk] + exact ⟨hinv.1, trivial⟩ + · intro y hy + have h1 := (E.pgd r0 (Or.inr ⟨r0, hr0, Or.inl rfl⟩)).2 + have h2 := (E.pgd _ (Or.inr ⟨r0, hr0, Or.inr rfl⟩)).2 + rw [hw _ (.wl0 y hy) (rootNeE ha0 hj0 (al4kE (hY y hy)) (by decide) (fun e => h1 (e ▸ hy)) + (fun e => h2 (e ▸ hy)))] + exact E.wl0 y hy + · intro y hy i hi + have h1 := (E.pgd r0 (Or.inr ⟨r0, hr0, Or.inl rfl⟩)).2 + have h2 := (E.pgd _ (Or.inr ⟨r0, hr0, Or.inr rfl⟩)).2 + rw [hw _ (.wlo y i hy hi) (rootNeE ha0 hj0 (al4kE (hY y hy)) (by omega) (fun e => h1 (e ▸ hy)) + (fun e => h2 (e ▸ hy)))] + exact E.wlo y hy i hi + +theorem VsRelA.of_hiRootWriteMapE {mem mem' : U64 → U64} {ksb word : U64} {pte : PTE} + (R : VsRelA Pp Tb Lv X Y mem ksb σ) (hK : Aligned ksb) + {y0 j0 : U64} (hy0 : y0 ∈ Y) (hj0 : j0.val < 512) (hA : ¬ AsW ksb (peAF (wa y0 4096#u64) j0)) + (hpte : PteRelA Tb Lv 1 word pte) (hnp : wa y0 4096#u64 ∉ Pp) + (hm : ∀ b, Aligned b → mem' b = if peAF (wa y0 4096#u64) j0 = b then word else mem b) + (hch : ∀ ppn, pte = .pageTablePTE ppn → + ∀ j : U64, j.val < 512 → dBitA (mem (peAF (dTabA word) j)) 0 = false ∧ + ∀ t1, σ.kheap (specPtr (dTabA word)) = some (.archObj (.pageTable t1)) → + Fl4SpecA64.ptApply t1 j.bv = .invalidPTE) : + VsRelA Pp Tb Lv X Y mem' ksb (extSlotWriteX σ (.root (specPtr y0) j0.val) pte) := by + have ha0 : 4096 ∣ (wa y0 4096#u64).val := Nat.dvd_of_mod_eq_zero (waWin_modA (R.yal y0 hy0)) + have ha0' : (wa y0 4096#u64).val % 4096 = 0 := waWin_modA (R.yal y0 hy0) + obtain ⟨l1, hk0, he0⟩ := R.high y0 hy0 + have hk1 := extRootSetE hk0 j0.val pte + have hko : ∀ q, q ≠ y0 → (extSlotWriteX σ (.root (specPtr y0) j0.val) pte).kheap (specPtr q) = + σ.kheap (specPtr q) := fun q hq => kheap_writeX (fun e => hq (specPtr_inj e)) + have hw : ∀ b, VsFootA Pp Tb Y ksb b → b ≠ peAF (wa y0 4096#u64) j0 → mem' b = mem b := fun b hb hne => by + rw [hm b (R.foot_alignedA hK b hb), if_neg (Ne.symm hne)] + have hne_asid : ∀ i : U64, i.val < 128 → atAF ksb i ≠ peAF (wa y0 4096#u64) j0 := fun i hi e => + hA (e ▸ atAF_asWA hi) + have hne_pool : ∀ p ∈ Pp, ∀ lo : U64, lo.val < 512 → peAF p lo ≠ peAF (wa y0 4096#u64) j0 := fun p hp lo hlo => + pgNeE (R.pal p hp) ha0 hlo hj0 (fun e => hnp (e ▸ hp)) + have hActP : ∀ {p}, ActivePoolA mem' ksb p → ActivePoolA mem ksb p := by + rintro p ⟨hpne, hi, hh, htab⟩ + rw [hw _ (.asid hi hh) (hne_asid hi hh)] at htab + exact ⟨hpne, hi, hh, htab⟩ + have hActR : ∀ {asid raw}, ActiveAsidRootA mem' ksb Pp asid raw → ActiveAsidRootA mem ksb Pp asid raw := by + rintro asid raw ⟨hi, lo, hh, hl, ha', pool, hp, hpne, htab, hword, hrne⟩ + rw [hw _ (.asid hi hh) (hne_asid hi hh)] at htab + rw [hw _ (.pool pool lo hp hl) (hne_pool pool hp lo hl)] at hword + exact ⟨hi, lo, hh, hl, ha', pool, hp, hpne, htab, hword, hrne⟩ + have hActH : ∀ {y}, ActiveHighRootA mem' ksb Pp y → ActiveHighRootA mem ksb Pp y := by + rintro y ⟨asid, raw, ha, htag, hy⟩ + exact ⟨asid, raw, hActR ha, htag, hy⟩ + have hPathBack : ∀ {tag sh q}, VsPathA Pp Tb Y mem' ksb tag sh q → + VsPathA Pp Tb Y mem ksb tag sh q ∨ (∃ ppn, pte = .pageTablePTE ppn ∧ q = dTabA word) := by + intro tag sh q hpth + induction hpth with + | native ha htag htb => exact Or.inl (.native (hActR ha) htag htb) + | high ha htag hy => exact Or.inl (.high (hActR ha) htag hy) + | @child tag tb j sh _ htY h12 hj h3' ih => + rcases ih with hpar | ⟨ppn, hpte', rfl⟩ + · by_cases heq : peAF tb j = peAF (wa y0 4096#u64) j0 + · rcases htY with htb | ⟨y, hy, rfl⟩ + · exact absurd heq (pgNeE (R.al tb htb) ha0 hj hj0 (fun e => R.ytb y0 hy0 (e ▸ htb))) + · rw [heq, hm _ (peAF_alignedA ha0' hj0), if_pos rfl] at h3' ⊢ + obtain ⟨hb0, hb1⟩ := (and3_bitsA word).1 h3' + cases pte with + | invalidPTE => exact absurd (hpte.symm.trans hb0) Bool.false_ne_true + | pageTablePTE ppn => exact Or.inr ⟨ppn, rfl, rfl⟩ + | pagePTE _ _ _ _ => + have := hpte.2.2.2.1.1 hb1; omega + · have hmemEq : mem' (peAF tb j) = mem (peAF tb j) := by + rcases htY with htb | ⟨y, hy, rfl⟩ + · exact hw _ (.pt tb j htb hj) heq + · exact hw _ (.high y j hy hj) heq + rw [hmemEq] at h3' ⊢ + exact Or.inl (.child hpar htY h12 hj h3') + · subst hpte' + have hdTb : dTabA word ∈ Tb := hpte.2.2.2.2.2.1 + have heqN : peAF (dTabA word) j ≠ peAF (wa y0 4096#u64) j0 := + pgNeE (R.al _ hdTb) ha0 hj hj0 (fun e => R.ytb y0 hy0 (e ▸ hdTb)) + rw [hw _ (.pt (dTabA word) j hdTb hj) heqN] at h3' + obtain ⟨hb0, -⟩ := (and3_bitsA _).1 h3' + exact absurd ((hch ppn rfl j hj).1.symm.trans hb0) Bool.false_ne_true + refine ⟨?_, ?_, ?_, ?_, ?_, ?_, ?_, R.lv, R.al, R.pal, R.yal, R.ytb⟩ + · intro i hi + rw [show (extSlotWriteX σ (.root (specPtr y0) j0.val) pte).archState = σ.archState from rfl] + rw [hw _ (.asid i hi) (hne_asid i hi)] + exact R.table i hi + · intro i hi + rw [hw _ (.asid i hi) (hne_asid i hi)] + exact R.tmem i hi + · intro p hp + obtain ⟨f, hkf, hf⟩ := R.pool p hp + have hne : p ≠ y0 := fun e => by subst e; rw [hk0] at hkf; cases hkf + refine ⟨f, by rw [hko p hne]; exact hkf, fun ha lo hlo => ?_⟩ + rw [hw _ (.pool p lo hp hlo) (hne_pool p hp lo hlo)] + exact hf (hActP ha) lo hlo + · intro p hp ha lo hlo + rw [hw _ (.pool p lo hp hlo) (hne_pool p hp lo hlo)] + exact R.kind p hp (hActP ha) lo hlo + · intro h0 + have h0' : mem (atAF ksb 0#u64) ≠ 0#u64 := by + rwa [hw _ (.asid 0#u64 (by decide)) (hne_asid 0#u64 (by decide))] at h0 + have hp0 := R.tmem 0#u64 (by decide) h0' + rw [hw _ (.asid 0#u64 (by decide)) (hne_asid 0#u64 (by decide)), + hw _ (.pool _ 0#u64 hp0 (by decide)) (hne_pool _ hp0 0#u64 (by decide))] + exact R.asid0 h0' + · intro t ht + obtain ⟨pt, hkt, hty, he⟩ := R.pt t ht + have hne : t ≠ y0 := fun e => by subst e; rw [hk0] at hkt; cases hkt + have hne2 : t ≠ wa y0 4096#u64 := fun e => R.ytb y0 hy0 (e ▸ ht) + refine ⟨pt, by rw [hko t hne]; exact hkt, hty, fun hr i hi => ?_⟩ + rw [hw _ (.pt t i ht hi) (pgNeE (R.al t ht) ha0 hi hj0 hne2)] + obtain ⟨tag, sh, htag, hpth⟩ := hr + rcases hPathBack hpth with hpar | ⟨ppn, hpte', rfl⟩ + · exact he ⟨tag, sh, htag, hpar⟩ i hi + · have hinv := hch ppn hpte' i hi + rw [hinv.2 pt hkt] + exact hinv.1 + · intro y hy + by_cases hyy : y = y0 + · subst hyy + refine ⟨_, hk1, fun ha i hi => ?_⟩ + show PteRelA Tb Lv 1 _ (if i.val = j0.val then pte else l1 ⟨i.val, hi⟩) + by_cases hij : i.val = j0.val + · rw [if_pos hij, hm _ (peAF_alignedA ha0' hi), if_pos (by rw [UScalar.eq_of_val_eq hij])] + exact hpte + · rw [if_neg hij, hw _ (.high y i hy hi) (fun e => hij (congrArg UScalar.val (peAF_injA ha0' ha0' hi hj0 e).2))] + exact he0 (hActH ha) i hi + · obtain ⟨l1', hk', he'⟩ := R.high y hy + have hne : wa y 4096#u64 ≠ wa y0 4096#u64 := fun e => hyy (by + have h1 := congrArg UScalar.val e + have hy1 := al8kE (R.yal y hy) + have hy2 := al8kE (R.yal y0 hy0) + rw [wa_val, wa_val, show (4096#u64 : U64).val = 4096 from rfl, + Nat.mod_eq_of_lt (by omega), Nat.mod_eq_of_lt (by omega)] at h1 + exact UScalar.eq_of_val_eq (by omega)) + refine ⟨l1', by rw [hko y hyy]; exact hk', fun ha i hi => ?_⟩ + rw [hw _ (.high y i hy hi) (pgNeE (Nat.dvd_of_mod_eq_zero (waWin_modA (R.yal y hy))) ha0 hi hj0 hne)] + exact he' (hActH ha) i hi + +theorem VsRelA.of_ptWriteMapE {mem mem' : U64 → U64} {ksb word : U64} {pte : PTE} + (R : VsRelA Pp Tb Lv X Y mem ksb σ) (hK : Aligned ksb) + {t0 i0 : U64} (ht0 : t0 ∈ Tb) (_h1 : 1 ≤ Lv t0) (hi0 : i0.val < 512) (hA : ¬ AsW ksb (peAF t0 i0)) + (hpte : PteRelA Tb Lv (Lv t0) word pte) + (hm : ∀ b, Aligned b → mem' b = if peAF t0 i0 = b then word else mem b) + (hch : ∀ ppn, pte = .pageTablePTE ppn → dTabA word ≠ t0 ∧ + ∀ j : U64, j.val < 512 → dBitA (mem (peAF (dTabA word) j)) 0 = false ∧ + ∀ t1, σ.kheap (specPtr (dTabA word)) = some (.archObj (.pageTable t1)) → + Fl4SpecA64.ptApply t1 j.bv = .invalidPTE) : + VsRelA Pp Tb Lv X Y mem' ksb (extSlotWriteX σ (.pt (specPtr (peAF t0 i0))) pte) := by + have ha0' : t0.val % 4096 = 0 := R.al_mod ht0 + obtain ⟨pt0, hk0, -, -⟩ := R.pt t0 ht0 + rw [extPtSetE hk0 ha0' hi0] + exact R.of_storePteA ht0 hi0 hk0 hpte hA + (fun b hb hne => by rw [hm b (R.foot_alignedA hK b hb), if_neg (Ne.symm hne)]) + (by rw [hm _ (peAF_alignedA ha0' hi0), if_pos rfl]) hch + +set_option maxHeartbeats 1000000 in +theorem ept_lookup_slot_mapA + (E : ExtVsRelA Pp Tb X Y T2 Sh2 (L.mem s) σ) {p n : U64} (hp : p ∈ X) + (hact : ActiveS2RootA Pp X σ p) + (hn : n.val = fl4A64Platform.s2IpaBits) (va : U64) : + ept.lookup_slot st s p va n ⦃ lu => + let pos := extLookupSlot σ s2Geom (specPtr p) va.bv + lu.shift.val = pos.shift ∧ + SlotRelE (G2ReachE Pp X T2 Sh2 (L.mem s) σ) (TopS2ReachE Pp X σ) lu.slot pos.slot pos.shift ⦄ := by + let pos := extLookupSlot σ s2Geom (specPtr p) va.bv + obtain ⟨g, kk, hg, hkk, hmk, h64, hts, hrem, htop⟩ := s2StartE hn + obtain ⟨hj, hsl⟩ := htop va p + obtain ⟨⟨lsh, lsl⟩, hlu, hpost⟩ := spec_ex_fd (lookup_loopE (st := st) E.goodE (topOkS2E E) + va pos False 0#u64 (fun h => h.elim) g.top_shift (peAF p (idxME va g.top_shift kk)) + (core.num.U64.wrapping_sub g.levels 1#u64) + (Or.inl ⟨extTopPos s2Geom (specPtr p) va.bv, by rw [hrem]; rfl, hts.symm, + Or.inr ⟨p, _, hp, hact, hj, rfl, hsl, rfl⟩⟩)) + unfold ept.lookup_slot + rw [hg, bind_ok, ept_entry_addrE _ _ _ _ h64 hmk, bind_ok] + simp only [lift, bind_ok] + rw [ept_lookup_loopE, hlu, bind_ok] + exact (WP.spec_ok _).2 ⟨hpost.1.symm, hpost.2⟩ + +set_option maxHeartbeats 1000000 in +theorem lookup_pt_slot_hiA + (R : VsRelA Pp Tb Lv X Y (L.mem s) (L.ksb s) σ) + (E : ExtVsRelA Pp Tb X Y T2 Sh2 (L.mem s) σ) {y va : U64} (hy : y ∈ Y) + (hact : ActiveHighRootA (L.mem s) (L.ksb s) Pp y) + (hhi : idxA va 39#u64 = 511#u64) : + vspace.lookup_pt_slot st s y va ⦃ lu => + let pos := extLookupSlot σ highGeom (specPtr y) va.bv + lu.shift.val = pos.shift ∧ + SlotRelE (G1ReachE Pp Tb Y Lv (L.mem s) (L.ksb s)) (TopHiReachE Pp Y (L.mem s) (L.ksb s)) + lu.slot pos.slot pos.shift ⦄ := by + obtain ⟨hb0, hb1, htab⟩ := E.wl0 _ hy + have hP0 : True → (L.mem s (peAF y (idxA va 39#u64)) &&& 3#u64) = 3#u64 ∧ + ∀ s1 l1 : U64, s1.val = 30 → l1.val = 2 → + WkE (G1ReachE Pp Tb Y Lv (L.mem s) (L.ksb s)) (TopHiReachE Pp Y (L.mem s) (L.ksb s)) σ va + (extLookupSlot σ highGeom (specPtr y) va.bv) + (s1, peAF (dTabA (L.mem s (peAF y (idxA va 39#u64)))) (idxA va s1), l1) := fun _ => by + rw [hhi] + refine ⟨(and3_bitsA _).2 ⟨hb0, hb1⟩, fun s1 l1 hs1 hl1 => ⟨extTopPos highGeom + (specPtr y) va.bv, by rw [hl1]; rfl, hs1.symm, Or.inr ⟨y, idxA va s1, hy, hact, idxA_lt _ _, + by rw [htab], ?_, rfl⟩⟩⟩ + show ExtSlot.root _ ((va.bv.toNat >>> 30) % (511 + 1)) = _ + rw [idxA_val, hs1, Nat.shiftRight_eq_div_pow] + rfl + obtain ⟨⟨lsh, lsl⟩, hlu, hpost⟩ := spec_ex_fd (lookup_loopE (st := st) R.goodE (topOkHiE R) va _ True _ hP0 + 39#u64 _ 3#u64 (Or.inr ⟨trivial, rfl⟩)) + unfold vspace.lookup_pt_slot + rw [show vspace.TOP_LEVEL_SHIFT = 39#u64 by unfold vspace.TOP_LEVEL_SHIFT; rfl, entry_addr_eqA _ _ _ (by simp), + bind_ok, hlu, bind_ok] + exact (WP.spec_ok _).2 ⟨hpost.1.symm, hpost.2⟩ + +abbrev ExtWordWriteA (s s' : S) (slot word : U64) : Prop := + L.ksb s' = L.ksb s ∧ L.cpu s' = L.cpu s ∧ + ∀ b, Aligned b → L.mem s' b = if slot = b then word else L.mem s b + +theorem extWordWriteA {slot word : U64} (hal : Aligned slot) : + ExtWordWriteA (st := st) s (L.wr s slot word) slot word := + ⟨L.ksb_wr _ _ _, L.cpu_wr _ _ _, fun _ hb => L.mem_wr _ _ _ _ hal hb⟩ + +theorem extWordWrite_overA {slot word old : U64} (hal : Aligned slot) : + ExtWordWriteA (st := st) s (L.wr (L.wr s slot old) slot word) slot word := by + refine ⟨by simp only [LawfulStore.ksb_wr], by simp only [LawfulStore.cpu_wr], fun b hb => ?_⟩ + rw [L.mem_wr _ _ _ _ hal hb] + by_cases h : slot = b + · simp [h] + · rw [if_neg h, L.mem_wr _ _ _ _ hal hb, if_neg h, if_neg h] + +set_option maxHeartbeats 1000000 in +theorem install_s2_leaf_mapA {A : Type} {ar : machine.Arch A} {AR : A → HwRootA → Prop} + (K : ArchKeepsA ar AR) {a : A} {hw : HwRootA} (hAR : AR a hw) + (lu : ept.S2Slot) (word root asid ipa ptr attr : U64) (dev : Bool) (hal : Aligned lu.slot) : + ept.install_s2_leaf st ar s a lu word root asid ipa ptr dev attr ⦃ r => + ExtWordWriteA (st := st) s r.1 lu.slot word ∧ AR r.2 hw ⦄ := by + have cache (a0 : A) (ha : AR a0 hw) : + (if (word != 0#u64) = true then + if dev = true then ok a0 + else + if (attr &&& fl4_abi.consts.arm_vm_attr.PAGE_CACHEABLE) = 0#u64 then do + let bytes ← 1#u64 <<< (lu.shift &&& 63#u64) + let pa ← layout.paddr ptr + ar.cache_op a0 vspace.CACHE_CLEAN_INVALIDATE 0#u64 asid ipa bytes pa + else ok a0 + else ok a0) ⦃ a1 => AR a1 hw ⦄ := by + split <;> try exact (WP.spec_ok _).2 ha + split <;> try exact (WP.spec_ok _).2 ha + split <;> try exact (WP.spec_ok _).2 ha + have hsh : (lu.shift &&& 63#u64).val < 64 := by + rw [and_maskA lu.shift 6 _ rfl] + exact Nat.mod_lt _ (by norm_num) + rw [CapRt.shl_u64_eq _ _ hsh, bind_ok] + rw [show layout.paddr ptr = ok (core.num.U64.wrapping_sub ptr layout.PPTR_BASE) from rfl, bind_ok] + exact K.cacheOp a0 hw _ _ _ _ _ _ ha + have invalidate a0 (ha : AR a0 hw) : + ar.invalidate_stage2_page a0 asid ipa ⦃ a1 => AR a1 hw ⦄ := + K.invS2Page a0 hw asid ipa ha + unfold ept.install_s2_leaf + rw [L.read_eq, bind_ok, ept_is_leafE, is_pageE, bind_ok] + generalize isPgE (L.mem s lu.slot) lu.shift.val = isPg + cases isPg + · simp only [Bool.false_eq_true, ↓reduceIte, lift, bind_ok] + refine WP.spec_bind (cache a hAR) fun a1 ha1 => ?_ + rw [L.write_eq, bind_ok] + exact (WP.spec_ok _).2 ⟨extWordWriteA hal, ha1⟩ + · simp only [↓reduceIte, lift, bind_ok, eq_of_spec_fd Fl4ProofsA64.P0.Consts.ept.S2_BBM_MASK.spec] + split <;> rename_i hne + · split + · rw [L.write_eq, bind_ok] + exact WP.spec_bind (invalidate a hAR) fun a1 ha1 => (WP.spec_ok _).2 ⟨extWordWriteA hal, ha1⟩ + · rw [L.write_eq, bind_ok] + refine WP.spec_bind (invalidate a hAR) fun a1 ha1 => ?_ + have hc := cache a1 ha1; rw [if_pos hne] at hc + refine WP.spec_bind hc fun a2 ha2 => ?_ + rw [L.write_eq, bind_ok] + exact (WP.spec_ok _).2 ⟨extWordWrite_overA hal, ha2⟩ + · rw [L.write_eq, bind_ok] + refine WP.spec_bind (invalidate a hAR) fun a1 ha1 => ?_ + rw [bind_ok, L.write_eq, bind_ok] + exact (WP.spec_ok _).2 ⟨extWordWrite_overA hal, ha1⟩ + +set_option maxHeartbeats 1000000 in +theorem install_page_hiA {A : Type} {ar : machine.Arch A} {AR : A → HwRootA → Prop} + (K : ArchKeepsA ar AR) {a : A} {hw : HwRootA} (hAR : AR a hw) + (lu : vspace.PtSlot) (word root asid vaddr attr : U64) (f : vspace.FrameCap) (hal : Aligned lu.slot) : + vspace.install_page st ar s a lu word root asid vaddr f attr true ⦃ r => + ExtWordWriteA (st := st) s r.1 lu.slot word ∧ AR r.2 hw ⦄ := by + have cache (a0 : A) (ha : AR a0 hw) : + (if f.is_device = true then ok a0 + else + if (attr &&& fl4_abi.consts.arm_vm_attr.PAGE_CACHEABLE) = 0#u64 then do + let bytes ← 1#u64 <<< (lu.shift &&& 63#u64) + let pa ← layout.paddr f.ptr + ar.cache_op a0 vspace.CACHE_CLEAN_INVALIDATE root asid vaddr bytes pa + else ok a0) ⦃ a1 => AR a1 hw ⦄ := by + split <;> try exact (WP.spec_ok _).2 ha + split <;> try exact (WP.spec_ok _).2 ha + have hsh : (lu.shift &&& 63#u64).val < 64 := by + rw [and_maskA lu.shift 6 _ rfl] + exact Nat.mod_lt _ (by norm_num) + rw [CapRt.shl_u64_eq _ _ hsh, bind_ok] + rw [show layout.paddr f.ptr = ok (core.num.U64.wrapping_sub f.ptr layout.PPTR_BASE) from rfl, bind_ok] + exact K.cacheOp a0 hw _ _ _ _ _ _ ha + unfold vspace.install_page vspace.invalidate_mapping + rw [L.read_eq, bind_ok, is_page_eqA, bind_ok] + generalize (if lu.shift = 12#u64 then decide ((L.mem s lu.slot &&& 3#u64) = 3#u64) + else decide ((L.mem s lu.slot &&& 3#u64) = 1#u64)) = isPg + cases isPg + · simp only [Bool.false_eq_true, ↓reduceIte, lift, bind_ok] + refine WP.spec_bind (cache a hAR) fun a1 ha1 => ?_ + rw [L.write_eq, bind_ok] + exact (WP.spec_ok _).2 ⟨extWordWriteA hal, ha1⟩ + · simp only [↓reduceIte, lift, bind_ok, eq_of_spec_fd P0.Consts.vspace.BBM_MASK.spec] + split + · rw [L.write_eq, bind_ok] + exact WP.spec_bind (K.invHiPage a hw vaddr hAR) fun a1 ha1 => (WP.spec_ok _).2 ⟨extWordWriteA hal, ha1⟩ + · rw [L.write_eq, bind_ok] + refine WP.spec_bind (K.invHiPage a hw vaddr hAR) fun a1 ha1 => ?_ + refine WP.spec_bind (cache a1 ha1) fun a2 ha2 => ?_ + rw [L.write_eq, bind_ok] + exact (WP.spec_ok _).2 ⟨extWordWrite_overA hal, ha2⟩ + +end extMap + +/-! ## Helpers for `s2Limit`, `HIGH_VSPACE`, `performExtVSpace (.map …)`, `CapRt.EncWf` -/ + +section helpers + +private theorem s2LimitEq_loc {n : U64} (h : n.val = fl4A64Platform.s2IpaBits) : + ept.ipa_limit n = ok (⟨s2IpaLimit⟩ : U64) := by + unfold ept.ipa_limit ept.geom s2IpaLimit s2IpaBitsOf + by_cases h42 : n ≤ 42#u64 + · have h42' : fl4A64Platform.s2IpaBits ≤ 42 := by rw [← h]; exact h42 + rw [if_pos h42, if_pos h42', bind_ok, CapRt.shl_u64_eq 1#u64 40#u64 (by decide), bind_ok] + rfl + · have h42' : ¬ fl4A64Platform.s2IpaBits ≤ 42 := fun h' => h42 (show n.val ≤ 42 by rw [h]; exact h') + rw [if_neg h42, if_neg h42'] + by_cases h44 : n = 44#u64 + · have h44' : fl4A64Platform.s2IpaBits = 44 := by rw [← h, h44]; rfl + rw [if_pos h44, if_pos h44', bind_ok, CapRt.shl_u64_eq 1#u64 44#u64 (by decide), bind_ok] + rfl + · have h44' : fl4A64Platform.s2IpaBits ≠ 44 := fun h' => h44 (UScalar.eq_of_val_eq (by rw [h, h']; rfl)) + rw [if_neg h44, if_neg h44', bind_ok, CapRt.shl_u64_eq 1#u64 48#u64 (by decide), bind_ok] + rfl + +private theorem s2IpaLimit_lt48 : s2IpaLimit.toNat < 2 ^ 48 := by + unfold s2IpaLimit s2IpaBitsOf + split_ifs <;> decide + +private theorem hi_idx39_eq (va : U64) (hlo : fl4_abi.consts.HIGH_VSPACE_BASE ≤ va) : + idxA va 39#u64 = 511#u64 := by + have h1 : 18446743936270598144 ≤ va.val := by + have hb : fl4_abi.consts.HIGH_VSPACE_BASE.val = 18446743936270598144 := by + unfold fl4_abi.consts.HIGH_VSPACE_BASE; rfl + rw [← hb]; exact hlo + have h2 : va.val < 2 ^ 64 := va.bv.isLt + apply UScalar.eq_of_val_eq + rw [idxA_val] + show (va.val / 2 ^ 39) % 512 = 511 + omega + +private theorem hi_canA (va : U64) (hlo : fl4_abi.consts.HIGH_VSPACE_BASE ≤ va) : CapRt.Canonical va := by + have h1 : 18446743936270598144 ≤ va.val := by + have hb : fl4_abi.consts.HIGH_VSPACE_BASE.val = 18446743936270598144 := by + unfold fl4_abi.consts.HIGH_VSPACE_BASE; rfl + rw [← hb]; exact hlo + have h2 : va.val < 2 ^ 64 := va.bv.isLt + right + intro j hj hj47 + show va.val.testBit j = true + have hpow : 2 ^ 64 - 2 ^ 47 ≤ va.val := by omega + have hsub : va.val / 2 ^ j ≥ 2 ^ (64 - j) - 1 := by + have hle : (2 ^ (64 - j) - 1) * 2 ^ j = 2 ^ 64 - 2 ^ j := by + rw [Nat.sub_mul, Nat.one_mul, ← Nat.pow_add, Nat.sub_add_cancel (by omega)] + have h47 : 2 ^ 47 ≤ 2 ^ j := Nat.pow_le_pow_right (by norm_num) hj47 + have hge : 2 ^ 64 - 2 ^ j ≤ 2 ^ 64 - 2 ^ 47 := by omega + exact (Nat.le_div_iff_mul_le (Nat.two_pow_pos j)).2 (by omega) + have hlt : va.val / 2 ^ j < 2 ^ (64 - j) := by + rw [Nat.div_lt_iff_lt_mul (Nat.two_pow_pos j), ← Nat.pow_add, Nat.sub_add_cancel (by omega)] + exact h2 + have heq : va.val / 2 ^ j = 2 ^ (64 - j) - 1 := by omega + rw [Nat.testBit_eq_decide_div_mod_eq, heq] + have hpos : 1 ≤ 64 - j := by omega + have hodd : (2 ^ (64 - j) - 1) % 2 = 1 := by + rcases Nat.exists_eq_add_of_le' hpos with ⟨k, hk⟩ + rw [hk, Nat.pow_succ] + have : 1 ≤ 2 ^ k := Nat.one_le_two_pow + omega + simp [hodd] + +private theorem frameSize_bits_eqA (sz : capability.FrameSize) : + capability.FrameSize.bits sz = ok (psA sz) := by + cases sz <;> rfl + +private theorem align_check_eqA (sz : capability.FrameSize) (va : U64) : + ((va &&& core.num.U64.wrapping_sub (⟨(1#u64 : U64).bv <<< ((psA sz &&& 63#u64).val)⟩ : U64) 1#u64) != 0#u64) = + decide (va.bv &&& mask (pageBitsForSize (frameSizeOfA sz)) ≠ 0) := by + have hm : (core.num.U64.wrapping_sub (⟨(1#u64 : U64).bv <<< ((psA sz &&& 63#u64).val)⟩ : U64) 1#u64).bv = + mask (pageBitsForSize (frameSizeOfA sz)) := by cases sz <;> decide + have hm' : (core.num.U64.wrapping_sub (⟨(1#u64 : U64).bv <<< ((psA sz &&& 63#u64).val)⟩ : U64) 1#u64).val = + (mask (pageBitsForSize (frameSizeOfA sz)) : Word).toNat := congrArg BitVec.toNat hm + apply Bool.eq_iff_iff.2 + simp only [bne_iff_ne, decide_eq_true_iff] + constructor <;> intro h e + · exact h (UScalar.eq_of_val_eq (by rw [UScalar.val_and, hm']; exact congrArg BitVec.toNat e)) + · exact h (BitVec.eq_of_toNat_eq (by rw [BitVec.toNat_and, ← hm']; exact congrArg UScalar.val e)) + +private theorem checkVpAlignment_runA (sz : capability.FrameSize) (va : U64) (σ : State Unit) : + (checkVpAlignment (Z := Unit) (frameSizeOfA sz) va.bv).run σ = + if va.bv &&& mask (pageBitsForSize (frameSizeOfA sz)) ≠ 0 then + ({(.error .alignmentError, σ)}, False) + else ({(.ok (), σ)}, False) := by + have hiff : isAligned va.bv (pageBitsForSize (frameSizeOfA sz)) ↔ + va.bv &&& mask (pageBitsForSize (frameSizeOfA sz)) = 0 := by + unfold isAligned + have hm : (mask (pageBitsForSize (frameSizeOfA sz)) : Word).toNat = + 2 ^ pageBitsForSize (frameSizeOfA sz) - 1 := by + cases sz <;> decide + rw [show (va.bv &&& mask (pageBitsForSize (frameSizeOfA sz)) = 0) ↔ + (va.bv.toNat &&& (2 ^ pageBitsForSize (frameSizeOfA sz) - 1) = 0) from by + constructor + · intro h; have := congrArg BitVec.toNat h; rwa [BitVec.toNat_and, hm] at this + · intro h; apply BitVec.eq_of_toNat_eq; rw [BitVec.toNat_and, hm]; exact h] + rw [Nat.and_two_pow_sub_one_eq_mod, Nat.dvd_iff_mod_eq_zero] + unfold checkVpAlignment NonDetE.unlessE + by_cases h : va.bv &&& mask (pageBitsForSize (frameSizeOfA sz)) ≠ 0 + · have hna : ¬ isAligned va.bv (pageBitsForSize (frameSizeOfA sz)) := fun ha => h (hiff.1 ha) + rw [if_neg hna, if_pos h]; rfl + · have ha : isAligned va.bv (pageBitsForSize (frameSizeOfA sz)) := hiff.2 (Classical.not_not.mp h) + rw [if_pos ha, if_neg h]; rfl + +private theorem last_eqA (sz : capability.FrameSize) (va : U64) : + (core.num.U64.wrapping_add va + (core.num.U64.wrapping_sub (⟨(1#u64 : U64).bv <<< ((psA sz &&& 63#u64).val)⟩ : U64) 1#u64)).bv = + va.bv + mask (pageBitsForSize (frameSizeOfA sz)) := by + have hm : (core.num.U64.wrapping_sub (⟨(1#u64 : U64).bv <<< ((psA sz &&& 63#u64).val)⟩ : U64) 1#u64).bv = + mask (pageBitsForSize (frameSizeOfA sz)) := by cases sz <;> decide + apply BitVec.eq_of_toNat_eq + rw [show (core.num.U64.wrapping_add va _).bv.toNat = (va.val + _ ) % 2 ^ 64 from rfl, BitVec.toNat_add, ← hm] + rfl + +private theorem performExtMap_memA {σ1 σ2 : State Unit} {cap : ArchCap} {cte : CslotPtr} {pte : PTE} + {sl : ExtSlot} (hsc : ((), σ2) ∈ (setCap (Z := Unit) (.archObjectCap cap) cte σ1).1) + (hok : ExtSlotOkX σ2 sl) : + (.ok ([] : List Data), extSlotWriteX σ2 sl pte) ∈ + ((fl4ArchPerformInvocation (Z := Unit) (.extVSpace (.map cap cte pte sl))).run σ1).1 := by + apply mem_archExtA + unfold performExtVSpace + have hst : (storeExtSlot (Z := Unit) sl pte >>= fun _ => pure ([] : List Data)) σ2 = + ({([], extSlotWriteX σ2 sl pte)}, False) := by + rw [nd_bind_oneD (storeExtSlot_runX hok pte)]; rfl + exact ⟨((), σ2), hsc, by dsimp only; rw [hst]; rfl⟩ + +private theorem extSlotOk_of_s2SlotA {Pp Tb X Y T2 : Set U64} {Sh2 : U64 → Nat} {mem : U64 → U64} {σ : State Unit} + (E : ExtVsRelA Pp Tb X Y T2 Sh2 mem σ) {x : U64} {sl : ExtSlot} {sh : Nat} + (h : SlotRelE (G2E T2 Sh2) (TopS2E X) x sl sh) : ExtSlotOkX σ sl := by + rcases h with ⟨t, i, ⟨ht, -⟩, hi, rfl, rfl⟩ | ⟨r, j, hr, hj, rfl, rfl, -⟩ + · obtain ⟨pt, hk, hty, -⟩ := E.s2pt t ht + cases pt with + | vsRootPT _ => cases hty + | normalPT f => + obtain ⟨hal, hb, -⟩ := slot_geomA .normalPtT (Nat.mod_eq_zero_of_dvd (E.tal t ht)) hi + exact ⟨hal, f, by rw [hb]; exact hk⟩ + · obtain ⟨es, hk, -⟩ := E.s2root r hr + exact ⟨.stage2Root es, extObjAt_someX.2 hk⟩ + +private theorem run_liftME_congrA {α β : Type} {f : α → β} {m1 m2 : SEMonad Unit α} {σ : State Unit} + (h : m1.run σ = m2.run σ) : (NonDetE.liftME f m1).run σ = (NonDetE.liftME f m2).run σ := by + unfold NonDetE.liftME + change (NonDet.bind (m1.run) _ σ) = (NonDet.bind (m2.run) _ σ) + simp only [NonDet.bind, h] + +private theorem extSlotOk_of_hiSlotA {Pp Tb X Y : Set U64} {Lv : U64 → Nat} {mem : U64 → U64} {ksb : U64} + {σ : State Unit} (R : VsRelA Pp Tb Lv X Y mem ksb σ) {x : U64} {sl : ExtSlot} {sh : Nat} + (h : SlotRelE (G1E Tb Lv) (TopHiE Y) x sl sh) : ExtSlotOkX σ sl := by + rcases h with ⟨t, i, ⟨ht, h1, -⟩, hi, rfl, rfl⟩ | ⟨y, j, hy, hj, rfl, rfl, -⟩ + · obtain ⟨pt, hk, hty, -⟩ := R.pt t ht + have hty' : ptType pt = .normalPtT := by rw [hty]; unfold ptTyA; rw [if_neg (by omega)] + cases pt with + | vsRootPT _ => cases hty' + | normalPT f => + obtain ⟨hal, hb, -⟩ := slot_geomA .normalPtT (R.al_mod ht) hi + exact ⟨hal, f, by rw [hb]; exact hk⟩ + · obtain ⟨l1, hk, -⟩ := R.high y hy + exact ⟨.highVSpace l1, extObjAt_someX.2 hk⟩ + +end helpers + +/-! ## Stage-2 `ept.map_frame` refinement -/ + +section s2Map +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {s : S} {a : A} {σ : State Unit} {m : McState} +variable {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} {ia ie : Nat} + +set_option maxHeartbeats 4000000 in +/-- **`ept.map_frame` against `decodeS2PageMapAt`** (module docstring): both unmapped plain frames +(`e = false, mp = false`) and mapped Stage-2 frames (`e = true, mp = true`). -/ +theorem s2_map_frame_jA {ar : machine.Arch A} (K : ArchKeepsA ar AR) + (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) + (hTO : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s q) (hEO : ExtTabOffA (L := L) Z m s) + (hK : Aligned (L.ksb s)) (hT : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s)) (hIpa : S2IpaE ar a) + (hTr : TreeA st OS Z.G.chains Z.G.par s) (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) + (cte : CslotPtr) (hab : AbsSlotA Z.th cte) (hos : cslotAddrA cte ∈ OS) + (fp rt asid va : U64) (sz : capability.FrameSize) (dev mp e : Bool) + (hcap : capAtA (L.mem s) (cslotAddrA cte) = .Frame fp sz rt dev mp e asid va) + (FP : FrPlA sz fp mp asid) (hemp : e = mp) + (root asid1 ipa vm attr : U64) (hrX : root ∈ Z.X) (hactR : ActiveS2RootA Z.Pp Z.X σ root) + (ha1 : asid1 ≠ 0#u64) (ha16 : asid1.val < 2 ^ 16) + (hvmRange : vm = capability.VM_KERNEL_ONLY ∨ vm = capability.VM_READ_ONLY ∨ vm = capability.VM_READ_WRITE) + {rightsMask : U64} + (hvmEq : vmRightsOfA vm = maskVmRights (vmRightsOfA rt) (dataToRights rightsMask.bv)) + {D : Set (Except SyscallError Fl4ArchInvocation × State Unit) × Prop} + (hD : D = (NonDetE.liftME Fl4ArchInvocation.extVSpace + (decodeS2PageMapAt (Z := Unit) cte (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev + (mapDataA mp asid va) (specPtr root) (asidOfA asid1) ipa.bv rightsMask.bv attr.bv)).run σ) : + ept.map_frame st ar s a root asid1 ipa vm attr fp sz rt dev asid va (cslotAddrA cte) ⦃ res => + VsTopPostA (L := L) AR Z OS m σ D s a res ⦄ := by + obtain ⟨c0, -, hrel0⟩ := StateRelA.getAbsA E.j.1.st hab + rw [hcap] at hrel0 + obtain ⟨⟨hr1, hr3, ⟨has16, hmz0, hmz1, _⟩, hov⟩, -⟩ := hrel0 + have hcanP : CapRt.Canonical fp := by + have := CapRt.decodeF_encWf (L.mem s (cslotAddrA cte)).bv (L.mem s (wa (cslotAddrA cte) 8#u64)).bv + change CapRt.EncWf (capAtA (L.mem s) (cslotAddrA cte)) at this + rw [hcap] at this + cases e <;> exact this.1 + have h16_of_mp : mp = true → va.val < 2 ^ 48 := by + intro hmp + have he1 : e = true := hemp.trans hmp + have := CapRt.decodeF_encWf (L.mem s (cslotAddrA cte)).bv (L.mem s (wa (cslotAddrA cte) 8#u64)).bv + change CapRt.EncWf (capAtA (L.mem s) (cslotAddrA cte)) at this + rw [hcap, he1] at this + exact this.2.2.2 + have hsh63 : (psA sz &&& 63#u64).val < 64 := by cases sz <;> decide + obtain ⟨nIpa, hnIpa, hnvIpa⟩ := hIpa + -- Unfold ept.map_frame and decodeS2PageMapAt + unfold ept.map_frame + rw [frameSize_bits_eqA, bind_ok, hnIpa, bind_ok] + simp only [lift, bind_ok] + rw [CapRt.shl_u64_eq 1#u64 _ hsh63, bind_ok] + simp only [lift, bind_ok] + rw [align_check_eqA sz ipa] + -- Alignment check + by_cases hal : ipa.bv &&& mask (pageBitsForSize (frameSizeOfA sz)) ≠ 0 + · rw [decide_eq_true hal, if_pos rfl] + refine (WP.spec_ok _).2 ⟨rfl, rfl, .alignmentError, ?_, Or.inl trivial⟩ + rw [hD]; unfold decodeS2PageMapAt + exact run_liftME_errA (run_bind_errorA (by rw [checkVpAlignment_runA, if_pos hal]) _) + rw [decide_eq_false hal, if_neg Bool.false_ne_true] + -- Common tail once ipa < 2^48 and lookup is reached + have tail (hipa48 : ipa.val < 2 ^ 48) + (hD2 : D = (NonDetE.liftME Fl4ArchInvocation.extVSpace (do + let pos ← NonDetE.liftE (NonDet.gets fun s : State Unit => extLookupSlot s s2Geom (specPtr root) ipa.bv) + NonDetE.unlessE (pos.shift = pageBitsForSize (frameSizeOfA sz)) + (NonDetE.throwError (.failedLookup false (.missingCapability pos.shift))) + let vm' := maskVmRights (vmRightsOfA rt) (dataToRights rightsMask.bv) + if vm' = ∅ then + NonDetE.returnOk (.map (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev none) cte .invalidPTE pos.slot) + else + NonDetE.returnOk (.map (.ext (.stage2FrameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev + (some (asidOfA asid1, ipa.bv)))) cte + (makeUserPte (addrFromPPtr (specPtr fp)) (attribsFromWord attr.bv) vm' (vmRightsOfA rt) (frameSizeOfA sz)) + pos.slot))).run σ) : + (do + let lu ← ept.lookup_slot st s root ipa nIpa + if lu.shift != psA sz then + ok (core.result.Result.Err (error.Exc.Sys (error.SysError.FailedLookup false (error.LookupFault.MissingCapability lu.shift))), s, a) + else do + let cap_read ← vspace.rights_readable rt + let i4 ← layout.paddr fp + let ew ← ept.make_s2_leaf i4 vm cap_read attr (psA sz) + let ct ← sched.cur_thread st s + let s1 ← sched.set_thread_state st s ct sched.TS_RESTART + if ew != 0#u64 then + if ew != 0#u64 then do + let s2 ← obj.Slot.set_cap st (cslotAddrA cte) s1 + (.Frame fp sz rt dev (ew != 0#u64) (ew != 0#u64) asid1 ipa) + let (s3, a1) ← ept.install_s2_leaf st ar s2 a lu ew root asid1 ipa fp dev attr + ok (core.result.Result.Ok (), s3, a1) + else do + let s2 ← obj.Slot.set_cap st (cslotAddrA cte) s1 + (.Frame fp sz rt dev (ew != 0#u64) (ew != 0#u64) asid1 0#u64) + let (s3, a1) ← ept.install_s2_leaf st ar s2 a lu ew root asid1 ipa fp dev attr + ok (core.result.Result.Ok (), s3, a1) + else + if ew != 0#u64 then do + let s2 ← obj.Slot.set_cap st (cslotAddrA cte) s1 + (.Frame fp sz rt dev (ew != 0#u64) (ew != 0#u64) 0#u64 ipa) + let (s3, a1) ← ept.install_s2_leaf st ar s2 a lu ew root asid1 ipa fp dev attr + ok (core.result.Result.Ok (), s3, a1) + else do + let s2 ← obj.Slot.set_cap st (cslotAddrA cte) s1 + (.Frame fp sz rt dev (ew != 0#u64) (ew != 0#u64) 0#u64 0#u64) + let (s3, a1) ← ept.install_s2_leaf st ar s2 a lu ew root asid1 ipa fp dev attr + ok (core.result.Result.Ok (), s3, a1)) ⦃ res => VsTopPostA (L := L) AR Z OS m σ D s a res ⦄ := by + refine WP.spec_bind (ept_lookup_slot_mapA E.j.1.ext hrX hactR hnvIpa ipa) fun lu ⟨hlsh, hslR⟩ => ?_ + have hsl : SlotRelE (G2E Z.T2 Z.Sh2) (TopS2E Z.X) lu.slot + (extLookupSlot σ s2Geom (specPtr root) ipa.bv).slot + (extLookupSlot σ s2Geom (specPtr root) ipa.bv).shift := + hslR.mono (fun _ _ g => g.1) (fun _ _ _ ht => ht.toTopS2E) + let pos := extLookupSlot σ s2Geom (specPtr root) ipa.bv + have hD3 : D = (NonDetE.liftME Fl4ArchInvocation.extVSpace (do + NonDetE.unlessE (pos.shift = pageBitsForSize (frameSizeOfA sz)) + (NonDetE.throwError (.failedLookup false (.missingCapability pos.shift))) + let vm' := maskVmRights (vmRightsOfA rt) (dataToRights rightsMask.bv) + if vm' = ∅ then + NonDetE.returnOk (.map (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev none) cte .invalidPTE pos.slot) + else + NonDetE.returnOk (.map (.ext (.stage2FrameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev + (some (asidOfA asid1, ipa.bv)))) cte + (makeUserPte (addrFromPPtr (specPtr fp)) (attribsFromWord attr.bv) vm' (vmRightsOfA rt) (frameSizeOfA sz)) + pos.slot))).run σ := by + rw [hD2] + exact run_liftME_congrA (run_bind_okA (liftE_runA (m := NonDet.gets _) rfl) _) + by_cases hsh : lu.shift = psA sz + swap + · rw [if_pos (bne_iff_ne.2 hsh)] + have hne : pos.shift ≠ pageBitsForSize (frameSizeOfA sz) := fun heq => + hsh (UScalar.eq_of_val_eq (by rw [hlsh, heq, ← pbfs_eqA])) + refine (WP.spec_ok _).2 ⟨rfl, rfl, .failedLookup false (.missingCapability pos.shift), ?_, ?_⟩ + · rw [hD3] + exact run_liftME_errA (run_bind_errorA (by + simp only [NonDetE.unlessE, hne, ↓reduceIte, NonDetE.throwError]; rfl) _) + · exact Or.inl ⟨rfl, hlsh.symm⟩ + rw [if_neg (by rw [hsh, bne_self_eq_false]; exact Bool.false_ne_true)] + have hpsEq : pos.shift = pageBitsForSize (frameSizeOfA sz) := by + rw [← hlsh, hsh, pbfs_eqA] + rw [rights_readableA, bind_ok, + show layout.paddr fp = ok (core.num.U64.wrapping_sub fp layout.PPTR_BASE) from rfl, bind_ok] + -- Helper to finish after restart + set_cap + install_s2_leaf + have finish (ew : U64) (newC : capability.Cap) (newSC : ArchCap) (pte : PTE) + (hEnc : CapRt.EncWf newC) (hca : CapRelA newC (.archObjectCap newSC)) + (hnn : newC ≠ .Null) (hUA : UA newC (.Frame fp sz rt dev mp e asid va)) + (hObj : CapObjOkA Z newC) (hpt : ∀ p a v, newC ≠ .PageTable p true a v) + (hfe : CapFinEqA newC (.Frame fp sz rt dev mp e asid va)) + (hpte : DescRelE (G2E Z.T2 Z.Sh2) pos.shift ew pte ∧ S2AttrE pos.shift ew pte) + (hnotPt : ∀ ppn, pte ≠ .pageTablePTE ppn) + (hInv : D = ({(.ok (.extVSpace (.map newSC cte pte pos.slot)), σ)}, False)) : + (do + let ct ← sched.cur_thread st s + let s1 ← sched.set_thread_state st s ct sched.TS_RESTART + let s2 ← obj.Slot.set_cap st (cslotAddrA cte) s1 newC + let (s3, a1) ← ept.install_s2_leaf st ar s2 a lu ew root asid1 ipa fp dev attr + ok (core.result.Result.Ok (), s3, a1)) ⦃ res => VsTopPostA (L := L) AR Z OS m σ D s a res ⦄ := by + rw [cur_thread_ok, bind_ok] + obtain ⟨x, s2, σ2, _, hst, hset, hsc, hσ2, HS, hksb2, R2, hcpu2, hwi2, hJ2, hFr2⟩ := + restart_setcapJA E RS cte hab hos hEnc hca + rw [hst, bind_ok, hsc, bind_ok] + have hcap0 : capA st s (cslotAddrA cte) = .Frame fp sz rt dev mp e asid va := by + rw [← capAtA_eq]; exact hcap + obtain ⟨J2, T2⟩ := hJ2 hTr hnul hnn (by rw [hcap0]; intro h; cases h) (by rw [hcap0]; exact hUA) hObj + have hSlotAl : Aligned lu.slot := by + rcases hsl with ⟨t, i, ⟨ht, -⟩, hi, hluEq, -⟩ | ⟨r, j, hr, hj, hluEq, -, -⟩ + · rw [hluEq]; exact peAF_alignedA (Nat.mod_eq_zero_of_dvd (E.j.1.ext.tal t ht)) hi + · rw [hluEq]; exact E.j.1.ext.foot_alignedE E.j.1.vs.yal _ (rootFootE hr (E.j.1.ext.xal r hr) (by + have := s2GeomNumE.2.2.1; omega)) + refine WP.spec_bind (install_s2_leaf_mapA K J2.1.ar lu ew root asid1 ipa fp attr dev hSlotAl) fun r ⟨hW, hAR'⟩ => ?_ + have hExtOk : ExtSlotOkX σ2 pos.slot := extSlotOk_of_s2SlotA J2.1.ext hsl + have hPerf := performExtMap_memA (pte := pte) hσ2 hExtOk + -- Update VsRelA, ExtVsRelA, JAtZAX, TreeA, CarryA + have hfa2 : ∀ w, FootA (L := L) Z s2 w → Aligned w := + FootA.aligned J2.1 (fun c hc o h1 h2 => by rw [nodeAt_ksb hksb2]; exact bxi_nodes E.sys.ent.bxi c hc o h1 h2) + E.scAl E.ap.vcAl + have hEO2 : ExtTabOffA (L := L) Z m s2 := hEO.of_ksA hksb2 hcpu2 + have hTO2 : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s2 q := fun q hq => (hTO q hq).of_ksA hksb2 hcpu2 + have hK2 : Aligned (L.ksb s2) := hksb2 ▸ hK + have hT2 : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s2) := hksb2 ▸ hT + have hJ3 : JAtZAX (L := L) AR Z r.1 r.2 (extSlotWriteX σ2 pos.slot pte) m := by + rcases hsl with ⟨t, i, ⟨ht, hst'⟩, hi, hluEq, hslEq⟩ | ⟨r0, j0, hr0, hj0, hluEq, hslEq, hshEq⟩ + · rw [hluEq] at hW + rw [hslEq] + rw [← hst'] at hpte + have hE3 := J2.1.ext.of_ptWriteMapE J2.1.vs.yal ht hi hpte hW.2.2 (fun ppn h => absurd h (hnotPt ppn)) + have hV3 := J2.1.vs.of_s2WriteE (mem' := L.mem r.1) J2.1.ext (Or.inl ht) (J2.1.ext.tal t ht) hi + (hT2 t i hi (Or.inl (Or.inl ht))) hK2 (fun b hb hne => by rw [hW.2.2 b hb, if_neg (Ne.symm hne)]) + (Or.inl ht) (slotObjPtE (Nat.mod_eq_zero_of_dvd (J2.1.ext.tal t ht)) hi) pte + have hJ' := J2.of_extPageA hfa2 hTO2 hEO2 (Or.inl (Or.inl ht)) hW.1 hW.2.1 + (fun w hw hne => by rw [hW.2.2 w hw, if_neg (Ne.symm (hne i hi))]) + (Or.inr (Or.inr (Or.inl ht))) (fun p hp => + kheap_writeX (by rw [slotObjPtE (Nat.mod_eq_zero_of_dvd (J2.1.ext.tal t ht)) hi]; exact hp)) + (by rw [hW.1]; exact hV3) hE3 + exact hJ'.of_arA hAR' + · rw [hluEq] at hW + rw [hslEq] + rw [hshEq] at hpte + have hj1k : j0.val < 1024 := by have := s2GeomNumE.2.2.1; omega + have hE3 := J2.1.ext.of_rootWriteMapE J2.1.vs.yal hr0 hj1k hpte hW.2.2 (fun ppn h => absurd h (hnotPt ppn)) + obtain ⟨q, j', hq, hj', hpeq⟩ := rootWordE (J2.1.ext.xal r0 hr0) hj1k + have hqa : 4096 ∣ q.val := by + rcases hq with h | h <;> rw [h] + · exact al4kE (J2.1.ext.xal r0 hr0) + · exact al4kHiE (J2.1.ext.xal r0 hr0) + have hV3 := J2.1.vs.of_s2WriteE (mem' := L.mem r.1) J2.1.ext (Or.inr ⟨r0, hr0, hq⟩) hqa hj' + (hT2 q j' hj' (Or.inl (Or.inr ⟨r0, hr0, hq⟩))) hK2 + (fun b hb hne => by rw [hW.2.2 b hb, if_neg (by rw [hpeq]; exact Ne.symm hne)]) + (Or.inr ⟨r0, hr0, Or.inl rfl⟩) (sl := .root (specPtr r0) j0.val) rfl pte + have hJ' := J2.of_extPageA hfa2 hTO2 hEO2 (Or.inl (Or.inr ⟨r0, hr0, hq⟩)) hW.1 hW.2.1 + (fun w hw hne => by rw [hW.2.2 w hw, if_neg (by rw [hpeq]; exact Ne.symm (hne j' hj'))]) + (Or.inl hr0) (fun p hp => kheap_writeX (sl := .root (specPtr r0) j0.val) (pte := pte) hp) + (by rw [hW.1]; exact hV3) hE3 + exact hJ'.of_arA hAR' + have hoffOS : ¬ InOS OS lu.slot := by + have hft : ExtFootE Z.X Z.Y Z.T2 lu.slot := by + rcases hsl with ⟨t, i, ⟨ht, -⟩, hi, hluEq, -⟩ | ⟨r0, j0, hr0, hj0, hluEq, -, -⟩ + · rw [hluEq]; exact .s2pt t i ht hi + · rw [hluEq]; exact rootFootE hr0 (E.j.1.ext.xal r0 hr0) (by have := s2GeomNumE.2.2.1; omega) + exact (E.ap.ext lu.slot hft).1 + have hOSK : OSKeep st OS s2 r.1 := + osKeep_tabWriteA E.sys.ent.os hoffOS (fun w hw hne => by rw [hW.2.2 w hw, if_neg (Ne.symm hne)]) + have hTree3 : TreeA st OS Z.G.chains Z.G.par r.1 := T2.keepO hOSK + have hCarry3 : CarryA Z (L.mem s) σ → CarryA Z (L.mem r.1) (extSlotWriteX σ2 pos.slot pte) := by + have hTy : TyKeepA σ (extSlotWriteX σ2 pos.slot pte) := + TyKeepA.trans (setThreadState_tyA σ.curThread .restart σ _ (by rw [hset]; rfl)) + (TyKeepA.trans (setCap_tyA _ cte _ _ hσ2) + (storeExtSlot_tyA pos.slot pte _ _ (by rw [storeExtSlot_runX hExtOk pte]; rfl))) + have hFr3 : SlotFrA (L := L) OS (cslotAddrA cte) newC s r.1 := hFr2.keepR hos hOSK + exact fun C => C.of_slotFrA RS.sub hTr hFr3 rfl rfl (fun _ h => h) (fun _ h => h) + (fun p a v h => (hpt p a v h).elim) (by rw [hcap]; exact hfe) hTy + exact (WP.spec_ok _).2 ⟨.extVSpace (.map newSC cte pte pos.slot), + by rw [hInv]; exact ⟨rfl, not_false⟩, stUpdA σ σ.curThread x .restart, hset, + extSlotWriteX σ2 pos.slot pte, [], hPerf, Z, rfl, rfl, hJ3, hTree3, hCarry3⟩ + -- Case split on vm = kernelOnly vs ro/rw + rcases hvmRange with rfl | hvm23 + · rw [make_s2_leaf_zeroA _ _ _ _ _ (by unfold capability.VM_KERNEL_ONLY capability.VM_READ_WRITE; decide) + (by unfold capability.VM_KERNEL_ONLY capability.VM_READ_ONLY; decide), bind_ok] + simp only [bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + have hvmEmpty : maskVmRights (vmRightsOfA rt) (dataToRights rightsMask.bv) = ∅ := by + rw [← hvmEq]; unfold capability.VM_KERNEL_ONLY vmRightsOfA; rfl + have hInv : D = ({(.ok (.extVSpace (.map (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev none) + cte .invalidPTE pos.slot)), σ)}, False) := by + rw [hD3] + apply run_liftME_okA + rw [run_bind_okA (by simp only [NonDetE.unlessE, hpsEq, ↓reduceIte]; rfl)] + rw [if_pos hvmEmpty]; rfl + exact finish 0#u64 (.Frame fp sz rt dev false false 0#u64 0#u64) + (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev none) .invalidPTE + (by simp only [CapRt.EncWf]; exact ⟨hcanP, by omega, by decide, Or.inl (fun _ _ _ => by simp)⟩) + ⟨⟨hr1, hr3, ⟨by decide, fun _ => rfl, by simp, by simp⟩, hov⟩, rfl⟩ + (by intro h; cases h) (UA.of_shapeA rfl (by simp [isUtA])) + ⟨fun _ h => (by cases h), fun _ _ _ _ _ h => (by cases h), fun _ _ _ h => (by cases h), + fun _ _ _ h => (by cases h), fun h _ => (by cases h)⟩ + (fun _ _ _ h => by cases h) + (⟨by cases e <;> rfl, rfl, rfl, rfl⟩) ⟨descRelE_zero _ _, trivial⟩ (fun _ h => by cases h) hInv + · refine WP.spec_bind (make_s2_leaf_valA _ _ _ _ _ hvm23.symm) fun ew hew => ?_ + obtain ⟨hne0, hDesc, hAttr⟩ := s2_leaf_relA (G := G2E Z.T2 Z.Sh2) (R := vmRightsOfA rt) + rfl hvm23.symm hew FP.al FP.pa + have hvmNE : maskVmRights (vmRightsOfA rt) (dataToRights rightsMask.bv) ≠ ∅ := by + rw [← hvmEq] + intro h + have hm : Rights.allowRead ∈ (∅ : Set Rights) := by + rw [← h] + rcases hvm23 with rfl | rfl <;> simp [vmRO_litA, vmRW_litA, vmRightsOfA, vmReadOnly, vmReadWrite] + simp at hm + have hbne : (ew != 0#u64) = true := bne_iff_ne.2 hne0 + simp only [hbne, ↓reduceIte] + have hInv : D = ({(.ok (.extVSpace (.map (.ext (.stage2FrameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) + dev (some (asidOfA asid1, ipa.bv)))) cte + (makeUserPte (addrFromPPtr (specPtr fp)) (attribsFromWord attr.bv) (vmRightsOfA vm) + (vmRightsOfA rt) (frameSizeOfA sz)) pos.slot)), σ)}, False) := by + rw [hD3] + apply run_liftME_okA + rw [run_bind_okA (by simp only [NonDetE.unlessE, hpsEq, ↓reduceIte]; rfl)] + rw [if_neg hvmNE, ← hvmEq]; rfl + have hpaSpec : (core.num.U64.wrapping_sub fp layout.PPTR_BASE).bv = addrFromPPtr (specPtr fp) := + (addr_specA fp).symm + rw [hpaSpec, ← hsh, hlsh] at hDesc hAttr + exact finish ew (.Frame fp sz rt dev true true asid1 ipa) + (.ext (.stage2FrameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev (some (asidOfA asid1, ipa.bv)))) + (makeUserPte (addrFromPPtr (specPtr fp)) (attribsFromWord attr.bv) (vmRightsOfA vm) + (vmRightsOfA rt) (frameSizeOfA sz)) + (by simp only [CapRt.EncWf]; exact ⟨hcanP, by omega, by omega, hipa48⟩) + ⟨⟨hr1, hr3, ⟨ha16, by simp, fun _ => ha1, fun _ => rfl⟩, hov⟩, rfl⟩ + (by intro h; cases h) (UA.of_shapeA rfl (by simp [isUtA])) + ⟨fun _ h => (by cases h), fun _ _ _ _ _ h => (by cases h), fun _ _ _ h => (by cases h), + fun _ _ _ h => (by cases h), fun _ h => (by cases h)⟩ + (fun _ _ _ h => by cases h) + (⟨by cases e <;> rfl, rfl, rfl, rfl⟩) ⟨hDesc, hAttr⟩ + (fun _ h => by unfold makeUserPte at h; cases h) hInv + -- Split on mapped_asid != 0 (which is mp = true by CapWfA) + cases mp + · have hz : asid = 0#u64 := UScalar.eq_of_val_eq (hmz0 rfl) + subst hz + simp only [bne_self_eq_false, Bool.false_eq_true, ↓reduceIte, lift, bind_ok] + rw [s2LimitEq_loc hnvIpa, bind_ok] + let last := core.num.U64.wrapping_add ipa + (core.num.U64.wrapping_sub (⟨(1#u64 : U64).bv <<< (psA sz &&& 63#u64).val⟩ : U64) 1#u64) + have hD1 : D = (NonDetE.liftME Fl4ArchInvocation.extVSpace (do + NonDetE.whenE (last.bv > s2IpaLimit ∨ last.bv < ipa.bv) (NonDetE.throwError (.invalidArgument 0)) + let pos ← NonDetE.liftE (NonDet.gets fun s : State Unit => extLookupSlot s s2Geom (specPtr root) ipa.bv) + NonDetE.unlessE (pos.shift = pageBitsForSize (frameSizeOfA sz)) + (NonDetE.throwError (.failedLookup false (.missingCapability pos.shift))) + let vm' := maskVmRights (vmRightsOfA rt) (dataToRights rightsMask.bv) + if vm' = ∅ then + NonDetE.returnOk (.map (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev none) cte .invalidPTE pos.slot) + else + NonDetE.returnOk (.map (.ext (.stage2FrameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev + (some (asidOfA asid1, ipa.bv)))) cte + (makeUserPte (addrFromPPtr (specPtr fp)) (attribsFromWord attr.bv) vm' (vmRightsOfA rt) (frameSizeOfA sz)) + pos.slot))).run σ := by + rw [hD]; unfold decodeS2PageMapAt + rw [run_liftME_congrA (run_bind_okA (by rw [checkVpAlignment_runA, if_neg hal]) _)] + apply run_liftME_congrA + change ((NonDetE.whenE (ipa.bv + mask (pageBitsForSize (frameSizeOfA sz)) > s2IpaLimit ∨ + ipa.bv + mask (pageBitsForSize (frameSizeOfA sz)) < ipa.bv) (NonDetE.throwError (SyscallError.invalidArgument 0))) >>= + _).run σ = _ + rw [← last_eqA sz ipa] + by_cases hgt : last > (⟨s2IpaLimit⟩ : U64) + · rw [if_pos hgt, error.invalid_arg, bind_ok] + refine (WP.spec_ok _).2 ⟨rfl, rfl, .invalidArgument 0, ?_, Or.inl rfl⟩ + rw [hD1] + have hgt' : last.bv > s2IpaLimit := hgt + exact run_liftME_errA (run_bind_errorA (by rw [NonDetE.whenE, if_pos (Or.inl hgt')]; rfl) _) + rw [if_neg hgt] + by_cases hlt : last < ipa + · rw [if_pos hlt, error.invalid_arg, bind_ok] + refine (WP.spec_ok _).2 ⟨rfl, rfl, .invalidArgument 0, ?_, Or.inl rfl⟩ + rw [hD1] + have hlt' : last.bv < ipa.bv := hlt + exact run_liftME_errA (run_bind_errorA (by rw [NonDetE.whenE, if_pos (Or.inr hlt')]; rfl) _) + rw [if_neg hlt] + have hnot : ¬ (last.bv > s2IpaLimit ∨ last.bv < ipa.bv) := by + rintro (h | h) + · exact hgt h + · exact hlt h + have hipa48 : ipa.val < 2 ^ 48 := by + have h1 : last.val ≤ s2IpaLimit.toNat := Nat.not_lt.mp hgt + have h2 : ipa.val ≤ last.val := Nat.not_lt.mp hlt + have h3 := s2IpaLimit_lt48 + omega + exact tail hipa48 (by + rw [hD1] + exact run_liftME_congrA (run_bind_okA (by rw [NonDetE.whenE, if_neg hnot]; rfl) _)) + · have hnz : asid ≠ 0#u64 := hmz1 rfl + rw [if_pos (bne_iff_ne.2 hnz)] + have hD1 : D = (NonDetE.liftME Fl4ArchInvocation.extVSpace (do + NonDetE.whenE (asidOfA asid ≠ asidOfA asid1) (NonDetE.throwError (.invalidCapability 1)) + NonDetE.whenE (va.bv ≠ ipa.bv) (NonDetE.throwError (.invalidArgument 0)) + let pos ← NonDetE.liftE (NonDet.gets fun s : State Unit => extLookupSlot s s2Geom (specPtr root) ipa.bv) + NonDetE.unlessE (pos.shift = pageBitsForSize (frameSizeOfA sz)) + (NonDetE.throwError (.failedLookup false (.missingCapability pos.shift))) + let vm' := maskVmRights (vmRightsOfA rt) (dataToRights rightsMask.bv) + if vm' = ∅ then + NonDetE.returnOk (.map (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev none) cte .invalidPTE pos.slot) + else + NonDetE.returnOk (.map (.ext (.stage2FrameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev + (some (asidOfA asid1, ipa.bv)))) cte + (makeUserPte (addrFromPPtr (specPtr fp)) (attribsFromWord attr.bv) vm' (vmRightsOfA rt) (frameSizeOfA sz)) + pos.slot))).run σ := by + rw [hD]; unfold decodeS2PageMapAt + rw [run_liftME_congrA (run_bind_okA (by rw [checkVpAlignment_runA, if_neg hal]) _)] + apply run_liftME_congrA + simp only [mapDataA, ↓reduceIte] + by_cases hae : asid = asid1 + swap + · rw [if_pos (bne_iff_ne.2 hae), error.invalid_cap, bind_ok] + have hae' : asidOfA asid ≠ asidOfA asid1 := fun heq => hae (UScalar.eq_of_val_eq (by + have := congrArg BitVec.toNat heq + simp only [asidOfA, BitVec.toNat_ofNat, Nat.mod_eq_of_lt has16, Nat.mod_eq_of_lt ha16] at this + exact this)) + refine (WP.spec_ok _).2 ⟨rfl, rfl, .invalidCapability 1, ?_, Or.inr ⟨1#u64, rfl, rfl⟩⟩ + rw [hD1] + exact run_liftME_errA (run_bind_errorA (by rw [NonDetE.whenE, if_pos hae']; rfl) _) + subst hae + rw [if_neg (by simp)] + by_cases hve : va = ipa + swap + · rw [if_pos (bne_iff_ne.2 hve), error.invalid_arg, bind_ok] + have hve' : va.bv ≠ ipa.bv := fun heq => hve (UScalar.eq_of_val_eq (congrArg BitVec.toNat heq)) + refine (WP.spec_ok _).2 ⟨rfl, rfl, .invalidArgument 0, ?_, Or.inl rfl⟩ + rw [hD1] + exact run_liftME_errA ((run_bind_okA (by rw [NonDetE.whenE, if_neg (by simp)]; rfl) _).trans + (run_bind_errorA (by rw [NonDetE.whenE, if_pos hve']; rfl) _)) + subst hve + rw [if_neg (by simp)] + exact tail (h16_of_mp rfl) (by + rw [hD1] + exact run_liftME_congrA ((run_bind_okA (by rw [NonDetE.whenE, if_neg (by simp)]; rfl) _).trans + (run_bind_okA (by rw [NonDetE.whenE, if_neg (by simp)]; rfl) _))) + +end s2Map + +/-! ## HighVSpace `decode_high_page_map` refinement -/ + +section hiMap +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {s : S} {a : A} {σ : State Unit} {m : McState} +variable {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} {ia ie : Nat} + +set_option maxHeartbeats 4000000 in +/-- **`vspace.decode_high_page_map` against `decodeHighPageMap`** (module docstring). -/ +theorem hi_map_frame_jA {ar : machine.Arch A} (K : ArchKeepsA ar AR) + (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) + (hTO : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s q) (hEO : ExtTabOffA (L := L) Z m s) + (hK : Aligned (L.ksb s)) (hT : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s)) + (hTr : TreeA st OS Z.G.chains Z.G.par s) (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) + (cte : CslotPtr) (hab : AbsSlotA Z.th cte) (hos : cslotAddrA cte ∈ OS) + (fp rt asid va : U64) (sz : capability.FrameSize) (dev mp : Bool) + (hcap : capAtA (L.mem s) (cslotAddrA cte) = .Frame fp sz rt dev mp false asid va) + (FP : FrPlA sz fp mp asid) + (p1 asid1 : U64) (mp1 : Bool) (h16 : asid1.val < 2 ^ 16) + (vaddr rightsMask vm attr : U64) + (hvmEq : vmRightsOfA vm = maskVmRights (vmRightsOfA rt) (dataToRights rightsMask.bv)) + {D : Set (Except SyscallError Fl4ArchInvocation × State Unit) × Prop} + (hD : D = (NonDetE.liftME Fl4ArchInvocation.extVSpace + (decodeHighPageMap (Z := Unit) cte (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev + (mapDataA mp asid va)) (extRootCapA .high (specPtr p1) (asidDataA mp1 asid1)) + vaddr.bv rightsMask.bv attr.bv)).run σ) : + vspace.decode_high_page_map st ar s a (cslotAddrA cte) + { ptr := fp, size := sz, rights := rt, is_device := dev, mapped := mp, is_ept := false, asid := asid, vaddr := va } + (.HighVSpace p1 mp1 asid1) vaddr vm attr ⦃ res => VsTopPostA (L := L) AR Z OS m σ D s a res ⦄ := by + obtain ⟨c0, -, hrel0⟩ := StateRelA.getAbsA E.j.1.st hab + rw [hcap] at hrel0 + obtain ⟨⟨hr1, hr3, ⟨has16, hmz0, hmz1, _⟩, hov⟩, -⟩ := hrel0 + have hcanP : CapRt.Canonical fp := by + have := CapRt.decodeF_encWf (L.mem s (cslotAddrA cte)).bv (L.mem s (wa (cslotAddrA cte) 8#u64)).bv + change CapRt.EncWf (capAtA (L.mem s) (cslotAddrA cte)) at this + rw [hcap] at this + exact this.1 + have hsh63 : (psA sz &&& 63#u64).val < 64 := by cases sz <;> decide + have hX : ∀ r ∈ Z.X, ∃ es, σ.kheap (specPtr r) = some (.archObj (.ext (.stage2Root es))) := + fun r hr => let ⟨es, hk, _⟩ := E.j.1.ext.s2root r hr; ⟨es, hk⟩ + unfold vspace.decode_high_page_map + rw [cur_thread_ok, bind_ok] + refine WP.spec_bind (check_high_vspaceA E.j.1.vs hX p1 asid1 1#u64 mp1 (lt_of_lt_of_eq h16 (by norm_num))) fun r hr => ?_ + rcases r with ⟨root, asid'⟩ | err + swap + · obtain ⟨e', he', hrel⟩ := hr + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame.from, + bind_tc_ok, WP.spec_ok] + refine ⟨rfl, rfl, e', ?_, hrel⟩ + rw [hD]; unfold decodeHighPageMap + exact run_liftME_errA (run_bind_errorA he' _) + obtain ⟨⟨rfl, rfl⟩, hrY, has1nz, hchk⟩ := hr + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok, page_shift_eqA, lift] + rw [CapRt.shl_u64_eq 1#u64 _ hsh63] + simp only [bind_ok, lift] + rw [align_check_eqA sz vaddr] + by_cases hal : vaddr.bv &&& mask (pageBitsForSize (frameSizeOfA sz)) ≠ 0 + · simp only [decide_eq_true hal, ↓reduceIte] + refine (WP.spec_ok _).2 ⟨rfl, rfl, .alignmentError, ?_, Or.inl trivial⟩ + rw [hD]; unfold decodeHighPageMap + exact run_liftME_errA ((run_bind_okA hchk _).trans (run_bind_errorA (by rw [checkVpAlignment_runA, if_pos hal]) _)) + simp only [decide_eq_false hal, Bool.false_eq_true, ↓reduceIte] + -- Common tail after the mapped check + have tail (f' : vspace.FrameCap) + (hD2 : D = (NonDetE.liftME Fl4ArchInvocation.extVSpace (do + let last := vaddr.bv + mask (pageBitsForSize (frameSizeOfA sz)) + NonDetE.whenE (vaddr.bv < highVSpaceBase ∨ last ≥ highVSpaceTop ∨ last < vaddr.bv) + (NonDetE.throwError (.invalidArgument 0)) + let pos ← NonDetE.liftE (NonDet.gets fun s : State Unit => extLookupSlot s highGeom (specPtr p1) vaddr.bv) + NonDetE.unlessE (pos.shift = pageBitsForSize (frameSizeOfA sz)) + (NonDetE.throwError (.failedLookup false (.missingCapability pos.shift))) + let vm' := maskVmRights (vmRightsOfA rt) (dataToRights rightsMask.bv) + NonDetE.returnOk (.map (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev + (some (asidOfA asid1, vaddr.bv))) cte + (makeUserPte (addrFromPPtr (specPtr fp)) (attribsFromWord attr.bv) vm' (vmRightsOfA rt) (frameSizeOfA sz)) + pos.slot))).run σ) : + (let last := core.num.U64.wrapping_add vaddr + (core.num.U64.wrapping_sub (⟨(1#u64 : U64).bv <<< (psA sz &&& 63#u64).val⟩ : U64) 1#u64) + if vaddr < fl4_abi.consts.HIGH_VSPACE_BASE then do + let r1 ← error.invalid_arg Unit 0#u64; ok (r1, s, a) + else if last >= fl4_abi.consts.HIGH_VSPACE_TOP then do + let r1 ← error.invalid_arg Unit 0#u64; ok (r1, s, a) + else if last < vaddr then do + let r1 ← error.invalid_arg Unit 0#u64; ok (r1, s, a) + else do + let lu ← vspace.lookup_pt_slot st s p1 vaddr + if lu.shift != psA sz then + ok (core.result.Result.Err (error.Exc.Sys (error.SysError.FailedLookup false (error.LookupFault.MissingCapability lu.shift))), s, a) + else do + let cap_read ← vspace.rights_readable rt + let i7 ← layout.paddr fp + let ew ← vspace.make_user_page i7 vm cap_read attr (psA sz) + let s1 ← sched.set_thread_state st s (L.curW s) sched.TS_RESTART + let s2 ← obj.Slot.set_cap st (cslotAddrA cte) s1 (.Frame fp sz rt dev true false asid1 vaddr) + let (s3, a1) ← vspace.install_page st ar s2 a lu ew 0#u64 asid1 vaddr f' attr true + ok (core.result.Result.Ok (), s3, a1)) ⦃ res => VsTopPostA (L := L) AR Z OS m σ D s a res ⦄ := by + let last := core.num.U64.wrapping_add vaddr + (core.num.U64.wrapping_sub (⟨(1#u64 : U64).bv <<< (psA sz &&& 63#u64).val⟩ : U64) 1#u64) + have hBase : fl4_abi.consts.HIGH_VSPACE_BASE.bv = highVSpaceBase := by + unfold fl4_abi.consts.HIGH_VSPACE_BASE highVSpaceBase; decide + have hTop : fl4_abi.consts.HIGH_VSPACE_TOP.bv = highVSpaceTop := by + unfold fl4_abi.consts.HIGH_VSPACE_TOP highVSpaceTop; decide + by_cases h1 : vaddr < fl4_abi.consts.HIGH_VSPACE_BASE + · rw [if_pos h1, error.invalid_arg, bind_ok] + have h1' : vaddr.bv < highVSpaceBase := by rw [← hBase]; exact h1 + refine (WP.spec_ok _).2 ⟨rfl, rfl, .invalidArgument 0, ?_, Or.inl rfl⟩ + rw [hD2] + exact run_liftME_errA (run_bind_errorA (by rw [NonDetE.whenE, if_pos (Or.inl h1')]; rfl) _) + rw [if_neg h1] + by_cases h2 : last ≥ fl4_abi.consts.HIGH_VSPACE_TOP + · rw [if_pos h2, error.invalid_arg, bind_ok] + have h2' : vaddr.bv + mask (pageBitsForSize (frameSizeOfA sz)) ≥ highVSpaceTop := by + rw [← hTop, ← last_eqA]; exact h2 + refine (WP.spec_ok _).2 ⟨rfl, rfl, .invalidArgument 0, ?_, Or.inl rfl⟩ + rw [hD2] + exact run_liftME_errA (run_bind_errorA (by rw [NonDetE.whenE, if_pos (Or.inr (Or.inl h2'))]; rfl) _) + rw [if_neg h2] + by_cases h3 : last < vaddr + · rw [if_pos h3, error.invalid_arg, bind_ok] + have h3' : vaddr.bv + mask (pageBitsForSize (frameSizeOfA sz)) < vaddr.bv := by + rw [← last_eqA]; exact h3 + refine (WP.spec_ok _).2 ⟨rfl, rfl, .invalidArgument 0, ?_, Or.inl rfl⟩ + rw [hD2] + exact run_liftME_errA (run_bind_errorA (by rw [NonDetE.whenE, if_pos (Or.inr (Or.inr h3'))]; rfl) _) + rw [if_neg h3] + have hnot : ¬ (vaddr.bv < highVSpaceBase ∨ vaddr.bv + mask (pageBitsForSize (frameSizeOfA sz)) ≥ highVSpaceTop ∨ + vaddr.bv + mask (pageBitsForSize (frameSizeOfA sz)) < vaddr.bv) := by + rintro (h | h | h) + · exact h1 (by rw [← hBase] at h; exact h) + · exact h2 (by rw [← hTop, ← last_eqA] at h; exact h) + · exact h3 (by rw [← last_eqA] at h; exact h) + have hlo : fl4_abi.consts.HIGH_VSPACE_BASE ≤ vaddr := Nat.not_lt.mp h1 + have hactY := activeHighRootA_of_vspaceForAsid E.j.1.vs hX hrY + (vspaceForAsid_of_checkExtRootA (by decide) hchk) + refine WP.spec_bind (lookup_pt_slot_hiA E.j.1.vs E.j.1.ext hrY hactY (hi_idx39_eq vaddr hlo)) + fun lu ⟨hlsh, hslR⟩ => ?_ + have hsl : SlotRelE (G1E Z.S1 Z.Lv) (TopHiE Z.Y) lu.slot + (extLookupSlot σ highGeom (specPtr p1) vaddr.bv).slot + (extLookupSlot σ highGeom (specPtr p1) vaddr.bv).shift := + hslR.mono (fun _ _ g => g.1) (fun _ _ _ ht => ht.toTopHiE) + let pos := extLookupSlot σ highGeom (specPtr p1) vaddr.bv + have hD3 : D = (NonDetE.liftME Fl4ArchInvocation.extVSpace (do + NonDetE.unlessE (pos.shift = pageBitsForSize (frameSizeOfA sz)) + (NonDetE.throwError (.failedLookup false (.missingCapability pos.shift))) + let vm' := maskVmRights (vmRightsOfA rt) (dataToRights rightsMask.bv) + NonDetE.returnOk (.map (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev + (some (asidOfA asid1, vaddr.bv))) cte + (makeUserPte (addrFromPPtr (specPtr fp)) (attribsFromWord attr.bv) vm' (vmRightsOfA rt) (frameSizeOfA sz)) + pos.slot))).run σ := by + rw [hD2] + exact run_liftME_congrA ((run_bind_okA (by rw [NonDetE.whenE, if_neg hnot]; rfl) _).trans + (run_bind_okA (liftE_runA (m := NonDet.gets _) rfl) _)) + by_cases hsh : lu.shift = psA sz + swap + · rw [if_pos (bne_iff_ne.2 hsh)] + have hne : pos.shift ≠ pageBitsForSize (frameSizeOfA sz) := fun heq => + hsh (UScalar.eq_of_val_eq (by rw [hlsh, heq, ← pbfs_eqA])) + refine (WP.spec_ok _).2 ⟨rfl, rfl, .failedLookup false (.missingCapability pos.shift), ?_, ?_⟩ + · rw [hD3] + exact run_liftME_errA (run_bind_errorA (by + simp only [NonDetE.unlessE, hne, ↓reduceIte, NonDetE.throwError]; rfl) _) + · exact Or.inl ⟨rfl, hlsh.symm⟩ + rw [if_neg (by rw [hsh, bne_self_eq_false]; exact Bool.false_ne_true)] + have hpsEq : pos.shift = pageBitsForSize (frameSizeOfA sz) := by rw [← hlsh, hsh, pbfs_eqA] + rw [rights_readableA, bind_ok, + show layout.paddr fp = ok (core.num.U64.wrapping_sub fp layout.PPTR_BASE) from rfl, bind_ok] + refine WP.spec_bind (mup_specA _ vm attr (psA sz) _) fun ew hew => ?_ + have hEnc : CapRt.EncWf (.Frame fp sz rt dev true false asid1 vaddr) := by + simp only [CapRt.EncWf]; exact ⟨hcanP, by omega, by omega, hi_canA vaddr hlo⟩ + have hca : CapRelA (.Frame fp sz rt dev true false asid1 vaddr) + (.archObjectCap (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev (some (asidOfA asid1, vaddr.bv)))) := + ⟨⟨hr1, hr3, ⟨h16, by simp, fun _ => has1nz, by simp⟩, hov⟩, rfl⟩ + obtain ⟨x, s2, σ2, _, hst, hset, hsc, hσ2, HS, hksb2, R2, hcpu2, hwi2, hJ2, hFr2⟩ := + restart_setcapJA E RS cte hab hos hEnc hca + rw [hst, bind_ok, hsc, bind_ok] + have hcap0 : capA st s (cslotAddrA cte) = .Frame fp sz rt dev mp false asid va := by + rw [← capAtA_eq]; exact hcap + obtain ⟨J2, T2⟩ := hJ2 hTr hnul (by intro h; cases h) (by rw [hcap0]; intro h; cases h) + (by rw [hcap0]; exact UA.of_shapeA rfl (by simp [isUtA])) + ⟨fun _ h => (by cases h), fun _ _ _ _ _ h => (by cases h), fun _ _ _ h => (by cases h), + fun _ _ _ h => (by cases h), fun _ _ => Or.inr ⟨by + have hb : fl4_abi.consts.HIGH_VSPACE_BASE.val = 18446743936270598144 := by + unfold fl4_abi.consts.HIGH_VSPACE_BASE; decide + have : fl4_abi.consts.HIGH_VSPACE_BASE.val ≤ vaddr.val := hlo + omega, by + have ht : fl4_abi.consts.HIGH_VSPACE_TOP.val = 18446744056529682432 := by + unfold fl4_abi.consts.HIGH_VSPACE_TOP; decide + have h2' : last.val < fl4_abi.consts.HIGH_VSPACE_TOP.val := Nat.lt_of_not_ge h2 + have h3' : vaddr.val ≤ last.val := Nat.not_lt.mp h3 + omega⟩⟩ + have hSlotAl : Aligned lu.slot := by + rcases hsl with ⟨t, i, ⟨ht, -⟩, hi, hluEq, -⟩ | ⟨y, j, hy, hj, hluEq, -, -⟩ + · rw [hluEq]; exact peAF_alignedA (E.j.1.vs.al_mod ht) hi + · rw [hluEq]; exact peAF_alignedA (waWin_modA (E.j.1.vs.yal y hy)) hj + refine WP.spec_bind (install_page_hiA K J2.1.ar lu ew 0#u64 asid1 vaddr attr _ hSlotAl) fun r ⟨hW, hAR'⟩ => ?_ + let pte := makeUserPte (addrFromPPtr (specPtr fp)) (attribsFromWord attr.bv) + (vmRightsOfA vm) (vmRightsOfA rt) (frameSizeOfA sz) + have hExtOk : ExtSlotOkX σ2 pos.slot := extSlotOk_of_hiSlotA J2.1.vs hsl + have hPerf := performExtMap_memA (pte := pte) hσ2 hExtOk + have hInv : D = ({(.ok (.extVSpace (.map (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev + (some (asidOfA asid1, vaddr.bv))) cte pte pos.slot)), σ)}, False) := by + rw [hD3] + apply run_liftME_okA + rw [run_bind_okA (by simp only [NonDetE.unlessE, hpsEq, ↓reduceIte]; rfl), ← hvmEq]; rfl + have hfa2 : ∀ w, FootA (L := L) Z s2 w → Aligned w := + FootA.aligned J2.1 (fun c hc o h1 h2 => by rw [nodeAt_ksb hksb2]; exact bxi_nodes E.sys.ent.bxi c hc o h1 h2) + E.scAl E.ap.vcAl + have hEO2 : ExtTabOffA (L := L) Z m s2 := hEO.of_ksA hksb2 hcpu2 + have hTO2 : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s2 q := fun q hq => (hTO q hq).of_ksA hksb2 hcpu2 + have hK2 : Aligned (L.ksb s2) := hksb2 ▸ hK + have hT2 : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s2) := hksb2 ▸ hT + have hpaSpec : (core.num.U64.wrapping_sub fp layout.PPTR_BASE).bv = addrFromPPtr (specPtr fp) := + (addr_specA fp).symm + have hPteAt (k : Nat) (hk1 : 0 < k) (hk3 : k ≤ 3) (hshk : shiftA k = (psA sz).val) : + PteRelA Z.S1 Z.Lv k ew pte := by + have hkEq : decide (k = 3) = decide (psA sz = 12#u64) := by + rcases (show k = 1 ∨ k = 2 ∨ k = 3 by omega) with rfl | rfl | rfl <;> + cases sz <;> simp [shiftA, psA] at hshk ⊢ + have hsmEq : decide (frameSizeOfA sz = .armSmallPage) = decide (k = 3) := by + rcases (show k = 1 ∨ k = 2 ∨ k = 3 by omega) with rfl | rfl | rfl <;> + cases sz <;> simp [shiftA, psA, frameSizeOfA] at hshk ⊢ + have h1 := pteRel_mupA (Tb := Z.S1) (Lv := Z.Lv) hk1 hk3 (R := vmRightsOfA rt) rfl + (by rw [hkEq]; exact hew) (by rw [hshk]; exact FP.al) FP.pa + unfold pte makeUserPte + rw [hsmEq, ← hpaSpec] + exact h1 + have hJ3 : JAtZAX (L := L) AR Z r.1 r.2 (extSlotWriteX σ2 pos.slot pte) m := by + rcases hsl with ⟨t, i, ⟨ht, h1t, hst⟩, hi, hluEq, hslEq⟩ | ⟨y, j, hy, hj, hluEq, hslEq, hshEq⟩ + · rw [hluEq] at hW + rw [hslEq] + have h3t : Z.Lv t ≤ 3 := J2.1.vs.lv t ht + have hshk : shiftA (Z.Lv t) = (psA sz).val := by + unfold shiftA; rw [← hst, ← hlsh, hsh] + have hV3 := J2.1.vs.of_ptWriteMapE hK2 ht h1t hi (hT2 t i hi (Or.inr (Or.inr (Or.inl ht)))) + (hPteAt (Z.Lv t) (by omega) h3t hshk) hW.2.2 + (fun _ h => by unfold pte makeUserPte at h; cases h) + have hE3 := J2.1.ext.of_vsWriteE (mem' := L.mem r.1) J2.1.vs.yal (Or.inr (Or.inl ht)) (J2.1.vs.al t ht) hi + (fun b hb hne => by rw [hW.2.2 b hb, if_neg (Ne.symm hne)]) (Or.inl ht) + (fun hp => by obtain ⟨_, h1, -⟩ := J2.1.vs.pool t hp; obtain ⟨_, h2, -⟩ := J2.1.vs.pt t ht; rw [h1] at h2; cases h2) + (slotObjPtE (J2.1.vs.al_mod ht) hi) pte + have hJ' := J2.of_extPageA hfa2 hTO2 hEO2 (Or.inr (Or.inr ht)) hW.1 hW.2.1 + (fun w hw hne => by rw [hW.2.2 w hw, if_neg (Ne.symm (hne i hi))]) + (Or.inr (Or.inr (Or.inr ht))) (fun p hp => + kheap_writeX (by rw [slotObjPtE (J2.1.vs.al_mod ht) hi]; exact hp)) + (by rw [hW.1]; exact hV3) hE3 + exact hJ'.of_arA hAR' + · rw [hluEq] at hW + rw [hslEq] + have hshk : shiftA 1 = (psA sz).val := by + have : pos.shift = 30 := hshEq + rw [← hlsh, hsh] at this; unfold shiftA; omega + have hV3 := J2.1.vs.of_hiRootWriteMapE hK2 hy hj (hT2 (wa y 4096#u64) j hj (Or.inr (Or.inr (Or.inr ⟨y, hy, rfl⟩)))) + (hPteAt 1 (by omega) (by omega) hshk) (J2.1.ext.ypp y hy) hW.2.2 + (fun _ h => by unfold pte makeUserPte at h; cases h) + have hE3 := J2.1.ext.of_vsWriteE (mem' := L.mem r.1) J2.1.vs.yal (Or.inr (Or.inr ⟨y, hy, rfl⟩)) + (Nat.dvd_of_mod_eq_zero (waWin_modA (J2.1.vs.yal y hy))) hj + (fun b hb hne => by rw [hW.2.2 b hb, if_neg (Ne.symm hne)]) (sl := .root (specPtr y) j.val) (Or.inr hy) + (fun hp => J2.1.ext.ypg y hy (Or.inl hp)) rfl pte + have hJ' := J2.of_extPageA hfa2 hTO2 hEO2 (Or.inr (Or.inl ⟨y, hy, rfl⟩)) hW.1 hW.2.1 + (fun w hw hne => by rw [hW.2.2 w hw, if_neg (Ne.symm (hne j hj))]) + (Or.inr (Or.inl hy)) (fun p hp => kheap_writeX (sl := .root (specPtr y) j.val) (pte := pte) hp) + (by rw [hW.1]; exact hV3) hE3 + exact hJ'.of_arA hAR' + have hoffOS : ¬ InOS OS lu.slot := by + have hft : VsFootA Z.Pp Z.S1 Z.Y (L.ksb s) lu.slot := by + rcases hsl with ⟨t, i, ⟨ht, -⟩, hi, hluEq, -⟩ | ⟨y, j, hy, hj, hluEq, -, -⟩ + · rw [hluEq]; exact .pt t i ht hi + · rw [hluEq]; exact .high y j hy hj + exact (E.ap.vs lu.slot hft).1 + have hOSK : OSKeep st OS s2 r.1 := + osKeep_tabWriteA E.sys.ent.os hoffOS (fun w hw hne => by rw [hW.2.2 w hw, if_neg (Ne.symm hne)]) + have hTree3 : TreeA st OS Z.G.chains Z.G.par r.1 := T2.keepO hOSK + have hCarry3 : CarryA Z (L.mem s) σ → CarryA Z (L.mem r.1) (extSlotWriteX σ2 pos.slot pte) := by + have hTy : TyKeepA σ (extSlotWriteX σ2 pos.slot pte) := + TyKeepA.trans (setThreadState_tyA σ.curThread .restart σ _ (by rw [hset]; rfl)) + (TyKeepA.trans (setCap_tyA _ cte _ _ hσ2) + (storeExtSlot_tyA pos.slot pte _ _ (by rw [storeExtSlot_runX hExtOk pte]; rfl))) + have hFr3 : SlotFrA (L := L) OS (cslotAddrA cte) (.Frame fp sz rt dev true false asid1 vaddr) s r.1 := + hFr2.keepR hos hOSK + exact fun C => C.of_slotFrA RS.sub hTr hFr3 rfl rfl (fun _ h => h) (fun _ h => h) + (fun _ _ _ h => by cases h) (by rw [hcap]; exact ⟨rfl, rfl, rfl, rfl⟩) hTy + exact (WP.spec_ok _).2 ⟨.extVSpace (.map (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev + (some (asidOfA asid1, vaddr.bv))) cte pte pos.slot), + by rw [hInv]; exact ⟨rfl, not_false⟩, stUpdA σ σ.curThread x .restart, hset, + extSlotWriteX σ2 pos.slot pte, [], hPerf, Z, rfl, rfl, hJ3, hTree3, hCarry3⟩ + -- Split on f.asid != 0 (which is mp = true by CapWfA) + change (if (asid != 0#u64) = true then _ else _) ⦃ _ ⦄ + cases mp + · have hz : asid = 0#u64 := UScalar.eq_of_val_eq (hmz0 rfl) + subst hz + simp only [bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + exact tail _ (by + rw [hD]; unfold decodeHighPageMap; dsimp only + have hchk1 : (checkExtRoot .high (extRootCapA .high (specPtr p1) (asidDataA mp1 asid1)) 1).run σ = + ({(.ok (specPtr p1, asidOfA asid1), σ)}, False) := hchk + exact run_liftME_congrA ((run_bind_okA hchk1 _).trans + ((run_bind_okA (by rw [checkVpAlignment_runA, if_neg hal]) _).trans + (run_bind_okA (by simp [mapDataA]; rfl) _)))) + · have hnz : asid ≠ 0#u64 := hmz1 rfl + rw [if_pos (bne_iff_ne.2 hnz)] + have hD1 : D = (NonDetE.liftME Fl4ArchInvocation.extVSpace (do + NonDetE.whenE (asidOfA asid ≠ asidOfA asid1) (NonDetE.throwError (.invalidCapability 1)) + NonDetE.whenE (va.bv ≠ vaddr.bv) (NonDetE.throwError (.invalidArgument 0)) + let last := vaddr.bv + mask (pageBitsForSize (frameSizeOfA sz)) + NonDetE.whenE (vaddr.bv < highVSpaceBase ∨ last ≥ highVSpaceTop ∨ last < vaddr.bv) + (NonDetE.throwError (.invalidArgument 0)) + let pos ← NonDetE.liftE (NonDet.gets fun s : State Unit => extLookupSlot s highGeom (specPtr p1) vaddr.bv) + NonDetE.unlessE (pos.shift = pageBitsForSize (frameSizeOfA sz)) + (NonDetE.throwError (.failedLookup false (.missingCapability pos.shift))) + let vm' := maskVmRights (vmRightsOfA rt) (dataToRights rightsMask.bv) + NonDetE.returnOk (.map (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev + (some (asidOfA asid1, vaddr.bv))) cte + (makeUserPte (addrFromPPtr (specPtr fp)) (attribsFromWord attr.bv) vm' (vmRightsOfA rt) (frameSizeOfA sz)) + pos.slot))).run σ := by + rw [hD]; unfold decodeHighPageMap; dsimp only + have hchk1 : (checkExtRoot .high (extRootCapA .high (specPtr p1) (asidDataA mp1 asid1)) 1).run σ = + ({(.ok (specPtr p1, asidOfA asid1), σ)}, False) := hchk + rw [run_liftME_congrA ((run_bind_okA hchk1 _).trans (run_bind_okA (by rw [checkVpAlignment_runA, if_neg hal]) _))] + apply run_liftME_congrA + simp only [mapDataA, ↓reduceIte] + by_cases hae : asid = asid1 + swap + · rw [if_pos (bne_iff_ne.2 hae), error.invalid_cap, bind_ok] + have hae' : asidOfA asid ≠ asidOfA asid1 := fun heq => hae (UScalar.eq_of_val_eq (by + have := congrArg BitVec.toNat heq + simp only [asidOfA, BitVec.toNat_ofNat, Nat.mod_eq_of_lt has16, Nat.mod_eq_of_lt h16] at this + exact this)) + refine (WP.spec_ok _).2 ⟨rfl, rfl, .invalidCapability 1, ?_, Or.inr ⟨1#u64, rfl, rfl⟩⟩ + rw [hD1] + exact run_liftME_errA (run_bind_errorA (by rw [NonDetE.whenE, if_pos hae']; rfl) _) + subst hae + rw [if_neg (by simp)] + by_cases hve : va = vaddr + swap + · rw [if_pos (bne_iff_ne.2 hve), error.invalid_arg, bind_ok] + have hve' : va.bv ≠ vaddr.bv := fun heq => hve (UScalar.eq_of_val_eq (congrArg BitVec.toNat heq)) + refine (WP.spec_ok _).2 ⟨rfl, rfl, .invalidArgument 0, ?_, Or.inl rfl⟩ + rw [hD1] + exact run_liftME_errA ((run_bind_okA (by rw [NonDetE.whenE, if_neg (by simp)]; rfl) _).trans + (run_bind_errorA (by rw [NonDetE.whenE, if_pos hve']; rfl) _)) + subst hve + rw [if_neg (by simp)] + exact tail _ (by + rw [hD1] + exact run_liftME_congrA ((run_bind_okA (by rw [NonDetE.whenE, if_neg (by simp)]; rfl) _).trans + (run_bind_okA (by rw [NonDetE.whenE, if_neg (by simp)]; rfl) _))) + +end hiMap + +/-! ## Combined `ARMPageMap` top (all frame and extra-cap combinations) -/ + +section pageMapAll +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {s : S} {a : A} {σ : State Unit} {m : McState} +variable {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} {ia ie : Nat} + +private theorem vm_rangeA (rt : U64) (r : capability.Rights) : + capability.mask_vm_rights rt r ⦃ vm => + vm = capability.VM_KERNEL_ONLY ∨ vm = capability.VM_READ_ONLY ∨ vm = capability.VM_READ_WRITE ⦄ := by + unfold capability.mask_vm_rights + repeat (first | exact (WP.spec_ok _).2 (by simp) | split) + +private theorem isPageFlush_pgMapA : + isPageFlushLabel (invocationType (specLabelWord (archL .armPageMap))) = false := by + rw [invType_pgMapA]; rfl + +private theorem dec_pgMap_s1_noExtRootA (calling : Bool) (args : List Data) (xSlot : CapRef) (cte : CslotPtr) + (exs : List (Cap × CslotPtr)) (p : ObjRef) (R : CapRights) (sz : VMPageSize) (dev : Bool) + (mp : Option (Asid × VSpaceRef)) + (hno : ∀ c sl rest, exs = (c, sl) :: rest → + (∀ q a, c ≠ .archObjectCap (.ext (.stage2VSpaceCap q a))) ∧ + (∀ q a, c ≠ .archObjectCap (.ext (.highVSpaceCap q a)))) : + fl4ArchDecodeInvocation (Z := Unit) calling (archL .armPageMap) args xSlot cte + (.frameCap p R sz dev mp) exs = + NonDetE.liftME Fl4ArchInvocation.spec + (archDecodeInvocation (specLabelWord (archL .armPageMap)) args xSlot cte + (.frameCap p R sz dev mp) exs) := by + unfold fl4ArchDecodeInvocation + simp only [show archL .armPageMap ≠ .ext .armPageDeriveSubframe by decide, + show archL .armPageMap ≠ .ext .armPageTestAndClearAccessed by decide, + isPageFlush_pgMapA, + show archL .armPageMap ≠ archL .armPageUnmap by decide, + ↓reduceIte, Bool.false_eq_true] + by_cases h : args.length > 2 ∧ exs.length > 0 + · rw [if_pos ⟨trivial, h⟩] + cases exs with + | nil => simp at h + | cons x rest => + obtain ⟨h1, h2⟩ := hno x.1 x.2 rest rfl + dsimp only [List.getElem?_cons_zero, Fl4SpecA64.the] + cases hx : x.1 with + | archObjectCap ac => + cases ac with + | ext e => + cases e with + | stage2VSpaceCap q a => exact (h1 q a hx).elim + | highVSpaceCap q a => exact (h2 q a hx).elim + | stage2FrameCap _ _ _ _ _ => rfl + | _ => rfl + | _ => rfl + · rw [if_neg (fun ⟨_, h'⟩ => h h')] + +set_option maxHeartbeats 4000000 in +/-- **All `ARMPageMap` arms** (Stage-1, Stage-2, `ArmHighVSpace`, plain and `is_ept` frames), +retiring `NoExtXA` and `e = false` via `CapWfA`'s canonical frame form. -/ +theorem page_map_all_jA {ar : machine.Arch A} (K : ArchKeepsA ar AR) + (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) + (hTO : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s q) (hEO : ExtTabOffA (L := L) Z m s) + (hK : Aligned (L.ksb s)) (hT : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s)) (hIpa : S2IpaE ar a) + (hTr : TreeA st OS Z.G.chains Z.G.par s) (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) + (cte : CslotPtr) (hab : AbsSlotA Z.th cte) (hos : cslotAddrA cte ∈ OS) + (fp rt asid va : U64) (sz : capability.FrameSize) (dev mp e : Bool) + (hcap : capAtA (L.mem s) (cslotAddrA cte) = .Frame fp sz rt dev mp e asid va) + (FP : FrPlA sz fp mp asid) + (length buf : U64) (call calling : Bool) (ex : Aeneas.Std.Array obj.Slot 3#usize) + (args : List U64) (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) + (x0 : U64) (hx0 : Aeneas.Std.Array.index_usize ex 0#usize = ok x0) + (xSlot : CapRef) (exs : List (Cap × CslotPtr)) + (hex : ExtraRelA Z.th (L.mem s) x0 (exs.map Prod.fst)) : + vspace.decode_frame st ar s a fl4_abi.label.ARMPageMap length (cslotAddrA cte) + (.Frame fp sz rt dev mp e asid va) call buf ex ⦃ res => + VsTopPostA (L := L) AR Z OS m σ ((fl4ArchDecodeInvocation (Z := Unit) calling + (archL .armPageMap) (args.map UScalar.bv) xSlot cte + (absFrameE e (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev (mapDataA mp asid va)) exs).run σ) s a res ⦄ := by + obtain ⟨c0, -, hrel0⟩ := StateRelA.getAbsA E.j.1.st hab + rw [hcap] at hrel0 + obtain ⟨⟨hr1, hr3, ⟨has16, hmz0, hmz1, hemp1⟩, hov⟩, -⟩ := hrel0 + have hX : ∀ r ∈ Z.X, ∃ es, σ.kheap (specPtr r) = some (.archObj (.ext (.stage2Root es))) := + fun r hr => let ⟨es, hk, _⟩ := E.j.1.ext.s2root r hr; ⟨es, hk⟩ + -- First check if e = false and extra cap is neither Ept nor HighVSpace -> delegate to existing page_map_jA + by_cases hS1 : e = false ∧ (∀ p l mp1 a1 v1, capAtA (L.mem s) x0 ≠ .Ept p l mp1 a1 v1) ∧ + (∀ p mp1 a1, capAtA (L.mem s) x0 ≠ .HighVSpace p mp1 a1) + · obtain ⟨rfl, hneEpt, hneHi⟩ := hS1 + have hno : ∀ c sl rest, exs = (c, sl) :: rest → + (∀ q a, c ≠ .archObjectCap (.ext (.stage2VSpaceCap q a))) ∧ + (∀ q a, c ≠ .archObjectCap (.ext (.highVSpaceCap q a))) := by + rintro c sl rest rfl + rcases hex with ⟨-, hnil⟩ | ⟨-, -, a', rest', ⟨-, habs⟩, hm⟩ + · simp at hnil + simp only [List.map_cons, List.cons.injEq] at hm + obtain ⟨rfl, -⟩ := hm + generalize hcc : capAtA (L.mem s) x0 = cc at hneEpt hneHi habs + cases cc with + | Ept _ _ _ _ _ => exact (hneEpt _ _ _ _ _ rfl).elim + | HighVSpace _ _ _ => exact (hneHi _ _ _ rfl).elim + | Frame _ _ _ _ _ isE _ _ => + cases isE <;> constructor <;> intro q a h <;> simp [CapEqA, absCapA, absFrameE] at habs <;> rw [← habs] at h <;> cases h + | PageTable p m asid va => + cases m + · constructor <;> intro q a h <;> simp [CapEqA, absCapA] at habs <;> rw [← habs] at h <;> cases h + · obtain ⟨_, _, rfl, _, _⟩ := CapEqA.pt_mapped_iff.mp habs + constructor <;> intro q a h <;> cases h + | _ => constructor <;> intro q a h <;> simp [CapEqA, absCapA] at habs <;> rw [← habs] at h <;> cases h + rw [show absFrameE false (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev (mapDataA mp asid va) = + .frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev (mapDataA mp asid va) from rfl, + dec_pgMap_s1_noExtRootA calling _ xSlot cte exs _ _ _ _ _ hno] + exact vsTop_ofPgMapA (page_map_jA ar K E RS cte hab hos hcap FP _ invType_pgMapA length buf args hlen hargs call + x0 hx0 exs hex hneEpt hneHi hTr hnul hTO) + -- Otherwise, unfold decode_frame -> decode_page_map + have hct : sched.cur_thread st s = ok (L.curW s) := cur_thread_ok s + unfold vspace.decode_frame + simp only [vspace.as_frame, hct, bind_ok, ↓reduceIte] + unfold vspace.decode_page_map + rw [hct, bind_ok, vspace.extra, hx0, bind_ok] + simp only [show ¬ (0#usize ≥ 3#usize) by decide, ↓reduceIte, obj.Slot.is_null, bind_ok] + -- Truncated message cases (x0 = 0 or length < 3) + by_cases hx0z : x0 = 0#u64 + · simp only [hx0z, decide_true, ↓reduceIte, bind_ok, error.truncated] + have hnil : exs = [] := by + rcases hex with ⟨-, h⟩ | ⟨hne, -⟩ + · exact List.map_eq_nil_iff.mp h + · exact (hne hx0z).elim + refine (WP.spec_ok _).2 ⟨rfl, rfl, .truncatedMessage, ?_, Or.inl trivial⟩ + subst hnil + cases e <;> ( + unfold fl4ArchDecodeInvocation + simp only [show archL .armPageMap ≠ .ext .armPageDeriveSubframe by decide, + show archL .armPageMap ≠ .ext .armPageTestAndClearAccessed by decide, + isPageFlush_pgMapA, + show archL .armPageMap ≠ archL .armPageUnmap by decide, + List.length_nil, gt_iff_lt, Nat.not_lt_zero, and_false, ↓reduceIte, Bool.false_eq_true] + apply run_liftME_errA + try unfold absFrameE + unfold archDecodeInvocation decodeFrameInvocation + rw [invType_pgMapA] + exact frDec_truncA (specLabelWord (archL .armPageMap)) _ _ _ _ _ _ _ _ _ (by simp)) + simp only [decide_eq_false hx0z, Bool.false_eq_true, ↓reduceIte, bind_ok] + by_cases hlen3 : length ≥ 3#u64 + swap + · rw [if_neg hlen3, error.truncated, bind_ok] + have hlt3 : (args.map UScalar.bv).length < 3 := by + rw [List.length_map, ← hlen]; exact Nat.lt_of_not_ge hlen3 + refine (WP.spec_ok _).2 ⟨rfl, rfl, .truncatedMessage, ?_, Or.inl trivial⟩ + cases e <;> ( + unfold fl4ArchDecodeInvocation + simp only [show archL .armPageMap ≠ .ext .armPageDeriveSubframe by decide, + show archL .armPageMap ≠ .ext .armPageTestAndClearAccessed by decide, + isPageFlush_pgMapA, + show archL .armPageMap ≠ archL .armPageUnmap by decide, + gt_iff_lt, show ¬ 2 < (args.map UScalar.bv).length by omega, false_and, and_false, ↓reduceIte, Bool.false_eq_true] + apply run_liftME_errA + try unfold absFrameE + unfold archDecodeInvocation decodeFrameInvocation + rw [invType_pgMapA] + exact frDec_truncA (specLabelWord (archL .armPageMap)) _ _ _ _ _ _ _ _ _ (by omega)) + rw [if_pos hlen3] + have hlenA : 3 ≤ args.length := by rw [← hlen]; exact hlen3 + obtain ⟨a0, a1, a2, restA, rfl⟩ : ∃ a0 a1 a2 rest, args = a0 :: a1 :: a2 :: rest := by + rcases args with _ | ⟨a0, _ | ⟨a1, _ | ⟨a2, rest⟩⟩⟩ <;> simp at hlenA + exact ⟨a0, a1, a2, rest, rfl⟩ + rw [hargs 0#u64 a0 rfl, bind_ok, hargs 1#u64 a1 rfl, bind_ok, hargs 2#u64 a2 rfl, bind_ok] + rcases hex with ⟨hz, -⟩ | ⟨-, habx, vc, restX, hrelX, hmapX⟩ + · exact (hx0z hz).elim + obtain ⟨xSlot0, restEx, rfl⟩ : ∃ sl rest', exs = (vc, sl) :: rest' := by + cases exs with + | nil => simp at hmapX + | cons p rest' => + cases p with + | mk c sl => + simp only [List.map_cons, List.cons.injEq] at hmapX + exact ⟨sl, rest', by rw [hmapX.1]⟩ + rw [from_word_eqA, bind_ok] + obtain ⟨vm, hvmOk, hvmEq, hvmRange⟩ := spec_ex_fd (WP.spec_and (mask_vmA rt (rightsWA a1)) (vm_rangeA rt (rightsWA a1))) + rw [hvmOk, bind_ok] + have hvmSpec : vmRightsOfA vm = maskVmRights (vmRightsOfA rt) (dataToRights a1.bv) := by + rw [hvmEq, rightsW_specA] + have hslotCap : obj.Slot.cap st x0 s = ok (capAtA (L.mem s) x0) := by + rw [capAtA_eq, capA_eq] + rw [hslotCap, bind_ok] + obtain ⟨hwfX, habsX⟩ := hrelX + -- Case split on vcap = capAtA (L.mem s) x0 + cases hvc : capAtA (L.mem s) x0 with + | Ept p1 lvl1 mp1 asid1 va1 => + rw [hvc] at hwfX habsX + obtain ⟨hlvl, h16⟩ := hwfX + have hl0 : lvl1 = 0#u64 := UScalar.eq_of_val_eq hlvl + subst hl0 + have hvcEq : vc = extRootCapA .stage2 (specPtr p1) (asidDataA mp1 asid1) := by + rw [← habsX]; rfl + subst hvcEq + simp only [bind_ok, ↓reduceIte] + refine WP.spec_bind (ept_rootA E.j.1.vs hX p1 asid1 va1 1#u64 mp1 (lt_of_lt_of_eq h16 (by norm_num))) fun r hr => ?_ + have hD_s2 : (fl4ArchDecodeInvocation (Z := Unit) calling (archL .armPageMap) + ((a0 :: a1 :: a2 :: restA).map UScalar.bv) xSlot cte + (absFrameE e (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev (mapDataA mp asid va)) + ((extRootCapA .stage2 (specPtr p1) (asidDataA mp1 asid1), xSlot0) :: restEx)).run σ = + (NonDetE.liftME Fl4ArchInvocation.extVSpace + (decodeS2PageMap (Z := Unit) cte + (absFrameE e (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev (mapDataA mp asid va)) + (extRootCapA .stage2 (specPtr p1) (asidDataA mp1 asid1)) a0.bv a1.bv a2.bv)).run σ := by + cases e <;> ( + unfold fl4ArchDecodeInvocation + simp only [show archL .armPageMap ≠ .ext .armPageDeriveSubframe by decide, + show archL .armPageMap ≠ .ext .armPageTestAndClearAccessed by decide, + isPageFlush_pgMapA, + List.map_cons, List.length_cons, gt_iff_lt, Nat.zero_lt_succ, + show 2 < restA.length + 1 + 1 + 1 by omega, and_self, ↓reduceIte, Bool.false_eq_true, + List.getElem?_cons_zero, List.getElem?_cons_succ, Fl4SpecA64.the, extRootCapA, absFrameE]; rfl) + rcases r with ⟨root, asid'⟩ | err + · obtain ⟨⟨rfl, rfl⟩, hrX, ha1nz, hchk⟩ := hr + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok] + have hactR := activeS2RootA_of_vspaceForAsid E.j.1.vs hrX + (vspaceForAsid_of_checkExtRootA (by decide) hchk) + cases e + · cases mp + · have hz : asid = 0#u64 := UScalar.eq_of_val_eq (hmz0 rfl) + subst hz + change (if (0#u64 != 0#u64) = true then _ else _) ⦃ _ ⦄ + simp only [bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + exact s2_map_frame_jA K E RS hTO hEO hK hT hIpa hTr hnul cte hab hos fp rt 0#u64 va sz dev false false + hcap FP rfl p1 asid1 a0 vm a2 hrX hactR ha1nz h16 hvmRange hvmSpec (by + rw [hD_s2]; unfold decodeS2PageMap absFrameE + exact run_liftME_congrA ((run_bind_okA hchk _).trans + (run_bind_okA (by simp [mapDataA, NonDetE.whenE]; rfl) _))) + · have hnz : asid ≠ 0#u64 := hmz1 rfl + change (if (asid != 0#u64) = true then _ else _) ⦃ _ ⦄ + rw [if_pos (bne_iff_ne.2 hnz), if_neg Bool.false_ne_true, error.invalid_cap, bind_ok] + refine (WP.spec_ok _).2 ⟨rfl, rfl, .invalidCapability 1, ?_, Or.inr ⟨1#u64, rfl, rfl⟩⟩ + rw [hD_s2]; unfold decodeS2PageMap absFrameE + exact run_liftME_errA ((run_bind_okA hchk _).trans (run_bind_errorA (by + simp only [mapDataA, NonDetE.whenE, ne_eq, reduceCtorEq, not_false_eq_true, ↓reduceIte, + NonDetE.throwError]; rfl) _)) + · have hmp1 : mp = true := hemp1 rfl + subst hmp1 + have hnz : asid ≠ 0#u64 := hmz1 rfl + change (if (asid != 0#u64) = true then _ else _) ⦃ _ ⦄ + rw [if_pos (bne_iff_ne.2 hnz), if_pos rfl] + exact s2_map_frame_jA K E RS hTO hEO hK hT hIpa hTr hnul cte hab hos fp rt asid va sz dev true true + hcap FP rfl p1 asid1 a0 vm a2 hrX hactR ha1nz h16 hvmRange hvmSpec (by + rw [hD_s2]; unfold decodeS2PageMap absFrameE + exact run_liftME_congrA (run_bind_okA hchk _)) + · obtain ⟨e', he', hrel⟩ := hr + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame.from, + bind_tc_ok, WP.spec_ok] + refine ⟨rfl, rfl, e', ?_, hrel⟩ + rw [hD_s2] + cases e <;> (unfold decodeS2PageMap absFrameE; exact run_liftME_errA (run_bind_errorA he' _)) + | HighVSpace p1 mp1 asid1 => + rw [hvc] at hwfX habsX + have hvcEq : vc = extRootCapA .high (specPtr p1) (asidDataA mp1 asid1) := by rw [← habsX]; rfl + subst hvcEq + simp only [bind_ok, Bool.false_eq_true, ↓reduceIte] + cases e + · have hD_hi : (fl4ArchDecodeInvocation (Z := Unit) calling (archL .armPageMap) + ((a0 :: a1 :: a2 :: restA).map UScalar.bv) xSlot cte + (absFrameE false (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev (mapDataA mp asid va)) + ((extRootCapA .high (specPtr p1) (asidDataA mp1 asid1), xSlot0) :: restEx)).run σ = + (NonDetE.liftME Fl4ArchInvocation.extVSpace + (decodeHighPageMap (Z := Unit) cte + (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev (mapDataA mp asid va)) + (extRootCapA .high (specPtr p1) (asidDataA mp1 asid1)) a0.bv a1.bv a2.bv)).run σ := by + unfold fl4ArchDecodeInvocation + simp only [show archL .armPageMap ≠ .ext .armPageDeriveSubframe by decide, + show archL .armPageMap ≠ .ext .armPageTestAndClearAccessed by decide, + isPageFlush_pgMapA, + List.map_cons, List.length_cons, gt_iff_lt, Nat.zero_lt_succ, + show 2 < restA.length + 1 + 1 + 1 by omega, and_self, ↓reduceIte, Bool.false_eq_true, + List.getElem?_cons_zero, List.getElem?_cons_succ, Fl4SpecA64.the, extRootCapA, absFrameE]; rfl + cases mp + · have hz : asid = 0#u64 := UScalar.eq_of_val_eq (hmz0 rfl) + subst hz + simp only [bne_self_eq_false, Bool.false_eq_true, ↓reduceIte, bind_ok] + exact hi_map_frame_jA K E RS hTO hEO hK hT hTr hnul cte hab hos fp rt 0#u64 va sz dev false + hcap FP p1 asid1 mp1 hwfX a0 a1 vm a2 hvmSpec hD_hi + · have hnz : asid ≠ 0#u64 := hmz1 rfl + rw [if_pos (bne_iff_ne.2 hnz), if_neg Bool.false_ne_true] + exact hi_map_frame_jA K E RS hTO hEO hK hT hTr hnul cte hab hos fp rt asid va sz dev true + hcap FP p1 asid1 mp1 hwfX a0 a1 vm a2 hvmSpec hD_hi + · have hmp1 : mp = true := hemp1 rfl + subst hmp1 + have hnz : asid ≠ 0#u64 := hmz1 rfl + rw [if_pos (bne_iff_ne.2 hnz), if_pos rfl, error.invalid_cap, bind_ok] + refine (WP.spec_ok _).2 ⟨rfl, rfl, .invalidCapability 1, ?_, Or.inr ⟨1#u64, rfl, rfl⟩⟩ + unfold fl4ArchDecodeInvocation + simp only [show archL .armPageMap ≠ .ext .armPageDeriveSubframe by decide, + show archL .armPageMap ≠ .ext .armPageTestAndClearAccessed by decide, + isPageFlush_pgMapA, + List.map_cons, List.length_cons, gt_iff_lt, Nat.zero_lt_succ, + show 2 < restA.length + 1 + 1 + 1 by omega, and_self, ↓reduceIte, Bool.false_eq_true, + List.getElem?_cons_zero, Fl4SpecA64.the, extRootCapA, absFrameE, NonDetE.throwError] + rfl + | _ => + -- Here e must be true (otherwise hS1 would have held) + have he1 : e = true := by + cases e with + | true => rfl + | false => + exfalso + exact hS1 ⟨rfl, (fun _ _ _ _ _ h => by rw [hvc] at h; cases h), (fun _ _ _ h => by rw [hvc] at h; cases h)⟩ + subst he1 + have hmp1 : mp = true := hemp1 rfl + subst hmp1 + have hnz : asid ≠ 0#u64 := hmz1 rfl + simp only [bind_ok, Bool.false_eq_true, ↓reduceIte, bne_iff_ne.2 hnz, error.invalid_cap] + refine (WP.spec_ok _).2 ⟨rfl, rfl, .invalidCapability 1, ?_, Or.inr ⟨1#u64, rfl, rfl⟩⟩ + have hnotS2 : ∀ q a', vc ≠ .archObjectCap (.ext (.stage2VSpaceCap q a')) := by + rw [hvc] at habsX + intro q a' h; subst h + simp only [CapEqA] at habsX + first + | (split at habsX <;> simp [absCapA, absFrameE] at habsX) + | (simp only [absCapA, absFrameE] at habsX; first | cases habsX | (split at habsX <;> cases habsX)) + unfold fl4ArchDecodeInvocation + simp only [show archL .armPageMap ≠ .ext .armPageDeriveSubframe by decide, + show archL .armPageMap ≠ .ext .armPageTestAndClearAccessed by decide, + isPageFlush_pgMapA, + List.map_cons, List.length_cons, gt_iff_lt, Nat.zero_lt_succ, + show 2 < restA.length + 1 + 1 + 1 by omega, and_self, ↓reduceIte, Bool.false_eq_true, + List.getElem?_cons_zero, Fl4SpecA64.the, absFrameE] + cases hvc' : vc with + | archObjectCap ac => + cases ac with + | ext ex' => + cases ex' with + | stage2VSpaceCap q a' => exact (hnotS2 q a' hvc').elim + | _ => rfl + | _ => rfl + | _ => rfl + +end pageMapAll + +end +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/ExtMapPtA.lean b/proofs/Fl4ProofsA64/P3/ExtMapPtA.lean new file mode 100644 index 000000000..003fb16b2 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ExtMapPtA.lean @@ -0,0 +1,1575 @@ +/- +P3 (aarch64, A64-P3 S3 gen 40): **Stage-2 and `ArmHighVSpace` `ARMPageTableMap`** — FL4's +`ept::map_table` (`ept.rs` 174–197) and `vspace::map_high_page_table` (`vspace.rs` 1149–1164) +against `fl4ArchDecodeInvocation` at label 47 (`ARMPageTableMap`), `decodeS2PageTableMap` +(`Ext/Stage2`) and `decodeHighPageTableMap` (`Ext/ArmHighVSpace`), followed by +`set_thread_state Restart` and `performExtVSpace (.map …)`, with `VsTopPostA`. + +* **48-bit `PageTable` VA normalization** (Architect 09:59Z ruling 1): `unpack48A_canonical`, + `set_cap_pt_unpack48A` — `obj.Slot.set_cap` on `.PageTable p m asid va` (with `va < 2^48`) + writes the exact same words as `.PageTable p m asid ⟨unpack48A va.bv⟩`, which is `CapRt.EncWf` + and relates to `va.bv` via `CapEqA`. +* **Table-extension helpers**: `DescRelE.monoE`, `ExtVsRelA.addS2PtE`, `ExtVsRelA.addS1PtE`, + `descRelE_s2tabA`. +* **Arms and combined top**: `s2_map_pt_jA`, `hi_map_pt_jA`, `pt_map_all_jA`, and + `decInv_pt_all_jA` (all labels on `Cap::PageTable`, retiring `NoExtXA`). + +Named premises: `ArchKeepsA` [P], `S2IpaE` [P] (standing). No new named premise. +-/ +import Fl4ProofsA64.P3.ExtMapFrA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## 48-bit `PageTable` VA encoding bridge -/ + +section ptEnc + +theorem unpack48A_canonical {va : BitVec 64} (h48 : va.toNat < 2 ^ 48) : + CapRt.Canonical (⟨unpack48A va⟩ : U64) := by + unfold unpack48A CapRt.Canonical + by_cases h : va &&& 0x800000000000#64 = 0#64 + · rw [if_pos h] + left + intro j hj hj47 + by_cases hj48 : 48 ≤ j + · exact CapRt.high_bits_false h48 j hj hj48 + · have hjEq : j = 47 := by omega + subst hjEq + have h0 : (va &&& 0x800000000000#64)[47] = false := by rw [h]; simp + simpa [BitVec.getElem_and] using h0 + · rw [if_neg h] + right + intro j hj hj47 + show (va ||| 0xffff000000000000#64)[j] = true + simp only [BitVec.getElem_or] + by_cases hj48 : 48 ≤ j + · have hhi : (0xffff000000000000#64 : BitVec 64)[j] = true := by + simp only [BitVec.getElem_eq_testBit_toNat, BitVec.toNat_ofNat] + interval_cases j <;> decide + rw [hhi, Bool.or_true] + · have hjEq : j = 47 := by omega + subst hjEq + have hb47 : va[47] = true := by + by_contra hne + have hf : va[47] = false := Bool.eq_false_iff.mpr hne + apply h + apply BitVec.eq_of_getElem_eq + intro i hi + simp only [BitVec.getElem_and] + by_cases hi47 : i = 47 + · subst hi47; simp [hf] + · have hz : (0x800000000000#64 : BitVec 64)[i] = false := by + have : (0x800000000000 : Nat) = 2 ^ 47 := by norm_num + change (0x800000000000 : Nat).testBit i = false + rw [this, Nat.testBit_two_pow]; simp [Ne.symm hi47] + simp [hz] + rw [hb47, Bool.true_or] + +theorem set_cap_pt_unpack48A {S : Type} {st : machine.Store S} (sl : obj.Slot) (s : S) + (p asid va : U64) (m : Bool) (h48 : va.val < 2 ^ 48) : + obj.Slot.set_cap st sl s (.PageTable p m asid va) = + obj.Slot.set_cap st sl s (.PageTable p m asid ⟨unpack48A va.bv⟩) := by + unfold obj.Slot.set_cap + have henc : capability.Cap.encode (.PageTable p m asid va) = + capability.Cap.encode (.PageTable p m asid ⟨unpack48A va.bv⟩) := by + rw [CapRt.encode_eq, CapRt.encode_eq] + simp only [CapRt.encodeF] + have hm1 : va.bv &&& 0xffffffffffff#64 = va.bv := by + apply BitVec.eq_of_toNat_eq + simp only [BitVec.toNat_and, BitVec.toNat_ofNat] + exact (Nat.and_two_pow_sub_one_eq_mod va.bv.toNat 48).trans (Nat.mod_eq_of_lt h48) + rw [hm1, unpack48A_and_m48 h48] + rw [henc] + +end ptEnc + +/-! ## Table-extension helpers -/ + +section tabExt + +theorem DescRelE.monoE {G1 G2 : U64 → Nat → Prop} (hm : ∀ t s, G1 t s → G2 t s) + {sh : Nat} {w : U64} {pte : PTE} (h : DescRelE G1 sh w pte) : DescRelE G2 sh w pte := by + cases pte with + | invalidPTE => exact h + | pageTablePTE ppn => exact ⟨h.1, h.2.1, h.2.2.1, h.2.2.2.1, h.2.2.2.2.1, hm _ _ h.2.2.2.2.2⟩ + | pagePTE a sm at' r => exact h + +theorem descRelE_invA {G : U64 → Nat → Prop} {sh : Nat} {w : U64} {pte : PTE} + (h : DescRelE G sh w pte) : (pte = .invalidPTE ↔ dBitA w 0 = false) := by + cases pte with + | invalidPTE => exact ⟨fun _ => h, fun _ => rfl⟩ + | pageTablePTE ppn => + exact ⟨fun e => (by cases e), fun hf => by rw [h.2.1] at hf; cases hf⟩ + | pagePTE a sm at' r => + exact ⟨fun e => (by cases e), fun hf => by rw [h.1] at hf; cases hf⟩ + +theorem ExtVsRelA.addS2PtE {Pp Tb X Y T2 : Set U64} {Sh2 : U64 → Nat} {mem : U64 → U64} {σ : State Unit} + (E : ExtVsRelA Pp Tb X Y T2 Sh2 mem σ) {ptr : U64} {sh' : Nat} + (hnp : ptr ∉ Pp) (hnt : ptr ∉ Tb) (hS2 : ptr ∉ S2PgE X T2) (hY : ptr ∉ Y) + (hal : 4096 ∣ ptr.val) (hytb : ∀ y ∈ Y, wa y 4096#u64 ≠ ptr) (hsh' : 12 ≤ sh' ∧ sh' ≤ 39) + (hinv : ∀ i : U64, i.val < 512 → dBitA (mem (peAF ptr i)) 0 = false) + {pt : PT} (hk : σ.kheap (specPtr ptr) = some (.archObj (.pageTable pt))) + (hty : ptType pt = .normalPtT) + (he : ∀ i : U64, i.val < 512 → ptApply pt i.bv = .invalidPTE) : + ExtVsRelA Pp Tb X Y (insert ptr T2) (Function.update Sh2 ptr sh') mem σ := by + have hnt2 : ptr ∉ T2 := fun h => hS2 (Or.inl h) + have hG : ∀ t s, G2E T2 Sh2 t s → G2E (insert ptr T2) (Function.update Sh2 ptr sh') t s := by + rintro t s ⟨ht, rfl⟩ + have hne : t ≠ ptr := fun e => hnt2 (e ▸ ht) + exact ⟨Set.mem_insert_of_mem _ ht, Function.update_of_ne hne _ _⟩ + have hReach : ∀ {q}, S2ReachA Pp X (insert ptr T2) (Function.update Sh2 ptr sh') mem σ q → + S2ReachA Pp X T2 Sh2 mem σ q := by + intro q hq + induction hq with + | root ha => exact .root ha + | @stepRoot r i ha hi h3 _ => + have hi1k : i.val < 1024 := by have := s2GeomNumE.2.2.1; omega + obtain ⟨es, -, her⟩ := E.s2root r ha.1 + have hd := (her ha i hi1k hi).1 + obtain ⟨hb0, hb1⟩ := (and3_bitsA _).1 h3 + generalize es ⟨i.val, hi1k⟩ = pte at hd + cases pte with + | invalidPTE => exact absurd (hd.symm.trans hb0) Bool.false_ne_true + | pageTablePTE _ => exact .stepRoot ha hi h3 hd.2.2.2.2.2.1 + | pagePTE _ _ _ _ => + have h12 : s2Geom.topShift = 12 := hd.2.1.1 hb1 + rcases s2GeomNumE.1 with h | h <;> omega + | @stepPt t i _ ht' h12' hi h3 _ ih => + obtain ⟨hb0, hb1⟩ := (and3_bitsA _).1 h3 + rcases Set.mem_insert_iff.1 ht' with rfl | ht + · exact absurd ((hinv i hi).symm.trans hb0) Bool.false_ne_true + · have hne : t ≠ ptr := fun e => hnt2 (e ▸ ht) + rw [Function.update_of_ne hne] at h12' + obtain ⟨pt0, -, -, he0⟩ := E.s2pt t ht + have hd := (he0 ih i hi).1 + generalize ptApply pt0 i.bv = pte at hd + cases pte with + | invalidPTE => exact absurd (hd.symm.trans hb0) Bool.false_ne_true + | pageTablePTE _ => exact .stepPt ih ht h12' hi h3 hd.2.2.2.2.2.1 + | pagePTE _ _ _ _ => have := hd.2.1.1 hb1; omega + refine ⟨?_, ?_, ?_, E.wl0, E.wlo, E.xal, ?_, ?_, ?_, E.ypg, E.ypp⟩ + · intro r hr + obtain ⟨es, hkr, her⟩ := E.s2root r hr + exact ⟨es, hkr, fun ha i hi hmi => ⟨(her ha i hi hmi).1.monoE hG, (her ha i hi hmi).2⟩⟩ + · intro t ht + rcases Set.mem_insert_iff.1 ht with rfl | ht' + · refine ⟨pt, hk, hty, fun _ i hi => ?_⟩ + rw [he i hi] + exact ⟨hinv i hi, trivial⟩ + · have hne : t ≠ ptr := fun e => hnt2 (e ▸ ht') + obtain ⟨pt0, hk0, hty0, he0⟩ := E.s2pt t ht' + refine ⟨pt0, hk0, hty0, fun hrc i hi => ?_⟩ + rw [Function.update_of_ne hne] + exact ⟨(he0 (hReach hrc) i hi).1.monoE hG, (he0 (hReach hrc) i hi).2⟩ + · intro t ht + rcases Set.mem_insert_iff.1 ht with rfl | ht' + · rw [Function.update_self]; exact hsh' + · have hne : t ≠ ptr := fun e => hnt2 (e ▸ ht') + rw [Function.update_of_ne hne]; exact E.tsh t ht' + · intro t ht + rcases Set.mem_insert_iff.1 ht with rfl | ht' + · exact hal + · exact E.tal t ht' + · intro q hq + rcases hq with hq | hq + · rcases Set.mem_insert_iff.1 hq with rfl | hq' + · refine ⟨?_, hY⟩ + rintro (h | h | ⟨y, hy, h⟩) + · exact hnp h + · exact hnt h + · exact hytb y hy h.symm + · exact E.pgd q (Or.inl hq') + · exact E.pgd q (Or.inr hq) + · intro t ht r hr + rcases Set.mem_insert_iff.1 ht with rfl | ht' + · exact ⟨fun e => hS2 (Or.inr ⟨r, hr, Or.inl e⟩), fun e => hS2 (Or.inr ⟨r, hr, Or.inr e⟩)⟩ + · exact E.t2x t ht' r hr + +theorem ExtVsRelA.addS1PtE {Pp Tb X Y T2 : Set U64} {Sh2 : U64 → Nat} {mem : U64 → U64} {σ : State Unit} + (E : ExtVsRelA Pp Tb X Y T2 Sh2 mem σ) {ptr : U64} (hS2 : ptr ∉ S2PgE X T2) (hY : ptr ∉ Y) : + ExtVsRelA Pp (insert ptr Tb) X Y T2 Sh2 mem σ := + ⟨E.s2root, E.s2pt, E.tsh, E.wl0, E.wlo, E.xal, E.tal, + fun q hq => ⟨by + rintro (h | h | h) + · exact (E.pgd q hq).1 (Or.inl h) + · rcases Set.mem_insert_iff.1 h with rfl | h' + · exact hS2 hq + · exact (E.pgd q hq).1 (Or.inr (Or.inl h')) + · exact (E.pgd q hq).1 (Or.inr (Or.inr h)), (E.pgd q hq).2⟩, + E.t2x, + fun y hy h => by + rcases h with h | h | h + · exact E.ypg y hy (Or.inl h) + · rcases Set.mem_insert_iff.1 h with rfl | h' + · exact hY hy + · exact E.ypg y hy (Or.inr (Or.inl h')) + · exact E.ypg y hy (Or.inr (Or.inr h)), + E.ypp⟩ + +theorem descRelE_s2tabA {G : U64 → Nat → Prop} {sh : Nat} {v ptr : U64} + (hsh : 12 < sh) (hv3 : (v &&& 3#u64) = 3#u64) + (hvm : (v &&& ept.ADDR_MASK) = core.num.U64.wrapping_sub ptr layout.PPTR_BASE) + (hpa : (core.num.U64.wrapping_sub ptr layout.PPTR_BASE).val < 2 ^ 44) + (hG : G ptr (sh - 9)) : + DescRelE G sh v (.pageTablePTE (ppnFromPptr (specPtr ptr))) ∧ + S2AttrE sh v (.pageTablePTE (ppnFromPptr (specPtr ptr))) := by + have hb := (and3_bitsA v).1 hv3 + have hdaddr : dAddrA v = core.num.U64.wrapping_sub ptr layout.PPTR_BASE := by + unfold dAddrA + have hmask : (281474976706560#u64 : U64) = ept.ADDR_MASK := by + unfold ept.ADDR_MASK; rfl + rw [hmask, hvm] + have hdtab : dTabA v = ptr := by + unfold dTabA + rw [hdaddr, show (18446603336221196288#u64 : U64) = layout.PPTR_BASE by unfold layout.PPTR_BASE; rfl] + exact wa_ws ptr layout.PPTR_BASE + have hppn : ppnFromPptr (specPtr ptr) = ppnA v := by + apply BitVec.eq_of_toNat_eq + simp only [ppnFromPptr, ppnA, ucast, pageBits, ppnLen, hdaddr, addr_specA, + BitVec.toNat_ofNat, BitVec.toNat_ushiftRight, BitVec.toNat_setWidth, Nat.shiftRight_eq_div_pow] + rfl + exact ⟨⟨hsh, hb.1, hb.2, by rw [hdaddr]; exact hpa, hppn, by rw [hdtab]; exact hG⟩, trivial⟩ + +end tabExt + +/-! ## Local helpers -/ + +section helpers + +private theorem extWalkN_shift_pt {Z : Type} (s : State Z) (va : Word) : + ∀ (n : Nat) (p : ExtPos), + ∃ k, k ≤ n ∧ (extWalkN s va n p).shift = p.shift - 9 * k := by + intro n + induction n with + | zero => intro p; exact ⟨0, le_refl 0, by simp [extWalkN]⟩ + | succ n ih => + intro p + unfold extWalkN + dsimp only + split + · obtain ⟨k, hk, heq⟩ := ih ⟨extNextSlot va (p.slot.read s) p.shift, p.shift - ptTranslationBits .normalPtT⟩ + refine ⟨k + 1, Nat.succ_le_succ hk, ?_⟩ + rw [heq] + change p.shift - 9 - 9 * k = p.shift - 9 * (k + 1) + omega + · exact ⟨0, Nat.zero_le _, by simp⟩ + +private theorem s2Lookup_shift_pt (σ : State Unit) (r : ObjRef) (va : Word) : + let sh := (extLookupSlot σ s2Geom r va).shift + sh = 39 ∨ sh = 30 ∨ sh = 21 ∨ sh = 12 := by + obtain ⟨k, hk, heq⟩ := extWalkN_shift_pt σ va (s2Geom.levels - 1) (extTopPos s2Geom r va) + dsimp only + unfold extLookupSlot + rw [heq] + have hgeom : (s2Geom.levels = 3 ∧ (extTopPos s2Geom r va).shift = 30) ∨ + (s2Geom.levels = 4 ∧ (extTopPos s2Geom r va).shift = 39) := by + unfold extTopPos s2Geom s2GeomOf + split_ifs <;> simp + rcases hgeom with ⟨hl, ht⟩ | ⟨hl, ht⟩ + · rw [hl] at hk; rw [ht]; interval_cases k <;> simp + · rw [hl] at hk; rw [ht]; interval_cases k <;> simp + +private theorem hiLookup_shift_pt (σ : State Unit) (r : ObjRef) (va : Word) : + let sh := (extLookupSlot σ highGeom r va).shift + sh = 30 ∨ sh = 21 ∨ sh = 12 := by + obtain ⟨k, hk, heq⟩ := extWalkN_shift_pt σ va (highGeom.levels - 1) (extTopPos highGeom r va) + have hl : highGeom.levels = 3 := rfl + have ht : (extTopPos highGeom r va).shift = 30 := rfl + dsimp only + unfold extLookupSlot + rw [heq, ht] + rw [hl] at hk + interval_cases k <;> simp + +private theorem s2LimitEq_pt {n : U64} (h : n.val = fl4A64Platform.s2IpaBits) : + ept.ipa_limit n = ok (⟨s2IpaLimit⟩ : U64) := by + unfold ept.ipa_limit ept.geom s2IpaLimit s2IpaBitsOf + by_cases h42 : n ≤ 42#u64 + · have h42' : fl4A64Platform.s2IpaBits ≤ 42 := by rw [← h]; exact h42 + rw [if_pos h42, if_pos h42', bind_ok, CapRt.shl_u64_eq 1#u64 40#u64 (by decide), bind_ok] + rfl + · have h42' : ¬ fl4A64Platform.s2IpaBits ≤ 42 := fun h' => h42 (show n.val ≤ 42 by rw [h]; exact h') + rw [if_neg h42, if_neg h42'] + by_cases h44 : n = 44#u64 + · have h44' : fl4A64Platform.s2IpaBits = 44 := by rw [← h, h44]; rfl + rw [if_pos h44, if_pos h44', bind_ok, CapRt.shl_u64_eq 1#u64 44#u64 (by decide), bind_ok] + rfl + · have h44' : fl4A64Platform.s2IpaBits ≠ 44 := fun h' => h44 (UScalar.eq_of_val_eq (by rw [h, h']; rfl)) + rw [if_neg h44, if_neg h44', bind_ok, CapRt.shl_u64_eq 1#u64 48#u64 (by decide), bind_ok] + rfl + +private theorem s2IpaLimit_lt48_pt : s2IpaLimit.toNat < 2 ^ 48 := by + unfold s2IpaLimit s2IpaBitsOf + split_ifs <;> decide + +private theorem hi_idx39_eq_pt (va : U64) (hlo : fl4_abi.consts.HIGH_VSPACE_BASE ≤ va) : + idxA va 39#u64 = 511#u64 := by + have h1 : 18446743936270598144 ≤ va.val := by + have hb : fl4_abi.consts.HIGH_VSPACE_BASE.val = 18446743936270598144 := by + unfold fl4_abi.consts.HIGH_VSPACE_BASE; rfl + rw [← hb]; exact hlo + have h2 : va.val < 2 ^ 64 := va.bv.isLt + apply UScalar.eq_of_val_eq + rw [idxA_val] + show (va.val / 2 ^ 39) % 512 = 511 + omega + +private theorem hi_base_ge_pt (va : U64) (hlo : fl4_abi.consts.HIGH_VSPACE_BASE ≤ va) {sh : Nat} (hsh : sh ≤ 30) : + fl4_abi.consts.HIGH_VSPACE_BASE ≤ (va &&& ~~~(⟨BitVec.ofNat 64 (2 ^ sh - 1)⟩ : U64)) := by + have hlo' : (18446743936270598144 : Nat) ≤ va.val := by + have hb : fl4_abi.consts.HIGH_VSPACE_BASE.val = 18446743936270598144 := by + unfold fl4_abi.consts.HIGH_VSPACE_BASE; rfl + rw [← hb]; exact hlo + have hlt : va.val < 2 ^ 64 := va.bv.isLt + have hbase : (18446743936270598144 : Nat) = 2 ^ 64 - 2 ^ 37 := by norm_num + have hmask : (~~~(⟨BitVec.ofNat 64 (2 ^ sh - 1)⟩ : U64)).val = 2 ^ 64 - 2 ^ sh := by + have hlt2 : 2 ^ sh - 1 < 2 ^ 64 := by + have : 2 ^ sh ≤ 2 ^ 30 := Nat.pow_le_pow_right (by norm_num) hsh + omega + show (~~~BitVec.ofNat 64 (2 ^ sh - 1)).toNat = _ + rw [BitVec.toNat_not, BitVec.toNat_ofNat, Nat.mod_eq_of_lt hlt2] + have : 1 ≤ 2 ^ sh := Nat.one_le_two_pow + omega + have hge : 2 ^ 64 - 2 ^ 37 ≤ (va &&& ~~~(⟨BitVec.ofNat 64 (2 ^ sh - 1)⟩ : U64)).val := by + have hdiv : (va &&& ~~~(⟨BitVec.ofNat 64 (2 ^ sh - 1)⟩ : U64)).val = (va.val / 2 ^ sh) * 2 ^ sh := by + rw [UScalar.val_and, hmask] + apply Nat.eq_of_testBit_eq; intro i + rw [Nat.testBit_and] + by_cases hi : i < sh + · have h1 : (2 ^ 64 - 2 ^ sh).testBit i = false := by + have : 2 ^ 64 - 2 ^ sh = (2 ^ (64 - sh) - 1) * 2 ^ sh := by + rw [Nat.sub_mul, one_mul, ← Nat.pow_add, Nat.sub_add_cancel (by omega)] + rw [this, Nat.testBit_mul_two_pow]; simp [Nat.not_le.mpr hi] + have h2 : ((va.val / 2 ^ sh) * 2 ^ sh).testBit i = false := by + rw [Nat.testBit_mul_two_pow]; simp [Nat.not_le.mpr hi] + rw [h1, Bool.and_false, h2] + · have hge_i : sh ≤ i := Nat.le_of_not_lt hi + by_cases hi64 : i < 64 + · have h1 : (2 ^ 64 - 2 ^ sh).testBit i = true := by + have : 2 ^ 64 - 2 ^ sh = (2 ^ (64 - sh) - 1) * 2 ^ sh := by + rw [Nat.sub_mul, one_mul, ← Nat.pow_add, Nat.sub_add_cancel (by omega)] + rw [this, Nat.testBit_mul_two_pow, decide_eq_true hge_i, Bool.true_and, Nat.testBit_two_pow_sub_one] + exact decide_eq_true (by omega) + have h2 : ((va.val / 2 ^ sh) * 2 ^ sh).testBit i = va.val.testBit i := by + rw [Nat.testBit_mul_two_pow, decide_eq_true hge_i, Bool.true_and, + ← Nat.shiftRight_eq_div_pow, Nat.testBit_shiftRight, Nat.add_sub_cancel' hge_i] + rw [h1, Bool.and_true, h2] + · have h1 : va.val.testBit i = false := + Nat.testBit_lt_two_pow (lt_of_lt_of_le hlt (Nat.pow_le_pow_right (by norm_num) (Nat.le_of_not_lt hi64))) + have h2 : ((va.val / 2 ^ sh) * 2 ^ sh).testBit i = false := + Nat.testBit_lt_two_pow (lt_of_le_of_lt (Nat.div_mul_le_self _ _) + (lt_of_lt_of_le hlt (Nat.pow_le_pow_right (by norm_num) (Nat.le_of_not_lt hi64)))) + rw [h1, Bool.false_and, h2] + rw [hdiv] + have hmul : 2 ^ 37 = 2 ^ (37 - sh) * 2 ^ sh := by rw [← Nat.pow_add, Nat.sub_add_cancel (by omega)] + have h2 : 2 ^ 64 - 2 ^ 37 = (2 ^ (64 - sh) - 2 ^ (37 - sh)) * 2 ^ sh := by + rw [Nat.sub_mul, ← Nat.pow_add, Nat.sub_add_cancel (by omega), ← hmul] + have hq : 2 ^ (64 - sh) - 2 ^ (37 - sh) ≤ va.val / 2 ^ sh := by + rw [Nat.le_div_iff_mul_le (Nat.two_pow_pos sh), ← h2]; omega + exact le_trans (by rw [h2]) (Nat.mul_le_mul_right _ hq) + have hb : fl4_abi.consts.HIGH_VSPACE_BASE.val = 18446743936270598144 := by + unfold fl4_abi.consts.HIGH_VSPACE_BASE; rfl + show fl4_abi.consts.HIGH_VSPACE_BASE.val ≤ _ + rw [hb, hbase]; exact hge + +private theorem hi_can_pt (va : U64) (hlo : fl4_abi.consts.HIGH_VSPACE_BASE ≤ va) : CapRt.Canonical va := by + have h1 : 18446743936270598144 ≤ va.val := by + have hb : fl4_abi.consts.HIGH_VSPACE_BASE.val = 18446743936270598144 := by + unfold fl4_abi.consts.HIGH_VSPACE_BASE; rfl + rw [← hb]; exact hlo + have h2 : va.val < 2 ^ 64 := va.bv.isLt + right + intro j hj hj47 + show va.val.testBit j = true + have hpow : 2 ^ 64 - 2 ^ 47 ≤ va.val := by omega + have hsub : va.val / 2 ^ j ≥ 2 ^ (64 - j) - 1 := by + have hle : (2 ^ (64 - j) - 1) * 2 ^ j = 2 ^ 64 - 2 ^ j := by + rw [Nat.sub_mul, Nat.one_mul, ← Nat.pow_add, Nat.sub_add_cancel (by omega)] + have h47 : 2 ^ 47 ≤ 2 ^ j := Nat.pow_le_pow_right (by norm_num) hj47 + have hge : 2 ^ 64 - 2 ^ j ≤ 2 ^ 64 - 2 ^ 47 := by omega + exact (Nat.le_div_iff_mul_le (Nat.two_pow_pos j)).2 (by omega) + have hlt : va.val / 2 ^ j < 2 ^ (64 - j) := by + rw [Nat.div_lt_iff_lt_mul (Nat.two_pow_pos j), ← Nat.pow_add, Nat.sub_add_cancel (by omega)] + exact h2 + have heq : va.val / 2 ^ j = 2 ^ (64 - j) - 1 := by omega + rw [Nat.testBit_eq_decide_div_mod_eq, heq] + have hpos : 1 ≤ 64 - j := by omega + have hodd : (2 ^ (64 - j) - 1) % 2 = 1 := by + rcases Nat.exists_eq_add_of_le' hpos with ⟨k, hk⟩ + rw [hk, Nat.pow_succ] + have : 1 ≤ 2 ^ k := Nat.one_le_two_pow + omega + simp [hodd] + +private theorem mask_eq_ofNat_pt {sh : Nat} (hsh : sh = 39 ∨ sh = 30 ∨ sh = 21) : + Fl4SpecA64.mask sh = BitVec.ofNat 64 (2 ^ sh - 1) := by + rcases hsh with rfl | rfl | rfl <;> decide + +private theorem performExtMap_mem_pt {σ1 σ2 : State Unit} {cap : ArchCap} {cte : CslotPtr} {pte : PTE} + {sl : ExtSlot} + (hset : ((), σ2) ∈ (setCap (Z := Unit) (.archObjectCap cap) cte σ1).1) + (hok : ExtSlotOkX σ2 sl) : + (.ok ([] : List Data), extSlotWriteX σ2 sl pte) ∈ + ((fl4ArchPerformInvocation (Z := Unit) (.extVSpace (.map cap cte pte sl))).run σ1).1 := by + apply mem_archExtA + unfold performExtVSpace + have hst : (storeExtSlot (Z := Unit) sl pte >>= fun _ => pure ([] : List Data)) σ2 = + ({([], extSlotWriteX σ2 sl pte)}, False) := by + rw [nd_bind_oneD (storeExtSlot_runX hok pte)]; rfl + exact ⟨((), σ2), hset, by dsimp only; rw [hst]; rfl⟩ + +private theorem extSlotOk_of_s2Slot_pt {Pp Tb X Y T2 : Set U64} {Sh2 : U64 → Nat} {mem : U64 → U64} {σ : State Unit} + (E : ExtVsRelA Pp Tb X Y T2 Sh2 mem σ) {luSl : U64} {sl : ExtSlot} {sh : Nat} + (hsl : SlotRelE (G2E T2 Sh2) (TopS2E X) luSl sl sh) : ExtSlotOkX σ sl := by + rcases hsl with ⟨t, i, ⟨ht, -⟩, hi, rfl, rfl⟩ | ⟨r0, j0, hr0, hj0, rfl, rfl, -⟩ + · obtain ⟨pt, hk, hty, -⟩ := E.s2pt t ht + cases pt with + | vsRootPT _ => cases hty + | normalPT f => + obtain ⟨hal, hb, -⟩ := slot_geomA .normalPtT (Nat.mod_eq_zero_of_dvd (E.tal t ht)) hi + exact ⟨hal, f, by rw [hb]; exact hk⟩ + · obtain ⟨es, hk, -⟩ := E.s2root r0 hr0 + exact ⟨.stage2Root es, extObjAt_someX.2 hk⟩ + +private theorem extSlotOk_of_hiSlot_pt {Pp Tb X Y : Set U64} {Lv : U64 → Nat} {mem : U64 → U64} {ksb : U64} + {σ : State Unit} (R : VsRelA Pp Tb Lv X Y mem ksb σ) {luSl : U64} {sl : ExtSlot} {sh : Nat} + (hsl : SlotRelE (G1E Tb Lv) (TopHiE Y) luSl sl sh) : ExtSlotOkX σ sl := by + rcases hsl with ⟨t, i, ⟨ht, h1, -⟩, hi, rfl, rfl⟩ | ⟨y, j, hy, hj, rfl, rfl, -⟩ + · obtain ⟨pt, hk, hty, -⟩ := R.pt t ht + have hty' : ptType pt = .normalPtT := by rw [hty]; unfold ptTyA; rw [if_neg (by omega)] + cases pt with + | vsRootPT _ => cases hty' + | normalPT f => + obtain ⟨hal, hb, -⟩ := slot_geomA .normalPtT (R.al_mod ht) hi + exact ⟨hal, f, by rw [hb]; exact hk⟩ + · obtain ⟨l1, hk, -⟩ := R.high y hy + exact ⟨.highVSpace l1, extObjAt_someX.2 hk⟩ + +private theorem extSlotRead_of_s2Slot_pt {Pp Tb X Y T2 : Set U64} {Sh2 : U64 → Nat} {mem : U64 → U64} {σ : State Unit} + (E : ExtVsRelA Pp Tb X Y T2 Sh2 mem σ) {luSl : U64} {sl : ExtSlot} {sh : Nat} + (hsl : SlotRelE (G2ReachE Pp X T2 Sh2 mem σ) (TopS2ReachE Pp X σ) luSl sl sh) : + DescRelE (G2E T2 Sh2) sh (mem luSl) (sl.read σ) := by + rcases hsl with ⟨t, i, ⟨⟨ht, rfl⟩, hreach⟩, hi, rfl, rfl⟩ | ⟨r0, j0, hr0, hact, hj0, rfl, rfl, rfl⟩ + · obtain ⟨pt, hk, hty, he⟩ := E.s2pt t ht + rw [readPtE hk hty (Nat.mod_eq_zero_of_dvd (E.tal t ht)) hi] + exact (he hreach i hi).1 + · obtain ⟨es, hk, he⟩ := E.s2root r0 hr0 + have hj1024 : j0.val < 1024 := by have := s2GeomNumE.2.2.1; omega + have hread : ExtSlot.read σ (.root (specPtr r0) j0.val) = es ⟨j0.val, hj1024⟩ := by + rw [readRootE hk] + show (if h : j0.val < 1024 then es ⟨j0.val, h⟩ else .invalidPTE) = _ + rw [dif_pos hj1024] + rw [hread] + exact (he hact j0 hj1024 hj0).1 + +private theorem extSlotRead_of_hiSlot_pt {Pp Tb X Y : Set U64} {Lv : U64 → Nat} {mem : U64 → U64} {ksb : U64} + {σ : State Unit} (R : VsRelA Pp Tb Lv X Y mem ksb σ) {luSl : U64} {sl : ExtSlot} {sh : Nat} + (hsl : SlotRelE (G1ReachE Pp Tb Y Lv mem ksb) (TopHiReachE Pp Y mem ksb) luSl sl sh) : + DescRelE (G1E Tb Lv) sh (mem luSl) (sl.read σ) := by + rcases hsl with ⟨t, i, ⟨⟨ht, h1, rfl⟩, hreach⟩, hi, rfl, rfl⟩ | ⟨y, j, hy, hact, hj, rfl, rfl, rfl⟩ + · obtain ⟨pt, hk, hty, he⟩ := R.pt t ht + have hty' : ptType pt = .normalPtT := by rw [hty]; unfold ptTyA; rw [if_neg (by omega)] + rw [readPtE hk hty' (R.al_mod ht) hi] + exact descE_of_pteRel h1 (R.lv t ht) (he hreach i hi) + · obtain ⟨l1, hk, he⟩ := R.high y hy + have hread : ExtSlot.read σ (.root (specPtr y) j.val) = l1 ⟨j.val, hj⟩ := by + rw [readRootE hk] + show (if h : j.val < 512 then l1 ⟨j.val, h⟩ else .invalidPTE) = _ + rw [dif_pos hj] + rw [hread] + exact descE_of_pteRel (k := 1) (by omega) (by omega) (he hact j hj) + +private theorem run_liftME_congr_pt {α β : Type} {f : α → β} {m1 m2 : SEMonad Unit α} {σ : State Unit} + (h : m1.run σ = m2.run σ) : (NonDetE.liftME f m1).run σ = (NonDetE.liftME f m2).run σ := by + unfold NonDetE.liftME + change (NonDet.bind (m1.run) _ σ) = (NonDet.bind (m2.run) _ σ) + simp only [NonDet.bind, h] + +private theorem liftE_gets_pt {α : Type} (f : State Unit → α) (σ : State Unit) : + (NonDetE.liftE (ε := SyscallError) (NonDet.gets f)).run σ = ({(Except.ok (f σ), σ)}, False) := by + unfold NonDetE.liftE ExceptT.lift NonDet.gets + simp [Functor.map, NonDet.bind, NonDet.pure] + +private theorem unm_not_pp_pt {Pp Tb X Y : Set U64} {Lv : U64 → Nat} {mem : U64 → U64} {ksb : U64} + {σ : State Unit} {ptr : U64} (R : VsRelA Pp Tb Lv X Y mem ksb σ) + {pt : PT} (hk : σ.kheap (specPtr ptr) = some (.archObj (.pageTable pt))) : ptr ∉ Pp := by + intro hpp + obtain ⟨f, hf, -⟩ := R.pool ptr hpp + rw [hk] at hf; cases hf + +private theorem unm_mem_keep_pt {S : Type} {st : machine.Store S} [L : LawfulStore st] + {Z : XSetsA} {H : Heap} {OS : Set U64} {s s2 : S} {ptr : U64} + (hcurT : L.curW s ∈ H.tcbs) (hfitT : ∀ t ∈ H.tcbs, t.val + 4096 ≤ 2 ^ 64) + (sA : Aligned (stA (L.curW s))) + (UP : UnmPtA Z.S1 Z.Y OS H.tcbs (L.mem s) σ ptr) + (hw2 : WritesIn st (L.wr s (stA (L.curW s)) sched.TS_RESTART) s2 (W7.CapW H.tcbs {b | InOS OS b} ∅)) : + ∀ i : U64, i.val < 512 → L.mem s2 (peAF ptr i) = L.mem s (peAF ptr i) := by + intro i hi + have hpal : ptr.val % 4096 = 0 := Nat.mod_eq_zero_of_dvd UP.al + have ha := peAF_alignedA hpal hi + have hT := UP.tcb i hi + have hnc : ¬ W7.CapW H.tcbs {b | InOS OS b} ∅ (peAF ptr i) := by + rintro (⟨t, ht, K, hK, he⟩ | ⟨hin, -⟩) + · have hfit := hfitT t ht + have := congrArg UScalar.val he + rw [wa_valm] at this + rcases hT t ht with h | h <;> omega + · exact UP.os i hi hin + have hns : stA (L.curW s) ≠ peAF ptr i := fun e => by + have := congrArg UScalar.val e + have e3 : (3328#u64 : U64).val = 3328 := rfl + have hfit := hfitT _ hcurT + rw [stA_eq, wa_valm, e3] at this + rcases hT _ hcurT with h | h <;> omega + rw [hw2.2.2 _ ha hnc, mem_wr_ne' sA ha hns] + +end helpers + +/-! ## Stage-2 `ARMPageTableMap` (`ept.map_table` vs `decodeS2PageTableMap`) -/ + +section s2PtMap +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {s : S} {a : A} {σ : State Unit} {m : McState} + +set_option maxHeartbeats 4000000 in +/-- **`ept.map_table` against `decodeS2PageTableMap`** (after `ept.root` succeeds with `(root, asid1)`). -/ +theorem s2_map_pt_jA {ar : machine.Arch A} + (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) + (hTO : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s q) (hEO : ExtTabOffA (L := L) Z m s) + (hK : Aligned (L.ksb s)) (hT : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s)) (hIpa : S2IpaE ar a) + (hTr : TreeA st OS Z.G.chains Z.G.par s) (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) + (cte : CslotPtr) (hab : AbsSlotA Z.th cte) (hos : cslotAddrA cte ∈ OS) + (ptr asid0 va0 : U64) (hcap : capAtA (L.mem s) (cslotAddrA cte) = .PageTable ptr false asid0 va0) + (UP : UnmPtA Z.S1 Z.Y OS H.tcbs (L.mem s) σ ptr) (hS2 : ptr ∉ S2PgE Z.X Z.T2) (hY : ptr ∉ Z.Y) + (root asid1 ipa : U64) (hrX : root ∈ Z.X) (hactR : ActiveS2RootA Z.Pp Z.X σ root) + (ha1 : asid1 ≠ 0#u64) (ha16 : asid1.val < 2 ^ 16) + {D : Set (Except SyscallError Fl4ArchInvocation × State Unit) × Prop} + (hD : D = (NonDetE.liftME Fl4ArchInvocation.extVSpace (do + NonDetE.whenE (ipa.bv > s2IpaLimit) (NonDetE.throwError (.invalidArgument 0)) + let pos ← NonDetE.liftE (NonDet.gets fun s : State Unit => extLookupSlot s s2Geom (specPtr root) ipa.bv) + let old ← NonDetE.liftE (NonDet.gets fun s : State Unit => pos.slot.read s) + NonDetE.whenE (pos.shift = pageBits ∨ old ≠ .invalidPTE) (NonDetE.throwError .deleteFirst) + NonDetE.returnOk (.map (.pageTableCap (specPtr ptr) .normalPtT (some (asidOfA asid1, ipa.bv &&& ~~~(mask pos.shift)))) + cte (.pageTablePTE (ppnFromPptr (specPtr ptr))) pos.slot))).run σ) : + ept.map_table st ar s a root asid1 ipa ptr (cslotAddrA cte) ⦃ res => + VsTopPostA (L := L) AR Z OS m σ D s a res ⦄ := by + obtain ⟨nIpa, hnIpa, hnvIpa⟩ := hIpa + unfold ept.map_table + rw [hnIpa, bind_ok, s2LimitEq_pt hnvIpa, bind_ok] + -- Range check: ipa > s2IpaLimit + by_cases hgt : ipa > (⟨s2IpaLimit⟩ : U64) + · rw [if_pos hgt] + simp only [error.invalid_arg, bind_ok] + refine (WP.spec_ok _).2 ⟨rfl, rfl, .invalidArgument 0, ?_, Or.inl rfl⟩ + rw [hD] + have hgt' : ipa.bv > s2IpaLimit := hgt + exact run_liftME_errA (run_bind_errorA (by simp only [NonDetE.whenE, if_pos hgt']; rfl) _) + rw [if_neg hgt] + have hgt' : ¬ (ipa.bv > s2IpaLimit) := hgt + have hipa48 : ipa.val < 2 ^ 48 := by + have hle : ipa.val ≤ s2IpaLimit.toNat := by + have : ¬ (s2IpaLimit.toNat < ipa.val) := hgt + omega + exact lt_of_le_of_lt hle s2IpaLimit_lt48_pt + -- Lookup slot + let pos := extLookupSlot σ s2Geom (specPtr root) ipa.bv + obtain ⟨lu, hlu, hlsh, hslR⟩ := spec_ex_fd (ept_lookup_slot_mapA E.j.1.ext hrX hactR hnvIpa ipa) + have hsl : SlotRelE (G2E Z.T2 Z.Sh2) (TopS2E Z.X) lu.slot pos.slot pos.shift := + hslR.mono (fun _ _ g => g.1) (fun _ _ _ ht => ht.toTopS2E) + rw [hlu, bind_ok] + have hD2 : D = (NonDetE.liftME Fl4ArchInvocation.extVSpace (do + NonDetE.whenE (pos.shift = pageBits ∨ pos.slot.read σ ≠ .invalidPTE) (NonDetE.throwError .deleteFirst) + NonDetE.returnOk (.map (.pageTableCap (specPtr ptr) .normalPtT (some (asidOfA asid1, ipa.bv &&& ~~~(mask pos.shift)))) + cte (.pageTablePTE (ppnFromPptr (specPtr ptr))) pos.slot))).run σ := by + rw [hD] + apply run_liftME_congr_pt + rw [run_bind_okA (by simp only [NonDetE.whenE, if_neg hgt']; rfl)] + rw [run_bind_okA (liftE_gets_pt (fun s : State Unit => extLookupSlot s s2Geom (specPtr root) ipa.bv) σ)] + rw [run_bind_okA (liftE_gets_pt (fun s : State Unit => pos.slot.read s) σ)] + -- Leaf level check: lu.shift = PAGE_BITS (12) + have hpb : ept.PAGE_BITS = 12#u64 := by unfold ept.PAGE_BITS; rfl + by_cases h12 : lu.shift = ept.PAGE_BITS + · rw [if_pos h12] + have hp12 : pos.shift = pageBits := by + unfold pageBits; rw [← hlsh, h12, hpb]; rfl + refine (WP.spec_ok _).2 ⟨rfl, rfl, .deleteFirst, ?_, Or.inl trivial⟩ + rw [hD2] + exact run_liftME_errA (run_bind_errorA (by simp only [NonDetE.whenE, if_pos (Or.inl hp12)]; rfl) _) + rw [if_neg h12] + have hp12 : pos.shift ≠ pageBits := by + intro h; apply h12; apply UScalar.eq_of_val_eq + rw [hlsh, h, hpb]; rfl + -- Occupied slot check: (mem lu.slot &&& D_VALID) != 0 + rw [L.read_eq, bind_ok, eq_of_spec_fd P0.Consts.ept.D_VALID.spec, bind_ok] + simp only [lift, bind_ok, valid_bitA] + have hDescRead := extSlotRead_of_s2Slot_pt E.j.1.ext hslR + by_cases hv : dBitA (L.mem s lu.slot) 0 = true + · rw [if_pos hv] + have hneInv : pos.slot.read σ ≠ .invalidPTE := fun h => by + rw [(descRelE_invA hDescRead).1 h] at hv; cases hv + refine (WP.spec_ok _).2 ⟨rfl, rfl, .deleteFirst, ?_, Or.inl trivial⟩ + rw [hD2] + exact run_liftME_errA (run_bind_errorA (by simp only [NonDetE.whenE, if_pos (Or.inr hneInv)]; rfl) _) + have hvf : dBitA (L.mem s lu.slot) 0 = false := by simpa using hv + rw [if_neg hv] + have hInvEq : pos.slot.read σ = .invalidPTE := (descRelE_invA hDescRead).2 hvf + -- Shift bounds from s2Lookup_shift_pt: pos.shift ∈ {39, 30, 21} + have hshCases : pos.shift = 39 ∨ pos.shift = 30 ∨ pos.shift = 21 := by + rcases s2Lookup_shift_pt σ (specPtr root) ipa.bv with h | h | h | h + · exact Or.inl h + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr h) + · exact (hp12 h).elim + have hlsh' : lu.shift.val = pos.shift := hlsh + have hs64 : lu.shift.val < 64 := by rcases hshCases with h | h | h <;> omega + have e63 : (lu.shift &&& 63#u64) = lu.shift := + UScalar.eq_of_val_eq (by + rw [and_maskA lu.shift 6 _ rfl]; exact Nat.mod_eq_of_lt (by simp only [Nat.reducePow]; omega)) + rw [e63, CapRt.shl_u64_eq 1#u64 lu.shift hs64, bind_ok] + try simp only [lift, bind_ok] + rw [ws1A lu.shift hs64] + generalize hbase : ipa &&& ~~~(⟨BitVec.ofNat 64 (2 ^ lu.shift.val - 1)⟩ : U64) = base + have hbv : base.bv = ipa.bv &&& ~~~(Fl4SpecA64.mask pos.shift) := by + rw [← hbase, ← hlsh, mask_eq_ofNat_pt (by omega)]; rfl + have hbase48 : base.val < 2 ^ 48 := by + rw [← hbase, UScalar.val_and]; exact lt_of_le_of_lt Nat.and_le_left hipa48 + -- Restart + set_cap via set_cap_pt_unpack48A + have hcanon : CapRt.Canonical ptr := by + have h : CapRt.EncWf (capAtA (L.mem s) (cslotAddrA cte)) := CapRt.decodeF_encWf _ _ + rw [hcap] at h; exact h.1 + let baseU : U64 := ⟨unpack48A base.bv⟩ + have hEnc : CapRt.EncWf (.PageTable ptr true asid1 baseU) := + ⟨hcanon, by omega, unpack48A_canonical hbase48⟩ + have hca : CapRelA (.PageTable ptr true asid1 baseU) + (.archObjectCap (.pageTableCap (specPtr ptr) .normalPtT (some (asidOfA asid1, base.bv)))) := + ⟨ha16, rfl, rfl, Or.inr ⟨rfl, hbase48⟩⟩ + obtain ⟨x, s2, σ2, hkx, hst, hset, hsc, hσ2, HS, hksb2, R2, hcpu2, hw2, hJ2, hFr2⟩ := + restart_setcapJA E RS cte hab hos hEnc hca + rw [cur_thread_ok, bind_ok, hst, bind_ok, set_cap_pt_unpack48A _ _ _ _ _ _ hbase48, hsc, bind_ok] + -- make_s2_table and write + rw [show layout.paddr ptr = ok (core.num.U64.wrapping_sub ptr layout.PPTR_BASE) from rfl, bind_ok] + have hpaMod : (core.num.U64.wrapping_sub ptr layout.PPTR_BASE).val % 4096 = 0 := by + rw [ws_valA]; have := Nat.mod_eq_zero_of_dvd UP.al; omega + have hpa48 : (core.num.U64.wrapping_sub ptr layout.PPTR_BASE).val < 2 ^ 48 := by + have := UP.pa; omega + refine WP.spec_bind (s2tab_v _ hpaMod hpa48) fun ew ⟨hv3, hvm⟩ => ?_ + rw [L.write_eq, bind_ok] + -- Build JAtZAX at Z' = { Z with T2 := insert ptr Z.T2, Sh2 := Function.update Z.Sh2 ptr (pos.shift - 9) } + let T2' : Set U64 := insert ptr Z.T2 + let Sh2' : U64 → Nat := Function.update Z.Sh2 ptr (pos.shift - 9) + let Z' : XSetsA := { Z with T2 := T2', Sh2 := Sh2' } + let pte : PTE := .pageTablePTE (ppnFromPptr (specPtr ptr)) + have hco : capA st s (cslotAddrA cte) = .PageTable ptr false asid0 va0 := by rw [← capAtA_eq, hcap] + obtain ⟨J2, hT2⟩ := hJ2 hTr hnul (by simp) (by rw [hco]; simp) + (UA.of_shapeA (by rw [hco]; rfl) fun h => by rw [hco] at h; simp [isUtA] at h) + ⟨fun _ h => (by cases h), fun _ _ _ _ _ h => (by cases h), fun _ _ _ h => (by cases h), + fun _ _ _ h => (by cases h), trivial⟩ + have hmem2 := unm_mem_keep_pt E.sq.curT (fun t ht => by have := (E.sq.tcbs t ht).fits; simp only [Nat.reducePow] at this; exact this) + (by rw [stA_eq]; exact field_aligned (E.sq.tcbs _ E.sq.curT) (by simp) (by simp)) UP hw2 + obtain ⟨tp, hkp, htp, hep⟩ := UP.spec + have hkp1 : (stUpdA σ σ.curThread x .restart).kheap (specPtr ptr) = some (.archObj (.pageTable tp)) := + stUpd_keep hkx hkp (fun _ h => by cases h) + have hkp2 := HS.keep hkp1 (fun _ _ h => by cases h) (fun _ h => by cases h) + have hnp : ptr ∉ Z.Pp := unm_not_pp_pt J2.1.vs hkp2 + have hE2Add : ExtVsRelA Z.Pp Z.S1 Z.X Z.Y T2' Sh2' (L.mem s2) σ2 := + J2.1.ext.addS2PtE hnp UP.nt hS2 hY UP.al UP.ytb (by omega) + (fun i hi => by rw [hmem2 i hi]; exact UP.inv i hi) hkp2 htp hep + have hAlLu : Aligned lu.slot := by + rcases hsl with ⟨t, i, ⟨ht, -⟩, hi, hluEq, -⟩ | ⟨r0, j0, hr0, hj0, hluEq, -, -⟩ + · rw [hluEq]; exact peAF_alignedA (Nat.mod_eq_zero_of_dvd (E.j.1.ext.tal t ht)) hi + · rw [hluEq]; exact E.j.1.ext.foot_alignedE E.j.1.vs.yal _ + (rootFootE hr0 (E.j.1.ext.xal r0 hr0) (by have := s2GeomNumE.2.2.1; omega)) + have hW := extWordWriteA (st := st) (s := s2) (slot := lu.slot) (word := ew) hAlLu + have hDescNew := descRelE_s2tabA (G := G2E T2' Sh2') (sh := pos.shift) (by omega) hv3 hvm UP.pa + ⟨Set.mem_insert _ _, Function.update_self _ _ _⟩ + have hExtOk : ExtSlotOkX σ2 pos.slot := extSlotOk_of_s2Slot_pt J2.1.ext hsl + have hPerf := performExtMap_mem_pt (pte := pte) hσ2 hExtOk + have hInv : D = ({(.ok (.extVSpace (.map (.pageTableCap (specPtr ptr) .normalPtT (some (asidOfA asid1, base.bv))) + cte pte pos.slot)), σ)}, False) := by + rw [hD2] + apply run_liftME_okA + have hokW : (NonDetE.whenE (pos.shift = pageBits ∨ pos.slot.read σ ≠ .invalidPTE) + (NonDetE.throwError SyscallError.deleteFirst)).run σ = ({(Except.ok (), σ)}, False) := by + unfold NonDetE.whenE + rw [if_neg (by rintro (h | h); exact hp12 h; exact h hInvEq)]; rfl + rw [run_bind_okA hokW, hbv] + rfl + have hfa2 : ∀ w, FootA (L := L) Z s2 w → Aligned w := + FootA.aligned J2.1 (fun c hc o h1 h2 => by rw [nodeAt_ksb hksb2]; exact bxi_nodes E.sys.ent.bxi c hc o h1 h2) + E.scAl E.ap.vcAl + have hEO2 : ExtTabOffA (L := L) Z m s2 := hEO.of_ksA hksb2 hcpu2 + have hTO2 : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s2 q := fun q hq => (hTO q hq).of_ksA hksb2 hcpu2 + have hK2 : Aligned (L.ksb s2) := hksb2 ▸ hK + have hT2' : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s2) := hksb2 ▸ hT + have hdTab : dTabA ew = ptr := by + unfold dTabA dAddrA + have hmask : (281474976706560#u64 : U64) = ept.ADDR_MASK := by unfold ept.ADDR_MASK; rfl + rw [hmask, hvm, show (18446603336221196288#u64 : U64) = layout.PPTR_BASE by unfold layout.PPTR_BASE; rfl] + exact wa_ws ptr layout.PPTR_BASE + have hchEmpty : ∀ j : U64, j.val < 512 → dBitA (L.mem s2 (peAF (dTabA ew) j)) 0 = false ∧ + ∀ pt1, σ2.kheap (specPtr (dTabA ew)) = some (.archObj (.pageTable pt1)) → + Fl4SpecA64.ptApply pt1 j.bv = .invalidPTE := by + rw [hdTab] + intro j hj + refine ⟨by rw [hmem2 j hj]; exact UP.inv j hj, fun pt1 hk1 => ?_⟩ + rw [hkp2] at hk1 + cases hk1 + exact hep j hj + have hJ3 : JAtZAX (L := L) AR Z' (L.wr s2 lu.slot ew) a (extSlotWriteX σ2 pos.slot pte) m := by + have hER3 : ExtVsRelA Z.Pp Z.S1 Z.X Z.Y T2' Sh2' (L.mem (L.wr s2 lu.slot ew)) (extSlotWriteX σ2 pos.slot pte) := by + rcases hsl with ⟨t, i, ⟨ht, hst⟩, hi, hluEq, hslEq⟩ | ⟨r0, j0, hr0, hj0, hluEq, hslEq, hshEq⟩ + · rw [hluEq] at hW ⊢; rw [hslEq] + have hne : t ≠ ptr := fun e => hS2 (Or.inl (e ▸ ht)) + have hst' : Sh2' t = pos.shift := by show Function.update Z.Sh2 ptr (pos.shift - 9) t = pos.shift; rw [Function.update_of_ne hne]; exact hst + exact hE2Add.of_ptWriteMapE J2.1.vs.yal (Set.mem_insert_of_mem _ ht) hi (by rw [hst']; exact hDescNew) hW.2.2 + (fun _ _ => ⟨by rw [hdTab]; exact hne.symm, hchEmpty⟩) + · rw [hluEq] at hW ⊢; rw [hslEq] + have hj1024 : j0.val < 1024 := by have := s2GeomNumE.2.2.1; omega + exact hE2Add.of_rootWriteMapE J2.1.vs.yal hr0 hj1024 (by rw [← hshEq]; exact hDescNew) hW.2.2 + (fun _ _ => hchEmpty) + have hV3 : VsRelA Z.Pp Z.S1 Z.Lv Z.X Z.Y (L.mem (L.wr s2 lu.slot ew)) (L.ksb (L.wr s2 lu.slot ew)) + (extSlotWriteX σ2 pos.slot pte) := by + rcases hsl with ⟨t, i, ⟨ht, -⟩, hi, hluEq, hslEq⟩ | ⟨r0, j0, hr0, hj0, hluEq, hslEq, -⟩ + · rw [hluEq] at hW ⊢; rw [hslEq, hW.1] + exact J2.1.vs.of_s2WriteE (mem' := L.mem (L.wr s2 (peAF t i) ew)) J2.1.ext (Or.inl ht) + (J2.1.ext.tal t ht) hi (hT2' t i hi (Or.inl (Or.inl ht))) hK2 + (fun b hb hne => by rw [hW.2.2 b hb, if_neg (Ne.symm hne)]) + (Or.inl ht) (slotObjPtE (Nat.mod_eq_zero_of_dvd (J2.1.ext.tal t ht)) hi) pte + · rw [hluEq] at hW ⊢; rw [hslEq, hW.1] + have hj1024 : j0.val < 1024 := by have := s2GeomNumE.2.2.1; omega + obtain ⟨q, j, hq, hj, hpe⟩ := rootWordE (J2.1.ext.xal r0 hr0) hj1024 + have hqal : 4096 ∣ q.val := hq.elim (fun e => e ▸ al4kE (J2.1.ext.xal r0 hr0)) + (fun e => e ▸ al4kHiE (J2.1.ext.xal r0 hr0)) + exact J2.1.vs.of_s2WriteE (mem' := L.mem (L.wr s2 (peAF r0 j0) ew)) J2.1.ext + (Or.inr ⟨r0, hr0, hq⟩) hqal hj (hT2' q j hj (Or.inl (Or.inr ⟨r0, hr0, hq⟩))) hK2 + (fun b hb hne => by rw [hW.2.2 b hb, if_neg (by rw [hpe]; exact Ne.symm hne)]) + (Or.inr ⟨r0, hr0, Or.inl rfl⟩) (sl := .root (specPtr r0) j0.val) rfl pte + -- Build JAtZAX at Z' using J2.of_sameXA-style proof (only J.1.ext changes to Z'.T2 / Z'.Sh2) + have hSame : SameA (L := L) (extZA Z) s2 (L.wr s2 lu.slot ew) := by + refine ⟨hW.1, hW.2.1, fun w hw => ?_⟩ + have hne : lu.slot ≠ w := by + intro heq; subst heq + rcases hsl with ⟨t, i, ⟨ht, -⟩, hi, hluEq, -⟩ | ⟨r0, j0, hr0, hj0, hluEq, -, -⟩ + · rw [hluEq] at hw; exact hEO2.mem t (Or.inl (Or.inl ht)) i hi hw + · have hj1024 : j0.val < 1024 := by have := s2GeomNumE.2.2.1; omega + obtain ⟨q, j, hq, hj, hpe⟩ := rootWordE (J2.1.ext.xal r0 hr0) hj1024 + rw [hluEq, hpe] at hw; exact hEO2.mem q (Or.inl (Or.inr ⟨r0, hr0, hq⟩)) j hj hw + rw [hW.2.2 w (hfa2 w (FootA.s1_monoA hw.extMonoA)), if_neg hne] + have hkh : ∀ p, RelPA (extZA Z) m p → (extSlotWriteX σ2 pos.slot pte).kheap p = σ2.kheap p := by + intro p hp + rcases hsl with ⟨t, i, ⟨ht, -⟩, hi, -, hslEq⟩ | ⟨r0, j0, hr0, hj0, -, hslEq, -⟩ + · rw [hslEq]; exact kheap_writeX (by + rw [slotObjPtE (Nat.mod_eq_zero_of_dvd (J2.1.ext.tal t ht)) hi] + intro e; subst e; exact hEO2.obj t (Or.inr (Or.inr ht)) hp) + · rw [hslEq]; exact kheap_writeX (by + intro e; subst e; exact hEO2.obj r0 (Or.inl hr0) hp) + have hobj : ∀ ko, (extSlotWriteX σ2 pos.slot pte).kheap (extSlotObjX pos.slot) = some ko → ExtObjA ko → + (∃ es, ko = .archObj (.ext (.stage2Root es)) ∧ ∃ r ∈ Z.X, specPtr r = extSlotObjX pos.slot) := by + intro ko e hx + rcases hsl with ⟨t, i, ⟨ht, -⟩, hi, -, hslEq⟩ | ⟨r0, j0, hr0, hj0, -, hslEq, -⟩ + · rw [hslEq] at e ⊢ + have hso : extSlotObjX (ExtSlot.pt (specPtr (peAF t i))) = specPtr t := + slotObjPtE (Nat.mod_eq_zero_of_dvd (J2.1.ext.tal t ht)) hi + rw [hso] at e + obtain ⟨pt', hk', -, -⟩ := hER3.s2pt t (Set.mem_insert_of_mem _ ht) + rw [hslEq] at hk' + obtain rfl := hk'.symm.trans e |> Option.some.inj + rcases hx with ⟨_, h'⟩ | ⟨_, h'⟩ | ⟨_, h'⟩ <;> cases h' + · rw [hslEq] at e ⊢ + obtain ⟨es, hk', -⟩ := hER3.s2root r0 hr0 + rw [hslEq] at hk' + obtain rfl := hk'.symm.trans e |> Option.some.inj + exact ⟨es, rfl, r0, hr0, rfl⟩ + have hv' : ∀ p x, (extSlotWriteX σ2 pos.slot pte).kheap p = some (.archObj (.vcpu x)) → + σ2.kheap p = some (.archObj (.vcpu x)) := fun p x e => by + by_cases hp : p = extSlotObjX pos.slot + · subst hp; obtain ⟨_, h, -⟩ := hobj _ e (Or.inl ⟨x, rfl⟩); cases h + · rwa [kheap_writeX hp] at e + have hx' : ∀ p es, (extSlotWriteX σ2 pos.slot pte).kheap p = some (.archObj (.ext (.stage2Root es))) → + ∃ r ∈ Z.X, specPtr r = p := fun p es e => by + by_cases hp : p = extSlotObjX pos.slot + · subst hp; obtain ⟨_, -, r, hr, hreq⟩ := hobj _ e (Or.inr (Or.inl ⟨es, rfl⟩)); exact ⟨r, hr, hreq⟩ + · exact J2.2.2.1.back p es (by rwa [kheap_writeX hp] at e) + have hy' : ∀ p l1, (extSlotWriteX σ2 pos.slot pte).kheap p = some (.archObj (.ext (.highVSpace l1))) → + ∃ r ∈ Z.Y, specPtr r = p := fun p l1 e => by + by_cases hp : p = extSlotObjX pos.slot + · subst hp; obtain ⟨_, h, -⟩ := hobj _ e (Or.inr (Or.inr ⟨l1, rfl⟩)); cases h + · exact J2.2.2.2.back p l1 (by rwa [kheap_writeX hp] at e) + -- Construct JAtZAX at Z' directly from J2's components + obtain ⟨J, V, X0, Y0⟩ := J2 + have HG : SpecGlobA σ2 (extSlotWriteX σ2 pos.slot pte) := (by constructor <;> first | rfl | exact ⟨rfl, rfl, rfl, rfl⟩) + have hT' : TcbKeepA Z.T σ2 (extSlotWriteX σ2 pos.slot pte) := + TcbKeepA.of_eq fun t h => hkh _ (Or.inr (Or.inl ⟨t, h, rfl⟩)) + have hC' : ∀ c ∈ Z.C, (extSlotWriteX σ2 pos.slot pte).kheap (scRenA m c) = σ2.kheap (scRenA m c) := + fun c h => hkh _ (Or.inr (Or.inr (Or.inl ⟨c, h, rfl⟩))) + have hV' : ∀ v ∈ Z.V, (extSlotWriteX σ2 pos.slot pte).kheap (specPtr v) = σ2.kheap (specPtr v) := + fun v h => hkh _ (Or.inr (Or.inr (Or.inr (Or.inl ⟨v, h, rfl⟩)))) + have hcn' : ∀ p r, Z.th p = some (.cnode r) → + (extSlotWriteX σ2 pos.slot pte).kheap (specPtr p) = σ2.kheap (specPtr p) := + fun p _ h => hkh _ (Or.inl ⟨p, by rw [h]; exact Option.some_ne_none _, rfl⟩) + have htc' : ∀ p, Z.th p = some .tcb → + (extSlotWriteX σ2 pos.slot pte).kheap (specPtr p) = σ2.kheap (specPtr p) := + fun p h => hkh _ (Or.inl ⟨p, by rw [h]; exact Option.some_ne_none _, rfl⟩) + have R0 : SchedRestA (L := L) (extZA Z) s2 σ2 m := + ⟨J.rest.rq, J.rest.rqDom, J.rest.rel, J.rest.consumed, J.rest.repr, J.rest.curDom, J.rest.domTime, + J.rest.prio, J.rest.scMem, J.rest.refillMax⟩ + have hR := (R0.of_mem hSame.toN).of_spec hT' hC' HG + have S0 := StateRelA.of_mem (Z := extZA Z) J.st hSame + have C0 := CdtRelA.of_mem (Z := extZA Z) J.cdt hSame + refine ⟨{ st := ⟨fun p r hp => by rw [hcn' p r hp]; exact S0.cnode p r hp, + fun t' h => by rw [htc' t' h]; exact S0.tcb t' h⟩ + cdt := ⟨C0.tree, C0.cdt, C0.cdt_slot, C0.orig, C0.par_slot, C0.prev⟩ + sch := (SchedRelA.of_mem (Z := extZA Z) J.sch hSame.toN).of_spec hT' hC' HG + ar := J.ar + place := by rw [hSame.ksb]; exact J.place + irq := (IrqRelA.of_mem (Z := extZA Z) J.irq hSame.toN).of_spec HG + rest := ⟨hR.rq, hR.rqDom, hR.rel, hR.consumed, hR.repr, hR.curDom, hR.domTime, hR.prio, + hR.scMem, hR.refillMax⟩ + thT := J.thT, csN := J.csN + idleT := fun c hc => by rw [(hSame.toN.node hc).2.1]; exact J.idleT c hc + idleC := fun c hc => by rw [(hSame.toN.node hc).2.2.2]; exact J.idleC c hc + curT := fun c hc => by rw [(hSame.toN.node hc).1]; exact J.curT c hc + cscC := fun c hc => by rw [(hSame.toN.node hc).2.2.1]; exact J.cscC c hc + ksFit := by rw [hSame.ksb]; exact J.ksFit + scOffD := J.scOffD + cv := fun y hy => by rw [hSame.cap (Or.inl hy)]; exact J.cv y hy + pml := fun y hy q mp asid e => by rw [hSame.cap (Or.inl hy)] at e; exact J.pml y hy q mp asid e + vs := hV3 + ext := hER3 }, ?_, ?_, ?_⟩ + · refine ⟨fun v hv'' => ?_, fun q x e => V.back q x (hv' q x e), fun t h => ?_, + fun y hy q e => V.cap y hy q (by rw [← hSame.cap (Or.inl hy)]; exact e)⟩ + · obtain ⟨x, e, h2⟩ := V.obj v hv'' + rw [(hSame.toN.vcW hv'').1, (hSame.toN.vcW hv'').2, hV' v hv''] + exact ⟨x, e, h2⟩ + · obtain ⟨x, e, h1, h2⟩ := V.tcb t h + rw [(hSame.toN.tcbW h).2.2.2.1, (hSame.toN.tcbW h).2.2.2.2, hkh _ (Or.inr (Or.inl ⟨t, h, rfl⟩))] + exact ⟨x, e, h1, h2⟩ + · refine ⟨fun r hr => ?_, hx', fun y hy q l mp asid va e => X0.cap y hy q l mp asid va + (by rw [← hSame.cap (Or.inl hy)]; exact e)⟩ + obtain ⟨es, e, -⟩ := hER3.s2root r hr + exact ⟨es, e⟩ + · refine ⟨fun r hr => ?_, hy', fun y hy q mp asid e => Y0.cap y hy q mp asid + (by rw [← hSame.cap (Or.inl hy)]; exact e)⟩ + obtain ⟨l1, e, -⟩ := hV3.high r hr + exact ⟨l1, e⟩ + have hoffOS : ¬ InOS OS lu.slot := by + have hft : ExtFootE Z.X Z.Y Z.T2 lu.slot := by + rcases hsl with ⟨t, i, ⟨ht, -⟩, hi, hluEq, -⟩ | ⟨r0, j0, hr0, hj0, hluEq, -, -⟩ + · rw [hluEq]; exact .s2pt t i ht hi + · rw [hluEq]; exact rootFootE hr0 (E.j.1.ext.xal r0 hr0) (by have := s2GeomNumE.2.2.1; omega) + exact (E.ap.ext lu.slot hft).1 + have hOSK : OSKeep st OS s2 (L.wr s2 lu.slot ew) := + osKeep_tabWriteA E.sys.ent.os hoffOS (fun w hw hne => by rw [hW.2.2 w hw, if_neg (Ne.symm hne)]) + have hTree3 : TreeA st OS Z.G.chains Z.G.par (L.wr s2 lu.slot ew) := hT2.keepO hOSK + have hCarry3 : CarryA Z (L.mem s) σ → CarryA Z' (L.mem (L.wr s2 lu.slot ew)) (extSlotWriteX σ2 pos.slot pte) := by + have hTy : TyKeepA σ (extSlotWriteX σ2 pos.slot pte) := + TyKeepA.trans (setThreadState_tyA σ.curThread .restart σ _ (by rw [hset]; rfl)) + (TyKeepA.trans (setCap_tyA _ cte _ _ hσ2) + (storeExtSlot_tyA pos.slot pte _ _ (by rw [storeExtSlot_runX hExtOk pte]; rfl))) + have hFr3 : SlotFrA (L := L) OS (cslotAddrA cte) (.PageTable ptr true asid1 baseU) s (L.wr s2 lu.slot ew) := + hFr2.keepR hos hOSK + exact fun C => C.of_slotFrA RS.sub hTr hFr3 rfl rfl (fun _ h => h) (fun _ h => Set.mem_insert_of_mem _ h) + (fun p a v e => by cases e; exact Or.inr (Set.mem_insert _ _)) + (by rw [hcap]; exact CapFinEqA.pt2A _ _ _ _ _ _ _) hTy + exact (WP.spec_ok _).2 ⟨.extVSpace (.map (.pageTableCap (specPtr ptr) .normalPtT (some (asidOfA asid1, base.bv))) + cte pte pos.slot), by rw [hInv]; exact ⟨rfl, not_false⟩, + stUpdA σ σ.curThread x .restart, hset, extSlotWriteX σ2 pos.slot pte, [], hPerf, Z', rfl, rfl, + hJ3, hTree3, hCarry3⟩ + +end s2PtMap + +/-! ## `ArmHighVSpace` `ARMPageTableMap` (`vspace.map_high_page_table` vs `decodeHighPageTableMap`) -/ + +section hiPtMap +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {s : S} {a : A} {σ : State Unit} {m : McState} + +set_option maxHeartbeats 4000000 in +/-- **`vspace.map_high_page_table` against `decodeHighPageTableMap`**. -/ +theorem hi_map_pt_jA + (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) + (hTO : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s q) (hEO : ExtTabOffA (L := L) Z m s) + (hK : Aligned (L.ksb s)) (hT : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s)) + (hTr : TreeA st OS Z.G.chains Z.G.par s) (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) + (cte : CslotPtr) (hab : AbsSlotA Z.th cte) (hos : cslotAddrA cte ∈ OS) + (ptr asid0 va0 : U64) (hcap : capAtA (L.mem s) (cslotAddrA cte) = .PageTable ptr false asid0 va0) + (UP : UnmPtA Z.S1 Z.Y OS H.tcbs (L.mem s) σ ptr) (hS2 : ptr ∉ S2PgE Z.X Z.T2) (hY : ptr ∉ Z.Y) + (vp vasid vaddr : U64) (vmp : Bool) (hvwf : CapWfA (.HighVSpace vp vmp vasid)) + {D : Set (Except SyscallError Fl4ArchInvocation × State Unit) × Prop} + (hD : D = (NonDetE.liftME Fl4ArchInvocation.extVSpace + (decodeHighPageTableMap (Z := Unit) cte (.pageTableCap (specPtr ptr) .normalPtT none) + (extRootCapA .high (specPtr vp) (asidDataA vmp vasid)) vaddr.bv)).run σ) : + vspace.map_high_page_table st s (cslotAddrA cte) ptr (.HighVSpace vp vmp vasid) vaddr ⦃ res => + VsTopPostA (L := L) AR Z OS m σ D s a (res.1, res.2, a) ⦄ := by + have hX : ∀ r ∈ Z.X, ∃ es, σ.kheap (specPtr r) = some (.archObj (.ext (.stage2Root es))) := + fun r hr => ⟨(E.j.1.ext.s2root r hr).choose, (E.j.1.ext.s2root r hr).choose_spec.1⟩ + unfold vspace.map_high_page_table + rw [cur_thread_ok, bind_ok] + refine WP.spec_bind (check_high_vspaceA E.j.1.vs hX vp vasid 1#u64 vmp hvwf) fun rchk hchk => ?_ + rcases rchk with ⟨root, asid1⟩ | err + swap + · simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, bind_tc_ok, WP.spec_ok] + obtain ⟨e', hchkErr, hER⟩ := hchk + refine ⟨rfl, rfl, e', ?_, hER⟩ + rw [hD]; unfold decodeHighPageTableMap; dsimp only + exact run_liftME_errA (run_bind_errorA hchkErr _) + -- check_high_vspace succeeded + obtain ⟨hpair, hrootY, ha1ne, hchkOk⟩ := hchk + have h1 : root = vp := congrArg Prod.fst hpair + have h2 : asid1 = vasid := congrArg Prod.snd hpair + subst root; subst asid1 + have hactY : ActiveHighRootA (L.mem s) (L.ksb s) Z.Pp vp := + activeHighRootA_of_vspaceForAsid E.j.1.vs hX hrootY (vspaceForAsid_of_checkExtRootA (by decide) hchkOk) + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok] + have ha16 : vasid.val < 2 ^ 16 := hvwf + have hD1 : D = (NonDetE.liftME Fl4ArchInvocation.extVSpace (do + NonDetE.whenE (vaddr.bv < highVSpaceBase ∨ vaddr.bv ≥ highVSpaceTop) (NonDetE.throwError (.invalidArgument 0)) + let pos ← NonDetE.liftE (NonDet.gets fun s : State Unit => extLookupSlot s highGeom (specPtr vp) vaddr.bv) + let old ← NonDetE.liftE (NonDet.gets fun s : State Unit => pos.slot.read s) + NonDetE.whenE (pos.shift = pageBits ∨ old ≠ .invalidPTE) (NonDetE.throwError .deleteFirst) + NonDetE.returnOk (.map (.pageTableCap (specPtr ptr) .normalPtT (some (asidOfA vasid, vaddr.bv &&& ~~~(mask pos.shift)))) + cte (.pageTablePTE (ppnFromPptr (specPtr ptr))) pos.slot))).run σ := by + rw [hD]; unfold decodeHighPageTableMap + dsimp only + apply run_liftME_congr_pt + exact run_bind_okA hchkOk _ + -- Range check: vaddr < HIGH_VSPACE_BASE + by_cases hlt : vaddr < fl4_abi.consts.HIGH_VSPACE_BASE + · simp only [if_pos hlt, error.invalid_arg, bind_ok] + have hlt' : vaddr.bv < highVSpaceBase := by + show vaddr.val < highVSpaceBase.toNat + have hb : highVSpaceBase.toNat = fl4_abi.consts.HIGH_VSPACE_BASE.val := by + unfold highVSpaceBase fl4_abi.consts.HIGH_VSPACE_BASE; rfl + rw [hb]; exact hlt + refine (WP.spec_ok _).2 ⟨rfl, rfl, .invalidArgument 0, ?_, Or.inl rfl⟩ + rw [hD1] + exact run_liftME_errA (run_bind_errorA (by simp only [NonDetE.whenE, if_pos (Or.inl hlt')]; rfl) _) + simp only [if_neg hlt] + have hlo : fl4_abi.consts.HIGH_VSPACE_BASE ≤ vaddr := by scalar_tac + have hlt' : ¬ (vaddr.bv < highVSpaceBase) := by + intro h; apply hlt + show vaddr.val < fl4_abi.consts.HIGH_VSPACE_BASE.val + have hb : highVSpaceBase.toNat = fl4_abi.consts.HIGH_VSPACE_BASE.val := by + unfold highVSpaceBase fl4_abi.consts.HIGH_VSPACE_BASE; rfl + rw [← hb]; exact h + -- Range check: vaddr >= HIGH_VSPACE_TOP + by_cases hge : vaddr >= fl4_abi.consts.HIGH_VSPACE_TOP + · simp only [if_pos hge, error.invalid_arg, bind_ok] + have hge' : vaddr.bv ≥ highVSpaceTop := by + show highVSpaceTop.toNat ≤ vaddr.val + have ht : highVSpaceTop.toNat = fl4_abi.consts.HIGH_VSPACE_TOP.val := by + unfold highVSpaceTop fl4_abi.consts.HIGH_VSPACE_TOP; rfl + rw [ht]; exact hge + refine (WP.spec_ok _).2 ⟨rfl, rfl, .invalidArgument 0, ?_, Or.inl rfl⟩ + rw [hD1] + exact run_liftME_errA (run_bind_errorA (by simp only [NonDetE.whenE, if_pos (Or.inr hge')]; rfl) _) + simp only [if_neg hge] + have hge' : ¬ (vaddr.bv ≥ highVSpaceTop) := by + intro h; apply hge + show fl4_abi.consts.HIGH_VSPACE_TOP.val ≤ vaddr.val + have ht : highVSpaceTop.toNat = fl4_abi.consts.HIGH_VSPACE_TOP.val := by + unfold highVSpaceTop fl4_abi.consts.HIGH_VSPACE_TOP; rfl + rw [← ht]; exact h + -- Lookup slot + let pos := extLookupSlot σ highGeom (specPtr vp) vaddr.bv + refine WP.spec_bind (lookup_pt_slot_hiA E.j.1.vs E.j.1.ext hrootY hactY (hi_idx39_eq_pt vaddr hlo)) fun lu ⟨hlsh, hslR⟩ => ?_ + have hsl : SlotRelE (G1E Z.S1 Z.Lv) (TopHiE Z.Y) lu.slot pos.slot pos.shift := + hslR.mono (fun _ _ g => g.1) (fun _ _ _ ht => ht.toTopHiE) + have hD2 : D = (NonDetE.liftME Fl4ArchInvocation.extVSpace (do + NonDetE.whenE (pos.shift = pageBits ∨ pos.slot.read σ ≠ .invalidPTE) (NonDetE.throwError .deleteFirst) + NonDetE.returnOk (.map (.pageTableCap (specPtr ptr) .normalPtT (some (asidOfA vasid, vaddr.bv &&& ~~~(mask pos.shift)))) + cte (.pageTablePTE (ppnFromPptr (specPtr ptr))) pos.slot))).run σ := by + rw [hD1] + apply run_liftME_congr_pt + rw [run_bind_okA (by simp only [NonDetE.whenE, if_neg (not_or.2 ⟨hlt', hge'⟩)]; rfl)] + rw [run_bind_okA (liftE_gets_pt (fun s : State Unit => extLookupSlot s highGeom (specPtr vp) vaddr.bv) σ)] + rw [run_bind_okA (liftE_gets_pt (fun s : State Unit => pos.slot.read s) σ)] + -- Leaf level check: lu.shift = PAGE_BITS (12) + have hpb : vspace.PAGE_BITS = 12#u64 := by unfold vspace.PAGE_BITS; rfl + by_cases h12 : lu.shift = vspace.PAGE_BITS + · rw [if_pos h12] + have hp12 : pos.shift = pageBits := by unfold pageBits; rw [← hlsh, h12, hpb]; rfl + refine (WP.spec_ok _).2 ⟨rfl, rfl, .deleteFirst, ?_, Or.inl trivial⟩ + rw [hD2] + exact run_liftME_errA (run_bind_errorA (by simp only [NonDetE.whenE, if_pos (Or.inl hp12)]; rfl) _) + rw [if_neg h12] + have hp12 : pos.shift ≠ pageBits := by + intro h; apply h12; apply UScalar.eq_of_val_eq; rw [hlsh, h, hpb]; rfl + -- Shift cases: pos.shift ∈ {30, 21}, so lu.shift ≠ TOP_LEVEL_SHIFT (39) + have hshCases : pos.shift = 30 ∨ pos.shift = 21 := by + rcases hiLookup_shift_pt σ (specPtr vp) vaddr.bv with h | h | h + · exact Or.inl h + · exact Or.inr h + · exact (hp12 h).elim + have hlsh' : lu.shift.val = pos.shift := hlsh + have hneTop : lu.shift ≠ vspace.TOP_LEVEL_SHIFT := by + intro h + have hv39 : lu.shift.val = 39 := by rw [h]; unfold vspace.TOP_LEVEL_SHIFT; rfl + rcases hshCases with h' | h' <;> omega + rw [if_neg hneTop] + -- Occupied slot check: (mem lu.slot &&& D_VALID) != 0 + rw [L.read_eq, bind_ok, eq_of_spec_fd P0.Consts.vspace.D_VALID.spec, bind_ok] + simp only [lift, bind_ok, valid_bitA] + have hDescRead := extSlotRead_of_hiSlot_pt E.j.1.vs hslR + by_cases hv : dBitA (L.mem s lu.slot) 0 = true + · rw [if_pos hv] + have hneInv : pos.slot.read σ ≠ .invalidPTE := fun h => by + rw [(descRelE_invA hDescRead).1 h] at hv; cases hv + refine (WP.spec_ok _).2 ⟨rfl, rfl, .deleteFirst, ?_, Or.inl trivial⟩ + rw [hD2] + exact run_liftME_errA (run_bind_errorA (by simp only [NonDetE.whenE, if_pos (Or.inr hneInv)]; rfl) _) + have hvf : dBitA (L.mem s lu.slot) 0 = false := by simpa using hv + rw [if_neg hv] + have hInvEq : pos.slot.read σ = .invalidPTE := (descRelE_invA hDescRead).2 hvf + -- Base address + have hlsh' : lu.shift.val = pos.shift := hlsh + have hs64 : lu.shift.val < 64 := by rcases hshCases with h | h <;> omega + have e63 : (lu.shift &&& 63#u64) = lu.shift := + UScalar.eq_of_val_eq (by + rw [and_maskA lu.shift 6 _ rfl]; exact Nat.mod_eq_of_lt (by simp only [Nat.reducePow]; omega)) + rw [e63, CapRt.shl_u64_eq 1#u64 lu.shift hs64, bind_ok] + try simp only [lift, bind_ok] + rw [ws1A lu.shift hs64] + generalize hbase : vaddr &&& ~~~(⟨BitVec.ofNat 64 (2 ^ lu.shift.val - 1)⟩ : U64) = base + have hbv : base.bv = vaddr.bv &&& ~~~(Fl4SpecA64.mask pos.shift) := by + rw [← hbase, ← hlsh, mask_eq_ofNat_pt (by omega)]; rfl + have hbaseCan : CapRt.Canonical base := by + rw [← hbase]; exact hi_can_pt _ (hi_base_ge_pt vaddr hlo (by omega)) + have hcanon : CapRt.Canonical ptr := by + have h : CapRt.EncWf (capAtA (L.mem s) (cslotAddrA cte)) := CapRt.decodeF_encWf _ _ + rw [hcap] at h; exact h.1 + have hEnc : CapRt.EncWf (.PageTable ptr true vasid base) := ⟨hcanon, by omega, hbaseCan⟩ + have hca : CapRelA (.PageTable ptr true vasid base) + (.archObjectCap (.pageTableCap (specPtr ptr) .normalPtT (some (asidOfA vasid, base.bv)))) := + ⟨ha16, rfl, rfl, Or.inl rfl⟩ + obtain ⟨x, s2, σ2, hkx, hst, hset, hsc, hσ2, HS, hksb2, R2, hcpu2, hw2, hJ2, hFr2⟩ := + restart_setcapJA E RS cte hab hos hEnc hca + rw [hst, bind_ok, hsc, bind_ok] + -- make_user_table and write + rw [show layout.paddr ptr = ok (core.num.U64.wrapping_sub ptr layout.PPTR_BASE) from rfl, bind_ok] + have hpaMod : (core.num.U64.wrapping_sub ptr layout.PPTR_BASE).val % 4096 = 0 := by + rw [ws_valA]; have := Nat.mod_eq_zero_of_dvd UP.al; omega + have hpa48 : (core.num.U64.wrapping_sub ptr layout.PPTR_BASE).val < 2 ^ 48 := by + have := UP.pa; omega + refine WP.spec_bind (s1tab_v _ hpaMod hpa48) fun ew ⟨hv3, hvm⟩ => ?_ + rw [L.write_eq, bind_ok] + -- Determine new level n for ptr in Z.S1 + let n : Nat := (39 - (pos.shift - 9)) / 9 + have hnBounds : 1 ≤ n ∧ n ≤ 3 ∧ 39 - 9 * n = pos.shift - 9 := by + rcases hshCases with h | h <;> simp [n, h] + let S1' : Set U64 := insert ptr Z.S1 + let Lv' : U64 → Nat := Function.update Z.Lv ptr n + let Z' : XSetsA := { Z with S1 := S1', Lv := Lv' } + let pte : PTE := .pageTablePTE (ppnFromPptr (specPtr ptr)) + have hco : capA st s (cslotAddrA cte) = .PageTable ptr false asid0 va0 := by rw [← capAtA_eq, hcap] + obtain ⟨J2, hT2⟩ := hJ2 hTr hnul (by simp) (by rw [hco]; simp) + (UA.of_shapeA (by rw [hco]; rfl) fun h => by rw [hco] at h; simp [isUtA] at h) + ⟨fun _ h => (by cases h), fun _ _ _ _ _ h => (by cases h), fun _ _ _ h => (by cases h), + fun _ _ _ h => (by cases h), trivial⟩ + have hmem2 := unm_mem_keep_pt E.sq.curT (fun t ht => by have := (E.sq.tcbs t ht).fits; simp only [Nat.reducePow] at this; exact this) + (by rw [stA_eq]; exact field_aligned (E.sq.tcbs _ E.sq.curT) (by simp) (by simp)) UP hw2 + obtain ⟨tp, hkp, htp, hep⟩ := UP.spec + have hkp1 : (stUpdA σ σ.curThread x .restart).kheap (specPtr ptr) = some (.archObj (.pageTable tp)) := + stUpd_keep hkx hkp (fun _ h => by cases h) + have hkp2 := HS.keep hkp1 (fun _ _ h => by cases h) (fun _ h => by cases h) + have R2Add : VsRelA Z.Pp S1' Lv' Z.X Z.Y (L.mem s2) (L.ksb s2) σ2 := + J2.1.vs.addPtA UP.nt UP.al UP.ytb (fun i hi => by rw [hmem2 i hi]; exact UP.inv i hi) hkp2 htp hep + (by omega) hnBounds.2.1 + have E2Add : ExtVsRelA Z.Pp S1' Z.X Z.Y Z.T2 Z.Sh2 (L.mem s2) σ2 := + J2.1.ext.addS1PtE hS2 hY + have hAlLu : Aligned lu.slot := by + rcases hsl with ⟨t, i, ⟨ht, -⟩, hi, hluEq, -⟩ | ⟨y, j, hy, hj, hluEq, -, -⟩ + · rw [hluEq]; exact peAF_alignedA (E.j.1.vs.al_mod ht) hi + · rw [hluEq]; exact peAF_alignedA (waWin_modA (E.j.1.vs.yal y hy)) hj + have hW := extWordWriteA (st := st) (s := s2) (slot := lu.slot) (word := ew) hAlLu + have hExtOk : ExtSlotOkX σ2 pos.slot := extSlotOk_of_hiSlot_pt J2.1.vs hsl + have hPerf := performExtMap_mem_pt (pte := pte) hσ2 hExtOk + have hInv : D = ({(.ok (.extVSpace (.map (.pageTableCap (specPtr ptr) .normalPtT (some (asidOfA vasid, base.bv))) + cte pte pos.slot)), σ)}, False) := by + rw [hD2] + apply run_liftME_okA + have hokW : (NonDetE.whenE (pos.shift = pageBits ∨ pos.slot.read σ ≠ .invalidPTE) + (NonDetE.throwError SyscallError.deleteFirst)).run σ = ({(Except.ok (), σ)}, False) := by + unfold NonDetE.whenE + rw [if_neg (by rintro (h | h); exact hp12 h; exact h hInvEq)]; rfl + rw [run_bind_okA hokW, hbv] + rfl + have hfa2 : ∀ w, FootA (L := L) Z s2 w → Aligned w := + FootA.aligned J2.1 (fun c hc o h1 h2 => by rw [nodeAt_ksb hksb2]; exact bxi_nodes E.sys.ent.bxi c hc o h1 h2) + E.scAl E.ap.vcAl + have hEO2 : ExtTabOffA (L := L) Z m s2 := hEO.of_ksA hksb2 hcpu2 + have hTO2 : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s2 q := fun q hq => (hTO q hq).of_ksA hksb2 hcpu2 + have hK2 : Aligned (L.ksb s2) := hksb2 ▸ hK + have hT2' : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s2) := hksb2 ▸ hT + have hdTab : dTabA ew = ptr := by + unfold dTabA dAddrA + have hmask : (281474976706560#u64 : U64) = vspace.ADDR_MASK := by unfold vspace.ADDR_MASK; rfl + rw [hmask, hvm, ← pptrBase_litA] + exact wa_ws ptr layout.PPTR_BASE + have hchEmpty : ∀ j : U64, j.val < 512 → dBitA (L.mem s2 (peAF (dTabA ew) j)) 0 = false ∧ + ∀ t1, σ2.kheap (specPtr (dTabA ew)) = some (.archObj (.pageTable t1)) → + Fl4SpecA64.ptApply t1 j.bv = .invalidPTE := by + rw [hdTab] + intro j hj + refine ⟨by rw [hmem2 j hj]; exact UP.inv j hj, fun t1 hk1 => ?_⟩ + rw [hkp2] at hk1 + cases hk1 + exact hep j hj + have hV3 : VsRelA Z.Pp S1' Lv' Z.X Z.Y (L.mem (L.wr s2 lu.slot ew)) (L.ksb (L.wr s2 lu.slot ew)) + (extSlotWriteX σ2 pos.slot pte) := by + rcases hsl with ⟨t, i, ⟨ht, h1t, hst⟩, hi, hluEq, hslEq⟩ | ⟨y, j, hy, hj, hluEq, hslEq, hshEq⟩ + · rw [hluEq] at hW ⊢; rw [hslEq] + have hne : t ≠ ptr := fun e => UP.nt (e ▸ ht) + have hLvt : Lv' t = Z.Lv t := Function.update_of_ne hne _ _ + have h3t : Z.Lv t < 3 := by omega + have hnEq : n = Z.Lv t + 1 := by omega + have hPte := pteRel_mutA (Tb := S1') (Lv := Lv') (k := Z.Lv t) h3t hv3 hvm UP.pa + (Set.mem_insert _ _) (by show Function.update Z.Lv ptr n ptr = Z.Lv t + 1; rw [Function.update_self, hnEq]) + have hV' := R2Add.of_ptWriteMapE hK2 (Set.mem_insert_of_mem _ ht) (by rw [hLvt]; exact h1t) hi + (hT2' t i hi (Or.inr (Or.inr (Or.inl ht)))) (by rw [hLvt]; exact hPte) hW.2.2 + (fun _ _ => ⟨by rw [hdTab]; exact hne.symm, hchEmpty⟩) + rw [hW.1]; exact hV' + · rw [hluEq] at hW ⊢; rw [hslEq] + have hnEq : n = 2 := by + have h30 : pos.shift = 30 := hshEq + simp [n, h30] + have hPte := pteRel_mutA (Tb := S1') (Lv := Lv') (k := 1) (by omega) hv3 hvm UP.pa + (Set.mem_insert _ _) (by show Function.update Z.Lv ptr n ptr = 1 + 1; rw [Function.update_self, hnEq]) + have hV' := R2Add.of_hiRootWriteMapE hK2 hy hj + (hT2' (wa y 4096#u64) j hj (Or.inr (Or.inr (Or.inr ⟨y, hy, rfl⟩)))) hPte (J2.1.ext.ypp y hy) hW.2.2 + (fun _ _ => hchEmpty) + rw [hW.1]; exact hV' + have hE3 : ExtVsRelA Z.Pp S1' Z.X Z.Y Z.T2 Z.Sh2 (L.mem (L.wr s2 lu.slot ew)) (extSlotWriteX σ2 pos.slot pte) := by + rcases hsl with ⟨t, i, ⟨ht, -⟩, hi, hluEq, hslEq⟩ | ⟨y, j, hy, hj, hluEq, hslEq, -⟩ + · rw [hluEq] at hW ⊢; rw [hslEq] + exact E2Add.of_vsWriteE (mem' := L.mem (L.wr s2 (peAF t i) ew)) J2.1.vs.yal + (Or.inr (Or.inl (Set.mem_insert_of_mem _ ht))) (J2.1.vs.al t ht) hi + (fun b hb hne => by rw [hW.2.2 b hb, if_neg (Ne.symm hne)]) + (Or.inl (Set.mem_insert_of_mem _ ht)) (unm_not_pp_pt J2.1.vs (J2.1.vs.pt t ht).choose_spec.1) + (slotObjPtE (J2.1.vs.al_mod ht) hi) pte + · rw [hluEq] at hW ⊢; rw [hslEq] + exact E2Add.of_vsWriteE (mem' := L.mem (L.wr s2 (peAF (wa y 4096#u64) j) ew)) J2.1.vs.yal + (Or.inr (Or.inr ⟨y, hy, rfl⟩)) (Nat.dvd_of_mod_eq_zero (waWin_modA (J2.1.vs.yal y hy))) hj + (fun b hb hne => by rw [hW.2.2 b hb, if_neg (Ne.symm hne)]) + (sl := .root (specPtr y) j.val) (Or.inr hy) (fun hyP => J2.1.ext.ypg y hy (Or.inl hyP)) rfl pte + have hJ3 : JAtZAX (L := L) AR Z' (L.wr s2 lu.slot ew) a (extSlotWriteX σ2 pos.slot pte) m := by + have hSame : SameA (L := L) (extZA Z) s2 (L.wr s2 lu.slot ew) := by + refine ⟨hW.1, hW.2.1, fun w hw => ?_⟩ + have hne : lu.slot ≠ w := by + intro heq; subst heq + rcases hsl with ⟨t, i, ⟨ht, -⟩, hi, hluEq, -⟩ | ⟨y, j, hy, hj, hluEq, -, -⟩ + · rw [hluEq] at hw; exact (hTO2 t ht).mem i hi hw.extMonoA + · rw [hluEq] at hw; exact hEO2.mem (wa y 4096#u64) (Or.inr ⟨y, hy, rfl⟩) j hj hw + rw [hW.2.2 w (hfa2 w (FootA.s1_monoA hw.extMonoA)), if_neg hne] + have hkh : ∀ p, RelPA (extZA Z) m p → (extSlotWriteX σ2 pos.slot pte).kheap p = σ2.kheap p := by + intro p hp + rcases hsl with ⟨t, i, ⟨ht, -⟩, hi, -, hslEq⟩ | ⟨y, j, hy, hj, -, hslEq, -⟩ + · rw [hslEq]; exact kheap_writeX (by + rw [slotObjPtE (J2.1.vs.al_mod ht) hi] + intro e; subst e; exact (hTO2 t ht).obj _ hp.extMonoA rfl) + · rw [hslEq]; exact kheap_writeX (by + intro e; subst e; exact hEO2.obj y (Or.inr (Or.inl hy)) hp) + have hobj : ∀ ko, (extSlotWriteX σ2 pos.slot pte).kheap (extSlotObjX pos.slot) = some ko → ExtObjA ko → + (∃ l1, ko = .archObj (.ext (.highVSpace l1)) ∧ ∃ r ∈ Z.Y, specPtr r = extSlotObjX pos.slot) := by + intro ko e hx + rcases hsl with ⟨t, i, ⟨ht, -⟩, hi, -, hslEq⟩ | ⟨y, j, hy, hj, -, hslEq, -⟩ + · rw [hslEq] at e ⊢ + have hso : extSlotObjX (ExtSlot.pt (specPtr (peAF t i))) = specPtr t := slotObjPtE (J2.1.vs.al_mod ht) hi + rw [hso] at e + obtain ⟨pt', hk', -, -⟩ := hV3.pt t (Set.mem_insert_of_mem _ ht) + rw [hslEq] at hk' + obtain rfl := hk'.symm.trans e |> Option.some.inj + rcases hx with ⟨_, h'⟩ | ⟨_, h'⟩ | ⟨_, h'⟩ <;> cases h' + · rw [hslEq] at e ⊢ + obtain ⟨l1, hk', -⟩ := hV3.high y hy + rw [hslEq] at hk' + obtain rfl := hk'.symm.trans e |> Option.some.inj + exact ⟨l1, rfl, y, hy, rfl⟩ + have hv' : ∀ p x, (extSlotWriteX σ2 pos.slot pte).kheap p = some (.archObj (.vcpu x)) → + σ2.kheap p = some (.archObj (.vcpu x)) := fun p x e => by + by_cases hp : p = extSlotObjX pos.slot + · subst hp; obtain ⟨_, h, -⟩ := hobj _ e (Or.inl ⟨x, rfl⟩); cases h + · rwa [kheap_writeX hp] at e + have hx' : ∀ p es, (extSlotWriteX σ2 pos.slot pte).kheap p = some (.archObj (.ext (.stage2Root es))) → + ∃ r ∈ Z.X, specPtr r = p := fun p es e => by + by_cases hp : p = extSlotObjX pos.slot + · subst hp; obtain ⟨_, h, -⟩ := hobj _ e (Or.inr (Or.inl ⟨es, rfl⟩)); cases h + · exact J2.2.2.1.back p es (by rwa [kheap_writeX hp] at e) + have hy' : ∀ p l1, (extSlotWriteX σ2 pos.slot pte).kheap p = some (.archObj (.ext (.highVSpace l1))) → + ∃ r ∈ Z.Y, specPtr r = p := fun p l1 e => by + by_cases hp : p = extSlotObjX pos.slot + · subst hp; obtain ⟨_, -, r, hr, hreq⟩ := hobj _ e (Or.inr (Or.inr ⟨l1, rfl⟩)); exact ⟨r, hr, hreq⟩ + · exact J2.2.2.2.back p l1 (by rwa [kheap_writeX hp] at e) + exact J2.of_sameXA hSame (by constructor <;> first | rfl | exact ⟨rfl, rfl, rfl, rfl⟩) hkh rfl rfl hv' hx' hy' + (fun q hq hl => ⟨Set.mem_insert_of_mem _ hq, by + have hne : q ≠ ptr := fun e => UP.nt (e ▸ hq) + show Function.update Z.Lv ptr n q = 0 + rw [Function.update_of_ne hne]; exact hl⟩) hV3 hE3 + have hoffOS : ¬ InOS OS lu.slot := by + have hft : VsFootA Z.Pp Z.S1 Z.Y (L.ksb s) lu.slot := by + rcases hsl with ⟨t, i, ⟨ht, -⟩, hi, hluEq, -⟩ | ⟨y, j, hy, hj, hluEq, -, -⟩ + · rw [hluEq]; exact .pt t i ht hi + · rw [hluEq]; exact .high y j hy hj + exact (E.ap.vs lu.slot hft).1 + have hOSK : OSKeep st OS s2 (L.wr s2 lu.slot ew) := + osKeep_tabWriteA E.sys.ent.os hoffOS (fun w hw hne => by rw [hW.2.2 w hw, if_neg (Ne.symm hne)]) + have hTree3 : TreeA st OS Z.G.chains Z.G.par (L.wr s2 lu.slot ew) := hT2.keepO hOSK + have hCarry3 : CarryA Z (L.mem s) σ → CarryA Z' (L.mem (L.wr s2 lu.slot ew)) (extSlotWriteX σ2 pos.slot pte) := by + have hTy : TyKeepA σ (extSlotWriteX σ2 pos.slot pte) := + TyKeepA.trans (setThreadState_tyA σ.curThread .restart σ _ (by rw [hset]; rfl)) + (TyKeepA.trans (setCap_tyA _ cte _ _ hσ2) + (storeExtSlot_tyA pos.slot pte _ _ (by rw [storeExtSlot_runX hExtOk pte]; rfl))) + have hFr3 : SlotFrA (L := L) OS (cslotAddrA cte) (.PageTable ptr true vasid base) s (L.wr s2 lu.slot ew) := + hFr2.keepR hos hOSK + exact fun C => C.of_slotFrA RS.sub hTr hFr3 rfl rfl + (fun q ⟨hq, hl⟩ => ⟨Set.mem_insert_of_mem _ hq, by + show 1 ≤ Function.update Z.Lv ptr n q + by_cases e : q = ptr + · rw [e, Function.update_self]; exact hnBounds.1 + · rw [Function.update_of_ne e]; exact hl⟩) + (fun _ h => h) + (fun p _ _ e => by cases e; exact Or.inl ⟨Set.mem_insert _ _, by + show 1 ≤ Function.update Z.Lv ptr n ptr; rw [Function.update_self]; exact hnBounds.1⟩) + (by rw [hcap]; exact CapFinEqA.pt2A _ _ _ _ _ _ _) hTy + exact (WP.spec_ok _).2 ⟨.extVSpace (.map (.pageTableCap (specPtr ptr) .normalPtT (some (asidOfA vasid, base.bv))) + cte pte pos.slot), by rw [hInv]; exact ⟨rfl, not_false⟩, + stUpdA σ σ.curThread x .restart, hset, extSlotWriteX σ2 pos.slot pte, [], hPerf, Z', rfl, rfl, + hJ3, hTree3, hCarry3⟩ + +end hiPtMap + +/-! ## Combined `ARMPageTableMap` and full `Cap::PageTable` top (retiring `NoExtXA`) -/ + +section allPtMap +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {s : S} {a : A} {σ : State Unit} {m : McState} + +private theorem dec_ptMap_noExtRootA (calling : Bool) (args : List Data) (xSlot : CapRef) (cte : CslotPtr) + (exs : List (Cap × CslotPtr)) (p : ObjRef) (mp : Option (Asid × VSpaceRef)) + (hno : ∀ c sl rest, exs = (c, sl) :: rest → + (∀ q a, c ≠ .archObjectCap (.ext (.stage2VSpaceCap q a))) ∧ + (∀ q a, c ≠ .archObjectCap (.ext (.highVSpaceCap q a)))) : + (fl4ArchDecodeInvocation (Z := Unit) calling (archL .armPageTableMap) args xSlot cte + (.pageTableCap p .normalPtT mp) exs).run σ = + (NonDetE.liftME Fl4ArchInvocation.spec (archDecodeInvocation (specLabelWord (archL .armPageTableMap)) + args xSlot cte (.pageTableCap p .normalPtT mp) exs)).run σ := by + unfold fl4ArchDecodeInvocation + by_cases hcond : args.length > 1 ∧ exs.length > 0 + · simp only [hcond, true_and, ↓reduceIte] + rcases exs with _ | ⟨⟨vc, sl⟩, rest⟩ + · simp at hcond + simp only [List.getElem?_cons_zero, Fl4SpecA64.the] + have hno' := hno vc sl rest rfl + cases vc with + | archObjectCap ac => + cases ac with + | ext e => + cases e with + | stage2VSpaceCap q a => exact (hno'.1 q a rfl).elim + | highVSpaceCap q a => exact (hno'.2 q a rfl).elim + | stage2FrameCap fp R sz dev fmp => + cases mp with + | none => + simp only [NonDetE.whenE, ne_self_iff_false, ↓reduceIte, ndE_pure_bindA] + | some m0 => + simp only [NonDetE.whenE, NonDetE.throwError] + rw [show (if some m0 ≠ none then (throw (.invalidCapability 0) : SEMonad Unit Unit) else pure ()).run σ = + ({(.error (.invalidCapability 0), σ)}, False) by simp |> fun h => run_bind_errorA h _] + symm + apply run_liftME_errA + show (decodePageTableInvocation (specLabelWord (archL .armPageTableMap)) args cte + (.pageTableCap p .normalPtT (some m0)) + ((.archObjectCap (.ext (.stage2FrameCap fp R sz dev fmp)), sl) :: rest)).run σ = _ + rw [ptInv_mapA invType_ptMapA] + exact ptDec_mappedA _ _ _ _ _ _ _ σ (by simp [hcond.1]) (by simp) + | _ => rfl + | _ => rfl + · simp only [hcond, and_false, ↓reduceIte, show archL .armPageTableMap ≠ archL .armPageTableUnmap by decide] + +set_option maxHeartbeats 4000000 in +/-- **All `ARMPageTableMap` cases** (Stage-1, Stage-2, `ArmHighVSpace`, and all error/rejection paths), +retiring `NoExtXA`. -/ +theorem pt_map_all_jA {ar : machine.Arch A} + (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) + (hTO : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s q) (hEO : ExtTabOffA (L := L) Z m s) + (hK : Aligned (L.ksb s)) (hT : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s)) (hIpa : S2IpaE ar a) + (hTr : TreeA st OS Z.G.chains Z.G.par s) (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) + (cte : CslotPtr) (hab : AbsSlotA Z.th cte) (hos : cslotAddrA cte ∈ OS) + (ptr asid va : U64) (mp : Bool) (hcap : capAtA (L.mem s) (cslotAddrA cte) = .PageTable ptr mp asid va) + (UP : UnmPtA Z.S1 Z.Y OS H.tcbs (L.mem s) σ ptr) (hS2 : ptr ∉ S2PgE Z.X Z.T2) (hY : ptr ∉ Z.Y) + (length buf : U64) (calling : Bool) (ex : Aeneas.Std.Array obj.Slot 3#usize) + (args : List U64) (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) + (x0 : U64) (hx0 : Aeneas.Std.Array.index_usize ex 0#usize = ok x0) + (xSlot : CapRef) (exs : List (Cap × CslotPtr)) + (hex : ExtraRelA Z.th (L.mem s) x0 (exs.map Prod.fst)) : + vspace.decode_page_table st ar s a fl4_abi.label.ARMPageTableMap length (cslotAddrA cte) + (.PageTable ptr mp asid va) buf ex ⦃ res => VsTopPostA (L := L) AR Z OS m σ + ((fl4ArchDecodeInvocation (Z := Unit) calling (archL .armPageTableMap) (args.map UScalar.bv) + xSlot cte (.pageTableCap (specPtr ptr) .normalPtT (mapDataA mp asid va)) exs).run σ) s a res ⦄ := by + by_cases hExt : (∃ p l mp' asid' va', capAtA (L.mem s) x0 = .Ept p l mp' asid' va') ∨ + (∃ p mp' asid', capAtA (L.mem s) x0 = .HighVSpace p mp' asid') + swap + · push_neg at hExt + obtain ⟨hneEpt, hneHi⟩ := hExt + have hno : ∀ c sl rest, exs = (c, sl) :: rest → + (∀ q a, c ≠ .archObjectCap (.ext (.stage2VSpaceCap q a))) ∧ + (∀ q a, c ≠ .archObjectCap (.ext (.highVSpaceCap q a))) := by + rintro c sl rest rfl + rcases hex with ⟨-, hnil⟩ | ⟨-, -, a', rest', ⟨-, habs⟩, hm⟩ + · simp at hnil + simp only [List.map_cons, List.cons.injEq] at hm + obtain ⟨rfl, -⟩ := hm + generalize capAtA (L.mem s) x0 = cc at hneEpt hneHi habs + cases cc with + | Ept _ _ _ _ _ => exact (hneEpt _ _ _ _ _ rfl).elim + | HighVSpace _ _ _ => exact (hneHi _ _ _ rfl).elim + | Frame _ _ _ _ _ isE _ _ => + cases isE <;> constructor <;> intro q a h <;> simp [CapEqA, absCapA, absFrameE] at habs <;> rw [← habs] at h <;> cases h + | PageTable p m asid va => + cases m + · constructor <;> intro q a h <;> simp [CapEqA, absCapA] at habs <;> rw [← habs] at h <;> cases h + · obtain ⟨_, _, rfl, _, _⟩ := CapEqA.pt_mapped_iff.mp habs + constructor <;> intro q a h <;> cases h + | _ => constructor <;> intro q a h <;> simp [CapEqA, absCapA] at habs <;> rw [← habs] at h <;> cases h + rw [dec_ptMap_noExtRootA calling _ xSlot cte exs _ _ hno] + exact WP.spec_mono (pt_map_jA ar E RS cte hab hos hcap UP _ invType_ptMapA length buf args hlen hargs + x0 hx0 exs hex hneEpt hneHi hTr hnul hTO hS2 hY) fun _ h => vsTop_ofPtMapA h + -- Otherwise x0 holds Ept or HighVSpace; unfold decode_page_table + have hct : sched.cur_thread st s = ok (L.curW s) := cur_thread_ok s + have hne48 : ¬ fl4_abi.label.ARMPageTableMap = fl4_abi.label.ARMPageTableUnmap := by + unfold fl4_abi.label.ARMPageTableMap fl4_abi.label.ARMPageTableUnmap; decide + unfold vspace.decode_page_table + simp only [hct, bind_ok, hne48, ↓reduceIte, bne_self_eq_false, Bool.false_eq_true, extra0_eqA hx0] + -- Truncated message: x0 = 0 + rcases hex with ⟨rfl, hnil⟩ | ⟨hx0n, -, vc', vrest, ⟨hvwf, habsX⟩, hmap⟩ + · have he : exs = [] := List.map_eq_nil_iff.1 hnil + subst he + rw [if_pos rfl] + simp only [error.truncated, bind_ok] + refine (WP.spec_ok _).2 ⟨rfl, rfl, .truncatedMessage, ?_, Or.inl trivial⟩ + rw [dec_ptMap_noExtRootA calling _ xSlot cte [] _ _ (fun _ _ _ h => by cases h)] + apply run_liftME_errA + show (decodePageTableInvocation (specLabelWord (archL .armPageTableMap)) (args.map UScalar.bv) cte + (.pageTableCap (specPtr ptr) .normalPtT (mapDataA mp asid va)) []).run σ = _ + rw [ptInv_mapA invType_ptMapA] + exact ptDec_truncA _ _ _ _ _ _ _ σ (fun ⟨_, h⟩ => by simp at h) + rw [if_neg hx0n] + obtain ⟨⟨vc, xsl⟩, rest, rfl⟩ : ∃ e rest, exs = e :: rest := by + cases exs with + | nil => simp at hmap + | cons e rest => exact ⟨e, rest, rfl⟩ + simp only [List.map_cons, List.cons.injEq] at hmap + obtain ⟨rfl, -⟩ := hmap + dsimp only + -- Truncated message: length < 2 + by_cases h2 : length >= 2#u64 + swap + · rw [if_neg h2] + simp only [error.truncated, bind_ok] + have hlt2 : ¬ ((args.map UScalar.bv).length > 1) := by + simp only [List.length_map, ← hlen]; intro h; exact h2 (by scalar_tac) + refine (WP.spec_ok _).2 ⟨rfl, rfl, .truncatedMessage, ?_, Or.inl trivial⟩ + unfold fl4ArchDecodeInvocation + simp only [hlt2, false_and, ↓reduceIte, show archL .armPageTableMap ≠ archL .armPageTableUnmap by decide] + apply run_liftME_errA + show (decodePageTableInvocation (specLabelWord (archL .armPageTableMap)) (args.map UScalar.bv) cte + (.pageTableCap (specPtr ptr) .normalPtT (mapDataA mp asid va)) ((vc, xsl) :: rest)).run σ = _ + rw [ptInv_mapA invType_ptMapA] + exact ptDec_truncA _ _ _ _ _ _ _ σ (fun ⟨h, _⟩ => hlt2 h) + rw [if_pos h2] + have h2' : 2 ≤ args.length := by + have : 2 ≤ length.val := by scalar_tac + omega + obtain ⟨a0, a1, r', rfl⟩ : ∃ a0 a1 r', args = a0 :: a1 :: r' := by + match args, h2' with + | a0 :: a1 :: r', _ => exact ⟨_, _, _, rfl⟩ + simp only [List.map_cons] + -- Mapped page-table cap -> InvalidCapability 0 + cases mp + swap + · simp only [↓reduceIte, error.invalid_cap, bind_ok] + refine (WP.spec_ok _).2 ⟨rfl, rfl, .invalidCapability 0, ?_, Or.inr ⟨0#u64, rfl, rfl⟩⟩ + unfold fl4ArchDecodeInvocation + rcases hExt with ⟨vp, vlev, vmp, vasid, vva, hv⟩ | ⟨vp, vmp, vasid, hv⟩ + · rw [hv] at habsX; simp [CapEqA, absCapA] at habsX; subst habsX + simp only [List.length_cons, List.length_map, gt_iff_lt, Nat.zero_lt_succ, show 1 < r'.length + 1 + 1 by omega, + and_self, ↓reduceIte, List.getElem?_cons_zero, Fl4SpecA64.the, mapDataA, NonDetE.whenE, NonDetE.throwError] + exact run_bind_errorA (by simp) _ + · rw [hv] at habsX; simp [CapEqA, absCapA] at habsX; subst habsX + simp only [List.length_cons, List.length_map, gt_iff_lt, Nat.zero_lt_succ, show 1 < r'.length + 1 + 1 by omega, + and_self, ↓reduceIte, List.getElem?_cons_zero, Fl4SpecA64.the, mapDataA, NonDetE.whenE, NonDetE.throwError] + exact run_bind_errorA (by simp) _ + rw [show mapDataA false asid va = none from rfl] + simp only [Bool.false_eq_true, ↓reduceIte, hargs 0#u64 a0 (by simp), capA_eq, bind_ok] + rw [← capAtA_eq] + rcases hExt with ⟨vp, vlev, vmp, vasid, vva, hv⟩ | ⟨vp, vmp, vasid, hv⟩ + · -- Stage-2 VSpace root (`Cap::Ept`) + rw [hv] at hvwf habsX ⊢ + simp only [CapEqA, absCapA] at habsX + subst habsX + have hvlev : vlev = 0#u64 := UScalar.eq_of_val_eq hvwf.1 + subst hvlev + have hD : (fl4ArchDecodeInvocation (Z := Unit) calling (archL .armPageTableMap) + (a0.bv :: a1.bv :: r'.map UScalar.bv) xSlot cte (.pageTableCap (specPtr ptr) .normalPtT none) + ((.archObjectCap (.ext (.stage2VSpaceCap (specPtr vp) (asidDataA vmp vasid))), xsl) :: rest)).run σ = + (NonDetE.liftME Fl4ArchInvocation.extVSpace + (decodeS2PageTableMap (Z := Unit) cte (.pageTableCap (specPtr ptr) .normalPtT none) + (extRootCapA .stage2 (specPtr vp) (asidDataA vmp vasid)) a0.bv)).run σ := by + unfold fl4ArchDecodeInvocation + simp only [List.length_cons, List.length_map, gt_iff_lt, Nat.zero_lt_succ, show 1 < r'.length + 1 + 1 by omega, + and_self, ↓reduceIte, List.getElem?_cons_zero, Fl4SpecA64.the, NonDetE.whenE, ne_self_iff_false, + ndE_pure_bindA, extRootCapA] + rw [hD] + cases vmp + · rw [show vspace.is_valid_vtable_root (.Ept vp 0#u64 false vasid vva) = ok false from rfl] + simp only [bind_ok, Bool.false_eq_true, ↓reduceIte, error.invalid_cap, WP.spec_ok] + refine ⟨rfl, rfl, .invalidCapability 1, ?_, Or.inr ⟨1#u64, rfl, rfl⟩⟩ + unfold decodeS2PageTableMap; dsimp only + exact run_liftME_errA (run_bind_errorA (checkExtRoot_unmappedA .stage2 (specPtr vp) 1) _) + rw [show vspace.is_valid_vtable_root (.Ept vp 0#u64 true vasid vva) = ok true from rfl] + simp only [bind_ok, Bool.false_eq_true, ↓reduceIte] + have hX : ∀ r ∈ Z.X, ∃ es, σ.kheap (specPtr r) = some (.archObj (.ext (.stage2Root es))) := + fun r hr => ⟨(E.j.1.ext.s2root r hr).choose, (E.j.1.ext.s2root r hr).choose_spec.1⟩ + obtain ⟨rr, hrr, hroot⟩ := spec_ex_fd (ept_rootA E.j.1.vs hX vp vasid vva 1#u64 true hvwf.2) + rw [hrr, bind_ok] + rcases rr with ⟨root, asid1⟩ | err + swap + · simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, bind_tc_ok, WP.spec_ok] + obtain ⟨e', hchkErr, hER⟩ := hroot + refine ⟨rfl, rfl, e', ?_, hER⟩ + unfold decodeS2PageTableMap; dsimp only + exact run_liftME_errA (run_bind_errorA hchkErr _) + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok] + obtain ⟨hpair, hrootX, ha1ne, hchkOk⟩ : (root, asid1) = (vp, vasid) ∧ vp ∈ Z.X ∧ vasid ≠ 0#u64 ∧ _ := hroot + have h1 : root = vp := congrArg Prod.fst hpair + have h2 : asid1 = vasid := congrArg Prod.snd hpair + subst root; subst asid1 + exact s2_map_pt_jA E RS hTO hEO hK hT hIpa hTr hnul cte hab hos ptr asid va hcap UP hS2 hY + vp vasid a0 hrootX + (activeS2RootA_of_vspaceForAsid E.j.1.vs hrootX (vspaceForAsid_of_checkExtRootA (by decide) hchkOk)) + ha1ne hvwf.2 + (D := (NonDetE.liftME Fl4ArchInvocation.extVSpace + (decodeS2PageTableMap (Z := Unit) cte (.pageTableCap (specPtr ptr) .normalPtT none) + (extRootCapA .stage2 (specPtr vp) (asidDataA true vasid)) a0.bv)).run σ) + (by + unfold decodeS2PageTableMap; dsimp only + exact run_liftME_congr_pt (run_bind_okA hchkOk _)) + · -- ArmHighVSpace root (`Cap::HighVSpace`) + rw [hv] at hvwf habsX ⊢ + simp only [CapEqA, absCapA] at habsX + subst habsX + simp only [bind_ok, ↓reduceIte] + have hD : (fl4ArchDecodeInvocation (Z := Unit) calling (archL .armPageTableMap) + (a0.bv :: a1.bv :: r'.map UScalar.bv) xSlot cte (.pageTableCap (specPtr ptr) .normalPtT none) + ((.archObjectCap (.ext (.highVSpaceCap (specPtr vp) (asidDataA vmp vasid))), xsl) :: rest)).run σ = + (NonDetE.liftME Fl4ArchInvocation.extVSpace + (decodeHighPageTableMap (Z := Unit) cte (.pageTableCap (specPtr ptr) .normalPtT none) + (extRootCapA .high (specPtr vp) (asidDataA vmp vasid)) a0.bv)).run σ := by + unfold fl4ArchDecodeInvocation + simp only [List.length_cons, List.length_map, gt_iff_lt, Nat.zero_lt_succ, show 1 < r'.length + 1 + 1 by omega, + and_self, ↓reduceIte, List.getElem?_cons_zero, Fl4SpecA64.the, NonDetE.whenE, ne_self_iff_false, + ndE_pure_bindA, extRootCapA] + rfl + exact WP.spec_bind (hi_map_pt_jA E RS hTO hEO hK hT hTr hnul cte hab hos ptr asid va hcap UP hS2 hY + vp vasid a0 vmp hvwf hD) fun ⟨r, s1⟩ h => (WP.spec_ok _).2 h + +set_option maxHeartbeats 4000000 in +/-- **Full `Cap::PageTable` top (every label, retiring `NoExtXA`)**: `decode_invocation` on a +`Cap::PageTable` at `ARMPageTableMap` (Stage-1, Stage-2, `ArmHighVSpace`), `ARMPageTableUnmap`, +and illegal labels, against `fl4ArchDecodeInvocation`, with `VsTopPostA`. -/ +theorem decInv_pt_all_jA {ar : machine.Arch A} (K : ArchKeepsA ar AR) + (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) + (hTO : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s q) (hEO : ExtTabOffA (L := L) Z m s) + (hK : Aligned (L.ksb s)) (hT : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s)) (hIpa : S2IpaE ar a) + (hPC : PtCapInA Z (L.mem s)) (hFI : FinInvA Z.th (L.mem s) σ Z.G) + (hTr : TreeA st OS Z.G.chains Z.G.par s) (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) + (cte : CslotPtr) (hab : AbsSlotA Z.th cte) (hos : cslotAddrA cte ∈ OS) + (ptr asid va : U64) (mp : Bool) (hcap : capAtA (L.mem s) (cslotAddrA cte) = .PageTable ptr mp asid va) + (lbl length buf : U64) (block call cd fp calling : Bool) (ex : Aeneas.Std.Array obj.Slot 3#usize) + (args : List U64) (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) + (x0 : U64) (hx0 : Aeneas.Std.Array.index_usize ex 0#usize = ok x0) + (xSlot : CapRef) (exs : List (Cap × CslotPtr)) + (hex : ExtraRelA Z.th (L.mem s) x0 (exs.map Prod.fst)) + (hMap : lbl = fl4_abi.label.ARMPageTableMap → + UnmPtA Z.S1 Z.Y OS H.tcbs (L.mem s) σ ptr ∧ ptr ∉ S2PgE Z.X Z.T2 ∧ ptr ∉ Z.Y) : + syscall.decode_invocation st ar s a lbl length (cslotAddrA cte) (.PageTable ptr mp asid va) block call cd fp buf + ex ⦃ res => VsTopPostA (L := L) AR Z OS m σ ((fl4ArchDecodeInvocation (Z := Unit) calling + (fl4LabelOf lbl.bv) (args.map UScalar.bv) xSlot cte + (.pageTableCap (specPtr ptr) .normalPtT (mapDataA mp asid va)) exs).run σ) s a res ⦄ := by + rw [decInv_ptA] + by_cases h48 : lbl = fl4_abi.label.ARMPageTableUnmap + · subst h48 + have hlab : fl4LabelOf fl4_abi.label.ARMPageTableUnmap.bv = archL .armPageTableUnmap := by + rw [fl4LabelOf_bvA, show fl4_abi.label.ARMPageTableUnmap.val = 48 by simp [fl4_abi.label.ARMPageTableUnmap]]; rfl + rw [hlab] + obtain ⟨b, hf, -⟩ := spec_existsA (Fl4ProofsA64.P0.cdt.is_final_capability.p0 st s (cslotAddrA cte) L.total) + cases b + · obtain ⟨hD, hdec, hrel, -⟩ := pt_unmap_nfJA (ar := ar) E hFI length buf calling ex (args.map UScalar.bv) xSlot + exs cte hab ptr asid va mp hcap hf + rw [hdec] + exact (WP.spec_ok _).2 ⟨rfl, rfl, .revokeFirst, hD, Or.inl hrel⟩ + cases mp + · rw [show mapDataA false asid va = none from rfl] + obtain ⟨hD, x, -, hset, hrest⟩ := pt_unmapU_decJA (ar := ar) E RS length buf calling ex (args.map UScalar.bv) + xSlot exs cte hab ptr asid va hcap hFI hf + obtain ⟨s3, σ3, hdec, hp, J3, T3, C3⟩ := hrest hTr hnul + rw [hdec] + exact (WP.spec_ok _).2 ⟨_, okD_memA hD, _, hset, σ3, [], mem_archNRA hp, Z, rfl, rfl, J3, T3, C3⟩ + rw [show mapDataA true asid va = some (BitVec.ofNat 16 asid.val, va.bv) from rfl] + have hS1 : ∀ k, asidRootKind σ (BitVec.ofNat 16 asid.val) = k → (k = none ∨ k = some .stage1) → + vspace.decode_page_table st ar s a fl4_abi.label.ARMPageTableUnmap length (cslotAddrA cte) + (.PageTable ptr true asid va) buf ex ⦃ res => VsTopPostA (L := L) AR Z OS m σ + ((fl4ArchDecodeInvocation (Z := Unit) calling (archL .armPageTableUnmap) + (args.map UScalar.bv) xSlot cte (.pageTableCap (specPtr ptr) .normalPtT + (some (BitVec.ofNat 16 asid.val, va.bv))) exs).run σ) s a res ⦄ := by + rintro k hk hk' + obtain ⟨hD, x, -, hset, hrest⟩ := pt_unmapS1_decJA K E RS hTO hEO hK hT length buf calling ex + (args.map UScalar.bv) xSlot exs cte hab ptr asid va hPC hcap hFI hf (by subst hk; exact hk') + obtain ⟨s3, σ3, fa, hdec, hp, hJ, T3, C3⟩ := hrest hTr hnul + rw [hdec] + exact vsTop_ofFaA hD hset (mem_archNRA hp) hJ T3 C3 + rcases hk : asidRootKind σ (BitVec.ofNat 16 asid.val) with _ | k + · exact hS1 _ hk (Or.inl rfl) + by_cases hk1 : k = .stage1 + · subst hk1; exact hS1 _ hk (Or.inr rfl) + obtain ⟨hD, x, -, hset, hrest⟩ := pt_unmap_decJA K E RS hTO hEO hK hT hIpa length buf calling ex + (args.map UScalar.bv) xSlot exs cte hab ptr asid va hPC hcap hFI hf k hk hk1 + obtain ⟨s3, σ3, fa, hdec, hp, hJ, T3, C3⟩ := hrest hTr hnul + rw [hdec] + exact vsTop_ofFaA hD hset (mem_archExtA hp) hJ T3 C3 + by_cases h47 : lbl = fl4_abi.label.ARMPageTableMap + · subst h47 + obtain ⟨UP, hS2, hY⟩ := hMap rfl + have hlab : fl4LabelOf fl4_abi.label.ARMPageTableMap.bv = archL .armPageTableMap := by + rw [fl4LabelOf_bvA, show fl4_abi.label.ARMPageTableMap.val = 47 by simp [fl4_abi.label.ARMPageTableMap]]; rfl + rw [hlab] + exact pt_map_all_jA E RS hTO hEO hK hT hIpa hTr hnul cte hab hos ptr asid va mp hcap UP hS2 hY + length buf calling ex args hlen hargs x0 hx0 xSlot exs hex + rw [decode_pt_otherA lbl length _ buf ex ptr asid va mp h48 h47] + have hv48 : lbl.val ≠ 48 := fun e => h48 (UScalar.eq_of_val_eq (by rw [e]; simp [fl4_abi.label.ARMPageTableUnmap])) + have hv47 : lbl.val ≠ 47 := fun e => h47 (UScalar.eq_of_val_eq (by rw [e]; simp [fl4_abi.label.ARMPageTableMap])) + rw [dec_ptOtherA calling _ xSlot cte exs _ + (by rw [fl4LabelOf_bvA]; exact labNA_neA (fun n hn => (labNA_tabA n hn).1) (by decide) hv47) + (by rw [fl4LabelOf_bvA]; exact labNA_neA (fun n hn => (labNA_tabA n hn).2.1) (by decide) hv48)] + exact (WP.spec_ok _).2 ⟨rfl, rfl, .illegalOperation, rfl, Or.inl trivial⟩ + +end allPtMap + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/ExtVsDefA.lean b/proofs/Fl4ProofsA64/P3/ExtVsDefA.lean new file mode 100644 index 000000000..abe46d95e --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ExtVsDefA.lean @@ -0,0 +1,369 @@ +/- +P3 (aarch64, A64-P3 S3 gen 18): **the definitions of the Stage-2 / `ArmHighVSpace` relation `ExtVsRelA`**, split out +of `P3/ExtVsRelA` (ExtVsA gens 1–3, unchanged) so that `P3/JointA` can carry it as **`JAtZA.ext`** (ruling 18:13Z +item 4): the descriptor vocabulary (`DescRelE`, `S2AttrE`, `G1E` / `G2E`, `GoodTabE`), the relation `ExtVsRelA` +(`s2root`, `s2pt`, `tsh`, `wl0`, `wlo`, `xal`, `tal`, `pgd`, `t2x`, `ypg`, `ypp`), its footprint `ExtFootE` and the +frame lemmas `ExtVsRelA.of_mem` / `of_spec` / `foot_alignedE` that `P3/FrameA` uses. `P3/ExtVsRelA` keeps the +Stage-1 bridge (`descE_of_pteRel`, `VsRelA.goodE`), `of_writesIn` and the cross frames, and imports this module. + +New here (gen 18): `ExtVsRelA.empty` (no root, window or Stage-2 table: the boot witnesses `w0` / `w1`), +`ExtVsRelA.not_tcb` / `not_cnode` (a root or Stage-2 table is no TCB / CNode of the spec, so slot-only and TCB-state +spec steps keep it). + +No named premise. +-/ +import Fl4ProofsA64.P3.VsRelA +import Fl4SpecA64 +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## Descriptors at a shift -/ + +/-- The output address of a leaf at shift `sh` (`page_base` / `leaf_base`). -/ +def baseShE (w : U64) (sh : Nat) : Fl4SpecA64.Paddr := (dAddrA w).bv &&& ~~~(BitVec.ofNat 64 (2 ^ sh - 1)) + +/-- **A descriptor at a slot of shift `sh`** (module docstring). -/ +abbrev DescRelE (G : U64 → Nat → Prop) (sh : Nat) (w : U64) : Fl4SpecA64.PTE → Prop + | .invalidPTE => dBitA w 0 = false + | .pageTablePTE ppn => 12 < sh ∧ dBitA w 0 = true ∧ dBitA w 1 = true ∧ (dAddrA w).val < 2 ^ 44 ∧ ppn = ppnA w ∧ + G (dTabA w) (sh - 9) + | .pagePTE a _ _ _ => dBitA w 0 = true ∧ (dBitA w 1 = true ↔ sh = 12) ∧ a = baseShE w sh ∧ (dAddrA w).val < 2 ^ 44 + +theorem descRelE_zero (G : U64 → Nat → Prop) (sh : Nat) : DescRelE G sh 0#u64 .invalidPTE := by + show dBitA 0#u64 0 = false; decide + +/-- A Stage-2 leaf's rights (`s2_rights_of`: `S2AP` 3 read-write, 1 read-only, else none). -/ +def s2RightsE (w : U64) : Fl4SpecA64.VMRights := + if apA w = 3 then Fl4SpecA64.vmReadWrite else if apA w = 1 then Fl4SpecA64.vmReadOnly else Fl4SpecA64.vmKernelOnly + +/-- **A Stage-2 leaf's attributes** (`make_s2_leaf`): small iff the page level, the rights `s2RightsE`, `Execute` +iff `XN` (bit 54) clear, `Device` iff `MemAttr` (bits 2–5) is not normal write-back, never `Global`. -/ +abbrev S2AttrE (sh : Nat) (w : U64) : Fl4SpecA64.PTE → Prop + | .pagePTE _ small attrs rights => small = decide (sh = 12) ∧ rights = s2RightsE w ∧ + (Fl4SpecA64.VMAttribute.execute ∈ attrs ↔ dBitA w 54 = false) ∧ + (Fl4SpecA64.VMAttribute.device ∈ attrs ↔ (w.val / 4) % 16 ≠ 15) ∧ Fl4SpecA64.VMAttribute.global ∉ attrs + | _ => True + +/-- The Stage-1 tables below a window: a table of `Tb` below the root level, at its depth's shift. -/ +abbrev G1E (Tb : Set U64) (Lv : U64 → Nat) (t : U64) (sh : Nat) : Prop := t ∈ Tb ∧ 1 ≤ Lv t ∧ sh = 39 - 9 * Lv t + +/-- The Stage-2 tables at their shift. -/ +abbrev G2E (T2 : Set U64) (Sh2 : U64 → Nat) (t : U64) (sh : Nat) : Prop := t ∈ T2 ∧ Sh2 t = sh + +theorem DescRelE.mono {G G' : U64 → Nat → Prop} {sh : Nat} {w : U64} {pte : Fl4SpecA64.PTE} + (h : DescRelE G sh w pte) (hG : dBitA w 0 = true → dBitA w 1 = true → 12 < sh → G (dTabA w) (sh - 9) → + G' (dTabA w) (sh - 9)) : + DescRelE G' sh w pte := by + cases pte with + | invalidPTE => exact h + | pageTablePTE ppn => + obtain ⟨h12, hb0, hb1, h44, hppn, hg⟩ := h + exact ⟨h12, hb0, hb1, h44, hppn, hG hb0 hb1 h12 hg⟩ + | pagePTE _ _ _ _ => exact h + +/-- **The tables below a root are walkable** (module docstring). -/ +abbrev GoodTabE (G : U64 → Nat → Prop) (mem : U64 → U64) (σ : Fl4SpecA64.State Unit) : Prop := + ∀ t sh, G t sh → 12 ≤ sh ∧ sh ≤ 39 ∧ t.val % 4096 = 0 ∧ ∃ pt, σ.kheap (specPtr t) = some (.archObj (.pageTable pt)) ∧ + Fl4SpecA64.ptType pt = .normalPtT ∧ ∀ i : U64, i.val < 512 → + DescRelE G sh (mem (peAF t i)) (Fl4SpecA64.ptApply pt i.bv) + +/-! ## Stage-2 reachability from `armAsidTable` (Design 1A) -/ + +/-- A Stage-2 root `r ∈ X` installed at a nonzero ASID in a pool `p ∈ Pp` of `σ.archState.armAsidTable`. -/ +def ActiveS2RootA (Pp X : Set U64) (σ : Fl4SpecA64.State Unit) (r : U64) : Prop := + r ∈ X ∧ ∃ (asid : Fl4SpecA64.Asid) (p : U64) (f : Fl4SpecA64.AsidPool), + 0 < asid ∧ p ∈ Pp ∧ + σ.archState.armAsidTable (Fl4SpecA64.asidHighBitsOf asid) = some (specPtr p) ∧ + σ.kheap (specPtr p) = some (.archObj (.asidPool f)) ∧ + f (Fl4SpecA64.asidLowBitsOf asid) = some (.asidPoolVSpace (specPtr r)) + +/-- Stage-2 reachability (tag-1 root `r ∈ X` or child table `t ∈ T2` reachable from an active Stage-2 root). -/ +inductive S2ReachA (Pp X T2 : Set U64) (Sh2 : U64 → Nat) (mem : U64 → U64) + (σ : Fl4SpecA64.State Unit) : U64 → Prop + | root {r : U64} : + ActiveS2RootA Pp X σ r → S2ReachA Pp X T2 Sh2 mem σ r + | stepRoot {r i : U64} : + ActiveS2RootA Pp X σ r → i.val < Fl4SpecA64.s2Geom.topMask + 1 → + (mem (peAF r i) &&& 3#u64) = 3#u64 → + dTabA (mem (peAF r i)) ∈ T2 → + S2ReachA Pp X T2 Sh2 mem σ (dTabA (mem (peAF r i))) + | stepPt {t i : U64} : + S2ReachA Pp X T2 Sh2 mem σ t → t ∈ T2 → 12 < Sh2 t → i.val < 512 → + (mem (peAF t i) &&& 3#u64) = 3#u64 → + dTabA (mem (peAF t i)) ∈ T2 → + S2ReachA Pp X T2 Sh2 mem σ (dTabA (mem (peAF t i))) + +/-- Reachable Stage-2 child tables at their shift. -/ +abbrev G2ReachE (Pp X T2 : Set U64) (Sh2 : U64 → Nat) (mem : U64 → U64) (σ : Fl4SpecA64.State Unit) + (t : U64) (sh : Nat) : Prop := + G2E T2 Sh2 t sh ∧ S2ReachA Pp X T2 Sh2 mem σ t + +private theorem and3_of_bits_ext {e : U64} (hb0 : dBitA e 0 = true) (hb1 : dBitA e 1 = true) : + (e &&& 3#u64) = 3#u64 := by + have d0 : e.val % 2 = 1 := by + have : decide (e.val % 2 = 1) = true := by + unfold dBitA at hb0; rw [BitVec.getLsbD, Nat.testBit_zero] at hb0; exact hb0 + exact of_decide_eq_true this + have d1 : e.val / 2 % 2 = 1 := by + have : decide (e.val / 2 % 2 = 1) = true := by + unfold dBitA at hb1 + rw [BitVec.getLsbD, ← Nat.zero_add 1, ← Nat.testBit_div_two_pow, Nat.testBit_zero] at hb1 + exact hb1 + exact of_decide_eq_true this + apply UScalar.eq_of_val_eq + rw [UScalar.val_and, show (3#u64 : U64).val = 2 ^ 2 - 1 from rfl, Nat.and_two_pow_sub_one_eq_mod] + show e.val % 4 = 3 + omega + +/-! ## The relation -/ + +/-- The pages of the Stage-2 objects: every table of `T2`, both pages of every root of `X`. -/ +def S2PgE (X T2 : Set U64) : Set U64 := {q | q ∈ T2 ∨ ∃ r ∈ X, q = r ∨ q = wa r 4096#u64} + +/-- **The Stage-2 / high-window relation** (module docstring; Design 1A reachability guards). -/ +structure ExtVsRelA (Pp Tb X Y T2 : Set U64) (Sh2 : U64 → Nat) (mem : U64 → U64) (σ : Fl4SpecA64.State Unit) : + Prop where + s2root : ∀ r ∈ X, ∃ es, σ.kheap (specPtr r) = some (.archObj (.ext (.stage2Root es))) ∧ + (ActiveS2RootA Pp X σ r → + ∀ i : U64, (h : i.val < 1024) → i.val < Fl4SpecA64.s2Geom.topMask + 1 → + DescRelE (G2E T2 Sh2) Fl4SpecA64.s2Geom.topShift (mem (peAF r i)) (es ⟨i.val, h⟩) ∧ + S2AttrE Fl4SpecA64.s2Geom.topShift (mem (peAF r i)) (es ⟨i.val, h⟩)) + s2pt : ∀ t ∈ T2, ∃ pt, σ.kheap (specPtr t) = some (.archObj (.pageTable pt)) ∧ + Fl4SpecA64.ptType pt = .normalPtT ∧ + (S2ReachA Pp X T2 Sh2 mem σ t → ∀ i : U64, i.val < 512 → + DescRelE (G2E T2 Sh2) (Sh2 t) (mem (peAF t i)) (Fl4SpecA64.ptApply pt i.bv) ∧ + S2AttrE (Sh2 t) (mem (peAF t i)) (Fl4SpecA64.ptApply pt i.bv)) + tsh : ∀ t ∈ T2, 12 ≤ Sh2 t ∧ Sh2 t ≤ 39 + wl0 : ∀ y ∈ Y, dBitA (mem (peAF y 511#u64)) 0 = true ∧ dBitA (mem (peAF y 511#u64)) 1 = true ∧ + dTabA (mem (peAF y 511#u64)) = wa y 4096#u64 + /-- (ExtVsA gen 3) the window's level-0 entries `0..255` are zero (`init_high_vspace` copies the kernel's TTBR1 + level-0 entries, which map nothing below index 256). -/ + wlo : ∀ y ∈ Y, ∀ i : U64, i.val < 256 → mem (peAF y i) = 0#u64 + xal : ∀ r ∈ X, 8192 ∣ r.val + tal : ∀ t ∈ T2, 4096 ∣ t.val + pgd : ∀ q ∈ S2PgE X T2, q ∉ VsPtA Pp Tb Y ∧ q ∉ Y + t2x : ∀ t ∈ T2, ∀ r ∈ X, t ≠ r ∧ t ≠ wa r 4096#u64 + ypg : ∀ y ∈ Y, y ∉ VsPtA Pp Tb Y + ypp : ∀ y ∈ Y, wa y 4096#u64 ∉ Pp + +section rel +variable {Pp Tb X Y T2 : Set U64} {Sh2 : U64 → Nat} {σ : Fl4SpecA64.State Unit} + +/-- **The reachable Stage-2 tables are walkable** (module docstring). -/ +theorem ExtVsRelA.goodE {mem : U64 → U64} (E : ExtVsRelA Pp Tb X Y T2 Sh2 mem σ) : + GoodTabE (G2ReachE Pp X T2 Sh2 mem σ) mem σ := by + rintro t sh ⟨⟨ht, rfl⟩, hreach⟩ + obtain ⟨pt, hk, hty, he⟩ := E.s2pt t ht + refine ⟨(E.tsh t ht).1, (E.tsh t ht).2, Nat.mod_eq_zero_of_dvd (E.tal t ht), pt, hk, hty, fun i hi => ?_⟩ + exact (he hreach i hi).1.mono fun hb0 hb1 h12 hg => + ⟨hg, .stepPt hreach ht h12 hi (and3_of_bits_ext hb0 hb1) hg.1⟩ + +end rel + +/-! ## Words and pages -/ + +theorem peAF_val1kE {p i : U64} (hp : p.val + 8192 ≤ 2 ^ 64) (hi : i.val < 1024) : + (peAF p i).val = p.val + 8 * i.val := by + unfold peAF + rw [wa_valP] + have e : (core.num.U64.wrapping_mul i 8#u64).val = 8 * i.val := by + simp [core.num.U64.wrapping_mul_val_eq, U64.size_def, U64.numBits] + omega + rw [e] + simp only [Nat.reducePow] at hp ⊢ + omega + +theorem al8kE {r : U64} (hr : 8192 ∣ r.val) : r.val + 8192 ≤ 2 ^ 64 := by + have hb : r.val < 2 ^ 64 := r.bv.isLt + simp only [Nat.reducePow] at hb ⊢ + omega + +/-- **A root's word lies in one of its two pages** (`bits::STAGE2_VSPACE = 13`: 1024 entries). -/ +theorem rootWordE {r i : U64} (hr : 8192 ∣ r.val) (hi : i.val < 1024) : + ∃ q j : U64, (q = r ∨ q = wa r 4096#u64) ∧ j.val < 512 ∧ peAF r i = peAF q j := by + by_cases h : i.val < 512 + · exact ⟨r, i, Or.inl rfl, h, rfl⟩ + · have hj : (⟨BitVec.ofNat 64 (i.val - 512)⟩ : U64).val = i.val - 512 := by + show (BitVec.ofNat 64 (i.val - 512)).toNat = _ + rw [BitVec.toNat_ofNat] + apply Nat.mod_eq_of_lt + simp only [Nat.reducePow] + omega + refine ⟨wa r 4096#u64, ⟨BitVec.ofNat 64 (i.val - 512)⟩, Or.inr rfl, by rw [hj]; omega, ?_⟩ + apply UScalar.eq_of_val_eq + have h8 := al8kE hr + rw [peAF_val1kE h8 hi, peAF_valA (by rw [waWin_valA hr]; omega) (by rw [hj]; omega), waWin_valA hr, hj] + omega + +theorem al4kE {r : U64} (hr : 8192 ∣ r.val) : 4096 ∣ r.val := Nat.dvd_trans (by decide) hr + +theorem al4kHiE {r : U64} (hr : 8192 ∣ r.val) : 4096 ∣ (wa r 4096#u64).val := + Nat.dvd_of_mod_eq_zero (waWin_modA hr) + +/-- Two windows (or roots) never share a page: `y + 4096 ≠ y'` for 8 KiB-aligned objects. -/ +theorem hiNeE {y y' : U64} (hy : 8192 ∣ y.val) (hy' : 8192 ∣ y'.val) : wa y 4096#u64 ≠ y' := fun e => by + have := congrArg UScalar.val e + rw [waWin_valA hy] at this + omega + +/-! ## The footprint -/ + +/-- **The words `ExtVsRelA` reads** (module docstring). -/ +inductive ExtFootE (X Y T2 : Set U64) : U64 → Prop + | s2lo (r i : U64) : r ∈ X → i.val < 512 → ExtFootE X Y T2 (peAF r i) + | s2hi (r i : U64) : r ∈ X → i.val < 512 → ExtFootE X Y T2 (peAF (wa r 4096#u64) i) + | s2pt (t i : U64) : t ∈ T2 → i.val < 512 → ExtFootE X Y T2 (peAF t i) + | wl0 (y : U64) : y ∈ Y → ExtFootE X Y T2 (peAF y 511#u64) + | wlo (y i : U64) : y ∈ Y → i.val < 256 → ExtFootE X Y T2 (peAF y i) + +theorem rootFootE {X Y T2 : Set U64} {r i : U64} (hX : r ∈ X) (hr : 8192 ∣ r.val) (hi : i.val < 1024) : + ExtFootE X Y T2 (peAF r i) := by + obtain ⟨q, j, hq | hq, hj, e⟩ := rootWordE hr hi <;> rw [e, hq] + · exact .s2lo r j hX hj + · exact .s2hi r j hX hj + +section foot +variable {Pp Tb X Y T2 : Set U64} {Sh2 : U64 → Nat} {σ : Fl4SpecA64.State Unit} + +/-- **Every footprint word is a word of a page-aligned Stage-2 page or of a window's level-0 page.** -/ +theorem ExtFootE.pgE {mem : U64 → U64} (E : ExtVsRelA Pp Tb X Y T2 Sh2 mem σ) (hY : ∀ y ∈ Y, 8192 ∣ y.val) + {b : U64} (h : ExtFootE X Y T2 b) : + ∃ q j : U64, j.val < 512 ∧ b = peAF q j ∧ 4096 ∣ q.val ∧ (q ∈ S2PgE X T2 ∨ q ∈ Y) := by + cases h with + | s2lo r i hr hi => exact ⟨r, i, hi, rfl, al4kE (E.xal r hr), Or.inl (Or.inr ⟨r, hr, Or.inl rfl⟩)⟩ + | s2hi r i hr hi => exact ⟨_, i, hi, rfl, al4kHiE (E.xal r hr), Or.inl (Or.inr ⟨r, hr, Or.inr rfl⟩)⟩ + | s2pt t i ht hi => exact ⟨t, i, hi, rfl, E.tal t ht, Or.inl (Or.inl ht)⟩ + | wl0 y hy => exact ⟨y, 511#u64, by decide, rfl, al4kE (hY y hy), Or.inr hy⟩ + | wlo y i hy hi => exact ⟨y, i, by omega, rfl, al4kE (hY y hy), Or.inr hy⟩ + +/-- **The footprint is aligned** (roots, tables and windows page-aligned). -/ +theorem ExtVsRelA.foot_alignedE {mem : U64 → U64} (E : ExtVsRelA Pp Tb X Y T2 Sh2 mem σ) + (hY : ∀ y ∈ Y, 8192 ∣ y.val) : ∀ b, ExtFootE X Y T2 b → Aligned b := by + intro b h + obtain ⟨q, j, hj, rfl, hq, -⟩ := ExtFootE.pgE E hY h + exact peAF_alignedA (Nat.mod_eq_zero_of_dvd hq) hj + +theorem S2ReachA.of_mem {mem mem' : U64 → U64} {q : U64} + (hxal : ∀ r ∈ X, 8192 ∣ r.val) + (h : ∀ b, ExtFootE X Y T2 b → mem' b = mem b) + (hr : S2ReachA Pp X T2 Sh2 mem' σ q) : S2ReachA Pp X T2 Sh2 mem σ q := by + induction hr with + | root ha => exact .root ha + | @stepRoot r i ha hi h3 ht2 => + have hi1k : i.val < 1024 := by + unfold Fl4SpecA64.s2Geom Fl4SpecA64.s2GeomOf at hi + split_ifs at hi <;> dsimp only at hi <;> omega + rw [h _ (rootFootE ha.1 (hxal r ha.1) hi1k)] at h3 ht2 ⊢ + exact .stepRoot ha hi h3 ht2 + | @stepPt t i _ ht h12 hi h3 ht2 ih => + rw [h _ (.s2pt t i ht hi)] at h3 ht2 ⊢ + exact .stepPt ih ht h12 hi h3 ht2 + +theorem ActiveS2RootA.of_spec {σ' : Fl4SpecA64.State Unit} {r : U64} + (ht : σ'.archState.armAsidTable = σ.archState.armAsidTable) + (hkP : ∀ p ∈ Pp, σ'.kheap (specPtr p) = σ.kheap (specPtr p)) + (ha : ActiveS2RootA Pp X σ' r) : ActiveS2RootA Pp X σ r := by + obtain ⟨hr, asid, p, f, ha0, hp, htab, hkh, hf⟩ := ha + rw [ht] at htab + rw [hkP p hp] at hkh + exact ⟨hr, asid, p, f, ha0, hp, htab, hkh, hf⟩ + +theorem S2ReachA.of_spec {mem : U64 → U64} {σ' : Fl4SpecA64.State Unit} {q : U64} + (ht : σ'.archState.armAsidTable = σ.archState.armAsidTable) + (hkP : ∀ p ∈ Pp, σ'.kheap (specPtr p) = σ.kheap (specPtr p)) + (hr : S2ReachA Pp X T2 Sh2 mem σ' q) : S2ReachA Pp X T2 Sh2 mem σ q := by + induction hr with + | root ha => exact .root (ha.of_spec ht hkP) + | stepRoot ha hi h3 ht2 => exact .stepRoot (ha.of_spec ht hkP) hi h3 ht2 + | stepPt _ hT h12 hi h3 ht2 ih => exact .stepPt ih hT h12 hi h3 ht2 + +/-- **`ExtVsRelA` reads only its footprint.** -/ +theorem ExtVsRelA.of_mem {mem mem' : U64 → U64} (E : ExtVsRelA Pp Tb X Y T2 Sh2 mem σ) + (h : ∀ b, ExtFootE X Y T2 b → mem' b = mem b) : ExtVsRelA Pp Tb X Y T2 Sh2 mem' σ where + s2root r hr := by + obtain ⟨es, hk, he⟩ := E.s2root r hr + exact ⟨es, hk, fun ha i hi hm => by rw [h _ (rootFootE hr (E.xal r hr) hi)]; exact he ha i hi hm⟩ + s2pt t ht := by + obtain ⟨pt, hk, hty, he⟩ := E.s2pt t ht + exact ⟨pt, hk, hty, fun hr i hi => by rw [h _ (.s2pt t i ht hi)]; exact he (hr.of_mem E.xal h) i hi⟩ + tsh := E.tsh + wl0 y hy := by rw [h _ (.wl0 y hy)]; exact E.wl0 y hy + wlo y hy i hi := by rw [h _ (.wlo y i hy hi)]; exact E.wlo y hy i hi + xal := E.xal + tal := E.tal + pgd := E.pgd + t2x := E.t2x + ypg := E.ypg + ypp := E.ypp + +/-- **The spec-side frame**: the ASID table, pools, roots and Stage-2 tables kept. -/ +theorem ExtVsRelA.of_spec {mem : U64 → U64} {σ' : Fl4SpecA64.State Unit} (E : ExtVsRelA Pp Tb X Y T2 Sh2 mem σ) + (ht : σ'.archState.armAsidTable = σ.archState.armAsidTable) + (hk : ∀ q, q ∈ Pp ∨ q ∈ X ∨ q ∈ T2 → σ'.kheap (specPtr q) = σ.kheap (specPtr q)) : + ExtVsRelA Pp Tb X Y T2 Sh2 mem σ' where + s2root r hr := by + rw [hk r (Or.inr (Or.inl hr))] + obtain ⟨es, hkh, he⟩ := E.s2root r hr + exact ⟨es, hkh, fun ha => he (ha.of_spec ht (fun p hp => hk p (Or.inl hp)))⟩ + s2pt t ht_in := by + rw [hk t (Or.inr (Or.inr ht_in))] + obtain ⟨pt, hkh, hty, he⟩ := E.s2pt t ht_in + exact ⟨pt, hkh, hty, fun hr => he (hr.of_spec ht (fun p hp => hk p (Or.inl hp)))⟩ + tsh := E.tsh + wl0 := E.wl0 + wlo := E.wlo + xal := E.xal + tal := E.tal + pgd := E.pgd + t2x := E.t2x + ypg := E.ypg + ypp := E.ypp + +end foot + +section rel2 +variable {Pp Tb X Y T2 : Set U64} {Sh2 : U64 → Nat} {σ : Fl4SpecA64.State Unit} {mem : U64 → U64} + +/-- (S3 gen 18) **`ExtVsRelA` with no root, window or Stage-2 table** (the boot witnesses). -/ +theorem ExtVsRelA.empty (Pp Tb : Set U64) (Sh2 : U64 → Nat) (mem : U64 → U64) (σ : Fl4SpecA64.State Unit) : + ExtVsRelA Pp Tb ∅ ∅ ∅ Sh2 mem σ where + s2root _ h := by simp at h + s2pt _ h := by simp at h + tsh _ h := by simp at h + wl0 _ h := by simp at h + wlo _ h := by simp at h + xal _ h := by simp at h + tal _ h := by simp at h + pgd q h := by simp [S2PgE] at h + t2x _ h := by simp at h + ypg _ h := by simp at h + ypp _ h := by simp at h + +/-- (S3 gen 18) With no root, window or Stage-2 table there is no footprint word. -/ +theorem extFoot_emptyE {b : U64} (h : ExtFootE ∅ ∅ ∅ b) : False := by + cases h <;> simp_all + +/-- (S3 gen 18) **A root or a Stage-2 table is no TCB and no CNode of the spec.** -/ +theorem ExtVsRelA.not_tcb (E : ExtVsRelA Pp Tb X Y T2 Sh2 mem σ) {q : U64} (hq : q ∈ X ∨ q ∈ T2) + (x : Fl4SpecA64.Tcb) : σ.kheap (specPtr q) ≠ some (.tcb x) := by + rcases hq with hq | hq + · obtain ⟨es, hk, -⟩ := E.s2root q hq; rw [hk]; exact fun h => by cases h + · obtain ⟨pt, hk, -⟩ := E.s2pt q hq; rw [hk]; exact fun h => by cases h + +theorem ExtVsRelA.not_cnode (E : ExtVsRelA Pp Tb X Y T2 Sh2 mem σ) {q : U64} (hq : q ∈ X ∨ q ∈ T2) + (r : Nat) (cs : Fl4SpecA64.CnodeContents) : σ.kheap (specPtr q) ≠ some (.cnode r cs) := by + rcases hq with hq | hq + · obtain ⟨es, hk, -⟩ := E.s2root q hq; rw [hk]; exact fun h => by cases h + · obtain ⟨pt, hk, -⟩ := E.s2pt q hq; rw [hk]; exact fun h => by cases h + +end rel2 + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/ExtVsHitA.lean b/proofs/Fl4ProofsA64/P3/ExtVsHitA.lean new file mode 100644 index 000000000..b0e67783d --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ExtVsHitA.lean @@ -0,0 +1,167 @@ +/- +P3 (aarch64, A64-P3 ExtVsA gen 2): **after an Ext unmap's hit the frame is no longer mapped** — `extMappedEntry` +(FL4's `mapped_entry` in a Stage-2 VSpace or an `ArmHighVSpace`) is `none` in the state `extUnmapPage` leaves. +Spec-only (`Fl4SpecA64/Ext/Stage2`), at `Z = Unit`; the plug-in of the handoff's "after the hit +`extMappedEntry` is `none`" step (ExtVsSpec gen 1 next step 4, ExtVsA gen 1 next step 4). + +* `slot_injX`: two aligned slots of the same normal `PageTable` with the same `pt_index` are the same slot + (l4v `table_base` / `table_index` determine an aligned PTE pointer). +* **`read_write_neX`**: storing at a real slot (`ExtSlotOkX`) leaves every *other* slot's entry unchanged — the + slot-level frame of `extSlotWriteX` (ExtVsSpec gen 1 had only the object-level `read_write_otherX` and the + same-root `read_write_root_neX`). +* **`extWalkN_clearX`**: FL4's walk (`extWalkN`) is unchanged by a state that changes only the slot the walk stops at, + if that slot held no table descriptor and holds none afterwards. No acyclicity invariant is needed: a slot the + walk passes through holds a table descriptor, so it is not the stopping slot, which holds a page descriptor. +* **`extMappedEntry_clearX`**: clearing the slot `extMappedEntry` found leaves `extMappedEntry` `none` (same root, + frame, size, address); **`extUnmapPage_unmappedX`**: in the state `extUnmapPage` leaves (`extUnmapPageStX`, hit or + miss), the frame is not mapped at the address in the ASID's root of that kind. + +No named premise ([J] / [P] / [W]: none). New module; no `Fl4SpecA64` edit, no `VsRelA` / `JAtZA` / `JEntA` edit. +-/ +import Fl4ProofsA64.P3.ExtVsRelA + +namespace Fl4ProofsA64.P3 + +noncomputable section +open Classical + +/-- **Aligned slots of one normal table are determined by their index** (module docstring). -/ +theorem slot_injX {p q : Fl4SpecA64.ObjRef} (hp : Fl4SpecA64.isAligned p Fl4SpecA64.pteBits) + (hq : Fl4SpecA64.isAligned q Fl4SpecA64.pteBits) + (hb : Fl4SpecA64.tableBase .normalPtT q = Fl4SpecA64.tableBase .normalPtT p) + (hi : (Fl4SpecA64.ucast Fl4SpecA64.ptIndexLen (Fl4SpecA64.tableIndex .normalPtT q) : Fl4SpecA64.PtIndex) = + Fl4SpecA64.ucast Fl4SpecA64.ptIndexLen (Fl4SpecA64.tableIndex .normalPtT p)) : q = p := by + have e1 : (q &&& ~~~(Fl4SpecA64.mask (Fl4SpecA64.ptBits .normalPtT) : BitVec 64)) = + p &&& ~~~(Fl4SpecA64.mask (Fl4SpecA64.ptBits .normalPtT) : BitVec 64) := hb + rw [ptBits12] at e1 + have e1' := congrArg BitVec.toNat e1 + rw [BitVec.toNat_and, BitVec.toNat_and, notMask12_toNatA, + show (18446744073709547520 : Nat) = 2 ^ 64 - 1 - (2 ^ 12 - 1) from rfl, + nat_and_hiA _ 12 q.isLt (by omega), nat_and_hiA _ 12 p.isLt (by omega)] at e1' + have e2 : (BitVec.setWidth 9 ((q &&& (Fl4SpecA64.mask (Fl4SpecA64.ptBits .normalPtT) : BitVec 64)) >>> + Fl4SpecA64.pteBits)).toNat = (BitVec.setWidth 9 ((p &&& (Fl4SpecA64.mask (Fl4SpecA64.ptBits .normalPtT) : + BitVec 64)) >>> Fl4SpecA64.pteBits)).toNat := congrArg BitVec.toNat hi + rw [ptBits12, pteBits3, BitVec.toNat_setWidth, BitVec.toNat_setWidth, BitVec.toNat_ushiftRight, + BitVec.toNat_ushiftRight, BitVec.toNat_and, BitVec.toNat_and, mask12_toNatA, + show (4095 : Nat) = 2 ^ 12 - 1 from rfl, Nat.and_two_pow_sub_one_eq_mod, Nat.and_two_pow_sub_one_eq_mod, + Nat.shiftRight_eq_div_pow, Nat.shiftRight_eq_div_pow] at e2 + unfold Fl4SpecA64.isAligned at hp hq + rw [pteBits3] at hp hq + obtain ⟨a, ha⟩ := hp + obtain ⟨b, hb2⟩ := hq + apply BitVec.eq_of_toNat_eq + simp only [Nat.reducePow] at e1' e2 ha hb2 + omega + +/-- **Storing at a real slot leaves every other slot's entry unchanged** (module docstring). -/ +theorem read_write_neX {σ : Fl4SpecA64.State Unit} {sl sl' : Fl4SpecA64.ExtSlot} (h : ExtSlotOkX σ sl) + (hne : sl' ≠ sl) (pte : Fl4SpecA64.PTE) : + Fl4SpecA64.ExtSlot.read (extSlotWriteX σ sl pte) sl' = Fl4SpecA64.ExtSlot.read σ sl' := by + by_cases ho : extSlotObjX sl' = extSlotObjX sl + swap + · exact read_write_otherX ho + cases sl with + | root r i => + obtain ⟨o, hro⟩ := h + cases sl' with + | root r' j => + have e : r' = r := ho + subst e + exact read_write_root_neX hro (fun e => hne (by rw [e])) pte + | pt q => + have e : Fl4SpecA64.tableBase .normalPtT q = r := ho + have hk := extObjAt_someX.1 hro + have hk' : (extSlotWriteX σ (.root r i) pte).kheap r = some (.archObj (.ext (o.setEntry i pte))) := by + show extSlotKhX σ (.root r i) pte r = _ + simp [extSlotKhX, hro] + rw [read_pt_invX (fun ⟨f, hf⟩ => by rw [e, hk'] at hf; cases hf), + read_pt_invX (fun ⟨f, hf⟩ => by rw [e, hk] at hf; cases hf)] + | pt p => + obtain ⟨ha, f, hk⟩ := h + cases sl' with + | root r' j => + have e : r' = Fl4SpecA64.tableBase .normalPtT p := ho + subst e + have n1 : Fl4SpecA64.extObjAt σ (Fl4SpecA64.tableBase .normalPtT p) = none := by + unfold Fl4SpecA64.extObjAt; rw [hk] <;> rfl + have n2 : Fl4SpecA64.extObjAt (extSlotWriteX σ (.pt p) pte) (Fl4SpecA64.tableBase .normalPtT p) = none := by + unfold Fl4SpecA64.extObjAt; rw [kheap_write_ptX hk pte] <;> rfl + rw [read_root_eqX, read_root_eqX, n1, n2] + | pt q => + have hb : Fl4SpecA64.tableBase .normalPtT q = Fl4SpecA64.tableBase .normalPtT p := ho + rw [read_write_ptX hk hb, read_pt_eqX (by rw [hb]; exact hk)] + by_cases hq : Fl4SpecA64.isAligned q Fl4SpecA64.pteBits + · rw [if_pos hq, if_pos hq] + exact Function.update_of_ne (fun e => hne (by rw [slot_injX ha hq hb e])) _ _ + · rw [if_neg hq, if_neg hq] + +/-- A page descriptor is no table descriptor. -/ +theorem pg_notTabX {e : Fl4SpecA64.PTE} (h : e.isPagePTE = true) : e.isPageTablePTE = false := by + cases e <;> simp_all [Fl4SpecA64.PTE.isPagePTE, Fl4SpecA64.PTE.isPageTablePTE] + +/-- **The walk survives a change at its stopping slot** (module docstring). -/ +theorem extWalkN_clearX {σ σ' : Fl4SpecA64.State Unit} (va : Fl4SpecA64.Word) (n : Nat) (p : Fl4SpecA64.ExtPos) + (hW : (Fl4SpecA64.ExtSlot.read σ (Fl4SpecA64.extWalkN σ va n p).slot).isPageTablePTE = false) + (hr : ∀ sl, sl ≠ (Fl4SpecA64.extWalkN σ va n p).slot → + Fl4SpecA64.ExtSlot.read σ' sl = Fl4SpecA64.ExtSlot.read σ sl) + (hc : Fl4SpecA64.extIsTable (Fl4SpecA64.ExtSlot.read σ' (Fl4SpecA64.extWalkN σ va n p).slot) + (Fl4SpecA64.extWalkN σ va n p).shift = false) : + Fl4SpecA64.extWalkN σ' va n p = Fl4SpecA64.extWalkN σ va n p := by + induction n generalizing p with + | zero => rfl + | succ n ih => + by_cases h : Fl4SpecA64.extIsTable (Fl4SpecA64.ExtSlot.read σ p.slot) p.shift = true + · have hW' : Fl4SpecA64.extWalkN σ va (n + 1) p = Fl4SpecA64.extWalkN σ va n + ⟨Fl4SpecA64.extNextSlot va (Fl4SpecA64.ExtSlot.read σ p.slot) p.shift, p.shift - 9⟩ := by + rw [extWalkN_succX, if_pos h] + rw [hW'] at hW hr hc + have hne : p.slot ≠ (Fl4SpecA64.extWalkN σ va n + ⟨Fl4SpecA64.extNextSlot va (Fl4SpecA64.ExtSlot.read σ p.slot) p.shift, p.shift - 9⟩).slot := fun e => by + rw [← e, (extIsTable_iffX.1 h).2] at hW + cases hW + rw [extWalkN_succX σ' va n p, hr p.slot hne, if_pos h, hW'] + exact ih _ hW hr hc + · have hW' : Fl4SpecA64.extWalkN σ va (n + 1) p = p := by rw [extWalkN_succX, if_neg h] + rw [hW'] at hc + rw [extWalkN_succX σ' va n p, if_neg (by rw [hc]; exact Bool.false_ne_true), hW'] + +/-- **Clearing the slot `mapped_entry` found unmaps the frame there** (module docstring). -/ +theorem extMappedEntry_clearX {σ : Fl4SpecA64.State Unit} {k : Fl4SpecA64.ExtRootKind} + {r p : Fl4SpecA64.ObjRef} {sz : Fl4SpecA64.VMPageSize} {va : Fl4SpecA64.Word} {pos : Fl4SpecA64.ExtPos} + {pte : Fl4SpecA64.PTE} (h : Fl4SpecA64.extMappedEntry σ k r p sz va = some (pos, pte)) : + Fl4SpecA64.extMappedEntry (extSlotWriteX σ pos.slot .invalidPTE) k r p sz va = none := by + obtain ⟨hpos, hpte, -, hpg, -⟩ := extMappedEntry_someX h + subst hpos hpte + have hok : ExtSlotOkX σ (Fl4SpecA64.extLookupSlot σ k.geom r va).slot := + okOfReadX fun e => by rw [e] at hpg; cases hpg + have hinv := read_write_invalidX hok + have hwalk : Fl4SpecA64.extLookupSlot (extSlotWriteX σ (Fl4SpecA64.extLookupSlot σ k.geom r va).slot .invalidPTE) + k.geom r va = Fl4SpecA64.extLookupSlot σ k.geom r va := + extWalkN_clearX va _ _ (pg_notTabX hpg) (fun sl hsl => read_write_neX hok hsl _) + (by + show Fl4SpecA64.extIsTable (Fl4SpecA64.ExtSlot.read _ (Fl4SpecA64.extLookupSlot σ k.geom r va).slot) _ = false + rw [hinv] + simp [Fl4SpecA64.extIsTable, Fl4SpecA64.PTE.isPageTablePTE]) + apply extMappedEntry_noneX + rw [hwalk, hinv] + simp [Fl4SpecA64.PTE.isPagePTE] + +/-- **After `extUnmapPage` the frame is not mapped at the address** in the ASID's root of that kind (hit: the +cleared slot, `extMappedEntry_clearX`; miss: nothing changed and nothing was mapped), at an address the unmap +looks at (`extVaOk`: in a high window, `HIGH_L0_INDEX`; ExtVsA gen 3). -/ +theorem extUnmapPage_unmappedX {σ : Fl4SpecA64.State Unit} {k : Fl4SpecA64.ExtRootKind} {sz : Fl4SpecA64.VMPageSize} + {asid : Fl4SpecA64.Asid} {va : Fl4SpecA64.VSpaceRef} {p r : Fl4SpecA64.ObjRef} (hg : Fl4SpecA64.extVaOk k va) + (hr : Fl4SpecA64.vspaceForAsid asid σ = some r) (hk : Fl4SpecA64.rootKindAt σ r = k) : + Fl4SpecA64.extMappedEntry (extUnmapPageStX σ k sz asid va p) k r p sz va = none := by + cases hm : Fl4SpecA64.extMappedEntry σ k r p sz va with + | none => + rw [extUnmapPageStX_missX (fun _ r' hr' _ => by rw [hr] at hr'; cases hr'; exact hm)] + exact hm + | some x => + obtain ⟨pos, pte⟩ := x + rw [extUnmapPageStX_hitX hg hr hk hm] + exact extMappedEntry_clearX hm + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/ExtVsPerfA.lean b/proofs/Fl4ProofsA64/P3/ExtVsPerfA.lean new file mode 100644 index 000000000..a3d0b912d --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ExtVsPerfA.lean @@ -0,0 +1,241 @@ +/- +P3 (aarch64, A64-P3 S3 gen 24, ruling 21:50Z item 1): **the `performExtVSpace` unmap tops** — FL4's `decode_frame` +at `ARMPageUnmap` on a mapped frame cap and `decode_page_table` at `ARMPageTableUnmap` on a final mapped table cap +(`vspace.rs`: the caller's restart, the unmap, then `set_cap` of the unmapped cap) against the spec's +`performExtVSpace` `.pageUnmap` / `.tableUnmap` (`Ext/Stage2`). The x86 counterparts are `P3/EptFrameUnmap` +(`performFrameUnmap_eq`, `EptCore.frameUnmapF`) and `P3/EptTableUnmapDec`. + +* **Spec runs**: `perform_pgUnmapS2A` (a `stage2FrameCap`: `extUnmapPage .stage2` whatever `k`), `perform_pgUnmapFrA` + (a plain frame cap: `extUnmapPage .high` when `k = .high`, else nothing), `perform_tabUnmapA` (`extUnmapTable k`, then + the table's zeroing `mapMx storePte` over its slots, then the cap): each is the cap write (and for a table the + zeroing) run at the state the Ext unmap leaves (`extUnmapPageStX` / `extUnmapTableStX`, `P3/ExtVsSpecA`). +* **FL4's arms**: `page_unmap_eqA` (a mapped frame with ASID ≠ 0: restart, `is_ept ? ept::unmap : unmap_page`, + `set_cap` of `unmapped_frame`) and `pt_unmap_eqA` (a final mapped table: restart, `unmap_page_table`, `zero(ptr, + 4096)`, `set_cap` of the unmapped table cap). +* **`perform_pgUnmapXA`** (the frame top): under `VsRelA` / `ExtVsRelA` at the restart's state, FL4's unmap of the + frame and the spec's `performExtVSpace (.pageUnmap k …)` meet at the state `σu` the spec's unmap leaves: the spec + run is the cap write at `σu`, and FL4's unmap and `σu` are `ExtUnmapRelE`-related (nothing on both sides, or one + zero write with both VSpace relations at `σu`), FL4's continuation an arch step. `k` is the decoder's + (`extKindOfAsid`: the ASID names an FL4 root) for a plain frame; a Stage-2 frame cap ignores it. +* **`perform_tabUnmapXA`** (the table top's unmap half): FL4's `unmap_page_table` and the spec's `extUnmapTable k` + (`k` an FL4 root kind the ASID names) meet in the same way, and the spec run is the zeroing and the cap write at that + state. + +(S3 gen 25, ruling Q2) `perform_tabUnmapZA` pins the zeroing's slot list; **changed statement (sanctioned by +Q2): `perform_tabUnmapXA`'s `∃ zs` is replaced by that list** (`perform_tabUnmapA` unchanged, now its corollary). The +zeroing's correspondence and the `JAtZAX` tops are `P3/TabZeroA` / `P3/UnmapTopJA`. + +(S3 gen 26, ruling Q4) **Changed statements (forced by Q4)**: `perform_pgUnmapXA` / `perform_tabUnmapXA` expose FL4's +continuation as a flush of the entry's arch state (`ArchFlE ar a fk`, was `ArchContE fk`), so `P3/UnmapTopJA`'s tops +relate the returned arch state. + +Named premises: none new (`S2IpaE` [P] of `P3/ExtVsTopA`). Lemma hypotheses as `unmap_frameXE` (`hK`, `hT`, `hX`, +`hVa`). No existing statement changed. +-/ +import Fl4ProofsA64.P3.FinVsA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4Spec + +noncomputable section + +/-! ## Spec runs -/ + +section spec +variable {σ : Fl4SpecA64.State Unit} {asid : Fl4SpecA64.Asid} {va : Fl4SpecA64.VSpaceRef} {p : Fl4SpecA64.ObjRef} + {R : Fl4SpecA64.CapRights} {sz : Fl4SpecA64.VMPageSize} {dev : Bool} {slot : Fl4SpecA64.CslotPtr} + +/-- **`ARMPageUnmap` of a Stage-2 frame cap** (module docstring). -/ +theorem perform_pgUnmapS2A (k : Fl4SpecA64.ExtRootKind) : + Fl4SpecA64.performExtVSpace (Z := Unit) + (.pageUnmap k (.ext (.stage2FrameCap p R sz dev (some (asid, va)))) slot) σ = + (do Fl4SpecA64.setCap (Z := Unit) + (.archObjectCap (Fl4SpecA64.extUnmappedFrame (.ext (.stage2FrameCap p R sz dev (some (asid, va)))))) slot + return ([] : List Fl4SpecA64.Data)) (extUnmapPageStX σ .stage2 sz asid va p) := by + unfold Fl4SpecA64.performExtVSpace + dsimp only + rw [nd_bind_oneD (extUnmapPage_runX .stage2 sz asid va p σ)] + +/-- **`ARMPageUnmap` of a plain frame cap in an FL4 root of kind `k`** (module docstring). -/ +theorem perform_pgUnmapFrA (k : Fl4SpecA64.ExtRootKind) : + Fl4SpecA64.performExtVSpace (Z := Unit) (.pageUnmap k (.frameCap p R sz dev (some (asid, va))) slot) σ = + (do Fl4SpecA64.setCap (Z := Unit) + (.archObjectCap (Fl4SpecA64.extUnmappedFrame (.frameCap p R sz dev (some (asid, va))))) slot + return ([] : List Fl4SpecA64.Data)) + (if k = Fl4SpecA64.ExtRootKind.high then extUnmapPageStX σ .high sz asid va p else σ) := by + unfold Fl4SpecA64.performExtVSpace + dsimp only + by_cases hk : k = .high + · subst hk + rw [if_pos rfl] + exact nd_bind_oneD (extUnmapPage_runX .high sz asid va p σ) _ + · rw [if_neg hk] + exact nd_bind_oneD (m := NonDet.when (k = .high) _) (by unfold NonDet.when; rw [if_neg hk]; rfl) _ + +/-- (S3 gen 25) **`ARMPageTableUnmap` of a mapped table cap in an FL4 root of kind `k`, the slot list pinned** +(module docstring): the zeroing over the table's slots `uptoEnumStep p (p + 2^pteBits) (p + mask (ptBits t))` and the +cap write, at the state `extUnmapTable` leaves. -/ +theorem perform_tabUnmapZA (k : Fl4SpecA64.ExtRootKind) (t : Fl4SpecA64.PtType) : + Fl4SpecA64.performExtVSpace (Z := Unit) (.tableUnmap k (.pageTableCap p t (some (asid, va))) slot) σ = + (NonDet.mapMx (fun sl => Fl4SpecA64.storePte (Z := Unit) t sl .invalidPTE) + (Fl4SpecA64.uptoEnumStep p (p + ((1 : Fl4SpecA64.Word) <<< Fl4SpecA64.pteBits)) + (p + Fl4SpecA64.mask (Fl4SpecA64.ptBits t))) >>= fun _ => do + Fl4SpecA64.setCap (Z := Unit) + (.archObjectCap (Fl4SpecA64.updateMapData (.pageTableCap p t (some (asid, va))) none)) slot + return ([] : List Fl4SpecA64.Data)) (extUnmapTableStX σ k asid va p) := by + unfold Fl4SpecA64.performExtVSpace + dsimp only + rw [nd_bind_oneD (extUnmapTable_runX k asid va p σ)] + all_goals rfl + +/-- **`ARMPageTableUnmap` of a mapped table cap in an FL4 root of kind `k`** (module docstring): the zeroing over the +table's slots `zs` and the cap write, at the state `extUnmapTable` leaves. (Gen 25: `perform_tabUnmapZA` pins `zs`.) -/ +theorem perform_tabUnmapA (k : Fl4SpecA64.ExtRootKind) (t : Fl4SpecA64.PtType) : + ∃ zs : List Fl4SpecA64.ObjRef, + Fl4SpecA64.performExtVSpace (Z := Unit) (.tableUnmap k (.pageTableCap p t (some (asid, va))) slot) σ = + (NonDet.mapMx (fun sl => Fl4SpecA64.storePte (Z := Unit) t sl .invalidPTE) zs >>= fun _ => do + Fl4SpecA64.setCap (Z := Unit) + (.archObjectCap (Fl4SpecA64.updateMapData (.pageTableCap p t (some (asid, va))) none)) slot + return ([] : List Fl4SpecA64.Data)) (extUnmapTableStX σ k asid va p) := + ⟨_, perform_tabUnmapZA k t⟩ + +end spec + +/-- Two single-outcome runs with the same value: their states agree. -/ +theorem sing_stA {α σ : Type} {c : α} {x y : σ} (h : ({(c, x)}, False) = (({(c, y)}, False) : Set (α × σ) × Prop)) : + x = y := by + have e := congrArg Prod.fst h + simp only [Set.singleton_eq_singleton_iff, Prod.mk.injEq, true_and] at e + exact e + +/-! ## FL4's arms -/ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **FL4's `ARMPageUnmap` of a mapped frame with ASID ≠ 0** (module docstring). -/ +theorem page_unmap_eqA {A : Type} (ar : machine.Arch A) (s : S) (a : A) (length : U64) (slot : obj.Slot) + (fp rt asid va : U64) (sz : capability.FrameSize) (dev isEpt call : Bool) (buf : U64) + (ex : Aeneas.Std.Array obj.Slot 3#usize) (h0 : asid ≠ 0#u64) : + vspace.decode_frame st ar s a fl4_abi.label.ARMPageUnmap length slot (.Frame fp sz rt dev true isEpt asid va) + call buf ex = + (do let s1 ← sched.set_thread_state st s (L.curW s) sched.TS_RESTART + let (s2, a1) ← (if isEpt then (do let i ← capability.FrameSize.bits sz; ept.unmap st ar s1 a asid va fp i false) + else vspace.unmap_page st ar s1 a sz asid va fp) + let s3 ← obj.Slot.set_cap st slot s2 (.Frame fp sz rt dev false false 0#u64 0#u64) + ok (core.result.Result.Ok (), s3, a1)) := by + have hct : sched.cur_thread st s = ok (L.curW s) := cur_thread_ok s + have hne : ¬ fl4_abi.label.ARMPageUnmap = fl4_abi.label.ARMPageMap := by + unfold fl4_abi.label.ARMPageUnmap fl4_abi.label.ARMPageMap; decide + have hb : (asid != 0#u64) = true := by simp [h0] + unfold vspace.decode_frame + simp only [vspace.as_frame, hct, bind_ok, hne, hb, eq_self_iff_true, if_true, ↓reduceIte, vspace.unmapped_frame] + +/-- **FL4's `ARMPageTableUnmap` of a final mapped table cap** (module docstring). -/ +theorem pt_unmap_eqA {A : Type} (ar : machine.Arch A) (s : S) (a : A) (length : U64) (slot : obj.Slot) + (ptr asid va buf : U64) (ex : Aeneas.Std.Array obj.Slot 3#usize) + (hf : cdt.is_final_capability st s slot = ok true) : + vspace.decode_page_table st ar s a fl4_abi.label.ARMPageTableUnmap length slot (.PageTable ptr true asid va) buf ex = + (do let s1 ← sched.set_thread_state st s (L.curW s) sched.TS_RESTART + let (s2, a1) ← (do + let (s3, a2) ← vspace.unmap_page_table st ar s1 a asid va ptr + let s4 ← st.zero s3 ptr 4096#u64 + ok (s4, a2)) + let s3 ← obj.Slot.set_cap st slot s2 (.PageTable ptr false 0#u64 0#u64) + ok (core.result.Result.Ok (), s3, a1)) := by + have hct : sched.cur_thread st s = ok (L.curW s) := cur_thread_ok s + unfold vspace.decode_page_table + simp only [hct, hf, bind_ok, ↓reduceIte] + +/-! ## The tops -/ + +variable {Pp Tb : Set U64} {Lv : U64 → Nat} {X Y T2 : Set U64} {Sh2 : U64 → Nat} {σ : Fl4SpecA64.State Unit} + +set_option maxHeartbeats 2000000 in +/-- **The `ARMPageUnmap` top** (module docstring). -/ +theorem perform_pgUnmapXA {A : Type} {ar : machine.Arch A} {s : S} {a : A} + (R : VsRelA Pp Tb Lv X Y (L.mem s) (L.ksb s) σ) (E : ExtVsRelA Pp Tb X Y T2 Sh2 (L.mem s) σ) + (hK : Aligned (L.ksb s)) (hT : OffAsE Pp Tb X Y T2 (L.ksb s)) + (hX : ∀ r ∈ X, ∃ es, σ.kheap (specPtr r) = some (.archObj (.ext (.stage2Root es)))) + (hIpa : S2IpaE ar a) {asid : U64} (h16 : asid.val < 65536) (sz : capability.FrameSize) (va pptr : U64) + (isEpt : Bool) (hVa : isEpt = false → VaOkE va) (Rt : Fl4SpecA64.CapRights) (dev : Bool) + (k : Fl4SpecA64.ExtRootKind) + (hk : isEpt = false → Fl4SpecA64.asidRootKind σ (BitVec.ofNat 16 asid.val) = some k ∧ k ≠ .stage1) + (slot : Fl4SpecA64.CslotPtr) : + ∃ σu fk, ArchFlE ar a fk ∧ + Fl4SpecA64.performExtVSpace (Z := Unit) (.pageUnmap k (absFrameE isEpt (specPtr pptr) Rt (frameSizeOfA sz) dev + (some (BitVec.ofNat 16 asid.val, va.bv))) slot) σ = + (do Fl4SpecA64.setCap (Z := Unit) (.archObjectCap (Fl4SpecA64.extUnmappedFrame (absFrameE isEpt (specPtr pptr) + Rt (frameSizeOfA sz) dev (some (BitVec.ofNat 16 asid.val, va.bv))))) slot + return ([] : List Fl4SpecA64.Data)) σu ∧ + ExtUnmapRelE Pp Tb Lv X Y T2 Sh2 σ st s a fk + (if isEpt then (do let i ← capability.FrameSize.bits sz; ept.unmap st ar s a asid va pptr i false) + else vspace.unmap_page st ar s a sz asid va pptr) ({((), σu)}, False) := by + rcases unmap_frameXGE R E hK hT hX hIpa h16 sz va pptr isEpt hVa Rt dev true with + ⟨he, hn, -, -⟩ | ⟨σ', fk, hfk, hF, hU⟩ + · obtain ⟨hs, hk1⟩ := hk he + rcases hn with h | h <;> rw [h] at hs + · cases hs + · exact (hk1 (Option.some.inj hs).symm).elim + refine ⟨σ', fk, hfk, ?_, hU⟩ + cases isEpt with + | true => + rw [show absFrameE true (specPtr pptr) Rt (frameSizeOfA sz) dev (some (BitVec.ofNat 16 asid.val, va.bv)) = + .ext (.stage2FrameCap (specPtr pptr) Rt (frameSizeOfA sz) dev (some (BitVec.ofNat 16 asid.val, va.bv))) from rfl, + perform_pgUnmapS2A k] + rw [show absFrameE true (specPtr pptr) Rt (frameSizeOfA sz) dev (some (BitVec.ofNat 16 asid.val, va.bv)) = + .ext (.stage2FrameCap (specPtr pptr) Rt (frameSizeOfA sz) dev (some (BitVec.ofNat 16 asid.val, va.bv))) from rfl, + fl4FinaliseCap_s2frame_runX] at hF + rw [sing_stA hF] + | false => + obtain ⟨hs, hk1⟩ := hk rfl + rw [show absFrameE false (specPtr pptr) Rt (frameSizeOfA sz) dev (some (BitVec.ofNat 16 asid.val, va.bv)) = + .frameCap (specPtr pptr) Rt (frameSizeOfA sz) dev (some (BitVec.ofNat 16 asid.val, va.bv)) from rfl, + perform_pgUnmapFrA k] + rw [show absFrameE false (specPtr pptr) Rt (frameSizeOfA sz) dev (some (BitVec.ofNat 16 asid.val, va.bv)) = + .frameCap (specPtr pptr) Rt (frameSizeOfA sz) dev (some (BitVec.ofNat 16 asid.val, va.bv)) from rfl] at hF + cases k with + | stage1 => exact (hk1 rfl).elim + | stage2 => + rw [if_neg (by decide), sing_stA (hF.symm.trans (fl4FinaliseCap_frame_s2X hs))] + | high => + rw [if_pos rfl, sing_stA (hF.symm.trans (fl4FinaliseCap_frame_runX hs))] + +set_option maxHeartbeats 2000000 in +/-- **The `ARMPageTableUnmap` top's unmap half** (module docstring). -/ +theorem perform_tabUnmapXA {A : Type} {ar : machine.Arch A} {s : S} {a : A} + (R : VsRelA Pp Tb Lv X Y (L.mem s) (L.ksb s) σ) (E : ExtVsRelA Pp Tb X Y T2 Sh2 (L.mem s) σ) + (hK : Aligned (L.ksb s)) (hT : OffAsE Pp Tb X Y T2 (L.ksb s)) + (hX : ∀ r ∈ X, ∃ es, σ.kheap (specPtr r) = some (.archObj (.ext (.stage2Root es)))) + (hIpa : S2IpaE ar a) {asid : U64} (h16 : asid.val < 65536) (va pt : U64) (k : Fl4SpecA64.ExtRootKind) + (hk : Fl4SpecA64.asidRootKind σ (BitVec.ofNat 16 asid.val) = some k) (hk1 : k ≠ .stage1) + (slot : Fl4SpecA64.CslotPtr) : + ∃ σu fk, ArchFlE ar a fk ∧ + Fl4SpecA64.performExtVSpace (Z := Unit) + (.tableUnmap k (.pageTableCap (specPtr pt) .normalPtT (some (BitVec.ofNat 16 asid.val, va.bv))) slot) σ = + (NonDet.mapMx (fun sl => Fl4SpecA64.storePte (Z := Unit) .normalPtT sl .invalidPTE) + (Fl4SpecA64.uptoEnumStep (specPtr pt) (specPtr pt + ((1 : Fl4SpecA64.Word) <<< Fl4SpecA64.pteBits)) + (specPtr pt + Fl4SpecA64.mask (Fl4SpecA64.ptBits .normalPtT))) >>= fun _ => do + Fl4SpecA64.setCap (Z := Unit) (.archObjectCap (Fl4SpecA64.updateMapData + (.pageTableCap (specPtr pt) .normalPtT (some (BitVec.ofNat 16 asid.val, va.bv))) none)) slot + return ([] : List Fl4SpecA64.Data)) σu ∧ + ExtUnmapRelE Pp Tb Lv X Y T2 Sh2 σ st s a fk (vspace.unmap_page_table st ar s a asid va pt) ({((), σu)}, False) := by + rcases unmap_page_tableXGE R E hK hT hX hIpa h16 va pt with ⟨hn, -, -⟩ | ⟨k', fk, -, hfk, hF, hU⟩ + · rcases hn with h | h <;> rw [h] at hk + · cases hk + · exact (hk1 (Option.some.inj hk).symm).elim + have hz := perform_tabUnmapZA (σ := σ) (asid := BitVec.ofNat 16 asid.val) (va := va.bv) (p := specPtr pt) + (slot := slot) k .normalPtT + have e : extUnmapTableStX σ k' (BitVec.ofNat 16 asid.val) va.bv (specPtr pt) = + extUnmapTableStX σ k (BitVec.ofNat 16 asid.val) va.bv (specPtr pt) := + sing_stA (hF.symm.trans (fl4FinaliseCap_pt_runX hk hk1)) + refine ⟨_, fk, hfk, hz, ?_⟩ + rw [extUnmapTable_runX, e] at hU + exact hU + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/ExtVsRelA.lean b/proofs/Fl4ProofsA64/P3/ExtVsRelA.lean new file mode 100644 index 000000000..19e73e616 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ExtVsRelA.lean @@ -0,0 +1,227 @@ +/- +P3 (aarch64, A64-P3 ExtVsA gen 1): **the Stage-2 / `ArmHighVSpace` relation `ExtVsRelA`** — FL4's Stage-2 roots and +Stage-2 page tables (`ept.rs`, object type 13 and the `Cap::PageTable`s mapped below it) and the high windows' +level-0 entry (`vspace.rs` `init_high_vspace`) against `Fl4SpecA64/Ext/Stage2`'s `stage2Root` objects and normal +`PageTable`s, under `specPtr`. A sibling record of `VsRelA` (which already relates the windows' level-1 tables and +the Stage-1 tables below them); it is stated over `VsRelA`'s ghosts `Pp` / `Tb` / `Y` and the joint relation's `X`, +plus two new ghosts: the Stage-2 tables `T2` and the shift `Sh2 t` of the addresses an entry of `t` maps. +x86 counterpart: `P3/EptRel` (`EptRel`, the EPT tables) and the EPT half of `VmEptState`. + +* **`DescRelE G sh w pte`** — a descriptor word `w` at a slot of shift `sh` against the spec's `pte`, for any table + vocabulary `G t sh'` ("`t` is a table whose entries map `2^sh'` bytes"): invalid ↔ bit 0 clear; `PageTablePTE` + ↔ a table descriptor (`e & 3 = 3`) above the page level whose address bits are `ppn`'s and name a `G` table one + level down (`table_of`); `PagePTE` ↔ a leaf (bit 0; bit 1 iff `sh = 12`) whose base is FL4's `page_base` / + `leaf_base` at `sh` (`baseShE`). It is the shift-indexed form of VS's `PteRelA` (`descE_of_pteRel`: a `PteRelA` + at depth `k ≥ 1` is a `DescRelE (G1E Tb Lv)` at shift `39 - 9k`), and the Stage-2 format's common part (`ept.rs` + `is_table` / `is_leaf` / `leaf_base` have the Stage-1 bit layout); `S2AttrE` adds a Stage-2 leaf's `S2AP` + rights, `XN` and `MemAttr`. +* **`GoodTabE G mem σ`** — every `G` table is page-aligned, a spec normal `PageTable` at `specPtr t`, and its 512 + entries are `DescRelE` at its shift: the walk's only requirement on the tables below a root. `VsRelA.goodE` + (Stage-1 tables under a window, `G1E`) and `ExtVsRelA.goodE` (Stage-2 tables, `G2E`). +* **`ExtVsRelA Pp Tb X Y T2 Sh2 mem σ`** — `s2root` (each root of `X` is the spec's `stage2Root es`, its first + `s2Geom.topMask + 1` entries `DescRelE` / `S2AttrE` at `s2Geom.topShift`), `s2pt` (each table of `T2` a spec + normal `PageTable` with `DescRelE` / `S2AttrE` entries at `Sh2 t`), `tsh` (`12 ≤ Sh2 t ≤ 39`), `wl0` (each window's + level-0 entry `HIGH_L0_INDEX = 511` is the table descriptor of its level-1 table `y + 4096`, as + `init_high_vspace` writes it), `wlo` (gen 3: its level-0 entries `0..255` are zero), `xal` / `tal` (roots 8 KiB-, tables 4 KiB-aligned: `bits::STAGE2_VSPACE = 13`), + and the page disjointness `pgd` / `t2x` / `ypg` (a Stage-2 page is no pool, Stage-1 table, window level-1 table or + window; a Stage-2 table is no page of a root; a window's level-0 page is no `VsRelA` page). +* **Footprint `ExtFootE X Y T2`** — the two pages of every root, every Stage-2 table's 512 words, every window's + level-0 entry `511` and (gen 3) entries `0..255`: all **non-slot** words of `FrameA`'s vocabulary (at integration one `FootNA.ext` constructor, beside + `FootNA.vs`). `ExtFootE.pgE`: each is a word `peAF q j` of a page-aligned page `q` of `S2PgE X T2` or a window. +* **Frame lemmas**: `ExtVsRelA.of_mem` (footprint words kept), **`ExtVsRelA.of_writesIn`** (a step whose `WritesIn` + set misses the footprint), `ExtVsRelA.of_spec` (the roots and Stage-2 tables kept), `ExtVsRelA.foot_alignedE`; + the cross frames **`VsRelA.of_s2WriteE`** (a write into a Stage-2 page keeps `VsRelA` on both sides) and + **`ExtVsRelA.of_vsWriteE`** (a write into a `VsRelA` page keeps `ExtVsRelA`). + +**How it joins `JAtZA` later**: `XSetsA` gains `T2` / `Sh2`; `JAtZA.ext : ExtVsRelA Z.Pp Z.S1 Z.X Z.Y Z.T2 Z.Sh2 +(L.mem s) σ` beside `JAtZA.vs`; `FootNA.ext (w) : ExtFootE Z.X Z.Y Z.T2 w → FootNA Z s w`, `SlotApA.ext` (the words lie +in no slot / TCB, as `SlotApA.vs`); `of_slot` / `of_spec` carry it by `ExtVsRelA.of_mem` / `of_spec` (no `SpecSlotA` +step touches a root or a page table). The fields `xal` / `tal` / `pgd` / `t2x` / `ypg` / `wl0` come from A64 P1's +`S2Ty` / `VsTy` / `HiTy` typing at that point. + +**S3 gen 18.** The definitions, the footprint and `of_mem` / `of_spec` moved to `P3/ExtVsDefA` (imported by +`P3/JointA`: **`JAtZA.ext`**, `FootNA.ext`, `SlotApA.ext`); new **`UnmapRelA.extE`**: `ExtVsRelA` at every l4v +outcome of a Stage-1 unmap (`unmap_pageA`, `unmap_page_tableA`, the Stage-1 branches of the combined tops), from +`SpecOkA`'s `KeepTbA` and the page disjointness `pgd` / `ypg`. + +No named premise (definitions and frame lemmas). No `Fl4SpecA64` edit. +-/ +import Fl4ProofsA64.P3.VsUnmapA +import Fl4ProofsA64.P3.ExtVsSpecA +import Fl4ProofsA64.P3.ExtVsDefA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-- **A Stage-1 descriptor below the root is a `DescRelE` at its depth's shift** (module docstring). -/ +theorem descE_of_pteRel {Tb : Set U64} {Lv : U64 → Nat} {k : Nat} {w : U64} {pte : Fl4SpecA64.PTE} (hk1 : 1 ≤ k) + (hk3 : k ≤ 3) (h : PteRelA Tb Lv k w pte) : DescRelE (G1E Tb Lv) (39 - 9 * k) w pte := by + cases pte with + | invalidPTE => exact h + | pageTablePTE ppn => + obtain ⟨hk, h0, h1, h44, hp, hT, hL⟩ := h + exact ⟨by omega, h0, h1, h44, hp, hT, by omega, by rw [hL]; omega⟩ + | pagePTE a sm at' rt => + obtain ⟨-, -, h0, h1, -, ha, h44, -⟩ := h + exact ⟨h0, h1.trans ⟨fun e => by omega, fun e => by omega⟩, ha, h44⟩ + +/-- Reachable Stage-1 tables below a window: a `VsReachA` table of `Tb` below the root level, at its depth's shift. -/ +abbrev G1ReachE (Pp Tb Y : Set U64) (Lv : U64 → Nat) (mem : U64 → U64) (ksb : U64) (t : U64) (sh : Nat) : Prop := + G1E Tb Lv t sh ∧ VsReachA Pp Tb Y mem ksb t + +/-- **`VsRelA`'s reachable Stage-1 tables below the root level are walkable** (module docstring). -/ +theorem VsRelA.goodE {Pp Tb : Set U64} {Lv : U64 → Nat} {X Y : Set U64} {mem : U64 → U64} {ksb : U64} + {σ : Fl4SpecA64.State Unit} (R : VsRelA Pp Tb Lv X Y mem ksb σ) : + GoodTabE (G1ReachE Pp Tb Y Lv mem ksb) mem σ := by + rintro t sh ⟨⟨ht, h1, rfl⟩, hreach⟩ + have h3 := R.lv t ht + obtain ⟨pt, hk, hty, he⟩ := R.pt t ht + refine ⟨by omega, by omega, R.al_mod ht, pt, hk, ?_, fun i hi => ?_⟩ + · rw [hty]; unfold ptTyA; rw [if_neg (by omega)] + · refine (descE_of_pteRel h1 h3 (he hreach i hi)).mono fun hb0 hb1 h12 hg => ⟨hg, ?_⟩ + obtain ⟨tag, sh0, htag, hpath⟩ := hreach + have hsh0 : sh0 = 39 - 9 * Lv t := by + rcases (R.path_props hpath).2 with ⟨-, -, hsh0⟩ | ⟨-, -, y, hy, -, hq⟩ + · exact hsh0 + · exact absurd (hq ▸ ht) (R.ytb y hy) + exact ⟨tag, sh0 - 9, htag, .child hpath (Or.inl ht) (by omega) hi ((and3_bitsA _).2 ⟨hb0, hb1⟩)⟩ + +/-- Words of two different page-aligned pages differ. -/ +theorem pgNeE {p q i j : U64} (hp : 4096 ∣ p.val) (hq : 4096 ∣ q.val) (hi : i.val < 512) (hj : j.val < 512) + (hne : p ≠ q) : peAF p i ≠ peAF q j := + fun e => hne (peAF_injA (Nat.mod_eq_zero_of_dvd hp) (Nat.mod_eq_zero_of_dvd hq) hi hj e).1 + + +section foot +variable {Pp Tb X Y T2 : Set U64} {Sh2 : U64 → Nat} {σ : Fl4SpecA64.State Unit} + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **The implementation-side frame** (module docstring): a step writing only `W` words, none in the footprint. -/ +theorem ExtVsRelA.of_writesIn {s s' : S} (E : ExtVsRelA Pp Tb X Y T2 Sh2 (L.mem s) σ) {W : U64 → Prop} + (hw : WritesIn st s s' W) (hW : ∀ b, W b → ¬ ExtFootE X Y T2 b) (hY : ∀ y ∈ Y, 8192 ∣ y.val) : + ExtVsRelA Pp Tb X Y T2 Sh2 (L.mem s') σ := + E.of_mem fun b hb => hw.2.2 b (E.foot_alignedE hY b hb) fun e => hW b e hb + +end + +/-! ## Cross frames -/ + +theorem s2Pg_ne_vsE {Lv : U64 → Nat} {mem : U64 → U64} {ksb : U64} (R : VsRelA Pp Tb Lv X Y mem ksb σ) + {q : U64} (hq : q ∉ VsPtA Pp Tb Y) (hqa : 4096 ∣ q.val) {j : U64} (hj : j.val < 512) : + ∀ b, VsFootA Pp Tb Y ksb b → ¬ AsW ksb (peAF q j) → b ≠ peAF q j := by + intro b h hA e + cases h with + | asid hi hh => exact hA (e ▸ atAF_asWA hh) + | pool p lo hp hl => exact pgNeE (R.pal p hp) hqa hl hj (fun e' => hq (e' ▸ Or.inl hp)) e + | pt p i hp hi => exact pgNeE (R.al p hp) hqa hi hj (fun e' => hq (e' ▸ Or.inr (Or.inl hp))) e + | high y i hy hi => + exact pgNeE (Nat.dvd_of_mod_eq_zero (waWin_modA (R.yal y hy))) hqa hi hj + (fun e' => hq (Or.inr (Or.inr ⟨y, hy, e'.symm⟩))) e + +/-- **A write into a Stage-2 page keeps `VsRelA`** (module docstring): FL4's word `peAF P j` of a Stage-2 page +changes (no ASID-table word, `hA`), and the spec changes one object of a Stage-2 page `q` (`extSlotWriteX`). -/ +theorem VsRelA.of_s2WriteE {Lv : U64 → Nat} {mem mem' : U64 → U64} {ksb : U64} + (R : VsRelA Pp Tb Lv X Y mem ksb σ) {mem0 : U64 → U64} (E : ExtVsRelA Pp Tb X Y T2 Sh2 mem0 σ) + {P j q : U64} (hP : P ∈ S2PgE X T2) (hPa : 4096 ∣ P.val) (hj : j.val < 512) (hA : ¬ AsW ksb (peAF P j)) + (hK : Aligned ksb) (hm : ∀ b, Aligned b → b ≠ peAF P j → mem' b = mem b) (hq : q ∈ S2PgE X T2) + {sl : Fl4SpecA64.ExtSlot} (hsl : extSlotObjX sl = specPtr q) (pte : Fl4SpecA64.PTE) : + VsRelA Pp Tb Lv X Y mem' ksb (extSlotWriteX σ sl pte) := by + have R1 := R.of_mem fun b hb => hm b (R.foot_alignedA hK b hb) (s2Pg_ne_vsE R (E.pgd P hP).1 hPa hj b hb hA) + refine R1.of_spec rfl fun p hp => kheap_writeX ?_ + rw [hsl] + obtain ⟨hq1, hq2⟩ := E.pgd q hq + rcases hp with ⟨q', hq', rfl⟩ | ⟨q', hq', rfl⟩ | ⟨q', hq', rfl⟩ <;> intro e <;> have e' := specPtr_inj e <;> + subst e' + · exact hq1 (Or.inl hq') + · exact hq1 (Or.inr (Or.inl hq')) + · exact hq2 hq' + +/-- **A write into a `VsRelA` page keeps `ExtVsRelA`** (module docstring): FL4's word `peAF P j` of a pool, a +Stage-1 table or a window's level-1 table changes, and the spec changes one object `specPtr q` of a Stage-1 table or +a window. -/ +theorem ExtVsRelA.of_vsWriteE {mem mem' : U64 → U64} (E : ExtVsRelA Pp Tb X Y T2 Sh2 mem σ) + (hY : ∀ y ∈ Y, 8192 ∣ y.val) {P j q : U64} (hP : P ∈ VsPtA Pp Tb Y) (hPa : 4096 ∣ P.val) (hj : j.val < 512) + (hm : ∀ b, Aligned b → b ≠ peAF P j → mem' b = mem b) (hq : q ∈ Tb ∨ q ∈ Y) (hqP : q ∉ Pp) + {sl : Fl4SpecA64.ExtSlot} (hsl : extSlotObjX sl = specPtr q) (pte : Fl4SpecA64.PTE) : + ExtVsRelA Pp Tb X Y T2 Sh2 mem' (extSlotWriteX σ sl pte) := by + have E1 := E.of_mem fun b hb => hm b (E.foot_alignedE hY b hb) (by + obtain ⟨q', j', hj', rfl, hq', hc⟩ := ExtFootE.pgE E hY hb + refine pgNeE hq' hPa hj' hj fun e => ?_ + subst e + rcases hc with h | h + · exact (E.pgd _ h).1 hP + · exact E.ypg _ h hP) + refine E1.of_spec rfl fun q' hq' => kheap_writeX ?_ + rw [hsl] + intro e + have e' := specPtr_inj e + subst e' + rcases hq' with hp | hq' + · exact hqP hp + · have h2 : q' ∈ S2PgE X T2 := by + rcases hq' with h | h + · exact Or.inr ⟨q', h, Or.inl rfl⟩ + · exact Or.inl h + rcases hq with h | h + · exact (E.pgd _ h2).1 (Or.inr (Or.inl h)) + · exact (E.pgd _ h2).2 h + +/-! ## (S3 gen 18) `ExtVsRelA` at l4v's Stage-1 unmap outcomes -/ + +section s1 +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- (S3 gen 18, ruling 18:13Z item 4) **`ExtVsRelA` across a Stage-1 unmap** (VS gen 4's `unmap_pageA` / +`unmap_page_tableA` and the combined tops' Stage-1 branches, all concluding `UnmapRelA`): FL4's zero write lands in +a Stage-1 table of `Tb`, a page no Stage-2 page or window shares (`pgd`, `ypg`), and l4v's outcomes change only +Stage-1 tables (`SpecOkA`'s `KeepTbA`), so every outcome is also `ExtVsRelA`-related to the written memory. -/ +theorem UnmapRelA.extE {A : Type} {Lv : U64 → Nat} {s : S} {a : A} {fl : Result A} {r : Result (S × A)} + {W : Set (Unit × Fl4SpecA64.State Unit) × Prop} + (R : VsRelA Pp Tb Lv X Y (L.mem s) (L.ksb s) σ) (E : ExtVsRelA Pp Tb X Y T2 Sh2 (L.mem s) σ) + (h : UnmapRelA Pp Tb Lv X Y σ st s a fl r W) : + (r = ok (s, a) ∧ W = ({((), σ)}, False)) ∨ + ∃ slot, (∃ p ∈ Tb, ∃ i : U64, i.val < 512 ∧ slot = peAF p i) ∧ + r = (do let a1 ← fl; ok (L.wr s slot 0#u64, a1)) ∧ + SpecOkA Pp Tb Lv X Y (L.mem (L.wr s slot 0#u64)) (L.ksb (L.wr s slot 0#u64)) σ W ∧ + ∀ x ∈ W.1, ExtVsRelA Pp Tb X Y T2 Sh2 (L.mem (L.wr s slot 0#u64)) x.2 := by + rcases h with h | ⟨slot, ⟨p, hp, i, hi, rfl⟩, hr, hS, hAsid⟩ + · exact Or.inl h + refine Or.inr ⟨_, ⟨p, hp, i, hi, rfl⟩, hr, hS, fun x hx => ?_⟩ + have hpa : 4096 ∣ p.val := Nat.dvd_of_mod_eq_zero (R.al_mod hp) + have hPV : p ∈ VsPtA Pp Tb Y := Or.inr (Or.inl hp) + have hsa := peAF_alignedA (R.al_mod hp) hi + have hnp : ∀ q, (q ∈ S2PgE X T2 ∨ q ∈ Y) → q ≠ p := by + rintro q (hq | hq) rfl + · exact (E.pgd _ hq).1 hPV + · exact E.ypg _ hq hPV + have E1 : ExtVsRelA Pp Tb X Y T2 Sh2 (L.mem (L.wr s (peAF p i) 0#u64)) σ := E.of_mem fun b hb => by + rw [L.mem_wr _ _ _ _ hsa (E.foot_alignedE R.yal b hb), if_neg ?_] + obtain ⟨q, j, hj, rfl, hq, hc⟩ := ExtFootE.pgE E R.yal hb + exact pgNeE hpa hq hi hj (fun e => hnp q hc e.symm) + refine E1.of_spec (hAsid x hx) fun q hq => (hS.2.2 x hx).2 _ fun p' hp' e => ?_ + have e' := specPtr_inj e + subst e' + rcases hq with hq | hq + · obtain ⟨f, h1, -⟩ := R.pool q hq + obtain ⟨t, h2, -⟩ := R.pt q hp' + rw [h1] at h2; cases h2 + · have hq' : q ∈ S2PgE X T2 := by + rcases hq with hq | hq + · exact Or.inr ⟨q, hq, Or.inl rfl⟩ + · exact Or.inl hq + exact (E.pgd _ hq').1 (Or.inr (Or.inl hp')) + +end s1 + +end foot + +end + +end Fl4ProofsA64.P3 \ No newline at end of file diff --git a/proofs/Fl4ProofsA64/P3/ExtVsSpecA.lean b/proofs/Fl4ProofsA64/P3/ExtVsSpecA.lean new file mode 100644 index 000000000..da8acee53 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ExtVsSpecA.lean @@ -0,0 +1,988 @@ +/- +P3 (aarch64, A64-P3 ExtVsSpec gen 1): **the spec-side toolkit for FL4's Stage-2 and `ArmHighVSpace` unmaps** — pure +reasoning about `Fl4SpecA64/Ext/Stage2` (`ExtGeom`, `extWalkN`, `extLookupSlot`, `extFindTableSlot`, +`storeExtSlot`, `findExtRoot`, `extUnmapPage`, `extUnmapTable`, `extMappedEntry`), for the later `P3/ExtVsA` +refinement of FL4's `ept::unmap` / `ept::unmap_table` and the `ROOT_HIGH` branches of `vspace::unmap_page` / +`unmap_page_table` (the Ext twin of the VS line's Stage-1 `VsWalkA` / `VsLeafA` / `VsUnmapA` spec steps; x86 +counterparts: the spec halves of `P3/EptUnmapRel`, `P3/EptUnmapCore` and `P3/EptFinUnmap`). + +This module imports only `Fl4SpecA64` and `Fl4ProofsA64.P3.DelSpecA` (for its monad steps `nd_bind_oneD`, +`run_bind_okD`, … and its `fl4FinaliseCap` dispatch lemmas); it has no `StateRelA` / `JAtZA` / `specPtr` / +`VsRelA` / `Fl4CoreA64` coupling. All statements are at `Z = Unit`. Generic helpers carry the suffix `X`. + +* **Geometry** (`ptTB9X`, `highGeom_eqX`, `s2Geom_casesX`, `geom_casesX`, `extTopPos_rootX`, + `geom_mask_le_sizeX`): the high window's walk is `⟨30, 511, 3⟩`; the Stage-2 walk (`s2Geom`, of the opaque + platform's IPA size) is one of `⟨30, 1023, 3⟩`, `⟨39, 31, 4⟩`, `⟨39, 511, 4⟩`; the top index is within the root + object's table. +* **Walk steps** (`extWalkN_zeroX` / `_succX`, `extFindTableN_zeroX` / `_succX`, `extLookupSlot_highX` — the + high window's two-step walk written out): the one-step unfoldings (the Ext analogue of VS gen 3's + `ptLookupFromLevelN_succA`); `extWalkN_casesX` / `extLookupSlot_casesX` (the walk stops at the top slot, or at a + `PageTable` slot of a smaller shift), `extWalkN_shiftX` / `extLookupSlot_shiftX` (the shift drops by 9 a level); + `extFindTableN_someX` / `extFindTableSlot_someX` (the slot found holds a table descriptor naming the table). +* **The store** (`extSlotKhX`, `extSlotWriteX`: the state `storeExtSlot` leaves; `ExtSlotOkX`: the slot is a real + slot — an FL4 root, or an aligned slot of a normal `PageTable`): `storeExtSlot_runX` (single outcome, no + failure), `okOfReadX` (a slot holding a valid entry is a real slot), `kheap_writeX` (only the one table object + changes; every other state field is unchanged by `extSlotWriteX`'s definition), the read-after-write lemmas + `read_write_rootX`, `read_write_ptX`, `read_write_invalidX`, `read_write_otherX`, and the entry lemmas + `entry_setEntry_selfX` / `_neX` / `_invX`. +* **`find_ext_root`** (`findAnyRoot_runX`, `findExtRoot_runX`, `extRootResX`): deterministic, no failure. +* **The unmaps** (the Ext analogue of `UnmapRelA`'s two cases): **`extUnmapPage_runX`** / + **`extUnmapTable_runX`** — never fail, a single outcome `extUnmapPageStX` / `extUnmapTableStX`, which is the + state unchanged (`_missX`: no such root, or no matching entry / table) or the one entry cleared + (`_hitX`: `extSlotWriteX σ sl .invalidPTE` at the slot `extMappedEntry` / `extFindTableSlot` finds). + `Ext/Stage2` models no TLB or cache operation for these unmaps (its module docstring: machine effects, not + modelled), so the only change is the one table write (`extUnmapPageStX_kheapX`). +* **`extMappedEntry`** (the Ext `lookup_frame` / `mapped_entry`): `extMappedEntry_someX` / `_of_condX` / + `_noneX`. +* **Dispatch** (`fl4FinaliseCap`): `fl4FinaliseCap_frame_runX` / `fl4FinaliseCap_pt_runX` (a mapped frame / + final mapped table cap whose ASID names an FL4 root: the single outcome `(nullCap, nullCap)` at the unmap's + state), and `fl4FinaliseCap_frame_splitX` / `fl4FinaliseCap_pt_splitX` (the three-way split: FL4 root → the + Ext unmap; l4v root or none → l4v's `unmap_page` / `unmap_page_table`). ExtVsA gen 3 (the spec's + `stage2FrameCap`): a plain frame cap goes to the Ext unmap only in a high window (`fl4FinaliseCap_frame_runX`), + to nothing at a Stage-2 root (`fl4FinaliseCap_frame_s2X`); a Stage-2 frame cap to `extUnmapPage .stage2` + (`fl4FinaliseCap_s2frame_runX`); `extPageSlotX` / `extUnmapPage_runX` carry the high-window guard `extVaOk`. + +No named premise ([J] / [P] / [W]): every statement is about the spec alone. New module. +-/ +import Fl4SpecA64 +import Fl4ProofsA64.P3.DelSpecA + +namespace Fl4ProofsA64.P3 +open Classical Fl4Spec Fl4SpecA64 + +noncomputable section + +/-! ## Generic monad helpers -/ + +section +variable {σ ε α β : Type} + +theorem assert_runX {P : Prop} (h : P) (s : σ) : NonDet.assert P s = ({((), s)}, False) := by + show ({x | P ∧ x = ((), s)}, ¬P) = _ + refine Prod.ext ?_ (propext ⟨fun hn => absurd h hn, False.elim⟩) + ext x + simp [h] + +theorem getsThe_runX {f : σ → Option α} {s : σ} {a : α} (h : f s = some a) : + NonDet.getsThe f s = ({(a, s)}, False) := by + show NonDet.assertOpt (f s) s = _ + rw [h] + rfl + +theorem run_bind_getsX (f : σ → α) (g : α → NonDetE σ ε β) (s : σ) : + (NonDetE.liftE (NonDet.gets f) >>= g).run s = (g (f s)).run s := + run_bind_okD (run_liftE_stD (m := NonDet.gets f) (s' := s) (a := f s) rfl) g + +theorem unlessE_pos_bindX {P : Prop} [Decidable P] (h : P) (m : NonDetE σ ε Unit) (g : Unit → NonDetE σ ε β) : + (NonDetE.unlessE P m >>= g) = g () := by + unfold NonDetE.unlessE + rw [if_pos h, ndE_pure_bindD] + +theorem unlessE_neg_runX {P : Prop} [Decidable P] (h : ¬ P) (e : ε) (g : Unit → NonDetE σ ε β) (s : σ) : + (NonDetE.unlessE P (NonDetE.throwError e) >>= g).run s = ({(.error e, s)}, False) := by + unfold NonDetE.unlessE + rw [if_neg h] + exact run_bind_errorD (run_throwErrorD e s) g + +theorem catch_okX {m : NonDetE σ ε α} {h : ε → NonDet σ α} {s s' : σ} {a : α} + (hm : m.run s = ({(.ok a, s')}, False)) : NonDetE.catch m h s = ({(a, s')}, False) := by + have e : NonDetE.catch m h s = (ExceptT.run m >>= fun + | .ok a => NonDet.pure a + | .error e => h e) s := rfl + rw [e, nd_bind_oneD hm] + rfl + +theorem catch_errX {m : NonDetE σ ε α} {h : ε → NonDet σ α} {s s' : σ} {e : ε} + (hm : m.run s = ({(.error e, s')}, False)) : NonDetE.catch m h s = h e s' := by + have e' : NonDetE.catch m h s = (ExceptT.run m >>= fun + | .ok a => NonDet.pure a + | .error e => h e) s := rfl + rw [e', nd_bind_oneD hm] + +theorem rbindX {ρ α β : Type} (x : Lookup ρ α) (f : α → Lookup ρ β) (r : ρ) : + (x >>= f) r = Option.bind (x r) (fun a => f a r) := rfl + +end + +/-! ## Geometry -/ + +theorem ptTB9X : ptTranslationBits .normalPtT = 9 := by + first + | rfl + | simp [ptTranslationBits] + +/-- The high window's walk (`Ext/ArmHighVSpace` `highGeom`): shifts 30, 21, 12 from a 512-entry level-1 table. -/ +theorem highGeom_eqX : ExtRootKind.high.geom = ⟨30, 511, 3⟩ := rfl + +theorem highGeom_eq'X : highGeom = ⟨30, 511, 3⟩ := rfl + +/-- FL4's Stage-2 walk on the (opaque) platform: a 3-level walk from a 1024-entry concatenated root (40-bit IPA), +or a 4-level walk from the first 32 or all 512 entries (44 / 48 bits). -/ +theorem s2Geom_casesX : s2Geom = ⟨30, 1023, 3⟩ ∨ s2Geom = ⟨39, 31, 4⟩ ∨ s2Geom = ⟨39, 511, 4⟩ := by + unfold s2Geom s2GeomOf + by_cases h1 : fl4A64Platform.s2IpaBits ≤ 42 + · rw [if_pos h1]; exact Or.inl rfl + · rw [if_neg h1] + by_cases h2 : fl4A64Platform.s2IpaBits = 44 + · rw [if_pos h2]; exact Or.inr (Or.inl rfl) + · rw [if_neg h2]; exact Or.inr (Or.inr rfl) + +theorem stage2Geom_eqX : ExtRootKind.stage2.geom = s2Geom := rfl + +/-- Every FL4 root kind's geometry. -/ +theorem geom_casesX (k : ExtRootKind) : + k.geom = ⟨30, 511, 3⟩ ∨ k.geom = ⟨30, 1023, 3⟩ ∨ k.geom = ⟨39, 31, 4⟩ ∨ k.geom = ⟨39, 511, 4⟩ := by + cases k with + | high => exact Or.inl rfl + | stage1 => exact Or.inr (s2Geom_casesX) + | stage2 => exact Or.inr (s2Geom_casesX) + +/-- The top position of a walk: entry `(va >> topShift) mod (topMask + 1)` of the root, at the top shift. -/ +theorem extTopPos_rootX (g : ExtGeom) (r : ObjRef) (va : Word) : + (extTopPos g r va).slot = .root r ((va.toNat >>> g.topShift) % (g.topMask + 1)) ∧ + (va.toNat >>> g.topShift) % (g.topMask + 1) < g.topMask + 1 ∧ (extTopPos g r va).shift = g.topShift := + ⟨rfl, Nat.mod_lt _ (Nat.succ_pos _), rfl⟩ + +/-- The number of entries of an FL4 root's top table (`ExtArchObj.entry`'s bound). -/ +def extObjSizeX : ExtArchObj PTE → Nat + | .stage2Root _ => 1024 + | .highVSpace _ => 512 + +/-- The FL4 object at `r` as a heap fact. -/ +theorem extObjAt_someX {σ : State Unit} {r : ObjRef} {o : ExtArchObj PTE} : + extObjAt σ r = some o ↔ σ.kheap r = some (.archObj (.ext o)) := by + unfold extObjAt + cases h : σ.kheap r with + | none => simp + | some ko => + cases ko with + | archObj ao => + cases ao with + | ext o' => simp + | _ => simp + | _ => simp + +theorem extObjAt_congrX {σ σ' : State Unit} {r : ObjRef} (h : σ'.kheap r = σ.kheap r) : + extObjAt σ' r = extObjAt σ r := by + unfold extObjAt + rw [h] + +/-- A Stage-2 root names a `stage2Root` object, a high root a `highVSpace` one. -/ +theorem rootKindAt_objX {σ : State Unit} {r : ObjRef} {k : ExtRootKind} (hk : rootKindAt σ r = k) + (hk1 : k ≠ .stage1) : + ∃ o, extObjAt σ r = some o ∧ (k = .stage2 → ∃ e, o = .stage2Root e) ∧ (k = .high → ∃ e, o = .highVSpace e) := by + unfold rootKindAt at hk + cases ho : extObjAt σ r with + | none => rw [ho] at hk; exact absurd hk.symm hk1 + | some o => + rw [ho] at hk + cases o with + | stage2Root e => + refine ⟨_, rfl, fun _ => ⟨e, rfl⟩, fun h => ?_⟩ + rw [← hk] at h; cases h + | highVSpace e => + refine ⟨_, rfl, fun h => ?_, fun _ => ⟨e, rfl⟩⟩ + rw [← hk] at h; cases h + +/-- **The top index lies in the root object's table**: the root `r` of kind `k` names an FL4 object `o`, and the +walk geometry's top mask fits `o`'s entries. -/ +theorem geom_mask_le_sizeX {σ : State Unit} {r : ObjRef} {k : ExtRootKind} {o : ExtArchObj PTE} + (hk : rootKindAt σ r = k) (ho : extObjAt σ r = some o) : k.geom.topMask + 1 ≤ extObjSizeX o := by + unfold rootKindAt at hk + rw [ho] at hk + cases o with + | stage2Root e => + have e2 : k = .stage2 := hk.symm + subst e2 + show s2Geom.topMask + 1 ≤ 1024 + rcases s2Geom_casesX with h | h | h <;> rw [h] <;> decide + | highVSpace e => + have e2 : k = .high := hk.symm + subst e2 + show 511 + 1 ≤ 512 + omega + +/-! ## Walk steps -/ + +theorem extWalkN_zeroX (s : State Unit) (va : Word) (p : ExtPos) : extWalkN s va 0 p = p := rfl + +/-- **One step of FL4's walk** (`lookup_slot` / `lookup_pt_slot`). -/ +theorem extWalkN_succX (s : State Unit) (va : Word) (n : Nat) (p : ExtPos) : + extWalkN s va (n + 1) p = + if extIsTable (ExtSlot.read s p.slot) p.shift then + extWalkN s va n ⟨extNextSlot va (ExtSlot.read s p.slot) p.shift, p.shift - 9⟩ + else p := by + first + | rfl + | simp only [extWalkN, ptTB9X] + +theorem extFindTableN_zeroX (s : State Unit) (va pt : Word) (p : ExtPos) : extFindTableN s va pt 0 p = none := rfl + +/-- **One step of FL4's table search** (`unmap_table` / `unmap_page_table`). -/ +theorem extFindTableN_succX (s : State Unit) (va pt : Word) (n : Nat) (p : ExtPos) : + extFindTableN s va pt (n + 1) p = + if ¬ extIsTable (ExtSlot.read s p.slot) p.shift then none + else if pptrFromPte (ExtSlot.read s p.slot) = pt then some p.slot + else extFindTableN s va pt n ⟨extNextSlot va (ExtSlot.read s p.slot) p.shift, p.shift - 9⟩ := by + first + | rfl + | simp only [extFindTableN, ptTB9X] + +/-- A table descriptor is one above the page level. -/ +theorem extIsTable_iffX {e : PTE} {sh : Nat} : extIsTable e sh = true ↔ 12 < sh ∧ e.isPageTablePTE = true := by + unfold extIsTable + rw [Bool.and_eq_true, decide_eq_true_iff] + rfl + +/-- **The walk of the high window, written out** (`highGeom`: two steps below the level-1 table). -/ +theorem extLookupSlot_highX (s : State Unit) (r : ObjRef) (va : Word) : + extLookupSlot s ExtRootKind.high.geom r va = + if extIsTable (ExtSlot.read s (extTopPos ExtRootKind.high.geom r va).slot) 30 then + (if extIsTable (ExtSlot.read s (extNextSlot va (ExtSlot.read s (extTopPos ExtRootKind.high.geom r va).slot) 30)) 21 + then ⟨extNextSlot va (ExtSlot.read s (extNextSlot va + (ExtSlot.read s (extTopPos ExtRootKind.high.geom r va).slot) 30)) 21, 12⟩ + else ⟨extNextSlot va (ExtSlot.read s (extTopPos ExtRootKind.high.geom r va).slot) 30, 21⟩) + else extTopPos ExtRootKind.high.geom r va := by + show extWalkN s va (1 + 1) (extTopPos ExtRootKind.high.geom r va) = _ + rw [extWalkN_succX] + show (if extIsTable _ 30 then extWalkN s va (0 + 1) _ else _) = _ + rw [extWalkN_succX] + rfl + +/-- The walk stops where it starts, or at a `PageTable` slot of a smaller shift. -/ +theorem extWalkN_casesX (s : State Unit) (va : Word) (n : Nat) (p : ExtPos) : + extWalkN s va n p = p ∨ ((∃ q, (extWalkN s va n p).slot = .pt q) ∧ (extWalkN s va n p).shift < p.shift) := by + induction n generalizing p with + | zero => exact Or.inl rfl + | succ n ih => + rw [extWalkN_succX] + by_cases h : extIsTable (ExtSlot.read s p.slot) p.shift = true + · rw [if_pos h] + have h12 : 12 < p.shift := (extIsTable_iffX.1 h).1 + right + rcases ih ⟨extNextSlot va (ExtSlot.read s p.slot) p.shift, p.shift - 9⟩ with e | ⟨hq, hlt⟩ + · rw [e] + exact ⟨⟨_, rfl⟩, show p.shift - 9 < p.shift by omega⟩ + · exact ⟨hq, by simp only at hlt; omega⟩ + · rw [if_neg h] + exact Or.inl rfl + +/-- The shift drops by 9 a level. -/ +theorem extWalkN_shiftX (s : State Unit) (va : Word) (n : Nat) (p : ExtPos) : + ∃ j, j ≤ n ∧ (extWalkN s va n p).shift = p.shift - 9 * j := by + induction n generalizing p with + | zero => exact ⟨0, le_refl _, by rw [extWalkN_zeroX]; omega⟩ + | succ n ih => + rw [extWalkN_succX] + by_cases h : extIsTable (ExtSlot.read s p.slot) p.shift = true + · rw [if_pos h] + obtain ⟨j, hj, he⟩ := ih ⟨extNextSlot va (ExtSlot.read s p.slot) p.shift, p.shift - 9⟩ + refine ⟨j + 1, by omega, ?_⟩ + rw [he] + show p.shift - 9 - 9 * j = p.shift - 9 * (j + 1) + omega + · rw [if_neg h] + exact ⟨0, Nat.zero_le _, by omega⟩ + +/-- **FL4's lookup stops at the root's top slot, or at a `PageTable` slot below it.** -/ +theorem extLookupSlot_casesX (s : State Unit) (g : ExtGeom) (r : ObjRef) (va : Word) : + extLookupSlot s g r va = extTopPos g r va ∨ + ((∃ q, (extLookupSlot s g r va).slot = .pt q) ∧ (extLookupSlot s g r va).shift < g.topShift) := + extWalkN_casesX s va (g.levels - 1) (extTopPos g r va) + +theorem extLookupSlot_shiftX (s : State Unit) (g : ExtGeom) (r : ObjRef) (va : Word) : + ∃ j, j ≤ g.levels - 1 ∧ (extLookupSlot s g r va).shift = g.topShift - 9 * j := + extWalkN_shiftX s va (g.levels - 1) (extTopPos g r va) + +/-- **The slot the table search finds holds a table descriptor naming the table.** -/ +theorem extFindTableN_someX {s : State Unit} {va pt : Word} {n : Nat} {p : ExtPos} {sl : ExtSlot} + (h : extFindTableN s va pt n p = some sl) : + (ExtSlot.read s sl).isPageTablePTE = true ∧ pptrFromPte (ExtSlot.read s sl) = pt := by + induction n generalizing p with + | zero => rw [extFindTableN_zeroX] at h; cases h + | succ n ih => + rw [extFindTableN_succX] at h + by_cases h1 : extIsTable (ExtSlot.read s p.slot) p.shift = true + · rw [if_neg (not_not.2 h1)] at h + by_cases h2 : pptrFromPte (ExtSlot.read s p.slot) = pt + · rw [if_pos h2] at h + cases h + exact ⟨(extIsTable_iffX.1 h1).2, h2⟩ + · rw [if_neg h2] at h + exact ih h + · rw [if_pos h1] at h + cases h + +theorem extFindTableSlot_someX {s : State Unit} {g : ExtGeom} {r : ObjRef} {va pt : Word} {sl : ExtSlot} + (h : extFindTableSlot s g r va pt = some sl) : + (ExtSlot.read s sl).isPageTablePTE = true ∧ pptrFromPte (ExtSlot.read s sl) = pt := + extFindTableN_someX h + +/-! ## The store -/ + +/-- The heap `storeExtSlot sl pte` leaves (when it succeeds): the root's entry, or the `PageTable`'s slot, +replaced. -/ +def extSlotKhX (σ : State Unit) (sl : ExtSlot) (pte : PTE) : KHeap := + match sl with + | .root r i => + match extObjAt σ r with + | some o => Function.update σ.kheap r (some (.archObj (.ext (o.setEntry i pte)))) + | none => σ.kheap + | .pt p => + match σ.kheap (tableBase .normalPtT p) with + | some (.archObj (.pageTable t)) => + Function.update σ.kheap (tableBase .normalPtT p) + (some (.archObj (.pageTable (ptUpd t (tableIndex .normalPtT p) pte)))) + | _ => σ.kheap + +/-- **The state after writing `pte` to the slot `sl`**: only the heap changes. -/ +def extSlotWriteX (σ : State Unit) (sl : ExtSlot) (pte : PTE) : State Unit := + { σ with kheap := extSlotKhX σ sl pte } + +/-- The object a slot lives in. -/ +def extSlotObjX : ExtSlot → ObjRef + | .root r _ => r + | .pt p => tableBase .normalPtT p + +/-- A real slot: an FL4 root's entry, or an aligned slot of a normal `PageTable`. -/ +def ExtSlotOkX (σ : State Unit) : ExtSlot → Prop + | .root r _ => ∃ o, extObjAt σ r = some o + | .pt p => isAligned p pteBits ∧ + ∃ f, σ.kheap (tableBase .normalPtT p) = some (.archObj (.pageTable (.normalPT f))) + +theorem getObject_runX {σ : State Unit} {q : ObjRef} {o : KernelObject} (hk : σ.kheap q = some o) : + getObject (Z := Unit) q σ = ({(o, σ)}, False) := by + show NonDet.assertOpt (σ.kheap q) σ = _ + rw [hk] + rfl + +theorem ptsOf_eqX {σ : State Unit} {b : ObjRef} {t : PT} (hk : σ.kheap b = some (.archObj (.pageTable t))) : + ptsOf σ b = some t := by + unfold ptsOf aobjsOf optMap + rw [hk] <;> rfl + +theorem ptsOf_someX {σ : State Unit} {b : ObjRef} {t : PT} (h : ptsOf σ b = some t) : + σ.kheap b = some (.archObj (.pageTable t)) := by + unfold ptsOf aobjsOf optMap at h + cases hk : σ.kheap b with + | none => rw [hk] at h; cases h + | some ko => + rw [hk] at h + cases ko with + | archObj ao => + cases ao with + | pageTable t' => cases h; rfl + | _ => cases h + | _ => cases h + +theorem getPt_runX {σ : State Unit} {q : ObjRef} {t : PT} (hk : σ.kheap q = some (.archObj (.pageTable t))) : + getPt (Z := Unit) q σ = ({(t, σ)}, False) := by + show (NonDet.gets (ptsOf (Z := Unit)) >>= fun m => NonDet.assertOpt (m q)) σ = _ + rw [nd_bind_oneD (s' := σ) (a := ptsOf σ) rfl] + show NonDet.assertOpt (ptsOf σ q) σ = _ + rw [ptsOf_eqX hk] + rfl + +/-- `ptes_of` at a normal `PageTable` slot defines a real slot. -/ +theorem ptesOf_okX {σ : State Unit} {p : ObjRef} {x : PTE} (h : ptesOf σ .normalPtT p = some x) : + isAligned p pteBits ∧ ∃ f, σ.kheap (tableBase .normalPtT p) = some (.archObj (.pageTable (.normalPT f))) := by + have h' : levelPteOf .normalPtT p (ptsOf σ) = some x := h + unfold levelPteOf Lookup.oassert at h' + by_cases ha : isAligned p pteBits + · refine ⟨ha, ?_⟩ + cases hb : ptsOf σ (tableBase .normalPtT p) with + | none => simp [rbindX, ha, oapply, hb, Lookup.oreturn] at h' + | some t => + cases t with + | normalPT f => exact ⟨f, ptsOf_someX hb⟩ + | vsRootPT f => simp [rbindX, ha, oapply, hb, Lookup.oreturn, Lookup.ofail, ptType] at h' + · simp [rbindX, ha, Lookup.ofail] at h' + +theorem read_root_eqX (σ : State Unit) (r : ObjRef) (j : Nat) : + ExtSlot.read σ (.root r j) = ((extObjAt σ r).map fun o => o.entry j).getD .invalidPTE := rfl + +/-- The entry at a normal `PageTable` slot. -/ +theorem read_pt_eqX {σ : State Unit} {q : ObjRef} {f : PtIndex → PTE} + (hk : σ.kheap (tableBase .normalPtT q) = some (.archObj (.pageTable (.normalPT f)))) : + ExtSlot.read σ (.pt q) = + if isAligned q pteBits then f (ucast ptIndexLen (tableIndex .normalPtT q)) else .invalidPTE := by + have hpt : ptsOf σ (tableBase .normalPtT q) = some (.normalPT f) := ptsOf_eqX hk + show (levelPteOf .normalPtT q (ptsOf σ)).getD .invalidPTE = _ + unfold levelPteOf Lookup.oassert + by_cases ha : isAligned q pteBits <;> + simp [rbindX, ha, oapply, hpt, Lookup.oreturn, Lookup.ofail, ptType, ptApply] + +theorem read_pt_invX {σ : State Unit} {q : ObjRef} + (h : ¬ ∃ f, σ.kheap (tableBase .normalPtT q) = some (.archObj (.pageTable (.normalPT f)))) : + ExtSlot.read σ (.pt q) = .invalidPTE := by + cases hp : ptesOf σ .normalPtT q with + | none => + show (ptesOf σ .normalPtT q).getD .invalidPTE = _ + rw [hp] + rfl + | some x => exact absurd (ptesOf_okX hp).2 h + +/-- **A slot holding a valid entry is a real slot.** -/ +theorem okOfReadX {σ : State Unit} {sl : ExtSlot} (h : ExtSlot.read σ sl ≠ .invalidPTE) : ExtSlotOkX σ sl := by + cases sl with + | root r i => + cases ho : extObjAt σ r with + | none => exact (h (by rw [read_root_eqX, ho]; rfl)).elim + | some o => exact ⟨o, ho⟩ + | pt p => + by_cases hq : ∃ f, σ.kheap (tableBase .normalPtT p) = some (.archObj (.pageTable (.normalPT f))) + · cases hp : ptesOf σ .normalPtT p with + | none => + refine (h ?_).elim + show (ptesOf σ .normalPtT p).getD .invalidPTE = _ + rw [hp] + rfl + | some x => exact ptesOf_okX hp + · exact (h (read_pt_invX hq)).elim + +theorem storeExtSlot_rootX {σ : State Unit} {r : ObjRef} {i : Nat} {o : ExtArchObj PTE} + (ho : extObjAt σ r = some o) (pte : PTE) : + storeExtSlot (Z := Unit) (.root r i) pte σ = ({((), extSlotWriteX σ (.root r i) pte)}, False) := by + have hk := extObjAt_someX.1 ho + have hty : aType (.archObj (.ext o)) = aType (.archObj (.ext (o.setEntry i pte))) := by + cases o <;> rfl + have hw : extSlotWriteX σ (.root r i) pte = + { σ with kheap := Function.update σ.kheap r (some (.archObj (.ext (o.setEntry i pte)))) } := by + simp only [extSlotWriteX, extSlotKhX, ho] + rw [hw] + show (NonDet.getsThe (fun s : State Unit => extObjAt s r) >>= fun o => + setObject (Z := Unit) r (.archObj (.ext (o.setEntry i pte)))) σ = _ + rw [nd_bind_oneD (getsThe_runX (f := fun s : State Unit => extObjAt s r) (s := σ) ho)] + unfold setObject + rw [nd_bind_oneD (getObject_runX hk), nd_bind_oneD (assert_runX hty σ), + nd_bind_oneD (show (NonDet.get : NonDet (State Unit) _) σ = ({(σ, σ)}, False) from rfl)] + rfl + +theorem storePte_normalX {σ : State Unit} {p : ObjRef} {f : PtIndex → PTE} (hal : isAligned p pteBits) + (hk : σ.kheap (tableBase .normalPtT p) = some (.archObj (.pageTable (.normalPT f)))) (pte : PTE) : + storeExtSlot (Z := Unit) (.pt p) pte σ = ({((), extSlotWriteX σ (.pt p) pte)}, False) := by + have hty : aType (.archObj (.pageTable (.normalPT f))) = + aType (.archObj (.pageTable (ptUpd (.normalPT f) (tableIndex .normalPtT p) pte))) := rfl + have hw : extSlotWriteX σ (.pt p) pte = + { σ with kheap := (Function.update σ.kheap (tableBase .normalPtT p) + (some (.archObj (.pageTable (ptUpd (.normalPT f) (tableIndex .normalPtT p) pte))))) } := by + simp only [extSlotWriteX, extSlotKhX, hk] + rw [hw] + show storePte (Z := Unit) .normalPtT p pte σ = _ + unfold storePte + rw [nd_bind_oneD (assert_runX hal σ), nd_pure_bindD, nd_bind_oneD (getPt_runX hk), + nd_bind_oneD (assert_runX (P := ptType (.normalPT f) = PtType.normalPtT) rfl σ)] + unfold setPt + rw [nd_bind_oneD (getPt_runX hk)] + unfold setObject + rw [nd_bind_oneD (getObject_runX hk), nd_bind_oneD (assert_runX hty σ), + nd_bind_oneD (show (NonDet.get : NonDet (State Unit) _) σ = ({(σ, σ)}, False) from rfl)] + rfl + +/-- **`storeExtSlot` at a real slot**: no failure, the single outcome `extSlotWriteX`. -/ +theorem storeExtSlot_runX {σ : State Unit} {sl : ExtSlot} (h : ExtSlotOkX σ sl) (pte : PTE) : + storeExtSlot (Z := Unit) sl pte σ = ({((), extSlotWriteX σ sl pte)}, False) := by + cases sl with + | root r i => obtain ⟨o, ho⟩ := h; exact storeExtSlot_rootX ho pte + | pt p => obtain ⟨ha, f, hk⟩ := h; exact storePte_normalX ha hk pte + +/-- **The store touches only its own object** (and, by `extSlotWriteX`'s definition, no other state field). -/ +theorem kheap_writeX {σ : State Unit} {sl : ExtSlot} {pte : PTE} {x : ObjRef} (hx : x ≠ extSlotObjX sl) : + (extSlotWriteX σ sl pte).kheap x = σ.kheap x := by + show extSlotKhX σ sl pte x = σ.kheap x + cases sl with + | root r i => + simp only [extSlotKhX] + cases ho : extObjAt σ r with + | none => rfl + | some o => exact Function.update_of_ne hx _ _ + | pt p => + simp only [extSlotKhX] + cases hk : σ.kheap (tableBase .normalPtT p) with + | none => rfl + | some ko => + cases ko with + | archObj ao => + cases ao with + | pageTable t => exact Function.update_of_ne hx _ _ + | _ => rfl + | _ => rfl + +theorem extSlotWriteX_fieldsX (σ : State Unit) (sl : ExtSlot) (pte : PTE) : + extSlotWriteX σ sl pte = { σ with kheap := extSlotKhX σ sl pte } := rfl + +/-! ### Entries of an FL4 root -/ + +theorem entry_setEntry_selfX (o : ExtArchObj PTE) {i : Nat} (h : i < extObjSizeX o) (pte : PTE) : + (o.setEntry i pte).entry i = pte := by + cases o with + | stage2Root e => + have h' : i < 1024 := h + simp [ExtArchObj.setEntry, ExtArchObj.entry, h'] + | highVSpace e => + have h' : i < 512 := h + simp [ExtArchObj.setEntry, ExtArchObj.entry, h'] + +theorem entry_setEntry_neX (o : ExtArchObj PTE) {i j : Nat} (h : j ≠ i) (pte : PTE) : + (o.setEntry i pte).entry j = o.entry j := by + cases o <;> simp [ExtArchObj.setEntry, ExtArchObj.entry, h] + +theorem entry_setEntry_invX (o : ExtArchObj PTE) (i : Nat) : (o.setEntry i .invalidPTE).entry i = .invalidPTE := by + cases o <;> simp [ExtArchObj.setEntry, ExtArchObj.entry] + +/-! ### Reading after the store -/ + +theorem read_write_rootX {σ : State Unit} {r : ObjRef} {i j : Nat} {o : ExtArchObj PTE} + (ho : extObjAt σ r = some o) (pte : PTE) : + ExtSlot.read (extSlotWriteX σ (.root r i) pte) (.root r j) = (o.setEntry i pte).entry j := by + have hk : (extSlotWriteX σ (.root r i) pte).kheap r = some (.archObj (.ext (o.setEntry i pte))) := by + show extSlotKhX σ (.root r i) pte r = _ + simp [extSlotKhX, ho] + rw [read_root_eqX, extObjAt_someX.2 hk] + rfl + +theorem kheap_write_ptX {σ : State Unit} {p : ObjRef} {f : PtIndex → PTE} + (hk : σ.kheap (tableBase .normalPtT p) = some (.archObj (.pageTable (.normalPT f)))) (pte : PTE) : + (extSlotWriteX σ (.pt p) pte).kheap (tableBase .normalPtT p) = + some (.archObj (.pageTable (.normalPT (Function.update f (ucast ptIndexLen (tableIndex .normalPtT p)) pte)))) := by + show extSlotKhX σ (.pt p) pte (tableBase .normalPtT p) = _ + simp [extSlotKhX, hk, ptUpd] + +/-- A slot `q` of the same table after the store at `p`. -/ +theorem read_write_ptX {σ : State Unit} {p q : ObjRef} {f : PtIndex → PTE} + (hk : σ.kheap (tableBase .normalPtT p) = some (.archObj (.pageTable (.normalPT f)))) + (hb : tableBase .normalPtT q = tableBase .normalPtT p) (pte : PTE) : + ExtSlot.read (extSlotWriteX σ (.pt p) pte) (.pt q) = + if isAligned q pteBits then + Function.update f (ucast ptIndexLen (tableIndex .normalPtT p)) pte (ucast ptIndexLen (tableIndex .normalPtT q)) + else .invalidPTE := by + have h2 := kheap_write_ptX hk pte + rw [← hb] at h2 + exact read_pt_eqX h2 + +/-- **The written slot reads the written entry** (a real `PageTable` slot). -/ +theorem read_write_pt_selfX {σ : State Unit} {p : ObjRef} {f : PtIndex → PTE} (hal : isAligned p pteBits) + (hk : σ.kheap (tableBase .normalPtT p) = some (.archObj (.pageTable (.normalPT f)))) (pte : PTE) : + ExtSlot.read (extSlotWriteX σ (.pt p) pte) (.pt p) = pte := by + rw [read_write_ptX hk rfl, if_pos hal] + simp + +/-- **Clearing a real slot leaves it invalid.** -/ +theorem read_write_invalidX {σ : State Unit} {sl : ExtSlot} (h : ExtSlotOkX σ sl) : + ExtSlot.read (extSlotWriteX σ sl .invalidPTE) sl = .invalidPTE := by + cases sl with + | root r i => + obtain ⟨o, ho⟩ := h + rw [read_write_rootX ho, entry_setEntry_invX] + | pt p => + obtain ⟨ha, f, hk⟩ := h + exact read_write_pt_selfX ha hk _ + +/-- **Slots of other objects are unchanged by the store.** -/ +theorem read_write_otherX {σ : State Unit} {sl sl' : ExtSlot} {pte : PTE} (h : extSlotObjX sl' ≠ extSlotObjX sl) : + ExtSlot.read (extSlotWriteX σ sl pte) sl' = ExtSlot.read σ sl' := by + have hk : (extSlotWriteX σ sl pte).kheap (extSlotObjX sl') = σ.kheap (extSlotObjX sl') := kheap_writeX h + cases sl' with + | root r j => + rw [read_root_eqX, read_root_eqX, extObjAt_congrX (r := r) hk] + | pt q => + by_cases hq : ∃ f, σ.kheap (tableBase .normalPtT q) = some (.archObj (.pageTable (.normalPT f))) + · obtain ⟨f, hf⟩ := hq + rw [read_pt_eqX hf, read_pt_eqX (hk.trans hf)] + · rw [read_pt_invX hq, read_pt_invX (fun ⟨f, hf⟩ => hq ⟨f, hk.symm.trans hf⟩)] + +/-- Other entries of the same FL4 root are unchanged by the store. -/ +theorem read_write_root_neX {σ : State Unit} {r : ObjRef} {i j : Nat} {o : ExtArchObj PTE} + (ho : extObjAt σ r = some o) (h : j ≠ i) (pte : PTE) : + ExtSlot.read (extSlotWriteX σ (.root r i) pte) (.root r j) = ExtSlot.read σ (.root r j) := by + rw [read_write_rootX ho, entry_setEntry_neX o h, read_root_eqX, ho] + rfl + +/-! ## `find_ext_root` -/ + +/-- The result of `findExtRoot k asid` (`ept::find` / `find_high_asid`). -/ +def extRootResX (σ : State Unit) (k : ExtRootKind) (asid : Asid) : Except LookupFailure ObjRef := + match vspaceForAsid asid σ with + | some r => if rootKindAt σ r = k then .ok r else .error .invalidRoot + | none => .error .invalidRoot + +theorem extRootResX_okX {σ : State Unit} {k : ExtRootKind} {asid : Asid} {r : ObjRef} + (hr : vspaceForAsid asid σ = some r) (hk : rootKindAt σ r = k) : extRootResX σ k asid = .ok r := by + simp [extRootResX, hr, hk] + +theorem extRootResX_errX {σ : State Unit} {k : ExtRootKind} {asid : Asid} {r : ObjRef} + (hr : vspaceForAsid asid σ = some r) (hk : ¬ rootKindAt σ r = k) : + extRootResX σ k asid = .error .invalidRoot := by + simp [extRootResX, hr, hk] + +/-- (S3 gen 18: `findAnyRootForAsid`, the unhooked lookup `findExtRoot` now uses) -/ +theorem findAnyRoot_runX (asid : Asid) (σ : State Unit) : + (findAnyRootForAsid (Z := Unit) asid).run σ = + ({((match vspaceForAsid asid σ with + | some r => .ok r + | none => .error .invalidRoot : Except LookupFailure ObjRef), σ)}, False) := by + unfold findAnyRootForAsid + rw [run_bind_getsX] + cases vspaceForAsid asid σ <;> rfl + +/-- **`findExtRoot` is deterministic and never fails.** -/ +theorem findExtRoot_runX (k : ExtRootKind) (asid : Asid) (σ : State Unit) : + (findExtRoot (Z := Unit) k asid).run σ = ({(extRootResX σ k asid, σ)}, False) := by + unfold findExtRoot + cases hv : vspaceForAsid asid σ with + | none => + have h0 : (findAnyRootForAsid (Z := Unit) asid).run σ = ({(.error .invalidRoot, σ)}, False) := by + rw [findAnyRoot_runX, hv] + rw [run_bind_errorD h0] + simp only [extRootResX, hv] + | some r => + have h0 : (findAnyRootForAsid (Z := Unit) asid).run σ = ({(.ok r, σ)}, False) := by + rw [findAnyRoot_runX, hv] + rw [run_bind_okD h0, run_bind_getsX] + by_cases hk : rootKindAt σ r = k + · rw [unlessE_pos_bindX hk, extRootResX_okX hv hk] + rfl + · rw [unlessE_neg_runX hk, extRootResX_errX hv hk] + +theorem extRootResX_okE {σ : State Unit} {k : ExtRootKind} {asid : Asid} {r : ObjRef} + (h : extRootResX σ k asid = .ok r) : vspaceForAsid asid σ = some r ∧ rootKindAt σ r = k := by + unfold extRootResX at h + cases hv : vspaceForAsid asid σ with + | none => rw [hv] at h; cases h + | some r' => + rw [hv] at h + by_cases hk : rootKindAt σ r' = k + · have e : (if rootKindAt σ r' = k then (Except.ok r' : Except LookupFailure ObjRef) + else .error .invalidRoot) = .ok r' := if_pos hk + have h' := e.symm.trans h + cases h' + exact ⟨rfl, hk⟩ + · have e : (if rootKindAt σ r' = k then (Except.ok r' : Except LookupFailure ObjRef) + else .error .invalidRoot) = .error .invalidRoot := if_neg hk + have h' := e.symm.trans h + cases h' + +/-! ## `extMappedEntry` (FL4 `mapped_entry`) -/ + +theorem extMappedEntry_of_condX {σ : State Unit} {k : ExtRootKind} {r p : ObjRef} {sz : VMPageSize} {va : Word} + (h : (extLookupSlot σ k.geom r va).shift = pageBitsForSize sz ∧ + (ExtSlot.read σ (extLookupSlot σ k.geom r va).slot).isPagePTE = true ∧ + pptrFromPte (ExtSlot.read σ (extLookupSlot σ k.geom r va).slot) = p) : + extMappedEntry σ k r p sz va = + some (extLookupSlot σ k.geom r va, ExtSlot.read σ (extLookupSlot σ k.geom r va).slot) := by + unfold extMappedEntry + exact if_pos h + +theorem extMappedEntry_noneX {σ : State Unit} {k : ExtRootKind} {r p : ObjRef} {sz : VMPageSize} {va : Word} + (h : ¬ ((extLookupSlot σ k.geom r va).shift = pageBitsForSize sz ∧ + (ExtSlot.read σ (extLookupSlot σ k.geom r va).slot).isPagePTE = true ∧ + pptrFromPte (ExtSlot.read σ (extLookupSlot σ k.geom r va).slot) = p)) : + extMappedEntry σ k r p sz va = none := by + unfold extMappedEntry + exact if_neg h + +/-- **What `mapped_entry` returns**: the slot FL4's lookup stops at and its entry, a page or block descriptor of +the frame at the frame's size. -/ +theorem extMappedEntry_someX {σ : State Unit} {k : ExtRootKind} {r p : ObjRef} {sz : VMPageSize} {va : Word} + {pos : ExtPos} {pte : PTE} (h : extMappedEntry σ k r p sz va = some (pos, pte)) : + pos = extLookupSlot σ k.geom r va ∧ pte = ExtSlot.read σ pos.slot ∧ pos.shift = pageBitsForSize sz ∧ + pte.isPagePTE = true ∧ pptrFromPte pte = p := by + by_cases hc : (extLookupSlot σ k.geom r va).shift = pageBitsForSize sz ∧ + (ExtSlot.read σ (extLookupSlot σ k.geom r va).slot).isPagePTE = true ∧ + pptrFromPte (ExtSlot.read σ (extLookupSlot σ k.geom r va).slot) = p + · rw [extMappedEntry_of_condX hc] at h + cases h + exact ⟨rfl, rfl, hc⟩ + · rw [extMappedEntry_noneX hc] at h + cases h + +/-! ## The unmaps -/ + +/-- The slot `ept::unmap` / `unmap_page` clears, if any (ExtVsA gen 3: none at a high-window address outside +`HIGH_L0_INDEX`, `extVaOk`). -/ +def extPageSlotX (σ : State Unit) (k : ExtRootKind) (sz : VMPageSize) (asid : Asid) (va : VSpaceRef) (p : ObjRef) : + Option ExtSlot := + if extVaOk k va then + match extRootResX σ k asid with + | .ok r => (extMappedEntry σ k r p sz va).map fun x => x.1.slot + | .error _ => none + else none + +/-- **The state `extUnmapPage` leaves.** -/ +def extUnmapPageStX (σ : State Unit) (k : ExtRootKind) (sz : VMPageSize) (asid : Asid) (va : VSpaceRef) + (p : ObjRef) : State Unit := + match extPageSlotX σ k sz asid va p with + | some sl => extSlotWriteX σ sl .invalidPTE + | none => σ + +/-- The slot `ept::unmap_table` / `unmap_page_table` clears, if any. -/ +def extTableSlotX (σ : State Unit) (k : ExtRootKind) (asid : Asid) (va : VSpaceRef) (pt : ObjRef) : + Option ExtSlot := + match extRootResX σ k asid with + | .ok r => extFindTableSlot σ k.geom r va pt + | .error _ => none + +/-- **The state `extUnmapTable` leaves.** -/ +def extUnmapTableStX (σ : State Unit) (k : ExtRootKind) (asid : Asid) (va : VSpaceRef) (pt : ObjRef) : + State Unit := + match extTableSlotX σ k asid va pt with + | some sl => extSlotWriteX σ sl .invalidPTE + | none => σ + +section unmap +variable {σ : State Unit} {k : ExtRootKind} {sz : VMPageSize} {asid : Asid} {va : VSpaceRef} {p : ObjRef} + +theorem extUnmapPageStX_someX {sl : ExtSlot} (h : extPageSlotX σ k sz asid va p = some sl) : + extUnmapPageStX σ k sz asid va p = extSlotWriteX σ sl .invalidPTE := by + simp only [extUnmapPageStX, h] + +theorem extUnmapPageStX_noneX (h : extPageSlotX σ k sz asid va p = none) : extUnmapPageStX σ k sz asid va p = σ := by + simp only [extUnmapPageStX, h] + +theorem extUnmapTableStX_someX {sl : ExtSlot} (h : extTableSlotX σ k asid va p = some sl) : + extUnmapTableStX σ k asid va p = extSlotWriteX σ sl .invalidPTE := by + simp only [extUnmapTableStX, h] + +theorem extUnmapTableStX_noneX (h : extTableSlotX σ k asid va p = none) : extUnmapTableStX σ k asid va p = σ := by + simp only [extUnmapTableStX, h] + +/-- **The hit**: the ASID names a root of kind `k` and `mapped_entry` finds the frame: its slot is cleared. -/ +theorem extUnmapPageStX_hitX {r : ObjRef} {pos : ExtPos} {pte : PTE} (hg : extVaOk k va) + (hr : vspaceForAsid asid σ = some r) + (hk : rootKindAt σ r = k) (hm : extMappedEntry σ k r p sz va = some (pos, pte)) : + extUnmapPageStX σ k sz asid va p = extSlotWriteX σ pos.slot .invalidPTE := by + apply extUnmapPageStX_someX + simp only [extPageSlotX, if_pos hg, extRootResX_okX hr hk, hm] + rfl + +/-- **The miss**: a high-window address outside `HIGH_L0_INDEX`, no root of kind `k`, or the frame is not mapped +there: nothing changes. -/ +theorem extUnmapPageStX_missX + (h : extVaOk k va → ∀ r, vspaceForAsid asid σ = some r → rootKindAt σ r = k → extMappedEntry σ k r p sz va = none) : + extUnmapPageStX σ k sz asid va p = σ := by + apply extUnmapPageStX_noneX + unfold extPageSlotX + by_cases hg : extVaOk k va + · rw [if_pos hg] + cases hr : extRootResX σ k asid with + | error e => rfl + | ok r => + obtain ⟨h1, h2⟩ := extRootResX_okE hr + simp only [h hg r h1 h2] + rfl + · rw [if_neg hg] + +theorem extUnmapTableStX_hitX {r : ObjRef} {sl : ExtSlot} (hr : vspaceForAsid asid σ = some r) + (hk : rootKindAt σ r = k) (hs : extFindTableSlot σ k.geom r va p = some sl) : + extUnmapTableStX σ k asid va p = extSlotWriteX σ sl .invalidPTE := by + apply extUnmapTableStX_someX + simp only [extTableSlotX, extRootResX_okX hr hk, hs] + +theorem extUnmapTableStX_missX + (h : ∀ r, vspaceForAsid asid σ = some r → rootKindAt σ r = k → extFindTableSlot σ k.geom r va p = none) : + extUnmapTableStX σ k asid va p = σ := by + apply extUnmapTableStX_noneX + unfold extTableSlotX + cases hr : extRootResX σ k asid with + | error e => rfl + | ok r => + obtain ⟨h1, h2⟩ := extRootResX_okE hr + simp only [h r h1 h2] + +/-- Only the heap changes (`Ext/Stage2` has no TLB / cache effect for these unmaps). -/ +theorem extUnmapPageStX_kheapX : + extUnmapPageStX σ k sz asid va p = { σ with kheap := (extUnmapPageStX σ k sz asid va p).kheap } := by + unfold extUnmapPageStX + cases extPageSlotX σ k sz asid va p <;> rfl + +theorem extUnmapTableStX_kheapX : + extUnmapTableStX σ k asid va p = { σ with kheap := (extUnmapTableStX σ k asid va p).kheap } := by + unfold extUnmapTableStX + cases extTableSlotX σ k asid va p <;> rfl + +end unmap + +/-- **`ept::unmap` / `unmap_page` in a high window never fails and has a single outcome** (no-op, or the one +entry cleared: `extUnmapPageStX_hitX` / `_missX`). -/ +theorem extUnmapPage_runX (k : ExtRootKind) (sz : VMPageSize) (asid : Asid) (va : VSpaceRef) (p : ObjRef) + (σ : State Unit) : + extUnmapPage (Z := Unit) k sz asid va p σ = ({((), extUnmapPageStX σ k sz asid va p)}, False) := by + unfold extUnmapPage + by_cases hg : extVaOk k va + swap + · rw [extUnmapPageStX_noneX (by simp only [extPageSlotX, if_neg hg])] + exact (catch_errX (e := LookupFailure.invalidRoot) (s' := σ) (unlessE_neg_runX hg _ _ _)).trans rfl + rw [unlessE_pos_bindX hg] + cases hr : extRootResX σ k asid with + | error e => + have hst : extUnmapPageStX σ k sz asid va p = σ := by + apply extUnmapPageStX_noneX + simp only [extPageSlotX, if_pos hg, hr] + rw [hst] + refine (catch_errX (e := e) (s' := σ) ?_).trans rfl + exact run_bind_errorD (by rw [findExtRoot_runX, hr]) _ + | ok r => + have hf : (findExtRoot (Z := Unit) k asid).run σ = ({(.ok r, σ)}, False) := by rw [findExtRoot_runX, hr] + obtain ⟨hv, hk⟩ := extRootResX_okE hr + by_cases hm : (extLookupSlot σ k.geom r va).shift = pageBitsForSize sz ∧ + (ExtSlot.read σ (extLookupSlot σ k.geom r va).slot).isPagePTE = true ∧ + pptrFromPte (ExtSlot.read σ (extLookupSlot σ k.geom r va).slot) = p + · rw [extUnmapPageStX_hitX hg hv hk (extMappedEntry_of_condX hm)] + have hok : ExtSlotOkX σ (extLookupSlot σ k.geom r va).slot := by + apply okOfReadX + intro e + have h2 := hm.2.1 + rw [e] at h2 + cases h2 + refine catch_okX ?_ + rw [run_bind_okD hf, run_bind_getsX, unlessE_pos_bindX hm.1, run_bind_getsX, unlessE_pos_bindX hm.2] + exact run_liftE_stD (storeExtSlot_runX hok .invalidPTE) + · rw [extUnmapPageStX_missX (fun _ r' hv' hk' => by + rw [hv] at hv' + cases hv' + exact extMappedEntry_noneX hm)] + refine (catch_errX (e := .invalidRoot) (s' := σ) ?_).trans rfl + rw [run_bind_okD hf, run_bind_getsX] + by_cases h1 : (extLookupSlot σ k.geom r va).shift = pageBitsForSize sz + · rw [unlessE_pos_bindX h1, run_bind_getsX] + exact unlessE_neg_runX (fun h2 => hm ⟨h1, h2⟩) _ _ _ + · exact unlessE_neg_runX h1 _ _ _ + +/-- **`ept::unmap_table` / `unmap_page_table` in a high window never fails and has a single outcome.** -/ +theorem extUnmapTable_runX (k : ExtRootKind) (asid : Asid) (va : VSpaceRef) (pt : ObjRef) (σ : State Unit) : + extUnmapTable (Z := Unit) k asid va pt σ = ({((), extUnmapTableStX σ k asid va pt)}, False) := by + unfold extUnmapTable + cases hr : extRootResX σ k asid with + | error e => + have hst : extUnmapTableStX σ k asid va pt = σ := by + apply extUnmapTableStX_noneX + simp only [extTableSlotX, hr] + rw [hst] + refine (catch_errX (e := e) (s' := σ) ?_).trans rfl + exact run_bind_errorD (by rw [findExtRoot_runX, hr]) _ + | ok r => + have hf : (findExtRoot (Z := Unit) k asid).run σ = ({(.ok r, σ)}, False) := by rw [findExtRoot_runX, hr] + obtain ⟨hv, hk⟩ := extRootResX_okE hr + cases hs : extFindTableSlot σ k.geom r va pt with + | none => + rw [extUnmapTableStX_missX (fun r' hv' hk' => by rw [hv] at hv'; cases hv'; exact hs)] + refine catch_okX ?_ + rw [run_bind_okD hf, run_bind_getsX, hs] + rfl + | some sl => + rw [extUnmapTableStX_hitX hv hk hs] + have hok : ExtSlotOkX σ sl := by + apply okOfReadX + intro e + have h2 := (extFindTableSlot_someX hs).1 + rw [e] at h2 + cases h2 + refine catch_okX ?_ + rw [run_bind_okD hf, run_bind_getsX, hs] + exact run_liftE_stD (storeExtSlot_runX hok .invalidPTE) + +theorem extUnmapPage_noFailX (k : ExtRootKind) (sz : VMPageSize) (asid : Asid) (va : VSpaceRef) (p : ObjRef) + (σ : State Unit) : NonDet.NoFail (extUnmapPage (Z := Unit) k sz asid va p) σ := by + show ¬ (extUnmapPage (Z := Unit) k sz asid va p σ).2 + rw [extUnmapPage_runX] + exact fun h => h + +theorem extUnmapTable_noFailX (k : ExtRootKind) (asid : Asid) (va : VSpaceRef) (pt : ObjRef) (σ : State Unit) : + NonDet.NoFail (extUnmapTable (Z := Unit) k asid va pt) σ := by + show ¬ (extUnmapTable (Z := Unit) k asid va pt σ).2 + rw [extUnmapTable_runX] + exact fun h => h + +/-! ## Dispatch from `fl4FinaliseCap` -/ + +theorem asidRootKind_someX {σ : State Unit} {asid : Asid} {k : ExtRootKind} (h : asidRootKind σ asid = some k) : + ∃ r, vspaceForAsid asid σ = some r ∧ rootKindAt σ r = k := by + unfold asidRootKind at h + cases hv : vspaceForAsid asid σ with + | none => rw [hv] at h; cases h + | some r => + rw [hv] at h + exact ⟨r, rfl, Option.some.inj h⟩ + +section dispatch +variable {σ : State Unit} {asid : Asid} {va : VSpaceRef} {p : ObjRef} + +/-- **A mapped plain frame whose ASID names a high window** (ExtVsA gen 3): `fl4FinaliseCap`'s single outcome is +`(nullCap, nullCap)` at the state `unmap_page`'s `ROOT_HIGH` walk leaves, final or not. -/ +theorem fl4FinaliseCap_frame_runX {R : CapRights} {sz : VMPageSize} {dev final : Bool} + (hk : asidRootKind σ asid = some .high) : + fl4FinaliseCap (Z := Unit) (.archObjectCap (.frameCap p R sz dev (some (asid, va)))) final σ = + ({((Cap.nullCap, Cap.nullCap), extUnmapPageStX σ .high sz asid va p)}, False) := by + rw [fl4FinaliseCap_frame_hiA (extKindOfAsid_someA hk (by decide)), + nd_bind_oneD (extUnmapPage_runX .high sz asid va p σ)] + all_goals rfl + +/-- **A mapped plain frame whose ASID names a Stage-2 root** (ExtVsA gen 3): nothing (FL4 `unmap_page`'s +`unmap_root` finds no Stage-1 or high root). -/ +theorem fl4FinaliseCap_frame_s2X {R : CapRights} {sz : VMPageSize} {dev final : Bool} + (hk : asidRootKind σ asid = some .stage2) : + fl4FinaliseCap (Z := Unit) (.archObjectCap (.frameCap p R sz dev (some (asid, va)))) final σ = + ({((Cap.nullCap, Cap.nullCap), σ)}, False) := + fl4FinaliseCap_frame_s2A (extKindOfAsid_someA hk (by decide)) + +/-- **A mapped Stage-2 frame cap** (ExtVsA gen 3, `stage2FrameCap`): the single outcome `(nullCap, nullCap)` at the +state `ept::unmap` leaves, whatever its ASID names. -/ +theorem fl4FinaliseCap_s2frame_runX {R : CapRights} {sz : VMPageSize} {dev final : Bool} : + fl4FinaliseCap (Z := Unit) (.archObjectCap (.ext (.stage2FrameCap p R sz dev (some (asid, va))))) final σ = + ({((Cap.nullCap, Cap.nullCap), extUnmapPageStX σ .stage2 sz asid va p)}, False) := by + rw [fl4FinaliseCap_s2frameA, nd_bind_oneD (extUnmapPage_runX .stage2 sz asid va p σ)] + all_goals rfl + +/-- **The final cap of a mapped page table in an FL4 root**: the single outcome `(nullCap, nullCap)` at the +state `unmap_table` / `unmap_page_table` leaves. -/ +theorem fl4FinaliseCap_pt_runX {k : ExtRootKind} (hk : asidRootKind σ asid = some k) (hk1 : k ≠ .stage1) : + fl4FinaliseCap (Z := Unit) (.archObjectCap (.pageTableCap p .normalPtT (some (asid, va)))) true σ = + ({((Cap.nullCap, Cap.nullCap), extUnmapTableStX σ k asid va p)}, False) := by + rw [fl4FinaliseCap_pt_extA (extKindOfAsid_someA hk hk1), nd_bind_oneD (extUnmapTable_runX k asid va p σ)] + all_goals rfl + +/-- **The three-way split of a mapped plain frame's finalisation** (ExtVsA gen 3): a high window goes to the Ext +unmap, a Stage-2 root to nothing, an l4v root or no root to l4v's `unmap_page`. -/ +theorem fl4FinaliseCap_frame_splitX {R : CapRights} {sz : VMPageSize} {dev final : Bool} : + (∃ r, vspaceForAsid asid σ = some r ∧ rootKindAt σ r = .high ∧ + fl4FinaliseCap (Z := Unit) (.archObjectCap (.frameCap p R sz dev (some (asid, va)))) final σ = + ({((Cap.nullCap, Cap.nullCap), extUnmapPageStX σ .high sz asid va p)}, False)) ∨ + (∃ r, vspaceForAsid asid σ = some r ∧ rootKindAt σ r = .stage2 ∧ + fl4FinaliseCap (Z := Unit) (.archObjectCap (.frameCap p R sz dev (some (asid, va)))) final σ = + ({((Cap.nullCap, Cap.nullCap), σ)}, False)) ∨ + (asidRootKind σ asid = none ∨ asidRootKind σ asid = some .stage1) ∧ + fl4FinaliseCap (Z := Unit) (.archObjectCap (.frameCap p R sz dev (some (asid, va)))) final σ = (do + unmapPage sz asid va p + return (Cap.nullCap, Cap.nullCap) : SMonad Unit (Cap × Cap)) σ := by + cases h : asidRootKind σ asid with + | none => exact Or.inr (Or.inr ⟨Or.inl rfl, fl4FinaliseCap_frame_l4vA (extKindOfAsid_noneA (Or.inl h))⟩) + | some k => + obtain ⟨r, hv, hk⟩ := asidRootKind_someX h + cases k with + | stage1 => exact Or.inr (Or.inr ⟨Or.inr rfl, fl4FinaliseCap_frame_l4vA (extKindOfAsid_noneA (Or.inr h))⟩) + | stage2 => exact Or.inr (Or.inl ⟨r, hv, hk, fl4FinaliseCap_frame_s2X h⟩) + | high => exact Or.inl ⟨r, hv, hk, fl4FinaliseCap_frame_runX h⟩ + +/-- **The three-way split of a final mapped page table's finalisation.** -/ +theorem fl4FinaliseCap_pt_splitX : + (∃ k r, vspaceForAsid asid σ = some r ∧ rootKindAt σ r = k ∧ k ≠ .stage1 ∧ + fl4FinaliseCap (Z := Unit) (.archObjectCap (.pageTableCap p .normalPtT (some (asid, va)))) true σ = + ({((Cap.nullCap, Cap.nullCap), extUnmapTableStX σ k asid va p)}, False)) ∨ + (asidRootKind σ asid = none ∨ asidRootKind σ asid = some .stage1) ∧ + fl4FinaliseCap (Z := Unit) (.archObjectCap (.pageTableCap p .normalPtT (some (asid, va)))) true σ = (do + unmapPageTable asid va p + return (Cap.nullCap, Cap.nullCap) : SMonad Unit (Cap × Cap)) σ := by + cases h : asidRootKind σ asid with + | none => exact Or.inr ⟨Or.inl rfl, fl4FinaliseCap_pt_l4vA (extKindOfAsid_noneA (Or.inl h))⟩ + | some k => + by_cases hk1 : k = .stage1 + · subst hk1 + exact Or.inr ⟨Or.inr rfl, fl4FinaliseCap_pt_l4vA (extKindOfAsid_noneA (Or.inr h))⟩ + · obtain ⟨r, hv, hk⟩ := asidRootKind_someX h + exact Or.inl ⟨k, r, hv, hk, hk1, fl4FinaliseCap_pt_runX h hk1⟩ + +end dispatch + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/ExtVsTopA.lean b/proofs/Fl4ProofsA64/P3/ExtVsTopA.lean new file mode 100644 index 000000000..62820637b --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ExtVsTopA.lean @@ -0,0 +1,722 @@ +/- +P3 (aarch64, A64-P3 ExtVsA gen 1): **the Ext unmap tops and the combined Stage-1 / Stage-2 / high tops** (module +docstring of `P3/ExtVsUnmapA` for the parts; this module puts them together). + +* `ept_unmapE` / `ept_unmap_tableE` (`ept::unmap` / `ept::unmap_table`, any ASID, against `extUnmapPage .stage2` / + `extUnmapTable .stage2`); `unmap_page_hiE` / `unmap_page_table_hiE` (the `ROOT_HIGH` branches of + `vspace::unmap_page` / `unmap_page_table`); `unmap_page_table_s2E` (its `ROOT_S2` branch is `ept::unmap_table`). +* **`unmap_page_tableXE`**: FL4's `unmap_page_table` against `fl4FinaliseCap` of a final mapped page table cap, all + three root kinds, no `hS1` (`fl4FinaliseCap_pt_splitX`; Stage-1 / none by VS gen 4's `unmap_page_tableA`). +* (gen 3) **`unmap_frameXE`**: the `Cap::Frame` arm's unmap (`is_ept ? ept::unmap : unmap_page`) against + `fl4FinaliseCap` of the frame's abstract cap (`absFrameE`: the spec's `stage2FrameCap` for `is_ept`, l4v's + `frameCap` otherwise), **with no premise**: `unmap_frame_eptXE` (`ept::unmap` ↔ `extUnmapPage .stage2`, any ASID) + and `unmap_frame_s1XE` (`unmap_page` ↔ the plain frame's three-way split `fl4FinaliseCap_frame_splitX`: Stage-1 / + none by VS gen 4's `unmap_pageA`; a high window by `unmap_page_hiE` at `HIGH_L0_INDEX`, and below index 256 by + `unmap_page_hiLoE` — FL4's walk stops at the zero level-0 entry, `ExtVsRelA.wlo`, the spec's `extVaOk` guard finds + nothing; a Stage-2 root by `unmap_page_s2E` — FL4's `unmap_root` takes no `ROOT_S2`, the spec does nothing). + The plain frame's address is a Stage-1 or a window address (`VaOkE`, a lemma hypothesis from the map checks, as + `hX` / `hT`), so its level-0 index is `< 256` or `511` (`vaIdxE`). (S3 gen 18: `VaOkE` and `absFrameE` moved to + `P3/StateRelA` / `P3/CapRelA`; at `JAtZA` the hypothesis is `JAtZA.vaOkE`, and `absCapA` of a frame is `absFrameE`.) + +(S3 gen 26, ruling Q4) The `…XGE` tops expose FL4's continuation as one flush of the entry's arch state (`FlushE`, +`ArchFlE`; was `ArchContE`, its corollary `ArchFlE.cont`), so the whole-step tops can relate the returned arch state. + +Named premises: **`S2IpaE ar a` [P]** (`Arch::s2_ipa_bits` returns the platform's IPA size). No [J], no [W] +(gen 1's [J] `FrameKindE` was eliminated in gen 3 by the Architect's ruling (b′): the spec's `stage2FrameCap`). +-/ +import Fl4ProofsA64.P3.ExtVsUnmapA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-- **[P] The arch's Stage-2 IPA size is the platform's** (`Arch::s2_ipa_bits`, read by `ept::unmap` / +`unmap_table` from the arch state, is `fl4A64Platform.s2IpaBits`; A64 P1's `ArchPlat.s2_ipa_bits_const` [P] is its +constancy). -/ +abbrev S2IpaE {A : Type} (ar : machine.Arch A) (a : A) : Prop := + ∃ n : U64, ar.s2_ipa_bits a = ok n ∧ n.val = Fl4SpecA64.fl4A64Platform.s2IpaBits + +/-- Such an address has level-0 index `< 256` (Stage-1) or `HIGH_L0_INDEX = 511` (the window). -/ +theorem vaIdxE {va : U64} (h : VaOkE va) : (idxA va 39#u64).val < 256 ∨ idxA va 39#u64 = 511#u64 := by + have e39 : (39#u64 : U64).val = 39 := rfl + rcases h with h | ⟨h1, h2⟩ + · left + rw [idxA_val, e39] + simp only [Nat.reducePow] + omega + · right + apply UScalar.eq_of_val_eq + rw [idxA_val, e39, show (511#u64 : U64).val = 511 from rfl] + simp only [Nat.reducePow] + omega + +/-- At `HIGH_L0_INDEX` the spec's high-window guard holds. -/ +theorem extVaOk_hiE {va : U64} (hva : idxA va 39#u64 = 511#u64) : Fl4SpecA64.extVaOk .high va.bv := by + unfold Fl4SpecA64.extVaOk + refine Or.inr ?_ + have h := congrArg UScalar.val hva + rw [idxA_val, show (39#u64 : U64).val = 39 from rfl, show (511#u64 : U64).val = 511 from rfl] at h + show (va.bv.toNat >>> 39) % 512 = 511 + rw [Nat.shiftRight_eq_div_pow] + exact h + +/-- Below level-0 index 256 it fails. -/ +theorem extVaOk_loE {va : U64} (hlo : (idxA va 39#u64).val < 256) : ¬ Fl4SpecA64.extVaOk .high va.bv := by + unfold Fl4SpecA64.extVaOk + rintro (h | h) + · exact h rfl + · rw [idxA_val, show (39#u64 : U64).val = 39 from rfl] at hlo + change (va.bv.toNat >>> 39) % 512 = 511 at h + rw [Nat.shiftRight_eq_div_pow] at h + change va.val / 2 ^ 39 % 512 = 511 at h + omega + +theorem asidRootKind_ofE {σ : Fl4SpecA64.State Unit} {a : Fl4SpecA64.Asid} {r : Fl4SpecA64.ObjRef} + (hv : Fl4SpecA64.vspaceForAsid a σ = some r) : Fl4SpecA64.asidRootKind σ a = some (Fl4SpecA64.rootKindAt σ r) := by + unfold Fl4SpecA64.asidRootKind; rw [hv]; rfl + +theorem hs1_ofE {σ : Fl4SpecA64.State Unit} {a : Fl4SpecA64.Asid} + (hn : Fl4SpecA64.asidRootKind σ a = none ∨ Fl4SpecA64.asidRootKind σ a = some .stage1) : + ∀ r, Fl4SpecA64.vspaceForAsid a σ = some r → Fl4SpecA64.rootKindAt σ r = .stage1 := fun r hr => by + rw [asidRootKind_ofE hr] at hn + rcases hn with h | h + · cases h + · exact Option.some.inj h + +theorem wsub1E {x : U64} (h : 1 ≤ x.val) : (core.num.U64.wrapping_sub x 1#u64).val = x.val - 1 := by + have hx : x.val ≤ 18446744073709551615 := by scalar_tac + simp [core.num.U64.wrapping_sub_val_eq, U64.size_def, U64.numBits]; omega + +theorem frameBitsE (sz : capability.FrameSize) : capability.FrameSize.bits sz = ok (psA sz) := by cases sz <;> rfl + +/-- (gen 23) **FL4's continuation after an Ext unmap's zero write is an arch step**: it keeps the store state (its +flush, or nothing). Exposed by the `…XGE` tops for `P3/FinVsA`. -/ +def ArchContE {S A : Type} (fk : S → Result (S × A)) : Prop := + ∀ s1, ∃ fa : Result A, fk s1 = (do let a1 ← fa; ok (s1, a1)) + +theorem archContE_bind {S A : Type} (fa : Result A) : ArchContE (fun s1 : S => do let a1 ← fa; ok (s1, a1)) := + fun _ => ⟨fa, rfl⟩ + +theorem archContE_ok {S A : Type} (a : A) : ArchContE (fun s1 : S => ok (s1, a)) := + fun _ => ⟨ok a, by rw [bind_ok]⟩ + +/-- (gen 26, ruling Q4) **The Ext unmaps' arch step is one flush** (or none): `invalidate_stage2_page` / +`invalidate_stage2_vmid` (`ept::unmap` / `unmap_table`), `invalidate_high_page` / `invalidate_high_vspace` (the +`ROOT_HIGH` branches). `P3/VsMapJA`'s `ArchKeepsA` [P] keeps the arch relation across each (`P3/UnmapTopJA`). -/ +def FlushE {A : Type} (ar : machine.Arch A) (a : A) (fa : Result A) : Prop := + fa = ok a ∨ (∃ asid va : U64, fa = ar.invalidate_stage2_page a asid va) ∨ + (∃ asid : U64, fa = ar.invalidate_stage2_vmid a asid) ∨ (∃ va : U64, fa = ar.invalidate_high_page a va) ∨ + ∃ y : U64, fa = ar.invalidate_high_vspace a y + +/-- (gen 26, ruling Q4) **`ArchContE` with the arch step a flush of `a`** (`FlushE`): exposed by the `…XGE` tops so +the whole-step tops conclude `JAtZAX` at FL4's returned arch state. -/ +def ArchFlE {S A : Type} (ar : machine.Arch A) (a : A) (fk : S → Result (S × A)) : Prop := + ∀ s1, ∃ fa : Result A, FlushE ar a fa ∧ fk s1 = (do let a1 ← fa; ok (s1, a1)) + +theorem ArchFlE.cont {S A : Type} {ar : machine.Arch A} {a : A} {fk : S → Result (S × A)} (h : ArchFlE ar a fk) : + ArchContE fk := fun s1 => (h s1).imp fun _ h => h.2 + +theorem archFlE_bind {S A : Type} {ar : machine.Arch A} {a : A} {fa : Result A} (h : FlushE ar a fa) : + ArchFlE ar a (fun s1 : S => do let a1 ← fa; ok (s1, a1)) := fun _ => ⟨fa, h, rfl⟩ + +theorem archFlE_ok {S A : Type} {ar : machine.Arch A} (a : A) : ArchFlE ar a (fun s1 : S => ok (s1, a)) := + fun _ => ⟨ok a, Or.inl rfl, by rw [bind_ok]⟩ + +theorem flE_s2pageA {A : Type} {ar : machine.Arch A} {a : A} {x y : U64} : + FlushE ar a (ar.invalidate_stage2_page a x y) := Or.inr (Or.inl ⟨x, y, rfl⟩) +theorem flE_s2vmidA {A : Type} {ar : machine.Arch A} {a : A} {x : U64} : + FlushE ar a (ar.invalidate_stage2_vmid a x) := Or.inr (Or.inr (Or.inl ⟨x, rfl⟩)) +theorem flE_hiPageA {A : Type} {ar : machine.Arch A} {a : A} {x : U64} : + FlushE ar a (ar.invalidate_high_page a x) := Or.inr (Or.inr (Or.inr (Or.inl ⟨x, rfl⟩))) +theorem flE_hiVsA {A : Type} {ar : machine.Arch A} {a : A} {x : U64} : + FlushE ar a (ar.invalidate_high_vspace a x) := Or.inr (Or.inr (Or.inr (Or.inr ⟨x, rfl⟩))) + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +theorem unmap_root_hiE {s : S} {asid w : U64} (h : vspace.find_asid_raw st s asid = ok (.Ok w)) (ht : tagA w = 2) : + vspace.unmap_root st s asid = ok (some (untagA w, true)) := by + unfold vspace.unmap_root + rw [h, bind_ok] + simp only [lift, bind_ok, rtag, rs1, rhigh] + rw [if_neg (fun e => by rw [(tagNE w 0 0#u64 rfl).1 e] at ht; cases ht), if_pos ((tagNE w 2 2#u64 rfl).2 ht), + untagE] + +/-- `vspace::unmap_page` at a `ROOT_HIGH` raw entry: the walk from the window's level-0 table. -/ +theorem unmap_page_hiProgE {A : Type} {ar : machine.Arch A} {a : A} {s : S} {asid w : U64} + (hr : vspace.find_asid_raw st s asid = ok (.Ok w)) (ht2 : tagA w = 2) (sz : capability.FrameSize) (va ptr : U64) : + vspace.unmap_page st ar s a sz asid va ptr = (do + let lu ← vspace.lookup_pt_slot st s (untagA w) va + let i ← vspace.page_shift sz + if lu.shift != i then ok (s, a) else do + let e ← st.read s lu.slot + let b ← vspace.is_page e lu.shift + if b then do + let i1 ← vspace.page_base e lu.shift + let i2 ← layout.paddr ptr + if i1 != i2 then ok (s, a) else do + let s1 ← st.write s lu.slot 0#u64 + let a1 ← ar.invalidate_high_page a va + ok (s1, a1) + else ok (s, a)) := by + unfold vspace.unmap_page + rw [unmap_root_hiE hr ht2, bind_ok] + rfl + +variable {Pp Tb : Set U64} {Lv : U64 → Nat} {X Y T2 : Set U64} {Sh2 : U64 → Nat} {σ : Fl4SpecA64.State Unit} + +/-- The Stage-2 walk's start from FL4's `ept::geom` (shared by both Stage-2 tops). -/ +theorem s2StartE {n : U64} (hnv : n.val = Fl4SpecA64.fl4A64Platform.s2IpaBits) : + ∃ g kk, ept.geom n = ok g ∧ kk ≤ 10 ∧ g.top_mask.val = 2 ^ kk - 1 ∧ g.top_shift.val < 64 ∧ + g.top_shift.val = Fl4SpecA64.s2Geom.topShift ∧ + (core.num.U64.wrapping_sub g.levels 1#u64).val = Fl4SpecA64.s2Geom.levels - 1 ∧ + ∀ (va : U64) (w : U64), (idxME va g.top_shift kk).val < Fl4SpecA64.s2Geom.topMask + 1 ∧ + (Fl4SpecA64.extTopPos Fl4SpecA64.s2Geom (specPtr w) va.bv).slot = + .root (specPtr w) (idxME va g.top_shift kk).val := by + obtain ⟨g, hg, hts, htm, hlv⟩ := ept_geomE n + have hg2 : Fl4SpecA64.s2GeomOf n.val = Fl4SpecA64.s2Geom := by rw [hnv]; rfl + rw [hg2] at hts htm hlv + obtain ⟨hts', ⟨kk, hkk, hmk⟩, -, hlv', -⟩ := s2GeomNumE + have hidx : ∀ va : U64, (idxME va g.top_shift kk).val = + (va.bv.toNat >>> Fl4SpecA64.s2Geom.topShift) % (Fl4SpecA64.s2Geom.topMask + 1) := fun va => by + rw [idxME_val va _ hkk, hts, hmk, Nat.shiftRight_eq_div_pow, Nat.sub_add_cancel Nat.one_le_two_pow]; rfl + refine ⟨g, kk, hg, hkk, by rw [htm, hmk], by rw [hts]; rcases hts' with h | h <;> omega, hts, ?_, fun va w => ⟨?_, ?_⟩⟩ + · rw [wsub1E (by rw [hlv]; rcases hlv' with h | h <;> omega), hlv] + · rw [hidx]; exact Nat.mod_lt _ (by omega) + · rw [hidx]; rfl + +set_option maxHeartbeats 4000000 in +/-- **`ept::unmap` against `extUnmapPage .stage2`** (module docstring). -/ +theorem ept_unmapE {A : Type} {ar : machine.Arch A} {s : S} {a : A} + (R : VsRelA Pp Tb Lv X Y (L.mem s) (L.ksb s) σ) (E : ExtVsRelA Pp Tb X Y T2 Sh2 (L.mem s) σ) + (hK : Aligned (L.ksb s)) (hT : OffAsE Pp Tb X Y T2 (L.ksb s)) + (hX : ∀ r ∈ X, ∃ es, σ.kheap (specPtr r) = some (.archObj (.ext (.stage2Root es)))) + (hIpa : S2IpaE ar a) {asid : U64} (h16 : asid.val < 65536) (sz : capability.FrameSize) (va ptr : U64) : + ExtUnmapRelE Pp Tb Lv X Y T2 Sh2 σ st s a + (fun s1 => do let a1 ← ar.invalidate_stage2_page a asid va; ok (s1, a1)) + (ept.unmap st ar s a asid va ptr (psA sz) false) + (Fl4SpecA64.extUnmapPage (Z := Unit) .stage2 (frameSizeOfA sz) (BitVec.ofNat 16 asid.val) va.bv (specPtr ptr) σ) := by + obtain ⟨r, hr, hok, herr⟩ := spec_ex_fd (ept_findE (st := st) R hX h16) + unfold ept.unmap + rw [hr, bind_ok] + rcases r with w | e + · dsimp only + obtain ⟨hwX, hres⟩ := hok w rfl + obtain ⟨hv, hkd⟩ := extRootResX_okE hres + obtain ⟨n, hn, hnv⟩ := hIpa + obtain ⟨g, kk, hg, hkk, hmk, h64, hts, hrem, htop⟩ := s2StartE hnv + rw [hn, bind_ok] + obtain ⟨hj, hsl⟩ := htop va w + have hactW := activeS2RootA_of_vspaceForAsid R hwX hv + obtain ⟨⟨lsh, lsl⟩, hlu, hpost⟩ := spec_ex_fd (lookup_loopE (st := st) E.goodE (topOkS2E E) va + (Fl4SpecA64.extLookupSlot σ Fl4SpecA64.s2Geom (specPtr w) va.bv) False 0#u64 (fun h => h.elim) g.top_shift + (peAF w (idxME va g.top_shift kk)) (core.num.U64.wrapping_sub g.levels 1#u64) + (Or.inl ⟨Fl4SpecA64.extTopPos Fl4SpecA64.s2Geom (specPtr w) va.bv, by rw [hrem]; rfl, hts.symm, + Or.inr ⟨w, _, hwX, hactW, hj, rfl, hsl, rfl⟩⟩)) + have hlk : ept.lookup_slot st s w va n = ok { slot := lsl, shift := lsh } := by + unfold ept.lookup_slot + rw [hg, bind_ok, ept_entry_addrE _ _ _ _ h64 hmk, bind_ok] + simp only [lift, bind_ok] + rw [ept_lookup_loopE, hlu, bind_ok] + all_goals rfl + rw [hlk, bind_ok] + simp only [ept_is_leafE, ept_leaf_baseE] + exact pageTailE (sz := sz) (pptr := ptr) (a := a) + (fun s1 => do let a1 ← ar.invalidate_stage2_page a asid va; ok (s1, a1)) E.goodE (topOkS2E E) + (fun x sl sh h => writeS2E R E hK hT (h.mono (fun _ _ g => g.1) (fun _ _ _ ht => ht.toTopS2E))) + (fun x sl sh h => wrS2E E (h.mono (fun _ _ g => g.1) (fun _ _ _ ht => ht.toTopS2E))) + (Or.inl (fun h => by cases h)) hv hkd rfl hpost + · exact Or.inl ⟨rfl, extUnmapPage_errE (herr e rfl)⟩ + +set_option maxHeartbeats 4000000 in +/-- **`ept::unmap_table` against `extUnmapTable .stage2`** (module docstring). -/ +theorem ept_unmap_tableE {A : Type} {ar : machine.Arch A} {s : S} {a : A} + (R : VsRelA Pp Tb Lv X Y (L.mem s) (L.ksb s) σ) (E : ExtVsRelA Pp Tb X Y T2 Sh2 (L.mem s) σ) + (hK : Aligned (L.ksb s)) (hT : OffAsE Pp Tb X Y T2 (L.ksb s)) + (hX : ∀ r ∈ X, ∃ es, σ.kheap (specPtr r) = some (.archObj (.ext (.stage2Root es)))) + (hIpa : S2IpaE ar a) {asid : U64} (h16 : asid.val < 65536) (va pt : U64) : + ExtUnmapRelE Pp Tb Lv X Y T2 Sh2 σ st s a + (fun s1 => do let a1 ← ar.invalidate_stage2_vmid a asid; ok (s1, a1)) + (ept.unmap_table st ar s a asid va pt) + (Fl4SpecA64.extUnmapTable (Z := Unit) .stage2 (BitVec.ofNat 16 asid.val) va.bv (specPtr pt) σ) := by + obtain ⟨r, hr, hok, herr⟩ := spec_ex_fd (ept_findE (st := st) R hX h16) + unfold ept.unmap_table + rw [hr, bind_ok] + rcases r with w | e + · dsimp only + obtain ⟨hwX, hres⟩ := hok w rfl + obtain ⟨hv, hkd⟩ := extRootResX_okE hres + obtain ⟨n, hn, hnv⟩ := hIpa + obtain ⟨g, kk, hg, hkk, hmk, h64, hts, hrem, htop⟩ := s2StartE hnv + rw [hn, bind_ok, hg, bind_ok] + simp only [lift, bind_ok] + obtain ⟨hj, hsl⟩ := htop va w + have hactW := activeS2RootA_of_vspaceForAsid R hwX hv + obtain ⟨res, hres', hpost⟩ := spec_ex_fd (ept_unmap_table_loopE (st := st) E.goodE (topOkS2E E) va pt + (Fl4SpecA64.extFindTableSlot σ Fl4SpecA64.s2Geom (specPtr w) va.bv (specPtr pt)) w g.top_shift g.top_mask + (core.num.U64.wrapping_sub g.levels 1#u64) + ⟨kk, hkk, hmk, Fl4SpecA64.extTopPos Fl4SpecA64.s2Geom (specPtr w) va.bv, by rw [hrem]; rfl, hts.symm, + Or.inr ⟨w, _, hwX, hactW, hj, rfl, hsl, rfl⟩⟩) + rw [hres', bind_ok] + exact tableTailE (a := a) (fun s1 => do let a1 ← ar.invalidate_stage2_vmid a asid; ok (s1, a1)) + (fun x sl sh h => writeS2E R E hK hT (h.mono (fun _ _ g => g.1) (fun _ _ _ ht => ht.toTopS2E))) + (fun x sl sh h => wrS2E E (h.mono (fun _ _ g => g.1) (fun _ _ _ ht => ht.toTopS2E))) hv hkd rfl hpost + · exact Or.inl ⟨rfl, extUnmapTable_errE (herr e rfl)⟩ + +set_option maxHeartbeats 4000000 in +/-- **`vspace::unmap_page` at a `ROOT_HIGH` ASID against `extUnmapPage .high`** (module docstring). -/ +theorem unmap_page_hiE {A : Type} {ar : machine.Arch A} {s : S} {a : A} + (R : VsRelA Pp Tb Lv X Y (L.mem s) (L.ksb s) σ) (E : ExtVsRelA Pp Tb X Y T2 Sh2 (L.mem s) σ) + (hK : Aligned (L.ksb s)) (hT : OffAsE Pp Tb X Y T2 (L.ksb s)) + (hX : ∀ r ∈ X, ∃ es, σ.kheap (specPtr r) = some (.archObj (.ext (.stage2Root es)))) + {asid : U64} (h16 : asid.val < 65536) + (hk : Fl4SpecA64.asidRootKind σ (BitVec.ofNat 16 asid.val) = some .high) (sz : capability.FrameSize) + {va : U64} (hva : idxA va 39#u64 = 511#u64) (ptr : U64) : + ExtUnmapRelE Pp Tb Lv X Y T2 Sh2 σ st s a + (fun s1 => do let a1 ← ar.invalidate_high_page a va; ok (s1, a1)) + (vspace.unmap_page st ar s a sz asid va ptr) + (Fl4SpecA64.extUnmapPage (Z := Unit) .high (frameSizeOfA sz) (BitVec.ofNat 16 asid.val) va.bv (specPtr ptr) σ) := by + obtain ⟨r, hr, hrel⟩ := spec_ex_fd (find_asid_rawA (st := st) R h16) + obtain ⟨y0, hv0, hk0⟩ := asidRootKind_someX hk + rcases r with w | e + swap + · have hn : Fl4SpecA64.vspaceForAsid (BitVec.ofNat 16 asid.val) σ = none := hrel + rw [hn] at hv0; cases hv0 + obtain ⟨hv, hkind⟩ := rawKindE R hX hrel + rw [hv] at hv0 + cases hv0 + rcases hkind with ⟨-, h⟩ | ⟨-, -, h⟩ | ⟨ht2, hy, -⟩ + · rw [h] at hk0; cases hk0 + · rw [h] at hk0; cases hk0 + have hprog := unmap_page_hiProgE (ar := ar) (a := a) hr ht2 sz va ptr + rw [hprog] + obtain ⟨hb0, hb1, htab⟩ := E.wl0 _ hy + have hactY := activeHighRootA_of_vspaceForAsid R hX hy hv + have hP0 : True → (L.mem s (peAF (untagA w) (idxA va 39#u64)) &&& 3#u64) = 3#u64 ∧ + ∀ s1 l1 : U64, s1.val = 30 → l1.val = 2 → + WkE (G1ReachE Pp Tb Y Lv (L.mem s) (L.ksb s)) (TopHiReachE Pp Y (L.mem s) (L.ksb s)) σ va + (Fl4SpecA64.extLookupSlot σ Fl4SpecA64.ExtRootKind.high.geom (specPtr (untagA w)) va.bv) + (s1, peAF (dTabA (L.mem s (peAF (untagA w) (idxA va 39#u64)))) (idxA va s1), l1) := fun _ => by + rw [hva] + refine ⟨(and3_bitsA _).2 ⟨hb0, hb1⟩, fun s1 l1 hs1 hl1 => ⟨Fl4SpecA64.extTopPos Fl4SpecA64.ExtRootKind.high.geom + (specPtr (untagA w)) va.bv, by rw [hl1]; rfl, hs1.symm, Or.inr ⟨untagA w, idxA va s1, hy, hactY, idxA_lt _ _, + by rw [htab], ?_, rfl⟩⟩⟩ + show Fl4SpecA64.ExtSlot.root _ ((va.bv.toNat >>> 30) % (511 + 1)) = _ + rw [idxA_val, hs1, Nat.shiftRight_eq_div_pow] + rfl + obtain ⟨⟨lsh, lsl⟩, hlu, hpost⟩ := spec_ex_fd (lookup_loopE (st := st) R.goodE (topOkHiE R) va _ True _ hP0 + 39#u64 _ 3#u64 (Or.inr ⟨trivial, rfl⟩)) + have hlk : vspace.lookup_pt_slot st s (untagA w) va = ok { slot := lsl, shift := lsh } := by + unfold vspace.lookup_pt_slot + rw [show vspace.TOP_LEVEL_SHIFT = 39#u64 by unfold vspace.TOP_LEVEL_SHIFT; rfl, entry_addr_eqA _ _ _ (by simp), + bind_ok, hlu, bind_ok] + all_goals rfl + rw [hlk, bind_ok, page_shift_eqA, bind_ok] + have hk2 : Fl4SpecA64.rootKindAt σ (specPtr (untagA w)) = .high := hk0 + exact pageTailE (sz := sz) (pptr := ptr) (a := a) + (fun s1 => do let a1 ← ar.invalidate_high_page a va; ok (s1, a1)) R.goodE (topOkHiE R) + (fun x sl sh h => writeHiE R E hK hT (h.mono (fun _ _ g => g.1) (fun _ _ _ ht => ht.toTopHiE))) + (fun x sl sh h => wrHiE R (h.mono (fun _ _ g => g.1) (fun _ _ _ ht => ht.toTopHiE))) + (extVaOk_hiE hva) hv hk2 rfl hpost + +set_option maxHeartbeats 4000000 in +/-- **`vspace::unmap_page_table` at a `ROOT_HIGH` ASID against `extUnmapTable .high`** (module docstring). -/ +theorem unmap_page_table_hiE {A : Type} {ar : machine.Arch A} {s : S} {a : A} + (R : VsRelA Pp Tb Lv X Y (L.mem s) (L.ksb s) σ) (E : ExtVsRelA Pp Tb X Y T2 Sh2 (L.mem s) σ) + (hK : Aligned (L.ksb s)) (hT : OffAsE Pp Tb X Y T2 (L.ksb s)) + (hX : ∀ r ∈ X, ∃ es, σ.kheap (specPtr r) = some (.archObj (.ext (.stage2Root es)))) + {asid : U64} (h16 : asid.val < 65536) + (hk : Fl4SpecA64.asidRootKind σ (BitVec.ofNat 16 asid.val) = some .high) (va pt : U64) : + ∃ y, ExtUnmapRelE Pp Tb Lv X Y T2 Sh2 σ st s a + (fun s1 => do let a1 ← ar.invalidate_high_vspace a y; ok (s1, a1)) + (vspace.unmap_page_table st ar s a asid va pt) + (Fl4SpecA64.extUnmapTable (Z := Unit) .high (BitVec.ofNat 16 asid.val) va.bv (specPtr pt) σ) := by + obtain ⟨r, hr, hrel⟩ := spec_ex_fd (find_asid_rawA (st := st) R h16) + obtain ⟨y0, hv0, hk0⟩ := asidRootKind_someX hk + rcases r with w | e + swap + · have hn : Fl4SpecA64.vspaceForAsid (BitVec.ofNat 16 asid.val) σ = none := hrel + rw [hn] at hv0; cases hv0 + obtain ⟨hv, hkind⟩ := rawKindE R hX hrel + rw [hv] at hv0 + cases hv0 + rcases hkind with ⟨-, h⟩ | ⟨-, -, h⟩ | ⟨ht2, hy, -⟩ + · rw [h] at hk0; cases hk0 + · rw [h] at hk0; cases hk0 + refine ⟨untagA w, ?_⟩ + have h2 : (w &&& 3#u64) = 2#u64 := (tagNE w 2 2#u64 rfl).2 ht2 + have hn21 : ¬ (2#u64 : U64) = 1#u64 := by decide + have h511 : vspace.HIGH_L0_INDEX.val = 511 := by simp [vspace.HIGH_L0_INDEX] + obtain ⟨i8, hie, hiv⟩ := spec_ex_fd (U64.mul_spec (x := vspace.HIGH_L0_INDEX) (y := 8#u64) (by + rw [h511]; scalar_tac)) + have e8 : i8 = 4088#u64 := UScalar.eq_of_val_eq (by have := hiv; simp only [h511] at this; simpa using this) + subst e8 + have hw8 : wa (untagA w) 4088#u64 = peAF (untagA w) 511#u64 := by + unfold peAF; rw [show core.num.U64.wrapping_mul 511#u64 8#u64 = 4088#u64 by decide] + obtain ⟨hb0, hb1, htab⟩ := E.wl0 _ hy + have hprog : vspace.unmap_page_table st ar s a asid va pt = + Std.bind (vspace.unmap_page_table_loop st s va pt (wa (untagA w) 4096#u64) 30#u64 1#u64) (fun found => + match found with + | none => ok (s, a) + | some slot => do + let s1 ← st.write s slot 0#u64 + let a1 ← ar.invalidate_high_vspace a (untagA w) + ok (s1, a1)) := by + unfold vspace.unmap_page_table + rw [hr, bind_ok] + simp only [lift, bind_ok, rtag, rs2, rhigh, h2, hn21, untagE, ↓reduceIte] + rw [hie, bind_ok] + simp only [bind_ok, hw8, L.read_eq, table_of_eqA, htab] + rfl + rw [hprog] + have hsl : (Fl4SpecA64.extTopPos Fl4SpecA64.ExtRootKind.high.geom (specPtr (untagA w)) va.bv).slot = + .root (specPtr (untagA w)) (idxA va 30#u64).val := by + show Fl4SpecA64.ExtSlot.root _ ((va.bv.toNat >>> 30) % (511 + 1)) = _ + rw [idxA_val, Nat.shiftRight_eq_div_pow] + rfl + have hactY := activeHighRootA_of_vspaceForAsid R hX hy hv + obtain ⟨res, hres', hpost⟩ := spec_ex_fd (unmap_pt_loopE (st := st) R.goodE (topOkHiE R) va pt + (Fl4SpecA64.extFindTableSlot σ Fl4SpecA64.ExtRootKind.high.geom (specPtr (untagA w)) va.bv (specPtr pt)) + (wa (untagA w) 4096#u64) 30#u64 1#u64 + ⟨by show (1#u64 : U64).val ≤ 3; decide, Fl4SpecA64.extTopPos Fl4SpecA64.ExtRootKind.high.geom (specPtr (untagA w)) va.bv, rfl, rfl, + Or.inr ⟨untagA w, idxA va 30#u64, hy, hactY, idxA_lt _ _, rfl, hsl, rfl⟩⟩) + rw [hres', bind_ok] + have hk2 : Fl4SpecA64.rootKindAt σ (specPtr (untagA w)) = .high := hk0 + exact tableTailE (a := a) (fun s1 => do let a1 ← ar.invalidate_high_vspace a (untagA w); ok (s1, a1)) + (fun x sl sh h => writeHiE R E hK hT (h.mono (fun _ _ g => g.1) (fun _ _ _ ht => ht.toTopHiE))) + (fun x sl sh h => wrHiE R (h.mono (fun _ _ g => g.1) (fun _ _ _ ht => ht.toTopHiE))) hv hk2 rfl hpost + +set_option maxHeartbeats 2000000 in +/-- **`vspace::unmap_page_table` at a `ROOT_S2` ASID is `ept::unmap_table`.** -/ +theorem unmap_page_table_s2E {A : Type} {ar : machine.Arch A} {s : S} {a : A} + (R : VsRelA Pp Tb Lv X Y (L.mem s) (L.ksb s) σ) (E : ExtVsRelA Pp Tb X Y T2 Sh2 (L.mem s) σ) + (hK : Aligned (L.ksb s)) (hT : OffAsE Pp Tb X Y T2 (L.ksb s)) + (hX : ∀ r ∈ X, ∃ es, σ.kheap (specPtr r) = some (.archObj (.ext (.stage2Root es)))) + (hIpa : S2IpaE ar a) {asid : U64} (h16 : asid.val < 65536) + (hk : Fl4SpecA64.asidRootKind σ (BitVec.ofNat 16 asid.val) = some .stage2) (va pt : U64) : + ExtUnmapRelE Pp Tb Lv X Y T2 Sh2 σ st s a + (fun s1 => do let a1 ← ar.invalidate_stage2_vmid a asid; ok (s1, a1)) + (vspace.unmap_page_table st ar s a asid va pt) + (Fl4SpecA64.extUnmapTable (Z := Unit) .stage2 (BitVec.ofNat 16 asid.val) va.bv (specPtr pt) σ) := by + obtain ⟨r, hr, hrel⟩ := spec_ex_fd (find_asid_rawA (st := st) R h16) + obtain ⟨y0, hv0, hk0⟩ := asidRootKind_someX hk + rcases r with w | e + swap + · have hn : Fl4SpecA64.vspaceForAsid (BitVec.ofNat 16 asid.val) σ = none := hrel + rw [hn] at hv0; cases hv0 + obtain ⟨hv, hkind⟩ := rawKindE R hX hrel + rw [hv] at hv0 + cases hv0 + rcases hkind with ⟨-, h⟩ | ⟨ht1, -, -⟩ | ⟨-, -, h⟩ + · rw [h] at hk0; cases hk0 + swap + · rw [h] at hk0; cases hk0 + have h1 : (w &&& 3#u64) = 1#u64 := (tagNE w 1 1#u64 rfl).2 ht1 + have hu : vspace.unmap_page_table st ar s a asid va pt = ept.unmap_table st ar s a asid va pt := by + unfold vspace.unmap_page_table + rw [hr, bind_ok] + simp only [lift, bind_ok, rtag, rs2, h1, eq_self_iff_true, if_true, ↓reduceIte] + rw [hu] + exact ept_unmap_tableE R E hK hT hX hIpa h16 va pt + +/-! ## The combined tops -/ + +set_option maxHeartbeats 2000000 in +/-- (gen 23) **`unmap_page_tableXE` with FL4's continuation an arch step** (`ArchContE`; gen 26: a flush, `ArchFlE`). -/ +theorem unmap_page_tableXGE {A : Type} {ar : machine.Arch A} {s : S} {a : A} + (R : VsRelA Pp Tb Lv X Y (L.mem s) (L.ksb s) σ) (E : ExtVsRelA Pp Tb X Y T2 Sh2 (L.mem s) σ) + (hK : Aligned (L.ksb s)) (hT : OffAsE Pp Tb X Y T2 (L.ksb s)) + (hX : ∀ r ∈ X, ∃ es, σ.kheap (specPtr r) = some (.archObj (.ext (.stage2Root es)))) + (hIpa : S2IpaE ar a) {asid : U64} (h16 : asid.val < 65536) (va pt : U64) : + ((Fl4SpecA64.asidRootKind σ (BitVec.ofNat 16 asid.val) = none ∨ + Fl4SpecA64.asidRootKind σ (BitVec.ofNat 16 asid.val) = some .stage1) ∧ + Fl4SpecA64.fl4FinaliseCap (Z := Unit) + (.archObjectCap (.pageTableCap (specPtr pt) .normalPtT (some (BitVec.ofNat 16 asid.val, va.bv)))) true σ = + (do Fl4SpecA64.unmapPageTable (BitVec.ofNat 16 asid.val) va.bv (specPtr pt) + return (Fl4SpecA64.Cap.nullCap, Fl4SpecA64.Cap.nullCap) : Fl4SpecA64.SMonad Unit (Fl4SpecA64.Cap × Fl4SpecA64.Cap)) σ ∧ + UnmapRelA Pp Tb Lv X Y σ st s a (ar.invalidate_asid a asid) (vspace.unmap_page_table st ar s a asid va pt) + (Fl4SpecA64.unmapPageTable (Z := Unit) (BitVec.ofNat 16 asid.val) va.bv (specPtr pt) σ)) ∨ + ∃ k fk, k ≠ .stage1 ∧ ArchFlE ar a fk ∧ + Fl4SpecA64.fl4FinaliseCap (Z := Unit) + (.archObjectCap (.pageTableCap (specPtr pt) .normalPtT (some (BitVec.ofNat 16 asid.val, va.bv)))) true σ = + ({((Fl4SpecA64.Cap.nullCap, Fl4SpecA64.Cap.nullCap), + extUnmapTableStX σ k (BitVec.ofNat 16 asid.val) va.bv (specPtr pt))}, False) ∧ + ExtUnmapRelE Pp Tb Lv X Y T2 Sh2 σ st s a fk (vspace.unmap_page_table st ar s a asid va pt) + (Fl4SpecA64.extUnmapTable (Z := Unit) k (BitVec.ofNat 16 asid.val) va.bv (specPtr pt) σ) := by + rcases fl4FinaliseCap_pt_splitX (σ := σ) (asid := BitVec.ofNat 16 asid.val) (va := va.bv) (p := specPtr pt) with + ⟨k, r, hv, hk, hk1, hF⟩ | ⟨hn, hF⟩ + · right + have hak := asidRootKind_ofE hv + rw [hk] at hak + cases k with + | stage1 => exact absurd rfl hk1 + | stage2 => exact ⟨.stage2, _, hk1, archFlE_bind flE_s2vmidA, hF, unmap_page_table_s2E R E hK hT hX hIpa h16 hak va pt⟩ + | high => + obtain ⟨y, hy⟩ := unmap_page_table_hiE (ar := ar) (a := a) R E hK hT hX h16 hak va pt + exact ⟨.high, _, hk1, archFlE_bind flE_hiVsA, hF, hy⟩ + · left + exact ⟨hn, hF, unmap_page_tableA R hK (fun p hp i hi => hT p i hi (Or.inr (Or.inr (Or.inl hp)))) hX h16 + (hs1_ofE hn) va pt⟩ + +/-- **The final mapped page table cap's unmap, all root kinds, no `hS1`** (module docstring; gen 23: the corollary +of `unmap_page_tableXGE`, statement unchanged). -/ +theorem unmap_page_tableXE {A : Type} {ar : machine.Arch A} {s : S} {a : A} + (R : VsRelA Pp Tb Lv X Y (L.mem s) (L.ksb s) σ) (E : ExtVsRelA Pp Tb X Y T2 Sh2 (L.mem s) σ) + (hK : Aligned (L.ksb s)) (hT : OffAsE Pp Tb X Y T2 (L.ksb s)) + (hX : ∀ r ∈ X, ∃ es, σ.kheap (specPtr r) = some (.archObj (.ext (.stage2Root es)))) + (hIpa : S2IpaE ar a) {asid : U64} (h16 : asid.val < 65536) (va pt : U64) : + ((Fl4SpecA64.asidRootKind σ (BitVec.ofNat 16 asid.val) = none ∨ + Fl4SpecA64.asidRootKind σ (BitVec.ofNat 16 asid.val) = some .stage1) ∧ + Fl4SpecA64.fl4FinaliseCap (Z := Unit) + (.archObjectCap (.pageTableCap (specPtr pt) .normalPtT (some (BitVec.ofNat 16 asid.val, va.bv)))) true σ = + (do Fl4SpecA64.unmapPageTable (BitVec.ofNat 16 asid.val) va.bv (specPtr pt) + return (Fl4SpecA64.Cap.nullCap, Fl4SpecA64.Cap.nullCap) : Fl4SpecA64.SMonad Unit (Fl4SpecA64.Cap × Fl4SpecA64.Cap)) σ ∧ + UnmapRelA Pp Tb Lv X Y σ st s a (ar.invalidate_asid a asid) (vspace.unmap_page_table st ar s a asid va pt) + (Fl4SpecA64.unmapPageTable (Z := Unit) (BitVec.ofNat 16 asid.val) va.bv (specPtr pt) σ)) ∨ + ∃ k fk, k ≠ .stage1 ∧ Fl4SpecA64.fl4FinaliseCap (Z := Unit) + (.archObjectCap (.pageTableCap (specPtr pt) .normalPtT (some (BitVec.ofNat 16 asid.val, va.bv)))) true σ = + ({((Fl4SpecA64.Cap.nullCap, Fl4SpecA64.Cap.nullCap), + extUnmapTableStX σ k (BitVec.ofNat 16 asid.val) va.bv (specPtr pt))}, False) ∧ + ExtUnmapRelE Pp Tb Lv X Y T2 Sh2 σ st s a fk (vspace.unmap_page_table st ar s a asid va pt) + (Fl4SpecA64.extUnmapTable (Z := Unit) k (BitVec.ofNat 16 asid.val) va.bv (specPtr pt) σ) := + (unmap_page_tableXGE R E hK hT hX hIpa h16 va pt).imp_right fun ⟨k, fk, h1, _, h2, h3⟩ => ⟨k, fk, h1, h2, h3⟩ + +/-! ## FL4 `unmap_page` finding nothing (ExtVsA gen 3) -/ + +theorem unmap_root_s2E {s : S} {asid w : U64} (h : vspace.find_asid_raw st s asid = ok (.Ok w)) (ht : tagA w = 1) : + vspace.unmap_root st s asid = ok none := by + unfold vspace.unmap_root + rw [h, bind_ok] + simp only [lift, bind_ok, rtag, rs1, rhigh] + rw [if_neg (fun e => by rw [(tagNE w 0 0#u64 rfl).1 e] at ht; cases ht), + if_neg (fun e => by rw [(tagNE w 2 2#u64 rfl).1 e] at ht; cases ht)] + +/-- **FL4's lookup stops at a zero level-0 entry** (`lookup_pt_slot`'s first step: no table descriptor). -/ +theorem lookup_l0_zeroE {s : S} {root va : U64} (hz : L.mem s (peAF root (idxA va 39#u64)) = 0#u64) : + vspace.lookup_pt_slot st s root va = ok { slot := peAF root (idxA va 39#u64), shift := 39#u64 } := by + have hl : vspace.lookup_pt_slot_loop st s va 39#u64 (peAF root (idxA va 39#u64)) 3#u64 + ⦃ r => r = (39#u64, peAF root (idxA va 39#u64)) ⦄ := by + unfold vspace.lookup_pt_slot_loop + apply loop.spec_decr_nat (measure := fun x => x.2.2.val) + (inv := fun x => x = (39#u64, peAF root (idxA va 39#u64), 3#u64)) + · rintro ⟨sh, sl, lv⟩ hx + simp only [Prod.mk.injEq] at hx + obtain ⟨rfl, rfl, rfl⟩ := hx + dsimp only + unfold vspace.lookup_pt_slot_loop.body + rw [if_pos (show (3#u64 : U64) > 0#u64 by scalar_tac), L.read_eq, bind_ok, vs_is_tableE, bind_ok, hz, + if_neg (fun h => by have h' := (of_decide_eq_true h).2; revert h'; decide)] + exact (WP.spec_ok _).2 rfl + all_goals rfl + obtain ⟨r, hr, rfl⟩ := spec_ex_fd hl + unfold vspace.lookup_pt_slot + rw [show vspace.TOP_LEVEL_SHIFT = 39#u64 by unfold vspace.TOP_LEVEL_SHIFT; rfl, entry_addr_eqA _ _ _ (by simp), + bind_ok, hr, bind_ok] + all_goals rfl + +/-- **`vspace::unmap_page` at a `ROOT_S2` ASID does nothing** (`unmap_root` takes only Stage-1 and high roots). -/ +theorem unmap_page_s2E {A : Type} {ar : machine.Arch A} {s : S} {a : A} + (R : VsRelA Pp Tb Lv X Y (L.mem s) (L.ksb s) σ) + (hX : ∀ r ∈ X, ∃ es, σ.kheap (specPtr r) = some (.archObj (.ext (.stage2Root es)))) + {asid : U64} (h16 : asid.val < 65536) + (hk : Fl4SpecA64.asidRootKind σ (BitVec.ofNat 16 asid.val) = some .stage2) (sz : capability.FrameSize) + (va ptr : U64) : vspace.unmap_page st ar s a sz asid va ptr = ok (s, a) := by + obtain ⟨r, hr, hrel⟩ := spec_ex_fd (find_asid_rawA (st := st) R h16) + obtain ⟨y0, hv0, hk0⟩ := asidRootKind_someX hk + rcases r with w | e + swap + · have hn : Fl4SpecA64.vspaceForAsid (BitVec.ofNat 16 asid.val) σ = none := hrel + rw [hn] at hv0; cases hv0 + obtain ⟨hv, hkind⟩ := rawKindE R hX hrel + rw [hv] at hv0 + cases hv0 + rcases hkind with ⟨-, h⟩ | ⟨ht1, -, -⟩ | ⟨-, -, h⟩ + · rw [h] at hk0; cases hk0 + · unfold vspace.unmap_page + rw [unmap_root_s2E hr ht1, bind_ok] + · rw [h] at hk0; cases hk0 + +/-- **`vspace::unmap_page` at a `ROOT_HIGH` ASID below level-0 index 256 does nothing** (the window's level-0 +entry is zero, `ExtVsRelA.wlo`: the walk stops at level 0, shift 39, no frame size). -/ +theorem unmap_page_hiLoE {A : Type} {ar : machine.Arch A} {s : S} {a : A} + (R : VsRelA Pp Tb Lv X Y (L.mem s) (L.ksb s) σ) (E : ExtVsRelA Pp Tb X Y T2 Sh2 (L.mem s) σ) + (hX : ∀ r ∈ X, ∃ es, σ.kheap (specPtr r) = some (.archObj (.ext (.stage2Root es)))) + {asid : U64} (h16 : asid.val < 65536) + (hk : Fl4SpecA64.asidRootKind σ (BitVec.ofNat 16 asid.val) = some .high) (sz : capability.FrameSize) + {va : U64} (hlo : (idxA va 39#u64).val < 256) (ptr : U64) : + vspace.unmap_page st ar s a sz asid va ptr = ok (s, a) := by + obtain ⟨r, hr, hrel⟩ := spec_ex_fd (find_asid_rawA (st := st) R h16) + obtain ⟨y0, hv0, hk0⟩ := asidRootKind_someX hk + rcases r with w | e + swap + · have hn : Fl4SpecA64.vspaceForAsid (BitVec.ofNat 16 asid.val) σ = none := hrel + rw [hn] at hv0; cases hv0 + obtain ⟨hv, hkind⟩ := rawKindE R hX hrel + rw [hv] at hv0 + cases hv0 + rcases hkind with ⟨-, h⟩ | ⟨-, -, h⟩ | ⟨ht2, hy, -⟩ + · rw [h] at hk0; cases hk0 + · rw [h] at hk0; cases hk0 + have hprog := unmap_page_hiProgE (ar := ar) (a := a) hr ht2 sz va ptr + rw [hprog, lookup_l0_zeroE (E.wlo _ hy _ hlo), bind_ok, page_shift_eqA, bind_ok, + if_pos (by cases sz <;> rfl)] + +/-! ## The frame tops (ExtVsA gen 3: no `FrameKindE`) -/ + +/-- **A Stage-2 frame cap's unmap** (`Cap::Frame { is_ept: true }`: `ept::unmap`, against `fl4FinaliseCap` of a +`stage2FrameCap`, final or not, whatever its ASID names). -/ +theorem unmap_frame_eptXE {A : Type} {ar : machine.Arch A} {s : S} {a : A} + (R : VsRelA Pp Tb Lv X Y (L.mem s) (L.ksb s) σ) (E : ExtVsRelA Pp Tb X Y T2 Sh2 (L.mem s) σ) + (hK : Aligned (L.ksb s)) (hT : OffAsE Pp Tb X Y T2 (L.ksb s)) + (hX : ∀ r ∈ X, ∃ es, σ.kheap (specPtr r) = some (.archObj (.ext (.stage2Root es)))) + (hIpa : S2IpaE ar a) {asid : U64} (h16 : asid.val < 65536) (sz : capability.FrameSize) (va pptr : U64) + (Rt : Fl4SpecA64.CapRights) (dev final : Bool) : + Fl4SpecA64.fl4FinaliseCap (Z := Unit) (.archObjectCap (.ext (.stage2FrameCap (specPtr pptr) Rt + (frameSizeOfA sz) dev (some (BitVec.ofNat 16 asid.val, va.bv))))) final σ = + ({((Fl4SpecA64.Cap.nullCap, Fl4SpecA64.Cap.nullCap), + extUnmapPageStX σ .stage2 (frameSizeOfA sz) (BitVec.ofNat 16 asid.val) va.bv (specPtr pptr))}, False) ∧ + ExtUnmapRelE Pp Tb Lv X Y T2 Sh2 σ st s a + (fun s1 => do let a1 ← ar.invalidate_stage2_page a asid va; ok (s1, a1)) + (do let i ← capability.FrameSize.bits sz; ept.unmap st ar s a asid va pptr i false) + ({((), extUnmapPageStX σ .stage2 (frameSizeOfA sz) (BitVec.ofNat 16 asid.val) va.bv (specPtr pptr))}, False) := by + refine ⟨fl4FinaliseCap_s2frame_runX, ?_⟩ + rw [frameBitsE, bind_ok, ← extUnmapPage_runX] + exact ept_unmapE R E hK hT hX hIpa h16 sz va pptr + +set_option maxHeartbeats 4000000 in +/-- **A plain frame cap's unmap** (`Cap::Frame { is_ept: false }`: `vspace::unmap_page`, against `fl4FinaliseCap` +of a mapped `frameCap`, final or not), all root kinds, no `hS1`, no premise: an l4v root or none → VS gen 4's +`unmap_pageA`; a high window → `unmap_page_hiE` at `HIGH_L0_INDEX`, else nothing on both sides (`wlo`, the spec's +guard); a Stage-2 root → nothing on both sides. (Gen 23: FL4's continuation an arch step, `ArchContE`; gen 26: a +flush, `ArchFlE`; +`unmap_frame_s1XE` is its corollary.) -/ +theorem unmap_frame_s1XGE {A : Type} {ar : machine.Arch A} {s : S} {a : A} + (R : VsRelA Pp Tb Lv X Y (L.mem s) (L.ksb s) σ) (E : ExtVsRelA Pp Tb X Y T2 Sh2 (L.mem s) σ) + (hK : Aligned (L.ksb s)) (hT : OffAsE Pp Tb X Y T2 (L.ksb s)) + (hX : ∀ r ∈ X, ∃ es, σ.kheap (specPtr r) = some (.archObj (.ext (.stage2Root es)))) + {asid : U64} (h16 : asid.val < 65536) (sz : capability.FrameSize) (va pptr : U64) (hVa : VaOkE va) + (Rt : Fl4SpecA64.CapRights) (dev final : Bool) : + ((Fl4SpecA64.asidRootKind σ (BitVec.ofNat 16 asid.val) = none ∨ + Fl4SpecA64.asidRootKind σ (BitVec.ofNat 16 asid.val) = some .stage1) ∧ + Fl4SpecA64.fl4FinaliseCap (Z := Unit) (.archObjectCap (.frameCap (specPtr pptr) Rt (frameSizeOfA sz) dev + (some (BitVec.ofNat 16 asid.val, va.bv)))) final σ = + (do Fl4SpecA64.unmapPage (frameSizeOfA sz) (BitVec.ofNat 16 asid.val) va.bv (specPtr pptr) + return (Fl4SpecA64.Cap.nullCap, Fl4SpecA64.Cap.nullCap) : Fl4SpecA64.SMonad Unit (Fl4SpecA64.Cap × Fl4SpecA64.Cap)) σ ∧ + UnmapRelA Pp Tb Lv X Y σ st s a (ar.invalidate_page a asid va) (vspace.unmap_page st ar s a sz asid va pptr) + (Fl4SpecA64.unmapPage (Z := Unit) (frameSizeOfA sz) (BitVec.ofNat 16 asid.val) va.bv (specPtr pptr) σ)) ∨ + ∃ σ' fk, ArchFlE ar a fk ∧ + Fl4SpecA64.fl4FinaliseCap (Z := Unit) (.archObjectCap (.frameCap (specPtr pptr) Rt (frameSizeOfA sz) dev + (some (BitVec.ofNat 16 asid.val, va.bv)))) final σ = + ({((Fl4SpecA64.Cap.nullCap, Fl4SpecA64.Cap.nullCap), σ')}, False) ∧ + ExtUnmapRelE Pp Tb Lv X Y T2 Sh2 σ st s a fk (vspace.unmap_page st ar s a sz asid va pptr) ({((), σ')}, False) := by + rcases fl4FinaliseCap_frame_splitX (σ := σ) (asid := BitVec.ofNat 16 asid.val) (va := va.bv) + (p := specPtr pptr) (R := Rt) (sz := frameSizeOfA sz) (dev := dev) (final := final) with + ⟨r, hv, hk, hF⟩ | ⟨r, hv, hk, hF⟩ | ⟨hn, hF⟩ + · right + have hak := asidRootKind_ofE hv + rw [hk] at hak + refine ⟨_, fun s1 => do let a1 ← ar.invalidate_high_page a va; ok (s1, a1), archFlE_bind flE_hiPageA, hF, ?_⟩ + rcases vaIdxE hVa with hlo | h511 + · rw [extUnmapPageStX_missX (fun hg => absurd hg (extVaOk_loE hlo))] + exact Or.inl ⟨unmap_page_hiLoE R E hX h16 hak sz hlo pptr, rfl⟩ + · rw [← extUnmapPage_runX] + exact unmap_page_hiE R E hK hT hX h16 hak sz h511 pptr + · right + have hak := asidRootKind_ofE hv + rw [hk] at hak + exact ⟨σ, fun s1 => ok (s1, a), archFlE_ok a, hF, Or.inl ⟨unmap_page_s2E R hX h16 hak sz va pptr, rfl⟩⟩ + · left + have hT1 : ∀ p ∈ Tb, ∀ i : U64, i.val < 512 → ¬ AsW (L.ksb s) (peAF p i) := + fun p hp i hi => hT p i hi (Or.inr (Or.inr (Or.inl hp))) + exact ⟨hn, hF, unmap_pageA R hK hT1 hX sz h16 (hs1_ofE hn) va pptr⟩ + +/-- **A plain frame cap's unmap** (gen 23: the corollary of `unmap_frame_s1XGE`, statement unchanged). -/ +theorem unmap_frame_s1XE {A : Type} {ar : machine.Arch A} {s : S} {a : A} + (R : VsRelA Pp Tb Lv X Y (L.mem s) (L.ksb s) σ) (E : ExtVsRelA Pp Tb X Y T2 Sh2 (L.mem s) σ) + (hK : Aligned (L.ksb s)) (hT : OffAsE Pp Tb X Y T2 (L.ksb s)) + (hX : ∀ r ∈ X, ∃ es, σ.kheap (specPtr r) = some (.archObj (.ext (.stage2Root es)))) + {asid : U64} (h16 : asid.val < 65536) (sz : capability.FrameSize) (va pptr : U64) (hVa : VaOkE va) + (Rt : Fl4SpecA64.CapRights) (dev final : Bool) : + ((Fl4SpecA64.asidRootKind σ (BitVec.ofNat 16 asid.val) = none ∨ + Fl4SpecA64.asidRootKind σ (BitVec.ofNat 16 asid.val) = some .stage1) ∧ + Fl4SpecA64.fl4FinaliseCap (Z := Unit) (.archObjectCap (.frameCap (specPtr pptr) Rt (frameSizeOfA sz) dev + (some (BitVec.ofNat 16 asid.val, va.bv)))) final σ = + (do Fl4SpecA64.unmapPage (frameSizeOfA sz) (BitVec.ofNat 16 asid.val) va.bv (specPtr pptr) + return (Fl4SpecA64.Cap.nullCap, Fl4SpecA64.Cap.nullCap) : Fl4SpecA64.SMonad Unit (Fl4SpecA64.Cap × Fl4SpecA64.Cap)) σ ∧ + UnmapRelA Pp Tb Lv X Y σ st s a (ar.invalidate_page a asid va) (vspace.unmap_page st ar s a sz asid va pptr) + (Fl4SpecA64.unmapPage (Z := Unit) (frameSizeOfA sz) (BitVec.ofNat 16 asid.val) va.bv (specPtr pptr) σ)) ∨ + ∃ σ' fk, Fl4SpecA64.fl4FinaliseCap (Z := Unit) (.archObjectCap (.frameCap (specPtr pptr) Rt (frameSizeOfA sz) + dev (some (BitVec.ofNat 16 asid.val, va.bv)))) final σ = + ({((Fl4SpecA64.Cap.nullCap, Fl4SpecA64.Cap.nullCap), σ')}, False) ∧ + ExtUnmapRelE Pp Tb Lv X Y T2 Sh2 σ st s a fk (vspace.unmap_page st ar s a sz asid va pptr) ({((), σ')}, False) := + (unmap_frame_s1XGE (ar := ar) (a := a) R E hK hT hX h16 sz va pptr hVa Rt dev final).imp_right + fun ⟨σ', fk, _, h1, h2⟩ => ⟨σ', fk, h1, h2⟩ + +/-- **The `Cap::Frame` arm's unmap** (`objects.rs` `finalise_cap_object`: `is_ept ? ept::unmap : unmap_page`) +against `fl4FinaliseCap` of the frame's abstract cap (`absFrameE`), final or not, all root kinds, **no premise** +(ExtVsA gen 1's [J] `FrameKindE` is gone: the cap's kind is FL4's `is_ept`). `hVa` only for a plain frame. (Gen 23: +FL4's continuation an arch step, `ArchContE`, gen 26 `ArchFlE`; `unmap_frameXE` is its corollary.) -/ +theorem unmap_frameXGE {A : Type} {ar : machine.Arch A} {s : S} {a : A} + (R : VsRelA Pp Tb Lv X Y (L.mem s) (L.ksb s) σ) (E : ExtVsRelA Pp Tb X Y T2 Sh2 (L.mem s) σ) + (hK : Aligned (L.ksb s)) (hT : OffAsE Pp Tb X Y T2 (L.ksb s)) + (hX : ∀ r ∈ X, ∃ es, σ.kheap (specPtr r) = some (.archObj (.ext (.stage2Root es)))) + (hIpa : S2IpaE ar a) {asid : U64} (h16 : asid.val < 65536) (sz : capability.FrameSize) (va pptr : U64) + (isEpt : Bool) (hVa : isEpt = false → VaOkE va) (Rt : Fl4SpecA64.CapRights) (dev final : Bool) : + (isEpt = false ∧ (Fl4SpecA64.asidRootKind σ (BitVec.ofNat 16 asid.val) = none ∨ + Fl4SpecA64.asidRootKind σ (BitVec.ofNat 16 asid.val) = some .stage1) ∧ + Fl4SpecA64.fl4FinaliseCap (Z := Unit) (.archObjectCap (absFrameE isEpt (specPtr pptr) Rt (frameSizeOfA sz) dev + (some (BitVec.ofNat 16 asid.val, va.bv)))) final σ = + (do Fl4SpecA64.unmapPage (frameSizeOfA sz) (BitVec.ofNat 16 asid.val) va.bv (specPtr pptr) + return (Fl4SpecA64.Cap.nullCap, Fl4SpecA64.Cap.nullCap) : Fl4SpecA64.SMonad Unit (Fl4SpecA64.Cap × Fl4SpecA64.Cap)) σ ∧ + UnmapRelA Pp Tb Lv X Y σ st s a (ar.invalidate_page a asid va) (vspace.unmap_page st ar s a sz asid va pptr) + (Fl4SpecA64.unmapPage (Z := Unit) (frameSizeOfA sz) (BitVec.ofNat 16 asid.val) va.bv (specPtr pptr) σ)) ∨ + ∃ σ' fk, ArchFlE ar a fk ∧ + Fl4SpecA64.fl4FinaliseCap (Z := Unit) (.archObjectCap (absFrameE isEpt (specPtr pptr) Rt (frameSizeOfA sz) dev + (some (BitVec.ofNat 16 asid.val, va.bv)))) final σ = + ({((Fl4SpecA64.Cap.nullCap, Fl4SpecA64.Cap.nullCap), σ')}, False) ∧ + ExtUnmapRelE Pp Tb Lv X Y T2 Sh2 σ st s a fk + (if isEpt then (do let i ← capability.FrameSize.bits sz; ept.unmap st ar s a asid va pptr i false) + else vspace.unmap_page st ar s a sz asid va pptr) ({((), σ')}, False) := by + cases isEpt with + | true => + obtain ⟨hF, hU⟩ := unmap_frame_eptXE (ar := ar) (a := a) R E hK hT hX hIpa h16 sz va pptr Rt dev final + exact Or.inr ⟨_, _, archFlE_bind flE_s2pageA, hF, hU⟩ + | false => + rcases unmap_frame_s1XGE (ar := ar) (a := a) R E hK hT hX h16 sz va pptr (hVa rfl) Rt dev final with + ⟨hn, hF, hU⟩ | ⟨σ', fk, hk, hF, hU⟩ + · exact Or.inl ⟨rfl, hn, hF, hU⟩ + · exact Or.inr ⟨σ', fk, hk, hF, hU⟩ + +/-- **The `Cap::Frame` arm's unmap** (gen 23: the corollary of `unmap_frameXGE`, statement unchanged). -/ +theorem unmap_frameXE {A : Type} {ar : machine.Arch A} {s : S} {a : A} + (R : VsRelA Pp Tb Lv X Y (L.mem s) (L.ksb s) σ) (E : ExtVsRelA Pp Tb X Y T2 Sh2 (L.mem s) σ) + (hK : Aligned (L.ksb s)) (hT : OffAsE Pp Tb X Y T2 (L.ksb s)) + (hX : ∀ r ∈ X, ∃ es, σ.kheap (specPtr r) = some (.archObj (.ext (.stage2Root es)))) + (hIpa : S2IpaE ar a) {asid : U64} (h16 : asid.val < 65536) (sz : capability.FrameSize) (va pptr : U64) + (isEpt : Bool) (hVa : isEpt = false → VaOkE va) (Rt : Fl4SpecA64.CapRights) (dev final : Bool) : + (isEpt = false ∧ (Fl4SpecA64.asidRootKind σ (BitVec.ofNat 16 asid.val) = none ∨ + Fl4SpecA64.asidRootKind σ (BitVec.ofNat 16 asid.val) = some .stage1) ∧ + Fl4SpecA64.fl4FinaliseCap (Z := Unit) (.archObjectCap (absFrameE isEpt (specPtr pptr) Rt (frameSizeOfA sz) dev + (some (BitVec.ofNat 16 asid.val, va.bv)))) final σ = + (do Fl4SpecA64.unmapPage (frameSizeOfA sz) (BitVec.ofNat 16 asid.val) va.bv (specPtr pptr) + return (Fl4SpecA64.Cap.nullCap, Fl4SpecA64.Cap.nullCap) : Fl4SpecA64.SMonad Unit (Fl4SpecA64.Cap × Fl4SpecA64.Cap)) σ ∧ + UnmapRelA Pp Tb Lv X Y σ st s a (ar.invalidate_page a asid va) (vspace.unmap_page st ar s a sz asid va pptr) + (Fl4SpecA64.unmapPage (Z := Unit) (frameSizeOfA sz) (BitVec.ofNat 16 asid.val) va.bv (specPtr pptr) σ)) ∨ + ∃ σ' fk, Fl4SpecA64.fl4FinaliseCap (Z := Unit) (.archObjectCap (absFrameE isEpt (specPtr pptr) Rt + (frameSizeOfA sz) dev (some (BitVec.ofNat 16 asid.val, va.bv)))) final σ = + ({((Fl4SpecA64.Cap.nullCap, Fl4SpecA64.Cap.nullCap), σ')}, False) ∧ + ExtUnmapRelE Pp Tb Lv X Y T2 Sh2 σ st s a fk + (if isEpt then (do let i ← capability.FrameSize.bits sz; ept.unmap st ar s a asid va pptr i false) + else vspace.unmap_page st ar s a sz asid va pptr) ({((), σ')}, False) := + (unmap_frameXGE R E hK hT hX hIpa h16 sz va pptr isEpt hVa Rt dev final).imp_right + fun ⟨σ', fk, _, h1, h2⟩ => ⟨σ', fk, h1, h2⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/ExtVsUnmapA.lean b/proofs/Fl4ProofsA64/P3/ExtVsUnmapA.lean new file mode 100644 index 000000000..a60ec0ffd --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ExtVsUnmapA.lean @@ -0,0 +1,596 @@ +/- +P3 (aarch64, A64-P3 ExtVsA gen 1): **the Stage-2 / high-window unmaps and the combined frame / page-table unmap +tops** — FL4's `ept::unmap` / `ept::unmap_table` and the `ROOT_HIGH` branches of `vspace::unmap_page` / +`unmap_page_table` against `Ext/Stage2`'s `extUnmapPage` / `extUnmapTable`, under `VsRelA` and `ExtVsRelA`; then the +three-way tops (Stage-1 / Stage-2 / high) against `fl4FinaliseCap`, without VS gen 4's `hS1`. + +* **The zero write** (`writeS2E`, `writeHiE`): FL4's `write s a 0` at a word `a` that is the spec's slot `sl` + (`SlotRelE`) against `extSlotWriteX σ sl .invalidPTE`, keeping **both** `VsRelA` and `ExtVsRelA`: a Stage-2 + table's entry (`ExtVsRelA.of_ptClearE` + `VsRelA.of_s2WriteE`), a Stage-2 root's entry (`ExtVsRelA.of_rootClearE` + + `VsRelA.of_s2WriteE`), a window's level-1 entry (`VsRelA.of_hiClearE` + `ExtVsRelA.of_vsWriteE`), a Stage-1 table's + entry under a window (`extPtSetE`, VS gen 3's `VsRelA.of_storePteA` + `ExtVsRelA.of_vsWriteE`). x86: `EptUnmapCore`'s + `storeGuestEntry_eq` / the `EptRel` frame; Stage-1 twin `write_clearPteA`. +* **`ExtUnmapRelE`** (the Ext `UnmapRelA`): FL4 returns its inputs and the spec's run is `{((), σ)}`, or FL4 writes 0 + at one word, then runs its flush `fl`, and the spec's run is the single `{((), σ')}` with `VsRelA` and `ExtVsRelA` + at the written memory and `σ'` (`Ext/Stage2` models no TLB operation, so there is no machine-state outcome). + (S3 gen 25, ruling 22:14Z item 1, forced strengthening) and **`ExtWrE`**: where the word lies and which spec object + changes (`wrS2E` / `wrHiE`, a hypothesis `hwe` of `pageTailE` / `tableTailE`), for `P3/FinVsXA`'s `JAtZAX` lift. +* **Ext tops**: **`ept_unmapE`** (`ept::unmap` ↔ `extUnmapPage .stage2`, any ASID), **`ept_unmap_tableE`** + (`ept::unmap_table` ↔ `extUnmapTable .stage2`), **`unmap_page_hiE`** (`vspace::unmap_page` at a `ROOT_HIGH` ASID ↔ + `extUnmapPage .high`), **`unmap_page_table_hiE`** / **`unmap_page_table_s2E`** (`vspace::unmap_page_table` at a + `ROOT_HIGH` / `ROOT_S2` ASID). x86: `EptUnmapRel.ept_unmap_rel` / `ept_unmap_table_rel`. +* **Combined tops** (no `hS1`): **`unmap_page_tableXE`** — FL4's `unmap_page_table` (the `Cap::PageTable` arm of + `finalise_cap_object`) against `fl4FinaliseCap` of the final mapped table cap, split by + `fl4FinaliseCap_pt_splitX`: Stage-1 / no root → VS gen 4's `unmap_page_tableA` (its `hS1` discharged from the root's + kind), Stage-2 / high → the Ext tops. **`unmap_frameXE`** — the `Cap::Frame` arm's unmap (`is_ept ? ept::unmap : + unmap_page`) against `fl4FinaliseCap` of the frame's abstract cap (`P3/ExtVsTopA`, gen 3). + +**Named premises**: `S2IpaE ar a` **[P]** (`Arch::s2_ipa_bits` is the platform's IPA size, `fl4A64Platform.s2IpaBits`: +FL4 reads it from the arch state, the spec from the platform; A64 P1's `ArchPlat.s2_ipa_bits_const` [P] is its +constancy). (Gen 1's [J] `FrameKindE` was eliminated in gen 3: the spec's `stage2FrameCap`, `P3/ExtVsTopA`; the +page tails take the spec's high-window guard `extVaOk`.) +Lemma hypotheses (not premises): `hK` (aligned kernel-state base), `hT` (the VSpace pages' words are off the ASID-table +block, VS gen 4's `hT` widened to the Stage-2 pages), `hX` (`Stage2CoreA.obj`). No [W]. No `Fl4SpecA64` edit. +-/ +import Fl4ProofsA64.P3.ExtVsWalkA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## Spec states of one slot write -/ + +/-- The store at a related Stage-1 / Stage-2 table's slot is `set_pt`. -/ +theorem extPtSetE {σ : Fl4SpecA64.State Unit} {t i : U64} {pt : Fl4SpecA64.PT} + (hk : σ.kheap (specPtr t) = some (.archObj (.pageTable pt))) (ha : t.val % 4096 = 0) (hi : i.val < 512) + (pte : Fl4SpecA64.PTE) : + extSlotWriteX σ (.pt (specPtr (peAF t i))) pte = ptSetA σ (specPtr t) (Fl4SpecA64.ptUpd pt i.bv pte) := by + obtain ⟨-, hb, hx⟩ := slot_geomA .normalPtT ha hi + simp only [extSlotWriteX, extSlotKhX, hb, hk, hx, ptSetA] + +theorem slotObjPtE {t i : U64} (ha : t.val % 4096 = 0) (hi : i.val < 512) : + extSlotObjX (.pt (specPtr (peAF t i))) = specPtr t := (slot_geomA .normalPtT ha hi).2.1 + +/-- The store at an FL4 root's entry replaces that entry. -/ +theorem extRootSetE {σ : Fl4SpecA64.State Unit} {r : Fl4SpecA64.ObjRef} {o : Fl4SpecA64.ExtArchObj Fl4SpecA64.PTE} + (hk : σ.kheap r = some (.archObj (.ext o))) (j : Nat) (pte : Fl4SpecA64.PTE) : + (extSlotWriteX σ (.root r j) pte).kheap r = some (.archObj (.ext (o.setEntry j pte))) := by + show extSlotKhX σ (.root r j) pte r = _ + simp [extSlotKhX, extObjAt_someX.2 hk] + +/-! ## The zero write, Stage-2 side -/ + +section s2 +variable {Pp Tb X Y T2 : Set U64} {Sh2 : U64 → Nat} {σ : Fl4SpecA64.State Unit} + +/-- A word of a Stage-2 root's entry `j < 1024` is no word of another page. -/ +theorem rootNeE {r j q i : U64} (hr : 8192 ∣ r.val) (hj : j.val < 1024) (hq : 4096 ∣ q.val) (hi : i.val < 512) + (h1 : q ≠ r) (h2 : q ≠ wa r 4096#u64) : peAF q i ≠ peAF r j := by + obtain ⟨q', j', hq' | hq', hj', e⟩ := rootWordE hr hj <;> rw [e] <;> subst hq' + · exact pgNeE hq (al4kE hr) hi hj' h1 + · exact pgNeE hq (al4kHiE hr) hi hj' h2 + +/-- **A Stage-2 table's entry cleared keeps `ExtVsRelA`** (module docstring). -/ +theorem ExtVsRelA.of_ptClearE {mem mem' : U64 → U64} (E : ExtVsRelA Pp Tb X Y T2 Sh2 mem σ) + (hY : ∀ y ∈ Y, 8192 ∣ y.val) {t0 i0 : U64} (ht0 : t0 ∈ T2) (hi0 : i0.val < 512) + (hm : ∀ b, Aligned b → mem' b = if peAF t0 i0 = b then 0#u64 else mem b) : + ExtVsRelA Pp Tb X Y T2 Sh2 mem' (extSlotWriteX σ (.pt (specPtr (peAF t0 i0))) .invalidPTE) := by + have ha0 := E.tal t0 ht0 + have ha0' : t0.val % 4096 = 0 := Nat.mod_eq_zero_of_dvd ha0 + obtain ⟨pt0, hk0, hty0, he0⟩ := E.s2pt t0 ht0 + rw [extPtSetE hk0 ha0' hi0] + have hko : ∀ q, q ≠ t0 → (ptSetA σ (specPtr t0) (Fl4SpecA64.ptUpd pt0 i0.bv .invalidPTE)).kheap (specPtr q) = + σ.kheap (specPtr q) := fun q hq => by rw [ptSetA_kheap, if_neg (fun e => hq (specPtr_inj e))] + have hw : ∀ b, ExtFootE X Y T2 b → b ≠ peAF t0 i0 → mem' b = mem b := fun b hb hne => by + rw [hm b (E.foot_alignedE hY b hb), if_neg (Ne.symm hne)] + have hActS2 : ∀ {r}, ActiveS2RootA Pp X (ptSetA σ (specPtr t0) (Fl4SpecA64.ptUpd pt0 i0.bv .invalidPTE)) r → + ActiveS2RootA Pp X σ r := fun ha => + ha.of_spec (σ' := ptSetA σ (specPtr t0) (Fl4SpecA64.ptUpd pt0 i0.bv .invalidPTE)) rfl + fun p hp => hko p (fun e => (E.pgd t0 (Or.inl ht0)).1 (e ▸ Or.inl hp)) + have hReachS2 : ∀ {q}, + S2ReachA Pp X T2 Sh2 mem' (ptSetA σ (specPtr t0) (Fl4SpecA64.ptUpd pt0 i0.bv .invalidPTE)) q → + S2ReachA Pp X T2 Sh2 mem σ q := by + intro q hq + induction hq with + | root ha => exact .root (hActS2 ha) + | @stepRoot r i ha hi h3 ht2 => + have ha' := hActS2 ha + have hi1k : i.val < 1024 := by have := s2GeomNumE.2.2.1; omega + obtain ⟨n1, n2⟩ := E.t2x t0 ht0 r ha'.1 + rw [hw _ (rootFootE ha'.1 (E.xal r ha'.1) hi1k) (Ne.symm (rootNeE (E.xal r ha'.1) hi1k ha0 hi0 n1 n2))] at h3 ht2 ⊢ + exact .stepRoot ha' hi h3 ht2 + | @stepPt t i _ ht h12 hi h3 ht2 ih => + by_cases heq : peAF t i = peAF t0 i0 + · rw [heq, hm _ (peAF_alignedA ha0' hi0), if_pos rfl] at h3 + exact absurd h3 (by decide) + · rw [hw _ (.s2pt t i ht hi) heq] at h3 ht2 ⊢ + exact .stepPt ih ht h12 hi h3 ht2 + refine ⟨?_, ?_, E.tsh, ?_, ?_, E.xal, E.tal, E.pgd, E.t2x, E.ypg, E.ypp⟩ + · intro r hr + obtain ⟨es, hk, he⟩ := E.s2root r hr + obtain ⟨n1, n2⟩ := E.t2x t0 ht0 r hr + refine ⟨es, by rw [hko r (Ne.symm n1)]; exact hk, fun ha i hi hmi => ?_⟩ + rw [hw _ (rootFootE hr (E.xal r hr) hi) (Ne.symm (rootNeE (E.xal r hr) hi ha0 hi0 n1 n2))] + exact he (hActS2 ha) i hi hmi + · intro t ht + by_cases htt : t = t0 + · subst htt + refine ⟨Fl4SpecA64.ptUpd pt0 i0.bv .invalidPTE, by rw [ptSetA_kheap, if_pos rfl], by rw [ptUpd_typeA]; exact hty0, + fun hr j hj => ?_⟩ + rw [ptApply_updA pt0 hi0 hj] + by_cases hji : j = i0 + · subst hji + rw [if_pos rfl, hm _ (peAF_alignedA ha0' hj), if_pos rfl] + exact ⟨descRelE_zero _ _, trivial⟩ + · rw [if_neg hji, hw _ (.s2pt t j ht hj) (fun e => hji (peAF_injA ha0' ha0' hj hi0 e).2)] + exact he0 (hReachS2 hr) j hj + · obtain ⟨pt, hk, hty, he⟩ := E.s2pt t ht + refine ⟨pt, by rw [hko t htt]; exact hk, hty, fun hr j hj => ?_⟩ + rw [hw _ (.s2pt t j ht hj) (pgNeE (E.tal t ht) ha0 hj hi0 htt)] + exact he (hReachS2 hr) j hj + · intro y hy + have hne : y ≠ t0 := fun e => (E.pgd t0 (Or.inl ht0)).2 (e ▸ hy) + rw [hw _ (.wl0 y hy) (pgNeE (al4kE (hY y hy)) ha0 (by decide) hi0 hne)] + exact E.wl0 y hy + · intro y hy i hi + have hne : y ≠ t0 := fun e => (E.pgd t0 (Or.inl ht0)).2 (e ▸ hy) + rw [hw _ (.wlo y i hy hi) (pgNeE (al4kE (hY y hy)) ha0 (by omega) hi0 hne)] + exact E.wlo y hy i hi + +theorem peAF_1kInjE {r i j : U64} (hr : 8192 ∣ r.val) (hi : i.val < 1024) (hj : j.val < 1024) + (h : peAF r i = peAF r j) : i = j := by + have e := congrArg UScalar.val h + rw [peAF_val1kE (al8kE hr) hi, peAF_val1kE (al8kE hr) hj] at e + exact UScalar.eq_of_val_eq (by omega) + +theorem peAF_rootNeE {r r' i j : U64} (hr : 8192 ∣ r.val) (hr' : 8192 ∣ r'.val) (hi : i.val < 1024) + (hj : j.val < 1024) (hne : r ≠ r') : peAF r i ≠ peAF r' j := fun h => by + have e := congrArg UScalar.val h + rw [peAF_val1kE (al8kE hr) hi, peAF_val1kE (al8kE hr') hj] at e + exact hne (UScalar.eq_of_val_eq (by omega)) + +/-- **A Stage-2 root's entry cleared keeps `ExtVsRelA`** (module docstring). -/ +theorem ExtVsRelA.of_rootClearE {mem mem' : U64 → U64} (E : ExtVsRelA Pp Tb X Y T2 Sh2 mem σ) + (hY : ∀ y ∈ Y, 8192 ∣ y.val) {r0 j0 : U64} (hr0 : r0 ∈ X) (hj0 : j0.val < 1024) + (hm : ∀ b, Aligned b → mem' b = if peAF r0 j0 = b then 0#u64 else mem b) : + ExtVsRelA Pp Tb X Y T2 Sh2 mem' (extSlotWriteX σ (.root (specPtr r0) j0.val) .invalidPTE) := by + have ha0 := E.xal r0 hr0 + obtain ⟨es0, hk0, he0⟩ := E.s2root r0 hr0 + have hk1 := extRootSetE hk0 j0.val .invalidPTE + have hko : ∀ q, q ≠ r0 → (extSlotWriteX σ (.root (specPtr r0) j0.val) .invalidPTE).kheap (specPtr q) = + σ.kheap (specPtr q) := fun q hq => kheap_writeX (fun e => hq (specPtr_inj e)) + have hw : ∀ b, ExtFootE X Y T2 b → b ≠ peAF r0 j0 → mem' b = mem b := fun b hb hne => by + rw [hm b (E.foot_alignedE hY b hb), if_neg (Ne.symm hne)] + have hActS2 : ∀ {r}, ActiveS2RootA Pp X (extSlotWriteX σ (.root (specPtr r0) j0.val) .invalidPTE) r → + ActiveS2RootA Pp X σ r := fun ha => + ha.of_spec (σ' := extSlotWriteX σ (.root (specPtr r0) j0.val) .invalidPTE) rfl + fun p hp => hko p (fun e => (E.pgd r0 (Or.inr ⟨r0, hr0, Or.inl rfl⟩)).1 (e ▸ Or.inl hp)) + have hReachS2 : ∀ {q}, + S2ReachA Pp X T2 Sh2 mem' (extSlotWriteX σ (.root (specPtr r0) j0.val) .invalidPTE) q → + S2ReachA Pp X T2 Sh2 mem σ q := by + intro q hq + induction hq with + | root ha => exact .root (hActS2 ha) + | @stepRoot r i ha hi h3 ht2 => + have ha' := hActS2 ha + have hi1k : i.val < 1024 := by have := s2GeomNumE.2.2.1; omega + by_cases heq : peAF r i = peAF r0 j0 + · rw [heq, hm _ (E.foot_alignedE hY _ (rootFootE hr0 ha0 hj0)), if_pos rfl] at h3 + exact absurd h3 (by decide) + · rw [hw _ (rootFootE ha'.1 (E.xal r ha'.1) hi1k) heq] at h3 ht2 ⊢ + exact .stepRoot ha' hi h3 ht2 + | @stepPt t i _ ht h12 hi h3 ht2 ih => + obtain ⟨n1, n2⟩ := E.t2x t ht r0 hr0 + rw [hw _ (.s2pt t i ht hi) (rootNeE ha0 hj0 (E.tal t ht) hi n1 n2)] at h3 ht2 ⊢ + exact .stepPt ih ht h12 hi h3 ht2 + refine ⟨?_, ?_, E.tsh, ?_, ?_, E.xal, E.tal, E.pgd, E.t2x, E.ypg, E.ypp⟩ + · intro r hr + by_cases hrr : r = r0 + · subst hrr + refine ⟨_, hk1, fun ha i hi hmi => ?_⟩ + show DescRelE _ _ _ (if i.val = j0.val then .invalidPTE else es0 ⟨i.val, hi⟩) ∧ + S2AttrE _ _ (if i.val = j0.val then .invalidPTE else es0 ⟨i.val, hi⟩) + by_cases hij : i.val = j0.val + · rw [if_pos hij, hm _ (E.foot_alignedE hY _ (rootFootE hr ha0 hi)), + if_pos (by rw [UScalar.eq_of_val_eq hij])] + exact ⟨descRelE_zero _ _, trivial⟩ + · rw [if_neg hij, hw _ (rootFootE hr ha0 hi) (fun e => hij (by rw [peAF_1kInjE ha0 hi hj0 e]))] + exact he0 (hActS2 ha) i hi hmi + · obtain ⟨es, hk, he⟩ := E.s2root r hr + refine ⟨es, by rw [hko r hrr]; exact hk, fun ha i hi hmi => ?_⟩ + rw [hw _ (rootFootE hr (E.xal r hr) hi) (peAF_rootNeE (E.xal r hr) ha0 hi hj0 hrr)] + exact he (hActS2 ha) i hi hmi + · intro t ht + obtain ⟨n1, n2⟩ := E.t2x t ht r0 hr0 + obtain ⟨pt, hk, hty, he⟩ := E.s2pt t ht + refine ⟨pt, by rw [hko t n1]; exact hk, hty, fun hr j hj => ?_⟩ + rw [hw _ (.s2pt t j ht hj) (rootNeE ha0 hj0 (E.tal t ht) hj n1 n2)] + exact he (hReachS2 hr) j hj + · intro y hy + have h1 := (E.pgd r0 (Or.inr ⟨r0, hr0, Or.inl rfl⟩)).2 + have h2 := (E.pgd _ (Or.inr ⟨r0, hr0, Or.inr rfl⟩)).2 + rw [hw _ (.wl0 y hy) (rootNeE ha0 hj0 (al4kE (hY y hy)) (by decide) (fun e => h1 (e ▸ hy)) + (fun e => h2 (e ▸ hy)))] + exact E.wl0 y hy + · intro y hy i hi + have h1 := (E.pgd r0 (Or.inr ⟨r0, hr0, Or.inl rfl⟩)).2 + have h2 := (E.pgd _ (Or.inr ⟨r0, hr0, Or.inr rfl⟩)).2 + rw [hw _ (.wlo y i hy hi) (rootNeE ha0 hj0 (al4kE (hY y hy)) (by omega) (fun e => h1 (e ▸ hy)) + (fun e => h2 (e ▸ hy)))] + exact E.wlo y hy i hi + +end s2 + +/-! ## The zero write, high-window side -/ + +section hi +variable {Pp Tb : Set U64} {Lv : U64 → Nat} {X Y T2 : Set U64} {Sh2 : U64 → Nat} {σ : Fl4SpecA64.State Unit} + +/-- **A window's level-1 entry cleared keeps `VsRelA`** (module docstring). -/ +theorem VsRelA.of_hiClearE {mem mem' : U64 → U64} {ksb : U64} (R : VsRelA Pp Tb Lv X Y mem ksb σ) + {mem0 : U64 → U64} (E : ExtVsRelA Pp Tb X Y T2 Sh2 mem0 σ) (hK : Aligned ksb) {y0 j0 : U64} (hy0 : y0 ∈ Y) + (hj0 : j0.val < 512) (hA : ¬ AsW ksb (peAF (wa y0 4096#u64) j0)) + (hm : ∀ b, Aligned b → mem' b = if peAF (wa y0 4096#u64) j0 = b then 0#u64 else mem b) : + VsRelA Pp Tb Lv X Y mem' ksb (extSlotWriteX σ (.root (specPtr y0) j0.val) .invalidPTE) := by + have hal := R.yal y0 hy0 + have hP : 4096 ∣ (wa y0 4096#u64).val := al4kHiE hal + obtain ⟨l0, hk0, he0⟩ := R.high y0 hy0 + have hk1 := extRootSetE hk0 j0.val .invalidPTE + have hko : ∀ q, q ≠ y0 → (extSlotWriteX σ (.root (specPtr y0) j0.val) .invalidPTE).kheap (specPtr q) = + σ.kheap (specPtr q) := fun q hq => kheap_writeX (fun e => hq (specPtr_inj e)) + have hw : ∀ b, VsFootA Pp Tb Y ksb b → b ≠ peAF (wa y0 4096#u64) j0 → mem' b = mem b := fun b hb hne => by + rw [hm b (R.foot_alignedA hK b hb), if_neg (Ne.symm hne)] + have hy0p : y0 ∉ VsPtA Pp Tb Y := E.ypg y0 hy0 + have asNe : ∀ hi : U64, hi.val < 128 → atAF ksb hi ≠ peAF (wa y0 4096#u64) j0 := + fun hi hh e => hA (e ▸ atAF_asWA hh) + have poolNe : ∀ p ∈ Pp, ∀ lo : U64, lo.val < 512 → peAF p lo ≠ peAF (wa y0 4096#u64) j0 := + fun p hp lo hl => pgNeE (R.pal p hp) hP hl hj0 (fun e' => E.ypp y0 hy0 (e' ▸ hp)) + have pNe : ∀ p, p ∈ Pp ∨ p ∈ Tb → p ≠ y0 := fun p hp e => hy0p (by + rcases hp with h | h + · exact e ▸ Or.inl h + · exact e ▸ Or.inr (Or.inl h)) + have hActP : ∀ {p}, ActivePoolA mem' ksb p → ActivePoolA mem ksb p := by + rintro p ⟨hpne, hi, hh, htab⟩ + rw [hw _ (.asid hi hh) (asNe hi hh)] at htab + exact ⟨hpne, hi, hh, htab⟩ + have hActR : ∀ {asid raw}, ActiveAsidRootA mem' ksb Pp asid raw → ActiveAsidRootA mem ksb Pp asid raw := by + rintro asid raw ⟨hi, lo, hh, hl, ha', pool, hp, hpne, htab, hword, hrne⟩ + rw [hw _ (.asid hi hh) (asNe hi hh)] at htab + rw [hw _ (.pool pool lo hp hl) (poolNe pool hp lo hl)] at hword + exact ⟨hi, lo, hh, hl, ha', pool, hp, hpne, htab, hword, hrne⟩ + have hActH : ∀ {y}, ActiveHighRootA mem' ksb Pp y → ActiveHighRootA mem ksb Pp y := by + rintro y ⟨asid, raw, ha, htag, hy⟩ + exact ⟨asid, raw, hActR ha, htag, hy⟩ + have hPath : ∀ {tag sh q}, VsPathA Pp Tb Y mem' ksb tag sh q → VsPathA Pp Tb Y mem ksb tag sh q := by + intro tag sh q hpth + induction hpth with + | native ha htag htb => exact .native (hActR ha) htag htb + | high ha htag hy => exact .high (hActR ha) htag hy + | @child tag t i sh _ htY h12 hi h3' ih => + by_cases heq : peAF t i = peAF (wa y0 4096#u64) j0 + · rw [heq, hm _ (peAF_alignedA (waWin_modA hal) hj0), if_pos rfl] at h3' + exact absurd h3' (by decide) + · have hmemEq : mem' (peAF t i) = mem (peAF t i) := by + rcases htY with ht | ⟨y, hy, rfl⟩ + · exact hw _ (.pt t i ht hi) heq + · exact hw _ (.high y i hy hi) heq + rw [hmemEq] at h3' ⊢ + exact .child ih htY h12 hi h3' + have hReach : ∀ {q}, VsReachA Pp Tb Y mem' ksb q → VsReachA Pp Tb Y mem ksb q := by + rintro q ⟨tag, sh, htag, hpth⟩ + exact ⟨tag, sh, htag, hPath hpth⟩ + refine ⟨?_, ?_, ?_, ?_, ?_, ?_, ?_, R.lv, R.al, R.pal, R.yal, R.ytb⟩ + · intro hi hh + rw [hw _ (.asid hi hh) (asNe hi hh)] + exact R.table hi hh + · intro hi hh h0 + rw [hw _ (.asid hi hh) (asNe hi hh)] at h0 ⊢ + exact R.tmem hi hh h0 + · intro p hp + obtain ⟨f, hk, hf⟩ := R.pool p hp + refine ⟨f, by rw [hko p (pNe p (Or.inl hp))]; exact hk, fun ha lo hl => ?_⟩ + rw [hw _ (.pool p lo hp hl) (poolNe p hp lo hl)] + exact hf (hActP ha) lo hl + · intro p hp ha lo hl + rw [hw _ (.pool p lo hp hl) (poolNe p hp lo hl)] + exact R.kind p hp (hActP ha) lo hl + · intro h0 + have h0' := h0 + rw [hw _ (.asid 0#u64 (by decide)) (asNe _ (by decide))] at h0' ⊢ + have hq := R.tmem 0#u64 (by decide) h0' + rw [hw _ (.pool _ 0#u64 hq (by decide)) (poolNe _ hq _ (by decide))] + exact R.asid0 h0' + · intro p hp + obtain ⟨t, hk, ht, he⟩ := R.pt p hp + refine ⟨t, by rw [hko p (pNe p (Or.inr hp))]; exact hk, ht, fun hr i hi => ?_⟩ + rw [hw _ (.pt p i hp hi) (pgNeE (R.al p hp) hP hi hj0 (fun e' => R.ytb y0 hy0 (e' ▸ hp)))] + exact he (hReach hr) i hi + · intro y hy + by_cases hyy : y = y0 + · subst hyy + refine ⟨_, hk1, fun ha i hi => ?_⟩ + show PteRelA Tb Lv 1 _ (if i.val = j0.val then .invalidPTE else l0 ⟨i.val, hi⟩) + by_cases hij : i.val = j0.val + · rw [if_pos hij, hm _ (peAF_alignedA (waWin_modA hal) hi), if_pos (by rw [UScalar.eq_of_val_eq hij])] + exact pteRelA_zero_inv Tb Lv 1 + · rw [if_neg hij, hw _ (.high y i hy hi) (fun e => hij (by + rw [(peAF_injA (waWin_modA hal) (waWin_modA hal) hi hj0 e).2]))] + exact he0 (hActH ha) i hi + · obtain ⟨l1, hk, he⟩ := R.high y hy + refine ⟨l1, by rw [hko y hyy]; exact hk, fun ha i hi => ?_⟩ + have hne : wa y 4096#u64 ≠ wa y0 4096#u64 := fun e => hyy (by + have e1 := congrArg UScalar.val e + rw [waWin_valA (R.yal y hy), waWin_valA hal] at e1 + exact UScalar.eq_of_val_eq (by omega)) + rw [hw _ (.high y i hy hi) (pgNeE (al4kHiE (R.yal y hy)) hP hi hj0 hne)] + exact he (hActH ha) i hi + +end hi + +/-! ## The zero write against `extSlotWriteX` -/ + +/-- **The VSpace pages' words are off the ASID-table block** (a lemma hypothesis, VS gen 4's `hT` widened to the +Stage-2 pages: A64 P1's `HeapOK` / `HeapKsOff` at integration). -/ +abbrev OffAsE (Pp Tb X Y T2 : Set U64) (ksb : U64) : Prop := + ∀ q j : U64, j.val < 512 → (q ∈ S2PgE X T2 ∨ q ∈ VsPtA Pp Tb Y) → ¬ AsW ksb (peAF q j) + +section wr +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {Pp Tb : Set U64} {Lv : U64 → Nat} {X Y T2 : Set U64} {Sh2 : U64 → Nat} {σ : Fl4SpecA64.State Unit} + +/-- **FL4's zero write at a Stage-2 slot** (module docstring). -/ +theorem writeS2E {s : S} (R : VsRelA Pp Tb Lv X Y (L.mem s) (L.ksb s) σ) + (E : ExtVsRelA Pp Tb X Y T2 Sh2 (L.mem s) σ) (hK : Aligned (L.ksb s)) (hT : OffAsE Pp Tb X Y T2 (L.ksb s)) + {a : U64} {sl : Fl4SpecA64.ExtSlot} {sh : Nat} (h : SlotRelE (G2E T2 Sh2) (TopS2E X) a sl sh) : + VsRelA Pp Tb Lv X Y (L.mem (L.wr s a 0#u64)) (L.ksb (L.wr s a 0#u64)) (extSlotWriteX σ sl .invalidPTE) ∧ + ExtVsRelA Pp Tb X Y T2 Sh2 (L.mem (L.wr s a 0#u64)) (extSlotWriteX σ sl .invalidPTE) := by + rw [L.ksb_wr] + rcases h with ⟨t, i, ⟨ht, -⟩, hi, rfl, rfl⟩ | ⟨r, j, hr, hj, rfl, rfl, -⟩ + · have ha : t.val % 4096 = 0 := Nat.mod_eq_zero_of_dvd (E.tal t ht) + have hm : ∀ b, Aligned b → L.mem (L.wr s (peAF t i) 0#u64) b = if peAF t i = b then 0#u64 else L.mem s b := + fun b hb => L.mem_wr _ _ _ _ (peAF_alignedA ha hi) hb + exact ⟨R.of_s2WriteE E (Or.inl ht) (E.tal t ht) hi (hT t i hi (Or.inl (Or.inl ht))) hK + (fun b hb hne => by rw [hm b hb, if_neg (Ne.symm hne)]) (Or.inl ht) (slotObjPtE ha hi) _, + E.of_ptClearE R.yal ht hi hm⟩ + · have hj1 : j.val < 1024 := by have := s2GeomNumE.2.2.1; omega + obtain ⟨q, j', hq, hj', e⟩ := rootWordE (E.xal r hr) hj1 + have hqa : 4096 ∣ q.val := by + rcases hq with h | h <;> rw [h] + · exact al4kE (E.xal r hr) + · exact al4kHiE (E.xal r hr) + have hqS : q ∈ S2PgE X T2 := Or.inr ⟨r, hr, hq⟩ + have hsa : Aligned (peAF r j) := by rw [e]; exact peAF_alignedA (Nat.mod_eq_zero_of_dvd hqa) hj' + have hm : ∀ b, Aligned b → L.mem (L.wr s (peAF r j) 0#u64) b = if peAF r j = b then 0#u64 else L.mem s b := + fun b hb => L.mem_wr _ _ _ _ hsa hb + refine ⟨R.of_s2WriteE E hqS hqa hj' (hT q j' hj' (Or.inl hqS)) hK + (fun b hb hne => by rw [hm b hb, if_neg (by rw [e]; exact Ne.symm hne)]) (q := r) + (Or.inr ⟨r, hr, Or.inl rfl⟩) (sl := .root (specPtr r) j.val) rfl _, E.of_rootClearE R.yal hr hj1 hm⟩ + +/-- **FL4's zero write at a high-window slot** (module docstring). -/ +theorem writeHiE {s : S} (R : VsRelA Pp Tb Lv X Y (L.mem s) (L.ksb s) σ) + (E : ExtVsRelA Pp Tb X Y T2 Sh2 (L.mem s) σ) (hK : Aligned (L.ksb s)) (hT : OffAsE Pp Tb X Y T2 (L.ksb s)) + {a : U64} {sl : Fl4SpecA64.ExtSlot} {sh : Nat} (h : SlotRelE (G1E Tb Lv) (TopHiE Y) a sl sh) : + VsRelA Pp Tb Lv X Y (L.mem (L.wr s a 0#u64)) (L.ksb (L.wr s a 0#u64)) (extSlotWriteX σ sl .invalidPTE) ∧ + ExtVsRelA Pp Tb X Y T2 Sh2 (L.mem (L.wr s a 0#u64)) (extSlotWriteX σ sl .invalidPTE) := by + rw [L.ksb_wr] + rcases h with ⟨t, i, ⟨ht, -, -⟩, hi, rfl, rfl⟩ | ⟨y, j, hy, hj, rfl, rfl, -⟩ + · obtain ⟨pt, hk, -, -⟩ := R.pt t ht + have ha := R.al_mod ht + have hsa := peAF_alignedA ha hi + have hm : ∀ b, Aligned b → L.mem (L.wr s (peAF t i) 0#u64) b = if peAF t i = b then 0#u64 else L.mem s b := + fun b hb => L.mem_wr _ _ _ _ hsa hb + have E1 := E.of_vsWriteE (mem' := L.mem (L.wr s (peAF t i) 0#u64)) R.yal (Or.inr (Or.inl ht)) (R.al t ht) hi + (fun b hb hne => by rw [hm b hb, if_neg (Ne.symm hne)]) (Or.inl ht) + (fun hp => by obtain ⟨_, h1, -⟩ := R.pool t hp; rw [hk] at h1; cases h1) (slotObjPtE ha hi) .invalidPTE + rw [extPtSetE hk ha hi] at E1 ⊢ + exact ⟨R.of_storePteA ht hi hk (pteRelA_zero_inv Tb Lv (Lv t)) (hT t i hi (Or.inr (Or.inr (Or.inl ht)))) + (fun b hb hne => by rw [hm b (R.foot_alignedA hK b hb), if_neg (Ne.symm hne)]) (by rw [hm _ hsa, if_pos rfl]) + (fun _ h => by cases h), + E1⟩ + · have hal := R.yal y hy + have hsa := peAF_alignedA (waWin_modA hal) hj + have hm : ∀ b, Aligned b → L.mem (L.wr s (peAF (wa y 4096#u64) j) 0#u64) b = + if peAF (wa y 4096#u64) j = b then 0#u64 else L.mem s b := fun b hb => L.mem_wr _ _ _ _ hsa hb + exact ⟨R.of_hiClearE E hK hy hj (hT _ j hj (Or.inr (Or.inr (Or.inr ⟨y, hy, rfl⟩)))) hm, + E.of_vsWriteE R.yal (Or.inr (Or.inr ⟨y, hy, rfl⟩)) (al4kHiE hal) hj + (fun b hb hne => by rw [hm b hb, if_neg (Ne.symm hne)]) (Or.inr hy) + (fun hp => E.ypg y hy (Or.inl hp)) rfl _⟩ + +end wr + +/-! ## Where the write lands (S3 gen 25) -/ + +/-- (S3 gen 25, ruling 22:14Z item 1) **Where an Ext unmap's zero write lands**: word `j < 512` of a page-aligned +page `q` that is a Stage-2 page (`S2PgE`), a window's level-1 page or a Stage-1 table of `Tb`; and the spec's outcome +changes only the heap, and there only the object `specPtr p0` of a Stage-2 root, a window, a Stage-2 table or a +Stage-1 table. -/ +def ExtWrE (Tb X Y T2 : Set U64) (σ : Fl4SpecA64.State Unit) (slot : U64) (σ' : Fl4SpecA64.State Unit) : Prop := + (∃ q j : U64, j.val < 512 ∧ slot = peAF q j ∧ 4096 ∣ q.val ∧ + (q ∈ S2PgE X T2 ∨ (∃ y ∈ Y, q = wa y 4096#u64) ∨ q ∈ Tb)) ∧ + ∃ p0 : U64, (p0 ∈ X ∨ p0 ∈ Y ∨ p0 ∈ T2 ∨ p0 ∈ Tb) ∧ + ∃ kh, σ' = { σ with kheap := kh } ∧ ∀ p, p ≠ specPtr p0 → kh p = σ.kheap p + +section wrl +variable {Pp Tb : Set U64} {Lv : U64 → Nat} {X Y T2 : Set U64} {Sh2 : U64 → Nat} {σ : Fl4SpecA64.State Unit} + +/-- (S3 gen 25) **A Stage-2 slot's write lands in a Stage-2 page and changes its table or root.** -/ +theorem wrS2E {mem : U64 → U64} (E : ExtVsRelA Pp Tb X Y T2 Sh2 mem σ) {a : U64} {sl : Fl4SpecA64.ExtSlot} + {sh : Nat} (h : SlotRelE (G2E T2 Sh2) (TopS2E X) a sl sh) : + ExtWrE Tb X Y T2 σ a (extSlotWriteX σ sl .invalidPTE) := by + rcases h with ⟨t, i, ⟨ht, -⟩, hi, rfl, rfl⟩ | ⟨r, j, hr, hj, rfl, rfl, -⟩ + · refine ⟨⟨t, i, hi, rfl, E.tal t ht, Or.inl (Or.inl ht)⟩, t, Or.inr (Or.inr (Or.inl ht)), + extSlotKhX σ _ .invalidPTE, rfl, fun p hp => kheap_writeX (σ := σ) (sl := .pt (specPtr (peAF t i))) (pte := .invalidPTE) (x := p) ?_⟩ + rw [slotObjPtE (Nat.mod_eq_zero_of_dvd (E.tal t ht)) hi] + exact hp + · have hj1 : j.val < 1024 := by have := s2GeomNumE.2.2.1; omega + obtain ⟨q, j', hq, hj', e⟩ := rootWordE (E.xal r hr) hj1 + refine ⟨⟨q, j', hj', e, ?_, Or.inl (Or.inr ⟨r, hr, hq⟩)⟩, r, Or.inl hr, extSlotKhX σ _ .invalidPTE, rfl, + fun p hp => kheap_writeX (σ := σ) (sl := .root (specPtr r) j.val) (pte := .invalidPTE) (x := p) hp⟩ + rcases hq with h | h <;> rw [h] + · exact al4kE (E.xal r hr) + · exact al4kHiE (E.xal r hr) + +/-- (S3 gen 25) **A window slot's write lands in a window's level-1 page or a Stage-1 table below it.** -/ +theorem wrHiE {mem : U64 → U64} {ksb : U64} (R : VsRelA Pp Tb Lv X Y mem ksb σ) {a : U64} + {sl : Fl4SpecA64.ExtSlot} {sh : Nat} (h : SlotRelE (G1E Tb Lv) (TopHiE Y) a sl sh) : + ExtWrE Tb X Y T2 σ a (extSlotWriteX σ sl .invalidPTE) := by + rcases h with ⟨t, i, ⟨ht, -, -⟩, hi, rfl, rfl⟩ | ⟨y, j, hy, hj, rfl, rfl, -⟩ + · refine ⟨⟨t, i, hi, rfl, R.al t ht, Or.inr (Or.inr ht)⟩, t, Or.inr (Or.inr (Or.inr ht)), + extSlotKhX σ _ .invalidPTE, rfl, fun p hp => kheap_writeX (σ := σ) (sl := .pt (specPtr (peAF t i))) (pte := .invalidPTE) (x := p) ?_⟩ + rw [slotObjPtE (R.al_mod ht) hi] + exact hp + · exact ⟨⟨_, j, hj, rfl, al4kHiE (R.yal y hy), Or.inr (Or.inl ⟨y, hy, rfl⟩)⟩, y, Or.inr (Or.inl hy), + extSlotKhX σ _ .invalidPTE, rfl, + fun p hp => kheap_writeX (σ := σ) (sl := .root (specPtr y) j.val) (pte := .invalidPTE) (x := p) hp⟩ + +end wrl + +/-! ## The post -/ + +/-- **The post of the Ext unmaps** (module docstring); `fk` is FL4's continuation after the write (its flush). -/ +abbrev ExtUnmapRelE (Pp Tb : Set U64) (Lv : U64 → Nat) (X Y T2 : Set U64) (Sh2 : U64 → Nat) + (σ : Fl4SpecA64.State Unit) {S A : Type} (st : machine.Store S) [L : LawfulStore st] (s : S) (a : A) + (fk : S → Result (S × A)) (r : Result (S × A)) (W : Set (Unit × Fl4SpecA64.State Unit) × Prop) : Prop := + (r = ok (s, a) ∧ W = ({((), σ)}, False)) ∨ + ∃ slot σ', r = fk (L.wr s slot 0#u64) ∧ W = ({((), σ')}, False) ∧ + VsRelA Pp Tb Lv X Y (L.mem (L.wr s slot 0#u64)) (L.ksb (L.wr s slot 0#u64)) σ' ∧ + ExtVsRelA Pp Tb X Y T2 Sh2 (L.mem (L.wr s slot 0#u64)) σ' ∧ ExtWrE Tb X Y T2 σ slot σ' + +section spec +variable {σ : Fl4SpecA64.State Unit} {k : Fl4SpecA64.ExtRootKind} {r : Fl4SpecA64.ObjRef} + {asid : Fl4SpecA64.Asid} {va : Fl4SpecA64.Word} + +/-- The spec's page unmap from the lookup's position `W`: no-op unless the frame is there, else `W`'s slot +cleared. -/ +theorem extPageStE {pptr : Fl4SpecA64.ObjRef} {sz : Fl4SpecA64.VMPageSize} {W : Fl4SpecA64.ExtPos} + (hg : Fl4SpecA64.extVaOk k va) (hv : Fl4SpecA64.vspaceForAsid asid σ = some r) (hk : Fl4SpecA64.rootKindAt σ r = k) + (hW : Fl4SpecA64.extLookupSlot σ k.geom r va = W) : + (¬ (W.shift = Fl4SpecA64.pageBitsForSize sz ∧ (Fl4SpecA64.ExtSlot.read σ W.slot).isPagePTE = true ∧ + Fl4SpecA64.pptrFromPte (Fl4SpecA64.ExtSlot.read σ W.slot) = pptr) → + Fl4SpecA64.extUnmapPage (Z := Unit) k sz asid va pptr σ = ({((), σ)}, False)) ∧ + ((W.shift = Fl4SpecA64.pageBitsForSize sz ∧ (Fl4SpecA64.ExtSlot.read σ W.slot).isPagePTE = true ∧ + Fl4SpecA64.pptrFromPte (Fl4SpecA64.ExtSlot.read σ W.slot) = pptr) → + Fl4SpecA64.extUnmapPage (Z := Unit) k sz asid va pptr σ = ({((), extSlotWriteX σ W.slot .invalidPTE)}, False)) := by + subst hW + refine ⟨fun h => ?_, fun h => ?_⟩ + · rw [extUnmapPage_runX, extUnmapPageStX_missX (fun _ r' hv' hk' => by + rw [hv] at hv'; cases hv'; exact extMappedEntry_noneX h)] + · rw [extUnmapPage_runX, extUnmapPageStX_hitX hg hv hk (extMappedEntry_of_condX h)] + +/-- The spec's table unmap from the search's result. -/ +theorem extTableStE {pt : Fl4SpecA64.ObjRef} {Wt : Option Fl4SpecA64.ExtSlot} + (hv : Fl4SpecA64.vspaceForAsid asid σ = some r) (hk : Fl4SpecA64.rootKindAt σ r = k) + (hW : Fl4SpecA64.extFindTableSlot σ k.geom r va pt = Wt) : + (Wt = none → Fl4SpecA64.extUnmapTable (Z := Unit) k asid va pt σ = ({((), σ)}, False)) ∧ + (∀ sl, Wt = some sl → + Fl4SpecA64.extUnmapTable (Z := Unit) k asid va pt σ = ({((), extSlotWriteX σ sl .invalidPTE)}, False)) := by + subst hW + refine ⟨fun h => ?_, fun sl h => ?_⟩ + · rw [extUnmapTable_runX, extUnmapTableStX_missX (fun r' hv' hk' => by rw [hv] at hv'; cases hv'; exact h)] + · rw [extUnmapTable_runX, extUnmapTableStX_hitX hv hk h] + +theorem extUnmapPage_errE {pptr : Fl4SpecA64.ObjRef} {sz : Fl4SpecA64.VMPageSize} {e : Fl4SpecA64.LookupFailure} + (h : extRootResX σ k asid = .error e) : + Fl4SpecA64.extUnmapPage (Z := Unit) k sz asid va pptr σ = ({((), σ)}, False) := by + rw [extUnmapPage_runX, extUnmapPageStX_noneX (by simp only [extPageSlotX, h, ite_self])] + +theorem extUnmapTable_errE {pt : Fl4SpecA64.ObjRef} {e : Fl4SpecA64.LookupFailure} + (h : extRootResX σ k asid = .error e) : + Fl4SpecA64.extUnmapTable (Z := Unit) k asid va pt σ = ({((), σ)}, False) := by + rw [extUnmapTable_runX, extUnmapTableStX_noneX (by simp only [extTableSlotX, h])] + +end spec + +/-! ## The tails -/ + +section tails +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {Pp Tb : Set U64} {Lv : U64 → Nat} {X Y T2 : Set U64} {Sh2 : U64 → Nat} {σ : Fl4SpecA64.State Unit} +variable {G : U64 → Nat → Prop} {Top : U64 → Fl4SpecA64.ExtSlot → Nat → Prop} + +/-- **The frame check and write after the lookup** (`ept::unmap` / `unmap_page` after `lookup_slot`): against the +spec's `mapped_entry` check and `storeExtSlot`. -/ +theorem pageTailE {A : Type} {s : S} {a : A} (fk : S → Result (S × A)) (hG : GoodTabE G (L.mem s) σ) + (hTo : TopOkE G Top (L.mem s) σ) + (hwr : ∀ x sl sh, SlotRelE G Top x sl sh → + VsRelA Pp Tb Lv X Y (L.mem (L.wr s x 0#u64)) (L.ksb (L.wr s x 0#u64)) (extSlotWriteX σ sl .invalidPTE) ∧ + ExtVsRelA Pp Tb X Y T2 Sh2 (L.mem (L.wr s x 0#u64)) (extSlotWriteX σ sl .invalidPTE)) + (hwe : ∀ x sl sh, SlotRelE G Top x sl sh → ExtWrE Tb X Y T2 σ x (extSlotWriteX σ sl .invalidPTE)) + {k : Fl4SpecA64.ExtRootKind} {r : Fl4SpecA64.ObjRef} {asid : Fl4SpecA64.Asid} {va pptr : U64} + {sz : capability.FrameSize} (hg : Fl4SpecA64.extVaOk k va.bv) (hv : Fl4SpecA64.vspaceForAsid asid σ = some r) + (hk : Fl4SpecA64.rootKindAt σ r = k) + {W : Fl4SpecA64.ExtPos} (hW : Fl4SpecA64.extLookupSlot σ k.geom r va.bv = W) {lsh lsl : U64} + (hp : WkPostE G Top W (lsh, lsl)) : + ExtUnmapRelE Pp Tb Lv X Y T2 Sh2 σ st s a fk + (if lsh != psA sz then ok (s, a) else do + let e ← st.read s lsl + let b ← vspace.is_page e lsh + if b then do + let i1 ← vspace.page_base e lsh + let i2 ← layout.paddr pptr + if i1 != i2 then ok (s, a) else do + let s1 ← st.write s lsl 0#u64 + fk s1 + else ok (s, a)) + (Fl4SpecA64.extUnmapPage (Z := Unit) k (frameSizeOfA sz) asid va.bv (specPtr pptr) σ) := by + obtain ⟨hWs, hsr⟩ := hp + simp only at hWs hsr + obtain ⟨h12, h39, hd⟩ := descAtE hG hTo hsr + rw [hWs] at h12 h39 hd + have hm := pageMatchE hd pptr + have hspec := extPageStE (sz := frameSizeOfA sz) (pptr := specPtr pptr) hg hv hk hW + rw [hWs] at hspec + by_cases hs : lsh = psA sz + swap + · rw [if_pos (bne_iff_ne.2 hs)] + exact Or.inl ⟨rfl, hspec.1 fun h => hs (UScalar.eq_of_val_eq (h.1.trans (pbfs_eqA sz)))⟩ + rw [if_neg (by rw [hs, bne_self_eq_false]; exact Bool.false_ne_true), L.read_eq, bind_ok, is_pageE, bind_ok] + by_cases hb : isPgE (L.mem s lsl) lsh.val = true + swap + · rw [if_neg hb] + exact Or.inl ⟨rfl, hspec.1 fun h => hb (hm.1 h.2).1⟩ + rw [if_pos hb, page_baseE _ _ (by omega), bind_ok, + show layout.paddr pptr = ok (core.num.U64.wrapping_sub pptr layout.PPTR_BASE) from rfl, bind_ok] + by_cases hq : (⟨baseShE (L.mem s lsl) lsh.val⟩ : U64) = core.num.U64.wrapping_sub pptr layout.PPTR_BASE + swap + · rw [if_pos (bne_iff_ne.2 hq)] + exact Or.inl ⟨rfl, hspec.1 fun h => hq (hm.1 h.2).2⟩ + rw [if_neg (by rw [hq, bne_self_eq_false]; exact Bool.false_ne_true), L.write_eq, bind_ok] + obtain ⟨R1, E1⟩ := hwr _ _ _ hsr + exact Or.inr ⟨lsl, _, rfl, hspec.2 ⟨by rw [hs, pbfs_eqA], hm.2 ⟨hb, hq⟩⟩, R1, E1, hwe _ _ _ hsr⟩ + +/-- **The write after the table search** (`ept::unmap_table` / `unmap_page_table` after their loops). -/ +theorem tableTailE {A : Type} {s : S} {a : A} (fk : S → Result (S × A)) + (hwr : ∀ x sl sh, SlotRelE G Top x sl sh → + VsRelA Pp Tb Lv X Y (L.mem (L.wr s x 0#u64)) (L.ksb (L.wr s x 0#u64)) (extSlotWriteX σ sl .invalidPTE) ∧ + ExtVsRelA Pp Tb X Y T2 Sh2 (L.mem (L.wr s x 0#u64)) (extSlotWriteX σ sl .invalidPTE)) + (hwe : ∀ x sl sh, SlotRelE G Top x sl sh → ExtWrE Tb X Y T2 σ x (extSlotWriteX σ sl .invalidPTE)) + {k : Fl4SpecA64.ExtRootKind} {r : Fl4SpecA64.ObjRef} {asid : Fl4SpecA64.Asid} {va pt : U64} + (hv : Fl4SpecA64.vspaceForAsid asid σ = some r) (hk : Fl4SpecA64.rootKindAt σ r = k) + {Wt : Option Fl4SpecA64.ExtSlot} (hW : Fl4SpecA64.extFindTableSlot σ k.geom r va.bv (specPtr pt) = Wt) + {res : Option U64} (hp : FdPostE G Top Wt res) : + ExtUnmapRelE Pp Tb Lv X Y T2 Sh2 σ st s a fk + (match (generalizing := false) res with + | none => ok (s, a) + | some slot => do + let s1 ← st.write s slot 0#u64 + fk s1) + (Fl4SpecA64.extUnmapTable (Z := Unit) k asid va.bv (specPtr pt) σ) := by + have hspec := extTableStE hv hk hW + cases res with + | none => exact Or.inl ⟨rfl, hspec.1 hp⟩ + | some x => + obtain ⟨sl, sh, hWt, hsr⟩ := hp + obtain ⟨R1, E1⟩ := hwr _ _ _ hsr + refine Or.inr ⟨x, _, ?_, hspec.2 sl hWt, R1, E1, hwe _ _ _ hsr⟩ + simp only [L.write_eq, bind_ok] <;> rfl + +end tails + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/ExtVsWalkA.lean b/proofs/Fl4ProofsA64/P3/ExtVsWalkA.lean new file mode 100644 index 000000000..e687e1254 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ExtVsWalkA.lean @@ -0,0 +1,668 @@ +/- +P3 (aarch64, A64-P3 ExtVsA gen 1): **FL4's Stage-2 and high-window walks against `Ext/Stage2`'s** — the +correspondences of the `ExtVsA` line on top of `ExtVsRelA`. + +* **FL4's words** (`ept.rs`): `ept::is_table` / `table_of` / `is_leaf` / `leaf_base` are `vspace.rs`'s + (`ept_is_tableE`, `ept_table_ofE`, `ept_is_leafE`, `ept_leaf_baseE`), `ept::entry_addr` with mask `2^k - 1` is + `peAF t (idxME v sh k)` (`ept_entry_addrE`); so **`ept::lookup_slot`'s loop is `vspace::lookup_pt_slot`'s** + (`ept_lookup_loopE`). `vs_is_tableE` (`is_table` at any shift), `isPgE` / `is_pageE` (`is_page` at a shift), + `ept_geomE` (`ept::geom` is `s2GeomOf`). +* **Slots** (`SlotRelE G Top a sl sh`): FL4's word `a` is the spec's slot `sl` — a word of a `G` table (the spec's + `.pt` slot at its shifted pointer) or a top slot (`Top`: a root's entry). `TopOkE`: the top slots' entries are + `DescRelE`. Instances: `TopS2E X` (a Stage-2 root's entry `j < topMask + 1`) with `topOkS2E`, `TopHiE Y` (a + window's level-1 entry, word `y + 4096 + 8j`) with `topOkHiE`. +* **One step** (`descStepE`): FL4's `is_table` at a related descriptor is the spec's `extIsTable`, and a table + descriptor is `PageTablePTE (ppnA w)` naming the `G` table `table_of w` one level down, whose slot of `va` is the + spec's `extNextSlot` (`nextSlotE`). `pageMatchE`: FL4's `is_page ∧ page_base = paddr(pptr)` is the spec's + `isPagePTE ∧ pptrFromPte = pptr` (x86 `EptUnmapCore`'s leaf check; Stage-1 twin VS gen 4's `pageMatchA`). +* **The walks**: **`lookup_loopE`** — FL4's `lookup_pt_slot` loop (hence `ept::lookup_slot`'s) against `extWalkN`, + with an optional first step through a window's level-0 entry (`P0`: the high window's walk starts at FL4's level + 0, the spec's at the level-1 table); **`unmap_pt_loopE`** — `vspace::unmap_page_table`'s loop from a window's + level-1 table against `extFindTableN`; **`ept_unmap_table_loopE`** — `ept::unmap_table`'s loop from a Stage-2 + root (mask `topMask`, then 511) against `extFindTableN`. Twins of VS gen 3's `lookup_pt_slot_loopA` / + `unmap_page_table_loopA` (x86: `EptUnmapCore.lookup_of_path` / `guestUnmap_reach`). +* **Root tags** (`rawKindE`): a raw ASID lookup's `Ok w` under `VsRelA` is tag `ROOT_S1` / `ROOT_S2` / + `ROOT_HIGH` exactly when l4v's `vspace_for_asid` names a `stage1` / `stage2` / `high` root (`rootKindAt`), the + untagged word shifted; `tag1E` / `tag2E` (FL4's `r & ROOT_TAG` tests); with `extRootResX` + (`ExtVsSpecA`) this is FL4's `ept::find` / `ROOT_HIGH` branch against the spec's `findExtRoot` (`ept_findE`). + +Hypotheses: `VsRelA` / `ExtVsRelA` (through `GoodTabE` / `TopOkE`), `hX` (`Stage2CoreA.obj`). No named premise. No +`Fl4SpecA64` edit. +-/ +import Fl4ProofsA64.P3.ExtVsRelA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add +local notation "wm" => core.num.U64.wrapping_mul + +/-! ## FL4's words -/ + +theorem pageBits12E : vspace.PAGE_BITS = 12#u64 := by unfold vspace.PAGE_BITS; rfl + +/-- `is_table` at any shift. -/ +theorem vs_is_tableE (e sh : U64) : + vspace.is_table e sh = ok (decide (12 < sh.val ∧ (e &&& 3#u64) = 3#u64)) := by + by_cases h : 12 < sh.val + · rw [is_table_eqA e sh h]; simp [h] + · unfold vspace.is_table + rw [pageBits12E, if_neg (by scalar_tac)] + simp [h] + +theorem ept_is_tableE (e sh : U64) : ept.is_table e sh = vspace.is_table e sh := by + unfold ept.is_table vspace.is_table + rw [eq_of_spec_fd P0.Consts.ept.D_VALID.spec, eq_of_spec_fd P0.Consts.ept.D_TABLE_OR_PAGE.spec, + eq_of_spec_fd P0.Consts.vspace.D_VALID.spec, eq_of_spec_fd P0.Consts.vspace.D_TABLE_OR_PAGE.spec, + show ept.PAGE_BITS = vspace.PAGE_BITS by unfold ept.PAGE_BITS vspace.PAGE_BITS; rfl] + +theorem ept_is_leafE (e sh : U64) : ept.is_leaf e sh = vspace.is_page e sh := by + unfold ept.is_leaf vspace.is_page + rw [eq_of_spec_fd P0.Consts.ept.D_VALID.spec, eq_of_spec_fd P0.Consts.ept.D_TABLE_OR_PAGE.spec, + eq_of_spec_fd P0.Consts.vspace.D_VALID.spec, eq_of_spec_fd P0.Consts.vspace.D_TABLE_OR_PAGE.spec, + show ept.PAGE_BITS = vspace.PAGE_BITS by unfold ept.PAGE_BITS vspace.PAGE_BITS; rfl] + +theorem ept_table_ofE (e : U64) : ept.table_of e = vspace.table_of e := by + unfold ept.table_of vspace.table_of ept.ADDR_MASK vspace.ADDR_MASK; rfl + +theorem ept_leaf_baseE (e sh : U64) : ept.leaf_base e sh = vspace.page_base e sh := by + unfold ept.leaf_base vspace.page_base ept.ADDR_MASK vspace.ADDR_MASK; rfl + +theorem ept_entry511E (t v sh : U64) : ept.entry_addr t v sh 511#u64 = vspace.entry_addr t v sh := by + unfold ept.entry_addr vspace.entry_addr; rfl + +/-- The `k`-bit index of `v` at shift `sh` (`(v >> sh) & (2^k - 1)`). -/ +def idxME (v sh : U64) (k : Nat) : U64 := ⟨BitVec.ofNat 64 (v.val / 2 ^ sh.val % 2 ^ k)⟩ + +theorem idxME_val (v sh : U64) {k : Nat} (hk : k ≤ 10) : (idxME v sh k).val = v.val / 2 ^ sh.val % 2 ^ k := by + show (BitVec.ofNat 64 _).toNat = _ + rw [BitVec.toNat_ofNat] + apply Nat.mod_eq_of_lt + have h1 := Nat.mod_lt (v.val / 2 ^ sh.val) (Nat.two_pow_pos k) + have h2 : 2 ^ k ≤ 2 ^ 10 := Nat.pow_le_pow_right (by decide) hk + simp only [Nat.reducePow] at h2 ⊢ + omega + +theorem idxME9 (v sh : U64) : idxME v sh 9 = idxA v sh := rfl + +/-- **`ept::entry_addr`** with a mask `2^k - 1`. -/ +theorem ept_entry_addrE (t v sh m : U64) {k : Nat} (hs : sh.val < 64) (hm : m.val = 2 ^ k - 1) : + ept.entry_addr t v sh m = ok (peAF t (idxME v sh k)) := by + unfold ept.entry_addr + simp only [lift, bind_ok] + have e63 : (sh &&& 63#u64) = sh := + UScalar.eq_of_val_eq (by rw [and_maskA sh 6 _ rfl]; exact Nat.mod_eq_of_lt (by simp only [Nat.reducePow]; omega)) + rw [e63] + obtain ⟨i, ei, vi⟩ := shrA v sh (2 ^ sh.val) hs rfl + rw [ei] + simp only [bind_ok] + have e : (i &&& m) = idxME v sh k := by + apply UScalar.eq_of_val_eq + rw [and_maskA i k m hm, vi] + show _ = (BitVec.ofNat 64 _).toNat + rw [BitVec.toNat_ofNat] + refine (Nat.mod_eq_of_lt ?_).symm + have h1 : v.val / 2 ^ sh.val ≤ v.val := Nat.div_le_self _ _ + have h2 := Nat.mod_le (v.val / 2 ^ sh.val) (2 ^ k) + have h3 : v.val < 2 ^ 64 := v.bv.isLt + omega + rw [e]; rfl + +theorem ept_lookup_bodyE {S : Type} (st : machine.Store S) (s : S) (ipa sh sl rem : U64) : + ept.lookup_slot_loop.body st s ipa sh sl rem = vspace.lookup_pt_slot_loop.body st s ipa sh sl rem := by + unfold ept.lookup_slot_loop.body vspace.lookup_pt_slot_loop.body + simp only [ept_is_tableE, ept_table_ofE, ept_entry511E] + +/-- **`ept::lookup_slot`'s loop is `lookup_pt_slot`'s** (module docstring). -/ +theorem ept_lookup_loopE {S : Type} (st : machine.Store S) (s : S) (ipa sh sl rem : U64) : + ept.lookup_slot_loop st s ipa sh sl rem = vspace.lookup_pt_slot_loop st s ipa sh sl rem := by + unfold ept.lookup_slot_loop vspace.lookup_pt_slot_loop + congr 1 + funext x + obtain ⟨a, b, c⟩ := x + exact ept_lookup_bodyE st s ipa a b c + +/-- **`ept::geom` is `s2GeomOf`.** -/ +theorem ept_geomE (n : U64) : ∃ g, ept.geom n = ok g ∧ g.top_shift.val = (Fl4SpecA64.s2GeomOf n.val).topShift ∧ + g.top_mask.val = (Fl4SpecA64.s2GeomOf n.val).topMask ∧ g.levels.val = (Fl4SpecA64.s2GeomOf n.val).levels := by + unfold ept.geom Fl4SpecA64.s2GeomOf + by_cases h1 : n.val ≤ 42 + · rw [if_pos (by scalar_tac), if_pos h1]; exact ⟨_, rfl, rfl, rfl, rfl⟩ + · rw [if_neg (by scalar_tac), if_neg h1] + by_cases h2 : n.val = 44 + · rw [if_pos (UScalar.eq_of_val_eq (by rw [h2]; rfl)), if_pos h2]; exact ⟨_, rfl, rfl, rfl, rfl⟩ + · rw [if_neg (fun e => h2 (by rw [e]; rfl)), if_neg h2]; exact ⟨_, rfl, rfl, rfl, rfl⟩ + +/-- The Stage-2 geometry's numbers: top shift 30 or 39, mask `2^k - 1` with `k ∈ {10, 5, 9}`, 3 or 4 levels. -/ +theorem s2GeomNumE : (Fl4SpecA64.s2Geom.topShift = 30 ∨ Fl4SpecA64.s2Geom.topShift = 39) ∧ + (∃ k, k ≤ 10 ∧ Fl4SpecA64.s2Geom.topMask = 2 ^ k - 1) ∧ Fl4SpecA64.s2Geom.topMask + 1 ≤ 1024 ∧ + (Fl4SpecA64.s2Geom.levels = 3 ∨ Fl4SpecA64.s2Geom.levels = 4) ∧ + (Fl4SpecA64.s2Geom.topShift = 30 ↔ Fl4SpecA64.s2Geom.levels = 3) := by + rcases s2Geom_casesX with h | h | h <;> rw [h] + · exact ⟨Or.inl rfl, ⟨10, by decide, rfl⟩, by decide, Or.inl rfl, by decide⟩ + · exact ⟨Or.inr rfl, ⟨5, by decide, rfl⟩, by decide, Or.inr rfl, by decide⟩ + · exact ⟨Or.inr rfl, ⟨9, by decide, rfl⟩, by decide, Or.inr rfl, by decide⟩ + +/-! ## Descriptor steps -/ + +/-- l4v-side read of a related table's slot. -/ +theorem readPtE {σ : Fl4SpecA64.State Unit} {t i : U64} {pt : Fl4SpecA64.PT} + (hk : σ.kheap (specPtr t) = some (.archObj (.pageTable pt))) (hty : Fl4SpecA64.ptType pt = .normalPtT) + (ha : t.val % 4096 = 0) (hi : i.val < 512) : + Fl4SpecA64.ExtSlot.read σ (.pt (specPtr (peAF t i))) = Fl4SpecA64.ptApply pt i.bv := by + show (Fl4SpecA64.ptesOf σ .normalPtT (specPtr (peAF t i))).getD .invalidPTE = _ + rw [ptesOf_atA hk hty ha hi]; rfl + +/-- **One step at a related descriptor** (module docstring). -/ +theorem descStepE {G : U64 → Nat → Prop} {sh : Nat} {w : U64} {pte : Fl4SpecA64.PTE} (hd : DescRelE G sh w pte) : + (Fl4SpecA64.extIsTable pte sh = true ↔ (12 < sh ∧ (w &&& 3#u64) = 3#u64)) ∧ + (Fl4SpecA64.extIsTable pte sh = true → + pte = .pageTablePTE (ppnA w) ∧ G (dTabA w) (sh - 9) ∧ (dAddrA w).val < 2 ^ 44) := by + rw [extIsTable_iffX] + cases pte with + | invalidPTE => + exact ⟨⟨fun h => absurd h.2 Bool.false_ne_true, + fun h => absurd (hd.symm.trans ((and3_bitsA w).1 h.2).1) Bool.false_ne_true⟩, + fun h => absurd h.2 Bool.false_ne_true⟩ + | pageTablePTE ppn => + obtain ⟨h12, h0, h1, h44, hp, hG⟩ := hd + exact ⟨⟨fun _ => ⟨h12, (and3_bitsA w).2 ⟨h0, h1⟩⟩, fun _ => ⟨h12, rfl⟩⟩, fun _ => ⟨by rw [hp], hG, h44⟩⟩ + | pagePTE a sm at' rt => + obtain ⟨h0, h1, -, -⟩ := hd + exact ⟨⟨fun h => absurd h.2 Bool.false_ne_true, + fun ⟨h12, h3⟩ => absurd (h1.1 ((and3_bitsA w).1 h3).2) (by omega)⟩, + fun h => absurd h.2 Bool.false_ne_true⟩ + +theorem nextOffE (va s9 : U64) : + ((va.bv >>> s9.val) &&& Fl4SpecA64.mask 9) <<< Fl4SpecA64.pteBits = (wm (idxA va s9) 8#u64).bv := by + apply BitVec.eq_of_toNat_eq + rw [pteBits3, BitVec.toNat_shiftLeft, BitVec.toNat_and, BitVec.toNat_ushiftRight, mask9_toNatA] + show _ = (wm (idxA va s9) 8#u64).val + rw [wm8_valA (idxA_lt va s9), idxA_val, Nat.shiftRight_eq_div_pow, Nat.shiftLeft_eq, + show (511 : Nat) = 2 ^ 9 - 1 from rfl, Nat.and_two_pow_sub_one_eq_mod] + have e : va.bv.toNat = va.val := rfl + rw [e] + generalize va.val / 2 ^ s9.val = x + simp only [Nat.reducePow] + omega + +/-- **The spec's next slot is FL4's** (module docstring). -/ +theorem nextSlotE (va : U64) {w : U64} (h44 : (dAddrA w).val < 2 ^ 44) {sh : Nat} (s9 : U64) (hs9 : s9.val = sh - 9) : + Fl4SpecA64.extNextSlot va.bv (.pageTablePTE (ppnA w)) sh = .pt (specPtr (peAF (dTabA w) (idxA va s9))) := by + unfold Fl4SpecA64.extNextSlot + rw [pptrFromPte_tabA w h44, ptTB9X, ← hs9, nextOffE va s9] + unfold peAF + rw [specPtr_wa] + +/-- FL4's `is_page` at a shift. -/ +def isPgE (w : U64) (sh : Nat) : Bool := isPageA w (if sh = 12 then 3 else 0) + +theorem is_pageE (e sh : U64) : vspace.is_page e sh = ok (isPgE e sh.val) := by + rw [is_page_eqA] + unfold isPgE isPageA + by_cases h : sh.val = 12 + · rw [if_pos (UScalar.eq_of_val_eq (by rw [h]; rfl)), if_pos h, if_pos rfl] + · rw [if_neg (fun e => h (by rw [e]; rfl)), if_neg h, if_neg (by decide)] + +theorem page_baseE (e sh : U64) (hs : sh.val < 64) : vspace.page_base e sh = ok ⟨baseShE e sh.val⟩ := + page_base_eqA e sh hs + +/-- **The leaf check** (module docstring). -/ +theorem pageMatchE {G : U64 → Nat → Prop} {sh : Nat} {w : U64} {pte : Fl4SpecA64.PTE} (hd : DescRelE G sh w pte) + (pptr : U64) : + (pte.isPagePTE = true ∧ Fl4SpecA64.pptrFromPte pte = specPtr pptr) ↔ + (isPgE w sh = true ∧ (⟨baseShE w sh⟩ : U64) = core.num.U64.wrapping_sub pptr layout.PPTR_BASE) := by + unfold isPgE + rw [isPageA_iff] + cases pte with + | invalidPTE => + have h0 : dBitA w 0 = false := hd + constructor + · rintro ⟨h, -⟩; exact absurd h Bool.false_ne_true + · rintro ⟨⟨h, -⟩, -⟩; rw [h0] at h; exact absurd h Bool.false_ne_true + | pageTablePTE ppn => + obtain ⟨h12, -, hb1, -⟩ := hd + constructor + · rintro ⟨h, -⟩; exact absurd h Bool.false_ne_true + · rintro ⟨⟨-, hi⟩, -⟩ + have := hi.1 hb1 + split at this <;> omega + | pagePTE pa sm pat prt => + obtain ⟨hb0, hb1, ha, -⟩ := hd + have hpp : Fl4SpecA64.pptrFromPte (.pagePTE pa sm pat prt) = specPtr ⟨pa + fl4PptrBaseA⟩ := ptrFromPAddr_eq pa + rw [hpp, specPtr_iff, ha, paddr_iffA] + have hb1' : (dBitA w 1 = true ↔ (if sh = 12 then 3 else 0) = 3) := by + rw [hb1]; split <;> simp_all + exact ⟨fun h => ⟨⟨hb0, hb1'⟩, h.2⟩, fun h => ⟨rfl, h.2⟩⟩ + +/-! ## Slots -/ + +/-- **FL4's word `a` is the spec's slot `sl` at shift `sh`** (module docstring). -/ +abbrev SlotRelE (G : U64 → Nat → Prop) (Top : U64 → Fl4SpecA64.ExtSlot → Nat → Prop) (a : U64) + (sl : Fl4SpecA64.ExtSlot) (sh : Nat) : Prop := + (∃ t i : U64, G t sh ∧ i.val < 512 ∧ a = peAF t i ∧ sl = .pt (specPtr (peAF t i))) ∨ Top a sl sh + +/-- The top slots' entries are related. -/ +abbrev TopOkE (G : U64 → Nat → Prop) (Top : U64 → Fl4SpecA64.ExtSlot → Nat → Prop) (mem : U64 → U64) + (σ : Fl4SpecA64.State Unit) : Prop := + ∀ a sl sh, Top a sl sh → 12 ≤ sh ∧ sh ≤ 39 ∧ DescRelE G sh (mem a) (Fl4SpecA64.ExtSlot.read σ sl) + +/-- A Stage-2 root's entry `j` (`j < topMask + 1`). -/ +abbrev TopS2E (X : Set U64) (a : U64) (sl : Fl4SpecA64.ExtSlot) (sh : Nat) : Prop := + ∃ r j : U64, r ∈ X ∧ j.val < Fl4SpecA64.s2Geom.topMask + 1 ∧ a = peAF r j ∧ sl = .root (specPtr r) j.val ∧ + sh = Fl4SpecA64.s2Geom.topShift + +/-- An active Stage-2 root's entry `j` (`j < topMask + 1`). -/ +abbrev TopS2ReachE (Pp X : Set U64) (σ : Fl4SpecA64.State Unit) (a : U64) (sl : Fl4SpecA64.ExtSlot) + (sh : Nat) : Prop := + ∃ r j : U64, r ∈ X ∧ ActiveS2RootA Pp X σ r ∧ j.val < Fl4SpecA64.s2Geom.topMask + 1 ∧ a = peAF r j ∧ + sl = .root (specPtr r) j.val ∧ sh = Fl4SpecA64.s2Geom.topShift + +/-- A window's level-1 entry `j` (word `y + 4096 + 8j`). -/ +abbrev TopHiE (Y : Set U64) (a : U64) (sl : Fl4SpecA64.ExtSlot) (sh : Nat) : Prop := + ∃ y j : U64, y ∈ Y ∧ j.val < 512 ∧ a = peAF (wa y 4096#u64) j ∧ sl = .root (specPtr y) j.val ∧ sh = 30 + +/-- An active window's level-1 entry `j` (word `y + 4096 + 8j`). -/ +abbrev TopHiReachE (Pp Y : Set U64) (mem : U64 → U64) (ksb : U64) (a : U64) (sl : Fl4SpecA64.ExtSlot) + (sh : Nat) : Prop := + ∃ y j : U64, y ∈ Y ∧ ActiveHighRootA mem ksb Pp y ∧ j.val < 512 ∧ a = peAF (wa y 4096#u64) j ∧ + sl = .root (specPtr y) j.val ∧ sh = 30 + +theorem SlotRelE.mono {G G' : U64 → Nat → Prop} {Top Top' : U64 → Fl4SpecA64.ExtSlot → Nat → Prop} + {a : U64} {sl : Fl4SpecA64.ExtSlot} {sh : Nat} (h : SlotRelE G Top a sl sh) + (hG : ∀ t s, G t s → G' t s) (hTop : ∀ x l s, Top x l s → Top' x l s) : + SlotRelE G' Top' a sl sh := by + rcases h with ⟨t, i, hg, hi, ha, hsl⟩ | ht + · exact Or.inl ⟨t, i, hG t sh hg, hi, ha, hsl⟩ + · exact Or.inr (hTop a sl sh ht) + +theorem TopS2ReachE.toTopS2E {Pp X : Set U64} {σ : Fl4SpecA64.State Unit} {a : U64} {sl : Fl4SpecA64.ExtSlot} + {sh : Nat} (h : TopS2ReachE Pp X σ a sl sh) : TopS2E X a sl sh := by + obtain ⟨r, j, hr, -, hj, ha, hsl, hsh⟩ := h + exact ⟨r, j, hr, hj, ha, hsl, hsh⟩ + +theorem TopHiReachE.toTopHiE {Pp Y : Set U64} {mem : U64 → U64} {ksb : U64} {a : U64} {sl : Fl4SpecA64.ExtSlot} + {sh : Nat} (h : TopHiReachE Pp Y mem ksb a sl sh) : TopHiE Y a sl sh := by + obtain ⟨y, j, hy, -, hj, ha, hsl, hsh⟩ := h + exact ⟨y, j, hy, hj, ha, hsl, hsh⟩ + +section slots +variable {G : U64 → Nat → Prop} {Top : U64 → Fl4SpecA64.ExtSlot → Nat → Prop} {σ : Fl4SpecA64.State Unit} + +theorem descAtE {mem : U64 → U64} (hG : GoodTabE G mem σ) (hT : TopOkE G Top mem σ) {a : U64} + {sl : Fl4SpecA64.ExtSlot} {sh : Nat} (h : SlotRelE G Top a sl sh) : + 12 ≤ sh ∧ sh ≤ 39 ∧ DescRelE G sh (mem a) (Fl4SpecA64.ExtSlot.read σ sl) := by + rcases h with ⟨t, i, ht, hi, rfl, rfl⟩ | h + · obtain ⟨h1, h2, ha, pt, hk, hty, he⟩ := hG t sh ht + exact ⟨h1, h2, by rw [readPtE hk hty ha hi]; exact he i hi⟩ + · exact hT a sl sh h + +theorem readRootE {σ : Fl4SpecA64.State Unit} {r : Fl4SpecA64.ObjRef} {o : Fl4SpecA64.ExtArchObj Fl4SpecA64.PTE} + (hk : σ.kheap r = some (.archObj (.ext o))) (j : Nat) : + Fl4SpecA64.ExtSlot.read σ (.root r j) = o.entry j := by + rw [read_root_eqX, extObjAt_someX.2 hk]; rfl + +theorem topOkS2E {Pp Tb X Y T2 : Set U64} {Sh2 : U64 → Nat} {mem : U64 → U64} + (E : ExtVsRelA Pp Tb X Y T2 Sh2 mem σ) : + TopOkE (G2ReachE Pp X T2 Sh2 mem σ) (TopS2ReachE Pp X σ) mem σ := by + rintro a sl sh ⟨r, j, hr, hact, hj, rfl, rfl, rfl⟩ + obtain ⟨hts, -, h1k, -, -⟩ := s2GeomNumE + obtain ⟨es, hk, he⟩ := E.s2root r hr + have hj1 : j.val < 1024 := by omega + refine ⟨by rcases hts with h | h <;> omega, by rcases hts with h | h <;> omega, ?_⟩ + rw [readRootE hk] + show DescRelE _ _ _ (if h : j.val < 1024 then es ⟨j.val, h⟩ else .invalidPTE) + rw [dif_pos hj1] + exact (he hact j hj1 hj).1.mono fun hb0 hb1 _ hg => + ⟨hg, .stepRoot hact hj ((and3_bitsA _).2 ⟨hb0, hb1⟩) hg.1⟩ + +theorem topOkHiE {Pp Tb : Set U64} {Lv : U64 → Nat} {X Y : Set U64} {mem : U64 → U64} {ksb : U64} + (R : VsRelA Pp Tb Lv X Y mem ksb σ) : + TopOkE (G1ReachE Pp Tb Y Lv mem ksb) (TopHiReachE Pp Y mem ksb) mem σ := by + rintro a sl sh ⟨y, j, hy, hact, hj, rfl, rfl, rfl⟩ + obtain ⟨l1, hk, he⟩ := R.high y hy + refine ⟨by omega, by omega, ?_⟩ + rw [readRootE hk] + show DescRelE _ _ _ (if h : j.val < 512 then l1 ⟨j.val, h⟩ else .invalidPTE) + rw [dif_pos hj] + refine (descE_of_pteRel (k := 1) le_rfl (by omega) (he hact j hj)).mono fun hb0 hb1 _ hg => ⟨hg, ?_⟩ + obtain ⟨asid, raw, ha, htag2, rfl⟩ := hact + exact ⟨2#u64, 21, Or.inr rfl, + .child (.high ha htag2 hy) (Or.inr ⟨_, hy, rfl⟩) (by decide) hj ((and3_bitsA _).2 ⟨hb0, hb1⟩)⟩ + +end slots + +/-! ## The lookup walk -/ + +/-- **The lookup loop's invariant** (module docstring): FL4's `(shift, slot, level)` against a spec position whose +walk of `level` more steps is the whole walk `W`. -/ +abbrev WkE (G : U64 → Nat → Prop) (Top : U64 → Fl4SpecA64.ExtSlot → Nat → Prop) (σ : Fl4SpecA64.State Unit) + (va : U64) (W : Fl4SpecA64.ExtPos) (x : U64 × U64 × U64) : Prop := + ∃ pos : Fl4SpecA64.ExtPos, Fl4SpecA64.extWalkN σ va.bv x.2.2.val pos = W ∧ pos.shift = x.1.val ∧ + SlotRelE G Top x.2.1 pos.slot pos.shift + +/-- Where the lookup stops: the spec's position `W`, FL4's shift and slot its. -/ +abbrev WkPostE (G : U64 → Nat → Prop) (Top : U64 → Fl4SpecA64.ExtSlot → Nat → Prop) (W : Fl4SpecA64.ExtPos) + (r : U64 × U64) : Prop := + W.shift = r.1.val ∧ SlotRelE G Top r.2 W.slot W.shift + +section walk +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {G : U64 → Nat → Prop} {Top : U64 → Fl4SpecA64.ExtSlot → Nat → Prop} {σ : Fl4SpecA64.State Unit} + +theorem wsub9E {sh : U64} (h : 9 ≤ sh.val) : (core.num.U64.wrapping_sub sh 9#u64).val = sh.val - 9 := by + have hx : sh.val ≤ 18446744073709551615 := by scalar_tac + simp [core.num.U64.wrapping_sub_val_eq, U64.size_def, U64.numBits]; omega + +set_option maxHeartbeats 4000000 in +/-- **FL4's lookup loop against `extWalkN`** (module docstring). -/ +theorem lookup_loopE {s : S} (hG : GoodTabE G (L.mem s) σ) (hT : TopOkE G Top (L.mem s) σ) (va : U64) + (W : Fl4SpecA64.ExtPos) (P0 : Prop) (a0 : U64) + (hP0 : P0 → (L.mem s a0 &&& 3#u64) = 3#u64 ∧ ∀ s1 l1 : U64, s1.val = 30 → l1.val = 2 → + WkE G Top σ va W (s1, peAF (dTabA (L.mem s a0)) (idxA va s1), l1)) + (shift slot level : U64) + (hi : WkE G Top σ va W (shift, slot, level) ∨ (P0 ∧ (shift, slot, level) = (39#u64, a0, 3#u64))) : + vspace.lookup_pt_slot_loop st s va shift slot level ⦃ r => WkPostE G Top W r ⦄ := by + unfold vspace.lookup_pt_slot_loop + apply loop.spec_decr_nat (measure := fun x => x.2.2.val) + (inv := fun x => WkE G Top σ va W x ∨ (P0 ∧ x = (39#u64, a0, 3#u64))) + · rintro ⟨sh, sl, lv⟩ hx + dsimp only + unfold vspace.lookup_pt_slot_loop.body + rcases hx with ⟨pos, hw, hsh, hsr⟩ | ⟨h0, hx⟩ + · simp only at hw hsh hsr + obtain ⟨h12, h39, hd⟩ := descAtE hG hT hsr + obtain ⟨hiff, htab⟩ := descStepE hd + split + · rename_i hpos + obtain ⟨m, hm⟩ : ∃ m, lv.val = m + 1 := ⟨lv.val - 1, by scalar_tac⟩ + rw [hm, extWalkN_succX] at hw + rw [L.read_eq, bind_ok, vs_is_tableE, bind_ok] + by_cases hc : 12 < pos.shift ∧ (L.mem s sl &&& 3#u64) = 3#u64 + · rw [if_pos (decide_eq_true (by rw [← hsh]; exact hc))] + have hT1 := hiff.2 hc + obtain ⟨hpe, hGn, h44⟩ := htab hT1 + rw [if_pos hT1, hpe] at hw + step as ⟨lv1, hlv1⟩ + have h9 := wsub9E (sh := sh) (by omega) + simp only [lift, bind_ok, table_of_eqA] + rw [entry_addr_eqA _ _ _ (by rw [h9]; omega)] + simp only [bind_ok] + refine (WP.spec_ok _).2 ⟨Or.inl ⟨_, by rw [show lv1.val = m by omega]; exact hw, ?_, + Or.inl ⟨_, _, hGn, idxA_lt va _, rfl, nextSlotE va h44 (sh := pos.shift) _ (by rw [h9, hsh])⟩⟩, ?_⟩ + · show pos.shift - 9 = _ + rw [h9, hsh] + · show lv1.val < lv.val + omega + · rw [if_neg (fun h => hc (by rw [hsh]; exact of_decide_eq_true h))] + rw [if_neg (fun h => hc (hiff.1 h))] at hw + refine (WP.spec_ok _).2 ?_ + rw [← hw] + exact ⟨hsh, hsr⟩ + · have h0 : lv.val = 0 := by scalar_tac + rw [h0, extWalkN_zeroX] at hw + refine (WP.spec_ok _).2 ?_ + rw [← hw] + exact ⟨hsh, hsr⟩ + · simp only [Prod.mk.injEq] at hx + obtain ⟨rfl, rfl, rfl⟩ := hx + obtain ⟨h3, hW⟩ := hP0 h0 + split + · rw [L.read_eq, bind_ok, vs_is_tableE, bind_ok, + if_pos (decide_eq_true ⟨by show (12 : Nat) < 39; decide, h3⟩)] + step as ⟨lv1, hlv1⟩ + have h9 : (core.num.U64.wrapping_sub 39#u64 9#u64).val = 30 := by + simp [core.num.U64.wrapping_sub_val_eq, U64.size_def, U64.numBits] + simp only [lift, bind_ok, table_of_eqA] + rw [entry_addr_eqA _ _ _ (by rw [h9]; omega)] + simp only [bind_ok] + have hl2 : lv1.val = 2 := by scalar_tac + refine (WP.spec_ok _).2 ⟨Or.inl (hW _ _ h9 hl2), ?_⟩ + show lv1.val < (3#u64 : U64).val + rw [hl2]; decide + · rename_i hz + exact absurd hz (by scalar_tac) + · exact hi + +/-! ## The table searches -/ + +/-- **The search's post** (module docstring): `none` is the spec's `none`; `some a` a word that is the spec's +slot. -/ +abbrev FdPostE (G : U64 → Nat → Prop) (Top : U64 → Fl4SpecA64.ExtSlot → Nat → Prop) + (W : Option Fl4SpecA64.ExtSlot) : Option U64 → Prop + | none => W = none + | some a => ∃ sl sh, W = some sl ∧ SlotRelE G Top a sl sh + +/-- `vspace::unmap_page_table`'s loop state `(table, shift, level)` (level = FL4 depth, at most 3). -/ +abbrev FdVE (G : U64 → Nat → Prop) (Top : U64 → Fl4SpecA64.ExtSlot → Nat → Prop) (σ : Fl4SpecA64.State Unit) + (va pt : U64) (W : Option Fl4SpecA64.ExtSlot) (x : U64 × U64 × U64) : Prop := + x.2.2.val ≤ 3 ∧ ∃ pos : Fl4SpecA64.ExtPos, Fl4SpecA64.extFindTableN σ va.bv (specPtr pt) (3 - x.2.2.val) pos = W ∧ + pos.shift = x.2.1.val ∧ SlotRelE G Top (peAF x.1 (idxA va x.2.1)) pos.slot pos.shift + +/-- **The search step at a related descriptor** (module docstring). -/ +theorem findStepE {sh : Nat} {w : U64} {sl : Fl4SpecA64.ExtSlot} + (hd : DescRelE G sh w (Fl4SpecA64.ExtSlot.read σ sl)) (va pt : U64) (n : Nat) (s9 : U64) (hs9 : s9.val = sh - 9) : + Fl4SpecA64.extFindTableN σ va.bv (specPtr pt) (n + 1) ⟨sl, sh⟩ = + if 12 < sh ∧ (w &&& 3#u64) = 3#u64 then + (if dTabA w = pt then some sl + else Fl4SpecA64.extFindTableN σ va.bv (specPtr pt) n ⟨.pt (specPtr (peAF (dTabA w) (idxA va s9))), sh - 9⟩) + else none := by + obtain ⟨hiff, htab⟩ := descStepE hd + rw [extFindTableN_succX] + dsimp only + by_cases hc : 12 < sh ∧ (w &&& 3#u64) = 3#u64 + · obtain ⟨hpe, -, h44⟩ := htab (hiff.2 hc) + rw [if_neg (not_not.2 (hiff.2 hc)), if_pos hc, hpe, pptrFromPte_tabA _ h44, nextSlotE va h44 s9 hs9] + by_cases hq : dTabA w = pt + · rw [if_pos (by rw [hq]), if_pos hq] + · rw [if_neg (fun e => hq (specPtr_inj e)), if_neg hq] + · rw [if_pos (fun h => hc (hiff.1 h)), if_neg hc] + +set_option maxHeartbeats 4000000 in +/-- **`vspace::unmap_page_table`'s search against `extFindTableN`** (module docstring). -/ +theorem unmap_pt_loopE {s : S} (hG : GoodTabE G (L.mem s) σ) (hT : TopOkE G Top (L.mem s) σ) (va pt : U64) + (W : Option Fl4SpecA64.ExtSlot) (table shift level : U64) (hi : FdVE G Top σ va pt W (table, shift, level)) : + vspace.unmap_page_table_loop st s va pt table shift level ⦃ r => FdPostE G Top W r ⦄ := by + unfold vspace.unmap_page_table_loop + apply loop.spec_decr_nat (measure := fun x => 3 - x.2.2.val) (inv := FdVE G Top σ va pt W) + · rintro ⟨tb, sh, lv⟩ ⟨hl3, ⟨psl, psh⟩, hw, hsh, hsr⟩ + simp only at hl3 hw hsh hsr + subst hsh + dsimp only + unfold vspace.unmap_page_table_loop.body + obtain ⟨h12, h39, hd⟩ := descAtE hG hT hsr + split + · rename_i hlt + have hlt' : lv.val < 3 := by scalar_tac + obtain ⟨m, hm⟩ : ∃ m, 3 - lv.val = m + 1 := ⟨2 - lv.val, by omega⟩ + have h9 := wsub9E (sh := sh) (by omega) + rw [hm, findStepE hd va pt m _ h9] at hw + rw [entry_addr_eqA _ _ _ (by omega), bind_ok, L.read_eq, bind_ok, vs_is_tableE, bind_ok] + by_cases hc : 12 < sh.val ∧ (L.mem s (peAF tb (idxA va sh)) &&& 3#u64) = 3#u64 + · rw [if_pos (decide_eq_true hc)] + rw [if_pos hc] at hw + obtain ⟨-, hGn, -⟩ := (descStepE hd).2 ((descStepE hd).1.2 hc) + rw [table_of_eqA, bind_ok] + by_cases hq : dTabA (L.mem s (peAF tb (idxA va sh))) = pt + · rw [if_pos hq] + rw [if_pos hq] at hw + exact (WP.spec_ok _).2 ⟨_, _, hw.symm, hsr⟩ + · rw [if_neg hq] + rw [if_neg hq] at hw + simp only [lift, bind_ok] + obtain ⟨lv1, hle, hlv1⟩ := spec_ex_fd (U64.add_spec (x := lv) (y := 1#u64) (by scalar_tac)) + rw [hle, bind_ok] + have hl1 : lv1.val = lv.val + 1 := by scalar_tac + have e3 : 3 - lv1.val = m := by omega + have hinv : FdVE G Top σ va pt W (dTabA (L.mem s (peAF tb (idxA va sh))), + core.num.U64.wrapping_sub sh 9#u64, lv1) := + ⟨by dsimp only; omega, ⟨.pt (specPtr (peAF (dTabA (L.mem s (peAF tb (idxA va sh)))) + (idxA va (core.num.U64.wrapping_sub sh 9#u64)))), sh.val - 9⟩, + by dsimp only; rw [e3]; exact hw, by dsimp only; rw [h9], + Or.inl ⟨dTabA (L.mem s (peAF tb (idxA va sh))), idxA va (core.num.U64.wrapping_sub sh 9#u64), hGn, + idxA_lt _ _, rfl, rfl⟩⟩ + have hmeas : 3 - lv1.val < 3 - lv.val := by omega + exact (WP.spec_ok _).2 ⟨hinv, hmeas⟩ + · rw [if_neg (fun h => hc (of_decide_eq_true h))] + rw [if_neg hc] at hw + exact (WP.spec_ok _).2 hw.symm + · rename_i hge + have h3 : lv.val = 3 := by scalar_tac + rw [h3] at hw + exact (WP.spec_ok _).2 (by rw [← hw]; rfl) + · exact hi + +/-- `ept::unmap_table`'s loop state `(table, shift, mask, rem)`, the mask `2^k - 1`. -/ +abbrev FdEE (G : U64 → Nat → Prop) (Top : U64 → Fl4SpecA64.ExtSlot → Nat → Prop) (σ : Fl4SpecA64.State Unit) + (va pt : U64) (W : Option Fl4SpecA64.ExtSlot) (x : U64 × U64 × U64 × U64) : Prop := + ∃ k, k ≤ 10 ∧ x.2.2.1.val = 2 ^ k - 1 ∧ ∃ pos : Fl4SpecA64.ExtPos, + Fl4SpecA64.extFindTableN σ va.bv (specPtr pt) x.2.2.2.val pos = W ∧ pos.shift = x.2.1.val ∧ + SlotRelE G Top (peAF x.1 (idxME va x.2.1 k)) pos.slot pos.shift + +set_option maxHeartbeats 4000000 in +/-- **`ept::unmap_table`'s search against `extFindTableN`** (module docstring). -/ +theorem ept_unmap_table_loopE {s : S} (hG : GoodTabE G (L.mem s) σ) (hT : TopOkE G Top (L.mem s) σ) (va pt : U64) + (W : Option Fl4SpecA64.ExtSlot) (table shift mask rem : U64) + (hi : FdEE G Top σ va pt W (table, shift, mask, rem)) : + ept.unmap_table_loop st s va pt table shift mask rem ⦃ r => FdPostE G Top W r ⦄ := by + unfold ept.unmap_table_loop + apply loop.spec_decr_nat (measure := fun x => x.2.2.2.val) (inv := FdEE G Top σ va pt W) + · rintro ⟨tb, sh, mk, rm⟩ ⟨k, hk, hmk, ⟨psl, psh⟩, hw, hsh, hsr⟩ + simp only at hmk hw hsh hsr + subst hsh + dsimp only + unfold ept.unmap_table_loop.body + obtain ⟨h12, h39, hd⟩ := descAtE hG hT hsr + split + · rename_i hpos + obtain ⟨m, hm⟩ : ∃ m, rm.val = m + 1 := ⟨rm.val - 1, by scalar_tac⟩ + have h9 := wsub9E (sh := sh) (by omega) + rw [hm, findStepE hd va pt m _ h9] at hw + rw [ept_entry_addrE _ _ _ _ (by omega) hmk, bind_ok, L.read_eq, bind_ok, ept_is_tableE, vs_is_tableE, bind_ok] + by_cases hc : 12 < sh.val ∧ (L.mem s (peAF tb (idxME va sh k)) &&& 3#u64) = 3#u64 + · rw [if_pos (decide_eq_true hc)] + rw [if_pos hc] at hw + obtain ⟨-, hGn, -⟩ := (descStepE hd).2 ((descStepE hd).1.2 hc) + rw [ept_table_ofE, table_of_eqA, bind_ok] + by_cases hq : dTabA (L.mem s (peAF tb (idxME va sh k))) = pt + · rw [if_pos hq] + rw [if_pos hq] at hw + exact (WP.spec_ok _).2 ⟨_, _, hw.symm, hsr⟩ + · rw [if_neg hq] + rw [if_neg hq] at hw + simp only [lift, bind_ok] + obtain ⟨rm1, hle, hrm1⟩ := spec_ex_fd (U64.sub_spec (x := rm) (y := 1#u64) (by scalar_tac)) + rw [hle, bind_ok] + have hr1 : rm1.val = rm.val - 1 := by scalar_tac + have e3 : rm1.val = m := by omega + have hinv : FdEE G Top σ va pt W (dTabA (L.mem s (peAF tb (idxME va sh k))), + core.num.U64.wrapping_sub sh 9#u64, 511#u64, rm1) := + ⟨9, by decide, rfl, ⟨.pt (specPtr (peAF (dTabA (L.mem s (peAF tb (idxME va sh k)))) + (idxA va (core.num.U64.wrapping_sub sh 9#u64)))), sh.val - 9⟩, + by dsimp only; rw [e3]; exact hw, by dsimp only; rw [h9], + Or.inl ⟨dTabA (L.mem s (peAF tb (idxME va sh k))), idxA va (core.num.U64.wrapping_sub sh 9#u64), hGn, + idxA_lt _ _, rfl, rfl⟩⟩ + have hmeas : rm1.val < rm.val := by omega + exact (WP.spec_ok _).2 ⟨hinv, hmeas⟩ + · rw [if_neg (fun h => hc (of_decide_eq_true h))] + rw [if_neg hc] at hw + exact (WP.spec_ok _).2 hw.symm + · have h0 : rm.val = 0 := by scalar_tac + rw [h0] at hw + exact (WP.spec_ok _).2 (by rw [← hw]; rfl) + · exact hi + +end walk + +/-! ## Root tags -/ + +theorem tagNE (w : U64) (n : Nat) (c : U64) (hc : c.val = n) : (w &&& 3#u64) = c ↔ tagA w = n := by + constructor + · intro h + have := congrArg UScalar.val h + rw [and3_valA, hc] at this + exact this + · intro h + apply UScalar.eq_of_val_eq + rw [and3_valA, hc] + exact h + +/-- **A raw lookup's tag is the kind of the spec's root** (module docstring). -/ +theorem rawKindE {Pp Tb : Set U64} {Lv : U64 → Nat} {X Y : Set U64} {mem : U64 → U64} {ksb : U64} + {σ : Fl4SpecA64.State Unit} (R : VsRelA Pp Tb Lv X Y mem ksb σ) + (hX : ∀ r ∈ X, ∃ es, σ.kheap (specPtr r) = some (.archObj (.ext (.stage2Root es)))) + {a : Fl4SpecA64.Asid} {w : U64} (h : RawRelA Tb Lv X Y σ a (.Ok w)) : + Fl4SpecA64.vspaceForAsid a σ = some (specPtr (untagA w)) ∧ + ((tagA w = 0 ∧ Fl4SpecA64.rootKindAt σ (specPtr (untagA w)) = .stage1) ∨ + (tagA w = 1 ∧ untagA w ∈ X ∧ Fl4SpecA64.rootKindAt σ (specPtr (untagA w)) = .stage2) ∨ + (tagA w = 2 ∧ untagA w ∈ Y ∧ Fl4SpecA64.rootKindAt σ (specPtr (untagA w)) = .high)) := by + obtain ⟨hs, hw0, hk⟩ := h + refine ⟨hs, ?_⟩ + rcases hk with h | ⟨h0, hT, -⟩ | ⟨h1, hx⟩ | ⟨h2, hy⟩ + · exact absurd h hw0 + · obtain ⟨t, hkt, -⟩ := R.pt _ hT + exact Or.inl ⟨h0, rootKind_ptA hkt⟩ + · obtain ⟨es, hes⟩ := hX _ hx + exact Or.inr (Or.inl ⟨h1, hx, rootKind_s2A hes⟩) + · obtain ⟨l1, hl, -⟩ := R.high _ hy + exact Or.inr (Or.inr ⟨h2, hy, rootKind_highA hl⟩) + +theorem untagE (w : U64) : (w &&& ~~~(3#u64 : U64)) = untagA w := by + unfold untagA + rw [show (~~~(3#u64 : U64)) = 18446744073709551612#u64 by decide] + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +set_option maxHeartbeats 2000000 in +/-- **`ept::find` against `findExtRoot .stage2`** (module docstring). -/ +theorem ept_findE {Pp Tb : Set U64} {Lv : U64 → Nat} {X Y : Set U64} {σ : Fl4SpecA64.State Unit} {s : S} + (R : VsRelA Pp Tb Lv X Y (L.mem s) (L.ksb s) σ) + (hX : ∀ r ∈ X, ∃ es, σ.kheap (specPtr r) = some (.archObj (.ext (.stage2Root es)))) + {asid : U64} (h16 : asid.val < 65536) : + ept.find st s asid ⦃ r => (∀ w, r = .Ok w → w ∈ X ∧ + extRootResX σ .stage2 (BitVec.ofNat 16 asid.val) = .ok (specPtr w)) ∧ + (∀ e, r = .Err e → extRootResX σ .stage2 (BitVec.ofNat 16 asid.val) = .error .invalidRoot) ⦄ := by + obtain ⟨r, hr, hrel⟩ := spec_ex_fd (find_asid_rawA (st := st) R h16) + unfold ept.find + rw [hr, bind_ok] + rcases r with w | e + · obtain ⟨hs, hk⟩ := rawKindE R hX hrel + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok, lift, rtag, rs2] + split + · rename_i h1 + have h1' : ¬ tagA w = 1 := fun e => by + have := (tagNE w 1 1#u64 rfl).2 e + rw [this] at h1 + simp at h1 + refine (WP.spec_ok _).2 ⟨fun _ e => (by cases e), fun _ _ => extRootResX_errX hs ?_⟩ + rcases hk with ⟨-, h⟩ | ⟨h, -⟩ | ⟨-, -, h⟩ + · rw [h]; decide + · exact absurd h h1' + · rw [h]; decide + · rename_i h1 + have h1' : (w &&& 3#u64) = 1#u64 := by apply UScalar.eq_of_val_eq; simpa using h1 + have ht := (tagNE w 1 1#u64 rfl).1 h1' + rcases hk with ⟨h, -⟩ | ⟨-, hx, hk2⟩ | ⟨h, -⟩ + · rw [h] at ht; cases ht + · refine (WP.spec_ok _).2 ⟨fun w' e => ?_, fun _ e => by cases e⟩ + cases e + rw [untagE] + exact ⟨hx, extRootResX_okX hs hk2⟩ + · rw [h] at ht; cases ht + · simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame.from, + bind_tc_ok] + refine (WP.spec_ok _).2 ⟨fun _ e => (by cases e), fun _ _ => ?_⟩ + have hn : Fl4SpecA64.vspaceForAsid (BitVec.ofNat 16 asid.val) σ = none := hrel + simp only [extRootResX, hn] + +end + + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/FinBootA.lean b/proofs/Fl4ProofsA64/P3/FinBootA.lean new file mode 100644 index 000000000..cf6d7e90a --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/FinBootA.lean @@ -0,0 +1,287 @@ +/- +P3 (aarch64, A64-P3 S3 gen 27, ruling 23:23Z item 2): **`FinInvA` (`P3/FinDefsA`) at the boot witnesses `w0` and +`w1`** (x86 `JBootCs.w0_finInv`, `JBoot1Cs.w1_finInv` / `w1_finM`). + +* `w0`: the ghost has no chain (`FinMA` vacuous, **`finM_nilA`**); **`kTyped_bootA`**: `bootSA`'s CNodes are the + IRQ node's one-slot CNodes and its TCBs the idle TCBs, all objects of `bootThA`. **`finInv_w0A`**, at the concrete + layout **`finInv_w0A_ex`** (the `Z`, memory and spec state of `boot_jEntA_ex`). +* `w1`: **`kTyped_w1A`** (the root CNode and TCB are `w1ThA`'s, the rest `w0`'s); **`finM_w1A`**: every chain of + `w1G nc` is one root CNode slot `i` (`w1ChA i`: slot 2 / 3 / 10 with the root TCB's CTABLE / VTABLE / BUFFER slot, + otherwise alone), each cap of it has slot `i`'s reference, key and zombie bit (`w1ChA_capA`), and the root caps of + two slots never share a reference (**`w1_grefInjA`**: the pointers of the 53 non-`SchedControl` root slots are + distinct, `w1PtrN` by `decide`), so `conn` / `key` hold; chains have at most two slots, so `chunk` is vacuous; no + cap is a zombie. **`finInv_w1A`**, at the concrete layout **`finInv_w1A_ex`** (the `Z`, memory and spec state of + `w1_jEntA_ex`). + +No named premise; new module, no existing statement changed. +-/ +import Fl4ProofsA64.P3.FinDefsA +import Fl4ProofsA64.P3.BootAddrA +import Fl4ProofsA64.P3.JEntW1A +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P1.W7.R.ST.BootWit + +noncomputable section + +/-! ## `w0` -/ + +/-- `FinMA` of the empty ghost. -/ +theorem finM_nilA (mem : U64 → U64) : FinMA mem [] := + ⟨by simp, by simp, by simp, by simp⟩ + +section w0 +variable {kb ib : U64} {nc : Nat} (hL : BootLayA ib nc) (hK : BootKsA kb ib nc) +include hL hK + +/-- **`KTypedA` at `w0`** (module docstring). -/ +theorem kTyped_bootA (now mt : U64) : KTypedA (bootThA kb ib nc) (bootSA kb ib nc now mt) := by + constructor + · intro o r cs h + change bootKA kb ib nc now mt o = _ at h + unfold bootKA at h + split_ifs at h with h1 h2 h3 h4 + all_goals first + | (cases h; done) + | (cases h + obtain ⟨q, hq, rfl⟩ := ‹∃ q, IrqNodeA kb q ∧ o = specPtr q› + exact ⟨q, rfl, by unfold bootThA; rw [if_neg (irqN_notT hL hK hq), if_pos hq]⟩) + · intro o t h + change bootKA kb ib nc now mt o = _ at h + unfold bootKA at h + split_ifs at h with h1 h2 h3 h4 + all_goals first + | (cases h; done) + | (obtain ⟨c, hc, rfl⟩ := ‹∃ c, c < nc ∧ o = specPtr (idleTA ib c)› + exact ⟨idleTA ib c, rfl, by unfold bootThA; rw [if_pos ⟨c, hc, rfl⟩]⟩) + +/-- **`FinInvA` at `w0`** (module docstring). -/ +theorem finInv_w0A (mem : U64 → U64) (now mt : U64) : + FinInvA (bootThA kb ib nc) mem (bootSA kb ib nc now mt) ⟨[], fun _ => none⟩ := + ⟨kTyped_bootA hL hK now mt, finM_nilA mem⟩ + +end w0 + +/-- **`FinInvA` at A64-Boot's concrete `w0`** (the ghosts, memory and spec state of `boot_jEntA_ex`). -/ +theorem finInv_w0A_ex (r now mt : U64) : + FinInvA (bootZA 18446603337294938112#u64 18446603338368679936#u64 128).th + (bL.mem (bootS 18446603337294938112#u64 18446603338368679936#u64 128 r now mt)) (bootSA 18446603337294938112#u64 18446603338368679936#u64 128 now mt) + (bootZA 18446603337294938112#u64 18446603338368679936#u64 128).G := by + have hi : (18446603338368679936#u64 : U64).val = 18446603338368679936 := by simp + have hk : (18446603337294938112#u64 : U64).val = 18446603337294938112 := by simp + exact finInv_w0A (bootLayA_of bootLayC_ex (Or.inl (by rw [hi]; omega))) + (bootKsA_of bootLayC_ex (Or.inl (by rw [hk]; omega))) _ now mt + +/-! ## `w1`: the root slots' references -/ + +/-- The root slots by a normalised index `j < 53` (`j < 16` the fixed slots, `16 + k` the tail slot `k`). -/ +def capNA (j : Nat) : capability.Cap := if j < 16 then (fixC j).1 else (tailC (j - 16)).1 + +/-- The pointer a normalised root slot's cap names, if it names one (`grefA`). -/ +def w1PtrN (j : Nat) : Option Nat := + if j = 1 then some RTb else if j = 2 then some RRb else if j = 3 then some VSb else if j = 6 then some PLb + else if j = 9 then some BIb else if j = 10 then some IPb else if j = 14 then some SCb + else if j = 16 then some L1b else if j = 17 then some L2b else if j = 18 then some L3b + else if 19 ≤ j ∧ j < 51 then some (EFb + 4096 * (j - 19)) else if j = 51 then some IMb else none + +set_option maxHeartbeats 2000000 in +theorem grefN_eqA : ∀ j < 53, grefA (capNA j) = (w1PtrN j).map fun v => .objRef (specPtr (bwv v)) := by + intro j hj + interval_cases j <;> rfl + +set_option maxHeartbeats 4000000 in +theorem w1PtrN_inj : ∀ j < 53, ∀ j' < 53, w1PtrN j ≠ none → w1PtrN j = w1PtrN j' → j = j' := by + decide + +theorem w1PtrN_lt : ∀ j < 53, ∀ v, w1PtrN j = some v → v < 2 ^ 64 := by + intro j hj v h + interval_cases j <;> simp [w1PtrN] at h <;> omega + +section +variable {nc : Nat} + +theorem capOf_normA {i : Nat} (hi : i < 53 + nc) (hg : grefA (capOf nc i) ≠ none) : + ∃ j < 53, capOf nc i = capNA j ∧ (i < 16 → j = i) ∧ (16 ≤ i → j = i - nc ∧ 16 + nc ≤ i) := by + by_cases h1 : i < 16 + · exact ⟨i, by omega, by unfold capOf capNA; rw [slotC_lo h1, if_pos h1], fun _ => rfl, fun h => by omega⟩ + by_cases h2 : i < 16 + nc + · exfalso; apply hg; unfold capOf; rw [slotC_sch (by omega) h2]; rfl + refine ⟨i - nc, by omega, ?_, fun h => by omega, fun _ => ⟨rfl, by omega⟩⟩ + unfold capOf capNA; rw [slotC_hi (by omega), if_neg (by omega), show i - nc - 16 = i - (16 + nc) by omega] + +/-- **Two root slots' caps never share a reference** (module docstring). -/ +theorem w1_grefInjA {i j : Nat} {r : Fl4SpecA64.GenObjRef} (hi : i < 53 + nc) (hj : j < 53 + nc) + (h1 : grefA (capOf nc i) = some r) (h2 : grefA (capOf nc j) = some r) : i = j := by + obtain ⟨a, ha, ea, ha1, ha2⟩ := capOf_normA hi (by rw [h1]; exact Option.some_ne_none _) + obtain ⟨b, hb, eb, hb1, hb2⟩ := capOf_normA hj (by rw [h2]; exact Option.some_ne_none _) + rw [ea, grefN_eqA a ha] at h1 + rw [eb, grefN_eqA b hb] at h2 + obtain ⟨v, hv, rfl⟩ := Option.map_eq_some_iff.1 h1 + obtain ⟨v', hv', e⟩ := Option.map_eq_some_iff.1 h2 + have e1 : bwv v' = bwv v := specPtr_inj (Fl4SpecA64.GenObjRef.objRef.inj e) + have e2 : v' = v := by + have := congrArg UScalar.val e1 + rwa [bwv_val (w1PtrN_lt b hb v' hv'), bwv_val (w1PtrN_lt a ha v hv)] at this + subst e2 + have hab : a = b := w1PtrN_inj a ha b hb (by rw [hv]; exact Option.some_ne_none _) (hv.trans hv'.symm) + subst hab + by_cases h16 : i < 16 <;> by_cases j16 : j < 16 + · exact (ha1 h16).symm.trans (hb1 j16) + · have := ha1 h16; have := hb2 (by omega); omega + · have := ha2 (by omega); have := hb1 j16; omega + · have := ha2 (by omega); have := hb2 (by omega); omega + +theorem capOf_nzbA (i : Nat) : isZbA (capOf nc i) = false := by + unfold capOf + by_cases h1 : i < 16 + · rw [slotC_lo h1]; unfold fixC; split_ifs <;> rfl + by_cases h2 : i < 16 + nc + · rw [slotC_sch (by omega) h2]; rfl + · rw [slotC_hi (by omega)]; unfold tailC; split_ifs <;> rfl + +end + +/-! ## `w1`: the chains -/ + +/-- The chain of root slot `i` at `w1`. -/ +def w1ChA (i : Nat) : List U64 := + if i = 2 then [rs 2, ts 0#u64] else if i = 3 then [rs 3, ts 1#u64] else if i = 10 then [rs 10, ts 2#u64] + else [rs i] + +/-- Same reference, key and zombie bit. -/ +def SameObjA (c d : capability.Cap) : Prop := grefA c = grefA d ∧ okeyA c = okeyA d ∧ isZbA c = isZbA d + +theorem w1G_chA {nc : Nat} : ∀ ch ∈ w1G nc, ∃ i, ch = w1ChA i ∧ i < 53 + nc ∧ cw0 nc i ≠ 0 := by + intro ch h + simp only [w1G, List.mem_append, List.mem_cons, List.mem_nil_iff, or_false, List.mem_map] at h + rcases h with (rfl | rfl | rfl) | ⟨i, hi, rfl⟩ + · exact ⟨2, by simp [w1ChA], by omega, cw0_2 nc⟩ + · exact ⟨3, by simp [w1ChA], by omega, cw0_3 nc⟩ + · exact ⟨10, by simp [w1ChA], by omega, cw0_10 nc⟩ + · obtain ⟨h1, h2, e2, e3, e10⟩ := w1Single_mem.1 hi + exact ⟨i, by simp [w1ChA, e2, e3, e10], h1, h2⟩ + +section kt +variable {kb ib now mt sl : U64} {nc : Nat} + +/-- **`KTypedA` at `w1`** (module docstring). -/ +theorem kTyped_w1A (hL : BootLayA ib nc) (hK : BootKsA kb ib nc) (P : BootLay1 kb ib nc) : + KTypedA (w1ThA kb ib nc) (w1SA kb ib nc now mt sl) := by + have KB := kTyped_bootA hL hK now mt + have old : ∀ {q : U64} {k : ObjKindA}, bootThA kb ib nc q = some k → w1ThA kb ib nc q = some k := fun hq => by + have := bootTh_ltA hL hK P hq + rw [w1ThA_old (by omega)]; exact hq + constructor + · intro o r cs h + change w1KA kb ib nc now mt sl o = _ at h + unfold w1KA at h + split_ifs at h with h1 h2 h3 h4 h5 h6 + · cases h + refine ⟨fl4Ptr o, (specPtr_fl4Ptr o).symm, ?_⟩ + rw [show fl4Ptr o = bwv RRb from UScalar.eq_of_val_eq (by rw [h1, vRR])] + exact w1ThA_rr + all_goals first + | (cases h; done) + | (obtain ⟨q, e, hq⟩ := KB.cn o r cs h; exact ⟨q, e, old hq⟩) + · intro o t h + change w1KA kb ib nc now mt sl o = _ at h + unfold w1KA at h + split_ifs at h with h1 h2 h3 h4 h5 h6 + · cases h + · refine ⟨fl4Ptr o, (specPtr_fl4Ptr o).symm, ?_⟩ + rw [show fl4Ptr o = bwv RTb from UScalar.eq_of_val_eq (by rw [h2, vRT])] + exact w1ThA_rt + all_goals first + | (cases h; done) + | (obtain ⟨q, e, hq⟩ := KB.tc o t h; exact ⟨q, e, old hq⟩) + +end kt + +section mem +variable {kb ib r now mt sl : U64} {nc : Nat} {uw : U64 → U64} (P : BootLay1 kb ib nc) +include P + +set_option hygiene false in +local notation "S1" => w1S kb ib nc r now mt sl uw + +theorem w1ChA_capA {i : Nat} (hi : i < 53 + nc) {x : U64} (hx : x ∈ w1ChA i) : + SameObjA (capAtA (bL.mem S1) x) (capOf nc i) := by + have hrs : ∀ j < 8192, capAtA (bL.mem S1) (rs j) = capOf nc j := fun j hj => cap_root P hj (rs_val hj) + have hts : ∀ k : U64, k.val < 7 → capAtA (bL.mem S1) (ts k) = + if k.val = 0 then .CNode (bwv RRb) 13#u64 51#u64 0#u64 else if k.val = 1 then .Pml4 (bwv VSb) true 1#u64 else + if k.val = 2 then .Frame (bwv IPb) .Small 3#u64 false false false 0#u64 (bwv 0) else .Null := + fun k hk => cap_rt P hk + have refl : ∀ c, SameObjA c c := fun c => ⟨rfl, rfl, rfl⟩ + unfold w1ChA at hx + split_ifs at hx with h2 h3 h10 + · subst h2 + rcases List.mem_cons.1 hx with rfl | hx + · rw [hrs 2 (by omega)]; exact refl _ + · rw [List.mem_singleton.1 hx, hts 0#u64 (by simp), cap2]; simpa using refl _ + · subst h3 + rcases List.mem_cons.1 hx with rfl | hx + · rw [hrs 3 (by omega)]; exact refl _ + · rw [List.mem_singleton.1 hx, hts 1#u64 (by simp), cap3]; simpa using refl _ + · subst h10 + rcases List.mem_cons.1 hx with rfl | hx + · rw [hrs 10 (by omega)]; exact refl _ + · rw [List.mem_singleton.1 hx, hts 2#u64 (by simp), capOf_lo (by omega)] + simp only [show (2#u64 : U64).val = 2 by simp] + exact ⟨rfl, rfl, rfl⟩ + · have := P.w0.nc128 + rw [List.mem_singleton.1 hx, hrs i (by omega)]; exact refl _ + +/-- **`FinMA` at `w1`** (module docstring). -/ +theorem finM_w1A : FinMA (bL.mem S1) (w1G nc) := by + have chOf : ∀ ch ∈ w1G nc, ∀ x ∈ ch, ∃ i, ch = w1ChA i ∧ i < 53 + nc ∧ + SameObjA (capAtA (bL.mem S1) x) (capOf nc i) := fun ch hch x hx => by + obtain ⟨i, rfl, hi, -⟩ := w1G_chA ch hch + exact ⟨i, rfl, hi, w1ChA_capA P hi hx⟩ + have flOf : ∀ x ∈ (w1G nc).flatten, ∃ i, i < 53 + nc ∧ SameObjA (capAtA (bL.mem S1) x) (capOf nc i) := + fun x hx => by + obtain ⟨ch, hch, hxc⟩ := List.mem_flatten.1 hx + obtain ⟨i, -, hi, hs⟩ := chOf ch hch x hxc + exact ⟨i, hi, hs⟩ + have same : ∀ x y i j, i < 53 + nc → j < 53 + nc → SameObjA (capAtA (bL.mem S1) x) (capOf nc i) → + SameObjA (capAtA (bL.mem S1) y) (capOf nc j) → + OvlA (absCapA (capAtA (bL.mem S1) x)) (absCapA (capAtA (bL.mem S1) y)) → i = j := by + intro x y i j hi hj hx hy hov + obtain ⟨g, g1, g2⟩ := (ovlA_iff _ _).1 hov + exact w1_grefInjA hi hj (hx.1.symm.trans g1) (hy.1.symm.trans g2) + refine ⟨fun ch1 h1 ch2 h2 x hx y hy hov => ?_, fun ch hch i j k y z y' _ _ hk hij hjk => ?_, + fun x hx y hy _ hov _ _ _ => ?_, fun x hx y hy _ hz => ?_⟩ + · obtain ⟨i, rfl, hi, hsx⟩ := chOf ch1 h1 x hx + obtain ⟨j, rfl, hj, hsy⟩ := chOf ch2 h2 y hy + rw [same x y i j hi hj hsx hsy hov] + · have := (List.getElem?_eq_some_iff.1 hk).1 + have := w1G_len ch hch + exfalso; omega + · obtain ⟨i, hi, hsx⟩ := flOf x hx + obtain ⟨j, hj, hsy⟩ := flOf y hy + obtain rfl := same x y i j hi hj hsx hsy hov + exact hsx.2.1.trans hsy.2.1.symm + · obtain ⟨i, -, hsx⟩ := flOf x hx + rw [hsx.2.2, capOf_nzbA] at hz + cases hz + +/-- **`FinInvA` at `w1`** (module docstring). -/ +theorem finInv_w1A (hL : BootLayA ib nc) (hK : BootKsA kb ib nc) : + FinInvA (w1ThA kb ib nc) (bL.mem S1) (w1SA kb ib nc now mt sl) ⟨w1G nc, w1Par⟩ := + ⟨kTyped_w1A hL hK P, finM_w1A P⟩ + +end mem + +/-- **`FinInvA` at A64-Boot's concrete `w1`** (the ghosts, memory and spec state of `w1_jEntA_ex`). -/ +theorem finInv_w1A_ex (r now mt sl : U64) (uw : U64 → U64) : + FinInvA (w1ZA 18446603337294938112#u64 18446603338368679936#u64 128).th + (bL.mem (w1S 18446603337294938112#u64 18446603338368679936#u64 128 r now mt sl uw)) (w1SA 18446603337294938112#u64 18446603338368679936#u64 128 now mt sl) + (w1ZA 18446603337294938112#u64 18446603338368679936#u64 128).G := by + have hi : (18446603338368679936#u64 : U64).val = 18446603338368679936 := by simp + have hk : (18446603337294938112#u64 : U64).val = 18446603337294938112 := by simp + have P := bootLay1C_ex + exact finInv_w1A P (bootLayA_of P.w0 (Or.inl (by rw [hi]; omega))) (bootKsA_of P.w0 (Or.inl (by rw [hk]; omega))) + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/FinDefsA.lean b/proofs/Fl4ProofsA64/P3/FinDefsA.lean new file mode 100644 index 000000000..fbc0f7246 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/FinDefsA.lean @@ -0,0 +1,511 @@ +/- +P3 (aarch64, A64-P3 S3 gen 27, ruling 23:23Z item 2): **finality, against the spec's** — the A64 twin of x86's +`P3/FinBase` (`FinInv`, `FinM`, `KTyped`, `okey`, `Ovl`) and `P3/FinDefs` (`final_iff`, `finF_iff`), as a +standalone carried invariant `FinInvA` (not a [J] premise, not a `JEntA` field). + +* **`FinInvA th mem σ G`** (x86 `FinInv`): `kt : KTypedA th σ` (x86 `KTyped`: the spec's CNodes and TCBs are objects + of the typed heap) and `fm : FinMA mem G.chains` (x86 `FinM`): + - `conn` (x86 `FinM.conn`; l4v `mdb_chunked`, the reachability half): caps sharing an object are in one chain; + - `chunk` (l4v `mdb_chunked`, the contiguity half; on x86 it is `MdbInv.chunk`, which A64's `TreeA` does not + carry, so it is a field here, stated on the object key): between two caps of one non-frame key in a chain every + cap has that key; + - `key` (x86 `FinM.key`): caps sharing an object, neither a zombie nor both frames, agree on `okeyA`; + - `zfin` (x86 `FinM.zfin`; l4v `zombies_final`): a zombie shares no object with another slot's cap. + **Frame non-nesting** (x86 `FinM.fnest`) holds outright on AArch64: FL4's `same_region_as` of a frame cap with a + non-frame cap is `false` (objects.rs), so it is the theorem **`fnestA`**, not a field. +* `okeyA` (x86 `okey`): the object a cap names as `same_object_as` compares it. `PageDirectory` / `Pdpt` / `IoPort` + caps have none (their abstract caps are `NullCap` on AArch64, `absCapA`), x86's `pd` / `pdpt` / `io` keys are + dropped, `HighVSpace` gets `hv`. **`key_of_soA`** (x86 `key_of_so` / `key_of_so_left`, here unconditional: + `same_object_as a b` ⟹ `okeyA a = okeyA b`), **`soA_of_keyA`** (x86 `soB_of_key`). +* `grefA` (x86 `gref`): the one generic reference of an abstract cap — **`genObjRefs_absA`** (on AArch64 every + `absCapA` has at most one, `archGenObjRefs` is empty), **`ovlA_iff`**, **`ovl_of_keyA`** (x86 `ovl_of_key`). +* `NeedsFinA` (x86 `NeedsFin`): the caps whose finalisation depends on finality. +* **`finF_iffA`** (x86 `finF_iff`): for a `NeedsFinA`, non-zombie cap in the MDB, FL4's `is_final_capability` is + `true` iff no other MDB slot's cap overlaps it (`fin_noOvlA` / `fin_of_noOvlA`; A64 P1's `is_final.true_of` / + `is_final.false_next` / `is_final.false_prev`). +* **`final_iffA`** (x86 `final_iff`): in related states (`StateRelA`, `MdbLk`'s members, `KTypedA`), the spec's + `is_final_cap'` is the same condition (`getCap_singleA`, `IsSlotA.absA` (x86 `IsSlot.abs`), `cov_of_ktA` (x86 + `cov_of_kt`), `tcbCnodeMap_someA` (x86 `tcbCnodeMap_some`, seven slots)). +* **`fin_iff_ofA`** (x86 `VsDecFrame.fin_iff_of`): the two together. + +`KTypedA` asks every spec CNode (not only the well-formed ones, as x86's) to be a `th` CNode: the radix check comes +from `StateRelA`'s well-formedness instead. No named premise; new module, no existing statement changed. +-/ +import Fl4ProofsA64.P3.LookupA +import Fl4ProofsA64.P1.MdbKind +import Fl4ProofsA64.P1.MdbCovZL +import Fl4ProofsA64.P1.MdbStepF +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## The object a cap names (x86 `FinBase`) -/ + +/-- The object a cap names, as `same_object_as` compares it (x86 `OKey`). -/ +inductive OKeyA where + | ep (p : U64) | ntfn (p : U64) | reply (p : U64) | cnode (p r : U64) | thread (p : U64) | irq (i : U64) + | sc (p sb : U64) | frame (p : U64) (sz : capability.FrameSize) (d : Bool) | pt (p : U64) | pml4 (p : U64) + | pool (p : U64) | vcpu (p : U64) | ept (p level : U64) | hv (p : U64) + +/-- The cap's object key (x86 `okey`). -/ +def okeyA : capability.Cap → Option OKeyA + | .Endpoint p .. => some (.ep p) + | .Notification p .. => some (.ntfn p) + | .Reply p _ => some (.reply p) + | .CNode p r _ _ => some (.cnode p r) + | .Thread p => some (.thread p) + | .IrqHandler i => some (.irq i) + | .SchedContext p sb => some (.sc p sb) + | .Frame p sz _ d .. => some (.frame p sz d) + | .PageTable p .. => some (.pt p) + | .Pml4 p .. => some (.pml4 p) + | .Vcpu p => some (.vcpu p) + | .Ept p l .. => some (.ept p l) + | .HighVSpace p .. => some (.hv p) + | .AsidPool p _ => some (.pool p) + | _ => none + +/-- A zombie cap (x86 `isZb`). -/ +def isZbA : capability.Cap → Bool + | .Zombie .. => true + | _ => false + +/-- A frame cap (x86 `isFrame`). -/ +def isFrameA : capability.Cap → Bool + | .Frame .. => true + | _ => false + +/-- A key that is not a frame's. -/ +def NfKeyA (k : Option OKeyA) : Prop := ∀ p sz d, k ≠ some (.frame p sz d) + +/-- Two abstract caps share a generic object reference (x86 `Ovl`). -/ +def OvlA (a b : Fl4SpecA64.Cap) : Prop := Fl4SpecA64.genObjRefs a ∩ Fl4SpecA64.genObjRefs b ≠ ∅ + +theorem OvlA.symm {a b : Fl4SpecA64.Cap} (h : OvlA a b) : OvlA b a := by + unfold OvlA at *; rwa [Set.inter_comm] + +/-- **The spec's CNodes and TCBs are FL4's** (x86 `KTyped`). -/ +structure KTypedA (th : TypedHeapA) (σ : Fl4SpecA64.State Unit) : Prop where + cn : ∀ o r cs, σ.kheap o = some (.cnode r cs) → ∃ p, o = specPtr p ∧ th p = some (.cnode r) + tc : ∀ o t, σ.kheap o = some (.tcb t) → ∃ p, o = specPtr p ∧ th p = some .tcb + +/-- **The MDB part of `FinInvA`** (module docstring; x86 `FinM`). -/ +structure FinMA (mem : U64 → U64) (G : List (List U64)) : Prop where + conn : ∀ ch1 ∈ G, ∀ ch2 ∈ G, ∀ x ∈ ch1, ∀ y ∈ ch2, + OvlA (absCapA (capAtA mem x)) (absCapA (capAtA mem y)) → ch1 = ch2 + chunk : ∀ ch ∈ G, ∀ (i j k : Nat) (y z y' : U64), ch[i]? = some y → ch[j]? = some z → ch[k]? = some y' → + i < j → j < k → okeyA (capAtA mem y) = okeyA (capAtA mem y') → okeyA (capAtA mem y) ≠ none → + NfKeyA (okeyA (capAtA mem y)) → okeyA (capAtA mem z) = okeyA (capAtA mem y) + key : ∀ x ∈ G.flatten, ∀ y ∈ G.flatten, x ≠ y → OvlA (absCapA (capAtA mem x)) (absCapA (capAtA mem y)) → + isZbA (capAtA mem x) = false → isZbA (capAtA mem y) = false → + ¬ (isFrameA (capAtA mem x) = true ∧ isFrameA (capAtA mem y) = true) → okeyA (capAtA mem x) = okeyA (capAtA mem y) + zfin : ∀ x ∈ G.flatten, ∀ y ∈ G.flatten, x ≠ y → isZbA (capAtA mem x) = true → + ¬ OvlA (absCapA (capAtA mem x)) (absCapA (capAtA mem y)) + +/-- **The finality invariant** (module docstring; x86 `FinInv`). -/ +structure FinInvA (th : TypedHeapA) (mem : U64 → U64) (σ : Fl4SpecA64.State Unit) (G : MdbGhostA) : Prop where + kt : KTypedA th σ + fm : FinMA mem G.chains + +/-- The caps whose finalisation is object-specific work (x86 `FinObjKind`, without the caps `absCapA` nulls). -/ +def FinObjKindA : capability.Cap → Prop + | .Endpoint .. | .Notification .. | .Reply .. | .Thread _ | .SchedContext .. | .IrqHandler _ + | .Frame .. | .PageTable .. | .Pml4 .. | .Vcpu .. | .Ept .. | .HighVSpace .. | .AsidPool .. => True + | _ => False + +/-- The caps whose finalisation depends on finality (x86 `NeedsFin`). -/ +def NeedsFinA (c : capability.Cap) : Prop := + (FinObjKindA c ∧ isFrameA c = false) ∨ (∃ p r gs g, c = .CNode p r gs g) ∨ isZbA c = true + +theorem NeedsFinA.nf {c : capability.Cap} (h : NeedsFinA c) : isFrameA c = false := by + rcases h with ⟨-, h⟩ | ⟨p, r, gs, g, rfl⟩ | h + · exact h + · rfl + · cases c <;> simp_all [isZbA, isFrameA] + +theorem needsFin_keyA {c : capability.Cap} (h : NeedsFinA c) (hz : isZbA c = false) : okeyA c ≠ none := by + rcases h with ⟨h, -⟩ | ⟨p, r, gs, g, rfl⟩ | h + · cases c <;> simp [FinObjKindA, okeyA] at h ⊢ + · simp [okeyA] + · rw [hz] at h; cases h + +theorem nfKey_ofA {c : capability.Cap} (h : isFrameA c = false) : NfKeyA (okeyA c) := by + intro p sz d e + cases c <;> simp [okeyA, isFrameA] at e h + +theorem pt_needsFinA (p : U64) (m : Bool) (a v : U64) : NeedsFinA (.PageTable p m a v) := + Or.inl ⟨trivial, rfl⟩ + +/-! ## `same_object_as` against the key -/ + +set_option maxHeartbeats 1000000 in +theorem frame_keyA {p sz r d m e a v p' sz' r' d' m' e' a' v'} + (h : objects.same_object_as (.Frame p sz r d m e a v) (.Frame p' sz' r' d' m' e' a' v') = ok true) : + okeyA (.Frame p sz r d m e a v) = okeyA (.Frame p' sz' r' d' m' e' a' v') := by + simp only [objects.same_object_as] at h + by_cases hp : p = p' + · subst hp + cases sz <;> cases sz' <;> simp [capability.FrameSize.Insts.CoreCmpPartialEqFrameSize.eq, read_discriminant, + capability.FrameSize.read_discriminant, okeyA] at h ⊢ <;> exact h + · simp [hp] at h + +set_option maxHeartbeats 1000000 in +theorem soA_frameA (p : U64) (sz : capability.FrameSize) (r : U64) (d m e : Bool) (a v r' : U64) (m' e' : Bool) + (a' v' : U64) : + objects.same_object_as (.Frame p sz r d m e a v) (.Frame p sz r' d m' e' a' v') = ok true := by + simp only [objects.same_object_as] + cases sz <;> simp [capability.FrameSize.Insts.CoreCmpPartialEqFrameSize.eq, read_discriminant, + capability.FrameSize.read_discriminant] + +set_option maxHeartbeats 4000000 in +/-- **`same_object_as` ⟹ the same object key** (x86 `key_of_so` / `key_of_so_left`; unconditional on AArch64). -/ +theorem key_of_soA {a b : capability.Cap} (h : objects.same_object_as a b = ok true) : okeyA a = okeyA b := by + cases a <;> cases b <;> first + | rfl + | exact frame_keyA h + | (simp [objects.same_object_as, objects.same_region_as] at h; done) + | (simp only [objects.same_object_as, objects.same_region_as] at h + split_ifs at h <;> simp_all [okeyA]) + +set_option maxHeartbeats 2000000 in +/-- **The same object key ⟹ `same_object_as`** (x86 `soB_of_key`). -/ +theorem soA_of_keyA {a b : capability.Cap} (h : okeyA a = okeyA b) (hn : okeyA a ≠ none) : + objects.same_object_as a b = ok true := by + cases a <;> simp [okeyA] at hn <;> cases b <;> simp [okeyA] at h + all_goals first + | (obtain ⟨rfl, rfl, rfl⟩ := h; exact soA_frameA _ _ _ _ _ _ _ _ _ _ _ _ _) + | (obtain ⟨rfl, rfl⟩ := h; simp [objects.same_object_as, objects.same_region_as]) + | (subst h; simp [objects.same_object_as, objects.same_region_as]) + +/-- **Frame non-nesting** (x86 `FinM.fnest`, module docstring): a frame cap is `same_region_as` only frame caps. -/ +theorem fnestA {c d : capability.Cap} (hc : isFrameA c = true) (h : objects.same_region_as c d = ok true) : + isFrameA d = true := by + cases c <;> simp [isFrameA] at hc + cases d <;> simp [objects.same_region_as, isFrameA] at h ⊢ + +/-! ## Generic object references (x86 `gref`) -/ + +/-- The generic object reference of a cap's abstract cap (x86 `gref`). -/ +def grefA : capability.Cap → Option Fl4SpecA64.GenObjRef + | .Endpoint p .. | .Notification p .. | .Reply p _ | .CNode p .. | .Thread p | .Zombie p .. | .SchedContext p _ + | .Frame p .. | .PageTable p .. | .Pml4 p .. | .Vcpu p | .Ept p .. | .HighVSpace p .. | .AsidPool p _ => + some (.objRef (specPtr p)) + | .IrqHandler i => some (.iRQRef (BitVec.ofNat 13 i.val)) + | _ => none + +/-- The reference a key names. -/ +def krefA : OKeyA → Fl4SpecA64.GenObjRef + | .ep p | .ntfn p | .reply p | .cnode p _ | .thread p | .sc p _ | .frame p _ _ | .pt p | .pml4 p | .pool p + | .vcpu p | .ept p _ | .hv p => .objRef (specPtr p) + | .irq i => .iRQRef (BitVec.ofNat 13 i.val) + +theorem aobjRef_absFrameEA (e : Bool) (p : Fl4SpecA64.ObjRef) (R : Fl4SpecA64.CapRights) + (sz : Fl4SpecA64.VMPageSize) (d : Bool) (m : Option (Fl4SpecA64.Asid × Fl4SpecA64.VSpaceRef)) : + Fl4SpecA64.aobjRef (absFrameE e p R sz d m) = some p := by + cases e <;> rfl + +set_option maxHeartbeats 1000000 in +/-- **An abstract cap's generic references are its `grefA`** (module docstring). -/ +theorem genObjRefs_absA (c : capability.Cap) (r : Fl4SpecA64.GenObjRef) : + r ∈ Fl4SpecA64.genObjRefs (absCapA c) ↔ grefA c = some r := by + cases c + case Frame p sz rt d m e a v => + cases e <;> simp [Fl4SpecA64.genObjRefs, Fl4SpecA64.objRefs, Fl4SpecA64.capIrqs, Fl4SpecA64.capIrqOpt, + Fl4SpecA64.archCapSetMap, Fl4SpecA64.archGenObjRefs, Fl4SpecA64.aobjRef, Fl4SpecA64.ArchCap.acapObj, + Fl4SpecA64.ExtArchCap.objRef, absCapA, grefA, absFrameE] <;> exact eq_comm + case IrqHandler i => + simp [Fl4SpecA64.genObjRefs, Fl4SpecA64.objRefs, Fl4SpecA64.capIrqs, Fl4SpecA64.capIrqOpt, + Fl4SpecA64.archCapSetMap, Fl4SpecA64.archGenObjRefs, Fl4SpecA64.aobjRef, Fl4SpecA64.ArchCap.acapObj, + Fl4SpecA64.ExtArchCap.objRef, absCapA, grefA] + exact ⟨fun ⟨x, hx, e⟩ => by rw [Option.some.inj hx]; exact e, fun e => ⟨_, rfl, e⟩⟩ + all_goals simp [Fl4SpecA64.genObjRefs, Fl4SpecA64.objRefs, Fl4SpecA64.capIrqs, Fl4SpecA64.capIrqOpt, + Fl4SpecA64.archCapSetMap, Fl4SpecA64.archGenObjRefs, Fl4SpecA64.aobjRef, Fl4SpecA64.ArchCap.acapObj, + Fl4SpecA64.ExtArchCap.objRef, absCapA, grefA] <;> exact eq_comm + +theorem ovlA_iff (a b : capability.Cap) : + OvlA (absCapA a) (absCapA b) ↔ ∃ r, grefA a = some r ∧ grefA b = some r := by + unfold OvlA + rw [← Set.nonempty_iff_ne_empty] + constructor + · rintro ⟨r, h1, h2⟩; exact ⟨r, (genObjRefs_absA a r).1 h1, (genObjRefs_absA b r).1 h2⟩ + · rintro ⟨r, h1, h2⟩; exact ⟨r, (genObjRefs_absA a r).2 h1, (genObjRefs_absA b r).2 h2⟩ + +theorem gref_keyA {c : capability.Cap} {k : OKeyA} (h : okeyA c = some k) : grefA c = some (krefA k) := by + cases c <;> simp [okeyA] at h <;> subst h <;> rfl + +/-- **Caps of one key overlap** (x86 `ovl_of_key`). -/ +theorem ovl_of_keyA {a b : capability.Cap} (h : okeyA a = okeyA b) (hn : okeyA a ≠ none) : + OvlA (absCapA a) (absCapA b) := by + obtain ⟨k, hk⟩ := Option.ne_none_iff_exists'.1 hn + exact (ovlA_iff a b).2 ⟨_, gref_keyA hk, gref_keyA (h ▸ hk)⟩ + +theorem refs_ne_of_keyA {c : capability.Cap} (hn : okeyA c ≠ none) : Fl4SpecA64.genObjRefs (absCapA c) ≠ ∅ := by + obtain ⟨k, hk⟩ := Option.ne_none_iff_exists'.1 hn + intro he + have := (genObjRefs_absA c _).2 (gref_keyA hk) + rw [he] at this; exact this + +/-! ## FL4's finality test (x86 `finF_iff`) -/ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +theorem idx_ofMemA {l : List U64} {x : U64} (h : x ∈ l) : ∃ i : Nat, l[i]? = some x := by + obtain ⟨i, hi, e⟩ := List.getElem_of_mem h + exact ⟨i, by rw [List.getElem?_eq_getElem hi, e]⟩ + +set_option maxHeartbeats 1000000 in +/-- **FL4-final ⟹ no other MDB slot overlaps** (x86 `finF_iff`, `→`). -/ +theorem fin_noOvlA {OS : Set U64} {G : List (List U64)} {s : S} (hM : MdbLk st OS G s) + (h0 : ∀ y ∈ OS, y ≠ 0#u64) (hF : FinMA (L.mem s) G) {x : U64} (hx : x ∈ G.flatten) + (hN : NeedsFinA (capAtA (L.mem s) x)) (hz : isZbA (capAtA (L.mem s) x) = false) + (hf : cdt.is_final_capability st s x = ok true) : + ∀ y ∈ G.flatten, y ≠ x → ¬ OvlA (absCapA (capAtA (L.mem s) x)) (absCapA (capAtA (L.mem s) y)) := by + intro y hy hyx hov + have nz : ∀ v ∈ G.flatten, v ≠ 0#u64 := fun v hv => h0 v ((hM.members v).1 hv).1 + by_cases hzy : isZbA (capAtA (L.mem s) y) = true + · exact hF.zfin y hy x hx hyx hzy hov.symm + have hk := hF.key x hx y hy (Ne.symm hyx) hov hz (by simpa using hzy) + (fun h => absurd h.1 (by rw [hN.nf]; decide)) + have hkx : okeyA (capAtA (L.mem s) x) ≠ none := needsFin_keyA hN hz + have hnf : NfKeyA (okeyA (capAtA (L.mem s) x)) := nfKey_ofA hN.nf + have notF : cdt.is_final_capability st s x ≠ ok false := fun e => Bool.false_ne_true (okinj (e.symm.trans hf)) + obtain ⟨ch, hch, hxc⟩ := List.mem_flatten.1 hx + obtain ⟨ch2, hch2, hyc⟩ := List.mem_flatten.1 hy + obtain rfl := hF.conn ch hch ch2 hch2 x hxc y hyc hov + obtain ⟨i, hxi⟩ := idx_ofMemA hxc + obtain ⟨k, hyk⟩ := idx_ofMemA hyc + have hik : i ≠ k := fun e => hyx (by subst e; rw [hxi] at hyk; exact (Option.some.inj hyk).symm) + have hilt : i < ch.length := (List.getElem?_eq_some_iff.1 hxi).1 + have hklt : k < ch.length := (List.getElem?_eq_some_iff.1 hyk).1 + rcases Nat.lt_or_gt_of_ne hik with hlt | hlt + · have hi1 : i + 1 < ch.length := by omega + obtain ⟨b, hb⟩ : ∃ b, ch[i + 1]? = some b := ⟨_, List.getElem?_eq_getElem hi1⟩ + have hkb : okeyA (capAtA (L.mem s) b) = okeyA (capAtA (L.mem s) x) := by + by_cases e : i + 1 = k + · subst e; rw [hb] at hyk; rw [Option.some.inj hyk]; exact hk.symm + · exact hF.chunk ch hch i (i + 1) k x b y hxi hb hyk (by omega) (by omega) hk hkx hnf + have hl := hM.chain ch hch i x b hxi hb + have hbm : b ∈ G.flatten := List.mem_flatten.2 ⟨ch, hch, List.mem_of_getElem? hb⟩ + exact notF (is_final.false_next rfl hl.1 (nz b hbm) rfl (soA_of_keyA hkb.symm hkx)) + · obtain ⟨j, rfl⟩ : ∃ j, i = j + 1 := ⟨i - 1, by omega⟩ + have hj : j < ch.length := by omega + obtain ⟨b, hb⟩ : ∃ b, ch[j]? = some b := ⟨_, List.getElem?_eq_getElem hj⟩ + have hkb : okeyA (capAtA (L.mem s) b) = okeyA (capAtA (L.mem s) x) := by + by_cases e : j = k + · subst e; rw [hb] at hyk; rw [Option.some.inj hyk]; exact hk.symm + · have := hF.chunk ch hch k j (j + 1) y b x hyk hb hxi (by omega) (by omega) hk.symm (hk ▸ hkx) (hk ▸ hnf) + exact this.trans hk.symm + have hl := hM.chain ch hch j b x hb hxi + have hbm : b ∈ G.flatten := List.mem_flatten.2 ⟨ch, hch, List.mem_of_getElem? hb⟩ + exact notF (is_final.false_prev rfl hl.2 (nz b hbm) rfl (soA_of_keyA hkb (hkb ▸ hkx))) + +/-- **No other MDB slot overlaps ⟹ FL4-final** (x86 `finF_iff`, `←`). -/ +theorem fin_of_noOvlA {OS : Set U64} {G : List (List U64)} {s : S} (hM : MdbLk st OS G s) + (h0 : ∀ y ∈ OS, y ≠ 0#u64) {x : U64} (hx : x ∈ G.flatten) + (hkx : okeyA (capAtA (L.mem s) x) ≠ none) + (hno : ∀ y ∈ G.flatten, y ≠ x → ¬ OvlA (absCapA (capAtA (L.mem s) x)) (absCapA (capAtA (L.mem s) y))) : + cdt.is_final_capability st s x = ok true := by + have F := hM.lkF h0 + unfold cdt.is_final_capability + rw [capA_eq, bind_ok] + refine is_final.true_of ?_ ?_ + · by_cases hp0 : L.mem s (wa x 16#u64) = 0#u64 + · exact Or.inl hp0 + · right + obtain ⟨q, hq⟩ := so_total (capA st s (L.mem s (wa x 16#u64))) (capA st s x) + cases q + · exact hq + · exact absurd (ovl_of_keyA (key_of_soA hq).symm hkx) (hno _ (F.pvm x hx hp0) (F.pvne x hx)) + · by_cases hn0 : NxA st s x = 0#u64 + · exact Or.inl hn0 + · right + obtain ⟨q, hq⟩ := so_total (capA st s x) (capA st s (NxA st s x)) + cases q + · exact hq + · exact absurd (ovl_of_keyA (key_of_soA hq) hkx) (hno _ (F.nxm x hx hn0) (F.nxne x hx)) + +/-- **FL4's test is "no other MDB slot overlaps"** (x86 `finF_iff`), for a `NeedsFinA` cap, not a zombie. -/ +theorem finF_iffA {OS : Set U64} {G : List (List U64)} {s : S} (hM : MdbLk st OS G s) + (h0 : ∀ y ∈ OS, y ≠ 0#u64) (hF : FinMA (L.mem s) G) {x : U64} (hx : x ∈ G.flatten) + (hN : NeedsFinA (capAtA (L.mem s) x)) (hz : isZbA (capAtA (L.mem s) x) = false) : + cdt.is_final_capability st s x = ok true ↔ + ∀ y ∈ G.flatten, y ≠ x → ¬ OvlA (absCapA (capAtA (L.mem s) x)) (absCapA (capAtA (L.mem s) y)) := + ⟨fin_noOvlA hM h0 hF hx hN hz, fin_of_noOvlA hM h0 hx (needsFin_keyA hN hz)⟩ + +end + +/-! ## The spec's finality (x86 `final_iff`) -/ + +section spec +open Fl4SpecA64 + +/-- `get_cap` has at most one outcome, the state unchanged. -/ +theorem getCap_singleA {sl : CslotPtr} {σ : State Unit} {c : Cap} (h : (c, σ) ∈ (getCap (Z := Unit) sl σ).1) : + (getCap (Z := Unit) sl σ).1 = {(c, σ)} := by + refine Set.eq_singleton_iff_unique_mem.2 ⟨h, fun r hr => ?_⟩ + obtain ⟨e2, o, ho, hc⟩ := getCap_memA hr + obtain ⟨-, o', ho', hc'⟩ := getCap_memA h + obtain rfl : o' = o := Option.some.inj (ho'.symm.trans ho) + exact Prod.ext (Option.some.inj (hc.symm.trans hc')) e2 + +/-- The TCB cap slots (x86 `tcbCnodeMap_some`, seven slots). -/ +theorem tcbCnodeMap_someA {t : Tcb} {c : CnodeIndex} {a : Cap} (h : tcbCnodeMap t c = some a) : + ∃ i, i < tcbSlotsA ∧ c = tcbCnodeIndex i := by + unfold tcbCnodeMap at h + dsimp only at h + by_cases h6 : c = tcbCnodeIndex tcbHighVSpaceSlot + · exact ⟨6, by decide, h6⟩ + rw [Function.update_of_ne h6] at h + by_cases h5 : c = tcbCnodeIndex tcbStage2Slot + · exact ⟨5, by decide, h5⟩ + rw [Function.update_of_ne h5] at h + by_cases h4 : c = tcbCnodeIndex 4 + · exact ⟨4, by decide, h4⟩ + rw [Function.update_of_ne h4] at h + by_cases h3 : c = tcbCnodeIndex 3 + · exact ⟨3, by decide, h3⟩ + rw [Function.update_of_ne h3] at h + by_cases h2 : c = tcbCnodeIndex 2 + · exact ⟨2, by decide, h2⟩ + rw [Function.update_of_ne h2] at h + by_cases h1 : c = tcbCnodeIndex 1 + · exact ⟨1, by decide, h1⟩ + rw [Function.update_of_ne h1] at h + by_cases h0 : c = tcbCnodeIndex 0 + · exact ⟨0, by decide, h0⟩ + rw [Function.update_of_ne h0] at h + cases h + +/-- **A slot of `th` as the spec names it** (x86 `IsSlot.abs`). -/ +theorem IsSlotA.absA {th : TypedHeapA} {kb : U64} (hP : PlaceA th kb) {y : U64} (h : IsSlotA th y) : + ∃ sl, AbsSlotA th sl ∧ cslotAddrA sl = y := by + obtain ⟨p, ⟨r, hr, i, hi, rfl⟩ | ⟨ht, i, hi, rfl⟩⟩ := h + · have hr60 := cnode_r hP hr + refine ⟨(specPtr p, bitsOfA i r), ⟨p, rfl, Or.inl (by rw [length_bitsOfA]; exact hr)⟩, ?_⟩ + show slotAddrA (specPtr p) (ofBl (bitsOfA i r)) = _ + rw [ofBl_bitsOfA i r (by omega), Nat.mod_eq_of_lt hi] + · refine ⟨(specPtr p, tcbCnodeIndex i), ⟨p, rfl, Or.inr ⟨ht, i, hi, rfl⟩⟩, ?_⟩ + show slotAddrA (specPtr p) (ofBl (tcbCnodeIndex i)) = _ + rw [ofBl_tcbIdx i (by unfold tcbSlotsA at hi; omega)] + +/-- **Every slot the spec can read is a slot of the typed heap** (x86 `cov_of_kt`). -/ +theorem cov_of_ktA {th : TypedHeapA} {mem : U64 → U64} {σ : State Unit} (hK : KTypedA th σ) + (hR : StateRelA th mem σ) {cref : CslotPtr} {cap' : Cap} (hg : (cap', σ) ∈ (getCap (Z := Unit) cref σ).1) : + AbsSlotA th cref := by + obtain ⟨-, o, ho, hc⟩ := getCap_memA hg + obtain ⟨oref, c⟩ := cref + simp only at ho hc + cases o with + | cnode r cs => + obtain ⟨p, rfl, hp⟩ := hK.cn _ r cs ho + obtain ⟨cs', hk', hwf, -⟩ := hR.cnode p r hp + rw [ho] at hk' + obtain rfl : cs = cs' := by cases hk'; rfl + have hd : c ∈ dom cs := by simp [dom, capOf] at hc ⊢; simp [hc] + rw [hwf] at hd + exact ⟨p, rfl, Or.inl (by rw [show c.length = r from hd]; exact hp)⟩ + | tcb t => + obtain ⟨p, rfl, hp⟩ := hK.tc _ t ho + obtain ⟨i, hi, rfl⟩ := tcbCnodeMap_someA (a := cap') hc + exact ⟨p, rfl, Or.inr ⟨hp, i, hi, rfl⟩⟩ + | _ => simp [capOf] at hc + +/-- `CapEqA` preserves `genObjRefs` (mapped `PageTable` virtual-address normalization does not change object refs). -/ +theorem CapEqA.genObjRefs_eq {c : capability.Cap} {a : Cap} (h : CapEqA c a) : + genObjRefs a = genObjRefs (absCapA c) := by + cases c with + | PageTable p m asid va => + cases m + · rw [← h] + · obtain ⟨_, _, rfl, _, _⟩ := CapEqA.pt_mapped_iff.mp h + rfl + | _ => rw [← h] + +/-- The cap of a slot of `th`, as `get_cap`'s only outcome, related by `CapEqA`. -/ +theorem getCap_eqA {th : TypedHeapA} {mem : U64 → U64} {σ : State Unit} (hR : StateRelA th mem σ) + {sl : CslotPtr} (hs : AbsSlotA th sl) : + ∃ c, (getCap (Z := Unit) sl σ).1 = {(c, σ)} ∧ CapEqA (capAtA mem (cslotAddrA sl)) c := by + obtain ⟨c, hc, hrel⟩ := StateRelA.getAbsA hR hs + exact ⟨c, getCap_singleA hc, hrel.2⟩ + +/-- The cap of a slot of `th`, as `get_cap`'s only outcome, when it is not a mapped `PageTable`. -/ +theorem getCap_absA {th : TypedHeapA} {mem : U64 → U64} {σ : State Unit} (hR : StateRelA th mem σ) + {sl : CslotPtr} (hs : AbsSlotA th sl) + (hne : ∀ p asid va, capAtA mem (cslotAddrA sl) ≠ .PageTable p true asid va) : + (getCap (Z := Unit) sl σ).1 = {(absCapA (capAtA mem (cslotAddrA sl)), σ)} := by + obtain ⟨c, hc, hrel⟩ := StateRelA.getAbsA hR hs + rw [← hrel.2.eq_abs hne] at hc + exact getCap_singleA hc + +theorem refs_nullA : genObjRefs (absCapA .Null) = ∅ := by + ext r; rw [genObjRefs_absA]; simp [grefA] + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **Spec finality, in related states** (x86 `final_iff`): no other MDB slot's cap overlaps. -/ +theorem final_iffA {th : TypedHeapA} {kb : U64} (hP : PlaceA th kb) {s : S} {σ : State Unit} + {G : List (List U64)} (hR : StateRelA th (L.mem s) σ) (hM : MdbLk st {a | IsSlotA th a} G s) + (hK : KTypedA th σ) {sl : CslotPtr} (hA : AbsSlotA th sl) + (hne : genObjRefs (absCapA (capAtA (L.mem s) (cslotAddrA sl))) ≠ ∅) : + isFinalCap' (absCapA (capAtA (L.mem s) (cslotAddrA sl))) σ ↔ + ∀ y ∈ G.flatten, y ≠ cslotAddrA sl → + ¬ OvlA (absCapA (capAtA (L.mem s) (cslotAddrA sl))) (absCapA (capAtA (L.mem s) y)) := by + obtain ⟨cx, hgx, heqx⟩ := getCap_eqA hR hA + constructor + · rintro ⟨cref, hset⟩ y hy hyx hov + obtain ⟨hys, -⟩ := (hM.members y).1 hy + obtain ⟨ysl, hysl, rfl⟩ := IsSlotA.absA hP hys + obtain ⟨cy, hgy, heqy⟩ := getCap_eqA hR hysl + have hx : sl ∈ {cref' : CslotPtr | ∃ cap', ((getCap (Z := Unit) cref' : SMonad Unit Cap) σ).1 = {(cap', σ)} ∧ + genObjRefs (absCapA (capAtA (L.mem s) (cslotAddrA sl))) ∩ genObjRefs cap' ≠ ∅} := + ⟨cx, hgx, by rw [heqx.genObjRefs_eq, Set.inter_self]; exact hne⟩ + have hy' : ysl ∈ {cref' : CslotPtr | ∃ cap', ((getCap (Z := Unit) cref' : SMonad Unit Cap) σ).1 = {(cap', σ)} ∧ + genObjRefs (absCapA (capAtA (L.mem s) (cslotAddrA sl))) ∩ genObjRefs cap' ≠ ∅} := + ⟨cy, hgy, by rw [heqy.genObjRefs_eq]; exact hov⟩ + rw [hset] at hx hy' + exact hyx (by rw [(Set.mem_singleton_iff.1 hy').trans (Set.mem_singleton_iff.1 hx).symm]) + · intro hno + refine ⟨sl, Set.ext fun cref => ⟨fun ⟨cap', hg, hov⟩ => ?_, fun h => ?_⟩⟩ + · have hm : (cap', σ) ∈ (getCap (Z := Unit) cref σ).1 := by rw [hg]; rfl + have hcr := cov_of_ktA hK hR hm + obtain ⟨cc, hgc, heqc⟩ := getCap_eqA hR hcr + rw [hgc] at hg + obtain rfl : cc = cap' := (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 hg)).1 + rw [heqc.genObjRefs_eq] at hov + by_cases he : cslotAddrA cref = cslotAddrA sl + · exact Set.mem_singleton_iff.2 (cslotAddrA_inj hP hcr hA he) + · exfalso + have hcn : capAtA (L.mem s) (cslotAddrA cref) ≠ .Null := by + intro e; rw [e, refs_nullA, Set.inter_empty] at hov; exact hov rfl + exact hno _ ((hM.members _).2 ⟨AbsSlotA.isSlot hP hcr, hcn⟩) he hov + · rw [Set.mem_singleton_iff.1 h] + exact ⟨cx, hgx, by rw [heqx.genObjRefs_eq, Set.inter_self]; exact hne⟩ + +/-- **FL4's finality is the spec's** (x86 `VsDecFrame.fin_iff_of`): for a `NeedsFinA`, non-zombie cap of a slot of +`th`, in related states with `FinInvA`. -/ +theorem fin_iff_ofA {th : TypedHeapA} {kb : U64} (hP : PlaceA th kb) {s : S} {σ : State Unit} + {G : MdbGhostA} (hR : StateRelA th (L.mem s) σ) (hM : MdbLk st {a | IsSlotA th a} G.chains s) + (hFI : FinInvA th (L.mem s) σ G) {sl : CslotPtr} (hA : AbsSlotA th sl) + (hN : NeedsFinA (capAtA (L.mem s) (cslotAddrA sl))) (hz : isZbA (capAtA (L.mem s) (cslotAddrA sl)) = false) : + cdt.is_final_capability st s (cslotAddrA sl) = ok true ↔ + isFinalCap' (absCapA (capAtA (L.mem s) (cslotAddrA sl))) σ := by + have hkx := needsFin_keyA hN hz + have hx : cslotAddrA sl ∈ G.chains.flatten := (hM.members _).2 ⟨AbsSlotA.isSlot hP hA, fun e => + hkx (by rw [show capAtA (L.mem s) (cslotAddrA sl) = .Null from e]; rfl)⟩ + rw [finF_iffA hM (fun y hy => hP.slot_ne0 hy) hFI.fm hx hN hz, + final_iffA hP hR hM hFI.kt hA (refs_ne_of_keyA hkx)] + +end + +end spec + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/FinHookFrameA.lean b/proofs/Fl4ProofsA64/P3/FinHookFrameA.lean new file mode 100644 index 000000000..777108da8 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/FinHookFrameA.lean @@ -0,0 +1,223 @@ +/- +P3 (AArch64, delete hooks W1; contract E1–E7): **the Frame finaliser hook** `finHookFrameA : FinHookKA .frame` at +the loop record `DelJWQA s0 (DelXA6 … (J5A st) (J7LA st (J7R2A st ar ipa)))`. + +An unmapped frame changes nothing on either side; a mapped one's unmap is `P3/DelFrameLeafA`'s leaf, the record goes +across by `P3/DelZeroKeepA`, and A64 P1's R2 part of `J7` by `S1aR2.clearing` over `s1aFrameRemove`'s footprint. A +final device frame's `retire_dma_frame` is a no-op under `PlainStore` [P] (frame masks `(0, 0)`). + +Named premises: as `P3/FinHookPtA`'s (`BufOffTabA` [J], `ArchKeepsA`, `S2IpaE`, `P0.ArchTotal` / `ArchPlat` [P]) and +`PlainStore` [P]. Decode-site hypotheses as there. New module. `Std6A` across the unmap by `Std6GA.vsZeroA`. +-/ +import Fl4ProofsA64.P3.DelFrameLeafA +import Fl4ProofsA64.P3.FinHookPtA +import Fl4ProofsA64.P1.S1aR2FinAlias +import Fl4ProofsA64.P1.S1aR2FrameRemove +import Fl4ProofsA64.P3.DelRdXA +import Fl4ProofsA64.P3.DelRec6A +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {m : Fl4SpecA64.McState} +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Pp Tb : Set U64} {Lv : U64 → Nat} {ia ie : Nat} + {N E Rp : Set U64} {κ : U64 → U64} {n : Nat} + +/-- A zero write inside the stage-1 tables outside `Hr` or the stage-2 granules keeps or clears the VSpace words and +keeps every HighVSpace root's reserved word. -/ +theorem s1aWordsKeptA {Pt Pp Tb Hr Te : Set U64} {Lv bits : U64 → Nat} {kb : U64} {s q : S} + (place : S1aR2Place Pt Pp Tb Hr Te Lv bits kb) (aligned : ∀ b, VsR kb Pt b → Aligned b) + (write : Wr0 st (TbO Tb Hr ∪ Gran Te bits) s q) : VsZ st kb Pt s q ∧ HrW st Hr s q := by + have covers : TbO Tb Hr ∪ Gran Te bits ⊆ Pt := Set.union_subset (tbo_sub.trans place.tables) place.granules + refine ⟨Wr0.vsz aligned covers write, Wr0.hrw (fun b hb => aligned b (Or.inl (ptw_mono covers hb))) + (fun r hr => aligned _ (Or.inl ⟨r, place.tables (place.high.sub hr), 4088#u64, by simp, by simp, rfl⟩)) + ?_ write⟩ + intro b hb r hr eq + rcases ptw_union hb with htb | hte + · obtain ⟨t, ht, o, ho, al, rfl⟩ := htb + have same := place.high.tb t ht.1 o ho al r hr eq + subst t + exact ht.2 hr + · exact place.stage2.hr b (gran_ptwb (fun q hq => place.stage2.sz q hq) hte) r hr eq + +/-- **The Frame finaliser hook** (module docstring). -/ +theorem finHookFrameA [PlainStore st] {ar : machine.Arch A} (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (K : ArchKeepsA ar AR) (hIpa : ∀ a, S2IpaE ar a) (s0 : S) (hT : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s0)) + (hbf : DelBufOffA st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n (J5A st) (J7LA st (J7R2A st ar hPl.ipa))) + (Rv : S → A → State Unit → MdbGhostA → U64 → Prop) : + FinHookKA st .frame ar Z.th OS fl4FinaliseCap + (DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st (J7R2A st ar hPl.ipa)))) + (DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st J7R2tA))) + (DelRdXA st ar Z H OS hPl.ipa Rv) (DelRzA st) := by + intro s a σ G sl c ac f D hW hsl hc hk hg hac hf + have hrel : CapRelA c ac := by + obtain ⟨c', h1, h2⟩ := D.rel.getCap_oneA hsl + rw [hg] at h1 + have e : c' = ac := by + have e0 : ({(ac, σ)} : Set (Cap × State Unit)) = {(c', σ)} := h1 + have h3 : (c', σ) ∈ ({(ac, σ)} : Set (Cap × State Unit)) := by rw [e0]; exact rfl + exact (Prod.mk.inj h3).1 + rw [capAtA_eq, hc, e] at h2 + exact h2 + obtain ⟨ptr, sz, rt, dev, mp, ept, asid, va, rfl⟩ : ∃ ptr sz rt dev mp ept asid va, + c = .Frame ptr sz rt dev mp ept asid va := by + cases c <;> simp [finKindA] at hk + exact ⟨_, _, _, _, _, _, _, _, rfl⟩ + have hwf := hrel.1 + simp only [CapWfA] at hwf + obtain ⟨-, -, ⟨h16, hum, hm0, hept⟩, -⟩ := hwf + have eac : ac = .archObjectCap (absFrameE ept (specPtr ptr) (vmRightsOfA rt) (frameSizeOfA sz) dev + (mapDataA mp asid va)) := hac.symm + obtain ⟨hcore, ⟨Q, R, hw, Ac, Bf, stack, J, X⟩, hPA, hZT, hBH, hCT, hFx⟩ := hW + have hOSx : cslotAddrA sl ∈ OS := D.mem_slot hsl + have rd : ∀ (s' : S) (a' : A) (σ' : State Unit), capA st s' (cslotAddrA sl) = .Frame ptr sz rt dev mp ept asid va → + DelRdA st Rv s' a' σ' G (cslotAddrA sl) := fun s' a' σ' e => + delRdA_other (fun _ _ h => by rw [e] at h; cases h) (fun _ _ h => by rw [e] at h; cases h) + (fun _ h => by rw [e] at h; cases h) + -- the spec cap ignores `final` + obtain ⟨d, hd⟩ : ∃ d : Bool, d = true ↔ isFinalCap' ac σ := by + classical + exact ⟨decide (isFinalCap' ac σ), decide_eq_true_iff⟩ + have indep : fl4FinaliseCap (Z := Unit) ac d = fl4FinaliseCap (Z := Unit) ac f := by + rw [eac]; exact fl4FinaliseCap_frame_indepA _ _ _ _ _ _ _ _ + rw [fin_cap_eq_obj (by simp)] + cases mp + · -- not mapped + have e : objects.finalise_cap_object st ar s a (.Frame ptr sz rt dev false ept asid va) f = + ok ((capability.Cap.Null, capability.Cap.Null), s, a) := by + unfold objects.finalise_cap_object + cases f <;> cases dev <;> simp + have hnoop : ((Cap.nullCap, Cap.nullCap), σ) ∈ (fl4FinaliseCap (Z := Unit) ac d σ).1 := by + cases ept + · rw [eac] + simp only [absFrameE, Bool.false_eq_true, ↓reduceIte, mapDataA] + rw [fl4FinaliseCap_plainA (by intro h; exact h)] + cases d <;> exact rfl + · exact absurd (hept rfl) (by simp) + rw [e, WP.spec_ok] + refine ⟨rfl, trivial, trivial, σ, G, ⟨d, hd, hnoop⟩, D, hc, fun y _ _ => Or.inl rfl, ?_, + fun _ => ⟨⟨rd _ _ _ hc, rdThA_of_ne fun t h => by rw [hc] at h; cases h⟩, m, (by + have h := rdXA_of_J7A (st := st) (x := cslotAddrA sl) (σ := σ) (J.j7.preMapA) (fun _ _ => rfl) (by intros; exact VsZ.refl s) (fun _ _ _ _ _ _ _ _ _ _ vr R2 => pnFrameA hA hPl vr J.ent.os J.ent.heap J.ko J.ent.ksw D.kind hOSx R2 + ⟨ptr, sz, rt, dev, false, ept, asid, va⟩ hc f e) + exact h)⟩, fun _ _ _ h => by cases h⟩ + exact ⟨fun _ => ⟨hcore, ⟨Q, R, hw, Ac, Bf, stack, J, X⟩, hPA, hZT, hBH, hCT, hFx⟩, + fun _ => ⟨hcore, ⟨Q, R, hw, Ac, Bf, stack, J.mapJ7A fun _ _ => trivial, X⟩, hPA, hZT, hBH, hCT, hFx⟩⟩ + have h0 : asid ≠ 0#u64 := hm0 rfl + have kb : L.ksb s = L.ksb s0 := hcore.1 + have hT' : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s) := by rw [kb]; exact hT + have J0 := J.j + obtain ⟨Jz, -⟩ := id J0 + have hfa := FootA.aligned Jz (fun c hc' off ho hal => bxi_nodes J.ent.bxi c hc' off ho hal) + J.scAl J.ap.vcAl + have hcx : capA st s (cslotAddrA sl) = .Frame ptr sz rt dev true ept asid va := hc + have hVa : ept = false → VaOkE va := by + intro he + have := D.cv _ hOSx + rw [hcx] at this + exact this rfl he + have leaf := unmap_frame_leafA K J0 hfa J.tabS1 J.tabExt J.vsLeafA.1 hT' (hIpa a) h16 sz va ptr ept hVa + (vmRightsOfA rt) dev f + obtain ⟨out, eout, σ', hmem, jx, HS, HC, hform⟩ := spec_existsA leaf + obtain ⟨s1, a1⟩ := out + have tail : (if f then + if dev then do + let a2 ← objects.retire_dma_frame st ar s1 a1 ptr sz + ok ((capability.Cap.Null, capability.Cap.Null), s1, a2) + else ok ((capability.Cap.Null, capability.Cap.Null), s1, a1) + else ok ((capability.Cap.Null, capability.Cap.Null), s1, a1)) = + ok ((capability.Cap.Null, capability.Cap.Null), s1, a1) := by + cases f <;> cases dev <;> simp [plain_retire] + have ecall : objects.finalise_cap_object st ar s a (.Frame ptr sz rt dev true ept asid va) f = + ok ((capability.Cap.Null, capability.Cap.Null), s1, a1) := by + rw [fin_frame_eqA ar s a ptr asid va sz rt dev ept f h0, eout, bind_ok] + exact tail + rw [ecall, WP.spec_ok] + have hmem' : ((Cap.nullCap, Cap.nullCap), σ') ∈ (fl4FinaliseCap (Z := Unit) ac d σ).1 := by + rw [indep, eac]; exact hmem + have done : DStateA st Z.th OS s1 σ' G ∧ (∀ y ∈ OS, capA st s1 y = capA st s y) ∧ + DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st (J7R2A st ar hPl.ipa))) + s1 a1 σ' G ∧ + (∀ Rj' : Set U64 → JSlotA S, (∀ Pt Ub : Set U64, VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt → + (∀ q, SchedPageA st { Z with G := G, Q := Q, R := R, hw := hw } OS s q → q ∈ Pt) → + J7R2A st ar hPl.ipa Pt { Z with G := G, Q := Q, R := R, hw := hw } H OS s σ m → + Rj' Pt { Z with G := G, Q := Q, R := R, hw := hw } H OS s1 σ' m) → + J7LA st Rj' { Z with G := G, Q := Q, R := R, hw := hw } H OS s1 σ' m) := by + rcases hform with ⟨e, eσ⟩ | ⟨p, i, hi, hp, e⟩ + · obtain ⟨rfl, rfl⟩ := Prod.mk.inj e + subst eσ + exact ⟨D, fun _ _ => rfl, ⟨hcore, ⟨Q, R, hw, Ac, Bf, stack, J, X⟩, hPA, hZT, hBH, hCT, hFx⟩, J.j7.preMapA⟩ + · dsimp only at e jx + subst e + have hp' : SchedPageA st { Z with G := G, Q := Q, R := R, hw := hw } OS s p := hp.schedA + -- A64 P1's walk facts over the call + have p1 : ∀ Pt Ub : Set U64, VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt → + ∀ (Pp2 Tb2 Hr Te Kt : Set U64) (Lv2 Re bits : U64 → Nat), + S1aR2 st H OS Pt Pp2 Tb2 Hr Te Kt Lv2 Re bits hPl.ipa s → + VsZ st (L.ksb s) Pt s (L.wr s (peAF p i) 0#u64) ∧ HrW st Hr s (L.wr s (peAF p i) 0#u64) := by + intro Pt Ub vr Pp2 Tb2 Hr Te Kt Lv2 Re bits R2 + have R2' := S1aR2.activePools R2 + have rem := s1aFrameRemove (f := ⟨ptr, sz, rt, dev, true, ept, asid, va⟩) hA hPl a R2' + (fun b hb => vr.al b (Or.inl hb)) + have h0' : (asid != 0#u64) = true := by simp [h0] + simp only [h0', ↓reduceIte] at rem + rw [eout, WP.spec_ok] at rem + exact s1aWordsKeptA R2.place vr.al rem.1 + have R2 : ∀ Pt Ub : Set U64, VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt → + (∀ q, SchedPageA st { Z with G := G, Q := Q, R := R, hw := hw } OS s q → q ∈ Pt) → + J7R2A st ar hPl.ipa Pt { Z with G := G, Q := Q, R := R, hw := hw } H OS s σ m → + J7R2A st ar hPl.ipa Pt { Z with G := G, Q := Q, R := R, hw := hw } H OS (L.wr s (peAF p i) 0#u64) σ' m := by + rintro Pt Ub vr link ⟨Pp2, Tb2, Hr, Te, Kt, Lv2, Re, bits, R2, sep, KLv, ktK, hik⟩ + obtain ⟨words, headers⟩ := p1 Pt Ub vr Pp2 Tb2 Hr Te Kt Lv2 Re bits R2 + have hal : Aligned (peAF p i) := vr.al _ (vsW_pageA link hp' hi) + have ht := tab_of_w vr (writesIn_wr (v := 0#u64) hal) (fun b hb => hb ▸ hp.notAsA hT' hi) + have osk : OSKeep st OS s (L.wr s (peAF p i) 0#u64) := + OSKeep.of_in J.ent.os (writesIn_wr hal) fun x hx b hb h1 h2 hF => + vr.os b (hF ▸ vsW_pageA link hp' hi) ⟨x, hx, h1, h2⟩ + exact ⟨_, Tb2, _, Te, Kt, Lv2, Re, bits, S1aR2.clearing (S1aR2.activePools R2) J.ent.heap D.kind + R2.place ⟨L.ksb_wr _ _ _, ht⟩ words headers (fun y hy => osk.capA hy), sep.keep osk, KLv, ktK, + S1aHiK.keep hik ⟨EdgeKeepOn.ofVsZ R2 words, fun y hy R m a c => ⟨y, hy, m, a, by rwa [osk.capA hy] at c⟩⟩⟩ + have hdr : ∀ y ∈ OS, ∀ q mp ad, capA st s y = .HighVSpace q mp ad → + L.mem (L.wr s (peAF p i) 0#u64) (wa q 4088#u64) = L.mem s (wa q 4088#u64) := by + intro y hy q mq ad hq + obtain ⟨Pt, Ub, vr, -, -, -, -, -, -, -, -, -, -, -, ⟨Pp2, Tb2, Hr, Te, Kt, Lv2, Re, bits, R2, -⟩⟩ := J.j7 + exact (p1 Pt Ub vr Pp2 Tb2 Hr Te Kt Lv2 Re bits R2).2 q (by rw [R2.highRoots]; exact ⟨y, hy, mq, ad, hq⟩) + obtain ⟨D', J', hPA', hZT', osk, J7f⟩ := delVs_zeroKeepA (Z := { Z with G := G, Q := Q, R := R, hw := hw }) J D hPA + hZT hp' hi (hp.notAsA hT' hi) (fun h => hbf Q R hw G Ac Bf stack m s a σ J _ h p hp' i hi rfl) + jx HS HC hdr R2 + have X' : Std6GA st Ag Z H OS Bf (L.wr s (peAF p i) 0#u64) σ' := + (Std6GA.vsZeroA J (X.ghostA G Q R hw) D.sub (fun _ _ _ link => vsW_pageA link hp' hi) HS + (fun p' dev pg h => HC.dataA p' dev pg h)).unghostA + exact ⟨D', fun y hy => osk.capA hy, + ⟨⟨(L.ksb_wr _ _ _).trans hcore.1, (L.cpu_wr _ _ _).trans hcore.2⟩, ⟨Q, R, hw, Ac, Bf, stack, J', X'⟩, hPA', + hZT', bufHold_sameA st hBH (fun y hy => osk.capA hy), + ctOff_sameA st hCT (L.ksb_wr _ _ _) (fun y hy => osk.capA hy), hFx.keepA st D.sub (fun y hy => osk.capA hy)⟩, J7f⟩ + obtain ⟨D', caps, hW', J7f⟩ := done + have hc' : capA st s1 (cslotAddrA sl) = .Frame ptr sz rt dev true ept asid va := (caps _ hOSx).trans hc + exact ⟨rfl, trivial, trivial, σ', G, ⟨d, hd, hmem'⟩, D', hc', fun y hy _ => Or.inl (caps y hy), + ⟨fun _ => hW', fun _ => ⟨hW'.1, by + obtain ⟨Q', R', hw', Ac', Bf', stack', J', X'⟩ := hW'.2.1 + exact ⟨Q', R', hw', Ac', Bf', stack', J'.mapJ7A fun _ _ => trivial, X'⟩, hW'.2.2⟩⟩, + fun _ => ⟨⟨rd _ _ _ hc', rdThA_of_ne fun t h => by rw [hc'] at h; cases h⟩, m, (by + have h := rdXA_of_J7A (st := st) (x := cslotAddrA sl) (σ := σ) J7f caps (fun _ _ _ _ _ _ _ _ _ _ vr _ R2 => by + have R2' := S1aR2.activePools R2 + have rem := s1aFrameRemove (f := ⟨ptr, sz, rt, dev, true, ept, asid, va⟩) hA hPl a R2' + (fun b hb => vr.al b (Or.inl hb)) + have h0' : (asid != 0#u64) = true := by simp [h0] + simp only [h0', ↓reduceIte] at rem + rw [eout, WP.spec_ok] at rem + exact (s1aWordsKeptA R2.place vr.al rem.1).1) (fun _ _ _ _ _ _ _ _ _ _ vr R2 => pnFrameA hA hPl vr J.ent.os J.ent.heap J.ko J.ent.ksw D.kind hOSx R2 + ⟨ptr, sz, rt, dev, true, ept, asid, va⟩ hcx f ecall) + exact h)⟩, fun _ _ _ h => by cases h⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/FinHookPoolA.lean b/proofs/Fl4ProofsA64/P3/FinHookPoolA.lean new file mode 100644 index 000000000..738142906 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/FinHookPoolA.lean @@ -0,0 +1,175 @@ +/- +P3 (AArch64, delete hooks W2, Architect ruling E2′ / Option P): **the AsidPool finaliser hook** `finHookPoolA : +FinHookKA .asidPool` with the window invariant `Wx` of the ruling. + +`W` is the loop record as for the other kinds. `Wx` is `W` with J7's R2 part relaxed to `True` (`J7R2tA`): after +`delete_asid_pool` the slot still holds its AsidPool cap but the pool's ASID-table entry is gone, so A64 P1's +`S1aPoolCaps.cap` (inside R2 over all `OS`) fails. The slot-pinned fact lives in `Rd`: **`DelRdXA`** is `DelRdA` and +J7 with R2 over the slots other than the slot (`J7R2xA`, A64 P1's `S1aDelPn`), proved by `S1aR2.poolClearedA` +(the pool is final) or `S1aFin.pool` (not final: another cap holds the pool). + +Named premises as `P3/FinHookRootA`'s plus the decode-site hypothesis `hYAs` (the windows' words are off the ASID +table). New module. +-/ +import Fl4ProofsA64.P3.DelRdXA +import Fl4ProofsA64.P3.DelPoolRecA +import Fl4ProofsA64.P3.FinHookRootKA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {m : Fl4SpecA64.McState} +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Pp Tb : Set U64} {Lv : U64 → Nat} {ia ie : Nat} + {N E Rp : Set U64} {κ : U64 → U64} {n : Nat} + +set_option maxHeartbeats 1000000 in +/-- **The AsidPool finaliser hook** (module docstring). -/ +theorem finHookPoolA {ar : machine.Arch A} (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) (K : ArchKeepsA ar AR) + {Clock : A → Fl4SpecA64.MachineState → Prop} (Da : DelArchA st ar AR Clock) (s0 : S) (hT : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s0)) + (hYAs : ∀ y ∈ Z.Y, ∀ j : U64, j.val < 512 → ¬ AsW (L.ksb s0) (peAF y j)) + (Rv : S → A → State Unit → MdbGhostA → U64 → Prop) : + FinHookKA st .asidPool ar Z.th OS fl4FinaliseCap + (DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st (J7R2A st ar hPl.ipa)))) + (DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st J7R2tA))) + (DelRdXA st ar Z H OS hPl.ipa Rv) (DelRzA st) := by + intro s a σ G sl c ac f D hW hsl hc hk hg hac hf + have hrel : CapRelA c ac := by + obtain ⟨c', h1, h2⟩ := D.rel.getCap_oneA hsl + rw [hg] at h1 + have e : c' = ac := by + have e0 : ({(ac, σ)} : Set (Cap × State Unit)) = {(c', σ)} := h1 + have h3 : (c', σ) ∈ ({(ac, σ)} : Set (Cap × State Unit)) := by rw [e0]; exact rfl + exact (Prod.mk.inj h3).1 + rw [capAtA_eq, hc, e] at h2 + exact h2 + obtain ⟨ptr, base, rfl⟩ : ∃ ptr base, c = .AsidPool ptr base := by + cases c <;> simp [finKindA] at hk + exact ⟨_, _, rfl⟩ + have hfin := hf trivial + have hwf := hrel.1 + simp only [CapWfA] at hwf + obtain ⟨h16, hb0⟩ := hwf + have eac : ac = absCapA (.AsidPool ptr base) := hac.symm + obtain ⟨hcore, ⟨Q, R, hw, Ac, Bf, stack, J, X⟩, hPA, hZT, hBH, hCT, hFx⟩ := hW + have hOSx : cslotAddrA sl ∈ OS := D.mem_slot hsl + have kb : L.ksb s = L.ksb s0 := hcore.1 + have hT' : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s) := by rw [kb]; exact hT + have hY' : ∀ y ∈ Z.Y, ∀ j : U64, j.val < 512 → ¬ AsW (L.ksb s) (peAF y j) := by rw [kb]; exact hYAs + have rdOther : ∀ (s' : S) (a' : A) (σ' : State Unit), + capA st s' (cslotAddrA sl) = .AsidPool ptr base → + (∀ p b', capA st s' (cslotAddrA sl) = .AsidPool p b' → + isFinalCap' (absCapA (capA st s' (cslotAddrA sl))) σ' → ∀ i, σ'.archState.armAsidTable i ≠ some (specPtr p)) → + DelRdA st Rv s' a' σ' G (cslotAddrA sl) := fun s' a' σ' e hp => + ⟨fun r g h => absurd h (by rw [e]; simp), hp, fun v h => absurd h (by rw [e]; simp)⟩ + obtain ⟨Pt, Ub, vr, pn, arm, capc, vc, df, hicap, link, vcks, fu, ual, vcu, Pp2, Tb2, Hr, Te, Kt, Lv2, Re, bits, R2, sep, KLv, ktK, hik⟩ := J.j7 + obtain ⟨i0, hi0, hbi0, hptr0, word0⟩ := R2.poolCaps.cap (cslotAddrA sl) hOSx ptr base hc + have hiand0 : i0 &&& 127#u64 = i0 := + UScalar.eq_of_val_eq (by rw [and_maskA i0 7 _ rfl]; exact Nat.mod_eq_of_lt hi0) + have hAt : ∀ i : U64, i &&& 127#u64 = i → wa (L.ksb s) (AtOff i) = atAF (L.ksb s) i := fun i hi => by + unfold AtOff atAF; rw [hi] + have hin : ∀ i : U64, i.val < 128 → base.val = i.val * 512 → L.mem s (atAF (L.ksb s) i) = ptr := by + intro i hi hbi + have e : i = i0 := UScalar.eq_of_val_eq (by omega) + subst e + rw [← hAt i hiand0] + exact word0 + have hfinal : cdt.is_final_capability_with_cap st s (cslotAddrA sl) (.AsidPool ptr base) = ok f := by + obtain ⟨b, hb, hbf⟩ := (finalHypA (st := st) Z.th OS G.chains s σ sl (.AsidPool ptr base) D.toFinStA hsl hc + trivial).1 + have e : b = f := by + rw [eac] at hfin + exact Bool.eq_iff_iff.2 (hbf.trans hfin.symm) + rw [hb, e] + have mdb : MdbS st OS s := ⟨G.chains, D.lk, D.rng, D.kind, D.nl, D.cls⟩ + have R2a := S1aR2.activePools R2 + have hHr := R2a.highRoots + have high : HvRt st OS s ⊆ Hr := fun q hq => by rw [hHr]; exact hq + have M : S1aDelM st H OS Pt (PoolSet st Pp2 s) Tb2 Te Kt Lv2 Re bits hPl.ipa s := R2a.toDelM + rw [fin_cap_eq_obj (by simp)] + cases f + · -- not final: nothing happens, another cap holds the pool + have e : objects.finalise_cap_object st ar s a (.AsidPool ptr base) false = + ok ((capability.Cap.Null, capability.Cap.Null), s, a) := rfl + have P1 := S1aFin.pool hA J.ent.os vr J.ent.heap R2.place high M mdb hOSx hc hfinal a + rw [fin_cap_eq_obj (by simp), e, WP.spec_ok] at P1 + obtain ⟨-, hdel⟩ := P1 + have hnoop : ((Cap.nullCap, Cap.nullCap), σ) ∈ (fl4FinaliseCap (Z := Unit) ac false σ).1 := by + rw [eac] + simp only [absCapA] + rw [fl4FinaliseCap_plainA (by intro h; exact h)] + exact rfl + rw [e, WP.spec_ok] + refine ⟨rfl, trivial, trivial, σ, G, ⟨false, hfin, hnoop⟩, D, hc, fun y _ _ => Or.inl rfl, ?_, ?_, + fun _ _ _ h => by cases h⟩ + · exact ⟨fun h => absurd (Or.inl rfl) h, fun _ => ⟨hcore, ⟨Q, R, hw, Ac, Bf, stack, + J.mapJ7A fun _ _ => trivial, X⟩, hPA, hZT, hBH, hCT, hFx⟩⟩ + · refine fun _ => ⟨⟨rdOther _ _ _ hc fun p b' h hfin' => ?_, rdThA_of_ne fun t h => by rw [hc] at h; cases h⟩, m, Pt, Ub, vr, pn, arm, capc, vc, df, hicap, link, vcks, + fu, ual, vcu, PoolSet st Pp2 s, Tb2, Te, Kt, Lv2, Re, bits, hdel, sep, KLv, ktK, hik.kt, fun y hy => hik.hdr y hy.1⟩ + rw [hc, ← eac] at hfin' + exact absurd (hfin.2 hfin') (by decide) + · -- final + unfold objects.finalise_cap_object + simp only [↓reduceIte] + refine WP.spec_bind (delete_asid_pool_recA hA hPl K Da J D hPA hZT hT' hY' h16 hb0 hptr0 hin) ?_ + rintro ⟨s1, a1⟩ ⟨σ', hw', hmem, D', caps, Jx, hPA', hZT', hk1, hc1, J7f, K6, i, hi, hbi, e, htab⟩ + dsimp only at hmem D' caps Jx hPA' hZT' hk1 hc1 J7f K6 e htab ⊢ + have X' : Std6GA st Ag Z H OS Bf s1 σ' := (K6 D.sub (X.ghostA G Q R hw)).unghostA + refine (WP.spec_ok _).2 ?_ + have hc' : capA st s1 (cslotAddrA sl) = .AsidPool ptr base := (caps _ hOSx).trans hc + have hmem' : ((Cap.nullCap, Cap.nullCap), σ') ∈ (fl4FinaliseCap (Z := Unit) ac true σ).1 := by + rw [eac] + simp only [absCapA] + rw [fl4FinaliseCap_plainA (by intro h; exact h)] + exact mem_bindA.2 ⟨((), σ'), hmem, rfl⟩ + have hW' : DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st J7R2tA)) s1 a1 σ' G := + ⟨⟨hk1.trans hcore.1, hc1.trans hcore.2⟩, ⟨Q, R, hw', Ac, Bf, stack, Jx, X'⟩, hPA', hZT', bufHold_sameA st hBH caps, ctOff_sameA st hCT hk1 caps, hFx.keepA st D.sub caps⟩ + have hi0eq : i = i0 := UScalar.eq_of_val_eq (by omega) + subst hi0eq + refine ⟨rfl, trivial, trivial, σ', G, ⟨true, hfin, hmem'⟩, D', hc', fun y hy _ => Or.inl (caps y hy), + ⟨fun h => absurd (Or.inl rfl) h, fun _ => hW'⟩, fun _ => ⟨?_, m, ?_⟩, fun _ _ _ h => by cases h⟩ + · refine ⟨rdOther _ _ _ hc' fun p b' h hfin' => ?_, rdThA_of_ne fun t h => by rw [hc'] at h; cases h⟩ + rw [hc'] at h + injection h with hp hb + subst hp + intro j hj + rw [htab] at hj + by_cases ej : j = BitVec.ofNat 7 i.val + · subst ej; simp at hj + · rw [Function.update_of_ne ej] at hj + have tab0 := J.j.1.vs.table i hi + rw [hin i hi hbi] at tab0 + rw [optPtrA_ne hptr0] at tab0 + exact ej (J.tabInj _ _ _ hj tab0) + · refine J7f (J7R2xA st ar hPl.ipa (cslotAddrA sl)) fun Pt' Ub' vr' link' Rpre => ?_ + obtain ⟨Pp3, Tb3, Hr3, Te3, Kt3, Lv3, Re3, bits3, R3, sep3, KLv3, ktK3, hik3⟩ := Rpre + have vz3 : VsZ st (L.ksb s) Pt' s s1 := by + rw [e]; exact VsZ.wr0 vr'.al (vr'.al _ (vsW_asidA hi)) + have hik3' : S1aHiK st (OS \ {cslotAddrA sl}) Kt3 KLv3 s1 := + S1aHiK.keep hik3 ⟨EdgeKeepOn.ofVsZ R3 vz3, fun y hy R m a c => ⟨y, hy.1, m, a, by rwa [caps y hy.1] at c⟩⟩ + have R3a := S1aR2.activePools R3 + have hHr3 := R3a.highRoots + have high3 : HvRt st OS s ⊆ Hr3 := fun q hq => by rw [hHr3]; exact hq + have M3 : S1aDelM st H OS Pt' (PoolSet st Pp3 s) Tb3 Te3 Kt3 Lv3 Re3 bits3 hPl.ipa s := R3a.toDelM + have fin' : cdt.is_final_capability_with_cap st s (cslotAddrA sl) (.AsidPool ptr base) = ok true := hfinal + have done := S1aR2.poolClearedA J.ent.os vr' J.ent.heap R3.place high3 M3 mdb hOSx hc fin' + (i := i) hi hbi hptr0 (by rw [hAt i hiand0]; exact hin i hi hbi) + refine ⟨PoolSet st Pp3 s, Tb3, Te3, Kt3, Lv3, Re3, bits3, ?_, (fun k hk y hy p b' ii d cap => ?_), KLv3, ktK3, hik3'⟩ + · rw [e] + rw [hAt i hiand0] at done + exact done + · exact sep3 k hk y hy p b' ii d (by rw [← caps y hy]; exact cap) + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/FinHookPtA.lean b/proofs/Fl4ProofsA64/P3/FinHookPtA.lean new file mode 100644 index 000000000..29b23e2b1 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/FinHookPtA.lean @@ -0,0 +1,273 @@ +/- +P3 (AArch64, delete hooks W1; contract E1–E7): **the PageTable finaliser hook** `finHookPtA : FinHookKA .pageTable` +at the loop record `DelJWQA s0 (DelXA6 … (J5A st) (J7LA st (J7R2A st ar ipa)))`. + +A non-final or unmapped cap changes nothing on either side; a final mapped cap's `unmap_page_table` is +`P3/DelUnmapLeafA`'s leaf, the record goes across by `P3/DelZeroKeepA`, and A64 P1's R2 part of `J7` comes from +`vspace.unmap_page_table.activeLookup` / `activeUnr` (**`S1aR2.zeroedKeepA`**: R2 over every slot). + +Named premises: `BufOffTabA` [J] (local until the merge line's `BufOffA`), `ArchKeepsA` [P], `S2IpaE` [P], +`P0.ArchTotal` / `ArchPlat` [P]. Decode-site hypotheses (state-independent, at the entry's block base): `OffAsE`, +the VSpace words' alignment (`hvs`'s alignment half). New module. `Std6A` across the unmap by `Std6GA.vsZeroA`. +-/ +import Fl4ProofsA64.P3.DelUnmapLeafA +import Fl4ProofsA64.P3.DelRdXA +import Fl4ProofsA64.P3.CnodeDelDefA +import Fl4ProofsA64.P3.DelVaA +import Fl4ProofsA64.P3.DelHookKindA +import Fl4ProofsA64.P3.CnodeFinalA +import Fl4ProofsA64.P1.S1aR2FinPt +import Fl4ProofsA64.P1.S1aStepR2Boot +import Fl4ProofsA64.P3.DelRec6A +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {m : Fl4SpecA64.McState} +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-- **R2 over every slot after a step that keeps or clears the VSpace words and keeps the capabilities**, given +the walk's facts at the new state (A64 P1's `S1aR2.dropSlot` without the excluded slot). -/ +theorem S1aR2.zeroedKeepA {H : Heap} {OS Pt Pp Tb Hr Te Kt : Set U64} {Lv Re bits : U64 → Nat} {n : U64} {s q : S} + (h : S1aR2 st H OS Pt Pp Tb Hr Te Kt Lv Re bits n s) (heap : HeapOK H) (kinds : KindA st OS s) + (table : TabKeep st s q) (mem : VsZ st (L.ksb s) Pt s q) (headers : HrW st Hr s q) + (caps : ∀ y ∈ OS, capA st q y = capA st s y) + (lookup : VsLk st H OS Pp Tb Te Kt Lv Re bits q) (root : RootAgreement st OS Pp q) + (stage1 : VsTyR st (L.ksb q) (PoolSet st Pp q) ∅ Tb Lv q) + (stage2 : S2TyR st n (PoolSet st Pp q) Hr Te Re bits q) : + S1aR2 st H OS Pt Pp Tb (HvRt st OS q) Te Kt Lv Re bits n q := by + have tab := table.toDel + have poolWord (p : U64) (hp : p ∈ PoolSet st Pp s) (o : U64) (ho : o.val < 4096) (al : o.val % 8 = 0) : + L.mem q (wa p o) = L.mem s (wa p o) ∨ L.mem q (wa p o) = 0#u64 := + mem _ (Or.inl ⟨p, h.place.pools hp, o, ho, al, rfl⟩) + have eHr : HvRt st OS q = Hr := by + rw [h.highRoots] + refine Set.ext fun p => ⟨?_, ?_⟩ + · rintro ⟨y, hy, m, a, eq⟩ + exact ⟨y, hy, m, a, (caps y hy).symm.trans eq⟩ + · rintro ⟨y, hy, m, a, eq⟩ + exact ⟨y, hy, m, a, (caps y hy).trans eq⟩ + have pools : S1aPoolCaps st H OS Pt Pp q := h.poolCaps.zero table poolWord (fun y hy _ _ => by rw [caps y hy]) + have back {tag a t : U64} {sh b : Nat} (path : S1aPath st Pp n q tag a sh b t) : S1aPath st Pp n s tag a sh b t := by + refine path.back (fun _ _ e => e.back tab poolWord) (fun a raw entry kind => ?_) + (fun _ _ _ _ _ old _ bd al => mem _ (Or.inl (h.pathWord old bd al))) + obtain ⟨y, hy, backed⟩ := h.core.root.backed a raw entry + rw [kind] at backed + rcases backed with ⟨bad, _⟩ | ⟨bad, _⟩ | ⟨_, backed⟩ + · cases bad + · cases bad + exact headers _ (by rw [h.highRoots]; exact ⟨y, hy, true, a, backed⟩) + have paths : S1aPathBacking st H OS Pp Tb Te Lv Re bits n q := + h.paths.shrink (fun _ _ _ _ _ path => back path) + (fun _ _ _ _ _ path _ bd al => mem _ (Or.inl (h.pathWord (back path) bd al))) + (fun _ _ _ _ _ _ _ y hy _ eq => ⟨y, hy, (caps y hy).trans eq⟩) + (fun _ _ _ _ _ _ _ _ y hy _ _ _ _ _ => ⟨y, hy, caps y hy⟩) + exact h.zeroedOn heap kinds tab mem headers (fun _ h => h) caps lookup root stage1 (by rw [eHr]; exact stage2) + pools paths + +/-- The page of a written table word is a scheduler-or-VSpace table page. -/ +theorem VsPageA.schedA {Z : XSetsA} {OS : Set U64} {s : S} {p : U64} (h : VsPageA Z p) : SchedPageA st Z OS s p := by + rcases h with h | ⟨h | ⟨r, hr, rfl | rfl⟩⟩ | ⟨y, hy, rfl⟩ + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr (Or.inl h)) + · exact Or.inr (Or.inr (Or.inr (Or.inl hr))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨r, hr, rfl⟩)))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨y, hy, rfl⟩)))))) + +theorem VsPageA.notAsA {Z : XSetsA} {p kb i : U64} (h : VsPageA Z p) (hT : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 kb) + (hi : i.val < 512) : ¬ AsW kb (peAF p i) := by + refine hT p i hi ?_ + rcases h with h | h | h + · exact Or.inr (Or.inr (Or.inl h)) + · exact Or.inl h + · exact Or.inr (Or.inr (Or.inr h)) + +/-- A table cap with no mapping, or a non-final one: the finaliser does nothing on the spec side. -/ +theorem fin_pt_noopA {ac : Cap} {σ : State Unit} {ptr asid va : U64} {mp f : Bool} + (hac : CapEqA (.PageTable ptr mp asid va) ac) (h : f = false ∨ mp = false) : + ((Cap.nullCap, Cap.nullCap), σ) ∈ (fl4FinaliseCap (Z := Unit) ac f σ).1 := by + cases mp + · have e : ac = .archObjectCap (.pageTableCap (specPtr ptr) .normalPtT none) := hac.symm + subst e + rw [fl4FinaliseCap_plainA (by intro h; exact h)] + cases f <;> exact rfl + · obtain ⟨asid', va', rfl, -, -⟩ := CapEqA.pt_mapped_iff.1 hac + obtain rfl : f = false := h.resolve_right (by simp) + rw [fl4FinaliseCap_pt_nfA (extKindOfAsid_eqA asid' σ)] + exact rfl + +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Pp Tb : Set U64} {Lv : U64 → Nat} {ia ie : Nat} + {N E Rp : Set U64} {κ : U64 → U64} {n : Nat} + +variable (st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n) in +/-- **[J] `BufOffTabA` at every state of the loop record** (module docstring). -/ +def DelBufOffA (J5 J7 : JSlotA S) : Prop := + ∀ (Q : U64 → U64 → List U64) (R : U64 → List U64) (hw : HwRootA) (G : MdbGhostA) (Ac : Set U64) + (Bf : Set (U64 × Fl4SpecA64.Word)) (stack : U64 → List U64) (m : McState) (s : S) (a : A) (σ : State Unit), + JStage3TA st AR { Z with G := G, Q := Q, R := R, hw := hw } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 + s a σ m → + BufOffTabA st { Z with G := G, Q := Q, R := R, hw := hw } OS Bf s + +/-- **The PageTable finaliser hook** (module docstring). -/ +theorem finHookPtA {ar : machine.Arch A} (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) (K : ArchKeepsA ar AR) + (hIpa : ∀ a, S2IpaE ar a) (s0 : S) (hT : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s0)) + (hbf : DelBufOffA st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n (J5A st) (J7LA st (J7R2A st ar hPl.ipa))) + (Rv : S → A → State Unit → MdbGhostA → U64 → Prop) : + FinHookKA st .pageTable ar Z.th OS fl4FinaliseCap + (DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st (J7R2A st ar hPl.ipa)))) + (DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st J7R2tA))) + (DelRdXA st ar Z H OS hPl.ipa Rv) (DelRzA st) := by + intro s a σ G sl c ac f D hW hsl hc hk hg hac hf + have hrel : CapRelA c ac := by + obtain ⟨c', h1, h2⟩ := D.rel.getCap_oneA hsl + rw [hg] at h1 + have e : c' = ac := by + have e0 : ({(ac, σ)} : Set (Cap × State Unit)) = {(c', σ)} := h1 + have h3 : (c', σ) ∈ ({(ac, σ)} : Set (Cap × State Unit)) := by rw [e0]; exact rfl + exact (Prod.mk.inj h3).1 + rw [capAtA_eq, hc, e] at h2 + exact h2 + obtain ⟨ptr, mp, asid, va, rfl⟩ : ∃ ptr mp asid va, c = .PageTable ptr mp asid va := by + cases c <;> simp [finKindA] at hk + exact ⟨_, _, _, _, rfl⟩ + have hfin := hf trivial + obtain ⟨hcore, ⟨Q, R, hw, Ac, Bf, stack, J, X⟩, hPA, hZT, hBH, hCT, hFx⟩ := hW + have hOSx : cslotAddrA sl ∈ OS := D.mem_slot hsl + have rd : ∀ (s' : S) (a' : A) (σ' : State Unit), capA st s' (cslotAddrA sl) = .PageTable ptr mp asid va → + DelRdA st Rv s' a' σ' G (cslotAddrA sl) := fun s' a' σ' e => + delRdA_other (fun _ _ h => by rw [e] at h; cases h) (fun _ _ h => by rw [e] at h; cases h) + (fun _ h => by rw [e] at h; cases h) + have hfinal : cdt.is_final_capability_with_cap st s (cslotAddrA sl) (.PageTable ptr mp asid va) = ok f := by + obtain ⟨b, hb, hbf⟩ := (finalHypA (st := st) Z.th OS G.chains s σ sl (.PageTable ptr mp asid va) D.toFinStA hsl hc trivial).1 + have e : b = f := Bool.eq_iff_iff.2 (hbf.trans ((isFinalCap'_eqA hac σ).symm.trans hfin.symm)) + rw [hb, e] + have mdb : MdbS st OS s := ⟨G.chains, D.lk, D.rng, D.kind, D.nl, D.cls⟩ + rw [fin_cap_eq_obj (by simp)] + by_cases hno : f = false ∨ mp = false + · -- nothing to do + have e : objects.finalise_cap_object st ar s a (.PageTable ptr mp asid va) f = + ok ((capability.Cap.Null, capability.Cap.Null), s, a) := by + unfold objects.finalise_cap_object + dsimp only + rcases hno with rfl | rfl + · simp only [Bool.false_and, Bool.false_eq_true, ↓reduceIte, bind_ok] + · cases f <;> simp only [Bool.true_and, Bool.false_and, Bool.false_eq_true, ↓reduceIte, bind_ok] + rw [e, WP.spec_ok] + refine ⟨rfl, trivial, trivial, σ, G, ⟨f, hfin, fin_pt_noopA hac hno⟩, D, hc, fun y _ _ => Or.inl rfl, ?_, + fun _ => ⟨⟨rd _ _ _ hc, rdThA_of_ne fun t h => by rw [hc] at h; cases h⟩, m, (by + have h := rdXA_of_J7A (st := st) (x := cslotAddrA sl) (σ := σ) (J.j7.preMapA) (fun _ _ => rfl) (by intros; exact VsZ.refl s) (fun _ _ _ _ _ _ _ _ _ _ vr R2 => pnPtA hA hPl vr J.ent.os J.ent.heap J.ko J.ent.ksw mdb hOSx R2 hc hfinal e) + exact h)⟩, fun _ _ _ h => by cases h⟩ + exact ⟨fun _ => ⟨hcore, ⟨Q, R, hw, Ac, Bf, stack, J, X⟩, hPA, hZT, hBH, hCT, hFx⟩, + fun _ => ⟨hcore, ⟨Q, R, hw, Ac, Bf, stack, J.mapJ7A fun _ _ => trivial, X⟩, hPA, hZT, hBH, hCT, hFx⟩⟩ + rw [not_or, Bool.not_eq_false, Bool.not_eq_false] at hno + obtain ⟨rfl, rfl⟩ := hno + have h16 : asid.val < 65536 := hrel.1 + have kb : L.ksb s = L.ksb s0 := hcore.1 + have hT' : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s) := by rw [kb]; exact hT + have J0 := J.j + obtain ⟨Jz, -⟩ := id J0 + have hfa := FootA.aligned Jz (fun c hc' off ho hal => bxi_nodes J.ent.bxi c hc' off ho hal) + J.scAl J.ap.vcAl + have leaf := unmap_pt_leafA K J0 hfa J.tabS1 J.tabExt J.vsLeafA.1 hT' (hIpa a) h16 va ptr + obtain ⟨out, eout, σ', hmem, jx, HS, HC, hform⟩ := spec_existsA leaf + obtain ⟨s1, a1⟩ := out + have ecall : objects.finalise_cap_object st ar s a (.PageTable ptr true asid va) true = + ok ((capability.Cap.Null, capability.Cap.Null), s1, a1) := by + rw [fin_pt_eqA, eout, bind_ok] + rfl + rw [ecall, WP.spec_ok] + -- the spec cap of the slot + have hmem' : ((Cap.nullCap, Cap.nullCap), σ') ∈ (fl4FinaliseCap (Z := Unit) ac true σ).1 := by + obtain ⟨asid', va', rfl, ha, hv⟩ := CapEqA.pt_mapped_iff.1 hac + have ha' : asid' = BitVec.ofNat 16 asid.val := ha.symm + subst ha' + rcases hv with hv | ⟨hv, -⟩ + · rw [← hv]; exact hmem + · rw [hv, fl4FinaliseCap_pt_unpackA] at hmem; exact hmem + have done : DStateA st Z.th OS s1 σ' G ∧ (∀ y ∈ OS, capA st s1 y = capA st s y) ∧ + DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st (J7R2A st ar hPl.ipa))) + s1 a1 σ' G ∧ + (∀ Rj' : Set U64 → JSlotA S, (∀ Pt Ub : Set U64, VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt → + (∀ q, SchedPageA st { Z with G := G, Q := Q, R := R, hw := hw } OS s q → q ∈ Pt) → + J7R2A st ar hPl.ipa Pt { Z with G := G, Q := Q, R := R, hw := hw } H OS s σ m → + Rj' Pt { Z with G := G, Q := Q, R := R, hw := hw } H OS s1 σ' m) → + J7LA st Rj' { Z with G := G, Q := Q, R := R, hw := hw } H OS s1 σ' m) := by + rcases hform with ⟨e, eσ⟩ | ⟨p, i, hi, hp, e⟩ + swap + · -- the table word is zeroed + dsimp only at e jx + subst e + have hp' : SchedPageA st { Z with G := G, Q := Q, R := R, hw := hw } OS s p := hp.schedA + have hcx : capA st s (cslotAddrA sl) = .PageTable ptr true asid va := hc + have R2 : ∀ Pt Ub : Set U64, VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt → + (∀ q, SchedPageA st { Z with G := G, Q := Q, R := R, hw := hw } OS s q → q ∈ Pt) → + J7R2A st ar hPl.ipa Pt { Z with G := G, Q := Q, R := R, hw := hw } H OS s σ m → + J7R2A st ar hPl.ipa Pt { Z with G := G, Q := Q, R := R, hw := hw } H OS (L.wr s (peAF p i) 0#u64) σ' m := by + rintro Pt Ub vr link ⟨Pp2, Tb2, Hr, Te, Kt, Lv2, Re, bits, R2, sep, KLv, ktK, hik⟩ + have R2' := S1aR2.activePools R2 + have found := R2'.core.walk (cslotAddrA sl) hOSx ptr asid va hcx + have look := vspace.unmap_page_table.activeLookup hA hPl a vr J.ent.os J.ent.heap R2'.pageTables + R2'.eptRoots R2'.core R2.place found + have eff := vspace.unmap_page_table.activeUnr hA hPl a R2'.core R2.place vr.al found + rw [eout, WP.spec_ok] at look eff + obtain ⟨lk, root, t1, t2, -, -, -⟩ := look + obtain ⟨words, headers, -, -, -⟩ := eff + dsimp only at lk root t1 t2 words headers + have hal : Aligned (peAF p i) := vr.al _ (vsW_pageA link hp' hi) + have ht := tab_of_w vr (writesIn_wr (v := 0#u64) hal) (fun b hb => hb ▸ hp.notAsA hT' hi) + have osk : OSKeep st OS s (L.wr s (peAF p i) 0#u64) := + OSKeep.of_in J.ent.os (writesIn_wr hal) fun x hx b hb h1 h2 hF => + vr.os b (hF ▸ vsW_pageA link hp' hi) ⟨x, hx, h1, h2⟩ + refine ⟨_, Tb2, _, Te, Kt, Lv2, Re, bits, S1aR2.zeroedKeepA R2' J.ent.heap D.kind + ⟨L.ksb_wr _ _ _, ht⟩ words headers (fun y hy => osk.capA hy) lk root t1 t2, sep.keep osk, KLv, ktK, + S1aHiK.keep hik ⟨EdgeKeepOn.ofVsZ R2 words, fun y hy R m a c => ⟨y, hy, m, a, by rwa [osk.capA hy] at c⟩⟩⟩ + have hdr : ∀ y ∈ OS, ∀ q mp ad, capA st s y = .HighVSpace q mp ad → + L.mem (L.wr s (peAF p i) 0#u64) (wa q 4088#u64) = L.mem s (wa q 4088#u64) := by + intro y hy q mq ad hq + obtain ⟨Pt, Ub, vr, -, -, -, -, -, -, link, -, -, -, -, ⟨Pp2, Tb2, Hr, Te, Kt, Lv2, Re, bits, R2, -⟩⟩ := J.j7 + have R2' := S1aR2.activePools R2 + have found := R2'.core.walk (cslotAddrA sl) hOSx ptr asid va hcx + have eff := vspace.unmap_page_table.activeUnr hA hPl a R2'.core R2.place vr.al found + rw [eout, WP.spec_ok] at eff + obtain ⟨-, headers, -, -, -⟩ := eff + exact headers q (by rw [R2'.highRoots]; exact ⟨y, hy, mq, ad, hq⟩) + obtain ⟨D', J', hPA', hZT', osk, J7f⟩ := delVs_zeroKeepA (Z := { Z with G := G, Q := Q, R := R, hw := hw }) J D hPA + hZT hp' hi (hp.notAsA hT' hi) (fun h => hbf Q R hw G Ac Bf stack m s a σ J _ h p hp' i hi rfl) + jx HS HC hdr R2 + have X' : Std6GA st Ag Z H OS Bf (L.wr s (peAF p i) 0#u64) σ' := + (Std6GA.vsZeroA J (X.ghostA G Q R hw) D.sub (fun _ _ _ link => vsW_pageA link hp' hi) HS + (fun p' dev pg h => HC.dataA p' dev pg h)).unghostA + exact ⟨D', fun y hy => osk.capA hy, + ⟨⟨(L.ksb_wr _ _ _).trans hcore.1, (L.cpu_wr _ _ _).trans hcore.2⟩, ⟨Q, R, hw, Ac, Bf, stack, J', X'⟩, hPA', + hZT', bufHold_sameA st hBH (fun y hy => osk.capA hy), + ctOff_sameA st hCT (L.ksb_wr _ _ _) (fun y hy => osk.capA hy), hFx.keepA st D.sub (fun y hy => osk.capA hy)⟩, J7f⟩ + · -- nothing written + obtain ⟨rfl, rfl⟩ := Prod.mk.inj e + subst eσ + exact ⟨D, fun _ _ => rfl, ⟨hcore, ⟨Q, R, hw, Ac, Bf, stack, J, X⟩, hPA, hZT, hBH, hCT, hFx⟩, J.j7.preMapA⟩ + obtain ⟨D', caps, hW', J7f⟩ := done + have hc' : capA st s1 (cslotAddrA sl) = .PageTable ptr true asid va := (caps _ hOSx).trans hc + exact ⟨rfl, trivial, trivial, σ', G, ⟨true, hfin, hmem'⟩, D', hc', fun y hy _ => Or.inl (caps y hy), + ⟨fun _ => hW', fun _ => ⟨hW'.1, by + obtain ⟨Q', R', hw', Ac', Bf', stack', J', X'⟩ := hW'.2.1 + exact ⟨Q', R', hw', Ac', Bf', stack', J'.mapJ7A fun _ _ => trivial, X'⟩, hW'.2.2⟩⟩, + fun _ => ⟨⟨rd _ _ _ hc', rdThA_of_ne fun t h => by rw [hc'] at h; cases h⟩, m, (by + have h := rdXA_of_J7A (st := st) (x := cslotAddrA sl) (σ := σ) J7f caps (fun _ _ _ _ _ _ _ _ _ _ vr _ R2 => by + have R2' := S1aR2.activePools R2 + have eff := vspace.unmap_page_table.activeUnr hA hPl a R2'.core R2.place vr.al + (R2'.core.walk (cslotAddrA sl) hOSx ptr asid va hc) + rw [eout, WP.spec_ok] at eff + exact eff.1) (fun _ _ _ _ _ _ _ _ _ _ vr R2 => pnPtA hA hPl vr J.ent.os J.ent.heap J.ko J.ent.ksw mdb hOSx R2 hc hfinal ecall) + exact h)⟩, fun _ _ _ h => by cases h⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/FinHookRootA.lean b/proofs/Fl4ProofsA64/P3/FinHookRootA.lean new file mode 100644 index 000000000..a529a4747 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/FinHookRootA.lean @@ -0,0 +1,145 @@ +/- +P3 (AArch64, delete hooks W2): **the VSpace-root finaliser hooks** `finHookPml4A`, `finHookHighA`, `finHookEptA` +(`FinHookKA .pml4 / .highVSpace / .ept`) at the loop record `DelJWQA s0 (DelXA6 … (J5A st) (J7LA st (J7R2A st ar ipa)))`. + +A non-final or unassigned root changes nothing on either side. A final assigned root's `delete_asid` (the pool entry +`ptr ||| tag`, tag `ROOT_S1` / `ROOT_S2` / `ROOT_HIGH`) is `P3/DelRootA`'s `delete_asid_recA`; the pool entry that names +the root is told from the others' by `EntKindA` and the apartness of the Stage-1 / Stage-2 / window objects +(`ExtVsRelA.pgd` / `ypg`). + +* `untagA_eq`, `rootOrA`, `rootSplitA`: the entry `ptr ||| tag` and which entries name `ptr`; +* **`finRoot_runA`**: `delete_asid` of `ptr ||| tag` at the loop record, with `Std6KeepA`. + +Named premises as `P3/FinHookPtA`'s plus [P] `DelArchA` (E7): `BufOffTabA` [J] (through `DelBufOffA`), `ArchKeepsA`, +`ArchPlat`, `P0.ArchTotal`. Decode-site hypotheses (state-independent): `OffAsE`, the VSpace words' alignment. New module. +-/ +import Fl4ProofsA64.P3.DelRootA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +theorem untagA_eq (w : U64) : untagA w = w &&& ~~~3#u64 := by + apply UScalar.eq_of_val_eq + unfold untagA + show (w.bv &&& 18446744073709551612#64).toNat = (w.bv &&& ~~~3#64).toNat + have e : (18446744073709551612#64 : BitVec 64) = ~~~3#64 := by decide + rw [e] + +theorem tagA_eq (w : U64) : tagA w = (w &&& 3#u64).val := by + unfold tagA + rw [UScalar.val_and, show (3#u64 : U64).val = 2 ^ 2 - 1 from rfl, Nat.and_two_pow_sub_one_eq_mod] + +theorem rebuildA (w : U64) : w = untagA w ||| (w &&& 3#u64) := by + rw [untagA_eq] + apply UScalar.eq_of_val_eq + change w.bv.toNat = ((w.bv &&& ~~~3#64) ||| (w.bv &&& 3#64)).toNat + congr 1 + rw [← BitVec.and_or_distrib_left, BitVec.not_or_self, BitVec.and_allOnes] + +/-- The pool entry of a root: `ptr ||| tag` untags to `ptr` and tags to `tag`. -/ +theorem rootOrA {ptr g : U64} (al : ptr &&& 3#u64 = 0#u64) (hg : g = 0#u64 ∨ g = 1#u64 ∨ g = 2#u64) : + untagA (ptr ||| g) = ptr ∧ (ptr ||| g) &&& 3#u64 = g := by + have low : ptr &&& ~~~3#u64 = ptr := maskedRoot al + have l1 := congrArg UScalar.bv low + have a1 := congrArg UScalar.bv al + change ptr.bv &&& ~~~3#64 = ptr.bv at l1 + change ptr.bv &&& 3#64 = 0#64 at a1 + have tags : g.bv &&& ~~~3#64 = 0#64 ∧ g.bv &&& 3#64 = g.bv := by + rcases hg with rfl | rfl | rfl <;> exact ⟨by decide, by decide⟩ + refine ⟨?_, ?_⟩ <;> apply UScalar.eq_of_val_eq + · rw [untagA_eq] + change ((ptr.bv ||| g.bv) &&& ~~~3#64).toNat = ptr.bv.toNat + rw [BitVec.and_or_distrib_right, l1, tags.1, BitVec.or_zero] + · change ((ptr.bv ||| g.bv) &&& 3#64).toNat = g.bv.toNat + rw [BitVec.and_or_distrib_right, a1, tags.2, BitVec.zero_or] + +section +variable {Pp Tb : Set U64} {Lv : U64 → Nat} {X Y T2 : Set U64} {Sh2 : U64 → Nat} {mem : U64 → U64} {ksb : U64} + {σ : Fl4SpecA64.State Unit} + +/-- **Which pool entries name a root object**: only `ptr ||| tag` (module docstring). -/ +theorem rootSplitA (VR : VsRelA Pp Tb Lv X Y mem ksb σ) (ER : ExtVsRelA Pp Tb X Y T2 Sh2 mem σ) {ptr g : U64} + (al : ptr &&& 3#u64 = 0#u64) (hg : g = 0#u64 ∨ g = 1#u64 ∨ g = 2#u64) + (hk : (g = 0#u64 ∧ ptr ∈ Tb) ∨ (g = 1#u64 ∧ ptr ∈ X) ∨ (g = 2#u64 ∧ ptr ∈ Y)) : + ∀ p ∈ Pp, ActivePoolA mem ksb p → ∀ lo : U64, lo.val < 512 → ∀ w, mem (peAF p lo) = w → w ≠ 0#u64 → + untagA w = ptr → w = ptr ||| g := by + have aTX : ptr ∈ Tb → ptr ∈ X → False := fun h1 h2 => + (ER.pgd ptr (Or.inr ⟨ptr, h2, Or.inl rfl⟩)).1 (Or.inr (Or.inl h1)) + have aTY : ptr ∈ Tb → ptr ∈ Y → False := fun h1 h2 => ER.ypg ptr h2 (Or.inr (Or.inl h1)) + have aXY : ptr ∈ X → ptr ∈ Y → False := fun h1 h2 => + (ER.pgd ptr (Or.inr ⟨ptr, h1, Or.inl rfl⟩)).2 h2 + intro p hp hact lo hlo w hw hw0 hu + have hK := VR.kind p hp hact lo hlo + rw [hw] at hK + have fin (t : U64) (ht : tagA w = t.val) (hgt : g = t) : w = ptr ||| g := by + have e3 : w &&& 3#u64 = g := by + rw [hgt]; apply UScalar.eq_of_val_eq; rw [← tagA_eq]; exact ht + conv_lhs => rw [rebuildA w] + rw [hu, e3] + rcases hK with rfl | ⟨h0, hT, -⟩ | ⟨h1, hX⟩ | ⟨h2, hY⟩ + · exact absurd rfl hw0 + · rw [hu] at hT + rcases hk with ⟨hg0, hp'⟩ | ⟨hg1, hp'⟩ | ⟨hg2, hp'⟩ + · exact fin 0#u64 h0 hg0 + · exact (aTX hT hp').elim + · exact (aTY hT hp').elim + · rw [hu] at hX + rcases hk with ⟨hg0, hp'⟩ | ⟨hg1, hp'⟩ | ⟨hg2, hp'⟩ + · exact (aTX hp' hX).elim + · exact fin 1#u64 h1 hg1 + · exact (aXY hX hp').elim + · rw [hu] at hY + rcases hk with ⟨hg0, hp'⟩ | ⟨hg1, hp'⟩ | ⟨hg2, hp'⟩ + · exact (aTY hp' hY).elim + · exact (aXY hp' hY).elim + · exact fin 2#u64 h2 hg2 + +end + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Pp Tb : Set U64} {Lv : U64 → Nat} {ia ie : Nat} + {N E Rp : Set U64} {κ : U64 → U64} {n : Nat} {Ac : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} + +/-- **`delete_asid` of a root's pool entry at the loop record** (module docstring). -/ +theorem finRoot_runA {ar : machine.Arch A} (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) (K : ArchKeepsA ar AR) + {Clock : A → Fl4SpecA64.MachineState → Prop} (Da : DelArchA st ar AR Clock) {s : S} {a : A} {σ : State Unit} {m : McState} + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7LA st (J7R2A st ar hPl.ipa)) s a σ m) + (D : DStateA st Z.th OS s σ Z.G) (hPA : ParentAdjA st Z.G.chains Z.G.par s) (hZT : ZombieTcbNoVcpuA st OS s) + {asid ptr g : U64} (h16 : asid.val < 2 ^ 16) (al : ptr &&& 3#u64 = 0#u64) + (hg : g = 0#u64 ∨ g = 1#u64 ∨ g = 2#u64) + (hk : (g = 0#u64 ∧ ptr ∈ Z.S1) ∨ (g = 1#u64 ∧ ptr ∈ Z.X) ∨ (g = 2#u64 ∧ ptr ∈ Z.Y)) + (hbf : ∀ p ∈ Z.Pp, ∀ i : U64, i.val < 512 → ¬ BufW Bf (peAF p i)) + (hT : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s)) : + vspace.delete_asid st ar s a asid (ptr ||| g) ⦃ r => ∃ σ' hw, + ((), σ') ∈ (deleteAsid (Z := Unit) (asidOfA asid) (specPtr ptr) σ).1 ∧ + DStateA st Z.th OS r.1 σ' Z.G ∧ (∀ y ∈ OS, capA st r.1 y = capA st s y) ∧ + JStage3TA st AR { Z with hw := hw } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7LA st (J7R2A st ar hPl.ipa)) r.1 r.2 σ' m ∧ + ParentAdjA st Z.G.chains Z.G.par r.1 ∧ ZombieTcbNoVcpuA st OS r.1 ∧ + L.ksb r.1 = L.ksb s ∧ L.cpu r.1 = L.cpu s ∧ + (∀ Pt Ub : Set U64, VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt → + (∀ q, SchedPageA st Z OS s q → q ∈ Pt) → VsZ st (L.ksb s) Pt s r.1) ∧ + (∀ Rj' : Set U64 → JSlotA S, (∀ Pt Ub : Set U64, VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt → + (∀ q, SchedPageA st Z OS s q → q ∈ Pt) → J7R2A st ar hPl.ipa Pt Z H OS s σ m → Rj' Pt { Z with hw := hw } H OS r.1 σ' m) → + J7LA st Rj' { Z with hw := hw } H OS r.1 σ' m) ∧ Std6KeepA st Z H OS Bf s σ r.1 σ' ⦄ := by + have hlov : (asid &&& 511#u64).val = asid.val % 512 := by + rw [and_maskA asid 9 _ rfl]; rfl + have hlo : (asid &&& 511#u64).val < 512 := by omega + obtain ⟨hu, -⟩ := rootOrA al hg + exact delete_asid_recA hA hPl K Da J D hPA hZT h16 hu + (fun p hp hact w hw hw0 hu' => rootSplitA J.j.1.vs J.j.1.ext al hg hk p hp hact _ hlo w hw hw0 hu') + hbf (fun p hp i hi => hT p i hi (Or.inr (Or.inl hp))) + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/FinHookRootKA.lean b/proofs/Fl4ProofsA64/P3/FinHookRootKA.lean new file mode 100644 index 000000000..2b8d2e568 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/FinHookRootKA.lean @@ -0,0 +1,336 @@ +/- +P3 (AArch64, delete hooks W2): **the VSpace-root finaliser hooks** `finHookPml4A`, `finHookHighA`, `finHookEptA` +(`FinHookKA .pml4 / .highVSpace / .ept`) at the loop record `DelJWQA s0 (DelXA6 … (J5A st) (J7LA st (J7R2A st ar ipa)))`. + +A non-final or unassigned root changes nothing on either side; a final assigned root's `delete_asid` is +`P3/FinHookRootA`'s `finRoot_runA`. Generated by `handoff/tools/gen_rootk.py`. + +Named premises as `P3/FinHookPtA`'s plus [P] `DelArchA` (E7): `BufOffTabA` [J] (through `DelBufOffA`), `ArchKeepsA`, +`ArchPlat`, `P0.ArchTotal`. Decode-site hypotheses (state-independent, at the entry's block base): `OffAsE`, the VSpace +words' alignment. New module; no existing statement changed. +-/ +import Fl4ProofsA64.P3.FinHookRootA +import Fl4ProofsA64.P3.FinHookPtA +import Fl4ProofsA64.P3.DelRdXA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +theorem and3_zeroA {v : U64} (h : 4 ∣ v.val) : v &&& 3#u64 = 0#u64 := by + apply UScalar.eq_of_val_eq + rw [UScalar.val_and, show (3#u64 : U64).val = 2 ^ 2 - 1 from rfl, Nat.and_two_pow_sub_one_eq_mod] + show v.val % 4 = 0 + omega + +section +variable {m : Fl4SpecA64.McState} +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Pp Tb : Set U64} {Lv : U64 → Nat} {ia ie : Nat} + {N E Rp : Set U64} {κ : U64 → U64} {n : Nat} + +/-- **The Pml4 finaliser hook** (module docstring). -/ +theorem finHookPml4A {ar : machine.Arch A} (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) (K : ArchKeepsA ar AR) + {Clock : A → Fl4SpecA64.MachineState → Prop} (Da : DelArchA st ar AR Clock) (s0 : S) (hT : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s0)) + (hbf : DelBufOffA st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n (J5A st) (J7LA st (J7R2A st ar hPl.ipa))) + (Rv : S → A → State Unit → MdbGhostA → U64 → Prop) : + FinHookKA st .pml4 ar Z.th OS fl4FinaliseCap + (DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st (J7R2A st ar hPl.ipa)))) + (DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st J7R2tA))) + (DelRdXA st ar Z H OS hPl.ipa Rv) (DelRzA st) := by + intro s a σ G sl c ac f D hW hsl hc hk hg hac hf + have hrel : CapRelA c ac := by + obtain ⟨c', h1, h2⟩ := D.rel.getCap_oneA hsl + rw [hg] at h1 + have e : c' = ac := by + have e0 : ({(ac, σ)} : Set (Cap × State Unit)) = {(c', σ)} := h1 + have h3 : (c', σ) ∈ ({(ac, σ)} : Set (Cap × State Unit)) := by rw [e0]; exact rfl + exact (Prod.mk.inj h3).1 + rw [capAtA_eq, hc, e] at h2 + exact h2 + obtain ⟨ptr, mp, asid, rfl⟩ : ∃ ptr mp asid, c = .Pml4 ptr mp asid := by + cases c <;> simp [finKindA] at hk + exact ⟨_, _, _, rfl⟩ + have hfin := hf trivial + have hwf := hrel.1 + simp only [CapWfA] at hwf + have h16 : asid.val < 2 ^ 16 := hwf + have eac : ac = absCapA (.Pml4 ptr mp asid) := hac.symm + obtain ⟨hcore, ⟨Q, R, hw, Ac, Bf, stack, J, X⟩, hPA, hZT, hBH, hCT, hFx⟩ := hW + have hOSx : cslotAddrA sl ∈ OS := D.mem_slot hsl + have rd : ∀ (s' : S) (a' : A) (σ' : State Unit), capA st s' (cslotAddrA sl) = .Pml4 ptr mp asid → + DelRdA st Rv s' a' σ' G (cslotAddrA sl) := fun s' a' σ' e => + delRdA_other (fun _ _ h => by rw [e] at h; cases h) (fun _ _ h => by rw [e] at h; cases h) + (fun _ h => by rw [e] at h; cases h) + have hfinal : cdt.is_final_capability_with_cap st s (cslotAddrA sl) (.Pml4 ptr mp asid) = ok f := by + obtain ⟨b, hb, hbf⟩ := (finalHypA (st := st) Z.th OS G.chains s σ sl (.Pml4 ptr mp asid) D.toFinStA hsl hc trivial).1 + have e : b = f := Bool.eq_iff_iff.2 (hbf.trans ((isFinalCap'_eqA hac σ).symm.trans hfin.symm)) + rw [hb, e] + have mdb : MdbS st OS s := ⟨G.chains, D.lk, D.rng, D.kind, D.nl, D.cls⟩ + rw [fin_cap_eq_obj (by simp)] + by_cases hno : f = false ∨ mp = false + · -- nothing to do + have e : objects.finalise_cap_object st ar s a (.Pml4 ptr mp asid) f = ok ((capability.Cap.Null, capability.Cap.Null), s, a) := by + rcases hno with rfl | rfl <;> first | rfl | (cases f <;> rfl) + have hnoop : ((Cap.nullCap, Cap.nullCap), σ) ∈ (fl4FinaliseCap (Z := Unit) ac f σ).1 := by + rw [eac] + simp only [absCapA] + rw [fl4FinaliseCap_plainA (by intro h; exact h)] + rcases hno with rfl | rfl + · cases mp <;> exact rfl + · cases f <;> exact rfl + rw [e, WP.spec_ok] + refine ⟨rfl, trivial, trivial, σ, G, ⟨f, hfin, hnoop⟩, D, hc, fun y _ _ => Or.inl rfl, ?_, + fun _ => ⟨⟨rd _ _ _ hc, rdThA_of_ne fun t h => by rw [hc] at h; cases h⟩, m, (by + have h := rdXA_of_J7A (st := st) (x := cslotAddrA sl) (σ := σ) (J.j7.preMapA) (fun _ _ => rfl) (by intros; exact VsZ.refl s) (fun _ _ _ _ _ _ _ _ _ _ vr R2 => pnRootA hA vr J.ent.os J.ent.heap J.ko J.ent.ksw mdb hOSx R2 (ptr := ptr) hc + (Or.inl ⟨_, _, rfl⟩) hfinal (by simp) (by simp) e) + exact h)⟩, fun _ _ _ h => by cases h⟩ + exact ⟨fun _ => ⟨hcore, ⟨Q, R, hw, Ac, Bf, stack, J, X⟩, hPA, hZT, hBH, hCT, hFx⟩, + fun _ => ⟨hcore, ⟨Q, R, hw, Ac, Bf, stack, J.mapJ7A fun _ _ => trivial, X⟩, hPA, hZT, hBH, hCT, hFx⟩⟩ + rw [not_or, Bool.not_eq_false, Bool.not_eq_false] at hno + obtain ⟨rfl, rfl⟩ := hno + have kb : L.ksb s = L.ksb s0 := hcore.1 + have hT' : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s) := by rw [kb]; exact hT + have hobj := J.j.capObj (y := cslotAddrA sl) (hsl.isSlot D.place) + rw [capAtA_eq, hc] at hobj + have hS : ptr ∈ Z.S1 := (hobj.2.2.2.1 ptr true asid rfl).1 + have hal : ptr &&& 3#u64 = 0#u64 := and3_zeroA (Nat.dvd_trans (by decide) (J.j.1.vs.al ptr hS)) + have hbf' := hbf Q R hw G Ac Bf stack m s a σ J + have run := finRoot_runA (Z := { Z with G := G, Q := Q, R := R, hw := hw }) hA hPl K Da J D hPA hZT h16 hal + (g := 0#u64) (Or.inl rfl) (Or.inl ⟨rfl, hS⟩) (fun p hp i hi h => hbf' _ h p (Or.inl hp) i hi rfl) hT' + obtain ⟨⟨s1, a1⟩, er, σ', hw', hmem, D', caps, J', hPA', hZT', hk1, hc1, vzr, J7f, K6⟩ := spec_existsA run + dsimp only at er hmem D' caps J' hPA' hZT' hk1 hc1 vzr J7f K6 + have X' : Std6GA st Ag Z H OS Bf s1 σ' := (K6 D.sub (X.ghostA G Q R hw)).unghostA + have ecall : objects.finalise_cap_object st ar s a (.Pml4 ptr true asid) true = + ok ((capability.Cap.Null, capability.Cap.Null), s1, a1) := by + have e0 : ptr ||| 0#u64 = ptr := UScalar.eq_of_val_eq (by rw [UScalar.val_or]; exact Nat.or_zero _) + rw [e0] at er + unfold objects.finalise_cap_object + simp only [↓reduceIte] + rw [er] + first + | (simp only [bind_ok]; rfl) + | (split <;> first | (simp only [bind_ok]; rfl) | exact absurd rfl (by assumption)) + rw [ecall, WP.spec_ok] + have hmem' : ((Cap.nullCap, Cap.nullCap), σ') ∈ (fl4FinaliseCap (Z := Unit) ac true σ).1 := by + rw [eac] + simp only [absCapA, mapDataA, ↓reduceIte] + rw [fl4FinaliseCap_plainA (by intro h; exact h)] + exact mem_bindA.2 ⟨((), σ'), hmem, rfl⟩ + have hc' : capA st s1 (cslotAddrA sl) = .Pml4 ptr true asid := (caps _ hOSx).trans hc + have hW' : DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st (J7R2A st ar hPl.ipa))) + s1 a1 σ' G := + ⟨⟨hk1.trans hcore.1, hc1.trans hcore.2⟩, ⟨Q, R, hw', Ac, Bf, stack, J', X'⟩, hPA', hZT', bufHold_sameA st hBH caps, ctOff_sameA st hCT hk1 caps, hFx.keepA st D.sub caps⟩ + exact ⟨rfl, trivial, trivial, σ', G, ⟨true, hfin, hmem'⟩, D', hc', fun y hy _ => Or.inl (caps y hy), + ⟨fun _ => hW', fun _ => ⟨hW'.1, ⟨Q, R, hw', Ac, Bf, stack, J'.mapJ7A fun _ _ => trivial, X'⟩, hPA', hZT', bufHold_sameA st hBH caps, ctOff_sameA st hCT hk1 caps, hFx.keepA st D.sub caps⟩⟩, + fun _ => ⟨⟨rd _ _ _ hc', rdThA_of_ne fun t h => by rw [hc'] at h; cases h⟩, m, (by + have h := rdXA_of_J7A (st := st) (x := cslotAddrA sl) (σ := σ) J7f caps (fun _ _ _ _ _ _ _ _ _ _ vr link _ => vzr _ _ vr link) (fun _ _ _ _ _ _ _ _ _ _ vr R2 => pnRootA hA vr J.ent.os J.ent.heap J.ko J.ent.ksw mdb hOSx R2 (ptr := ptr) hc + (Or.inl ⟨_, _, rfl⟩) hfinal (by simp) (by simp) ecall) + exact h)⟩, fun _ _ _ h => by cases h⟩ + +/-- **The high-window finaliser hook** (module docstring). -/ +theorem finHookHighA {ar : machine.Arch A} (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) (K : ArchKeepsA ar AR) + {Clock : A → Fl4SpecA64.MachineState → Prop} (Da : DelArchA st ar AR Clock) (s0 : S) (hT : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s0)) + (hbf : DelBufOffA st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n (J5A st) (J7LA st (J7R2A st ar hPl.ipa))) + (Rv : S → A → State Unit → MdbGhostA → U64 → Prop) : + FinHookKA st .highVSpace ar Z.th OS fl4FinaliseCap + (DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st (J7R2A st ar hPl.ipa)))) + (DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st J7R2tA))) + (DelRdXA st ar Z H OS hPl.ipa Rv) (DelRzA st) := by + intro s a σ G sl c ac f D hW hsl hc hk hg hac hf + have hrel : CapRelA c ac := by + obtain ⟨c', h1, h2⟩ := D.rel.getCap_oneA hsl + rw [hg] at h1 + have e : c' = ac := by + have e0 : ({(ac, σ)} : Set (Cap × State Unit)) = {(c', σ)} := h1 + have h3 : (c', σ) ∈ ({(ac, σ)} : Set (Cap × State Unit)) := by rw [e0]; exact rfl + exact (Prod.mk.inj h3).1 + rw [capAtA_eq, hc, e] at h2 + exact h2 + obtain ⟨ptr, mp, asid, rfl⟩ : ∃ ptr mp asid, c = .HighVSpace ptr mp asid := by + cases c <;> simp [finKindA] at hk + exact ⟨_, _, _, rfl⟩ + have hfin := hf trivial + have hwf := hrel.1 + simp only [CapWfA] at hwf + have h16 : asid.val < 2 ^ 16 := hwf + have eac : ac = absCapA (.HighVSpace ptr mp asid) := hac.symm + obtain ⟨hcore, ⟨Q, R, hw, Ac, Bf, stack, J, X⟩, hPA, hZT, hBH, hCT, hFx⟩ := hW + have hOSx : cslotAddrA sl ∈ OS := D.mem_slot hsl + have rd : ∀ (s' : S) (a' : A) (σ' : State Unit), capA st s' (cslotAddrA sl) = .HighVSpace ptr mp asid → + DelRdA st Rv s' a' σ' G (cslotAddrA sl) := fun s' a' σ' e => + delRdA_other (fun _ _ h => by rw [e] at h; cases h) (fun _ _ h => by rw [e] at h; cases h) + (fun _ h => by rw [e] at h; cases h) + have hfinal : cdt.is_final_capability_with_cap st s (cslotAddrA sl) (.HighVSpace ptr mp asid) = ok f := by + obtain ⟨b, hb, hbf⟩ := (finalHypA (st := st) Z.th OS G.chains s σ sl (.HighVSpace ptr mp asid) D.toFinStA hsl hc trivial).1 + have e : b = f := Bool.eq_iff_iff.2 (hbf.trans ((isFinalCap'_eqA hac σ).symm.trans hfin.symm)) + rw [hb, e] + have mdb : MdbS st OS s := ⟨G.chains, D.lk, D.rng, D.kind, D.nl, D.cls⟩ + rw [fin_cap_eq_obj (by simp)] + by_cases hno : f = false ∨ mp = false + · -- nothing to do + have e : objects.finalise_cap_object st ar s a (.HighVSpace ptr mp asid) f = ok ((capability.Cap.Null, capability.Cap.Null), s, a) := by + rcases hno with rfl | rfl <;> first | rfl | (cases f <;> rfl) + have hnoop : ((Cap.nullCap, Cap.nullCap), σ) ∈ (fl4FinaliseCap (Z := Unit) ac f σ).1 := by + rw [eac] + simp only [absCapA] + rw [fl4FinaliseCap_root_otherA (c := .highVSpaceCap (specPtr ptr) (asidDataA mp asid)) rfl (final := f) + (by rcases hno with rfl | rfl <;> simp [ExtArchCap.mapData, asidDataA])] + exact rfl + rw [e, WP.spec_ok] + refine ⟨rfl, trivial, trivial, σ, G, ⟨f, hfin, hnoop⟩, D, hc, fun y _ _ => Or.inl rfl, ?_, + fun _ => ⟨⟨rd _ _ _ hc, rdThA_of_ne fun t h => by rw [hc] at h; cases h⟩, m, (by + have h := rdXA_of_J7A (st := st) (x := cslotAddrA sl) (σ := σ) (J.j7.preMapA) (fun _ _ => rfl) (by intros; exact VsZ.refl s) (fun _ _ _ _ _ _ _ _ _ _ vr R2 => pnRootA hA vr J.ent.os J.ent.heap J.ko J.ent.ksw mdb hOSx R2 (ptr := ptr) hc + (Or.inr (Or.inl ⟨_, _, rfl⟩)) hfinal (by simp) (by simp) e) + exact h)⟩, fun _ _ _ h => by cases h⟩ + exact ⟨fun _ => ⟨hcore, ⟨Q, R, hw, Ac, Bf, stack, J, X⟩, hPA, hZT, hBH, hCT, hFx⟩, + fun _ => ⟨hcore, ⟨Q, R, hw, Ac, Bf, stack, J.mapJ7A fun _ _ => trivial, X⟩, hPA, hZT, hBH, hCT, hFx⟩⟩ + rw [not_or, Bool.not_eq_false, Bool.not_eq_false] at hno + obtain ⟨rfl, rfl⟩ := hno + have kb : L.ksb s = L.ksb s0 := hcore.1 + have hT' : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s) := by rw [kb]; exact hT + have hobj := J.j.capObj (y := cslotAddrA sl) (hsl.isSlot D.place) + rw [capAtA_eq, hc] at hobj + have hS : ptr ∈ Z.Y := hobj.2.2.1 ptr true asid rfl + have hal : ptr &&& 3#u64 = 0#u64 := and3_zeroA (Nat.dvd_trans (by decide) (J.j.1.vs.yal ptr hS)) + have hbf' := hbf Q R hw G Ac Bf stack m s a σ J + have run := finRoot_runA (Z := { Z with G := G, Q := Q, R := R, hw := hw }) hA hPl K Da J D hPA hZT h16 hal + (g := 2#u64) (Or.inr (Or.inr rfl)) (Or.inr (Or.inr ⟨rfl, hS⟩)) (fun p hp i hi h => hbf' _ h p (Or.inl hp) i hi rfl) hT' + obtain ⟨⟨s1, a1⟩, er, σ', hw', hmem, D', caps, J', hPA', hZT', hk1, hc1, vzr, J7f, K6⟩ := spec_existsA run + dsimp only at er hmem D' caps J' hPA' hZT' hk1 hc1 vzr J7f K6 + have X' : Std6GA st Ag Z H OS Bf s1 σ' := (K6 D.sub (X.ghostA G Q R hw)).unghostA + have ecall : objects.finalise_cap_object st ar s a (.HighVSpace ptr true asid) true = + ok ((capability.Cap.Null, capability.Cap.Null), s1, a1) := by + have eH : vspace.ROOT_HIGH = 2#u64 := by unfold vspace.ROOT_HIGH; rfl + unfold objects.finalise_cap_object + simp only [↓reduceIte, lift, bind_ok, eH] + rw [er] + first + | (simp only [bind_ok]; rfl) + | (split <;> first | (simp only [bind_ok]; rfl) | exact absurd rfl (by assumption)) + rw [ecall, WP.spec_ok] + have hmem' : ((Cap.nullCap, Cap.nullCap), σ') ∈ (fl4FinaliseCap (Z := Unit) ac true σ).1 := by + rw [eac] + simp only [absCapA, asidDataA, ↓reduceIte] + rw [fl4FinaliseCap_root_finalA (c := .highVSpaceCap (specPtr ptr) (some (asidOfA asid))) rfl] + exact mem_bindA.2 ⟨((), σ'), hmem, rfl⟩ + have hc' : capA st s1 (cslotAddrA sl) = .HighVSpace ptr true asid := (caps _ hOSx).trans hc + have hW' : DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st (J7R2A st ar hPl.ipa))) + s1 a1 σ' G := + ⟨⟨hk1.trans hcore.1, hc1.trans hcore.2⟩, ⟨Q, R, hw', Ac, Bf, stack, J', X'⟩, hPA', hZT', bufHold_sameA st hBH caps, ctOff_sameA st hCT hk1 caps, hFx.keepA st D.sub caps⟩ + exact ⟨rfl, trivial, trivial, σ', G, ⟨true, hfin, hmem'⟩, D', hc', fun y hy _ => Or.inl (caps y hy), + ⟨fun _ => hW', fun _ => ⟨hW'.1, ⟨Q, R, hw', Ac, Bf, stack, J'.mapJ7A fun _ _ => trivial, X'⟩, hPA', hZT', bufHold_sameA st hBH caps, ctOff_sameA st hCT hk1 caps, hFx.keepA st D.sub caps⟩⟩, + fun _ => ⟨⟨rd _ _ _ hc', rdThA_of_ne fun t h => by rw [hc'] at h; cases h⟩, m, (by + have h := rdXA_of_J7A (st := st) (x := cslotAddrA sl) (σ := σ) J7f caps (fun _ _ _ _ _ _ _ _ _ _ vr link _ => vzr _ _ vr link) (fun _ _ _ _ _ _ _ _ _ _ vr R2 => pnRootA hA vr J.ent.os J.ent.heap J.ko J.ent.ksw mdb hOSx R2 (ptr := ptr) hc + (Or.inr (Or.inl ⟨_, _, rfl⟩)) hfinal (by simp) (by simp) ecall) + exact h)⟩, fun _ _ _ h => by cases h⟩ + +/-- **The Stage-2 root finaliser hook** (module docstring). -/ +theorem finHookEptA {ar : machine.Arch A} (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) (K : ArchKeepsA ar AR) + {Clock : A → Fl4SpecA64.MachineState → Prop} (Da : DelArchA st ar AR Clock) (s0 : S) (hT : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s0)) + (hbf : DelBufOffA st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n (J5A st) (J7LA st (J7R2A st ar hPl.ipa))) + (Rv : S → A → State Unit → MdbGhostA → U64 → Prop) : + FinHookKA st .ept ar Z.th OS fl4FinaliseCap + (DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st (J7R2A st ar hPl.ipa)))) + (DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st J7R2tA))) + (DelRdXA st ar Z H OS hPl.ipa Rv) (DelRzA st) := by + intro s a σ G sl c ac f D hW hsl hc hk hg hac hf + have hrel : CapRelA c ac := by + obtain ⟨c', h1, h2⟩ := D.rel.getCap_oneA hsl + rw [hg] at h1 + have e : c' = ac := by + have e0 : ({(ac, σ)} : Set (Cap × State Unit)) = {(c', σ)} := h1 + have h3 : (c', σ) ∈ ({(ac, σ)} : Set (Cap × State Unit)) := by rw [e0]; exact rfl + exact (Prod.mk.inj h3).1 + rw [capAtA_eq, hc, e] at h2 + exact h2 + obtain ⟨ptr, l, mp, asid, v, rfl⟩ : ∃ ptr l mp asid v, c = .Ept ptr l mp asid v := by + cases c <;> simp [finKindA] at hk + exact ⟨_, _, _, _, _, rfl⟩ + have hfin := hf trivial + have hwf := hrel.1 + simp only [CapWfA] at hwf + have hl0 : l = 0#u64 := UScalar.eq_of_val_eq hwf.1 + subst hl0 + have h16 : asid.val < 2 ^ 16 := hwf.2 + have eac : ac = absCapA (.Ept ptr 0#u64 mp asid v) := hac.symm + obtain ⟨hcore, ⟨Q, R, hw, Ac, Bf, stack, J, X⟩, hPA, hZT, hBH, hCT, hFx⟩ := hW + have hOSx : cslotAddrA sl ∈ OS := D.mem_slot hsl + have rd : ∀ (s' : S) (a' : A) (σ' : State Unit), capA st s' (cslotAddrA sl) = .Ept ptr 0#u64 mp asid v → + DelRdA st Rv s' a' σ' G (cslotAddrA sl) := fun s' a' σ' e => + delRdA_other (fun _ _ h => by rw [e] at h; cases h) (fun _ _ h => by rw [e] at h; cases h) + (fun _ h => by rw [e] at h; cases h) + have hfinal : cdt.is_final_capability_with_cap st s (cslotAddrA sl) (.Ept ptr 0#u64 mp asid v) = ok f := by + obtain ⟨b, hb, hbf⟩ := (finalHypA (st := st) Z.th OS G.chains s σ sl (.Ept ptr 0#u64 mp asid v) D.toFinStA hsl hc trivial).1 + have e : b = f := Bool.eq_iff_iff.2 (hbf.trans ((isFinalCap'_eqA hac σ).symm.trans hfin.symm)) + rw [hb, e] + have mdb : MdbS st OS s := ⟨G.chains, D.lk, D.rng, D.kind, D.nl, D.cls⟩ + rw [fin_cap_eq_obj (by simp)] + by_cases hno : f = false ∨ mp = false + · -- nothing to do + have e : objects.finalise_cap_object st ar s a (.Ept ptr 0#u64 mp asid v) f = ok ((capability.Cap.Null, capability.Cap.Null), s, a) := by + rcases hno with rfl | rfl <;> first | rfl | (cases f <;> simp [objects.finalise_cap_object, ept.finalise] <;> split <;> simp) + have hnoop : ((Cap.nullCap, Cap.nullCap), σ) ∈ (fl4FinaliseCap (Z := Unit) ac f σ).1 := by + rw [eac] + simp only [absCapA] + rw [fl4FinaliseCap_root_otherA (c := .stage2VSpaceCap (specPtr ptr) (asidDataA mp asid)) rfl (final := f) + (by rcases hno with rfl | rfl <;> simp [ExtArchCap.mapData, asidDataA])] + exact rfl + rw [e, WP.spec_ok] + refine ⟨rfl, trivial, trivial, σ, G, ⟨f, hfin, hnoop⟩, D, hc, fun y _ _ => Or.inl rfl, ?_, + fun _ => ⟨⟨rd _ _ _ hc, rdThA_of_ne fun t h => by rw [hc] at h; cases h⟩, m, (by + have h := rdXA_of_J7A (st := st) (x := cslotAddrA sl) (σ := σ) (J.j7.preMapA) (fun _ _ => rfl) (by intros; exact VsZ.refl s) (fun _ _ _ _ _ _ _ _ _ _ vr R2 => pnRootA hA vr J.ent.os J.ent.heap J.ko J.ent.ksw mdb hOSx R2 (ptr := ptr) hc + (Or.inr (Or.inr ⟨_, _, _, rfl⟩)) hfinal (by simp) (by simp) e) + exact h)⟩, fun _ _ _ h => by cases h⟩ + exact ⟨fun _ => ⟨hcore, ⟨Q, R, hw, Ac, Bf, stack, J, X⟩, hPA, hZT, hBH, hCT, hFx⟩, + fun _ => ⟨hcore, ⟨Q, R, hw, Ac, Bf, stack, J.mapJ7A fun _ _ => trivial, X⟩, hPA, hZT, hBH, hCT, hFx⟩⟩ + rw [not_or, Bool.not_eq_false, Bool.not_eq_false] at hno + obtain ⟨rfl, rfl⟩ := hno + have kb : L.ksb s = L.ksb s0 := hcore.1 + have hT' : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s) := by rw [kb]; exact hT + have hobj := J.j.capObj (y := cslotAddrA sl) (hsl.isSlot D.place) + rw [capAtA_eq, hc] at hobj + have hS : ptr ∈ Z.X := hobj.2.1 ptr 0#u64 true asid v rfl + have hal : ptr &&& 3#u64 = 0#u64 := and3_zeroA (Nat.dvd_trans (by decide) (J.j.1.ext.xal ptr hS)) + have hbf' := hbf Q R hw G Ac Bf stack m s a σ J + have run := finRoot_runA (Z := { Z with G := G, Q := Q, R := R, hw := hw }) hA hPl K Da J D hPA hZT h16 hal + (g := 1#u64) (Or.inr (Or.inl rfl)) (Or.inr (Or.inl ⟨rfl, hS⟩)) (fun p hp i hi h => hbf' _ h p (Or.inl hp) i hi rfl) hT' + obtain ⟨⟨s1, a1⟩, er, σ', hw', hmem, D', caps, J', hPA', hZT', hk1, hc1, vzr, J7f, K6⟩ := spec_existsA run + dsimp only at er hmem D' caps J' hPA' hZT' hk1 hc1 vzr J7f K6 + have X' : Std6GA st Ag Z H OS Bf s1 σ' := (K6 D.sub (X.ghostA G Q R hw)).unghostA + have ecall : objects.finalise_cap_object st ar s a (.Ept ptr 0#u64 true asid v) true = + ok ((capability.Cap.Null, capability.Cap.Null), s1, a1) := by + have eS : vspace.ROOT_S2 = 1#u64 := by unfold vspace.ROOT_S2; rfl + unfold objects.finalise_cap_object ept.finalise + simp only [↓reduceIte, lift, bind_ok, eS] + rw [er] + first + | (simp only [bind_ok]; rfl) + | (split <;> first | (simp only [bind_ok]; rfl) | exact absurd rfl (by assumption)) + rw [ecall, WP.spec_ok] + have hmem' : ((Cap.nullCap, Cap.nullCap), σ') ∈ (fl4FinaliseCap (Z := Unit) ac true σ).1 := by + rw [eac] + simp only [absCapA, asidDataA, ↓reduceIte] + rw [fl4FinaliseCap_root_finalA (c := .stage2VSpaceCap (specPtr ptr) (some (asidOfA asid))) rfl] + exact mem_bindA.2 ⟨((), σ'), hmem, rfl⟩ + have hc' : capA st s1 (cslotAddrA sl) = .Ept ptr 0#u64 true asid v := (caps _ hOSx).trans hc + have hW' : DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st (J7R2A st ar hPl.ipa))) + s1 a1 σ' G := + ⟨⟨hk1.trans hcore.1, hc1.trans hcore.2⟩, ⟨Q, R, hw', Ac, Bf, stack, J', X'⟩, hPA', hZT', bufHold_sameA st hBH caps, ctOff_sameA st hCT hk1 caps, hFx.keepA st D.sub caps⟩ + exact ⟨rfl, trivial, trivial, σ', G, ⟨true, hfin, hmem'⟩, D', hc', fun y hy _ => Or.inl (caps y hy), + ⟨fun _ => hW', fun _ => ⟨hW'.1, ⟨Q, R, hw', Ac, Bf, stack, J'.mapJ7A fun _ _ => trivial, X'⟩, hPA', hZT', bufHold_sameA st hBH caps, ctOff_sameA st hCT hk1 caps, hFx.keepA st D.sub caps⟩⟩, + fun _ => ⟨⟨rd _ _ _ hc', rdThA_of_ne fun t h => by rw [hc'] at h; cases h⟩, m, (by + have h := rdXA_of_J7A (st := st) (x := cslotAddrA sl) (σ := σ) J7f caps (fun _ _ _ _ _ _ _ _ _ _ vr link _ => vzr _ _ vr link) (fun _ _ _ _ _ _ _ _ _ _ vr R2 => pnRootA hA vr J.ent.os J.ent.heap J.ko J.ent.ksw mdb hOSx R2 (ptr := ptr) hc + (Or.inr (Or.inr ⟨_, _, _, rfl⟩)) hfinal (by simp) (by simp) ecall) + exact h)⟩, fun _ _ _ h => by cases h⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/FinKeepA.lean b/proofs/Fl4ProofsA64/P3/FinKeepA.lean new file mode 100644 index 000000000..c0df9c202 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/FinKeepA.lean @@ -0,0 +1,121 @@ +/- +P3 (aarch64, A64-P3 S3 gen 28, ruling 23:55Z item 4, first part): **the generic keepers of the carried invariants** +— `FinInvA` (gen 27, `P3/FinDefsA`) across steps that rewrite caps in place without changing what finality reads, +and the standalone **`PtCapInA`** (the mapped page-table caps name relation tables; ruling Q2, `hp` of +`pt_unmap_decJA` / `pt_unmapS1_decJA`). The x86 counterparts are `P3/FinKeep`'s `FinM.of_same` / `KTyped.of_types` +and `EptCore.capTbl`. + +* **`CapFinEqA c c'`**: `c` and `c'` agree on everything `FinMA` reads — the abstract cap's generic references + (`genObjRefs ∘ absCapA`), the object key `okeyA`, `isZbA`, `isFrameA`. **`CapFinEqA.frameA`** / + **`CapFinEqA.ptA`**: an unmap's cap write (`Frame … false false 0 0`, `PageTable … false 0 0`) is `CapFinEqA` to + the cap it replaces (FL4's in-place `set_cap` keeps the object; ruling Q3's "in-place `set_cap` updating mapped + ASID / vaddr keeps `okeyA`"). +* **`FinMA.of_capEqA`**: `FinMA` over the same chains is kept when every chain member's cap is `CapFinEqA` to its + old one. **`KTypedA.of_typesA`**: `KTypedA` is kept by a spec step whose CNodes and TCBs were already CNodes (same + radix) and TCBs. **`FinInvA.of_keepA`**: the two together. +* **`PtCapInA Z mem`**: every relation slot holding a mapped `PageTable ptr true asid va` has `ptr ∈ Z.S1` at depth + ≥ 1 or `ptr ∈ Z.T2`. **`PtCapInA.hpA`** gives `hp` at the invoked slot; **`PtCapInA.of_capsA`** keeps it across a + step whose slot caps are unchanged or are no mapped page-table cap (the unmap tops' writes). + +Not a [J] premise and not in `JEntA` / `JAtZA` (ruling 23:55Z: bundling waits for the S3 + IPC + MCS join). No named +premise. New module; no existing statement changed. +-/ +import Fl4ProofsA64.P3.FinDefsA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Classical + +noncomputable section + +/-! ## `FinInvA` -/ + +/-- **Two caps agree on what finality reads** (module docstring). -/ +def CapFinEqA (c c' : capability.Cap) : Prop := + Fl4SpecA64.genObjRefs (absCapA c) = Fl4SpecA64.genObjRefs (absCapA c') ∧ okeyA c = okeyA c' ∧ + isZbA c = isZbA c' ∧ isFrameA c = isFrameA c' + +theorem CapFinEqA.refl (c : capability.Cap) : CapFinEqA c c := ⟨rfl, rfl, rfl, rfl⟩ + +/-- An unmapped frame cap against the frame cap it replaces. -/ +theorem CapFinEqA.frameA (p rt asid va : U64) (sz : capability.FrameSize) (d m e : Bool) : + CapFinEqA (.Frame p sz rt d false false 0#u64 0#u64) (.Frame p sz rt d m e asid va) := + ⟨Set.ext fun r => by rw [genObjRefs_absA, genObjRefs_absA]; exact Iff.rfl, rfl, rfl, rfl⟩ + +/-- An unmapped page-table cap against the page-table cap it replaces. -/ +theorem CapFinEqA.ptA (p asid va : U64) (m : Bool) : + CapFinEqA (.PageTable p false 0#u64 0#u64) (.PageTable p m asid va) := + ⟨Set.ext fun r => by rw [genObjRefs_absA, genObjRefs_absA]; exact Iff.rfl, rfl, rfl, rfl⟩ + +/-- **`FinMA` across in-place cap rewrites that finality does not see** (module docstring). -/ +theorem FinMA.of_capEqA {mem mem' : U64 → U64} {G : List (List U64)} (h : FinMA mem G) + (he : ∀ y ∈ G.flatten, CapFinEqA (capAtA mem' y) (capAtA mem y)) : FinMA mem' G := by + have hO : ∀ x ∈ G.flatten, ∀ y ∈ G.flatten, OvlA (absCapA (capAtA mem' x)) (absCapA (capAtA mem' y)) ↔ + OvlA (absCapA (capAtA mem x)) (absCapA (capAtA mem y)) := fun x hx y hy => by + unfold OvlA; rw [(he x hx).1, (he y hy).1] + have hm : ∀ ch ∈ G, ∀ x ∈ ch, x ∈ G.flatten := fun ch hch x hx => List.mem_flatten.2 ⟨ch, hch, hx⟩ + refine ⟨fun ch1 h1 ch2 h2 x hx y hy ho => h.conn ch1 h1 ch2 h2 x hx y hy + ((hO x (hm _ h1 _ hx) y (hm _ h2 _ hy)).1 ho), ?_, ?_, ?_⟩ + · intro ch hch i j k y z y' hy hz hy' hij hjk hk hn hnf + have ey := (he y (hm _ hch _ (List.mem_of_getElem? hy))).2.1 + have ez := (he z (hm _ hch _ (List.mem_of_getElem? hz))).2.1 + have ey' := (he y' (hm _ hch _ (List.mem_of_getElem? hy'))).2.1 + rw [ey, ey'] at hk + rw [ey] at hn hnf + rw [ez, ey] + exact h.chunk ch hch i j k y z y' hy hz hy' hij hjk hk hn hnf + · intro x hx y hy hne ho hzx hzy hfr + rw [(he x hx).2.1, (he y hy).2.1] + refine h.key x hx y hy hne ((hO x hx y hy).1 ho) ?_ ?_ ?_ + · rw [← (he x hx).2.2.1]; exact hzx + · rw [← (he y hy).2.2.1]; exact hzy + · rw [← (he x hx).2.2.2, ← (he y hy).2.2.2]; exact hfr + · intro x hx y hy hne hz ho + exact h.zfin x hx y hy hne (by rw [← (he x hx).2.2.1]; exact hz) ((hO x hx y hy).1 ho) + +/-- **`KTypedA` across a spec step that keeps the CNodes' radixes and the TCBs' kind** (module docstring). -/ +theorem KTypedA.of_typesA {th : TypedHeapA} {σ σ' : Fl4SpecA64.State Unit} (h : KTypedA th σ) + (hc : ∀ o r cs, σ'.kheap o = some (.cnode r cs) → ∃ cs0, σ.kheap o = some (.cnode r cs0)) + (ht : ∀ o t, σ'.kheap o = some (.tcb t) → ∃ t0, σ.kheap o = some (.tcb t0)) : KTypedA th σ' := + ⟨fun o r cs e => by obtain ⟨cs0, e0⟩ := hc o r cs e; exact h.cn o r cs0 e0, + fun o t e => by obtain ⟨t0, e0⟩ := ht o t e; exact h.tc o t0 e0⟩ + +/-- **`FinInvA` kept** (module docstring). -/ +theorem FinInvA.of_keepA {th : TypedHeapA} {mem mem' : U64 → U64} {σ σ' : Fl4SpecA64.State Unit} {G : MdbGhostA} + (h : FinInvA th mem σ G) + (hc : ∀ o r cs, σ'.kheap o = some (.cnode r cs) → ∃ cs0, σ.kheap o = some (.cnode r cs0)) + (ht : ∀ o t, σ'.kheap o = some (.tcb t) → ∃ t0, σ.kheap o = some (.tcb t0)) + (he : ∀ y ∈ G.chains.flatten, CapFinEqA (capAtA mem' y) (capAtA mem y)) : FinInvA th mem' σ' G := + ⟨h.kt.of_typesA hc ht, h.fm.of_capEqA he⟩ + +/-! ## `PtCapInA` -/ + +/-- **The mapped page-table caps name relation tables** (module docstring; x86 `EptCore.capTbl`). -/ +def PtCapInA (Z : XSetsA) (mem : U64 → U64) : Prop := + ∀ y, IsSlotA Z.th y → ∀ ptr asid va : U64, capAtA mem y = .PageTable ptr true asid va → + (ptr ∈ Z.S1 ∧ 1 ≤ Z.Lv ptr) ∨ ptr ∈ Z.T2 + +/-- `hp` at the invoked slot. -/ +theorem PtCapInA.hpA {Z : XSetsA} {mem : U64 → U64} (h : PtCapInA Z mem) {cte : Fl4SpecA64.CslotPtr} {kb : U64} + (hP : PlaceA Z.th kb) (hab : AbsSlotA Z.th cte) {ptr asid va : U64} + (hcap : capAtA mem (cslotAddrA cte) = .PageTable ptr true asid va) : + (ptr ∈ Z.S1 ∧ 1 ≤ Z.Lv ptr) ∨ ptr ∈ Z.T2 := + h _ (hab.isSlot hP) ptr asid va hcap + +/-- **`PtCapInA` across a step whose slot caps are unchanged or no mapped page-table cap** (module docstring). -/ +theorem PtCapInA.of_capsA {Z Z' : XSetsA} {mem mem' : U64 → U64} (h : PtCapInA Z mem) (hth : Z'.th = Z.th) + (hS : ∀ p, p ∈ Z.S1 ∧ 1 ≤ Z.Lv p → p ∈ Z'.S1 ∧ 1 ≤ Z'.Lv p) (hT : ∀ p ∈ Z.T2, p ∈ Z'.T2) + (hc : ∀ y, IsSlotA Z.th y → capAtA mem' y = capAtA mem y ∨ ∀ ptr asid va, capAtA mem' y ≠ .PageTable ptr true asid va) : + PtCapInA Z' mem' := by + intro y hy ptr asid va e + rw [hth] at hy + rcases hc y hy with h1 | h1 + · rw [h1] at e + rcases h y hy ptr asid va e with h2 | h2 + · exact Or.inl (hS _ h2) + · exact Or.inr (hT _ h2) + · exact (h1 ptr asid va e).elim + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/FinVsA.lean b/proofs/Fl4ProofsA64/P3/FinVsA.lean new file mode 100644 index 000000000..95ebbaf4f --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/FinVsA.lean @@ -0,0 +1,153 @@ +/- +P3 (aarch64, A64-P3 S3 gen 23, ruling 21:26Z item 2): **the S4 finaliser leaves of the VSpace caps** — FL4's +`objects::finalise_cap_object` arms `Cap::PageTable` (final, mapped: `vspace::unmap_page_table`) and `Cap::Frame` +(mapped, ASID ≠ 0: `is_ept ? ept::unmap : vspace::unmap_page`, then a final device frame's `retire_dma_frame`, an arch +step) against the spec's `fl4FinaliseCap` (`Ext/Finalise`) of their abstract caps, with **both** VSpace relations +(`VsRelA`, `ExtVsRelA`) at every outcome. The x86 counterparts are `P3/EptFinUnmap`'s `EptCore.finTblF` / +`finFrameFG` (and `P3/VsFin`'s `vs_fin` for the native tables); the `JAtZAX` lift (x86 `JAtZ.finVsJ`) is later work. + +* **`FinVsA`** (the leaves' post, over FL4's unmap result `u` and the finaliser's spec run `W`): nothing changed on + both sides (`u = ok (s, a)`, `W = {((nullCap, nullCap), σ)}`), or FL4 writes `0` at one word `slot` and then runs an + arch step `fa` (its flush), and the spec run never fails, has an outcome, and every outcome is `(nullCap, nullCap)` + with `VsRelA` and `ExtVsRelA` at the written memory. +* **`UnmapRelA.finVsA`** (the Stage-1 / no-root branch: `P3/ExtVsRelA`'s **`UnmapRelA.extE`** adds `ExtVsRelA` to + `UnmapRelA`'s `SpecOkA`; `finRetA` reads `do unmap…; return (nullCap, nullCap)`'s outcomes) and + **`ExtUnmapRelE.finVsA`** (the Stage-2 / high branches, the continuation an arch step `ArchContE`, from the `…XGE` + tops of `P3/ExtVsTopA`). +* **`fin_ptVA`** (from `unmap_page_tableXGE`) and **`fin_frameVA`** (from `unmap_frameXGE`): the leaves. +* **`fin_pt_eqA`** / **`fin_frame_eqA`**: FL4's two arms are their unmap followed by an arch-only tail (the frame's + `retire_dma_frame` changes only the arch state), so `FinVsA`'s store state is the arm's. + +Named premises: none new (`S2IpaE` [P] of `P3/ExtVsTopA`, as the tops it reads). Lemma hypotheses as +`unmap_page_tableXE` / `unmap_frameXE` (`hK`, `hT`, `hX`, `hVa` — `VaOkE` from `JAtZA.cv` for a plain frame). +Changed statements: none (`P3/ExtVsTopA`'s `unmap_page_tableXE`, `unmap_frame_s1XE`, `unmap_frameXE` are now +corollaries of their `…XGE` forms, statements unchanged). +-/ +import Fl4ProofsA64.P3.ExtVsTopA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {Pp Tb : Set U64} {Lv : U64 → Nat} {X Y T2 : Set U64} {Sh2 : U64 → Nat} {σ : Fl4SpecA64.State Unit} + +/-- **The finaliser leaves' post** (module docstring). -/ +def FinVsA (Pp Tb : Set U64) (Lv : U64 → Nat) (X Y T2 : Set U64) (Sh2 : U64 → Nat) (σ : Fl4SpecA64.State Unit) + {S A : Type} (st : machine.Store S) [L : LawfulStore st] (s : S) (a : A) (u : Result (S × A)) + (W : Set ((Fl4SpecA64.Cap × Fl4SpecA64.Cap) × Fl4SpecA64.State Unit) × Prop) : Prop := + (u = ok (s, a) ∧ W = ({((Fl4SpecA64.Cap.nullCap, Fl4SpecA64.Cap.nullCap), σ)}, False)) ∨ + ∃ (slot : U64) (fa : Result A), u = (do let a1 ← fa; ok (L.wr s slot 0#u64, a1)) ∧ ¬ W.2 ∧ (∃ x, x ∈ W.1) ∧ + ∀ x ∈ W.1, x.1 = (Fl4SpecA64.Cap.nullCap, Fl4SpecA64.Cap.nullCap) ∧ + VsRelA Pp Tb Lv X Y (L.mem (L.wr s slot 0#u64)) (L.ksb (L.wr s slot 0#u64)) x.2 ∧ + ExtVsRelA Pp Tb X Y T2 Sh2 (L.mem (L.wr s slot 0#u64)) x.2 + +/-- The outcomes of `do m; return c`. -/ +theorem finRetA (m : Fl4SpecA64.SMonad Unit Unit) (c : Fl4SpecA64.Cap × Fl4SpecA64.Cap) (σ : Fl4SpecA64.State Unit) : + ((do m; return c : Fl4SpecA64.SMonad Unit (Fl4SpecA64.Cap × Fl4SpecA64.Cap)) σ).2 = (m σ).2 ∧ + ∀ x, x ∈ ((do m; return c : Fl4SpecA64.SMonad Unit (Fl4SpecA64.Cap × Fl4SpecA64.Cap)) σ).1 ↔ + ∃ y ∈ (m σ).1, x = (c, y.2) := by + have hp : ∀ σ' : Fl4SpecA64.State Unit, + (pure c : Fl4SpecA64.SMonad Unit (Fl4SpecA64.Cap × Fl4SpecA64.Cap)) σ' = ({(c, σ')}, False) := fun _ => rfl + refine ⟨?_, fun x => ?_⟩ + · simp [nd_bind_applyD, hp] + · simp [nd_bind_applyD, hp] + +/-- **The Stage-1 / no-root branch** (module docstring: `UnmapRelA.extE`). -/ +theorem UnmapRelA.finVsA {A : Type} {s : S} {a : A} {fl : Result A} {u : Result (S × A)} + {m : Fl4SpecA64.SMonad Unit Unit} + (R : VsRelA Pp Tb Lv X Y (L.mem s) (L.ksb s) σ) (E : ExtVsRelA Pp Tb X Y T2 Sh2 (L.mem s) σ) + (h : UnmapRelA Pp Tb Lv X Y σ st s a fl u (m σ)) : + FinVsA Pp Tb Lv X Y T2 Sh2 σ st s a u + ((do m; return (Fl4SpecA64.Cap.nullCap, Fl4SpecA64.Cap.nullCap) : + Fl4SpecA64.SMonad Unit (Fl4SpecA64.Cap × Fl4SpecA64.Cap)) σ) := by + obtain ⟨hf, hm⟩ := finRetA m (Fl4SpecA64.Cap.nullCap, Fl4SpecA64.Cap.nullCap) σ + rcases UnmapRelA.extE R E h with ⟨hu, hW⟩ | ⟨slot, -, hu, ⟨h1, ⟨x0, hx0⟩, h3⟩, h4⟩ + · exact Or.inl ⟨hu, by rw [nd_bind_oneD hW]; rfl⟩ + · refine Or.inr ⟨slot, fl, hu, by rw [hf]; exact h1, ⟨_, (hm _).2 ⟨x0, hx0, rfl⟩⟩, fun x hx => ?_⟩ + obtain ⟨y, hy, rfl⟩ := (hm x).1 hx + exact ⟨rfl, (h3 y hy).1, h4 y hy⟩ + +/-- **The Stage-2 / high branches** (module docstring). -/ +theorem ExtUnmapRelE.finVsA {A : Type} {s : S} {a : A} {fk : S → Result (S × A)} (hk : ArchContE fk) + {u : Result (S × A)} {σ1 : Fl4SpecA64.State Unit} + (h : ExtUnmapRelE Pp Tb Lv X Y T2 Sh2 σ st s a fk u ({((), σ1)}, False)) : + FinVsA Pp Tb Lv X Y T2 Sh2 σ st s a u + ({((Fl4SpecA64.Cap.nullCap, Fl4SpecA64.Cap.nullCap), σ1)}, False) := by + rcases h with ⟨hu, hW⟩ | ⟨slot, σ', hu, hW, hR, hE, -⟩ + · have e := congrArg Prod.fst hW + simp only [Set.singleton_eq_singleton_iff, Prod.mk.injEq, true_and] at e + exact Or.inl ⟨hu, by rw [e]⟩ + · have e := congrArg Prod.fst hW + simp only [Set.singleton_eq_singleton_iff, Prod.mk.injEq, true_and] at e + subst e + obtain ⟨fa, hfa⟩ := hk (L.wr s slot 0#u64) + refine Or.inr ⟨slot, fa, hu.trans hfa, fun h => h, ⟨_, Set.mem_singleton _⟩, fun x hx => ?_⟩ + rw [Set.mem_singleton_iff] at hx + subst hx + exact ⟨rfl, hR, hE⟩ + +/-! ## The leaves -/ + +/-- **The final mapped page table cap's finaliser leaf** (module docstring). -/ +theorem fin_ptVA {A : Type} {ar : machine.Arch A} {s : S} {a : A} + (R : VsRelA Pp Tb Lv X Y (L.mem s) (L.ksb s) σ) (E : ExtVsRelA Pp Tb X Y T2 Sh2 (L.mem s) σ) + (hK : Aligned (L.ksb s)) (hT : OffAsE Pp Tb X Y T2 (L.ksb s)) + (hX : ∀ r ∈ X, ∃ es, σ.kheap (specPtr r) = some (.archObj (.ext (.stage2Root es)))) + (hIpa : S2IpaE ar a) {asid : U64} (h16 : asid.val < 65536) (va pt : U64) : + FinVsA Pp Tb Lv X Y T2 Sh2 σ st s a (vspace.unmap_page_table st ar s a asid va pt) + (Fl4SpecA64.fl4FinaliseCap (Z := Unit) + (.archObjectCap (.pageTableCap (specPtr pt) .normalPtT (some (BitVec.ofNat 16 asid.val, va.bv)))) true σ) := by + rcases unmap_page_tableXGE R E hK hT hX hIpa h16 va pt with ⟨-, hF, hU⟩ | ⟨k, fk, -, hk, hF, hU⟩ + · rw [hF]; exact UnmapRelA.finVsA R E hU + · rw [hF]; rw [extUnmapTable_runX] at hU; exact ExtUnmapRelE.finVsA hk.cont hU + +/-- **A mapped frame cap's finaliser leaf** (module docstring; `hVa` only for a plain frame). -/ +theorem fin_frameVA {A : Type} {ar : machine.Arch A} {s : S} {a : A} + (R : VsRelA Pp Tb Lv X Y (L.mem s) (L.ksb s) σ) (E : ExtVsRelA Pp Tb X Y T2 Sh2 (L.mem s) σ) + (hK : Aligned (L.ksb s)) (hT : OffAsE Pp Tb X Y T2 (L.ksb s)) + (hX : ∀ r ∈ X, ∃ es, σ.kheap (specPtr r) = some (.archObj (.ext (.stage2Root es)))) + (hIpa : S2IpaE ar a) {asid : U64} (h16 : asid.val < 65536) (sz : capability.FrameSize) (va pptr : U64) + (isEpt : Bool) (hVa : isEpt = false → VaOkE va) (Rt : Fl4SpecA64.CapRights) (dev final : Bool) : + FinVsA Pp Tb Lv X Y T2 Sh2 σ st s a + (if isEpt then (do let i ← capability.FrameSize.bits sz; ept.unmap st ar s a asid va pptr i false) + else vspace.unmap_page st ar s a sz asid va pptr) + (Fl4SpecA64.fl4FinaliseCap (Z := Unit) (.archObjectCap (absFrameE isEpt (specPtr pptr) Rt (frameSizeOfA sz) + dev (some (BitVec.ofNat 16 asid.val, va.bv)))) final σ) := by + rcases unmap_frameXGE R E hK hT hX hIpa h16 sz va pptr isEpt hVa Rt dev final with + ⟨rfl, -, hF, hU⟩ | ⟨σ', fk, hk, hF, hU⟩ + · rw [hF, if_neg Bool.false_ne_true]; exact UnmapRelA.finVsA R E hU + · rw [hF]; exact ExtUnmapRelE.finVsA hk.cont hU + +/-! ## FL4's arms -/ + +/-- FL4's final mapped `Cap::PageTable` arm is its unmap. -/ +theorem fin_pt_eqA {A : Type} (ar : machine.Arch A) (s : S) (a : A) (pt asid va : U64) : + objects.finalise_cap_object st ar s a (.PageTable pt true asid va) true = + (do let (s1, a1) ← vspace.unmap_page_table st ar s a asid va pt + ok ((capability.Cap.Null, capability.Cap.Null), s1, a1)) := by + simp only [objects.finalise_cap_object, ↓reduceIte] + +/-- FL4's mapped `Cap::Frame` arm (ASID ≠ 0) is its unmap followed by an arch-only tail. -/ +theorem fin_frame_eqA {A : Type} (ar : machine.Arch A) (s : S) (a : A) (ptr asid va : U64) + (sz : capability.FrameSize) (rt : U64) (dev isEpt fin : Bool) (h0 : asid ≠ 0#u64) : + objects.finalise_cap_object st ar s a (.Frame ptr sz rt dev true isEpt asid va) fin = + (do let (s1, a1) ← (if isEpt then (do let i ← capability.FrameSize.bits sz; ept.unmap st ar s a asid va ptr i false) + else vspace.unmap_page st ar s a sz asid va ptr) + if fin then + if dev then + let a2 ← objects.retire_dma_frame st ar s1 a1 ptr sz + ok ((capability.Cap.Null, capability.Cap.Null), s1, a2) + else ok ((capability.Cap.Null, capability.Cap.Null), s1, a1) + else ok ((capability.Cap.Null, capability.Cap.Null), s1, a1)) := by + have hb : (asid != 0#u64) = true := by simp [h0] + simp only [objects.finalise_cap_object, ↓reduceIte, hb] + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/FinVsJA.lean b/proofs/Fl4ProofsA64/P3/FinVsJA.lean new file mode 100644 index 000000000..3017f34be --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/FinVsJA.lean @@ -0,0 +1,356 @@ +/- +P3 (aarch64, A64-P3 S3 gen 24, ruling 21:50Z item 2 and Q3): **the `JAtZAX` lift of the VSpace finaliser leaves** +(`P3/FinVsA`'s `fin_ptVA` / `fin_frameVA`) on their **Stage-1 / no-root branch** — FL4's `unmap_page_table` / +`unmap_page` writes `0` into one word of a Stage-1 table, l4v's `unmap_page_table` / `unmap_page` changes that table +and the machine state, and the whole relation `JAtZAX` holds after both. The x86 counterpart is `JAtZ.finVsJ` +(`P3/VsFin`). + +* **The pure spec frame (ruling Q3)**: `KmA σ σ'` (the states differ only in `kheap` and `machineState`), `OutKmA m` + (every outcome of `m` is `KmA`-related to its start; closed under `bind`, the exception bind `ebind`, `liftE`, + `catch`, `unlessE`, `throwOpt` and the read-only primitives), and the spec operations: `setObject`, `storePte`, + `doMachineOp`, the two TLB invalidations, `findVspaceForAsid`, `ptLookupFromLevel` and **`unmapPage_kmA`** / + **`unmapPageTable_kmA`**. `KmA.globA`: such states have the same scheduler / IRQ / domain globals, ASID table, CDT + and original-cap map. `SpecOkA` and the unmap statements of `P3/VsUnmapA` are untouched. +* **`JAtZAX.of_s1ClearA`**: `JAtZAX` across FL4's zero write into word `i` of a Stage-1 table `p ∈ Z.S1` and a spec + outcome `σ'` that is `KmA`- and `KeepTbA`-related, with `VsRelA` / `ExtVsRelA` at `σ'` (from `UnmapRelA` / + `UnmapRelA.extE`), given `TabOffA` for every Stage-1 table (the decode-site hypothesis of gen 21) and the + footprint's alignment — through `P3/VsTabJA`'s `JAtZAX.of_sameS1A` (gen 24, the core of `JAtZAX.of_tabWriteA`). +* **`FinJA`** (`FinVsA` with `JAtZAX` at the written state and the entry's arch state in place of the two VSpace + relations), **`UnmapRelA.finJA`**, and the leaves **`fin_ptJA`** (a final mapped page table cap) and + **`fin_frameJA`** (a mapped plain frame cap), each on the branch `hS1` (the ASID names no root or a Stage-1 root). + The arch state is left at the entry's: FL4's flush after the write is the caller's arch step. + +The Stage-2 / high branches (`ExtUnmapRelE`) are not lifted here: their write lands in a Stage-2 / window word and the +spec rewrites a root or a Stage-2 table, so the lift needs an `ExtFootE`-word twin of `TabOffA` (gen 25). + +No named premise. Decode-site hypothesis: `TabOffA` (gen 21). No existing statement changed. +-/ +import Fl4ProofsA64.P3.FinVsA +import Fl4ProofsA64.P3.VsTabJA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4Spec + +noncomputable section + +/-! ## The pure spec frame of the Stage-1 unmaps -/ + +/-- **Only the heap and the machine state differ** (module docstring). -/ +def KmA (σ σ' : Fl4SpecA64.State Unit) : Prop := + ∃ kh ms, σ' = { σ with kheap := kh, machineState := ms } + +theorem KmA.refl (σ : Fl4SpecA64.State Unit) : KmA σ σ := ⟨σ.kheap, σ.machineState, rfl⟩ + +theorem KmA.trans {σ1 σ2 σ3 : Fl4SpecA64.State Unit} (h1 : KmA σ1 σ2) (h2 : KmA σ2 σ3) : KmA σ1 σ3 := by + obtain ⟨k1, m1, rfl⟩ := h1 + obtain ⟨k2, m2, rfl⟩ := h2 + exact ⟨k2, m2, rfl⟩ + +/-- Such states share the globals `JAtZAX` reads outside the heap. -/ +theorem KmA.globA {σ σ' : Fl4SpecA64.State Unit} (h : KmA σ σ') : + SpecGlobA σ σ' ∧ σ'.cdt = σ.cdt ∧ σ'.isOriginalCap = σ.isOriginalCap := by + obtain ⟨kh, ms, rfl⟩ := h + exact ⟨(by constructor <;> first | rfl | exact ⟨rfl, rfl, rfl, rfl⟩), rfl, rfl⟩ + +/-- **Every outcome is `KmA`-related to its start** (module docstring). -/ +def OutKmA {α : Type} (m : NonDet (Fl4SpecA64.State Unit) α) : Prop := + ∀ σ r, r ∈ (m σ).1 → KmA σ r.2 + +section out +variable {α β ε : Type} + +theorem OutKmA.bind {m : NonDet (Fl4SpecA64.State Unit) α} {f : α → NonDet (Fl4SpecA64.State Unit) β} + (hm : OutKmA m) (hf : ∀ a, OutKmA (f a)) : OutKmA (m >>= f) := fun σ r hr => by + obtain ⟨x, hx, hr⟩ := hr + exact (hm σ x hx).trans (hf x.1 x.2 r hr) + +/-- A computation with a single outcome at its start state. -/ +theorem OutKmA.one {m : NonDet (Fl4SpecA64.State Unit) α} (h : ∀ σ, ∃ v, m σ = ({(v, σ)}, False)) : + OutKmA m := fun σ r hr => by + obtain ⟨v, e⟩ := h σ + rw [e] at hr + obtain rfl : r = (v, σ) := hr + exact KmA.refl σ + +theorem OutKmA.pure (a : α) : OutKmA (Pure.pure a : NonDet (Fl4SpecA64.State Unit) α) := OutKmA.one fun _ => ⟨a, rfl⟩ + +theorem OutKmA.gets (g : Fl4SpecA64.State Unit → α) : OutKmA (NonDet.gets g) := OutKmA.one fun _ => ⟨_, rfl⟩ + +theorem OutKmA.get : OutKmA (NonDet.get : NonDet (Fl4SpecA64.State Unit) (Fl4SpecA64.State Unit)) := + OutKmA.one fun _ => ⟨_, rfl⟩ + +theorem OutKmA.assertOpt (o : Option α) : OutKmA (NonDet.assertOpt o : NonDet (Fl4SpecA64.State Unit) α) := by + cases o with + | none => exact fun _ _ h => absurd h (Set.notMem_empty _) + | some a => exact OutKmA.one fun _ => ⟨a, rfl⟩ + +theorem OutKmA.getsThe (g : Fl4SpecA64.State Unit → Option α) : OutKmA (NonDet.getsThe g) := fun σ r hr => + OutKmA.assertOpt (g σ) σ r hr + +theorem OutKmA.getsMap {γ : Type} (g : Fl4SpecA64.State Unit → γ → Option α) (p : γ) : + OutKmA (NonDet.getsMap g p) := + OutKmA.bind (OutKmA.gets g) fun x => OutKmA.assertOpt (x p) + +theorem OutKmA.assert (P : Prop) : OutKmA (NonDet.assert P : NonDet (Fl4SpecA64.State Unit) Unit) := fun σ r hr => by + obtain ⟨-, rfl⟩ := hr + exact KmA.refl σ + +theorem OutKmA.selectF (S : Set α × Prop) : OutKmA (NonDet.selectF S : NonDet (Fl4SpecA64.State Unit) α) := + fun σ r hr => by rw [hr.2]; exact KmA.refl σ + +theorem OutKmA.getPut {g : Fl4SpecA64.State Unit → Fl4SpecA64.State Unit} (hg : ∀ σ, KmA σ (g σ)) : + OutKmA (NonDet.get >>= fun s => NonDet.put (g s)) := fun σ r hr => by + obtain ⟨x, hx, hr⟩ := hr + obtain rfl : x = (σ, σ) := hx + obtain rfl : r = ((), g σ) := hr + exact hg σ + +theorem OutKmA.modify {g : Fl4SpecA64.State Unit → Fl4SpecA64.State Unit} (hg : ∀ σ, KmA σ (g σ)) : + OutKmA (NonDet.modify g) := fun σ r hr => by + obtain rfl : r = ((), g σ) := hr + exact hg σ + +/-! The exception monad, through `ExceptT.run`. -/ + +theorem OutKmA.ebind {m : NonDetE (Fl4SpecA64.State Unit) ε α} {f : α → NonDetE (Fl4SpecA64.State Unit) ε β} + (hm : OutKmA m.run) (hf : ∀ a, OutKmA (f a).run) : OutKmA (m >>= f).run := fun σ r hr => by + obtain ⟨⟨e | a, σ1⟩, hx, hr⟩ := hr + · obtain rfl : r = (.error e, σ1) := hr + exact hm σ _ hx + · exact (hm σ _ hx).trans (hf a σ1 r hr) + +theorem OutKmA.liftE {m : NonDet (Fl4SpecA64.State Unit) α} (hm : OutKmA m) : + OutKmA (NonDetE.liftE (ε := ε) m).run := fun σ r hr => by + obtain ⟨x, hx, hr⟩ := hr + obtain rfl : r = (.ok x.1, x.2) := hr + exact hm σ x hx + +theorem OutKmA.ret (a : α) : OutKmA (Pure.pure a : NonDetE (Fl4SpecA64.State Unit) ε α).run := + OutKmA.one fun _ => ⟨_, rfl⟩ + +theorem OutKmA.retOk (a : α) : OutKmA (NonDetE.returnOk a : NonDetE (Fl4SpecA64.State Unit) ε α).run := + OutKmA.one fun _ => ⟨_, rfl⟩ + +theorem OutKmA.throw (e : ε) : OutKmA (NonDetE.throwError e : NonDetE (Fl4SpecA64.State Unit) ε α).run := + OutKmA.one fun _ => ⟨_, rfl⟩ + +theorem OutKmA.unlessE {P : Prop} [Decidable P] {m : NonDetE (Fl4SpecA64.State Unit) ε Unit} (hm : OutKmA m.run) : + OutKmA (NonDetE.unlessE P m).run := by + unfold NonDetE.unlessE + split + · exact OutKmA.ret () + · exact hm + +theorem OutKmA.throwOpt (e : ε) (o : Option α) : + OutKmA (NonDetE.throwOpt e o : NonDetE (Fl4SpecA64.State Unit) ε α).run := by + cases o with + | none => exact OutKmA.one fun _ => ⟨_, rfl⟩ + | some a => exact OutKmA.ret a + +theorem OutKmA.catch {m : NonDetE (Fl4SpecA64.State Unit) ε α} {h : ε → NonDet (Fl4SpecA64.State Unit) α} + (hm : OutKmA m.run) (hh : ∀ e, OutKmA (h e)) : OutKmA (NonDetE.catch m h) := fun σ r hr => by + obtain ⟨⟨e | a, σ1⟩, hx, hr⟩ := hr + · exact (hm σ _ hx).trans (hh e σ1 r hr) + · obtain rfl : r = (a, σ1) := hr + exact hm σ _ hx + +end out + +/-! The spec operations. -/ + +theorem setObject_kmA (p : Fl4SpecA64.ObjRef) (o : Fl4SpecA64.KernelObject) : + OutKmA (Fl4SpecA64.setObject (Z := Unit) p o) := by + unfold Fl4SpecA64.setObject Fl4SpecA64.getObject + exact OutKmA.bind (OutKmA.getsThe _) fun _ => OutKmA.bind (OutKmA.assert _) fun _ => + OutKmA.getPut fun σ => ⟨_, σ.machineState, rfl⟩ + +theorem storePte_kmA (ptT : Fl4SpecA64.PtType) (p : Fl4SpecA64.ObjRef) (pte : Fl4SpecA64.PTE) : + OutKmA (Fl4SpecA64.storePte (Z := Unit) ptT p pte) := by + unfold Fl4SpecA64.storePte Fl4SpecA64.setPt + exact OutKmA.bind (OutKmA.assert _) fun _ => OutKmA.bind (OutKmA.pure _) fun _ => + OutKmA.bind (OutKmA.getsMap _ _) fun _ => OutKmA.bind (OutKmA.assert _) fun _ => + OutKmA.bind (OutKmA.getsMap _ _) fun _ => setObject_kmA _ _ + +theorem doMachineOp_kmA {α : Type} (mop : NonDet Fl4SpecA64.MachineState α) : + OutKmA (Fl4SpecA64.doMachineOp (Z := Unit) mop) := by + unfold Fl4SpecA64.doMachineOp + exact OutKmA.bind (OutKmA.gets _) fun _ => OutKmA.bind (OutKmA.selectF _) fun ⟨_, ms'⟩ => + OutKmA.bind (OutKmA.modify fun σ => ⟨σ.kheap, ms', rfl⟩) fun _ => OutKmA.pure _ + +theorem invTlbVa_kmA (asid : Fl4SpecA64.Asid) (v : Fl4SpecA64.VSpaceRef) : + OutKmA (Fl4SpecA64.invalidateTlbByAsidVa (Z := Unit) asid v) := by + unfold Fl4SpecA64.invalidateTlbByAsidVa Fl4SpecA64.loadVmid + refine OutKmA.bind (OutKmA.bind (OutKmA.gets _) fun _ => OutKmA.pure _) fun o => ?_ + cases o with + | none => exact OutKmA.pure _ + | some _ => exact doMachineOp_kmA _ + +theorem invTlb_kmA (asid : Fl4SpecA64.Asid) : OutKmA (Fl4SpecA64.invalidateTlbByAsid (Z := Unit) asid) := by + unfold Fl4SpecA64.invalidateTlbByAsid Fl4SpecA64.loadVmid + refine OutKmA.bind (OutKmA.bind (OutKmA.gets _) fun _ => OutKmA.pure _) fun o => ?_ + cases o with + | none => exact OutKmA.pure _ + | some _ => exact doMachineOp_kmA _ + +theorem findVs_kmA (asid : Fl4SpecA64.Asid) : OutKmA (Fl4SpecA64.findVspaceForAsid (Z := Unit) asid).run := by + unfold Fl4SpecA64.findVspaceForAsid + exact OutKmA.ebind (OutKmA.liftE (OutKmA.gets _)) fun _ => OutKmA.ebind (OutKmA.liftE (OutKmA.gets _)) fun o => + OutKmA.throwOpt _ o + +theorem ptLookupN_kmA : ∀ (n : Nat) (level : Fl4SpecA64.VmLevel) (pt : Fl4SpecA64.ObjRef) + (v : Fl4SpecA64.VSpaceRef) (t : Fl4SpecA64.ObjRef), + OutKmA (Fl4SpecA64.ptLookupFromLevelN (Z := Unit) n level pt v t).run + | 0, level, pt, v, t => by + rw [Fl4SpecA64.ptLookupFromLevelN] + exact OutKmA.ebind (OutKmA.unlessE (OutKmA.throw _)) fun _ => OutKmA.throw _ + | n + 1, level, pt, v, t => by + rw [Fl4SpecA64.ptLookupFromLevelN] + refine OutKmA.ebind (OutKmA.unlessE (OutKmA.throw _)) fun _ => ?_ + refine OutKmA.ebind (OutKmA.retOk _) fun _ => ?_ + refine OutKmA.ebind (OutKmA.liftE (OutKmA.getsThe _)) fun _ => ?_ + refine OutKmA.ebind (OutKmA.unlessE (OutKmA.throw _)) fun _ => ?_ + refine OutKmA.ebind (OutKmA.retOk _) fun ptr => ?_ + by_cases hp : ptr = t + · rw [if_pos hp]; exact OutKmA.retOk _ + · rw [if_neg hp]; exact ptLookupN_kmA n _ _ _ _ + +/-- **l4v's `unmap_page` changes only the heap and the machine state** (ruling Q3). -/ +theorem unmapPage_kmA (sz : Fl4SpecA64.VMPageSize) (asid : Fl4SpecA64.Asid) (v : Fl4SpecA64.VSpaceRef) + (p : Fl4SpecA64.ObjRef) : OutKmA (Fl4SpecA64.unmapPage (Z := Unit) sz asid v p) := by + unfold Fl4SpecA64.unmapPage + refine OutKmA.catch ?_ fun _ => OutKmA.pure _ + refine OutKmA.ebind (findVs_kmA _) fun _ => ?_ + refine OutKmA.ebind (OutKmA.liftE (OutKmA.getsThe _)) fun x => ?_ + obtain ⟨lev, slot⟩ := x + refine OutKmA.ebind (OutKmA.unlessE (OutKmA.throw _)) fun _ => ?_ + refine OutKmA.ebind (OutKmA.liftE (OutKmA.getsMap _ _)) fun _ => ?_ + refine OutKmA.ebind (OutKmA.unlessE (OutKmA.throw _)) fun _ => ?_ + refine OutKmA.ebind (OutKmA.liftE (storePte_kmA _ _ _)) fun _ => ?_ + refine OutKmA.ebind (OutKmA.liftE (doMachineOp_kmA _)) fun _ => ?_ + exact OutKmA.liftE (invTlbVa_kmA _ _) + +/-- **l4v's `unmap_page_table` changes only the heap and the machine state** (ruling Q3). -/ +theorem unmapPageTable_kmA (asid : Fl4SpecA64.Asid) (v : Fl4SpecA64.VSpaceRef) (pt : Fl4SpecA64.ObjRef) : + OutKmA (Fl4SpecA64.unmapPageTable (Z := Unit) asid v pt) := by + unfold Fl4SpecA64.unmapPageTable Fl4SpecA64.ptLookupFromLevel + refine OutKmA.catch ?_ fun _ => OutKmA.pure _ + refine OutKmA.ebind (findVs_kmA _) fun _ => ?_ + refine OutKmA.ebind (ptLookupN_kmA _ _ _ _ _) fun x => ?_ + obtain ⟨slot, level⟩ := x + refine OutKmA.ebind (OutKmA.liftE (storePte_kmA _ _ _)) fun _ => ?_ + refine OutKmA.ebind (OutKmA.liftE (doMachineOp_kmA _)) fun _ => ?_ + exact OutKmA.liftE (invTlb_kmA _) + +/-! ## `JAtZAX` across a Stage-1 unmap -/ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} + +/-- **`JAtZAX` across FL4's zero write into a Stage-1 table and l4v's unmap outcome** (module docstring). -/ +theorem JAtZAX.of_s1ClearA (J : JAtZAX (L := L) AR Z s a σ m) (hfa : ∀ w, FootA (L := L) Z s w → Aligned w) + (hTO : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s q) {p i : U64} (hp : p ∈ Z.S1) (hi : i.val < 512) + {σ' : Fl4SpecA64.State Unit} (hkm : KmA σ σ') (hkt : KeepTbA Z.S1 σ σ') + (VR : VsRelA Z.Pp Z.S1 Z.Lv Z.X Z.Y (L.mem (L.wr s (peAF p i) 0#u64)) (L.ksb (L.wr s (peAF p i) 0#u64)) σ') + (ER : ExtVsRelA Z.Pp Z.S1 Z.X Z.Y Z.T2 Z.Sh2 (L.mem (L.wr s (peAF p i) 0#u64)) σ') : + JAtZAX (L := L) AR Z (L.wr s (peAF p i) 0#u64) a σ' m := by + obtain ⟨HG, hcdt, horig⟩ := hkm.globA + have hal : Aligned (peAF p i) := peAF_alignedA (VR.al_mod hp) hi + have H : SameA (L := L) { Z with S1 := ∅ } s (L.wr s (peAF p i) 0#u64) := + ⟨L.ksb_wr _ _ _, L.cpu_wr _ _ _, fun w hw => by + rw [L.mem_wr _ _ _ _ hal (hfa w (FootA.s1_monoA hw)), if_neg fun (e : peAF p i = w) => (hTO p hp).mem i hi (e ▸ hw)]⟩ + have HS : SpecSameA { Z with S1 := ∅ } m σ σ' := + { toSpecGlobA := HG + kh := fun q hq => hkt q fun p' hp' => (hTO p' hp').obj q hq + ext := fun q ko e hx => by + by_cases hq : ∃ p' ∈ Z.S1, q = specPtr p' + · obtain ⟨p', hp', rfl⟩ := hq + obtain ⟨t, hk, -⟩ := VR.pt p' hp' + rw [hk] at e + cases e + rcases hx with ⟨_, h⟩ | ⟨_, h⟩ | ⟨_, h⟩ <;> cases h + · rwa [hkt q fun p' hp' e' => hq ⟨p', hp', e'⟩] at e + cdt := hcdt + orig := horig } + exact J.of_sameS1A H HS (fun _ hq hl => ⟨hq, hl⟩) VR ER + +/-- **The finaliser leaves' post with `JAtZAX`** (module docstring). -/ +def FinJA (AR : A → HwRootA → Prop) (Z : XSetsA) (σ : Fl4SpecA64.State Unit) (m : Fl4SpecA64.McState) (s : S) + (a : A) (u : Result (S × A)) + (W : Set ((Fl4SpecA64.Cap × Fl4SpecA64.Cap) × Fl4SpecA64.State Unit) × Prop) : Prop := + (u = ok (s, a) ∧ W = ({((Fl4SpecA64.Cap.nullCap, Fl4SpecA64.Cap.nullCap), σ)}, False)) ∨ + ∃ (slot : U64) (fa : Result A), u = (do let a1 ← fa; ok (L.wr s slot 0#u64, a1)) ∧ ¬ W.2 ∧ (∃ x, x ∈ W.1) ∧ + ∀ x ∈ W.1, x.1 = (Fl4SpecA64.Cap.nullCap, Fl4SpecA64.Cap.nullCap) ∧ + JAtZAX (L := L) AR Z (L.wr s slot 0#u64) a x.2 m + +/-- **The Stage-1 / no-root branch with `JAtZAX`** (module docstring). -/ +theorem UnmapRelA.finJA {fl : Result A} {u : Result (S × A)} {mm : Fl4SpecA64.SMonad Unit Unit} (hmm : OutKmA mm) + (J : JAtZAX (L := L) AR Z s a σ m) (hfa : ∀ w, FootA (L := L) Z s w → Aligned w) + (hTO : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s q) + (h : UnmapRelA Z.Pp Z.S1 Z.Lv Z.X Z.Y σ st s a fl u (mm σ)) : + FinJA (L := L) AR Z σ m s a u + ((do mm; return (Fl4SpecA64.Cap.nullCap, Fl4SpecA64.Cap.nullCap) : + Fl4SpecA64.SMonad Unit (Fl4SpecA64.Cap × Fl4SpecA64.Cap)) σ) := by + obtain ⟨hf, hm⟩ := finRetA mm (Fl4SpecA64.Cap.nullCap, Fl4SpecA64.Cap.nullCap) σ + rcases UnmapRelA.extE J.1.vs J.1.ext h with ⟨hu, hW⟩ | + ⟨slot, ⟨p, hp, i, hi, rfl⟩, hu, ⟨h1, ⟨x0, hx0⟩, h3⟩, h4⟩ + · exact Or.inl ⟨hu, by rw [nd_bind_oneD hW]; rfl⟩ + · refine Or.inr ⟨_, fl, hu, by rw [hf]; exact h1, ⟨_, (hm _).2 ⟨x0, hx0, rfl⟩⟩, fun x hx => ?_⟩ + obtain ⟨y, hy, rfl⟩ := (hm x).1 hx + exact ⟨rfl, J.of_s1ClearA hfa hTO hp hi (hmm σ y hy) (h3 y hy).2 (h3 y hy).1 (h4 y hy)⟩ + +/-- The branch hypothesis excludes an FL4 root. -/ +theorem s1_notExtA {asid : Fl4SpecA64.Asid} {r : Fl4SpecA64.ObjRef} {k : Fl4SpecA64.ExtRootKind} + (hS1 : Fl4SpecA64.asidRootKind σ asid = none ∨ Fl4SpecA64.asidRootKind σ asid = some .stage1) + (hv : Fl4SpecA64.vspaceForAsid asid σ = some r) (hk : Fl4SpecA64.rootKindAt σ r = k) (hk1 : k ≠ .stage1) : + False := by + have hak := asidRootKind_ofE hv + rw [hk] at hak + rcases hS1 with h | h <;> rw [h] at hak + · cases hak + · exact hk1 (Option.some.inj hak).symm + +/-- **The final mapped page table cap's finaliser leaf with `JAtZAX`**, Stage-1 / no-root branch (module +docstring; x86 `JAtZ.finVsJ`). -/ +theorem fin_ptJA {ar : machine.Arch A} (J : JAtZAX (L := L) AR Z s a σ m) + (hfa : ∀ w, FootA (L := L) Z s w → Aligned w) (hTO : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s q) + (hK : Aligned (L.ksb s)) (hT : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s)) {asid : U64} (h16 : asid.val < 65536) + (hS1 : Fl4SpecA64.asidRootKind σ (BitVec.ofNat 16 asid.val) = none ∨ + Fl4SpecA64.asidRootKind σ (BitVec.ofNat 16 asid.val) = some .stage1) (va pt : U64) : + FinJA (L := L) AR Z σ m s a (vspace.unmap_page_table st ar s a asid va pt) + (Fl4SpecA64.fl4FinaliseCap (Z := Unit) + (.archObjectCap (.pageTableCap (specPtr pt) .normalPtT (some (BitVec.ofNat 16 asid.val, va.bv)))) true σ) := by + rcases fl4FinaliseCap_pt_splitX (σ := σ) (asid := BitVec.ofNat 16 asid.val) (va := va.bv) (p := specPtr pt) with + ⟨k, r, hv, hk, hk1, -⟩ | ⟨-, hF⟩ + · exact (s1_notExtA hS1 hv hk hk1).elim + · rw [hF] + exact UnmapRelA.finJA (unmapPageTable_kmA _ _ _) J hfa hTO (unmap_page_tableA J.1.vs hK + (fun p hp i hi => hT p i hi (Or.inr (Or.inr (Or.inl hp)))) J.2.2.1.obj h16 (hs1_ofE hS1) va pt) + +/-- **A mapped plain frame cap's finaliser leaf with `JAtZAX`**, Stage-1 / no-root branch (module docstring; x86 +`JAtZ.finVsJ`). -/ +theorem fin_frameJA {ar : machine.Arch A} (J : JAtZAX (L := L) AR Z s a σ m) + (hfa : ∀ w, FootA (L := L) Z s w → Aligned w) (hTO : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s q) + (hK : Aligned (L.ksb s)) (hT : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s)) {asid : U64} (h16 : asid.val < 65536) + (hS1 : Fl4SpecA64.asidRootKind σ (BitVec.ofNat 16 asid.val) = none ∨ + Fl4SpecA64.asidRootKind σ (BitVec.ofNat 16 asid.val) = some .stage1) + (sz : capability.FrameSize) (va pptr : U64) (Rt : Fl4SpecA64.CapRights) (dev final : Bool) : + FinJA (L := L) AR Z σ m s a (vspace.unmap_page st ar s a sz asid va pptr) + (Fl4SpecA64.fl4FinaliseCap (Z := Unit) (.archObjectCap (.frameCap (specPtr pptr) Rt (frameSizeOfA sz) dev + (some (BitVec.ofNat 16 asid.val, va.bv)))) final σ) := by + rcases fl4FinaliseCap_frame_splitX (σ := σ) (asid := BitVec.ofNat 16 asid.val) (va := va.bv) + (p := specPtr pptr) (R := Rt) (sz := frameSizeOfA sz) (dev := dev) (final := final) with + ⟨r, hv, hk, -⟩ | ⟨r, hv, hk, -⟩ | ⟨-, hF⟩ + · exact (s1_notExtA hS1 hv hk (by decide)).elim + · exact (s1_notExtA hS1 hv hk (by decide)).elim + · rw [hF] + exact UnmapRelA.finJA (unmapPage_kmA _ _ _ _) J hfa hTO (unmap_pageA J.1.vs hK + (fun p hp i hi => hT p i hi (Or.inr (Or.inr (Or.inl hp)))) J.2.2.1.obj sz h16 (hs1_ofE hS1) va pptr) + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/FinVsXA.lean b/proofs/Fl4ProofsA64/P3/FinVsXA.lean new file mode 100644 index 000000000..a5a902e31 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/FinVsXA.lean @@ -0,0 +1,182 @@ +/- +P3 (aarch64, A64-P3 S3 gen 25, ruling 22:14Z item 1 and Q1): **the `JAtZAX` lift of the VSpace finaliser leaves on +their Stage-2 / high-window branch** (`P3/FinVsA`'s `fin_ptVA` / `fin_frameVA` through `ExtUnmapRelE`), completing +`P3/FinVsJA`'s Stage-1 / no-root branch: the leaves **`fin_ptJXA`** / **`fin_frameJXA`** hold for every root kind, +without `hS1`. The x86 counterpart is `JAtZ.finVsJ` (`P3/VsFin`) with `EptCore.finTblF` / `finFrameFG`. + +* **`ExtTabOffA Z m s`** — **decode-site hypothesis (ruling Q1, not a named premise)**, the Ext-word twin of + `TabOffA` (`P3/VsTabJA`): every word of a Stage-2 page (`S2PgE`: the roots' two pages, the Stage-2 tables) and of a + window's level-1 page is off the footprint at `extZA Z` (`Z` with the Stage-1 tables, Stage-2 roots / tables and + windows emptied: the slots, nodes, IRQ table, kernel-state globals, TCB / SC / VCPU words, pools and ASID table), and + no Stage-2 root, window or Stage-2 table is named by a related object of `extZA Z` (CNodes, TCBs, SCs, VCPUs, pools). + Same kind as `TabOffA` / `NewTabA`: A64 P1's `HeapKsOff` lets `other` objects (where the VSpace objects live) sit in + the kernel-state block, so it is not derivable from `JEntA`; S1A's placement discharges it. +* **`JAtZAX.of_extPageA`**: `JAtZAX` across a change of the words of one VSpace page `q` (a Stage-2 page, a window's + level-1 page or a Stage-1 table) and of the heap at one VSpace object (a Stage-2 root, a window, a Stage-2 or Stage-1 + table), with both VSpace relations after — shared with `P3/TabZeroA`'s table zeroing. +* **`JAtZAX.of_extClearA`**: `JAtZAX` across FL4's zero write and the spec's `extSlotWriteX` outcome, given + `ExtWrE` (gen 25's exposure of the written word and the changed object in `ExtUnmapRelE`), `TabOffA` for every + Stage-1 table (a window's walk may clear a Stage-1 table's entry) and `ExtTabOffA` — through `P3/VsTabJA`'s + `JAtZAX.of_sameXA`; the Stage-2 / window records' `back` from the kinds `ExtVsRelA` / `VsRelA` give the changed object. +* **`ExtUnmapRelE.finJA`** (the Ext branch into `P3/FinVsJA`'s `FinJA`), **`fin_ptJXA`**, **`fin_frameJXA`**. + +No named premise (`S2IpaE` [P] read by the leaves as in `P3/FinVsA`). Decode-site hypotheses: `TabOffA` (gen 21), +**`ExtTabOffA`** (new). No existing statement changed here (`ExtUnmapRelE`'s strengthening is in `P3/ExtVsUnmapA`). +-/ +import Fl4ProofsA64.P3.FinVsJA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4Spec + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} + +/-- **The Stage-2 and window objects off the rest of the relation** (decode-site hypothesis, module docstring). -/ +structure ExtTabOffA (Z : XSetsA) (m : Fl4SpecA64.McState) (s : S) : Prop where + mem : ∀ q, (q ∈ S2PgE Z.X Z.T2 ∨ ∃ y ∈ Z.Y, q = wa y 4096#u64) → ∀ i : U64, i.val < 512 → + ¬ FootA (L := L) (extZA Z) s (peAF q i) + obj : ∀ p, (p ∈ Z.X ∨ p ∈ Z.Y ∨ p ∈ Z.T2) → ¬ RelPA (extZA Z) m (specPtr p) + +/-- **`JAtZAX` across a change of one VSpace page's words and one VSpace object** (module docstring): FL4's state +changes only words of the page-aligned page `q` (a Stage-2 page, a window's level-1 page or a Stage-1 table), the +spec's only the heap at the object `specPtr p0` (a Stage-2 root, a window, a Stage-2 or a Stage-1 table), with both +VSpace relations after. The core of `JAtZAX.of_extClearA` (one zero write) and of `P3/TabZeroA`'s table zeroing. -/ +theorem JAtZAX.of_extPageA (J : JAtZAX (L := L) AR Z s a σ m) (hfa : ∀ w, FootA (L := L) Z s w → Aligned w) + (hTO : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s q) (hEO : ExtTabOffA (L := L) Z m s) {q : U64} + (hq : q ∈ S2PgE Z.X Z.T2 ∨ (∃ y ∈ Z.Y, q = wa y 4096#u64) ∨ q ∈ Z.S1) {s' : S} + (hks : L.ksb s' = L.ksb s) (hcpu : L.cpu s' = L.cpu s) + (hm : ∀ w, Aligned w → (∀ j : U64, j.val < 512 → w ≠ peAF q j) → L.mem s' w = L.mem s w) {p0 : U64} + (hp0 : p0 ∈ Z.X ∨ p0 ∈ Z.Y ∨ p0 ∈ Z.T2 ∨ p0 ∈ Z.S1) {kh : Fl4SpecA64.KHeap} + (hkh : ∀ p, p ≠ specPtr p0 → kh p = σ.kheap p) + (VR : VsRelA Z.Pp Z.S1 Z.Lv Z.X Z.Y (L.mem s') (L.ksb s') { σ with kheap := kh }) + (ER : ExtVsRelA Z.Pp Z.S1 Z.X Z.Y Z.T2 Z.Sh2 (L.mem s') { σ with kheap := kh }) : + JAtZAX (L := L) AR Z s' a { σ with kheap := kh } m := by + have hoff : ∀ j : U64, j.val < 512 → ¬ FootA (L := L) (extZA Z) s (peAF q j) := fun j hj => by + rcases hq with h | h | h + · exact hEO.mem q (Or.inl h) j hj + · exact hEO.mem q (Or.inr h) j hj + · exact fun hf => (hTO q h).mem j hj hf.extMonoA + have H : SameA (L := L) (extZA Z) s s' := + ⟨hks, hcpu, fun w hw => hm w (hfa w (FootA.s1_monoA hw.extMonoA)) fun j hj e => hoff j hj (e ▸ hw)⟩ + have hne : ∀ p, RelPA (extZA Z) m p → p ≠ specPtr p0 := fun p hp e => by + subst e + rcases hp0 with h | h | h | h + · exact hEO.obj p0 (Or.inl h) hp + · exact hEO.obj p0 (Or.inr (Or.inl h)) hp + · exact hEO.obj p0 (Or.inr (Or.inr h)) hp + · exact (hTO p0 h).obj _ hp.extMonoA rfl + -- The changed object is a Stage-2 root of `Z.X`, a window of `Z.Y`, or a table. + have hobj : ∀ ko, kh (specPtr p0) = some ko → ExtObjA ko → + (∃ es, ko = .archObj (.ext (.stage2Root es)) ∧ p0 ∈ Z.X) ∨ + (∃ l1, ko = .archObj (.ext (.highVSpace l1)) ∧ p0 ∈ Z.Y) := by + intro ko e hx + rcases hp0 with h | h | h | h + · obtain ⟨es, hk, -⟩ := ER.s2root p0 h + obtain rfl := (show kh (specPtr p0) = _ from hk).symm.trans e |> Option.some.inj + exact Or.inl ⟨es, rfl, h⟩ + · obtain ⟨l1, hk, -⟩ := VR.high p0 h + obtain rfl := (show kh (specPtr p0) = _ from hk).symm.trans e |> Option.some.inj + exact Or.inr ⟨l1, rfl, h⟩ + · obtain ⟨pt, hk, -⟩ := ER.s2pt p0 h + obtain rfl := (show kh (specPtr p0) = _ from hk).symm.trans e |> Option.some.inj + rcases hx with ⟨_, h'⟩ | ⟨_, h'⟩ | ⟨_, h'⟩ <;> cases h' + · obtain ⟨pt, hk, -⟩ := VR.pt p0 h + obtain rfl := (show kh (specPtr p0) = _ from hk).symm.trans e |> Option.some.inj + rcases hx with ⟨_, h'⟩ | ⟨_, h'⟩ | ⟨_, h'⟩ <;> cases h' + have hv : ∀ p x, kh p = some (.archObj (.vcpu x)) → σ.kheap p = some (.archObj (.vcpu x)) := fun p x e => by + by_cases hp : p = specPtr p0 + · subst hp + rcases hobj _ e (Or.inl ⟨x, rfl⟩) with ⟨_, h, -⟩ | ⟨_, h, -⟩ <;> cases h + · rwa [hkh p hp] at e + have hx : ∀ p es, kh p = some (.archObj (.ext (.stage2Root es))) → ∃ r ∈ Z.X, specPtr r = p := fun p es e => by + by_cases hp : p = specPtr p0 + · subst hp + rcases hobj _ e (Or.inr (Or.inl ⟨es, rfl⟩)) with ⟨_, -, h⟩ | ⟨_, h, -⟩ + · exact ⟨p0, h, rfl⟩ + · cases h + · exact J.2.2.1.back p es (by rwa [hkh p hp] at e) + have hy : ∀ p l1, kh p = some (.archObj (.ext (.highVSpace l1))) → ∃ r ∈ Z.Y, specPtr r = p := fun p l1 e => by + by_cases hp : p = specPtr p0 + · subst hp + rcases hobj _ e (Or.inr (Or.inr ⟨l1, rfl⟩)) with ⟨_, h, -⟩ | ⟨_, -, h⟩ + · cases h + · exact ⟨p0, h, rfl⟩ + · exact J.2.2.2.back p l1 (by rwa [hkh p hp] at e) + exact J.of_sameXA (σ' := { σ with kheap := kh }) H (by constructor <;> first | rfl | exact ⟨rfl, rfl, rfl, rfl⟩) (fun p hp => hkh p (hne p hp)) rfl rfl + hv hx hy (fun _ hq hl => ⟨hq, hl⟩) VR ER + +/-- **`JAtZAX` across an Ext unmap's zero write and its spec outcome** (module docstring). -/ +theorem JAtZAX.of_extClearA (J : JAtZAX (L := L) AR Z s a σ m) (hfa : ∀ w, FootA (L := L) Z s w → Aligned w) + (hTO : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s q) (hEO : ExtTabOffA (L := L) Z m s) {slot : U64} + {σ' : Fl4SpecA64.State Unit} (hw : ExtWrE Z.S1 Z.X Z.Y Z.T2 σ slot σ') + (VR : VsRelA Z.Pp Z.S1 Z.Lv Z.X Z.Y (L.mem (L.wr s slot 0#u64)) (L.ksb (L.wr s slot 0#u64)) σ') + (ER : ExtVsRelA Z.Pp Z.S1 Z.X Z.Y Z.T2 Z.Sh2 (L.mem (L.wr s slot 0#u64)) σ') : + JAtZAX (L := L) AR Z (L.wr s slot 0#u64) a σ' m := by + obtain ⟨⟨q, j, hj, rfl, hq4, hq⟩, p0, hp0, kh, rfl, hkh⟩ := hw + have hal : Aligned (peAF q j) := peAF_alignedA (Nat.mod_eq_zero_of_dvd hq4) hj + exact J.of_extPageA hfa hTO hEO hq (L.ksb_wr _ _ _) (L.cpu_wr _ _ _) (fun w hw hn => by + rw [L.mem_wr _ _ _ _ hal hw, if_neg fun e => hn j hj e.symm]) hp0 hkh VR ER + +/-- **The Stage-2 / high branch with `JAtZAX`** (module docstring). -/ +theorem ExtUnmapRelE.finJA {fk : S → Result (S × A)} (hk : ArchContE fk) {u : Result (S × A)} + {σ1 : Fl4SpecA64.State Unit} (J : JAtZAX (L := L) AR Z s a σ m) (hfa : ∀ w, FootA (L := L) Z s w → Aligned w) + (hTO : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s q) (hEO : ExtTabOffA (L := L) Z m s) + (h : ExtUnmapRelE Z.Pp Z.S1 Z.Lv Z.X Z.Y Z.T2 Z.Sh2 σ st s a fk u ({((), σ1)}, False)) : + FinJA (L := L) AR Z σ m s a u ({((Fl4SpecA64.Cap.nullCap, Fl4SpecA64.Cap.nullCap), σ1)}, False) := by + rcases h with ⟨hu, hW⟩ | ⟨slot, σ', hu, hW, hR, hE, hw⟩ + · have e := congrArg Prod.fst hW + simp only [Set.singleton_eq_singleton_iff, Prod.mk.injEq, true_and] at e + exact Or.inl ⟨hu, by rw [e]⟩ + · have e := congrArg Prod.fst hW + simp only [Set.singleton_eq_singleton_iff, Prod.mk.injEq, true_and] at e + subst e + obtain ⟨fa, hfa'⟩ := hk (L.wr s slot 0#u64) + refine Or.inr ⟨slot, fa, hu.trans hfa', fun h => h, ⟨_, Set.mem_singleton _⟩, fun x hx => ?_⟩ + rw [Set.mem_singleton_iff] at hx + subst hx + exact ⟨rfl, J.of_extClearA hfa hTO hEO hw hR hE⟩ + +/-- **The final mapped page table cap's finaliser leaf with `JAtZAX`, every root kind** (module docstring; x86 +`JAtZ.finVsJ`). -/ +theorem fin_ptJXA {ar : machine.Arch A} (J : JAtZAX (L := L) AR Z s a σ m) + (hfa : ∀ w, FootA (L := L) Z s w → Aligned w) (hTO : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s q) + (hEO : ExtTabOffA (L := L) Z m s) (hK : Aligned (L.ksb s)) (hT : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s)) + (hIpa : S2IpaE ar a) {asid : U64} (h16 : asid.val < 65536) (va pt : U64) : + FinJA (L := L) AR Z σ m s a (vspace.unmap_page_table st ar s a asid va pt) + (Fl4SpecA64.fl4FinaliseCap (Z := Unit) + (.archObjectCap (.pageTableCap (specPtr pt) .normalPtT (some (BitVec.ofNat 16 asid.val, va.bv)))) true σ) := by + rcases unmap_page_tableXGE J.1.vs J.1.ext hK hT J.2.2.1.obj hIpa h16 va pt with + ⟨hS1, -, -⟩ | ⟨k, fk, -, hk, hF, hU⟩ + · exact fin_ptJA J hfa hTO hK hT h16 hS1 va pt + · rw [hF]; rw [extUnmapTable_runX] at hU; exact ExtUnmapRelE.finJA hk.cont J hfa hTO hEO hU + +/-- **A mapped frame cap's finaliser leaf with `JAtZAX`, every root kind** (module docstring; `hVa` only for a plain +frame; x86 `JAtZ.finVsJ`). -/ +theorem fin_frameJXA {ar : machine.Arch A} (J : JAtZAX (L := L) AR Z s a σ m) + (hfa : ∀ w, FootA (L := L) Z s w → Aligned w) (hTO : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s q) + (hEO : ExtTabOffA (L := L) Z m s) (hK : Aligned (L.ksb s)) (hT : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s)) + (hIpa : S2IpaE ar a) {asid : U64} (h16 : asid.val < 65536) (sz : capability.FrameSize) (va pptr : U64) + (isEpt : Bool) (hVa : isEpt = false → VaOkE va) (Rt : Fl4SpecA64.CapRights) (dev final : Bool) : + FinJA (L := L) AR Z σ m s a + (if isEpt then (do let i ← capability.FrameSize.bits sz; ept.unmap st ar s a asid va pptr i false) + else vspace.unmap_page st ar s a sz asid va pptr) + (Fl4SpecA64.fl4FinaliseCap (Z := Unit) (.archObjectCap (absFrameE isEpt (specPtr pptr) Rt (frameSizeOfA sz) + dev (some (BitVec.ofNat 16 asid.val, va.bv)))) final σ) := by + rcases unmap_frameXGE J.1.vs J.1.ext hK hT J.2.2.1.obj hIpa h16 sz va pptr isEpt hVa Rt dev final with + ⟨rfl, hS1, -, -⟩ | ⟨σ', fk, hk, hF, hU⟩ + · rw [if_neg Bool.false_ne_true] + exact fin_frameJA J hfa hTO hK hT h16 hS1 sz va pptr Rt dev final + · rw [hF]; exact ExtUnmapRelE.finJA hk.cont J hfa hTO hEO hU + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/FrAgDefA.lean b/proofs/Fl4ProofsA64/P3/FrAgDefA.lean new file mode 100644 index 000000000..c886f84c9 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/FrAgDefA.lean @@ -0,0 +1,96 @@ +/- +P3 (AArch64, A64-P3 delframe): **frame-word agreement** of the deletion engine (Architect ruling (b′), 2026-12-05). + +* **`FrameWordLive st OS s b`**: some `OS` slot holds a non-device `Frame` cap covering `b` (the words `FrUbP` puts in + `Ub`). +* **`FrAgA st OS Ag s σ`**: every aligned live frame word of the guard set `Ag` holds the same 64-bit word in FL4's + memory and in the spec's `underlyingMemory` (at `specPtr b`). `Ag = ∅` gives `True` (`FrAgA.nilA`). +* **`FrAgA.stepA`**: agreement across a step that keeps liveness backwards (`FrameWordLive.keepA` / `.fromA`), the + live words and the spec's user memory. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.EpRelA +import Fl4ProofsA64.P1.MdbA64 + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4SpecA64 + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **A live non-device frame word**: some `OS` slot holds a non-device `Frame` cap covering `b` (the words `FrUbP` +puts in `Ub`). -/ +def FrameWordLive (st : machine.Store S) [L : LawfulStore st] (OS : Set U64) (s : S) (b : U64) : Prop := + ∃ u ∈ OS, ∃ ptr size rights x1 x2 x3 x4 bits, capA st s u = .Frame ptr size rights false x1 x2 x3 x4 ∧ + capability.FrameSize.bits size = ok bits ∧ ptr.val ≤ b.val ∧ b.val < ptr.val + 2 ^ bits.val + +/-- **Frame-word agreement on `Ag`** (module docstring): every aligned live frame word of `Ag` holds the same 64-bit +word on both sides. -/ +def FrAgA (st : machine.Store S) [L : LawfulStore st] (OS Ag : Set U64) (s : S) (σ : Fl4SpecA64.State Unit) : Prop := + ∀ b ∈ Ag, Aligned b → FrameWordLive st OS s b → + (L.mem s b).bv = loadWordAt σ.machineState.underlyingMemory (specPtr b) + +variable {OS Ag : Set U64} {s s' : S} {σ σ' : Fl4SpecA64.State Unit} + +theorem FrAgA.nilA : FrAgA st OS ∅ s σ := fun _ h => by simp at h + +/-- **Agreement across a step** that keeps liveness backwards, the live words and the spec's user memory. -/ +theorem FrAgA.stepA (h : FrAgA st OS Ag s σ) (back : ∀ b, FrameWordLive st OS s' b → FrameWordLive st OS s b) + (fm : ∀ b, Aligned b → FrameWordLive st OS s b → L.mem s' b = L.mem s b) + (um : σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory) : FrAgA st OS Ag s' σ' := + fun b hb al lv => by rw [um, fm b al (back b lv)]; exact h b hb al (back b lv) + +/-- Liveness backwards across a step keeping the `OS` caps. -/ +theorem FrameWordLive.keepA (capE : ∀ u ∈ OS, capA st s' u = capA st s u) {b : U64} + (h : FrameWordLive st OS s' b) : FrameWordLive st OS s b := by + obtain ⟨u, hu, ptr, size, rights, x1, x2, x3, x4, bits, hc, r⟩ := h + exact ⟨u, hu, ptr, size, rights, x1, x2, x3, x4, bits, (capE u hu).symm.trans hc, r⟩ + +/-- Liveness backwards across a step moving caps (`Null`, `Zombie`, or a cap an `OS` slot held). -/ +theorem FrameWordLive.fromA + (caps : ∀ y ∈ OS, capA st s' y = .Null ∨ (∃ p t c, capA st s' y = .Zombie p t c) ∨ + ∃ z ∈ OS, capA st s' y = capA st s z) {b : U64} + (h : FrameWordLive st OS s' b) : FrameWordLive st OS s b := by + obtain ⟨u, hu, ptr, size, rights, x1, x2, x3, x4, bits, hc, r⟩ := h + rcases caps u hu with c | ⟨_, _, _, c⟩ | ⟨z, hz, c⟩ + · rw [c] at hc; cases hc + · rw [c] at hc; cases hc + · exact ⟨z, hz, ptr, size, rights, x1, x2, x3, x4, bits, c.symm.trans hc, r⟩ + +/-- **Agreement kept pointwise** by a step that keeps the `OS` caps: every aligned live frame word that agreed still +agrees (VCPU steps; `read_state_to_frame` writes the same word on both sides). -/ +def FrKeepA (st : machine.Store S) [L : LawfulStore st] (OS : Set U64) (s : S) (σ : Fl4SpecA64.State Unit) (s' : S) + (σ' : Fl4SpecA64.State Unit) : Prop := + (∀ u ∈ OS, capA st s' u = capA st s u) ∧ + ∀ b, Aligned b → FrameWordLive st OS s b → + (L.mem s b).bv = loadWordAt σ.machineState.underlyingMemory (specPtr b) → + (L.mem s' b).bv = loadWordAt σ'.machineState.underlyingMemory (specPtr b) + +theorem FrKeepA.refl : FrKeepA st OS s σ s σ := ⟨fun _ _ => rfl, fun _ _ _ h => h⟩ + +theorem FrKeepA.trans {s'' : S} {σ'' : Fl4SpecA64.State Unit} (h : FrKeepA st OS s σ s' σ') + (k : FrKeepA st OS s' σ' s'' σ'') : FrKeepA st OS s σ s'' σ'' := + ⟨fun u hu => (k.1 u hu).trans (h.1 u hu), fun b al lv e => + k.2 b al (FrameWordLive.keepA (fun u hu => (h.1 u hu).symm) lv) (h.2 b al lv e)⟩ + +/-- A step keeping the live words and the spec's user memory. -/ +theorem FrKeepA.of_off (capE : ∀ u ∈ OS, capA st s' u = capA st s u) + (fm : ∀ b, Aligned b → FrameWordLive st OS s b → L.mem s' b = L.mem s b) + (um : σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory) : FrKeepA st OS s σ s' σ' := + ⟨capE, fun b al lv e => by rw [um, fm b al lv]; exact e⟩ + +theorem FrAgA.keepP (h : FrAgA st OS Ag s σ) (k : FrKeepA st OS s σ s' σ') : FrAgA st OS Ag s' σ' := + fun b hb al lv => by + have lv0 := FrameWordLive.keepA k.1 lv + exact k.2 b al lv0 (h b hb al lv0) + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/FrFlushA.lean b/proofs/Fl4ProofsA64/P3/FrFlushA.lean new file mode 100644 index 000000000..206d78063 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/FrFlushA.lean @@ -0,0 +1,1550 @@ +/- +P3 (aarch64, A64-P3 S3 gen 34, ruling 02:48Z item 1): **the frame cache-maintenance labels and the frame top with +them** — FL4's `decode_page_flush` (`crates/fl4-core/src/arch/aarch64/vspace.rs` 884–921, and `ept.rs` 338–368 +`ept::flush_frame` for a Stage-2 frame) against the spec's decoders the dispatcher `fl4ArchDecodeInvocation` picks at +the frame cache labels (`Fl4SpecA64/Ext/Invocation`): a plain frame whose ASID names no root or a Stage-1 root, +FL4's EL1 `decodeFl4FrInvFlush` (`Ext/VSpace` item 3); a plain frame whose ASID names an FL4 root, +`decodeExtPageFlushAt .high`; a Stage-2 frame, `decodeExtPageFlushAt .stage2` (`Ext/Stage2`). The outcome is +`P3/VsTopPtA`'s `VsTopPostA`. The x86 counterpart is `XTopJointVsMap`'s frame top (x86 has no frame cache labels; the +shape is gen 33's `P3/VsFlushA`). + +* **The labels**: **`labNA_frFlA`** (FL4's 51..54 are l4v's page flush labels, `Invalidate_Data` exactly 52), + **`is_inv_frA`**, **`decode_frame_flA`** (`decode_frame` at 51..54 is `decode_page_flush`). +* **FL4's front** (**`flTailF`**: `decode_page_flush` after the checks, verbatim; **`page_flush_frontA`**: the length, + ASID 0, `end <= start` and page-size checks for any postcondition), **`frame_root_s1A`** / **`_hiA`** / **`_s2A`** / + **`_errA`** (FL4's `frame_root` by the raw entry's tag), **`ept_find_errE`**. +* **The spec's decoders**: the dispatcher's arms **`dec_frFlS1A`** / **`dec_frFlHiA`** / **`dec_frFlS2A`**; the runs of + `decodeFl4FrInvFlush` (**`frFl_truncA`** / **`_illA`** / **`_leA`** / **`_szA`** / **`_bodyA`** to the tail + **`frFlTailA`**, **`frFlTail_errA`** / **`_okA`**, the frame tail `fl4FrFlushFrame`'s **`frFF_noneA`** / **`_misA`** / + **`_rightsA`** / **`_okA`**) and of `decodeExtPageFlushAt` (**`exFl_*A`**, the tail **`exFlTailA`** with its + descriptor part **`exFFA`**); **`run_liftME_congrA`**. +* **The front adapters**: **`frontS1A`**, **`frontExA`** (FL4's front against each decoder's). +* **The mapping checks**: **`mapped_entry_s1A`** (FL4's `mapped_entry` in l4v's tables against `lookupFramePte` and + `fl4FrFlushFrame`'s check), **`extMatchE`** (`extMappedEntry` from a related lookup position, FL4's three checks), + **`mapped_entry_hiE`** (from a window root: below level-0 index 256 the walk stops, `ExtVsRelA.wlo`; at 511 the + window's walk), the rights at a related slot (**`rightsHiE`**, **`rightsS2E`**, **`s2_rights_ofE`**). +* **The performs**: **`perfPgFl_memA`** (l4v's `pageFlush`), **`perfExtFl_memA`** (the Ext `.flush`). +* **The arms**: **`fl_s1JA`** (no root / a Stage-1 root: `frame_root`'s Stage-1 branch, `mapped_entry` against + `lookupFramePte`, the EL1 rights check, the restart, `ArchKeepsA.cacheOp`, l4v's `perform_flush`), **`fl_hiJA`** (a + high window: `frame_root`'s `ROOT_HIGH` branch, the window walk), **`fl_hiS2JA`** (a plain frame on a Stage-2 ASID: + `frame_root` `InvalidRoot` against `findExtRoot .high`), **`fl_s2JA`** (a Stage-2 frame: `ept::flush_frame` against + `decodeExtPageFlushAt .stage2`; a device frame restarts with nothing to flush, the spec's `vspaceNothing`). +* **`fr_flushJA`**: FL4's `decode_page_flush` against the dispatcher at 51..54, every frame cap. +* **`decInv_frame_jA`** (moved from `P3/VsTopFrA`): the frame top, now with the cache labels. + +**The unmapped frame's ASID** (ruling 02:48Z F4): FL4's `decode_page_flush` reads only `f.asid`; `CapWfA`'s frame +arm now carries `mapped = false → asid = 0` (`P3/CapRelA`), so an ASID-0 test is the spec's unmapped test. + +Named premises: `ArchKeepsA` [P], `S2IpaE` [P] (as the map / unmap tops). Decode-site hypotheses (standing): +`RsSiteA`, the front end's `hlen` / `hargs`, `TabOffA`, `ExtTabOffA`, `FrPlA`, `NoExtXA`. **Changed statement**: +`decInv_frame_jA`'s case selector `hLbl` narrows to the labels 55..57 (`lbl.val ≤ 54 ∨ 57 < lbl.val`; was +`lbl.val ≤ 50 ∨ 57 < lbl.val`); module `VsTopFrA` → `FrFlushA`. No new named premise. +-/ +import Fl4ProofsA64.P3.ExtMapFrA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## The labels -/ + +section lbl +open Fl4SpecA64 + +set_option maxRecDepth 8000 in +/-- FL4's frame cache labels 51..54: l4v's page flush labels, `Invalidate_Data` exactly at 52, neither FL4-Ext frame +label. -/ +theorem labNA_frFlA : ∀ n, 51 ≤ n → n ≤ 54 → + isPageFlushLabel (specInvA (labNA n)) = true ∧ + (labelToFlushType (specLabelWord (labNA n)) = .invalidate ↔ n = 52) ∧ + labNA n ≠ .ext .armPageDeriveSubframe ∧ labNA n ≠ .ext .armPageTestAndClearAccessed := by + intro n h1 h2 + unfold labelToFlushType + rw [invType_specA] + interval_cases n <;> decide + +end lbl + +section fl4lbl +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {s : S} {a : A} + +theorem is_inv_frA {lbl : U64} (hl : 51 ≤ lbl.val ∧ lbl.val ≤ 54) : + vspace.is_invalidate lbl = ok (decide (lbl.val = 52)) := by + have v52 : fl4_abi.label.ARMPageInvalidate_Data.val = 52 := by simp [fl4_abi.label.ARMPageInvalidate_Data] + have v43 : fl4_abi.label.ARMVSpaceInvalidate_Data.val = 43 := by simp [fl4_abi.label.ARMVSpaceInvalidate_Data] + unfold vspace.is_invalidate + by_cases h : lbl.val = 52 + · rw [if_pos (UScalar.eq_of_val_eq (by rw [h, v52])), decide_eq_true h] + · rw [if_neg (fun e => h (by rw [e, v52])), decide_eq_false h] + congr 1 + exact decide_eq_false (fun e => by rw [e, v43] at hl; omega) + +/-- FL4's `decode_frame` at a frame cache label is `decode_page_flush`. -/ +theorem decode_frame_flA (ar : machine.Arch A) (lbl length : U64) (slot : obj.Slot) (fp rt asid va : U64) + (sz : capability.FrameSize) (d mp e call : Bool) (buf : U64) (ex : Aeneas.Std.Array obj.Slot 3#usize) + (h : 51 ≤ lbl.val ∧ lbl.val ≤ 54) : + vspace.decode_frame st ar s a lbl length slot (.Frame fp sz rt d mp e asid va) call buf ex = + vspace.decode_page_flush st ar s a lbl length + { ptr := fp, size := sz, rights := rt, is_device := d, mapped := mp, is_ept := e, asid := asid, vaddr := va } + buf := by + have v49 : fl4_abi.label.ARMPageMap.val = 49 := by simp [fl4_abi.label.ARMPageMap] + have v50 : fl4_abi.label.ARMPageUnmap.val = 50 := by simp [fl4_abi.label.ARMPageUnmap] + have v51 : fl4_abi.label.ARMPageClean_Data.val = 51 := by simp [fl4_abi.label.ARMPageClean_Data] + have v52 : fl4_abi.label.ARMPageInvalidate_Data.val = 52 := by simp [fl4_abi.label.ARMPageInvalidate_Data] + have v53 : fl4_abi.label.ARMPageCleanInvalidate_Data.val = 53 := by + simp [fl4_abi.label.ARMPageCleanInvalidate_Data] + have v54 : fl4_abi.label.ARMPageUnify_Instruction.val = 54 := by simp [fl4_abi.label.ARMPageUnify_Instruction] + have h49 : lbl ≠ fl4_abi.label.ARMPageMap := fun e => by rw [e, v49] at h; omega + have h50 : lbl ≠ fl4_abi.label.ARMPageUnmap := fun e => by rw [e, v50] at h; omega + have hc : vspace.is_cache_label lbl = ok true := by + unfold vspace.is_cache_label + by_cases e1 : lbl = fl4_abi.label.ARMPageClean_Data + · rw [if_pos e1] + rw [if_neg e1] + by_cases e2 : lbl = fl4_abi.label.ARMPageInvalidate_Data + · rw [if_pos e2] + rw [if_neg e2] + by_cases e3 : lbl = fl4_abi.label.ARMPageCleanInvalidate_Data + · rw [if_pos e3] + rw [if_neg e3] + have n1 : lbl.val ≠ 51 := fun x => e1 (UScalar.eq_of_val_eq (by rw [x, v51])) + have n2 : lbl.val ≠ 52 := fun x => e2 (UScalar.eq_of_val_eq (by rw [x, v52])) + have n3 : lbl.val ≠ 53 := fun x => e3 (UScalar.eq_of_val_eq (by rw [x, v53])) + have e4 : lbl = fl4_abi.label.ARMPageUnify_Instruction := UScalar.eq_of_val_eq (by rw [v54]; omega) + congr 1 + exact decide_eq_true e4 + unfold vspace.decode_frame vspace.as_frame + simp only [cur_thread_ok, bind_ok, h49, h50, hc, ↓reduceIte] + +end fl4lbl + +/-! ## FL4's front -/ + +section front +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {s : S} {a : A} + +/-- FL4's frame view of a `Cap::Frame` (`as_frame`). -/ +abbrev frCapA (fp : U64) (sz : capability.FrameSize) (rt : U64) (dev mp e : Bool) (asid va : U64) : vspace.FrameCap := + { ptr := fp, size := sz, rights := rt, is_device := dev, mapped := mp, is_ept := e, asid := asid, vaddr := va } + +/-- **`decode_page_flush` after its checks** (Funs `vspace.decode_page_flush`, verbatim; module docstring). -/ +def flTailF (st : machine.Store S) (ar : machine.Arch A) (s : S) (a : A) (lbl : U64) (f : vspace.FrameCap) + (ct start «end» : U64) : Result ((core.result.Result Unit error.Exc) × S × A) := + if f.is_ept + then do + let i2 ← vspace.page_cache_op lbl + let b ← vspace.is_invalidate lbl + ept.flush_frame st ar s a i2 b f.asid f.vaddr f.ptr f.size f.is_device start «end» + else do + let r ← vspace.frame_root st s f.asid + match r with + | core.result.Result.Ok r1 => + let (root, cache_root) := r1 + let o ← vspace.mapped_entry st s f root + match o with + | none => let r2 ← error.invalid_cap Unit 0#u64 + ok (r2, s, a) + | some x => + let b ← vspace.is_invalidate lbl + if b + then + let i2 ← st.read s x.slot + let i3 ← vspace.rights_of i2 + if i3 != capability.VM_READ_WRITE + then let r2 ← error.illegal Unit + ok (r2, s, a) + else + let s1 ← sched.set_thread_state st s ct sched.TS_RESTART + let i4 ← layout.paddr f.ptr + let pstart ← lift (core.num.U64.wrapping_add i4 start) + let i5 ← vspace.page_cache_op lbl + let i6 ← lift (core.num.U64.wrapping_add f.vaddr start) + let i7 ← «end» - start + let a1 ← ar.cache_op a i5 cache_root f.asid i6 i7 pstart + ok (core.result.Result.Ok (), s1, a1) + else + let s1 ← sched.set_thread_state st s ct sched.TS_RESTART + let i2 ← layout.paddr f.ptr + let pstart ← lift (core.num.U64.wrapping_add i2 start) + let i3 ← vspace.page_cache_op lbl + let i4 ← lift (core.num.U64.wrapping_add f.vaddr start) + let i5 ← «end» - start + let a1 ← ar.cache_op a i3 cache_root f.asid i4 i5 pstart + ok (core.result.Result.Ok (), s1, a1) + | core.result.Result.Err lf => + ok (core.result.Result.Err (error.Exc.Sys (error.SysError.FailedLookup false lf)), s, a) + +theorem one_shl_valA {k : U64} (hk : k.val < 64) : (⟨(1#u64 : U64).bv <<< k.val⟩ : U64).val = 2 ^ k.val := by + have hp : 2 ^ k.val < 2 ^ 64 := Nat.pow_lt_pow_right (by decide) hk + show ((1#u64 : U64).bv <<< k.val).toNat = _ + rw [BitVec.toNat_shiftLeft, Nat.shiftLeft_eq] + show 1 * 2 ^ k.val % 2 ^ 64 = _ + rw [Nat.one_mul, Nat.mod_eq_of_lt hp] + +set_option maxHeartbeats 1000000 in +/-- **FL4's front checks** (module docstring), for any postcondition. -/ +theorem page_flush_frontA {ar : machine.Arch A} {P : core.result.Result Unit error.Exc × S × A → Prop} + (lbl length buf : U64) (f : vspace.FrameCap) (args : List U64) (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) + (htr : args.length < 2 → P (.Err (.Sys .TruncatedMessage), s, a)) + (hil : 2 ≤ args.length → f.asid = 0#u64 → P (.Err (.Sys .IllegalOperation), s, a)) + (hle : ∀ a0 a1 r, args = a0 :: a1 :: r → f.asid ≠ 0#u64 → a1 ≤ a0 → + P (.Err (.Sys (.InvalidArgument 1#u64)), s, a)) + (hsz : ∀ a0 a1 r, args = a0 :: a1 :: r → f.asid ≠ 0#u64 → ¬ a1 ≤ a0 → + (2 ^ (psA f.size).val ≤ a0.val ∨ 2 ^ (psA f.size).val < a1.val) → + P (.Err (.Sys (.InvalidArgument 0#u64)), s, a)) + (hk : ∀ a0 a1 r, args = a0 :: a1 :: r → f.asid ≠ 0#u64 → ¬ a1 ≤ a0 → a0.val < 2 ^ (psA f.size).val → + a1.val ≤ 2 ^ (psA f.size).val → flTailF st ar s a lbl f (L.curW s) a0 a1 ⦃ P ⦄) : + vspace.decode_page_flush st ar s a lbl length f buf ⦃ P ⦄ := by + unfold vspace.decode_page_flush + rw [cur_thread_ok, bind_ok] + by_cases h2 : length < 2#u64 + · rw [if_pos h2] + simp only [error.truncated, bind_ok, WP.spec_ok] + have : length.val < 2 := by scalar_tac + exact htr (by omega) + rw [if_neg h2] + have h2' : 2 ≤ args.length := by + have : ¬ length.val < 2 := fun h => h2 (by scalar_tac) + omega + by_cases h0 : f.asid = 0#u64 + · rw [if_pos h0] + simp only [error.illegal, bind_ok, WP.spec_ok] + exact hil h2' h0 + rw [if_neg h0] + obtain ⟨a0, a1, r', rfl⟩ : ∃ a0 a1 r', args = a0 :: a1 :: r' := by + match args, h2' with + | a0 :: a1 :: r', _ => exact ⟨_, _, _, rfl⟩ + rw [hargs 0#u64 a0 (by simp), bind_ok, hargs 1#u64 a1 (by simp), bind_ok] + by_cases h3 : a1 ≤ a0 + · rw [if_pos h3] + simp only [error.invalid_arg, bind_ok, WP.spec_ok] + exact hle a0 a1 r' rfl h0 h3 + rw [if_neg h3, page_shift_eqA, bind_ok] + have hs64 : (psA f.size).val < 64 := by rcases psA_valA f.size with h | h | h <;> omega + have e63 : (psA f.size &&& 63#u64) = psA f.size := + UScalar.eq_of_val_eq (by + rw [and_maskA (psA f.size) 6 _ rfl]; exact Nat.mod_eq_of_lt (by simp only [Nat.reducePow]; omega)) + rw [show (lift (psA f.size &&& 63#u64) : Result U64) = ok (psA f.size) by rw [e63]; rfl, bind_ok, + CapRt.shl_u64_eq 1#u64 (psA f.size) hs64, bind_ok] + have hsv := one_shl_valA hs64 + generalize (⟨(1#u64 : U64).bv <<< (psA f.size).val⟩ : U64) = SZ at hsv + by_cases h5 : a0 ≥ SZ + · rw [if_pos h5] + simp only [error.invalid_arg, bind_ok, WP.spec_ok] + have : SZ.val ≤ a0.val := h5 + exact hsz a0 a1 r' rfl h0 h3 (Or.inl (by omega)) + rw [if_neg h5] + by_cases h6 : a1 > SZ + · rw [if_pos h6] + simp only [error.invalid_arg, bind_ok, WP.spec_ok] + have : SZ.val < a1.val := h6 + exact hsz a0 a1 r' rfl h0 h3 (Or.inr (by omega)) + rw [if_neg h6] + have h5' : ¬ SZ.val ≤ a0.val := h5 + have h6' : ¬ SZ.val < a1.val := h6 + exact hk a0 a1 r' rfl h0 h3 (by omega) (by omega) + +/-! ### `frame_root` -/ + +theorem frame_root_s1A {asid w : U64} (h : vspace.find_asid_raw st s asid = ok (.Ok w)) (ht : tagA w = 0) : + vspace.frame_root st s asid = ok (.Ok (w, w)) := by + unfold vspace.frame_root + rw [h, bind_ok] + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok, lift, rtag, rs1, rhigh] + rw [(tag_iffA w).2 ht, if_pos rfl] + +theorem frame_root_hiA {asid w : U64} (h : vspace.find_asid_raw st s asid = ok (.Ok w)) (ht : tagA w = 2) : + vspace.frame_root st s asid = ok (.Ok (untagA w, 0#u64)) := by + unfold vspace.frame_root + rw [h, bind_ok] + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok, lift, rtag, rs1, rhigh] + rw [if_neg (fun e => by rw [(tagNE w 0 0#u64 rfl).1 e] at ht; cases ht), if_pos ((tagNE w 2 2#u64 rfl).2 ht), + untagE] + +theorem frame_root_s2A {asid w : U64} (h : vspace.find_asid_raw st s asid = ok (.Ok w)) (ht : tagA w = 1) : + vspace.frame_root st s asid = ok (.Err .InvalidRoot) := by + unfold vspace.frame_root + rw [h, bind_ok] + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok, lift, rtag, rs1, rhigh] + rw [if_neg (fun e => by rw [(tagNE w 0 0#u64 rfl).1 e] at ht; cases ht), + if_neg (fun e => by rw [(tagNE w 2 2#u64 rfl).1 e] at ht; cases ht)] + +theorem frame_root_errA {asid : U64} {e : error.LookupFault} (h : vspace.find_asid_raw st s asid = ok (.Err e)) : + vspace.frame_root st s asid = ok (.Err e) := by + unfold vspace.frame_root + rw [h, bind_ok] + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame.from, + bind_tc_ok] + +/-- `ept::find`'s errors are `InvalidRoot`. -/ +theorem ept_find_errE {asid : U64} (h16 : asid.val < 65536) : + ept.find st s asid ⦃ r => ∀ e, r = .Err e → e = .InvalidRoot ⦄ := by + unfold ept.find + refine WP.spec_bind (find_asid_raw_errA s h16) fun r hr => ?_ + rcases r with w | e + · simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok, lift] + split + · exact (WP.spec_ok _).2 fun e h => by injection h with h; exact h.symm + · exact (WP.spec_ok _).2 fun e h => by cases h + · simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame.from, + bind_tc_ok, WP.spec_ok] + intro e' h + injection h with h + subst h + exact hr _ rfl + +end front + +/-! ## The spec's decoders -/ + +section spec +open Fl4Spec Fl4SpecA64 + +theorem run_bind_congrA {σ ε α β : Type} {m m' : NonDetE σ ε α} {s : σ} (h : m.run s = m'.run s) + (f : α → NonDetE σ ε β) : (m >>= f).run s = (m' >>= f).run s := by + rw [ExceptT.run_bind, ExceptT.run_bind] + show NonDet.bind _ _ s = NonDet.bind _ _ s + rw [ndBindA, ndBindA, h] + +/-- `liftME` of two decoders with the same run. -/ +theorem run_liftME_congrA {α β : Type} {f : α → β} {m m' : NonDetE (State Unit) SyscallError α} + {σ : State Unit} (h : m.run σ = m'.run σ) : (NonDetE.liftME f m).run σ = (NonDetE.liftME f m').run σ := by + unfold NonDetE.liftME + exact run_bind_congrA h _ + +variable (calling : Bool) (xSlot : CapRef) (cte : CslotPtr) (exs : List (Cap × CslotPtr)) {σ : State Unit} + +/-- **The dispatcher on a plain frame with no FL4 root** (module docstring). -/ +theorem dec_frFlS1A (args : List Data) (l : Fl4Label) + (hf : isPageFlushLabel (invocationType (specLabelWord l)) = true) (hd : l ≠ .ext .armPageDeriveSubframe) + (ht : l ≠ .ext .armPageTestAndClearAccessed) (p : ObjRef) (R : CapRights) (sz : VMPageSize) (dev : Bool) + (md : Option (Asid × VSpaceRef)) + (hk : ∀ asid v, md = some (asid, v) → asidRootKind σ asid = none ∨ asidRootKind σ asid = some .stage1) : + (fl4ArchDecodeInvocation (Z := Unit) calling l args xSlot cte (.frameCap p R sz dev md) exs).run σ = + (NonDetE.liftME Fl4ArchInvocation.spec + (decodeFl4FrInvFlush (specLabelWord l) args cte (.frameCap p R sz dev md) exs)).run σ := by + unfold fl4ArchDecodeInvocation + dsimp only + rw [if_neg hd, if_neg ht, if_pos hf] + cases md with + | none => + simp only [NonDetE.returnOk, ndE_pure_bindA] + | some x => + obtain ⟨asid, v⟩ := x + refine (run_bind_okD (run_liftE_stD (extKindOfAsid_noneA (hk asid v rfl))) _).trans ?_ + rfl + +/-- **The dispatcher on a plain frame whose ASID names an FL4 root** (module docstring). -/ +theorem dec_frFlHiA (args : List Data) (l : Fl4Label) + (hf : isPageFlushLabel (invocationType (specLabelWord l)) = true) (hd : l ≠ .ext .armPageDeriveSubframe) + (ht : l ≠ .ext .armPageTestAndClearAccessed) (p : ObjRef) (R : CapRights) (sz : VMPageSize) (dev : Bool) + (md : Option (Asid × VSpaceRef)) (asid : Asid) (v : VSpaceRef) (hmd : md = some (asid, v)) {k : ExtRootKind} + (hk : asidRootKind σ asid = some k) (hk1 : k ≠ .stage1) : + (fl4ArchDecodeInvocation (Z := Unit) calling l args xSlot cte (.frameCap p R sz dev md) exs).run σ = + (NonDetE.liftME Fl4ArchInvocation.extVSpace (decodeExtPageFlushAt .high (specLabelWord l) args p sz dev md)).run σ := by + subst hmd + unfold fl4ArchDecodeInvocation + dsimp only + rw [if_neg hd, if_neg ht, if_pos hf] + refine (run_bind_okD (run_liftE_stD (extKindOfAsid_someA hk hk1)) _).trans ?_ + rfl + +/-- **The dispatcher on a Stage-2 frame** (module docstring). -/ +theorem dec_frFlS2A (args : List Data) (l : Fl4Label) + (hf : isPageFlushLabel (invocationType (specLabelWord l)) = true) (hd : l ≠ .ext .armPageDeriveSubframe) + (ht : l ≠ .ext .armPageTestAndClearAccessed) (p : ObjRef) (R : CapRights) (sz : VMPageSize) (dev : Bool) + (md : Option (Asid × VSpaceRef)) : + fl4ArchDecodeInvocation (Z := Unit) calling l args xSlot cte (.ext (.stage2FrameCap p R sz dev md)) exs = + NonDetE.liftME Fl4ArchInvocation.extVSpace (decodeExtPageFlushAt .stage2 (specLabelWord l) args p sz dev md) := by + unfold fl4ArchDecodeInvocation + dsimp only + rw [if_neg hd, if_neg ht, if_pos hf] + rfl + +/-! ### `decodeFl4FrInvFlush` -/ + +/-- `decodeFl4FrInvFlush` after its checks (module docstring). -/ +def frFlTailA (lab a0 a1 : Data) (p : ObjRef) (pgsz : VMPageSize) (v : VSpaceRef) (asid : Asid) : + SEMonad Unit ArchInvocation := do + let vspace ← lookupErrorOnFailure false <| findVspaceForAsid asid + let frameInfo ← NonDetE.liftE (NonDet.gets fun s : State Unit => lookupFramePte vspace v (ptesOf s)) + fl4FrFlushFrame lab a0 a1 p pgsz v vspace asid frameInfo + +theorem frFl_truncA (lab : Data) (args : List Data) (p : ObjRef) (R : CapRights) (pgsz : VMPageSize) (dev : Bool) + (md : Option (Asid × VSpaceRef)) (h : ¬ args.length > 1) : + (decodeFl4FrInvFlush (Z := Unit) lab args cte (.frameCap p R pgsz dev md) exs).run σ = + ({(.error .truncatedMessage, σ)}, False) := by + simp only [decodeFl4FrInvFlush, if_neg h]; rfl + +theorem frFl_illA (lab a0 a1 : Data) (r : List Data) (p : ObjRef) (R : CapRights) (pgsz : VMPageSize) (dev : Bool) + (md : Option (Asid × VSpaceRef)) (h : ∀ asid v, md = some (asid, v) → asid = 0) : + (decodeFl4FrInvFlush (Z := Unit) lab (a0 :: a1 :: r) cte (.frameCap p R pgsz dev md) exs).run σ = + ({(.error .illegalOperation, σ)}, False) := by + cases md with + | none => + simp only [decodeFl4FrInvFlush, List.length_cons, gt_iff_lt, Nat.lt_add_left_iff_pos, Nat.zero_lt_succ, + ↓reduceIte, NonDetE.throwError, ndE_throw_bindA] + rfl + | some x => + obtain ⟨asid, v⟩ := x + obtain rfl := h asid v rfl + simp only [decodeFl4FrInvFlush, List.length_cons, gt_iff_lt, Nat.lt_add_left_iff_pos, Nat.zero_lt_succ, + ↓reduceIte, NonDetE.throwError, ndE_throw_bindA] + rfl + +theorem frFl_leA (lab a0 a1 : Data) (r : List Data) (p : ObjRef) (R : CapRights) (pgsz : VMPageSize) (dev : Bool) + (asid : Asid) (v : VSpaceRef) (hz : asid ≠ 0) (h : a1 ≤ a0) : + (decodeFl4FrInvFlush (Z := Unit) lab (a0 :: a1 :: r) cte (.frameCap p R pgsz dev (some (asid, v))) exs).run σ = + ({(.error (.invalidArgument 1), σ)}, False) := by + simp only [decodeFl4FrInvFlush, List.length_cons, gt_iff_lt, Nat.lt_add_left_iff_pos, Nat.zero_lt_succ, + ↓reduceIte, List.getElem?_cons_zero, List.getElem?_cons_succ, the, hz, NonDetE.returnOk, ndE_pure_bindA, + NonDetE.whenE, h, NonDetE.throwError, ndE_throw_bindA] + rfl + +theorem frFl_szA (lab a0 a1 : Data) (r : List Data) (p : ObjRef) (R : CapRights) (pgsz : VMPageSize) (dev : Bool) + (asid : Asid) (v : VSpaceRef) (hz : asid ≠ 0) (h1 : ¬ a1 ≤ a0) + (h2 : a0 ≥ (1 : Word) <<< pageBitsForSize pgsz ∨ a1 > (1 : Word) <<< pageBitsForSize pgsz) : + (decodeFl4FrInvFlush (Z := Unit) lab (a0 :: a1 :: r) cte (.frameCap p R pgsz dev (some (asid, v))) exs).run σ = + ({(.error (.invalidArgument 0), σ)}, False) := by + simp only [decodeFl4FrInvFlush, List.length_cons, gt_iff_lt, Nat.lt_add_left_iff_pos, Nat.zero_lt_succ, + ↓reduceIte, List.getElem?_cons_zero, List.getElem?_cons_succ, the, hz, NonDetE.returnOk, ndE_pure_bindA, + NonDetE.whenE, h1, h2, NonDetE.throwError, ndE_throw_bindA] + rfl + +theorem frFl_bodyA (lab a0 a1 : Data) (r : List Data) (p : ObjRef) (R : CapRights) (pgsz : VMPageSize) (dev : Bool) + (asid : Asid) (v : VSpaceRef) (hz : asid ≠ 0) (h1 : ¬ a1 ≤ a0) + (h2 : ¬ (a0 ≥ (1 : Word) <<< pageBitsForSize pgsz ∨ a1 > (1 : Word) <<< pageBitsForSize pgsz)) : + (decodeFl4FrInvFlush (Z := Unit) lab (a0 :: a1 :: r) cte (.frameCap p R pgsz dev (some (asid, v))) exs).run σ = + (frFlTailA lab a0 a1 p pgsz v asid).run σ := by + simp only [decodeFl4FrInvFlush, List.length_cons, gt_iff_lt, Nat.lt_add_left_iff_pos, Nat.zero_lt_succ, + ↓reduceIte, List.getElem?_cons_zero, List.getElem?_cons_succ, the, hz, NonDetE.returnOk, ndE_pure_bindA, + NonDetE.whenE, h1, h2, frFlTailA] + try rfl + +theorem frFlTail_errA {lab a0 a1 : Data} {p : ObjRef} {pgsz : VMPageSize} {v : VSpaceRef} {asid : Asid} + (h : (findVspaceForAsid (Z := Unit) asid).run σ = ({(.error .invalidRoot, σ)}, False)) : + (frFlTailA lab a0 a1 p pgsz v asid).run σ = ({(.error (.failedLookup false .invalidRoot), σ)}, False) := by + unfold frFlTailA + exact run_bind_errorA (lefo_errA h) _ + +theorem frFlTail_okA {lab a0 a1 : Data} {p : ObjRef} {pgsz : VMPageSize} {v : VSpaceRef} {asid : Asid} {w : ObjRef} + (h : (lookupErrorOnFailure (Z := Unit) false (findVspaceForAsid asid)).run σ = ({(.ok w, σ)}, False)) : + (frFlTailA lab a0 a1 p pgsz v asid).run σ = + (fl4FrFlushFrame (Z := Unit) lab a0 a1 p pgsz v w asid (lookupFramePte w v (ptesOf σ))).run σ := by + unfold frFlTailA + rw [run_bind_okA h] + exact run_bind_okA (liftE_runA (m := NonDet.gets _) rfl) _ + +section ff +variable {lab st en : Data} {p : ObjRef} {pgsz : VMPageSize} {v : VSpaceRef} {vs : ObjRef} {asid : Asid} + +theorem frFF_noneA : (fl4FrFlushFrame (Z := Unit) lab st en p pgsz v vs asid none).run σ = + ({(.error (.invalidCapability 0), σ)}, False) := rfl + +theorem frFF_misA {sz : VMPageSize} {pte : PTE} (h : ¬ (sz = pgsz ∧ pteBaseAddr pte = addrFromPPtr p)) : + (fl4FrFlushFrame (Z := Unit) lab st en p pgsz v vs asid (some (sz, pte))).run σ = + ({(.error (.invalidCapability 0), σ)}, False) := by + unfold fl4FrFlushFrame + dsimp only + rw [if_neg h] + rfl + +theorem frFF_rightsA {sz : VMPageSize} {pte : PTE} (h1 : sz = pgsz ∧ pteBaseAddr pte = addrFromPPtr p) + (h2 : labelToFlushType lab = .invalidate ∧ pte.pteRights ≠ vmReadWrite) : + (fl4FrFlushFrame (Z := Unit) lab st en p pgsz v vs asid (some (sz, pte))).run σ = + ({(.error .illegalOperation, σ)}, False) := by + unfold fl4FrFlushFrame + dsimp only + rw [if_pos h1, if_pos h2] + rfl + +theorem frFF_okA {sz : VMPageSize} {pte : PTE} (h1 : sz = pgsz ∧ pteBaseAddr pte = addrFromPPtr p) + (h2 : ¬ (labelToFlushType lab = .invalidate ∧ pte.pteRights ≠ vmReadWrite)) : + (fl4FrFlushFrame (Z := Unit) lab st en p pgsz v vs asid (some (sz, pte))).run σ = + ({(.ok (.invokePage (.pageFlush (labelToFlushType lab) (v + st) (v + en - 1) (addrFromPPtr p + st) vs asid)), + σ)}, False) := by + unfold fl4FrFlushFrame + dsimp only + rw [if_pos h1, if_neg h2] + rfl + +end ff + +/-! ### `decodeExtPageFlushAt` -/ + +/-- The descriptor part of `decodeExtPageFlushAt` (module docstring). -/ +def exFFA (k : ExtRootKind) (lab a0 a1 : Data) (p : ObjRef) (dev : Bool) (asid : Asid) (va : VSpaceRef) + (r : ObjRef) : Option (ExtPos × PTE) → SEMonad Unit ExtVSpaceInvocation + | none => NonDetE.throwError (.invalidCapability 0) + | some (_, pte) => do + NonDetE.whenE (labelToFlushType lab = .invalidate ∧ pte.pteRights ≠ vmReadWrite) <| + NonDetE.throwError .illegalOperation + if k = .stage2 ∧ dev then NonDetE.returnOk (.flush .vspaceNothing) + else NonDetE.returnOk <| .flush <| + .vspaceFlush (labelToFlushType lab) (va + a0) (va + a1 - 1) (addrFromPPtr p + a0) r asid + +/-- `decodeExtPageFlushAt` after its checks (module docstring). -/ +def exFlTailA (k : ExtRootKind) (lab a0 a1 : Data) (p : ObjRef) (sz : VMPageSize) (dev : Bool) (asid : Asid) + (va : VSpaceRef) : SEMonad Unit ExtVSpaceInvocation := do + let r ← lookupErrorOnFailure false <| findExtRoot k asid + let me ← NonDetE.liftE <| NonDet.gets fun s : State Unit => + if extVaOk k va then extMappedEntry s k r p sz va else none + exFFA k lab a0 a1 p dev asid va r me + +variable {k : ExtRootKind} + +theorem exFl_truncA (lab : Data) (args : List Data) (p : ObjRef) (sz : VMPageSize) (dev : Bool) + (md : Option (Asid × VSpaceRef)) (h : args.length < 2) : + (decodeExtPageFlushAt (Z := Unit) k lab args p sz dev md).run σ = ({(.error .truncatedMessage, σ)}, False) := by + simp only [decodeExtPageFlushAt, NonDetE.whenE, h, ↓reduceIte, NonDetE.throwError, ndE_throw_bindA] + rfl + +theorem exFl_illA (lab a0 a1 : Data) (r : List Data) (p : ObjRef) (sz : VMPageSize) (dev : Bool) + (md : Option (Asid × VSpaceRef)) (h : ∀ asid v, md = some (asid, v) → asid = 0) : + (decodeExtPageFlushAt (Z := Unit) k lab (a0 :: a1 :: r) p sz dev md).run σ = + ({(.error .illegalOperation, σ)}, False) := by + have hl : ¬ (a0 :: a1 :: r).length < 2 := by simp + cases md with + | none => + simp only [decodeExtPageFlushAt, NonDetE.whenE, hl, ↓reduceIte, ndE_pure_bindA, NonDetE.throwError, + ndE_throw_bindA] + rfl + | some x => + obtain ⟨asid, v⟩ := x + obtain rfl := h asid v rfl + simp only [decodeExtPageFlushAt, NonDetE.whenE, hl, ↓reduceIte, ndE_pure_bindA, NonDetE.throwError, + ndE_throw_bindA] + rfl + +theorem exFl_leA (lab a0 a1 : Data) (r : List Data) (p : ObjRef) (sz : VMPageSize) (dev : Bool) + (asid : Asid) (v : VSpaceRef) (hz : asid ≠ 0) (h : a1 ≤ a0) : + (decodeExtPageFlushAt (Z := Unit) k lab (a0 :: a1 :: r) p sz dev (some (asid, v))).run σ = + ({(.error (.invalidArgument 1), σ)}, False) := by + have hl : ¬ (a0 :: a1 :: r).length < 2 := by simp + simp only [decodeExtPageFlushAt, NonDetE.whenE, hl, ↓reduceIte, ndE_pure_bindA, hz, NonDetE.returnOk, + List.getElem?_cons_zero, List.getElem?_cons_succ, the, h, NonDetE.throwError, ndE_throw_bindA] + rfl + +theorem exFl_szA (lab a0 a1 : Data) (r : List Data) (p : ObjRef) (sz : VMPageSize) (dev : Bool) + (asid : Asid) (v : VSpaceRef) (hz : asid ≠ 0) (h1 : ¬ a1 ≤ a0) + (h2 : a0 ≥ (1 : Word) <<< pageBitsForSize sz ∨ a1 > (1 : Word) <<< pageBitsForSize sz) : + (decodeExtPageFlushAt (Z := Unit) k lab (a0 :: a1 :: r) p sz dev (some (asid, v))).run σ = + ({(.error (.invalidArgument 0), σ)}, False) := by + have hl : ¬ (a0 :: a1 :: r).length < 2 := by simp + simp only [decodeExtPageFlushAt, NonDetE.whenE, hl, ↓reduceIte, ndE_pure_bindA, hz, NonDetE.returnOk, + List.getElem?_cons_zero, List.getElem?_cons_succ, the, h1, h2, NonDetE.throwError, ndE_throw_bindA] + rfl + +theorem exFl_bodyA (lab a0 a1 : Data) (r : List Data) (p : ObjRef) (sz : VMPageSize) (dev : Bool) + (asid : Asid) (v : VSpaceRef) (hz : asid ≠ 0) (h1 : ¬ a1 ≤ a0) + (h2 : ¬ (a0 ≥ (1 : Word) <<< pageBitsForSize sz ∨ a1 > (1 : Word) <<< pageBitsForSize sz)) : + (decodeExtPageFlushAt (Z := Unit) k lab (a0 :: a1 :: r) p sz dev (some (asid, v))).run σ = + (exFlTailA k lab a0 a1 p sz dev asid v).run σ := by + have hl : ¬ (a0 :: a1 :: r).length < 2 := by simp + simp only [decodeExtPageFlushAt, NonDetE.whenE, hl, ↓reduceIte, ndE_pure_bindA, hz, NonDetE.returnOk, + List.getElem?_cons_zero, List.getElem?_cons_succ, the, h1, h2, exFlTailA] + try rfl + +theorem exFlTail_errA {lab a0 a1 : Data} {p : ObjRef} {sz : VMPageSize} {dev : Bool} {asid : Asid} {v : VSpaceRef} + {e : LookupFailure} (h : extRootResX σ k asid = .error e) : + (exFlTailA k lab a0 a1 p sz dev asid v).run σ = ({(.error (.failedLookup false e), σ)}, False) := by + unfold exFlTailA + exact run_bind_errorA (lefo_errA (by rw [findExtRoot_runX, h])) _ + +theorem exFlTail_okA {lab a0 a1 : Data} {p : ObjRef} {sz : VMPageSize} {dev : Bool} {asid : Asid} {v : VSpaceRef} + {r : ObjRef} (h : extRootResX σ k asid = .ok r) : + (exFlTailA k lab a0 a1 p sz dev asid v).run σ = + (exFFA k lab a0 a1 p dev asid v r (if extVaOk k v then extMappedEntry σ k r p sz v else none)).run σ := by + unfold exFlTailA + rw [run_bind_okA (lefo_okA (by rw [findExtRoot_runX, h]))] + exact run_bind_okA (liftE_runA (m := NonDet.gets _) rfl) _ + +section eff +variable {lab a0 a1 : Data} {p : ObjRef} {dev : Bool} {asid : Asid} {va : VSpaceRef} {r : ObjRef} + +theorem exFF_noneA : (exFFA k lab a0 a1 p dev asid va r none).run σ = + ({(.error (.invalidCapability 0), σ)}, False) := rfl + +theorem exFF_rightsA {pos : ExtPos} {pte : PTE} + (h : labelToFlushType lab = .invalidate ∧ pte.pteRights ≠ vmReadWrite) : + (exFFA k lab a0 a1 p dev asid va r (some (pos, pte))).run σ = ({(.error .illegalOperation, σ)}, False) := by + simp only [exFFA, NonDetE.whenE, if_pos h, NonDetE.throwError, ndE_throw_bindA] + rfl + +theorem exFF_okA {pos : ExtPos} {pte : PTE} + (h : ¬ (labelToFlushType lab = .invalidate ∧ pte.pteRights ≠ vmReadWrite)) (hk : ¬ (k = .stage2 ∧ dev = true)) : + (exFFA k lab a0 a1 p dev asid va r (some (pos, pte))).run σ = + ({(.ok (.flush (.vspaceFlush (labelToFlushType lab) (va + a0) (va + a1 - 1) (addrFromPPtr p + a0) r asid)), + σ)}, False) := by + simp only [exFFA, NonDetE.whenE, if_neg h, ndE_pure_bindA, if_neg hk] + rfl + +theorem exFF_nothingA {pos : ExtPos} {pte : PTE} + (h : ¬ (labelToFlushType lab = .invalidate ∧ pte.pteRights ≠ vmReadWrite)) (hk : k = .stage2 ∧ dev = true) : + (exFFA k lab a0 a1 p dev asid va r (some (pos, pte))).run σ = ({(.ok (.flush .vspaceNothing), σ)}, False) := by + simp only [exFFA, NonDetE.whenE, if_neg h, ndE_pure_bindA, if_pos hk] + rfl + +end eff + +/-! ### The performs -/ + +theorem perfVsInv_okA (iv : VSpaceInvocation) : NdOkA MsA (performVspaceInvocation (Z := Unit) iv) := by + unfold performVspaceInvocation + cases iv with + | vspaceNothing => exact NdOkA.pure msA_refl _ + | vspaceFlush t vs ve ps sp asd => exact performFlush_okA t vs ve ps sp asd + +/-- l4v's page flush has an outcome `[]` changing only the machine state. -/ +theorem perfPgFl_memA (t : FlushType) (vs ve : VSpaceRef) (ps : Paddr) (sp : ObjRef) (asd : Asid) + (σ1 : State Unit) : + ∃ σ', (([] : List Data), σ') ∈ (performPageInvocation (Z := Unit) (.pageFlush t vs ve ps sp asd) σ1).1 ∧ + MsA σ1 σ' := by + obtain ⟨-, ⟨⟨⟨⟩, σ'⟩, hx⟩, hq⟩ := performFlush_okA t vs ve ps sp asd σ1 + exact ⟨σ', mem_bind_ndD hx rfl, hq _ hx⟩ + +/-- The Ext cache invocation has an outcome `[]` changing only the machine state. -/ +theorem perfExtFl_memA (iv : VSpaceInvocation) (σ1 : State Unit) : + ∃ σ', (([] : List Data), σ') ∈ (performExtVSpace (Z := Unit) (.flush iv) σ1).1 ∧ MsA σ1 σ' := by + obtain ⟨-, ⟨⟨⟨⟩, σ'⟩, hx⟩, hq⟩ := perfVsInv_okA iv σ1 + exact ⟨σ', mem_bind_ndD hx rfl, hq _ hx⟩ + +/-! ### Small facts -/ + +theorem pgSzA (sz : capability.FrameSize) : + ((1 : BitVec 64) <<< pageBitsForSize (frameSizeOfA sz)).toNat = 2 ^ (psA sz).val := by + rw [pbfs_eqA]; cases sz <;> decide + +theorem pgSz_iffA (sz : capability.FrameSize) (x : U64) : + (x.bv ≥ (1 : BitVec 64) <<< pageBitsForSize (frameSizeOfA sz) ↔ 2 ^ (psA sz).val ≤ x.val) ∧ + (x.bv > (1 : BitVec 64) <<< pageBitsForSize (frameSizeOfA sz) ↔ 2 ^ (psA sz).val < x.val) := by + refine ⟨?_, ?_⟩ + · rw [ge_iff_le, BitVec.le_def, pgSzA]; exact Iff.rfl + · rw [gt_iff_lt, BitVec.lt_def, pgSzA]; exact Iff.rfl + +theorem asidOf_ne0A {asid : U64} (h16 : asid.val < 65536) (h0 : asid ≠ 0#u64) : asidOfA asid ≠ 0 := fun h => by + apply h0 + apply UScalar.eq_of_val_eq + have e := congrArg BitVec.toNat h + simp only [asidOfA, BitVec.toNat_ofNat] at e + rw [Nat.mod_eq_of_lt (by omega)] at e + rw [e]; rfl + +theorem asidOf_zeroA {asid : U64} (h0 : asid = 0#u64) : asidOfA asid = 0 := by + subst h0; rfl + +/-- l4v's base check is the pointer check. -/ +theorem base_iffA (pte : PTE) (p : ObjRef) : pteBaseAddr pte = addrFromPPtr p ↔ pptrFromPte pte = p := by + unfold pptrFromPte ptrFromPAddr addrFromPPtr + constructor + · intro h; rw [h, BitVec.sub_add_cancel] + · intro h; rw [← h, BitVec.add_sub_cancel] + +theorem vmsz_iffA {l : Nat} (hl : l ≤ 2) (sz : capability.FrameSize) : + vmsizeOfLevel (lvA l) = frameSizeOfA sz ↔ 12 + 9 * l = (psA sz).val := by + interval_cases l <;> cases sz <;> decide + +end spec + +/-! ## The front adapters -/ + +section adapt +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {OS : Set U64} {m : Fl4SpecA64.McState} {σ : Fl4SpecA64.State Unit} + {s : S} {a : A} + +theorem mapD_trueA {f : vspace.FrameCap} (hmz : f.mapped = false → f.asid.val = 0) (h0 : f.asid ≠ 0#u64) : + mapDataA f.mapped f.asid f.vaddr = some (asidOfA f.asid, f.vaddr.bv) := by + cases hm : f.mapped + · exact absurd (UScalar.eq_of_val_eq (hmz hm)) h0 + · rfl + +theorem mapD_zeroA {f : vspace.FrameCap} (h0 : f.asid = 0#u64) : + ∀ asid v, mapDataA f.mapped f.asid f.vaddr = some (asid, v) → asid = 0 := fun asid v h => by + unfold mapDataA at h + split at h + · cases h; exact asidOf_zeroA h0 + · cases h + +set_option maxHeartbeats 1000000 in +/-- **FL4's front against `decodeFl4FrInvFlush`'s** (module docstring). -/ +theorem frontS1A {ar : machine.Arch A} (lbl length buf : U64) (f : vspace.FrameCap) (h16 : f.asid.val < 65536) + (hmz : f.mapped = false → f.asid.val = 0) (args : List U64) (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) + (lab : Fl4SpecA64.Data) (R : Fl4SpecA64.CapRights) (cte : Fl4SpecA64.CslotPtr) + (exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) + (hk : ∀ a0 a1 : U64, ¬ a1 ≤ a0 → f.asid ≠ 0#u64 → a0.val < 2 ^ (psA f.size).val → + a1.val ≤ 2 ^ (psA f.size).val → flTailF st ar s a lbl f (L.curW s) a0 a1 ⦃ res => VsTopPostA (L := L) AR Z OS m σ + ((Fl4Spec.NonDetE.liftME Fl4SpecA64.Fl4ArchInvocation.spec (frFlTailA lab a0.bv a1.bv (specPtr f.ptr) + (frameSizeOfA f.size) f.vaddr.bv (asidOfA f.asid))).run σ) s a res ⦄) : + vspace.decode_page_flush st ar s a lbl length f buf ⦃ res => VsTopPostA (L := L) AR Z OS m σ + ((Fl4Spec.NonDetE.liftME Fl4SpecA64.Fl4ArchInvocation.spec (Fl4SpecA64.decodeFl4FrInvFlush (Z := Unit) lab + (args.map UScalar.bv) cte (.frameCap (specPtr f.ptr) R (frameSizeOfA f.size) f.is_device + (mapDataA f.mapped f.asid f.vaddr)) exs)).run σ) s a res ⦄ := by + refine page_flush_frontA lbl length buf f args hlen hargs ?_ ?_ ?_ ?_ ?_ + · intro h + exact ⟨rfl, rfl, _, run_liftME_errA (frFl_truncA cte exs _ _ _ _ _ _ _ (by simp only [List.length_map]; omega)), + Or.inl trivial⟩ + · intro h2 h0 + obtain ⟨a0, a1, r', rfl⟩ : ∃ a0 a1 r', args = a0 :: a1 :: r' := by + match args, h2 with + | a0 :: a1 :: r', _ => exact ⟨_, _, _, rfl⟩ + exact ⟨rfl, rfl, _, run_liftME_errA (frFl_illA cte exs _ _ _ _ _ _ _ _ _ (mapD_zeroA h0)), Or.inl trivial⟩ + · rintro a0 a1 r' rfl h0 h3 + rw [mapD_trueA hmz h0] + exact ⟨rfl, rfl, _, run_liftME_errA (frFl_leA cte exs _ _ _ _ _ _ _ _ _ _ (asidOf_ne0A h16 h0) h3), Or.inl rfl⟩ + · rintro a0 a1 r' rfl h0 h3 h4 + rw [mapD_trueA hmz h0] + have hp := pgSz_iffA f.size + exact ⟨rfl, rfl, _, run_liftME_errA (frFl_szA cte exs _ _ _ _ _ _ _ _ _ _ (asidOf_ne0A h16 h0) h3 + (h4.imp (hp a0).1.2 (hp a1).2.2)), Or.inl rfl⟩ + · rintro a0 a1 r' rfl h0 h3 h4 h5 + rw [mapD_trueA hmz h0] + have hp := pgSz_iffA f.size + simp only [List.map_cons] + rw [run_liftME_congrA (frFl_bodyA cte exs _ _ _ _ _ _ _ _ _ _ (asidOf_ne0A h16 h0) h3 + (fun h => h.elim (fun h' => absurd ((hp a0).1.1 h') (by omega)) (fun h' => absurd ((hp a1).2.1 h') (by omega))))] + exact hk a0 a1 h3 h0 h4 h5 + +set_option maxHeartbeats 1000000 in +/-- **FL4's front against `decodeExtPageFlushAt`'s** (module docstring). -/ +theorem frontExA {ar : machine.Arch A} (k : Fl4SpecA64.ExtRootKind) (lbl length buf : U64) (f : vspace.FrameCap) + (h16 : f.asid.val < 65536) (hmz : f.mapped = false → f.asid.val = 0) (args : List U64) + (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) + (lab : Fl4SpecA64.Data) + (hk : ∀ a0 a1 : U64, ¬ a1 ≤ a0 → f.asid ≠ 0#u64 → a0.val < 2 ^ (psA f.size).val → + a1.val ≤ 2 ^ (psA f.size).val → flTailF st ar s a lbl f (L.curW s) a0 a1 ⦃ res => VsTopPostA (L := L) AR Z OS m σ + ((Fl4Spec.NonDetE.liftME Fl4SpecA64.Fl4ArchInvocation.extVSpace (exFlTailA k lab a0.bv a1.bv (specPtr f.ptr) + (frameSizeOfA f.size) f.is_device (asidOfA f.asid) f.vaddr.bv)).run σ) s a res ⦄) : + vspace.decode_page_flush st ar s a lbl length f buf ⦃ res => VsTopPostA (L := L) AR Z OS m σ + ((Fl4Spec.NonDetE.liftME Fl4SpecA64.Fl4ArchInvocation.extVSpace (Fl4SpecA64.decodeExtPageFlushAt (Z := Unit) k + lab (args.map UScalar.bv) (specPtr f.ptr) (frameSizeOfA f.size) f.is_device + (mapDataA f.mapped f.asid f.vaddr))).run σ) s a res ⦄ := by + refine page_flush_frontA lbl length buf f args hlen hargs ?_ ?_ ?_ ?_ ?_ + · intro h + exact ⟨rfl, rfl, _, run_liftME_errA (exFl_truncA _ _ _ _ _ _ (by simp only [List.length_map]; omega)), + Or.inl trivial⟩ + · intro h2 h0 + obtain ⟨a0, a1, r', rfl⟩ : ∃ a0 a1 r', args = a0 :: a1 :: r' := by + match args, h2 with + | a0 :: a1 :: r', _ => exact ⟨_, _, _, rfl⟩ + exact ⟨rfl, rfl, _, run_liftME_errA (exFl_illA _ _ _ _ _ _ _ _ (mapD_zeroA h0)), Or.inl trivial⟩ + · rintro a0 a1 r' rfl h0 h3 + rw [mapD_trueA hmz h0] + exact ⟨rfl, rfl, _, run_liftME_errA (exFl_leA _ _ _ _ _ _ _ _ _ (asidOf_ne0A h16 h0) h3), Or.inl rfl⟩ + · rintro a0 a1 r' rfl h0 h3 h4 + rw [mapD_trueA hmz h0] + have hp := pgSz_iffA f.size + exact ⟨rfl, rfl, _, run_liftME_errA (exFl_szA _ _ _ _ _ _ _ _ _ (asidOf_ne0A h16 h0) h3 + (h4.imp (hp a0).1.2 (hp a1).2.2)), Or.inl rfl⟩ + · rintro a0 a1 r' rfl h0 h3 h4 h5 + rw [mapD_trueA hmz h0] + have hp := pgSz_iffA f.size + simp only [List.map_cons] + rw [run_liftME_congrA (exFl_bodyA _ _ _ _ _ _ _ _ _ (asidOf_ne0A h16 h0) h3 + (fun h => h.elim (fun h' => absurd ((hp a0).1.1 h') (by omega)) (fun h' => absurd ((hp a1).2.1 h') (by omega))))] + exact hk a0 a1 h3 h0 h4 h5 + +end adapt + +/-! ## The mapping checks and the rights -/ + +section me +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {Pp Tb : Set U64} {Lv : U64 → Nat} {X Y T2 : Set U64} {Sh2 : U64 → Nat} {σ : Fl4SpecA64.State Unit} + +/-- What `mapped_entry` in l4v's tables says (module docstring). -/ +abbrev MeS1A (σ : Fl4SpecA64.State Unit) (mem : U64 → U64) (w : U64) (f : vspace.FrameCap) : + Option vspace.PtSlot → Prop + | none => ∀ sz pte, Fl4SpecA64.lookupFramePte (specPtr w) f.vaddr.bv (Fl4SpecA64.ptesOf σ) = some (sz, pte) → + ¬ (sz = frameSizeOfA f.size ∧ Fl4SpecA64.pteBaseAddr pte = Fl4SpecA64.addrFromPPtr (specPtr f.ptr)) + | some lu => ∃ pte, Fl4SpecA64.lookupFramePte (specPtr w) f.vaddr.bv (Fl4SpecA64.ptesOf σ) = + some (frameSizeOfA f.size, pte) ∧ Fl4SpecA64.pteBaseAddr pte = Fl4SpecA64.addrFromPPtr (specPtr f.ptr) ∧ + (pte.pteRights = Fl4SpecA64.vmReadWrite ↔ apA (mem lu.slot) = 1) + +set_option maxHeartbeats 2000000 in +/-- **FL4's `mapped_entry` in l4v's tables** (module docstring). -/ +theorem mapped_entry_s1A {s : S} (R : VsRelA Pp Tb Lv X Y (L.mem s) (L.ksb s) σ) {w : U64} (hw : w ∈ Tb) + (h0 : Lv w = 0) (hreach : VsReachA Pp Tb Y (L.mem s) (L.ksb s) w) (f : vspace.FrameCap) : + vspace.mapped_entry st s f w ⦃ o => MeS1A σ (L.mem s) w f o ⦄ := by + unfold vspace.mapped_entry + refine WP.spec_bind (lookup_pt_slotA R hw h0 hreach f.vaddr) fun lu ⟨p, hp, l, hl3, hsh, hlv, hsl, _, hLs, hreachP⟩ => ?_ + rw [L.read_eq, bind_ok, page_shift_eqA, bind_ok] + obtain ⟨tq, hkq, htq, hrq⟩ := R.pt p hp + have hidx := idxA_lt f.vaddr lu.shift + have hpte := ptesOf_atA hkq (ptT := Fl4SpecA64.levelType (lvA l)) + (by rw [htq, levelType_lvA hl3]; congr 1; omega) (R.al_mod hp) hidx + rw [← hsl] at hpte + have hpre : PteRelA Tb Lv (Lv p) (L.mem s lu.slot) (Fl4SpecA64.ptApply tq (idxA f.vaddr lu.shift).bv) := by + rw [hsl]; exact hrq hreachP _ hidx + generalize Fl4SpecA64.ptApply tq (idxA f.vaddr lu.shift).bv = pte at hpte hpre + have hLF := lookupFramePte_atA hLs hpte + have hm := pageMatchA hpre f.ptr + have hfound : ∀ sz' pte', Fl4SpecA64.lookupFramePte (specPtr w) f.vaddr.bv (Fl4SpecA64.ptesOf σ) = some (sz', pte') → + sz' = frameSizeOfA f.size ∧ Fl4SpecA64.pteBaseAddr pte' = Fl4SpecA64.addrFromPPtr (specPtr f.ptr) → + lu.shift = psA f.size ∧ pte.isPagePTE = true ∧ Fl4SpecA64.pptrFromPte pte = specPtr f.ptr := by + intro sz' pte' hL hc' + obtain ⟨hsz, hb⟩ := hc' + rw [hLF] at hL + by_cases hc : pte.isPagePTE = true ∧ lvA l ≤ 2 + · rw [if_pos hc] at hL + simp only [Option.some.injEq, Prod.mk.injEq] at hL + obtain ⟨rfl, rfl⟩ := hL + have hl2 : l ≤ 2 := (lvA_le2 hl3).1 hc.2 + refine ⟨UScalar.eq_of_val_eq ?_, hc.1, (base_iffA _ _).1 hb⟩ + rw [hsh]; exact (vmsz_iffA hl2 f.size).1 hsz + · rw [if_neg hc] at hL; cases hL + by_cases hs : lu.shift = psA f.size + swap + · rw [if_pos (bne_iff_ne.2 hs)] + refine (WP.spec_ok _).2 ?_ + intro sz' pte' hL hc + exact hs (hfound sz' pte' hL hc).1 + rw [if_neg (by rw [hs, bne_self_eq_false]; exact Bool.false_ne_true), + is_page_shA (k := Lv p) (by rw [hsh]; omega) (by omega), bind_ok] + by_cases hb : isPageA (L.mem s lu.slot) (Lv p) = true + swap + · rw [if_neg hb] + refine (WP.spec_ok _).2 ?_ + intro sz' pte' hL hc + obtain ⟨-, h1, h2⟩ := hfound sz' pte' hL hc + exact hb (hm.1 ⟨h1, h2⟩).1 + rw [if_pos hb, page_base_shA (k := Lv p) (by rw [hsh]; unfold shiftA; omega), bind_ok, + show layout.paddr f.ptr = ok (core.num.U64.wrapping_sub f.ptr layout.PPTR_BASE) from rfl, bind_ok] + by_cases hq : (⟨pageBaseA (L.mem s lu.slot) (Lv p)⟩ : U64) = core.num.U64.wrapping_sub f.ptr layout.PPTR_BASE + swap + · rw [if_pos (bne_iff_ne.2 hq)] + refine (WP.spec_ok _).2 ?_ + intro sz' pte' hL hc + obtain ⟨-, h1, h2⟩ := hfound sz' pte' hL hc + exact hq (hm.1 ⟨h1, h2⟩).2 + rw [if_neg (by rw [hq, bne_self_eq_false]; exact Bool.false_ne_true)] + obtain ⟨hpg, hpp⟩ := hm.2 ⟨hb, hq⟩ + have hl2 : l ≤ 2 := by + have h1 := psA_valA f.size + have h' := congrArg UScalar.val hs + rw [hsh] at h' + omega + have hsz : Fl4SpecA64.vmsizeOfLevel (lvA l) = frameSizeOfA f.size := + (vmsz_iffA hl2 f.size).2 (by rw [← hsh, hs]) + refine (WP.spec_ok _).2 ⟨pte, ?_, (base_iffA _ _).2 hpp, ?_⟩ + · rw [hLF, if_pos ⟨hpg, (lvA_le2 hl3).2 hl2⟩, hsz] + · cases pte with + | invalidPTE => simp [Fl4SpecA64.PTE.isPagePTE] at hpg + | pageTablePTE _ => simp [Fl4SpecA64.PTE.isPagePTE] at hpg + | pagePTE pa sm at' rt => + obtain ⟨-, -, -, -, -, -, -, hrt, -⟩ := hpre + show rt = _ ↔ _ + rw [hrt]; exact pteRights_rwA _ + +/-- **`extMappedEntry` from a related lookup position: FL4's three checks** (module docstring). -/ +theorem extMatchE {G : U64 → Nat → Prop} {Top : U64 → Fl4SpecA64.ExtSlot → Nat → Prop} {mem : U64 → U64} + (hG : GoodTabE G mem σ) (hTo : TopOkE G Top mem σ) {k : Fl4SpecA64.ExtRootKind} {r : Fl4SpecA64.ObjRef} + {va : U64} {W : Fl4SpecA64.ExtPos} (hW : Fl4SpecA64.extLookupSlot σ k.geom r va.bv = W) {lsh lsl : U64} + (hp : WkPostE G Top W (lsh, lsl)) (ptr : U64) (sz : capability.FrameSize) : + Fl4SpecA64.extMappedEntry σ k r (specPtr ptr) (frameSizeOfA sz) va.bv = + if lsh = psA sz ∧ isPgE (mem lsl) lsh.val = true ∧ + (⟨baseShE (mem lsl) lsh.val⟩ : U64) = core.num.U64.wrapping_sub ptr layout.PPTR_BASE + then some (W, Fl4SpecA64.ExtSlot.read σ W.slot) else none := by + obtain ⟨hWs, hsr⟩ := hp + simp only at hWs hsr + obtain ⟨-, -, hd⟩ := descAtE hG hTo hsr + rw [hWs] at hd + have hm := pageMatchE hd ptr + unfold Fl4SpecA64.extMappedEntry + dsimp only + rw [hW] + by_cases hc : lsh = psA sz ∧ isPgE (mem lsl) lsh.val = true ∧ + (⟨baseShE (mem lsl) lsh.val⟩ : U64) = core.num.U64.wrapping_sub ptr layout.PPTR_BASE + · rw [if_pos hc, if_pos ⟨by rw [hWs, hc.1, pbfs_eqA], (hm.2 hc.2).1, (hm.2 hc.2).2⟩] + · rw [if_neg hc, if_neg] + rintro ⟨h1, h2, h3⟩ + exact hc ⟨UScalar.eq_of_val_eq (by rw [← hWs, h1, pbfs_eqA]), hm.1 ⟨h2, h3⟩⟩ + +/-- A window-walk slot's rights (module docstring). -/ +theorem rightsHiE {mem : U64 → U64} {ksb : U64} (R : VsRelA Pp Tb Lv X Y mem ksb σ) {x : U64} + {sl : Fl4SpecA64.ExtSlot} {sh : Nat} (h : SlotRelE (G1ReachE Pp Tb Y Lv mem ksb) (TopHiReachE Pp Y mem ksb) x sl sh) + (hp : (Fl4SpecA64.ExtSlot.read σ sl).isPagePTE = true) : + ((Fl4SpecA64.ExtSlot.read σ sl).pteRights = Fl4SpecA64.vmReadWrite ↔ apA (mem x) = 1) := by + rcases h with ⟨t, i, ⟨⟨ht, h1, -⟩, hreach⟩, hi, rfl, rfl⟩ | ⟨y, j, hy, hact, hj, rfl, rfl, -⟩ + · obtain ⟨pt, hk, hty, he⟩ := R.pt t ht + have h3 := R.lv t ht + have hty' : Fl4SpecA64.ptType pt = .normalPtT := by rw [hty]; unfold ptTyA; rw [if_neg (by omega)] + rw [readPtE hk hty' (R.al_mod ht) hi] at hp ⊢ + have hr := he hreach i hi + generalize Fl4SpecA64.ptApply pt i.bv = pte at hp hr ⊢ + cases pte with + | invalidPTE => simp [Fl4SpecA64.PTE.isPagePTE] at hp + | pageTablePTE _ => simp [Fl4SpecA64.PTE.isPagePTE] at hp + | pagePTE pa sm at' rt => + obtain ⟨-, -, -, -, -, -, -, hrt, -⟩ := hr + show rt = _ ↔ _ + rw [hrt]; exact pteRights_rwA _ + · obtain ⟨l1, hk, he⟩ := R.high y hy + have e2 : Fl4SpecA64.ExtSlot.read σ (.root (specPtr y) j.val) = l1 ⟨j.val, hj⟩ := by + rw [readRootE hk] + show (if h : j.val < 512 then l1 ⟨j.val, h⟩ else .invalidPTE) = _ + rw [dif_pos hj] + rw [e2] at hp ⊢ + have hr := he hact j hj + generalize l1 ⟨j.val, hj⟩ = pte at hp hr ⊢ + cases pte with + | invalidPTE => simp [Fl4SpecA64.PTE.isPagePTE] at hp + | pageTablePTE _ => simp [Fl4SpecA64.PTE.isPagePTE] at hp + | pagePTE pa sm at' rt => + obtain ⟨-, -, -, -, -, -, -, hrt, -⟩ := hr + show rt = _ ↔ _ + rw [hrt]; exact pteRights_rwA _ + +theorem s2Rights_rwE (w : U64) : s2RightsE w = Fl4SpecA64.vmReadWrite ↔ apA w = 3 := by + unfold s2RightsE + by_cases h3 : apA w = 3 + · rw [if_pos h3]; exact iff_of_true rfl h3 + · rw [if_neg h3] + refine iff_of_false (fun h => ?_) h3 + have hw : Fl4SpecA64.Rights.allowWrite ∈ Fl4SpecA64.vmReadWrite := by simp [Fl4SpecA64.vmReadWrite] + rw [← h] at hw + split at hw <;> simp [Fl4SpecA64.vmReadOnly, Fl4SpecA64.vmKernelOnly] at hw + +/-- A Stage-2 walk slot's rights (module docstring). -/ +theorem rightsS2E {mem : U64 → U64} (E : ExtVsRelA Pp Tb X Y T2 Sh2 mem σ) {x : U64} + {sl : Fl4SpecA64.ExtSlot} {sh : Nat} (h : SlotRelE (G2ReachE Pp X T2 Sh2 mem σ) (TopS2ReachE Pp X σ) x sl sh) + (hp : (Fl4SpecA64.ExtSlot.read σ sl).isPagePTE = true) : + ((Fl4SpecA64.ExtSlot.read σ sl).pteRights = Fl4SpecA64.vmReadWrite ↔ apA (mem x) = 3) := by + rcases h with ⟨t, i, ⟨⟨ht, -⟩, hreach⟩, hi, rfl, rfl⟩ | ⟨r, j, hr, hact, hj, rfl, rfl, -⟩ + · obtain ⟨pt, hk, hty, he⟩ := E.s2pt t ht + rw [readPtE hk hty (Nat.mod_eq_zero_of_dvd (E.tal t ht)) hi] at hp ⊢ + have hr := (he hreach i hi).2 + generalize Fl4SpecA64.ptApply pt i.bv = pte at hp hr ⊢ + cases pte with + | invalidPTE => simp [Fl4SpecA64.PTE.isPagePTE] at hp + | pageTablePTE _ => simp [Fl4SpecA64.PTE.isPagePTE] at hp + | pagePTE pa sm at' rt => + obtain ⟨-, hrt, -⟩ := hr + show rt = _ ↔ _ + rw [hrt]; exact s2Rights_rwE _ + · obtain ⟨-, -, h1k, -, -⟩ := s2GeomNumE + have hj1 : j.val < 1024 := by omega + obtain ⟨es, hk, he⟩ := E.s2root r hr + have e2 : Fl4SpecA64.ExtSlot.read σ (.root (specPtr r) j.val) = es ⟨j.val, hj1⟩ := by + rw [readRootE hk] + show (if h : j.val < 1024 then es ⟨j.val, h⟩ else .invalidPTE) = _ + rw [dif_pos hj1] + rw [e2] at hp ⊢ + have hr' := (he hact j hj1 hj).2 + generalize es ⟨j.val, hj1⟩ = pte at hp hr' ⊢ + cases pte with + | invalidPTE => simp [Fl4SpecA64.PTE.isPagePTE] at hp + | pageTablePTE _ => simp [Fl4SpecA64.PTE.isPagePTE] at hp + | pagePTE pa sm at' rt => + obtain ⟨-, hrt, -⟩ := hr' + show rt = _ ↔ _ + rw [hrt]; exact s2Rights_rwE _ + +/-- FL4's `s2_rights_of` is read-write iff `S2AP` is 3. -/ +theorem s2_rights_ofE (e : U64) : ∃ r, ept.s2_rights_of e = ok r ∧ (r = capability.VM_READ_WRITE ↔ apA e = 3) := by + have hs : (e >>> 6#i32) ⦃ t => t.val = e.val >>> 6 ⦄ := by + step as ⟨t', h1, h2⟩ + simpa using h1 + obtain ⟨t, ht, htv⟩ := spec_ex_fd hs + have hv : (t &&& 3#u64).val = apA e := by + rw [and3_valA, htv, Nat.shiftRight_eq_div_pow]; rfl + have h13 : capability.VM_READ_ONLY ≠ capability.VM_READ_WRITE := by + unfold capability.VM_READ_ONLY capability.VM_READ_WRITE; decide + have h12 : capability.VM_KERNEL_ONLY ≠ capability.VM_READ_WRITE := by + unfold capability.VM_KERNEL_ONLY capability.VM_READ_WRITE; decide + unfold ept.s2_rights_of + rw [ht, bind_ok] + simp only [lift, bind_ok] + by_cases h1 : apA e = 3 + · rw [if_pos (UScalar.eq_of_val_eq (by rw [hv, h1]; rfl))] + exact ⟨_, rfl, iff_of_true rfl h1⟩ + · rw [if_neg (fun h => h1 (by rw [← hv, h]; rfl))] + split + · exact ⟨_, rfl, iff_of_false h13 h1⟩ + · exact ⟨_, rfl, iff_of_false h12 h1⟩ + +/-- What `mapped_entry` from an FL4 root says (module docstring). -/ +abbrev MeExA (σ : Fl4SpecA64.State Unit) (k : Fl4SpecA64.ExtRootKind) (mem : U64 → U64) (r : Fl4SpecA64.ObjRef) + (f : vspace.FrameCap) (Q : U64 → Prop) : Option vspace.PtSlot → Prop + | none => (if Fl4SpecA64.extVaOk k f.vaddr.bv then Fl4SpecA64.extMappedEntry σ k r (specPtr f.ptr) + (frameSizeOfA f.size) f.vaddr.bv else none) = none + | some lu => ∃ pos pte, (if Fl4SpecA64.extVaOk k f.vaddr.bv then Fl4SpecA64.extMappedEntry σ k r (specPtr f.ptr) + (frameSizeOfA f.size) f.vaddr.bv else none) = some (pos, pte) ∧ + (pte.pteRights = Fl4SpecA64.vmReadWrite ↔ Q (mem lu.slot)) + +set_option maxHeartbeats 4000000 in +/-- **FL4's `mapped_entry` from a high window** (module docstring). -/ +theorem mapped_entry_hiE {s : S} (R : VsRelA Pp Tb Lv X Y (L.mem s) (L.ksb s) σ) + (E : ExtVsRelA Pp Tb X Y T2 Sh2 (L.mem s) σ) {y : U64} (hy : y ∈ Y) + (hact : ActiveHighRootA (L.mem s) (L.ksb s) Pp y) (f : vspace.FrameCap) (hva : VaOkE f.vaddr) : + vspace.mapped_entry st s f y ⦃ o => MeExA σ .high (L.mem s) (specPtr y) f (fun w => apA w = 1) o ⦄ := by + unfold vspace.mapped_entry + rcases vaIdxE hva with hlo | hhi + · have hne : (39#u64 : U64) ≠ psA f.size := fun e => by + have h39 : (psA f.size).val = 39 := by rw [← e]; rfl + rcases psA_valA f.size with h | h | h <;> omega + rw [lookup_l0_zeroE (E.wlo _ hy _ hlo), bind_ok, L.read_eq, bind_ok, page_shift_eqA, bind_ok] + dsimp only + rw [if_pos (bne_iff_ne.2 hne)] + exact (WP.spec_ok _).2 (if_neg (extVaOk_loE hlo)) + obtain ⟨hb0, hb1, htab⟩ := E.wl0 _ hy + have hP0 : True → (L.mem s (peAF y (idxA f.vaddr 39#u64)) &&& 3#u64) = 3#u64 ∧ + ∀ s1 l1 : U64, s1.val = 30 → l1.val = 2 → + WkE (G1ReachE Pp Tb Y Lv (L.mem s) (L.ksb s)) (TopHiReachE Pp Y (L.mem s) (L.ksb s)) σ f.vaddr + (Fl4SpecA64.extLookupSlot σ Fl4SpecA64.ExtRootKind.high.geom (specPtr y) f.vaddr.bv) + (s1, peAF (dTabA (L.mem s (peAF y (idxA f.vaddr 39#u64)))) (idxA f.vaddr s1), l1) := fun _ => by + rw [hhi] + refine ⟨(and3_bitsA _).2 ⟨hb0, hb1⟩, fun s1 l1 hs1 hl1 => ⟨Fl4SpecA64.extTopPos Fl4SpecA64.ExtRootKind.high.geom + (specPtr y) f.vaddr.bv, by rw [hl1]; rfl, hs1.symm, Or.inr ⟨y, idxA f.vaddr s1, hy, hact, idxA_lt _ _, + by rw [htab], ?_, rfl⟩⟩⟩ + show Fl4SpecA64.ExtSlot.root _ ((f.vaddr.bv.toNat >>> 30) % (511 + 1)) = _ + rw [idxA_val, hs1, Nat.shiftRight_eq_div_pow] + rfl + obtain ⟨⟨lsh, lsl⟩, hlu, hpost⟩ := spec_ex_fd (lookup_loopE (st := st) R.goodE (topOkHiE R) f.vaddr _ True _ hP0 + 39#u64 _ 3#u64 (Or.inr ⟨trivial, rfl⟩)) + have hlk : vspace.lookup_pt_slot st s y f.vaddr = ok { slot := lsl, shift := lsh } := by + unfold vspace.lookup_pt_slot + rw [show vspace.TOP_LEVEL_SHIFT = 39#u64 by unfold vspace.TOP_LEVEL_SHIFT; rfl, entry_addr_eqA _ _ _ (by simp), + bind_ok, hlu, bind_ok] + all_goals rfl + rw [hlk, bind_ok, L.read_eq, bind_ok, page_shift_eqA, bind_ok] + dsimp only + have hme := extMatchE (k := .high) (r := specPtr y) (va := f.vaddr) R.goodE (topOkHiE R) rfl hpost f.ptr f.size + have hok := extVaOk_hiE hhi + obtain ⟨hWs, hsr⟩ := hpost + simp only at hWs hsr + obtain ⟨h12, h39, hd⟩ := descAtE R.goodE (topOkHiE R) hsr + rw [hWs] at h12 h39 hd + by_cases hs : lsh = psA f.size + swap + · rw [if_pos (bne_iff_ne.2 hs)] + exact (WP.spec_ok _).2 (by show _ = none; rw [if_pos hok, hme, if_neg (fun h => hs h.1)]) + rw [if_neg (by rw [hs, bne_self_eq_false]; exact Bool.false_ne_true), is_pageE, bind_ok] + by_cases hb : isPgE (L.mem s lsl) lsh.val = true + swap + · rw [if_neg hb] + exact (WP.spec_ok _).2 (by show _ = none; rw [if_pos hok, hme, if_neg (fun h => hb h.2.1)]) + rw [if_pos hb, page_baseE _ _ (by omega), bind_ok, + show layout.paddr f.ptr = ok (core.num.U64.wrapping_sub f.ptr layout.PPTR_BASE) from rfl, bind_ok] + by_cases hq : (⟨baseShE (L.mem s lsl) lsh.val⟩ : U64) = core.num.U64.wrapping_sub f.ptr layout.PPTR_BASE + swap + · rw [if_pos (bne_iff_ne.2 hq)] + exact (WP.spec_ok _).2 (by show _ = none; rw [if_pos hok, hme, if_neg (fun h => hq h.2.2)]) + rw [if_neg (by rw [hq, bne_self_eq_false]; exact Bool.false_ne_true)] + have hpg := ((pageMatchE hd f.ptr).2 ⟨hb, hq⟩).1 + exact (WP.spec_ok _).2 ⟨_, _, by rw [if_pos hok, hme, if_pos ⟨hs, hb, hq⟩], rightsHiE R hsr hpg⟩ + +end me + +/-! ## The arms -/ + +section arms +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} +variable {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} {ia ie : Nat} + +set_option maxHeartbeats 4000000 in +/-- **No root or a Stage-1 root** (module docstring). -/ +theorem fl_s1JA {ar : machine.Arch A} (K : ArchKeepsA ar AR) + (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) + (hTr : TreeA st OS Z.G.chains Z.G.par s) (lbl : U64) (hl : 51 ≤ lbl.val ∧ lbl.val ≤ 54) + (f : vspace.FrameCap) (hfe : f.is_ept = false) (h16 : f.asid.val < 65536) + (hk : Fl4SpecA64.asidRootKind σ (asidOfA f.asid) = none ∨ + Fl4SpecA64.asidRootKind σ (asidOfA f.asid) = some .stage1) (a0 a1 : U64) (h3 : ¬ a1 ≤ a0) : + flTailF st ar s a lbl f (L.curW s) a0 a1 ⦃ res => VsTopPostA (L := L) AR Z OS m σ + ((Fl4Spec.NonDetE.liftME Fl4SpecA64.Fl4ArchInvocation.spec (frFlTailA (Fl4SpecA64.specLabelWord (labNA lbl.val)) + a0.bv a1.bv (specPtr f.ptr) (frameSizeOfA f.size) f.vaddr.bv (asidOfA f.asid))).run σ) s a res ⦄ := by + have R := E.j.1.vs + have hX := E.j.2.2.1.obj + have hS1 : ∀ r, Fl4SpecA64.vspaceForAsid (asidOfA f.asid) σ = some r → Fl4SpecA64.rootKindAt σ r = .stage1 := + fun r hv => by + rcases hk with h | h <;> rw [asidRootKind_ofE hv] at h + · cases h + · exact Option.some.inj h + unfold flTailF + rw [if_neg (by rw [hfe]; exact Bool.false_ne_true)] + obtain ⟨r, hr, hrel, herr⟩ := spec_ex_fd (WP.spec_and (find_asid_rawA (st := st) R h16) + (find_asid_raw_errA (st := st) s h16)) + rcases r with w | e + · obtain ⟨hTag, hwT, hw0, hv⟩ := raw_s1A R hX hS1 hrel + rw [frame_root_s1A hr hTag, bind_ok] + dsimp only + have hrun : (Fl4SpecA64.lookupErrorOnFailure (Z := Unit) false (Fl4SpecA64.findVspaceForAsid (asidOfA f.asid))).run σ = + ({(.ok (specPtr w), σ)}, False) := lefo_okA (findVs_runA hv (hS1 _ hv)) + have hT2 := frFlTail_okA (lab := Fl4SpecA64.specLabelWord (labNA lbl.val)) (a0 := a0.bv) (a1 := a1.bv) + (p := specPtr f.ptr) (pgsz := frameSizeOfA f.size) (v := f.vaddr.bv) hrun + refine WP.spec_bind (mapped_entry_s1A R hwT hw0 (vsReachA_of_vspaceForAsid R hX hwT hv) f) fun o ho => ?_ + rcases o with _ | lu + · simp only [error.invalid_cap, bind_ok, WP.spec_ok] + refine ⟨rfl, rfl, .invalidCapability 0, run_liftME_errA (hT2.trans ?_), Or.inr ⟨0#u64, rfl, rfl⟩⟩ + cases hL : Fl4SpecA64.lookupFramePte (specPtr w) f.vaddr.bv (Fl4SpecA64.ptesOf σ) with + | none => exact frFF_noneA + | some x => + obtain ⟨sz', pte'⟩ := x + exact frFF_misA (ho sz' pte' hL) + obtain ⟨pte, hL, hbase, hrw⟩ := ho + rw [hL] at hT2 + dsimp only + rw [is_inv_frA hl, bind_ok] + obtain ⟨x, hkx, hst, hset, J1, T1, C1⟩ := restart_onlyJA E RS hTr + obtain ⟨c, hc⟩ := page_cache_op_okA lbl + obtain ⟨d, hd, -⟩ := spec_ex_fd (U64.sub_spec (x := a1) (y := a0) (by scalar_tac)) + have hinvI := (labNA_frFlA lbl.val hl.1 hl.2).2.1 + obtain ⟨σ', hp', hms⟩ := perfPgFl_memA (Fl4SpecA64.labelToFlushType (Fl4SpecA64.specLabelWord (labNA lbl.val))) + (f.vaddr.bv + a0.bv) (f.vaddr.bv + a1.bv - 1) (Fl4SpecA64.addrFromPPtr (specPtr f.ptr) + a0.bv) (specPtr w) + (asidOfA f.asid) (stUpdA σ σ.curThread x .restart) + have hTC : CarryA Z (L.mem s) σ → CarryA Z (L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART)) σ' := + fun C => (C1 C).of_tyA (TyKeepA.of_kheap (by obtain ⟨ms, rfl⟩ := hms; rfl)) + have hpa : layout.paddr f.ptr = ok (core.num.U64.wrapping_sub f.ptr layout.PPTR_BASE) := rfl + by_cases hinv : lbl.val = 52 + · rw [if_pos (decide_eq_true hinv)] + obtain ⟨r, hr', hriff⟩ := rights_ofA (L.mem s lu.slot) + rw [L.read_eq, bind_ok, hr', bind_ok] + by_cases hrr : r = capability.VM_READ_WRITE + · rw [if_neg (show ¬ ((r != capability.VM_READ_WRITE) = true) by + rw [hrr, bne_self_eq_false]; exact Bool.false_ne_true), hst, bind_ok, hpa, bind_ok] + simp only [lift, bind_ok, hc, hd] + exact WP.spec_bind (K.cacheOp a Z.hw _ _ _ _ _ _ J1.1.ar) fun a' ha' => (WP.spec_ok _).2 + ⟨_, okD_memA (run_liftME_okA (hT2.trans (frFF_okA ⟨rfl, hbase⟩ + (fun h => h.2 (hrw.2 (hriff.1 hrr)))))), _, hset, σ', [], mem_archPgA hp', Z, rfl, rfl, + (J1.of_arA ha').of_msA hms, T1, hTC⟩ + · rw [if_pos (bne_iff_ne.2 hrr)] + simp only [error.illegal, bind_ok, WP.spec_ok] + exact ⟨rfl, rfl, .illegalOperation, run_liftME_errA (hT2.trans (frFF_rightsA ⟨rfl, hbase⟩ + ⟨hinvI.2 hinv, fun h => hrr (hriff.2 (hrw.1 h))⟩)), Or.inl trivial⟩ + · rw [if_neg (by simpa using hinv), hst, bind_ok, hpa, bind_ok] + simp only [lift, bind_ok, hc, hd] + exact WP.spec_bind (K.cacheOp a Z.hw _ _ _ _ _ _ J1.1.ar) fun a' ha' => (WP.spec_ok _).2 + ⟨_, okD_memA (run_liftME_okA (hT2.trans (frFF_okA ⟨rfl, hbase⟩ (fun h => hinv (hinvI.1 h.1))))), _, hset, + σ', [], mem_archPgA hp', Z, rfl, rfl, (J1.of_arA ha').of_msA hms, T1, hTC⟩ + · rw [frame_root_errA hr, bind_ok] + dsimp only + obtain rfl := herr e rfl + have hn : Fl4SpecA64.vspaceForAsid (asidOfA f.asid) σ = none := hrel + exact (WP.spec_ok _).2 ⟨rfl, rfl, .failedLookup false .invalidRoot, + run_liftME_errA (frFlTail_errA (findVs_noneA hn)), Or.inl ⟨rfl, trivial⟩⟩ + +set_option maxHeartbeats 4000000 in +/-- **A high window** (module docstring). -/ +theorem fl_hiJA {ar : machine.Arch A} (K : ArchKeepsA ar AR) + (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) + (hTr : TreeA st OS Z.G.chains Z.G.par s) (lbl : U64) (hl : 51 ≤ lbl.val ∧ lbl.val ≤ 54) + (f : vspace.FrameCap) (hfe : f.is_ept = false) (h16 : f.asid.val < 65536) + (hk : Fl4SpecA64.asidRootKind σ (asidOfA f.asid) = some .high) (hva : VaOkE f.vaddr) (a0 a1 : U64) + (h3 : ¬ a1 ≤ a0) : + flTailF st ar s a lbl f (L.curW s) a0 a1 ⦃ res => VsTopPostA (L := L) AR Z OS m σ + ((Fl4Spec.NonDetE.liftME Fl4SpecA64.Fl4ArchInvocation.extVSpace (exFlTailA .high + (Fl4SpecA64.specLabelWord (labNA lbl.val)) a0.bv a1.bv (specPtr f.ptr) (frameSizeOfA f.size) f.is_device + (asidOfA f.asid) f.vaddr.bv)).run σ) s a res ⦄ := by + have R := E.j.1.vs + have EX := E.j.1.ext + have hX := E.j.2.2.1.obj + unfold flTailF + rw [if_neg (by rw [hfe]; exact Bool.false_ne_true)] + obtain ⟨r, hr, hrel⟩ := spec_ex_fd (find_asid_rawA (st := st) R h16) + obtain ⟨y0, hv0, hk0⟩ := asidRootKind_someX hk + rcases r with w | e + swap + · have hn : Fl4SpecA64.vspaceForAsid (asidOfA f.asid) σ = none := hrel + rw [hn] at hv0; cases hv0 + obtain ⟨hv, hkind⟩ := rawKindE R hX hrel + have hv' : Fl4SpecA64.vspaceForAsid (asidOfA f.asid) σ = some (specPtr (untagA w)) := hv + rw [hv'] at hv0 + cases hv0 + rcases hkind with ⟨-, h⟩ | ⟨-, -, h⟩ | ⟨ht2, hy, -⟩ + · rw [h] at hk0; cases hk0 + · rw [h] at hk0; cases hk0 + rw [frame_root_hiA hr ht2, bind_ok] + dsimp only + have hT2 := exFlTail_okA (k := .high) (lab := Fl4SpecA64.specLabelWord (labNA lbl.val)) (a0 := a0.bv) + (a1 := a1.bv) (p := specPtr f.ptr) (sz := frameSizeOfA f.size) (dev := f.is_device) (v := f.vaddr.bv) + (extRootResX_okX hv' hk0) + refine WP.spec_bind (mapped_entry_hiE R EX hy (activeHighRootA_of_vspaceForAsid R hX hy hv') f hva) fun o ho => ?_ + rcases o with _ | lu + · simp only [error.invalid_cap, bind_ok, WP.spec_ok] + exact ⟨rfl, rfl, .invalidCapability 0, run_liftME_errA (hT2.trans (by rw [show _ = none from ho]; exact exFF_noneA)), + Or.inr ⟨0#u64, rfl, rfl⟩⟩ + obtain ⟨pos, pte, hme, hrw⟩ := ho + rw [hme] at hT2 + dsimp only + rw [is_inv_frA hl, bind_ok] + obtain ⟨x, hkx, hst, hset, J1, T1, C1⟩ := restart_onlyJA E RS hTr + obtain ⟨c, hc⟩ := page_cache_op_okA lbl + obtain ⟨d, hd, -⟩ := spec_ex_fd (U64.sub_spec (x := a1) (y := a0) (by scalar_tac)) + have hinvI := (labNA_frFlA lbl.val hl.1 hl.2).2.1 + obtain ⟨σ', hp', hms⟩ := perfExtFl_memA (.vspaceFlush (Fl4SpecA64.labelToFlushType + (Fl4SpecA64.specLabelWord (labNA lbl.val))) (f.vaddr.bv + a0.bv) (f.vaddr.bv + a1.bv - 1) + (Fl4SpecA64.addrFromPPtr (specPtr f.ptr) + a0.bv) (specPtr (untagA w)) (asidOfA f.asid)) + (stUpdA σ σ.curThread x .restart) + have hTC : CarryA Z (L.mem s) σ → CarryA Z (L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART)) σ' := + fun C => (C1 C).of_tyA (TyKeepA.of_kheap (by obtain ⟨ms, rfl⟩ := hms; rfl)) + have hpa : layout.paddr f.ptr = ok (core.num.U64.wrapping_sub f.ptr layout.PPTR_BASE) := rfl + have hk2 : ¬ (Fl4SpecA64.ExtRootKind.high = .stage2 ∧ f.is_device = true) := fun h => by cases h.1 + by_cases hinv : lbl.val = 52 + · rw [if_pos (decide_eq_true hinv)] + obtain ⟨r, hr', hriff⟩ := rights_ofA (L.mem s lu.slot) + rw [L.read_eq, bind_ok, hr', bind_ok] + by_cases hrr : r = capability.VM_READ_WRITE + · rw [if_neg (show ¬ ((r != capability.VM_READ_WRITE) = true) by + rw [hrr, bne_self_eq_false]; exact Bool.false_ne_true), hst, bind_ok, hpa, bind_ok] + simp only [lift, bind_ok, hc, hd] + exact WP.spec_bind (K.cacheOp a Z.hw _ _ _ _ _ _ J1.1.ar) fun a' ha' => (WP.spec_ok _).2 + ⟨_, okD_memA (run_liftME_okA (hT2.trans (exFF_okA (fun h => h.2 (hrw.2 (hriff.1 hrr))) hk2))), _, hset, σ', + [], mem_archExtA hp', Z, rfl, rfl, (J1.of_arA ha').of_msA hms, T1, hTC⟩ + · rw [if_pos (bne_iff_ne.2 hrr)] + simp only [error.illegal, bind_ok, WP.spec_ok] + exact ⟨rfl, rfl, .illegalOperation, run_liftME_errA (hT2.trans (exFF_rightsA + ⟨hinvI.2 hinv, fun h => hrr (hriff.2 (hrw.1 h))⟩)), Or.inl trivial⟩ + · rw [if_neg (by simpa using hinv), hst, bind_ok, hpa, bind_ok] + simp only [lift, bind_ok, hc, hd] + exact WP.spec_bind (K.cacheOp a Z.hw _ _ _ _ _ _ J1.1.ar) fun a' ha' => (WP.spec_ok _).2 + ⟨_, okD_memA (run_liftME_okA (hT2.trans (exFF_okA (fun h => hinv (hinvI.1 h.1)) hk2))), _, hset, + σ', [], mem_archExtA hp', Z, rfl, rfl, (J1.of_arA ha').of_msA hms, T1, hTC⟩ + +/-- **A plain frame on a Stage-2 ASID** (module docstring). -/ +theorem fl_hiS2JA {ar : machine.Arch A} (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) (lbl : U64) + (f : vspace.FrameCap) (hfe : f.is_ept = false) (h16 : f.asid.val < 65536) + (hk : Fl4SpecA64.asidRootKind σ (asidOfA f.asid) = some .stage2) (a0 a1 : U64) : + flTailF st ar s a lbl f (L.curW s) a0 a1 ⦃ res => VsTopPostA (L := L) AR Z OS m σ + ((Fl4Spec.NonDetE.liftME Fl4SpecA64.Fl4ArchInvocation.extVSpace (exFlTailA .high + (Fl4SpecA64.specLabelWord (labNA lbl.val)) a0.bv a1.bv (specPtr f.ptr) (frameSizeOfA f.size) f.is_device + (asidOfA f.asid) f.vaddr.bv)).run σ) s a res ⦄ := by + have R := E.j.1.vs + have hX := E.j.2.2.1.obj + unfold flTailF + rw [if_neg (by rw [hfe]; exact Bool.false_ne_true)] + obtain ⟨r, hr, hrel⟩ := spec_ex_fd (find_asid_rawA (st := st) R h16) + obtain ⟨y0, hv0, hk0⟩ := asidRootKind_someX hk + rcases r with w | e + swap + · have hn : Fl4SpecA64.vspaceForAsid (asidOfA f.asid) σ = none := hrel + rw [hn] at hv0; cases hv0 + obtain ⟨hv, hkind⟩ := rawKindE R hX hrel + have hv' : Fl4SpecA64.vspaceForAsid (asidOfA f.asid) σ = some (specPtr (untagA w)) := hv + rw [hv'] at hv0 + cases hv0 + rcases hkind with ⟨-, h⟩ | ⟨ht1, -, -⟩ | ⟨-, -, h⟩ + · rw [h] at hk0; cases hk0 + · rw [frame_root_s2A hr ht1, bind_ok] + dsimp only + exact (WP.spec_ok _).2 ⟨rfl, rfl, .failedLookup false .invalidRoot, + run_liftME_errA (exFlTail_errA (extRootResX_errX hv' (by rw [hk0]; exact fun h => by cases h))), + Or.inl ⟨rfl, trivial⟩⟩ + · rw [h] at hk0; cases hk0 + +set_option maxHeartbeats 4000000 in +/-- **A Stage-2 frame** (module docstring). -/ +theorem fl_s2JA {ar : machine.Arch A} (K : ArchKeepsA ar AR) + (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) + (hTr : TreeA st OS Z.G.chains Z.G.par s) (hIpa : S2IpaE ar a) (lbl : U64) (hl : 51 ≤ lbl.val ∧ lbl.val ≤ 54) + (f : vspace.FrameCap) (hfe : f.is_ept = true) (h16 : f.asid.val < 65536) (a0 a1 : U64) (h3 : ¬ a1 ≤ a0) : + flTailF st ar s a lbl f (L.curW s) a0 a1 ⦃ res => VsTopPostA (L := L) AR Z OS m σ + ((Fl4Spec.NonDetE.liftME Fl4SpecA64.Fl4ArchInvocation.extVSpace (exFlTailA .stage2 + (Fl4SpecA64.specLabelWord (labNA lbl.val)) a0.bv a1.bv (specPtr f.ptr) (frameSizeOfA f.size) f.is_device + (asidOfA f.asid) f.vaddr.bv)).run σ) s a res ⦄ := by + have R := E.j.1.vs + have EX := E.j.1.ext + have hX := E.j.2.2.1.obj + unfold flTailF + rw [if_pos hfe] + obtain ⟨c, hc⟩ := page_cache_op_okA lbl + rw [hc, bind_ok, is_inv_frA hl, bind_ok] + obtain ⟨r, hr, ⟨hok, herr⟩, herr2⟩ := spec_ex_fd (WP.spec_and (ept_findE (st := st) R hX h16) + (ept_find_errE (st := st) (s := s) h16)) + unfold ept.flush_frame + rw [hr, bind_ok] + rcases r with w | e + swap + · obtain rfl := herr2 e rfl + simp only [core.result.Result.map_err, ept.flush_frame.closure.Insts.CoreOpsFunctionFnOnceTupleLookupFaultExc, + ept.flush_frame.closure.Insts.CoreOpsFunctionFnOnceTupleLookupFaultExc.call_once, bind_ok, + core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame.from, + bind_tc_ok, WP.spec_ok] + exact ⟨rfl, rfl, .failedLookup false .invalidRoot, run_liftME_errA (exFlTail_errA (herr _ rfl)), + Or.inl ⟨rfl, trivial⟩⟩ + simp only [core.result.Result.map_err, bind_ok, core.result.Result.Insts.CoreOpsTry.branch] + obtain ⟨hwX, hres⟩ := hok w rfl + obtain ⟨n, hn, hnv⟩ := hIpa + obtain ⟨g, kk, hg, hkk, hmk, h64, hts, hrem, htop⟩ := s2StartE hnv + rw [hn, bind_ok] + obtain ⟨hj, hsl⟩ := htop f.vaddr w + obtain ⟨⟨lsh, lsl⟩, hlu, hpost⟩ := spec_ex_fd (lookup_loopE (st := st) EX.goodE (topOkS2E EX) f.vaddr + (Fl4SpecA64.extLookupSlot σ Fl4SpecA64.s2Geom (specPtr w) f.vaddr.bv) False 0#u64 (fun h => h.elim) g.top_shift + (peAF w (idxME f.vaddr g.top_shift kk)) (core.num.U64.wrapping_sub g.levels 1#u64) + (Or.inl ⟨Fl4SpecA64.extTopPos Fl4SpecA64.s2Geom (specPtr w) f.vaddr.bv, by rw [hrem]; rfl, hts.symm, + Or.inr ⟨w, _, hwX, activeS2RootA_of_vspaceForAsid R hwX (extRootResX_okE hres).1, hj, rfl, hsl, rfl⟩⟩)) + have hlk : ept.lookup_slot st s w f.vaddr n = ok { slot := lsl, shift := lsh } := by + unfold ept.lookup_slot + rw [hg, bind_ok, ept_entry_addrE _ _ _ _ h64 hmk, bind_ok] + simp only [lift, bind_ok] + rw [ept_lookup_loopE, hlu, bind_ok] + all_goals rfl + rw [hlk, bind_ok, L.read_eq, bind_ok, frameBitsE, bind_ok] + dsimp only + have hT2 := exFlTail_okA (k := .stage2) (lab := Fl4SpecA64.specLabelWord (labNA lbl.val)) (a0 := a0.bv) + (a1 := a1.bv) (p := specPtr f.ptr) (sz := frameSizeOfA f.size) (dev := f.is_device) (v := f.vaddr.bv) hres + have hok2 : Fl4SpecA64.extVaOk .stage2 f.vaddr.bv := Or.inl (fun h => by cases h) + have hme := extMatchE (k := .stage2) (r := specPtr w) (va := f.vaddr) EX.goodE (topOkS2E EX) rfl hpost f.ptr f.size + rw [if_pos hok2, hme] at hT2 + obtain ⟨hWs, hsr⟩ := hpost + simp only at hWs hsr + obtain ⟨h12, h39, hd⟩ := descAtE EX.goodE (topOkS2E EX) hsr + rw [hWs] at h12 h39 hd + by_cases hs : lsh = psA f.size + swap + · rw [if_pos (bne_iff_ne.2 hs)] + simp only [error.invalid_cap, bind_ok, WP.spec_ok] + exact ⟨rfl, rfl, .invalidCapability 0, + run_liftME_errA (hT2.trans (by rw [if_neg (fun h => hs h.1)]; exact exFF_noneA)), Or.inr ⟨0#u64, rfl, rfl⟩⟩ + rw [if_neg (by rw [hs, bne_self_eq_false]; exact Bool.false_ne_true), ept_is_leafE, is_pageE, bind_ok] + by_cases hb : isPgE (L.mem s lsl) lsh.val = true + swap + · rw [if_neg hb] + simp only [error.invalid_cap, bind_ok, WP.spec_ok] + exact ⟨rfl, rfl, .invalidCapability 0, + run_liftME_errA (hT2.trans (by rw [if_neg (fun h => hb h.2.1)]; exact exFF_noneA)), Or.inr ⟨0#u64, rfl, rfl⟩⟩ + rw [if_pos hb, ept_leaf_baseE, page_baseE _ _ (by omega), bind_ok, + show layout.paddr f.ptr = ok (core.num.U64.wrapping_sub f.ptr layout.PPTR_BASE) from rfl, bind_ok] + by_cases hq : (⟨baseShE (L.mem s lsl) lsh.val⟩ : U64) = core.num.U64.wrapping_sub f.ptr layout.PPTR_BASE + swap + · rw [if_pos (bne_iff_ne.2 hq)] + simp only [error.invalid_cap, bind_ok, WP.spec_ok] + exact ⟨rfl, rfl, .invalidCapability 0, + run_liftME_errA (hT2.trans (by rw [if_neg (fun h => hq h.2.2)]; exact exFF_noneA)), Or.inr ⟨0#u64, rfl, rfl⟩⟩ + rw [if_neg (by rw [hq, bne_self_eq_false]; exact Bool.false_ne_true)] + rw [if_pos ⟨hs, hb, hq⟩] at hT2 + have hpg := ((pageMatchE hd f.ptr).2 ⟨hb, hq⟩).1 + have hrw := rightsS2E EX hsr hpg + obtain ⟨x, hkx, hst, hset, J1, T1, C1⟩ := restart_onlyJA E RS hTr + obtain ⟨d, hd', -⟩ := spec_ex_fd (U64.sub_spec (x := a1) (y := a0) (by scalar_tac)) + have hinvI := (labNA_frFlA lbl.val hl.1 hl.2).2.1 + have hTC : ∀ σ', MsA (stUpdA σ σ.curThread x .restart) σ' → CarryA Z (L.mem s) σ → + CarryA Z (L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART)) σ' := + fun σ' hms C => (C1 C).of_tyA (TyKeepA.of_kheap (by obtain ⟨ms, rfl⟩ := hms; rfl)) + obtain ⟨σn, hpn, hmsn⟩ := perfExtFl_memA .vspaceNothing (stUpdA σ σ.curThread x .restart) + obtain ⟨σf, hpf, hmsf⟩ := perfExtFl_memA (.vspaceFlush (Fl4SpecA64.labelToFlushType + (Fl4SpecA64.specLabelWord (labNA lbl.val))) (f.vaddr.bv + a0.bv) (f.vaddr.bv + a1.bv - 1) + (Fl4SpecA64.addrFromPPtr (specPtr f.ptr) + a0.bv) (specPtr w) (asidOfA f.asid)) + (stUpdA σ σ.curThread x .restart) + by_cases hinv : lbl.val = 52 + · rw [if_pos (decide_eq_true hinv)] + obtain ⟨r, hr', hriff⟩ := s2_rights_ofE (L.mem s lsl) + rw [hr', bind_ok] + by_cases hrr : r = capability.VM_READ_WRITE + · have HR : ¬ (Fl4SpecA64.labelToFlushType (Fl4SpecA64.specLabelWord (labNA lbl.val)) = .invalidate ∧ + (Fl4SpecA64.ExtSlot.read σ (Fl4SpecA64.extLookupSlot σ Fl4SpecA64.s2Geom (specPtr w) + f.vaddr.bv).slot).pteRights ≠ Fl4SpecA64.vmReadWrite) := fun h => h.2 (hrw.2 (hriff.1 hrr)) + rw [if_neg (show ¬ ((r != capability.VM_READ_WRITE) = true) by + rw [hrr, bne_self_eq_false]; exact Bool.false_ne_true), cur_thread_ok, bind_ok, hst, bind_ok] + by_cases hdev : f.is_device = true + · rw [if_pos hdev] + exact (WP.spec_ok _).2 ⟨_, okD_memA (run_liftME_okA (hT2.trans (exFF_nothingA HR ⟨rfl, hdev⟩))), _, hset, + σn, [], mem_archExtA hpn, Z, rfl, rfl, J1.of_msA hmsn, T1, hTC σn hmsn⟩ + · rw [if_neg hdev] + simp only [lift, bind_ok, hd'] + exact WP.spec_bind (K.cacheOp a Z.hw _ _ _ _ _ _ J1.1.ar) fun a' ha' => (WP.spec_ok _).2 + ⟨_, okD_memA (run_liftME_okA (hT2.trans (exFF_okA HR (fun h => hdev h.2)))), _, hset, σf, [], + mem_archExtA hpf, Z, rfl, rfl, (J1.of_arA ha').of_msA hmsf, T1, hTC σf hmsf⟩ + · rw [if_pos (bne_iff_ne.2 hrr)] + simp only [error.illegal, bind_ok, WP.spec_ok] + exact ⟨rfl, rfl, .illegalOperation, run_liftME_errA (hT2.trans (exFF_rightsA + ⟨hinvI.2 hinv, fun h => hrr (hriff.2 (hrw.1 h))⟩)), Or.inl trivial⟩ + · have HR : ¬ (Fl4SpecA64.labelToFlushType (Fl4SpecA64.specLabelWord (labNA lbl.val)) = .invalidate ∧ + (Fl4SpecA64.ExtSlot.read σ (Fl4SpecA64.extLookupSlot σ Fl4SpecA64.s2Geom (specPtr w) + f.vaddr.bv).slot).pteRights ≠ Fl4SpecA64.vmReadWrite) := fun h => hinv (hinvI.1 h.1) + rw [if_neg (by simpa using hinv), cur_thread_ok, bind_ok, hst, bind_ok] + by_cases hdev : f.is_device = true + · rw [if_pos hdev] + exact (WP.spec_ok _).2 ⟨_, okD_memA (run_liftME_okA (hT2.trans (exFF_nothingA HR ⟨rfl, hdev⟩))), _, hset, + σn, [], mem_archExtA hpn, Z, rfl, rfl, J1.of_msA hmsn, T1, hTC σn hmsn⟩ + · rw [if_neg hdev] + simp only [lift, bind_ok, hd'] + exact WP.spec_bind (K.cacheOp a Z.hw _ _ _ _ _ _ J1.1.ar) fun a' ha' => (WP.spec_ok _).2 + ⟨_, okD_memA (run_liftME_okA (hT2.trans (exFF_okA HR (fun h => hdev h.2)))), _, hset, σf, [], + mem_archExtA hpf, Z, rfl, rfl, (J1.of_arA ha').of_msA hmsf, T1, hTC σf hmsf⟩ + +set_option maxHeartbeats 2000000 in +/-- **`decode_page_flush` against the dispatcher at the frame cache labels** (module docstring). -/ +theorem fr_flushJA {ar : machine.Arch A} (K : ArchKeepsA ar AR) + (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) + (hTr : TreeA st OS Z.G.chains Z.G.par s) (hIpa : S2IpaE ar a) (cte : Fl4SpecA64.CslotPtr) + (hab : AbsSlotA Z.th cte) (fp rt asid va : U64) (sz : capability.FrameSize) (dev mp e : Bool) + (hcap : capAtA (L.mem s) (cslotAddrA cte) = .Frame fp sz rt dev mp e asid va) + (lbl : U64) (hl : 51 ≤ lbl.val ∧ lbl.val ≤ 54) (length buf : U64) (args : List U64) + (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) + (calling : Bool) (xSlot : Fl4SpecA64.CapRef) (exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) : + vspace.decode_page_flush st ar s a lbl length + { ptr := fp, size := sz, rights := rt, is_device := dev, mapped := mp, is_ept := e, asid := asid, vaddr := va } + buf ⦃ res => VsTopPostA (L := L) AR Z OS m σ ((Fl4SpecA64.fl4ArchDecodeInvocation (Z := Unit) calling + (labNA lbl.val) (args.map UScalar.bv) xSlot cte + (absFrameE e (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev (mapDataA mp asid va)) exs).run σ) s a res ⦄ := by + obtain ⟨c0, -, hrel0⟩ := StateRelA.getAbsA E.j.1.st hab + rw [hcap] at hrel0 + obtain ⟨⟨-, -, ⟨h16a, hmz, _, _⟩, -⟩, -⟩ := hrel0 + have h16 : asid.val < 65536 := lt_of_lt_of_eq h16a (by norm_num) + obtain ⟨hfl, -, hd, ht⟩ := labNA_frFlA lbl.val hl.1 hl.2 + have hf : Fl4SpecA64.isPageFlushLabel (Fl4SpecA64.invocationType (Fl4SpecA64.specLabelWord (labNA lbl.val))) = + true := by rw [invType_specA]; exact hfl + cases e with + | true => + rw [show absFrameE true (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev (mapDataA mp asid va) = + .ext (.stage2FrameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev (mapDataA mp asid va)) from rfl, + dec_frFlS2A calling xSlot cte exs _ _ hf hd ht] + exact frontExA (f := frCapA fp sz rt dev mp true asid va) .stage2 lbl length buf h16 hmz args hlen hargs _ fun a0 a1 h3 _ _ _ => + fl_s2JA K E RS hTr hIpa lbl hl _ rfl h16 a0 a1 h3 + | false => + rw [show absFrameE false (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev (mapDataA mp asid va) = + .frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev (mapDataA mp asid va) from rfl] + cases mp with + | false => + rw [dec_frFlS1A calling xSlot cte exs _ _ hf hd ht _ _ _ _ _ (fun _ _ h => by cases h)] + exact frontS1A (f := frCapA fp sz rt dev false false asid va) lbl length buf h16 hmz args hlen hargs _ _ cte exs + fun a0 a1 _ h0 _ _ => absurd (UScalar.eq_of_val_eq (hmz rfl)) h0 + | true => + have hmd : mapDataA true asid va = some (asidOfA asid, va.bv) := rfl + rcases hk : Fl4SpecA64.asidRootKind σ (asidOfA asid) with _ | k + · rw [dec_frFlS1A calling xSlot cte exs _ _ hf hd ht _ _ _ _ _ + (fun _ _ h => by rw [hmd] at h; cases h; exact Or.inl hk)] + exact frontS1A (f := frCapA fp sz rt dev true false asid va) lbl length buf h16 hmz args hlen hargs _ _ cte exs + fun a0 a1 h3 _ _ _ => fl_s1JA K E RS hTr lbl hl _ rfl h16 (Or.inl hk) a0 a1 h3 + cases k with + | stage1 => + rw [dec_frFlS1A calling xSlot cte exs _ _ hf hd ht _ _ _ _ _ + (fun _ _ h => by rw [hmd] at h; cases h; exact Or.inr hk)] + exact frontS1A (f := frCapA fp sz rt dev true false asid va) lbl length buf h16 hmz args hlen hargs _ _ cte exs + fun a0 a1 h3 _ _ _ => fl_s1JA K E RS hTr lbl hl _ rfl h16 (Or.inr hk) a0 a1 h3 + | stage2 => + rw [dec_frFlHiA calling xSlot cte exs _ _ hf hd ht _ _ _ _ _ _ _ hmd hk (fun h => by cases h)] + exact frontExA (f := frCapA fp sz rt dev true false asid va) .high lbl length buf h16 hmz args hlen hargs _ + fun a0 a1 _ _ _ _ => fl_hiS2JA E lbl _ rfl h16 hk a0 a1 + | high => + have hva : VaOkE va := E.j.1.vaOkE (hab.isSlot E.j.1.place) hcap + rw [dec_frFlHiA calling xSlot cte exs _ _ hf hd ht _ _ _ _ _ _ _ hmd hk (fun h => by cases h)] + exact frontExA (f := frCapA fp sz rt dev true false asid va) .high lbl length buf h16 hmz args hlen hargs _ + fun a0 a1 h3 _ _ _ => fl_hiJA K E RS hTr lbl hl _ rfl h16 hk hva a0 a1 h3 + +set_option maxHeartbeats 4000000 in +/-- **The frame cap-kind top** (module docstring; ruling 01:37Z item 2, S3 gen 34 ruling 02:48Z item 1, moved from +`P3/VsTopFrA`; S3 gen 39d: Stage-2, `ArmHighVSpace`, and `is_ept` frames lifted via `page_map_all_jA`, retiring +`NoExtXA` and `e = false` in `hMap`): `decode_invocation` on a `Cap::Frame` at `ARMPageMap`, `ARMPageUnmap`, the cache +labels 51..54 (`fr_flushJA`) and every label outside 49..57, against `fl4ArchDecodeInvocation` at FL4's label, with +`VsTopPostA`; the case selectors `hLbl` (not 55..57) and `hMap` (`FrPlA`). -/ +theorem decInv_frame_jA {ar : machine.Arch A} (K : ArchKeepsA ar AR) + (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) + (hTO : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s q) (hEO : ExtTabOffA (L := L) Z m s) + (hK : Aligned (L.ksb s)) (hT : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s)) (hIpa : S2IpaE ar a) + (hTr : TreeA st OS Z.G.chains Z.G.par s) (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) + (cte : Fl4SpecA64.CslotPtr) (hab : AbsSlotA Z.th cte) (hos : cslotAddrA cte ∈ OS) + (fp rt asid va : U64) (sz : capability.FrameSize) (dev mp e : Bool) + (hcap : capAtA (L.mem s) (cslotAddrA cte) = .Frame fp sz rt dev mp e asid va) + (lbl length buf : U64) (block call cd fpb calling : Bool) (ex : Aeneas.Std.Array obj.Slot 3#usize) + (args : List U64) (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) + (x0 : U64) (hx0 : Aeneas.Std.Array.index_usize ex 0#usize = ok x0) + (xSlot : Fl4SpecA64.CapRef) (exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) + (hex : ExtraRelA Z.th (L.mem s) x0 (exs.map Prod.fst)) + (hLbl : lbl.val ≤ 54 ∨ 57 < lbl.val) + (hMap : lbl = fl4_abi.label.ARMPageMap → FrPlA sz fp mp asid) : + syscall.decode_invocation st ar s a lbl length (cslotAddrA cte) (.Frame fp sz rt dev mp e asid va) block call cd + fpb buf ex ⦃ res => VsTopPostA (L := L) AR Z OS m σ ((Fl4SpecA64.fl4ArchDecodeInvocation (Z := Unit) calling + (Fl4SpecA64.fl4LabelOf lbl.bv) (args.map UScalar.bv) xSlot cte + (absFrameE e (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev (mapDataA mp asid va)) exs).run σ) s a res ⦄ := by + rw [decInv_frameA] + by_cases h49 : lbl = fl4_abi.label.ARMPageMap + · subst h49 + have hlab : Fl4SpecA64.fl4LabelOf fl4_abi.label.ARMPageMap.bv = Fl4SpecA64.archL .armPageMap := by + rw [fl4LabelOf_bvA, show fl4_abi.label.ARMPageMap.val = 49 by simp [fl4_abi.label.ARMPageMap]]; rfl + rw [hlab] + exact page_map_all_jA K E RS hTO hEO hK hT hIpa hTr hnul cte hab hos fp rt asid va sz dev mp e hcap (hMap rfl) + length buf call calling ex args hlen hargs x0 hx0 xSlot exs hex + by_cases h50 : lbl = fl4_abi.label.ARMPageUnmap + · subst h50 + have hlab : Fl4SpecA64.fl4LabelOf fl4_abi.label.ARMPageUnmap.bv = Fl4SpecA64.archL .armPageUnmap := by + rw [fl4LabelOf_bvA, show fl4_abi.label.ARMPageUnmap.val = 50 by simp [fl4_abi.label.ARMPageUnmap]]; rfl + rw [hlab] + cases mp + · cases e + · obtain ⟨hD, x, -, hset, hrest⟩ := page_unmapU_decJA (ar := ar) E RS length buf call calling ex + (args.map UScalar.bv) xSlot exs cte hab fp rt asid va sz dev hcap + obtain ⟨s3, σ3, hdec, hp, J3, T3, C3⟩ := hrest hTr hnul + rw [hdec] + exact (WP.spec_ok _).2 ⟨_, okD_memA hD, _, hset, σ3, [], mem_archPgA hp, Z, rfl, rfl, J3, T3, C3⟩ + · obtain ⟨hD, x, -, hset, hrest⟩ := page_unmapS2U_decJA (ar := ar) E RS length buf call calling ex + (args.map UScalar.bv) xSlot exs cte hab fp rt asid va sz dev hcap + obtain ⟨s3, σ3, hdec, hp, J3, T3, C3⟩ := hrest hTr hnul + rw [hdec] + exact (WP.spec_ok _).2 ⟨_, okD_memA hD, _, hset, σ3, [], mem_archExtA hp, Z, rfl, rfl, J3, T3, C3⟩ + rw [show mapDataA true asid va = some (BitVec.ofNat 16 asid.val, va.bv) from rfl] + by_cases h0 : asid = 0#u64 + · subst h0 + cases e + · obtain ⟨hD, x, -, hset, hrest⟩ := page_unmapZ_decJA (ar := ar) E RS length buf call calling ex + (args.map UScalar.bv) xSlot exs cte hab fp rt va sz dev hcap + obtain ⟨s3, σ3, hdec, hp, J3, T3, C3⟩ := hrest hTr hnul + rw [hdec] + exact (WP.spec_ok _).2 ⟨_, okD_memA hD, _, hset, σ3, [], mem_archPgA hp, Z, rfl, rfl, J3, T3, C3⟩ + · obtain ⟨hD, x, -, hset, hrest⟩ := page_unmapS2Z_decJA (ar := ar) E RS length buf call calling ex + (args.map UScalar.bv) xSlot exs cte hab fp rt va sz dev hcap + obtain ⟨s3, σ3, hdec, hp, J3, T3, C3⟩ := hrest hTr hnul + rw [hdec] + exact (WP.spec_ok _).2 ⟨_, okD_memA hD, _, hset, σ3, [], mem_archExtA hp, Z, rfl, rfl, J3, T3, C3⟩ + cases e + · rcases hk : Fl4SpecA64.asidRootKind σ (BitVec.ofNat 16 asid.val) with _ | k + · obtain ⟨hD, x, -, hset, hrest⟩ := page_unmapS1_decJA K E RS hTO hK hT length buf call calling ex + (args.map UScalar.bv) xSlot exs cte hab fp rt asid va sz dev h0 hcap (Or.inl hk) + obtain ⟨s3, σ3, fa, hdec, hp, hJ, T3, C3⟩ := hrest hTr hnul + rw [hdec] + exact vsTop_ofFaA hD hset (mem_archPgA hp) hJ T3 C3 + by_cases hk1 : k = .stage1 + · subst hk1 + obtain ⟨hD, x, -, hset, hrest⟩ := page_unmapS1_decJA K E RS hTO hK hT length buf call calling ex + (args.map UScalar.bv) xSlot exs cte hab fp rt asid va sz dev h0 hcap (Or.inr hk) + obtain ⟨s3, σ3, fa, hdec, hp, hJ, T3, C3⟩ := hrest hTr hnul + rw [hdec] + exact vsTop_ofFaA hD hset (mem_archPgA hp) hJ T3 C3 + obtain ⟨hD, x, -, hset, hrest⟩ := page_unmap_decJA K E RS hTO hEO hK hT hIpa length buf call calling ex + (args.map UScalar.bv) xSlot exs cte hab fp rt asid va sz dev false h0 hcap k (fun _ => ⟨hk, hk1⟩) + (fun h => by cases h) + obtain ⟨s3, σ3, fa, hdec, hp, hJ, T3, C3⟩ := hrest hTr hnul + rw [hdec] + exact vsTop_ofFaA hD hset (mem_archExtA hp) hJ T3 C3 + · obtain ⟨hD, x, -, hset, hrest⟩ := page_unmap_decJA K E RS hTO hEO hK hT hIpa length buf call calling ex + (args.map UScalar.bv) xSlot exs cte hab fp rt asid va sz dev true h0 hcap .stage2 (fun h => by cases h) + (fun _ => rfl) + obtain ⟨s3, σ3, fa, hdec, hp, hJ, T3, C3⟩ := hrest hTr hnul + rw [hdec] + exact vsTop_ofFaA hD hset (mem_archExtA hp) hJ T3 C3 + by_cases hfl : 51 ≤ lbl.val ∧ lbl.val ≤ 54 + · rw [decode_frame_flA ar lbl length _ fp rt asid va sz dev mp e call buf ex hfl, fl4LabelOf_bvA] + exact fr_flushJA K E RS hTr hIpa cte hab fp rt asid va sz dev mp e hcap lbl hfl length buf args hlen hargs + calling xSlot exs + have hv49 : lbl.val ≠ 49 := fun e => h49 (UScalar.eq_of_val_eq (by rw [e]; simp [fl4_abi.label.ARMPageMap])) + have hv50 : lbl.val ≠ 50 := fun e => h50 (UScalar.eq_of_val_eq (by rw [e]; simp [fl4_abi.label.ARMPageUnmap])) + have hv : lbl.val < 49 ∨ 57 < lbl.val := by omega + rw [decode_frame_otherA lbl length _ buf ex fp rt asid va sz dev mp e call hv] + rw [dec_frOtherA calling _ xSlot cte exs _ + (by rw [fl4LabelOf_bvA]; exact labNA_neA (fun n hn => (labNA_tabA n hn).2.2.2.2.1) (by decide) (by omega)) + (by rw [fl4LabelOf_bvA]; exact labNA_neA (fun n hn => (labNA_tabA n hn).2.2.2.2.2.1) (by decide) (by omega)) + (by rw [fl4LabelOf_bvA]; exact flush_ofNA (by omega)) + (by rw [fl4LabelOf_bvA]; exact labNA_neA (fun n hn => (labNA_tabA n hn).2.2.1) (by decide) hv49) + (by rw [fl4LabelOf_bvA]; exact labNA_neA (fun n hn => (labNA_tabA n hn).2.2.2.1) (by decide) hv50) + (by rw [fl4LabelOf_bvA]; exact labNA_neA (fun n hn => (labNA_tabA n hn).2.2.2.2.2.2) (by decide) (by omega))] + exact (WP.spec_ok _).2 ⟨rfl, rfl, .illegalOperation, rfl, Or.inl trivial⟩ + +end arms + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/FrReplyA.lean b/proofs/Fl4ProofsA64/P3/FrReplyA.lean new file mode 100644 index 000000000..24b0bce1e --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/FrReplyA.lean @@ -0,0 +1,1840 @@ +/- +P3 (aarch64, A64-P3 S3 gen 40, Architect 10:07Z Step 3a): **unified `ArchTopPostA` and the reply-aware frame +invocations** — `ARMPageGetAddress` (label 57) and `ARMPageTestAndClearAccessed` (label 56). + +* **`ArchTopPostA`**: unified arch top postcondition covering all three outcome shapes: + - error (`VsErrRelA`, unchanged state), + - fixed-`Z` or CDT/pool-extending no-reply (`∃ Z', JAtZAX AR Z' s' a' σ2 m ∧ TreeA ... ∧ CarryA`), + - fixed-`Z` reply-aware (`∃ σ3, ((), σ3) ∈ (NonDet.when calling (replyFromKernel ...) >>= setThreadState .running) σ2 ∧ ...`). + **`ArchTopPostA.of_vsTopA`** embeds `VsTopPostA`. +* **`reply1_jA`**: FL4's `vspace.reply1 st s t call v` (writing `BADGE`, `X2`, `MSG_INFO` registers when `call = true`, + then `set_thread_state t TS_RUNNING`) refines `NonDet.when call (replyFromKernel σ.curThread (0, [v.bv])) >>= + setThreadState σ.curThread .running`, preserving `JAtZAX`, `TreeA` and `CarryA`. +* **`page_get_addr_jA`** (label 57, `ARMPageGetAddress`): FL4's `decode_frame` at `ARMPageGetAddress` refines + `fl4ArchDecodeInvocation` (both plain and Stage-2 frames) on `ArchTopPostA`. +* **`pteRelA_clearAF`** / **`jat_clearAF`**: atomic `fetch_and` with `~~~vspace.D_AF` on a Stage-1 or high-window L1 + descriptor preserves `PteRelA`, `VsRelA`, `ExtVsRelA`, `JAtZAX`, `TreeA` and `CarryA` with unchanged spec state `σ`. +* **`test_clear_accessed_jA`** (label 56, `ARMPageTestAndClearAccessed`): FL4's `vspace.decode_test_and_clear_accessed` + refines `fl4ArchDecodeInvocation` (`decodeTestAndClearAccessed` / `decodeExtTestAndClearAccessed`, using + `ArchKeepsA.hwAccessFlag` [P]) on `ArchTopPostA`. + +Named premises: `ArchKeepsA` [P]. Decode-site hypotheses: `RsSiteA`, `TabOffA`, `ExtTabOffA`, `hvcR` (VCPUs disjoint +from the current thread's register words, vacuous at `w0`/`w1`). No new named premise. No existing statement changed. +-/ +import Fl4ProofsA64.P3.FrFlushA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## Unified `ArchTopPostA` -/ + +section post +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-- **Unified outcome of an arch cap-kind top** (module docstring): covers error, no-reply (fixed-`Z` or +CDT/pool-extending), and reply-aware (`ARMPageGetAddress`, `ARMPageTestAndClearAccessed`). -/ +def ArchTopPostA (AR : A → HwRootA → Prop) (Z : XSetsA) (OS : Set U64) (m : Fl4SpecA64.McState) + (calling : Bool) (σ : Fl4SpecA64.State Unit) + (D : Set (Except Fl4SpecA64.SyscallError Fl4SpecA64.Fl4ArchInvocation × Fl4SpecA64.State Unit) × Prop) + (s : S) (a : A) : core.result.Result Unit error.Exc × S × A → Prop + | (.Err e, s', a') => s' = s ∧ a' = a ∧ ∃ e', D = ({(.error e', σ)}, False) ∧ VsErrRelA e e' + | (.Ok (), s', a') => ∃ inv, ((.ok inv, σ) ∈ D.1 ∧ ¬ D.2) ∧ ∃ σ1, + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ∃ (σ2 : Fl4SpecA64.State Unit) (rv : List Fl4SpecA64.Data), + (.ok rv, σ2) ∈ ((Fl4SpecA64.fl4ArchPerformInvocation (Z := Unit) inv).run σ1).1 ∧ + ((∃ Z' : XSetsA, + JAtZAX (L := L) AR Z' s' a' σ2 m ∧ + TreeA st OS Z'.G.chains Z'.G.par s' ∧ + (CarryA Z (L.mem s) σ → CarryA Z' (L.mem s') σ2)) ∨ + (∃ σ3 : Fl4SpecA64.State Unit, + ((), σ3) ∈ ((do + Fl4Spec.NonDet.when calling (Fl4SpecA64.replyFromKernel (Z := Unit) σ.curThread (0, rv)) + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .running) σ2).1 ∧ + JAtZAX (L := L) AR Z s' a' σ3 m ∧ + TreeA st OS Z.G.chains Z.G.par s' ∧ + (CarryA Z (L.mem s) σ → CarryA Z (L.mem s') σ3))) + +/-- `VsTopPostA` embeds into `ArchTopPostA` (no-reply branch). -/ +theorem ArchTopPostA.of_vsTopA {AR : A → HwRootA → Prop} {Z : XSetsA} {OS : Set U64} {m : Fl4SpecA64.McState} + {calling : Bool} {σ : Fl4SpecA64.State Unit} + {D : Set (Except Fl4SpecA64.SyscallError Fl4SpecA64.Fl4ArchInvocation × Fl4SpecA64.State Unit) × Prop} + {s : S} {a : A} : + ∀ {res}, VsTopPostA (L := L) AR Z OS m σ D s a res → ArchTopPostA (L := L) AR Z OS m calling σ D s a res + | (.Err _, _, _), h => h + | (.Ok (), _, _), ⟨inv, hD, σ1, h1, σ', rv, hp, Z', _, hG, hJ, hT, hC⟩ => + ⟨inv, hD, σ1, h1, σ', rv, hp, Or.inl ⟨Z', hJ, by rw [hG]; exact hT, hC⟩⟩ + +end post + +/-! ## Spec side: `replyFromKernel` + `setThreadState .running` -/ + +section spec_reply +open Fl4Spec Fl4SpecA64 + +/-- Spec TCB context update (keeps `tcbVcpu`, `tcbState`, slots, SC, priority, domain unchanged). -/ +abbrev ctxUpdA (σ : State Unit) (t : ObjRef) (x : Tcb) (uc : UserContext) : State Unit := + setObjA σ t (.tcb { x with tcbArch := { x.tcbArch with tcbContext := uc } }) + +/-- Explicit user context after `replyFromKernel` with `(0, [v])` (depends only on `tcbArch`, `tcbIpcBuffer`, +`tcbIpcframe`, never on `tcbState`). -/ +def replyCtxA (x : Tcb) (v : Word) : UserContext := + let uc1 : UserContext := + ⟨x.tcbArch.tcbContext.userFpuState, Function.update x.tcbArch.tcbContext.userRegs badge_register 0⟩ + let newRegs : Register → Word := fun reg => + if reg ∈ msg_registers.take [v].length then the [v][theIndex msg_registers reg]? + else uc1.userRegs reg + let uc2 : UserContext := ⟨uc1.userFpuState, newRegs⟩ + let len : LengthType := match x.tcbIpcframe with + | .archObjectCap (.frameCap _ R _ false _) => + if vmReadWrite ⊆ R ∨ vmReadOnly ⊆ R ∧ ¬true = true + then natToLen (min [v].length msgMaxLength) + else natToLen (min msg_registers.length [v].length) + | _ => natToLen (min msg_registers.length [v].length) + ⟨uc2.userFpuState, Function.update uc2.userRegs msg_info_register (messageInfoToData ⟨len, 0, 0, 0⟩)⟩ + +theorem lookupIpcBuffer_oneA {σ : State Unit} {t : ObjRef} {x : Tcb} (hk : σ.kheap t = some (.tcb x)) : + lookupIpcBuffer (Z := Unit) true t σ = + ({((match x.tcbIpcframe with + | .archObjectCap (.frameCap p R vms false _) => + if vmReadWrite ⊆ R ∨ vmReadOnly ⊆ R ∧ ¬true = true + then some (p + (x.tcbIpcBuffer &&& mask (pageBitsForSize vms))) + else none + | _ => none), σ)}, False) := by + have htg : threadGet (Z := Unit) Tcb.tcbIpcBuffer t σ = ({(x.tcbIpcBuffer, σ)}, False) := by + unfold threadGet + have hg : NonDet.getsThe (getTcb (Z := Unit) t) σ = ({(x, σ)}, False) := by + simp [NonDet.getsThe, getTcb, readObject, hk, NonDet.assertOpt]; rfl + rw [nd_bind_oneA hg]; rfl + have hgc : getCap (Z := Unit) (t, tcbCnodeIndex 2) σ = ({(x.tcbIpcframe, σ)}, False) := by + unfold getCap + have ho : getObject (Z := Unit) t σ = ({(.tcb x, σ)}, False) := by + simp [getObject, NonDet.getsThe, readObject, hk, NonDet.assertOpt]; rfl + dsimp only + rw [nd_bind_oneA ho] + have hcm : tcbCnodeMap x (tcbCnodeIndex 2) = some x.tcbIpcframe := by + have n3 : tcbCnodeIndex 2 ≠ tcbCnodeIndex 3 := by decide + have n4 : tcbCnodeIndex 2 ≠ tcbCnodeIndex 4 := by decide + have n5 : tcbCnodeIndex 2 ≠ tcbCnodeIndex tcbStage2Slot := by decide + have n6 : tcbCnodeIndex 2 ≠ tcbCnodeIndex tcbHighVSpaceSlot := by decide + simp [tcbCnodeMap, Function.update_of_ne n6, Function.update_of_ne n5, + Function.update_of_ne n4, Function.update_of_ne n3] + simp [NonDet.assertOpt, hcm]; rfl + unfold lookupIpcBuffer + rw [nd_bind_oneA htg, nd_bind_oneA (show (pure (t, tcbCnodeIndex 2) : SMonad Unit CslotPtr) σ = _ from rfl), + nd_bind_oneA hgc] + cases x.tcbIpcframe with + | archObjectCap ac => + cases ac with + | frameCap p R vms dev mp => + cases dev with + | false => by_cases h : vmReadWrite ⊆ R <;> simp [h] <;> rfl + | true => rfl + | _ => rfl + | _ => rfl + +theorem asUser_setReg_oneA {σ : State Unit} {t : ObjRef} {x : Tcb} (hk : σ.kheap t = some (.tcb x)) + (r : Register) (v : Word) : + asUser (Z := Unit) t (setRegister r v) σ = + ({((), ctxUpdA σ t x ⟨x.tcbArch.tcbContext.userFpuState, + Function.update x.tcbArch.tcbContext.userRegs r v⟩)}, False) := by + have hg : NonDet.getsThe (getTcb (Z := Unit) t) σ = ({(x, σ)}, False) := by + simp [NonDet.getsThe, getTcb, readObject, hk, NonDet.assertOpt]; rfl + have ho : getObject (Z := Unit) t σ = ({(.tcb x, σ)}, False) := by + simp [getObject, NonDet.getsThe, readObject, hk, NonDet.assertOpt]; rfl + set uc' : UserContext := ⟨x.tcbArch.tcbContext.userFpuState, Function.update x.tcbArch.tcbContext.userRegs r v⟩ + have hsel : NonDet.selectF (setRegister r v (archTcbContextGet x.tcbArch)) σ = ({(((), uc'), σ)}, False) := by + have h1 : setRegister r v (archTcbContextGet x.tcbArch) = ({((), uc')}, False) := rfl + rw [h1] + simp only [NonDet.selectF, Set.mem_singleton_iff, Prod.mk.injEq] + refine ⟨Set.ext fun x => ?_, trivial⟩ + simp [Prod.ext_iff] + have hso : setObject (Z := Unit) t (.tcb { x with tcbArch := archTcbContextSet uc' x.tcbArch }) σ = + ({((), ctxUpdA σ t x uc')}, False) := by + unfold setObject + rw [nd_bind_singleA ho, nd_bind_singleA (show (NonDet.assert (aType (.tcb x) = aType (.tcb _)) : + NonDet (State Unit) Unit) σ = ({((), σ)}, False) by simp [NonDet.assert, aType]), + nd_bind_singleA (show NonDet.get σ = ({(σ, σ)}, False) from rfl)] + rfl + unfold asUser + rw [nd_bind_oneA hg, nd_bind_oneA (show (pure (archTcbContextGet x.tcbArch) : SMonad Unit UserContext) σ = _ from rfl), + nd_bind_oneA hsel] + dsimp only + rw [nd_bind_oneA (show (pure _ : SMonad Unit Tcb) σ = _ from rfl), nd_bind_oneA hso] + rfl + +theorem setMrs_oneA {σ : State Unit} {t : ObjRef} {x : Tcb} (hk : σ.kheap t = some (.tcb x)) + (buf : Option ObjRef) (v : Word) : + setMrs (Z := Unit) t buf [v] σ = + ({((match buf with + | none => natToLen (min msg_registers.length [v].length) + | some _ => natToLen (min [v].length msgMaxLength)), + ctxUpdA σ t x ⟨x.tcbArch.tcbContext.userFpuState, fun reg => + if reg ∈ msg_registers.take [v].length then the [v][theIndex msg_registers reg]? + else x.tcbArch.tcbContext.userRegs reg⟩)}, False) := by + have hg : NonDet.getsThe (getTcb (Z := Unit) t) σ = ({(x, σ)}, False) := by + simp [NonDet.getsThe, getTcb, readObject, hk, NonDet.assertOpt]; rfl + have ho : getObject (Z := Unit) t σ = ({(.tcb x, σ)}, False) := by + simp [getObject, NonDet.getsThe, readObject, hk, NonDet.assertOpt]; rfl + set newRegs : Register → Word := fun reg => + if reg ∈ msg_registers.take [v].length then the [v][theIndex msg_registers reg]? + else archTcbGetRegisters x.tcbArch reg + set uc' : UserContext := ⟨x.tcbArch.tcbContext.userFpuState, newRegs⟩ + have hso : setObject (Z := Unit) t (.tcb { x with tcbArch := archTcbSetRegisters newRegs x.tcbArch }) σ = + ({((), ctxUpdA σ t x uc')}, False) := by + unfold setObject + rw [nd_bind_singleA ho, nd_bind_singleA (show (NonDet.assert (aType (.tcb x) = aType (.tcb _)) : + NonDet (State Unit) Unit) σ = ({((), σ)}, False) by simp [NonDet.assert, aType]), + nd_bind_singleA (show NonDet.get σ = ({(σ, σ)}, False) from rfl)] + rfl + have hdrop : [v].drop msg_registers.length = [] := by + have hlen : msg_registers.length = 4 := by decide + rw [hlen]; rfl + unfold setMrs + rw [nd_bind_oneA hg, nd_bind_oneA (show (pure (archTcbGetRegisters x.tcbArch) : SMonad Unit _) σ = _ from rfl), + nd_bind_oneA (show (pure newRegs : SMonad Unit _) σ = _ from rfl), nd_bind_oneA hso, + nd_bind_oneA (show (pure ([v].drop msg_registers.length) : SMonad Unit _) _ = _ from rfl), hdrop] + cases buf with + | none => rfl + | some pptr => + dsimp only + have hz : NonDet.zipWithMx (fun x => storeWordOffs (Z := Unit) pptr x) + (List.range' (msg_registers.length + 1) (msgMaxLength + 1 - (msg_registers.length + 1))) [] + (ctxUpdA σ t x uc') = ({((), ctxUpdA σ t x uc')}, False) := by + simp [NonDet.zipWithMx]; rfl + rw [nd_bind_oneA hz]; rfl + +private theorem reply_len_eqA (f : Cap) (ib : Word) (v : Word) : + (match (match f with + | .archObjectCap (.frameCap p R vms false _) => + if vmReadWrite ⊆ R ∨ vmReadOnly ⊆ R ∧ ¬true = true + then some (p + (ib &&& mask (pageBitsForSize vms))) else none + | _ => none) with + | none => natToLen (min msg_registers.length [v].length) + | some _ => natToLen (min [v].length msgMaxLength)) = + (match f with + | .archObjectCap (.frameCap _ R _ false _) => + if vmReadWrite ⊆ R ∨ vmReadOnly ⊆ R ∧ ¬true = true + then natToLen (min [v].length msgMaxLength) + else natToLen (min msg_registers.length [v].length) + | _ => natToLen (min msg_registers.length [v].length)) := by + cases f with + | archObjectCap ac => + cases ac with + | frameCap p R vms dev mp => + cases dev with + | false => by_cases h : vmReadWrite ⊆ R <;> simp [h] + | true => rfl + | _ => rfl + | _ => rfl + +theorem replyFromKernel_oneA {σ : State Unit} {t : ObjRef} {x : Tcb} (hk : σ.kheap t = some (.tcb x)) (v : Word) : + replyFromKernel (Z := Unit) t (0, [v]) σ = ({((), ctxUpdA σ t x (replyCtxA x v))}, False) := by + let buf : Option ObjRef := match x.tcbIpcframe with + | .archObjectCap (.frameCap p R vms false _) => + if vmReadWrite ⊆ R ∨ vmReadOnly ⊆ R ∧ ¬true = true + then some (p + (x.tcbIpcBuffer &&& mask (pageBitsForSize vms))) else none + | _ => none + let len : LengthType := match buf with + | none => natToLen (min msg_registers.length [v].length) + | some _ => natToLen (min [v].length msgMaxLength) + have hbuf : lookupIpcBuffer (Z := Unit) true t σ = ({(buf, σ)}, False) := lookupIpcBuffer_oneA hk + have h1 := asUser_setReg_oneA hk badge_register 0 + let uc1 : UserContext := ⟨x.tcbArch.tcbContext.userFpuState, + Function.update x.tcbArch.tcbContext.userRegs badge_register 0⟩ + have hk1 : (ctxUpdA σ t x uc1).kheap t = some (.tcb { x with tcbArch := { x.tcbArch with tcbContext := uc1 } }) := by + simp [ctxUpdA, setObjA] + have h2 := setMrs_oneA hk1 buf v + let uc2 : UserContext := ⟨uc1.userFpuState, fun reg => + if reg ∈ msg_registers.take [v].length then the [v][theIndex msg_registers reg]? else uc1.userRegs reg⟩ + have hk2 : (ctxUpdA (ctxUpdA σ t x uc1) t { x with tcbArch := { x.tcbArch with tcbContext := uc1 } } uc2).kheap t = + some (.tcb { x with tcbArch := { x.tcbArch with tcbContext := uc2 } }) := by + simp [ctxUpdA, setObjA] + have h2' : setMrs (Z := Unit) t buf [v] (ctxUpdA σ t x uc1) = + ({(len, ctxUpdA (ctxUpdA σ t x uc1) t { x with tcbArch := { x.tcbArch with tcbContext := uc1 } } uc2)}, False) := h2 + have h3 := asUser_setReg_oneA hk2 msg_info_register (messageInfoToData ⟨len, 0, 0, 0⟩) + have hlen : len = (match x.tcbIpcframe with + | .archObjectCap (.frameCap _ R _ false _) => + if vmReadWrite ⊆ R ∨ vmReadOnly ⊆ R ∧ ¬true = true + then natToLen (min [v].length msgMaxLength) + else natToLen (min msg_registers.length [v].length) + | _ => natToLen (min msg_registers.length [v].length)) := reply_len_eqA x.tcbIpcframe x.tcbIpcBuffer v + have heq : ctxUpdA (ctxUpdA (ctxUpdA σ t x uc1) t { x with tcbArch := { x.tcbArch with tcbContext := uc1 } } uc2) + t { x with tcbArch := { x.tcbArch with tcbContext := uc2 } } + ⟨uc2.userFpuState, Function.update uc2.userRegs msg_info_register (messageInfoToData ⟨len, 0, 0, 0⟩)⟩ = + ctxUpdA σ t x (replyCtxA x v) := by + simp only [ctxUpdA, setObjA, Function.update_idem, replyCtxA, hlen, uc1, uc2] + unfold replyFromKernel setMessageInfo + have hp : (pure ((0 : Data), [v]) : SMonad Unit (Data × List Data)) σ = ({(((0 : Data), [v]), σ)}, False) := rfl + rw [nd_bind_oneA hp] + dsimp only + rw [nd_bind_oneA hbuf, nd_bind_oneA h1, nd_bind_oneA h2', h3, heq] + +/-- Spec `NonDet.when call (replyFromKernel t (0, [v])) >>= setThreadState t .running` from a TCB `x`. -/ +theorem replyRun_oneA {σ : State Unit} {t : ObjRef} {x : Tcb} (hk : σ.kheap t = some (.tcb x)) + (call : Bool) (v : Word) + (hsch : ∀ uc', t = σ.curThread → σ.schedulerAction = .resumeCurThread → + schedulable t (setObjA σ t (.tcb { x with tcbArch := { x.tcbArch with tcbContext := uc' }, tcbState := .running })) = true) : + ((do + NonDet.when call (replyFromKernel (Z := Unit) t (0, [v])) + setThreadState (Z := Unit) t .running) σ) = + ({((), stUpdA (ctxUpdA σ t x (if call then replyCtxA x v else x.tcbArch.tcbContext)) t + { x with tcbArch := { x.tcbArch with tcbContext := if call then replyCtxA x v else x.tcbArch.tcbContext } } + .running)}, False) := by + cases call with + | false => + have hs := setThreadState_eqA σ t x hk .running (hsch x.tcbArch.tcbContext) + unfold NonDet.when + rw [if_neg Bool.false_ne_true, nd_bind_oneA (show (NonDet.pure () : SMonad Unit Unit) σ = _ from rfl), hs] + congr 2; simp [ctxUpdA, stUpdA, setObjA, Function.update_idem] + | true => + have hrep := replyFromKernel_oneA hk v + have hk' : (ctxUpdA σ t x (replyCtxA x v)).kheap t = + some (.tcb { x with tcbArch := { x.tcbArch with tcbContext := replyCtxA x v } }) := by + simp [ctxUpdA, setObjA] + have hs := setThreadState_eqA (ctxUpdA σ t x (replyCtxA x v)) t + { x with tcbArch := { x.tcbArch with tcbContext := replyCtxA x v } } hk' .running (fun h1 h2 => by + have h3 := hsch (replyCtxA x v) h1 h2 + simpa [ctxUpdA, stUpdA, setObjA, Function.update_idem] using h3) + unfold NonDet.when + rw [if_pos rfl, nd_bind_oneA hrep] + exact hs + +end spec_reply + +/-! ## `JAtZAX` and `CarryA` across TCB context update and register writes -/ + +section reply_frame +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- Replacing only `tcbContext` on a TCB in `σ` keeps `JAtZAX` at the same `s`, `a`, `Z`. -/ +theorem JAtZAX.of_tcbCtxA (J : JAtZAX (L := L) AR Z s a σ m) {t : U64} {x : Fl4SpecA64.Tcb} + (hk : σ.kheap (specPtr t) = some (.tcb x)) (uc : Fl4SpecA64.UserContext) : + JAtZAX (L := L) AR Z s a (ctxUpdA σ (specPtr t) x uc) m := by + obtain ⟨J, V, X, Y⟩ := J + set σ1 := ctxUpdA σ (specPtr t) x uc + set x1 : Fl4SpecA64.Tcb := { x with tcbArch := { x.tcbArch with tcbContext := uc } } + have hkt : σ1.kheap (specPtr t) = some (.tcb x1) := by + show Function.update σ.kheap (specPtr t) (some (.tcb x1)) (specPtr t) = some (.tcb x1) + exact Function.update_self _ _ _ + have hkn : ∀ p, p ≠ specPtr t → σ1.kheap p = σ.kheap p := fun p hp => + Function.update_of_ne hp _ _ + have hkeep : ∀ p o, σ.kheap p = some o → (∀ y, o ≠ .tcb y) → σ1.kheap p = some o := fun p o hp ho => by + have e : p ≠ specPtr t := fun e => by subst e; rw [hk] at hp; exact ho x (Option.some.inj hp).symm + rw [hkn p e, hp] + have hback : ∀ p o, σ1.kheap p = some o → (∀ y, o ≠ .tcb y) → σ.kheap p = some o := fun p o hp ho => by + by_cases e : p = specPtr t + · subst e; rw [hkt] at hp; exact absurd (Option.some.inj hp).symm (ho x1) + · rw [hkn p e] at hp; exact hp + have hTk : ∀ p y, σ.kheap p = some (.tcb y) → + ∃ y', σ1.kheap p = some (.tcb y') ∧ y'.tcbState = y.tcbState ∧ y'.tcbSchedContext = y.tcbSchedContext ∧ + y'.tcbPriority = y.tcbPriority ∧ y'.tcbDomain = y.tcbDomain ∧ y'.tcbArch.tcbVcpu = y.tcbArch.tcbVcpu ∧ + y'.tcbExt = y.tcbExt ∧ Fl4SpecA64.tcbCnodeMap y' = Fl4SpecA64.tcbCnodeMap y := by + intro p y hy + by_cases e : p = specPtr t + · subst e; rw [hk] at hy; cases hy + exact ⟨x1, hkt, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ + · exact ⟨y, by rw [hkn p e, hy], rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ + have hst : StateRelA Z.th (L.mem s) σ1 := by + refine ⟨fun p r hr => ?_, fun p ht => ?_⟩ + · obtain ⟨cs, e, h2, h3⟩ := J.st.cnode p r hr + exact ⟨cs, hkeep _ _ e (fun _ h => by cases h), h2, h3⟩ + · obtain ⟨y, e, hc⟩ := J.st.tcb p ht + obtain ⟨y', e', -, -, -, -, -, -, hcm⟩ := hTk _ y e + refine ⟨y', e', fun j hj => ?_⟩ + rw [hcm]; exact hc j hj + have hcdt : CdtRelA st Z.th s σ1 Z.G := + CdtRelA.mkA J.cdt.tree J.cdt.cdt J.cdt.cdt_slot J.cdt.orig J.cdt.par_slot J.cdt.po + have hirq : IrqRelA (L.mem s) (L.ksb s) σ1 := ⟨J.irq.st, J.irq.node, J.irq.nodeHi⟩ + have hsch : SchedRelA (L := L) Z.T Z.C Z.Cs Z.curOf Z.curScOf s σ1 m := + { core := J.sch.core, cpuCs := J.sch.cpuCs, curOfW := J.sch.curOfW, curScOfW := J.sch.curScOfW, + cur := J.sch.cur, csc := J.sch.csc, idle := J.sch.idle, idleOf := J.sch.idleOf, idleScOf := J.sch.idleScOf, + act := J.sch.act, time := J.sch.time, aff := J.sch.aff, affT := J.sch.affT, + tcb := fun y hy => by + obtain ⟨y0, e, h1, h2⟩ := J.sch.tcb y hy + obtain ⟨y', e', hs, hsc, -⟩ := hTk _ y0 e + exact ⟨y', e', by rw [hs]; exact h1, by rw [hsc]; exact h2⟩, + sc := fun c hc => by + have := J.sch.sc c hc + by_cases ep : scRenA m c = specPtr t + · rw [ep, hk] at this; rcases this with ⟨_, _, h, -⟩; cases h + · rw [hkn _ ep]; exact this, + scOff := J.sch.scOff } + have hrest : SchedRestA (L := L) Z s σ1 m := + { rq := J.rest.rq, rqDom := J.rest.rqDom, rel := J.rest.rel, consumed := J.rest.consumed, + repr := J.rest.repr, curDom := J.rest.curDom, domTime := J.rest.domTime, + prio := fun y hy => by + obtain ⟨y0, e, h1, h2, h3⟩ := J.rest.prio y hy + obtain ⟨y', e', -, -, hp, hd, -⟩ := hTk _ y0 e + exact ⟨y', e', by rw [hd]; exact h1, h2, by rw [hp]; exact h3⟩, + scMem := J.rest.scMem, + refillMax := fun c hc => by + obtain ⟨sc, n, e, hr⟩ := J.rest.refillMax c hc + exact ⟨sc, n, hkeep _ _ e (fun _ h => by cases h), hr⟩ } + have J' : JAtZA (L := L) AR Z s a σ1 m := + { st := hst, cdt := hcdt, sch := hsch, ar := J.ar, place := J.place, irq := hirq, rest := hrest, + thT := J.thT, csN := J.csN, idleT := J.idleT, idleC := J.idleC, curT := J.curT, cscC := J.cscC, + ksFit := J.ksFit, scOffD := J.scOffD, cv := J.cv, pml := J.pml, + vs := J.vs.of_spec rfl fun p hp => by + by_cases ep : p = specPtr t + · subst ep; exact absurd hk (J.vs.not_tcb hp) + · exact hkn p ep, + ext := J.ext.of_spec rfl fun q hq => by + by_cases ep : specPtr q = specPtr t + · rcases hq with hqP | hqXT + · exact absurd (ep ▸ hk) (J.vs.not_tcb (Or.inl ⟨q, hqP, rfl⟩)) + · exact absurd (ep ▸ hk) (J.ext.not_tcb hqXT x) + · exact hkn _ ep } + refine ⟨J', ⟨fun w hw => ?_, fun p y e => V.back p y (hback _ _ e (fun _ h => by cases h)), fun y hy => ?_, V.cap⟩, + ⟨fun r hr => ?_, fun p es e => X.back p es (hback _ _ e (fun _ h => by cases h)), X.cap⟩, + ⟨fun r hr => ?_, fun p l1 e => Y.back p l1 (hback _ _ e (fun _ h => by cases h)), Y.cap⟩⟩ + · obtain ⟨vx, e, h2⟩ := V.obj w hw + exact ⟨vx, hkeep _ _ e (fun _ h => by cases h), h2⟩ + · obtain ⟨y0, e, h1, h2⟩ := V.tcb y hy + obtain ⟨y', e', -, -, -, -, hv, hext, -⟩ := hTk _ y0 e + exact ⟨y', e', by rw [hv]; exact h1, by rw [hext]; exact h2⟩ + · obtain ⟨es, e⟩ := X.obj r hr + exact ⟨es, hkeep _ _ e (fun _ h => by cases h)⟩ + · obtain ⟨l1, e⟩ := Y.obj r hr + exact ⟨l1, hkeep _ _ e (fun _ h => by cases h)⟩ + +theorem TyKeepA.ctxUpdA {σ : Fl4SpecA64.State Unit} {t : Fl4SpecA64.ObjRef} {x : Fl4SpecA64.Tcb} + (hk : σ.kheap t = some (.tcb x)) (uc : Fl4SpecA64.UserContext) : TyKeepA σ (ctxUpdA σ t x uc) := + TyKeepA.upd hk (fun _ _ e => by cases e) fun _ _ => ⟨x, rfl⟩ + +/-- A TCB register word (`256 ≤ K.val < 3328`) of the current thread is off `FootA Z s`. -/ +theorem tcbReg_offFootA (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + (hvcR : ∀ v ∈ Z.V, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → wa (L.curW s) K ≠ v ∧ wa (L.curW s) K ≠ wa v 24#u64) + {K : U64} (hK1 : 256 ≤ K.val) (hK2 : K.val < 3328) : + ¬ FootA (L := L) Z s (wa (L.curW s) K) := by + have q := E.sq + obtain ⟨-, -, hX, -, hS⟩ := E.sys.ent.bxi + have hct : L.curW s ∈ H.tcbs := q.curT + have hta : TcbAddr (L.curW s) := q.tcbs _ hct + have hT : ∀ y, y ∈ Z.T → y ∈ H.tcbs := fun y hy => by rw [E.tcbs]; exact hy + have hΩ : ∀ b ∈ H.omega, b ≠ wa (L.curW s) K := fun b hb e => by + have := out_ne_field (q.omega b hb) hct hta (K := K) (by omega) + rw [e] at this; exact this rfl + have hks : ∀ o : U64, o.val < 223488 → o.val % 8 = 0 → wa (L.ksb s) o ≠ wa (L.curW s) K := + fun o h1 h2 => hΩ _ (hS.ksΩ o h1 h2) + intro hf + generalize hb : wa (L.curW s) K = b at hf + cases hf with + | slot y _ hy hw => + obtain ⟨p, k, j, hp, hj, hyv, hk7⟩ := slotW_valA E.j.1.place hy + have hfit := E.j.1.place.fit p k hp + have ht : Z.th (L.curW s) = some .tcb := (E.j.1.thT _).2 (by rw [← E.tcbs]; exact hct) + have hK' : ∃ jw : U64, jw.val < 32 ∧ wa (L.curW s) K = wa y jw := by + rcases hw with he | he | he | he + · exact ⟨0#u64, by simp, hb.trans (he.trans (by apply UScalar.eq_of_val_eq; rw [wa_valm]; simp))⟩ + · exact ⟨8#u64, by simp, hb.trans he⟩ + · exact ⟨16#u64, by simp, hb.trans he⟩ + · exact ⟨24#u64, by simp, hb.trans he⟩ + obtain ⟨jw, hjw, heq⟩ := hK' + have ev := congrArg UScalar.val heq + rw [tcb_wadd_val hta (by omega), wa_valm, Nat.mod_eq_of_lt (by omega)] at ev + by_cases ep : p = L.curW s + · subst ep; rw [ht] at hp; cases hp; have := hk7 rfl; omega + · have hsz : (ObjKindA.tcb).size = 4096 := rfl + rcases E.j.1.place.apart p (L.curW s) k .tcb hp ht ep with h | h <;> omega + | nslot _ hn => + cases hn with + | node c _ hc ho => + rcases ho with he | he | he | he + · exact node_ne_field (q.nodes c hc) (by simp [layout.node.CUR_THREAD]) hct hta (by omega) (hb.trans he).symm + · exact node_ne_field (q.nodes c hc) (by simp [layout.node.IDLE_THREAD]) hct hta (by omega) (hb.trans he).symm + · exact node_ne_field (q.nodes c hc) (by simp [layout.node.CUR_SC]) hct hta (by omega) (hb.trans he).symm + · exact node_ne_field (q.nodes c hc) (by simp [layout.node.IDLE_SC]) hct hta (by omega) (hb.trans he).symm + | cur _ ho => + rcases ho with he | he + · exact node_ne_field (q.nodes _ E.j.1.sch.cpuCs) (by simp [layout.node.SCHED_ACTION]) hct hta (by omega) (hb.trans he).symm + · exact node_ne_field (q.nodes _ E.j.1.sch.cpuCs) (by simp [layout.node.CUR_TIME]) hct hta (by omega) (hb.trans he).symm + | cur2 _ ho => + rcases ho with he | he + · exact node_ne_field (q.nodes _ E.j.1.sch.cpuCs) (by simp [layout.node.CONSUMED]) hct hta (by omega) (hb.trans he).symm + · exact node_ne_field (q.nodes _ E.j.1.sch.cpuCs) (by simp [layout.node.REPROGRAM]) hct hta (by omega) (hb.trans he).symm + | tcb y _ hy ho => + have hne : ∀ (o' : U64), o'.val ≥ 3328 → o'.val < 4096 → wa y o' ≠ wa (L.curW s) K := fun o' h1 h2 => + field_ne (q.tcbs _ (hT y hy)) hta h2 (by omega) (fun _ e => by omega) + rcases ho with he | he | he | he | he + · exact hne 3328#u64 (by simp) (by simp) (by rw [← stA_eq]; exact (hb.trans he).symm) + · exact hne 3464#u64 (by simp) (by simp) (hb.trans he).symm + · exact hne 3520#u64 (by simp) (by simp) (hb.trans he).symm + · exact hne 3664#u64 (by simp) (by simp) (hb.trans he).symm + · exact hne 3672#u64 (by simp) (by simp) (hb.trans he).symm + | tcb2 y _ hy ho => + have hne : ∀ (o' : U64), o'.val ≥ 3328 → o'.val < 4096 → wa y o' ≠ wa (L.curW s) K := fun o' h1 h2 => + field_ne (q.tcbs _ (hT y hy)) hta h2 (by omega) (fun _ e => by omega) + rcases ho with he | he + · exact hne 3448#u64 (by simp) (by simp) (hb.trans he).symm + · exact hne 3432#u64 (by simp) (by simp) (hb.trans he).symm + | sc c hc => + rw [← E.scs] at hc + have e : scTcbA c = wa c 24#u64 := by unfold scTcbA layout.sc.TCB; rfl + exact hΩ _ (hS.sub _ (hX.1.fixW _ hc).1) (e.trans hb.symm) + | sc2 c hc => + rw [← E.scs] at hc + exact hΩ _ (hS.sub _ (hS.all _ hc).2.1) ((maxA_eqA c).trans hb.symm) + | irq i hi => + exact hks (irqOffA i) (by rw [irqOffA_val hi]; omega) (by rw [irqOffA_val hi]; omega) hb.symm + | glob _ ho => + rcases ho with he | he + · exact hks _ (by simp) (by simp) (hb.trans he).symm + · exact hks _ (by simp) (by simp) (hb.trans he).symm + | vcpu v _ hv hw => + rcases hw with he | he + · exact (hvcR v hv K hK1 hK2).1 (hb.trans he) + · exact (hvcR v hv K hK1 hK2).2 (hb.trans he) + | vs _ hb' => + have ev := tcb_wadd_val hta (o := K) (by omega) + rw [hb] at ev + rcases (E.ap.vs _ hb').2 _ hct with h | h <;> omega + | ext _ hb' => + have ev := tcb_wadd_val hta (o := K) (by omega) + rw [hb] at ev + rcases (E.ap.ext _ hb').2 _ hct with h | h <;> omega + +/-- A TCB field word (`256 ≤ K.val < 4096`) of the current thread is no W7 cap-slot word (`W7.CapW`). -/ +theorem tcbFld_notCapWA (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + {K : U64} (hK1 : 256 ≤ K.val) (hK2 : K.val < 4096) : + ¬ W7.CapW H.tcbs H.omega H.frame (wa (L.curW s) K) := by + have hct : L.curW s ∈ H.tcbs := E.sq.curT + have hta : TcbAddr (L.curW s) := E.sq.tcbs _ hct + rintro (⟨t', ht', K', hK', he⟩ | ⟨hOm, -⟩) + · exact field_ne (E.sq.tcbs _ ht') hta (by omega) hK2 (fun _ e => by omega) he.symm + · exact out_ne_field (E.sq.omega _ hOm) hct hta hK2 rfl + +/-- Single aligned write outside `InOS OS` keeps all `OS` slots (`OSKeep`). -/ +theorem osKeep_of_wr_notInOS {OS : Set U64} (hO : OSOK H.tcbs H.omega H.frame OS) + {s0 : S} {sl v : U64} (hsl : Aligned sl) (hoff : ¬ InOS OS sl) : + OSKeep st OS s0 (L.wr s0 sl v) := by + intro y hy + have hfit := hO.fit y hy + have hal := (hO.slot y hy).1 + have hw : ∀ o : U64, o.val < 32 → o.val % 8 = 0 → L.mem (L.wr s0 sl v) (wa y o) = L.mem s0 (wa y o) := by + intro o ho1 ho2 + have hval : (wa y o).val = y.val + o.val := by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have hwa : Aligned (wa y o) := by + unfold Aligned at hal ⊢; rw [hval]; omega + refine mem_wr_ne' hsl hwa fun e => hoff ⟨y, hy, ?_, ?_⟩ + · rw [e, hval]; omega + · rw [e, hval]; omega + have h0 : wa y 0#u64 = y := by apply UScalar.eq_of_val_eq; rw [wa_valm]; simp + refine ⟨by have := hw 0#u64 (by simp) (by simp); rwa [h0] at this, + hw 8#u64 (by simp) (by simp), hw 16#u64 (by simp) (by simp), hw 24#u64 (by simp) (by simp)⟩ + +/-- FL4's `sched.set_thread_state ... TS_RUNNING` of a bound thread with an active SC out of the release queue. -/ +theorem set_thread_state_run_wrA {s : S} {t : U64} (hta : TcbAddr t) + (hsc : L.mem s (scA t) ≠ 0#u64) (hrm : L.rmax s (L.mem s (scA t)) ≠ 0#u64) + (hiq : (L.mem s (inqA t) &&& sched.Q_RELEASE) = 0#u64) + (hma : Aligned (maxA (L.mem s (scA t)))) (hmx : stA t ≠ maxA (L.mem s (scA t))) : + sched.set_thread_state st s t sched.TS_RUNNING = ok (L.wr s (stA t) sched.TS_RUNNING) := by + have sA : Aligned (stA t) := by rw [stA_eq]; exact field_aligned hta (by simp) (by simp) + have aS : Aligned (scA t) := field_aligned (K := 3464#u64) hta (by simp) (by simp) + have aQ : Aligned (inqA t) := field_aligned (K := 3592#u64) hta (by simp) (by simp) + have nsc : stA t ≠ scA t := by + rw [stA_eq]; exact field_ne (K2 := 3464#u64) hta hta (by simp) (by simp) (fun _ => by simp) + have niq : stA t ≠ inqA t := by + rw [stA_eq]; exact field_ne (K2 := 3592#u64) hta hta (by simp) (by simp) (fun _ => by simp) + have hs : sched.is_schedulable st (L.wr s (stA t) sched.TS_RUNNING) t = ok true := by + apply is_schedA + · rw [L.mem_wr_same sA]; exact Or.inl rfl + · rw [mem_wr_ne' sA aS nsc]; exact hsc + · rw [mem_wr_ne' sA aQ niq]; exact hiq + · rw [mem_wr_ne' sA aS nsc] + show L.mem _ (maxA _) ≠ 0#u64 + rw [mem_wr_ne' sA hma hmx]; exact hrm + unfold sched.set_thread_state + rw [eq_of_spec_fd (obj.Tcb.set_state.spec (L := L) s t _)] + simp only [bind_ok] + exact schedule_tcb_sch _ _ hs + +/-- The register-write prefix of `vspace.reply1`: writes only TCB register words (`256 ≤ K < 3328`) of `t`. -/ +theorem reply1_regs_wrA {s : S} {t v : U64} (hta : TcbAddr t) (call : Bool) : + (if call then do + let buf ← msg.lookup_ipc_buffer st s true t + let s2 ← obj.Tcb.set_reg st t s layout.reg.BADGE 0#u64 + let (n, s3) ← msg.set_mr st s2 t buf 0#u64 v + let mi ← msginfo.MessageInfo.new 0#u64 0#u64 0#u64 n + msg.set_msg_info st s3 t mi + else ok s) ⦃ s1 => WritesIn st s s1 (fun b => ∃ K : U64, 256 ≤ K.val ∧ K.val < 3328 ∧ b = wa t K) ⦄ := by + cases call with + | false => exact (WP.spec_ok _).2 (WritesIn.refl _ _) + | true => + have hreg : ∀ (s0 : S) (r val : U64), r.val < 384 → + obj.Tcb.set_reg st t s0 r val ⦃ s' => + WritesIn st s0 s' (fun b => ∃ K : U64, 256 ≤ K.val ∧ K.val < 3328 ∧ b = wa t K) ⦄ := fun s0 r val hr => + WP.spec_mono (obj.Tcb.set_reg.wrK s0 t r val hta hr) fun s' ⟨K, hK, e⟩ => by + subst e + have hA : Aligned (wa t K) := field_aligned hta (by omega) (by omega) + exact (writesIn_wr hA).mono fun b hb => ⟨K, by omega, by omega, hb⟩ + have htr : ∀ {s0 s1 s2 : S}, + WritesIn st s0 s1 (fun b => ∃ K : U64, 256 ≤ K.val ∧ K.val < 3328 ∧ b = wa t K) → + WritesIn st s1 s2 (fun b => ∃ K : U64, 256 ≤ K.val ∧ K.val < 3328 ∧ b = wa t K) → + WritesIn st s0 s2 (fun b => ∃ K : U64, 256 ≤ K.val ∧ K.val < 3328 ∧ b = wa t K) := + fun h1 h2 => (h1.trans h2).mono fun _ h => h.elim id id + refine WP.spec_bind (P0.msg.lookup_ipc_buffer.p0 st s true t L.total) fun buf _ => ?_ + refine WP.spec_bind (hreg s layout.reg.BADGE 0#u64 (by simp [layout.reg.BADGE, layout.reg.X0])) fun s2 w2 => ?_ + unfold msg.set_mr + simp only [show (0#u64 < msg.N_MSG_REGISTERS) = True from by simp [msg.N_MSG_REGISTERS], if_true, + msg.msg_register, Aeneas.Std.bind_ok] + refine WP.spec_bind (Pₘ := fun r : U64 × S => + WritesIn st s2 r.2 (fun b => ∃ K : U64, 256 ≤ K.val ∧ K.val < 3328 ∧ b = wa t K)) ?_ ?_ + · refine WP.spec_bind (hreg s2 layout.reg.X2 v (by simp [layout.reg.X2])) fun s3 w3 => ?_ + simp only [lift, Aeneas.Std.bind_ok, WP.spec_ok]; exact w3 + rintro ⟨n, s3⟩ w3 + simp only at w3 + try simp only [WP.uncurry'_pair] + unfold msginfo.MessageInfo.new msg.set_msg_info + simp only [Aeneas.Std.bind_ok] + refine WP.spec_bind (P0.msginfo.MessageInfo.to_word.p0 _) fun mi _ => ?_ + exact WP.spec_mono (hreg s3 layout.reg.MSG_INFO mi (by simp [layout.reg.MSG_INFO, layout.reg.X1])) + fun s4 w4 => htr w2 (htr w3 w4) + +/-- **`vspace.reply1` refines `NonDet.when call (replyFromKernel …) >>= setThreadState .running`**, preserving +`JAtZAX`, `TreeA`, `CarryA` from any state `s1` that has `JAtZAX` at `σ2` (e.g. after restart or after `fetch_and`). -/ +theorem reply1_jA (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) + (hvcR : ∀ v ∈ Z.V, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → wa (L.curW s) K ≠ v ∧ wa (L.curW s) K ≠ wa v 24#u64) + {s1 : S} {σ2 : Fl4SpecA64.State Unit} {x2 : Fl4SpecA64.Tcb} + (J1 : JAtZAX (L := L) AR Z s1 a σ2 m) + (hTr1 : TreeA st OS Z.G.chains Z.G.par s1) + (hks1 : L.ksb s1 = L.ksb s) (hcpu1 : L.cpu s1 = L.cpu s) (hcur1 : L.curW s1 = L.curW s) + (hsc1 : L.mem s1 (scA (L.curW s)) = L.mem s (scA (L.curW s))) + (hiq1 : L.mem s1 (inqA (L.curW s)) = L.mem s (inqA (L.curW s))) + (hrm1 : L.rmax s1 (L.mem s (scA (L.curW s))) = L.rmax s (L.mem s (scA (L.curW s)))) + (hk2 : σ2.kheap σ.curThread = some (.tcb x2)) + (hct2 : σ2.curThread = σ.curThread) + (hrq2 : σ.curThread ∉ σ2.releaseQueue) + (hsc2 : x2.tcbSchedContext = some (scRenA m (L.mem s (scA (L.curW s))))) + (hy2 : ∃ y n, σ2.kheap (scRenA m (L.mem s (scA (L.curW s)))) = some (.schedContext y n) ∧ 0 < y.scRefillMax) + (call : Bool) (v : U64) : + vspace.reply1 st s1 (L.curW s) call v ⦃ s' => + ∃ σ3, ((), σ3) ∈ ((do + Fl4Spec.NonDet.when call (Fl4SpecA64.replyFromKernel (Z := Unit) σ.curThread (0, [v.bv])) + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .running) σ2).1 ∧ + JAtZAX (L := L) AR Z s' a σ3 m ∧ + TreeA st OS Z.G.chains Z.G.par s' ∧ + (CarryA Z (L.mem s1) σ2 → CarryA Z (L.mem s') σ3) ⦄ := by + have q := E.sq + obtain ⟨-, -, hX, -, hS⟩ := E.sys.ent.bxi + set t := L.curW s + have hct : t ∈ H.tcbs := q.curT + have hta : TcbAddr t := q.tcbs t hct + have htZ : t ∈ Z.T := by rw [← E.tcbs]; exact hct + have hcT : σ.curThread = specPtr t := E.j.1.sch.curThread + have O0 := E.stOffA RS.vc + have O1 : StOffA (L := L) Z s1 t := by + refine ⟨fun c hc o ho => by rw [nodeAt_ksb hks1]; exact O0.node c hc o ho, + O0.fld, fun b hb => by rw [hks1] at hb; exact O0.om b hb, + O0.slot, O0.vc, fun b hb => by rw [hks1] at hb; exact O0.vs b hb, O0.ext⟩ + have sA : Aligned (stA t) := by rw [stA_eq]; exact field_aligned hta (by simp) (by simp) + set w := L.mem s (scA t) + have hw0 : w ≠ 0#u64 := RS.cur.2.1 + have hwC : w ∈ Z.C := E.j.1.rest.scMem t htZ hw0 + have hwH : w ∈ H.scs := by rw [E.scs]; exact hwC + have hrm : L.rmax s w ≠ 0#u64 := (hS.act w hwH).1 RS.cur.2.2 + have hmΩ : maxA w ∈ H.omega := hS.sub _ (hS.all _ hwH).2.1 + have hma : Aligned (maxA w) := (q.omega _ hmΩ).1 + have hmx : stA t ≠ maxA w := fun e => O0.om _ (Or.inr (Or.inr (Or.inr ⟨w, hwC, Or.inr (maxA_eqA w)⟩))) e.symm + -- Spec side + obtain ⟨ysc, nsc, hysc, hpos⟩ := hy2 + set uc' := if call then replyCtxA x2 v.bv else x2.tcbArch.tcbContext + have hspec := replyRun_oneA hk2 call v.bv fun uc'' _ _ => by + refine schedulable_ofA (x := { x2 with tcbArch := { x2.tcbArch with tcbContext := uc'' }, tcbState := .running }) + (n := nsc) (by simp [setObjA]) rfl hsc2 ?_ hpos hrq2 + have hne : scRenA m w ≠ σ.curThread := fun e => by rw [e, hk2] at hysc; cases hysc + simpa [setObjA, Function.update_of_ne hne] using hysc + -- FL4 side + unfold vspace.reply1 + refine WP.spec_bind (reply1_regs_wrA (s := s1) hta call) fun s2 hw2 => ?_ + have hks2 : L.ksb s2 = L.ksb s := hw2.1.trans hks1 + have hcpu2 : L.cpu s2 = L.cpu s := hw2.2.1.trans hcpu1 + have hWoff : ∀ b, (∃ K : U64, 256 ≤ K.val ∧ K.val < 3328 ∧ b = wa t K) → ¬ FootA (L := L) Z s1 b := by + rintro b ⟨K, hK1, hK2, rfl⟩ hf + have hF : ∀ bb, FootA (L := L) Z s1 bb → FootA (L := L) Z s bb := by + have hna := nodeAt_ksb hks1 + intro bb hbb + cases hbb with + | slot y _ hy hw => exact .slot y _ hy hw + | nslot _ hn => + refine .nslot _ ?_ + cases hn with + | node c _ hc ho => simp only [hna] at ho; exact .node c _ hc ho + | cur _ ho => simp only [hcpu1, hna] at ho; exact .cur _ ho + | cur2 _ ho => simp only [hcpu1, hna] at ho; exact .cur2 _ ho + | tcb y _ hy ho => exact .tcb y _ hy ho + | tcb2 y _ hy ho => exact .tcb2 y _ hy ho + | sc c hc => exact .sc c hc + | sc2 c hc => exact .sc2 c hc + | irq i hi => rw [hks1] at *; exact .irq i hi + | glob _ ho => rw [hks1] at ho; exact .glob _ ho + | vcpu v _ hv hw => exact .vcpu v _ hv hw + | vs _ hb => rw [hks1] at hb; exact .vs _ hb + | ext _ hb => exact .ext _ hb + exact tcbReg_offFootA E hvcR hK1 hK2 (hF _ hf) + have hal1 : ∀ b, FootA (L := L) Z s1 b → Aligned b := + FootA.aligned J1.1 (fun c hc o ho => by rw [nodeAt_ksb hks1]; exact bxi_nodes E.sys.ent.bxi c hc o ho) + E.scAl E.ap.vcAl + have J2 : JAtZAX (L := L) AR Z s2 a σ2 m := J1.of_writesIn hw2 hWoff hal1 + have hOS2 : OSKeep st OS s1 s2 := + OSKeep.of_writes E.sys.ent.os hw2 fun b hb ⟨K, hK1, hK2, he⟩ => + tcbFld_notCapWA E hK1 (by omega) (he ▸ hb) + have hTr2 : TreeA st OS Z.G.chains Z.G.par s2 := treeA_of_mem hTr1 hOS2 + -- `scA`, `inqA`, `maxA` preserved across `hw2` + have hsc2f : L.mem s2 (scA t) = w := by + have h3464 : (3464#u64 : U64).val = 3464 := by simp + have h1 : L.mem s2 (wa t 3464#u64) = L.mem s1 (wa t 3464#u64) := + hw2.2.2 _ (field_aligned (K := 3464#u64) hta (by simp) (by simp)) + fun ⟨K, _, hK2, he⟩ => field_ne (K1 := 3464#u64) hta hta (by simp) (by omega) (fun _ e => by omega) he + exact h1.trans hsc1 + have hiq2f : (L.mem s2 (inqA t) &&& sched.Q_RELEASE) = 0#u64 := by + have h3592 : (3592#u64 : U64).val = 3592 := by simp + have h1 : L.mem s2 (wa t 3592#u64) = L.mem s1 (wa t 3592#u64) := + hw2.2.2 _ (field_aligned (K := 3592#u64) hta (by simp) (by simp)) + fun ⟨K, _, hK2, he⟩ => field_ne (K1 := 3592#u64) hta hta (by simp) (by omega) (fun _ e => by omega) he + show (L.mem s2 (wa t 3592#u64) &&& sched.Q_RELEASE) = 0#u64 + rw [h1]; exact hiq1 ▸ RS.cur.1 + have hrm2f : L.rmax s2 (L.mem s2 (scA t)) ≠ 0#u64 := by + rw [hsc2f] + show L.mem s2 (maxA w) ≠ 0#u64 + rw [hw2.2.2 _ hma (fun ⟨K, _, hK2, he⟩ => + out_ne_field (q.omega _ hmΩ) hct hta (K := K) (by omega) (congrArg UScalar.val he.symm))] + show L.rmax s1 w ≠ 0#u64 + rw [hrm1]; exact hrm + rw [set_thread_state_run_wrA hta (by rw [hsc2f]; exact hw0) hrm2f hiq2f (by rw [hsc2f]; exact hma) + (by rw [hsc2f]; exact hmx)] + have hOS3' : OSKeep st OS s2 (L.wr s2 (stA t) sched.TS_RUNNING) := + OSKeep.of_writes E.sys.ent.os (writesIn_wr sA) fun b hb he => by + rw [he, stA_eq] at hb + exact tcbFld_notCapWA E (by simp) (by simp) hb + refine (WP.spec_ok _).2 ⟨_, by rw [hspec]; exact Set.mem_singleton _, ?_, treeA_of_mem hTr2 hOS3', ?_⟩ + · have J2c : JAtZAX (L := L) AR Z s2 a (ctxUpdA σ2 (specPtr t) x2 uc') m := + J2.of_tcbCtxA (hcT ▸ hk2) uc' + have O2 : StOffA (L := L) Z s2 t := by + refine ⟨fun c hc o ho => by rw [nodeAt_ksb hks2]; exact O0.node c hc o ho, + O0.fld, fun b hb => by rw [hks2] at hb; exact O0.om b hb, + O0.slot, O0.vc, fun b hb => by rw [hks2] at hb; exact O0.vs b hb, O0.ext⟩ + have hal2 : ∀ b, FootA (L := L) Z s2 b → Aligned b := + FootA.aligned J2c.1 (fun c hc o ho => by rw [nodeAt_ksb hks2]; exact bxi_nodes E.sys.ent.bxi c hc o ho) + E.scAl E.ap.vcAl + have hk2c : (ctxUpdA σ2 (specPtr t) x2 uc').kheap (specPtr t) = + some (.tcb { x2 with tcbArch := { x2.tcbArch with tcbContext := uc' } }) := by + simp [ctxUpdA, setObjA] + simpa [hcT] using J2c.of_stA O2 htZ hk2c (ts := .running) (v := sched.TS_RUNNING) + (by simp [sched.TS_RUNNING, tsCodeA]) hal2 sA + · intro hC + have hOS3 : OSKeep st OS s1 (L.wr s2 (stA t) sched.TS_RUNNING) := fun y hy => + ⟨(hOS3' y hy).1.trans (hOS2 y hy).1, (hOS3' y hy).2.1.trans (hOS2 y hy).2.1, + (hOS3' y hy).2.2.1.trans (hOS2 y hy).2.2.1, (hOS3' y hy).2.2.2.trans (hOS2 y hy).2.2.2⟩ + have hty : TyKeepA σ2 (stUpdA (ctxUpdA σ2 σ.curThread x2 uc') σ.curThread + { x2 with tcbArch := { x2.tcbArch with tcbContext := uc' } } .running) := + (TyKeepA.ctxUpdA hk2 uc').trans (TyKeepA.stUpdA (by simp [ctxUpdA, setObjA]) .running) + exact ⟨hC.1.of_capsA rfl (fun _ h => h) (fun _ h => h) (fun y hy => Or.inl (by + have h' := hOS3 y (RS.sub y hy); unfold capAtA; rw [h'.1, h'.2.1])), + hC.2.of_keepA hty.1 hty.2 (fun y hy => by + have h' := hOS3 y ((hTr1.lk.members y).1 hy).1; unfold capAtA; rw [h'.1, h'.2.1]; exact CapFinEqA.refl _)⟩ + +end reply_frame + +/-! ## Label 57: `ARMPageGetAddress` -/ + +section getaddr +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- Spec `fl4ArchDecodeInvocation` at `ARMPageGetAddress` (plain or Stage-2 frame). -/ +theorem dec_pageGetAddrA (calling : Bool) (args : List Fl4SpecA64.Data) (xSlot : Fl4SpecA64.CapRef) + (cte : Fl4SpecA64.CslotPtr) (exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) + (e : Bool) (p : Fl4SpecA64.ObjRef) (R : Fl4SpecA64.CapRights) (sz : Fl4SpecA64.VMPageSize) (dev : Bool) + (mp : Option (Fl4SpecA64.Asid × Fl4SpecA64.VSpaceRef)) : + (Fl4SpecA64.fl4ArchDecodeInvocation (Z := Unit) calling (Fl4SpecA64.archL .armPageGetAddress) args xSlot cte + (absFrameE e p R sz dev mp) exs).run σ = + ({(.ok (.spec (.invokePage (.pageGetAddr p))), σ)}, False) := by + have h57 : Fl4SpecA64.invocationType (Fl4SpecA64.specLabelWord (Fl4SpecA64.archL .armPageGetAddress)) = + .archInvocationLabel .armPageGetAddress := invType_specA _ + have hfl : Fl4SpecA64.isPageFlushLabel (Fl4SpecA64.invocationType (Fl4SpecA64.specLabelWord + (Fl4SpecA64.archL .armPageGetAddress))) = false := by rw [h57]; rfl + cases e <;> + simp only [absFrameE, Fl4SpecA64.fl4ArchDecodeInvocation, hfl, Bool.false_eq_true, ↓reduceIte, + Fl4SpecA64.archDecodeInvocation, Fl4SpecA64.decodeFrameInvocation, h57, Fl4SpecA64.ArchCap.acapObj] <;> + exact run_liftME_okA rfl + +/-- FL4's `decode_frame` at `ARMPageGetAddress` (label 57). -/ +theorem decode_frame_getAddrA (ar : machine.Arch A) (length : U64) (slot : obj.Slot) + (fp rt asid va : U64) (sz : capability.FrameSize) (dev mp e call : Bool) (buf : U64) + (ex : Aeneas.Std.Array obj.Slot 3#usize) : + vspace.decode_frame st ar s a fl4_abi.label.ARMPageGetAddress length slot + (.Frame fp sz rt dev mp e asid va) call buf ex = + (do + let s1 ← sched.set_thread_state st s (L.curW s) sched.TS_RESTART + let i ← layout.paddr fp + let s2 ← vspace.reply1 st s1 (L.curW s) call i + ok (core.result.Result.Ok (), s2, a)) := by + have hne : fl4_abi.label.ARMPageGetAddress ≠ fl4_abi.label.ARMPageMap ∧ + fl4_abi.label.ARMPageGetAddress ≠ fl4_abi.label.ARMPageUnmap ∧ + vspace.is_cache_label fl4_abi.label.ARMPageGetAddress = ok false := by + refine ⟨fun h => by have := congrArg UScalar.val h; simp [fl4_abi.label.ARMPageGetAddress, + fl4_abi.label.ARMPageMap] at this, + fun h => by have := congrArg UScalar.val h; simp [fl4_abi.label.ARMPageGetAddress, + fl4_abi.label.ARMPageUnmap] at this, ?_⟩ + unfold vspace.is_cache_label + simp [fl4_abi.label.ARMPageGetAddress, fl4_abi.label.ARMPageClean_Data, + fl4_abi.label.ARMPageInvalidate_Data, fl4_abi.label.ARMPageCleanInvalidate_Data, + fl4_abi.label.ARMPageUnify_Instruction] + unfold vspace.decode_frame vspace.as_frame + simp only [cur_thread_ok, bind_ok, hne.1, hne.2.1, hne.2.2, Bool.false_eq_true, ↓reduceIte] + +/-- **Label 57 (`ARMPageGetAddress`) on `ArchTopPostA`** (module docstring): both plain and Stage-2 frames. -/ +theorem page_get_addr_jA {ar : machine.Arch A} + (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) + (hvcR : ∀ v ∈ Z.V, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → wa (L.curW s) K ≠ v ∧ wa (L.curW s) K ≠ wa v 24#u64) + (hTr : TreeA st OS Z.G.chains Z.G.par s) + (cte : Fl4SpecA64.CslotPtr) (fp rt asid va : U64) (sz : capability.FrameSize) (dev mp e : Bool) + (length buf : U64) (call : Bool) (ex : Aeneas.Std.Array obj.Slot 3#usize) + (args : List U64) (xSlot : Fl4SpecA64.CapRef) (exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) : + vspace.decode_frame st ar s a fl4_abi.label.ARMPageGetAddress length (cslotAddrA cte) + (.Frame fp sz rt dev mp e asid va) call buf ex ⦃ res => + ArchTopPostA (L := L) AR Z OS m call σ ((Fl4SpecA64.fl4ArchDecodeInvocation (Z := Unit) call + (Fl4SpecA64.archL .armPageGetAddress) (args.map UScalar.bv) xSlot cte + (absFrameE e (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev (mapDataA mp asid va)) exs).run σ) s a res ⦄ := by + rw [decode_frame_getAddrA, dec_pageGetAddrA] + obtain ⟨Rp, Nt, extra, sp, hP⟩ := RS.pl + obtain ⟨x, hk, hst, hset, E1, -, hOS1, -, -⟩ := E.restartA hP RS.sym RS.run RS.cur RS.vs RS.vc + rw [hst, bind_ok] + set s1 := L.wr s (stA (L.curW s)) sched.TS_RESTART + set σ1 := stUpdA σ σ.curThread x .restart + set pa := core.num.U64.wrapping_sub fp layout.PPTR_BASE + rw [show layout.paddr fp = ok pa from rfl, bind_ok] + have hpaSpec : pa.bv = Fl4SpecA64.addrFromPPtr (specPtr fp) := by + rw [addrFromPPtr_specPtr] + apply BitVec.eq_of_toNat_eq + have h1 : (core.num.U64.wrapping_sub fp layout.PPTR_BASE).bv.toNat = + (fp.val + (2 ^ 64 - layout.PPTR_BASE.val)) % 2 ^ 64 := by + change (core.num.U64.wrapping_sub fp layout.PPTR_BASE).val = _ + simp only [core.num.U64.wrapping_sub_val_eq, UScalar.size_UScalarTyU64, U64.size_def, U64.numBits, + UScalarTy.U64_numBits_eq] + have h2 : layout.PPTR_BASE.val = fl4PptrBaseA.toNat := by + unfold layout.PPTR_BASE fl4PptrBaseA; decide + change (core.num.U64.wrapping_sub fp layout.PPTR_BASE).bv.toNat = (fp.bv - fl4PptrBaseA).toNat + rw [h1, BitVec.toNat_sub, ← h2] + exact congrArg (· % 2 ^ 64) (Nat.add_comm _ _) + have hct : L.curW s ∈ H.tcbs := E.sq.curT + have hta : TcbAddr (L.curW s) := E.sq.tcbs _ hct + have sA : Aligned (stA (L.curW s)) := by rw [stA_eq]; exact field_aligned hta (by simp) (by simp) + have hks1 : L.ksb s1 = L.ksb s := L.ksb_wr _ _ _ + have hcpu1 : L.cpu s1 = L.cpu s := L.cpu_wr _ _ _ + have O0 := E.stOffA RS.vc + have hcur1 : L.curW s1 = L.curW s := by + show L.mem s1 (L.nodeAt s1 (L.cpu s1) layout.node.CUR_THREAD) = L.mem s (L.nodeAt s (L.cpu s) layout.node.CUR_THREAD) + rw [hcpu1, nodeAt_ksb hks1] + exact mem_wr_ne' sA (bxi_nodes E.sys.ent.bxi _ E.j.1.sch.cpuCs layout.node.CUR_THREAD + (by simp [layout.node.CUR_THREAD]) (by simp [layout.node.CUR_THREAD])) + (Ne.symm (O0.node _ E.j.1.sch.cpuCs layout.node.CUR_THREAD (by simp [layout.node.CUR_THREAD]))) + have hsc1 : L.mem s1 (scA (L.curW s)) = L.mem s (scA (L.curW s)) := + mem_wr_ne' sA (field_aligned (K := 3464#u64) hta (by simp) (by simp)) + (by rw [stA_eq]; exact field_ne (K2 := 3464#u64) hta hta (by simp) (by simp) (fun _ => by simp)) + have hiq1 : L.mem s1 (inqA (L.curW s)) = L.mem s (inqA (L.curW s)) := + mem_wr_ne' sA (field_aligned (K := 3592#u64) hta (by simp) (by simp)) + (by rw [stA_eq]; exact field_ne (K2 := 3592#u64) hta hta (by simp) (by simp) (fun _ => by simp)) + set w := L.mem s (scA (L.curW s)) + have hwC : w ∈ Z.C := E.j.1.rest.scMem (L.curW s) (by rw [← E.tcbs]; exact hct) RS.cur.2.1 + have hwH : w ∈ H.scs := by rw [E.scs]; exact hwC + have hmΩ : maxA w ∈ H.omega := E.sys.ent.bxi.2.2.2.2.sub _ (E.sys.ent.bxi.2.2.2.2.all _ hwH).2.1 + have hrm1 : L.rmax s1 w = L.rmax s w := + mem_wr_ne' sA (E.sq.omega _ hmΩ).1 + (fun e => O0.om _ (Or.inr (Or.inr (Or.inr ⟨w, hwC, Or.inr (maxA_eqA w)⟩))) e.symm) + have hTr1 : TreeA st OS Z.G.chains Z.G.par s1 := treeA_of_mem hTr hOS1 + have hk1 : σ1.kheap σ.curThread = some (.tcb { x with tcbState := .restart }) := by simp [σ1, stUpdA, setObjA] + have hrq1 : σ.curThread ∉ σ1.releaseQueue := by + have hq : specPtr (L.curW s) ∉ σ.releaseQueue := by + rw [E.j.1.rest.rel]; intro hm + obtain ⟨r, hr, er⟩ := List.mem_map.1 hm + have e' : r = L.curW s := specPtr_inj er; subst e' + have haf := E.sq.inR _ E.sq.cur _ hr + exact ((E.sq.released _ hct).2 (by rw [haf]; exact hr)) RS.cur.1 + simpa [σ1, stUpdA, setObjA, E.j.1.sch.curThread] using hq + have hscx : x.tcbSchedContext = some (scRenA m w) := by + obtain ⟨x', hk', -, hsx⟩ := E.j.1.sch.tcb (L.curW s) (by rw [← E.tcbs]; exact hct) + rw [E.j.1.sch.curThread] at hk; rw [hk'] at hk; cases hk + rw [hsx]; unfold optScA; simp [w, RS.cur.2.1] + have hy1 : ∃ y n, σ1.kheap (scRenA m w) = some (.schedContext y n) ∧ 0 < y.scRefillMax := by + obtain ⟨y, n, ey, eyr⟩ := E.j.1.rest.refillMax w hwC + have hrm : L.rmax s w ≠ 0#u64 := (E.sys.ent.bxi.2.2.2.2.act w hwH).1 RS.cur.2.2 + refine ⟨y, n, stUpd_keep hk ey (fun _ h => by cases h), ?_⟩ + rw [eyr] + have e2 : L.rmax s w = L.mem s (wa w 72#u64) := by unfold LawfulStore.rmax; rw [maxA_eqA] + rw [e2] at hrm + exact Nat.pos_of_ne_zero (fun e => hrm (UScalar.eq_of_val_eq (by simpa using e))) + refine WP.spec_bind (reply1_jA E RS hvcR E1.j hTr1 hks1 hcpu1 hcur1 hsc1 hiq1 hrm1 hk1 rfl hrq1 hscx hy1 call pa) + fun s2 ⟨σ3, hrep, J3, T3, C3⟩ => ?_ + refine (WP.spec_ok _).2 ⟨.spec (.invokePage (.pageGetAddr (specPtr fp))), okD_memA rfl, σ1, hset, + σ1, [Fl4SpecA64.addrFromPPtr (specPtr fp)], ?_, Or.inr ⟨σ3, ?_, J3, T3, ?_⟩⟩ + · exact mem_archPgA (by unfold Fl4SpecA64.performPageInvocation; exact Set.mem_singleton _) + · rw [← hpaSpec]; exact hrep + · intro hC + have hty := TyKeepA.stUpdA hk .restart + exact C3 ⟨hC.1.of_capsA rfl (fun _ h => h) (fun _ h => h) (fun y hy => Or.inl (by + have h' := hOS1 y (RS.sub y hy); unfold capAtA; rw [h'.1, h'.2.1])), + hC.2.of_keepA hty.1 hty.2 (fun y hy => by + have h' := hOS1 y ((hTr.lk.members y).1 hy).1; unfold capAtA; rw [h'.1, h'.2.1]; exact CapFinEqA.refl _)⟩ + +end getaddr + +/-! ## Clearing `D_AF` (bit 10) keeps `PteRelA`, `VsRelA`, `ExtVsRelA`, `JAtZAX` -/ + +section clear_af +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +theorem d_af_valA : vspace.D_AF = ok 1024#u64 := by + unfold vspace.D_AF + have h : (1#u64 <<< 10#i32) ⦃ r => r = 1024#u64 ⦄ := by + step as ⟨r, hr⟩ + apply UScalar.eq_of_val_eq + simpa [U64.size, U64.numBits] using hr + exact eq_of_spec_fd h + +private theorem clearAF_bitA (w : U64) (i : Nat) (hi : i ≠ 10) (h64 : i < 64) : + dBitA (w &&& ~~~1024#u64) i = dBitA w i := by + unfold dBitA + change (w.bv &&& ~~~(1024#64)).getLsbD i = w.bv.getLsbD i + rw [BitVec.getLsbD_and] + have h10 : (~~~(1024#64 : BitVec 64)).getLsbD i = true := by + rw [BitVec.getLsbD_not, decide_eq_true h64, Bool.true_and] + have : (1024#64 : BitVec 64).getLsbD i = false := by + simp only [BitVec.getLsbD, BitVec.toNat_ofNat] + rw [show (1024 % 2 ^ 64) = 2 ^ 10 from rfl, Nat.testBit_two_pow] + exact decide_eq_false (Ne.symm hi) + rw [this]; rfl + rw [h10, Bool.and_true] + +private theorem clearAF_dAddrA (w : U64) : dAddrA (w &&& ~~~1024#u64) = dAddrA w := by + unfold dAddrA + apply UScalar.eq_of_val_eq + have hbv : (w &&& ~~~1024#u64 &&& 281474976706560#u64).bv = (w &&& 281474976706560#u64).bv := by + ext i hi + change ((w.bv &&& ~~~(1024#64)) &&& 281474976706560#64)[i] = (w.bv &&& 281474976706560#64)[i] + rw [BitVec.getElem_and, BitVec.getElem_and, BitVec.getElem_and] + by_cases h10 : i = 10 + · subst h10 + have h0 : (281474976706560#64 : BitVec 64)[10] = false := by + rw [BitVec.getElem_eq_testBit_toNat, BitVec.toNat_ofNat]; rfl + simp [h0] + · have hnot : (~~~(1024#64 : BitVec 64))[i] = true := by + rw [BitVec.getElem_not, BitVec.getElem_eq_testBit_toNat, BitVec.toNat_ofNat] + rw [show (1024 % 2 ^ 64) = 2 ^ 10 from rfl, Nat.testBit_two_pow, decide_eq_false (Ne.symm h10)]; rfl + rw [hnot, Bool.and_true] + exact congrArg BitVec.toNat hbv + +private theorem clearAF_mod1024 (w : U64) : (w &&& ~~~1024#u64).val % 1024 = w.val % 1024 := by + have hbv : (w &&& ~~~1024#u64 &&& 1023#u64).bv = (w &&& 1023#u64).bv := by + ext i hi + change ((w.bv &&& ~~~(1024#64)) &&& 1023#64)[i] = (w.bv &&& 1023#64)[i] + rw [BitVec.getElem_and, BitVec.getElem_and, BitVec.getElem_and] + by_cases h10 : i = 10 + · subst h10 + have h0 : (1023#64 : BitVec 64)[10] = false := by + rw [BitVec.getElem_eq_testBit_toNat, BitVec.toNat_ofNat]; rfl + simp [h0] + · have hnot : (~~~(1024#64 : BitVec 64))[i] = true := by + rw [BitVec.getElem_not, BitVec.getElem_eq_testBit_toNat, BitVec.toNat_ofNat] + rw [show (1024 % 2 ^ 64) = 2 ^ 10 from rfl, Nat.testBit_two_pow, decide_eq_false (Ne.symm h10)]; rfl + rw [hnot, Bool.and_true] + have h1 := congrArg BitVec.toNat hbv + have hL : (w &&& ~~~1024#u64 &&& 1023#u64).val = (w &&& ~~~1024#u64).val % 1024 := + and_maskA _ 10 _ rfl + have hR : (w &&& 1023#u64).val = w.val % 1024 := and_maskA _ 10 _ rfl + rw [← hL, ← hR]; exact h1 + +/-- Clearing `D_AF` (bit 10) keeps `PteRelA` at every depth. -/ +theorem pteRelA_clearAF {Tb : Set U64} {Lv : U64 → Nat} {k : Nat} {w : U64} {pte : Fl4SpecA64.PTE} + (h : PteRelA Tb Lv k w pte) : PteRelA Tb Lv k (w &&& ~~~1024#u64) pte := by + have hm := clearAF_mod1024 w + have hap : apA (w &&& ~~~1024#u64) = apA w := by unfold apA; omega + have hat : attrIdxA (w &&& ~~~1024#u64) = attrIdxA w := by unfold attrIdxA; omega + cases pte with + | invalidPTE => rw [PteRelA, clearAF_bitA w 0 (by decide) (by decide)]; exact h + | pageTablePTE ppn => + obtain ⟨hk, h0, h1, h44, hp, hT, hL⟩ := h + refine ⟨hk, by rw [clearAF_bitA w 0 (by decide) (by decide)]; exact h0, + by rw [clearAF_bitA w 1 (by decide) (by decide)]; exact h1, + by rw [clearAF_dAddrA]; exact h44, + by unfold ppnA; rw [clearAF_dAddrA]; exact hp, + by unfold dTabA; rw [clearAF_dAddrA]; exact hT, + by unfold dTabA; rw [clearAF_dAddrA]; exact hL⟩ + | pagePTE pa sm at' rt => + obtain ⟨hk1, hk3, h0, h1, hsm, hpa, h44, hrt, ⟨hex, hdv, hgl⟩⟩ := h + refine ⟨hk1, hk3, by rw [clearAF_bitA w 0 (by decide) (by decide)]; exact h0, + by rw [clearAF_bitA w 1 (by decide) (by decide)]; exact h1, hsm, + by unfold pageBaseA; rw [clearAF_dAddrA]; exact hpa, + by rw [clearAF_dAddrA]; exact h44, + by unfold pteRightsA; rw [hap]; exact hrt, + ⟨by rw [clearAF_bitA w 54 (by decide) (by decide)]; exact hex, + by rw [hat]; exact hdv, hgl⟩⟩ + +/-- Clearing `D_AF` on a Stage-1 table word or high-window L1 word keeps `VsRelA`, `ExtVsRelA`, `JAtZAX`, `TreeA`, +and `CarryA` with unchanged spec state `σ`. -/ +theorem jat_clearAF (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) + (hTO : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s q) (hEO : ExtTabOffA (L := L) Z m s) + (hTr : TreeA st OS Z.G.chains Z.G.par s) + {P j : U64} (hP : P ∈ Z.S1 ∨ ∃ y ∈ Z.Y, P = wa y 4096#u64) (hj : j.val < 512) : + let sl := peAF P j + let s1 := L.wr s sl (L.mem s sl &&& ~~~1024#u64) + JAtZAX (L := L) AR Z s1 a σ m ∧ + TreeA st OS Z.G.chains Z.G.par s1 ∧ + (CarryA Z (L.mem s) σ → CarryA Z (L.mem s1) σ) ∧ + L.ksb s1 = L.ksb s ∧ L.cpu s1 = L.cpu s ∧ L.curW s1 = L.curW s ∧ + L.mem s1 (scA (L.curW s)) = L.mem s (scA (L.curW s)) ∧ + L.mem s1 (inqA (L.curW s)) = L.mem s (inqA (L.curW s)) ∧ + L.rmax s1 (L.mem s (scA (L.curW s))) = L.rmax s (L.mem s (scA (L.curW s))) := by + intro sl s1 + have R := E.j.1.vs + have hPa : 4096 ∣ P.val := by + rcases hP with h | ⟨y, hy, rfl⟩ + · exact R.al P h + · exact Nat.dvd_of_mod_eq_zero (waWin_modA (R.yal y hy)) + have hPvs : P ∈ VsPtA Z.Pp Z.S1 Z.Y := by + rcases hP with h | ⟨y, hy, rfl⟩ + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr ⟨y, hy, rfl⟩) + have hslAl : Aligned sl := peAF_alignedA (Nat.mod_eq_zero_of_dvd hPa) hj + have hslFoot : VsFootA Z.Pp Z.S1 Z.Y (L.ksb s) sl := by + rcases hP with h | ⟨y, hy, rfl⟩ + · exact .pt P j h hj + · exact .high y j hy hj + have hks1 : L.ksb s1 = L.ksb s := L.ksb_wr _ _ _ + have hcpu1 : L.cpu s1 = L.cpu s := L.cpu_wr _ _ _ + have hm : ∀ w, Aligned w → w ≠ sl → L.mem s1 w = L.mem s w := fun w hw hne => + mem_wr_ne' hslAl hw hne.symm + have hnotF : ¬ FootA (L := L) (extZA Z) s sl := by + rcases hP with h | ⟨y, hy, rfl⟩ + · intro hf + have hF : ∀ bb, FootA (L := L) (extZA Z) s bb → FootA (L := L) { Z with S1 := ∅ } s bb := by + intro bb hbb + cases hbb with + | slot y _ hy hw => exact .slot y _ hy hw + | nslot _ hn => + refine .nslot _ ?_ + cases hn with + | node c _ hc ho => exact .node c _ hc ho + | cur _ ho => exact .cur _ ho + | cur2 _ ho => exact .cur2 _ ho + | tcb y _ hy ho => exact .tcb y _ hy ho + | tcb2 y _ hy ho => exact .tcb2 y _ hy ho + | sc c hc => exact .sc c hc + | sc2 c hc => exact .sc2 c hc + | irq i hi => exact .irq i hi + | glob _ ho => exact .glob _ ho + | vcpu v _ hv hw => exact .vcpu v _ hv hw + | vs _ hb => + cases hb with + | asid hi hh => exact .vs _ (.asid hi hh) + | pool p lo hp hl => exact .vs _ (.pool p lo hp hl) + | pt _ _ hp _ => exact absurd hp (Set.notMem_empty _) + | high _ _ hy _ => exact absurd hy (Set.notMem_empty _) + | ext _ hb => + cases hb with + | s2lo _ _ hr _ => exact absurd hr (Set.notMem_empty _) + | s2hi _ _ hr _ => exact absurd hr (Set.notMem_empty _) + | s2pt _ _ ht _ => exact absurd ht (Set.notMem_empty _) + | wl0 _ hy => exact absurd hy (Set.notMem_empty _) + | wlo _ _ hy _ => exact absurd hy (Set.notMem_empty _) + exact (hTO P h).1 j hj (hF _ hf) + · exact hEO.mem _ (Or.inr ⟨y, hy, rfl⟩) j hj + -- `VsRelA` preserved + have hK : Aligned (L.ksb s) := by unfold Aligned; have := E.j.1.ksbAl; omega + have R1 : VsRelA Z.Pp Z.S1 Z.Lv Z.X Z.Y (L.mem s1) (L.ksb s) σ := by + have hnotAt : ∀ hi : U64, hi.val < 128 → atAF (L.ksb s) hi ≠ sl := fun hi hhi e => + hnotF (e ▸ .nslot _ (.vs _ (.asid hi hhi))) + have hnotPp : ∀ p ∈ Z.Pp, ∀ lo : U64, lo.val < 512 → peAF p lo ≠ sl := fun p hp lo hlo e => + hnotF (e ▸ .nslot _ (.vs _ (.pool p lo hp hlo))) + have hActP : ∀ {p}, ActivePoolA (L.mem s1) (L.ksb s) p → ActivePoolA (L.mem s) (L.ksb s) p := by + rintro p ⟨hpne, hi, hhi, htab⟩ + rw [hm _ (R.foot_alignedA hK _ (.asid hi hhi)) (hnotAt hi hhi)] at htab + exact ⟨hpne, hi, hhi, htab⟩ + have hActR : ∀ {asid raw}, ActiveAsidRootA (L.mem s1) (L.ksb s) Z.Pp asid raw → + ActiveAsidRootA (L.mem s) (L.ksb s) Z.Pp asid raw := by + rintro asid raw ⟨hi, lo, hhi, hlo, ha', pool, hp, hpne, htab, hw, hrne⟩ + rw [hm _ (R.foot_alignedA hK _ (.asid hi hhi)) (hnotAt hi hhi)] at htab + rw [hm _ (peAF_alignedA (Nat.mod_eq_zero_of_dvd (R.pal pool hp)) hlo) (hnotPp pool hp lo hlo)] at hw + exact ⟨hi, lo, hhi, hlo, ha', pool, hp, hpne, htab, hw, hrne⟩ + have hActH : ∀ {y}, ActiveHighRootA (L.mem s1) (L.ksb s) Z.Pp y → ActiveHighRootA (L.mem s) (L.ksb s) Z.Pp y := by + rintro y ⟨asid, raw, ha', htag, hy⟩ + exact ⟨asid, raw, hActR ha', htag, hy⟩ + have hClear3 : ∀ w : U64, ((w &&& ~~~1024#u64) &&& 3#u64) = (w &&& 3#u64) := by + intro w + apply UScalar.eq_of_val_eq + rw [and3_valA, and3_valA, ← Nat.mod_mod_of_dvd (w &&& ~~~1024#u64).val (by decide : 4 ∣ 1024), + clearAF_mod1024, Nat.mod_mod_of_dvd w.val (by decide : 4 ∣ 1024)] + have hClearTab : ∀ w : U64, dTabA (w &&& ~~~1024#u64) = dTabA w := by + intro w; unfold dTabA; rw [clearAF_dAddrA] + have hPath : ∀ {tag sh q}, VsPathA Z.Pp Z.S1 Z.Y (L.mem s1) (L.ksb s) tag sh q → + VsPathA Z.Pp Z.S1 Z.Y (L.mem s) (L.ksb s) tag sh q := by + intro tag sh q hpth + induction hpth with + | native ha' htag htb => exact .native (hActR ha') htag htb + | high ha' htag hy => exact .high (hActR ha') htag hy + | @child tag tb i sh _ htY h12 hi h3 ih => + have hpeAl : Aligned (peAF tb i) := by + rcases htY with htb | ⟨y, hy, rfl⟩ + · exact peAF_alignedA (R.al_mod htb) hi + · exact peAF_alignedA (waWin_modA (R.yal y hy)) hi + by_cases e : peAF tb i = sl + · rw [e, L.mem_wr_same hslAl, hClear3, ← e] at h3 + rw [e, L.mem_wr_same hslAl, hClearTab, ← e] + exact .child ih htY h12 hi h3 + · rw [hm _ hpeAl e] at h3 ⊢ + exact .child ih htY h12 hi h3 + have hReach : ∀ {q}, VsReachA Z.Pp Z.S1 Z.Y (L.mem s1) (L.ksb s) q → + VsReachA Z.Pp Z.S1 Z.Y (L.mem s) (L.ksb s) q := by + rintro q ⟨tag, sh, htag, hpth⟩ + exact ⟨tag, sh, htag, hPath hpth⟩ + refine ⟨fun hi hhi => by rw [hm _ (R.foot_alignedA hK _ (.asid hi hhi)) (hnotAt hi hhi)]; exact R.table hi hhi, + fun hi hhi => by rw [hm _ (R.foot_alignedA hK _ (.asid hi hhi)) (hnotAt hi hhi)]; exact R.tmem hi hhi, + fun p hp => by + obtain ⟨f, hf, hfe⟩ := R.pool p hp + exact ⟨f, hf, fun ha lo hlo => by + rw [hm _ (peAF_alignedA (Nat.mod_eq_zero_of_dvd (R.pal p hp)) hlo) (hnotPp p hp lo hlo)] + exact hfe (hActP ha) lo hlo⟩, + fun p hp ha lo hlo => by + rw [hm _ (peAF_alignedA (Nat.mod_eq_zero_of_dvd (R.pal p hp)) hlo) (hnotPp p hp lo hlo)] + exact R.kind p hp (hActP ha) lo hlo, + fun h0 => by + have e0 := hm _ (R.foot_alignedA hK _ (.asid 0#u64 (by simp))) (hnotAt 0#u64 (by simp)) + rw [e0] at h0 ⊢ + have hp0 := R.tmem 0#u64 (by simp) h0 + rw [hm _ (peAF_alignedA (Nat.mod_eq_zero_of_dvd (R.pal _ hp0)) (by simp)) (hnotPp _ hp0 0#u64 (by simp))] + exact R.asid0 h0, + fun p hp => by + obtain ⟨pt, hpt, hty, hpe⟩ := R.pt p hp + refine ⟨pt, hpt, hty, fun hrc i hi => ?_⟩ + by_cases e : peAF p i = sl + · rw [e, L.mem_wr_same hslAl, ← e]; exact pteRelA_clearAF (hpe (hReach hrc) i hi) + · rw [hm _ (peAF_alignedA (Nat.mod_eq_zero_of_dvd (R.al p hp)) hi) e]; exact hpe (hReach hrc) i hi, + fun y hy => by + obtain ⟨l1, hl1, hle⟩ := R.high y hy + refine ⟨l1, hl1, fun ha i hi => ?_⟩ + by_cases e : peAF (wa y 4096#u64) i = sl + · rw [e, L.mem_wr_same hslAl, ← e]; exact pteRelA_clearAF (hle (hActH ha) i hi) + · rw [hm _ (peAF_alignedA (waWin_modA (R.yal y hy)) hi) e]; exact hle (hActH ha) i hi, + R.lv, R.al, R.pal, R.yal, R.ytb⟩ + -- `ExtVsRelA` preserved + have E1 : ExtVsRelA Z.Pp Z.S1 Z.X Z.Y Z.T2 Z.Sh2 (L.mem s1) σ := + E.j.1.ext.of_mem fun b hb => hm b (E.j.1.ext.foot_alignedE R.yal b hb) (by + obtain ⟨q', j', hj', rfl, hq', hc⟩ := ExtFootE.pgE E.j.1.ext R.yal hb + refine pgNeE hq' hPa hj' hj fun e => ?_ + subst e + rcases hc with h | h + · exact (E.j.1.ext.pgd _ h).1 hPvs + · exact E.j.1.ext.ypg _ h hPvs) + -- `JAtZAX` via `JAtZAX.of_extPageA` + have hal : ∀ w, FootA (L := L) Z s w → Aligned w := + FootA.aligned E.j.1 (bxi_nodes E.sys.ent.bxi) E.scAl E.ap.vcAl + have hq : P ∈ S2PgE Z.X Z.T2 ∨ (∃ y ∈ Z.Y, P = wa y 4096#u64) ∨ P ∈ Z.S1 := by + rcases hP with h | ⟨y, hy, rfl⟩ + · exact Or.inr (Or.inr h) + · exact Or.inr (Or.inl ⟨y, hy, rfl⟩) + have hp0 : ∃ p0, (p0 ∈ Z.X ∨ p0 ∈ Z.Y ∨ p0 ∈ Z.T2 ∨ p0 ∈ Z.S1) := by + rcases hP with h | ⟨y, hy, _⟩ + · exact ⟨P, Or.inr (Or.inr (Or.inr h))⟩ + · exact ⟨y, Or.inr (Or.inl hy)⟩ + obtain ⟨p0, hp0⟩ := hp0 + have J1 : JAtZAX (L := L) AR Z s1 a σ m := + E.j.of_extPageA hal hTO hEO hq hks1 hcpu1 (fun w hw hne => hm w hw (hne j hj)) + hp0 (kh := σ.kheap) (fun _ _ => rfl) (by rw [hks1]; exact R1) E1 + -- `OSKeep` (since `sl` is in `VsFootA`, which is off `OS` by `E.ap.vs`) + have hOS : OSKeep st OS s s1 := osKeep_of_wr_notInOS E.sys.ent.os hslAl (E.ap.vs sl hslFoot).1 + have hTr1 : TreeA st OS Z.G.chains Z.G.par s1 := treeA_of_mem hTr hOS + have hC : CarryA Z (L.mem s) σ → CarryA Z (L.mem s1) σ := fun hc => + ⟨hc.1.of_capsA rfl (fun _ h => h) (fun _ h => h) (fun y hy => Or.inl (by + have h' := hOS y (RS.sub y hy); unfold capAtA; rw [h'.1, h'.2.1])), + hc.2.of_keepA (TyKeepA.refl σ).1 (TyKeepA.refl σ).2 (fun y hy => by + have h' := hOS y ((hTr.lk.members y).1 hy).1; unfold capAtA; rw [h'.1, h'.2.1]; exact CapFinEqA.refl _)⟩ + have hct : L.curW s ∈ H.tcbs := E.sq.curT + have hta : TcbAddr (L.curW s) := E.sq.tcbs _ hct + have htZ : L.curW s ∈ Z.T := by rw [← E.tcbs]; exact hct + have hcur1 : L.curW s1 = L.curW s := by + show L.mem s1 (L.nodeAt s1 (L.cpu s1) layout.node.CUR_THREAD) = L.mem s (L.nodeAt s (L.cpu s) layout.node.CUR_THREAD) + rw [hcpu1, nodeAt_ksb hks1] + exact hm _ (bxi_nodes E.sys.ent.bxi _ E.j.1.sch.cpuCs layout.node.CUR_THREAD + (by simp [layout.node.CUR_THREAD]) (by simp [layout.node.CUR_THREAD])) + (fun e => hnotF (e ▸ .nslot _ (.node _ _ E.j.1.sch.cpuCs (Or.inl rfl)))) + have hsc1 : L.mem s1 (scA (L.curW s)) = L.mem s (scA (L.curW s)) := + hm _ (field_aligned (K := 3464#u64) hta (by simp) (by simp)) + (fun e => hnotF (e ▸ .nslot _ (.tcb _ _ htZ (Or.inr (Or.inl rfl))))) + have hiq1 : L.mem s1 (inqA (L.curW s)) = L.mem s (inqA (L.curW s)) := by + refine hm _ (field_aligned (K := 3592#u64) hta (by simp) (by simp)) fun e => ?_ + have ev := tcb_wadd_val hta (o := 3592#u64) (by simp) + rw [show inqA (L.curW s) = wa (L.curW s) 3592#u64 from rfl] at e + rw [e] at ev + have h3592 : (3592#u64 : U64).val = 3592 := by simp + rw [h3592] at ev + rcases (E.ap.vs sl hslFoot).2 _ hct with h | h <;> omega + have hrm1 : L.rmax s1 (L.mem s (scA (L.curW s))) = L.rmax s (L.mem s (scA (L.curW s))) := by + have hwC : L.mem s (scA (L.curW s)) ∈ Z.C := E.j.1.rest.scMem _ htZ RS.cur.2.1 + have hwH : L.mem s (scA (L.curW s)) ∈ H.scs := by rw [E.scs]; exact hwC + have hmΩ : maxA (L.mem s (scA (L.curW s))) ∈ H.omega := + E.sys.ent.bxi.2.2.2.2.sub _ (E.sys.ent.bxi.2.2.2.2.all _ hwH).2.1 + show L.mem s1 (maxA _) = L.mem s (maxA _) + rw [maxA_eqA] + exact hm _ (by rw [← maxA_eqA]; exact (E.sq.omega _ hmΩ).1) (fun e => hnotF (e ▸ .nslot _ (.sc2 _ hwC))) + exact ⟨J1, hTr1, hC, hks1, hcpu1, hcur1, hsc1, hiq1, hrm1⟩ + +end clear_af + +/-! ## Label 56: `ARMPageTestAndClearAccessed` -/ + +section test_clear +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +theorem lookupFrame_of_pteA (vs : Fl4SpecA64.ObjRef) (va : Fl4SpecA64.VSpaceRef) (ps : Fl4SpecA64.PtesOf) : + Fl4SpecA64.lookupFrame vs va ps = + (Fl4SpecA64.lookupFramePte vs va ps).map fun x => (x.1, Fl4SpecA64.pteBaseAddr x.2) := by + unfold Fl4SpecA64.lookupFrame Fl4SpecA64.lookupFramePte + simp only [Bind.bind, ReaderT.bind, Option.bind] + cases Fl4SpecA64.ptLookupSlot vs va ps with + | none => rfl + | some p => + dsimp only + cases Fl4SpecA64.oapply2 (Fl4SpecA64.levelType p.1) p.2 ps with + | none => rfl + | some pte => + dsimp only + unfold Fl4Spec.Lookup.oassert Fl4Spec.Lookup.oreturn + by_cases h1 : pte.isPagePTE = true <;> by_cases h2 : p.1 ≤ 2 <;> + simp [h1, h2, Fl4Spec.Lookup.ofail] + +private theorem mapped_entry_eq_lu {f : vspace.FrameCap} {root : U64} {lu0 lu : vspace.PtSlot} + (hlu : vspace.lookup_pt_slot st s root f.vaddr = ok lu0) + (h : vspace.mapped_entry st s f root = ok (some lu)) : lu = lu0 := by + unfold vspace.mapped_entry at h + rw [hlu, bind_ok, L.read_eq, bind_ok, page_shift_eqA, bind_ok] at h + by_cases h1 : (lu0.shift != psA f.size) = true + · rw [if_pos h1] at h; exact absurd (okinj h) (by simp) + rw [if_neg h1] at h + have hsh : lu0.shift = psA f.size := by + by_contra hne; exact h1 (bne_iff_ne.2 hne) + have hk : ∃ k : Nat, 1 ≤ k ∧ k ≤ 3 ∧ lu0.shift.val = 39 - 9 * k := by + rw [hsh]; rcases f.size with _ | _ | _ + · exact ⟨3, by decide, by decide, by simp [psA]⟩ + · exact ⟨2, by decide, by decide, by simp [psA]⟩ + · exact ⟨1, by decide, by decide, by simp [psA]⟩ + obtain ⟨k, hk1, hk3, hkv⟩ := hk + rw [is_page_shA (k := k) (by omega) (by omega), bind_ok] at h + by_cases hb : isPageA (L.mem s lu0.slot) k = true + · rw [if_pos hb, page_base_shA (k := k) (by unfold shiftA; omega), bind_ok, + show layout.paddr f.ptr = ok (core.num.U64.wrapping_sub f.ptr layout.PPTR_BASE) from rfl, bind_ok] at h + by_cases h4 : ((⟨pageBaseA (L.mem s lu0.slot) k⟩ : U64) != core.num.U64.wrapping_sub f.ptr layout.PPTR_BASE) = true + · rw [if_pos h4] at h; exact absurd (okinj h) (by simp) + rw [if_neg h4] at h + exact (Option.some.inj (okinj h)).symm + · rw [if_neg hb] at h; exact absurd (okinj h) (by simp) + +/-- Slot location of `vspace.mapped_entry` in Stage-1 tables. -/ +theorem mapped_entry_s1_slotA {Pp Tb X Y : Set U64} {Lv : U64 → Nat} + (R : VsRelA Pp Tb Lv X Y (L.mem s) (L.ksb s) σ) {w : U64} (hw : w ∈ Tb) (h0 : Lv w = 0) + (hreach : VsReachA Pp Tb Y (L.mem s) (L.ksb s) w) + (f : vspace.FrameCap) {lu : vspace.PtSlot} (h : vspace.mapped_entry st s f w = ok (some lu)) : + ∃ P j, P ∈ Tb ∧ j.val < 512 ∧ lu.slot = peAF P j := by + obtain ⟨lu0, hlu, p, hp, -, -, -, -, hsl, -, -⟩ := spec_ex_fd (lookup_pt_slotA R hw h0 hreach f.vaddr) + rw [mapped_entry_eq_lu hlu h] + exact ⟨p, idxA f.vaddr lu0.shift, hp, idxA_lt _ _, hsl⟩ + +/-- Slot location of `vspace.mapped_entry` in a high window `y ∈ Y`. -/ +theorem mapped_entry_hi_slotE {Pp Tb X Y T2 : Set U64} {Lv : U64 → Nat} {Sh2 : U64 → Nat} + (R : VsRelA Pp Tb Lv X Y (L.mem s) (L.ksb s) σ) (EX : ExtVsRelA Pp Tb X Y T2 Sh2 (L.mem s) σ) + {y : U64} (hy : y ∈ Y) (hact : ActiveHighRootA (L.mem s) (L.ksb s) Pp y) + (f : vspace.FrameCap) (hva : VaOkE f.vaddr) {lu : vspace.PtSlot} + (h : vspace.mapped_entry st s f y = ok (some lu)) : + ∃ P j, (P ∈ Tb ∨ ∃ y' ∈ Y, P = wa y' 4096#u64) ∧ j.val < 512 ∧ lu.slot = peAF P j := by + rcases vaIdxE hva with hlo | hhi + · unfold vspace.mapped_entry at h + have hne : (39#u64 : U64) ≠ psA f.size := fun e => by + have h39 : (psA f.size).val = 39 := by rw [← e]; rfl + rcases psA_valA f.size with h' | h' | h' <;> omega + rw [lookup_l0_zeroE (EX.wlo _ hy _ hlo), bind_ok, L.read_eq, bind_ok, page_shift_eqA, bind_ok] at h + dsimp only at h + rw [if_pos (bne_iff_ne.2 hne)] at h + exact absurd (okinj h) (by simp) + · obtain ⟨hb0, hb1, htab⟩ := EX.wl0 _ hy + have hP0 : True → (L.mem s (peAF y (idxA f.vaddr 39#u64)) &&& 3#u64) = 3#u64 ∧ + ∀ s1 l1 : U64, s1.val = 30 → l1.val = 2 → + WkE (G1ReachE Pp Tb Y Lv (L.mem s) (L.ksb s)) (TopHiReachE Pp Y (L.mem s) (L.ksb s)) σ f.vaddr + (Fl4SpecA64.extLookupSlot σ Fl4SpecA64.ExtRootKind.high.geom (specPtr y) f.vaddr.bv) + (s1, peAF (dTabA (L.mem s (peAF y (idxA f.vaddr 39#u64)))) (idxA f.vaddr s1), l1) := fun _ => by + rw [hhi] + refine ⟨(and3_bitsA _).2 ⟨hb0, hb1⟩, fun s1 l1 hs1 hl1 => ⟨Fl4SpecA64.extTopPos Fl4SpecA64.ExtRootKind.high.geom + (specPtr y) f.vaddr.bv, by rw [hl1]; rfl, hs1.symm, Or.inr ⟨y, idxA f.vaddr s1, hy, hact, idxA_lt _ _, + by rw [htab], ?_, rfl⟩⟩⟩ + show Fl4SpecA64.ExtSlot.root _ ((f.vaddr.bv.toNat >>> 30) % (511 + 1)) = _ + rw [idxA_val, hs1, Nat.shiftRight_eq_div_pow] + rfl + obtain ⟨⟨lsh, lsl⟩, hlu, hpost⟩ := spec_ex_fd (lookup_loopE (st := st) R.goodE (topOkHiE R) f.vaddr _ True _ hP0 + 39#u64 _ 3#u64 (Or.inr ⟨trivial, rfl⟩)) + have hlk : vspace.lookup_pt_slot st s y f.vaddr = ok { slot := lsl, shift := lsh } := by + unfold vspace.lookup_pt_slot + rw [show vspace.TOP_LEVEL_SHIFT = 39#u64 by unfold vspace.TOP_LEVEL_SHIFT; rfl, entry_addr_eqA _ _ _ (by simp), + bind_ok, hlu, bind_ok] + all_goals rfl + rw [mapped_entry_eq_lu hlk h] + rcases hpost.2 with ⟨t, j, ⟨⟨ht, -⟩, -⟩, hj, hslot, -⟩ | ⟨y', j, hy', -, hj, hslot, -, -⟩ + · exact ⟨t, j, Or.inl ht, hj, hslot⟩ + · exact ⟨wa y' 4096#u64, j, Or.inr ⟨y', hy', rfl⟩, hj, hslot⟩ + +/-- FL4's `decode_frame` at `ARMPageTestAndClearAccessed` (label 56). -/ +theorem decode_frame_testClearA (ar : machine.Arch A) (length : U64) (slot : obj.Slot) + (fp rt asid va : U64) (sz : capability.FrameSize) (dev mp e call : Bool) (buf : U64) + (ex : Aeneas.Std.Array obj.Slot 3#usize) : + vspace.decode_frame st ar s a fl4_abi.label.ARMPageTestAndClearAccessed length slot + (.Frame fp sz rt dev mp e asid va) call buf ex = + vspace.decode_test_and_clear_accessed st ar s a (frCapA fp sz rt dev mp e asid va) call := by + have hne : fl4_abi.label.ARMPageTestAndClearAccessed ≠ fl4_abi.label.ARMPageMap ∧ + fl4_abi.label.ARMPageTestAndClearAccessed ≠ fl4_abi.label.ARMPageUnmap ∧ + fl4_abi.label.ARMPageTestAndClearAccessed ≠ fl4_abi.label.ARMPageGetAddress ∧ + vspace.is_cache_label fl4_abi.label.ARMPageTestAndClearAccessed = ok false := by + refine ⟨fun h => by have := congrArg UScalar.val h; simp [fl4_abi.label.ARMPageTestAndClearAccessed, + fl4_abi.label.ARMPageMap] at this, + fun h => by have := congrArg UScalar.val h; simp [fl4_abi.label.ARMPageTestAndClearAccessed, + fl4_abi.label.ARMPageUnmap] at this, + fun h => by have := congrArg UScalar.val h; simp [fl4_abi.label.ARMPageTestAndClearAccessed, + fl4_abi.label.ARMPageGetAddress] at this, ?_⟩ + unfold vspace.is_cache_label + simp [fl4_abi.label.ARMPageTestAndClearAccessed, fl4_abi.label.ARMPageClean_Data, + fl4_abi.label.ARMPageInvalidate_Data, fl4_abi.label.ARMPageCleanInvalidate_Data, + fl4_abi.label.ARMPageUnify_Instruction] + unfold vspace.decode_frame vspace.as_frame frCapA + simp [cur_thread_ok, bind_ok, hne.1, hne.2.1, hne.2.2.1, hne.2.2.2] + +/-- Spec `fl4ArchDecodeInvocation` at `ARMPageTestAndClearAccessed` on a Stage-2 frame cap. -/ +theorem dec_testClearS2A (calling : Bool) (args : List Fl4SpecA64.Data) (xSlot : Fl4SpecA64.CapRef) + (cte : Fl4SpecA64.CslotPtr) (exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) + (p : Fl4SpecA64.ObjRef) (R : Fl4SpecA64.CapRights) (sz : Fl4SpecA64.VMPageSize) (dev : Bool) + (mp : Option (Fl4SpecA64.Asid × Fl4SpecA64.VSpaceRef)) : + (Fl4SpecA64.fl4ArchDecodeInvocation (Z := Unit) calling (.ext .armPageTestAndClearAccessed) args xSlot cte + (.ext (.stage2FrameCap p R sz dev mp)) exs).run σ = + ({(.error .illegalOperation, σ)}, False) := by + simp only [Fl4SpecA64.fl4ArchDecodeInvocation, show (.ext .armPageTestAndClearAccessed : Fl4SpecA64.Fl4Label) ≠ + .ext .armPageDeriveSubframe by decide, ↓reduceIte] + exact run_liftME_errA (show (Fl4SpecA64.decodeExtTestAndClearAccessed (Z := Unit) + (.ext (.stage2FrameCap p R sz dev mp))).run σ = ({(.error .illegalOperation, σ)}, False) from rfl) + +/-- Spec `fl4ArchDecodeInvocation` at `ARMPageTestAndClearAccessed` on a plain frame cap with Stage-1 / no root. -/ +theorem dec_testClearS1A (calling : Bool) (args : List Fl4SpecA64.Data) (xSlot : Fl4SpecA64.CapRef) + (cte : Fl4SpecA64.CslotPtr) (exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) + (p : Fl4SpecA64.ObjRef) (R : Fl4SpecA64.CapRights) (sz : Fl4SpecA64.VMPageSize) (dev : Bool) + (mp : Option (Fl4SpecA64.Asid × Fl4SpecA64.VSpaceRef)) + (hk : ∀ asid va, mp = some (asid, va) → Fl4SpecA64.asidRootKind σ asid = none ∨ + Fl4SpecA64.asidRootKind σ asid = some .stage1) : + (Fl4SpecA64.fl4ArchDecodeInvocation (Z := Unit) calling (.ext .armPageTestAndClearAccessed) args xSlot cte + (.frameCap p R sz dev mp) exs).run σ = + (Fl4Spec.NonDetE.liftME Fl4SpecA64.Fl4ArchInvocation.testAndClearAccessed + (Fl4SpecA64.decodeTestAndClearAccessed (Z := Unit) (.frameCap p R sz dev mp))).run σ := by + simp only [Fl4SpecA64.fl4ArchDecodeInvocation, show (.ext .armPageTestAndClearAccessed : Fl4SpecA64.Fl4Label) ≠ + .ext .armPageDeriveSubframe by decide, ↓reduceIte] + cases mp with + | none => + rw [run_bind_okD (show (Fl4Spec.NonDetE.returnOk none : Fl4SpecA64.SEMonad Unit _).run σ = ({(.ok none, σ)}, False) from rfl)] + | some av => + obtain ⟨asid, va⟩ := av + exact (run_bind_okD (run_liftE_stD (extKindOfAsid_noneA (hk asid va rfl))) _).trans rfl + +/-- Spec `fl4ArchDecodeInvocation` at `ARMPageTestAndClearAccessed` on a plain frame cap with an Ext root. -/ +theorem dec_testClearExtA (calling : Bool) (args : List Fl4SpecA64.Data) (xSlot : Fl4SpecA64.CapRef) + (cte : Fl4SpecA64.CslotPtr) (exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) + (p : Fl4SpecA64.ObjRef) (R : Fl4SpecA64.CapRights) (sz : Fl4SpecA64.VMPageSize) (dev : Bool) + (asid : Fl4SpecA64.Asid) (va : Fl4SpecA64.VSpaceRef) (k : Fl4SpecA64.ExtRootKind) + (hk : Fl4SpecA64.asidRootKind σ asid = some k) (h1 : k ≠ .stage1) : + (Fl4SpecA64.fl4ArchDecodeInvocation (Z := Unit) calling (.ext .armPageTestAndClearAccessed) args xSlot cte + (.frameCap p R sz dev (some (asid, va))) exs).run σ = + (Fl4Spec.NonDetE.liftME Fl4SpecA64.Fl4ArchInvocation.testAndClearAccessed + (Fl4SpecA64.decodeExtTestAndClearAccessed (Z := Unit) (.frameCap p R sz dev (some (asid, va))))).run σ := by + simp only [Fl4SpecA64.fl4ArchDecodeInvocation, show (.ext .armPageTestAndClearAccessed : Fl4SpecA64.Fl4Label) ≠ + .ext .armPageDeriveSubframe by decide, ↓reduceIte] + exact (run_bind_okD (run_liftE_stD (extKindOfAsid_someA hk h1)) _).trans rfl + +private theorem dec_tca_slA {S A : Type} {st : machine.Store S} (ar : machine.Arch A) (s : S) (a : A) + (fp rt asid va : U64) (sz : capability.FrameSize) (hsz : sz = .Small ∨ sz = .Large) (dev call : Bool) : + vspace.decode_test_and_clear_accessed st ar s a (frCapA fp sz rt dev true false asid va) call = + (if asid = 0#u64 then do let r ← error.illegal Unit; ok (r, s, a) + else do + let b1 ← ar.hw_access_flag a + if b1 = true then do + let r ← vspace.frame_root st s asid + match r with + | core.result.Result.Ok p => + let (r1, _) := p + let o ← vspace.mapped_entry st s (frCapA fp sz rt dev true false asid va) r1 + match o with + | none => do let r2 ← error.invalid_cap Unit 0#u64; ok (r2, s, a) + | some x => do + let ct ← sched.cur_thread st s + let i ← vspace.D_AF + let i1 ← lift (~~~i) + let (old, s1) ← st.fetch_and s x.slot i1 + let i2 ← lift (old &&& i) + if i2 != 0#u64 then do + let s2 ← vspace.reply1 st s1 ct call 1#u64 + ok (core.result.Result.Ok (), s2, a) + else do + let s2 ← vspace.reply1 st s1 ct call 0#u64 + ok (core.result.Result.Ok (), s2, a) + | core.result.Result.Err _ => do let r1 ← error.invalid_cap Unit 0#u64; ok (r1, s, a) + else do let r ← error.illegal Unit; ok (r, s, a)) := by + rcases hsz with rfl | rfl <;> + (unfold vspace.decode_test_and_clear_accessed frCapA; simp only [bind_ok]; rfl) + +private theorem dec_tca_body_okA {S A : Type} {st : machine.Store S} (s : S) (a : A) + (f : vspace.FrameCap) (call : Bool) (r1 r2 : U64) : + (do + match (core.result.Result.Ok (r1, r2) : core.result.Result (U64 × U64) error.Exc) with + | core.result.Result.Ok p => + let (r1, _) := p + let o ← vspace.mapped_entry st s f r1 + match o with + | none => do let r2 ← error.invalid_cap Unit 0#u64; ok (r2, s, a) + | some x => do + let ct ← sched.cur_thread st s + let i ← vspace.D_AF + let i1 ← lift (~~~i) + let (old, s1) ← st.fetch_and s x.slot i1 + let i2 ← lift (old &&& i) + if i2 != 0#u64 then do + let s2 ← vspace.reply1 st s1 ct call 1#u64 + ok (core.result.Result.Ok (), s2, a) + else do + let s2 ← vspace.reply1 st s1 ct call 0#u64 + ok (core.result.Result.Ok (), s2, a) + | core.result.Result.Err _ => do let r1 ← error.invalid_cap Unit 0#u64; ok (r1, s, a)) = + (do + let o ← vspace.mapped_entry st s f r1 + match o with + | none => do let r2 ← error.invalid_cap Unit 0#u64; ok (r2, s, a) + | some x => do + let ct ← sched.cur_thread st s + let i ← vspace.D_AF + let i1 ← lift (~~~i) + let (old, s1) ← st.fetch_and s x.slot i1 + let i2 ← lift (old &&& i) + if i2 != 0#u64 then do + let s2 ← vspace.reply1 st s1 ct call 1#u64 + ok (core.result.Result.Ok (), s2, a) + else do + let s2 ← vspace.reply1 st s1 ct call 0#u64 + ok (core.result.Result.Ok (), s2, a)) := rfl + +private theorem dec_tca_errA {S A : Type} {st : machine.Store S} (ar : machine.Arch A) (s : S) (a : A) + (fp rt asid va : U64) (sz : capability.FrameSize) (dev mp call : Bool) + (h : mp = false ∨ sz = .Huge) : + vspace.decode_test_and_clear_accessed st ar s a (frCapA fp sz rt dev mp false asid va) call = + ok (.Err (.Sys .IllegalOperation), s, a) := by + rcases h with rfl | rfl + · cases sz <;> (unfold vspace.decode_test_and_clear_accessed frCapA; simp only [bind_ok, error.illegal]; rfl) + · unfold vspace.decode_test_and_clear_accessed frCapA; simp only [bind_ok, error.illegal]; rfl + +set_option maxHeartbeats 4000000 in +/-- **Label 56 (`ARMPageTestAndClearAccessed`) on `ArchTopPostA`** (module docstring). -/ +theorem test_clear_accessed_jA {ar : machine.Arch A} (K : ArchKeepsA ar AR) + (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) + (hvcR : ∀ v ∈ Z.V, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → wa (L.curW s) K ≠ v ∧ wa (L.curW s) K ≠ wa v 24#u64) + (hTO : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s q) (hEO : ExtTabOffA (L := L) Z m s) + (hTr : TreeA st OS Z.G.chains Z.G.par s) + (cte : Fl4SpecA64.CslotPtr) (hab : AbsSlotA Z.th cte) + (fp rt asid va : U64) (sz : capability.FrameSize) (dev mp e : Bool) + (hcap : capAtA (L.mem s) (cslotAddrA cte) = .Frame fp sz rt dev mp e asid va) + (call : Bool) (args : List U64) (xSlot : Fl4SpecA64.CapRef) + (exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) : + vspace.decode_test_and_clear_accessed st ar s a (frCapA fp sz rt dev mp e asid va) call ⦃ res => + ArchTopPostA (L := L) AR Z OS m call σ ((Fl4SpecA64.fl4ArchDecodeInvocation (Z := Unit) call + (.ext .armPageTestAndClearAccessed) (args.map UScalar.bv) xSlot cte + (absFrameE e (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev (mapDataA mp asid va)) exs).run σ) s a res ⦄ := by + obtain ⟨c0, -, hrel0⟩ := StateRelA.getAbsA E.j.1.st hab + rw [hcap] at hrel0 + obtain ⟨⟨-, -, ⟨h16a, hmz, _, _⟩, -⟩, -⟩ := hrel0 + have h16 : asid.val < 65536 := lt_of_lt_of_eq h16a (by norm_num) + -- 1. `is_ept` + cases e with + | true => + unfold vspace.decode_test_and_clear_accessed + rw [show absFrameE true (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev (mapDataA mp asid va) = + .ext (.stage2FrameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev (mapDataA mp asid va)) from rfl, + dec_testClearS2A] + simp only [frCapA, ↓reduceIte, error.illegal, bind_ok, WP.spec_ok] + exact ⟨rfl, rfl, .illegalOperation, rfl, Or.inl trivial⟩ + | false => + rw [show absFrameE false (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev (mapDataA mp asid va) = + .frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev (mapDataA mp asid va) from rfl] + -- Helper to finish the success arm (`fetch_and` + `reply1`) once we have `P`, `j`, and `D = ok (.testAndClearAccessed (specPtr fp))` + have hsucc : ∀ (P j : U64) (hP : P ∈ Z.S1 ∨ ∃ y ∈ Z.Y, P = wa y 4096#u64) (hj : j.val < 512) + (D : Set (Except Fl4SpecA64.SyscallError Fl4SpecA64.Fl4ArchInvocation × Fl4SpecA64.State Unit) × Prop), + D = ({(.ok (.testAndClearAccessed (specPtr fp)), σ)}, False) → + (do + let ct ← sched.cur_thread st s + let i ← vspace.D_AF + let i1 ← lift (~~~i) + let (old, s1) ← st.fetch_and s (peAF P j) i1 + let i2 ← lift (old &&& i) + if i2 != 0#u64 then do + let s2 ← vspace.reply1 st s1 ct call 1#u64 + ok (core.result.Result.Ok (), s2, a) + else do + let s2 ← vspace.reply1 st s1 ct call 0#u64 + ok (core.result.Result.Ok (), s2, a)) ⦃ res => ArchTopPostA (L := L) AR Z OS m call σ D s a res ⦄ := by + intro P j hP hj D hD + rw [cur_thread_ok, bind_ok, d_af_valA, bind_ok] + simp only [lift, bind_ok, L.fetch_and_eq] + obtain ⟨J1, T1, C1, hks1, hcpu1, hcur1, hsc1, hiq1, hrm1⟩ := jat_clearAF E RS hTO hEO hTr hP hj + obtain ⟨Rp, Nt, extra, sp, hPpl⟩ := RS.pl + obtain ⟨x, hk, -, hset, -, -, -, -, -⟩ := E.restartA hPpl RS.sym RS.run RS.cur RS.vs RS.vc + set σ1 := stUpdA σ σ.curThread x .restart + have hk1 : σ1.kheap σ.curThread = some (.tcb { x with tcbState := .restart }) := by simp [σ1, stUpdA, setObjA] + have hct : L.curW s ∈ H.tcbs := E.sq.curT + have hrq1 : σ.curThread ∉ σ1.releaseQueue := by + have hq : specPtr (L.curW s) ∉ σ.releaseQueue := by + rw [E.j.1.rest.rel]; intro hm + obtain ⟨r, hr, er⟩ := List.mem_map.1 hm + have e' : r = L.curW s := specPtr_inj er; subst e' + have haf := E.sq.inR _ E.sq.cur _ hr + exact ((E.sq.released _ hct).2 (by rw [haf]; exact hr)) RS.cur.1 + simpa [σ1, stUpdA, setObjA, E.j.1.sch.curThread] using hq + set w := L.mem s (scA (L.curW s)) + have hwC : w ∈ Z.C := E.j.1.rest.scMem (L.curW s) (by rw [← E.tcbs]; exact hct) RS.cur.2.1 + have hwH : w ∈ H.scs := by rw [E.scs]; exact hwC + have hscx : x.tcbSchedContext = some (scRenA m w) := by + obtain ⟨x', hk', -, hsx⟩ := E.j.1.sch.tcb (L.curW s) (by rw [← E.tcbs]; exact hct) + rw [E.j.1.sch.curThread] at hk; rw [hk'] at hk; cases hk + rw [hsx]; unfold optScA; simp [w, RS.cur.2.1] + have hy1 : ∃ y n, σ1.kheap (scRenA m w) = some (.schedContext y n) ∧ 0 < y.scRefillMax := by + obtain ⟨y, n, ey, eyr⟩ := E.j.1.rest.refillMax w hwC + have hrm : L.rmax s w ≠ 0#u64 := (E.sys.ent.bxi.2.2.2.2.act w hwH).1 RS.cur.2.2 + refine ⟨y, n, stUpd_keep hk ey (fun _ h => by cases h), ?_⟩ + rw [eyr] + have e2 : L.rmax s w = L.mem s (wa w 72#u64) := by unfold LawfulStore.rmax; rw [maxA_eqA] + rw [e2] at hrm + exact Nat.pos_of_ne_zero (fun e => hrm (UScalar.eq_of_val_eq (by simpa using e))) + -- Notice: `J1` holds at `σ`, while `reply1_jA` starts from `σ1`? + -- Wait: `reply1` on FL4 runs from `s1` (which has `J1` at `σ`), and `stUpdA` on `σ1` sets `tcbState := .running` + -- which produces the EXACT SAME state as `stUpdA` on `σ`! + -- Even better: `reply1_jA` only needs `J1` at `σ`, and `σ3` from `σ` equals `σ3` from `σ1` because + -- `replyRun_oneA` on `σ1` gives `stUpdA (ctxUpdA σ1 t x1 uc') t _ .running = stUpdA (ctxUpdA σ t x uc') t _ .running`! + have hrq0 : σ.curThread ∉ σ.releaseQueue := hrq1 + have hy0 : ∃ y n, σ.kheap (scRenA m w) = some (.schedContext y n) ∧ 0 < y.scRefillMax := by + obtain ⟨y, n, ey, eyr⟩ := E.j.1.rest.refillMax w hwC + have hrm : L.rmax s w ≠ 0#u64 := (E.sys.ent.bxi.2.2.2.2.act w hwH).1 RS.cur.2.2 + refine ⟨y, n, ey, ?_⟩ + rw [eyr] + have e2 : L.rmax s w = L.mem s (wa w 72#u64) := by unfold LawfulStore.rmax; rw [maxA_eqA] + rw [e2] at hrm + exact Nat.pos_of_ne_zero (fun e => hrm (UScalar.eq_of_val_eq (by simpa using e))) + have hrepEq : ∀ (v : U64) (σ3 : Fl4SpecA64.State Unit), + ((), σ3) ∈ ((do + Fl4Spec.NonDet.when call (Fl4SpecA64.replyFromKernel (Z := Unit) σ.curThread (0, [v.bv])) + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .running) σ).1 → + ((), σ3) ∈ ((do + Fl4Spec.NonDet.when call (Fl4SpecA64.replyFromKernel (Z := Unit) σ.curThread (0, [v.bv])) + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .running) σ1).1 := by + intro v σ3 h3 + have h0 := replyRun_oneA hk call v.bv fun uc' _ _ => by + obtain ⟨y, n, ey, hpos⟩ := hy0 + refine schedulable_ofA (x := { x with tcbArch := { x.tcbArch with tcbContext := uc' }, tcbState := .running }) + (n := n) (by simp [setObjA]) rfl hscx ?_ hpos hrq0 + have hne : scRenA m w ≠ σ.curThread := fun e => by rw [e, hk] at ey; cases ey + simpa [setObjA, Function.update_of_ne hne] using ey + have h1 := replyRun_oneA hk1 call v.bv fun uc' _ _ => by + obtain ⟨y, n, ey, hpos⟩ := hy1 + refine schedulable_ofA (x := { { x with tcbState := .restart } with + tcbArch := { x.tcbArch with tcbContext := uc' }, tcbState := .running }) + (n := n) (by simp [setObjA]) rfl hscx ?_ hpos hrq1 + have hne : scRenA m w ≠ σ.curThread := fun e => by rw [e, hk1] at ey; cases ey + simpa [setObjA, Function.update_of_ne hne] using ey + rw [h0] at h3; simp only [Set.mem_singleton_iff, Prod.mk.injEq, true_and] at h3 + rw [h1]; simp only [Set.mem_singleton_iff, Prod.mk.injEq, true_and] + rw [h3] + simp only [σ1, stUpdA, ctxUpdA, setObjA, Function.update_idem, replyCtxA] + change (if ((L.mem s (peAF P j) &&& 1024#u64) != 0#u64) = true then do + let s2 ← vspace.reply1 st (L.wr s (peAF P j) (L.mem s (peAF P j) &&& ~~~1024#u64)) (L.curW s) call 1#u64 + ok (core.result.Result.Ok (), s2, a) + else do + let s2 ← vspace.reply1 st (L.wr s (peAF P j) (L.mem s (peAF P j) &&& ~~~1024#u64)) (L.curW s) call 0#u64 + ok (core.result.Result.Ok (), s2, a)) ⦃ res => ArchTopPostA (L := L) AR Z OS m call σ D s a res ⦄ + by_cases hbit : ((L.mem s (peAF P j) &&& 1024#u64) != 0#u64) = true + · rw [if_pos hbit] + refine WP.spec_bind (reply1_jA E RS hvcR J1 T1 hks1 hcpu1 hcur1 hsc1 hiq1 hrm1 hk rfl hrq0 hscx hy0 call 1#u64) + fun s2 ⟨σ3, hrep, J3, T3, C3⟩ => ?_ + refine (WP.spec_ok _).2 ⟨.testAndClearAccessed (specPtr fp), okD_memA hD, σ1, hset, + σ1, [1], mem_liftED (by simp [Fl4SpecA64.performTestAndClearAccessed, Fl4Spec.NonDet.select]), + Or.inr ⟨σ3, hrepEq 1#u64 σ3 hrep, J3, T3, fun hC => C3 (C1 hC)⟩⟩ + · rw [if_neg hbit] + refine WP.spec_bind (reply1_jA E RS hvcR J1 T1 hks1 hcpu1 hcur1 hsc1 hiq1 hrm1 hk rfl hrq0 hscx hy0 call 0#u64) + fun s2 ⟨σ3, hrep, J3, T3, C3⟩ => ?_ + refine (WP.spec_ok _).2 ⟨.testAndClearAccessed (specPtr fp), okD_memA hD, σ1, hset, + σ1, [0], mem_liftED (by simp [Fl4SpecA64.performTestAndClearAccessed, Fl4Spec.NonDet.select]), + Or.inr ⟨σ3, hrepEq 0#u64 σ3 hrep, J3, T3, fun hC => C3 (C1 hC)⟩⟩ + -- 2. Split `mp` first so we know whether `mapDataA mp asid va` is `none` or `some` + cases mp with + | false => + rw [dec_tca_errA ar s a fp rt asid va sz dev false call (Or.inl rfl), + show mapDataA false asid va = none from rfl, + dec_testClearS1A call _ xSlot cte exs _ _ _ _ none (fun _ _ h => by cases h)] + have hD : (Fl4Spec.NonDetE.liftME Fl4SpecA64.Fl4ArchInvocation.testAndClearAccessed + (Fl4SpecA64.decodeTestAndClearAccessed (Z := Unit) + (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev none))).run σ = + ({(.error .illegalOperation, σ)}, False) := run_liftME_errA rfl + rw [hD, WP.spec_ok] + exact ⟨rfl, rfl, .illegalOperation, rfl, Or.inl trivial⟩ + | true => + have hmd : mapDataA true asid va = some (asidOfA asid, va.bv) := rfl + rw [hmd] + -- Case split on `sz = .Huge` + by_cases hH : sz = .Huge + · subst hH + rw [dec_tca_errA ar s a fp rt asid va .Huge dev true call (Or.inr rfl), WP.spec_ok] + have hD : (Fl4SpecA64.fl4ArchDecodeInvocation (Z := Unit) call (.ext .armPageTestAndClearAccessed) + (args.map UScalar.bv) xSlot cte (.frameCap (specPtr fp) (vmRightsOfA rt) .armHugePage dev + (some (asidOfA asid, va.bv))) exs).run σ = ({(.error .illegalOperation, σ)}, False) := by + rcases hk : Fl4SpecA64.asidRootKind σ (asidOfA asid) with _ | k + · rw [dec_testClearS1A call _ xSlot cte exs _ _ _ _ _ (fun _ _ h => by cases h; exact Or.inl hk)] + exact run_liftME_errA (by unfold Fl4SpecA64.decodeTestAndClearAccessed; simp) + · cases k with + | stage1 => + rw [dec_testClearS1A call _ xSlot cte exs _ _ _ _ _ (fun _ _ h => by cases h; exact Or.inr hk)] + exact run_liftME_errA (by unfold Fl4SpecA64.decodeTestAndClearAccessed; simp) + | stage2 => + rw [dec_testClearExtA call _ xSlot cte exs _ _ _ _ _ _ .stage2 hk (by decide)] + exact run_liftME_errA (by unfold Fl4SpecA64.decodeExtTestAndClearAccessed; simp) + | high => + rw [dec_testClearExtA call _ xSlot cte exs _ _ _ _ _ _ .high hk (by decide)] + exact run_liftME_errA (by unfold Fl4SpecA64.decodeExtTestAndClearAccessed; simp) + exact ⟨rfl, rfl, .illegalOperation, hD, Or.inl trivial⟩ + · have hsz : sz = .Small ∨ sz = .Large := by cases sz <;> simp_all + rw [dec_tca_slA ar s a fp rt asid va sz hsz dev call] + -- 3. `asid = 0` + by_cases h0 : asid = 0#u64 + · rw [if_pos h0] + simp only [error.illegal, bind_ok, WP.spec_ok] + have ha0 : asidOfA asid = 0 := by subst h0; rfl + refine ⟨rfl, rfl, .illegalOperation, ?_, Or.inl trivial⟩ + rcases hk : Fl4SpecA64.asidRootKind σ (asidOfA asid) with _ | k + · rw [dec_testClearS1A call _ xSlot cte exs _ _ _ _ _ (fun _ _ h => by cases h; exact Or.inl hk)] + exact run_liftME_errA (by unfold Fl4SpecA64.decodeTestAndClearAccessed; simp [ha0]) + · cases k with + | stage1 => + rw [dec_testClearS1A call _ xSlot cte exs _ _ _ _ _ (fun _ _ h => by cases h; exact Or.inr hk)] + exact run_liftME_errA (by unfold Fl4SpecA64.decodeTestAndClearAccessed; simp [ha0]) + | stage2 => + rw [dec_testClearExtA call _ xSlot cte exs _ _ _ _ _ _ .stage2 hk (by decide)] + exact run_liftME_errA (by unfold Fl4SpecA64.decodeExtTestAndClearAccessed; simp [ha0]) + | high => + rw [dec_testClearExtA call _ xSlot cte exs _ _ _ _ _ _ .high hk (by decide)] + exact run_liftME_errA (by unfold Fl4SpecA64.decodeExtTestAndClearAccessed; simp [ha0]) + rw [if_neg h0] + have ha0 : asidOfA asid ≠ 0 := asidOf_ne0A h16 h0 + -- 4. `hw_access_flag` + rw [K.hwAccessFlag a, bind_ok] + cases hhw : Fl4SpecA64.fl4A64Platform.hwAccessFlag with + | false => + simp only [Bool.false_eq_true, ↓reduceIte, error.illegal, bind_ok, WP.spec_ok] + refine ⟨rfl, rfl, .illegalOperation, ?_, Or.inl trivial⟩ + rcases hk : Fl4SpecA64.asidRootKind σ (asidOfA asid) with _ | k + · rw [dec_testClearS1A call _ xSlot cte exs _ _ _ _ _ (fun _ _ h => by cases h; exact Or.inl hk)] + exact run_liftME_errA (by unfold Fl4SpecA64.decodeTestAndClearAccessed; simp [hhw]) + · cases k with + | stage1 => + rw [dec_testClearS1A call _ xSlot cte exs _ _ _ _ _ (fun _ _ h => by cases h; exact Or.inr hk)] + exact run_liftME_errA (by unfold Fl4SpecA64.decodeTestAndClearAccessed; simp [hhw]) + | stage2 => + rw [dec_testClearExtA call _ xSlot cte exs _ _ _ _ _ _ .stage2 hk (by decide)] + exact run_liftME_errA (by unfold Fl4SpecA64.decodeExtTestAndClearAccessed; simp [hhw]) + | high => + rw [dec_testClearExtA call _ xSlot cte exs _ _ _ _ _ _ .high hk (by decide)] + exact run_liftME_errA (by unfold Fl4SpecA64.decodeExtTestAndClearAccessed; simp [hhw]) + | true => + simp only [↓reduceIte] + -- 5. Split on `asidRootKind σ (asidOfA asid)` + have hcond : ¬ (frameSizeOfA sz = .armHugePage ∨ asidOfA asid = 0 ∨ Fl4SpecA64.fl4A64Platform.hwAccessFlag = false) := by + rintro (h | h | h) + · rcases hsz with rfl | rfl <;> cases h + · exact ha0 h + · rw [hhw] at h; cases h + rcases hk : Fl4SpecA64.asidRootKind σ (asidOfA asid) with _ | k + · -- No root for `asid` + rw [dec_testClearS1A call _ xSlot cte exs _ _ _ _ _ (fun _ _ h => by cases h; exact Or.inl hk)] + obtain ⟨r, hfind, hrel⟩ := spec_ex_fd (find_asid_rawA (st := st) E.j.1.vs h16) + rcases r with w | lf + · obtain ⟨hv, -⟩ := rawKindE E.j.1.vs E.j.2.2.1.obj hrel + have hne : Fl4SpecA64.asidRootKind σ (asidOfA asid) ≠ none := by + unfold Fl4SpecA64.asidRootKind asidOfA; rw [hv]; simp + exact absurd hk hne + · rw [frame_root_errA hfind, bind_ok] + simp only [error.invalid_cap, bind_ok, WP.spec_ok] + have hspecF : (Fl4SpecA64.findVspaceForAsid (Z := Unit) (asidOfA asid)).run σ = + ({(.error .invalidRoot, σ)}, False) := by + have hn : Fl4SpecA64.vspaceForAsid (asidOfA asid) σ = none := hrel + exact findVs_noneA hn + have hD : (Fl4Spec.NonDetE.liftME Fl4SpecA64.Fl4ArchInvocation.testAndClearAccessed + (Fl4SpecA64.decodeTestAndClearAccessed (Z := Unit) (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev + (some (asidOfA asid, va.bv))))).run σ = ({(.error (.invalidCapability 0), σ)}, False) := by + refine run_liftME_errA ?_ + unfold Fl4SpecA64.decodeTestAndClearAccessed + dsimp only + rw [if_neg hcond] + rw [run_bind_errorD ((run_handleE_errA hspecF).trans rfl)] + exact ⟨rfl, rfl, .invalidCapability 0, hD, Or.inr ⟨0#u64, rfl, rfl⟩⟩ + cases k with + | stage1 => + rw [dec_testClearS1A call _ xSlot cte exs _ _ _ _ _ (fun _ _ h => by cases h; exact Or.inr hk)] + have hS1 : ∀ r, Fl4SpecA64.vspaceForAsid (asidOfA asid) σ = some r → + Fl4SpecA64.rootKindAt σ r = .stage1 := fun r hr => by + obtain ⟨r', hr', hk'⟩ := asidRootKind_someX hk + rw [hr] at hr'; cases hr'; exact hk' + obtain ⟨r, hfind, hrel⟩ := spec_ex_fd (find_asid_rawA (st := st) E.j.1.vs h16) + rcases r with w | lf + swap + · obtain ⟨r', hr', -⟩ := asidRootKind_someX hk + have hn : Fl4SpecA64.vspaceForAsid (asidOfA asid) σ = none := hrel + rw [hn] at hr'; cases hr' + obtain ⟨htag, hwS1, hw0, hv⟩ := raw_s1A E.j.1.vs E.j.2.2.1.obj hS1 hrel + have hspecF : (Fl4SpecA64.findVspaceForAsid (Z := Unit) (asidOfA asid)).run σ = + ({(.ok (specPtr w), σ)}, False) := findVs_runA hv (hS1 _ hv) + rw [frame_root_s1A hfind htag, bind_ok] + have hreachW := vsReachA_of_vspaceForAsid E.j.1.vs E.j.2.2.1.obj hwS1 hv + have hme := mapped_entry_s1A E.j.1.vs hwS1 hw0 hreachW (frCapA fp sz rt dev true false asid va) + obtain ⟨o, ho, hmeo⟩ := spec_ex_fd hme + refine WP.spec_bind (Pₘ := fun o' => o' = o) (by rw [ho]; exact (WP.spec_ok _).2 rfl) fun o' ho' => ?_ + subst o' + cases o with + | none => + simp only [error.invalid_cap, bind_ok, WP.spec_ok] + have hD : (Fl4Spec.NonDetE.liftME Fl4SpecA64.Fl4ArchInvocation.testAndClearAccessed + (Fl4SpecA64.decodeTestAndClearAccessed (Z := Unit) (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev + (some (asidOfA asid, va.bv))))).run σ = ({(.error (.invalidCapability 0), σ)}, False) := by + refine run_liftME_errA ?_ + unfold Fl4SpecA64.decodeTestAndClearAccessed + dsimp only + rw [if_neg hcond, run_bind_okD (run_handleE_okA hspecF), + run_bind_okD (liftE_runA (m := Fl4Spec.NonDet.gets _) rfl)] + rw [lookupFrame_of_pteA] + cases hlf : Fl4SpecA64.lookupFramePte (specPtr w) va.bv (Fl4SpecA64.ptesOf σ) with + | none => rfl + | some x => + dsimp only [Option.map] + rw [if_neg (hmeo x.1 x.2 hlf)]; rfl + exact ⟨rfl, rfl, .invalidCapability 0, hD, Or.inr ⟨0#u64, rfl, rfl⟩⟩ + | some lu => + dsimp only + obtain ⟨pte, hlf, hbase, -⟩ := hmeo + obtain ⟨P, j, hPS1, hj, hsl⟩ := mapped_entry_s1_slotA E.j.1.vs hwS1 hw0 hreachW _ ho + rw [hsl] + have hD : (Fl4Spec.NonDetE.liftME Fl4SpecA64.Fl4ArchInvocation.testAndClearAccessed + (Fl4SpecA64.decodeTestAndClearAccessed (Z := Unit) (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev + (some (asidOfA asid, va.bv))))).run σ = + ({(.ok (.testAndClearAccessed (specPtr fp)), σ)}, False) := by + refine run_liftME_okA ?_ + unfold Fl4SpecA64.decodeTestAndClearAccessed + dsimp only + rw [if_neg hcond, run_bind_okD (run_handleE_okA hspecF), + run_bind_okD (liftE_runA (m := Fl4Spec.NonDet.gets _) rfl), + lookupFrame_of_pteA, hlf] + dsimp only [Option.map] + rw [if_pos ⟨rfl, hbase⟩]; rfl + exact hsucc P j (Or.inl hPS1) hj _ hD + | stage2 => + rw [dec_testClearExtA call _ xSlot cte exs _ _ _ _ _ _ .stage2 hk (by decide)] + obtain ⟨r, hfind, hrel⟩ := spec_ex_fd (find_asid_rawA (st := st) E.j.1.vs h16) + obtain ⟨y0, hv0, hk0⟩ := asidRootKind_someX hk + rcases r with w | lf + swap + · have hn : Fl4SpecA64.vspaceForAsid (asidOfA asid) σ = none := hrel + rw [hn] at hv0; cases hv0 + obtain ⟨hv, hkind⟩ := rawKindE E.j.1.vs E.j.2.2.1.obj hrel + have hv' : Fl4SpecA64.vspaceForAsid (asidOfA asid) σ = some (specPtr (untagA w)) := hv + rw [hv'] at hv0; cases hv0 + rcases hkind with ⟨-, h⟩ | ⟨ht1, -, -⟩ | ⟨-, -, h⟩ + · rw [h] at hk0; cases hk0 + · rw [frame_root_s2A hfind ht1, bind_ok] + simp only [error.invalid_cap, bind_ok, WP.spec_ok] + have hferr : (Fl4SpecA64.findExtRoot (Z := Unit) .high (asidOfA asid)).run σ = + ({(.error .invalidRoot, σ)}, False) := by + rw [findExtRoot_runX]; simp [extRootResX, hv', hk0] + have hD : (Fl4Spec.NonDetE.liftME Fl4SpecA64.Fl4ArchInvocation.testAndClearAccessed + (Fl4SpecA64.decodeExtTestAndClearAccessed (Z := Unit) (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev + (some (asidOfA asid, va.bv))))).run σ = ({(.error (.invalidCapability 0), σ)}, False) := by + refine run_liftME_errA ?_ + unfold Fl4SpecA64.decodeExtTestAndClearAccessed + dsimp only + rw [if_neg hcond, run_bind_errorD ((run_handleE_errA hferr).trans rfl)] + exact ⟨rfl, rfl, .invalidCapability 0, hD, Or.inr ⟨0#u64, rfl, rfl⟩⟩ + · rw [h] at hk0; cases hk0 + | high => + have hva : VaOkE va := E.j.1.vaOkE (hab.isSlot E.j.1.place) hcap + rw [dec_testClearExtA call _ xSlot cte exs _ _ _ _ _ _ .high hk (by decide)] + obtain ⟨r, hfind, hrel⟩ := spec_ex_fd (find_asid_rawA (st := st) E.j.1.vs h16) + obtain ⟨y0, hv0, hk0⟩ := asidRootKind_someX hk + rcases r with w | lf + swap + · have hn : Fl4SpecA64.vspaceForAsid (asidOfA asid) σ = none := hrel + rw [hn] at hv0; cases hv0 + obtain ⟨hv, hkind⟩ := rawKindE E.j.1.vs E.j.2.2.1.obj hrel + have hv' : Fl4SpecA64.vspaceForAsid (asidOfA asid) σ = some (specPtr (untagA w)) := hv + rw [hv'] at hv0; cases hv0 + rcases hkind with ⟨-, h⟩ | ⟨-, -, h⟩ | ⟨ht2, hyY, -⟩ + · rw [h] at hk0; cases hk0 + · rw [h] at hk0; cases hk0 + · rw [frame_root_hiA hfind ht2, bind_ok] + have hspecF : (Fl4SpecA64.findExtRoot (Z := Unit) .high (asidOfA asid)).run σ = + ({(.ok (specPtr (untagA w)), σ)}, False) := by + rw [findExtRoot_runX]; simp [extRootResX, hv', hk0] + have hactY := activeHighRootA_of_vspaceForAsid E.j.1.vs E.j.2.2.1.obj hyY hv' + have hme := mapped_entry_hiE E.j.1.vs E.j.1.ext hyY hactY (frCapA fp sz rt dev true false asid va) hva + obtain ⟨o, ho, hmeo⟩ := spec_ex_fd hme + refine WP.spec_bind (Pₘ := fun o' => o' = o) (by rw [ho]; exact (WP.spec_ok _).2 rfl) fun o' ho' => ?_ + subst o' + cases o with + | none => + simp only [error.invalid_cap, bind_ok, WP.spec_ok] + have hD : (Fl4Spec.NonDetE.liftME Fl4SpecA64.Fl4ArchInvocation.testAndClearAccessed + (Fl4SpecA64.decodeExtTestAndClearAccessed (Z := Unit) (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev + (some (asidOfA asid, va.bv))))).run σ = ({(.error (.invalidCapability 0), σ)}, False) := by + refine run_liftME_errA ?_ + unfold Fl4SpecA64.decodeExtTestAndClearAccessed + dsimp only + rw [if_neg hcond, run_bind_okD (run_handleE_okA hspecF), + run_bind_okD (liftE_runA (m := Fl4Spec.NonDet.gets _) rfl)] + rw [hmeo]; rfl + exact ⟨rfl, rfl, .invalidCapability 0, hD, Or.inr ⟨0#u64, rfl, rfl⟩⟩ + | some lu => + dsimp only + obtain ⟨pos, pte, hlf, -⟩ := hmeo + obtain ⟨P, j, hPY, hj, hsl⟩ := mapped_entry_hi_slotE E.j.1.vs E.j.1.ext hyY hactY _ hva ho + rw [hsl] + have hD : (Fl4Spec.NonDetE.liftME Fl4SpecA64.Fl4ArchInvocation.testAndClearAccessed + (Fl4SpecA64.decodeExtTestAndClearAccessed (Z := Unit) (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev + (some (asidOfA asid, va.bv))))).run σ = + ({(.ok (.testAndClearAccessed (specPtr fp)), σ)}, False) := by + refine run_liftME_okA ?_ + unfold Fl4SpecA64.decodeExtTestAndClearAccessed + dsimp only + rw [if_neg hcond, run_bind_okD (run_handleE_okA hspecF), + run_bind_okD (liftE_runA (m := Fl4Spec.NonDet.gets _) rfl), hlf] + rfl + exact hsucc P j hPY hj _ hD + +end test_clear + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/FrSubframeA.lean b/proofs/Fl4ProofsA64/P3/FrSubframeA.lean new file mode 100644 index 000000000..050cc2644 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/FrSubframeA.lean @@ -0,0 +1,1992 @@ +/- +P3 (aarch64, A64-P3 S3 gen 40, Step 3b): +- **Label 55 (`ARMPageDeriveSubframe`)** on `ArchTopPostA`: `subframe_dest_refinesA`, + `ensure_empty_run_refinesA`, `cap_insert_frame_stepA`, `insert_subframes_refinesA`, `derive_subframe_jA`. +- **Complete `Cap::Frame` top (`decInv_frame_all_jA`)** on `ArchTopPostA` covering every label + (49 `ARMPageMap`, 50 `ARMPageUnmap`, 51–54 page cache flushes, 55 `ARMPageDeriveSubframe`, + 56 `ARMPageTestAndClearAccessed`, 57 `ARMPageGetAddress`, and illegal labels). + +No named premise. New module; no existing statement changed. +-/ +import Fl4ProofsA64.P3.FrReplyA +import Fl4ProofsA64.P3.InsDisA +import Fl4ProofsA64.P3.CnodeCapsA +import Fl4ProofsA64.P3.CnodeCopyA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## Subframe cap arithmetic, well-formedness, and `sameRegionAs` -/ + +section subframe_arith + +theorem canonical_iff_rangeA (p : U64) : + CapRt.Canonical p ↔ p.val < 2 ^ 47 ∨ 2 ^ 64 - 2 ^ 47 ≤ p.val := by + unfold CapRt.Canonical + have hlo : (∀ j (hj : j < 64), 47 ≤ j → p.bv[j] = false) ↔ p.val < 2 ^ 47 := by + constructor + · intro h + by_contra hge + push_neg at hge + have hne : p.val / 2 ^ 47 ≠ 0 := by omega + have hlt : p.val / 2 ^ 47 < 2 ^ 17 := by + have := p.hBounds; simp [U64.size] at this; omega + have hex : ∃ k < 17, (p.val / 2 ^ 47).testBit k = true := by + by_contra hall + push_neg at hall + have hz : p.val / 2 ^ 47 = 0 := by + apply Nat.eq_of_testBit_eq + intro k + simp only [Nat.zero_testBit] + by_cases hk : k < 17 + · exact Bool.eq_false_iff.2 (hall k hk) + · exact Nat.testBit_lt_two_pow (lt_of_lt_of_le hlt (Nat.pow_le_pow_right (by omega) (by omega))) + exact hne hz + obtain ⟨k, hk, htb⟩ := hex + have h1 := h (47 + k) (by omega) (by omega) + rw [BitVec.getElem_eq_testBit_toNat] at h1 + change p.val.testBit (47 + k) = false at h1 + rw [Nat.add_comm, ← Nat.testBit_div_two_pow] at h1 + rw [htb] at h1; cases h1 + · intro hlt j hj h47 + rw [BitVec.getElem_eq_testBit_toNat] + exact Nat.testBit_lt_two_pow (lt_of_lt_of_le hlt (Nat.pow_le_pow_right (by omega) h47)) + have hhi : (∀ j (hj : j < 64), 47 ≤ j → p.bv[j] = true) ↔ 2 ^ 64 - 2 ^ 47 ≤ p.val := by + constructor + · intro h + have hdiv : p.val / 2 ^ 47 = 2 ^ 17 - 1 := by + apply Nat.eq_of_testBit_eq + intro k + by_cases hk : k < 17 + · rw [Nat.testBit_div_two_pow, Nat.add_comm] + have h1 := h (47 + k) (by omega) (by omega) + rw [BitVec.getElem_eq_testBit_toNat] at h1 + change p.val.testBit (47 + k) = true at h1 + rw [h1] + have h2 : (2 ^ 17 - 1 : Nat).testBit k = true := by + rw [Nat.testBit_two_pow_sub_one]; exact decide_eq_true hk + exact h2.symm + · have hlt : p.val / 2 ^ 47 < 2 ^ 17 := by + have := p.hBounds; simp [U64.size] at this; omega + rw [Nat.testBit_lt_two_pow (lt_of_lt_of_le hlt (Nat.pow_le_pow_right (by omega) (by omega))), + Nat.testBit_two_pow_sub_one, decide_eq_false hk] + omega + · intro hge j hj h47 + rw [BitVec.getElem_eq_testBit_toNat] + have hlt : p.val < 2 ^ 64 := by have := p.hBounds; simp [U64.size] at this; exact this + have hdiv : p.val / 2 ^ 47 = 2 ^ 17 - 1 := by omega + change p.val.testBit j = true + have hj' : j = (j - 47) + 47 := by omega + rw [hj', ← Nat.testBit_div_two_pow, hdiv, Nat.testBit_two_pow_sub_one] + exact decide_eq_true (by omega) + exact or_congr hlo hhi + +theorem canonical_wa_subframeA {fp k : U64} (hc : CapRt.Canonical fp) (hal : fp.val % 2 ^ 21 = 0) + (hk : k.val < 2 ^ 21) : CapRt.Canonical (wa fp k) := by + rw [canonical_iff_rangeA] at hc ⊢ + have hfp : fp.val < 2 ^ 64 := by have := fp.hBounds; simp [U64.size] at this; exact this + have hwa : (wa fp k).val = fp.val + k.val := by + rw [wa_valP] + have : fp.val + k.val < 2 ^ 64 := by omega + exact Nat.mod_eq_of_lt this + rw [hwa] + rcases hc with h | h + · left; omega + · right; omega + +private theorem bv_one_toNat : (1 : BitVec 64).toNat = 1 := by + change (BitVec.ofNat 64 1).toNat = 1 + rw [BitVec.toNat_ofNat] + +private theorem bv_shl_one_toNat (b : Nat) (hb : b < 64) : ((1 : BitVec 64) <<< b).toNat = 2 ^ b := by + rw [BitVec.toNat_shiftLeft, bv_one_toNat, Nat.shiftLeft_eq, Nat.one_mul] + exact Nat.mod_eq_of_lt (Nat.pow_lt_pow_right (by omega) hb) + +private theorem bv_sub1_toNat (x : BitVec 64) : + (x - 1).toNat = if x.toNat = 0 then 2 ^ 64 - 1 else x.toNat - 1 := by + have hx := x.isLt + rw [BitVec.toNat_sub, bv_one_toNat] + by_cases h : x.toNat = 0 + · rw [if_pos h, h, Nat.add_zero] + exact Nat.mod_eq_of_lt (Nat.sub_lt (Nat.two_pow_pos 64) (by omega)) + · rw [if_neg h] + have h1 : 2 ^ 64 - 1 + x.toNat = (x.toNat - 1) + 2 ^ 64 := by omega + rw [h1, Nat.add_mod_right] + exact Nat.mod_eq_of_lt (by omega) + +private theorem spNoOv_nat {N a : Nat} (ha : a < N) (hN : 2 ^ 21 ≤ N) + (hle : a ≤ if (a + 2 ^ 21) % N = 0 then N - 1 else (a + 2 ^ 21) % N - 1) : + a + 2 ^ 21 ≤ N := by + by_cases h : a + 2 ^ 21 ≤ N + · exact h + · have hm : (a + 2 ^ 21) % N = a + 2 ^ 21 - N := by + rw [Nat.mod_eq_sub_mod (by omega), Nat.mod_eq_of_lt (by omega)] + rw [hm, if_neg (by omega)] at hle + omega + +theorem subframe_spNoOvA {fp k : U64} (hno : SpNoOvA fp 21) (hk : k.val + 4096 ≤ 2 ^ 21) : + SpNoOvA (wa fp k) 12 := by + unfold SpNoOvA at hno ⊢ + rw [specPtr_wa] + have hle : (specPtr fp).toNat ≤ (specPtr fp + ((1 : BitVec 64) <<< 21) - 1).toNat := BitVec.le_def.1 hno + rw [bv_sub1_toNat, BitVec.toNat_add, bv_shl_one_toNat 21 (by omega)] at hle + have h0 : (specPtr fp).toNat + 2 ^ 21 ≤ 2 ^ 64 := + spNoOv_nat (specPtr fp).isLt (Nat.pow_le_pow_right (by omega) (by omega)) hle + rw [BitVec.le_def, bv_sub1_toNat, BitVec.toNat_add, BitVec.toNat_add, bv_shl_one_toNat 12 (by omega)] + have hkv : k.bv.toNat = k.val := rfl + have h1 : (specPtr fp).toNat + k.val < 2 ^ 64 := by omega + have hadd : (specPtr fp + k.bv).toNat = (specPtr fp).toNat + k.val := by + rw [BitVec.toNat_add, hkv, Nat.mod_eq_of_lt h1] + rw [hkv, Nat.mod_eq_of_lt h1, hadd] + by_cases htop : (specPtr fp).toNat + k.val + 2 ^ 12 = 2 ^ 64 + · rw [htop, Nat.mod_self, if_pos rfl]; omega + · have hlt : (specPtr fp).toNat + k.val + 2 ^ 12 < 2 ^ 64 := by omega + rw [Nat.mod_eq_of_lt hlt, if_neg (by omega)]; omega + +theorem subframe_wfA {fp rt asid va k vm : U64} {mp e : Bool} + (hwf : CapWfA (.Frame fp .Large rt false mp e asid va)) + (hvm : 1 ≤ vm.val ∧ vm.val ≤ 3) (hk : k.val + 4096 ≤ 2 ^ 21) : + CapWfA (.Frame (wa fp k) .Small vm false false false 0#u64 0#u64) := by + obtain ⟨-, -, -, hno⟩ := hwf + refine ⟨hvm.1, hvm.2, ⟨by simp, fun _ => rfl, fun h => (by cases h), fun h => (by cases h)⟩, subframe_spNoOvA hno hk⟩ + +theorem subframe_encWfA {fp k vm : U64} (hc : CapRt.Canonical fp) (hal : fp.val % 2 ^ 21 = 0) + (hvm : 1 ≤ vm.val ∧ vm.val ≤ 3) (hk : k.val < 2 ^ 21) : + CapRt.EncWf (.Frame (wa fp k) .Small vm false false false 0#u64 0#u64) := by + refine ⟨canonical_wa_subframeA hc hal hk, by omega, by simp, ?_⟩ + rw [canonical_iff_rangeA]; left; simp + +theorem subframe_srA {fp rt asid va k vm : U64} {mp e : Bool} + (hwf : CapWfA (.Frame fp .Large rt false mp e asid va)) (hk : k.val + 4096 ≤ 2 ^ 21) : + Fl4SpecA64.sameRegionAs + (absCapA (.Frame fp .Large rt false mp e asid va)) + (absCapA (.Frame (wa fp k) .Small vm false false false 0#u64 0#u64)) := by + obtain ⟨-, -, -, hno⟩ := hwf + unfold SpNoOvA at hno + have hle : (specPtr fp).toNat ≤ (specPtr fp + ((1 : BitVec 64) <<< 21) - 1).toNat := BitVec.le_def.1 hno + rw [bv_sub1_toNat, BitVec.toNat_add, bv_shl_one_toNat 21 (by omega)] at hle + have h0 : (specPtr fp).toNat + 2 ^ 21 ≤ 2 ^ 64 := + spNoOv_nat (specPtr fp).isLt (Nat.pow_le_pow_right (by omega) (by omega)) hle + have hkv : k.bv.toNat = k.val := rfl + have htopA : (specPtr fp + ((1 : BitVec 64) <<< 21) - 1).toNat = (specPtr fp).toNat + 2 ^ 21 - 1 := by + rw [bv_sub1_toNat, BitVec.toNat_add, bv_shl_one_toNat 21 (by omega)] + by_cases h : (specPtr fp).toNat + 2 ^ 21 = 2 ^ 64 + · rw [h, Nat.mod_self, if_pos rfl] + · rw [Nat.mod_eq_of_lt (by omega), if_neg (by omega)] + have htopB : (specPtr fp + k.bv + ((1 : BitVec 64) <<< 12) - 1).toNat = (specPtr fp).toNat + k.val + 2 ^ 12 - 1 := by + have hadd : (specPtr fp + k.bv).toNat = (specPtr fp).toNat + k.val := by + rw [BitVec.toNat_add, hkv, Nat.mod_eq_of_lt (by omega)] + rw [bv_sub1_toNat, BitVec.toNat_add, hadd, bv_shl_one_toNat 12 (by omega)] + by_cases h : (specPtr fp).toNat + k.val + 2 ^ 12 = 2 ^ 64 + · rw [h, Nat.mod_self, if_pos rfl] + · rw [Nat.mod_eq_of_lt (by omega), if_neg (by omega)] + have hmain : specPtr fp ≤ specPtr fp + k.bv ∧ + specPtr fp + ((1 : BitVec 64) <<< 21) - 1 ≥ specPtr fp + k.bv + ((1 : BitVec 64) <<< 12) - 1 ∧ + specPtr fp + k.bv ≤ specPtr fp + k.bv + ((1 : BitVec 64) <<< 12) - 1 := by + refine ⟨?_, ?_, ?_⟩ + · rw [BitVec.le_def, BitVec.toNat_add, hkv, Nat.mod_eq_of_lt (by omega)]; omega + · rw [ge_iff_le, BitVec.le_def] + change (specPtr fp + k.bv + ((1 : BitVec 64) <<< 12) - 1).toNat ≤ (specPtr fp + ((1 : BitVec 64) <<< 21) - 1).toNat + rw [htopA, htopB]; omega + · rw [BitVec.le_def, htopB, BitVec.toNat_add, hkv, Nat.mod_eq_of_lt (by omega)]; omega + cases e <;> + simp only [absCapA, absFrameE, Bool.false_eq_true, ↓reduceIte, frameSizeOfA, Fl4SpecA64.sameRegionAs, + Fl4SpecA64.archSameRegionAs, Fl4SpecA64.ExtArchCap.sameRegionWith, Fl4SpecA64.frameInRegion, + Fl4SpecA64.ArchCap.isFrameCap, Fl4SpecA64.ArchCap.acapObj, Fl4SpecA64.ArchCap.acapFsize, + Fl4SpecA64.pageBitsForSize, specPtr_wa] <;> + exact ⟨trivial, hmain⟩ + +open Classical in +theorem subframe_derivA {fp rt asid va k vm : U64} {mp e : Bool} + (hwf : CapWfA (.Frame fp .Large rt false mp e asid va)) (hk : k.val + 4096 ≤ 2 ^ 21) : + Fl4SpecA64.shouldBeParentOf + (absCapA (.Frame fp .Large rt false mp e asid va)) true + (absCapA (.Frame (wa fp k) .Small vm false false false 0#u64 0#u64)) + (decide (Fl4SpecA64.isCapRevocable + (absCapA (.Frame (wa fp k) .Small vm false false false 0#u64 0#u64)) + (absCapA (.Frame fp .Large rt false mp e asid va)))) := + derivA_of _ _ (subframe_srA hwf hk) (fun _ _ _ h => by rcases h with h | h <;> cases h) + +theorem natToCref_eq_bitsOfA (r n : Nat) (hr : r ≤ 64) : + Fl4SpecA64.natToCref r n = bitsOfA (n % 2 ^ 64) r := by + unfold Fl4SpecA64.natToCref Fl4SpecA64.wordBits + rw [toBl_dropA _ r hr, BitVec.toNat_ofNat] + +theorem natToCref_lengthA (r n : Nat) (hr : r ≤ 64) : + (Fl4SpecA64.natToCref r n).length = r := by + rw [natToCref_eq_bitsOfA r n hr]; simp + +theorem cslotAddr_natToCrefA (dest : U64) (r : Nat) (hr : 1 ≤ r ∧ r ≤ 58) (i : Nat) (hi : i < 2 ^ r) : + cslotAddrA (specPtr dest, Fl4SpecA64.natToCref r i) = wa dest ⟨BitVec.ofNat 64 (i * 32)⟩ := by + have hi64 : i * 32 < 2 ^ 64 := by + have h2 : 2 ^ r ≤ 2 ^ 58 := Nat.pow_le_pow_right (by omega) hr.2; omega + have hof : Fl4SpecA64.ofBl (Fl4SpecA64.natToCref r i) = BitVec.ofNat 64 i := by + rw [natToCref_eq_bitsOfA r i (by omega), ofBl_bitsOfA _ r (by omega), + Nat.mod_eq_of_lt (by omega : i < 2 ^ 64), Nat.mod_eq_of_lt hi] + unfold cslotAddrA; dsimp only; rw [slotAddrA_sp, hof] + apply UScalar.eq_of_val_eq + rw [wa_valP] + change (dest.bv + BitVec.ofNat 64 i * 32#64).toNat = (dest.val + (BitVec.ofNat 64 (i * 32)).toNat) % 2 ^ 64 + rw [BitVec.toNat_add, BitVec.toNat_mul, BitVec.toNat_ofNat, BitVec.toNat_ofNat] + have hdv : dest.bv.toNat = dest.val := rfl + rw [hdv, Nat.mod_eq_of_lt (by omega : i < 2 ^ 64), Nat.mod_eq_of_lt (by omega : 32 < 2 ^ 64), + Nat.mod_eq_of_lt hi64, BitVec.toNat_ofNat, Nat.mod_eq_of_lt hi64] + +theorem slotIndex_eqA (dest idx : U64) (h : idx.val < 2 ^ 58) : + obj.Slot.index dest idx = ok (wa dest ⟨BitVec.ofNat 64 (idx.val * 32)⟩) := by + unfold obj.Slot.index + simp only [lift, bind_ok] + congr 1; congr 1 + apply UScalar.eq_of_val_eq + have h32 : layout.cte.SIZE = 32#u64 := by unfold layout.cte.SIZE; rfl + rw [h32, core.num.U64.wrapping_mul_val_eq] + have h32v : (32#u64 : U64).val = 32 := rfl + have hbv : (⟨BitVec.ofNat 64 (idx.val * 32)⟩ : U64).val = (BitVec.ofNat 64 (idx.val * 32)).toNat := rfl + rw [h32v, hbv, BitVec.toNat_ofNat] + simp only [UScalar.size_UScalarTyU64, U64.size_def, U64.numBits, UScalarTy.U64_numBits_eq] + +end subframe_arith + +/-! ## `CarryA` and single-step `cap_insert` for a `.Frame` cap -/ + +section frame_ins +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +private theorem insA_chunkA {mem mem' : U64 → U64} {ss sd : U64} + (hss_nf : ¬ NfKeyA (okeyA (capAtA mem ss))) + (hsd_nf : ¬ NfKeyA (okeyA (capAtA mem' sd))) : + ∀ (ch : List U64), ch.Nodup → sd ∉ ch → + (∀ y ∈ ch, capAtA mem' y = capAtA mem y) → + (∀ (i j k : Nat) y z y', ch[i]? = some y → ch[j]? = some z → ch[k]? = some y' → + i < j → j < k → okeyA (capAtA mem y) = okeyA (capAtA mem y') → okeyA (capAtA mem y) ≠ none → + NfKeyA (okeyA (capAtA mem y)) → okeyA (capAtA mem z) = okeyA (capAtA mem y)) → + ∀ (i j k : Nat) y z y', (insA ss sd ch)[i]? = some y → (insA ss sd ch)[j]? = some z → (insA ss sd ch)[k]? = some y' → + i < j → j < k → okeyA (capAtA mem' y) = okeyA (capAtA mem' y') → okeyA (capAtA mem' y) ≠ none → + NfKeyA (okeyA (capAtA mem' y)) → okeyA (capAtA mem' z) = okeyA (capAtA mem' y) := by + intro ch + induction ch with + | nil => intro _ _ _ _ i _ _ _ _ _ hi; simp [insA] at hi + | cons x l ih => + intro hnd hsd heq hch i j k y z y' hi hj hk hij hjk hyy' hne hnf + rw [List.nodup_cons] at hnd + have hxl : x ≠ sd ∧ sd ∉ l := ⟨fun e => hsd (e ▸ List.mem_cons_self), fun h => hsd (List.mem_cons_of_mem x h)⟩ + have heq_x : capAtA mem' x = capAtA mem x := heq x List.mem_cons_self + have heq_l : ∀ y ∈ l, capAtA mem' y = capAtA mem y := fun y hy => heq y (List.mem_cons_of_mem x hy) + have hch_l : ∀ i j k y z y', l[i]? = some y → l[j]? = some z → l[k]? = some y' → + i < j → j < k → okeyA (capAtA mem y) = okeyA (capAtA mem y') → okeyA (capAtA mem y) ≠ none → + NfKeyA (okeyA (capAtA mem y)) → okeyA (capAtA mem z) = okeyA (capAtA mem y) := + fun i j k y z y' h1 h2 h3 h4 h5 => hch (i + 1) (j + 1) (k + 1) y z y' h1 h2 h3 (by omega) (by omega) + unfold insA at hi hj hk + by_cases hxs : x = ss + · rw [if_pos hxs] at hi hj hk + cases i with + | zero => + simp only [List.getElem?_cons_zero, Option.some.injEq] at hi + subst hi; rw [heq_x, hxs] at hnf; exact absurd hnf hss_nf + | succ i => + cases i with + | zero => + simp only [List.getElem?_cons_succ, List.getElem?_cons_zero, Option.some.injEq] at hi + subst hi; exact absurd hnf hsd_nf + | succ i => + obtain ⟨j', rfl⟩ : ∃ j', j = j' + 2 := ⟨j - 2, by omega⟩ + obtain ⟨k', rfl⟩ : ∃ k', k = k' + 2 := ⟨k - 2, by omega⟩ + simp only [List.getElem?_cons_succ] at hi hj hk + rw [heq_l y (List.mem_of_getElem? hi), heq_l y' (List.mem_of_getElem? hk)] at hyy' + rw [heq_l y (List.mem_of_getElem? hi)] at hne hnf ⊢ + rw [heq_l z (List.mem_of_getElem? hj)] + exact hch_l i j' k' y z y' hi hj hk (by omega) (by omega) hyy' hne hnf + · rw [if_neg hxs] at hi hj hk + cases i with + | succ i => + obtain ⟨j', rfl⟩ : ∃ j', j = j' + 1 := ⟨j - 1, by omega⟩ + obtain ⟨k', rfl⟩ : ∃ k', k = k' + 1 := ⟨k - 1, by omega⟩ + simp only [List.getElem?_cons_succ] at hi hj hk + exact ih hnd.2 hxl.2 heq_l hch_l i j' k' y z y' hi hj hk (by omega) (by omega) hyy' hne hnf + | zero => + simp only [List.getElem?_cons_zero, Option.some.injEq] at hi + subst hi + obtain ⟨j', rfl⟩ : ∃ j', j = j' + 1 := ⟨j - 1, by omega⟩ + obtain ⟨k', rfl⟩ : ∃ k', k = k' + 1 := ⟨k - 1, by omega⟩ + simp only [List.getElem?_cons_succ] at hj hk + by_cases hss : ss ∈ l + swap + · rw [insA_of_not_mem hss] at hj hk + rw [heq_x, heq_l y' (List.mem_of_getElem? hk)] at hyy' + rw [heq_x] at hne hnf ⊢ + rw [heq_l z (List.mem_of_getElem? hj)] + exact hch 0 (j' + 1) (k' + 1) x z y' rfl hj hk (by omega) (by omega) hyy' hne hnf + · have hins_split : ∀ (m : List U64), m.Nodup → ss ∈ m → + ∃ p, m[p]? = some ss ∧ insA ss sd m = m.take (p + 1) ++ sd :: m.drop (p + 1) := by + intro m + induction m with + | nil => intro _ h; cases h + | cons a r ihr => + intro hnd' hm + rw [List.nodup_cons] at hnd' + unfold insA + by_cases ha : a = ss + · subst ha; rw [if_pos rfl]; exact ⟨0, rfl, rfl⟩ + · rw [if_neg ha] + have hr : ss ∈ r := (List.mem_cons.1 hm).resolve_left (Ne.symm ha) + obtain ⟨p, hp, heq'⟩ := ihr hnd'.2 hr + exact ⟨p + 1, hp, by simp [heq']⟩ + obtain ⟨p, hp, hsp⟩ := hins_split l hnd.2 hss + have hplt : p < l.length := (List.getElem?_eq_some_iff.1 hp).1 + have hget : ∀ t, (insA ss sd l)[t]? = + if t ≤ p then l[t]? else if t = p + 1 then some sd else l[t - 1]? := by + intro t + rw [hsp] + by_cases h1 : t ≤ p + · rw [if_pos h1, List.getElem?_append_left (by simp; omega), List.getElem?_take_of_lt (by omega)] + · rw [if_neg h1, List.getElem?_append_right (by simp; omega)] + have hlen : (l.take (p + 1)).length = p + 1 := by simp; omega + rw [hlen] + by_cases h2 : t = p + 1 + · rw [if_pos h2, h2, Nat.sub_self]; rfl + · rw [if_neg h2] + obtain ⟨d, hd⟩ : ∃ d, t - (p + 1) = d + 1 := ⟨t - (p + 2), by omega⟩ + rw [hd, List.getElem?_cons_succ, List.getElem?_drop] + congr 1; omega + have hk_ne : k' ≠ p + 1 := by + intro e; rw [e, hget, if_neg (by omega), if_pos rfl] at hk + cases hk; rw [hyy'] at hnf; exact absurd hnf hsd_nf + by_cases hk_lt : k' ≤ p + · rw [hget j', if_pos (by omega)] at hj + rw [hget k', if_pos hk_lt] at hk + rw [heq_x, heq_l y' (List.mem_of_getElem? hk)] at hyy' + rw [heq_x] at hne hnf ⊢ + rw [heq_l z (List.mem_of_getElem? hj)] + exact hch 0 (j' + 1) (k' + 1) x z y' rfl hj hk (by omega) (by omega) hyy' hne hnf + · rw [hget k', if_neg hk_lt, if_neg hk_ne] at hk + rw [heq_x, heq_l y' (List.mem_of_getElem? hk)] at hyy' + rw [heq_x] at hne hnf + obtain ⟨m, rfl⟩ := Nat.exists_eq_succ_of_ne_zero (by omega : k' ≠ 0) + have hkm : (x :: l)[m + 1]? = some y' := by simpa using hk + have hss_eq := hch 0 (p + 1) (m + 1) x ss y' rfl hp hkm + (by omega) (by omega) hyy' hne hnf + exact absurd (hss_eq ▸ hnf) hss_nf + +theorem finMA_insFrameA {mem mem' : U64 → U64} {G : List (List U64)} {ss sd q vm : U64} + (hF : FinMA mem G) (hnd : G.flatten.Nodup) (hdG : sd ∉ G.flatten) + (heq : ∀ y ∈ G.flatten, capAtA mem' y = capAtA mem y) + (hss_nf : ¬ NfKeyA (okeyA (capAtA mem ss))) + (hsd : capAtA mem' sd = .Frame q .Small vm false false false 0#u64 0#u64) + (hfr : ∀ w ∈ G.flatten, OvlA (absCapA (capAtA mem' sd)) (absCapA (capAtA mem w)) → + isFrameA (capAtA mem w) = true ∧ ∃ ch ∈ G, ss ∈ ch ∧ w ∈ ch) : + FinMA mem' (insG ss sd G) := by + have hmem : ∀ y ∈ (insG ss sd G).flatten, y = sd ∨ y ∈ G.flatten := by + intro y hy + obtain ⟨ch', hch', hych'⟩ := List.mem_flatten.1 hy + rw [insG, List.mem_map] at hch' + obtain ⟨ch, hch, rfl⟩ := hch' + by_cases hss : ss ∈ ch + · have := (insA_perm (dest := sd) hss).mem_iff.1 hych' + rcases List.mem_cons.1 this with rfl | h + · exact Or.inl rfl + · exact Or.inr (List.mem_flatten.2 ⟨ch, hch, h⟩) + · rw [insA_of_not_mem hss] at hych' + exact Or.inr (List.mem_flatten.2 ⟨ch, hch, hych'⟩) + have huniq : ∀ ch1 ∈ G, ∀ ch2 ∈ G, ∀ z, z ∈ ch1 → z ∈ ch2 → ch1 = ch2 := by + have : ∀ L : List (List U64), L.flatten.Nodup → ∀ ch1 ∈ L, ∀ ch2 ∈ L, ∀ z, z ∈ ch1 → z ∈ ch2 → ch1 = ch2 := by + intro L + induction L with + | nil => intro _ _ h; cases h + | cons a l ih => + intro hn ch1 h1 ch2 h2 z hz1 hz2 + rw [List.flatten_cons, List.nodup_append] at hn + rcases List.mem_cons.1 h1 with rfl | h1' <;> rcases List.mem_cons.1 h2 with rfl | h2' + · rfl + · exact absurd rfl (hn.2.2 z hz1 z (List.mem_flatten.2 ⟨ch2, h2', hz2⟩)) + · exact absurd rfl (hn.2.2 z hz2 z (List.mem_flatten.2 ⟨ch1, h1', hz1⟩)) + · exact ih hn.2.1 ch1 h1' ch2 h2' z hz1 hz2 + exact this G hnd + have hsd_in : ∀ ch ∈ G, sd ∈ insA ss sd ch → ss ∈ ch := by + intro ch hch h + by_contra hns + rw [insA_of_not_mem hns] at h + exact hdG (List.mem_flatten.2 ⟨ch, hch, h⟩) + have hsd_nf : ¬ NfKeyA (okeyA (capAtA mem' sd)) := by + rw [hsd]; intro h; exact h q .Small false rfl + have hfr_sd : isFrameA (capAtA mem' sd) = true := by rw [hsd]; rfl + have hzb_sd : isZbA (capAtA mem' sd) = false := by rw [hsd]; rfl + refine ⟨?_, ?_, ?_, ?_⟩ + · -- `conn` + intro ch1' h1 ch2' h2 x hx y hy ho + rw [insG, List.mem_map] at h1 h2 + obtain ⟨ch1, hch1, rfl⟩ := h1 + obtain ⟨ch2, hch2, rfl⟩ := h2 + congr 1 + by_cases hx' : x = sd + · subst x + have hs1 : ss ∈ ch1 := hsd_in ch1 hch1 hx + by_cases hy' : y = sd + · subst y + exact huniq ch1 hch1 ch2 hch2 ss hs1 (hsd_in ch2 hch2 hy) + · have hy2 : y ∈ ch2 := by + by_cases h : ss ∈ ch2 + · exact (List.mem_cons.1 ((insA_perm (dest := sd) h).mem_iff.1 hy)).resolve_left hy' + · rwa [insA_of_not_mem h] at hy + rw [heq y (List.mem_flatten.2 ⟨ch2, hch2, hy2⟩)] at ho + obtain ⟨-, ch, hch, hsc, hyc⟩ := hfr y (List.mem_flatten.2 ⟨ch2, hch2, hy2⟩) ho + exact (huniq ch1 hch1 ch hch ss hs1 hsc).trans (huniq ch hch ch2 hch2 y hyc hy2) + · have hx1 : x ∈ ch1 := by + by_cases h : ss ∈ ch1 + · exact (List.mem_cons.1 ((insA_perm (dest := sd) h).mem_iff.1 hx)).resolve_left hx' + · rwa [insA_of_not_mem h] at hx + by_cases hy' : y = sd + · subst y + have hs2 : ss ∈ ch2 := hsd_in ch2 hch2 hy + rw [heq x (List.mem_flatten.2 ⟨ch1, hch1, hx1⟩)] at ho + obtain ⟨-, ch, hch, hsc, hxc⟩ := hfr x (List.mem_flatten.2 ⟨ch1, hch1, hx1⟩) ho.symm + exact (huniq ch1 hch1 ch hch x hx1 hxc).trans (huniq ch hch ch2 hch2 ss hsc hs2) + · have hy2 : y ∈ ch2 := by + by_cases h : ss ∈ ch2 + · exact (List.mem_cons.1 ((insA_perm (dest := sd) h).mem_iff.1 hy)).resolve_left hy' + · rwa [insA_of_not_mem h] at hy + rw [heq x (List.mem_flatten.2 ⟨ch1, hch1, hx1⟩), heq y (List.mem_flatten.2 ⟨ch2, hch2, hy2⟩)] at ho + exact hF.conn ch1 hch1 ch2 hch2 x hx1 y hy2 ho + · -- `chunk` + intro ch' hch' i j k y z y' hi hj hk hij hjk hyy' hne hnf + rw [insG, List.mem_map] at hch' + obtain ⟨ch, hch, rfl⟩ := hch' + have hnd_ch : ch.Nodup := (List.nodup_flatten.1 hnd).1 ch hch + have hsd_ch : sd ∉ ch := fun h => hdG (List.mem_flatten.2 ⟨ch, hch, h⟩) + have heq_ch : ∀ w ∈ ch, capAtA mem' w = capAtA mem w := fun w hw => heq w (List.mem_flatten.2 ⟨ch, hch, hw⟩) + exact insA_chunkA hss_nf hsd_nf ch hnd_ch hsd_ch heq_ch (hF.chunk ch hch) i j k y z y' hi hj hk hij hjk hyy' hne hnf + · -- `key` + intro x hx y hy hne ho hzx hzy hnf + by_cases hx' : x = sd + · subst x + have hyG : y ∈ G.flatten := (hmem y hy).resolve_left hne.symm + rw [heq y hyG] at ho hnf + exact absurd ⟨hfr_sd, (hfr y hyG ho).1⟩ hnf + · by_cases hy' : y = sd + · subst y + have hxG : x ∈ G.flatten := (hmem x hx).resolve_left hx' + rw [heq x hxG] at ho hnf + exact absurd ⟨(hfr x hxG ho.symm).1, hfr_sd⟩ hnf + · have hxG : x ∈ G.flatten := (hmem x hx).resolve_left hx' + have hyG : y ∈ G.flatten := (hmem y hy).resolve_left hy' + rw [heq x hxG] at ho hzx hnf ⊢ + rw [heq y hyG] at ho hzy hnf ⊢ + exact hF.key x hxG y hyG hne ho hzx hzy hnf + · -- `zfin` + intro x hx y hy hne hzx ho + have hx' : x ≠ sd := fun e => by rw [e, hzb_sd] at hzx; cases hzx + have hxG : x ∈ G.flatten := (hmem x hx).resolve_left hx' + by_cases hy' : y = sd + · subst y + rw [heq x hxG] at hzx ho + have hfx := (hfr x hxG ho.symm).1 + cases hc : capAtA mem x <;> rw [hc] at hzx hfx <;> simp [isFrameA, isZbA] at hzx hfx + · have hyG : y ∈ G.flatten := (hmem y hy).resolve_left hy' + rw [heq x hxG] at hzx ho; rw [heq y hyG] at ho + exact hF.zfin x hxG y hyG hne hzx ho + +private theorem utInvA_insSubframeA {s1 s2 : S} {OS : Set U64} {par : U64 → Option U64} + (hU : UtInvA st OS par s1) (hP : ParIn OS par) {src dest : U64} + (hsrc : src ∈ OS) (hd : dest ∈ OS) (hsd : src ≠ dest) + (hdp : ∀ z, par z ≠ some dest) (hdn : par dest = none) + (hk : ∀ y ∈ OS, y ≠ dest → capA st s2 y = capA st s1 y) + {fp rt asid va k vm : U64} {mp e : Bool} + (hcs : capA st s1 src = .Frame fp .Large rt false mp e asid va) + (hcd : capA st s2 dest = .Frame (wa fp k) .Small vm false false false 0#u64 0#u64) + (hfpAl : fp.val % 2 ^ 21 = 0) (hkB : k.val + 4096 ≤ 2 ^ 21) (bp : Bool) : + UtInvA st OS (Function.update par dest (if bp then some src else par src)) s2 := by + have hnp : (if bp then some src else par src) ≠ some dest := by cases bp <;> simp [hsd, hdp src] + have hD : ∀ y x, Fl4Proofs.P3.Mdb.Desc (Function.update par dest (if bp then some src else par src)) y x ↔ + Fl4Proofs.P3.Mdb.Desc par y x ∨ (y = dest ∧ ((bp = true ∧ x = src) ∨ Fl4Proofs.P3.Mdb.Desc par src x)) := by + intro y x; rw [Fl4Proofs.P3.Mdb.desc_update_iff hdp hdn hnp, npA_desc_iff] + have ndD : ∀ x, ¬ Fl4Proofs.P3.Mdb.Desc par dest x := fun x => Fl4Proofs.P3.Mdb.not_desc_of_none hdn + have nDd : ∀ y, ¬ Fl4Proofs.P3.Mdb.Desc par y dest := fun y => Fl4Proofs.P3.Mdb.not_desc_of_no_child hdp + have hfpFit : fp.val + 2 ^ 21 ≤ 2 ^ 64 := by have : fp.val < 2 ^ 64 := fp.hBounds; omega + have hbaseS : baseA (capA st s1 src) = fp.val := by + rw [hcs]; unfold baseA; rw [ptrA_of rfl] + have htopS : topA (capA st s1 src) = fp.val + 2 ^ 21 - 1 := by + rw [hcs]; unfold topA + rw [ptrA_of rfl, sizeA_of (v := 21#u64) rfl] + have hm : (mskA 21#u64).val = 2 ^ 21 - 1 := mskA_val (by decide) + rw [wa_valP, hm]; exact Nat.mod_eq_of_lt (by omega) + have hbaseD : baseA (capA st s2 dest) = fp.val + k.val := by + rw [hcd]; unfold baseA; rw [ptrA_of rfl, wa_valP]; exact Nat.mod_eq_of_lt (by omega) + have htopD : topA (capA st s2 dest) = fp.val + k.val + 4095 := by + rw [hcd]; unfold topA + rw [ptrA_of rfl, sizeA_of (v := 12#u64) rfl] + have hm : (mskA 12#u64).val = 4095 := mskA_val (by decide) + have hfk : (wa fp k).val = fp.val + k.val := by rw [wa_valP]; exact Nat.mod_eq_of_lt (by omega) + rw [wa_valP, hfk, hm]; exact Nat.mod_eq_of_lt (by omega) + have hphysS : physA (capA st s1 src) = true := by rw [hcs]; exact physA_of rfl + have hphysD : physA (capA st s2 dest) = true := by rw [hcd]; exact physA_of rfl + have hnuS : isUtA (capA st s1 src) = false := by rw [hcs]; rfl + have hnuD : isUtA (capA st s2 dest) = false := by rw [hcd]; rfl + refine ⟨?_, ?_, ?_, ?_, ?_, ?_, ?_⟩ + · -- reg + intro x hx hp + by_cases hxd : x = dest + · subst hxd; rw [hbaseD, htopD]; omega + · rw [hk x hx hxd] at hp ⊢; exact hU.reg x hx hp + · -- inc + intro x hx p b i d hxu y hyd + have hxd : x ≠ dest := fun he => by rw [he, hcd] at hxu; cases hxu + rw [hk x hx hxd] at hxu + have hxs : x ≠ src := fun he => by rw [he, hcs] at hxu; cases hxu + rw [hD] at hyd + rcases hyd with hy0 | ⟨rfl, ⟨-, he⟩ | hds⟩ + · have hyd' : y ≠ dest := fun he => ndD x (he ▸ hy0) + have hyOS : y ∈ OS := (desc_in hP hy0).1 + rw [hk y hyOS hyd']; exact hU.inc x hx p b i d hxu y hy0 + · exact absurd he hxs + · obtain ⟨-, hb, ht⟩ := hU.inc x hx p b i d hxu src hds + rw [hbaseS] at hb; rw [htopS] at ht + exact ⟨hphysD, by rw [hbaseD]; omega, by rw [htopD]; omega⟩ + · -- noov + intro x hx p b i d hxu y hy hp + have hxd : x ≠ dest := fun he => by rw [he, hcd] at hxu; cases hxu + rw [hk x hx hxd] at hxu + by_cases hyd : y = dest + · subst hyd + rcases hU.noov x hx p b i d hxu src hsrc hphysS with hdis | ⟨hu, -⟩ + · left; rcases hdis with h1 | h2 + · left; rw [htopS] at h1; rw [htopD]; omega + · right; rw [hbaseS] at h2; rw [hbaseD]; omega + · rw [hnuS] at hu; cases hu + · rw [hk y hy hyd] at hp ⊢; rw [hk x hx hxd] + exact hU.noov x hx p b i d hxu y hy hp + · -- excl + intro x hx y hy hxy p b i i' d d' h1 h2 + have hxd : x ≠ dest := fun he => by rw [he, hcd] at h1; cases h1 + have hyd : y ≠ dest := fun he => by rw [he, hcd] at h2; cases h2 + rw [hk x hx hxd] at h1; rw [hk y hy hyd] at h2 + exact hU.excl x hx y hy hxy p b i i' d d' h1 h2 + · -- fib + intro x hx p b i d hxu + have hxd : x ≠ dest := fun he => by rw [he, hcd] at hxu; cases hxu + rw [hk x hx hxd] at hxu; exact hU.fib x hx p b i d hxu + · -- drange + intro x hx p b i d hxu y hy hyx hp hnd + have hxd : x ≠ dest := fun he => by rw [he, hcd] at hxu; cases hxu + rw [hk x hx hxd] at hxu + have hxs : x ≠ src := fun he => by rw [he, hcs] at hxu; cases hxu + by_cases hyd : y = dest + · subst hyd + have hndS : ¬ DisjA (capA st s1 src) (capA st s1 x) := fun hdis => hnd (by + rw [hk x hx hxd] + rcases hdis with h1 | h2 + · left; rw [htopS] at h1; rw [htopD]; omega + · right; rw [hbaseS] at h2; rw [hbaseD]; omega) + rcases hU.drange x hx p b i d hxu src hsrc hxs.symm hphysS hndS with h1 | h2 + · left; exact (hD y x).2 (Or.inr ⟨rfl, Or.inr h1⟩) + · have := hU.anc src x h2 (by rw [hxu]; rfl) + rw [hnuS] at this; cases this + · rw [hk y hy hyd] at hp hnd; rw [hk x hx hxd] at hnd + rcases hU.drange x hx p b i d hxu y hy hyx hp hnd with h1 | h2 + · left; exact (hD y x).2 (Or.inl h1) + · right; exact (hD x y).2 (Or.inl h2) + · -- anc + intro x y hyd hu + rw [hD] at hyd + rcases hyd with hy0 | ⟨rfl, -⟩ + · have hyd' : y ≠ dest := fun he => ndD x (he ▸ hy0) + have hxd' : x ≠ dest := fun he => nDd y (he ▸ hy0) + have ⟨hyOS, hxOS⟩ := desc_in hP hy0 + rw [hk y hyOS hyd'] at hu; rw [hk x hxOS hxd'] + exact hU.anc x y hy0 hu + · rw [hnuD] at hu; cases hu + +private theorem insWC_tree_subframeA {T Ω Sg OS : Set U64} {G : List (List U64)} {par : U64 → Option U64} + (hO : OSOK T Ω Sg OS) {s1 : S} (hT : TreeA st OS G par s1) {src dest : U64} + {fp rt asid va k vm : U64} {mp e : Bool} + (hsrc : src ∈ G.flatten) (hd : dest ∈ OS) (hd0 : capA st s1 dest = .Null) + (hdc : obj.sext48 dest = ok dest) + (hsc : capA st s1 src = .Frame fp .Large rt false mp e asid va) + (hfpC : CapRt.Canonical fp) (hfpAl : fp.val % 2 ^ 21 = 0) + (hvm : 1 ≤ vm.val ∧ vm.val ≤ 3) (hkB : k.val + 4096 ≤ 2 ^ 21) (bp : Bool) + (hsib : bp = false → ∀ y, ¬ Fl4Proofs.P3.Mdb.Desc par y src) : + cdt.insert_with_cap st s1 + (.Frame (wa fp k) .Small vm false false false 0#u64 0#u64) (capA st s1 src) src dest ⦃ s2 => + TreeA st OS (insG src dest G) + (Function.update par dest (if bp then some src else par src)) s2 ⦄ := by + set nc : capability.Cap := .Frame (wa fp k) .Small vm false false false 0#u64 0#u64 + have hM := hT.lk + have h0 : ∀ y ∈ OS, y ≠ 0#u64 := hO.nz + have inOS : ∀ y ∈ G.flatten, y ∈ OS := fun y hy => ((hM.members y).1 hy).1 + have F := hM.lkF h0 + have os := inOS src hsrc + have hs0 : capA st s1 src ≠ .Null := ((hM.members src).1 hsrc).2 + have dG : dest ∉ G.flatten := fun hm => ((hM.members dest).1 hm).2 hd0 + have hsd : src ≠ dest := fun e' => dG (e' ▸ hsrc) + have hNm : NxA st s1 src ≠ 0#u64 → NxA st s1 src ∈ G.flatten := F.nxm _ hsrc + have hNd : NxA st s1 src ≠ dest := fun e' => by + have h1 : NxA st s1 src ≠ 0#u64 := fun e0 => h0 dest hd (e' ▸ e0) + exact dG (e' ▸ hNm h1) + have hw : CapRt.EncWf nc := subframe_encWfA hfpC hfpAl hvm (by omega) + have hnf0 : ¬ FullCondA (capA st s1 src) nc := by + rw [hsc]; unfold FullCondA; simp [nc] + refine WP.spec_mono (WP.spec_and + (cdt.insert_with_cap.lr hO (LR.self s1) os hd hsd hdc rfl hs0 hw (fun h => inOS _ (hNm h))) + (insWC_walkA hO os hd hsd rfl hw (fun h => inOS _ (hNm h)))) ?_ + rintro s2 ⟨⟨hL, ed, -, -, ek, -⟩, -, -, -, hnf⟩ + have hsrc_eq : capA st s2 src = capA st s1 src := hnf hnf0 + have hkAll : ∀ y ∈ OS, y ≠ dest → capA st s2 y = capA st s1 y := fun y hy hyd => by + by_cases hys : y = src + · subst hys; exact hsrc_eq + · exact ek y hy hys hyd + have hLk : MdbLk st OS (insG src dest G) s2 := by + refine hM.ins h0 hsrc hd hd0 (by rw [ed]; intro h; dsimp only [nc] at h; cases h) + (fun y hy hyd => by rw [hkAll y hy hyd]) ?_ ?_ ?_ (fun hN => ?_) (fun y hy h1 h2 => ?_) (fun y hy h1 h2 => ?_) + · rw [(hL src os).2]; simp + · rw [(hL dest hd).1]; simp [hNd.symm] + · rw [(hL dest hd).2]; simp [hsd.symm] + · rw [(hL _ (inOS _ (hNm hN))).1]; simp [hN] + · rw [(hL y hy).2]; simp [h1, h2] + · rw [(hL y hy).1]; simp [h1, h2] + refine ⟨hLk, by rw [insG_eq]; exact hT.tree.insert hsrc dG (by cases bp; exacts [Or.inr ⟨rfl, hsib rfl⟩, Or.inl rfl]), ?_⟩ + refine utInvA_insSubframeA hT.ut hT.parIn os hd hsd (fun z e' => dG (hT.tree.par_mem z dest e').2) ?_ hkAll hsc ed hfpAl hkB bp + cases e' : par dest with + | none => rfl + | some q => exact absurd (hT.tree.par_mem dest q e').1 dG + +private theorem cap_insert_subframe_refinesA {T Ω Sg OS : Set U64} {th : TypedHeapA} {kb : U64} + (s1 : S) (σ1 : Fl4SpecA64.State Unit) (G1 : MdbGhostA) + (hR : StateRelA th (L.mem s1) σ1) (hC : CdtRelA st th s1 σ1 G1) (hP : PlaceA th kb) + (hO : OSOK T Ω Sg OS) (hT : TreeA st OS G1.chains G1.par s1) (hsub : ∀ y, IsSlotA th y → y ∈ OS) + (hnul : ∀ y ∈ OS, ¬ IsSlotA th y → capA st s1 y = .Null) + {ss sd : Fl4SpecA64.CslotPtr} (hAs : AbsSlotA th ss) (hAd : AbsSlotA th sd) + {fp rt asid va k vm : U64} {mp e : Bool} + (hsrc : capAtA (L.mem s1) (cslotAddrA ss) = .Frame fp .Large rt false mp e asid va) + (hdn : capAtA (L.mem s1) (cslotAddrA sd) = .Null) + (hdc : obj.sext48 (cslotAddrA sd) = ok (cslotAddrA sd)) + (hfpC : CapRt.Canonical fp) (hfpAl : fp.val % 2 ^ 21 = 0) + (hvm : 1 ≤ vm.val ∧ vm.val ≤ 3) (hkB : k.val + 4096 ≤ 2 ^ 21) : + let nc := capability.Cap.Frame (wa fp k) .Small vm false false false 0#u64 0#u64 + let anc := absCapA nc + cdt.insert st s1 nc (cslotAddrA ss) (cslotAddrA sd) ⦃ s2 => + ∃ σ2, ((), σ2) ∈ (Fl4SpecA64.capInsert (Z := Unit) anc ss sd σ1).1 ∧ + StateRelA th (L.mem s2) σ2 ∧ + CdtRelA st th s2 σ2 (insGA G1 (cslotAddrA ss) (cslotAddrA sd) + (insBA σ1 ss anc (absCapA (capA st s1 (cslotAddrA ss))))) ∧ + TreeA st OS (insG (cslotAddrA ss) (cslotAddrA sd) G1.chains) + (insGA G1 (cslotAddrA ss) (cslotAddrA sd) + (insBA σ1 ss anc (absCapA (capA st s1 (cslotAddrA ss))))).par s2 ∧ + SpecSlotA σ1 σ2 ∧ + ∀ y, IsSlotA th y → capAtA (L.mem s2) y = capAtA (L.mem s1) y ∨ capAtA (L.mem s2) y = nc ∨ + ∃ p b i d, capAtA (L.mem s2) y = .Untyped p b i d ⦄ := by + intro nc anc + have hsS := hAs.isSlot hP + have hdS := hAd.isSlot hP + have hsn : capAtA (L.mem s1) (cslotAddrA ss) ≠ .Null := by rw [hsrc]; intro h; cases h + have hsG : cslotAddrA ss ∈ G1.chains.flatten := (hT.lk.members _).2 ⟨hsub _ hsS, hsn⟩ + have hdG : cslotAddrA sd ∉ G1.chains.flatten := fun hm => ((hT.lk.members _).1 hm).2 hdn + have hsd : cslotAddrA ss ≠ cslotAddrA sd := fun e' => hdG (e' ▸ hsG) + have hne : ss ≠ sd := fun e' => hsd (by rw [e']) + have F := hT.lk.lkF hO.nz + have inOS : ∀ y ∈ G1.chains.flatten, y ∈ OS := fun y hy => ((hT.lk.members y).1 hy).1 + obtain ⟨asc, hgs, hsrel⟩ := hR.getAbsA hAs + obtain ⟨adc, hgd, hdrel⟩ := hR.getAbsA hAd + have hadc : adc = .nullCap := by have h := hdrel.2; rw [hdn] at h; exact h.symm + subst hadc + have hwf : CapWfA nc := subframe_wfA (by rw [← hsrc]; exact hsrel.1) hvm hkB + have hw : CapRt.EncWf nc := subframe_encWfA hfpC hfpAl hvm (by omega) + have hanc : CapEqA nc anc := CapEqA.of_abs rfl + have hkSB : SameBadgeKindA nc (capA st s1 (cslotAddrA ss)) := by + dsimp only [nc]; show SameBadgeKindA _ (capAtA (L.mem s1) (cslotAddrA ss)); rw [hsrc]; simp [SameBadgeKindA] + have hsib : insBA σ1 ss anc (absCapA (capA st s1 (cslotAddrA ss))) = false → + ∀ y, ¬ Fl4Proofs.P3.Mdb.Desc G1.par y (cslotAddrA ss) := + hsib_ofA hC hAs (by + show Fl4SpecA64.shouldBeParentOf (absCapA (capAtA (L.mem s1) (cslotAddrA ss))) _ _ _ + rw [hsrc]; exact subframe_derivA (vm := vm) (by rw [← hsrc]; exact hsrel.1) hkB) + classical + have hasc : asc = absCapA (capA st s1 (cslotAddrA ss)) := by + have h : CapEqA (capAtA (L.mem s1) (cslotAddrA ss)) asc := hsrel.2 + show asc = absCapA (capAtA (L.mem s1) (cslotAddrA ss)) + rw [hsrc] at h ⊢; exact h.symm + have hisRev : Fl4SpecA64.isCapRevocable anc asc ↔ + Fl4SpecA64.isCapRevocable anc (absCapA (capA st s1 (cslotAddrA ss))) := by + rw [hasc] + have hsetUtEq : Fl4SpecA64.setUntypedCapAsFull (Z := Unit) asc anc ss σ1 = + Fl4SpecA64.setUntypedCapAsFull (Z := Unit) (absCapA (capA st s1 (cslotAddrA ss))) anc ss σ1 := by + rw [hasc] + have hv : revFA nc (capA st s1 (cslotAddrA ss)) = + decide (Fl4SpecA64.isCapRevocable anc (absCapA (capA st s1 (cslotAddrA ss)))) := by + rw [Bool.eq_iff_iff]; simp only [decide_eq_true_eq]; exact revFA_spec _ _ hkSB hwf + unfold cdt.insert + rw [capA_eq, bind_ok] + refine WP.spec_mono (WP.spec_and (WP.spec_and + (insWC_tree_subframeA hO hT hsG (hsub _ hdS) hdn hdc hsrc hfpC hfpAl hvm hkB + (insBA σ1 ss anc (absCapA (capA st s1 (cslotAddrA ss)))) hsib) + (cdt.insert_with_cap.lr hO (LR.self s1) (hsub _ hsS) (hsub _ hdS) hsd hdc rfl hsn hw + (fun h => inOS _ (F.nxm _ hsG h)))) + (insWC_walkA hO (hsub _ hsS) (hsub _ hdS) hsd rfl hw (fun h => inOS _ (F.nxm _ hsG h)))) ?_ + rintro s2 ⟨⟨hT', -, ed, -, -, ek, -⟩, hb, hdb, -, hnf⟩ + have hnc_fc : ¬ FullCondA (capA st s1 (cslotAddrA ss)) nc := by + show ¬ FullCondA (capAtA (L.mem s1) (cslotAddrA ss)) nc + rw [hsrc]; unfold FullCondA; dsimp only [nc]; simp + have hm1 : ∀ y, IsSlotA th y → y ≠ cslotAddrA ss → + capAtA (mixA (L.mem s1) (L.mem s2) (cslotAddrA sd)) y = capAtA (L.mem s1) y := by + intro y hy hys + by_cases e' : y = cslotAddrA sd + · rw [e']; exact capAt_mixA_self _ _ _ + · rw [capAt_mixA_other _ _ (slot_ne8 hP hdS hy e')]; exact ek y (hsub y hy) hys e' + have hms : capAtA (mixA (L.mem s1) (L.mem s2) (cslotAddrA sd)) (cslotAddrA ss) = + capA st s2 (cslotAddrA ss) := capAt_mixA_other _ _ (slot_ne8 hP hdS hsS hsd) + have h1 : ((), σ1) ∈ (Fl4SpecA64.setUntypedCapAsFull (Z := Unit) + (absCapA (capA st s1 (cslotAddrA ss))) anc ss σ1).1 := by + unfold Fl4SpecA64.setUntypedCapAsFull + rw [if_neg fun h => hnc_fc ((utCondA_iff _ _).1 h)]; exact rfl + have hR1 : StateRelA th (mixA (L.mem s1) (L.mem s2) (cslotAddrA sd)) σ1 := + hR.congrA fun y hy => by + have hy' := slotW_isSlot hP hy + by_cases e' : y = cslotAddrA ss + · rw [e', hms]; exact hnf hnc_fc + · exact hm1 y hy' e' + obtain ⟨o2, h2, hR2, hS2⟩ := StateRelA.setAbsA (mem' := L.mem s2) hR1 hP hAd (a := anc) + (by show CapRelA (capA st s2 _) _; rw [ed]; exact ⟨hwf, hanc⟩) + (fun y hy hyd => (capAt_mixA_other _ _ (slot_ne8 hP hdS hy hyd)).symm) + have h1' : ((), σ1) ∈ (Fl4SpecA64.setUntypedCapAsFull (Z := Unit) asc anc ss σ1).1 := by + rw [hsetUtEq]; exact h1 + have hinsEq : decide (Fl4SpecA64.isCapRevocable anc asc) = + decide (Fl4SpecA64.isCapRevocable anc (absCapA (capA st s1 (cslotAddrA ss)))) := + decide_eq_decide.mpr hisRev + have hsbEq : ∀ o r, Fl4SpecA64.shouldBeParentOf asc o anc r ↔ + Fl4SpecA64.shouldBeParentOf (absCapA (capA st s1 (cslotAddrA ss))) o anc r := + fun o r => by rw [hasc] + have hmem := capInsert_memA hgs hgd h1' h2 + simp only [hinsEq, decide_eq_decide.mpr (hsbEq _ _)] at hmem + refine ⟨_, hmem, hR2.of_kheap rfl, + CdtRelA.insertA hP hC hAs hAd _ (decide (Fl4SpecA64.isCapRevocable anc + (absCapA (capA st s1 (cslotAddrA ss))))) ?_ ?_ ?_ ?_ hne + (fun h => by unfold insBA at h; exact (of_decide_eq_true h).1) + (fun sl h => hdG (hC.tree.tree.par_mem _ _ + (by have e' := hC.cdt sl (hC.cdt_slot sl _ h).1; rw [h] at e'; rw [← e']; rfl)).2), hT', + hS2.trans (SpecSlotA.insFin _ _ _ _ _), + fun y hy => ?_⟩ + · refine hT'.sub (fun y hy => hsub y hy) fun y hy hn' => ?_ + have f1 : y ≠ cslotAddrA ss := fun e' => hn' (e' ▸ hsS) + have f2 : y ≠ cslotAddrA sd := fun e' => hn' (e' ▸ hdS) + rw [ek y hy f1 f2]; exact hnul y hy hn' + · rfl + · rfl + · intro sl hsl + by_cases e' : sl = sd + · rw [if_pos e', e', ← hv]; exact revA_of_val hdb + · rw [if_neg e'] + exact revA_of_keep (hb _ (hsub _ (hsl.isSlot hP)) (fun h => e' (cslotAddrA_inj hP hsl hAd h))) + · by_cases f2 : y = cslotAddrA sd + · exact Or.inr (Or.inl (by rw [f2]; exact ed)) + · by_cases f1 : y = cslotAddrA ss + · exact Or.inl (by rw [f1]; exact hnf hnc_fc) + · exact Or.inl (ek y (hsub y hy) f1 f2) + +/-- Single `cdt.insert` step for a 4 KiB subframe cap: preserves `JAtZAX`, `TreeA`, `CarryA`, `ksb`, `cpu`, +`hnul`, and every slot cap off `cslotAddrA sd` (without rebuilding `JEntA`). -/ +theorem cap_insert_frame_stepA + (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + {s1 : S} {σ1 : Fl4SpecA64.State Unit} {G1 : MdbGhostA} + (hJ1 : JAtZAX (L := L) AR { Z with G := G1 } s1 a σ1 m) + (hT1 : TreeA st OS G1.chains G1.par s1) + (hks1 : L.ksb s1 = L.ksb s) (hcpu1 : L.cpu s1 = L.cpu s) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s1 y = .Null) + {ss sd : Fl4SpecA64.CslotPtr} (hAs : AbsSlotA Z.th ss) (hAd : AbsSlotA Z.th sd) + {fp rt asid va k vm : U64} {mp e : Bool} + (hsrc : capAtA (L.mem s1) (cslotAddrA ss) = .Frame fp .Large rt false mp e asid va) + (hdn : capAtA (L.mem s1) (cslotAddrA sd) = .Null) + (hdc : obj.sext48 (cslotAddrA sd) = ok (cslotAddrA sd)) + (hfpC : CapRt.Canonical fp) (hfpAl : fp.val % 2 ^ 21 = 0) + (hvm : 1 ≤ vm.val ∧ vm.val ≤ 3) (hk : k.val + 4096 ≤ 2 ^ 21) + (hfr : ∀ w ∈ G1.chains.flatten, OvlA (absCapA (.Frame (wa fp k) .Small vm false false false 0#u64 0#u64)) + (absCapA (capAtA (L.mem s1) w)) → + isFrameA (capAtA (L.mem s1) w) = true ∧ ∃ ch ∈ G1.chains, cslotAddrA ss ∈ ch ∧ w ∈ ch) : + let nc := capability.Cap.Frame (wa fp k) .Small vm false false false 0#u64 0#u64 + let anc := absCapA nc + cdt.insert st s1 nc (cslotAddrA ss) (cslotAddrA sd) ⦃ s2 => + ∃ σ2 G2, + G2.chains = insG (cslotAddrA ss) (cslotAddrA sd) G1.chains ∧ + ((), σ2) ∈ (Fl4SpecA64.capInsert (Z := Unit) anc ss sd σ1).1 ∧ + JAtZAX (L := L) AR { Z with G := G2 } s2 a σ2 m ∧ + TreeA st OS G2.chains G2.par s2 ∧ + L.ksb s2 = L.ksb s ∧ L.cpu s2 = L.cpu s ∧ + (∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s2 y = .Null) ∧ + (∀ y ∈ OS, y ≠ cslotAddrA sd → capAtA (L.mem s2) y = capAtA (L.mem s1) y) ∧ + capAtA (L.mem s2) (cslotAddrA sd) = nc ∧ + (CarryA { Z with G := G1 } (L.mem s1) σ1 → CarryA { Z with G := G2 } (L.mem s2) σ2) ⦄ := by + intro nc anc + have hO := E.sys.ent.os + have hP := hJ1.1.place + have hsS : IsSlotA Z.th (cslotAddrA ss) := hAs.isSlot hP + have hdS : IsSlotA Z.th (cslotAddrA sd) := hAd.isSlot hP + have hsn : capAtA (L.mem s1) (cslotAddrA ss) ≠ .Null := by rw [hsrc]; intro h; cases h + have hsG : cslotAddrA ss ∈ G1.chains.flatten := (hT1.lk.members _).2 ⟨hsub _ hsS, hsn⟩ + have hdG : cslotAddrA sd ∉ G1.chains.flatten := fun hm => ((hT1.lk.members _).1 hm).2 hdn + have hsd : cslotAddrA ss ≠ cslotAddrA sd := fun e => hdG (e ▸ hsG) + have F := hT1.lk.lkF hO.nz + have inOS : ∀ y ∈ G1.chains.flatten, y ∈ OS := fun y hy => ((hT1.lk.members y).1 hy).1 + obtain ⟨_, -, hcr⟩ := hJ1.1.st.getAbsA hAs + rw [hsrc] at hcr + have hwf : CapWfA nc := subframe_wfA hcr.1 hvm hk + have hw : CapRt.EncWf nc := subframe_encWfA hfpC hfpAl hvm (by omega) + have hderiv := subframe_derivA (vm := vm) hcr.1 hk + have hwr : cdt.insert st s1 nc (cslotAddrA ss) (cslotAddrA sd) ⦃ s2 => + WritesIn st s1 s2 (W7.CapW H.tcbs {b | InOS OS b} ∅) ∧ + (∀ y ∈ OS, y ≠ cslotAddrA sd → capA st s2 y = capA st s1 y) ∧ + capA st s2 (cslotAddrA sd) = nc ⦄ := by + unfold cdt.insert + rw [capA_eq, bind_ok] + have hlr0 := cdt.insert_with_cap.lr (osokA hO) (LR.self s1) (hsub _ hsS) (hsub _ hdS) hsd hdc rfl hsn hw + (fun h => inOS _ (F.nxm _ hsG h)) + have hlr := cdt.insert_with_cap.lrF (osokA hO) (LR.self s1) (hsub _ hsS) (hsub _ hdS) hsd hdc rfl hsn hw + (fun h => inOS _ (F.nxm _ hsG h)) + have hwk := insWC_walkA (osokA hO) (hsub _ hsS) (hsub _ hdS) hsd rfl hw + (fun h => inOS _ (F.nxm _ hsG h)) + have hnf0 : ¬ FullCondA (capA st s1 (cslotAddrA ss)) nc := by + show ¬ FullCondA (capAtA (L.mem s1) (cslotAddrA ss)) nc + rw [hsrc]; unfold FullCondA; simp [nc] + refine WP.spec_mono (WP.spec_and (WP.spec_and hlr hlr0) hwk) ?_ + rintro s2 ⟨⟨h1, ⟨-, ed, -, -, ek, -⟩⟩, -, -, -, hnf⟩ + refine ⟨h1.1, fun y hy hne => ?_, ed⟩ + by_cases hys : y = cslotAddrA ss + · subst hys; exact hnf hnf0 + · exact ek y hy hys hne + have href := cap_insert_subframe_refinesA s1 σ1 G1 hJ1.1.st hJ1.1.cdt hP hO hT1 hsub hnul hAs hAd + hsrc hdn hdc hfpC hfpAl hvm hk + refine WP.spec_mono (WP.spec_and href hwr) fun s2 ⟨⟨σ2, hm, hst2, hcdt2, hT2, hSS2, hcs2⟩, hw2, hkeep, hsd2⟩ => ?_ + have hks2 : L.ksb s2 = L.ksb s := hw2.1.trans hks1 + have hcpu2 : L.cpu s2 = L.cpu s := hw2.2.1.trans hcpu1 + have hfnOff : ∀ b, FootNA (L := L) { Z with G := G1 } s1 b → ¬ W7.CapW H.tcbs {b | InOS OS b} ∅ b := by + intro b hb + have hb0 : FootNA (L := L) Z s b := by + have hna := nodeAt_ksb hks1 + cases hb with + | node c _ hc ho => simp only [hna] at ho; exact .node c _ hc ho + | cur _ ho => simp only [hcpu1, hna] at ho; exact .cur _ ho + | cur2 _ ho => simp only [hcpu1, hna] at ho; exact .cur2 _ ho + | tcb y _ hy ho => exact .tcb y _ hy ho + | tcb2 y _ hy ho => exact .tcb2 y _ hy ho + | sc c hc => exact .sc c hc + | sc2 c hc => exact .sc2 c hc + | irq i hi => rw [hks1] at *; exact .irq i hi + | glob _ ho => rw [hks1] at ho; exact .glob _ ho + | vcpu v _ hv hw => exact .vcpu v _ hv hw + | vs _ h => rw [hks1] at h; exact .vs _ h + | ext _ h => exact .ext _ h + exact footN_offA E b hb0 + have hal1 : ∀ b, FootNA (L := L) { Z with G := G1 } s1 b → Aligned b := + FootNA.aligned hJ1.1 (fun c hc o ho => by rw [nodeAt_ksb hks1]; exact bxi_nodes E.sys.ent.bxi c hc o ho) + E.scAl E.ap.vcAl + set G2 := insGA G1 (cslotAddrA ss) (cslotAddrA sd) + (insBA σ1 ss (absCapA nc) (absCapA (capA st s1 (cslotAddrA ss)))) + have hJ2 : JAtZAX (L := L) AR { Z with G := G2 } s2 a σ2 m := by + refine hJ1.of_slotWrites hw2 (fun b hw' hb => hfnOff b hb hw') hal1 G2 hSS2 hst2 hcdt2 fun y hy => ?_ + rcases hcs2 y hy with e | e | ⟨p, b, i, d, e⟩ <;> rw [e] + · exact hJ1.capObj hy + · exact ⟨fun _ h => (by cases h), fun _ _ _ _ _ h => (by cases h), + fun _ _ _ h => (by cases h), fun _ _ _ h => (by cases h), + fun h => (by cases h)⟩ + · exact ⟨fun _ h => (by cases h), fun _ _ _ _ _ h => (by cases h), fun _ _ _ h => (by cases h), + fun _ _ _ h => (by cases h), trivial⟩ + refine ⟨σ2, G2, rfl, hm, hJ2, hT2, hks2, hcpu2, fun y hy hni => ?_, hkeep, hsd2, fun hC => ?_⟩ + · have hne : y ≠ cslotAddrA sd := fun e => hni (e ▸ hdS) + rw [hkeep y hy hne]; exact hnul y hy hni + · refine ⟨hC.1.of_capsA rfl (fun _ h => h) (fun _ h => h) (fun y hy => ?_), ?_⟩ + · by_cases hne : y = cslotAddrA sd + · exact Or.inr (fun _ _ _ h => by rw [hne] at h; change capA st s2 (cslotAddrA sd) = _ at h; rw [hsd2] at h; dsimp only [nc] at h; cases h) + · exact Or.inl (hkeep y (hsub y hy) hne) + · refine ⟨hC.2.kt.of_typesA (fun o r cs ho => ?_) (fun o t ho => ?_), ?_⟩ + · rcases hSS2.kh o with h | ⟨r', cs0, cs', h1, h2⟩ | ⟨x0, x', h1, h2, -⟩ + · exact ⟨cs, h ▸ ho⟩ + · rw [ho] at h2; cases h2; exact ⟨cs0, h1⟩ + · rw [ho] at h2; cases h2 + · rcases hSS2.kh o with h | ⟨r', cs0, cs', h1, h2⟩ | ⟨x0, x', h1, h2, -⟩ + · exact ⟨t, h ▸ ho⟩ + · rw [ho] at h2; cases h2 + · rw [ho] at h2; cases h2; exact ⟨x0, h1⟩ + · exact finMA_insFrameA hC.2.fm hT1.lk.nodup hdG + (fun y hy => hkeep y ((hT1.lk.members y).1 hy).1 (fun e => hdG (e ▸ hy))) + (by rw [hsrc]; intro h; exact h fp .Large false rfl) hsd2 (fun w hw ho => by + change OvlA (absCapA (capA st s2 (cslotAddrA sd))) _ at ho + rw [hsd2] at ho; exact hfr w hw ho) + +end frame_ins + +/-! ## `subframe_dest`, `ensure_empty_run`, `insert_subframes`, and Label 55 `derive_subframe_jA` -/ + +section subframe_top +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +private def subframeCnodeTailSpecA (ac : Fl4SpecA64.Cap) (node_depth node_offset count : U64) : + Fl4SpecA64.SEMonad Unit (Fl4SpecA64.ObjRef × Nat) := + match ac with + | .cnodeCap ptr bits _ => + let size := 2 ^ bits + do + Fl4Spec.NonDetE.whenE (size - 1 < node_offset.val) <| + Fl4Spec.NonDetE.throwError (.rangeError 0 (BitVec.ofNat 64 (size - 1))) + Fl4Spec.NonDetE.whenE (size - node_offset.val < count.val) <| + Fl4Spec.NonDetE.throwError (.rangeError 1 (BitVec.ofNat 64 (size - node_offset.val))) + Fl4Spec.NonDetE.returnOk (ptr, bits) + | _ => Fl4Spec.NonDetE.throwError (.failedLookup false (.missingCapability node_depth.val)) + +private theorem whenE_falseD {σ ε : Type} {P : Prop} [Decidable P] {m : Fl4Spec.NonDetE σ ε Unit} + (h : ¬ P) : Fl4Spec.NonDetE.whenE P m = pure () := if_neg h + +private theorem run_pure_bindD {α β : Type} {x : α} + {f : α → Fl4Spec.NonDetE (Fl4SpecA64.State Unit) Fl4SpecA64.SyscallError β} + {σ : Fl4SpecA64.State Unit} : + ((pure x : Fl4Spec.NonDetE (Fl4SpecA64.State Unit) Fl4SpecA64.SyscallError α) >>= f).run σ = (f x).run σ := + run_bind_okD (m := pure x) rfl f + +private theorem run_throw_bindD {α β : Type} {e : Fl4SpecA64.SyscallError} + {f : α → Fl4Spec.NonDetE (Fl4SpecA64.State Unit) Fl4SpecA64.SyscallError β} + {σ : Fl4SpecA64.State Unit} : + ((Fl4Spec.NonDetE.throwError e : Fl4Spec.NonDetE (Fl4SpecA64.State Unit) Fl4SpecA64.SyscallError α) >>= f).run σ = + ({(.error e, σ)}, False) := + run_bind_errorD (m := Fl4Spec.NonDetE.throwError e) rfl f + +private theorem nonDetE_bind_assocA {σ ε α β γ : Type} + (m : Fl4Spec.NonDetE σ ε α) (f : α → Fl4Spec.NonDetE σ ε β) (g : β → Fl4Spec.NonDetE σ ε γ) : + ((m >>= f) >>= g) = (m >>= fun a => f a >>= g) := by + funext s + change ((m >>= f) >>= g).run s = (m >>= fun a => f a >>= g).run s + simp only [ExceptT.run, Bind.bind, ExceptT.bind, ExceptT.mk, Fl4Spec.NonDet.bind, Fl4Spec.NonDet.pure] + apply Prod.ext + · ext ⟨r, s'⟩; constructor + · rintro ⟨⟨b, s1⟩, ⟨⟨a, s0⟩, ha, hb⟩, hg⟩ + cases a with + | error e => + simp only [Prod.mk.injEq] at hb + rcases hb with ⟨rfl, rfl⟩ + exact ⟨(.error e, s1), ha, hg⟩ + | ok x => exact ⟨(.ok x, s0), ha, ⟨(b, s1), hb, hg⟩⟩ + · rintro ⟨⟨a, s0⟩, ha, hfg⟩ + cases a with + | error e => exact ⟨(.error e, s0), ⟨(.error e, s0), ha, Set.mem_singleton _⟩, hfg⟩ + | ok x => + rcases hfg with ⟨⟨b, s1⟩, hb, hg⟩ + exact ⟨(b, s1), ⟨(.ok x, s0), ha, hb⟩, hg⟩ + · apply propext; constructor + · rintro ((hm | ⟨⟨a, s0⟩, ha, hf⟩) | ⟨⟨b, s1⟩, ⟨⟨a, s0⟩, ha, hb⟩, hg⟩) + · exact Or.inl hm + · cases a with + | error e => exact False.elim hf + | ok x => exact Or.inr ⟨(.ok x, s0), ha, Or.inl hf⟩ + · cases a with + | error e => + simp only [Set.mem_singleton_iff, Prod.mk.injEq] at hb + rcases hb with ⟨rfl, rfl⟩ + exact False.elim hg + | ok x => exact Or.inr ⟨(.ok x, s0), ha, Or.inr ⟨(b, s1), hb, hg⟩⟩ + · rintro (hm | ⟨⟨a, s0⟩, ha, hfg⟩) + · exact Or.inl (Or.inl hm) + · cases a with + | error e => exact False.elim hfg + | ok x => + rcases hfg with hf | ⟨⟨b, s1⟩, hb, hg⟩ + · exact Or.inl (Or.inr ⟨(.ok x, s0), ha, hf⟩) + · exact Or.inr ⟨(b, s1), ⟨(.ok x, s0), ha, hb⟩, hg⟩ + +private theorem nonDetE_pure_bindA {α β : Type} (x : α) + (f : α → Fl4Spec.NonDetE (Fl4SpecA64.State Unit) Fl4SpecA64.SyscallError β) : + (pure x >>= f) = f x := by + funext s; exact run_pure_bindD (x := x) (f := f) (σ := s) + +private theorem nonDetE_throw_bindA {α β : Type} (e : Fl4SpecA64.SyscallError) + (f : α → Fl4Spec.NonDetE (Fl4SpecA64.State Unit) Fl4SpecA64.SyscallError β) : + (Fl4Spec.NonDetE.throwError e >>= f) = Fl4Spec.NonDetE.throwError e := by + funext s; exact run_throw_bindD (e := e) (f := f) (σ := s) + +private theorem subframe_tail_assocA (ac0 : Fl4SpecA64.Cap) (a0 a1 a2 a3 a4 a5 : U64) + (cte : Fl4SpecA64.CslotPtr) (fp rt : U64) (σ : Fl4SpecA64.State Unit) : + (do + let nc ← Fl4SpecA64.subframeNode (Z := Unit) ac0 a0.bv a1.bv + match nc with + | .cnodeCap ptr bits _ => + let size := 2 ^ bits + Fl4Spec.NonDetE.whenE (size - 1 < Fl4SpecA64.unat a2.bv) <| + Fl4Spec.NonDetE.throwError (.rangeError 0 (BitVec.ofNat 64 (size - 1))) + Fl4Spec.NonDetE.whenE (size - Fl4SpecA64.unat a2.bv < Fl4SpecA64.unat a4.bv) <| + Fl4Spec.NonDetE.throwError (.rangeError 1 (BitVec.ofNat 64 (size - Fl4SpecA64.unat a2.bv))) + let dests := (List.range' (Fl4SpecA64.unat a2.bv) (Fl4SpecA64.unat a4.bv)).map fun i => (ptr, Fl4SpecA64.natToCref bits i) + Fl4Spec.NonDetE.mapMEx (Fl4SpecA64.ensureEmpty (Z := Unit)) dests + Fl4Spec.NonDetE.returnOk (Fl4SpecA64.Fl4SubframeInvocation.mk cte (specPtr fp + a3.bv) + (Fl4SpecA64.maskVmRights (vmRightsOfA rt) (Fl4SpecA64.dataToRights a5.bv)) dests) + | _ => Fl4Spec.NonDetE.throwError (.failedLookup false (.missingCapability (Fl4SpecA64.unat a1.bv)))).run σ = + ((do + let nc ← Fl4SpecA64.subframeNode (Z := Unit) ac0 a0.bv a1.bv + subframeCnodeTailSpecA nc a1 a2 a4) >>= fun p => do + let dests := (List.range' a2.val a4.val).map fun i => (p.1, Fl4SpecA64.natToCref p.2 i) + Fl4Spec.NonDetE.mapMEx (Fl4SpecA64.ensureEmpty (Z := Unit)) dests + Fl4Spec.NonDetE.returnOk (Fl4SpecA64.Fl4SubframeInvocation.mk cte (specPtr fp + a3.bv) + (Fl4SpecA64.maskVmRights (vmRightsOfA rt) (Fl4SpecA64.dataToRights a5.bv)) dests)).run σ := by + refine congrFun (congrArg ExceptT.run ?_) σ + rw [nonDetE_bind_assocA] + congr 1; funext nc + cases nc with + | cnodeCap ptr bits g => + dsimp only [subframeCnodeTailSpecA] + rw [nonDetE_bind_assocA] + congr 1; funext _ + rw [nonDetE_bind_assocA] + congr 1; funext _ + dsimp only [Fl4Spec.NonDetE.returnOk] + rw [nonDetE_pure_bindA] + rfl + | _ => + dsimp only [subframeCnodeTailSpecA] + exact (nonDetE_throw_bindA _ _).symm + +private theorem decodeDeriveSubframe_stepA {a0 a1 a2 a3 a4 a5 : U64} {rest : List U64} + {cte : Fl4SpecA64.CslotPtr} {fp rt asid va : U64} {mp : Bool} + {exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)} {ac0 : Fl4SpecA64.Cap} + (hnotTrunc : ¬ (exs.length = 0 ∨ (List.map UScalar.bv (a0 :: a1 :: a2 :: a3 :: a4 :: a5 :: rest)).length < 6)) + (hnotOff : ¬ (2 ^ 21 ≤ Fl4SpecA64.unat a3.bv ∨ Fl4SpecA64.unat a3.bv % 4096 ≠ 0)) + (hnotCnt : ¬ (Fl4SpecA64.unat a4.bv < 1 ∨ (2 ^ 21 - Fl4SpecA64.unat a3.bv) / 4096 < Fl4SpecA64.unat a4.bv)) + (hex0_eq : (Fl4SpecA64.the exs[0]?).1 = ac0) (σ : Fl4SpecA64.State Unit) : + (Fl4SpecA64.decodeDeriveSubframe (Z := Unit) + (List.map UScalar.bv (a0 :: a1 :: a2 :: a3 :: a4 :: a5 :: rest)) cte + (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA .Large) false (mapDataA mp asid va)) exs).run σ = + ((do + let nc ← Fl4SpecA64.subframeNode (Z := Unit) ac0 a0.bv a1.bv + subframeCnodeTailSpecA nc a1 a2 a4) >>= fun p => do + let dests := (List.range' a2.val a4.val).map fun i => (p.1, Fl4SpecA64.natToCref p.2 i) + Fl4Spec.NonDetE.mapMEx (Fl4SpecA64.ensureEmpty (Z := Unit)) dests + Fl4Spec.NonDetE.returnOk (Fl4SpecA64.Fl4SubframeInvocation.mk cte (specPtr fp + a3.bv) + (Fl4SpecA64.maskVmRights (vmRightsOfA rt) (Fl4SpecA64.dataToRights a5.bv)) dests)).run σ := by + unfold Fl4SpecA64.decodeDeriveSubframe + rw [if_neg hnotTrunc] + dsimp only [frameSizeOfA] + have h0 : Fl4SpecA64.the (List.map UScalar.bv (a0 :: a1 :: a2 :: a3 :: a4 :: a5 :: rest))[0]? = a0.bv := rfl + have h1 : Fl4SpecA64.the (List.map UScalar.bv (a0 :: a1 :: a2 :: a3 :: a4 :: a5 :: rest))[1]? = a1.bv := rfl + have h2 : Fl4SpecA64.the (List.map UScalar.bv (a0 :: a1 :: a2 :: a3 :: a4 :: a5 :: rest))[2]? = a2.bv := rfl + have h3 : Fl4SpecA64.the (List.map UScalar.bv (a0 :: a1 :: a2 :: a3 :: a4 :: a5 :: rest))[3]? = a3.bv := rfl + have h4 : Fl4SpecA64.the (List.map UScalar.bv (a0 :: a1 :: a2 :: a3 :: a4 :: a5 :: rest))[4]? = a4.bv := rfl + have h5 : Fl4SpecA64.the (List.map UScalar.bv (a0 :: a1 :: a2 :: a3 :: a4 :: a5 :: rest))[5]? = a5.bv := rfl + rw [h0, h1, h2, h3, h4, h5, hex0_eq, whenE_falseD hnotOff, run_pure_bindD, + whenE_falseD hnotCnt, run_pure_bindD] + exact subframe_tail_assocA ac0 a0 a1 a2 a3 a4 a5 cte fp rt σ + +private theorem subframe_cnode_tailA {kb : U64} {node_cap : capability.Cap} {ac : Fl4SpecA64.Cap} + (hP : PlaceA Z.th kb) + (hrel : CapRelA node_cap ac) (hcv : CapValidA Z.th node_cap) + (node_depth node_offset count : U64) : + (do + let o ← vspace.cnode_of node_cap + match o with + | none => vspace.failed_lookup U64 (error.LookupFault.MissingCapability node_depth) + | some x => + let (dest, radix) := x + let i ← lift (radix &&& 63#u64) + let node_size ← 1#u64 <<< i + let i1 ← lift (core.num.U64.wrapping_sub node_size 1#u64) + if node_offset > i1 then + let i2 ← lift (core.num.U64.wrapping_sub node_size 1#u64) + error.range U64 0#u64 i2 + else + let i2 ← lift (core.num.U64.wrapping_sub node_size node_offset) + if count > i2 then + let i3 ← lift (core.num.U64.wrapping_sub node_size node_offset) + error.range U64 1#u64 i3 + else ok (core.result.Result.Ok dest)) ⦃ res => + match res with + | .Err e => + ∃ e', (subframeCnodeTailSpecA ac node_depth node_offset count).run σ = ({(.error e', σ)}, False) ∧ VsErrRelA e e' + | .Ok dest => + ∃ radix : U64, + ac = .cnodeCap (specPtr dest) radix.val (match node_cap with | .CNode _ _ gs g => bitsOfA g.val gs.val | _ => []) ∧ + Z.th dest = some (.cnode radix.val) ∧ + 1 ≤ radix.val ∧ radix.val ≤ 58 ∧ + node_offset.val < 2 ^ radix.val ∧ + count.val ≤ 2 ^ radix.val - node_offset.val ∧ + (subframeCnodeTailSpecA ac node_depth node_offset count).run σ = + ({(.ok (specPtr dest, radix.val), σ)}, False) ⦄ := by + obtain ⟨hwf, heq⟩ := hrel + cases node_cap + case CNode dest radix gs g => + subst heq + obtain ⟨hth, hr0⟩ := hcv + have hr1 : 1 ≤ radix.val := by omega + have hfit : dest.val + 2 ^ (radix.val + 5) ≤ 2 ^ 64 := hP.fit dest (.cnode radix.val) hth + have hnz : dest.val ≠ 0 := fun e => hP.nz dest (.cnode radix.val) hth (UScalar.eq_of_val_eq e) + have hr58 : radix.val ≤ 58 := by + by_contra h + have h59 : 64 ≤ radix.val + 5 := by omega + have := Nat.pow_le_pow_right (by omega : 1 ≤ 2) h59 + omega + have hr64 : radix.val < 64 := by omega + have hand : radix &&& 63#u64 = radix := by + apply UScalar.eq_of_val_eq + have := and_maskA radix 6 63#u64 rfl + rw [this]; exact Nat.mod_eq_of_lt hr64 + have hshl : (1#u64 <<< radix) ⦃ ns => ns.val = 2 ^ radix.val ⦄ := by + step as ⟨ns, hns⟩ + have hlt : 2 ^ radix.val < 2 ^ 64 := Nat.pow_lt_pow_right (by omega) hr64 + simpa [U64.size, Nat.mod_eq_of_lt hlt] using hns + obtain ⟨ns, hnsOk, hnsVal⟩ := spec_ex_fd hshl + have hpos : 1 ≤ 2 ^ radix.val := Nat.one_le_two_pow + have hlt64 : 2 ^ radix.val < 2 ^ 64 := Nat.pow_lt_pow_right (by omega) hr64 + have hsub1 : (core.num.U64.wrapping_sub ns 1#u64).val = 2 ^ radix.val - 1 := by + simp only [core.num.U64.wrapping_sub_val_eq, UScalar.size_UScalarTyU64, U64.size_def, U64.numBits, + UScalarTy.U64_numBits_eq, hnsVal] + have : (1#u64 : U64).val = 1 := rfl + rw [this]; omega + simp only [vspace.cnode_of, bind_ok, lift] + refine WP.spec_bind (Pₘ := fun node_size => node_size = ns) + (by rw [hand, hnsOk]; exact (WP.spec_ok _).2 rfl) fun node_size hns' => ?_ + rw [hns'] + by_cases h1 : node_offset > core.num.U64.wrapping_sub ns 1#u64 + · rw [if_pos h1] + simp only [error.range] + have h1' : 2 ^ radix.val - 1 < node_offset.val := by + have : (core.num.U64.wrapping_sub ns 1#u64).val < node_offset.val := h1 + omega + refine (WP.spec_ok _).2 ⟨.rangeError 0 (BitVec.ofNat 64 (2 ^ radix.val - 1)), ?_, Or.inl ⟨rfl, ?_⟩⟩ + · simp only [subframeCnodeTailSpecA, absCapA, Fl4Spec.NonDetE.whenE, if_pos h1'] + exact run_throw_bindD + · apply BitVec.eq_of_toNat_eq + rw [BitVec.toNat_ofNat] + change (2 ^ radix.val - 1) % 2 ^ 64 = (core.num.U64.wrapping_sub ns 1#u64).val + rw [hsub1, Nat.mod_eq_of_lt (by omega)] + · rw [if_neg h1] + have h1' : ¬ (2 ^ radix.val - 1 < node_offset.val) := by + intro hc; exact h1 (show (core.num.U64.wrapping_sub ns 1#u64).val < node_offset.val by omega) + have hsub2 : (core.num.U64.wrapping_sub ns node_offset).val = 2 ^ radix.val - node_offset.val := by + simp only [core.num.U64.wrapping_sub_val_eq, UScalar.size_UScalarTyU64, U64.size_def, U64.numBits, + UScalarTy.U64_numBits_eq, hnsVal] + omega + by_cases h2 : count > core.num.U64.wrapping_sub ns node_offset + · rw [if_pos h2] + simp only [error.range] + have h2' : 2 ^ radix.val - node_offset.val < count.val := by + have : (core.num.U64.wrapping_sub ns node_offset).val < count.val := h2 + omega + refine (WP.spec_ok _).2 ⟨.rangeError 1 (BitVec.ofNat 64 (2 ^ radix.val - node_offset.val)), ?_, Or.inl ⟨rfl, ?_⟩⟩ + · simp only [subframeCnodeTailSpecA, absCapA, Fl4Spec.NonDetE.whenE, if_neg h1', if_pos h2'] + rw [run_pure_bindD]; exact run_throw_bindD + · apply BitVec.eq_of_toNat_eq + rw [BitVec.toNat_ofNat] + change (2 ^ radix.val - node_offset.val) % 2 ^ 64 = (core.num.U64.wrapping_sub ns node_offset).val + rw [hsub2, Nat.mod_eq_of_lt (by omega)] + · rw [if_neg h2] + have h2' : ¬ (2 ^ radix.val - node_offset.val < count.val) := by + intro hc; exact h2 (show (core.num.U64.wrapping_sub ns node_offset).val < count.val by omega) + refine (WP.spec_ok _).2 ⟨radix, rfl, hth, hr1, hr58, by omega, by omega, ?_⟩ + simp only [subframeCnodeTailSpecA, absCapA, Fl4Spec.NonDetE.whenE, if_neg h1', if_neg h2'] + rw [run_pure_bindD, run_pure_bindD]; rfl + case PageTable p m asid va => + simp only [vspace.cnode_of, bind_ok, vspace.failed_lookup, WP.spec_ok] + have hnc : ∀ ptr bits g, ac ≠ .cnodeCap ptr bits g := by + intro ptr bits g h; rw [h] at heq; cases m + · simp [CapEqA, absCapA] at heq + · cases heq + refine ⟨.failedLookup false (.missingCapability node_depth.val), ?_, Or.inl ⟨rfl, rfl⟩⟩ + cases ac <;> first | (exfalso; exact hnc _ _ _ rfl) | rfl + all_goals + subst heq + simp only [vspace.cnode_of, bind_ok, vspace.failed_lookup, WP.spec_ok] + exact ⟨.failedLookup false (.missingCapability node_depth.val), rfl, Or.inl ⟨rfl, rfl⟩⟩ + +theorem subframe_dest_refinesA {kb : U64} + (hR : StateRelA Z.th (L.mem s) σ) (hV : SlotsValidA Z.th (L.mem s)) (hP : PlaceA Z.th kb) + {x0 : U64} {ac0 : Fl4SpecA64.Cap} (hx0s : IsSlotA Z.th x0) (hca0 : CapRelA (capAtA (L.mem s) x0) ac0) + (node_index node_depth node_offset count : U64) : + vspace.subframe_dest st s x0 node_index node_depth node_offset count ⦃ res => + match res with + | .Err e => + ∃ e', (do + let nc ← Fl4SpecA64.subframeNode (Z := Unit) ac0 node_index.bv node_depth.bv + subframeCnodeTailSpecA nc node_depth node_offset count).run σ = + ({(.error e', σ)}, False) ∧ VsErrRelA e e' + | .Ok dest => + ∃ radix : U64, + Z.th dest = some (.cnode radix.val) ∧ + 1 ≤ radix.val ∧ radix.val ≤ 58 ∧ + node_offset.val < 2 ^ radix.val ∧ + count.val ≤ 2 ^ radix.val - node_offset.val ∧ + (do + let nc ← Fl4SpecA64.subframeNode (Z := Unit) ac0 node_index.bv node_depth.bv + subframeCnodeTailSpecA nc node_depth node_offset count).run σ = + ({(.ok (specPtr dest, radix.val), σ)}, False) ⦄ := by + unfold vspace.subframe_dest + rw [capA_eq, bind_ok] + by_cases hd0 : node_depth = 0#u64 + · rw [show (node_depth != 0#u64) = false by rw [hd0]; rfl, if_neg Bool.false_ne_true] + have hsn : (Fl4SpecA64.subframeNode (Z := Unit) ac0 node_index.bv node_depth.bv).run σ = + ({(.ok ac0, σ)}, False) := by + unfold Fl4SpecA64.subframeNode + rw [if_pos (by subst hd0; rfl)]; rfl + refine WP.spec_mono (subframe_cnode_tailA (σ := σ) hP hca0 (hV x0 hx0s) node_depth node_offset count) fun res hr => ?_ + cases res with + | Err e => + obtain ⟨e', he', hv⟩ := hr + exact ⟨e', by rw [run_bind_okD hsn]; exact he', hv⟩ + | Ok dest => + obtain ⟨radix, -, hth, hr1, hr58, ho, hc, hok⟩ := hr + exact ⟨radix, hth, hr1, hr58, ho, hc, by rw [run_bind_okD hsn]; exact hok⟩ + · have hne : (node_depth != 0#u64) = true := bne_iff_ne.2 hd0 + have hne' : node_depth.bv ≠ 0 := fun e => hd0 (UScalar.eq_of_val_eq (by simpa using congrArg BitVec.toNat e)) + rw [if_pos hne] + have hlt := lookup_slot_for_cnode_op_refinesA Z.th s σ hR hV false (capAtA (L.mem s) x0) ac0 hca0 + (hV x0 hx0s) node_index node_depth + obtain ⟨r1, hr1, e1, he1, hrel1⟩ := spec_ex_fd hlt + rw [show cspace.lookup_target_slot st s (capA st s x0) node_index node_depth = + cspace.lookup_slot_for_cnode_op st s false (capAtA (L.mem s) x0) node_index node_depth from rfl, hr1, bind_ok] + have hls : (Fl4SpecA64.lookupTargetSlot (Z := Unit) ac0 (Fl4SpecA64.toBl node_index.bv) node_depth.val).run σ = + ({(e1, σ)}, False) := he1 + cases r1 with + | Err e => + cases e1 with + | ok _ => exact hrel1.elim + | error e' => + simp only [core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame, + core.convert.FromSame.from, bind_ok, bind_tc_ok, WP.spec_ok] + refine ⟨e', ?_, Or.inl hrel1⟩ + have hsn : (Fl4SpecA64.subframeNode (Z := Unit) ac0 node_index.bv node_depth.bv).run σ = + ({(.error e', σ)}, False) := by + unfold Fl4SpecA64.subframeNode + rw [if_neg hne', show Fl4SpecA64.unat node_depth.bv = node_depth.val from rfl] + exact run_bind_errorD hls _ + exact run_bind_errorD hsn _ + | Ok sl_addr => + cases e1 with + | error _ => exact hrel1.elim + | ok sl => + obtain ⟨rfl, ps, hps1, hps, -⟩ := hrel1 + obtain ⟨ac1, hca1, hgc1⟩ := hR.slot_capA hps1 hps + have hsn : (Fl4SpecA64.subframeNode (Z := Unit) ac0 node_index.bv node_depth.bv).run σ = + ({(.ok ac1, σ)}, False) := by + unfold Fl4SpecA64.subframeNode + rw [if_neg hne', show Fl4SpecA64.unat node_depth.bv = node_depth.val from rfl, run_bind_okD hls] + exact liftE_runA hgc1 + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok, capA_eq] + have hAs : AbsSlotA Z.th sl := ⟨ps, hps1, Or.inl hps⟩ + have hIs : IsSlotA Z.th (cslotAddrA sl) := hAs.isSlot hP + refine WP.spec_mono (subframe_cnode_tailA (σ := σ) hP hca1 (hV _ hIs) node_depth node_offset count) fun res hr => ?_ + cases res with + | Err e => + obtain ⟨e', he', hv⟩ := hr + exact ⟨e', by rw [run_bind_okD hsn]; exact he', hv⟩ + | Ok dest => + obtain ⟨radix, -, hth, hr1', hr58', ho, hc, hok⟩ := hr + exact ⟨radix, hth, hr1', hr58', ho, hc, by rw [run_bind_okD hsn]; exact hok⟩ + +private theorem mapMEx_ensureEmpty_appendA {σ : Fl4SpecA64.State Unit} + (xs ys : List Fl4SpecA64.CslotPtr) + (hx : ∀ x ∈ xs, (Fl4SpecA64.ensureEmpty (Z := Unit) x).run σ = ({(.ok (), σ)}, False)) : + (Fl4Spec.NonDetE.mapMEx (Fl4SpecA64.ensureEmpty (Z := Unit)) (xs ++ ys)).run σ = + (Fl4Spec.NonDetE.mapMEx (Fl4SpecA64.ensureEmpty (Z := Unit)) ys).run σ := by + induction xs with + | nil => rfl + | cons a l ih => + simp only [List.cons_append, Fl4Spec.NonDetE.mapMEx] + rw [run_bind_okD (hx a (List.mem_cons_self ..))] + exact ih fun x h => hx x (List.mem_cons_of_mem _ h) + +private theorem mapMx_appendA {α : Type} (f : α → Fl4SpecA64.SMonad Unit Unit) + {σ0 σ1 σ2 : Fl4SpecA64.State Unit} (xs ys : List α) + (h1 : ((), σ1) ∈ (Fl4Spec.NonDet.mapMx f xs σ0).1) + (h2 : ((), σ2) ∈ (Fl4Spec.NonDet.mapMx f ys σ1).1) : + ((), σ2) ∈ (Fl4Spec.NonDet.mapMx f (xs ++ ys) σ0).1 := by + induction xs generalizing σ0 with + | nil => + simp only [Fl4Spec.NonDet.mapMx, Fl4Spec.NonDet.pure, Set.mem_singleton_iff, Prod.mk.injEq, true_and] at h1 + subst h1; exact h2 + | cons a l ih => + simp only [List.cons_append, Fl4Spec.NonDet.mapMx, Fl4Spec.NonDet.bind] at h1 ⊢ + obtain ⟨⟨_, σm⟩, hm, hl⟩ := h1 + exact ⟨((), σm), hm, ih hl⟩ + +theorem ensure_empty_run_refinesA + (hR : StateRelA Z.th (L.mem s) σ) (hP : PlaceA Z.th (L.ksb s)) + {dest : U64} {r : Nat} (hth : Z.th dest = some (.cnode r)) (hr : 1 ≤ r ∧ r ≤ 58) + (first count : U64) (hfc : first.val + count.val ≤ 2 ^ r) : + vspace.ensure_empty_run st s dest first count ⦃ res => + let ds := (List.range' first.val count.val).map fun j => (specPtr dest, Fl4SpecA64.natToCref r j) + match res with + | .Err e => + ∃ e', (Fl4Spec.NonDetE.mapMEx (Fl4SpecA64.ensureEmpty (Z := Unit)) ds).run σ = + ({(.error e', σ)}, False) ∧ VsErrRelA e e' + | .Ok () => + (Fl4Spec.NonDetE.mapMEx (Fl4SpecA64.ensureEmpty (Z := Unit)) ds).run σ = ({(.ok (), σ)}, False) ∧ + ∀ j < count.val, capAtA (L.mem s) (cslotAddrA (specPtr dest, Fl4SpecA64.natToCref r (first.val + j))) = .Null ⦄ := by + unfold vspace.ensure_empty_run vspace.ensure_empty_run_loop + have h2r58 : 2 ^ r ≤ 2 ^ 58 := Nat.pow_le_pow_right (by omega) hr.2 + apply loop.spec_decr_nat (measure := fun i => count.val - i.val) + (inv := fun i => i.val ≤ count.val ∧ + (∀ j < i.val, + (Fl4SpecA64.ensureEmpty (Z := Unit) (specPtr dest, Fl4SpecA64.natToCref r (first.val + j))).run σ = + ({(.ok (), σ)}, False)) ∧ + (∀ j < i.val, capAtA (L.mem s) (cslotAddrA (specPtr dest, Fl4SpecA64.natToCref r (first.val + j))) = .Null)) + · intro i ⟨hi, hok, hall⟩ + dsimp only + unfold vspace.ensure_empty_run_loop.body + by_cases hlt : i < count + · rw [if_pos hlt] + have hiLt : i.val < count.val := hlt + have hsum : (wa first i).val = first.val + i.val := by + rw [wa_valP]; exact Nat.mod_eq_of_lt (by omega) + have hsl : obj.Slot.index dest (wa first i) = + ok (cslotAddrA (specPtr dest, Fl4SpecA64.natToCref r (first.val + i.val))) := by + rw [slotIndex_eqA dest (wa first i) (by omega), hsum, cslotAddr_natToCrefA dest r hr _ (by omega)] + simp only [lift, bind_ok, hsl] + have hcrLen : (Fl4SpecA64.natToCref r (first.val + i.val)).length = r := natToCref_lengthA r _ (by omega) + obtain ⟨ad, hcad, hgd⟩ := hR.slot_capA (sl := (specPtr dest, Fl4SpecA64.natToCref r (first.val + i.val))) + (p := dest) rfl (by simpa [hcrLen] using hth) + have hee := ensureEmpty_runA (specPtr dest, Fl4SpecA64.natToCref r (first.val + i.val)) σ hgd + unfold cdt.ensure_empty_slot + rw [capA_eq, bind_ok] + by_cases hdn : capAtA (L.mem s) (cslotAddrA (specPtr dest, Fl4SpecA64.natToCref r (first.val + i.val))) = .Null + · have hadn : ad = .nullCap := by have h := hcad.2; rw [hdn] at h; exact h.symm + simp only [hadn, ↓reduceIte] at hee + have hdn' : capA st s (cslotAddrA (specPtr dest, Fl4SpecA64.natToCref r (first.val + i.val))) = .Null := hdn + rw [hdn', cap_is_null_nullA] + simp only [bind_ok, ↓reduceIte, core.result.Result.Insts.CoreOpsTry.branch] + have hinc : (i + 1#u64) ⦃ i1 => i1.val = i.val + 1 ⦄ := by step as ⟨i1, hi1⟩; omega + obtain ⟨i1, hi1Ok, hi1Val⟩ := spec_ex_fd hinc + rw [hi1Ok, bind_ok, WP.spec_ok] + refine ⟨⟨by omega, fun j hj => ?_, fun j hj => ?_⟩, by omega⟩ + · by_cases hj' : j < i.val + · exact hok j hj' + · have : j = i.val := by omega + subst this; exact hee + · by_cases hj' : j < i.val + · exact hall j hj' + · have : j = i.val := by omega + subst this; exact hdn + · have hdn' : capA st s (cslotAddrA (specPtr dest, Fl4SpecA64.natToCref r (first.val + i.val))) ≠ .Null := hdn + rw [cap_is_null_neA hdn'] + simp only [bind_ok, Bool.false_eq_true, ↓reduceIte, core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame, + core.convert.FromSame.from, bind_tc_ok] + have hne : ad ≠ .nullCap := hcad.ne_null hdn + simp only [hne, ↓reduceIte] at hee + refine (WP.spec_ok _).2 ⟨.deleteFirst, ?_, Or.inl trivial⟩ + have hsp : List.range' first.val count.val = + List.range' first.val i.val ++ List.range' (first.val + i.val) (count.val - i.val) := by + simpa [show first.val + 1 * i.val = first.val + i.val by omega, + show i.val + (count.val - i.val) = count.val by omega] using + (List.range'_append (s := first.val) (m := i.val) (n := count.val - i.val) (step := 1)).symm + rw [hsp, List.map_append, mapMEx_ensureEmpty_appendA] + · rw [show count.val - i.val = (count.val - i.val - 1) + 1 by omega, List.range'_succ, List.map_cons] + unfold Fl4Spec.NonDetE.mapMEx + exact run_bind_errorD hee _ + · intro x hx + obtain ⟨j, hj, rfl⟩ := List.mem_map.1 hx + rw [List.mem_range'] at hj + obtain ⟨k, hk, rfl⟩ := hj + simpa using hok k hk + · rw [if_neg hlt, WP.spec_ok] + have hieq : i.val = count.val := by + have : ¬ (i.val < count.val) := hlt; omega + rw [← hieq] + refine ⟨?_, hall⟩ + have := mapMEx_ensureEmpty_appendA (σ := σ) + ((List.range' first.val i.val).map fun j => (specPtr dest, Fl4SpecA64.natToCref r j)) [] (fun x hx => by + obtain ⟨j, hj, rfl⟩ := List.mem_map.1 hx + rw [List.mem_range'] at hj; obtain ⟨k, hk, rfl⟩ := hj; simpa using hok k hk) + rw [List.append_nil] at this + exact this + · exact ⟨by change 0 ≤ count.val; omega, fun _ h => by change _ < 0 at h; omega, fun _ h => by change _ < 0 at h; omega⟩ + +theorem insert_subframes_refinesA + (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + {ss : Fl4SpecA64.CslotPtr} (hAs : AbsSlotA Z.th ss) + {dest : U64} {r : Nat} (hth : Z.th dest = some (.cnode r)) (hr : 1 ≤ r ∧ r ≤ 58) + {fp rt asid va offset first count vm : U64} {mp e : Bool} + (hfpC : CapRt.Canonical fp) (hfpAl : fp.val % 2 ^ 21 = 0) + (hvm : 1 ≤ vm.val ∧ vm.val ≤ 3) + (hoff : offset.val + count.val * 4096 ≤ 2 ^ 21) + (hfc : first.val + count.val ≤ 2 ^ r) + (hcO : ∀ y ∈ OS, obj.sext48 y = ok y) + {s0 : S} {σ0 : Fl4SpecA64.State Unit} + (hJ0 : JAtZAX (L := L) AR Z s0 a σ0 m) + (hT0 : TreeA st OS Z.G.chains Z.G.par s0) + (hks0 : L.ksb s0 = L.ksb s) (hcpu0 : L.cpu s0 = L.cpu s) + (hnul0 : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s0 y = .Null) + (hsrc0 : capAtA (L.mem s0) (cslotAddrA ss) = .Frame fp .Large rt false mp e asid va) + (hall0 : ∀ j < count.val, capAtA (L.mem s0) (cslotAddrA (specPtr dest, Fl4SpecA64.natToCref r (first.val + j))) = .Null) + (hfr0 : ∀ w ∈ Z.G.chains.flatten, ∀ (q vm' : U64), fp.val ≤ q.val → q.val < fp.val + 2 ^ 21 → + OvlA (absCapA (.Frame q .Small vm' false false false 0#u64 0#u64)) (absCapA (capAtA (L.mem s0) w)) → + isFrameA (capAtA (L.mem s0) w) = true ∧ ∃ ch ∈ Z.G.chains, cslotAddrA ss ∈ ch ∧ w ∈ ch) : + vspace.insert_subframes st s0 (cslotAddrA ss) dest first (wa fp offset) count vm ⦃ s2 => + let ds := (List.range' first.val count.val).map fun j => (specPtr dest, Fl4SpecA64.natToCref r j) + let ps := (List.range' 0 count.val).zip ds + ∃ σ2 G2, + ((), σ2) ∈ (Fl4Spec.NonDet.mapMx (fun d : Nat × Fl4SpecA64.CslotPtr => + Fl4SpecA64.capInsert (Z := Unit) (.archObjectCap (.frameCap + (specPtr fp + offset.bv + BitVec.ofNat 64 (d.1 * 4096)) (vmRightsOfA vm) .armSmallPage false none)) + ss d.2) ps σ0).1 ∧ + JAtZAX (L := L) AR { Z with G := G2 } s2 a σ2 m ∧ + TreeA st OS G2.chains G2.par s2 ∧ + (CarryA Z (L.mem s0) σ0 → CarryA { Z with G := G2 } (L.mem s2) σ2) ⦄ := by + unfold vspace.insert_subframes vspace.insert_subframes_loop + have h2r58 : 2 ^ r ≤ 2 ^ 58 := Nat.pow_le_pow_right (by omega) hr.2 + apply loop.spec_decr_nat (measure := fun p : S × U64 => count.val - p.2.val) + (inv := fun p : S × U64 => + let (s1, i) := p + i.val ≤ count.val ∧ + ∃ σ1 G1, + ((), σ1) ∈ (Fl4Spec.NonDet.mapMx (fun d : Nat × Fl4SpecA64.CslotPtr => + Fl4SpecA64.capInsert (Z := Unit) (.archObjectCap (.frameCap + (specPtr fp + offset.bv + BitVec.ofNat 64 (d.1 * 4096)) (vmRightsOfA vm) .armSmallPage false none)) + ss d.2) ((List.range' 0 i.val).zip ((List.range' first.val i.val).map fun j => + (specPtr dest, Fl4SpecA64.natToCref r j))) σ0).1 ∧ + JAtZAX (L := L) AR { Z with G := G1 } s1 a σ1 m ∧ + TreeA st OS G1.chains G1.par s1 ∧ + L.ksb s1 = L.ksb s ∧ L.cpu s1 = L.cpu s ∧ + (∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s1 y = .Null) ∧ + capAtA (L.mem s1) (cslotAddrA ss) = .Frame fp .Large rt false mp e asid va ∧ + (∀ j, i.val ≤ j → j < count.val → + capAtA (L.mem s1) (cslotAddrA (specPtr dest, Fl4SpecA64.natToCref r (first.val + j))) = .Null) ∧ + (∀ w ∈ G1.chains.flatten, ∀ (q vm' : U64), fp.val ≤ q.val → q.val < fp.val + 2 ^ 21 → + OvlA (absCapA (.Frame q .Small vm' false false false 0#u64 0#u64)) (absCapA (capAtA (L.mem s1) w)) → + isFrameA (capAtA (L.mem s1) w) = true ∧ ∃ ch ∈ G1.chains, cslotAddrA ss ∈ ch ∧ w ∈ ch) ∧ + (CarryA Z (L.mem s0) σ0 → CarryA { Z with G := G1 } (L.mem s1) σ1)) + · rintro ⟨s1, i⟩ ⟨hi, σ1, G1, hm1, hJ1, hT1, hks1, hcpu1, hnul1, hsrc1, hall1, hfr1, hC1⟩ + dsimp only + unfold vspace.insert_subframes_loop.body + by_cases hlt : i < count + · rw [if_pos hlt] + have hiLt : i.val < count.val := hlt + have hshl : (i <<< 12#i32) ⦃ i1 => i1.val = i.val * 4096 ⦄ := by + step as ⟨i1, hi1⟩ + have hlt64 : i.val * 4096 < 2 ^ 64 := by omega + simp only [U64.size, U64.numBits, UScalarTy.U64_numBits_eq, Nat.shiftLeft_eq] at hi1 + rw [Nat.mod_eq_of_lt hlt64] at hi1 + exact hi1 + obtain ⟨i1, hi1Ok, hi1Val⟩ := spec_ex_fd hshl + have hsum : (wa first i).val = first.val + i.val := by + rw [wa_valP]; exact Nat.mod_eq_of_lt (by omega) + have hki : (wa offset i1).val = offset.val + i.val * 4096 := by + rw [wa_valP, hi1Val]; exact Nat.mod_eq_of_lt (by omega) + have hwa_assoc : wa (wa fp offset) i1 = wa fp (wa offset i1) := by + apply UScalar.eq_of_val_eq + simp only [wa_valP, hi1Val] + have : offset.val + i.val * 4096 < 2 ^ 64 := by omega + rw [Nat.mod_eq_of_lt this]; omega + have hsl : obj.Slot.index dest (wa first i) = + ok (cslotAddrA (specPtr dest, Fl4SpecA64.natToCref r (first.val + i.val))) := by + rw [slotIndex_eqA dest (wa first i) (by omega), hsum, cslotAddr_natToCrefA dest r hr _ (by omega)] + rw [hi1Ok, bind_ok] + simp only [lift, bind_ok, hsl, hwa_assoc] + set sd : Fl4SpecA64.CslotPtr := (specPtr dest, Fl4SpecA64.natToCref r (first.val + i.val)) + have hAd : AbsSlotA Z.th sd := ⟨dest, rfl, Or.inl (by rw [natToCref_lengthA r _ (by omega)]; exact hth)⟩ + have hdn : capAtA (L.mem s1) (cslotAddrA sd) = .Null := hall1 i.val le_rfl hiLt + have hdc : obj.sext48 (cslotAddrA sd) = ok (cslotAddrA sd) := hcO _ (hsub _ (hAd.isSlot hJ1.1.place)) + have hqval : (wa fp (wa offset i1)).val = fp.val + (wa offset i1).val := by + rw [wa_valP] + have : fp.val + 2 ^ 21 ≤ 2 ^ 64 := by + have : fp.val < 2 ^ 64 := fp.hBounds; omega + exact Nat.mod_eq_of_lt (by omega) + have hstep := cap_insert_frame_stepA E hJ1 hT1 hks1 hcpu1 hsub hnul1 hAs hAd hsrc1 hdn hdc hfpC hfpAl hvm + (by omega) (fun w hw ho => hfr1 w hw (wa fp (wa offset i1)) vm (by omega) (by omega) ho) + refine WP.spec_bind hstep fun s2 ⟨σ2, G2, hG2, hm2, hJ2, hT2, hks2, hcpu2, hnul2, hkeep2, hsd2, hC2⟩ => ?_ + have hinc : (i + 1#u64) ⦃ i4 => i4.val = i.val + 1 ⦄ := by step as ⟨i4, hi4⟩; omega + obtain ⟨i4, hi4Ok, hi4Val⟩ := spec_ex_fd hinc + rw [hi4Ok, bind_ok, WP.spec_ok] + have hss_ne_sd : cslotAddrA ss ≠ cslotAddrA sd := fun e => by rw [e, hdn] at hsrc1; cases hsrc1 + have hsrc2 : capAtA (L.mem s2) (cslotAddrA ss) = .Frame fp .Large rt false mp e asid va := by + rw [hkeep2 _ (hsub _ (hAs.isSlot hJ1.1.place)) hss_ne_sd, hsrc1] + have hall2 : ∀ j, i4.val ≤ j → j < count.val → + capAtA (L.mem s2) (cslotAddrA (specPtr dest, Fl4SpecA64.natToCref r (first.val + j))) = .Null := by + intro j hj1 hj2 + set sd' : Fl4SpecA64.CslotPtr := (specPtr dest, Fl4SpecA64.natToCref r (first.val + j)) + have hAd' : AbsSlotA Z.th sd' := ⟨dest, rfl, Or.inl (by rw [natToCref_lengthA r _ (by omega)]; exact hth)⟩ + have hne : cslotAddrA sd' ≠ cslotAddrA sd := fun e => by + have h3 := cslotAddr_natToCrefA dest r hr (first.val + j) (by omega) + have h4 := cslotAddr_natToCrefA dest r hr (first.val + i.val) (by omega) + rw [h3, h4] at e + have hbv : dest.bv + BitVec.ofNat 64 ((first.val + j) * 32) = + dest.bv + BitVec.ofNat 64 ((first.val + i.val) * 32) := congrArg UScalar.bv e + have h5 := congrArg BitVec.toNat (add_left_cancel hbv) + rw [BitVec.toNat_ofNat, BitVec.toNat_ofNat, + Nat.mod_eq_of_lt (by omega : (first.val + j) * 32 < 2 ^ 64), + Nat.mod_eq_of_lt (by omega : (first.val + i.val) * 32 < 2 ^ 64)] at h5 + omega + rw [hkeep2 _ (hsub _ (hAd'.isSlot hJ1.1.place)) hne] + exact hall1 j (by omega) hj2 + have hfr2 : ∀ w ∈ G2.chains.flatten, ∀ (q vm' : U64), fp.val ≤ q.val → q.val < fp.val + 2 ^ 21 → + OvlA (absCapA (.Frame q .Small vm' false false false 0#u64 0#u64)) (absCapA (capAtA (L.mem s2) w)) → + isFrameA (capAtA (L.mem s2) w) = true ∧ ∃ ch ∈ G2.chains, cslotAddrA ss ∈ ch ∧ w ∈ ch := by + intro w hw q vm' hq1 hq2 ho + rw [hG2] at hw ⊢ + have hsG : cslotAddrA ss ∈ G1.chains.flatten := + (hT1.lk.members _).2 ⟨hsub _ (hAs.isSlot hJ1.1.place), by + show capAtA (L.mem s1) (cslotAddrA ss) ≠ .Null; rw [hsrc1]; intro h; cases h⟩ + have hsd_mem : w = cslotAddrA sd ∨ w ∈ G1.chains.flatten := by + obtain ⟨ch', hch', hw'⟩ := List.mem_flatten.1 hw + rw [insG, List.mem_map] at hch' + obtain ⟨ch, hch, rfl⟩ := hch' + by_cases hss : cslotAddrA ss ∈ ch + · rcases List.mem_cons.1 ((insA_perm (dest := cslotAddrA sd) hss).mem_iff.1 hw') with rfl | h + · exact Or.inl rfl + · exact Or.inr (List.mem_flatten.2 ⟨ch, hch, h⟩) + · rw [insA_of_not_mem hss] at hw' + exact Or.inr (List.mem_flatten.2 ⟨ch, hch, hw'⟩) + rcases hsd_mem with rfl | hw1 + · obtain ⟨ch, hch, hsc⟩ := List.mem_flatten.1 hsG + refine ⟨by rw [hsd2]; rfl, insA (cslotAddrA ss) (cslotAddrA sd) ch, List.mem_map.2 ⟨ch, hch, rfl⟩, + (insA_perm (dest := cslotAddrA sd) hsc).mem_iff.2 (List.mem_cons_of_mem _ hsc), + (insA_perm (dest := cslotAddrA sd) hsc).mem_iff.2 (List.mem_cons_self ..)⟩ + · have hwne : w ≠ cslotAddrA sd := fun e => ((hT1.lk.members _).1 (e ▸ hw1)).2 hdn + rw [hkeep2 w ((hT1.lk.members w).1 hw1).1 hwne] at ho ⊢ + obtain ⟨hf, ch, hch, hsc, hwc⟩ := hfr1 w hw1 q vm' hq1 hq2 ho + exact ⟨hf, insA (cslotAddrA ss) (cslotAddrA sd) ch, List.mem_map.2 ⟨ch, hch, rfl⟩, + (insA_perm (dest := cslotAddrA sd) hsc).mem_iff.2 (List.mem_cons_of_mem _ hsc), + (insA_perm (dest := cslotAddrA sd) hsc).mem_iff.2 (List.mem_cons_of_mem _ hwc)⟩ + have hanc : absCapA (.Frame (wa fp (wa offset i1)) .Small vm false false false 0#u64 0#u64) = + .archObjectCap (.frameCap (specPtr fp + offset.bv + BitVec.ofNat 64 (i.val * 4096)) + (vmRightsOfA vm) .armSmallPage false none) := by + simp only [absCapA, absFrameE, Bool.false_eq_true, ↓reduceIte, frameSizeOfA, mapDataA, specPtr_wa] + have hptr : specPtr fp + (wa offset i1).bv = specPtr fp + offset.bv + BitVec.ofNat 64 (i.val * 4096) := by + rw [add_assoc]; congr 1 + apply BitVec.eq_of_toNat_eq + rw [BitVec.toNat_add, BitVec.toNat_ofNat] + have h1 : (wa offset i1).bv.toNat = offset.val + i.val * 4096 := hki + have h2 : offset.bv.toNat = offset.val := rfl + rw [h1, h2, Nat.mod_eq_of_lt (by omega : i.val * 4096 < 2 ^ 64), + Nat.mod_eq_of_lt (by omega : offset.val + i.val * 4096 < 2 ^ 64)] + rw [hptr] + rw [hanc] at hm2 + have hzip : ((List.range' 0 i4.val).zip ((List.range' first.val i4.val).map fun j => + (specPtr dest, Fl4SpecA64.natToCref r j))) = + ((List.range' 0 i.val).zip ((List.range' first.val i.val).map fun j => + (specPtr dest, Fl4SpecA64.natToCref r j))) ++ [(i.val, sd)] := by + have hr1 : List.range' 0 i4.val = List.range' 0 i.val ++ [i.val] := by + rw [hi4Val, List.range'_concat]; simp + have hr2 : List.range' first.val i4.val = List.range' first.val i.val ++ [first.val + i.val] := by + rw [hi4Val, List.range'_concat]; simp + rw [hr1, hr2, List.map_append, List.map_singleton, List.zip_append (by simp)] + rfl + refine ⟨⟨by dsimp only; omega, σ2, G2, ?_, hJ2, hT2, hks2, hcpu2, hnul2, hsrc2, hall2, hfr2, fun hC => hC2 (hC1 hC)⟩, by dsimp only; omega⟩ + rw [hzip] + exact mapMx_appendA _ _ _ hm1 (by + unfold Fl4Spec.NonDet.mapMx Fl4Spec.NonDet.bind + exact ⟨((), σ2), hm2, Set.mem_singleton _⟩) + · rw [if_neg hlt, WP.spec_ok] + have hieq : i.val = count.val := by have : ¬ (i.val < count.val) := hlt; omega + rw [hieq] at hm1 + exact ⟨σ1, G1, hm1, hJ1, hT1, hC1⟩ + · exact ⟨by change 0 ≤ count.val; omega, σ0, Z.G, Set.mem_singleton _, hJ0, hT0, hks0, hcpu0, hnul0, hsrc0, + fun j _ hj => hall0 j hj, hfr0, id⟩ + +private theorem dec_deriveSubA (calling : Bool) (args : List Fl4SpecA64.Data) (xSlot : Fl4SpecA64.CapRef) + (cte : Fl4SpecA64.CslotPtr) (e : Bool) (p : Fl4SpecA64.ObjRef) (r : Fl4SpecA64.CapRights) + (sz : Fl4SpecA64.VMPageSize) (dev : Bool) (md : Option (Fl4SpecA64.Asid × Fl4SpecA64.VSpaceRef)) + (exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) : + (Fl4SpecA64.fl4ArchDecodeInvocation (Z := Unit) calling (.ext .armPageDeriveSubframe) args xSlot cte + (absFrameE e p r sz dev md) exs).run σ = + (Fl4Spec.NonDetE.liftME Fl4SpecA64.Fl4ArchInvocation.deriveSubframe + (Fl4SpecA64.decodeDeriveSubframe (Z := Unit) args cte (.frameCap p r sz dev md) exs)).run σ := by + cases e <;> simp [absFrameE, Fl4SpecA64.fl4ArchDecodeInvocation] + +/-- **Label 55 (`ARMPageDeriveSubframe`) on `ArchTopPostA`** (module docstring): both plain and Stage-2 frames, +covering all error paths and the CDT-extending `insert_subframes` success loop. -/ +theorem derive_subframe_jA + (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) + (hTr : TreeA st OS Z.G.chains Z.G.par s) + (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) + (hcO : ∀ y ∈ OS, obj.sext48 y = ok y) + (cte : Fl4SpecA64.CslotPtr) (hab : AbsSlotA Z.th cte) + (fp rt asid va : U64) (sz : capability.FrameSize) (dev mp e : Bool) + (hcap : capAtA (L.mem s) (cslotAddrA cte) = .Frame fp sz rt dev mp e asid va) + (hfp : sz = .Large → dev = false → fp.val % 2 ^ 21 = 0 ∧ CapRt.Canonical fp) + (hfr : sz = .Large → dev = false → + ∀ w ∈ Z.G.chains.flatten, ∀ (q vm' : U64), fp.val ≤ q.val → q.val < fp.val + 2 ^ 21 → + OvlA (absCapA (.Frame q .Small vm' false false false 0#u64 0#u64)) (absCapA (capAtA (L.mem s) w)) → + isFrameA (capAtA (L.mem s) w) = true ∧ ∃ ch ∈ Z.G.chains, cslotAddrA cte ∈ ch ∧ w ∈ ch) + (length buf : U64) (call : Bool) (ex : Aeneas.Std.Array obj.Slot 3#usize) + (args : List U64) (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) + (x0 : U64) (hx0 : Aeneas.Std.Array.index_usize ex 0#usize = ok x0) + (xSlot : Fl4SpecA64.CapRef) (exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) + (hex : ExtraRelA Z.th (L.mem s) x0 (exs.map Prod.fst)) : + (do + let (r, s1) ← vspace.derive_subframe st s length (cslotAddrA cte) (.Frame fp sz rt dev mp e asid va) buf ex + ok (r, s1, a)) ⦃ res => + ArchTopPostA (L := L) AR Z OS m call σ ((Fl4SpecA64.fl4ArchDecodeInvocation (Z := Unit) call + (.ext .armPageDeriveSubframe) (args.map UScalar.bv) xSlot cte + (absFrameE e (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev (mapDataA mp asid va)) exs).run σ) s a res ⦄ := by + rw [dec_deriveSubA] + refine WP.spec_bind + (Pₘ := fun p => ArchTopPostA (L := L) AR Z OS m call σ + ((Fl4Spec.NonDetE.liftME Fl4SpecA64.Fl4ArchInvocation.deriveSubframe + (Fl4SpecA64.decodeDeriveSubframe (Z := Unit) (args.map UScalar.bv) cte + (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev (mapDataA mp asid va)) exs)).run σ) + s a (p.1, p.2, a)) ?_ fun ⟨r, s1⟩ hp => (WP.spec_ok _).2 hp + unfold vspace.derive_subframe + rw [cur_thread_ok, bind_ok] + -- 1. Extra cap 0 + have hex0 : vspace.extra ex 0#usize = ok (if x0 = 0#u64 then none else some x0) := by + unfold vspace.extra obj.Slot.is_null + simp only [show ¬(0#usize ≥ 3#usize) by decide, ↓reduceIte, hx0, bind_ok] + by_cases h : x0 = 0#u64 <;> simp [h] + rw [hex0, bind_ok] + by_cases hx0z : x0 = 0#u64 + · rw [if_pos hx0z]; dsimp only + simp only [error.truncated, bind_ok] + have he0 : exs.length = 0 := by + rcases hex with ⟨-, h⟩ | ⟨hne, -⟩ + · simpa using congrArg List.length h + · exact absurd hx0z hne + refine (WP.spec_ok _).2 ⟨rfl, rfl, .truncatedMessage, run_liftME_errA ?_, Or.inl trivial⟩ + unfold Fl4SpecA64.decodeDeriveSubframe; rw [if_pos (Or.inl he0)]; rfl + rw [if_neg hx0z]; dsimp only + have ⟨hx0s, ac0, exs', hca0, hexs⟩ : + IsSlotA Z.th x0 ∧ ∃ ac0 exs', CapRelA (capAtA (L.mem s) x0) ac0 ∧ exs.map Prod.fst = ac0 :: exs' := by + rcases hex with ⟨h0, -⟩ | ⟨-, hs, ac0, exs', hc, he⟩ + · exact absurd h0 hx0z + · exact ⟨hs, ac0, exs', hc, he⟩ + have hexLen : exs.length ≠ 0 := by + intro h; have : (exs.map Prod.fst).length = 0 := by simp [h] + rw [hexs] at this; simp at this + -- 2. Length ≥ 6 + by_cases hl6 : length ≥ 6#u64 + swap + · rw [if_neg hl6] + simp only [error.truncated, bind_ok] + have hlt6 : (args.map UScalar.bv).length < 6 := by + simp only [List.length_map]; rw [← hlen] + have : ¬ (6 ≤ length.val) := hl6; omega + refine (WP.spec_ok _).2 ⟨rfl, rfl, .truncatedMessage, run_liftME_errA ?_, Or.inl trivial⟩ + unfold Fl4SpecA64.decodeDeriveSubframe; rw [if_pos (Or.inr hlt6)]; rfl + rw [if_pos hl6] + have hge6 : 6 ≤ args.length := by rw [← hlen]; exact hl6 + have hnotTrunc : ¬ (exs.length = 0 ∨ (args.map UScalar.bv).length < 6) := by + simp only [List.length_map]; omega + -- 3. `large_ram_frame` + cases sz with + | Small | Huge => + simp only [vspace.large_ram_frame, bind_ok, error.illegal] + refine (WP.spec_ok _).2 ⟨rfl, rfl, .illegalOperation, run_liftME_errA ?_, Or.inl trivial⟩ + unfold Fl4SpecA64.decodeDeriveSubframe; rw [if_neg hnotTrunc]; rfl + | Large => + cases dev with + | true => + simp only [vspace.large_ram_frame, bind_ok, ↓reduceIte, error.illegal] + refine (WP.spec_ok _).2 ⟨rfl, rfl, .illegalOperation, run_liftME_errA ?_, Or.inl trivial⟩ + unfold Fl4SpecA64.decodeDeriveSubframe; rw [if_neg hnotTrunc]; rfl + | false => + simp only [vspace.large_ram_frame, bind_ok, Bool.false_eq_true, ↓reduceIte] + obtain ⟨hfpAl, hfpC⟩ := hfp rfl rfl + obtain ⟨a0, a1, a2, a3, a4, a5, rest, rfl⟩ : + ∃ a0 a1 a2 a3 a4 a5 rest, args = a0 :: a1 :: a2 :: a3 :: a4 :: a5 :: rest := by + match args, hge6 with + | a0 :: a1 :: a2 :: a3 :: a4 :: a5 :: rest, _ => exact ⟨a0, a1, a2, a3, a4, a5, rest, rfl⟩ + simp only [hargs 0#u64 a0 rfl, hargs 1#u64 a1 rfl, hargs 2#u64 a2 rfl, hargs 3#u64 a3 rfl, + hargs 4#u64 a4 rfl, bind_ok] + have hsh21 : (1#u64 <<< 21#i32) ⦃ w => w = 2097152#u64 ⦄ := by + step as ⟨w, hw⟩; apply UScalar.eq_of_val_eq; simpa [U64.size, U64.numBits] using hw + simp only [eq_of_spec_fd hsh21, bind_ok] + change (if a3 ≥ 2097152#u64 then _ else _) ⦃ _ ⦄ + have harg0 : Fl4SpecA64.the (List.map UScalar.bv (a0 :: a1 :: a2 :: a3 :: a4 :: a5 :: rest))[0]? = a0.bv := rfl + have harg1 : Fl4SpecA64.the (List.map UScalar.bv (a0 :: a1 :: a2 :: a3 :: a4 :: a5 :: rest))[1]? = a1.bv := rfl + have harg2 : Fl4SpecA64.the (List.map UScalar.bv (a0 :: a1 :: a2 :: a3 :: a4 :: a5 :: rest))[2]? = a2.bv := rfl + have harg3 : Fl4SpecA64.the (List.map UScalar.bv (a0 :: a1 :: a2 :: a3 :: a4 :: a5 :: rest))[3]? = a3.bv := rfl + have harg4 : Fl4SpecA64.the (List.map UScalar.bv (a0 :: a1 :: a2 :: a3 :: a4 :: a5 :: rest))[4]? = a4.bv := rfl + have harg5 : Fl4SpecA64.the (List.map UScalar.bv (a0 :: a1 :: a2 :: a3 :: a4 :: a5 :: rest))[5]? = a5.bv := rfl + have hex0_eq : (Fl4SpecA64.the exs[0]?).1 = ac0 := by + cases exs with + | nil => simp at hexLen + | cons p ps => + simp only [List.map_cons, List.cons.injEq] at hexs + simpa [Fl4SpecA64.the] using hexs.1 + -- 4. `offset >= 2^21` or `offset % 4096 ≠ 0` + by_cases hoff1 : a3 ≥ 2097152#u64 + · rw [if_pos hoff1] + simp only [error.invalid_arg, bind_ok] + have hc : 2 ^ 21 ≤ Fl4SpecA64.unat a3.bv ∨ Fl4SpecA64.unat a3.bv % 4096 ≠ 0 := + Or.inl (by change 2 ^ 21 ≤ a3.val; have : 2097152 ≤ a3.val := hoff1; omega) + refine (WP.spec_ok _).2 ⟨rfl, rfl, .invalidArgument 3, run_liftME_errA ?_, Or.inl rfl⟩ + unfold Fl4SpecA64.decodeDeriveSubframe; rw [if_neg hnotTrunc] + simp only [absFrameE, frameSizeOfA, List.map_cons, List.getElem?_cons_zero, List.getElem?_cons_succ, + Fl4SpecA64.the, Fl4Spec.NonDetE.whenE, if_pos hc] + exact run_throw_bindD + rw [if_neg hoff1] + have hmod4k : (a3 &&& 4095#u64).val = a3.val % 4096 := and_maskA a3 12 4095#u64 rfl + simp only [lift, bind_ok] + by_cases hoff2 : (a3 &&& 4095#u64) != 0#u64 + · rw [if_pos hoff2] + simp only [error.invalid_arg, bind_ok] + have hne : a3.val % 4096 ≠ 0 := by + have := bne_iff_ne.1 hoff2 + intro hz; exact this (UScalar.eq_of_val_eq (by rw [hmod4k, hz]; rfl)) + have hc : 2 ^ 21 ≤ Fl4SpecA64.unat a3.bv ∨ Fl4SpecA64.unat a3.bv % 4096 ≠ 0 := Or.inr hne + refine (WP.spec_ok _).2 ⟨rfl, rfl, .invalidArgument 3, run_liftME_errA ?_, Or.inl rfl⟩ + unfold Fl4SpecA64.decodeDeriveSubframe; rw [if_neg hnotTrunc] + simp only [absFrameE, frameSizeOfA, List.map_cons, List.getElem?_cons_zero, List.getElem?_cons_succ, + Fl4SpecA64.the, Fl4Spec.NonDetE.whenE, if_pos hc] + exact run_throw_bindD + rw [if_neg hoff2] + have hoffLt : a3.val < 2 ^ 21 := by + have : ¬ (2097152 ≤ a3.val) := hoff1; omega + have hoffMod : a3.val % 4096 = 0 := by + have : (a3 &&& 4095#u64) = 0#u64 := by by_contra hne; exact hoff2 (bne_iff_ne.2 hne) + rw [← hmod4k, this]; rfl + have hnotOff : ¬ (2 ^ 21 ≤ Fl4SpecA64.unat a3.bv ∨ Fl4SpecA64.unat a3.bv % 4096 ≠ 0) := by + change ¬ (2 ^ 21 ≤ a3.val ∨ a3.val % 4096 ≠ 0); omega + -- 5. `max = (2^21 - offset) >>> 12`, `count < 1 ∨ count > max` + have hsub21 : (2097152#u64 - a3) ⦃ w => w.val = 2 ^ 21 - a3.val ⦄ := by + step as ⟨w, hw⟩; omega + obtain ⟨i2, hi2Ok, hi2Val⟩ := spec_ex_fd hsub21 + have hmx : (i2 >>> 12#i32) ⦃ mx => mx.val = (2 ^ 21 - a3.val) / 4096 ⦄ := by + step as ⟨mx, hmx⟩ + rw [hmx, hi2Val, Nat.shiftRight_eq_div_pow] + obtain ⟨mx, hmxOk, hmxVal⟩ := spec_ex_fd hmx + rw [hi2Ok, bind_ok, hmxOk, bind_ok] + by_cases hc1 : a4 < 1#u64 + · rw [if_pos hc1] + simp only [error.range, bind_ok] + have hc : Fl4SpecA64.unat a4.bv < 1 ∨ (2 ^ 21 - Fl4SpecA64.unat a3.bv) / 4096 < Fl4SpecA64.unat a4.bv := + Or.inl (by change a4.val < 1; exact hc1) + refine (WP.spec_ok _).2 ⟨rfl, rfl, .rangeError 1 (BitVec.ofNat 64 ((2 ^ 21 - a3.val) / 4096)), run_liftME_errA ?_, Or.inl ⟨rfl, ?_⟩⟩ + · unfold Fl4SpecA64.decodeDeriveSubframe; rw [if_neg hnotTrunc] + simp only [absFrameE, frameSizeOfA, List.map_cons, List.getElem?_cons_zero, List.getElem?_cons_succ, + Fl4SpecA64.the, Fl4Spec.NonDetE.whenE, if_neg hnotOff, if_pos hc] + rw [run_pure_bindD]; exact run_throw_bindD + · apply BitVec.eq_of_toNat_eq + rw [BitVec.toNat_ofNat] + change ((2 ^ 21 - a3.val) / 4096) % 2 ^ 64 = mx.val + rw [hmxVal, Nat.mod_eq_of_lt (by omega)] + rw [if_neg hc1] + by_cases hc2 : a4 > mx + · rw [if_pos hc2] + simp only [error.range, bind_ok] + have hc : Fl4SpecA64.unat a4.bv < 1 ∨ (2 ^ 21 - Fl4SpecA64.unat a3.bv) / 4096 < Fl4SpecA64.unat a4.bv := + Or.inr (by change (2 ^ 21 - a3.val) / 4096 < a4.val; have : mx.val < a4.val := hc2; omega) + refine (WP.spec_ok _).2 ⟨rfl, rfl, .rangeError 1 (BitVec.ofNat 64 ((2 ^ 21 - a3.val) / 4096)), run_liftME_errA ?_, Or.inl ⟨rfl, ?_⟩⟩ + · unfold Fl4SpecA64.decodeDeriveSubframe; rw [if_neg hnotTrunc] + simp only [absFrameE, frameSizeOfA, List.map_cons, List.getElem?_cons_zero, List.getElem?_cons_succ, + Fl4SpecA64.the, Fl4Spec.NonDetE.whenE, if_neg hnotOff, if_pos hc] + rw [run_pure_bindD]; exact run_throw_bindD + · apply BitVec.eq_of_toNat_eq + rw [BitVec.toNat_ofNat] + change ((2 ^ 21 - a3.val) / 4096) % 2 ^ 64 = mx.val + rw [hmxVal, Nat.mod_eq_of_lt (by omega)] + rw [if_neg hc2] + have hnotCnt : ¬ (Fl4SpecA64.unat a4.bv < 1 ∨ (2 ^ 21 - Fl4SpecA64.unat a3.bv) / 4096 < Fl4SpecA64.unat a4.bv) := by + change ¬ (a4.val < 1 ∨ (2 ^ 21 - a3.val) / 4096 < a4.val) + have h1 : ¬ (a4.val < 1) := hc1 + have h2 : ¬ (mx.val < a4.val) := hc2 + omega + have hcntBound : a3.val + a4.val * 4096 ≤ 2 ^ 21 := by + have h2 : ¬ (mx.val < a4.val) := hc2 + omega + -- 6. `subframe_dest` + have hsd := subframe_dest_refinesA E.j.1.st E.j.1.cv E.j.1.place hx0s hca0 a0 a1 a2 a4 + obtain ⟨rd, hrd, hsdRel⟩ := spec_ex_fd hsd + rw [hrd, bind_ok] + cases rd with + | Err e => + obtain ⟨e', he', hv⟩ := hsdRel + simp only [core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame, + core.convert.FromSame.from, bind_ok, bind_tc_ok] + refine (WP.spec_ok _).2 ⟨rfl, rfl, e', run_liftME_errA ?_, hv⟩ + rw [decodeDeriveSubframe_stepA hnotTrunc hnotOff hnotCnt hex0_eq] + exact run_bind_errorD he' _ + | Ok dest => + obtain ⟨radix, hth, hr1, hr58, hoffR, hcntR, hsdOk⟩ := hsdRel + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok] + -- 7. `ensure_empty_run` + have her := ensure_empty_run_refinesA E.j.1.st E.j.1.place hth ⟨hr1, hr58⟩ a2 a4 (by omega) + obtain ⟨re, hre, herRel⟩ := spec_ex_fd her + rw [hre, bind_ok] + cases re with + | Err e => + obtain ⟨e', he', hv⟩ := herRel + simp only [core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame, + core.convert.FromSame.from, bind_ok, bind_tc_ok] + refine (WP.spec_ok _).2 ⟨rfl, rfl, e', run_liftME_errA ?_, hv⟩ + rw [decodeDeriveSubframe_stepA hnotTrunc hnotOff hnotCnt hex0_eq, run_bind_okD hsdOk] + exact run_bind_errorD he' _ + | Ok u => + obtain ⟨herOk, hall0⟩ := herRel + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok] + -- 8. `mask`, `Rights.from_word`, `mask_vm_rights`, `set_thread_state TS_RESTART` + rw [hargs 5#u64 a5 rfl, bind_ok, from_word_eqA, bind_ok] + obtain ⟨_, -, hcr⟩ := E.j.1.st.getAbsA hab + rw [hcap] at hcr + obtain ⟨vm, hvmOk, hvm1, hvm3, hvmEq⟩ := vm_maskA rt ⟨hcr.1.1, hcr.1.2.1⟩ (rightsWA a5) + rw [hvmOk, bind_ok] + have hvmSpec : vmRightsOfA vm = Fl4SpecA64.maskVmRights (vmRightsOfA rt) (Fl4SpecA64.dataToRights a5.bv) := by + rw [hvmEq, rightsW_specA]; rfl + obtain ⟨Rp, Nt, extra, sp, hPpl⟩ := RS.pl + obtain ⟨x, hk, hst, hset, E1, -, hOS1, -, -⟩ := E.restartA hPpl RS.sym RS.run RS.cur RS.vs RS.vc + rw [hst, bind_ok] + set s1 := L.wr s (stA (L.curW s)) sched.TS_RESTART + set σ1 := stUpdA σ σ.curThread x .restart + have hTr1 : TreeA st OS Z.G.chains Z.G.par s1 := treeA_of_mem hTr hOS1 + have hks1 : L.ksb s1 = L.ksb s := L.ksb_wr _ _ _ + have hcpu1 : L.cpu s1 = L.cpu s := L.cpu_wr _ _ _ + have hnul1 : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s1 y = .Null := fun y hy hn => by + have h' := hOS1 y hy; show capAtA (L.mem s1) y = .Null + unfold capAtA; rw [h'.1, h'.2.1]; exact hnul y hy hn + have hsrc1 : capAtA (L.mem s1) (cslotAddrA cte) = .Frame fp .Large rt false mp e asid va := by + have h' := hOS1 _ (RS.sub _ (hab.isSlot E.j.1.place)) + unfold capAtA; rw [h'.1, h'.2.1]; exact hcap + have hall1 : ∀ j < a4.val, capAtA (L.mem s1) + (cslotAddrA (specPtr dest, Fl4SpecA64.natToCref radix.val (a2.val + j))) = .Null := fun j hj => by + have hAd : AbsSlotA Z.th (specPtr dest, Fl4SpecA64.natToCref radix.val (a2.val + j)) := + ⟨dest, rfl, Or.inl (by rw [natToCref_lengthA radix.val _ (by omega)]; exact hth)⟩ + have h' := hOS1 _ (RS.sub _ (hAd.isSlot E.j.1.place)) + unfold capAtA; rw [h'.1, h'.2.1] + exact hall0 j hj + have hfr1 : ∀ w ∈ Z.G.chains.flatten, ∀ (q vm' : U64), fp.val ≤ q.val → q.val < fp.val + 2 ^ 21 → + OvlA (absCapA (.Frame q .Small vm' false false false 0#u64 0#u64)) (absCapA (capAtA (L.mem s1) w)) → + isFrameA (capAtA (L.mem s1) w) = true ∧ ∃ ch ∈ Z.G.chains, cslotAddrA cte ∈ ch ∧ w ∈ ch := by + intro w hw q vm' hq1 hq2 ho + have h' := hOS1 w ((hTr.lk.members w).1 hw).1 + have hcw : capAtA (L.mem s1) w = capAtA (L.mem s) w := by unfold capAtA; rw [h'.1, h'.2.1] + rw [hcw] at ho ⊢ + exact hfr rfl rfl w hw q vm' hq1 hq2 ho + -- 9. `insert_subframes` + have hins := insert_subframes_refinesA E RS.sub hab hth ⟨hr1, hr58⟩ hfpC hfpAl ⟨hvm1, hvm3⟩ + hcntBound (by omega) hcO E1.j hTr1 hks1 hcpu1 hnul1 hsrc1 hall1 hfr1 + refine WP.spec_bind hins fun s2 ⟨σ2, G2, hm2, hJ2, hT2, hC2⟩ => ?_ + refine (WP.spec_ok _).2 ?_ + set dests := (List.range' a2.val a4.val).map fun j => (specPtr dest, Fl4SpecA64.natToCref radix.val j) + set iv : Fl4SpecA64.Fl4SubframeInvocation := ⟨cte, specPtr fp + a3.bv, vmRightsOfA vm, dests⟩ + have hD : (Fl4Spec.NonDetE.liftME Fl4SpecA64.Fl4ArchInvocation.deriveSubframe + (Fl4SpecA64.decodeDeriveSubframe (Z := Unit) + (List.map UScalar.bv (a0 :: a1 :: a2 :: a3 :: a4 :: a5 :: rest)) cte + (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA .Large) false (mapDataA mp asid va)) exs)).run σ = + ({(.ok (.deriveSubframe iv), σ)}, False) := by + refine run_liftME_okA ?_ + rw [decodeDeriveSubframe_stepA hnotTrunc hnotOff hnotCnt hex0_eq, + run_bind_okD hsdOk, run_bind_okD herOk, ← hvmSpec] + rfl + have hperf : (.ok ([] : List Fl4SpecA64.Data), σ2) ∈ + ((Fl4SpecA64.fl4ArchPerformInvocation (Z := Unit) (.deriveSubframe iv)).run σ1).1 := by + unfold Fl4SpecA64.fl4ArchPerformInvocation Fl4SpecA64.performDeriveSubframe + dsimp only + have hlenD : dests.length = a4.val := by simp [dests] + have hrg : List.range dests.length = List.range' 0 a4.val := by + rw [hlenD, List.range_eq_range'] + rw [hrg] + exact mem_liftED ⟨((), σ2), by simpa [dests, iv] using hm2, Set.mem_singleton _⟩ + refine ⟨.deriveSubframe iv, okD_memA hD, σ1, hset, σ2, [], hperf, Or.inl ⟨{ Z with G := G2 }, hJ2, hT2, ?_⟩⟩ + intro hC + have hty := TyKeepA.stUpdA hk .restart + exact hC2 ⟨hC.1.of_capsA rfl (fun _ h => h) (fun _ h => h) (fun y hy => Or.inl (by + have h' := hOS1 y (RS.sub y hy); unfold capAtA; rw [h'.1, h'.2.1])), + hC.2.of_keepA hty.1 hty.2 (fun y hy => by + have h' := hOS1 y ((hTr.lk.members y).1 hy).1; unfold capAtA; rw [h'.1, h'.2.1]; exact CapFinEqA.refl _)⟩ + +end subframe_top + +/-! ## Complete `Cap::Frame` top (`decInv_frame_all_jA`) on `ArchTopPostA` -/ + +section frame_all +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +private theorem decode_frame_subframeA (ar : machine.Arch A) (length : U64) (slot : obj.Slot) + (fp rt asid va : U64) (sz : capability.FrameSize) (dev mp e call : Bool) (buf : U64) + (ex : Aeneas.Std.Array obj.Slot 3#usize) : + vspace.decode_frame st ar s a fl4_abi.label.ARMPageDeriveSubframe length slot + (.Frame fp sz rt dev mp e asid va) call buf ex = + (do + let (r, s1) ← vspace.derive_subframe st s length slot (.Frame fp sz rt dev mp e asid va) buf ex + ok (r, s1, a)) := by + have hne : fl4_abi.label.ARMPageDeriveSubframe ≠ fl4_abi.label.ARMPageMap ∧ + fl4_abi.label.ARMPageDeriveSubframe ≠ fl4_abi.label.ARMPageUnmap ∧ + vspace.is_cache_label fl4_abi.label.ARMPageDeriveSubframe = ok false ∧ + fl4_abi.label.ARMPageDeriveSubframe ≠ fl4_abi.label.ARMPageGetAddress ∧ + fl4_abi.label.ARMPageDeriveSubframe ≠ fl4_abi.label.ARMPageTestAndClearAccessed := by + refine ⟨by intro h; have := congrArg UScalar.val h; simp [fl4_abi.label.ARMPageDeriveSubframe, fl4_abi.label.ARMPageMap] at this, + by intro h; have := congrArg UScalar.val h; simp [fl4_abi.label.ARMPageDeriveSubframe, fl4_abi.label.ARMPageUnmap] at this, + by unfold vspace.is_cache_label; simp [fl4_abi.label.ARMPageDeriveSubframe, fl4_abi.label.ARMPageClean_Data, + fl4_abi.label.ARMPageInvalidate_Data, fl4_abi.label.ARMPageCleanInvalidate_Data, + fl4_abi.label.ARMPageUnify_Instruction], + by intro h; have := congrArg UScalar.val h; simp [fl4_abi.label.ARMPageDeriveSubframe, fl4_abi.label.ARMPageGetAddress] at this, + by intro h; have := congrArg UScalar.val h; simp [fl4_abi.label.ARMPageDeriveSubframe, fl4_abi.label.ARMPageTestAndClearAccessed] at this⟩ + unfold vspace.decode_frame vspace.as_frame + simp only [cur_thread_ok, bind_ok, hne.1, hne.2.1, hne.2.2.1, hne.2.2.2.1, hne.2.2.2.2, + Bool.false_eq_true, ↓reduceIte] + +/-- **Complete `Cap::Frame` top at `syscall.decode_invocation` on `ArchTopPostA`**: +covers every label (49 `ARMPageMap`, 50 `ARMPageUnmap`, 51–54 page cache flushes, 55 `ARMPageDeriveSubframe`, +56 `ARMPageTestAndClearAccessed`, 57 `ARMPageGetAddress`, and all illegal labels). -/ +theorem decInv_frame_all_jA {ar : machine.Arch A} (K : ArchKeepsA ar AR) + (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) + (hvcR : ∀ v ∈ Z.V, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → wa (L.curW s) K ≠ v ∧ wa (L.curW s) K ≠ wa v 24#u64) + (hTO : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s q) (hEO : ExtTabOffA (L := L) Z m s) + (hK : Aligned (L.ksb s)) (hT : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s)) (hIpa : S2IpaE ar a) + (hTr : TreeA st OS Z.G.chains Z.G.par s) (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) + (hcO : ∀ y ∈ OS, obj.sext48 y = ok y) + (cte : Fl4SpecA64.CslotPtr) (hab : AbsSlotA Z.th cte) (hos : cslotAddrA cte ∈ OS) + (fp rt asid va : U64) (sz : capability.FrameSize) (dev mp e : Bool) + (hcap : capAtA (L.mem s) (cslotAddrA cte) = .Frame fp sz rt dev mp e asid va) + (lbl length buf : U64) (block call cd fpb : Bool) (ex : Aeneas.Std.Array obj.Slot 3#usize) + (args : List U64) (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) + (x0 : U64) (hx0 : Aeneas.Std.Array.index_usize ex 0#usize = ok x0) + (xSlot : Fl4SpecA64.CapRef) (exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) + (hex : ExtraRelA Z.th (L.mem s) x0 (exs.map Prod.fst)) + (hMap : lbl = fl4_abi.label.ARMPageMap → FrPlA sz fp mp asid) + (hSub : lbl = fl4_abi.label.ARMPageDeriveSubframe → sz = .Large → dev = false → + (fp.val % 2 ^ 21 = 0 ∧ CapRt.Canonical fp) ∧ + ∀ w ∈ Z.G.chains.flatten, ∀ (q vm' : U64), fp.val ≤ q.val → q.val < fp.val + 2 ^ 21 → + OvlA (absCapA (.Frame q .Small vm' false false false 0#u64 0#u64)) (absCapA (capAtA (L.mem s) w)) → + isFrameA (capAtA (L.mem s) w) = true ∧ ∃ ch ∈ Z.G.chains, cslotAddrA cte ∈ ch ∧ w ∈ ch) : + syscall.decode_invocation st ar s a lbl length (cslotAddrA cte) (.Frame fp sz rt dev mp e asid va) block call cd + fpb buf ex ⦃ res => ArchTopPostA (L := L) AR Z OS m call σ ((Fl4SpecA64.fl4ArchDecodeInvocation (Z := Unit) call + (Fl4SpecA64.fl4LabelOf lbl.bv) (args.map UScalar.bv) xSlot cte + (absFrameE e (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev (mapDataA mp asid va)) exs).run σ) s a res ⦄ := by + by_cases h57 : lbl = fl4_abi.label.ARMPageGetAddress + · subst h57 + rw [decInv_frameA] + have hlab : Fl4SpecA64.fl4LabelOf fl4_abi.label.ARMPageGetAddress.bv = Fl4SpecA64.archL .armPageGetAddress := by + rw [fl4LabelOf_bvA, show fl4_abi.label.ARMPageGetAddress.val = 57 by simp [fl4_abi.label.ARMPageGetAddress]]; rfl + rw [hlab] + exact page_get_addr_jA E RS hvcR hTr cte fp rt asid va sz dev mp e length buf call ex args xSlot exs + by_cases h56 : lbl = fl4_abi.label.ARMPageTestAndClearAccessed + · subst h56 + rw [decInv_frameA, decode_frame_testClearA] + have hlab : Fl4SpecA64.fl4LabelOf fl4_abi.label.ARMPageTestAndClearAccessed.bv = + .ext .armPageTestAndClearAccessed := by + rw [fl4LabelOf_bvA, show fl4_abi.label.ARMPageTestAndClearAccessed.val = 56 by + simp [fl4_abi.label.ARMPageTestAndClearAccessed]]; rfl + rw [hlab] + exact test_clear_accessed_jA K E RS hvcR hTO hEO hTr cte hab fp rt asid va sz dev mp e hcap call args xSlot exs + by_cases h55 : lbl = fl4_abi.label.ARMPageDeriveSubframe + · subst h55 + rw [decInv_frameA, decode_frame_subframeA] + have hlab : Fl4SpecA64.fl4LabelOf fl4_abi.label.ARMPageDeriveSubframe.bv = + .ext .armPageDeriveSubframe := by + rw [fl4LabelOf_bvA, show fl4_abi.label.ARMPageDeriveSubframe.val = 55 by + simp [fl4_abi.label.ARMPageDeriveSubframe]]; rfl + rw [hlab] + exact derive_subframe_jA E RS hTr hnul hcO cte hab fp rt asid va sz dev mp e hcap + (fun h1 h2 => (hSub rfl h1 h2).1) (fun h1 h2 => (hSub rfl h1 h2).2) + length buf call ex args hlen hargs x0 hx0 xSlot exs hex + have hLbl : lbl.val ≤ 54 ∨ 57 < lbl.val := by + have h55' : lbl.val ≠ 55 := fun h => h55 (UScalar.eq_of_val_eq (by rw [h]; simp [fl4_abi.label.ARMPageDeriveSubframe])) + have h56' : lbl.val ≠ 56 := fun h => h56 (UScalar.eq_of_val_eq (by rw [h]; simp [fl4_abi.label.ARMPageTestAndClearAccessed])) + have h57' : lbl.val ≠ 57 := fun h => h57 (UScalar.eq_of_val_eq (by rw [h]; simp [fl4_abi.label.ARMPageGetAddress])) + omega + exact WP.spec_mono (decInv_frame_jA K E RS hTO hEO hK hT hIpa hTr hnul cte hab hos fp rt asid va sz dev mp e hcap + lbl length buf block call cd fpb call ex args hlen hargs x0 hx0 xSlot exs hex hLbl hMap) + fun _ hr => ArchTopPostA.of_vsTopA hr + +end frame_all + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/FrameA.lean b/proofs/Fl4ProofsA64/P3/FrameA.lean new file mode 100644 index 000000000..59832c8a2 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/FrameA.lean @@ -0,0 +1,891 @@ +/- +P3 (aarch64, A64-P3 S3 gen 2, S4 step 2): **frame lemmas for `JAtZAX` and `SInvXA`** — the twins of x86's +`StateRel.of_agree` (P3/UntypedPml4), `SchedRel.memFrame` (P3/VsTop), `SlotsSame` (P3/IpcSlotsSame), `CdtRel.of_same` +(P3/Restart) and the `JAtZB.*_frame*` family (P3/XTopJointStep, XTopJointReadFpu `frameWrJ`), in one place. + +**Implementation side.** `FootA Z s` is every word the relation reads at `s`: +* the four words of every slot (`IsSlotA` and the CNode slots by `ofBl` index, `CnSlotA`): cap words `+0` / `+8` + (`capAtA`), `PREV` `+16` and `NEXT` `+24` (`TreeA`'s `MdbLk`, `revA`); +* every core's node words `CUR_THREAD`, `IDLE_THREAD`, `CUR_SC`, `IDLE_SC`, and the current core's `SCHED_ACTION`, + `CUR_TIME`; +* every TCB's `STATE`, `SC`, `AFFINITY`, `VCPU` (+3664), `RUN_VCPU` (+3672); every SC's `TCB` (+24); every VCPU's + `TCB` (+0) and `RUN_CALLER` (+24); +* (gen 3) the IRQ state words of FL4's 5376 IRQs; the current core's `CONSUMED` / `REPROGRAM`, the global + `CUR_DOMAIN` / `DOMAIN_TIME`, every TCB's `PRIO` / `DOMAIN`, every SC's `REFILL_MAX` (`SchedRestA`). Gen 3 states `FootA` as an inductive, one constructor per kind of + word, so a new relation field adds a constructor. + +`SameA Z s s'` (the kernel base and core kept, the footprint's words equal) gives **`JAtZAX.of_mem`**. +**`JAtZAX.of_writesIn`**: a step that writes only `W` words (A64 P1's `WritesIn`), none of them in the `JAtZAX`, given that the footprint's words are aligned; **`FootA.aligned`** proves that from the +placement (`PlaceA.align` for the slots and TCBs) and three alignment facts of what `JAtZA` does not place (the +nodes, the SCs and the VCPUs: hypotheses; the nodes' from A64 P1's `NodePlaced` at entry level, `P3/EntryA`). + +**Gen 5 (ruling 11:09Z item 3, in place): slot vs non-slot footprint.** `FootA` is now the slot words (`slot`) or a +**`FootNA`** word (`nslot`: every non-slot word above); `SameNA` agrees on `FootNA` only, and `SchedRelA.of_mem`, +`IrqRelA.of_mem`, `SchedRestA.of_mem` (and the `SameNA.*` word lemmas, gen 2–3's `SameA.node` … `SameA.tcb2W`) take +it. **`JAtZAX.of_slot`**: `SameNA`, a slot-only spec step (**`SpecSlotA`**: every object kept or a CNode of the same +radix before and after, the globals `SpecGlobA` kept), the CSpace / CDT relations re-established at a new ghost +`G'`, and every slot's cap naming counted objects (`CapObjOkA`) give `JAtZAX` at `{Z with G := G'}`; **one lemma, +`JAtZAX.of_slotWrites`**, lifts every slot-only step (its `WritesIn` set off `FootNA`). `JAtZAX.of_mem` is now its +instance (gen 2's `JAtZA.of_mem`, `VcpuCoreA.of_mem`, `Stage2CoreA.of_mem`, `HighVSpaceCoreA.of_mem` are subsumed +and removed). + +**Spec side.** `SpecSameA Z m σ σ'`: the kernel objects at the related names (`RelPA`: `th`'s, `T`'s, the renamed SCs +of `C`, `V`'s, `X`'s and `Y`'s) are kept, no new VCPU / Stage-2 root / high window appears (`ExtObjA`), the CDT and +the original-cap map are kept, and the globals (`SpecGlobA`, gen 5: the current / idle thread, the current SC, the +scheduler action, the clock, the IRQ states and node, the queues, consumed time, reprogram flag, domain and domain +time) are kept: then **`JAtZAX.of_spec`**. `SchedRelA.of_spec` / `SchedRestA.of_spec` (gen 5) take the TCBs and SCs +kept and `SpecGlobA`, `IrqRelA.of_spec` takes `SpecGlobA`. `SchedSameA σ σ'`: every TCB and SC object is kept both +ways, and the current / idle thread, current SC, ready and release queues are kept: then **`SInvXA.of_spec`** +(`SInvXA` reads nothing of `s`; gen 4: given the IRQ node's `SpecIrqA` at `σ'`, a changed statement). + +**Gen 8 (ruling 12:48Z item 1, TCB slots).** `SpecSlotA` also allows a TCB that differs only in its cap fields +(`tcbNcA`, the TCB with its seven cap slots erased; `tcbNcA_eq` reads back the fields the relation uses); +`SpecSlotA.keep` / `.back` exclude TCBs too, new `SpecSlotA.tcb`; `SchedRelA.of_spec` / `SchedRestA.of_spec` take +`TcbKeepA` (every TCB of `T` kept up to its caps) instead of the TCBs kept; `SpecGlobA.trans` / `SpecSlotA.trans` +moved here from `MoveA`. + +No named premise. +-/ +import Fl4ProofsA64.P3.JointA +import Fl4ProofsA64.P3.Stage3SortA +import Fl4ProofsA64.P3.SInvXA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## Word arithmetic -/ + +theorem wa_valm (x k : U64) : (wa x k).val = (x.val + k.val) % 2 ^ 64 := wa_valP x k + +theorem wa_al {x k : U64} (hx : Aligned x) (hk : k.val % 8 = 0) : Aligned (wa x k) := by + unfold Aligned at hx ⊢; rw [wa_valm]; omega + +theorem slotAddrA_al {p : U64} (hp : p.val % 8 = 0) (i : BitVec 64) : Aligned (slotAddrA (specPtr p) i) := by + rw [slotAddrA_sp] + show (p.bv + i * 32#64).toNat % 8 = 0 + have e : p.bv.toNat = p.val := rfl + simp only [BitVec.toNat_add, BitVec.toNat_mul, e] + have : (32#64 : BitVec 64).toNat = 32 := rfl + rw [this]; omega + +theorem mem_of_head' {l : List U64} {a : U64} (h : l.head? = some a) : a ∈ l := by + cases l with + | nil => cases h + | cons b l => cases h; exact List.mem_cons_self + +theorem mem_of_getLast' {l : List U64} {a : U64} (h : l.getLast? = some a) : a ∈ l := by + rw [List.getLast?_eq_getElem?] at h; exact List.mem_iff_getElem?.2 ⟨_, h⟩ + +/-! ## The implementation footprint -/ + +/-- A CNode slot as `StateRelA` names it (by an `ofBl` index of the radix's length). -/ +def CnSlotA (th : TypedHeapA) (y : U64) : Prop := + ∃ p r, th p = some (.cnode r) ∧ ∃ idx : List Bool, idx.length = r ∧ y = slotAddrA (specPtr p) (Fl4SpecA64.ofBl idx) + +/-- A slot the relation reads. -/ +def SlotWA (th : TypedHeapA) (y : U64) : Prop := IsSlotA th y ∨ CnSlotA th y + +/-- An abstract slot of `th` is at a slot the relation reads. -/ +theorem absSlot_w {th : TypedHeapA} {sl : Fl4SpecA64.CslotPtr} (h : AbsSlotA th sl) : SlotWA th (cslotAddrA sl) := by + obtain ⟨p, e1, hc | ⟨ht, i, hi, e2⟩⟩ := h + · exact Or.inr ⟨p, _, hc, sl.2, rfl, by unfold cslotAddrA; rw [e1]⟩ + · refine Or.inl ⟨p, Or.inr ⟨ht, i, hi, ?_⟩⟩ + unfold cslotAddrA; rw [e1, e2, ofBl_tcbIdx i (by unfold tcbSlotsA at hi; omega)] + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-- **The words the relation reads outside the slots** (gen 5, ruling 11:09Z item 3: `FootA` factored into the slot +words and these). -/ +inductive FootNA (Z : XSetsA) (s : S) : U64 → Prop + | node (c w : U64) : c ∈ Z.Cs → (w = L.nodeAt s c layout.node.CUR_THREAD ∨ w = L.nodeAt s c layout.node.IDLE_THREAD ∨ + w = L.nodeAt s c layout.node.CUR_SC ∨ w = L.nodeAt s c layout.node.IDLE_SC) → FootNA Z s w + | cur (w : U64) : (w = L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION ∨ + w = L.nodeAt s (L.cpu s) layout.node.CUR_TIME) → FootNA Z s w + | tcb (t w : U64) : t ∈ Z.T → (w = stA t ∨ w = scA t ∨ w = affA t ∨ w = wa t 3664#u64 ∨ w = runVcpuA t) → + FootNA Z s w + | sc (c : U64) : c ∈ Z.C → FootNA Z s (wa c 24#u64) + | vcpu (v w : U64) : v ∈ Z.V → (w = v ∨ w = wa v 24#u64) → FootNA Z s w + /-- (gen 3) The IRQ state words of FL4's 5376 IRQs (`IrqRelA.st`). -/ + | irq (i : U64) : i.val < 5376 → FootNA Z s (irqWordA (L.ksb s) i) + /-- (gen 3, `SchedRestA`) The current core's `CONSUMED` / `REPROGRAM`, the global `CUR_DOMAIN` / `DOMAIN_TIME`, + each TCB's `PRIO` / `DOMAIN` and each SC's `REFILL_MAX`. -/ + | cur2 (w : U64) : (w = L.nodeAt s (L.cpu s) layout.node.CONSUMED ∨ + w = L.nodeAt s (L.cpu s) layout.node.REPROGRAM) → FootNA Z s w + | glob (w : U64) : (w = wa (L.ksb s) 8#u64 ∨ w = wa (L.ksb s) 16#u64) → FootNA Z s w + | tcb2 (t w : U64) : t ∈ Z.T → (w = prioA t ∨ w = domA t) → FootNA Z s w + | sc2 (c : U64) : c ∈ Z.C → FootNA Z s (wa c 72#u64) + /-- (gen 12, ruling 15:01Z item 3) The VSpace relation's words (`VsFootA`: the ASID table, the pools, the Stage-1 + tables and the windows' level-1 tables), all outside the slots. -/ + | vs (w : U64) : VsFootA Z.Pp Z.S1 Z.Y (L.ksb s) w → FootNA Z s w + /-- (gen 18, ruling 18:13Z item 4) The Stage-2 / high-window relation's words (`ExtFootE`: the roots' two pages, + the Stage-2 tables, the windows' level-0 entries 511 and 0..255), all outside the slots. -/ + | ext (w : U64) : ExtFootE Z.X Z.Y Z.T2 w → FootNA Z s w + +/-- **The words the relation reads** (module docstring; gen 3: an inductive, one constructor per kind of word, with +the IRQ state words; gen 5: the four words of the slots, or a `FootNA` word). -/ +inductive FootA (Z : XSetsA) (s : S) : U64 → Prop + | slot (y w : U64) : SlotWA Z.th y → (w = y ∨ w = wa y 8#u64 ∨ w = wa y 16#u64 ∨ w = wa y 24#u64) → FootA Z s w + | nslot (w : U64) : FootNA (L := L) Z s w → FootA Z s w + +/-- **Two states that agree on the footprint** (module docstring). -/ +structure SameA (Z : XSetsA) (s s' : S) : Prop where + ksb : L.ksb s' = L.ksb s + cpu : L.cpu s' = L.cpu s + mem : ∀ w, FootA (L := L) Z s w → L.mem s' w = L.mem s w + +/-- (gen 5) **Two states that agree on the footprint outside the slots.** -/ +structure SameNA (Z : XSetsA) (s s' : S) : Prop where + ksb : L.ksb s' = L.ksb s + cpu : L.cpu s' = L.cpu s + mem : ∀ w, FootNA (L := L) Z s w → L.mem s' w = L.mem s w + +theorem SameA.toN {Z : XSetsA} {s s' : S} (H : SameA (L := L) Z s s') : SameNA (L := L) Z s s' := + ⟨H.ksb, H.cpu, fun w h => H.mem w (.nslot w h)⟩ + +theorem nodeAt_ksb {s s' : S} (h : L.ksb s' = L.ksb s) (c o : U64) : L.nodeAt s' c o = L.nodeAt s c o := by + unfold LawfulStore.nodeAt; rw [h] + +section same +variable {Z : XSetsA} {s s' : S} (H : SameA (L := L) Z s s') +include H + +theorem SameA.slot {y : U64} (hy : SlotWA Z.th y) : + L.mem s' y = L.mem s y ∧ L.mem s' (wa y 8#u64) = L.mem s (wa y 8#u64) ∧ + L.mem s' (wa y 16#u64) = L.mem s (wa y 16#u64) ∧ L.mem s' (wa y 24#u64) = L.mem s (wa y 24#u64) := + ⟨H.mem _ (.slot _ _ hy (Or.inl rfl)), H.mem _ (.slot _ _ hy (Or.inr (Or.inl rfl))), + H.mem _ (.slot _ _ hy (Or.inr (Or.inr (Or.inl rfl)))), H.mem _ (.slot _ _ hy (Or.inr (Or.inr (Or.inr rfl))))⟩ + +theorem SameA.cap {y : U64} (hy : SlotWA Z.th y) : capAtA (L.mem s') y = capAtA (L.mem s) y := by + obtain ⟨e1, e2, -, -⟩ := H.slot hy + unfold capAtA; rw [e1, e2] + +theorem SameA.rev {y : U64} (hy : SlotWA Z.th y) : revA (L.mem s') y = revA (L.mem s) y := by + obtain ⟨-, -, -, e4⟩ := H.slot hy + unfold revA; rw [e4] + +end same + +section sameN +variable {Z : XSetsA} {s s' : S} (H : SameNA (L := L) Z s s') +include H + +theorem SameNA.node {c : U64} (hc : c ∈ Z.Cs) : + L.mem s' (L.nodeAt s' c layout.node.CUR_THREAD) = L.mem s (L.nodeAt s c layout.node.CUR_THREAD) ∧ + L.mem s' (L.nodeAt s' c layout.node.IDLE_THREAD) = L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) ∧ + L.mem s' (L.nodeAt s' c layout.node.CUR_SC) = L.mem s (L.nodeAt s c layout.node.CUR_SC) ∧ + L.mem s' (L.nodeAt s' c layout.node.IDLE_SC) = L.mem s (L.nodeAt s c layout.node.IDLE_SC) := by + simp only [nodeAt_ksb H.ksb] + exact ⟨H.mem _ (.node _ _ hc (Or.inl rfl)), H.mem _ (.node _ _ hc (Or.inr (Or.inl rfl))), + H.mem _ (.node _ _ hc (Or.inr (Or.inr (Or.inl rfl)))), H.mem _ (.node _ _ hc (Or.inr (Or.inr (Or.inr rfl))))⟩ + +theorem SameNA.tcbW {t : U64} (ht : t ∈ Z.T) : + L.mem s' (stA t) = L.mem s (stA t) ∧ L.mem s' (scA t) = L.mem s (scA t) ∧ + L.mem s' (affA t) = L.mem s (affA t) ∧ L.mem s' (wa t 3664#u64) = L.mem s (wa t 3664#u64) ∧ + L.mem s' (runVcpuA t) = L.mem s (runVcpuA t) := + ⟨H.mem _ (.tcb _ _ ht (Or.inl rfl)), H.mem _ (.tcb _ _ ht (Or.inr (Or.inl rfl))), + H.mem _ (.tcb _ _ ht (Or.inr (Or.inr (Or.inl rfl)))), + H.mem _ (.tcb _ _ ht (Or.inr (Or.inr (Or.inr (Or.inl rfl))))), + H.mem _ (.tcb _ _ ht (Or.inr (Or.inr (Or.inr (Or.inr rfl)))))⟩ + +theorem SameNA.scW {c : U64} (hc : c ∈ Z.C) : L.mem s' (wa c 24#u64) = L.mem s (wa c 24#u64) := + H.mem _ (.sc _ hc) + +theorem SameNA.vcW {v : U64} (hv : v ∈ Z.V) : + L.mem s' v = L.mem s v ∧ L.mem s' (wa v 24#u64) = L.mem s (wa v 24#u64) := + ⟨H.mem _ (.vcpu _ _ hv (Or.inl rfl)), H.mem _ (.vcpu _ _ hv (Or.inr rfl))⟩ + +theorem SameNA.act : L.actionW s' = L.actionW s := by + unfold LawfulStore.actionW; rw [H.cpu, nodeAt_ksb H.ksb] + exact H.mem _ (.cur _ (Or.inl rfl)) + +theorem SameNA.time : L.curTime s' = L.curTime s := by + unfold LawfulStore.curTime LawfulStore.nodeA; rw [H.cpu, nodeAt_ksb H.ksb] + exact H.mem _ (.cur _ (Or.inr rfl)) + +/-- (gen 3) The IRQ state words. -/ +theorem SameNA.irqW {i : U64} (hi : i.val < 5376) : + L.mem s' (irqWordA (L.ksb s') i) = L.mem s (irqWordA (L.ksb s) i) := by + rw [H.ksb]; exact H.mem _ (.irq _ hi) + +/-- (gen 3) The words `SchedRestA` reads. -/ +theorem SameNA.restW : + L.mem s' (L.nodeAt s' (L.cpu s') layout.node.CONSUMED) = L.mem s (L.nodeAt s (L.cpu s) layout.node.CONSUMED) ∧ + L.mem s' (L.nodeAt s' (L.cpu s') layout.node.REPROGRAM) = + L.mem s (L.nodeAt s (L.cpu s) layout.node.REPROGRAM) ∧ + L.mem s' (wa (L.ksb s') 8#u64) = L.mem s (wa (L.ksb s) 8#u64) ∧ + L.mem s' (wa (L.ksb s') 16#u64) = L.mem s (wa (L.ksb s) 16#u64) := by + rw [H.cpu]; simp only [nodeAt_ksb H.ksb]; rw [H.ksb] + exact ⟨H.mem _ (.cur2 _ (Or.inl rfl)), H.mem _ (.cur2 _ (Or.inr rfl)), H.mem _ (.glob _ (Or.inl rfl)), + H.mem _ (.glob _ (Or.inr rfl))⟩ + +/-- (gen 12) `VsRelA` carried by a state agreeing on the footprint outside the slots. -/ +theorem SameNA.vs {X : Set U64} {σ : Fl4SpecA64.State Unit} (h : VsRelA Z.Pp Z.S1 Z.Lv X Z.Y (L.mem s) (L.ksb s) σ) : + VsRelA Z.Pp Z.S1 Z.Lv X Z.Y (L.mem s') (L.ksb s') σ := by + rw [H.ksb]; exact h.of_mem fun b hb => H.mem b (.vs b hb) + +/-- (gen 18) `ExtVsRelA` carried by a state agreeing on the footprint outside the slots. -/ +theorem SameNA.ext {σ : Fl4SpecA64.State Unit} (h : ExtVsRelA Z.Pp Z.S1 Z.X Z.Y Z.T2 Z.Sh2 (L.mem s) σ) : + ExtVsRelA Z.Pp Z.S1 Z.X Z.Y Z.T2 Z.Sh2 (L.mem s') σ := + h.of_mem fun b hb => H.mem b (.ext b hb) + +theorem SameNA.tcb2W {t : U64} (ht : t ∈ Z.T) : + L.mem s' (prioA t) = L.mem s (prioA t) ∧ L.mem s' (domA t) = L.mem s (domA t) := + ⟨H.mem _ (.tcb2 _ _ ht (Or.inl rfl)), H.mem _ (.tcb2 _ _ ht (Or.inr rfl))⟩ + +end sameN + +/-! ## Memory frames, record by record -/ + +/-- **`TreeA` kept by equal slot words** (A64 P1's tree reads only the four words of its slots). -/ +theorem treeA_of_mem {OS : Set U64} {G : List (List U64)} {par : U64 → Option U64} {s s' : S} + (h : TreeA st OS G par s) + (hc : ∀ y ∈ OS, L.mem s' y = L.mem s y ∧ L.mem s' (wa y 8#u64) = L.mem s (wa y 8#u64) ∧ + L.mem s' (wa y 16#u64) = L.mem s (wa y 16#u64) ∧ L.mem s' (wa y 24#u64) = L.mem s (wa y 24#u64)) : + TreeA st OS G par s' := by + have cap : ∀ y ∈ OS, capA st s' y = capA st s y := fun y hy => by + obtain ⟨e1, e2, -, -⟩ := hc y hy + unfold capA; rw [e1, e2] + have nx : ∀ y ∈ OS, NxA st s' y = NxA st s y := fun y hy => by + unfold NxA; rw [(hc y hy).2.2.2] + have mem : ∀ ch ∈ G, ∀ a ∈ ch, a ∈ OS := fun ch hch a ha => + ((h.lk.members a).1 (List.mem_flatten.2 ⟨ch, hch, ha⟩)).1 + refine ⟨⟨h.lk.nodup, h.lk.ne_nil, fun ch hch i a b ha hb => ?_, fun ch hch a ha => ?_, fun ch hch a ha => ?_, + fun x => ?_⟩, h.tree, h.ut.recap h.parIn fun y hy => by rw [cap y hy]; exact UA.refl _⟩ + · obtain ⟨e1, e2⟩ := h.lk.chain ch hch i a b ha hb + rw [nx a (mem ch hch a (List.mem_iff_getElem?.2 ⟨_, ha⟩)), + (hc b (mem ch hch b (List.mem_iff_getElem?.2 ⟨_, hb⟩))).2.2.1] + exact ⟨e1, e2⟩ + · rw [(hc a (mem ch hch a (mem_of_head' ha))).2.2.1]; exact h.lk.head ch hch a ha + · rw [nx a (mem ch hch a (mem_of_getLast' ha))]; exact h.lk.last ch hch a ha + · rw [h.lk.members x] + constructor + · rintro ⟨hx, hn⟩; exact ⟨hx, by rw [cap x hx]; exact hn⟩ + · rintro ⟨hx, hn⟩; exact ⟨hx, by rw [← cap x hx]; exact hn⟩ + +section mem +variable {Z : XSetsA} {s s' : S} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +theorem StateRelA.of_mem (h : StateRelA Z.th (L.mem s) σ) (H : SameA (L := L) Z s s') : + StateRelA Z.th (L.mem s') σ where + cnode p r hp := by + obtain ⟨cs, h1, h2, h3⟩ := h.cnode p r hp + refine ⟨cs, h1, h2, fun idx hl => ?_⟩ + obtain ⟨ac, e, hr⟩ := h3 idx hl + exact ⟨ac, e, by rw [H.cap (Or.inr ⟨p, r, hp, idx, hl, rfl⟩)]; exact hr⟩ + tcb t ht := by + obtain ⟨x, h1, h2⟩ := h.tcb t ht + refine ⟨x, h1, fun i hi => ?_⟩ + obtain ⟨ac, e, hr⟩ := h2 i hi + exact ⟨ac, e, by rw [H.cap (Or.inl ⟨t, Or.inr ⟨ht, i, hi, rfl⟩⟩)]; exact hr⟩ + +theorem CdtRelA.of_mem (h : CdtRelA st Z.th s σ Z.G) (H : SameA (L := L) Z s s') : CdtRelA st Z.th s' σ Z.G := + CdtRelA.mkA (treeA_of_mem h.tree fun y hy => H.slot (Or.inl hy)) h.cdt h.cdt_slot + (fun sl hs => by rw [H.rev (absSlot_w hs)]; exact h.orig sl hs) h.par_slot h.po + +/-- (gen 5: from the footprint outside the slots, `SameNA`) -/ +theorem SchedRelA.of_mem (h : SchedRelA (L := L) Z.T Z.C Z.Cs Z.curOf Z.curScOf s σ m) + (H : SameNA (L := L) Z s s') : SchedRelA (L := L) Z.T Z.C Z.Cs Z.curOf Z.curScOf s' σ m where + core := by rw [H.cpu]; exact h.core + cpuCs := by rw [H.cpu]; exact h.cpuCs + curOfW c hc := by rw [(H.node hc).1]; exact h.curOfW c hc + curScOfW c hc := by rw [(H.node hc).2.2.1]; exact h.curScOfW c hc + cur := h.cur + csc := h.csc + idle := by + have e := (H.node h.cpuCs).2.1 + unfold LawfulStore.idleW; rw [H.cpu, e]; exact h.idle + idleOf c hc := by rw [(H.node hc).2.1]; exact h.idleOf c hc + idleScOf c hc := by rw [(H.node hc).2.2.2]; exact h.idleScOf c hc + act := by rw [H.act]; exact h.act + time := by rw [H.time]; exact h.time + aff t ht := by rw [(H.tcbW ht).2.2.1]; exact h.aff t ht + affT := h.affT + tcb t ht := by rw [(H.tcbW ht).1, (H.tcbW ht).2.1]; exact h.tcb t ht + sc c hc := by rw [H.scW hc]; exact h.sc c hc + scOff := h.scOff + +/-- (gen 3) `IrqRelA` kept by equal IRQ state words (gen 5: `SameNA`). -/ +theorem IrqRelA.of_mem (h : IrqRelA (L.mem s) (L.ksb s) σ) (H : SameNA (L := L) Z s s') : + IrqRelA (L.mem s') (L.ksb s') σ where + st i hi := by rw [H.irqW hi]; exact h.st i hi + node i hi := by rw [H.ksb]; exact h.node i hi + nodeHi i hi := by rw [H.ksb]; exact h.nodeHi i hi + +/-- (gen 3) `SchedRestA` kept by equal footprint words (gen 5: `SameNA`). -/ +theorem SchedRestA.of_mem (h : SchedRestA (L := L) Z s σ m) (H : SameNA (L := L) Z s s') : + SchedRestA (L := L) Z s' σ m where + rq := by rw [H.cpu]; exact h.rq + rqDom := h.rqDom + rel := by rw [H.cpu]; exact h.rel + consumed := by rw [H.restW.1]; exact h.consumed + repr := by rw [H.restW.2.1]; exact h.repr + curDom := by rw [H.restW.2.2.1]; exact h.curDom + domTime := by rw [H.restW.2.2.2]; exact h.domTime + prio t ht := by rw [(H.tcb2W ht).1, (H.tcb2W ht).2]; exact h.prio t ht + scMem t ht := by rw [(H.tcbW ht).2.1]; exact h.scMem t ht + refillMax c hc := by rw [H.mem _ (.sc2 _ hc)]; exact h.refillMax c hc + +/-- The TCBs and CNodes of a placed typed heap are 8-aligned. -/ +theorem PlaceA.al8 {th : TypedHeapA} {kb : U64} (hP : PlaceA th kb) {p : U64} {k : ObjKindA} (hp : th p = some k) + (hk : k = .tcb ∨ ∃ r, k = .cnode r) : p.val % 8 = 0 := by + have h := hP.align p k hp + rcases hk with rfl | ⟨r, rfl⟩ + · simp only [ObjKindA.size] at h; omega + · simp only [ObjKindA.size] at h + have : 8 ∣ 2 ^ (r + 5) := ⟨2 ^ (r + 2), by rw [show r + 5 = 3 + (r + 2) by omega, pow_add]; rfl⟩ + exact Nat.mod_eq_zero_of_dvd (Nat.dvd_trans this (Nat.dvd_of_mod_eq_zero h)) + +/-- **The footprint outside the slots is aligned** (gen 5; gen 3's `FootA.aligned` for these words): from `JAtZA`'s +placement, with the nodes' words (A64 P1's `NodePlaced`, `nodeAt_aligned`), the SCs and the VCPUs 8-aligned. -/ +theorem FootNA.aligned {AR : A → HwRootA → Prop} {a : A} (J : JAtZA (L := L) AR Z s a σ m) + (hN : ∀ c ∈ Z.Cs, ∀ o : U64, o.val < 4288 → o.val % 8 = 0 → Aligned (L.nodeAt s c o)) + (hC : ∀ c ∈ Z.C, Aligned c) (hV : ∀ v ∈ Z.V, Aligned v) : + ∀ b, FootNA (L := L) Z s b → Aligned b := by + have t8 : ∀ t ∈ Z.T, Aligned t := fun t ht => J.place.al8 ((J.thT t).2 ht) (Or.inl rfl) + have hcs := J.sch.cpuCs + have hkb : (L.ksb s).val % 32 = 0 := by + have h0 : (0#u64 : U64).val < 5376 := by simp + have h1 := J.place.align _ _ (J.place.irq 0#u64 h0) + rw [irqSlotA_val J.ksFit h0, show ObjKindA.size (.cnode 0) = 32 from rfl, + show (0#u64 : U64).val = 0 from rfl] at h1 + omega + intro b hb + cases hb with + | node c w hc hw => + rcases hw with rfl | rfl | rfl | rfl + · exact hN c hc _ (by simp [layout.node.CUR_THREAD]) (by simp [layout.node.CUR_THREAD]) + · exact hN c hc _ (by simp [layout.node.IDLE_THREAD]) (by simp [layout.node.IDLE_THREAD]) + · exact hN c hc _ (by simp [layout.node.CUR_SC]) (by simp [layout.node.CUR_SC]) + · exact hN c hc _ (by simp [layout.node.IDLE_SC]) (by simp [layout.node.IDLE_SC]) + | cur w hw => + rcases hw with rfl | rfl + · exact hN _ hcs _ (by simp [layout.node.SCHED_ACTION]) (by simp [layout.node.SCHED_ACTION]) + · exact hN _ hcs _ (by simp [layout.node.CUR_TIME]) (by simp [layout.node.CUR_TIME]) + | tcb t w ht hw => + rcases hw with rfl | rfl | rfl | rfl | rfl + · exact wa_al (t8 _ ht) (by simp [layout.tcb.STATE]) + · exact wa_al (t8 _ ht) (by simp) + · exact wa_al (t8 _ ht) (by simp) + · exact wa_al (t8 _ ht) (by simp) + · exact wa_al (t8 _ ht) (by simp) + | sc c hc => exact wa_al (hC _ hc) (by simp) + | vcpu v w hv hw => + rcases hw with rfl | rfl + · exact hV _ hv + · exact wa_al (hV _ hv) (by simp) + | irq i hi => + show (irqWordA (L.ksb s) i).val % 8 = 0 + rw [irqWordA_val J.ksFit hi]; omega + | cur2 w hw => + rcases hw with rfl | rfl + · exact hN _ hcs _ (by simp [layout.node.CONSUMED]) (by simp [layout.node.CONSUMED]) + · exact hN _ hcs _ (by simp [layout.node.REPROGRAM]) (by simp [layout.node.REPROGRAM]) + | glob w hw => + have hk8 : Aligned (L.ksb s) := by show (L.ksb s).val % 8 = 0; omega + rcases hw with rfl | rfl + · exact wa_al hk8 (by simp) + · exact wa_al hk8 (by simp) + | tcb2 t w ht hw => + rcases hw with rfl | rfl + · exact wa_al (t8 _ ht) (by simp) + · exact wa_al (t8 _ ht) (by simp) + | sc2 c hc => exact wa_al (hC _ hc) (by simp) + | vs w hw => + exact J.vs.foot_alignedA (show (L.ksb s).val % 8 = 0 by omega) _ hw + | ext w hw => exact J.ext.foot_alignedE J.vs.yal _ hw + +/-- **The footprint is aligned** (module docstring; gen 5: the slot words, then `FootNA.aligned`). -/ +theorem FootA.aligned {AR : A → HwRootA → Prop} {a : A} (J : JAtZA (L := L) AR Z s a σ m) + (hN : ∀ c ∈ Z.Cs, ∀ o : U64, o.val < 4288 → o.val % 8 = 0 → Aligned (L.nodeAt s c o)) + (hC : ∀ c ∈ Z.C, Aligned c) (hV : ∀ v ∈ Z.V, Aligned v) : + ∀ b, FootA (L := L) Z s b → Aligned b := by + have sl8 : ∀ y, SlotWA Z.th y → Aligned y := by + rintro y (⟨p, ⟨r, hr, i, -, rfl⟩ | ⟨ht, i, -, rfl⟩⟩ | ⟨p, r, hr, idx, -, rfl⟩) + · exact slotAddrA_al (J.place.al8 hr (Or.inr ⟨r, rfl⟩)) _ + · exact slotAddrA_al (J.place.al8 ht (Or.inl rfl)) _ + · exact slotAddrA_al (J.place.al8 hr (Or.inr ⟨r, rfl⟩)) _ + intro b hb + cases hb with + | slot y w hy hw => + rcases hw with rfl | rfl | rfl | rfl + · exact sl8 _ hy + · exact wa_al (sl8 _ hy) (by simp) + · exact wa_al (sl8 _ hy) (by simp) + · exact wa_al (sl8 _ hy) (by simp) + | nslot _ hw => exact FootNA.aligned J hN hC hV _ hw + +end mem + +/-! ## Spec frames -/ + +/-- The spec names of the related objects (module docstring). -/ +def RelPA (Z : XSetsA) (m : Fl4SpecA64.McState) (p : Fl4SpecA64.ObjRef) : Prop := + (∃ q, Z.th q ≠ none ∧ (specPtr q) = p) ∨ (∃ t ∈ Z.T, (specPtr t) = p) ∨ (∃ c ∈ Z.C, scRenA m c = p) ∨ + (∃ v ∈ Z.V, (specPtr v) = p) ∨ (∃ r ∈ Z.X, (specPtr r) = p) ∨ (∃ r ∈ Z.Y, (specPtr r) = p) ∨ + (∃ q ∈ Z.Pp, specPtr q = p) ∨ (∃ q ∈ Z.S1, specPtr q = p) ∨ (∃ q ∈ Z.T2, specPtr q = p) + +/-- A VCPU, a Stage-2 root or a high window (the objects the extension records' `back` fields count). -/ +def ExtObjA (ko : Fl4SpecA64.KernelObject) : Prop := + (∃ x, ko = .archObj (.vcpu x)) ∨ (∃ es, ko = .archObj (.ext (.stage2Root es))) ∨ + (∃ l1, ko = .archObj (.ext (.highVSpace l1))) + +/-- (gen 5) **The spec's scheduler, IRQ and domain globals kept** (gen 3's `SpecSameA` fields, factored out). -/ +structure SpecGlobA (σ σ' : Fl4SpecA64.State Unit) : Prop where + cur : σ'.curThread = σ.curThread + idle : σ'.idleThread = σ.idleThread + csc : σ'.curSc = σ.curSc + act : σ'.schedulerAction = σ.schedulerAction + time : σ'.curTime = σ.curTime + /-- (gen 3) The IRQ states and IRQ node are kept. -/ + irqS : σ'.interruptStates = σ.interruptStates + irqN : σ'.interruptIrqNode = σ.interruptIrqNode + /-- (gen 3) The queues, consumed time, reprogram flag, domain and domain time are kept. -/ + rq : σ'.readyQueues = σ.readyQueues + rls : σ'.releaseQueue = σ.releaseQueue + cons : σ'.consumedTime = σ.consumedTime + repr : σ'.reprogramTimer = σ.reprogramTimer + dom : σ'.curDomain = σ.curDomain + domT : σ'.domainTime = σ.domainTime + /-- (gen 12, ruling 15:01Z item 3) The ASID table is kept (`VsRelA.table`). -/ + asid : σ'.archState.armAsidTable = σ.archState.armAsidTable + /-- (record edit #7) The current VCPU is kept. -/ + vcur : σ'.archState.armCurrentVcpu = σ.archState.armCurrentVcpu + /-- (record edit #8b-B1, `domOK`) The domain-schedule position, start and table are kept. -/ + domk : DomKeepA σ σ' + +/-- **A spec update outside the related objects** (module docstring; gen 5: the globals as `SpecGlobA`). -/ +structure SpecSameA (Z : XSetsA) (m : Fl4SpecA64.McState) (σ σ' : Fl4SpecA64.State Unit) : Prop + extends SpecGlobA σ σ' where + kh : ∀ p, RelPA Z m p → σ'.kheap p = σ.kheap p + ext : ∀ p ko, σ'.kheap p = some ko → ExtObjA ko → σ.kheap p = some ko + cdt : σ'.cdt = σ.cdt + orig : σ'.isOriginalCap = σ.isOriginalCap + +/-- (gen 8) **A TCB with its seven cap slots erased**: two TCBs with the same `tcbNcA` differ only in their cap +fields (`tcb_cnode_map`), the fields a slot-only step writes. -/ +def tcbNcA (x : Fl4SpecA64.Tcb) : Fl4SpecA64.Tcb := + { x with tcbCtable := .nullCap, tcbVtable := .nullCap, tcbIpcframe := .nullCap, tcbFaultHandler := .nullCap, + tcbTimeoutHandler := .nullCap, + tcbExt := { x.tcbExt with tcbStage2 := .nullCap, tcbHighVSpace := .nullCap } } + +/-- (gen 8) The non-cap fields the relation reads are equal for TCBs equal up to their caps. -/ +theorem tcbNcA_eq {x x' : Fl4SpecA64.Tcb} (h : tcbNcA x' = tcbNcA x) : + x'.tcbState = x.tcbState ∧ x'.tcbSchedContext = x.tcbSchedContext ∧ x'.tcbDomain = x.tcbDomain ∧ + x'.tcbPriority = x.tcbPriority ∧ x'.tcbArch = x.tcbArch ∧ x'.tcbExt.tcbRunVcpu = x.tcbExt.tcbRunVcpu := by + have e1 := congrArg Fl4SpecA64.Tcb.tcbState h + have e2 := congrArg Fl4SpecA64.Tcb.tcbSchedContext h + have e3 := congrArg Fl4SpecA64.Tcb.tcbDomain h + have e4 := congrArg Fl4SpecA64.Tcb.tcbPriority h + have e5 := congrArg Fl4SpecA64.Tcb.tcbArch h + have e6 := congrArg (fun y : Fl4SpecA64.Tcb => y.tcbExt.tcbRunVcpu) h + exact ⟨e1, e2, e3, e4, e5, e6⟩ + +/-- (gen 5, ruling 11:09Z item 3; gen 8: TCB slots) **A spec step that changes only cap slots** (and the CDT and +original-cap map): every object is kept, or is a CNode of the same radix before and after, or (gen 8) a TCB before +and after that differs only in its cap fields (`tcbNcA`); the globals are kept. The spec side of a slot-only step +(`empty_slot`, `cap_insert`, `cap_move`, `cap_swap` at CNode and TCB slots). -/ +structure SpecSlotA (σ σ' : Fl4SpecA64.State Unit) : Prop extends SpecGlobA σ σ' where + kh : ∀ p, σ'.kheap p = σ.kheap p ∨ + (∃ r cs cs', σ.kheap p = some (.cnode r cs) ∧ σ'.kheap p = some (.cnode r cs')) ∨ + ∃ x x', σ.kheap p = some (.tcb x) ∧ σ'.kheap p = some (.tcb x') ∧ tcbNcA x' = tcbNcA x + +section spec +variable {Z : XSetsA} {s : S} {σ σ' : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +theorem SpecGlobA.refl (σ : Fl4SpecA64.State Unit) : SpecGlobA σ σ := + ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, ⟨rfl, rfl, rfl, rfl⟩⟩ + +theorem SpecSlotA.refl (σ : Fl4SpecA64.State Unit) : SpecSlotA σ σ := ⟨SpecGlobA.refl σ, fun _ => Or.inl rfl⟩ + +/-- A non-CNode, non-TCB object is kept by a slot-only spec step (gen 8: and not a TCB). -/ +theorem SpecSlotA.keep (H : SpecSlotA σ σ') {p : Fl4SpecA64.ObjRef} {ko : Fl4SpecA64.KernelObject} + (h : σ.kheap p = some ko) (hk : ∀ r cs, ko ≠ .cnode r cs) (ht : ∀ x, ko ≠ .tcb x) : σ'.kheap p = some ko := by + rcases H.kh p with e | ⟨r, cs, cs', e1, -⟩ | ⟨x, x', e1, -⟩ + · rw [e]; exact h + · rw [h] at e1; exact absurd (Option.some.inj e1) (hk r cs) + · rw [h] at e1; exact absurd (Option.some.inj e1) (ht x) + +/-- A non-CNode, non-TCB object after a slot-only spec step was there before (gen 8: and not a TCB). -/ +theorem SpecSlotA.back (H : SpecSlotA σ σ') {p : Fl4SpecA64.ObjRef} {ko : Fl4SpecA64.KernelObject} + (h : σ'.kheap p = some ko) (hk : ∀ r cs, ko ≠ .cnode r cs) (ht : ∀ x, ko ≠ .tcb x) : σ.kheap p = some ko := by + rcases H.kh p with e | ⟨r, cs, cs', -, e2⟩ | ⟨x, x', -, e2, -⟩ + · rw [← e]; exact h + · rw [h] at e2; exact absurd (Option.some.inj e2) (hk r cs') + · rw [h] at e2; exact absurd (Option.some.inj e2) (ht x') + +/-- (gen 8) A TCB after a slot-only spec step is a TCB equal up to its caps. -/ +theorem SpecSlotA.tcb (H : SpecSlotA σ σ') {p : Fl4SpecA64.ObjRef} {x : Fl4SpecA64.Tcb} + (h : σ.kheap p = some (.tcb x)) : ∃ x', σ'.kheap p = some (.tcb x') ∧ tcbNcA x' = tcbNcA x := by + rcases H.kh p with e | ⟨r, cs, cs', e1, -⟩ | ⟨y, y', e1, e2, hy⟩ + · exact ⟨x, by rw [e]; exact h, rfl⟩ + · rw [h] at e1; cases e1 + · rw [h] at e1 + obtain rfl : x = y := by injection e1 with e1; injection e1 + exact ⟨y', e2, hy⟩ + +/-- (record edit #8) **A slot-only spec step keeps the queue invariants' ingredients.** -/ +theorem QKeepA.of_slotA (H : SpecSlotA σ σ') : QKeepA σ σ' := by + refine ⟨fun t x hx => ?_, fun p x' hp => ?_, fun p x' hp => ?_, fun p y k hp => ?_, + fun p y hp => ⟨y, H.back hp (fun _ _ e => by cases e) (fun _ e => by cases e), id⟩, H.domk⟩ + · rcases H.kh t with e | ⟨r, cs, cs', -, e2⟩ | ⟨x, x', e1, e2, e3⟩ + · exact ⟨x, by rw [← e]; exact hx, rfl⟩ + · rw [hx] at e2; cases e2 + · rw [hx] at e2; cases e2 + exact ⟨x, e1, (tcbNcA_eq e3).2.2.2.1⟩ + · exact ⟨x', H.back hp (fun _ _ e => by cases e) (fun _ e => by cases e), List.Sublist.refl _⟩ + · exact ⟨x', H.back hp (fun _ _ e => by cases e) (fun _ e => by cases e), List.Sublist.refl _⟩ + · exact ⟨y, k, H.back hp (fun _ _ e => by cases e) (fun _ e => by cases e), fun r => ⟨r, id⟩⟩ + +/-- (VS gen 1, in `P3/VsRelA`; gen 12: moved here with `SpecSlotA`'s TCB case) **`VsRelA` across a slot-only spec +step** (`SpecSlotA`: every object kept, a CNode or a TCB up to its caps before and after) with the ASID table kept. -/ +theorem VsRelA.of_specSlot {Pp Tb : Set U64} {Lv : U64 → Nat} {X Y : Set U64} {ksb : U64} {mem : U64 → U64} + {σ σ' : Fl4SpecA64.State Unit} (R : VsRelA Pp Tb Lv X Y mem ksb σ) + (HS : SpecSlotA σ σ') (ht : σ'.archState.armAsidTable = σ.archState.armAsidTable) : + VsRelA Pp Tb Lv X Y mem ksb σ' := + R.of_spec ht fun p hp => by + rcases HS.kh p with e | ⟨r, cs, cs', h1, -⟩ | ⟨x, x', h1, -⟩ + · exact e + · exact absurd h1 (R.not_cnode hp) + · exact absurd h1 (R.not_tcb hp) + +/-- (gen 18) **`ExtVsRelA` across a slot-only spec step**: a pool, root or Stage-2 table is no CNode and no TCB. -/ +theorem ExtVsRelA.of_specSlot {Pp Tb : Set U64} {Lv : U64 → Nat} {X Y T2 : Set U64} {Sh2 : U64 → Nat} + {mem mem0 : U64 → U64} {ksb : U64} {σ σ' : Fl4SpecA64.State Unit} + (E : ExtVsRelA Pp Tb X Y T2 Sh2 mem σ) (R : VsRelA Pp Tb Lv X Y mem0 ksb σ) (HS : SpecSlotA σ σ') : + ExtVsRelA Pp Tb X Y T2 Sh2 mem σ' := + E.of_spec HS.asid fun q hq => by + rcases HS.kh (specPtr q) with e | ⟨r, cs, cs', h1, -⟩ | ⟨x, x', h1, -⟩ + · exact e + · rcases hq with hq | hq + · exact absurd h1 (R.not_cnode (Or.inl ⟨q, hq, rfl⟩)) + · exact absurd h1 (E.not_cnode hq r cs) + · rcases hq with hq | hq + · exact absurd h1 (R.not_tcb (Or.inl ⟨q, hq, rfl⟩)) + · exact absurd h1 (E.not_tcb hq x) + +theorem SpecGlobA.trans {σ1 σ2 σ3 : Fl4SpecA64.State Unit} (h1 : SpecGlobA σ1 σ2) (h2 : SpecGlobA σ2 σ3) : + SpecGlobA σ1 σ3 := + ⟨h2.cur.trans h1.cur, h2.idle.trans h1.idle, h2.csc.trans h1.csc, h2.act.trans h1.act, h2.time.trans h1.time, + h2.irqS.trans h1.irqS, h2.irqN.trans h1.irqN, h2.rq.trans h1.rq, h2.rls.trans h1.rls, h2.cons.trans h1.cons, + h2.repr.trans h1.repr, h2.dom.trans h1.dom, h2.domT.trans h1.domT, h2.asid.trans h1.asid, h2.vcur.trans h1.vcur, h1.domk.trans h2.domk⟩ + +/-- Slot-only spec steps compose (gen 5, `MoveA`; gen 8: moved here, with the TCB case). -/ +theorem SpecSlotA.trans {σ1 σ2 σ3 : Fl4SpecA64.State Unit} (h1 : SpecSlotA σ1 σ2) (h2 : SpecSlotA σ2 σ3) : + SpecSlotA σ1 σ3 := by + refine ⟨h1.toSpecGlobA.trans h2.toSpecGlobA, fun p => ?_⟩ + rcases h1.kh p with e1 | ⟨r, c0, c1, f1, f2⟩ | ⟨x0, x1, f1, f2, g1⟩ + · rcases h2.kh p with e2 | ⟨r', c1', c2, e1', e2'⟩ | ⟨y1, y2, e1', e2', g2⟩ + · exact Or.inl (e2.trans e1) + · exact Or.inr (Or.inl ⟨r', c1', c2, e1.symm.trans e1', e2'⟩) + · exact Or.inr (Or.inr ⟨y1, y2, e1.symm.trans e1', e2', g2⟩) + · rcases h2.kh p with e2 | ⟨r', c1', c2, e1', e2'⟩ | ⟨y1, y2, e1', e2', g2⟩ + · exact Or.inr (Or.inl ⟨r, c0, c1, f1, e2.trans f2⟩) + · rw [f2] at e1' + obtain rfl : r = r' := by injection e1' with h; injection h + exact Or.inr (Or.inl ⟨r, c0, c2, f1, e2'⟩) + · rw [f2] at e1'; cases e1' + · rcases h2.kh p with e2 | ⟨r', c1', c2, e1', e2'⟩ | ⟨y1, y2, e1', e2', g2⟩ + · exact Or.inr (Or.inr ⟨x0, x1, f1, e2.trans f2, g1⟩) + · rw [f2] at e1'; cases e1' + · rw [f2] at e1' + obtain rfl : x1 = y1 := by injection e1' with h; injection h + exact Or.inr (Or.inr ⟨x0, y2, f1, e2', g2.trans g1⟩) + +/-- (gen 8) The TCBs kept up to their caps (`SchedRelA.of_spec` / `SchedRestA.of_spec`'s hypothesis). -/ +def TcbKeepA (T : Set U64) (σ σ' : Fl4SpecA64.State Unit) : Prop := + ∀ t ∈ T, ∀ x, σ.kheap (specPtr t) = some (.tcb x) → ∃ x', σ'.kheap (specPtr t) = some (.tcb x') ∧ tcbNcA x' = tcbNcA x + +theorem TcbKeepA.of_eq {T : Set U64} (hT : ∀ t ∈ T, σ'.kheap (specPtr t) = σ.kheap (specPtr t)) : TcbKeepA T σ σ' := + fun t ht x e => ⟨x, by rw [hT t ht]; exact e, rfl⟩ + +theorem SpecSameA.th (H : SpecSameA Z m σ σ') {q : U64} {k : ObjKindA} (h : Z.th q = some k) : + σ'.kheap (specPtr q) = σ.kheap (specPtr q) := H.kh _ (Or.inl ⟨q, by rw [h]; exact Option.some_ne_none _, rfl⟩) + +theorem SpecSameA.t (H : SpecSameA Z m σ σ') {t : U64} (h : t ∈ Z.T) : σ'.kheap (specPtr t) = σ.kheap (specPtr t) := + H.kh _ (Or.inr (Or.inl ⟨t, h, rfl⟩)) + +theorem SpecSameA.c (H : SpecSameA Z m σ σ') {c : U64} (h : c ∈ Z.C) : + σ'.kheap (scRenA m c) = σ.kheap (scRenA m c) := + H.kh _ (Or.inr (Or.inr (Or.inl ⟨c, h, rfl⟩))) + +theorem StateRelA.of_spec (h : StateRelA Z.th (L.mem s) σ) (H : SpecSameA Z m σ σ') : + StateRelA Z.th (L.mem s) σ' where + cnode p r hp := by rw [H.th hp]; exact h.cnode p r hp + tcb t ht := by rw [H.th ht]; exact h.tcb t ht + +theorem CdtRelA.of_spec (h : CdtRelA st Z.th s σ Z.G) (H : SpecSameA Z m σ σ') : CdtRelA st Z.th s σ' Z.G where + tree := h.tree + cdt := by rw [H.cdt]; exact h.cdt + cdt_slot := by rw [H.cdt]; exact h.cdt_slot + orig := by rw [H.orig]; exact h.orig + par_slot := h.par_slot + prev := h.prev + +/-- (gen 5: from the TCBs and SCs kept and `SpecGlobA`; gen 8: the TCBs kept up to their caps, `TcbKeepA`) -/ +theorem SchedRelA.of_spec (h : SchedRelA (L := L) Z.T Z.C Z.Cs Z.curOf Z.curScOf s σ m) + (hT : TcbKeepA Z.T σ σ') (hC : ∀ c ∈ Z.C, σ'.kheap (scRenA m c) = σ.kheap (scRenA m c)) + (H : SpecGlobA σ σ') : SchedRelA (L := L) Z.T Z.C Z.Cs Z.curOf Z.curScOf s σ' m where + core := h.core + cpuCs := h.cpuCs + curOfW := h.curOfW + curScOfW := h.curScOfW + cur := by rw [H.cur]; exact h.cur + csc := by rw [H.csc]; exact h.csc + idle := by rw [H.idle]; exact h.idle + idleOf := h.idleOf + idleScOf := h.idleScOf + act := by rw [H.act]; exact h.act + time := by rw [H.time]; exact h.time + aff := h.aff + affT := h.affT + tcb t ht := by + obtain ⟨x, e, h1, h2⟩ := h.tcb t ht + obtain ⟨x', e', hx⟩ := hT t ht x e + obtain ⟨f1, f2, -⟩ := tcbNcA_eq hx + exact ⟨x', e', by rw [f1]; exact h1, by rw [f2]; exact h2⟩ + sc c hc := by rw [hC c hc]; exact h.sc c hc + scOff := h.scOff + +/-- (gen 3) `IrqRelA` kept by a spec update that keeps the IRQ states and node (gen 5: `SpecGlobA`). -/ +theorem IrqRelA.of_spec {mem : U64 → U64} {kb : U64} (h : IrqRelA mem kb σ) (H : SpecGlobA σ σ') : + IrqRelA mem kb σ' where + st := by rw [H.irqS]; exact h.st + node := by rw [H.irqN]; exact h.node + nodeHi := by rw [H.irqN]; exact h.nodeHi + +/-- (gen 3) `SchedRestA` kept by a spec update keeping the TCBs, SCs and globals (gen 5: as `SchedRelA.of_spec`; +gen 8: `TcbKeepA`). -/ +theorem SchedRestA.of_spec (h : SchedRestA (L := L) Z s σ m) + (hT : TcbKeepA Z.T σ σ') (hC : ∀ c ∈ Z.C, σ'.kheap (scRenA m c) = σ.kheap (scRenA m c)) + (H : SpecGlobA σ σ') : SchedRestA (L := L) Z s σ' m where + rq := by rw [H.rq]; exact h.rq + rqDom := by rw [H.rq]; exact h.rqDom + rel := by rw [H.rls]; exact h.rel + consumed := by rw [H.cons]; exact h.consumed + repr := by rw [H.repr]; exact h.repr + curDom := by rw [H.dom]; exact h.curDom + domTime := by rw [H.domT]; exact h.domTime + prio t ht := by + obtain ⟨x, e, h1, h2, h3⟩ := h.prio t ht + obtain ⟨x', e', hx⟩ := hT t ht x e + obtain ⟨-, -, f3, f4, -⟩ := tcbNcA_eq hx + exact ⟨x', e', by rw [f3]; exact h1, h2, by rw [f4]; exact h3⟩ + scMem := h.scMem + refillMax c hc := by rw [hC c hc]; exact h.refillMax c hc + +/-- **`JAtZAX` kept by a spec update outside the related objects** (module docstring). -/ +theorem JAtZAX.of_spec {AR : A → HwRootA → Prop} {a : A} (J : JAtZAX (L := L) AR Z s a σ m) + (H : SpecSameA Z m σ σ') : JAtZAX (L := L) AR Z s a σ' m := by + obtain ⟨J, V, X, Y⟩ := J + have hT : TcbKeepA Z.T σ σ' := TcbKeepA.of_eq fun t ht => H.t ht + have hC := fun c (hc : c ∈ Z.C) => H.c hc + have J' : JAtZA (L := L) AR Z s a σ' m := + { J with + st := J.st.of_spec H + cdt := J.cdt.of_spec H + sch := J.sch.of_spec hT hC H.toSpecGlobA + irq := J.irq.of_spec H.toSpecGlobA + rest := J.rest.of_spec hT hC H.toSpecGlobA + vs := J.vs.of_spec H.asid fun p hp => H.kh p (by + rcases hp with ⟨q, hq, rfl⟩ | ⟨q, hq, rfl⟩ | ⟨q, hq, rfl⟩ + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨q, hq, rfl⟩)))))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨q, hq, rfl⟩))))))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨q, hq, rfl⟩)))))) + ext := J.ext.of_spec H.asid fun q hq => H.kh _ (by + rcases hq with hq | hq | hq + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨q, hq, rfl⟩)))))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨q, hq, rfl⟩)))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ⟨q, hq, rfl⟩)))))))) } + refine ⟨J', ?_, ?_, ?_⟩ + · exact ⟨fun v hv => by rw [H.kh _ (Or.inr (Or.inr (Or.inr (Or.inl ⟨v, hv, rfl⟩))))]; exact V.obj v hv, + fun p x e => V.back p x (H.ext p _ e (Or.inl ⟨x, rfl⟩)), fun t ht => by rw [H.t ht]; exact V.tcb t ht, V.cap⟩ + · exact ⟨fun r hr => by rw [H.kh _ (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨r, hr, rfl⟩)))))]; exact X.obj r hr, + fun p es e => X.back p es (H.ext p _ e (Or.inr (Or.inl ⟨es, rfl⟩))), X.cap⟩ + · exact ⟨fun r hr => by rw [H.kh _ (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨r, hr, rfl⟩))))))]; exact Y.obj r hr, + fun p l1 e => Y.back p l1 (H.ext p _ e (Or.inr (Or.inr ⟨l1, rfl⟩))), Y.cap⟩ + +end spec + +/-! ## Slot-word steps (gen 5, ruling 11:09Z item 3) -/ + +/-- The objects a cap names that the extension records count: a `Vcpu` cap's VCPU is in `V`, an `Ept` cap's root +in `X`, a `HighVSpace` cap's window in `Y`; (gen 9) and a CNode / thread cap names a CNode / TCB of `th` +(`CapValidA`, `JAtZA.cv`). A `Pml4` cap names a depth-0 Stage-1 table (`JAtZA.pml`). -/ +def CapObjOkA (Z : XSetsA) (c : capability.Cap) : Prop := + (∀ p, c = .Vcpu p → p ∈ Z.V) ∧ (∀ p l mp asid va, c = .Ept p l mp asid va → p ∈ Z.X) ∧ + (∀ p mp asid, c = .HighVSpace p mp asid → p ∈ Z.Y) ∧ + (∀ p mp asid, c = .Pml4 p mp asid → p ∈ Z.S1 ∧ Z.Lv p = 0) ∧ CapValidA Z.th c + +theorem CapObjOkA.null (Z : XSetsA) : CapObjOkA Z .Null := + ⟨fun _ h => (by cases h), fun _ _ _ _ _ h => (by cases h), fun _ _ _ h => (by cases h), fun _ _ _ h => (by cases h), trivial⟩ + +section slotw +variable {Z : XSetsA} {s s' : S} {σ σ' : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- (gen 18, ruling 18:13Z item 4) **A mapped plain frame cap's address is a Stage-1 or a window address** +(`VaOkE`, `CapValidA`'s frame arm through `cv`): ExtVsA gen 3's `hVa` of `unmap_frameXE` at `JAtZA`. -/ +theorem JAtZA.vaOkE {AR : A → HwRootA → Prop} {a : A} (J : JAtZA (L := L) AR Z s a σ m) {y : U64} + (hy : IsSlotA Z.th y) {p : U64} {sz : capability.FrameSize} {r : U64} {dev : Bool} {asid va : U64} + (hc : capAtA (L.mem s) y = .Frame p sz r dev true false asid va) : VaOkE va := by + have h := J.cv y hy + rw [hc] at h + exact h rfl rfl + +/-- Every slot's cap names counted objects (the extension records' `cap` fields). -/ +theorem JAtZAX.capObj {AR : A → HwRootA → Prop} {a : A} (J : JAtZAX (L := L) AR Z s a σ m) {y : U64} + (hy : IsSlotA Z.th y) : CapObjOkA Z (capAtA (L.mem s) y) := + ⟨J.2.1.cap y hy, J.2.2.1.cap y hy, J.2.2.2.cap y hy, J.1.pml y hy, J.1.cv y hy⟩ + +/-- **`JAtZAX` across a slot-only step** (gen 5, ruling 11:09Z item 3): the words outside the slots are kept +(`SameNA`), the spec changes only CNode contents, the CDT and the original-cap map (`SpecSlotA`), the CSpace and CDT +relations hold afterwards at the new ghost `G'`, and every slot's cap names counted objects. -/ +theorem JAtZAX.of_slot {AR : A → HwRootA → Prop} {a : A} (J : JAtZAX (L := L) AR Z s a σ m) (G' : MdbGhostA) + (H : SameNA (L := L) Z s s') (HS : SpecSlotA σ σ') (hst : StateRelA Z.th (L.mem s') σ') + (hcdt : CdtRelA st Z.th s' σ' G') (hcap : ∀ y, IsSlotA Z.th y → CapObjOkA Z (capAtA (L.mem s') y)) : + JAtZAX (L := L) AR { Z with G := G' } s' a σ' m := by + obtain ⟨J, V, X, Y⟩ := J + have hT : TcbKeepA Z.T σ σ' := fun t _ x e => HS.tcb e + have hC : ∀ c ∈ Z.C, σ'.kheap (scRenA m c) = σ.kheap (scRenA m c) := fun c hc => by + obtain ⟨y, n, e, -⟩ := J.sch.sc c hc + rw [e]; exact HS.keep e (fun _ _ h => by cases h) (fun _ h => by cases h) + have hR := (J.rest.of_mem H).of_spec hT hC HS.toSpecGlobA + have J' : JAtZA (L := L) AR { Z with G := G' } s' a σ' m := + { st := hst + cdt := hcdt + sch := (J.sch.of_mem H).of_spec hT hC HS.toSpecGlobA + ar := J.ar + place := by rw [H.ksb]; exact J.place + irq := (J.irq.of_mem H).of_spec HS.toSpecGlobA + rest := ⟨hR.rq, hR.rqDom, hR.rel, hR.consumed, hR.repr, hR.curDom, hR.domTime, hR.prio, hR.scMem, + hR.refillMax⟩ + thT := J.thT + csN := J.csN + idleT := fun c hc => by rw [(H.node hc).2.1]; exact J.idleT c hc + idleC := fun c hc => by rw [(H.node hc).2.2.2]; exact J.idleC c hc + curT := fun c hc => by rw [(H.node hc).1]; exact J.curT c hc + cscC := fun c hc => by rw [(H.node hc).2.2.1]; exact J.cscC c hc + ksFit := by rw [H.ksb]; exact J.ksFit + scOffD := J.scOffD + cv := fun y hy => (hcap y hy).2.2.2.2 + pml := fun y hy => (hcap y hy).2.2.2.1 + vs := (H.vs J.vs).of_specSlot HS HS.asid + ext := (H.ext J.ext).of_specSlot J.vs HS } + refine ⟨J', ?_, ?_, ?_⟩ + · refine ⟨fun v hv => ?_, fun p x e => V.back p x (HS.back e (fun _ _ h => by cases h) (fun _ h => by cases h)), + fun t ht => ?_, fun y hy p e => (hcap y hy).1 p e⟩ + · obtain ⟨x, e, h2⟩ := V.obj v hv + rw [(H.vcW hv).1, (H.vcW hv).2] + exact ⟨x, HS.keep e (fun _ _ h => by cases h) (fun _ h => by cases h), h2⟩ + · obtain ⟨x, e, h1, h2⟩ := V.tcb t ht + obtain ⟨x', e', hx⟩ := HS.tcb e + obtain ⟨-, -, -, -, f5, f6⟩ := tcbNcA_eq hx + rw [(H.tcbW ht).2.2.2.1, (H.tcbW ht).2.2.2.2] + exact ⟨x', e', by rw [f5]; exact h1, by rw [f6]; exact h2⟩ + · refine ⟨fun r hr => ?_, fun p es e => X.back p es (HS.back e (fun _ _ h => by cases h) (fun _ h => by cases h)), + fun y hy p l mp asid va e => (hcap y hy).2.1 p l mp asid va e⟩ + obtain ⟨es, e⟩ := X.obj r hr + exact ⟨es, HS.keep e (fun _ _ h => by cases h) (fun _ h => by cases h)⟩ + · refine ⟨fun r hr => ?_, fun p l1 e => Y.back p l1 (HS.back e (fun _ _ h => by cases h) (fun _ h => by cases h)), + fun y hy p mp asid e => (hcap y hy).2.2.1 p mp asid e⟩ + obtain ⟨l1, e⟩ := Y.obj r hr + exact ⟨l1, HS.keep e (fun _ _ h => by cases h) (fun _ h => by cases h)⟩ + +/-- **`JAtZAX` kept by a state that agrees on the footprint** (module docstring; gen 5: `JAtZAX.of_slot` with the +spec and the ghost kept). -/ +theorem JAtZAX.of_mem {AR : A → HwRootA → Prop} {a : A} (J : JAtZAX (L := L) AR Z s a σ m) + (H : SameA (L := L) Z s s') : JAtZAX (L := L) AR Z s' a σ m := + J.of_slot Z.G H.toN (SpecSlotA.refl σ) (J.1.st.of_mem H) (J.1.cdt.of_mem H) fun y hy => by + rw [H.cap (Or.inl hy)]; exact J.capObj hy + +/-- **`JAtZAX` across a step writing only words off the footprint** (module docstring; A64 P1's `WritesIn`). -/ +theorem JAtZAX.of_writesIn {AR : A → HwRootA → Prop} {a : A} (J : JAtZAX (L := L) AR Z s a σ m) {W : U64 → Prop} + (hw : WritesIn st s s' W) (hW : ∀ b, W b → ¬ FootA (L := L) Z s b) + (hal : ∀ b, FootA (L := L) Z s b → Aligned b) : JAtZAX (L := L) AR Z s' a σ m := + J.of_mem ⟨hw.1, hw.2.1, fun b hb => hw.2.2 b (hal b hb) fun e => hW b e hb⟩ + +/-- **`JAtZAX` across a slot-word step** (gen 5, ruling 11:09Z item 3): a step writing only `W` words (A64 P1's +`WritesIn`), none of them outside-the-slots footprint (`FootNA`), with the spec side a `SpecSlotA` step and the +CSpace / CDT relations re-established at the new ghost by the leaf. One lemma for every slot-only step: the +scheduler, IRQ, VCPU, Stage-2 and high-window parts carry over. -/ +theorem JAtZAX.of_slotWrites {AR : A → HwRootA → Prop} {a : A} (J : JAtZAX (L := L) AR Z s a σ m) + {W : U64 → Prop} (hw : WritesIn st s s' W) (hW : ∀ b, W b → ¬ FootNA (L := L) Z s b) + (hal : ∀ b, FootNA (L := L) Z s b → Aligned b) (G' : MdbGhostA) (HS : SpecSlotA σ σ') + (hst : StateRelA Z.th (L.mem s') σ') (hcdt : CdtRelA st Z.th s' σ' G') + (hcap : ∀ y, IsSlotA Z.th y → CapObjOkA Z (capAtA (L.mem s') y)) : + JAtZAX (L := L) AR { Z with G := G' } s' a σ' m := + J.of_slot G' ⟨hw.1, hw.2.1, fun b hb => hw.2.2 b (hal b hb) fun e => hW b e hb⟩ HS hst hcdt hcap + +end slotw + + +end + +/-! ## The spec invariant's frame -/ + +/-- A TCB or an SC. -/ +def SchedObjA (ko : Fl4SpecA64.KernelObject) : Prop := (∃ x, ko = .tcb x) ∨ ∃ y k, ko = .schedContext y k + +/-- **A spec update that keeps every TCB and SC and the scheduler's names and queues** (module docstring). -/ +structure SchedSameA (σ σ' : Fl4SpecA64.State Unit) : Prop where + kh : ∀ p ko, SchedObjA ko → (σ'.kheap p = some ko ↔ σ.kheap p = some ko) + cur : σ'.curThread = σ.curThread + idle : σ'.idleThread = σ.idleThread + csc : σ'.curSc = σ.curSc + rq : σ'.readyQueues = σ.readyQueues + rls : σ'.releaseQueue = σ.releaseQueue + /-- (record edit #8b-C2, item 11) The time and the consumed time are kept. -/ + time : σ'.curTime = σ.curTime + cons : σ'.consumedTime = σ.consumedTime + +theorem SchedSameA.tcb {σ σ' : Fl4SpecA64.State Unit} (H : SchedSameA σ σ') {p : Fl4SpecA64.ObjRef} + {x : Fl4SpecA64.Tcb} : σ'.kheap p = some (.tcb x) ↔ σ.kheap p = some (.tcb x) := H.kh p _ (Or.inl ⟨x, rfl⟩) + +theorem SchedSameA.sc {σ σ' : Fl4SpecA64.State Unit} (H : SchedSameA σ σ') {p : Fl4SpecA64.ObjRef} + {y : Fl4SpecA64.SchedContext} {k : Nat} : + σ'.kheap p = some (.schedContext y k) ↔ σ.kheap p = some (.schedContext y k) := H.kh p _ (Or.inr ⟨y, k, rfl⟩) + +/-- **`SInvXA` kept by a spec update that keeps every TCB and SC** (module docstring; gen 4: given the IRQ node's +`SpecIrqA` after). -/ +theorem SInvXA.of_spec {σ σ' : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} {n : Nat} (h : SInvXA σ m n) + (H : SchedSameA σ σ') (hac : σ'.schedulerAction = σ.schedulerAction) (hI : SpecIrqA σ') : SInvXA σ' m n where + mc := h.mc + view := by unfold Fl4SpecA64.McView; rw [H.idle]; exact h.view + curTcb := by obtain ⟨x, e⟩ := h.curTcb; exact ⟨x, by rw [H.cur]; exact H.tcb.2 e⟩ + idleTcb := by obtain ⟨x, e, e2⟩ := h.idleTcb; exact ⟨x, by rw [H.idle]; exact H.tcb.2 e, e2⟩ + onlyIdle t x e e2 := h.onlyIdle t x (H.tcb.1 e) e2 + idleSc := by + obtain ⟨x, y, k, e1, e2, e3, e4⟩ := h.idleSc + exact ⟨x, y, k, by rw [H.idle]; exact H.tcb.2 e1, e2, H.sc.2 e3, by rw [H.idle]; exact e4⟩ + curSc := by obtain ⟨y, k, e⟩ := h.curSc; exact ⟨y, k, by rw [H.csc]; exact H.sc.2 e⟩ + ctQ ha d p := by rw [H.cur, H.rq]; exact h.ctQ (hac.symm.trans ha) d p + idleQ d p := by rw [H.rq]; exact h.idleQ d p + rq d p t ht := by + rw [H.rq] at ht + obtain ⟨x, e, e2⟩ := h.rq d p t ht + exact ⟨x, H.tcb.2 e, e2⟩ + rls t ht := by + rw [H.rls] at ht + obtain ⟨x, e⟩ := h.rls t ht + exact ⟨x, H.tcb.2 e⟩ + rpos p y k e a := h.rpos p y k (H.sc.1 e) a + zc := (CurFactsA.back (σ := σ) (σ' := σ') ⟨h.zc, h.rlsRun, h.curScReady⟩ (fun t ht => by rw [H.rls] at ht; exact ht) H.cons H.csc H.time + (fun t x' e => ⟨x', H.tcb.1 e, rfl⟩) (fun t x e _ => ⟨x, H.tcb.2 e, id⟩) + (fun y k e => ⟨y, H.sc.1 e, fun a => ⟨a, id⟩⟩)).1 + rlsRun := (CurFactsA.back (σ := σ) (σ' := σ') ⟨h.zc, h.rlsRun, h.curScReady⟩ (fun t ht => by rw [H.rls] at ht; exact ht) H.cons H.csc H.time + (fun t x' e => ⟨x', H.tcb.1 e, rfl⟩) (fun t x e _ => ⟨x, H.tcb.2 e, id⟩) + (fun y k e => ⟨y, H.sc.1 e, fun a => ⟨a, id⟩⟩)).2.1 + faultSt t x e f := h.faultSt t x (H.tcb.1 e) f + curScReady := (CurFactsA.back (σ := σ) (σ' := σ') ⟨h.zc, h.rlsRun, h.curScReady⟩ (fun t ht => by rw [H.rls] at ht; exact ht) H.cons H.csc H.time + (fun t x' e => ⟨x', H.tcb.1 e, rfl⟩) (fun t x e _ => ⟨x, H.tcb.2 e, id⟩) + (fun y k e => ⟨y, H.sc.1 e, fun a => ⟨a, id⟩⟩)).2.2 + irqN := hI + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/FrontEndExtraA.lean b/proofs/Fl4ProofsA64/P3/FrontEndExtraA.lean new file mode 100644 index 000000000..213c44a15 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/FrontEndExtraA.lean @@ -0,0 +1,1049 @@ +/- +P3 (aarch64, A64-P3 S3 gen 16, ruling 17:32Z item 1): **the invocation's extra caps, label and argument words** — +the A64 port of x86's `P3/FrontEndExtra` (with the pieces of x86's `IpcGrant` / `Lookup` / `EpXferSpec` / +`IpcFaultReply` it stands on, which the A64 line did not have yet). + +`handle_invocation` reads the message info (`msg_info`), the IPC buffer and the extra caps (`lookup_extra_caps`); the +spec's `handle_invocation` reads `getMessageInfo`, `lookupIpcBuffer`, `lookupExtraCaps` and `getMrs`. + +* Spec side: `loadWordOffs_eqA`, `mapM_loadA`, `getExtraCptrs_someA` (the buffer words, aligned), `getMessageInfo_eqA` + (`asUser getRegister` keeps the state), `getMrs_eqA` / `specMrsA` (x86 `getMrs_eq` / `specMrs`), `fl4GetMr_eqA`, + the `handleE` / `mapME` one-outcome lemmas. +* **`lookup_slot_refinesA`** (x86 `lookup_slot_refines`): FL4's `lookup_slot` of a TCB of `th` against l4v + `lookup_slot_for_thread` at `to_bl cptr` (`resolve_address_bits_refinesA` from the TCB's CTable slot). +* **`ExtraOKA`** (x86 `ExtraOK`): FL4's extra-slot array against the spec's extra caps, slot by slot (`SrcOKA`: a CNode + slot of `th` whose cap is `CapRelA`-related to the spec's), zero past the end. **Parametric in `CapRelA`**: nothing + here matches on a cap constructor (in particular not on `ExtArchCap`), so the Stage-2 frame caps of ExtVsA do not + interact. +* **`lookup_extra_relA`** (x86 `lookup_extra_rel`): `lookup_extra_caps` against `lookupExtraCaps`: both succeed with + `ExtraOKA`, or both fault with the same cap fault (`CapFaultCorrA`). +* **`ExtraOKA.rel1` → `ExtraRelA`, `.rel2` → `ExtraRel2A`, `.relS` → `ExtraSRelA`, `.xc` → `XcRelA`** (x86 `ExtraOK.rel1` + …; `ExtraSRelA` / `XcRelA` are the A64 forms of x86's `ExtraSRel` / `XcRel`), so the decode tops' extra-cap case + hypotheses (`cnode_decode_refinesA`'s `ExtraRel2A`) follow from the lookups. +* **`JAtZA.frontJ`** (label words agree; both lookups succeeding give `ExtraOKA`), **`JAtZA.extraJ`** (both succeed or + both fault, `CapFaultCorrA`), **`JAtZA.argsJ`** (the spec's `getMrs` words are FL4's `syscall_arg`s, `msgLenA` of + them, and the first five are `fl4GetMr`'s: `ArgRelA`), so `cnode_decode_refinesA`'s `hargs` follows. + +Case hypotheses (not named premises): x86 reads the current thread's registers and IPC buffer through its IPC relation +(`XTopAt.irelK`, `lookup_buf_full`, `BufCover`); A64's `JAtZA` has no IPC relation yet (S4), so the `J` theorems take +the two views explicitly: **`RegViewA`** (the current TCB's message-info and message registers are the spec TCB's) and +**`BufViewA`** (FL4's buffer word `buf` is 0 exactly when the spec has no buffer, else its 128 words are the spec's +aligned buffer words). When the IPC relation enters `JAtZA` both become projections of it. The conversions take +`PlaceA` (for `IsSlotA`, and since S3 gen 18 for "no slot of `th` is at address 0", `PlaceA.slot_ne0`, as x86 from +`Placed`). No named premise; no existing statement changed. New module. + +**Decode-site hypotheses, for the premise accounting** (gen 17, ruling 17:58Z; AGENTS.md "named premises"): these are +not named premises of the refinement but case hypotheses of the decode-site theorems, listed here so the accounting is +transparent. +* **`RegViewA`** [J-side, decode-site] (`JAtZA.curViewA`, `frontJ`, `extraJ`, `argsJ`, and P3/CnodeFrontA's + `cnode_decode_frontA`): x86's `XTopAt.irelK` register half. Discharged when S4's unified IPC relation enters + `JAtZA` (a TCB's message-info / message registers are the spec TCB's `userRegs`). +* **`BufViewA`** [J-side, decode-site] (`lookup_extra_relA`, `frontJ`, `extraJ`, `argsJ`, `syscall_arg_relA`, + `cnode_decode_frontA`): x86's `lookup_buf_full` / `BufCover`. Discharged when S4's IPC relation enters `JAtZA` (the + IPC-buffer words of every TCB). +* (gen 18, ruling 18:13Z item 2) gen 16's **`hnz`** is gone: `PlaceA.nz` (no object of `th` at 0) gives it + (`PlaceA.slot_ne0`); `srcOK_factsA`, `ExtraOKA.rel1` / `rel2` / `relS` / `xc` and `cnode_decode_frontA` no longer + take it (statements changed). +-/ +import Fl4ProofsA64.P3.CnodeRotA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## The spec's monad -/ + +section nd +variable {σ α β ε : Type} + +theorem nd_gets_bindA (g : σ → α) (f : α → Fl4Spec.NonDet σ β) (s : σ) : + (Fl4Spec.NonDet.gets g >>= f) s = f (g s) s := + nd_bind_singleA (m := Fl4Spec.NonDet.gets g) (s := s) (a := g s) rfl f + +theorem nd_modify_bindA (g : σ → σ) (f : Unit → Fl4Spec.NonDet σ β) (s : σ) : + (Fl4Spec.NonDet.modify g >>= f) s = f () (g s) := + nd_bind_oneA (m := Fl4Spec.NonDet.modify g) (s := s) (s' := g s) (a := ()) rfl f + +theorem assert_trueA {P : Prop} (h : P) (s : σ) : Fl4Spec.NonDet.assert P s = ({((), s)}, False) := by + simp [Fl4Spec.NonDet.assert, h] + +theorem selectF_oneA (a : α) (s : σ) : + Fl4Spec.NonDet.selectF (σ := σ) (({a} : Set α), False) s = ({(a, s)}, False) := by + unfold Fl4Spec.NonDet.selectF + ext ⟨x, y⟩ <;> simp [and_comm] + +theorem run_handleE_okA {ε' : Type} {m : Fl4Spec.NonDetE σ ε α} {h : ε → Fl4Spec.NonDetE σ ε' α} {s : σ} {a : α} + (hm : m.run s = ({(.ok a, s)}, False)) : (Fl4Spec.NonDetE.handleE m h).run s = ({(.ok a, s)}, False) := by + unfold Fl4Spec.NonDetE.handleE + rw [ExceptT.run_mk] + show (ExceptT.run m >>= _) s = _ + rw [nd_bind_singleA hm]; rfl + +theorem run_handleE_errA {ε' : Type} {m : Fl4Spec.NonDetE σ ε α} {h : ε → Fl4Spec.NonDetE σ ε' α} {s : σ} {e : ε} + (hm : m.run s = ({(.error e, s)}, False)) : (Fl4Spec.NonDetE.handleE m h).run s = (h e).run s := by + unfold Fl4Spec.NonDetE.handleE + rw [ExceptT.run_mk] + show (ExceptT.run m >>= _) s = _ + rw [nd_bind_singleA hm] + +variable {f : α → Fl4Spec.NonDetE σ ε β} {s : σ} + +theorem mapME_cons_okA {a : α} {xs : List α} {b : β} {bs : List β} + (hf : (f a).run s = ({(.ok b, s)}, False)) (hr : (Fl4Spec.NonDetE.mapME f xs).run s = ({(.ok bs, s)}, False)) : + (Fl4Spec.NonDetE.mapME f (a :: xs)).run s = ({(.ok (b :: bs), s)}, False) := by + unfold Fl4Spec.NonDetE.mapME + rw [run_bind_okA hf, run_bind_okA hr]; rfl + +theorem mapME_cons_tail_errA {a : α} {xs : List α} {b : β} {e : ε} + (hf : (f a).run s = ({(.ok b, s)}, False)) (hr : (Fl4Spec.NonDetE.mapME f xs).run s = ({(.error e, s)}, False)) : + (Fl4Spec.NonDetE.mapME f (a :: xs)).run s = ({(.error e, s)}, False) := by + unfold Fl4Spec.NonDetE.mapME + rw [run_bind_okA hf, run_bind_errorA hr] + +theorem mapME_cons_errA {a : α} {xs : List α} {e : ε} (hf : (f a).run s = ({(.error e, s)}, False)) : + (Fl4Spec.NonDetE.mapME f (a :: xs)).run s = ({(.error e, s)}, False) := by + unfold Fl4Spec.NonDetE.mapME + rw [run_bind_errorA hf] + +end nd + +/-! ## The spec's reads -/ + +section spec +open Fl4SpecA64 + +/-- The word l4v's `loadWord` reads at `p` (x86 `loadWordAt`). -/ +def loadWordAtA (um : Word → Word8) (p : Word) : Word := + wordRcat ((intUpto 0 7).map fun i => um (p + (7 - BitVec.ofInt 64 i))) + +/-- Word `x` of the buffer at `p` (x86 `bufAddr`). -/ +def bufAddrA (p : Word) (x : Nat) : Word := p + BitVec.ofNat 64 (x * wordSize) + +theorem aligned_maskA {a : Word} (h : a.toNat % 8 = 0) : a &&& mask 3 = 0 := by + apply BitVec.eq_of_toNat_eq + rw [BitVec.toNat_and, show (mask 3 : Word).toNat = 2 ^ 3 - 1 from by decide, Nat.and_two_pow_sub_one_eq_mod] + simpa using h + +theorem loadWord_eqA (ms : MachineState) (a : Word) (h : a.toNat % 8 = 0) : + loadWord a ms = ({(loadWordAtA ms.underlyingMemory a, ms)}, False) := by + unfold loadWord + rw [nd_gets_bindA, nd_bind_singleA (assert_trueA (aligned_maskA h) ms)] + rfl + +theorem loadWordOffs_eqA (σ : State Unit) (p : ObjRef) (x : Nat) (h : (bufAddrA p x).toNat % 8 = 0) : + loadWordOffs (Z := Unit) p x σ = ({(loadWordAtA σ.machineState.underlyingMemory (bufAddrA p x), σ)}, False) := by + unfold loadWordOffs doMachineOp + unfold bufAddrA at h ⊢ + rw [nd_gets_bindA, loadWord_eqA _ _ h, nd_bind_singleA (selectF_oneA _ σ)] + dsimp only + rw [nd_modify_bindA] + rfl + +theorem mapM_loadA (sb : Word) : ∀ (xs : List Nat) (σ : State Unit), (∀ x ∈ xs, (bufAddrA sb x).toNat % 8 = 0) → + Fl4Spec.NonDet.mapM (loadWordOffs (Z := Unit) sb) xs σ = + ({(xs.map fun k => loadWordAtA σ.machineState.underlyingMemory (bufAddrA sb k), σ)}, False) + | [], σ, _ => rfl + | x :: xs, σ, h => by + unfold Fl4Spec.NonDet.mapM + rw [nd_bind_singleA' (loadWordOffs_eqA σ sb x (h x (by simp))), + nd_bind_singleA' (mapM_loadA sb xs σ fun y hy => h y (by simp [hy]))] + rfl + +/-- The spec's extra cap pointers: words `122 ..` of the sender's buffer (x86 `xCptrs`). -/ +def xCptrsA (um : Word → Word8) (sb : Word) (n : Nat) : List CapRef := + (List.range' 122 n).map (fun k => dataToCptr (loadWordAtA um (bufAddrA sb k))) + +/-- The spec's lookup of one extra cap (x86 `xLook`). -/ +abbrev xLookA (t : ObjRef) (cptr : CapRef) : Fl4Spec.NonDetE (State Unit) Fault (Cap × CslotPtr) := + capFaultOnFailure (Z := Unit) (ofBl cptr) false (lookupCapAndSlot t cptr) + +theorem getExtraCptrs_someA {σ : State Unit} {sb : Word} {mi : MessageInfo} (h3 : unat mi.miExtraCaps ≤ 3) + (hal : ∀ k, k < 128 → (bufAddrA sb k).toNat % 8 = 0) : + getExtraCptrs (Z := Unit) (some sb) mi σ = + ({(xCptrsA σ.machineState.underlyingMemory sb (unat mi.miExtraCaps), σ)}, False) := by + unfold getExtraCptrs + dsimp only + rw [show bufferCptrIndex + unat mi.miExtraCaps - bufferCptrIndex = unat mi.miExtraCaps by omega] + unfold Fl4Spec.NonDet.liftM + rw [nd_bind_singleA' (mapM_loadA sb _ σ fun k hk => hal k (by + simp [List.mem_range'] at hk; unfold bufferCptrIndex msgMaxLength at hk; omega))] + simp [xCptrsA, bufferCptrIndex, msgMaxLength, Fl4Spec.NonDet.pure] + +theorem getTcb_oneA {σ : State Unit} {t : ObjRef} {x : Tcb} (hk : σ.kheap t = some (.tcb x)) : + Fl4Spec.NonDet.getsThe (getTcb (Z := Unit) t) σ = ({(x, σ)}, False) := by + simp [Fl4Spec.NonDet.getsThe, getTcb, readObject, hk, Fl4Spec.NonDet.assertOpt]; rfl + +theorem threadGet_eqA {γ : Type} {σ : State Unit} {t : ObjRef} {x : Tcb} (f : Tcb → γ) + (hk : σ.kheap t = some (.tcb x)) : threadGet (Z := Unit) f t σ = ({(f x, σ)}, False) := by + unfold threadGet + rw [nd_bind_singleA (getTcb_oneA hk)]; rfl + +theorem setObject_selfA {σ : State Unit} {t : ObjRef} {o : KernelObject} (hk : σ.kheap t = some o) : + setObject (Z := Unit) t o σ = ({((), σ)}, False) := by + have ho : getObject (Z := Unit) t σ = ({(o, σ)}, False) := by + show Fl4Spec.NonDet.assertOpt (σ.kheap t) σ = _ + rw [hk]; rfl + unfold setObject + rw [nd_bind_singleA ho, nd_bind_singleA (assert_trueA (P := aType o = aType o) rfl σ), + nd_bind_singleA (show Fl4Spec.NonDet.get σ = ({(σ, σ)}, False) from rfl)] + show ({((), { σ with kheap := Function.update σ.kheap t (some o) })}, False) = _ + rw [← hk, Function.update_eq_self] + +theorem asUser_getRegister_eqA {σ : State Unit} {t : ObjRef} {x : Tcb} (hk : σ.kheap t = some (.tcb x)) + (r : Register) : asUser (Z := Unit) t (getRegister r) σ = ({(x.tcbArch.tcbContext.userRegs r, σ)}, False) := by + unfold asUser + rw [nd_bind_singleA (getTcb_oneA hk), nd_pure_bindA] + rw [nd_bind_singleA (show Fl4Spec.NonDet.selectF (σ := State Unit) (getRegister r (archTcbContextGet x.tcbArch)) σ = + ({((x.tcbArch.tcbContext.userRegs r, archTcbContextGet x.tcbArch), σ)}, False) by + simp only [Fl4Spec.NonDet.selectF, getRegister, Fl4Spec.NonDet.gets] + ext ⟨a, b⟩ <;> simp [archTcbContextGet])] + dsimp only + rw [nd_pure_bindA] + have hx : ({ x with tcbArch := archTcbContextSet (archTcbContextGet x.tcbArch) x.tcbArch } : Tcb) = x := rfl + rw [hx, nd_bind_singleA (setObject_selfA hk)] + rfl + +theorem getMessageInfo_eqA {σ : State Unit} {t : ObjRef} {x : Tcb} (hk : σ.kheap t = some (.tcb x)) : + getMessageInfo (Z := Unit) t σ = + ({(dataToMessageInfo (x.tcbArch.tcbContext.userRegs msg_info_register), σ)}, False) := by + unfold getMessageInfo + rw [nd_bind_singleA (asUser_getRegister_eqA hk _)]; rfl + +/-- The spec's argument word `i` (`fl4GetMr`, and `getMrs`' entry `i`; x86 `specArg`). -/ +def specArgA (regs : Register → Word) (sbo : Option ObjRef) (um : Word → Word8) (i : Nat) : Word := + if i < 4 then regs (msgRegisters.getD i .x5) else match sbo with + | none => 0 + | some sb => loadWordAtA um (bufAddrA sb (i + 1)) + +theorem fl4GetMr_eqA {σ : State Unit} {t : ObjRef} {x : Tcb} (hk : σ.kheap t = some (.tcb x)) + (sbo : Option ObjRef) (hal : ∀ sb, sbo = some sb → ∀ k, k < 128 → (bufAddrA sb k).toNat % 8 = 0) + (i : Nat) (hi : i < 127) : + fl4GetMr (Z := Unit) t sbo i σ = + ({(specArgA x.tcbArch.tcbContext.userRegs sbo σ.machineState.underlyingMemory i, σ)}, False) := by + unfold fl4GetMr + rw [nd_bind_singleA (threadGet_eqA _ hk)] + try dsimp only + by_cases h4 : i < msgRegisters.length + · rw [dif_pos h4] + have : i < 4 := by simpa [msgRegisters, Register.x2ToX5] using h4 + unfold specArgA; rw [if_pos this, List.getD_eq_getElem _ _ h4]; rfl + · rw [dif_neg h4] + have : ¬ i < 4 := by simpa [msgRegisters, Register.x2ToX5] using h4 + unfold specArgA; rw [if_neg this] + cases sbo with + | none => rfl + | some sb => exact loadWordOffs_eqA σ sb (i + 1) (hal sb rfl (i + 1) (by omega)) + +/-- l4v `get_mrs`' result (x86 `specMrs`): the message registers, then the buffer words 5..120, the first `len`. -/ +def specMrsA (regs : Register → Word) (sbo : Option ObjRef) (um : Word → Word8) (len : Nat) : List Word := + ((msg_registers.map regs) ++ (match sbo with + | none => [] + | some sb => (List.range' 5 116).map fun k => loadWordAtA um (bufAddrA sb k))).take len + +theorem getMrs_eqA {σ : State Unit} {t : ObjRef} {x : Tcb} (hk : σ.kheap t = some (.tcb x)) + (sbo : Option ObjRef) (hal : ∀ sb, sbo = some sb → ∀ k, k < 128 → (bufAddrA sb k).toNat % 8 = 0) + (mi : MessageInfo) : + getMrs (Z := Unit) t sbo mi σ = + ({(specMrsA x.tcbArch.tcbContext.userRegs sbo σ.machineState.underlyingMemory (unat mi.miLength), σ)}, + False) := by + unfold getMrs + rw [nd_bind_singleA (threadGet_eqA _ hk), nd_pure_bindA] + cases sbo with + | none => dsimp only; rw [nd_pure_bindA]; rfl + | some sb => + dsimp only + rw [nd_bind_singleA (mapM_loadA sb _ σ fun k hk' => hal sb rfl k (by + simp [msg_registers, msgRegisters, Register.x2ToX5, msgMaxLength] at hk'; omega))] + rfl + +theorem specMrsA_lo (regs : Register → Word) (sbo : Option ObjRef) (um : Word → Word8) (len i : Nat) (hi : i < 4) + (hil : i < len) : i < (specMrsA regs sbo um len).length ∧ + (specMrsA regs sbo um len)[i]! = regs (msgRegisters.getD i .x5) := by + have hlt : i < (specMrsA regs sbo um len).length := by + unfold specMrsA; rw [List.length_take]; simp [msg_registers, msgRegisters, Register.x2ToX5]; omega + refine ⟨hlt, ?_⟩ + rw [getElem!_pos _ i hlt] + unfold specMrsA + rcases (show i = 0 ∨ i = 1 ∨ i = 2 ∨ i = 3 by omega) with rfl | rfl | rfl | rfl <;> + simp [List.getElem_append_left, msg_registers, msgRegisters, Register.x2ToX5] + +theorem specMrsA_hi (regs : Register → Word) (sb : ObjRef) (um : Word → Word8) (len i : Nat) (hi : 4 ≤ i) + (hil : i < len) (h120 : i < 120) : i < (specMrsA regs (some sb) um len).length ∧ + (specMrsA regs (some sb) um len)[i]! = loadWordAtA um (bufAddrA sb (i + 1)) := by + have hlt : i < (specMrsA regs (some sb) um len).length := by + unfold specMrsA; rw [List.length_take]; simp [msg_registers, msgRegisters, Register.x2ToX5]; omega + refine ⟨hlt, ?_⟩ + rw [getElem!_pos _ i hlt] + unfold specMrsA + simp only [List.getElem_take, List.getElem_append, msg_registers, msgRegisters, Register.x2ToX5, List.length_map, + List.length_cons, List.length_nil] + rw [dif_neg (by omega)] + simp only [List.getElem_map, List.getElem_range'] + congr 2 + omega + +theorem specMrsA_len_le (regs : Register → Word) (sbo : Option ObjRef) (um : Word → Word8) (len : Nat) : + (specMrsA regs sbo um len).length ≤ len := by + unfold specMrsA; rw [List.length_take]; omega + +theorem specMrsA_len_none (regs : Register → Word) (um : Word → Word8) (len : Nat) : + (specMrsA regs none um len).length ≤ 4 := by + unfold specMrsA; rw [List.length_take]; simp [msg_registers, msgRegisters, Register.x2ToX5] + +theorem specMrsA_length (regs : Register → Word) (sbo : Option ObjRef) (um : Word → Word8) (len : Nat) : + (specMrsA regs sbo um len).length = min len (match sbo with | none => 4 | some _ => 120) := by + unfold specMrsA + cases sbo <;> simp [msg_registers, msgRegisters, Register.x2ToX5] <;> omega + +theorem specMrsA_get (regs : Register → Word) (sbo : Option ObjRef) (um : Word → Word8) (len i : Nat) + (hl : len ≤ 120) (hi : i < (specMrsA regs sbo um len).length) : + (specMrsA regs sbo um len)[i]! = specArgA regs sbo um i := by + have hil : i < len := lt_of_lt_of_le hi (specMrsA_len_le _ _ _ _) + unfold specArgA + by_cases h4 : i < 4 + · rw [if_pos h4]; exact (specMrsA_lo regs sbo um len i h4 hil).2 + · rw [if_neg h4] + cases sbo with + | none => exact absurd (lt_of_lt_of_le hi (specMrsA_len_none _ _ _)) h4 + | some sb => exact (specMrsA_hi regs sb um len i (by omega) hil (by omega)).2 + +theorem mask7A : (mask 7 : BitVec 64) = 127#64 := by decide +theorem mask2A : (mask 2 : BitVec 64) = 3#64 := by decide +theorem mask52A : (mask msgLabelBits : BitVec 64) = 4503599627370495#64 := by + unfold msgLabelBits; decide + +/-- The label field of a message-info word has its 52 bits (x86 `label_mask`). -/ +theorem label_maskA (w : Word) : (w >>> 12) &&& mask msgLabelBits = w >>> 12 := by + rw [mask52A] + apply BitVec.eq_of_toNat_eq + rw [BitVec.toNat_and, show (4503599627370495#64).toNat = 2 ^ 52 - 1 from rfl, Nat.and_two_pow_sub_one_eq_mod, + BitVec.toNat_ushiftRight] + refine Nat.mod_eq_of_lt ?_ + have := w.isLt + rw [Nat.shiftRight_eq_div_pow] + omega + +theorem extra_le3A (w : Word) : ((dataToMessageInfo w).miExtraCaps).toNat ≤ 3 := by + show ((w >>> 7) &&& mask 2).toNat ≤ 3 + rw [BitVec.toNat_and] + exact (Nat.and_le_right).trans (by decide) + +theorem ofBl_toBlA (w : BitVec 64) : (ofBl (toBl w) : BitVec 64) = w := by + rw [toBl_bitsOfA, ofBl_bitsOfA _ _ (le_refl _), Nat.mod_eq_of_lt w.isLt]; simp + +/-- `lookup_slot_for_thread` from the TCB's CTable (one outcome). -/ +theorem lookupSlotForThread_eqA {σ : State Unit} {t : ObjRef} {x : Tcb} (hk : σ.kheap t = some (.tcb x)) + (cref : CapRef) : (lookupSlotForThread (Z := Unit) t cref).run σ = (resolveAddressBits' (Z := Unit) (x.tcbCtable, cref)).run σ := by + unfold lookupSlotForThread resolveAddressBits + rw [run_bind_okA (run_liftEA (getTcb_oneA hk))] + +theorem lcs_okA {σ : State Unit} {t : ObjRef} {cptr : CapRef} {sl : CslotPtr} {rest : CapRef} {a : Cap} (x : ObjRef) + (h : (lookupSlotForThread (Z := Unit) t cptr).run σ = ({(.ok (sl, rest), σ)}, False)) + (hg : getCap (Z := Unit) sl σ = ({(a, σ)}, False)) : + (capFaultOnFailure (Z := Unit) x false (lookupCapAndSlot t cptr)).run σ = ({(.ok (a, sl), σ)}, False) := by + unfold capFaultOnFailure + apply run_handleE_okA + unfold lookupCapAndSlot + rw [run_bind_okA h] + dsimp only + rw [run_bind_okA (run_liftEA hg)]; rfl + +theorem lcs_errA {σ : State Unit} {t : ObjRef} {cptr : CapRef} {lf : LookupFailure} (x : ObjRef) + (h : (lookupSlotForThread (Z := Unit) t cptr).run σ = ({(.error lf, σ)}, False)) : + (capFaultOnFailure (Z := Unit) x false (lookupCapAndSlot t cptr)).run σ = + ({(.error (.capFault x false lf), σ)}, False) := by + unfold capFaultOnFailure + rw [run_handleE_errA] + · rfl + unfold lookupCapAndSlot + rw [run_bind_errorA h] + +end spec + +/-! ## FL4's reads -/ + +section fl4 +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- FL4's register word `r` of TCB `t` (`obj::Tcb::reg`). -/ +def regWA (t r : U64) : U64 := wa (wa t layout.tcb.REGS) (core.num.U64.wrapping_mul r 8#u64) + +/-- FL4's IPC-buffer word `k` at `buf` (`msg::buf_get`; x86 `fl4BufA`). -/ +def bufWA (buf k : U64) : U64 := wa buf (core.num.U64.wrapping_mul k 8#u64) + +theorem tcb_reg_eqA (s : S) (t r : U64) : obj.Tcb.reg st t s r = ok (L.mem s (regWA t r)) := by + unfold obj.Tcb.reg regWA; simp only [lift, bind_ok, L.read_eq] + +theorem buf_get_eqA (s : S) (buf k : U64) : msg.buf_get st s buf k = ok (L.mem s (bufWA buf k)) := by + unfold msg.buf_get bufWA; simp only [lift, bind_ok, L.read_eq] + +theorem buf_caps_eqA : msg.BUF_CAPS = ok 122#u64 := by + unfold msg.BUF_CAPS msg.BUF_USER_DATA msg.MSG_MAX_LENGTH + obtain ⟨z, hz, hzv⟩ := spec_existsA (U64.add_spec (x := 1#u64) (y := 120#u64) (by scalar_tac)) + rw [hz]; simp only [bind_ok] + obtain ⟨z2, hz2, hzv2⟩ := spec_existsA (U64.add_spec (x := z) (y := 1#u64) (by scalar_tac)) + rw [hz2] + congr 1 + apply UScalar.eq_of_val_eq + rw [hzv2, hzv]; rfl + +theorem tcb_slot0A (t : U64) : obj.Tcb.slot t layout.tcb.SLOT_CTABLE = ok t := by + unfold obj.Tcb.slot + simp only [lift, bind_ok] + congr 1 + apply UScalar.eq_of_val_eq + simp [core.num.U64.wrapping_add_val_eq, core.num.U64.wrapping_mul_val_eq, layout.tcb.SLOT_CTABLE, U64.size_def, + U64.numBits] <;> scalar_tac + +/-- `msg_info`: the message info, as l4v's `data_to_message_info` (x86 `msg_info_spec`). -/ +theorem msg_info_specA (s : S) (t : U64) : + msg.msg_info st s t ⦃ mi => mi.label.bv = (L.mem s (regWA t layout.reg.MSG_INFO)).bv >>> 12 ∧ + mi.length.bv = (Fl4SpecA64.dataToMessageInfo (L.mem s (regWA t layout.reg.MSG_INFO)).bv).miLength ∧ + mi.extra_caps.bv = (Fl4SpecA64.dataToMessageInfo (L.mem s (regWA t layout.reg.MSG_INFO)).bv).miExtraCaps ∧ + mi.length.val ≤ 120 ⦄ := by + unfold msg.msg_info + rw [tcb_reg_eqA, bind_ok] + unfold msginfo.MessageInfo.from_word_clamped msginfo.MessageInfo.from_word + generalize L.mem s (regWA t layout.reg.MSG_INFO) = w + step as ⟨i, hi1, hi2⟩ + step as ⟨i1, hi11, hi12⟩ + step as ⟨i2, hi2', hi2b⟩ + step as ⟨i3, hi31, hi32⟩ + step as ⟨i4, hi4, hi4b⟩ + step as ⟨i5, hi5, hi5b⟩ + have hl : i5.bv = w.bv &&& Fl4SpecA64.mask 7 := by rw [mask7A]; exact hi5b + have hx : i4.bv = (Fl4SpecA64.dataToMessageInfo w.bv).miExtraCaps := by + show _ = (w.bv >>> 7) &&& Fl4SpecA64.mask 2 + rw [mask2A, ← hi32]; exact hi4b + have hmax : fl4_abi.consts.MSG_MAX_LENGTH.val = 120 := by unfold fl4_abi.consts.MSG_MAX_LENGTH; rfl + have hspecL : (Fl4SpecA64.dataToMessageInfo w.bv).miLength = if i5.bv > 120 then 120 else i5.bv := by + show (let v := _; if v > 120 then 120 else v) = _ + simp only [hl] + by_cases hc : i5 > fl4_abi.consts.MSG_MAX_LENGTH + · simp only [hc, ↓reduceIte, WP.spec_ok] + have hc' : i5.bv > 120 := by + rw [gt_iff_val, hmax] at hc + show (120 : BitVec 64).toNat < i5.bv.toNat + simpa using hc + refine ⟨hi2, ?_, hx, by rw [hmax]⟩ + rw [hspecL, if_pos hc']; unfold fl4_abi.consts.MSG_MAX_LENGTH; rfl + · simp only [hc, ↓reduceIte, WP.spec_ok] + have hc' : ¬ i5.bv > 120 := by + rw [gt_iff_val, hmax] at hc + show ¬ (120 : BitVec 64).toNat < i5.bv.toNat + simpa using hc + refine ⟨hi2, ?_, hx, ?_⟩ + · rw [hspecL, if_neg hc'] + · rw [gt_iff_val, hmax] at hc; omega + +end fl4 + +/-! ## The thread's CSpace lookup -/ + +section look +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +theorem tcbCnodeMap_ctableA (x : Fl4SpecA64.Tcb) : + Fl4SpecA64.tcbCnodeMap x (Fl4SpecA64.tcbCnodeIndex 0) = some x.tcbCtable := by + simp [Fl4SpecA64.tcbCnodeMap, tcbIdx_eq, Fl4SpecA64.tcbStage2Slot, Fl4SpecA64.tcbHighVSpaceSlot] + +/-- The root CNode cap of a TCB of `th` (x86 `StateRel.ctable`): FL4's CTable slot is the TCB's address. -/ +theorem StateRelA.ctableA {th : TypedHeapA} {mem : U64 → U64} {σ : Fl4SpecA64.State Unit} + (hR : StateRelA th mem σ) (hV : SlotsValidA th mem) {t : U64} (ht : th t = some .tcb) : + ∃ x, σ.kheap (specPtr t) = some (.tcb x) ∧ CapRelA (capAtA mem t) x.tcbCtable ∧ CapValidA th (capAtA mem t) := by + obtain ⟨x, hk, hs⟩ := hR.tcb t ht + have e : slotAddrA (specPtr t) (BitVec.ofNat 64 0) = t := by + rw [slotAddrA_sp]; apply UScalar.eq_of_val_eq; simp + obtain ⟨a, ha, hca⟩ := hs 0 (by decide) + rw [tcbCnodeMap_ctableA] at ha + cases ha + rw [e] at hca + refine ⟨x, hk, hca, hV _ ⟨t, Or.inr ⟨ht, 0, by decide, e.symm⟩⟩⟩ + +/-- **FL4's `lookup_slot` refines l4v `lookup_slot_for_thread`** at `to_bl cptr`, for a TCB of `th` (x86 +`lookup_slot_refines`). -/ +theorem lookup_slot_refinesA (th : TypedHeapA) (s : S) (σ : Fl4SpecA64.State Unit) + (hR : StateRelA th (L.mem s) σ) (hV : SlotsValidA th (L.mem s)) (t : U64) (ht : th t = some .tcb) (cptr : U64) : + cspace.lookup_slot st s t cptr ⦃ r => + SingleA ((Fl4SpecA64.lookupSlotForThread (Z := Unit) (specPtr t) (Fl4SpecA64.toBl cptr.bv)).run σ) σ + (WalkRelA th (L.mem s) (capAtA (L.mem s) t) r) ⦄ := by + obtain ⟨x, hk, hc, hcv⟩ := hR.ctableA hV ht + rw [lookupSlotForThread_eqA hk, toBl_bitsOfA] + unfold cspace.lookup_slot + rw [tcb_slot0A, bind_ok, W7.R.ST.capA_eq, bind_ok] + have h := resolve_address_bits_refinesA th s σ hR hV _ _ hc hcv cptr cspace.WORD_BITS (by simp [cspace.WORD_BITS]) + have e : cspace.WORD_BITS.val = 64 := by simp [cspace.WORD_BITS] + rw [e] at h + exact h + +end look + +/-! ## The extra caps -/ + +section extra +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- Slot `j` of the extra caps (x86 `exAt`). -/ +def exAtA (extra : Aeneas.Std.Array obj.Slot 3#usize) (j : Nat) : U64 := extra.val.getD j 0#u64 + +theorem index_exAtA (extra : Aeneas.Std.Array obj.Slot 3#usize) (i : Usize) (hi : i.val < 3) : + Aeneas.Std.Array.index_usize extra i = ok (exAtA extra i.val) := by + have hl : extra.val.length = 3 := by simpa using extra.property + unfold Aeneas.Std.Array.index_usize exAtA + have : extra[i]? = some (extra.val[i.val]'(by omega)) := by + simp [Aeneas.Std.Array.getElem?_Usize_eq, List.getElem?_eq_getElem (by omega : i.val < extra.val.length)] + rw [this, List.getD_eq_getElem _ _ (by omega)] + +theorem exAtA_idx0 (e : Aeneas.Std.Array obj.Slot 3#usize) : Aeneas.Std.Array.index_usize e 0#usize = ok (exAtA e 0) := by + rw [index_exAtA e 0#usize (by simp)]; rfl + +theorem exAtA_idx1 (e : Aeneas.Std.Array obj.Slot 3#usize) : Aeneas.Std.Array.index_usize e 1#usize = ok (exAtA e 1) := by + rw [index_exAtA e 1#usize (by simp)]; rfl + +theorem exAtA_idx2 (e : Aeneas.Std.Array obj.Slot 3#usize) : Aeneas.Std.Array.index_usize e 2#usize = ok (exAtA e 2) := by + rw [index_exAtA e 2#usize (by simp)]; rfl + +theorem cast_us_valA (i : U64) (h : i.val < 3) : (UScalar.cast .Usize i).val = i.val := by + rw [UScalar.cast_val_eq] + refine Nat.mod_eq_of_lt ?_ + rw [UScalarTy.Usize_numBits_eq] + rcases System.Platform.numBits_eq with e | e <;> rw [e] <;> omega + +theorem exAtA_none (j : Nat) (hj : j < 3) : exAtA ipc.NO_EXTRA_CAPS j = 0#u64 := by + unfold exAtA ipc.NO_EXTRA_CAPS + interval_cases j <;> rfl + +theorem exAtA_set (extra : Aeneas.Std.Array obj.Slot 3#usize) (i : Usize) (x : U64) (j : Nat) : + exAtA (extra.set i x) j = if i.val = j ∧ j < 3 then x else exAtA extra j := by + have hl : extra.val.length = 3 := by simpa using extra.property + unfold exAtA + rw [Aeneas.Std.Array.set_val_eq] + by_cases h : i.val = j + · subst h + by_cases h3 : i.val < 3 + · rw [if_pos ⟨rfl, h3⟩, List.getD_eq_getElem _ _ (by simp; omega)]; simp + · rw [if_neg (fun h => h3 h.2), List.getD_eq_default _ _ (by simp; omega), + List.getD_eq_default _ _ (by omega)] + · rw [if_neg (fun h' => h h'.1)] + simp [List.getD_eq_getElem?_getD, List.getElem?_set_ne h] + +/-- A source of the extra caps (x86 `SrcOK`, parametric in `CapRelA`): a slot of a CNode of `th` whose cap is related to +the spec's. -/ +def SrcOKA (th : TypedHeapA) (m : U64 → U64) (x : U64) (y : Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr) : Prop := + ∃ ps, th ps = some (.cnode y.2.2.length) ∧ y.2.1 = specPtr ps ∧ x = cslotAddrA y.2 ∧ CapRelA (capAtA m x) y.1 + +/-- **FL4's extra-slot array and the spec's extra caps** (module docstring; x86 `ExtraOK`). -/ +def ExtraOKA (th : TypedHeapA) (m : U64 → U64) (extra : Aeneas.Std.Array obj.Slot 3#usize) + (caps : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) : Prop := + caps.length ≤ 3 ∧ (∀ j (hj : j < caps.length), SrcOKA th m (exAtA extra j) caps[j]) ∧ + ∀ j, caps.length ≤ j → j < 3 → exAtA extra j = 0#u64 + +theorem extraOK_noneA (th : TypedHeapA) (m : U64 → U64) : ExtraOKA th m ipc.NO_EXTRA_CAPS [] := + ⟨by simp, fun j hj => by simp at hj, fun j _ hj3 => exAtA_none j hj3⟩ + +/-- FL4's and the spec's cap fault (x86 `CapFaultCorr`). -/ +def CapFaultCorrA (f : error.Fault) (f' : Fl4SpecA64.Fault) : Prop := + ∃ (a : U64) (rp : Bool) (lf : error.LookupFault) (lf' : Fl4SpecA64.LookupFailure), + f = .Cap a rp lf ∧ f' = .capFault a.bv rp lf' ∧ LookupFaultRelA lf lf' + +set_option maxHeartbeats 4000000 in +/-- **`lookup_extra_caps`' loop refines the spec's lookups** of the extra caps (x86 `lookup_extra_loop`). -/ +theorem lookup_extra_loopA {th : TypedHeapA} {s : S} {σ : Fl4SpecA64.State Unit} + (hR : StateRelA th (L.mem s) σ) (hV : SlotsValidA th (L.mem s)) {t : U64} (ht : th t = some .tcb) + {sbw : U64} {sb : Fl4SpecA64.Word} + (hm : ∀ k : U64, k.val < 128 → + (L.mem s (bufWA sbw k)).bv = loadWordAtA σ.machineState.underlyingMemory (bufAddrA sb k.val)) + (n : U64) (hn : n.val ≤ 3) : + ipc.lookup_extra_caps_loop st s t sbw ipc.NO_EXTRA_CAPS n 0#u64 ⦃ r => match r with + | .Ok e => ∃ caps, (Fl4Spec.NonDetE.mapME (xLookA (specPtr t)) + (xCptrsA σ.machineState.underlyingMemory sb n.val)).run σ = ({(.ok caps, σ)}, False) ∧ + ExtraOKA th (L.mem s) e caps + | .Err f => ∃ e', (Fl4Spec.NonDetE.mapME (xLookA (specPtr t)) + (xCptrsA σ.machineState.underlyingMemory sb n.val)).run σ = ({(.error e', σ)}, False) ∧ + CapFaultCorrA f e' ⦄ := by + set cp := xCptrsA σ.machineState.underlyingMemory sb n.val with hcp + have hcpl : cp.length = n.val := by simp [hcp, xCptrsA] + unfold ipc.lookup_extra_caps_loop + refine loop.spec_decr_nat (fun x => 3 - x.2.val) + (fun x => x.2.val ≤ n.val ∧ ∃ acc : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr), acc.length = x.2.val ∧ + (∀ j (hj : j < acc.length), SrcOKA th (L.mem s) (exAtA x.1 j) acc[j]) ∧ + (∀ j, x.2.val ≤ j → j < 3 → exAtA x.1 j = 0#u64) ∧ + (∀ res, (Fl4Spec.NonDetE.mapME (xLookA (specPtr t)) (cp.drop x.2.val)).run σ = ({(.ok res, σ)}, False) → + (Fl4Spec.NonDetE.mapME (xLookA (specPtr t)) cp).run σ = ({(.ok (acc ++ res), σ)}, False)) ∧ + (∀ e, (Fl4Spec.NonDetE.mapME (xLookA (specPtr t)) (cp.drop x.2.val)).run σ = ({(.error e, σ)}, False) → + (Fl4Spec.NonDetE.mapME (xLookA (specPtr t)) cp).run σ = ({(.error e, σ)}, False))) _ _ _ (fun x hx => ?_) ?_ + · obtain ⟨arr, i⟩ := x + obtain ⟨hi, acc, hacc, hsrc, hz, hok, herr⟩ := hx + dsimp only at hi hacc hsrc hz hok herr ⊢ + unfold ipc.lookup_extra_caps_loop.body + by_cases hin : i < n + · have hin' : i.val < n.val := by scalar_tac + have hi3 : i < 3#u64 := by scalar_tac + rw [if_pos hin, if_pos hi3, buf_caps_eqA] + simp only [lift, bind_ok] + have hk : (wa 122#u64 i).val = 122 + i.val := by + simp [core.num.U64.wrapping_add_val_eq, U64.size_def, U64.numBits]; omega + have hw : (L.mem s (bufWA sbw (wa 122#u64 i))).bv = + loadWordAtA σ.machineState.underlyingMemory (bufAddrA sb (122 + i.val)) := by + rw [← hk]; exact hm _ (by rw [hk]; omega) + have hdrop : cp.drop i.val = Fl4SpecA64.dataToCptr (loadWordAtA σ.machineState.underlyingMemory + (bufAddrA sb (122 + i.val))) :: cp.drop (i.val + 1) := by + rw [List.drop_eq_getElem_cons (by omega)] + simp [hcp, xCptrsA, List.getElem_range'] + rw [buf_get_eqA, bind_ok] + refine WP.spec_bind (lookup_slot_refinesA th s σ hR hV t ht _) ?_ + rintro r ⟨e, he, hrel, hsok⟩ + have he' : (Fl4SpecA64.lookupSlotForThread (Z := Unit) (specPtr t) + (Fl4SpecA64.dataToCptr (loadWordAtA σ.machineState.underlyingMemory (bufAddrA sb (122 + i.val))))).run σ = + ({(e, σ)}, False) := by + rw [← hw]; exact he + cases r with + | Ok r1 => + cases e with + | error _ => exact hrel.elim + | ok v => + obtain ⟨sl, rest⟩ := v + obtain ⟨hsl, -⟩ := hrel + obtain ⟨p, hp1, hp, -⟩ := hsok + obtain ⟨a, hca, hg⟩ := hR.slot_capA hp1 hp + have hel := lcs_okA (Fl4SpecA64.ofBl (Fl4SpecA64.dataToCptr (loadWordAtA σ.machineState.underlyingMemory + (bufAddrA sb (122 + i.val))))) he' hg + simp only [lift, bind_ok] + obtain ⟨a', ha', ha'v⟩ := spec_existsA (Aeneas.Std.Array.update_spec arr (UScalar.cast .Usize i) r1.slot + (by have hl : arr.val.length = 3 := by simpa using arr.property + rw [cast_us_valA i (by omega)]; simp [Aeneas.Std.Array.length, hl]; omega)) + rw [ha'] + simp only [bind_ok] + obtain ⟨i4, h4e, h4v⟩ := spec_existsA (U64.add_spec (x := i) (y := 1#u64) (by scalar_tac)) + rw [h4e, bind_ok, WP.spec_ok] + have hi4 : i4.val = i.val + 1 := by simpa using h4v + subst ha'v + have hex : ∀ j, exAtA (arr.set (UScalar.cast .Usize i) r1.slot) j = + if i.val = j ∧ j < 3 then r1.slot else exAtA arr j := fun j => by + rw [exAtA_set, cast_us_valA i (by omega)] + refine ⟨⟨by rw [hi4]; omega, acc ++ [(a, sl)], by simp [hacc, hi4], fun j hj => ?_, fun j hj hj3 => ?_, + fun res hres => ?_, fun e hres => ?_⟩, by rw [hi4]; omega⟩ + · by_cases hji : j < acc.length + · rw [List.getElem_append_left hji, hex, if_neg (fun h => by omega)] + exact hsrc j hji + · have hje : j = acc.length := by simp at hj; omega + subst hje + rw [List.getElem_append_right (by omega)] + simp only [Nat.sub_self, List.getElem_cons_zero] + rw [hex, if_pos ⟨hacc.symm, by omega⟩] + exact ⟨p, hp, hp1, hsl, by rw [hsl]; exact hca⟩ + · rw [hex, if_neg (fun h => by rw [hi4] at hj; omega)] + exact hz j (by rw [hi4] at hj; omega) hj3 + · rw [hi4] at hres + have := hok ((a, sl) :: res) (by rw [hdrop]; exact mapME_cons_okA hel hres) + simpa using this + · rw [hi4] at hres + exact herr e (by rw [hdrop]; exact mapME_cons_tail_errA hel hres) + | Err lf => + cases e with + | ok _ => exact hrel.elim + | error lf' => + simp only [WP.spec_ok] + refine ⟨_, herr _ (by rw [hdrop]; exact mapME_cons_errA (lcs_errA _ he')), _, false, lf, lf', rfl, ?_, + hrel⟩ + rw [← hw] + unfold Fl4SpecA64.dataToCptr + rw [ofBl_toBlA] + · rw [if_neg hin, WP.spec_ok] + have hieq : i.val = n.val := by have : ¬ i.val < n.val := fun h => hin (by scalar_tac); omega + refine ⟨acc, ?_, by omega, fun j hj => hsrc j hj, fun j hj hj3 => hz j (by omega) hj3⟩ + have := hok [] (by rw [hieq, ← hcpl, List.drop_length]; rfl) + simpa using this + · refine ⟨by simp, [], rfl, fun j hj => by simp at hj, fun j _ hj3 => exAtA_none j hj3, fun res h => by simpa using h, + fun e h => by simpa using h⟩ + +/-- **The IPC-buffer view** (module docstring): FL4's buffer word `buf` is 0 exactly when the spec has none; else its +first 128 words are the spec's buffer words, aligned. -/ +def BufViewA (mem : U64 → U64) (um : Fl4SpecA64.Word → Fl4SpecA64.Word8) (buf : U64) + (buffer : Option Fl4SpecA64.ObjRef) : Prop := + (buf = 0#u64 ↔ buffer = none) ∧ ∀ sb, buffer = some sb → ∀ k : U64, k.val < 128 → + (mem (bufWA buf k)).bv = loadWordAtA um (bufAddrA sb k.val) ∧ (bufAddrA sb k.val).toNat % 8 = 0 + +theorem BufViewA.al {mem : U64 → U64} {um : Fl4SpecA64.Word → Fl4SpecA64.Word8} {buf : U64} + {buffer : Option Fl4SpecA64.ObjRef} (h : BufViewA mem um buf buffer) : + ∀ sb, buffer = some sb → ∀ k, k < 128 → (bufAddrA sb k).toNat % 8 = 0 := fun sb hs k hk => by + have hv : (⟨BitVec.ofNat 64 k⟩ : U64).val = k := by + show (BitVec.ofNat 64 k).toNat = k; simp; omega + have h' := (h.2 sb hs ⟨BitVec.ofNat 64 k⟩ (by rw [hv]; omega)).2 + rw [hv] at h'; exact h' + +set_option maxHeartbeats 4000000 in +/-- **`lookup_extra_caps` refines `lookup_extra_caps`**, both outcomes (x86 `lookup_extra_rel`): the extra caps +related (`ExtraOKA`) or the same cap fault (`CapFaultCorrA`). -/ +theorem lookup_extra_relA {th : TypedHeapA} {s : S} {σ : Fl4SpecA64.State Unit} + (hR : StateRelA th (L.mem s) σ) (hV : SlotsValidA th (L.mem s)) {t : U64} (ht : th t = some .tcb) + (sbw : U64) (sbo : Option Fl4SpecA64.ObjRef) (hBV : BufViewA (L.mem s) σ.machineState.underlyingMemory sbw sbo) + (info : msginfo.MessageInfo) (mi : Fl4SpecA64.MessageInfo) + (hn : info.extra_caps.val = Fl4SpecA64.unat mi.miExtraCaps) (hn3 : info.extra_caps.val ≤ 3) : + ipc.lookup_extra_caps st s t sbw info ⦃ r => match r with + | .Ok e => ∃ caps, (Fl4SpecA64.lookupExtraCaps (Z := Unit) (specPtr t) sbo mi).run σ = ({(.ok caps, σ)}, False) ∧ + ExtraOKA th (L.mem s) e caps + | .Err f => ∃ f', (Fl4SpecA64.lookupExtraCaps (Z := Unit) (specPtr t) sbo mi).run σ = + ({(.error f', σ)}, False) ∧ CapFaultCorrA f f' ⦄ := by + unfold ipc.lookup_extra_caps + by_cases h0 : sbw = 0#u64 + · rw [if_pos h0, WP.spec_ok] + refine ⟨[], ?_, extraOK_noneA th _⟩ + have hn' : sbo = none := hBV.1.mp h0 + subst hn' + unfold Fl4SpecA64.lookupExtraCaps + rw [run_bind_okA (run_liftEA (show Fl4SpecA64.getExtraCptrs (Z := Unit) none mi σ = ({([], σ)}, False) from rfl))] + rfl + · rw [if_neg h0] + obtain ⟨sb, rfl⟩ : ∃ sb, sbo = some sb := by + cases e : sbo with + | none => exact absurd (hBV.1.mpr e) h0 + | some sb => exact ⟨sb, rfl⟩ + have hge := getExtraCptrs_someA (σ := σ) (sb := sb) (mi := mi) (by omega) (hBV.al sb rfl) + rw [← hn] at hge + refine WP.spec_mono (lookup_extra_loopA hR hV ht (fun k hk => (hBV.2 sb rfl k hk).1) info.extra_caps hn3) ?_ + intro r hr + have hrun : ∀ res, (Fl4Spec.NonDetE.mapME (xLookA (specPtr t)) + (xCptrsA σ.machineState.underlyingMemory sb info.extra_caps.val)).run σ = res → + (Fl4SpecA64.lookupExtraCaps (Z := Unit) (specPtr t) (some sb) mi).run σ = res := by + intro res h + unfold Fl4SpecA64.lookupExtraCaps + rw [run_bind_okA (run_liftEA hge)]; exact h + cases r with + | Ok e => + obtain ⟨caps, hc, hok⟩ := hr + exact ⟨caps, hrun _ hc, hok⟩ + | Err f => + obtain ⟨e', he, hf⟩ := hr + exact ⟨e', hrun _ he, hf⟩ + +end extra + +/-! ## `ExtraOKA` as the decodes' extra-cap relations -/ + +section conv +variable {th : TypedHeapA} {kb : U64} {m : U64 → U64} {e : Aeneas.Std.Array obj.Slot 3#usize} + {caps : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)} + +/-- The first extra cap with its slot (x86 `ExtraSRel`): none, or the cap of a slot of a CNode of `th`. -/ +def ExtraSRelA (th : TypedHeapA) (mem : U64 → U64) (x0 : U64) (cps : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) : + Prop := + (x0 = 0#u64 ∧ cps = []) ∨ + (x0 ≠ 0#u64 ∧ ∃ ps idxs rest a, th ps = some (.cnode idxs.length) ∧ x0 = cslotAddrA (specPtr ps, idxs) ∧ + CapEqA (capAtA mem x0) a ∧ cps = (a, (specPtr ps, idxs)) :: rest) + +/-- FL4's extra slots `x0 x1 x2` against the spec's extra caps (x86 `XcRel`): in order, `0` past the end; each a CNode +slot of `th` whose cap is the spec's. -/ +def XcRelA (th : TypedHeapA) (mem : U64 → U64) (x0 x1 x2 : U64) + (excaps : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) : Prop := + excaps.length ≤ 3 ∧ ∀ (i : Nat) (x : U64), [x0, x1, x2][i]? = some x → + (x = 0#u64 ↔ excaps.length ≤ i) ∧ ∀ y, excaps[i]? = some y → + x = cslotAddrA y.2 ∧ CapEqA (capAtA mem x) y.1 ∧ ∃ ps, th ps = some (.cnode y.2.2.length) ∧ y.2.1 = specPtr ps + +/-- A source slot of `ExtraOKA` (x86 `srcOK_facts`). -/ +theorem srcOK_factsA (hP : PlaceA th kb) {x : U64} + {y : Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr} (h : SrcOKA th m x y) : + x ≠ 0#u64 ∧ IsSlotA th x ∧ x = cslotAddrA y.2 ∧ CapRelA (capAtA m x) y.1 ∧ + ∃ ps, th ps = some (.cnode y.2.2.length) ∧ y.2.1 = specPtr ps := by + obtain ⟨ps, hth, hy1, hx, hcr⟩ := h + have hS : IsSlotA th x := by rw [hx]; exact AbsSlotA.isSlot hP ⟨ps, hy1, Or.inl hth⟩ + exact ⟨hP.slot_ne0 hS, hS, hx, hcr, ps, hth, hy1⟩ + +theorem ExtraOKA.rel1 (hP : PlaceA th kb) (h : ExtraOKA th m e caps) : + ExtraRelA th m (exAtA e 0) (caps.map Prod.fst) := by + obtain ⟨-, hs, hz⟩ := h + cases caps with + | nil => exact Or.inl ⟨hz 0 (by simp) (by omega), rfl⟩ + | cons y rest => + obtain ⟨hne, hS, -, hcr, -⟩ := srcOK_factsA hP (hs 0 (by simp)) + exact Or.inr ⟨hne, hS, y.1, rest.map Prod.fst, hcr, rfl⟩ + +theorem ExtraOKA.rel2 (hP : PlaceA th kb) (h : ExtraOKA th m e caps) : + ExtraRel2A th m (exAtA e 0) (exAtA e 1) (caps.map Prod.fst) := by + obtain ⟨-, hs, hz⟩ := h + cases caps with + | nil => exact Or.inl ⟨hz 0 (by simp) (by omega), rfl⟩ + | cons y rest => + obtain ⟨hne, hS, -, hcr, -⟩ := srcOK_factsA hP (hs 0 (by simp)) + refine Or.inr ⟨hne, hS, y.1, hcr, ?_⟩ + cases rest with + | nil => exact Or.inl ⟨hz 1 (by simp) (by omega), rfl⟩ + | cons y' rest' => + obtain ⟨hne', hS', -, hcr', -⟩ := srcOK_factsA hP (hs 1 (by simp)) + exact Or.inr ⟨hne', hS', y'.1, rest'.map Prod.fst, hcr', rfl⟩ + +theorem ExtraOKA.relS (hP : PlaceA th kb) (h : ExtraOKA th m e caps) : + ExtraSRelA th m (exAtA e 0) caps := by + obtain ⟨-, hs, hz⟩ := h + cases caps with + | nil => exact Or.inl ⟨hz 0 (by simp) (by omega), rfl⟩ + | cons y rest => + have h0 := hs 0 (by simp) + obtain ⟨hne, -, -, -⟩ := srcOK_factsA hP h0 + obtain ⟨ps, hth, hy1, hx, hcr⟩ := h0 + obtain ⟨y1, y2a, y2b⟩ := y + simp only [List.getElem_cons_zero] at hth hy1 hx hcr + subst hy1 + refine Or.inr ⟨hne, ps, y2b, rest, y1, hth, hx, hcr.2, rfl⟩ + +theorem ExtraOKA.xc (hP : PlaceA th kb) (h : ExtraOKA th m e caps) : + XcRelA th m (exAtA e 0) (exAtA e 1) (exAtA e 2) caps := by + obtain ⟨hl, hs, hz⟩ := h + suffices H : ∀ i < 3, (exAtA e i = 0#u64 ↔ caps.length ≤ i) ∧ ∀ y, caps[i]? = some y → + exAtA e i = cslotAddrA y.2 ∧ CapEqA (capAtA m (exAtA e i)) y.1 ∧ + ∃ ps, th ps = some (.cnode y.2.2.length) ∧ y.2.1 = specPtr ps by + refine ⟨hl, fun i x hx => ?_⟩ + rcases i with _ | _ | _ | i + all_goals simp at hx + all_goals (subst hx; exact H _ (by omega)) + intro i hi + by_cases hlt : i < caps.length + · obtain ⟨hne, -, hxa, hcr, hps⟩ := srcOK_factsA hP (hs i hlt) + refine ⟨⟨fun h0 => absurd h0 hne, fun h => by omega⟩, fun y hy => ?_⟩ + rw [List.getElem?_eq_getElem hlt] at hy + cases hy + exact ⟨hxa, hcr.2, hps⟩ + · refine ⟨⟨fun _ => by omega, fun _ => hz i (by omega) hi⟩, fun y hy => ?_⟩ + rw [List.getElem?_eq_none (by omega)] at hy; cases hy + +end conv + +/-! ## The front end at `J` -/ + +section front +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- **The register view** (module docstring): the current TCB's message-info register and message registers are the +spec TCB's. -/ +def RegViewA (mem : U64 → U64) (σ : Fl4SpecA64.State Unit) (t : U64) : Prop := + ∀ x, σ.kheap (specPtr t) = some (.tcb x) → + (mem (regWA t layout.reg.MSG_INFO)).bv = x.tcbArch.tcbContext.userRegs Fl4SpecA64.msg_info_register ∧ + ∀ i r : U64, i.val < 4 → msg.msg_register i = ok r → + (mem (regWA t r)).bv = x.tcbArch.tcbContext.userRegs (Fl4SpecA64.msgRegisters.getD i.val .x5) + +/-- The current thread at `J`: a spec TCB, a TCB of `th`, with the register view's message-info word. -/ +theorem JAtZA.curViewA (J : JAtZA (L := L) AR Z s a σ m) (hRV : RegViewA (L.mem s) σ (L.curW s)) : + ∃ xs, σ.kheap (specPtr (L.curW s)) = some (.tcb xs) ∧ Z.th (L.curW s) = some .tcb ∧ + (L.mem s (regWA (L.curW s) layout.reg.MSG_INFO)).bv = + xs.tcbArch.tcbContext.userRegs Fl4SpecA64.msg_info_register ∧ + ∀ i r : U64, i.val < 4 → msg.msg_register i = ok r → + (L.mem s (regWA (L.curW s) r)).bv = xs.tcbArch.tcbContext.userRegs (Fl4SpecA64.msgRegisters.getD i.val .x5) := by + have hT : L.curW s ∈ Z.T := J.curT _ J.sch.cpuCs + obtain ⟨xs, hks, -, -⟩ := J.sch.tcb _ hT + exact ⟨xs, hks, (J.thT _).2 hT, (hRV xs hks).1, (hRV xs hks).2⟩ + +theorem single_fstA {α : Type} {x y : α} {σ : Fl4SpecA64.State Unit} + (h : (({(x, σ)} : Set (α × Fl4SpecA64.State Unit)), False) = ({(y, σ)}, False)) : x = y := + congrArg Prod.fst (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst h)) + +set_option maxHeartbeats 4000000 in +/-- **The front end at `J`** (x86 `JAtZB.frontJ`): the label words agree, and when both lookups of the extra caps +succeed, FL4's extra-slot array is the spec's extra caps (`ExtraOKA`). -/ +theorem JAtZA.frontJ (J : JAtZA (L := L) AR Z s a σ m) (hRV : RegViewA (L.mem s) σ (L.curW s)) + {info : msginfo.MessageInfo} (hinfo : msg.msg_info st s (L.curW s) = ok info) + {mi : Fl4SpecA64.MessageInfo} + (hmi : Fl4SpecA64.getMessageInfo (Z := Unit) (specPtr (L.curW s)) σ = ({(mi, σ)}, False)) + {buf : U64} {buffer : Option Fl4SpecA64.ObjRef} + (hBV : BufViewA (L.mem s) σ.machineState.underlyingMemory buf buffer) + {r2 : core.result.Result (Aeneas.Std.Array obj.Slot 3#usize) error.Fault} + (hr2 : ipc.lookup_extra_caps st s (L.curW s) buf info = ok r2) + {er : Except Fl4SpecA64.Fault (List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr))} + (her : (Fl4SpecA64.lookupExtraCaps (Z := Unit) (specPtr (L.curW s)) buffer mi).run σ = ({(er, σ)}, False)) : + info.label.bv = mi.miLabel ∧ + ∀ e exs, r2 = .Ok e → er = .ok exs → ExtraOKA Z.th (L.mem s) e exs := by + obtain ⟨xs, hks, ht, hmw, -⟩ := J.curViewA hRV + have hmi' := single_fstA (hmi.symm.trans (getMessageInfo_eqA hks)) + subst hmi' + have hm := msg_info_specA (L := L) s (L.curW s) + rw [hinfo, WP.spec_ok] at hm + obtain ⟨hlab, -, hext, -⟩ := hm + refine ⟨?_, ?_⟩ + · show info.label.bv = (xs.tcbArch.tcbContext.userRegs Fl4SpecA64.msg_info_register >>> 12) &&& + Fl4SpecA64.mask Fl4SpecA64.msgLabelBits + rw [label_maskA, hlab, hmw] + intro e exs h1 h2 + subst h1 h2 + have hlk := lookup_extra_relA J.st J.cv ht buf buffer hBV info + (Fl4SpecA64.dataToMessageInfo (xs.tcbArch.tcbContext.userRegs Fl4SpecA64.msg_info_register)) + (by show info.extra_caps.bv.toNat = _; rw [hext, hmw]) + (by show info.extra_caps.bv.toNat ≤ 3; rw [hext, hmw]; exact extra_le3A _) + rw [hr2, WP.spec_ok] at hlk + obtain ⟨caps, hc, hok⟩ := hlk + have e2 := single_fstA (her.symm.trans hc) + cases e2 + exact hok + +set_option maxHeartbeats 4000000 in +/-- **The extra caps' lookups at `J`** (x86 `JAtZB.extraJ`): FL4's and the spec's both succeed, or both fault with the +same cap fault (`CapFaultCorrA`). -/ +theorem JAtZA.extraJ (J : JAtZA (L := L) AR Z s a σ m) (hRV : RegViewA (L.mem s) σ (L.curW s)) + {info : msginfo.MessageInfo} (hinfo : msg.msg_info st s (L.curW s) = ok info) + {mi : Fl4SpecA64.MessageInfo} + (hmi : Fl4SpecA64.getMessageInfo (Z := Unit) (specPtr (L.curW s)) σ = ({(mi, σ)}, False)) + {buf : U64} {buffer : Option Fl4SpecA64.ObjRef} + (hBV : BufViewA (L.mem s) σ.machineState.underlyingMemory buf buffer) + {r2 : core.result.Result (Aeneas.Std.Array obj.Slot 3#usize) error.Fault} + (hr2 : ipc.lookup_extra_caps st s (L.curW s) buf info = ok r2) + {er : Except Fl4SpecA64.Fault (List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr))} + (her : (Fl4SpecA64.lookupExtraCaps (Z := Unit) (specPtr (L.curW s)) buffer mi).run σ = ({(er, σ)}, False)) : + (∃ e exs, r2 = .Ok e ∧ er = .ok exs) ∨ (∃ f f', r2 = .Err f ∧ er = .error f' ∧ CapFaultCorrA f f') := by + obtain ⟨xs, hks, ht, hmw, -⟩ := J.curViewA hRV + have hmi' := single_fstA (hmi.symm.trans (getMessageInfo_eqA hks)) + subst hmi' + have hm := msg_info_specA (L := L) s (L.curW s) + rw [hinfo, WP.spec_ok] at hm + obtain ⟨-, -, hext, -⟩ := hm + have hlk := lookup_extra_relA J.st J.cv ht buf buffer hBV info + (Fl4SpecA64.dataToMessageInfo (xs.tcbArch.tcbContext.userRegs Fl4SpecA64.msg_info_register)) + (by show info.extra_caps.bv.toNat = _; rw [hext, hmw]) + (by show info.extra_caps.bv.toNat ≤ 3; rw [hext, hmw]; exact extra_le3A _) + rw [hr2, WP.spec_ok] at hlk + cases r2 with + | Ok e => + obtain ⟨caps, hc, -⟩ := hlk + exact Or.inl ⟨e, caps, rfl, single_fstA (her.symm.trans hc)⟩ + | Err f => + obtain ⟨f', hc, hf⟩ := hlk + exact Or.inr ⟨f, f', rfl, single_fstA (her.symm.trans hc), hf⟩ + +/-- FL4's message length after the IPC-buffer check (`handle_invocation`; x86 `msgLen`). -/ +def msgLenA (info : msginfo.MessageInfo) (buf : U64) : U64 := + if info.length > msg.N_MSG_REGISTERS then (if buf = 0#u64 then msg.N_MSG_REGISTERS else info.length) + else info.length + +/-- The first `k` argument words are `fl4GetMr`'s (x86 `ArgRel`). -/ +def ArgRelA (s : S) (σ : Fl4SpecA64.State Unit) (ct buf : U64) (buffer : Option Fl4SpecA64.ObjRef) (k : Nat) : Prop := + ∀ i : U64, i.val < k → ∃ x, msg.syscall_arg st s ct buf i = ok x ∧ + Fl4SpecA64.fl4GetMr (Z := Unit) (specPtr ct) buffer i.val σ = ({(x.bv, σ)}, False) + +theorem msg_register_okA (i : U64) : ∃ r, msg.msg_register i = ok r := by + unfold msg.msg_register + by_cases h0 : i = 0#u64 + · rw [if_pos h0]; exact ⟨_, rfl⟩ + rw [if_neg h0] + by_cases h1 : i = 1#u64 + · rw [if_pos h1]; exact ⟨_, rfl⟩ + rw [if_neg h1] + by_cases h2 : i = 2#u64 + · rw [if_pos h2]; exact ⟨_, rfl⟩ + rw [if_neg h2]; exact ⟨_, rfl⟩ + +/-- One argument word (x86 `syscall_arg_rel`). -/ +theorem syscall_arg_relA {um : Fl4SpecA64.Word → Fl4SpecA64.Word8} {regs : Fl4SpecA64.Register → Fl4SpecA64.Word} + {ct buf : U64} {sbo : Option Fl4SpecA64.ObjRef} + (hmr : ∀ i r : U64, i.val < 4 → msg.msg_register i = ok r → + (L.mem s (regWA ct r)).bv = regs (Fl4SpecA64.msgRegisters.getD i.val .x5)) + (hBV : BufViewA (L.mem s) um buf sbo) (i : U64) (hi : i.val < 127) : + ∃ x, msg.syscall_arg st s ct buf i = ok x ∧ x.bv = specArgA regs sbo um i.val := by + have hN : msg.N_MSG_REGISTERS.val = 4 := by simp [msg.N_MSG_REGISTERS] + unfold msg.syscall_arg + by_cases h4 : i < msg.N_MSG_REGISTERS + · have hi4' : i.val < msg.N_MSG_REGISTERS.val := by scalar_tac + have hi4 : i.val < 4 := by omega + rw [if_pos h4] + obtain ⟨r, hr⟩ := msg_register_okA i + rw [hr, bind_ok, tcb_reg_eqA] + refine ⟨_, rfl, ?_⟩ + rw [hmr i r hi4 hr] + unfold specArgA; rw [if_pos hi4] + · have hi4 : ¬ i.val < 4 := by + intro h; apply h4; have : i.val < msg.N_MSG_REGISTERS.val := by omega + scalar_tac + rw [if_neg h4] + by_cases h0 : buf = 0#u64 + · have hn := hBV.1.mp h0 + subst hn + simp only [h0, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + refine ⟨0#u64, rfl, ?_⟩ + unfold specArgA; rw [if_neg hi4]; rfl + · obtain ⟨sb, rfl⟩ : ∃ sb, sbo = some sb := by + cases e : sbo with + | none => exact absurd (hBV.1.mpr e) h0 + | some sb => exact ⟨sb, rfl⟩ + have hb : (buf != 0#u64) = true := by simpa using h0 + rw [if_pos hb] + simp only [lift, bind_ok] + have hk : (wa i 1#u64).val = i.val + 1 := by + simp [core.num.U64.wrapping_add_val_eq, U64.size_def, U64.numBits]; omega + rw [buf_get_eqA] + refine ⟨_, rfl, ?_⟩ + rw [(hBV.2 sb rfl _ (by rw [hk]; omega)).1] + unfold specArgA; rw [if_neg hi4]; dsimp only; rw [hk] + +set_option maxHeartbeats 4000000 in +/-- **The argument words at `J`** (x86 `JAtZB.argsJ`): the spec's `getMrs` words are FL4's `syscall_arg`s, `msgLenA` +of them, and the first five arguments are `fl4GetMr`'s (`ArgRelA`, also past the message length). -/ +theorem JAtZA.argsJ (J : JAtZA (L := L) AR Z s a σ m) (hRV : RegViewA (L.mem s) σ (L.curW s)) + {info : msginfo.MessageInfo} (hinfo : msg.msg_info st s (L.curW s) = ok info) + {mi : Fl4SpecA64.MessageInfo} + (hmi : Fl4SpecA64.getMessageInfo (Z := Unit) (specPtr (L.curW s)) σ = ({(mi, σ)}, False)) + {buf : U64} {buffer : Option Fl4SpecA64.ObjRef} + (hBV : BufViewA (L.mem s) σ.machineState.underlyingMemory buf buffer) + {argsU : List U64} + (hmrs : Fl4SpecA64.getMrs (Z := Unit) (specPtr (L.curW s)) buffer mi σ = ({(argsU.map UScalar.bv, σ)}, False)) : + (msgLenA info buf).val = argsU.length ∧ + (∀ (i : U64) x, argsU[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) ∧ + ArgRelA (st := st) s σ (L.curW s) buf buffer 5 := by + obtain ⟨xs, hks, -, hmw, hmr⟩ := J.curViewA hRV + have hmi' := single_fstA (hmi.symm.trans (getMessageInfo_eqA hks)) + subst hmi' + have hm := msg_info_specA (L := L) s (L.curW s) + rw [hinfo, WP.spec_ok] at hm + obtain ⟨-, hlen, -, hle⟩ := hm + have hal := hBV.al + set regs := xs.tcbArch.tcbContext.userRegs + set um := σ.machineState.underlyingMemory + set len := Fl4SpecA64.unat (Fl4SpecA64.dataToMessageInfo (regs Fl4SpecA64.msg_info_register)).miLength with hlen' + have hlv : info.length.val = len := by + show info.length.bv.toNat = _; rw [hlen, hmw] + have hV : argsU.map UScalar.bv = specMrsA regs buffer um len := + single_fstA (hmrs.symm.trans (getMrs_eqA hks _ hal _)) + have hL : argsU.length = (specMrsA regs buffer um len).length := by + rw [← hV, List.length_map] + have hrel := syscall_arg_relA (L := L) (st := st) (s := s) (regs := regs) hmr hBV + refine ⟨?_, fun i x hx => ?_, fun i hi => ?_⟩ + · rw [hL, specMrsA_length] + have hN : msg.N_MSG_REGISTERS.val = 4 := by simp [msg.N_MSG_REGISTERS] + unfold msgLenA + by_cases hgt : info.length > msg.N_MSG_REGISTERS + · have hgt0 : msg.N_MSG_REGISTERS.val < info.length.val := by scalar_tac + rw [if_pos hgt] + by_cases h0 : buf = 0#u64 + · rw [if_pos h0, hN, hBV.1.mp h0]; (try simp only); omega + · rw [if_neg h0] + cases e : buffer with + | none => exact absurd (hBV.1.mpr e) h0 + | some _ => (try simp only); omega + · have hle' : info.length.val ≤ 4 := by + have : ¬ msg.N_MSG_REGISTERS.val < info.length.val := fun h => hgt (by scalar_tac) + omega + rw [if_neg hgt] + cases buffer <;> (try simp only) <;> omega + · have hlt : i.val < argsU.length := by + by_contra h; rw [List.getElem?_eq_none (by omega)] at hx; cases hx + have hx' : argsU[i.val] = x := by rw [List.getElem?_eq_getElem hlt] at hx; cases hx; rfl + have h120 : argsU.length ≤ 120 := by + rw [hL]; exact le_trans (specMrsA_len_le _ _ _ _) (by omega) + obtain ⟨y, hy, hyv⟩ := hrel i (by omega) + have hg := specMrsA_get regs buffer um len i.val (by omega) (by rw [← hL]; exact hlt) + have hm : (argsU.map UScalar.bv)[i.val]! = x.bv := by + rw [getElem!_pos _ i.val (by simpa using hlt)]; simp [hx'] + rw [hV] at hm + rw [hy]; congr 1 + apply UScalar.eq_of_val_eq + show y.bv.toNat = x.bv.toNat + rw [hyv, ← hg, hm] + · obtain ⟨y, hy, hyv⟩ := hrel i (by omega) + refine ⟨y, hy, ?_⟩ + rw [fl4GetMr_eqA hks _ hal i.val (by omega), hyv] + +end front + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/InsBaseA.lean b/proofs/Fl4ProofsA64/P3/InsBaseA.lean new file mode 100644 index 000000000..7a06608ef --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/InsBaseA.lean @@ -0,0 +1,209 @@ +/- +P3 (aarch64, Stage-3 `IpcExtraCapTransferA`): **`cap_insert_dA` from what it reads of `JEntA`**, so an insert +after steps that write only words off the relation's footprint and off the `OS` slots (the extra-cap transfer's +badge stores into the receive buffer) is refined without re-establishing A64 P1's whole `SysInvA`. + +* **`InsBaseA`**: `JAtZAX`, `OSOK`, the `OS` slots canonical (`hCan`, a case hypothesis), the nodes' and SCs' and VCPUs' alignment, the `FootNA` / `CapW` + disjointness (`footN_offA`), A64 P1's `CtOff` and `UtRev` at the state; **`InsBaseA.of_jEntA`** (from + `JEntA`), **`InsBaseA.keep`** (to a state with the same kernel-state base and core, the `OS` slots' words kept + and `JAtZAX` re-established there; `FootNA.of_ksbA` maps the footprint back). +* **`cap_insert_jBA`** / **`cap_insert_dBA`**: `cap_insert_jA` / `cap_insert_dA` (`InsertA`, `InsDisA`) with + `InsBaseA` for `JEntA`; the bodies are theirs with each `JEntA` projection replaced by the field, and the + posts also give `SpecSlotA` and the slots' caps (`InsSlotsA`, from A64 P1's `lrF` / `srcF`). + +Case hypotheses only; no named premise. New module; no existing statement changed. +-/ +import Fl4ProofsA64.P3.InsDisA +import Fl4ProofsA64.P3.SlotJA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-- A footprint word at a state with the same kernel-state base and core is one at the first. -/ +theorem FootNA.of_ksbA {Z : XSetsA} {s s1 : S} (hk : L.ksb s1 = L.ksb s) (hcp : L.cpu s1 = L.cpu s) {b : U64} + (h : FootNA (L := L) Z s1 b) : FootNA (L := L) Z s b := by + have hna := nodeAt_ksb (L := L) hk + cases h with + | node c _ hc' ho => simp only [hna] at ho; exact .node c _ hc' ho + | cur _ ho => simp only [hcp, hna] at ho; exact .cur _ ho + | cur2 _ ho => simp only [hcp, hna] at ho; exact .cur2 _ ho + | tcb y _ hy ho => exact .tcb y _ hy ho + | tcb2 y _ hy ho => exact .tcb2 y _ hy ho + | sc c hc' => exact .sc c hc' + | sc2 c hc' => exact .sc2 c hc' + | irq i hi => rw [hk]; exact .irq i hi + | glob _ ho => rw [hk] at ho; exact .glob _ ho + | vcpu v _ hv hw => exact .vcpu v _ hv hw + | vs _ hb => rw [hk] at hb; exact .vs _ hb + | ext _ hb => exact .ext _ hb + +/-- The kernel-state base and core kept, and the slots' caps after `cdt::insert nc src dest` (A64 P1's `lrF` / `srcF`): `dest` holds `nc`, `src` its cap +or that cap with a full untyped index, every other `OS` slot its cap. -/ +def InsSlotsA (st : machine.Store S) [L : LawfulStore st] (OS : Set U64) (s s' : S) (src dest : U64) + (nc : capability.Cap) : Prop := + L.ksb s' = L.ksb s ∧ L.cpu s' = L.cpu s ∧ capA st s' dest = nc ∧ FullA (capA st s' src) (capA st s src) ∧ + ∀ y ∈ OS, y ≠ src → y ≠ dest → capA st s' y = capA st s y + +/-- **What `cap_insert_dA` reads of `JEntA`** (module docstring). -/ +structure InsBaseA (AR : A → HwRootA → Prop) (Z : XSetsA) (H : Heap) (OS : Set U64) (s : S) (a : A) + (σ : State Unit) (m : McState) : Prop where + j : JAtZAX (L := L) AR Z s a σ m + os : OSOK H.tcbs H.omega H.frame OS + /-- Every `OS` slot is canonical (P1 `DCan OS`; a case hypothesis, as `CnodeDelFullA`'s `hCan`). -/ + can : ∀ x ∈ OS, obj.sext48 x = ok x + nodes : ∀ c ∈ Z.Cs, ∀ o : U64, o.val < 4288 → o.val % 8 = 0 → Aligned (L.nodeAt s c o) + scAl : ∀ c ∈ Z.C, Aligned c + vcAl : ∀ v ∈ Z.V, Aligned v + footN : ∀ b, FootNA (L := L) Z s b → ¬ W7.CapW H.tcbs {b | InOS OS b} ∅ b + ct : CtOff st (L.ksb s) OS s + utRev : UtRev st OS s + +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {s : S} {a : A} {σ : State Unit} {m : McState} + +theorem InsBaseA.of_jEntA (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + (hCan : ∀ x ∈ OS, obj.sext48 x = ok x) : + InsBaseA (L := L) AR Z H OS s a σ m := + ⟨E.j, E.sys.ent.os, hCan, bxi_nodes E.sys.ent.bxi, E.scAl, E.ap.vcAl, footN_offA E, + E.sys.sysl.ct, E.sys.sysl.sys.stz.1.2.1⟩ + +theorem InsBaseA.keep (B : InsBaseA (L := L) AR Z H OS s a σ m) {s1 : S} {σ1 : State Unit} + (J1 : JAtZAX (L := L) AR Z s1 a σ1 m) (hk : L.ksb s1 = L.ksb s) (hcp : L.cpu s1 = L.cpu s) + (hO : OSKeep st OS s s1) : InsBaseA (L := L) AR Z H OS s1 a σ1 m := by + refine ⟨J1, B.os, B.can, fun c hc o h1 h2 => ?_, B.scAl, B.vcAl, + fun b hb => B.footN b (FootNA.of_ksbA hk hcp hb), fun y hy => ?_, fun u hu hut => ?_⟩ + · rw [nodeAt_ksb hk]; exact B.nodes c hc o h1 h2 + · rw [hk, hO.capA hy]; exact B.ct y hy + · rw [hO.capA hu] at hut + rw [revocable_congr (hO u hu).2.2.2] + exact B.utRev u hu hut + +/-- **`cap_insert_jA` from `InsBaseA`** (module docstring). -/ +theorem cap_insert_jBA (B : InsBaseA (L := L) AR Z H OS s a σ m) + (hT : TreeA st OS Z.G.chains Z.G.par s) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) + {ss sd : CslotPtr} (hAs : AbsSlotA Z.th ss) (hAd : AbsSlotA Z.th sd) + (hsn : capAtA (L.mem s) (cslotAddrA ss) ≠ .Null) + (hdn : capAtA (L.mem s) (cslotAddrA sd) = .Null) + (hdc : obj.sext48 (cslotAddrA sd) = ok (cslotAddrA sd)) + {nc : capability.Cap} {anc : Fl4SpecA64.Cap} (hwf : CapWfA nc) (hanc : CapEqA nc anc) + (hw : CapRt.EncWf nc) (hn : nc ≠ .Null) + (hal : UA nc (capA st s (cslotAddrA ss))) + (hk : SameBadgeKindA nc (capA st s (cslotAddrA ss))) + (hut : isUtA (capA st s (cslotAddrA ss)) = true → + ∀ y, ¬ Fl4Proofs.P3.Mdb.Desc Z.G.par y (cslotAddrA ss)) + (hub : isUtA (capA st s (cslotAddrA ss)) = true → + insBA σ ss (absCapA nc) (absCapA (capA st s (cslotAddrA ss))) = true) + (hsib : insBA σ ss (absCapA nc) (absCapA (capA st s (cslotAddrA ss))) = false → + ∀ y, ¬ Fl4Proofs.P3.Mdb.Desc Z.G.par y (cslotAddrA ss)) + (hno : CapObjOkA Z nc) : + cdt.insert st s nc (cslotAddrA ss) (cslotAddrA sd) ⦃ s' => + ∃ σ', ((), σ') ∈ (Fl4SpecA64.capInsert (Z := Unit) anc ss sd σ).1 ∧ + JAtZAX (L := L) AR { Z with G := (insGA Z.G (cslotAddrA ss) (cslotAddrA sd) + (insBA σ ss (absCapA nc) (absCapA (capA st s (cslotAddrA ss))))) } s' a σ' m ∧ + TreeA st OS (insG (cslotAddrA ss) (cslotAddrA sd) Z.G.chains) + (insGA Z.G (cslotAddrA ss) (cslotAddrA sd) + (insBA σ ss (absCapA nc) (absCapA (capA st s (cslotAddrA ss))))).par s' ∧ + (CapOkA σ.interruptIrqNode sd anc → ∀ n, SInvXA σ m n → SInvXA σ' m n) ∧ + SpecSlotA σ σ' ∧ InsSlotsA st OS s s' (cslotAddrA ss) (cslotAddrA sd) nc ∧ + WritesIn st s s' (W7.CapW H.tcbs {b | InOS OS b} ∅) ⦄ := by + have hO := B.os + have hP := B.j.1.place + have hsS : IsSlotA Z.th (cslotAddrA ss) := AbsSlotA.isSlot hP hAs + have hdS : IsSlotA Z.th (cslotAddrA sd) := AbsSlotA.isSlot hP hAd + have hsG : cslotAddrA ss ∈ Z.G.chains.flatten := (hT.lk.members _).2 ⟨hsub _ hsS, hsn⟩ + have hdG : cslotAddrA sd ∉ Z.G.chains.flatten := fun m => ((hT.lk.members _).1 m).2 hdn + have hsd : cslotAddrA ss ≠ cslotAddrA sd := fun e => hdG (e ▸ hsG) + have F := hT.lk.lkF hO.nz + have inOS : ∀ y ∈ Z.G.chains.flatten, y ∈ OS := fun y hy => ((hT.lk.members y).1 hy).1 + have hwr : cdt.insert st s nc (cslotAddrA ss) (cslotAddrA sd) ⦃ s' => + WritesIn st s s' (W7.CapW H.tcbs {b | InOS OS b} ∅) ∧ InsSlotsA st OS s s' (cslotAddrA ss) (cslotAddrA sd) nc ⦄ := by + unfold cdt.insert + rw [capA_eq, bind_ok] + exact WP.spec_mono (WP.spec_and (cdt.insert_with_cap.lrF (osokA hO) (LR.self s) (hsub _ hsS) (hsub _ hdS) hsd hdc + rfl hsn hw (fun h => inOS _ (F.nxm _ hsG h))) + (cdt.insert_with_cap.srcF hO (hsub _ hsS) (hsub _ hdS) hsd rfl hw (fun h => inOS _ (F.nxm _ hsG h)))) + fun _ ⟨⟨h1, _, h3, _, _, h6, _⟩, h7⟩ => ⟨h1, h1.1, h1.2.1, h3, h7, h6⟩ + refine WP.spec_mono (WP.spec_and (cap_insert_refinesA s σ Z.G B.j.1.st B.j.1.cdt hP hO hT hsub hnul + hAs hAd hsn hdn hdc hwf hanc hw hn hal hk hut hub hsib) hwr) ?_ + rintro s' ⟨⟨σ', hm, hst, hcdt, hT', hSS, hcs⟩, hw', hIS⟩ + refine ⟨σ', hm, B.j.of_slotWrites hw' (fun b hb' hb => B.footN b hb hb') + (FootNA.aligned B.j.1 B.nodes B.scAl B.vcAl) _ hSS hst hcdt fun y hy => ?_, hT', + fun hc n hI => SInvXA.capInsert hI hc hm, hSS, hIS, hw'⟩ + rcases hcs y hy with e | e | ⟨p, b, i, d, e⟩ <;> rw [e] + · exact B.j.capObj hy + · exact hno + · exact ⟨fun _ h => (by cases h), fun _ _ _ _ _ h => (by cases h), fun _ _ _ h => (by cases h), fun _ _ _ h => (by cases h), trivial⟩ + +open Classical in +/-- **`cap_insert_dA` from `InsBaseA`** (module docstring). -/ +theorem cap_insert_dBA (B : InsBaseA (L := L) AR Z H OS s a σ m) + (hT : TreeA st OS Z.G.chains Z.G.par s) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) + {ss sd : CslotPtr} (hAs : AbsSlotA Z.th ss) (hAd : AbsSlotA Z.th sd) + (hsn : capAtA (L.mem s) (cslotAddrA ss) ≠ .Null) + (hdn : capAtA (L.mem s) (cslotAddrA sd) = .Null) + (hdc : obj.sext48 (cslotAddrA sd) = ok (cslotAddrA sd)) + {nc : capability.Cap} {anc : Fl4SpecA64.Cap} (hwf : CapWfA nc) (hanc : CapEqA nc anc) + (hw : CapRt.EncWf nc) (hn : nc ≠ .Null) + (hal : UA nc (capA st s (cslotAddrA ss))) + (hk : SameBadgeKindA nc (capA st s (cslotAddrA ss))) + (hut : isUtA (capA st s (cslotAddrA ss)) = true → + ∀ y, ¬ Fl4Proofs.P3.Mdb.Desc Z.G.par y (cslotAddrA ss)) + (hsh : shapeA nc = shapeA (capA st s (cslotAddrA ss))) + (hz : ∀ p t n, capA st s (cslotAddrA ss) ≠ .Zombie p t n) + (hbd : ∀ r b rs, (absCapA (capA st s (cslotAddrA ss)) = .endpointCap r b rs ∨ + absCapA (capA st s (cslotAddrA ss)) = .notificationCap r b rs) → b ≠ 0 → + Fl4SpecA64.capEpBadge (absCapA nc) = b) + (hdo : ¬ IrqNodeA (L.ksb s) (fl4Ptr sd.1) ∨ IrqCapA (absCapA nc)) + (hcv : CapValidA Z.th (capA st s (cslotAddrA ss)) → CapValidA Z.th nc) : + cdt.insert st s nc (cslotAddrA ss) (cslotAddrA sd) ⦃ s' => + ∃ σ', ((), σ') ∈ (Fl4SpecA64.capInsert (Z := Unit) anc ss sd σ).1 ∧ + JAtZAX (L := L) AR { Z with G := (insGA Z.G (cslotAddrA ss) (cslotAddrA sd) + (insBA σ ss (absCapA nc) (absCapA (capA st s (cslotAddrA ss))))) } s' a σ' m ∧ + TreeA st OS (insG (cslotAddrA ss) (cslotAddrA sd) Z.G.chains) + (insGA Z.G (cslotAddrA ss) (cslotAddrA sd) + (insBA σ ss (absCapA nc) (absCapA (capA st s (cslotAddrA ss))))).par s' ∧ + (∀ n, SInvXA σ m n → SInvXA σ' m n) ∧ SpecSlotA σ σ' ∧ + InsSlotsA st OS s s' (cslotAddrA ss) (cslotAddrA sd) nc ∧ + WritesIn st s s' (W7.CapW H.tcbs {b | InOS OS b} ∅) ⦄ := by + have hsS : IsSlotA Z.th (cslotAddrA ss) := AbsSlotA.isSlot B.j.1.place hAs + obtain ⟨_, -, hcr⟩ := B.j.1.st.getAbsA hAs + have hov : NoOvA (capA st s (cslotAddrA ss)) := noOvA_of_wf hcr.1 + have hd := derivA_of _ _ (srA_shape hsh hov hcr.1 hsn hz) hbd + have hno : CapObjOkA Z nc := (B.j.capObj hsS).of_shapeA hsh hcv + have hoff : NodeOffA (L.ksb s) nc := + (NodeOffA.of_ctOff (B.ct _ (hsub _ hsS))).of_shapeA hsh + have hokAnc : CapOkA σ.interruptIrqNode sd anc := by + have h0 := capOk_insA B.j.1.irq hdo hoff + cases nc with + | PageTable p m asid va => + cases m + · rw [← hanc]; exact h0 + · obtain ⟨_, _, rfl, _, _⟩ := CapEqA.pt_mapped_iff.mp hanc + refine ⟨fun irq hirq hsl => ?_, fun ⟨_, _, _, _, e⟩ => by cases e⟩ + have hi := h0.1 irq hirq hsl + simp [IrqCapA, absCapA] at hi + | _ => rw [← hanc]; exact h0 + have hub : isUtA (capA st s (cslotAddrA ss)) = true → + insBA σ ss (absCapA nc) (absCapA (capA st s (cslotAddrA ss))) = true := fun hu => + insBA_of_orig (by rw [B.j.1.cdt.orig ss hAs]; exact revA_of_revocableA (B.utRev _ (hsub _ hsS) hu)) hd + refine WP.spec_mono (cap_insert_jBA B hT hsub hnul hAs hAd hsn hdn hdc hwf hanc hw hn hal hk hut + hub (hsib_ofA B.j.1.cdt hAs hd) hno) ?_ + rintro s' ⟨σ', hm, hJ, hT', hI, hSS, hIS, hw'⟩ + exact ⟨σ', hm, hJ, hT', hI hokAnc, hSS, hIS, hw'⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/InsDisA.lean b/proofs/Fl4ProofsA64/P3/InsDisA.lean new file mode 100644 index 000000000..fb7d37d8f --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/InsDisA.lean @@ -0,0 +1,356 @@ +/- +P3 (aarch64, A64-P3 S3 gen 7, ruling 12:26Z item 2): **the call-site discharges of `cap_insert_jA`'s case +hypotheses** `hub` / `hsib` / `hno` (`CapObjOkA`) / `CapOkA` — the A64 counterpart of x86's `MdbInv.sib` +(`P3/MdbInvIns`) and of the discharges at x86's `cap_insert` call sites (`TcbCaps`, `CtlIns`, `UntypedUtFull`). + +* The invariant is **`CdtRelA.prev`** (`P3/StateRelA`, gen 7; x86 `MdbInv.orig`): every ghost parent's slot has its + revocable bit set; spec side `SpecPOA` / `CdtRelA.po` (every `cdt` parent is an original cap). It is a field of + the CDT relation, so `JAtZAX` carries it; it is kept by `CdtRelA.removeA` / `moveA` / `swapA` / `insertA` (hence + across all five CSpace leaves: `empty_slot_jA`, `cap_move_jA`, `cap_swap_jA`, `cap_swap_for_delete_jA`, + `cap_insert_jA`) and holds at `w0` (`CdtRelA.empty`, no parent). +* **`CdtRelA.origOfDesc`** (x86 `MdbInv.sib`'s core, `orig`): a slot with a descendant is an original cap. +* **`insBA_of_orig`**: from an original source, `insBA` is `should_be_parent_of` at `original = true`; + **`derivA_of`** proves that from l4v's `same_region_as` of the two caps and the badge relation (a badged source's + copy keeps the badge; x86 `hsh` / `hbd`, stated on the spec caps); the arch part holds as `absCapA` makes no SGI / + SMC cap. +* **`hsib_ofA`** (x86 `MdbInv.sib`): in the sibling case the source has no descendants. **`hub_ofA`**: an untyped + source makes `dest` its child (A64 P1's `UtRev` in `SysL`: untyped slots are revocable, so original). +* **`CapObjOkA.of_shapeA`** (gen 9; gen 7's `of_kindA`): a cap of the source's shape names counted objects (from + `JAtZAX.capObj`), including (gen 9) a CNode / thread cap naming a CNode of its radix / a TCB of `th` (`CapValidA`). +* `NodeOffA` / `NodeOffA.of_ctOff` (A64 P1's `CtOff` in `SysL`, slot 0) / `.of_shapeA`, **`offNode_ofA`** (a pointer + off FL4's IRQ-node slots is off the spec's IRQ node, `IrqRelA.node`), **`capOk_insA`** (`CapOkA` for the + destination: off the IRQ node or an IRQ cap, and the new cap names no IRQ-node CNode). +* **`cap_insert_dA`**: `cap_insert_jA` with `hub`, `hsib`, `hno` and `CapOkA` discharged from `JEntA`, the spec + caps' `same_region_as`, the badge relation, the kind (`kindA`) and the destination off the IRQ node (or an IRQ + cap): S4's call sites owe no `cap_insert` debt beyond these decode-path facts and the leaf's own `hk` / `CapWfA` + (x86 parity, ruling 12:26Z item 3), `hut` (`ensure_no_children`) and P1's tree facts. + +* (gen 8, ruling 12:48Z item 2) **`same_region_as` from the shape** (x86 `MdbSem.shape` / `srB_spec_self`): `shapeA` + (the fields `same_region_as` ignores reset), `NoOvA` (an untyped's / frame's range does not wrap, as the spec + states it) with **`noOvA_of_wf`** (gen 12: from `CapWfA`'s `SpNoOvA`; gen 8's `noOvA_of` read A64 P1's + `UtInvA.reg` at the identity naming), and + **`srA_shape`**: `shapeA d = shapeA c` ⇒ `same_region_as (absCapA c) (absCapA d)` for a non-null, non-zombie, + `CapWfA` source with `NoOvA`. `cap_insert_dA` (statement changed) now takes `hsh : shapeA nc = shapeA src_cap` + and `hz` (source not a zombie) instead of `hsr`, and any `AbsSlotA` source / destination (gen 8, TCB slots); + `capOk_insA` takes any destination slot. +* (gen 9, ruling 13:20Z item 1) `cap_insert_dA` (statement changed): **`hkd` (`kindA nc = kindA src`) removed** — + `hno` and `NodeOffA` now follow from `hsh` (`CapObjOkA.of_shapeA`, `NodeOffA.of_shapeA`); the decode call sites + supply `hsh`, `hz`, `hbd`, `hdo` (+ the leaf's `hk`, `hwf`, `hw`, `hut`). + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.InsertA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 + +noncomputable section + +/-! ## The sibling fact (x86 `MdbInv.sib`) -/ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **A slot with a descendant is an original cap** (x86 `MdbInv.sib`'s core: `MdbInv.orig`). -/ +theorem CdtRelA.origOfDesc {th : TypedHeapA} {s : S} {σ : State Unit} {G : MdbGhostA} + (hC : CdtRelA st th s σ G) {src : CslotPtr} (hs : AbsSlotA th src) {y : U64} + (hy : Fl4Proofs.P3.Mdb.Desc G.par y (cslotAddrA src)) : σ.isOriginalCap src = true := by + obtain ⟨z, -, hz⟩ := Relation.TransGen.tail'_iff.1 hy + rw [hC.orig src hs] + exact hC.prev _ _ hz + +open Classical in +/-- From an original source, `insBA` is l4v's parent test at `original = true`. -/ +theorem insBA_of_orig {σ : State Unit} {src : CslotPtr} {a asc : Fl4SpecA64.Cap} + (ho : σ.isOriginalCap src = true) + (hd : Fl4SpecA64.shouldBeParentOf asc true a (decide (Fl4SpecA64.isCapRevocable a asc))) : + insBA σ src a asc = true := by + unfold insBA + exact decide_eq_true (by rw [ho]; exact hd) + +open Classical in +/-- **The sibling case leaves the source childless** (x86 `MdbInv.sib`, `hsib` of `cap_insert_jA`). -/ +theorem hsib_ofA {th : TypedHeapA} {s : S} {σ : State Unit} {G : MdbGhostA} (hC : CdtRelA st th s σ G) + {src : CslotPtr} (hs : AbsSlotA th src) {a asc : Fl4SpecA64.Cap} + (hd : Fl4SpecA64.shouldBeParentOf asc true a (decide (Fl4SpecA64.isCapRevocable a asc))) : + insBA σ src a asc = false → ∀ y, ¬ Fl4Proofs.P3.Mdb.Desc G.par y (cslotAddrA src) := by + intro hb y hy + rw [insBA_of_orig (hC.origOfDesc hs hy) hd] at hb + cases hb + +/-- A64 P1's `revocable` read as `revA`. -/ +theorem revA_of_revocableA {s : S} {u : U64} (h : obj.Slot.revocable st u s = ok true) : revA (L.mem s) u = true := by + rw [revocable_eqA] at h + have e : (281474976710656#u64 : U64) = RVB := rfl + unfold revA; rw [e]; exact okinj h + +end + +/-! ## The parent test from the spec caps -/ + +open Classical in +/-- **l4v's parent test at an original source** from `same_region_as` and the badge relation (x86 `sbp_eq` with +`isPF_self` / `isPF_child`): the arch part holds as `absCapA` makes no SGI / SMC cap. -/ +theorem derivA_of (c d : capability.Cap) (hsr : Fl4SpecA64.sameRegionAs (absCapA c) (absCapA d)) + (hbd : ∀ r b rs, (absCapA c = .endpointCap r b rs ∨ absCapA c = .notificationCap r b rs) → b ≠ 0 → + Fl4SpecA64.capEpBadge (absCapA d) = b) : + Fl4SpecA64.shouldBeParentOf (absCapA c) true (absCapA d) + (decide (Fl4SpecA64.isCapRevocable (absCapA d) (absCapA c))) := by + refine ⟨rfl, hsr, ?_⟩ + cases c with + | Endpoint p b s r g gr => + intro hb + have e := hbd _ _ _ (Or.inl rfl) hb + refine ⟨e, ?_⟩ + have hd : Fl4SpecA64.isEpCap (absCapA d) = true := hsr.1 + cases d <;> simp only [absCapA, Fl4SpecA64.isEpCap, Bool.false_eq_true] at hd + simp only [absCapA, Fl4SpecA64.capEpBadge] at e + simp [absCapA, Fl4SpecA64.isCapRevocable, Fl4SpecA64.capEpBadge, e] + | Notification p b s r => + intro hb + have e := hbd _ _ _ (Or.inr rfl) hb + refine ⟨e, ?_⟩ + have hd : Fl4SpecA64.isNtfnCap (absCapA d) = true := hsr.1 + cases d <;> simp only [absCapA, Fl4SpecA64.isNtfnCap, Bool.false_eq_true] at hd + simp only [absCapA, Fl4SpecA64.capEpBadge] at e + simp [absCapA, Fl4SpecA64.isCapRevocable, Fl4SpecA64.capEpBadge, e] + | Frame p sz r dev m e asid va => + cases e <;> simp [absCapA, absFrameE, Fl4SpecA64.shouldBeArchParentOf, Fl4SpecA64.ArchCap.isSGISignalCap, + Fl4SpecA64.ArchCap.isSMCCap] + | _ => first | trivial | simp [absCapA, Fl4SpecA64.shouldBeArchParentOf, Fl4SpecA64.ArchCap.isSGISignalCap, + Fl4SpecA64.ArchCap.isSMCCap] + +/-! ## `same_region_as` from the caps' shape (gen 8, ruling 12:48Z item 2; x86 `MdbSem.shape`, `srB_spec_self`) -/ + +/-- **A cap with the fields `same_region_as` ignores reset** (x86 `shape`): rights, badges, mapping data, guards, +free indices and device bits. -/ +def shapeA : capability.Cap → capability.Cap + | .Untyped p sb _ _ => .Untyped p sb 0#u64 false + | .Endpoint p _ _ _ _ _ => .Endpoint p 0#u64 false false false false + | .Notification p _ _ _ => .Notification p 0#u64 false false + | .Reply p _ => .Reply p false + | .CNode p r _ _ => .CNode p r 0#u64 0#u64 + | .SchedControl _ => .SchedControl 0#u64 + | .Frame p sz _ _ _ _ _ _ => .Frame p sz 0#u64 false false false 0#u64 0#u64 + | .PageTable p _ _ _ => .PageTable p false 0#u64 0#u64 + | .Pml4 p _ _ => .Pml4 p false 0#u64 + | .Ept p level _ _ _ => .Ept p level false 0#u64 0#u64 + | .HighVSpace p _ _ => .HighVSpace p false 0#u64 + | .AsidPool p _ => .AsidPool p 0#u64 + | c => c + +/-- **The no-overflow facts `same_region_as` of a cap with itself needs** (x86 `srB c c = true`): an untyped's and a +frame's last byte does not wrap, as the spec states it (gen 12: at the spec name `specPtr p`, `SpNoOvA`). -/ +def NoOvA : capability.Cap → Prop + | .Untyped p sb _ _ => SpNoOvA p sb.val + | .Frame p sz _ _ _ _ _ _ => SpNoOvA p (Fl4SpecA64.pageBitsForSize (frameSizeOfA sz)) + | _ => True + +/-- (gen 12) **`NoOvA` from `CapWfA`** (gen 8's `noOvA_of` read it off A64 P1's `UtInvA.reg` at the identity naming; +under `specPtr` the spec's range is the shifted one, which `CapWfA` carries as `SpNoOvA`). -/ +theorem noOvA_of_wf {c : capability.Cap} (hw : CapWfA c) : NoOvA c := by + cases c with + | Untyped p b i d => exact hw + | Frame p sz r d m e asid va => exact hw.2.2.2 + | _ => trivial + +/-- (S3 gen 18) **Two frames of one object and size are in one region**, plain or Stage-2 (`absFrameE`: l4v's +`frameCap` rule, the `stage2FrameCap` arm's `sameRegionWith`), given the no-overflow fact. -/ +theorem srA_frameA {p : U64} {sz : capability.FrameSize} + (hov : SpNoOvA p (Fl4SpecA64.pageBitsForSize (frameSizeOfA sz))) (e e' : Bool) (R R' : Fl4SpecA64.CapRights) + (d d' : Bool) (m m' : Option (Fl4SpecA64.Asid × Fl4SpecA64.VSpaceRef)) : + Fl4SpecA64.sameRegionAs (.archObjectCap (absFrameE e (specPtr p) R (frameSizeOfA sz) d m)) + (.archObjectCap (absFrameE e' (specPtr p) R' (frameSizeOfA sz) d' m')) := by + cases e <;> cases e' <;> + simpa [absFrameE, SpNoOvA, Fl4SpecA64.sameRegionAs, Fl4SpecA64.archSameRegionAs, Fl4SpecA64.ArchCap.isFrameCap, + Fl4SpecA64.ArchCap.acapObj, Fl4SpecA64.ArchCap.acapFsize, Fl4SpecA64.frameInRegion, + Fl4SpecA64.ExtArchCap.objRef] using hov + +set_option maxHeartbeats 4000000 in +/-- **A cap of the source's shape is in the source's region** (x86 `srB_spec_self`): l4v's `same_region_as` of the +abstract caps from `shapeA d = shapeA c`, the source's no-overflow facts and `CapWfA` (no x86-only cap), for a +non-null, non-zombie source. -/ +theorem srA_shape {c d : capability.Cap} (hsh : shapeA d = shapeA c) (hov : NoOvA c) (hw : CapWfA c) + (hn : c ≠ .Null) (hz : ∀ p t n, c ≠ .Zombie p t n) : + Fl4SpecA64.sameRegionAs (absCapA c) (absCapA d) := by + cases c + case Frame p sz r dev m e asid va => + cases d <;> simp [shapeA] at hsh + obtain ⟨h1, h2⟩ := hsh + subst h1 h2 + exact srA_frameA hov _ _ _ _ _ _ _ _ + all_goals cases d <;> simp [shapeA] at hsh + all_goals (try casesm* _ ∧ _) + all_goals (try subst_vars) + all_goals first + | exact absurd rfl hn + | exact absurd rfl (hz _ _ _) + | exact (hw : False).elim + | (simp only [absCapA, Fl4SpecA64.sameRegionAs, Fl4SpecA64.objSize, Fl4SpecA64.isPhysical, + Fl4SpecA64.objRefOf] + exact ⟨trivial, BitVec.le_refl _, hov, BitVec.le_refl _⟩) + | simpa [NoOvA, SpNoOvA, absCapA, Fl4SpecA64.sameRegionAs, Fl4SpecA64.archSameRegionAs, + Fl4SpecA64.ArchCap.isFrameCap, Fl4SpecA64.ArchCap.acapObj, Fl4SpecA64.ArchCap.acapFsize] using hov + | simp [absCapA, Fl4SpecA64.sameRegionAs, Fl4SpecA64.isEpCap, Fl4SpecA64.isNtfnCap, Fl4SpecA64.objRefOf, + Fl4SpecA64.isCnodeCap, Fl4SpecA64.bitsOf, Fl4SpecA64.isThreadCap, Fl4SpecA64.isSchedContextCap, + Fl4SpecA64.archSameRegionAs, Fl4SpecA64.ExtArchCap.sameObject] + +/-! ## Discharges at `JEntA` -/ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +open Classical in +/-- **An untyped source makes `dest` its child** (`hub` of `cap_insert_jA`): A64 P1's `UtRev` (in `SysL`) makes the +source revocable, hence original. -/ +theorem hub_ofA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {s : S} {a : A} {σ : State Unit} {m : McState} + (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) {src : CslotPtr} (hs : AbsSlotA Z.th src) + (hsO : cslotAddrA src ∈ OS) {nc : Fl4SpecA64.Cap} + (hd : Fl4SpecA64.shouldBeParentOf (absCapA (capA st s (cslotAddrA src))) true nc + (decide (Fl4SpecA64.isCapRevocable nc (absCapA (capA st s (cslotAddrA src)))))) : + isUtA (capA st s (cslotAddrA src)) = true → + insBA σ src nc (absCapA (capA st s (cslotAddrA src))) = true := fun hu => + insBA_of_orig (by rw [E.j.1.cdt.orig src hs]; exact revA_of_revocableA (E.sys.sysl.sys.stz.1.2.1 _ hsO hu)) hd + +theorem ptrA_vcpuA (p : U64) : ptrA (.Vcpu p) = p := (okinj (ptrA_eq (.Vcpu p))).symm +theorem ptrA_eptA (p l : U64) (mp : Bool) (asid va : U64) : ptrA (.Ept p l mp asid va) = p := + (okinj (ptrA_eq (.Ept p l mp asid va))).symm +theorem ptrA_highA (p : U64) (mp : Bool) (asid : U64) : ptrA (.HighVSpace p mp asid) = p := + (okinj (ptrA_eq (.HighVSpace p mp asid))).symm +theorem ptrA_cnodeA (p r g b : U64) : ptrA (.CNode p r g b) = p := (okinj (ptrA_eq (.CNode p r g b))).symm + +/-- **A cap of the source's shape names counted objects** (`hno` of `cap_insert_jA`, from `JAtZAX.capObj` of the +source; gen 9: from `shapeA`, which keeps the constructor, the pointer and a CNode's radix, so `CapValidA` carries +over; gen 7–8's `of_kindA` asked the kind and pointer; S3 gen 18: `CapValidA` now also fixes a mapped plain frame's +address, which `shapeA` forgets, so it is the hypothesis `hv`, from the decode's cap relation; statement changed). -/ +theorem CapObjOkA.of_shapeA {Z : XSetsA} {d c : capability.Cap} (hsh : shapeA d = shapeA c) + (h : CapObjOkA Z c) (hv : CapValidA Z.th c → CapValidA Z.th d) : CapObjOkA Z d := by + obtain ⟨h1, h2, h3, h4, h5⟩ := h + refine ⟨fun p e => ?_, fun p l mp asid va e => ?_, fun p mp asid e => ?_, fun p mp asid e => ?_, hv h5⟩ + · subst e + cases c <;> simp [shapeA] at hsh + subst hsh; exact h1 _ rfl + · subst e + cases c <;> simp [shapeA] at hsh + obtain ⟨rfl, rfl⟩ := hsh; exact h2 _ _ _ _ _ rfl + · subst e + cases c <;> simp [shapeA] at hsh + subst hsh; exact h3 _ _ _ rfl + · subst e + cases c <;> simp [shapeA] at hsh + subst hsh; exact h4 _ _ _ rfl + +/-- A CNode cap off FL4's IRQ-node slots (A64 P1's `CtOffP` at slot 0). -/ +def NodeOffA (kb : U64) (c : capability.Cap) : Prop := ∀ p r g b, c = .CNode p r g b → ¬ IslN kb p + +theorem NodeOffA.of_ctOff {kb : U64} {c : capability.Cap} (h : CtOffP kb c) : NodeOffA kb c := by + intro p r g b e + have := h.1 p r g b e 0#u64 (by simp) + rwa [slotIdx0] at this + +/-- (gen 9: from the shape; gen 7–8's `of_kindA` asked the kind and pointer) -/ +theorem NodeOffA.of_shapeA {kb : U64} {d c : capability.Cap} (hsh : shapeA d = shapeA c) + (h : NodeOffA kb c) : NodeOffA kb d := by + intro p r g b e + subst e + cases c <;> simp [shapeA] at hsh + obtain ⟨rfl, rfl⟩ := hsh; exact h _ _ _ _ rfl + +/-- **A pointer off FL4's IRQ-node slots is off the spec's IRQ node** (`IrqRelA.node`). -/ +theorem offNode_ofA {mem : U64 → U64} {ksb : U64} {σ : State Unit} (hI : IrqRelA mem ksb σ) {p : U64} + (h : ¬ IrqNodeA ksb p) : OffNodeA σ.interruptIrqNode (specPtr p) := by + intro irq hirq e + have h1 : irq.toNat < 5376 := hirq + have h2 : irq.toNat < 8192 := irq.isLt + have hv : (⟨BitVec.ofNat 64 irq.toNat⟩ : U64).val = irq.toNat := by + show (BitVec.ofNat 64 irq.toNat).toNat = _ + rw [BitVec.toNat_ofNat]; omega + obtain ⟨i, hiv⟩ : ∃ i : U64, i.val = irq.toNat := ⟨_, hv⟩ + have hn := hI.node i (by rw [hiv]; exact h1) + have hb : BitVec.ofNat 13 i.val = irq := by + apply BitVec.eq_of_toNat_eq + rw [BitVec.toNat_ofNat] + first + | (rw [hiv]; exact Nat.mod_eq_of_lt h2) + | (simp only [UScalar.val] at hiv ⊢; rw [hiv]; exact Nat.mod_eq_of_lt h2) + | (have := hiv; simp at this ⊢; omega) + rw [hb] at hn + exact h ⟨i, by rw [hiv]; exact h1, specPtr_inj (e.trans hn)⟩ + +/-- **`CapOkA` for an inserted cap** (module docstring): the destination's object is off FL4's IRQ-node slots or the +cap is an IRQ cap, and the cap names no IRQ-node CNode (gen 8: any destination slot `sd`, its object `fl4Ptr sd.1`, gen 12). -/ +theorem capOk_insA {mem : U64 → U64} {ksb : U64} {σ : State Unit} (hI : IrqRelA mem ksb σ) {sd : CslotPtr} + {nc : capability.Cap} (hdo : ¬ IrqNodeA ksb (fl4Ptr sd.1) ∨ IrqCapA (absCapA nc)) + (hno : NodeOffA ksb nc) : CapOkA σ.interruptIrqNode sd (absCapA nc) := by + refine ⟨fun irq hirq e => ?_, fun ⟨irq, hirq, n, g, e⟩ => ?_⟩ + · rcases hdo with h | h + · exact absurd ((specPtr_fl4Ptr _).trans e) (offNode_ofA hI h irq hirq) + · exact h + · cases nc with + | CNode p r gs g' => + simp only [absCapA, Fl4SpecA64.Cap.cnodeCap.injEq] at e + exact offNode_ofA hI (hno p r gs g' rfl) irq hirq e.1 + | _ => simp [absCapA] at e + +open Classical in +/-- **`cap_insert_jA` with its call-site debts discharged** (module docstring; ruling 12:26Z item 2). -/ +theorem cap_insert_dA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} + {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} {ia ie : Nat} {s : S} {a : A} {σ : State Unit} {m : McState} + (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + (hT : TreeA st OS Z.G.chains Z.G.par s) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) + {ss sd : CslotPtr} (hAs : AbsSlotA Z.th ss) (hAd : AbsSlotA Z.th sd) + (hsn : capAtA (L.mem s) (cslotAddrA ss) ≠ .Null) + (hdn : capAtA (L.mem s) (cslotAddrA sd) = .Null) + (hdc : obj.sext48 (cslotAddrA sd) = ok (cslotAddrA sd)) + {nc : capability.Cap} {anc : Fl4SpecA64.Cap} (hwf : CapWfA nc) (hanc : CapEqA nc anc) + (hw : CapRt.EncWf nc) (hn : nc ≠ .Null) + (hal : UA nc (capA st s (cslotAddrA ss))) + (hk : SameBadgeKindA nc (capA st s (cslotAddrA ss))) + (hut : isUtA (capA st s (cslotAddrA ss)) = true → + ∀ y, ¬ Fl4Proofs.P3.Mdb.Desc Z.G.par y (cslotAddrA ss)) + (hsh : shapeA nc = shapeA (capA st s (cslotAddrA ss))) + (hz : ∀ p t n, capA st s (cslotAddrA ss) ≠ .Zombie p t n) + (hbd : ∀ r b rs, (absCapA (capA st s (cslotAddrA ss)) = .endpointCap r b rs ∨ + absCapA (capA st s (cslotAddrA ss)) = .notificationCap r b rs) → b ≠ 0 → + Fl4SpecA64.capEpBadge (absCapA nc) = b) + (hdo : ¬ IrqNodeA (L.ksb s) (fl4Ptr sd.1) ∨ IrqCapA (absCapA nc)) + (hcv : CapValidA Z.th (capA st s (cslotAddrA ss)) → CapValidA Z.th nc) : + cdt.insert st s nc (cslotAddrA ss) (cslotAddrA sd) ⦃ s' => + ∃ σ', ((), σ') ∈ (Fl4SpecA64.capInsert (Z := Unit) anc ss sd σ).1 ∧ + JAtZAX (L := L) AR { Z with G := (insGA Z.G (cslotAddrA ss) (cslotAddrA sd) + (insBA σ ss (absCapA nc) (absCapA (capA st s (cslotAddrA ss))))) } s' a σ' m ∧ + TreeA st OS (insG (cslotAddrA ss) (cslotAddrA sd) Z.G.chains) + (insGA Z.G (cslotAddrA ss) (cslotAddrA sd) + (insBA σ ss (absCapA nc) (absCapA (capA st s (cslotAddrA ss))))).par s' ∧ + ∀ n, SInvXA σ m n → SInvXA σ' m n ⦄ := by + have hsS : IsSlotA Z.th (cslotAddrA ss) := AbsSlotA.isSlot E.j.1.place hAs + obtain ⟨_, -, hcr⟩ := E.j.1.st.getAbsA hAs + have hov : NoOvA (capA st s (cslotAddrA ss)) := noOvA_of_wf hcr.1 + have hd := derivA_of _ _ (srA_shape hsh hov hcr.1 hsn hz) hbd + have hno : CapObjOkA Z nc := (E.j.capObj hsS).of_shapeA hsh hcv + have hoff : NodeOffA (L.ksb s) nc := + (NodeOffA.of_ctOff (E.sys.sysl.ct _ (hsub _ hsS))).of_shapeA hsh + have hokAnc : CapOkA σ.interruptIrqNode sd anc := by + have h0 := capOk_insA E.j.1.irq hdo hoff + cases nc with + | PageTable p m asid va => + cases m + · rw [← hanc]; exact h0 + · obtain ⟨_, _, rfl, _, _⟩ := CapEqA.pt_mapped_iff.mp hanc + refine ⟨fun irq hirq hsl => ?_, fun ⟨_, _, _, _, e⟩ => by cases e⟩ + have hi := h0.1 irq hirq hsl + simp [IrqCapA, absCapA] at hi + | _ => rw [← hanc]; exact h0 + refine WP.spec_mono (cap_insert_jA E hT hsub hnul hAs hAd hsn hdn hdc hwf hanc hw hn hal hk hut + (hub_ofA E hAs (hsub _ hsS) hd) (hsib_ofA E.j.1.cdt hAs hd) hno) ?_ + rintro s' ⟨σ', hm, hJ, hT', hI⟩ + exact ⟨σ', hm, hJ, hT', hI hokAnc⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/InsertA.lean b/proofs/Fl4ProofsA64/P3/InsertA.lean new file mode 100644 index 000000000..7c7fbb584 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/InsertA.lean @@ -0,0 +1,891 @@ +/- +P3 (aarch64, A64-P3 S3 gen 6, ruling 11:49Z step 2): **the joint `cap_insert` leaf: FL4's `cdt::insert` refines l4v +`cap_insert`** (CSpace_A, nondeterministic spec, `Z = Unit`) between two slots of the typed heap (gen 8: CNode or TCB slots), **both the +child and the sibling case** (ruling q3, as x86 `InsertRefine`): the ghost parent of `dest` is +`if b then some src else par src` with `b = decide (shouldBeParentOf …)` (`insBA`), with the whole `JAtZAX` carried. + +* `revFA` / `is_cap_revocable_eqA` / `SameBadgeKindA` / **`revFA_spec`** (x86 `revF`, `revF_spec`): FL4's + `is_cap_revocable` is l4v's `isCapRevocable` through `absCapA`, for a well-formed cap of the source's badge kind. +* `FullCondA` / **`utCondA_iff`** (x86 `fullF_spec`): FL4's `set_untyped_cap_as_full` test is l4v's; + **`setUtFull_capA`** (the source's cap after it, exactly). +* **`insWC_walkA`**: `insert_with_cap`'s exact bit 48 (`dest`'s is `revFA`, every other `OS` slot's kept; through + `set_nf_rvE` and the shared `linkBitA`) and the source's cap after it, exactly. +* `npA_desc_iff` (x86 `np_desc_iff`), **`UtInvA.insGenA`** (ruling q3: A64 P1's `UtInvA.ins` over `bp : Bool`, x86 + `InsCtx.utinv`; zero P1 edits), **`insWC_treeGA`** (A64 P1's `cdt.insert_with_cap.tree` over `bp`, `Mdb.InsCase`). +* Spec side: `insFinA` / **`capInsert_memA`** (x86 `capInsert_eq`, membership), `StateRelA.congrA`, + `SpecSlotA.insFin`. +* **`CdtRelA.insertA`** (x86 `CdtRel.insert`), ghost **`insGA`**. +* **`cap_insert_refinesA`** (x86 `insert_refines`): case hypotheses `hk` (x86 `SameBadgeKind`), `hut` (`derive_cap`'s + `ensure_no_children`), `hub` (an untyped source makes `dest` its child: l4v's `should_be_parent_of` for an original + untyped; P1's `UtRev`), `hsib` (x86 `hsib`: in the sibling case `src` has no descendants), all discharged at call + sites. +* **`cap_insert_jA`**: at `JEntA`, the whole `JAtZAX` at `insGA` (`JAtZAX.of_slotWrites`, `footN_offA`, the write set + from A64 P1's `cdt.insert_with_cap.lrF` at `osokA`), `SInvXA` when the cap may go into `dest` (`CapOkA`). + +Gen 8: CNode or TCB slots (`AbsSlotA`, `StateRelA.setAbsA` / `getAbsA`). Named premise: none (gen 7: `SlotApA` is `JEntA.ap`, no longer a premise; `CdtRelA.prev` kept, gen 7). +-/ +import Fl4ProofsA64.P3.SwapA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## FL4's revocable bit and untyped test, against l4v's -/ + +/-- FL4's `is_cap_revocable` (A64 `objects.is_cap_revocable`; x86 `revF`). -/ +def revFA : capability.Cap → capability.Cap → Bool + | .Endpoint _ b _ _ _ _, .Endpoint _ b' _ _ _ _ => b != b' + | .Endpoint .., _ => true + | .Notification _ b _ _, .Notification _ b' _ _ => b != b' + | .Notification .., _ => true + | .IrqHandler _, .IrqControl => true + | .Untyped .., _ => true + | .IoPort .., .IoPortControl => true + | _, _ => false + +theorem is_cap_revocable_eqA (d s : capability.Cap) : objects.is_cap_revocable d s = ok (revFA d s) := by + cases d <;> cases s <;> rfl + +/-- A badged cap is derived from a cap of its own kind (x86 `SameBadgeKind`). -/ +def SameBadgeKindA : capability.Cap → capability.Cap → Prop + | .Endpoint .., .Endpoint .. => True + | .Endpoint .., _ => False + | .Notification .., .Notification .. => True + | .Notification .., _ => False + | _, _ => True + +set_option maxHeartbeats 4000000 in +/-- **FL4's revocable bit is l4v's `is_cap_revocable`** (x86 `revF_spec`). -/ +theorem revFA_spec (d s : capability.Cap) (hk : SameBadgeKindA d s) (hd : CapWfA d) : + (revFA d s = true ↔ Fl4SpecA64.isCapRevocable (absCapA d) (absCapA s)) := by + cases d <;> cases s <;> simp only [absCapA, absFrameE] <;> (try split_ifs) <;> simp_all [SameBadgeKindA, revFA, Fl4SpecA64.isCapRevocable, absCapA, CapWfA, + Fl4SpecA64.archIsCapRevocable, Fl4SpecA64.capEpBadge, UScalar.eq_equiv_bv_eq, bne_iff_ne] + +/-- FL4's `set_untyped_cap_as_full` test: both untyped, same region and size. -/ +def FullCondA (sc nc : capability.Cap) : Prop := + ∃ p b i d i' d', sc = .Untyped p b i d ∧ nc = .Untyped p b i' d' + +/-- **FL4's test is l4v's `set_untyped_cap_as_full` test** (x86 `fullF_spec`). -/ +theorem utCondA_iff (sc nc : capability.Cap) : + (Fl4SpecA64.isUntypedCap (absCapA sc) = true ∧ Fl4SpecA64.isUntypedCap (absCapA nc) = true ∧ + Fl4SpecA64.objRefOf (absCapA sc) = Fl4SpecA64.objRefOf (absCapA nc) ∧ + Fl4SpecA64.capBitsUntyped (absCapA sc) = Fl4SpecA64.capBitsUntyped (absCapA nc)) ↔ FullCondA sc nc := by + constructor + · rintro ⟨h1, h2, h3, h4⟩ + cases sc with + | Untyped p b i d => + cases nc with + | Untyped p' b' i' d' => + have e1 : p = p' := specPtr_inj h3 + have e2 : b = b' := UScalar.eq_of_val_eq h4 + subst e1 e2; exact ⟨p, b, i, d, i', d', rfl, rfl⟩ + | _ => simp [absCapA, Fl4SpecA64.isUntypedCap] at h2 + | _ => simp [absCapA, Fl4SpecA64.isUntypedCap] at h1 + · rintro ⟨p, b, i, d, i', d', rfl, rfl⟩; exact ⟨rfl, rfl, rfl, rfl⟩ + +/-- A `NEXT` word's bit 48 as `revA`, from a written value. -/ +theorem revA_of_val {m : U64 → U64} {a : U64} {r : Bool} (h : m (wa a 24#u64) &&& RVB = if r then RVB else 0#u64) : + revA m a = r := by + have e : (281474976710656#u64 : U64) = RVB := rfl + unfold revA; rw [e, h]; cases r <;> rfl + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {T Ω Sg OS : Set U64} + +/-- **The source's cap after `set_untyped_cap_as_full`**, exactly (A64 P1's `.fullU` with the other case named). -/ +theorem setUtFull_capA (hO : OSOK T Ω Sg OS) {s : S} {src : U64} (hsrc : src ∈ OS) {sc nc : capability.Cap} + (hsc : capA st s src = sc) : + cdt.set_untyped_cap_as_full st s sc nc src ⦃ s1 => + (FullCondA sc nc → ∃ p b i d i1, sc = .Untyped p b i d ∧ capA st s1 src = .Untyped p b i1 d ∧ + i1.val = 2 ^ b.val) ∧ (¬ FullCondA sc nc → capA st s1 src = sc) ⦄ := by + have hd : capability.Cap.decode (L.mem s src) (L.mem s (wa src 8#u64)) = ok sc := by + have e := capA_eq (st := st) s src; rw [DPF.slot_cap_dec, hsc] at e; exact e + cases sc with + | Untyped p b i d => + cases nc with + | Untyped p' b' i' d' => + simp only [cdt.set_untyped_cap_as_full] + by_cases e1 : p = p' + · rw [if_pos e1] + by_cases e2 : b = b' + · rw [if_pos e2] + subst e1 e2 + simp only [lift, bind_ok] + have wf0 := CapRt.decode_encWf hd + have hb : b.val < 64 := wf0.2 + refine WP.spec_bind (shl1_val hb) fun i1 hi1 => ?_ + have wf : CapRt.EncWf (.Untyped p b i1 d) := wf0 + obtain ⟨w0, w1, he, hr⟩ := CapRt.decode_encode _ wf + rw [set_cap_eq he, WP.spec_ok] + exact ⟨fun _ => ⟨p, b, i, d, i1, rfl, capA_wr2_self hO hsrc hr, hi1⟩, + fun h => absurd ⟨p, b, i, d, i', d', rfl, rfl⟩ h⟩ + · rw [if_neg e2] + refine (WP.spec_ok _).2 ⟨fun ⟨_, _, _, _, _, _, h1, h2⟩ => ?_, fun _ => hsc⟩ + injection h1 with a1 a2; injection h2 with b1 b2; exact absurd (a2.trans b2.symm) e2 + · rw [if_neg e1] + refine (WP.spec_ok _).2 ⟨fun ⟨_, _, _, _, _, _, h1, h2⟩ => ?_, fun _ => hsc⟩ + injection h1 with a1 a2; injection h2 with b1 b2; exact absurd (a1.trans b1.symm) e1 + | _ => exact (WP.spec_ok _).2 ⟨fun ⟨_, _, _, _, _, _, _, h2⟩ => (by cases h2), fun _ => hsc⟩ + | _ => exact (WP.spec_ok _).2 ⟨fun ⟨_, _, _, _, _, _, h1, _⟩ => (by cases h1), fun _ => hsc⟩ + +set_option maxHeartbeats 2000000 in +/-- **`insert_with_cap`'s bit 48 and source cap, exactly** (module docstring). -/ +theorem insWC_walkA (hO : OSOK T Ω Sg OS) {s : S} {src dest : U64} {nc sc : capability.Cap} + (hsrc : src ∈ OS) (hd : dest ∈ OS) (hsd : src ≠ dest) (hsc : capA st s src = sc) (hw : CapRt.EncWf nc) + (hnN : NxA st s src ≠ 0#u64 → NxA st s src ∈ OS) : + cdt.insert_with_cap st s nc sc src dest ⦃ s' => + (∀ y ∈ OS, y ≠ dest → L.mem s' (wa y 24#u64) &&& RVB = L.mem s (wa y 24#u64) &&& RVB) ∧ + L.mem s' (wa dest 24#u64) &&& RVB = (if revFA nc sc then RVB else 0#u64) ∧ + (FullCondA sc nc → ∃ p b i d i1, sc = .Untyped p b i d ∧ capA st s' src = .Untyped p b i1 d ∧ + i1.val = 2 ^ b.val) ∧ (¬ FullCondA sc nc → capA st s' src = sc) ⦄ := by + unfold cdt.insert_with_cap + rw [is_cap_revocable_eqA, bind_ok, next_eq, bind_ok] + refine WP.spec_bind (WP.spec_and (setUtFull_capA hO hsrc (nc := nc) hsc) + (cdt.set_untyped_cap_as_full.w2 (nc := nc) hsc)) ?_ + rintro s1 ⟨hF, hw2⟩ + have K1 : ∀ y ∈ OS, L.mem s1 (wa y 24#u64) = L.mem s (wa y 24#u64) := by + rcases hw2 with rfl | ⟨w0, w1, rfl⟩ + · exact fun _ _ => rfl + · exact fun y hy => (set_cap_links hO hsrc y hy).2 + obtain ⟨w0, w1, he, -⟩ := CapRt.decode_encode nc hw + rw [set_cap_eq he, bind_ok, set_prev_eq, bind_ok] + have C2 : capA st (L.wr (L.wr s1 dest w0) (wa dest 8#u64) w1) src = capA st s1 src := + capA_wr2_other hO hd hsrc hsd + have K2 : ∀ y ∈ OS, L.mem (L.wr (L.wr (L.wr s1 dest w0) (wa dest 8#u64) w1) (wa dest 16#u64) src) + (wa y 24#u64) = L.mem s (wa y 24#u64) := + fun y hy => (w24_wr16 hO _ hd hy _).trans (((set_cap_links hO hd y hy).2).trans (K1 y hy)) + generalize L.wr (L.wr s1 dest w0) (wa dest 8#u64) w1 = s2 at C2 K2 ⊢ + refine WP.spec_bind (set_nf_rvE _ dest _ _ _) ?_ + rintro s4 ⟨w4, rfl, hw4⟩ + have C5 := (((CK.refl (st := st) (OS := OS) s2).p hO hd src).n hO hd w4).n hO hsrc + ((L.mem (L.wr (L.wr s2 (wa dest 16#u64) src) (wa dest 24#u64) w4) (wa src 24#u64) &&& ~~~ obj.MDB_PTR_MASK) ||| + (dest &&& obj.MDB_PTR_MASK)) + have hck : ∀ s5 : S, (if decide (NxA st s src = 0#u64) = true then ok s5 else + obj.Slot.set_prev st (NxA st s src) s5 dest) ⦃ s6 => CK st OS s5 s6 ⦄ := by + intro s5 + split + · exact (WP.spec_ok _).2 (CK.refl _) + · rename_i h + rw [set_prev_eq, WP.spec_ok] + exact (CK.refl _).p hO (hnN (by simpa using h)) _ + rw [set_next_eq, bind_ok] + simp only [obj.Slot.is_null, bind_ok] + refine WP.spec_mono (WP.spec_and (linkBitA hO _ (p := NxA st s src) (x := dest) ?_ (Or.inr rfl)) (hck _)) ?_ + · exact fun h => hnN (by simpa using h) + rintro s6 ⟨hb6, hc6⟩ + refine ⟨fun y hy hyd => ?_, ?_, fun hc => ?_, fun hc => ?_⟩ + · rw [hb6 y hy, bit_next hO _ hsrc hy dest, w24_wr24 hO _ hd hy hyd, K2 y hy] + · rw [hb6 dest hd, bit_next hO _ hsrc hd dest, w24_self hO _ hd]; exact hw4 + · obtain ⟨p, b, i, d, i1, e1, e2, e3⟩ := hF.1 hc + exact ⟨p, b, i, d, i1, e1, by rw [hc6 src hsrc, C5 src hsrc, C2]; exact e2, e3⟩ + · rw [hc6 src hsrc, C5 src hsrc, C2]; exact hF.2 hc + +end + +/-! ## The ghost tree with the sibling case -/ + +theorem npA_desc_iff {par : U64 → Option U64} {src x : U64} (b : Bool) : + ((if b then some src else par src) = some x ∨ + ∃ p, (if b then some src else par src) = some p ∧ Fl4Proofs.P3.Mdb.Desc par p x) ↔ + (b = true ∧ x = src) ∨ Fl4Proofs.P3.Mdb.Desc par src x := by + cases b + · simp only [Bool.false_eq_true, ↓reduceIte, false_and, false_or] + constructor + · rintro (h | ⟨p, hp, h⟩) + · exact Relation.TransGen.single h + · exact Relation.TransGen.head hp h + · intro h + obtain ⟨p, hp, h2⟩ := Relation.TransGen.head'_iff.1 h + rcases Relation.reflTransGen_iff_eq_or_transGen.1 h2 with rfl | h3 + · exact Or.inl hp + · exact Or.inr ⟨p, hp, h3⟩ + · simp only [↓reduceIte, Option.some.injEq, true_and] + constructor + · rintro (rfl | ⟨p, rfl, h⟩) + · exact Or.inl rfl + · exact Or.inr h + · rintro (rfl | h) + · exact Or.inl rfl + · exact Or.inr ⟨src, rfl, h⟩ + +section gen +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {T Ω Sg OS : Set U64} + {G : List (List U64)} {par : U64 → Option U64} + +set_option maxHeartbeats 2000000 in +/-- (gen 6, ruling 11:49Z q3) **Insertion of `dest` after `src`, as its child (`bp`) or its sibling** (A64 P1's +`UtInvA.ins` over `bp : Bool`, x86 `InsCtx.utinv`): `dest` gets the parent `if bp then some src else par src`; an +untyped source forces the child case (`hb`). -/ +theorem UtInvA.insGenA {s s' : S} (hU : UtInvA st OS par s) (hP : ParIn OS par) {src dest : U64} + (hsrc : src ∈ OS) (hd : dest ∈ OS) (hsd : src ≠ dest) + (hdp : ∀ z, par z ≠ some dest) (hdn : par dest = none) + (hk : ∀ y ∈ OS, y ≠ src → y ≠ dest → capA st s' y = capA st s y) + (hs : UA (capA st s' src) (capA st s src)) (hdA : UA (capA st s' dest) (capA st s src)) + (hfull : ∀ p b i e, capA st s' dest = .Untyped p b i e → + ∃ i1 e1, capA st s' src = .Untyped p b i1 e1 ∧ i1.val = 2 ^ b.val) + (hut : isUtA (capA st s src) = true → ∀ y, ¬ Fl4Proofs.P3.Mdb.Desc par y src) (bp : Bool) + (hb : isUtA (capA st s src) = true → bp = true) : + UtInvA st OS (Function.update par dest (if bp then some src else par src)) s' := by + have hnp : (if bp then some src else par src) ≠ some dest := by cases bp <;> simp [hsd, hdp src] + have hD : ∀ y x, Fl4Proofs.P3.Mdb.Desc (Function.update par dest (if bp then some src else par src)) y x ↔ + Fl4Proofs.P3.Mdb.Desc par y x ∨ (y = dest ∧ ((bp = true ∧ x = src) ∨ Fl4Proofs.P3.Mdb.Desc par src x)) := by + intro y x + rw [Fl4Proofs.P3.Mdb.desc_update_iff hdp hdn hnp, npA_desc_iff] + have ndD : ∀ x, ¬ Fl4Proofs.P3.Mdb.Desc par dest x := fun x => Fl4Proofs.P3.Mdb.not_desc_of_none hdn + have nDd : ∀ y, ¬ Fl4Proofs.P3.Mdb.Desc par y dest := fun y => Fl4Proofs.P3.Mdb.not_desc_of_no_child hdp + have V : ∀ z ∈ OS, z ≠ dest → UA (capA st s' z) (capA st s z) := fun z hz hzd => by + by_cases e : z = src + · rw [e]; exact hs + · rw [hk z hz e hzd]; exact UA.refl _ + have W : ∀ z ∈ OS, ∃ z0 ∈ OS, UA (capA st s' z) (capA st s z0) ∧ (z ≠ dest → z0 = z) ∧ + (z = dest → z0 = src) := fun z hz => by + by_cases hzd : z = dest + · exact ⟨src, hsrc, by rw [hzd]; exact hdA, fun h => absurd hzd h, fun _ => rfl⟩ + · exact ⟨z, hz, V z hz hzd, fun _ => rfl, fun h => absurd h hzd⟩ + -- the source is full when the copy is an `Untyped` + have sfull : ∀ p b i e, capA st s' src = .Untyped p b i e → isUtA (capA st s src) = true → + i.val = 2 ^ b.val := fun p b i e hc hu => by + have hu' : isUtA (capA st s' dest) = true := by rw [hdA.isut]; exact hu + obtain ⟨p', b', i', e', hd'⟩ := isUtA_ex hu' + obtain ⟨i1, e1, hs1, hi1⟩ := hfull _ _ _ _ hd' + rw [hc] at hs1 + injection hs1 with a1 a2 a3 a4 + subst a1 a2 a3 a4 + exact hi1 + refine ⟨fun x hx hp => ?_, fun x hx p b i d hc y hy => ?_, fun x hx p b i d hc y hy hp => ?_, + fun x hx y hy hxy p b i i' d d' hcx hcy => ?_, fun x hx p b i d hc => ?_, + fun x hx p b i d hc y hy hyx hp hd => ?_, fun x y hd hu => ?_⟩ + -- reg + · obtain ⟨x0, hx0, q, -, -⟩ := W x hx + rw [q.phys] at hp; rw [q.base, q.top]; exact hU.reg x0 hx0 hp + -- inc + · rcases (hD y x).1 hy with h0 | ⟨hyd, ⟨-, hxs⟩ | h0⟩ + · have hyd : y ≠ dest := fun e => ndD x (by rw [← e]; exact h0) + have hxd : x ≠ dest := fun e => nDd y (by rw [← e]; exact h0) + obtain ⟨hyO, -⟩ := desc_in hP h0 + obtain ⟨i0, e0, hc0, hle, -⟩ := (V x hx hxd).ut p b i d hc + obtain ⟨g1, g2, g3⟩ := hU.inc x hx p b i0 e0 hc0 y h0 + have q := V y hyO hyd + rw [q.phys, q.base, q.top]; exact ⟨g1, g2, by omega⟩ + · subst y; subst x + obtain ⟨i0, e0, hc0, -, -⟩ := hs.ut p b i d hc + have hi := sfull p b i d hc (by rw [hc0]; rfl) + have hr := hU.reg src hsrc (by rw [hc0]; exact physA_ut _ _ _ _) + rw [hc0] at hr + obtain ⟨r1, r2, -⟩ := ut_rangeA (capA_ut_b hc0) hr + have h2 : 1 ≤ 2 ^ b.val := Nat.one_le_two_pow + rw [hdA.phys, hdA.base, hdA.top, hc0, r1, r2, physA_ut] + exact ⟨rfl, le_refl _, by omega⟩ + · subst y + have hxd : x ≠ dest := fun e => nDd src (by rw [← e]; exact h0) + obtain ⟨i0, e0, hc0, hle, -⟩ := (V x hx hxd).ut p b i d hc + obtain ⟨g1, g2, g3⟩ := hU.inc x hx p b i0 e0 hc0 src h0 + rw [hdA.phys, hdA.base, hdA.top]; exact ⟨g1, g2, by omega⟩ + -- noov + · obtain ⟨x0, hx0, qx, -, -⟩ := W x hx + obtain ⟨y0, hy0, qy, -, -⟩ := W y hy + obtain ⟨i0, e0, hc0, hle, -⟩ := qx.ut p b i d hc + rw [qy.phys] at hp + rcases hU.noov x0 hx0 p b i0 e0 hc0 y0 hy0 hp with g | ⟨g1, g2⟩ + · left; unfold DisjRA at g ⊢; rw [qy.base, qy.top]; omega + · right; rw [qy.isut]; refine ⟨g1, ?_⟩ + unfold WithinA at g2 ⊢; rw [qx.base, qx.top, qy.base, qy.top]; exact g2 + -- excl + · obtain ⟨x0, hx0, qx, hxn, hxd'⟩ := W x hx + obtain ⟨y0, hy0, qy, hyn, hyd'⟩ := W y hy + obtain ⟨g0, f0, hg0, hgl, -⟩ := qx.ut _ _ _ _ hcx + obtain ⟨g0', f0', hg0', hgl', -⟩ := qy.ut _ _ _ _ hcy + by_cases hxy0 : x0 = y0 + · by_cases hx' : x = dest + · have hy' : y ≠ dest := fun e => hxy (hx'.trans e.symm) + have e1 : y = src := by rw [← hyn hy', ← hxy0, hxd' hx'] + have ey : y0 = src := by rw [hyn hy', e1] + rw [e1] at hcy + exact Or.inr (le_of_eq (sfull _ _ _ _ hcy (by rw [← ey, hg0']; rfl)).symm) + · have e1 : x = src := by + rw [← hxn hx'] + by_cases hy' : y = dest + · rw [hxy0, hyd' hy'] + · exact absurd ((hxn hx').symm.trans (hxy0.trans (hyn hy'))) hxy + have ex : x0 = src := by rw [hxn hx', e1] + rw [e1] at hcx + exact Or.inl (le_of_eq (sfull _ _ _ _ hcx (by rw [← ex, hg0]; rfl)).symm) + · rcases hU.excl x0 hx0 y0 hy0 hxy0 _ _ _ _ _ _ hg0 hg0' with g | g + · left; omega + · right; omega + -- fib + · obtain ⟨x0, hx0, q, -, -⟩ := W x hx + obtain ⟨i0, e0, hc0, -, hor⟩ := q.ut p b i d hc + rcases hor with h | h + · rw [h]; exact hU.fib x0 hx0 _ _ _ _ hc0 + · exact h + -- drange + · have up : ∀ {a z}, Fl4Proofs.P3.Mdb.Desc par a z → Fl4Proofs.P3.Mdb.Desc (Function.update par dest (if bp then some src else par src)) a z := + fun e => (hD _ _).2 (Or.inl e) + have dn : ∀ {z}, (bp = true ∧ z = src ∨ Fl4Proofs.P3.Mdb.Desc par src z) → Fl4Proofs.P3.Mdb.Desc (Function.update par dest (if bp then some src else par src)) dest z := + fun e => (hD _ _).2 (Or.inr ⟨rfl, e⟩) + by_cases hxD : x = dest + · subst x + have qy := V y hy hyx + obtain ⟨i0, e0, hc0, -, -⟩ := hdA.ut p b i d hc + have hsu : isUtA (capA st s src) = true := by rw [hc0]; rfl + rw [qy.phys] at hp + have hd0 : ¬ DisjA (capA st s y) (capA st s src) := by + unfold DisjA at hd ⊢; rw [qy.base, qy.top, hdA.base, hdA.top] at hd; exact hd + by_cases hys : y = src + · right; rw [hys]; exact dn (Or.inl ⟨hb hsu, rfl⟩) + · rcases hU.drange src hsrc _ _ _ _ hc0 y hy hys hp hd0 with g | g + · exact absurd g (hut hsu y) + · exact Or.inr (dn (Or.inr g)) + · have qx := V x hx hxD + obtain ⟨i0, e0, hc0, -, -⟩ := qx.ut p b i d hc + by_cases hyD : y = dest + · subst y + rw [hdA.phys] at hp + have hd0 : ¬ DisjA (capA st s src) (capA st s x) := by + unfold DisjA at hd ⊢; rw [qx.base, qx.top, hdA.base, hdA.top] at hd; exact hd + by_cases hxs : x = src + · left; rw [hxs]; exact dn (Or.inl ⟨hb (by rw [← hxs, hc0]; rfl), rfl⟩) + · rcases hU.drange x hx _ _ _ _ hc0 src hsrc (Ne.symm hxs) hp hd0 with g | g + · exact Or.inl (dn (Or.inr g)) + · exact absurd g (hut (hU.anc src x g (by rw [hc0]; rfl)) x) + · have qy := V y hy hyD + rw [qy.phys] at hp + have hd0 : ¬ DisjA (capA st s y) (capA st s x) := by + unfold DisjA at hd ⊢; rw [qx.base, qx.top, qy.base, qy.top] at hd; exact hd + rcases hU.drange x hx _ _ _ _ hc0 y hy hyx hp hd0 with g | g + · exact Or.inl (up g) + · exact Or.inr (up g) + -- anc + · rcases (hD y x).1 hd with h0 | ⟨hyd, ⟨-, hxs⟩ | h0⟩ + · have hyd : y ≠ dest := fun e => ndD x (by rw [← e]; exact h0) + have hxd : x ≠ dest := fun e => nDd y (by rw [← e]; exact h0) + obtain ⟨hyO, hxO⟩ := desc_in hP h0 + rw [(V y hyO hyd).isut] at hu; rw [(V x hxO hxd).isut]; exact hU.anc x y h0 hu + · rw [hyd, hdA.isut] at hu; rw [hxs, hs.isut]; exact hu + · rw [hyd, hdA.isut] at hu + have hxO := (desc_in hP h0).2 + have hxd : x ≠ dest := fun e => nDd src (by rw [← e]; exact h0) + rw [(V x hxO hxd).isut]; exact hU.anc x src h0 hu + + + +set_option maxHeartbeats 2000000 in +/-- (gen 6) **`insert_with_cap` as `src`'s child (`bp`) or sibling** (A64 P1's `cdt.insert_with_cap.tree` over +`bp`, ruling 11:49Z q3; `Fl4Proofs.P3.Mdb.InsCase` for the tree, `UtInvA.insGenA` for the untyped invariant). -/ +theorem insWC_treeGA (hO : OSOK T Ω Sg OS) {s : S} (hT : TreeA st OS G par s) {src dest : U64} + {nc sc : capability.Cap} (hsrc : src ∈ G.flatten) (hd : dest ∈ OS) (hd0 : capA st s dest = .Null) + (hdc : obj.sext48 dest = ok dest) (hsc : capA st s src = sc) (hw : CapRt.EncWf nc) (hnc : nc ≠ .Null) + (hA : UA nc sc) (hut : isUtA sc = true → ∀ y, ¬ Fl4Proofs.P3.Mdb.Desc par y src) (bp : Bool) + (hb : isUtA sc = true → bp = true) (hsib : bp = false → ∀ y, ¬ Fl4Proofs.P3.Mdb.Desc par y src) : + cdt.insert_with_cap st s nc sc src dest ⦃ s' => TreeA st OS (insG src dest G) + (Function.update par dest (if bp then some src else par src)) s' ⦄ := by + have hM := hT.lk + have h0 : ∀ y ∈ OS, y ≠ 0#u64 := hO.nz + have inOS : ∀ y ∈ G.flatten, y ∈ OS := fun y hy => ((hM.members y).1 hy).1 + have F := hM.lkF h0 + have os := inOS src hsrc + have hs0 : capA st s src ≠ .Null := ((hM.members src).1 hsrc).2 + have hsc0 : sc ≠ .Null := by rw [← hsc]; exact hs0 + have dG : dest ∉ G.flatten := fun hm => ((hM.members dest).1 hm).2 hd0 + have hsd : src ≠ dest := fun e => dG (e ▸ hsrc) + have hNm : NxA st s src ≠ 0#u64 → NxA st s src ∈ G.flatten := F.nxm _ hsrc + have hNd : NxA st s src ≠ dest := fun e => by + have h1 : NxA st s src ≠ 0#u64 := fun e0 => h0 dest hd (e ▸ e0) + exact dG (e ▸ hNm h1) + refine WP.spec_mono (WP.spec_and (cdt.insert_with_cap.lr hO (LR.self s) os hd hsd hdc hsc hsc0 hw + (fun h => inOS _ (hNm h))) (cdt.insert_with_cap.srcU hO os hd hsd hsc hw (fun h => inOS _ (hNm h)))) ?_ + rintro s' ⟨⟨hL, ed, -, ns, ek, -⟩, hsu⟩ + have hk : ∀ y ∈ OS, y ≠ src → y ≠ dest → capA st s' y = capA st s y := ek + have hLk : MdbLk st OS (insG src dest G) s' := by + refine hM.ins h0 hsrc hd hd0 (by rw [ed]; exact hnc) (fun y hy hyd => ?_) ?_ ?_ ?_ (fun hN => ?_) + (fun y hy h1 h2 => ?_) (fun y hy h1 h2 => ?_) + · by_cases e : y = src + · rw [e] + exact ⟨fun h => absurd h ns, fun h => absurd h hs0⟩ + · rw [hk y hy e hyd] + · rw [(hL src os).2]; simp + · rw [(hL dest hd).1]; simp [hNd.symm] + · rw [(hL dest hd).2]; simp [hsd.symm] + · rw [(hL _ (inOS _ (hNm hN))).1]; simp [hN] + · rw [(hL y hy).2]; simp [h1, h2] + · rw [(hL y hy).1]; simp [h1, h2] + have hfib : ∀ p b i e, capA st s src = .Untyped p b i e → i.val ≤ 2 ^ b.val := + fun p b i e h => hT.ut.fib src os p b i e h + have hs : UA (capA st s' src) (capA st s src) := by + rw [hsc]; exact hsu.1.ua fun p b i e h => hfib p b i e (hsc.trans h) + have hdA : UA (capA st s' dest) (capA st s src) := by rw [ed, hsc]; exact hA + refine ⟨hLk, by rw [insG_eq]; exact hT.tree.insert hsrc dG (by cases bp; exacts [Or.inr ⟨rfl, hsib rfl⟩, Or.inl rfl]), ?_⟩ + refine UtInvA.insGenA hT.ut hT.parIn os hd hsd (fun z e => dG (hT.tree.par_mem z dest e).2) ?_ hk hs hdA + (fun p b i e h => ?_) (fun hu => hut (by rw [← hsc]; exact hu)) bp (fun hu => hb (by rw [← hsc]; exact hu)) + · cases e : par dest with + | none => rfl + | some q => exact absurd (hT.tree.par_mem dest q e).1 dG + · rw [ed] at h + obtain ⟨i0, e0, hsc0', -, -⟩ := hA.ut p b i e h + obtain ⟨i1, h1, h2⟩ := hsu.2 p b i0 e0 i e hsc0' h + exact ⟨i1, e0, h1, h2⟩ + +end gen + +/-! ## The spec's outcome -/ + +/-- The state after `cap_insert`'s `set_cap`: its CDT and original-cap updates (x86 `capInsert_eq`'s state). -/ +def insFinA (σ : State Unit) (src dst : CslotPtr) (b v : Bool) : State Unit := + { σ with + cdt := Function.update σ.cdt dst (if b then some src else σ.cdt src) + isOriginalCap := Function.update σ.isOriginalCap dst v } + +open Classical in +/-- l4v's parent test of `cap_insert` (`should_be_parent_of`, with `is_cap_revocable` for the new cap's flag). -/ +def insBA (σ : State Unit) (src : CslotPtr) (a asc : Fl4SpecA64.Cap) : Bool := + decide (Fl4SpecA64.shouldBeParentOf asc (σ.isOriginalCap src) a (decide (Fl4SpecA64.isCapRevocable a asc))) + +open Classical in +/-- **An outcome of `cap_insert`** (x86 `capInsert_eq`, as a membership). -/ +theorem capInsert_memA {σ σ1 σ2 : State Unit} {a asc : Fl4SpecA64.Cap} {src dst : CslotPtr} + (hgs : (asc, σ) ∈ (getCap (Z := Unit) src σ).1) (hgd : (Fl4SpecA64.Cap.nullCap, σ) ∈ (getCap (Z := Unit) dst σ).1) + (h1 : ((), σ1) ∈ (Fl4SpecA64.setUntypedCapAsFull (Z := Unit) asc a src σ).1) + (h2 : ((), σ2) ∈ (setCap (Z := Unit) a dst σ1).1) : + ((), insFinA σ2 src dst (decide (Fl4SpecA64.shouldBeParentOf asc (σ2.isOriginalCap src) a + (decide (Fl4SpecA64.isCapRevocable a asc)))) (decide (Fl4SpecA64.isCapRevocable a asc))) ∈ + (Fl4SpecA64.capInsert (Z := Unit) a src dst σ).1 := by + unfold Fl4SpecA64.capInsert + refine mem_bindA.2 ⟨(asc, σ), hgs, ?_⟩ + refine mem_bindA.2 ⟨(_, σ), rfl, ?_⟩ + refine mem_bindA.2 ⟨(.nullCap, σ), hgd, ?_⟩ + refine mem_bindA.2 ⟨((), σ), ⟨rfl, rfl⟩, ?_⟩ + refine mem_bindA.2 ⟨((), σ1), h1, ?_⟩ + refine mem_bindA.2 ⟨((), σ2), h2, ?_⟩ + refine mem_bindA.2 ⟨(σ2.isOriginalCap, σ2), rfl, ?_⟩ + refine mem_bindA.2 ⟨(_, σ2), rfl, ?_⟩ + refine mem_bindA.2 ⟨(σ2.cdt src, σ2), rfl, ?_⟩ + refine mem_bindA.2 ⟨(σ2.cdt dst, σ2), rfl, ?_⟩ + refine mem_bindA.2 ⟨((), _), mem_bindA.2 ⟨(σ2.cdt, σ2), rfl, mem_bindA.2 ⟨(σ2, σ2), rfl, rfl⟩⟩, ?_⟩ + refine mem_bindA.2 ⟨((), _), doExtendedOp_memA _ _, ?_⟩ + exact mem_bindA.2 ⟨(_, _), rfl, rfl⟩ + +theorem SpecSlotA.insFin (σ : State Unit) (src dst : CslotPtr) (b v : Bool) : SpecSlotA σ (insFinA σ src dst b v) := + ⟨⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, ⟨rfl, rfl, rfl, rfl⟩⟩, fun _ => Or.inl rfl⟩ + +/-- `StateRelA` across a memory with the same caps at every slot word. -/ +theorem StateRelA.congrA {th : TypedHeapA} {mem mem' : U64 → U64} {σ : State Unit} (h : StateRelA th mem σ) + (hc : ∀ y, SlotWA th y → capAtA mem' y = capAtA mem y) : StateRelA th mem' σ where + cnode p r hp := by + obtain ⟨cs, h1, h2, h3⟩ := h.cnode p r hp + refine ⟨cs, h1, h2, fun idx hl => ?_⟩ + obtain ⟨ac, e, hr⟩ := h3 idx hl + exact ⟨ac, e, by rw [hc _ (Or.inr ⟨p, r, hp, idx, hl, rfl⟩)]; exact hr⟩ + tcb t ht := by + obtain ⟨x, h1, h2⟩ := h.tcb t ht + refine ⟨x, h1, fun i hi => ?_⟩ + obtain ⟨ac, e, hr⟩ := h2 i hi + exact ⟨ac, e, by rw [hc _ (Or.inl ⟨t, Or.inr ⟨ht, i, hi, rfl⟩⟩)]; exact hr⟩ + +/-! ## The CDT after `cap_insert` -/ + +/-- The ghost after inserting `dest` after `src`, as its child iff `b` (x86 `insG`). -/ +def insGA (G : MdbGhostA) (src dest : U64) (b : Bool) : MdbGhostA := + ⟨insG src dest G.chains, Function.update G.par dest (if b then some src else G.par src)⟩ + +section cdt +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **The spec's CDT after `cap_insert`** against the ghost with `dest` inserted (x86 `CdtRel.insert`). -/ +theorem CdtRelA.insertA {th : TypedHeapA} {kb : U64} (hP : PlaceA th kb) {s s' : S} {σ σ' : State Unit} + {G : MdbGhostA} (hC : CdtRelA st th s σ G) {chains' : List (List U64)} {srcSl destSl : CslotPtr} + (hs : AbsSlotA th srcSl) (hd : AbsSlotA th destSl) (b v : Bool) + (htr : TreeA st {a | IsSlotA th a} chains' (Function.update G.par (cslotAddrA destSl) + (if b then some (cslotAddrA srcSl) else G.par (cslotAddrA srcSl))) s') + (hcdt : σ'.cdt = Function.update σ.cdt destSl (if b then some srcSl else σ.cdt srcSl)) + (hio : σ'.isOriginalCap = Function.update σ.isOriginalCap destSl v) + (hrev : ∀ sl, AbsSlotA th sl → + revA (L.mem s') (cslotAddrA sl) = if sl = destSl then v else revA (L.mem s) (cslotAddrA sl)) + (hne : srcSl ≠ destSl) (hb : b = true → σ.isOriginalCap srcSl = true) (hnd : ∀ sl, σ.cdt sl ≠ some destSl) : + CdtRelA st th s' σ' ⟨chains', Function.update G.par (cslotAddrA destSl) + (if b then some (cslotAddrA srcSl) else G.par (cslotAddrA srcSl))⟩ := by + refine CdtRelA.mkA htr ?_ ?_ ?_ ?_ ?_ + · intro sl hsl + show Option.map cslotAddrA (σ'.cdt sl) = Function.update G.par _ _ (cslotAddrA sl) + rw [hcdt] + by_cases he : sl = destSl + · subst he + simp only [Function.update_self] + cases b + · simpa using hC.cdt srcSl hs + · simp + · have hne : cslotAddrA sl ≠ cslotAddrA destSl := fun e => he (cslotAddrA_inj hP hsl hd e) + simp only [Function.update_of_ne he, Function.update_of_ne hne] + exact hC.cdt sl hsl + · intro sl q hq + rw [hcdt] at hq + by_cases he : sl = destSl + · subst he + simp only [Function.update_self] at hq + cases b + · exact ⟨hd, (hC.cdt_slot _ _ (by simpa using hq)).2⟩ + · simp at hq; subst hq; exact ⟨hd, hs⟩ + · rw [Function.update_of_ne he] at hq; exact hC.cdt_slot sl q hq + · intro sl hsl + rw [hio, hrev _ hsl] + by_cases he : sl = destSl + · subst he; simp + · simp only [Function.update_of_ne he, he, ↓reduceIte] + exact hC.orig sl hsl + · intro a x hax + by_cases he : a = cslotAddrA destSl + · exact ⟨destSl, hd, he.symm⟩ + · change Function.update G.par _ _ a = some x at hax + rw [Function.update_of_ne he] at hax + exact hC.par_slot a x hax + · -- (gen 7) every parent is original: `dest`'s parent is `src` only when `b` (then original, `hb`) + intro sl q hq + rw [hcdt] at hq + rw [hio] + by_cases he : sl = destSl + · subst he + simp only [Function.update_self] at hq + cases b + · simp only [Bool.false_eq_true, ↓reduceIte] at hq + rw [Function.update_of_ne (fun e => hnd srcSl (by rw [← e]; exact hq))]; exact hC.po _ _ hq + · simp only [↓reduceIte, Option.some.injEq] at hq + subst hq + rw [Function.update_of_ne hne]; exact hb rfl + · rw [Function.update_of_ne he] at hq + rw [Function.update_of_ne (fun e => hnd sl (by rw [← e]; exact hq))]; exact hC.po _ _ hq + +end cdt + +/-! ## The joint leaf -/ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +private theorem CapEqA.isCapRevocable_eq {nc sc : capability.Cap} {anc asc : Fl4SpecA64.Cap} + (h1 : CapEqA nc anc) (h2 : CapEqA sc asc) : + Fl4SpecA64.isCapRevocable anc asc ↔ Fl4SpecA64.isCapRevocable (absCapA nc) (absCapA sc) := by + have h1' : Fl4SpecA64.isCapRevocable anc asc ↔ Fl4SpecA64.isCapRevocable (absCapA nc) asc := by + cases nc with + | PageTable p m asid va => + cases m + · rw [← h1] + · obtain ⟨_, _, rfl, _, _⟩ := CapEqA.pt_mapped_iff.mp h1 + simp [absCapA, mapDataA, Fl4SpecA64.isCapRevocable, Fl4SpecA64.archIsCapRevocable] + | _ => rw [← h1] + rw [h1'] + cases sc with + | PageTable p m asid va => + cases m + · rw [← h2] + · obtain ⟨_, _, rfl, _, _⟩ := CapEqA.pt_mapped_iff.mp h2 + cases nc <;> simp only [absCapA, absFrameE] <;> (try split_ifs) <;> + simp [mapDataA, Fl4SpecA64.isCapRevocable, Fl4SpecA64.archIsCapRevocable, Fl4SpecA64.capEpBadge] + | _ => rw [← h2] + +private theorem CapEqA.setUntypedCapAsFull_eq {sc nc : capability.Cap} {asc anc : Fl4SpecA64.Cap} + (h1 : CapEqA sc asc) (h2 : CapEqA nc anc) (ss : CslotPtr) (σ : State Unit) : + Fl4SpecA64.setUntypedCapAsFull (Z := Unit) asc anc ss σ = + Fl4SpecA64.setUntypedCapAsFull (Z := Unit) (absCapA sc) (absCapA nc) ss σ := by + have h1' : Fl4SpecA64.setUntypedCapAsFull (Z := Unit) asc anc ss σ = + Fl4SpecA64.setUntypedCapAsFull (Z := Unit) (absCapA sc) anc ss σ := by + cases sc with + | PageTable p m asid va => + cases m + · rw [← h1] + · obtain ⟨_, _, rfl, _, _⟩ := CapEqA.pt_mapped_iff.mp h1 + simp [Fl4SpecA64.setUntypedCapAsFull, Fl4SpecA64.isUntypedCap, absCapA] + | _ => rw [← h1] + rw [h1'] + cases nc with + | PageTable p m asid va => + cases m + · rw [← h2] + · obtain ⟨_, _, rfl, _, _⟩ := CapEqA.pt_mapped_iff.mp h2 + simp [Fl4SpecA64.setUntypedCapAsFull, Fl4SpecA64.isUntypedCap, absCapA] + | _ => rw [← h2] + +private theorem CapEqA.shouldBeParentOf_eq {sc nc : capability.Cap} {asc anc : Fl4SpecA64.Cap} + (h1 : CapEqA sc asc) (h2 : CapEqA nc anc) (o r : Bool) : + Fl4SpecA64.shouldBeParentOf asc o anc r ↔ Fl4SpecA64.shouldBeParentOf (absCapA sc) o (absCapA nc) r := by + have hsr : Fl4SpecA64.sameRegionAs asc anc ↔ Fl4SpecA64.sameRegionAs (absCapA sc) (absCapA nc) := by + have ha : Fl4SpecA64.sameRegionAs asc anc ↔ Fl4SpecA64.sameRegionAs (absCapA sc) anc := by + cases sc with + | PageTable p m asid va => + cases m + · rw [← h1] + · obtain ⟨_, _, rfl, _, _⟩ := CapEqA.pt_mapped_iff.mp h1 + cases anc <;> simp [Fl4SpecA64.sameRegionAs, Fl4SpecA64.archSameRegionAs, absCapA, mapDataA] + | _ => rw [← h1] + rw [ha] + cases nc with + | PageTable p m asid va => + cases m + · rw [← h2] + · obtain ⟨w1, w2, rfl, _, _⟩ := CapEqA.pt_mapped_iff.mp h2 + have hp1 : Fl4SpecA64.isPhysical (.archObjectCap (.pageTableCap (specPtr p) .normalPtT (some (w1, w2)))) = + Fl4SpecA64.isPhysical (.archObjectCap (.pageTableCap (specPtr p) .normalPtT (some (asidOfA asid, va.bv)))) := rfl + have hp2 : Fl4SpecA64.objRefOf (.archObjectCap (.pageTableCap (specPtr p) .normalPtT (some (w1, w2)))) = + Fl4SpecA64.objRefOf (.archObjectCap (.pageTableCap (specPtr p) .normalPtT (some (asidOfA asid, va.bv)))) := rfl + have hp3 : Fl4SpecA64.objSize (.archObjectCap (.pageTableCap (specPtr p) .normalPtT (some (w1, w2)))) = + Fl4SpecA64.objSize (.archObjectCap (.pageTableCap (specPtr p) .normalPtT (some (asidOfA asid, va.bv)))) := rfl + have hp4 : Fl4SpecA64.isIrqControlDescendant (.pageTableCap (specPtr p) .normalPtT (some (w1, w2))) = + Fl4SpecA64.isIrqControlDescendant (.pageTableCap (specPtr p) .normalPtT (some (asidOfA asid, va.bv))) := rfl + generalize absCapA sc = asc0 + cases asc0 with + | archObjectCap ac => + cases ac <;> simp [Fl4SpecA64.sameRegionAs, Fl4SpecA64.archSameRegionAs, + Fl4SpecA64.ArchCap.isFrameCap, Fl4SpecA64.ArchCap.isSMCCap, Fl4SpecA64.ArchCap.acapObj, + Fl4SpecA64.ArchCap.acapFsize, absCapA, mapDataA] + | _ => + simp [Fl4SpecA64.sameRegionAs, Fl4SpecA64.isEpCap, Fl4SpecA64.isNtfnCap, Fl4SpecA64.isCnodeCap, + Fl4SpecA64.isThreadCap, Fl4SpecA64.isSchedContextCap, Fl4SpecA64.isArchCap, + Fl4SpecA64.Cap.theArchCap, absCapA, mapDataA, hp1, hp2, hp3, hp4] + | _ => rw [← h2] + have h1' : Fl4SpecA64.shouldBeParentOf asc o anc r ↔ + (o ∧ Fl4SpecA64.sameRegionAs (absCapA sc) (absCapA nc) ∧ + (match absCapA sc with + | .endpointCap _ badge _ => badge ≠ 0 → Fl4SpecA64.capEpBadge (absCapA nc) = badge ∧ ¬r + | .notificationCap _ badge _ => badge ≠ 0 → Fl4SpecA64.capEpBadge (absCapA nc) = badge ∧ ¬r + | .archObjectCap ac => Fl4SpecA64.shouldBeArchParentOf ac (absCapA nc).theArchCap r + | _ => True)) := by + unfold Fl4SpecA64.shouldBeParentOf + rw [hsr] + have hbd : Fl4SpecA64.capEpBadge anc = Fl4SpecA64.capEpBadge (absCapA nc) := by + cases nc with + | PageTable p m asid va => + cases m + · rw [← h2] + · obtain ⟨_, _, rfl, _, _⟩ := CapEqA.pt_mapped_iff.mp h2; rfl + | _ => rw [← h2] + have harch : ∀ ac, Fl4SpecA64.shouldBeArchParentOf ac anc.theArchCap r ↔ + Fl4SpecA64.shouldBeArchParentOf ac (absCapA nc).theArchCap r := by + intro ac + cases nc with + | PageTable p m asid va => + cases m + · rw [← h2] + · obtain ⟨_, _, rfl, _, _⟩ := CapEqA.pt_mapped_iff.mp h2 + simp [Fl4SpecA64.shouldBeArchParentOf, Fl4SpecA64.Cap.theArchCap, Fl4SpecA64.ArchCap.acapSmcBadge, absCapA, mapDataA] + | _ => rw [← h2] + cases sc with + | PageTable p m asid va => + cases m + · rw [← h1]; simp [absCapA, harch] + · obtain ⟨_, _, rfl, _, _⟩ := CapEqA.pt_mapped_iff.mp h1 + simp [absCapA, mapDataA, Fl4SpecA64.shouldBeArchParentOf, Fl4SpecA64.ArchCap.isSGISignalCap, + Fl4SpecA64.ArchCap.isSMCCap] + | _ => + rw [← h1] + cases absCapA _ <;> simp [hbd, harch] + exact h1' + +open Classical in +set_option maxHeartbeats 4000000 in +/-- **FL4's `cdt::insert` refines l4v `cap_insert`** between two slots of the typed heap (gen 8: CNode or +TCB slots, `AbsSlotA`), child or sibling (module docstring; +x86 `insert_refines`). -/ +theorem cap_insert_refinesA {T Ω Sg OS : Set U64} {th : TypedHeapA} {kb : U64} (s : S) (σ : State Unit) + (G : MdbGhostA) (hR : StateRelA th (L.mem s) σ) (hC : CdtRelA st th s σ G) (hP : PlaceA th kb) + (hO : OSOK T Ω Sg OS) (hT : TreeA st OS G.chains G.par s) (hsub : ∀ y, IsSlotA th y → y ∈ OS) + (hnul : ∀ y ∈ OS, ¬ IsSlotA th y → capA st s y = .Null) + {ss sd : CslotPtr} (hAs : AbsSlotA th ss) (hAd : AbsSlotA th sd) + (hsn : capAtA (L.mem s) (cslotAddrA ss) ≠ .Null) + (hdn : capAtA (L.mem s) (cslotAddrA sd) = .Null) + (hdc : obj.sext48 (cslotAddrA sd) = ok (cslotAddrA sd)) + {nc : capability.Cap} {anc : Fl4SpecA64.Cap} (hwf : CapWfA nc) (hanc : CapEqA nc anc) + (hw : CapRt.EncWf nc) (hn : nc ≠ .Null) + (hal : UA nc (capA st s (cslotAddrA ss))) + (hk : SameBadgeKindA nc (capA st s (cslotAddrA ss))) + (hut : isUtA (capA st s (cslotAddrA ss)) = true → + ∀ y, ¬ Fl4Proofs.P3.Mdb.Desc G.par y (cslotAddrA ss)) + (hub : isUtA (capA st s (cslotAddrA ss)) = true → + insBA σ ss (absCapA nc) (absCapA (capA st s (cslotAddrA ss))) = true) + (hsib : insBA σ ss (absCapA nc) (absCapA (capA st s (cslotAddrA ss))) = false → + ∀ y, ¬ Fl4Proofs.P3.Mdb.Desc G.par y (cslotAddrA ss)) : + cdt.insert st s nc (cslotAddrA ss) (cslotAddrA sd) ⦃ s' => + ∃ σ', ((), σ') ∈ (Fl4SpecA64.capInsert (Z := Unit) anc ss sd σ).1 ∧ + StateRelA th (L.mem s') σ' ∧ + CdtRelA st th s' σ' (insGA G (cslotAddrA ss) (cslotAddrA sd) + (insBA σ ss (absCapA nc) (absCapA (capA st s (cslotAddrA ss))))) ∧ + TreeA st OS (insG (cslotAddrA ss) (cslotAddrA sd) G.chains) + (insGA G (cslotAddrA ss) (cslotAddrA sd) + (insBA σ ss (absCapA nc) (absCapA (capA st s (cslotAddrA ss))))).par s' ∧ + SpecSlotA σ σ' ∧ + ∀ y, IsSlotA th y → capAtA (L.mem s') y = capAtA (L.mem s) y ∨ capAtA (L.mem s') y = nc ∨ + ∃ p b i e, capAtA (L.mem s') y = .Untyped p b i e ⦄ := by + have hsS := hAs.isSlot hP + have hdS := hAd.isSlot hP + have hsG : cslotAddrA ss ∈ G.chains.flatten := (hT.lk.members _).2 ⟨hsub _ hsS, hsn⟩ + have hdG : cslotAddrA sd ∉ G.chains.flatten := fun m => ((hT.lk.members _).1 m).2 hdn + have hsd : cslotAddrA ss ≠ cslotAddrA sd := fun e => hdG (e ▸ hsG) + have hne : ss ≠ sd := fun e => hsd (by rw [e]) + have F := hT.lk.lkF hO.nz + have inOS : ∀ y ∈ G.chains.flatten, y ∈ OS := fun y hy => ((hT.lk.members y).1 hy).1 + obtain ⟨asc, hgs, hsrel⟩ := hR.getAbsA hAs + obtain ⟨adc, hgd, hdrel⟩ := hR.getAbsA hAd + have hadc : adc = .nullCap := by + have h := hdrel.2 + rw [hdn] at h; exact h.symm + subst hadc + have hisRev : Fl4SpecA64.isCapRevocable anc asc ↔ + Fl4SpecA64.isCapRevocable (absCapA nc) (absCapA (capA st s (cslotAddrA ss))) := + CapEqA.isCapRevocable_eq hanc hsrel.2 + have hsetUtEq : Fl4SpecA64.setUntypedCapAsFull (Z := Unit) asc anc ss σ = + Fl4SpecA64.setUntypedCapAsFull (Z := Unit) (absCapA (capA st s (cslotAddrA ss))) (absCapA nc) ss σ := + CapEqA.setUntypedCapAsFull_eq hsrel.2 hanc ss σ + have hv : revFA nc (capA st s (cslotAddrA ss)) = + decide (Fl4SpecA64.isCapRevocable (absCapA nc) (absCapA (capA st s (cslotAddrA ss)))) := by + rw [Bool.eq_iff_iff]; simp only [decide_eq_true_eq]; exact revFA_spec _ _ hk hwf + unfold cdt.insert + rw [capA_eq, bind_ok] + refine WP.spec_mono (WP.spec_and (WP.spec_and (insWC_treeGA hO hT hsG (hsub _ hdS) hdn hdc rfl hw hn hal hut + (insBA σ ss (absCapA nc) (absCapA (capA st s (cslotAddrA ss)))) hub hsib) + (cdt.insert_with_cap.lr hO (LR.self s) (hsub _ hsS) (hsub _ hdS) hsd hdc rfl hsn hw + (fun h => inOS _ (F.nxm _ hsG h)))) + (insWC_walkA hO (hsub _ hsS) (hsub _ hdS) hsd rfl hw (fun h => inOS _ (F.nxm _ hsG h)))) ?_ + rintro s' ⟨⟨hT', -, ed, -, -, ek, -⟩, hb, hdb, hfu, hnf⟩ + -- the memory with `dest`'s cap words from before + have hm1 : ∀ y, IsSlotA th y → y ≠ cslotAddrA ss → + capAtA (mixA (L.mem s) (L.mem s') (cslotAddrA sd)) y = capAtA (L.mem s) y := by + intro y hy hys + by_cases e : y = cslotAddrA sd + · rw [e]; exact capAt_mixA_self _ _ _ + · rw [capAt_mixA_other _ _ (slot_ne8 hP hdS hy e)]; exact ek y (hsub y hy) hys e + have hms : capAtA (mixA (L.mem s) (L.mem s') (cslotAddrA sd)) (cslotAddrA ss) = + capA st s' (cslotAddrA ss) := capAt_mixA_other _ _ (slot_ne8 hP hdS hsS hsd) + -- `set_untyped_cap_as_full` + obtain ⟨σ1, h1, hR1, hSS1, hk1⟩ : ∃ σ1, ((), σ1) ∈ (Fl4SpecA64.setUntypedCapAsFull (Z := Unit) + (absCapA (capA st s (cslotAddrA ss))) (absCapA nc) ss σ).1 ∧ + StateRelA th (mixA (L.mem s) (L.mem s') (cslotAddrA sd)) σ1 ∧ SpecSlotA σ σ1 ∧ + σ1.cdt = σ.cdt ∧ σ1.isOriginalCap = σ.isOriginalCap := by + by_cases hc : FullCondA (capA st s (cslotAddrA ss)) nc + · obtain ⟨p, b, i, d, i1, e1, e2, e3⟩ := hfu hc + have hcf : CapRelA (capAtA (mixA (L.mem s) (L.mem s') (cslotAddrA sd)) (cslotAddrA ss)) + (Fl4SpecA64.maxFreeIndexUpdate (absCapA (capA st s (cslotAddrA ss)))) := by + rw [hms, e2, e1] + have hwu : CapWfA (capA st s (cslotAddrA ss)) := hsrel.1 + rw [e1] at hwu + exact CapRelA.of_abs hwu (by simp only [absCapA, Fl4SpecA64.maxFreeIndexUpdate, Fl4SpecA64.freeIndexUpdate, + Fl4SpecA64.maxFreeIndex, Fl4SpecA64.untypedSzBits, e3]) + obtain ⟨o, hm, hRx, hSx⟩ := StateRelA.setAbsA hR hP hAs hcf hm1 + refine ⟨_, ?_, hRx, hSx, rfl, rfl⟩ + unfold Fl4SpecA64.setUntypedCapAsFull + rw [if_pos ((utCondA_iff _ _).2 hc)] + exact hm + · refine ⟨σ, ?_, hR.congrA fun y hy => ?_, SpecSlotA.refl σ, rfl, rfl⟩ + · unfold Fl4SpecA64.setUntypedCapAsFull + rw [if_neg fun h => hc ((utCondA_iff _ _).1 h)] + exact rfl + · have hy' := slotW_isSlot hP hy + by_cases e : y = cslotAddrA ss + · rw [e, hms]; exact hnf hc + · exact hm1 y hy' e + -- `set_cap` of the new cap at `dest` + obtain ⟨o2, h2, hR2, hS2⟩ := StateRelA.setAbsA (mem' := L.mem s') hR1 hP hAd (a := anc) + (by show CapRelA (capA st s' _) _; rw [ed]; exact ⟨hwf, hanc⟩) + (fun y hy hyd => (capAt_mixA_other _ _ (slot_ne8 hP hdS hy hyd)).symm) + have h1' : ((), σ1) ∈ (Fl4SpecA64.setUntypedCapAsFull (Z := Unit) asc anc ss σ).1 := by + rw [hsetUtEq]; exact h1 + have hinsEq : decide (Fl4SpecA64.isCapRevocable anc asc) = + decide (Fl4SpecA64.isCapRevocable (absCapA nc) (absCapA (capA st s (cslotAddrA ss)))) := + decide_eq_decide.mpr hisRev + have hsbEq : ∀ o r, Fl4SpecA64.shouldBeParentOf asc o anc r ↔ + Fl4SpecA64.shouldBeParentOf (absCapA (capA st s (cslotAddrA ss))) o (absCapA nc) r := + fun o r => CapEqA.shouldBeParentOf_eq hsrel.2 hanc o r + have hmem := capInsert_memA hgs hgd h1' h2 + simp only [hinsEq, decide_eq_decide.mpr (hsbEq _ _)] at hmem + refine ⟨_, hmem, hR2.of_kheap rfl, + CdtRelA.insertA hP hC hAs hAd _ (decide (Fl4SpecA64.isCapRevocable (absCapA nc) + (absCapA (capA st s (cslotAddrA ss))))) ?_ ?_ ?_ ?_ hne + (fun h => by unfold insBA at h; exact (of_decide_eq_true h).1) + (fun sl h => hdG (hC.tree.tree.par_mem _ _ + (by have e := hC.cdt sl (hC.cdt_slot sl _ h).1; rw [h] at e; rw [← e]; rfl)).2), hT', + (hSS1.trans hS2).trans (SpecSlotA.insFin _ _ _ _ _), + fun y hy => ?_⟩ + · refine hT'.sub (fun y hy => hsub y hy) fun y hy hn' => ?_ + have f1 : y ≠ cslotAddrA ss := fun e => hn' (e ▸ hsS) + have f2 : y ≠ cslotAddrA sd := fun e => hn' (e ▸ hdS) + rw [ek y hy f1 f2]; exact hnul y hy hn' + · (simp only [insFinA, setObjA, hk1.1, hk1.2]) <;> rfl + · (simp only [insFinA, setObjA, hk1.2]) <;> rfl + · intro sl hsl + by_cases e : sl = sd + · rw [if_pos e, e, ← hv]; exact revA_of_val hdb + · rw [if_neg e] + exact revA_of_keep (hb _ (hsub _ (hsl.isSlot hP)) (fun h => e (cslotAddrA_inj hP hsl hAd h))) + · by_cases f2 : y = cslotAddrA sd + · exact Or.inr (Or.inl (by rw [f2]; exact ed)) + · by_cases f1 : y = cslotAddrA ss + · by_cases hc : FullCondA (capA st s (cslotAddrA ss)) nc + · obtain ⟨p, b, i, d, i1, -, e2, -⟩ := hfu hc + exact Or.inr (Or.inr ⟨p, b, i1, d, by rw [f1]; exact e2⟩) + · exact Or.inl (by rw [f1]; exact hnf hc) + · exact Or.inl (ek y (hsub y hy) f1 f2) + +variable {A : Type} + +/-- **FL4's `cdt::insert` refines l4v `cap_insert` with the whole relation** (module docstring; ruling 11:49Z +step 2). `hno`: the new cap names counted objects. -/ +theorem cap_insert_jA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} + {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} {ia ie : Nat} {s : S} {a : A} {σ : State Unit} {m : McState} + (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + (hT : TreeA st OS Z.G.chains Z.G.par s) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) + {ss sd : CslotPtr} (hAs : AbsSlotA Z.th ss) (hAd : AbsSlotA Z.th sd) + (hsn : capAtA (L.mem s) (cslotAddrA ss) ≠ .Null) + (hdn : capAtA (L.mem s) (cslotAddrA sd) = .Null) + (hdc : obj.sext48 (cslotAddrA sd) = ok (cslotAddrA sd)) + {nc : capability.Cap} {anc : Fl4SpecA64.Cap} (hwf : CapWfA nc) (hanc : CapEqA nc anc) + (hw : CapRt.EncWf nc) (hn : nc ≠ .Null) + (hal : UA nc (capA st s (cslotAddrA ss))) + (hk : SameBadgeKindA nc (capA st s (cslotAddrA ss))) + (hut : isUtA (capA st s (cslotAddrA ss)) = true → + ∀ y, ¬ Fl4Proofs.P3.Mdb.Desc Z.G.par y (cslotAddrA ss)) + (hub : isUtA (capA st s (cslotAddrA ss)) = true → + insBA σ ss (absCapA nc) (absCapA (capA st s (cslotAddrA ss))) = true) + (hsib : insBA σ ss (absCapA nc) (absCapA (capA st s (cslotAddrA ss))) = false → + ∀ y, ¬ Fl4Proofs.P3.Mdb.Desc Z.G.par y (cslotAddrA ss)) + (hno : CapObjOkA Z nc) : + cdt.insert st s nc (cslotAddrA ss) (cslotAddrA sd) ⦃ s' => + ∃ σ', ((), σ') ∈ (Fl4SpecA64.capInsert (Z := Unit) anc ss sd σ).1 ∧ + JAtZAX (L := L) AR { Z with G := (insGA Z.G (cslotAddrA ss) (cslotAddrA sd) + (insBA σ ss (absCapA nc) (absCapA (capA st s (cslotAddrA ss))))) } s' a σ' m ∧ + TreeA st OS (insG (cslotAddrA ss) (cslotAddrA sd) Z.G.chains) + (insGA Z.G (cslotAddrA ss) (cslotAddrA sd) + (insBA σ ss (absCapA nc) (absCapA (capA st s (cslotAddrA ss))))).par s' ∧ + (CapOkA σ.interruptIrqNode sd anc → ∀ n, SInvXA σ m n → SInvXA σ' m n) ⦄ := by + have hO := E.sys.ent.os + have hP := E.j.1.place + have hsS : IsSlotA Z.th (cslotAddrA ss) := AbsSlotA.isSlot hP hAs + have hdS : IsSlotA Z.th (cslotAddrA sd) := AbsSlotA.isSlot hP hAd + have hsG : cslotAddrA ss ∈ Z.G.chains.flatten := (hT.lk.members _).2 ⟨hsub _ hsS, hsn⟩ + have hdG : cslotAddrA sd ∉ Z.G.chains.flatten := fun m => ((hT.lk.members _).1 m).2 hdn + have hsd : cslotAddrA ss ≠ cslotAddrA sd := fun e => hdG (e ▸ hsG) + have F := hT.lk.lkF hO.nz + have inOS : ∀ y ∈ Z.G.chains.flatten, y ∈ OS := fun y hy => ((hT.lk.members y).1 hy).1 + have hwr : cdt.insert st s nc (cslotAddrA ss) (cslotAddrA sd) ⦃ s' => + WritesIn st s s' (W7.CapW H.tcbs {b | InOS OS b} ∅) ⦄ := by + unfold cdt.insert + rw [capA_eq, bind_ok] + exact WP.spec_mono (cdt.insert_with_cap.lrF (osokA hO) (LR.self s) (hsub _ hsS) (hsub _ hdS) hsd hdc rfl hsn hw + (fun h => inOS _ (F.nxm _ hsG h))) fun _ h => h.1 + refine WP.spec_mono (WP.spec_and (cap_insert_refinesA s σ Z.G E.j.1.st E.j.1.cdt hP hO hT hsub hnul + hAs hAd hsn hdn hdc hwf hanc hw hn hal hk hut hub hsib) hwr) ?_ + rintro s' ⟨⟨σ', hm, hst, hcdt, hT', hSS, hcs⟩, hw'⟩ + refine ⟨σ', hm, E.j.of_slotWrites hw' (fun b hb' hb => footN_offA E b hb hb') + (FootNA.aligned E.j.1 (bxi_nodes E.sys.ent.bxi) E.scAl E.ap.vcAl) _ hSS hst hcdt fun y hy => ?_, hT', + fun hc n hI => SInvXA.capInsert hI hc hm⟩ + rcases hcs y hy with e | e | ⟨p, b, i, d, e⟩ <;> rw [e] + · exact E.j.capObj hy + · exact hno + · exact ⟨fun _ h => (by cases h), fun _ _ _ _ _ h => (by cases h), fun _ _ _ h => (by cases h), fun _ _ _ h => (by cases h), trivial⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/InvokeRestartJointA.lean b/proofs/Fl4ProofsA64/P3/InvokeRestartJointA.lean new file mode 100644 index 000000000..8158204ee --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/InvokeRestartJointA.lean @@ -0,0 +1,186 @@ +/- +P3 (AArch64): combine the CSpace/entry and MCS views at the concrete +TS_RESTART write. The register reads and slot ghosts in this post +are for the reached successor. + +[J] The accepted invocation supplies the existing Restart placement, +current-SC and scheduler entry facts used by JEntA.restartA. +-/ +import Fl4ProofsA64.P3.RestartA +import Fl4ProofsA64.P3.EpCancelA +import Fl4ProofsA64.P3.NtfnWriteA +import Fl4ProofsA64.P3.LocalInvokePostA +import Fl4ProofsA64.P3.SlotCoverageA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4SpecA64 Sched + +noncomputable section + +/-- Updating an existing TCB's state preserves the converse read coverage +of both kinds of slot-bearing object. -/ +theorem SlotHeapCovA.threadState {th : TypedHeapA} {σ : State Unit} {t : ObjRef} {x : Tcb} + (cov : SlotHeapCovA th σ) (hk : σ.kheap t = some (.tcb x)) (state : ThreadState) : + SlotHeapCovA th (stUpdA σ t x state) where + cnode p radix cs h wf := cov.cnode p radix cs (stUpd_back h (fun _ eq => by cases eq)) wf + tcb p y h := by + by_cases e : p = t + · subst p + exact cov.tcb t x hk + · exact cov.tcb p y (by simpa [setObjA, Function.update_of_ne e] using h) + +section +variable {S A : Type} {st : machine.Store S} [L : LawfulStore st] + +private theorem msgRegisterBoundA {i r : U64} (hr : msg.msg_register i = ok r) : r.val < 37 := by + unfold msg.msg_register at hr + split_ifs at hr <;> rw [← okinj hr] <;> + simp [layout.reg.X2, layout.reg.X3, layout.reg.X4, layout.reg.X5] + +private theorem restart_register_wordA {s : S} {t r : U64} (ht : TcbAddr t) (hr : r.val < 37) : + L.mem (L.wr s (stA t) sched.TS_RESTART) (regWA t r) = L.mem s (regWA t r) := by + change L.mem (L.wr s (stA t) sched.TS_RESTART) (Endp.regA t r) = _ + refine mem_wr_ne' (Endp.a_st ht).1 (Endp.regA_tf ht hr).1 ?_ + intro e + have ev := congrArg UScalar.val e + rw [stA_eq, Notif.tfield_val ht (by decide : (3328#u64 : U64).val < 4096), Endp.regA_val ht hr] at ev + simp at ev + omega + +/-- The caller's message registers are read back after the actual Restart +write. The spec updates only the thread state at this point. -/ +theorem RegViewA.restartJoint {s : S} {σ : State Unit} {t : U64} {x : Tcb} + (V : RegViewA (L.mem s) σ t) (ht : TcbAddr t) + (hk : σ.kheap (specPtr t) = some (.tcb x)) : + RegViewA (L.mem (L.wr s (stA t) sched.TS_RESTART)) (stUpdA σ (specPtr t) x .restart) t := by + intro y hy + have e : y = { x with tcbState := .restart } := by + simpa [setObjA, eq_comm] using hy + subst y + obtain ⟨info, words⟩ := V x hk + refine ⟨?_, fun i r hi hr => ?_⟩ + · rw [restart_register_wordA ht (by simp [layout.reg.MSG_INFO, layout.reg.X1] : layout.reg.MSG_INFO.val < 37)] + exact info + · rw [restart_register_wordA ht (msgRegisterBoundA hr)] + exact words i r hi hr + +private theorem runnableNotWaitingA {ts : ThreadState} (hr : runnable ts = true) : + Notif.tsCode ts ≠ 3 ∧ Notif.tsCode ts ≠ 4 ∧ Notif.tsCode ts ≠ 6 := by + cases ts <;> simp [runnable, Notif.tsCode] at hr ⊢ + +/-- The exact caller state write preserves both IPC views. A runnable +caller cannot appear in either waiting queue. -/ +theorem restart_ipc_viewsA {s : S} {σ : State Unit} {T E R N : Set U64} {Bf : Set (U64 × Word)} + (ep : Endp.EpStateRel T E R Bf (L.mem s) σ) (epPlace : Endp.EPlaced T E R Bf) + (ntfn : Notif.NtfnStateRel N T (L.mem s) σ) (ntfnPlace : Notif.NPlaced T N) + {t : U64} (ht : t ∈ T) {x : Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hr : runnable x.tcbState = true) : + let s1 := L.wr s (stA t) sched.TS_RESTART + let σ1 := stUpdA σ (specPtr t) x .restart + Endp.EpStateRel T E R Bf (L.mem s1) σ1 ∧ Notif.NtfnStateRel N T (L.mem s1) σ1 := by + intro s1 σ1 + have code : sched.TS_RESTART.val = Notif.tsCode .restart := by rw [ts_vals.2.1]; rfl + obtain ⟨notRecv, notSend, notNtfn⟩ := runnableNotWaitingA hr + have epTcb := ep.at_tcb ht hk + have ntTcb := ntfn.at_tcb ht hk + constructor + · have notQueued : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → + specPtr t ∉ z.epQueue := by + intro e he z hz hq + obtain ⟨z', hz', rel⟩ := ep.ep e he + rw [hz] at hz' + obtain rfl := KernelObject.endpoint.inj (Option.some.inj hz') + obtain ⟨qs, _, hqs, members⟩ := rel.queue + rw [hqs, List.mem_map] at hq + obtain ⟨u, hu, hEq⟩ := hq + have eu : u = t := specPtr_inj hEq + subst u + have state := (members t hu).2 + rw [epTcb.st] at state + exact state.elim notRecv notSend + exact (Endp.write_state_rel epPlace s σ ep ht hk notQueued .restart (by decide) _ code).1 + · obtain ⟨writes, kept⟩ := Notif.twr_wr (st := st) ntfnPlace ht (K := 3328#u64) (Or.inl rfl) s + sched.TS_RESTART + rw [← stA_eq] at writes kept + refine ntfn.upd_tcb ht _ writes.ntfn writes.tcb writes.links (Or.inl ?_) ?_ + · intro n hn z hz hq + obtain ⟨z', hz', rel⟩ := ntfn.ntfn n hn + rw [hz] at hz' + obtain rfl := KernelObject.notification.inj (Option.some.inj hz') + have blocked := (rel.obj.blocked hq).2.1 + rw [ntTcb.st] at blocked + exact notNtfn blocked + · refine ⟨?_, (fun _ h => by cases h), ?_, ?_⟩ + · rw [L.mem_wr_same (Notif.foot_st ntfnPlace ht).1]; exact code + · show (L.mem s1 (Notif.badgeRegA t)).bv = Notif.badgeOf x + rw [Notif.badgeRegA_eq, kept _ (by simp) (by simp) (by simp), ← Notif.badgeRegA_eq] + exact ntTcb.badge + · show (L.mem s1 (prioA t)).val = x.tcbPriority.toNat + rw [Notif.prioA_eq', kept _ (by simp) (by simp) (by simp), ← Notif.prioA_eq'] + exact ntTcb.prio + +set_option maxHeartbeats 950000 in +/-- The one concrete Restart write simultaneously produces JEntA, +SchedSRA, the register view and the typed lookup/CDT coverage. -/ +theorem invokeRestart_jointA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} + {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} {ia ie : Nat} + {s : S} {a : A} {σ : State Unit} {m : McState} {κ : U64 → U64} + (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + {Rp Nt : Set U64} {extra : Array obj.Slot 3#usize} {slot : U64} + (P : SysPlace st H Z.Cs H.frame Rp Nt OS s extra slot) (Y : SymL st H Z.Cs s) + (hrun : Runnable (L.mem s (stA (L.curW s)))) + (hcur : (L.mem s (inqA (L.curW s)) &&& sched.Q_RELEASE) = 0#u64 ∧ + L.mem s (scA (L.curW s)) ≠ 0#u64 ∧ L.mem s (scA (L.curW s)) ∈ Ac) + (hvs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → Aligned b ∧ b ≠ stA (L.curW s)) + (hvc : ∀ v ∈ Z.V, stA (L.curW s) ≠ v ∧ + stA (L.curW s) ≠ core.num.U64.wrapping_add v 24#u64) + (SR : SchedSRA st Z m κ s σ) (states : StEqA Z.T (L.mem s) σ) + (registers : RegViewA (L.mem s) σ (L.curW s)) (coverage : SlotHeapCovA Z.th σ) + {En Re N : Set U64} {Bf : Set (U64 × Word)} + (ep : Endp.EpStateRel Z.T En Re Bf (L.mem s) σ) (epPlace : Endp.EPlaced Z.T En Re Bf) + (ntfn : Notif.NtfnStateRel N Z.T (L.mem s) σ) (ntfnPlace : Notif.NPlaced Z.T N) : + sched.set_thread_state st s (L.curW s) sched.TS_RESTART ⦃ s1 => + ∃ σ1, + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s1 a σ1 m ∧ + SchedSRA st Z m κ s1 σ1 ∧ StEqA Z.T (L.mem s1) σ1 ∧ + RegViewA (L.mem s1) σ1 (L.curW s1) ∧ SlotHeapCovA Z.th σ1 ∧ + Endp.EpStateRel Z.T En Re Bf (L.mem s1) σ1 ∧ Notif.NtfnStateRel N Z.T (L.mem s1) σ1 ∧ + SymL st H Z.Cs s1 ∧ OSKeep st OS s s1 ∧ + (∀ y, SlotWA Z.th y → capAtA (L.mem s1) y = capAtA (L.mem s) y) ∧ + ∀ n, SInvXA σ m n → SInvXA σ1 m n ⦄ := by + obtain ⟨x, hk, run, step, E1, sym, os, slots, inv⟩ := E.restartA P Y hrun hcur hvs hvc + let t := L.curW s + let s1 := L.wr s (stA t) sched.TS_RESTART + let σ1 := stUpdA σ σ.curThread x .restart + have ct : σ.curThread = specPtr t := E.j.1.sch.curThread + have hx : σ.kheap (specPtr t) = some (.tcb x) := ct ▸ hk + have hr : runnable x.tcbState = true := (runnable_iffA (states t SR.curT x hx)).1 hrun + have code : sched.TS_RESTART.val = tsCodeA .restart := by rw [ts_vals.2.1]; rfl + have result := sc_set_runnable_stateA SR states SR.curT hx hr + (ts := .restart) rfl code + rw [run, WP.spec_ok] at result ⊢ + obtain ⟨τ, schedStep, SR1, states1, _, _⟩ := result + have eq : τ = σ1 := by + rw [← ct, step] at schedStep + exact (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst schedStep))).2.symm + subst τ + have ht : TcbAddr t := E.sq.tcbs t E.sq.curT + have newCur : L.curW s1 = t := + specPtr_inj (E1.j.1.sch.curThread.symm.trans ct) + have regs : RegViewA (L.mem s1) σ1 (L.curW s1) := by + rw [newCur] + change RegViewA (L.mem s1) (stUpdA σ σ.curThread x .restart) t + rw [ct] + exact registers.restartJoint ht hx + have views : Endp.EpStateRel Z.T En Re Bf (L.mem s1) σ1 ∧ Notif.NtfnStateRel N Z.T (L.mem s1) σ1 := by + rw [show σ1 = stUpdA σ (specPtr t) x .restart by simp only [σ1, ct]] + exact restart_ipc_viewsA ep epPlace ntfn ntfnPlace SR.curT hx hr + exact ⟨σ1, step, E1, SR1, states1, regs, coverage.threadState hk .restart, + views.1, views.2, sym, os, slots, inv⟩ + +end +end +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/IpcCancelA.lean b/proofs/Fl4ProofsA64/P3/IpcCancelA.lean new file mode 100644 index 000000000..e74e37f58 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/IpcCancelA.lean @@ -0,0 +1,929 @@ +/- +P3 (aarch64, A64-P3-IPC gen 6, ruling (b)): **`cancel_ipc` on both IPC relations** (x86 `P3/IpcCancelAll`'s +`cancel_ipc_irelP`, without the `BlockedOnReply` arm; x86 merged the relations into `IpcRel`, here `NtfnStateRel` and +`EpStateRel` stay self-contained and are combined by one placement record). + +* **`IPlacedA N T E R Bf`** (x86 `Ipc.IPlaced`): `NPlaced T N`, `EPlaced T E R Bf`, and notifications disjoint from + endpoints, replies and the IPC-buffer words. A placement hypothesis, to be discharged from `PlaceA` / `HeapOK` at + `JAtZA` (the typed heap's object disjointness), as `NPlaced` / `EPlaced`. +* Cross frames: `NKeep` (the words `NtfnStateRel` reads, but the links of TCBs blocked on no notification) and + `NtfnStateRel.frameX`; `EKeep` (`EFoot` but the links of TCBs blocked on no endpoint) and `EpStateRel.frameX`; the + endpoint code's writes miss `NKeep` (`nk_fault`, `nk_ep`, `nk_rep`), the notification dequeue misses `EKeep` + (`ek_ntfn`). `notqE_of_code` / `notqN_of_code`: a TCB whose state word is not a queue code is in no queue. +* `set_thread_state_both`: `set_thread_state t Inactive` keeps both relations (the two `schedule_tcb` contracts of + `SchedOpsA` and `SchedOpsEA`, conjoined: `WP.spec_and`), `SR` after the state word supplied by the caller's + `stable`. +* The arms after `clear_fault` (`clear_both`): **`ntfn_arm`** (`cancel_signal`: notification dequeue, `Inactive`; + `SchedHypsA.stable` with the gen-6 `NWrite` / `NtfnStep` / `NoBlockE`: the fault clear is branch 1 since a thread + blocked on a notification is in `inClassR` whatever its fault, the state write branch 3), **`ep_armI`** (send / + receive with or without a reply object: `EpCancelIpcA.cancel_arm` with `NtfnStateRel` carried, `final_inactiveI`, + `reply_unlink_relI`, `reply_secondI`), the no-IPC arm (only the fault clear: `cancelIpc_plain`, branch 1). +* **`cancel_ipc_rel`**: for `t ∈ T` in any state but `BlockedOnReply`, from `SchedHypsA`, `SchedOpsA`, `SchedOpsEA`, + `IPlacedA`, `RepOKA σ t` and the case / placement hypotheses `hE` (a thread blocked on an endpoint: its block word in + `E`, queued there) and `hN` (on a notification: in `N`, queued there) (x86 `heE` / `hnN` / `hz`): + `ipc.cancel_ipc st ar s a t ⦃ r => r.2 = a ∧ ∃ σ', cancelIpc (specPtr t) σ = ({((), σ')}, False) ∧ + NtfnStateRel N T (L.mem r.1) σ' ∧ EpStateRel T E R Bf (L.mem r.1) σ' ∧ SR r.1 σ' ⦄`. + +**`cancelIpc_of_rel`** (x86 `IpcCancel.cancelIpc_top`) for a receiver with both IPC relations, the endpoint +holding it, and its reply backlink. **`Notif.send_signal_idle_bound_recv`** calls it before the wake-up. + +No named premise. +-/ +import Fl4ProofsA64.P3.EpCancelIpcA +import Fl4ProofsA64.P3.NtfnCancelAllA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif + +noncomputable section + +/-! ## Placement and cross frames -/ + +/-- **The joint placement of the IPC objects** (x86 `IPlaced`). -/ +structure IPlacedA (N T E R : Set U64) (Bf : Set (U64 × Fl4SpecA64.Word)) : Prop where + n : NPlaced T N + e : EPlaced T E R Bf + ne : ∀ n ∈ N, ∀ e ∈ E, n.val + 64 ≤ e.val ∨ e.val + 16 ≤ n.val + nr : ∀ n ∈ N, ∀ r ∈ R, n.val + 64 ≤ r.val ∨ r.val + 32 ≤ n.val + nb : ∀ n ∈ N, ∀ a, BufW Bf a → a.val < n.val ∨ n.val + 64 ≤ a.val + +section Cross +variable {N T E R : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + +/-- The words `NtfnStateRel` reads, but the `Ep` links of TCBs blocked on no notification. -/ +def NKeep (N T : Set U64) (m : U64 → U64) (b : U64) : Prop := + (∃ n ∈ N, n.val ≤ b.val ∧ b.val < n.val + 40) ∨ + (∃ y ∈ T, b = stA y ∨ b = tBlockA y ∨ b = badgeRegA y ∨ b = prioA y) ∨ + (∃ y ∈ T, m (tBlockA y) ∈ N ∧ (b = nextA .Ep y ∨ b = prevA .Ep y)) + +/-- `EFoot` but the `Ep` links of TCBs blocked on no endpoint. -/ +def EKeep (T E R : Set U64) (Bf : Set (U64 × Fl4SpecA64.Word)) (m : U64 → U64) (b : U64) : Prop := + EFoot T E R Bf b ∧ ¬ ∃ y ∈ T, m (tBlockA y) ∉ E ∧ TL y b + +theorem NKeep.vals (hP : NPlaced T N) {m : U64 → U64} {b : U64} (h : NKeep N T m b) : + (∃ n ∈ N, n.val ≤ b.val ∧ b.val < n.val + 40) ∨ + (∃ y ∈ T, b.val = y.val + 3328 ∨ b.val = y.val + 3336 ∨ b.val = y.val + 256 ∨ b.val = y.val + 3448) ∨ + (∃ y ∈ T, m (tBlockA y) ∈ N ∧ (b.val = y.val + 3496 ∨ b.val = y.val + 3504)) := by + rcases h with h | ⟨y, hy, h⟩ | ⟨y, hy, hm, h⟩ + · exact Or.inl h + · have hya := hP.tcb y hy + refine Or.inr (Or.inl ⟨y, hy, ?_⟩) + rcases h with rfl | rfl | rfl | rfl + · exact Or.inl (val_st hya) + · exact Or.inr (Or.inl (val_blk hya)) + · refine Or.inr (Or.inr (Or.inl ?_)); rw [badgeRegA_eq, tfield_val hya (by simp)]; simp + · exact Or.inr (Or.inr (Or.inr (val_prio hya))) + · have hya := hP.tcb y hy + refine Or.inr (Or.inr ⟨y, hy, hm, ?_⟩) + rcases h with rfl | rfl + · exact Or.inl (val_next hya) + · exact Or.inr (val_prev hya) + +theorem nw_not_efoot (hP : IPlacedA N T E R Bf) {n a : U64} (hn : n ∈ N) (h1 : n.val ≤ a.val) + (h2 : a.val < n.val + 40) : ¬ EFoot T E R Bf a := by + rintro (⟨e, he, h⟩ | ⟨r, hr, h⟩ | ⟨y, hy, hf | hl⟩ | hb) + · have := hP.ne n hn e he; omega + · have := hP.nr n hn r hr; omega + · have := hf.range; have := hP.n.disj n hn y hy; omega + · have := hP.n.disj n hn y hy; unfold TL at hl; omega + · have := hP.nb n hn a hb; omega + +theorem nk_fault (hP : IPlacedA N T E R Bf) {m : U64 → U64} {t b : U64} (ht : t ∈ T) (h : NKeep N T m b) : + b.val ≠ (tFaultA t).val := by + have hta := hP.e.tcb t ht + rw [val_fault hta] + rcases NKeep.vals hP.n h with ⟨n, hn, h1, h2⟩ | ⟨y, hy, h⟩ | ⟨y, hy, -, h⟩ + · have := hP.n.disj n hn t ht; omega + · by_cases e : y = t + · subst e; omega + · have := tcb_disj hP.e ht hy e; omega + · by_cases e : y = t + · subst e; omega + · have := tcb_disj hP.e ht hy e; omega + +theorem nk_ep (hP : IPlacedA N T E R Bf) {m : U64 → U64} {e b : U64} (he : e ∈ E) {xs : List U64} + (hxs : ∀ w ∈ xs, w ∈ T ∧ m (tBlockA w) = e) (h : NKeep N T m b) : ¬ EW e b ∧ ¬ ∃ w ∈ xs, TL w b := by + rcases NKeep.vals hP.n h with ⟨n, hn, h1, h2⟩ | ⟨y, hy, h⟩ | ⟨y, hy, hm, h⟩ + · refine ⟨fun hw => ?_, fun ⟨w, hw, hl⟩ => ?_⟩ + · have := hP.ne n hn e he; unfold EW at hw; omega + · have := hP.n.disj n hn w (hxs w hw).1; unfold TL at hl; omega + · refine ⟨fun hw => ?_, fun ⟨w, hw, hl⟩ => ?_⟩ + · have := hP.e.et e he y hy; unfold EW at hw; omega + · unfold TL at hl + by_cases ew : w = y + · subst ew; omega + · have := tcb_disj hP.e hy (hxs w hw).1 ew; omega + · refine ⟨fun hw => ?_, fun ⟨w, hw, hl⟩ => ?_⟩ + · have := hP.e.et e he y hy; unfold EW at hw; omega + · unfold TL at hl + by_cases ew : w = y + · subst ew; rw [(hxs w hw).2] at hm; have := hP.ne e hm e he; omega + · have := tcb_disj hP.e hy (hxs w hw).1 ew; omega + +theorem nk_rep (hP : IPlacedA N T E R Bf) {m : U64 → U64} {r b : U64} (hr : r ∈ R) (h : NKeep N T m b) : + b.val ≠ r.val := by + rcases NKeep.vals hP.n h with ⟨n, hn, h1, h2⟩ | ⟨y, hy, h⟩ | ⟨y, hy, -, h⟩ + · have := hP.nr n hn r hr; omega + · have := hP.e.rt r hr y hy; omega + · have := hP.e.rt r hr y hy; omega + +theorem ek_ntfn (hP : IPlacedA N T E R Bf) {m : U64 → U64} {n b : U64} (hn : n ∈ N) {xs : List U64} + (hxs : ∀ w ∈ xs, w ∈ T ∧ m (tBlockA w) = n) (h : EKeep T E R Bf m b) : + (∀ w ∈ xs, b ≠ nextA .Ep w ∧ b ≠ prevA .Ep w) ∧ b ≠ epHeadA n ∧ b ≠ epTSA n := by + have hnE : n ∉ E := fun hnE => by have := hP.ne n hn n hnE; omega + have ha := hP.n.ntfn n hn + refine ⟨fun w hw => ⟨fun e => h.2 ⟨w, (hxs w hw).1, by rw [(hxs w hw).2]; exact hnE, + Or.inl (by rw [e]; exact val_next (hP.e.tcb w (hxs w hw).1))⟩, + fun e => h.2 ⟨w, (hxs w hw).1, by rw [(hxs w hw).2]; exact hnE, + Or.inr (by rw [e]; exact val_prev (hP.e.tcb w (hxs w hw).1))⟩⟩, fun e => ?_, fun e => ?_⟩ + · have hv : b.val = n.val := by rw [e]; exact epHeadA_val + exact nw_not_efoot hP hn (by omega) (by omega) h.1 + · have hv : b.val = n.val + 8 := by rw [e, epTSA_eq, nfield_val ha (by simp)]; simp + exact nw_not_efoot hP hn (by omega) (by omega) h.1 + +section Rel +open Fl4SpecA64 +variable {Z : Type} + +/-- `NtfnStateRel` across memory agreeing on `NKeep` and a spec step keeping `Notif.KView`. -/ +theorem _root_.Fl4ProofsA64.P3.Notif.NtfnStateRel.frameX {m m' : U64 → U64} {σ σ' : State Z} (hR : NtfnStateRel N T m σ) (hP : NPlaced T N) + (hA : Agree m m' (NKeep N T m)) (hk : Notif.KView σ σ') : NtfnStateRel N T m' σ' := by + have hst : ∀ y ∈ T, TFieldsEq m m' y := fun y hy => + ⟨hA _ (foot_st hP hy).1 (Or.inr (Or.inl ⟨y, hy, Or.inl rfl⟩)), + hA _ (foot_blk hP hy).1 (Or.inr (Or.inl ⟨y, hy, Or.inr (Or.inl rfl)⟩)), + hA _ (foot_breg hP hy).1 (Or.inr (Or.inl ⟨y, hy, Or.inr (Or.inr (Or.inl rfl))⟩)), + hA _ (foot_prio hP hy).1 (Or.inr (Or.inl ⟨y, hy, Or.inr (Or.inr (Or.inr rfl))⟩))⟩ + refine ⟨fun n hn => ?_, fun y hy => ?_⟩ + · obtain ⟨x, hkx, hx⟩ := hR.ntfn n hn + have ha := hP.ntfn n hn + have g : ∀ K : U64, K.val < 40 → K.val % 8 = 0 → + m' (core.num.U64.wrapping_add n K) = m (core.num.U64.wrapping_add n K) := fun K h1 h2 => + hA _ (nword hP hn h1 h2).1 (Or.inl ⟨n, hn, by rw [nfield_val ha (by omega)]; omega, + by rw [nfield_val ha (by omega)]; omega⟩) + have hw : NWordsEq m m' n := + ⟨by rw [Notif.epHeadA_eq]; exact g _ (by simp) (by simp), by rw [Notif.epTSA_eq]; exact g _ (by simp) (by simp), + by rw [nBadgeA_eq]; exact g _ (by simp) (by simp), by rw [nBoundA_eq]; exact g _ (by simp) (by simp), + by rw [nScA_eq]; exact g _ (by simp) (by simp)⟩ + refine ⟨x, hk.1 _ _ hkx, hx.frame hw fun y hy hq => ?_⟩ + obtain ⟨-, -, hb⟩ := hx.obj.blocked hq + have hl : m (tBlockA y) ∈ N := by rw [hb]; exact hn + exact ⟨(hst y hy).st, (hst y hy).blk, hA _ (foot_prev hP hy).1 (Or.inr (Or.inr ⟨y, hy, hl, Or.inr rfl⟩)), + hA _ (foot_next hP hy).1 (Or.inr (Or.inr ⟨y, hy, hl, Or.inl rfl⟩))⟩ + · obtain ⟨x, hkx, hx⟩ := hR.tcb y hy + obtain ⟨z, hkz, e1, e2, e3⟩ := hk.2 _ _ hkx + have hx' := hx.frame (hst y hy) + exact ⟨z, hkz, ⟨e1 ▸ hx'.st, fun n hn => hx'.blk n (e1 ▸ hn), e2 ▸ hx'.badge, e3 ▸ hx'.prio⟩⟩ + +/-- `EpStateRel` across memory agreeing on `EKeep` and a spec step keeping `Endp.KView`. -/ +theorem EpStateRel.frameX {m m' : U64 → U64} {σ σ' : State Z} (hR : EpStateRel T E R Bf m σ) + (hP : EPlaced T E R Bf) (hm : Agree m m' (EKeep T E R Bf m)) (hk : Endp.KView σ σ') : + EpStateRel T E R Bf m' σ' := by + have hTF : ∀ y ∈ T, ∀ a, Aligned a → TF y a → m' a = m a := fun y hy a ha hf => + hm a ha ⟨Or.inr (Or.inr (Or.inl ⟨y, hy, Or.inl hf⟩)), fun ⟨w, hw, _, hl⟩ => by + by_cases ew : w = y + · subst ew; exact hl.not_tf hf + · exact notTF_tcb hP hy hw ew (Or.inr hl) hf⟩ + refine ⟨fun e he => ?_, fun r hr => ?_, fun t ht => ?_, fun bp hb k hk' => ?_⟩ + · obtain ⟨x, hkx, hx⟩ := hR.ep e he + have hnl : ∀ a, (a.val = e.val ∨ a.val = e.val + 8) → ¬ ∃ y ∈ T, m (tBlockA y) ∉ E ∧ TL y a := by + rintro a ha ⟨y, hy, -, hl⟩; have := hP.et e he y hy; unfold TL at hl; omega + refine ⟨x, hk.1 _ _ hkx, hx.frame (hm _ (a_head hP he).1 ⟨(a_head hP he).2, hnl _ (Or.inl (val_head e))⟩) + (hm _ (a_ts hP he).1 ⟨(a_ts hP he).2, hnl _ (Or.inr (val_ts hP he))⟩) fun y hy hq => ?_⟩ + have hya := hP.tcb y hy + have hb : m (tBlockA y) = e := (hx.blocked hq).2 + have hln : ∀ a, TL y a → EKeep T E R Bf m a := fun a hl => + ⟨Or.inr (Or.inr (Or.inl ⟨y, hy, Or.inr hl⟩)), fun ⟨w, hw, hwE, hl'⟩ => by + by_cases ew : w = y + · subst ew; exact hwE (hb ▸ he) + · have := tcb_disj hP hy hw ew; unfold TL at hl hl'; omega⟩ + exact ⟨hTF y hy _ (a_st hya).1 (a_st hya).2, hTF y hy _ (a_blk hya).1 (a_blk hya).2, + hm _ (a_prev hya).1 (hln _ (a_prev hya).2), hm _ (a_next hya).1 (hln _ (a_next hya).2)⟩ + · obtain ⟨x, hkx, hx⟩ := hR.reply r hr + obtain ⟨y, hky, ey⟩ := hk.2.1 _ _ hkx + refine ⟨y, hky, ?_⟩ + rw [ey, hx, hm _ (a_rtcb hP hr).1 ⟨(a_rtcb hP hr).2, ?_⟩] + rintro ⟨w, hw, -, hl⟩; have := hP.rt r hr w hw; unfold TL at hl; rw [rTcbA_val] at hl; omega + · obtain ⟨x, hkx, hx⟩ := hR.tcb t ht + obtain ⟨y, hky, e1, e2, e3, e4, e5⟩ := hk.2.2.1 _ _ hkx + have hx' := hx.frame (hP.tcb t ht) (fun a ha hf => hTF t ht a ha hf) + exact ⟨y, hky, ⟨e1 ▸ hx'.st, fun e hb => hx'.blk e (e1 ▸ hb), fun e d hs => hx'.send e d (e1 ▸ hs), + fun e r d hs => hx'.recv e r d (e1 ▸ hs), fun r hs => hx'.reply r (e1 ▸ hs), fun r => e2 ▸ hx'.regs r, + e3 ▸ hx'.prio, e4 ▸ hx'.fault0, e4 ▸ hx'.faultT, e5 ▸ hx'.bound, e4 ▸ hx'.faultR⟩⟩ + · have hw : BufW Bf (bufW bp.1 k) := ⟨bp, hb, k, hk', rfl⟩ + rw [hm _ (a_buf hP hb hk').1 ⟨(a_buf hP hb hk').2, ?_⟩, hk.2.2.2] + · exact hR.buf bp hb k hk' + · rintro ⟨w, hw', -, hl⟩; have := hP.bt _ hw w hw'; unfold TL at hl; omega + +end Rel + +section Spec +open Fl4SpecA64 + +/-- A spec step replacing a non-notification object (a TCB only by one with the same state, badge and priority). -/ +theorem nkview_upd {σ : State Unit} {p : ObjRef} {o o' : KernelObject} (hp : σ.kheap p = some o) + (hn : ∀ z, o ≠ .notification z) + (ht : ∀ x, o = .tcb x → ∃ y, o' = .tcb y ∧ y.tcbState = x.tcbState ∧ badgeOf y = badgeOf x ∧ + y.tcbPriority = x.tcbPriority) : + Notif.KView σ (updK σ p o') := by + refine ⟨fun q z hq => ?_, fun q x hq => ?_⟩ + · have : q ≠ p := fun e => by rw [e, hp] at hq; injection hq with hq; exact hn z hq + rw [updK_kheap, Function.update_of_ne this, hq] + · by_cases e : q = p + · subst e + rw [hp] at hq; injection hq with hq + obtain ⟨y, rfl, h⟩ := ht x hq + exact ⟨y, by rw [updK_kheap, Function.update_self], h⟩ + · exact ⟨x, by rw [updK_kheap, Function.update_of_ne e, hq], rfl, rfl, rfl⟩ + +/-- Replacing a notification keeps `Endp.KView`. -/ +theorem ekview_updN {σ : State Unit} {p : ObjRef} {x : Notification} (hp : σ.kheap p = some (.notification x)) + (y : Notification) : Endp.KView σ (updK σ p (.notification y)) := by + have hne : ∀ q o, σ.kheap q = some o → (∀ z, o ≠ .notification z) → q ≠ p := fun q o hq ho e => by + rw [e, hp] at hq; injection hq with hq; exact ho x hq.symm + refine ⟨fun q z hq => ?_, fun q z hq => ⟨z, ?_, rfl⟩, fun q z hq => ⟨z, ?_, rfl, rfl, rfl, rfl, rfl⟩, rfl⟩ + · rw [updK_kheap, Function.update_of_ne (hne q _ hq fun _ h => by cases h), hq] + · rw [updK_kheap, Function.update_of_ne (hne q _ hq fun _ h => by cases h), hq] + · rw [updK_kheap, Function.update_of_ne (hne q _ hq fun _ h => by cases h), hq] + +/-- `cancel_ipc` of a thread blocked on a notification: the fault clear, then `cancel_signal`. -/ +theorem cancelIpc_ntfn {σ σ' : State Unit} {t n : ObjRef} {x : Tcb} (hk : σ.kheap t = some (.tcb x)) + (hst : x.tcbState = .blockedOnNotification n) + (h : cancelSignal (Z := Unit) t n (updK σ t (.tcb { x with tcbFault := none })) = ({((), σ')}, False)) : + cancelIpc (Z := Unit) t σ = ({((), σ')}, False) := by + unfold cancelIpc + rw [nd_bind_one (getThreadState_eq hk), nd_bind_one (threadSet_eq _ hk)] + rw [hst] at h ⊢; exact h + +theorem tsCode_five {ts : ThreadState} (h : tsCode ts = 5) : ∃ r, ts = .blockedOnReply r := by + rcases ts with _ | _ | _ | _ | _ | _ | _ | _ | ⟨⟨⟩⟩ <;> simp [tsCode] at h ⊢ + +/-- `cancel_ipc` of a thread in no IPC state: the fault clear only. -/ +theorem cancelIpc_plain {σ : State Unit} {t : ObjRef} {x : Tcb} (hk : σ.kheap t = some (.tcb x)) + (h4 : ∀ e d, x.tcbState ≠ .blockedOnSend e d) (h3 : ∀ e r d, x.tcbState ≠ .blockedOnReceive e r d) + (h6 : ∀ n, x.tcbState ≠ .blockedOnNotification n) (h5 : ∀ r, x.tcbState ≠ .blockedOnReply r) : + cancelIpc (Z := Unit) t σ = ({((), updK σ t (.tcb { x with tcbFault := none }))}, False) := by + unfold cancelIpc + rw [nd_bind_one (getThreadState_eq hk), nd_bind_one (threadSet_eq _ hk)] + rcases hs : x.tcbState with _ | _ | _ | _ | _ | _ | _ | _ | _ + all_goals first + | rfl + | exact absurd hs (h4 _ _) + | exact absurd hs (h3 _ _ _) + | exact absurd hs (h6 _) + | exact absurd hs (h5 _) + +/-- (gen 7) **The outcome of `cancel_ipc` of a receiver** queued on its endpoint whose reply object (if any) points +back to it (`RepOKA`): single, the thread `Inactive` with its SC pointer, and the release queue, the time and every SC +object kept (what `RelIfBound` reads). -/ +theorem cancelIpc_recv_out {σ : State Unit} {t e : ObjRef} {x : Tcb} (hk : σ.kheap t = some (.tcb x)) + {ro : Option ObjRef} {d : ReceiverPayload} (hst : x.tcbState = .blockedOnReceive e ro d) + {z : Endpoint} (hkz : σ.kheap e = some (.endpoint z)) (hq : t ∈ z.epQueue) (hrep : RepOKA σ t) : + ∃ σ' x', cancelIpc (Z := Unit) t σ = ({((), σ')}, False) ∧ σ'.kheap t = some (.tcb x') ∧ + x'.tcbState = .inactive ∧ x'.tcbSchedContext = x.tcbSchedContext ∧ + σ'.releaseQueue = σ.releaseQueue ∧ σ'.curTime = σ.curTime ∧ + (∀ q y n, σ'.kheap q = some (.schedContext y n) → σ.kheap q = some (.schedContext y n)) := by + have het : e ≠ t := by intro h; rw [h, hk] at hkz; cases hkz + have hkz1 : (updK σ t (.tcb { x with tcbFault := none })).kheap e = some (.endpoint z) := by + rw [updK_kheap, Function.update_of_ne het, hkz] + have hdq := tcbEpDequeue_eq (Z := Unit) hkz1 hq + have hk2 : (updK (updK σ t (.tcb { x with tcbFault := none })) e (.endpoint (epDeq z t))).kheap t = + some (.tcb { x with tcbFault := none }) := by + rw [updK_kheap, Function.update_of_ne het.symm, updK_kheap, Function.update_self] + cases ro with + | none => + refine ⟨_, { { x with tcbFault := none } with tcbState := .inactive }, + cancelIpc_blocked hk none (Or.inr ⟨e, d, hst⟩) (bci_none (by rw [hst]; rfl) hdq hk2), + by rw [stsAct_kheap, updK_kheap, Function.update_self], rfl, rfl, by rw [stsAct_releaseQueue]; rfl, + by rw [stsAct_curTime]; rfl, fun q y n h => ?_⟩ + simp only [stsAct_kheap, updK_kheap, Function.update_apply] at h + split_ifs at h <;> first | exact h | cases h + | some r => + obtain ⟨xr, hkr, hback⟩ := hrep x hk e r d hst + have hrt : r ≠ t := by intro h; rw [h, hk] at hkr; cases hkr + have hre : r ≠ e := by intro h; rw [h, hkz] at hkr; cases hkr + have hkr2 : (updK (updK σ t (.tcb { x with tcbFault := none })) e (.endpoint (epDeq z t))).kheap r = + some (.reply xr) := by + rw [updK_kheap, Function.update_of_ne hre, updK_kheap, Function.update_of_ne hrt, hkr] + have hun := replyUnlinkTcb_eq hkr2 hback hk2 (x := { x with tcbFault := none }) hst + refine ⟨_, { { { x with tcbFault := none } with tcbState := .inactive } with tcbState := .inactive }, + cancelIpc_blocked hk _ (Or.inr ⟨e, d, hst⟩) + (bci_reply (x4 := { { x with tcbFault := none } with tcbState := .inactive }) (by rw [hst]; rfl) hdq hun + (by rw [stsAct_kheap, updK_kheap, Function.update_self])), + by rw [stsAct_kheap, updK_kheap, Function.update_self], rfl, rfl, + by rw [stsAct_releaseQueue]; show (stsAct _ _).releaseQueue = _; rw [stsAct_releaseQueue]; rfl, + by rw [stsAct_curTime]; show (stsAct _ _).curTime = _; rw [stsAct_curTime]; rfl, + fun q y n h => ?_⟩ + simp only [stsAct_kheap, updK_kheap, Function.update_apply] at h + split_ifs at h <;> first | exact h | cases h + +end Spec + +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + {SR : S → Fl4SpecA64.State Unit → Prop} {DL : S → U64 → Prop} + +/-- A TCB whose state word is not 3 / 4 is in no endpoint queue. -/ +theorem notqE_of_code {m : U64 → U64} {σ : Fl4SpecA64.State Unit} (hR : EpStateRel T E R Bf m σ) {t : U64} + (h3 : (m (stA t)).val ≠ 3) (h4 : (m (stA t)).val ≠ 4) : + ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue := by + intro e he z hkz hq + obtain ⟨xs, -, hxs, hB⟩ := (hR.at_ep he hkz).queue + rw [hxs, List.mem_map] at hq + obtain ⟨y, hy, ey⟩ := hq + have := specPtr_inj ey + subst this + rcases (hB y hy).2 with h | h + · exact h3 h + · exact h4 h + +/-- A TCB whose state word is not 6 is in no notification queue. -/ +theorem notqN_of_code {m : U64 → U64} {σ : Fl4SpecA64.State Unit} (hR : NtfnStateRel N T m σ) {t : U64} + (h6 : (m (stA t)).val ≠ 6) : + ∀ n ∈ N, ∀ z, σ.kheap (specPtr n) = some (.notification z) → specPtr t ∉ z.ntfnObj.ntfnQueue := by + intro n hn z hkz hq + exact h6 ((hR.at_ntfn hn hkz).obj.blocked hq).2.1 + +theorem stA_ew (hP : EPlaced T E R Bf) {t : U64} (ht : t ∈ T) : EWrite T E R Bf (stA t) := by + have hta := hP.tcb t ht + refine Or.inr (Or.inr (Or.inl ⟨t, ht, Or.inl ⟨(a_st hta).2.1, ?_⟩⟩)) + rw [val_st hta]; unfold TWOffN; omega + +/-- The cancellation steps use the notification frame for `schedule_tcb`. -/ +abbrev CancelScheduleA (st : machine.Store S) [L : LawfulStore st] (N T : Set U64) + (SR : S → Fl4SpecA64.State Unit → Prop) : Prop := + ∀ s σ (t : U64), NtfnStateRel N T (L.mem s) σ → SR s σ → t ∈ T → + sched.schedule_tcb st s t ⦃ s' => Notif.FootEq N T (L.mem s) (L.mem s') ∧ SR s' (stsAct σ (specPtr t)) ⦄ + +/-- **`set_thread_state t Inactive`** of a TCB in no queue keeps both relations (`SR` after the state word from the +caller's `stable`). -/ +theorem set_thread_state_both (hSched : CancelScheduleA st N T SR) (hOE : SchedOpsEA st T E R Bf SR) + (hP : IPlacedA N T E R Bf) (s : S) (σ : Fl4SpecA64.State Unit) (hRN : NtfnStateRel N T (L.mem s) σ) + (hRE : EpStateRel T E R Bf (L.mem s) σ) {t : U64} (ht : t ∈ T) {x : Fl4SpecA64.Tcb} + (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hqN : ∀ n ∈ N, ∀ z, σ.kheap (specPtr n) = some (.notification z) → specPtr t ∉ z.ntfnObj.ntfnQueue) + (hqE : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) + (hSR1 : ∀ s1, WritesIn st s s1 (· = stA t) → + SR s1 (updK σ (specPtr t) (.tcb { x with tcbState := .inactive }))) : + sched.set_thread_state st s t sched.TS_INACTIVE ⦃ s' => + NtfnStateRel N T (L.mem s') (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := .inactive })) (specPtr t)) ∧ + EpStateRel T E R Bf (L.mem s') (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := .inactive })) (specPtr t)) ∧ + SR s' (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := .inactive })) (specPtr t)) ⦄ := by + have hPN := hP.n + have hPE := hP.e + unfold sched.set_thread_state + step with obj.Tcb.set_state.spec as ⟨s1, hs1⟩ + subst hs1 + obtain ⟨hRE1, -, -⟩ := write_state_rel hPE s σ hRE ht hk hqE .inactive (by decide) sched.TS_INACTIVE + (by unfold sched.TS_INACTIVE; rfl) + have hx := hRN.at_tcb ht hk + obtain ⟨hw, hoth⟩ := twr_wr (st := st) hPN ht (K := 3328#u64) (Or.inl rfl) s sched.TS_INACTIVE + rw [← stA_eq] at hw hoth + have hRN1 : NtfnStateRel N T (L.mem (L.wr s (stA t) sched.TS_INACTIVE)) + (updK σ (specPtr t) (.tcb { x with tcbState := .inactive })) := by + refine hRN.upd_tcb ht _ hw.ntfn hw.tcb hw.links (Or.inl hqN) ⟨?_, ?_, ?_, ?_⟩ + · rw [rd_wr_eq (foot_st hPN ht).1]; unfold sched.TS_INACTIVE; rfl + · intro n hn; cases hn + · show (L.mem _ (badgeRegA t)).bv = badgeOf x + rw [badgeRegA_eq, hoth _ (by simp) (by simp) (by simp), ← badgeRegA_eq]; exact hx.badge + · show (L.mem _ (prioA t)).val = x.tcbPriority.toNat + rw [prioA_eq', hoth _ (by simp) (by simp) (by simp), ← prioA_eq']; exact hx.prio + have hS1 := hSR1 _ (writesIn_wr (v := sched.TS_INACTIVE) (foot_st hPN ht).1) + refine WP.spec_mono (WP.spec_and (hSched _ _ t hRN1 hS1 ht) (hOE.scheduleTcb _ _ t hRE1 hS1 ht)) ?_ + rintro s2 ⟨⟨hf, hS⟩, hfE, -⟩ + exact ⟨hRN1.frame hPN hf (Notif.KView.stsAct _ _), hRE1.frame hPE hfE (KView.stsAct' _ _), hS⟩ + +/-- `clear_fault`: both relations, the endpoint writes / step, the agreement. -/ +theorem clear_both (hP : IPlacedA N T E R Bf) (s : S) (σ : Fl4SpecA64.State Unit) + (hRN : NtfnStateRel N T (L.mem s) σ) (hRE : EpStateRel T E R Bf (L.mem s) σ) {t : U64} (ht : t ∈ T) + {x : Fl4SpecA64.Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) : + NtfnStateRel N T (L.mem (L.wr s (tFaultA t) 0#u64)) (updK σ (specPtr t) (.tcb { x with tcbFault := none })) ∧ + EpStateRel T E R Bf (L.mem (L.wr s (tFaultA t) 0#u64)) (updK σ (specPtr t) (.tcb { x with tcbFault := none })) ∧ + WritesIn st s (L.wr s (tFaultA t) 0#u64) (EWrite T E R Bf) ∧ + EpStep σ (updK σ (specPtr t) (.tcb { x with tcbFault := none })) ∧ + Agree (L.mem s) (L.mem (L.wr s (tFaultA t) 0#u64)) (fun b => b.val ≠ (tFaultA t).val) := by + obtain ⟨hRE1, hW1, hS1, hag⟩ := clear_fault_rel hP.e s σ hRE ht hk + exact ⟨hRN.frameX hP.n (hag.mono fun b hb => nk_fault hP ht hb) + (nkview_upd hk (fun _ h => by cases h) fun _ h => by cases h; exact ⟨_, rfl, rfl, rfl, rfl⟩), hRE1, hW1, hS1, hag⟩ + +/-! ## The notification arm -/ + +/-- `cancel_signal` after the fault clear (separate declaration at the post-dequeue state). -/ +theorem cancel_signal_rel (hH : SchedHypsA st N T SR) (hSched : CancelScheduleA st N T SR) (hOE : SchedOpsEA st T E R Bf SR) + (hP : IPlacedA N T E R Bf) (s1 : S) (σ1 : Fl4SpecA64.State Unit) (hRN1 : NtfnStateRel N T (L.mem s1) σ1) + (hRE1 : EpStateRel T E R Bf (L.mem s1) σ1) (hS1 : SR s1 σ1) {t : U64} (ht : t ∈ T) {x1 : Fl4SpecA64.Tcb} + (hk1 : σ1.kheap (specPtr t) = some (.tcb x1)) {n : U64} (hn : n ∈ N) + (hst : x1.tcbState = .blockedOnNotification (specPtr n)) + {z : Fl4SpecA64.Notification} (hkz1 : σ1.kheap (specPtr n) = some (.notification z)) + (hq : specPtr t ∈ z.ntfnObj.ntfnQueue) : + notification.cancel_signal st s1 t n ⦃ s' => ∃ σ', Fl4SpecA64.cancelSignal (Z := Unit) (specPtr t) (specPtr n) σ1 = + ({((), σ')}, False) ∧ NtfnStateRel N T (L.mem s') σ' ∧ EpStateRel T E R Bf (L.mem s') σ' ∧ SR s' σ' ⦄ := by + have hPN := hP.n + have hPE := hP.e + have hnt : specPtr n ≠ specPtr t := by intro h; rw [h, hk1] at hkz1; cases hkz1 + obtain ⟨xs, hw, hst1, hQ, hB⟩ : ∃ xs : List U64, z.ntfnObj = .waitingNtfn (xs.map specPtr) ∧ + stOf (L.mem s1) n = 1#u64 ∧ QueueRel (L.mem s1) T .Ep (qOf (L.mem s1) n) xs ∧ + ∀ y ∈ xs, BlockedOn (L.mem s1) n y := by + have ho := (hRN1.at_ntfn hn hkz1).obj + have hq' := hq + rcases hzo : z.ntfnObj with _ | q | b <;> rw [hzo] at ho hq' + · simp [Fl4SpecA64.Ntfn.ntfnQueue] at hq' + · obtain ⟨h1, xs, -, rfl, h2, h3⟩ := ho + exact ⟨xs, rfl, h1, h2, h3⟩ + · simp [Fl4SpecA64.Ntfn.ntfnQueue] at hq' + have htx : t ∈ xs := by + have hq' := hq + rw [hw] at hq' + simp only [Fl4SpecA64.Ntfn.ntfnQueue, List.mem_map] at hq' + obtain ⟨y, hy, ey⟩ := hq' + rw [← specPtr_inj ey]; exact hy + have hmem : specPtr t ∈ Fl4SpecA64.ntfnQueueOf z := hq + have hdq : Fl4SpecA64.tcbNtfnDequeue (Z := Unit) (specPtr t) (specPtr n) σ1 = + ({((), updK σ1 (specPtr n) (.notification (afterDequeue z t)))}, False) := tcbNtfnDequeue_eq hkz1 hmem + unfold notification.cancel_signal + refine WP.spec_bind (ntfn_dequeue_full hPN s1 hn t xs hQ htx) ?_ + rintro s2 ⟨h1, h2, h3⟩ + obtain ⟨hRN2, hqN2⟩ := rel_after_dequeue hRN1 hn hkz1 hw hQ hB htx h1 h2 h3 hst1 + have hS2 : SR s2 (updK σ1 (specPtr n) (.notification (afterDequeue z t))) := + hH.stable _ _ _ _ hS1 h3.foot (NtfnStep.ntfnK (y := afterDequeue z t) hkz1 rfl rfl) (NoBlockE.ntfnK _ _ _) + have hxsB : ∀ w ∈ xs, w ∈ T ∧ L.mem s1 (tBlockA w) = n := fun w hw => ⟨hQ.mem w hw, (hB w hw).2⟩ + have hRE2 : EpStateRel T E R Bf (L.mem s2) (updK σ1 (specPtr n) (.notification (afterDequeue z t))) := + hRE1.frameX hPE (fun b hb hk' => by + obtain ⟨c1, c2, c3⟩ := ek_ntfn hP hn hxsB hk' + exact h3.agree b hb c1 c2 c3) (ekview_updN hkz1 _) + have hk2 : (updK σ1 (specPtr n) (.notification (afterDequeue z t))).kheap (specPtr t) = some (.tcb x1) := by + rw [updK_kheap, Function.update_of_ne hnt.symm, hk1] + have h6 : (L.mem s2 (stA t)).val = 6 := by + rw [(hRE2.at_tcb ht hk2).st, hst]; rfl + have hqE2 := notqE_of_code hRE2 (t := t) (by omega) (by omega) + refine WP.spec_mono (set_thread_state_both hSched hOE hP s2 _ hRN2 hRE2 ht hk2 hqN2 hqE2 fun s3 hw => hH.stable _ _ _ _ hS2 + (hw.mono fun b hb => by subst hb; exact Or.inr ⟨t, ht, Or.inl rfl⟩) (NtfnStep.tcb hk2 .inactive x1.tcbArch) + (NoBlockE.updT hk2 _ (Or.inr (Or.inr ⟨rfl, by rw [hst]; rfl, by rw [hst]; exact fun h => by cases h⟩)))) ?_ + rintro s3 ⟨hRN3, hRE3, hS3⟩ + refine ⟨_, ?_, hRN3, hRE3, hS3⟩ + unfold Fl4SpecA64.cancelSignal + rw [nd_bind_one hdq] + exact setThreadState_eq _ hk2 + +/-- **The notification arm of `cancel_ipc`** after the fault clear and the state read. -/ +theorem ntfn_arm (hH : SchedHypsA st N T SR) (hSched : CancelScheduleA st N T SR) (hOE : SchedOpsEA st T E R Bf SR) + (hP : IPlacedA N T E R Bf) (s : S) (a : A) (σ : Fl4SpecA64.State Unit) (hRN : NtfnStateRel N T (L.mem s) σ) + (hRE : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) {t : U64} (ht : t ∈ T) {x : Fl4SpecA64.Tcb} + (hk : σ.kheap (specPtr t) = some (.tcb x)) {n : U64} (hn : n ∈ N) + (hst : x.tcbState = .blockedOnNotification (specPtr n)) + {z : Fl4SpecA64.Notification} (hkz : σ.kheap (specPtr n) = some (.notification z)) + (hq : specPtr t ∈ z.ntfnObj.ntfnQueue) (hb1 : L.mem (L.wr s (tFaultA t) 0#u64) (tBlockA t) = n) : + (do + let i ← obj.Tcb.block_obj st t (L.wr s (tFaultA t) 0#u64) + let s2 ← notification.cancel_signal st (L.wr s (tFaultA t) 0#u64) t i + ok (s2, a)) ⦃ r => r.2 = a ∧ ∃ σ', Fl4SpecA64.cancelIpc (Z := Unit) (specPtr t) σ = ({((), σ')}, False) ∧ + NtfnStateRel N T (L.mem r.1) σ' ∧ EpStateRel T E R Bf (L.mem r.1) σ' ∧ SR r.1 σ' ⦄ := by + have hta := hP.e.tcb t ht + obtain ⟨hRN1, hRE1, -, -, -⟩ := clear_both hP s σ hRN hRE ht hk + have hS1 : SR (L.wr s (tFaultA t) 0#u64) (updK σ (specPtr t) (.tcb { x with tcbFault := none })) := + hH.stable _ _ _ _ hSR ((writesIn_wr (a_fault hta).1).mono fun b hb => by + subst hb; exact Or.inr ⟨t, ht, Or.inr (Or.inr (Or.inr (Or.inr (Or.inr rfl))))⟩) + (NtfnStep.tcbF hk x.tcbState x.tcbArch none) + (NoBlockE.updT hk _ (Or.inl ⟨rfl, fun _ => Or.inr (Or.inl ⟨_, hst⟩)⟩)) + have hnt : specPtr n ≠ specPtr t := by intro h; rw [h, hk] at hkz; cases hkz + have hkz1 : (updK σ (specPtr t) (.tcb { x with tcbFault := none })).kheap (specPtr n) = some (.notification z) := by + rw [updK_kheap, Function.update_of_ne hnt, hkz] + step with obj.Tcb.block_obj.spec as ⟨i, hi⟩ + have hi' : i = n := hi.trans hb1 + subst hi' + refine WP.spec_bind (cancel_signal_rel hH hSched hOE hP _ _ hRN1 hRE1 hS1 ht (x1 := { x with tcbFault := none }) + (by rw [updK_kheap, Function.update_self]) hn hst hkz1 hq) ?_ + rintro s2 ⟨σ', hcs, hRN2, hRE2, hS2⟩ + rw [WP.spec_ok] + exact ⟨rfl, σ', cancelIpc_ntfn hk hst hcs, hRN2, hRE2, hS2⟩ + +/-! ## The endpoint arms, with `NtfnStateRel` -/ + +/-- `set_thread_state t Inactive` after endpoint writes from `(s0, σ0)` that changed no other TCB, of a thread blocked +on an endpoint at `σ0`: both relations, one `stable` step (`NoBlockE` branch 3). -/ +theorem final_inactiveI (hSched : CancelScheduleA st N T SR) (hOE : SchedOpsEA st T E R Bf SR) (hP : IPlacedA N T E R Bf) + {s0 : S} {σ0 : Fl4SpecA64.State Unit} (hSR0 : SR s0 σ0) (s : S) (σ : Fl4SpecA64.State Unit) + (hRN : NtfnStateRel N T (L.mem s) σ) (hR : EpStateRel T E R Bf (L.mem s) σ) + (hW0 : WritesIn st s0 s (EWrite T E R Bf)) (hS0 : EpStep σ0 σ) + {t : U64} (ht : t ∈ T) {x0 x : Fl4SpecA64.Tcb} (hk0 : σ0.kheap (specPtr t) = some (.tcb x0)) + (hk : σ.kheap (specPtr t) = some (.tcb x)) (hO0 : OnlyT σ0 σ (specPtr t)) + (hblk : Fl4SpecA64.runnable x0.tcbState = false ∧ x0.tcbState ≠ .idleThreadState) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) + (h6 : tsCode x.tcbState ≠ 6) : + sched.set_thread_state st s t sched.TS_INACTIVE ⦃ s' => + NtfnStateRel N T (L.mem s') (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := .inactive })) (specPtr t)) ∧ + EpStateRel T E R Bf (L.mem s') (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := .inactive })) (specPtr t)) ∧ + SR s' (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := .inactive })) (specPtr t)) ⦄ := + set_thread_state_both hSched hOE hP s σ hRN hR ht hk + (notqN_of_code hRN (by rw [(hR.at_tcb ht hk).st]; exact h6)) hq fun s1 hw => hOE.stable _ _ _ _ hSR0 + (WritesIn.ew hW0 (hw.mono fun b hb => by subst hb; exact stA_ew hP.e ht)) + (hS0.trans (EpStep.tcb hk .inactive x.tcbArch)) + (NoBlockE.only hk0 (by rw [updK_kheap, Function.update_self]) (hO0.updP _) (Or.inr (Or.inr ⟨rfl, hblk⟩))) + +/-- The receive arm's second `set_thread_state t Inactive`, from the state `reply_unlink` leaves. -/ +theorem reply_secondI (hSched : CancelScheduleA st N T SR) (hOE : SchedOpsEA st T E R Bf SR) (hP : IPlacedA N T E R Bf) + (a : A) {s4 : S} {σ3 : Fl4SpecA64.State Unit} {t : U64} (ht : t ∈ T) {x3 : Fl4SpecA64.Tcb} + (hk3 : σ3.kheap (specPtr t) = some (.tcb x3)) + (hq3 : ∀ e ∈ E, ∀ z, σ3.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) + (hRN4 : NtfnStateRel N T (L.mem s4) (stsAct (updK σ3 (specPtr t) (.tcb { x3 with tcbState := .inactive })) + (specPtr t))) + (hR4 : EpStateRel T E R Bf (L.mem s4) (stsAct (updK σ3 (specPtr t) (.tcb { x3 with tcbState := .inactive })) + (specPtr t))) + (hS4 : SR s4 (stsAct (updK σ3 (specPtr t) (.tcb { x3 with tcbState := .inactive })) (specPtr t))) : + (do + let s5 ← sched.set_thread_state st s4 t sched.TS_INACTIVE + ok (s5, a)) ⦃ r => r.2 = a ∧ + NtfnStateRel N T (L.mem r.1) + (stsAct (updK (stsAct (updK σ3 (specPtr t) (.tcb { x3 with tcbState := .inactive })) (specPtr t)) (specPtr t) + (.tcb { { x3 with tcbState := .inactive } with tcbState := .inactive })) (specPtr t)) ∧ + EpStateRel T E R Bf (L.mem r.1) + (stsAct (updK (stsAct (updK σ3 (specPtr t) (.tcb { x3 with tcbState := .inactive })) (specPtr t)) (specPtr t) + (.tcb { { x3 with tcbState := .inactive } with tcbState := .inactive })) (specPtr t)) ∧ + SR r.1 (stsAct (updK (stsAct (updK σ3 (specPtr t) (.tcb { x3 with tcbState := .inactive })) (specPtr t)) (specPtr t) + (.tcb { { x3 with tcbState := .inactive } with tcbState := .inactive })) (specPtr t)) ⦄ := by + obtain ⟨hk4, hq4, -, -⟩ := sts_facts hk3 hq3 .inactive + refine WP.spec_bind (set_thread_state_both hSched hOE hP s4 _ hRN4 hR4 ht hk4 + (notqN_of_code hRN4 (by rw [(hRN4.at_tcb ht hk4).st]; show tsCode Fl4SpecA64.ThreadState.inactive ≠ 6; decide)) hq4 fun s1 hw => hOE.stable _ _ _ _ hS4 + (hw.mono fun b hb => by subst hb; exact stA_ew hP.e ht) (EpStep.tcb hk4 .inactive _) + (NoBlockE.updT hk4 _ (Or.inl ⟨rfl, id⟩))) ?_ + rintro s5 ⟨hRN5, hR5, hS5⟩ + rw [WP.spec_ok] + exact ⟨rfl, hRN5, hR5, hS5⟩ + +/-- `reply_unlink` of a receiver whose reply points back to it, after endpoint writes from `(s0, σ0)`: both +relations. -/ +theorem reply_unlink_relI (hSched : CancelScheduleA st N T SR) (hOE : SchedOpsEA st T E R Bf SR) (hP : IPlacedA N T E R Bf) + {s0 : S} {σ0 : Fl4SpecA64.State Unit} (hSR0 : SR s0 σ0) {s2 : S} {σ2 : Fl4SpecA64.State Unit} + (hRN2 : NtfnStateRel N T (L.mem s2) σ2) (hR2 : EpStateRel T E R Bf (L.mem s2) σ2) + (hW02 : WritesIn st s0 s2 (EWrite T E R Bf)) (hS02 : EpStep σ0 σ2) + {t : U64} (ht : t ∈ T) {x0 x1 : Fl4SpecA64.Tcb} (hk0 : σ0.kheap (specPtr t) = some (.tcb x0)) + (hk2 : σ2.kheap (specPtr t) = some (.tcb x1)) (hO2 : OnlyT σ0 σ2 (specPtr t)) + (hblk : Fl4SpecA64.runnable x0.tcbState = false ∧ x0.tcbState ≠ .idleThreadState) + (hq2 : ∀ e ∈ E, ∀ z, σ2.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) + (h6 : tsCode x1.tcbState ≠ 6) + {i : U64} (hiR : i ∈ R) {xr : Fl4SpecA64.Reply} (hkr2 : σ2.kheap (specPtr i) = some (.reply xr)) : + ipc.reply_unlink st s2 i t ⦃ s3 => + NtfnStateRel N T (L.mem s3) (stsAct (updK (updK σ2 (specPtr i) (.reply { xr with replyTcb := none })) + (specPtr t) (.tcb { x1 with tcbState := .inactive })) (specPtr t)) ∧ + EpStateRel T E R Bf (L.mem s3) (stsAct (updK (updK σ2 (specPtr i) (.reply { xr with replyTcb := none })) + (specPtr t) (.tcb { x1 with tcbState := .inactive })) (specPtr t)) ∧ + SR s3 (stsAct (updK (updK σ2 (specPtr i) (.reply { xr with replyTcb := none })) + (specPtr t) (.tcb { x1 with tcbState := .inactive })) (specPtr t)) ⦄ := by + have hPE := hP.e + have hit : specPtr i ≠ specPtr t := by intro h; rw [h, hk2] at hkr2; cases hkr2 + have hal := (a_rtcb hPE hiR).1 + have hag := agree_wr (L := L) (s := s2) (v := 0#u64) hal + have hR3 : EpStateRel T E R Bf (L.mem (L.wr s2 (rTcbA i) 0#u64)) + (updK σ2 (specPtr i) (.reply { xr with replyTcb := none })) := + hR2.upd_reply hPE hiR _ (hag.mono fun b hb => by rw [rTcbA_val]; exact hb.2) (by rw [rd_wr_eq hal, optRef_zero]) + have hRN3 : NtfnStateRel N T (L.mem (L.wr s2 (rTcbA i) 0#u64)) + (updK σ2 (specPtr i) (.reply { xr with replyTcb := none })) := + hRN2.frameX hP.n (hag.mono fun b hb => by rw [rTcbA_val]; exact nk_rep hP hiR hb) + (nkview_upd hkr2 (fun _ h => by cases h) fun _ h => by cases h) + have hW3 : WritesIn st s2 (L.wr s2 (rTcbA i) 0#u64) (EWrite T E R Bf) := + (writesIn_wr hal).mono fun b hb => by subst hb; exact Or.inr (Or.inl ⟨i, hiR, rTcbA_val i⟩) + have hk3 : (updK σ2 (specPtr i) (.reply { xr with replyTcb := none })).kheap (specPtr t) = some (.tcb x1) := by + rw [updK_kheap, Function.update_of_ne hit.symm, hk2] + have hq3 := notq_updR (E := E) hq2 i { xr with replyTcb := none } + unfold ipc.reply_unlink + step with obj.Reply.set_tcb.spec as ⟨s3, hs3⟩ + subst hs3 + exact final_inactiveI hSched hOE hP hSR0 _ _ hRN3 hR3 (WritesIn.ew hW02 hW3) (hS02.trans (EpStep.reply hkr2 none)) + ht hk0 hk3 (hO2.updO _ fun _ h => by cases h) hblk hq3 h6 + +/-- The receive arm with a reply object, after the dequeue: both relations. -/ +theorem reply_armI (hSched : CancelScheduleA st N T SR) (hOE : SchedOpsEA st T E R Bf SR) (hP : IPlacedA N T E R Bf) + (a : A) {s0 : S} {σ0 : Fl4SpecA64.State Unit} (hSR0 : SR s0 σ0) {s2 : S} {σ2 : Fl4SpecA64.State Unit} + (hRN2 : NtfnStateRel N T (L.mem s2) σ2) (hR2 : EpStateRel T E R Bf (L.mem s2) σ2) + (hW02 : WritesIn st s0 s2 (EWrite T E R Bf)) (hS02 : EpStep σ0 σ2) + {t : U64} (ht : t ∈ T) {x0 x1 : Fl4SpecA64.Tcb} (hk0 : σ0.kheap (specPtr t) = some (.tcb x0)) + (hk2 : σ2.kheap (specPtr t) = some (.tcb x1)) (hO2 : OnlyT σ0 σ2 (specPtr t)) + (hblk : Fl4SpecA64.runnable x0.tcbState = false ∧ x0.tcbState ≠ .idleThreadState) + (hq2 : ∀ e ∈ E, ∀ z, σ2.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) + {i : U64} (hiR : i ∈ R) {xr : Fl4SpecA64.Reply} (hkr2 : σ2.kheap (specPtr i) = some (.reply xr)) + (hback : xr.replyTcb = some (specPtr t)) {ep : Fl4SpecA64.ObjRef} {d : Fl4SpecA64.ReceiverPayload} + (hst1 : x1.tcbState = .blockedOnReceive ep (some (specPtr i)) d) : + (do + let s3 ← ipc.reply_unlink st s2 i t + let s4 ← sched.set_thread_state st s3 t sched.TS_INACTIVE + ok (s4, a)) ⦃ r => r.2 = a ∧ ∃ σ4 x4, + Fl4SpecA64.replyUnlinkTcb (Z := Unit) (specPtr t) (specPtr i) σ2 = ({((), σ4)}, False) ∧ + σ4.kheap (specPtr t) = some (.tcb x4) ∧ + NtfnStateRel N T (L.mem r.1) (stsAct (updK σ4 (specPtr t) (.tcb { x4 with tcbState := .inactive })) (specPtr t)) ∧ + EpStateRel T E R Bf (L.mem r.1) (stsAct (updK σ4 (specPtr t) (.tcb { x4 with tcbState := .inactive })) (specPtr t)) ∧ + SR r.1 (stsAct (updK σ4 (specPtr t) (.tcb { x4 with tcbState := .inactive })) (specPtr t)) ⦄ := by + have hit : specPtr i ≠ specPtr t := by intro h; rw [h, hk2] at hkr2; cases hkr2 + have hk3 : (updK σ2 (specPtr i) (.reply { xr with replyTcb := none })).kheap (specPtr t) = some (.tcb x1) := by + rw [updK_kheap, Function.update_of_ne hit.symm, hk2] + have hq3 := notq_updR (E := E) hq2 i { xr with replyTcb := none } + refine WP.spec_bind (reply_unlink_relI hSched hOE hP hSR0 hRN2 hR2 hW02 hS02 ht hk0 hk2 hO2 hblk hq2 + (by rw [hst1]; simp [tsCode]) hiR hkr2) ?_ + rintro s4 ⟨hRN4, hR4, hS4⟩ + refine WP.spec_mono (reply_secondI hSched hOE hP a ht hk3 hq3 hRN4 hR4 hS4) ?_ + rintro r ⟨h1, h2, h3, h4⟩ + exact ⟨h1, _, _, replyUnlinkTcb_eq hkr2 hback hk2 hst1, by rw [stsAct_kheap, updK_kheap, Function.update_self], + h2, h3, h4⟩ + +/-- `cancel_ipc`'s send / receive arm after the fault clear and the state read `w` (the extracted text). -/ +def epArmProg {S A : Type} (st : machine.Store S) [L : LawfulStore st] (s : S) (a : A) (t w : U64) : Result (S × A) := + do + let ep ← obj.Tcb.block_obj st t (L.wr s (tFaultA t) 0#u64) + let s2 ← endpoint.ep_dequeue st (L.wr s (tFaultA t) 0#u64) ep t + let s3 ← + if w = sched.TS_BLOCKED_ON_RECV + then + do + let i ← obj.Tcb.reply_obj st t s2 + if i != 0#u64 + then ipc.reply_unlink st s2 i t + else ok s2 + else ok s2 + let s4 ← sched.set_thread_state st s3 t sched.TS_INACTIVE + ok (s4, a) + +/-- **The send / receive arm of `cancel_ipc`** after the fault clear and the state read `w`: both relations. -/ +theorem ep_armI (hSched : CancelScheduleA st N T SR) (hOE : SchedOpsEA st T E R Bf SR) (hP : IPlacedA N T E R Bf) + (s : S) (a : A) (σ : Fl4SpecA64.State Unit) (hSR : SR s σ) {t : U64} (ht : t ∈ T) + {x : Fl4SpecA64.Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) {e : U64} (he : e ∈ E) + {z : Fl4SpecA64.Endpoint} (hkz : σ.kheap (specPtr e) = some (.endpoint z)) (hq : specPtr t ∈ z.epQueue) + (hrep : RepOKA σ (specPtr t)) + (hRN1 : NtfnStateRel N T (L.mem (L.wr s (tFaultA t) 0#u64)) (updK σ (specPtr t) (.tcb { x with tcbFault := none }))) + (hR1 : EpStateRel T E R Bf (L.mem (L.wr s (tFaultA t) 0#u64)) (updK σ (specPtr t) (.tcb { x with tcbFault := none }))) + (hW1 : WritesIn st s (L.wr s (tFaultA t) 0#u64) (EWrite T E R Bf)) + (hS1 : EpStep σ (updK σ (specPtr t) (.tcb { x with tcbFault := none }))) + (hb1 : L.mem (L.wr s (tFaultA t) 0#u64) (tBlockA t) = e) {w : U64} + (hcase : (∃ d, x.tcbState = .blockedOnSend (specPtr e) d ∧ w ≠ sched.TS_BLOCKED_ON_RECV) ∨ + (∃ ro d, x.tcbState = .blockedOnReceive (specPtr e) ro d ∧ w = sched.TS_BLOCKED_ON_RECV)) : + epArmProg st s a t w ⦃ r => r.2 = a ∧ ∃ σ', Fl4SpecA64.cancelIpc (Z := Unit) (specPtr t) σ = ({((), σ')}, False) ∧ + NtfnStateRel N T (L.mem r.1) σ' ∧ EpStateRel T E R Bf (L.mem r.1) σ' ∧ SR r.1 σ' ⦄ := by + unfold epArmProg + have hPE := hP.e + have hta := hPE.tcb t ht + have hte : specPtr e ≠ specPtr t := by intro h; rw [h, hk] at hkz; cases hkz + have hkz1 : (updK σ (specPtr t) (.tcb { x with tcbFault := none })).kheap (specPtr e) = some (.endpoint z) := by + rw [updK_kheap, Function.update_of_ne hte, hkz] + obtain ⟨xs, hQ, hxs, hB⟩ := (hR1.at_ep he hkz1).queue + have htx : t ∈ xs := by + have hq' := hq + rw [hxs, List.mem_map] at hq' + obtain ⟨y, hy, ey⟩ := hq' + rw [← specPtr_inj ey]; exact hy + have hblk : Fl4SpecA64.runnable x.tcbState = false ∧ x.tcbState ≠ .idleThreadState := by + rcases hcase with ⟨d, hs, -⟩ | ⟨ro, d, hs, -⟩ <;> rw [hs] <;> exact ⟨rfl, fun h => by cases h⟩ + have h6 : tsCode x.tcbState ≠ 6 := by + rcases hcase with ⟨d, hs, -⟩ | ⟨ro, d, hs, -⟩ <;> rw [hs] <;> simp [tsCode] + step with obj.Tcb.block_obj.spec as ⟨ep, hep⟩ + have hep' : ep = e := hep.trans hb1 + subst hep' + refine WP.spec_bind (WP.spec_and (ep_dequeue_raw hPE _ _ hR1 he hkz1 hq) (ep_dequeue_full hPE _ he t xs hQ htx)) ?_ + rintro s2 ⟨⟨hdq, hR2, hW2, hS2, hA2⟩, -, -, hQA, -⟩ + have hW02 := WritesIn.ew hW1 hW2 + have hS02 := hS1.trans hS2 + have hRN2 : NtfnStateRel N T (L.mem s2) (updK (updK σ (specPtr t) (.tcb { x with tcbFault := none })) (specPtr ep) + (.endpoint (epDeq z (specPtr t)))) := + hRN1.frameX hP.n (fun b hb hk' => hQA b hb (nk_ep hP he (fun w hw => ⟨hQ.mem w hw, (hB w hw).1⟩) hk')) + (nkview_upd hkz1 (fun _ h => by cases h) fun _ h => by cases h) + have hk2 : (updK (updK σ (specPtr t) (.tcb { x with tcbFault := none })) (specPtr ep) + (.endpoint (epDeq z (specPtr t)))).kheap (specPtr t) = some (.tcb { x with tcbFault := none }) := by + rw [updK_kheap, Function.update_of_ne hte.symm, updK_kheap, Function.update_self] + have hkz2 : (updK (updK σ (specPtr t) (.tcb { x with tcbFault := none })) (specPtr ep) + (.endpoint (epDeq z (specPtr t)))).kheap (specPtr ep) = some (.endpoint (epDeq z (specPtr t))) := by + rw [updK_kheap, Function.update_self] + have hb2 : L.mem s2 (tBlockA t) = ep := (hA2 _ (a_blk hta).1 ⟨t, ht, (a_blk hta).2⟩).trans hb1 + have hq2 := dequeued_not_queued hR2 he hb2 hkz2 (epDeq_not_mem hxs hQ.nodup t) + have hO2 : OnlyT σ (updK (updK σ (specPtr t) (.tcb { x with tcbFault := none })) (specPtr ep) + (.endpoint (epDeq z (specPtr t)))) (specPtr t) := + ((OnlyT.refl σ _).updP _).updO _ fun _ h => by cases h + rcases hcase with ⟨d, hs, hwr⟩ | ⟨ro, d, hs, hwr⟩ + · simp only [hwr, ↓reduceIte, bind_ok] + refine WP.spec_bind (final_inactiveI hSched hOE hP hSR _ _ hRN2 hR2 hW02 hS02 ht hk hk2 hO2 hblk hq2 h6) ?_ + rintro s4 ⟨hRN4, hR4, hS4⟩ + rw [WP.spec_ok] + exact ⟨rfl, _, cancelIpc_blocked hk none (Or.inl ⟨_, d, hs, rfl⟩) (bci_none (by rw [hs]; rfl) hdq hk2), + hRN4, hR4, hS4⟩ + · simp only [hwr, ↓reduceIte] + have hrec := (hR2.at_tcb ht hk2).recv (specPtr ep) ro d hs + step with obj.Tcb.reply_obj.spec as ⟨i, hi⟩ + have hi' : i = L.mem s2 (tReplyA t) := hi + by_cases hi0 : i = 0#u64 + · have hro : ro = none := by rw [← hrec.1, ← hi', hi0, optRef_zero] + subst hro + simp only [hi0, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte, bind_ok] + refine WP.spec_bind (final_inactiveI hSched hOE hP hSR _ _ hRN2 hR2 hW02 hS02 ht hk hk2 hO2 hblk hq2 h6) ?_ + rintro s4 ⟨hRN4, hR4, hS4⟩ + rw [WP.spec_ok] + exact ⟨rfl, _, cancelIpc_blocked hk none (Or.inr ⟨_, d, hs⟩) (bci_none (by rw [hs]; rfl) hdq hk2), + hRN4, hR4, hS4⟩ + · have hro : ro = some (specPtr i) := by rw [← hrec.1, ← hi', optRef_ne hi0] + subst hro + have hiR : i ∈ R := by rw [hi']; exact hrec.2 (by simp) + obtain ⟨xr, hkr, hback⟩ := hrep x hk _ _ d hs + have hit : specPtr i ≠ specPtr t := by intro h; rw [h, hk] at hkr; cases hkr + have hie : specPtr i ≠ specPtr ep := by intro h; rw [h, hkz] at hkr; cases hkr + have hkr2 : (updK (updK σ (specPtr t) (.tcb { x with tcbFault := none })) (specPtr ep) + (.endpoint (epDeq z (specPtr t)))).kheap (specPtr i) = some (.reply xr) := by + rw [updK_kheap, Function.update_of_ne hie, updK_kheap, Function.update_of_ne hit, hkr] + have hnz : (i != 0#u64) = true := by simpa using hi0 + simp only [hnz, ↓reduceIte] + refine WP.spec_mono (reply_armI hSched hOE hP a hSR hRN2 hR2 hW02 hS02 ht hk hk2 hO2 hblk hq2 hiR hkr2 hback hs) ?_ + rintro r ⟨h1, σ4, x4, hun, hk4, hRN5, hR5, hS5⟩ + exact ⟨h1, _, cancelIpc_blocked hk _ (Or.inr ⟨_, d, hs⟩) (bci_reply (by rw [hs]; rfl) hdq hun hk4), + hRN5, hR5, hS5⟩ + +/-! ## `cancel_ipc` -/ + +set_option maxHeartbeats 4000000 in +/-- **`cancel_ipc` refines l4v `cancel_ipc` on both IPC relations** (every state but `BlockedOnReply`), from `RepOKA` +and the case / placement hypotheses `hE` / `hN` (the endpoint or notification the thread is blocked on is tracked and +holds it in its queue). -/ +theorem cancel_ipc_rel (hH : SchedHypsA st N T SR) (hSched : CancelScheduleA st N T SR) (hOE : SchedOpsEA st T E R Bf SR) + (hP : IPlacedA N T E R Bf) (s : S) (a : A) (σ : Fl4SpecA64.State Unit) + (hRN : NtfnStateRel N T (L.mem s) σ) (hRE : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) + {t : U64} (ht : t ∈ T) {x : Fl4SpecA64.Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hnr : ∀ r, x.tcbState ≠ .blockedOnReply r) + (hE : ∀ e', Fl4SpecA64.epBlocked x.tcbState = some e' → L.mem s (tBlockA t) ∈ E ∧ + ∃ z, σ.kheap e' = some (.endpoint z) ∧ specPtr t ∈ z.epQueue) + (hN : ∀ n', x.tcbState = .blockedOnNotification n' → L.mem s (tBlockA t) ∈ N ∧ + ∃ z, σ.kheap n' = some (.notification z) ∧ specPtr t ∈ z.ntfnObj.ntfnQueue) + (hrep : RepOKA σ (specPtr t)) : + ipc.cancel_ipc st ar s a t ⦃ r => r.2 = a ∧ ∃ σ', Fl4SpecA64.cancelIpc (Z := Unit) (specPtr t) σ = ({((), σ')}, False) ∧ + NtfnStateRel N T (L.mem r.1) σ' ∧ EpStateRel T E R Bf (L.mem r.1) σ' ∧ SR r.1 σ' ⦄ := by + have hPE := hP.e + have hta := hPE.tcb t ht + have hx := hRE.at_tcb ht hk + have hxN := hRN.at_tcb ht hk + obtain ⟨hRN1, hR1, hW1, hS1, hag⟩ := clear_both hP s σ hRN hRE ht hk + have vf := val_fault hta + have hb1 : L.mem (L.wr s (tFaultA t) 0#u64) (tBlockA t) = L.mem s (tBlockA t) := + hag _ (a_blk hta).1 (by dsimp only; rw [val_blk hta, vf]; omega) + unfold ipc.cancel_ipc faults.clear_fault + step with obj.Tcb.set_fault_type.spec as ⟨s1, hs1⟩ + have e1 : s1 = L.wr s (tFaultA t) 0#u64 := by rw [hs1]; unfold fl4_abi.consts.fault.NullFault; rfl + subst e1 + step with obj.Tcb.state.spec as ⟨w, hw⟩ + have hw' : w.val = tsCode x.tcbState := by + rw [hw, hag _ (a_st hta).1 (by dsimp only; rw [val_st hta, vf]; omega)]; exact hx.st + have hcS : ∀ e d, x.tcbState = .blockedOnSend e d → w = sched.TS_BLOCKED_ON_SEND := fun e d hs => + UScalar.eq_of_val_eq (by rw [hw', hs, tsVals.2.2.2.1]; rfl) + have hcR : ∀ e r d, x.tcbState = .blockedOnReceive e r d → w = sched.TS_BLOCKED_ON_RECV := fun e r d hs => + UScalar.eq_of_val_eq (by rw [hw', hs, tsVals.2.2.1]; rfl) + have hSR4 : sched.TS_BLOCKED_ON_RECV ≠ sched.TS_BLOCKED_ON_SEND := by rw [tsVals.2.2.1, tsVals.2.2.2.1]; decide + have hN6S : sched.TS_BLOCKED_ON_NTFN ≠ sched.TS_BLOCKED_ON_SEND := by rw [tsVals.2.2.2.2, tsVals.2.2.2.1]; decide + have hN6R : sched.TS_BLOCKED_ON_NTFN ≠ sched.TS_BLOCKED_ON_RECV := by rw [tsVals.2.2.2.2, tsVals.2.2.1]; decide + -- the endpoint arms + have harm : ∀ (e' : Fl4SpecA64.ObjRef), Fl4SpecA64.epBlocked x.tcbState = some e' → + ∃ e ∈ E, e' = specPtr e ∧ L.mem (L.wr s (tFaultA t) 0#u64) (tBlockA t) = e ∧ + ∃ z, σ.kheap (specPtr e) = some (.endpoint z) ∧ specPtr t ∈ z.epQueue := fun e' hb => by + obtain ⟨heE, z, hkz, hq⟩ := hE e' hb + have he' : specPtr (L.mem s (tBlockA t)) = e' := hx.blk e' hb + subst he' + exact ⟨_, heE, rfl, hb1, z, hkz, hq⟩ + by_cases hSd : ∃ e' d, x.tcbState = .blockedOnSend e' d + · obtain ⟨e', d, hs⟩ := hSd + obtain ⟨e, he, rfl, hbe, z, hkz, hq⟩ := harm e' (by rw [hs]; rfl) + have hwS := hcS _ d hs + rw [if_pos hwS] + exact ep_armI hSched hOE hP s a σ hSR ht hk he hkz hq hrep hRN1 hR1 hW1 hS1 hbe + (Or.inl ⟨d, hs, by rw [hwS]; exact hSR4.symm⟩) + by_cases hRc : ∃ e' ro d, x.tcbState = .blockedOnReceive e' ro d + · obtain ⟨e', ro, d, hs⟩ := hRc + obtain ⟨e, he, rfl, hbe, z, hkz, hq⟩ := harm e' (by rw [hs]; rfl) + have hwR := hcR _ ro d hs + rw [if_neg (by rw [hwR]; exact hSR4), if_pos hwR] + exact ep_armI hSched hOE hP s a σ hSR ht hk he hkz hq hrep hRN1 hR1 hW1 hS1 hbe (Or.inr ⟨ro, d, hs, hwR⟩) + have hwS : w ≠ sched.TS_BLOCKED_ON_SEND := fun h => + hSd (tsCode_four (by rw [← hw', h, tsVals.2.2.2.1]; rfl)) + have hwR : w ≠ sched.TS_BLOCKED_ON_RECV := fun h => + hRc (tsCode_three (by rw [← hw', h, tsVals.2.2.1]; rfl)) + rw [if_neg hwS, if_neg hwR] + by_cases hNc : ∃ n', x.tcbState = .blockedOnNotification n' + · obtain ⟨n', hs⟩ := hNc + obtain ⟨hnN, z, hkz, hq⟩ := hN n' hs + have hn' : specPtr (L.mem s (tBlockA t)) = n' := hxN.blk n' hs + subst hn' + have hw6 : w = sched.TS_BLOCKED_ON_NTFN := UScalar.eq_of_val_eq (by rw [hw', hs, tsVals.2.2.2.2]; rfl) + rw [if_pos hw6] + exact ntfn_arm hH hSched hOE hP s a σ hRN hRE hSR ht hk hnN hs hkz hq hb1 + have hw6 : w ≠ sched.TS_BLOCKED_ON_NTFN := fun h => + hNc (tsCode_six' (by rw [← hw', h, tsVals.2.2.2.2]; rfl)) + have hw5 : w ≠ sched.TS_BLOCKED_ON_REPLY := fun h => by + obtain ⟨r, hr⟩ := tsCode_five (by rw [← hw', h]; unfold sched.TS_BLOCKED_ON_REPLY; rfl) + exact hnr r hr + rw [if_neg hw6, if_neg hw5, WP.spec_ok] + refine ⟨rfl, _, cancelIpc_plain hk (fun e d h => hSd ⟨e, d, h⟩) (fun e r d h => hRc ⟨e, r, d, h⟩) + (fun n h => hNc ⟨n, h⟩) hnr, hRN1, hR1, ?_⟩ + refine hOE.stable _ _ _ _ hSR hW1 hS1 (NoBlockE.updT hk _ (Or.inl ⟨rfl, fun hc => ?_⟩)) + rcases hc with h | h | ⟨e, d, h, -⟩ | h + · exact Or.inl h + · exact Or.inr (Or.inl h) + · exact absurd ⟨e, d, h⟩ hSd + · exact Or.inr (Or.inr (Or.inr h)) + +/-! ## Cancellation of a bound receiver -/ + +/-- A receive-blocked TCB in its endpoint queue can be cancelled while keeping both IPC relations and `SR`. -/ +theorem cancelIpc_of_rel (hH : SchedHypsA st N T SR) (hSched : CancelScheduleA st N T SR) + (hOE : SchedOpsEA st T E R Bf SR) (hP : IPlacedA N T E R Bf) + (s : S) (a : A) (σ : Fl4SpecA64.State Unit) {t : U64} {x : Fl4SpecA64.Tcb} + (hRN : NtfnStateRel N T (L.mem s) σ) (hRE : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) + (ht : t ∈ T) (hk : σ.kheap (specPtr t) = some (.tcb x)) (hrb : Fl4SpecA64.receiveBlocked x.tcbState = true) + (hrep : RepOKA σ (specPtr t)) (heE : L.mem s (tBlockA t) ∈ E) + (hz : ∀ e', Fl4SpecA64.epBlocked x.tcbState = some e' → + ∃ z, σ.kheap e' = some (.endpoint z) ∧ specPtr t ∈ z.epQueue) : + ipc.cancel_ipc st ar s a t ⦃ r => ∃ σ' x', Fl4SpecA64.cancelIpc (Z := Unit) (specPtr t) σ = ({((), σ')}, False) ∧ + NtfnStateRel N T (L.mem r.1) σ' ∧ EpStateRel T E R Bf (L.mem r.1) σ' ∧ SR r.1 σ' ∧ + σ'.kheap (specPtr t) = some (.tcb x') ∧ x'.tcbState = .inactive ∧ RelIfBound σ' (specPtr t) ⦄ := by + obtain ⟨e, ro, d, hst⟩ : ∃ e ro d, x.tcbState = .blockedOnReceive e ro d := by + generalize x.tcbState = ts at hrb ⊢ + cases ts <;> first | exact ⟨_, _, _, rfl⟩ | simp [Fl4SpecA64.receiveBlocked] at hrb + obtain ⟨z, hkz, hq⟩ := hz e (by rw [hst]; rfl) + obtain ⟨σ', x', hc, hk', hin, hsc, hrq, hct, hscs⟩ := cancelIpc_recv_out hk hst hkz hq hrep + have hrel : RelIfBound σ (specPtr t) := hOE.rd _ _ _ _ hSR hk (Or.inr (Or.inr (Or.inr ⟨_, _, _, hst⟩))) + refine WP.spec_mono (cancel_ipc_rel hH hSched hOE hP s a σ hRN hRE hSR ht hk (fun r h => by rw [hst] at h; cases h) + (fun e' hb => ⟨heE, hz e' hb⟩) (fun n' h => by rw [hst] at h; cases h) hrep) ?_ + rintro r ⟨-, σ'', hc'', hRN', hRE', hS'⟩ + rw [hc] at hc'' + have he : σ' = σ'' := congrArg Prod.snd (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst hc'')) + subst he + refine ⟨_, x', hc, hRN', hRE', hS', hk', hin, RelIfBound.keep hrel hrq hct (fun y hy => ?_) hscs⟩ + rw [hk'] at hy + injection hy with hy; injection hy with hy + subst hy + exact ⟨x, hk, hsc.symm⟩ + +end Cross + +end + +end Fl4ProofsA64.P3.Endp + +namespace Fl4ProofsA64.P3.Notif +open fl4_core_a64 Fl4ProofsA64.P1 Endp + +noncomputable section + +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + {N T E R : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {SR : S → Fl4SpecA64.State Unit → Prop} {DL : S → U64 → Prop} + +/-- **Idle, bound TCB blocked on a receive**: its IPC is cancelled, then it +is woken with the badge. -/ +theorem send_signal_idle_bound_recv (hH : SchedHypsA st N T SR) (hO : SchedOpsA st ar N T SR DL) + (hOE : SchedOpsEA st T E R Bf SR) (hP : IPlacedA N T E R Bf) (s : S) (a : A) + (σ : Fl4SpecA64.State Unit) (hR : NtfnStateRel N T (L.mem s) σ) + (hRE : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) {n : U64} (hn : n ∈ N) + {x : Fl4SpecA64.Notification} (hk : σ.kheap (specPtr n) = some (.notification x)) (hidle : x.ntfnObj = .idleNtfn) + {t : Fl4SpecA64.ObjRef} {tcb : Fl4SpecA64.Tcb} (hb : x.ntfnBoundTcb = some t) (hkt : σ.kheap t = some (.tcb tcb)) + (hrb : Fl4SpecA64.receiveBlocked tcb.tcbState = true) + (hrep : RepOKA σ t) (heE : L.mem s (tBlockA (L.mem s (nBoundA n))) ∈ E) + (hz : ∀ e', Fl4SpecA64.epBlocked tcb.tcbState = some e' → + ∃ z, σ.kheap e' = some (.endpoint z) ∧ t ∈ z.epQueue) (badge : U64) + (hon : L.mem s (affA (L.mem s (nBoundA n))) = L.cpu s) (hdl : DL s n) : + notification.send_signal st ar s a n badge ⦃ r => ¬ (Fl4SpecA64.sendSignal (specPtr n) badge.bv σ).2 ∧ + ∃ σ', ((), σ') ∈ (Fl4SpecA64.sendSignal (specPtr n) badge.bv σ).1 ∧ NtfnStateRel N T (L.mem r.1) σ' ∧ SR r.1 σ' ⦄ := by + have hx := hR.at_ntfn hn hk + obtain ⟨hb0, hbt⟩ := optRef_some (hx.bound ▸ hb) + subst hbt + have hiT := hx.bound_mem hb0 + have hy := hR.at_tcb hiT hkt + have hs3 : L.mem s (stA (L.mem s (nBoundA n))) = sched.TS_BLOCKED_ON_RECV := by + have h3 := hy.st + rw [tsVals.2.2.1] + apply UScalar.eq_of_val_eq + revert hrb h3 + cases tcb.tcbState with + | ext e => cases e <;> simp [tsCode, Fl4SpecA64.receiveBlocked] + | _ => simp [tsCode, Fl4SpecA64.receiveBlocked] + unfold notification.send_signal + step with obj.Ntfn.state.spec as ⟨ns, hns⟩ + have hIdle : ns = obj.NTFN_IDLE := by rw [hns]; exact stOf_idle hx.obj hidle + simp only [hIdle, ↓reduceIte] + step with obj.Ntfn.bound_tcb.spec as ⟨i, hi⟩ + step with obj.Tcb.is_null.spec as ⟨b, hNull⟩ + subst i + simp only [hNull, hb0, decide_false, Bool.false_eq_true, ↓reduceIte] + step with obj.Tcb.state.spec as ⟨ts, hts⟩ + simp only [hts, hs3, ↓reduceIte] + refine WP.spec_bind (cancelIpc_of_rel hH hO.scheduleTcb hOE hP s a σ hR hRE hSR hiT hkt hrb hrep heE hz) ?_ + rintro ⟨s1, a1⟩ ⟨σ1, x1, hc, hR1, -, hSR1, hk1, hin, hrb1⟩ + have hspec : Fl4SpecA64.sendSignal (specPtr n) badge.bv σ = specWake (specPtr (L.mem s (nBoundA n))) (specPtr n) badge.bv σ1 := by + unfold Fl4SpecA64.sendSignal + rw [nd_bind_one (getNotification_eq hk)] + simp only [hidle, hb] + rw [nd_bind_one (getThreadState_eq hkt)] + simp only [hrb, ↓reduceIte] + rw [nd_bind_one hc] + rfl + rw [hspec] + have hq1 : ∀ n' ∈ N, ∀ z, σ1.kheap (specPtr n') = some (.notification z) → + specPtr (L.mem s (nBoundA n)) ∉ z.ntfnObj.ntfnQueue := by + intro n' hn' z hz hm + obtain ⟨-, h6, -⟩ := (hR1.at_ntfn hn' hz).obj.blocked hm + have := (hR1.at_tcb hiT hk1).st + rw [hin, h6] at this + cases this + exact wake_refines hH hO hP.n s1 a1 σ1 hR1 hSR1 hiT hn hk1 hq1 badge (Or.inr hrb1) (by rw [hin]; rfl) + (by rw [hin]; exact fun h => by cases h) ((hO.coreKeep _ _ _ _ hSR hSR1).onCore hiT hon) hR hSR hdl + (cancelIpc_recv_nscK hkt hrb hc) + +end +end Fl4ProofsA64.P3.Notif diff --git a/proofs/Fl4ProofsA64/P3/IpcCapsFullA.lean b/proofs/Fl4ProofsA64/P3/IpcCapsFullA.lean new file mode 100644 index 000000000..8d9449254 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/IpcCapsFullA.lean @@ -0,0 +1,150 @@ +/- +P3 (aarch64, Stage-3 `hcaps` drop, ruling (b), contract §13.4): **the send and receive joints without `hcaps`**. +`send_ipc_recvA` (`EpSendRecvA`) and `receive_from_senderA` (`EpRecvSendA`) take `hcase` (a faulted sender, or no +extra-cap insert); this module splits on it: the old theorems when it holds, `send_ipc_recvCapsA` / +`receive_from_senderCapsA` otherwise. New module; no existing statement changed. + +* **`send_ipc_recvFullA`**, **`receive_from_senderFullA`**: one outcome by membership, `EpStateRel`, the scheduler + relation, and the footprint `FpA` from the entry state or `XcJointFootA` (cap transfer). + +Case hypotheses (no named premise): those of the old theorems with `hcase` replaced by `FaultOKA` for a faulted +sender, and those of `do_ipc_transfer_capsA` at the entry state (as `send_ipc_recvCapsA`). +-/ +import Fl4ProofsA64.P3.IpcCapsRecvA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3 Fl4ProofsA64.P3.Notif Fl4SpecA64 Sched + +noncomputable section + +section Fl4 +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS : Set U64} +variable {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} +variable {Wr : State Unit → S → U64 → Prop} + +/-- **`send_ipc` to a waiting receiver without `hcase`** (module docstring). -/ +theorem send_ipc_recvFullA (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (hF : SchedFootEA st Z.T E R Bf (IpcSchedRelA st Z m κ) Wr) + {s : S} {a : A} {σ : State Unit} (hPl : PlaceA Z.th (L.ksb s)) + (hR : EpStateRel Z.T E R Bf (L.mem s) σ) (hSR : IpcSchedRelA st Z m κ s σ) + (hSt : StateRelA Z.th (L.mem s) σ) (hV : SlotsValidA Z.th (L.mem s)) (hC : BufCover st Z.T Bf s σ) + (hNative : BufNativeA Z.T (L.mem s)) + (hoff : ∀ y, IsSlotA Z.th y → ¬ EWrite Z.T E R Bf y ∧ ¬ EWrite Z.T E R Bf (core.num.U64.wrapping_add y 8#u64)) + {t : U64} (ht : t ∈ Z.T) (hth : Z.th t = some .tcb) {xt : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hnb : tsCode xt.tcbState ≠ 3) + {e d : U64} (he : e ∈ E) {q : List ObjRef} (hk : σ.kheap (specPtr e) = some (.endpoint (.recvEP (specPtr d :: q)))) + (hdh : Z.th d = some .tcb) {xd : Tcb} (hkd : σ.kheap (specPtr d) = some (.tcb xd)) + (hu : ∀ rp, ipcBufOfA true xd = some rp → ∀ k, k < 128 → inUserFrame (bufAddr rp k) σ) + (hrep : RepOKA σ (specPtr d)) (hon : L.mem s (affA d) = L.cpu s) + (badge : U64) (g gr : Bool) (blk call cd : Bool) + (hfault : ∀ f, xt.tcbFault = some f → FaultOKA (L := L) s t (ipcBufOfA true xd) xt f) + (hbr : (call = false ∧ xt.tcbFault = none ∧ cd = false) ∨ + ((call = true ∨ xt.tcbFault ≠ none) ∧ + ((g = false ∧ gr = false) ∨ ∀ e' r dd, xd.tcbState ≠ .blockedOnReceive e' (some r) dd) ∧ + SndRunA σ (specPtr t) xt)) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hJ : ∀ s1 σ1, EpStateRel Z.T E R Bf (L.mem s1) σ1 → IpcSchedRelA st Z m κ s1 σ1 → + WritesIn st s s1 (EWrite Z.T E R Bf) → EpStep σ σ1 → + InsBaseA (L := L) AR Z H OS s1 a σ1 m ∧ TreeA st OS Z.G.chains Z.G.par s1 ∧ + (∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s1 y = .Null) ∧ + ∀ bp ∈ Bf, ∀ k : U64, k.val < 128 → + ¬ FootA (L := L) Z s1 (bufW bp.1 k) ∧ ¬ InOS OS (bufW bp.1 k)) + (hep : ∀ s1 (x : U64) (c : Cap), CapRelA (capAtA (L.mem s1) x) c → + (UnwCA e (capA st s1 x) ↔ SUnwA (some (specPtr e)) c)) + (hE : ∀ x, W7.CapW H.tcbs {b | InOS OS b} ∅ x → ¬ EFoot Z.T E R Bf x) + (hRd : ∀ s1 σ1, L.ksb s1 = L.ksb s → IpcSchedRelA st Z m κ s1 σ1 → + ∀ x, W7.CapW H.tcbs {b | InOS OS b} ∅ x → ¬ SReadA st Z m s1 σ1 x) : + endpoint.send_ipc st ar s a blk call badge g gr cd t e ⦃ r => r.2 = a ∧ + ∃ σ', ((), σ') ∈ (sendIpc (Z := Unit) blk call badge.bv g gr cd (specPtr t) (specPtr e) σ).1 ∧ + EpStateRel Z.T E R Bf (L.mem r.1) σ' ∧ IpcSchedRelA st Z m κ r.1 σ' ∧ + (FpA st Z.T E R Bf Wr σ s r.1 ∨ XcJointFootA st AR Z OS m E R Bf Wr s a σ r.1) ⦄ := by + have hO := schedOpsEAOfPlacementA (st := st) (m := m) (κ := κ) P + by_cases hc : (xt.tcbFault = none ∧ (g = false ∨ ipcBufOfA false xt = none ∨ + unat (dataToMessageInfo (xt.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0)) ∨ + ∃ f, xt.tcbFault = some f ∧ FaultOKA (L := L) s t (ipcBufOfA true xd) xt f + · refine WP.spec_mono (send_ipc_recvA hO hF PE hPl s a σ hR hSR hSt hV hC hNative hoff ht hth hkt hnb he hk hdh + hkd hu hrep hon badge g gr blk call cd hc hbr) ?_ + rintro r ⟨ha, -, σ', hm, hR', hS', hfp⟩ + exact ⟨ha, σ', hm, hR', hS', Or.inl hfp⟩ + · have hft : xt.tcbFault = none := by + cases e1 : xt.tcbFault with + | none => rfl + | some f => exact absurd (Or.inr ⟨f, e1, hfault f e1⟩) hc + have hbr' : (call = false ∧ cd = false) ∨ + (call = true ∧ ((g = false ∧ gr = false) ∨ ∀ e' r dd, xd.tcbState ≠ .blockedOnReceive e' (some r) dd) ∧ + SndRunA σ (specPtr t) xt) := by + rcases hbr with ⟨h1, -, h3⟩ | ⟨h1, h2, h3⟩ + · exact Or.inl ⟨h1, h3⟩ + · exact Or.inr ⟨h1.resolve_right (fun h => h hft), h2, h3⟩ + refine WP.spec_mono (send_ipc_recvCapsA (AR := AR) (H := H) (OS := OS) P PE hF hPl hR hSR hSt hV hC hNative + hoff ht hth hkt hnb hft he hk hdh hkd hu hrep hon badge g gr blk call cd hbr' hsub hJ hep hE hRd) ?_ + rintro r ⟨ha, σ', hm, hR', hS', hX⟩ + exact ⟨ha, σ', hm, hR', hS', Or.inr hX⟩ + +/-- **`receive_from_sender` without `hcase`** (module docstring). -/ +theorem receive_from_senderFullA (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (hF : SchedFootEA st Z.T E R Bf (IpcSchedRelA st Z m κ) Wr) + {s : S} {a : A} {σ : State Unit} (hPl : PlaceA Z.th (L.ksb s)) + (hR : EpStateRel Z.T E R Bf (L.mem s) σ) (hSR : IpcSchedRelA st Z m κ s σ) + (hSt : StateRelA Z.th (L.mem s) σ) (hV : SlotsValidA Z.th (L.mem s)) (hC : BufCover st Z.T Bf s σ) + (hNative : BufNativeA Z.T (L.mem s)) + (hoff : ∀ y, IsSlotA Z.th y → ¬ EWrite Z.T E R Bf y ∧ ¬ EWrite Z.T E R Bf (core.num.U64.wrapping_add y 8#u64)) + {t : U64} (ht : t ∈ Z.T) (hth : Z.th t = some .tcb) {xt : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hnb : tsCode xt.tcbState ≠ 4) + {e sd : U64} (he : e ∈ E) {q : List ObjRef} + (hk : σ.kheap (specPtr e) = some (.endpoint (.sendEP (specPtr sd :: q)))) + (hsh : Z.th sd = some .tcb) {xs : Tcb} (hks : σ.kheap (specPtr sd) = some (.tcb xs)) + (hu : ∀ rp, ipcBufOfA true xt = some rp → ∀ k, k < 128 → inUserFrame (bufAddr rp k) σ) + (rv : U64) {rep : Option ObjRef} (hrv : optRef rv = rep) + (hfault : ∀ f, xs.tcbFault = some f → FaultOKA (L := L) s sd (ipcBufOfA true xt) xs f) + (hbr : ∀ e' dd, xs.tcbState = .blockedOnSend e' dd → + (dd.senderIsCall = false ∧ xs.tcbFault = none ∧ L.mem s (affA sd) = L.cpu s) ∨ + ((dd.senderIsCall = true ∨ xs.tcbFault ≠ none) ∧ + ((dd.senderCanGrant = false ∧ dd.senderCanGrantReply = false) ∨ rep = none))) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hJ : ∀ s1 σ1, EpStateRel Z.T E R Bf (L.mem s1) σ1 → IpcSchedRelA st Z m κ s1 σ1 → + WritesIn st s s1 (EWrite Z.T E R Bf) → EpStep σ σ1 → + InsBaseA (L := L) AR Z H OS s1 a σ1 m ∧ TreeA st OS Z.G.chains Z.G.par s1 ∧ + (∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s1 y = .Null) ∧ + ∀ bp ∈ Bf, ∀ k : U64, k.val < 128 → + ¬ FootA (L := L) Z s1 (bufW bp.1 k) ∧ ¬ InOS OS (bufW bp.1 k)) + (hep : ∀ s1 (x : U64) (c : Cap), CapRelA (capAtA (L.mem s1) x) c → + (UnwCA e (capA st s1 x) ↔ SUnwA (some (specPtr e)) c)) + (hE : ∀ x, W7.CapW H.tcbs {b | InOS OS b} ∅ x → ¬ EFoot Z.T E R Bf x) + (hRd : ∀ s1 σ1, L.ksb s1 = L.ksb s → IpcSchedRelA st Z m κ s1 σ1 → + ∀ x, W7.CapW H.tcbs {b | InOS OS b} ∅ x → ¬ SReadA st Z m s1 σ1 x) : + endpoint.receive_from_sender st ar s a t e rv ⦃ r => r.2 = a ∧ + ∃ σ', ((), σ') ∈ (specRecvSendA (specPtr t) (specPtr e) rep (specPtr sd :: q) σ).1 ∧ + EpStateRel Z.T E R Bf (L.mem r.1) σ' ∧ IpcSchedRelA st Z m κ r.1 σ' ∧ + (FpA st Z.T E R Bf Wr σ s r.1 ∨ XcJointFootA st AR Z OS m E R Bf Wr s a σ r.1) ⦄ := by + have hO := schedOpsEAOfPlacementA (st := st) (m := m) (κ := κ) P + by_cases hc : ∀ e' dd, xs.tcbState = .blockedOnSend e' dd → + (xs.tcbFault = none ∧ (dd.senderCanGrant = false ∨ ipcBufOfA false xs = none ∨ + unat (dataToMessageInfo (xs.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0)) ∨ + ∃ f, xs.tcbFault = some f ∧ FaultOKA (L := L) s sd (ipcBufOfA true xt) xs f + · refine WP.spec_mono (receive_from_senderA hO hF PE hPl s a σ hR hSR hSt hV hC hNative hoff ht hth hkt hnb he + hk hsh hks hu rv hrv hc hbr) ?_ + rintro r ⟨ha, -, σ', hm, hR', hS', hfp⟩ + exact ⟨ha, σ', hm, hR', hS', Or.inl hfp⟩ + · have hfs : xs.tcbFault = none := by + cases e1 : xs.tcbFault with + | none => rfl + | some f => exact absurd (fun _ _ _ => Or.inr ⟨f, e1, hfault f e1⟩) hc + have hbr' : ∀ e' dd, xs.tcbState = .blockedOnSend e' dd → + (dd.senderIsCall = false ∧ L.mem s (affA sd) = L.cpu s) ∨ + (dd.senderIsCall = true ∧ ((dd.senderCanGrant = false ∧ dd.senderCanGrantReply = false) ∨ rep = none)) := + fun e' dd h => (hbr e' dd h).imp (fun h' => ⟨h'.1, h'.2.2⟩) + (fun h' => ⟨h'.1.resolve_right (fun h => h hfs), h'.2⟩) + refine WP.spec_mono (receive_from_senderCapsA (AR := AR) (H := H) (OS := OS) P PE hF hPl hR hSR hSt hV hC + hNative hoff ht hth hkt hnb he hk hsh hks hfs hu rv hrv hbr' hsub hJ hep hE hRd) ?_ + rintro r ⟨ha, σ', hm, hR', hS', hX⟩ + exact ⟨ha, σ', hm, hR', hS', Or.inr hX⟩ + +end Fl4 + +end + +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/IpcCapsRecvA.lean b/proofs/Fl4ProofsA64/P3/IpcCapsRecvA.lean new file mode 100644 index 000000000..4e6098848 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/IpcCapsRecvA.lean @@ -0,0 +1,279 @@ +/- +P3 (aarch64, Stage-3 `hcaps` drop, ruling (b), contract §13.4): **`receive_from_sender` with an extra-cap +transfer** (the `SendEP` arm of `receive_ipc` for a granting, fault-free sender whose message carries extra caps). +`EpRecvSendA.receive_from_senderA` excludes this case (`hcase`); as for the send side (`IpcCapsSendA`), the caps +joint is a new module downstream of `do_ipc_transfer_capsA`. No existing statement changed. + +* Spec: **`recvSend_runMemA`** / **`recvSend_inactMemA`** (`recvSend_runA` / `recvSend_inactA` by membership). +* **`receive_from_senderCapsA`**: dequeue, the sender's payload words, `do_ipc_transfer_capsA`, then the tails of + `receive_from_senderA` from the transfer's post-state (the sender's TCB is kept up to its caps, `XcKhFrA.tcbA`). + +Case hypotheses (no named premise): those of `receive_from_senderA` without `hcase` (the sender has no fault) and +those of `do_ipc_transfer_capsA` at the entry state. The post gives an outcome by membership and the footprint +`XcJointFootA`. +-/ +import Fl4ProofsA64.P3.IpcCapsSendA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3 Fl4ProofsA64.P3.Notif Fl4SpecA64 Sched + +noncomputable section + +section Spec +open Fl4Spec +open Classical + +/-- `recvSend_runA` by membership (the sender's TCB after the transfer equal up to caps: any `xs2`). -/ +theorem recvSend_runMemA {σ : State Unit} {t e sd : ObjRef} {q : List ObjRef} (rep : Option ObjRef) + (hk : σ.kheap e = some (.endpoint (.sendEP (sd :: q)))) {xs : Tcb} (hks : σ.kheap sd = some (.tcb xs)) + (hse : sd ≠ e) {e' : ObjRef} {dd : SenderPayload} (hst : xs.tcbState = .blockedOnSend e' dd) + {σ2 σ3 σ' : State Unit} + (hT : ((), σ2) ∈ (doIpcTransfer (Z := Unit) sd (some e) dd.senderBadge dd.senderCanGrant t + (updK σ e (.endpoint (epDeq (.sendEP (sd :: q)) sd)))).1) + {xs2 : Tcb} (hks2 : σ2.kheap sd = some (.tcb xs2)) + (hA : ifSporadicCurScAssertRefillUnblockCheck (Z := Unit) xs2.tcbSchedContext σ2 = ({((), σ3)}, False)) + {xs3 : Tcb} (hks3 : σ3.kheap sd = some (.tcb xs3)) (hcall : dd.senderIsCall = false) + (hf3 : xs3.tcbFault = none) + (hτ : ((), σ') ∈ (possibleSwitchTo (Z := Unit) sd + (stsAct (updK σ3 sd (.tcb { xs3 with tcbState := .running })) sd)).1) : + ((), σ') ∈ (specRecvSendA t e rep (sd :: q) σ).1 := by + have hks1 : (updK σ e (.endpoint (epDeq (.sendEP (sd :: q)) sd))).kheap sd = some (.tcb xs) := by + rw [updK_kheap, Function.update_of_ne hse, hks] + unfold specRecvSendA + rw [nd_bind_one (assert_true (by simp) σ), nd_pure_bind'] + rw [show the (sd :: q).head? = sd from rfl, nd_bind_one (tcbEpDequeue_eq hk (by simp [Endpoint.epQueue])), + nd_bind_one (getThreadState_eq hks1), hst] + dsimp only + rw [nd_pure_bind'] + refine mem_bindA.2 ⟨((), σ2), hT, ?_⟩ + try dsimp only + rw [nd_bind_one (getTcbObjRef_eq _ hks2), nd_bind_one hA, + nd_bind_one (threadGet_eq _ hks3), if_neg (by rw [hcall, hf3]; simp), nd_bind_one (setThreadState_eq _ hks3)] + exact hτ + +/-- `recvSend_inactA` by membership. -/ +theorem recvSend_inactMemA {σ : State Unit} {t e sd : ObjRef} {q : List ObjRef} (rep : Option ObjRef) + (hk : σ.kheap e = some (.endpoint (.sendEP (sd :: q)))) {xs : Tcb} (hks : σ.kheap sd = some (.tcb xs)) + (hse : sd ≠ e) {e' : ObjRef} {dd : SenderPayload} (hst : xs.tcbState = .blockedOnSend e' dd) + {σ2 σ3 : State Unit} + (hT : ((), σ2) ∈ (doIpcTransfer (Z := Unit) sd (some e) dd.senderBadge dd.senderCanGrant t + (updK σ e (.endpoint (epDeq (.sendEP (sd :: q)) sd)))).1) + {xs2 : Tcb} (hks2 : σ2.kheap sd = some (.tcb xs2)) + (hA : ifSporadicCurScAssertRefillUnblockCheck (Z := Unit) xs2.tcbSchedContext σ2 = ({((), σ3)}, False)) + {xs3 : Tcb} (hks3 : σ3.kheap sd = some (.tcb xs3)) + (hcf : dd.senderIsCall = true ∨ xs3.tcbFault ≠ none) + (hnp : (dd.senderCanGrant = false ∧ dd.senderCanGrantReply = false) ∨ rep = none) : + ((), stsAct (updK σ3 sd (.tcb { xs3 with tcbState := .inactive })) sd) ∈ + (specRecvSendA t e rep (sd :: q) σ).1 := by + have hks1 : (updK σ e (.endpoint (epDeq (.sendEP (sd :: q)) sd))).kheap sd = some (.tcb xs) := by + rw [updK_kheap, Function.update_of_ne hse, hks] + have hn : ¬ ((dd.senderCanGrant = true ∨ dd.senderCanGrantReply = true) ∧ rep ≠ none) := by + rintro ⟨h1, h2⟩ + rcases hnp with ⟨h3, h4⟩ | h3 + · rw [h3, h4] at h1; simp at h1 + · exact h2 h3 + unfold specRecvSendA + rw [nd_bind_one (assert_true (by simp) σ), nd_pure_bind'] + rw [show the (sd :: q).head? = sd from rfl, nd_bind_one (tcbEpDequeue_eq hk (by simp [Endpoint.epQueue])), + nd_bind_one (getThreadState_eq hks1), hst] + dsimp only + rw [nd_pure_bind'] + refine mem_bindA.2 ⟨((), σ2), hT, ?_⟩ + try dsimp only + rw [nd_bind_one (getTcbObjRef_eq _ hks2), nd_bind_one hA, + nd_bind_one (threadGet_eq _ hks3), if_pos hcf, if_neg hn, setThreadState_eq _ hks3] + exact Set.mem_singleton _ + +end Spec + +section Fl4 +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS : Set U64} +variable {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} +variable {Wr : State Unit → S → U64 → Prop} + +/-- **`receive_from_sender` with an extra-cap transfer** (module docstring). -/ +theorem receive_from_senderCapsA (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (hF : SchedFootEA st Z.T E R Bf (IpcSchedRelA st Z m κ) Wr) + {s : S} {a : A} {σ : State Unit} (hPl : PlaceA Z.th (L.ksb s)) + (hR : EpStateRel Z.T E R Bf (L.mem s) σ) (hSR : IpcSchedRelA st Z m κ s σ) + (hSt : StateRelA Z.th (L.mem s) σ) (hV : SlotsValidA Z.th (L.mem s)) (hC : BufCover st Z.T Bf s σ) + (hNative : BufNativeA Z.T (L.mem s)) + (hoff : ∀ y, IsSlotA Z.th y → ¬ EWrite Z.T E R Bf y ∧ ¬ EWrite Z.T E R Bf (core.num.U64.wrapping_add y 8#u64)) + {t : U64} (ht : t ∈ Z.T) (hth : Z.th t = some .tcb) {xt : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hnb : tsCode xt.tcbState ≠ 4) + {e sd : U64} (he : e ∈ E) {q : List ObjRef} + (hk : σ.kheap (specPtr e) = some (.endpoint (.sendEP (specPtr sd :: q)))) + (hsh : Z.th sd = some .tcb) {xs : Tcb} (hks : σ.kheap (specPtr sd) = some (.tcb xs)) (hfs : xs.tcbFault = none) + (hu : ∀ rp, ipcBufOfA true xt = some rp → ∀ k, k < 128 → inUserFrame (bufAddr rp k) σ) + (rv : U64) {rep : Option ObjRef} (hrv : optRef rv = rep) + (hbr : ∀ e' dd, xs.tcbState = .blockedOnSend e' dd → + (dd.senderIsCall = false ∧ L.mem s (affA sd) = L.cpu s) ∨ + (dd.senderIsCall = true ∧ ((dd.senderCanGrant = false ∧ dd.senderCanGrantReply = false) ∨ rep = none))) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hJ : ∀ s1 σ1, EpStateRel Z.T E R Bf (L.mem s1) σ1 → IpcSchedRelA st Z m κ s1 σ1 → + WritesIn st s s1 (EWrite Z.T E R Bf) → EpStep σ σ1 → + InsBaseA (L := L) AR Z H OS s1 a σ1 m ∧ TreeA st OS Z.G.chains Z.G.par s1 ∧ + (∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s1 y = .Null) ∧ + ∀ bp ∈ Bf, ∀ k : U64, k.val < 128 → + ¬ FootA (L := L) Z s1 (bufW bp.1 k) ∧ ¬ InOS OS (bufW bp.1 k)) + (hep : ∀ s1 (x : U64) (c : Cap), CapRelA (capAtA (L.mem s1) x) c → + (UnwCA e (capA st s1 x) ↔ SUnwA (some (specPtr e)) c)) + (hE : ∀ x, W7.CapW H.tcbs {b | InOS OS b} ∅ x → ¬ EFoot Z.T E R Bf x) + (hRd : ∀ s1 σ1, L.ksb s1 = L.ksb s → IpcSchedRelA st Z m κ s1 σ1 → + ∀ x, W7.CapW H.tcbs {b | InOS OS b} ∅ x → ¬ SReadA st Z m s1 σ1 x) : + endpoint.receive_from_sender st ar s a t e rv ⦃ r => r.2 = a ∧ + ∃ σ', ((), σ') ∈ (specRecvSendA (specPtr t) (specPtr e) rep (specPtr sd :: q) σ).1 ∧ + EpStateRel Z.T E R Bf (L.mem r.1) σ' ∧ IpcSchedRelA st Z m κ r.1 σ' ∧ + XcJointFootA st AR Z OS m E R Bf Wr s a σ r.1 ⦄ := by + have hO := schedOpsEAOfPlacementA (st := st) (m := m) (κ := κ) P + obtain ⟨-, xs0, hxs0, hq, hQ, hB⟩ := hR.at_ep he hk + obtain ⟨sd', xs', rfl⟩ := List.exists_cons_of_ne_nil hxs0 + simp only [List.map_cons, List.cons.injEq] at hq + obtain ⟨hd', hq'⟩ := hq + have hdd : sd = sd' := specPtr_inj hd' + subst hdd + have hsT : sd ∈ Z.T := hQ.mem sd (by simp) + have hsa := PE.tcb sd hsT + have hxs := hR.at_tcb hsT hks + have hxt := hR.at_tcb ht hkt + have hs4 : (L.mem s (stA sd)).val = 4 := (hB sd (by simp)).1 + have hsb : L.mem s (tBlockA sd) = e := (hB sd (by simp)).2 + obtain ⟨e', dd, hstd⟩ := tsCode_four (hxs.st.symm.trans hs4) + have he' : e' = specPtr e := by + have := hxs.blk e' (by rw [hstd]; rfl); rw [hsb] at this; exact this.symm + subst he' + obtain ⟨hbadge, hflags⟩ := hxs.send _ _ hstd + have hts : t ≠ sd := by intro h; subst h; exact hnb (hxt.st.symm.trans hs4) + have hse : specPtr sd ≠ specPtr e := kheap_ne hks hk (fun _ h1 h2 => by subst h1; cases h2) + have hte : specPtr t ≠ specPtr e := kheap_ne hkt hk (fun _ h1 h2 => by subst h1; cases h2) + have hts' : specPtr t ≠ specPtr sd := fun h => hts (specPtr_inj h) + -- FL4: the queue's head and the dequeue + unfold endpoint.receive_from_sender + step with endpoint.get_queue.spec as ⟨q0, hq0⟩ + have hhead : q0.head = sd := by rw [hq0, epQ_eq]; exact hQ.head + rw [hhead] + have hd : specPtr sd ∈ (Endpoint.sendEP (specPtr sd :: q)).epQueue := by simp [Endpoint.epQueue] + refine WP.spec_bind (ep_dequeue_raw PE s σ hR he hk hd) ?_ + rintro s1 ⟨-, hR1, hW1, hE1, hA1⟩ + have hS1 := hO.stable _ _ _ _ hSR hW1 hE1 (NoBlockE.updO _ _ fun _ h => by cases h) + have hkt1 : (updK σ (specPtr e) (.endpoint (epDeq (.sendEP (specPtr sd :: q)) (specPtr sd)))).kheap (specPtr t) = + some (.tcb xt) := by rw [updK_kheap, Function.update_of_ne hte, hkt] + have hks1 : (updK σ (specPtr e) (.endpoint (epDeq (.sendEP (specPtr sd :: q)) (specPtr sd)))).kheap (specPtr sd) = + some (.tcb xs) := by rw [updK_kheap, Function.update_of_ne hse, hks] + -- the sender's payload words + have hbw : L.mem s1 (tBadgeA sd) = L.mem s (tBadgeA sd) := hA1 _ (a_badge hsa).1 ⟨sd, hsT, (a_badge hsa).2⟩ + have hfw : L.mem s1 (tFlagsA sd) = L.mem s (tFlagsA sd) := hA1 _ (a_flags hsa).1 ⟨sd, hsT, (a_flags hsa).2⟩ + step with obj.Tcb.block_badge.spec as ⟨bdg, hbdg⟩ + step with obj.Tcb.block_flags.spec as ⟨fl, hfl⟩ + simp only [lift, bind_ok] + have hg : (fl &&& endpoint.FLAG_GRANT != 0#u64) = dd.senderCanGrant := by + rw [hfl, hfw, hflags]; exact flags_grantA dd + have hgr : (fl &&& endpoint.FLAG_GRANT_REPLY != 0#u64) = dd.senderCanGrantReply := by + rw [hfl, hfw, hflags]; exact flags_grantReplyA dd + have hc : (fl &&& endpoint.FLAG_CALL != 0#u64) = dd.senderIsCall := by + rw [hfl, hfw, hflags]; exact flags_callA dd + have hbb : bdg.bv = dd.senderBadge := by rw [hbdg, hbw]; exact hbadge + rw [hg, hgr, hc] + have hmem : ∀ y, IsSlotA Z.th y → L.mem s1 y = L.mem s y ∧ + L.mem s1 (core.num.U64.wrapping_add y 8#u64) = L.mem s (core.num.U64.wrapping_add y 8#u64) := by + intro y hy + have hal : Aligned y := by have := (IsSlotA.al32 hPl hy).1; unfold Aligned; omega + exact ⟨hW1.2.2 y hal (hoff y hy).1, hW1.2.2 _ (wa_al hal (by simp)) (hoff y hy).2⟩ + have hSt1 := hSt.updEpA hPl hmem hk (epDeq (.sendEP (specPtr sd :: q)) (specPtr sd)) + have hV1 : SlotsValidA Z.th (L.mem s1) := slotsValid_xferA hV hmem + have hC1 : BufCover st Z.T Bf s1 + (updK σ (specPtr e) (.endpoint (epDeq (.sendEP (specPtr sd :: q)) (specPtr sd)))) := + BufCover.ewA PE hC hW1 fun u _ x hx => by + have hne : specPtr u ≠ specPtr e := fun h => by + rw [h, updK_kheap, Function.update_self] at hx; cases hx + rwa [updK_kheap, Function.update_of_ne hne] at hx + have hu1 : ∀ rp, ipcBufOfA true xt = some rp → ∀ k, k < 128 → + inUserFrame (bufAddr rp k) (updK σ (specPtr e) (.endpoint (epDeq (.sendEP (specPtr sd :: q)) (specPtr sd)))) := + fun rp hrp k hk' => inUserFrame_updK hk (fun _ h => by cases h) _ (hu rp hrp k hk') + have hPl1 : PlaceA Z.th (L.ksb s1) := by rw [hW1.1]; exact hPl + -- the transfer with extra caps + refine WP.spec_bind (do_ipc_transfer_capsA (AR := AR) (H := H) (OS := OS) P PE hPl1 hR1 hS1 hSt1 hV1 hC1 + (hNative.ewA PE hW1) hsT ht (Ne.symm hts) hsh hth hks1 hkt1 hfs hu1 hoff e bdg dd.senderCanGrant hsub + (fun s2 σ2 hR2 hS2 hW2 hE2 => hJ s2 σ2 hR2 hS2 (WritesIn.ew hW1 hW2) (hE1.trans hE2)) hep hE + (fun s2 σ2 h hS => hRd s2 σ2 (h.trans hW1.1) hS)) ?_ + rintro s2 ⟨σ2, hT, hR2, hS2, ⟨sa, σa, sb, σb, G', hWa, hEa, J, Tr, SI, hWb, hEb⟩, hfr, -, x2, hkt2, -⟩ + rw [hbb] at hT + obtain ⟨xs2, hks2, hxs2⟩ := XcKhFrA.tcbA hfr (Ne.symm hts') hks1 + have hk1e : (updK σ (specPtr e) (.endpoint (epDeq (.sendEP (specPtr sd :: q)) (specPtr sd)))).kheap (specPtr e) = + some (.endpoint (epDeq (.sendEP (specPtr sd :: q)) (specPtr sd))) := by rw [updK_kheap, Function.update_self] + have hsb1 : L.mem s1 (tBlockA sd) = e := by rw [hA1 _ (a_blk hsa).1 ⟨sd, hsT, (a_blk hsa).2⟩, hsb] + have hq1 := dequeued_not_queued hR1 he hsb1 hk1e + (epDeq_not_mem (z := .sendEP (specPtr sd :: q)) (xs := sd :: xs') (by rw [hq']; rfl) hQ.nodup sd) + have hq2 : ∀ e'' ∈ E, ∀ z, σ2.kheap (specPtr e'') = some (.endpoint z) → specPtr sd ∉ z.epQueue := by + intro e'' he'' z hz + have hne : specPtr e'' ≠ specPtr t := by intro h; rw [h, hkt2] at hz; cases hz + exact hq1 e'' he'' z (hfr.backA hne hz (fun _ _ h => by cases h) (fun _ h => by cases h)) + have hpre : ∀ s', FpA st Z.T E R Bf Wr σ2 s2 s' → + XcJointFootA st AR Z OS m E R Bf Wr s a σ s' := fun s' hfp => + ⟨sa, σa, sb, σb, G', WritesIn.ew hW1 hWa, hE1.trans hEa, J, Tr, SI, + FpA.trans hF (FpA.of_ew hWb) (ssz_of_epStep hEb) hfp⟩ + have hst2 : xs2.tcbState = .blockedOnSend (specPtr e) dd := by + have := congrArg Tcb.tcbState hxs2; exact this.trans hstd + have hfs2 : xs2.tcbFault = none := by + have := congrArg Tcb.tcbFault hxs2; exact this.trans hfs + have hsc2 : xs2.tcbSchedContext = xs.tcbSchedContext := by + have := congrArg Tcb.tcbSchedContext hxs2; exact this + -- the tails of `receive_from_senderA`, from the transfer's post-state + have hnr : runnable xs2.tcbState = false := by rw [hst2]; rfl + have hnq2 := hO.notQueued _ _ sd xs2 hS2 hks2 hnr + refine WP.spec_bind (WP.spec_and (hO.unblockSporadic s2 σ2 sd xs2 hR2 hS2 hsT hks2 hnr hnq2) + (hF.unblockSporadic s2 σ2 sd xs2 hR2 hS2 hsT hks2 hnr hnq2)) ?_ + rintro s3 ⟨⟨σ3, hA, hf3, hv3, hS3⟩, hw3⟩ + have hz3 : SSzA σ2 σ3 := hF.unblockSz _ _ _ hA + have hpre3 : FpA st Z.T E R Bf Wr σ2 s2 s3 := FpA.of_wr hw3 + have hR3 := hR2.frame PE hf3 hv3 + obtain ⟨xs3, hks3, hs3st, -, -, hs3f, -⟩ := hv3.2.2.1 _ _ hks2 + have hq3 : ∀ e'' ∈ E, ∀ z, σ3.kheap (specPtr e'') = some (.endpoint z) → specPtr sd ∉ z.epQueue := by + intro e'' he'' z hz + obtain ⟨z2, hz2, -⟩ := hR2.ep e'' he'' + rw [hv3.1 _ _ hz2] at hz + injection hz with hz; injection hz with hz; subst hz + exact hq2 e'' he'' z2 hz2 + have hxs3 := hR3.at_tcb hsT hks3 + have hst3 : xs3.tcbState = .blockedOnSend (specPtr e) dd := hs3st.trans hst2 + have hf3' : xs3.tcbFault = none := hs3f.trans hfs2 + step with obj.Tcb.fault_type.spec as ⟨ft, hft⟩ + have hft0 : ft = fl4_abi.consts.fault.NullFault := by + rw [hft, hxs3.fault0.2 hf3']; unfold fl4_abi.consts.fault.NullFault; rfl + rcases hbr _ _ hstd with ⟨hcall, hon⟩ | ⟨hcall, hnp⟩ + · -- a non-call sender runs + simp only [hcall, hft0, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + refine WP.spec_mono (recv_run_tailA hO hF PE a hSR hsT hon hR3 hS3 hks3 hst3 hf3' hq3 hpre3 hz3) ?_ + rintro r ⟨ha, -, σ', hm, hR', hS', hfp'⟩ + exact ⟨ha, σ', recvSend_runMemA rep hk hks hse hstd hT hks2 hA hks3 hcall hf3' hm, hR', hS', hpre _ hfp'⟩ + · -- a call without a reply push: the sender becomes inactive + have hI := set_thread_state_relF hO hF PE s3 σ3 hR3 hS3 hsT hks3 hq3 .inactive (by decide) sched.TS_INACTIVE + (by unfold sched.TS_INACTIVE; rfl) + (NoBlockE.updT hks3 _ (Or.inr (Or.inr ⟨rfl, (by rw [hst3]; rfl), (by rw [hst3]; exact fun h => by cases h)⟩))) + have hmem' := recvSend_inactMemA rep hk hks hse hstd hT hks2 hA hks3 (Or.inl hcall) hnp + have htail : (do + let s4 ← sched.set_thread_state st s3 sd sched.TS_INACTIVE + ok (s4, a)) ⦃ r => r.2 = a ∧ + ∃ σ', ((), σ') ∈ (specRecvSendA (specPtr t) (specPtr e) rep (specPtr sd :: q) σ).1 ∧ + EpStateRel Z.T E R Bf (L.mem r.1) σ' ∧ IpcSchedRelA st Z m κ r.1 σ' ∧ + XcJointFootA st AR Z OS m E R Bf Wr s a σ r.1 ⦄ := + WP.spec_bind hI fun s4 h4 => by + rw [WP.spec_ok] + obtain ⟨h4a, h4b, h4c⟩ := h4 + exact ⟨rfl, _, hmem', h4a, h4b, hpre _ (FpA.trans hF hpre3 hz3 h4c)⟩ + have hrv0 : rep = none → rv = 0#u64 := fun h => optRef_none (hrv.trans h) + simp only [hcall, ↓reduceIte] + rcases hnp with ⟨hg0, hgr0⟩ | hrn + · simp only [hg0, hgr0, Bool.false_eq_true, ↓reduceIte] + exact htail + · simp only [hrv0 hrn, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte, ite_self] + exact htail + +end Fl4 + +end + +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/IpcCapsReplyA.lean b/proofs/Fl4ProofsA64/P3/IpcCapsReplyA.lean new file mode 100644 index 000000000..c9cd161e9 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/IpcCapsReplyA.lean @@ -0,0 +1,331 @@ +/- +P3 (aarch64, Stage-3 `hcaps` drop, ruling (b), contract §13.4): **the live Reply joint without `hcaps`**. +`ReplyLiveJointA.do_reply_transfer_jointA` takes `hcaps` (no extra-cap insert); `do_ipc_transfer_capsA` lives +downstream of it (`IpcXferCapsDoA → IpcXferOrthA → ReplyLiveJointA`), so the general joint is this new module. +No existing statement changed. + +* **`XcReplyFootA`**: the footprint of a Reply joint with a cap transfer (joint words `ReplyW` up to the cap loop, the + joint relation `JAtZAX` at a new CDT ghost right after `transfer_caps`, then `ReplyW` from there; SC sizes kept on + both segments), with `.pre` / `.ext` (prefix / suffix segments). +* **`replyNormalCapsA`**: recipe steps 1–2 (`replyNormalJointA`) with `do_ipc_transfer_capsA`. +* **`do_reply_transfer_fullA`**: `do_reply_transfer_jointA` without `hcaps`: the normal arm splits on the extra caps + (`replyNormalJointA` or `replyNormalCapsA`); the post's footprint is `ReplyW` from the entry state or + `XcReplyFootA`. + +Case hypotheses (no named premise): those of `do_reply_transfer_jointA` without `hcaps`, and those of +`do_ipc_transfer_capsA` (endpoint `0`, no endpoint object) stated at the entry state: `hJ` over every +joint-footprint successor with the SC sizes kept, `hRd` over states with the entry's `ksb`. +-/ +import Fl4ProofsA64.P3.IpcCapsSendA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3 Fl4ProofsA64.P3.Notif Fl4SpecA64 Sched + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section Store +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {Hh : Heap} {OS : Set U64} +variable {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + +variable (st AR Z OS N E R Bf m) in +/-- **The footprint of a Reply joint with a cap transfer** from `(s, σ)` to `(s', σ')` (module docstring). -/ +def XcReplyFootA (s : S) (a : A) (σ : State Unit) (s' : S) (σ' : State Unit) : Prop := + ∃ sa σa sb σb G', WritesIn st s sa (ReplyW (st := st) N E R Bf Z m σ s) ∧ SSzA σ σa ∧ + JAtZAX (L := L) AR { Z with G := G' } sb a σb m ∧ TreeA st OS G'.chains G'.par sb ∧ + (∀ n, SInvXA σa m n → SInvXA σb m n) ∧ WritesIn st sb s' (ReplyW (st := st) N E R Bf Z m σb sb) ∧ + SSzA σb σ' + +/-- A joint segment before. -/ +theorem XcReplyFootA.pre {s0 s s' : S} {a : A} {σ0 σ σ' : State Unit} + (h : XcReplyFootA st AR Z OS m N E R Bf s a σ s' σ') + (w : WritesIn st s0 s (ReplyW (st := st) N E R Bf Z m σ0 s0)) (z : SSzA σ0 σ) : + XcReplyFootA st AR Z OS m N E R Bf s0 a σ0 s' σ' := by + obtain ⟨sa, σa, sb, σb, G', w1, z1, J, T, SI, w2, z2⟩ := h + exact ⟨sa, σa, sb, σb, G', ReplyW.trans w z w1, z.trans z1, J, T, SI, w2, z2⟩ + +/-- A joint segment after. -/ +theorem XcReplyFootA.ext {s s1 s2 : S} {a : A} {σ σ1 σ2 : State Unit} + (h : XcReplyFootA st AR Z OS m N E R Bf s a σ s1 σ1) + (w : WritesIn st s1 s2 (ReplyW (st := st) N E R Bf Z m σ1 s1)) (z : SSzA σ1 σ2) : + XcReplyFootA st AR Z OS m N E R Bf s a σ s2 σ2 := by + obtain ⟨sa, σa, sb, σb, G', w1, z1, J, T, SI, w2, z2⟩ := h + exact ⟨sa, σa, sb, σb, G', w1, z1, J, T, SI, ReplyW.trans w2 z2 w, z2.trans z⟩ + +/-- **Recipe steps 1–2 with an extra-cap transfer** (module docstring). -/ +theorem replyNormalCapsA {s2 : S} {a : A} {σ2 : State Unit} {sender t : U64} {grant : Bool} + (hS2 : IpcSchedRelA st Z m κ s2 σ2) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (E2 : EpStateRel Z.T E R Bf (L.mem s2) σ2) + (hSt : StateRelA Z.th (L.mem s2) σ2) (hV : SlotsValidA Z.th (L.mem s2)) + (hC : BufCover st Z.T Bf s2 σ2) (hNative : BufNativeA Z.T (L.mem s2)) + (hoff : ∀ y, IsSlotA Z.th y → ¬ EWrite Z.T E R Bf y ∧ ¬ EWrite Z.T E R Bf (wa y 8#u64)) + (hs : sender ∈ Z.T) (ht : t ∈ Z.T) (hne : sender ≠ t) + {xs y2 : Tcb} (hks : σ2.kheap (specPtr sender) = some (.tcb xs)) + (hky : σ2.kheap (specPtr t) = some (.tcb y2)) (hy2 : y2.tcbState = .inactive) + (hfs : xs.tcbFault = none) + (hu : ∀ rp, ipcBufOfA true y2 = some rp → ∀ k, k < 128 → inUserFrame (bufAddr rp k) σ2) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hJ : ∀ s1 σ1, EpStateRel Z.T E R Bf (L.mem s1) σ1 → IpcSchedRelA st Z m κ s1 σ1 → + WritesIn st s2 s1 (EWrite Z.T E R Bf) → EpStep σ2 σ1 → + InsBaseA (L := L) AR Z Hh OS s1 a σ1 m ∧ TreeA st OS Z.G.chains Z.G.par s1 ∧ + (∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s1 y = .Null) ∧ + ∀ bp ∈ Bf, ∀ k : U64, k.val < 128 → + ¬ FootA (L := L) Z s1 (bufW bp.1 k) ∧ ¬ InOS OS (bufW bp.1 k)) + (hep : ∀ s1 (x : U64) (c : Cap), CapRelA (capAtA (L.mem s1) x) c → + (UnwCA 0#u64 (capA st s1 x) ↔ SUnwA none c)) + (hE : ∀ x, W7.CapW Hh.tcbs {b | InOS OS b} ∅ x → ¬ EFoot Z.T E R Bf x) + (hRd : ∀ s1 σ1, L.ksb s1 = L.ksb s2 → IpcSchedRelA st Z m κ s1 σ1 → + ∀ x, W7.CapW Hh.tcbs {b | InOS OS b} ∅ x → ¬ SReadA st Z m s1 σ1 x) : + replyBodyProgramA st ar s2 a sender t fl4_abi.consts.fault.NullFault grant ⦃ r => ∃ σ4, + ((), σ4) ∈ ((fl4DoIpcTransfer (Z := Unit) (specPtr sender) none 0 grant (specPtr t) >>= fun _ => + setThreadState (specPtr t) .running) σ2).1 ∧ r.2 = a ∧ + ∃ Q R' x, SchedSRB st { Z with Q := Q, R := R' } m κ r.1 σ4 (specPtr t) ∧ + StEqA Z.T (L.mem r.1) σ4 ∧ σ4.kheap (specPtr t) = some (.tcb x) ∧ + x.tcbState = .running ∧ x.tcbFault = y2.tcbFault ∧ + (∀ d p, specPtr t ∉ σ4.readyQueues d p) ∧ specPtr t ∉ σ4.releaseQueue ∧ + XcReplyFootA st AR Z OS m N E R Bf s2 a σ2 r.1 σ4 ⦄ := by + obtain ⟨Q1, R1, H2⟩ := hS2 + refine replyNormalBody_refinesA (st := st) (ar := ar) (s := s2) (a := a) (σ := σ2) (sender := sender) + (t := t) (grant := grant) (M := fun s1 σ1 => EpStateRel Z.T E R Bf (L.mem s1) σ1 ∧ + IpcSchedRelA st Z m κ s1 σ1 ∧ + (∃ sa σa sb σb G', WritesIn st s2 sa (EWrite Z.T E R Bf) ∧ EpStep σ2 σa ∧ + JAtZAX (L := L) AR { Z with G := G' } sb a σb m ∧ TreeA st OS G'.chains G'.par sb ∧ + (∀ n, SInvXA σa m n → SInvXA σb m n) ∧ WritesIn st sb s1 (EWrite Z.T E R Bf) ∧ EpStep σb σ1) ∧ + ∃ x', σ1.kheap (specPtr t) = some (.tcb x') ∧ tcbNcA x' = tcbNcA { y2 with tcbArch := x'.tcbArch }) + (R := fun s4 a4 σ4 => a4 = a ∧ + ∃ Q R' x, SchedSRB st { Z with Q := Q, R := R' } m κ s4 σ4 (specPtr t) ∧ + StEqA Z.T (L.mem s4) σ4 ∧ σ4.kheap (specPtr t) = some (.tcb x) ∧ + x.tcbState = .running ∧ x.tcbFault = y2.tcbFault ∧ + (∀ d p, specPtr t ∉ σ4.readyQueues d p) ∧ specPtr t ∉ σ4.releaseQueue ∧ + XcReplyFootA st AR Z OS m N E R Bf s2 a σ2 s4 σ4) hks hky hfs ?_ ?_ + · refine WP.spec_mono (do_ipc_transfer_capsA (ar := ar) (AR := AR) (H := Hh) (OS := OS) (a := a) P PE H2.place + E2 ⟨Q1, R1, H2⟩ hSt hV hC hNative hs ht hne ((H2.thT sender).2 hs) ((H2.thT t).2 ht) hks hky hfs hu hoff + 0#u64 0#u64 grant hsub hJ hep hE hRd) ?_ + rintro s1 ⟨σ1, hm, hR1, hS1, hX, -, -, x', hk', hx'⟩ + exact ⟨σ1, hm, hR1, hS1, hX, x', hk', hx'⟩ + · rintro s3 σ3 ⟨E3, ⟨Q3, R3, H3⟩, ⟨sa, σa, sb, σb, G', hWa, hEa, J, Tr, SI, hWb, hEb⟩, x3, hx3, ex3⟩ + have hst3 : StEqA Z.T (L.mem s3) σ3 := fun u hu x hx => (E3.at_tcb hu hx).st + have hx3s : x3.tcbState = .inactive := by + have h := congrArg Tcb.tcbState ex3; exact h.trans hy2 + have hnq := H3.notQueued hx3 (by rw [hx3s]; rfl) + refine WP.spec_mono (set_thread_state_exAnyA H3 hst3 ht hx3 (ts := .running) rfl + (v := sched.TS_RUNNING) (by unfold sched.TS_RUNNING; rfl)) ?_ + rintro s4 ⟨σ4, he4, hk4, HB, hst4, ws⟩ + have e4 : σ4 = stsActA (updKSc σ3 (specPtr t) (.tcb { x3 with tcbState := .running })) (specPtr t) := + det_outA he4 (setThreadState_stsActExA hx3 _) + have hq4 : σ4.readyQueues = σ3.readyQueues ∧ σ4.releaseQueue = σ3.releaseQueue := by + rw [e4] + unfold stsActA + split <;> exact ⟨rfl, rfl⟩ + have hz34 : SSzA σ3 σ4 := by + intro p y n h + have hkh : σ4.kheap = Function.update σ3.kheap (specPtr t) (some (.tcb { x3 with tcbState := .running })) := by + rw [e4] + unfold stsActA + split <;> rfl + rw [hkh] at h + by_cases e : p = specPtr t + · subst e; rw [Function.update_self] at h; cases h + · rw [Function.update_of_ne e] at h; exact ⟨y, h⟩ + have hza : SSzA σ2 σa := ssz_of_epStep hEa + have hzb : SSzA σb σ3 := ssz_of_epStep hEb + have wa' : WritesIn st s2 sa (ReplyW (st := st) N E R Bf Z m σ2 s2) := + hWa.mono fun _ hb => ewrite_stepNA_buf PE.tcb hb + have wb' : WritesIn st sb s3 (ReplyW (st := st) N E R Bf Z m σb sb) := + hWb.mono fun _ hb => ewrite_stepNA_buf PE.tcb hb + have ws' : WritesIn st s3 s4 (ReplyW (st := st) N E R Bf Z m σ3 s3) := + ws.mono fun b hb => ReplyW.zc (Z := Z) (Z' := { Z with Q := Q3, R := R3 }) rfl rfl + (replyNormalW_wA (Z := { Z with Q := Q3, R := R3 }) PE H3.toSchedSRN ht (Or.inr hb)) + refine ⟨σ4, by rw [he4]; exact Set.mem_singleton _, rfl, Q3, R3, { x3 with tcbState := .running }, + HB, hst4, by rw [hk4, updKSc_kheap, Function.update_self], rfl, + (by have h := congrArg Tcb.tcbFault ex3; exact h), fun d p => by rw [hq4.1]; exact hnq.1 d p, + by rw [hq4.2]; exact hnq.2, + ⟨sa, σa, sb, σb, G', wa', hza, J, Tr, SI, ReplyW.trans wb' hzb ws', hzb.trans hz34⟩⟩ + +/-- **The live Reply joint without `hcaps`** (module docstring; hypotheses as `do_reply_transfer_jointA`, plus the +transfer's cap-insert case hypotheses at the entry state). -/ +theorem do_reply_transfer_fullA {s : S} {a : A} {σ : State Unit} {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {sender r t : U64} {grant : Bool} (hr : r ∈ R) (hs : sender ∈ Z.T) (ht : t ∈ Z.T) + (hne : sender ≠ t) + {xt xs : Tcb} {xr : Reply} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hkr : σ.kheap (specPtr r) = some (.reply xr)) (hb : xr.replyTcb = some (specPtr t)) + (hst : xt.tcbState = .blockedOnReply (specPtr r)) + (hks : σ.kheap (specPtr sender) = some (.tcb xs)) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) + (cov : ScReplyCovA Z.C m σ (specPtr r)) + (hsc0 : ∀ sc ∈ Z.C, (stack sc).head? = some r → sc ≠ 0#u64) + (hlocal : ∀ sc ∈ Z.C, (stack sc).head? = some r → xt.tcbSchedContext = none → + ReplyPopLocalA (st := st) (Z := Z) (m := m) s σ sc t) + (unb : ∀ s1 σ1 Q1 R1, SchedSRA st { Z with Q := Q1, R := R1 } m κ s1 σ1 → RemKeepA σ σ1 → + (L.mem s1 (scA t) ≠ 0#u64 → L.mem s1 (scCoreA (L.mem s1 (scA t))) = L.cpu s1) ∧ + (specPtr t ∈ σ1.releaseQueue → ∀ y n, + σ1.kheap (scNameA m (L.mem s1 (scA t))) = some (.schedContext y n) → + refillReady σ1.curTime (refillHd y) = false)) + (hoff : ∀ y, IsSlotA Z.th y → ¬ EWrite Z.T E R Bf y ∧ ¬ EWrite Z.T E R Bf (wa y 8#u64)) + (hSt : StateRelA Z.th (L.mem s) σ) (hV : SlotsValidA Z.th (L.mem s)) + (hC : BufCover st Z.T Bf s σ) (hNative : BufNativeA Z.T (L.mem s)) + (hfs : xs.tcbFault = none) + (hu : ∀ rp, ipcBufOfA true xt = some rp → ∀ k, k < 128 → inUserFrame (bufAddr rp k) σ) + (hmb : ∀ a', Fl4MinBudgetAgreeA ar a') + (hidle : ∀ c < fl4A64Platform.numNodes, specPtr t ≠ m.idleThreadOf c) + (tailS : ∀ (s4 : S) (σ4 : State Unit) Q4 R4 (x : Tcb), + SchedSRB st { Z with Q := Q4, R := R4 } m κ s4 σ4 (specPtr t) → + σ4.kheap (specPtr t) = some (.tcb x) → + L.mem s4 (affA t) = L.cpu s4 ∧ + (L.mem s4 (scA t) ≠ 0#u64 → L.mem s4 (scCoreA (L.mem s4 (scA t))) = L.cpu s4) ∧ + (∀ sc y n, L.mem s4 (scA t) = sc → sc ≠ 0#u64 → + σ4.kheap (scNameA m sc) = some (.schedContext y n) → scActive y = true) ∧ + faults.valid_timeout_handler st s4 t = ok (isEpCap x.tcbTimeoutHandler)) + {RT : S → A → State Unit → Prop} + (timeout : ∀ (s4 : S) (a4 : A) (σ4 : State Unit) Q4 R4 (x : Tcb) (sc : U64) (y : SchedContext) + (n : Nat) (badge : U64), + SchedSRB st { Z with Q := Q4, R := R4 } m κ s4 σ4 (specPtr t) → + σ4.kheap (specPtr t) = some (.tcb x) → L.mem s4 (scA t) = sc → sc ≠ 0#u64 → + σ4.kheap (scNameA m sc) = some (.schedContext y n) → badge.bv = y.scBadge → + isEpCap x.tcbTimeoutHandler = true → + faults.handle_timeout st ar s4 a4 t (.Timeout badge) ⦃ r => + ∃ σ', ((), σ') ∈ (fl4HandleTimeout (Z := Unit) (specPtr t) (.timeout badge.bv) σ4).1 ∧ + RT r.1 r.2 σ' ∧ WritesIn st s4 r.1 (ReplyW (st := st) N E R Bf Z m σ4 s4) ∧ SSzA σ4 σ' ⦄) + (faultArm : ∀ (s2 : S) (a1 : A) (σ2 : State Unit) (y2 : Tcb) (ft : U64) (f : Fault), + ReplyMidA st Z m κ E R Bf s σ r t s2 σ2 → σ2.kheap (specPtr t) = some (.tcb y2) → + ft = L.mem s2 (tFaultA t) → ft ≠ fl4_abi.consts.fault.NullFault → y2.tcbFault = some f → + replyBodyProgramA st ar s2 a1 sender t ft grant ⦃ out => ∃ σ3, + ((), σ3) ∈ (replyFaultBodyA (specPtr sender) (specPtr t) f σ2).1 ∧ + ReplyTailReadyA st Z m κ t out.1 σ3 ft (some f) ∧ + WritesIn st s2 out.1 (ReplyW (st := st) N E R Bf Z m σ2 s2) ∧ SSzA σ2 σ3 ⦄) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hJ : ∀ s1 σ1, EpStateRel Z.T E R Bf (L.mem s1) σ1 → IpcSchedRelA st Z m κ s1 σ1 → + WritesIn st s s1 (ReplyW (st := st) N E R Bf Z m σ s) → SSzA σ σ1 → + InsBaseA (L := L) AR Z Hh OS s1 a σ1 m ∧ TreeA st OS Z.G.chains Z.G.par s1 ∧ + (∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s1 y = .Null) ∧ + ∀ bp ∈ Bf, ∀ k : U64, k.val < 128 → + ¬ FootA (L := L) Z s1 (bufW bp.1 k) ∧ ¬ InOS OS (bufW bp.1 k)) + (hep : ∀ s1 (x : U64) (c : Cap), CapRelA (capAtA (L.mem s1) x) c → + (UnwCA 0#u64 (capA st s1 x) ↔ SUnwA none c)) + (hE : ∀ x, W7.CapW Hh.tcbs {b | InOS OS b} ∅ x → ¬ EFoot Z.T E R Bf x) + (hRd : ∀ s1 σ1, L.ksb s1 = L.ksb s → IpcSchedRelA st Z m κ s1 σ1 → + ∀ x, W7.CapW Hh.tcbs {b | InOS OS b} ∅ x → ¬ SReadA st Z m s1 σ1 x) : + ipc.do_reply_transfer st ar s a sender r grant ⦃ out => ∃ σ4, + ((), σ4) ∈ (fl4DoReplyTransfer (Z := Unit) (specPtr sender) (specPtr r) grant σ).1 ∧ + (IpcSchedRelA st Z m κ out.1 σ4 ∨ RT out.1 out.2 σ4) ∧ + ((WritesIn st s out.1 (ReplyW (st := st) N E R Bf Z m σ s) ∧ SSzA σ σ4) ∨ + XcReplyFootA st AR Z OS m N E R Bf s a σ out.1 σ4) ⦄ := by + have hrt : xr.replyTcb = optRef (L.mem s (rTcbA r)) := by + obtain ⟨x, hx, h⟩ := HE.reply r hr + rw [hkr] at hx + cases hx + exact h + obtain ⟨hw0, hwt⟩ := optRef_some (hrt.symm.trans hb) + have hw : L.mem s (rTcbA r) = t := specPtr_inj hwt + have htr : obj.Reply.tcb st r s = ok t := + (eq_of_spec_fd (obj.Reply.tcb.spec (st := st) s r)).trans (by rw [hw]) + have hn : t ≠ 0#u64 := hw ▸ hw0 + have hrel : TcbRelE R (L.mem s) t xt := by + obtain ⟨x, hx, h⟩ := HE.tcb t ht + rw [hkt] at hx + cases hx + exact h + have hsw : L.mem s (stA t) = sched.TS_BLOCKED_ON_REPLY := by + apply UScalar.eq_of_val_eq + rw [hrel.st, hst] + unfold sched.TS_BLOCKED_ON_REPLY + rfl + have hsr : obj.Tcb.state st t s = ok sched.TS_BLOCKED_ON_REPLY := + (eq_of_spec_fd (P1.obj.Tcb.state.spec (st := st) s t)).trans (by rw [hsw]) + refine do_reply_transfer_liveA + (M1 := fun s1 a1 σ1 => a1 = a ∧ replyUnblockProgramA st s1 t ⦃ s2 => ∃ σ2, + (getTcbObjRef (Z := Unit) Tcb.tcbSchedContext (specPtr t) >>= + ifSporadicCurScTestRefillUnblockCheck) σ1 = ({((), σ2)}, False) ∧ + ReplyMidA st Z m κ E R Bf s σ r t s2 σ2 ⦄) + (M2 := fun s2 a1 σ2 => a1 = a ∧ ReplyMidA st Z m κ E R Bf s σ r t s2 σ2) + (M3 := fun s3 _ σ3 ft fault => ReplyTailReadyA st Z m κ t s3 σ3 ft fault ∧ + ((WritesIn st s s3 (ReplyW (st := st) N E R Bf Z m σ s) ∧ SSzA σ σ3) ∨ + XcReplyFootA st AR Z OS m N E R Bf s a σ s3 σ3)) + (R := fun s' a' σ' => (IpcSchedRelA st Z m κ s' σ' ∨ RT s' a' σ') ∧ + ((WritesIn st s s' (ReplyW (st := st) N E R Bf Z m σ s) ∧ SSzA σ σ') ∨ + XcReplyFootA st AR Z OS m N E R Bf s a σ s' σ')) + hkr hb hkt hst htr hn hsr ?_ ?_ ?_ ?_ + · refine WP.spec_mono (replyRemoveThenUnblockA a H P PE HE HS hr ht hkt hkr hb hst hq cov hsc0 hlocal + unb hoff hSt hV hC hNative) ?_ + rintro out ⟨σ1, h1, ha, hU⟩ + exact ⟨σ1, h1, ha, hU⟩ + · rintro s1 a1 σ1 ⟨ha, hU⟩ + refine WP.spec_mono hU ?_ + rintro s2 ⟨σ2, h2, hM⟩ + exact ⟨σ2, h2, ha, hM⟩ + · rintro s2 a1 σ2 ft ⟨ha1, hM⟩ hft + obtain ⟨Q1, R1, stack', H2, E2, S2, iso2, keep, hSt2, hV2, hC2, hN2, ⟨y2, hy2, hy2s⟩, wm, szm⟩ := id hM + have wm' : WritesIn st s s2 (ReplyW (st := st) N E R Bf Z m σ s) := + wm.mono fun b hb => replyFootW_wA PE hb + have hrel2 : TcbRelE R (L.mem s2) t y2 := by + obtain ⟨x, hx, h⟩ := E2.tcb t ht + rw [hy2] at hx + cases hx + exact h + by_cases h0 : ft = fl4_abi.consts.fault.NullFault + · have hf0 : y2.tcbFault = none := hrel2.fault0.1 (by rw [← hft, h0]; unfold fl4_abi.consts.fault.NullFault; rfl) + obtain ⟨ys, st1, sc1, hys, eys⟩ := keep.2.2 _ xs hks + obtain ⟨yt, st2, sc2, hyt, eyt⟩ := keep.2.2 _ xt hkt + have e2 : yt = y2 := by + rw [hy2] at hyt + exact (KernelObject.tcb.inj (Option.some.inj hyt)).symm + subst e2 + subst eys + have hu2 : ∀ rp, ipcBufOfA true yt = some rp → ∀ k, k < 128 → inUserFrame (bufAddr rp k) σ2 := by + intro rp hrp k hk + rw [eyt] at hrp + have hin := hu rp hrp k hk + unfold inUserFrame at hin ⊢ + obtain ⟨sz, hsz⟩ := hin + exact ⟨sz, keep.2.1 _ _ hsz (fun _ _ e => by cases e) (fun _ e => by cases e) (fun _ e => by cases e)⟩ + subst h0 + have hto : (fl4_abi.consts.fault.NullFault != fl4_abi.consts.fault.Timeout) = + !(none : Option Fault).elim false isTimeoutFault := by + unfold fl4_abi.consts.fault.NullFault fl4_abi.consts.fault.Timeout + decide + by_cases hc : grant = false ∨ ipcBufOfA false xs = none ∨ + unat (dataToMessageInfo (xs.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0 + · refine WP.spec_mono (replyNormalJointA (Z := { Z with Q := Q1, R := R1 }) H2 P PE E2 hSt2 hV2 hC2 hN2 + hoff hs ht hne hys hy2 hy2s hfs hc hu2) ?_ + rintro out ⟨σ4, hm, -, Q, R', x, HB, hst4, hk4, hx4, -, hnqs, hnrel, wn, szn⟩ + refine ⟨σ4, yt, hy2, Or.inl ⟨hf0, hm⟩, ⟨Q, R', x, HB, hst4, hk4, + Or.inl (by rw [hx4]; rfl), hnqs, hnrel, by rw [hf0]; exact hto⟩, + Or.inl ⟨ReplyW.transZ (Z' := { Z with Q := Q1, R := R1 }) rfl rfl wm' szm wn, szm.trans szn⟩⟩ + · subst ha1 + have hks1 : L.ksb s2 = L.ksb s := wm.1 + refine WP.spec_mono (replyNormalCapsA (AR := AR) (Hh := Hh) (OS := OS) ⟨Q1, R1, H2⟩ P PE E2 hSt2 hV2 hC2 + hN2 hoff hs ht hne hys hy2 hy2s hfs hu2 hsub + (fun s1 σ1 hR1 hS1 hW1 hE1 => hJ s1 σ1 hR1 hS1 + (ReplyW.trans wm' szm (hW1.mono fun _ hb => ewrite_stepNA_buf PE.tcb hb)) + (szm.trans (ssz_of_epStep hE1))) + hep hE (fun s1 σ1 h hS => hRd s1 σ1 (h.trans hks1) hS)) ?_ + rintro out ⟨σ4, hm, -, Q, R', x, HB, hst4, hk4, hx4, -, hnqs, hnrel, hX⟩ + exact ⟨σ4, yt, hy2, Or.inl ⟨hf0, hm⟩, ⟨Q, R', x, HB, hst4, hk4, + Or.inl (by rw [hx4]; rfl), hnqs, hnrel, by rw [hf0]; exact hto⟩, Or.inr (hX.pre wm' szm)⟩ + · obtain ⟨f, hf⟩ : ∃ f, y2.tcbFault = some f := by + cases e : y2.tcbFault with + | none => exact absurd (by rw [hft, hrel2.fault0.2 e]; unfold fl4_abi.consts.fault.NullFault; rfl) h0 + | some f => exact ⟨f, rfl⟩ + refine WP.spec_mono (faultArm s2 a1 σ2 y2 ft f hM hy2 hft h0 hf) ?_ + rintro out ⟨σ3, hm, hT, wf, zf⟩ + exact ⟨σ3, y2, hy2, Or.inr ⟨f, hf, hm⟩, by rw [hf]; exact ⟨hT, Or.inl ⟨ReplyW.trans wm' szm wf, szm.trans zf⟩⟩⟩ + · rintro s3 a2 σ3 ft fault ⟨hT, hW⟩ + refine WP.spec_mono (replyTailJointA ht (hmb a2) hidle tailS timeout hT) ?_ + rintro out ⟨σ4, hm, hR, wt, zt⟩ + refine ⟨σ4, hm, hR, ?_⟩ + rcases hW with ⟨wT, zT⟩ | hX + · exact Or.inl ⟨ReplyW.trans wT zT wt, zT.trans zt⟩ + · exact Or.inr (hX.ext wt zt) + +end Store + +end + +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/IpcCapsSendA.lean b/proofs/Fl4ProofsA64/P3/IpcCapsSendA.lean new file mode 100644 index 000000000..b8ea7da06 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/IpcCapsSendA.lean @@ -0,0 +1,326 @@ +/- +P3 (aarch64, Stage-3 `hcaps` drop, ruling (b), contract §13.4): **`send_ipc` to a waiting receiver with an +extra-cap transfer** (a granting, fault-free sender whose message carries extra caps). `EpSendRecvA.send_ipc_recvA` +excludes this case (`hcase`); `do_ipc_transfer_capsA` lives downstream of `EpSendRecvA` +(`IpcXferCapsDoA → IpcXferOrthA → IpcExtraCapTransferDischA → … → EpSendRecvA`), so the caps joint is this new +downstream module. No existing statement changed. + +* Spec: **`sendIpc_recv_runMemA`** / **`sendIpc_recv_blockMemA`** (`sendIpc_recv_runA` / `sendIpc_recv_blockA` from + a transfer outcome by membership: `cap_insert` makes `do_ipc_transfer` nondeterministic). +* `XcKhFrA.tcbA` / `.keepA` / `.backA`: what the non-receiver heap frame of the transfer keeps. +* **`XcJointFootA`**: the footprint export of a joint with a cap transfer: endpoint writes up to the cap loop, the + joint relation `JAtZAX` at a new CDT ghost right after `transfer_caps`, then `FpA` (endpoint or scheduler words). +* **`send_ipc_recvCapsA`**: dequeue, `do_ipc_transfer_capsA`, then the unchanged tails of `send_ipc_recvA` + (`send_recv_replyA` rebased at the transfer's post-state, `send_tail_fromA`, `send_block_tailA`). + +Case hypotheses (no named premise): those of `send_ipc_recvA` (without `hcase`) and those of +`do_ipc_transfer_capsA`, stated at the entry state (`hJ` over every endpoint-write / `EpStep` successor; `hRd` over +states with the entry's `ksb`). The post gives an outcome by membership (no spec non-failure, which the dispatch +consumers drop). +-/ +import Fl4ProofsA64.P3.IpcXferCapsDoA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3 Fl4ProofsA64.P3.Notif Fl4SpecA64 Sched + +noncomputable section + +section Spec +open Fl4Spec + +/-- `sendIpc_recv_runA` by membership: the transfer's outcome is one of several. -/ +theorem sendIpc_recv_runMemA {σ : State Unit} {t e d : ObjRef} {q : List ObjRef} + (hk : σ.kheap e = some (.endpoint (.recvEP (d :: q)))) {xd : Tcb} (hkd : σ.kheap d = some (.tcb xd)) + (hde : d ≠ e) {e' : ObjRef} {rep : Option ObjRef} {dd : ReceiverPayload} + (hst : xd.tcbState = .blockedOnReceive e' rep dd) (blk : Bool) (badge : Badge) (g gr : Bool) + {σ2 σ3 σ' : State Unit} + (hT : ((), σ2) ∈ (doIpcTransfer (Z := Unit) t (some e) badge g d + (updK σ e (.endpoint (epDeq (.recvEP (d :: q)) d)))).1) + (hU : NonDet.maybeM (replyUnlinkTcb (Z := Unit) d) rep σ2 = ({((), σ3)}, False)) + {xt3 x3 : Tcb} (hkt3 : σ3.kheap t = some (.tcb xt3)) (hf3 : xt3.tcbFault = none) + (hkd3 : σ3.kheap d = some (.tcb x3)) + (hτ : ((), σ') ∈ (specSendTailA d (stsAct (updK σ3 d (.tcb { x3 with tcbState := .running })) d)).1) : + ((), σ') ∈ (sendIpc (Z := Unit) blk false badge g gr false t e σ).1 := by + have hkd1 : (updK σ e (.endpoint (epDeq (.recvEP (d :: q)) d))).kheap d = some (.tcb xd) := by + rw [updK_kheap, Function.update_of_ne hde, hkd] + unfold sendIpc + rw [nd_bind_one (getEndpoint_eq hk)] + cases blk <;> dsimp only <;> + · rw [nd_bind_one (tcbEpDequeue_eq hk (by simp [Endpoint.epQueue])), nd_bind_one (getThreadState_eq hkd1), hst] + dsimp only + rw [nd_pure_bind'] + refine mem_bindA.2 ⟨((), σ2), hT, ?_⟩ + try dsimp only + rw [nd_bind_one hU, nd_bind_one (getTcbObjRef_eq _ hkd3), nd_bind_one (threadGet_eq _ hkt3), hf3] + simp only [Bool.false_eq_true, ne_eq, not_true_eq_false, or_self, ↓reduceIte, false_and] + rw [nd_when_false_bindA (by simp), nd_bind_one (setThreadState_eq _ hkd3)] + exact hτ + +/-- `sendIpc_recv_blockA` by membership. -/ +theorem sendIpc_recv_blockMemA {σ : State Unit} {t e d : ObjRef} {q : List ObjRef} + (hk : σ.kheap e = some (.endpoint (.recvEP (d :: q)))) {xd : Tcb} (hkd : σ.kheap d = some (.tcb xd)) + (hde : d ≠ e) {e' : ObjRef} {rep : Option ObjRef} {dd : ReceiverPayload} + (hst : xd.tcbState = .blockedOnReceive e' rep dd) (blk call : Bool) (badge : Badge) (g gr cd : Bool) + {σ2 σ3 σ' : State Unit} + (hT : ((), σ2) ∈ (doIpcTransfer (Z := Unit) t (some e) badge g d + (updK σ e (.endpoint (epDeq (.recvEP (d :: q)) d)))).1) + (hU : NonDet.maybeM (replyUnlinkTcb (Z := Unit) d) rep σ2 = ({((), σ3)}, False)) + {xt3 x3 : Tcb} (hkt3 : σ3.kheap t = some (.tcb xt3)) (hkd3 : σ3.kheap d = some (.tcb x3)) (htd : t ≠ d) + (hcf : call = true ∨ xt3.tcbFault ≠ none) (hnp : (g = false ∧ gr = false) ∨ rep = none) + (hτ : ((), σ') ∈ (specSendTailA d (stsAct (updK (stsAct (updK σ3 t (.tcb { xt3 with tcbState := .inactive })) t) d + (.tcb { x3 with tcbState := .running })) d)).1) : + ((), σ') ∈ (sendIpc (Z := Unit) blk call badge g gr cd t e σ).1 := by + have hkd1 : (updK σ e (.endpoint (epDeq (.recvEP (d :: q)) d))).kheap d = some (.tcb xd) := by + rw [updK_kheap, Function.update_of_ne hde, hkd] + have hkd4 : (stsAct (updK σ3 t (.tcb { xt3 with tcbState := .inactive })) t).kheap d = some (.tcb x3) := by + rw [stsAct_kheap, updK_kheap, Function.update_of_ne (Ne.symm htd), hkd3] + have hn : ¬ ((g = true ∨ gr = true) ∧ rep ≠ none) := by + rintro ⟨h1, h2⟩ + rcases hnp with ⟨rfl, rfl⟩ | rfl + · simp at h1 + · exact h2 rfl + unfold sendIpc + rw [nd_bind_one (getEndpoint_eq hk)] + cases blk <;> dsimp only <;> + · rw [nd_bind_one (tcbEpDequeue_eq hk (by simp [Endpoint.epQueue])), nd_bind_one (getThreadState_eq hkd1), hst] + dsimp only + rw [nd_pure_bind'] + refine mem_bindA.2 ⟨((), σ2), hT, ?_⟩ + try dsimp only + rw [nd_bind_one hU, nd_bind_one (getTcbObjRef_eq _ hkd3), + nd_bind_one (threadGet_eq _ hkt3), if_pos hcf, if_neg hn, nd_bind_one (setThreadState_eq _ hkt3), + nd_bind_one (setThreadState_eq _ hkd4)] + exact hτ + +end Spec + +/-! ## The non-receiver heap frame -/ + +/-- A TCB other than `r` stays a TCB with the same non-cap fields. -/ +theorem XcKhFrA.tcbA {r p : ObjRef} {σ σ' : State Unit} (h : XcKhFrA r σ σ') (hp : p ≠ r) {x : Tcb} + (hx : σ.kheap p = some (.tcb x)) : ∃ y, σ'.kheap p = some (.tcb y) ∧ tcbNcA y = tcbNcA x := by + rcases h p hp with e | ⟨n, cs, cs', e1, -⟩ | ⟨z, z', e1, e2, e3⟩ + · exact ⟨x, e.trans hx, rfl⟩ + · rw [hx] at e1; cases e1 + · rw [hx] at e1; cases e1; exact ⟨z', e2, e3⟩ + +/-- An object other than `r` that is no CNode and no TCB is kept. -/ +theorem XcKhFrA.keepA {r p : ObjRef} {σ σ' : State Unit} (h : XcKhFrA r σ σ') (hp : p ≠ r) {o : KernelObject} + (ho : σ.kheap p = some o) (hc : ∀ n cs, o ≠ .cnode n cs) (ht : ∀ x, o ≠ .tcb x) : σ'.kheap p = some o := by + rcases h p hp with e | ⟨n, cs, cs', e1, -⟩ | ⟨z, z', e1, -, -⟩ + · exact e.trans ho + · rw [ho] at e1; cases e1; exact absurd rfl (hc n cs) + · rw [ho] at e1; cases e1; exact absurd rfl (ht z) + +/-- An object other than `r` that is no CNode and no TCB after the step was there before. -/ +theorem XcKhFrA.backA {r p : ObjRef} {σ σ' : State Unit} (h : XcKhFrA r σ σ') (hp : p ≠ r) {o : KernelObject} + (ho : σ'.kheap p = some o) (hc : ∀ n cs, o ≠ .cnode n cs) (ht : ∀ x, o ≠ .tcb x) : σ.kheap p = some o := by + rcases h p hp with e | ⟨n, cs, cs', -, e2⟩ | ⟨z, z', -, e2, -⟩ + · exact e.symm.trans ho + · rw [ho] at e2; cases e2; exact absurd rfl (hc n cs') + · rw [ho] at e2; cases e2; exact absurd rfl (ht z') + +section Fl4 +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS : Set U64} +variable {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} +variable {Wr : State Unit → S → U64 → Prop} + +variable (st AR Z OS E R Bf Wr m) in +/-- **The footprint of a joint with a cap transfer** from `(s, σ)` to `s'`: endpoint writes (an `EpStep`) up to the +cap loop, the joint relation at a new CDT ghost `G'` right after `transfer_caps` (the MDB tree, `SInvXA` kept), then +endpoint or scheduler words (`FpA`) from there. -/ +def XcJointFootA (s : S) (a : A) (σ : State Unit) (s' : S) : Prop := + ∃ s1 σ1 s2 σ2 G', WritesIn st s s1 (EWrite Z.T E R Bf) ∧ EpStep σ σ1 ∧ + JAtZAX (L := L) AR { Z with G := G' } s2 a σ2 m ∧ TreeA st OS G'.chains G'.par s2 ∧ + (∀ n, SInvXA σ1 m n → SInvXA σ2 m n) ∧ FpA st Z.T E R Bf Wr σ2 s2 s' + +/-- **`send_ipc` to a waiting receiver with an extra-cap transfer** (module docstring). -/ +theorem send_ipc_recvCapsA (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (hF : SchedFootEA st Z.T E R Bf (IpcSchedRelA st Z m κ) Wr) + {s : S} {a : A} {σ : State Unit} (hPl : PlaceA Z.th (L.ksb s)) + (hR : EpStateRel Z.T E R Bf (L.mem s) σ) (hSR : IpcSchedRelA st Z m κ s σ) + (hSt : StateRelA Z.th (L.mem s) σ) (hV : SlotsValidA Z.th (L.mem s)) (hC : BufCover st Z.T Bf s σ) + (hNative : BufNativeA Z.T (L.mem s)) + (hoff : ∀ y, IsSlotA Z.th y → ¬ EWrite Z.T E R Bf y ∧ ¬ EWrite Z.T E R Bf (core.num.U64.wrapping_add y 8#u64)) + {t : U64} (ht : t ∈ Z.T) (hth : Z.th t = some .tcb) {xt : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hnb : tsCode xt.tcbState ≠ 3) (hft : xt.tcbFault = none) + {e d : U64} (he : e ∈ E) {q : List ObjRef} (hk : σ.kheap (specPtr e) = some (.endpoint (.recvEP (specPtr d :: q)))) + (hdh : Z.th d = some .tcb) {xd : Tcb} (hkd : σ.kheap (specPtr d) = some (.tcb xd)) + (hu : ∀ rp, ipcBufOfA true xd = some rp → ∀ k, k < 128 → inUserFrame (bufAddr rp k) σ) + (hrep : RepOKA σ (specPtr d)) (hon : L.mem s (affA d) = L.cpu s) + (badge : U64) (g gr : Bool) (blk call cd : Bool) + (hbr : (call = false ∧ cd = false) ∨ + (call = true ∧ ((g = false ∧ gr = false) ∨ ∀ e' r dd, xd.tcbState ≠ .blockedOnReceive e' (some r) dd) ∧ + SndRunA σ (specPtr t) xt)) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hJ : ∀ s1 σ1, EpStateRel Z.T E R Bf (L.mem s1) σ1 → IpcSchedRelA st Z m κ s1 σ1 → + WritesIn st s s1 (EWrite Z.T E R Bf) → EpStep σ σ1 → + InsBaseA (L := L) AR Z H OS s1 a σ1 m ∧ TreeA st OS Z.G.chains Z.G.par s1 ∧ + (∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s1 y = .Null) ∧ + ∀ bp ∈ Bf, ∀ k : U64, k.val < 128 → + ¬ FootA (L := L) Z s1 (bufW bp.1 k) ∧ ¬ InOS OS (bufW bp.1 k)) + (hep : ∀ s1 (x : U64) (c : Cap), CapRelA (capAtA (L.mem s1) x) c → + (UnwCA e (capA st s1 x) ↔ SUnwA (some (specPtr e)) c)) + (hE : ∀ x, W7.CapW H.tcbs {b | InOS OS b} ∅ x → ¬ EFoot Z.T E R Bf x) + (hRd : ∀ s1 σ1, L.ksb s1 = L.ksb s → IpcSchedRelA st Z m κ s1 σ1 → + ∀ x, W7.CapW H.tcbs {b | InOS OS b} ∅ x → ¬ SReadA st Z m s1 σ1 x) : + endpoint.send_ipc st ar s a blk call badge g gr cd t e ⦃ r => r.2 = a ∧ + ∃ σ', ((), σ') ∈ (sendIpc (Z := Unit) blk call badge.bv g gr cd (specPtr t) (specPtr e) σ).1 ∧ + EpStateRel Z.T E R Bf (L.mem r.1) σ' ∧ IpcSchedRelA st Z m κ r.1 σ' ∧ + XcJointFootA st AR Z OS m E R Bf Wr s a σ r.1 ⦄ := by + have hO := schedOpsEAOfPlacementA (st := st) (m := m) (κ := κ) P + obtain ⟨hst2, xs, hxs0, hq, hQ, hB⟩ := hR.at_ep he hk + obtain ⟨d', xs', rfl⟩ := List.exists_cons_of_ne_nil hxs0 + simp only [List.map_cons, List.cons.injEq] at hq + obtain ⟨hd', hq'⟩ := hq + have hdd : d = d' := specPtr_inj hd' + subst hdd + have hdT : d ∈ Z.T := hQ.mem d (by simp) + have hda := PE.tcb d hdT + have hxd := hR.at_tcb hdT hkd + have hxt := hR.at_tcb ht hkt + have hd3 : (L.mem s (stA d)).val = 3 := (hB d (by simp)).1 + have hdb : L.mem s (tBlockA d) = e := (hB d (by simp)).2 + obtain ⟨e', rep, dd, hstd⟩ := tsCode_three (hxd.st.symm.trans hd3) + have he' : e' = specPtr e := by + have := hxd.blk e' (by rw [hstd]; rfl); rw [hdb] at this; exact this.symm + subst he' + have htd : t ≠ d := by intro h; subst h; exact hnb (hxt.st.symm.trans hd3) + have hde : specPtr d ≠ specPtr e := kheap_ne hkd hk (fun _ h1 h2 => by subst h1; cases h2) + have hte : specPtr t ≠ specPtr e := kheap_ne hkt hk (fun _ h1 h2 => by subst h1; cases h2) + have htd' : specPtr t ≠ specPtr d := fun h => htd (specPtr_inj h) + -- FL4: the endpoint's state and queue + unfold endpoint.send_ipc + step with obj.Ep.state.spec as ⟨st0, hst0⟩ + rw [epState_eq, hst2] at hst0 + subst hst0 + rw [if_neg (by rw [ep_state_vals.1]; decide), if_neg (by rw [ep_state_vals.2.1]; decide)] + step with endpoint.get_queue.spec as ⟨q0, hq0⟩ + have hhead : q0.head = d := by rw [hq0, epQ_eq]; exact hQ.head + rw [hhead] + -- the dequeue + have hd : specPtr d ∈ (Endpoint.recvEP (specPtr d :: q)).epQueue := by simp [Endpoint.epQueue] + refine WP.spec_bind (ep_dequeue_raw PE s σ hR he hk hd) ?_ + rintro s1 ⟨-, hR1, hW1, hE1, hA1⟩ + have hS1 := hO.stable _ _ _ _ hSR hW1 hE1 (NoBlockE.updO _ _ fun _ h => by cases h) + have hkt1 : (updK σ (specPtr e) (.endpoint (epDeq (.recvEP (specPtr d :: q)) (specPtr d)))).kheap (specPtr t) = + some (.tcb xt) := by rw [updK_kheap, Function.update_of_ne hte, hkt] + have hkd1 : (updK σ (specPtr e) (.endpoint (epDeq (.recvEP (specPtr d :: q)) (specPtr d)))).kheap (specPtr d) = + some (.tcb xd) := by rw [updK_kheap, Function.update_of_ne hde, hkd] + have hmem : ∀ y, IsSlotA Z.th y → L.mem s1 y = L.mem s y ∧ + L.mem s1 (core.num.U64.wrapping_add y 8#u64) = L.mem s (core.num.U64.wrapping_add y 8#u64) := by + intro y hy + have hal : Aligned y := by have := (IsSlotA.al32 hPl hy).1; unfold Aligned; omega + exact ⟨hW1.2.2 y hal (hoff y hy).1, hW1.2.2 _ (wa_al hal (by simp)) (hoff y hy).2⟩ + have hSt1 := hSt.updEpA hPl hmem hk (epDeq (.recvEP (specPtr d :: q)) (specPtr d)) + have hV1 : SlotsValidA Z.th (L.mem s1) := slotsValid_xferA hV hmem + have hC1 : BufCover st Z.T Bf s1 (updK σ (specPtr e) (.endpoint (epDeq (.recvEP (specPtr d :: q)) (specPtr d)))) := + BufCover.ewA PE hC hW1 fun u _ x hx => by + have hne : specPtr u ≠ specPtr e := fun h => by + rw [h, updK_kheap, Function.update_self] at hx; cases hx + rwa [updK_kheap, Function.update_of_ne hne] at hx + have hu1 : ∀ rp, ipcBufOfA true xd = some rp → ∀ k, k < 128 → + inUserFrame (bufAddr rp k) (updK σ (specPtr e) (.endpoint (epDeq (.recvEP (specPtr d :: q)) (specPtr d)))) := + fun rp hrp k hk' => inUserFrame_updK hk (fun _ h => by cases h) _ (hu rp hrp k hk') + have hPl1 : PlaceA Z.th (L.ksb s1) := by rw [hW1.1]; exact hPl + -- the transfer with extra caps + refine WP.spec_bind (do_ipc_transfer_capsA (AR := AR) (H := H) (OS := OS) P PE hPl1 hR1 hS1 hSt1 hV1 hC1 + (hNative.ewA PE hW1) ht hdT htd hth hdh hkt1 hkd1 hft hu1 hoff e badge g hsub + (fun s2 σ2 hR2 hS2 hW2 hE2 => hJ s2 σ2 hR2 hS2 (WritesIn.ew hW1 hW2) (hE1.trans hE2)) hep hE + (fun s2 σ2 h hS => hRd s2 σ2 (h.trans hW1.1) hS)) ?_ + rintro s2 ⟨σ2, hT, hR2, hS2, ⟨sa, σa, sb, σb, G', hWa, hEa, J, Tr, SI, hWb, hEb⟩, hfr, hK, x2, hkd2, hx2⟩ + obtain ⟨xt2, hkt2, hxt2⟩ := XcKhFrA.tcbA hfr htd' hkt1 + have hft2 : xt2.tcbFault = none := by + have := congrArg Tcb.tcbFault hxt2; exact this.trans hft + have hst2' : x2.tcbState = .blockedOnReceive (specPtr e) rep dd := by + have := congrArg Tcb.tcbState hx2; exact this.trans hstd + have hk1e : (updK σ (specPtr e) (.endpoint (epDeq (.recvEP (specPtr d :: q)) (specPtr d)))).kheap (specPtr e) = + some (.endpoint (epDeq (.recvEP (specPtr d :: q)) (specPtr d))) := by rw [updK_kheap, Function.update_self] + have hdb1 : L.mem s1 (tBlockA d) = e := by rw [hA1 _ (a_blk hda).1 ⟨d, hdT, (a_blk hda).2⟩, hdb] + have hq1 := dequeued_not_queued hR1 he hdb1 hk1e + (epDeq_not_mem (z := .recvEP (specPtr d :: q)) (xs := d :: xs') (by rw [hq']; rfl) hQ.nodup d) + have hq2 : ∀ e'' ∈ E, ∀ z, σ2.kheap (specPtr e'') = some (.endpoint z) → specPtr d ∉ z.epQueue := by + intro e'' he'' z hz + have hne : specPtr e'' ≠ specPtr d := by intro h; rw [h, hkd2] at hz; cases hz + exact hq1 e'' he'' z (hfr.backA hne hz (fun _ _ h => by cases h) (fun _ h => by cases h)) + have hrr : ∀ r, rep = some r → ∃ xr, σ2.kheap r = some (.reply xr) ∧ xr.replyTcb = some (specPtr d) := by + intro r hr + obtain ⟨xr, hkr, hb⟩ := hrep xd hkd _ _ _ (by rw [hstd, hr]) + have hre : r ≠ specPtr e := fun h => by rw [h, hk] at hkr; cases hkr + have hrd : r ≠ specPtr d := fun h => by rw [h, hkd] at hkr; cases hkr + refine ⟨xr, hfr.keepA hrd ?_ (fun _ _ h => by cases h) (fun _ h => by cases h), hb⟩ + rw [updK_kheap, Function.update_of_ne hre, hkr] + have hK2 : SchedKeepA σ σ2 := (SchedKeepA.step hE1).trans hK + have hpre : ∀ s', FpA st Z.T E R Bf Wr σ2 s2 s' → + XcJointFootA st AR Z OS m E R Bf Wr s a σ s' := fun s' hfp => + ⟨sa, σa, sb, σb, G', WritesIn.ew hW1 hWa, hE1.trans hEa, J, Tr, SI, + FpA.trans hF (FpA.of_ew hWb) (ssz_of_epStep hEb) hfp⟩ + -- the tails of `send_ipc_recvA`, rebased at the transfer's post-state + step with obj.Tcb.reply_obj.spec as ⟨i, hi⟩ + have hi0 : rep = none → i = 0#u64 := fun h => by + rw [hi]; exact optRef_none (((hR2.at_tcb hdT hkd2).recv _ _ _ hst2').1.trans h) + refine WP.spec_bind (send_recv_replyA hO.to0 hF.to0 PE hS2 hR2 hS2 (WritesIn.refl _ _) (EpStep.refl _) hdT + (fun _ _ => rfl) htd' hkt2 hkd2 hkd2 hst2' hst2' hrr hq2 i hi) ?_ + rintro s3 ⟨⟨σ3, x3, hR3, hS3, hU, hkt3, hkd3, hnr3, hni3, hcls3, hq3, hK3, hz3⟩, hfp3⟩ + have hxt3 := hR3.at_tcb ht hkt3 + rcases hbr with ⟨rfl, rfl⟩ | ⟨rfl, hnp, hsr⟩ + · -- no call, no donation: the receiver runs + simp only [Bool.false_eq_true, ↓reduceIte] + step with obj.Tcb.fault_type.spec as ⟨i1, hi1⟩ + have hf0 : i1 = fl4_abi.consts.fault.NullFault := by + rw [hi1, hxt3.fault0.2 hft2]; unfold fl4_abi.consts.fault.NullFault; rfl + simp only [hf0, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + apply ok_bind_specA + refine WP.spec_bind (set_thread_state_relF hO hF PE s3 σ3 hR3 hS3 hdT hkd3 hq3 .running (by decide) + sched.TS_RUNNING (by unfold sched.TS_RUNNING; rfl) (NoBlockE.updT hkd3 _ (Or.inr (Or.inl ⟨rfl, hni3, hcls3⟩)))) + ?_ + rintro s5 ⟨hR5, hS5, hfp5⟩ + refine WP.spec_mono (send_tail_fromA hO hF PE a hSR hdT hon hS3 hkd3 hnr3 hq3 hR5 hS5 + (FpA.trans hF hfp3 hz3 hfp5) (hz3.trans ((ssz_updK_tcb _ _ _).trans (ssz_stsAct _ _)))) ?_ + rintro r ⟨ha, -, σ', hm, hR', hS', hfp'⟩ + exact ⟨ha, σ', sendIpc_recv_runMemA hk hkd hde hstd blk badge.bv g gr hT hU hkt3 hft2 hkd3 hm, hR', hS', + hpre _ hfp'⟩ + · -- a call without a reply push: the sender becomes inactive + have hsr3 : SndRunA σ3 (specPtr t) xt2 := by + obtain ⟨h1, h2, h3, h4⟩ := hsr.keep (hK2.trans hK3) + refine ⟨?_, h2, h3, h4⟩ + have := congrArg Tcb.tcbState hxt2; rw [show xt2.tcbState = xt.tcbState from this]; exact h1 + have hnp' : (g = false ∧ gr = false) ∨ rep = none := hnp.imp id fun h => by + cases rep with + | none => rfl + | some r => exact absurd hstd (h _ r dd) + have htail := send_block_tailA hO hF PE a hSR ht hdT htd hon hR3 hS3 hkt3 hsr3 hkd3 hnr3 hni3 hcls3 hq3 rfl + hfp3 hz3 + have hfin : ∀ (x : Result (S × A)), x ⦃ r => r.2 = a ∧ + ¬ (specSendTailA (specPtr d) (stsAct (updK (stsAct (updK σ3 (specPtr t) + (.tcb { xt2 with tcbState := .inactive })) (specPtr t)) (specPtr d) (.tcb { x3 with tcbState := .running })) + (specPtr d))).2 ∧ + ∃ σ', ((), σ') ∈ (specSendTailA (specPtr d) (stsAct (updK (stsAct (updK σ3 (specPtr t) + (.tcb { xt2 with tcbState := .inactive })) (specPtr t)) (specPtr d) + (.tcb { x3 with tcbState := .running })) (specPtr d))).1 ∧ + EpStateRel Z.T E R Bf (L.mem r.1) σ' ∧ IpcSchedRelA st Z m κ r.1 σ' ∧ + FpA st Z.T E R Bf Wr σ2 s2 r.1 ⦄ → + x ⦃ r => r.2 = a ∧ + ∃ σ', ((), σ') ∈ (sendIpc (Z := Unit) blk true badge.bv g gr cd (specPtr t) (specPtr e) σ).1 ∧ + EpStateRel Z.T E R Bf (L.mem r.1) σ' ∧ IpcSchedRelA st Z m κ r.1 σ' ∧ + XcJointFootA st AR Z OS m E R Bf Wr s a σ r.1 ⦄ := by + intro x hx + refine WP.spec_mono hx ?_ + rintro r ⟨ha, -, σ', hm, hR', hS', hfp'⟩ + exact ⟨ha, σ', sendIpc_recv_blockMemA hk hkd hde hstd blk true badge.bv g gr cd hT hU hkt3 hkd3 htd' + (Or.inl rfl) hnp' hm, hR', hS', hpre _ hfp'⟩ + simp only [↓reduceIte] + rcases hnp' with ⟨rfl, rfl⟩ | hrn + · simp only [Bool.false_eq_true, ↓reduceIte, ite_self] + exact hfin _ htail + · simp only [hi0 hrn, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + exact hfin _ htail + +end Fl4 + +end + +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/IpcExtraCapInsertDischA.lean b/proofs/Fl4ProofsA64/P3/IpcExtraCapInsertDischA.lean new file mode 100644 index 000000000..0e14a7607 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/IpcExtraCapInsertDischA.lean @@ -0,0 +1,48 @@ +/- +P3 (aarch64, Stage-3 ledger): the selected insert of a cap-inserting IPC transfer, `IpcExtraCapInsertA`, from S3's +`cap_insert_jA`. + +The joint relation is the successor `TreeA` and the conditional preservation of `SInvXA` (given `CapOkA` at the +destination). The premises are `cap_insert_jA`'s, at the reached state, for the derived cap `nc` and its abstract +image `absCapA nc`. The loop `IpcExtraCapTransferA` is not discharged here. +New module; no existing statement changed. +-/ +import Fl4ProofsA64.P3.InsertA +import Fl4ProofsA64.P3.IpcExtraCapTransferA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 Fl4ProofsA64.P3 + +variable {S A : Type} {st : machine.Store S} [L : LawfulStore st] + +theorem ipcExtraCapInsertA_of_jEntA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} + {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} {ia ie : Nat} {s : S} {a : A} {σ : State Unit} {m : McState} + (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + (hT : TreeA st OS Z.G.chains Z.G.par s) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) + {ss sd : CslotPtr} (hAs : AbsSlotA Z.th ss) (hAd : AbsSlotA Z.th sd) + (hsn : capAtA (L.mem s) (cslotAddrA ss) ≠ .Null) + (hdn : capAtA (L.mem s) (cslotAddrA sd) = .Null) + (hdc : obj.sext48 (cslotAddrA sd) = ok (cslotAddrA sd)) + {nc : capability.Cap} (hwf : CapWfA nc) + (hw : CapRt.EncWf nc) (hn : nc ≠ .Null) + (hal : UA nc (capA st s (cslotAddrA ss))) + (hk : SameBadgeKindA nc (capA st s (cslotAddrA ss))) + (hut : isUtA (capA st s (cslotAddrA ss)) = true → + ∀ y, ¬ Fl4Proofs.P3.Mdb.Desc Z.G.par y (cslotAddrA ss)) + (hub : isUtA (capA st s (cslotAddrA ss)) = true → + insBA σ ss (absCapA nc) (absCapA (capA st s (cslotAddrA ss))) = true) + (hsib : insBA σ ss (absCapA nc) (absCapA (capA st s (cslotAddrA ss))) = false → + ∀ y, ¬ Fl4Proofs.P3.Mdb.Desc Z.G.par y (cslotAddrA ss)) + (hno : CapObjOkA Z nc) : + IpcExtraCapInsertA st AR Z m a s σ ss sd nc (fun G' s' σ' => + TreeA st OS G'.chains G'.par s' ∧ + (CapOkA σ.interruptIrqNode sd (absCapA nc) → ∀ n, SInvXA σ m n → SInvXA σ' m n)) := by + refine WP.spec_mono (cap_insert_jA E hT hsub hnul hAs hAd hsn hdn hdc hwf (CapEqA.of_abs rfl) hw hn hal hk + hut hub hsib hno) ?_ + rintro s' ⟨σ', hmem, hJ, hT', hS⟩ + exact ⟨σ', _, hmem, hJ, hT', hS⟩ + +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/IpcExtraCapTransferA.lean b/proofs/Fl4ProofsA64/P3/IpcExtraCapTransferA.lean new file mode 100644 index 000000000..4fd544148 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/IpcExtraCapTransferA.lean @@ -0,0 +1,51 @@ +/- +P3 (AArch64): the successful extra-cap transfer obligation. + +[J] Stage 3 discharges the selected insert through S3's cap_insert_jA, +including CapWfA for the derived cap and the updated CDT and slot ghosts. +The ordinary zero-extra-cap IPC and fault payload proofs use their +existing leaves. +-/ +import Fl4ProofsA64.P3.IpcScGlueA +import Fl4ProofsA64.P3.EpXferA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4SpecA64 + +noncomputable section + +/-- The selected successful insert at a concrete transfer-loop iteration. +The source remains the slot in the extra-cap array, and the reached +destination is the sole receive slot. The spec reads the same source, +derives the same non-null cap, then continues with no receive slot. +The post keeps IPC, scheduler and object frames in the supplied joint +relation while replacing the actual CDT ghost. -/ +def IpcExtraCapInsertA {S A : Type} (st : machine.Store S) [L : LawfulStore st] + (AR : A → HwRootA → Prop) (Z : XSetsA) (m : McState) + (a : A) (s : S) (σ : State Unit) (src dest : CslotPtr) (derived : capability.Cap) + (Joint : MdbGhostA → S → State Unit → Prop) : Prop := + cdt.insert st s derived (cslotAddrA src) (cslotAddrA dest) ⦃ s' => + ∃ σ' G', + ((), σ') ∈ (capInsert (Z := Unit) (absCapA derived) src dest σ).1 ∧ + JAtZAX (L := L) AR { Z with G := G' } s' a σ' m ∧ Joint G' s' σ' ⦄ + +/-- Lifting the checked loop, including its successful insertion and +remaining unwraps, must produce an outcome of the full abstract +transferCaps, with the related returned MessageInfo. -/ +def IpcExtraCapTransferA {S A : Type} (st : machine.Store S) [L : LawfulStore st] + (AR : A → HwRootA → Prop) (Z : XSetsA) (m : McState) (a : A) + (s : S) (σ : State Unit) (info : msginfo.MessageInfo) (mi : MessageInfo) + (extra : Array obj.Slot 3#usize) (caps : List (Cap × CslotPtr)) + (ep receiver recvBuf : U64) (ep' : Option ObjRef) (rb : ObjRef) + (Joint : MdbGhostA → S → State Unit → Prop) : Prop := + ipc.transfer_caps st s info extra ep receiver recvBuf ⦃ r => + ∃ mi' σ' G', + (mi', σ') ∈ (transferCaps (Z := Unit) mi caps ep' (specPtr receiver) (some rb) σ).1 ∧ + r.1.length.bv = mi'.miLength ∧ r.1.extra_caps.bv = mi'.miExtraCaps ∧ + r.1.caps_unwrapped.bv = mi'.miCapsUnwrapped ∧ r.1.label.bv = mi'.miLabel ∧ + JAtZAX (L := L) AR { Z with G := G' } r.2 a σ' m ∧ Joint G' r.2 σ' ⦄ + +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/IpcExtraCapTransferDischA.lean b/proofs/Fl4ProofsA64/P3/IpcExtraCapTransferDischA.lean new file mode 100644 index 000000000..695f553f2 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/IpcExtraCapTransferDischA.lean @@ -0,0 +1,117 @@ +/- +P3 (aarch64, Stage-3 ledger): **the extra-cap transfer obligation `IpcExtraCapTransferA` discharged** +(`ipcExtraCapTransferA_of_jEntA`, Architect 02:12Z), at the joint relation `JAtZAX` with the MDB tree and the +conditional-free preservation of `SInvXA`. + +* **`transfer_caps_relA`**: FL4's `transfer_caps` against l4v `transfer_caps`: no extra cap (the spec's list is + empty), or the receive slot (`get_receive_slot_relA`) and the loop (`transfer_caps_loop_relA`). +* **`ipcExtraCapTransferA_of_jEntA`**: `IpcExtraCapTransferA … (fun G' s' σ' => TreeA … ∧ ∀ n, SInvXA σ m n → + SInvXA σ' m n)` from `JEntA` (through `InsBaseA.of_jEntA`). + +Case hypotheses (decode / join-site facts, no named premise): the tree at `OS` with `th`'s slots in it and its +other slots null (as `cap_insert_dA`), the `OS` slots canonical (`hCan`, P1 `DCan OS`), `ExtraCapsRelA` (the lookup of the extra caps), `hep` (FL4 unwraps an extra +cap iff the spec does), the `MessageInfo` fields, the receiver a TCB of `th`, the receive buffer `recvBuf ≠ 0` +with its words the spec's (`EpStateRel.buf`), aligned, in user frames, off the footprint and off the `OS` slots (not `¬ CapW` at the heap's `Ω`: a user frame's +words are `Ω` words, so that would be unsatisfiable; the +join's placement, `IpcSchedPlacementA.buffer`). New module; no existing statement changed. +The reply / endpoint joint theorems' `hcaps` is not dropped here (Architect 02:12Z: follow-up, an `XcLoopPostA` +slot-insert frame). +-/ +import Fl4ProofsA64.P3.IpcXferLoopA +import Fl4ProofsA64.P3.IpcExtraCapTransferA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3 Fl4SpecA64 + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS : Set U64} + +set_option maxHeartbeats 1000000 in +/-- **FL4's `transfer_caps` refines l4v `transfer_caps`** (module docstring). -/ +theorem transfer_caps_relA {s : S} {a : A} {σ : State Unit} {m : McState} + (B0 : InsBaseA (L := L) AR Z H OS s a σ m) (hT0 : TreeA st OS Z.G.chains Z.G.par s) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) + {extra : Aeneas.Std.Array obj.Slot 3#usize} {caps : List (Cap × CslotPtr)} + (hX : ExtraCapsRelA Z.th (L.mem s) σ extra caps) {ep : U64} {ep' : Option ObjRef} + (hep : ∀ j (hj : j < caps.length), + UnwCA ep (capA st s (cslotAddrA (caps[j]).2)) ↔ SUnwA ep' (caps[j]).1) + {info : msginfo.MessageInfo} {mi : MessageInfo} (hml : info.length.bv = mi.miLength) + (hmb : info.label.bv = mi.miLabel) {receiver : U64} (hrt : Z.th receiver = some .tcb) + {recvBuf : U64} {rb : ObjRef} (hb0 : recvBuf ≠ 0#u64) (hal : (bufAddr rb 0).toNat % 8 = 0) + (hbw : ∀ k : U64, k.val < 128 → + (L.mem s (bufW recvBuf k)).bv = loadWordAt σ.machineState.underlyingMemory (bufAddr rb k.val)) + (hbu : ∀ k, k < 128 → inUserFrame (bufAddr rb k) σ) + (hbF : ∀ k : U64, k.val < 128 → ¬ FootA (L := L) Z s (bufW recvBuf k)) + (hbC : ∀ k : U64, k.val < 128 → ¬ InOS OS (bufW recvBuf k)) + (hbA : ∀ k : U64, k.val < 128 → Aligned (bufW recvBuf k)) + {Q : S → State Unit → Prop} (hQ : XcOrthA st H OS recvBuf rb Q) (hQ0 : Q s σ) : + ipc.transfer_caps st s info extra ep receiver recvBuf ⦃ r => + ∃ mi' σ' G', + (mi', σ') ∈ (transferCaps (Z := Unit) mi caps ep' (specPtr receiver) (some rb) σ).1 ∧ + r.1.length.bv = mi'.miLength ∧ r.1.extra_caps.bv = mi'.miExtraCaps ∧ + r.1.caps_unwrapped.bv = mi'.miCapsUnwrapped ∧ r.1.label.bv = mi'.miLabel ∧ + JAtZAX (L := L) AR { Z with G := G' } r.2 a σ' m ∧ TreeA st OS G'.chains G'.par r.2 ∧ + (∀ n, SInvXA σ m n → SInvXA σ' m n) ∧ Q r.2 σ' ∧ r.1.extra_caps.val ≤ 3 ∧ mi'.miCapsUnwrapped.toNat < 8 ∧ mi'.miLabel = mi.miLabel ⦄ := by + have hP := B0.j.1.place + have hal' : ∀ k, k < 128 → (bufAddr rb k).toNat % 8 = 0 := fun k hk => bufAddr_align hal k hk + obtain ⟨d0, sl0, hgr, hgs, hd0⟩ := get_receive_slot_relA (st := st) hP s σ B0.j.1.st B0.j.1.cv hrt hb0 hal hbw + have hl0 : (0#usize : Usize).val < extra.val.length := by rw [extra.property]; simp + obtain ⟨x0, hx0', e0⟩ := spec_existsA (Aeneas.Std.Array.index_usize_spec extra 0#usize hl0) + have hx0v : extra.val[0]? = some x0 := by rw [e0]; simp + unfold ipc.transfer_caps + simp only [msginfo.MessageInfo.new, bind_ok, hx0', obj.Slot.is_null] + by_cases h0 : x0 = 0#u64 + · simp only [h0, decide_true, ↓reduceIte, WP.spec_ok] + have hcl : caps = [] := by + cases caps with + | nil => rfl + | cons c cs => + exfalso + have e := (hX.2.2 0 (by simp)).1 + have hne := PlaceA.slot_ne0 hP (AbsSlotA.isSlot hP (hX.2.2 0 (by simp)).2.1) + rw [e, h0] at hx0v + exact hne (Option.some.inj hx0v) + subst hcl + refine ⟨miAtA ⟨mi.miLength, 0, 0, mi.miLabel⟩ 0, σ, Z.G, transferCaps_memA hgs (tcl_nilA _ _ _ _), + hml, rfl, rfl, hmb, B0.j, hT0, fun _ h => h, hQ0, by simp, by simp [miAtA], rfl⟩ + simp only [h0, decide_false, Bool.false_eq_true, ↓reduceIte, hb0] + rw [hgr, bind_ok] + refine WP.spec_bind (transfer_caps_loop_relA (ep' := ep') B0 hT0 hsub hnul hX hep hal' hbu hbF hbC hbA hd0 + (out0 := { label := info.label, caps_unwrapped := 0#u64, extra_caps := 0#u64, length := info.length }) + (mi0 := ⟨mi.miLength, 0, 0, mi.miLabel⟩) ⟨hml, hmb, rfl, by simp⟩ hQ hQ0) ?_ + rintro ⟨s2, o2, i2⟩ ⟨mi', σ', G', hm, hl, hla, hcu, hex, J, T, SI, Qr, h3, h8, hlbl⟩ + try dsimp only + exact (WP.spec_ok _).2 ⟨mi', σ', G', transferCaps_memA hgs hm, hl, hex, hcu, hla, J, T, SI, Qr, h3, h8, hlbl⟩ + +/-- **`IpcExtraCapTransferA` discharged** (module docstring). -/ +theorem ipcExtraCapTransferA_of_jEntA {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} {ia ie : Nat} + {s : S} {a : A} {σ : State Unit} {m : McState} + (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) (hT0 : TreeA st OS Z.G.chains Z.G.par s) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) + {extra : Aeneas.Std.Array obj.Slot 3#usize} {caps : List (Cap × CslotPtr)} + (hX : ExtraCapsRelA Z.th (L.mem s) σ extra caps) {ep : U64} {ep' : Option ObjRef} + (hep : ∀ j (hj : j < caps.length), + UnwCA ep (capA st s (cslotAddrA (caps[j]).2)) ↔ SUnwA ep' (caps[j]).1) + {info : msginfo.MessageInfo} {mi : MessageInfo} (hml : info.length.bv = mi.miLength) + (hmb : info.label.bv = mi.miLabel) {receiver : U64} (hrt : Z.th receiver = some .tcb) + {recvBuf : U64} {rb : ObjRef} (hb0 : recvBuf ≠ 0#u64) (hal : (bufAddr rb 0).toNat % 8 = 0) + (hbw : ∀ k : U64, k.val < 128 → + (L.mem s (bufW recvBuf k)).bv = loadWordAt σ.machineState.underlyingMemory (bufAddr rb k.val)) + (hbu : ∀ k, k < 128 → inUserFrame (bufAddr rb k) σ) + (hbF : ∀ k : U64, k.val < 128 → ¬ FootA (L := L) Z s (bufW recvBuf k)) + (hbC : ∀ k : U64, k.val < 128 → ¬ InOS OS (bufW recvBuf k)) + (hbA : ∀ k : U64, k.val < 128 → Aligned (bufW recvBuf k)) (hCan : ∀ x ∈ OS, obj.sext48 x = ok x) : + IpcExtraCapTransferA st AR Z m a s σ info mi extra caps ep receiver recvBuf ep' rb + (fun G' s' σ' => TreeA st OS G'.chains G'.par s' ∧ ∀ n, SInvXA σ m n → SInvXA σ' m n) := + WP.spec_mono (transfer_caps_relA (InsBaseA.of_jEntA E hCan) hT0 hsub hnul hX hep hml hmb hrt hb0 hal hbw hbu hbF + hbC hbA (Q := fun _ _ => True) ⟨fun _ _ _ _ _ _ => trivial, fun _ _ _ _ _ _ _ _ => trivial⟩ trivial) fun _ ⟨mi', σ', G', hm, h1, h2, h3, h4, J, T, SI, _, _, _, _⟩ => ⟨mi', σ', G', hm, h1, h2, h3, h4, J, T, SI⟩ + +end + +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/IpcInvokeDispatchA.lean b/proofs/Fl4ProofsA64/P3/IpcInvokeDispatchA.lean new file mode 100644 index 000000000..ba490a93d --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/IpcInvokeDispatchA.lean @@ -0,0 +1,164 @@ +/- +P3 (AArch64): Endpoint, Notification, Reply and SchedContext dispatch. +The IPC success theorem composes the actual caller Restart and the +selected operation. Its [J] continuation is the operation proof at the +post-Restart state. In particular, it retains the local scope of the IPC +operation and does not assume a global scheduler or reply-transfer post. +-/ +import Fl4ProofsA64.P3.ScSetStateA +import Fl4ProofsA64.P3.EpSpecA +import Fl4ProofsA64.P3.DelSpecA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4SpecA64 Sched + +noncomputable section + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {s : S} {a : A} {lbl length slot buf : U64} {block call donate first : Bool} +variable {extra : Array obj.Slot 3#usize} + +/-- The successful IPC dispatch preserves the concrete result of the operation +after setting the caller's state to Restart. -/ +def ipcInvokeProgramA (st : machine.Store S) (s : S) + (body : S → Result (S × A)) : Result (core.result.Result Unit error.Exc × S × A) := do + let t ← sched.cur_thread st s + let s1 ← sched.set_thread_state st s t sched.TS_RESTART + let (s2, a2) ← body s1 + ok (.Ok (), s2, a2) + +theorem decodeEndpoint_programA {p badge : U64} {send recv grant grantReply : Bool} : + syscall.decode_invocation st ar s a lbl length slot (.Endpoint p badge send recv grant grantReply) + block call donate first buf extra = + if send then + ipcInvokeProgramA st s + (fun s1 => endpoint.send_ipc st ar s1 a block call badge grant grantReply donate (L.curW s) p) + else ok (.Err (.Sys (.InvalidCapability 0#u64)), s, a) := by + cases send <;> simp [syscall.decode_invocation, ipcInvokeProgramA, cur_thread_ok, bind_ok] + +theorem decodeNotification_programA {p badge : U64} {send recv : Bool} : + syscall.decode_invocation st ar s a lbl length slot (.Notification p badge send recv) + block call donate first buf extra = + if send then ipcInvokeProgramA st s (fun s1 => notification.send_signal st ar s1 a p badge) + else ok (.Err (.Sys (.InvalidCapability 0#u64)), s, a) := by + cases send <;> simp [syscall.decode_invocation, ipcInvokeProgramA, cur_thread_ok, bind_ok] + +theorem decodeReply_programA {p : U64} {grant : Bool} : + syscall.decode_invocation st ar s a lbl length slot (.Reply p grant) block call donate first buf extra = + ipcInvokeProgramA st s (fun s1 => ipc.do_reply_transfer st ar s1 a (L.curW s) p grant) := by + simp [syscall.decode_invocation, ipcInvokeProgramA, cur_thread_ok, bind_ok] + +theorem decodeSc_programA {p size : U64} : + syscall.decode_invocation st ar s a lbl length slot (.SchedContext p size) + block call donate first buf extra = + if first then ok (.Err (.Sys (.InvalidCapability 0#u64)), s, a) + else schedcontext.decode st ar s a lbl p call extra := by + simp [syscall.decode_invocation, cur_thread_ok, bind_ok] + +/-- Compose the actual Restart and the chosen body. [J] The continuation +relates the body from that exact intermediate state and may carry its +operation-specific IPC, SC, queue, register and write-footprint facts. -/ +theorem ipcInvokeProgram_refinesA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {σ : State Unit} (H : Sched.SchedSRA st Z m κ s σ) (hst : StEqA Z.T (L.mem s) σ) + (ht : L.curW s ∈ Z.T) (hcur : σ.curThread = specPtr (L.curW s)) + {x : Tcb} (hx : σ.kheap σ.curThread = some (.tcb x)) (hr : runnable x.tcbState = true) + {body : S → Result (S × A)} {op : SMonad Unit Unit} {R : S → A → State Unit → Prop} + (step : ∀ s1 σ1, Sched.SchedSRA st Z m κ s1 σ1 → StEqA Z.T (L.mem s1) σ1 → + σ1.kheap = (Sched.updKSc σ (specPtr (L.curW s)) (.tcb { x with tcbState := .restart })).kheap → + WritesIn st s s1 (fun b => Sched.SWrA (st := st) Z s b ∨ b = stA (L.curW s)) → + body s1 ⦃ r => ∃ σ2, ((), σ2) ∈ (op σ1).1 ∧ R r.1 r.2 σ2 ⦄) : + ipcInvokeProgramA st s body ⦃ r => r.1 = .Ok () ∧ ∃ σ1 σ2, + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ((), σ2) ∈ (op σ1).1 ∧ R r.2.1 r.2.2 σ2 ⦄ := by + have code : sched.TS_RESTART.val = tsCodeA .restart := by + rw [ts_vals.2.1]; rfl + rw [hcur] at hx + simp only [ipcInvokeProgramA, cur_thread_ok, bind_ok] + refine WP.spec_bind (Sched.sc_set_runnable_stateA H hst ht hx hr (ts := .restart) rfl code) ?_ + rintro s1 ⟨σ1, hR, HS, hS, hk, hw⟩ + refine WP.spec_bind (step s1 σ1 HS hS hk hw) ?_ + rintro ⟨s2, a2⟩ ⟨σ2, h2, hpost⟩ + exact (WP.spec_ok _).2 ⟨rfl, σ1, σ2, by simpa only [hcur] using hR, h2, hpost⟩ + +/-- A detached reply executes no transfer after the dispatcher has run +the caller's Restart. -/ +theorem replyTransfer_detachedA {t r : U64} {grant : Bool} + (h : obj.Reply.tcb st r s = ok 0#u64) : + ipc.do_reply_transfer st ar s a t r grant = ok (s, a) := by + simp only [ipc.do_reply_transfer, h, bind_ok, obj.Tcb.is_null, decide_true, ↓reduceIte] + +end + +section +variable {σ : State Unit} {lbl : Data} {args : List Data} {ci : CapRef} {sl : CslotPtr} +variable {caps : List (Cap × CslotPtr)} {buffer : Option ObjRef} {call first : Bool} + +theorem decodeEndpoint_specA {p badge : U64} {send recv grant grantReply : Bool} : + (fl4DecodeInvocation call first lbl args ci sl (absCapA (.Endpoint p badge send recv grant grantReply)) + caps buffer).run σ = + if send then ({(.ok (.invokeEndpoint (specPtr p) badge.bv grant grantReply), σ)}, False) + else ({(.error (.invalidCapability 0), σ)}, False) := by + cases send <;> cases grant <;> cases grantReply <;> + simp [fl4DecodeInvocation, absCapA, AllowSend, rightsOfA] <;> rfl + +theorem decodeNotification_specA {p badge : U64} {send recv : Bool} : + (fl4DecodeInvocation call first lbl args ci sl (absCapA (.Notification p badge send recv)) + caps buffer).run σ = + if send then ({(.ok (.invokeNotification (specPtr p) badge.bv), σ)}, False) + else ({(.error (.invalidCapability 0), σ)}, False) := by + cases send <;> simp [fl4DecodeInvocation, absCapA, AllowSend, rightsOfA] <;> rfl + +theorem decodeReply_specA {p : U64} {grant : Bool} : + (fl4DecodeInvocation call first lbl args ci sl (absCapA (.Reply p grant)) caps buffer).run σ = + ({(.ok (.invokeReply (specPtr p) grant), σ)}, False) := by + cases grant <;> simp [fl4DecodeInvocation, absCapA, replyRightsA] <;> rfl + +theorem decodeSc_specA {p : ObjRef} {size : Nat} : + fl4DecodeInvocation (Z := Unit) call first lbl args ci sl (.schedContextCap p size) caps buffer = + if first then Fl4Spec.NonDetE.throwError (.invalidCapability 0) else + Fl4Spec.NonDetE.liftME Fl4Invocation.invokeSchedContext + (decodeSchedContextInvocation (specLabelWord (fl4LabelOf lbl)) p (caps.map Prod.fst)) := rfl + +/-- Lift the selected MCS arm's result through the outer cap-kind decoder. +This applies to each of the ratified Bind, Unbind, Consumed and YieldTo +lemmas with its own post, including any reply produced by the arm. -/ +theorem decodeSc_okA {p : ObjRef} {size : Nat} {inv : SchedContextInvocation} + (h : (decodeSchedContextInvocation (Z := Unit) (specLabelWord (fl4LabelOf lbl)) + p (caps.map Prod.fst)).run σ = ({(.ok inv, σ)}, False)) : + (fl4DecodeInvocation call false lbl args ci sl (.schedContextCap p size) caps buffer).run σ = + ({(.ok (.invokeSchedContext inv), σ)}, False) := by + rw [decodeSc_specA] + change (Fl4Spec.NonDetE.liftME Fl4Invocation.invokeSchedContext + (decodeSchedContextInvocation (Z := Unit) (specLabelWord (fl4LabelOf lbl)) + p (caps.map Prod.fst))).run σ = _ + unfold Fl4Spec.NonDetE.liftME + rw [run_bind_okD h] + rfl + +theorem decodeSc_errorA {p : ObjRef} {size : Nat} {e : SyscallError} + (h : (decodeSchedContextInvocation (Z := Unit) (specLabelWord (fl4LabelOf lbl)) + p (caps.map Prod.fst)).run σ = ({(.error e, σ)}, False)) : + (fl4DecodeInvocation call false lbl args ci sl (.schedContextCap p size) caps buffer).run σ = + ({(.error e, σ)}, False) := by + rw [decodeSc_specA] + change (Fl4Spec.NonDetE.liftME Fl4Invocation.invokeSchedContext + (decodeSchedContextInvocation (Z := Unit) (specLabelWord (fl4LabelOf lbl)) + p (caps.map Prod.fst))).run σ = _ + unfold Fl4Spec.NonDetE.liftME + exact run_bind_errorD h _ + +theorem replyTransfer_detached_specA {t r : ObjRef} {grant : Bool} {x : Reply} + (hk : σ.kheap r = some (.reply x)) (h : x.replyTcb = none) : + doReplyTransfer (Z := Unit) t r grant σ = ({((), σ)}, False) := by + unfold doReplyTransfer + rw [Sched.nd_bind_oneSc (Endp.getReplyTcb_eq hk), h] + rfl + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/IpcRecvSlotA.lean b/proofs/Fl4ProofsA64/P3/IpcRecvSlotA.lean new file mode 100644 index 000000000..c3b9e09aa --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/IpcRecvSlotA.lean @@ -0,0 +1,150 @@ +/- +P3 (aarch64, Stage-3 `IpcExtraCapTransferA` R2 item 1): **FL4's `get_receive_slot` against l4v +`get_receive_slots`** (the concrete twin of `EpXferA.getReceiveSlots_oneA`). + +* `RecvSlotRelA`: FL4's receive slot `d` against the spec's list: no slot is `0`, one slot `[x]` is the + slot `cslotAddrA x` of `th`, which holds the null cap. +* `emptyOnFailure_errA` / `emptyOnFailure_okA`: `empty_on_failure` of a one-outcome computation. +* **`get_receive_slot_relA`**: at a `StateRelA`-related state with `SlotsValidA`, a nonzero receive buffer word + whose words are the spec's buffer words (`EpStateRel.buf`), `get_receive_slot` returns `d`, the spec's + `get_receive_slots t (Some rb)` has one outcome `sl` and keeps the state, and `RecvSlotRelA d sl`. The root + lookup is `lookup_slot_refinesA`, the target `lookup_slot_for_cnode_op_refinesA false`, the cap + `StateRelA.slot_capA`; a returned slot is nonzero by `PlaceA.slot_ne0`. + +Case hypotheses only; no named premise. New module; no existing statement changed. +-/ +import Fl4ProofsA64.P3.EpXferA +import Fl4ProofsA64.P3.CnodeCopyA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4SpecA64 + +noncomputable section + +/-- FL4's receive slot against the spec's receive-slot list (module docstring). -/ +def RecvSlotRelA (th : TypedHeapA) (kb : U64) (mem : U64 → U64) (d : U64) (sl : List CslotPtr) : Prop := + (sl = [] ∧ d = 0#u64) ∨ ∃ x, sl = [x] ∧ d = cslotAddrA x ∧ capAtA mem d = .Null ∧ + ∃ p, x.1 = specPtr p ∧ th p = some (.cnode x.2.length) ∧ ¬ IrqNodeA kb p + +theorem emptyOnFailure_errA {α F : Type} {m : Fl4Spec.NonDetE (State Unit) F (List α)} {σ : State Unit} {e : F} + (h : m.run σ = ({(.error e, σ)}, False)) : emptyOnFailure (Z := Unit) m σ = ({([], σ)}, False) := by + unfold emptyOnFailure Fl4Spec.NonDetE.catch + rw [nd_bind_one' h] + rfl + +theorem emptyOnFailure_okA {α F : Type} {m : Fl4Spec.NonDetE (State Unit) F (List α)} {σ : State Unit} + {l : List α} (h : m.run σ = ({(.ok l, σ)}, False)) : emptyOnFailure (Z := Unit) m σ = ({(l, σ)}, False) := by + unfold emptyOnFailure Fl4Spec.NonDetE.catch + rw [nd_bind_one' h] + rfl + +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **FL4's `get_receive_slot` is l4v `get_receive_slots`** (module docstring). -/ +theorem get_receive_slot_relA {th : TypedHeapA} {kb : U64} (hPl : PlaceA th kb) (s : S) (σ : State Unit) + (hR : StateRelA th (L.mem s) σ) (hV : SlotsValidA th (L.mem s)) {t : U64} (ht : th t = some .tcb) + {buf : U64} {rb : Word} (hb0 : buf ≠ 0#u64) (hal : (bufAddr rb 0).toNat % 8 = 0) + (hw : ∀ k : U64, k.val < 128 → + (L.mem s (bufW buf k)).bv = loadWordAt σ.machineState.underlyingMemory (bufAddr rb k.val)) : + ∃ d sl, ipc.get_receive_slot st s t buf = ok d ∧ + getReceiveSlots (Z := Unit) (specPtr t) (some rb) σ = ({(sl, σ)}, False) ∧ + RecvSlotRelA th kb (L.mem s) d sl := by + have hal' : ∀ k, k < 128 → (bufAddr rb k).toNat % 8 = 0 := fun k hk => bufAddr_align hal k hk + obtain ⟨c0, hc0, e0⟩ := spec_existsA Fl4ProofsA64.P0.Consts.msg.BUF_RECV_CNODE.spec + obtain ⟨c1, hc1, e1⟩ := spec_existsA Fl4ProofsA64.P0.Consts.msg.BUF_RECV_INDEX.spec + obtain ⟨c2, hc2, e2⟩ := spec_existsA Fl4ProofsA64.P0.Consts.msg.BUF_RECV_DEPTH.spec + subst e0 e1 e2 + have h5 : loadWordAt σ.machineState.underlyingMemory (bufAddr rb 125) = (L.mem s (bufW buf 125#u64)).bv := + (hw 125#u64 (by simp)).symm + have h6 : loadWordAt σ.machineState.underlyingMemory (bufAddr rb 126) = (L.mem s (bufW buf 126#u64)).bv := + (hw 126#u64 (by simp)).symm + have h7 : loadWordAt σ.machineState.underlyingMemory (bufAddr rb 127) = (L.mem s (bufW buf 127#u64)).bv := + (hw 127#u64 (by simp)).symm + unfold ipc.get_receive_slot + rw [if_neg hb0, hc0, bind_ok, buf_get_eqA, bind_ok, hc1, bind_ok, buf_get_eqA, bind_ok, hc2, bind_ok, + buf_get_eqA, bind_ok] + simp only [getReceiveSlots] + rw [nd_bind_one (loadCapTransfer_eqA hal'), h5, h6, h7] + simp only [← bufW_eq_bufWA] + generalize L.mem s (bufW buf 125#u64) = w5 + generalize L.mem s (bufW buf 126#u64) = w6 + generalize L.mem s (bufW buf 127#u64) = w7 + try dsimp only + obtain ⟨r0, hr0, e0, he0, hrel0⟩ := spec_existsA (lookup_slot_refinesA th s σ hR hV t ht w5) + have he0' : (lookupSlotForThread (Z := Unit) (specPtr t) (dataToCptr w5.bv)).run σ = ({(e0, σ)}, False) := he0 + unfold cspace.lookup_cap + rw [hr0, bind_ok] + cases e0 with + | error lf => + cases r0 with + | Ok _ => exact hrel0.1.elim + | Err lf' => + have h1 : (lookupCap (Z := Unit) (specPtr t) (dataToCptr w5.bv)).run σ = ({(.error lf, σ)}, False) := by + unfold lookupCap; exact run_bind_errorA he0' _ + have hu1 : (unifyFailure (Z := Unit) (lookupCap (Z := Unit) (specPtr t) (dataToCptr w5.bv))).run σ = + ({(.error (), σ)}, False) := by + unfold unifyFailure; rw [run_handleE_errA h1]; rfl + exact ⟨0#u64, [], by simp, emptyOnFailure_errA (run_bind_errorA hu1 _), Or.inl ⟨rfl, rfl⟩⟩ + | ok v => + obtain ⟨sl, rest⟩ := v + cases r0 with + | Err _ => exact hrel0.1.elim + | Ok r1 => + obtain ⟨⟨hsl, -⟩, p, hp1, hp, -⟩ := hrel0 + obtain ⟨a, hca, hg⟩ := hR.slot_capA hp1 hp + have hcv : CapValidA th (capAtA (L.mem s) (cslotAddrA sl)) := hV _ (AbsSlotA.isSlot hPl ⟨p, hp1, Or.inl hp⟩) + have h1 : (lookupCap (Z := Unit) (specPtr t) (dataToCptr w5.bv)).run σ = ({(.ok a, σ)}, False) := by + unfold lookupCap; rw [run_bind_okA he0']; exact run_liftEA hg + have hu1 : (unifyFailure (Z := Unit) (lookupCap (Z := Unit) (specPtr t) (dataToCptr w5.bv))).run σ = + ({(.ok a, σ)}, False) := run_handleE_okA h1 + simp only [bind_ok] + rw [hsl, W7.R.ST.capA_eq, bind_ok] + obtain ⟨r2, hr2, e1, he1, hrel1⟩ := spec_existsA (lookup_slot_for_cnode_op_refinesA th s σ hR hV false _ a hca + hcv w6 w7) + have he1' : (lookupTargetSlot (Z := Unit) a (dataToCptr w6.bv) (unat w7.bv)).run σ = ({(e1, σ)}, False) := he1 + unfold cspace.lookup_target_slot + simp only [bind_ok] + first | rw [hr2] | rw [← capAtA_eq, hr2] | rw [capAtA_eq] at hr2; rw [hr2] + simp only [bind_ok] + cases e1 with + | error e' => + cases r2 with + | Ok _ => exact hrel1.elim + | Err _ => + have hu2 : (unifyFailure (Z := Unit) (lookupTargetSlot (Z := Unit) a (dataToCptr w6.bv) (unat w7.bv))).run σ = + ({(.error (), σ)}, False) := by + unfold unifyFailure; rw [run_handleE_errA he1']; rfl + exact ⟨0#u64, [], by simp, emptyOnFailure_errA ((run_bind_okA hu1 _).trans (run_bind_errorA hu2 _)), + Or.inl ⟨rfl, rfl⟩⟩ + | ok sl2 => + cases r2 with + | Err _ => exact hrel1.elim + | Ok d => + obtain ⟨hd, p2, h21, h22, hcr⟩ := hrel1 + subst hd + obtain ⟨a2, hca2, hg2⟩ := hR.slot_capA h21 h22 + have hu2 : (unifyFailure (Z := Unit) (lookupTargetSlot (Z := Unit) a (dataToCptr w6.bv) (unat w7.bv))).run σ = + ({(.ok sl2, σ)}, False) := run_handleE_okA he1' + simp only [bind_ok, W7.R.ST.capA_eq] + by_cases hn : capAtA (L.mem s) (cslotAddrA sl2) = .Null + · have ha2 : a2 = .nullCap := by + have h := hca2.2; rw [hn] at h; exact h.symm + rw [← capAtA_eq, hn, cap_is_null_nullA, bind_ok, if_pos rfl] + refine ⟨_, [sl2], rfl, emptyOnFailure_okA (((run_bind_okA hu1 _).trans ((run_bind_okA hu2 _).trans + (run_bind_okA (run_liftEA hg2) _))).trans ?_), Or.inr ⟨sl2, rfl, rfl, hn, p2, h21, h22, + noIrq_of_cnRefA hPl hV hcv hcr⟩⟩ + rw [ha2] + simp only [Fl4Spec.NonDetE.whenE, ne_eq, not_true_eq_false, ↓reduceIte] + exact (run_bind_okA (show (pure () : Fl4Spec.NonDetE (State Unit) Unit Unit).run σ = + ({(.ok (), σ)}, False) from rfl) _).trans rfl + · have ha2 : a2 ≠ .nullCap := hca2.ne_null hn + rw [← capAtA_eq, cap_is_null_neA hn, bind_ok, if_neg (by simp)] + refine ⟨0#u64, [], rfl, emptyOnFailure_errA (e := ()) (((run_bind_okA hu1 _).trans ((run_bind_okA hu2 _).trans + (run_bind_okA (run_liftEA hg2) _))).trans ?_), Or.inl ⟨rfl, rfl⟩⟩ + simp only [Fl4Spec.NonDetE.whenE, ne_eq, ha2, not_false_eq_true, ↓reduceIte] + exact run_bind_errorA (run_throwErrorA _ σ) _ + +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/IpcScBootA.lean b/proofs/Fl4ProofsA64/P3/IpcScBootA.lean new file mode 100644 index 000000000..40fc53d6e --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/IpcScBootA.lean @@ -0,0 +1,493 @@ +/- +P3 (AArch64): the scheduler's boot-state witnesses for the IPC join. +The S3 branch adds the idle-SC refill list [(now, mt), (now, 0)] to both +boot states. The Stage-3 merge makes the adapters below equal to S3's boot states. The root SC at w1 keeps its own time slice. +-/ +import Fl4ProofsA64.P3.ScBootA +import Fl4ProofsA64.P2.BootVt +import Fl4ProofsA64.P3.IpcScReplyA + +open Aeneas Aeneas.Std +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P1.W7.R.ST.BootWit Fl4SpecA64 Sched +noncomputable section + +/-- The w0 state with S3's two-refill idle SCs. -/ +def w0SchedStateA (kb ib : U64) (nc : Nat) (now mt : U64) : State Unit := + bootSA kb ib nc now mt + +/-- The w1 state with S3's two-refill idle SCs and the root SC's own time slice. -/ +def w1SchedStateA (kb ib : U64) (nc : Nat) (now mt sl : U64) : State Unit := + w1SA kb ib nc now mt sl + +private theorem bootHeapScNameA {kb ib now mt : U64} {nc : Nat} (hL : BootLayA ib nc) + {p : ObjRef} {x : SchedContext} {n : Nat} (hk : bootKA kb ib nc now mt p = some (.schedContext x n)) : + ∃ c ∈ bootCA ib nc, p = scNameA (bootMA ib nc) c := by + unfold bootKA at hk + split_ifs at hk with e _ hsc _ + · exact ⟨idleSA ib 0, ⟨0, hL.nc0, rfl⟩, e.trans (bootRen0 hL).symm⟩ + · cases hk + · obtain ⟨c, hp, hc, e⟩ := hsc + exact ⟨idleSA ib c, ⟨c, hc, rfl⟩, e.trans (bootRen hL hp hc).symm⟩ + · cases hk + +/-- All spec SCs in the w0 heap are among the idle SCs in the scheduler ghosts. -/ +theorem w0_scCovA {kb ib now mt : U64} {nc : Nat} (hL : BootLayA ib nc) : + ScCovA (bootZA kb ib nc).C (bootMA ib nc) (w0SchedStateA kb ib nc now mt) := by + intro p x n hk + exact bootHeapScNameA hL hk + +/-- All spec SCs in the w1 heap are named by the idle or root SC ghosts. -/ +theorem w1_scCovA {kb ib now mt sl : U64} {nc : Nat} (hL : BootLayA ib nc) (P : BootLay1 kb ib nc) : + ScCovA (w1ZA kb ib nc).C (w1MA ib nc) (w1SchedStateA kb ib nc now mt sl) := by + intro p x n hk + change w1KA kb ib nc now mt sl p = _ at hk + unfold w1KA at hk + split_ifs at hk with _ _ er _ _ _ + · cases hk + · cases hk + · have ep : p = specPtr (bwv SCb) := by + rw [← specPtr_fl4Ptr p] + exact congrArg specPtr (UScalar.eq_of_val_eq (er.trans vSC.symm)) + exact ⟨bwv SCb, w1_rsc, ep.trans (w1Ren_SC hL P).symm⟩ + · cases hk + · cases hk + · cases hk + · obtain ⟨c, hc, ep⟩ := bootHeapScNameA hL hk + have hc1 : c ∈ (bootH kb ib nc).scs := by rw [← bootCA_eq hL]; exact hc + exact ⟨c, (w1_idleS P hc1).1, ep⟩ + +private def bootScValueA (t now budget : U64) : SchedContext := + { defaultSchedContext with + scTcb := some (specPtr t) + scRefills := [⟨now.bv, budget.bv⟩, ⟨now.bv, 0⟩] + scRefillMax := 2 } + +private def bootScWordA (t now budget : U64) (k : Nat) : U64 := + if k = 24 then t else if k = 72 then 2#u64 else if k = 88 then 1#u64 + else if k = 96 ∨ k = 112 then now else if k = 104 then budget else 0#u64 + +section BootScRelation +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +private theorem bootRefillAddrA (sc : U64) : + rtA sc 0#u64 = scWA sc 96#u64 ∧ raA sc 0#u64 = scWA sc 104#u64 ∧ + rtA sc 1#u64 = scWA sc 112#u64 ∧ raA sc 1#u64 = scWA sc 120#u64 := by + simp only [rtA, raA, scWA, layout.sc.REFILLS, layout.sc.REFILL_SIZE] + <;> apply_rules [And.intro] <;> apply UScalar.eq_of_val_eq <;> + simp [core.num.U64.wrapping_add_val_eq, core.num.U64.wrapping_mul_val_eq, U64.size_def, U64.numBits, + Nat.mod_add_mod] + +private theorem bootRefillsA {s : S} {sc t now budget : U64} + (words : ∀ K : U64, K.val < 128 → K.val ≠ 16 → L.mem s (scWA sc K) = bootScWordA t now budget K.val) + (hm : L.rmax s sc = 2#u64) (hh : L.rhead s sc = 0#u64) (ht : L.rtail s sc = 1#u64) : + (L.refills s sc).map toRefillA = (bootScValueA t now budget).scRefills := by + have hsize : L.size s sc = 2 := by rw [LawfulStore.size, hm, hh, ht]; decide + have ix0 : L.ix s sc 0 = 0#u64 := by rw [LawfulStore.ix, hm, hh]; rfl + have ix1 : L.ix s sc 1 = 1#u64 := by rw [LawfulStore.ix, hm, hh]; rfl + have a := bootRefillAddrA sc + have h0 : L.refill s sc 0#u64 = ⟨now, budget⟩ := by + unfold LawfulStore.refill + rw [a.1, a.2.1, words 96#u64 (by decide) (by decide), words 104#u64 (by decide) (by decide)] + rfl + have h1 : L.refill s sc 1#u64 = ⟨now, 0#u64⟩ := by + unfold LawfulStore.refill + rw [a.2.2.1, a.2.2.2, words 112#u64 (by decide) (by decide), words 120#u64 (by decide) (by decide)] + rfl + rw [LawfulStore.refills, hsize] + change [toRefillA (L.rf s sc 0), toRefillA (L.rf s sc 1)] = _ + rw [LawfulStore.rf, ix0, LawfulStore.rf, ix1, h0, h1] + rfl + +private theorem bootScValueRelA {s : S} {sc t now budget : U64} {n : Nat} {idle : Prop} + (pl : Placed sc (n + minSchedContextBits)) (ht : t ≠ 0#u64) + (words : ∀ K : U64, K.val < 128 → K.val ≠ 16 → L.mem s (scWA sc K) = bootScWordA t now budget K.val) : + ScRelWA st idle s sc (bootScValueA t now budget) n := by + have hm : L.rmax s sc = 2#u64 := by rw [LawfulStore.rmax, maxA_eqS, words 72#u64 (by decide) (by decide)]; rfl + have hh : L.rhead s sc = 0#u64 := by rw [LawfulStore.rhead, headA_eqS, words 80#u64 (by decide) (by decide)]; rfl + have hl : L.rtail s sc = 1#u64 := by rw [LawfulStore.rtail, tailA_eqS, words 88#u64 (by decide) (by decide)]; rfl + have W : RefillsWF st s sc (n + minSchedContextBits) := by + refine ⟨pl, ?_, ?_, ?_, ?_⟩ + · rw [hm]; unfold sporadic.MIN_REFILLS; decide + · rw [hm] + have b : 2 ^ 7 ≤ 2 ^ (n + minSchedContextBits) := Nat.pow_le_pow_right (by decide) (by unfold minSchedContextBits; omega) + rw [absMax, if_neg (Nat.not_le_of_lt pl.lt64)] + change 2 ≤ (2 ^ (n + minSchedContextBits) - 96) / 16 + omega + · rw [hh, hm]; decide + · rw [hl, hm]; decide + have p : (bootScValueA t now budget).scPeriod = (L.rperiod s sc).bv := by + rw [LawfulStore.rperiod, periodA_eqS, words 0#u64 (by decide) (by decide)]; rfl + have c : (bootScValueA t now budget).scConsumed = (L.mem s (scWA sc 8#u64)).bv := by rw [words 8#u64 (by decide) (by decide)]; rfl + have tb : (bootScValueA t now budget).scTcb = optRefS (L.mem s (scWA sc 24#u64)) := by + rw [words 24#u64 (by decide) (by decide)]; change some (specPtr t) = optRefS t + simp [optRefS, ht] + have rp : (bootScValueA t now budget).scReplies.head? = optRefS (L.mem s (scWA sc 32#u64)) := by + rw [words 32#u64 (by decide) (by decide)]; rfl + have nt : (bootScValueA t now budget).scNtfn = optRefS (L.mem s (scWA sc 40#u64)) := by rw [words 40#u64 (by decide) (by decide)]; rfl + have ba : (bootScValueA t now budget).scBadge = (L.mem s (scWA sc 48#u64)).bv := by rw [words 48#u64 (by decide) (by decide)]; rfl + have yf : (bootScValueA t now budget).scYieldFrom = optRefS (L.mem s (scWA sc 56#u64)) := by + rw [words 56#u64 (by decide) (by decide)]; rfl + have sp : (bootScValueA t now budget).scSporadic = decide (L.mem s (scWA sc 64#u64) ≠ 0#u64) := by + rw [words 64#u64 (by decide) (by decide)]; rfl + have ma : (L.rmax s sc).val = (bootScValueA t now budget).scRefillMax := by rw [hm]; rfl + have ha : (L.rmax s sc).val ≠ 0 := by rw [hm]; decide + exact ⟨pl, p, c, tb, rp, nt, ba, yf, sp, fun _ => ha, fun _ => ha, ma, + fun _ => ⟨W, p, c, tb, rp, nt, ba, yf, sp, ma, (bootRefillsA words hm hh hl).symm⟩⟩ + +end BootScRelation + +section BootWitness +variable {kb ib r now mt sl : U64} {nc : Nat} {uw : U64 → U64} + +private theorem idleBootScPlacedA (hL : BootLayA ib nc) {c : Nat} (hc : c < nc) : + Placed (idleSA ib c) (0 + minSchedContextBits) := by + have ha := hL.al + have hf := hL.fit + refine ⟨by decide, ?_, ?_⟩ <;> rw [idleSA_val hL hc] + · change _ % 128 = 0 + omega + · change ib.val + 8192 * c + 4096 + 128 ≤ 2 ^ 64 + omega + +private theorem idleBootWordsA (P : BootLay kb ib nc) (hL : BootLayA ib nc) {c : Nat} (hc : c < nc) + (K : U64) (hK : K.val < 128) (hcore : K.val ≠ 16) : + bL.mem (bootS kb ib nc r now mt) (scWA (idleSA ib c) K) = + bootScWordA (idleTA ib c) now mt K.val := by + change bootM kb ib nc r now mt (core.num.U64.wrapping_add (idleSA ib c) K) = _ + rw [scM P hc (idleSA_val' hL hc) (by omega)] + by_cases h24 : K.val = 24 + · rw [scW, bootScWordA, if_neg hcore, if_pos h24, if_pos h24] + apply UScalar.eq_of_val_eq + rw [idleTA_val hL hc, bwv_val (by have := hL.fit; omega)] + omega + · simp only [scW, bootScWordA, if_neg hcore, if_neg h24] + by_cases h96 : K.val = 96 <;> by_cases h112 : K.val = 112 <;> + simp [h96, h112] + +private theorem bootIdleHeapA (hL : BootLayA ib nc) {c : Nat} (hc : c < nc) : + bootKA kb ib nc now mt (scNameA (bootMA ib nc) (idleSA ib c)) = + some (.schedContext (idleScObjA now mt (specPtr (idleTA ib c))) 0) := by + rcases Nat.eq_zero_or_pos c with rfl | hp + · rw [show scNameA (bootMA ib nc) (idleSA ib 0) = idleScPtr from bootRen0 hL] + exact bootKA_sc0 hL + · rw [show scNameA (bootMA ib nc) (idleSA ib c) = specPtr (idleSA ib c) from bootRen hL hp hc] + exact bootKA_sc hL hp hc + +theorem w0_scStateRelA (P : BootLay kb ib nc) (hL : BootLayA ib nc) : + ScStateRelA bst (bootZA kb ib nc).C (bootMA ib nc) (bootS kb ib nc r now mt) (w0SchedStateA kb ib nc now mt) := by + constructor + rintro _ ⟨c, hc, rfl⟩ + refine ⟨bootScValueA (idleTA ib c) now mt, 0, ?_, + bootScValueRelA (idleBootScPlacedA hL hc) (idleTA_ne0 hL hc) (idleBootWordsA P hL hc)⟩ + change bootKA kb ib nc now mt _ = _ + rw [bootIdleHeapA hL hc] + rfl + +theorem w1_scStateRelA (P : BootLay1 kb ib nc) (hL : BootLayA ib nc) + (hnn : fl4A64Platform.numNodes ≤ nc) : + ScStateRelA bst (w1ZA kb ib nc).C (w1MA ib nc) (w1S kb ib nc r now mt sl uw) + (w1SchedStateA kb ib nc now mt sl) := by + constructor + intro sc hsc + rcases w1Sc P hsc with ⟨hI, _⟩ | ⟨rfl, _⟩ + · obtain ⟨c, hc, rfl⟩ := bootC_of hL hI + have word (K : U64) (hK : K.val < 128) (hcore : K.val ≠ 16) : + bL.mem (w1S kb ib nc r now mt sl uw) (scWA (idleSA ib c) K) = + bootScWordA (idleTA ib c) now mt K.val := by + rw [show scWA (idleSA ib c) K = core.num.U64.wrapping_add (idleSA ib c) K from rfl, w1_sI P hI (by omega)] + exact idleBootWordsA P.w0 hL hc K hK hcore + refine ⟨bootScValueA (idleTA ib c) now mt, 0, ?_, + bootScValueRelA (idleBootScPlacedA hL hc) (idleTA_ne0 hL hc) word⟩ + change w1KA kb ib nc now mt sl _ = _ + rcases Nat.eq_zero_or_pos c with rfl | hp + · rw [show scNameA (w1MA ib nc) (idleSA ib 0) = idleScPtr from bootRen0 hL, w1KA_sc0 hL] + all_goals rfl + · rw [show scNameA (w1MA ib nc) (idleSA ib c) = specPtr (idleSA ib c) from bootRen hL hp hc, + w1KA_scc hL P hp hc] + have e : specPtr (bwv SCb) ≠ specPtr (idleSA ib c) := by + intro he + have hh := congrArg UScalar.val (specPtr_inj he) + rw [vSC, idleSA_val hL hc] at hh + have := P.ibR + omega + all_goals rfl + · have word (K : U64) (hK : K.val < 128) (_ : K.val ≠ 16) : + bL.mem (w1S kb ib nc r now mt sl uw) (scWA (bwv SCb) K) = + bootScWordA (bwv RTb) now sl K.val := by + rw [show scWA (bwv SCb) K = core.num.U64.wrapping_add (bwv SCb) K from rfl, w1_sR P hK] + by_cases h96 : K.val = 96 <;> by_cases h112 : K.val = 112 <;> + simp [rscW, bootScWordA, h96, h112] + have pl : Placed (bwv SCb) (0 + minSchedContextBits) := by + refine ⟨by decide, ?_, ?_⟩ <;> rw [vSC] <;> decide + have ht : bwv RTb ≠ 0#u64 := fun e => by have z := congrArg UScalar.val e; rw [vRT] at z; contradiction + refine ⟨rootScA now sl, 0, ?_, bootScValueRelA pl ht word⟩ + change w1KA kb ib nc now mt sl _ = _ + rw [show scNameA (w1MA ib nc) (bwv SCb) = specPtr (bwv SCb) from w1Ren_SC hL P, w1KA_sc] + +private theorem w1IdleHeapA (P : BootLay1 kb ib nc) (hL : BootLayA ib nc) {c : Nat} (hc : c < nc) : + w1KA kb ib nc now mt sl (scNameA (w1MA ib nc) (idleSA ib c)) = + some (.schedContext (idleScObjA now mt (specPtr (idleTA ib c))) 0) := by + rcases Nat.eq_zero_or_pos c with rfl | hp + · rw [show scNameA (w1MA ib nc) (idleSA ib 0) = idleScPtr from bootRen0 hL] + exact w1KA_sc0 hL + · rw [show scNameA (w1MA ib nc) (idleSA ib c) = specPtr (idleSA ib c) from bootRen hL hp hc] + exact w1KA_scc hL P hp hc + +private theorem rootScNameNeIdleA (P : BootLay1 kb ib nc) (hL : BootLayA ib nc) + (hnn : fl4A64Platform.numNodes ≤ nc) {c : Nat} (hc : c < nc) : + scNameA (w1MA ib nc) (idleSA ib c) ≠ scNameA (w1MA ib nc) (bwv SCb) := by + rw [show scNameA (w1MA ib nc) (bwv SCb) = specPtr (bwv SCb) from w1Ren_SC hL P] + rcases Nat.eq_zero_or_pos c with rfl | hp + · rw [show scNameA (w1MA ib nc) (idleSA ib 0) = idleScPtr from bootRen0 hL] + exact (w1_rootSc_notIdleA hL P hnn).1.symm + · rw [show scNameA (w1MA ib nc) (idleSA ib c) = specPtr (idleSA ib c) from bootRen hL hp hc] + intro e + have hv := congrArg UScalar.val (specPtr_inj e) + rw [vSC, idleSA_val hL hc] at hv + have := P.ibR + omega + +private theorem w1BootScRangeA (P : BootLay1 kb ib nc) (hL : BootLayA ib nc) + {c a : U64} (hc : c ∈ (w1H kb ib nc).scs) + {x : SchedContext} {n : Nat} + (hk : (w1SA kb ib nc now mt sl).kheap (scNameA (w1MA ib nc) c) = some (.schedContext x n)) + (ha : ScObjA c n a) : + (∃ j < nc, c = idleSA ib j ∧ c.val ≤ a.val ∧ a.val < c.val + 128) ∨ + (c = bwv SCb ∧ SCb ≤ a.val ∧ a.val < SCb + 128) := by + change w1KA kb ib nc now mt sl _ = _ at hk + rcases w1Sc P hc with ⟨hI, _⟩ | ⟨rfl, _⟩ + · obtain ⟨j, hj, rfl⟩ := bootC_of hL hI + rw [w1IdleHeapA P hL hj] at hk + cases hk + exact Or.inl ⟨j, hj, rfl, ha.1, ha.2⟩ + · rw [show scNameA (w1MA ib nc) (bwv SCb) = specPtr (bwv SCb) from w1Ren_SC hL P, w1KA_sc] at hk + cases hk + unfold ScObjA at ha + rw [vSC] at ha + exact Or.inr ⟨rfl, ha.1, ha.2⟩ + +private theorem w1BootScPlacedA (P : BootLay1 kb ib nc) (hL : BootLayA ib nc) (hK : BootKsA kb ib nc) + (hnn : fl4A64Platform.numNodes ≤ nc) : + ScPlacedA (w1ZA kb ib nc).th kb (w1ZA kb ib nc).C (w1MA ib nc) (w1SA kb ib nc now mt sl) := by + have old := boot_scPlacedA (now := now) (mt := mt) hL hK + have ibR := P.ibR + have kbR := P.kbR + constructor + · intro c hc d hd e + rcases w1Sc P hc with ⟨hc0, _⟩ | ⟨rfl, _⟩ <;> + rcases w1Sc P hd with ⟨hd0, _⟩ | ⟨rfl, _⟩ + · rw [← bootCA_eq hL] at hc0 hd0 + exact old.inj c hc0 d hd0 e + · obtain ⟨j, hj, rfl⟩ := bootC_of hL hc0 + exact (rootScNameNeIdleA P hL hnn hj e).elim + · obtain ⟨j, hj, rfl⟩ := bootC_of hL hd0 + exact (rootScNameNeIdleA P hL hnn hj e.symm).elim + · rfl + · intro c hc d hd hdif x n y k hx hy a ha hb + obtain ⟨i, hi, rfl, lo, up⟩ | ⟨rfl, lo, up⟩ := w1BootScRangeA P hL hc hx ha + all_goals obtain ⟨j, hj, rfl, lo', up'⟩ | ⟨rfl, lo', up'⟩ := w1BootScRangeA P hL hd hy hb + · rw [idleSA_val hL hi] at lo up + rw [idleSA_val hL hj] at lo' up' + have ne : i ≠ j := fun eq => hdif (by rw [eq]) + omega + · rw [idleSA_val hL hi] at lo up + omega + · rw [idleSA_val hL hj] at lo' up' + omega + · exact hdif rfl + · intro c hc x n hk a ha + obtain ⟨i, hi, rfl, lo, up⟩ | ⟨rfl, lo, up⟩ := w1BootScRangeA P hL hc hk ha + · have ha0 : ScObjA (idleSA ib i) 0 a := ⟨lo, up⟩ + obtain ⟨hOld, hKsb⟩ := old.out _ ⟨i, hi, rfl⟩ _ _ (bootIdleHeapA hL hi) a ha0 + rw [idleSA_val hL hi] at lo up + refine ⟨?_, hKsb⟩ + intro p k hp + rcases w1ThA_cases hp with ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨hF, rfl⟩ | h + · exact Or.inl (by rw [vRR]; omega) + · exact Or.inl (by rw [vRT]; omega) + · exact Or.inl (by unfold FrPg at hF; omega) + · exact hOld p k h + · refine ⟨?_, Or.inr (by unfold ksSizeA; omega)⟩ + intro p k hp + rcases w1ThA_cases hp with ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨hF, rfl⟩ | h + · right + rw [vRR] + change RRb + 262144 ≤ _ + omega + · right + rw [vRT] + change RTb + 4096 ≤ _ + omega + · simp only [ObjKindA.size] + unfold FrPg at hF + omega + · right + rcases bootTh_loc hL hK h with ⟨j, hj, _, hv, _, hs⟩ | ⟨_, q, hq, hv, _, hs⟩ + · rw [hv, hs]; omega + · rw [hv, hs]; omega + +private theorem w1BootScCaseA {p : ObjRef} {x : SchedContext} {n : Nat} + (hk : w1KA kb ib nc now mt sl p = some (.schedContext x n)) : + (p = specPtr (bwv SCb) ∧ x = rootScA now sl ∧ n = 0) ∨ + bootKA kb ib nc now mt p = some (.schedContext x n) := by + unfold w1KA at hk + split_ifs at hk with _ _ he _ _ _ + · cases hk + · cases hk + · cases hk + refine Or.inl ⟨?_, rfl, rfl⟩ + rw [← specPtr_fl4Ptr p] + exact congrArg specPtr (UScalar.eq_of_val_eq (he.trans vSC.symm)) + · cases hk + · cases hk + · cases hk + · exact Or.inr hk + +private theorem idleNotInClassA (p : ObjRef) : + ¬ inClassRSc (idleTcbA p).tcbState (idleTcbA p).tcbFault := by + simp [inClassRSc, idleTcbA, runnable] + +private theorem w0RefillReadyA (hL : BootLayA ib nc) : + RdA (w0SchedStateA kb ib nc now mt) := by + intro u x hx hrun + have ho := hx + change bootKA kb ib nc now mt u = _ at ho + rcases bootKA_cases hL ho with ⟨_, _, _, p, he⟩ | ⟨_, he⟩ | he + · cases he + exact (idleNotInClassA p hrun).elim + · cases he + · cases he + +private theorem w1RootRefillReadyA (hsl : MIN_BUDGET ≤ sl.bv) : + releasedSc (w1SchedStateA kb ib nc now mt sl) (rootScA now sl) := by + constructor + · change decide (now.bv + 0 ≤ now.bv) = true + simp + · change refillSufficient 0 ⟨now.bv, sl.bv⟩ = true + simp [refillSufficient, refillCapacity, hsl] + +private theorem w1RefillReadyA (hL : BootLayA ib nc) (hsl : MIN_BUDGET ≤ sl.bv) : + RdA (w1SchedStateA kb ib nc now mt sl) := by + intro u x hx hr x' p y n hx' hp hy _ _ + have ho := hx + change w1KA kb ib nc now mt sl u = _ at ho + rcases w1KA_cases hL ho with he | he | he | he | ⟨_, he⟩ | he | ⟨_, ⟨_, _, _, q, he⟩ | ⟨_, he⟩ | he⟩ + all_goals try (cases he; done) + · cases he + rw [hx] at hx' + cases hx' + have eh : some (specPtr (bwv SCb)) = some p := hp + cases eh + have ho : (w1SA kb ib nc now mt sl).kheap (specPtr (bwv SCb)) = some (.schedContext (rootScA now sl) 0) := w1KA_sc + unfold w1SchedStateA at hy + rw [ho] at hy + cases hy + exact w1RootRefillReadyA hsl + · cases he + exact (idleNotInClassA q hr).elim + +/-- The constant SC-to-core map for the boot idle area. -/ +def bootSchedScCoreA (ib sc : U64) : U64 := bwv ((sc.val - ib.val) / 8192) + +/-- The w1 root scheduling context is on core zero. -/ +def w1SchedScCoreA (ib sc : U64) : U64 := + if sc = bwv SCb then 0#u64 else bootSchedScCoreA ib sc + +private theorem idleBootScCoreA (P : BootLay kb ib nc) (hL : BootLayA ib nc) {j : Nat} (hj : j < nc) : + bL.mem (bootS kb ib nc r now mt) (scCoreA (idleSA ib j)) = bootSchedScCoreA ib (idleSA ib j) := by + have mid : idleSA ib j = bwv (ib.val + j * 8192 + 4096) := by + apply UScalar.eq_of_val_eq + rw [idleSA_val hL hj, idS_v P hj] + omega + calc + _ = bwv j := by rw [mid]; exact scCore_v P hj + _ = bootSchedScCoreA ib (idleSA ib j) := by + unfold bootSchedScCoreA + rw [idleSA_val hL hj] + exact congrArg bwv (by omega) + +/-- The w0 scheduler relation and SC heap coverage, with S3's boot refills. -/ +theorem w0_schedWitnessA (P : BootLay kb ib nc) + (hsc : 18446603336221208576 < ib.val ∨ ib.val + nc * 8192 ≤ 18446603336221208576) + (hsk : 18446603336221208576 < kb.val ∨ kb.val + 788736 ≤ 18446603336221208576) + (hnc : nc ≤ fl4A64Platform.numNodes) : + SchedSRA bst (bootZA kb ib nc) (bootMA ib nc) (bootSchedScCoreA ib) + (bootS kb ib nc r now mt) (w0SchedStateA kb ib nc now mt) ∧ + ScCovA (bootZA kb ib nc).C (bootMA ib nc) (w0SchedStateA kb ib nc now mt) := by + have hL := bootLayA_of P hsc + have hK := bootKsA_of P hsk + have J := (jbootA_w0 (r := r) (now := now) (mt := mt) (a := ()) (fun _ _ => True) P hsc hsk trivial).1.1 + have hCore : SchedCoreA bst (bootZA kb ib nc).T (bootZA kb ib nc).Cs (bootS kb ib nc r now mt) + (bootZA kb ib nc).Q (bootZA kb ib nc).R := by + change SchedCoreA bst (bootTA ib nc) (bootCs nc) (bootS kb ib nc r now mt) (fun _ _ => []) (fun _ => []) + rw [bootTA_eq (kb := kb) hL] + exact (boot_sqi P).toCoreA + have inv : ScInvA (bootMA ib nc) (w0SchedStateA kb ib nc now mt) := by + exact boot_scInvA hL hnc + refine ⟨⟨⟨Sched.SchedRelA.noSt J.sch, J.rest, w0_scStateRelA P hL, + boot_scPlacedA hL hK, J.thT, J.place, J.csN, J.ksFit, J.curT _ J.sch.cpuCs, + hCore, Fl4ProofsA64.P2.BootVt.b0_aff P, ⟨?_, ?_⟩, ?_, J.cscC _ J.sch.cpuCs, inv, ?_, J.cscC⟩, + w0RefillReadyA hL⟩, w0_scCovA hL⟩ + · intro d p t ht; cases ht + · intro t ht; cases ht + · intro u hu; cases hu + · rintro _ ⟨j, hj, rfl⟩ + exact idleBootScCoreA P hL hj + +/-- The w1 scheduler relation and SC heap coverage, with S3's boot refills. -/ +theorem w1_schedWitnessA (P : BootLay1 kb ib nc) + (hsc : 18446603336221208576 < ib.val ∨ ib.val + nc * 8192 ≤ 18446603336221208576) + (hsk : 18446603336221208576 < kb.val ∨ kb.val + 788736 ≤ 18446603336221208576) + (hnc : nc ≤ fl4A64Platform.numNodes) (hnn : fl4A64Platform.numNodes ≤ nc) + (hsl : MIN_BUDGET ≤ sl.bv) : + SchedSRA bst (w1ZA kb ib nc) (w1MA ib nc) (w1SchedScCoreA ib) + (w1S kb ib nc r now mt sl uw) (w1SchedStateA kb ib nc now mt sl) ∧ + ScCovA (w1ZA kb ib nc).C (w1MA ib nc) (w1SchedStateA kb ib nc now mt sl) := by + have hL := bootLayA_of P.w0 hsc + have hK := bootKsA_of P.w0 hsk + have W := bootWordsA_w0 (r := r) (now := now) (mt := mt) P.w0 hsc hsk + have J := w1_jAtZA (uw := uw) (sl := sl) hL hK P W (a := ()) (fun _ _ => True) trivial + have hCore : SchedCoreA bst (w1ZA kb ib nc).T (w1ZA kb ib nc).Cs (w1S kb ib nc r now mt sl uw) + (w1ZA kb ib nc).Q (w1ZA kb ib nc).R := (w1_sqi P).toCoreA + have hOn : bL.mem (w1S kb ib nc r now mt sl uw) (affA (bL.curW (w1S kb ib nc r now mt sl uw))) = 0#u64 := by + rw [cur1 P] + exact (w1_tI P (cur_mem P.w0) (K := 3520#u64) (by decide) (fun _ => by decide)).trans + (Fl4ProofsA64.P2.BootVt.b0_aff P.w0) + have inv : ScInvA (w1MA ib nc) (w1SchedStateA kb ib nc now mt sl) := by + exact w1_scInvA hL P hnc hnn hsl + refine ⟨⟨⟨Sched.SchedRelA.noSt J.sch, J.rest, w1_scStateRelA P hL hnn, + w1BootScPlacedA P hL hK hnn, J.thT, J.place, J.csN, J.ksFit, + J.curT _ J.sch.cpuCs, hCore, hOn, ⟨?_, ?_⟩, ?_, J.cscC _ J.sch.cpuCs, inv, ?_, J.cscC⟩, + w1RefillReadyA hL hsl⟩, w1_scCovA hL P⟩ + · intro d p t ht; cases ht + · intro t ht; cases ht + · intro u hu + change SchedulerAction.switchThread (specPtr (bwv RTb)) = _ at hu + have he : u = bwv RTb := specPtr_inj (SchedulerAction.switchThread.inj hu).symm + subst u + refine ⟨w1_rtT, ?_⟩ + exact (w1_tR P (K := 3520#u64) (by decide)).trans (by rfl) + · intro c hc + rcases w1Sc P hc with ⟨hI, _⟩ | ⟨rfl, _⟩ + · obtain ⟨j, hj, rfl⟩ := bootC_of hL hI + have hne : idleSA ib j ≠ bwv SCb := by + intro e + have h := congrArg UScalar.val e + rw [idleSA_val hL hj, vSC] at h + have := P.ibR + omega + rw [w1SchedScCoreA, if_neg hne] + have hw : bL.mem (w1S kb ib nc r now mt sl uw) (scCoreA (idleSA ib j)) = + bL.mem (bootS kb ib nc r now mt) (scCoreA (idleSA ib j)) := + w1_sI P hI (K := layout.sc.CORE) (by simp [layout.sc.CORE]) + exact hw.trans (idleBootScCoreA P.w0 hL hj) + · rw [w1SchedScCoreA, if_pos rfl] + have hw : bL.mem (w1S kb ib nc r now mt sl uw) (scCoreA (bwv SCb)) = + rscW now sl layout.sc.CORE.val := w1_sR P (K := layout.sc.CORE) (by simp [layout.sc.CORE]) + exact hw.trans (by simp [rscW, layout.sc.CORE]) + +end BootWitness + +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/IpcScEndpointFrameA.lean b/proofs/Fl4ProofsA64/P3/IpcScEndpointFrameA.lean new file mode 100644 index 000000000..45cd0c901 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/IpcScEndpointFrameA.lean @@ -0,0 +1,122 @@ +/- +P3 (AArch64): endpoint transfers and scheduler steps preserve the IPC +reply stack when their SC changes leave reply lists intact. +-/ +import Fl4ProofsA64.P3.IpcScPushTopA + +open Aeneas Aeneas.Std Result +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4SpecA64 Sched +noncomputable section + +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + +private theorem epStepScUnchangedA {σ σ' : State Unit} (hStep : EpStep σ σ') + {p : ObjRef} {x : SchedContext} {n : Nat} (hk : σ.kheap p = some (.schedContext x n)) : + σ'.kheap p = some (.schedContext x n) := by + rcases hStep.2.2 p with h | ⟨_, _, he, _⟩ | ⟨_, _, he, _, _⟩ | ⟨_, _, he, _, _⟩ + · exact h.trans hk + all_goals rw [hk] at he; cases he + +private theorem epStepReplyOwnerA {σ σ' : State Unit} (hStep : EpStep σ σ') + {p : ObjRef} {x y : Reply} (hk : σ.kheap p = some (.reply x)) + (hk' : σ'.kheap p = some (.reply y)) : + ∃ r, σ.kheap p = some (.reply r) ∧ y.replySc = r.replySc := by + rcases hStep.2.2 p with h | ⟨_, _, he, _⟩ | ⟨r, r', he, he', hsc⟩ | ⟨_, _, he, _, _⟩ + · have eq := KernelObject.reply.inj (Option.some.inj (hk'.symm.trans (h.trans hk))) + exact ⟨x, hk, congrArg Reply.replySc eq⟩ + · rw [hk] at he + cases he + · have eq := KernelObject.reply.inj (Option.some.inj (hk'.symm.trans he')) + exact ⟨r, he, (congrArg Reply.replySc eq).trans hsc⟩ + · rw [hk] at he + cases he + +theorem endpointStackFrameA {s s' : S} {σ σ' : State Unit} {stack : U64 → List U64} + (H : SchedSRN st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) + (PE : EPlaced Z.T E R Bf) (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + (w : WritesIn st s s' (EWrite Z.T E R Bf)) (hStep : EpStep σ σ') : + ReplyStackRelA (L.mem s') R Z.C σ' (scNameA m) stack := by + apply HS.frame + · intro r hr + obtain ⟨na, nv⟩ := replyNextPlaceA (Z := Z) PE hr + obtain ⟨pa, pv⟩ := replyPrevPlaceA (Z := Z) PE hr + exact ⟨w.mem_eq na (fun hb => replyLinkOffEp H P PE hr (by omega) (by omega) hb.foot), + w.mem_eq pa (fun hb => replyLinkOffEp H P PE hr (by omega) (by omega) hb.foot)⟩ + · intro c _ x n hk + exact ⟨x, n, epStepScUnchangedA hStep hk, rfl⟩ + · intro r hr y hk + obtain ⟨x, hx, _⟩ := HE.reply r hr + exact epStepReplyOwnerA hStep hx hk + +theorem endpointKeepsIsolatedA {s s' : S} {σ : State Unit} {stack : U64 → List U64} + (H : SchedSRN st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) + (PE : EPlaced Z.T E R Bf) {r : U64} (hr : r ∈ R) + (h : ReplyIsolatedA (L.mem s) Z.C stack r) (w : WritesIn st s s' (EWrite Z.T E R Bf)) : + ReplyIsolatedA (L.mem s') Z.C stack r := by + obtain ⟨na, nv⟩ := replyNextPlaceA (Z := Z) PE hr + obtain ⟨pa, pv⟩ := replyPrevPlaceA (Z := Z) PE hr + exact ⟨h.1, (w.mem_eq na (fun hb => replyLinkOffEp H P PE hr (by omega) (by omega) hb.foot)).trans h.2.1, + (w.mem_eq pa (fun hb => replyLinkOffEp H P PE hr (by omega) (by omega) hb.foot)).trans h.2.2⟩ + +theorem endpointKeepsVcpuA {s s' : S} (PE : EPlaced Z.T E R Bf) {t : U64} (ht : t ∈ Z.T) + (w : WritesIn st s s' (EWrite Z.T E R Bf)) : + L.mem s' (core.num.U64.wrapping_add t 3664#u64) = L.mem s (core.num.U64.wrapping_add t 3664#u64) := by + apply w.mem_eq (field_aligned (K := 3664#u64) (PE.tcb t ht) (by decide) (by decide)) + apply not_ew_offA PE ht (K := 3664#u64) (by decide) _ (by decide) (by decide) + change ¬ TWOffN 3664 + unfold TWOffN + omega + +theorem mcsReplyLinksA {s s' : S} {σ σ' : State Unit} + (H : SchedSRN st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) + (PE : EPlaced Z.T E R Bf) (F : MFrA (st := st) Z m s s' σ σ') : + ∀ r ∈ R, L.mem s' (rNextA r) = L.mem s (rNextA r) ∧ L.mem s' (rPrevA r) = L.mem s (rPrevA r) := by + intro r hr + obtain ⟨na, nv⟩ := replyNextPlaceA (Z := Z) PE hr + obtain ⟨pa, pv⟩ := replyPrevPlaceA (Z := Z) PE hr + exact ⟨F.wr.mem_eq na (replyWordOffSchedA H P hr (by omega) (by omega)).2, + F.wr.mem_eq pa (replyWordOffSchedA H P hr (by omega) (by omega)).2⟩ + +theorem mcsReplyFrameA {s s' : S} {σ σ' : State Unit} {stack : U64 → List U64} + (H : SchedSRN st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) + (PE : EPlaced Z.T E R Bf) (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + (F : MFrA (st := st) Z m s s' σ σ') + (hSc : ∀ p x n, σ.kheap p = some (.schedContext x n) → + ∃ y, σ'.kheap p = some (.schedContext y n) ∧ y.scReplies = x.scReplies) : + ReplyStackRelA (L.mem s') R Z.C σ' (scNameA m) stack := by + apply HS.frame (mcsReplyLinksA H P PE F) + · intro c _ x n hk + obtain ⟨y, hy, hEq⟩ := hSc _ _ _ hk + exact ⟨y, n, hy, hEq⟩ + · intro r hr y hy + obtain ⟨x, hx, _⟩ := HE.reply r hr + have hx' := F.keep.obj (specPtr r) (.reply x) hx (fun _ _ e => by cases e) (fun _ e => by cases e) + exact ⟨x, hx, congrArg Reply.replySc (KernelObject.reply.inj (Option.some.inj (hy.symm.trans hx')))⟩ + +theorem epStepTcbScA {σ σ' : State Unit} {p : ObjRef} {x y : Tcb} (hStep : EpStep σ σ') + (hk : σ.kheap p = some (.tcb x)) (hk' : σ'.kheap p = some (.tcb y)) : + y.tcbSchedContext = x.tcbSchedContext := by + rcases hStep.2.2 p with h | ⟨_, _, he, _⟩ | ⟨_, _, he, _, _⟩ | ⟨a, b, he, he', e⟩ + · exact congrArg Tcb.tcbSchedContext (KernelObject.tcb.inj (Option.some.inj (hk'.symm.trans (h.trans hk)))) + · rw [hk] at he + cases he + · rw [hk] at he + cases he + · have eqA := KernelObject.tcb.inj (Option.some.inj (he.symm.trans hk)) + have eqB := KernelObject.tcb.inj (Option.some.inj (hk'.symm.trans he')) + have e' : b.tcbSchedContext = a.tcbSchedContext := by rw [e] + exact (congrArg Tcb.tcbSchedContext eqB).trans (e'.trans (congrArg Tcb.tcbSchedContext eqA)) + +theorem schedTcbScA {s : S} {σ : State Unit} (H : SchedSRA st Z m κ s σ) + {t : U64} (ht : t ∈ Z.T) {x : Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) : + x.tcbSchedContext = optScA m (L.mem s (scA t)) := by + obtain ⟨y, hy, hs⟩ := H.sch.tcb t ht + exact (congrArg Tcb.tcbSchedContext (KernelObject.tcb.inj (Option.some.inj (hk.symm.trans hy)))).trans hs + + +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/IpcScEndpointPushProgramA.lean b/proofs/Fl4ProofsA64/P3/IpcScEndpointPushProgramA.lean new file mode 100644 index 000000000..ec1ef13c1 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/IpcScEndpointPushProgramA.lean @@ -0,0 +1,71 @@ +/- +P3 (AArch64): select the reply-push arm on each endpoint path. +-/ +import Fl4ProofsA64.P3.IpcScEndpointPushSpecA +import Fl4ProofsA64.P3.IpcScEndpointFrameA + +open Aeneas Aeneas.Std Result +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4SpecA64 + +noncomputable section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +/-- The concrete receiver wakeup that follows a call's reply push. -/ +def wakeAfterReplyPushA (s4 : S) (a : A) (d : U64) : Result (S × A) := do + let s5 ← sched.set_thread_state st s4 d sched.TS_RUNNING + let s6 ← endpoint.unblock_sporadic st s5 d + let s7 ← sched.possible_switch_to st s6 d + ok (s7, a) + +theorem sendRecvSelectedPushA (s : S) (a : A) (t d r : U64) (call g gr cd : Bool) + (hr : r ≠ 0#u64) (hg : g = true ∨ gr = true) + (hcf : call = true ∨ L.mem s (tFaultA t) ≠ fl4_abi.consts.fault.NullFault) : + sendRecvAfterUnlinkA (st := st) (ar := ar) s a t d r call g gr cd = + (do + let (s4, a1) ← ipc.reply_push st ar s a t d r cd + wakeAfterReplyPushA (st := st) s4 a1 d) := by + have hr' : (r != 0#u64) = true := bne_iff_ne.mpr hr + unfold sendRecvAfterUnlinkA + by_cases hcall : call = true + · rw [if_pos hcall, if_pos hr'] + rcases hg with hg | hgr + · rw [if_pos hg] <;> rfl + · by_cases h : g = true + · rw [if_pos h] <;> rfl + · rw [if_neg h, if_pos hgr] <;> rfl + · have hFault : (L.mem s (tFaultA t) != fl4_abi.consts.fault.NullFault) = true := + bne_iff_ne.mpr (hcf.resolve_left hcall) + rw [if_neg hcall, eq_of_spec_fd (obj.Tcb.fault_type.spec (L := L) s t), bind_ok, if_pos hFault, if_pos hr'] + rcases hg with hg | hgr + · rw [if_pos hg] <;> rfl + · by_cases h : g = true + · rw [if_pos h] <;> rfl + · rw [if_neg h, if_pos hgr] <;> rfl + +theorem recvSendSelectedDonateA (s : S) (a : A) (t f r : U64) (dd : SenderPayload) + (hr : r ≠ 0#u64) (hg : dd.senderCanGrant = true ∨ dd.senderCanGrantReply = true) + (hcf : dd.senderIsCall = true ∨ L.mem s (tFaultA f) ≠ fl4_abi.consts.fault.NullFault) + (hsc : L.mem s (scA f) ≠ 0#u64) + (hft : L.mem s (tFaultA f) ≠ fl4_abi.consts.fault.Timeout) : + recvSendAfterCheckA (st := st) (ar := ar) s a t f r dd = + ipc.reply_push st ar s a f t r true := by + have hr' : (r != 0#u64) = true := bne_iff_ne.mpr hr + have hsc' : (L.mem s (scA f) != 0#u64) = true := bne_iff_ne.mpr hsc + have hft' : (L.mem s (tFaultA f) != fl4_abi.consts.fault.Timeout) = true := bne_iff_ne.mpr hft + unfold recvSendAfterCheckA + rw [eq_of_spec_fd (obj.Tcb.fault_type.spec (L := L) s f), bind_ok] + by_cases hcall : dd.senderIsCall = true + all_goals + first + | rw [if_pos hcall] + | have hFault : (L.mem s (tFaultA f) != fl4_abi.consts.fault.NullFault) = true := + bne_iff_ne.mpr (hcf.resolve_left hcall) + rw [if_neg hcall, if_pos hFault] + by_cases h : dd.senderCanGrant = true + · rw [if_pos h, if_pos hr', eq_of_spec_fd (obj.Tcb.sc.spec (L := L) s f), bind_ok, if_pos hsc', hft', bind_ok] + · rw [if_neg h, if_pos (hg.resolve_left h), if_pos hr', + eq_of_spec_fd (obj.Tcb.sc.spec (L := L) s f), bind_ok, if_pos hsc', hft', bind_ok] + +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/IpcScEndpointPushSpecA.lean b/proofs/Fl4ProofsA64/P3/IpcScEndpointPushSpecA.lean new file mode 100644 index 000000000..4999783d1 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/IpcScEndpointPushSpecA.lean @@ -0,0 +1,72 @@ +import Fl4ProofsA64.P3.EpSendRecvA +import Fl4ProofsA64.P3.EpRecvSendA + +open Aeneas Aeneas.Std +namespace Fl4ProofsA64.P3.Endp +open Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4SpecA64 Fl4Spec + +noncomputable section + +theorem sendIpc_recv_pushSpecA {σ : State Unit} {t e d r : ObjRef} {q : List ObjRef} + (hk : σ.kheap e = some (.endpoint (.recvEP (d :: q)))) {xd : Tcb} (hkd : σ.kheap d = some (.tcb xd)) + (hde : d ≠ e) {e' : ObjRef} {dd : ReceiverPayload} + (hst : xd.tcbState = .blockedOnReceive e' (some r) dd) + (blk call : Bool) (badge : Badge) (g gr cd : Bool) {σ2 σ3 : State Unit} + (hT : doIpcTransfer (Z := Unit) t (some e) badge g d (updK σ e (.endpoint (epDeq (.recvEP (d :: q)) d))) = + ({((), σ2)}, False)) + (hU : replyUnlinkTcb (Z := Unit) d r σ2 = ({((), σ3)}, False)) + {xt3 x3 : Tcb} (hkt3 : σ3.kheap t = some (.tcb xt3)) (hkd3 : σ3.kheap d = some (.tcb x3)) + (hcf : call = true ∨ xt3.tcbFault ≠ none) (hg : g = true ∨ gr = true) : + sendIpc (Z := Unit) blk call badge g gr cd t e σ = + (do + replyPush t d r cd + setThreadState d .running + specSendTailA d) σ3 := by + have hkd1 : (updK σ e (.endpoint (epDeq (.recvEP (d :: q)) d))).kheap d = some (.tcb xd) := by + rw [updK_kheap, Function.update_of_ne hde, hkd] + have hg' : (g = true ∨ gr = true) ∧ (some r : Option ObjRef) ≠ none := + ⟨hg, Option.some_ne_none _⟩ + unfold sendIpc + rw [nd_bind_one (getEndpoint_eq hk)] + cases blk <;> dsimp only <;> + · rw [nd_bind_one (tcbEpDequeue_eq hk (by simp [Endpoint.epQueue])), nd_bind_one (getThreadState_eq hkd1), hst] + dsimp only + rw [nd_pure_bind'] + change _ = _ + rw [nd_bind_one hT] + change (replyUnlinkTcb d r >>= _) σ2 = _ + rw [nd_bind_one hU, nd_bind_one (getTcbObjRef_eq _ hkd3), nd_bind_one (threadGet_eq _ hkt3), + if_pos hcf, if_pos hg'] + rfl + +theorem recvSend_pushSpecA {σ : State Unit} {t e sd r : ObjRef} {q : List ObjRef} + (hk : σ.kheap e = some (.endpoint (.sendEP (sd :: q)))) {xs : Tcb} (hks : σ.kheap sd = some (.tcb xs)) + (hse : sd ≠ e) {e' : ObjRef} {dd : SenderPayload} (hst : xs.tcbState = .blockedOnSend e' dd) + {σ2 σ3 : State Unit} + (hT : doIpcTransfer (Z := Unit) sd (some e) dd.senderBadge dd.senderCanGrant t + (updK σ e (.endpoint (epDeq (.sendEP (sd :: q)) sd))) = ({((), σ2)}, False)) + (hks2 : σ2.kheap sd = some (.tcb xs)) + (hA : ifSporadicCurScAssertRefillUnblockCheck (Z := Unit) xs.tcbSchedContext σ2 = ({((), σ3)}, False)) + {xs3 : Tcb} (hks3 : σ3.kheap sd = some (.tcb xs3)) + (hcf : dd.senderIsCall = true ∨ xs3.tcbFault ≠ none) + (hg : dd.senderCanGrant = true ∨ dd.senderCanGrantReply = true) : + specRecvSendA t e (some r) (sd :: q) σ = + replyPush (Z := Unit) sd t r + (decide (xs3.tcbSchedContext ≠ none ∧ + ¬ (xs3.tcbFault.elim false isTimeoutFault = true))) σ3 := by + have hks1 : (updK σ e (.endpoint (epDeq (.sendEP (sd :: q)) sd))).kheap sd = some (.tcb xs) := by + rw [updK_kheap, Function.update_of_ne hse, hks] + have hg' : (dd.senderCanGrant = true ∨ dd.senderCanGrantReply = true) ∧ (some r : Option ObjRef) ≠ none := + ⟨hg, Option.some_ne_none _⟩ + unfold specRecvSendA + rw [nd_bind_one (assert_true (by simp) σ), nd_pure_bind'] + rw [show the (sd :: q).head? = sd from rfl, nd_bind_one (tcbEpDequeue_eq hk (by simp [Endpoint.epQueue])), + nd_bind_one (getThreadState_eq hks1), hst] + dsimp only + rw [nd_pure_bind', nd_bind_one hT, nd_bind_one (getTcbObjRef_eq _ hks2), nd_bind_one hA, + nd_bind_one (threadGet_eq _ hks3), if_pos hcf, if_pos hg', nd_bind_one (getTcbObjRef_eq _ hks3), + nd_pure_bind'] + rfl + +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/IpcScEndpointRefillA.lean b/proofs/Fl4ProofsA64/P3/IpcScEndpointRefillA.lean new file mode 100644 index 000000000..6c46469c7 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/IpcScEndpointRefillA.lean @@ -0,0 +1,162 @@ +/- +P3 (AArch64): the sporadic checks in the endpoint paths preserve +the scheduling contexts' reply lists. +-/ +import Fl4ProofsA64.P3.IpcScEndpointFrameA + +open Aeneas Aeneas.Std Result +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4Spec Fl4SpecA64 Sched +noncomputable section + +/-- Preservation of the reply lists and sizes of the SC objects across a spec step. -/ +structure ScReplyListsKeepA (σ σ' : State Unit) : Prop where + idle : σ'.idleThread = σ.idleThread + mem : σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory + sc : ∀ q x n, σ.kheap q = some (.schedContext x n) → + ∃ y, σ'.kheap q = some (.schedContext y n) ∧ y.scReplies = x.scReplies + +theorem ScReplyListsKeepA.refl (σ : State Unit) : ScReplyListsKeepA σ σ := + ⟨rfl, rfl, fun _ x _ h => ⟨x, h, rfl⟩⟩ + +private theorem refillLoopReplyListsA {σ σ' : State Unit} {p : ObjRef} {x : SchedContext} {n : Nat} + (hk : σ.kheap p = some (.schedContext x n)) + (h : ((), σ') ∈ (refillHeadOverlappingLoop (Z := Unit) p σ).1) : + ScReplyListsKeepA σ σ' := by + obtain ⟨y, hpost, _, _, he, _⟩ := merge_loop_resultA hk h + refine ⟨by rw [hpost]; rfl, by rw [hpost]; rfl, ?_⟩ + intro q z k hq + by_cases e : q = p + · subst q + rw [hk] at hq + cases hq + have he' := congrArg SchedContext.scReplies he + exact ⟨y, hpost.symm ▸ updKSc_same _ _ _, he'⟩ + · refine ⟨z, ?_, rfl⟩ + rw [hpost, updKSc_kheap, Function.update_of_ne e] + exact hq + +private theorem refillReadyNoneA {σ : State Unit} {p : ObjRef} {x : SchedContext} {n : Nat} + (hk : σ.kheap p = some (.schedContext x n)) (hn : x.scRefills = []) : + getScRefillReady (Z := Unit) p σ = (∅, True) := by + have headNone : readRefillHead (Z := Unit) p σ = none := by + show Option.bind (readSchedContext p σ) (fun sc => Option.bind (Lookup.oassert (sc.scRefills ≠ []) σ) + (fun _ => Lookup.oreturn (refillHd sc) σ)) = _ + rw [readSchedContext_eqA hk] + change (Lookup.oassert (x.scRefills ≠ []) σ).bind (fun _ => Lookup.oreturn (refillHd x) σ) = _ + rw [hn] + rfl + have readyNone : readScRefillReady (Z := Unit) p σ = none := by + show Option.bind (readRefillHead p σ) (fun h => Option.bind (Lookup.asks + (fun s : State Unit => s.curTime) σ) (fun ct => Lookup.oreturn (refillReady ct h) σ)) = _ + rw [headNone] + rfl + unfold getScRefillReady NonDet.getsThe + rw [readyNone] + rfl + +private theorem unblockReplyListsA {σ σ' : State Unit} {p : ObjRef} {x : SchedContext} {n : Nat} + (hk : σ.kheap p = some (.schedContext x n)) + (h : ((), σ') ∈ (refillUnblockCheck (Z := Unit) p σ).1) : + ScReplyListsKeepA σ σ' := by + unfold refillUnblockCheck at h + rw [nd_bind_oneSc (isRoundRobin_eqA hk)] at h + by_cases hn : x.scRefills = [] + · rw [nd_bind_applySc, refillReadyNoneA hk hn] at h + obtain ⟨_, hm, _⟩ := h + exact hm.elim + rw [nd_bind_oneSc (getScRefillReady_eqA hk hn), NonDet.when] at h + split at h + · rw [nd_gets_bindSc, nd_bind_oneSc (updateRefillHd_eqA hk _)] at h + let y := { x with scRefills := { the x.scRefills.head? with rTime := σ.curTime } :: x.scRefills.tail } + let pre := { updKSc σ p (.schedContext y n) with reprogramTimer := true } + have hh : pre.kheap p = some (.schedContext y n) := updKSc_same _ _ _ + have hm : ((), σ') ∈ (refillHeadOverlappingLoop (Z := Unit) p pre).1 := by + rw [nd_bind_oneSc (show (NonDet.modify (fun t : State Unit => { t with reprogramTimer := true })) + (updKSc σ p (.schedContext y n)) = ({((), pre)}, False) from rfl)] at h + exact h + have hkeep := refillLoopReplyListsA hh hm + refine ⟨hkeep.idle, hkeep.mem, ?_⟩ + intro q z k hq + by_cases e : q = p + · subst q + rw [hk] at hq + cases hq + exact hkeep.sc p y n hh + have hq' : pre.kheap q = some (.schedContext z k) := by + change (updKSc σ p (.schedContext y n)).kheap q = _ + rw [updKSc_kheap, Function.update_of_ne e] + exact hq + exact hkeep.sc q z k hq' + · have eq : σ' = σ := congrArg Prod.snd (Set.mem_singleton_iff.mp h) + subst σ' + exact .refl σ + +theorem condUnblockReplyListsA {σ σ' : State Unit} {o : Option ObjRef} (asst : Bool) + (hp : ∀ p, o = some p → ∃ x n, σ.kheap p = some (.schedContext x n)) + (h : ((), σ') ∈ (ifCondRefillUnblockCheck (Z := Unit) o (some false) (some asst) σ).1) : + ScReplyListsKeepA σ σ' := by + cases o with + | none => + have eq : σ' = σ := congrArg Prod.snd (Set.mem_singleton_iff.mp h) + subst σ' + exact .refl σ + | some p => + obtain ⟨x, n, hk⟩ := hp p rfl + simp only [ifCondRefillUnblockCheck, NonDet.maybeM] at h + rw [nd_bind_oneSc (getSchedContext_eqA hk), nd_gets_bindSc, nd_pure_bindSc] at h + unfold NonDet.when at h + split at h + · split at h + · exact unblockReplyListsA hk h + · have eq : σ' = σ := congrArg Prod.snd (Set.mem_singleton_iff.mp h) + subst σ' + exact .refl σ + · have eq : σ' = σ := congrArg Prod.snd (Set.mem_singleton_iff.mp h) + subst σ' + exact .refl σ + +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + +/-- The sender's sporadic check, preserving its TCB and the reply stack. -/ +theorem endpointPushSporadicA {s : S} {σ : State Unit} {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) + (PE : EPlaced Z.T E R Bf) (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {f r : U64} (hf : f ∈ Z.T) (hr : r ∈ R) (hi : ReplyIsolatedA (L.mem s) Z.C stack r) + {x : Tcb} (hk : σ.kheap (specPtr f) = some (.tcb x)) (hnr : runnable x.tcbState = false) : + endpoint.unblock_sporadic st s f ⦃ s' => ∃ σ', + ifSporadicCurScAssertRefillUnblockCheck (Z := Unit) x.tcbSchedContext σ = ({((), σ')}, False) ∧ + SchedSRA st Z m κ s' σ' ∧ EpStateRel Z.T E R Bf (L.mem s') σ' ∧ + ReplyStackRelA (L.mem s') R Z.C σ' (scNameA m) stack ∧ + ReplyIsolatedA (L.mem s') Z.C stack r ∧ + (L.mem s' (core.num.U64.wrapping_add f 3664#u64) = L.mem s (core.num.U64.wrapping_add f 3664#u64)) ∧ + (∀ q o, σ.kheap q = some o → (∀ y n, o ≠ .schedContext y n) → σ'.kheap q = some o) ∧ + σ'.schedulerAction = σ.schedulerAction ∧ σ'.idleThread = σ.idleThread ⦄ := by + obtain ⟨x', hk', hSC⟩ := H.sch.tcb f hf + have hSCeq := congrArg Tcb.tcbSchedContext (KernelObject.tcb.inj (Option.some.inj (hk.symm.trans hk'))) + have hsct := hSCeq.trans hSC + rw [endpoint_unblock_sporadic_eqA] + refine WP.spec_mono (unblock_sporadic_SRA H hf true (fun h => absurd h (H.notQueued hk hnr).2)) ?_ + rintro s' ⟨σ', eq, H', F⟩ + have hA : ifSporadicCurScAssertRefillUnblockCheck (Z := Unit) x.tcbSchedContext σ = ({((), σ')}, False) := by + rw [hsct] + exact eq + have hres : ((), σ') ∈ (ifSporadicCurScAssertRefillUnblockCheck (Z := Unit) x.tcbSchedContext σ).1 := by + rw [hA] + exact Set.mem_singleton _ + have preserve := condUnblockReplyListsA true (fun p hp => by + obtain ⟨y, n, hy, _⟩ := H.inv.back (specPtr f) x p hk hp + exact ⟨y, n, hy⟩) hres + have fm := F.mfr H.toSchedSRN + obtain ⟨FE, VIEW⟩ := epFrameOfMcsA H.toSchedSRN P fm preserve.mem + have links := mcsReplyLinksA H.toSchedSRN P PE fm r hr + have vcpu := fm.wr.mem_eq (field_aligned (K := 3664#u64) (PE.tcb f hf) (by decide) (by decide)) + (vcpuWordOffMcsA H.toSchedSRN hf) + exact ⟨σ', hA, H', HE.frame PE FE VIEW, mcsReplyFrameA H.toSchedSRN P PE HE HS fm preserve.sc, + ⟨hi.1, links.1.trans hi.2.1, links.2.trans hi.2.2⟩, vcpu, F.obj, F.act, preserve.idle⟩ + + +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/IpcScFootA.lean b/proofs/Fl4ProofsA64/P3/IpcScFootA.lean new file mode 100644 index 000000000..983150121 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/IpcScFootA.lean @@ -0,0 +1,130 @@ +/- +P3 (aarch64, A64-P3 Stage-3 integration, D8/T1 step 2): **the producer of `SchedFootEA`** from the MCS scheduler +(`schedOpsEA_of_sched`'s footprint twin). The write set is `MWrA Z m σ s`; the producers already hold these +footprints (`schedule_tcb_refinesA`, `restart_tail_localA`, `reschedule_required_refinesA`, +`unblock_sporadic_blockedA`, `switch_to_runA`) and `schedOpsEA_of_sched` drops them. No named premise. +-/ +import Fl4ProofsA64.P3.IpcScGlueA +import Fl4ProofsA64.P3.SchedFootA +import Fl4ProofsA64.P3.SchedFootNA +import Fl4ProofsA64.P3.SpecFrameSchedA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4SpecA64 Sched + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +theorem schedFootEA_of_sched {Z : XSetsA} {m : McState} {κ : U64 → U64} {E R : Set U64} {Bf : Set (U64 × Word)} + : + SchedFootEA st Z.T E R Bf (IpcSchedRelA st Z m κ) (fun σ s => MWrA (st := st) Z m σ s) := by + constructor + · intro σ σ1 s s1 b hz hk h + exact mwr_back rfl rfl hz hk h + · intro σ σ' o h + exact ((SPresInst.pres (m := Fl4SpecA64.ifSporadicCurScAssertRefillUnblockCheck (Z := Unit) o)) σ ((), σ') + (by rw [h]; exact Set.mem_singleton _)).2.1 + · rintro s σ t hRel ⟨Q, R', H⟩ _ + refine WP.spec_mono (schedule_tcb_refinesA H t (fun _ ht _ hx => (hRel.at_tcb ht hx).st)) ?_ + rintro s' ⟨hw, -⟩ + exact hw.mono fun b hb => hb ▸ swAction (Z := { Z with Q := Q, R := R' }) H + · rintro s σ t x hRel ⟨Q, R', H⟩ ht hk hr _ hon hq + refine WP.spec_mono (restart_tail_localA H ht hk hr hon (fun _ ht' _ hx => (hRel.at_tcb ht' hx).st) hq.2) ?_ + rintro s' ⟨σ', Q', -, -, F⟩ + exact F.wr + · rintro s σ hRel ⟨Q, R', H⟩ + refine WP.spec_mono (reschedule_required_refinesA H (fun _ ht _ hx => (hRel.at_tcb ht hx).st)) ?_ + rintro s' ⟨σ', Q', -, -, -, hw, -⟩ + exact hw.mono fun _ e => Or.inr e + · rintro s σ t x hRel ⟨Q, R', H⟩ ht hk hr _ + refine WP.spec_mono (unblock_sporadic_blockedA H ht hk hr) ?_ + rintro s' ⟨σ', -, -, F⟩ + exact F.wr + · rintro s σ t x hRel ⟨Q, R', H⟩ ht hk hr _ hon _ + refine WP.spec_mono (switch_to_runA H ht hk hr hon (fun _ ht' _ hx => (hRel.at_tcb ht' hx).st)) ?_ + rintro s' ⟨σ', Q', -, -, F⟩ + exact F.wr + + +end + +end + +end Fl4ProofsA64.P3.Endp + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4SpecA64 Sched + +noncomputable section + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +/-- The notification scheduler footprints from the MCS scheduler (`schedOpsA_of_sched`'s footprint twin). -/ +theorem schedFootNA_of_sched {Z : XSetsA} {m : McState} {κ : U64 → U64} {N : Set U64} + (hP : Notif.NPlaced Z.T N) (hc : m.curCore < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hN : ∀ n ∈ N, Z.th n = some (.other 64)) : + Notif.SchedFootNA st ar N Z.T (IpcSchedRelA st Z m κ) (DonLocalA (st := st) Z.C) + (fun σ s => MWrA (st := st) Z m σ s) := by + constructor + · intro σ σ1 s s1 b hz hk h + exact mwr_back rfl rfl hz hk h + · rintro s σ t hRel ⟨Q, R', H⟩ _ + refine WP.spec_mono (schedule_tcb_refinesA H t (fun _ ht _ hx => (hRel.at_tcb ht hx).st)) ?_ + rintro s' ⟨hw, -⟩ + exact hw.mono fun b hb => hb ▸ swAction (Z := { Z with Q := Q, R := R' }) H + · rintro s a σ t n x hRel ⟨Q, R, H⟩ ht hn hk hrun hq hon hd + have hr : ∀ y, σ.kheap (specPtr t) = some (.tcb y) → runnable y.tcbState = true := by + intro y hy + rw [hk] at hy + cases hy + rw [hrun] + rfl + refine WP.spec_mono (wake_tail_frameA H a (hmb a) ht hon (notifScWord hn hRel H.inv hc) + (fun _ ht' _ hx => (hRel.at_tcb ht' hx).st) hq.1 hq.2 hr hd) ?_ + rintro r ⟨_, σ', Q', R', hs, H', F⟩ + exact F.wr + · rintro s a σ t n hRel ⟨Q, R, H⟩ ht hn hq hr hon hd + refine WP.spec_mono (recv_tail_frameA H a (hmb a) ht hon (notifScWord hn hRel H.inv hc) + (fun _ ht' _ hx => (hRel.at_tcb ht' hx).st) hq.1 hq.2 hr hd) ?_ + rintro r ⟨_, σ', R', hs, H', F⟩ + exact F.wr + · rintro s a σ t n hRel ⟨Q, R, H⟩ ht hn hq hr hon hd + refine WP.spec_mono (completeTailFromSched H a hP hc (hmb a) hRel ht hn (hN n hn) hq hr hon hd) ?_ + rintro r ⟨_, σ', R', hs, H', F⟩ + exact F.wr + · rintro s σ0 σ t n hRel ⟨Q, R, H⟩ ht hn _ hb _ + obtain ⟨_, y, _, hy, _, hnr⟩ := hb.2.2.2.2.2 + have hrun : ∀ z, σ.kheap (specPtr t) = some (.tcb z) → runnable z.tcbState = false := by + intro z hz + rw [hy] at hz + cases hz + exact hnr + refine WP.spec_mono (maybe_return_frameA H (fun _ ht' _ hx => (hRel.at_tcb ht' hx).st) ht + (notifScWord hn hRel H.inv hc) hrun) ?_ + rintro s' ⟨σ', Q', hs, H', hw, hk, _⟩ + exact hw + · rintro s σ t x hRel ⟨Q, R, H⟩ ht hk hr _ hon hq + have hrun : ∀ z, σ.kheap (specPtr t) = some (.tcb z) → runnable z.tcbState = true := by + intro z hz + rw [hk] at hz + cases hz + rw [hr] + rfl + refine WP.spec_mono (restart_tail_frameA H ht hon hrun (fun _ ht' _ hx => (hRel.at_tcb ht' hx).st) hq.2) ?_ + rintro s' ⟨σ', Q', hs, H', F⟩ + exact F.wr + · rintro s σ hRel ⟨Q, R, H⟩ + refine WP.spec_mono (reschedule_required_refinesA H (fun _ ht _ hx => (hRel.at_tcb ht hx).st)) ?_ + rintro s' ⟨σ', Q', hs, hk, _, hw, H'⟩ + exact hw.mono fun _ e => Or.inr e + +end + +end + +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/IpcScGlueA.lean b/proofs/Fl4ProofsA64/P3/IpcScGlueA.lean new file mode 100644 index 000000000..d7bb6de8d --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/IpcScGlueA.lean @@ -0,0 +1,910 @@ +/- +P3 (AArch64): the scheduler interfaces at the IPC and MCS join. + +A timeout-faulted sender cannot be any core's idle thread. Its SC therefore has its native spec name. +The timeout caller supplies the non-idle witness. The update touches only the SC's consumed word. + +The active notification completion supplies a local endpoint frame. IPC footprints are separated +using the existing typed heap placement. No named premises. The time conversion laws are those of +`update_consumed_tcbA`. +-/ +import Fl4ProofsA64.P3.EpFaultTmoA +import Fl4ProofsA64.P3.ScIdleA +import Fl4ProofsA64.P3.ScConsA +import Fl4ProofsA64.P3.ScFrameA +import Fl4ProofsA64.P3.ScGlueA +import Fl4ProofsA64.P3.ScTailEA +import Fl4ProofsA64.P3.EpRecvBndA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4SpecA64 Sched + +noncomputable section + +section UserMemory +open Fl4Spec + +/-- All successful results of an operation keep the specification's user memory. -/ +private def KeepsUserMemory {α : Type} (op : SMonad Unit α) : Prop := + ∀ σ σ' a, (a, σ') ∈ (op σ).1 → σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory + +private theorem KeepsUserMemory.bind {α β : Type} {op : SMonad Unit α} {next : α → SMonad Unit β} + (h : KeepsUserMemory op) (hn : ∀ a, KeepsUserMemory (next a)) : KeepsUserMemory (op >>= next) := by + intro σ σ' b hb + obtain ⟨⟨a, σ₁⟩, ha, hnext⟩ := hb + exact (hn a σ₁ σ' b hnext).trans (h σ σ₁ a ha) + +private theorem KeepsUserMemory.singleton {α : Type} {op : SMonad Unit α} (f : State Unit → α × State Unit) + (he : ∀ σ, (op σ).1 = {f σ}) + (hm : ∀ σ, (f σ).2.machineState.underlyingMemory = σ.machineState.underlyingMemory) : + KeepsUserMemory op := by + intro σ σ' a ha + rw [he, Set.mem_singleton_iff] at ha + exact (congrArg (fun p : α × State Unit => p.2.machineState.underlyingMemory) ha).trans (hm σ) + +private theorem KeepsUserMemory.pure {α : Type} (a : α) : KeepsUserMemory (pure a) := + KeepsUserMemory.singleton (fun σ => (a, σ)) (fun _ => rfl) fun _ => rfl + +private theorem KeepsUserMemory.gets {α : Type} (f : State Unit → α) : KeepsUserMemory (NonDet.gets f) := + KeepsUserMemory.singleton (fun σ => (f σ, σ)) (fun _ => rfl) fun _ => rfl + +private theorem KeepsUserMemory.modify {f : State Unit → State Unit} + (h : ∀ σ, (f σ).machineState.underlyingMemory = σ.machineState.underlyingMemory) : + KeepsUserMemory (NonDet.modify f) := + KeepsUserMemory.singleton (fun σ => ((), f σ)) (fun _ => rfl) h + +private theorem KeepsUserMemory.fail {α : Type} : KeepsUserMemory (NonDet.fail : SMonad Unit α) := by + intro σ σ' a h + exact (Set.notMem_empty _ h).elim + +private theorem KeepsUserMemory.assert (P : Prop) : KeepsUserMemory (NonDet.assert P) := by + intro σ σ' a h + have e : (a, σ') = ((), σ) := h.2 + cases e + rfl + +private theorem KeepsUserMemory.assertOpt {α : Type} (x : Option α) : KeepsUserMemory (NonDet.assertOpt x) := by + cases x with + | none => exact KeepsUserMemory.fail + | some a => exact KeepsUserMemory.pure a + +private theorem KeepsUserMemory.getsThe {α : Type} (f : State Unit → Option α) : + KeepsUserMemory (NonDet.getsThe f) := + fun σ => KeepsUserMemory.assertOpt (f σ) σ + +private theorem KeepsUserMemory.when {P : Prop} {d : Decidable P} {op : SMonad Unit Unit} + (h : KeepsUserMemory op) : KeepsUserMemory (@NonDet.when _ P d op) := by + unfold NonDet.when + split_ifs + · exact h + · exact KeepsUserMemory.pure () + +private theorem KeepsUserMemory.maybeM {α : Type} {f : α → SMonad Unit Unit} (hf : ∀ x, KeepsUserMemory (f x)) + (o : Option α) : KeepsUserMemory (NonDet.maybeM f o) := by + cases o with + | none => exact KeepsUserMemory.pure () + | some x => exact hf x + +private theorem KeepsUserMemory.loop {α : Type} {cond : α → State Unit → Prop} {body : α → SMonad Unit α} + (h : ∀ a, KeepsUserMemory (body a)) (a : α) : KeepsUserMemory (NonDet.whileLoop cond body a) := by + intro σ σ' b hb + change NonDet.WhileResults cond body a σ (b, σ') at hb + suffices aux : ∀ a σ res, NonDet.WhileResults cond body a σ res → + res.2.machineState.underlyingMemory = σ.machineState.underlyingMemory from aux a σ _ hb + intro a σ res hw + induction hw with + | done _ => rfl + | step _ hm _ ih => exact ih.trans (h _ _ _ _ hm) + +private theorem KeepsUserMemory.mapM {α β : Type} {f : α → SMonad Unit β} (h : ∀ a, KeepsUserMemory (f a)) + (xs : List α) : KeepsUserMemory (NonDet.mapM f xs) := by + induction xs with + | nil => exact KeepsUserMemory.pure [] + | cons x xs hi => exact KeepsUserMemory.bind (h x) fun y => + KeepsUserMemory.bind hi fun ys => KeepsUserMemory.pure (y :: ys) + +private theorem KeepsUserMemory.setObject (p : ObjRef) (obj : KernelObject) : + KeepsUserMemory (setObject (Z := Unit) p obj) := by + unfold Fl4SpecA64.setObject getObject + refine KeepsUserMemory.bind (KeepsUserMemory.getsThe _) fun old => ?_ + refine KeepsUserMemory.bind (KeepsUserMemory.assert _) fun _ => ?_ + refine KeepsUserMemory.singleton (fun σ => ((), updKSc σ p obj)) (fun σ => ?_) fun _ => rfl + rw [Notif.nd_bind_one (show NonDet.get σ = ({(σ, σ)}, False) from rfl)] + rfl + +private theorem KeepsUserMemory.updateSc (p : ObjRef) (f : SchedContext → SchedContext) : + KeepsUserMemory (updateSchedContext (Z := Unit) p f) := by + unfold updateSchedContext getObject + refine KeepsUserMemory.bind (KeepsUserMemory.getsThe _) fun old => ?_ + cases old <;> first | exact KeepsUserMemory.setObject _ _ | exact KeepsUserMemory.fail + +private theorem KeepsUserMemory.getSc (p : ObjRef) : KeepsUserMemory (getSchedContext (Z := Unit) p) := by + unfold getSchedContext getObject + refine KeepsUserMemory.bind (KeepsUserMemory.getsThe _) fun old => ?_ + cases old <;> first | exact KeepsUserMemory.pure _ | exact KeepsUserMemory.fail + +private theorem KeepsUserMemory.getSimpleKo {α : Type} [Nonempty α] (tag : α → KernelObject) (p : ObjRef) : + KeepsUserMemory (Fl4SpecA64.getSimpleKo (Z := Unit) tag p) := by + unfold Fl4SpecA64.getSimpleKo getObject + refine KeepsUserMemory.bind (KeepsUserMemory.getsThe _) fun old => + KeepsUserMemory.bind (KeepsUserMemory.assert _) fun _ => ?_ + split + · apply KeepsUserMemory.pure + · exact KeepsUserMemory.fail + +-- Apply the frame rules to successive commands in an unfolded spec operation. +local macro "keep_user_memory" : tactic => `(tactic| + repeat' first + | assumption + | apply KeepsUserMemory.pure + | apply KeepsUserMemory.getsThe + | apply KeepsUserMemory.gets + | apply KeepsUserMemory.assertOpt + | apply KeepsUserMemory.assert + | apply KeepsUserMemory.fail + | exact KeepsUserMemory.modify (fun _ => rfl) + | apply KeepsUserMemory.setObject + | apply KeepsUserMemory.updateSc + | apply KeepsUserMemory.getSc + | apply KeepsUserMemory.getSimpleKo + | apply KeepsUserMemory.when + | (apply KeepsUserMemory.maybeM; intro) + | (apply KeepsUserMemory.loop; intro) + | (apply KeepsUserMemory.mapM; intro) + | refine KeepsUserMemory.bind ?_ (fun _ => ?_)) + +private theorem rescheduleKeepsUserMemory : KeepsUserMemory (rescheduleRequired (Z := Unit)) := by + unfold rescheduleRequired + refine KeepsUserMemory.bind (KeepsUserMemory.gets _) fun act => ?_ + cases act <;> + simp only [tcbSchedAction, getTcbQueue, setTcbQueue, threadGet, getScRefillSufficient, getScRefillReady, + setSchedulerAction] <;> + keep_user_memory + +theorem switchKeepsUserMemory (t : ObjRef) : KeepsUserMemory (possibleSwitchTo (Z := Unit) t) := by + unfold possibleSwitchTo getTcbObjRef + simp only [threadGet] + keep_user_memory + split_ifs with hd ha + · simp only [tcbSchedAction, getTcbQueue, setTcbQueue, threadGet] + keep_user_memory + · refine KeepsUserMemory.bind rescheduleKeepsUserMemory fun _ => ?_ + simp only [tcbSchedAction, getTcbQueue, setTcbQueue, threadGet] + keep_user_memory + · exact KeepsUserMemory.modify fun _ => rfl + +private theorem donateKeepsUserMemory (t n : ObjRef) : KeepsUserMemory (maybeDonateSc (Z := Unit) t n) := by + have hReschedule := rescheduleKeepsUserMemory + unfold maybeDonateSc + simp only [getTcbObjRef, threadGet, getNtfnObjRef, getSkObjRef, getScObjRef, schedContextDonate, + schedContextResume, tcbSchedAction, getTcbQueue, setTcbQueue, tcbReleaseRemove, getScRefillReady, + getScRefillSufficient, postpone, tcbReleaseEnqueue, readyTimesAppend, orderedInsert, threadSet, + threadGet, testReschedule] + keep_user_memory + +private theorem refillKeepsUserMemory (p : ObjRef) : KeepsUserMemory (refillUnblockCheck (Z := Unit) p) := by + unfold refillUnblockCheck + simp only [isRoundRobin, getScRefillReady, updateRefillHd, refillHeadOverlappingLoop, mergeOverlappingRefills, + refillPopHead, getRefillHead] + keep_user_memory + +private theorem condRefillKeepsUserMemory (p : Option ObjRef) (active test : Option Bool) : + KeepsUserMemory (ifCondRefillUnblockCheck (Z := Unit) p active test) := by + unfold ifCondRefillUnblockCheck + keep_user_memory + +private theorem completeTailKeepsUserMemory (t n : ObjRef) : KeepsUserMemory (Notif.specCompleteTail t n) := by + unfold Notif.specCompleteTail + refine KeepsUserMemory.bind (donateKeepsUserMemory t n) fun _ => ?_ + simp only [getTcbObjRef, getNtfnObjRef, getSkObjRef, threadGet] + keep_user_memory + +/-- The scheduler context donation updates only heap and scheduler fields. -/ +theorem schedContextDonateKeepsUserMemory (sc t : ObjRef) : + ∀ σ σ' : State Unit, ((), σ') ∈ (schedContextDonate (Z := Unit) sc t σ).1 → + σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory := by + have h : KeepsUserMemory (schedContextDonate (Z := Unit) sc t) := by + have hReschedule := rescheduleKeepsUserMemory + unfold schedContextDonate + simp only [getScObjRef, getTcbObjRef, getSkObjRef, threadGet, tcbSchedAction, getTcbQueue, + setTcbQueue, tcbReleaseRemove, threadSet, testReschedule] + keep_user_memory + exact fun σ σ' => h σ σ' () + +private theorem maybeReturnKeepsUserMemory (n t : ObjRef) : + KeepsUserMemory (maybeReturnSc (Z := Unit) n t) := by + have hReschedule := rescheduleKeepsUserMemory + unfold maybeReturnSc + simp only [getTcbObjRef, getNtfnObjRef, getSkObjRef, threadGet, setTcbObjRef, threadSet] + keep_user_memory + +end UserMemory + + +/-- IPC changes only the heap objects the scheduler permits to vary, with the queue and action fields kept. -/ +private theorem schedStepOfIpc {σ σ' : State Unit} + (hfix : SFixA σ σ') (hact : σ'.schedulerAction = σ.schedulerAction) + (hready : σ'.readyQueues = σ.readyQueues) (hrelease : σ'.releaseQueue = σ.releaseQueue) + (heap : ∀ p, σ'.kheap p = σ.kheap p ∨ + (∃ x y, σ.kheap p = some (.tcb x) ∧ σ'.kheap p = some (.tcb y) ∧ + y = { x with tcbState := y.tcbState, tcbArch := y.tcbArch, tcbFault := y.tcbFault }) ∨ + (∃ x y, σ.kheap p = some (.notification x) ∧ σ'.kheap p = some (.notification y) ∧ y.ntfnSc = x.ntfnSc) ∨ + (∃ x y, σ.kheap p = some (.endpoint x) ∧ σ'.kheap p = some (.endpoint y)) ∨ + (∃ x y, σ.kheap p = some (.reply x) ∧ σ'.kheap p = some (.reply y))) : SStepA σ σ' := by + refine ⟨hfix, hact, hready, hrelease, ?_, ?_, ?_, ?_, ?_⟩ + · intro p sc n hsc + rcases heap p with h | ⟨_, _, e, -, -⟩ | ⟨_, _, e, -, -⟩ | ⟨_, _, e, -⟩ | ⟨_, _, e, -⟩ + · exact h + all_goals rw [e] at hsc; cases hsc + · intro p t ht + rcases heap p with h | ⟨x, y, e, e', hy⟩ | ⟨_, _, e, -, -⟩ | ⟨_, _, e, -⟩ | ⟨_, _, e, -⟩ + · exact ⟨t, h.trans ht, rfl, rfl, rfl⟩ + · rw [e] at ht + cases ht + exact ⟨y, e', by rw [hy], by rw [hy], by rw [hy]⟩ + all_goals rw [e] at ht; cases ht + · intro p sc n hsc + rcases heap p with h | ⟨_, _, -, e, -⟩ | ⟨_, _, -, e, -⟩ | ⟨_, _, -, e⟩ | ⟨_, _, -, e⟩ + · exact h.symm + all_goals rw [e] at hsc; cases hsc + · intro p t ht + rcases heap p with h | ⟨x, _, e, -, -⟩ | ⟨_, _, -, e, -⟩ | ⟨_, _, -, e⟩ | ⟨_, _, -, e⟩ + · exact ⟨t, h.symm.trans ht⟩ + · exact ⟨x, e⟩ + all_goals rw [e] at ht; cases ht + · intro p n hn + rcases heap p with h | ⟨_, _, -, e, -⟩ | ⟨x, y, e, e', hy⟩ | ⟨_, _, -, e⟩ | ⟨_, _, -, e⟩ + · exact ⟨n, h.symm.trans hn, rfl⟩ + · rw [e] at hn; cases hn + · rw [e'] at hn; cases hn + exact ⟨x, e, hy⟩ + all_goals rw [e] at hn; cases hn + +private theorem schedFixOfAbs {σ σ' : State Unit} {k : ObjRef → Option KernelObject} {um : Word → Word8} + (he : σ'.toAbstractState = withKM σ.toAbstractState k um) : SFixA σ σ' := + ⟨(by simpa only [withKM] using congrArg AbstractState.curThread he), + (by simpa only [withKM] using congrArg AbstractState.curSc he), + (by simpa only [withKM] using congrArg AbstractState.idleThread he), + (by simpa only [withKM] using congrArg AbstractState.curTime he), + (by simpa only [withKM] using congrArg AbstractState.consumedTime he), + (by simpa only [withKM] using congrArg AbstractState.reprogramTimer he), + (by simpa only [withKM] using congrArg AbstractState.curDomain he), + (by simpa only [withKM] using congrArg AbstractState.domainTime he)⟩ + +theorem endpSchedStep {σ σ' : State Unit} (h : EpStep σ σ') : SStepA σ σ' := by + have e := h.2.1 + refine schedStepOfIpc (schedFixOfAbs e) (by simpa only [withKM] using congrArg AbstractState.schedulerAction e) + (by simpa only [withKM] using congrArg AbstractState.readyQueues e) (by simpa only [withKM] using congrArg AbstractState.releaseQueue e) ?_ + intro p + rcases h.2.2 p with he | ⟨x, y, h0, h1⟩ | ⟨x, y, h0, h1, -⟩ | hT + · exact Or.inl he + · exact Or.inr (Or.inr (Or.inr (Or.inl ⟨x, y, h0, h1⟩))) + · exact Or.inr (Or.inr (Or.inr (Or.inr ⟨x, y, h0, h1⟩))) + · exact Or.inr (Or.inl hT) + +private theorem notifSchedStep {σ σ' : State Unit} (h : Notif.NtfnStep σ σ') : SStepA σ σ' := by + have e := h.2.1 + refine schedStepOfIpc ⟨(by simpa only [withKM] using congrArg AbstractState.curThread e), + (by simpa only [withKM] using congrArg AbstractState.curSc e), + (by simpa only [withKM] using congrArg AbstractState.idleThread e), + (by simpa only [withKM] using congrArg AbstractState.curTime e), + (by simpa only [withKM] using congrArg AbstractState.consumedTime e), + (by simpa only [withKM] using congrArg AbstractState.reprogramTimer e), + (by simpa only [withKM] using congrArg AbstractState.curDomain e), + (by simpa only [withKM] using congrArg AbstractState.domainTime e)⟩ + (by simpa only [withKM] using congrArg AbstractState.schedulerAction e) (by simpa only [withKM] using congrArg AbstractState.readyQueues e) + (by simpa only [withKM] using congrArg AbstractState.releaseQueue e) ?_ + intro p + rcases h.2.2 p with he | ⟨x, y, h0, h1, -, hsc⟩ | hT + · exact Or.inl he + · exact Or.inr (Or.inr (Or.inl ⟨x, y, h0, h1, hsc⟩)) + · exact Or.inr (Or.inl hT) + + +section Placement +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +local notation "wa" => core.num.U64.wrapping_add + +/-- The IPC objects and buffer words in the scheduler's typed heap. -/ +structure IpcSchedPlacementA (th : TypedHeapA) (N E R : Set U64) (Bf : Set (U64 × Word)) : Prop where + notification : ∀ n ∈ N, th n = some (.other 64) + endpoint : ∀ e ∈ E, th e = some (.other 16) + reply : ∀ r ∈ R, th r = some (.other 32) + buffer : ∀ b, BufW Bf b → ∃ p len, th p = some (.other len) ∧ p.val ≤ b.val ∧ b.val < p.val + len + +variable {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + +private theorem typedWordOffStateBlock (H : SchedSRN st Z m κ s σ) {p b : U64} {k : ObjKindA} + (hp : Z.th p = some k) (hne : k ≠ .cnode 0) (hlo : p.val ≤ b.val) (hhi : b.val < p.val + k.size) : + b.val < (L.ksb s).val ∨ (L.ksb s).val + ksSizeA ≤ b.val := by + have h := H.place.ks p k hp (fun irq => hne (H.place.irqK hp irq)) + omega + +private theorem typedWordOffSc (H : SchedSRN st Z m κ s σ) {p b : U64} {k : ObjKindA} + (hp : Z.th p = some k) (hlo : p.val ≤ b.val) (hhi : b.val < p.val + k.size) : + ¬ ScFootA Z.C m σ b := by + intro h + have hd := H.scp.th_out h hp + omega + +private theorem schedReadOff (H : SchedSRN st Z m κ s σ) {b : U64} + (hK : b.val < (L.ksb s).val ∨ (L.ksb s).val + ksSizeA ≤ b.val) + (hS : ¬ ScFootA Z.C m σ b) + (hT : ∀ u ∈ Z.T, ∀ off : U64, off.val < 4096 → + (off = 3520#u64 ∨ off = 3448#u64 ∨ off = 3432#u64 ∨ off = 3464#u64 ∨ + off = 3592#u64 ∨ off = 3480#u64 ∨ off = 3488#u64) → b ≠ wa u off) : + ¬ SReadA st Z m s σ b := by + have hk (off : U64) (ho : off.val < ksSizeA) : b ≠ wa (L.ksb s) off := by + intro eq + have hfit := H.ksFit + have hv : (wa (L.ksb s) off).val = (L.ksb s).val + off.val := by rw [wa_valm]; omega + rw [eq, hv] at hK + omega + rintro (⟨c, hc, off, ho, rfl⟩ | e | e | ⟨u, hu, off, hoff, e⟩ | h) + · have he := H.node_in hc ho + omega + · exact hk 8#u64 (by decide) e + · exact hk 16#u64 (by decide) e + · apply hT u hu off _ hoff e + rcases hoff with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> decide + · exact hS h + +private theorem otherWordOffSched (H : SchedSRN st Z m κ s σ) {p b : U64} {len : Nat} + (hp : Z.th p = some (.other len)) (hlo : p.val ≤ b.val) (hhi : b.val < p.val + len) : + (¬ SReadA st Z m s σ b) ∧ (¬ MWrA (st := st) Z m σ s b) := by + have hK := typedWordOffStateBlock H hp (by intro e; cases e) hlo hhi + have hS := typedWordOffSc H hp hlo hhi + have hT : ∀ t ∈ Z.T, b.val < t.val ∨ t.val + 4096 ≤ b.val := by + intro t ht + have hty := (H.thT t).2 ht + have hne : p ≠ t := by + rintro rfl + rw [hty] at hp + cases hp + have h := H.place.apart p t (.other len) .tcb hp hty hne + change p.val + len ≤ t.val ∨ t.val + 4096 ≤ p.val at h + omega + refine ⟨schedReadOff H hK hS ?_, H.not_mwr hS hT hK⟩ + intro t ht off hof _ eq + have h := hT t ht + rw [eq, Notif.tfield_val (H.core.tcbs t ht) hof] at h + omega + +private theorem tcbFieldOffDistinct (H : SchedSRN st Z m κ s σ) {t b : U64} (ht : t ∈ Z.T) + (hlo : t.val ≤ b.val) (hhi : b.val < t.val + 4096) {u off : U64} (hu : u ∈ Z.T) + (hof : off.val < 4096) (hd : b.val - t.val ≠ off.val) : b ≠ wa u off := by + intro eq + have hev := congrArg UScalar.val eq + rw [Notif.tfield_val (H.core.tcbs u hu) hof] at hev + by_cases htu : t = u + · subst u + omega + · have h := H.place.apart t u .tcb .tcb ((H.thT t).2 ht) ((H.thT u).2 hu) htu + change t.val + 4096 ≤ u.val ∨ u.val + 4096 ≤ t.val at h + omega + +private theorem tcbFieldOffRead (H : SchedSRN st Z m κ s σ) {t b : U64} (ht : t ∈ Z.T) + (hlo : t.val ≤ b.val) (hhi : b.val < t.val + 4096) + (hoff : ∀ k : Nat, k = 3520 ∨ k = 3448 ∨ k = 3432 ∨ k = 3464 ∨ k = 3592 ∨ k = 3480 ∨ k = 3488 → + b.val - t.val ≠ k) : ¬ SReadA st Z m s σ b := by + have hty := (H.thT t).2 ht + refine schedReadOff H (typedWordOffStateBlock H hty (by decide) hlo hhi) (typedWordOffSc H hty hlo hhi) ?_ + intro u hu off ho hd + apply tcbFieldOffDistinct H ht hlo hhi hu ho + apply hoff + rcases hd with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> decide + +private theorem tcbFieldOffWrite (H : SchedSRN st Z m κ s σ) {t b : U64} (ht : t ∈ Z.T) + (hlo : t.val ≤ b.val) (hhi : b.val < t.val + 4096) + (hoff : ∀ k : Nat, k = 3464 ∨ k = 3480 ∨ k = 3488 ∨ k = 3592 → b.val - t.val ≠ k) : + ¬ MWrA (st := st) Z m σ s b := by + have hty := (H.thT t).2 ht + refine not_mwrA (typedWordOffSc H hty hlo hhi) ?_ (typedWordOffStateBlock H hty (by decide) hlo hhi) + intro u hu + exact ⟨tcbFieldOffDistinct H ht hlo hhi hu (off := 3464#u64) (by decide) (hoff _ (by decide)), + tcbFieldOffDistinct H ht hlo hhi hu (off := 3480#u64) (by decide) (hoff _ (by decide)), + tcbFieldOffDistinct H ht hlo hhi hu (off := 3488#u64) (by decide) (hoff _ (by decide)), + tcbFieldOffDistinct H ht hlo hhi hu (off := 3592#u64) (by decide) (hoff _ (by decide))⟩ + +/-- Scheduler writes leave the bound VCPU word untouched. -/ +theorem vcpuWordOffMcsA (H : SchedSRN st Z m κ s σ) {t : U64} (ht : t ∈ Z.T) : + ¬ MWrA (st := st) Z m σ s (wa t 3664#u64) := by + have hv : (wa t 3664#u64).val = t.val + 3664 := + Notif.tfield_val (H.core.tcbs t ht) (K := 3664#u64) (by decide) + exact tcbFieldOffWrite H ht (by rw [hv]; omega) (by rw [hv]; omega) (by rw [hv]; omega) + +private theorem notifFootOffWrite {N : Set U64} (H : SchedSRN st Z m κ s σ) + (hN : ∀ n ∈ N, Z.th n = some (.other 64)) {b : U64} (hb : Notif.NFoot N Z.T b) : + ¬ MWrA (st := st) Z m σ s b := by + rcases hb with ⟨n, hn, hlo, hhi⟩ | ⟨t, ht, h⟩ + · exact (otherWordOffSched H (hN n hn) hlo (by omega)).2 + have hval : b.val = t.val + 3328 ∨ b.val = t.val + 3336 ∨ b.val = t.val + 256 ∨ + b.val = t.val + 3448 ∨ b.val = t.val + 3496 ∨ b.val = t.val + 3504 := by + rcases h with h | h | h | h | h | h <;> + rw [h] <;> + simp only [stA_eq, Notif.tBlockA_eq, Notif.badgeRegA_eq, Notif.prioA_eq', Notif.nextEp_eq, + Notif.prevEp_eq] <;> + rw [Notif.tfield_val (H.core.tcbs t ht)] <;> simp + exact tcbFieldOffWrite H ht (by omega) (by omega) (by intro k hk; omega) + +private theorem notifWriteOffRead {N : Set U64} (H : SchedSRN st Z m κ s σ) + (hN : ∀ n ∈ N, Z.th n = some (.other 64)) {b : U64} (hb : Notif.NWrite N Z.T b) : + ¬ SReadA st Z m s σ b := by + rcases hb with ⟨n, hn, hlo, hhi⟩ | ⟨t, ht, h⟩ + · exact (otherWordOffSched H (hN n hn) hlo (by omega)).1 + have hval : b.val = t.val + 3328 ∨ b.val = t.val + 3336 ∨ b.val = t.val + 256 ∨ + b.val = t.val + 3496 ∨ b.val = t.val + 3504 ∨ b.val = t.val + 3376 := by + rcases h with h | h | h | h | h | h <;> + rw [h] <;> + (try simp only [stA_eq, Notif.tBlockA_eq, Notif.badgeRegA_eq, Notif.nextEp_eq, Notif.prevEp_eq]) <;> + rw [Notif.tfield_val (H.core.tcbs t ht)] <;> simp + exact tcbFieldOffRead H ht (by omega) (by omega) (by intro k hk; omega) + +theorem endpFootOffWrite {N E R : Set U64} {Bf : Set (U64 × Word)} (H : SchedSRN st Z m κ s σ) + (P : IpcSchedPlacementA Z.th N E R Bf) {b : U64} (hb : EFoot Z.T E R Bf b) : + ¬ MWrA (st := st) Z m σ s b := by + rcases hb with ⟨e, he, h⟩ | ⟨r, hr, h⟩ | ⟨t, ht, h | h⟩ | h + · exact (otherWordOffSched H (P.endpoint e he) (by omega) (by omega)).2 + · exact (otherWordOffSched H (P.reply r hr) (by omega) (by omega)).2 + · obtain ⟨hlo, hoff⟩ := h + unfold TOffN at hoff + exact tcbFieldOffWrite H ht hlo (by omega) (by intro k hk; omega) + · unfold TL at h + exact tcbFieldOffWrite H ht (by omega) (by omega) (by intro k hk; omega) + · obtain ⟨p, len, hp, hlo, hhi⟩ := P.buffer b h + exact (otherWordOffSched H hp hlo hhi).2 + +theorem endpWriteOffRead {N E R : Set U64} {Bf : Set (U64 × Word)} (H : SchedSRN st Z m κ s σ) + (P : IpcSchedPlacementA Z.th N E R Bf) {b : U64} (hb : EWrite Z.T E R Bf b) : + ¬ SReadA st Z m s σ b := by + rcases hb with ⟨e, he, h⟩ | ⟨r, hr, h⟩ | ⟨t, ht, h | h⟩ | h + · exact (otherWordOffSched H (P.endpoint e he) (by omega) (by omega)).1 + · exact (otherWordOffSched H (P.reply r hr) (by omega) (by omega)).1 + · obtain ⟨hlo, hoff⟩ := h + unfold TWOffN at hoff + exact tcbFieldOffRead H ht hlo (by omega) (by intro k hk; omega) + · unfold TL at h + exact tcbFieldOffRead H ht (by omega) (by omega) (by intro k hk; omega) + · obtain ⟨p, len, hp, hlo, hhi⟩ := P.buffer b h + exact (otherWordOffSched H hp hlo hhi).1 + +/-- A word in a placed reply object is separate from scheduler reads and writes. -/ +theorem replyWordOffSchedA {N E R : Set U64} {Bf : Set (U64 × Word)} (H : SchedSRN st Z m κ s σ) + (P : IpcSchedPlacementA Z.th N E R Bf) {r b : U64} (hr : r ∈ R) + (hlo : r.val ≤ b.val) (hhi : b.val < r.val + 32) : + (¬ SReadA st Z m s σ b) ∧ (¬ MWrA (st := st) Z m σ s b) := + otherWordOffSched H (P.reply r hr) hlo hhi + +end Placement + +section Scheduler +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +/-- IPC scheduler steps can change the ready and release queue ghosts. -/ +abbrev IpcSchedRelA (st : machine.Store S) [LawfulStore st] (Z : XSetsA) (m : McState) (κ : U64 → U64) + (s : S) (σ : State Unit) : Prop := + ∃ Q R, SchedSRA st { Z with Q := Q, R := R } m κ s σ + +private theorem notifStateWords {N T : Set U64} {s : S} {σ : State Unit} + (h : Notif.NtfnStateRel N T (L.mem s) σ) : StEqA T (L.mem s) σ := + fun _ ht _ hx => (h.at_tcb ht hx).st + +private theorem endpStateWords {T E R : Set U64} {Bf : Set (U64 × Word)} {s : S} {σ : State Unit} + (h : EpStateRel T E R Bf (L.mem s) σ) : StEqA T (L.mem s) σ := + fun _ ht _ hx => (h.at_tcb ht hx).st + +theorem notifScWord {N T : Set U64} {m : McState} {s : S} {σ : State Unit} + {n : U64} (hn : n ∈ N) (h : Notif.NtfnStateRel N T (L.mem s) σ) + (hI : ScInvA m σ) (hc : m.curCore < fl4A64Platform.numNodes) : NtfnScEqA m n (L.mem s) σ := by + obtain ⟨x, hx, hrel⟩ := h.ntfn n hn + refine ⟨x, hx, ?_⟩ + have h := hrel.sc + change x.ntfnSc = Notif.optRef (L.mem s (nScSc n)) at h + by_cases h0 : L.mem s (nScSc n) = 0#u64 + · simpa [optScA, h0, Notif.optRef] using h + · have hnIdle : specPtr (L.mem s (nScSc n)) ≠ m.idleScOf m.curCore := by + intro e + exact hI.ntfnIdleOf _ _ hx _ hc (h.trans (by simp [Notif.optRef, h0, e])) + simpa [optScA, scRenA, Notif.optRef, h0, hnIdle] using h + +theorem notifKView {σ σ' : State Unit} (h : SKeepA σ σ') : Notif.KView σ σ' := by + refine ⟨fun p x hx => h.obj p _ hx (by intro _ _ e; cases e) (by intro _ e; cases e), ?_⟩ + intro p x hx + obtain ⟨y, hy, sc, rfl⟩ := h.tcb p x hx + exact ⟨_, hy, rfl, rfl, rfl⟩ + +theorem endpKView {σ σ' : State Unit} (h : SKeepA σ σ') + (hmem : σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory) : KView σ σ' := by + refine ⟨fun p x hx => h.obj p _ hx (by intro _ _ e; cases e) (by intro _ e; cases e), ?_, ?_, hmem⟩ + · intro p x hx + exact ⟨x, h.obj p _ hx (by intro _ _ e; cases e) (by intro _ e; cases e), rfl⟩ + · intro p x hx + obtain ⟨y, hy, sc, rfl⟩ := h.tcb p x hx + exact ⟨_, hy, rfl, rfl, rfl, rfl, rfl⟩ + +/-- The concrete and spec frames on an MCS step preserve the endpoint relation. -/ +theorem epFrameOfMcsA {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} + {Bf : Set (U64 × Word)} {s s' : S} {σ σ' : State Unit} + (H : SchedSRN st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) + (F : MFrA (st := st) Z m s s' σ σ') (hm : σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory) : + FootEq Z.T E R Bf (L.mem s) (L.mem s') ∧ KView σ σ' := + ⟨fun b ha hb => F.wr.mem_eq ha (endpFootOffWrite H P hb), endpKView F.keep hm⟩ + +/-- An unqueued receiver may return its bound notification's SC before endpoint dispatch. -/ +theorem maybeReturnIpcA {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} + {Bf : Set (U64 × Word)} {s : S} {σ : State Unit} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) + (PE : EPlaced Z.T E R Bf) (PN : Notif.NPlaced Z.T N) (hc : m.curCore < fl4A64Platform.numNodes) + (HN : Notif.NtfnStateRel N Z.T (L.mem s) σ) (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + {n t : U64} (hn : n ∈ N) (ht : t ∈ Z.T) (hq : specPtr t ∉ σ.releaseQueue) : + notification.maybe_return_sched_context st s n t ⦃ s' => ∃ σ' Q, + maybeReturnSc (Z := Unit) (specPtr n) (specPtr t) σ = ({((), σ')}, False) ∧ + SchedSRA st { Z with Q := Q } m κ s' σ' ∧ + Notif.NtfnStateRel N Z.T (L.mem s') σ' ∧ EpStateRel Z.T E R Bf (L.mem s') σ' ∧ + MFrA (st := st) Z m s s' σ σ' ⦄ := by + refine WP.spec_mono (maybe_return_unqueuedA H (endpStateWords HE) ht (notifScWord hn HN H.inv hc) hq) ?_ + rintro s' ⟨σ', Q, h, H', F⟩ + have hm := maybeReturnKeepsUserMemory (specPtr n) (specPtr t) σ σ' () (by rw [h]; exact Set.mem_singleton _) + have frames := epFrameOfMcsA H.toSchedSRN P F hm + have fn : Notif.FootEq N Z.T (L.mem s) (L.mem s') := fun b ha hb => + F.wr.mem_eq ha (notifFootOffWrite H.toSchedSRN P.notification hb) + exact ⟨σ', Q, h, H', HN.frame PN fn (notifKView F.keep), HE.frame PE frames.1 frames.2, F⟩ + +theorem completeTailFromSched {Z : XSetsA} {m : McState} {κ : U64 → U64} {N : Set U64} + {s : S} {σ : State Unit} {t n : U64} (H : SchedSRA st Z m κ s σ) (a : A) + (hP : Notif.NPlaced Z.T N) (hc : m.curCore < fl4A64Platform.numNodes) + (hmb : sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hRN : Notif.NtfnStateRel N Z.T (L.mem s) σ) (ht : t ∈ Z.T) (hn : n ∈ N) (hN : Z.th n = some (.other 64)) + (hq : Notif.NotQueued σ (specPtr t)) (hr : ∀ x, σ.kheap (specPtr t) = some (.tcb x) → runnable x.tcbState = true) + (hon : L.mem s (affA t) = L.cpu s) (hd : DonLocalA (st := st) Z.C s n) : + Notif.rustCompleteTail st ar s a t n ⦃ r => r.2 = a ∧ ∃ σ' R', + Notif.specCompleteTail (specPtr t) (specPtr n) σ = ({((), σ')}, False) ∧ + SchedSRA st { Z with R := R' } m κ r.1 σ' ∧ MFrA (st := st) Z m s r.1 σ σ' ⦄ := by + have ha := hP.ntfn n hn + have hfield : (nScSc n).val = n.val + 32 := by + unfold nScSc layout.ntfn.SC + exact Notif.nfield_val ha (by decide) + have hal : Aligned (nScSc n) := by unfold Aligned; rw [hfield]; have := ha.align; omega + have hS : ¬ ScFootA Z.C m σ (nScSc n) := fun h => by + have e := H.scp.th_out h hN + change (nScSc n).val < n.val ∨ n.val + 64 ≤ (nScSc n).val at e + omega + have hT : ∀ u ∈ Z.T, (nScSc n).val < u.val ∨ u.val + 4096 ≤ (nScSc n).val := by + intro u hu + have e := hP.disj n hn u hu + omega + have hK : (nScSc n).val < (L.ksb s).val ∨ (L.ksb s).val + ksSizeA ≤ (nScSc n).val := by + have irq : ¬ IrqNodeA (L.ksb s) n := fun h => by + have he := H.place.irqK hN h + cases he + have e := H.place.ks n (.other 64) hN irq + change n.val + 64 ≤ (L.ksb s).val ∨ (L.ksb s).val + ksSizeA ≤ n.val at e + omega + exact complete_tail_frameA H a hmb ht hon (notifScWord hn hRN H.inv hc) + (notifStateWords hRN) hq.1 hq.2 hr hd hal hS hT hK + +/-- Carry both IPC frames through the concrete MCS completion tail at the scheduler relation. -/ +theorem completeTailFrameA_of_sched {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} + {Bf : Set (U64 × Word)} (hP : IPlacedA N Z.T E R Bf) (hc : m.curCore < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hN : ∀ n ∈ N, Z.th n = some (.other 64)) + (hnf : ∀ Q R s σ, SchedSRA st { Z with Q := Q, R := R } m κ s σ → + ∀ b, Notif.NFoot N Z.T b → ¬ MWrA (st := st) Z m σ s b) + (hef : ∀ Q R' s σ, SchedSRA st { Z with Q := Q, R := R' } m κ s σ → + ∀ b, EFoot Z.T E R Bf b → ¬ MWrA (st := st) Z m σ s b) : + Notif.CompleteTailFrameA st ar N Z.T (IpcSchedRelA st Z m κ) (DonLocalA (st := st) Z.C) + (fun s σ => EpStateRel Z.T E R Bf (L.mem s) σ) := by + rintro s a σ t n hRN ⟨Q, R', H⟩ ht hn hq hr hon hd hRE + refine WP.spec_mono (completeTailFromSched H a hP.n hc (hmb a) hRN ht hn (hN n hn) hq hr hon hd) ?_ + rintro r ⟨_, σ', R'', hstep, H', F⟩ + have hm : ((), σ') ∈ (Notif.specCompleteTail (specPtr t) (specPtr n) σ).1 := by + rw [hstep] + exact Set.mem_singleton _ + have hNf : Notif.FootEq N Z.T (L.mem s) (L.mem r.1) := fun b ha hb => + F.wr.mem_eq ha (hnf Q R' s σ H b hb) + have hEf : FootEq Z.T E R Bf (L.mem s) (L.mem r.1) := fun b ha hb => + F.wr.mem_eq ha (hef Q R' s σ H b hb) + exact ⟨by rw [hstep]; simp, σ', hm, hNf, notifKView F.keep, ⟨Q, R'', H'⟩, + hRE.frame hP.e hEf (endpKView F.keep (completeTailKeepsUserMemory _ _ σ σ' () hm))⟩ + + +/-- The notification stability and blocking laws are the scheduler's own invariant laws. -/ +theorem schedHypsA_of_sched {Z : XSetsA} {m : McState} {κ : U64 → U64} {N : Set U64} + (hOff : ∀ Q R s σ, SchedSRA st { Z with Q := Q, R := R } m κ s σ → + ∀ b, Notif.NWrite N Z.T b → ¬ SReadA st Z m s σ b) : + Notif.SchedHypsA st N Z.T (IpcSchedRelA st Z m κ) := by + constructor + · rintro s s' σ σ' ⟨Q, R, H⟩ hw hs hb + exact ⟨Q, R, H.stable hw (hOff Q R s σ H) (notifSchedStep hs) hb⟩ + · rintro s σ t x ⟨Q, R, H⟩ hk hb + exact H.notQueued hk hb + · rintro s s' σ σ' t ⟨Q, R, H⟩ hnq hrib _ _ hw hs hk + exact ⟨Q, R, H.block hw (hOff Q R s σ H) (notifSchedStep hs) hnq hrib hk⟩ + +theorem swAction {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRA st Z m κ s σ) : + MWrA (st := st) Z m σ s (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) := + Or.inr (Or.inl (H.node_sks H.sch.cpuCs (by simp [layout.node.SCHED_ACTION]))) + +private theorem notifTailResult {Z : XSetsA} {m : McState} {κ : U64 → U64} {N : Set U64} + {s s' : S} {σ σ' : State Unit} {op : SMonad Unit Unit} + (hs : op σ = ({((), σ')}, False)) (hSR : IpcSchedRelA st Z m κ s' σ') + (hw : WritesIn st s s' (MWrA (st := st) Z m σ s)) (keep : SKeepA σ σ') (hOff : ∀ b, Notif.NFoot N Z.T b → ¬ MWrA (st := st) Z m σ s b) : + ¬ (op σ).2 ∧ ∃ σ₁, ((), σ₁) ∈ (op σ).1 ∧ Notif.FootEq N Z.T (L.mem s) (L.mem s') ∧ + Notif.KView σ σ₁ ∧ IpcSchedRelA st Z m κ s' σ₁ := by + rw [hs] + exact ⟨by simp, σ', Set.mem_singleton _, fun b ha hb => hw.mem_eq ha (hOff b hb), + notifKView keep, hSR⟩ + +private theorem endpTailResult {Z : XSetsA} {m : McState} {κ : U64 → U64} {E R : Set U64} + {Bf : Set (U64 × Word)} {s s' : S} {σ σ' : State Unit} {op : SMonad Unit Unit} + (hs : op σ = ({((), σ')}, False)) (hSR : IpcSchedRelA st Z m κ s' σ') + (hw : WritesIn st s s' (MWrA (st := st) Z m σ s)) (keep : SKeepA σ σ') (hOff : ∀ b, EFoot Z.T E R Bf b → ¬ MWrA (st := st) Z m σ s b) + (hm : KeepsUserMemory op) : + ¬ (op σ).2 ∧ ∃ σ₁, ((), σ₁) ∈ (op σ).1 ∧ FootEq Z.T E R Bf (L.mem s) (L.mem s') ∧ + KView σ σ₁ ∧ IpcSchedRelA st Z m κ s' σ₁ := by + have hres : ((), σ') ∈ (op σ).1 := by rw [hs]; exact Set.mem_singleton _ + refine ⟨by rw [hs]; simp, σ', hres, fun b ha hb => hw.mem_eq ha (hOff b hb), + endpKView keep (hm σ σ' () hres), hSR⟩ + +private theorem restartKeepsUserMemory (t : ObjRef) : KeepsUserMemory (specRestartTail t) := by + unfold specRestartTail + refine KeepsUserMemory.bind ?_ fun _ => + KeepsUserMemory.bind (condRefillKeepsUserMemory _ _ _) fun _ => switchKeepsUserMemory t + unfold getTcbObjRef threadGet + exact KeepsUserMemory.bind (KeepsUserMemory.getsThe _) fun _ => KeepsUserMemory.pure _ + +/-- The scheduler operations at the endpoint interface. -/ +theorem schedOpsEA_of_sched {Z : XSetsA} {m : McState} {κ : U64 → U64} {E R : Set U64} {Bf : Set (U64 × Word)} + (hRead : ∀ Q R' s σ, SchedSRA st { Z with Q := Q, R := R' } m κ s σ → + ∀ b, EWrite Z.T E R Bf b → ¬ SReadA st Z m s σ b) + (hWrite : ∀ Q R' s σ, SchedSRA st { Z with Q := Q, R := R' } m κ s σ → + ∀ b, EFoot Z.T E R Bf b → ¬ MWrA (st := st) Z m σ s b) : + SchedOpsEA st Z.T E R Bf (IpcSchedRelA st Z m κ) := by + constructor + · rintro s s' σ σ' ⟨Q, R', H⟩ hw hs hb + exact ⟨Q, R', H.stable hw (hRead Q R' s σ H) (endpSchedStep hs) hb⟩ + · rintro s σ t hRel ⟨Q, R', H⟩ _ + refine WP.spec_mono (schedule_tcb_refinesA H t (endpStateWords hRel)) ?_ + rintro s' ⟨hw, H'⟩ + exact ⟨fun b hb hf => hw.mem_eq hb (fun eq => hWrite Q R' s σ H b hf (eq.symm ▸ swAction (Z := { Z with Q := Q, R := R' }) H)), + ⟨Q, R', H'⟩⟩ + · rintro s σ t x hRel ⟨Q, R', H⟩ ht hk hr _ hon hq + refine WP.spec_mono (restart_tail_localA H ht hk hr hon (endpStateWords hRel) hq.2) ?_ + rintro s' ⟨σ', Q', hs, H', F⟩ + exact endpTailResult hs ⟨Q', R', H'⟩ F.wr F.keep (hWrite Q R' s σ H) (restartKeepsUserMemory _) + · rintro s σ hRel ⟨Q, R', H⟩ + refine WP.spec_mono (reschedule_required_refinesA H (endpStateWords hRel)) ?_ + rintro s' ⟨σ', Q', hs, hk, _, hw, H'⟩ + exact endpTailResult hs ⟨Q', R', H'⟩ (hw.mono (fun _ e => Or.inr e)) (SKeepA.of_kheap hk) (hWrite Q R' s σ H) rescheduleKeepsUserMemory + · rintro s σ t x ⟨Q, R', H⟩ hk hr + exact H.rd t x hk hr + · rintro s s' σ σ' ⟨Q, R', H⟩ ⟨Q', R'', H'⟩ + exact H.coreKeepA H' rfl + · rintro s σ t x ⟨Q, R', H⟩ hk hr + exact H.notQueued hk hr + · rintro s s' σ σ' t ⟨Q, R', H⟩ hnq hrib _ _ hw hs hk + exact ⟨Q, R', H.block hw (hRead Q R' s σ H) (endpSchedStep hs) hnq hrib hk⟩ + · rintro s σ t x _ ⟨Q, R', H⟩ ht hk hr _ + refine WP.spec_mono (unblock_sporadic_blockedA H ht hk hr) ?_ + rintro s' ⟨σ', hs, H', F⟩ + have hm : ((), σ') ∈ (ifSporadicCurScAssertRefillUnblockCheck (Z := Unit) x.tcbSchedContext σ).1 := by + rw [hs] + exact Set.mem_singleton _ + exact ⟨σ', hs, fun b hb hf => F.wr.mem_eq hb (hWrite Q R' s σ H b hf), + endpKView F.keep (condRefillKeepsUserMemory _ _ _ _ _ _ hm), ⟨Q, R', H'⟩⟩ + · rintro s σ t x hRel ⟨Q, R', H⟩ ht hk hr _ hon _ + refine WP.spec_mono (switch_to_runA H ht hk hr hon (endpStateWords hRel)) ?_ + rintro s' ⟨σ', Q', hs, H', F⟩ + exact endpTailResult hs ⟨Q', R', H'⟩ F.wr F.keep (hWrite Q R' s σ H) (switchKeepsUserMemory _) + + +/-- The notification scheduler interface using the same scheduler relation as the endpoint interface. -/ +theorem schedOpsA_of_sched {Z : XSetsA} {m : McState} {κ : U64 → U64} {N : Set U64} + (hP : Notif.NPlaced Z.T N) (hc : m.curCore < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hN : ∀ n ∈ N, Z.th n = some (.other 64)) + (hWrite : ∀ Q R s σ, SchedSRA st { Z with Q := Q, R := R } m κ s σ → + ∀ b, Notif.NFoot N Z.T b → ¬ MWrA (st := st) Z m σ s b) : + Notif.SchedOpsA st ar N Z.T (IpcSchedRelA st Z m κ) (DonLocalA (st := st) Z.C) := by + constructor + · rintro s σ t hRel ⟨Q, R, H⟩ _ + refine WP.spec_mono (schedule_tcb_refinesA H t (notifStateWords hRel)) ?_ + rintro s' ⟨hw, H'⟩ + exact ⟨fun b hb hf => hw.mem_eq hb (fun eq => hWrite Q R s σ H b hf (eq.symm ▸ swAction (Z := { Z with Q := Q, R := R }) H)), + ⟨Q, R, H'⟩⟩ + · rintro s a σ t n x hRel ⟨Q, R, H⟩ ht hn hk hrun hq hon hd + have hr : ∀ y, σ.kheap (specPtr t) = some (.tcb y) → runnable y.tcbState = true := by + intro y hy + rw [hk] at hy + cases hy + rw [hrun] + rfl + refine WP.spec_mono (wake_tail_frameA H a (hmb a) ht hon (notifScWord hn hRel H.inv hc) + (notifStateWords hRel) hq.1 hq.2 hr hd) ?_ + rintro r ⟨_, σ', Q', R', hs, H', F⟩ + exact notifTailResult hs ⟨Q', R', H'⟩ F.wr F.keep (hWrite Q R s σ H) + · rintro s a σ t n hRel ⟨Q, R, H⟩ ht hn hq hr hon hd + refine WP.spec_mono (recv_tail_frameA H a (hmb a) ht hon (notifScWord hn hRel H.inv hc) + (notifStateWords hRel) hq.1 hq.2 hr hd) ?_ + rintro r ⟨_, σ', R', hs, H', F⟩ + exact notifTailResult hs ⟨Q, R', H'⟩ F.wr F.keep (hWrite Q R s σ H) + · rintro s a σ t n hRel ⟨Q, R, H⟩ ht hn hq hr hon hd + refine WP.spec_mono (completeTailFromSched H a hP hc (hmb a) hRel ht hn (hN n hn) hq hr hon hd) ?_ + rintro r ⟨_, σ', R', hs, H', F⟩ + exact notifTailResult hs ⟨Q, R', H'⟩ F.wr F.keep (hWrite Q R s σ H) + · rintro s _ σ t n hRel ⟨Q, R, H⟩ ht hn _ hb _ + obtain ⟨_, y, _, hy, _, hnr⟩ := hb.2.2.2.2.2 + have hrun : ∀ z, σ.kheap (specPtr t) = some (.tcb z) → runnable z.tcbState = false := by + intro z hz + rw [hy] at hz + cases hz + exact hnr + refine WP.spec_mono (maybe_return_frameA H (notifStateWords hRel) ht (notifScWord hn hRel H.inv hc) hrun) ?_ + rintro s' ⟨σ', Q', hs, H', hw, hk, _⟩ + exact notifTailResult hs ⟨Q', R, H'⟩ hw hk (hWrite Q R s σ H) + · rintro s σ t x hRel ⟨Q, R, H⟩ ht hk hr _ hon hq + have hrun : ∀ z, σ.kheap (specPtr t) = some (.tcb z) → runnable z.tcbState = true := by + intro z hz + rw [hk] at hz + cases hz + rw [hr] + rfl + refine WP.spec_mono (restart_tail_frameA H ht hon hrun (notifStateWords hRel) hq.2) ?_ + rintro s' ⟨σ', Q', hs, H', F⟩ + exact notifTailResult hs ⟨Q', R, H'⟩ F.wr F.keep (hWrite Q R s σ H) + · rintro s σ hRel ⟨Q, R, H⟩ + refine WP.spec_mono (reschedule_required_refinesA H (notifStateWords hRel)) ?_ + rintro s' ⟨σ', Q', hs, hk, _, hw, H'⟩ + exact notifTailResult hs ⟨Q', R, H'⟩ (hw.mono (fun _ e => Or.inr e)) (SKeepA.of_kheap hk) (hWrite Q R s σ H) + · rintro s s' σ σ' ⟨Q, R, H⟩ ⟨Q', R', H'⟩ + exact H.coreKeepA H' rfl + · rintro s s' σ σ' n _ _ _ ⟨Q, R, H⟩ ⟨Q', R', H'⟩ hn hd + exact H.donLocalKeepA H' rfl hn hd + +end Scheduler + +/-- The idle TCBs on every platform core exist and have no fault. -/ +def IdleNoFaultA (m : McState) (σ : State Unit) : Prop := + ∀ c < fl4A64Platform.numNodes, ∃ x, σ.kheap (m.idleThreadOf c) = some (.tcb x) ∧ x.tcbFault = none + +theorem IdleNoFaultA.notIdle {m : McState} {σ : State Unit} (hi : IdleNoFaultA m σ) + {t : ObjRef} {x : Tcb} {b : Word} (hk : σ.kheap t = some (.tcb x)) (hf : x.tcbFault = some (.timeout b)) : + ∀ c < fl4A64Platform.numNodes, t ≠ m.idleThreadOf c := by + intro c hc e + obtain ⟨xi, hki, hfi⟩ := hi c hc + rw [e, hki] at hk + cases hk + rw [hfi] at hf + cases hf + +/-- The MCS heap frame keeps every idle TCB's fault field. -/ +theorem IdleNoFaultA.of_skeep {m : McState} {σ σ' : State Unit} (hi : IdleNoFaultA m σ) + (hkeep : SKeepA σ σ') : IdleNoFaultA m σ' := by + intro c hc + obtain ⟨x, hx, hf⟩ := hi c hc + obtain ⟨y, hy, sc, rfl⟩ := hkeep.tcb _ x hx + exact ⟨_, hy, hf⟩ + +/-- The timeout-update field at the scheduler relation, with the non-idle fact supplied by its caller. -/ +theorem timeoutScOpA_of_sched {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + {Z : XSetsA} {m : McState} {κ : U64 → U64} {E R : Set U64} {Bf : Set (U64 × Word)} + (hscOut : ∀ s σ, SchedSRA st Z m κ s σ → ∀ a, EFoot Z.T E R Bf a → ¬ ScFootA Z.C m σ a) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) : + TimeoutScOpA st ar Z.T E R Bf (SchedSRA st Z m κ) + (fun t => ∀ c < fl4A64Platform.numNodes, specPtr t ≠ m.idleThreadOf c) := by + constructor + intro s a σ t _ H ht hni h0 + have hname := H.scName_of_notIdle ht h0 hni + obtain ⟨_, hup⟩ := update_consumed_tcbA H a ht h0 hmax (htu a) + refine WP.spec_mono hup ?_ + rintro r ⟨y, n, v, hky, hspec, _, hw, H'⟩ + have hwk : ∀ y2 n2, σ.kheap (specPtr (L.mem s (scA t))) = some (.schedContext y2 n2) → WritesIn st s r.2 + (fun b => (L.mem s (scA t)).val ≤ b.val ∧ b.val < (L.mem s (scA t)).val + 2 ^ (n2 + minSchedContextBits)) := by + intro y2 n2 h2 + rw [← hname, hky] at h2 + cases h2 + exact hw + let p := scNameA m (L.mem s (scA t)) + let σ' := updKSc σ p (.schedContext { y with scConsumed := v.bv } n) + have hkeep : ∀ q o, σ.kheap q = some o → (∀ z k, o ≠ .schedContext z k) → σ'.kheap q = some o := by + intro q o hq ho + have hne : q ≠ p := by + intro eq + change σ.kheap p = _ at hky + rw [eq, hky] at hq + cases hq + exact ho y n rfl + simp [σ', updKSc, hne, hq] + have hview : KView σ σ' := by + refine ⟨?_, ?_, ?_, rfl⟩ + · exact fun q z hz => hkeep q _ hz (by intro z k e; cases e) + · exact fun q z hz => ⟨z, hkeep q _ hz (by intro z k e; cases e), rfl⟩ + · exact fun q z hz => ⟨z, hkeep q _ hz (by intro z k e; cases e), rfl, rfl, rfl, rfl, rfl⟩ + refine ⟨σ', ?_, ?_, hview, H', hwk⟩ + · rw [← hname]; exact hspec + · intro w hwA hwE + exact hw.2.2 w hwA (fun hwSc => hscOut s σ H w hwE ⟨_, H.rest.scMem t ht h0, y, n, hky, hwSc⟩) + +/-- The timeout update on the scheduler relation with existential ready and release queues. -/ +theorem timeoutScOpA_of_ipcSched {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + {Z : XSetsA} {m : McState} {κ : U64 → U64} {E R : Set U64} {Bf : Set (U64 × Word)} + (hscOut : ∀ Q R' s σ, SchedSRA st { Z with Q := Q, R := R' } m κ s σ → + ∀ a, EFoot Z.T E R Bf a → ¬ ScFootA Z.C m σ a) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) : + TimeoutScOpA st ar Z.T E R Bf (IpcSchedRelA st Z m κ) + (fun t => ∀ c < fl4A64Platform.numNodes, specPtr t ≠ m.idleThreadOf c) := by + constructor + rintro s a σ t x ⟨Q, R', H⟩ ht hni h0 + have hop := timeoutScOpA_of_sched (st := st) (ar := ar) (Z := { Z with Q := Q, R := R' }) + (hscOut Q R') hmax htu + refine WP.spec_mono (hop.upd s a σ t x H ht hni h0) ?_ + rintro r ⟨σ', hm, hf, hv, H', hw⟩ + exact ⟨σ', hm, hf, hv, ⟨Q, R', H'⟩, hw⟩ + +/-- The IPC scheduler interfaces and the active-receive frame from placed IPC objects and `SchedSRA`. -/ +theorem ipcSchedOpsA {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + (hP : IPlacedA N Z.T E R Bf) (hp : IpcSchedPlacementA Z.th N E R Bf) + (hc : m.curCore < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) : + Notif.SchedHypsA st N Z.T (IpcSchedRelA st Z m κ) ∧ + Notif.SchedOpsA st ar N Z.T (IpcSchedRelA st Z m κ) (DonLocalA (st := st) Z.C) ∧ + SchedOpsEA st Z.T E R Bf (IpcSchedRelA st Z m κ) ∧ + TimeoutScOpA st ar Z.T E R Bf (IpcSchedRelA st Z m κ) + (fun t => ∀ c < fl4A64Platform.numNodes, specPtr t ≠ m.idleThreadOf c) ∧ + Notif.CompleteTailFrameA st ar N Z.T (IpcSchedRelA st Z m κ) (DonLocalA (st := st) Z.C) + (fun s σ => EpStateRel Z.T E R Bf (L.mem s) σ) := by + have hn := fun Q R s σ (H : SchedSRA st { Z with Q := Q, R := R } m κ s σ) b hb => + notifFootOffWrite H.toSchedSRN hp.notification (b := b) hb + have he := fun Q R s σ (H : SchedSRA st { Z with Q := Q, R := R } m κ s σ) b hb => + endpFootOffWrite H.toSchedSRN hp (b := b) hb + exact ⟨schedHypsA_of_sched (fun _ _ _ _ H _ hb => notifWriteOffRead H.toSchedSRN hp.notification hb), + schedOpsA_of_sched hP.n hc hmb hp.notification hn, + schedOpsEA_of_sched (fun _ _ _ _ H _ hb => endpWriteOffRead H.toSchedSRN hp hb) he, + timeoutScOpA_of_ipcSched (fun Q R s σ H b hb hf => he Q R s σ H b hb (Or.inl hf)) hmax htu, + completeTailFrameA_of_sched hP hc hmb hp.notification hn he⟩ + +/-- The concrete timeout transfer changes only the receiver's registers and the SC in the heap. -/ +theorem IdleNoFaultA.of_timeout_transfer {m : McState} {σ σ' : State Unit} (hi : IdleNoFaultA m σ) + {rcv sc : ObjRef} {xr xr' : Tcb} {y : SchedContext} {n : Nat} + (hr : σ.kheap rcv = some (.tcb xr)) (hsc : σ.kheap sc = some (.schedContext y n)) + (hkeep : ∀ p, p ≠ rcv → p ≠ sc → σ'.kheap p = σ.kheap p) + (hr' : σ'.kheap rcv = some (.tcb xr')) (hxr' : xr' = { xr with tcbArch := xr'.tcbArch }) : + IdleNoFaultA m σ' := by + intro c hc + obtain ⟨x, hx, hf⟩ := hi c hc + by_cases he : m.idleThreadOf c = rcv + · rw [he, hr] at hx + cases hx + exact ⟨xr', he.symm ▸ hr', by rw [hxr']; exact hf⟩ + · have hs : m.idleThreadOf c ≠ sc := by + intro eq + rw [eq, hsc] at hx + cases hx + exact ⟨x, (hkeep _ he hs).trans hx, hf⟩ + +/-- The specification's `w0` TCBs start without faults. -/ +theorem w0_idleNoFaultA {kb ib now mt : U64} {nc : Nat} (hL : BootLayA ib nc) + (hnn : fl4A64Platform.numNodes ≤ nc) : IdleNoFaultA (bootMA ib nc) (bootSA kb ib nc now mt) := by + intro c hc + have hcn := lt_of_lt_of_le hc hnn + exact ⟨_, bootKA_tcb (now := now) (mt := mt) hL hcn, rfl⟩ + +/-- The specification's `w1` retains the idle TCBs and their empty faults. -/ +theorem w1_idleNoFaultA {kb ib now mt sl : U64} {nc : Nat} (hL : BootLayA ib nc) + (P : Fl4ProofsA64.P1.W7.R.ST.BootWit.BootLay1 kb ib nc) (hnn : fl4A64Platform.numNodes ≤ nc) : + IdleNoFaultA (w1MA ib nc) (w1SA kb ib nc now mt sl) := by + intro c hc + have hcn := lt_of_lt_of_le hc hnn + exact ⟨_, w1KA_idle (now := now) (mt := mt) (sl := sl) hL P hcn, rfl⟩ + +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/IpcScPopA.lean b/proofs/Fl4ProofsA64/P3/IpcScPopA.lean new file mode 100644 index 000000000..f968f51b0 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/IpcScPopA.lean @@ -0,0 +1,169 @@ +/- +P3 (AArch64): the reply pop at the IPC and scheduler join. +The final TCB unlink is shared with reply removal below a stack head. +-/ +import Fl4ProofsA64.P3.IpcScPopLinksA +import Fl4ProofsA64.P3.EpCancelIpcA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4SpecA64 Sched + +noncomputable section + +/-- The spec state after clearing the reply's TCB and making that TCB inactive. -/ +def replyUnlinkStateA (σ : State Unit) (t r : ObjRef) (x : Tcb) (xr : Reply) : State Unit := + stsAct (updK (updK σ r (.reply { xr with replyTcb := none })) t + (.tcb { x with tcbState := .inactive })) t + +private theorem unlinkTcbSpecA {σ : State Unit} {t r : ObjRef} {x : Tcb} {xr : Reply} + (hk : σ.kheap t = some (.tcb x)) (hr : σ.kheap r = some (.reply xr)) + (hback : xr.replyTcb = some t) + (hblock : x.tcbState = .blockedOnReply r ∨ ∃ ep d, x.tcbState = .blockedOnReceive ep (some r) d) : + replyUnlinkTcb (Z := Unit) t r σ = ({((), replyUnlinkStateA σ t r x xr)}, False) := by + have hrt : r ≠ t := by intro h; rw [h, hk] at hr; cases hr + have hk' : (updK σ r (.reply { xr with replyTcb := none })).kheap t = some (.tcb x) := by + rw [updK_kheap, Function.update_of_ne hrt.symm, hk] + unfold replyUnlinkTcb replyUnlinkStateA + rw [nd_bind_one (getReply_eq hr), nd_bind_one (assert_true hback σ), nd_bind_one (getThreadState_eq hk), + nd_bind_one (assert_true hblock σ), nd_bind_one (setReplyTcb_eq none hr)] + exact setThreadState_eq .inactive hk' + +section Store +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + +theorem popStateWords {s : S} {σ : State Unit} (H : EpStateRel Z.T E R Bf (L.mem s) σ) : + StEqA Z.T (L.mem s) σ := + fun _ ht _ hx => (H.at_tcb ht hx).st + +theorem nextRawEq (s : S) (r : U64) : + obj.Reply.next_raw st r s = ok (L.mem s (rNextA r)) := by + unfold obj.Reply.next_raw + simp [lift, bind_ok, L.read_eq, rNextA, layout.reply.NEXT] + +theorem setNextRawEq (s : S) (r v : U64) : + obj.Reply.set_next_raw st r s v = ok (L.wr s (rNextA r) v) := by + unfold obj.Reply.set_next_raw + simp [lift, bind_ok, L.write_eq, rNextA, layout.reply.NEXT] + +theorem setNextZeroEq (s : S) (r : U64) : + obj.Reply.set_next st r s 0#u64 false = ok (L.wr s (rNextA r) 0#u64) := by + exact eq_of_spec_fd (WP.spec_mono (obj.Reply.set_next.val0 s r 0#u64) (fun _ h => h.trans (by rfl))) + +theorem setPrevZeroEq (s : S) (r : U64) : + obj.Reply.set_prev st r s 0#u64 = ok (L.wr s (rPrevA r) 0#u64) := by + exact eq_of_spec_fd (WP.spec_mono (obj.Reply.set_prev.val s r 0#u64) (fun _ h => h.trans (by rfl))) + +/-- The final unlink, including the state write, leaves the IPC stack words. -/ +theorem unlinkReplyRelFrameA {s : S} {σ : State Unit} {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) {t r : U64} (ht : t ∈ Z.T) (hr : r ∈ R) + (hi : ReplyIsolatedA (L.mem s) Z.C stack r) + {x : Tcb} {xr : Reply} (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hkr : σ.kheap (specPtr r) = some (.reply xr)) (hback : xr.replyTcb = some (specPtr t)) + (hblock : x.tcbState = .blockedOnReply (specPtr r) ∨ + ∃ ep d, x.tcbState = .blockedOnReceive ep (some (specPtr r)) d) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) : + ipc.reply_unlink st s r t ⦃ s' => ∃ σ', + replyUnlinkTcb (Z := Unit) (specPtr t) (specPtr r) σ = ({((), σ')}, False) ∧ + SchedSRA st Z m κ s' σ' ∧ EpStateRel Z.T E R Bf (L.mem s') σ' ∧ + ReplyStackRelA (L.mem s') R Z.C σ' (scNameA m) stack ∧ ReplyIsolatedA (L.mem s') Z.C stack r ∧ + (∀ u ∈ Z.T, L.mem s' (core.num.U64.wrapping_add u 3664#u64) = + L.mem s (core.num.U64.wrapping_add u 3664#u64)) ⦄ := by + have hrt : specPtr r ≠ specPtr t := by intro e; rw [e, hk] at hkr; cases hkr + have hta := PE.tcb t ht + have hra := (a_rtcb PE hr).1 + set σ1 := updK σ (specPtr r) (.reply { xr with replyTcb := none }) with e1 + set σ2 := updK σ1 (specPtr t) (.tcb { x with tcbState := .inactive }) with e2 + have hkt1 : σ1.kheap (specPtr t) = some (.tcb x) := by + rw [e1, updK_kheap, Function.update_of_ne hrt.symm, hk] + have hkt2 : σ2.kheap (specPtr t) = some (.tcb { x with tcbState := .inactive }) := by + rw [e2, updK_kheap, Function.update_self] + have hcase : runnable x.tcbState = false ∧ x.tcbState ≠ .idleThreadState := by + rcases hblock with h | ⟨ep, d, h⟩ <;> rw [h] <;> exact ⟨rfl, by intro e; cases e⟩ + unfold ipc.reply_unlink + refine WP.spec_bind (obj.Reply.set_tcb.spec s r 0#u64) ?_ + rintro s1 rfl + have he1 : EpStateRel Z.T E R Bf (L.mem (L.wr s (rTcbA r) 0#u64)) σ1 := + HE.upd_reply PE hr _ ((agree_wr (L := L) (s := s) (v := 0#u64) hra).mono fun b hb => by rw [rTcbA_val]; exact hb.2) + (by rw [rd_wr_eq hra, optRef_zero]) + have hw1 : WritesIn st s (L.wr s (rTcbA r) 0#u64) (EWrite Z.T E R Bf) := + (writesIn_wr hra).mono fun b hb => by subst b; exact Or.inr (Or.inl ⟨r, hr, rTcbA_val r⟩) + unfold sched.set_thread_state + refine WP.spec_bind (obj.Tcb.set_state.spec _ t sched.TS_INACTIVE) ?_ + rintro s2 rfl + obtain ⟨he2, hw2, hs2⟩ := write_state_rel PE _ σ1 he1 ht hkt1 + (notq_updR hq r { xr with replyTcb := none }) .inactive (by decide) sched.TS_INACTIVE + (by rw [ts_inactive]; rfl) + have hOnly : OnlyT σ σ2 (specPtr t) := + (OnlyT.refl σ _).updO _ (fun _ h => by cases h) + |>.updP _ + have hNB := NoBlockE.only hk hkt2 hOnly (Or.inr (Or.inr ⟨rfl, hcase⟩)) + have H2 := H.stable (WritesIn.ew hw1 hw2) (fun _ hb => endpWriteOffRead H.toSchedSRN P hb) + (endpSchedStep ((EpStep.reply hkr none).trans hs2)) hNB + refine WP.spec_mono (schedule_tcb_refinesA H2 t (popStateWords he2)) ?_ + rintro s3 ⟨hwa, H3⟩ + have ep3 : EpStateRel Z.T E R Bf (L.mem s3) (stsAct σ2 (specPtr t)) := + he2.frame PE (fun b hb hf => hwa.mem_eq hb + (fun eq => endpFootOffWrite H2.toSchedSRN P hf (eq.symm ▸ swAction H2))) (KView.stsAct' _ _) + have hlinks : ∀ q ∈ R, L.mem s3 (rNextA q) = L.mem s (rNextA q) ∧ L.mem s3 (rPrevA q) = L.mem s (rPrevA q) := by + intro q hq + obtain ⟨ha, hv⟩ := replyNextPlaceA (Z := Z) PE hq + obtain ⟨hb, hw⟩ := replyPrevPlaceA (Z := Z) PE hq + have keep (k : U64) (hal : Aligned k) (hlo : q.val < k.val) (hhi : k.val < q.val + 32) : + L.mem s3 k = L.mem s k := + (hwa.mem_eq hal (fun eq => (replyWordOffSchedA H2.toSchedSRN P hq (by omega) hhi).2 + (eq ▸ swAction H2))).trans ((WritesIn.ew hw1 hw2).mem_eq hal + (fun h => replyLinkOffEp H.toSchedSRN P PE hq hlo hhi (EWrite.foot h))) + exact ⟨keep _ ha (by omega) (by omega), keep _ hb (by omega) (by omega)⟩ + refine ⟨stsAct σ2 (specPtr t), unlinkTcbSpecA hk hkr hback hblock, H3, ep3, HS.frame hlinks ?_ ?_, + ⟨hi.1, (hlinks r hr).1.trans hi.2.1, (hlinks r hr).2.trans hi.2.2⟩, ?_⟩ + · intro c hc y n hy + have h1 : scNameA m c ≠ specPtr t := by intro e; rw [e, hk] at hy; cases hy + have h2 : scNameA m c ≠ specPtr r := by intro e; rw [e, hkr] at hy; cases hy + exact ⟨y, n, by rw [stsAct_kheap, e2, updK_kheap, Function.update_of_ne h1, + e1, updK_kheap, Function.update_of_ne h2]; exact hy, rfl⟩ + · intro q hq y hy + obtain ⟨z, hz, _⟩ := HE.reply q hq + have h1 : specPtr q ≠ specPtr t := by intro e; rw [e, hk] at hz; cases hz + rw [stsAct_kheap, e2, updK_kheap, Function.update_of_ne h1, e1, updK_kheap] at hy + by_cases e : q = r + · subst q + rw [Function.update_self] at hy + cases hy + exact ⟨xr, hkr, rfl⟩ + · rw [Function.update_of_ne (fun ep => e (specPtr_inj ep))] at hy + exact ⟨y, hy, rfl⟩ + + · intro u hu + have aligned : Aligned (core.num.U64.wrapping_add u 3664#u64) := + field_aligned (K := 3664#u64) (PE.tcb u hu) (by decide) (by decide) + have off : ¬ EWrite Z.T E R Bf (core.num.U64.wrapping_add u 3664#u64) := + not_ew_offA PE hu (K := 3664#u64) (by decide) (by change ¬ TWOffN 3664; unfold TWOffN; omega) (by decide) (by decide) + exact (hwa.mem_eq aligned (fun e => vcpuWordOffMcsA H2.toSchedSRN hu (e ▸ swAction H2))).trans + ((WritesIn.ew hw1 hw2).mem_eq aligned off) + +/-- Unlink a detached reply and retain the endpoint, scheduler, and stack relations. -/ +theorem unlinkReplyRelA {s : S} {σ : State Unit} {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) {t r : U64} (ht : t ∈ Z.T) (hr : r ∈ R) + (hi : ReplyIsolatedA (L.mem s) Z.C stack r) + {x : Tcb} {xr : Reply} (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hkr : σ.kheap (specPtr r) = some (.reply xr)) (hback : xr.replyTcb = some (specPtr t)) + (hblock : x.tcbState = .blockedOnReply (specPtr r) ∨ + ∃ ep d, x.tcbState = .blockedOnReceive ep (some (specPtr r)) d) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) : + ipc.reply_unlink st s r t ⦃ s' => ∃ σ', + replyUnlinkTcb (Z := Unit) (specPtr t) (specPtr r) σ = ({((), σ')}, False) ∧ + SchedSRA st Z m κ s' σ' ∧ EpStateRel Z.T E R Bf (L.mem s') σ' ∧ + ReplyStackRelA (L.mem s') R Z.C σ' (scNameA m) stack ∧ ReplyIsolatedA (L.mem s') Z.C stack r ⦄ := + WP.spec_mono (unlinkReplyRelFrameA H P PE HE HS ht hr hi hk hkr hback hblock hq) + fun _ ⟨σ', h, H', HE', HS', hi', _⟩ => ⟨σ', h, H', HE', HS', hi'⟩ + +end Store +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/IpcScPopDonateA.lean b/proofs/Fl4ProofsA64/P3/IpcScPopDonateA.lean new file mode 100644 index 000000000..9ba6e7cb2 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/IpcScPopDonateA.lean @@ -0,0 +1,171 @@ +/- +P3 (AArch64): the optional SC donation at the tail of the reply pop. + +[S] For an SC returned to the caller, migration is a no-op. A bound +donor is local and has no VCPU to stall. +-/ +import Fl4ProofsA64.P3.IpcScPopPrepA + +open Aeneas Aeneas.Std Result +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4SpecA64 Sched +noncomputable section + +private theorem unboundDonationListsA {σ σ' : State Unit} {p t q : ObjRef} {x y : SchedContext} {n k : Nat} + {xt : Tcb} (hp : σ.kheap p = some (.schedContext x n)) (hx : x.scTcb = none) + (ht : σ.kheap t = some (.tcb xt)) + (h : schedContextDonate (Z := Unit) p t σ = ({((), σ')}, False)) + (hq : σ.kheap q = some (.schedContext y k)) : + ∃ y', σ'.kheap q = some (.schedContext y' k) ∧ y'.scReplies = y.scReplies := by + have e : σ' = donStA σ p t x n xt := by + have hs : ((), σ') ∈ (schedContextDonate (Z := Unit) p t σ).1 := by + rw [h] + exact Set.mem_singleton _ + rw [donate_specA hp ht hx] at hs + exact congrArg Prod.snd (Set.mem_singleton_iff.mp hs) + rw [e, donStA, updKSc_kheap, Function.update_of_ne (by intro e; rw [e, ht] at hq; cases hq), updKSc_kheap] + by_cases he : q = p + · subst q + rw [hp] at hq + cases hq + rw [Function.update_self] + exact ⟨_, rfl, rfl⟩ + · rw [Function.update_of_ne he] + exact ⟨y, hq, rfl⟩ + +section Store +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + +/-- [S] Donation preconditions when the removed reply's caller lacks an SC. +The affinity condition makes the destination's migration a no-op. -/ +def ReplyPopLocalA (s : S) (σ : State Unit) (sc t : U64) : Prop := + NotIdleScA m (scNameA m sc) ∧ L.mem s (affA t) = L.mem s (scCoreA sc) ∧ + (L.mem s (scTcbA sc) = 0#u64 ∨ ∃ f ∈ Z.T, L.mem s (scA f) = sc ∧ + L.mem s (affA f) = L.cpu s ∧ + L.mem s (core.num.U64.wrapping_add f 3664#u64) = 0#u64 ∧ NotQueued σ (specPtr f)) + +private theorem popDonateFrameA {s : S} {σ : State Unit} {stack : U64 → List U64} + (H : SchedSRN st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + (HS : ReplyStackRelA (PopLinkMemA (L.mem s) r) R Z.C σ (scNameA m) stack) + (hr : r ∈ R) {s' : S} {σ' : State Unit} (F : MFrA (st := st) Z m s s' σ σ') + (hu : σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory) + (hsc : ∀ c ∈ Z.C, ∀ x n, σ.kheap (scNameA m c) = some (.schedContext x n) → + ∃ y k, σ'.kheap (scNameA m c) = some (.schedContext y k) ∧ y.scReplies = x.scReplies) : + EpStateRel Z.T E R Bf (L.mem s') σ' ∧ + ReplyStackRelA (PopLinkMemA (L.mem s') r) R Z.C σ' (scNameA m) stack ∧ + (∀ q ∈ R, (PopLinkMemA (L.mem s') r) (rNextA q) = (PopLinkMemA (L.mem s) r) (rNextA q) ∧ + (PopLinkMemA (L.mem s') r) (rPrevA q) = (PopLinkMemA (L.mem s) r) (rPrevA q)) := by + obtain ⟨FW, KW⟩ := epFrameOfMcsA H P F hu + have hm : ∀ q ∈ R, (PopLinkMemA (L.mem s') r) (rNextA q) = (PopLinkMemA (L.mem s) r) (rNextA q) ∧ + (PopLinkMemA (L.mem s') r) (rPrevA q) = (PopLinkMemA (L.mem s) r) (rPrevA q) := by + intro q hq + have hn := replyNextPlaceA (Z := Z) PE hq + have hp := replyPrevPlaceA (Z := Z) PE hq + have next := F.wr.mem_eq hn.1 (replyWordOffSchedA H P hq (by omega) (by omega)).2 + have prev := F.wr.mem_eq hp.1 (replyWordOffSchedA H P hq (by omega) (by omega)).2 + unfold PopLinkMemA + constructor + · by_cases e : rNextA q = rPrevA r + · simp [e] + · simp only [Function.update_of_ne e, next] + · by_cases e : rPrevA q = rPrevA r + · simp [e] + · simp only [Function.update_of_ne e, prev] + refine ⟨HE.frame PE FW KW, HS.frame hm hsc ?_, hm⟩ + intro q hq x hx + obtain ⟨y, hy, _⟩ := HE.reply q hq + have keep := F.keep.obj _ _ hy (fun _ _ eq => by cases eq) (fun _ eq => by cases eq) + exact ⟨y, hy, congrArg Reply.replySc (KernelObject.reply.inj (Option.some.inj (hx.symm.trans keep)))⟩ + +/-- The caller-SC test and optional donation, on the state after popping the SC's list. -/ +theorem replyPopDonateRefA {s : S} {σ : State Unit} (a : A) {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + (HS : ReplyStackRelA (PopLinkMemA (L.mem s) r) R Z.C σ (scNameA m) stack) + (hi : ReplyIsolatedA (PopLinkMemA (L.mem s) r) Z.C stack r) + {sc t : U64} (hc : sc ∈ Z.C) (hsc0 : sc ≠ 0#u64) (ht : t ∈ Z.T) (hr : r ∈ R) + {xt : Tcb} (hk : σ.kheap (specPtr t) = some (.tcb xt)) + {l : List ObjRef} (hh : l.head? = some (specPtr r)) + (hlocal : xt.tcbSchedContext = none → ReplyPopLocalA (st := st) (Z := Z) (m := m) s σ sc t) + (hpost : ∀ σ', schedContextDonate (Z := Unit) (scNameA m sc) (specPtr t) σ = ({((), σ')}, False) → + RelIfBoundSc σ' (specPtr t)) : + (if L.mem s (scA t) = 0#u64 then schedcontext.donate st ar s a sc t else ok (s, a)) ⦃ out => + out.2 = a ∧ ∃ σ' Q', + Fl4Spec.NonDet.when (the l.head? = specPtr r ∧ xt.tcbSchedContext = none) + (schedContextDonate (Z := Unit) (scNameA m sc) (specPtr t)) σ = ({((), σ')}, False) ∧ + SchedSRA st { Z with Q := Q' } m κ out.1 σ' ∧ + EpStateRel Z.T E R Bf (L.mem out.1) σ' ∧ + ReplyStackRelA (PopLinkMemA (L.mem out.1) r) R Z.C σ' (scNameA m) stack ∧ + ReplyIsolatedA (PopLinkMemA (L.mem out.1) r) Z.C stack r ∧ SKeepA σ σ' ⦄ := by + obtain ⟨y, hy, hsct⟩ := H.sch.tcb t ht + rw [hk] at hy + cases hy + by_cases h0 : L.mem s (scA t) = 0#u64 + · have hxt : xt.tcbSchedContext = none := by rw [hsct, h0]; simp [optScA] + obtain ⟨hni, hmig, hfrom⟩ := hlocal hxt + have hwhen : the l.head? = specPtr r ∧ xt.tcbSchedContext = none := + ⟨by rw [hh]; rfl, hxt⟩ + obtain ⟨x, n, hx, hxrel⟩ := H.scr.sc sc hc + have finish (out : S × A) (σ' : State Unit) (Q' : U64 → U64 → List U64) + (harch : out.2 = a) (HL : SchedSRB st { Z with Q := Q' } m κ out.1 σ' (specPtr t)) + (F : MFrA (st := st) Z m s out.1 σ σ') + (hD : schedContextDonate (Z := Unit) (scNameA m sc) (specPtr t) σ = ({((), σ')}, False)) + (scKeep : ∀ q y k, σ.kheap q = some (.schedContext y k) → + ∃ y', σ'.kheap q = some (.schedContext y' k) ∧ y'.scReplies = y.scReplies) : + out.2 = a ∧ ∃ σ' Q', + Fl4Spec.NonDet.when (the l.head? = specPtr r ∧ xt.tcbSchedContext = none) + (schedContextDonate (Z := Unit) (scNameA m sc) (specPtr t)) σ = ({((), σ')}, False) ∧ + SchedSRA st { Z with Q := Q' } m κ out.1 σ' ∧ EpStateRel Z.T E R Bf (L.mem out.1) σ' ∧ + ReplyStackRelA (PopLinkMemA (L.mem out.1) r) R Z.C σ' (scNameA m) stack ∧ + ReplyIsolatedA (PopLinkMemA (L.mem out.1) r) Z.C stack r ∧ SKeepA σ σ' := by + have hm : ((), σ') ∈ (schedContextDonate (Z := Unit) (scNameA m sc) (specPtr t) σ).1 := by + rw [hD] + exact Set.mem_singleton _ + have hu := schedContextDonateKeepsUserMemory _ _ _ _ hm + obtain ⟨HE', HS', frame⟩ := popDonateFrameA H.toSchedSRN P PE HE HS hr F hu (fun q _ y k hy => by + obtain ⟨y', hky', hL⟩ := scKeep (scNameA m q) y k hy + exact ⟨y', k, hky', hL⟩) + refine ⟨harch, σ', Q', ?_, HL.toA (hpost _ hD), HE', HS', ?_, F.keep⟩ + · unfold Fl4Spec.NonDet.when + rw [if_pos hwhen] + exact hD + · exact ⟨hi.1, (frame r hr).1.trans hi.2.1, (frame r hr).2.trans hi.2.2⟩ + rcases hfrom with hUnb | ⟨f, hf, hfs, hfon, hvc, hq⟩ + · refine WP.spec_mono (reply_pop_donUnbA H a hc ht hsc0 hUnb hni hmig hk hh) ?_ + rintro out ⟨harch, σ', hD, HB, F⟩ + unfold Fl4Spec.NonDet.when at hD + rw [if_pos hwhen] at hD + have hx0 : x.scTcb = none := by rw [hxrel.tcb, ← scTcbA_eqSc, hUnb, optRefS_zero] + exact finish out σ' Z.Q harch HB F hD (fun _ _ _ => unboundDonationListsA hx hx0 hk hD) + · have hstall := stall_keepA (ar := ar) a (PE.tcb f hf) hfon hvc (WritesIn.refl (st := st) s (fun _ => False)) + (fun h => h) (fun h => h) + refine WP.spec_mono (reply_pop_donFromA H (popStateWords HE) a hc hsc0 hf hfs ht hfon hq.1 hq.2 hstall + hmig hni hk hh) ?_ + rintro out ⟨harch, σ', Q', hD, HB, F⟩ + unfold Fl4Spec.NonDet.when at hD + rw [if_pos hwhen] at hD + obtain ⟨xf, hxf, esf⟩ := H.sch.tcb f hf + have eSc : xf.tcbSchedContext = some (scNameA m sc) := by rw [esf, hfs, optScA, if_neg hsc0]; rfl + obtain ⟨z, k, hz, zf⟩ := H.inv.back (specPtr f) xf _ hxf eSc + rw [hx] at hz + cases hz + obtain ⟨xf', hx', dom, _⟩ := H.rest.prio f hf + rw [hxf] at hx' + cases hx' + have hft : specPtr f ≠ specPtr t := fun e => hsc0 (by rw [← hfs, specPtr_inj e, h0]) + have hm : ((), σ') ∈ (schedContextDonate (Z := Unit) (scNameA m sc) (specPtr t) σ).1 := by + rw [hD] + exact Set.mem_singleton _ + exact finish out σ' Q' harch HB F hD (fun _ _ _ => donationKeepsReplyLists hx zf hxf hk hft dom hq hm) + · rw [if_neg h0, WP.spec_ok] + have hn : xt.tcbSchedContext ≠ none := by rw [hsct]; simp [optScA, h0] + refine ⟨rfl, σ, Z.Q, ?_, H, HE, HS, hi, SKeepA.refl _⟩ + unfold Fl4Spec.NonDet.when + rw [if_neg (fun e => hn e.2)] + rfl + +end Store +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/IpcScPopHeapA.lean b/proofs/Fl4ProofsA64/P3/IpcScPopHeapA.lean new file mode 100644 index 000000000..49c52e3e7 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/IpcScPopHeapA.lean @@ -0,0 +1,148 @@ +/- P3 (AArch64): the four heap projections used to rebuild the reply stack after a pop. -/ +import Fl4ProofsA64.P3.IpcScPopReadA + +open Aeneas Aeneas.Std +namespace Fl4ProofsA64.P3.Endp +open Fl4SpecA64 Sched + +set_option maxHeartbeats 300000 in +theorem popHeapScA {σ : State Unit} {p r : ObjRef} {x : SchedContext} {n : Nat} : + (popStA σ p x n r).kheap p = some (.schedContext { x with scReplies := x.scReplies.tail } n) := by + let l := x.scReplies.tail + let rep := repStA σ p x n l + let fixed := fixHdA rep p l + let result := rscA fixed r none + have finish : popStA σ p x n r = result := rfl + have repHeap : rep.kheap p = some (.schedContext { x with scReplies := x.scReplies.tail } n) := by + change Function.update σ.kheap p (some (.schedContext { x with scReplies := l } n)) p = _ + exact Function.update_self _ _ _ + have fixedHeap : fixed.kheap p = some (.schedContext { x with scReplies := x.scReplies.tail } n) := + (fixHdA_sc (σ := rep) (N := p) (l := l) (q := p)).2 repHeap + have resultHeap : result.kheap p = some (.schedContext { x with scReplies := x.scReplies.tail } n) := + (rscA_sc (σ := fixed) (p := r) (v := none) (q := p)).2 fixedHeap + exact (congrArg (fun st : State Unit => st.kheap p) finish).trans resultHeap + +set_option maxHeartbeats 300000 in +theorem popHeapOtherA {σ : State Unit} {p r q : ObjRef} {x : SchedContext} {n : Nat} + (hqp : q ≠ p) (hqr : q ≠ r) (hqHead : x.scReplies.tail.head? ≠ some q) : + (popStA σ p x n r).kheap q = σ.kheap q := by + let l := x.scReplies.tail + let rep := repStA σ p x n l + let fixed := fixHdA rep p l + let result := rscA fixed r none + have finish : popStA σ p x n r = result := rfl + have repKeep : rep.kheap q = σ.kheap q := repStA_ne hqp + have fixedKeep : fixed.kheap q = rep.kheap q := by + cases hl : l with + | nil => + have e : fixed = rep := (congrArg (fixHdA rep p) hl).trans (fixHdA_nil rep p) + exact congrArg (fun s : State Unit => s.kheap q) e + | cons h tl => + have ne : q ≠ h := by + intro e + apply hqHead + exact (congrArg List.head? hl).trans (congrArg some e.symm) + have e : fixed = rscA rep h (some p) := (congrArg (fixHdA rep p) hl).trans (fixHdA_cons rep p h tl) + exact (congrArg (fun s : State Unit => s.kheap q) e).trans (rscA_ne (some p) ne) + have resultKeep : result.kheap q = fixed.kheap q := rscA_ne none hqr + exact (congrArg (fun s : State Unit => s.kheap q) finish).trans (resultKeep.trans (fixedKeep.trans repKeep)) + +set_option maxHeartbeats 300000 in +theorem popHeapRemovedA {σ : State Unit} {p r : ObjRef} {x : SchedContext} {n : Nat} + {xr : Reply} (hk : σ.kheap p = some (.schedContext x n)) + (hr : σ.kheap r = some (.reply xr)) (hqHead : x.scReplies.tail.head? ≠ some r) : + (popStA σ p x n r).kheap r = some (.reply { xr with replySc := none }) := by + let l := x.scReplies.tail + let rep := repStA σ p x n l + let fixed := fixHdA rep p l + let result := rscA fixed r none + have finish : popStA σ p x n r = result := rfl + have hrp : r ≠ p := by + intro e + rw [e, hk] at hr + cases hr + have repKeep : rep.kheap r = σ.kheap r := repStA_ne hrp + have fixedKeep : fixed.kheap r = rep.kheap r := by + cases hl : l with + | nil => + have e : fixed = rep := (congrArg (fixHdA rep p) hl).trans (fixHdA_nil rep p) + exact congrArg (fun s : State Unit => s.kheap r) e + | cons h tl => + have ne : r ≠ h := by + intro e + apply hqHead + exact (congrArg List.head? hl).trans (congrArg some e.symm) + have e : fixed = rscA rep h (some p) := (congrArg (fixHdA rep p) hl).trans (fixHdA_cons rep p h tl) + exact (congrArg (fun s : State Unit => s.kheap r) e).trans (rscA_ne (some p) ne) + have fixedHeap : fixed.kheap r = some (.reply xr) := fixedKeep.trans (repKeep.trans hr) + have resultState : result = updKSc fixed r (.reply { xr with replySc := none }) := rscA_of fixedHeap none + have resultHeap : result.kheap r = some (.reply { xr with replySc := none }) := + (congrArg (fun s : State Unit => s.kheap r) resultState).trans (Function.update_self _ _ _) + exact (congrArg (fun s : State Unit => s.kheap r) finish).trans resultHeap + +set_option maxHeartbeats 300000 in +theorem popHeapSuccessorA {σ : State Unit} {p r d : ObjRef} {x : SchedContext} {n : Nat} + {xd : Reply} (hk : σ.kheap p = some (.schedContext x n)) + (hd : σ.kheap d = some (.reply xd)) (hh : x.scReplies.tail.head? = some d) (hne : d ≠ r) : + (popStA σ p x n r).kheap d = some (.reply { xd with replySc := some p }) := by + let l := x.scReplies.tail + let rep := repStA σ p x n l + let fixed := fixHdA rep p l + let result := rscA fixed r none + have finish : popStA σ p x n r = result := rfl + have hdp : d ≠ p := by + intro e + rw [e, hk] at hd + cases hd + have repHeap : rep.kheap d = some (.reply xd) := (repStA_ne hdp).trans hd + have fixedState : fixed = rscA rep d (some p) := by + cases hl : l with + | nil => + have e : (none : Option ObjRef) = some d := (congrArg List.head? hl).symm.trans hh + cases e + | cons h tl => + have e : h = d := Option.some.inj ((congrArg List.head? hl).symm.trans hh) + exact ((congrArg (fixHdA rep p) hl).trans (fixHdA_cons rep p h tl)).trans + (congrArg (fun o : ObjRef => rscA rep o (some p)) e) + have fixedWrite : rscA rep d (some p) = updKSc rep d (.reply { xd with replySc := some p }) := + rscA_of repHeap (some p) + have fixedHeap : fixed.kheap d = some (.reply { xd with replySc := some p }) := + (congrArg (fun st : State Unit => st.kheap d) (fixedState.trans fixedWrite)).trans (Function.update_self _ _ _) + have resultKeep : result.kheap d = fixed.kheap d := rscA_ne none hne + exact (congrArg (fun s : State Unit => s.kheap d) finish).trans (resultKeep.trans fixedHeap) + +set_option maxHeartbeats 300000 in +theorem popHeapWords {σ : State Unit} {p : ObjRef} {x : SchedContext} {n : Nat} + {r : U64} {rest : List U64} + (hk : σ.kheap p = some (.schedContext x n)) + (hL : x.scReplies = specPtr r :: rest.map specPtr) (hnd : (r :: rest).Nodup) + {xr : Reply} (hkr : σ.kheap (specPtr r) = some (.reply xr)) : + let σ' := popStA σ p x n (specPtr r) + σ'.kheap p = some (.schedContext { x with scReplies := x.scReplies.tail } n) ∧ + (∀ q, q ≠ p → q ≠ specPtr r → (∀ d, rest.head? = some d → q ≠ specPtr d) → + σ'.kheap q = σ.kheap q) ∧ + σ'.kheap (specPtr r) = some (.reply { xr with replySc := none }) ∧ + (∀ d y, rest.head? = some d → σ.kheap (specPtr d) = some (.reply y) → + σ'.kheap (specPtr d) = some (.reply { y with replySc := some p })) := by + have heads : x.scReplies.tail.head? = rest.head?.map specPtr := by + simp only [hL, List.tail_cons, List.head?_map] + have offHead (q : ObjRef) (h : ∀ d, rest.head? = some d → q ≠ specPtr d) : + x.scReplies.tail.head? ≠ some q := by + intro e + rw [heads] at e + cases hl : rest.head? with + | none => rw [hl] at e; cases e + | some d => + rw [hl] at e + exact h d hl (Option.some.inj e).symm + have absent : r ∉ rest := (List.nodup_cons.1 hnd).1 + have awayR : x.scReplies.tail.head? ≠ some (specPtr r) := + offHead _ fun d hd e => absent (specPtr_inj e ▸ List.mem_of_mem_head? hd) + refine ⟨popHeapScA, fun q hp hr hd => popHeapOtherA hp hr (offHead q hd), + popHeapRemovedA hk hkr awayR, ?_⟩ + intro d y hd ky + have oldHead : x.scReplies.tail.head? = some (specPtr d) := by rw [heads, hd]; rfl + have ne : specPtr d ≠ specPtr r := fun e => absent (specPtr_inj e ▸ List.mem_of_mem_head? hd) + exact popHeapSuccessorA hk ky oldHead ne + +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/IpcScPopLinksA.lean b/proofs/Fl4ProofsA64/P3/IpcScPopLinksA.lean new file mode 100644 index 000000000..acce71ea8 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/IpcScPopLinksA.lean @@ -0,0 +1,392 @@ +/- +P3 (AArch64): reply-stack relation after popping, cutting or detaching a reply. +The list transformations follow x86 `ScCallPop` and `ScCallCut`. +-/ +import Fl4ProofsA64.P3.IpcScPushA + +open Aeneas Aeneas.Std +namespace Fl4ProofsA64.P3.Endp +open Fl4ProofsA64.P1 Fl4SpecA64 +noncomputable section + +private def Detached (C : Set U64) (Lst : U64 → List U64) (r : U64) : Prop := + ∀ sc ∈ C, r ∉ Lst sc + +theorem headBitEvenA {r : U64} (h : r.val % 2 = 0) : headBit r = false := by + have eq : r &&& 1#u64 = 0#u64 := by + apply UScalar.eq_of_val_eq + rw [UScalar.val_and, show (1#u64 : U64).val = 2 ^ 1 - 1 from rfl, Nat.and_two_pow_sub_one_eq_mod] + exact h + simp [headBit, eq] + +private theorem takeHeadA (l : List U64) {k : Nat} (hk : 1 ≤ k) : (l.take k).head? = l.head? := by + cases l with + | nil => simp + | cons a l => obtain ⟨k', rfl⟩ : ∃ k', k = k' + 1 := ⟨k - 1, by omega⟩; rfl + +private theorem nodupIdxA {l : List U64} (h : l.Nodup) {i j : Nat} (hi : i < l.length) (hj : j < l.length) + (e : l[i] = l[j]) : i = j := by + have := (List.nodup_iff_injective_get.1 h) (a₁ := ⟨i, hi⟩) (a₂ := ⟨j, hj⟩) (by simpa using e) + simpa using this + +/-- **The call-stack relation after a pop** of the head `r` of `sc`'s stack. -/ +theorem ReplyStackRelA.pop {R C : Set U64} {m m' : U64 → U64} {σ σ' : State Unit} {scRef : U64 → ObjRef} {Lst : U64 → List U64} + (h : ReplyStackRelA m R C σ scRef Lst) {sc r : U64} (hsc : sc ∈ C) + {rest : List U64} (hL : Lst sc = r :: rest) + (hmP : ∀ d ∈ R, d ≠ r → m' (rPrevA d) = m (rPrevA d)) + (hmN : ∀ d ∈ R, d ≠ r → rest.head? ≠ some d → m' (rNextA d) = m (rNextA d)) + (hmH : ∀ d, rest.head? = some d → m' (rNextA d) = sc ||| 1#u64) + (hmPr : m' (rPrevA r) = 0#u64) (hmNr : m' (rNextA r) = 0#u64) + {y : SchedContext} {n : Nat} (hk : σ.kheap (scRef sc) = some (.schedContext y n)) + (hk' : σ'.kheap (scRef sc) = some (.schedContext { y with scReplies := y.scReplies.tail } n)) + (hkq : ∀ q ∈ C, q ≠ sc → σ'.kheap (scRef q) = σ.kheap (scRef q)) + {xr : Reply} (hkr : σ.kheap (specPtr r) = some (.reply xr)) + (hkr' : σ'.kheap (specPtr r) = some (.reply { xr with replySc := none })) + (hkh : ∀ d x, rest.head? = some d → σ.kheap (specPtr d) = some (.reply x) → + σ'.kheap (specPtr d) = some (.reply { x with replySc := some (scRef sc) })) + (hRep : ∀ d ∈ R, ∃ xd, σ.kheap (specPtr d) = some (.reply xd)) + (hkd : ∀ d ∈ R, d ≠ r → rest.head? ≠ some d → σ'.kheap (specPtr d) = σ.kheap (specPtr d)) : + ReplyStackRelA m' R C σ' scRef (Function.update Lst sc rest) ∧ + ReplyIsolatedA m' C (Function.update Lst sc rest) r := by + set Lst' := Function.update Lst sc rest + have hL'sc : Lst' sc = rest := by simp [Lst'] + have hL'q : ∀ q, q ≠ sc → Lst' q = Lst q := fun q hq => by simp [Lst', hq] + obtain ⟨⟨y0, n0, hk0, hy0⟩, hLR, hLnd, hLw⟩ := h.stackRel sc hsc + rw [hk] at hk0; cases hk0 + rw [hL] at hLR hLnd hLw + have hrrest : r ∉ rest := (List.nodup_cons.1 hLnd).1 + have hrestnd : rest.Nodup := (List.nodup_cons.1 hLnd).2 + have hmemsc : ∀ d q, q ∈ C → d ∈ Lst q → d ∈ r :: rest → q = sc := fun d q hq h1 h2 => + h.unique q hq sc hsc d h1 (by rw [hL]; exact h2) + have hrq : ∀ q ∈ C, q ≠ sc → r ∉ Lst q := fun q hq hne h' => hne (hmemsc r q hq h' (by simp)) + have hdet_r : Detached C Lst' r := by + intro q hq h' + by_cases e : q = sc + · subst e; rw [hL'sc] at h'; exact hrrest h' + · rw [hL'q q e] at h'; exact hrq q hq e h' + -- the old head is the only reply pointing to `sc`; old detached replies stay detached + have hdetold : ∀ d, Detached C Lst d → Detached C Lst' d := by + intro d hd q hq h' + by_cases e : q = sc + · subst e; rw [hL'sc] at h'; exact hd q hq (by rw [hL]; exact List.mem_cons_of_mem _ h') + · rw [hL'q q e] at h'; exact hd q hq h' + have hnh_det : ∀ d, Detached C Lst d → rest.head? ≠ some d := fun d hd h' => + hd sc hsc (by rw [hL]; exact List.mem_cons_of_mem _ (List.mem_of_mem_head? h')) + have hne_r_det : ∀ d, Detached C Lst d → d ≠ r := fun d hd e => hd sc hsc (by rw [hL, e]; simp) + refine ⟨⟨fun q hq => ?_, fun q hq q' hq' d h1 h2 => ?_, fun d hd => ?_, fun d hd hdd => ?_⟩, ?_⟩ + · by_cases e : q = sc + · subst e + refine ⟨⟨_, n, hk', by rw [hL'sc, hy0, hL]; rfl⟩, ?_, ?_, ?_⟩ + · rw [hL'sc]; exact fun d hd => hLR d (List.mem_cons_of_mem _ hd) + · rw [hL'sc]; exact hrestnd + · rw [hL'sc]; intro i hi + have hm := List.getElem_mem hi + have hdR := hLR _ (List.mem_cons_of_mem _ hm) + have hne : rest[i] ≠ r := fun e' => hrrest (e' ▸ hm) + obtain ⟨g1, g2⟩ := hLw (i + 1) (by simp; omega) + refine ⟨?_, ?_⟩ + · rw [hmP _ hdR hne]; simpa using g1 + · cases i with + | zero => + rw [hmH _ (by rw [List.head?_eq_getElem?]; exact List.getElem?_eq_getElem hi)] + | succ j => + have hnh : rest.head? ≠ some rest[j + 1] := by + intro e' + rw [List.head?_eq_getElem?, List.getElem?_eq_getElem (by omega)] at e' + have := (List.nodup_iff_injective_get.1 hrestnd) (a₁ := ⟨j + 1, hi⟩) (a₂ := ⟨0, by omega⟩) + (by simpa using (Option.some.inj e').symm) + simp at this + rw [hmN _ hdR hne hnh]; simpa using g2 + · rw [hL'q q e] + obtain ⟨⟨yq, nq, hkq0, hyq⟩, hqR, hqnd, hqw⟩ := h.stackRel q hq + refine ⟨⟨yq, nq, by rw [hkq q hq e]; exact hkq0, hyq⟩, hqR, hqnd, + fun i hi => ?_⟩ + have hm := List.getElem_mem hi + have hne : (Lst q)[i] ≠ r := fun e' => hrq q hq e (e' ▸ hm) + have hnh : rest.head? ≠ some (Lst q)[i] := fun e' => + e (hmemsc _ q hq hm (List.mem_cons_of_mem _ (List.mem_of_mem_head? e'))) + rw [hmP _ (hqR _ hm) hne, hmN _ (hqR _ hm) hne hnh]; exact hqw i hi + · have g : ∀ q, q ∈ C → d ∈ Lst' q → d ∈ Lst q := fun q hq hdq => by + by_cases e : q = sc + · subst e; rw [hL'sc] at hdq; rw [hL]; exact List.mem_cons_of_mem _ hdq + · rw [hL'q q e] at hdq; exact hdq + exact h.unique q hq q' hq' d (g q hq h1) (g q' hq' h2) + · intro x' hx' p + obtain ⟨x, hkx⟩ := hRep d hd + have hxs := h.owner d hd x hkx + by_cases e : d = r + · subst e + rw [hkr] at hkx; cases hkx + rw [hkr'] at hx' + cases hx' + refine ⟨fun hp => (by cases hp), fun ⟨q, hq, _, hhd⟩ => ?_⟩ + exact absurd (List.mem_of_mem_head? hhd) (hdet_r q hq) + · by_cases eh : rest.head? = some d + · rw [hkh d x eh hkx] at hx' + cases hx' + refine ⟨fun hp => ?_, fun ⟨q, hq, hqp, hhd⟩ => ?_⟩ + · cases hp; exact ⟨sc, hsc, rfl, by rw [hL'sc]; exact eh⟩ + · by_cases e' : q = sc + · subst e'; rw [hqp] + · rw [hL'q q e'] at hhd + exact absurd (hmemsc d q hq (List.mem_of_mem_head? hhd) + (List.mem_cons_of_mem _ (List.mem_of_mem_head? eh))) e' + · rw [hkd d hd e eh, hkx] at hx' + cases hx' + rw [hxs p] + constructor + · rintro ⟨q, hq, hqp, hhd⟩ + refine ⟨q, hq, hqp, ?_⟩ + by_cases e' : q = sc + · subst e'; rw [hL] at hhd; simp at hhd; exact absurd hhd.symm e + · rw [hL'q q e']; exact hhd + · rintro ⟨q, hq, hqp, hhd⟩ + refine ⟨q, hq, hqp, ?_⟩ + by_cases e' : q = sc + · subst e'; rw [hL'sc] at hhd; exact absurd hhd eh + · rw [hL'q q e'] at hhd; exact hhd + · by_cases e : d = r + · subst e + rw [hmNr, hmPr] + exact ⟨rfl, fun h' => absurd rfl h', fun h' => absurd rfl h'⟩ + · -- an old detached reply + have hdo : Detached C Lst d := by + intro q hq hdq + by_cases e' : q = sc + · subst e' + rw [hL] at hdq + rcases List.mem_cons.1 hdq with rfl | hdq + · exact e rfl + · exact hdd q hq (by rw [hL'sc]; exact hdq) + · exact hdd q hq (by rw [hL'q q e']; exact hdq) + obtain ⟨t1, t2, t3⟩ := h.detached d hd hdo + rw [hmP d hd e, hmN d hd e (hnh_det d hdo)] + refine ⟨t1, fun hne => ?_, fun hne => ?_⟩ + · obtain ⟨g1, g2, g3⟩ := t2 hne + exact ⟨g1, hdetold _ g2, by rw [hmP _ g1 (hne_r_det _ g2)]; exact g3⟩ + · obtain ⟨g1, g2, g3⟩ := t3 hne + exact ⟨g1, hdetold _ g2, by rw [hmN _ g1 (hne_r_det _ g2) (hnh_det _ g2)]; exact g3⟩ + · exact ⟨hdet_r, hmNr, hmPr⟩ + +/-- **The call-stack relation after splicing out a detached reply** `r`. -/ +theorem ReplyStackRelA.detachedSplice {R C : Set U64} {m m' : U64 → U64} {σ : State Unit} {scRef : U64 → ObjRef} {Lst : U64 → List U64} + (h : ReplyStackRelA m R C σ scRef Lst) (hR0 : ∀ d ∈ R, d ≠ 0#u64) {r : U64} (hr : r ∈ R) + (hdet : Detached C Lst r) + (hmP : ∀ d ∈ R, d ≠ r → d ≠ m (rNextA r) → m' (rPrevA d) = m (rPrevA d)) + (hmN : ∀ d ∈ R, d ≠ r → d ≠ m (rPrevA r) → m' (rNextA d) = m (rNextA d)) + (hmPn : m (rNextA r) ≠ 0#u64 → m' (rPrevA (m (rNextA r))) = 0#u64) + (hmNp : m (rPrevA r) ≠ 0#u64 → m' (rNextA (m (rPrevA r))) = 0#u64) + (hmPr : m' (rPrevA r) = 0#u64) (hmNr : m' (rNextA r) = 0#u64) : + ReplyStackRelA m' R C σ scRef Lst ∧ ReplyIsolatedA m' C Lst r := by + obtain ⟨t1, t2, t3⟩ := h.detached r hr hdet + have hnLive : ∀ q ∈ C, ∀ d ∈ Lst q, d ≠ m (rNextA r) := by + intro q hq d hd e + have hR := (h.stackRel q hq).2.1 d hd + exact (t2 (fun h0 => hR0 d hR (e.trans h0))).2.1 q hq (e ▸ hd) + have hpLive : ∀ q ∈ C, ∀ d ∈ Lst q, d ≠ m (rPrevA r) := by + intro q hq d hd e + have hR := (h.stackRel q hq).2.1 d hd + exact (t3 (fun h0 => hR0 d hR (e.trans h0))).2.1 q hq (e ▸ hd) + -- `r`'s neighbours are detached, so not live + refine ⟨⟨fun q hq => ?_, h.unique, h.owner, fun d hd hdd => ?_⟩, hdet, hmNr, hmPr⟩ + · obtain ⟨hk, hqR, hqnd, hqw⟩ := h.stackRel q hq + refine ⟨hk, hqR, hqnd, fun i hi => ?_⟩ + have hm := List.getElem_mem hi + have hnr : (Lst q)[i] ≠ r := fun e => hdet q hq (e ▸ hm) + rw [hmP _ (hqR _ hm) hnr (hnLive q hq _ hm), hmN _ (hqR _ hm) hnr (hpLive q hq _ hm)]; exact hqw i hi + · by_cases e : d = r + · subst e; rw [hmNr, hmPr]; exact ⟨rfl, fun h' => absurd rfl h', fun h' => absurd rfl h'⟩ + obtain ⟨s1, s2, s3⟩ := h.detached d hd hdd + refine ⟨?_, fun hne => ?_, fun hne => ?_⟩ + · by_cases ep : d = m (rPrevA r) + · rw [ep, hmNp (fun h0 => hR0 d hd (ep.trans h0))]; rfl + · rw [hmN d hd e ep]; exact s1 + · by_cases ep : d = m (rPrevA r) + · rw [ep, hmNp (fun h0 => hR0 d hd (ep.trans h0))] at hne; exact absurd rfl hne + rw [hmN d hd e ep] at hne ⊢ + obtain ⟨g1, g2, g3⟩ := s2 hne + -- the next of `d` is not `r` (else `d` would be `r`'s previous) + have hnr : m (rNextA d) ≠ r := fun h' => ep (by rw [h'] at g3; exact g3.symm) + have hnn : m (rNextA d) ≠ m (rNextA r) := fun h' => by + have := g3; rw [h'] at this + have h2 := (t2 (fun h0 => hR0 _ g1 (h'.trans h0))).2.2 + exact e (this.symm.trans h2) + exact ⟨g1, g2, by rw [hmP _ g1 hnr hnn]; exact g3⟩ + · by_cases en : d = m (rNextA r) + · rw [en, hmPn (fun h0 => hR0 d hd (en.trans h0))] at hne; exact absurd rfl hne + rw [hmP d hd e en] at hne ⊢ + obtain ⟨g1, g2, g3⟩ := s3 hne + have hnr : m (rPrevA d) ≠ r := fun h' => en (by rw [h'] at g3; exact g3.symm) + have hnp : m (rPrevA d) ≠ m (rPrevA r) := fun h' => by + have := g3; rw [h'] at this + have h2 := (t3 (fun h0 => hR0 _ g1 (h'.trans h0))).2.2 + exact e (this.symm.trans h2) + exact ⟨g1, g2, by rw [hmN _ g1 hnr hnp]; exact g3⟩ + +/-- **The call-stack relation after cutting a stack** at its `k`-th reply `r` +(`k ≥ 1`): the newer replies stay, `r` and the older ones are detached. -/ +theorem ReplyStackRelA.cut {R C : Set U64} {m m' : U64 → U64} {σ σ' : State Unit} {scRef : U64 → ObjRef} {Lst : U64 → List U64} + (h : ReplyStackRelA m R C σ scRef Lst) (hEv : ∀ d ∈ R, d.val % 2 = 0) {sc : U64} (hsc : sc ∈ C) + {k : Nat} (hk1 : 1 ≤ k) (hkl : k < (Lst sc).length) + (hmP : ∀ d ∈ R, d ≠ (Lst sc)[k] → d ≠ (Lst sc)[k - 1] → m' (rPrevA d) = m (rPrevA d)) + (hmN : ∀ d ∈ R, d ≠ (Lst sc)[k] → (∀ h : k + 1 < (Lst sc).length, d ≠ (Lst sc)[k + 1]) → + m' (rNextA d) = m (rNextA d)) + (hmPn : m' (rPrevA (Lst sc)[k - 1]) = 0#u64) + (hmNo : ∀ h : k + 1 < (Lst sc).length, m' (rNextA (Lst sc)[k + 1]) = 0#u64) + (hmPr : m' (rPrevA (Lst sc)[k]) = 0#u64) (hmNr : m' (rNextA (Lst sc)[k]) = 0#u64) + {y : SchedContext} {n : Nat} (hky : σ.kheap (scRef sc) = some (.schedContext y n)) + (hk' : σ'.kheap (scRef sc) = some (.schedContext { y with scReplies := ((Lst sc).take k).map specPtr } n)) + (hName : ∀ q ∈ C, q ≠ sc → scRef q ≠ scRef sc) + (hRep : ∀ d ∈ R, ∃ xd, σ.kheap (specPtr d) = some (.reply xd)) + (hko : ∀ q, q ≠ (scRef sc) → σ'.kheap q = σ.kheap q) : + ReplyStackRelA m' R C σ' scRef (Function.update Lst sc ((Lst sc).take k)) ∧ + ReplyIsolatedA m' C (Function.update Lst sc ((Lst sc).take k)) (Lst sc)[k] := by + set Lst' := Function.update Lst sc ((Lst sc).take k) + have hL'sc : Lst' sc = (Lst sc).take k := by simp [Lst'] + have hL'q : ∀ q, q ≠ sc → Lst' q = Lst q := fun q hq => by simp [Lst', hq] + obtain ⟨⟨y0, n0, hk0, hy0⟩, hLR, hLnd, hLw⟩ := h.stackRel sc hsc + rw [hky] at hk0; cases hk0 + have hmemL : ∀ i (hi : i < (Lst sc).length), (Lst sc)[i] ∈ Lst sc := fun i hi => List.getElem_mem hi + have hRL : ∀ i (hi : i < (Lst sc).length), (Lst sc)[i] ∈ R := fun i hi => hLR _ (hmemL i hi) + have hinj : ∀ i j (hi : i < (Lst sc).length) (hj : j < (Lst sc).length), (Lst sc)[i] = (Lst sc)[j] → i = j := + fun i j hi hj e => nodupIdxA hLnd hi hj e + have hneI : ∀ i j (hi : i < (Lst sc).length) (hj : j < (Lst sc).length), i ≠ j → (Lst sc)[i] ≠ (Lst sc)[j] := + fun i j hi hj hne e => hne (hinj i j hi hj e) + -- other stacks hold none of `L` + have hother : ∀ q ∈ C, q ≠ sc → ∀ d ∈ Lst q, d ∉ Lst sc := fun q hq hne d hd hdL => + hne (h.unique q hq sc hsc d hd hdL) + -- the cut-off replies + have hdetL : ∀ j (hj : j < (Lst sc).length), k ≤ j → Detached C Lst' (Lst sc)[j] := by + intro j hj hkj q hq hdq + by_cases e : q = sc + · subst q; rw [hL'sc] at hdq + obtain ⟨i, hi, hie⟩ := List.getElem_of_mem hdq + rw [List.getElem_take] at hie + have := hinj i j (by simp at hi; omega) hj hie + simp at hi; omega + · rw [hL'q q e] at hdq; exact hother q hq e _ hdq (hmemL j hj) + have hdetold : ∀ d, Detached C Lst d → Detached C Lst' d := by + intro d hd q hq h' + by_cases e : q = sc + · subst q; rw [hL'sc] at h'; exact hd sc hq (List.mem_of_mem_take h') + · rw [hL'q q e] at h'; exact hd q hq h' + have hdet_ne : ∀ d, Detached C Lst d → ∀ j (hj : j < (Lst sc).length), d ≠ (Lst sc)[j] := fun d hd j hj e => + hd sc hsc (e ▸ hmemL j hj) + refine ⟨⟨fun q hq => ?_, fun q hq q' hq' d h1 h2 => ?_, fun d hd => ?_, fun d hd hdd => ?_⟩, ?_⟩ + · by_cases e : q = sc + · subst q + refine ⟨⟨_, n, hk', by rw [hL'sc]⟩, ?_, ?_, ?_⟩ + · rw [hL'sc]; exact fun d hd => hLR d (List.mem_of_mem_take hd) + · rw [hL'sc]; exact hLnd.sublist (List.take_sublist _ _) + · rw [hL'sc]; intro i hi + have hik : i < k := by simp at hi; omega + have hiL : i < (Lst sc).length := by omega + rw [List.getElem_take] + have hnr : (Lst sc)[i] ≠ (Lst sc)[k] := hneI i k hiL hkl (by omega) + obtain ⟨g1, g2⟩ := hLw i hiL + refine ⟨?_, ?_⟩ + · by_cases hi1 : i = k - 1 + · subst hi1; rw [hmPn] + rw [List.getD_eq_default _ _ (by simp; omega)] + · rw [hmP _ (hRL i hiL) hnr (hneI i (k - 1) hiL (by omega) hi1), g1, + List.getD_eq_getElem _ _ (by omega), List.getD_eq_getElem _ _ (by simp; omega), List.getElem_take] + · rw [hmN _ (hRL i hiL) hnr (fun h' => hneI i (k + 1) hiL h' (by omega)), g2] + cases i with + | zero => rfl + | succ j => + show (Lst sc).getD j 0#u64 = ((Lst sc).take k).getD j 0#u64 + rw [List.getD_eq_getElem _ _ (by omega), List.getD_eq_getElem _ _ (by simp; omega), List.getElem_take] + · rw [hL'q q e] + obtain ⟨⟨yq, nq, hkq0, hyq⟩, hqR, hqnd, hqw⟩ := h.stackRel q hq + refine ⟨⟨yq, nq, by rw [hko _ (hName q hq e)]; exact hkq0, hyq⟩, hqR, hqnd, + fun i hi => ?_⟩ + have hm := List.getElem_mem hi + have hnL : ∀ j (hj : j < (Lst sc).length), (Lst q)[i] ≠ (Lst sc)[j] := fun j hj e' => hother q hq e _ hm (e' ▸ hmemL j hj) + rw [hmP _ (hqR _ hm) (hnL k hkl) (hnL (k - 1) (by omega)), + hmN _ (hqR _ hm) (hnL k hkl) (fun h' => hnL (k + 1) h')] + exact hqw i hi + · have g : ∀ q, q ∈ C → d ∈ Lst' q → d ∈ Lst q := fun q hq hdq => by + by_cases e : q = sc + · subst q; rw [hL'sc] at hdq; exact List.mem_of_mem_take hdq + · rw [hL'q q e] at hdq; exact hdq + exact h.unique q hq q' hq' d (g q hq h1) (g q' hq' h2) + · intro x' hx' p + obtain ⟨x, hkx⟩ := hRep d hd + have hxs := h.owner d hd x hkx + have hdsc : (specPtr d) ≠ (scRef sc) := fun e => by rw [e, hky] at hkx; cases hkx + rw [hko _ hdsc, hkx] at hx' + cases hx' + rw [hxs p] + have hhd : (Lst' sc).head? = (Lst sc).head? := by + rw [hL'sc, takeHeadA _ hk1] + constructor + · rintro ⟨q, hq, hqp, hhq⟩ + refine ⟨q, hq, hqp, ?_⟩ + by_cases e : q = sc + · subst q; rw [hhd]; exact hhq + · rw [hL'q q e]; exact hhq + · rintro ⟨q, hq, hqp, hhq⟩ + refine ⟨q, hq, hqp, ?_⟩ + by_cases e : q = sc + · subst q; rw [hhd] at hhq; exact hhq + · rw [hL'q q e] at hhq; exact hhq + · by_cases hdL : d ∈ Lst sc + · obtain ⟨j, hj, rfl⟩ := List.getElem_of_mem hdL + have hkj : k ≤ j := by + by_contra hlt + exact hdd sc hsc (by rw [hL'sc]; exact List.mem_iff_getElem.2 ⟨j, by simp; omega, by rw [List.getElem_take]⟩) + by_cases hjk : j = k + · subst hjk + rw [hmNr, hmPr]; exact ⟨rfl, fun h' => absurd rfl h', fun h' => absurd rfl h'⟩ + have hnr : (Lst sc)[j] ≠ (Lst sc)[k] := hneI j k hj hkl hjk + have hnp : (Lst sc)[j] ≠ (Lst sc)[k - 1] := hneI j (k - 1) hj (by omega) (by omega) + obtain ⟨g1, g2⟩ := hLw j hj + rw [hmP _ (hRL j hj) hnr hnp, g1] + by_cases hj1 : j = k + 1 + · subst hj1 + rw [hmNo hj] + refine ⟨rfl, fun h' => absurd rfl h', fun hne => ?_⟩ + have hj2 : k + 2 < (Lst sc).length := by + by_contra h''; rw [List.getD_eq_default _ _ (by omega)] at hne; exact hne rfl + rw [List.getD_eq_getElem _ _ hj2] + refine ⟨hRL _ hj2, hdetL _ hj2 (by omega), ?_⟩ + have := (hLw (k + 2) hj2).2 + dsimp only at this + rw [hmN _ (hRL _ hj2) (hneI _ k hj2 hkl (by omega)) (fun h' => hneI _ (k + 1) hj2 h' (by omega)), this] + rw [List.getD_eq_getElem _ _ hj] + · obtain ⟨j', rfl⟩ : ∃ j', j = j' + 1 := ⟨j - 1, by omega⟩ + dsimp only at g2 + rw [hmN _ (hRL _ hj) hnr (fun h' => hneI _ (k + 1) hj h' hj1), g2] + show headBit ((Lst sc).getD j' 0#u64) = false ∧ _ ∧ _ + have hj' : j' < (Lst sc).length := by omega + rw [List.getD_eq_getElem _ _ hj'] + refine ⟨headBitEvenA (hEv _ (hRL j' hj')), fun _ => ⟨hRL j' hj', hdetL j' hj' (by omega), ?_⟩, fun hne => ?_⟩ + · rw [hmP _ (hRL j' hj') (hneI j' k hj' hkl (by omega)) (hneI j' (k - 1) hj' (by omega) (by omega)), + (hLw j' hj').1, List.getD_eq_getElem _ _ hj] + · have hj2 : j' + 2 < (Lst sc).length := by + by_contra h''; rw [List.getD_eq_default _ _ (by omega)] at hne; exact hne rfl + rw [List.getD_eq_getElem _ _ hj2] + refine ⟨hRL _ hj2, hdetL _ hj2 (by omega), ?_⟩ + have := (hLw (j' + 2) hj2).2 + dsimp only at this + rw [hmN _ (hRL _ hj2) (hneI _ k hj2 hkl (by omega)) (fun h' => hneI _ (k + 1) hj2 h' (by omega)), this] + rw [List.getD_eq_getElem _ _ hj] + · -- an old detached reply + have hdo : Detached C Lst d := by + intro q hq hdq + by_cases e : q = sc + · subst q; exact hdL hdq + · exact hdd q hq (by rw [hL'q q e]; exact hdq) + obtain ⟨t1, t2, t3⟩ := h.detached d hd hdo + rw [hmP d hd (hdet_ne d hdo k hkl) (hdet_ne d hdo (k - 1) (by omega)), + hmN d hd (hdet_ne d hdo k hkl) (fun h' => hdet_ne d hdo (k + 1) h')] + refine ⟨t1, fun hne => ?_, fun hne => ?_⟩ + · obtain ⟨g1, g2, g3⟩ := t2 hne + exact ⟨g1, hdetold _ g2, by rw [hmP _ g1 (hdet_ne _ g2 k hkl) (hdet_ne _ g2 (k - 1) (by omega))]; exact g3⟩ + · obtain ⟨g1, g2, g3⟩ := t3 hne + exact ⟨g1, hdetold _ g2, by + rw [hmN _ g1 (hdet_ne _ g2 k hkl) (fun h' => hdet_ne _ g2 (k + 1) h')]; exact g3⟩ + · exact ⟨hdetL k hkl (by omega), hmNr, hmPr⟩ + +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/IpcScPopPrepA.lean b/proofs/Fl4ProofsA64/P3/IpcScPopPrepA.lean new file mode 100644 index 000000000..885def8bb --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/IpcScPopPrepA.lean @@ -0,0 +1,186 @@ +/- +P3 (AArch64): the head-pop prefix changes the SC head and the two NEXT words. +The removed reply's PREV word is cleared after donation. +-/ +import Fl4ProofsA64.P3.IpcScPopHeapA + +open Aeneas Aeneas.Std Result +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4SpecA64 Sched +noncomputable section + +section Store +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + +/-- Link equality ignores the pending PREV clear of the popped reply. -/ +def PopLinkMemA (mem : U64 → U64) (r : U64) : U64 → U64 := + Function.update mem (rPrevA r) 0#u64 + +theorem popLinkMem_next (H : SchedSRN st Z m κ s σ) + (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + {r d : U64} (hr : r ∈ R) (hd : d ∈ R) : + PopLinkMemA (L.mem s) r (rNextA d) = L.mem s (rNextA d) := by + apply Function.update_of_ne + have hdv := (replyNextPlaceA (Z := Z) PE hd).2 + have hrv := (replyPrevPlaceA (Z := Z) PE hr).2 + intro e + have hv := congrArg UScalar.val e + by_cases eq : d = r + · subst d; omega + · have := replyWordsApartA H P hd hr eq + omega + +theorem popLinkMem_prev (H : SchedSRN st Z m κ s σ) + (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + {r d : U64} (hr : r ∈ R) (hd : d ∈ R) (hne : d ≠ r) : + PopLinkMemA (L.mem s) r (rPrevA d) = L.mem s (rPrevA d) := by + apply Function.update_of_ne + have hdv := (replyPrevPlaceA (Z := Z) PE hd).2 + have hrv := (replyPrevPlaceA (Z := Z) PE hr).2 + have := replyWordsApartA H P hd hr hne + intro e + have hv := congrArg UScalar.val e + omega + +private theorem promotePopHeadA {s : S} {σ : State Unit} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + (sc : U64) (rest : List U64) (hm : ∀ d ∈ rest, d ∈ R) : + (if rest.headD 0#u64 != 0#u64 then obj.Reply.set_next_raw st (rest.headD 0#u64) s (sc ||| 1#u64) else ok s) ⦃ s' => + SchedSRA st Z m κ s' σ ∧ EpStateRel Z.T E R Bf (L.mem s') σ ∧ + (∀ d ∈ R, L.mem s' (rPrevA d) = L.mem s (rPrevA d)) ∧ + (∀ d ∈ R, rest.head? ≠ some d → L.mem s' (rNextA d) = L.mem s (rNextA d)) ∧ + (∀ d, rest.head? = some d → L.mem s' (rNextA d) = sc ||| 1#u64) ∧ + (∀ t ∈ Z.T, L.mem s' (core.num.U64.wrapping_add t 3664#u64) = + L.mem s (core.num.U64.wrapping_add t 3664#u64)) ⦄ := by + cases rest with + | nil => + exact (WP.spec_ok _).2 ⟨H, HE, fun _ _ => rfl, fun _ _ _ => rfl, fun _ h => (by cases h), fun _ _ => rfl⟩ + | cons q rest => + have hq := hm q (by simp) + have hn : q ≠ 0#u64 := fun e => (PE.rep q hq).2.1 (by rw [e]; rfl) + simp only [List.headD_cons, show (q != 0#u64) = true from by simpa using hn, ↓reduceIte, + setNextRawEq, WP.spec_ok] + have na := replyNextPlaceA (Z := Z) PE hq + obtain ⟨H', FE⟩ := writeLinkFrames (v := sc ||| 1#u64) H P PE hq na.1 (by omega) (by omega) + refine ⟨H', HE.frame PE FE (KView.refl _), ?_, ?_, ?_, ?_⟩ + · intro d hd + have pa := replyPrevPlaceA (Z := Z) PE hd + apply L.mem_wr_ne na.1 pa.1 + by_cases eq : q = d + · subst d; omega + · have := replyWordsApartA H.toSchedSRN P hq hd eq + omega + · intro d hd hnd + have he : q ≠ d := fun eq => hnd (by rw [eq]; rfl) + have da := replyNextPlaceA (Z := Z) PE hd + have := replyWordsApartA H.toSchedSRN P hq hd he + exact L.mem_wr_ne na.1 da.1 (by omega) + · intro d h + have e : q = d := Option.some.inj h + subst d + exact L.mem_wr_same na.1 + · intro t ht + exact replyWriteKeepsVcpu PE hq (by omega) (by omega) na.1 ht + +/-- Proof of the prefix up to the caller-SC test. The stack uses a virtual zero +for the popped reply's PREV word until the extracted program clears it. -/ +theorem replyPopPrepareRefA {s : S} {σ : State Unit} {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {sc r : U64} (hc : sc ∈ Z.C) {rest : List U64} (hl : stack sc = r :: rest) : + replyPopPrepareA (st := st) s r ⦃ s' => ∃ x n, σ.kheap (scNameA m sc) = some (.schedContext x n) ∧ + let σ' := popStA σ (scNameA m sc) x n (specPtr r) + replyUnlinkSc (Z := Unit) (scNameA m sc) (specPtr r) σ = ({((), σ')}, False) ∧ + SchedSRA st Z m κ s' σ' ∧ EpStateRel Z.T E R Bf (L.mem s') σ' ∧ + ReplyStackRelA (PopLinkMemA (L.mem s') r) R Z.C σ' (scNameA m) (Function.update stack sc rest) ∧ + ReplyIsolatedA (PopLinkMemA (L.mem s') r) Z.C (Function.update stack sc rest) r ∧ + (∀ t ∈ Z.T, L.mem s' (core.num.U64.wrapping_add t 3664#u64) = + L.mem s (core.num.U64.wrapping_add t 3664#u64)) ⦄ := by + obtain ⟨ra, nx, pv⟩ := replyHeadWordsA H PE HS hc hl + obtain ⟨⟨x, n, hk, hL⟩, hm, hn, _⟩ := HS.stackRel sc hc + have hr : r ∈ R := hm r (by simp [hl]) + have hrRest : r ∉ rest := by rw [hl] at hn; exact (List.nodup_cons.1 hn).1 + obtain ⟨y, hky, hy, hh, hp, ht⟩ := HS.popFacts PE HE hc (by rw [hl]; rfl) + have hPopp : ∀ x n, σ.kheap (scNameA m sc) = some (.schedContext x n) → + x.scReplies.tail.head? = optRefS (rest.headD 0#u64) := by + rw [← pv] + exact hp + unfold replyPopPrepareA + refine WP.spec_bind (obj.Reply.next.spec s r) ?_ + rintro a rfl + rw [nx] + refine WP.spec_bind (obj.Reply.prev.spec s r) ?_ + rintro a rfl + rw [pv, nextRawEq, bind_ok, ra] + refine WP.spec_bind (reply_pop_scA H hc hky hy hh hPopp ht) ?_ + rintro s1 ⟨z, k, hzk, hU, H1, w1, _⟩ + rw [hk] at hzk + cases hzk + let σ1 := popStA σ (scNameA m sc) x n (specPtr r) + have E1 : EpStateRel Z.T E R Bf (L.mem s1) σ1 := + HE.frame PE (fun b hb hf => w1.mem_eq hb (endpFootOffWrite H.toSchedSRN P hf)) (popStA_epView hk _) + have available : ∀ d ∈ rest, d ∈ R := fun d hd => hm d (by rw [hl]; exact List.mem_cons_of_mem _ hd) + refine WP.spec_bind (promotePopHeadA H1 P PE E1 sc rest available) ?_ + rintro s2 ⟨H2, E2, pm, nm, pr, vc⟩ + rw [setNextZeroEq, WP.spec_ok] + have na := replyNextPlaceA (Z := Z) PE hr + obtain ⟨H3, FE⟩ := writeLinkFrames (v := 0#u64) H2 P PE hr na.1 (by omega) (by omega) + let s3 := L.wr s2 (rNextA r) 0#u64 + obtain ⟨sc1, off, r1, tail1⟩ := popHeapWords hk (by rw [hL, hl]; rfl) (by rw [← hl]; exact hn) hky + have rep : ∀ d ∈ R, ∃ yd, σ.kheap (specPtr d) = some (.reply yd) := fun d hd => + let ⟨y, hy, _⟩ := HE.reply d hd + ⟨y, hy⟩ + have kp : ∀ d ∈ R, L.mem s1 (rPrevA d) = L.mem s (rPrevA d) := by + intro d hd + have h := replyPrevPlaceA (Z := Z) PE hd + exact w1.mem_eq h.1 (replyWordOffSchedA H.toSchedSRN P hd (by omega) (by omega)).2 + have kn : ∀ d ∈ R, L.mem s1 (rNextA d) = L.mem s (rNextA d) := by + intro d hd + have h := replyNextPlaceA (Z := Z) PE hd + exact w1.mem_eq h.1 (replyWordOffSchedA H.toSchedSRN P hd (by omega) (by omega)).2 + have popStack : ReplyStackRelA (PopLinkMemA (L.mem s3) r) R Z.C σ1 (scNameA m) + (Function.update stack sc rest) ∧ ReplyIsolatedA (PopLinkMemA (L.mem s3) r) Z.C + (Function.update stack sc rest) r := by + refine HS.pop hc hl ?_ ?_ ?_ (Function.update_self _ _ _) ?_ hk sc1 ?_ hky r1 tail1 rep ?_ + · intro d hd hne + have pa := replyPrevPlaceA (Z := Z) PE hd + have := replyWordsApartA H.toSchedSRN P hr hd hne.symm + rw [popLinkMem_prev H3.toSchedSRN P PE hr hd hne, L.mem_wr_ne na.1 pa.1 (by omega), pm d hd, kp d hd] + · intro d hd hne hhd + have da := replyNextPlaceA (Z := Z) PE hd + have := replyWordsApartA H.toSchedSRN P hr hd hne.symm + rw [popLinkMem_next H3.toSchedSRN P PE hr hd, L.mem_wr_ne na.1 da.1 (by omega), nm d hd hhd, kn d hd] + · intro d hd + have hdR := available d (List.mem_of_mem_head? hd) + have hne : r ≠ d := fun e => hrRest (e ▸ List.mem_of_mem_head? hd) + have da := replyNextPlaceA (Z := Z) PE hdR + have := replyWordsApartA H.toSchedSRN P hr hdR hne + rw [popLinkMem_next H3.toSchedSRN P PE hr hdR, L.mem_wr_ne na.1 da.1 (by omega), pr d hd] + · rw [popLinkMem_next H3.toSchedSRN P PE hr hr] + exact L.mem_wr_same na.1 + · intro c cC cne + obtain ⟨z, k, hz, _⟩ := H.scr.sc c cC + have non (d : U64) (hd : d ∈ R) : scNameA m c ≠ specPtr d := by + obtain ⟨yd, hyd⟩ := rep d hd + intro e + rw [e, hyd] at hz + cases hz + exact off _ (fun e => cne (H.scp.inj c cC sc hc e)) (non r hr) + (fun d hd => non d (available d (List.mem_of_mem_head? hd))) + · intro d hd hne hh + obtain ⟨yd, hyd⟩ := rep d hd + have ne : specPtr d ≠ scNameA m sc := by intro e; rw [e, hk] at hyd; cases hyd + exact off _ ne (fun e => hne (specPtr_inj e)) + (fun q hq e => hh (specPtr_inj e ▸ hq)) + refine ⟨x, n, hk, hU, H3, E2.frame PE FE (KView.refl _), popStack.1, popStack.2, ?_⟩ + intro t ht + have ha := field_aligned (K := 3664#u64) (PE.tcb t ht) (by decide) (by decide) + rw [replyWriteKeepsVcpu PE hr (by omega) (by omega) na.1 ht, vc t ht, + w1.mem_eq ha (vcpuWordOffMcsA H.toSchedSRN ht)] + +end Store +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/IpcScPopReadA.lean b/proofs/Fl4ProofsA64/P3/IpcScPopReadA.lean new file mode 100644 index 000000000..f727bd073 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/IpcScPopReadA.lean @@ -0,0 +1,64 @@ +/- +P3 (AArch64): the head-pop prefix changes the SC head and the two NEXT words. +The removed reply's PREV word is cleared after donation. +-/ +import Fl4ProofsA64.P3.IpcScPopA + +open Aeneas Aeneas.Std Result +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4SpecA64 Sched +noncomputable section + +theorem replyHeadMaskA {sc : U64} (he : sc.val % 2 = 0) : + (sc ||| 1#u64) &&& ~~~1#u64 = sc := by + calc + (sc ||| 1#u64) &&& ~~~1#u64 = + ((sc &&& ~~~1#u64) ||| 1#u64) &&& ~~~1#u64 := + congrArg (fun n : U64 => (n ||| 1#u64) &&& ~~~1#u64) (mask1_of_even he).symm + _ = sc &&& ~~~1#u64 := Fl4ProofsA64.P1.nx_mask _ _ (Or.inr rfl) + _ = sc := mask1_of_even he + +section Store +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + +/-- Read the concrete NEXT and PREV fields of a live stack head. -/ +theorem replyHeadWordsA {s : S} {σ : State Unit} {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (PE : EPlaced Z.T E R Bf) + (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {sc r : U64} (hc : sc ∈ Z.C) {rest : List U64} (hl : stack sc = r :: rest) : + L.mem s (rNextA r) = sc ||| 1#u64 ∧ + L.mem s (rNextA r) &&& ~~~1#u64 = sc ∧ + L.mem s (rPrevA r) &&& ~~~1#u64 = rest.headD 0#u64 := by + obtain ⟨y, n, _, hy⟩ := H.scr.sc sc hc + have hscEven : sc.val % 2 = 0 := by + have hb : 2 ∣ 2 ^ (n + minSchedContextBits) := + dvd_pow_self 2 (by unfold minSchedContextBits; omega) + exact Nat.mod_eq_zero_of_dvd (hb.trans (Nat.dvd_of_mod_eq_zero hy.pl.align)) + obtain ⟨-, hm, -, hw⟩ := HS.stackRel sc hc + rw [hl] at hm hw + have hraw := hw 0 (by simp) + simp only [List.getElem_cons_zero] at hraw + have hprev : L.mem s (rPrevA r) = rest.headD 0#u64 := by + cases rest <;> simpa [List.headD] using hraw.1 + have he : (rest.headD 0#u64).val % 2 = 0 := by + cases re : rest with + | nil => rfl + | cons p ps => + have := (PE.rep p (hm p (by simp [re]))).1 + rw [List.headD_cons] + omega + exact ⟨hraw.2, by rw [hraw.2, replyHeadMaskA hscEven], by rw [hprev, mask1_of_even he]⟩ + +/-- The first stores of the extracted pop, down to the caller-SC read. -/ +def replyPopPrepareA (s : S) (r : U64) : Result S := do + let sc ← obj.Reply.next st r s + let prev ← obj.Reply.prev st r s + let raw ← obj.Reply.next_raw st r s + let s1 ← obj.Sc.set_reply st sc s prev + let s2 ← if prev != 0#u64 then obj.Reply.set_next_raw st prev s1 raw else ok s1 + obj.Reply.set_next st r s2 0#u64 false + +end Store +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/IpcScPopTopA.lean b/proofs/Fl4ProofsA64/P3/IpcScPopTopA.lean new file mode 100644 index 000000000..c096a4923 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/IpcScPopTopA.lean @@ -0,0 +1,176 @@ +/- P3 (AArch64): compose the head-pop prefix, SC return and TCB unlink. -/ +import Fl4ProofsA64.P3.IpcScPopDonateA + +open Aeneas Aeneas.Std Result +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4SpecA64 Sched +noncomputable section + +/-- The three spec steps of a pop, with the list and caller SC read before unbinding. -/ +def replyPopSpecA (p t r : ObjRef) (l : List ObjRef) (callerSc : Option ObjRef) : SMonad Unit Unit := do + replyUnlinkSc p r + Fl4Spec.NonDet.when (the l.head? = r ∧ callerSc = none) (schedContextDonate p t) + replyUnlinkTcb t r + +private theorem popKeepsQueues {σ : State Unit} {p r : ObjRef} {x : SchedContext} {n : Nat} : + (popStA σ p x n r).readyQueues = σ.readyQueues ∧ + (popStA σ p x n r).releaseQueue = σ.releaseQueue := by + have keep (s : State Unit) (q : ObjRef) (o : Option ObjRef) : + (rscA s q o).readyQueues = s.readyQueues ∧ (rscA s q o).releaseQueue = s.releaseQueue := by + rw [rscA_fields] + exact ⟨rfl, rfl⟩ + let rep := repStA σ p x n x.scReplies.tail + let fixed := fixHdA rep p x.scReplies.tail + have e : popStA σ p x n r = rscA fixed r none := rfl + have frame : fixed.readyQueues = σ.readyQueues ∧ fixed.releaseQueue = σ.releaseQueue := by + unfold fixed + cases h : x.scReplies.tail with + | nil => exact ⟨rfl, rfl⟩ + | cons d l => exact keep rep d (some p) + rw [e] + exact ⟨(keep fixed r none).1.trans frame.1, (keep fixed r none).2.trans frame.2⟩ + +section Store +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + +private theorem popLocalAfter {s s' : S} {σ : State Unit} {sc r t : U64} {x : SchedContext} {n : Nat} + (H : SchedSRA st Z m κ s σ) + (H' : SchedSRA st Z m κ s' (popStA σ (scNameA m sc) x n (specPtr r))) + (hc : sc ∈ Z.C) (ht : t ∈ Z.T) (hsc0 : sc ≠ 0#u64) + (hk : σ.kheap (scNameA m sc) = some (.schedContext x n)) + (hv : ∀ f ∈ Z.T, L.mem s' (core.num.U64.wrapping_add f 3664#u64) = + L.mem s (core.num.U64.wrapping_add f 3664#u64)) + (hs : ReplyPopLocalA (st := st) (Z := Z) (m := m) s σ sc t) : + ReplyPopLocalA (st := st) (Z := Z) (m := m) s' (popStA σ (scNameA m sc) x n (specPtr r)) sc t := by + obtain ⟨ni, mig, src⟩ := hs + refine ⟨ni, by rw [H.aff_eq H' ht ht, H'.scCore sc hc, ← H.scCore sc hc]; exact mig, ?_⟩ + rcases src with unb | ⟨f, hf, fs, fon, vcpu, fq⟩ + · obtain ⟨y, k, hy, ry⟩ := H.scr.sc sc hc + rw [hk] at hy + cases hy + obtain ⟨z, j, hz, rz⟩ := H'.scr.sc sc hc + rw [popHeapScA] at hz + cases hz + have he := rz.tcb.symm.trans ry.tcb + rw [← scTcbA_eqSc, unb, optRefS_zero] at he + apply Or.inl + by_contra h + rw [optRefS, if_neg h] at he + cases he + · obtain ⟨xf, hxf, exf⟩ := H.sch.tcb f hf + obtain ⟨yf, hyf, eyf⟩ := H'.sch.tcb f hf + rw [popStA_keepsTcb (r := specPtr r) hk hxf] at hyf + cases hyf + have hfs : L.mem s' (scA f) = sc := by + apply optScA_eq_of (m := m) + · rw [← eyf, exf, fs] + · rw [optScA, if_neg hsc0] + exact fun h => ni.1 (Option.some.inj h) + obtain ⟨rq, rl⟩ := popKeepsQueues (σ := σ) (p := scNameA m sc) (r := specPtr r) (x := x) (n := n) + exact Or.inr ⟨f, hf, hfs, by rw [H.aff_eq H' hf hf, H.cpu_eq H']; exact fon, (hv f hf).trans vcpu, + by rw [rq]; exact fq.1, by rw [rl]; exact fq.2⟩ + +/-- Commit the pending PREV clear and unlink the TCB. -/ +theorem finishPoppedReplyA {s : S} {σ : State Unit} {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) {r t : U64} (hr : r ∈ R) (ht : t ∈ Z.T) + (HS : ReplyStackRelA (PopLinkMemA (L.mem s) r) R Z.C σ (scNameA m) stack) + (iso : ReplyIsolatedA (PopLinkMemA (L.mem s) r) Z.C stack r) + {x : Tcb} {xr : Reply} (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hkr : σ.kheap (specPtr r) = some (.reply xr)) (hb : xr.replyTcb = some (specPtr t)) + (hst : x.tcbState = .blockedOnReply (specPtr r) ∨ + ∃ ep d, x.tcbState = .blockedOnReceive ep (some (specPtr r)) d) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) : + (obj.Reply.set_prev st r s 0#u64 >>= fun s1 => ipc.reply_unlink st s1 r t) ⦃ s' => ∃ σ', + replyUnlinkTcb (Z := Unit) (specPtr t) (specPtr r) σ = ({((), σ')}, False) ∧ + SchedSRA st Z m κ s' σ' ∧ EpStateRel Z.T E R Bf (L.mem s') σ' ∧ + ReplyStackRelA (L.mem s') R Z.C σ' (scNameA m) stack ∧ ReplyIsolatedA (L.mem s') Z.C stack r ⦄ := by + rw [setPrevZeroEq, Std.bind_tc_ok] + have pa := replyPrevPlaceA (Z := Z) PE hr + obtain ⟨H1, FE⟩ := writeLinkFrames (v := 0#u64) H P PE hr pa.1 (by omega) (by omega) + have same (b : U64) (ha : Aligned b) : + L.mem (L.wr s (rPrevA r) 0#u64) b = PopLinkMemA (L.mem s) r b := by + rw [L.mem_wr _ _ _ _ pa.1 ha] + unfold PopLinkMemA + by_cases e : b = rPrevA r + · subst b; simp + · simp [e, Ne.symm e] + have links : ∀ d ∈ R, L.mem (L.wr s (rPrevA r) 0#u64) (rNextA d) = PopLinkMemA (L.mem s) r (rNextA d) ∧ + L.mem (L.wr s (rPrevA r) 0#u64) (rPrevA d) = PopLinkMemA (L.mem s) r (rPrevA d) := fun d hd => + ⟨same _ (replyNextPlaceA (Z := Z) PE hd).1, same _ (replyPrevPlaceA (Z := Z) PE hd).1⟩ + exact unlinkReplyRelA H1 P PE (HE.frame PE FE (KView.refl _)) + (HS.frame links (fun _ _ y k hy => ⟨y, k, hy, rfl⟩) (fun _ _ y hy => ⟨y, hy, rfl⟩)) ht hr + ⟨iso.1, (links r hr).1.trans iso.2.1, (links r hr).2.trans iso.2.2⟩ hk hkr hb hst hq + +/-- The extracted pop split at the caller's SC test. -/ +theorem reply_pop_programA (s : S) (a : A) (r t sc : U64) + (hn : obj.Reply.next st r s = ok sc) : + ipc.reply_pop st ar s a r t = (do + let s1 ← replyPopPrepareA (st := st) s r + let i ← obj.Tcb.sc st t s1 + let (s2, a2) ← if i = 0#u64 then schedcontext.donate st ar s1 a sc t else ok (s1, a) + let s3 ← obj.Reply.set_prev st r s2 0#u64 >>= fun s3 => ipc.reply_unlink st s3 r t + ok (s3, a2)) := by + unfold replyPopPrepareA ipc.reply_pop + rw [hn, bind_ok, bind_ok] + simp only [Std.bind_tc_eq, Std.bind_assoc] + +/-- Full head pop. Donation follows the single-core-view [S] scope. -/ +theorem reply_pop_relA {s : S} {σ : State Unit} (a : A) {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {sc r t : U64} (hc : sc ∈ Z.C) (hsc0 : sc ≠ 0#u64) (ht : t ∈ Z.T) + {rest : List U64} (hl : stack sc = r :: rest) + {xt : Tcb} {xr : Reply} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hkr : σ.kheap (specPtr r) = some (.reply xr)) (hb : xr.replyTcb = some (specPtr t)) + (hst : xt.tcbState = .blockedOnReply (specPtr r) ∨ + ∃ ep d, xt.tcbState = .blockedOnReceive ep (some (specPtr r)) d) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) + (hlocal : xt.tcbSchedContext = none → ReplyPopLocalA (st := st) (Z := Z) (m := m) s σ sc t) + (hpost : ∀ σ', ((replyUnlinkSc (Z := Unit) (scNameA m sc) (specPtr r)) >>= fun _ => + schedContextDonate (scNameA m sc) (specPtr t)) σ = ({((), σ')}, False) → RelIfBoundSc σ' (specPtr t)) : + ipc.reply_pop st ar s a r t ⦃ out => out.2 = a ∧ ∃ σ', + replyPopSpecA (scNameA m sc) (specPtr t) (specPtr r) ((stack sc).map specPtr) xt.tcbSchedContext σ = + ({((), σ')}, False) ∧ + IpcSchedRelA st Z m κ out.1 σ' ∧ EpStateRel Z.T E R Bf (L.mem out.1) σ' ∧ + ReplyStackRelA (L.mem out.1) R Z.C σ' (scNameA m) (Function.update stack sc rest) ∧ + ReplyIsolatedA (L.mem out.1) Z.C (Function.update stack sc rest) r ⦄ := by + have hr := (HS.stackRel sc hc).2.1 r (by simp [hl]) + have nx : obj.Reply.next st r s = ok sc := + eq_of_spec_fd (WP.spec_mono (obj.Reply.next.spec s r) fun _ h => h.trans (replyHeadWordsA H PE HS hc hl).2.1) + rw [reply_pop_programA (ar := ar) s a r t sc nx] + refine WP.spec_bind (replyPopPrepareRefA H P PE HE HS hc hl) ?_ + rintro s1 ⟨x, n, hk, hU, H1, E1, S1, iso1, v1⟩ + let σ1 := popStA σ (scNameA m sc) x n (specPtr r) + have kt1 : σ1.kheap (specPtr t) = some (.tcb xt) := popStA_keepsTcb hk hkt + have h1 : ((stack sc).map specPtr).head? = some (specPtr r) := by rw [hl]; rfl + refine WP.spec_bind (obj.Tcb.sc.spec s1 t) ?_ + rintro i rfl + refine WP.spec_bind (replyPopDonateRefA (ar := ar) a H1 P PE E1 S1 iso1 hc hsc0 ht hr kt1 h1 + (fun hn => popLocalAfter H H1 hc ht hsc0 hk v1 (hlocal hn)) (fun σ2 hD => hpost σ2 (by rw [nd_bind_oneSc hU, hD]))) ?_ + rintro ⟨s2, a2⟩ ⟨ha, σ2, Q2, hD, H2, E2, S2, iso2, keep⟩ + obtain ⟨x2, kt2, c, ex2⟩ := keep.tcb _ _ kt1 + have kp := popStA_epView hk (specPtr r) + obtain ⟨y, kr1, yr⟩ := kp.2.1 _ _ hkr + have kr2 := keep.obj _ _ kr1 (fun _ _ e => (by cases e)) (fun _ e => (by cases e)) + have hq2 : ∀ e ∈ E, ∀ z, σ2.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue := by + intro e he z hz + obtain ⟨z0, hz0, _⟩ := HE.ep e he + have hz2 := keep.obj _ _ (kp.1 _ _ hz0) (fun _ _ e => (by cases e)) (fun _ e => (by cases e)) + rw [hz] at hz2 + cases hz2 + exact hq e he _ hz0 + refine WP.spec_bind (finishPoppedReplyA H2 P PE E2 hr ht S2 iso2 kt2 kr2 (yr.trans hb) + (by rw [ex2]; exact hst) hq2) ?_ + rintro s3 ⟨σ3, hF, H3, E3, S3, iso3⟩ + rw [WP.spec_ok] + refine ⟨ha, σ3, ?_, ⟨Q2, Z.R, H3⟩, E3, S3, iso3⟩ + unfold replyPopSpecA + rw [nd_bind_oneSc hU, nd_bind_oneSc hD] + exact hF + +end Store +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/IpcScPushA.lean b/proofs/Fl4ProofsA64/P3/IpcScPushA.lean new file mode 100644 index 000000000..93a6195ed --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/IpcScPushA.lean @@ -0,0 +1,953 @@ +/- +P3 (AArch64): reply-stack push and migration-free SC donation. + +IPC owns the NEXT and PREV words. The SC relation owns REPLY. +-/ +import Fl4ProofsA64.P3.IpcScReplyA +import Fl4ProofsA64.P3.EpReplyPushA +import Fl4ProofsA64.P3.IpcScGlueA +import Fl4ProofsA64.P3.ScPopDonA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4SpecA64 Sched + +noncomputable section + +/-- A reply ready to be placed on a call stack. -/ +def ReplyIsolatedA (mem : U64 → U64) (C : Set U64) (stack : U64 → List U64) (r : U64) : Prop := + (∀ sc ∈ C, r ∉ stack sc) ∧ mem (rNextA r) = 0#u64 ∧ mem (rPrevA r) = 0#u64 + +/-- Adding an isolated reply preserves the list interpretation and the detached links. -/ +theorem ReplyStackRelA.push {R C : Set U64} {mem mem' : U64 → U64} {σ σ' : State Unit} + {scRef : U64 → ObjRef} {stack : U64 → List U64} (h : ReplyStackRelA mem R C σ scRef stack) + {sc r : U64} (hc : sc ∈ C) (hr : r ∈ R) (hR0 : ∀ d ∈ R, d ≠ 0#u64) + (hi : ReplyIsolatedA mem C stack r) + (hprev : ∀ d ∈ R, d ≠ r → mem' (rPrevA d) = mem (rPrevA d)) + (hnext : ∀ d ∈ R, d ≠ r → (stack sc).head? ≠ some d → mem' (rNextA d) = mem (rNextA d)) + (hhead : ∀ d, (stack sc).head? = some d → mem' (rNextA d) = r) + (hprevR : mem' (rPrevA r) = (stack sc).headD 0#u64) (hnextR : mem' (rNextA r) = sc ||| 1#u64) + {x : SchedContext} {n : Nat} (hk : σ.kheap (scRef sc) = some (.schedContext x n)) + (hk' : σ'.kheap (scRef sc) = some (.schedContext { x with scReplies := specPtr r :: x.scReplies } n)) + (hks : ∀ c ∈ C, c ≠ sc → σ'.kheap (scRef c) = σ.kheap (scRef c)) + {xr : Reply} (hkr' : σ'.kheap (specPtr r) = some (.reply { xr with replySc := some (scRef sc) })) + (hkh : ∀ d x, (stack sc).head? = some d → σ'.kheap (specPtr d) = some (.reply x) → x.replySc = none) + (hkd : ∀ d ∈ R, d ≠ r → (stack sc).head? ≠ some d → σ'.kheap (specPtr d) = σ.kheap (specPtr d)) : + ReplyStackRelA mem' R C σ' scRef (Function.update stack sc (r :: stack sc)) := by + let stack' := Function.update stack sc (r :: stack sc) + have hnew : stack' sc = r :: stack sc := Function.update_self _ _ _ + have hother : ∀ q, q ≠ sc → stack' q = stack q := fun _ eq => Function.update_of_ne eq _ _ + obtain ⟨⟨y, k, hy, hlist⟩, hm, hnd, hw⟩ := h.stackRel sc hc + rw [hk] at hy + cases hy + have hmem : ∀ d q, q ∈ C → d ∈ stack q → d ∈ stack sc → q = sc := + fun d q hq ha hb => h.unique q hq sc hc d ha hb + have oldMember : ∀ q d, q ∈ C → d ∈ stack' q → (d = r ∧ q = sc) ∨ d ∈ stack q := by + intro q d hq hd + by_cases eq : q = sc + · subst q + rw [hnew] at hd + exact (List.mem_cons.mp hd).imp (fun e => ⟨e, rfl⟩) id + · exact Or.inr (hother q eq ▸ hd) + change ReplyStackRelA mem' R C σ' scRef stack' + refine ⟨fun q hq => ?_, fun q hq p hp d hd he => ?_, fun d hd x' hx' p => ?_, fun d hd hdet => ?_⟩ + · by_cases eq : q = sc + · subst q + rw [hnew] + refine ⟨⟨_, n, hk', by rw [hlist]; rfl⟩, ?_, List.nodup_cons.mpr ⟨hi.1 sc hc, hnd⟩, ?_⟩ + · intro d hd + rcases List.mem_cons.mp hd with rfl | hd + · exact hr + · exact hm d hd + · intro i hidx + cases i with + | zero => + refine ⟨?_, hnextR⟩ + change mem' (rPrevA r) = (r :: stack sc).getD 1 0#u64 + rw [hprevR] + cases stack sc <;> rfl + | succ j => + have hj : j < (stack sc).length := by simpa using hidx + have hdj := hm _ (List.getElem_mem hj) + have hn : (stack sc)[j] ≠ r := fun e => hi.1 sc hc (e ▸ List.getElem_mem hj) + have ha : (stack sc).head? = some (stack sc)[0] := by + rw [List.head?_eq_getElem?] + exact List.getElem?_eq_getElem (by omega) + obtain ⟨v, w⟩ := hw j hj + refine ⟨?_, ?_⟩ + · change mem' (rPrevA (stack sc)[j]) = (r :: stack sc).getD (j + 2) 0#u64 + rw [hprev _ hdj hn, v] + rfl + · change mem' (rNextA (stack sc)[j]) = (r :: stack sc).getD j 0#u64 + cases j with + | zero => rw [hhead _ ha]; rfl + | succ j => + have hne : (stack sc).head? ≠ some (stack sc)[j + 1] := by + rw [ha] + intro e + have bad := (List.nodup_iff_injective_get.mp hnd) + (a₁ := ⟨0, by omega⟩) (a₂ := ⟨j + 1, hj⟩) (Option.some.inj e) + simp at bad + rw [hnext _ hdj hn hne, w] + rfl + · rw [hother q eq] + obtain ⟨⟨y, k, hy, hl⟩, hm, hnd, hw⟩ := h.stackRel q hq + refine ⟨⟨y, k, (hks q hq eq).trans hy, hl⟩, hm, hnd, fun i hi' => ?_⟩ + have hm' := List.getElem_mem hi' + have hn : (stack q)[i] ≠ r := fun e => hi.1 q hq (e ▸ hm') + have hh : (stack sc).head? ≠ some (stack q)[i] := fun e => eq (hmem _ q hq hm' (List.mem_of_mem_head? e)) + rw [hprev _ (hm _ hm') hn, hnext _ (hm _ hm') hn hh] + exact hw i hi' + · rcases oldMember q d hq hd with ⟨rfl, rfl⟩ | a <;> + rcases oldMember p d hp he with ⟨ed, rfl⟩ | b + · rfl + · exact (hi.1 p hp b).elim + · subst d; exact (hi.1 q hq a).elim + · exact h.unique q hq p hp d a b + · by_cases ed : d = r + · subst d + rw [hkr'] at hx' + cases hx' + constructor + · intro e + cases e + exact ⟨sc, hc, rfl, by rw [hnew]; rfl⟩ + · rintro ⟨q, hq, hqp, hh⟩ + by_cases eq : q = sc + · subst q; rw [← hqp] + · rw [hother q eq] at hh; exact (hi.1 q hq (List.mem_of_mem_head? hh)).elim + · by_cases eh : (stack sc).head? = some d + · rw [hkh d x' eh hx'] + constructor + · intro e; cases e + · rintro ⟨q, hq, -, hh⟩ + by_cases eq : q = sc + · subst q; rw [hnew] at hh; simp at hh; exact (ed hh.symm).elim + · rw [hother q eq] at hh + exact (eq (hmem d q hq (List.mem_of_mem_head? hh) (List.mem_of_mem_head? eh))).elim + · rw [hkd d hd ed eh] at hx' + rw [h.owner d hd x' hx'] + constructor + · rintro ⟨q, hq, hqp, hh⟩ + refine ⟨q, hq, hqp, ?_⟩ + by_cases eq : q = sc + · subst q; exact (eh hh).elim + · rw [hother q eq]; exact hh + · rintro ⟨q, hq, hqp, hh⟩ + refine ⟨q, hq, hqp, ?_⟩ + by_cases eq : q = sc + · subst q; rw [hnew] at hh; simp at hh; exact (ed hh.symm).elim + · rw [hother q eq] at hh; exact hh + · have hdR : d ≠ r := fun e => hdet sc hc (by rw [e, hnew]; simp) + have oldDetached : ∀ a, (∀ q ∈ C, a ∉ stack' q) → ∀ q ∈ C, a ∉ stack q := by + intro a h q hq ha + apply h q hq + by_cases eq : q = sc + · subst q; rw [hnew]; exact List.mem_cons_of_mem _ ha + · rw [hother q eq]; exact ha + have stillDetached : ∀ a, (∀ q ∈ C, a ∉ stack q) → a ≠ r → ∀ q ∈ C, a ∉ stack' q := by + intro a ha hne q hq h' + rcases oldMember q a hq h' with ⟨e, -⟩ | b + · exact hne e + · exact ha q hq b + have nh : ∀ a, (∀ q ∈ C, a ∉ stack q) → (stack sc).head? ≠ some a := + fun a ha hh => ha sc hc (List.mem_of_mem_head? hh) + obtain ⟨hb, hn, hp⟩ := h.detached d hd (oldDetached d hdet) + rw [hprev d hd hdR, hnext d hd hdR (nh d (oldDetached d hdet))] + refine ⟨hb, fun h0 => ?_, fun h0 => ?_⟩ + · obtain ⟨ha, hda, he⟩ := hn h0 + have hne : mem (rNextA d) ≠ r := by + intro e + rw [e, hi.2.2] at he + exact hR0 d hd he.symm + exact ⟨ha, stillDetached _ hda hne, (hprev _ ha hne).trans he⟩ + · obtain ⟨ha, hda, he⟩ := hp h0 + have hne : mem (rPrevA d) ≠ r := by + intro e + rw [e, hi.2.1] at he + exact hR0 d hd he.symm + exact ⟨ha, stillDetached _ hda hne, (hnext _ ha hne (nh _ hda)).trans he⟩ + + +private theorem replyScKView (σ : State Unit) (r : ObjRef) (v : Option ObjRef) : + KView σ (rscA σ r v) := by + refine ⟨?_, ?_, ?_, ?_⟩ + · intro p x hx + rcases rscA_kheap σ r v p with h | ⟨rfl, y, hy, _⟩ + · exact h.trans hx + · rw [hx] at hy; cases hy + · intro p x hx + rcases rscA_kheap σ r v p with h | ⟨rfl, y, hy, h'⟩ + · exact ⟨x, h.trans hx, rfl⟩ + · rw [hx] at hy; cases hy; exact ⟨_, h', rfl⟩ + · intro p x hx + rcases rscA_kheap σ r v p with h | ⟨rfl, y, hy, _⟩ + · exact ⟨x, h.trans hx, rfl, rfl, rfl, rfl, rfl⟩ + · rw [hx] at hy; cases hy + · rw [rscA_fields] + +private theorem unlinkHeadKView (σ : State Unit) (l : List ObjRef) : KView σ (unlHdA σ l) := by + cases l with + | nil => exact KView.refl _ + | cons p _ => exact replyScKView σ p none + +private theorem scReplyListKView {σ : State Unit} {p : ObjRef} {x : SchedContext} {n : Nat} + (hk : σ.kheap p = some (.schedContext x n)) (l : List ObjRef) : KView σ (repStA σ p x n l) := by + have keep : ∀ q (obj : KernelObject), σ.kheap q = some obj → (∀ y m, obj ≠ .schedContext y m) → + (repStA σ p x n l).kheap q = some obj := by + intro q obj ho hne + apply (repStA_ne ?_).trans ho + rintro rfl + rw [hk] at ho + exact hne _ _ (Option.some.inj ho.symm) + exact ⟨fun q y hy => keep q _ hy (by intro _ _ e; cases e), + fun q y hy => ⟨y, keep q _ hy (by intro _ _ e; cases e), rfl⟩, + fun q y hy => ⟨y, keep q _ hy (by intro _ _ e; cases e), rfl, rfl, rfl, rfl, rfl⟩, rfl⟩ + +/-- Binding the reply preserves the endpoint view of the state. -/ +theorem bindStA_epView {σ : State Unit} {p : ObjRef} {x : SchedContext} {n : Nat} + (hk : σ.kheap p = some (.schedContext x n)) (r : ObjRef) : KView σ (bindStA σ p x n r) := + (unlinkHeadKView σ x.scReplies).trans + ((scReplyListKView (unlHdA_sc.mpr hk) _).trans (replyScKView _ r (some p))) + +/-- Popping the SC's reply list changes neither endpoint contents nor reply backlinks. -/ +theorem popStA_epView {σ : State Unit} {p : ObjRef} {x : SchedContext} {n : Nat} + (hk : σ.kheap p = some (.schedContext x n)) (r : ObjRef) : KView σ (popStA σ p x n r) := by + rw [popStA_def] + apply (scReplyListKView hk x.scReplies.tail).trans + apply KView.trans _ (replyScKView _ r none) + cases x.scReplies.tail with + | nil => exact KView.refl _ + | cons h t => exact replyScKView _ h (some p) + +private theorem bindStA_keepTcb {σ : State Unit} {p r t : ObjRef} {x : SchedContext} {n : Nat} {xt : Tcb} + (hk : σ.kheap p = some (.schedContext x n)) (ht : σ.kheap t = some (.tcb xt)) : + (bindStA σ p x n r).kheap t = some (.tcb xt) := by + have off : t ≠ p := by + rintro rfl + rw [hk] at ht + cases ht + have rkeep (s : State Unit) (q : ObjRef) (v : Option ObjRef) (h : s.kheap t = some (.tcb xt)) : + (rscA s q v).kheap t = some (.tcb xt) := by + rcases rscA_kheap s q v t with h' | ⟨rfl, y, hy, _⟩ + · exact h'.trans h + · rw [h] at hy; cases hy + rw [bindStA_def] + apply rkeep + rw [repStA_ne off] + cases e : x.scReplies with + | nil => exact ht + | cons h l => exact rkeep σ h none ht + +private theorem bindStKeepQueue {σ : State Unit} {p r : ObjRef} {x : SchedContext} {n : Nat} : + (bindStA σ p x n r).readyQueues = σ.readyQueues ∧ (bindStA σ p x n r).releaseQueue = σ.releaseQueue := by + have h (s : State Unit) (q : ObjRef) (o : Option ObjRef) : + (rscA s q o).readyQueues = s.readyQueues ∧ (rscA s q o).releaseQueue = s.releaseQueue := by + rw [rscA_fields] + exact ⟨rfl, rfl⟩ + have hold : (unlHdA σ x.scReplies).readyQueues = σ.readyQueues ∧ + (unlHdA σ x.scReplies).releaseQueue = σ.releaseQueue := by + cases x.scReplies with + | nil => exact ⟨rfl, rfl⟩ + | cons q _ => exact h σ q none + have hnew := h (repStA (unlHdA σ x.scReplies) p x n (r :: x.scReplies)) r (some p) + exact ⟨hnew.1.trans hold.1, hnew.2.trans hold.2⟩ + +/-- Frame a reply stack through changes that preserve its links, SC lists and spec reply owners. -/ +theorem ReplyStackRelA.frame {R C : Set U64} {mem mem' : U64 → U64} {σ σ' : State Unit} + {scRef : U64 → ObjRef} {stack : U64 → List U64} (h : ReplyStackRelA mem R C σ scRef stack) + (hlink : ∀ r ∈ R, mem' (rNextA r) = mem (rNextA r) ∧ mem' (rPrevA r) = mem (rPrevA r)) + (hs : ∀ c ∈ C, ∀ x n, σ.kheap (scRef c) = some (.schedContext x n) → + ∃ y k, σ'.kheap (scRef c) = some (.schedContext y k) ∧ y.scReplies = x.scReplies) + (hr : ∀ r ∈ R, ∀ x', σ'.kheap (specPtr r) = some (.reply x') → + ∃ x, σ.kheap (specPtr r) = some (.reply x) ∧ x'.replySc = x.replySc) : + ReplyStackRelA mem' R C σ' scRef stack := by + refine ⟨?_, h.unique, ?_, ?_⟩ + · intro sc hc + obtain ⟨⟨x, n, hx, hl⟩, hm, hn, hw⟩ := h.stackRel sc hc + obtain ⟨y, k, hy, e⟩ := hs sc hc x n hx + refine ⟨⟨y, k, hy, e.trans hl⟩, hm, hn, fun i hi => ?_⟩ + have ha := hlink _ (hm _ (List.getElem_mem hi)) + rw [ha.1, ha.2] + exact hw i hi + · intro r hrR x' hx' p + obtain ⟨x, hx, eq⟩ := hr r hrR x' hx' + rw [eq] + exact h.owner r hrR x hx p + · intro r hrR hd + obtain ⟨hbit, hn, hp⟩ := h.detached r hrR hd + rw [(hlink r hrR).1, (hlink r hrR).2] + refine ⟨hbit, fun e => ?_, fun e => ?_⟩ + · obtain ⟨hq, hqD, he⟩ := hn e + exact ⟨hq, hqD, (hlink _ hq).2.trans he⟩ + · obtain ⟨hq, hqD, he⟩ := hp e + exact ⟨hq, hqD, (hlink _ hq).1.trans he⟩ + + +private theorem unlinkHeadAway {σ : State Unit} {l : List ObjRef} {p : ObjRef} + (hne : l.head? ≠ some p) : (unlHdA σ l).kheap p = σ.kheap p := by + cases l with + | nil => rfl + | cons q _ => + exact rscA_ne none (fun e => hne (by simp [e])) + +theorem bindStA_pushStack {R C : Set U64} {mem mem' : U64 → U64} {σ : State Unit} + {scRef : U64 → ObjRef} {stack : U64 → List U64} (H : ReplyStackRelA mem R C σ scRef stack) + {sc r : U64} (hc : sc ∈ C) (hr : r ∈ R) (hR0 : ∀ d ∈ R, d ≠ 0#u64) + (hi : ReplyIsolatedA mem C stack r) + (hName : ∀ q ∈ C, q ≠ sc → scRef q ≠ scRef sc) + (hprev : ∀ d ∈ R, d ≠ r → mem' (rPrevA d) = mem (rPrevA d)) + (hnext : ∀ d ∈ R, d ≠ r → (stack sc).head? ≠ some d → mem' (rNextA d) = mem (rNextA d)) + (hhead : ∀ d, (stack sc).head? = some d → mem' (rNextA d) = r) + (hprevR : mem' (rPrevA r) = (stack sc).headD 0#u64) (hnextR : mem' (rNextA r) = sc ||| 1#u64) + {x : SchedContext} {n : Nat} (hk : σ.kheap (scRef sc) = some (.schedContext x n)) + {xr : Reply} (hkr : σ.kheap (specPtr r) = some (.reply xr)) + (hRep : ∀ d ∈ R, ∃ xd, σ.kheap (specPtr d) = some (.reply xd)) : + ReplyStackRelA mem' R C (bindStA σ (scRef sc) x n (specPtr r)) scRef + (Function.update stack sc (r :: stack sc)) := by + obtain ⟨⟨y, k, hy, hlist⟩, hm, _, _⟩ := H.stackRel sc hc + rw [hk] at hy + cases hy + have hdistinct (q : U64) (hq : q ∈ R) : specPtr q ≠ scRef sc := by + obtain ⟨z, hz⟩ := hRep q hq + intro e + rw [e, hk] at hz + cases hz + have hnotHead (q : U64) (hq : (stack sc).head? ≠ some q) : + x.scReplies.head? ≠ some (specPtr q) := by + rw [hlist, List.head?_map] + intro e + cases eq : (stack sc).head? with + | none => simp [eq] at e + | some d => + have h := specPtr_inj (by simpa [eq] using e) + exact hq (by rw [eq, h]) + have hnr : (stack sc).head? ≠ some r := fun e => hi.1 sc hc (List.mem_of_mem_head? e) + have updateR : (repStA (unlHdA σ x.scReplies) (scRef sc) x n (specPtr r :: x.scReplies)).kheap (specPtr r) = + some (.reply xr) := by + rw [repStA_ne (hdistinct r hr), unlinkHeadAway (hnotHead r hnr)] + exact hkr + refine H.push hc hr hR0 hi hprev hnext hhead hprevR hnextR hk ?_ ?_ (xr := xr) ?_ ?_ ?_ + · rw [bindStA_def] + have hne := (hdistinct r hr).symm + rw [rscA_ne _ hne, repStA, updKSc_kheap, Function.update_self] + · intro q hq hne + obtain ⟨⟨z, k, hz, _⟩, _, _, _⟩ := H.stackRel q hq + have hnr' : scRef q ≠ specPtr r := by intro e; rw [e, hkr] at hz; cases hz + rw [bindStA_def, rscA_ne _ hnr', repStA_ne (hName q hq hne), unlHdA_sc.mpr hz, hz] + · rw [bindStA_def, rscA_of updateR, updKSc_kheap, Function.update_self] + · intro q xq hq hxq + have hqR := hm q (List.mem_of_mem_head? hq) + have hqr : specPtr q ≠ specPtr r := fun e => hnr (specPtr_inj e ▸ hq) + have hhd : x.scReplies.head? = some (specPtr q) := by rw [hlist, List.head?_map, hq]; rfl + obtain ⟨z, hz⟩ := hRep q hqR + rw [bindStA_def, rscA_ne _ hqr, repStA_ne (hdistinct q hqR)] at hxq + cases l : x.scReplies with + | nil => simp [l] at hhd + | cons p tail => + have eq : p = specPtr q := by simpa [l] using hhd + rw [l, unlHdA_cons, eq, rscA_of hz, updKSc_kheap, Function.update_self] at hxq + cases hxq + rfl + · intro q hq hqr hh + rw [bindStA_def, rscA_ne _ (fun e => hqr (specPtr_inj e)), repStA_ne (hdistinct q hq), + unlinkHeadAway (hnotHead q hh)] + + + +section SpecDonation +open Fl4Spec + +def KeepsHeap {α : Type} (f : SMonad Unit α) : Prop := + ∀ σ σ' a, (a, σ') ∈ (f σ).1 → σ'.kheap = σ.kheap + +private theorem keepHeapBind {α β : Type} {f : SMonad Unit α} {g : α → SMonad Unit β} + (hf : KeepsHeap f) (hg : ∀ a, KeepsHeap (g a)) : KeepsHeap (f >>= g) := by + rintro σ σ' b ⟨⟨a, s⟩, h₁, h₂⟩ + exact (hg a s σ' b h₂).trans (hf σ s a h₁) + +private theorem keepHeapPure {α : Type} (a : α) : KeepsHeap (pure a) := by + intro σ σ' b h + have e : (b, σ') = (a, σ) := Set.mem_singleton_iff.mp h + rw [show σ' = σ from congrArg Prod.snd e] + +private theorem keepHeapModify (f : State Unit → State Unit) (he : ∀ σ, (f σ).kheap = σ.kheap) : + KeepsHeap (NonDet.modify f) := by + intro σ σ' a h + have e : (a, σ') = ((), f σ) := Set.mem_singleton_iff.mp h + rw [show σ' = f σ from congrArg Prod.snd e, he] + +private theorem keepHeapGets {α : Type} (f : State Unit → α) : KeepsHeap (NonDet.gets f) := + fun σ σ' a h => keepHeapPure (f σ) σ σ' a h + +private theorem keepHeapOpt {α : Type} (o : Option α) : KeepsHeap (NonDet.assertOpt o) := by + cases o with + | none => intro σ σ' a h; exact h.elim + | some a => exact keepHeapPure a + +private theorem keepHeapGetsThe {α : Type} (f : State Unit → Option α) : KeepsHeap (NonDet.getsThe f) := + fun σ => keepHeapOpt (f σ) σ + +private theorem keepHeapAssert {p : Prop} [Decidable p] : KeepsHeap (NonDet.assert p) := by + intro σ σ' a h + exact congrArg (fun p : Unit × State Unit => p.2.kheap) h.2 + +private theorem keepHeapWhen {p : Prop} [Decidable p] {f : SMonad Unit Unit} + (h : KeepsHeap f) : KeepsHeap (NonDet.when p f) := by + unfold NonDet.when + split_ifs + · exact h + · exact keepHeapPure () + +local macro "keep_reply_heap" : tactic => `(tactic| + repeat' first + | assumption + | apply keepHeapGetsThe + | apply keepHeapGets + | apply keepHeapPure + | apply keepHeapOpt + | apply keepHeapAssert + | exact keepHeapModify _ (fun _ => rfl) + | apply keepHeapWhen + | refine keepHeapBind ?_ (fun _ => ?_)) + +private theorem rescheduleKeepsHeap : KeepsHeap (rescheduleRequired (Z := Unit)) := by + unfold rescheduleRequired + refine keepHeapBind (keepHeapGets _) fun act => ?_ + cases act <;> + simp only [tcbSchedAction, getTcbQueue, setTcbQueue, threadGet, getScRefillSufficient, getScRefillReady, + setSchedulerAction] <;> + keep_reply_heap + +theorem testRescheduleKeepsHeap (f : ObjRef) : KeepsHeap (testReschedule (Z := Unit) f) := by + have h := rescheduleKeepsHeap + unfold testReschedule + keep_reply_heap + unfold NonDet.when + split + · exact h + · exact keepHeapPure () + + +/-- A donation from a caller outside the queues preserves every SC's reply list. -/ +theorem donationKeepsReplyLists {σ σ' : State Unit} {p f t : ObjRef} {y : SchedContext} {k : Nat} {xf xt : Tcb} + (hp : σ.kheap p = some (.schedContext y k)) (hy : y.scTcb = some f) + (hf : σ.kheap f = some (.tcb xf)) (ht : σ.kheap t = some (.tcb xt)) (hft : f ≠ t) + (hdom : xf.tcbDomain = 0) (hnq : NotQueued σ f) + (h : ((), σ') ∈ (schedContextDonate (Z := Unit) p t σ).1) + {q : ObjRef} {x : SchedContext} {n : Nat} (hk : σ.kheap q = some (.schedContext x n)) : + ∃ x', σ'.kheap q = some (.schedContext x' n) ∧ x'.scReplies = x.scReplies := by + have hget : getScObjRef (Z := Unit) SchedContext.scTcb p σ = ({(some f, σ)}, False) := by + unfold getScObjRef + rw [nd_bind_oneSc (getSchedContext_eqA hp), hy] + rfl + have hs : tcbSchedAction (Z := Unit) tcbSchedDequeue f σ = ({((), σ)}, False) := by + rw [tcbSchedAction_eqA hf hdom] + have hfilt : tcbSchedDequeue f (σ.readyQueues 0 xf.tcbPriority) = σ.readyQueues 0 xf.tcbPriority := by + unfold tcbSchedDequeue + exact List.filter_eq_self.mpr fun a ha => by + have hne : f ≠ a := fun e => hnq.1 0 xf.tcbPriority (e ▸ ha) + simpa using hne + rw [hfilt, setRqA_selfA] + unfold schedContextDonate at h + rw [nd_bind_oneSc hget] at h + simp only [NonDet.when] at h + rw [if_pos (show (some f : Option ObjRef) ≠ none by simp)] at h + have hpre : (do + let f' ← NonDet.assertOpt (σ := State Unit) (some f) + tcbSchedAction tcbSchedDequeue f' + tcbReleaseRemove f' + setTcbObjRef tcbSchedContextUpdate f' none + testReschedule f' : SMonad Unit Unit) σ = + testReschedule (Z := Unit) f (unbTStA σ f xf) := by + rw [nd_bind_oneSc (show (NonDet.assertOpt (σ := State Unit) (some f)) σ = ({(f, σ)}, False) from rfl)] + rw [nd_bind_oneSc hs, nd_bind_oneSc (tcbReleaseRemove_notA hnq.2)] + exact nd_bind_oneSc (threadSet_eqSc _ hf) _ + change ∃ z ∈ ((do + let f' ← NonDet.assertOpt (σ := State Unit) (some f) + tcbSchedAction tcbSchedDequeue f' + tcbReleaseRemove f' + setTcbObjRef tcbSchedContextUpdate f' none + testReschedule f' : SMonad Unit Unit) σ).1, + ((), σ') ∈ ((do + setScObjRef scTcbUpdate p (some t) + setTcbObjRef tcbSchedContextUpdate t (some p) : SMonad Unit Unit) z.2).1 at h + rw [hpre] at h + obtain ⟨⟨_, s⟩, hres, hs'⟩ := h + have hsk := testRescheduleKeepsHeap _ _ _ _ hres + have old (q : ObjRef) (hq : q ≠ f) : s.kheap q = σ.kheap q := by + rw [hsk, unbTStA_kheap, if_neg hq] + have qoff : q ≠ f := by intro e; rw [e, hf] at hk; cases hk + have poff : p ≠ f := by intro e; rw [e, hf] at hp; cases hp + have ptoff : p ≠ t := by intro e; rw [e, ht] at hp; cases hp + have hps := (old p poff).trans hp + have hts : (updKSc s p (.schedContext { y with scTcb := some t } k)).kheap t = some (.tcb xt) := by + rw [updKSc_kheap, Function.update_of_ne ptoff.symm, old t hft.symm, ht] + change _ ∈ ((updateSchedContext p _ >>= _) s).1 at hs' + rw [nd_bind_oneSc (updateSchedContext_eqA hps _)] at hs' + change _ ∈ (threadSet _ t (updKSc s p (.schedContext { y with scTcb := some t } k))).1 at hs' + rw [threadSet_eqSc _ hts] at hs' + have heq : σ' = updKSc (updKSc s p (.schedContext { y with scTcb := some t } k)) + t (.tcb { xt with tcbSchedContext := some p }) := + congrArg Prod.snd (Set.mem_singleton_iff.mp hs') + rw [heq, updKSc_kheap, Function.update_of_ne (by intro e; rw [e, ht] at hk; cases hk), updKSc_kheap] + by_cases eq : q = p + · subst q + rw [hp] at hk + cases hk + rw [Function.update_self] + exact ⟨_, rfl, rfl⟩ + · rw [Function.update_of_ne eq, old q qoff, hk] + exact ⟨x, rfl, rfl⟩ + +end SpecDonation + +section Store +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + +theorem replyPrevPlaceA (P : EPlaced Z.T E R Bf) {r : U64} (hr : r ∈ R) : + Aligned (rPrevA r) ∧ (rPrevA r).val = r.val + 8 := by + have h := P.rep r hr + have v := rPrevA_val ⟨h.1, h.2.2⟩ + exact ⟨by unfold Aligned; rw [v]; omega, v⟩ + +theorem replyNextPlaceA (P : EPlaced Z.T E R Bf) {r : U64} (hr : r ∈ R) : + Aligned (rNextA r) ∧ (rNextA r).val = r.val + 16 := by + have h := P.rep r hr + have v := rNextA_val ⟨h.1, h.2.2⟩ + exact ⟨by unfold Aligned; rw [v]; omega, v⟩ + +theorem replyWordsApartA {s : S} {σ : State Unit} (H : SchedSRN st Z m κ s σ) + (P : IpcSchedPlacementA Z.th N E R Bf) {r q : U64} (hr : r ∈ R) (hq : q ∈ R) (hne : r ≠ q) : + r.val + 32 ≤ q.val ∨ q.val + 32 ≤ r.val := by + simpa [ObjKindA.size] using H.place.apart r q _ _ (P.reply r hr) (P.reply q hq) hne + +theorem replyLinkOffEp {s : S} {σ : State Unit} (H : SchedSRN st Z m κ s σ) + (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) {r b : U64} (hr : r ∈ R) + (hl : r.val < b.val) (hh : b.val < r.val + 32) : ¬ EFoot Z.T E R Bf b := by + rintro (⟨e, he, h⟩ | ⟨q, hq, h⟩ | ⟨t, ht, h | h⟩ | h) + · have := PE.er e he r hr + omega + · have hne : r ≠ q := fun eq => hl.ne' (eq ▸ h) + have := replyWordsApartA H P hr hq hne + omega + · have := PE.rt r hr t ht + obtain ⟨lo, off⟩ := h + unfold TOffN at off + omega + · have := PE.rt r hr t ht + unfold TL at h + omega + · have := PE.br b h r hr + omega + +theorem writeLinkFrames {s : S} {σ : State Unit} (H : SchedSRA st Z m κ s σ) + (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) {r b v : U64} (hr : r ∈ R) + (ha : Aligned b) (hl : r.val < b.val) (hh : b.val < r.val + 32) : + SchedSRA st Z m κ (L.wr s b v) σ ∧ FootEq Z.T E R Bf (L.mem s) (L.mem (L.wr s b v)) := by + have off := replyWordOffSchedA H.toSchedSRN P hr (Nat.le_of_lt hl) hh + have hw := writesIn_wr (st := st) (s := s) (v := v) ha + refine ⟨H.stable hw (fun a e => e ▸ off.1) (⟨SFixA.refl _, rfl, rfl, rfl, fun _ _ _ _ => rfl, + fun _ x hx => ⟨x, hx, rfl, rfl, rfl⟩, fun _ _ _ _ => rfl, fun _ x hx => ⟨x, hx⟩, + fun _ x hx => ⟨x, hx, rfl⟩⟩ : SStepA σ σ) ?_, ?_⟩ + · intro p x y hx hy + rw [hx] at hy + cases hy + exact Or.inl ⟨rfl, id⟩ + · intro b' ha' hb + exact hw.mem_eq ha' (fun e => replyLinkOffEp H.toSchedSRN P PE hr hl hh (e ▸ hb)) + +private theorem oldHeadWord {s : S} {σ : State Unit} (H : SchedSRA st Z m κ s σ) + {stack : U64 → List U64} (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {sc : U64} (hc : sc ∈ Z.C) : + L.mem s (scReplyA sc) = (stack sc).headD 0#u64 := by + obtain ⟨x, n, hk, hrel⟩ := H.scr.sc sc hc + obtain ⟨⟨y, k, hy, eq⟩, hm, _, _⟩ := HS.stackRel sc hc + rw [hk] at hy; cases hy + have he := hrel.reply + rw [← scReplyA_eqS, eq] at he + cases e : stack sc with + | nil => + have hnone : optRefS (L.mem s (scReplyA sc)) = none := by simpa [e] using he.symm + by_contra hn + rw [List.headD_nil] at hn + rw [optRefS_ne hn] at hnone + cases hnone + | cons r l => + have hne : L.mem s (scReplyA sc) ≠ 0#u64 := by + intro h + rw [h, optRefS_zero, e] at he + cases he + rw [optRefS_ne hne, e] at he + exact (specPtr_inj (Option.some.inj he)).symm + + + +theorem replyWriteKeepsVcpu {s : S} (PE : EPlaced Z.T E R Bf) + {q b v t : U64} (hq : q ∈ R) (lo : q.val ≤ b.val) (hi : b.val < q.val + 32) (ha : Aligned b) + (ht : t ∈ Z.T) : + L.mem (L.wr s b v) (core.num.U64.wrapping_add t 3664#u64) = + L.mem s (core.num.U64.wrapping_add t 3664#u64) := by + have hT := PE.tcb t ht + have hv : (core.num.U64.wrapping_add t 3664#u64).val = t.val + 3664 := + tfield_val hT (K := 3664#u64) (by decide) + have := PE.rt q hq t ht + exact L.mem_wr_ne ha (field_aligned (K := 3664#u64) hT (by decide) (by decide)) (by rw [hv]; omega) + +private theorem replyHeadBeforePush {s : S} {σ : State Unit} {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) {sc r : U64} (hc : sc ∈ Z.C) (hr : r ∈ R) : + let old := (stack sc).headD 0#u64 + (if (old != 0#u64) = true then obj.Reply.set_next st old s r false else ok s) ⦃ s' => + SchedSRA st Z m κ s' σ ∧ FootEq Z.T E R Bf (L.mem s) (L.mem s') ∧ + (∀ d ∈ R, L.mem s' (rPrevA d) = L.mem s (rPrevA d)) ∧ + (∀ d ∈ R, (stack sc).head? ≠ some d → L.mem s' (rNextA d) = L.mem s (rNextA d)) ∧ + (∀ d, (stack sc).head? = some d → L.mem s' (rNextA d) = r) ∧ + (∀ t ∈ Z.T, L.mem s' (core.num.U64.wrapping_add t 3664#u64) = + L.mem s (core.num.U64.wrapping_add t 3664#u64)) ∧ + WritesIn st s s' (fun b => ∃ q ∈ R, q.val ≤ b.val ∧ b.val < q.val + 32) ⦄ := by + intro old + obtain ⟨-, hm, -, -⟩ := HS.stackRel sc hc + have hrEven : r.val % 2 = 0 := by have := (PE.rep r hr).1; omega + cases eq : stack sc with + | nil => + have ho : old = 0#u64 := by simp [old, eq] + simp only [ho, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + exact ⟨H, Agree.rfl', fun _ _ => trivial, fun _ _ _ => trivial, (fun d e => by simp at e), fun _ _ => trivial, + WritesIn.refl _ _⟩ + | cons q tail => + have ho : old = q := by simp [old, eq] + have hq := hm q (by simp [eq]) + have hq0 : q ≠ 0#u64 := fun e => (PE.rep q hq).2.1 (by rw [e]; rfl) + rw [ho, if_pos (bne_iff_ne.mpr hq0)] + refine WP.spec_mono (obj.Reply.set_next.val0 s q r) ?_ + rintro s' rfl + have val0 : (r &&& ~~~1#u64) ||| 0#u64 = r := by + rw [mask1_of_even hrEven] + apply eq_of_bv + simp + rw [val0] + obtain ⟨ha, hv⟩ := replyNextPlaceA (Z := Z) PE hq + obtain ⟨H1, F1⟩ := writeLinkFrames (v := r) H P PE hq ha (by omega) (by omega) + refine ⟨H1, F1, ?_, ?_, ?_, fun t ht => replyWriteKeepsVcpu PE hq (by omega) (by omega) ha ht, + (writesIn_wr ha).mono fun b hb => ⟨q, hq, by rw [hb]; omega, by rw [hb]; omega⟩⟩ + · intro d hd + obtain ⟨hpa, hpv⟩ := replyPrevPlaceA (Z := Z) PE hd + apply L.mem_wr_ne ha hpa + by_cases e : q = d + · subst d; omega + · have := replyWordsApartA H.toSchedSRN P hq hd e; omega + · intro d hd hn + have hne : q ≠ d := by intro e; apply hn; simp [eq, e] + obtain ⟨hna, hnv⟩ := replyNextPlaceA (Z := Z) PE hd + have := replyWordsApartA H.toSchedSRN P hq hd hne + exact L.mem_wr_ne ha hna (by omega) + · intro d hd + have e : q = d := by simpa [eq] using hd + subst d + exact L.mem_wr_same ha + +/-- Concrete writes for pushing the reply onto the SC stack. -/ +def replyBindProgramA (s : S) (sc r : U64) : Result S := do + let old ← obj.Sc.reply st sc s + let s1 ← if (old != 0#u64) = true then obj.Reply.set_next st old s r false else ok s + let s2 ← obj.Reply.set_prev st r s1 old + let s3 ← obj.Sc.set_reply st sc s2 r + obj.Reply.set_next st r s3 sc true + +/-- `bindReplyLinksA` with its write set: reply objects' words and MCS words. -/ +theorem bindReplyLinksFpA {s : S} {σ : State Unit} {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {sc r : U64} (hc : sc ∈ Z.C) (hr : r ∈ R) (hi : ReplyIsolatedA (L.mem s) Z.C stack r) : + replyBindProgramA (st := st) s sc r ⦃ s' => + ∃ x n, σ.kheap (scNameA m sc) = some (.schedContext x n) ∧ + let σ' := bindStA σ (scNameA m sc) x n (specPtr r) + bindScReply (Z := Unit) (scNameA m sc) (specPtr r) σ = ({((), σ')}, False) ∧ + SchedSRA st Z m κ s' σ' ∧ EpStateRel Z.T E R Bf (L.mem s') σ' ∧ + ReplyStackRelA (L.mem s') R Z.C σ' (scNameA m) (Function.update stack sc (r :: stack sc)) ∧ + (∀ t ∈ Z.T, L.mem s' (core.num.U64.wrapping_add t 3664#u64) = + L.mem s (core.num.U64.wrapping_add t 3664#u64)) ∧ SSzA σ σ' ∧ + WritesIn st s s' (fun b => (∃ q ∈ R, q.val ≤ b.val ∧ b.val < q.val + 32) ∨ + MWrA (st := st) Z m σ s b) ⦄ := by + have hR0 : ∀ q ∈ R, q ≠ 0#u64 := fun q hq e => (PE.rep q hq).2.1 (by rw [e]; rfl) + obtain ⟨x, n, hk, hsc⟩ := H.scr.sc sc hc + obtain ⟨xr, hkr, _⟩ := HE.reply r hr + obtain ⟨⟨y, k, hy, hlist⟩, hm, _, _⟩ := HS.stackRel sc hc + rw [hk] at hy + cases hy + have hRep : ∀ d ∈ R, ∃ xd, σ.kheap (specPtr d) = some (.reply xd) := by + intro d hd + obtain ⟨xd, hkd, _⟩ := HE.reply d hd + exact ⟨xd, hkd⟩ + have hhead : ∀ q, x.scReplies.head? = some q → ∃ y, σ.kheap q = some (.reply y) := by + intro q hq + rw [hlist, List.head?_map] at hq + cases e : (stack sc).head? with + | none => simp [e] at hq + | some d => + have eq : specPtr d = q := by simpa [e] using hq + rw [← eq] + exact hRep d (hm d (List.mem_of_mem_head? e)) + let old := (stack sc).headD 0#u64 + have hEven : old.val % 2 = 0 := by + cases e : stack sc with + | nil => simp [old, e] + | cons q l => + have h := PE.rep q (hm q (by simp [e])) + change ((stack sc).headD 0#u64).val % 2 = 0 + rw [e, List.headD_cons] + omega + have hscEven : sc.val % 2 = 0 := by + have h := hsc.pl.align + have hb : 2 ∣ 2 ^ (n + minSchedContextBits) := + dvd_pow_self 2 (by unfold minSchedContextBits; omega) + exact Nat.mod_eq_zero_of_dvd (hb.trans (Nat.dvd_of_mod_eq_zero h)) + unfold replyBindProgramA + refine WP.spec_bind (obj.Sc.reply.val s sc) ?_ + rintro v rfl + rw [oldHeadWord H HS hc] + refine WP.spec_bind (replyHeadBeforePush H P PE HS hc hr) ?_ + rintro s1 ⟨H1, F1, hP1, hN1, hH1, hV1, wh⟩ + refine WP.spec_bind (obj.Reply.set_prev.val s1 r old) ?_ + rintro s2 rfl + rw [mask1_of_even hEven] + obtain ⟨pa, pv⟩ := replyPrevPlaceA (Z := Z) PE hr + obtain ⟨na, nv⟩ := replyNextPlaceA (Z := Z) PE hr + obtain ⟨H2, F2⟩ := writeLinkFrames (v := old) H1 P PE hr pa (by omega) (by omega) + refine WP.spec_bind (bind_sc_replyA H2 hc (hR0 r hr) hkr + (fun y k h hky hh => by rw [hk] at hky; cases hky; exact hhead h hh)) ?_ + rintro s3 ⟨y, k, hky, hB, H3, w3, z3⟩ + rw [hk] at hky + cases hky + refine WP.spec_mono (obj.Reply.set_next.val1 s3 r sc) ?_ + rintro s4 rfl + rw [mask1_of_even hscEven] + obtain ⟨H4, F4⟩ := writeLinkFrames (v := sc ||| 1#u64) H3 P PE hr na (by omega) (by omega) + have F3 : FootEq Z.T E R Bf (L.mem (L.wr s1 (rPrevA r) old)) (L.mem s3) := + (epFrameOfMcsA H2.toSchedSRN P + (σ' := σ) ⟨w3, SKeepA.refl _, SSzA.refl _⟩ rfl).1 + have ws (d : U64) (hd : d ∈ R) (b : U64) (ha : Aligned b) (hl : d.val ≤ b.val) (hh : b.val < d.val + 32) : + L.mem s3 b = L.mem (L.wr s1 (rPrevA r) old) b := + w3.mem_eq ha (replyWordOffSchedA H2.toSchedSRN P hd hl hh).2 + refine ⟨x, n, hk, hB, H4, HE.frame PE (F1.trans (F2.trans (F3.trans F4))) (bindStA_epView hk _), ?_, ?_, z3, ?_⟩ + · refine bindStA_pushStack HS hc hr hR0 hi (fun q hq hne hn => hne (H.scp.inj q hq sc hc hn)) + ?_ ?_ ?_ ?_ (L.mem_wr_same na) hk hkr hRep + · intro d hd hne + obtain ⟨da, dv⟩ := replyPrevPlaceA (Z := Z) PE hd + have sep := replyWordsApartA H.toSchedSRN P hd hr hne + rw [L.mem_wr_ne na da (by omega), ws d hd _ da (by omega) (by omega), L.mem_wr_ne pa da (by omega)] + exact hP1 d hd + · intro d hd hne hn + obtain ⟨da, dv⟩ := replyNextPlaceA (Z := Z) PE hd + have sep := replyWordsApartA H.toSchedSRN P hd hr hne + rw [L.mem_wr_ne na da (by omega), ws d hd _ da (by omega) (by omega), L.mem_wr_ne pa da (by omega)] + exact hN1 d hd hn + · intro d hd + have hdR := hm d (List.mem_of_mem_head? hd) + have hne : d ≠ r := fun e => hi.1 sc hc (e ▸ List.mem_of_mem_head? hd) + obtain ⟨da, dv⟩ := replyNextPlaceA (Z := Z) PE hdR + have sep := replyWordsApartA H.toSchedSRN P hdR hr hne + rw [L.mem_wr_ne na da (by omega), ws d hdR _ da (by omega) (by omega), L.mem_wr_ne pa da (by omega)] + exact hH1 d hd + · rw [L.mem_wr_ne na pa (by omega), ws r hr _ pa (by omega) (by omega), L.mem_wr_same pa] + · intro t ht + have ha := field_aligned (K := 3664#u64) (PE.tcb t ht) (by decide) (by decide) + rw [replyWriteKeepsVcpu PE hr (by omega) (by omega) na ht, + w3.mem_eq ha (vcpuWordOffMcsA H2.toSchedSRN ht), + replyWriteKeepsVcpu PE hr (by omega) (by omega) pa ht, hV1 t ht] + · have inR : ∀ (a : U64), a.val = r.val + 8 ∨ a.val = r.val + 16 → ∀ b : U64, b = a → + (∃ q ∈ R, q.val ≤ b.val ∧ b.val < q.val + 32) ∨ MWrA (st := st) Z m σ s b := fun a ha b hb => + Or.inl ⟨r, hr, by rw [hb]; omega, by rw [hb]; omega⟩ + have w1 : WritesIn st s (L.wr s1 (rPrevA r) old) + (fun b => (∃ q ∈ R, q.val ≤ b.val ∧ b.val < q.val + 32) ∨ MWrA (st := st) Z m σ s b) := + (WritesIn.trans (st := st) wh (writesIn_wr (v := old) pa)).mono fun b h => + h.elim Or.inl (inR _ (Or.inl pv) b) + have w2 : WritesIn st s s3 + (fun b => (∃ q ∈ R, q.val ≤ b.val ∧ b.val < q.val + 32) ∨ MWrA (st := st) Z m σ s b) := + (WritesIn.trans (st := st) w1 w3).mono fun b h => + h.elim id fun h => Or.inr (mwr_back rfl rfl (SSzA.refl σ) w1.1 h) + exact (WritesIn.trans (st := st) w2 (writesIn_wr na)).mono fun b h => h.elim id (inR _ (Or.inr nv) b) + +/-- The three IPC link writes and the SC's REPLY write implement `bindScReply`. -/ +theorem bindReplyLinksA {s : S} {σ : State Unit} {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {sc r : U64} (hc : sc ∈ Z.C) (hr : r ∈ R) (hi : ReplyIsolatedA (L.mem s) Z.C stack r) : + replyBindProgramA (st := st) s sc r ⦃ s' => + ∃ x n, σ.kheap (scNameA m sc) = some (.schedContext x n) ∧ + let σ' := bindStA σ (scNameA m sc) x n (specPtr r) + bindScReply (Z := Unit) (scNameA m sc) (specPtr r) σ = ({((), σ')}, False) ∧ + SchedSRA st Z m κ s' σ' ∧ EpStateRel Z.T E R Bf (L.mem s') σ' ∧ + ReplyStackRelA (L.mem s') R Z.C σ' (scNameA m) (Function.update stack sc (r :: stack sc)) ∧ + (∀ t ∈ Z.T, L.mem s' (core.num.U64.wrapping_add t 3664#u64) = + L.mem s (core.num.U64.wrapping_add t 3664#u64)) ⦄ := + WP.spec_mono (bindReplyLinksFpA H P PE HE HS hc hr hi) fun _ ⟨x, n, hk, hB, H', E', S', V, _, _⟩ => + ⟨x, n, hk, hB, H', E', S', V⟩ + + +/-- The donation suffix of `reply_push`, following the caller block, with its write set (reply objects' words and +MCS words). + +[S] The donor is on the current core and the destination already has the SC's +affinity. This makes migration a no-op. See `endpoint.rs:167–182`, +`ipc.rs:167–181`, `schedcontext.rs:65–78` and seL4 `schedcontext.c:320`. +-/ +theorem replyPushDonateTailFpA {s : S} {σ : State Unit} (a : A) {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {sc r f d : U64} + (hc : sc ∈ Z.C) (hsc0 : sc ≠ 0#u64) (hr : r ∈ R) (hi : ReplyIsolatedA (L.mem s) Z.C stack r) + (hf : f ∈ Z.T) (hd : d ∈ Z.T) (hfd : f ≠ d) (hfs : L.mem s (scA f) = sc) + (hd0 : L.mem s (scA d) = 0#u64) (hfon : L.mem s (affA f) = L.cpu s) + (hmig : L.mem s (affA d) = L.mem s (scCoreA sc)) (hV : L.mem s (core.num.U64.wrapping_add f 3664#u64) = 0#u64) + (hni : NotIdleScA m (scNameA m sc)) (hQ : NotQueued σ (specPtr f)) + (hpost : ∀ σ', ((bindScReply (Z := Unit) (scNameA m sc) (specPtr r)) >>= fun _ => + schedContextDonate (scNameA m sc) (specPtr d)) σ = ({((), σ')}, False) → RelIfBoundSc σ' (specPtr d)) : + (do let s1 ← replyBindProgramA (st := st) s sc r; schedcontext.donate st ar s1 a sc d) ⦃ out => + out.2 = a ∧ ∃ σ', + ((bindScReply (Z := Unit) (scNameA m sc) (specPtr r)) >>= fun _ => + schedContextDonate (scNameA m sc) (specPtr d)) σ = ({((), σ')}, False) ∧ + IpcSchedRelA st Z m κ out.1 σ' ∧ EpStateRel Z.T E R Bf (L.mem out.1) σ' ∧ + ReplyStackRelA (L.mem out.1) R Z.C σ' (scNameA m) (Function.update stack sc (r :: stack sc)) ∧ KView σ σ' ∧ + WritesIn st s out.1 (fun b => (∃ q ∈ R, q.val ≤ b.val ∧ b.val < q.val + 32) ∨ + MWrA (st := st) Z m σ s b) ⦄ := by + refine WP.spec_bind (bindReplyLinksFpA H P PE HE HS hc hr hi) ?_ + rintro s1 ⟨x, n, hk, hbind, H1, E1, S1, V1, zb, wb⟩ + obtain ⟨xf, hxf, ef⟩ := H.sch.tcb f hf + obtain ⟨xd, hxd, ed⟩ := H.sch.tcb d hd + have hf1 := bindStA_keepTcb (r := specPtr r) hk hxf + have hd1 := bindStA_keepTcb (r := specPtr r) hk hxd + obtain ⟨yf, hyf, ef1⟩ := H1.sch.tcb f hf + obtain ⟨yd, hyd, ed1⟩ := H1.sch.tcb d hd + rw [hf1] at hyf + cases hyf + rw [hd1] at hyd + cases hyd + have scWord : L.mem s1 (scA f) = sc := by + apply optScA_eq_of (m := m) + · rw [← ef1, ef, hfs] + · unfold optScA + rw [if_neg hsc0] + exact fun hn => hni.1 (Option.some.inj hn) + have hdWord : L.mem s1 (scA d) = 0#u64 := by + rw [hd0] at ed + by_contra hn + rw [ed] at ed1 + unfold optScA at ed1 + rw [if_neg hn] at ed1 + cases ed1 + have hon1 : L.mem s1 (affA f) = L.cpu s1 := by + rw [H.aff_eq H1 hf hf, H.cpu_eq H1, hfon] + have hmig1 : L.mem s1 (affA d) = L.mem s1 (scCoreA sc) := by + rw [H.aff_eq H1 hd hd, H1.scCore sc hc, ← H.scCore sc hc, hmig] + have hstall : smp.remote_tcb_stall st ar s1 a f = ok (s1, a) := + stall_keepA a (PE.tcb f hf) hon1 ((V1 f hf).trans hV) (WritesIn.refl (st := st) s1 (fun _ => False)) + (fun h => h) (fun h => h) + have hqueues : NotQueued (bindStA σ (scNameA m sc) x n (specPtr r)) (specPtr f) := by + obtain ⟨hrq, hrel⟩ := bindStKeepQueue (σ := σ) (p := scNameA m sc) (r := specPtr r) (x := x) (n := n) + exact ⟨by rw [hrq]; exact hQ.1, by rw [hrel]; exact hQ.2⟩ + have hst1 : StEqA Z.T (L.mem s1) (bindStA σ (scNameA m sc) x n (specPtr r)) := by + intro t ht xt hkt + exact (E1.at_tcb ht hkt).st + have hxfSc : xf.tcbSchedContext = some (scNameA m sc) := by + rw [ef, hfs, optScA, if_neg hsc0] + rfl + obtain ⟨xs, ns, hs, ha⟩ := H1.scr.sc sc hc + have scTcb : xs.scTcb = some (specPtr f) := by + obtain ⟨y, k, hy, hb⟩ := H1.inv.back (specPtr f) xf _ hf1 hxfSc + rw [hs] at hy + cases hy + exact hb + have dom : xf.tcbDomain = 0 := by + obtain ⟨y, hy, hdom, _⟩ := H1.rest.prio f hf + rw [hf1] at hy + cases hy + exact hdom + refine WP.spec_mono (sc_donate_fromA H1 hst1 a hc hsc0 hf scWord hd hdWord hfd hon1 + hqueues.1 hqueues.2 hstall hmig1 hni) ?_ + rintro out ⟨hArch, σ', Q', hD, HB, F⟩ + have specAll : + ((bindScReply (Z := Unit) (scNameA m sc) (specPtr r)) >>= fun _ => + schedContextDonate (scNameA m sc) (specPtr d)) σ = ({((), σ')}, False) := by + rw [nd_bind_oneSc hbind, hD] + have hRes : ((), σ') ∈ (schedContextDonate (Z := Unit) (scNameA m sc) (specPtr d) + (bindStA σ (scNameA m sc) x n (specPtr r))).1 := by rw [hD]; exact Set.mem_singleton _ + obtain ⟨FW, KW⟩ := epFrameOfMcsA H1.toSchedSRN P F + (schedContextDonateKeepsUserMemory _ _ _ _ hRes) + have wd : WritesIn st s out.1 (fun b => (∃ q ∈ R, q.val ≤ b.val ∧ b.val < q.val + 32) ∨ + MWrA (st := st) Z m σ s b) := + (WritesIn.trans (st := st) wb F.wr).mono fun b h => h.elim id fun h => Or.inr (mwr_back rfl rfl zb wb.1 h) + refine ⟨hArch, σ', specAll, ⟨Q', Z.R, HB.toA (hpost σ' specAll)⟩, E1.frame PE FW KW, ?_, + (bindStA_epView hk (specPtr r)).trans KW, wd⟩ + refine S1.frame ?_ (fun q _ y k hq => ?_) ?_ + · intro q hq + have hn := replyNextPlaceA (Z := Z) PE hq + have hp := replyPrevPlaceA (Z := Z) PE hq + exact ⟨F.wr.mem_eq hn.1 (replyWordOffSchedA H1.toSchedSRN P hq (by omega) (by omega)).2, + F.wr.mem_eq hp.1 (replyWordOffSchedA H1.toSchedSRN P hq (by omega) (by omega)).2⟩ + · obtain ⟨y', hq', ey⟩ := donationKeepsReplyLists hs scTcb hf1 hd1 (fun eq => hfd (specPtr_inj eq)) + dom hqueues hRes hq + exact ⟨y', k, hq', ey⟩ + · intro q hq y hky + obtain ⟨xq, hkq, _⟩ := E1.reply q hq + have hkq' := F.keep.obj (specPtr q) (.reply xq) hkq (by intro _ _ e; cases e) + (by intro _ e; cases e) + rw [hkq'] at hky + cases hky + exact ⟨_, hkq, rfl⟩ + + +/-- The donation suffix of `reply_push`, following the caller block (`replyPushDonateTailFpA` without the write +set). -/ +theorem replyPushDonateTailViewA {s : S} {σ : State Unit} (a : A) {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {sc r f d : U64} + (hc : sc ∈ Z.C) (hsc0 : sc ≠ 0#u64) (hr : r ∈ R) (hi : ReplyIsolatedA (L.mem s) Z.C stack r) + (hf : f ∈ Z.T) (hd : d ∈ Z.T) (hfd : f ≠ d) (hfs : L.mem s (scA f) = sc) + (hd0 : L.mem s (scA d) = 0#u64) (hfon : L.mem s (affA f) = L.cpu s) + (hmig : L.mem s (affA d) = L.mem s (scCoreA sc)) (hV : L.mem s (core.num.U64.wrapping_add f 3664#u64) = 0#u64) + (hni : NotIdleScA m (scNameA m sc)) (hQ : NotQueued σ (specPtr f)) + (hpost : ∀ σ', ((bindScReply (Z := Unit) (scNameA m sc) (specPtr r)) >>= fun _ => + schedContextDonate (scNameA m sc) (specPtr d)) σ = ({((), σ')}, False) → RelIfBoundSc σ' (specPtr d)) : + (do let s1 ← replyBindProgramA (st := st) s sc r; schedcontext.donate st ar s1 a sc d) ⦃ out => + out.2 = a ∧ ∃ σ', + ((bindScReply (Z := Unit) (scNameA m sc) (specPtr r)) >>= fun _ => + schedContextDonate (scNameA m sc) (specPtr d)) σ = ({((), σ')}, False) ∧ + IpcSchedRelA st Z m κ out.1 σ' ∧ EpStateRel Z.T E R Bf (L.mem out.1) σ' ∧ + ReplyStackRelA (L.mem out.1) R Z.C σ' (scNameA m) (Function.update stack sc (r :: stack sc)) ∧ KView σ σ' ⦄ := + WP.spec_mono (replyPushDonateTailFpA a H P PE HE HS hc hsc0 hr hi hf hd hfd hfs hd0 hfon hmig hV hni hQ hpost) + fun _ ⟨ha, σ', h, H', E', S', V, _⟩ => ⟨ha, σ', h, H', E', S', V⟩ + +/-- Finish a migration-free donating reply push after the caller block. -/ +theorem replyPushDonateTailA {s : S} {σ : State Unit} (a : A) {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) {sc r f d : U64} + (hc : sc ∈ Z.C) (hsc0 : sc ≠ 0#u64) (hr : r ∈ R) (hi : ReplyIsolatedA (L.mem s) Z.C stack r) + (hf : f ∈ Z.T) (hd : d ∈ Z.T) (hfd : f ≠ d) (hfs : L.mem s (scA f) = sc) + (hd0 : L.mem s (scA d) = 0#u64) (hfon : L.mem s (affA f) = L.cpu s) + (hmig : L.mem s (affA d) = L.mem s (scCoreA sc)) + (hV : L.mem s (core.num.U64.wrapping_add f 3664#u64) = 0#u64) + (hni : NotIdleScA m (scNameA m sc)) (hQ : NotQueued σ (specPtr f)) + (hpost : ∀ σ', ((bindScReply (Z := Unit) (scNameA m sc) (specPtr r)) >>= fun _ => + schedContextDonate (scNameA m sc) (specPtr d)) σ = ({((), σ')}, False) → RelIfBoundSc σ' (specPtr d)) : + (do let s1 ← replyBindProgramA (st := st) s sc r; schedcontext.donate st ar s1 a sc d) ⦃ out => + out.2 = a ∧ ∃ σ', + ((bindScReply (Z := Unit) (scNameA m sc) (specPtr r)) >>= fun _ => + schedContextDonate (scNameA m sc) (specPtr d)) σ = ({((), σ')}, False) ∧ + IpcSchedRelA st Z m κ out.1 σ' ∧ EpStateRel Z.T E R Bf (L.mem out.1) σ' ∧ + ReplyStackRelA (L.mem out.1) R Z.C σ' (scNameA m) (Function.update stack sc (r :: stack sc)) ⦄ := + WP.spec_mono (replyPushDonateTailViewA a H P PE HE HS hc hsc0 hr hi hf hd hfd hfs hd0 hfon hmig hV hni hQ hpost) + fun _ ⟨ha, σ', h, H', E', S', _⟩ => ⟨ha, σ', h, H', E', S'⟩ + +end Store + +end + +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/IpcScPushBlockA.lean b/proofs/Fl4ProofsA64/P3/IpcScPushBlockA.lean new file mode 100644 index 000000000..290b6ccfe --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/IpcScPushBlockA.lean @@ -0,0 +1,323 @@ +/- +P3 (AArch64): the concrete reply link and caller block, their write footprint, +and the heap projections and stack frame at the blocked state. +-/ +import Fl4ProofsA64.P3.IpcScPushA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4SpecA64 Sched + +noncomputable section + +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {Z Z' : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + +set_option maxHeartbeats 300000 in +private theorem scheduleTcbActionWrite {s : S} (f : U64) + (ha : Aligned (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION)) : + sched.schedule_tcb st s f ⦃ s' => WritesIn st s s' (· = L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) ⦄ := by + have noop : (ok s : Result S) ⦃ s' => WritesIn st s s' (· = L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) ⦄ := + (WP.spec_ok _).2 (WritesIn.refl _ _) + unfold sched.schedule_tcb + refine WP.spec_bind (sched.cur_thread.spec s) ?_ + intro t _ + refine WP.spec_bind (tcb_eq.blk f t) ?_ + intro b _ + by_cases hf : b = true + · rw [if_pos hf] + refine WP.spec_bind (sched.action.spec s) ?_ + intro a _ + by_cases hres : a = sched.ACTION_RESUME_CURRENT + · rw [if_pos hres] + refine WP.spec_bind (sched.is_schedulable.spec s f) ?_ + intro can _ + by_cases hc : can = true + · rw [if_pos hc]; exact noop + · rw [if_neg hc] + exact WP.spec_mono (sched.set_action.spec s _) fun s' eq => eq.symm ▸ writesIn_wr ha + · rw [if_neg hres]; exact noop + · rw [if_neg hf]; exact noop + +set_option maxHeartbeats 300000 in +def pushPrefixProgramA (s : S) (f r : U64) : Result S := do + let s' ← obj.Reply.set_tcb st r s f + sched.set_blocked_on_reply st s' f r + +set_option maxHeartbeats 300000 in +theorem pushPrefixWrites {s : S} {σ : State Unit} (H : SchedSRA st Z m κ s σ) + (PE : EPlaced Z.T E R Bf) {f r : U64} (hf : f ∈ Z.T) (hr : r ∈ R) : + pushPrefixProgramA (st := st) s f r ⦃ out => WritesIn st s out (fun b => + b = rTcbA r ∨ b = stA f ∨ b = tReplyA f ∨ + b = L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) ⦄ := by + obtain ⟨n, hn, hnc, hcs⟩ := H.csN + have hcl : (L.cpu s).val < 128 := by have := (hcs _).1 H.sch.cpuCs; omega + have hact : Aligned (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) := by + have h := H.toSchedSRN.ksb_al + unfold Aligned + rw [nodeAt_valSc H.ksFit hcl (by rw [act_val]; decide), act_val] + omega + have hta := PE.tcb f hf + let linked := L.wr s (Fl4ProofsA64.P1.rTcbA r) f + let stated := L.wr linked (stA f) sched.TS_BLOCKED_ON_REPLY + let paired := L.wr stated (Fl4ProofsA64.P1.replyA f) r + have eAct : L.nodeAt paired (L.cpu paired) layout.node.SCHED_ACTION = + L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION := by + simp only [paired, stated, linked, LawfulStore.cpu_wr, nodeAt_wr] + unfold pushPrefixProgramA + refine WP.spec_bind (Fl4ProofsA64.P1.obj.Reply.set_tcb.spec s r f) ?_ + rintro s1 rfl + change sched.set_blocked_on_reply st linked f r ⦃ _ ⦄ + unfold sched.set_blocked_on_reply + refine WP.spec_bind (obj.Tcb.set_state.spec (L := L) linked f _) ?_ + rintro s2 rfl + refine WP.spec_bind (obj.Tcb.set_reply_obj.wr (L := L) stated f r) ?_ + rintro s3 rfl + change sched.schedule_tcb st paired f ⦃ _ ⦄ + refine WP.spec_mono (scheduleTcbActionWrite (s := paired) f (eAct.symm ▸ hact)) ?_ + intro out h + have h0 : WritesIn st s linked (· = rTcbA r) := + writesIn_wr (st := st) (s := s) (v := f) (a_rtcb PE hr).1 + have h1 : WritesIn st linked stated (· = stA f) := + writesIn_wr (st := st) (s := linked) (v := sched.TS_BLOCKED_ON_REPLY) (a_st hta).1 + have h2 : WritesIn st stated paired (· = tReplyA f) := + writesIn_wr (st := st) (s := stated) (v := r) (a_reply hta).1 + rw [eAct] at h + exact h0.trans (h1.trans (h2.trans h)) + +set_option maxHeartbeats 300000 in +theorem pushPrefixVcpuAndLinks {s s' : S} {σ : State Unit} (H : SchedSRA st Z m κ s σ) + (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + {f r : U64} (hf : f ∈ Z.T) (hr : r ∈ R) + (hw : WritesIn st s s' (fun b => b = rTcbA r ∨ b = stA f ∨ b = tReplyA f ∨ + b = L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION)) : + (L.mem s' (core.num.U64.wrapping_add f 3664#u64) = L.mem s (core.num.U64.wrapping_add f 3664#u64)) ∧ + (∀ q ∈ R, L.mem s' (rNextA q) = L.mem s (rNextA q) ∧ L.mem s' (rPrevA q) = L.mem s (rPrevA q)) := by + have hn := H.toSchedSRN.node_in H.sch.cpuCs (o := layout.node.SCHED_ACTION) (by rw [act_val]; decide) + have hta := PE.tcb f hf + have hst := val_st hta + have hfr := val_reply hta + have hrt := rTcbA_val (r := r) + refine ⟨hw.mem_eq (field_aligned (K := 3664#u64) hta (by decide) (by decide)) ?_, ?_⟩ + · have hv : (core.num.U64.wrapping_add f 3664#u64).val = f.val + 3664 := tfield_val hta (by decide) + have hd := PE.rt r hr f hf + have hks := H.toSchedSRN.tcb_ks hf + rintro (h | h | h | h) <;> have e := congrArg UScalar.val h <;> omega + · intro q hq + have hqks := H.place.ks q (.other 32) (P.reply q hq) (by + rintro ⟨i, hi, e⟩ + have h := H.place.irq i hi + rw [← e, P.reply q hq] at h + cases h) + have far : ∀ b : U64, q.val < b.val → b.val < q.val + 32 → Aligned b → L.mem s' b = L.mem s b := by + intro b lo hi ha + have hd := PE.rt q hq f hf + apply hw.mem_eq ha + rintro (h | h | h | h) + · have he := congrArg UScalar.val h + by_cases e : q = r + · subst q; omega + · have := replyWordsApartA H.toSchedSRN P hq hr e + omega + · have he := congrArg UScalar.val h; omega + · have he := congrArg UScalar.val h; omega + · have he := congrArg UScalar.val h + change q.val + 32 ≤ (L.ksb s).val ∨ (L.ksb s).val + ksSizeA ≤ q.val at hqks + omega + have v1 := replyNextPlaceA (Z := Z) PE hq + have v2 := replyPrevPlaceA (Z := Z) PE hq + exact ⟨far _ (by omega) (by omega) v1.1, far _ (by omega) (by omega) v2.1⟩ + +set_option maxHeartbeats 300000 in +def pushBlockedStateA (σ : State Unit) (f r : ObjRef) (xf : Tcb) (xr : Reply) : State Unit := + stsAct (updK (updK σ r (.reply { xr with replyTcb := some f })) f + (.tcb { xf with tcbState := .blockedOnReply r })) f + +set_option maxHeartbeats 300000 in +theorem pushPrefixRelA (O : SchedOpsEA st Z.T E R Bf (IpcSchedRelA st Z m κ)) + {s : S} {σ : State Unit} (H : IpcSchedRelA st Z m κ s σ) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) {f r : U64} (hf : f ∈ Z.T) (hr : r ∈ R) + {xf : Tcb} {xr : Reply} (hkf : σ.kheap (specPtr f) = some (.tcb xf)) (hkr : σ.kheap (specPtr r) = some (.reply xr)) + (hEp : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr f ∉ z.epQueue) + (hQ : NotQueued σ (specPtr f)) (hRB : RelIfBound σ (specPtr f)) + (hAct : ∀ u, σ.schedulerAction = .switchThread u → u ≠ specPtr f) (hIdle : specPtr f ≠ σ.idleThread) : + pushPrefixProgramA (st := st) s f r ⦃ out => + EpStateRel Z.T E R Bf (L.mem out) (pushBlockedStateA σ (specPtr f) (specPtr r) xf xr) ∧ + IpcSchedRelA st Z m κ out (pushBlockedStateA σ (specPtr f) (specPtr r) xf xr) ⦄ := by + let σ1 := updK σ (specPtr r) (.reply { xr with replyTcb := some (specPtr f) }) + have hne : specPtr f ≠ specPtr r := by intro e; rw [e, hkr] at hkf; cases hkf + have hf1 : σ1.kheap (specPtr f) = some (.tcb xf) := by simp [σ1, hne, hkf] + have hEp1 : ∀ e ∈ E, ∀ z, σ1.kheap (specPtr e) = some (.endpoint z) → specPtr f ∉ z.epQueue := by + intro e he z hz + by_cases h : specPtr e = specPtr r + · simp [σ1, h] at hz + · exact hEp e he z (by simpa [σ1, h] using hz) + have hRB1 : RelIfBound σ1 (specPtr f) := + RelIfBound.keep hRB rfl rfl (fun x' hx => by rw [hf1] at hx; cases hx; exact ⟨_, hkf, rfl⟩) + (fun p y n hy => by + by_cases e : p = specPtr r + · simp [σ1, e] at hy + · simpa [σ1, e] using hy) + unfold pushPrefixProgramA + refine WP.spec_bind (linkReplyA O.to0 PE HE H hf hr hkr) ?_ + rintro s1 ⟨E1, H1⟩ + exact blockOnReplyA O PE E1 H1 hf r hf1 hEp1 hQ hRB1 hAct hIdle + +set_option maxHeartbeats 300000 in +private theorem pushBlockedOtherA {σ : State Unit} {f r q : ObjRef} {xf : Tcb} {xr : Reply} + (hqr : q ≠ r) (hqf : q ≠ f) : + (pushBlockedStateA σ f r xf xr).kheap q = σ.kheap q := by + unfold pushBlockedStateA + rw [stsAct_kheap, updK_kheap, Function.update_of_ne hqf, updK_kheap, Function.update_of_ne hqr] + +set_option maxHeartbeats 300000 in +private theorem pushBlockedReplyA {σ : State Unit} {f r : ObjRef} {xf : Tcb} {xr : Reply} + (rf : r ≠ f) : + (pushBlockedStateA σ f r xf xr).kheap r = some (.reply { xr with replyTcb := some f }) := by + unfold pushBlockedStateA + rw [stsAct_kheap, updK_kheap, Function.update_of_ne rf, updK_kheap, Function.update_self] + +private theorem pushBlockedTcbA (σ : State Unit) (f r : ObjRef) (xf : Tcb) (xr : Reply) : + (pushBlockedStateA σ f r xf xr).kheap f = some (.tcb { xf with tcbState := .blockedOnReply r }) := by + unfold pushBlockedStateA + rw [stsAct_kheap, updK_kheap, Function.update_self] + +private theorem pushBlockedNotQueuedA {σ : State Unit} {f r : ObjRef} {xf : Tcb} {xr : Reply} + (hQ : NotQueued σ f) : NotQueued (pushBlockedStateA σ f r xf xr) f := by + unfold pushBlockedStateA + exact ⟨by rw [stsAct_readyQueues]; exact hQ.1, by rw [stsAct_releaseQueue]; exact hQ.2⟩ + +attribute [local irreducible] pushBlockedStateA stsAct updK + +set_option maxHeartbeats 300000 in +private theorem pushBlockedStackScA {σ post : State Unit} {f r : ObjRef} {xf : Tcb} {xr : Reply} + (hkf : σ.kheap f = some (.tcb xf)) (hkr : σ.kheap r = some (.reply xr)) + (keep : ∀ q, q ≠ r → q ≠ f → post.kheap q = σ.kheap q) + (p : ObjRef) (x : SchedContext) (n : Nat) (h : σ.kheap p = some (.schedContext x n)) : + ∃ y k, post.kheap p = some (.schedContext y k) ∧ y.scReplies = x.scReplies := by + have pr : p ≠ r := by + intro e + have bad := h.symm.trans ((congrArg σ.kheap e).trans hkr) + exact nomatch bad + have pf : p ≠ f := by + intro e + have bad := h.symm.trans ((congrArg σ.kheap e).trans hkf) + exact nomatch bad + exact ⟨x, n, (keep p pr pf).trans h, rfl⟩ + +set_option maxHeartbeats 300000 in +private theorem pushBlockedStackReplyA {σ post : State Unit} {f r : ObjRef} {xf : Tcb} {xr : Reply} + (hkf : σ.kheap f = some (.tcb xf)) (hkr : σ.kheap r = some (.reply xr)) + (keep : ∀ q, q ≠ r → q ≠ f → post.kheap q = σ.kheap q) + (linked : post.kheap r = some (.reply { xr with replyTcb := some f })) + (q : ObjRef) (y : Reply) (hy : σ.kheap q = some (.reply y)) + (x : Reply) (hx : post.kheap q = some (.reply x)) : + ∃ z, σ.kheap q = some (.reply z) ∧ x.replySc = z.replySc := by + by_cases qr : q = r + · clear hkf keep hy + have eq := hx.symm.trans ((congrArg post.kheap qr).trans linked) + have eqReply : x = { xr with replyTcb := some f } := + KernelObject.reply.inj (Option.some.inj eq) + have scEq := congrArg Reply.replySc eqReply + exact ⟨xr, (congrArg σ.kheap qr).trans hkr, scEq⟩ + · clear linked hkr + have qf : q ≠ f := by + intro e + have bad := hy.symm.trans ((congrArg σ.kheap e).trans hkf) + exact nomatch bad + have eq := hx.symm.trans ((keep q qr qf).trans hy) + exact ⟨y, hy, congrArg Reply.replySc (KernelObject.reply.inj (Option.some.inj eq))⟩ + +set_option maxHeartbeats 300000 in +private theorem pushBlockedStackA {σ post : State Unit} {C R : Set U64} {m : McState} {stack : U64 → List U64} + {mem mem' : U64 → U64} (HS : ReplyStackRelA mem R C σ (scNameA m) stack) + (links : ∀ q ∈ R, mem' (rNextA q) = mem (rNextA q) ∧ mem' (rPrevA q) = mem (rPrevA q)) + (hSc : ∀ c ∈ C, ∀ x n, σ.kheap (scNameA m c) = some (.schedContext x n) → + ∃ y k, post.kheap (scNameA m c) = some (.schedContext y k) ∧ y.scReplies = x.scReplies) + (hRep : ∀ q ∈ R, ∀ x, post.kheap (specPtr q) = some (.reply x) → + ∃ y, σ.kheap (specPtr q) = some (.reply y) ∧ x.replySc = y.replySc) : + ReplyStackRelA mem' R C post (scNameA m) stack := + ReplyStackRelA.frame HS links hSc hRep + +set_option maxHeartbeats 20000 in +private theorem pushHeapReplyNeTcbA {σ : State Unit} {f r : ObjRef} {xf : Tcb} {xr : Reply} + (hkf : σ.kheap f = some (.tcb xf)) (hkr : σ.kheap r = some (.reply xr)) : r ≠ f := by + intro eq + have bad : some (KernelObject.reply xr) = some (KernelObject.tcb xf) := + hkr.symm.trans ((congrArg σ.kheap eq).trans hkf) + exact nomatch bad + +set_option maxHeartbeats 20000 in +private theorem pushBlockedCalleeA {σ : State Unit} {f d r : ObjRef} {xf xd : Tcb} {xr : Reply} + (hkd : σ.kheap d = some (.tcb xd)) (hkr : σ.kheap r = some (.reply xr)) (hdf : d ≠ f) : + (pushBlockedStateA σ f r xf xr).kheap d = some (.tcb xd) := + (pushBlockedOtherA (σ := σ) (xf := xf) (xr := xr) (Ne.symm (pushHeapReplyNeTcbA hkd hkr)) hdf).trans hkd + +set_option maxHeartbeats 20000 in +theorem pushBlockedHeapFactsA {σ : State Unit} {f d r : U64} {xf xd : Tcb} {xr : Reply} + (hkf : σ.kheap (specPtr f) = some (.tcb xf)) + (hkd : σ.kheap (specPtr d) = some (.tcb xd)) + (hkr : σ.kheap (specPtr r) = some (.reply xr)) + (hfd : f ≠ d) (hQ : NotQueued σ (specPtr f)) : + let post := pushBlockedStateA σ (specPtr f) (specPtr r) xf xr + (∀ q : ObjRef, q ≠ specPtr r → q ≠ specPtr f → post.kheap q = σ.kheap q) ∧ + post.kheap (specPtr r) = some (.reply { xr with replyTcb := some (specPtr f) }) ∧ + post.kheap (specPtr f) = some (.tcb { xf with tcbState := .blockedOnReply (specPtr r) }) ∧ + post.kheap (specPtr d) = some (.tcb xd) ∧ NotQueued post (specPtr f) := + ⟨fun q qr qf => pushBlockedOtherA (σ := σ) (f := specPtr f) (r := specPtr r) (q := q) (xf := xf) (xr := xr) qr qf, + pushBlockedReplyA (σ := σ) (xf := xf) (xr := xr) (pushHeapReplyNeTcbA hkf hkr), + pushBlockedTcbA σ (specPtr f) (specPtr r) xf xr, + pushBlockedCalleeA (xf := xf) hkd hkr (fun e => hfd (specPtr_inj e.symm)), + pushBlockedNotQueuedA (r := specPtr r) (xf := xf) (xr := xr) hQ⟩ + +set_option maxHeartbeats 300000 in +theorem pushAfterBlockScWordsA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} + {post : State Unit} (H : SchedSRA st Z m κ s post) + {f d sc : U64} (hf : f ∈ Z.T) (hd : d ∈ Z.T) (h0 : sc ≠ 0#u64) + (hni : NotIdleScA m (scNameA m sc)) {xf xd : Tcb} + (hkf : post.kheap (specPtr f) = some (.tcb xf)) (hkd : post.kheap (specPtr d) = some (.tcb xd)) + (hSCf : xf.tcbSchedContext = some (scNameA m sc)) (hSCd : xd.tcbSchedContext = none) : + L.mem s (scA f) = sc ∧ L.mem s (scA d) = 0#u64 := by + obtain ⟨yf, hyf, ef⟩ := H.sch.tcb f hf + obtain ⟨yd, hyd, ed⟩ := H.sch.tcb d hd + have eqf := congrArg Tcb.tcbSchedContext (KernelObject.tcb.inj (Option.some.inj (hyf.symm.trans hkf))) + have eqd := congrArg Tcb.tcbSchedContext (KernelObject.tcb.inj (Option.some.inj (hyd.symm.trans hkd))) + clear H hkf hkd hyf hyd + have hsc : optScA m sc = some (scNameA m sc) := by rw [optScA, if_neg h0]; rfl + refine ⟨optScA_eq_of (ef.symm.trans (eqf.trans (hSCf.trans hsc.symm))) ?_, ?_⟩ + · exact fun e => hni.1 (Option.some.inj (hsc.symm.trans e)) + · exact optScA_eq_of (ed.symm.trans (eqd.trans hSCd)) (by + change (none : Option ObjRef) ≠ some idleScPtr + exact fun e => nomatch e) + +set_option maxHeartbeats 300000 in +theorem pushFrameAfterBlockA {σ post : State Unit} {T C : Set U64} {stack : U64 → List U64} + {mem mem' : U64 → U64} (HS : ReplyStackRelA mem R C σ (scNameA m) stack) + (HE : EpStateRel T E R Bf mem σ) {f r : ObjRef} {xf : Tcb} {xr : Reply} + (hkf : σ.kheap f = some (.tcb xf)) (hkr : σ.kheap r = some (.reply xr)) + (keep : ∀ q, q ≠ r → q ≠ f → post.kheap q = σ.kheap q) + (linked : post.kheap r = some (.reply { xr with replyTcb := some f })) + (links : ∀ q ∈ R, mem' (rNextA q) = mem (rNextA q) ∧ mem' (rPrevA q) = mem (rPrevA q)) : + ReplyStackRelA mem' R C post (scNameA m) stack := + pushBlockedStackA HS links (fun c _ => pushBlockedStackScA hkf hkr keep (scNameA m c)) + (fun q hq x hx => by + obtain ⟨y, hy, _⟩ := HE.reply q hq + exact pushBlockedStackReplyA hkf hkr keep linked (specPtr q) y hy x hx) + +set_option maxHeartbeats 300000 in +/-- The caller's SC word after the block, for a caller whose TCB names the SC (the callee not mentioned). -/ +theorem pushAfterBlockScWordFA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} + {post : State Unit} (H : SchedSRA st Z m κ s post) + {f sc : U64} (hf : f ∈ Z.T) (h0 : sc ≠ 0#u64) + (hni : NotIdleScA m (scNameA m sc)) {xf : Tcb} + (hkf : post.kheap (specPtr f) = some (.tcb xf)) (hSCf : xf.tcbSchedContext = some (scNameA m sc)) : + L.mem s (scA f) = sc := by + obtain ⟨yf, hyf, ef⟩ := H.sch.tcb f hf + have eqf := congrArg Tcb.tcbSchedContext (KernelObject.tcb.inj (Option.some.inj (hyf.symm.trans hkf))) + clear H hkf hyf + have hsc : optScA m sc = some (scNameA m sc) := by rw [optScA, if_neg h0]; rfl + exact optScA_eq_of (ef.symm.trans (eqf.trans (hSCf.trans hsc.symm))) fun e => hni.1 (Option.some.inj (hsc.symm.trans e)) + +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/IpcScPushTopA.lean b/proofs/Fl4ProofsA64/P3/IpcScPushTopA.lean new file mode 100644 index 000000000..71c117298 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/IpcScPushTopA.lean @@ -0,0 +1,392 @@ +/- +P3 (AArch64): full donating reply_push. +[S] The donor is local and the receiver already has the donated SC's affinity. +The joint caller supplies the released-SC obligation after the donation. +-/ +import Fl4ProofsA64.P3.IpcScPushBlockA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4SpecA64 Sched + +noncomputable section + +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {Z Z' : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + +set_option maxHeartbeats 300000 in +private theorem pushSpecPrefixA {σ : State Unit} {p f d r : ObjRef} {xf xd : Tcb} {xr : Reply} + (hkf : σ.kheap f = some (.tcb xf)) (hkd : σ.kheap d = some (.tcb xd)) + (hkr : σ.kheap r = some (.reply xr)) + (hSCf : xf.tcbSchedContext = some p) (hSCd : xd.tcbSchedContext = none) : + replyPush (Z := Unit) f d r true σ = + (bindScReply (Z := Unit) p r >>= fun _ => schedContextDonate p d) + (pushBlockedStateA σ f r xf xr) := by + have hn : f ≠ r := fun e => by rw [e, hkr] at hkf; cases hkf + have hf1 : (updK σ r (.reply { xr with replyTcb := some f })).kheap f = some (.tcb xf) := by + rw [updK_kheap, Function.update_of_ne hn] + exact hkf + let σ2 := pushBlockedStateA σ f r xf xr + have stepBlock : setThreadState (Z := Unit) f (.blockedOnReply r) + (updK σ r (.reply { xr with replyTcb := some f })) = ({((), σ2)}, False) := + setThreadState_eq _ hf1 + change replyPush (Z := Unit) f d r true σ = + (bindScReply (Z := Unit) p r >>= fun _ => schedContextDonate p d) σ2 + unfold replyPush + rw [nd_bind_one (getTcbObjRef_eq _ hkf), nd_bind_one (getTcbObjRef_eq _ hkd), + nd_bind_one (setReplyTcb_eq _ hkr), nd_bind_one stepBlock, hSCf, hSCd] + have guard : (some p : Option ObjRef) ≠ none ∧ (none : Option ObjRef) = none ∧ (true : Bool) := + ⟨Option.some_ne_none p, rfl, rfl⟩ + rw [Fl4Spec.NonDet.when, if_pos guard, Sched.the_someA] + +set_option maxHeartbeats 300000 in +/-- The spec reply push with donation allowed to a callee that already has an SC: nothing is donated. -/ +private theorem pushSpecNoDonA {σ : State Unit} {f d r : ObjRef} {xf xd : Tcb} {xr : Reply} + (hkf : σ.kheap f = some (.tcb xf)) (hkd : σ.kheap d = some (.tcb xd)) + (hkr : σ.kheap r = some (.reply xr)) (hSCd : xd.tcbSchedContext ≠ none) : + replyPush (Z := Unit) f d r true σ = ({((), pushBlockedStateA σ f r xf xr)}, False) := by + have hn : f ≠ r := fun e => by rw [e, hkr] at hkf; cases hkf + have hf1 : (updK σ r (.reply { xr with replyTcb := some f })).kheap f = some (.tcb xf) := by + rw [updK_kheap, Function.update_of_ne hn] + exact hkf + have stepBlock : setThreadState (Z := Unit) f (.blockedOnReply r) + (updK σ r (.reply { xr with replyTcb := some f })) = ({((), pushBlockedStateA σ f r xf xr)}, False) := + setThreadState_eq _ hf1 + unfold replyPush + rw [nd_bind_one (getTcbObjRef_eq _ hkf), nd_bind_one (getTcbObjRef_eq _ hkd), + nd_bind_one (setReplyTcb_eq _ hkr), nd_bind_one stepBlock] + rw [Fl4Spec.NonDet.when, if_neg] + · first | rfl | simp [Fl4Spec.NonDet.pure] + · rintro ⟨-, h, -⟩ + exact hSCd h + +private def pushDonationBranchA (s : S) (a : A) (sc r d : U64) : Result (S × A) := do + let b ← obj.Sc.is_null sc + if b then ok (s, a) + else + let ds ← obj.Tcb.sc st d s + if ds = 0#u64 then + let s' ← replyBindProgramA (st := st) s sc r + schedcontext.donate st ar s' a sc d + else ok (s, a) + +set_option maxHeartbeats 300000 in +private theorem pushDonationBranchEqA (s : S) (a : A) (sc r d : U64) + (h0 : sc ≠ 0#u64) (hd : L.mem s (scA d) = 0#u64) : + pushDonationBranchA (st := st) (ar := ar) s a sc r d = + (replyBindProgramA (st := st) s sc r >>= fun s' => schedcontext.donate st ar s' a sc d) := by + unfold pushDonationBranchA + rw [eq_of_spec_fd (obj.Sc.is_null.spec sc), bind_ok, decide_eq_false h0, if_neg Bool.false_ne_true] + rw [eq_of_spec_fd (obj.Tcb.sc.spec (L := L) s d), bind_ok, hd, if_pos (show (0#u64 : U64) = 0#u64 from rfl)] + rfl + +set_option maxHeartbeats 300000 in +private theorem pushDonationBranchSkipA (s : S) (a : A) (sc r d : U64) + (h0 : sc ≠ 0#u64) (hd : L.mem s (scA d) ≠ 0#u64) : + pushDonationBranchA (st := st) (ar := ar) s a sc r d = ok (s, a) := by + unfold pushDonationBranchA + rw [eq_of_spec_fd (obj.Sc.is_null.spec sc), bind_ok, decide_eq_false h0, if_neg Bool.false_ne_true] + rw [eq_of_spec_fd (obj.Tcb.sc.spec (L := L) s d), bind_ok, if_neg hd] + +set_option maxHeartbeats 300000 in +private theorem replyPushDonateProgramA (s : S) (a : A) (f d r sc : U64) + (h : obj.Tcb.sc st f s = ok sc) : + ipc.reply_push st ar s a f d r true = + (pushPrefixProgramA (st := st) s f r >>= fun s' => pushDonationBranchA (st := st) (ar := ar) s' a sc r d) := by + unfold ipc.reply_push + rw [h, bind_ok] + simp only [pushPrefixProgramA, pushDonationBranchA, replyBindProgramA, Std.bind_tc_eq, Std.bind_assoc, ↓reduceIte] + +variable (st Z m R) in +/-- The write set of a donating reply push by `f`: reply objects' words, `f`'s state and reply words, MCS words. -/ +def PushFpA (σ : State Unit) (s : S) (f b : U64) : Prop := + (∃ q ∈ R, q.val ≤ b.val ∧ b.val < q.val + 32) ∨ b = stA f ∨ b = tReplyA f ∨ MWrA (st := st) Z m σ s b + +/-- The receiver's state and the endpoint queues survive a push by another thread. -/ +structure ReplyPushViewA (f : ObjRef) (σ σ' : State Unit) : Prop where + ep : ∀ q x, σ.kheap q = some (.endpoint x) → σ'.kheap q = some (.endpoint x) + tcb : ∀ q ≠ f, ∀ x, σ.kheap q = some (.tcb x) → + ∃ y, σ'.kheap q = some (.tcb y) ∧ y.tcbState = x.tcbState + +private theorem ReplyPushViewA.transView {f : ObjRef} {σ σ1 σ' : State Unit} + (h : ReplyPushViewA f σ σ1) (h' : KView σ1 σ') : ReplyPushViewA f σ σ' := by + refine ⟨fun q x hk => h'.1 _ _ (h.ep q x hk), ?_⟩ + intro q qf x hk + obtain ⟨y, hy, hs⟩ := h.tcb q qf x hk + obtain ⟨z, hz, hs', _⟩ := h'.2.2.1 q y hy + exact ⟨z, hz, hs'.trans hs⟩ + +private theorem pushBlockedRecvViewA {σ post : State Unit} {f r : ObjRef} {xf : Tcb} {xr : Reply} + (hkf : σ.kheap f = some (.tcb xf)) (hkr : σ.kheap r = some (.reply xr)) + (keep : ∀ q, q ≠ r → q ≠ f → post.kheap q = σ.kheap q) : + ReplyPushViewA f σ post := by + refine ⟨?_, ?_⟩ + · intro q x hx + have qr : q ≠ r := by intro e; rw [e, hkr] at hx; cases hx + have qf : q ≠ f := by intro e; rw [e, hkf] at hx; cases hx + exact (keep q qr qf).trans hx + · intro q qf x hx + have qr : q ≠ r := by intro e; rw [e, hkr] at hx; cases hx + exact ⟨x, (keep q qr qf).trans hx, rfl⟩ + +set_option maxHeartbeats 300000 in +private theorem replyPushAfterBlockA {s s2 : S} {σ post : State Unit} (a : A) {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (H2 : SchedSRA st Z' m κ s2 post) + (P : IpcSchedPlacementA Z'.th N E R Bf) (PE : EPlaced Z'.T E R Bf) + (E2 : EpStateRel Z'.T E R Bf (L.mem s2) post) + (HS2 : ReplyStackRelA (L.mem s2) R Z'.C post (scNameA m) stack) + {sc r f d : U64} (hc : sc ∈ Z'.C) (hc0 : sc ∈ Z.C) (hsc0 : sc ≠ 0#u64) + (hr : r ∈ R) (hi2 : ReplyIsolatedA (L.mem s2) Z'.C stack r) + (hf : f ∈ Z.T) (hd : d ∈ Z.T) (hf2 : f ∈ Z'.T) (hd2 : d ∈ Z'.T) + (hfd : f ≠ d) (hfs2 : L.mem s2 (scA f) = sc) (hd02 : L.mem s2 (scA d) = 0#u64) + (hfon : L.mem s (affA f) = L.cpu s) (hmig : L.mem s (affA d) = L.mem s (scCoreA sc)) + (hV2 : L.mem s2 (core.num.U64.wrapping_add f 3664#u64) = 0#u64) + (hni : NotIdleScA m (scNameA m sc)) (hQ2 : NotQueued post (specPtr f)) + (hFrame : ReplyPushViewA (specPtr f) σ post) + (hSpec : replyPush (Z := Unit) (specPtr f) (specPtr d) (specPtr r) true σ = + (bindScReply (Z := Unit) (scNameA m sc) (specPtr r) >>= fun _ => + schedContextDonate (scNameA m sc) (specPtr d)) post) + (hpost : ∀ σ', replyPush (Z := Unit) (specPtr f) (specPtr d) (specPtr r) true σ = ({((), σ')}, False) → + RelIfBoundSc σ' (specPtr d)) : + pushDonationBranchA (st := st) (ar := ar) s2 a sc r d ⦃ out => out.2 = a ∧ ∃ σ', + replyPush (Z := Unit) (specPtr f) (specPtr d) (specPtr r) true σ = ({((), σ')}, False) ∧ + IpcSchedRelA st Z' m κ out.1 σ' ∧ EpStateRel Z'.T E R Bf (L.mem out.1) σ' ∧ + ReplyStackRelA (L.mem out.1) R Z'.C σ' (scNameA m) (Function.update stack sc (r :: stack sc)) ∧ + ReplyPushViewA (specPtr f) σ σ' ∧ + WritesIn st s2 out.1 (fun b => (∃ q ∈ R, q.val ≤ b.val ∧ b.val < q.val + 32) ∨ + MWrA (st := st) Z' m post s2 b) ⦄ := by + have hon2 : L.mem s2 (affA f) = L.cpu s2 := by rw [H.aff_eq H2 hf hf2, H.cpu_eq H2, hfon] + have hmig2 : L.mem s2 (affA d) = L.mem s2 (scCoreA sc) := by + rw [H.aff_eq H2 hd hd2, H2.scCore sc hc, ← H.scCore sc hc0, hmig] + rw [pushDonationBranchEqA (ar := ar) s2 a sc r d hsc0 hd02] + exact WP.spec_mono + (replyPushDonateTailFpA a H2 P PE E2 HS2 hc hsc0 hr hi2 hf2 hd2 hfd hfs2 hd02 hon2 hmig2 + hV2 hni hQ2 (fun σ' h => hpost σ' (hSpec.trans h))) (fun _ ⟨ha, σ', hs, H', E', S', V, w⟩ => + ⟨ha, σ', hSpec.trans hs, H', E', S', hFrame.transView V, w⟩) + +set_option maxHeartbeats 20000 in +private theorem replyPushCallerContA {s s2 : S} {σ post : State Unit} (a : A) {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) {Q2 R2} + (H2 : SchedSRA st { Z with Q := Q2, R := R2 } m κ s2 post) + (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + (E2 : EpStateRel Z.T E R Bf (L.mem s2) post) + (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {sc r f d : U64} (hc : sc ∈ Z.C) (hsc0 : sc ≠ 0#u64) + (hr : r ∈ R) (hi : ReplyIsolatedA (L.mem s) Z.C stack r) + (hf : f ∈ Z.T) (hd : d ∈ Z.T) (hfd : f ≠ d) + (hfon : L.mem s (affA f) = L.cpu s) (hmig : L.mem s (affA d) = L.mem s (scCoreA sc)) + (hV : L.mem s (core.num.U64.wrapping_add f 3664#u64) = 0#u64) + (hni : NotIdleScA m (scNameA m sc)) (hQ2 : NotQueued post (specPtr f)) + {xf xd : Tcb} {xr : Reply} + (hkf : σ.kheap (specPtr f) = some (.tcb xf)) + (hkr : σ.kheap (specPtr r) = some (.reply xr)) + (keep : ∀ q : ObjRef, q ≠ specPtr r → q ≠ specPtr f → post.kheap q = σ.kheap q) + (linked : post.kheap (specPtr r) = some (.reply { xr with replyTcb := some (specPtr f) })) + (hf2 : post.kheap (specPtr f) = some (.tcb { xf with tcbState := .blockedOnReply (specPtr r) })) + (hd2 : post.kheap (specPtr d) = some (.tcb xd)) + (hSCf : xf.tcbSchedContext = some (scNameA m sc)) (hSCd : xd.tcbSchedContext = none) + (w2 : WritesIn st s s2 (fun b => b = rTcbA r ∨ b = stA f ∨ b = tReplyA f ∨ + b = L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION)) + (hSpec : replyPush (Z := Unit) (specPtr f) (specPtr d) (specPtr r) true σ = + (bindScReply (Z := Unit) (scNameA m sc) (specPtr r) >>= fun _ => + schedContextDonate (scNameA m sc) (specPtr d)) post) + (hpost : ∀ σ', replyPush (Z := Unit) (specPtr f) (specPtr d) (specPtr r) true σ = ({((), σ')}, False) → + RelIfBoundSc σ' (specPtr d)) : + pushDonationBranchA (st := st) (ar := ar) s2 a sc r d ⦃ out => out.2 = a ∧ ∃ σ', + replyPush (Z := Unit) (specPtr f) (specPtr d) (specPtr r) true σ = ({((), σ')}, False) ∧ + IpcSchedRelA st Z m κ out.1 σ' ∧ EpStateRel Z.T E R Bf (L.mem out.1) σ' ∧ + ReplyStackRelA (L.mem out.1) R Z.C σ' (scNameA m) (Function.update stack sc (r :: stack sc)) ∧ + ReplyPushViewA (specPtr f) σ σ' ∧ + WritesIn st s out.1 (PushFpA st Z m R σ s f) ⦄ := by + obtain ⟨V, links⟩ := pushPrefixVcpuAndLinks H P PE hf hr w2 + have HS2 : ReplyStackRelA (L.mem s2) R Z.C post (scNameA m) stack := + pushFrameAfterBlockA HS HE hkf hkr keep linked links + have hi2 : ReplyIsolatedA (L.mem s2) Z.C stack r := + ⟨hi.1, (links r hr).1.trans hi.2.1, (links r hr).2.trans hi.2.2⟩ + obtain ⟨hfs2, hd02⟩ := pushAfterBlockScWordsA H2 hf hd hsc0 hni hf2 hd2 + (show { xf with tcbState := .blockedOnReply (specPtr r) }.tcbSchedContext = _ from hSCf) hSCd + have hz : SSzA σ post := fun p x n h => by + have pr : p ≠ specPtr r := fun e => by rw [e, linked] at h; cases h + have pf : p ≠ specPtr f := fun e => by rw [e, hf2] at h; cases h + exact ⟨x, (keep p pr pf).symm.trans h⟩ + refine WP.spec_mono (replyPushAfterBlockA a H H2 P PE E2 HS2 hc hc hsc0 hr hi2 hf hd hf hd hfd + hfs2 hd02 hfon hmig (V.trans hV) hni hQ2 (pushBlockedRecvViewA hkf hkr keep) hSpec hpost) + fun out ⟨ha, σ', hs, H', E', S', V', w⟩ => ⟨ha, σ', hs, H', E', S', V', ?_⟩ + refine (WritesIn.trans (st := st) w2 w).mono fun b h => ?_ + rcases h with (h | h | h | h) | (h | h) + · exact Or.inl ⟨r, hr, by rw [h, rTcbA_val], by rw [h, rTcbA_val]; omega⟩ + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr (Or.inl h)) + · refine Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ?_)))) + rw [h]; exact H.node_sks H.sch.cpuCs (by simp [layout.node.SCHED_ACTION]) + · exact Or.inl h + · exact Or.inr (Or.inr (Or.inr (mwr_back rfl rfl hz w2.1 h))) + +/-- The endpoint scheduler bundle with disjointness from the typed placement. -/ +theorem schedOpsEAOfPlacementA {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} + {Bf : Set (U64 × Word)} (P : IpcSchedPlacementA Z.th N E R Bf) : + SchedOpsEA st Z.T E R Bf (IpcSchedRelA st Z m κ) := + schedOpsEA_of_sched (Z := Z) (m := m) (κ := κ) (fun _ _ _ _ H _ hb => endpWriteOffRead H.toSchedSRN P hb) + (fun _ _ _ _ H _ hb => endpFootOffWrite H.toSchedSRN P hb) + + +attribute [local irreducible] pushBlockedStateA stsAct updK + +set_option maxHeartbeats 20000 in +private theorem replyPushProgramWpA {s : S} {a : A} {f d r sc : U64} + (hRead : obj.Tcb.sc st f s = ok sc) {Post : S × A → Prop} + (h : (pushPrefixProgramA (st := st) s f r >>= fun s' => + pushDonationBranchA (st := st) (ar := ar) s' a sc r d) ⦃ Post ⦄) : + ipc.reply_push st ar s a f d r true ⦃ Post ⦄ := + (replyPushDonateProgramA (ar := ar) s a f d r sc hRead).symm ▸ h + +set_option maxHeartbeats 20000 in +/-- The full donating `reply_push` at the IPC/scheduler join. + +[S] The donor is on the current core and the destination has the SC's affinity. +See FL4 `endpoint.rs:167–182`, `ipc.rs:167–181`, `schedcontext.rs:65–78` and +seL4 `schedcontext.c:320`. The joint caller supplies the post-donation +`RelIfBoundSc` fact for the destination. +-/ +theorem reply_push_donateFpA {s : S} {σ : State Unit} (a : A) {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {sc r f d : U64} + (hc : sc ∈ Z.C) (hsc0 : sc ≠ 0#u64) (hr : r ∈ R) (hi : ReplyIsolatedA (L.mem s) Z.C stack r) + (hf : f ∈ Z.T) (hd : d ∈ Z.T) (hfd : f ≠ d) (hfs : L.mem s (scA f) = sc) + (hd0 : L.mem s (scA d) = 0#u64) (hfon : L.mem s (affA f) = L.cpu s) + (hmig : L.mem s (affA d) = L.mem s (scCoreA sc)) (hV : L.mem s (core.num.U64.wrapping_add f 3664#u64) = 0#u64) + (hni : NotIdleScA m (scNameA m sc)) + (hEp : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr f ∉ z.epQueue) + (hQ : NotQueued σ (specPtr f)) (hRB : RelIfBound σ (specPtr f)) + (hAct : ∀ u, σ.schedulerAction = .switchThread u → u ≠ specPtr f) (hIdle : specPtr f ≠ σ.idleThread) + (hpost : ∀ σ', replyPush (Z := Unit) (specPtr f) (specPtr d) (specPtr r) true σ = ({((), σ')}, False) → + RelIfBoundSc σ' (specPtr d)) : + ipc.reply_push st ar s a f d r true ⦃ out => out.2 = a ∧ ∃ σ', + replyPush (Z := Unit) (specPtr f) (specPtr d) (specPtr r) true σ = ({((), σ')}, False) ∧ + IpcSchedRelA st Z m κ out.1 σ' ∧ EpStateRel Z.T E R Bf (L.mem out.1) σ' ∧ + ReplyStackRelA (L.mem out.1) R Z.C σ' (scNameA m) (Function.update stack sc (r :: stack sc)) ∧ + ReplyPushViewA (specPtr f) σ σ' ∧ WritesIn st s out.1 (PushFpA st Z m R σ s f) ⦄ := by + obtain ⟨xf, hkf, hxf⟩ := H.sch.tcb f hf + obtain ⟨xd, hkd, hxd⟩ := H.sch.tcb d hd + obtain ⟨xr, hkr, _⟩ := HE.reply r hr + let post := pushBlockedStateA σ (specPtr f) (specPtr r) xf xr + have hSCf : xf.tcbSchedContext = some (scNameA m sc) := by + rw [hxf, hfs, optScA, if_neg hsc0] + rfl + have hSCd : xd.tcbSchedContext = none := by + rw [hxd, hd0] + rfl + have hSpec := pushSpecPrefixA hkf hkd hkr hSCf hSCd + have hSCRead : obj.Tcb.sc st f s = ok sc := + (eq_of_spec_fd (obj.Tcb.sc.spec (L := L) s f)).trans (congrArg ok hfs) + refine replyPushProgramWpA hSCRead ?_ + let O := schedOpsEAOfPlacementA (st := st) (m := m) (κ := κ) P + have hPrefix := WP.spec_and (pushPrefixRelA O ⟨Z.Q, Z.R, H⟩ PE HE hf hr hkf hkr hEp hQ hRB hAct hIdle) + (pushPrefixWrites H PE hf hr) + obtain ⟨keep, linked, hf2, hd2, hQ2⟩ := pushBlockedHeapFactsA hkf hkd hkr hfd hQ + refine WP.spec_bind hPrefix ?_ + rintro s2 ⟨⟨E2, Q2, R2, H2⟩, w2⟩ + exact replyPushCallerContA a H H2 P PE HE E2 HS hc hsc0 hr hi hf hd hfd hfon hmig hV hni hQ2 + hkf hkr keep linked hf2 hd2 hSCf hSCd w2 hSpec hpost + +/-- `reply_push_donateFpA` without the write set. -/ +theorem reply_push_donateViewA {s : S} {σ : State Unit} (a : A) {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {sc r f d : U64} + (hc : sc ∈ Z.C) (hsc0 : sc ≠ 0#u64) (hr : r ∈ R) (hi : ReplyIsolatedA (L.mem s) Z.C stack r) + (hf : f ∈ Z.T) (hd : d ∈ Z.T) (hfd : f ≠ d) (hfs : L.mem s (scA f) = sc) + (hd0 : L.mem s (scA d) = 0#u64) (hfon : L.mem s (affA f) = L.cpu s) + (hmig : L.mem s (affA d) = L.mem s (scCoreA sc)) (hV : L.mem s (core.num.U64.wrapping_add f 3664#u64) = 0#u64) + (hni : NotIdleScA m (scNameA m sc)) + (hEp : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr f ∉ z.epQueue) + (hQ : NotQueued σ (specPtr f)) (hRB : RelIfBound σ (specPtr f)) + (hAct : ∀ u, σ.schedulerAction = .switchThread u → u ≠ specPtr f) (hIdle : specPtr f ≠ σ.idleThread) + (hpost : ∀ σ', replyPush (Z := Unit) (specPtr f) (specPtr d) (specPtr r) true σ = ({((), σ')}, False) → + RelIfBoundSc σ' (specPtr d)) : + ipc.reply_push st ar s a f d r true ⦃ out => out.2 = a ∧ ∃ σ', + replyPush (Z := Unit) (specPtr f) (specPtr d) (specPtr r) true σ = ({((), σ')}, False) ∧ + IpcSchedRelA st Z m κ out.1 σ' ∧ EpStateRel Z.T E R Bf (L.mem out.1) σ' ∧ + ReplyStackRelA (L.mem out.1) R Z.C σ' (scNameA m) (Function.update stack sc (r :: stack sc)) ∧ + ReplyPushViewA (specPtr f) σ σ' ⦄ := + WP.spec_mono (reply_push_donateFpA a H P PE HE HS hc hsc0 hr hi hf hd hfd hfs hd0 hfon hmig hV hni hEp hQ hRB + hAct hIdle hpost) fun _ ⟨ha, σ', hs, H', E', S', V, _⟩ => ⟨ha, σ', hs, H', E', S', V⟩ + +/-- Full donating reply push at the fixed, migration-free scheduler view. -/ +theorem reply_push_donateA {s : S} {σ : State Unit} (a : A) {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) {sc r f d : U64} + (hc : sc ∈ Z.C) (hsc0 : sc ≠ 0#u64) (hr : r ∈ R) (hi : ReplyIsolatedA (L.mem s) Z.C stack r) + (hf : f ∈ Z.T) (hd : d ∈ Z.T) (hfd : f ≠ d) (hfs : L.mem s (scA f) = sc) + (hd0 : L.mem s (scA d) = 0#u64) (hfon : L.mem s (affA f) = L.cpu s) + (hmig : L.mem s (affA d) = L.mem s (scCoreA sc)) + (hV : L.mem s (core.num.U64.wrapping_add f 3664#u64) = 0#u64) + (hni : NotIdleScA m (scNameA m sc)) + (hEp : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr f ∉ z.epQueue) + (hQ : NotQueued σ (specPtr f)) (hRB : RelIfBound σ (specPtr f)) + (hAct : ∀ u, σ.schedulerAction = .switchThread u → u ≠ specPtr f) (hIdle : specPtr f ≠ σ.idleThread) + (hpost : ∀ σ', replyPush (Z := Unit) (specPtr f) (specPtr d) (specPtr r) true σ = ({((), σ')}, False) → + RelIfBoundSc σ' (specPtr d)) : + ipc.reply_push st ar s a f d r true ⦃ out => out.2 = a ∧ ∃ σ', + replyPush (Z := Unit) (specPtr f) (specPtr d) (specPtr r) true σ = ({((), σ')}, False) ∧ + IpcSchedRelA st Z m κ out.1 σ' ∧ EpStateRel Z.T E R Bf (L.mem out.1) σ' ∧ + ReplyStackRelA (L.mem out.1) R Z.C σ' (scNameA m) (Function.update stack sc (r :: stack sc)) ⦄ := + WP.spec_mono (reply_push_donateViewA a H P PE HE HS hc hsc0 hr hi hf hd hfd hfs hd0 hfon hmig hV hni hEp hQ hRB + hAct hIdle hpost) fun _ ⟨ha, σ', hs, H', E', S', _⟩ => ⟨ha, σ', hs, H', E', S'⟩ + +set_option maxHeartbeats 400000 in +/-- **`reply_push` with donation allowed to a callee that has its own SC**: nothing is donated (the spec step is the +`canDonate = false` one), the reply stacks are unchanged. -/ +theorem reply_push_calleeScFpA {s : S} {σ : State Unit} (a : A) {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {sc r f d : U64} (hsc0 : sc ≠ 0#u64) (hr : r ∈ R) (hf : f ∈ Z.T) (hd : d ∈ Z.T) (hfd : f ≠ d) + (hfs : L.mem s (scA f) = sc) {xd : Tcb} (hkd : σ.kheap (specPtr d) = some (.tcb xd)) + (hdsc : xd.tcbSchedContext ≠ none) + (hEp : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr f ∉ z.epQueue) + (hQ : NotQueued σ (specPtr f)) (hRB : RelIfBound σ (specPtr f)) + (hAct : ∀ u, σ.schedulerAction = .switchThread u → u ≠ specPtr f) (hIdle : specPtr f ≠ σ.idleThread) : + ipc.reply_push st ar s a f d r true ⦃ out => out.2 = a ∧ ∃ σ', + replyPush (Z := Unit) (specPtr f) (specPtr d) (specPtr r) true σ = ({((), σ')}, False) ∧ + IpcSchedRelA st Z m κ out.1 σ' ∧ EpStateRel Z.T E R Bf (L.mem out.1) σ' ∧ + ReplyStackRelA (L.mem out.1) R Z.C σ' (scNameA m) stack ∧ + ReplyPushViewA (specPtr f) σ σ' ∧ WritesIn st s out.1 (PushFpA st Z m R σ s f) ⦄ := by + obtain ⟨xf, hkf, -⟩ := H.sch.tcb f hf + obtain ⟨xr, hkr, -⟩ := HE.reply r hr + have hSpec := pushSpecNoDonA hkf hkd hkr hdsc + have hSCRead : obj.Tcb.sc st f s = ok sc := + (eq_of_spec_fd (obj.Tcb.sc.spec (L := L) s f)).trans (congrArg ok hfs) + refine replyPushProgramWpA hSCRead ?_ + let O := schedOpsEAOfPlacementA (st := st) (m := m) (κ := κ) P + have hPrefix := WP.spec_and (pushPrefixRelA O ⟨Z.Q, Z.R, H⟩ PE HE hf hr hkf hkr hEp hQ hRB hAct hIdle) + (pushPrefixWrites H PE hf hr) + obtain ⟨keep, linked, hf2, hd2, hQ2⟩ := pushBlockedHeapFactsA hkf hkd hkr hfd hQ + refine WP.spec_bind hPrefix ?_ + rintro s2 ⟨⟨E2, S2⟩, w2⟩ + obtain ⟨-, links⟩ := pushPrefixVcpuAndLinks H P PE hf hr w2 + have HS2 : ReplyStackRelA (L.mem s2) R Z.C (pushBlockedStateA σ (specPtr f) (specPtr r) xf xr) (scNameA m) stack := + pushFrameAfterBlockA HS HE hkf hkr keep linked links + obtain ⟨Q2, R2, H2⟩ := S2 + have hd02 : L.mem s2 (scA d) ≠ 0#u64 := by + intro h0 + obtain ⟨yd, hyd, ed⟩ := H2.sch.tcb d hd + have h : xd.tcbSchedContext = optScA m (L.mem s2 (scA d)) := + (congrArg Tcb.tcbSchedContext (KernelObject.tcb.inj (Option.some.inj (hd2.symm.trans hyd)))).trans ed + rw [h0, optScA, if_pos rfl] at h + exact hdsc h + rw [pushDonationBranchSkipA (ar := ar) s2 a sc r d hsc0 hd02] + refine (WP.spec_ok _).2 ⟨rfl, _, hSpec, ⟨Q2, R2, H2⟩, E2, HS2, pushBlockedRecvViewA hkf hkr keep, ?_⟩ + refine w2.mono fun b h => ?_ + rcases h with h | h | h | h + · exact Or.inl ⟨r, hr, by rw [h, rTcbA_val], by rw [h, rTcbA_val]; omega⟩ + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr (Or.inl h)) + · refine Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ?_)))) + rw [h]; exact H.node_sks H.sch.cpuCs (by simp [layout.node.SCHED_ACTION]) + +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/IpcScRecvPushA.lean b/proofs/Fl4ProofsA64/P3/IpcScRecvPushA.lean new file mode 100644 index 000000000..215f53054 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/IpcScRecvPushA.lean @@ -0,0 +1,173 @@ +/- +P3 (AArch64): receive from a queued caller or faulted sender with a reply object +and a live donation selector. The sender's sporadic check precedes reply_push. +[S] The donor is local, the receiver already has the SC's affinity, and the +donor has no bound VCPU. The joint caller supplies the released-SC fact. +-/ +import Fl4ProofsA64.P3.IpcScEndpointRefillA +import Fl4ProofsA64.P3.IpcScEndpointPushProgramA + +open Aeneas Aeneas.Std Result +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4SpecA64 Sched +noncomputable section + +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + +private theorem faultNotTimeoutA {o : Option Fault} (hn : ∀ b, o ≠ some (.timeout b)) : + o.elim false isTimeoutFault = false := by + cases h : o with + | none => rfl + | some f => + cases f + all_goals first | rfl | exact (hn _ h).elim + +set_option maxHeartbeats 200000 in +private theorem recvSendDonateTailA {s : S} {σ : State Unit} (a : A) {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {sc r f t e : U64} (hc : sc ∈ Z.C) (hsc0 : sc ≠ 0#u64) (hr : r ∈ R) (hi : ReplyIsolatedA (L.mem s) Z.C stack r) + (hf : f ∈ Z.T) (ht : t ∈ Z.T) (hft : f ≠ t) {xs xt : Tcb} + (hk : σ.kheap (specPtr f) = some (.tcb xs)) (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hsf : xs.tcbSchedContext = some (scNameA m sc)) (hst : xt.tcbSchedContext = none) + (hfon : L.mem s (affA f) = L.cpu s) (hmig : L.mem s (affA t) = L.mem s (scCoreA sc)) + (hV : L.mem s (core.num.U64.wrapping_add f 3664#u64) = 0#u64) + (hni : NotIdleScA m (scNameA m sc)) {dd : SenderPayload} + (hstd : xs.tcbState = .blockedOnSend (specPtr e) dd) + (hcall : dd.senderIsCall = true ∨ xs.tcbFault ≠ none) + (hgrant : dd.senderCanGrant = true ∨ dd.senderCanGrantReply = true) + (htmo : ∀ b, xs.tcbFault ≠ some (.timeout b)) + (hEp : ∀ e' ∈ E, ∀ z, σ.kheap (specPtr e') = some (.endpoint z) → specPtr f ∉ z.epQueue) + (hAct : ∀ u, σ.schedulerAction = .switchThread u → u ≠ specPtr f) (hIdle : specPtr f ≠ σ.idleThread) + (hpost : ∀ σ1 σ', ifSporadicCurScAssertRefillUnblockCheck (Z := Unit) xs.tcbSchedContext σ = ({((), σ1)}, False) → + σ1.kheap (specPtr f) = some (.tcb xs) → + replyPush (Z := Unit) (specPtr f) (specPtr t) (specPtr r) true σ1 = ({((), σ')}, False) → + RelIfBoundSc σ' (specPtr t)) : + recvSendAfterTransferA (st := st) (ar := ar) s a t f r dd ⦃ out => out.2 = a ∧ + ∃ σ1 σ', ifSporadicCurScAssertRefillUnblockCheck (Z := Unit) xs.tcbSchedContext σ = ({((), σ1)}, False) ∧ + σ1.kheap (specPtr f) = some (.tcb xs) ∧ + replyPush (Z := Unit) (specPtr f) (specPtr t) (specPtr r) true σ1 = ({((), σ')}, False) ∧ + EpStateRel Z.T E R Bf (L.mem out.1) σ' ∧ IpcSchedRelA st Z m κ out.1 σ' ∧ + ReplyStackRelA (L.mem out.1) R Z.C σ' (scNameA m) (Function.update stack sc (r :: stack sc)) ⦄ := by + have hnr : runnable xs.tcbState = false := by rw [hstd]; rfl + have hr0 : r ≠ 0#u64 := fun eq => (PE.rep r hr).2.1 (by rw [eq]; rfl) + unfold recvSendAfterTransferA + refine WP.spec_bind (endpointPushSporadicA H P PE HE HS hf hr hi hk hnr) ?_ + rintro s1 ⟨σ1, hA, H1, E1, S1, I1, V1, keep, ha, hi'⟩ + have hk1 := keep (specPtr f) (.tcb xs) hk (fun _ _ e' => by cases e') + have ht1 := keep (specPtr t) (.tcb xt) hkt (fun _ _ e' => by cases e') + have hEp1 : ∀ e' ∈ E, ∀ z, σ1.kheap (specPtr e') = some (.endpoint z) → specPtr f ∉ z.epQueue := by + intro e' he' z hz + obtain ⟨x, hx, _⟩ := HE.ep e' he' + have hx' := keep (specPtr e') (.endpoint x) hx (fun _ _ e'' => by cases e'') + have hEq := KernelObject.endpoint.inj (Option.some.inj (hz.symm.trans hx')) + exact hEp e' he' z (hEq.symm ▸ hx) + obtain ⟨hsf1, hst1⟩ := pushAfterBlockScWordsA H1 hf ht hsc0 hni hk1 ht1 hsf hst + have hfon1 : L.mem s1 (affA f) = L.cpu s1 := by rw [H.aff_eq H1 hf hf, H.cpu_eq H1, hfon] + have hmig1 : L.mem s1 (affA t) = L.mem s1 (scCoreA sc) := by + rw [H.aff_eq H1 ht ht, H1.scCore sc hc, ← H.scCore sc hc, hmig] + have hE := E1.at_tcb hf hk1 + have hft0 : dd.senderIsCall = true ∨ L.mem s1 (tFaultA f) ≠ fl4_abi.consts.fault.NullFault := + hcall.imp id (fun hn he => by + unfold fl4_abi.consts.fault.NullFault at he + exact hn (hE.fault0.mp he)) + have hftT : L.mem s1 (tFaultA f) ≠ fl4_abi.consts.fault.Timeout := by + intro eq + unfold fl4_abi.consts.fault.Timeout at eq + obtain ⟨b, hb⟩ := hE.faultT.mp eq + exact htmo b hb + have hword : L.mem s1 (scA f) ≠ 0#u64 := fun e => hsc0 (hsf1.symm.trans e) + rw [recvSendSelectedDonateA (ar := ar) s1 a t f r dd hr0 hgrant hft0 hword hftT] + have hRB : RelIfBound σ1 (specPtr f) := + H1.rd _ _ hk1 (Or.inr (Or.inr (Or.inl ⟨_, _, hstd, htmo⟩))) + refine WP.spec_mono (reply_push_donateA a H1 P PE E1 S1 hc hsc0 hr I1 hf ht hft hsf1 hst1 + hfon1 hmig1 (V1.trans hV) hni hEp1 (H1.notQueued hk1 hnr) hRB + (fun u hu => hAct u (ha.symm.trans hu)) (fun e => hIdle (e.trans hi')) (fun σ' hp => hpost σ1 σ' hA hk1 hp)) ?_ + rintro out ⟨ha', σ', hD, H', E', S'⟩ + exact ⟨ha', σ1, σ', hA, hk1, hD, E', H', S'⟩ + +set_option maxHeartbeats 300000 in +/-- Receive from a waiting caller or faulted sender and push the reply onto its donated SC. + +[S] The sender is on the current core, has no bound VCPU, and the receiver +already has the SC's affinity. See FL4 `endpoint.rs:167–182`, `ipc.rs:167–181`, +`schedcontext.rs:65–78` and seL4 `schedcontext.c:331–349`. +-/ +theorem receive_from_sender_pushDonateA {s : S} {σ : State Unit} (a : A) {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {kb : U64} (hPl : PlaceA Z.th kb) (hSt : StateRelA Z.th (L.mem s) σ) (hVld : SlotsValidA Z.th (L.mem s)) + (hC : BufCover st Z.T Bf s σ) (hNative : BufNativeA Z.T (L.mem s)) + (hoff : ∀ y, IsSlotA Z.th y → ¬ EWrite Z.T E R Bf y ∧ + ¬ EWrite Z.T E R Bf (core.num.U64.wrapping_add y 8#u64)) + {t : U64} (ht : t ∈ Z.T) {xt : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hnb : tsCode xt.tcbState ≠ 4) + {e f : U64} (he : e ∈ E) {q : List ObjRef} + (hk : σ.kheap (specPtr e) = some (.endpoint (.sendEP (specPtr f :: q)))) + (hf : f ∈ Z.T) {xs : Tcb} (hks : σ.kheap (specPtr f) = some (.tcb xs)) + (hu : ∀ rp, ipcBufOfA true xt = some rp → ∀ k, k < 128 → inUserFrame (bufAddr rp k) σ) + (hcase : ∀ e' dd, xs.tcbState = .blockedOnSend e' dd → + (xs.tcbFault = none ∧ (dd.senderCanGrant = false ∨ ipcBufOfA false xs = none ∨ + unat (dataToMessageInfo (xs.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0)) ∨ + ∃ fault, xs.tcbFault = some fault ∧ FaultOKA (L := L) s f (ipcBufOfA true xt) xs fault) + {sc r : U64} (hc : sc ∈ Z.C) (hsc0 : sc ≠ 0#u64) (hr : r ∈ R) (hi : ReplyIsolatedA (L.mem s) Z.C stack r) + (hfs : L.mem s (scA f) = sc) (ht0 : L.mem s (scA t) = 0#u64) + (hfon : L.mem s (affA f) = L.cpu s) (hmig : L.mem s (affA t) = L.mem s (scCoreA sc)) + (hV : L.mem s (core.num.U64.wrapping_add f 3664#u64) = 0#u64) + (hni : NotIdleScA m (scNameA m sc)) + (hbranch : ∀ d, xs.tcbState = .blockedOnSend (specPtr e) d → + (d.senderIsCall = true ∨ xs.tcbFault ≠ none) ∧ (d.senderCanGrant = true ∨ d.senderCanGrantReply = true)) + (htmo : ∀ b, xs.tcbFault ≠ some (.timeout b)) + (hAct : ∀ u, σ.schedulerAction = .switchThread u → u ≠ specPtr f) (hIdle : specPtr f ≠ σ.idleThread) + (hReleased : ∀ σ', ((), σ') ∈ (specRecvSendA (specPtr t) (specPtr e) + (some (specPtr r)) (specPtr f :: q) σ).1 → RelIfBoundSc σ' (specPtr t)) : + endpoint.receive_from_sender st ar s a t e r ⦃ out => out.2 = a ∧ + ∃ σ', specRecvSendA (specPtr t) (specPtr e) (some (specPtr r)) (specPtr f :: q) σ = ({((), σ')}, False) ∧ + EpStateRel Z.T E R Bf (L.mem out.1) σ' ∧ IpcSchedRelA st Z m κ out.1 σ' ∧ + ReplyStackRelA (L.mem out.1) R Z.C σ' (scNameA m) (Function.update stack sc (r :: stack sc)) ⦄ := by + let O := schedOpsEAOfPlacementA (st := st) (m := m) (κ := κ) P + have hsSpec : xs.tcbSchedContext = some (scNameA m sc) := by + rw [schedTcbScA H hf hks, hfs, optScA, if_neg hsc0] + rfl + have htSpec : xt.tcbSchedContext = none := by + rw [schedTcbScA H ht hkt, ht0, optScA, if_pos (show (0#u64 : U64) = 0#u64 from rfl)] + refine receive_from_senderContA O PE hPl s a σ HE ⟨Z.Q, Z.R, H⟩ hSt hVld hC hNative hoff ht + ((H.thT t).2 ht) hkt hnb he hk ((H.thT f).2 hf) hks hu r hcase ?_ + intro s2 σ2 dd _ htf hfe hst E2 hS2 hW hE hk2 hq2 hT + obtain ⟨Q2, R2, H2⟩ := hS2 + obtain ⟨xt2, hkt2, _⟩ := E2.tcb t ht + have htSpec2 := (epStepTcbScA hE hkt hkt2).trans htSpec + have HS2 := endpointStackFrameA H.toSchedSRN P PE HE HS hW hE + have hi2 := endpointKeepsIsolatedA H.toSchedSRN P PE hr hi hW + obtain ⟨hcall, hgrant⟩ := hbranch dd hst + have hnt := faultNotTimeoutA htmo + have hflag : decide (xs.tcbSchedContext ≠ none ∧ ¬ (xs.tcbFault.elim false isTimeoutFault = true)) = true := + decide_eq_true ⟨by rw [hsSpec]; exact Option.some_ne_none _, by rw [hnt]; exact Bool.false_ne_true⟩ + have exactArm (σ1 : State Unit) + (hA : ifSporadicCurScAssertRefillUnblockCheck (Z := Unit) xs.tcbSchedContext σ2 = ({((), σ1)}, False)) + (hx1 : σ1.kheap (specPtr f) = some (.tcb xs)) : + specRecvSendA (specPtr t) (specPtr e) (some (specPtr r)) (specPtr f :: q) σ = + replyPush (Z := Unit) (specPtr f) (specPtr t) (specPtr r) true σ1 := by + rw [recvSend_pushSpecA hk hks hfe hst hT hk2 hA hx1 hcall hgrant, hflag] + have act2 : σ2.schedulerAction = σ.schedulerAction := by + have eq := congrArg AbstractState.schedulerAction hE.2.1 + exact eq + have idle2 : σ2.idleThread = σ.idleThread := (SchedKeepA.step hE).2.2.1 + have hon2 : L.mem s2 (affA f) = L.cpu s2 := by rw [H.aff_eq H2 hf hf, H.cpu_eq H2, hfon] + have hmig2 : L.mem s2 (affA t) = L.mem s2 (scCoreA sc) := by + rw [H.aff_eq H2 ht ht, H2.scCore sc hc, ← H.scCore sc hc, hmig] + have hv2 := (endpointKeepsVcpuA (Z := Z) PE hf hW).trans hV + refine WP.spec_mono (recvSendDonateTailA a H2 P PE E2 HS2 hc hsc0 hr hi2 hf ht htf.symm hk2 hkt2 hsSpec + htSpec2 hon2 hmig2 hv2 hni hst hcall hgrant htmo hq2 (fun u hu' => hAct u (act2.symm.trans hu')) + (fun e => hIdle (e.trans idle2)) ?_) ?_ + · intro σ1 σ' hA hx1 hD + have hm : ((), σ') ∈ (specRecvSendA (specPtr t) (specPtr e) (some (specPtr r)) (specPtr f :: q) σ).1 := by + rw [exactArm σ1 hA hx1, hD] + exact Set.mem_singleton _ + exact hReleased σ' hm + · rintro out ⟨hv, σ1, σ', hA, hx1, hD, E', H', S'⟩ + exact ⟨hv, σ', (exactArm σ1 hA hx1).trans hD, E', H', S'⟩ + +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/IpcScRecvReturnA.lean b/proofs/Fl4ProofsA64/P3/IpcScRecvReturnA.lean new file mode 100644 index 000000000..071d5dfca --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/IpcScRecvReturnA.lean @@ -0,0 +1,75 @@ +/- +P3 (AArch64): a blocking receive on an inactive bound notification can return +the receiver's SC before the endpoint dispatch. SndRunA supplies absence from +the release queue. The receive entry theorem accepts this body at either of +the reply-cap prefix outcomes. +-/ +import Fl4ProofsA64.P3.IpcScGlueA + +open Aeneas Aeneas.Std Result +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4SpecA64 Sched +noncomputable section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +private theorem rcvReturningProgramA (s : S) (a : A) (t e n rv : U64) + (hz : n ≠ 0#u64) (hb : L.mem s (tBoundA t) = n) + (hs : stOf (L.mem s) n ≠ obj.NTFN_ACTIVE) : + rcvBodyA st ar s a t e true rv = + (notification.maybe_return_sched_context st s n t >>= fun s' => rcvDispA st ar s' a t e true rv) := by + unfold rcvBodyA + have hbRead := eq_of_spec_fd (obj.Tcb.bound_ntfn.spec (L := L) s t) + have hsRead := eq_of_spec_fd (obj.Ntfn.state.spec (L := L) s n) + rw [hbRead, bind_ok, hb, if_pos (show (n != 0#u64) = true by simpa using hz), + hsRead, bind_ok, epState_eq, if_neg hs] + simp only [show (n != 0#u64) = true by simpa using hz, ↓reduceIte, Std.bind_tc_eq, + Std.bind_assoc, bind_ok] + rfl + +private theorem specBodyReturningA {σ σ' : State Unit} {t e n : ObjRef} {xt : Tcb} + {x : Endpoint} {xn : Notification} (hkt : σ.kheap t = some (.tcb xt)) + (hke : σ.kheap e = some (.endpoint x)) (hkn : σ.kheap n = some (.notification xn)) + (hb : xt.tcbBoundNotification = some n) (hs : isActive xn = false) + (hret : maybeReturnSc (Z := Unit) n t σ = ({((), σ')}, False)) (reply : Option ObjRef) : + specRecvBodyA t e true reply σ = specDispA t e true reply x σ' := by + unfold specRecvBodyA + rw [nd_bind_one (getEndpoint_eq hke), nd_bind_one (getTcbObjRef_eq _ hkt)] + simp only [hb] + rw [nd_bind_one (getNotification_eq hkn), if_neg (by simp [hs]), + nd_when_true_bindA (by simp [hb]), the_someA, nd_bind_one hret] + cases x <;> rfl + +/-- The inactive bound-notification body with the real SC-return operation. +The continuation receives the framed dispatch state and the exact spec step. -/ +theorem rcv_body_return_scA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {N E R : Set U64} {Bf : Set (U64 × Word)} {s : S} {σ : State Unit} (a : A) + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) + (PI : IPlacedA N Z.T E R Bf) (hc : m.curCore < fl4A64Platform.numNodes) + (HN : NtfnStateRel N Z.T (L.mem s) σ) (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + {t e n rv : U64} (ht : t ∈ Z.T) (hn : n ∈ N) + {xt : Tcb} {x : Endpoint} {xn : Notification} + (hkt : σ.kheap (specPtr t) = some (.tcb xt)) (hke : σ.kheap (specPtr e) = some (.endpoint x)) + (hkn : σ.kheap (specPtr n) = some (.notification xn)) (hb : xt.tcbBoundNotification = some (specPtr n)) + (hs : isActive xn = false) (hrun : SndRunA σ (specPtr t) xt) + (hdisp : ∀ s' σ' Q, maybeReturnSc (Z := Unit) (specPtr n) (specPtr t) σ = ({((), σ')}, False) → + SchedSRA st { Z with Q := Q } m κ s' σ' → + NtfnStateRel N Z.T (L.mem s') σ' → EpStateRel Z.T E R Bf (L.mem s') σ' → + MFrA (st := st) Z m s s' σ σ' → + rcvDispA st ar s' a t e true rv ⦃ RcvPostA st Z.T E R Bf (IpcSchedRelA st Z m κ) + (specDispA (specPtr t) (specPtr e) true (optRef rv) x) σ' ⦄) : + rcvBodyA st ar s a t e true rv ⦃ RcvPostA st Z.T E R Bf (IpcSchedRelA st Z m κ) + (specRecvBodyA (specPtr t) (specPtr e) true (optRef rv)) σ ⦄ := by + obtain ⟨hz, hbRead⟩ := bound_wordA (HE.at_tcb ht hkt) hb + have hns := stOf_not_activeA (HN.at_ntfn hn hkn).obj (fun b e => by + simp [isActive, e] at hs) + rw [rcvReturningProgramA (ar := ar) s a t e n rv hz hbRead hns] + refine WP.spec_bind (maybeReturnIpcA H P PI.e PI.n hc HN HE hn ht hrun.2.1.2) ?_ + rintro s' ⟨σ', Q, h, H', HN', HE', F⟩ + have eqBody := specBodyReturningA hkt hke hkn hb hs h (optRef rv) + exact WP.spec_mono (hdisp s' σ' Q h H' HN' HE' F) fun out hp => by + unfold RcvPostA at hp ⊢ + rw [eqBody] + exact hp + +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/IpcScRemoveA.lean b/proofs/Fl4ProofsA64/P3/IpcScRemoveA.lean new file mode 100644 index 000000000..a3da25d56 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/IpcScRemoveA.lean @@ -0,0 +1,82 @@ +/- P3 (AArch64): the spec lookup and the detached arm of reply removal. -/ +import Fl4ProofsA64.P3.IpcScPopTopA +import Fl4ProofsA64.P3.IpcScRemoveSpliceA + +open Aeneas Aeneas.Std Result +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4SpecA64 Sched +noncomputable section + +theorem replyRemoveSomeA {σ : State Unit} {t r p : ObjRef} + {xr : Reply} (hkr : σ.kheap r = some (.reply xr)) (hb : xr.replyTcb = some t) + (hf : scWithReply r σ = some p) + {x : SchedContext} {n : Nat} (hk : σ.kheap p = some (.schedContext x n)) + {xt : Tcb} (hkt : σ.kheap t = some (.tcb xt)) : + replyRemove (Z := Unit) t r σ = replyPopSpecA p t r x.scReplies xt.tcbSchedContext σ := by + unfold replyRemove replyPopSpecA + rw [nd_bind_oneSc (getReply_eq hkr), nd_bind_oneSc (assert_trueSc hb σ), nd_gets_bindSc, hf] + simp only [getTcbObjRef, nd_bind_assocSc, nd_bind_oneSc (scReplies_getA hk), + nd_bind_oneSc (threadGet_eqSc Tcb.tcbSchedContext hkt)] + +theorem replyRemoveNoneA {σ : State Unit} {t r : ObjRef} + {xr : Reply} (hkr : σ.kheap r = some (.reply xr)) (hb : xr.replyTcb = some t) + (hf : scWithReply r σ = none) : + replyRemove (Z := Unit) t r σ = replyUnlinkTcb (Z := Unit) t r σ := by + unfold replyRemove + rw [nd_bind_oneSc (getReply_eq hkr), nd_bind_oneSc (assert_trueSc hb σ), nd_gets_bindSc, hf] + +section Store +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + +theorem replyPointerNonzeroA (PE : EPlaced Z.T E R Bf) {r : U64} (hr : r ∈ R) : r ≠ 0#u64 := + fun e => (PE.rep r hr).2.1 (by rw [e]; rfl) + +theorem replyPointerEvenA (PE : EPlaced Z.T E R Bf) {r : U64} (hr : r ∈ R) : r.val % 2 = 0 := by + have h := (PE.rep r hr).1 + omega + +private theorem replyMaskOfPlacedA (PE : EPlaced Z.T E R Bf) (r : U64) + (hr : r ≠ 0#u64 → r ∈ R) : r &&& ~~~1#u64 = r := by + apply mask1_of_even + by_cases hz : r = 0#u64 + · subst r; decide + · exact replyPointerEvenA PE (hr hz) + +/-- Detach a reply which belongs to no scheduling-context stack. -/ +theorem replyRemoveDetachedRefA {s : S} {σ : State Unit} (a : A) {stack : U64 → List U64} + (H : IpcSchedRelA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {r t : U64} (hr : r ∈ R) (ht : t ∈ Z.T) (hd : ∀ sc ∈ Z.C, r ∉ stack sc) + (hc : ScReplyCovA Z.C m σ (specPtr r)) + {xt : Tcb} {xr : Reply} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hkr : σ.kheap (specPtr r) = some (.reply xr)) (hb : xr.replyTcb = some (specPtr t)) + (hst : xt.tcbState = .blockedOnReply (specPtr r) ∨ + ∃ ep d, xt.tcbState = .blockedOnReceive ep (some (specPtr r)) d) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) : + ipc.reply_remove st ar s a r t ⦃ out => out.2 = a ∧ ∃ σ', + replyRemove (Z := Unit) (specPtr t) (specPtr r) σ = ({((), σ')}, False) ∧ + IpcSchedRelA st Z m κ out.1 σ' ∧ EpStateRel Z.T E R Bf (L.mem out.1) σ' ∧ + ReplyStackRelA (L.mem out.1) R Z.C σ' (scNameA m) stack ∧ + ReplyIsolatedA (L.mem out.1) Z.C stack r ⦄ := by + obtain ⟨Q, RR, H⟩ := H + obtain ⟨hh, hn, hp⟩ := HS.detached r hr hd + have hnR : L.mem s (rNextA r) ≠ 0#u64 → L.mem s (rNextA r) ∈ R := fun h => (hn h).1 + have hpR : L.mem s (rPrevA r) ≠ 0#u64 → L.mem s (rPrevA r) ∈ R := fun h => (hp h).1 + have en := (eq_of_spec_fd (obj.Reply.next.spec (L := L) s r)).trans + (congrArg ok (replyMaskOfPlacedA (Z := Z) PE _ hnR)) + have ep := (eq_of_spec_fd (obj.Reply.prev.spec (L := L) s r)).trans + (congrArg ok (replyMaskOfPlacedA (Z := Z) PE _ hpR)) + rw [replyRemoveSpliceProgramA (ar := ar) s a r t _ _ en ep hh] + refine WP.spec_bind (replySplicePrepareRefA H P PE HE hr hnR hpR) ?_ + rintro s1 ⟨H1, E1, mP, mN, mPn, mNp, mPr, mNr⟩ + obtain ⟨S1, i1⟩ := HS.detachedSplice (fun _ hd => replyPointerNonzeroA (Z := Z) PE hd) hr hd mP mN mPn mNp mPr mNr + refine WP.spec_bind (unlinkReplyRelA H1 P PE E1 S1 ht hr i1 hkt hkr hb hst hq) ?_ + rintro s2 ⟨σ2, hu, H2, E2, S2, i2⟩ + rw [WP.spec_ok, replyRemoveNoneA hkr hb (HS.scWithReply_none hd hc)] + exact ⟨rfl, σ2, hu, ⟨Q, RR, H2⟩, E2, S2, i2⟩ + +end Store +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/IpcScRemoveCutA.lean b/proofs/Fl4ProofsA64/P3/IpcScRemoveCutA.lean new file mode 100644 index 000000000..48749b0a2 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/IpcScRemoveCutA.lean @@ -0,0 +1,156 @@ +/- P3 (AArch64): cutting a live reply stack below its head. -/ +import Fl4ProofsA64.P3.IpcScRemoveA + +open Aeneas Aeneas.Std Result +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4SpecA64 Sched +noncomputable section + +private theorem takeWhileSpecPtrA (l : List U64) (hnd : l.Nodup) (k : Nat) (hk : k < l.length) : + (l.map specPtr).takeWhile (fun x => decide (x ≠ specPtr l[k])) = (l.take k).map specPtr := by + induction l generalizing k with + | nil => simp at hk + | cons d l ih => + cases k with + | zero => simp + | succ k => + have hk' : k < l.length := by simpa using hk + have hne : specPtr d ≠ specPtr l[k] := fun e => + (List.nodup_cons.1 hnd).1 (specPtr_inj e ▸ List.getElem_mem hk') + simp only [List.map_cons, List.getElem_cons_succ, List.take_succ_cons] + rw [List.takeWhile_cons_of_pos (by simpa using hne), ih (List.nodup_cons.1 hnd).2 k hk'] + +private theorem cutPopSpecA {σ σ1 : State Unit} {p t r h : ObjRef} {l : List ObjRef} {o : Option ObjRef} + (hh : l.head? = some h) (hne : h ≠ r) + (hu : replyUnlinkSc (Z := Unit) p r σ = ({((), σ1)}, False)) : + replyPopSpecA p t r l o σ = replyUnlinkTcb t r σ1 := by + unfold replyPopSpecA + rw [nd_bind_oneSc hu, hh] + simp only [Fl4Spec.NonDet.when, show the (some h) = h from rfl, hne, false_and, ↓reduceIte] + exact nd_bind_oneSc (show Fl4Spec.NonDet.pure () σ1 = ({((), σ1)}, False) from rfl) _ + +section Store +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + +private theorem cutReadNextA {s : S} {l : List U64} {sc : U64} + (hW : ReplyStackWordsA (L.mem s) sc l) (hR : ∀ d ∈ l, d ∈ R) (PE : EPlaced Z.T E R Bf) + {k : Nat} (hk : k + 1 < l.length) : + obj.Reply.next st l[k + 1] s = ok l[k] ∧ headBit (L.mem s (rNextA l[k + 1])) = false := by + have hraw : L.mem s (rNextA l[k + 1]) = l.getD k 0#u64 := (hW (k + 1) hk).2 + rw [List.getD_eq_getElem _ _ (by omega)] at hraw + have he := replyPointerEvenA (Z := Z) PE (hR _ (List.getElem_mem (by omega : k < l.length))) + exact ⟨(eq_of_spec_fd (obj.Reply.next.spec s _)).trans (by rw [hraw, mask1_of_even he]), + by rw [hraw]; exact headBitEvenA he⟩ + +private theorem cutReadPrevA {s : S} {l : List U64} {sc : U64} + (hW : ReplyStackWordsA (L.mem s) sc l) (hR : ∀ d ∈ l, d ∈ R) (PE : EPlaced Z.T E R Bf) + {k : Nat} (hk : k < l.length) : + obj.Reply.prev st l[k] s = ok (l.getD (k + 1) 0#u64) ∧ + (l.getD (k + 1) 0#u64 ≠ 0#u64 → l.getD (k + 1) 0#u64 ∈ R) := by + have hraw := (hW k hk).1 + have hpa : l.getD (k + 1) 0#u64 ≠ 0#u64 → l.getD (k + 1) 0#u64 ∈ R := by + intro h + by_cases hlt : k + 1 < l.length + · rw [List.getD_eq_getElem _ _ hlt] + exact hR _ (List.getElem_mem hlt) + · rw [List.getD_eq_default _ _ (by omega)] at h + exact (h rfl).elim + refine ⟨(eq_of_spec_fd (obj.Reply.prev.spec s _)).trans ?_, hpa⟩ + rw [hraw] + by_cases h : l.getD (k + 1) 0#u64 = 0#u64 + · rw [h, zero_or_masked] + · rw [mask1_of_even (replyPointerEvenA (Z := Z) PE (hpa h))] + +/-- Cut off the removed reply and the older callers, then unlink the removed reply's TCB. -/ +theorem replyRemoveCutRefA {s : S} {σ : State Unit} (a : A) {stack : U64 → List U64} + (H : IpcSchedRelA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {sc r t : U64} (hc : sc ∈ Z.C) (ht : t ∈ Z.T) + {k : Nat} (hk : k + 1 < (stack sc).length) (hr : (stack sc)[k + 1] = r) + (cov : ScReplyCovA Z.C m σ (specPtr r)) + {xt : Tcb} {xr : Reply} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hkr : σ.kheap (specPtr r) = some (.reply xr)) (hb : xr.replyTcb = some (specPtr t)) + (hst : xt.tcbState = .blockedOnReply (specPtr r) ∨ + ∃ ep d, xt.tcbState = .blockedOnReceive ep (some (specPtr r)) d) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) : + ipc.reply_remove st ar s a r t ⦃ out => out.2 = a ∧ ∃ σ', + replyRemove (Z := Unit) (specPtr t) (specPtr r) σ = ({((), σ')}, False) ∧ + IpcSchedRelA st Z m κ out.1 σ' ∧ EpStateRel Z.T E R Bf (L.mem out.1) σ' ∧ + ReplyStackRelA (L.mem out.1) R Z.C σ' (scNameA m) (Function.update stack sc ((stack sc).take (k + 1))) ∧ + ReplyIsolatedA (L.mem out.1) Z.C (Function.update stack sc ((stack sc).take (k + 1))) r ⦄ := by + subst r + obtain ⟨Q, RR, H⟩ := H + obtain ⟨⟨x, n, hx, hL⟩, hR, hnd, hW⟩ := HS.stackRel sc hc + have hrM : (stack sc)[k + 1] ∈ stack sc := List.getElem_mem hk + have hrR := hR _ hrM + have hprevR : (stack sc)[k] ∈ R := hR _ (List.getElem_mem (by omega : k < (stack sc).length)) + have h0 : 0 < (stack sc).length := by omega + have hHead : (stack sc).head? = some (stack sc)[0] := by + rw [List.head?_eq_getElem?, List.getElem?_eq_getElem h0] + have hne : (stack sc)[0] ≠ (stack sc)[k + 1] := by + intro e + have j := (List.nodup_iff_injective_get.1 hnd) (a₁ := ⟨0, h0⟩) (a₂ := ⟨k + 1, hk⟩) + (by simpa using e) + exact Nat.ne_of_lt (Nat.zero_lt_succ k) (congrArg Fin.val j) + have hrs : xr.replySc = none := by + cases ho : xr.replySc with + | none => rfl + | some p => + obtain ⟨c, hc', _, hh⟩ := (HS.owner _ hrR xr hkr p).1 ho + have e := HS.unique c hc' sc hc _ (List.mem_of_mem_head? hh) hrM + subst c + exact (hne (Option.some.inj (hHead.symm.trans hh))).elim + have hh : x.scReplies.head? = some (specPtr (stack sc)[0]) := by rw [hL, List.head?_map, hHead]; rfl + have hnePtr : specPtr (stack sc)[0] ≠ specPtr (stack sc)[k + 1] := fun e => hne (specPtr_inj e) + obtain ⟨hU, HS1, F⟩ := reply_remove_cutA H hc hkr hrs hx hh hnePtr + let σ1 := repStA σ (scNameA m sc) x n (x.scReplies.takeWhile fun r => decide (r ≠ specPtr (stack sc)[k + 1])) + have hEq : (x.scReplies.takeWhile fun r => decide (r ≠ specPtr (stack sc)[k + 1])) = + ((stack sc).take (k + 1)).map specPtr := by rw [hL]; exact takeWhileSpecPtrA _ hnd _ hk + have hx1 : σ1.kheap (scNameA m sc) = some (.schedContext { x with scReplies := ((stack sc).take (k + 1)).map specPtr } n) := by + unfold σ1 repStA + rw [hEq, updKSc_same] + have hof q (he : q ≠ scNameA m sc) : σ1.kheap q = σ.kheap q := repStA_ne he + have kt1 : σ1.kheap (specPtr t) = some (.tcb xt) := by + rw [hof _ (by intro e; rw [e, hx] at hkt; cases hkt)] + exact hkt + have kr1 : σ1.kheap (specPtr (stack sc)[k + 1]) = some (.reply xr) := by + rw [hof _ (by intro e; rw [e, hx] at hkr; cases hkr)] + exact hkr + have hq1 : ∀ e ∈ E, ∀ z, σ1.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue := by + intro e he z hz + obtain ⟨z0, hz0, _⟩ := HE.ep e he + rw [hof _ (by intro e'; rw [e', hx] at hz0; cases hz0), hz0] at hz + cases hz + exact hq e he _ hz0 + have E1 := HE.frame PE (fun _ _ _ => rfl) (epFrameOfMcsA H.toSchedSRN P F rfl).2 + obtain ⟨en, eb⟩ := cutReadNextA (Z := Z) hW hR PE hk + obtain ⟨ep, hp⟩ := cutReadPrevA (Z := Z) hW hR PE hk + rw [replyRemoveSpliceProgramA (ar := ar) s a _ t _ _ en ep eb] + refine WP.spec_bind (replySplicePrepareRefA HS1 P PE E1 hrR (fun _ => hprevR) hp) ?_ + rintro s1 ⟨H1, E2, mP, mN, mPn, mNp, mPr, mNr⟩ + have ev d (hd : d ∈ R) := replyPointerEvenA (Z := Z) PE hd + have hn d (hd : d ∈ R) (hd1 : d ≠ (stack sc)[k + 1]) + (hd2 : ∀ h : k + 1 + 1 < (stack sc).length, d ≠ (stack sc)[k + 1 + 1]) := + mN d hd hd1 (by + by_cases h : k + 1 + 1 < (stack sc).length + · rw [List.getD_eq_getElem _ _ h]; exact hd2 h + · rw [List.getD_eq_default _ _ (by omega)]; exact replyPointerNonzeroA (Z := Z) PE hd) + have hp1 (h : k + 1 + 1 < (stack sc).length) : + L.mem s1 (rNextA (stack sc)[k + 1 + 1]) = 0#u64 := by + have e := List.getD_eq_getElem (stack sc) (0#u64) h + have hnz := replyPointerNonzeroA (Z := Z) PE (hR _ (List.getElem_mem h)) + exact e ▸ mNp (fun z => hnz (e.symm.trans z)) + obtain ⟨SR1, i1⟩ := HS.cut ev hc (k := k + 1) (by omega) hk mP hn + (mPn (replyPointerNonzeroA (Z := Z) PE hprevR)) hp1 mPr mNr hx hx1 + (fun c hc' hne e => hne (H.scp.inj c hc' sc hc e)) + (fun d hd => let ⟨y, hy, _⟩ := HE.reply d hd; ⟨y, hy⟩) hof + refine WP.spec_bind (unlinkReplyRelA H1 P PE E2 SR1 ht hrR i1 kt1 kr1 hb hst hq1) ?_ + rintro s2 ⟨σ2, hF, H2, E3, SR2, i2⟩ + rw [WP.spec_ok, replyRemoveSomeA hkr hb (HS.scWithReply_some hc hrM cov) hx hkt, cutPopSpecA hh hnePtr hU] + exact ⟨rfl, σ2, hF, ⟨Q, RR, H2⟩, E3, SR2, i2⟩ + +end Store +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/IpcScRemoveSpliceA.lean b/proofs/Fl4ProofsA64/P3/IpcScRemoveSpliceA.lean new file mode 100644 index 000000000..dd7116fce --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/IpcScRemoveSpliceA.lean @@ -0,0 +1,183 @@ +/- P3 (AArch64): the concrete reply-link clears shared by the non-head remove arms. -/ +import Fl4ProofsA64.P3.IpcScPopA + +open Aeneas Aeneas.Std Result +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4SpecA64 Sched +noncomputable section + +private def replyLinkAddr (next : Bool) (r : U64) : U64 := + if next then rNextA r else rPrevA r + +section Store +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + +private theorem replyLinkAddrPlace (PE : EPlaced Z.T E R Bf) (next : Bool) {r : U64} (hr : r ∈ R) : + Aligned (replyLinkAddr next r) ∧ (replyLinkAddr next r).val = r.val + (if next then 16 else 8) := by + cases next + · exact replyPrevPlaceA (Z := Z) PE hr + · exact replyNextPlaceA (Z := Z) PE hr + +private theorem replyLinkAddrEq {s : S} {σ : State Unit} + (H : SchedSRN st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + {q r : U64} (hq : q ∈ R) (hr : r ∈ R) (i j : Bool) : + replyLinkAddr i q = replyLinkAddr j r ↔ i = j ∧ q = r := by + constructor + · intro eq + have vq := (replyLinkAddrPlace PE i hq).2 + have vr := (replyLinkAddrPlace PE j hr).2 + have va := congrArg UScalar.val eq + by_cases same : q = r + · subst r + have e : (if i then 16 else 8 : Nat) = (if j then 16 else 8 : Nat) := by omega + refine ⟨?_, rfl⟩ + cases i <;> cases j <;> first | rfl | cases e + · obtain split | split := replyWordsApartA H P hq hr same <;> + cases i <;> cases j <;> simp at vq vr <;> omega + · rintro ⟨rfl, rfl⟩ + rfl + +private def clearReplyFieldA (s : S) (next : Bool) (r : U64) : Result S := + if (r != 0#u64) = true then + if next then obj.Reply.set_next st r s 0#u64 false else obj.Reply.set_prev st r s 0#u64 + else ok s + +private theorem clearReplyFieldRefA {s : S} {σ : State Unit} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (side : Bool) (r : U64) (hr : r ≠ 0#u64 → r ∈ R) : + clearReplyFieldA (st := st) s side r ⦃ s' => + SchedSRA st Z m κ s' σ ∧ FootEq Z.T E R Bf (L.mem s) (L.mem s') ∧ + (∀ b, Aligned b → L.mem s' b = if r ≠ 0#u64 ∧ b = replyLinkAddr side r then 0#u64 else L.mem s b) ⦄ := by + by_cases hn : r = 0#u64 + · unfold clearReplyFieldA + rw [show (r != 0#u64) = false by simp [hn]] + refine (WP.spec_ok _).2 ⟨H, fun _ _ _ => rfl, ?_⟩ + intro b hb + simp [hn] + · have hR := hr hn + have ea : clearReplyFieldA (st := st) s side r = ok (L.wr s (replyLinkAddr side r) 0#u64) := by + unfold clearReplyFieldA + rw [show (r != 0#u64) = true by simpa using hn] + cases side + · exact setPrevZeroEq _ _ + · exact setNextZeroEq _ _ + rw [ea, WP.spec_ok] + have ha := replyLinkAddrPlace PE side hR + have hb : r.val < (replyLinkAddr side r).val ∧ (replyLinkAddr side r).val < r.val + 32 := by + cases side <;> simp at ha <;> omega + obtain ⟨H', HE⟩ := writeLinkFrames (v := 0#u64) H P PE hR ha.1 hb.1 hb.2 + refine ⟨H', HE, fun b hb => ?_⟩ + rw [L.mem_wr _ _ _ _ ha.1 hb] + by_cases eq : replyLinkAddr side r = b + · simp [eq, hn] + · simp [eq, Ne.symm eq, hn] + +/-- The four link writes before `reply_unlink` in a non-head remove. -/ +def replySplicePrepareA (s : S) (r next prev : U64) : Result S := do + let s1 ← clearReplyFieldA (st := st) s false next + let s2 ← clearReplyFieldA (st := st) s1 true prev + let s3 ← obj.Reply.set_prev st r s2 0#u64 + obj.Reply.set_next st r s3 0#u64 false + +variable {A : Type} {ar : machine.Arch A} + +theorem replyRemoveHeadProgramA (s : S) (a : A) (r t sc : U64) + (hn : obj.Reply.next st r s = ok sc) (h0 : sc ≠ 0#u64) + (hh : headBit (L.mem s (rNextA r)) = true) : + ipc.reply_remove st ar s a r t = ipc.reply_pop st ar s a r t := by + unfold ipc.reply_remove + rw [hn, bind_ok, eq_of_spec_fd (obj.Reply.prev.spec s r), bind_ok, + if_pos (show (sc != 0#u64) = true by simpa using h0), + eq_of_spec_fd (obj.Reply.next_is_head.spec s r), bind_ok, hh, if_pos rfl] + +theorem replyRemoveSpliceProgramA (s : S) (a : A) (r t nx pv : U64) + (hn : obj.Reply.next st r s = ok nx) (hp : obj.Reply.prev st r s = ok pv) + (hh : headBit (L.mem s (rNextA r)) = false) : + ipc.reply_remove st ar s a r t = (do + let s1 ← replySplicePrepareA (st := st) s r nx pv + let s2 ← ipc.reply_unlink st s1 r t + ok (s2, a)) := by + unfold ipc.reply_remove + rw [hn, bind_ok, hp, bind_ok] + by_cases h : (nx != 0#u64) = true + · rw [if_pos h, eq_of_spec_fd (obj.Reply.next_is_head.spec s r), bind_ok, hh, + if_neg Bool.false_ne_true] + simp only [replySplicePrepareA, clearReplyFieldA, h, Bool.false_eq_true, ↓reduceIte, Std.bind_assoc] + · rw [if_neg h] + simp only [replySplicePrepareA, clearReplyFieldA, h, Bool.false_eq_true, ↓reduceIte, Std.bind_assoc] + +/-- A splice removes each neighbouring link and clears the removed reply. -/ +theorem replySplicePrepareRefA {s : S} {σ : State Unit} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + {r next prev : U64} (hr : r ∈ R) (hn : next ≠ 0#u64 → next ∈ R) (hp : prev ≠ 0#u64 → prev ∈ R) : + replySplicePrepareA (st := st) s r next prev ⦃ s' => + SchedSRA st Z m κ s' σ ∧ EpStateRel Z.T E R Bf (L.mem s') σ ∧ + (∀ d ∈ R, d ≠ r → d ≠ next → L.mem s' (rPrevA d) = L.mem s (rPrevA d)) ∧ + (∀ d ∈ R, d ≠ r → d ≠ prev → L.mem s' (rNextA d) = L.mem s (rNextA d)) ∧ + (next ≠ 0#u64 → L.mem s' (rPrevA next) = 0#u64) ∧ + (prev ≠ 0#u64 → L.mem s' (rNextA prev) = 0#u64) ∧ + L.mem s' (rPrevA r) = 0#u64 ∧ L.mem s' (rNextA r) = 0#u64 ⦄ := by + have hr0 : r ≠ 0#u64 := fun e => (PE.rep r hr).2.1 (by rw [e]; rfl) + unfold replySplicePrepareA + refine WP.spec_bind (clearReplyFieldRefA H P PE false next hn) ?_ + rintro s1 ⟨H1, E1, m1⟩ + refine WP.spec_bind (clearReplyFieldRefA H1 P PE true prev hp) ?_ + rintro s2 ⟨H2, E2, m2⟩ + have ep s0 : clearReplyFieldA (st := st) s0 false r = obj.Reply.set_prev st r s0 0#u64 := by + unfold clearReplyFieldA + rw [show (r != 0#u64) = true by simpa using hr0] + rfl + have en s0 : clearReplyFieldA (st := st) s0 true r = obj.Reply.set_next st r s0 0#u64 false := by + unfold clearReplyFieldA + rw [show (r != 0#u64) = true by simpa using hr0] + rfl + rw [← ep s2] + refine WP.spec_bind (clearReplyFieldRefA H2 P PE false r (fun _ => hr)) ?_ + rintro s3 ⟨H3, E3, m3⟩ + rw [← en s3] + refine WP.spec_mono (clearReplyFieldRefA H3 P PE true r (fun _ => hr)) ?_ + rintro s4 ⟨H4, E4, m4⟩ + have epn (d : U64) (hd : d ∈ R) (n : U64) (hN : n ≠ 0#u64 → n ∈ R) : + ¬ (n ≠ 0#u64 ∧ rPrevA d = replyLinkAddr true n) := + fun e => (by decide : false ≠ true) ((replyLinkAddrEq H.toSchedSRN P PE hd (hN e.1) false true).1 e.2).1 + have enp (d : U64) (hd : d ∈ R) (n : U64) (hN : n ≠ 0#u64 → n ∈ R) : + ¬ (n ≠ 0#u64 ∧ rNextA d = replyLinkAddr false n) := + fun e => (by decide : true ≠ false) ((replyLinkAddrEq H.toSchedSRN P PE hd (hN e.1) true false).1 e.2).1 + have epp (d : U64) (hd : d ∈ R) (n : U64) (hN : n ≠ 0#u64 → n ∈ R) (hne : d ≠ n) : + ¬ (n ≠ 0#u64 ∧ rPrevA d = replyLinkAddr false n) := + fun e => hne ((replyLinkAddrEq H.toSchedSRN P PE hd (hN e.1) false false).1 e.2).2 + have enn (d : U64) (hd : d ∈ R) (n : U64) (hN : n ≠ 0#u64 → n ∈ R) (hne : d ≠ n) : + ¬ (n ≠ 0#u64 ∧ rNextA d = replyLinkAddr true n) := + fun e => hne ((replyLinkAddrEq H.toSchedSRN P PE hd (hN e.1) true true).1 e.2).2 + refine ⟨H4, HE.frame PE (E1.trans (E2.trans (E3.trans E4))) (KView.refl _), ?_, ?_, ?_, ?_, ?_, ?_⟩ + · intro d hd hd1 hd2 + have ha := (replyPrevPlaceA (Z := Z) PE hd).1 + rw [m4 _ ha, if_neg (epn d hd r (fun _ => hr)), m3 _ ha, if_neg (epp d hd r (fun _ => hr) hd1), + m2 _ ha, if_neg (epn d hd prev hp), m1 _ ha, if_neg (epp d hd next hn hd2)] + · intro d hd hd1 hd2 + have ha := (replyNextPlaceA (Z := Z) PE hd).1 + rw [m4 _ ha, if_neg (enn d hd r (fun _ => hr) hd1), m3 _ ha, if_neg (enp d hd r (fun _ => hr)), + m2 _ ha, if_neg (enn d hd prev hp hd2), m1 _ ha, if_neg (enp d hd next hn)] + · intro h + have ha := (replyPrevPlaceA (Z := Z) PE (hn h)).1 + rw [m4 _ ha, if_neg (epn next (hn h) r (fun _ => hr)), m3 _ ha] + by_cases e : next = r + · rw [e, if_pos ⟨hr0, rfl⟩] + · rw [if_neg (epp next (hn h) r (fun _ => hr) e), m2 _ ha, if_neg (epn next (hn h) prev hp), + m1 _ ha, if_pos ⟨h, rfl⟩] + · intro h + have ha := (replyNextPlaceA (Z := Z) PE (hp h)).1 + rw [m4 _ ha] + by_cases e : prev = r + · rw [e, if_pos ⟨hr0, rfl⟩] + · rw [if_neg (enn prev (hp h) r (fun _ => hr) e), m3 _ ha, if_neg (enp prev (hp h) r (fun _ => hr)), + m2 _ ha, if_pos ⟨h, rfl⟩] + · have ha := (replyPrevPlaceA (Z := Z) PE hr).1 + rw [m4 _ ha, if_neg (epn r hr r (fun _ => hr)), m3 _ ha, if_pos ⟨hr0, rfl⟩] + · rw [m4 _ (replyNextPlaceA (Z := Z) PE hr).1, if_pos ⟨hr0, rfl⟩] + +end Store +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/IpcScRemoveTopA.lean b/proofs/Fl4ProofsA64/P3/IpcScRemoveTopA.lean new file mode 100644 index 000000000..c1cfe06fe --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/IpcScRemoveTopA.lean @@ -0,0 +1,100 @@ +/- +P3 (AArch64): full reply removal via a head pop, a live stack cut, or a detached splice. +[P] The caller supplies ScReplyCovA for the removed reply. The joint SC heap +coverage witness ScCovA implies it. [S] A head pop that returns an SC uses the +local donation scope of reply_pop_relA. +-/ +import Fl4ProofsA64.P3.IpcScRemoveCutA + +open Aeneas Aeneas.Std Result +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4SpecA64 Sched +noncomputable section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + +private theorem replyRemoveAtHeadA {s : S} {σ : State Unit} (a : A) {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {sc r t : U64} (hc : sc ∈ Z.C) (hsc0 : sc ≠ 0#u64) (ht : t ∈ Z.T) + {rest : List U64} (hl : stack sc = r :: rest) + (cov : ScReplyCovA Z.C m σ (specPtr r)) + {xt : Tcb} {xr : Reply} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hkr : σ.kheap (specPtr r) = some (.reply xr)) (hb : xr.replyTcb = some (specPtr t)) + (hst : xt.tcbState = .blockedOnReply (specPtr r) ∨ + ∃ ep d, xt.tcbState = .blockedOnReceive ep (some (specPtr r)) d) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) + (hlocal : xt.tcbSchedContext = none → ReplyPopLocalA (st := st) (Z := Z) (m := m) s σ sc t) + (hpost : ∀ σ', ((replyUnlinkSc (Z := Unit) (scNameA m sc) (specPtr r)) >>= fun _ => + schedContextDonate (scNameA m sc) (specPtr t)) σ = ({((), σ')}, False) → RelIfBoundSc σ' (specPtr t)) : + ipc.reply_remove st ar s a r t ⦃ out => out.2 = a ∧ ∃ σ', + replyRemove (Z := Unit) (specPtr t) (specPtr r) σ = ({((), σ')}, False) ∧ + IpcSchedRelA st Z m κ out.1 σ' ∧ EpStateRel Z.T E R Bf (L.mem out.1) σ' ∧ + ReplyStackRelA (L.mem out.1) R Z.C σ' (scNameA m) (Function.update stack sc rest) ∧ + ReplyIsolatedA (L.mem out.1) Z.C (Function.update stack sc rest) r ⦄ := by + obtain ⟨rwNext, decNext, _⟩ := replyHeadWordsA H PE HS hc hl + have nx := (eq_of_spec_fd (obj.Reply.next.spec (L := L) s r)).trans (congrArg ok decNext) + have hh := nx_head1 (L.mem s (rNextA r)) + rw [decNext, ← rwNext] at hh + rw [replyRemoveHeadProgramA (ar := ar) s a r t sc nx hsc0 hh] + obtain ⟨⟨x, n, hk, hL⟩, _⟩ := HS.stackRel sc hc + have hm : r ∈ stack sc := by simp [hl] + rw [replyRemoveSomeA hkr hb (HS.scWithReply_some hc hm cov) hk hkt, hL] + exact reply_pop_relA (ar := ar) a H P PE HE HS hc hsc0 ht hl hkt hkr hb hst hq hlocal hpost + +/-- Refine l4v reply_remove with the caller's [P] reply-specific SC coverage. +The head case takes the same [S] donation facts as reply_pop_relA. -/ +theorem reply_remove_relA {s : S} {σ : State Unit} (a : A) {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {r t : U64} (hr : r ∈ R) (ht : t ∈ Z.T) (cov : ScReplyCovA Z.C m σ (specPtr r)) + {xt : Tcb} {xr : Reply} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hkr : σ.kheap (specPtr r) = some (.reply xr)) (hb : xr.replyTcb = some (specPtr t)) + (hst : xt.tcbState = .blockedOnReply (specPtr r) ∨ + ∃ ep d, xt.tcbState = .blockedOnReceive ep (some (specPtr r)) d) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) + (hsc0 : ∀ sc ∈ Z.C, (stack sc).head? = some r → sc ≠ 0#u64) + (hlocal : ∀ sc ∈ Z.C, (stack sc).head? = some r → xt.tcbSchedContext = none → + ReplyPopLocalA (st := st) (Z := Z) (m := m) s σ sc t) + (hpost : ∀ sc ∈ Z.C, (stack sc).head? = some r → ∀ σ', + ((replyUnlinkSc (Z := Unit) (scNameA m sc) (specPtr r)) >>= fun _ => + schedContextDonate (scNameA m sc) (specPtr t)) σ = ({((), σ')}, False) → RelIfBoundSc σ' (specPtr t)) : + ipc.reply_remove st ar s a r t ⦃ out => out.2 = a ∧ ∃ σ' stack', + replyRemove (Z := Unit) (specPtr t) (specPtr r) σ = ({((), σ')}, False) ∧ + IpcSchedRelA st Z m κ out.1 σ' ∧ EpStateRel Z.T E R Bf (L.mem out.1) σ' ∧ + ReplyStackRelA (L.mem out.1) R Z.C σ' (scNameA m) stack' ∧ + ReplyIsolatedA (L.mem out.1) Z.C stack' r ⦄ := by + classical + by_cases hd : ∀ sc ∈ Z.C, r ∉ stack sc + · refine WP.spec_mono (replyRemoveDetachedRefA (ar := ar) a ⟨Z.Q, Z.R, H⟩ P PE HE HS hr ht hd + cov hkt hkr hb hst hq) ?_ + rintro out ⟨ha, σ', hu, hS, hE, hR, hi⟩ + exact ⟨ha, σ', stack, hu, hS, hE, hR, hi⟩ + · have hLive : ∃ sc ∈ Z.C, r ∈ stack sc := by push_neg at hd; exact hd + obtain ⟨sc, hc, hm⟩ := hLive + obtain ⟨k, hk, he⟩ := List.getElem_of_mem hm + cases k with + | zero => + have hh : (stack sc).head? = some r := by + rw [List.head?_eq_getElem?, List.getElem?_eq_getElem hk, he] + obtain ⟨rest, hl⟩ : ∃ rest, stack sc = r :: rest := by + cases ee : stack sc with + | nil => rw [ee] at hh; cases hh + | cons d l => + rw [ee] at hh + cases hh + exact ⟨l, rfl⟩ + refine WP.spec_mono (replyRemoveAtHeadA (ar := ar) a H P PE HE HS hc (hsc0 sc hc hh) ht hl cov + hkt hkr hb hst hq (hlocal sc hc hh) (hpost sc hc hh)) ?_ + rintro out ⟨ha, σ', hu, hS, hE, hR, hi⟩ + exact ⟨ha, σ', Function.update stack sc rest, hu, hS, hE, hR, hi⟩ + | succ k => + refine WP.spec_mono (replyRemoveCutRefA (ar := ar) a ⟨Z.Q, Z.R, H⟩ P PE HE HS hc ht hk he cov + hkt hkr hb hst hq) ?_ + rintro out ⟨ha, σ', hu, hS, hE, hR, hi⟩ + exact ⟨ha, σ', Function.update stack sc ((stack sc).take (k + 1)), hu, hS, hE, hR, hi⟩ + +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/IpcScReplyA.lean b/proofs/Fl4ProofsA64/P3/IpcScReplyA.lean new file mode 100644 index 000000000..705b17b58 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/IpcScReplyA.lean @@ -0,0 +1,160 @@ +/- +P3 (AArch64): reply-stack premises at the IPC and MCS join. + +IPC supplies the links and reply references. MCS supplies the SC name and owns +the SC's REPLY word. The SC unlink leaves the caller's TCB available for the +donation test. No named premises. +-/ +import Fl4ProofsA64.P3.EpReplyStackA +import Fl4ProofsA64.P3.ScPopA +import Fl4ProofsA64.P1.QueueIpc + +open Aeneas Aeneas.Std + +namespace Fl4ProofsA64.P3.Endp +open Fl4ProofsA64.P1 Fl4SpecA64 Sched + +/-- Supply the reply heap object and the four list and ownership facts used by `reply_pop_scA`. -/ +theorem ReplyStackRelA.popFacts {T R C E : Set U64} {Bf : Set (U64 × Word)} {mem : U64 → U64} {σ : State Unit} + {scRef : U64 → ObjRef} {stack : U64 → List U64} {sc r : U64} + (hP : EPlaced T E R Bf) (hEp : EpStateRel T E R Bf mem σ) + (h : ReplyStackRelA mem R C σ scRef stack) (hc : sc ∈ C) (hh : (stack sc).head? = some r) : + ∃ xr, σ.kheap (specPtr r) = some (.reply xr) ∧ xr.replySc = some (scRef sc) ∧ + (∀ x n, σ.kheap (scRef sc) = some (.schedContext x n) → x.scReplies.head? = some (specPtr r)) ∧ + (∀ x n, σ.kheap (scRef sc) = some (.schedContext x n) → + x.scReplies.tail.head? = optRefS (mem (rPrevA r) &&& ~~~1#u64)) ∧ + (∀ x n p, σ.kheap (scRef sc) = some (.schedContext x n) → x.scReplies.tail.head? = some p → + ∃ xp, σ.kheap p = some (.reply xp)) := by + obtain ⟨⟨x, n, hk, hL⟩, hmem, _, hwords⟩ := h.stackRel sc hc + obtain ⟨rs, hr⟩ : ∃ rs, stack sc = r :: rs := by + cases e : stack sc with + | nil => simp [e] at hh + | cons a tail => + simp [e] at hh + exact ⟨tail, by rw [hh]⟩ + have hrR : r ∈ R := hmem r (by simp [hr]) + obtain ⟨xr, hkr, _⟩ := hEp.reply r hrR + have hList : ∀ y k, σ.kheap (scRef sc) = some (.schedContext y k) → y.scReplies = specPtr r :: rs.map specPtr := by + intro y k hy + rw [hk] at hy + cases hy + simpa [hr] using hL + have hprev : mem (rPrevA r) = rs.headD 0#u64 := by + have hv := (hwords 0 (by simp [hr])).1 + cases rs <;> simpa [hr, List.headD] using hv + refine ⟨xr, hkr, (h.owner r hrR xr hkr _).2 ⟨sc, hc, rfl, hh⟩, ?_, ?_, ?_⟩ + · intro y k hy + rw [hList y k hy] + rfl + · intro y k hy + rw [hList y k hy, hprev] + cases rs with + | nil => simp [optRefS] + | cons p ps => + have hpR : p ∈ R := hmem p (by simp [hr]) + have ha := (hP.rep p hpR).1 + have hn : p ≠ 0#u64 := fun e => (hP.rep p hpR).2.1 (by rw [e]; rfl) + rw [List.headD_cons, mask1_of_even (by omega), optRefS_ne hn] + rfl + · intro y k p hy hp + rw [hList y k hy] at hp + cases rs with + | nil => simp at hp + | cons q qs => + have hqR : q ∈ R := hmem q (by simp [hr]) + have eq : specPtr q = p := by simpa using hp + obtain ⟨xp, hkp, _⟩ := hEp.reply q hqR + exact ⟨xp, eq ▸ hkp⟩ + +private theorem replyScKeepsTcb {σ : State Unit} {r t : ObjRef} {xt : Tcb} + (ht : σ.kheap t = some (.tcb xt)) (v : Option ObjRef) : + (rscA σ r v).kheap t = some (.tcb xt) := by + rcases rscA_kheap σ r v t with h | ⟨rfl, x, hx, _⟩ + · exact h.trans ht + · rw [ht] at hx + cases hx + +/-- Unlinking the SC's reply head preserves the caller's TCB. -/ +theorem popStA_keepsTcb {σ : State Unit} {sc r t : ObjRef} {xs : SchedContext} {n : Nat} {xt : Tcb} + (hs : σ.kheap sc = some (.schedContext xs n)) (ht : σ.kheap t = some (.tcb xt)) : + (popStA σ sc xs n r).kheap t = some (.tcb xt) := by + have hne : t ≠ sc := by intro e; rw [e, hs] at ht; cases ht + have ht' : (repStA σ sc xs n xs.scReplies.tail).kheap t = some (.tcb xt) := (repStA_ne hne).trans ht + apply replyScKeepsTcb + cases e : xs.scReplies.tail with + | nil => simpa only [e, fixHdA_nil] using ht' + | cons h _ => simpa only [e, fixHdA_cons] using replyScKeepsTcb (r := h) ht' (some sc) + +/-- The spec's pre-unlink caller-SC test reads the same value at the post-unlink state. -/ +theorem callerScAfterUnlinkA {σ : State Unit} {sc r t : ObjRef} {xs : SchedContext} {n : Nat} {xt : Tcb} + (hs : σ.kheap sc = some (.schedContext xs n)) (ht : σ.kheap t = some (.tcb xt)) : + getTcbObjRef Tcb.tcbSchedContext t (popStA σ sc xs n r) = + ({(xt.tcbSchedContext, popStA σ sc xs n r)}, False) := + threadGet_eqSc _ (popStA_keepsTcb hs ht) + +/-- Every spec SC has a concrete name in the joint SC set. -/ +def ScCovA (C : Set U64) (m : McState) (σ : State Unit) : Prop := + ∀ p x n, σ.kheap p = some (.schedContext x n) → ∃ c ∈ C, p = scNameA m c + +/-- The coverage needed by `reply_remove` for the reply it removes. -/ +def ScReplyCovA (C : Set U64) (m : McState) (σ : State Unit) (r : ObjRef) : Prop := + ∀ p x n, σ.kheap p = some (.schedContext x n) → r ∈ x.scReplies → ∃ c ∈ C, p = scNameA m c + +theorem ScCovA.reply {C : Set U64} {m : McState} {σ : State Unit} (h : ScCovA C m σ) (r : ObjRef) : + ScReplyCovA C m σ r := fun p x n hk _ => h p x n hk + +private theorem scWithReplyOfUnique {σ : State Unit} {r p : ObjRef} + (h : ∀ q, (∃ y n, σ.kheap q = some (.schedContext y n) ∧ r ∈ y.scReplies) ↔ q = p) : + scWithReply r σ = some p := by + classical + have hu : ∃! q, decide (∃ y n, σ.kheap q = some (.schedContext y n) ∧ r ∈ y.scReplies) = true := + ⟨p, decide_eq_true ((h p).2 rfl), fun q hq => (h q).1 (of_decide_eq_true hq)⟩ + unfold scWithReply partialInv + rw [if_pos hu] + congr 1 + apply (h _).1 + apply of_decide_eq_true + unfold The + rw [dif_pos hu] + exact (Classical.choose_spec hu).1 + +/-- Whole-heap lookup of a reply on a tracked SC, using the caller's coverage. -/ +theorem ReplyStackRelA.scWithReply_some {R C : Set U64} {mem : U64 → U64} {σ : State Unit} + {m : McState} {stack : U64 → List U64} (H : ReplyStackRelA mem R C σ (scNameA m) stack) + {sc r : U64} (hc : sc ∈ C) (hr : r ∈ stack sc) (cov : ScReplyCovA C m σ (specPtr r)) : + scWithReply (specPtr r) σ = some (scNameA m sc) := by + apply scWithReplyOfUnique + intro p + constructor + · rintro ⟨x, n, hk, hx⟩ + obtain ⟨q, hq, rfl⟩ := cov p x n hk hx + obtain ⟨⟨y, k, hy, hl⟩, _⟩ := H.stackRel q hq + rw [hk] at hy + cases hy + rw [hl, List.mem_map] at hx + obtain ⟨d, hd, hEq⟩ := hx + have e := H.unique q hq sc hc r (specPtr_inj hEq ▸ hd) hr + rw [e] + · rintro rfl + obtain ⟨⟨x, n, hk, hl⟩, _⟩ := H.stackRel sc hc + exact ⟨x, n, hk, by rw [hl]; exact List.mem_map_of_mem hr⟩ + +/-- The whole-heap search returns none for a detached reply. -/ +theorem ReplyStackRelA.scWithReply_none {R C : Set U64} {mem : U64 → U64} {σ : State Unit} + {m : McState} {stack : U64 → List U64} (H : ReplyStackRelA mem R C σ (scNameA m) stack) + {r : U64} (hr : ∀ sc ∈ C, r ∉ stack sc) (cov : ScReplyCovA C m σ (specPtr r)) : + scWithReply (specPtr r) σ = none := by + classical + unfold scWithReply partialInv + rw [if_neg] + rintro ⟨p, hp, _⟩ + obtain ⟨x, n, hk, hx⟩ := of_decide_eq_true hp + obtain ⟨c, hc, rfl⟩ := cov p x n hk hx + obtain ⟨⟨y, k, hy, hl⟩, _⟩ := H.stackRel c hc + rw [hk] at hy + cases hy + rw [hl, List.mem_map] at hx + obtain ⟨d, hd, hEq⟩ := hx + exact hr c hc (specPtr_inj hEq ▸ hd) + +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/IpcScReplyBodyA.lean b/proofs/Fl4ProofsA64/P3/IpcScReplyBodyA.lean new file mode 100644 index 000000000..85ef50f1c --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/IpcScReplyBodyA.lean @@ -0,0 +1,109 @@ +/- +P3 (AArch64): normal and fault Reply bodies. +[J] Message/register transfer and scheduler entry relations are supplied at +their exact states, including the sender's native IPC buffer and the +receiver's SC backlink. The original fault remains available to the caller. +-/ +import Fl4ProofsA64.P3.IpcScReplyTransferA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4Spec Fl4SpecA64 Sched Notif + +noncomputable section +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +/-- The normal Reply transfers a message first and then makes the receiver +Running. [J] The message step supplies the receiver's actual successor +and the local scheduler step uses that state. -/ +theorem replyNormalBody_refinesA {s : S} {a : A} {σ : State Unit} {sender t : U64} + {grant : Bool} {x y : Tcb} + (hkx : σ.kheap (specPtr sender) = some (.tcb x)) (hky : σ.kheap (specPtr t) = some (.tcb y)) + (hfx : x.tcbFault = none) {M : S → State Unit → Prop} {R : S → A → State Unit → Prop} + (transfer : ipc.do_ipc_transfer st ar s a sender 0#u64 0#u64 grant t ⦃ s1 => + ∃ σ1, ((), σ1) ∈ (doIpcTransfer (Z := Unit) (specPtr sender) none 0 grant (specPtr t) σ).1 ∧ + M s1 σ1 ⦄) + (run : ∀ s1 σ1, M s1 σ1 → sched.set_thread_state st s1 t sched.TS_RUNNING ⦃ s2 => + ∃ σ2, ((), σ2) ∈ (setThreadState (Z := Unit) (specPtr t) .running σ1).1 ∧ R s2 a σ2 ⦄) : + replyBodyProgramA st ar s a sender t fl4_abi.consts.fault.NullFault grant ⦃ r => + ∃ σ2, ((), σ2) ∈ ((fl4DoIpcTransfer (Z := Unit) + (specPtr sender) none 0 grant (specPtr t) >>= fun _ => setThreadState (specPtr t) .running) σ).1 ∧ + R r.1 r.2 σ2 ⦄ := by + unfold replyBodyProgramA + rw [if_pos rfl] + refine WP.spec_bind transfer ?_ + rintro s1 ⟨σ1, h1, HM⟩ + refine WP.spec_bind (run s1 σ1 HM) ?_ + rintro s2 ⟨σ2, h2, HR⟩ + rw [WP.spec_ok] + have h1' : ((), σ1) ∈ (fl4DoIpcTransfer (Z := Unit) (specPtr sender) none 0 grant (specPtr t) σ).1 := by + rw [fl4ReplyNormalTransferA hkx hky hfx] + exact h1 + exact ⟨σ2, mem_bind_ndD h1' h2, HR⟩ + +private theorem replyFaultBody_memA {σ σ1 σ2 : State Unit} {sender t : ObjRef} + {fault : Fault} {restart : Bool} {x : Tcb} + (hk : σ1.kheap t = some (.tcb x)) + (payload : (restart, σ1) ∈ ((do + let mi ← getMessageInfo (Z := Unit) sender + let buf ← lookupIpcBuffer false sender + let mrs ← getMrs sender buf mi + fl4HandleFaultReply fault t mi.miLabel mrs) σ).1) + (run : ((), σ2) ∈ (setThreadState (Z := Unit) t (if restart then .restart else .inactive) + (updKSc σ1 t (.tcb { x with tcbFault := none }))).1) : + ((), σ2) ∈ (replyFaultBodyA sender t fault σ).1 := by + have h := mem_bind_ndD (f := fun b => do + threadSet (fun tcb => { tcb with tcbFault := none }) t + setThreadState t (if b then .restart else .inactive)) payload (by + rw [nd_bind_oneSc (threadSet_eqSc _ hk)] + exact run) + simpa only [replyFaultBodyA, nd_bind_assocSc, reply_bind_assocA] using h + +/-- The fault Reply applies the chosen payload law, clears the fault, +then sets Restart or Inactive using the handler's result. [J] The exact +payload and final scheduler steps carry their intermediate relations. -/ +theorem replyFaultBody_refinesA {s : S} {a : A} {σ : State Unit} {sender t ft : U64} + {grant : Bool} {fault : Fault} (hft : ft ≠ fl4_abi.consts.fault.NullFault) + {M : S → A → State Unit → Prop} {R : S → A → State Unit → Prop} + (payload : faults.handle_fault_reply st ar s a t sender ⦃ r => + ∃ σ1 x, σ1.kheap (specPtr t) = some (.tcb x) ∧ M r.2.1 r.2.2 σ1 ∧ + (r.1, σ1) ∈ ((do + let mi ← getMessageInfo (Z := Unit) (specPtr sender) + let buf ← lookupIpcBuffer false (specPtr sender) + let mrs ← getMrs (specPtr sender) buf mi + fl4HandleFaultReply fault (specPtr t) mi.miLabel mrs) σ).1 ⦄) + (run : ∀ (s1 : S) (a1 : A) σ1 x (restart : Bool), M s1 a1 σ1 → + σ1.kheap (specPtr t) = some (.tcb x) → + sched.set_thread_state st (L.wr s1 (faultA t) fl4_abi.consts.fault.NullFault) t + (if restart then sched.TS_RESTART else sched.TS_INACTIVE) ⦃ s2 => ∃ σ2, + ((), σ2) ∈ (setThreadState (Z := Unit) (specPtr t) (if restart then .restart else .inactive) + (updKSc σ1 (specPtr t) (.tcb { x with tcbFault := none }))).1 ∧ R s2 a1 σ2 ⦄) : + replyBodyProgramA st ar s a sender t ft grant ⦃ r => ∃ σ2, + ((), σ2) ∈ (replyFaultBodyA (specPtr sender) (specPtr t) fault σ).1 ∧ R r.1 r.2 σ2 ⦄ := by + unfold replyBodyProgramA + rw [if_neg hft] + refine WP.spec_bind payload ?_ + rintro ⟨restart, s1, a1⟩ ⟨σ1, x, hk, HM, h1⟩ + have hclear : faults.clear_fault st s1 t = ok (L.wr s1 (faultA t) fl4_abi.consts.fault.NullFault) := by + unfold faults.clear_fault + exact eq_of_spec_fd (P1.obj.Tcb.set_fault_type.spec _ _ _) + change (do + let s2 ← faults.clear_fault st s1 t + if restart then + let s3 ← sched.set_thread_state st s2 t sched.TS_RESTART + ok (s3, a1) + else + let s3 ← sched.set_thread_state st s2 t sched.TS_INACTIVE + ok (s3, a1)) ⦃ r => ∃ σ2, + ((), σ2) ∈ (replyFaultBodyA (specPtr sender) (specPtr t) fault σ).1 ∧ R r.1 r.2 σ2 ⦄ + rw [hclear, bind_ok] + have hr := run s1 a1 σ1 x restart HM hk + cases restart <;> simp only [Bool.false_eq_true, ↓reduceIte] at hr ⊢ <;> + (refine WP.spec_bind hr ?_; rintro s2 ⟨σ2, h2, HR⟩; rw [WP.spec_ok] + exact ⟨σ2, replyFaultBody_memA hk h1 h2, HR⟩) + +end +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/IpcScReplyTimeoutA.lean b/proofs/Fl4ProofsA64/P3/IpcScReplyTimeoutA.lean new file mode 100644 index 000000000..8fe37202c --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/IpcScReplyTimeoutA.lean @@ -0,0 +1,150 @@ +/- +P3 (AArch64): the live Reply timeout update and send. +[J] The local FL4 fault-send continuation uses the exact timeout successor +and the row-12 payload computation. +-/ +import Fl4ProofsA64.P3.IpcScReplyTransferA +import Fl4ProofsA64.P3.LocalInvokePostA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4Spec Fl4SpecA64 Sched + +noncomputable section + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +variable (st) in +/-- The four stores in the timeout branch of `faults.set_fault`. -/ +def timeoutFaultWordsA (s : S) (t badge : U64) : S := + L.wr (L.wr (L.wr (L.wr s (faultA t) fl4_abi.consts.fault.Timeout) + (tFw0A t) badge) (tFw1A t) 0#u64) (tFw2A t) 0#u64 + +theorem setTimeoutFault_exactA (s : S) (t badge : U64) : + faults.set_fault st s t (.Timeout badge) = ok (timeoutFaultWordsA st s t badge) := by + unfold faults.set_fault timeoutFaultWordsA + simp only [bind_ok, uncurry_apply_pair] + rw [eq_of_spec_fd (P1.obj.Tcb.set_fault_type.spec (st := st) s t _), bind_ok] + rw [eq_of_spec_fd (P1.obj.Tcb.set_fault_w0.wr (st := st) _ t badge), bind_ok] + rw [eq_of_spec_fd (P1.obj.Tcb.set_fault_w1.wr (st := st) _ t 0#u64), bind_ok] + exact eq_of_spec_fd (P1.obj.Tcb.set_fault_w2.wr _ t 0#u64) + +private theorem handleTimeout_memA {σ σ' : State Unit} {t ep b : ObjRef} {badge : Badge} + {x : Tcb} {send recv grant grantReply : Bool} + (hk : σ.kheap t = some (.tcb x)) + (hc : getCap (Z := Unit) (getTcbTimeoutHandlerPtr t) σ = + ({(.endpointCap ep b (rightsOfA send recv grant grantReply), σ)}, False)) + (hs : ((), σ') ∈ (fl4SendIpc (Z := Unit) true false b grant grantReply false t ep + (updKSc σ t (.tcb { x with tcbFault := some (.timeout badge) }))).1) : + ((), σ') ∈ (fl4HandleTimeout (Z := Unit) t (.timeout badge) σ).1 := by + have valid : isValidTimeoutHandler (Z := Unit) t σ = ({(true, σ)}, False) := by + unfold isValidTimeoutHandler + rw [nd_pure_bindSc, nd_bind_oneSc hc] + rfl + unfold fl4HandleTimeout + rw [nd_bind_oneSc valid, nd_bind_oneSc (assert_trueSc (show true = true from rfl) σ), + nd_pure_bindSc, nd_bind_oneSc hc] + have hg (d : Decidable (Rights.allowGrant ∈ rightsOfA send recv grant grantReply)) : + @decide _ d = grant := by + apply Bool.eq_iff_iff.2 + simp only [decide_eq_true_eq] + rfl + have hgr (d : Decidable (Rights.allowGrantReply ∈ rightsOfA send recv grant grantReply)) : + @decide _ d = grantReply := by + apply Bool.eq_iff_iff.2 + simp only [decide_eq_true_eq] + rfl + simp only [fl4SendFaultIpc, capEpBadge, capEpPtr, hg, hgr, nd_bind_assocSc] + rw [nd_bind_oneSc (threadSet_eqSc _ hk)] + exact mem_bind_ndD (mem_bind_ndD (y := (true, σ')) hs rfl) rfl + +/-- The live timeout arm reads the endpoint cap and installs its fault before +sending. [J] The local endpoint send supplies the precise FL4 successor, +including message delivery through the row-12 consumed-time operation. -/ +theorem replyHandleTimeout_refinesA {s : S} {a : A} {σ : State Unit} {t faultBadge ep handlerBadge slot : U64} + {send recv grant grantReply : Bool} {x : Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hslot : obj.Tcb.slot t layout.tcb.SLOT_TIMEOUT_HANDLER = ok slot) + (hc : obj.Slot.cap st slot s = ok (.Endpoint ep handlerBadge send recv grant grantReply)) + (ha : getCap (Z := Unit) (getTcbTimeoutHandlerPtr (specPtr t)) σ = + ({(.endpointCap (specPtr ep) handlerBadge.bv (rightsOfA send recv grant grantReply), σ)}, False)) + {R : S → A → State Unit → Prop} + (deliver : endpoint.send_ipc st ar (timeoutFaultWordsA st s t faultBadge) a true false handlerBadge + grant grantReply false t ep ⦃ r => ∃ σ', + ((), σ') ∈ (fl4SendIpc (Z := Unit) true false handlerBadge.bv grant grantReply false (specPtr t) (specPtr ep) + (updKSc σ (specPtr t) (.tcb { x with tcbFault := some (.timeout faultBadge.bv) }))).1 ∧ R r.1 r.2 σ' ⦄) : + faults.handle_timeout st ar s a t (.Timeout faultBadge) ⦃ r => ∃ σ', + ((), σ') ∈ (fl4HandleTimeout (Z := Unit) (specPtr t) (.timeout faultBadge.bv) σ).1 ∧ R r.1 r.2 σ' ⦄ := by + unfold faults.handle_timeout faults.send_fault_ipc + simp only [hslot, bind_ok, hc, setTimeoutFault_exactA, uncurry_apply_pair, Std.bind_assoc] + refine WP.spec_bind deliver ?_ + rintro ⟨s', a'⟩ ⟨σ', hm, hR⟩ + have hpost : ∃ σ', ((), σ') ∈ (fl4HandleTimeout (Z := Unit) (specPtr t) (.timeout faultBadge.bv) σ).1 ∧ + R s' a' σ' := ⟨σ', handleTimeout_memA hk ha hm, hR⟩ + simpa only [bind_ok, uncurry_apply_pair, WP.spec_ok] using hpost + +/-- Write the timeout badge and the FL4 consumed-time word to the +receiver's message. [J] The message law is for the concrete state after +the SC update and carries the receiver register or IPC buffer frame. -/ +theorem replyTimeoutMrs_refinesA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {a : A} {σ : State Unit} (H : SchedSRA st Z m κ s σ) + {sender receiver buf : U64} (hs : sender ∈ Z.T) + (hsc : L.mem s (scA sender) ≠ 0#u64) (htu : Fl4TicksToUsAgreeA ar a) + (hty : L.mem s (tFaultA sender) = 5#u64) + {badge : Badge} (hb : (L.mem s (tFw0A sender)).bv = badge) + {Bf : Set (U64 × Word)} {g : Nat → Nat} (hg : g 1 = 1) + {M : S → State Unit → S → List Word → Prop} + (message : ∀ s1 σ1 consumed, + fl4MakeFaultMsg (Z := Unit) (.timeout badge) (specPtr sender) σ = + ({((5, [badge, consumed]), σ1)}, False) → + SchedSRA st Z m κ s1 σ1 → + MsgLawA st Bf (M s1 σ1) receiver buf g s1) : + faults.set_mrs_fault st ar s a sender receiver buf ⦃ r => ∃ s1 σ1 consumed, + fl4MakeFaultMsg (Z := Unit) (.timeout badge) (specPtr sender) σ = + ({((5, [badge, consumed]), σ1)}, False) ∧ SchedSRA st Z m κ s1 σ1 ∧ + r.1.val = g 2 ∧ M s1 σ1 r.2 [badge, consumed] ⦄ := by + refine set_mrs_fault_tmo_specA s a hty (fun h => absurd h hsc) (fun _ => ?_) + refine WP.spec_bind (fl4ReplyTimeoutConsumedA H hs hsc htu badge) ?_ + rintro ⟨cv, s1⟩ ⟨σ1, hmsg, HS, -⟩ + have hmsg' : fl4MakeFaultMsg (Z := Unit) (.timeout badge) (specPtr sender) σ = + ({((5, [badge, cv.bv]), σ1)}, False) := by + simpa [wordsFromTime, ucast] using hmsg + have hM := message s1 σ1 cv.bv hmsg' HS + refine WP.spec_bind (hM.set _ _ hM.start 0#u64 rfl (by decide) (L.mem s (tFw0A sender))) ?_ + rintro ⟨len, s2⟩ ⟨hl, hi⟩ + have hl' : len.val = 1 := hl.trans hg + refine WP.spec_mono (hM.set _ _ hi len (by rw [hl']; rfl) (by rw [hl']; decide) cv) ?_ + rintro ⟨k, s3⟩ ⟨hk, hi'⟩ + refine ⟨s1, σ1, cv.bv, hmsg', HS, ?_, ?_⟩ + · rw [hk, hl'] + · rw [hb] at hi' + exact hi' + +/-- The Reply dispatch post after the actual Restart, including a live +Reply transfer or the detached identity arm. [J] The chosen transfer +supplies its successor and writes at that Restart state. -/ +theorem reply_local_performA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {a : A} + {σ : State Unit} (H : SchedSRA st Z m κ s σ) (hst : StEqA Z.T (L.mem s) σ) + {x : Tcb} (hx : σ.kheap σ.curThread = some (.tcb x)) (hr : runnable x.tcbState = true) + {lbl len src buf reply : U64} {grant block call donate first : Bool} + {extra : Array obj.Slot 3#usize} {R : S → A → State Unit → Prop} + (step : ∀ s1 σ1, SchedSRA st Z m κ s1 σ1 → StEqA Z.T (L.mem s1) σ1 → + σ1.kheap = (updKSc σ (specPtr (L.curW s)) (.tcb { x with tcbState := .restart })).kheap → + WritesIn st s s1 (fun b => SWrA (st := st) Z s b ∨ b = stA (L.curW s)) → + ipc.do_reply_transfer st ar s1 a (L.curW s) reply grant ⦃ r => + ∃ σ2, ((), σ2) ∈ ((NonDet.gets (fun s : State Unit => s.curThread) >>= fun sender => + fl4DoReplyTransfer sender (specPtr reply) grant) σ1).1 ∧ R r.1 r.2 σ2 ⦄) : + syscall.decode_invocation st ar s a lbl len src (.Reply reply grant) block call donate first buf extra + ⦃ LocalInvokePostA R block call donate (.invokeReply (specPtr reply) grant) σ ⦄ := by + rw [decodeReply_programA] + refine WP.spec_mono (ipcInvokeProgram_refinesA H hst H.curT H.sch.curThread hx hr step) ?_ + rintro r ⟨he, σ1, σ2, h1, h2, hR⟩ + refine ⟨he, σ1, σ2, [], h1, ?_, hR⟩ + apply mem_liftED + rw [nd_gets_bindSc] at h2 ⊢ + exact mem_bind_ndD h2 rfl + +end +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/IpcScReplyTransferA.lean b/proofs/Fl4ProofsA64/P3/IpcScReplyTransferA.lean new file mode 100644 index 000000000..2dfa95bdb --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/IpcScReplyTransferA.lean @@ -0,0 +1,516 @@ +/- +P3 (AArch64): live Reply dispatch and its SC budget test. +The budget decision and timeout payload use MCS's FL4 extension. +[S] A local switch or postpone uses the receiver's affinity and the SC's core. +[P] The concrete minimum budget is the shared FL4 platform value. +-/ +import Fl4ProofsA64.P3.IpcScRemoveTopA +import Fl4ProofsA64.P3.ScConsA +import Fl4ProofsA64.P3.ScRefillA +import Fl4ProofsA64.P3.ScSetStateA +import Fl4ProofsA64.P3.DelSpecA +import Fl4SpecA64.Ext.Reply + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Sched Notif Fl4Spec Fl4SpecA64 + +noncomputable section + +theorem fl4ReplyDetachedA {σ : State Unit} {t r : ObjRef} {grant : Bool} {x : Reply} + (hk : σ.kheap r = some (.reply x)) (h : x.replyTcb = none) : + fl4DoReplyTransfer (Z := Unit) t r grant σ = ({((), σ)}, False) := by + unfold fl4DoReplyTransfer + rw [nd_bind_oneSc (getReplyTcb_eq hk), h] + rfl + +/-- The fault-reply payload and state transition before the SC tail. -/ +def replyFaultBodyA (sender receiver : ObjRef) (fault : Fault) : SMonad Unit Unit := do + let mi ← getMessageInfo sender + let buf ← lookupIpcBuffer false sender + let mrs ← getMrs sender buf mi + let restart ← fl4HandleFaultReply fault receiver mi.miLabel mrs + threadSet (fun tcb => { tcb with tcbFault := none }) receiver + setThreadState receiver (if restart then .restart else .inactive) + +/-- An ineligible refill either delivers a new timeout or postpones the SC. -/ +def replyLateSpecA (receiver : ObjRef) (fault : Option Fault) (sc : SchedContext) + (scPtr : ObjRef) : SMonad Unit Unit := do + let tcb ← NonDet.getsThe (getTcb receiver) + if isEpCap tcb.tcbTimeoutHandler ∧ ¬(fault.elim false isTimeoutFault = true) + then fl4HandleTimeout receiver (.timeout sc.scBadge) + else postpone scPtr + +/-- The FL4 SC tail uses the receiver's state after the normal or fault reply. -/ +def replyTailSpecA (receiver : ObjRef) (fault : Option Fault) : SMonad Unit Unit := do + let scOpt ← getTcbObjRef Tcb.tcbSchedContext receiver + let state ← getThreadState receiver + NonDet.when (scOpt ≠ none ∧ runnable state) <| do + let scPtr ← NonDet.assertOpt scOpt + let sc ← getSchedContext scPtr + let curtime ← NonDet.gets fun s : State Unit => s.curTime + if refillReady curtime (refillHd sc) ∧ fl4RefillSufficient 0 (refillHd sc) + then possibleSwitchTo receiver + else replyLateSpecA receiver fault sc scPtr + +/-- **The tail takes the timeout send** at `σ`: `t` is runnable with a bound SC whose head is not ready and +sufficient, its timeout handler is an endpoint cap and its original fault is not a timeout fault. -/ +def ReplyTmoCondA (σ : State Unit) (t : ObjRef) (fault : Option Fault) : Prop := + ∃ x p y k, σ.kheap t = some (.tcb x) ∧ x.tcbSchedContext = some p ∧ runnable x.tcbState = true ∧ + σ.kheap p = some (.schedContext y k) ∧ + ¬(refillReady σ.curTime (refillHd y) = true ∧ fl4RefillSufficient 0 (refillHd y) = true) ∧ + isEpCap x.tcbTimeoutHandler = true ∧ ¬(fault.elim false isTimeoutFault = true) + +theorem reply_bind_assocA {α β γ : Type} (m : SMonad Unit α) + (f : α → SMonad Unit β) (g : β → SMonad Unit γ) : + ((m >>= f) >>= g) = (m >>= fun a => f a >>= g) := by + funext s + exact nd_bind_assocSc m f g s + +/-- A live FL4 Reply outcome from the exact remove, refill, transfer and +scheduling successors. The original fault selects the late-refill branch. -/ +theorem fl4ReplyLive_memA {σ σ1 σ2 σ3 σ4 : State Unit} {sender r t : ObjRef} + {grant : Bool} {x : Reply} {y y2 : Tcb} + (hk : σ.kheap r = some (.reply x)) (ht : x.replyTcb = some t) + (hkt : σ.kheap t = some (.tcb y)) (hs : y.tcbState = .blockedOnReply r) + (h1 : replyRemove (Z := Unit) t r σ = ({((), σ1)}, False)) + (h2 : (getTcbObjRef (Z := Unit) Tcb.tcbSchedContext t >>= + ifSporadicCurScTestRefillUnblockCheck) σ1 = ({((), σ2)}, False)) + (hk2 : σ2.kheap t = some (.tcb y2)) + (h3 : (y2.tcbFault = none ∧ ((), σ3) ∈ ((fl4DoIpcTransfer (Z := Unit) + sender none 0 grant t >>= fun _ => setThreadState t .running) σ2).1) ∨ + ∃ f, y2.tcbFault = some f ∧ ((), σ3) ∈ (replyFaultBodyA sender t f σ2).1) + (h4 : ((), σ4) ∈ (replyTailSpecA t y2.tcbFault σ3).1) : + ((), σ4) ∈ (fl4DoReplyTransfer (Z := Unit) sender r grant σ).1 := by + unfold fl4DoReplyTransfer + rw [nd_bind_oneSc (getReplyTcb_eq hk), ht] + simp only [NonDet.maybeM] + rw [nd_bind_oneSc (getThreadState_eq hkt), hs] + dsimp only + rw [nd_bind_oneSc (assert_trueSc (show r = r from rfl) σ), nd_bind_oneSc h1] + rw [← nd_bind_assocSc (getTcbObjRef (Z := Unit) Tcb.tcbSchedContext t) + ifSporadicCurScTestRefillUnblockCheck, nd_bind_oneSc h2, nd_bind_oneSc (threadGet_eq _ hk2)] + rcases h3 with ⟨hf, hm⟩ | ⟨f, hf, hm⟩ + · rw [hf] at h4 ⊢ + have hu := mem_bind_ndD (f := fun _ => replyTailSpecA t none) hm h4 + rw [reply_bind_assocA] at hu + exact hu + · rw [hf] at h4 ⊢ + have hu := mem_bind_ndD (f := fun _ => replyTailSpecA t (some f)) hm h4 + simp only [replyFaultBodyA, reply_bind_assocA] at hu + exact hu + +theorem replyTailSpec_skipA {σ : State Unit} {t : ObjRef} {x : Tcb} + (hk : σ.kheap t = some (.tcb x)) + (h : x.tcbSchedContext = none ∨ runnable x.tcbState = false) (fault : Option Fault) : + replyTailSpecA t fault σ = ({((), σ)}, False) := by + unfold replyTailSpecA + rw [nd_bind_oneSc (getTcbObjRef_eq _ hk), nd_bind_oneSc (getThreadState_eq hk)] + unfold NonDet.when + rw [if_neg (by rcases h with h | h <;> simp [h])] + rfl + +theorem replyTailSpec_runA {σ : State Unit} {t sc : ObjRef} {x : Tcb} {y : SchedContext} {n : Nat} + (hk : σ.kheap t = some (.tcb x)) (hs : x.tcbSchedContext = some sc) + (hr : runnable x.tcbState = true) (hc : σ.kheap sc = some (.schedContext y n)) + (fault : Option Fault) : + replyTailSpecA t fault σ = + (if refillReady σ.curTime (refillHd y) ∧ fl4RefillSufficient 0 (refillHd y) + then possibleSwitchTo t else replyLateSpecA t fault y sc) σ := by + unfold replyTailSpecA + rw [nd_bind_oneSc (getTcbObjRef_eq _ hk), nd_bind_oneSc (getThreadState_eq hk)] + unfold NonDet.when + rw [if_pos (by simp [hs, hr]), hs] + change (NonDet.pure sc >>= _) σ = _ + rw [nd_bind_oneSc (show (NonDet.pure sc : SMonad Unit ObjRef) σ = ({(sc, σ)}, False) from rfl), + nd_bind_oneSc (Sched.getSchedContext_eqA hc), nd_gets_bindSc] + +/-- On the normal-message arm, use the existing IPC transfer proof with the +FL4 operation. No legacy timer agreement is needed for this selector. -/ +theorem fl4ReplyNormalTransferA {σ : State Unit} {sender receiver : ObjRef} {x y : Tcb} + (hx : σ.kheap sender = some (.tcb x)) (hy : σ.kheap receiver = some (.tcb y)) + (hf : x.tcbFault = none) (grant : Bool) : + fl4DoIpcTransfer (Z := Unit) sender none 0 grant receiver σ = + doIpcTransfer (Z := Unit) sender none 0 grant receiver σ := by + unfold fl4DoIpcTransfer doIpcTransfer + repeat rw [nd_bind_oneSc (lookupIpcBuffer_eqA hy true)] + repeat rw [nd_bind_oneSc (threadGet_eq _ hx)] + rw [hf] + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +/-- The detached slot has no backpointer. The concrete and spec transfers +both leave the state unchanged. -/ +theorem replyDetached_refinesA {s : S} {a : A} {σ : State Unit} {sender r : U64} {grant : Bool} + {x : Reply} (hk : σ.kheap (specPtr r) = some (.reply x)) (ha : x.replyTcb = none) + (hc : obj.Reply.tcb st r s = ok 0#u64) : + ipc.do_reply_transfer st ar s a sender r grant = ok (s, a) ∧ + fl4DoReplyTransfer (Z := Unit) (specPtr sender) (specPtr r) grant σ = ({((), σ)}, False) := by + refine ⟨?_, fl4ReplyDetachedA hk ha⟩ + unfold ipc.do_reply_transfer + rw [hc, bind_ok, eq_of_spec_fd (P1.obj.Tcb.is_null.spec _), bind_ok] + rfl + +/-- The CapFault reply does not copy registers and selects Restart. -/ +theorem replyHandleCapFaultA (s : S) (a : A) (sender receiver : U64) + (h : obj.Tcb.fault_type st receiver s = ok fl4_abi.consts.fault.CapFault) : + faults.handle_fault_reply st ar s a receiver sender ⦃ out => out = (true, s, a) ⦄ := by + unfold faults.handle_fault_reply + refine WP.spec_bind (P0.msg.msg_info.p0 st s sender L.total) ?_ + intro tag _ + simp only [h, bind_ok, if_pos rfl, if_true, WP.spec_ok] + +/-- FL4 short-circuits the budget check on a late refill. This exact +read-only branch also supplies an abstract Boolean when it is early. -/ +theorem fl4ReplyBudgetTestA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {a : A} {σ : State Unit} (H : SchedSRA st Z m κ s σ) + {sc : U64} (hc : sc ∈ Z.C) {x : SchedContext} {n : Nat} + (hk : σ.kheap (scNameA m sc) = some (.schedContext x n)) (ha : scActive x = true) + (hm : Fl4MinBudgetAgreeA ar a) : + (do + let ready ← sporadic.ready st s sc + if ready then sporadic.sufficient st ar s a sc 0#u64 else ok false) ⦃ b => + b = (refillReady σ.curTime (refillHd x) && fl4RefillSufficient 0 (refillHd x)) ⦄ := by + refine WP.spec_bind (ready_refinesA H.scr H.sch.time hc hk ha) ?_ + intro b hb + rw [getScRefillReady_eqA hk (H.inv.ne hk ha)] at hb + have he := (nd_one_injA hb).1 + cases b + · have hready : refillReady σ.curTime (refillHd x) = false := he + simp only [Bool.false_eq_true, ↓reduceIte, hready, Bool.false_and, WP.spec_ok] + · have hready : refillReady σ.curTime (refillHd x) = true := he + simp only [↓reduceIte, hready, Bool.true_and] + exact WP.spec_mono (fl4_sufficient_refinesA a H.scr hc hk ha 0#u64 hm) fun _ h => h.symm + +variable (st ar) in +/-- The concrete late-refill branch of Reply. A timeout is issued only +when the handler is valid and the original fault was not already a timeout. -/ +def replyLateProgramA (s : S) (a : A) (t sc ft : U64) : Result (S × A) := do + let valid ← faults.valid_timeout_handler st s t + if valid then + if ft != fl4_abi.consts.fault.Timeout then + let badge ← obj.Sc.badge st sc s + faults.handle_timeout st ar s a t (.Timeout badge) + else + let s' ← sched.postpone st s sc + ok (s', a) + else + let s' ← sched.postpone st s sc + ok (s', a) + +/-- Resolve the late branch using the actual timeout-handler and original +fault tag. [J] The timeout send consumes the row-12 FL4 transfer operation. +The postpone case carries the local release-queue successor. -/ +theorem replyLate_refinesA {s : S} {a : A} {σ : State Unit} + {t sc ft badge : U64} {q : ObjRef} {x : Tcb} {y : SchedContext} {fault : Option Fault} + (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hv : faults.valid_timeout_handler st s t = ok (isEpCap x.tcbTimeoutHandler)) + (hf : (ft != fl4_abi.consts.fault.Timeout) = !fault.elim false isTimeoutFault) + (hb : obj.Sc.badge st sc s = ok badge) (hbadge : badge.bv = y.scBadge) + {R : S → A → State Unit → Prop} + (timeout : isEpCap x.tcbTimeoutHandler = true → fault.elim false isTimeoutFault = false → + faults.handle_timeout st ar s a t (.Timeout badge) ⦃ r => + ∃ σ', ((), σ') ∈ (fl4HandleTimeout (Z := Unit) (specPtr t) (.timeout badge.bv) σ).1 ∧ R r.1 r.2 σ' ⦄) + (postponed : ¬(isEpCap x.tcbTimeoutHandler = true ∧ ¬(fault.elim false isTimeoutFault = true)) → + sched.postpone st s sc ⦃ s' => ∃ σ', ((), σ') ∈ (postpone (Z := Unit) q σ).1 ∧ R s' a σ' ⦄) : + replyLateProgramA st ar s a t sc ft ⦃ r => + ∃ σ', ((), σ') ∈ (replyLateSpecA (specPtr t) fault y q σ).1 ∧ R r.1 r.2 σ' ⦄ := by + unfold replyLateProgramA + rw [hv, bind_ok, hf] + have hs : replyLateSpecA (specPtr t) fault y q σ = + (if isEpCap x.tcbTimeoutHandler ∧ ¬(fault.elim false isTimeoutFault = true) + then fl4HandleTimeout (specPtr t) (.timeout y.scBadge) else postpone q) σ := by + unfold replyLateSpecA + rw [nd_bind_oneSc (getTcb_eq hk)] + by_cases h : isEpCap x.tcbTimeoutHandler = true ∧ ¬(fault.elim false isTimeoutFault = true) + · have hfalse : fault.elim false isTimeoutFault = false := Bool.eq_false_of_not_eq_true h.2 + rw [if_pos h.1] + simp only [hfalse, Bool.not_false, ↓reduceIte, hb, bind_ok] + refine WP.spec_mono (timeout h.1 hfalse) ?_ + rintro r ⟨σ', hm, hR⟩ + exact ⟨σ', by rw [hs, if_pos h, ← hbadge]; exact hm, hR⟩ + · have hp : (do let s' ← sched.postpone st s sc; ok (s', a)) ⦃ r => + ∃ σ', ((), σ') ∈ (replyLateSpecA (specPtr t) fault y q σ).1 ∧ R r.1 r.2 σ' ⦄ := by + refine WP.spec_bind (postponed h) ?_ + rintro s' ⟨σ', hm, hR⟩ + exact (WP.spec_ok _).2 ⟨σ', by rw [hs, if_neg h]; exact hm, hR⟩ + cases hv' : isEpCap x.tcbTimeoutHandler + · simpa only [hv', Bool.false_eq_true, ↓reduceIte] using hp + · have hf' : fault.elim false isTimeoutFault = true := by + by_contra hne + exact h ⟨hv', hne⟩ + simpa only [hv', hf', Bool.not_true, Bool.false_eq_true, ↓reduceIte] using hp + +variable (st ar) in +/-- The concrete runnable tail. The readiness test short-circuits the +minimum-budget observation. -/ +def replyTailProgramA (s : S) (a : A) (t ft : U64) : Result (S × A) := do + let sc ← obj.Tcb.sc st t s + let empty ← obj.Sc.is_null sc + if empty then ok (s, a) else + let run ← sched.is_runnable st s t + if run then + let eligible ← do + let ready ← sporadic.ready st s sc + if ready then sporadic.sufficient st ar s a sc 0#u64 else ok false + if eligible then + let s' ← sched.possible_switch_to st s t + ok (s', a) + else replyLateProgramA st ar s a t sc ft + else ok (s, a) + +private theorem replyRunnableA {s : S} {σ : State Unit} {T : Set U64} {t : U64} {x : Tcb} + (H : StEqA T (L.mem s) σ) (ht : t ∈ T) (hk : σ.kheap (specPtr t) = some (.tcb x)) : + sched.is_runnable st s t = ok (runnable x.tcbState) := by + have hc := H t ht x hk + have hr := runnable_iffA hc + have hread := eq_of_spec_fd (P1.obj.Tcb.state.spec (st := st) s t) + unfold sched.is_runnable + rw [hread, bind_ok] + by_cases h : runnable x.tcbState = true + · obtain h0 | h1 | h2 := hr.2 h + · simp [h, h0] + · simp [h, h1] + · simp [h, h2] + · have h0 : L.mem s (stA t) ≠ sched.TS_RUNNING := fun e => h (hr.1 (Or.inl e)) + have h1 : L.mem s (stA t) ≠ sched.TS_RESTART := fun e => h (hr.1 (Or.inr (Or.inl e))) + have h2 : L.mem s (stA t) ≠ sched.TS_RUNNING_VM := fun e => h (hr.1 (Or.inr (Or.inr e))) + rw [if_neg h0, if_neg h1, decide_eq_false h2, Bool.eq_false_of_not_eq_true h] + +/-- The runnable tail takes the real local scheduler switch when the +refill is eligible. [J] The late continuation supplies either the local +postpone successor or the FL4 timeout-send successor. -/ +theorem replyTail_refinesA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {a : A} {σ : State Unit} (H : SchedSRA st Z m κ s σ) + (hst : StEqA Z.T (L.mem s) σ) {t ft : U64} (ht : t ∈ Z.T) + (hon : L.mem s (affA t) = L.cpu s) (hmb : Fl4MinBudgetAgreeA ar a) + (fault : Option Fault) + (active : ∀ sc y n, L.mem s (scA t) = sc → sc ≠ 0#u64 → + σ.kheap (scNameA m sc) = some (.schedContext y n) → scActive y = true) + {R : S → A → State Unit → Prop} + (same : R s a σ) + (afterSwitch : ∀ s' σ' Q', SchedSRA st { Z with Q := Q' } m κ s' σ' → + σ'.kheap = σ.kheap → WritesIn st s s' (SWrA (st := st) Z s) → R s' a σ') + (late : ∀ sc y n, L.mem s (scA t) = sc → sc ≠ 0#u64 → + σ.kheap (scNameA m sc) = some (.schedContext y n) → + (refillReady σ.curTime (refillHd y) && fl4RefillSufficient 0 (refillHd y)) = false → + replyLateProgramA st ar s a t sc ft ⦃ out => + ∃ σ', ((), σ') ∈ (replyLateSpecA (specPtr t) fault y (scNameA m sc) σ).1 ∧ + R out.1 out.2 σ' ⦄) : + replyTailProgramA st ar s a t ft ⦃ out => + ∃ σ', ((), σ') ∈ (replyTailSpecA (specPtr t) fault σ).1 ∧ R out.1 out.2 σ' ⦄ := by + obtain ⟨x, hx, hxs⟩ := H.sch.tcb t ht + let sc := L.mem s (scA t) + have hread : obj.Tcb.sc st t s = ok sc := eq_of_spec_fd (obj.Tcb.sc.spec (st := st) s t) + unfold replyTailProgramA + rw [hread, bind_ok] + refine WP.spec_bind (P1.obj.Sc.is_null.spec sc) ?_ + rintro _ rfl + by_cases h0 : sc = 0#u64 + · simp only [h0, decide_true, ↓reduceIte, WP.spec_ok] + have hs : x.tcbSchedContext = none := by rw [hxs, ← show sc = L.mem s (scA t) from rfl, h0]; rfl + exact ⟨σ, by rw [replyTailSpec_skipA hx (Or.inl hs)]; rfl, same⟩ + simp only [decide_eq_false h0, Bool.false_eq_true, ↓reduceIte] + rw [replyRunnableA hst ht hx, bind_ok] + cases hr : runnable x.tcbState + · simp only [Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + exact ⟨σ, by rw [replyTailSpec_skipA hx (Or.inr hr)]; rfl, same⟩ + simp only [↓reduceIte] + have hc : sc ∈ Z.C := H.rest.scMem t ht h0 + obtain ⟨y, n, hy, -⟩ := H.scr.sc sc hc + have hxs' : x.tcbSchedContext = some (scNameA m sc) := by + change x.tcbSchedContext = some (scNameA m (L.mem s (scA t))) + rw [hxs, optScA, if_neg h0] + rfl + have hspec := replyTailSpec_runA hx hxs' hr hy fault + refine WP.spec_bind (fl4ReplyBudgetTestA H hc hy (active sc y n rfl h0 hy) hmb) ?_ + intro eligible he + cases eligible + · simp only [Bool.false_eq_true, ↓reduceIte] + refine WP.spec_mono (late sc y n rfl h0 hy he.symm) ?_ + rintro out ⟨σ', hm, hR⟩ + refine ⟨σ', ?_, hR⟩ + have nh : ¬(refillReady σ.curTime (refillHd y) = true ∧ fl4RefillSufficient 0 (refillHd y) = true) := by + intro h + apply Bool.false_ne_true + rw [h.1, h.2] at he + exact he + rw [hspec, if_neg nh] + exact hm + · simp only [↓reduceIte] + refine WP.spec_bind (possible_switch_to_refinesA H t ht hon (fun x' hx' => by + rw [hx] at hx'; cases hx'; exact hr) hst) ?_ + rintro s' ⟨σ', Q', hs, hk, hw, H'⟩ + refine (WP.spec_ok _).2 ⟨σ', ?_, afterSwitch s' σ' Q' H' hk hw⟩ + have hh : refillReady σ.curTime (refillHd y) = true ∧ fl4RefillSufficient 0 (refillHd y) = true := + by simpa only [Bool.and_eq_true] using he.symm + rw [hspec, if_pos hh, hs] + rfl + +variable (st) in +/-- The pre-transfer refill read uses the SC's CPU after reply removal. -/ +def replyUnblockProgramA (s : S) (t : U64) : Result S := do + let sc ← obj.Tcb.sc st t s + let sporadicSc ← sporadic.sc_sporadic st s sc + if sporadicSc then + let cpu ← obj.Sc.cpu st sc s + let current ← obj.node_get st s cpu layout.node.CUR_SC + if sc != current then sporadic.unblock_check st s sc else ok s + else ok s + +private theorem replyUnblock_local_eqA (s : S) (t : U64) + (hon : L.mem s (scA t) ≠ 0#u64 → L.mem s (scCoreA (L.mem s (scA t))) = L.cpu s) : + replyUnblockProgramA st s t = notification.unblock_sporadic st s t := by + unfold replyUnblockProgramA notification.unblock_sporadic + simp only [eq_of_spec_fd (obj.Tcb.sc.spec (L := L) s t), bind_ok] + by_cases h0 : L.mem s (scA t) = 0#u64 + · simp only [sc_sporadic_eq, if_pos h0, bind_ok, Bool.false_eq_true, ↓reduceIte] + · have hcpu : obj.Sc.cpu st (L.mem s (scA t)) s = ok (L.cpu s) := + (eq_of_spec_fd (obj.Sc.cpu.spec (L := L) s _)).trans (congrArg ok (hon h0)) + simp only [hcpu, bind_ok, sc_ne_eqA, sched.cur_sc, L.cur_cpu_eq s, Std.bind_assoc, bne_iff_ne, decide_eq_true_eq] + +/-- [S] On the SC's core, the Reply refill test is the existing +nonasserting sporadic-unblock refinement. -/ +theorem replyUnblock_refinesA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {σ : State Unit} (H : SchedSRA st Z m κ s σ) {t : U64} (ht : t ∈ Z.T) + (hon : L.mem s (scA t) ≠ 0#u64 → L.mem s (scCoreA (L.mem s (scA t))) = L.cpu s) + (hrq : specPtr t ∈ σ.releaseQueue → ∀ y n, + σ.kheap (scNameA m (L.mem s (scA t))) = some (.schedContext y n) → + refillReady σ.curTime (refillHd y) = false) : + replyUnblockProgramA st s t ⦃ s' => ∃ σ', + (getTcbObjRef (Z := Unit) Tcb.tcbSchedContext (specPtr t) >>= + ifSporadicCurScTestRefillUnblockCheck) σ = ({((), σ')}, False) ∧ + SchedSRA st Z m κ s' σ' ∧ UnbFrA (st := st) Z m s s' σ σ' ⦄ := by + rw [replyUnblock_local_eqA s t hon] + obtain ⟨x, hx, hs⟩ := H.sch.tcb t ht + rw [nd_bind_oneSc (getTcbObjRef_eq _ hx), hs] + exact unblock_sporadic_SRA H ht false hrq + +variable (st ar) in +/-- The concrete message or fault reply, before testing the new receiver state. -/ +def replyBodyProgramA (s : S) (a : A) (sender receiver ft : U64) (grant : Bool) : Result (S × A) := do + if ft = fl4_abi.consts.fault.NullFault then + let s1 ← ipc.do_ipc_transfer st ar s a sender 0#u64 0#u64 grant receiver + let s2 ← sched.set_thread_state st s1 receiver sched.TS_RUNNING + ok (s2, a) + else + let (restart, s1, a1) ← faults.handle_fault_reply st ar s a receiver sender + let s2 ← faults.clear_fault st s1 receiver + if restart then + let s3 ← sched.set_thread_state st s2 receiver sched.TS_RESTART + ok (s3, a1) + else + let s3 ← sched.set_thread_state st s2 receiver sched.TS_INACTIVE + ok (s3, a1) + +private theorem replyBudgetProgramA (s : S) (a : A) (t sc ft : U64) : + (do + let ready ← sporadic.ready st s sc + if ready then + let enough ← sporadic.sufficient st ar s a sc 0#u64 + if enough then + let s' ← sched.possible_switch_to st s t + ok (s', a) + else replyLateProgramA st ar s a t sc ft + else replyLateProgramA st ar s a t sc ft) = + (do + let eligible ← do + let ready ← sporadic.ready st s sc + if ready then sporadic.sufficient st ar s a sc 0#u64 else ok false + if eligible then + let s' ← sched.possible_switch_to st s t + ok (s', a) + else replyLateProgramA st ar s a t sc ft) := by + rw [Std.bind_assoc] + congr 1 + funext ready + cases ready <;> simp only [bind_ok, Bool.false_eq_true, ↓reduceIte] <;> rfl + +/-- Split the actual live Rust path at reply removal, refill, transfer and +runnable tail. The fault tag read before transfer reaches the actual tail. -/ +theorem replyLiveProgramA {s : S} {a : A} {sender r t : U64} {grant : Bool} + (ht : obj.Reply.tcb st r s = ok t) (hn : t ≠ 0#u64) + (hs : obj.Tcb.state st t s = ok sched.TS_BLOCKED_ON_REPLY) : + ipc.do_reply_transfer st ar s a sender r grant = (do + let (s1, a1) ← ipc.reply_remove st ar s a r t + let s2 ← replyUnblockProgramA st s1 t + let ft ← obj.Tcb.fault_type st t s2 + let (s3, a2) ← replyBodyProgramA st ar s2 a1 sender t ft grant + replyTailProgramA st ar s3 a2 t ft) := by + unfold ipc.do_reply_transfer + rw [ht, bind_ok, eq_of_spec_fd (P1.obj.Tcb.is_null.spec t), bind_ok, hs] + simp only [decide_eq_false hn, bind_ok, Bool.false_eq_true, bne_self_eq_false, ↓reduceIte] + unfold replyUnblockProgramA replyBodyProgramA replyTailProgramA + simp only [← replyBudgetProgramA] + simp only [replyLateProgramA, Std.bind_assoc, Std.bind_tc_eq] + +/-- The actual live Reply composition. [J] Each operation carries only its +own intermediate relation. The same original fault is passed to the tail. -/ +theorem do_reply_transfer_liveA {s : S} {a : A} {σ : State Unit} {sender r t : U64} {grant : Bool} + {x : Reply} {y : Tcb} (hkr : σ.kheap (specPtr r) = some (.reply x)) + (hback : x.replyTcb = some (specPtr t)) (hkt : σ.kheap (specPtr t) = some (.tcb y)) + (hst : y.tcbState = .blockedOnReply (specPtr r)) + (ht : obj.Reply.tcb st r s = ok t) (hn : t ≠ 0#u64) + (hs : obj.Tcb.state st t s = ok sched.TS_BLOCKED_ON_REPLY) + {M1 M2 R : S → A → State Unit → Prop} + {M3 : S → A → State Unit → U64 → Option Fault → Prop} + (remove : ipc.reply_remove st ar s a r t ⦃ out => ∃ σ1, + replyRemove (Z := Unit) (specPtr t) (specPtr r) σ = ({((), σ1)}, False) ∧ M1 out.1 out.2 σ1 ⦄) + (unblock : ∀ s1 a1 σ1, M1 s1 a1 σ1 → replyUnblockProgramA st s1 t ⦃ s2 => ∃ σ2, + (getTcbObjRef Tcb.tcbSchedContext (specPtr t) >>= ifSporadicCurScTestRefillUnblockCheck) σ1 = + ({((), σ2)}, False) ∧ M2 s2 a1 σ2 ⦄) + (transfer : ∀ s2 a1 σ2 ft, M2 s2 a1 σ2 → ft = L.mem s2 (tFaultA t) → + replyBodyProgramA st ar s2 a1 sender t ft grant ⦃ out => ∃ σ3 y2, + σ2.kheap (specPtr t) = some (.tcb y2) ∧ + ((y2.tcbFault = none ∧ ((), σ3) ∈ ((fl4DoIpcTransfer (Z := Unit) + (specPtr sender) none 0 grant (specPtr t) >>= fun _ => setThreadState (specPtr t) .running) σ2).1) ∨ + ∃ f, y2.tcbFault = some f ∧ ((), σ3) ∈ (replyFaultBodyA (specPtr sender) (specPtr t) f σ2).1) ∧ + M3 out.1 out.2 σ3 ft y2.tcbFault ⦄) + (tail : ∀ s3 a2 σ3 ft fault, M3 s3 a2 σ3 ft fault → + replyTailProgramA st ar s3 a2 t ft ⦃ out => ∃ σ4, + ((), σ4) ∈ (replyTailSpecA (specPtr t) fault σ3).1 ∧ R out.1 out.2 σ4 ⦄) : + ipc.do_reply_transfer st ar s a sender r grant ⦃ out => ∃ σ4, + ((), σ4) ∈ (fl4DoReplyTransfer (Z := Unit) (specPtr sender) (specPtr r) grant σ).1 ∧ + R out.1 out.2 σ4 ⦄ := by + rw [replyLiveProgramA ht hn hs] + refine WP.spec_bind remove ?_ + rintro ⟨s1, a1⟩ ⟨σ1, h1, H1⟩ + refine WP.spec_bind (unblock s1 a1 σ1 H1) ?_ + rintro s2 ⟨σ2, h2, H2⟩ + refine WP.spec_bind (obj.Tcb.fault_type.spec s2 t) ?_ + intro ft hft + refine WP.spec_bind (transfer s2 a1 σ2 ft H2 hft) ?_ + rintro ⟨s3, a2⟩ ⟨σ3, y2, hk2, h3, H3⟩ + refine WP.spec_mono (tail s3 a2 σ3 ft y2.tcbFault H3) ?_ + rintro out ⟨σ4, h4, H4⟩ + exact ⟨σ4, fl4ReplyLive_memA hkr hback hkt hst h1 h2 hk2 h3 h4, H4⟩ + +/-- The live timeout payload comes from the actual SC consume/reset, +with the FL4 timer observation and scheduler relation. -/ +theorem fl4ReplyTimeoutConsumedA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {a : A} {σ : State Unit} (H : SchedSRA st Z m κ s σ) + {t : U64} (ht : t ∈ Z.T) (hsc : L.mem s (scA t) ≠ 0#u64) + (htu : Fl4TicksToUsAgreeA ar a) (badge : Badge) : + schedcontext.update_consumed st ar s a (L.mem s (scA t)) ⦃ r => + ∃ σ', fl4MakeFaultMsg (Z := Unit) (.timeout badge) (specPtr t) σ = + ({((5, badge :: wordsFromTime r.1.bv), σ')}, False) ∧ + SchedSRA st Z m κ r.2 σ' ∧ + ∃ n, WritesIn st s r.2 (ScObjA (L.mem s (scA t)) n) ⦄ := by + obtain ⟨x, hx, hs⟩ := H.sch.tcb t ht + have hs' : x.tcbSchedContext = some (scNameA m (L.mem s (scA t))) := by + rw [hs, optScA, if_neg hsc] + rfl + refine WP.spec_mono (fl4_update_consumed_refinesA H a (H.rest.scMem t ht hsc) htu) ?_ + rintro r ⟨y, n, v, hy, hu, _, hw, HS⟩ + refine ⟨_, ?_, HS, n, hw⟩ + simp only [fl4MakeFaultMsg] + rw [nd_bind_oneSc (getTcb_eq hx), hs'] + change (fl4SchedContextUpdateConsumed (Z := Unit) _ >>= _) σ = _ + rw [nd_bind_oneSc hu] + rfl + +end +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/IpcScSendPushA.lean b/proofs/Fl4ProofsA64/P3/IpcScSendPushA.lean new file mode 100644 index 000000000..93072b008 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/IpcScSendPushA.lean @@ -0,0 +1,448 @@ +/- +P3 (AArch64): send to a waiting receiver in the selected donating reply-push +arm. After transfer and unlink, push the reply, then wake the receiver. +[S] Local donor, unchanged receiver affinity and no bound donor VCPU. +The joint caller supplies the released-SC fact at the reply-push result. +`send_ipc_recv_pushDonate_fpA` also gives the write set (`DonFpA`). +-/ +import Fl4ProofsA64.P3.IpcScSendWakeA +import Fl4ProofsA64.P3.IpcScFootA +import Fl4ProofsA64.P3.EpSendFpA +import Fl4ProofsA64.P3.SpecFrameIpcA + +open Aeneas Aeneas.Std Result +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4SpecA64 Sched +noncomputable section + +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + +theorem sendUnlinkFactsA {σ σ' : State Unit} {t d r : U64} {xt xd : Tcb} {xr : Reply} + (htd : t ≠ d) (hkt : σ.kheap (specPtr t) = some (.tcb xt)) (hkd : σ.kheap (specPtr d) = some (.tcb xd)) + (hkr : σ.kheap (specPtr r) = some (.reply xr)) (hback : xr.replyTcb = some (specPtr d)) + {e : ObjRef} {dd : ReceiverPayload} (hst : xd.tcbState = .blockedOnReceive e (some (specPtr r)) dd) + (hq : ∀ p ∈ E, ∀ z, σ.kheap (specPtr p) = some (.endpoint z) → specPtr d ∉ z.epQueue) + (hu : replyUnlinkTcb (Z := Unit) (specPtr d) (specPtr r) σ = ({((), σ')}, False)) : + σ'.kheap (specPtr t) = some (.tcb xt) ∧ + σ'.kheap (specPtr d) = some (.tcb { xd with tcbState := .inactive }) ∧ + (∀ p ∈ E, ∀ z, σ'.kheap (specPtr p) = some (.endpoint z) → specPtr d ∉ z.epQueue) ∧ SchedKeepA σ σ' := by + have eq := (nd_one_injA (hu.symm.trans (replyUnlinkTcb_eq hkr hback hkd hst))).2 + subst σ' + have htr : specPtr t ≠ specPtr r := + kheap_ne hkt hkr (fun _ h1 h2 => by subst h1; cases h2) + refine ⟨?_, ?_, ?_, (SchedKeepA.upd _ _ _).trans ((SchedKeepA.upd _ _ _).trans (SchedKeepA.sts _ _))⟩ + · rw [stsAct_kheap, updK_kheap, Function.update_of_ne (fun h => htd (specPtr_inj h)), + updK_kheap, Function.update_of_ne htr, hkt] + · rw [stsAct_kheap, updK_kheap, Function.update_self] + · intro p hp z hz + rw [stsAct_kheap, updK_kheap] at hz + have hpd : specPtr p ≠ specPtr d := by intro h; rw [h, Function.update_self] at hz; cases hz + rw [Function.update_of_ne hpd] at hz + exact notq_updR hq r { xr with replyTcb := none } p hp z hz + +/-- A spec step with one outcome keeps the SC sizes. -/ +theorem sszRunA {n : SMonad Unit Unit} [SPresInst n] {σ σ' : State Unit} + (h : n σ = ({((), σ')}, False)) : SSzA σ σ' := + (SPresInst.pres (m := n) σ ((), σ') (by rw [h]; exact Set.mem_singleton _)).2.1 + +/-- An `Inactive` thread is a wake target of `sendWakeAfterPushA`. -/ +theorem nrInactiveA {ts : ThreadState} (h : ts = .inactive) : runnable ts = false ∧ ts ≠ .idleThreadState := by + subst h; exact ⟨rfl, by decide⟩ + +/-- `sendWakeAfterPushA`'s write set: the receiver's state word and scheduler words. -/ +theorem sendWakeFpA {s : S} {σ : State Unit} (a : A) + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) {d : U64} (hd : d ∈ Z.T) (hon : L.mem s (affA d) = L.cpu s) + {x : Tcb} (hk : σ.kheap (specPtr d) = some (.tcb x)) + (hst : runnable x.tcbState = false ∧ x.tcbState ≠ .idleThreadState) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr d ∉ z.epQueue) + (hb : RelIfBoundSc σ (specPtr d)) : + wakeAfterReplyPushA (st := st) s a d ⦃ out => + WritesIn st s out.1 (fun b => b = stA d ∨ MWrA (st := st) Z m σ s b) ⦄ := by + let O := schedOpsEAOfPlacementA (st := st) (m := m) (κ := κ) P + have F : SchedFootEA st Z.T E R Bf (IpcSchedRelA st Z m κ) (fun σ s => MWrA (st := st) Z m σ s) := + schedFootEA_of_sched + have hS : IpcSchedRelA st Z m κ s σ := ⟨Z.Q, Z.R, H⟩ + have hnr : runnable x.tcbState = false := hst.1 + have nIdle : x.tcbState ≠ .idleThreadState := hst.2 + have hNB := NoBlockE.updT hk { x with tcbState := .running } (Or.inr (Or.inl ⟨rfl, nIdle, Or.inr hb⟩)) + unfold wakeAfterReplyPushA + refine WP.spec_bind (WP.spec_and + (set_thread_state_relF O F PE s σ HE hS hd hk hq .running (by decide) sched.TS_RUNNING + (by unfold sched.TS_RUNNING; rfl) hNB) + (set_thread_state_fpA O.to0 F.to0 PE hS s σ HE (WritesIn.refl _ _) (EpStep.refl _) hd hk hq .running (by decide) + sched.TS_RUNNING (by unfold sched.TS_RUNNING; rfl) hNB)) ?_ + rintro s5 ⟨⟨hR5, hS5, -⟩, hw5⟩ + refine WP.spec_mono (send_tail_fpA O F a hS hd hon hS hk hnr hq hR5 hS5) fun r hr => ?_ + exact WritesIn.schedA F hw5 ((ssz_updK_tcb _ _ _).trans (ssz_stsAct _ _)) (B := fun _ => False) + (hr.mono fun _ h => Or.inr h) (fun _ h => h.elim) (fun _ h => Or.inr h) + +set_option maxHeartbeats 250000 in +private theorem sendRecvDonateAfterUnlinkA {s : S} {σ : State Unit} (a : A) {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {sc r t d : U64} (hc : sc ∈ Z.C) (hsc0 : sc ≠ 0#u64) (hr : r ∈ R) (hi : ReplyIsolatedA (L.mem s) Z.C stack r) + (ht : t ∈ Z.T) (hd : d ∈ Z.T) (htd : t ≠ d) {xt xd : Tcb} + (hkt : σ.kheap (specPtr t) = some (.tcb xt)) (hkd : σ.kheap (specPtr d) = some (.tcb xd)) + (hsf : L.mem s (scA t) = sc) (hsd : L.mem s (scA d) = 0#u64) + (hfon : L.mem s (affA t) = L.cpu s) (hon : L.mem s (affA d) = L.cpu s) + (hmig : L.mem s (affA d) = L.mem s (scCoreA sc)) + (hV : L.mem s (core.num.U64.wrapping_add t 3664#u64) = 0#u64) + (hni : NotIdleScA m (scNameA m sc)) (hRun : SndRunA σ (specPtr t) xt) + (hInact : xd.tcbState = .inactive) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr d ∉ z.epQueue) + (call g gr : Bool) (hcf : call = true ∨ xt.tcbFault ≠ none) (hg : g = true ∨ gr = true) + (hpost : ∀ σ', replyPush (Z := Unit) (specPtr t) (specPtr d) (specPtr r) true σ = ({((), σ')}, False) → + RelIfBoundSc σ' (specPtr d)) : + sendRecvAfterUnlinkA (st := st) (ar := ar) s a t d r call g gr true ⦃ out => out.2 = a ∧ + ∃ σ', (do + replyPush (Z := Unit) (specPtr t) (specPtr d) (specPtr r) true + setThreadState (specPtr d) .running + specSendTailA (specPtr d)) σ = ({((), σ')}, False) ∧ + EpStateRel Z.T E R Bf (L.mem out.1) σ' ∧ IpcSchedRelA st Z m κ out.1 σ' ∧ + ReplyStackRelA (L.mem out.1) R Z.C σ' (scNameA m) (Function.update stack sc (r :: stack sc)) ∧ + WritesIn st s out.1 (fun b => (∃ q ∈ R, q.val ≤ b.val ∧ b.val < q.val + 32) ∨ b = stA t ∨ + b = tReplyA t ∨ b = stA d ∨ MWrA (st := st) Z m σ s b) ⦄ := by + have hr0 : r ≠ 0#u64 := fun eq => (PE.rep r hr).2.1 (by rw [eq]; rfl) + have hFault : call = true ∨ L.mem s (tFaultA t) ≠ fl4_abi.consts.fault.NullFault := + hcf.imp id (fun hn he => by + unfold fl4_abi.consts.fault.NullFault at he + exact hn ((HE.at_tcb ht hkt).fault0.mp he)) + rw [sendRecvSelectedPushA s a t d r call g gr true hr0 hg hFault] + have hEp := not_queued_codeA HE ht hkt (runnable_codeA hRun.1) + have hRB : RelIfBound σ (specPtr t) := H.rd _ _ hkt (Or.inl hRun.1) + refine WP.spec_bind (reply_push_donateFpA a H P PE HE HS hc hsc0 hr hi ht hd htd hsf hsd hfon hmig + hV hni hEp hRun.2.1 hRB hRun.2.2.1 hRun.2.2.2 hpost) ?_ + rintro ⟨s4, a4⟩ ⟨rfl, σ4, hp, ⟨Q4, R4, H4⟩, E4, S4, V4, w4⟩ + obtain ⟨x4, hk4, hs4⟩ := V4.tcb (specPtr d) (fun eq => htd (specPtr_inj eq).symm) xd hkd + have hq4 : ∀ e ∈ E, ∀ z, σ4.kheap (specPtr e) = some (.endpoint z) → specPtr d ∉ z.epQueue := by + intro e he z hz + obtain ⟨y, hy, _⟩ := HE.ep e he + have eq := KernelObject.endpoint.inj (Option.some.inj (hz.symm.trans (V4.ep _ _ hy))) + exact hq e he z (eq.symm ▸ hy) + have hon4 : L.mem s4 (affA d) = L.cpu s4 := by rw [H.aff_eq H4 hd hd, H.cpu_eq H4, hon] + refine WP.spec_mono (WP.spec_and + (sendWakeAfterPushA a4 H4 P PE E4 S4 hd hon4 hk4 (nrInactiveA (hs4.trans hInact)) hq4 (hpost _ hp)) + (sendWakeFpA a4 H4 P PE E4 hd hon4 hk4 (nrInactiveA (hs4.trans hInact)) hq4 (hpost _ hp))) ?_ + rintro out ⟨⟨ha, σ', hW, E', H', S'⟩, w5⟩ + refine ⟨ha, σ', (nd_bind_one hp _).trans hW, E', H', S', (WritesIn.trans (st := st) w4 w5).mono fun b h => ?_⟩ + rcases h with (h | h | h | h) | (h | h) + · exact Or.inl h + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr (Or.inl h)) + · exact Or.inr (Or.inr (Or.inr (Or.inr h))) + · exact Or.inr (Or.inr (Or.inr (Or.inl h))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (mwr_back rfl rfl (sszRunA hp) w4.1 h)))) + +set_option maxHeartbeats 250000 in +private theorem sendRecvNoDonAfterUnlinkA {s : S} {σ : State Unit} (a : A) {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {sc r t d : U64} (hsc0 : sc ≠ 0#u64) (hr : r ∈ R) + (ht : t ∈ Z.T) (hd : d ∈ Z.T) (htd : t ≠ d) {xt xd : Tcb} + (hkt : σ.kheap (specPtr t) = some (.tcb xt)) (hkd : σ.kheap (specPtr d) = some (.tcb xd)) + (hsf : L.mem s (scA t) = sc) (hdsc : xd.tcbSchedContext ≠ none) + (hon : L.mem s (affA d) = L.cpu s) (hRun : SndRunA σ (specPtr t) xt) + (hInact : xd.tcbState = .inactive) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr d ∉ z.epQueue) + (call g gr : Bool) (hcf : call = true ∨ xt.tcbFault ≠ none) (hg : g = true ∨ gr = true) + (hpost : ∀ σ', replyPush (Z := Unit) (specPtr t) (specPtr d) (specPtr r) true σ = ({((), σ')}, False) → + RelIfBoundSc σ' (specPtr d)) : + sendRecvAfterUnlinkA (st := st) (ar := ar) s a t d r call g gr true ⦃ out => out.2 = a ∧ + ∃ σ', (do + replyPush (Z := Unit) (specPtr t) (specPtr d) (specPtr r) true + setThreadState (specPtr d) .running + specSendTailA (specPtr d)) σ = ({((), σ')}, False) ∧ + EpStateRel Z.T E R Bf (L.mem out.1) σ' ∧ IpcSchedRelA st Z m κ out.1 σ' ∧ + ReplyStackRelA (L.mem out.1) R Z.C σ' (scNameA m) stack ∧ + WritesIn st s out.1 (fun b => (∃ q ∈ R, q.val ≤ b.val ∧ b.val < q.val + 32) ∨ b = stA t ∨ + b = tReplyA t ∨ b = stA d ∨ MWrA (st := st) Z m σ s b) ⦄ := by + have hr0 : r ≠ 0#u64 := fun eq => (PE.rep r hr).2.1 (by rw [eq]; rfl) + have hFault : call = true ∨ L.mem s (tFaultA t) ≠ fl4_abi.consts.fault.NullFault := + hcf.imp id (fun hn he => by + unfold fl4_abi.consts.fault.NullFault at he + exact hn ((HE.at_tcb ht hkt).fault0.mp he)) + rw [sendRecvSelectedPushA s a t d r call g gr true hr0 hg hFault] + have hEp := not_queued_codeA HE ht hkt (runnable_codeA hRun.1) + have hRB : RelIfBound σ (specPtr t) := H.rd _ _ hkt (Or.inl hRun.1) + refine WP.spec_bind (reply_push_calleeScFpA a H P PE HE HS hsc0 hr ht hd htd hsf hkd hdsc hEp hRun.2.1 hRB + hRun.2.2.1 hRun.2.2.2) ?_ + rintro ⟨s4, a4⟩ ⟨rfl, σ4, hp, ⟨Q4, R4, H4⟩, E4, S4, V4, w4⟩ + obtain ⟨x4, hk4, hs4⟩ := V4.tcb (specPtr d) (fun eq => htd (specPtr_inj eq).symm) xd hkd + have hq4 : ∀ e ∈ E, ∀ z, σ4.kheap (specPtr e) = some (.endpoint z) → specPtr d ∉ z.epQueue := by + intro e he z hz + obtain ⟨y, hy, _⟩ := HE.ep e he + have eq := KernelObject.endpoint.inj (Option.some.inj (hz.symm.trans (V4.ep _ _ hy))) + exact hq e he z (eq.symm ▸ hy) + have hon4 : L.mem s4 (affA d) = L.cpu s4 := by rw [H.aff_eq H4 hd hd, H.cpu_eq H4, hon] + refine WP.spec_mono (WP.spec_and + (sendWakeAfterPushA a4 H4 P PE E4 S4 hd hon4 hk4 (nrInactiveA (hs4.trans hInact)) hq4 (hpost _ hp)) + (sendWakeFpA a4 H4 P PE E4 hd hon4 hk4 (nrInactiveA (hs4.trans hInact)) hq4 (hpost _ hp))) ?_ + rintro out ⟨⟨ha, σ', hW, E', H', S'⟩, w5⟩ + refine ⟨ha, σ', (nd_bind_one hp _).trans hW, E', H', S', (WritesIn.trans (st := st) w4 w5).mono fun b h => ?_⟩ + rcases h with (h | h | h | h) | (h | h) + · exact Or.inl h + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr (Or.inl h)) + · exact Or.inr (Or.inr (Or.inr (Or.inr h))) + · exact Or.inr (Or.inr (Or.inr (Or.inl h))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (mwr_back rfl rfl (sszRunA hp) w4.1 h)))) + +variable (st Z m R Bf) in +/-- The write set of the donating send: `send_ipc_recv_fpA`'s (`SendFpA`), reply objects' words and the sender's +reply word. -/ +def DonFpA (σ : State Unit) (s : S) (t e d b : U64) : Prop := + SendFpA st Z.T R Bf (fun σ s => MWrA (st := st) Z m σ s) σ s t e d True b ∨ + (∃ r ∈ R, r.val ≤ b.val ∧ b.val < r.val + 32) ∨ b = tReplyA t + +set_option maxHeartbeats 400000 in +/-- Send to a waiting receiver, push the reply with SC donation, and wake the receiver. + +[S] The donor is on the current core with no bound VCPU, and the receiver +is already on the SC's core. See FL4 `endpoint.rs:92–112`, `ipc.rs:167–181`, +`schedcontext.rs:65–78` and seL4 `schedcontext.c:331–349`. +-/ +theorem send_ipc_recv_pushDonate_fpA {s : S} {σ : State Unit} (a : A) {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {kb : U64} (hPl : PlaceA Z.th kb) (hSt : StateRelA Z.th (L.mem s) σ) (hVld : SlotsValidA Z.th (L.mem s)) + (hC : BufCover st Z.T Bf s σ) (hNative : BufNativeA Z.T (L.mem s)) + (hoff : ∀ y, IsSlotA Z.th y → ¬ EWrite Z.T E R Bf y ∧ + ¬ EWrite Z.T E R Bf (core.num.U64.wrapping_add y 8#u64)) + {t : U64} (ht : t ∈ Z.T) {xt : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hRun : SndRunA σ (specPtr t) xt) + {e d : U64} (he : e ∈ E) {q : List ObjRef} + (hk : σ.kheap (specPtr e) = some (.endpoint (.recvEP (specPtr d :: q)))) + (hd : d ∈ Z.T) {xd : Tcb} (hkd : σ.kheap (specPtr d) = some (.tcb xd)) + (hu : ∀ rp, ipcBufOfA true xd = some rp → ∀ k, k < 128 → inUserFrame (bufAddr rp k) σ) + (hrep : RepOKA σ (specPtr d)) (badge : U64) (g gr blk call : Bool) + (hcase : (xt.tcbFault = none ∧ (g = false ∨ ipcBufOfA false xt = none ∨ + unat (dataToMessageInfo (xt.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0)) ∨ + ∃ fault, xt.tcbFault = some fault ∧ FaultOKA (L := L) s t (ipcBufOfA true xd) xt fault) + {sc r : U64} (hc : sc ∈ Z.C) (hsc0 : sc ≠ 0#u64) (hr : r ∈ R) (hi : ReplyIsolatedA (L.mem s) Z.C stack r) + (hfs : L.mem s (scA t) = sc) (hd0 : L.mem s (scA d) = 0#u64) + (hfon : L.mem s (affA t) = L.cpu s) (hon : L.mem s (affA d) = L.cpu s) + (hmig : L.mem s (affA d) = L.mem s (scCoreA sc)) + (hV : L.mem s (core.num.U64.wrapping_add t 3664#u64) = 0#u64) + (hni : NotIdleScA m (scNameA m sc)) {dd : ReceiverPayload} + (hRecv : xd.tcbState = .blockedOnReceive (specPtr e) (some (specPtr r)) dd) + (hcf : call = true ∨ xt.tcbFault ≠ none) (hg : g = true ∨ gr = true) + (hReleased : ∀ σ2 σ3 σ4, + doIpcTransfer (Z := Unit) (specPtr t) (some (specPtr e)) badge.bv g (specPtr d) + (updK σ (specPtr e) (.endpoint (epDeq (.recvEP (specPtr d :: q)) (specPtr d)))) = ({((), σ2)}, False) → + replyUnlinkTcb (Z := Unit) (specPtr d) (specPtr r) σ2 = ({((), σ3)}, False) → + replyPush (Z := Unit) (specPtr t) (specPtr d) (specPtr r) true σ3 = ({((), σ4)}, False) → + RelIfBoundSc σ4 (specPtr d)) : + endpoint.send_ipc st ar s a blk call badge g gr true t e ⦃ out => out.2 = a ∧ + ∃ σ', sendIpc (Z := Unit) blk call badge.bv g gr true (specPtr t) (specPtr e) σ = ({((), σ')}, False) ∧ + EpStateRel Z.T E R Bf (L.mem out.1) σ' ∧ IpcSchedRelA st Z m κ out.1 σ' ∧ + ReplyStackRelA (L.mem out.1) R Z.C σ' (scNameA m) (Function.update stack sc (r :: stack sc)) ∧ + WritesIn st s out.1 (DonFpA st Z m R Bf σ s t e d) ⦄ := by + let O := schedOpsEAOfPlacementA (st := st) (m := m) (κ := κ) P + have htSpec : xt.tcbSchedContext = some (scNameA m sc) := by + rw [schedTcbScA H ht hkt, hfs, optScA, if_neg hsc0] + rfl + have hdSpec : xd.tcbSchedContext = none := by + rw [schedTcbScA H hd hkd, hd0, optScA, if_pos (show (0#u64 : U64) = 0#u64 from rfl)] + refine send_ipc_recvContA O PE hPl s a σ HE ⟨Z.Q, Z.R, H⟩ hSt hVld hC hNative hoff ht + ((H.thT t).2 ht) hkt (runnable_codeA hRun.1).1 he hk ((H.thT d).2 hd) hkd hu hrep + badge g gr blk call true hcase ?_ + intro s1' s2 σ2 x2 rep dd' σ1' _ htd hde hS1 E2 hS2 hW hE hW2 hE2 hfr2 hkt2 hkd1 hkd2 hstd hst2 hrr hq2 hT hfp + have F : SchedFootEA st Z.T E R Bf (IpcSchedRelA st Z m κ) (fun σ s => MWrA (st := st) Z m σ s) := + schedFootEA_of_sched + have hrep' : rep = some (specPtr r) := by + have eq := hstd.symm.trans hRecv + injection eq + subst rep + obtain ⟨Q2, R2, H2⟩ := hS2 + obtain ⟨xr, hkr, hback⟩ := hrr _ rfl + have HS2 := endpointStackFrameA H.toSchedSRN P PE HE HS hW hE + have hi2 := endpointKeepsIsolatedA H.toSchedSRN P PE hr hi hW + have hv2 := (endpointKeepsVcpuA (Z := Z) PE ht hW).trans hV + have hdSpec2 := (epStepTcbScA hE hkd hkd2).trans hdSpec + have hr0 : r ≠ 0#u64 := fun eq => (PE.rep r hr).2.1 (by rw [eq]; rfl) + have hword : L.mem s2 (tReplyA d) = r := by + obtain ⟨_, eq⟩ := optRef_some ((E2.at_tcb hd hkd2).recv _ _ _ hst2).1 + exact specPtr_inj eq + unfold sendRecvAfterTransferA + rw [eq_of_spec_fd (obj.Tcb.reply_obj.spec (L := L) s2 d), bind_ok, hword, if_pos (bne_iff_ne.mpr hr0)] + have wu : ipc.reply_unlink st s2 r d ⦃ s3 => WritesIn st s2 s3 + (fun b => (∃ q ∈ R, b = rTcbA q) ∨ b = stA d ∨ MWrA (st := st) Z m σ2 s2 b) ⦄ := by + have h := send_recv_reply_fpA O.to0 F.to0 PE hS1 E2 hW2 hE2 hd hfr2 (fun h => htd (specPtr_inj h)) hkd1 hkd2 hstd hst2 + hrr hq2 r hword.symm + rwa [if_pos (bne_iff_ne.mpr hr0)] at h + refine WP.spec_bind (WP.spec_and (unlinkReplyRelFrameA H2 P PE E2 HS2 hd hr hi2 hkd2 hkr hback + (Or.inr ⟨_, _, hst2⟩) hq2) wu) ?_ + rintro s3 ⟨⟨σ3, hU, H3, E3, S3, I3, V3⟩, w3⟩ + obtain ⟨hkt3, hkd3, hq3, keep3⟩ := sendUnlinkFactsA htd hkt2 hkd2 hkr hback hst2 hq2 hU + obtain ⟨hsf3, hsd3⟩ := pushAfterBlockScWordsA H3 ht hd hsc0 hni hkt3 hkd3 htSpec hdSpec2 + have hfon3 : L.mem s3 (affA t) = L.cpu s3 := by rw [H.aff_eq H3 ht ht, H.cpu_eq H3, hfon] + have hon3 : L.mem s3 (affA d) = L.cpu s3 := by rw [H.aff_eq H3 hd hd, H.cpu_eq H3, hon] + have hmig3 : L.mem s3 (affA d) = L.mem s3 (scCoreA sc) := by + rw [H.aff_eq H3 hd hd, H3.scCore sc hc, ← H.scCore sc hc, hmig] + have hRun3 := hRun.keep ((SchedKeepA.step hE).trans keep3) + have spec := sendIpc_recv_pushSpecA hk hkd hde hstd blk call badge.bv g gr true hT hU hkt3 hkd3 hcf hg + refine WP.spec_mono (sendRecvDonateAfterUnlinkA a H3 P PE E3 S3 hc hsc0 hr I3 ht hd htd hkt3 hkd3 + hsf3 hsd3 hfon3 hon3 hmig3 ((V3 t ht).trans hv2) hni hRun3 rfl hq3 call g gr hcf hg + (fun σ4 hp => hReleased σ2 σ3 σ4 hT hU hp)) ?_ + rintro out ⟨ha, σ', hP, E', H', S', w4⟩ + have hz2 : SSzA σ σ2 := ssz_of_epStep hE + have hz3 : SSzA σ σ3 := hz2.trans (sszRunA hU) + have w03 : WritesIn st s s3 (DonFpA st Z m R Bf σ s t e d) := + (WritesIn.trans (st := st) hfp w3).mono fun b h => by + rcases h with h | (h | h | h) + · exact Or.inl (Or.inl h) + · exact Or.inl (Or.inr (Or.inl h)) + · exact Or.inl (Or.inr (Or.inr (Or.inr (Or.inl h)))) + · exact Or.inl (Or.inr (Or.inr (Or.inr (Or.inr (mwr_back rfl rfl hz2 hfp.1 h))))) + refine ⟨ha, σ', spec.trans hP, E', H', S', (WritesIn.trans (st := st) w03 w4).mono fun b h => ?_⟩ + rcases h with h | (h | h | h | h | h) + · exact h + · exact Or.inr (Or.inl h) + · exact Or.inl (Or.inr (Or.inr (Or.inl ⟨trivial, h⟩))) + · exact Or.inr (Or.inr h) + · exact Or.inl (Or.inr (Or.inr (Or.inr (Or.inl h)))) + · exact Or.inl (Or.inr (Or.inr (Or.inr (Or.inr (mwr_back rfl rfl hz3 w03.1 h))))) + +/-- `send_ipc_recv_pushDonate_fpA` without the write set. -/ +theorem send_ipc_recv_pushDonateA {s : S} {σ : State Unit} (a : A) {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {kb : U64} (hPl : PlaceA Z.th kb) (hSt : StateRelA Z.th (L.mem s) σ) (hVld : SlotsValidA Z.th (L.mem s)) + (hC : BufCover st Z.T Bf s σ) (hNative : BufNativeA Z.T (L.mem s)) + (hoff : ∀ y, IsSlotA Z.th y → ¬ EWrite Z.T E R Bf y ∧ + ¬ EWrite Z.T E R Bf (core.num.U64.wrapping_add y 8#u64)) + {t : U64} (ht : t ∈ Z.T) {xt : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hRun : SndRunA σ (specPtr t) xt) + {e d : U64} (he : e ∈ E) {q : List ObjRef} + (hk : σ.kheap (specPtr e) = some (.endpoint (.recvEP (specPtr d :: q)))) + (hd : d ∈ Z.T) {xd : Tcb} (hkd : σ.kheap (specPtr d) = some (.tcb xd)) + (hu : ∀ rp, ipcBufOfA true xd = some rp → ∀ k, k < 128 → inUserFrame (bufAddr rp k) σ) + (hrep : RepOKA σ (specPtr d)) (badge : U64) (g gr blk call : Bool) + (hcase : (xt.tcbFault = none ∧ (g = false ∨ ipcBufOfA false xt = none ∨ + unat (dataToMessageInfo (xt.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0)) ∨ + ∃ fault, xt.tcbFault = some fault ∧ FaultOKA (L := L) s t (ipcBufOfA true xd) xt fault) + {sc r : U64} (hc : sc ∈ Z.C) (hsc0 : sc ≠ 0#u64) (hr : r ∈ R) (hi : ReplyIsolatedA (L.mem s) Z.C stack r) + (hfs : L.mem s (scA t) = sc) (hd0 : L.mem s (scA d) = 0#u64) + (hfon : L.mem s (affA t) = L.cpu s) (hon : L.mem s (affA d) = L.cpu s) + (hmig : L.mem s (affA d) = L.mem s (scCoreA sc)) + (hV : L.mem s (core.num.U64.wrapping_add t 3664#u64) = 0#u64) + (hni : NotIdleScA m (scNameA m sc)) {dd : ReceiverPayload} + (hRecv : xd.tcbState = .blockedOnReceive (specPtr e) (some (specPtr r)) dd) + (hcf : call = true ∨ xt.tcbFault ≠ none) (hg : g = true ∨ gr = true) + (hReleased : ∀ σ2 σ3 σ4, + doIpcTransfer (Z := Unit) (specPtr t) (some (specPtr e)) badge.bv g (specPtr d) + (updK σ (specPtr e) (.endpoint (epDeq (.recvEP (specPtr d :: q)) (specPtr d)))) = ({((), σ2)}, False) → + replyUnlinkTcb (Z := Unit) (specPtr d) (specPtr r) σ2 = ({((), σ3)}, False) → + replyPush (Z := Unit) (specPtr t) (specPtr d) (specPtr r) true σ3 = ({((), σ4)}, False) → + RelIfBoundSc σ4 (specPtr d)) : + endpoint.send_ipc st ar s a blk call badge g gr true t e ⦃ out => out.2 = a ∧ + ∃ σ', sendIpc (Z := Unit) blk call badge.bv g gr true (specPtr t) (specPtr e) σ = ({((), σ')}, False) ∧ + EpStateRel Z.T E R Bf (L.mem out.1) σ' ∧ IpcSchedRelA st Z m κ out.1 σ' ∧ + ReplyStackRelA (L.mem out.1) R Z.C σ' (scNameA m) (Function.update stack sc (r :: stack sc)) ⦄ := + WP.spec_mono (send_ipc_recv_pushDonate_fpA a H P PE HE HS hPl hSt hVld hC hNative hoff ht hkt hRun he hk hd hkd hu hrep badge g gr blk call + hcase hc hsc0 hr hi hfs hd0 hfon hon hmig hV hni hRecv hcf hg hReleased) + fun _ ⟨ha, σ', hs, E', H', S', _⟩ => ⟨ha, σ', hs, E', H', S'⟩ + +set_option maxHeartbeats 400000 in +/-- Send to a waiting receiver with a reply object **that has its own SC** (a call with grant or grant-reply): +the reply is pushed without a donation (`canDonate` is set but the receiver has an SC), the receiver is woken, and +the reply stacks are unchanged. Write set: `DonFpA`. -/ +theorem send_ipc_recv_pushNoDon_fpA {s : S} {σ : State Unit} (a : A) {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {kb : U64} (hPl : PlaceA Z.th kb) (hSt : StateRelA Z.th (L.mem s) σ) (hVld : SlotsValidA Z.th (L.mem s)) + (hC : BufCover st Z.T Bf s σ) (hNative : BufNativeA Z.T (L.mem s)) + (hoff : ∀ y, IsSlotA Z.th y → ¬ EWrite Z.T E R Bf y ∧ + ¬ EWrite Z.T E R Bf (core.num.U64.wrapping_add y 8#u64)) + {t : U64} (ht : t ∈ Z.T) {xt : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hRun : SndRunA σ (specPtr t) xt) + {e d : U64} (he : e ∈ E) {q : List ObjRef} + (hk : σ.kheap (specPtr e) = some (.endpoint (.recvEP (specPtr d :: q)))) + (hd : d ∈ Z.T) {xd : Tcb} (hkd : σ.kheap (specPtr d) = some (.tcb xd)) + (hu : ∀ rp, ipcBufOfA true xd = some rp → ∀ k, k < 128 → inUserFrame (bufAddr rp k) σ) + (hrep : RepOKA σ (specPtr d)) (badge : U64) (g gr blk call : Bool) + (hcase : (xt.tcbFault = none ∧ (g = false ∨ ipcBufOfA false xt = none ∨ + unat (dataToMessageInfo (xt.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0)) ∨ + ∃ fault, xt.tcbFault = some fault ∧ FaultOKA (L := L) s t (ipcBufOfA true xd) xt fault) + {sc r : U64} (hsc0 : sc ≠ 0#u64) (hr : r ∈ R) (hi : ReplyIsolatedA (L.mem s) Z.C stack r) + (hfs : L.mem s (scA t) = sc) (hdsc : xd.tcbSchedContext ≠ none) + (hon : L.mem s (affA d) = L.cpu s) (hni : NotIdleScA m (scNameA m sc)) {dd : ReceiverPayload} + (hRecv : xd.tcbState = .blockedOnReceive (specPtr e) (some (specPtr r)) dd) + (hcf : call = true ∨ xt.tcbFault ≠ none) (hg : g = true ∨ gr = true) + (hReleased : ∀ σ2 σ3 σ4, + doIpcTransfer (Z := Unit) (specPtr t) (some (specPtr e)) badge.bv g (specPtr d) + (updK σ (specPtr e) (.endpoint (epDeq (.recvEP (specPtr d :: q)) (specPtr d)))) = ({((), σ2)}, False) → + replyUnlinkTcb (Z := Unit) (specPtr d) (specPtr r) σ2 = ({((), σ3)}, False) → + replyPush (Z := Unit) (specPtr t) (specPtr d) (specPtr r) true σ3 = ({((), σ4)}, False) → + RelIfBoundSc σ4 (specPtr d)) : + endpoint.send_ipc st ar s a blk call badge g gr true t e ⦃ out => out.2 = a ∧ + ∃ σ', sendIpc (Z := Unit) blk call badge.bv g gr true (specPtr t) (specPtr e) σ = ({((), σ')}, False) ∧ + EpStateRel Z.T E R Bf (L.mem out.1) σ' ∧ IpcSchedRelA st Z m κ out.1 σ' ∧ + ReplyStackRelA (L.mem out.1) R Z.C σ' (scNameA m) stack ∧ + WritesIn st s out.1 (DonFpA st Z m R Bf σ s t e d) ⦄ := by + let O := schedOpsEAOfPlacementA (st := st) (m := m) (κ := κ) P + have htSpec : xt.tcbSchedContext = some (scNameA m sc) := by + rw [schedTcbScA H ht hkt, hfs, optScA, if_neg hsc0] + rfl + refine send_ipc_recvContA O PE hPl s a σ HE ⟨Z.Q, Z.R, H⟩ hSt hVld hC hNative hoff ht + ((H.thT t).2 ht) hkt (runnable_codeA hRun.1).1 he hk ((H.thT d).2 hd) hkd hu hrep + badge g gr blk call true hcase ?_ + intro s1' s2 σ2 x2 rep dd' σ1' _ htd hde hS1 E2 hS2 hW hE hW2 hE2 hfr2 hkt2 hkd1 hkd2 hstd hst2 hrr hq2 hT hfp + have F : SchedFootEA st Z.T E R Bf (IpcSchedRelA st Z m κ) (fun σ s => MWrA (st := st) Z m σ s) := + schedFootEA_of_sched + have hrep' : rep = some (specPtr r) := by + have eq := hstd.symm.trans hRecv + injection eq + subst rep + obtain ⟨Q2, R2, H2⟩ := hS2 + obtain ⟨xr, hkr, hback⟩ := hrr _ rfl + have HS2 := endpointStackFrameA H.toSchedSRN P PE HE HS hW hE + have hdsc2 : x2.tcbSchedContext ≠ none := by + rw [epStepTcbScA hE hkd hkd2]; exact hdsc + have hr0 : r ≠ 0#u64 := fun eq => (PE.rep r hr).2.1 (by rw [eq]; rfl) + have hword : L.mem s2 (tReplyA d) = r := by + obtain ⟨_, eq⟩ := optRef_some ((E2.at_tcb hd hkd2).recv _ _ _ hst2).1 + exact specPtr_inj eq + unfold sendRecvAfterTransferA + rw [eq_of_spec_fd (obj.Tcb.reply_obj.spec (L := L) s2 d), bind_ok, hword, if_pos (bne_iff_ne.mpr hr0)] + have wu : ipc.reply_unlink st s2 r d ⦃ s3 => WritesIn st s2 s3 + (fun b => (∃ q ∈ R, b = rTcbA q) ∨ b = stA d ∨ MWrA (st := st) Z m σ2 s2 b) ⦄ := by + have h := send_recv_reply_fpA O.to0 F.to0 PE hS1 E2 hW2 hE2 hd hfr2 (fun h => htd (specPtr_inj h)) hkd1 hkd2 hstd hst2 + hrr hq2 r hword.symm + rwa [if_pos (bne_iff_ne.mpr hr0)] at h + have hi2 := endpointKeepsIsolatedA H.toSchedSRN P PE hr hi hW + refine WP.spec_bind (WP.spec_and (unlinkReplyRelFrameA H2 P PE E2 HS2 hd hr hi2 hkd2 hkr hback + (Or.inr ⟨_, _, hst2⟩) hq2) wu) ?_ + rintro s3 ⟨⟨σ3, hU, H3, E3, S3, I3, V3⟩, w3⟩ + obtain ⟨hkt3, hkd3, hq3, keep3⟩ := sendUnlinkFactsA htd hkt2 hkd2 hkr hback hst2 hq2 hU + have hsf3 : L.mem s3 (scA t) = sc := by + exact pushAfterBlockScWordFA H3 ht hsc0 hni hkt3 htSpec + have hon3 : L.mem s3 (affA d) = L.cpu s3 := by rw [H.aff_eq H3 hd hd, H.cpu_eq H3, hon] + have hRun3 := hRun.keep ((SchedKeepA.step hE).trans keep3) + have spec := sendIpc_recv_pushSpecA hk hkd hde hstd blk call badge.bv g gr true hT hU hkt3 hkd3 hcf hg + refine WP.spec_mono (sendRecvNoDonAfterUnlinkA a H3 P PE E3 S3 hsc0 hr ht hd htd hkt3 hkd3 + hsf3 hdsc2 hon3 hRun3 rfl hq3 call g gr hcf hg + (fun σ4 hp => hReleased σ2 σ3 σ4 hT hU hp)) ?_ + rintro out ⟨ha, σ', hP, E', H', S', w4⟩ + have hz2 : SSzA σ σ2 := ssz_of_epStep hE + have hz3 : SSzA σ σ3 := hz2.trans (sszRunA hU) + have w03 : WritesIn st s s3 (DonFpA st Z m R Bf σ s t e d) := + (WritesIn.trans (st := st) hfp w3).mono fun b h => by + rcases h with h | (h | h | h) + · exact Or.inl (Or.inl h) + · exact Or.inl (Or.inr (Or.inl h)) + · exact Or.inl (Or.inr (Or.inr (Or.inr (Or.inl h)))) + · exact Or.inl (Or.inr (Or.inr (Or.inr (Or.inr (mwr_back rfl rfl hz2 hfp.1 h))))) + refine ⟨ha, σ', spec.trans hP, E', H', S', (WritesIn.trans (st := st) w03 w4).mono fun b h => ?_⟩ + rcases h with h | (h | h | h | h | h) + · exact h + · exact Or.inr (Or.inl h) + · exact Or.inl (Or.inr (Or.inr (Or.inl ⟨trivial, h⟩))) + · exact Or.inr (Or.inr h) + · exact Or.inl (Or.inr (Or.inr (Or.inr (Or.inl h)))) + · exact Or.inl (Or.inr (Or.inr (Or.inr (Or.inr (mwr_back rfl rfl hz3 w03.1 h))))) + +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/IpcScSendRunningA.lean b/proofs/Fl4ProofsA64/P3/IpcScSendRunningA.lean new file mode 100644 index 000000000..6af8bc275 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/IpcScSendRunningA.lean @@ -0,0 +1,79 @@ +/- +P3 (AArch64): Set the receiver running after a reply push. +-/ +import Fl4ProofsA64.P3.IpcScEndpointRefillA +import Fl4ProofsA64.P3.IpcScPopA + +open Aeneas Aeneas.Std Result +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4SpecA64 Sched +noncomputable section + +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + +/-- The named state produced by setting the receiver running. -/ +def sendPushRunningStateA (σ : State Unit) (d : U64) (x : Tcb) : State Unit := + stsAct (updK σ (specPtr d) (.tcb { x with tcbState := .running })) (specPtr d) + +theorem sendPushRunningSpecA {σ : State Unit} {d : U64} {x : Tcb} + (hk : σ.kheap (specPtr d) = some (.tcb x)) : + setThreadState (Z := Unit) (specPtr d) .running σ = ({((), sendPushRunningStateA σ d x)}, False) := + setThreadState_eq .running hk + +theorem sendPushRunningHeapA (σ : State Unit) (d : U64) (x : Tcb) : + (sendPushRunningStateA σ d x).kheap (specPtr d) = some (.tcb { x with tcbState := .running }) := by + rw [sendPushRunningStateA, stsAct_kheap, updK_kheap, Function.update_self] + +theorem sendPushRunningNotQueuedA {σ : State Unit} {d : U64} (x : Tcb) (h : NotQueued σ (specPtr d)) : + NotQueued (sendPushRunningStateA σ d x) (specPtr d) := + NotQueued.sts h (specPtr d) (.tcb { x with tcbState := .running }) (specPtr d) + +set_option maxHeartbeats 25000 in +private theorem sendPushSetRunningRawA {s : S} {σ : State Unit} {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {d : U64} (hd : d ∈ Z.T) {x : Tcb} (hk : σ.kheap (specPtr d) = some (.tcb x)) + (hst : runnable x.tcbState = false ∧ x.tcbState ≠ .idleThreadState) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr d ∉ z.epQueue) + (hb : RelIfBoundSc σ (specPtr d)) : + sched.set_thread_state st s d sched.TS_RUNNING ⦃ s' => + let post := stsAct (updK σ (specPtr d) (.tcb { x with tcbState := .running })) (specPtr d) + SchedSRA st Z m κ s' post ∧ EpStateRel Z.T E R Bf (L.mem s') post ∧ + ReplyStackRelA (L.mem s') R Z.C post (scNameA m) stack ⦄ := by + unfold sched.set_thread_state + refine WP.spec_bind (obj.Tcb.set_state.spec s d sched.TS_RUNNING) ?_ + rintro s1 rfl + obtain ⟨E1, W1, K1⟩ := write_state_rel PE s σ HE hd hk hq .running (by decide) sched.TS_RUNNING (by unfold sched.TS_RUNNING; rfl) + have nIdle : x.tcbState ≠ .idleThreadState := hst.2 + have nb := NoBlockE.updT hk { x with tcbState := .running } (Or.inr (Or.inl ⟨rfl, nIdle, Or.inr hb⟩)) + have H1 := H.stable W1 (fun _ b => endpWriteOffRead H.toSchedSRN P b) (endpSchedStep K1) nb + have S1 := endpointStackFrameA H.toSchedSRN P PE HE HS W1 K1 + refine WP.spec_mono (schedule_tcb_refinesA H1 d (popStateWords E1)) ?_ + rintro s' ⟨w, H'⟩ + have hHeap := stsAct_kheap (updK σ (specPtr d) (.tcb { x with tcbState := .running })) (specPtr d) + have F : MFrA (st := st) Z m _ s' _ _ := + ⟨w.mono (fun _ eq => eq.symm ▸ swAction H1), SKeepA.of_kheap hHeap, SSzA.of_kheap hHeap⟩ + have hMem := (KView.stsAct' (updK σ (specPtr d) (.tcb { x with tcbState := .running })) (specPtr d)).2.2.2 + obtain ⟨foot, view⟩ := epFrameOfMcsA H1.toSchedSRN P F hMem + exact ⟨H', E1.frame PE foot view, + mcsReplyFrameA H1.toSchedSRN P PE E1 S1 F (fun p y n h => ⟨y, (congrFun hHeap p).trans h, rfl⟩)⟩ + + +theorem sendPushSetRunningA {s : S} {σ : State Unit} {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {d : U64} (hd : d ∈ Z.T) {x : Tcb} (hk : σ.kheap (specPtr d) = some (.tcb x)) + (hst : runnable x.tcbState = false ∧ x.tcbState ≠ .idleThreadState) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr d ∉ z.epQueue) + (hb : RelIfBoundSc σ (specPtr d)) : + sched.set_thread_state st s d sched.TS_RUNNING ⦃ s' => + let post := sendPushRunningStateA σ d x + SchedSRA st Z m κ s' post ∧ EpStateRel Z.T E R Bf (L.mem s') post ∧ + ReplyStackRelA (L.mem s') R Z.C post (scNameA m) stack ⦄ := by + dsimp only [sendPushRunningStateA] + exact sendPushSetRunningRawA H P PE HE HS hd hk hst hq hb + + +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/IpcScSendSwitchA.lean b/proofs/Fl4ProofsA64/P3/IpcScSendSwitchA.lean new file mode 100644 index 000000000..2d49d863f --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/IpcScSendSwitchA.lean @@ -0,0 +1,60 @@ +/- +P3 (AArch64): Frame the reply stack through the refill check and switch. +-/ +import Fl4ProofsA64.P3.IpcScEndpointRefillA +import Fl4ProofsA64.P3.IpcScPopA + +open Aeneas Aeneas.Std Result +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4SpecA64 Sched +noncomputable section + +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + +set_option maxHeartbeats 25000 in +theorem sendPushSwitchA {s : S} {σ : State Unit} {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {d : U64} (hd : d ∈ Z.T) (hon : L.mem s (affA d) = L.cpu s) + {x : Tcb} (hk : σ.kheap (specPtr d) = some (.tcb x)) (hrun : runnable x.tcbState = true) + (hnq : specPtr d ∉ σ.releaseQueue) : + (do + let s1 ← endpoint.unblock_sporadic st s d + sched.possible_switch_to st s1 d) ⦃ s' => ∃ σ', + specSendTailA (specPtr d) σ = ({((), σ')}, False) ∧ + EpStateRel Z.T E R Bf (L.mem s') σ' ∧ IpcSchedRelA st Z m κ s' σ' ∧ + ReplyStackRelA (L.mem s') R Z.C σ' (scNameA m) stack ⦄ := by + rw [endpoint_unblock_sporadic_eqA] + refine WP.spec_bind (unblock_sporadic_SRA H hd true (fun h => (hnq h).elim)) ?_ + rintro s1 ⟨σ1, hA, H1, F1⟩ + obtain ⟨hon1, hst1, _, hTcb⟩ := F1.switch_pre H hd hon (popStateWords HE) + have hRun1 : ∀ y, σ1.kheap (specPtr d) = some (.tcb y) → runnable y.tcbState = true := by + intro y hy + have eq := KernelObject.tcb.inj (Option.some.inj ((hTcb y hy).symm.trans hk)) + exact eq.symm ▸ hrun + obtain ⟨y, hy, hSC⟩ := H.sch.tcb d hd + have hSCx : x.tcbSchedContext = optScA m (L.mem s (scA d)) := + (congrArg Tcb.tcbSchedContext (KernelObject.tcb.inj (Option.some.inj (hk.symm.trans hy)))).trans hSC + have keep := condUnblockReplyListsA true (o := x.tcbSchedContext) (σ' := σ1) + (fun p hp => by obtain ⟨z, n, hz, _⟩ := H.inv.back (specPtr d) x p hk hp; exact ⟨z, n, hz⟩) + (by rw [hSCx, hA]; exact Set.mem_singleton _) + refine WP.spec_mono (possible_switch_to_refinesA H1 d hd hon1 hRun1 hst1) ?_ + rintro s' ⟨σ', Q', hS, hh, w, H'⟩ + have F := (F1.mfr H.toSchedSRN).trans (MFrA.of_sw hh w) rfl rfl + have hsMem : σ'.machineState.underlyingMemory = σ1.machineState.underlyingMemory := + switchKeepsUserMemory (specPtr d) σ1 σ' () (by rw [hS]; exact Set.mem_singleton _) + obtain ⟨foot, view⟩ := epFrameOfMcsA H.toSchedSRN P F (hsMem.trans keep.mem) + have heq : specSendTailA (specPtr d) σ = ({((), σ')}, False) := by + rw [specSendTailA_eq] + unfold specRestartTail getTcbObjRef + rw [nd_bind_one (threadGet_eqSc _ hk), hSCx, nd_bind_one hA, hS] + refine ⟨σ', heq, HE.frame PE foot view, ⟨Q', Z.R, H'⟩, + mcsReplyFrameA H.toSchedSRN P PE HE HS F ?_⟩ + intro p z n hz + obtain ⟨z', hz', eq⟩ := keep.sc p z n hz + exact ⟨z', (congrFun hh p).trans hz', eq⟩ + + +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/IpcScSendWakeA.lean b/proofs/Fl4ProofsA64/P3/IpcScSendWakeA.lean new file mode 100644 index 000000000..ce9fd58cb --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/IpcScSendWakeA.lean @@ -0,0 +1,53 @@ +/- +P3 (AArch64): wake the receiver after a donating reply push and preserve +the updated reply stack through the state write, refill check and switch. +-/ +import Fl4ProofsA64.P3.IpcScSendRunningA +import Fl4ProofsA64.P3.IpcScSendSwitchA +import Fl4ProofsA64.P3.IpcScEndpointPushProgramA + +open Aeneas Aeneas.Std Result +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4SpecA64 Sched +noncomputable section + +variable {S A : Type} {st : machine.Store S} [L : LawfulStore st] +variable {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + +attribute [local irreducible] sendPushRunningStateA updK stsAct + +set_option maxHeartbeats 25000 in +/-- The receiver runs after the donation, keeping the new reply stack. -/ +theorem sendWakeAfterPushA {s : S} {σ : State Unit} (a : A) {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {d : U64} (hd : d ∈ Z.T) (hon : L.mem s (affA d) = L.cpu s) + {x : Tcb} (hk : σ.kheap (specPtr d) = some (.tcb x)) (hst : runnable x.tcbState = false ∧ x.tcbState ≠ .idleThreadState) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr d ∉ z.epQueue) + (hb : RelIfBoundSc σ (specPtr d)) : + wakeAfterReplyPushA (st := st) s a d ⦃ out => out.2 = a ∧ ∃ σ', + (setThreadState (specPtr d) .running >>= fun _ => specSendTailA (specPtr d)) σ = ({((), σ')}, False) ∧ + EpStateRel Z.T E R Bf (L.mem out.1) σ' ∧ IpcSchedRelA st Z m κ out.1 σ' ∧ + ReplyStackRelA (L.mem out.1) R Z.C σ' (scNameA m) stack ⦄ := by + have hp := sendPushRunningHeapA σ d x + have hnr : runnable x.tcbState = false := hst.1 + have nq := sendPushRunningNotQueuedA x (H.notQueued hk hnr) + have program : wakeAfterReplyPushA (st := st) s a d = + (do + let s1 ← sched.set_thread_state st s d sched.TS_RUNNING + let s' ← (do + let s2 ← endpoint.unblock_sporadic st s1 d + sched.possible_switch_to st s2 d) + ok (s', a)) := by + simp only [wakeAfterReplyPushA, Std.bind_tc_eq, Std.bind_assoc] + rw [program] + refine WP.spec_bind (sendPushSetRunningA H P PE HE HS hd hk hst hq hb) ?_ + rintro s1 ⟨H1, E1, S1⟩ + have hon1 : L.mem s1 (affA d) = L.cpu s1 := by rw [H.aff_eq H1 hd hd, H.cpu_eq H1, hon] + refine WP.spec_bind (sendPushSwitchA H1 P PE E1 S1 hd hon1 hp rfl nq.2) ?_ + rintro s' ⟨σ', htail, E', H', S'⟩ + rw [WP.spec_ok] + exact ⟨rfl, σ', (nd_bind_one (sendPushRunningSpecA hk) _).trans htail, E', H', S'⟩ + +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/IpcXferCapsDoA.lean b/proofs/Fl4ProofsA64/P3/IpcXferCapsDoA.lean new file mode 100644 index 000000000..501862108 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/IpcXferCapsDoA.lean @@ -0,0 +1,462 @@ +/- +P3 (aarch64, Stage-3 `hcaps`, option (D) / lead ruling pending on the join premise): **`do_ipc_transfer` of a +fault-free sender with any grant and any extra caps**. + +`do_ipc_transfer_relA` (gen 9) requires no extra cap to be looked up. This is the general statement, as a new +theorem; no ratified statement changed. + +* **`to_word_genA`** / **`set_msg_info_genA`**: `to_word` of a message info with extra and unwrapped caps. +* **`extraCaps_getCapA`**: every cap the spec's `lookup_extra_caps` returns is `get_cap` of its slot. +* **`do_ipc_transfer_capsA`**: the extra-cap lookup (`lookup_extra_relA`), `copy_mrs` (`copy_mrs_anyA`), the cap + transfer (`transfer_caps_relA`, with the IPC relations carried by `xcOrth_ipcA`) and the two register writes. + The post gives one spec outcome (membership: `cap_insert` is nondeterministic in the extended state), the + endpoint and scheduler relations, and the state `(s2, σ2)` right after `transfer_caps` with `JAtZAX` at the new + CDT ghost, from which only the receiver's registers are written (`EWrite` words, an `EpStep`); the non-receiver + heap frame `XcKhFrA` and the scheduler queues / idle thread / action `SchedKeepA`. + +Case hypothesis (the join's invariants at the transfer state, no named premise): **`hJ`**, at every state reached +from the pre-state by endpoint writes and an `EpStep` (that is, after `copy_mrs`), `InsBaseA`, the MDB tree and the +null `OS` slots outside `th` hold, and the receive-buffer words are off the footprint and are no cap word. The +reply and endpoint joint theorems carry no `JAtZAX`; Stage-3's `of_invFrame` pass is what discharges it. `hE` / +`hRd`: the slots' cap and MDB words are outside the endpoint footprint and the scheduler reads. +-/ +import Fl4ProofsA64.P3.IpcXferOrthA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3 Fl4ProofsA64.P3.Notif Fl4SpecA64 Sched + +noncomputable section + +section Store +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- `to_word` of a message info with extra and unwrapped caps. -/ +theorem to_word_genA (lab n e u : U64) (hn : n.val < 128) (he : e.val ≤ 3) (hu : u.val < 8) : + msginfo.MessageInfo.to_word { label := lab, caps_unwrapped := u, extra_caps := e, length := n } ⦃ v => + v.bv = Fl4SpecA64.messageInfoToData ⟨n.bv, e.bv, u.bv, lab.bv &&& Fl4SpecA64.mask Fl4SpecA64.msgLabelBits⟩ ⦄ := by + unfold msginfo.MessageInfo.to_word + have hu7 : UScalar.bv u &&& UScalar.bv (7#u64 : U64) = UScalar.bv u := by + apply BitVec.eq_of_toNat_eq + rw [BitVec.toNat_and, show (UScalar.bv (7#u64 : U64)).toNat = 2 ^ 3 - 1 from rfl, + Nat.and_two_pow_sub_one_eq_mod] + exact Nat.mod_eq_of_lt hu + have he3 : UScalar.bv e &&& UScalar.bv (3#u64 : U64) = UScalar.bv e := by + apply BitVec.eq_of_toNat_eq + rw [BitVec.toNat_and, show (UScalar.bv (3#u64 : U64)).toNat = 2 ^ 2 - 1 from rfl, + Nat.and_two_pow_sub_one_eq_mod] + exact Nat.mod_eq_of_lt (by have : (UScalar.bv e).toNat = e.val := rfl; omega) + step* + simp only [Fl4SpecA64.messageInfoToData, mask52A] + have h8 : i8.bv = n.bv := i8_post1.trans ((UScalar.bv_and n 127#u64).symm.trans (and127A hn)) + simp only [UScalar.bv_or, UScalar.bv_and, i7_post1, i4_post1, i6_post1, i5_post1, i3_post1, i2_post1, i1_post1, + i_post1, h8] + have hm : UScalar.bv (4503599627370495#u64 : U64) = (4503599627370495#64) := rfl + erw [hu7, he3, hm] + apply BitVec.eq_of_getLsbD_eq + intro i _ + simp only [BitVec.getLsbD_or, Bool.or_assoc, Bool.or_comm, Bool.or_left_comm] + +theorem set_msg_info_genA (s : S) (t : obj.Tcb) (lab n e u : U64) (hn : n.val < 128) (he : e.val ≤ 3) + (hu : u.val < 8) : + msg.set_msg_info st s t { label := lab, caps_unwrapped := u, extra_caps := e, length := n } ⦃ s' => + ∃ v : U64, s' = L.wr s (regA t layout.reg.MSG_INFO) v ∧ + v.bv = Fl4SpecA64.messageInfoToData ⟨n.bv, e.bv, u.bv, + lab.bv &&& Fl4SpecA64.mask Fl4SpecA64.msgLabelBits⟩ ⦄ := by + unfold msg.set_msg_info + refine WP.spec_bind (to_word_genA lab n e u hn he hu) ?_ + rintro v hv + rw [set_reg_eqA, WP.spec_ok] + exact ⟨v, rfl, hv⟩ + +/-- Every cap the spec's extra-cap lookups return is `get_cap` of its slot. -/ +theorem extraCaps_getCapA {th : TypedHeapA} {s : S} {σ : State Unit} (hR : StateRelA th (L.mem s) σ) + (hV : SlotsValidA th (L.mem s)) {t : U64} (ht : th t = some .tcb) : + ∀ ws : List U64, ∃ e, (Fl4Spec.NonDetE.mapME (xLookA (specPtr t)) + (ws.map fun w => dataToCptr w.bv)).run σ = ({(e, σ)}, False) ∧ + ∀ caps, e = .ok caps → ∀ y ∈ caps, getCap (Z := Unit) y.2 σ = ({(y.1, σ)}, False) + | [] => ⟨.ok [], rfl, fun caps h y hy => by cases h; cases hy⟩ + | w :: ws => by + obtain ⟨r, -, e0, he0, hrel⟩ := spec_existsA (lookup_slot_refinesA th s σ hR hV t ht w) + have he0' : (lookupSlotForThread (Z := Unit) (specPtr t) (dataToCptr w.bv)).run σ = ({(e0, σ)}, False) := he0 + obtain ⟨et, het, hgt⟩ := extraCaps_getCapA hR hV ht ws + cases e0 with + | error lf => + exact ⟨_, mapME_cons_errA (lcs_errA _ he0'), fun caps h => by cases h⟩ + | ok v => + obtain ⟨sl, rest⟩ := v + obtain ⟨hr, hsok⟩ := hrel + obtain ⟨p, hp1, hp, -⟩ := hsok + obtain ⟨a, -, hg⟩ := hR.slot_capA hp1 hp + have hel := lcs_okA (ofBl (dataToCptr w.bv)) he0' hg + cases et with + | error e' => exact ⟨_, mapME_cons_tail_errA hel het, fun caps h => by cases h⟩ + | ok bs => + refine ⟨_, mapME_cons_okA hel het, fun caps h y hy => ?_⟩ + cases h + rcases List.mem_cons.1 hy with rfl | hy + · exact hg + · exact hgt bs rfl y hy + +variable {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS : Set U64} +variable {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + +/-- **Every object but `r`'s is kept**, up to cap slots: unchanged, a CNode of the same radix, or a TCB that differs +only in its cap fields. -/ +def XcKhFrA (r : ObjRef) (σ σ' : State Unit) : Prop := + ∀ p, p ≠ r → σ'.kheap p = σ.kheap p ∨ + (∃ n cs cs', σ.kheap p = some (.cnode n cs) ∧ σ'.kheap p = some (.cnode n cs')) ∨ + ∃ x x', σ.kheap p = some (.tcb x) ∧ σ'.kheap p = some (.tcb x') ∧ tcbNcA x' = tcbNcA x + +/-- **`do_ipc_transfer` of a fault-free sender with extra caps** (module docstring). -/ +theorem do_ipc_transfer_capsA (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + {s : S} {a : A} {σ : State Unit} (hPl : PlaceA Z.th (L.ksb s)) + (hR : EpStateRel Z.T E R Bf (L.mem s) σ) (hSR : IpcSchedRelA st Z m κ s σ) + (hSt : StateRelA Z.th (L.mem s) σ) (hV : SlotsValidA Z.th (L.mem s)) (hC : BufCover st Z.T Bf s σ) + (hNative : BufNativeA Z.T (L.mem s)) + {snd rcv : U64} (hs : snd ∈ Z.T) (hr : rcv ∈ Z.T) (hne : snd ≠ rcv) (hts : Z.th snd = some .tcb) + (htr : Z.th rcv = some .tcb) {xs xr : Tcb} (hks : σ.kheap (specPtr snd) = some (.tcb xs)) + (hkr : σ.kheap (specPtr rcv) = some (.tcb xr)) (hf : xs.tcbFault = none) + (hu : ∀ rp, ipcBufOfA true xr = some rp → ∀ k, k < 128 → inUserFrame (bufAddr rp k) σ) + (hoff : ∀ y, IsSlotA Z.th y → ¬ EWrite Z.T E R Bf y ∧ ¬ EWrite Z.T E R Bf (core.num.U64.wrapping_add y 8#u64)) + (ep badge : U64) (grant : Bool) {epo : Option ObjRef} + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hJ : ∀ s1 σ1, EpStateRel Z.T E R Bf (L.mem s1) σ1 → IpcSchedRelA st Z m κ s1 σ1 → + WritesIn st s s1 (EWrite Z.T E R Bf) → EpStep σ σ1 → + InsBaseA (L := L) AR Z H OS s1 a σ1 m ∧ TreeA st OS Z.G.chains Z.G.par s1 ∧ + (∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s1 y = .Null) ∧ + ∀ bp ∈ Bf, ∀ k : U64, k.val < 128 → + ¬ FootA (L := L) Z s1 (bufW bp.1 k) ∧ ¬ InOS OS (bufW bp.1 k)) + (hep : ∀ s1 (x : U64) (c : Cap), CapRelA (capAtA (L.mem s1) x) c → + (UnwCA ep (capA st s1 x) ↔ SUnwA epo c)) + (hE : ∀ x, W7.CapW H.tcbs {b | InOS OS b} ∅ x → ¬ EFoot Z.T E R Bf x) + (hRd : ∀ s1 σ1, L.ksb s1 = L.ksb s → IpcSchedRelA st Z m κ s1 σ1 → + ∀ x, W7.CapW H.tcbs {b | InOS OS b} ∅ x → ¬ SReadA st Z m s1 σ1 x) : + ipc.do_ipc_transfer st ar s a snd ep badge grant rcv ⦃ s' => ∃ σ', + ((), σ') ∈ (doIpcTransfer (Z := Unit) (specPtr snd) epo badge.bv grant (specPtr rcv) σ).1 ∧ + EpStateRel Z.T E R Bf (L.mem s') σ' ∧ IpcSchedRelA st Z m κ s' σ' ∧ + (∃ s1 σ1 s2 σ2 G', WritesIn st s s1 (EWrite Z.T E R Bf) ∧ EpStep σ σ1 ∧ + JAtZAX (L := L) AR { Z with G := G' } s2 a σ2 m ∧ TreeA st OS G'.chains G'.par s2 ∧ + (∀ n, SInvXA σ1 m n → SInvXA σ2 m n) ∧ WritesIn st s2 s' (EWrite Z.T E R Bf) ∧ EpStep σ2 σ') ∧ + XcKhFrA (specPtr rcv) σ σ' ∧ SchedKeepA σ σ' ∧ + ∃ x', σ'.kheap (specPtr rcv) = some (.tcb x') ∧ + tcbNcA x' = tcbNcA { xr with tcbArch := x'.tcbArch } ⦄ := by + have hO := schedOpsEAOfPlacementA (st := st) (m := m) (κ := κ) P + have hra := PE.tcb rcv hr + have hxs := hR.at_tcb hs hks + have hwmi : (L.mem s (regWA snd layout.reg.MSG_INFO)).bv = xs.tcbArch.tcbContext.userRegs msg_info_register := by + rw [← regIdxA_msgInfo]; exact hxs.regs _ + unfold ipc.do_ipc_transfer + refine WP.spec_bind (lookup_buf_full s σ hSt htr hr hkr hC hNative true) ?_ + rintro rbw ⟨hrw0, hrwB⟩ + refine WP.spec_bind (obj.Tcb.fault_type.spec (L := L) s snd) ?_ + intro ft hft + have hft0 : ft = fl4_abi.consts.fault.NullFault := by + rw [hft, hxs.fault0.2 hf]; unfold fl4_abi.consts.fault.NullFault; rfl + rw [if_pos hft0] + refine WP.spec_bind (lookup_buf_full s σ hSt hts hs hks hC hNative false) ?_ + rintro sw ⟨hsw0, hswB⟩ + unfold ipc.do_normal_transfer + refine WP.spec_bind (msg_info_specA (L := L) s snd) ?_ + rintro tag ⟨hlab, hlen, hext, hle⟩ + rw [hwmi] at hlen hext + set mi := dataToMessageInfo (xs.tcbArch.tcbContext.userRegs msg_info_register) with hmi + have hxv : tag.extra_caps.val = unat mi.miExtraCaps := by + show tag.extra_caps.bv.toNat = _ + rw [hext] + have hx3 : tag.extra_caps.val ≤ 3 := by + have h : mi.miExtraCaps.toNat < 4 := by + rw [hmi] + unfold dataToMessageInfo + rw [BitVec.toNat_and, show (mask 2 : Word).toNat = 2 ^ 2 - 1 from by decide, Nat.and_two_pow_sub_one_eq_mod] + exact Nat.mod_lt _ (by decide) + have : tag.extra_caps.val = mi.miExtraCaps.toNat := hxv + omega + have hBV : BufViewA (L.mem s) σ.machineState.underlyingMemory sw (ipcBufOfA false xs) := by + refine ⟨hsw0, fun sb hsb k hk => ?_⟩ + exact hR.buf (sw, sb) (hswB sb hsb) k hk + -- the extra caps + have hex : (if grant = true then (do + let r ← ipc.lookup_extra_caps st s snd sw tag + match r with + | core.result.Result.Ok e => ok e + | core.result.Result.Err _ => ok ipc.NO_EXTRA_CAPS) + else ok ipc.NO_EXTRA_CAPS) ⦃ e => ∃ caps, + (if grant then Fl4Spec.NonDetE.catch (lookupExtraCaps (Z := Unit) (specPtr snd) (ipcBufOfA false xs) mi) + (fun _ => pure []) + else pure []) σ = ({(caps, σ)}, False) ∧ ExtraOKA Z.th (L.mem s) e caps ∧ + ∀ y ∈ caps, getCap (Z := Unit) y.2 σ = ({(y.1, σ)}, False) ⦄ := by + cases grant with + | false => + simp only [Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + exact ⟨[], rfl, extraOK_noneA _ _, fun _ h => by cases h⟩ + | true => + simp only [↓reduceIte] + refine WP.spec_bind (lookup_extra_relA hSt hV hts sw (ipcBufOfA false xs) hBV tag mi hxv hx3) ?_ + intro r hr' + have catchE : ∀ res, (lookupExtraCaps (Z := Unit) (specPtr snd) (ipcBufOfA false xs) mi).run σ = + ({(res, σ)}, False) → + Fl4Spec.NonDetE.catch (lookupExtraCaps (Z := Unit) (specPtr snd) (ipcBufOfA false xs) mi) + (fun _ => pure []) σ = ({(res.toOption.getD [], σ)}, False) := by + intro res h + unfold Fl4Spec.NonDetE.catch + rw [nd_bind_one' h] + cases res <;> rfl + cases r with + | Err f => + obtain ⟨f', h, -⟩ := hr' + simp only [WP.spec_ok] + exact ⟨[], catchE _ h, extraOK_noneA _ _, fun _ h => by cases h⟩ + | Ok e => + obtain ⟨caps, h, hok⟩ := hr' + simp only [WP.spec_ok] + refine ⟨caps, catchE _ h, hok, ?_⟩ + cases hsbo : ipcBufOfA false xs with + | none => + intro y hy + have h' : (lookupExtraCaps (Z := Unit) (specPtr snd) none mi).run σ = ({(.ok [], σ)}, False) := by + unfold lookupExtraCaps + rw [run_bind_okA (run_liftEA (show getExtraCptrs (Z := Unit) none mi σ = ({([], σ)}, False) from rfl))] + rfl + rw [hsbo, h'] at h + have := congrArg Prod.fst h + simp only [Set.singleton_eq_singleton_iff, Prod.mk.injEq, Except.ok.injEq, and_true] at this + subst this + cases hy + | some sb => + rw [hsbo] at h hBV + have h3 : unat mi.miExtraCaps ≤ 3 := by omega + let ws : List U64 := (List.range' 122 (unat mi.miExtraCaps)).map fun k => + ⟨loadWordAtA σ.machineState.underlyingMemory (bufAddrA sb k)⟩ + have hws : (ws.map fun w => dataToCptr w.bv) = + xCptrsA σ.machineState.underlyingMemory sb (unat mi.miExtraCaps) := by + simp only [ws, xCptrsA, List.map_map]; rfl + obtain ⟨et, het, hgt⟩ := extraCaps_getCapA hSt hV hts ws + rw [hws] at het + have h' : (lookupExtraCaps (Z := Unit) (specPtr snd) (some sb) mi).run σ = ({(et, σ)}, False) := by + unfold lookupExtraCaps + rw [run_bind_okA (run_liftEA (getExtraCptrs_someA (σ := σ) (sb := sb) (mi := mi) h3 (hBV.al sb rfl)))] + exact het + rw [h'] at h + have := congrArg Prod.fst h + simp only [Set.singleton_eq_singleton_iff, Prod.mk.injEq, and_true] at this + exact hgt caps this + refine WP.spec_bind hex ?_ + rintro extra ⟨caps, hcaps, hXO, hgc⟩ + refine WP.spec_bind (copy_mrs_anyA hO PE s σ hR hSR hs hr hne hks hkr (buf_caseA hrw0 hrwB) hu + (buf_caseA hsw0 hswB) tag.length hle) ?_ + rintro ⟨n, s1⟩ ⟨σc, hcopy, hkcf, hRc, hSc, hAc, hn, hWc, hEc⟩ + simp only at hcopy hkcf hRc hSc hAc hn hWc + set x1 := copyRegsTcbA xr xs.tcbArch.tcbContext.userRegs (msg_registers.take tag.length.val) with hx1 + have hkc : σc.kheap (specPtr rcv) = some (.tcb x1) := by rw [hkcf]; simp + have hmem : ∀ y, IsSlotA Z.th y → L.mem s1 y = L.mem s y ∧ + L.mem s1 (core.num.U64.wrapping_add y 8#u64) = L.mem s (core.num.U64.wrapping_add y 8#u64) := by + intro y hy + have hal : Aligned y := by have := (IsSlotA.al32 hPl hy).1; unfold Aligned; omega + exact ⟨hAc y hal (not_rcv_bufA hr hra (hoff y hy).1), + hAc _ (wa_al hal (by simp)) (not_rcv_bufA hr hra (hoff y hy).2)⟩ + have hStc : StateRelA Z.th (L.mem s1) σc := hSt.xferA hPl hmem hkr (copyRegsTcbA_other _ _ _) hkcf + have hVc : SlotsValidA Z.th (L.mem s1) := slotsValid_xferA hV hmem + obtain ⟨B1, T1, hnul1, hbuf1⟩ := hJ s1 σc hRc hSc hWc hEc + -- the extra caps at the transfer state + have hX : ExtraCapsRelA Z.th (L.mem s1) σc extra caps := by + obtain ⟨h3, hsrc, hz⟩ := hXO + have hl : extra.val.length = 3 := by simpa using extra.property + have get : ∀ j, j < 3 → extra.val[j]? = some (exAtA extra j) := fun j hj => by + unfold exAtA + rw [List.getD_eq_getElem _ _ (by omega), List.getElem?_eq_getElem (by omega)] + refine ⟨h3, fun hlt => by rw [get _ hlt, hz _ le_rfl hlt], fun j hj => ?_⟩ + obtain ⟨ps, hps, hp1, hx, hcr⟩ := hsrc j hj + have hA : AbsSlotA Z.th (caps[j]).2 := ⟨ps, hp1, Or.inl hps⟩ + have hS : IsSlotA Z.th (cslotAddrA (caps[j]).2) := AbsSlotA.isSlot hPl hA + have hcm : capAtA (L.mem s1) (cslotAddrA (caps[j]).2) = capAtA (L.mem s) (cslotAddrA (caps[j]).2) := by + have h := hmem _ hS + unfold capAtA + rw [h.1, h.2] + have hkh : σc.kheap (specPtr ps) = σ.kheap (specPtr ps) := by + have hne' : specPtr ps ≠ specPtr rcv := fun e => by + have := specPtr_inj e + rw [this, htr] at hps + cases hps + rw [hkcf, Function.update_of_ne hne'] + have hg0 := hgc _ (List.getElem_mem hj) + have hg1 : getCap (Z := Unit) (caps[j]).2 σc = ({((caps[j]).1, σc)}, False) := by + obtain ⟨cs, hk, hwf, hsl⟩ := hSt.cnode ps _ hps + obtain ⟨a0, ha0, -⟩ := hsl (caps[j]).2.2 rfl + have e2 : (caps[j]).2 = (specPtr ps, (caps[j]).2.2) := by rw [← hp1] + have h0 : getCap (Z := Unit) (caps[j]).2 σ = ({(a0, σ)}, False) := by + rw [e2]; exact getCap_cnodeA hk hwf ha0 + have e : a0 = (caps[j]).1 := getCap_detA h0 hg0 + rw [e2, ← e] + exact getCap_cnodeA (hkh.trans hk) hwf ha0 + refine ⟨?_, hA, by rw [hcm, ← hx]; exact hcr, hg1⟩ + rw [get j (by omega), hx] + -- the transfer + have hlenw : tag.length.bv = mi.miLength := hlen + rw [hlenw] at hcopy + have hspec : ∀ (mi' : MessageInfo) (σ2 : State Unit) (x2 : Tcb), + (mi', σ2) ∈ (transferCaps (Z := Unit) mi caps epo (specPtr rcv) (ipcBufOfA true xr) σc).1 → + σ2.kheap (specPtr rcv) = some (.tcb x2) → + ((), updK (updK σ2 (specPtr rcv) (.tcb (setReg x2 msg_info_register (messageInfoToData + ⟨n.bv, mi'.miExtraCaps, mi'.miCapsUnwrapped, mi.miLabel⟩)))) (specPtr rcv) + (.tcb (setReg (setReg x2 msg_info_register (messageInfoToData + ⟨n.bv, mi'.miExtraCaps, mi'.miCapsUnwrapped, mi.miLabel⟩)) badge_register badge.bv))) ∈ + (doIpcTransfer (Z := Unit) (specPtr snd) epo badge.bv grant (specPtr rcv) σ).1 := by + intro mi' σ2 x2 hm hk2 + unfold doIpcTransfer + rw [nd_bind_one (lookupIpcBuffer_eqA hkr true), nd_bind_one (threadGet_eq _ hks), hf] + dsimp only + rw [nd_bind_one (lookupIpcBuffer_eqA hks false)] + unfold doNormalTransfer + rw [nd_bind_one (getMessageInfo_eqA hks), ← hmi] + cases grant <;> simp only [Bool.false_eq_true, ↓reduceIte] at hcaps ⊢ + all_goals + rw [nd_bind_one hcaps, nd_bind_one hcopy] + refine mem_bindA.2 ⟨(mi', σ2), hm, ?_⟩ + unfold setMessageInfo + rw [nd_bind_one (asUser_setRegister_eq _ _ hk2)] + rw [asUser_setRegister_eq (x := setReg x2 msg_info_register (messageInfoToData + ⟨n.bv, mi'.miExtraCaps, mi'.miCapsUnwrapped, mi.miLabel⟩)) _ _ + (by simp only [updK_kheap, Function.update_self])] + exact Set.mem_singleton _ + -- after the transfer + have fin : ∀ (out : msginfo.MessageInfo) (s2 : S) (mi' : MessageInfo) (σ2 : State Unit) (G' : MdbGhostA), + out.label = tag.label → out.extra_caps.bv = mi'.miExtraCaps → out.caps_unwrapped.bv = mi'.miCapsUnwrapped → + out.extra_caps.val ≤ 3 → out.caps_unwrapped.val < 8 → + (mi', σ2) ∈ (transferCaps (Z := Unit) mi caps epo (specPtr rcv) (ipcBufOfA true xr) σc).1 → + XcPostIpcA st Z m κ E R Bf s1 σc s2 σ2 → JAtZAX (L := L) AR { Z with G := G' } s2 a σ2 m → + TreeA st OS G'.chains G'.par s2 → (∀ nn, SInvXA σc m nn → SInvXA σ2 m nn) → + (do + let s3 ← msg.set_msg_info st s2 rcv { out with length := n } + obj.Tcb.set_reg st rcv s3 layout.reg.BADGE badge) ⦃ s' => ∃ σ', + ((), σ') ∈ (doIpcTransfer (Z := Unit) (specPtr snd) epo badge.bv grant (specPtr rcv) σ).1 ∧ + EpStateRel Z.T E R Bf (L.mem s') σ' ∧ IpcSchedRelA st Z m κ s' σ' ∧ + (∃ s1 σ1 s2 σ2 G', WritesIn st s s1 (EWrite Z.T E R Bf) ∧ EpStep σ σ1 ∧ + JAtZAX (L := L) AR { Z with G := G' } s2 a σ2 m ∧ TreeA st OS G'.chains G'.par s2 ∧ + (∀ nn, SInvXA σ1 m nn → SInvXA σ2 m nn) ∧ WritesIn st s2 s' (EWrite Z.T E R Bf) ∧ EpStep σ2 σ') ∧ + XcKhFrA (specPtr rcv) σ σ' ∧ SchedKeepA σ σ' ∧ + ∃ x', σ'.kheap (specPtr rcv) = some (.tcb x') ∧ + tcbNcA x' = tcbNcA { xr with tcbArch := x'.tcbArch } ⦄ := by + intro out s2 mi' σ2 G' hol hoe hou h3 h8 hm q J T SI + obtain ⟨x2, hk2, e2⟩ := q.tcb _ _ hkc + have e : ({ out with length := n } : msginfo.MessageInfo) = + { label := tag.label, caps_unwrapped := out.caps_unwrapped, extra_caps := out.extra_caps, length := n } := by + rw [← hol] + rw [e] + refine WP.spec_bind (set_msg_info_genA s2 rcv tag.label n _ _ (by omega) h3 h8) ?_ + rintro s3 ⟨v, hs3, hv⟩ + rw [set_reg_eqA, WP.spec_ok] + subst hs3 + have hvx : v.bv = messageInfoToData ⟨n.bv, mi'.miExtraCaps, mi'.miCapsUnwrapped, mi.miLabel⟩ := by + rw [hv, hoe, hou]; congr 2; rw [hlab, hwmi]; rfl + obtain ⟨hR2, hW2, hS2⟩ := write_reg_rel PE s2 _ q.ep hr hk2 msg_info_register v + rw [regIdxA_msgInfo] at hR2 hW2 + have hk3 : (updK σ2 (specPtr rcv) (.tcb (setReg x2 msg_info_register v.bv))).kheap (specPtr rcv) = + some (.tcb (setReg x2 msg_info_register v.bv)) := by simp + obtain ⟨hR3, hW3, hS3⟩ := write_reg_rel PE _ _ hR2 hr hk3 badge_register badge + rw [regIdxA_badge] at hR3 hW3 + have hSR2 := hO.stable _ _ _ _ q.sr hW2 hS2 (NoBlockE.tcbK hk2 _ rfl rfl) + have hSR3 := hO.stable _ _ _ _ hSR2 hW3 hS3 (NoBlockE.tcbK hk3 _ rfl rfl) + have hsp := hspec mi' σ2 x2 hm hk2 + rw [← hvx] at hsp + refine ⟨_, hsp, hR3, hSR3, ⟨s1, σc, s2, σ2, G', hWc, hEc, J, T, SI, + (hW2.trans hW3).mono fun b h => h.elim id id, hS2.trans hS3⟩, ?_, + SchedKeepA.trans (SchedKeepA.trans (SchedKeepA.step hEc) + (⟨q.frame.rq, q.frame.rls, q.frame.idle, Or.inl q.frame.act⟩ : SchedKeepA σc σ2)) + (SchedKeepA.step (hS2.trans hS3)), ?_⟩ + · intro p hp + have e1 : σc.kheap p = σ.kheap p := by rw [hkcf, Function.update_of_ne hp] + have e2' : (updK (updK σ2 (specPtr rcv) (.tcb (setReg x2 msg_info_register v.bv))) (specPtr rcv) + (.tcb (setReg (setReg x2 msg_info_register v.bv) badge_register badge.bv))).kheap p = σ2.kheap p := by + rw [updK_kheap, Function.update_of_ne hp, updK_kheap, Function.update_of_ne hp] + rw [e2', ← e1] + exact q.frame.kh p + refine ⟨_, by rw [updK_kheap, Function.update_self], ?_⟩ + have h1 : ∀ v1 v2, tcbNcA (setReg (setReg x2 msg_info_register v1) badge_register v2) = + { tcbNcA x2 with tcbArch := (setReg (setReg x2 msg_info_register v1) badge_register v2).tcbArch } := + fun _ _ => rfl + rw [h1, e2, show x1 = { xr with tcbArch := x1.tcbArch } from copyRegsTcbA_other _ _ _] + rfl + have hmil : mi.miLabel = tag.label.bv := by + have h : mi.miLabel = (xs.tcbArch.tcbContext.userRegs msg_info_register >>> 12) &&& mask msgLabelBits := by + rw [hmi]; rfl + rw [h, hlab, hwmi] + apply BitVec.eq_of_toNat_eq + rw [BitVec.toNat_and, show (mask msgLabelBits : Word).toNat = 2 ^ 52 - 1 from by decide, + Nat.and_two_pow_sub_one_eq_mod, BitVec.toNat_ushiftRight, Nat.shiftRight_eq_div_pow] + have := (xs.tcbArch.tcbContext.userRegs msg_info_register).isLt + exact Nat.mod_eq_of_lt (by omega) + cases hrbo : ipcBufOfA true xr with + | none => + have hr0 : rbw = 0#u64 := hrw0.2 hrbo + have hl0 : (0#usize : Usize).val < extra.val.length := by rw [extra.property]; simp + obtain ⟨x0, hx0', -⟩ := spec_existsA (Aeneas.Std.Array.index_usize_spec extra 0#usize hl0) + have htc : ipc.transfer_caps st s1 tag extra ep rcv rbw ⦃ r => + r = ({ label := tag.label, caps_unwrapped := 0#u64, extra_caps := 0#u64, length := tag.length }, s1) ⦄ := by + unfold ipc.transfer_caps + simp [msginfo.MessageInfo.new, bind_ok, hx0', obj.Slot.is_null, hr0] + refine WP.spec_bind htc ?_ + rintro ⟨out, s2⟩ hout + simp only [Prod.mk.injEq] at hout + obtain ⟨ho, hs2⟩ := hout + subst ho + rw [hs2] + show (do + let s3 ← msg.set_msg_info st s1 rcv + { ({ label := tag.label, caps_unwrapped := 0#u64, extra_caps := 0#u64, length := tag.length } : + msginfo.MessageInfo) with length := n } + obj.Tcb.set_reg st rcv s3 layout.reg.BADGE badge) ⦃ _ ⦄ + refine fin { label := tag.label, caps_unwrapped := 0#u64, extra_caps := 0#u64, length := tag.length } s1 + ⟨mi.miLength, 0, 0, mi.miLabel⟩ σc Z.G rfl rfl rfl (by scalar_tac) (by scalar_tac) ?_ + ⟨hRc, hSc, fun _ x h => ⟨x, h, rfl⟩, rfl, SpecSlotA.refl _⟩ B1.j T1 (fun _ h => h) + rw [hrbo] + unfold transferCaps + rw [nd_bind_one (show getReceiveSlots (Z := Unit) (specPtr rcv) none σc = ({([], σc)}, False) from rfl), + nd_pure_bind'] + exact Set.mem_singleton _ + | some rb => + have hm := hrwB rb hrbo + have hb0 : rbw ≠ 0#u64 := fun h => by + have := hrw0.1 h + rw [hrbo] at this + cases this + have hbu : ∀ k, k < 128 → inUserFrame (bufAddr rb k) σc := by + intro k hk + obtain ⟨sz, hsz⟩ := hu rb hrbo k hk + refine ⟨sz, ?_⟩ + rw [hkcf, Function.update_of_ne] + · exact hsz + · intro e + rw [e, hkr] at hsz + cases hsz + have hks1 : L.ksb s1 = L.ksb s := hWc.1 + have hQ := xcOrth_ipcA (st := st) (m := m) (κ := κ) (H := H) (OS := OS) (s0 := s1) (σ0 := σc) P PE hm hE + (fun s' σ' h hsr => hRd s' σ' (h.trans hks1) hsr) + have hX' := hX + refine WP.spec_bind (transfer_caps_relA (st := st) B1 T1 hsub hnul1 hX' + (fun j hj => by + refine hep s1 _ _ ?_ + exact (hX.2.2 j hj).2.2.1) + (info := tag) (mi := mi) hlenw hmil.symm htr hb0 + (bufAddr_align (hRc.buf (rbw, rb) hm 0#u64 (by decide)).2 0 (by omega)) + (fun k hk => (hRc.buf (rbw, rb) hm k hk).1) hbu + (fun k hk => (hbuf1 (rbw, rb) hm k hk).1) (fun k hk => (hbuf1 (rbw, rb) hm k hk).2) + (fun k hk => PE.balign _ ⟨(rbw, rb), hm, k, hk, rfl⟩) hQ ⟨hRc, hSc, fun _ x h => ⟨x, h, rfl⟩, rfl, SpecSlotA.refl _⟩) ?_ + rintro ⟨out, s2⟩ ⟨mi', σ2, G', hmt, -, hoe, hou, hol, J, T, SI, q, h3, h8, hlbl⟩ + show (do + let s3 ← msg.set_msg_info st s2 rcv { out with length := n } + obj.Tcb.set_reg st rcv s3 layout.reg.BADGE badge) ⦃ _ ⦄ + have hol' : out.label = tag.label := by + apply UScalar.eq_of_val_eq + show out.label.bv.toNat = tag.label.bv.toNat + rw [hol, hlbl, hmil] + refine fin out s2 mi' σ2 G' hol' hoe hou h3 (by + show out.caps_unwrapped.bv.toNat < 8 + rw [hou]; exact h8) (by rw [hrbo]; exact hmt) q J T SI + +end Store + +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/IpcXferDeriveA.lean b/proofs/Fl4ProofsA64/P3/IpcXferDeriveA.lean new file mode 100644 index 000000000..877a4bedc --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/IpcXferDeriveA.lean @@ -0,0 +1,82 @@ +/- +P3 (aarch64, Stage-3 `IpcExtraCapTransferA` R2 item 2): **`derive_cap` against l4v `derive_cap` at a related +spec cap** (`CapRelA`), and what a successful spec derivation tells `cap_insert_dBA`. + +* `DeriveRelEqA`: `DeriveRelA` with `CapEqA` for the result (a mapped Stage-2 page-table cap's spec cap is + related, not equal, to `absCapA`). +* **`derive_relEqA`**: `CnodeCapsA.derive_relA` (every kind; an untyped cap under `NoChildrenRelA`) at any + `CapRelA`-related spec cap; the mapped page-table cap derives to itself on both sides. +* **`deriveOk_ofA`**: a non-null outcome of l4v `derive_cap` comes from no zombie, keeps a badge, and an untyped + source is unchanged and has no `cdt` child (`CopyOkA`, as `copyOk_ofA` for the unmasked cap). + +Case hypotheses only; no named premise. New module; no existing statement changed. +-/ +import Fl4ProofsA64.P3.IpcXferLoopSpecA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3 Fl4SpecA64 + +noncomputable section + +/-- `DeriveRelA` with the result's spec cap `CapEqA`-related (module docstring). -/ +def DeriveRelEqA (c : capability.Cap) : core.result.Result capability.Cap error.Exc → + Except SyscallError Cap → Prop + | .Ok c', .ok a => CapWfA c' ∧ CapEqA c' a ∧ CapRt.EncWf c' ∧ (∀ th, CapValidA th c → CapValidA th c') ∧ + SameBadgeKindA c' c ∧ (c' ≠ .Null → shapeA c' = shapeA c) + | .Err e, .error e' => SysErrRelA e e' + | _, _ => False + +theorem deriveRelEq_of_absA {c : capability.Cap} {r : core.result.Result capability.Cap error.Exc} + {r' : Except SyscallError Cap} (h : DeriveRelA c r r') : DeriveRelEqA c r r' := by + cases r <;> cases r' <;> simp only [DeriveRelA, DeriveRelEqA] at h ⊢ + · exact ⟨h.1, CapEqA.of_abs h.2.1, h.2.2⟩ + all_goals exact h + +section +variable {S : Type} {st : machine.Store S} + +/-- **`derive_cap` against l4v `derive_cap` at a related spec cap** (module docstring). -/ +theorem derive_relEqA (s : S) (σ : State Unit) (slot : obj.Slot) (sl : CslotPtr) (c : capability.Cap) (a : Cap) + (hca : CapRelA c a) (he : CapRt.EncWf c) + (hnc : isUtA c = true → NoChildrenRelA (st := st) s σ slot sl) : + ∃ r, objects.derive_cap st s slot c = ok r ∧ ∃ r', + (deriveCap (Z := Unit) sl a).run σ = ({(r', σ)}, False) ∧ DeriveRelEqA c r r' := by + obtain ⟨r, hr, r', hr', hrel⟩ := derive_relA s σ slot sl c hca.1 he (fun h => hnc (by + revert h; cases c <;> simp [isUtA])) + have hgen : absCapA c = a → ∃ r, objects.derive_cap st s slot c = ok r ∧ ∃ r', + (deriveCap (Z := Unit) sl a).run σ = ({(r', σ)}, False) ∧ DeriveRelEqA c r r' := by + rintro rfl + exact ⟨r, hr, r', hr', deriveRelEq_of_absA hrel⟩ + cases c with + | PageTable p m asid va => + cases m + · exact hgen hca.2 + · obtain ⟨asid', va', rfl, -, -⟩ := CapEqA.pt_mapped_iff.mp hca.2 + exact ⟨_, rfl, _, rfl, hca.1, hca.2, he, fun _ h => h, trivial, fun _ => rfl⟩ + | _ => exact hgen hca.2 + +end + +/-- **A non-null spec derivation** (module docstring). -/ +theorem deriveOk_ofA {σ : State Unit} {sl : CslotPtr} {a an : Cap} + (hd : (deriveCap (Z := Unit) sl a).run σ = ({(.ok an, σ)}, False)) (hn : an ≠ .nullCap) : + CopyOkA σ sl a an := by + refine ⟨fun p n z e => ?_, fun r b rs e _ => ?_, fun dv p b i e => ?_⟩ + · subst e + exact hn (run_single_okA hd).symm + · rcases e with rfl | rfl + · rw [← run_single_okA hd]; rfl + · rw [← run_single_okA hd]; rfl + · subst e + unfold Fl4SpecA64.deriveCap at hd + rw [ExceptT.run_bind, nd_bind_singleA (ensureNoChildren_runA σ sl)] at hd + by_cases hc : ∃ x, σ.cdt x = some sl + · simp only [hc, ↓reduceIte] at hd + exact (run_single_errA hd).elim + · simp only [hc, ↓reduceIte] at hd + exact ⟨(run_single_okA hd).symm, hc⟩ + +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/IpcXferLoopA.lean b/proofs/Fl4ProofsA64/P3/IpcXferLoopA.lean new file mode 100644 index 000000000..ced67a572 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/IpcXferLoopA.lean @@ -0,0 +1,497 @@ +/- +P3 (aarch64, Stage-3 `IpcExtraCapTransferA`): **FL4's `transfer_caps` loop against l4v `transfer_caps_loop`**. + +The loop runs at most three iterations over the extra caps; a cap to the message's endpoint is unwrapped into a +badge (a store into the receive buffer, `unwrap_stepA`), any other cap is derived (`derive_relEqA`) and inserted +into the one receive slot (`cap_insert_dBA`), after which no slot is left. FL4 re-reads each slot's cap, the spec +uses the caps looked up before: they differ only at the receive slot (now the derived cap) and the insert's source +(an untyped cap made full), and neither is then a cap FL4 or the spec unwraps, so both loops stop at the same +index. + +* `XcLoopInvA`: the invariant at `(s, out, dest, i)` — the spec loop from `i` reaches outcomes of the whole loop, the + `MessageInfo` fields related, `JAtZAX` at the current CDT ghost, the MDB tree, `SInvXA` from the start, the + buffer's user frames, the kernel-state base and core, either no receive slot left or nothing inserted yet (the + `OS` slots' words and the spec state but its memory as at the start), and each remaining cap as at the start + or unwrapped by neither side. +* `XcLoopPostA`: an outcome of the whole spec loop, with the related `MessageInfo` (extra-cap count the index), + `JAtZAX`, the tree and `SInvXA`. +* **`transfer_caps_loop_relA`**. + +Case hypotheses (no named premise): `InsBaseA` at the start, the tree, `th`'s slots in `OS` and its other slots +null, `ExtraCapsRelA`, `hep` (FL4 unwraps an extra cap iff the spec does), the receive buffer's words (aligned, +user frames, off the footprint, no cap word), `RecvSlotRelA` for the receive slot. New module; no existing +statement changed. +-/ +import Fl4ProofsA64.P3.IpcXferStepA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3 Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +theorem capEqA_abs_of_notPtA {c : capability.Cap} {a : Cap} (h : CapEqA c a) + (hc : ∀ p asid va, c ≠ .PageTable p true asid va) : a = absCapA c := by + cases c with + | PageTable p m asid va => + cases m + · exact (h : absCapA _ = a).symm + · exact absurd rfl (hc p asid va) + | _ => exact (h : absCapA _ = a).symm + +theorem not_pt_of_epA {c nc : capability.Cap} (hsh : shapeA nc = shapeA c) + (h : ∃ r b rs, absCapA c = .endpointCap r b rs ∨ absCapA c = .notificationCap r b rs) : + (∀ p a v, c ≠ .PageTable p true a v) ∧ ∀ p a v, nc ≠ .PageTable p true a v := by + obtain ⟨r, b, rs, h⟩ := h + cases c with + | Endpoint => exact ⟨fun _ _ _ e => (by cases e), fun p a v e => (by subst e; simp [shapeA] at hsh)⟩ + | Notification => exact ⟨fun _ _ _ e => (by cases e), fun p a v e => (by subst e; simp [shapeA] at hsh)⟩ + | PageTable p m a v => + exfalso + cases m <;> rcases h with h | h <;> simp [absCapA, mapDataA] at h + | Frame p sz r' d m ept a' va => + exfalso + cases ept <;> rcases h with h | h <;> simp [absCapA, absFrameE] at h + | _ => exfalso; rcases h with h | h <;> revert h <;> simp [absCapA, mapDataA, absFrameE] <;> (try (split <;> simp)) + +theorem shapeA_null {c : capability.Cap} (h : shapeA c = .Null) : c = .Null := by + cases c <;> simp_all [shapeA] + +theorem getCap_detA {sl : CslotPtr} {σ : State Unit} {a b : Cap} + (ha : getCap (Z := Unit) sl σ = ({(a, σ)}, False)) (hb : getCap (Z := Unit) sl σ = ({(b, σ)}, False)) : + a = b := by + have h := congrArg Prod.fst (ha.symm.trans hb) + simp only [Set.singleton_eq_singleton_iff, Prod.mk.injEq] at h + exact h.1 + +theorem inUserFrame_slotA {σ σ' : State Unit} (H : SpecSlotA σ σ') {p : ObjRef} (h : inUserFrame p σ) : + inUserFrame p σ' := by + obtain ⟨sz, hsz⟩ := h + refine ⟨sz, ?_⟩ + rcases H.kh (p &&& ~~~(mask (pageBitsForSize sz))) with e | ⟨r, cs, cs', e1, -⟩ | ⟨x, x', e1, -⟩ + · rw [e]; exact hsz + · rw [hsz] at e1; cases e1 + · rw [hsz] at e1; cases e1 + +/-! ## The spec's machine state across `cap_insert` (nominal leaves, as `SpecFrA`'s `NdRel`) -/ + +/-- Every outcome keeps the machine state. -/ +def NdMsA {α : Type} (f : Fl4Spec.NonDet (State Unit) α) : Prop := ∀ σ r, r ∈ (f σ).1 → r.2.machineState = σ.machineState + +theorem NdMsA.bind {α β : Type} {m : Fl4Spec.NonDet (State Unit) α} {f : α → Fl4Spec.NonDet (State Unit) β} + (hm : NdMsA m) (hf : ∀ a, NdMsA (f a)) : NdMsA (m >>= f) := fun σ r hr => by + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + exact (hf x.1 x.2 r hr).trans (hm σ x hx) + +theorem NdMsA.ro {α : Type} {f : Fl4Spec.NonDet (State Unit) α} (h : ∀ σ r, r ∈ (f σ).1 → r.2 = σ) : NdMsA f := + fun σ r hr => by rw [h σ r hr] + +theorem NdMsA.pure {α : Type} (a : α) : NdMsA (Pure.pure a : Fl4Spec.NonDet (State Unit) α) := + NdMsA.ro fun σ r h => by obtain rfl : r = (a, σ) := h; rfl + +theorem NdMsA.fail {α : Type} : NdMsA (Fl4Spec.NonDet.fail : Fl4Spec.NonDet (State Unit) α) := + fun _ _ h => absurd h (Set.notMem_empty _) + +theorem NdMsA.assert (P : Prop) : NdMsA (Fl4Spec.NonDet.assert P : Fl4Spec.NonDet (State Unit) Unit) := + NdMsA.ro fun _ _ h => by obtain ⟨-, rfl⟩ := h; rfl + +theorem NdMsA.assertOpt {α : Type} (o : Option α) : + NdMsA (Fl4Spec.NonDet.assertOpt o : Fl4Spec.NonDet (State Unit) α) := by + cases o + · exact NdMsA.fail + · exact NdMsA.pure _ + +theorem NdMsA.gets {α : Type} (g : State Unit → α) : NdMsA (Fl4Spec.NonDet.gets g) := + NdMsA.ro fun σ r h => by obtain rfl : r = (g σ, σ) := h; rfl + +theorem NdMsA.get : NdMsA (Fl4Spec.NonDet.get : Fl4Spec.NonDet (State Unit) (State Unit)) := + NdMsA.ro fun σ r h => by obtain rfl : r = (σ, σ) := h; rfl + +theorem NdMsA.selectF {α : Type} (S : Set α × Prop) : NdMsA (Fl4Spec.NonDet.selectF S : Fl4Spec.NonDet (State Unit) α) := + NdMsA.ro fun _ _ h => h.2 + +theorem NdMsA.modify {g : State Unit → State Unit} (h : ∀ σ, (g σ).machineState = σ.machineState) : + NdMsA (Fl4Spec.NonDet.modify g) := fun σ r hr => by obtain rfl : r = ((), g σ) := hr; exact h σ + +theorem NdMsA.getPut {g : State Unit → State Unit} (h : ∀ σ, (g σ).machineState = σ.machineState) : + NdMsA (Fl4Spec.NonDet.get >>= fun s => Fl4Spec.NonDet.put (g s)) := fun σ r hr => by + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + obtain rfl : x = (σ, σ) := hx + obtain rfl : r = ((), g σ) := hr + exact h σ + +theorem getObject_msA (p : ObjRef) : NdMsA (getObject (Z := Unit) p) := NdMsA.ro fun _ _ h => (getObject_mem h).1 + +theorem setObject_msA (p : ObjRef) (o : KernelObject) : NdMsA (setObject (Z := Unit) p o) := fun σ r hr => by + obtain ⟨_, -, h⟩ := setObject_mem hr; rw [h] + +theorem getCap_msA (sl : CslotPtr) : NdMsA (getCap (Z := Unit) sl) := by + obtain ⟨oref, cref⟩ := sl + refine NdMsA.bind (getObject_msA oref) fun obj => ?_ + cases obj <;> dsimp only <;> first + | exact NdMsA.fail + | exact NdMsA.bind NdMsA.fail fun _ => NdMsA.assertOpt _ + | exact NdMsA.bind (NdMsA.pure _) fun _ => NdMsA.assertOpt _ + | exact NdMsA.bind (NdMsA.assert _) fun _ => NdMsA.bind (NdMsA.pure _) fun _ => NdMsA.assertOpt _ + +theorem setCap_msA (cap : Cap) (sl : CslotPtr) : NdMsA (setCap (Z := Unit) cap sl) := by + obtain ⟨oref, cref⟩ := sl + refine NdMsA.bind (getObject_msA oref) fun obj => ?_ + cases obj <;> dsimp only <;> (try split_ifs) <;> first + | exact NdMsA.bind (NdMsA.pure _) fun _ => setObject_msA _ _ + | exact NdMsA.bind NdMsA.fail fun _ => setObject_msA _ _ + | exact NdMsA.fail + +/-- **`cap_insert` keeps the machine state.** -/ +theorem capInsert_msA (c : Cap) (src dst : CslotPtr) (σ : State Unit) (r : Unit × State Unit) + (hr : r ∈ (capInsert (Z := Unit) c src dst σ).1) : r.2.machineState = σ.machineState := by + have h : NdMsA (capInsert (Z := Unit) c src dst) := by + have hG : ∀ sl, NdMsA (getCap (Z := Unit) sl) := getCap_msA + unfold capInsert setUntypedCapAsFull updateCdt setCdt setOriginal doExtendedOp + repeat' first + | exact NdMsA.pure _ | exact hG _ | exact setCap_msA _ _ | exact NdMsA.gets _ | exact NdMsA.get + | exact NdMsA.assert _ | exact NdMsA.selectF _ | exact NdMsA.modify (fun _ => rfl) + | exact NdMsA.getPut (fun _ => rfl) + | refine NdMsA.bind ?_ (fun _ => ?_) + | split + exact h σ r hr + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- (Stage-3 `hcaps`) **The orthogonality of the extra-cap delta to a relation `Q`**: `Q` is kept by the transfer +loop's two writes, an unwrapped badge stored into the receive buffer on both sides (`store`) and a `cap_insert`, +which writes cap and MDB words only and keeps the spec's user memory and every non-cap field (`insert`). -/ +structure XcOrthA (st : machine.Store S) [L : LawfulStore st] (H : Heap) (OS : Set U64) (buf : U64) (rb : ObjRef) + (Q : S → State Unit → Prop) : Prop where + store : ∀ s σ (j : U64) (v : U64), j.val < 3 → Q s σ → + Q (L.wr s (bufW buf (core.num.U64.wrapping_add 122#u64 j)) v) + (setUm σ (storeMem σ.machineState.underlyingMemory (bufAddr rb (bufferCptrIndex + j.val)) v.bv)) + insert : ∀ s σ s' σ', Q s σ → WritesIn st s s' (W7.CapW H.tcbs {b | InOS OS b} ∅) → SpecSlotA σ σ' → + σ'.machineState = σ.machineState → Q s' σ' + +end + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS : Set U64} + +theorem unw_bit_ltA {u : Data} (hu : u.toNat < 8) {j : Nat} (hj : j < 3) : (u ||| ((1 : Data) <<< j)).toNat < 8 := by + rw [BitVec.toNat_or] + refine Nat.or_lt_two_pow (n := 3) hu ?_ + interval_cases j <;> decide + +/-- The loop's invariant (module docstring). -/ +def XcLoopInvA (AR : A → HwRootA → Prop) (Z : XSetsA) (OS : Set U64) (s0 : S) (a : A) (σ0 : State Unit) + (m : McState) (caps : List (Cap × CslotPtr)) (ep : U64) (ep' : Option ObjRef) (rb : ObjRef) (d0 : U64) + (Q : S → State Unit → Prop) (sl0 : List CslotPtr) (mi0 : MessageInfo) (y : S × msginfo.MessageInfo × obj.Slot × U64) : Prop := + y.2.2.2.val ≤ caps.length ∧ ∃ σ1 G1 mi1 slots1, + Q y.1 σ1 ∧ mi1.miCapsUnwrapped.toNat < 8 ∧ mi1.miLabel = mi0.miLabel ∧ + (∀ r, r ∈ (transferCapsLoop (Z := Unit) ep' rb y.2.2.2.val (caps.drop y.2.2.2.val) slots1 mi1 σ1).1 → + r ∈ (transferCapsLoop (Z := Unit) ep' rb 0 caps sl0 mi0 σ0).1) ∧ + y.2.1.length.bv = mi1.miLength ∧ y.2.1.label.bv = mi1.miLabel ∧ + y.2.1.caps_unwrapped.bv = mi1.miCapsUnwrapped ∧ + JAtZAX (L := L) AR { Z with G := G1 } y.1 a σ1 m ∧ TreeA st OS G1.chains G1.par y.1 ∧ + (∀ n, SInvXA σ0 m n → SInvXA σ1 m n) ∧ (∀ k, k < 128 → inUserFrame (bufAddr rb k) σ1) ∧ + L.ksb y.1 = L.ksb s0 ∧ L.cpu y.1 = L.cpu s0 ∧ + ((y.2.2.1 = 0#u64 ∧ slots1 = []) ∨ + (G1 = Z.G ∧ OSKeep st OS s0 y.1 ∧ (∃ um, σ1 = setUm σ0 um) ∧ slots1 = sl0 ∧ y.2.2.1 = d0 ∧ + d0 ≠ 0#u64)) ∧ + ∀ k (hk : k < caps.length), y.2.2.2.val ≤ k → + capA st y.1 (cslotAddrA (caps[k]).2) = capA st s0 (cslotAddrA (caps[k]).2) ∨ + (¬ UnwCA ep (capA st y.1 (cslotAddrA (caps[k]).2)) ∧ ¬ SUnwA ep' (caps[k]).1) + +/-- The loop's outcome (module docstring). -/ +def XcLoopPostA (AR : A → HwRootA → Prop) (Z : XSetsA) (OS : Set U64) (a : A) (σ0 : State Unit) + (m : McState) (caps : List (Cap × CslotPtr)) (ep' : Option ObjRef) (rb : ObjRef) (Q : S → State Unit → Prop) + (sl0 : List CslotPtr) (mi0 : MessageInfo) (r : S × msginfo.MessageInfo × U64) : Prop := + ∃ mi' σ' G', (mi', σ') ∈ (transferCapsLoop (Z := Unit) ep' rb 0 caps sl0 mi0 σ0).1 ∧ + r.2.1.length.bv = mi'.miLength ∧ r.2.1.label.bv = mi'.miLabel ∧ + r.2.1.caps_unwrapped.bv = mi'.miCapsUnwrapped ∧ r.2.2.bv = mi'.miExtraCaps ∧ + JAtZAX (L := L) AR { Z with G := G' } r.1 a σ' m ∧ TreeA st OS G'.chains G'.par r.1 ∧ + (∀ n, SInvXA σ0 m n → SInvXA σ' m n) ∧ Q r.1 σ' ∧ r.2.2.val ≤ 3 ∧ mi'.miCapsUnwrapped.toNat < 8 ∧ mi'.miLabel = mi0.miLabel + +set_option maxHeartbeats 4000000 in +/-- **FL4's `transfer_caps` loop refines l4v `transfer_caps_loop`** (module docstring). -/ +theorem transfer_caps_loop_relA {s0 : S} {a : A} {σ0 : State Unit} {m : McState} + (B0 : InsBaseA (L := L) AR Z H OS s0 a σ0 m) (hT0 : TreeA st OS Z.G.chains Z.G.par s0) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s0 y = .Null) + {extra : Aeneas.Std.Array obj.Slot 3#usize} {caps : List (Cap × CslotPtr)} + (hX : ExtraCapsRelA Z.th (L.mem s0) σ0 extra caps) {ep : U64} {ep' : Option ObjRef} + (hep : ∀ j (hj : j < caps.length), + UnwCA ep (capA st s0 (cslotAddrA (caps[j]).2)) ↔ SUnwA ep' (caps[j]).1) + {buf : U64} {rb : ObjRef} (hbal : ∀ k, k < 128 → (bufAddr rb k).toNat % 8 = 0) + (hbu : ∀ k, k < 128 → inUserFrame (bufAddr rb k) σ0) + (hbF : ∀ k : U64, k.val < 128 → ¬ FootA (L := L) Z s0 (bufW buf k)) + (hbC : ∀ k : U64, k.val < 128 → ¬ InOS OS (bufW buf k)) + (hbA : ∀ k : U64, k.val < 128 → Aligned (bufW buf k)) + {d0 : U64} {sl0 : List CslotPtr} (hd0 : RecvSlotRelA Z.th (L.ksb s0) (L.mem s0) d0 sl0) + {out0 : msginfo.MessageInfo} {mi0 : MessageInfo} + (ho : out0.length.bv = mi0.miLength ∧ out0.label.bv = mi0.miLabel ∧ + out0.caps_unwrapped.bv = mi0.miCapsUnwrapped ∧ mi0.miCapsUnwrapped.toNat < 8) + {Q : S → State Unit → Prop} (hQ : XcOrthA st H OS buf rb Q) (hQ0 : Q s0 σ0) : + ipc.transfer_caps_loop st s0 extra ep buf out0 d0 0#u64 ⦃ r => + XcLoopPostA (L := L) AR Z OS a σ0 m caps ep' rb Q sl0 mi0 r ⦄ := by + have hP := B0.j.1.place + obtain ⟨hlen, hnext, hcaps⟩ := hX + have hsO : ∀ k (hk : k < caps.length), cslotAddrA (caps[k]).2 ∈ OS := fun k hk => + hsub _ (AbsSlotA.isSlot hP (hcaps k hk).2.1) + unfold ipc.transfer_caps_loop + apply loop.spec_decr_nat (measure := fun y : S × msginfo.MessageInfo × obj.Slot × U64 => 3 - y.2.2.2.val) + (inv := XcLoopInvA (L := L) AR Z OS s0 a σ0 m caps ep ep' rb d0 Q sl0 mi0) + · rintro ⟨s1, o1, d1, j⟩ ⟨hj, σ1, G1, mi1, slots1, hq, hu8, hlb, R, hl, hla, hcu, J1, T1, SI1, U1, hk1, hc1, hD, hK⟩ + dsimp only at hj R hl hla hcu J1 T1 hk1 hc1 hD hK + have stop : (miAtA mi1 j.val, σ1) ∈ + (transferCapsLoop (Z := Unit) ep' rb j.val (caps.drop j.val) slots1 mi1 σ1).1 → + XcLoopPostA (L := L) AR Z OS a σ0 m caps ep' rb Q sl0 mi0 (s1, o1, j) := fun hm => + ⟨_, σ1, G1, R _ hm, hl, hla, hcu, by simp [miAtA], J1, T1, SI1, hq, (by dsimp only; omega), hu8, hlb⟩ + unfold ipc.transfer_caps_loop.body + dsimp only + by_cases h3 : j < 3#u64 + swap + · rw [if_neg h3, WP.spec_ok] + have hjv : ¬ j.val < 3 := fun h => h3 (by scalar_tac) + have : j.val = caps.length := by omega + apply stop + rw [this, List.drop_length] + exact tcl_nilA _ _ _ _ + rw [if_pos h3] + have hj3 : j.val < 3 := by scalar_tac + obtain ⟨x, hx, hxv⟩ := index_castA extra hj3 + simp only [lift, bind_ok, hx, obj.Slot.is_null] + by_cases hx0 : x = 0#u64 + · simp only [hx0, decide_true, ↓reduceIte, WP.spec_ok] + have : j.val = caps.length := by + by_contra hne + have hjl : j.val < caps.length := by omega + have e := (hcaps j.val hjl).1 + rw [hxv, hx0] at e + exact PlaceA.slot_ne0 hP (AbsSlotA.isSlot hP (hcaps j.val hjl).2.1) (Option.some.inj e).symm + apply stop + rw [this, List.drop_length] + exact tcl_nilA _ _ _ _ + have hjl : j.val < caps.length := by + by_contra hne + have e : j.val = caps.length := by omega + have e2 := hnext (by omega) + rw [← e, hxv] at e2 + exact hx0 (Option.some.inj e2) + obtain ⟨hxe, hAs, hcr, hg⟩ := hcaps j.val hjl + have hxeq : x = cslotAddrA (caps[j.val]).2 := by rw [hxv] at hxe; exact Option.some.inj hxe + subst hxeq + have hdrop : caps.drop j.val = (caps[j.val]) :: caps.drop (j.val + 1) := List.drop_eq_getElem_cons hjl + simp only [hx0, decide_false, Bool.false_eq_true, ↓reduceIte, capA_eq, bind_ok] + obtain ⟨j', hj', hj'v⟩ := spec_existsA (U64.add_spec (x := j) (y := 1#u64) (by scalar_tac)) + have hj'1 : j'.val = j.val + 1 := by rw [hj'v]; rfl + have hKj := hK j.val hjl le_rfl + rcases unwrapped_badgeA (capA st s1 (cslotAddrA (caps[j.val]).2)) ep with ⟨hnu, hub⟩ | + ⟨badge, bx, by', bz, bw, hce, hub⟩ + · -- not unwrapped + rw [hub] + simp only [bind_ok, obj.Slot.is_null] + have hns : ¬ SUnwA ep' (caps[j.val]).1 := by + rcases hKj with e | ⟨-, h⟩ + · exact fun hs => hnu (e ▸ (hep j.val hjl).2 hs) + · exact h + by_cases hd1 : d1 = 0#u64 + · simp only [hd1, decide_true, ↓reduceIte, WP.spec_ok] + have hsl : slots1 = [] := by + rcases hD with ⟨-, h⟩ | ⟨-, -, -, -, e, h⟩ + · exact h + · exact absurd (e.symm.trans hd1) h + apply stop + rw [hsl, hdrop] + exact tcl_noSlotA hns + simp only [hd1, decide_false, Bool.false_eq_true, ↓reduceIte] + obtain ⟨hG1, hOK, ⟨um, hσ1⟩, hsl1, hdd, hd0n⟩ : G1 = Z.G ∧ OSKeep st OS s0 s1 ∧ + (∃ um, σ1 = setUm σ0 um) ∧ slots1 = sl0 ∧ d1 = d0 ∧ d0 ≠ 0#u64 := by + rcases hD with ⟨e, -⟩ | h + · exact absurd e hd1 + · exact h + subst hG1 + subst slots1 + subst d1 + obtain ⟨D, hslD, hdD, hdN, pD, hpD1, hpD, hnI⟩ : ∃ D, sl0 = [D] ∧ d0 = cslotAddrA D ∧ + capAtA (L.mem s0) d0 = .Null ∧ ∃ p, D.1 = specPtr p ∧ Z.th p = some (.cnode D.2.length) ∧ + ¬ IrqNodeA (L.ksb s0) p := by + rcases hd0 with ⟨-, e⟩ | ⟨D, h1, h2, h3, h4⟩ + · exact absurd e hd0n + · exact ⟨D, h1, h2, h3, h4⟩ + subst hdD + have hAd : AbsSlotA Z.th D := ⟨pD, hpD1, Or.inl hpD⟩ + have hdO : cslotAddrA D ∈ OS := hsub _ (AbsSlotA.isSlot hP hAd) + have hc0 : capA st s1 (cslotAddrA (caps[j.val]).2) = capA st s0 (cslotAddrA (caps[j.val]).2) := + hOK.capA (hsO j.val hjl) + set c := capA st s1 (cslotAddrA (caps[j.val]).2) with hcdef + have hcr1 : CapRelA c (caps[j.val]).1 := by rw [hc0]; exact hcr + have J1' : JAtZAX (L := L) AR Z s1 a σ1 m := J1 + have B1 := B0.keep J1' hk1 hc1 hOK + have hnc : isUtA c = true → + NoChildrenRelA (st := st) s1 σ1 (cslotAddrA (caps[j.val]).2) (caps[j.val]).2 := by + intro hu + obtain ⟨p, b, i, e, hcu⟩ := isUtA_ut hu + exact noChildren_of_adjA hP B1.os T1 J1'.1.cdt hAs (hsO j.val hjl) hcu + (B1.utRev _ (hsO j.val hjl) hu) + obtain ⟨r, hr, r', hr', hrel⟩ := derive_relEqA (st := st) s1 σ1 (cslotAddrA (caps[j.val]).2) + (caps[j.val]).2 c (caps[j.val]).1 hcr1 (CapRt.decodeF_encWf _ _) hnc + rw [hr] + simp only [bind_ok] + have hdslot : (caps.drop j.val).length ≠ 0 := by simp; omega + cases r with + | Err e => + cases r' with + | ok _ => exact hrel.elim + | error e' => + simp only [WP.spec_ok] + apply stop + rw [hslD, hdrop] + exact tcl_deriveErrA hns (by simp) hr' + | Ok nc => + cases r' with + | error _ => exact hrel.elim + | ok an => + obtain ⟨hwf, hanc, hw, hcv, hkd, hsh⟩ := hrel + try dsimp only + by_cases hn : nc = .Null + · subst hn + rw [cap_is_null_nullA] + simp only [bind_ok, ↓reduceIte, WP.spec_ok] + have han : an = .nullCap := (hanc : absCapA .Null = an).symm + subst han + apply stop + rw [hslD, hdrop] + exact tcl_deriveNullA hns (by simp) hr' + rw [cap_is_null_neA hn] + simp only [bind_ok, Bool.false_eq_true, ↓reduceIte] + have hann : an ≠ .nullCap := CapRelA.ne_null ⟨hwf, hanc⟩ hn + have hok := deriveOk_ofA hr' hann + have hsh' := hsh hn + have hcn : c ≠ .Null := fun e => hn (shapeA_null (by rw [hsh', e]; rfl)) + have hcabs : ∀ (hc : ∀ p asid va, c ≠ .PageTable p true asid va), (caps[j.val]).1 = absCapA c := + fun hc => capEqA_abs_of_notPtA hcr1.2 hc + have hnabs : ∀ (hc : ∀ p asid va, nc ≠ .PageTable p true asid va), an = absCapA nc := + fun hc => capEqA_abs_of_notPtA hanc hc + have hut : isUtA c = true → ∀ y, ¬ Fl4Proofs.P3.Mdb.Desc Z.G.par y (cslotAddrA (caps[j.val]).2) := by + intro hu y hy + obtain ⟨p, b, i, e, hcu⟩ := isUtA_ut hu + have ha := hcabs (by rw [hcu]; intro _ _ _ h; cases h) + rw [hcu] at ha + exact (hok.2.2 e (specPtr p) b.val i.val ha).2 + ((J1'.1.cdt.hasChildA hP hAs).2 (has_child_desc.2 ⟨y, hy⟩)) + have hal : UA nc c := UA.of_shapeA hsh' fun hu => by + obtain ⟨p, b, i, e, hcu⟩ := isUtA_ut hu + have ha := hcabs (by rw [hcu]; intro _ _ _ h; cases h) + have hnu' : isUtA nc = true := by rw [← isUtA_shapeA nc, hsh', isUtA_shapeA]; exact hu + obtain ⟨p', b', i', e', hncu⟩ := isUtA_ut hnu' + have hna := hnabs (by rw [hncu]; intro _ _ _ h; cases h) + have h2 := (hok.2.2 e (specPtr p) b.val i.val (by rw [ha, hcu]; rfl)).1 + exact ut_of_absA hsh' hu (by rw [← hna, h2, ha]) + have hz : ∀ p t n, c ≠ .Zombie p t n := by + intro p t n e + have ha := hcabs (by rw [e]; intro _ _ _ h; cases h) + rw [e] at ha + exact hok.1 (specPtr p) (zombieBitsA t) n.val (by rw [ha]; rfl) + have hbd : ∀ r b rs, (absCapA c = .endpointCap r b rs ∨ absCapA c = .notificationCap r b rs) → b ≠ 0 → + capEpBadge (absCapA nc) = b := by + intro r b rs hrb hb + obtain ⟨hnpt, hnpt'⟩ := not_pt_of_epA hsh' ⟨r, b, rs, hrb⟩ + rw [← hnabs hnpt'] + exact hok.2.1 r b rs (by rw [hcabs hnpt]; exact hrb) hb + have hdo : ¬ IrqNodeA (L.ksb s1) (fl4Ptr D.1) ∨ IrqCapA (absCapA nc) := by + left; rw [hk1, hpD1, fl4Ptr_specPtr]; exact hnI + have hdn1 : capAtA (L.mem s1) (cslotAddrA D) = .Null := by + rw [capAtA_eq, hOK.capA hdO, ← capAtA_eq]; exact hdN + have hsn1 : capAtA (L.mem s1) (cslotAddrA (caps[j.val]).2) ≠ .Null := hcn + have hnul1 : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s1 y = .Null := fun y hy hn' => by + rw [hOK.capA hy]; exact hnul y hy hn' + obtain ⟨s2, hs2, σ2, hm, J2, T2, SI2, HS2, ⟨hk2, hc2, hnew, hfull, hoth⟩, hWi⟩ := + spec_existsA (cap_insert_dBA B1 T1 hsub hnul1 hAs hAd hsn1 hdn1 (B0.can _ hdO) hwf + hanc hw hn hal hkd hut hsh' hz hbd hdo (hcv Z.th)) + rw [hs2, bind_ok, hj', bind_ok, WP.spec_ok] + refine ⟨⟨by first | omega | (dsimp only; omega) | (simp only []; omega), σ2, _, mi1, [], + hQ.insert _ _ _ _ hq hWi HS2 (capInsert_msA _ _ _ _ _ hm), hu8, hlb, fun r hr => R r ?_, hl, hla, hcu, J2, T2, + fun n h => SI2 n (SI1 n h), fun k hk => inUserFrame_slotA HS2 (U1 k hk), hk2.trans hk1, + hc2.trans hc1, Or.inl ⟨rfl, rfl⟩, fun k hk hjk => ?_⟩, by first | omega | (dsimp only; omega) | (simp only []; omega)⟩ + · rw [hslD, hdrop] + rw [hj'1] at hr + exact tcl_insertA hns hr' hann hm hr + · rw [hj'1] at hjk + have hkO := hsO k hk + by_cases hks : cslotAddrA (caps[k]).2 = cslotAddrA (caps[j.val]).2 + · right + have he : (caps[k]).2 = (caps[j.val]).2 := cslotAddrA_inj hP (hcaps k hk).2.1 hAs hks + have hcv' : (caps[k]).1 = (caps[j.val]).1 := + getCap_detA (he ▸ (hcaps k hk).2.2.2) hg + rw [hks, hcv'] + exact ⟨fun hu => hnu (UnwCA.of_fullA hfull hu), hns⟩ + by_cases hkd' : cslotAddrA (caps[k]).2 = cslotAddrA D + · right + rw [hkd', hnew] + refine ⟨fun hu => hnu (UnwCA.of_shapeA hsh' hu), ?_⟩ + have h0 := (hcaps k hk).2.2.1 + rw [hkd'] at h0 + have h0' : (caps[k]).1 = .nullCap := by + have h := h0.2; rw [hdN] at h; exact h.symm + rw [h0']; exact sunw_nullA _ + rw [hoth _ hkO hks hkd'] + exact hK k hk (by omega) + · -- unwrapped + rw [hub] + have hc0 : capA st s1 (cslotAddrA (caps[j.val]).2) = capA st s0 (cslotAddrA (caps[j.val]).2) := by + rcases hKj with e | ⟨h, -⟩ + · exact e + · exact absurd ⟨badge, bx, by', bz, bw, hce⟩ h + have hunw0 : UnwCA ep (capA st s0 (cslotAddrA (caps[j.val]).2)) := by + rw [← hc0]; exact ⟨badge, bx, by', bz, bw, hce⟩ + have hsu : SUnwA ep' (caps[j.val]).1 := (hep j.val hjl).1 hunw0 + obtain ⟨b2, b3, b4, b5, b6, hce2, -, hbg⟩ := UnwCA.capRelA hcr hunw0 + have hbb : b2 = badge := by + have e := hce2.symm.trans ((capAtA_eq s0 _).trans (hc0.symm.trans hce)) + injection e with _ e2 + subst hbb + have hkk : (wa 122#u64 j).val < 128 := by + simp [core.num.U64.wrapping_add_val_eq, U64.size_def, U64.numBits]; omega + have hn : bufferCptrIndex + j.val < 128 := by unfold bufferCptrIndex msgMaxLength; omega + obtain ⟨σ2, hsw, ⟨um2, hσ2⟩, J2, T2, hO2, hk2, hc2⟩ := unwrap_stepA (L := L) B0 J1 T1 hk1 hc1 + hkk hn (hbal _ hn) (U1 _ hn) (hbF _ hkk) (hbC _ hkk) (hbA _ hkk) b2 (capEpBadge (caps[j.val]).1) + have hσs : σ2 = setUm σ1 (storeMem σ1.machineState.underlyingMemory (bufAddr rb (bufferCptrIndex + j.val)) b2.bv) := by + have e := hsw.symm.trans (storeWordOffs_eq σ1 rb _ (capEpBadge (caps[j.val]).1) (hbal _ hn) (U1 _ hn)) + have e' := congrArg Prod.fst e + simp only [Set.singleton_eq_singleton_iff, Prod.mk.injEq, true_and] at e' + rw [e', hbg] + obtain ⟨v, hv, hvb⟩ := shl_bvA (j := j) (by omega) + simp only [buf_caps_eqA, bind_ok, buf_set_eq, hv, hj', WP.spec_ok] + refine ⟨⟨by first | omega | (dsimp only; omega) | (simp only []; omega), σ2, G1, ⟨mi1.miLength, mi1.miExtraCaps, mi1.miCapsUnwrapped ||| ((1 : Data) <<< j.val), + mi1.miLabel⟩, slots1, by rw [hσs]; exact hQ.store _ _ j b2 hj3 hq, unw_bit_ltA hu8 hj3, hlb, fun r hr => R r ?_, hl, hla, ?_, J2, T2, fun n h => ?_, fun k hk => ?_, hk2, hc2, + ?_, fun k hk hjk => ?_⟩, by first | omega | (dsimp only; omega) | (simp only []; omega)⟩ + · rw [hdrop] + rw [hj'1] at hr + refine tcl_unwrapA hsu ?_ hr + unfold setExtraBadge + simp only [List.get_eq_getElem] + rw [hsw]; exact Set.mem_singleton _ + · show (o1.caps_unwrapped ||| v).bv = _ + rw [← hcu, ← hvb]; rfl + · rw [hσ2]; exact SInvXA.setUmA _ (SI1 n h) + · rw [hσ2]; exact U1 k hk + · rcases hD with h | ⟨hG, hOK, ⟨um, hσ1⟩, hs, hd, hd0⟩ + · exact Or.inl h + · exact Or.inr ⟨hG, OSKeep.transA hOK hO2, ⟨um2, by rw [hσ2, hσ1]; rfl⟩, hs, hd, hd0⟩ + · rw [hj'1] at hjk + rw [hO2.capA (hsO k hk)] + exact hK k hk (by omega) + · refine ⟨Nat.zero_le _, σ0, Z.G, mi0, sl0, hQ0, ho.2.2.2, rfl, fun r hr => hr, ho.1, ho.2.1, ho.2.2.1, B0.j, hT0, fun _ h => h, + hbu, rfl, rfl, ?_, fun k hk _ => Or.inl rfl⟩ + rcases hd0 with ⟨h1, h2⟩ | ⟨D, h1, h2, -, p, hp1, hp, -⟩ + · exact Or.inl ⟨h2, h1⟩ + · refine Or.inr ⟨rfl, OSKeep.reflA s0, ⟨σ0.machineState.underlyingMemory, rfl⟩, rfl, rfl, ?_⟩ + rw [h2] + exact PlaceA.slot_ne0 hP (AbsSlotA.isSlot hP ⟨p, hp1, Or.inl hp⟩) + +end + +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/IpcXferLoopSpecA.lean b/proofs/Fl4ProofsA64/P3/IpcXferLoopSpecA.lean new file mode 100644 index 000000000..07bfac894 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/IpcXferLoopSpecA.lean @@ -0,0 +1,153 @@ +/- +P3 (aarch64, Stage-3 `IpcExtraCapTransferA`): **l4v `transfer_caps_loop`, one iteration at a time** (spec side +of the extra-cap transfer loop). + +* `SUnwA ep c`: the spec unwraps `c` (an endpoint cap to the message's endpoint); `miAtA mi n`: the result of + stopping at `n`. +* Outcome membership, each arm of the loop: `tcl_nilA` (no caps left), `tcl_unwrapA` (the badge stored, then + the rest), `tcl_noSlotA` (no receive slot left), `tcl_deriveErrA` / `tcl_deriveNullA` (the derivation fails + or gives the null cap: stop, the state kept), `tcl_insertA` (the derived cap inserted into the receive slot, + then the rest with no slot). +* `transferCaps_memA`: `transfer_caps` with a receive buffer from the receive-slot lookup's single outcome. + +New module; no existing statement changed; no named premise. +-/ +import Fl4ProofsA64.P3.IpcRecvSlotA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4SpecA64 + +noncomputable section + +/-- The spec unwraps `c`: an endpoint cap to the message's endpoint. -/ +def SUnwA (ep : Option ObjRef) (c : Cap) : Prop := isEpCap c = true ∧ ep = some (objRefOf c) + +instance (ep : Option ObjRef) (c : Cap) : Decidable (SUnwA ep c) := by unfold SUnwA; infer_instance + +/-- The loop's result when it stops at `n`. -/ +def miAtA (mi : MessageInfo) (n : Nat) : MessageInfo := + ⟨mi.miLength, BitVec.ofNat 64 n, mi.miCapsUnwrapped, mi.miLabel⟩ + +section +variable {σ ε α β : Type} + +theorem run_bind_memA {m : Fl4Spec.NonDetE σ ε α} {f : α → Fl4Spec.NonDetE σ ε β} {s s1 : σ} {a : α} + {r : Except ε β × σ} (h1 : (.ok a, s1) ∈ (m.run s).1) (h2 : r ∈ ((f a).run s1).1) : + r ∈ ((m >>= f).run s).1 := by + rw [ExceptT.run_bind] + exact ⟨(.ok a, s1), h1, h2⟩ + +theorem run_bind_errMemA {m : Fl4Spec.NonDetE σ ε α} {f : α → Fl4Spec.NonDetE σ ε β} {s s1 : σ} {e : ε} + (h1 : (.error e, s1) ∈ (m.run s).1) : (.error e, s1) ∈ ((m >>= f).run s).1 := by + rw [ExceptT.run_bind] + exact ⟨(.error e, s1), h1, rfl⟩ + +theorem liftE_memA' {m : Fl4Spec.NonDet σ α} {s s1 : σ} {a : α} (h : (a, s1) ∈ (m s).1) : + (.ok a, s1) ∈ ((Fl4Spec.NonDetE.liftE (ε := ε) m).run s).1 := by + unfold Fl4Spec.NonDetE.liftE + rw [ExceptT.run_lift] + exact ⟨(a, s1), h, rfl⟩ + +theorem catch_okMemA {m : Fl4Spec.NonDetE σ ε α} {h : ε → Fl4Spec.NonDet σ α} {s s1 : σ} {a : α} + (hm : (.ok a, s1) ∈ (m.run s).1) : (a, s1) ∈ (Fl4Spec.NonDetE.catch m h s).1 := + ⟨(.ok a, s1), hm, rfl⟩ + +theorem catch_errMemA {m : Fl4Spec.NonDetE σ ε α} {h : ε → Fl4Spec.NonDet σ α} {s s1 : σ} {e : ε} + {r : α × σ} (hm : (.error e, s1) ∈ (m.run s).1) (hh : r ∈ (h e s1).1) : + r ∈ (Fl4Spec.NonDetE.catch m h s).1 := + ⟨(.error e, s1), hm, hh⟩ + +end + +variable {ep : Option ObjRef} {rb : ObjRef} + +theorem tcl_nilA (n : Nat) (slots : List CslotPtr) (mi : MessageInfo) (σ : State Unit) : + (miAtA mi n, σ) ∈ (transferCapsLoop (Z := Unit) ep rb n [] slots mi σ).1 := by + simp only [transferCapsLoop] + exact Set.mem_singleton _ + +theorem tcl_unwrapA {n : Nat} {cap : Cap} {slot : CslotPtr} {more : List (Cap × CslotPtr)} + {slots : List CslotPtr} {mi : MessageInfo} {σ σ1 : State Unit} {r : MessageInfo × State Unit} + (hu : SUnwA ep cap) (hs : ((), σ1) ∈ (setExtraBadge (Z := Unit) rb (capEpBadge cap) n σ).1) + (hr : r ∈ (transferCapsLoop (Z := Unit) ep rb (n + 1) more slots + ⟨mi.miLength, mi.miExtraCaps, mi.miCapsUnwrapped ||| ((1 : Data) <<< n), mi.miLabel⟩ σ1).1) : + r ∈ (transferCapsLoop (Z := Unit) ep rb n ((cap, slot) :: more) slots mi σ).1 := by + rw [transferCapsLoop] + unfold constOnFailure + obtain ⟨r1, r2⟩ := r + apply catch_okMemA + refine run_bind_memA (a := transferCapsLoop ep rb (n + 1) more) (s1 := σ) (Set.mem_singleton _) ?_ + have hc : (isEpCap cap = true ∧ ep = some (objRefOf cap)) := hu + rw [if_pos hc] + exact run_bind_memA (liftE_memA' hs) (liftE_memA' hr) + +theorem tcl_noSlotA {n : Nat} {cap : Cap} {slot : CslotPtr} {more : List (Cap × CslotPtr)} + {mi : MessageInfo} {σ : State Unit} (hu : ¬ SUnwA ep cap) : + (miAtA mi n, σ) ∈ (transferCapsLoop (Z := Unit) ep rb n ((cap, slot) :: more) [] mi σ).1 := by + rw [transferCapsLoop] + unfold constOnFailure + apply catch_okMemA + refine run_bind_memA (a := transferCapsLoop ep rb (n + 1) more) (s1 := σ) (Set.mem_singleton _) ?_ + have hc : ¬ (isEpCap cap = true ∧ ep = some (objRefOf cap)) := hu + simp only [hc, ↓reduceIte, ne_eq, not_true_eq_false] + exact Set.mem_singleton _ + +theorem tcl_deriveErrA {n : Nat} {cap : Cap} {slot : CslotPtr} {more : List (Cap × CslotPtr)} + {slots : List CslotPtr} {mi : MessageInfo} {σ : State Unit} (hu : ¬ SUnwA ep cap) (hsl : slots ≠ []) + {e : SyscallError} (hd : (deriveCap (Z := Unit) slot cap).run σ = ({(.error e, σ)}, False)) : + (miAtA mi n, σ) ∈ (transferCapsLoop (Z := Unit) ep rb n ((cap, slot) :: more) slots mi σ).1 := by + rw [transferCapsLoop] + unfold constOnFailure + refine catch_errMemA (e := e) (s1 := σ) ?_ (Set.mem_singleton _) + refine run_bind_memA (a := transferCapsLoop ep rb (n + 1) more) (s1 := σ) (Set.mem_singleton _) ?_ + have hc : ¬ (isEpCap cap = true ∧ ep = some (objRefOf cap)) := hu + simp only [hc, ↓reduceIte, hsl, ne_eq, not_false_eq_true] + exact run_bind_errMemA (by rw [hd]; exact Set.mem_singleton _) + +theorem tcl_deriveNullA {n : Nat} {cap : Cap} {slot : CslotPtr} {more : List (Cap × CslotPtr)} + {slots : List CslotPtr} {mi : MessageInfo} {σ : State Unit} (hu : ¬ SUnwA ep cap) (hsl : slots ≠ []) + (hd : (deriveCap (Z := Unit) slot cap).run σ = ({(.ok .nullCap, σ)}, False)) : + (miAtA mi n, σ) ∈ (transferCapsLoop (Z := Unit) ep rb n ((cap, slot) :: more) slots mi σ).1 := by + rw [transferCapsLoop] + unfold constOnFailure + refine catch_errMemA (s1 := σ) (e := (Fl4SpecA64.undefined : SyscallError)) ?_ (Set.mem_singleton _) + refine run_bind_memA (a := transferCapsLoop ep rb (n + 1) more) (s1 := σ) (Set.mem_singleton _) ?_ + have hc : ¬ (isEpCap cap = true ∧ ep = some (objRefOf cap)) := hu + simp only [hc, ↓reduceIte, hsl, ne_eq, not_false_eq_true] + refine run_bind_memA (by rw [hd]; exact Set.mem_singleton _) ?_ + simp only [Fl4Spec.NonDetE.whenE, ↓reduceIte] + exact run_bind_errMemA (Set.mem_singleton _) + +theorem tcl_insertA {n : Nat} {cap cap' : Cap} {slot d : CslotPtr} {more : List (Cap × CslotPtr)} + {mi : MessageInfo} {σ σ1 : State Unit} {r : MessageInfo × State Unit} (hu : ¬ SUnwA ep cap) + (hd : (deriveCap (Z := Unit) slot cap).run σ = ({(.ok cap', σ)}, False)) (hn : cap' ≠ .nullCap) + (hi : ((), σ1) ∈ (capInsert (Z := Unit) cap' slot d σ).1) + (hr : r ∈ (transferCapsLoop (Z := Unit) ep rb (n + 1) more [] mi σ1).1) : + r ∈ (transferCapsLoop (Z := Unit) ep rb n ((cap, slot) :: more) [d] mi σ).1 := by + rw [transferCapsLoop] + unfold constOnFailure + obtain ⟨r1, r2⟩ := r + apply catch_okMemA + refine run_bind_memA (a := transferCapsLoop ep rb (n + 1) more) (s1 := σ) (Set.mem_singleton _) ?_ + have hc : ¬ (isEpCap cap = true ∧ ep = some (objRefOf cap)) := hu + simp only [hc, ↓reduceIte, ne_eq, reduceCtorEq, not_false_eq_true] + refine run_bind_memA (by rw [hd]; exact Set.mem_singleton _) ?_ + simp only [Fl4Spec.NonDetE.whenE, hn, ↓reduceIte] + refine run_bind_memA (Set.mem_singleton _) ?_ + refine run_bind_memA (liftE_memA' hi) ?_ + exact liftE_memA' hr + +/-- `transfer_caps` with a receive buffer, from the receive-slot lookup's one outcome. -/ +theorem transferCaps_memA {mi : MessageInfo} {caps : List (Cap × CslotPtr)} {t : ObjRef} {σ : State Unit} + {sl : List CslotPtr} (hs : getReceiveSlots (Z := Unit) t (some rb) σ = ({(sl, σ)}, False)) + {r : MessageInfo × State Unit} + (hr : r ∈ (transferCapsLoop (Z := Unit) ep rb 0 caps sl ⟨mi.miLength, 0, 0, mi.miLabel⟩ σ).1) : + r ∈ (transferCaps (Z := Unit) mi caps ep t (some rb) σ).1 := by + unfold transferCaps + rw [nd_bind_one hs] + exact ⟨(_, σ), Set.mem_singleton _, hr⟩ + +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/IpcXferOrthA.lean b/proofs/Fl4ProofsA64/P3/IpcXferOrthA.lean new file mode 100644 index 000000000..f631cc1bc --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/IpcXferOrthA.lean @@ -0,0 +1,195 @@ +/- +P3 (aarch64, Stage-3 `hcaps`, option (D), contract §11/§12): **the extra-cap transfer delta is orthogonal to the +IPC relations**. + +`transfer_caps` writes two things: the unwrapped badges into the receive buffer (`XcOrthA.store`) and, in +`cap_insert`, cap and MDB words with a slot-only spec step (`XcOrthA.insert`). This file proves that the endpoint +relation and the scheduler relation are kept by both: + +* **`SpecSlotA.sstepA`** / **`SpecSlotA.kviewA`**: a slot-only spec step is an `SStepA` step that blocks no thread, + and (with the machine state kept) a `KView` step; +* **`EpStateRel.storeBufA`**: a paired store into a tracked buffer word keeps the endpoint relation; +* **`XcPostIpcA`** and **`xcOrth_ipcA`**: `EpStateRel`, `IpcSchedRelA` and every TCB's non-cap fields are kept by + the transfer loop, as an `XcOrthA`. + +Case hypotheses (placement facts of the join, no named premise): `hE` / `hRd`, the cap and MDB words of the slots +are outside the endpoint relation's footprint and the scheduler relation's reads; `hb`, the receive buffer is +tracked. New module; no existing statement changed. +-/ +import Fl4ProofsA64.P3.IpcExtraCapTransferDischA +import Fl4ProofsA64.P3.ReplyLiveJointA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3 Fl4ProofsA64.P3.Notif Fl4SpecA64 Sched + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-- A slot-only spec step is an `SStepA` step that blocks no thread. -/ +theorem SpecSlotA.sstepA {σ σ' : State Unit} (h : SpecSlotA σ σ') : SStepA σ σ' ∧ NoBlockESc σ σ' := by + have tk : ∀ p x, σ.kheap p = some (.tcb x) → ∃ y, σ'.kheap p = some (.tcb y) ∧ tcbNcA y = tcbNcA x := by + intro p x hx + rcases h.kh p with e | ⟨r, cs, cs', e1, -⟩ | ⟨z, z', e1, e2, e3⟩ + · exact ⟨x, e.trans hx, rfl⟩ + · rw [hx] at e1; cases e1 + · rw [hx] at e1; cases e1; exact ⟨z', e2, e3⟩ + have back : ∀ p o, σ'.kheap p = some o → (∀ r cs, o ≠ .cnode r cs) → (∀ x, o ≠ .tcb x) → σ.kheap p = some o := by + intro p o ho hc ht + rcases h.kh p with e | ⟨r, cs, cs', -, e2⟩ | ⟨z, z', -, e2, -⟩ + · exact e.symm.trans ho + · rw [ho] at e2; exact absurd (Option.some.inj e2) (hc _ _) + · rw [ho] at e2; exact absurd (Option.some.inj e2) (ht _) + refine ⟨⟨⟨h.cur, h.csc, h.idle, h.time, h.cons, h.repr, h.dom, h.domT⟩, h.act, h.rq, h.rls, + fun p x n hx => (h.keep hx (fun _ _ e => by cases e) (fun _ e => by cases e)).trans hx.symm, + fun p x hx => ?_, + fun p x n hx => (back p _ hx (fun _ _ e => by cases e) (fun _ e => by cases e)).trans hx.symm, + fun p y hy => ?_, + fun p y hy => ⟨y, back p _ hy (fun _ _ e => by cases e) (fun _ e => by cases e), rfl⟩⟩, ?_⟩ + · obtain ⟨y, hy, e⟩ := tk p x hx + obtain ⟨-, h2, h3, h4, -, -⟩ := tcbNcA_eq e + exact ⟨y, hy, h2, h3, h4⟩ + · rcases h.kh p with e | ⟨r, cs, cs', -, e2⟩ | ⟨z, z', e1, -, -⟩ + · exact ⟨y, e.symm.trans hy⟩ + · rw [hy] at e2; cases e2 + · exact ⟨z, e1⟩ + · intro p x y hx hy + obtain ⟨y', hy', e⟩ := tk p x hx + rw [hy] at hy' + cases hy' + have hs := (tcbNcA_eq e).1 + have hf : y.tcbFault = x.tcbFault := by have := congrArg Tcb.tcbFault e; exact this + exact Or.inl ⟨hs, fun hc => by rwa [hs, hf] at hc⟩ + +/-- A slot-only spec step that keeps the machine state keeps what the endpoint relation reads. -/ +theorem SpecSlotA.kviewA {σ σ' : State Unit} (h : SpecSlotA σ σ') (hm : σ'.machineState = σ.machineState) : + Endp.KView σ σ' := by + refine ⟨fun p x hx => h.keep hx (fun _ _ e => by cases e) (fun _ e => by cases e), + fun p x hx => ⟨x, h.keep hx (fun _ _ e => by cases e) (fun _ e => by cases e), rfl⟩, + fun p x hx => ?_, by rw [hm]⟩ + rcases h.kh p with e | ⟨r, cs, cs', e1, -⟩ | ⟨z, z', e1, e2, e3⟩ + · exact ⟨x, e.trans hx, rfl, rfl, rfl, rfl, rfl⟩ + · rw [hx] at e1; cases e1 + · rw [hx] at e1 + cases e1 + obtain ⟨h1, -, -, h4, h5, -⟩ := tcbNcA_eq e3 + have hf : z'.tcbFault = x.tcbFault := by have := congrArg Tcb.tcbFault e3; exact this + have hn : z'.tcbBoundNotification = x.tcbBoundNotification := by + have := congrArg Tcb.tcbBoundNotification e3; exact this + exact ⟨z', e2, h1, by rw [h5], h4, hf, hn⟩ + +section Store +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {T E R : Set U64} {Bf : Set (U64 × Word)} + +/-- **A paired store into a tracked buffer word keeps the endpoint relation.** -/ +theorem EpStateRel.storeBufA {s : S} {σ : State Unit} (hR : EpStateRel T E R Bf (L.mem s) σ) (hP : EPlaced T E R Bf) + {buf : U64} {rb : Word} (hb : (buf, rb) ∈ Bf) {k : U64} (hk : k.val < 128) (v : U64) : + EpStateRel T E R Bf (L.mem (L.wr s (bufW buf k) v)) + (setUm σ (storeMem σ.machineState.underlyingMemory (bufAddr rb k.val) v.bv)) := by + have hw : BufW Bf (bufW buf k) := ⟨(buf, rb), hb, k, hk, rfl⟩ + have hal : Aligned (bufW buf k) := hP.balign _ hw + have hag := agree_wr (L := L) (s := s) (v := v) hal + have hP0 : EPlaced T E R ∅ := + { hP with + balign := fun _ ⟨_, h, _⟩ => h.elim + bt := fun _ ⟨_, h, _⟩ => h.elim + be := fun _ ⟨_, h, _⟩ => h.elim + br := fun _ ⟨_, h, _⟩ => h.elim + binj := fun _ h => h.elim } + have hR0 : EpStateRel T E R ∅ (L.mem s) σ := ⟨hR.ep, hR.reply, hR.tcb, fun _ h => h.elim⟩ + have hfoot : FootEq T E R ∅ (L.mem s) (L.mem (L.wr s (bufW buf k) v)) := by + intro a ha hf + refine hag a ha fun e => ?_ + have e' : a = bufW buf k := UScalar.eq_of_val_eq e + rcases hf with ⟨x, hx, h⟩ | ⟨x, hx, h⟩ | ⟨t, ht, h⟩ | ⟨_, h, _⟩ + · have := hP.be _ hw x hx + unfold Aligned at hal ha + have := (hP.ep x hx).1 + rw [← e'] at * + omega + · have := hP.br _ hw x hx + rw [← e'] at * + omega + · have := hP.bt _ hw t ht + rw [← e'] at this + rcases h with h | h + · have := h.range; omega + · unfold TL at h; omega + · exact h.elim + have hF := hR0.frame hP0 hfoot ⟨fun _ _ h => h, fun _ x h => ⟨x, h, rfl⟩, + fun _ x h => ⟨x, h, rfl, rfl, rfl, rfl, rfl⟩, rfl⟩ + refine ⟨hF.ep, hF.reply, hF.tcb, fun bp hbp k' hk' => ?_⟩ + have hold := hR.buf bp hbp k' hk' + have hnew := hR.buf (buf, rb) hb k hk + refine ⟨?_, hold.2⟩ + show (L.mem (L.wr s (bufW buf k) v) (bufW bp.1 k')).bv = + loadWordAt (storeMem σ.machineState.underlyingMemory (bufAddr rb k.val) v.bv) (bufAddr bp.2 k'.val) + by_cases e : bufW bp.1 k' = bufW buf k + · have ea := (hP.binj bp hbp (buf, rb) hb k' k hk' hk).1 e + simp only at ea + rw [e, ea, L.mem_wr_same hal, load_store_same] + · have ea : bufAddr bp.2 k'.val ≠ bufAddr rb k.val := fun h => + e ((hP.binj bp hbp (buf, rb) hb k' k hk' hk).2 h) + have hal' : Aligned (bufW bp.1 k') := hP.balign _ ⟨bp, hbp, k', hk', rfl⟩ + rw [hag _ hal' (fun h => e (UScalar.eq_of_val_eq h)), load_store_other _ _ hnew.2 hold.2 ea] + exact hold.1 + +variable {Z : XSetsA} {m : McState} {κ : U64 → U64} {N : Set U64} + +/-- What the transfer loop keeps of the IPC side (module docstring). -/ +structure XcPostIpcA (st : machine.Store S) [L : LawfulStore st] (Z : XSetsA) (m : McState) (κ : U64 → U64) + (E R : Set U64) (Bf : Set (U64 × Word)) (s0 : S) (σ0 : State Unit) (s : S) (σ : State Unit) : Prop where + ep : EpStateRel Z.T E R Bf (L.mem s) σ + sr : IpcSchedRelA st Z m κ s σ + tcb : ∀ p x, σ0.kheap p = some (.tcb x) → ∃ y, σ.kheap p = some (.tcb y) ∧ tcbNcA y = tcbNcA x + ksb : L.ksb s = L.ksb s0 + frame : SpecSlotA σ0 σ + +/-- **The extra-cap delta is orthogonal to the IPC relations** (module docstring). -/ +theorem xcOrth_ipcA {H : Heap} {OS : Set U64} {s0 : S} {σ0 : State Unit} + (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) {buf : U64} {rb : ObjRef} (hb : (buf, rb) ∈ Bf) + (hE : ∀ a, W7.CapW H.tcbs {b | InOS OS b} ∅ a → ¬ EFoot Z.T E R Bf a) + (hRd : ∀ s σ, L.ksb s = L.ksb s0 → IpcSchedRelA st Z m κ s σ → + ∀ a, W7.CapW H.tcbs {b | InOS OS b} ∅ a → ¬ SReadA st Z m s σ a) : + XcOrthA st H OS buf rb (XcPostIpcA st Z m κ E R Bf s0 σ0) := by + refine ⟨fun s σ j v hj q => ?_, fun s σ s' σ' q hw hS hms => ?_⟩ + · have hjv : (wa 122#u64 j).val = bufferCptrIndex + j.val := by + unfold bufferCptrIndex msgMaxLength + simp [core.num.U64.wrapping_add_val_eq, U64.size_def, U64.numBits]; omega + have hk : (wa 122#u64 j).val < 128 := by rw [hjv]; unfold bufferCptrIndex msgMaxLength; omega + have hE' := q.ep.storeBufA PE hb hk v + rw [hjv] at hE' + have hbw : BufW Bf (bufW buf (wa 122#u64 j)) := ⟨(buf, rb), hb, _, hk, rfl⟩ + have hW : WritesIn st s (L.wr s (bufW buf (wa 122#u64 j)) v) (EWrite Z.T E R Bf) := + (writesIn_wr (st := st) (s := s) (v := v) (PE.balign _ hbw)).mono fun a e => by + subst e; exact Or.inr (Or.inr (Or.inr hbw)) + have hsr := (schedOpsEAOfPlacementA (st := st) (m := m) (κ := κ) P).stable _ _ _ _ q.sr hW + (EpStep.setUmA σ (storeMem σ.machineState.underlyingMemory (bufAddr rb (bufferCptrIndex + j.val)) v.bv)) (fun p x y hx hy => by + have e : x = y := by + have := hx.symm.trans (show σ.kheap p = some (.tcb y) from hy) + cases this; rfl + subst e; exact Or.inl ⟨rfl, fun h => h⟩) + exact ⟨hE', hsr, q.tcb, (L.ksb_wr _ _ _).trans q.ksb, { q.frame with kh := q.frame.kh, domk := ⟨q.frame.domk.index, q.frame.domk.start, q.frame.domk.table, q.frame.domk.gvs⟩ }⟩ + · have hfoot : FootEq Z.T E R Bf (L.mem s) (L.mem s') := fun a ha hf => + hw.mem_eq ha (fun h => hE a h hf) + obtain ⟨hSS, hNB⟩ := SpecSlotA.sstepA hS + refine ⟨q.ep.frame PE hfoot (SpecSlotA.kviewA hS hms), ?_, fun p x hx => ?_, hw.1.trans q.ksb, + SpecSlotA.trans q.frame hS⟩ + · have hsr := q.sr + obtain ⟨Q, R', H'⟩ := q.sr + have hrd : ∀ a, W7.CapW H.tcbs {b | InOS OS b} ∅ a → ¬ SReadA st { Z with Q := Q, R := R' } m s σ a := + fun a h => hRd s σ q.ksb hsr a h + exact ⟨Q, R', SchedSRA.stableR H' hw hrd hSS (H'.run.step hSS hNB) (H'.rd.step hSS hNB)⟩ + · obtain ⟨y, hy, e⟩ := q.tcb p x hx + rcases hS.kh p with e' | ⟨r, cs, cs', e1, -⟩ | ⟨z, z', e1, e2, e3⟩ + · exact ⟨y, e'.trans hy, e⟩ + · rw [hy] at e1; cases e1 + · rw [hy] at e1; cases e1; exact ⟨z', e2, e3.trans e⟩ + +end Store + +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/IpcXferStepA.lean b/proofs/Fl4ProofsA64/P3/IpcXferStepA.lean new file mode 100644 index 000000000..44dc90a1a --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/IpcXferStepA.lean @@ -0,0 +1,181 @@ +/- +P3 (aarch64, Stage-3 `IpcExtraCapTransferA`): **the pieces of one iteration of FL4's `transfer_caps` loop**. + +* `UnwCA ep c` (FL4 unwraps `c`: an endpoint cap to `ep`), `unwrapped_badgeA`, `UnwCA.of_shapeA`, + `UnwCA.of_fullA`, `UnwCA.capRelA` (the related spec cap and its badge); `sunw_nullA`. +* `ExtraCapsRelA`: FL4's extra-cap array against the spec's looked-up caps (the non-null prefix are the caps' + slots, related, read by `getCap`; the entry after it is null). +* `index_castA` (the array read at a `U64` index), `shl_bvA` (`1 <<< j` as the spec's; CnodeDelHookA's + `shl_radixA` argument), `FootA.of_ksbA`. +* `unwrap_stepA`: the badge store into the receive buffer against l4v `set_extra_badge`, with `JAtZAX`, the MDB + tree, `SInvXA` and the buffer's user frames carried (the store's word is off the footprint and no cap word). + +Case hypotheses only; no named premise. New module; no existing statement changed. +-/ +import Fl4ProofsA64.P3.IpcXferDeriveA +import Fl4ProofsA64.P3.InsBaseA +import Fl4ProofsA64.P3.UnmapS1JA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3 Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-- FL4 unwraps `c` at `ep` (module docstring). -/ +def UnwCA (ep : U64) (c : capability.Cap) : Prop := ∃ b x y z w, c = .Endpoint ep b x y z w + +theorem unwrapped_badgeA (c : capability.Cap) (ep : U64) : + (¬ UnwCA ep c ∧ ipc.unwrapped_badge c ep = ok none) ∨ + ∃ b x y z w, c = .Endpoint ep b x y z w ∧ ipc.unwrapped_badge c ep = ok (some b) := by + cases c with + | Endpoint p b x y z w => + by_cases h : p = ep + · subst h + exact Or.inr ⟨b, x, y, z, w, rfl, by simp [ipc.unwrapped_badge]⟩ + · refine Or.inl ⟨fun ⟨_, _, _, _, _, e⟩ => ?_, by simp [ipc.unwrapped_badge, h]⟩ + injection e with e1 + exact h e1 + | _ => exact Or.inl ⟨(fun ⟨_, _, _, _, _, e⟩ => by cases e), rfl⟩ + +theorem UnwCA.of_shapeA {ep : U64} {d c : capability.Cap} (h : shapeA d = shapeA c) (hd : UnwCA ep d) : + UnwCA ep c := by + obtain ⟨b, x, y, z, w, rfl⟩ := hd + cases c <;> simp [shapeA] at h + case Endpoint p b' x' y' z' w' => exact ⟨b', x', y', z', w', by rw [h]⟩ + +theorem UnwCA.of_fullA {ep : U64} {d c : capability.Cap} (h : FullA d c) (hd : UnwCA ep d) : UnwCA ep c := by + rcases h with rfl | ⟨p, b, i, e, i1, -, rfl, -⟩ + · exact hd + · obtain ⟨_, _, _, _, _, e⟩ := hd + cases e + +/-- The spec cap related to an unwrapped cap is the endpoint cap with its badge. -/ +theorem UnwCA.capRelA {ep : U64} {c : capability.Cap} {a : Cap} (hc : CapRelA c a) (h : UnwCA ep c) : + ∃ b x y z w, c = .Endpoint ep b x y z w ∧ a = absCapA c ∧ capEpBadge a = b.bv := by + obtain ⟨b, x, y, z, w, rfl⟩ := h + have ha : absCapA (.Endpoint ep b x y z w) = a := hc.2 + exact ⟨b, x, y, z, w, rfl, ha.symm, by rw [← ha]; rfl⟩ + +theorem sunw_nullA (ep : Option ObjRef) : ¬ SUnwA ep .nullCap := fun h => by + have := h.1 + simp [isEpCap] at this + +/-- FL4's extra-cap array against the spec's looked-up caps (module docstring). -/ +def ExtraCapsRelA (th : TypedHeapA) (mem : U64 → U64) (σ : State Unit) + (extra : Aeneas.Std.Array obj.Slot 3#usize) (caps : List (Cap × CslotPtr)) : Prop := + caps.length ≤ 3 ∧ (caps.length < 3 → extra.val[caps.length]? = some 0#u64) ∧ + ∀ j (hj : j < caps.length), extra.val[j]? = some (cslotAddrA (caps[j]).2) ∧ AbsSlotA th (caps[j]).2 ∧ + CapRelA (capAtA mem (cslotAddrA (caps[j]).2)) (caps[j]).1 ∧ + getCap (Z := Unit) (caps[j]).2 σ = ({((caps[j]).1, σ)}, False) + +theorem index_castA (extra : Aeneas.Std.Array obj.Slot 3#usize) {j : U64} (hj : j.val < 3) : + ∃ x, Aeneas.Std.Array.index_usize extra (UScalar.cast .Usize j) = ok x ∧ extra.val[j.val]? = some x := by + have hc : (UScalar.cast .Usize j).val = j.val := by + have : 2 ^ 32 ≤ 2 ^ System.Platform.numBits := by + rcases System.Platform.numBits_eq with e | e <;> simp [e] + rw [UScalar.cast_val_eq]; simp only [UScalar.size, UScalarTy.numBits] + rw [Nat.mod_eq_of_lt (by omega)] + have hl : (UScalar.cast .Usize j).val < extra.val.length := by + rw [extra.property, hc]; simpa using hj + obtain ⟨x, hx, e⟩ := spec_existsA (Aeneas.Std.Array.index_usize_spec extra _ hl) + refine ⟨x, hx, ?_⟩ + rw [e, ← hc] + simp [hl] + +theorem shl_bvA {j : U64} (hj : j.val < 64) : + ∃ v : U64, (1#u64 <<< (j &&& 63#u64)) = ok v ∧ v.bv = (1 : BitVec 64) <<< j.val := by + have h63 : (j &&& 63#u64).val = j.val := by + have : (j &&& 63#u64).val = j.val % 64 := by simp; exact Nat.and_two_pow_sub_one_eq_mod j.val 6 + rw [this]; exact Nat.mod_eq_of_lt hj + obtain ⟨z, hz, hv, -⟩ := spec_existsA (U64.ShiftLeft_spec 1#u64 (j &&& 63#u64) (by rw [h63]; omega)) + refine ⟨z, hz, ?_⟩ + apply BitVec.eq_of_toNat_eq + have hlt : 2 ^ j.val < 2 ^ 64 := Nat.pow_lt_pow_right (by decide) hj + have h1 : (1#u64 : U64).val = 1 := rfl + have hz' : z.val = 2 ^ j.val := by + rw [hv, h63, Nat.shiftLeft_eq, h1, Nat.one_mul] + exact Nat.mod_eq_of_lt (by simpa [U64.size, U64.numBits] using hlt) + show z.val = _ + rw [hz', BitVec.toNat_shiftLeft, Nat.shiftLeft_eq] + simp + exact (Nat.mod_eq_of_lt (lt_of_lt_of_eq hlt (by norm_num))).symm + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-- A footprint word at a state with the same kernel-state base and core is one at the first. -/ +theorem FootNA.withGA {Z : XSetsA} {G : MdbGhostA} {s : S} {b : U64} + (h : FootNA (L := L) { Z with G := G } s b) : FootNA (L := L) Z s b := by + cases h with + | node c _ hc' ho => exact .node c _ hc' ho + | cur _ ho => exact .cur _ ho + | cur2 _ ho => exact .cur2 _ ho + | tcb y _ hy ho => exact .tcb y _ hy ho + | tcb2 y _ hy ho => exact .tcb2 y _ hy ho + | sc c hc' => exact .sc c hc' + | sc2 c hc' => exact .sc2 c hc' + | irq i hi => exact .irq i hi + | glob _ ho => exact .glob _ ho + | vcpu v _ hv hw => exact .vcpu v _ hv hw + | vs _ hb => exact .vs _ hb + | ext _ hb => exact .ext _ hb + +/-- A footprint word at the ghost `G` and a state with the same kernel-state base and core is one of `Z` at the +first. -/ +theorem FootA.of_ksbA {Z : XSetsA} {G : MdbGhostA} {s s1 : S} (hk : L.ksb s1 = L.ksb s) + (hcp : L.cpu s1 = L.cpu s) {b : U64} (h : FootA (L := L) { Z with G := G } s1 b) : FootA (L := L) Z s b := by + cases h with + | slot y _ hy hw => exact .slot y _ hy hw + | nslot _ hn => exact .nslot _ (FootNA.withGA (FootNA.of_ksbA hk hcp hn)) + +theorem OSKeep.transA {OS : Set U64} {s1 s2 s3 : S} (h1 : OSKeep st OS s1 s2) (h2 : OSKeep st OS s2 s3) : + OSKeep st OS s1 s3 := fun y hy => by + obtain ⟨a1, a2, a3, a4⟩ := h1 y hy + obtain ⟨b1, b2, b3, b4⟩ := h2 y hy + exact ⟨b1.trans a1, b2.trans a2, b3.trans a3, b4.trans a4⟩ + +theorem OSKeep.reflA {OS : Set U64} (s : S) : OSKeep st OS s s := fun _ _ => ⟨rfl, rfl, rfl, rfl⟩ + +theorem SInvXA.setUmA {σ : State Unit} {m : McState} {n : Nat} (um : Word → Word8) (h : SInvXA σ m n) : + SInvXA (setUm σ um) m n := + { mc := h.mc, view := h.view, curTcb := h.curTcb, idleTcb := h.idleTcb, onlyIdle := h.onlyIdle, + idleSc := h.idleSc, curSc := h.curSc, ctQ := h.ctQ, idleQ := h.idleQ, rq := h.rq, rls := h.rls, + rpos := h.rpos, zc := h.zc, rlsRun := h.rlsRun, curScReady := h.curScReady, faultSt := h.faultSt, + irqN := ⟨h.irqN.node, h.irqN.slot, h.irqN.ref⟩ } + +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS : Set U64} + +/-- **The badge store** (module docstring). -/ +theorem unwrap_stepA {s0 s : S} {a : A} {σ0 σ : State Unit} {m : McState} {G : MdbGhostA} + (B0 : InsBaseA (L := L) AR Z H OS s0 a σ0 m) + (J : JAtZAX (L := L) AR { Z with G := G } s a σ m) (hT : TreeA st OS G.chains G.par s) + (hks : L.ksb s = L.ksb s0) (hcp : L.cpu s = L.cpu s0) + {buf : U64} {rb : ObjRef} {k : U64} (hk : k.val < 128) {n : Nat} (hn : n < 128) + (hbal : (bufAddr rb n).toNat % 8 = 0) (hbu : inUserFrame (bufAddr rb n) σ) + (hbF : ¬ FootA (L := L) Z s0 (bufW buf k)) + (hbC : ¬ InOS OS (bufW buf k)) (hbA : Aligned (bufW buf k)) (v : U64) (v' : Word) : + ∃ σ', storeWordOffs (Z := Unit) rb n v' σ = ({((), σ')}, False) ∧ (∃ um, σ' = setUm σ um) ∧ + JAtZAX (L := L) AR { Z with G := G } (L.wr s (bufW buf k) v) a σ' m ∧ + TreeA st OS G.chains G.par (L.wr s (bufW buf k) v) ∧ OSKeep st OS s (L.wr s (bufW buf k) v) ∧ + L.ksb (L.wr s (bufW buf k) v) = L.ksb s0 ∧ L.cpu (L.wr s (bufW buf k) v) = L.cpu s0 := by + have hw := writesIn_wr (st := st) (s := s) (v := v) hbA + have hal : ∀ b, FootA (L := L) { Z with G := G } s b → Aligned b := + FootA.aligned J.1 (fun c hc o h1 h2 => by rw [nodeAt_ksb hks]; exact B0.nodes c hc o h1 h2) B0.scAl B0.vcAl + have J1 : JAtZAX (L := L) AR { Z with G := G } (L.wr s (bufW buf k) v) a σ m := J.of_writesIn hw (fun b hb hf => hbF (hb ▸ FootA.of_ksbA hks hcp hf)) hal + have hO : OSKeep st OS s (L.wr s (bufW buf k) v) := + OSKeep.of_off B0.os hw fun y hy o o1 _ e => by + have hfit := B0.os.fit y hy + have hv : (core.num.U64.wrapping_add y o).val = y.val + o.val := by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact hbC (e ▸ ⟨y, hy, by omega, by omega⟩) + refine ⟨_, storeWordOffs_eq σ rb n v' hbal hbu, ⟨_, rfl⟩, JAtZAX.of_msA J1 ⟨_, rfl⟩, treeA_of_mem hT hO, hO, + (L.ksb_wr _ _ _).trans hks, (L.cpu_wr _ _ _).trans hcp⟩ + +end + +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/IrqDeleteEntryFrameA.lean b/proofs/Fl4ProofsA64/P3/IrqDeleteEntryFrameA.lean new file mode 100644 index 000000000..bbed88f1e --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/IrqDeleteEntryFrameA.lean @@ -0,0 +1,194 @@ +/- +P3 (AArch64): the concrete isolated IRQ-node deletion frame at the +related abstract slot. Both handler Set and Clear read this slot after +Restart. Full IRQHandler finalisation uses the same delete-one body. + +[J] The concrete deletion inputs and Restart successor are the +existing P1 DelIn and DelR. An insertion source is off the IRQ node, +as obtained from the actual extra-cap slot's P1 SlotCov. +[J] The linked notification finaliser supplies the joint entry at its +actual cancellation successor. The Set insertion continues from the +joint and P1 facts of the actual deletion successor. +-/ +import Fl4ProofsA64.P3.IrqInvokeGhostA +import Fl4ProofsA64.P3.IrqFinalCoverageA +import Fl4ProofsA64.P1.MdbIrqC + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4SpecA64 + +noncomputable section + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +/-- Join the concrete P1 frame with a refinement of the same delete-one +run. The empty destination and surviving source are available at the +successor that Set passes to insertion. -/ +theorem irqDeleteOne_entryFrameA {H : Heap} {Cs OS Vc Ub Pt Ac : Set U64} {ia ie : Nat} + {K : capability.Cap → Prop} {Ty : S → Prop} {s0 s : S} {a : A} + {th : TypedHeapA} {σ : State Unit} {irq : U64} + (I : DelIn st ar H Cs OS Vc Ub Pt K Ty s0) + (R : DelR st H Cs OS Vc Ub Ac Ty ia ie s0 s) + (irqRel : IrqRelA (L.mem s) (L.ksb s) σ) (place : PlaceA th (L.ksb s)) + (hi : irq.val < 5376) {Post : S × A → Prop} + (refinement : let sl : CslotPtr := (σ.interruptIrqNode (BitVec.ofNat 13 irq.val), []) + cdt.delete_one st ar s a (cslotAddrA sl) ⦃ Post ⦄) : + let sl : CslotPtr := (σ.interruptIrqNode (BitVec.ofNat 13 irq.val), []) + cdt.delete_one st ar s a (cslotAddrA sl) ⦃ r => + Post r ∧ (SysL st H Cs OS ia ie r.1 ∧ SymL st H Cs r.1) ∧ + JV st H.tcbs Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac OS Vc Ub s0 r.1 ∧ + VcCap st Vc OS r.1 ∧ capAtA (L.mem r.1) (cslotAddrA sl) = .Null ∧ + ∀ y ∈ OS, ¬ IslN (L.ksb s) y → capA st r.1 y = capA st s y ⦄ := by + obtain ⟨_, he, _⟩ := irqSlot_relatedA (st := st) irqRel place hi + dsimp only at refinement ⊢ + rw [he, R.jv.ksb] at refinement ⊢ + exact WP.spec_and refinement (cdt.delete_one.irqLK I R a hi) + +/-- An empty selected node keeps the incoming relation and therefore the +original derivation ghost. This also gives exact equality of the machine +state on both sides. -/ +theorem irqDeleteOne_nullEntryA {s : S} {a : A} {σ : State Unit} {th : TypedHeapA} + {sl : CslotPtr} (rel : StateRelA th (L.mem s) σ) (hsl : AbsSlotA th sl) + (hc : capAtA (L.mem s) (cslotAddrA sl) = .Null) : + cdt.delete_one st ar s a (cslotAddrA sl) ⦃ r => + r = (s, a) ∧ ((), σ) ∈ (capDeleteOne (Z := Unit) sl σ).1 ⦄ := by + obtain ⟨run, post⟩ := irqDeleteNullA (ar := ar) (a := a) rel hsl hc + rw [run, WP.spec_ok] + exact ⟨rfl, post⟩ + +open Classical in +/-- A non-final notification has an identity finaliser. A final one +uses the joint entry at the successor of unbind and signal cancellation, +including its actual queue ghosts. Emptying the slot preserves the +architecture state returned by that finaliser. -/ +theorem irqDeleteNotification_entryA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} + {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} {ia ie : Nat} + {s : S} {a : A} {σ : State Unit} {m : McState} {sl : CslotPtr} + (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + (cov : SlotHeapCovA Z.th σ) (deviceFrames : DevFrDisjA st H OS s σ) + (tree : TreeA st OS Z.G.chains Z.G.par s) + (ranges : RngOneA64 st Z.G.chains s) (kind : KindA st OS s) + (slots : ∀ y, IsSlotA Z.th y → y ∈ OS) + (nullOutside : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) + (ht : AbsSlotA Z.th sl) {p b : U64} {send recv : Bool} (ntfn : H p = some .ntfn) + (cap : capAtA (L.mem s) (cslotAddrA sl) = .Notification p b send recv) + (finalize : isFinalCap' (absCapA (.Notification p b send recv)) σ → + objects.finalise_cap_object st ar s a (.Notification p b send recv) true ⦃ r => + r.2.2 = a ∧ ∃ σ1 Q R Ac1, + ((), σ1) ∈ (fastFinalise (Z := Unit) (absCapA (.Notification p b send recv)) true σ).1 ∧ + JEntA (L := L) AR {Z with Q := Q, R := R} H OS U Ac1 Pp Tb Lv ia ie r.2.1 a σ1 m ∧ + TreeA st OS Z.G.chains Z.G.par r.2.1 ∧ RngOneA64 st Z.G.chains r.2.1 ∧ KindA st OS r.2.1 ∧ + (∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st r.2.1 y = .Null) ∧ + capAtA (L.mem r.2.1) (cslotAddrA sl) ≠ .Null ⦄) : + cdt.delete_one st ar s a (cslotAddrA sl) ⦃ r => + r.2 = a ∧ ∃ σ2 Q R, + ((), σ2) ∈ (capDeleteOne (Z := Unit) sl σ).1 ∧ + JAtZAX (L := L) AR {Z with Q := Q, R := R, G := remGA Z.G (cslotAddrA sl)} r.1 a σ2 m ∧ + TreeA st OS (remGA Z.G (cslotAddrA sl)).chains (remGA Z.G (cslotAddrA sl)).par r.1 ∧ + RngOneA64 st (remGA Z.G (cslotAddrA sl)).chains r.1 ∧ KindA st OS r.1 ⦄ := by + let c := capability.Cap.Notification p b send recv + let fin := decide (isFinalCap' (absCapA c) σ) + let Mid := fun s1 (a1 : A) σ1 => a1 = a ∧ ∃ Q R Ac1, + JEntA (L := L) AR {Z with Q := Q, R := R} H OS U Ac1 Pp Tb Lv ia ie s1 a σ1 m ∧ + TreeA st OS Z.G.chains Z.G.par s1 ∧ RngOneA64 st Z.G.chains s1 ∧ KindA st OS s1 ∧ + (∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s1 y = .Null) ∧ + capAtA (L.mem s1) (cslotAddrA sl) ≠ .Null + have hfin := ntfnFinality_of_slotHeapA cov E.j.1.st E.j.1.place tree.lk ranges kind E.sys.ent.os.nz + slots nullOutside E.sys.sysl.sys.fr ht ntfn cap deviceFrames + have hf : objects.finalise_cap_object st ar s a c fin ⦃ r => + ∃ σ1, ((), σ1) ∈ (fastFinalise (Z := Unit) (absCapA c) fin σ).1 ∧ Mid r.2.1 r.2.2 σ1 ⦄ := by + by_cases h : isFinalCap' (absCapA c) σ + · have eq : fin = true := decide_eq_true h + rw [eq] + refine WP.spec_mono (finalize h) ?_ + rintro r ⟨har, σ1, Q, R, Ac1, outcome, rest⟩ + exact ⟨σ1, outcome, har, Q, R, Ac1, rest⟩ + · have eq : fin = false := decide_eq_false h + rw [eq] + simp only [c, objects.finalise_cap_object, Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + exact ⟨σ, rfl, rfl, Z.Q, Z.R, Ac, E, tree, ranges, kind, nullOutside, + by rw [cap]; simp⟩ + have main := irqDeleteNotificationA E.j.1.st ht cap hfin (M := Mid) + (R := fun s2 (a2 : A) σ2 => a2 = a ∧ ∃ Q R, + JAtZAX (L := L) AR {Z with Q := Q, R := R, G := remGA Z.G (cslotAddrA sl)} s2 a σ2 m ∧ + TreeA st OS (remGA Z.G (cslotAddrA sl)).chains (remGA Z.G (cslotAddrA sl)).par s2 ∧ + RngOneA64 st (remGA Z.G (cslotAddrA sl)).chains s2 ∧ KindA st OS s2) hf ?_ + · refine WP.spec_mono main ?_ + rintro r ⟨σ2, step, har, Q, R, post⟩ + exact ⟨har, σ2, Q, R, step, post⟩ + · rintro s1 _ σ1 ⟨rfl, Q, R, Ac1, E1, tree1, ranges1, kind1, outside1, live1⟩ + refine WP.spec_mono (empty_slot_jA E1 tree1 ranges1 kind1 slots outside1 ht live1) ?_ + rintro r ⟨har, σ2, outcome, post⟩ + exact ⟨σ2, outcome, har, Q, R, post⟩ + +set_option maxHeartbeats 800000 in +/-- Clear returns the joint delete post and the concrete entry frame +from that same successor. -/ +theorem invokeIrqClear_entryFrameA {H : Heap} {Cs OS Vc Ub Pt Ac : Set U64} {ia ie : Nat} + {K : capability.Cap → Prop} {Ty : S → Prop} {s0 s : S} {a : A} + {th : TypedHeapA} {σ : State Unit} {irq : U64} + (I : DelIn st ar H Cs OS Vc Ub Pt K Ty s0) + (R : DelR st H Cs OS Vc Ub Ac Ty ia ie s0 s) + (irqRel : IrqRelA (L.mem s) (L.ksb s) σ) (place : PlaceA th (L.ksb s)) + (hi : irq.val < 5376) {Post : S → A → State Unit → Prop} + (del : let sl : CslotPtr := (σ.interruptIrqNode (BitVec.ofNat 13 irq.val), []) + cdt.delete_one st ar s a (cslotAddrA sl) ⦃ r => + ∃ σ', ((), σ') ∈ (capDeleteOne (Z := Unit) sl σ).1 ∧ Post r.1 r.2 σ' ⦄) : + (do let q ← interrupt.irq_slot st s irq + cdt.delete_one st ar s a q) ⦃ r => + ∃ σ', ((), σ') ∈ (invokeIrqHandler (Z := Unit) (.clearIRQHandler (BitVec.ofNat 13 irq.val)) σ).1 ∧ + Post r.1 r.2 σ' ∧ SysL st H Cs OS ia ie r.1 ∧ SymL st H Cs r.1 ⦄ := by + refine invokeIrqClear_refinesA irqRel place hi + (R := fun s1 a1 σ1 => Post s1 a1 σ1 ∧ SysL st H Cs OS ia ie s1 ∧ SymL st H Cs s1) ?_ + refine WP.spec_mono (irqDeleteOne_entryFrameA I R irqRel place hi del) ?_ + rintro r ⟨⟨σ', step, joint⟩, concrete, _⟩ + exact ⟨σ', step, joint, concrete⟩ + +/-- Set inserts into the slot that the preceding delete cleared and +uses the source cap preserved off the IRQ node. The insertion +continuation receives the same concrete and abstract delete successor. -/ +theorem invokeIrqSet_entryFrameA {H : Heap} {Cs OS Vc Ub Pt Ac : Set U64} {ia ie : Nat} + {K : capability.Cap → Prop} {Ty : S → Prop} {s0 s : S} {a : A} + {th : TypedHeapA} {σ : State Unit} {irq : U64} + (I : DelIn st ar H Cs OS Vc Ub Pt K Ty s0) + (R : DelR st H Cs OS Vc Ub Ac Ty ia ie s0 s) + (irqRel : IrqRelA (L.mem s) (L.ksb s) σ) (place : PlaceA th (L.ksb s)) + (hi : irq.val < 5376) {p b : U64} {recv : Bool} (src : CslotPtr) + (srcOS : cslotAddrA src ∈ OS) (srcOff : ¬ IslN (L.ksb s) (cslotAddrA src)) + (srcCap : capA st s (cslotAddrA src) = .Notification p b true recv) + {Deleted Post : S → A → State Unit → Prop} + (del : let sl : CslotPtr := (σ.interruptIrqNode (BitVec.ofNat 13 irq.val), []) + cdt.delete_one st ar s a (cslotAddrA sl) ⦃ r => + ∃ σ', ((), σ') ∈ (capDeleteOne (Z := Unit) sl σ).1 ∧ Deleted r.1 r.2 σ' ⦄) + (insert : ∀ s1 a1 σ1, Deleted s1 a1 σ1 → + SysL st H Cs OS ia ie s1 → SymL st H Cs s1 → + let sl : CslotPtr := (σ.interruptIrqNode (BitVec.ofNat 13 irq.val), []) + capAtA (L.mem s1) (cslotAddrA sl) = .Null → + capA st s1 (cslotAddrA src) = .Notification p b true recv → + cdt.insert st s1 (.Notification p b true recv) (cslotAddrA src) (cslotAddrA sl) ⦃ s2 => + ∃ σ2, ((), σ2) ∈ (capInsert (Z := Unit) (absCapA (.Notification p b true recv)) src sl σ1).1 ∧ + Post s2 a1 σ2 ⦄) : + (do let q ← interrupt.irq_slot st s irq + let (s1, a1) ← cdt.delete_one st ar s a q + let s2 ← cdt.insert st s1 (.Notification p b true recv) (cslotAddrA src) q + ok (s2, a1)) ⦃ r => + ∃ σ', ((), σ') ∈ (invokeIrqHandler (Z := Unit) (.setIRQHandler + (BitVec.ofNat 13 irq.val) (absCapA (.Notification p b true recv)) src) σ).1 ∧ Post r.1 r.2 σ' ⦄ := by + let sl : CslotPtr := (σ.interruptIrqNode (BitVec.ofNat 13 irq.val), []) + let Mid := fun s1 a1 σ1 => Deleted s1 a1 σ1 ∧ + SysL st H Cs OS ia ie s1 ∧ SymL st H Cs s1 ∧ + capAtA (L.mem s1) (cslotAddrA sl) = .Null ∧ + capA st s1 (cslotAddrA src) = .Notification p b true recv + refine invokeIrqSet_refinesA irqRel place hi src (M := Mid) ?_ ?_ + · refine WP.spec_mono (irqDeleteOne_entryFrameA I R irqRel place hi del) ?_ + rintro r ⟨⟨σ1, step, joint⟩, ⟨sys, sym⟩, _, _, empty, preserved⟩ + exact ⟨σ1, step, joint, sys, sym, empty, (preserved _ srcOS srcOff).trans srcCap⟩ + · rintro s1 a1 σ1 ⟨joint, sys, sym, empty, source⟩ + exact insert s1 a1 σ1 joint sys sym empty source + +end +end +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/IrqFinalCoverageA.lean b/proofs/Fl4ProofsA64/P3/IrqFinalCoverageA.lean new file mode 100644 index 000000000..1cf85d895 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/IrqFinalCoverageA.lean @@ -0,0 +1,119 @@ +/- +P3 (AArch64): project the IRQ notification finality test from the +shared typed store and capability-indexed MDB invariants. + +[J] SlotHeapCovA is the converse object coverage from the frozen heap +correspondence. Abstract object references and concrete same-object +tests agree here if frame cap bases are distinct from the notification. +KindA proves the corresponding exclusion for non-frame caps. +-/ +import Fl4ProofsA64.P3.SlotCoverageA +import Fl4ProofsA64.P3.DevFrDisjA +import Fl4ProofsA64.P3.IrqInvokeFinalA +import Fl4ProofsA64.P3.FinDefsA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4SpecA64 + +noncomputable section + +private theorem objRef_of_absCapA {c : capability.Cap} {p : U64} + (h : GenObjRef.objRef (specPtr p) ∈ genObjRefs (absCapA c)) : + ptrA c = p ∧ (RngKeyA c ∨ isFrA c = true) := by + have base := ptrA_eq c + have phys := physA_eq c + cases c + case Frame q size rights dev mapped ept asid va => + cases ept <;> + simp [genObjRefs, objRefs, capIrqs, capIrqOpt, archCapSetMap, absCapA, absFrameE, + aobjRef, ArchCap.acapObj, ExtArchCap.objRef, specPtr_iff] at h <;> + exact ⟨(okinj base).symm.trans (by first | exact h | exact h.symm), Or.inr rfl⟩ + all_goals + simp [genObjRefs, objRefs, capIrqs, capIrqOpt, archCapSetMap, absCapA, + aobjRef, ArchCap.acapObj, ExtArchCap.objRef, specPtr_iff] at h + all_goals + exact ⟨(okinj base).symm.trans (by first | exact h | exact h.symm), + Or.inl ⟨(okinj phys).symm, rfl, rfl⟩⟩ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- Project the finality coverage for the selected notification. Frame +object names need the same separation from notification names in the +abstract heap that the physical capabilities have. -/ +theorem SlotHeapCovA.ntfnFinalCover {th : TypedHeapA} {s : S} {σ : State Unit} {OS : Set U64} + (cov : SlotHeapCovA th σ) (rel : StateRelA th (L.mem s) σ) (place : PlaceA th (L.ksb s)) + (kind : KindA st OS s) (slots : ∀ y, IsSlotA th y → y ∈ OS) + {sl : CslotPtr} (hsl : AbsSlotA th sl) {p b : U64} {send recv : Bool} + (cap : capA st s (cslotAddrA sl) = .Notification p b send recv) + (frameAway : ∀ y ∈ OS, isFrA (capA st s y) = true → ptrA (capA st s y) ≠ p) + {q : CslotPtr} {c : Cap} (hg : (Fl4SpecA64.getCap (Z := Unit) q σ).1 = {(c, σ)}) + (href : GenObjRef.objRef (specPtr p) ∈ genObjRefs c) : + AbsSlotA th q ∧ ∃ b' send' recv', capA st s (cslotAddrA q) = .Notification p b' send' recv' := by + have hg' : (c, σ) ∈ (Fl4SpecA64.getCap (Z := Unit) q σ).1 := by rw [hg]; rfl + obtain ⟨hq, hr⟩ := cov.getCap rel hg' + let d := capA st s (cslotAddrA q) + have hd : CapEqA d c := by simpa only [d, capAtA_eq] using hr.2 + rw [CapEqA.genObjRefs_eq hd] at href + obtain ⟨hp, hr | hf⟩ := objRef_of_absCapA href + · have hn : RngKeyA (.Notification p b send recv) := ⟨(okinj (physA_eq (.Notification p b send recv))).symm, rfl, rfl⟩ + have hbase : ptrA (.Notification p b send recv) = p := + (okinj (ptrA_eq (.Notification p b send recv))).symm + have he := kind.kind _ (slots _ (hsl.isSlot place)) _ (slots _ (hq.isSlot place)) + (cap ▸ hn) hr (by rw [cap, hbase]; exact hp.symm) + rw [cap] at he + change kindA (.Notification p b send recv) = kindA d at he + refine ⟨hq, ?_⟩ + change ∃ b' send' recv', d = .Notification p b' send' recv' + revert hp he + generalize d = k + intro hp he + cases k <;> simp only [kindA] at he <;> try omega + rename_i p' b' s' r' + have ep : ptrA (.Notification p' b' s' r') = p' := + (okinj (ptrA_eq (.Notification p' b' s' r'))).symm + rw [ep] at hp + exact ⟨b', s', r', by rw [hp]⟩ + · exact (frameAway _ (slots _ (hq.isSlot place)) hf hp).elim + +open Classical in +/-- Notification finality from the frozen slot coverage and the MDB range +invariants. P1's FrameOK excludes RAM frames at a notification's base. +[J] DevFrDisjA extends the cross-kind separation to device-backed frames. -/ +theorem ntfnFinality_of_slotHeapA {s : S} {σ : State Unit} {th : TypedHeapA} {H : Heap} + {OS : Set U64} {chains : List (List U64)} + (cov : SlotHeapCovA th σ) (rel : StateRelA th (L.mem s) σ) (place : PlaceA th (L.ksb s)) + (links : MdbLk st OS chains s) (ranges : RngOneA64 st chains s) (kind : KindA st OS s) + (nonzero : ∀ y ∈ OS, y ≠ 0#u64) (slots : ∀ y, IsSlotA th y → y ∈ OS) + (nullOutside : ∀ y ∈ OS, ¬ IsSlotA th y → capA st s y = .Null) + (frames : FrameOK st H OS s) {sl : CslotPtr} (hsl : AbsSlotA th sl) + {p b : U64} {send recv : Bool} (ntfn : H p = some .ntfn) + (cap : capAtA (L.mem s) (cslotAddrA sl) = .Notification p b send recv) + (deviceFrames : DevFrDisjA st H OS s σ) : + cdt.is_final_capability_with_cap st s (cslotAddrA sl) (.Notification p b send recv) = + ok (decide (isFinalCap' (absCapA (.Notification p b send recv)) σ)) := by + refine ntfnFinality_refinesA rel place links ranges kind nonzero slots nullOutside hsl cap ?_ + have cap' : capA st s (cslotAddrA sl) = .Notification p b send recv := by + simpa only [capAtA_eq] using cap + apply cov.ntfnFinalCover rel place kind slots hsl cap' + intro y hy hf eq + cases hcap : capA st s y <;> simp only [hcap, isFrA, Bool.false_eq_true] at hf + case Frame q size rights dev mapped ept asid va => + rw [hcap] at eq + have base := (okinj (ptrA_eq (.Frame q size rights dev mapped ept asid va))).symm + have same : q = p := base.symm.trans eq + subst q + cases dev + · obtain ⟨bits, hbits⟩ : ∃ bits, size.bits = ok bits := by + cases size <;> exact ⟨_, rfl⟩ + have hFrameType := frames y hy p size rights mapped ept asid va hcap bits hbits + rw [ntfn] at hFrameType + cases hFrameType + · obtain ⟨⟨bits, htype⟩, _⟩ := deviceFrames y hy p size rights mapped ept asid va hcap .ntfn ntfn + cases htype + +end +end +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/IrqInvokeDispatchA.lean b/proofs/Fl4ProofsA64/P3/IrqInvokeDispatchA.lean new file mode 100644 index 000000000..9a7098d03 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/IrqInvokeDispatchA.lean @@ -0,0 +1,314 @@ +/- +P3 (AArch64): IRQControl and IRQHandler decoder boundaries. +The configured trigger and route remain explicit in the concrete result. +[P] Relating the architecture part requires the hardware operations: +label 65 also routes the SPI to core zero, and FL4 Ack unmasks the IRQ. +The spec's existing IRQ state and node relation is used for active tests. +-/ +import Fl4ProofsA64.P3.DelSpecA +import Fl4ProofsA64.P3.JointA +import Fl4ProofsA64.P3.ScSpecA +import Fl4ProofsA64.P1.QueueDecIrq + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4SpecA64 + +noncomputable section + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {s : S} {a : A} {lbl len src buf irq : U64} {b c d fp : Bool} +variable {extra : Array obj.Slot 3#usize} + +theorem decodeIrqControl_programA : + syscall.decode_invocation st ar s a lbl len src .IrqControl b c d fp buf extra = + interrupt.decode_irq_control st ar s a lbl len src buf extra := by + simp only [syscall.decode_invocation, cur_thread_ok, bind_ok] + +theorem decodeIrqHandler_programA : + syscall.decode_invocation st ar s a lbl len src (.IrqHandler irq) b c d fp buf extra = + interrupt.decode_irq_handler st ar s a lbl irq extra := by + simp only [syscall.decode_invocation, cur_thread_ok, bind_ok] + +/-- The IRQ issue write sequence, after the argument, active and slot guards. -/ +def irqIssueProgramA (st : machine.Store S) (ar : machine.Arch A) (s : S) (a : A) + (ct irq dest src : U64) (edge : Bool) (cpu : U64) : Result (core.result.Result Unit error.Exc × S × A) := do + let s1 ← sched.set_thread_state st s ct sched.TS_RESTART + let a1 ← ar.configure_irq a irq edge cpu + let (s2, a2) ← interrupt.invoke_irq_control st ar s1 a1 irq dest src + ok (.Ok (), s2, a2) + +/-- The plain issue sequence leaves the IRQ route and trigger untouched. -/ +def irqIssuePlainProgramA (st : machine.Store S) (ar : machine.Arch A) (s : S) (a : A) + (ct irq dest src : U64) : Result (core.result.Result Unit error.Exc × S × A) := do + let s1 ← sched.set_thread_state st s ct sched.TS_RESTART + let (s2, a2) ← interrupt.invoke_irq_control st ar s1 a irq dest src + ok (.Ok (), s2, a2) + +local macro "irq_label_simp" : tactic => `(tactic| simp only [ + if_neg (show fl4_abi.label.ARMIRQIssueIRQHandlerTrigger ≠ fl4_abi.label.ARMIRQIssueIRQHandlerMSI from by rw [fl4_abi.label.ARMIRQIssueIRQHandlerTrigger, fl4_abi.label.ARMIRQIssueIRQHandlerMSI]; decide), + if_neg (show fl4_abi.label.ARMIRQIssueIRQHandlerTrigger ≠ fl4_abi.label.IRQIssueIRQHandler from by rw [fl4_abi.label.ARMIRQIssueIRQHandlerTrigger, fl4_abi.label.IRQIssueIRQHandler]; decide), + if_neg (show fl4_abi.label.ARMIRQIssueIRQHandlerTrigger ≠ fl4_abi.label.ARMIRQIssueIRQHandlerTriggerCore from by rw [fl4_abi.label.ARMIRQIssueIRQHandlerTrigger, fl4_abi.label.ARMIRQIssueIRQHandlerTriggerCore]; decide), + if_neg (show fl4_abi.label.ARMIRQIssueIRQHandlerTriggerCore ≠ fl4_abi.label.ARMIRQIssueIRQHandlerMSI from by rw [fl4_abi.label.ARMIRQIssueIRQHandlerTriggerCore, fl4_abi.label.ARMIRQIssueIRQHandlerMSI]; decide), + if_neg (show fl4_abi.label.ARMIRQIssueIRQHandlerTriggerCore ≠ fl4_abi.label.IRQIssueIRQHandler from by rw [fl4_abi.label.ARMIRQIssueIRQHandlerTriggerCore, fl4_abi.label.IRQIssueIRQHandler]; decide), + if_neg (show fl4_abi.label.IRQSetIRQHandler ≠ fl4_abi.label.IRQAckIRQ from by rw [fl4_abi.label.IRQSetIRQHandler, fl4_abi.label.IRQAckIRQ]; decide), + if_neg (show fl4_abi.label.IRQClearIRQHandler ≠ fl4_abi.label.IRQAckIRQ from by rw [fl4_abi.label.IRQClearIRQHandler, fl4_abi.label.IRQAckIRQ]; decide), + if_neg (show fl4_abi.label.IRQClearIRQHandler ≠ fl4_abi.label.IRQSetIRQHandler from by rw [fl4_abi.label.IRQClearIRQHandler, fl4_abi.label.IRQSetIRQHandler]; decide), + if_pos rfl, if_true, if_false, cur_thread_ok, bind_ok, uncurry_apply_pair]) + +theorem irqIssuePlain_acceptA {id index depth max dest : U64} {root : capability.Cap} + (he : interrupt.extra0 st s extra = ok (some root)) (hlen : len ≥ 3#u64) + (h0 : msg.syscall_arg st s (L.curW s) buf 0#u64 = ok id) + (h1 : msg.syscall_arg st s (L.curW s) buf 1#u64 = ok index) + (h2 : msg.syscall_arg st s (L.curW s) buf 2#u64 = ok depth) + (hm : ar.max_spi a = ok max) (hi : ¬ id > max) + (hnum : layout.irq.of id 0#u64 = ok irq) + (hactive : interrupt.is_irq_active st s irq = ok false) + (hslot : cspace.lookup_target_slot st s root index depth = ok (.Ok dest)) + (hempty : cdt.ensure_empty_slot st s dest = ok (.Ok ())) : + interrupt.decode_irq_control st ar s a fl4_abi.label.IRQIssueIRQHandler len src buf extra = + irqIssuePlainProgramA st ar s a (L.curW s) irq dest src := by + have hmsi : fl4_abi.label.IRQIssueIRQHandler ≠ fl4_abi.label.ARMIRQIssueIRQHandlerMSI := by + rw [fl4_abi.label.IRQIssueIRQHandler, fl4_abi.label.ARMIRQIssueIRQHandlerMSI] + decide + have hcore : fl4_abi.label.IRQIssueIRQHandler ≠ fl4_abi.label.ARMIRQIssueIRQHandlerTriggerCore := by + rw [fl4_abi.label.IRQIssueIRQHandler, fl4_abi.label.ARMIRQIssueIRQHandlerTriggerCore] + decide + unfold interrupt.decode_irq_control + simp only [if_neg hmsi, if_neg hcore, if_pos rfl, cur_thread_ok, bind_ok, uncurry_apply_pair, + he, h0, h1, h2, hm, if_pos hlen, if_neg hi, hnum, hactive, hslot, hempty, P1.irq_branch_ok, + Bool.false_eq_true, ↓reduceIte, irqIssuePlainProgramA] + +theorem irqTrigger_acceptA {id trigger index depth max dest : U64} {root : capability.Cap} + (he : interrupt.extra0 st s extra = ok (some root)) (hlen : len ≥ 4#u64) + (h0 : msg.syscall_arg st s (L.curW s) buf 0#u64 = ok id) + (h1 : msg.syscall_arg st s (L.curW s) buf 1#u64 = ok trigger) + (h2 : msg.syscall_arg st s (L.curW s) buf 2#u64 = ok index) + (h3 : msg.syscall_arg st s (L.curW s) buf 3#u64 = ok depth) + (hm : ar.max_spi a = ok max) (hi : ¬ id > max) (hspi : ¬ id < layout.irq.SPI_MIN) + (hnum : layout.irq.of id 0#u64 = ok irq) + (hactive : interrupt.is_irq_active st s irq = ok false) + (hslot : cspace.lookup_target_slot st s root index depth = ok (.Ok dest)) + (hempty : cdt.ensure_empty_slot st s dest = ok (.Ok ())) : + interrupt.decode_irq_control st ar s a fl4_abi.label.ARMIRQIssueIRQHandlerTrigger len src buf extra = + irqIssueProgramA st ar s a (L.curW s) irq dest src (trigger != 0#u64) 0#u64 := by + unfold interrupt.decode_irq_control + irq_label_simp + simp only [he, h0, h1, h2, h3, hm, if_pos hlen, if_neg hi, if_neg hspi, bne_self_eq_false, + hnum, hactive, hslot, hempty, P1.irq_branch_ok, if_true, if_false, Bool.false_eq_true, ↓reduceIte, + bind_ok, uncurry_apply_pair, irqIssueProgramA] + +theorem irqTriggerCore_acceptA {id trigger index depth cpu max nodes dest : U64} {root : capability.Cap} + (he : interrupt.extra0 st s extra = ok (some root)) (hlen : len ≥ 5#u64) + (h0 : msg.syscall_arg st s (L.curW s) buf 0#u64 = ok id) + (h1 : msg.syscall_arg st s (L.curW s) buf 1#u64 = ok trigger) + (h2 : msg.syscall_arg st s (L.curW s) buf 2#u64 = ok index) + (h3 : msg.syscall_arg st s (L.curW s) buf 3#u64 = ok depth) + (h4 : msg.syscall_arg st s (L.curW s) buf 4#u64 = ok cpu) + (hm : ar.max_spi a = ok max) (hi : ¬ id > max) + (hn : obj.ks_get st s layout.ks.NUM_NODES = ok nodes) (hcpu : ¬ cpu ≥ nodes) + (hnum : layout.irq.of id cpu = ok irq) + (hactive : interrupt.is_irq_active st s irq = ok false) + (hslot : cspace.lookup_target_slot st s root index (depth &&& 255#u64) = ok (.Ok dest)) + (hempty : cdt.ensure_empty_slot st s dest = ok (.Ok ())) : + interrupt.decode_irq_control st ar s a fl4_abi.label.ARMIRQIssueIRQHandlerTriggerCore len src buf extra = + irqIssueProgramA st ar s a (L.curW s) irq dest src (trigger != 0#u64) cpu := by + unfold interrupt.decode_irq_control + irq_label_simp + simp only [he, h0, h1, h2, h3, h4, hm, if_pos hlen, if_neg hi, hn, if_neg hcpu, + hnum, hactive, hslot, hempty, P1.irq_branch_ok, if_true, if_false, Bool.false_eq_true, ↓reduceIte, + bind_ok, uncurry_apply_pair, irqIssueProgramA] + +theorem irqAck_programA : + interrupt.decode_irq_handler st ar s a fl4_abi.label.IRQAckIRQ irq extra = (do + let s1 ← sched.set_thread_state st s (L.curW s) sched.TS_RESTART + let a1 ← ar.mask_irq a irq false + ok (.Ok (), s1, a1)) := by + simp only [interrupt.decode_irq_handler, cur_thread_ok, bind_ok, if_pos rfl, if_true] + +theorem irqSet_programA {slot p badge : U64} {recv : Bool} + (he : Array.index_usize extra 0#usize = ok slot) (hx : slot ≠ 0#u64) + (hc : obj.Slot.cap st slot s = ok (.Notification p badge true recv)) : + interrupt.decode_irq_handler st ar s a fl4_abi.label.IRQSetIRQHandler irq extra = (do + let s1 ← sched.set_thread_state st s (L.curW s) sched.TS_RESTART + let isl ← interrupt.irq_slot st s1 irq + let (s2, a1) ← cdt.delete_one st ar s1 a isl + let s3 ← cdt.insert st s2 (.Notification p badge true recv) slot isl + ok (.Ok (), s3, a1)) := by + unfold interrupt.decode_irq_handler + irq_label_simp + simp only [he, obj.Slot.is_null, bind_ok, decide_eq_true_eq, if_neg hx, hc, ↓reduceIte] + +theorem irqClear_programA : + interrupt.decode_irq_handler st ar s a fl4_abi.label.IRQClearIRQHandler irq extra = (do + let s1 ← sched.set_thread_state st s (L.curW s) sched.TS_RESTART + let isl ← interrupt.irq_slot st s1 irq + let (s2, a1) ← cdt.delete_one st ar s1 a isl + ok (.Ok (), s2, a1)) := by + unfold interrupt.decode_irq_handler + irq_label_simp + +/-- The active check uses the same bounded IRQ as the spec relation. -/ +theorem irqActive_refinesA {σ : State Unit} (H : IrqRelA (L.mem s) (L.ksb s) σ) (hi : irq.val < 5376) : + interrupt.is_irq_active st s irq ⦃ active => + isIrqActive (Z := Unit) (BitVec.ofNat 13 irq.val) σ = ({(active, σ)}, False) ⦄ := by + have off : interrupt.state_off irq = ok (irqOffA irq) := by + have const : layout.ks.IRQ_STATE = ok 224#u64 := + eq_of_spec_fd Fl4ProofsA64.P0.Consts.layout.ks.IRQ_STATE.spec + unfold interrupt.state_off irqOffA + simp only [lift, bind_ok, const] + unfold interrupt.is_irq_active interrupt.irq_state + rw [off, bind_ok, eq_of_spec_fd (obj.ks_get.spec (L := L) s _), bind_ok] + rw [WP.spec_ok] + have hactive : (L.mem s (irqWordA (L.ksb s) irq) != interrupt.IRQ_INACTIVE) = + decide (σ.interruptStates (BitVec.ofNat 13 irq.val) ≠ .irqInactive) := by + rw [interrupt.IRQ_INACTIVE, H.st irq hi] + apply Bool.eq_iff_iff.2 + simp [bne_iff_ne, irqStOfA_inactive] + change _ = ({(L.mem s (irqWordA (L.ksb s) irq) != interrupt.IRQ_INACTIVE, σ)}, False) + rw [hactive] + change (Fl4Spec.NonDet.gets + (fun s : State Unit => s.interruptStates (BitVec.ofNat 13 irq.val)) >>= fun state => + pure (decide (state ≠ IrqState.irqInactive))) σ = _ + rw [Sched.nd_gets_bindSc] + rfl + +end + +/-- The ABI uses 65 for GetTrigger and 66 for GetTriggerCore. Translate +before dispatch: GetTriggerCore is an FL4 extension label. -/ +theorem irqIssue_labelsA : + fl4LabelOf fl4_abi.label.ARMIRQIssueIRQHandlerTrigger.bv = + .spec (.archInvocationLabel .armIRQIssueIRQHandlerTrigger) ∧ + fl4LabelOf fl4_abi.label.ARMIRQIssueIRQHandlerTriggerCore.bv = + .ext .armIRQIssueIRQHandlerTriggerCore := by + constructor + · rw [fl4_abi.label.ARMIRQIssueIRQHandlerTrigger] + rfl + · rw [fl4_abi.label.ARMIRQIssueIRQHandlerTriggerCore] + rfl + +open Classical in +/-- Label 29 selects the plain issue operation after the target checks. -/ +theorem irqIssuePlain_specA {σ : State Unit} {id index depth : Data} + {src dest : CslotPtr} {cap : Cap} (args : List Data) (caps : List (Cap × CslotPtr)) + (hmax : ¬ id > fl4A64Platform.maxSpi) + (ht : (fl4IrqTarget (Z := Unit) (ucast irqLen (fl4IrqOf id 0)) cap index depth).run σ = + ({(.ok dest, σ)}, False)) : + (fl4DecodeInvocation (Z := Unit) false false fl4_abi.label.IRQIssueIRQHandler.bv + (id :: index :: depth :: args) [] src .irqControlCap ((cap, src) :: caps) none).run σ = + ({(.ok (.invokeIRQControl (.spec (.irqControl (ucast irqLen (fl4IrqOf id 0)) dest src))), σ)}, False) := by + have hl : fl4LabelOf fl4_abi.label.IRQIssueIRQHandler.bv = + .spec (.genInvocationLabel .irqIssueIRQHandler) := by + rw [fl4_abi.label.IRQIssueIRQHandler] + rfl + simp only [fl4DecodeInvocation, hl, decodeFl4IrqControl, decodeFl4IrqIssue] + change (Fl4Spec.NonDetE.liftME Fl4Invocation.invokeIRQControl (do + Fl4Spec.NonDetE.whenE (id > fl4A64Platform.maxSpi) (Fl4Spec.NonDetE.throwError (.rangeError 0 fl4A64Platform.maxSpi)) + Fl4Spec.NonDetE.whenE (0 = 2 ∧ (0 : Word).toNat ≥ fl4A64Platform.numNodes) + (Fl4Spec.NonDetE.throwError (.invalidArgument 4)) + Fl4Spec.NonDetE.whenE (0 = 1 ∧ id < 32) (Fl4Spec.NonDetE.throwError .illegalOperation) + let out ← fl4IrqTarget (Z := Unit) (ucast irqLen (fl4IrqOf id 0)) cap index depth + Fl4Spec.NonDetE.returnOk (.spec (.irqControl (ucast irqLen (fl4IrqOf id 0)) out src)))).run σ = _ + simp only [Fl4Spec.NonDetE.whenE, if_neg hmax, Nat.reduceEqDiff, false_and, if_false, + Fl4Spec.NonDetE.liftME, Fl4Spec.NonDetE.returnOk, ndE_pure_bindD] + rw [run_bind_okD (run_bind_okD ht _)] + rfl + +open Classical in +/-- A valid label-65 request selects the trigger-and-route operation on core zero. The target +premise is the abstract CSpace lookup and ensure-empty result. -/ +theorem irqTrigger_specA {σ : State Unit} {id trigger index depth : Data} + {src dest : CslotPtr} {cap : Cap} (args : List Data) (caps : List (Cap × CslotPtr)) + (hmax : ¬ id > fl4A64Platform.maxSpi) (hspi : ¬ id < 32) + (ht : (fl4IrqTarget (Z := Unit) (ucast irqLen (fl4IrqOf id 0)) cap index depth).run σ = + ({(.ok dest, σ)}, False)) : + (fl4DecodeInvocation (Z := Unit) false false fl4_abi.label.ARMIRQIssueIRQHandlerTrigger.bv + (id :: trigger :: index :: depth :: args) [] src .irqControlCap ((cap, src) :: caps) none).run σ = + ({(.ok (.invokeIRQControl (.triggerCore + (ucast irqLen (fl4IrqOf id 0)) dest src (decide (trigger ≠ 0)) 0)), σ)}, False) := by + simp only [fl4DecodeInvocation, irqIssue_labelsA.1, decodeFl4IrqControl, decodeFl4IrqIssue] + change (Fl4Spec.NonDetE.liftME Fl4Invocation.invokeIRQControl (do + Fl4Spec.NonDetE.whenE (id > fl4A64Platform.maxSpi) (Fl4Spec.NonDetE.throwError (.rangeError 0 fl4A64Platform.maxSpi)) + Fl4Spec.NonDetE.whenE (1 = 2 ∧ (0 : Word).toNat ≥ fl4A64Platform.numNodes) + (Fl4Spec.NonDetE.throwError (.invalidArgument 4)) + Fl4Spec.NonDetE.whenE (1 = 1 ∧ id < 32) (Fl4Spec.NonDetE.throwError .illegalOperation) + let out ← fl4IrqTarget (Z := Unit) (ucast irqLen (fl4IrqOf id 0)) cap index depth + Fl4Spec.NonDetE.returnOk (.triggerCore (ucast irqLen (fl4IrqOf id 0)) out src + (decide (1 ≠ 0 ∧ trigger ≠ 0)) 0))).run σ = _ + simp only [Fl4Spec.NonDetE.whenE, if_neg hmax, Nat.reduceEqDiff, false_and, true_and, if_false, + if_neg hspi, Fl4Spec.NonDetE.liftME, Fl4Spec.NonDetE.returnOk, ndE_pure_bindD] + rw [run_bind_okD (run_bind_okD ht _)] + rfl + +open Classical in +/-- Label 66 selects the core-specific operation and masks the supplied +depth to eight bits before the target lookup. -/ +theorem irqTriggerCore_specA {σ : State Unit} {id trigger index depth cpu : Data} + {src dest : CslotPtr} {cap : Cap} (args : List Data) (caps : List (Cap × CslotPtr)) + (hmax : ¬ id > fl4A64Platform.maxSpi) (hcpu : ¬ cpu.toNat ≥ fl4A64Platform.numNodes) + (ht : (fl4IrqTarget (Z := Unit) (ucast irqLen (fl4IrqOf id cpu)) cap index (depth &&& 0xff)).run σ = + ({(.ok dest, σ)}, False)) : + (fl4DecodeInvocation (Z := Unit) false false fl4_abi.label.ARMIRQIssueIRQHandlerTriggerCore.bv + (id :: trigger :: index :: depth :: cpu :: args) [] src .irqControlCap ((cap, src) :: caps) none).run σ = + ({(.ok (.invokeIRQControl (.triggerCore (ucast irqLen (fl4IrqOf id cpu)) dest src + (decide (trigger ≠ 0)) cpu)), σ)}, False) := by + simp only [fl4DecodeInvocation, irqIssue_labelsA.2, decodeFl4IrqControl, decodeFl4IrqIssue] + change (Fl4Spec.NonDetE.liftME Fl4Invocation.invokeIRQControl (do + Fl4Spec.NonDetE.whenE (id > fl4A64Platform.maxSpi) (Fl4Spec.NonDetE.throwError (.rangeError 0 fl4A64Platform.maxSpi)) + Fl4Spec.NonDetE.whenE (2 = 2 ∧ cpu.toNat ≥ fl4A64Platform.numNodes) + (Fl4Spec.NonDetE.throwError (.invalidArgument 4)) + Fl4Spec.NonDetE.whenE (2 = 1 ∧ id < 32) (Fl4Spec.NonDetE.throwError .illegalOperation) + let out ← fl4IrqTarget (Z := Unit) (ucast irqLen (fl4IrqOf id cpu)) cap index (depth &&& 0xff) + Fl4Spec.NonDetE.returnOk (.triggerCore (ucast irqLen (fl4IrqOf id cpu)) out src + (decide (2 ≠ 0 ∧ trigger ≠ 0)) cpu))).run σ = _ + simp only [Fl4Spec.NonDetE.whenE, if_neg hmax, Nat.reduceEqDiff, false_and, true_and, if_false, + if_neg hcpu, Fl4Spec.NonDetE.liftME, Fl4Spec.NonDetE.returnOk, ndE_pure_bindD] + rw [run_bind_okD (run_bind_okD ht _)] + rfl + +private theorem irqDecode_run_iteA {p : Prop} [Decidable p] + (m n : SEMonad Unit IrqHandlerInvocation) (σ : State Unit) : + (if p then m else n).run σ = if p then m.run σ else n.run σ := by + by_cases h : p + · simp only [if_pos h] + · simp only [if_neg h] + +open Classical in +/-- Exact abstract branch selection on IRQHandler, including the +truncated and invalid-cap outcomes of Set. -/ +theorem irqHandler_specA {σ : State Unit} {label : Data} {irq : Irq} + (caps : List (Cap × CslotPtr)) : + (decodeIrqHandlerInvocation (Z := Unit) label irq caps).run σ = + ({((match genInvocationType label with + | .irqAckIRQ => .ok (.aCKIrq irq) + | .irqSetIRQHandler => match caps with + | [] => .error .truncatedMessage + | (cap, slot) :: _ => + if isNtfnCap cap ∧ AllowSend ∈ capRights cap + then .ok (.setIRQHandler irq cap slot) + else .error (.invalidCapability 0) + | .irqClearIRQHandler => .ok (.clearIRQHandler irq) + | _ => .error .illegalOperation), σ)}, False) := by + classical + unfold decodeIrqHandlerInvocation + cases genInvocationType label <;> simp only [reduceCtorEq, ↓reduceIte] + case irqSetIRQHandler => + cases caps with + | nil => rfl + | cons p xs => + obtain ⟨cap, slot⟩ := p + rw [if_pos (show (cap, slot) :: xs ≠ [] from List.cons_ne_nil _ _)] + simp only [List.head?_cons, Sched.the_someA] + change (if isNtfnCap cap = true ∧ AllowSend ∈ capRights cap + then (Fl4Spec.NonDetE.returnOk (.setIRQHandler irq cap slot) : SEMonad Unit IrqHandlerInvocation) + else Fl4Spec.NonDetE.throwError (.invalidCapability 0)).run σ = _ + rw [irqDecode_run_iteA] + split_ifs <;> rfl + all_goals rfl + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/IrqInvokeFinalA.lean b/proofs/Fl4ProofsA64/P3/IrqInvokeFinalA.lean new file mode 100644 index 000000000..76041834b --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/IrqInvokeFinalA.lean @@ -0,0 +1,265 @@ +/- +P3 (AArch64): final IRQ-node Notification selection. The existing MDB +range and kind invariants bring another cap to an adjacent slot. +-/ +import Fl4ProofsA64.P3.IrqInvokeSlotsA +import Fl4ProofsA64.P3.LookupA +import Fl4ProofsA64.P3.EpBufA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST +open Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +private theorem ntfnRangeKeyA (p b : U64) (send recv : Bool) : + RngKeyA (.Notification p b send recv) := + ⟨(okinj (physA_eq (.Notification p b send recv))).symm, rfl, rfl⟩ + +private theorem ntfnPtrA (p b : U64) (send recv : Bool) : + ptrA (.Notification p b send recv) = p := + (okinj (ptrA_eq (.Notification p b send recv))).symm + +private theorem ntfnClassA (p b : U64) (send recv : Bool) (b' : U64) (send' recv' : Bool) : + clsA (.Notification p b send recv) = clsA (.Notification p b' send' recv') := by + unfold clsA + rw [ntfnPtrA, ntfnPtrA] + congr 1 + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {OS : Set U64} {G : List (List U64)} + +private theorem ntfnKindA {s : S} (H : KindA st OS s) {x b p badge : U64} {send recv : Bool} + (hx : x ∈ OS) (hb : b ∈ OS) (hc : capA st s x = .Notification p badge send recv) + (hk : RngKeyA (capA st s b)) (hp : ptrA (capA st s b) = p) : + ∃ badge' send' recv', capA st s b = .Notification p badge' send' recv' := by + have he := H.kind x hx b hb (hc ▸ ntfnRangeKeyA p badge send recv) hk (by rw [hc, ntfnPtrA, hp]) + rw [hc] at he + generalize capA st s b = cap at hp he ⊢ + cases cap <;> simp only [kindA] at he <;> try omega + rename_i q badge' send' recv' + rw [ntfnPtrA] at hp + exact ⟨badge', send', recv', by rw [hp]⟩ + +/-- A distinct Notification alias puts a cap for the same object adjacent to +an occupied IRQ node slot. This is the notification case of the range proof in +`MdbLk.pt_nb`. -/ +theorem ntfnNeighbourA {s : S} (hM : MdbLk st OS G s) (hRO : RngOneA64 st G s) (hK : KindA st OS s) + {x y p : U64} {m m' : U64} {a v a' v' : Bool} (hx : x ∈ OS) (hy : y ∈ OS) (hne : y ≠ x) + (hcx : capA st s x = .Notification p m a v) (hcy : capA st s y = .Notification p m' a' v') : + ∃ b ∈ OS, (∃ m a v, capA st s b = .Notification p m a v) ∧ + (NxA st s x = b ∨ L.mem s (wa x 16#u64) = b) := by + have kx : RngKeyA (capA st s x) := by rw [hcx]; exact ntfnRangeKeyA p m a v + have ky : RngKeyA (capA st s y) := by rw [hcy]; exact ntfnRangeKeyA p m' a' v' + have hxG : x ∈ G.flatten := (hM.members x).2 ⟨hx, by rw [hcx]; simp⟩ + have hyG : y ∈ G.flatten := (hM.members y).2 ⟨hy, by rw [hcy]; simp⟩ + have inOS : ∀ ch ∈ G, ∀ (n : Nat) (w : U64), ch[n]? = some w → w ∈ OS := fun ch hch n w e => + ((hM.members w).1 (List.mem_flatten.2 ⟨ch, hch, List.mem_of_getElem? e⟩)).1 + obtain ⟨ch, hch, hxc, hyc⟩ := hRO.one x hxG y hyG kx ky (by rw [hcx, hcy]; exact ntfnClassA _ _ _ _ _ _ _) + obtain ⟨i, hi, hxi⟩ := List.getElem_of_mem hxc + obtain ⟨k, hk, hyk⟩ := List.getElem_of_mem hyc + have ei : ch[i]? = some x := by rw [getElem?_of_lt hi, hxi] + have ek : ch[k]? = some y := by rw [getElem?_of_lt hk, hyk] + have hik : i ≠ k := fun e => hne (by subst e; exact hyk.symm.trans hxi) + have hpx : ptrA (capA st s x) = p := by rw [hcx, ntfnPtrA] + rcases Nat.lt_or_gt_of_ne hik with hlt | hlt + · have h1 : i + 1 < ch.length := by omega + have e1 := getElem?_of_lt h1 + have hb := inOS ch hch _ _ e1 + have hcl : RngKeyA (capA st s ch[i + 1]) ∧ ptrA (capA st s ch[i + 1]) = p := by + by_cases e : i + 1 = k + · have : ch[i + 1] = y := by rw [← hyk]; congr + rw [this, hcy, ntfnPtrA]; exact ⟨ntfnRangeKeyA p m' a' v', rfl⟩ + · obtain ⟨kb, cb⟩ := hRO.chunk ch hch i (i + 1) k x _ y ei e1 ek (by omega) (by omega) kx ky + (by rw [hcx, hcy]; exact ntfnClassA _ _ _ _ _ _ _) + exact ⟨kb, (congrArg Prod.fst cb).trans hpx⟩ + exact ⟨_, hb, ntfnKindA hK hx hb hcx hcl.1 hcl.2, Or.inl (hM.chain ch hch i x _ ei e1).1⟩ + · obtain ⟨j, rfl⟩ : ∃ j, i = j + 1 := ⟨i - 1, by omega⟩ + have h1 : j < ch.length := by omega + have e1 := getElem?_of_lt h1 + have hb := inOS ch hch _ _ e1 + have hcl : RngKeyA (capA st s ch[j]) ∧ ptrA (capA st s ch[j]) = p := by + by_cases e : j = k + · have : ch[j] = y := by rw [← hyk]; congr + rw [this, hcy, ntfnPtrA]; exact ⟨ntfnRangeKeyA p m' a' v', rfl⟩ + · obtain ⟨kb, cb⟩ := hRO.chunk ch hch k j (j + 1) y _ x ek e1 ei (by omega) (by omega) ky kx + (by rw [hcx, hcy]; exact ntfnClassA _ _ _ _ _ _ _) + exact ⟨kb, (congrArg Prod.fst cb).trans (by rw [hcy]; exact ntfnPtrA _ _ _ _)⟩ + exact ⟨_, hb, ntfnKindA hK hx hb hcx hcl.1 hcl.2, Or.inr (hM.chain ch hch j _ x e1 ei).2⟩ + + +private theorem ntfnSameObjectRightA (p badge : U64) (send recv : Bool) (c : capability.Cap) + (h : ∀ b s r, c ≠ .Notification p b s r) : + objects.same_object_as (.Notification p badge send recv) c = ok false := by + cases c <;> try rfl + rename_i q b s r + have hne : q ≠ p := fun e => h b s r (by rw [e]) + simp only [objects.same_object_as, objects.same_region_as, if_neg hne] + +private theorem ntfnSameObjectLeftA (p badge : U64) (send recv : Bool) (c : capability.Cap) + (h : ∀ b s r, c ≠ .Notification p b s r) : + objects.same_object_as c (.Notification p badge send recv) = ok false := by + cases c <;> try rfl + case Notification q b s r => + have hne : p ≠ q := fun e => h b s r (by rw [e]) + simp only [objects.same_object_as, objects.same_region_as, if_neg hne] + case IoPortControl => + simp only [objects.same_object_as, objects.same_region_as, bind_ok, Bool.false_eq_true, ↓reduceIte] + +open Classical in +/-- Test finality of an occupied Notification slot using the current MDB. +The whole alias class determines the result of the adjacent-slot reads. -/ +theorem ntfnConcreteFinalA {s : S} (hM : MdbLk st OS G s) (hRO : RngOneA64 st G s) + (hK : KindA st OS s) (h0 : ∀ w ∈ OS, w ≠ 0#u64) + {x p badge : U64} {send recv : Bool} (hx : x ∈ OS) + (hcx : capA st s x = .Notification p badge send recv) : + cdt.is_final_capability_with_cap st s x (.Notification p badge send recv) = + ok (decide (∀ w ∈ OS, w ≠ x → ∀ b u v, capA st s w ≠ .Notification p b u v)) := by + by_cases hu : ∀ w ∈ OS, w ≠ x → ∀ b u v, capA st s w ≠ .Notification p b u v + · rw [decide_eq_true hu] + let prev := L.mem s (wa x 16#u64) + let next := NxA st s x + have hxm : x ∈ G.flatten := (hM.members x).2 ⟨hx, by rw [hcx]; simp⟩ + have hL := hM.lkF h0 + have hprev : prev ≠ 0#u64 → + objects.same_object_as (capA st s prev) (.Notification p badge send recv) = ok false := + fun hn => ntfnSameObjectLeftA _ _ _ _ _ (hu prev ((hM.members prev).1 (hL.pvm x hxm hn)).1 (hL.pvne x hxm)) + have hnext : next ≠ 0#u64 → + objects.same_object_as (.Notification p badge send recv) (capA st s next) = ok false := + fun hn => ntfnSameObjectRightA _ _ _ _ _ (hu next ((hM.members next).1 (hL.nxm x hxm hn)).1 (hL.nxne x hxm)) + have hread : (do + let b ← obj.Slot.is_null prev + if b then ok false else + let c ← obj.Slot.cap st prev s + objects.same_object_as c (.Notification p badge send recv)) = ok false := by + by_cases hp : prev = 0#u64 + · simp [obj.Slot.is_null, hp] + · simp [obj.Slot.is_null, hp, capA_eq, hprev hp] + unfold cdt.is_final_capability_with_cap + rw [slot_prev_eq, bind_ok, ← Std.bind_assoc (obj.Slot.is_null prev), hread, bind_ok] + simp only [Bool.false_eq_true, ↓reduceIte, next_eq, bind_ok] + change (do + let b ← obj.Slot.is_null next + if b then ok true else + let c ← obj.Slot.cap st next s + let q ← objects.same_object_as (.Notification p badge send recv) c + ok (!q)) = _ + by_cases hn : next = 0#u64 + · simp [obj.Slot.is_null, hn] + · simp [obj.Slot.is_null, hn, capA_eq, hnext hn] + · rw [decide_eq_false hu] + push_neg at hu + obtain ⟨y, hy, hne, b, u, v, hcy⟩ := hu + obtain ⟨z, hz, ⟨b', u', v', hcz⟩, hn | hp⟩ := ntfnNeighbourA hM hRO hK hx hy hne hcx hcy + · have h := is_final.false_next hcx hn (h0 z hz) hcz (by + simp only [objects.same_object_as, objects.same_region_as, if_true]) + unfold cdt.is_final_capability at h + simpa only [capA_eq, hcx, bind_ok] using h + · have h := is_final.false_prev hcx hp (h0 z hz) hcz (by + simp only [objects.same_object_as, objects.same_region_as, if_true]) + unfold cdt.is_final_capability at h + simpa only [capA_eq, hcx, bind_ok] using h + +private theorem ntfnSlotAbsA {th : TypedHeapA} {kb y : U64} + (hp : PlaceA th kb) (hy : IsSlotA th y) : ∃ sl, AbsSlotA th sl ∧ cslotAddrA sl = y := by + obtain ⟨p, ⟨r, hr, i, hi, rfl⟩ | ⟨ht, i, hi, rfl⟩⟩ := hy + · refine ⟨(specPtr p, bitsOfA i r), ⟨p, rfl, Or.inl (by simpa using hr)⟩, ?_⟩ + change slotAddrA (specPtr p) (ofBl (bitsOfA i r)) = _ + rw [ofBl_bitsOfA i r (by have := cnode_r hp hr; omega), Nat.mod_eq_of_lt hi] + · refine ⟨(specPtr p, tcbCnodeIndex i), ⟨p, rfl, Or.inr ⟨ht, i, hi, rfl⟩⟩, ?_⟩ + change slotAddrA (specPtr p) (ofBl (tcbCnodeIndex i)) = _ + rw [ofBl_tcbIdx i (by unfold tcbSlotsA at hi; omega)] + +private theorem ntfnFinalSlotGetA {th : TypedHeapA} {mem : U64 → U64} {σ : State Unit} + (H : StateRelA th mem σ) {sl : CslotPtr} (hs : AbsSlotA th sl) : + ∃ c, (getCap (Z := Unit) sl σ).1 = {(c, σ)} ∧ CapRelA (capAtA mem (cslotAddrA sl)) c := by + obtain ⟨o, idx⟩ := sl + obtain ⟨p, e1, hc | ⟨ht, i, hi, e2⟩⟩ := hs + · simp only at e1 hc + subst e1 + obtain ⟨cs, hk, hwf, hr⟩ := H.cnode p _ hc + obtain ⟨c, ha, hrel⟩ := hr idx rfl + exact ⟨c, congrArg Prod.fst (getCap_cnodeA hk hwf ha), hrel⟩ + · simp only at e1 e2 + subst e1 e2 + obtain ⟨x, hk, hr⟩ := H.tcb p ht + obtain ⟨c, ha, hrel⟩ := hr i hi + refine ⟨c, congrArg Prod.fst (Endp.getCap_tcb_eqA hk ha), ?_⟩ + unfold cslotAddrA + rw [ofBl_tcbIdx i (by unfold tcbSlotsA at hi; omega)] + exact hrel + +private theorem ntfnGenRefsA (p b : U64) (u v : Bool) : + genObjRefs (absCapA (.Notification p b u v)) = {GenObjRef.objRef (specPtr p)} := by + simp [absCapA, genObjRefs, objRefs, capIrqs, capIrqOpt, archCapSetMap] + +open Classical in +/-- Match the adjacent-slot test against the abstract last-cap test. +[J] Every abstract slot naming the notification is represented by a +typed concrete Notification slot, using the frozen heap correspondence. -/ +theorem ntfnFinality_refinesA {s : S} {σ : State Unit} {th : TypedHeapA} + (H : StateRelA th (L.mem s) σ) (hp : PlaceA th (L.ksb s)) + (hM : MdbLk st OS G s) (hRO : RngOneA64 st G s) (hK : KindA st OS s) + (h0 : ∀ w ∈ OS, w ≠ 0#u64) + (hsub : ∀ w, IsSlotA th w → w ∈ OS) + (hslot : ∀ w ∈ OS, ¬ IsSlotA th w → capA st s w = .Null) + {sl : CslotPtr} (hsl : AbsSlotA th sl) {p b : U64} {u v : Bool} + (hc : capAtA (L.mem s) (cslotAddrA sl) = .Notification p b u v) + (cover : ∀ q c, (getCap (Z := Unit) q σ).1 = {(c, σ)} → + GenObjRef.objRef (specPtr p) ∈ genObjRefs c → + AbsSlotA th q ∧ ∃ b' u' v', capA st s (cslotAddrA q) = .Notification p b' u' v') : + cdt.is_final_capability_with_cap st s (cslotAddrA sl) (.Notification p b u v) = + ok (decide (isFinalCap' (absCapA (.Notification p b u v)) σ)) := by + have hx := hsub _ (hsl.isSlot hp) + have hc' : capA st s (cslotAddrA sl) = .Notification p b u v := (capAtA_eq (st := st) s _).symm.trans hc + rw [ntfnConcreteFinalA hM hRO hK h0 hx hc'] + congr 1 + apply Bool.eq_iff_iff.2 + simp only [decide_eq_true_eq] + let cap := absCapA (.Notification p b u v) + let overlaps : Set CslotPtr := {q | ∃ c, (getCap (Z := Unit) q σ).1 = {(c, σ)} ∧ + genObjRefs cap ∩ genObjRefs c ≠ ∅} + have hself : sl ∈ overlaps := by + obtain ⟨c, hget, hrel⟩ := ntfnFinalSlotGetA H hsl + rw [hc] at hrel + have he : cap = c := hrel.2 + refine ⟨cap, hget.trans (by rw [he]), ?_⟩ + simp [cap, ntfnGenRefsA] + constructor + · intro alone + refine ⟨sl, Set.ext fun q => ⟨?_, fun eq => (Set.mem_singleton_iff.1 eq).symm ▸ hself⟩⟩ + rintro ⟨c, hget, hov⟩ + have hmem : GenObjRef.objRef (specPtr p) ∈ genObjRefs c := by + by_contra hn + apply hov + rw [ntfnGenRefsA] + exact Set.eq_empty_iff_forall_notMem.2 (fun y hy => hn ((Set.mem_singleton_iff.1 hy.1) ▸ hy.2)) + obtain ⟨hq, b', u', v', h'⟩ := cover q c hget hmem + by_cases he : cslotAddrA q = cslotAddrA sl + · exact Set.mem_singleton_iff.2 (cslotAddrA_inj hp hq hsl he) + · exact (alone _ (hsub _ (hq.isSlot hp)) he b' u' v' h').elim + · rintro ⟨q0, hq0⟩ w hw hne b' u' v' hwc + change overlaps = {q0} at hq0 + have hwslot : IsSlotA th w := by + by_contra h + rw [hslot w hw h] at hwc + cases hwc + obtain ⟨q, hq, ha⟩ := ntfnSlotAbsA hp hwslot + have hqmem : q ∈ overlaps := by + obtain ⟨c, hg, hrel⟩ := ntfnFinalSlotGetA H hq + rw [ha, capAtA_eq, hwc] at hrel + refine ⟨c, hg, ?_⟩ + rw [← hrel.2] + simp [cap, ntfnGenRefsA] + rw [hq0] at hqmem hself + have eq : q = sl := (Set.mem_singleton_iff.1 hqmem).trans (Set.mem_singleton_iff.1 hself).symm + exact hne (ha.symm.trans (congrArg cslotAddrA eq)) + + +end +end +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/IrqInvokeGhostA.lean b/proofs/Fl4ProofsA64/P3/IrqInvokeGhostA.lean new file mode 100644 index 000000000..a22b3d5b9 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/IrqInvokeGhostA.lean @@ -0,0 +1,107 @@ +/- +P3 (AArch64): linked notification objects and CDT transitions for IRQHandler. +-/ +import Fl4ProofsA64.P3.IrqInvokeSlotsA +import Fl4ProofsA64.P3.SlotJA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST +open Fl4Spec Fl4SpecA64 Sched + +noncomputable section +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +open Classical in +/-- Copy the decoded Notification from its source into the now-empty node +slot, carrying the CDT insertion ghost through the joint post. -/ +theorem irqInsertNotification_jA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} + {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} {ia ie : Nat} {s : S} {a : A} {σ : State Unit} {m : McState} + (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + (hT : TreeA st OS Z.G.chains Z.G.par s) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) + {src dest : CslotPtr} (hs : AbsSlotA Z.th src) (hd : AbsSlotA Z.th dest) + {p badge : U64} {recv : Bool} + (hc : capA st s (cslotAddrA src) = .Notification p badge true recv) + (he : capAtA (L.mem s) (cslotAddrA dest) = .Null) + (hcanon : obj.sext48 (cslotAddrA dest) = ok (cslotAddrA dest)) : + let c := capability.Cap.Notification p badge true recv + let ac := absCapA c + let G := insGA Z.G (cslotAddrA src) (cslotAddrA dest) (insBA σ src ac ac) + cdt.insert st s c (cslotAddrA src) (cslotAddrA dest) ⦃ s' => + ∃ σ', ((), σ') ∈ (capInsert (Z := Unit) ac src dest σ).1 ∧ + JAtZAX (L := L) AR { Z with G := G } s' a σ' m ∧ TreeA st OS G.chains G.par s' ∧ + ∀ n, SInvXA σ m n → SInvXA σ' m n ⦄ := by + dsimp only + let c := capability.Cap.Notification p badge true recv + have hs0 : capAtA (L.mem s) (cslotAddrA src) ≠ .Null := by + rw [capAtA_eq, hc] + simp + have hnode : IrqCapA (absCapA c) := Or.inr ⟨_, _, _, rfl⟩ + have hbadge : ∀ r b rs, (absCapA (capA st s (cslotAddrA src)) = .endpointCap r b rs ∨ + absCapA (capA st s (cslotAddrA src)) = .notificationCap r b rs) → b ≠ 0 → + capEpBadge (absCapA c) = b := by + intro r b rs hb _ + rw [hc] at hb + rcases hb with hb | hb + · cases hb + · exact (Cap.notificationCap.inj hb).2.1 + have hwf : CapRt.EncWf c := by + change CapRt.EncWf (.Notification p badge true recv) + rw [← hc] + exact CapRt.decodeF_encWf _ _ + have hcall := cap_insert_dA E hT hsub hnul hs hd hs0 he hcanon + (nc := c) (anc := absCapA c) trivial (CapEqA.of_abs rfl) hwf (show c ≠ .Null by simp [c]) + (by rw [hc]; exact UA.refl c) (by rw [hc]; trivial) + (by rw [hc]; simp [isUtA]) (by rw [hc]) + (by rw [hc]; simp) hbadge (Or.inr hnode) (by rw [hc]; exact id) + rw [hc] at hcall + exact hcall + +set_option maxHeartbeats 700000 in +open Classical in +/-- Removing a live node notification uses the deletion ghost. The finaliser +prepares the entry facts for the empty-slot step on its own successor. -/ +theorem irqDeleteNotification_jA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} + {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} {ia ie : Nat} {s : S} {a : A} + {σ : State Unit} {m : McState} {sl : CslotPtr} {p badge : U64} {send recv : Bool} + (hs : StateRelA Z.th (L.mem s) σ) (ht : AbsSlotA Z.th sl) + (hc : capAtA (L.mem s) (cslotAddrA sl) = .Notification p badge send recv) + (hfin : cdt.is_final_capability_with_cap st s (cslotAddrA sl) (.Notification p badge send recv) = + ok (decide (isFinalCap' (absCapA (.Notification p badge send recv)) σ))) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (finish : objects.finalise_cap_object st ar s a (.Notification p badge send recv) + (decide (isFinalCap' (absCapA (.Notification p badge send recv)) σ)) ⦃ r => + ∃ σ1, ((), σ1) ∈ (fastFinalise (Z := Unit) (absCapA (.Notification p badge send recv)) + (decide (isFinalCap' (absCapA (.Notification p badge send recv)) σ)) σ).1 ∧ + JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie r.2.1 r.2.2 σ1 m ∧ + TreeA st OS Z.G.chains Z.G.par r.2.1 ∧ RngOneA64 st Z.G.chains r.2.1 ∧ + KindA st OS r.2.1 ∧ (∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st r.2.1 y = .Null) ∧ + capAtA (L.mem r.2.1) (cslotAddrA sl) ≠ .Null ⦄) : + let G := remGA Z.G (cslotAddrA sl) + cdt.delete_one st ar s a (cslotAddrA sl) ⦃ r => + ∃ σ2, ((), σ2) ∈ (capDeleteOne (Z := Unit) sl σ).1 ∧ + JAtZAX (L := L) AR { Z with G := G } r.1 r.2 σ2 m ∧ + TreeA st OS G.chains G.par r.1 ∧ RngOneA64 st G.chains r.1 ∧ KindA st OS r.1 ⦄ := by + let G := remGA Z.G (cslotAddrA sl) + let M : S → A → State Unit → Prop := fun s1 a1 σ1 => + JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s1 a1 σ1 m ∧ + TreeA st OS Z.G.chains Z.G.par s1 ∧ RngOneA64 st Z.G.chains s1 ∧ + KindA st OS s1 ∧ (∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s1 y = .Null) ∧ + capAtA (L.mem s1) (cslotAddrA sl) ≠ .Null + let R : S → A → State Unit → Prop := fun s2 a2 σ2 => + JAtZAX (L := L) AR { Z with G := G } s2 a2 σ2 m ∧ + TreeA st OS G.chains G.par s2 ∧ RngOneA64 st G.chains s2 ∧ KindA st OS s2 + refine irqDeleteNotificationA hs ht hc hfin (M := M) (R := R) finish ?_ + rintro s1 a1 σ1 ⟨E, hT, hRO, hK, hnul, hcn⟩ + refine WP.spec_mono (empty_slot_jA E hT hRO hK hsub hnul ht hcn) ?_ + rintro r ⟨har, σ', he, HJ, HT, HR, HK⟩ + exact ⟨σ', he, har.symm ▸ HJ, HT, HR, HK⟩ + + +end +end +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/IrqInvokePerformA.lean b/proofs/Fl4ProofsA64/P3/IrqInvokePerformA.lean new file mode 100644 index 000000000..f6ad1a3b9 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/IrqInvokePerformA.lean @@ -0,0 +1,257 @@ +/- +P3 (AArch64): the machine operations used by IRQ invocation. +[P] IrqHwA matches configure and mask at the Arch boundary on the actual +successor machine state. IrqRelA remains a software relation. +-/ +import Fl4ProofsA64.P3.IrqInvokeDispatchA +import Fl4ProofsA64.P3.IpcInvokeDispatchA +import Fl4ProofsA64.P3.FrontEndExtraA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4SpecA64 Sched +open Fl4Spec + +noncomputable section + +/-- The related abstract index of a bounded FL4 IRQ. -/ +private abbrev irqIndexA (irq : U64) : Irq := BitVec.ofNat 13 irq.val + +/-- [P] The IRQ machine operations match at the platform boundary. The relation +can observe routing as well as masks and is independent of the kernel store. -/ +structure IrqHwA {A : Type} (ar : machine.Arch A) (MR : A → MachineState → Prop) : Prop where + configure : ∀ a ms irq trigger cpu, MR a ms → irq.val < 5376 → + ar.configure_irq a irq trigger cpu ⦃ a' => + ∃ ms', ((), ms') ∈ (setIRQTriggerCore (irqIndexA irq) trigger cpu.bv ms).1 ∧ MR a' ms' ⦄ + mask : ∀ a ms irq masked, MR a ms → irq.val < 5376 → + ar.mask_irq a irq masked ⦃ a' => + ∃ ms', ((), ms') ∈ (maskInterrupt masked (irqIndexA irq) ms).1 ∧ MR a' ms' ⦄ + +/-- Both configured spec branches implement Ack by unmasking the interrupt. -/ +theorem archInvokeIrqHandler_ackA (irq : Irq) : + archInvokeIrqHandler (Z := Unit) (.aCKIrq irq) = doMachineOp (maskInterrupt false irq) := by + change doMachineOp (Z := Unit) + (if config_ARM_GIC_V3 = true then deactivateInterrupt irq else maskInterrupt false irq) = _ + by_cases h : config_ARM_GIC_V3 = true + · rw [if_pos h] + congr 1 + funext ms + unfold deactivateInterrupt + exact nd_bind_oneA (assert_trueA h ms) _ + · rw [if_neg h] + +/-- A machine outcome embeds into the kernel state with the software fields preserved. -/ +theorem irqMachine_memA (op : MachineMonad Unit) (σ : State Unit) {ms' : MachineState} + (h : ((), ms') ∈ (op σ.machineState).1) : + ((), {σ with machineState := ms'}) ∈ (doMachineOp op σ).1 := by + unfold doMachineOp + rw [nd_gets_bindA] + refine mem_bind_ndD (x := (((), ms'), σ)) ?_ ?_ + · exact ⟨h, rfl⟩ + · change ((), {σ with machineState := ms'}) ∈ + ((NonDet.modify (fun state : State Unit => {state with machineState := ms'}) >>= fun _ => + NonDet.pure ()) σ).1 + rw [nd_modify_bindA] + rfl + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {MR : A → MachineState → Prop} (hw : IrqHwA ar MR) +include hw + +/-- Ack's operation body is a machine-only step. -/ +theorem invokeIrqAck_refinesA (s : S) (a : A) (σ : State Unit) (irq : U64) + (hi : irq.val < 5376) (hm : MR a σ.machineState) + (h : IrqRelA (L.mem s) (L.ksb s) σ) : + ar.mask_irq a irq false ⦃ a' => + ∃ ms', ((), {σ with machineState := ms'}) ∈ (invokeIrqHandler (.aCKIrq (irqIndexA irq)) σ).1 ∧ + MR a' ms' ∧ IrqRelA (L.mem s) (L.ksb s) {σ with machineState := ms'} ⦄ := by + refine WP.spec_mono (hw.mask a σ.machineState irq false hm hi) ?_ + rintro a' ⟨ms', hms, hr⟩ + refine ⟨ms', ?_, hr, ⟨h.st, h.node, h.nodeHi⟩⟩ + change ((), {σ with machineState := ms'}) ∈ (archInvokeIrqHandler (.aCKIrq (irqIndexA irq)) σ).1 + rw [archInvokeIrqHandler_ackA] + exact irqMachine_memA _ _ hms + +/-- The software state update followed by the spec's unmask step. -/ +def irqSignalStateA (σ : State Unit) (irq : U64) : State Unit := + { σ with interruptStates := Function.update σ.interruptStates (irqIndexA irq) .irqSignal } + +/-- The concrete state write and the shared hardware unmask for an issue. -/ +theorem setIrqSignal_refinesA (s : S) (a : A) (σ : State Unit) (irq : U64) + (hi : irq.val < 5376) (hm : MR a σ.machineState) + (h : IrqRelA (L.mem s) (L.ksb s) σ) (hf : (L.ksb s).val + ksSizeA ≤ 2 ^ 64) + (hal : ∀ i : U64, i.val < 5376 → Aligned (irqWordA (L.ksb s) i)) : + interrupt.set_irq_state st ar s a interrupt.IRQ_SIGNAL irq ⦃ r => + ∃ ms', ((), {irqSignalStateA σ irq with machineState := ms'}) ∈ + (setIrqState .irqSignal (irqIndexA irq) σ).1 ∧ + MR r.2 ms' ∧ IrqRelA (L.mem r.1) (L.ksb r.1) {irqSignalStateA σ irq with machineState := ms'} ∧ + WritesIn st s r.1 (· = irqWordA (L.ksb s) irq) ⦄ := by + have hs : interrupt.state_off irq = ok (irqOffA irq) := by + unfold interrupt.state_off + obtain ⟨v, hv, e⟩ := spec_existsA P0.Consts.layout.ks.IRQ_STATE.spec + subst v + simp only [lift, bind_ok, hv, irqOffA] + have hn : (interrupt.IRQ_SIGNAL : U64) ≠ interrupt.IRQ_INACTIVE := by + rw [interrupt.IRQ_SIGNAL, interrupt.IRQ_INACTIVE] + decide + unfold interrupt.set_irq_state + simp only [hs, bind_ok, decide_eq_false hn] + refine WP.spec_bind (obj.ks_set.spec s (irqOffA irq) interrupt.IRQ_SIGNAL) ?_ + rintro s' rfl + refine WP.spec_bind (hw.mask a σ.machineState irq false hm hi) ?_ + rintro a' ⟨ms', hm', ha'⟩ + refine (WP.spec_ok _).2 ⟨ms', ?_, ha', ?_, writesIn_wr (hal irq hi)⟩ + · unfold setIrqState + rw [nd_modify_bindA] + have hm'': (.irqSignal : IrqState) ≠ .irqInactive := by decide + simp only [decide_eq_false hm''] + exact irqMachine_memA _ _ hm' + · have hk : L.ksb (L.wr s (irqWordA (L.ksb s) irq) interrupt.IRQ_SIGNAL) = L.ksb s := + (writesIn_wr (st := st) (v := interrupt.IRQ_SIGNAL) (s := s) (hal irq hi)).1 + change IrqRelA (L.mem (L.wr s (irqWordA (L.ksb s) irq) interrupt.IRQ_SIGNAL)) + (L.ksb (L.wr s (irqWordA (L.ksb s) irq) interrupt.IRQ_SIGNAL)) _ + rw [hk] + constructor + · intro i hib + rw [L.mem_wr _ _ _ _ (hal irq hi) (hal i hib)] + change Function.update σ.interruptStates (irqIndexA irq) .irqSignal (irqIndexA i) = _ + by_cases e : i = irq + · subst i + simp [irqStOfA, interrupt.IRQ_SIGNAL] + · have en : irqWordA (L.ksb s) i ≠ irqWordA (L.ksb s) irq := by + intro eq + apply e + apply UScalar.eq_of_val_eq + have q := congrArg (fun u : U64 => u.val) eq + rw [irqWordA_val hf hib, irqWordA_val hf hi] at q + omega + have ei : irqIndexA i ≠ irqIndexA irq := by + intro eq + apply e + apply UScalar.eq_of_val_eq + have q := congrArg BitVec.toNat eq + have h1 : i.val < 2 ^ 13 := by omega + have h2 : irq.val < 2 ^ 13 := by omega + change i.val % 2 ^ 13 = irq.val % 2 ^ 13 at q + rwa [Nat.mod_eq_of_lt h1, Nat.mod_eq_of_lt h2] at q + simp only [Function.update_of_ne ei, if_neg (Ne.symm en), h.st i hib] + · exact h.node + · exact h.nodeHi + +/-- A direct Ack decoder proof. [J] The scheduler Restart continuation supplies +the machine and IRQ views at its actual successor. -/ +theorem irqAck_refinesA {s : S} {a : A} {σ : State Unit} {irq : U64} {Z : XSetsA} + {m : McState} {κ : U64 → U64} {extra : Std.Array obj.Slot 3#usize} + (H : Sched.SchedSRA st Z m κ s σ) (hst : StEqA Z.T (L.mem s) σ) + (ht : L.curW s ∈ Z.T) (hc : σ.curThread = specPtr (L.curW s)) + {tcb : Tcb} (hk : σ.kheap σ.curThread = some (.tcb tcb)) (hr : runnable tcb.tcbState = true) + (hi : irq.val < 5376) + (frame : ∀ s1 σ1, sched.set_thread_state st s (L.curW s) sched.TS_RESTART = ok s1 → + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) → + MR a σ1.machineState ∧ IrqRelA (L.mem s1) (L.ksb s1) σ1) : + interrupt.decode_irq_handler st ar s a fl4_abi.label.IRQAckIRQ irq extra ⦃ r => + r.1 = .Ok () ∧ ∃ σ1 σ2, + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ((), σ2) ∈ (invokeIrqHandler (.aCKIrq (irqIndexA irq)) σ1).1 ∧ + MR r.2.2 σ2.machineState ∧ IrqRelA (L.mem r.2.1) (L.ksb r.2.1) σ2 ∧ + Sched.SchedSRA st Z m κ r.2.1 σ2 ∧ StEqA Z.T (L.mem r.2.1) σ2 ⦄ := by + have hv : sched.TS_RESTART.val = tsCodeA .restart := by + rw [ts_vals.2.1] + rfl + rw [hc] at hk + obtain ⟨s1, hR, σ1, hs, HS, HE, -, -⟩ := + spec_existsA (Sched.sc_set_runnable_stateA H hst ht hk hr (ts := .restart) rfl hv) + have hs' : setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) := by + simpa only [hc] using hs + obtain ⟨hm, hirq⟩ := frame s1 σ1 hR hs' + rw [irqAck_programA, hR] + simp only [bind_ok] + refine WP.spec_bind (invokeIrqAck_refinesA hw s1 a σ1 irq hi hm hirq) ?_ + rintro a' ⟨ms', hstep, hMR, hIrq⟩ + refine (WP.spec_ok _).2 ⟨rfl, σ1, {σ1 with machineState := ms'}, hs', hstep, hMR, hIrq, ?_, HE⟩ + refine ⟨⟨⟨HS.sch.core, HS.sch.cpuCs, HS.sch.curOfW, HS.sch.curScOfW, HS.sch.cur, HS.sch.csc, + HS.sch.idle, HS.sch.idleOf, HS.sch.idleScOf, HS.sch.act, HS.sch.time, HS.sch.aff, HS.sch.affT, + HS.sch.tcb, HS.sch.sc, HS.sch.scOff⟩, + ⟨HS.rest.rq, HS.rest.rqDom, HS.rest.rel, HS.rest.consumed, HS.rest.repr, HS.rest.curDom, + HS.rest.domTime, HS.rest.prio, HS.rest.scMem, HS.rest.refillMax⟩, + ⟨HS.scr.sc⟩, ⟨HS.scp.inj, HS.scp.disj, HS.scp.out⟩, HS.thT, HS.place, HS.csN, HS.ksFit, + HS.curT, HS.core, HS.curOn, HS.run, HS.actOn, HS.cscC, ?_, HS.scCore, HS.cscAll⟩, HS.rd.of_eq rfl rfl rfl⟩ + exact HS.inv.of_kheap rfl rfl + +/-- The issue tail shared by the plain and routed invocations. [J] The typed +CSpace frame runs the insertion on the exact state after the IRQ-state write. -/ +theorem invokeIrqControlTail_refinesA (s : S) (a : A) (σ : State Unit) (irq : U64) + (src dest : CslotPtr) (hi : irq.val < 5376) (hm : MR a σ.machineState) + (hirq : IrqRelA (L.mem s) (L.ksb s) σ) (hf : (L.ksb s).val + ksSizeA ≤ 2 ^ 64) + (hal : ∀ i : U64, i.val < 5376 → Aligned (irqWordA (L.ksb s) i)) + {R : S → A → State Unit → Prop} + (insert : ∀ s1 a1 ms, MR a1 ms → + IrqRelA (L.mem s1) (L.ksb s1) {irqSignalStateA σ irq with machineState := ms} → + WritesIn st s s1 (· = irqWordA (L.ksb s) irq) → + cdt.insert st s1 (.IrqHandler irq) (cslotAddrA src) (cslotAddrA dest) ⦃ s2 => + ∃ σ2, ((), σ2) ∈ (capInsert (.irqHandlerCap (irqIndexA irq)) src dest + {irqSignalStateA σ irq with machineState := ms}).1 ∧ R s2 a1 σ2 ⦄) : + interrupt.invoke_irq_control st ar s a irq (cslotAddrA dest) (cslotAddrA src) ⦃ r => + ∃ σ', ((), σ') ∈ ((setIrqState (Z := Unit) .irqSignal (irqIndexA irq) >>= fun _ => + capInsert (.irqHandlerCap (irqIndexA irq)) src dest) σ).1 ∧ R r.1 r.2 σ' ⦄ := by + unfold interrupt.invoke_irq_control + refine WP.spec_bind (setIrqSignal_refinesA hw s a σ irq hi hm hirq hf hal) ?_ + rintro ⟨s1, a1⟩ ⟨ms', hms, hMR, hIrq, hW⟩ + refine WP.spec_bind (insert s1 a1 ms' hMR hIrq hW) ?_ + rintro s2 ⟨σ2, h2, hR⟩ + exact (WP.spec_ok _).2 ⟨σ2, mem_bind_ndD hms h2, hR⟩ + +/-- Labels 65 and 66 share the same routed perform path. [J] The +insertion carries the CDT ghost after the actual configure and unmask. -/ +theorem invokeIrqTriggerCore_refinesA (s : S) (a : A) (σ : State Unit) (irq cpu : U64) + (trigger : Bool) (src dest : CslotPtr) (hi : irq.val < 5376) (hm : MR a σ.machineState) + (hirq : IrqRelA (L.mem s) (L.ksb s) σ) (hf : (L.ksb s).val + ksSizeA ≤ 2 ^ 64) + (hal : ∀ i : U64, i.val < 5376 → Aligned (irqWordA (L.ksb s) i)) + {R : S → A → State Unit → Prop} + (insert : ∀ s1 a1 ms, MR a1 ms → + IrqRelA (L.mem s1) (L.ksb s1) {irqSignalStateA σ irq with machineState := ms} → + WritesIn st s s1 (· = irqWordA (L.ksb s) irq) → + cdt.insert st s1 (.IrqHandler irq) (cslotAddrA src) (cslotAddrA dest) ⦃ s2 => + ∃ σ2, ((), σ2) ∈ (capInsert (.irqHandlerCap (irqIndexA irq)) src dest + {irqSignalStateA σ irq with machineState := ms}).1 ∧ R s2 a1 σ2 ⦄) : + (do + let a1 ← ar.configure_irq a irq trigger cpu + interrupt.invoke_irq_control st ar s a1 irq (cslotAddrA dest) (cslotAddrA src)) ⦃ r => + ∃ σ', (.ok (), σ') ∈ ((invokeFl4IrqControl (Z := Unit) + (.triggerCore (irqIndexA irq) dest src trigger cpu.bv)).run σ).1 ∧ R r.1 r.2 σ' ⦄ := by + refine WP.spec_bind (hw.configure a σ.machineState irq trigger cpu hm hi) ?_ + rintro a1 ⟨ms, hms, hMR⟩ + have hi' : IrqRelA (L.mem s) (L.ksb s) {σ with machineState := ms} := ⟨hirq.st, hirq.node, hirq.nodeHi⟩ + refine WP.spec_mono (invokeIrqControlTail_refinesA hw s a1 {σ with machineState := ms} + irq src dest hi hMR hi' hf hal insert) ?_ + rintro r ⟨σ', hstep, hR⟩ + refine ⟨σ', ?_, hR⟩ + apply mem_liftED + exact mem_bind_ndD (irqMachine_memA _ _ hms) hstep + +/-- Label 29 uses the same issue tail with no preceding configure step. -/ +theorem invokeIrqPlain_refinesA (s : S) (a : A) (σ : State Unit) (irq : U64) + (src dest : CslotPtr) (hi : irq.val < 5376) (hm : MR a σ.machineState) + (hirq : IrqRelA (L.mem s) (L.ksb s) σ) (hf : (L.ksb s).val + ksSizeA ≤ 2 ^ 64) + (hal : ∀ i : U64, i.val < 5376 → Aligned (irqWordA (L.ksb s) i)) + {R : S → A → State Unit → Prop} + (insert : ∀ s1 a1 ms, MR a1 ms → + IrqRelA (L.mem s1) (L.ksb s1) {irqSignalStateA σ irq with machineState := ms} → + WritesIn st s s1 (· = irqWordA (L.ksb s) irq) → + cdt.insert st s1 (.IrqHandler irq) (cslotAddrA src) (cslotAddrA dest) ⦃ s2 => + ∃ σ2, ((), σ2) ∈ (capInsert (.irqHandlerCap (irqIndexA irq)) src dest + {irqSignalStateA σ irq with machineState := ms}).1 ∧ R s2 a1 σ2 ⦄) : + interrupt.invoke_irq_control st ar s a irq (cslotAddrA dest) (cslotAddrA src) ⦃ r => + ∃ σ', (.ok (), σ') ∈ ((invokeFl4IrqControl (Z := Unit) + (.spec (.irqControl (irqIndexA irq) dest src))).run σ).1 ∧ R r.1 r.2 σ' ⦄ := by + refine WP.spec_mono (invokeIrqControlTail_refinesA hw s a σ irq src dest hi hm hirq hf hal insert) ?_ + rintro r ⟨σ', hstep, hR⟩ + exact ⟨σ', mem_liftED hstep, hR⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/IrqInvokeSlotsA.lean b/proofs/Fl4ProofsA64/P3/IrqInvokeSlotsA.lean new file mode 100644 index 000000000..93f5859b4 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/IrqInvokeSlotsA.lean @@ -0,0 +1,244 @@ +/- +P3 (AArch64): the typed IRQ-node slot and IRQControl's CDT insertion. +The updated ghost is the existing insert ghost at the IRQControl parent. +-/ +import Fl4ProofsA64.P3.IrqInvokePerformA +import Fl4ProofsA64.P3.InsDisA +import Fl4ProofsA64.P3.JBootA +import Fl4ProofsA64.P3.NtfnCancelAllA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST +open Fl4Spec Fl4SpecA64 + +noncomputable section +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +/-- The spec IRQ slot agrees with the concrete one-slot CNode. -/ +theorem irqSlot_relatedA {s : S} {σ : State Unit} {th : TypedHeapA} + (h : IrqRelA (L.mem s) (L.ksb s) σ) (hp : PlaceA th (L.ksb s)) + {irq : U64} (hi : irq.val < 5376) : + let sl : CslotPtr := (σ.interruptIrqNode (BitVec.ofNat 13 irq.val), []) + AbsSlotA th sl ∧ cslotAddrA sl = irqSlotA (L.ksb s) irq ∧ + interrupt.irq_slot st s irq = ok (irqSlotA (L.ksb s) irq) := by + dsimp only + rw [h.node irq hi] + obtain ⟨addr, eq, rfl⟩ := spec_existsA (interrupt.irq_slot.val (st := st) s irq) + exact ⟨⟨_, rfl, Or.inl (hp.irq irq hi)⟩, slotAddrA_nil _, eq⟩ + +/-- Both delete operations are identities on an empty related slot. -/ +theorem irqDeleteNullA {s : S} {a : A} {σ : State Unit} {th : TypedHeapA} {sl : CslotPtr} + (hs : StateRelA th (L.mem s) σ) (ht : AbsSlotA th sl) + (hnull : capAtA (L.mem s) (cslotAddrA sl) = .Null) : + cdt.delete_one st ar s a (cslotAddrA sl) = ok (s, a) ∧ + ((), σ) ∈ (capDeleteOne (Z := Unit) sl σ).1 := by + constructor + · unfold cdt.delete_one + rw [capA_eq, ← capAtA_eq, hnull] + simp only [bind_ok, is_null_t, ↓reduceIte] + · obtain ⟨cap, hcap, hrel⟩ := hs.getAbsA ht + rw [hnull] at hrel + have eq : cap = .nullCap := hrel.2.symm + subst cap + unfold capDeleteOne + refine mem_bind_ndD hcap ?_ + simp [Fl4Spec.NonDet.unless, Fl4Spec.NonDet.pure] + +private theorem handlerFromControlA (irq : U64) : + UA (.IrqHandler irq) .IrqControl := by + refine ⟨?_, ?_, ?_, rfl, ?_⟩ + · apply okinj + exact (physA_eq (.IrqHandler irq)).symm.trans (physA_eq .IrqControl) + · apply okinj + exact (ptrA_eq (.IrqHandler irq)).symm.trans (ptrA_eq .IrqControl) + · apply okinj + exact (sizeA_eq (.IrqHandler irq)).symm.trans (sizeA_eq .IrqControl) + · intro p b i e h + cases h + +open Classical in +/-- The IRQControl insertion preserves the typed heap and CSpace at the +updated ghost. The destination is a regular CSpace slot off the IRQ node. -/ +theorem irqInsertHandler_jA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} + {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} {ia ie : Nat} {s : S} {a : A} {σ : State Unit} {m : McState} + (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + (hT : TreeA st OS Z.G.chains Z.G.par s) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) + {src dest : CslotPtr} (hs : AbsSlotA Z.th src) (hd : AbsSlotA Z.th dest) + (hc : capA st s (cslotAddrA src) = .IrqControl) + (he : capAtA (L.mem s) (cslotAddrA dest) = .Null) + (hcanon : obj.sext48 (cslotAddrA dest) = ok (cslotAddrA dest)) + (hoff : ¬ IrqNodeA (L.ksb s) (fl4Ptr dest.1)) {irq : U64} (hi : irq.val < 5376) : + let parent := insBA σ src (.irqHandlerCap (BitVec.ofNat 13 irq.val)) .irqControlCap + let G := insGA Z.G (cslotAddrA src) (cslotAddrA dest) parent + cdt.insert st s (.IrqHandler irq) (cslotAddrA src) (cslotAddrA dest) ⦃ s' => + ∃ σ', ((), σ') ∈ (capInsert (Z := Unit) (.irqHandlerCap (BitVec.ofNat 13 irq.val)) src dest σ).1 ∧ + JAtZAX (L := L) AR { Z with G := G } s' a σ' m ∧ + TreeA st OS G.chains G.par s' ∧ + ∀ n, SInvXA σ m n → SInvXA σ' m n ⦄ := by + dsimp only + have hs0 : capAtA (L.mem s) (cslotAddrA src) ≠ .Null := by + rw [capAtA_eq, hc] + simp + have hpar : shouldBeParentOf .irqControlCap true (.irqHandlerCap (BitVec.ofNat 13 irq.val)) + (decide (isCapRevocable (.irqHandlerCap (BitVec.ofNat 13 irq.val)) .irqControlCap)) := by + exact ⟨rfl, Or.inr (Or.inl ⟨_, rfl⟩), trivial⟩ + have hn : CapObjOkA Z (.IrqHandler irq) := + ⟨(fun _ h => by cases h), (fun _ _ _ _ _ h => by cases h), (fun _ _ _ h => by cases h), + (fun _ _ _ h => by cases h), trivial⟩ + have hcall := cap_insert_jA E hT hsub hnul hs hd hs0 he hcanon + (show CapWfA (.IrqHandler irq) from hi) + (anc := .irqHandlerCap (BitVec.ofNat 13 irq.val)) (CapEqA.of_abs rfl) (show CapRt.EncWf (.IrqHandler irq) from trivial) + (show capability.Cap.IrqHandler irq ≠ .Null by simp) + (show UA (.IrqHandler irq) (capA st s (cslotAddrA src)) by rw [hc]; exact handlerFromControlA irq) + (by rw [hc]; trivial) (by rw [hc]; simp [isUtA]) (by rw [hc]; simp [isUtA]) + (by rw [hc]; exact hsib_ofA E.j.1.cdt hs hpar) hn + refine WP.spec_mono hcall ?_ + rintro s' ⟨σ', hm, HJ, HT, HI⟩ + rw [hc] at HJ HT + refine ⟨σ', hm, HJ, HT, ?_⟩ + intro n hsInv + exact HI (capOk_insA (nc := .IrqHandler irq) E.j.1.irq (Or.inl hoff) (by intro p r g b e; cases e)) n hsInv + +/-- The actual final-notification finaliser. [J] The two unlink transitions +supply their exact successor relations. [S] Waiters are local at the point +where the finaliser cancels their waits, as in +`possible_switch_to_remote_not_specA` and `cancel_all_signals_rel`. -/ +theorem irqFinaliseNotificationA {s : S} {a : A} {σ : State Unit} + {p badge : U64} {send recv fin : Bool} + {N T : Set U64} {SR : S → State Unit → Prop} {DL : S → U64 → Prop} + (hH : Notif.SchedHypsA st N T SR) (hO : Notif.SchedOpsA st ar N T SR DL) + (hP : Notif.NPlaced T N) (hn : p ∈ N) + (hR : Notif.NtfnStateRel N T (L.mem s) σ) (hSR : SR s σ) + {U : S → State Unit → Prop} + (unlinkSc : fin = true → schedcontext.maybe_unbind_ntfn st s p ⦃ s1 => ∃ σ1, + ((), σ1) ∈ (schedContextMaybeUnbindNtfn (Z := Unit) (specPtr p) σ).1 ∧ U s1 σ1 ⦄) + (unlinkTcb : fin = true → ∀ s1 σ1, U s1 σ1 → + notification.unbind_maybe_notification st s1 p ⦃ s2 => ∃ σ2, + ((), σ2) ∈ (unbindMaybeNotification (Z := Unit) (specPtr p) σ1).1 ∧ + Notif.NtfnStateRel N T (L.mem s2) σ2 ∧ SR s2 σ2 ∧ + (∀ z, σ2.kheap (specPtr p) = some (.notification z) → + ∀ t ∈ T, specPtr t ∈ z.ntfnObj.ntfnQueue → L.mem s2 (affA t) = L.cpu s2) ⦄) : + objects.finalise_cap_object st ar s a (.Notification p badge send recv) fin ⦃ r => + r.1 = (.Null, .Null) ∧ r.2.2 = a ∧ ∃ σ', + ((), σ') ∈ (fastFinalise (Z := Unit) (absCapA (.Notification p badge send recv)) fin σ).1 ∧ + Notif.NtfnStateRel N T (L.mem r.2.1) σ' ∧ SR r.2.1 σ' ⦄ := by + cases fin + · simp only [objects.finalise_cap_object, Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + exact ⟨trivial, trivial, σ, by rfl, hR, hSR⟩ + · simp only [objects.finalise_cap_object, ↓reduceIte] + refine WP.spec_bind (unlinkSc rfl) ?_ + rintro s1 ⟨σ1, h1, H1⟩ + refine WP.spec_bind (unlinkTcb rfl s1 σ1 H1) ?_ + rintro s2 ⟨σ2, h2, H2, hs2, hon⟩ + refine WP.spec_bind (Notif.cancel_all_signals_rel hH hO hP s2 σ2 H2 hs2 hn hon) ?_ + rintro s3 ⟨-, σ3, h3, H3, hs3⟩ + refine (WP.spec_ok _).2 ⟨rfl, rfl, σ3, ?_, H3, hs3⟩ + change ((), σ3) ∈ ((schedContextMaybeUnbindNtfn (Z := Unit) (specPtr p) >>= fun _ => + unbindMaybeNotification (specPtr p) >>= fun _ => cancelAllSignals (specPtr p)) σ).1 + exact mem_bind_ndD h1 (mem_bind_ndD h2 h3) + +open Classical in +/-- Compose finalisation and emptySlot for an IRQ-node notification. +[J] The finality decision comes from the MDB adjacency and spec slot coverage. +[J] The finaliser and slot continuations must provide their actual successors, +including the notification, scheduler and CDT frames. -/ +theorem irqDeleteNotificationA {s : S} {a : A} {σ : State Unit} {th : TypedHeapA} {sl : CslotPtr} + {p badge : U64} {send recv : Bool} + (hs : StateRelA th (L.mem s) σ) (ht : AbsSlotA th sl) + (hc : capAtA (L.mem s) (cslotAddrA sl) = .Notification p badge send recv) + (hfin : cdt.is_final_capability_with_cap st s (cslotAddrA sl) (.Notification p badge send recv) = + ok (decide (isFinalCap' (absCapA (.Notification p badge send recv)) σ))) + {M : S → A → State Unit → Prop} {R : S → A → State Unit → Prop} + (finish : objects.finalise_cap_object st ar s a (.Notification p badge send recv) + (decide (isFinalCap' (absCapA (.Notification p badge send recv)) σ)) ⦃ r => + ∃ σ1, ((), σ1) ∈ (fastFinalise (Z := Unit) (absCapA (.Notification p badge send recv)) + (decide (isFinalCap' (absCapA (.Notification p badge send recv)) σ)) σ).1 ∧ M r.2.1 r.2.2 σ1 ⦄) + (empty : ∀ s1 a1 σ1, M s1 a1 σ1 → + cdt.empty_slot st ar s1 a1 (cslotAddrA sl) .Null ⦃ r => + ∃ σ2, ((), σ2) ∈ (emptySlot (Z := Unit) sl .nullCap σ1).1 ∧ R r.1 r.2 σ2 ⦄) : + cdt.delete_one st ar s a (cslotAddrA sl) ⦃ r => + ∃ σ2, ((), σ2) ∈ (capDeleteOne (Z := Unit) sl σ).1 ∧ R r.1 r.2 σ2 ⦄ := by + obtain ⟨cap, hcap, hrel⟩ := hs.getAbsA ht + rw [hc] at hrel + have eq : cap = absCapA (.Notification p badge send recv) := hrel.2.symm + subst cap + have hn : capability.Cap.Notification p badge send recv ≠ .Null := by simp + unfold cdt.delete_one + rw [capA_eq, ← capAtA_eq, hc] + simp only [bind_ok, is_null_f hn, ↓reduceIte, hfin] + refine WP.spec_bind finish ?_ + rintro ⟨⟨rem, cleanup⟩, s1, a1⟩ ⟨σ1, hf, hM⟩ + refine WP.spec_mono (empty s1 a1 σ1 hM) ?_ + rintro r ⟨σ2, he, hR⟩ + refine ⟨σ2, ?_, hR⟩ + unfold capDeleteOne + refine mem_bind_ndD hcap ?_ + simp only [absCapA, NonDet.unless, reduceCtorEq, ↓reduceIte] + exact mem_bind_ndD (x := (decide (isFinalCap' (absCapA (.Notification p badge send recv)) σ), σ)) + rfl (mem_bind_ndD hf he) + +/-- Clear at the related node. [J] The delete-one post uses an unchanged +ghost for Null and the actual removal ghost for a live cap. -/ +theorem invokeIrqClear_refinesA {s : S} {a : A} {σ : State Unit} {th : TypedHeapA} {irq : U64} + (h : IrqRelA (L.mem s) (L.ksb s) σ) (hp : PlaceA th (L.ksb s)) (hi : irq.val < 5376) + {R : S → A → State Unit → Prop} + (del : let sl : CslotPtr := (σ.interruptIrqNode (BitVec.ofNat 13 irq.val), []) + cdt.delete_one st ar s a (cslotAddrA sl) ⦃ r => + ∃ σ', ((), σ') ∈ (capDeleteOne (Z := Unit) sl σ).1 ∧ R r.1 r.2 σ' ⦄) : + (do + let q ← interrupt.irq_slot st s irq + cdt.delete_one st ar s a q) ⦃ r => + ∃ σ', ((), σ') ∈ (invokeIrqHandler (Z := Unit) (.clearIRQHandler (BitVec.ofNat 13 irq.val)) σ).1 ∧ + R r.1 r.2 σ' ⦄ := by + obtain ⟨_, he, hslot⟩ := irqSlot_relatedA (st := st) h hp hi + rw [hslot, bind_ok, ← he] + refine WP.spec_mono del ?_ + rintro r ⟨σ', hd, hR⟩ + refine ⟨σ', ?_, hR⟩ + change ((), σ') ∈ ((getIrqSlot (Z := Unit) (BitVec.ofNat 13 irq.val) >>= capDeleteOne) σ).1 + unfold getIrqSlot + rw [nd_gets_bindA] + exact hd + +/-- Replace the node cap by a send-capable Notification. [J] The two +CDT posts carry their own ghosts, with insertion starting at the exact +deletion successor. -/ +theorem invokeIrqSet_refinesA {s : S} {a : A} {σ : State Unit} {th : TypedHeapA} {irq : U64} + (h : IrqRelA (L.mem s) (L.ksb s) σ) (hp : PlaceA th (L.ksb s)) (hi : irq.val < 5376) + {p badge : U64} {recv : Bool} (src : CslotPtr) + {M : S → A → State Unit → Prop} {R : S → A → State Unit → Prop} + (del : let sl : CslotPtr := (σ.interruptIrqNode (BitVec.ofNat 13 irq.val), []) + cdt.delete_one st ar s a (cslotAddrA sl) ⦃ r => + ∃ σ', ((), σ') ∈ (capDeleteOne (Z := Unit) sl σ).1 ∧ M r.1 r.2 σ' ⦄) + (ins : ∀ s1 a1 σ1, M s1 a1 σ1 → + let sl : CslotPtr := (σ.interruptIrqNode (BitVec.ofNat 13 irq.val), []) + cdt.insert st s1 (.Notification p badge true recv) (cslotAddrA src) (cslotAddrA sl) ⦃ s2 => + ∃ σ2, ((), σ2) ∈ (capInsert (Z := Unit) (absCapA (.Notification p badge true recv)) src sl σ1).1 ∧ + R s2 a1 σ2 ⦄) : + (do + let q ← interrupt.irq_slot st s irq + let (s1, a1) ← cdt.delete_one st ar s a q + let s2 ← cdt.insert st s1 (.Notification p badge true recv) (cslotAddrA src) q + ok (s2, a1)) ⦃ r => + ∃ σ', ((), σ') ∈ (invokeIrqHandler (Z := Unit) (.setIRQHandler + (BitVec.ofNat 13 irq.val) (absCapA (.Notification p badge true recv)) src) σ).1 ∧ R r.1 r.2 σ' ⦄ := by + obtain ⟨_, he, hslot⟩ := irqSlot_relatedA (st := st) h hp hi + rw [hslot, bind_ok, ← he] + refine WP.spec_bind del ?_ + rintro ⟨s1, a1⟩ ⟨σ1, h1, hM⟩ + refine WP.spec_bind (ins s1 a1 σ1 hM) ?_ + rintro s2 ⟨σ2, h2, hR⟩ + refine (WP.spec_ok _).2 ⟨σ2, ?_, hR⟩ + change ((), σ2) ∈ ((getIrqSlot (Z := Unit) (BitVec.ofNat 13 irq.val) >>= fun sl => + capDeleteOne sl >>= fun _ => capInsert (absCapA (.Notification p badge true recv)) src sl) σ).1 + unfold getIrqSlot + rw [nd_gets_bindA] + exact mem_bind_ndD h1 h2 + +end +end +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/IrqInvokeTopA.lean b/proofs/Fl4ProofsA64/P3/IrqInvokeTopA.lean new file mode 100644 index 000000000..5515e95c5 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/IrqInvokeTopA.lean @@ -0,0 +1,104 @@ +/- +P3 (AArch64): accepted IRQ syscall perform posts after the actual +Restart state. [J] The entry projection supplies the IRQ/node and CSpace +facts at that state. The operation post carries the updated CDT ghost. +-/ +import Fl4ProofsA64.P3.IrqInvokeFinalA +import Fl4ProofsA64.P3.IrqInvokeGhostA +import Fl4ProofsA64.P3.IrqInvokeUnbindA +import Fl4ProofsA64.P3.IpcInvokeDispatchA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4SpecA64 Sched + +noncomputable section +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +/-- The Set/Clear selector starts the same local Restart before operating +on the IRQ node. [J] The operation proof carries its CSpace and IRQ frames. -/ +theorem irqHandlerSelected_refinesA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {a : A} {σ : State Unit} (H : SchedSRA st Z m κ s σ) (hst : StEqA Z.T (L.mem s) σ) + {x : Tcb} (hk : σ.kheap σ.curThread = some (.tcb x)) (hr : runnable x.tcbState = true) + {label irq : U64} {extra : Array obj.Slot 3#usize} + {body : S → Result (S × A)} {op : IrqHandlerInvocation} {R : S → A → State Unit → Prop} + (selected : interrupt.decode_irq_handler st ar s a label irq extra = ipcInvokeProgramA st s body) + (step : ∀ s1 σ1, SchedSRA st Z m κ s1 σ1 → StEqA Z.T (L.mem s1) σ1 → + σ1.kheap = (updKSc σ (specPtr (L.curW s)) (.tcb { x with tcbState := .restart })).kheap → + WritesIn st s s1 (fun b => SWrA (st := st) Z s b ∨ b = stA (L.curW s)) → + body s1 ⦃ r => ∃ σ2, ((), σ2) ∈ (invokeIrqHandler (Z := Unit) op σ1).1 ∧ R r.1 r.2 σ2 ⦄) : + interrupt.decode_irq_handler st ar s a label irq extra ⦃ r => + r.1 = .Ok () ∧ ∃ σ1 σ2, + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ((), σ2) ∈ (invokeIrqHandler (Z := Unit) op σ1).1 ∧ R r.2.1 r.2.2 σ2 ⦄ := by + rw [selected] + exact ipcInvokeProgram_refinesA H hst H.curT H.sch.curThread hk hr step + +/-- Label 32 uses the exact clear body with the IRQ node read after Restart. -/ +theorem irqClear_selectPerformA {s : S} {a : A} {irq : U64} {extra : Array obj.Slot 3#usize} : + interrupt.decode_irq_handler st ar s a fl4_abi.label.IRQClearIRQHandler irq extra = + ipcInvokeProgramA st s (fun s1 => do + let sl ← interrupt.irq_slot st s1 irq + cdt.delete_one st ar s1 a sl) := by + rw [irqClear_programA] + simp only [ipcInvokeProgramA, cur_thread_ok, bind_ok, Std.bind_assoc] + +/-- Label 31 uses the decoded Notification and actual delete-then-insert +sequence on the IRQ node. -/ +theorem irqSet_selectPerformA {s : S} {a : A} {irq slot p badge : U64} + {extra : Array obj.Slot 3#usize} {recv : Bool} + (he : Array.index_usize extra 0#usize = ok slot) (hx : slot ≠ 0#u64) + (hc : obj.Slot.cap st slot s = ok (.Notification p badge true recv)) : + interrupt.decode_irq_handler st ar s a fl4_abi.label.IRQSetIRQHandler irq extra = + ipcInvokeProgramA st s (fun s1 => do + let sl ← interrupt.irq_slot st s1 irq + let (s2, a1) ← cdt.delete_one st ar s1 a sl + let s3 ← cdt.insert st s2 (.Notification p badge true recv) slot sl + ok (s3, a1)) := by + rw [irqSet_programA he hx hc] + simp only [ipcInvokeProgramA, cur_thread_ok, bind_ok, Std.bind_assoc] + congr 1 + funext s1 + congr 1 + funext sl + congr 1 + funext result + rcases result with ⟨s2, a1⟩ + simp only [uncurry_apply_pair, Std.bind_assoc, bind_ok] + +/-- Compose a Plain or TriggerCore issue body with the actual caller Restart. +[J] The body post supplies the new CDT ghost after the machine and slot +steps. The decoder equality is supplied by the accepted label-29, -65 or +-66 selector. -/ +theorem irqControlSelected_refinesA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {a : A} {σ : State Unit} (H : SchedSRA st Z m κ s σ) (hst : StEqA Z.T (L.mem s) σ) + {x : Tcb} (hk : σ.kheap σ.curThread = some (.tcb x)) (hr : runnable x.tcbState = true) + {label len src buf : U64} {extra : Array obj.Slot 3#usize} + {body : S → Result (S × A)} {op : Fl4IrqControlInvocation} {R : S → A → State Unit → Prop} + (selected : interrupt.decode_irq_control st ar s a label len src buf extra = ipcInvokeProgramA st s body) + (step : ∀ s1 σ1, SchedSRA st Z m κ s1 σ1 → StEqA Z.T (L.mem s1) σ1 → + σ1.kheap = (updKSc σ (specPtr (L.curW s)) (.tcb { x with tcbState := .restart })).kheap → + WritesIn st s s1 (fun b => SWrA (st := st) Z s b ∨ b = stA (L.curW s)) → + body s1 ⦃ r => ∃ σ2, (.ok (), σ2) ∈ ((invokeFl4IrqControl (Z := Unit) op).run σ1).1 ∧ R r.1 r.2 σ2 ⦄) : + interrupt.decode_irq_control st ar s a label len src buf extra ⦃ r => + r.1 = .Ok () ∧ ∃ σ1 σ2, + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + (.ok (), σ2) ∈ ((invokeFl4IrqControl (Z := Unit) op).run σ1).1 ∧ R r.2.1 r.2.2 σ2 ⦄ := by + rw [selected] + have code : sched.TS_RESTART.val = tsCodeA .restart := by rw [ts_vals.2.1]; rfl + have hcur := H.sch.curThread + rw [hcur] at hk + simp only [ipcInvokeProgramA, cur_thread_ok, bind_ok] + refine WP.spec_bind (sc_set_runnable_stateA H hst H.curT hk hr (ts := .restart) rfl code) ?_ + rintro s1 ⟨σ1, h1, HS, HT, hh, hw⟩ + refine WP.spec_bind (step s1 σ1 HS HT hh hw) ?_ + rintro ⟨s2, a2⟩ ⟨σ2, h2, hR⟩ + change (ok (.Ok (), s2, a2) : Result (core.result.Result Unit error.Exc × S × A)) ⦃ _ ⦄ + rw [WP.spec_ok] + exact ⟨rfl, σ1, σ2, by simpa only [hcur] using h1, h2, hR⟩ + +end +end +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/IrqInvokeUnbindA.lean b/proofs/Fl4ProofsA64/P3/IrqInvokeUnbindA.lean new file mode 100644 index 000000000..45530057a --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/IrqInvokeUnbindA.lean @@ -0,0 +1,155 @@ +/- +P3 (AArch64): linked notification objects and CDT transitions for IRQHandler. +-/ +import Fl4ProofsA64.P3.NtfnRelA +import Fl4ProofsA64.P3.ScNtfnLinkA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 +open Fl4Spec Fl4SpecA64 Sched + +noncomputable section + +set_option Elab.async false +set_option maxRecDepth 768 +set_option maxHeartbeats 90000 +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +private theorem irqUnbindNotificationSc_programA (s : S) (n : U64) : + schedcontext.maybe_unbind_ntfn st s n = + if L.mem s (nScSc n) = 0#u64 then ok s + else schedcontext.unbind_ntfn st s (L.mem s (nScSc n)) := by + have hword : obj.Ntfn.sc st n s = ok (L.mem s (nScSc n)) := + eq_of_spec_fd (Notif.obj.Ntfn.sc.spec s n) + unfold schedcontext.maybe_unbind_ntfn + rw [hword, bind_ok, eq_of_spec_fd (P1.obj.Sc.is_null.spec _), bind_ok] + by_cases h : L.mem s (nScSc n) = 0#u64 + · rw [if_pos h, decide_eq_true h, if_pos rfl] + · rw [if_neg h, decide_eq_false h, if_neg Bool.false_ne_true] + +private theorem irqUnbindNotificationSc_liveSpecA {σ : State Unit} {p q : ObjRef} + {n : Notification} {sc : SchedContext} {k : Nat} + (hn : σ.kheap p = some (.notification n)) (hs : σ.kheap q = some (.schedContext sc k)) + (hns : n.ntfnSc = some q) (hsn : sc.scNtfn = some p) : + schedContextMaybeUnbindNtfn (Z := Unit) p σ = schedContextUnbindNtfn (Z := Unit) q σ := by + unfold schedContextMaybeUnbindNtfn schedContextUnbindNtfn + rw [nd_bind_oneSc (getNtfnSc_eqSc hn), nd_bind_oneSc (getScNtfn_eqA hs), hns, hsn] + rfl + +private theorem irqUnbindNotificationSc_nullSpecA {σ : State Unit} {p : ObjRef} + {n : Notification} (hn : σ.kheap p = some (.notification n)) (hs : n.ntfnSc = none) : + schedContextMaybeUnbindNtfn (Z := Unit) p σ = ({((), σ)}, False) := by + unfold schedContextMaybeUnbindNtfn + rw [nd_bind_oneSc (getNtfnSc_eqSc hn), hs] + rfl + +/-- Clear the notification and SC backlinks through the existing live +unbinding lemma, with the notification-only store word in its write set. -/ +theorem irqUnbindNotificationScA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {σ : State Unit} (H : SchedSRA st Z m κ s σ) (n : U64) + (hn0 : n ≠ 0#u64) (hN : NtfnScEqA m n (L.mem s) σ) + (hal : Aligned (nScSc n)) (hout : ¬ SReadA st Z m s σ (nScSc n)) + (hsc : ∀ sc, L.mem s (nScSc n) = sc → sc ≠ 0#u64 → + sc ∈ Z.C ∧ L.mem s (scWA sc 40#u64) = n) : + schedcontext.maybe_unbind_ntfn st s n ⦃ s' => ∃ σ', + schedContextMaybeUnbindNtfn (Z := Unit) (specPtr n) σ = ({((), σ')}, False) ∧ + SchedSRA st Z m κ s' σ' ∧ NtfnScEqA m n (L.mem s') σ' ∧ + WritesIn st s s' (fun w => MWrA (st := st) Z m σ s w ∨ w = nScSc n) ⦄ := by + obtain ⟨xn, hkn, hxn⟩ := hN + let sc := L.mem s (nScSc n) + rw [irqUnbindNotificationSc_programA] + by_cases h0 : sc = 0#u64 + · have hn : xn.ntfnSc = none := by + rw [hxn] + change optScA m sc = none + rw [optScA, if_pos h0] + rw [if_pos h0, WP.spec_ok] + exact ⟨σ, irqUnbindNotificationSc_nullSpecA hkn hn, H, ⟨xn, hkn, hxn⟩, WritesIn.refl _ _⟩ + · obtain ⟨hc, hback⟩ := hsc sc rfl h0 + obtain ⟨xs, ns, hks, hxs⟩ := H.scr.sc sc hc + have hxs' : xs.scNtfn = some (specPtr n) := by rw [hxs.ntfn, hback, optRefS_ne hn0] + have hxn' : xn.ntfnSc = some (scNameA m sc) := by + rw [hxn] + change optScA m sc = _ + rw [optScA, if_neg h0] + rfl + rw [if_neg h0, irqUnbindNotificationSc_liveSpecA hkn hks hxn' hxs'] + exact sc_unbind_ntfn_someA H hc h0 hn0 hal hout hback hkn + +private theorem getNtfnBound_irqA {σ : State Unit} {q : ObjRef} {x : Notification} + (hk : σ.kheap q = some (.notification x)) : + getNtfnObjRef (Z := Unit) Notification.ntfnBoundTcb q σ = ({(x.ntfnBoundTcb, σ)}, False) := by + unfold getNtfnObjRef getSkObjRef + rw [nd_bind_oneSc (getSimpleKo_ntfnSc hk)] + rfl + +private theorem setNtfnBound_irqA {σ : State Unit} {q : ObjRef} {x : Notification} + (hk : σ.kheap q = some (.notification x)) : + setNtfnObjRef (Z := Unit) ntfnBoundTcbUpdate q none σ = + ({((), updKSc σ q (.notification { x with ntfnBoundTcb := none }))}, False) := by + unfold setNtfnObjRef updateSkObjRef + rw [nd_bind_oneSc (getSimpleKo_ntfnSc hk)] + unfold setSimpleKo + rw [nd_bind_oneSc (getObject_eqSc hk)] + rw [nd_bind_oneSc (show (Fl4Spec.NonDet.assert (σ := State Unit) (isSimpleType (.notification x))) σ = + ({((), σ)}, False) by simp [Fl4Spec.NonDet.assert, isSimpleType, aType])] + rw [nd_bind_oneSc (show (Fl4Spec.NonDet.assert (σ := State Unit) + (partialInv KernelObject.notification (.notification x) ≠ none)) σ = ({((), σ)}, False) by + simp [Fl4Spec.NonDet.assert, partialInv_ntfnSc])] + exact setObject_eqSc hk rfl + +/-- No TCB is bound to this notification, so the two operations are identities. -/ +theorem irqUnbindNotificationTcb_nullA {s : S} {σ : State Unit} {n : U64} {x : Notification} + (hk : σ.kheap (specPtr n) = some (.notification x)) + (hs : x.ntfnBoundTcb = none) (hw : L.mem s (Notif.nBoundA n) = 0#u64) : + notification.unbind_maybe_notification st s n = ok s ∧ + unbindMaybeNotification (Z := Unit) (specPtr n) σ = ({((), σ)}, False) := by + constructor + · unfold notification.unbind_maybe_notification + rw [eq_of_spec_fd (Notif.obj.Ntfn.bound_tcb.spec s n), bind_ok, hw, + eq_of_spec_fd (P1.obj.Tcb.is_null.spec _), bind_ok] + rfl + · unfold unbindMaybeNotification + rw [nd_bind_oneSc (getNtfnBound_irqA hk), hs] + rfl + +/-- A bound notification and its TCB each lose their one backlink. -/ +theorem irqUnbindNotificationTcb_someA {s : S} {σ : State Unit} {n t : U64} + {x : Notification} {y : Tcb} + (hk : σ.kheap (specPtr n) = some (.notification x)) + (ht : σ.kheap (specPtr t) = some (.tcb y)) (ht0 : t ≠ 0#u64) + (hs : x.ntfnBoundTcb = some (specPtr t)) (hw : L.mem s (Notif.nBoundA n) = t) : + let s' := L.wr (L.wr s (ntfnBtA n) 0#u64) (boundA t) 0#u64 + let σ' := updKSc (updKSc σ (specPtr n) (.notification { x with ntfnBoundTcb := none })) + (specPtr t) (.tcb { y with tcbBoundNotification := none }) + notification.unbind_maybe_notification st s n = ok s' ∧ + unbindMaybeNotification (Z := Unit) (specPtr n) σ = ({((), σ')}, False) := by + dsimp only + constructor + · unfold notification.unbind_maybe_notification + rw [eq_of_spec_fd (Notif.obj.Ntfn.bound_tcb.spec s n), bind_ok, hw, + eq_of_spec_fd (P1.obj.Tcb.is_null.spec _), bind_ok] + simp only [decide_eq_false ht0, Bool.false_eq_true, ↓reduceIte] + rw [eq_of_spec_fd (P1.obj.Ntfn.set_bound_tcb.wr (st := st) s n 0#u64), bind_ok] + exact eq_of_spec_fd (P1.obj.Tcb.set_bound_ntfn.wr _ t 0#u64) + · have hp : specPtr t ≠ specPtr n := by + intro e + rw [e, hk] at ht + cases ht + have hnew : (updKSc σ (specPtr n) (.notification { x with ntfnBoundTcb := none })).kheap (specPtr t) = + some (.tcb y) := by rw [updKSc_kheap, Function.update_of_ne hp, ht] + unfold unbindMaybeNotification + rw [nd_bind_oneSc (getNtfnBound_irqA hk), hs] + simp only [Fl4Spec.NonDet.maybeM] + change (setNtfnObjRef (Z := Unit) ntfnBoundTcbUpdate (specPtr n) none >>= fun _ => + threadSet (fun tcb => { tcb with tcbBoundNotification := none }) (specPtr t)) σ = _ + rw [nd_bind_oneSc (setNtfnBound_irqA hk)] + exact threadSet_eqSc _ hnew + + +end +end +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/IrqSlotsFootA.lean b/proofs/Fl4ProofsA64/P3/IrqSlotsFootA.lean new file mode 100644 index 000000000..7cb9151f8 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/IrqSlotsFootA.lean @@ -0,0 +1,141 @@ +/- +P3 (aarch64, A64-P3 Stage-3 integration; Architect 01:46Z R-hirq): **`IrqSlotsKeepA` from the scheduler and reply-removal +footprints**, moved here from `P3/Stage3SchedPresA` into a leaf module upstream of `ReplyRemoveBorA` / +`ReplyRemoveUnblockJointA` / `ReplyLiveJointA`, so those can discharge their `hirqR` / `hirqU` premises with +`IrqSlotsKeepA.of_replyFoot` on the footprints they compute. Contents unchanged: `SchedWrA`, `SchedSRN.swr_tcb_in`, +`SchedSRN.swr_off_irq`, `IrqSlotsKeepA.of_swr`, `IrqSlotsKeepA.of_mwr`, `SchedSRN.ewrite_off_irq`, +`IrqSlotsKeepA.of_replyFoot`. +-/ +import Fl4ProofsA64.P3.ReplyPopFootA +import Fl4ProofsA64.P3.ReplyXferKeepA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **The contract's `SchedWrA`**: the narrowed scheduler write set (`MWrA`, module docstring). -/ +abbrev SchedWrA (Z : XSetsA) (m : Fl4SpecA64.McState) (σ : Fl4SpecA64.State Unit) (s : S) (b : U64) : Prop := + MWrA (st := st) Z m σ s b + +/-- A scheduler word of a TCB of `Z.T` lies inside the TCB object. -/ +theorem SchedSRN.swr_tcb_in {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} + {σ : Fl4SpecA64.State Unit} (H : SchedSRN st Z m κ s σ) {u b : U64} (hu : u ∈ Z.T) + (h : b = scA u ∨ b = nextA .Sched u ∨ b = prevA .Sched u ∨ b = inqA u) : + u.val ≤ b.val ∧ b.val < u.val + 4096 := by + have hfit := (H.core.tcbs u hu).fits + have hv : ∀ K : U64, K.val < 4096 → (wa u K).val = u.val + K.val := by + intro K hK; rw [wa_valm]; omega + rcases h with e | e | e | e + · change b = wa u 3464#u64 at e + rw [e, hv _ (by simp)]; simp + · change b = wa u 3480#u64 at e + rw [e, hv _ (by simp)]; simp + · change b = wa u 3488#u64 at e + rw [e, hv _ (by simp)]; simp + · change b = wa u 3592#u64 at e + rw [e, hv _ (by simp)]; simp + +/-- **The scheduler words are off the IRQ-node slot region.** -/ +theorem SchedSRN.swr_off_irq {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} + {σ : Fl4SpecA64.State Unit} (H : SchedSRN st Z m κ s σ) {b : U64} (h : SWrA (st := st) Z s b) : + b.val < (L.ksb s).val + 43232 ∨ (L.ksb s).val + 215264 ≤ b.val := by + rcases h with h | ⟨u, hu, e⟩ + · unfold SKsA at h; omega + · have hin := H.swr_tcb_in hu e + rcases H.tcb_ks hu with h | h + · omega + · unfold ksSizeA at h; omega + +/-- **`IrqSlotsKeepA` from the scheduler write set** (module docstring). -/ +theorem IrqSlotsKeepA.of_swr {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s s' : S} + {σ : Fl4SpecA64.State Unit} (H : SchedSRN st Z m κ s σ) (w : WritesIn st s s' (SWrA (st := st) Z s)) : + IrqSlotsKeepA (st := st) s s' := by + intro b hb hlo hhi + refine w.mem_eq hb fun h => ?_ + have := H.swr_off_irq h + omega + +/-- The same from the bundle `MFrA`, through the scheduler-word part (the SC footprint is off the kernel-state +block). -/ +theorem IrqSlotsKeepA.of_mwr {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s s' : S} + {σ σ' : Fl4SpecA64.State Unit} (H : SchedSRN st Z m κ s σ) (F : MFrA (st := st) Z m s s' σ σ') : + IrqSlotsKeepA (st := st) s s' := by + intro b hb hlo hhi + refine F.wr.mem_eq hb fun h => ?_ + rcases h with hs | hs + · have := H.scp.ks_out hs + have hk : ksSizeA = 788736 := rfl + omega + · have := H.swr_off_irq hs + omega + +section ipcfoot +variable {N E R : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + +/-- **The IPC objects' words are off the IRQ-node slot region**: endpoint, reply, TCB and buffer words +(`EWrite`) and the reply link words (`RLinkW`). -/ +theorem SchedSRN.ewrite_off_irq {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} + {σ : Fl4SpecA64.State Unit} (H : SchedSRN st Z m κ s σ) (PE : EPlaced Z.T E R Bf) + (P : IpcSchedPlacementA Z.th N E R Bf) {b : U64} (h : EWrite Z.T E R Bf b ∨ RLinkW R b) : + b.val < (L.ksb s).val + 43232 ∨ (L.ksb s).val + 215264 ≤ b.val := by + have typed : ∀ p z, Z.th p = some (.other z) → p.val ≤ b.val → b.val < p.val + z → + b.val < (L.ksb s).val + 43232 ∨ (L.ksb s).val + 215264 ≤ b.val := by + intro p z hp hlo hhi + have hk := H.place.ks p (.other z) hp (fun irq => by have := H.place.irqK hp irq; cases this) + change p.val + z ≤ (L.ksb s).val ∨ (L.ksb s).val + ksSizeA ≤ p.val at hk + have hks : ksSizeA = 788736 := rfl + omega + have tcbOff : ∀ t ∈ Z.T, t.val ≤ b.val → b.val < t.val + 4096 → + b.val < (L.ksb s).val + 43232 ∨ (L.ksb s).val + 215264 ≤ b.val := by + intro t ht hlo hhi + rcases H.tcb_ks ht with h | h + · omega + · unfold ksSizeA at h; omega + rcases h with (⟨e, he, h | h⟩ | ⟨r, hr, h⟩ | ⟨t, ht, h | h⟩ | h) | ⟨r, hr, h | h⟩ + · exact typed e 16 (P.endpoint e he) (by omega) (by omega) + · exact typed e 16 (P.endpoint e he) (by omega) (by omega) + · exact typed r 32 (P.reply r hr) (by omega) (by omega) + · obtain ⟨h1, h2⟩ := h + exact tcbOff t ht h1 (by unfold TWOffN at h2; omega) + · exact tcbOff t ht (by unfold TL at h; omega) (by unfold TL at h; omega) + · obtain ⟨p, len, hp, h1, h2⟩ := P.buffer b h + exact typed p len hp h1 h2 + · obtain ⟨-, v⟩ := replyNextPlaceA (Z := Z) PE hr + have ev := congrArg UScalar.val h + rw [v] at ev + exact typed r 32 (P.reply r hr) (by omega) (by omega) + · obtain ⟨-, v⟩ := replyPrevPlaceA (Z := Z) PE hr + have ev := congrArg UScalar.val h + rw [v] at ev + exact typed r 32 (P.reply r hr) (by omega) (by omega) + +/-- **`IrqSlotsKeepA` from the reply-removal footprint `ReplyFootW`** (discharges `hirqR` / `hirqU`). -/ +theorem IrqSlotsKeepA.of_replyFoot {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s s1 : S} + {σ : Fl4SpecA64.State Unit} (H : SchedSRN st Z m κ s σ) (PE : EPlaced Z.T E R Bf) + (P : IpcSchedPlacementA Z.th N E R Bf) (w : WritesIn st s s1 (ReplyFootW (st := st) Z m σ s E R Bf)) : + IrqSlotsKeepA (st := st) s s1 := by + intro b hb hlo hhi + refine w.mem_eq hb fun h => ?_ + rcases h with h | h | h + · have := H.ewrite_off_irq PE P (Or.inl h); omega + · rcases h with hs | hs + · have := H.scp.ks_out hs + have hk : ksSizeA = 788736 := rfl + omega + · have := H.swr_off_irq hs; omega + · have := H.ewrite_off_irq PE P (Or.inr h); omega + +end ipcfoot + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/JBootA.lean b/proofs/Fl4ProofsA64/P3/JBootA.lean new file mode 100644 index 000000000..2ba54df94 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/JBootA.lean @@ -0,0 +1,831 @@ +/- +P3 (aarch64, A64-P3 S3 gens 1–2): **the early non-vacuity witness `jbootA`**: `JAtZAX` (the joint relation with its +three extension records) and `SInvXA` hold **jointly**, at `nc` cores (`0 < nc ≤ 128`), at the pair + +* implementation: any `LawfulStore` state `s` whose words are A64-Boot's `w0` (`BootWordsA`, below), and +* abstract: **`bootSA kb ib nc now mt`**, l4v's `init_A_st` (`Fl4SpecA64.initASt`) with the heap laid out as FL4's boot + (`bootKA`: core `c`'s idle TCB at `ib + 8192 c`, its idle SC at `+ 4096`; core 0's idle SC named + `idle_sc_ptr`, `scRenA`; gen 3: the IRQ node's 5376 empty one-slot CNodes at `kb + 43232 + 32 i`, and + `interrupt_irq_node` naming them, `irqNodeOfA`), with `m = bootMA ib nc` (core 0's view, each core's idle thread + and SC, each idle TCB's affinity its core). Gen 30: each idle SC carries FL4's two boot refills `(now, mt), (now, 0)` + (`idleScObjA now mt`, `configure_sched_context`), so `bootSA` takes the boot time slice `mt`. + +So the relation is not vacuous at FL4's boot layout **with any number of cores**, unlike x86's (jboot gen 6: +`idle2_not_sinvX`, two idle TCBs against x86's `OnlyIdle`). `initASt` itself is not used as the abstract state for +the reason x86 found (JBootInitA `initASt_not_jAtZ`: l4v's fixed `idle_thread_ptr` is not FL4's idle TCB); `bootSA` +keeps every other field of `initASt`. + +**Plugged into A64-Boot's `w0`** (gen 2, after `par/a64-boot` was merged): `P3/JBootW0A` proves `BootWordsA` at +`bootS kb ib nc r now mt` from `bootM`'s word lemmas and `BootLayA` from `BootLay` (with the distance from +`idle_sc_ptr`), so `jbootA_w0` / `jbootA_w0_ex` state the witness at the concrete `w0`. `w1` (post-`create_root_task`) +needs the CNode, untyped and VSpace rows of S4 and is not attempted. + +Named premises of `jbootA`: +* `hW : BootWordsA st s ib now nc` **[J]** — the boot state's words (discharged by A64-Boot's `w0`, above); +* `hL : BootLayA ib nc` **[J]** — the idle area's layout (A64-Boot's `BootLay`, plus `scOff`); the block's layout + `BootKsA kb ib nc` [J] is the field `hW.ks` (gen 3); +* `hAR : AR a (bootZA ib nc).hw` **[P]** — the platform's hardware root at boot is related to l4v's global user + VSpace (`init_arch_state`'s `arm_us_global_vspace`, no ASID: `bootHwA`; the arch relation is a parameter). + +Covered since gen 3: the IRQ node (`th`'s one-slot CNodes, `PlaceA.irq`, `IrqRelA` with every IRQ inactive); gen 4: +`SInvXA.irqN` (`boot_specIrq`: 5376 empty one-slot CNodes, none above, every cap null; `boot_sinvXA` now takes the +block's layout `hK`). Not +covered (S4): the global page table, the root task (`w1`). +-/ +import Fl4ProofsA64.P3.JointA +import Fl4ProofsA64.P3.SInvXA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## The boot layout -/ + +/-- Core `c`'s idle TCB (`idle_base + 8192 c`, A64-Boot's IdleArea). -/ +def idleTA (ib : U64) (c : Nat) : U64 := ⟨ib.bv + BitVec.ofNat 64 (8192 * c)⟩ + +/-- Core `c`'s idle SC (`idle_base + 8192 c + 4096`). -/ +def idleSA (ib : U64) (c : Nat) : U64 := ⟨ib.bv + BitVec.ofNat 64 (8192 * c + 4096)⟩ + +/-- `idle_sc_ptr` as a number (`pptr_base + 0x3000`, `pptr_base = 2^39`). -/ +theorem idleScPtr_toNat : Fl4SpecA64.idleScPtr.toNat = 549755826176 := by decide + +/-- **The idle area's layout** [J] (A64-Boot's `BootLay` for the idle area, and `scOff`: the area misses the FL4 +pointer l4v's `idle_sc_ptr` names, `fl4Ptr idle_sc_ptr = 0xffff_8000_0000_3000`; gen 12: was `idle_sc_ptr` itself +under the identity naming). -/ +structure BootLayA (ib : U64) (nc : Nat) : Prop where + al : ib.val % 4096 = 0 + pos : 0 < ib.val + nc0 : 0 < nc + nc128 : nc ≤ 128 + fit : ib.val + nc * 8192 ≤ 2 ^ 64 + scOff : 18446603336221208576 < ib.val ∨ ib.val + nc * 8192 ≤ 18446603336221208576 + +/-- (gen 3) **The kernel-state block's layout at boot** [J] (A64-Boot's `BootLay` for the block: it fits, misses the +idle area, is 32-aligned, and misses the FL4 pointer of l4v's `idle_sc_ptr` (gen 12, as `BootLayA.scOff`), so the IRQ node's CNodes are not renamed away). -/ +structure BootKsA (kb ib : U64) (nc : Nat) : Prop where + fit : kb.val + ksSizeA ≤ 2 ^ 64 + apart : ib.val + nc * 8192 ≤ kb.val ∨ kb.val + ksSizeA ≤ ib.val + al : kb.val % 32 = 0 + scOff : 18446603336221208576 < kb.val ∨ kb.val + ksSizeA ≤ 18446603336221208576 + +/-- `BootLayA` at A64-Boot's `boot_witness` constants (`ib = PPTR_BASE + 2^31`, 128 cores). -/ +theorem bootLayA_ex : BootLayA 18446603338368679936#u64 128 := ⟨by decide, by decide, by decide, by decide, by decide, + Or.inl (by decide)⟩ + +section lay +variable {ib : U64} {nc : Nat} (hL : BootLayA ib nc) +include hL + +theorem idleTA_val {c : Nat} (hc : c < nc) : (idleTA ib c).val = ib.val + 8192 * c := by + have hf := hL.fit + show (ib.bv + BitVec.ofNat 64 (8192 * c)).toNat = _ + rw [BitVec.toNat_add, BitVec.toNat_ofNat] + have : ib.bv.toNat = ib.val := rfl + omega + +theorem idleSA_val {c : Nat} (hc : c < nc) : (idleSA ib c).val = ib.val + 8192 * c + 4096 := by + have hf := hL.fit + show (ib.bv + BitVec.ofNat 64 (8192 * c + 4096)).toNat = _ + rw [BitVec.toNat_add, BitVec.toNat_ofNat] + have : ib.bv.toNat = ib.val := rfl + omega + +theorem idleTA_inj {c d : Nat} (hc : c < nc) (hd : d < nc) (h : specPtr (idleTA ib c) = specPtr (idleTA ib d)) : + c = d := by + have e := congrArg UScalar.val (specPtr_inj h) + have h1 := idleTA_val hL hc + have h2 := idleTA_val hL hd + have : (idleTA ib c).val = (idleTA ib d).val := e + omega + +theorem idleSA_inj {c d : Nat} (hc : c < nc) (hd : d < nc) (h : specPtr (idleSA ib c) = specPtr (idleSA ib d)) : + c = d := by + have e := congrArg UScalar.val (specPtr_inj h) + have h1 := idleSA_val hL hc + have h2 := idleSA_val hL hd + have : (idleSA ib c).val = (idleSA ib d).val := e + omega + +theorem idleSA_ne_TA {c d : Nat} (hc : c < nc) (hd : d < nc) : specPtr (idleSA ib c) ≠ specPtr (idleTA ib d) := by + intro h + have e := congrArg UScalar.val (specPtr_inj h) + have h1 := idleSA_val hL hc + have h2 := idleTA_val hL hd + have : (idleSA ib c).val = (idleTA ib d).val := e + omega + +theorem idleTA_ne_sc {c : Nat} (hc : c < nc) : specPtr (idleTA ib c) ≠ Fl4SpecA64.idleScPtr := by + intro h + have h1 := idleTA_val hL hc + have : (idleTA ib c).val = 18446603336221208576 := specPtr_eq_idleSc.1 h + have := hL.scOff + omega + +theorem idleSA_ne_sc {c : Nat} (hc : c < nc) : specPtr (idleSA ib c) ≠ Fl4SpecA64.idleScPtr := by + intro h + have h1 := idleSA_val hL hc + have : (idleSA ib c).val = 18446603336221208576 := specPtr_eq_idleSc.1 h + have := hL.scOff + omega + +theorem idleTA_ne0 {c : Nat} (hc : c < nc) : idleTA ib c ≠ 0#u64 := by + intro h + have h1 := congrArg UScalar.val h + rw [idleTA_val hL hc] at h1 + have h2 := hL.pos + have h3 : (0#u64 : U64).val = 0 := rfl + omega + +theorem idleSA_ne0 {c : Nat} (hc : c < nc) : idleSA ib c ≠ 0#u64 := by + intro h + have h1 := congrArg UScalar.val h + rw [idleSA_val hL hc] at h1 + have h3 : (0#u64 : U64).val = 0 := rfl + omega + +end lay + +/-- `SA` is `TA + 4096` (modular, no premise). -/ +theorem idleTA_addR (ib : U64) (c : Nat) : (⟨(idleTA ib c).bv + 4096#64⟩ : U64) = idleSA ib c := by + apply UScalar.eq_of_val_eq + show ((ib.bv + BitVec.ofNat 64 (8192 * c)) + 4096#64).toNat = (ib.bv + BitVec.ofNat 64 (8192 * c + 4096)).toNat + simp only [BitVec.toNat_add, BitVec.toNat_ofNat] + omega + +/-- (gen 12) `TA + 4096` is `SA` at the spec names (modular, no premise). -/ +theorem idleTA_add (ib : U64) (c : Nat) : fl4Ptr (specPtr (idleTA ib c) + 4096#64) = idleSA ib c := by + rw [show specPtr (idleTA ib c) + 4096#64 = specPtr ⟨(idleTA ib c).bv + 4096#64⟩ from (specPtr_mk_add _ _).symm, + fl4Ptr_specPtr] + exact idleTA_addR ib c + +/-- (gen 12) `SA - 4096` is `TA` at the spec names (modular, no premise). -/ +theorem idleSA_sub (ib : U64) (c : Nat) : specPtr (idleSA ib c) - 4096#64 = specPtr (idleTA ib c) := by + rw [← idleTA_addR, specPtr_mk_add]; exact BitVec.add_sub_cancel _ _ + +/-! ## The boot ghosts and abstract state -/ + +/-- The idle TCBs. -/ +def bootTA (ib : U64) (nc : Nat) : Set U64 := {t | ∃ c < nc, t = idleTA ib c} + +/-- The idle SCs. -/ +def bootCA (ib : U64) (nc : Nat) : Set U64 := {x | ∃ c < nc, x = idleSA ib c} + +/-- The typed heap at boot: the idle TCBs and (gen 3) the 5376 one-slot IRQ-node CNodes of the block at `kb`. -/ +def bootThA (kb ib : U64) (nc : Nat) : TypedHeapA := fun p => + if p ∈ bootTA ib nc then some .tcb else if IrqNodeA kb p then some (.cnode 0) else none + +/-- The multi-core state at boot, core 0's view. -/ +def bootMA (ib : U64) (nc : Nat) : Fl4SpecA64.McState where + curCore := 0 + idleThreadOf c := specPtr (idleTA ib c) + idleScOf c := specPtr (idleSA ib c) + tcbAffinity p := if h : ∃ c, c < nc ∧ p = (specPtr (idleTA ib c)) then some (Classical.choose h) else none + +/-- The hardware root at boot: l4v's global user VSpace (`init_arch_state`), no ASID. -/ +def bootHwA : HwRootA := ⟨(Fl4SpecA64.initASt (Z := Unit)).archState.armUsGlobalVspace, none⟩ + +/-- The ghost sets at boot (the IRQ node's CNodes only, no CDT edge, no VCPU, no FL4 root; each core runs its idle +thread on its idle SC). -/ +def bootZA (kb ib : U64) (nc : Nat) : XSetsA := + ⟨bootThA kb ib nc, ⟨[], fun _ => none⟩, bootTA ib nc, bootCA ib nc, {c | c.val < nc}, ∅, ∅, ∅, + fun c => (specPtr (idleTA ib c)), fun c => scRenA (bootMA ib nc) (idleSA ib c), bootHwA, + fun _ _ => [], fun _ => [], ∅, ∅, fun _ => 0, ∅, fun _ => 0⟩ + +/-- An idle TCB: l4v's `init_kheap` idle TCB (`default_tcb` in `IdleThreadState`) with its SC. -/ +def idleTcbA (sc : Fl4SpecA64.ObjRef) : Fl4SpecA64.Tcb := + { Fl4SpecA64.defaultTcb 0 with tcbState := .idleThreadState, tcbSchedContext := some sc } + +/-- An idle SC bound to `t` (gen 3: `sc_refill_max` 2, FL4's `REFILL_MAX` at boot; gen 30: FL4's two boot refills +`(now, mt), (now, 0)`, `configure_sched_context` → `refill_new` at period 0 + `maybe_add_empty_tail`). -/ +def idleScObjA (now mt : U64) (t : Fl4SpecA64.ObjRef) : Fl4SpecA64.SchedContext := + { Fl4SpecA64.defaultSchedContext with scTcb := some t, scRefills := [⟨now.bv, mt.bv⟩, ⟨now.bv, 0⟩], scRefillMax := 2 } + +/-- **The boot heap** (module docstring): the idle TCBs and SCs and (gen 3) the IRQ node's empty one-slot CNodes. -/ +def bootKA (kb ib : U64) (nc : Nat) (now mt : U64) : Fl4SpecA64.KHeap := fun p => + if p = Fl4SpecA64.idleScPtr then some (.schedContext (idleScObjA now mt (specPtr (idleTA ib 0))) 0) + else if ∃ c, c < nc ∧ p = (specPtr (idleTA ib c)) then some (.tcb (idleTcbA (scRenA (bootMA ib nc) (fl4Ptr (p + 4096#64))))) + else if ∃ c, 0 < c ∧ c < nc ∧ p = (specPtr (idleSA ib c)) then some (.schedContext (idleScObjA now mt (p - 4096#64)) 0) + else if ∃ q, IrqNodeA kb q ∧ p = (specPtr q) then some (.cnode 0 (Fl4SpecA64.emptyCnode 0)) + else none + +/-- (gen 3) The spec's IRQ node at boot: IRQ `irq`'s slot is FL4's `kb + 43232 + 32 irq` (`irqSlotA`). -/ +def irqNodeOfA (kb : U64) (irq : Fl4SpecA64.Irq) : Fl4SpecA64.ObjRef := + specPtr (irqSlotA kb ⟨BitVec.ofNat 64 irq.toNat⟩) + +/-- **The abstract boot state** (module docstring): `initASt` with FL4's boot heap, idle thread and (gen 3) IRQ +node. -/ +def bootSA (kb ib : U64) (nc : Nat) (now mt : U64) : Fl4SpecA64.State Unit := + { Fl4SpecA64.initASt (Z := Unit) with + kheap := bootKA kb ib nc now mt, + isOriginalCap := fun _ => false, + curThread := (specPtr (idleTA ib 0)), + idleThread := (specPtr (idleTA ib 0)), + curTime := now.bv, + reprogramTimer := true, + domainTime := 72057594037927935#64, + interruptIrqNode := irqNodeOfA kb, + -- boot has read the counter at `now` + machineState := { (Fl4SpecA64.initASt (Z := Unit)).machineState with lastMachineTime := now.bv } } + +/-- The boot state's heap is `bootKA`. -/ +theorem bootSA_kheap (kb ib : U64) (nc : Nat) (now mt : U64) : (bootSA kb ib nc now mt).kheap = bootKA kb ib nc now mt := rfl + +theorem idleScObjA_tcb {now mt : U64} (t : Fl4SpecA64.ObjRef) : (idleScObjA now mt t).scTcb = some t := rfl + +/-! ## The boot words -/ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **The words of the boot state the relation reads** [J] (module docstring: A64-Boot's `w0` writes each). -/ +structure BootWordsA (st : machine.Store S) [L : LawfulStore st] (s : S) (kb ib now : U64) (nc : Nat) : Prop where + cpu : L.cpu s = 0#u64 + /-- (gen 3) The block is at `kb`, laid out as `BootKsA`. -/ + ksb : L.ksb s = kb + ks : BootKsA kb ib nc + idleT : ∀ c : U64, c.val < nc → L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) = idleTA ib c.val + idleS : ∀ c : U64, c.val < nc → L.mem s (L.nodeAt s c layout.node.IDLE_SC) = idleSA ib c.val + curT : ∀ c : U64, c.val < nc → L.mem s (L.nodeAt s c layout.node.CUR_THREAD) = idleTA ib c.val + curS : ∀ c : U64, c.val < nc → L.mem s (L.nodeAt s c layout.node.CUR_SC) = idleSA ib c.val + act : L.mem s (L.nodeAt s 0#u64 layout.node.SCHED_ACTION) = 0#u64 + time : L.mem s (L.nodeAt s 0#u64 layout.node.CUR_TIME) = now + tst : ∀ c < nc, L.mem s (stA (idleTA ib c)) = 7#u64 + aff : ∀ c < nc, (L.mem s (affA (idleTA ib c))).val = c + tsc : ∀ c < nc, L.mem s (scA (idleTA ib c)) = idleSA ib c + vcpu : ∀ c < nc, L.mem s (wa (idleTA ib c) 3664#u64) = 0#u64 + runV : ∀ c < nc, L.mem s (runVcpuA (idleTA ib c)) = 0#u64 + null : ∀ c < nc, ∀ i < tcbSlotsA, capAtA (L.mem s) (slotAddrA (specPtr (idleTA ib c)) (BitVec.ofNat 64 i)) = .Null + rev : ∀ c < nc, ∀ i < tcbSlotsA, revA (L.mem s) (slotAddrA (specPtr (idleTA ib c)) (BitVec.ofNat 64 i)) = false + scTcb : ∀ c < nc, L.mem s (wa (idleSA ib c) 24#u64) = idleTA ib c + /-- (gen 3) The IRQ state words are `IRQ_INACTIVE` and the IRQ node's slots are null with the revocable bit clear + (the cleared block). -/ + irqSt : ∀ i : U64, i.val < 5376 → L.mem s (irqWordA kb i) = 0#u64 + irqNull : ∀ i : U64, i.val < 5376 → capAtA (L.mem s) (irqSlotA kb i) = .Null + irqRev : ∀ i : U64, i.val < 5376 → revA (L.mem s) (irqSlotA kb i) = false + /-- (gen 3, `SchedRestA`) Core 0's `CONSUMED` = 0 and `REPROGRAM` = 1, `CUR_DOMAIN` = 0, `DOMAIN_TIME` = + `2^56 - 1`, each idle TCB's `PRIO` / `DOMAIN` = 0, each idle SC's `REFILL_MAX` = 2. -/ + cons : L.mem s (L.nodeAt s 0#u64 layout.node.CONSUMED) = 0#u64 + repr : L.mem s (L.nodeAt s 0#u64 layout.node.REPROGRAM) = 1#u64 + curD : L.mem s (wa kb 8#u64) = 0#u64 + domT : L.mem s (wa kb 16#u64) = 72057594037927935#u64 + prio : ∀ c < nc, L.mem s (prioA (idleTA ib c)) = 0#u64 + dom : ∀ c < nc, L.mem s (domA (idleTA ib c)) = 0#u64 + rmax : ∀ c < nc, L.mem s (wa (idleSA ib c) 72#u64) = 2#u64 + /-- (gen 12, `VsRelA`) The ASID table is empty (A64-Boot's cleared block past the nodes). -/ + asid : ∀ hi : U64, hi.val < 128 → L.mem s (atAF kb hi) = 0#u64 + +end + +/-! ## Facts of the boot state -/ + +/-- Every one of an idle TCB's seven slots is a null cap. -/ +theorem idleTcbA_slot (sc : Fl4SpecA64.ObjRef) {i : Nat} (hi : i < tcbSlotsA) : + Fl4SpecA64.tcbCnodeMap (idleTcbA sc) (Fl4SpecA64.tcbCnodeIndex i) = some .nullCap := by + unfold tcbSlotsA at hi + interval_cases i <;> + simp [Fl4SpecA64.tcbCnodeMap, Function.update_apply, tcbIdx_eq, Fl4SpecA64.tcbStage2Slot, + Fl4SpecA64.tcbHighVSpaceSlot, idleTcbA, Fl4SpecA64.defaultTcb, Fl4SpecA64.ExtTcb.default] + +/-- (gen 3) A one-slot CNode's slot is the CNode. -/ +theorem slotAddrA_zero (p : U64) : slotAddrA (specPtr p) (BitVec.ofNat 64 0) = p := by + rw [slotAddrA_sp] + apply UScalar.eq_of_val_eq + show (p.bv + BitVec.ofNat 64 0 * 32#64).toNat = p.bv.toNat + simp + +theorem slotAddrA_nil (p : U64) : slotAddrA (specPtr p) (Fl4SpecA64.ofBl ([] : List Bool)) = p := slotAddrA_zero p + +/-- An empty CNode is well formed. -/ +theorem emptyCnode_wf (n : Nat) : Fl4SpecA64.wellFormedCnodeN n (Fl4SpecA64.emptyCnode n) := by + unfold Fl4SpecA64.wellFormedCnodeN Fl4SpecA64.dom Fl4SpecA64.emptyCnode + ext x; by_cases h : x.length = n <;> simp [h] + +section ks +variable {kb ib now mt : U64} {nc : Nat} (hL : BootLayA ib nc) (hK : BootKsA kb ib nc) +include hK + +/-- An IRQ-node slot by value. -/ +theorem irqN_val {q : U64} (h : IrqNodeA kb q) : ∃ i : U64, i.val < 5376 ∧ q.val = kb.val + 43232 + 32 * i.val := by + obtain ⟨i, hi, rfl⟩ := h + exact ⟨i, hi, irqSlotA_val hK.fit hi⟩ + +theorem irqN_ne_sc {q : U64} (h : IrqNodeA kb q) : specPtr q ≠ Fl4SpecA64.idleScPtr := by + intro e + have : q.val = 18446603336221208576 := specPtr_eq_idleSc.1 e + obtain ⟨i, hi, hv⟩ := irqN_val hK h + have := hK.scOff + have hf : ksSizeA = 788736 := rfl + omega + +include hL + +theorem irqN_ne_TA {q : U64} (h : IrqNodeA kb q) {c : Nat} (hc : c < nc) : specPtr q ≠ specPtr (idleTA ib c) := by + intro e + have e' : q.val = (idleTA ib c).val := congrArg UScalar.val (specPtr_inj e) + rw [idleTA_val hL hc] at e' + obtain ⟨i, hi, hv⟩ := irqN_val hK h + have := hK.apart + have hf : ksSizeA = 788736 := rfl + omega + +theorem irqN_ne_SA {q : U64} (h : IrqNodeA kb q) {c : Nat} (hc : c < nc) : specPtr q ≠ specPtr (idleSA ib c) := by + intro e + have e' : q.val = (idleSA ib c).val := congrArg UScalar.val (specPtr_inj e) + rw [idleSA_val hL hc] at e' + obtain ⟨i, hi, hv⟩ := irqN_val hK h + have := hK.apart + have hf : ksSizeA = 788736 := rfl + omega + +theorem irqN_notT {q : U64} (h : IrqNodeA kb q) : q ∉ bootTA ib nc := by + rintro ⟨c, hc, rfl⟩; exact irqN_ne_TA hL hK h hc rfl + +/-- **Where a boot object lies** (gen 3): an idle TCB, or an IRQ-node slot. -/ +theorem bootTh_loc {p : U64} {k : ObjKindA} (h : bootThA kb ib nc p = some k) : + (∃ c < nc, p = idleTA ib c ∧ p.val = ib.val + 8192 * c ∧ k = .tcb ∧ k.size = 4096) ∨ + (IrqNodeA kb p ∧ ∃ i : U64, i.val < 5376 ∧ p.val = kb.val + 43232 + 32 * i.val ∧ k = .cnode 0 ∧ + k.size = 32) := by + unfold bootThA at h + split_ifs at h with h1 h2 + · cases h + obtain ⟨c, hc, rfl⟩ := h1 + exact Or.inl ⟨c, hc, rfl, idleTA_val hL hc, rfl, rfl⟩ + · cases h + obtain ⟨i, hi, hv⟩ := irqN_val hK h2 + exact Or.inr ⟨h2, i, hi, hv, rfl, rfl⟩ + +theorem bootThA_tcb {p : U64} (h : bootThA kb ib nc p = some .tcb) : p ∈ bootTA ib nc := by + rcases bootTh_loc hL hK h with ⟨c, hc, rfl, -⟩ | ⟨-, -, -, -, e, -⟩ + · exact ⟨c, hc, rfl⟩ + · cases e + +/-- The heap at an IRQ-node slot. -/ +theorem bootKA_irq {q : U64} (h : IrqNodeA kb q) : + bootKA kb ib nc now mt (specPtr q) = some (.cnode 0 (Fl4SpecA64.emptyCnode 0)) := by + unfold bootKA + rw [if_neg (irqN_ne_sc hK h), if_neg (fun ⟨c, hc, e⟩ => irqN_ne_TA hL hK h hc e), + if_neg (fun ⟨c, _, hc, e⟩ => irqN_ne_SA hL hK h hc e), if_pos ⟨q, h, rfl⟩] + +end ks + +section heap +variable {kb ib now mt : U64} {nc : Nat} (hL : BootLayA ib nc) +include hL + +/-- The heap at an idle TCB. -/ +theorem bootKA_tcb {c : Nat} (hc : c < nc) : + bootKA kb ib nc now mt (specPtr (idleTA ib c)) = some (.tcb (idleTcbA (scRenA (bootMA ib nc) (idleSA ib c)))) := by + unfold bootKA + rw [if_neg (idleTA_ne_sc hL hc), if_pos ⟨c, hc, rfl⟩, idleTA_add] + +/-- The heap at `idle_sc_ptr`. -/ +theorem bootKA_sc0 : bootKA kb ib nc now mt Fl4SpecA64.idleScPtr = some (.schedContext (idleScObjA now mt (specPtr (idleTA ib 0))) 0) := by + unfold bootKA; rw [if_pos rfl] + +/-- The heap at another core's idle SC. -/ +theorem bootKA_sc {c : Nat} (h0 : 0 < c) (hc : c < nc) : + bootKA kb ib nc now mt (specPtr (idleSA ib c)) = some (.schedContext (idleScObjA now mt (specPtr (idleTA ib c))) 0) := by + unfold bootKA + rw [if_neg (idleSA_ne_sc hL hc), if_neg, if_pos ⟨c, h0, hc, rfl⟩, idleSA_sub] + rintro ⟨d, hd, e⟩; exact idleSA_ne_TA hL hc hd e + +/-- What the heap holds: idle TCBs, idle SCs and (gen 3) empty one-slot CNodes. -/ +theorem bootKA_cases {p : Fl4SpecA64.ObjRef} {ko : Fl4SpecA64.KernelObject} (h : bootKA kb ib nc now mt p = some ko) : + (∃ c < nc, p = (specPtr (idleTA ib c)) ∧ ∃ sc, ko = .tcb (idleTcbA sc)) ∨ (∃ t, ko = .schedContext (idleScObjA now mt t) 0) ∨ + ko = .cnode 0 (Fl4SpecA64.emptyCnode 0) := by + unfold bootKA at h + split_ifs at h with h1 h2 h3 h4 + · cases h; exact Or.inr (Or.inl ⟨_, rfl⟩) + · cases h; obtain ⟨c, hc, e⟩ := h2; exact Or.inl ⟨c, hc, e, _, rfl⟩ + · cases h; exact Or.inr (Or.inl ⟨_, rfl⟩) + · cases h; exact Or.inr (Or.inr rfl) + +/-- `bootKA_cases` at the boot state. -/ +theorem bootSA_cases {p : Fl4SpecA64.ObjRef} {ko : Fl4SpecA64.KernelObject} + (h : (bootSA kb ib nc now mt).kheap p = some ko) : + (∃ c < nc, p = (specPtr (idleTA ib c)) ∧ ∃ sc, ko = .tcb (idleTcbA sc)) ∨ (∃ t, ko = .schedContext (idleScObjA now mt t) 0) ∨ + ko = .cnode 0 (Fl4SpecA64.emptyCnode 0) := by + rw [bootSA_kheap] at h; exact bootKA_cases hL h + +/-- The affinity of core `c`'s idle TCB is `c`. -/ +theorem bootMA_aff {c : Nat} (hc : c < nc) : (bootMA ib nc).tcbAffinity (specPtr (idleTA ib c)) = some c := by + have h : ∃ d, d < nc ∧ (specPtr (idleTA ib c)) = (specPtr (idleTA ib d)) := ⟨c, hc, rfl⟩ + show (if h : _ then _ else _) = _ + rw [dif_pos h] + obtain ⟨hd, e⟩ := Classical.choose_spec h + exact congrArg some (idleTA_inj hL hd hc e.symm) + +/-- The renamed name of core `c`'s idle SC: `idle_sc_ptr` for core 0, its address otherwise. -/ +theorem bootRen0 : scRenA (bootMA ib nc) (idleSA ib 0) = Fl4SpecA64.idleScPtr := if_pos rfl + +theorem bootRen {c : Nat} (h0 : 0 < c) (hc : c < nc) : scRenA (bootMA ib nc) (idleSA ib c) = (specPtr (idleSA ib c)) := + if_neg fun e => absurd (idleSA_inj hL hc hL.nc0 e) (by omega) + +end heap + +/-! ## The IRQ node at boot (gen 4) -/ + +/-- An IRQ-node slot by value, for any 13-bit IRQ (`irqSlotA_val` beyond FL4's 5376). -/ +theorem irqSlotA_val13 {kb i : U64} (hf : kb.val + ksSizeA ≤ 2 ^ 64) (hi : i.val < 8192) : + (irqSlotA kb i).val = kb.val + 43232 + 32 * i.val := by + unfold irqSlotA + unfold ksSizeA at hf + simp [core.num.U64.wrapping_add_val_eq, core.num.U64.wrapping_mul_val_eq, U64.size_def, U64.numBits] + omega + +/-- A spec IRQ as an FL4 IRQ number. -/ +theorem irqU_val (i : Fl4SpecA64.Irq) : (⟨BitVec.ofNat 64 i.toNat⟩ : U64).val = i.toNat := by + show (BitVec.ofNat 64 i.toNat).toNat = i.toNat + have h := i.isLt + have e : (2 : Nat) ^ Fl4SpecA64.irqLen = 8192 := rfl + rw [e] at h + rw [BitVec.toNat_ofNat] + exact Nat.mod_eq_of_lt (Nat.lt_of_lt_of_le h (by decide)) + +theorem irqU_lt (i : Fl4SpecA64.Irq) : i.toNat < 8192 := by + have h := i.isLt + have e : (2 : Nat) ^ Fl4SpecA64.irqLen = 8192 := rfl + rwa [e] at h + +section irq +variable {kb ib now mt : U64} {nc : Nat} (hL : BootLayA ib nc) (hK : BootKsA kb ib nc) +include hL hK + +/-- The boot IRQ node by value (gen 12: of the FL4 pointer it names). -/ +theorem irqNodeOfA_val (i : Fl4SpecA64.Irq) : (fl4Ptr (irqNodeOfA kb i)).val = kb.val + 43232 + 32 * i.toNat := by + unfold irqNodeOfA; rw [fl4Ptr_specPtr] + have := irqSlotA_val13 (i := ⟨BitVec.ofNat 64 i.toNat⟩) hK.fit (by rw [irqU_val]; exact irqU_lt i) + rw [irqU_val] at this + exact this + +/-- Every cap of the boot heap is null. -/ +theorem bootKA_null {p : Fl4SpecA64.ObjRef} {o : Fl4SpecA64.KernelObject} {b : List Bool} {a : Fl4SpecA64.Cap} + (h : bootKA kb ib nc now mt p = some o) (hb : Fl4SpecA64.capOf o b = some a) : a = .nullCap := by + rcases bootKA_cases hL h with ⟨_, _, _, sc, rfl⟩ | ⟨t, rfl⟩ | rfl + · simp only [Fl4SpecA64.capOf, Fl4SpecA64.tcbCnodeMap, Function.update_apply] at hb + split_ifs at hb <;> (cases hb <;> simp [idleTcbA, Fl4SpecA64.defaultTcb, Fl4SpecA64.ExtTcb.default]) + · simp [Fl4SpecA64.capOf] at hb + · simp only [Fl4SpecA64.capOf, Fl4SpecA64.emptyCnode] at hb + split_ifs at hb <;> cases hb <;> rfl + +/-- (gen 4) **`SpecIrqA` at boot**: 5376 empty one-slot CNodes at FL4's IRQ node, none above, every cap null. -/ +theorem boot_specIrq : SpecIrqA (bootSA kb ib nc now mt) where + node := by + refine ⟨⟨fun i j hi hj e => ?_, fun irq hi => ?_⟩, fun irq hi n hc => ?_⟩ + · have e' : (fl4Ptr (irqNodeOfA kb i)).val = (fl4Ptr (irqNodeOfA kb j)).val := + congrArg (fun x => (fl4Ptr x).val) e + rw [irqNodeOfA_val hL hK, irqNodeOfA_val hL hK] at e' + exact BitVec.eq_of_toNat_eq (by omega) + · refine ⟨_, bootKA_irq hL hK ⟨⟨BitVec.ofNat 64 irq.toNat⟩, by rw [irqU_val]; exact hi, rfl⟩, emptyCnode_wf 0⟩ + · obtain ⟨cs, e, -⟩ := hc + change bootKA kb ib nc now mt (irqNodeOfA kb irq) = _ at e + unfold bootKA at e + split_ifs at e with h1 h2 h3 h4 + · cases e + · cases e + · cases e + · obtain ⟨q, ⟨j, hj, rfl⟩, hq⟩ := h4 + have e' : (fl4Ptr (irqNodeOfA kb irq)).val = (irqSlotA kb j).val := by rw [hq, fl4Ptr_specPtr] + rw [irqNodeOfA_val hL hK, irqSlotA_val hK.fit hj] at e' + have : Fl4SpecA64.fl4IrqCount = 5376 := rfl + omega + slot irq hi o b a ho hb := by + have e2 : bootKA kb ib nc now mt (irqNodeOfA kb irq) = some (.cnode 0 (Fl4SpecA64.emptyCnode 0)) := + bootKA_irq hL hK ⟨⟨BitVec.ofNat 64 irq.toNat⟩, by rw [irqU_val]; exact hi, rfl⟩ + change bootKA kb ib nc now mt (irqNodeOfA kb irq) = some o at ho + rw [e2] at ho + cases ho + exact Or.inl (bootKA_null hL hK e2 hb) + ref p o b a ho hb := by + rw [bootKA_null hL hK ho hb] + rintro ⟨_, _, _, _, e⟩ + cases e + +end irq + +/-! ## The witness -/ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {kb ib now mt : U64} {nc : Nat} {s : S} + +/-- The boot slots are null: the idle TCBs' and (gen 3) the IRQ node's. -/ +theorem boot_null (hL : BootLayA ib nc) (hW : BootWordsA st s kb ib now nc) {y : U64} + (hy : IsSlotA (bootThA kb ib nc) y) : capAtA (L.mem s) y = .Null := by + obtain ⟨p, ⟨r, hr, i, hi, rfl⟩ | ⟨ht, i, hi, rfl⟩⟩ := hy + · rcases bootTh_loc hL hW.ks hr with ⟨-, -, -, -, e, -⟩ | ⟨hn, -, -, -, e, -⟩ + · cases e + · cases e + obtain rfl : i = 0 := by simpa using hi + rw [slotAddrA_zero] + obtain ⟨j, hj, rfl⟩ := hn + exact hW.irqNull j hj + · obtain ⟨c, hc, rfl⟩ := bootThA_tcb hL hW.ks ht + exact hW.null c hc i hi + +/-- **`StateRelA` at boot.** -/ +theorem boot_stateRel (hL : BootLayA ib nc) (hW : BootWordsA st s kb ib now nc) : + StateRelA (bootThA kb ib nc) (L.mem s) (bootSA kb ib nc now mt) where + cnode p r h := by + rcases bootTh_loc hL hW.ks h with ⟨-, -, -, -, e, -⟩ | ⟨hn, -, -, -, e, -⟩ + · cases e + · cases e + refine ⟨_, bootKA_irq hL hW.ks hn, emptyCnode_wf 0, fun idx hl => ?_⟩ + obtain rfl : idx = [] := List.eq_nil_of_length_eq_zero hl + refine ⟨.nullCap, by simp [Fl4SpecA64.emptyCnode], ?_⟩ + rw [slotAddrA_nil] + obtain ⟨j, hj, rfl⟩ := hn + rw [hW.irqNull j hj]; exact CapRelA.null + tcb t h := by + obtain ⟨c, hc, rfl⟩ := bootThA_tcb hL hW.ks h + refine ⟨_, bootKA_tcb hL hc, fun i hi => ⟨.nullCap, idleTcbA_slot _ hi, ?_⟩⟩ + rw [hW.null c hc i hi]; exact CapRelA.null + +/-- **`CdtRelA` at boot** (the empty ghost). -/ +theorem boot_cdtRel (hL : BootLayA ib nc) (hW : BootWordsA st s kb ib now nc) : + CdtRelA st (bootThA kb ib nc) s (bootSA kb ib nc now mt) ⟨[], fun _ => none⟩ := by + refine CdtRelA.empty (fun y hy => boot_null hL hW hy) (fun _ => rfl) ?_ + rintro ⟨q, idx⟩ ⟨p, e1, hcn | ⟨ht, i, hi, e2⟩⟩ + · rcases bootTh_loc hL hW.ks hcn with ⟨-, -, -, -, e, -⟩ | ⟨hn, -, -, -, e, -⟩ + · cases e + · have hl : idx.length = 0 := ObjKindA.cnode.inj e + obtain rfl : idx = [] := List.eq_nil_of_length_eq_zero hl + simp only at e1 + subst e1 + show false = revA (L.mem s) (slotAddrA (specPtr p) (Fl4SpecA64.ofBl [])) + rw [slotAddrA_nil] + obtain ⟨j, hj, rfl⟩ := hn + rw [hW.irqRev j hj] + · obtain ⟨c, hc, rfl⟩ := bootThA_tcb hL hW.ks ht + simp only at e1 e2 + subst e1 e2 + show false = revA (L.mem s) (slotAddrA (specPtr (idleTA ib c)) (Fl4SpecA64.ofBl (Fl4SpecA64.tcbCnodeIndex i))) + rw [ofBl_tcbIdx i (by unfold tcbSlotsA at hi; omega), hW.rev c hc i hi] + +/-- The current core's node words at boot. -/ +theorem boot_node0 (hW : BootWordsA st s kb ib now nc) (off : U64) : + L.nodeAt s (L.cpu s) off = L.nodeAt s 0#u64 off := by + rw [hW.cpu] + +/-- The boot ghost's current SC of core `c`: its idle SC, renamed. -/ +theorem bootZA_curSc (c : Nat) : (bootZA kb ib nc).curScOf c = scRenA (bootMA ib nc) (idleSA ib c) := by + simp only [bootZA] + +/-- **`SchedRelA` at boot.** -/ +theorem boot_schedRel (hL : BootLayA ib nc) (hW : BootWordsA st s kb ib now nc) : + SchedRelA (L := L) (bootTA ib nc) (bootCA ib nc) {c | c.val < nc} (bootZA kb ib nc).curOf (bootZA kb ib nc).curScOf s + (bootSA kb ib nc now mt) (bootMA ib nc) where + core := by rw [hW.cpu]; rfl + cpuCs := by rw [hW.cpu]; exact hL.nc0 + curOfW c hc := by + show specPtr (idleTA ib c.val) = _ + rw [hW.curT c hc] + curScOfW c hc := by rw [bootZA_curSc, hW.curS c hc] + cur := rfl + csc := by rw [bootZA_curSc]; exact bootRen0 hL + idle := by + show specPtr (idleTA ib 0) = specPtr (L.mem s (L.nodeAt s (L.cpu s) layout.node.IDLE_THREAD)) + rw [boot_node0 hW, hW.idleT 0#u64 hL.nc0]; rfl + idleOf c hc := by rw [hW.idleT c hc]; rfl + idleScOf c hc := by rw [hW.idleS c hc]; rfl + act := by + show Fl4SpecA64.SchedulerAction.resumeCurThread = actOfA (L.mem s (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION)) + rw [boot_node0 hW, hW.act]; rfl + time := by + show now.bv = (L.mem s (L.nodeAt s (L.cpu s) layout.node.CUR_TIME)).bv + rw [boot_node0 hW, hW.time] + aff t ht := by + obtain ⟨c, hc, rfl⟩ := ht + rw [bootMA_aff hL hc, hW.aff c hc] + affT p c h := by + simp only [bootMA] at h + split_ifs at h with hp + obtain ⟨d, hd, e⟩ := hp + exact ⟨idleTA ib d, ⟨d, hd, rfl⟩, e.symm⟩ + tcb t ht := by + obtain ⟨c, hc, rfl⟩ := ht + refine ⟨_, bootKA_tcb hL hc, by rw [hW.tst c hc]; rfl, ?_⟩ + rw [hW.tsc c hc] + show some _ = (if idleSA ib c = 0#u64 then none else some _) + rw [if_neg (idleSA_ne0 hL hc)] + sc x hx := by + obtain ⟨c, hc, rfl⟩ := hx + rw [hW.scTcb c hc] + have hz : optRefA (idleTA ib c) = some (specPtr (idleTA ib c)) := if_neg (idleTA_ne0 hL hc) + rw [bootSA_kheap, hz] + rcases Nat.eq_zero_or_pos c with h0 | h0 + · have e0 : scRenA (bootMA ib nc) (idleSA ib c) = Fl4SpecA64.idleScPtr := by rw [h0]; exact bootRen0 hL + rw [e0] + refine ⟨_, _, bootKA_sc0 hL, ?_⟩ + rw [idleScObjA_tcb, h0] + · rw [bootRen hL h0 hc] + exact ⟨_, _, bootKA_sc hL h0 hc, idleScObjA_tcb _⟩ + scOff x hx _ := by + obtain ⟨c, hc, rfl⟩ := hx + exact idleSA_ne_sc hL hc + +/-- **`PlaceA` at boot** (gen 3: with the IRQ node's CNodes inside the block). -/ +theorem boot_place (hL : BootLayA ib nc) (hW : BootWordsA st s kb ib now nc) : + PlaceA (bootThA kb ib nc) (L.ksb s) := by + rw [hW.ksb] + have hK := hW.ks + have hf : ksSizeA = 788736 := rfl + have hA := hK.apart + have hF := hK.fit + have hal := hK.al + have hl1 := hL.al + have hl2 := hL.fit + refine ⟨fun p k h => ?_, fun p k h => ?_, fun p q kp kq hp hq hne => ?_, fun p k h hn => ?_, fun i hi => ?_, + fun p k h => ?_⟩ + · rcases bootTh_loc hL hK h with ⟨c, hc, -, hv, -, hs⟩ | ⟨-, i, hi, hv, -, hs⟩ <;> rw [hs, hv] <;> omega + · rcases bootTh_loc hL hK h with ⟨c, hc, -, hv, -, hs⟩ | ⟨-, i, hi, hv, -, hs⟩ <;> rw [hs, hv] <;> omega + · have hv : p.val ≠ q.val := fun e => hne (UScalar.eq_of_val_eq e) + rcases bootTh_loc hL hK hp with ⟨c, hc, -, h1, -, s1⟩ | ⟨-, i, hi, h1, -, s1⟩ <;> + rcases bootTh_loc hL hK hq with ⟨d, hd, -, h2, -, s2⟩ | ⟨-, j, hj, h2, -, s2⟩ <;> + rw [s1, s2] <;> omega + · rcases bootTh_loc hL hK h with ⟨c, hc, -, hv, -, hs⟩ | ⟨hn', -⟩ + · rw [hs, hv]; omega + · exact absurd hn' hn + · unfold bootThA + rw [if_neg (irqN_notT hL hK ⟨i, hi, rfl⟩), if_pos ⟨i, hi, rfl⟩] + · rcases bootTh_loc hL hK h with ⟨c, hc, rfl, -⟩ | ⟨-, i, hi, hv, -⟩ + · exact idleTA_ne0 hL hc + · intro e + have h0 : (0#u64 : U64).val = 0 := rfl + rw [e, h0] at hv + omega + +/-- (gen 3) **`IrqRelA` at boot**: every IRQ inactive, the spec's IRQ node FL4's. -/ +theorem boot_irqRel (hW : BootWordsA st s kb ib now nc) : IrqRelA (L.mem s) (L.ksb s) (bootSA kb ib nc now mt) where + st i hi := by + rw [hW.ksb, hW.irqSt i hi] + exact ((irqStOfA_inactive _).2 rfl).symm + node i hi := by + rw [hW.ksb] + show specPtr (irqSlotA kb ⟨BitVec.ofNat 64 (BitVec.ofNat 13 i.val).toNat⟩) = specPtr (irqSlotA kb i) + have e : (⟨BitVec.ofNat 64 (BitVec.ofNat 13 i.val).toNat⟩ : U64) = i := by + apply UScalar.eq_of_val_eq + show (BitVec.ofNat 64 (BitVec.ofNat 13 i.val).toNat).toNat = i.val + simp only [BitVec.toNat_ofNat] + rw [Nat.mod_eq_of_lt (by omega : i.val < 2 ^ 13), Nat.mod_eq_of_lt (by omega : i.val < 2 ^ 64)] + rw [e] + nodeHi i _ := by + rw [hW.ksb] + rfl + +/-- (gen 3) **`SchedRestA` at boot**: empty queues, one domain, FL4's boot words. -/ +theorem boot_rest (hL : BootLayA ib nc) (hW : BootWordsA st s kb ib now nc) : + SchedRestA (L := L) (bootZA kb ib nc) s (bootSA kb ib nc now mt) (bootMA ib nc) where + rq _ _ := rfl + rqDom _ _ _ := rfl + rel := rfl + consumed := by + show (0 : BitVec 64) = _ + rw [boot_node0 hW, hW.cons]; rfl + repr := by + show true = _ + rw [boot_node0 hW, hW.repr]; decide + curDom := ⟨rfl, by rw [hW.ksb]; exact hW.curD⟩ + domTime := by + show 72057594037927935#64 = _ + rw [hW.ksb, hW.domT]; rfl + prio t ht := by + obtain ⟨c, hc, rfl⟩ := ht + exact ⟨_, bootKA_tcb hL hc, rfl, hW.dom c hc, by rw [hW.prio c hc]; rfl⟩ + scMem t ht _ := by + obtain ⟨c, hc, rfl⟩ := ht + rw [hW.tsc c hc]; exact ⟨c, hc, rfl⟩ + refillMax x hx := by + obtain ⟨c, hc, rfl⟩ := hx + rw [hW.rmax c hc, bootSA_kheap] + rcases Nat.eq_zero_or_pos c with h0 | h0 + · have e0 : scRenA (bootMA ib nc) (idleSA ib c) = Fl4SpecA64.idleScPtr := by rw [h0]; exact bootRen0 hL + rw [e0] + exact ⟨_, _, bootKA_sc0 (kb := kb) hL, rfl⟩ + · rw [bootRen hL h0 hc] + exact ⟨_, _, bootKA_sc (kb := kb) hL h0 hc, rfl⟩ + +/-- (gen 12; VS gen 1's `boot_vsRelA`, generalised to any memory with an empty ASID table) **`VsRelA` at boot**: no +pool, table or window; the spec's ASID table is `init_arch_state`'s empty one. -/ +theorem boot_vsRelA_of {mem : U64 → U64} (hz : ∀ hi : U64, hi.val < 128 → mem (atAF kb hi) = 0#u64) + (Lv : U64 → Nat) : VsRelA ∅ ∅ Lv ∅ ∅ mem kb (bootSA kb ib nc now mt) where + table hi hh := by + show (none : Option Fl4SpecA64.ObjRef) = _ + rw [hz hi hh] + unfold optPtrA; rw [if_pos rfl] + tmem hi hh h0 := absurd (hz hi hh) h0 + asid0 h0 := absurd (hz (0#u64) (by decide)) h0 + pool p hp := by simp at hp + kind p hp := by simp at hp + pt p hp := by simp at hp + high y hy := by simp at hy + lv p hp := by simp at hp + al p hp := by simp at hp + pal p hp := by simp at hp + yal y hy := by simp at hy + ytb y hy := by simp at hy + +/-- **`JAtZA` at boot.** -/ +theorem boot_jAtZA (AR : A → HwRootA → Prop) {a : A} (hL : BootLayA ib nc) + (hW : BootWordsA st s kb ib now nc) (hAR : AR a (bootZA kb ib nc).hw) : + JAtZA (L := L) AR (bootZA kb ib nc) s a (bootSA kb ib nc now mt) (bootMA ib nc) where + st := boot_stateRel hL hW + cdt := boot_cdtRel hL hW + sch := boot_schedRel hL hW + ar := hAR + place := boot_place hL hW + irq := boot_irqRel hW + rest := boot_rest hL hW + thT t := ⟨fun h => bootThA_tcb hL hW.ks h, fun h => if_pos h⟩ + csN := ⟨nc, hL.nc0, hL.nc128, fun _ => Iff.rfl⟩ + idleT c hc := by rw [hW.idleT c hc]; exact ⟨_, hc, rfl⟩ + idleC c hc := by rw [hW.idleS c hc]; exact ⟨_, hc, rfl⟩ + curT c hc := by rw [hW.curT c hc]; exact ⟨_, hc, rfl⟩ + cscC c hc := by rw [hW.curS c hc]; exact ⟨_, hc, rfl⟩ + ksFit := by rw [hW.ksb]; exact hW.ks.fit + scOffD p hp _ := by + rcases hp with hp | hp | hp | hp + · obtain ⟨k, hk⟩ : ∃ k, bootThA kb ib nc p = some k := by + cases e : bootThA kb ib nc p with + | none => exact absurd e hp + | some k => exact ⟨k, rfl⟩ + rcases bootTh_loc hL hW.ks hk with ⟨c, hc, rfl, -⟩ | ⟨hn, -⟩ + · exact idleTA_ne_sc hL hc + · exact irqN_ne_sc hW.ks hn + all_goals exact absurd hp (Set.notMem_empty _) + cv y hy := by rw [boot_null hL hW hy]; trivial + pml y hy _ _ _ e := by rw [boot_null hL hW hy] at e; cases e + vs := by rw [hW.ksb]; exact boot_vsRelA_of (fun hi hh => hW.asid hi hh) _ + ext := ExtVsRelA.empty _ _ _ _ _ + +/-- **The extension records at boot** (no VCPU, Stage-2 root or high window; every slot null). -/ +theorem boot_ext (hL : BootLayA ib nc) (hW : BootWordsA st s kb ib now nc) : + VcpuCoreA (L := L) (bootZA kb ib nc) s (bootSA kb ib nc now mt) ∧ Stage2CoreA (L := L) (bootZA kb ib nc) s (bootSA kb ib nc now mt) ∧ + HighVSpaceCoreA (L := L) (bootZA kb ib nc) s (bootSA kb ib nc now mt) := by + refine ⟨⟨fun _ h => absurd h (Set.notMem_empty _), fun p x h => ?_, fun t ht => ?_, + fun y hy p e => by rw [boot_null hL hW hy] at e; cases e⟩, + ⟨fun _ h => absurd h (Set.notMem_empty _), fun p es h => ?_, + fun y hy p l mp asid va e => by rw [boot_null hL hW hy] at e; cases e⟩, + ⟨fun _ h => absurd h (Set.notMem_empty _), fun p l1 h => ?_, + fun y hy p mp asid e => by rw [boot_null hL hW hy] at e; cases e⟩⟩ + · rcases bootSA_cases hL h with ⟨_, _, _, _, e⟩ | ⟨_, e⟩ | e <;> cases e + · obtain ⟨c, hc, rfl⟩ := ht + refine ⟨_, bootKA_tcb hL hc, ?_, ?_⟩ + · show none = optRefA _; rw [hW.vcpu c hc]; rfl + · show none = optRefA _; rw [hW.runV c hc]; rfl + · rcases bootSA_cases hL h with ⟨_, _, _, _, e⟩ | ⟨_, e⟩ | e <;> cases e + · rcases bootSA_cases hL h with ⟨_, _, _, _, e⟩ | ⟨_, e⟩ | e <;> cases e + +/-- **`SInvXA` at boot**, at `nc` cores (gen 4: with the IRQ node, given the block's layout `hK`). -/ +theorem boot_sinvXA (hL : BootLayA ib nc) (hK : BootKsA kb ib nc) : + SInvXA (bootSA kb ib nc now mt) (bootMA ib nc) nc where + mc := ⟨hL.nc0, fun c d hc hd e => idleTA_inj hL hc hd e, fun c d hc hd e => idleSA_inj hL hc hd e, + fun t c h => by + simp only [bootMA] at h + split_ifs at h with hp + cases h; exact (Classical.choose_spec hp).1⟩ + view := rfl + curTcb := ⟨_, bootKA_tcb hL hL.nc0⟩ + idleTcb := ⟨_, bootKA_tcb hL hL.nc0, rfl⟩ + onlyIdle t x h _ := by + rcases bootSA_cases hL h with ⟨c, hc, e, -⟩ | ⟨_, e⟩ | e + · exact ⟨c, hc, e⟩ + · cases e + · cases e + idleSc := ⟨_, _, _, bootKA_tcb hL hL.nc0, by rw [bootRen0 hL]; rfl, bootKA_sc0 (kb := kb) hL, rfl⟩ + curSc := ⟨_, _, bootKA_sc0 (kb := kb) hL⟩ + ctQ _ _ _ h := by cases h + idleQ _ _ _ _ h := by cases h + rq _ _ _ h := by cases h + rls _ h := by cases h + rpos p y k h _ := by + rcases bootSA_cases hL h with ⟨_, _, _, _, e⟩ | ⟨t, e⟩ | e + · cases e + · cases e; simp [idleScObjA] + · cases e + zc _ _ _ _ h := by cases h + rlsRun _ h := by cases h + curScReady hc := absurd rfl hc + faultSt t x h hf := by + rcases bootSA_cases hL h with ⟨c, hc, -, sc, e⟩ | ⟨_, e⟩ | e <;> cases e + exact (hf rfl).elim + irqN := boot_specIrq hL hK + +/-- **The early non-vacuity witness** (module docstring): at FL4's boot words `hW` [J] and layout `hL` [J], with +the arch relation at boot `hAR` [P], `JAtZAX ∧ SInvXA` hold jointly at `nc` cores. -/ +theorem jbootA (AR : A → HwRootA → Prop) {a : A} (hL : BootLayA ib nc) + (hW : BootWordsA st s kb ib now nc) (hAR : AR a (bootZA kb ib nc).hw) : + JAtZAX (L := L) AR (bootZA kb ib nc) s a (bootSA kb ib nc now mt) (bootMA ib nc) ∧ + SInvXA (bootSA kb ib nc now mt) (bootMA ib nc) nc := + ⟨⟨boot_jAtZA AR hL hW hAR, boot_ext hL hW⟩, boot_sinvXA hL hW.ks⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/JBootW0A.lean b/proofs/Fl4ProofsA64/P3/JBootW0A.lean new file mode 100644 index 000000000..82e12d6c5 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/JBootW0A.lean @@ -0,0 +1,297 @@ +/- +P3 (aarch64, A64-P3 S3 gen 2, S4 step 1): **A64-Boot's `w0` plugged into the boot witness `jbootA`**. + +A64-Boot's `w0` (`P1/BootWit`, merged from `par/a64-boot` `951b366d8`) is the kernel state after the steps of boot +that build the idle threads: the store `bst` / `bL` and the state `bootS kb ib nc r now mt` (memory `bootM`, core 0), +under the layout `BootLay kb ib nc`. This module proves + +* **`bootLayA_of`**: `BootLay kb ib nc` and the idle area's distance from the FL4 pointer of l4v's `idle_sc_ptr` (`hsc`; gen 12: + `fl4Ptr idle_sc_ptr = 0xffff_8000_0000_3000`, was `idle_sc_ptr` itself) give + `BootLayA ib nc` (`BootLay` has no `scOff`: it is about FL4's own constants, `hsc` about l4v's); +* **`bootWordsA_w0`**: `BootWordsA bst (bootS kb ib nc r now mt) ib now nc`, every field read from `bootM` (the + node words `nodeW`, the idle TCB words `tcbW`, the idle SC words `scW`: `nodeM`, `tcbM`, `tcbV`, `scM`); +* **`jbootA_w0`**: `JAtZAX ∧ SInvXA` at `w0` against `bootSA kb ib nc now mt`, for any `kb`, `ib`, `nc`, `r`, `now`, `mt` + with `BootLay` and `hsc`; +* **`jbootA_w0_ex`**: the same at A64-Boot's concrete `boot_witness` layout (`kb = PPTR_BASE + 2^30`, `ib = PPTR_BASE + 2^31`, 128 cores), + where `hsc` is closed; the only premise left is the arch relation at boot; +* the P3 boot ghosts are P1's at `w0`: **`bootTA_eq`** (the TCBs are `bootH`'s), **`bootCA_eq`** (gen 3: the SCs + are `bootH`'s, `JEntA.scs`) and **`bootOS_eq`** (gen 6: the slots of `bootThA` are A64 P1's `bootOS kb ib nc`, + which since A64-Boot gen 6 holds the IRQ-node slots too). +* gen 3: `BootWordsA`'s IRQ fields at `w0` (the cleared block: `ksW_zero`) and the block's layout `bootKsA_of`, with + the block's distance from l4v's `idle_sc_ptr` (`hsk`, closed at the concrete layout like `hsc`). + +Named premises: `hAR : AR a bootHwA` **[P]** (the arch relation at boot; as `jbootA`). `jbootA`'s [J] premises +`BootWordsA` / `BootLayA` are discharged at `w0`; `hsc` is a hypothesis of the generic form and closed at the +concrete layout. No new [J] / [W]. + +`w1` (post-`create_root_task`, `P1/BootW1`) is not plugged in: its root CNode, untypeds and VSpace objects are not in +`bootSA` (S4's CNode / untyped / VSpace rows). +-/ +import Fl4ProofsA64.P3.JBootA +import Fl4ProofsA64.P3.BootAddrA +import Fl4ProofsA64.P1.BootWit +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P1.W7.R.ST.BootWit + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {kb ib r now mt : U64} {nc : Nat} + +/-- **`BootLay` gives `BootLayA`** (module docstring), with the idle area off l4v's `idle_sc_ptr`. -/ +theorem bootLayA_of (P : BootLay kb ib nc) (hsc : 18446603336221208576 < ib.val ∨ ib.val + nc * 8192 ≤ 18446603336221208576) : + BootLayA ib nc := + ⟨P.ib4k, P.ib0, P.nc0, P.nc128, P.ibw, hsc⟩ + +theorem idleTA_bwv (hL : BootLayA ib nc) {c : Nat} (hc : c < nc) : bwv (ib.val + c * 8192) = idleTA ib c := by + have hf := hL.fit + apply UScalar.eq_of_val_eq + rw [bwv_val (by omega), idleTA_val hL hc]; omega + +theorem idleSA_bwv (hL : BootLayA ib nc) {c : Nat} (hc : c < nc) : + bwv (ib.val + c * 8192 + 4096) = idleSA ib c := by + have hf := hL.fit + apply UScalar.eq_of_val_eq + rw [bwv_val (by omega), idleSA_val hL hc]; omega + +theorem idleTA_val' (hL : BootLayA ib nc) {c : Nat} (hc : c < nc) : (idleTA ib c).val = ib.val + c * 8192 := by + rw [idleTA_val hL hc]; omega + +theorem idleSA_val' (hL : BootLayA ib nc) {c : Nat} (hc : c < nc) : + (idleSA ib c).val = ib.val + c * 8192 + 4096 := by + rw [idleSA_val hL hc]; omega + +/-- A slot address by value. -/ +theorem slotAddrA_val {p : U64} {i : Nat} (h : p.val + i * 32 < 2 ^ 64) : + (slotAddrA (specPtr p) (BitVec.ofNat 64 i)).val = p.val + i * 32 := by + rw [slotAddrA_sp] + show (p.bv + BitVec.ofNat 64 i * 32#64).toNat = _ + have e : p.bv.toNat = p.val := rfl + simp only [BitVec.toNat_add, BitVec.toNat_mul, BitVec.toNat_ofNat, e] + have hi : i % 2 ^ 64 = i := Nat.mod_eq_of_lt (by omega) + rw [hi] + omega + +theorem w0_nodeT {c o : Nat} (ho : o = 0 ∨ o = 8) : nodeW ib c now o = bwv (ib.val + c * 8192) := by + unfold nodeW; rw [if_pos (by omega)] + +theorem w0_nodeS {c o : Nat} (ho : o = 24 ∨ o = 32) : nodeW ib c now o = bwv (ib.val + c * 8192 + 4096) := by + unfold nodeW; rw [if_neg (by omega), if_pos (by omega)] + +theorem w0_csc : layout.node.CUR_SC.val = 24 := by simp [layout.node.CUR_SC] +theorem w0_isc : layout.node.IDLE_SC.val = 32 := by simp [layout.node.IDLE_SC] +theorem w0_ctm : layout.node.CUR_TIME.val = 48 := by simp [layout.node.CUR_TIME] + +/-- (gen 3) A word of the block below the nodes that `ksW` does not set is zero (the IRQ state words and the IRQ +node). -/ +theorem ksW_zero {o : Nat} (h1 : o < 223488) (h2 : 224 ≤ o) : ksW ib nc r now o = 0#u64 := by + unfold ksW + rw [if_neg (by omega), if_neg (by omega), if_neg (by omega), if_neg (by omega), if_neg (by omega)] + +/-- (gen 3) **The block's layout at `w0`**, with its distance from l4v's `idle_sc_ptr` (`hsk`). -/ +theorem bootKsA_of (P : BootLay kb ib nc) (hsk : 18446603336221208576 < kb.val ∨ kb.val + 788736 ≤ 18446603336221208576) : + BootKsA kb ib nc := + ⟨P.kbw, P.apart, by have := P.kb64; omega, hsk⟩ + +/-- **The ASID table at `w0` is zero** (VS gen 1, `P3/VsBootA`; gen 12: here, for `BootWordsA.asid`). -/ +theorem boot_asidW (P : BootLay kb ib nc) {hi : U64} (hh : hi.val < 128) : + bootM kb ib nc r now mt (atAF kb hi) = 0#u64 := by + have hv := atAF_valA (kb := kb) P.kbw hh + have h128 := P.nc128 + rw [bootM_ks P.kbw (o := 772352 + 8 * hi.val) (by omega) (by omega)] + unfold ksW + rw [if_neg (by omega), if_neg (by omega), if_neg (by omega), if_neg (by omega), if_neg (by omega)] + +/-- **The boot words at `w0`** (module docstring). -/ +theorem bootWordsA_w0 (P : BootLay kb ib nc) (hsc : 18446603336221208576 < ib.val ∨ ib.val + nc * 8192 ≤ 18446603336221208576) + (hsk : 18446603336221208576 < kb.val ∨ kb.val + 788736 ≤ 18446603336221208576) : + BootWordsA bst (bootS kb ib nc r now mt) kb ib now nc := by + have hL := bootLayA_of P hsc + have hw := P.ibw + have hn : (0#u64 : U64).val < nc := P.nc0 + refine + { cpu := rfl + ksb := rfl + ks := bootKsA_of P hsk + idleT := fun c hc => (nodeM (r := r) (now := now) (mt := mt) P hc (by simp [layout.node.IDLE_THREAD])).trans (by + rw [idle8, w0_nodeT (Or.inr rfl)]; exact idleTA_bwv hL hc) + idleS := fun c hc => (nodeM (r := r) (now := now) (mt := mt) P hc (by simp [layout.node.IDLE_SC])).trans (by + rw [w0_isc, w0_nodeS (Or.inr rfl)]; exact idleSA_bwv hL hc) + curT := fun c hc => (nodeM (r := r) (now := now) (mt := mt) P hc (by simp [layout.node.CUR_THREAD])).trans (by + rw [curth0, w0_nodeT (Or.inl rfl)]; exact idleTA_bwv hL hc) + curS := fun c hc => (nodeM (r := r) (now := now) (mt := mt) P hc (by simp [layout.node.CUR_SC])).trans (by + rw [w0_csc, w0_nodeS (Or.inl rfl)]; exact idleSA_bwv hL hc) + act := (nodeM (r := r) (now := now) (mt := mt) P hn (by simp [layout.node.SCHED_ACTION])).trans (by + rw [act_val]; exact nodeW_zero ⟨by omega, by omega, by omega, by omega, by omega, by omega, by omega⟩) + time := (nodeM (r := r) (now := now) (mt := mt) P hn (by simp [layout.node.CUR_TIME])).trans + (by rw [w0_ctm]; simp [nodeW]) + tst := fun c hc => + (tcbM (r := r) (now := now) (mt := mt) P hc (idleTA_val' hL hc) (K := layout.tcb.STATE) (by simp [layout.tcb.STATE])).trans + (by simp [tcbW, layout.tcb.STATE]) + aff := fun c hc => by + have e : bootM kb ib nc r now mt (wa (idleTA ib c) 3520#u64) = bwv c := + (tcbM (r := r) (now := now) (mt := mt) P hc (idleTA_val' hL hc) (K := 3520#u64) (by simp)).trans (by simp [tcbW]) + show (bootM kb ib nc r now mt (affA (idleTA ib c))).val = c + rw [show affA (idleTA ib c) = wa (idleTA ib c) 3520#u64 from rfl, e, bwv_val (by omega)] + tsc := fun c hc => ((boot_tcbSc (r := r) (now := now) (mt := mt) P hc (idleTA_val' hL hc)).1).trans (idleSA_bwv hL hc) + vcpu := fun c hc => (tcbM (r := r) (now := now) (mt := mt) P hc (idleTA_val' hL hc) (K := 3664#u64) (by simp)).trans + (tcbW_zero (by simp [TZ])) + runV := fun c hc => (tcbM (r := r) (now := now) (mt := mt) P hc (idleTA_val' hL hc) (K := 3672#u64) (by simp)).trans + (tcbW_zero (by simp [TZ])) + null := fun c hc i hi => by + unfold tcbSlotsA at hi + have ht := idleTA_val' hL hc + refine capA_null ?_ + show bootM kb ib nc r now mt _ = 0#u64 + rw [tcbV (r := r) (now := now) (mt := mt) P hc (o := i * 32) (by omega) + (by rw [slotAddrA_val (by omega), ht])] + exact tcbW_zero ⟨by omega, by omega, by omega, by omega, by omega, by omega, by omega⟩ + rev := fun c hc i hi => by + unfold tcbSlotsA at hi + have ht := idleTA_val' hL hc + have hv := slotAddrA_val (p := idleTA ib c) (i := i) (by omega) + have e : bootM kb ib nc r now mt (wa (slotAddrA (specPtr (idleTA ib c)) (BitVec.ofNat 64 i)) 24#u64) = 0#u64 := by + rw [tcbV (r := r) (now := now) (mt := mt) P hc (o := i * 32 + 24) (by omega) + (by rw [wa_lit (show (24#u64 : U64).val = 24 from rfl) (by omega), hv, ht]; omega)] + exact tcbW_zero ⟨by omega, by omega, by omega, by omega, by omega, by omega, by omega⟩ + show ((bootM kb ib nc r now mt (wa (slotAddrA (specPtr (idleTA ib c)) (BitVec.ofNat 64 i)) 24#u64) &&& + 281474976710656#u64) != 0#u64) = false + rw [e]; first | decide | rfl | simp + scTcb := fun c hc => by + have e : bootM kb ib nc r now mt (wa (idleSA ib c) 24#u64) = bwv (ib.val + c * 8192) := + (scM (r := r) (now := now) (mt := mt) P hc (idleSA_val' hL hc) (K := 24#u64) (by simp)).trans (by simp [scW]) + exact e.trans (idleTA_bwv hL hc) + irqSt := fun i hi => by + have hk := P.kbw + show bootM kb ib nc r now mt _ = 0#u64 + rw [bootM_ks hk (o := 224 + 8 * i.val) (by omega) (by rw [irqWordA_val hk hi]; omega)] + exact ksW_zero (by omega) (by omega) + irqNull := fun i hi => by + have hk := P.kbw + refine capA_null ?_ + show bootM kb ib nc r now mt _ = 0#u64 + rw [bootM_ks hk (o := 43232 + 32 * i.val) (by omega) (by rw [irqSlotA_val hk hi]; omega)] + exact ksW_zero (by omega) (by omega) + irqRev := fun i hi => by + have hk := P.kbw + have hv := irqSlotA_val hk hi + have e : bootM kb ib nc r now mt (wa (irqSlotA kb i) 24#u64) = 0#u64 := by + rw [bootM_ks hk (o := 43232 + 32 * i.val + 24) (by omega) + (by rw [wa_lit (show (24#u64 : U64).val = 24 from rfl) (by omega), hv]; omega)] + exact ksW_zero (by omega) (by omega) + show ((bootM kb ib nc r now mt (wa (irqSlotA kb i) 24#u64) &&& 281474976710656#u64) != 0#u64) = false + rw [e]; first | decide | rfl | simp + cons := (nodeM (r := r) (now := now) (mt := mt) P hn (by simp [layout.node.CONSUMED])).trans (by + rw [show layout.node.CONSUMED.val = 40 by simp [layout.node.CONSUMED]] + exact nodeW_zero ⟨by omega, by omega, by omega, by omega, by omega, by omega, by omega⟩) + repr := (nodeM (r := r) (now := now) (mt := mt) P hn (by simp [layout.node.REPROGRAM])).trans (by + rw [show layout.node.REPROGRAM.val = 56 by simp [layout.node.REPROGRAM]]; simp [nodeW]) + curD := by + have hk := P.kbw + show bootM kb ib nc r now mt _ = _ + rw [bootM_ks P.kbw (o := 8) (by omega) (wa_lit (show (8#u64 : U64).val = 8 from rfl) (by omega))] + unfold ksW + rw [if_neg (by omega), if_neg (by omega), if_neg (by omega), if_neg (by omega), if_neg (by omega)] + domT := by + have hk := P.kbw + show bootM kb ib nc r now mt _ = _ + rw [bootM_ks P.kbw (o := 16) (by omega) (wa_lit (show (16#u64 : U64).val = 16 from rfl) (by omega))] + unfold ksW + rw [if_neg (by omega), if_neg (by omega), if_pos rfl] + prio := fun c hc => + (tcbM (r := r) (now := now) (mt := mt) P hc (idleTA_val' hL hc) (K := 3448#u64) (by simp)).trans + (tcbW_zero (by simp [TZ])) + dom := fun c hc => + (tcbM (r := r) (now := now) (mt := mt) P hc (idleTA_val' hL hc) (K := 3432#u64) (by simp)).trans + (tcbW_zero (by simp [TZ])) + rmax := fun c hc => + (scM (r := r) (now := now) (mt := mt) P hc (idleSA_val' hL hc) (K := 72#u64) (by simp)).trans + (by simp [scW]) + asid := fun hi hh => boot_asidW P hh } + +/-- **`jbootA` at `w0`** (module docstring): `JAtZAX ∧ SInvXA` at A64-Boot's boot state, with the arch relation at +boot `hAR` [P]. -/ +theorem jbootA_w0 {A : Type} (AR : A → HwRootA → Prop) {a : A} (P : BootLay kb ib nc) + (hsc : 18446603336221208576 < ib.val ∨ ib.val + nc * 8192 ≤ 18446603336221208576) + (hsk : 18446603336221208576 < kb.val ∨ kb.val + 788736 ≤ 18446603336221208576) (hAR : AR a bootHwA) : + JAtZAX (L := bL) AR (bootZA kb ib nc) (bootS kb ib nc r now mt) a (bootSA kb ib nc now mt) (bootMA ib nc) ∧ + SInvXA (bootSA kb ib nc now mt) (bootMA ib nc) nc := + jbootA AR (bootLayA_of P hsc) (bootWordsA_w0 P hsc hsk) hAR + +/-- **`jbootA` at A64-Boot's concrete witness** (`kb = PPTR_BASE + 2^30`, `ib = PPTR_BASE + 2^31`, 128 cores; any `r`, `now`, `mt`). -/ +theorem jbootA_w0_ex {A : Type} (AR : A → HwRootA → Prop) {a : A} (hAR : AR a bootHwA) : + JAtZAX (L := bL) AR (bootZA 18446603337294938112#u64 18446603338368679936#u64 128) (bootS 18446603337294938112#u64 18446603338368679936#u64 128 r now mt) + a (bootSA 18446603337294938112#u64 18446603338368679936#u64 128 now mt) (bootMA 18446603338368679936#u64 128) ∧ + SInvXA (bootSA 18446603337294938112#u64 18446603338368679936#u64 128 now mt) (bootMA 18446603338368679936#u64 128) 128 := by + have hi : (18446603338368679936#u64 : U64).val = 18446603338368679936 := by simp + have hk : (18446603337294938112#u64 : U64).val = 18446603337294938112 := by simp + exact jbootA_w0 AR bootLayC_ex (Or.inl (by rw [hi]; omega)) (Or.inl (by rw [hk]; omega)) hAR + +/-! ## The P3 boot ghosts are P1's -/ + +/-- The boot TCBs are `bootH`'s. -/ +theorem bootTA_eq (hL : BootLayA ib nc) : bootTA ib nc = (bootH kb ib nc).tcbs := by + ext t + constructor + · rintro ⟨c, hc, rfl⟩ + exact bootH_tcb ⟨c, hc, idleTA_val' hL hc⟩ + · intro ht + obtain ⟨c, hc, e⟩ := bootH_tcbs ht + exact ⟨c, hc, UScalar.eq_of_val_eq (by rw [e, idleTA_val' hL hc])⟩ + +/-- (gen 3, with `JEntA.scs`) The boot SCs are `bootH`'s. -/ +theorem bootCA_eq (hL : BootLayA ib nc) : bootCA ib nc = (bootH kb ib nc).scs := by + ext x + constructor + · rintro ⟨c, hc, rfl⟩ + refine ⟨8, ?_⟩ + have hT : ¬ IdleT ib nc (idleSA ib c) := by + rintro ⟨d, hd, e⟩; rw [idleSA_val' hL hc] at e; omega + have hS : IdleS ib nc (idleSA ib c) := ⟨c, hc, idleSA_val' hL hc⟩ + simp only [bootH, hT, hS, ↓reduceIte] + · rintro ⟨z, hz⟩ + rcases bootH_some hz with ⟨-, e⟩ | ⟨⟨c, hc, e⟩, -⟩ | ⟨e, -⟩ + · cases e + · exact ⟨c, hc, UScalar.eq_of_val_eq (by rw [e, idleSA_val' hL hc])⟩ + · cases e + +/-- The slots of the boot typed heap are A64 P1's `bootOS kb ib nc` (gen 6, A64-Boot gen 6: `bootOS` now holds the +idle TCBs' slots and the 5376 IRQ-node slots, closing gen 3's q3; `slotAddrA` is `slotIdx`). -/ +theorem bootOS_eq (hL : BootLayA ib nc) (hK : BootKsA kb ib nc) : + {a | IsSlotA (bootThA kb ib nc) a} = bootOS kb ib nc := by + have hw := hL.fit + ext x + constructor + · rintro ⟨p, ⟨r, hr, i, hi, rfl⟩ | ⟨ht, i, hi, rfl⟩⟩ + · rcases bootTh_loc hL hK hr with ⟨-, -, -, -, e, -⟩ | ⟨hn, -, -, -, e, -⟩ + · cases e + · cases e + obtain rfl : i = 0 := by simpa using hi + rw [slotAddrA_zero] + exact Or.inr hn + · obtain ⟨c, hc, rfl⟩ := bootThA_tcb hL hK ht + unfold tcbSlotsA at hi + have ht' := idleTA_val' hL hc + refine Or.inl ⟨idleTA ib c, ⟨c, hc, ht'⟩, bwv i, by rw [bwv_val (by omega)]; exact hi, ?_⟩ + apply UScalar.eq_of_val_eq + rw [slotAddrA_val (by omega), slotIdx_val (by omega) (by rw [bwv_val (by omega)]; exact hi), + bwv_val (by omega)] + · rintro (⟨t, ⟨c, hc, e⟩, i, hi, rfl⟩ | (hn : IrqNodeA kb x)) + · have ht : t = idleTA ib c := UScalar.eq_of_val_eq (by rw [e, idleTA_val' hL hc]) + subst ht + refine ⟨idleTA ib c, Or.inr ⟨if_pos ⟨c, hc, rfl⟩, i.val, by unfold tcbSlotsA; exact hi, ?_⟩⟩ + apply UScalar.eq_of_val_eq + rw [slotAddrA_val (by omega), slotIdx_val (by omega) hi] + · refine ⟨x, Or.inl ⟨0, ?_, 0, by norm_num, (slotAddrA_zero x).symm⟩⟩ + unfold bootThA + rw [if_neg (irqN_notT hL hK hn), if_pos hn] + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/JBootW1A.lean b/proofs/Fl4ProofsA64/P3/JBootW1A.lean new file mode 100644 index 000000000..c4a1e74fb --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/JBootW1A.lean @@ -0,0 +1,625 @@ +/- +P3 (aarch64, A64-P3 S3 gen 13, ruling 15:58Z item 2, first half): **the CSpace and CDT parts of the joint relation +at A64-Boot's post-`create_root_task` witness `w1`** (`P1/BootW1`: the store `bst`, the state `w1S kb ib nc r now +mt sl uw`, memory `w1M`, under `BootLay1`). The x86 counterpart is `JBoot`'s CSpace / CDT part of `w1`. + +* **`w1ThA kb ib nc`** (the typed heap at `w1`): `w0`'s (`bootThA`: the idle TCBs, the IRQ node's one-slot CNodes), + with the root CNode at `RR = PPTR_BASE + 2^40` (`cnode 13`, 8192 slots, `init_root_cnode`), the root TCB at `RT` and the root IPC-buffer frame at `IPb` + (`.other 4096`, the object `IpcSchedPlacementA.buffer` needs). +* **`rootCsA nc`** (the spec root CNode's contents): slot `idx` holds `absCapA (capOf nc (blvA idx))`, A64-Boot's + `capOf` (the fixed slots, the `SchedControl` caps, the three page tables, the 33 frames, the image frame, the RAM + untyped); **`rtCapA i`** (the root TCB's seven caps: `CTABLE` / `VTABLE` / `BUFFER` as `init_root_thread` + writes them, the rest null); **`w1OrigA nc`** (`is_original_cap`: the non-null root CNode slots, whose `NEXT` word + has `REVOCABLE`, `mdbR`). +* **`W1CsA kb ib nc σ`**: what the CSpace / CDT relation reads of the spec state — the root CNode is `cnode 13 + (rootCsA nc)`, the root TCB's caps are `rtCapA`, the idle TCBs' caps are null, the IRQ node's CNodes are `w0`'s, + no `cdt` edge, `is_original_cap = w1OrigA`. The full spec state `w1SA` (gen 14) is to be built from `bootSA` and + proved to satisfy it; this module proves the relation for every such `σ`. +* **`w1OS_eqA`**: the slots of `w1ThA` are A64 P1's `w1OS` (so A64-Boot's `w1_treeA` / `w1_capS` read them). +* **`w1_stateRelA`** (`StateRelA`, from `cap_root` / `cap_rt` / `cap_idle`), **`w1_cdtRelA`** (`CdtRelA` at the + ghost `⟨w1G nc, none⟩`: the tree is A64-Boot's `w1_treeA`, the revocable bits `mdbR` / `rtW`), **`w1_placeA`** + (`PlaceA`: the two new objects above the block and the idle area, `BootLay1`), **`w1_cvA`** (`JAtZA.cv`: the + root CNode cap and the thread cap name `w1ThA`'s objects). +* Cap facts: **`capOf_wfA`** / **`rtCapA_wf`** (`CapWfA` of every root cap; `SpNoOvA` for the frames and the untyped + by **`spNoOvA_of`**: a window region within `2^46` of `PPTR_BASE` does not wrap at the shifted name), + `capOf_validA`, `rtCapA_valid`. + +Hypotheses: `BootLayA ib nc` / `BootKsA kb ib nc` (the w0 layouts, carrying `hsc` / `hsk`, closed at the concrete +layout as at `w0`) and `BootLay1`. Named premises: none. New module; no existing statement changed. + +Note (gen 14, ruling 16:30Z Q4): the spec `cdt` at `w1` is empty (`W1CsA.cdt`), unlike l4v's `init_cdt`, which makes +the root TCB's caps CDT children of the root CNode's: FL4's `boot.rs` (`cdt::insert` with `is_cap_revocable` false) and +A64-Boot's `BootMdb1.w1_treeA` have no parent-child link. + +(gen 15, ruling 16:55Z) A64-Boot gen 9 moved `w1`'s root-task objects into FL4's kernel window (`RR = PPTR_BASE + +2^40`, `P1/BootW1Win`), which resolves gen 14's stop-first finding: the three refutation theorems (`w1_tabE_bad`, +`w1_not_vsRelA`, `w1_not_jAtZA`) are dropped (false at the window addresses), and **`spNoOvA_of`** is restated for +window pointers (`PPTR_BASE ≤ p`, `p + 2^n ≤ PPTR_BASE + 2^46`: `specPtr p = p - PPTR_BASE + 2^39`). The full w1 +witness is `P3/JBootW1SA` / `P3/JEntW1A`. +-/ +import Fl4ProofsA64.P3.JBootW0A +import Fl4ProofsA64.P3.EmptyA +import Fl4ProofsA64.P3.LookupA +import Fl4ProofsA64.P1.BootMdb1 +import Fl4ProofsA64.P1.MdbBootInvA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P1.W7.R.ST.BootWit + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## The typed heap and the spec CSpace contents at `w1` -/ + +open Classical in +/-- **The typed heap at `w1`** (module docstring). -/ +def w1ThA (kb ib : U64) (nc : Nat) : TypedHeapA := fun p => + if p = bwv RRb then some (.cnode 13) else if p = bwv RTb then some .tcb else + if FrPg p.val then some (.other 4096) else bootThA kb ib nc p + +/-- **The spec root CNode's contents** (module docstring). -/ +def rootCsA (nc : Nat) : Fl4SpecA64.CnodeContents := fun idx => + if idx.length = 13 then some (absCapA (capOf nc (blvA idx))) else none + +/-- **The root TCB's seven caps** (A64-Boot's `cap_rt`). -/ +def rtCapA (i : Nat) : capability.Cap := + if i = 0 then .CNode (bwv RRb) 13#u64 51#u64 0#u64 else if i = 1 then .Pml4 (bwv VSb) true 1#u64 else + if i = 2 then .Frame (bwv IPb) .Small 3#u64 false false false 0#u64 (bwv 0) else .Null + +/-- **`is_original_cap` at `w1`**: exactly the non-null root CNode slots (`write_slot` sets `REVOCABLE`). -/ +def w1OrigA (nc : Nat) (x : Fl4SpecA64.CslotPtr) : Bool := + if x.1 = specPtr (bwv RRb) ∧ x.2.length = 13 then decide (cw0 nc (blvA x.2) ≠ 0) else false + +/-- Root TCB slot `i`, as the spec names it. -/ +def w1RtSlotA (i : Nat) : Fl4SpecA64.CslotPtr := (specPtr (bwv RTb), Fl4SpecA64.tcbCnodeIndex i) + +/-- Root CNode slot `j`, as the spec names it. -/ +def w1RrSlotA (j : Nat) : Fl4SpecA64.CslotPtr := (specPtr (bwv RRb), bitsOfA j 13) + +open Classical in +/-- **The spec CDT at `w1`** (R1, Architect 01:46Z): the root TCB's CNode, VSpace and buffer caps are CDT children of +root CNode slots 2, 3 and 10 (l4v's `init_cdt`; A64 P1's `w1Par`). -/ +noncomputable def w1CdtA (sl : Fl4SpecA64.CslotPtr) : Option Fl4SpecA64.CslotPtr := + if sl = w1RtSlotA 0 then some (w1RrSlotA 2) else if sl = w1RtSlotA 1 then some (w1RrSlotA 3) else + if sl = w1RtSlotA 2 then some (w1RrSlotA 10) else none + +/-- **What the CSpace / CDT relation at `w1` reads of the spec state** (module docstring). -/ +structure W1CsA (kb ib : U64) (nc : Nat) (σ : Fl4SpecA64.State Unit) : Prop where + cn : σ.kheap (specPtr (bwv RRb)) = some (.cnode 13 (rootCsA nc)) + rt : ∃ x, σ.kheap (specPtr (bwv RTb)) = some (.tcb x) ∧ + ∀ i < tcbSlotsA, Fl4SpecA64.tcbCnodeMap x (Fl4SpecA64.tcbCnodeIndex i) = some (absCapA (rtCapA i)) + idle : ∀ c < nc, ∃ x, σ.kheap (specPtr (idleTA ib c)) = some (.tcb x) ∧ + ∀ i < tcbSlotsA, Fl4SpecA64.tcbCnodeMap x (Fl4SpecA64.tcbCnodeIndex i) = some .nullCap + irq : ∀ q, IrqNodeA kb q → σ.kheap (specPtr q) = some (.cnode 0 (Fl4SpecA64.emptyCnode 0)) + cdt : ∀ x, σ.cdt x = w1CdtA x + orig : ∀ x, σ.isOriginalCap x = w1OrigA nc x + +theorem rootCsA_wf (nc : Nat) : Fl4SpecA64.wellFormedCnodeN 13 (rootCsA nc) := by + unfold Fl4SpecA64.wellFormedCnodeN Fl4SpecA64.dom rootCsA + ext x; by_cases h : x.length = 13 <;> simp [h] + +/-! ## Cap well-formedness -/ + +/-- **A kernel-window region within `2^46` of `PPTR_BASE` does not wrap at the shifted name** (`SpNoOvA`; gen 15: +for window pointers, `specPtr p = p - PPTR_BASE + 2^39`). -/ +theorem spNoOvA_of {p : U64} {n : Nat} (h0 : PPb ≤ p.val) (h : p.val + 2 ^ n ≤ PPb + 2 ^ 46) : SpNoOvA p n := by + unfold SpNoOvA + have h1 : 1 ≤ 2 ^ n := Nat.one_le_two_pow + have e1 : (1 : BitVec 64).toNat = 1 := rfl + have hp : (specPtr p).toNat = p.val - PPb + 549755813888 := by + rw [specPtr_toNat] + have hb : p.val < 2 ^ 64 := p.bv.isLt + omega + have hs : ((1 : BitVec 64) <<< n).toNat = 2 ^ n := by + rw [BitVec.toNat_shiftLeft, Nat.shiftLeft_eq, e1, one_mul]; exact Nat.mod_eq_of_lt (by omega) + have ha : (specPtr p + (1 : BitVec 64) <<< n).toNat = p.val - PPb + 549755813888 + 2 ^ n := by + rw [BitVec.toNat_add, hp, hs]; exact Nat.mod_eq_of_lt (by omega) + have hb : (specPtr p + (1 : BitVec 64) <<< n - 1).toNat = p.val - PPb + 549755813888 + 2 ^ n - 1 := by + rw [BitVec.toNat_sub, ha, e1]; omega + rw [BitVec.le_def, hb, hp]; omega + +theorem pbSmallA : Fl4SpecA64.pageBitsForSize (frameSizeOfA .Small) = 12 := rfl + +/-- A mapped root frame cap is well formed. -/ +theorem frC_wfA {p v : Nat} (h0 : PPb ≤ p) (hp : p + 4096 ≤ PPb + 2 ^ 46) : CapWfA (frC p v) := by + refine ⟨by decide, by decide, by decide, ?_⟩ + rw [pbSmallA] + exact spNoOvA_of (by rw [bwv_val (n := p) (by omega)]; omega) (by rw [bwv_val (n := p) (by omega)]; omega) + +theorem utC_wfA : CapWfA (.Untyped (bwv UTb) 20#u64 0#u64 false) := by + show SpNoOvA _ (20#u64 : U64).val + rw [show (20#u64 : U64).val = 20 from rfl] + exact spNoOvA_of (by rw [bwv_val (n := UTb) (by norm_num)]; norm_num) + (by rw [bwv_val (n := UTb) (by norm_num)]; norm_num) + +/-- **Every root CNode cap is well formed.** -/ +theorem capOf_wfA (nc i : Nat) : CapWfA (capOf nc i) := by + unfold capOf + by_cases h1 : i < 16 + · rw [slotC_lo h1] + interval_cases i <;> first + | exact trivial + | exact ⟨by decide, by decide, by decide⟩ + | exact frC_wfA (by norm_num) (by norm_num) + | exact (by decide : (1 : Nat) < 2 ^ 16) + | exact (by decide : (0 : Nat) < 2 ^ 16 ∧ (0 : Nat) % 512 = 0) + | exact (by decide : (7 : Nat) ≤ 7) + by_cases h2 : i < 16 + nc + · rw [slotC_sch (by omega) h2]; exact trivial + rw [slotC_hi (by omega)] + generalize i - (16 + nc) = k + by_cases h3 : k < 37 + · interval_cases k <;> first + | exact trivial + | exact utC_wfA + | exact frC_wfA (by norm_num) (by norm_num) + | exact (by decide : (1 : Nat) < 2 ^ 16) + · rw [tailC_big (by omega)]; exact trivial + +theorem rtCapA_wf (i : Nat) : CapWfA (rtCapA i) := by + unfold rtCapA + split_ifs + · exact ⟨by decide, by decide, by decide⟩ + · exact (by decide : (1 : Nat) < 2 ^ 16) + · refine ⟨by decide, by decide, by decide, ?_⟩ + rw [pbSmallA] + exact spNoOvA_of (by rw [bwv_val (n := IPb) (by norm_num)]; norm_num) + (by rw [bwv_val (n := IPb) (by norm_num)]; norm_num) + · exact trivial + +/-- (gen 18) A user address below `USER_TOP` is `VaOkE` (`CapValidA`'s frame arm at the root task's frames). -/ +theorem vaOk_lowA {n : Nat} (h : n ≤ 0x7fffffffffff) : VaOkE (bwv n) := + Or.inl (by rw [bwv_val (by omega)]; exact h) + +/-- **The root caps name `th`'s objects** (`CapValidA`): the root CNode cap and the thread cap; (gen 18) the root +task's mapped frames are at user addresses. -/ +theorem capOf_validA {th : TypedHeapA} (hRR : th (bwv RRb) = some (.cnode 13)) (hRT : th (bwv RTb) = some .tcb) + (nc i : Nat) : CapValidA th (capOf nc i) := by + unfold capOf + by_cases h1 : i < 16 + · rw [slotC_lo h1] + interval_cases i <;> first | exact trivial | exact ⟨hRR, by decide⟩ | exact hRT | exact fun h => absurd h (by decide) | exact fun _ _ => vaOk_lowA (by norm_num) + by_cases h2 : i < 16 + nc + · rw [slotC_sch (by omega) h2]; exact trivial + rw [slotC_hi (by omega)] + generalize i - (16 + nc) = k + by_cases h3 : k < 37 + · interval_cases k <;> first | exact trivial | exact fun h => absurd h (by decide) | exact fun _ _ => vaOk_lowA (by norm_num) + · rw [tailC_big (by omega)]; exact trivial + +theorem rtCapA_valid {th : TypedHeapA} (hRR : th (bwv RRb) = some (.cnode 13)) (i : Nat) : + CapValidA th (rtCapA i) := by + unfold rtCapA + split_ifs + · exact ⟨hRR, by decide⟩ + all_goals first | exact trivial | exact fun h => absurd h (by decide) + +/-! ## The typed heap's objects -/ + +section heap +variable {kb ib : U64} {nc : Nat} + +theorem w1ThA_cases {p : U64} {k : ObjKindA} (h : w1ThA kb ib nc p = some k) : + (p = bwv RRb ∧ k = .cnode 13) ∨ (p = bwv RTb ∧ k = .tcb) ∨ (FrPg p.val ∧ k = .other 4096) ∨ + bootThA kb ib nc p = some k := by + unfold w1ThA at h + split_ifs at h with h1 h2 h3 + · cases h; exact Or.inl ⟨h1, rfl⟩ + · cases h; exact Or.inr (Or.inl ⟨h2, rfl⟩) + · cases h; exact Or.inr (Or.inr (Or.inl ⟨h3, rfl⟩)) + · exact Or.inr (Or.inr (Or.inr h)) + +theorem rt_ne_rrA : bwv RTb ≠ bwv RRb := fun e => by + have := congrArg UScalar.val e; rw [vRT, vRR] at this; omega + +theorem w1ThA_rr : w1ThA kb ib nc (bwv RRb) = some (.cnode 13) := by unfold w1ThA; rw [if_pos rfl] + +theorem w1ThA_rt : w1ThA kb ib nc (bwv RTb) = some .tcb := by + unfold w1ThA; rw [if_neg rt_ne_rrA, if_pos rfl] + +theorem ip_ne_rrA : bwv IPb ≠ bwv RRb := fun e => by + have := congrArg UScalar.val e; rw [vIP, vRR] at this; omega + +theorem ip_ne_rtA : bwv IPb ≠ bwv RTb := fun e => by + have := congrArg UScalar.val e; rw [vIP, vRT] at this; omega + +theorem w1ThA_fr {p : U64} (h : FrPg p.val) : w1ThA kb ib nc p = some (.other 4096) := by + have h' := h + unfold FrPg at h' + unfold w1ThA + rw [if_neg (fun e => by rw [e, vRR] at h'; omega), if_neg (fun e => by rw [e, vRT] at h'; omega), if_pos h] + +theorem w1ThA_ip : w1ThA kb ib nc (bwv IPb) = some (.other 4096) := + w1ThA_fr (by unfold FrPg; exact Or.inr (Or.inl vIP)) + +theorem w1ThA_old {p : U64} (h : p.val < RRb) : w1ThA kb ib nc p = bootThA kb ib nc p := by + unfold w1ThA + rw [if_neg (fun e => by rw [e, vRR] at h; omega), if_neg (fun e => by rw [e, vRT] at h; omega), + if_neg (fun e => by unfold FrPg at e; omega)] + +theorem sp_ne_rrA {p : U64} (h : p.val ≠ RRb) : specPtr p ≠ specPtr (bwv RRb) := fun e => by + have := congrArg UScalar.val (specPtr_inj e); rw [vRR] at this; exact h this + +variable (hL : BootLayA ib nc) (hK : BootKsA kb ib nc) (P : BootLay1 kb ib nc) +include hL hK P + +/-- `w0`'s objects lie below the root-server block. -/ +theorem bootTh_ltA {p : U64} {k : ObjKindA} (h : bootThA kb ib nc p = some k) : + p.val + k.size ≤ RRb ∧ 0 < k.size := by + have h1 := P.ibR; have h2 := P.kbR + rcases bootTh_loc hL hK h with ⟨c, hc, -, hv, -, hs⟩ | ⟨-, i, hi, hv, -, hs⟩ <;> rw [hs] <;> + exact ⟨by omega, by omega⟩ + +/-- **The slots of `w1ThA` are A64 P1's `w1OS`.** -/ +theorem w1OS_eqA : {a | IsSlotA (w1ThA kb ib nc) a} = w1OS kb ib nc := by + ext x + constructor + · rintro ⟨p, ⟨r, hr, i, hi, rfl⟩ | ⟨ht, i, hi, rfl⟩⟩ + · rcases w1ThA_cases hr with ⟨rfl, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | hb + · obtain rfl : r = 13 := ObjKindA.cnode.inj e + norm_num at hi + refine Or.inr ⟨i, hi, ?_⟩ + rw [slotAddrA_valN (by rw [vRR]; omega), vRR] + · cases e + · cases e + · have hm : slotAddrA (specPtr p) (BitVec.ofNat 64 i) ∈ {a | IsSlotA (bootThA kb ib nc) a} := + ⟨p, Or.inl ⟨r, hb, i, hi, rfl⟩⟩ + rw [bootOS_eq hL hK] at hm + exact Or.inl (Or.inl hm) + · rcases w1ThA_cases ht with ⟨-, e⟩ | ⟨rfl, -⟩ | ⟨-, e⟩ | hb + · cases e + · unfold tcbSlotsA at hi + refine Or.inl (Or.inr ⟨bwv i, by rw [bwv_val (n := i) (by omega)]; exact hi, ?_⟩) + apply UScalar.eq_of_val_eq + rw [slotAddrA_val (by rw [vRT]; omega), slotIdx_val (by rw [vRT]; norm_num) + (by rw [bwv_val (n := i) (by omega)]; exact hi), bwv_val (n := i) (by omega), vRT] + · cases e + · have hm : slotAddrA (specPtr p) (BitVec.ofNat 64 i) ∈ {a | IsSlotA (bootThA kb ib nc) a} := + ⟨p, Or.inr ⟨hb, i, hi, rfl⟩⟩ + rw [bootOS_eq hL hK] at hm + exact Or.inl (Or.inl hm) + · rintro ((hb | ⟨i, hi, rfl⟩) | ⟨i, hi, hx⟩) + · have hm : x ∈ {a | IsSlotA (bootThA kb ib nc) a} := by rw [bootOS_eq hL hK]; exact hb + obtain ⟨p, ⟨r, hr, i, hi, rfl⟩ | ⟨ht, i, hi, rfl⟩⟩ := hm + · exact ⟨p, Or.inl ⟨r, by rw [w1ThA_old (by have := bootTh_ltA hL hK P hr; omega)]; exact hr, i, hi, rfl⟩⟩ + · exact ⟨p, Or.inr ⟨by rw [w1ThA_old (by have := bootTh_ltA hL hK P ht; omega)]; exact ht, i, hi, rfl⟩⟩ + · refine ⟨bwv RTb, Or.inr ⟨w1ThA_rt, i.val, hi, ?_⟩⟩ + apply UScalar.eq_of_val_eq + rw [slotAddrA_val (by rw [vRT]; omega), slotIdx_val (by rw [vRT]; norm_num) hi, vRT] + · refine ⟨bwv RRb, Or.inl ⟨13, w1ThA_rr, i, by norm_num; exact hi, ?_⟩⟩ + apply UScalar.eq_of_val_eq + rw [slotAddrA_valN (by rw [vRR]; omega), vRR, hx] + +/-- A slot of `w0`'s objects is a `bootOS` slot. -/ +theorem w1_bootOSA {y : U64} (hy : IsSlotA (bootThA kb ib nc) y) : y ∈ bootOS kb ib nc := by + have hm : y ∈ {a | IsSlotA (bootThA kb ib nc) a} := hy + rwa [bootOS_eq hL hK] at hm + +/-- **`PlaceA` at `w1`.** -/ +theorem w1_placeA : PlaceA (w1ThA kb ib nc) kb := by + have hB : PlaceA (bootThA kb ib nc) kb := + boot_place hL (bootWordsA_w0 (r := 0#u64) (now := 0#u64) (mt := 0#u64) P.w0 hL.scOff hK.scOff) + have s18 : (ObjKindA.cnode 13).size = 262144 := rfl + have s12 : ObjKindA.tcb.size = 4096 := rfl + have sip : (ObjKindA.other 4096).size = 4096 := rfl + have r3 := vIP + have r1 := vRR + have r2 := vRT + have hf : ksSizeA = 788736 := rfl + have k1 := P.kbR + have k2 := hK.fit + refine ⟨fun p k h => ?_, fun p k h => ?_, fun p q kp kq hp hq hne => ?_, fun p k h hn => ?_, fun i hi => ?_, + fun p k h => ?_⟩ + · rcases w1ThA_cases h with ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨hF, rfl⟩ | hb + · rw [s18, r1] + · rw [s12, r2] + · rw [sip]; unfold FrPg at hF; omega + · exact hB.align p k hb + · rcases w1ThA_cases h with ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨hF, rfl⟩ | hb + · rw [s18, r1]; norm_num + · rw [s12, r2]; norm_num + · have eB := Fl4ProofsA64.P0.Consts.layout.PPTR_BASE.val_eq + rw [sip]; unfold FrPg at hF; omega + · exact hB.fit p k hb + · have hne' : p.val ≠ q.val := fun h => hne (UScalar.eq_of_val_eq h) + rcases w1ThA_cases hp with ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨hF, rfl⟩ | hp' <;> + rcases w1ThA_cases hq with ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨hG, rfl⟩ | hq' + all_goals (try unfold FrPg at *) + all_goals first + | exact absurd rfl hne + | omega + | exact hB.apart p q kp kq hp' hq' hne + | (have := bootTh_ltA hL hK P hq'; omega) + | (have := bootTh_ltA hL hK P hp'; omega) + · rcases w1ThA_cases h with ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨hF, rfl⟩ | hb + · omega + · omega + · unfold FrPg at hF; omega + · exact hB.ks p k hb hn + · rw [w1ThA_old (by rw [irqSlotA_val hK.fit hi]; omega)] + exact hB.irq i hi + · have h0 : (0#u64 : U64).val = 0 := rfl + rcases w1ThA_cases h with ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨hF, rfl⟩ | hb + · intro e; have := congrArg UScalar.val e; rw [r1, h0] at this; omega + · intro e; have := congrArg UScalar.val e; rw [r2, h0] at this; omega + · intro e; have := congrArg UScalar.val e; rw [h0] at this; unfold FrPg at hF; omega + · exact hB.nz p k hb + +end heap + +/-! ## The memory at the slots -/ + +section mem +variable {kb ib r now mt sl : U64} {nc : Nat} {uw : U64 → U64} (P : BootLay1 kb ib nc) +include P + +/-- A `w0` slot's `NEXT` word is still zero at `w1`. -/ +theorem w1_rev0A {y : U64} (hy : y ∈ bootOS kb ib nc) : w1M kb ib nc r now mt sl uw (wa y 24#u64) = 0#u64 := by + have hw := P.w0.ibw + have hk := P.w0.kbw + rcases bootOS_val P.w0 hy with ⟨t, c, i, -, hc, et, hi, -, ey⟩ | ⟨j, hj, -, ey⟩ + · have hv : (wa y 24#u64).val = ib.val + c * 8192 + (i.val * 32 + 24) := by + rw [wa_lit (show (24#u64 : U64).val = 24 from rfl) (by omega), ey, et]; omega + rw [w1M_idle P (by omega) (by omega) (by omega), tcbV P.w0 hc (o := i.val * 32 + 24) (by omega) hv] + exact tcbW_zero ⟨by omega, by omega, by omega, by omega, by omega, by omega, by omega⟩ + · have hv : (wa y 24#u64).val = kb.val + (43232 + 32 * j.val + 24) := by + rw [wa_lit (show (24#u64 : U64).val = 24 from rfl) (by omega), ey]; omega + rw [w1M_ksOld P (o := 43232 + 32 * j.val + 24) (by omega) hv (by omega) (by omega) (by omega)] + exact bootM_irqZ P.w0 (by omega) (by omega) hv + +theorem w1_revBootA {y : U64} (hy : y ∈ bootOS kb ib nc) : revA (w1M kb ib nc r now mt sl uw) y = false := by + unfold revA; rw [w1_rev0A P hy]; first | decide | rfl | simp + +/-- **A root CNode slot by index**: its number, its address and its cap. -/ +theorem rr_capA {idx : List Bool} (hl : idx.length = 13) : + blvA idx < 8192 ∧ slotAddrA (specPtr (bwv RRb)) (Fl4SpecA64.ofBl idx) = rs (blvA idx) ∧ + capAtA (w1M kb ib nc r now mt sl uw) (slotAddrA (specPtr (bwv RRb)) (Fl4SpecA64.ofBl idx)) = + capOf nc (blvA idx) := by + have hj : blvA idx < 8192 := by have := blvA_lt idx; rw [hl] at this; norm_num at this; exact this + have ha : slotAddrA (specPtr (bwv RRb)) (Fl4SpecA64.ofBl idx) = rs (blvA idx) := by + rw [ofBlA_eq (by omega)] + apply UScalar.eq_of_val_eq + rw [slotAddrA_valN (by rw [vRR]; omega), vRR, rs_val hj] + refine ⟨hj, ha, ?_⟩ + rw [ha] + exact cap_root P hj (rs_val hj) + +omit P in +/-- A root TCB slot is A64-Boot's `slotIdx`. -/ +theorem rtSlot_eqA {i : Nat} (hi : i < 7) : + slotAddrA (specPtr (bwv RTb)) (BitVec.ofNat 64 i) = slotIdx (bwv RTb) (bwv i) := by + apply UScalar.eq_of_val_eq + rw [slotAddrA_val (by rw [vRT]; omega), slotIdx_val (by rw [vRT]; norm_num) + (by rw [bwv_val (n := i) (by omega)]; exact hi), bwv_val (n := i) (by omega), vRT] + +/-- **A root TCB slot's cap** (A64-Boot's `cap_rt`). -/ +theorem rt_capA {i : Nat} (hi : i < tcbSlotsA) : + capAtA (w1M kb ib nc r now mt sl uw) (slotAddrA (specPtr (bwv RTb)) (BitVec.ofNat 64 i)) = rtCapA i := by + unfold tcbSlotsA at hi + have h2 := cap_rt (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) P (i := bwv i) + (by rw [bwv_val (n := i) (by omega)]; exact hi) + rw [bwv_val (n := i) (by omega)] at h2 + rw [rtSlot_eqA hi]; exact h2 + +omit P in +theorem rtW_revA {i : Nat} (hi : i < 7) : rtW ib (i * 32 + 24) = 0#u64 := by + interval_cases i <;> rfl + +omit P in +theorem mdbR_revA (nc j : Nat) : + ((mdbR nc j 24 &&& 281474976710656#u64) != 0#u64) = decide (cw0 nc j ≠ 0) := by + unfold mdbR + by_cases h : cw0 nc j = 0 + · rw [if_pos (Or.inr h), h]; decide + · rw [if_neg (by omega), decide_eq_true h] + split_ifs <;> decide + +end mem + +/-! ## The relations at `w1` -/ + +section rel +variable {kb ib r now mt sl : U64} {nc : Nat} {uw : U64 → U64} {σ : Fl4SpecA64.State Unit} +variable (hL : BootLayA ib nc) (hK : BootKsA kb ib nc) (P : BootLay1 kb ib nc) +include hL hK P + +theorem w1_nullBootA {y : U64} (hy : IsSlotA (bootThA kb ib nc) y) : + capAtA (w1M kb ib nc r now mt sl uw) y = .Null := + cap_idle P (w1_bootOSA hL hK P hy) + +/-- **`StateRelA` at `w1`** (module docstring). -/ +theorem w1_stateRelA (C : W1CsA kb ib nc σ) : + StateRelA (w1ThA kb ib nc) (w1M kb ib nc r now mt sl uw) σ where + cnode p r' h := by + rcases w1ThA_cases h with ⟨rfl, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | hb + · obtain rfl : r' = 13 := ObjKindA.cnode.inj e + refine ⟨_, C.cn, rootCsA_wf nc, fun idx hl => ?_⟩ + obtain ⟨-, -, hc⟩ := rr_capA (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) P hl + refine ⟨absCapA (capOf nc (blvA idx)), by simp [rootCsA, hl], ?_⟩ + rw [hc]; exact CapRelA.of_abs (capOf_wfA nc _) rfl + · cases e + · cases e + · rcases bootTh_loc hL hK hb with ⟨-, -, -, -, e, -⟩ | ⟨hn, -, -, -, e, -⟩ + · cases e + · cases e + refine ⟨_, C.irq p hn, emptyCnode_wf 0, fun idx hl => ?_⟩ + obtain rfl : idx = [] := List.eq_nil_of_length_eq_zero hl + refine ⟨.nullCap, by simp [Fl4SpecA64.emptyCnode], ?_⟩ + rw [slotAddrA_nil, w1_nullBootA hL hK P ⟨p, Or.inl ⟨0, hb, 0, by norm_num, (slotAddrA_zero p).symm⟩⟩] + exact CapRelA.null + tcb t h := by + rcases w1ThA_cases h with ⟨-, e⟩ | ⟨rfl, -⟩ | ⟨-, e⟩ | hb + · cases e + · obtain ⟨x, hx, hs⟩ := C.rt + refine ⟨x, hx, fun i hi => ⟨_, hs i hi, ?_⟩⟩ + rw [rt_capA P hi]; exact CapRelA.of_abs (rtCapA_wf i) rfl + · cases e + · obtain ⟨c, hc, rfl⟩ := bootThA_tcb hL hK hb + obtain ⟨x, hx, hs⟩ := C.idle c hc + refine ⟨x, hx, fun i hi => ⟨_, hs i hi, ?_⟩⟩ + rw [w1_nullBootA hL hK P ⟨_, Or.inr ⟨hb, i, hi, rfl⟩⟩]; exact CapRelA.null + +/-- **`is_original_cap` is the revocable bit at `w1`** (`CdtRelA.orig`). -/ +theorem w1_origA (C : W1CsA kb ib nc σ) : + ∀ x, AbsSlotA (w1ThA kb ib nc) x → σ.isOriginalCap x = revA (w1M kb ib nc r now mt sl uw) (cslotAddrA x) := by + have hB : PlaceA (bootThA kb ib nc) kb := + boot_place hL (bootWordsA_w0 (r := 0#u64) (now := 0#u64) (mt := 0#u64) P.w0 hL.scOff hK.scOff) + rintro ⟨q, idx⟩ ⟨p, e1, hcn | ⟨ht, i, hi, e2⟩⟩ + · simp only at e1 + subst e1 + rw [C.orig] + show w1OrigA nc (specPtr p, idx) = revA (w1M kb ib nc r now mt sl uw) (slotAddrA (specPtr p) (Fl4SpecA64.ofBl idx)) + rcases w1ThA_cases hcn with ⟨rfl, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | hb + · have hl : idx.length = 13 := ObjKindA.cnode.inj e + obtain ⟨hj, ha, -⟩ := rr_capA (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) P hl + have e3 : w1M kb ib nc r now mt sl uw (wa (rs (blvA idx)) 24#u64) = mdbR nc (blvA idx) 24 := + rs_md P hj rfl (by omega) (by omega) + rw [ha] + unfold revA + rw [e3, mdbR_revA] + simp [w1OrigA, hl] + · cases e + · cases e + · have hv := cn_slotV hB hb rfl + have hm : slotAddrA (specPtr p) (Fl4SpecA64.ofBl idx) ∈ bootOS kb ib nc := + w1_bootOSA hL hK P ⟨p, Or.inl ⟨_, hb, blvA idx, hv.2.1, hv.1⟩⟩ + have hlt := bootTh_ltA hL hK P hb + rw [w1_revBootA P hm] + simp [w1OrigA, sp_ne_rrA (p := p) (by omega)] + · simp only at e1 e2 + subst e1 + subst e2 + rw [C.orig] + show w1OrigA nc (specPtr p, Fl4SpecA64.tcbCnodeIndex i) = + revA (w1M kb ib nc r now mt sl uw) (slotAddrA (specPtr p) (Fl4SpecA64.ofBl (Fl4SpecA64.tcbCnodeIndex i))) + have hi7 := hi + unfold tcbSlotsA at hi7 + rw [ofBl_tcbIdx i (by omega)] + rcases w1ThA_cases ht with ⟨-, e⟩ | ⟨rfl, -⟩ | ⟨-, e⟩ | hb + · cases e + · have e3 : w1M kb ib nc r now mt sl uw (wa (slotIdx (bwv RTb) (bwv i)) 24#u64) = + rtW ib ((bwv i).val * 32 + 24) := + ts_w P (by rw [bwv_val (n := i) (by omega)]; exact hi7) rfl (by omega) + rw [bwv_val (n := i) (by omega), rtW_revA hi7] at e3 + rw [rtSlot_eqA hi7] + unfold revA + rw [e3] + simp [w1OrigA, sp_ne_rrA (p := bwv RTb) (by rw [vRT]; omega)] + · cases e + · have hm : slotAddrA (specPtr p) (BitVec.ofNat 64 i) ∈ bootOS kb ib nc := + w1_bootOSA hL hK P ⟨p, Or.inr ⟨hb, i, hi, rfl⟩⟩ + have hlt := bootTh_ltA hL hK P hb + rw [w1_revBootA P hm] + simp [w1OrigA, sp_ne_rrA (p := p) (by omega)] + +omit hL hK P in +theorem cslotAddr_rtA {i : Nat} (hi : i < 8) (u : U64) (hu : u.bv = BitVec.ofNat 64 i) : + cslotAddrA (w1RtSlotA i) = ts u := by + unfold cslotAddrA w1RtSlotA + dsimp only + rw [slotAddrA_sp, ofBl_tcbIdx i hi] + apply eq_of_bv + unfold ts slotIdx + rw [core.num.U64.wrapping_add_bv_eq, core.num.U64.wrapping_mul_bv_eq, hu] + rfl + +omit hL hK P in +theorem cslotAddr_rt0A : cslotAddrA (w1RtSlotA 0) = ts 0#u64 := cslotAddr_rtA (by omega) _ rfl +omit hL hK P in +theorem cslotAddr_rt1A : cslotAddrA (w1RtSlotA 1) = ts 1#u64 := cslotAddr_rtA (by omega) _ rfl +omit hL hK P in +theorem cslotAddr_rt2A : cslotAddrA (w1RtSlotA 2) = ts 2#u64 := cslotAddr_rtA (by omega) _ rfl + +omit hL hK P in +theorem bitsOfA_len' (x n : Nat) : (bitsOfA x n).length = n := by + induction n with + | zero => rfl + | succ n ih => simp [bitsOfA, ih] + +theorem cslotAddr_rrA {j : Nat} (hj : j < 8192) : cslotAddrA (w1RrSlotA j) = rs j := by + have := (rr_capA (r := 0#u64) (now := 0#u64) (mt := 0#u64) (sl := 0#u64) (uw := fun _ => 0#u64) P (idx := bitsOfA j 13) + (bitsOfA_len' j 13)).2.1 + have e : blvA (bitsOfA j 13) = j := by + rw [blvA_bitsOfA]; exact Nat.mod_eq_of_lt (by norm_num; omega) + rw [e] at this + exact this + +omit hL hK P in +/-- The root TCB slots `0..2` are abstract slots of `w1ThA`. -/ +theorem w1RtSlotA_abs {i : Nat} (hi : i < 3) : AbsSlotA (w1ThA kb ib nc) (w1RtSlotA i) := + ⟨bwv RTb, rfl, Or.inr ⟨w1ThA_rt, i, by unfold tcbSlotsA; omega, rfl⟩⟩ + +omit hL hK P in +/-- Every root CNode slot is an abstract slot of `w1ThA`. -/ +theorem w1RrSlotA_abs (j : Nat) : AbsSlotA (w1ThA kb ib nc) (w1RrSlotA j) := + ⟨bwv RRb, rfl, Or.inl (by simp only [w1RrSlotA, length_bitsOfA]; exact w1ThA_rr)⟩ + +/-- **`CdtRelA` at `w1`** (module docstring): A64-Boot's tree over the ghost `w1G nc` with the parent map `w1Par`. -/ +theorem w1_cdtRelA (C : W1CsA kb ib nc σ) : + CdtRelA (L := bL) bst (w1ThA kb ib nc) (w1S kb ib nc r now mt sl uw) σ ⟨w1G nc, w1Par⟩ where + tree := by + show TreeA bst {a | IsSlotA (w1ThA kb ib nc) a} (w1G nc) w1Par (w1S kb ib nc r now mt sl uw) + rw [w1OS_eqA hL hK P] + exact w1_treeA_par P + cdt sl' hsl := by + have hP := w1_placeA hL hK P + change (σ.cdt sl').map cslotAddrA = w1Par (cslotAddrA sl') + rw [C.cdt sl'] + unfold w1CdtA + by_cases h0 : sl' = w1RtSlotA 0 + · rw [if_pos h0, h0, cslotAddr_rt0A, w1Par_ts0]; simp only [Option.map_some]; exact congrArg some (cslotAddr_rrA (j := 2) hL hK P (by omega)) + · by_cases h1 : sl' = w1RtSlotA 1 + · rw [if_neg h0, if_pos h1, h1, cslotAddr_rt1A, w1Par_ts1]; simp only [Option.map_some]; exact congrArg some (cslotAddr_rrA (j := 3) hL hK P (by omega)) + · by_cases h2 : sl' = w1RtSlotA 2 + · rw [if_neg h0, if_neg h1, if_pos h2, h2, cslotAddr_rt2A, w1Par_ts2]; simp only [Option.map_some]; exact congrArg some (cslotAddr_rrA (j := 10) hL hK P (by omega)) + · rw [if_neg h0, if_neg h1, if_neg h2] + symm + by_contra hne + obtain ⟨y, hy⟩ := Option.ne_none_iff_exists'.1 hne + rcases w1Par_some_iff.1 hy with ⟨e, -⟩ | ⟨e, -⟩ | ⟨e, -⟩ + · exact h0 (cslotAddrA_inj hP hsl (w1RtSlotA_abs (i := 0) (by omega)) (e.trans cslotAddr_rt0A.symm)) + · exact h1 (cslotAddrA_inj hP hsl (w1RtSlotA_abs (i := 1) (by omega)) (e.trans cslotAddr_rt1A.symm)) + · exact h2 (cslotAddrA_inj hP hsl (w1RtSlotA_abs (i := 2) (by omega)) (e.trans cslotAddr_rt2A.symm)) + cdt_slot x q h := by + rw [C.cdt x] at h + unfold w1CdtA at h + by_cases h0 : x = w1RtSlotA 0 + · rw [if_pos h0] at h; obtain rfl := Option.some.inj h + exact ⟨by rw [h0]; exact w1RtSlotA_abs (i := 0) (by omega), w1RrSlotA_abs _⟩ + · by_cases h1 : x = w1RtSlotA 1 + · rw [if_neg h0, if_pos h1] at h; obtain rfl := Option.some.inj h + exact ⟨by rw [h1]; exact w1RtSlotA_abs (i := 1) (by omega), w1RrSlotA_abs _⟩ + · by_cases h2 : x = w1RtSlotA 2 + · rw [if_neg h0, if_neg h1, if_pos h2] at h; obtain rfl := Option.some.inj h + exact ⟨by rw [h2]; exact w1RtSlotA_abs (i := 2) (by omega), w1RrSlotA_abs _⟩ + · rw [if_neg h0, if_neg h1, if_neg h2] at h; cases h + orig := w1_origA hL hK P C + par_slot a b e := by + rcases w1Par_some_iff.1 e with ⟨rfl, -⟩ | ⟨rfl, -⟩ | ⟨rfl, -⟩ + · exact ⟨_, w1RtSlotA_abs (i := 0) (by omega), cslotAddr_rt0A⟩ + · exact ⟨_, w1RtSlotA_abs (i := 1) (by omega), cslotAddr_rt1A⟩ + · exact ⟨_, w1RtSlotA_abs (i := 2) (by omega), cslotAddr_rt2A⟩ + prev a p e := (w1_mdbInvA P).orig a p e + + +/-- **Every slot's CNode / thread cap names a `w1ThA` object at `w1`** (`JAtZA.cv`). -/ +theorem w1_cvA : ∀ y, IsSlotA (w1ThA kb ib nc) y → + CapValidA (w1ThA kb ib nc) (capAtA (w1M kb ib nc r now mt sl uw) y) := by + intro y hy + have hy' : y ∈ w1OS kb ib nc := by rw [← w1OS_eqA hL hK P]; exact hy + show CapValidA _ (capA bst (w1S kb ib nc r now mt sl uw) y) + rcases w1_capS P hy' with e | ⟨i, -, -, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨i, -, -, -, e⟩ <;> rw [e] + · exact trivial + · exact capOf_validA w1ThA_rr w1ThA_rt nc i + · exact capOf_validA w1ThA_rr w1ThA_rt nc 2 + · exact capOf_validA w1ThA_rr w1ThA_rt nc 3 + · exact fun h => absurd h (by decide) + · exact trivial + +end rel + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/JBootW1SA.lean b/proofs/Fl4ProofsA64/P3/JBootW1SA.lean new file mode 100644 index 000000000..8b4fc0793 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/JBootW1SA.lean @@ -0,0 +1,559 @@ +/- +P3 (aarch64, A64-P3 S3 gen 15, ruling 16:55Z item 4): **the abstract state at A64-Boot's post-`create_root_task` +witness `w1`** (`P1/BootW1`, the root task in FL4's kernel window since A64-Boot gen 9) and the VSpace half of the +joint relation there. The x86 counterpart is `JBoot`'s `w1` spec state. + +* **`w1SA kb ib nc now mt sl`**: `w0`'s `bootSA` with the root task's objects added to the heap (`w1KA`): the root + CNode `cnode 13 (rootCsA nc)` (`P3/JBootW1A`), the root TCB **`rootTcbA`** (`Running`, priority / MCP 255, its SC, + its IPC buffer, its three caps `rtCapA`), the root SC **`rootScA now sl`** (bound to the root TCB, refills + `(now, sl), (now, 0)`, `sc_refill_max` 2), the ASID pool **`w1PoolA`** (entry 1 = the VSpace root), the VSpace + root and the level-1 / 2 / 3 tables (**`ptOfA`**, each entry **`pteOfA`** of FL4's descriptor word **`ptWA`**), and + the 35 root frames as small data pages; `is_original_cap = w1OrigA`, the scheduler action `switchThread` the root + TCB (FL4's core 0 `SCHED_ACTION`), the IRQ states FL4's `irqW`, ASID-table entry 0 the pool (`w1AsidA`). +* **`w1MA ib nc`** (`bootMA` with the root TCB's affinity core 0), **`w1ZA kb ib nc`** (the ghosts: `w1ThA`, the CDT + ghost `⟨w1G nc, none⟩`, the TCBs / SCs of A64-Boot's `w1H`, pools `w1Pp`, Stage-1 tables `w1Tb`, depths `w1Lv`). +* **`w1SA_cs`**: `W1CsA` (gen 13's CSpace / CDT interface) holds at `w1SA`. +* **Descriptors** (`pteOfA`, `WordOkA`, **`pteOfA_rel`**): a word that is invalid, a table descriptor naming a table + one level down, or a last-level page descriptor is `PteRelA` to its decoding; **`ptWA_ok`**: every word of `w1`'s + four tables is such a word (the table entries by `w1_tabE_addr`, the 35 page entries by `w1_l3W_addr`: address bits + `< 2^44`, A64-Boot gen 9). +* **`w1_vsRelA`** (`VsRelA w1Pp w1Tb w1Lv ∅ ∅` at `w1M` and the block `kb`: the walk ASID 1 → pool → root → 0 → 0 → 2 + → level 3), **`w1_specVsA`** (`SpecVsA`), **`w1_hwTieA`** (`HwTieA`: the current thread is still core 0's idle + thread, so the ghost root is `bootHwA`, l4v's global user VSpace; the Stage-2 part vacuous). + +w0 counterparts: `bootSA` / `bootMA` / `bootZA` (`P3/JBootA`), `boot_vsRelA` / `boot_specVsA` / `boot_hwTieA` +(`P3/VsBootA`). Hypotheses: `BootLayA` / `BootKsA` (w0's `hsc` / `hsk`, closed at the concrete layout) and +`BootLay1`. Named premises: none. New module; no existing statement changed. +-/ +import Fl4ProofsA64.P3.JBootW1A +import Fl4ProofsA64.P3.VsBootA +import Fl4ProofsA64.P3.VsWalkA +import Fl4ProofsA64.P1.BootW1Win +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P1.W7.R.ST.BootWit + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## Descriptors as l4v's `pte` -/ + +/-- The attributes of a page descriptor as `PteAttrsA` reads them. -/ +def attrsA (w : U64) : Fl4SpecA64.VMAttributes := + {a | (a = .execute ∧ dBitA w 54 = false) ∨ (a = .device ∧ attrIdxA w ≠ 0)} + +/-- **A descriptor word as l4v's `pte` at depth `k`** (module docstring). -/ +def pteOfA (k : Nat) (w : U64) : Fl4SpecA64.PTE := + if dBitA w 0 = false then .invalidPTE + else if k < 3 ∧ dBitA w 1 = true then .pageTablePTE (ppnA w) + else .pagePTE (pageBaseA w k) (decide (k = 3)) (attrsA w) (pteRightsA w) + +theorem attrsA_rel (w : U64) : PteAttrsA w (attrsA w) where + exec := by simp [attrsA] + dev := by simp [attrsA] + glob := by simp [attrsA] + +/-- **The words `pteOfA` relates** (module docstring): invalid, a table descriptor naming a table of `Tb` one level +down, or a last-level page descriptor. -/ +def WordOkA (Tb : Set U64) (Lv : U64 → Nat) (k : Nat) (w : U64) : Prop := + dBitA w 0 = false ∨ (k < 3 ∧ dBitA w 1 = true ∧ (dAddrA w).val < 2 ^ 44 ∧ dTabA w ∈ Tb ∧ Lv (dTabA w) = k + 1) ∨ + (k = 3 ∧ dBitA w 1 = true ∧ (dAddrA w).val < 2 ^ 44) + +theorem pteOfA_rel {Tb : Set U64} {Lv : U64 → Nat} {k : Nat} {w : U64} (h : WordOkA Tb Lv k w) : + PteRelA Tb Lv k w (pteOfA k w) := by + by_cases h0 : dBitA w 0 = false + · unfold pteOfA; rw [if_pos h0]; exact h0 + have h0' : dBitA w 0 = true := by simpa using h0 + rcases h with h | ⟨h1, h2, h3, h4, h5⟩ | ⟨rfl, h2, h3⟩ + · exact absurd h h0 + · unfold pteOfA; rw [if_neg h0, if_pos ⟨h1, h2⟩] + exact ⟨h1, h0', h2, h3, rfl, h4, h5⟩ + · unfold pteOfA; rw [if_neg h0, if_neg (fun h => absurd h.1 (lt_irrefl 3))] + exact ⟨by omega, le_refl 3, h0', iff_of_true h2 rfl, rfl, rfl, h3, rfl, attrsA_rel w⟩ + +/-- A table entry of `pteOfA` is a table descriptor above the last level. -/ +theorem pteOfA_tab {k : Nat} {w : U64} {ppn : Fl4SpecA64.Ppn} (h : pteOfA k w = .pageTablePTE ppn) : + dBitA w 0 = true ∧ k < 3 ∧ dBitA w 1 = true ∧ ppn = ppnA w := by + unfold pteOfA at h + by_cases h0 : dBitA w 0 = false + · rw [if_pos h0] at h; cases h + rw [if_neg h0] at h + by_cases h1 : k < 3 ∧ dBitA w 1 = true + · rw [if_pos h1] at h; cases h; exact ⟨by simpa using h0, h1.1, h1.2, rfl⟩ + · rw [if_neg h1] at h; cases h + +theorem dBitA_bwv {n : Nat} (h : n < 2 ^ 64) (k : Nat) : dBitA (bwv n) k = decide (n / 2 ^ k % 2 = 1) := by + rw [dBitA_eq, bwv_val h] + +theorem dAddrA_mask (w : U64) : dAddrA w = w &&& vspace.ADDR_MASK := by unfold dAddrA vspace.ADDR_MASK; rfl + +theorem dBitA_zero (k : Nat) : dBitA 0#u64 k = false := by + unfold dBitA; simp + +/-- A table entry of `w1` (`tabE t`, A64-Boot gen 9's `w1_tabE_addr`): a table descriptor naming `t`. -/ +theorem tabE_okA {t : Nat} (ht : t = L1b ∨ t = L2b ∨ t = L3b) : + dBitA (bwv (tabE t)) 0 = true ∧ dBitA (bwv (tabE t)) 1 = true ∧ (dAddrA (bwv (tabE t))).val < 2 ^ 44 ∧ + dTabA (bwv (tabE t)) = bwv t := by + have ha := w1_tabE_addr t ht + rw [← dAddrA_mask] at ha + have hb : tabE t < 2 ^ 64 := by unfold tabE; rcases ht with rfl | rfl | rfl <;> norm_num + refine ⟨?_, ?_, by rw [ha.1]; exact ha.2, ?_⟩ + · rw [dBitA_bwv hb]; unfold tabE; rcases ht with rfl | rfl | rfl <;> decide + · rw [dBitA_bwv hb]; unfold tabE; rcases ht with rfl | rfl | rfl <;> decide + · apply UScalar.eq_of_val_eq + unfold dTabA + have e : (18446603336221196288#u64 : U64).val = 18446603336221196288 := rfl + rw [wa_valP, ha.1, e, bwv_val (by rcases ht with rfl | rfl | rfl <;> norm_num)] + rcases ht with rfl | rfl | rfl <;> norm_num + +/-- A page entry of `w1`'s level-3 table (`pgE f` for a page in the window): a page descriptor. -/ +theorem pgE_okA {f : Nat} (h1 : PPb ≤ f) (h2 : f < PPb + 2 ^ 44) (h3 : f % 4096 = 0) : + dBitA (bwv (pgE f)) 0 = true ∧ dBitA (bwv (pgE f)) 1 = true := by + have hb : pgE f < 2 ^ 64 := by unfold pgE; omega + rw [dBitA_bwv hb, dBitA_bwv hb] + simp only [decide_eq_true_eq, pow_zero, pow_one, Nat.div_one] + unfold pgE + omega + +/-! ## The root task's spec objects -/ + +/-- **The words of `w1`'s four tables** (module docstring), by the table's address and the byte offset. -/ +def ptWA (q o : Nat) : U64 := + if q = VSb then (if o = 0 then bwv (tabE L1b) else 0#u64) + else if q = L1b then (if o = 0 then bwv (tabE L2b) else 0#u64) + else if q = L2b then (if o = 16 then bwv (tabE L3b) else 0#u64) + else l3W o + +theorem ptWA_VS (o : Nat) : ptWA VSb o = if o = 0 then bwv (tabE L1b) else 0#u64 := by + unfold ptWA; rw [if_pos rfl] +theorem ptWA_L1 (o : Nat) : ptWA L1b o = if o = 0 then bwv (tabE L2b) else 0#u64 := by + unfold ptWA; rw [if_neg (show L1b ≠ VSb by norm_num), if_pos rfl] +theorem ptWA_L2 (o : Nat) : ptWA L2b o = if o = 16 then bwv (tabE L3b) else 0#u64 := by + unfold ptWA; rw [if_neg (show L2b ≠ VSb by norm_num), if_neg (show L2b ≠ L1b by norm_num), if_pos rfl] +theorem ptWA_L3 (o : Nat) : ptWA L3b o = l3W o := by + unfold ptWA + rw [if_neg (show L3b ≠ VSb by norm_num), if_neg (show L3b ≠ L1b by norm_num), if_neg (show L3b ≠ L2b by norm_num)] + +/-- **A `w1` table as l4v's `pt`** (module docstring): the VSpace root a `VSRootPT`, the others `NormalPT`s. -/ +def ptOfA (p : U64) : Fl4SpecA64.PT := + if p.val = VSb then .vsRootPT fun j => pteOfA 0 (ptWA p.val (8 * j.toNat)) + else .normalPT fun j => pteOfA (w1Lv p) (ptWA p.val (8 * j.toNat)) + +/-- **The root ASID pool** (`init_asid_pool`: entry `IT_ASID & 511 = 1` the VSpace root). -/ +def w1PoolA : Fl4SpecA64.AsidPool := fun lo => if lo = 1 then some (.asidPoolVSpace (specPtr (bwv VSb))) else none + +/-- **The root TCB's registers** (A64-Boot's `rtW`: `x0` the BootInfo address, `ELR` / `FaultIP` the entry point, +`SPSR = 0x140`, the others 0). -/ +def rootRegsA : Fl4SpecA64.Register → Fl4SpecA64.Word + | .x0 => 4202496 | .nextIP => 4194304 | .faultIP => 4194304 | .spsrEl1 => 0x140 | _ => 0 + +/-- **The root TCB** (module docstring; A64-Boot's `rtW`: `init_root_thread`). -/ +def rootTcbA : Fl4SpecA64.Tcb := + { Fl4SpecA64.defaultTcb 0 with + tcbArch := { (Fl4SpecA64.defaultTcb 0).tcbArch with + tcbContext := { (Fl4SpecA64.defaultTcb 0).tcbArch.tcbContext with userRegs := rootRegsA } } + tcbCtable := absCapA (rtCapA 0) + tcbVtable := absCapA (rtCapA 1) + tcbIpcframe := absCapA (rtCapA 2) + tcbState := .running + tcbIpcBuffer := BitVec.ofNat 64 4198400 + tcbMcpriority := 255#8 + tcbSchedContext := some (specPtr (bwv SCb)) + tcbPriority := 255#8 } + +/-- **The root SC** (module docstring; A64-Boot's `rscW`: `configure_sched_context` with the time slice `sl`). -/ +def rootScA (now sl : U64) : Fl4SpecA64.SchedContext := + { Fl4SpecA64.defaultSchedContext with + scTcb := some (specPtr (bwv RTb)) + scRefills := [⟨now.bv, sl.bv⟩, ⟨now.bv, 0⟩] + scRefillMax := 2 } + +open Classical in +/-- **The heap at `w1`** (module docstring): `w0`'s heap with the root task's objects. -/ +def w1KA (kb ib : U64) (nc : Nat) (now mt sl : U64) : Fl4SpecA64.KHeap := fun p => + if (fl4Ptr p).val = RRb then some (.cnode 13 (rootCsA nc)) + else if (fl4Ptr p).val = RTb then some (.tcb rootTcbA) + else if (fl4Ptr p).val = SCb then some (.schedContext (rootScA now sl) 0) + else if (fl4Ptr p).val = PLb then some (.archObj (.asidPool w1PoolA)) + else if fl4Ptr p ∈ w1Tb then some (.archObj (.pageTable (ptOfA (fl4Ptr p)))) + else if FrPg (fl4Ptr p).val then some (.archObj (.dataPage false .armSmallPage)) + else bootKA kb ib nc now mt p + +/-- ASID-table entry 0 is the root pool (`init_asid_pool`). -/ +def w1AsidA (hi : Fl4SpecA64.AsidHighIndex) : Option Fl4SpecA64.ObjRef := + if hi = 0 then some (specPtr (bwv PLb)) else none + +/-- **The abstract state at `w1`** (module docstring). -/ +def w1SA (kb ib : U64) (nc : Nat) (now mt sl : U64) : Fl4SpecA64.State Unit := + { bootSA kb ib nc now mt with + kheap := w1KA kb ib nc now mt sl + isOriginalCap := w1OrigA nc + cdt := w1CdtA + schedulerAction := .switchThread (specPtr (bwv RTb)) + interruptStates := fun irq => irqStOfA (irqW irq.toNat) + archState := { (bootSA kb ib nc now mt).archState with armAsidTable := w1AsidA } } + +/-- **The multi-core state at `w1`**: `bootMA` with the root TCB on core 0. -/ +def w1MA (ib : U64) (nc : Nat) : Fl4SpecA64.McState := + { bootMA ib nc with + tcbAffinity := fun p => if p = specPtr (bwv RTb) then some 0 else (bootMA ib nc).tcbAffinity p } + +/-- **The ghosts at `w1`** (module docstring). -/ +def w1ZA (kb ib : U64) (nc : Nat) : XSetsA := + ⟨w1ThA kb ib nc, ⟨w1G nc, w1Par⟩, (w1H kb ib nc).tcbs, (w1H kb ib nc).scs, bootCs nc, ∅, ∅, ∅, + fun c => specPtr (idleTA ib c), fun c => scRenA (w1MA ib nc) (idleSA ib c), bootHwA, + fun _ _ => [], fun _ => [], w1Pp, w1Tb, w1Lv, ∅, fun _ => 0⟩ + +/-! ## The heap -/ + +section heap +variable {kb ib now mt sl : U64} {nc : Nat} + +theorem w1KA_old {p : Fl4SpecA64.ObjRef} (h : (fl4Ptr p).val < RRb) : w1KA kb ib nc now mt sl p = bootKA kb ib nc now mt p := by + unfold w1KA + rw [if_neg (by omega), if_neg (by omega), if_neg (by omega), if_neg (by omega), + if_neg (by rintro (e | e | e | e) <;> omega), if_neg (by unfold FrPg; omega)] + +theorem w1KA_rr : w1KA kb ib nc now mt sl (specPtr (bwv RRb)) = some (.cnode 13 (rootCsA nc)) := by + unfold w1KA; rw [fl4Ptr_specPtr, vRR, if_pos rfl] + +theorem w1KA_rt : w1KA kb ib nc now mt sl (specPtr (bwv RTb)) = some (.tcb rootTcbA) := by + unfold w1KA; rw [fl4Ptr_specPtr, vRT, if_neg (by norm_num), if_pos rfl] + +theorem w1KA_sc : w1KA kb ib nc now mt sl (specPtr (bwv SCb)) = some (.schedContext (rootScA now sl) 0) := by + unfold w1KA; rw [fl4Ptr_specPtr, vSC, if_neg (by norm_num), if_neg (by norm_num), if_pos rfl] + +theorem w1KA_pl : w1KA kb ib nc now mt sl (specPtr (bwv PLb)) = some (.archObj (.asidPool w1PoolA)) := by + unfold w1KA; rw [fl4Ptr_specPtr, vPL, if_neg (by norm_num), if_neg (by norm_num), if_neg (by norm_num), if_pos rfl] + +theorem w1Tb_val {p : U64} (hp : p ∈ w1Tb) : p.val = VSb ∨ p.val = L1b ∨ p.val = L2b ∨ p.val = L3b := hp + +theorem w1KA_pt {p : U64} (hp : p ∈ w1Tb) : + w1KA kb ib nc now mt sl (specPtr p) = some (.archObj (.pageTable (ptOfA p))) := by + have h := w1Tb_val hp + unfold w1KA; rw [fl4Ptr_specPtr, if_neg (by omega), if_neg (by omega), if_neg (by omega), if_neg (by omega), + if_pos hp] + +/-- **What the `w1` heap holds** (module docstring): the root task's objects, or `w0`'s. -/ +theorem w1KA_cases (hL : BootLayA ib nc) {p : Fl4SpecA64.ObjRef} {ko : Fl4SpecA64.KernelObject} + (h : w1KA kb ib nc now mt sl p = some ko) : + ko = .cnode 13 (rootCsA nc) ∨ ko = .tcb rootTcbA ∨ ko = .schedContext (rootScA now sl) 0 ∨ + ko = .archObj (.asidPool w1PoolA) ∨ (fl4Ptr p ∈ w1Tb ∧ ko = .archObj (.pageTable (ptOfA (fl4Ptr p)))) ∨ + ko = .archObj (.dataPage false .armSmallPage) ∨ + (bootKA kb ib nc now mt p = some ko ∧ ((∃ c < nc, p = specPtr (idleTA ib c) ∧ ∃ sc, ko = .tcb (idleTcbA sc)) ∨ + (∃ t, ko = .schedContext (idleScObjA now mt t) 0) ∨ ko = .cnode 0 (Fl4SpecA64.emptyCnode 0))) := by + unfold w1KA at h + split_ifs at h with h1 h2 h3 h4 h5 h6 + · cases h; exact Or.inl rfl + · cases h; exact Or.inr (Or.inl rfl) + · cases h; exact Or.inr (Or.inr (Or.inl rfl)) + · cases h; exact Or.inr (Or.inr (Or.inr (Or.inl rfl))) + · cases h; exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨h5, rfl⟩)))) + · cases h; exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl rfl))))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ⟨h, bootKA_cases hL h⟩))))) + +/-- An idle TCB of `w1` (`w0`'s). -/ +theorem w1KA_idle (hL : BootLayA ib nc) (P : BootLay1 kb ib nc) {c : Nat} (hc : c < nc) : + w1KA kb ib nc now mt sl (specPtr (idleTA ib c)) = some (.tcb (idleTcbA (scRenA (bootMA ib nc) (idleSA ib c)))) := by + have := P.ibR + rw [w1KA_old (by rw [fl4Ptr_specPtr, idleTA_val' hL hc]; omega)] + exact bootKA_tcb hL hc + +end heap + +/-! ## The root TCB's caps and the CSpace interface -/ + +theorem rootTcbA_slot {i : Nat} (hi : i < tcbSlotsA) : + Fl4SpecA64.tcbCnodeMap rootTcbA (Fl4SpecA64.tcbCnodeIndex i) = some (absCapA (rtCapA i)) := by + unfold tcbSlotsA at hi + interval_cases i <;> + simp [Fl4SpecA64.tcbCnodeMap, Function.update_apply, tcbIdx_eq, Fl4SpecA64.tcbStage2Slot, + Fl4SpecA64.tcbHighVSpaceSlot, rootTcbA, Fl4SpecA64.defaultTcb, Fl4SpecA64.ExtTcb.default, rtCapA, absCapA] + +section cs +variable {kb ib now mt sl : U64} {nc : Nat} + +/-- **`W1CsA` at `w1SA`** (module docstring). -/ +theorem w1SA_cs (hL : BootLayA ib nc) (hK : BootKsA kb ib nc) (P : BootLay1 kb ib nc) : + W1CsA kb ib nc (w1SA kb ib nc now mt sl) where + cn := w1KA_rr + rt := ⟨rootTcbA, w1KA_rt, fun _ hi => rootTcbA_slot hi⟩ + idle c hc := ⟨_, w1KA_idle hL P hc, fun _ hi => idleTcbA_slot _ hi⟩ + irq q hq := by + obtain ⟨i, hi, hv⟩ := irqN_val hK hq + have := P.kbR + show w1KA kb ib nc now mt sl (specPtr q) = _ + rw [w1KA_old (by rw [fl4Ptr_specPtr]; omega)] + exact bootKA_irq hL hK hq + cdt _ := rfl + orig _ := rfl + +end cs + +/-! ## The tables -/ + +theorem lvVS {p : U64} (h : p.val = VSb) : w1Lv p = 0 := by unfold w1Lv; rw [h]; norm_num +theorem lvL1 {p : U64} (h : p.val = L1b) : w1Lv p = 1 := by unfold w1Lv; rw [h]; norm_num +theorem lvL2 {p : U64} (h : p.val = L2b) : w1Lv p = 2 := by unfold w1Lv; rw [h]; norm_num +theorem lvL3 {p : U64} (h : p.val = L3b) : w1Lv p = 3 := by unfold w1Lv; rw [h]; norm_num + +/-- A table entry naming table `t` at depth `k` is `WordOkA`. -/ +theorem tabE_wordOkA {t : Nat} (ht : t = L1b ∨ t = L2b ∨ t = L3b) {k : Nat} (hk : k < 3) + (hl : w1Lv (bwv t) = k + 1) : WordOkA w1Tb w1Lv k (bwv (tabE t)) := by + obtain ⟨b0, b1, ha, ht'⟩ := tabE_okA ht + refine Or.inr (Or.inl ⟨hk, b1, ha, ?_, by rw [ht']; exact hl⟩) + rw [ht'] + show (bwv t).val = VSb ∨ (bwv t).val = L1b ∨ (bwv t).val = L2b ∨ (bwv t).val = L3b + rw [bwv_val (by rcases ht with rfl | rfl | rfl <;> norm_num)] + omega + +theorem zero_wordOkA {k : Nat} : WordOkA w1Tb w1Lv k 0#u64 := Or.inl (dBitA_zero 0) + +/-- **Every word of `w1`'s four tables is `WordOkA`** (module docstring). -/ +theorem ptWA_ok {p : U64} (hp : p ∈ w1Tb) (o : Nat) : WordOkA w1Tb w1Lv (w1Lv p) (ptWA p.val o) := by + rcases w1Tb_val hp with h | h | h | h + · rw [lvVS h, h, ptWA_VS] + split_ifs + · exact tabE_wordOkA (Or.inl rfl) (by omega) (lvL1 vL1) + · exact zero_wordOkA + · rw [lvL1 h, h, ptWA_L1] + split_ifs + · exact tabE_wordOkA (Or.inr (Or.inl rfl)) (by omega) (lvL2 vL2) + · exact zero_wordOkA + · rw [lvL2 h, h, ptWA_L2] + split_ifs + · exact tabE_wordOkA (Or.inr (Or.inr rfl)) (by omega) (lvL3 vL3) + · exact zero_wordOkA + · rw [lvL3 h, h, ptWA_L3] + have ha : (dAddrA (l3W o)).val < 2 ^ 44 := by rw [dAddrA_mask]; exact w1_l3W_addr o + have pg : ∀ f, PPb ≤ f → f < PPb + 2 ^ 44 → f % 4096 = 0 → l3W o = bwv (pgE f) → WordOkA w1Tb w1Lv 3 (l3W o) := + fun f h1 h2 h3 e => Or.inr (Or.inr ⟨rfl, by rw [e]; exact (pgE_okA h1 h2 h3).2, ha⟩) + by_cases c0 : o = 0 + · exact pg IMb (by norm_num) (by norm_num) (by norm_num) (by unfold l3W; rw [if_pos c0]) + by_cases c8 : o = 8 + · exact pg IPb (by norm_num) (by norm_num) (by norm_num) (by unfold l3W; rw [if_neg c0, if_pos c8]) + by_cases c16 : o = 16 + · exact pg BIb (by norm_num) (by norm_num) (by norm_num) (by unfold l3W; rw [if_neg c0, if_neg c8, if_pos c16]) + by_cases cE : 24 ≤ o ∧ o < 280 + · exact pg (EFb + 4096 * ((o - 24) / 8)) (by omega) (by omega) (by omega) + (by unfold l3W; rw [if_neg c0, if_neg c8, if_neg c16, if_pos cE]) + · have e : l3W o = 0#u64 := by unfold l3W; rw [if_neg c0, if_neg c8, if_neg c16, if_neg cE] + rw [e]; exact zero_wordOkA + +theorem ptOfA_type {p : U64} (hp : p ∈ w1Tb) : Fl4SpecA64.ptType (ptOfA p) = ptTyA (w1Lv p) := by + unfold ptOfA + by_cases h : p.val = VSb + · rw [if_pos h, lvVS h]; rfl + · rw [if_neg h] + have : w1Lv p ≠ 0 := by rcases w1Tb_val hp with e | e | e | e <;> [exact absurd e h; rw [lvL1 e]; rw [lvL2 e]; + rw [lvL3 e]] <;> omega + unfold ptTyA; rw [if_neg this]; rfl + +/-- An entry of a `w1` table at any spec index is the decoding of one of its words. -/ +theorem ptOfA_applyW (p : U64) (i : Fl4SpecA64.Word) : + ∃ o < 4096, Fl4SpecA64.ptApply (ptOfA p) i = pteOfA (w1Lv p) (ptWA p.val o) := by + unfold ptOfA + by_cases h : p.val = VSb + · rw [if_pos h, lvVS h] + refine ⟨8 * (Fl4SpecA64.ucast Fl4SpecA64.vsIndexLen i).toNat, ?_, rfl⟩ + have : (Fl4SpecA64.ucast Fl4SpecA64.vsIndexLen i).toNat < 512 := (Fl4SpecA64.ucast Fl4SpecA64.vsIndexLen i).isLt + omega + · rw [if_neg h] + refine ⟨8 * (Fl4SpecA64.ucast Fl4SpecA64.ptIndexLen i).toNat, ?_, rfl⟩ + have : (Fl4SpecA64.ucast Fl4SpecA64.ptIndexLen i).toNat < 512 := (Fl4SpecA64.ucast Fl4SpecA64.ptIndexLen i).isLt + omega + +/-- An entry of a `w1` table at an FL4 index `i < 512`. -/ +theorem ptOfA_apply (p : U64) {i : U64} (hi : i.val < 512) : + Fl4SpecA64.ptApply (ptOfA p) i.bv = pteOfA (w1Lv p) (ptWA p.val (8 * i.val)) := by + have e : ∀ n, (Fl4SpecA64.ucast n i.bv).toNat = i.val % 2 ^ n := fun n => by + rw [show Fl4SpecA64.ucast n i.bv = i.bv.setWidth n from rfl, BitVec.toNat_setWidth]; rfl + unfold ptOfA + by_cases h : p.val = VSb + · rw [if_pos h, lvVS h] + show pteOfA 0 (ptWA p.val (8 * (Fl4SpecA64.ucast Fl4SpecA64.vsIndexLen i.bv).toNat)) = _ + rw [e]; exact congrArg _ (congrArg _ (by rw [Nat.mod_eq_of_lt (by exact hi)])) + · rw [if_neg h] + show pteOfA _ (ptWA p.val (8 * (Fl4SpecA64.ucast Fl4SpecA64.ptIndexLen i.bv).toNat)) = _ + rw [e]; exact congrArg _ (congrArg _ (by rw [Nat.mod_eq_of_lt (by exact hi)])) + +/-! ## FL4's VSpace words at `w1` -/ + +section mem +variable {kb ib r now mt sl : U64} {nc : Nat} {uw : U64 → U64} (P : BootLay1 kb ib nc) +include P + +theorem rootW_l3 {o : Nat} (ho : o < 4096) (b : U64) : rootW ib nc now sl uw (421888 + o) b = l3W o := by + unfold rootW + rw [if_neg (by omega), if_neg (by omega), if_neg (by omega), if_neg (by omega), if_neg (by omega), + if_neg (by omega), if_neg (by omega), if_neg (by omega), if_pos (by omega), show 421888 + o - 421888 = o by omega] + +/-- **The ASID table at `w1`**: entry 0 the pool, the rest zero. -/ +theorem w1_asidW {hi : U64} (hh : hi.val < 128) : + w1M kb ib nc r now mt sl uw (atAF kb hi) = if hi.val = 0 then bwv PLb else 0#u64 := by + have hk := P.w0.kbw + have hv := atAF_valA (kb := kb) hk hh + by_cases h0 : hi.val = 0 + · rw [if_pos h0, w1M_ks P (o := 772352) (by omega) (by omega)] + unfold ksW1; rw [if_neg (by omega), if_pos rfl] + · rw [if_neg h0, w1M_ksOld P (o := 772352 + 8 * hi.val) (by omega) (by omega) (by omega) (by omega) (by omega)] + exact boot_asidW P.w0 hh + +/-- **The pool's words at `w1`**: entry 1 the VSpace root, the rest zero. -/ +theorem w1_poolW {lo : U64} (hl : lo.val < 512) : + w1M kb ib nc r now mt sl uw (peAF (bwv PLb) lo) = if lo.val = 1 then bwv VSb else 0#u64 := by + have hv := peAF_valA (p := bwv PLb) (by rw [vPL]; norm_num) hl + rw [vPL] at hv + rw [w1M_root P (o := 397312 + 8 * lo.val) (by omega) (by omega), rootW_pl (by omega)] + by_cases h : lo.val = 1 + · rw [if_pos (by omega), if_pos h] + · rw [if_neg (by omega), if_neg h] + +/-- **A table's words at `w1`** are `ptWA`'s. -/ +theorem w1_ptMem {p : U64} (hp : p ∈ w1Tb) {i : U64} (hi : i.val < 512) : + w1M kb ib nc r now mt sl uw (peAF p i) = ptWA p.val (8 * i.val) := by + have hb := w1Tb_val hp + have hv := peAF_valA (p := p) (by omega) hi + rcases hb with h | h | h | h + · rw [w1M_root P (o := 393216 + 8 * i.val) (by omega) (by omega), rootW_vs (by omega)] + rw [h, ptWA_VS] + · rw [w1M_root P (o := 413696 + 8 * i.val) (by omega) (by omega), rootW_l1 (by omega)] + rw [h, ptWA_L1] + · rw [w1M_root P (o := 417792 + 8 * i.val) (by omega) (by omega), rootW_l2 (by omega)] + rw [h, ptWA_L2] + · rw [w1M_root P (o := 421888 + 8 * i.val) (by omega) (by omega), rootW_l3 P (by omega)] + rw [h, ptWA_L3] + +end mem + +/-! ## The VSpace relation, the spec invariant and the hardware-root tie at `w1` -/ + +section vs +variable {kb ib r now mt sl : U64} {nc : Nat} {uw : U64 → U64} + +theorem bwv_PL_ne0 : bwv PLb ≠ 0#u64 := fun e => by have := congrArg UScalar.val e; rw [vPL] at this; simp at this + +theorem ofNat7_ne0 {hi : U64} (hh : hi.val < 128) (h0 : hi.val ≠ 0) : BitVec.ofNat 7 hi.val ≠ 0 := by + intro e + have h := congrArg BitVec.toNat e + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)] at h + exact h0 h + +theorem ofNat9_eq1 {lo : U64} (hl : lo.val < 512) : BitVec.ofNat 9 lo.val = 1 ↔ lo.val = 1 := by + constructor + · intro e + have h := congrArg BitVec.toNat e + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)] at h + exact h + · intro h; rw [h]; rfl + +/-- **`VsRelA` at `w1`** (module docstring). -/ +theorem w1_vsRelA (P : BootLay1 kb ib nc) : + VsRelA w1Pp w1Tb w1Lv ∅ ∅ (w1M kb ib nc r now mt sl uw) kb (w1SA kb ib nc now mt sl) where + table hi hh := by + show w1AsidA (BitVec.ofNat 7 hi.val) = optPtrA (w1M kb ib nc r now mt sl uw (atAF kb hi)) + rw [w1_asidW P hh] + unfold w1AsidA + by_cases h0 : hi.val = 0 + · rw [if_pos h0, h0, if_pos (by decide)] + unfold optPtrA; rw [if_neg bwv_PL_ne0] + · rw [if_neg h0, if_neg (ofNat7_ne0 hh h0), optPtrA_zero] + tmem hi hh h := by + rw [w1_asidW P hh] at h ⊢ + by_cases h0 : hi.val = 0 + · rw [if_pos h0]; exact vPL + · rw [if_neg h0] at h; exact absurd rfl h + pool p hp := by + obtain rfl : p = bwv PLb := UScalar.eq_of_val_eq (by rw [vPL]; exact hp) + refine ⟨w1PoolA, w1KA_pl, fun _ lo hl => ?_⟩ + rw [w1_poolW P hl] + unfold w1PoolA + by_cases h : lo.val = 1 + · rw [if_pos ((ofNat9_eq1 hl).2 h), if_pos h] + unfold poolEntA; rw [if_neg (fun e => by have := congrArg UScalar.val e; rw [vVS] at this; simp at this)] + rfl + · rw [if_neg (fun e => h ((ofNat9_eq1 hl).1 e)), if_neg h, poolEntA_zero] + kind p hp _ lo hl := by + obtain rfl : p = bwv PLb := UScalar.eq_of_val_eq (by rw [vPL]; exact hp) + rw [w1_poolW P hl] + by_cases h : lo.val = 1 + · rw [if_pos h] + have u : untagA (bwv VSb) = bwv VSb := by unfold untagA; rfl + refine Or.inr (Or.inl ⟨by simp [tagA, vVS], ?_, ?_⟩) <;> rw [u] + · exact Or.inl vVS + · exact lv_vs + · rw [if_neg h]; exact Or.inl rfl + asid0 _ := by + rw [w1_asidW P (hi := 0#u64) (by decide), if_pos (show (0#u64 : U64).val = 0 from rfl), + w1_poolW P (lo := 0#u64) (by decide), if_neg (show ¬ (0#u64 : U64).val = 1 by decide)] + pt p hp := by + refine ⟨ptOfA p, w1KA_pt hp, ptOfA_type hp, fun _ i hi => ?_⟩ + rw [w1_ptMem P hp hi, ptOfA_apply p hi] + exact pteOfA_rel (ptWA_ok hp _) + high _ hy := absurd hy (Set.notMem_empty _) + lv p hp := by rcases w1Tb_val hp with e | e | e | e <;> [rw [lvVS e]; rw [lvL1 e]; rw [lvL2 e]; rw [lvL3 e]] <;> omega + al p hp := Nat.dvd_of_mod_eq_zero (by rcases w1Tb_val hp with e | e | e | e <;> omega) + pal p hp := Nat.dvd_of_mod_eq_zero (by have e : p.val = PLb := hp; omega) + yal _ hy := absurd hy (Set.notMem_empty _) + ytb _ hy := absurd hy (Set.notMem_empty _) + +theorem w1AsidA_some {hi : Fl4SpecA64.AsidHighIndex} {p : Fl4SpecA64.ObjRef} (h : w1AsidA hi = some p) : + hi = 0 ∧ p = specPtr (bwv PLb) := by + unfold w1AsidA at h + split_ifs at h with e + · cases h; exact ⟨e, rfl⟩ + +/-- **`SpecVsA` at `w1SA`** (module docstring). -/ +theorem w1_specVsA (hL : BootLayA ib nc) : SpecVsA (w1SA kb ib nc now mt sl) where + tab hi p h := by + obtain ⟨-, rfl⟩ := w1AsidA_some (show w1AsidA hi = some p from h) + exact ⟨_, w1KA_pl⟩ + tabInj hi hi' p h h' := by + rw [(w1AsidA_some (show w1AsidA hi = some p from h)).1, (w1AsidA_some (show w1AsidA hi' = some p from h')).1] + asid0 p f h hk := by + obtain ⟨-, rfl⟩ := w1AsidA_some (show w1AsidA 0 = some p from h) + change w1KA kb ib nc now mt sl _ = _ at hk + rw [w1KA_pl] at hk + cases hk + unfold w1PoolA; rw [if_neg (by decide)] + pool p f lo e hk he := by + change w1KA kb ib nc now mt sl _ = _ at hk + rcases w1KA_cases hL hk with e1 | e1 | e1 | e1 | ⟨-, e1⟩ | e1 | ⟨-, ⟨_, _, _, _, e1⟩ | ⟨_, e1⟩ | e1⟩ <;> cases e1 + unfold w1PoolA at he + split_ifs at he + cases he + exact Or.inl ⟨ptOfA (bwv VSb), w1KA_pt (Or.inl vVS), by unfold ptOfA; rw [if_pos vVS]; rfl⟩ + pte p t i ppn hk he := by + change w1KA kb ib nc now mt sl _ = _ at hk + rcases w1KA_cases hL hk with e1 | e1 | e1 | e1 | ⟨hq, e1⟩ | e1 | ⟨-, ⟨_, _, _, _, e1⟩ | ⟨_, e1⟩ | e1⟩ <;> cases e1 + obtain ⟨o, -, ho⟩ := ptOfA_applyW (fl4Ptr p) i + rw [ho] at he + obtain ⟨b0, hk3, -, rfl⟩ := pteOfA_tab he + rcases ptWA_ok hq o with h | ⟨-, -, ha, hT, hLv⟩ | ⟨e3, -⟩ + · rw [h] at b0; cases b0 + · rw [paddrFromPpn_ppnA _ ha, ptrFromPAddr_wa, pptrBase_litA] + show ∃ t', w1KA kb ib nc now mt sl (specPtr (dTabA _)) = _ ∧ _ + refine ⟨_, w1KA_pt hT, ?_⟩ + rw [ptOfA_type hT, hLv]; rfl + · omega + hpte p l1 i ppn hk he := by + change w1KA kb ib nc now mt sl _ = _ at hk + rcases w1KA_cases hL hk with e1 | e1 | e1 | e1 | ⟨-, e1⟩ | e1 | ⟨-, ⟨_, _, _, _, e1⟩ | ⟨_, e1⟩ | e1⟩ <;> cases e1 + +/-- **The hardware-root tie at `w1`** (module docstring): the current thread is core 0's idle thread. -/ +theorem w1_hwTieA (hL : BootLayA ib nc) (P : BootLay1 kb ib nc) (s2 : Option HwRootA) : + HwTieA (w1ZA kb ib nc) s2 (w1SA kb ib nc now mt sl) := by + have ht : (w1SA kb ib nc now mt sl).kheap (specPtr (idleTA ib 0)) = _ := w1KA_idle hL P hL.nc0 + refine ⟨?_, HwS2TieA.of_none ((vcpuOfA_of ht).trans (idleTcbA_vcpu _))⟩ + unfold HwS1TieA + show bootHwA = vmRootA (w1SA kb ib nc now mt sl) (specPtr (idleTA ib 0)) + exact (vmRootA_global (σ := w1SA kb ib nc now mt sl) (t := specPtr (idleTA ib 0)) + (fun pt asid v => by rw [vtCapA_of ht, idleTcbA_slot _ (by decide)]; exact fun e => by cases e)).symm + +end vs + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/JEntW0A.lean b/proofs/Fl4ProofsA64/P3/JEntW0A.lean new file mode 100644 index 000000000..1037805b6 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/JEntW0A.lean @@ -0,0 +1,114 @@ +/- +P3 (aarch64, A64-P3 S3 gen 6, ruling 11:49Z step 3): **the entry-level relation `JEntA` at A64-Boot's idle-only +boot witness `w0`** — A64 P1's invariants (`SysInvA`) from A64-Boot gen 6's lemmas (consumed, not re-proved) beside +the joint relation of `JBootW0A` (x86 `JBoot`'s `w0` with `JAtZB.zH` / `P1Inv`). + +* **`boot_sysInvA`**: `SysInvA` at `w0` for the boot heap `bootH`, the slots `bootOS kb ib nc` (A64-Boot gen 6: idle + TCB slots and the 5376 IRQ-node slots), the cores `bootCs nc`, `U = Pp = Tb = ∅`, `Ac = H.scs`, any `Lv`, the + idle area `[ib, ib + 8192·nc)`: `EntryInvA` from `bootH_ok`, `boot_bxi`, `boot_osOK`, `bootEpochP1` (`UserOK`, + `ksw`) and `BootItems.column` (`VsTy`); `SysL` is A64-Boot's `boot_sysL`. +* **`boot_jEntA`**: `JEntA` at `w0` (the relation `jbootA_w0`, the TCB / SC sets `bootTA_eq` / `bootCA_eq`, the queue + ghosts A64-Boot's `boot_sqi`); **`boot_jEntA_ex`** at the concrete layout. +* (gen 7) **`boot_slotApA`**: `SlotApA` at `w0` from A64-Boot gen 8's `P1/BootScAp` (`boot_scApart`, + `boot_irqStApartK`, `boot_vcApart`), now `JEntA.ap` inside `boot_jEntA` (ruling 12:26Z item 1), so + `boot_jEntA_ex` keeps only `hAR` [P]. (Gen 6's `boot_irqStApartA` is removed: Boot's `boot_irqStApartK`.) + +Premises: those of `jbootA_w0` (`hAR` [P]); `hsc` / `hsk` case hypotheses closed at the concrete layout. +-/ +import Fl4ProofsA64.P3.VsBootA +import Fl4ProofsA64.P3.BootAddrA +import Fl4ProofsA64.P3.SlotJA +import Fl4ProofsA64.P1.BootSysL0 +import Fl4ProofsA64.P1.BootIrq +import Fl4ProofsA64.P1.BootScAp +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P1.W7.R.ST.BootWit + +noncomputable section + +variable {kb ib r now mt : U64} {nc : Nat} + +/-- **A64 P1's invariants at `w0`** (module docstring). -/ +theorem boot_sysInvA (P : BootLay kb ib nc) (Lv : U64 → Nat) : + SysInvA bst (bootH kb ib nc) (bootCs nc) (bootOS kb ib nc) ∅ (bootH kb ib nc).scs ∅ ∅ Lv ib.val + (ib.val + nc * 8192) (bootS kb ib nc r now mt) := + ⟨EntryInvA3.of_full ⟨bootH_ok P, boot_bxi P, boot_osOK P, (bootEpochP1 P (bootCs nc)).2.2.1, + ((bootItems P).column (bootCs nc) ∅ Lv).2.2.2.2.1, (bootEpochP1 (r := r) (now := now) (mt := mt) P + (bootCs nc)).2.2.2.1⟩, boot_sysL P (bootCs nc)⟩ + +/-- (gen 12) **The VSpace words at `w0`** (`SlotApA.vs`): with no pool, table or window, only the ASID table's words, +which lie in the kernel-state block, off the idle TCBs and their slots (`BootLay.apart`) and above the IRQ node. -/ +theorem boot_vsApA (P : BootLay kb ib nc) : + ∀ b, VsFootA (bootZA kb ib nc).Pp (bootZA kb ib nc).S1 (bootZA kb ib nc).Y + (LawfulStore.ksb (st := bst) (bootS kb ib nc r now mt)) b → + ¬ InOS (bootOS kb ib nc) b ∧ ∀ t ∈ (bootH kb ib nc).tcbs, b.val < t.val ∨ t.val + 4096 ≤ b.val := by + intro b hb + have hk := P.kbw + have hA := P.apart + cases hb with + | asid hi hh => + have hv : (atAF (LawfulStore.ksb (st := bst) (bootS kb ib nc r now mt)) hi).val = + kb.val + 772352 + 8 * hi.val := atAF_valA (kb := kb) hk hh + refine ⟨?_, fun t ht => ?_⟩ + · rintro ⟨y, hy, g1, g2⟩ + rcases bootOS_val P hy with ⟨t, c, i, -, hc, et, hi', -, ey⟩ | ⟨j, hj, -, ey⟩ <;> omega + · obtain ⟨c, hc, e⟩ := bootH_tcbs ht + omega + | pool p lo hp hl => exact absurd hp (Set.notMem_empty _) + | pt p i hp hi => exact absurd hp (Set.notMem_empty _) + | high y i hy hi => exact absurd hy (Set.notMem_empty _) + +/-- (gen 7) **`SlotApA` at `w0`** (ruling 12:26Z item 1): A64-Boot gen 8's `P1/BootScAp` facts (`boot_scApart`, +`boot_irqStApartK`, `boot_vcApart` at `Z.V = ∅`), not re-proved. -/ +theorem boot_slotApA (P : BootLay kb ib nc) : + SlotApA (bootZA kb ib nc) (bootH kb ib nc) (bootOS kb ib nc) + (LawfulStore.ksb (st := bst) (bootS kb ib nc r now mt)) := + ⟨boot_scApart P, boot_irqStApartK P, (boot_vcApart (kb := kb) (ib := ib) (nc := nc)).1, boot_vcApart.2, + boot_vsApA P, fun _ hb => (extFoot_emptyE hb).elim⟩ + +/-- **`JEntA` at `w0`** (module docstring). -/ +theorem boot_jEntA {A : Type} (AR : A → HwRootA → Prop) {a : A} (P : BootLay kb ib nc) (Lv : U64 → Nat) + (hsc : 18446603336221208576 < ib.val ∨ ib.val + nc * 8192 ≤ 18446603336221208576) + (hsk : 18446603336221208576 < kb.val ∨ kb.val + 788736 ≤ 18446603336221208576) (hAR : AR a bootHwA) : + JEntA (L := bL) AR (bootZA kb ib nc) (bootH kb ib nc) (bootOS kb ib nc) ∅ (bootH kb ib nc).scs ∅ ∅ Lv ib.val + (ib.val + nc * 8192) (bootS kb ib nc r now mt) a (bootSA kb ib nc now mt) (bootMA ib nc) where + sys := boot_sysInvA P Lv + j := (jbootA_w0 AR P hsc hsk hAR).1 + tcbs := (bootTA_eq (bootLayA_of P hsc)).symm + scs := (bootCA_eq (bootLayA_of P hsc)).symm + sq := boot_sqi P + ap := boot_slotApA P + pp := rfl + s1 := fun _ h => h + ko := bootH_ksOff P + ptH := fun _ h => by rcases h with h | h <;> exact absurd h (Set.notMem_empty _) + tabInj := (boot_specVsA (kb := kb) (now := now) (mt := mt) (bootLayA_of P hsc)).tabInj + poolOff := PoolOff.boot (boot_asZero P) + objAlloc := by + intro p bytes _ u hu q b i d hc + rw [Fl4ProofsA64.P1.W7.R.ST.BootWit.boot_null P u hu] at hc + cases hc + idleAnchor := by + intro u hu q b i d hc + rw [Fl4ProofsA64.P1.W7.R.ST.BootWit.boot_null P u hu] at hc + cases hc + vsVc := fun _ _ ⟨v, hv, _⟩ => Set.notMem_empty v hv + +/-- **`JEntA` at A64-Boot's concrete witness** (`kb = PPTR_BASE + 2^30`, `ib = PPTR_BASE + 2^31`, 128 cores), with `SInvXA`. -/ +theorem boot_jEntA_ex {A : Type} (AR : A → HwRootA → Prop) {a : A} (Lv : U64 → Nat) (hAR : AR a bootHwA) : + JEntA (L := bL) AR (bootZA 18446603337294938112#u64 18446603338368679936#u64 128) (bootH 18446603337294938112#u64 18446603338368679936#u64 128) + (bootOS 18446603337294938112#u64 18446603338368679936#u64 128) ∅ (bootH 18446603337294938112#u64 18446603338368679936#u64 128).scs ∅ ∅ Lv + (18446603338368679936#u64 : U64).val ((18446603338368679936#u64 : U64).val + 128 * 8192) + (bootS 18446603337294938112#u64 18446603338368679936#u64 128 r now mt) a (bootSA 18446603337294938112#u64 18446603338368679936#u64 128 now mt) + (bootMA 18446603338368679936#u64 128) ∧ + SInvXA (bootSA 18446603337294938112#u64 18446603338368679936#u64 128 now mt) (bootMA 18446603338368679936#u64 128) 128 := by + have hi : (18446603338368679936#u64 : U64).val = 18446603338368679936 := by simp + have hk : (18446603337294938112#u64 : U64).val = 18446603337294938112 := by simp + exact ⟨boot_jEntA AR bootLayC_ex Lv (Or.inl (by rw [hi]; omega)) (Or.inl (by rw [hk]; omega)) hAR, + (jbootA_w0_ex (r := r) (mt := mt) AR hAR).2⟩ + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/JEntW1A.lean b/proofs/Fl4ProofsA64/P3/JEntW1A.lean new file mode 100644 index 000000000..335edfa85 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/JEntW1A.lean @@ -0,0 +1,669 @@ +/- +P3 (aarch64, A64-P3 S3 gen 15, ruling 16:55Z item 4): **the entry-level relation `JEntA` at A64-Boot's +post-`create_root_task` witness `w1`** — the joint relation `JAtZA` with its three extension records, the spec +invariant `SInvXA`, and A64 P1's invariants beside them, against the abstract state `w1SA` (`P3/JBootW1SA`). The x86 +counterpart is `JBoot`'s `w1`; the A64 `w0` counterparts are `P3/JBootA` / `P3/JBootW0A` / `P3/JEntW0A`. + +* **`w1_schedRelA`** (`SchedRelA`: each core still runs its idle thread on its idle SC — `create_root_task` only sets + core 0's `SCHED_ACTION` to the root TCB, which is the spec's `switchThread`; the root TCB `Running` on the root SC, + affinity core 0), **`w1_restA`** (`SchedRestA`: empty queues, `w0`'s node and domain words, the root TCB's priority + 255, the root SC's `REFILL_MAX` 2), **`w1_irqRelA`** (`IrqRelA`: the IRQ states FL4's `irqW`, the IRQ node `w0`'s). +* **`w1_jAtZA`** (`JAtZA` at the ghosts `w1ZA`: `w1_stateRelA` / `w1_cdtRelA` / `w1_placeA` / `w1_cvA` of + `P3/JBootW1A` at `w1SA_cs`, `w1_vsRelA`), **`w1_extA`** (no VCPU, Stage-2 root or high window; no slot holds such a + cap, `capOf_noExtA`), **`w1_sinvXA`** (`SInvXA` at `nc` cores; the IRQ node `w0`'s, `w1_specIrqA`). +* **`w1_slotApA`** (`SlotApA`: A64-Boot gen 8's `w1_slotAp`, and the VSpace footprint (ASID table, pool, the four + tables) off every slot and every TCB, `w1_vsApA`). +* **`w1_jEntA`** (`JEntA` at `w1`: `SysInvA` from A64-Boot's `w1_sysParts`, the queues `w1_bxiQ`, `SlotApA`, and gen + 13's `pp` (`w1Pp` is A64-Boot gen 7's pool set), `s1` (`w1Tb` itself), `ko` (`w1H_ksOff`), `ptH` (the pool and the + tables are `other 12` pages of `w1H`)) and **`w1_jEntA_ex`** (with `SInvXA`, `SpecVsA`, `HwTieA`, at A64-Boot's + concrete layout `kb = PPTR_BASE + 2^30`, `ib = PPTR_BASE + 2^31`, 128 cores; as `boot_jEntA_ex` at `w0`). + +Premises: `hAR : AR a bootHwA` **[P]** (the arch relation at boot, as at `w0`: the current thread is still idle); +`hsc` / `hsk` (w0's distance of the idle area and the block from l4v's `idle_sc_ptr`) as hypotheses of the generic +form, closed at the concrete layout. No new premise. New module; no existing statement changed. +-/ +import Fl4ProofsA64.P3.JBootW1SA +import Fl4ProofsA64.P3.BootAddrA +import Fl4ProofsA64.P3.JEntW0A +import Fl4ProofsA64.P2.BootSysA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P1.W7.R.ST.BootWit + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {kb ib r now mt sl : U64} {nc : Nat} {uw : U64 → U64} + +set_option hygiene false in +local notation "S1" => w1S kb ib nc r now mt sl uw +set_option hygiene false in +local notation "S0" => bootS kb ib nc r now mt + +/-! ## Names -/ + +theorem sp_ne_scA {x : Nat} (hx : x < 2 ^ 64) (h : x ≠ 18446603336221208576) : specPtr (bwv x) ≠ Fl4SpecA64.idleScPtr := + fun e => h (by rw [← bwv_val hx]; exact specPtr_eq_idleSc.1 e) + +theorem sp_ne_ofVal {p q : U64} (h : p.val ≠ q.val) : specPtr p ≠ specPtr q := fun e => h (by rw [specPtr_inj e]) + +theorem w1Ren_SC (hL : BootLayA ib nc) (P : BootLay1 kb ib nc) : + scRenA (w1MA ib nc) (bwv SCb) = specPtr (bwv SCb) := by + have h1 := idleSA_val' hL hL.nc0 + have h2 := P.ibR + have h3 := hL.nc0 + refine if_neg (fun e => ?_) + have e' : bwv SCb = idleSA ib 0 := specPtr_inj (e.trans rfl) + have := congrArg UScalar.val e' + rw [vSC] at this + omega + +theorem w1KA_sc0 (hL : BootLayA ib nc) : + w1KA kb ib nc now mt sl Fl4SpecA64.idleScPtr = some (.schedContext (idleScObjA now mt (specPtr (idleTA ib 0))) 0) := by + rw [w1KA_old (by rw [fl4Ptr_idleSc]; norm_num)] + exact bootKA_sc0 hL + +theorem w1KA_scc (hL : BootLayA ib nc) (P : BootLay1 kb ib nc) {c : Nat} (h0 : 0 < c) (hc : c < nc) : + w1KA kb ib nc now mt sl (specPtr (idleSA ib c)) = some (.schedContext (idleScObjA now mt (specPtr (idleTA ib c))) 0) := by + have := P.ibR + rw [w1KA_old (by rw [fl4Ptr_specPtr, idleSA_val' hL hc]; omega)] + exact bootKA_sc hL h0 hc + +theorem idle_ne_rtA (hL : BootLayA ib nc) (P : BootLay1 kb ib nc) {c : Nat} (hc : c < nc) : + specPtr (idleTA ib c) ≠ specPtr (bwv RTb) := by + have := idleTA_val' hL hc; have := P.ibR + exact sp_ne_ofVal (by rw [vRT]; omega) + +theorem bootT_of (hL : BootLayA ib nc) {t : U64} (h : t ∈ (bootH kb ib nc).tcbs) : ∃ c < nc, t = idleTA ib c := by + have : t ∈ bootTA ib nc := by rw [bootTA_eq (kb := kb) hL]; exact h + exact this + +theorem bootC_of (hL : BootLayA ib nc) {p : U64} (h : p ∈ (bootH kb ib nc).scs) : ∃ c < nc, p = idleSA ib c := by + have : p ∈ bootCA ib nc := by rw [bootCA_eq (kb := kb) hL]; exact h + exact this + +theorem idleT_in (hL : BootLayA ib nc) (P : BootLay1 kb ib nc) {c : Nat} (hc : c < nc) : + idleTA ib c ∈ (w1H kb ib nc).tcbs := w1_idleT P ⟨c, hc, idleTA_val' hL hc⟩ + +theorem idleS_in (hL : BootLayA ib nc) (P : BootLay1 kb ib nc) {c : Nat} (hc : c < nc) : + idleSA ib c ∈ (w1H kb ib nc).scs := by + have : idleSA ib c ∈ bootCA ib nc := ⟨c, hc, rfl⟩ + rw [bootCA_eq (kb := kb) hL] at this + exact (w1_idleS P this).1 + +/-! ## The scheduler relation at `w1` -/ + +variable (hL : BootLayA ib nc) (hK : BootKsA kb ib nc) (P : BootLay1 kb ib nc) + (hW : BootWordsA bst S0 kb ib now nc) + +include P in +theorem w1_nodeA {c K : U64} (hc : c.val < nc) (hK4 : K.val < 4288) (h16 : K.val ≠ 16) : + bL.mem S1 (bL.nodeAt S1 c K) = bL.mem S0 (bL.nodeAt S0 c K) := + w1_node P hc hK4 (by omega) + +include hL P hW in +/-- **`SchedRelA` at `w1`** (module docstring). -/ +theorem w1_schedRelA : + SchedRelA (L := bL) (w1H kb ib nc).tcbs (w1H kb ib nc).scs (bootCs nc) (w1ZA kb ib nc).curOf + (w1ZA kb ib nc).curScOf S1 (w1SA kb ib nc now mt sl) (w1MA ib nc) where + core := rfl + cpuCs := hL.nc0 + curOfW c hc := by + show specPtr (idleTA ib c.val) = _ + rw [w1_nodeA P hc (by simp [layout.node.CUR_THREAD]) (by simp [layout.node.CUR_THREAD]), hW.curT c hc] + curScOfW c hc := by + show scRenA (w1MA ib nc) (idleSA ib c.val) = _ + rw [w1_nodeA P hc (by simp [layout.node.CUR_SC]) (by simp [layout.node.CUR_SC]), hW.curS c hc] + cur := rfl + csc := bootRen0 hL + idle := by + show specPtr (idleTA ib 0) = specPtr (bL.mem S1 (bL.nodeAt S1 0#u64 layout.node.IDLE_THREAD)) + rw [w1_nodeA P (c := 0#u64) hL.nc0 (by simp [layout.node.IDLE_THREAD]) (by simp [layout.node.IDLE_THREAD]), + hW.idleT 0#u64 hL.nc0] + rfl + idleOf c hc := by + rw [w1_nodeA P hc (by simp [layout.node.IDLE_THREAD]) (by simp [layout.node.IDLE_THREAD]), hW.idleT c hc]; rfl + idleScOf c hc := by + rw [w1_nodeA P hc (by simp [layout.node.IDLE_SC]) (by simp [layout.node.IDLE_SC]), hW.idleS c hc]; rfl + act := by + show Fl4SpecA64.SchedulerAction.switchThread (specPtr (bwv RTb)) = actOfA (bL.actionW S1) + rw [w1_act P] + unfold actOfA + rw [if_neg (fun e => by have := congrArg UScalar.val e; rw [vRT] at this; simp at this), + if_neg (fun e => by have := congrArg UScalar.val e; rw [vRT] at this; simp at this)] + time := by + show now.bv = (bL.mem S1 (bL.nodeAt S1 0#u64 layout.node.CUR_TIME)).bv + rw [w1_nodeA P (c := 0#u64) hL.nc0 (by simp [layout.node.CUR_TIME]) (by simp [layout.node.CUR_TIME]), hW.time] + aff t ht := by + rcases m1_aff P ht with ⟨h, e⟩ | ⟨rfl, e⟩ + · obtain ⟨c, hc, rfl⟩ := bootT_of hL h + show (if specPtr (idleTA ib c) = specPtr (bwv RTb) then some 0 else (bootMA ib nc).tcbAffinity _) = _ + rw [if_neg (idle_ne_rtA hL P hc), bootMA_aff hL hc, e, hW.aff c hc] + · show (if specPtr (bwv RTb) = specPtr (bwv RTb) then some 0 else (bootMA ib nc).tcbAffinity _) = _ + rw [if_pos rfl, e]; rfl + affT p c h := by + change (if p = specPtr (bwv RTb) then some 0 else (bootMA ib nc).tcbAffinity p) = some c at h + split_ifs at h with hp + · exact ⟨bwv RTb, w1_rtT, hp.symm⟩ + · simp only [bootMA] at h + split_ifs at h with hq + obtain ⟨d, hd, e⟩ := hq + exact ⟨idleTA ib d, idleT_in hL P hd, e.symm⟩ + tcb t ht := by + rcases m1_st P ht with ⟨h, es⟩ | ⟨rfl, es⟩ + · obtain ⟨c, hc, rfl⟩ := bootT_of hL h + refine ⟨_, w1KA_idle hL P hc, by rw [es]; rfl, ?_⟩ + rcases m1_sc P ht with ⟨-, e⟩ | ⟨e', -⟩ + · rw [e, hW.tsc c hc] + show some _ = (if idleSA ib c = 0#u64 then none else some _) + rw [if_neg (idleSA_ne0 hL hc)]; rfl + · exact absurd (congrArg specPtr e') (idle_ne_rtA hL P hc) + · refine ⟨rootTcbA, w1KA_rt, by rw [es]; rfl, ?_⟩ + rcases m1_sc P ht with ⟨h, -⟩ | ⟨-, e⟩ + · obtain ⟨c, hc, e⟩ := bootT_of hL h + exact absurd (congrArg specPtr e).symm (idle_ne_rtA hL P hc) + · rw [e] + show some (specPtr (bwv SCb)) = (if bwv SCb = 0#u64 then none else some (scRenA (w1MA ib nc) (bwv SCb))) + rw [if_neg (fun e => by have := congrArg UScalar.val e; rw [vSC] at this; simp at this), w1Ren_SC hL P] + sc x hx := by + rcases w1Sc P hx with ⟨h, -⟩ | ⟨rfl, -⟩ + · obtain ⟨c, hc, rfl⟩ := bootC_of hL h + rw [w1_sI P h (K := 24#u64) (by simp), hW.scTcb c hc] + have hz : optRefA (idleTA ib c) = some (specPtr (idleTA ib c)) := if_neg (idleTA_ne0 hL hc) + rw [hz] + rcases Nat.eq_zero_or_pos c with h0 | h0 + · subst h0 + exact ⟨_, _, (congrArg (w1KA kb ib nc now mt sl) (bootRen0 hL)).trans (w1KA_sc0 hL), rfl⟩ + · exact ⟨_, _, (congrArg (w1KA kb ib nc now mt sl) (bootRen hL h0 hc)).trans (w1KA_scc hL P h0 hc), rfl⟩ + · refine ⟨rootScA now sl, 0, (congrArg (w1KA kb ib nc now mt sl) (w1Ren_SC hL P)).trans w1KA_sc, ?_⟩ + rw [w1_sR P (K := 24#u64) (by simp)] + show some (specPtr (bwv RTb)) = optRefA (rscW now sl 24) + rw [show rscW now sl 24 = bwv RTb by simp [rscW]] + exact (if_neg rt_ne0).symm + scOff x hx _ := by + rcases w1Sc P hx with ⟨h, -⟩ | ⟨rfl, -⟩ + · obtain ⟨c, hc, rfl⟩ := bootC_of hL h + exact idleSA_ne_sc hL hc + · exact sp_ne_scA (by norm_num) (by norm_num) + +include hL P hW in +/-- **`SchedRestA` at `w1`** (module docstring). -/ +theorem w1_restA : SchedRestA (L := bL) (w1ZA kb ib nc) S1 (w1SA kb ib nc now mt sl) (w1MA ib nc) where + rq _ _ := rfl + rqDom _ _ _ := rfl + rel := rfl + consumed := by + show (0 : BitVec 64) = (bL.mem S1 (bL.nodeAt S1 0#u64 layout.node.CONSUMED)).bv + rw [w1_nodeA P (c := 0#u64) hL.nc0 (by simp [layout.node.CONSUMED]) (by simp [layout.node.CONSUMED]), hW.cons]; rfl + repr := by + show true = decide (bL.mem S1 (bL.nodeAt S1 0#u64 layout.node.REPROGRAM) ≠ 0#u64) + rw [w1_nodeA P (c := 0#u64) hL.nc0 (by simp [layout.node.REPROGRAM]) (by simp [layout.node.REPROGRAM]), hW.repr]; decide + curDom := by + refine ⟨rfl, ?_⟩ + have hk := P.w0.kbw + have hv : (wa kb 8#u64).val = kb.val + 8 := by + rw [wa_valP]; have : (8#u64 : U64).val = 8 := rfl; rw [this]; omega + show w1M kb ib nc r now mt sl uw (wa kb 8#u64) = 0#u64 + rw [w1M_ksOld P (o := 8) (by omega) hv (by omega) (by omega) (by omega)] + exact hW.curD + domTime := by + have hk := P.w0.kbw + have hv : (wa kb 16#u64).val = kb.val + 16 := by + rw [wa_valP]; have : (16#u64 : U64).val = 16 := rfl; rw [this]; omega + show 72057594037927935#64 = (w1M kb ib nc r now mt sl uw (wa kb 16#u64)).bv + rw [w1M_ksOld P (o := 16) (by omega) hv (by omega) (by omega) (by omega)] + show 72057594037927935#64 = (bL.mem S0 (wa kb 16#u64)).bv + rw [hW.domT]; rfl + prio t ht := by + rcases w1T P ht with h | rfl + · obtain ⟨c, hc, rfl⟩ := bootT_of hL h + refine ⟨_, w1KA_idle hL P hc, rfl, ?_, ?_⟩ + · rw [show domA (idleTA ib c) = wa (idleTA ib c) 3432#u64 from rfl, + w1_tI P h (K := 3432#u64) (by simp) (fun _ => by simp)] + exact hW.dom c hc + · rw [show prioA (idleTA ib c) = wa (idleTA ib c) 3448#u64 from rfl, + w1_tI P h (K := 3448#u64) (by simp) (fun _ => by simp)] + rw [show wa (idleTA ib c) 3448#u64 = prioA (idleTA ib c) from rfl, hW.prio c hc]; rfl + · refine ⟨rootTcbA, w1KA_rt, rfl, ?_, ?_⟩ + · exact (w1_tR P (K := 3432#u64) (by simp)).trans (by simp [rtW]) + · have e : bL.mem S1 (prioA (bwv RTb)) = 255#u64 := (w1_tR P (K := 3448#u64) (by simp)).trans (by simp [rtW]) + rw [e]; rfl + scMem t ht _ := sc1_mem P ht + refillMax x hx := by + rcases w1Sc P hx with ⟨h, -⟩ | ⟨rfl, -⟩ + · obtain ⟨c, hc, rfl⟩ := bootC_of hL h + rw [w1_sI P h (K := 72#u64) (by simp), hW.rmax c hc] + rcases Nat.eq_zero_or_pos c with h0 | h0 + · subst h0 + exact ⟨_, _, (congrArg (w1KA kb ib nc now mt sl) (bootRen0 hL)).trans (w1KA_sc0 hL), rfl⟩ + · exact ⟨_, _, (congrArg (w1KA kb ib nc now mt sl) (bootRen hL h0 hc)).trans (w1KA_scc hL P h0 hc), rfl⟩ + · refine ⟨rootScA now sl, 0, (congrArg (w1KA kb ib nc now mt sl) (w1Ren_SC hL P)).trans w1KA_sc, ?_⟩ + rw [w1_sR P (K := 72#u64) (by simp)] + show 2 = (rscW now sl 72).val + rw [show rscW now sl 72 = 2#u64 by simp [rscW]]; rfl + +include hK P hW in +/-- **`IrqRelA` at `w1`** (module docstring). -/ +theorem w1_irqRelA : IrqRelA (bL.mem S1) (bL.ksb S1) (w1SA kb ib nc now mt sl) where + st i hi := by + show irqStOfA (irqW (BitVec.ofNat 13 i.val).toNat) = irqStOfA (w1M kb ib nc r now mt sl uw (irqWordA kb i)) + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega), + w1M_ks P (o := 224 + 8 * i.val) (by omega) (by rw [irqWordA_val hK.fit hi]; omega)] + unfold ksW1 + rw [if_neg (by omega), if_neg (by omega), if_pos (by omega), show (224 + 8 * i.val - 224) / 8 = i.val by omega] + node i hi := (boot_irqRel (mt := mt) hW).node i hi + nodeHi i hi := (boot_irqRel (mt := mt) hW).nodeHi i hi + +/-! ## The joint relation at `w1` -/ + +/-- The only root named by the root CNode's caps is the initial VSpace. -/ +private theorem capOf_rootA (nc i : Nat) {p : U64} {mp : Bool} {asid : U64} + (h : capOf nc i = .Pml4 p mp asid) : p = bwv VSb := by + unfold capOf at h + by_cases hlo : i < 16 + · rw [slotC_lo hlo] at h + interval_cases i <;> cases h <;> rfl + by_cases hsc : i < 16 + nc + · rw [slotC_sch (by omega) hsc] at h + cases h + rw [slotC_hi (by omega)] at h + generalize i - (16 + nc) = k at h + by_cases hlt : k < 37 + · interval_cases k <;> cases h + · rw [tailC_big (by omega)] at h + cases h + +include hL hK P in +/-- Both copies of the root cap at `w1` name the depth-0 table in `w1Tb`. -/ +theorem w1_pmlA (y : U64) (hy : IsSlotA (w1ThA kb ib nc) y) (p : U64) (mp : Bool) (asid : U64) + (hc : capAtA (bL.mem S1) y = .Pml4 p mp asid) : p ∈ w1Tb ∧ w1Lv p = 0 := by + have hy' : y ∈ w1OS kb ib nc := + (congrArg (fun slots : Set U64 => y ∈ slots) (w1OS_eqA hL hK P)).mp hy + change capA bst S1 y = .Pml4 p mp asid at hc + have hp : p = bwv VSb := by + rcases w1_capS P hy' with e | ⟨i, -, -, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨i, -, -, -, e⟩ <;> rw [e] at hc + · cases hc + · exact capOf_rootA nc i hc + · exact capOf_rootA nc 2 hc + · exact capOf_rootA nc 3 hc + · cases hc + · cases hc + subst p + refine ⟨Or.inl vVS, ?_⟩ + unfold w1Lv + rw [vVS] + decide + +include hL hK P hW in +/-- **`JAtZA` at `w1`** (module docstring). -/ +theorem w1_jAtZA {A : Type} (AR : A → HwRootA → Prop) {a : A} (hAR : AR a bootHwA) : + JAtZA (L := bL) AR (w1ZA kb ib nc) S1 a (w1SA kb ib nc now mt sl) (w1MA ib nc) where + st := w1_stateRelA hL hK P (w1SA_cs hL hK P) + cdt := w1_cdtRelA hL hK P (w1SA_cs hL hK P) + sch := w1_schedRelA hL P hW + ar := hAR + place := w1_placeA hL hK P + irq := w1_irqRelA hK P hW + rest := w1_restA hL P hW + thT t := by + constructor + · intro h + rcases w1ThA_cases h with ⟨-, e⟩ | ⟨rfl, -⟩ | ⟨-, e⟩ | hb + · cases e + · exact w1_rtT + · cases e + · obtain ⟨c, hc, rfl⟩ := bootThA_tcb hL hK hb + exact idleT_in hL P hc + · intro h + rcases w1T P h with h' | rfl + · obtain ⟨c, hc, rfl⟩ := bootT_of hL h' + have := P.ibR + show w1ThA kb ib nc (idleTA ib c) = some .tcb + rw [w1ThA_old (by rw [idleTA_val' hL hc]; omega)] + exact if_pos ⟨c, hc, rfl⟩ + · exact w1ThA_rt (kb := kb) (ib := ib) (nc := nc) + csN := ⟨nc, hL.nc0, hL.nc128, fun _ => Iff.rfl⟩ + idleT c hc := by + rw [w1_nodeA P hc (by simp [layout.node.IDLE_THREAD]) (by simp [layout.node.IDLE_THREAD]), hW.idleT c hc] + exact idleT_in hL P hc + idleC c hc := by + rw [w1_nodeA P hc (by simp [layout.node.IDLE_SC]) (by simp [layout.node.IDLE_SC]), hW.idleS c hc] + exact idleS_in hL P hc + curT c hc := by + rw [w1_nodeA P hc (by simp [layout.node.CUR_THREAD]) (by simp [layout.node.CUR_THREAD]), hW.curT c hc] + exact idleT_in hL P hc + cscC c hc := by + rw [w1_nodeA P hc (by simp [layout.node.CUR_SC]) (by simp [layout.node.CUR_SC]), hW.curS c hc] + exact idleS_in hL P hc + ksFit := hK.fit + scOffD p hp _ := by + rcases hp with hp | hp | hp | hp + · obtain ⟨k, hk⟩ : ∃ k, w1ThA kb ib nc p = some k := by + cases e : w1ThA kb ib nc p with + | none => exact absurd e hp + | some k => exact ⟨k, rfl⟩ + rcases w1ThA_cases hk with ⟨rfl, -⟩ | ⟨rfl, -⟩ | ⟨hF, -⟩ | hb + · exact sp_ne_scA (by norm_num) (by norm_num) + · exact sp_ne_scA (by norm_num) (by norm_num) + · intro e + have h1 := specPtr_eq_idleSc.1 e + unfold FrPg at hF; omega + · rcases bootTh_loc hL hK hb with ⟨c, hc, rfl, -⟩ | ⟨hn, -⟩ + · exact idleTA_ne_sc hL hc + · exact irqN_ne_sc hK hn + all_goals exact absurd hp (Set.notMem_empty _) + cv := w1_cvA hL hK P + pml := w1_pmlA hL hK P + vs := w1_vsRelA P + ext := ExtVsRelA.empty _ _ _ _ _ + +/-! ## The extension records at `w1` -/ + +/-- No cap of `w1` is a Stage-2 or high-window cap. -/ +def NoExtCapA (c : capability.Cap) : Prop := + (∀ p l mp asid va, c ≠ .Ept p l mp asid va) ∧ ∀ p mp asid, c ≠ .HighVSpace p mp asid + +macro "noext" : tactic => `(tactic| (unfold NoExtCapA; refine ⟨fun _ _ _ _ _ h => ?_, fun _ _ _ h => ?_⟩ <;> cases h)) + +theorem capOf_noExtA (nc i : Nat) : NoExtCapA (capOf nc i) := by + unfold capOf + by_cases h1 : i < 16 + · rw [slotC_lo h1]; interval_cases i <;> noext + by_cases h2 : i < 16 + nc + · rw [slotC_sch (by omega) h2]; noext + rw [slotC_hi (by omega)] + generalize i - (16 + nc) = k + by_cases h3 : k < 37 + · interval_cases k <;> noext + · rw [tailC_big (by omega)]; noext + +include P in +theorem w1_noExtA {y : U64} (hy : y ∈ w1OS kb ib nc) : NoExtCapA (capAtA (bL.mem S1) y) := by + show NoExtCapA (capA bst S1 y) + rcases w1_capS P hy with e | ⟨i, -, -, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨i, -, -, -, e⟩ <;> rw [e] + · noext + · exact capOf_noExtA nc i + · exact capOf_noExtA nc 2 + · exact capOf_noExtA nc 3 + · noext + · noext + +include hL hK P in +/-- **The extension records at `w1`** (module docstring). -/ +theorem w1_extA : + VcpuCoreA (L := bL) (w1ZA kb ib nc) S1 (w1SA kb ib nc now mt sl) ∧ + Stage2CoreA (L := bL) (w1ZA kb ib nc) S1 (w1SA kb ib nc now mt sl) ∧ + HighVSpaceCoreA (L := bL) (w1ZA kb ib nc) S1 (w1SA kb ib nc now mt sl) := by + have hos : ∀ y, IsSlotA (w1ThA kb ib nc) y → y ∈ w1OS kb ib nc := fun y hy => by + rw [← w1OS_eqA hL hK P]; exact hy + refine ⟨⟨fun _ h => absurd h (Set.notMem_empty _), fun p x h => ?_, fun t ht => ?_, + fun y hy p e => (w1_rootCap P (hos y hy)).nv p e⟩, + ⟨fun _ h => absurd h (Set.notMem_empty _), fun p es h => ?_, + fun y hy p l mp asid va e => absurd e ((w1_noExtA P (hos y hy)).1 p l mp asid va)⟩, + ⟨fun _ h => absurd h (Set.notMem_empty _), fun p l1 h => ?_, + fun y hy p mp asid e => absurd e ((w1_noExtA P (hos y hy)).2 p mp asid)⟩⟩ + · change w1KA kb ib nc now mt sl _ = _ at h + rcases w1KA_cases hL h with e1 | e1 | e1 | e1 | ⟨-, e1⟩ | e1 | ⟨-, ⟨_, _, _, _, e1⟩ | ⟨_, e1⟩ | e1⟩ <;> cases e1 + · have hv : bL.mem S1 (wa t 3664#u64) = 0#u64 := m1_vc P ht + have hr : bL.mem S1 (runVcpuA t) = 0#u64 := m1_run P ht + rcases w1T P ht with h | rfl + · obtain ⟨c, hc, rfl⟩ := bootT_of hL h + refine ⟨_, w1KA_idle hL P hc, ?_, ?_⟩ + · show none = optRefA _; rw [hv]; rfl + · show none = optRefA _; rw [hr]; rfl + · refine ⟨rootTcbA, w1KA_rt, ?_, ?_⟩ + · show none = optRefA _; rw [hv]; rfl + · show none = optRefA _; rw [hr]; rfl + · change w1KA kb ib nc now mt sl _ = _ at h + rcases w1KA_cases hL h with e1 | e1 | e1 | e1 | ⟨-, e1⟩ | e1 | ⟨-, ⟨_, _, _, _, e1⟩ | ⟨_, e1⟩ | e1⟩ <;> cases e1 + · change w1KA kb ib nc now mt sl _ = _ at h + rcases w1KA_cases hL h with e1 | e1 | e1 | e1 | ⟨-, e1⟩ | e1 | ⟨-, ⟨_, _, _, _, e1⟩ | ⟨_, e1⟩ | e1⟩ <;> cases e1 + +/-! ## The spec invariant at `w1` -/ + +/-- A CNode cap of the root CNode names the root CNode. -/ +theorem capOf_cnodeA {nc i : Nat} {x : Fl4SpecA64.ObjRef} {n : Nat} {g : List Bool} + (h : absCapA (capOf nc i) = .cnodeCap x n g) : x = specPtr (bwv RRb) := by + unfold capOf at h + by_cases h1 : i < 16 + · rw [slotC_lo h1] at h; interval_cases i <;> first | (cases h; done) | (cases h; rfl) + by_cases h2 : i < 16 + nc + · rw [slotC_sch (by omega) h2] at h; cases h + rw [slotC_hi (by omega)] at h + generalize i - (16 + nc) = k at h + by_cases h3 : k < 37 + · interval_cases k <;> cases h + · rw [tailC_big (by omega)] at h; cases h + +/-- The root TCB's caps (`rtCapA`, or null). -/ +theorem rootTcbA_cap {b : Fl4SpecA64.CnodeIndex} {a : Fl4SpecA64.Cap} + (h : Fl4SpecA64.tcbCnodeMap rootTcbA b = some a) : + a = .nullCap ∨ a = absCapA (rtCapA 0) ∨ a = absCapA (rtCapA 1) ∨ a = absCapA (rtCapA 2) := by + simp only [Fl4SpecA64.tcbCnodeMap, Function.update_apply] at h + split_ifs at h <;> cases h <;> simp [rootTcbA, Fl4SpecA64.defaultTcb, Fl4SpecA64.ExtTcb.default] + +include hL hK P in +/-- **`SpecIrqA` at `w1SA`**: the IRQ node's CNodes are `w0`'s, and no cap of the root task names one. -/ +theorem w1_specIrqA : SpecIrqA (w1SA kb ib nc now mt sl) := by + have B := boot_specIrq (now := now) (mt := mt) hL hK + have heq : ∀ irq : Fl4SpecA64.Irq, (w1SA kb ib nc now mt sl).kheap (irqNodeOfA kb irq) = + (bootSA kb ib nc now mt).kheap (irqNodeOfA kb irq) := fun irq => by + have := irqNodeOfA_val hL hK irq; have := irqU_lt irq; have := P.kbR + exact w1KA_old (by omega) + refine ⟨⟨⟨B.node.1.1, fun irq hi => ?_⟩, fun irq hi n hc => ?_⟩, fun irq hi o b a ho hb => ?_, + fun p o b a ho hb => ?_⟩ + · obtain ⟨cs, e, w⟩ := B.node.1.2 irq hi + exact ⟨cs, (heq irq).trans e, w⟩ + · obtain ⟨cs, e, w⟩ := hc + exact B.node.2 irq hi n ⟨cs, (heq irq).symm.trans e, w⟩ + · exact B.slot irq hi o b a ((heq irq).symm.trans ho) hb + · rintro ⟨irq, hi, n, g, ha⟩ + have hv := irqNodeOfA_val hL hK irq + have hlt := irqU_lt irq + have hR := P.kbR + have hne : fl4Ptr (irqNodeOfA kb irq) ≠ bwv RRb := fun e => by rw [e, vRR] at hv; omega + change w1KA kb ib nc now mt sl p = some o at ho + rcases w1KA_cases hL ho with rfl | rfl | rfl | rfl | ⟨-, rfl⟩ | rfl | ⟨hb', -⟩ + · simp only [Fl4SpecA64.capOf, rootCsA] at hb + split_ifs at hb + have e := Option.some.inj hb + rw [ha] at e + exact hne (by rw [show irqNodeOfA kb irq = specPtr (bwv RRb) from capOf_cnodeA e, fl4Ptr_specPtr]) + · rcases rootTcbA_cap hb with e | e | e | e <;> rw [ha] at e + · cases e + · rw [show rtCapA 0 = capOf nc 2 by rw [cap2]; rfl] at e + exact hne (by rw [show irqNodeOfA kb irq = specPtr (bwv RRb) from capOf_cnodeA e.symm, fl4Ptr_specPtr]) + · rw [show rtCapA 1 = .Pml4 (bwv VSb) true 1#u64 from rfl] at e; cases e + · rw [show rtCapA 2 = .Frame (bwv IPb) .Small 3#u64 false false false 0#u64 (bwv 0) from rfl] at e; cases e + all_goals first | (simp [Fl4SpecA64.capOf] at hb; done) | skip + exact B.ref p o b a hb' hb ⟨irq, hi, n, g, ha⟩ + +include hL hK P in +/-- **`SInvXA` at `w1`** (module docstring). -/ +theorem w1_sinvXA : SInvXA (w1SA kb ib nc now mt sl) (w1MA ib nc) nc where + mc := ⟨hL.nc0, fun c d hc hd e => idleTA_inj hL hc hd e, fun c d hc hd e => idleSA_inj hL hc hd e, + fun t c h => by + change (if t = specPtr (bwv RTb) then some 0 else (bootMA ib nc).tcbAffinity t) = some c at h + split_ifs at h with ht + · cases h; exact hL.nc0 + · simp only [bootMA] at h + split_ifs at h with hp + cases h; exact (Classical.choose_spec hp).1⟩ + view := rfl + curTcb := ⟨_, w1KA_idle hL P hL.nc0⟩ + idleTcb := ⟨_, w1KA_idle hL P hL.nc0, rfl⟩ + onlyIdle t x h hs := by + change w1KA kb ib nc now mt sl t = _ at h + rcases w1KA_cases hL h with e | e | e | e | ⟨-, e⟩ | e | ⟨-, ⟨c, hc, et, -, -⟩ | ⟨_, e⟩ | e⟩ + · cases e + · cases e; cases hs + · cases e + · cases e + · cases e + · cases e + · exact ⟨c, hc, et⟩ + · cases e + · cases e + idleSc := ⟨_, _, _, w1KA_idle hL P hL.nc0, by rw [bootRen0 hL]; rfl, w1KA_sc0 hL, rfl⟩ + curSc := ⟨_, _, w1KA_sc0 hL⟩ + ctQ _ _ _ h := by cases h + idleQ _ _ _ _ h := by cases h + rq _ _ _ h := by cases h + rls _ h := by cases h + rpos p y k h _ := by + change w1KA kb ib nc now mt sl p = _ at h + rcases w1KA_cases hL h with e | e | e | e | ⟨-, e⟩ | e | ⟨-, ⟨_, _, _, _, e⟩ | ⟨_, e⟩ | e⟩ <;> cases e + · simp [rootScA] + · simp [idleScObjA] + zc _ _ _ _ h := by cases h + rlsRun _ h := by cases h + curScReady hc := absurd rfl hc + faultSt t x h hf := by + change w1KA kb ib nc now mt sl t = _ at h + rcases w1KA_cases hL h with e | e | e | e | ⟨-, e⟩ | e | ⟨-, ⟨c, hc, -, sc, e⟩ | ⟨_, e⟩ | e⟩ <;> cases e <;> + exact (hf rfl).elim + irqN := w1_specIrqA hL hK P + +/-! ## The slot apartness and the entry relation at `w1` -/ + +include P in +/-- **The VSpace footprint at `w1` is off every slot and every TCB** (`SlotApA.vs`). -/ +theorem w1_vsApA : ∀ b, VsFootA w1Pp w1Tb ∅ kb b → + ¬ InOS (w1OS kb ib nc) b ∧ ∀ t ∈ (w1H kb ib nc).tcbs, b.val < t.val ∨ t.val + 4096 ≤ b.val := by + have hk := P.w0.kbw; have hA := P.w0.apart; have h1 := P.kbR; have h2 := P.ibR + have key : ∀ b : U64, ((kb.val + 772352 ≤ b.val ∧ b.val < kb.val + 773376) ∨ (VSb ≤ b.val ∧ b.val < PLb + 4096) ∨ + (L1b ≤ b.val ∧ b.val < L3b + 4096)) → + ¬ InOS (w1OS kb ib nc) b ∧ ∀ t ∈ (w1H kb ib nc).tcbs, b.val < t.val ∨ t.val + 4096 ≤ b.val := by + intro b hb + refine ⟨?_, fun t ht => ?_⟩ + · rintro ⟨y, hy, g1, g2⟩ + rcases w1OS_val P hy with ⟨t, c, i, -, hc, et, hi, -, ey⟩ | ⟨i, hi, -, ey⟩ | ⟨i, hi, ey⟩ | ⟨j, hj, -, ey⟩ <;> + omega + · rcases w1H_tcbs P ht with ⟨c, hc, e⟩ | e <;> omega + intro b hb + apply key + cases hb with + | asid hi hh => have := atAF_valA (kb := kb) hk hh; omega + | pool p lo hp hl => + have e : p.val = PLb := hp + have := peAF_valA (p := p) (by omega) hl; omega + | pt p i hp hi => + have := peAF_valA (p := p) (by rcases w1Tb_val hp with e | e | e | e <;> omega) hi + rcases w1Tb_val hp with e | e | e | e <;> omega + | high y i hy hi => exact absurd hy (Set.notMem_empty _) + +include P in +/-- **`SlotApA` at `w1`** (module docstring). -/ +theorem w1_slotApA : SlotApA (w1ZA kb ib nc) (w1H kb ib nc) (w1OS kb ib nc) (bL.ksb S1) := + ⟨(Fl4ProofsA64.P2.BootSysA.w1_slotAp (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) P).1, + (Fl4ProofsA64.P2.BootSysA.w1_slotAp (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) P).2.1, + (Fl4ProofsA64.P2.BootSysA.w1_slotAp (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) P).2.2.1, + (Fl4ProofsA64.P2.BootSysA.w1_slotAp (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) P).2.2.2, + w1_vsApA P, fun _ hb => (extFoot_emptyE hb).elim⟩ + +include P in +/-- The relation's pools are A64-Boot gen 7's pool set (`JEntA.pp`). -/ +theorem w1_ppEqA : (w1ZA kb ib nc).Pp = Fl4ProofsA64.P2.BootEpB.w1Pp kb ib nc r now mt sl uw := by + ext x + constructor + · intro h + obtain rfl : x = bwv PLb := UScalar.eq_of_val_eq (by rw [vPL]; exact h) + exact Fl4ProofsA64.P2.BootEpB.w1_ppNe P + · intro h + rw [Fl4ProofsA64.P2.BootEpB.w1_pp P h] + exact vPL + +/-- The pool's and the tables' pages are `other 12` objects of `w1H` (`JEntA.ptH`). -/ +theorem w1_ptHA : PtHA (w1H kb ib nc) (w1Pp ∪ w1Tb) := by + intro p hp + refine ⟨p, 12, ?_, le_refl _, by norm_num⟩ + rcases hp with e | e + · exact w1H_pg (j := 97) (by omega) (by omega) (by omega) (by have : p.val = PLb := e; omega) + · rcases w1Tb_val e with e | e | e | e + · exact w1H_pg (j := 96) (by omega) (by omega) (by omega) (by omega) + · exact w1H_pg (j := 101) (by omega) (by omega) (by omega) (by omega) + · exact w1H_pg (j := 102) (by omega) (by omega) (by omega) (by omega) + · exact w1H_pg (j := 103) (by omega) (by omega) (by omega) (by omega) + +include hL hK P hW in +/-- **`JEntA` at `w1`** (module docstring). -/ +theorem w1_jEntA {A : Type} (AR : A → HwRootA → Prop) {a : A} (hAR : AR a bootHwA) : + JEntA (L := bL) AR (w1ZA kb ib nc) (w1H kb ib nc) (w1OS kb ib nc) w1U (w1H kb ib nc).scs + (Fl4ProofsA64.P2.BootEpB.w1Pp kb ib nc r now mt sl uw) w1Tb w1Lv ib.val (ib.val + nc * 8192) S1 a + (w1SA kb ib nc now mt sl) (w1MA ib nc) where + sys := ⟨EntryInvA3.of_full (Fl4ProofsA64.P2.BootSysA.w1_sysParts P).1, (Fl4ProofsA64.P2.BootSysA.w1_sysParts P).2⟩ + j := ⟨w1_jAtZA hL hK P hW AR hAR, w1_extA hL hK P⟩ + tcbs := rfl + scs := rfl + sq := (Fl4ProofsA64.P2.BootSysA.w1_bxiQ P).2 + ap := w1_slotApA P + pp := w1_ppEqA P + s1 := fun _ h => h + ko := w1H_ksOff P + ptH := w1_ptHA + tabInj := (w1_specVsA (kb := kb) (now := now) (mt := mt) (sl := sl) hL).tabInj + poolOff := PoolOff.bootG (w1H_ok P) (w1_bootTab P) + objAlloc := by + intro p bytes hp u hu q b i d hc + obtain ⟨_, rfl, rfl, _, _⟩ := w1_ut P hu hc + rw [vUT] + refine Or.imp_right Or.inl ?_ + rcases hp with ⟨rfl, hp | hp | hp | hp | rfl⟩ | ⟨rfl, hp | hp⟩ | ⟨kind, hk, rfl⟩ | ⟨k, hk, rfl⟩ + · left; have h : p.val = PLb := hp; rw [h]; norm_num + · left; rcases w1Tb_val hp with h | h | h | h <;> rw [h] <;> norm_num + · exact (Set.notMem_empty p hp).elim + · exact (Set.notMem_empty p hp).elim + · left; rw [fl4Ptr_idleSc]; norm_num + · exact (Set.notMem_empty p hp).elim + · exact (Set.notMem_empty p hp).elim + · rcases w1ThA_cases hk with ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨hF, rfl⟩ | hOld + · left; rw [vRR]; norm_num [ObjKindA.size] + · left; rw [vRT]; norm_num [ObjKindA.size] + · have h20 : (20#u64 : U64).val = 20 := rfl + unfold FrPg at hF; simp only [ObjKindA.size, h20]; omega + · left + rcases bootTh_loc hL hK hOld with ⟨c, hc, _, hv, _, hn⟩ | ⟨_, i, hi, hv, _, hn⟩ + · rw [hv, hn]; have ht := P.ibR; omega + · rw [hv, hn]; have ht := P.kbR; omega + · have h20 : (20#u64 : U64).val = 20 := rfl + rw [h20] + simpa using w1H_offUT P hk + idleAnchor := by + intro u hu q b i d hc + obtain ⟨_, rfl, rfl, _, _⟩ := w1_ut P hu hc + rw [vUT, fl4Ptr_idleSc]; left; norm_num + vsVc := fun _ _ ⟨v, hv, _⟩ => Set.notMem_empty v hv + +end + +/-- **`JEntA` at A64-Boot's concrete `w1`** (`kb = PPTR_BASE + 2^30`, `ib = PPTR_BASE + 2^31`, 128 cores; any `r`, `now`, `mt`, `sl`, `uw`), +with `SInvXA`, `SpecVsA` and the hardware-root tie (as `boot_jEntA_ex` at `w0`). -/ +theorem w1_jEntA_ex {A : Type} (AR : A → HwRootA → Prop) {a : A} (r now mt sl : U64) (uw : U64 → U64) + (s2 : Option HwRootA) (hAR : AR a bootHwA) : + JEntA (L := bL) AR (w1ZA 18446603337294938112#u64 18446603338368679936#u64 128) (w1H 18446603337294938112#u64 18446603338368679936#u64 128) + (w1OS 18446603337294938112#u64 18446603338368679936#u64 128) w1U (w1H 18446603337294938112#u64 18446603338368679936#u64 128).scs + (Fl4ProofsA64.P2.BootEpB.w1Pp 18446603337294938112#u64 18446603338368679936#u64 128 r now mt sl uw) w1Tb w1Lv + (18446603338368679936#u64 : U64).val ((18446603338368679936#u64 : U64).val + 128 * 8192) + (w1S 18446603337294938112#u64 18446603338368679936#u64 128 r now mt sl uw) a (w1SA 18446603337294938112#u64 18446603338368679936#u64 128 now mt sl) + (w1MA 18446603338368679936#u64 128) ∧ + SInvXA (w1SA 18446603337294938112#u64 18446603338368679936#u64 128 now mt sl) (w1MA 18446603338368679936#u64 128) 128 ∧ + SpecVsA (w1SA 18446603337294938112#u64 18446603338368679936#u64 128 now mt sl) ∧ + HwTieA (w1ZA 18446603337294938112#u64 18446603338368679936#u64 128) s2 (w1SA 18446603337294938112#u64 18446603338368679936#u64 128 now mt sl) := by + have hi : (18446603338368679936#u64 : U64).val = 18446603338368679936 := by simp + have hk : (18446603337294938112#u64 : U64).val = 18446603337294938112 := by simp + have P := bootLay1C_ex + have hsc : 18446603336221208576 < (18446603338368679936#u64 : U64).val ∨ + (18446603338368679936#u64 : U64).val + 128 * 8192 ≤ 18446603336221208576 := Or.inl (by rw [hi]; omega) + have hsk : 18446603336221208576 < (18446603337294938112#u64 : U64).val ∨ + (18446603337294938112#u64 : U64).val + 788736 ≤ 18446603336221208576 := Or.inl (by rw [hk]; omega) + have hL := bootLayA_of P.w0 hsc + have hK := bootKsA_of P.w0 hsk + have hW := bootWordsA_w0 (r := r) (now := now) (mt := mt) P.w0 hsc hsk + exact ⟨w1_jEntA hL hK P hW AR hAR, w1_sinvXA hL hK P, w1_specVsA hL, w1_hwTieA hL P s2⟩ + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/JointA.lean b/proofs/Fl4ProofsA64/P3/JointA.lean new file mode 100644 index 000000000..215e1ff3d --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/JointA.lean @@ -0,0 +1,413 @@ +/- +P3 (aarch64, A64-P3 S3 gens 1–2): **the A64 joint relation `JAtZA`** between FL4's AArch64 kernel state (a +`LawfulStore` state `s`, an arch state `a`) and the abstract state (`σ : Fl4SpecA64.State Unit`, the view of one +core, with the multi-core part `m : McState` beside it, `Ext/MultiCore`), and the three extension records beside it. + +**Flat and multi-core-ready** (Architect's ruling 08:10Z): every record below is one level of `Prop` fields over +the ghost sets `XSetsA` (one record of sets, as x86's `XSets`); `JAtZA` `extends` `XTopAtA`, so its fields are +read directly (`J.st`, `J.sch`, …). Nothing assumes one core: the spec state is the view of core `L.cpu s` +(`SchedRelA.core`), each core's idle thread and idle SC are `m`'s (`idleOf`, `idleScOf`), every TCB's affinity is +`m`'s (`aff`, `affT`), and each core's current thread and SC are the ghosts `curOf` / `curScOf` (`SchedRelA`) and +typed (`JAtZA.curT`, `cscC`). **One spec view per active serial core** (ruling 08:46Z item 1): `σ` is core +`m.curCore`'s view; `Fl4SpecA64` is not edited. + +* `XSetsA` (x86 `XSets`): the typed heap `th`, the CDT ghost `G`, the TCBs `T`, SCs `C`, cores `Cs`, VCPUs `V`, + Stage-2 roots `X`, `ArmHighVSpace`s `Y`; the per-core ghosts `curOf` / `curScOf` and the hardware root `hw`; + (gen 3) the per-core ready-queue lists `Q c p` and release-queue lists `R c` (A64 P1's `SchedQueuesInv` ghosts). +* **`SchedRestA Z s σ m`** (gen 3, ruling 09:59Z item 3; x86 `Sched.SchedSpec`'s remaining fields): the view's + ready queues (domain 0) and release queue are the current core's ghost lists, other domains empty; consumed time, + reprogram flag, current domain and domain time are FL4's words; each TCB's domain 0 and priority FL4's `PRIO`; a + nonzero TCB SC word names an SC of `C`; each SC's `sc_refill_max` is its `REFILL_MAX`. A sibling record of + `SchedRelA` (its signature unchanged), `XTopAtA.rest`. +* **`SchedRelA T C Cs curOf curScOf s σ m`** (x86 `Sched.SchedRel`'s current-thread / SC / TCB-state part, + `JKern.cur`, and `McState` for the per-core facts x86 has no field for): **each core's** current thread and SC are + the ghosts `curOf c` / `curScOf c`, its node's `CUR_THREAD` / `CUR_SC` (renamed), with `curOf m.curCore = + σ.curThread` and `curScOf m.curCore = σ.curSc` (`SchedRelA.curThread` / `curScW` derive x86's `cur` / `csc`); idle + thread, scheduler action and clock are the current core's node words; each TCB of `T` is a spec TCB with FL4's + `STATE` code and `SC` word; each SC of `C` is a spec SC whose `scTcb` is FL4's `TCB` word. + **The idle-SC renaming `scRenA m`**: l4v's idle SC is the constant `idle_sc_ptr` (`Machine_A`, `pptr_base + + 0x3000`), FL4's are per core; in the view of core `m.curCore` that core's idle SC is named `idle_sc_ptr` and every + other SC by its address (`Ext/MultiCore`: "the view of another core renames it, which is the refinement's + business"; ruling 08:46Z item 2 approves it). **Injectivity is explicit**: `SchedRelA.scOff` (SCs) and + `JAtZA.scOffD` (`th`, `V`, `X`, `Y`) ask that nothing but the view's idle SC `m.idleScOf m.curCore` lie at + `idle_sc_ptr` (`idle_sc_ptr ∉ dom \ {m.idleScOf m.curCore}`), so no two objects alias in `σ.kheap` (`scRenA_inj`). + (Gen 12: under `specPtr` naming, `idle_sc_ptr` names FL4's `0xffff_8000_0000_3000`, physical page 3, which no + object but the view's idle SC may occupy.) +* **`MachPlatCA arI MR`** [P] (x86 `MachPlatC`, minimal): `now` is l4v's `getCurrentTime` (`gctValA`, `gctStateA`), + and `MR` reads only the masks, the oracle positions and the last machine time. Only `now` and `frame`: the S3 + lemmas read nothing else; x86's `irqPending` / `mask1` come with the interrupt relation (S4). +* **`AR : A → HwRootA → Prop`** (x86 `AR : A → Cr3 → Prop`; ruling 08:46Z item 3): the arch relation on a narrow + hardware-root view, the active Stage-1 root and ASID (`HwRootA`). The spec does not record it (`setVSpaceRoot` + is a machine op, PORTING.md "Remaining"; x86's spec has `x64_current_cr3`), so it is the ghost `Z.hw`; + `XTopAtA.ar : AR a Z.hw`. The ASID tables stay with the VSpace relation (S4), the VCPU / Stage-2 / high-window + roots with the sibling records; the whole `ArchState` is not related. +* **`IrqRelA mem ksb σ`** (gen 3, ruling 09:59Z item 2; x86 `IrqRel`): for FL4's 5376 IRQs, the spec's + `interrupt_states` is FL4's state word (`ksb + 224 + 8 i`, `irqStOfA` as x86's `irqSt`) and `interrupt_irq_node` + is FL4's IRQ-node slot (`ksb + 43232 + 32 i`, a one-slot CNode of `th` by `PlaceA.irq`). +* **`XTopAtA`** (x86 `XTopAt`, its CSpace / scheduler / placement core): `st` (`StateRelA`), `cdt` (`CdtRelA`, + A64 P1's `TreeA` inside, ruling item 4), `sch` (`SchedRelA`), `ar`, `place` (`PlaceA`), `irq` (`IrqRelA`, gen 3), `rest` (`SchedRestA`, gen 3). +* **`JAtZA`** (x86 `JAtZB`'s core): `XTopAtA` and the joint facts: `thT` (the typed heap's TCBs are `T`, x86 + `thT` both ways), `csN` (cores `0 .. n-1`, `n ≤ 128`, x86 `JKern.csN` / `csLt` / `cs0`), each core's idle + thread, idle SC, current thread and current SC in the sets, `ksFit` (x86 `JKern.ksbFit`), `scOffD` (above), + (gen 9) `cv` (every slot's CNode / thread cap names a CNode / TCB of `th`, `CapValidA`; x86 `SlotsValid`). +* **The extension records, flat siblings of `JAtZA`** (as x86's `VcpuCore` / `EptCore` beside `JAtZB`), each + relating `Fl4SpecA64`'s `ext` / VCPU parts to FL4's objects: + - `VcpuCoreA V` (x86 `VcpuCore`'s `vo` / `vr` / `vb` core): every VCPU of `V` is a spec `vcpu` whose `vcpuTcb` + is FL4's `TCB` word (+0) and whose `vcpuExt.runCaller` is FL4's `RUN_CALLER` (+0x18); the spec has no other + VCPU; each TCB's `tcbVcpu` is its `VCPU` word (+3664) and `tcbExt.tcbRunVcpu` its `RUN_VCPU` (+3672); + every `Vcpu` cap of a slot names a VCPU of `V`. + - `Stage2CoreA X` (x86 `EptCore`'s `rel` object part / `exte`): every root of `X` is a spec + `ext (stage2Root _)` and conversely; every `Ept` cap names a root of `X`. + - `HighVSpaceCoreA Y` (no x86 counterpart): the same for `ext (highVSpace _)` and `HighVSpace` caps. + The entries of the roots and windows (their PTE relation) are S4. +* `JAtZAX := JAtZA ∧ VcpuCoreA ∧ Stage2CoreA ∧ HighVSpaceCoreA` (x86 `VmEptState`'s shape, flat). + +**Gen 12 (ruling 15:01Z items 2–3, 15:08Z).** Every object is named in the spec by **`specPtr p`** (`P3/PtrA`; +was `p.bv`): `optRefA`, `scRenA`, `actOfA`, the scheduler relation's thread / SC / affinity names, the queues +(`map specPtr`), the IRQ node, `scOffD`, and the extension records. `idle_sc_ptr` is then the name of the FL4 +pointer `0xffff_8000_0000_3000` (`specPtr_eq_idleSc`), which `scOff` / `scOffD` keep free. `XSetsA` gains the VSpace +ghosts `Pp` (pools), `S1` (Stage-1 tables, VS gen 2 Q2's subset of A64 P1's `Tb`) and `Lv`, and **`JAtZA.vs : +VsRelA`** (`P3/VsRelA`) is a field (x86: `VsRel` / `PtRel` inside `JAtZ`), so `JAtZAX` carries the VSpace relation +and every leaf keeps it through `FootNA.vs` / `SpecGlobA.asid` (`P3/FrameA`). `HwTieA` stays a flat predicate +beside `JAtZA` (`P3/VsTieA`, `s2` a parameter). + +**Gen 18 (ruling 18:13Z item 4): the `ext` component.** `XSetsA` gains the Stage-2 ghosts `T2` / `Sh2`, and +**`JAtZA.ext : ExtVsRelA`** (`P3/ExtVsDefA`, the merged ExtVsA relation with `wlo`) is a field beside `vs`; every +leaf keeps it through `FootNA.ext` / `SlotApA.ext` (its words are no slot or TCB words) and `ExtVsRelA.of_spec` +(no slot-only or TCB-state spec step touches a root or a page table). `VaOkE` for mapped plain frame caps (ExtVsA +gen 3's `hVa`) is `CapValidA`'s frame arm, so `cv` carries it (`JAtZA.vaOkE`, `P3/FrameA`). + +FL4-only invariants (A64 P1's `BXI`, `EntryInvA`, `SysL`) are not repeated here (ruling item 5): they stay beside +`JAtZA` and are combined at entry level as `SysInvA ∧ JAtZAX` (`P3/EntryA`). + +Named premises: `MachPlatCA` [P] (a structure, not taken by any theorem here). No definition is an existing one +changed. +-/ +import Fl4ProofsA64.P3.StateRelA +import Fl4ProofsA64.P3.VsRelA +import Fl4ProofsA64.P3.ExtVsDefA +import Fl4ProofsA64.P1.QueueSchedStep +import Fl4ProofsA64.P1.QueueSchedInv +import Fl4ProofsA64.P1.QueueCancel +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## The ghost sets -/ + +/-- **The hardware-root view** (module docstring; x86 `Cr3`): the active Stage-1 root and its ASID (`none` for the +global user VSpace with the reserved VMID, `set_global_user_vspace`), as `set_vm_root` / `arm_context_switch` install +them (`setVSpaceRoot`). -/ +structure HwRootA where + root : Fl4SpecA64.ObjRef + asid : Option Fl4SpecA64.Asid + +/-- The typed heap, CDT ghost, object sets and per-core ghosts of the A64 relation (module docstring; x86 `XSets`). -/ +structure XSetsA where + th : TypedHeapA + G : MdbGhostA + T : Set U64 + C : Set U64 + Cs : Set U64 + V : Set U64 + X : Set U64 + Y : Set U64 + /-- Each core's current thread (node `CUR_THREAD`; ruling 08:46Z item 1). -/ + curOf : Fl4SpecA64.CoreId → Fl4SpecA64.ObjRef + /-- Each core's current SC, named in the view (`scRenA`) (node `CUR_SC`). -/ + curScOf : Fl4SpecA64.CoreId → Fl4SpecA64.ObjRef + /-- The hardware root installed last (the spec does not model it: `setVSpaceRoot` is a machine op). -/ + hw : HwRootA + /-- (gen 3) The ready queues of each core and priority, as ghost lists (A64 P1's `SchedQueuesInv` `Q`). -/ + Q : U64 → U64 → List U64 + /-- (gen 3) The release queue of each core (A64 P1's `SchedQueuesInv` `R`). -/ + R : U64 → List U64 + /-- (gen 12) The ASID pools `VsRelA` relates (A64 P1's `VsTy` `Pp`). -/ + Pp : Set U64 + /-- (gen 12) The Stage-1 page tables `VsRelA` relates (VS gen 2 Q2: a subset of A64 P1's `VsTy` `Tb`, which also + holds the high windows' level-0 tables and kernel tables). -/ + S1 : Set U64 + /-- (gen 12) Their depths (0 = a root, 3 = the last level; A64 P1's `Lv`). -/ + Lv : U64 → Nat + /-- (gen 18, ruling 18:13Z item 4) The Stage-2 page tables `ExtVsRelA` relates (the `Cap::PageTable`s mapped below + a Stage-2 root; ExtVsA gen 1's ghost `T2`). -/ + T2 : Set U64 + /-- (gen 18) The shift of the addresses an entry of a Stage-2 table maps (ExtVsA gen 1's ghost `Sh2`). -/ + Sh2 : U64 → Nat + +/-! ## The scheduler relation -/ + +/-- FL4's thread-state code (`sched.rs` `TS_*`) of a spec thread state (x86 `tsCode`; `ext runningVM` is +`TS_RUNNING_VM = 8`). -/ +def tsCodeA : Fl4SpecA64.ThreadState → Nat + | .inactive => 0 + | .running => 1 + | .restart => 2 + | .blockedOnReceive .. => 3 + | .blockedOnSend .. => 4 + | .blockedOnReply _ => 5 + | .blockedOnNotification _ => 6 + | .idleThreadState => 7 + | .ext .runningVM => 8 + +/-- An FL4 pointer word as an optional spec reference (`0` is `none`; x86 `Notif.optRef`). -/ +def optRefA (w : U64) : Option Fl4SpecA64.ObjRef := if w = 0#u64 then none else some (specPtr w) + +/-- **The spec name of an FL4 SC in the view of core `m.curCore`** (module docstring): that core's idle SC is l4v's +`idle_sc_ptr`, every other SC its address. -/ +def scRenA (m : Fl4SpecA64.McState) (p : U64) : Fl4SpecA64.ObjRef := + if specPtr p = m.idleScOf m.curCore then Fl4SpecA64.idleScPtr else specPtr p + +/-- **The renaming is injective** off `idle_sc_ptr` (ruling 08:46Z item 2): two FL4 pointers, each the view's idle SC +or off `idle_sc_ptr`, with the same spec name are equal. -/ +theorem scRenA_inj (m : Fl4SpecA64.McState) {p q : U64} + (hp : specPtr p ≠ m.idleScOf m.curCore → specPtr p ≠ Fl4SpecA64.idleScPtr) + (hq : specPtr q ≠ m.idleScOf m.curCore → specPtr q ≠ Fl4SpecA64.idleScPtr) (h : scRenA m p = scRenA m q) : p = q := by + unfold scRenA at h + by_cases ep : specPtr p = m.idleScOf m.curCore + · by_cases eq : specPtr q = m.idleScOf m.curCore + · exact specPtr_inj (ep.trans eq.symm) + · rw [if_pos ep, if_neg eq] at h; exact absurd h.symm (hq eq) + · by_cases eq : specPtr q = m.idleScOf m.curCore + · rw [if_neg ep, if_pos eq] at h; exact absurd h (hp ep) + · rw [if_neg ep, if_neg eq] at h; exact specPtr_inj h + +/-- An FL4 SC word as an optional spec SC reference, renamed. -/ +def optScA (m : Fl4SpecA64.McState) (w : U64) : Option Fl4SpecA64.ObjRef := + if w = 0#u64 then none else some (scRenA m w) + +/-- FL4's `SCHED_ACTION` word (`ACTION_RESUME_CURRENT = 0`, `ACTION_CHOOSE_NEW = 1`, else the thread to switch to) +as the spec's `scheduler_action`. -/ +def actOfA (w : U64) : Fl4SpecA64.SchedulerAction := + if w = 0#u64 then .resumeCurThread else if w = 1#u64 then .chooseNewThread else .switchThread (specPtr w) + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **The scheduler relation, multi-core form** (module docstring). -/ +structure SchedRelA (T C Cs : Set U64) (curOf curScOf : Fl4SpecA64.CoreId → Fl4SpecA64.ObjRef) (s : S) + (σ : Fl4SpecA64.State Unit) (m : Fl4SpecA64.McState) : Prop where + /-- The view is the current core's. -/ + core : m.curCore = (L.cpu s).val + cpuCs : L.cpu s ∈ Cs + /-- Every core's current thread is its node's `CUR_THREAD` (ruling 08:46Z item 1). -/ + curOfW : ∀ c ∈ Cs, curOf c.val = specPtr (L.mem s (L.nodeAt s c layout.node.CUR_THREAD)) + /-- Every core's current SC is its node's `CUR_SC`, renamed. -/ + curScOfW : ∀ c ∈ Cs, curScOf c.val = scRenA m (L.mem s (L.nodeAt s c layout.node.CUR_SC)) + /-- The view's current thread and SC are its core's. -/ + cur : curOf m.curCore = σ.curThread + csc : curScOf m.curCore = σ.curSc + idle : σ.idleThread = specPtr (L.idleW s) + /-- Every core's idle thread is `m`'s (FL4 node `IDLE_THREAD`). -/ + idleOf : ∀ c ∈ Cs, m.idleThreadOf c.val = specPtr (L.mem s (L.nodeAt s c layout.node.IDLE_THREAD)) + /-- Every core's idle SC is `m`'s (FL4 node `IDLE_SC`). -/ + idleScOf : ∀ c ∈ Cs, m.idleScOf c.val = specPtr (L.mem s (L.nodeAt s c layout.node.IDLE_SC)) + act : σ.schedulerAction = actOfA (L.actionW s) + time : σ.curTime = (L.curTime s).bv + /-- Every TCB's affinity is its `AFFINITY` word, and only TCBs have one. -/ + aff : ∀ t ∈ T, m.tcbAffinity (specPtr t) = some (L.mem s (affA t)).val + affT : ∀ p c, m.tcbAffinity p = some c → ∃ t ∈ T, specPtr t = p + /-- Every TCB of `T` is a spec TCB with FL4's state code and SC (renamed). -/ + tcb : ∀ t ∈ T, ∃ x, σ.kheap (specPtr t) = some (.tcb x) ∧ (L.mem s (stA t)).val = tsCodeA x.tcbState ∧ + x.tcbSchedContext = optScA m (L.mem s (scA t)) + /-- Every SC of `C` is a spec SC (renamed) whose `scTcb` is FL4's `TCB` word. -/ + sc : ∀ c ∈ C, ∃ y n, σ.kheap (scRenA m c) = some (.schedContext y n) ∧ y.scTcb = optRefA (L.mem s (wa c 24#u64)) + /-- No SC but the view's idle SC is at `idle_sc_ptr` (the renaming is injective on `C`; ruling 08:46Z item 2). -/ + scOff : ∀ c ∈ C, specPtr c ≠ m.idleScOf m.curCore → specPtr c ≠ Fl4SpecA64.idleScPtr + +/-- The view's current thread is the current core's `CUR_THREAD` (x86 `SchedRel.cur`). -/ +theorem SchedRelA.curThread {T C Cs : Set U64} {curOf curScOf : Fl4SpecA64.CoreId → Fl4SpecA64.ObjRef} {s : S} + {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} (h : SchedRelA (L := L) T C Cs curOf curScOf s σ m) : + σ.curThread = specPtr (L.curW s) := by + rw [← h.cur, h.core]; exact h.curOfW _ h.cpuCs + +/-- The view's current SC is the current core's `CUR_SC`, renamed. -/ +theorem SchedRelA.curScW {T C Cs : Set U64} {curOf curScOf : Fl4SpecA64.CoreId → Fl4SpecA64.ObjRef} {s : S} + {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} (h : SchedRelA (L := L) T C Cs curOf curScOf s σ m) : + σ.curSc = scRenA m (L.cscW s) := by + rw [← h.csc, h.core]; exact h.curScOfW _ h.cpuCs + +/-- **The rest of the scheduler relation** (gen 3, ruling 09:59Z item 3; x86 `Sched.SchedSpec`'s `rq`, `rqDom`, +`rel`, `consumed`, `repr`, `curDom`, `domTime`, `tcb`'s domain / priority, `scMem`, and `ScRelW`'s `sc_refill_max`): +a sibling of `SchedRelA` over the same ghosts, so `SchedRelA`'s signature stays. The spec's ready queues of domain 0 +and its release queue are the **current core's** ghost lists (`Z.Q (L.cpu s)`, `Z.R (L.cpu s)`; the other cores' +lists are other views' queues), other domains' queues are empty (FL4 has one domain, as on x86_64); the consumed +time and reprogram flag are the current core's node words `CONSUMED` (+40) / `REPROGRAM` (+56); the current domain +and domain time are the kernel-global `CUR_DOMAIN` (+8) / `DOMAIN_TIME` (+16) words; each TCB's domain is 0 and its +priority FL4's `PRIO` word; a TCB's nonzero SC word names an SC of `C`; each SC's `sc_refill_max` is its +`REFILL_MAX` word (+72). The ghost lists are tied to FL4's queue words by A64 P1's `SchedQueuesInv` at entry level +(`JEntA.sq`). -/ +structure SchedRestA (Z : XSetsA) (s : S) (σ : Fl4SpecA64.State Unit) (m : Fl4SpecA64.McState) : Prop where + rq : ∀ p : U64, p.val < 256 → σ.readyQueues 0 (BitVec.ofNat 8 p.val) = (Z.Q (L.cpu s) p).map specPtr + rqDom : ∀ d p, d ≠ 0 → σ.readyQueues d p = [] + rel : σ.releaseQueue = (Z.R (L.cpu s)).map specPtr + consumed : σ.consumedTime = (L.mem s (L.nodeAt s (L.cpu s) layout.node.CONSUMED)).bv + repr : σ.reprogramTimer = decide (L.mem s (L.nodeAt s (L.cpu s) layout.node.REPROGRAM) ≠ 0#u64) + curDom : σ.curDomain = 0 ∧ L.mem s (wa (L.ksb s) 8#u64) = 0#u64 + domTime : σ.domainTime = (L.mem s (wa (L.ksb s) 16#u64)).bv + prio : ∀ t ∈ Z.T, ∃ x, σ.kheap (specPtr t) = some (.tcb x) ∧ x.tcbDomain = 0 ∧ L.mem s (domA t) = 0#u64 ∧ + (L.mem s (prioA t)).val = x.tcbPriority.toNat + scMem : ∀ t ∈ Z.T, L.mem s (scA t) ≠ 0#u64 → L.mem s (scA t) ∈ Z.C + refillMax : ∀ c ∈ Z.C, ∃ y n, σ.kheap (scRenA m c) = some (.schedContext y n) ∧ + y.scRefillMax = (L.mem s (wa c 72#u64)).val + +end + +/-! ## The machine and arch relations -/ + +/-- The value `getCurrentTime` computes at a machine state (x86 `gctNat`). -/ +def gctNatA (ms : Fl4SpecA64.MachineState) : Nat := + min (Fl4SpecA64.unat (-(Fl4SpecA64.getCurrentTimeBuffer + 1))) + (Fl4SpecA64.unat ms.lastMachineTime + Fl4SpecA64.timeOracle (ms.timeState + 1)) + +/-- The value `getCurrentTime` returns (x86 `gctVal`). -/ +def gctValA (ms : Fl4SpecA64.MachineState) : BitVec 64 := BitVec.ofNat 64 (gctNatA ms) + +/-- The machine state after `getCurrentTime` (x86 `gctState`). -/ +def gctStateA (ms : Fl4SpecA64.MachineState) : Fl4SpecA64.MachineState := + { ms with timeState := ms.timeState + 1, lastMachineTime := gctValA ms } + +section +variable {A : Type} (arI : machine.Arch A) + +/-- **The platform reads the S3 relation uses** [P] (module docstring; x86 `MachPlatC` without `irqPending` / +`mask1`). `MR a ms`: the arch state `a` and the spec machine state `ms` agree on what the platform reads. -/ +structure MachPlatCA (MR : A → Fl4SpecA64.MachineState → Prop) : Prop where + now : ∀ a ms, MR a ms → arI.now a ⦃ r => r.1.bv = gctValA ms ∧ MR r.2 (gctStateA ms) ⦄ + frame : ∀ a ms ms', MR a ms → ms'.irqMasks = ms.irqMasks → ms'.irqState = ms.irqState → + ms'.timeState = ms.timeState → ms'.lastMachineTime = ms.lastMachineTime → MR a ms' + +end + +/-! ## The IRQ relation -/ + +/-- Offset of IRQ `i`'s state word in the kernel-state block (`interrupt::state_off`: `ks::IRQ_STATE + 8 i`, +`IRQ_STATE` = 224 on AArch64; x86 `irqOff`). -/ +def irqOffA (i : U64) : U64 := wa 224#u64 (core.num.U64.wrapping_mul i 8#u64) + +/-- Address of IRQ `i`'s state word (x86 `irqStA`). -/ +def irqWordA (ksb i : U64) : U64 := wa ksb (irqOffA i) + +/-- FL4's IRQ state word as the spec's state (`interrupt.rs`: 0 inactive, 1 signal, 2 timer; FL4's IPI state 3 and +`IRQ_RESERVED` = 4 are reserved; x86 `irqSt`). -/ +def irqStOfA (w : U64) : Fl4SpecA64.IrqState := + if w = 0#u64 then .irqInactive else if w = 1#u64 then .irqSignal else if w = 2#u64 then .irqTimer + else .irqReserved + +/-- **The IRQ relation** (gen 3, ruling 09:59Z item 2; x86 `IrqRel`, there for 128 IRQs): for each of FL4's 5376 +IRQs, the spec's `interrupt_states` is FL4's state word and the spec's IRQ node slot is FL4's IRQ-node slot +`ksb + 43232 + 32 i` (a one-slot CNode of `th`, `PlaceA.irq`, so the CSpace relation covers its cap). The spec IRQ +is the 13-bit `irq` of `i`. -/ +structure IrqRelA (mem : U64 → U64) (ksb : U64) (σ : Fl4SpecA64.State Unit) : Prop where + st : ∀ i : U64, i.val < 5376 → σ.interruptStates (BitVec.ofNat 13 i.val) = irqStOfA (mem (irqWordA ksb i)) + node : ∀ i : U64, i.val < 5376 → + σ.interruptIrqNode (BitVec.ofNat 13 i.val) = specPtr (Fl4ProofsA64.P1.irqSlotA ksb i) + /-- (record edit #5) The spec's IRQ node above FL4's 5376 IRQs is the boot one (never written). -/ + nodeHi : ∀ i : Fl4SpecA64.Irq, 5376 ≤ i.toNat → + σ.interruptIrqNode i = specPtr (Fl4ProofsA64.P1.irqSlotA ksb ⟨BitVec.ofNat 64 i.toNat⟩) + +theorem irqStOfA_inactive (w : U64) : irqStOfA w = .irqInactive ↔ w = 0#u64 := by + unfold irqStOfA; split_ifs <;> simp_all + +/-- An IRQ state word by value (x86 `irqStA_val`). -/ +theorem irqWordA_val {kb i : U64} (hf : kb.val + ksSizeA ≤ 2 ^ 64) (hi : i.val < 5376) : + (irqWordA kb i).val = kb.val + 224 + 8 * i.val := by + unfold irqWordA irqOffA + unfold ksSizeA at hf + simp [core.num.U64.wrapping_add_val_eq, core.num.U64.wrapping_mul_val_eq, U64.size_def, U64.numBits] + omega + +/-! ## `XTopAtA` and `JAtZA` -/ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-- A pointer of the related objects other than the SCs: `th`'s objects (so `T`'s, `thT`), the VCPUs, the Stage-2 +roots and the high windows. -/ +def InDomA (Z : XSetsA) (p : U64) : Prop := Z.th p ≠ none ∨ p ∈ Z.V ∨ p ∈ Z.X ∨ p ∈ Z.Y + +/-- **The composite relation at the sets `Z`** (module docstring; x86 `XTopAt`'s core). -/ +structure XTopAtA (AR : A → HwRootA → Prop) (Z : XSetsA) (s : S) (a : A) + (σ : Fl4SpecA64.State Unit) (m : Fl4SpecA64.McState) : Prop where + st : StateRelA Z.th (L.mem s) σ + cdt : CdtRelA (L := L) _ Z.th s σ Z.G + sch : SchedRelA (L := L) Z.T Z.C Z.Cs Z.curOf Z.curScOf s σ m + ar : AR a Z.hw + place : PlaceA Z.th (L.ksb s) + /-- (gen 3) The IRQ states and IRQ node (x86 `VsP.irq`). -/ + irq : IrqRelA (L.mem s) (L.ksb s) σ + /-- (gen 3) The rest of the scheduler relation. -/ + rest : SchedRestA (L := L) Z s σ m + +/-- **The A64 joint relation at the ghosts** (module docstring; x86 `JAtZB`'s core, flat). -/ +structure JAtZA (AR : A → HwRootA → Prop) (Z : XSetsA) (s : S) (a : A) + (σ : Fl4SpecA64.State Unit) (m : Fl4SpecA64.McState) : Prop extends XTopAtA (L := L) AR Z s a σ m where + /-- The typed heap's TCBs are the scheduler's (x86 `thT`, both ways). -/ + thT : ∀ t, Z.th t = some .tcb ↔ t ∈ Z.T + /-- The cores are `0 .. n - 1`, at most 128 (`MAX_NUM_NODES`; x86 `JKern.csN`, `csLt`, `cs0`). -/ + csN : ∃ n, 0 < n ∧ n ≤ 128 ∧ ∀ c : U64, c ∈ Z.Cs ↔ c.val < n + /-- Each core's idle thread, idle SC, current thread and current SC are in the sets. -/ + idleT : ∀ c ∈ Z.Cs, L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) ∈ Z.T + idleC : ∀ c ∈ Z.Cs, L.mem s (L.nodeAt s c layout.node.IDLE_SC) ∈ Z.C + curT : ∀ c ∈ Z.Cs, L.mem s (L.nodeAt s c layout.node.CUR_THREAD) ∈ Z.T + cscC : ∀ c ∈ Z.Cs, L.mem s (L.nodeAt s c layout.node.CUR_SC) ∈ Z.C + /-- The kernel-state block does not wrap (x86 `JKern.ksbFit`). -/ + ksFit : (L.ksb s).val + ksSizeA ≤ 2 ^ 64 + /-- No object of `th`, `V`, `X` or `Y` but the view's idle SC is at `idle_sc_ptr` (ruling 08:46Z item 2: with + `SchedRelA.scOff`, `idle_sc_ptr ∉ dom \ {m.idleScOf m.curCore}`, so no two objects alias in `σ.kheap`). -/ + scOffD : ∀ p, InDomA Z p → specPtr p ≠ m.idleScOf m.curCore → specPtr p ≠ Fl4SpecA64.idleScPtr + /-- (gen 9) **Every slot's CNode / thread cap names a CNode / TCB of `th`** (`CapValidA`; x86 `SlotsValid`): what + the CSpace walk (`P3/LookupA`) needs to read the CNodes it reaches through the CSpace relation. -/ + cv : ∀ y, IsSlotA Z.th y → CapValidA Z.th (capAtA (L.mem s) y) + /-- A root cap names a depth-0 Stage-1 table, including before ASID assignment. -/ + pml : ∀ y, IsSlotA Z.th y → ∀ p mp asid, capAtA (L.mem s) y = .Pml4 p mp asid → p ∈ Z.S1 ∧ Z.Lv p = 0 + /-- (gen 12, ruling 15:01Z item 3 / 15:08Z) **The VSpace relation** (`P3/VsRelA`, VS gens 1–4; x86 `VsRel` + + `PtRel` inside x86's `JAtZ`): FL4's ASID table, pools `Z.Pp`, Stage-1 tables `Z.S1` (depths `Z.Lv`) and the high + windows `Z.Y` against the spec's ASID table, `ASIDPool` / `PageTable` objects and `ext (highVSpace _)` windows. -/ + vs : VsRelA Z.Pp Z.S1 Z.Lv Z.X Z.Y (L.mem s) (L.ksb s) σ + /-- (gen 18, ruling 18:13Z item 4) **The Stage-2 / high-window relation** (`P3/ExtVsDefA`, ExtVsA gens 1–3; x86 + `EptRel` inside `VmEptState`): FL4's Stage-2 roots `Z.X`, Stage-2 tables `Z.T2` (shifts `Z.Sh2`) and the high + windows' level-0 entries (511 the table descriptor of the level-1 table, `wl0`; 0..255 zero, `wlo`) against the + spec's `stage2Root` objects and normal `PageTable`s. The address half of the integration (`VaOkE` for every mapped + plain frame cap) is `cv` (`CapValidA`'s frame arm). -/ + ext : ExtVsRelA Z.Pp Z.S1 Z.X Z.Y Z.T2 Z.Sh2 (L.mem s) σ + +/-! ## The extension records -/ + +/-- **The VCPU record** (module docstring; x86 `VcpuCore`'s core). -/ +structure VcpuCoreA (Z : XSetsA) (s : S) (σ : Fl4SpecA64.State Unit) : Prop where + obj : ∀ v ∈ Z.V, ∃ x, σ.kheap (specPtr v) = some (.archObj (.vcpu x)) ∧ x.vcpuTcb = optRefA (L.mem s v) ∧ + x.vcpuExt.runCaller = optRefA (L.mem s (wa v 24#u64)) + back : ∀ p x, σ.kheap p = some (.archObj (.vcpu x)) → ∃ v ∈ Z.V, specPtr v = p + tcb : ∀ t ∈ Z.T, ∃ x, σ.kheap (specPtr t) = some (.tcb x) ∧ x.tcbArch.tcbVcpu = optRefA (L.mem s (wa t 3664#u64)) ∧ + x.tcbExt.tcbRunVcpu = optRefA (L.mem s (runVcpuA t)) + cap : ∀ y, IsSlotA Z.th y → ∀ p, capAtA (L.mem s) y = .Vcpu p → p ∈ Z.V + +/-- **The Stage-2 record** (module docstring; x86 `EptCore`'s object part). -/ +structure Stage2CoreA (Z : XSetsA) (s : S) (σ : Fl4SpecA64.State Unit) : Prop where + obj : ∀ r ∈ Z.X, ∃ es, σ.kheap (specPtr r) = some (.archObj (.ext (.stage2Root es))) + back : ∀ p es, σ.kheap p = some (.archObj (.ext (.stage2Root es))) → ∃ r ∈ Z.X, specPtr r = p + cap : ∀ y, IsSlotA Z.th y → ∀ p l mp asid va, capAtA (L.mem s) y = .Ept p l mp asid va → p ∈ Z.X + +/-- **The `ArmHighVSpace` record** (module docstring; no x86 counterpart). -/ +structure HighVSpaceCoreA (Z : XSetsA) (s : S) (σ : Fl4SpecA64.State Unit) : Prop where + obj : ∀ r ∈ Z.Y, ∃ l1, σ.kheap (specPtr r) = some (.archObj (.ext (.highVSpace l1))) + back : ∀ p l1, σ.kheap p = some (.archObj (.ext (.highVSpace l1))) → ∃ r ∈ Z.Y, specPtr r = p + cap : ∀ y, IsSlotA Z.th y → ∀ p mp asid, capAtA (L.mem s) y = .HighVSpace p mp asid → p ∈ Z.Y + +/-- **`JAtZA` with the three extension records** (module docstring; x86 `VmEptState`'s shape). -/ +abbrev JAtZAX (AR : A → HwRootA → Prop) (Z : XSetsA) (s : S) (a : A) + (σ : Fl4SpecA64.State Unit) (m : Fl4SpecA64.McState) : Prop := + JAtZA (L := L) AR Z s a σ m ∧ VcpuCoreA (L := L) Z s σ ∧ Stage2CoreA (L := L) Z s σ ∧ + HighVSpaceCoreA (L := L) Z s σ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/LocalInvokePostA.lean b/proofs/Fl4ProofsA64/P3/LocalInvokePostA.lean new file mode 100644 index 000000000..f71ebdb75 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/LocalInvokePostA.lean @@ -0,0 +1,155 @@ +/- +P3 (AArch64): lift local invocation bodies to FL4's common perform result. +The state relation parameter carries the operation's actual successor +and can include the Stage-3 joint ghost. The caller finish is separate: +SC Consumed and completed YieldTo can already have written the reply. +-/ +import Fl4ProofsA64.P3.IpcInvokeDispatchA +import Fl4ProofsA64.P3.DomainSetDispatchA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4Spec Fl4SpecA64 Sched + +noncomputable section + +/-- The concrete call after its Restart and perform steps. The spec's +reply words remain available for the caller's finish step. -/ +def LocalInvokePostA {S A : Type} (R : S → A → State Unit → Prop) + (block call donate : Bool) (op : Fl4Invocation) (σ : State Unit) + (r : core.result.Result Unit error.Exc × S × A) : Prop := + r.1 = .Ok () ∧ ∃ σ1 σ2 data, + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + (.ok data, σ2) ∈ ((fl4PerformInvocation (Z := Unit) block call donate op).run σ1).1 ∧ + R r.2.1 r.2.2 σ2 + +section +variable {σ σ' : State Unit} {block call donate : Bool} + +theorem performEndpoint_memA {ep : ObjRef} {badge : Badge} {grant grantReply : Bool} + (h : ((), σ') ∈ (sendIpc (Z := Unit) block call badge grant grantReply donate σ.curThread ep σ).1) : + (.ok [], σ') ∈ ((fl4PerformInvocation (Z := Unit) block call donate + (.invokeEndpoint ep badge grant grantReply)).run σ).1 := by + apply mem_liftED + rw [nd_gets_bindSc] + exact mem_bind_ndD h rfl + +theorem performNotification_memA {n : ObjRef} {badge : Badge} + (h : ((), σ') ∈ (sendSignal (Z := Unit) n badge σ).1) : + (.ok [], σ') ∈ ((fl4PerformInvocation (Z := Unit) block call donate + (.invokeNotification n badge)).run σ).1 := by + exact mem_bindE_okD (mem_liftED h) rfl + +theorem performDomain_memA {d : DomainInvocation} + (h : ((), σ') ∈ (invokeDomain (Z := Unit) d σ).1) : + (.ok [], σ') ∈ ((fl4PerformInvocation (Z := Unit) block call donate (.invokeDomain d)).run σ).1 := + mem_liftED (mem_bind_ndD h rfl) + +theorem performSchedContext_memA {op : SchedContextInvocation} {data : List Data} + (h : (data, σ') ∈ (invokeSchedContext (Z := Unit) op σ).1) : + (.ok data, σ') ∈ ((fl4PerformInvocation (Z := Unit) block call donate + (.invokeSchedContext op)).run σ).1 := + mem_liftED h + +end + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +/-- [J] The selected local endpoint arm supplies its concrete successor and +the Stage-3 frame after the actual Restart. -/ +theorem endpoint_local_performA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {a : A} + {σ : State Unit} (H : SchedSRA st Z m κ s σ) (hst : StEqA Z.T (L.mem s) σ) + {x : Tcb} (hkc : σ.kheap σ.curThread = some (.tcb x)) (hr : runnable x.tcbState = true) + {lbl len src buf ep badge : U64} {recv grant grantReply block call donate first : Bool} + {extra : Array obj.Slot 3#usize} {R : S → A → State Unit → Prop} + (step : ∀ s1 σ1, SchedSRA st Z m κ s1 σ1 → StEqA Z.T (L.mem s1) σ1 → + σ1.kheap = (updKSc σ (specPtr (L.curW s)) (.tcb { x with tcbState := .restart })).kheap → + WritesIn st s s1 (fun b => SWrA (st := st) Z s b ∨ b = stA (L.curW s)) → + endpoint.send_ipc st ar s1 a block call badge grant grantReply donate (L.curW s) ep ⦃ r => + ∃ σ2, ((), σ2) ∈ (sendIpc (Z := Unit) block call badge.bv grant grantReply donate σ1.curThread + (specPtr ep) σ1).1 ∧ R r.1 r.2 σ2 ⦄) : + syscall.decode_invocation st ar s a lbl len src (.Endpoint ep badge true recv grant grantReply) + block call donate first buf extra ⦃ LocalInvokePostA R block call donate + (.invokeEndpoint (specPtr ep) badge.bv grant grantReply) σ ⦄ := by + rw [decodeEndpoint_programA] + simp only [↓reduceIte] + let op : SMonad Unit Unit := do + let sender ← NonDet.gets fun s : State Unit => s.curThread + sendIpc block call badge.bv grant grantReply donate sender (specPtr ep) + have hstep : ∀ s1 σ1, SchedSRA st Z m κ s1 σ1 → StEqA Z.T (L.mem s1) σ1 → + σ1.kheap = (updKSc σ (specPtr (L.curW s)) (.tcb { x with tcbState := .restart })).kheap → + WritesIn st s s1 (fun b => SWrA (st := st) Z s b ∨ b = stA (L.curW s)) → + endpoint.send_ipc st ar s1 a block call badge grant grantReply donate (L.curW s) ep ⦃ r => + ∃ σ2, ((), σ2) ∈ (op σ1).1 ∧ R r.1 r.2 σ2 ⦄ := by + intro s1 σ1 h1 h2 hk hw + simpa only [op, nd_gets_bindSc] using step s1 σ1 h1 h2 hk hw + refine WP.spec_mono (ipcInvokeProgram_refinesA H hst H.curT H.sch.curThread hkc hr hstep) ?_ + rintro r ⟨he, σ1, σ2, h1, h2, hR⟩ + have h2' : ((), σ2) ∈ (sendIpc (Z := Unit) block call badge.bv grant grantReply donate σ1.curThread + (specPtr ep) σ1).1 := by simpa only [op, nd_gets_bindSc] using h2 + exact ⟨he, σ1, σ2, [], h1, performEndpoint_memA h2', hR⟩ + +/-- [J] The notification arm is selected at the exact Restart state. Its +local wake or badge update supplies the corresponding joint successor. -/ +theorem notification_local_performA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {a : A} + {σ : State Unit} (H : SchedSRA st Z m κ s σ) (hst : StEqA Z.T (L.mem s) σ) + {x : Tcb} (hkc : σ.kheap σ.curThread = some (.tcb x)) (hr : runnable x.tcbState = true) + {lbl len src buf n badge : U64} {recv block call donate first : Bool} + {extra : Array obj.Slot 3#usize} {R : S → A → State Unit → Prop} + (step : ∀ s1 σ1, SchedSRA st Z m κ s1 σ1 → StEqA Z.T (L.mem s1) σ1 → + σ1.kheap = (updKSc σ (specPtr (L.curW s)) (.tcb { x with tcbState := .restart })).kheap → + WritesIn st s s1 (fun b => SWrA (st := st) Z s b ∨ b = stA (L.curW s)) → + notification.send_signal st ar s1 a n badge ⦃ r => + ∃ σ2, ((), σ2) ∈ (sendSignal (Z := Unit) (specPtr n) badge.bv σ1).1 ∧ R r.1 r.2 σ2 ⦄) : + syscall.decode_invocation st ar s a lbl len src (.Notification n badge true recv) + block call donate first buf extra ⦃ LocalInvokePostA R block call donate + (.invokeNotification (specPtr n) badge.bv) σ ⦄ := by + rw [decodeNotification_programA] + simp only [↓reduceIte] + refine WP.spec_mono (ipcInvokeProgram_refinesA H hst H.curT H.sch.curThread hkc hr step) ?_ + rintro r ⟨he, σ1, σ2, h1, h2, hR⟩ + exact ⟨he, σ1, σ2, [], h1, performNotification_memA h2, hR⟩ + +/-- Transport a ratified SC body and its return data through the cap-kind +dispatcher. Early-finishing SC cases can carry their separate finish proof. -/ +theorem sc_local_performA {s : S} {a : A} {σ : State Unit} + {lbl len src buf sc size : U64} {block call donate : Bool} + {extra : Array obj.Slot 3#usize} {op : SchedContextInvocation} {R : S → A → State Unit → Prop} + (h : schedcontext.decode st ar s a lbl sc call extra ⦃ r => r.1 = .Ok () ∧ ∃ σ1 σ2 data, + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + (data, σ2) ∈ (invokeSchedContext (Z := Unit) op σ1).1 ∧ R r.2.1 r.2.2 σ2 ⦄) : + syscall.decode_invocation st ar s a lbl len src (.SchedContext sc size) block call donate false buf extra + ⦃ LocalInvokePostA R block call donate (.invokeSchedContext op) σ ⦄ := by + rw [decodeSc_programA] + simp only [Bool.false_eq_true, ↓reduceIte] + refine WP.spec_mono h ?_ + rintro r ⟨he, σ1, σ2, data, h1, h2, hR⟩ + exact ⟨he, σ1, σ2, data, h1, performSchedContext_memA h2, hR⟩ + +/-- Transfer the one-domain local DomainSet result to the same +perform post as IPC and scheduling-context invocations. -/ +theorem domainSet_local_performA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {a : A} + {σ : State Unit} (H : SchedSRA st Z m κ s σ) (hs : StEqA Z.T (L.mem s) σ) + {len src buf x0 t : U64} {extra : Array obj.Slot 3#usize} {block call donate : Bool} + (ht : t ∈ Z.T) (hon : L.mem s (affA t) = L.cpu s) + (hstall : smp.remote_tcb_stall st ar s a t = ok (s, a)) + {xc : Tcb} (hc : σ.kheap (specPtr (L.curW s)) = some (.tcb xc)) + (hr : runnable xc.tcbState = true) + (hl : len ≠ 0#u64) (hd : msg.syscall_arg st s (L.curW s) buf 0#u64 = ok 0#u64) + (he : Array.index_usize extra 0#usize = ok x0) (hx : x0 ≠ 0#u64) + (hextra : obj.Slot.cap st x0 s = ok (.Thread t)) : + syscall.decode_invocation st ar s a fl4_abi.label.DomainSetSet len src .Domain block call donate + false buf extra ⦃ LocalInvokePostA (fun s' a' σ' => a' = a ∧ ∃ Q', + SchedSRA st { Z with Q := Q' } m κ s' σ' ∧ StEqA Z.T (L.mem s') σ') + block call donate (.invokeDomain (.invokeDomainSet (specPtr t) 0)) σ ⦄ := by + refine WP.spec_mono (decodeInvocation_domainSet_localA H hs ht hon hstall hc hr hl hd he hx hextra) ?_ + rintro r ⟨hr, har, σ1, σ2, Q', hR, hD, H2, hs2⟩ + refine ⟨hr, σ1, σ2, [], hR, performDomain_memA ?_, har, Q', H2, hs2⟩ + rw [hD] + rfl + +end +end +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/LocalIpcInvokeA.lean b/proofs/Fl4ProofsA64/P3/LocalIpcInvokeA.lean new file mode 100644 index 000000000..6dc5debc7 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/LocalIpcInvokeA.lean @@ -0,0 +1,174 @@ +/- +P3 (AArch64): invoke local IPC bodies at the reached Restart state. + +[J] The restart proof supplies the phase relation, including the actual +current thread, IPC queues and selected capability. Each local body +uses its own branch selector and returns its actual IPC successor. +-/ +import Fl4ProofsA64.P3.InvokeRestartJointA +import Fl4ProofsA64.P3.IpcScGlueA +import Fl4ProofsA64.P3.NtfnSendA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4SpecA64 + +noncomputable section + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +/-- Compose the perform stage directly with an exact Restart post. -/ +theorem localInvokeAtRestartA {s : S} {σ : State Unit} + {J : S → State Unit → Prop} {R : S → A → State Unit → Prop} + {op : Fl4Invocation} {blk call donate : Bool} {body : S → Result (S × A)} + (restart : sched.set_thread_state st s (L.curW s) sched.TS_RESTART ⦃ s1 => + ∃ σ1, setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ J s1 σ1 ⦄) + (perform : ∀ s1 σ1, J s1 σ1 → body s1 ⦃ r => ∃ σ2 data, + (.ok data, σ2) ∈ ((fl4PerformInvocation (Z := Unit) blk call donate op).run σ1).1 ∧ + R r.1 r.2 σ2 ⦄) : + ipcInvokeProgramA st s body ⦃ LocalInvokePostA R blk call donate op σ ⦄ := by + simp only [ipcInvokeProgramA, cur_thread_ok, bind_ok] + refine WP.spec_bind restart ?_ + rintro s1 ⟨σ1, step, joint⟩ + refine WP.spec_bind (perform s1 σ1 joint) ?_ + rintro ⟨s2, a2⟩ ⟨σ2, data, invoke, post⟩ + exact (WP.spec_ok _).2 ⟨rfl, σ1, σ2, data, step, invoke, post⟩ + +/-- When an endpoint has no receiver, the reached relation selects +either the blocking queue append or the nonblocking identity. -/ +theorem endpointNoReceiver_localA {s : S} {a : A} {σ : State Unit} + {T E R : Set U64} {Bf : Set (U64 × Word)} {SR : S → State Unit → Prop} + {Wr : State Unit → S → U64 → Prop} + (ops : Endp.SchedOpsEA st T E R Bf SR) (hF : Endp.SchedFootEA st T E R Bf SR Wr) + (place : Endp.EPlaced T E R Bf) + {lbl len src buf e badge : U64} {recv grant grantReply block call donate first : Bool} + {extra : Array obj.Slot 3#usize} {J : S → State Unit → Prop} + (restart : sched.set_thread_state st s (L.curW s) sched.TS_RESTART ⦃ s1 => + ∃ σ1, setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ J s1 σ1 ⦄) + (ep : e ∈ E) (thread : L.curW s ∈ T) + (selected : ∀ s1 σ1, J s1 σ1 → + Endp.EpStateRel T E R Bf (L.mem s1) σ1 ∧ SR s1 σ1 ∧ σ1.curThread = specPtr (L.curW s) ∧ + (∃ x, σ1.kheap (specPtr e) = some (.endpoint x) ∧ Endp.CanBlockA false x) ∧ + (block = true → (∃ tcb, σ1.kheap (specPtr (L.curW s)) = some (.tcb tcb) ∧ + Endp.SndRunA σ1 (specPtr (L.curW s)) tcb) ∧ + ∀ xs, QueueRel (L.mem s1) T .Ep (Notif.qOf (L.mem s1) e) xs → + PrioSorted (fun y => L.mem s1 (prioA y)) xs)) : + syscall.decode_invocation st ar s a lbl len src (.Endpoint e badge true recv grant grantReply) + block call donate first buf extra ⦃ LocalInvokePostA + (fun s2 a2 σ2 => a2 = a ∧ Endp.EpStateRel T E R Bf (L.mem s2) σ2 ∧ SR s2 σ2) + block call donate (.invokeEndpoint (specPtr e) badge.bv grant grantReply) σ ⦄ := by + rw [decodeEndpoint_programA] + simp only [↓reduceIte] + refine localInvokeAtRestartA restart ?_ + intro s1 σ1 hs1 + obtain ⟨HE, HS, hc, ⟨x, hx, hb⟩, active⟩ := selected s1 σ1 hs1 + cases block + · refine WP.spec_mono (Endp.send_ipc_noblockA s1 a σ1 HE ep hx hb call badge + grant grantReply donate (L.curW s)) ?_ + rintro r ⟨rfl, h⟩ + refine ⟨σ1, [], performEndpoint_memA ?_, rfl, HE, HS⟩ + rw [hc, h] + rfl + · obtain ⟨⟨tcb, htcb, sndRun⟩, sorted⟩ := active rfl + refine WP.spec_mono (Endp.send_ipc_blockA ops hF place s1 a σ1 HE HS thread htcb sndRun + ep hx hb sorted call badge grant grantReply donate) ?_ + rintro r ⟨har, σ2, h, HE', HS', -⟩ + refine ⟨σ2, [], performEndpoint_memA ?_, har, HE', HS'⟩ + rw [hc, h] + rfl + +/-- An already active notification returns the matched badge update at +the exact Restart state. -/ +theorem notificationActive_localA {s : S} {a : A} {σ : State Unit} + {N T : Set U64} (place : Notif.NPlaced T N) + {lbl len src buf n badge : U64} {recv block call donate first : Bool} + {extra : Array obj.Slot 3#usize} {J : S → State Unit → Prop} + (restart : sched.set_thread_state st s (L.curW s) sched.TS_RESTART ⦃ s1 => + ∃ σ1, setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ J s1 σ1 ⦄) + (hn : n ∈ N) + (selected : ∀ s1 σ1, J s1 σ1 → + Notif.NtfnStateRel N T (L.mem s1) σ1 ∧ + ∃ x b, σ1.kheap (specPtr n) = some (.notification x) ∧ x.ntfnObj = .activeNtfn b) : + syscall.decode_invocation st ar s a lbl len src (.Notification n badge true recv) + block call donate first buf extra ⦃ LocalInvokePostA + (fun s2 a2 σ2 => a2 = a ∧ Notif.NtfnStateRel N T (L.mem s2) σ2) + block call donate (.invokeNotification (specPtr n) badge.bv) σ ⦄ := by + rw [decodeNotification_programA] + simp only [↓reduceIte] + refine localInvokeAtRestartA restart ?_ + intro s1 σ1 hs1 + obtain ⟨HN, x, b, hx, hb⟩ := selected s1 σ1 hs1 + refine WP.spec_mono (Notif.send_signal_active place s1 a σ1 HN hn hx hb badge) ?_ + rintro r ⟨har, σ2, step, HN', _, _⟩ + refine ⟨σ2, [], performNotification_memA ?_, har, HN'⟩ + rw [step] + rfl + +/-- The idle notification stores the first badge, including the case of +a bound TCB that is neither receiving nor running a guest. -/ +theorem notificationIdle_localA {s : S} {a : A} {σ : State Unit} + {N T : Set U64} (place : Notif.NPlaced T N) + {lbl len src buf n badge : U64} {recv block call donate first : Bool} + {extra : Array obj.Slot 3#usize} {J : S → State Unit → Prop} + (restart : sched.set_thread_state st s (L.curW s) sched.TS_RESTART ⦃ s1 => + ∃ σ1, setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ J s1 σ1 ⦄) + (hn : n ∈ N) + (selected : ∀ s1 σ1, J s1 σ1 → + Notif.NtfnStateRel N T (L.mem s1) σ1 ∧ + ∃ x, σ1.kheap (specPtr n) = some (.notification x) ∧ x.ntfnObj = .idleNtfn ∧ + (x.ntfnBoundTcb = none ∨ ∃ t xt, x.ntfnBoundTcb = some t ∧ + σ1.kheap t = some (.tcb xt) ∧ receiveBlocked xt.tcbState = false ∧ + xt.tcbState ≠ .ext .runningVM)) : + syscall.decode_invocation st ar s a lbl len src (.Notification n badge true recv) + block call donate first buf extra ⦃ LocalInvokePostA + (fun s2 a2 σ2 => a2 = a ∧ Notif.NtfnStateRel N T (L.mem s2) σ2) + block call donate (.invokeNotification (specPtr n) badge.bv) σ ⦄ := by + rw [decodeNotification_programA] + simp only [↓reduceIte] + refine localInvokeAtRestartA restart ?_ + intro s1 σ1 hs1 + obtain ⟨HN, x, hx, idle, bound⟩ := selected s1 σ1 hs1 + have send : notification.send_signal st ar s1 a n badge ⦃ r => + r.2 = a ∧ Notif.SingleRel (st := st) N T s1 σ1 (sendSignal (specPtr n) badge.bv σ1) r.1 ⦄ := by + rcases bound with h | ⟨t, xt, hb, ht, hr, hv⟩ + · exact Notif.send_signal_idle_unbound place s1 a σ1 HN hn hx idle h badge + · exact Notif.send_signal_idle_bound_idle place s1 a σ1 HN hn hx idle hb ht hr hv badge + refine WP.spec_mono send ?_ + rintro r ⟨har, σ2, step, HN', _, _⟩ + refine ⟨σ2, [], performNotification_memA ?_, har, HN'⟩ + rw [step] + rfl + +/-- Wake the first waiting TCB with the selected scheduler operation at +the reached Restart state. -/ +theorem notificationWaiting_localA {s : S} {a : A} {σ : State Unit} + {N T : Set U64} {SR : S → State Unit → Prop} {DL : S → U64 → Prop} + (hyps : Notif.SchedHypsA st N T SR) (ops : Notif.SchedOpsA st ar N T SR DL) + (place : Notif.NPlaced T N) + {lbl len src buf n badge : U64} {recv block call donate first : Bool} + {extra : Array obj.Slot 3#usize} {J : S → State Unit → Prop} + (restart : sched.set_thread_state st s (L.curW s) sched.TS_RESTART ⦃ s1 => + ∃ σ1, setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ J s1 σ1 ⦄) + (hn : n ∈ N) + (selected : ∀ s1 σ1, J s1 σ1 → + Notif.NtfnStateRel N T (L.mem s1) σ1 ∧ SR s1 σ1 ∧ DL s1 n ∧ + ∃ x q, σ1.kheap (specPtr n) = some (.notification x) ∧ x.ntfnObj = .waitingNtfn q ∧ + (∀ d ∈ T, specPtr d ∈ q → L.mem s1 (affA d) = L.cpu s1)) : + syscall.decode_invocation st ar s a lbl len src (.Notification n badge true recv) + block call donate first buf extra ⦃ LocalInvokePostA + (fun s2 _ σ2 => Notif.NtfnStateRel N T (L.mem s2) σ2 ∧ SR s2 σ2) + block call donate (.invokeNotification (specPtr n) badge.bv) σ ⦄ := by + rw [decodeNotification_programA] + simp only [↓reduceIte] + refine localInvokeAtRestartA restart ?_ + intro s1 σ1 hs1 + obtain ⟨HN, HS, Hd, x, q, hx, hq, local'⟩ := selected s1 σ1 hs1 + refine WP.spec_mono (Notif.send_signal_waiting hyps ops place s1 a σ1 HN HS hn hx hq badge local' Hd) ?_ + rintro r ⟨_, σ2, step, HN', HS'⟩ + exact ⟨σ2, [], performNotification_memA step, HN', HS'⟩ + +end +end +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/LocalIpcRecvInvokeA.lean b/proofs/Fl4ProofsA64/P3/LocalIpcRecvInvokeA.lean new file mode 100644 index 000000000..321283895 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/LocalIpcRecvInvokeA.lean @@ -0,0 +1,177 @@ +/- +P3 (AArch64): the remaining local IPC invocations at the reached Restart. + +An endpoint with a waiting receiver (without and with selected local SC +donation) and an idle notification bound to a receive-blocked TCB. +Each reuses the ratified body theorem and returns its actual successor +through `LocalInvokePostA`. The caller supplies the reached branch facts +as `selected` at the exact post-Restart state. +-/ +import Fl4ProofsA64.P3.LocalIpcInvokeA +import Fl4ProofsA64.P3.LocalInvokePostA +import Fl4ProofsA64.P3.IpcScSendPushA +import Fl4ProofsA64.P3.EpSendRecvA +import Fl4ProofsA64.P3.IpcCancelA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4SpecA64 Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched + +noncomputable section + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +/-- Endpoint with a waiting receiver and no selected reply donation. -/ +theorem endpointRecv_localA {s : S} {a : A} {σ : State Unit} + {T E R : Set U64} {Bf : Set (U64 × Word)} {SR : S → State Unit → Prop} + {Wr : State Unit → S → U64 → Prop} + (ops : Endp.SchedOpsEA st T E R Bf SR) (hF : Endp.SchedFootEA st T E R Bf SR Wr) + (place : Endp.EPlaced T E R Bf) + {lbl len src buf e badge : U64} {recv grant grantReply block call donate first : Bool} + {extra : Array obj.Slot 3#usize} {J : S → State Unit → Prop} + (restart : sched.set_thread_state st s (L.curW s) sched.TS_RESTART ⦃ s1 => + ∃ σ1, setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ J s1 σ1 ⦄) + (ep : e ∈ E) (thread : L.curW s ∈ T) + (selected : ∀ s1 σ1, J s1 σ1 → σ1.curThread = specPtr (L.curW s) ∧ + ∃ (th : TypedHeapA) (kb : U64), PlaceA th kb ∧ + Endp.EpStateRel T E R Bf (L.mem s1) σ1 ∧ SR s1 σ1 ∧ StateRelA th (L.mem s1) σ1 ∧ + SlotsValidA th (L.mem s1) ∧ Endp.BufCover st T Bf s1 σ1 ∧ Endp.BufNativeA T (L.mem s1) ∧ + (∀ y, IsSlotA th y → ¬ Endp.EWrite T E R Bf y ∧ + ¬ Endp.EWrite T E R Bf (core.num.U64.wrapping_add y 8#u64)) ∧ + th (L.curW s) = some .tcb ∧ + ∃ xt, σ1.kheap (specPtr (L.curW s)) = some (.tcb xt) ∧ tsCode xt.tcbState ≠ 3 ∧ + ∃ (d : U64) (q : List ObjRef), + σ1.kheap (specPtr e) = some (.endpoint (.recvEP (specPtr d :: q))) ∧ + th d = some .tcb ∧ + ∃ xd, σ1.kheap (specPtr d) = some (.tcb xd) ∧ + (∀ rp, ipcBufOfA true xd = some rp → ∀ k, k < 128 → inUserFrame (bufAddr rp k) σ1) ∧ + RepOKA σ1 (specPtr d) ∧ L.mem s1 (affA d) = L.cpu s1 ∧ + ((xt.tcbFault = none ∧ (grant = false ∨ ipcBufOfA false xt = none ∨ + unat (dataToMessageInfo (xt.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0)) ∨ + ∃ f, xt.tcbFault = some f ∧ Endp.FaultOKA (L := L) s1 (L.curW s) (ipcBufOfA true xd) xt f) ∧ + ((call = false ∧ xt.tcbFault = none ∧ donate = false) ∨ + ((call = true ∨ xt.tcbFault ≠ none) ∧ + ((grant = false ∧ grantReply = false) ∨ + ∀ e' r dd, xd.tcbState ≠ .blockedOnReceive e' (some r) dd) ∧ + Endp.SndRunA σ1 (specPtr (L.curW s)) xt))) : + syscall.decode_invocation st ar s a lbl len src (.Endpoint e badge true recv grant grantReply) + block call donate first buf extra ⦃ LocalInvokePostA + (fun s2 a2 σ2 => a2 = a ∧ Endp.EpStateRel T E R Bf (L.mem s2) σ2 ∧ SR s2 σ2) + block call donate (.invokeEndpoint (specPtr e) badge.bv grant grantReply) σ ⦄ := by + rw [decodeEndpoint_programA] + simp only [↓reduceIte] + refine localInvokeAtRestartA restart ?_ + intro s1 σ1 hs1 + obtain ⟨hc, th, kb, hPl, HE, HS, hSt, hV, hC, hNat, hoff, hth, xt, hkt, hnb, d, q, hk, hdh, + xd, hkd, hu, hrep, hon, hcase, hbr⟩ := selected s1 σ1 hs1 + refine WP.spec_mono (Endp.send_ipc_recvA ops hF place hPl s1 a σ1 HE HS hSt hV hC hNat hoff + thread hth hkt hnb ep hk hdh hkd hu hrep hon badge grant grantReply block call + donate hcase hbr) ?_ + rintro r ⟨har, _, σ2, h, HE', HS', -⟩ + have h' : ((), σ2) ∈ (sendIpc (Z := Unit) block call badge.bv grant grantReply donate σ1.curThread + (specPtr e) σ1).1 := by rw [hc]; exact h + exact ⟨σ2, [], performEndpoint_memA h', har, HE', HS'⟩ + +/-- Endpoint with a waiting receiver that has a reply object: the selected +send pushes the sender's SC donation, then wakes the receiver. -/ +theorem endpointRecvDonate_localA {s : S} {a : A} {σ : State Unit} + {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + (P : IpcSchedPlacementA Z.th N E R Bf) (PE : Endp.EPlaced Z.T E R Bf) + {lbl len src buf e badge : U64} {recv grant grantReply block call first : Bool} + {extra : Array obj.Slot 3#usize} {J : S → State Unit → Prop} + (restart : sched.set_thread_state st s (L.curW s) sched.TS_RESTART ⦃ s1 => + ∃ σ1, setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ J s1 σ1 ⦄) + (ep : e ∈ E) (thread : L.curW s ∈ Z.T) + (selected : ∀ s1 σ1, J s1 σ1 → σ1.curThread = specPtr (L.curW s) ∧ + ∃ (stack : U64 → List U64) (kb : U64), + SchedSRA st Z m κ s1 σ1 ∧ Endp.EpStateRel Z.T E R Bf (L.mem s1) σ1 ∧ + ReplyStackRelA (L.mem s1) R Z.C σ1 (scNameA m) stack ∧ PlaceA Z.th kb ∧ + StateRelA Z.th (L.mem s1) σ1 ∧ SlotsValidA Z.th (L.mem s1) ∧ + Endp.BufCover st Z.T Bf s1 σ1 ∧ Endp.BufNativeA Z.T (L.mem s1) ∧ + (∀ y, IsSlotA Z.th y → ¬ Endp.EWrite Z.T E R Bf y ∧ + ¬ Endp.EWrite Z.T E R Bf (core.num.U64.wrapping_add y 8#u64)) ∧ + ∃ xt, σ1.kheap (specPtr (L.curW s)) = some (.tcb xt) ∧ + Endp.SndRunA σ1 (specPtr (L.curW s)) xt ∧ + ∃ (d : U64) (q : List ObjRef), + σ1.kheap (specPtr e) = some (.endpoint (.recvEP (specPtr d :: q))) ∧ d ∈ Z.T ∧ + ∃ xd, σ1.kheap (specPtr d) = some (.tcb xd) ∧ + (∀ rp, ipcBufOfA true xd = some rp → ∀ k, k < 128 → inUserFrame (bufAddr rp k) σ1) ∧ + RepOKA σ1 (specPtr d) ∧ + ((xt.tcbFault = none ∧ (grant = false ∨ ipcBufOfA false xt = none ∨ + unat (dataToMessageInfo (xt.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0)) ∨ + ∃ f, xt.tcbFault = some f ∧ Endp.FaultOKA (L := L) s1 (L.curW s) (ipcBufOfA true xd) xt f) ∧ + ∃ (sc r : U64), sc ∈ Z.C ∧ sc ≠ 0#u64 ∧ r ∈ R ∧ ReplyIsolatedA (L.mem s1) Z.C stack r ∧ + L.mem s1 (scA (L.curW s)) = sc ∧ L.mem s1 (scA d) = 0#u64 ∧ + L.mem s1 (affA (L.curW s)) = L.cpu s1 ∧ L.mem s1 (affA d) = L.cpu s1 ∧ + L.mem s1 (affA d) = L.mem s1 (scCoreA sc) ∧ + L.mem s1 (core.num.U64.wrapping_add (L.curW s) 3664#u64) = 0#u64 ∧ + NotIdleScA m (scNameA m sc) ∧ + (∃ dd : ReceiverPayload, xd.tcbState = .blockedOnReceive (specPtr e) (some (specPtr r)) dd) ∧ + (call = true ∨ xt.tcbFault ≠ none) ∧ (grant = true ∨ grantReply = true) ∧ + (∀ σ2 σ3 σ4, + doIpcTransfer (Z := Unit) (specPtr (L.curW s)) (some (specPtr e)) badge.bv grant (specPtr d) + (updK σ1 (specPtr e) (.endpoint (epDeq (.recvEP (specPtr d :: q)) (specPtr d)))) = + ({((), σ2)}, False) → + replyUnlinkTcb (Z := Unit) (specPtr d) (specPtr r) σ2 = ({((), σ3)}, False) → + replyPush (Z := Unit) (specPtr (L.curW s)) (specPtr d) (specPtr r) true σ3 = + ({((), σ4)}, False) → + RelIfBoundSc σ4 (specPtr d))) : + syscall.decode_invocation st ar s a lbl len src (.Endpoint e badge true recv grant grantReply) + block call true first buf extra ⦃ LocalInvokePostA + (fun s2 a2 σ2 => a2 = a ∧ Endp.EpStateRel Z.T E R Bf (L.mem s2) σ2 ∧ + IpcSchedRelA st Z m κ s2 σ2 ∧ ∃ stack', ReplyStackRelA (L.mem s2) R Z.C σ2 (scNameA m) stack') + block call true (.invokeEndpoint (specPtr e) badge.bv grant grantReply) σ ⦄ := by + rw [decodeEndpoint_programA] + simp only [↓reduceIte] + refine localInvokeAtRestartA restart ?_ + intro s1 σ1 hs1 + obtain ⟨hc, stack, kb, H, HE, HS, hPl, hSt, hV, hC, hNat, hoff, xt, hkt, hRun, d, q, hk, hd, xd, hkd, + hu, hrep, hcase, sc, r, hsc, hsc0, hr, hi, hfs, hd0, hfon, hon, hmig, hvcpu, hni, ⟨dd, hRecv⟩, + hcf, hg, hRel⟩ := selected s1 σ1 hs1 + refine WP.spec_mono (Endp.send_ipc_recv_pushDonateA a H P PE HE HS hPl hSt hV hC hNat hoff thread hkt + hRun ep hk hd hkd hu hrep badge grant grantReply block call hcase hsc hsc0 hr hi hfs hd0 hfon hon hmig + hvcpu hni hRecv hcf hg hRel) ?_ + rintro r' ⟨har, σ2, h, HE', H', HS'⟩ + have h' : ((), σ2) ∈ (sendIpc (Z := Unit) block call badge.bv grant grantReply true σ1.curThread + (specPtr e) σ1).1 := by rw [hc, h]; rfl + exact ⟨σ2, [], performEndpoint_memA h', har, HE', H', _, HS'⟩ + +/-- An idle notification whose bound TCB is receive-blocked: the bound TCB's +IPC is cancelled and it is woken with the badge. -/ +theorem notificationIdleBoundRecv_localA {s : S} {a : A} {σ : State Unit} + {N T E R : Set U64} {Bf : Set (U64 × Word)} {SR : S → State Unit → Prop} {DL : S → U64 → Prop} + (hyps : Notif.SchedHypsA st N T SR) (ops : Notif.SchedOpsA st ar N T SR DL) + (opsE : Endp.SchedOpsEA st T E R Bf SR) (place : IPlacedA N T E R Bf) + {lbl len src buf n badge : U64} {recv block call donate first : Bool} + {extra : Array obj.Slot 3#usize} {J : S → State Unit → Prop} + (restart : sched.set_thread_state st s (L.curW s) sched.TS_RESTART ⦃ s1 => + ∃ σ1, setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ J s1 σ1 ⦄) + (hn : n ∈ N) + (selected : ∀ s1 σ1, J s1 σ1 → + Notif.NtfnStateRel N T (L.mem s1) σ1 ∧ Endp.EpStateRel T E R Bf (L.mem s1) σ1 ∧ SR s1 σ1 ∧ + ∃ x, σ1.kheap (specPtr n) = some (.notification x) ∧ x.ntfnObj = .idleNtfn ∧ + ∃ t tcb, x.ntfnBoundTcb = some t ∧ σ1.kheap t = some (.tcb tcb) ∧ + receiveBlocked tcb.tcbState = true ∧ RepOKA σ1 t ∧ + L.mem s1 (tBlockA (L.mem s1 (nBoundA n))) ∈ E ∧ + (∀ e', epBlocked tcb.tcbState = some e' → + ∃ z, σ1.kheap e' = some (.endpoint z) ∧ t ∈ z.epQueue) ∧ + L.mem s1 (affA (L.mem s1 (nBoundA n))) = L.cpu s1 ∧ DL s1 n) : + syscall.decode_invocation st ar s a lbl len src (.Notification n badge true recv) + block call donate first buf extra ⦃ LocalInvokePostA + (fun s2 _ σ2 => Notif.NtfnStateRel N T (L.mem s2) σ2 ∧ SR s2 σ2) + block call donate (.invokeNotification (specPtr n) badge.bv) σ ⦄ := by + rw [decodeNotification_programA] + simp only [↓reduceIte] + refine localInvokeAtRestartA restart ?_ + intro s1 σ1 hs1 + obtain ⟨HN, HE, HS, x, hx, idle, t, tcb, hb, hkt, hrb, hrep, heE, hz, hon, hdl⟩ := selected s1 σ1 hs1 + refine WP.spec_mono (Notif.send_signal_idle_bound_recv hyps ops opsE place s1 a σ1 HN HE HS hn hx idle + hb hkt hrb hrep heE hz badge hon hdl) ?_ + rintro r ⟨_, σ2, step, HN', HS'⟩ + exact ⟨σ2, [], performNotification_memA step, HN', HS'⟩ + +end +end +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/LocalScInvokeA.lean b/proofs/Fl4ProofsA64/P3/LocalScInvokeA.lean new file mode 100644 index 000000000..8e26a353e --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/LocalScInvokeA.lean @@ -0,0 +1,127 @@ +/- +P3 (AArch64): early reply ownership at the SC decoder exit. +The Consumed and YieldTo producers supply their existing local and +platform guards. Both retain the state on which the outer reply guard runs. +-/ +import Fl4ProofsA64.P3.LocalInvokePostA +import Fl4ProofsA64.P3.ScYieldTopA +import Fl4ProofsA64.P3.SpecFrA +import Fl4ProofsA64.P3.SchedOpsA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4SpecA64 Fl4Spec Sched + +noncomputable section + +private theorem runningAfterA {σ τ : State Unit} {p : ObjRef} + (h : ((), τ) ∈ (setThreadState (Z := Unit) p .running σ).1) : + ∃ x, τ.kheap p = some (.tcb x) ∧ x.tcbState = .running := by + have get : ∃ x, σ.kheap p = some (.tcb x) := by + unfold setThreadState threadSet at h + rw [nd_bind_assocSc] at h + obtain ⟨⟨x, vx⟩, hx, _⟩ := mem_bindA.1 h + cases e : σ.kheap p with + | none => simp [NonDet.getsThe, getTcb, readObject, e, NonDet.assertOpt, NonDet.fail] at hx + | some k => + cases k <;> simp [NonDet.getsThe, getTcb, readObject, e, NonDet.assertOpt, NonDet.fail] at hx + exact ⟨_, rfl⟩ + obtain ⟨x, hx⟩ := get + rw [Notif.setThreadState_eq .running hx, Set.mem_singleton_iff] at h + obtain rfl := (Prod.mk.inj h).2 + refine ⟨{ x with tcbState := .running }, ?_, rfl⟩ + rw [Notif.stsAct_kheap, Notif.updK_kheap, Function.update_self] + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +/-- A reached Running successor makes the common outer Restart guard +false. The comparison reads the concrete TCB state word. -/ +theorem scEarlyReplyGuardA {Z : XSetsA} {s : S} {σ τ : State Unit} {t : U64} + (ht : t ∈ Z.T) (states : StEqA Z.T (L.mem s) τ) + (h : ((), τ) ∈ (setThreadState (Z := Unit) (specPtr t) .running σ).1) : + obj.Tcb.state st t s = ok sched.TS_RUNNING ∧ sched.TS_RUNNING ≠ sched.TS_RESTART := by + obtain ⟨x, hx, hs⟩ := runningAfterA h + have e := states t ht x hx + rw [hs] at e + refine ⟨?_, ?_⟩ + · rw [eq_of_spec_fd (Fl4ProofsA64.P1.obj.Tcb.state.spec (L := L) s t)] + congr 1 + apply UScalar.eq_of_val_eq + rw [e, ts_vals.1] + rfl + · intro eq + rw [ts_vals.1, ts_vals.2.1] at eq + exact (by decide : (1#u64 : U64) ≠ 2#u64) eq + +/-- Consumed's local perform result precedes its decoder-owned Running +transition. Carry both through the common invocation post. -/ +theorem scConsumed_localA (hA : P0.ArchTotal ar) + {Z : XSetsA} {m : McState} {κ : U64 → U64} {s s1 : S} {σ : State Unit} + (H : SchedSRA st Z m κ s σ) (hst : StEqA Z.T (L.mem s) σ) + {a a1 : A} {sc target ct size len src buf : U64} (hc : sc ∈ Z.C) (hct : ct ∈ Z.T) + (ctEq : σ.curThread = specPtr ct) + {xc : Tcb} (hkc : σ.kheap (specPtr ct) = some (.tcb xc)) (hr : runnable xc.tcbState = true) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ v, ar.ticks_to_us a1 v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (ht : obj.Sc.tcb st sc s = ok target) + (he : (if target != 0#u64 then smp.remote_tcb_stall st ar s a target else ok (s, a)) = ok (s1, a1)) + (hq : target != 0#u64 → smp.remote_tcb_stall st ar s a target = ok (s, a)) + (hcur : sched.cur_thread st s1 = ok ct) + {block call donate : Bool} {extra : Array obj.Slot 3#usize} {label : Data} + (hl : genInvocationType label = .schedContextConsumed) (caps : List Cap) : + syscall.decode_invocation st ar s a fl4_abi.label.SchedContextConsumed len src + (.SchedContext sc size) block call donate false buf extra ⦃ LocalInvokePostA + (fun s' a' σ3 => a' = a1 ∧ ∃ σ4, + setThreadState (Z := Unit) (specPtr ct) .running σ3 = ({((), σ4)}, False) ∧ + SchedSRA st Z m κ s' σ4 ∧ StEqA Z.T (L.mem s') σ4 ∧ + obj.Tcb.state st ct s' = ok sched.TS_RUNNING) + block call donate (.invokeSchedContext (.invokeSchedContextConsumed (scNameA m sc))) σ ⦄ := by + apply sc_local_performA + refine WP.spec_mono (sc_decode_consumed_callA hA H hst hc hct hkc hr hmax htu ht he hq hcur call extra hl caps) ?_ + rintro r ⟨hr', ha, tm, σ2, σ3, σ4, _, pre, inv, run, H4, states, _⟩ + refine ⟨hr', σ2, σ3, wordsFromTime tm, by rw [ctEq]; exact pre, ?_, ha, σ4, run, H4, states, + (scEarlyReplyGuardA (σ := σ3) hct states ?_).1⟩ + · rw [inv]; rfl + · rw [run]; rfl + +/-- Lift the selected YieldTo decoder through the same cap dispatch. +The one-word arm carries the reached Running word and its reply. +The deferred arm returns the perform state for outer finish. -/ +theorem scYieldTo_localA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + {a : A} {sc ct size len src buf : U64} (hct : ct ∈ Z.T) (ctEq : σ.curThread = specPtr ct) + {block call donate : Bool} {extra : Array obj.Slot 3#usize} + (yield : schedcontext.decode st ar s a fl4_abi.label.SchedContextYieldTo sc call extra ⦃ r => + r.1 = .Ok () ∧ ∃ data σ0 σ1 σ' Q', + setThreadState (Z := Unit) (specPtr ct) .restart σ = ({((), σ0)}, False) ∧ + invokeSchedContext (Z := Unit) (.invokeSchedContextYieldTo (scNameA m sc)) σ0 = ({(data, σ1)}, False) ∧ + ((data = [] ∧ σ' = σ1) ∨ ∃ tm, data = wordsFromTime tm ∧ + (do NonDet.when (call = true) (replyFromKernel (specPtr ct) (0, data)) + setThreadState (specPtr ct) .running) σ1 = ({((), σ')}, False)) ∧ + YieldQueueRelA (st := st) {Z with Q := Q'} m κ r.2.1 σ' ⦄) : + syscall.decode_invocation st ar s a fl4_abi.label.SchedContextYieldTo len src + (.SchedContext sc size) block call donate false buf extra ⦃ LocalInvokePostA + (fun s' _ σ1 => ∃ σ' Q', YieldQueueRelA (st := st) {Z with Q := Q'} m κ s' σ' ∧ + (σ' = σ1 ∨ ∃ tm, + (do NonDet.when (call = true) (replyFromKernel (specPtr ct) (0, wordsFromTime tm)) + setThreadState (specPtr ct) .running) σ1 = ({((), σ')}, False) ∧ + obj.Tcb.state st ct s' = ok sched.TS_RUNNING)) + block call donate (.invokeSchedContext (.invokeSchedContextYieldTo (scNameA m sc))) σ ⦄ := by + apply sc_local_performA + refine WP.spec_mono yield ?_ + rintro r ⟨hr, data, σ0, σ1, σ', Q', h0, h1, owns, HS⟩ + refine ⟨hr, σ0, σ1, data, ?_, ?_, σ', Q', HS, ?_⟩ + · rw [ctEq]; exact h0 + · rw [h1]; rfl + · rcases owns with ⟨_, h⟩ | ⟨tm, rfl, h⟩ + · exact Or.inl h + · have reached : ((), σ') ∈ ((do + NonDet.when (call = true) (replyFromKernel (specPtr ct) (0, wordsFromTime tm)) + setThreadState (specPtr ct) .running) σ1).1 := by rw [h]; rfl + obtain ⟨⟨_, υ⟩, _, run⟩ := mem_bindA.1 reached + exact Or.inr ⟨tm, h, (scEarlyReplyGuardA hct HS.states run).1⟩ + +end +end +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/LookupA.lean b/proofs/Fl4ProofsA64/P3/LookupA.lean new file mode 100644 index 000000000..4d85a094b --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/LookupA.lean @@ -0,0 +1,689 @@ +/- +P3 (aarch64, A64-P3 S3 gen 9, ruling 13:20Z item 1): **FL4's CSpace walk refines the AArch64 spec's** — the A64 +twin of x86's `P3/Lookup` (`resolve_address_bits_refines`, `lookup_slot_for_cnode_op_refines`), the first piece of +the CNode decode (FL4's `cspace.rs` is architecture-neutral: the A64 extraction differs from x86's only by the +`HighVSpace` arms, which are not CNode caps; `Fl4SpecA64/CSpace` is a near-copy of x86's). + +* Bit lists (x86 `CapRel`'s `bitsOf` facts, for `bitsOfA`): `length_bitsOfA`, `bitsOfA_drop`, `bitsOfA_take`, + `bitsOfA_eq_iff`, `blvA_bitsOfA`, `ofBl_bitsOfA` (below 64 bits), `toBl_bitsOfA`, `toBl_dropA`; one round's + guard / offset arithmetic (x86 `guard_bits`, `offset_bits`, `offset_list`). +* The spec's monad (x86 `Lookup`'s `nd_*`, `run_bind_*`, over `Fl4SpecA64.State Unit`) and + `resolve_address_bits'` unfolded (`rab_*A`), `getCap_cnodeA` (one outcome). +* **`resolve_address_bits_refinesA`** (x86 `resolve_address_bits_refines`): for `StateRelA`, `SlotsValidA` (gen 9: + `JAtZA.cv`), a `CapRelA`-related root with `CapValidA` and a depth `n ≤ 64`, the spec resolving the low `n` bits + of `cptr` has exactly one outcome, does not fail and keeps the state (`SingleA`), and FL4's result corresponds to it + (`WalkRelA`: the same slot and bits left, or the corresponding lookup fault `LookupFaultRelA`; a returned slot is a + slot of a CNode of `th` reached from the root, `CnRefA`). +* **`lookup_slot_for_cnode_op_refinesA`** (x86 `lookup_slot_for_cnode_op_refines`, hence `lookup_source_slot`, + `lookup_target_slot`, `lookup_pivot_slot`): against l4v `lookup_slot_for_cnode_op` at `to_bl cptr` and `unat + depth`, errors `SysErrRelA`-related. + +Hypotheses (case hypotheses of the lemmas, no named premise): `StateRelA`, `SlotsValidA` (both `JAtZA` fields), the +root cap's `CapRelA` / `CapValidA` (the invoked cap's, from the decode call site). New module; no existing statement +changed. +-/ +import Fl4ProofsA64.P3.EmptyA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 + +noncomputable section + +/-! ## Bit lists -/ + +@[simp] theorem bitsOfA_zero (x : Nat) : bitsOfA x 0 = [] := rfl +theorem bitsOfA_succ (x n : Nat) : bitsOfA x (n + 1) = x.testBit n :: bitsOfA x n := rfl + +@[simp] theorem length_bitsOfA (x n : Nat) : (bitsOfA x n).length = n := by + induction n with + | zero => rfl + | succ n ih => simp [bitsOfA_succ, ih] + +theorem bitsOfA_eq_nil {x n : Nat} : bitsOfA x n = [] ↔ n = 0 := by + constructor + · intro h; have := congrArg List.length h; simpa using this + · rintro rfl; rfl + +theorem getElem_bitsOfA (x n i : Nat) (h : i < (bitsOfA x n).length) : + (bitsOfA x n)[i] = x.testBit (n - 1 - i) := by + induction n generalizing i with + | zero => simp at h + | succ n ih => + cases i with + | zero => simp [bitsOfA_succ] + | succ i => + simp only [bitsOfA_succ, List.getElem_cons_succ] + rw [ih]; congr 1; omega + +theorem bitsOfA_drop (x : Nat) {k n : Nat} (hk : k ≤ n) : (bitsOfA x n).drop k = bitsOfA x (n - k) := by + induction k generalizing n with + | zero => simp + | succ k ih => + obtain ⟨m, rfl⟩ : ∃ m, n = m + 1 := ⟨n - 1, by omega⟩ + simp only [bitsOfA_succ, List.drop_succ_cons] + rw [ih (by omega)] + congr 1; omega + +theorem bitsOfA_take (x : Nat) {k n : Nat} (hk : k ≤ n) : + (bitsOfA x n).take k = bitsOfA (x / 2 ^ (n - k)) k := by + apply List.ext_getElem + · simp; omega + · intro i h1 h2 + simp only [List.getElem_take, getElem_bitsOfA, Nat.testBit_div_two_pow] + congr 1 + simp at h1 h2 + omega + +theorem bitsOfA_eq_iff (x y n : Nat) : bitsOfA x n = bitsOfA y n ↔ x % 2 ^ n = y % 2 ^ n := by + constructor + · intro h + apply Nat.eq_of_testBit_eq + intro i + simp only [Nat.testBit_mod_two_pow] + by_cases hi : i < n + · have := congrArg (fun l => l[n - 1 - i]?) h + rw [List.getElem?_eq_getElem (by simp; omega), List.getElem?_eq_getElem (by simp; omega)] at this + simp only [Option.some.injEq, getElem_bitsOfA] at this + have e : n - 1 - (n - 1 - i) = i := by omega + rw [e] at this + simp [hi, this] + · simp [hi] + · intro h + apply List.ext_getElem + · simp + · intro i h1 h2 + simp only [getElem_bitsOfA] + have := congrArg (fun z => z.testBit (n - 1 - i)) h + simp only [Nat.testBit_mod_two_pow] at this + simp at h1 + have hlt : n - 1 - i < n := by omega + simpa [hlt] using this + +theorem blvA_bitsOfA (x : Nat) : ∀ n, blvA (bitsOfA x n) = x % 2 ^ n + | 0 => by simp [blvA, Nat.mod_one] + | n + 1 => by + simp only [bitsOfA_succ, blvA, length_bitsOfA, blvA_bitsOfA x n] + rw [Nat.mod_pow_succ, Nat.testBit_eq_decide_div_mod_eq] + rcases Nat.mod_two_eq_zero_or_one (x / 2 ^ n) with h | h <;> simp [h] <;> omega + +/-- A slot index `bitsOfA v r` as a word (x86 `ofBl_bitsOf_eq`, here below 64 bits). -/ +theorem ofBl_bitsOfA (x n : Nat) (hn : n ≤ 64) : + (ofBl (bitsOfA x n) : BitVec 64) = BitVec.ofNat 64 (x % 2 ^ n) := by + rw [ofBlA_eq (by simpa using hn), blvA_bitsOfA] + +theorem toBl_bitsOfA (w : BitVec 64) : toBl w = bitsOfA w.toNat 64 := by + apply List.ext_getElem + · simp [toBl] + · intro i h1 h2 + simp only [toBl, List.getElem_map, List.getElem_reverse, List.getElem_range, + getElem_bitsOfA, BitVec.getLsbD, List.length_range] + +/-- A 64-bit capability pointer resolved to depth `n` (x86 `toBl_drop`). -/ +theorem toBl_dropA (w : BitVec 64) (n : Nat) (hn : n ≤ 64) : + (toBl w).drop (64 - n) = bitsOfA w.toNat n := by + rw [toBl_bitsOfA, bitsOfA_drop _ (by omega)] + congr 1; omega + +/-! ## One round's arithmetic (x86 `Lookup`) -/ + +theorem guard_not_prefixA {c n gs guard : Nat} (h : n < gs) : ¬ bitsOfA guard gs <+: bitsOfA c n := by + intro hp; have := hp.length_le; simp at this; omega + +theorem guard_prefix_iffA {c n gs guard : Nat} (h : gs ≤ n) (hg : guard < 2 ^ gs) : + bitsOfA guard gs <+: bitsOfA c n ↔ c / 2 ^ (n - gs) % 2 ^ gs = guard := by + rw [List.prefix_iff_eq_take, length_bitsOfA, bitsOfA_take _ h, bitsOfA_eq_iff, Nat.mod_eq_of_lt hg] + exact eq_comm + +theorem guard_bitsA (c n gs : Nat) (h : gs ≤ n) (hn : n ≤ 64) : + (c >>> ((n - gs) % 64)) &&& (2 ^ gs - 1) = c / 2 ^ (n - gs) % 2 ^ gs := by + rw [Nat.and_two_pow_sub_one_eq_mod, Nat.shiftRight_eq_div_pow] + by_cases h64 : n - gs = 64 + · have : gs = 0 := by omega + subst this; simp [Nat.mod_one] + · have : (n - gs) % 64 = n - gs := Nat.mod_eq_of_lt (by omega) + rw [this] + +theorem offset_bitsA (c n level radix : Nat) (h : level ≤ n) (h1 : 1 ≤ level) (hn : n ≤ 64) : + (c >>> ((n - level) % 64)) &&& (2 ^ radix - 1) = c / 2 ^ (n - level) % 2 ^ radix := by + have : (n - level) % 64 = n - level := Nat.mod_eq_of_lt (by omega) + rw [Nat.and_two_pow_sub_one_eq_mod, Nat.shiftRight_eq_div_pow, this] + +theorem offset_listA (c n gs radix : Nat) (h : radix + gs ≤ n) : + ((bitsOfA c n).drop gs).take radix = bitsOfA (c / 2 ^ (n - (radix + gs))) radix := by + rw [bitsOfA_drop _ (by omega), bitsOfA_take _ (by omega)] + congr 3; omega + +theorem bv_eq_ofNatA (x : U64) : x.bv = BitVec.ofNat 64 x.val := by + apply BitVec.eq_of_toNat_eq; simp + +/-! ## The spec's monad, one outcome at a time (x86 `Lookup`) -/ + +section +variable {σ α β ε : Type} +@[simp] theorem nd_pure_applyA (a : α) (s : σ) : (pure a : NonDet σ α) s = ({(a, s)}, False) := rfl +@[simp] theorem nd_bind_applyA (m : NonDet σ α) (f : α → NonDet σ β) (s : σ) : + (m >>= f) s = ({r | ∃ x ∈ (m s).1, r ∈ (f x.1 x.2).1}, (m s).2 ∨ ∃ x ∈ (m s).1, (f x.1 x.2).2) := rfl +theorem nd_pure_bindA (a : α) (f : α → NonDet σ β) : (pure a >>= f) = f a := by + funext s + simp only [nd_bind_applyA, nd_pure_applyA, Set.mem_singleton_iff, exists_eq_left, false_or] + ext <;> simp +theorem nd_bind_singleA {m : NonDet σ α} {s : σ} {a : α} (h : m s = ({(a, s)}, False)) (f : α → NonDet σ β) : + (m >>= f) s = f a s := by + simp only [nd_bind_applyA, h, Set.mem_singleton_iff, exists_eq_left, false_or] + ext <;> simp +theorem nd_bind_singleA' {m : NonDet σ α} {s : σ} {a : α} (h : m s = ({(a, s)}, False)) + (f : α → NonDet σ β) : NonDet.bind m f s = f a s := nd_bind_singleA h f +@[simp] theorem ndE_pure_bindA (a : α) (f : α → NonDetE σ ε β) : (pure a >>= f) = f a := by + show ExceptT.mk (pure (Except.ok a) >>= ExceptT.bindCont f) = f a + rw [nd_pure_bindA]; rfl +@[simp] theorem ndE_throw_bindA (e : ε) (f : α → NonDetE σ ε β) : (throw e >>= f) = throw e := by + show ExceptT.mk (pure (Except.error e) >>= ExceptT.bindCont f) = ExceptT.mk (pure (Except.error e)) + rw [nd_pure_bindA]; rfl + +@[simp] theorem run_returnOkA (a : α) (s : σ) : + (NonDetE.returnOk a : NonDetE σ ε α).run s = ({(.ok a, s)}, False) := rfl + +@[simp] theorem run_throwErrorA (e : ε) (s : σ) : + (NonDetE.throwError e : NonDetE σ ε α).run s = ({(.error e, s)}, False) := rfl + +theorem run_bind_okA {m : NonDetE σ ε α} {s : σ} {a : α} (h : m.run s = ({(.ok a, s)}, False)) + (f : α → NonDetE σ ε β) : (m >>= f).run s = (f a).run s := by + rw [ExceptT.run_bind, nd_bind_singleA h] + +theorem run_bind_errorA {m : NonDetE σ ε α} {s : σ} {e : ε} (h : m.run s = ({(.error e, s)}, False)) + (f : α → NonDetE σ ε β) : (m >>= f).run s = ({(.error e, s)}, False) := by + rw [ExceptT.run_bind, nd_bind_singleA h]; rfl + +theorem run_liftEA {m : NonDet σ α} {s : σ} {a : α} (h : m s = ({(a, s)}, False)) : + (NonDetE.liftE (ε := ε) m).run s = ({(.ok a, s)}, False) := by + unfold NonDetE.liftE + rw [ExceptT.run_lift] + show (m >>= _) s = _ + rw [nd_bind_singleA h]; rfl +end + +/-! ## The spec side: `resolve_address_bits'` unfolded -/ + +section Spec + +theorem rab_noncnodeA (a : Cap) (cref : CapRef) (σ : State Unit) (h : isCnodeCap a = false) : + (resolveAddressBits' (Z := Unit) (a, cref)).run σ = ({(.error .invalidRoot, σ)}, False) := by + rw [resolveAddressBits'] + cases a <;> simp [isCnodeCap] at h <;> rfl + +theorem rab_guardA (oref : ObjRef) (radix : Nat) (guard : List Bool) (cref : CapRef) (σ : State Unit) + (h0 : radix + guard.length ≠ 0) (hg : ¬ guard <+: cref) : + (resolveAddressBits' (Z := Unit) (.cnodeCap oref radix guard, cref)).run σ = + ({(.error (.guardMismatch cref.length guard), σ)}, False) := by + rw [resolveAddressBits'] + simp only [h0, _root_.dite_false] + simp [hg, NonDetE.whenE, NonDetE.throwError] + +theorem rab_depthA (oref : ObjRef) (radix : Nat) (guard : List Bool) (cref : CapRef) (σ : State Unit) + (h0 : radix + guard.length ≠ 0) (hg : guard <+: cref) (hd : cref.length < radix + guard.length) : + (resolveAddressBits' (Z := Unit) (.cnodeCap oref radix guard, cref)).run σ = + ({(.error (.depthMismatch cref.length (radix + guard.length)), σ)}, False) := by + rw [resolveAddressBits'] + simp only [h0, _root_.dite_false] + simp [hg, hd, NonDetE.whenE, NonDetE.throwError] + +theorem rab_lastA (oref : ObjRef) (radix : Nat) (guard : List Bool) (cref : CapRef) (σ : State Unit) + (h0 : radix + guard.length ≠ 0) (hg : guard <+: cref) (hd : ¬ cref.length < radix + guard.length) + (hr : cref.drop (radix + guard.length) = []) : + (resolveAddressBits' (Z := Unit) (.cnodeCap oref radix guard, cref)).run σ = + ({(.ok ((oref, (cref.drop guard.length).take radix), []), σ)}, False) := by + rw [resolveAddressBits'] + simp only [h0, _root_.dite_false] + simp [hg, hd, hr, NonDetE.whenE, NonDetE.returnOk] + +theorem rab_nextA (oref : ObjRef) (radix : Nat) (guard : List Bool) (cref : CapRef) (σ : State Unit) (c : Cap) + (h0 : radix + guard.length ≠ 0) (hg : guard <+: cref) (hd : ¬ cref.length < radix + guard.length) + (hr : cref.drop (radix + guard.length) ≠ []) + (hget : getCap (Z := Unit) (oref, (cref.drop guard.length).take radix) σ = ({(c, σ)}, False)) : + (resolveAddressBits' (Z := Unit) (.cnodeCap oref radix guard, cref)).run σ = + if isCnodeCap c then (resolveAddressBits' (Z := Unit) (c, cref.drop (radix + guard.length))).run σ + else ({(.ok ((oref, (cref.drop guard.length).take radix), cref.drop (radix + guard.length)), σ)}, False) := by + rw [resolveAddressBits'] + simp only [h0, _root_.dite_false] + have h1 : (Except.ok <$> getCap (Z := Unit) (oref, (cref.drop guard.length).take radix) : + NonDet (State Unit) (Except LookupFailure Cap)) σ = ({(.ok c, σ)}, False) := by + show (getCap _ >>= _) σ = _ + rw [nd_bind_singleA hget]; rfl + simp only [hg, hd, hr, not_true_eq_false, ite_false, _root_.dite_false, NonDetE.whenE, NonDetE.returnOk, + NonDetE.liftE, ExceptT.run_bind, ExceptT.run_lift, ExceptT.run_pure, nd_pure_bindA] + rw [nd_bind_singleA h1] + by_cases h : isCnodeCap c = true <;> simp only [h] <;> rfl + +/-- `get_cap` of a CNode slot, as its one outcome (x86 `getCap_cnode`; EmptyA's `getCap_cnode_memA` is the +membership form). -/ +theorem getCap_cnodeA {σ : State Unit} {p : ObjRef} {r : Nat} {cs : CnodeContents} {idx : List Bool} {a : Cap} + (hk : σ.kheap p = some (.cnode r cs)) (hwf : wellFormedCnodeN r cs) (ha : cs idx = some a) : + getCap (Z := Unit) (p, idx) σ = ({(a, σ)}, False) := by + unfold getCap + have h1 : getObject (Z := Unit) p σ = ({(.cnode r cs, σ)}, False) := by + simp [getObject, NonDet.getsThe, readObject, hk, NonDet.assertOpt]; rfl + show (getObject p >>= _) σ = _ + rw [nd_bind_singleA h1] + have h2 : (NonDet.assert (wellFormedCnodeN r cs) : NonDet (State Unit) Unit) σ = ({((), σ)}, False) := by + simp [NonDet.assert, hwf] + show (NonDet.assert _ >>= _) σ = _ + rw [nd_bind_singleA h2] + show ((pure cs : NonDet (State Unit) CnodeContents) >>= _) σ = _ + rw [nd_pure_bindA] + simp [ha, NonDet.assertOpt]; rfl + +end Spec + +/-! ## The concrete side -/ + +section Concrete +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +theorem mask_specA (bits : U64) (h : bits.val < 64) : + cspace.mask bits ⦃ m => m.val = 2 ^ bits.val - 1 ⦄ := by + unfold cspace.mask + have : ¬ (bits ≥ 64#u64) := by scalar_tac + simp only [this, ite_false, lift, bind_ok] + step* + have h63 : (bits &&& 63#u64).val = bits.val := by + have : (bits &&& 63#u64).val = bits.val % 64 := by + simp; exact Nat.and_two_pow_sub_one_eq_mod bits.val 6 + omega + have hp : 2 ^ bits.val < 2 ^ 64 := Nat.pow_lt_pow_right (by decide) h + have hp1 : 1 ≤ 2 ^ bits.val := Nat.one_le_two_pow + have hi : i1.val = 2 ^ bits.val := by rw [i1_post, h63] + simp [core.num.U64.wrapping_sub_val_eq, U64.size_def, UScalarTy.U64_numBits_eq, U64.numBits, hi] + omega + +theorem slot_index_specA (p i : U64) : obj.Slot.index p i ⦃ sl => sl = slotAddrA (specPtr p) i.bv ⦄ := by + rw [slotAddrA_sp] + unfold obj.Slot.index + simp only [lift, bind_ok, WP.spec_ok, UScalar.eq_equiv_bv_eq] + simp [layout.cte.SIZE] + +end Concrete + +/-! ## The walk refines `resolve_address_bits'` -/ + +/-- Lookup faults (x86 `LookupFaultRel`; FL4's `bits_left` / `bits_found` are `n ≤ 64`, `CapWfA`'s guard size +`< 64`). -/ +def LookupFaultRelA : error.LookupFault → LookupFailure → Prop + | .InvalidRoot, .invalidRoot => True + | .MissingCapability bl, .missingCapability bl' => bl' = bl.val + | .DepthMismatch found left, .depthMismatch left' found' => left' = left.val ∧ found' = found.val + | .GuardMismatch g left gs, .guardMismatch left' g' => left' = left.val ∧ g' = bitsOfA g.val gs.val ∧ + g.val < 2 ^ gs.val ∧ gs.val < 64 ∧ left.val ≤ 64 + | _, _ => False + +/-- **Every slot's CNode / thread cap is valid** (`JAtZA.cv`; x86 `SlotsValid`). -/ +abbrev SlotsValidA (th : TypedHeapA) (mem : U64 → U64) : Prop := ∀ y, IsSlotA th y → CapValidA th (capAtA mem y) + +/-- The spec's `resolve_address_bits'` run from `σ` (x86 `rab`). -/ +abbrev rabA (p : Cap × CapRef) (σ : State Unit) := (resolveAddressBits' (Z := Unit) p).run σ + +/-- Results of FL4's walk against outcomes of the spec's (x86 `ResRel`). -/ +def ResRelA : core.result.Result cspace.Resolved error.LookupFault → Except LookupFailure (CslotPtr × CapRef) → Prop + | .Ok r, .ok (sl, rest) => r.slot = cslotAddrA sl ∧ r.bits_left.val = rest.length + | .Err lf, .error lf' => LookupFaultRelA lf lf' + | _, _ => False + +/-- A cap the walk reaches from the root `c0`: the root, or a cap held in a CNode slot of `th` (x86 `RefCap`). -/ +def RefCapA (th : TypedHeapA) (mem : U64 → U64) (c0 c : capability.Cap) : Prop := + (∃ p r idx, th p = some (.cnode r) ∧ idx.length = r ∧ capAtA mem (cslotAddrA ((specPtr p), idx)) = c) ∨ c = c0 + +/-- A CNode named by a cap the walk reaches (x86 `CnRef`). -/ +def CnRefA (th : TypedHeapA) (mem : U64 → U64) (c0 : capability.Cap) (p : U64) : Prop := + ∃ r gs g, RefCapA th mem c0 (.CNode p r gs g) + +/-- A slot the walk returns is a slot of a CNode of `th` reached from the root (x86 `SlotOk`). -/ +def SlotOkA (th : TypedHeapA) (mem : U64 → U64) (c0 : capability.Cap) : + Except LookupFailure (CslotPtr × CapRef) → Prop + | .ok (sl, _) => ∃ p, sl.1 = (specPtr p) ∧ th p = some (.cnode sl.2.length) ∧ CnRefA th mem c0 p + | .error _ => True + +/-- The walk's results against the spec's outcomes (x86 `WalkRel`). -/ +def WalkRelA (th : TypedHeapA) (mem : U64 → U64) (c0 : capability.Cap) + (r : core.result.Result cspace.Resolved error.LookupFault) (e : Except LookupFailure (CslotPtr × CapRef)) : + Prop := + ResRelA r e ∧ SlotOkA th mem c0 e + +/-- A spec computation run from `σ` has exactly one outcome, `e`, with `P e`, keeps the state and cannot fail +(x86 `Single`). -/ +def SingleA {α : Type} (m : Set (α × State Unit) × Prop) (σ : State Unit) (P : α → Prop) : Prop := + ∃ e, m = ({(e, σ)}, False) ∧ P e + +/-- The walk's loop invariant (x86 `WalkInv`). -/ +def WalkInvA (th : TypedHeapA) (mem : U64 → U64) (σ : State Unit) (cptr : U64) (c0 : capability.Cap) + (p0 : Cap × CapRef) (x : capability.Cap × U64 × U64) : Prop := + RefCapA th mem c0 x.1 ∧ x.2.1.val ≤ 64 ∧ x.2.1.val < x.2.2.val ∧ + ∃ a, CapRelA x.1 a ∧ CapValidA th x.1 ∧ rabA (a, bitsOfA cptr.val x.2.1.val) σ = rabA p0 σ + +/-- Slot `bitsOfA v r` of a related CNode (x86 `StateRel.cnode_slot`): the cap there is related and valid. -/ +theorem StateRelA.cnode_slotA {th : TypedHeapA} {mem : U64 → U64} {σ : State Unit} (hR : StateRelA th mem σ) + (hV : SlotsValidA th mem) {p : U64} {r : Nat} (hp : th p = some (.cnode r)) (hr : r ≤ 64) (v : Nat) : + ∃ cs a, σ.kheap (specPtr p) = some (.cnode r cs) ∧ wellFormedCnodeN r cs ∧ cs (bitsOfA v r) = some a ∧ + CapRelA (capAtA mem (slotAddrA (specPtr p) (BitVec.ofNat 64 (v % 2 ^ r)))) a ∧ + CapValidA th (capAtA mem (slotAddrA (specPtr p) (BitVec.ofNat 64 (v % 2 ^ r)))) := by + obtain ⟨cs, hk, hwf, hs⟩ := hR.cnode p r hp + obtain ⟨a, ha, hca⟩ := hs (bitsOfA v r) (by simp) + rw [ofBl_bitsOfA _ _ hr] at hca + exact ⟨cs, a, hk, hwf, ha, hca, hV _ ⟨p, Or.inl ⟨r, hp, v % 2 ^ r, Nat.mod_lt _ (by positivity), rfl⟩⟩⟩ + +section WalkProof +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- One round of the walk (x86 `body_spec`). -/ +theorem body_specA (th : TypedHeapA) (s : S) (σ : State Unit) (hR : StateRelA th (L.mem s) σ) + (hV : SlotsValidA th (L.mem s)) (cptr : U64) (c0 : capability.Cap) (p0 : Cap × CapRef) + (cap : capability.Cap) (n fuel : U64) (hx : WalkInvA th (L.mem s) σ cptr c0 p0 (cap, n, fuel)) : + cspace.resolve_address_bits_loop.body st s cptr cap n fuel ⦃ r => + match r with + | .done y => SingleA (rabA p0 σ) σ (WalkRelA th (L.mem s) c0 y) + | .cont x' => WalkInvA th (L.mem s) σ cptr c0 p0 x' ∧ x'.2.1.val < n.val ⦄ := by + obtain ⟨hrf, hn, hf, a, ⟨hwf, heq⟩, hcv, hrab⟩ := hx + simp only at hn hf hrab hcv hwf hrf heq ⊢ + rw [← hrab] + cases cap + case CNode ptr radix gs guard => + subst heq + obtain ⟨hr, hgs, hg⟩ := hwf + obtain ⟨hth, hr0⟩ := hcv + unfold cspace.resolve_address_bits_loop.body + have h0 : radix.val + (bitsOfA guard.val gs.val).length ≠ 0 := by simp; omega + step as ⟨level, hlev⟩ + have hl : level.val = radix.val + gs.val := by + subst hlev + simp [core.num.U64.wrapping_add_val_eq, U64.size_def, UScalarTy.U64_numBits_eq, U64.numBits] + omega + split + · exfalso; rename_i h; subst h; simp at hl; omega + split + · exfalso; rename_i h; subst h; simp at hf + step as ⟨fuel1, hf1, hf1'⟩ + step as ⟨i, hi⟩ + step as ⟨shift, hsh, hsh'⟩ + step as ⟨i1, hi1, hi1'⟩ + step with mask_specA as ⟨i2, hi2⟩ + step as ⟨g, hgv, hgv'⟩ + split + · -- not enough bits for the guard + rename_i hgn + simp only [WP.spec_ok] + have hlt : n.val < gs.val := by scalar_tac + refine ⟨_, rab_guardA _ _ _ _ _ h0 (guard_not_prefixA hlt), ?_⟩ + simp [WalkRelA, ResRelA, SlotOkA, LookupFaultRelA, hg, hgs, hn] + have hgsn : gs.val ≤ n.val := by rename_i h; scalar_tac + have hgval : g.val = cptr.val / 2 ^ (n.val - gs.val) % 2 ^ gs.val := by + have hiv : i.val = n.val - gs.val := by + have : n.val < 2 ^ 64 := by scalar_tac + subst hi + simp [core.num.U64.wrapping_sub_val_eq, U64.size_def, UScalarTy.U64_numBits_eq, U64.numBits] + omega + have hshv : shift.val = (n.val - gs.val) % 64 := by + rw [hsh, ← hiv]; simp; exact Nat.and_two_pow_sub_one_eq_mod i.val 6 + rw [hgv]; simp only [UScalar.val_and]; rw [hi1, hi2, hshv] + exact guard_bitsA _ _ _ hgsn hn + have hpre : bitsOfA guard.val gs.val <+: bitsOfA cptr.val n.val ↔ g = guard := by + rw [guard_prefix_iffA hgsn hg, ← hgval, UScalar.eq_equiv] + split + · -- guard mismatch + rename_i hne + simp only [WP.spec_ok] + have hne' : g ≠ guard := by simpa using hne + refine ⟨_, rab_guardA _ _ _ _ _ h0 (fun h => hne' (hpre.1 h)), ?_⟩ + simp [WalkRelA, ResRelA, SlotOkA, LookupFaultRelA, hg, hgs, hn] + have hp : bitsOfA guard.val gs.val <+: bitsOfA cptr.val n.val := + hpre.2 (by rename_i h; exact UScalar.eq_of_val_eq (by simpa using h)) + split + · -- not enough bits for the level + rename_i hgt + simp only [WP.spec_ok] + have : n.val < level.val := by scalar_tac + refine ⟨_, rab_depthA _ _ _ _ _ h0 hp (by simp; omega), ?_⟩ + simp [WalkRelA, ResRelA, SlotOkA, LookupFaultRelA, hl] + have hln : level.val ≤ n.val := by rename_i h; scalar_tac + step as ⟨i3, hi3, hi3'⟩ + step as ⟨i4, hi4, hi4'⟩ + step as ⟨i5, hi5, hi5'⟩ + step with mask_specA as ⟨i6, hi6⟩ + step as ⟨offset, hoff, hoff'⟩ + step with slot_index_specA as ⟨slot, hslot⟩ + have hofv : offset.val = cptr.val / 2 ^ (n.val - (radix.val + gs.val)) % 2 ^ radix.val := by + have hi4v : i4.val = (n.val - level.val) % 64 := by + rw [hi4, ← hi3]; simp; exact Nat.and_two_pow_sub_one_eq_mod _ 6 + rw [hoff]; simp only [UScalar.val_and]; rw [hi5, hi6, hi4v, ← hl] + exact offset_bitsA _ _ _ _ hln (by omega) hn + have hoffL : ((bitsOfA cptr.val n.val).drop (bitsOfA guard.val gs.val).length).take radix.val = + bitsOfA (cptr.val / 2 ^ (n.val - (radix.val + gs.val))) radix.val := by + rw [length_bitsOfA]; exact offset_listA _ _ _ _ (by omega) + have hslotN : slot = slotAddrA (specPtr ptr) + (BitVec.ofNat 64 (cptr.val / 2 ^ (n.val - (radix.val + gs.val)) % 2 ^ radix.val)) := by + rw [hslot, bv_eq_ofNatA offset, hofv] + have hslot' : slot = cslotAddrA ((specPtr ptr), bitsOfA (cptr.val / 2 ^ (n.val - (radix.val + gs.val))) radix.val) := by + rw [hslotN, cslotAddrA, ofBl_bitsOfA _ _ (by omega)] + split + · -- all bits resolved + rename_i heq + simp only [WP.spec_ok] + have hnl : n.val = level.val := by rw [heq] + have hr' : (bitsOfA cptr.val n.val).drop (radix.val + (bitsOfA guard.val gs.val).length) = [] := by + rw [length_bitsOfA, bitsOfA_drop _ (by omega), bitsOfA_eq_nil]; omega + refine ⟨_, rab_lastA _ _ _ _ _ h0 hp (by simp; omega) hr', ?_⟩ + simp only [WalkRelA, ResRelA, SlotOkA]; rw [hoffL] + exact ⟨⟨hslot', by simp⟩, ptr, rfl, by simpa using hth, radix, gs, guard, hrf⟩ + have hlt : level.val < n.val := by + rename_i h; have : n.val ≠ level.val := fun e => h (UScalar.eq_of_val_eq e); omega + obtain ⟨cs, a', hk, hwf, hca', hrel, hval⟩ := + hR.cnode_slotA hV hth (by omega) (cptr.val / 2 ^ (n.val - (radix.val + gs.val))) + rw [← hslotN] at hrel hval + have hget : getCap (Z := Unit) ((specPtr ptr), + ((bitsOfA cptr.val n.val).drop (bitsOfA guard.val gs.val).length).take radix.val) σ = ({(a', σ)}, False) := by + rw [hoffL]; exact getCap_cnodeA hk hwf hca' + have hrest : (bitsOfA cptr.val n.val).drop (radix.val + (bitsOfA guard.val gs.val).length) = + bitsOfA cptr.val i3.val := by + rw [length_bitsOfA, bitsOfA_drop _ (by omega), hi3, hl] + have hnil : (bitsOfA cptr.val n.val).drop (radix.val + (bitsOfA guard.val gs.val).length) ≠ [] := by + rw [hrest, ne_eq, bitsOfA_eq_nil]; omega + have hstep := rab_nextA (specPtr ptr) radix.val (bitsOfA guard.val gs.val) (bitsOfA cptr.val n.val) σ a' h0 hp + (by simp; omega) hnil hget + rw [hrest, hoffL] at hstep + have hdone : isCnodeCap a' = false → + SingleA (rabA (absCapA (.CNode ptr radix gs guard), bitsOfA cptr.val n.val) σ) σ + (WalkRelA th (L.mem s) c0 (.Ok { slot := slot, bits_left := i3 })) := by + intro hc + refine ⟨.ok (((specPtr ptr), bitsOfA (cptr.val / 2 ^ (n.val - (radix.val + gs.val))) radix.val), + bitsOfA cptr.val i3.val), ?_, ?_⟩ + · show (resolveAddressBits' _).run σ = _ + rw [show absCapA (.CNode ptr radix gs guard) = .cnodeCap (specPtr ptr) radix.val (bitsOfA guard.val gs.val) from rfl, + hstep, hc] + rfl + · exact ⟨⟨hslot', by simp⟩, ptr, rfl, by simpa using hth, radix, gs, guard, hrf⟩ + have hcont : isCnodeCap a' = true → rabA (a', bitsOfA cptr.val i3.val) σ = rabA p0 σ := by + intro hc + rw [← hrab] + show _ = (resolveAddressBits' _).run σ + rw [show absCapA (.CNode ptr radix gs guard) = .cnodeCap (specPtr ptr) radix.val (bitsOfA guard.val gs.val) from rfl, + hstep, hc] + rfl + rw [capA_eq, bind_ok] + have ha' := hrel.2 + have hsl : capAtA (L.mem s) (cslotAddrA ((specPtr ptr), bitsOfA (cptr.val / 2 ^ (n.val - (radix.val + gs.val))) + radix.val)) = capA st s slot := by rw [← hslot']; rfl + generalize hnx : capA st s slot = next at hsl + have hnx' : capAtA (L.mem s) slot = next := hnx + rw [hnx'] at hrel hval ha' + cases next + case CNode p' r' gs' g' => + simp only [WP.spec_ok] + exact ⟨⟨Or.inl ⟨ptr, radix.val, _, by simpa using hth, by simp, hsl⟩, show i3.val ≤ 64 by omega, + show i3.val < fuel1.val by omega, a', hrel, hval, hcont (by rw [← ha']; rfl)⟩, show i3.val < n.val by omega⟩ + all_goals + simp only [WP.spec_ok] + exact hdone (by rw [ha'.isCnode_eq]; rfl) + all_goals + unfold cspace.resolve_address_bits_loop.body + simp only [WP.spec_ok] + exact ⟨_, rab_noncnodeA _ _ _ (by rw [heq.isCnode_eq]; rfl), trivial, trivial⟩ + +/-- **FL4's `resolve_address_bits` refines l4v `resolve_address_bits'`** (x86 `resolve_address_bits_refines`; +module docstring). -/ +theorem resolve_address_bits_refinesA (th : TypedHeapA) (s : S) (σ : State Unit) + (hR : StateRelA th (L.mem s) σ) (hV : SlotsValidA th (L.mem s)) + (c : capability.Cap) (a : Cap) (hc : CapRelA c a) (hcv : CapValidA th c) + (cptr n : U64) (hn : n.val ≤ 64) : + cspace.resolve_address_bits st s c cptr n ⦃ r => + SingleA (rabA (a, bitsOfA cptr.val n.val) σ) σ (WalkRelA th (L.mem s) c r) ⦄ := by + unfold cspace.resolve_address_bits cspace.resolve_address_bits_loop + apply loop.spec_decr_nat (measure := fun x => x.2.1.val) + (inv := WalkInvA th (L.mem s) σ cptr c (a, bitsOfA cptr.val n.val)) + · rintro ⟨c1, n1, f1⟩ hx + have h := body_specA th s σ hR hV cptr c _ c1 n1 f1 hx + convert h using 2 + rename_i r; cases r <;> rfl + · exact ⟨Or.inr rfl, hn, by simp; omega, a, hc, hcv, rfl⟩ + +end WalkProof + +/-! ## `lookup_slot_for_cnode_op` -/ + +/-- What `lookup_slot_for_cnode_op` makes of the walk's outcome (x86 `cnodeOpOutcome`). -/ +def cnodeOpOutcomeA (b : Bool) : Except LookupFailure (CslotPtr × CapRef) → Except SyscallError CslotPtr + | .ok (sl, []) => .ok sl + | .ok (_, rem) => .error (.failedLookup b (.depthMismatch rem.length 0)) + | .error lf => .error (.failedLookup b lf) + +theorem lsfco_noncnodeA (b : Bool) (croot : Cap) (ptr : CapRef) (depth : Nat) (σ : State Unit) + (h : isCnodeCap croot = false) : + (lookupSlotForCnodeOp (Z := Unit) b croot ptr depth).run σ = + ({(.error (.failedLookup b .invalidRoot), σ)}, False) := by + unfold lookupSlotForCnodeOp; simp only [h, Bool.false_eq_true, ↓reduceIte]; rfl + +theorem lsfco_rangeA (b : Bool) (croot : Cap) (ptr : CapRef) (depth : Nat) (σ : State Unit) + (h : isCnodeCap croot = true) (hd : depth < 1 ∨ depth > wordBits) : + (lookupSlotForCnodeOp (Z := Unit) b croot ptr depth).run σ = + ({(.error (.rangeError 1 (BitVec.ofNat 64 wordBits)), σ)}, False) := by + unfold lookupSlotForCnodeOp + simp only [h, NonDetE.whenE, hd, ↓reduceIte, NonDetE.throwError, ndE_throw_bindA]; rfl + +theorem lsfco_walkA (b : Bool) (croot : Cap) (ptr : CapRef) (depth : Nat) (σ : State Unit) + (h : isCnodeCap croot = true) (hd : ¬ (depth < 1 ∨ depth > wordBits)) {e} + (hw : (resolveAddressBits' (Z := Unit) (croot, ptr.drop (ptr.length - depth))).run σ = ({(e, σ)}, False)) : + (lookupSlotForCnodeOp (Z := Unit) b croot ptr depth).run σ = ({(cnodeOpOutcomeA b e, σ)}, False) := by + unfold lookupSlotForCnodeOp; simp only [h, NonDetE.whenE, hd, ↓reduceIte, ndE_pure_bindA] + unfold lookupErrorOnFailure NonDetE.handleE NonDetE.returnOk resolveAddressBits + rw [ndE_pure_bindA] + have hm : ∃ e', (resolveAddressBits' (Z := Unit) (croot, ptr.drop (ptr.length - depth)) >>= fun __x => + match __x.2 with + | [] => (pure __x.1 : LFMonad Unit CslotPtr) + | _ => NonDetE.throwError (LookupFailure.depthMismatch __x.2.length 0)).run σ = ({(e', σ)}, False) ∧ + cnodeOpOutcomeA b e = (match e' with | .ok a => .ok a | .error lf => .error (.failedLookup b lf)) := by + cases e with + | ok v => + obtain ⟨sl, rem⟩ := v + rw [run_bind_okA hw] + cases rem with + | nil => exact ⟨.ok sl, rfl, rfl⟩ + | cons r rs => exact ⟨.error (.depthMismatch (r :: rs).length 0), rfl, rfl⟩ + | error lf => exact ⟨.error lf, run_bind_errorA hw _, rfl⟩ + obtain ⟨e', he', hout⟩ := hm + rw [ExceptT.run_mk] + refine (nd_bind_singleA' (a := e') ?_ _).trans ?_ + · exact he' + · rw [hout]; cases e' <;> rfl + +/-- Syscall errors (x86 `SysErrRel`: the cases `lookup_slot_for_cnode_op` and the CNode invocations produce). -/ +def SysErrRelA : error.Exc → SyscallError → Prop + | .Sys (.RangeError mn mx), .rangeError mn' mx' => mn' = mn.bv ∧ mx' = mx.bv + | .Sys (.FailedLookup src lf), .failedLookup src' lf' => src' = src ∧ LookupFaultRelA lf lf' + | .Sys .IllegalOperation, .illegalOperation => True + | .Sys .AlignmentError, .alignmentError => True + | .Sys (.InvalidArgument n), .invalidArgument n' => n' = n.val + | .Sys (.NotEnoughMemory b), .notEnoughMemory b' => b' = b.bv + | .Sys .TruncatedMessage, .truncatedMessage => True + | .Sys .DeleteFirst, .deleteFirst => True + | .Sys .RevokeFirst, .revokeFirst => True + | _, _ => False + +/-- `lookup_slot_for_cnode_op` results (x86 `SlotResRel`): the slot's address, a slot of a CNode of `th` reached +from the root, or the corresponding syscall error. -/ +def SlotResRelA (th : TypedHeapA) (mem : U64 → U64) (root : capability.Cap) : + core.result.Result obj.Slot error.Exc → Except SyscallError CslotPtr → Prop + | .Ok sl, .ok sl' => sl = cslotAddrA sl' ∧ ∃ p, sl'.1 = (specPtr p) ∧ th p = some (.cnode sl'.2.length) ∧ + CnRefA th mem root p + | .Err e, .error e' => SysErrRelA e e' + | _, _ => False + +section LookupProof +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **FL4's `lookup_slot_for_cnode_op` refines l4v `lookup_slot_for_cnode_op`** (x86 +`lookup_slot_for_cnode_op_refines`; module docstring). -/ +theorem lookup_slot_for_cnode_op_refinesA (th : TypedHeapA) (s : S) (σ : State Unit) + (hR : StateRelA th (L.mem s) σ) (hV : SlotsValidA th (L.mem s)) (b : Bool) + (root : capability.Cap) (a : Cap) (hc : CapRelA root a) (hcv : CapValidA th root) + (cptr depth : U64) : + cspace.lookup_slot_for_cnode_op st s b root cptr depth ⦃ r => + SingleA ((lookupSlotForCnodeOp (Z := Unit) b a (toBl cptr.bv) depth.val).run σ) σ + (SlotResRelA th (L.mem s) root r) ⦄ := by + obtain ⟨hwf, heq⟩ := hc + unfold cspace.lookup_slot_for_cnode_op + cases root + case CNode ptr radix gs guard => + subst heq + have hcn : isCnodeCap (absCapA (.CNode ptr radix gs guard)) = true := rfl + simp only [bind_ok, ↓reduceIte] + by_cases h1 : depth < 1#u64 + · simp only [h1, ↓reduceIte, WP.spec_ok] + refine ⟨_, lsfco_rangeA _ _ _ _ σ hcn (Or.inl (by scalar_tac)), ?_⟩ + simp [SlotResRelA, SysErrRelA, cspace.WORD_BITS, wordBits] + by_cases h2 : depth > cspace.WORD_BITS + · simp only [h1, h2, ↓reduceIte, WP.spec_ok] + refine ⟨_, lsfco_rangeA _ _ _ _ σ hcn (Or.inr ?_), ?_⟩ + · simp [cspace.WORD_BITS] at h2; simp [wordBits]; scalar_tac + · simp [SlotResRelA, SysErrRelA, cspace.WORD_BITS, wordBits] + simp only [h1, h2, ↓reduceIte] + have hd : depth.val ≤ 64 := by simp [cspace.WORD_BITS] at h2; scalar_tac + have hd' : ¬ (depth.val < 1 ∨ depth.val > wordBits) := by + simp [wordBits]; constructor + · scalar_tac + · omega + step with resolve_address_bits_refinesA th s σ hR hV _ _ ⟨hwf, rfl⟩ hcv cptr depth hd as ⟨r, hr⟩ + obtain ⟨e, he, hrel, hok⟩ := hr + have hlen : (toBl cptr.bv).length = 64 := by simp [toBl_bitsOfA] + rw [lsfco_walkA _ _ _ _ σ hcn hd' (e := e) (by rw [hlen, toBl_dropA _ _ hd]; exact he)] + cases r with + | Ok r1 => + cases e with + | error lf => exact hrel.elim + | ok v => + obtain ⟨sl, rest⟩ := v + obtain ⟨hsl, hbl⟩ := hrel + by_cases hz : r1.bits_left = 0#u64 + · have hrest : rest = [] := by + rw [← List.length_eq_zero_iff, ← hbl, hz]; rfl + subst hrest + simp only [hz, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + exact ⟨_, rfl, hsl, hok⟩ + · have hne : (r1.bits_left != 0#u64) = true := by simpa using hz + simp only [hne, ↓reduceIte, WP.spec_ok] + cases rest with + | nil => exact absurd (UScalar.eq_of_val_eq (by simpa using hbl)) hz + | cons x xs => + refine ⟨_, rfl, ?_⟩ + simp [SlotResRelA, SysErrRelA, LookupFaultRelA, cnodeOpOutcomeA, hbl] + | Err lf => + cases e with + | ok v => exact hrel.elim + | error lf' => + simp only [WP.spec_ok] + exact ⟨_, rfl, rfl, hrel⟩ + all_goals + simp only [bind_ok, Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + exact ⟨_, lsfco_noncnodeA _ _ _ _ σ (by rw [heq.isCnode_eq]; rfl), rfl, trivial⟩ + +end LookupProof + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/MachKeepA.lean b/proofs/Fl4ProofsA64/P3/MachKeepA.lean new file mode 100644 index 000000000..9ae25063b --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/MachKeepA.lean @@ -0,0 +1,121 @@ +/- +P3 (aarch64, A64-P3 Lift 1; Architect ruling on record edit #8): **the arch-to-spec machine relation stays outside +`JStage3A`**, as in x86's `Fl4Proofs/P3/MachKeep`. The record is polymorphic in `AR`; an arm that does not touch the +clock or the IRQ masks is instantiated at `AR := withMRA AR0 MR ms` (`ms` the entry's machine state). + +* **`MRKeepA arI AR`** [P] (x86 `MRKeep`): the state-returning arch operations other than the clock (`now`) and the + IRQ-controller ones (`irq_pending`, `take_pending_irq`, `mask_irq`, `ack_irq`, `irq_free`, `configure_irq`) keep + `AR` at every second argument. Use it at `AR := MR`. +* **`withMRA AR0 MR ms`**: `AR0` and `MR` at the fixed spec machine state `ms`. +* **`ArchKeepsA.withMR`**: `ArchKeepsA` at `withMRA`, from `ArchKeepsA` at `AR0` and `MRKeepA` of `MR`. +* **`JStage3A.arStateA`**: the record at another arch state the relation holds of. +* **`JStage3A.arSwapA`**: the record at another arch relation that holds of the arch state. Arms that step the clock or + the masks (IRQ, Yield, timer / interrupt / fault entries) step `MR` directly and use it. + +Named premises: [P] `MRKeepA`, `MsiKeepA`. +-/ +import Fl4ProofsA64.P3.VsMapJA +import Fl4ProofsA64.P3.Stage3A +import Fl4ProofsA64.P3.CnodeCapsA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 + +noncomputable section + +section +variable {A C : Type} (arI : machine.Arch A) + +/-- **[P] The state-returning arch operations other than the clock and the IRQ controller keep `AR`** (module +docstring). -/ +structure MRKeepA (AR : A → C → Prop) : Prop where + set_deadline : ∀ a d c, AR a c → arI.set_deadline a d ⦃ a' => AR a' c ⦄ + send_reschedule : ∀ a x c, AR a c → arI.send_reschedule a x ⦃ a' => AR a' c ⦄ + remote_stall : ∀ a cpu c, AR a c → arI.remote_stall a cpu ⦃ a' => AR a' c ⦄ + fpu_flush : ∀ a t c, AR a c → arI.fpu_flush a t ⦃ a' => AR a' c ⦄ + fpu_release : ∀ a t c, AR a c → arI.fpu_release a t ⦃ a' => AR a' c ⦄ + fpu_sanitise : ∀ a t c, AR a c → arI.fpu_sanitise a t ⦃ a' => AR a' c ⦄ + fpu_reload : ∀ a t c, AR a c → arI.fpu_reload a t ⦃ a' => AR a' c ⦄ + vcpu_barrier : ∀ a c, AR a c → arI.vcpu_barrier a ⦃ a' => AR a' c ⦄ + vcpu_sync : ∀ a x y c, AR a c → arI.vcpu_sync a x y ⦃ a' => AR a' c ⦄ + vcpu_release : ∀ a x c, AR a c → arI.vcpu_release a x ⦃ a' => AR a' c ⦄ + vcpu_flush : ∀ a x c, AR a c → arI.vcpu_flush a x ⦃ a' => AR a' c ⦄ + vcpu_flush_state : ∀ a x y c, AR a c → arI.vcpu_flush_state a x y ⦃ a' => AR a' c ⦄ + cache_op : ∀ a op root asid va n pa c, AR a c → arI.cache_op a op root asid va n pa ⦃ a' => AR a' c ⦄ + invalidate_page : ∀ a x y c, AR a c → arI.invalidate_page a x y ⦃ a' => AR a' c ⦄ + invalidate_asid : ∀ a x c, AR a c → arI.invalidate_asid a x ⦃ a' => AR a' c ⦄ + invalidate_stage2_page : ∀ a x y c, AR a c → arI.invalidate_stage2_page a x y ⦃ a' => AR a' c ⦄ + invalidate_stage2_vmid : ∀ a x c, AR a c → arI.invalidate_stage2_vmid a x ⦃ a' => AR a' c ⦄ + invalidate_high_page : ∀ a x c, AR a c → arI.invalidate_high_page a x ⦃ a' => AR a' c ⦄ + invalidate_high_vspace : ∀ a x c, AR a c → arI.invalidate_high_vspace a x ⦃ a' => AR a' c ⦄ + set_vspace : ∀ a x y c, AR a c → arI.set_vspace a x y ⦃ a' => AR a' c ⦄ + set_stage2_vspace : ∀ a x y c, AR a c → arI.set_stage2_vspace a x y ⦃ a' => AR a' c ⦄ + set_high_vspace : ∀ a x y c, AR a c → arI.set_high_vspace a x y ⦃ a' => AR a' c ⦄ + +end + +/-- **[P] `msi_map` keeps `AR`** at every second argument: the IRQ control decoder's `msi_map` changes the arch +state before it may return an `Err` (`P3/Stage3ErrIrqA`). -/ +structure MsiKeepA {A C : Type} (arI : machine.Arch A) (AR : A → C → Prop) : Prop where + msi_map : ∀ a irq dev ev cpu c, AR a c → arI.msi_map a irq dev ev cpu ⦃ r => AR r.2 c ⦄ + +/-- `AR0` together with `MR` at the fixed spec machine state `ms`. -/ +def withMRA {A C : Type} (AR0 : A → C → Prop) (MR : A → Fl4SpecA64.MachineState → Prop) + (ms : Fl4SpecA64.MachineState) : A → C → Prop := fun a c => AR0 a c ∧ MR a ms + +theorem ArchKeepsA.withMR {A : Type} {ar : machine.Arch A} {AR0 : A → HwRootA → Prop} + {MR : A → Fl4SpecA64.MachineState → Prop} (K : ArchKeepsA ar AR0) (M : MRKeepA ar MR) + (ms : Fl4SpecA64.MachineState) : ArchKeepsA ar (withMRA AR0 MR ms) := by + have both : ∀ {r : Result A} {c : HwRootA}, r ⦃ a' => AR0 a' c ⦄ → r ⦃ a' => MR a' ms ⦄ → + r ⦃ a' => withMRA AR0 MR ms a' c ⦄ := by + intro r c h1 h2 + obtain ⟨x, e, p⟩ := spec_existsA h1 + rw [e, WP.spec_ok] at h2 ⊢ + exact ⟨p, h2⟩ + exact + { invPage := fun a h x y ha => both (K.invPage a h x y ha.1) (M.invalidate_page a x y ms ha.2) + invAsid := fun a h x ha => both (K.invAsid a h x ha.1) (M.invalidate_asid a x ms ha.2) + cacheOp := fun a h o r s v n p ha => + both (K.cacheOp a h o r s v n p ha.1) (M.cache_op a o r s v n p ms ha.2) + invS2Page := fun a h x y ha => both (K.invS2Page a h x y ha.1) (M.invalidate_stage2_page a x y ms ha.2) + invS2Vmid := fun a h x ha => both (K.invS2Vmid a h x ha.1) (M.invalidate_stage2_vmid a x ms ha.2) + invHiPage := fun a h x ha => both (K.invHiPage a h x ha.1) (M.invalidate_high_page a x ms ha.2) + invHiVs := fun a h x ha => both (K.invHiVs a h x ha.1) (M.invalidate_high_vspace a x ms ha.2) + hwAccessFlag := K.hwAccessFlag } + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR AR' : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- **The record at another arch relation** (module docstring). -/ +theorem JStage3A.arSwapA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) (h : AR' a Z.hw) : + JStage3A st AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m := + by + have j := J.j.1 + have x : JAtZA (L := L) AR' Z s a σ m := + { st := j.st, cdt := j.cdt, sch := j.sch, ar := h, place := j.place, irq := j.irq, rest := j.rest + thT := j.thT, csN := j.csN, idleT := j.idleT, idleC := j.idleC, curT := j.curT, cscC := j.cscC + ksFit := j.ksFit, scOffD := j.scOffD, cv := j.cv, pml := j.pml, vs := j.vs, ext := j.ext } + exact { J with j := ⟨x, J.j.2⟩ } + +/-- **The record at another arch state** that the arch relation holds of. -/ +theorem JStage3A.arStateA {a' : A} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) (h : AR a' Z.hw) : + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a' σ m := + by + have j := J.j.1 + have x : JAtZA (L := L) AR Z s a' σ m := + { st := j.st, cdt := j.cdt, sch := j.sch, ar := h, place := j.place, irq := j.irq, rest := j.rest + thT := j.thT, csN := j.csN, idleT := j.idleT, idleC := j.idleC, curT := j.curT, cscC := j.cscC + ksFit := j.ksFit, scOffD := j.scOffD, cv := j.cv, pml := j.pml, vs := j.vs, ext := j.ext } + exact { J with j := ⟨x, J.j.2⟩ } + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/MoveA.lean b/proofs/Fl4ProofsA64/P3/MoveA.lean new file mode 100644 index 000000000..a90270049 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/MoveA.lean @@ -0,0 +1,543 @@ +/- +P3 (aarch64, A64-P3 S3 gen 5, ruling 11:09Z step 2): **the joint `cap_move` leaf: FL4's `cdt::move_cap` refines +l4v `cap_move`** (CSpace_A, nondeterministic spec, `Z = Unit`) between two slots of the typed heap (gen 8: CNode or TCB slots), with the +whole `JAtZAX` carried — the twin of x86's `P3/MoveRefine` (`move_refines`, `CdtRel.move`), on `EmptyA`'s slot and +spec machinery and `SlotJA`'s frame. + +* **Bit 48 exactly** (A64 P1's `cdt.move_cap.rvb` gives only "set ⇒ set"): `nf_bitsRA`, `set_nf_rvE` (the word + `set_next_flags` writes has bit 48 = its `revocable` argument), (gen 6, ruling 11:49Z q2) **`BitKA`** / + **`linkBitA`** (the one shared helper: a null-skipped `set_next` / `set_prev` of an `OS` slot keeps every `OS` + slot's bit; used by `move_cap`, `swap` (SwapA) and `insert_with_cap` (InsertA)), **`moveCap_bitA`** + (`move_cap.rvb`'s walk with `dest`'s bit equal to the source's and the source's cleared; gen 6: its two link + writes through `linkBitA`). +* **`moveCap_osA`**: `move_cap`'s write set, `OS` slot words (A64 P1's slot primitives' `capw`, as `emptySlot_osA`). +* Spec side: `setCnA` (a CNode slot replaced, `setCap_cnode_memA`'s outcome), `moveFinA` / **`capMove_memA`** (an + outcome of `cap_move`, x86 `capMove_eq`), `SpecSlotA.trans` / `.fin`. +* **`StateRelA.of_kheap`**, the mixed memory `mixA` (the source's cap words from before, the rest after) with + `slot_ne8` (two slots' cap words are apart), so `StateRelA.setCn` runs twice (x86 `StateRel.update_cnode_slot` + twice in `move_refines`). +* **`CdtRelA.moveA`** (x86 `CdtRel.move`): the spec's CDT after `cap_move` against the ghost renamed by the + transposition of the two slots (`Mdb.conjPar`, A64 P1's `cdt.move_cap.tree`). +* **`cap_move_refinesA`** (x86 `move_refines`): from `StateRelA`, `CdtRelA`, `PlaceA` and A64 P1's `TreeA` at `OS` + (as `empty_slot_refinesA`), a non-null source, a null canonical destination and a related, encodable, non-null new + cap alike the source's, ∃ outcome of `cap_move` with `StateRelA`, `CdtRelA` and P1's tree at the renamed ghost + `mvGA`, a slot-only spec step, and every slot null, unchanged or the new cap. +* **`cap_move_jA`**: at `JEntA`, the whole `JAtZAX` across it (`JAtZAX.of_slotWrites`, `footN_offA`), given the new + cap names counted objects (`CapObjOkA`, from the source cap at the call site). + +Gen 8: the leaves take any `AbsSlotA` slots (`StateRelA.setAbsA`); `SpecGlobA.trans` / `SpecSlotA.trans` and +`StateRelA.of_kheap` moved to `FrameA` / `EmptyA`. Named premise: none (gen 7: `SlotApA` is `JEntA.ap`, no longer a premise; `CdtRelA.prev` kept, gen 7). +-/ +import Fl4ProofsA64.P3.SlotJA +import Fl4ProofsA64.P1.MdbTreeK +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## Bit 48 exactly -/ + +theorem nf_bitsRA (w n r f M P R : U64) (hP : P &&& R = 0#u64) (hf : f &&& R = 0#u64) (hM : ~~~M &&& R = 0#u64) : + ((((w &&& ~~~M) ||| (n &&& P)) ||| r) ||| f) &&& R = r &&& R := by + rw [UScalar.eq_equiv_bv_eq] at hP hf hM ⊢ + apply BitVec.eq_of_getLsbD_eq; intro i + have h1 := congrArg (fun x => BitVec.getLsbD x i) hP + have h2 := congrArg (fun x => BitVec.getLsbD x i) hf + have h3 := congrArg (fun x => BitVec.getLsbD x i) hM + simp only [UScalar.bv_and, UScalar.bv_or, UScalar.bv_not, BitVec.getLsbD_and, BitVec.getLsbD_or, + BitVec.getLsbD_not] at h1 h2 h3 ⊢ + revert h1 h2 h3 + cases w.bv.getLsbD i <;> cases n.bv.getLsbD i <;> cases r.bv.getLsbD i <;> cases f.bv.getLsbD i <;> + cases M.bv.getLsbD i <;> cases P.bv.getLsbD i <;> cases R.bv.getLsbD i <;> (try simp) <;> omega + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **`set_next_flags` writes bit 48 = `revocable`.** -/ +theorem set_nf_rvE (s : S) (x n : U64) (r f : Bool) : + obj.Slot.set_next_flags st x s n r f ⦃ s' => ∃ w, s' = L.wr s (wa x 24#u64) w ∧ + w &&& RVB = (if r then RVB else 0#u64) ⦄ := by + unfold obj.Slot.set_next_flags obj.Slot.next_word + simp only [lift, bind_ok, cte_next, L.read_eq, fb_val, rvb_val] + cases r <;> cases f <;> + simp only [Bool.false_eq_true, ↓reduceIte, bind_ok, L.write_eq, WP.spec_ok] <;> + (refine ⟨_, rfl, ?_⟩; rw [nf_bitsRA] <;> first | rfl | (unfold obj.MDB_PTR_MASK; rfl)) + +/-- A `NEXT` word's bit 48 as `revA`, from the exact bit. -/ +theorem revA_of_bit {m m' : U64 → U64} {a b : U64} + (h : m' (wa a 24#u64) &&& RVB = if ((m (wa b 24#u64) &&& RVB) != 0#u64) then RVB else 0#u64) : + revA m' a = revA m b := by + have e : (281474976710656#u64 : U64) = RVB := rfl + unfold revA; rw [e, h] + generalize ((m (wa b 24#u64) &&& RVB) != 0#u64) = c + cases c <;> rfl + +theorem revA_of_zero {m : U64 → U64} {a : U64} (h : m (wa a 24#u64) &&& RVB = 0#u64) : revA m a = false := by + have e : (281474976710656#u64 : U64) = RVB := rfl + unfold revA; rw [e, h]; rfl + +theorem revA_of_keep {m m' : U64 → U64} {a : U64} (h : m' (wa a 24#u64) &&& RVB = m (wa a 24#u64) &&& RVB) : + revA m' a = revA m a := by + have e : (281474976710656#u64 : U64) = RVB := rfl + unfold revA; rw [e, h] + +variable {T Ω Sg OS : Set U64} + +variable (st) in +/-- (gen 6) Bit 48 of every `OS` slot's `NEXT` word agrees between `s` and `s'`. -/ +def BitKA (OS : Set U64) (s s' : S) : Prop := + ∀ y ∈ OS, L.mem s' (wa y 24#u64) &&& RVB = L.mem s (wa y 24#u64) &&& RVB + +/-- (gen 6) **The shared bit-48 helper** (ruling 11:49Z q2, for `move_cap`, `swap` and `insert_with_cap`): a link +write skipped on a null link — `set_next` of an `OS` slot (pointer bits only, `bit_next`) or `set_prev` (the `PREV` +word, `w24_wr16`) — keeps bit 48 of every `OS` slot. With `set_nf_rvE` (the written slot's bit exactly) and +`w24_wr24` (another slot's `NEXT` write), it gives the exact bit-48 walks on the P3 side. -/ +theorem linkBitA (hO : OSOK T Ω Sg OS) (s : S) {q : Prop} [Decidable q] {p x : U64} (hp : ¬ q → p ∈ OS) + {f : S → Result S} + (hf : f = (fun s => obj.Slot.set_next st p s x) ∨ f = (fun s => obj.Slot.set_prev st p s x)) : + (if q then ok s else f s) ⦃ s' => BitKA st OS s s' ⦄ := by + by_cases h : q + · rw [if_pos h]; exact (WP.spec_ok _).2 fun _ _ => rfl + · rw [if_neg h] + rcases hf with rfl | rfl + · dsimp only; rw [set_next_eq, WP.spec_ok]; exact fun y hy => bit_next hO s (hp h) hy x + · dsimp only; rw [set_prev_eq, WP.spec_ok]; exact fun y hy => by rw [w24_wr16 hO s (hp h) hy] + +set_option maxHeartbeats 2000000 in +/-- **`move_cap` and bit 48, exactly** (A64 P1's `cdt.move_cap.rvb` walk, `set_nf_rvE` for `set_nf_rv`): every slot +but the two keeps its bit, `dest` gets the source's, the source's is clear. -/ +theorem moveCap_bitA (hO : OSOK T Ω Sg OS) {s : S} {src dest : U64} {nc : capability.Cap} + (hsrc : src ∈ OS) (hd : dest ∈ OS) (hsd : src ≠ dest) (hw : CapRt.EncWf nc) + (hpP : L.mem s (wa src 16#u64) ≠ 0#u64 → L.mem s (wa src 16#u64) ∈ OS) + (hnN : NxA st s src ≠ 0#u64 → NxA st s src ∈ OS) : + cdt.move_cap st s nc src dest ⦃ s' => + (∀ y ∈ OS, y ≠ src → y ≠ dest → L.mem s' (wa y 24#u64) &&& RVB = L.mem s (wa y 24#u64) &&& RVB) ∧ + L.mem s' (wa dest 24#u64) &&& RVB = + (if ((L.mem s (wa src 24#u64) &&& RVB) != 0#u64) then RVB else 0#u64) ∧ + L.mem s' (wa src 24#u64) &&& RVB = 0#u64 ⦄ := by + have hS : P0.StoreTotal st := L.total + unfold cdt.move_cap + rw [slot_prev_eq s src, bind_ok, next_eq, bind_ok, revocable_eqA, bind_ok] + refine WP.spec_bind (P0.obj.Slot.first_badged.p0 st src s hS) fun f1 _ => ?_ + obtain ⟨w0, w1, he, -⟩ := CapRt.decode_encode nc hw + rw [set_cap_eq he, bind_ok] + have K1 : ∀ y ∈ OS, L.mem (L.wr (L.wr s dest w0) (wa dest 8#u64) w1) (wa y 24#u64) = L.mem s (wa y 24#u64) := + fun y hy => (set_cap_links hO hd y hy).2 + generalize L.wr (L.wr s dest w0) (wa dest 8#u64) w1 = s1 at K1 ⊢ + rw [set_prev_eq, bind_ok] + have K2 : ∀ y ∈ OS, L.mem (L.wr s1 (wa dest 16#u64) (L.mem s (wa src 16#u64))) (wa y 24#u64) = + L.mem s (wa y 24#u64) := fun y hy => (w24_wr16 hO s1 hd hy _).trans (K1 y hy) + generalize L.wr s1 (wa dest 16#u64) (L.mem s (wa src 16#u64)) = s2 at K2 ⊢ + refine WP.spec_bind (set_nf_rvE s2 dest _ _ f1) ?_ + rintro s3 ⟨w3, rfl, hw3⟩ + have K3 : ∀ y ∈ OS, y ≠ dest → L.mem (L.wr s2 (wa dest 24#u64) w3) (wa y 24#u64) = L.mem s (wa y 24#u64) := + fun y hy e => (w24_wr24 hO s2 hd hy e w3).trans (K2 y hy) + have D3 : L.mem (L.wr s2 (wa dest 24#u64) w3) (wa dest 24#u64) &&& RVB = + (if ((L.mem s (wa src 24#u64) &&& RVB) != 0#u64) then RVB else 0#u64) := by + rw [w24_self hO s2 hd]; exact hw3 + generalize L.wr s2 (wa dest 24#u64) w3 = s3 at K3 D3 ⊢ + rw [clear_eq, bind_ok] + have K4 : ∀ y ∈ OS, y ≠ src → L.mem (L.wr (L.wr (L.wr (L.wr s3 src 0#u64) (wa src 8#u64) 0#u64) + (wa src 16#u64) 0#u64) (wa src 24#u64) 0#u64) (wa y 24#u64) = L.mem s3 (wa y 24#u64) := fun y hy e => by + rw [w24_wr24 hO _ hsrc hy e, w24_wr16 hO _ hsrc hy, (set_cap_links hO hsrc y hy).2] + have Z4 : L.mem (L.wr (L.wr (L.wr (L.wr s3 src 0#u64) (wa src 8#u64) 0#u64) + (wa src 16#u64) 0#u64) (wa src 24#u64) 0#u64) (wa src 24#u64) = 0#u64 := w24_self hO _ hsrc _ + generalize L.wr (L.wr (L.wr (L.wr s3 src 0#u64) (wa src 8#u64) 0#u64) (wa src 16#u64) 0#u64) + (wa src 24#u64) 0#u64 = s4 at K4 Z4 ⊢ + simp only [obj.Slot.is_null, bind_ok] + refine WP.spec_bind (linkBitA hO s4 (p := L.mem s (wa src 16#u64)) (x := dest) ?_ (Or.inl rfl)) + fun s5 h5 => ?_ + · exact fun h => hpP (by simpa using h) + refine WP.spec_mono (linkBitA hO s5 (p := NxA st s src) (x := dest) ?_ (Or.inr rfl)) fun s6 h6 => ?_ + · exact fun h => hnN (by simpa using h) + exact ⟨fun y hy e1 e2 => by rw [h6 y hy, h5 y hy, K4 y hy e1, K3 y hy e2], + by rw [h6 dest hd, h5 dest hd, K4 dest hd hsd.symm]; exact D3, + by rw [h6 src hsrc, h5 src hsrc, Z4]; rfl⟩ + +set_option maxHeartbeats 1000000 in +/-- **`move_cap`'s write set** (module docstring): `OS` slot words or TCB slot words. -/ +theorem moveCap_osA (hO : OSOK T Ω Sg OS) {s : S} {src dest : U64} {nc : capability.Cap} + (hsrc : src ∈ OS) (hd : dest ∈ OS) + (hpP : L.mem s (wa src 16#u64) ≠ 0#u64 → L.mem s (wa src 16#u64) ∈ OS) + (hnN : NxA st s src ≠ 0#u64 → NxA st s src ∈ OS) : + cdt.move_cap st s nc src dest ⦃ s' => WritesIn st s s' (W7.CapW T {b | InOS OS b} ∅) ⦄ := by + have hS : P0.StoreTotal st := L.total + unfold cdt.move_cap + rw [slot_prev_eq s src, bind_ok, next_eq, bind_ok, revocable_eqA, bind_ok] + refine WP.spec_bind (P0.obj.Slot.first_badged.p0 st src s hS) fun f1 _ => ?_ + refine WP.spec_bind (W7.obj.Slot.set_cap.capw (osw hO hd)) fun s1 w1 => ?_ + refine WP.spec_bind (W7.obj.Slot.set_prev.capw (osw hO hd)) fun s2 w2 => ?_ + refine WP.spec_bind (W7.obj.Slot.set_next_flags.capw (osw hO hd)) fun s3 w3 => ?_ + refine WP.spec_bind (W7.obj.Slot.clear.capw (osw hO hsrc)) fun s4 w4 => ?_ + have h4 := ((w1.trans1 w2).trans1 w3).trans1 w4 + simp only [obj.Slot.is_null, bind_ok] + refine WP.spec_bind (Pₘ := fun s5 => WritesIn st s s5 (W7.CapW T {b | InOS OS b} ∅)) ?_ fun s5 h5 => ?_ + · split + · exact (WP.spec_ok _).2 h4 + · rename_i hp0 + exact WP.spec_mono (W7.obj.Slot.set_next.capw (osw hO (hpP (by simpa using hp0)))) fun _ w => h4.trans1 w + split + · exact (WP.spec_ok _).2 h5 + · rename_i hn0 + exact WP.spec_mono (W7.obj.Slot.set_prev.capw (osw hO (hnN (by simpa using hn0)))) fun _ w => h5.trans1 w + +end + +/-! ## The spec's outcome -/ + +/-- A CNode slot replaced (`setCap_cnode_memA`'s outcome). -/ +def setCnA (σ : State Unit) (p : ObjRef) (r : Nat) (cs : CnodeContents) (idx : List Bool) (a : Cap) : State Unit := + { σ with kheap := Function.update σ.kheap p (some (.cnode r (Function.update cs idx (some a)))) } + +/-- The state after `cap_move`'s two `set_cap`s: its CDT and original-cap updates (x86 `capMove_eq`'s state). -/ +def moveFinA (σ : State Unit) (src dst : CslotPtr) : State Unit := + { σ with + cdt := fun r => if Function.update (Function.update σ.cdt dst (σ.cdt src)) src none r = some src then some dst + else Function.update (Function.update σ.cdt dst (σ.cdt src)) src none r + isOriginalCap := Function.update (Function.update σ.isOriginalCap dst (σ.isOriginalCap src)) src false } + +/-- **An outcome of `cap_move`** (x86 `capMove_eq`, as a membership). -/ +theorem capMove_memA {σ σ1 σ2 : State Unit} {a : Cap} {src dst : CslotPtr} + (h1 : ((), σ1) ∈ (setCap (Z := Unit) a dst σ).1) (h2 : ((), σ2) ∈ (setCap (Z := Unit) .nullCap src σ1).1) : + ((), moveFinA σ2 src dst) ∈ (capMove (Z := Unit) a src dst σ).1 := by + unfold capMove + refine mem_bindA.2 ⟨((), σ1), h1, ?_⟩ + refine mem_bindA.2 ⟨((), σ2), h2, ?_⟩ + refine mem_bindA.2 ⟨(σ2.cdt src, σ2), rfl, ?_⟩ + refine mem_bindA.2 ⟨(σ2.cdt dst, σ2), rfl, ?_⟩ + refine mem_bindA.2 ⟨(σ2.cdt, σ2), rfl, ?_⟩ + refine mem_bindA.2 ⟨(σ2.cdt src, σ2), rfl, ?_⟩ + refine mem_bindA.2 ⟨(_, σ2), rfl, ?_⟩ + refine mem_bindA.2 ⟨((), _), mem_bindA.2 ⟨(σ2, σ2), rfl, rfl⟩, ?_⟩ + refine mem_bindA.2 ⟨((), _), doExtendedOp_memA _ _, ?_⟩ + refine mem_bindA.2 ⟨(_, _), rfl, ?_⟩ + refine mem_bindA.2 ⟨((), _), mem_bindA.2 ⟨(_, _), rfl, rfl⟩, ?_⟩ + exact mem_bindA.2 ⟨(_, _), rfl, rfl⟩ + +theorem SpecSlotA.ofSetCnA {σ : State Unit} {p : ObjRef} {r : Nat} {cs : CnodeContents} (idx : List Bool) (a : Cap) + (hk : σ.kheap p = some (.cnode r cs)) : SpecSlotA σ (setCnA σ p r cs idx a) := + SpecSlotA.setCn hk rfl ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, ⟨rfl, rfl, rfl, rfl⟩⟩ + +theorem SpecSlotA.fin (σ : State Unit) (src dst : CslotPtr) : SpecSlotA σ (moveFinA σ src dst) := + ⟨⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, ⟨rfl, rfl, rfl, rfl⟩⟩, fun _ => Or.inl rfl⟩ + +/-! ## Two slots updated -/ + +/-- The cap words of `x` from `m`, every other word from `m'`. -/ +def mixA (m m' : U64 → U64) (x : U64) : U64 → U64 := fun w => if w = x ∨ w = wa x 8#u64 then m w else m' w + +theorem capAt_mixA_self (m m' : U64 → U64) (x : U64) : capAtA (mixA m m' x) x = capAtA m x := by + unfold capAtA mixA; simp + +theorem capAt_mixA_other (m m' : U64 → U64) {x y : U64} + (h : y ≠ x ∧ y ≠ wa x 8#u64 ∧ wa y 8#u64 ≠ x ∧ wa y 8#u64 ≠ wa x 8#u64) : + capAtA (mixA m m' x) y = capAtA m' y := by + obtain ⟨h1, h2, h3, h4⟩ := h + unfold capAtA mixA; simp [h1, h2, h3, h4] + +/-- A slot of a placed `th` is 32-aligned and does not wrap. -/ +theorem IsSlotA.al32 {th : TypedHeapA} {kb : U64} (hP : PlaceA th kb) {y : U64} (h : IsSlotA th y) : + y.val % 32 = 0 ∧ y.val + 32 ≤ 2 ^ 64 := by + obtain ⟨p, ⟨r, hr, i, hi, rfl⟩ | ⟨ht, i, hi, rfl⟩⟩ := h + · have hf := hP.fit p _ hr + have ha := hP.align p _ hr + rw [cnode_size] at hf ha + have hi' : 32 * (i + 1) ≤ 32 * 2 ^ r := by + have : i + 1 ≤ 2 ^ r := hi; omega + rw [slotAddrA_valN (by omega)] + have : p.val % 32 = 0 := by + have h32 : 32 ∣ 32 * 2 ^ r := ⟨2 ^ r, rfl⟩ + exact Nat.mod_eq_zero_of_dvd (Nat.dvd_trans h32 (Nat.dvd_of_mod_eq_zero ha)) + omega + · have hf := hP.fit p _ ht + have ha := hP.align p _ ht + simp only [ObjKindA.size] at hf ha + unfold tcbSlotsA at hi + rw [slotAddrA_valN (by omega)] + omega + +/-- **Two distinct slots' cap words are apart.** -/ +theorem slot_ne8 {th : TypedHeapA} {kb : U64} (hP : PlaceA th kb) {x y : U64} (hx : IsSlotA th x) + (hy : IsSlotA th y) (hne : y ≠ x) : y ≠ x ∧ y ≠ wa x 8#u64 ∧ wa y 8#u64 ≠ x ∧ wa y 8#u64 ≠ wa x 8#u64 := by + obtain ⟨ax, fx⟩ := hx.al32 hP + obtain ⟨ay, fy⟩ := hy.al32 hP + have vx : (wa x 8#u64).val = x.val + 8 := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by simp; omega) + have vy : (wa y 8#u64).val = y.val + 8 := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by simp; omega) + have hv : y.val ≠ x.val := fun e => hne (UScalar.eq_of_val_eq e) + refine ⟨hne, fun e => ?_, fun e => ?_, fun e => ?_⟩ + · have := congrArg UScalar.val e; rw [vx] at this; omega + · have := congrArg UScalar.val e; rw [vy] at this; omega + · have := congrArg UScalar.val e; rw [vx, vy] at this; omega + +/-! ## The CDT after `cap_move` -/ + +section cdt +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +set_option maxHeartbeats 1000000 in +/-- **The spec's CDT after `cap_move`** against the ghost renamed by the transposition (x86 `CdtRel.move`). -/ +theorem CdtRelA.moveA {th : TypedHeapA} {kb : U64} (hP : PlaceA th kb) {s s' : S} {σ σ' : State Unit} + {G : MdbGhostA} (hC : CdtRelA st th s σ G) {chains' : List (List U64)} {srcSl destSl : CslotPtr} + (hs : AbsSlotA th srcSl) (hd : AbsSlotA th destSl) (hsrcm : cslotAddrA srcSl ∈ G.chains.flatten) + (hdestm : cslotAddrA destSl ∉ G.chains.flatten) + (htr : TreeA st {a | IsSlotA th a} chains' + (Fl4Proofs.P3.Mdb.conjPar (Equiv.swap (cslotAddrA destSl) (cslotAddrA srcSl)) G.par) s') + (hcdt : σ'.cdt = fun r => + if Function.update (Function.update σ.cdt destSl (σ.cdt srcSl)) srcSl none r = some srcSl then some destSl + else Function.update (Function.update σ.cdt destSl (σ.cdt srcSl)) srcSl none r) + (hio : σ'.isOriginalCap = + Function.update (Function.update σ.isOriginalCap destSl (σ.isOriginalCap srcSl)) srcSl false) + (hrev : ∀ sl, AbsSlotA th sl → revA (L.mem s') (cslotAddrA sl) = if sl = destSl then + revA (L.mem s) (cslotAddrA srcSl) else if sl = srcSl then false else revA (L.mem s) (cslotAddrA sl)) : + CdtRelA st th s' σ' + ⟨chains', Fl4Proofs.P3.Mdb.conjPar (Equiv.swap (cslotAddrA destSl) (cslotAddrA srcSl)) G.par⟩ := by + have hsw : Equiv.swap (cslotAddrA destSl) (cslotAddrA srcSl) = Equiv.swap (cslotAddrA srcSl) (cslotAddrA destSl) := + Equiv.swap_comm _ _ + rw [hsw] at htr ⊢ + have hne : srcSl ≠ destSl := by rintro rfl; exact hdestm hsrcm + have hAne : cslotAddrA srcSl ≠ cslotAddrA destSl := fun h => hne (cslotAddrA_inj hP hs hd h) + have hpd : G.par (cslotAddrA destSl) = none := by + cases h : G.par (cslotAddrA destSl) with + | none => rfl + | some b => exact absurd (hC.tree.tree.par_mem _ _ h).1 hdestm + have hnpd : ∀ z, G.par z ≠ some (cslotAddrA destSl) := fun z h => hdestm (hC.tree.tree.par_mem _ _ h).2 + have hnc : ∀ sl, σ.cdt sl ≠ some destSl := by + intro sl h + have hsl := (hC.cdt_slot sl _ h).1 + have := hC.cdt sl hsl; rw [h] at this + exact hnpd _ (by simpa using this.symm) + have hss : σ.cdt srcSl ≠ some srcSl := by + intro h; have := hC.cdt srcSl hs; rw [h] at this + exact hC.tree.noself _ (by simpa using this.symm) + have hupd : ∀ r, Function.update (Function.update σ.cdt destSl (σ.cdt srcSl)) srcSl none r = + if r = srcSl then none else if r = destSl then σ.cdt srcSl else σ.cdt r := by + intro r + by_cases h1 : r = srcSl + · subst r; simp + · by_cases h2 : r = destSl + · subst r; simp [h1] + · simp [h1, h2] + have hpar_fix : ∀ q, G.par (cslotAddrA srcSl) = some q → + (Equiv.swap (cslotAddrA srcSl) (cslotAddrA destSl)) q = q := by + intro q hq + apply Equiv.swap_apply_of_ne_of_ne + · intro h; subst h; exact hC.tree.noself _ hq + · intro h; subst h; exact hnpd _ hq + have hcp : ∀ x, Fl4Proofs.P3.Mdb.conjPar (Equiv.swap (cslotAddrA srcSl) (cslotAddrA destSl)) G.par x = + (G.par ((Equiv.swap (cslotAddrA srcSl) (cslotAddrA destSl)) x)).map + (Equiv.swap (cslotAddrA srcSl) (cslotAddrA destSl)) := by + intro x; simp [Fl4Proofs.P3.Mdb.conjPar, Equiv.symm_swap] + have hfix : ∀ sl, AbsSlotA th sl → sl ≠ srcSl → sl ≠ destSl → + (Equiv.swap (cslotAddrA srcSl) (cslotAddrA destSl)) (cslotAddrA sl) = cslotAddrA sl := by + intro sl hsl h1 h2 + exact Equiv.swap_apply_of_ne_of_ne (fun h => h1 (cslotAddrA_inj hP hsl hs h)) + (fun h => h2 (cslotAddrA_inj hP hsl hd h)) + refine CdtRelA.mkA htr ?_ ?_ ?_ ?_ ?_ + · intro sl hsl + show Option.map cslotAddrA (σ'.cdt sl) = Fl4Proofs.P3.Mdb.conjPar _ G.par (cslotAddrA sl) + rw [hcdt, hcp]; simp only [hupd] + by_cases h1 : sl = srcSl + · rw [h1] + simp only [↓reduceIte, reduceCtorEq, Option.map_none, Equiv.swap_apply_left, hpd] + · by_cases h2 : sl = destSl + · rw [h2] + simp only [hne.symm, ↓reduceIte, hss, Equiv.swap_apply_right] + rw [hC.cdt srcSl hs] + cases hq : G.par (cslotAddrA srcSl) with + | none => rfl + | some q => simp [hpar_fix q hq] + · simp only [h1, h2, ↓reduceIte, hfix sl hsl h1 h2] + rw [← hC.cdt sl hsl] + by_cases h3 : σ.cdt sl = some srcSl + · simp [h3] + · rw [if_neg h3] + cases hq : σ.cdt sl with + | none => rfl + | some q => + have hq' := (hC.cdt_slot sl q hq).2 + have hqs : q ≠ srcSl := fun h => h3 (by rw [hq, h]) + have hqd : q ≠ destSl := fun h => hnc sl (by rw [hq, h]) + simp [hfix q hq' hqs hqd] + · intro sl q hq + rw [hcdt] at hq; simp only [hupd] at hq + by_cases h1 : sl = srcSl + · rw [h1] at hq; simp at hq + · by_cases h2 : sl = destSl + · rw [h2] at hq + simp only [hne.symm, ↓reduceIte, hss] at hq + exact ⟨h2 ▸ hd, (hC.cdt_slot _ _ hq).2⟩ + · simp only [h1, h2, ↓reduceIte] at hq + by_cases h3 : σ.cdt sl = some srcSl + · rw [if_pos h3] at hq; cases hq; exact ⟨(hC.cdt_slot _ _ h3).1, hd⟩ + · rw [if_neg h3] at hq; exact hC.cdt_slot _ _ hq + · intro sl hsl + rw [hio, hrev _ hsl] + by_cases h1 : sl = srcSl + · rw [h1]; simp [hne] + · by_cases h2 : sl = destSl + · rw [h2]; simp [hne.symm, hC.orig srcSl hs] + · simp only [Function.update_of_ne h1, Function.update_of_ne h2, h1, h2, ↓reduceIte] + exact hC.orig sl hsl + · intro a b hab + change Fl4Proofs.P3.Mdb.conjPar _ G.par a = some b at hab + rw [hcp] at hab + simp only [Option.map_eq_some_iff] at hab + obtain ⟨b0, hb0, -⟩ := hab + obtain ⟨sl0, hsl0, he0⟩ := hC.par_slot _ _ hb0 + by_cases h1 : (Equiv.swap (cslotAddrA srcSl) (cslotAddrA destSl)) a = (cslotAddrA srcSl) + · have : a = cslotAddrA destSl := by + have := congrArg (Equiv.swap (cslotAddrA srcSl) (cslotAddrA destSl)) h1; simpa using this + exact ⟨destSl, hd, this.symm⟩ + · by_cases h2 : (Equiv.swap (cslotAddrA srcSl) (cslotAddrA destSl)) a = (cslotAddrA destSl) + · rw [h2, hpd] at hb0; cases hb0 + · have ha1 : a ≠ (cslotAddrA srcSl) := fun h => h2 (by rw [h]; simp) + have ha2 : a ≠ (cslotAddrA destSl) := fun h => h1 (by rw [h]; simp) + exact ⟨sl0, hsl0, he0.trans (Equiv.swap_apply_of_ne_of_ne ha1 ha2)⟩ + · -- (gen 7) every parent is original: `src`'s children now name `dest`, which takes `src`'s flag + intro sl q hq + rw [hcdt] at hq; simp only [hupd] at hq + rw [hio] + by_cases h1 : sl = srcSl + · rw [h1] at hq; simp at hq + · by_cases h2 : sl = destSl + · rw [h2] at hq + simp only [hne.symm, ↓reduceIte, hss] at hq + have hqs : q ≠ srcSl := fun h => hss (by rw [hq, h]) + have hqd : q ≠ destSl := fun h => hnc srcSl (by rw [hq, h]) + rw [Function.update_of_ne hqs, Function.update_of_ne hqd]; exact hC.po _ _ hq + · simp only [h1, h2, ↓reduceIte] at hq + by_cases h3 : σ.cdt sl = some srcSl + · rw [if_pos h3] at hq; cases hq + rw [Function.update_of_ne hne.symm, Function.update_self]; exact hC.po _ _ h3 + · rw [if_neg h3] at hq + have hqs : q ≠ srcSl := fun h => h3 (by rw [hq, h]) + have hqd : q ≠ destSl := fun h => hnc sl (by rw [hq, h]) + rw [Function.update_of_ne hqs, Function.update_of_ne hqd]; exact hC.po _ _ hq + +end cdt + +/-! ## The joint leaf -/ + +/-- The ghost after moving `src` to `dst` (x86 `renG` with the transposition; A64 P1's `cdt.move_cap.tree`). -/ +def mvGA (G : MdbGhostA) (src dst : U64) : MdbGhostA := + ⟨renG dst src G.chains, Fl4Proofs.P3.Mdb.conjPar (Equiv.swap dst src) G.par⟩ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +set_option maxHeartbeats 2000000 in +/-- **FL4's `cdt::move_cap` refines l4v `cap_move`** between two slots of the typed heap (gen 8: CNode or TCB slots, +`AbsSlotA`; module docstring; x86 `move_refines`). -/ +theorem cap_move_refinesA {T Ω Sg OS : Set U64} {th : TypedHeapA} {kb : U64} (s : S) (σ : State Unit) + (G : MdbGhostA) (hR : StateRelA th (L.mem s) σ) (hC : CdtRelA st th s σ G) (hP : PlaceA th kb) + (hO : OSOK T Ω Sg OS) (hT : TreeA st OS G.chains G.par s) (hsub : ∀ y, IsSlotA th y → y ∈ OS) + (hnul : ∀ y ∈ OS, ¬ IsSlotA th y → capA st s y = .Null) + {ss sd : CslotPtr} (hAs : AbsSlotA th ss) (hAd : AbsSlotA th sd) + (hsn : capAtA (L.mem s) (cslotAddrA ss) ≠ .Null) + (hdn : capAtA (L.mem s) (cslotAddrA sd) = .Null) + (hdc : obj.sext48 (cslotAddrA sd) = ok (cslotAddrA sd)) + {nc : capability.Cap} {ac : Cap} (hca : CapRelA nc ac) (hw : CapRt.EncWf nc) (hn : nc ≠ .Null) + (hal : UA nc (capA st s (cslotAddrA ss))) : + cdt.move_cap st s nc (cslotAddrA ss) (cslotAddrA sd) ⦃ s' => + ∃ σ', ((), σ') ∈ (capMove (Z := Unit) ac ss sd σ).1 ∧ StateRelA th (L.mem s') σ' ∧ + CdtRelA st th s' σ' (mvGA G (cslotAddrA ss) (cslotAddrA sd)) ∧ + TreeA st OS (mvGA G (cslotAddrA ss) (cslotAddrA sd)).chains + (mvGA G (cslotAddrA ss) (cslotAddrA sd)).par s' ∧ SpecSlotA σ σ' ∧ + ∀ y, IsSlotA th y → capAtA (L.mem s') y = .Null ∨ capAtA (L.mem s') y = capAtA (L.mem s) y ∨ + capAtA (L.mem s') y = nc ⦄ := by + have hsS := hAs.isSlot hP + have hdS := hAd.isSlot hP + have hsG : cslotAddrA ss ∈ G.chains.flatten := (hT.lk.members _).2 ⟨hsub _ hsS, hsn⟩ + have hdG : cslotAddrA sd ∉ G.chains.flatten := fun m => ((hT.lk.members _).1 m).2 hdn + have hsd : cslotAddrA ss ≠ cslotAddrA sd := fun e => hdG (e ▸ hsG) + have F := hT.lk.lkF hO.nz + have inOS : ∀ y ∈ G.chains.flatten, y ∈ OS := fun y hy => ((hT.lk.members y).1 hy).1 + refine WP.spec_mono (WP.spec_and (WP.spec_and (cdt.move_cap.tree hO hT hsG (hsub _ hdS) hdn hdc hw hn hal) + (cdt.move_cap.lr hO (LR.self s) (hsub _ hsS) (hsub _ hdS) hsd hdc hw (fun h => inOS _ (F.pvm _ hsG h)) + (fun h => inOS _ (F.nxm _ hsG h)))) + (moveCap_bitA hO (hsub _ hsS) (hsub _ hdS) hsd hw (fun h => inOS _ (F.pvm _ hsG h)) + (fun h => inOS _ (F.nxm _ hsG h)))) ?_ + rintro s' ⟨⟨hT', -, ed, es, ek⟩, hb, hdb, hsb⟩ + -- the caps after + have hk' : ∀ y, IsSlotA th y → y ≠ cslotAddrA ss → y ≠ cslotAddrA sd → + capAtA (L.mem s') y = capAtA (L.mem s) y := fun y hy e1 e2 => ek y (hsub y hy) e1 e2 + -- the spec: `set_cap` at `dest` (through the memory with `src`'s cap words from before), then at `src` + obtain ⟨o1, h1, hR1, hS1⟩ := StateRelA.setAbsA (mem' := mixA (L.mem s) (L.mem s') (cslotAddrA ss)) hR hP hAd + (a := ac) (by + rw [capAt_mixA_other _ _ (slot_ne8 hP hsS hdS hsd.symm)] + show CapRelA (capA st s' _) ac + rw [ed]; exact hca) + (fun y hy hyd => by + by_cases e : y = cslotAddrA ss + · rw [e]; exact capAt_mixA_self _ _ _ + · rw [capAt_mixA_other _ _ (slot_ne8 hP hsS hy e)]; exact hk' y hy e hyd) + obtain ⟨o2, h2, hR2, hS2⟩ := StateRelA.setAbsA (mem' := L.mem s') hR1 hP hAs (a := .nullCap) + (by show CapRelA (capA st s' _) _; rw [es]; exact CapRelA.null) + (fun y hy hys => (capAt_mixA_other _ _ (slot_ne8 hP hsS hy hys)).symm) + refine ⟨_, capMove_memA h1 h2, hR2.of_kheap rfl, CdtRelA.moveA hP hC hAs hAd hsG hdG ?_ rfl rfl ?_, hT', + (hS1.trans hS2).trans (SpecSlotA.fin _ _ _), fun y hy => ?_⟩ + · refine hT'.sub (fun y hy => hsub y hy) fun y hy hn' => ?_ + have e1 : y ≠ cslotAddrA ss := fun e => hn' (e ▸ hsS) + have e2 : y ≠ cslotAddrA sd := fun e => hn' (e ▸ hdS) + rw [ek y hy e1 e2]; exact hnul y hy hn' + · intro sl hsl + by_cases e1 : sl = sd + · rw [if_pos e1, e1]; exact revA_of_bit hdb + · rw [if_neg e1] + by_cases e2 : sl = ss + · rw [if_pos e2, e2]; exact revA_of_zero hsb + · rw [if_neg e2] + exact revA_of_keep (hb _ (hsub _ (hsl.isSlot hP)) (fun h => e2 (cslotAddrA_inj hP hsl hAs h)) + (fun h => e1 (cslotAddrA_inj hP hsl hAd h))) + · by_cases e1 : y = cslotAddrA ss + · exact Or.inl (by rw [e1]; exact es) + · by_cases e2 : y = cslotAddrA sd + · exact Or.inr (Or.inr (by rw [e2]; exact ed)) + · exact Or.inr (Or.inl (hk' y hy e1 e2)) + +variable {A : Type} + +/-- **FL4's `cdt::move_cap` refines l4v `cap_move` with the whole relation** (module docstring; ruling 11:09Z +step 2). `hno`: the new cap names counted objects (it is derived from the source cap at the call site). -/ +theorem cap_move_jA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} + {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} {ia ie : Nat} {s : S} {a : A} {σ : State Unit} {m : McState} + (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + (hT : TreeA st OS Z.G.chains Z.G.par s) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) + {ss sd : CslotPtr} (hAs : AbsSlotA Z.th ss) (hAd : AbsSlotA Z.th sd) + (hsn : capAtA (L.mem s) (cslotAddrA ss) ≠ .Null) + (hdn : capAtA (L.mem s) (cslotAddrA sd) = .Null) + (hdc : obj.sext48 (cslotAddrA sd) = ok (cslotAddrA sd)) + {nc : capability.Cap} {ac : Cap} (hca : CapRelA nc ac) (hw : CapRt.EncWf nc) (hn : nc ≠ .Null) + (hal : UA nc (capA st s (cslotAddrA ss))) (hno : CapObjOkA Z nc) : + cdt.move_cap st s nc (cslotAddrA ss) (cslotAddrA sd) ⦃ s' => + ∃ σ', ((), σ') ∈ (capMove (Z := Unit) ac ss sd σ).1 ∧ + JAtZAX (L := L) AR { Z with G := mvGA Z.G (cslotAddrA ss) (cslotAddrA sd) } s' a σ' m ∧ + TreeA st OS (mvGA Z.G (cslotAddrA ss) (cslotAddrA sd)).chains + (mvGA Z.G (cslotAddrA ss) (cslotAddrA sd)).par s' ⦄ := by + have hO := E.sys.ent.os + have hsS : IsSlotA Z.th (cslotAddrA ss) := AbsSlotA.isSlot E.j.1.place hAs + have hsG : cslotAddrA ss ∈ Z.G.chains.flatten := (hT.lk.members _).2 ⟨hsub _ hsS, hsn⟩ + have F := hT.lk.lkF hO.nz + have inOS : ∀ y ∈ Z.G.chains.flatten, y ∈ OS := fun y hy => ((hT.lk.members y).1 hy).1 + have hdS : IsSlotA Z.th (cslotAddrA sd) := AbsSlotA.isSlot E.j.1.place hAd + refine WP.spec_mono (WP.spec_and (cap_move_refinesA s σ Z.G E.j.1.st E.j.1.cdt E.j.1.place hO hT hsub hnul + hAs hAd hsn hdn hdc hca hw hn hal) (moveCap_osA hO (hsub _ hsS) (hsub _ hdS) + (fun h => inOS _ (F.pvm _ hsG h)) (fun h => inOS _ (F.nxm _ hsG h)))) ?_ + rintro s' ⟨⟨σ', hm, hst, hcdt, hT', hSS, hcs⟩, hw'⟩ + refine ⟨σ', hm, E.j.of_slotWrites hw' (fun b hb' hb => footN_offA E b hb hb') + (FootNA.aligned E.j.1 (bxi_nodes E.sys.ent.bxi) E.scAl E.ap.vcAl) _ hSS hst hcdt fun y hy => ?_, hT'⟩ + rcases hcs y hy with e | e | e <;> rw [e] + · exact CapObjOkA.null Z + · exact E.j.capObj hy + · exact hno + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/NoChildA.lean b/proofs/Fl4ProofsA64/P3/NoChildA.lean new file mode 100644 index 000000000..fd0eb48f7 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/NoChildA.lean @@ -0,0 +1,157 @@ +/- +P3 (aarch64, A64-P3 S3 gen 10, ruling 13:44Z item 1): **FL4's `ensure_no_children` at an untyped slot decides the +spec's "has children"** (`noChildren_of_adjA`, discharging `CnodeCapsA`'s `NoChildrenRelA` for `derive_cap` of an +untyped source) — the A64 port of x86's `P3/NoChildren` (`noChildren_of_adj`). + +x86 decides it for every slot through the MDB invariant `ParentAdj`; on A64 only untyped sources need it (FL4's +`derive_cap` calls `ensure_no_children` only for them), and A64 P1's tree gives both directions at an untyped slot: +* **the ghost side** (`TreeA.hasChildUtA`): an untyped slot `u` of the tree has a child iff its `NEXT` slot is nonzero + and lies in `u`'s region (`same_region_as`). Forward: the next slot is the first descendant (`MdbOK`'s segments) + and descendants lie in the region (A64 P1's `TreeA.inU_desc`). Backward: a cap in the region is a descendant or an + ancestor (`UtInvA.drange`), and the next slot in the chain is no ancestor (`Mdb.desc_after`, the chain `Nodup`). + A64 P1's memory-side `cdt.ensure_no_children.noDesc` is the `Ok` half of the same fact; +* **the spec side** (`CdtRelA.hasChildA`, x86 `CdtRel.has_child_iff`): the spec's `cdt` has a child of `sl` iff the + ghost parent map has one of `cslotAddrA sl`; +* FL4's `is_mdb_parent_of u next` at a revocable untyped `u` is `same_region_as (cap u) (cap next)` (`UtRev`). + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.CnodeCapsA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **The spec's children through `CdtRelA`** (x86 `CdtRel.has_child_iff`). -/ +theorem CdtRelA.hasChildA {th : TypedHeapA} {kb : U64} (hP : PlaceA th kb) {s : S} {σ : Fl4SpecA64.State Unit} + {G : MdbGhostA} (hC : CdtRelA st th s σ G) {sl : Fl4SpecA64.CslotPtr} (hs : AbsSlotA th sl) : + (∃ c, σ.cdt c = some sl) ↔ ∃ a, G.par a = some (cslotAddrA sl) := by + constructor + · rintro ⟨c, hc⟩ + refine ⟨cslotAddrA c, ?_⟩ + rw [← hC.cdt c (hC.cdt_slot c sl hc).1, hc]; rfl + · rintro ⟨a, ha⟩ + obtain ⟨c, hcs, rfl⟩ := hC.par_slot a _ ha + have h1 := hC.cdt c hcs + rw [ha] at h1 + obtain ⟨q, hq, hqe⟩ := Option.map_eq_some_iff.1 h1 + exact ⟨c, by rw [hq, cslotAddrA_inj hP (hC.cdt_slot c q hq).2 hs hqe]⟩ + +theorem has_child_desc {par : U64 → Option U64} {u : U64} : + (∃ a, par a = some u) ↔ ∃ y, Fl4Proofs.P3.Mdb.Desc par y u := by + constructor + · rintro ⟨a, ha⟩; exact ⟨a, Relation.TransGen.single ha⟩ + · rintro ⟨y, hy⟩ + obtain ⟨b, -, hb⟩ := Relation.TransGen.tail'_iff.1 hy + exact ⟨b, hb⟩ + +/-- A nonzero `NEXT` of a slot of the tree is a slot of the tree. -/ +theorem TreeA.nxInA {OS : Set U64} {G : List (List U64)} {par : U64 → Option U64} {s : S} + (hT : TreeA st OS G par s) {u : U64} (hu : u ∈ G.flatten) (hn : NxA st s u ≠ 0#u64) : NxA st s u ∈ OS := by + obtain ⟨ch, hch, huc⟩ := List.mem_flatten.1 hu + obtain ⟨A, B, hsplit⟩ := List.append_of_mem huc + cases B with + | nil => + have : ch.getLast? = some u := by rw [hsplit]; simp + exact absurd (hT.lk.last ch hch u this) hn + | cons n B' => + rw [nx_split hT.lk hch (by rw [hsplit])] + exact ((hT.lk.members n).1 (List.mem_flatten.2 ⟨ch, hch, by rw [hsplit]; simp⟩)).1 + +/-- **An untyped slot of the tree has a child iff its `NEXT` slot is in its region** (module docstring). -/ +theorem TreeA.hasChildUtA {T Ω Sg OS : Set U64} {G : List (List U64)} {par : U64 → Option U64} {s : S} + (hO : OSOK T Ω Sg OS) (hT : TreeA st OS G par s) {u p b i : U64} {d : Bool} (hu : u ∈ OS) (hc : capA st s u = .Untyped p b i d) : + (∃ y, Fl4Proofs.P3.Mdb.Desc par y u) ↔ (NxA st s u ≠ 0#u64 ∧ NxA st s u ∈ OS ∧ + sraA (.Untyped p b i d) (capA st s (NxA st s u)) = true) := by + have huG : u ∈ G.flatten := (hT.lk.members u).2 ⟨hu, by rw [hc]; exact fun e => by cases e⟩ + obtain ⟨ch, hch, huc⟩ := List.mem_flatten.1 huG + obtain ⟨A, B, hsplit⟩ := List.append_of_mem huc + have hnd : ch.Nodup := (List.nodup_flatten.1 hT.lk.nodup).1 ch hch + have mem : ∀ z ∈ ch, z ∈ OS := fun z hz => ((hT.lk.members z).1 (List.mem_flatten.2 ⟨ch, hch, hz⟩)).1 + constructor + · rintro ⟨y, hy⟩ + obtain ⟨dd, rest, hpost, hd⟩ := hT.tree.seg ch hch A u B hsplit + have hyd := (hd y).1 hy + cases dd with + | nil => simp at hyd + | cons d0 d' => + have e : ch = A ++ u :: d0 :: (d' ++ rest) := by rw [hsplit, hpost]; simp + have hn := nx_split hT.lk hch e + have hd0 : Fl4Proofs.P3.Mdb.Desc par d0 u := (hd d0).2 (by simp) + have hi := hT.inU_desc hu hc hd0 + rw [hn] + exact ⟨hO.nz _ hi.1, hi.1, hi.2⟩ + · rintro ⟨hn0, hnO, hsr⟩ + cases B with + | nil => + have : ch.getLast? = some u := by rw [hsplit]; simp + exact absurd (hT.lk.last ch hch u this) hn0 + | cons n B' => + have hn : NxA st s u = n := nx_split hT.lk hch (by rw [hsplit]) + rw [hn] at hsr hnO + have hU := hT.ut + have hnu : n ≠ u := by + intro e + have h3 : (u :: n :: B').Nodup := by rw [hsplit] at hnd; exact List.Nodup.of_append_right hnd + exact (List.nodup_cons.1 h3).1 (by simp [e]) + rw [sraA_ut] at hsr + simp only [Bool.and_eq_true, decide_eq_true_eq] at hsr + obtain ⟨h1, h2, h3, h4⟩ := hsr + have a2 : p.val ≤ (ptrA (capA st s n)).val := h2 + have a3 : topA (capA st s n) ≤ (wa p (mskA b)).val := h3 + have a4 : (ptrA (capA st s n)).val ≤ topA (capA st s n) := h4 + have hnd' : ¬ DisjA (capA st s n) (capA st s u) := by + unfold DisjA + have eb : baseA (capA st s u) = p.val := by rw [hc]; unfold baseA; rw [ptrA_ut] + have et : topA (capA st s u) = (wa p (mskA b)).val := by rw [hc]; unfold topA; rw [ptrA_ut, sizeA_ut] + have ebn : baseA (capA st s n) = (ptrA (capA st s n)).val := rfl + rw [eb, et, ebn]; omega + rcases hU.drange u hu p b i d hc n hnO hnu h1 hnd' with hD | hD + · exact ⟨n, hD⟩ + · exfalso + have e : ch = (A ++ [u]) ++ n :: B' := by rw [hsplit]; simp + have hm := Fl4Proofs.P3.Mdb.desc_after (hT.tree.seg ch hch) e hD + have h3 : (u :: n :: B').Nodup := by rw [hsplit] at hnd; exact List.Nodup.of_append_right hnd + exact (List.nodup_cons.1 h3).1 (List.mem_cons_of_mem _ hm) + +/-- **`ensure_no_children` decides the spec's "has children" at an untyped slot** (module docstring; x86 +`noChildren_of_adj`): at a slot `sl` of the relation, in `OS`, revocable (A64 P1's `UtRev`), holding an untyped cap. -/ +theorem noChildren_of_adjA {th : TypedHeapA} {kb : U64} (hP : PlaceA th kb) {T Ω Sg OS : Set U64} + (hO : OSOK T Ω Sg OS) {G : MdbGhostA} {s : S} + {σ : Fl4SpecA64.State Unit} (hT : TreeA st OS G.chains G.par s) (hC : CdtRelA st th s σ G) + {sl : Fl4SpecA64.CslotPtr} (hs : AbsSlotA th sl) (hsO : cslotAddrA sl ∈ OS) {p b i : U64} {d : Bool} + (hc : capA st s (cslotAddrA sl) = .Untyped p b i d) (hr : obj.Slot.revocable st (cslotAddrA sl) s = ok true) : + NoChildrenRelA (st := st) s σ (cslotAddrA sl) sl := by + have h1 := (hC.hasChildA hP hs).trans (has_child_desc.trans (TreeA.hasChildUtA hO hT hsO hc)) + unfold NoChildrenRelA cdt.ensure_no_children + rw [next_eq] + simp only [bind_ok, obj.Slot.is_null] + by_cases hn : NxA st s (cslotAddrA sl) = 0#u64 + · have : ¬ ∃ x, σ.cdt x = some sl := fun h => (h1.1 h).1 hn + simp [hn, this] + · have hp : cdt.is_mdb_parent_of st s (cslotAddrA sl) (NxA st s (cslotAddrA sl)) = + ok (sraA (.Untyped p b i d) (capA st s (NxA st s (cslotAddrA sl)))) := by + unfold cdt.is_mdb_parent_of + simp only [hr, capA_eq, hc, sraA_eq, bind_ok, ↓reduceIte] + split <;> simp_all + simp only [hn, decide_false, Bool.false_eq_true, ↓reduceIte, hp, bind_ok] + by_cases hsr : sraA (.Untyped p b i d) (capA st s (NxA st s (cslotAddrA sl))) = true + · have hnO : NxA st s (cslotAddrA sl) ∈ OS := + TreeA.nxInA hT ((hT.lk.members _).2 ⟨hsO, by rw [hc]; exact fun e => by cases e⟩) hn + have : ∃ x, σ.cdt x = some sl := h1.2 ⟨hn, hnO, hsr⟩ + simp [hsr, this] + · have : ¬ ∃ x, σ.cdt x = some sl := fun h => hsr (h1.1 h).2.2 + simp [hsr, this] + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/NtfnBlockA.lean b/proofs/Fl4ProofsA64/P3/NtfnBlockA.lean new file mode 100644 index 000000000..74e0fd15f --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/NtfnBlockA.lean @@ -0,0 +1,717 @@ +/- +P3 (aarch64, A64-P3-IPC gen 3): **the remaining `receive_signal` cases and `complete_signal` refine +`Fl4SpecA64.receiveSignal` / `completeSignal`** (`Z = Unit`) over the scheduler interface `SchedHypsA` / +`SchedOpsA` (`P3/SchedOpsA`); x86 `P3/NtfnRecv`'s interface part and `P3/NtfnBlock`, ported (FL4's AArch64 +`receive_signal`, `complete_signal`, `ntfn_append`, `maybe_return_sched_context` are the x86 text): + +* `receive_signal_active`: on an active notification, the badge goes to the badge register (`X0`), the notification + becomes idle, then the SC tail (`SchedOpsA.recvTail`); +* `complete_signal_active`: the same for `complete_signal` (`SchedOpsA.completeTail`); +* `receive_signal_block`: a blocking receive on an idle or waiting notification: the state becomes + `BlockedOnNotification n` (`schedule_tcb` ↔ `set_thread_state_act`), the thread is inserted in priority order + (FL4 `append_by_prio` ↔ l4v `tcb_append`, `specIns`; equal on priority-sorted queues, `hsort`), the + notification becomes waiting, then `maybe_return_sched_context` ↔ `maybe_return_sc` (`SchedOpsA.returnSc`); +* the spec's ordered insertion: `specIns`, `tcbAppend_eq`, `tcbNtfnAppend_eq`, and (deferred from `P3/EpSpecA`) + **`Endp.tcbEpAppend_eq`**. + +Case / decode-site hypotheses as x86's: `hact` / `hna` (the case), `hnb` (the thread is not blocked on a +notification: it is the running thread), `hsort` (the queue is priority-sorted: A64 P1's `PrioSorted`, kept by +`append_by_prio`), `hAR` / `hNQ` / `hRB` / `hsane` / `hnidle` / `hRun` (the scheduler facts at the running +thread: l4v `valid_sched_action`, `not_queued`, `scheduler_act_sane`, not idle), `NPlaced`. No named premise. +(gen 12, Architect 01:39Z) The active receive / `complete_signal` take the thread on the current core (`hon`) and the +donation clause `DL s n` at entry, carried to `recvTail` / `completeTail` by `recv_prefix_local` (`coreKeep`, +`donLocalKeep`). + +Kernel cost: the blocking receive is split in declarations (`block_append`, `block_spec_eq`, `block_step`, +`specIns_map`, `block_tail`, `receive_signal_block`), as `P3/NtfnWakeA`'s wake-up: as one proof its kernel +check ran away in memory. +-/ +import Fl4ProofsA64.P3.SchedOpsA +import Fl4ProofsA64.P3.NtfnWakeA +import Fl4ProofsA64.P3.EpSpecA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Notif +open fl4_core_a64 Fl4ProofsA64.P1 + +noncomputable section + +/-! ## The spec's ordered insertion -/ + +section Spec +open Fl4SpecA64 Fl4Spec +variable {Z : Type} + +theorem getsThe_threadRead {γ : Type} {σ : State Z} {p : ObjRef} {x : Tcb} (f : Tcb → γ) + (hk : σ.kheap p = some (.tcb x)) : NonDet.getsThe (threadRead (Z := Z) f p) σ = ({(f x, σ)}, False) := by + have h1 : getTcb (Z := Z) p σ = some x := by simp [getTcb, readObject, hk] + have : threadRead (Z := Z) f p σ = some (f x) := by + change Option.bind (getTcb (Z := Z) p σ) (fun a => Lookup.oreturn (f a) σ) = _ + rw [h1]; rfl + simp [NonDet.getsThe, this, NonDet.assertOpt]; rfl + +theorem mapM_one {α β : Type} {σ : State Z} (f : α → NonDet (State Z) β) (g : α → β) : + ∀ ys : List α, (∀ y ∈ ys, f y σ = ({(g y, σ)}, False)) → NonDet.mapM f ys σ = ({(ys.map g, σ)}, False) + | [], _ => rfl + | y :: ys, h => by + unfold NonDet.mapM + rw [nd_bind_one' (h y List.mem_cons_self), + nd_bind_one' (mapM_one f g ys fun z hz => h z (List.mem_cons_of_mem _ hz))] + rfl + +theorem zip_takeWhile {α β : Type} (g : α → β) (p : α × β → Bool) : + ∀ ts : List α, ((ts.zip (ts.map g)).takeWhile p).map Prod.fst = ts.takeWhile (fun y => p (y, g y)) + | [] => rfl + | a :: rs => by + simp only [List.map_cons, List.zip_cons_cons, List.takeWhile_cons] + split <;> simp [zip_takeWhile g p rs] + +theorem zip_dropWhile {α β : Type} (g : α → β) (p : α × β → Bool) : + ∀ ts : List α, ((ts.zip (ts.map g)).dropWhile p).map Prod.fst = ts.dropWhile (fun y => p (y, g y)) + | [] => rfl + | a :: rs => by + simp only [List.map_cons, List.zip_cons_cons, List.dropWhile_cons] + split + · exact zip_dropWhile g p rs + · rw [List.map_cons, List.map_fst_zip (by simp)] + +/-- The spec's ordered insertion of `t` (priority `v`) into `qs`, by the +priorities `g`. -/ +def specIns (qs : List ObjRef) (g : ObjRef → Priority) (v : Priority) (t : ObjRef) : List ObjRef := + qs.takeWhile (fun y => @decide (g y ≥ v) (Classical.propDecidable _)) ++ + t :: qs.dropWhile (fun y => @decide (g y ≥ v) (Classical.propDecidable _)) + +/-- `tcb_append` on TCBs: the ordered insertion by the spec's priorities. -/ +theorem tcbAppend_eq {σ : State Z} {t : ObjRef} {xt : Tcb} (hkt : σ.kheap t = some (.tcb xt)) + (qs : List ObjRef) (g : ObjRef → Priority) + (hq : ∀ y ∈ qs, ∃ x, σ.kheap y = some (.tcb x) ∧ x.tcbPriority = g y) : + tcbAppend (Z := Z) t qs σ = ({(specIns qs g xt.tcbPriority t, σ)}, False) := by + unfold tcbAppend orderedInsert + rw [nd_bind_one (getsThe_threadRead _ hkt), + nd_bind_one (mapM_one (fun t' => NonDet.getsThe (threadRead (Z := Z) Tcb.tcbPriority t')) g qs + (fun y hy => by obtain ⟨x, hx, e⟩ := hq y hy; rw [getsThe_threadRead _ hx, e])), + nd_pure_bind', nd_pure_bind'] + show ({(_, σ)}, False) = _ + congr + simp only [List.map_append, List.map_cons, zip_takeWhile, zip_dropWhile, List.append_assoc, + List.singleton_append, specIns] + +theorem tcbNtfnAppend_eq {σ : State Z} {t p : ObjRef} {x : Notification} {xt : Tcb} + (hk : σ.kheap p = some (.notification x)) (hkt : σ.kheap t = some (.tcb xt)) (g : ObjRef → Priority) + (hq : ∀ y ∈ ntfnQueueOf x, ∃ x, σ.kheap y = some (.tcb x) ∧ x.tcbPriority = g y) : + tcbNtfnAppend (Z := Z) t p σ = ({((), updK σ p (.notification + { x with ntfnObj := .waitingNtfn (specIns (ntfnQueueOf x) g xt.tcbPriority t) }))}, False) := by + unfold tcbNtfnAppend + rw [nd_bind_one (getNotification_eq hk), nd_pure_bind', nd_bind_one (tcbAppend_eq hkt _ g hq)] + exact setNotification_eq _ hk + +end Spec + +/-! ## Lists -/ + +theorem takeWhile_congr' {α : Type} {p q : α → Bool} : + ∀ {l : List α}, (∀ a ∈ l, p a = q a) → l.takeWhile p = l.takeWhile q + | [], _ => rfl + | a :: l, h => by + simp only [List.takeWhile_cons, h a List.mem_cons_self, + takeWhile_congr' (fun b hb => h b (List.mem_cons_of_mem _ hb))] + +theorem dropWhile_congr' {α : Type} {p q : α → Bool} : + ∀ {l : List α}, (∀ a ∈ l, p a = q a) → l.dropWhile p = l.dropWhile q + | [], _ => rfl + | a :: l, h => by + simp only [List.dropWhile_cons, h a List.mem_cons_self, + dropWhile_congr' (fun b hb => h b (List.mem_cons_of_mem _ hb))] + +theorem mem_insPrio_sorted {pr : U64 → U64} {p t : U64} {xs : List U64} (h : PrioSorted pr xs) {y : U64} + (hy : y ∈ insPrio pr p t xs) : y = t ∨ y ∈ xs := by + rw [insPrio_eq_sorted pr p t xs h] at hy + rcases List.mem_append.1 hy with hy | hy + · exact Or.inr ((List.takeWhile_sublist _).subset hy) + · rcases List.mem_cons.1 hy with rfl | hy + · exact Or.inl rfl + · exact Or.inr ((List.dropWhile_suffix _).subset hy) + +theorem insPrio_ne_nil {pr : U64 → U64} {p t : U64} {xs : List U64} (h : PrioSorted pr xs) : + insPrio pr p t xs ≠ [] := by + rw [insPrio_eq_sorted pr p t xs h]; simp + +theorem prioSorted_congr {pr pr' : U64 → U64} {xs : List U64} (h : PrioSorted pr xs) + (he : ∀ y ∈ xs, pr' y = pr y) : PrioSorted pr' xs := + h.imp_of_mem fun ha hb hab => by rw [he _ ha, he _ hb]; exact hab + + +/-- The priority of the TCB at `y` in the spec's heap (0 if none). -/ +def prioOf (σ : Fl4SpecA64.State Unit) (y : Fl4SpecA64.ObjRef) : Fl4SpecA64.Priority := + match σ.kheap y with + | some (.tcb z) => z.tcbPriority + | _ => 0 + +/-- The queue of a related notification, whatever its state. -/ +theorem NtfnObjRel.queue {m : U64 → U64} {T : Set U64} {n : U64} {o : Fl4SpecA64.Ntfn} (h : NtfnObjRel m T n o) : + ∃ xs, QueueRel m T .Ep (qOf m n) xs ∧ o.ntfnQueue = xs.map specPtr ∧ ∀ y ∈ xs, BlockedOn m n y := by + cases o with + | idleNtfn => exact ⟨[], h.2, rfl, by simp⟩ + | activeNtfn b => exact ⟨[], h.2.1, rfl, by simp⟩ + | waitingNtfn q => + obtain ⟨-, xs, -, rfl, h2, h3⟩ := h + exact ⟨xs, h2, rfl, h3⟩ + +/-! ## `receive_signal` -/ + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] {N T : Set U64} + {SR : S → Fl4SpecA64.State Unit → Prop} {DL : S → U64 → Prop} + +/-- Receiving from an active notification: the badge register and the +notification after the two writes, related to the spec's. -/ +theorem recv_active_prefix (hP : NPlaced T N) {s : S} {σ : Fl4SpecA64.State Unit} + (hR : NtfnStateRel N T (L.mem s) σ) {t : U64} (ht : t ∈ T) {n : U64} (hn : n ∈ N) + {x : Fl4SpecA64.Notification} (hk : σ.kheap (specPtr n) = some (.notification x)) {b0 : Fl4SpecA64.Badge} + (hact : x.ntfnObj = .activeNtfn b0) {xt : Fl4SpecA64.Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) : + let s1 := L.wr s (badgeRegA t) (L.mem s (nBadgeA n)) + let s2 := L.wr s1 (epTSA n) ((L.mem s1 (epTSA n) &&& ~~~3#u64) ||| (obj.NTFN_IDLE &&& 3#u64)) + let σ1 := updK σ (specPtr t) (.tcb (setReg xt Fl4SpecA64.badge_register b0)) + let σ2 := updK σ1 (specPtr n) (.notification (Fl4SpecA64.ntfnObjUpdate (fun _ => .idleNtfn) x)) + Fl4SpecA64.asUser (Z := Unit) (specPtr t) (Fl4SpecA64.setRegister Fl4SpecA64.badge_register b0) σ = ({((), σ1)}, False) ∧ + Fl4SpecA64.setNotification (Z := Unit) (specPtr n) (Fl4SpecA64.ntfnObjUpdate (fun _ => .idleNtfn) x) σ1 = + ({((), σ2)}, False) ∧ + NtfnStateRel N T (L.mem s2) σ2 ∧ WritesIn st s s2 (NWrite N T) ∧ NtfnStep σ σ2 := by + intro s1 s2 σ1 σ2 + have hx := hR.at_ntfn hn hk + have hb0 : (L.mem s (nBadgeA n)).bv = b0 := by + have := hx.obj; rw [hact] at this; exact this.2.2 + obtain ⟨hR1, hW1, hS1⟩ := set_badge_reg_rel hP hR ht hkt (L.mem s (nBadgeA n)) + rw [hb0] at hR1 hS1 + have hnt : (specPtr n) ≠ (specPtr t) := by intro e; rw [e, hkt] at hk; cases hk + have hk1 : σ1.kheap (specPtr n) = some (.notification x) := by simp [σ1, hnt, hk] + obtain ⟨hR2, hW2, hS2⟩ := set_idle_rel hP hR1 hn hk1 hact + exact ⟨asUser_setRegister_eq _ _ hkt, setNotification_eq _ hk1, hR2, + (hW1.trans hW2).mono (fun _ h => by rcases h with h | h <;> exact h), hS1.trans hS2⟩ + +/-- (gen 12, Architect 01:39Z) The local-core preconditions of `recvTail` / `completeTail` after the receive prefix: +the thread's core by `coreKeep`, the donation clause by `donLocalKeep` (the notification's SC pointer kept). -/ +theorem recv_prefix_local (hO : SchedOpsA st ar N T SR DL) {s s2 : S} {σ : Fl4SpecA64.State Unit} + {t n : U64} {x : Fl4SpecA64.Notification} {xt : Fl4SpecA64.Tcb} (hR : NtfnStateRel N T (L.mem s) σ) + {b0 : Fl4SpecA64.Badge} + (hR2 : NtfnStateRel N T (L.mem s2) (updK (updK σ (specPtr t) (.tcb (setReg xt Fl4SpecA64.badge_register b0))) + (specPtr n) (.notification (Fl4SpecA64.ntfnObjUpdate (fun _ => .idleNtfn) x)))) + (hSR : SR s σ) + (hSR2 : SR s2 (updK (updK σ (specPtr t) (.tcb (setReg xt Fl4SpecA64.badge_register b0))) + (specPtr n) (.notification (Fl4SpecA64.ntfnObjUpdate (fun _ => .idleNtfn) x)))) + (ht : t ∈ T) (hn : n ∈ N) (hk : σ.kheap (specPtr n) = some (.notification x)) + (hkt : σ.kheap (specPtr t) = some (.tcb xt)) (hnt : specPtr n ≠ specPtr t) + (hon : L.mem s (affA t) = L.cpu s) (hdl : DL s n) : + L.mem s2 (affA t) = L.cpu s2 ∧ DL s2 n := by + have hK2 : NScK σ (updK (updK σ (specPtr t) (.tcb (setReg xt Fl4SpecA64.badge_register b0))) + (specPtr n) (.notification (Fl4SpecA64.ntfnObjUpdate (fun _ => .idleNtfn) x))) := + (NScK.upd (fun z h => by rw [hkt] at h; cases h) _).trans + (NScK.updN (x := x) (by rw [updK_kheap, Function.update_of_ne hnt, hk]) _ rfl) + exact ⟨(hO.coreKeep _ _ _ _ hSR hSR2).onCore ht hon, + hO.donLocalKeep _ _ _ _ n hn hR hR2 hSR hSR2 (nsc_word hR hR2 hn hK2) hdl⟩ + +/-- **Receive on an active notification**: the badge goes to the badge +register, the notification becomes idle, then the scheduler tail. -/ +theorem receive_signal_active (hH : SchedHypsA st N T SR) (hO : SchedOpsA st ar N T SR DL) (hP : NPlaced T N) + (s : S) (a : A) (σ : Fl4SpecA64.State Unit) (hR : NtfnStateRel N T (L.mem s) σ) (hSR : SR s σ) + {t : U64} (ht : t ∈ T) {n : U64} (hn : n ∈ N) {x : Fl4SpecA64.Notification} + (hk : σ.kheap (specPtr n) = some (.notification x)) {b0 : Fl4SpecA64.Badge} (hact : x.ntfnObj = .activeNtfn b0) + (blk : Bool) (cb : Fl4SpecA64.Badge) (cr : Fl4SpecA64.CapRights) (hNQ : NotQueued σ (specPtr t)) + (hRun : ∀ x, σ.kheap (specPtr t) = some (.tcb x) → Fl4SpecA64.runnable x.tcbState = true) + (hon : L.mem s (affA t) = L.cpu s) (hdl : DL s n) : + notification.receive_signal st ar s a t n blk ⦃ r => + ¬ (Fl4SpecA64.receiveSignal (specPtr t) (.notificationCap (specPtr n) cb cr) blk σ).2 ∧ + ∃ σ', ((), σ') ∈ (Fl4SpecA64.receiveSignal (specPtr t) (.notificationCap (specPtr n) cb cr) blk σ).1 ∧ + NtfnStateRel N T (L.mem r.1) σ' ∧ SR r.1 σ' ⦄ := by + have hx := hR.at_ntfn hn hk + obtain ⟨xt, hkt, -⟩ := hR.tcb t ht + obtain ⟨e1, e2, hR2, hW, hS⟩ := recv_active_prefix (st := st) hP hR ht hn hk hact hkt + have hspec : Fl4SpecA64.receiveSignal (Z := Unit) (specPtr t) (.notificationCap (specPtr n) cb cr) blk σ = + specRecvTail (specPtr t) (specPtr n) (updK (updK σ (specPtr t) (.tcb (setReg xt Fl4SpecA64.badge_register b0))) (specPtr n) + (.notification (Fl4SpecA64.ntfnObjUpdate (fun _ => .idleNtfn) x))) := by + rw [receiveSignal_cap hk] + simp only [hact] + rw [nd_bind_one e1, nd_bind_one e2] + rw [hspec] + have hnt : (specPtr n) ≠ (specPtr t) := by intro e; rw [e, hkt] at hk; cases hk + have hSR2 := hH.stable _ _ _ _ hSR hW hS (Endp.NoBlockE.recvPrefix hkt hnt _ _ _) + have hloc := recv_prefix_local hO hR hR2 hSR hSR2 ht hn hk hkt hnt hon hdl + have hNQ2 := hNQ + have hRun2 : ∀ z, (updK (updK σ (specPtr t) (.tcb (setReg xt Fl4SpecA64.badge_register b0))) (specPtr n) + (.notification (Fl4SpecA64.ntfnObjUpdate (fun _ => .idleNtfn) x))).kheap (specPtr t) = some (.tcb z) → + Fl4SpecA64.runnable z.tcbState = true := by + intro y hy + simp only [updK_kheap, Function.update_apply, Ne.symm hnt, ↓reduceIte] at hy + cases hy; exact hRun xt hkt + have hsa : stOf (L.mem s) n = obj.NTFN_ACTIVE := stOf_active hx.obj hact + unfold notification.receive_signal + step + have hi : ¬ L.epState s n = obj.NTFN_IDLE := by + rw [epState_eq, hsa, ntfn_consts.1, ntfn_consts.2.2]; decide + have hw' : ¬ L.epState s n = obj.NTFN_WAITING := by + rw [epState_eq, hsa, ntfn_consts.2.1, ntfn_consts.2.2]; decide + rw [st_post] + simp only [hi, hw', ↓reduceIte] + step + step + step + subst_vars + refine WP.spec_mono (hO.recvTail _ a _ t n hR2 hSR2 ht hn hNQ2 hRun2 hloc.1 hloc.2) ?_ + rintro r ⟨hnf, σ', hm, hf, hv, hS'⟩ + exact ⟨hnf, σ', hm, hR2.frame hP hf hv, hS'⟩ + +variable (st) in +/-- (gen 14) The state after the active receive's two writes (the badge register, the notification idle). -/ +def rsPreA (s : S) (t n : U64) : S := + L.wr (L.wr s (badgeRegA t) (L.mem s (nBadgeA n))) (epTSA n) + ((L.mem (L.wr s (badgeRegA t) (L.mem s (nBadgeA n))) (epTSA n) &&& ~~~3#u64) ||| (obj.NTFN_IDLE &&& 3#u64)) + +/-- (gen 14) The spec's state after `complete_signal`'s badge register and notification writes. -/ +def rsPreσA (σ : Fl4SpecA64.State Unit) (t n : Fl4SpecA64.ObjRef) (xt : Fl4SpecA64.Tcb) + (x : Fl4SpecA64.Notification) (b0 : Fl4SpecA64.Badge) : Fl4SpecA64.State Unit := + updK (updK σ t (.tcb (setReg xt Fl4SpecA64.badge_register b0))) n + (.notification (Fl4SpecA64.ntfnObjUpdate (fun _ => .idleNtfn) x)) + +/-- An extra caller relation carried through the scheduler tail of `complete_signal`. -/ +abbrev CompleteTailFrameA (st : machine.Store S) (ar : machine.Arch A) [L : LawfulStore st] + (N T : Set U64) (SR : S → Fl4SpecA64.State Unit → Prop) (DL : S → U64 → Prop) + (Extra : S → Fl4SpecA64.State Unit → Prop) : Prop := + ∀ s a σ t n, NtfnStateRel N T (L.mem s) σ → SR s σ → t ∈ T → n ∈ N → + NotQueued σ (specPtr t) → + (∀ x, σ.kheap (specPtr t) = some (.tcb x) → Fl4SpecA64.runnable x.tcbState = true) → + L.mem s (affA t) = L.cpu s → DL s n → Extra s σ → + TailRef st N T (fun s σ => SR s σ ∧ Extra s σ) Prod.fst s σ + (rustCompleteTail st ar s a t n) (specCompleteTail (specPtr t) (specPtr n)) + +/-- The active notification completion with a caller relation established after the two prefix writes. -/ +theorem complete_signal_of_frame {Extra : S → Fl4SpecA64.State Unit → Prop} + (hO : SchedOpsA st ar N T SR DL) (tail : CompleteTailFrameA st ar N T SR DL Extra) (hP : NPlaced T N) + (s : S) (a : A) (σ : Fl4SpecA64.State Unit) (hR : NtfnStateRel N T (L.mem s) σ) (hSR : SR s σ) + {t : U64} (ht : t ∈ T) {n : U64} (hn : n ∈ N) {x : Fl4SpecA64.Notification} + (hk : σ.kheap (specPtr n) = some (.notification x)) {b0 : Fl4SpecA64.Badge} (hact : x.ntfnObj = .activeNtfn b0) + {xt : Fl4SpecA64.Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hNQ : NotQueued σ (specPtr t)) (hRun : ∀ x, σ.kheap (specPtr t) = some (.tcb x) → Fl4SpecA64.runnable x.tcbState = true) + (hon : L.mem s (affA t) = L.cpu s) (hdl : DL s n) + (hSR2 : SR (rsPreA st s t n) (rsPreσA σ (specPtr t) (specPtr n) xt x b0)) + (hExtra : Extra (rsPreA st s t n) (rsPreσA σ (specPtr t) (specPtr n) xt x b0)) : + notification.complete_signal st ar s a n t ⦃ r => + ¬ (Fl4SpecA64.completeSignal (specPtr n) (specPtr t) σ).2 ∧ ∃ σ', ((), σ') ∈ (Fl4SpecA64.completeSignal (specPtr n) (specPtr t) σ).1 ∧ + NtfnStateRel N T (L.mem r.1) σ' ∧ SR r.1 σ' ∧ Extra r.1 σ' ⦄ := by + have hx := hR.at_ntfn hn hk + obtain ⟨e1, e2, hR2, -, -⟩ := recv_active_prefix (st := st) hP hR ht hn hk hact hkt + have hspec : Fl4SpecA64.completeSignal (Z := Unit) (specPtr n) (specPtr t) σ = + specCompleteTail (specPtr t) (specPtr n) (updK (updK σ (specPtr t) (.tcb (setReg xt Fl4SpecA64.badge_register b0))) (specPtr n) + (.notification (Fl4SpecA64.ntfnObjUpdate (fun _ => .idleNtfn) x))) := by + unfold Fl4SpecA64.completeSignal + rw [nd_bind_one (getNotification_eq hk)] + simp only [hact] + rw [nd_bind_one e1, nd_bind_one e2] + rfl + rw [hspec] + have hnt : (specPtr n) ≠ (specPtr t) := by intro e; rw [e, hkt] at hk; cases hk + have hloc := recv_prefix_local hO hR hR2 hSR hSR2 ht hn hk hkt hnt hon hdl + have hNQ2 := hNQ + have hRun2 : ∀ z, (updK (updK σ (specPtr t) (.tcb (setReg xt Fl4SpecA64.badge_register b0))) (specPtr n) + (.notification (Fl4SpecA64.ntfnObjUpdate (fun _ => .idleNtfn) x))).kheap (specPtr t) = some (.tcb z) → + Fl4SpecA64.runnable z.tcbState = true := by + intro y hy + simp only [updK_kheap, Function.update_apply, Ne.symm hnt, ↓reduceIte] at hy + cases hy; exact hRun xt hkt + have hsa : stOf (L.mem s) n = obj.NTFN_ACTIVE := stOf_active hx.obj hact + have ht0 : t ≠ 0#u64 := fun e => (hP.tcb t ht).ne0 (by rw [e]; rfl) + unfold notification.complete_signal + step + simp only [b_post, ht0, decide_false, Bool.false_eq_true, ↓reduceIte] + step + rw [i_post, epState_eq, hsa] + simp only [bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + step + step + step + subst_vars + refine WP.spec_mono (tail _ a _ t n hR2 hSR2 ht hn hNQ2 hRun2 hloc.1 hloc.2 hExtra) ?_ + rintro r ⟨hnf, σ', hm, hf, hv, hS', hE'⟩ + exact ⟨hnf, σ', hm, hR2.frame hP hf hv, hS', hE'⟩ + + +/-- The active notification completion with the scheduler relation established after the prefix. -/ +theorem complete_signal_of (hO : SchedOpsA st ar N T SR DL) (hP : NPlaced T N) + (s : S) (a : A) (σ : Fl4SpecA64.State Unit) (hR : NtfnStateRel N T (L.mem s) σ) (hSR : SR s σ) + {t : U64} (ht : t ∈ T) {n : U64} (hn : n ∈ N) {x : Fl4SpecA64.Notification} + (hk : σ.kheap (specPtr n) = some (.notification x)) {b0 : Fl4SpecA64.Badge} (hact : x.ntfnObj = .activeNtfn b0) + {xt : Fl4SpecA64.Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hNQ : NotQueued σ (specPtr t)) (hRun : ∀ x, σ.kheap (specPtr t) = some (.tcb x) → Fl4SpecA64.runnable x.tcbState = true) + (hon : L.mem s (affA t) = L.cpu s) (hdl : DL s n) + (hSR2 : SR (rsPreA st s t n) (rsPreσA σ (specPtr t) (specPtr n) xt x b0)) : + notification.complete_signal st ar s a n t ⦃ r => + ¬ (Fl4SpecA64.completeSignal (specPtr n) (specPtr t) σ).2 ∧ ∃ σ', ((), σ') ∈ (Fl4SpecA64.completeSignal (specPtr n) (specPtr t) σ).1 ∧ + NtfnStateRel N T (L.mem r.1) σ' ∧ SR r.1 σ' ⦄ := by + have tail : CompleteTailFrameA st ar N T SR DL (fun _ _ => True) := by + intro s a σ t n hRN hSR ht hn hNQ hRun hon hdl _ + refine WP.spec_mono (hO.completeTail s a σ t n hRN hSR ht hn hNQ hRun hon hdl) ?_ + rintro r ⟨hf, σ', hm, hW, hK, hS'⟩ + exact ⟨hf, σ', hm, hW, hK, hS', trivial⟩ + refine WP.spec_mono + (complete_signal_of_frame hO tail hP s a σ hR hSR ht hn hk hact hkt hNQ hRun hon hdl hSR2 trivial) ?_ + rintro r ⟨hf, σ', hm, hRN', hS', _⟩ + exact ⟨hf, σ', hm, hRN', hS'⟩ + +/-- **`complete_signal` on an active notification** (bound notification at +the end of a receive): as the active receive, with `complete_signal`'s +tail. -/ +theorem complete_signal_active (hH : SchedHypsA st N T SR) (hO : SchedOpsA st ar N T SR DL) (hP : NPlaced T N) + (s : S) (a : A) (σ : Fl4SpecA64.State Unit) (hR : NtfnStateRel N T (L.mem s) σ) (hSR : SR s σ) + {t : U64} (ht : t ∈ T) {n : U64} (hn : n ∈ N) {x : Fl4SpecA64.Notification} + (hk : σ.kheap (specPtr n) = some (.notification x)) {b0 : Fl4SpecA64.Badge} (hact : x.ntfnObj = .activeNtfn b0) + (hNQ : NotQueued σ (specPtr t)) (hRun : ∀ x, σ.kheap (specPtr t) = some (.tcb x) → Fl4SpecA64.runnable x.tcbState = true) + (hon : L.mem s (affA t) = L.cpu s) (hdl : DL s n) : + notification.complete_signal st ar s a n t ⦃ r => + ¬ (Fl4SpecA64.completeSignal (specPtr n) (specPtr t) σ).2 ∧ ∃ σ', ((), σ') ∈ (Fl4SpecA64.completeSignal (specPtr n) (specPtr t) σ).1 ∧ + NtfnStateRel N T (L.mem r.1) σ' ∧ SR r.1 σ' ⦄ := by + obtain ⟨xt, hkt, -⟩ := hR.tcb t ht + obtain ⟨-, -, -, hW, hS⟩ := recv_active_prefix (st := st) hP hR ht hn hk hact hkt + have hnt : (specPtr n) ≠ (specPtr t) := by intro e; rw [e, hkt] at hk; cases hk + exact complete_signal_of hO hP s a σ hR hSR ht hn hk hact hkt hNQ hRun hon hdl + (hH.stable _ _ _ _ hSR hW hS (Endp.NoBlockE.recvPrefix hkt hnt _ _ _)) + +end + +/-! ## The blocking receive -/ + +/-- The spec's blocking receive on a notification that is not active, up to `maybe_return_sc` (stated for any object +names). -/ +theorem receiveSignalBlocked_eq {σ : Fl4SpecA64.State Unit} {t n : Fl4SpecA64.ObjRef} {x : Fl4SpecA64.Notification} + {xt : Fl4SpecA64.Tcb} (hk : σ.kheap n = some (.notification x)) (hkt : σ.kheap t = some (.tcb xt)) (hnt : n ≠ t) + (g : Fl4SpecA64.ObjRef → Fl4SpecA64.Priority) + (hq : ∀ y ∈ Fl4SpecA64.ntfnQueueOf x, ∃ z, (stsAct (updK σ t (.tcb { xt with tcbState := .blockedOnNotification n })) + t).kheap y = some (.tcb z) ∧ z.tcbPriority = g y) : + Fl4SpecA64.receiveSignalBlocked (Z := Unit) t n true σ = Fl4SpecA64.maybeReturnSc n t + (updK (stsAct (updK σ t (.tcb { xt with tcbState := .blockedOnNotification n })) t) n + (.notification { x with ntfnObj := .waitingNtfn (specIns (Fl4SpecA64.ntfnQueueOf x) g xt.tcbPriority t) })) := by + have hk2 : (stsAct (updK σ t (.tcb { xt with tcbState := .blockedOnNotification n })) t).kheap n = + some (.notification x) := by + rw [stsAct_kheap, updK_kheap, Function.update_of_ne hnt, hk] + have hkt2 : (stsAct (updK σ t (.tcb { xt with tcbState := .blockedOnNotification n })) t).kheap t = + some (.tcb { xt with tcbState := .blockedOnNotification n }) := by + rw [stsAct_kheap, updK_kheap, Function.update_self] + unfold Fl4SpecA64.receiveSignalBlocked + simp only [↓reduceIte] + rw [nd_bind_one (setThreadState_eq _ hkt), nd_bind_one (tcbNtfnAppend_eq hk2 hkt2 g hq)] + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] {N T : Set U64} + {SR : S → Fl4SpecA64.State Unit → Prop} {DL : S → U64 → Prop} + +theorem tsCode_six {ts : Fl4SpecA64.ThreadState} (h : tsCode ts = 6) : ∃ m, ts = .blockedOnNotification m := + tsCode_six' h + +/-- A TCB not blocked on a notification is in no notification queue. -/ +theorem not_member {m : U64 → U64} {σ : Fl4SpecA64.State Unit} (hR : NtfnStateRel N T m σ) {t : U64} (ht : t ∈ T) + {xt : Fl4SpecA64.Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) (hnb : ∀ m, xt.tcbState ≠ .blockedOnNotification m) : + ∀ n ∈ N, ∀ z, σ.kheap (specPtr n) = some (.notification z) → (specPtr t) ∉ z.ntfnObj.ntfnQueue := by + intro n hn z hz hm + obtain ⟨-, h6, -⟩ := (hR.at_ntfn hn hz).obj.blocked hm + have := (hR.at_tcb ht hkt).st + rw [h6] at this + obtain ⟨m', e⟩ := tsCode_six this.symm + exact hnb m' e + +theorem six_val : sched.TS_BLOCKED_ON_NTFN.val = 6 := by unfold sched.TS_BLOCKED_ON_NTFN; rfl + +theorem prio_ge_iff (p q : U64) (v g : Fl4SpecA64.Priority) (hp : p.val = v.toNat) (hq : q.val = g.toNat) : + (!decide (p > q)) = decide (g ≥ v) := by + have e1 : (p > q) ↔ g.toNat < v.toNat := by rw [gt_iff_val, hp, hq] + have e2 : (g ≥ v) ↔ v.toNat ≤ g.toNat := BitVec.le_def + by_cases h : v.toNat ≤ g.toNat + · have h1 : ¬ p > q := fun h' => by have := e1.1 h'; omega + have h2 : g ≥ v := e2.2 h + simp [h1, h2] + · have h1 : p > q := e1.2 (by omega) + have h2 : ¬ g ≥ v := fun h' => h (e2.1 h') + simp [h1, h2] + +/-- The spec's ordered insertion of `t` into a priority-sorted queue named by `specPtr` is FL4's `insPrio` +(priorities related by `m`). -/ +theorem specIns_map {σ2 : Fl4SpecA64.State Unit} {m : U64 → U64} {xs : List U64} {t : U64} + {pt : Fl4SpecA64.Priority} (hsorted : PrioSorted (fun y => m (prioA y)) xs) (hpt : (m (prioA t)).val = pt.toNat) + (hpr : ∀ y ∈ xs, ∃ z, σ2.kheap (specPtr y) = some (.tcb z) ∧ (m (prioA y)).val = z.tcbPriority.toNat) : + specIns (xs.map specPtr) (prioOf σ2) pt (specPtr t) = + (insPrio (fun y => m (prioA y)) (m (prioA t)) t xs).map specPtr := by + rw [specIns, List.takeWhile_map, List.dropWhile_map] + simp only [insPrio_eq_sorted _ _ t xs hsorted, List.map_append, List.map_cons] + have hc : ∀ y ∈ xs, ((fun y => @decide (prioOf σ2 y ≥ pt) (Classical.propDecidable _)) ∘ specPtr) y = + (fun y => !decide (m (prioA t) > m (prioA y))) y := by + intro y hy + obtain ⟨z, hz, hzr⟩ := hpr y hy + have hpz : prioOf σ2 (specPtr y) = z.tcbPriority := by simp [prioOf, hz] + simp only [Function.comp, hpz] + convert (prio_ge_iff _ _ _ _ hpt hzr).symm using 2 + rw [takeWhile_congr' hc, dropWhile_congr' hc] + +/-- The blocking receive's `ntfn_append`: the notification related to the waiting one with FL4's insertion. -/ +theorem block_append (hP : NPlaced T N) (s s2 s3 : S) (σ2 : Fl4SpecA64.State Unit) {t n : U64} (ht : t ∈ T) + (hn : n ∈ N) {x : Fl4SpecA64.Notification} {xt : Fl4SpecA64.Tcb} + (hk2 : σ2.kheap (specPtr n) = some (.notification x)) + (hkt2 : σ2.kheap (specPtr t) = some (.tcb { xt with tcbState := .blockedOnNotification (specPtr n) })) + {xs : List U64} (hQ : QueueRel (L.mem s) T .Ep (qOf (L.mem s) n) xs) (hxs : x.ntfnObj.ntfnQueue = xs.map specPtr) + (htxs : t ∉ xs) (hsorted : PrioSorted (fun y => L.mem s (prioA y)) xs) + (hwp : ∀ y ∈ T, y ≠ t → L.mem s2 (prioA y) = L.mem s (prioA y)) + (hf3 : FootEq N T (L.mem s2) (L.mem s3)) (hR3 : NtfnStateRel N T (L.mem s3) σ2) : + notification.ntfn_append st s3 n t ⦃ s4 => NtfnStateRel N T (L.mem s4) (updK σ2 (specPtr n) (.notification + { x with ntfnObj := .waitingNtfn ((insPrio (fun y => L.mem s3 (prioA y)) (L.mem s3 (prioA t)) t xs).map specPtr) })) + ∧ WritesIn st s3 s4 (NWrite N T) ⦄ := by + obtain ⟨xs3, hQ3, hxs3, hB3⟩ := (hR3.at_ntfn hn hk2).obj.queue + have e3 : xs3 = xs := by + rw [hxs] at hxs3 + exact (List.map_injective_iff.2 (fun a b h => specPtr_inj h) hxs3).symm + subst e3 + have hxt3 := hR3.at_tcb ht hkt2 + -- priorities agree with those of `s` + have hpr : ∀ y ∈ xs3, L.mem s3 (prioA y) = L.mem s (prioA y) := by + intro y hy + have hyT := hQ.mem y hy + have hyt : y ≠ t := fun e => htxs (e ▸ hy) + exact (hf3.tfields hP hyT).prio.trans (hwp y hyT hyt) + have hsorted3 : PrioSorted (fun y => L.mem s3 (prioA y)) xs3 := prioSorted_congr hsorted hpr + refine WP.spec_mono (ntfn_append_full hP s3 hn t xs3 hQ3 ht htxs) ?_ + rintro s4 ⟨hA1, hA2, hA3⟩ + set ys := insPrio (fun y => L.mem s3 (prioA y)) (L.mem s3 (prioA t)) t xs3 + have hblk3 : L.mem s3 (tBlockA t) = n := specPtr_inj (hxt3.blk (specPtr n) rfl) + have hR4 : NtfnStateRel N T (L.mem s4) + (updK σ2 (specPtr n) (.notification { x with ntfnObj := .waitingNtfn (ys.map specPtr) })) := by + have hx2 := hR3.at_ntfn hn hk2 + refine hR3.upd_ntfn hn _ hA3.ntfn hA3.tcb (fun y hy hb => hA3.links y hy ?_) ⟨?_, ?_, ?_, ?_⟩ + · intro hm + rcases List.mem_cons.1 hm with rfl | hm + · exact hb hblk3 + · exact hb (hB3 y hm).2 + · refine ⟨hA2, ys, insPrio_ne_nil hsorted3, rfl, hA1, fun y hy => ?_⟩ + have hyT : y ∈ T := by + rcases mem_insPrio_sorted hsorted3 hy with rfl | hy' + · exact ht + · exact hQ.mem y hy' + have e := hA3.tcb y hyT + rcases mem_insPrio_sorted hsorted3 hy with rfl | hy' + · exact ⟨by rw [e.st, hxt3.st]; rfl, by rw [e.blk]; exact hblk3⟩ + · exact ⟨by rw [e.st]; exact (hB3 y hy').1, by rw [e.blk]; exact (hB3 y hy').2⟩ + · show x.ntfnBoundTcb = _; rw [hA3.bound]; exact hx2.bound + · rw [hA3.bound]; exact hx2.bound_mem + · show x.ntfnSc = _; rw [hA3.sc]; exact hx2.sc + exact ⟨hR4, hA3.foot⟩ + +/-- The spec side of the blocking receive, down to `maybe_return_sc`. -/ +theorem block_spec_eq {σ σ2 : Fl4SpecA64.State Unit} {t n : U64} {x : Fl4SpecA64.Notification} {xt : Fl4SpecA64.Tcb} + (hk : σ.kheap (specPtr n) = some (.notification x)) (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hna : ∀ b, x.ntfnObj ≠ .activeNtfn b) + (hσ2 : stsAct (updK σ (specPtr t) (.tcb { xt with tcbState := .blockedOnNotification (specPtr n) })) (specPtr t) = + σ2) + (hq : ∀ y ∈ Fl4SpecA64.ntfnQueueOf x, ∃ z, σ2.kheap y = some (.tcb z) ∧ z.tcbPriority = prioOf σ2 y) + {l : List Fl4SpecA64.ObjRef} (hl : specIns (Fl4SpecA64.ntfnQueueOf x) (prioOf σ2) xt.tcbPriority (specPtr t) = l) + (cb : Fl4SpecA64.Badge) (cr : Fl4SpecA64.CapRights) : + Fl4SpecA64.receiveSignal (Z := Unit) (specPtr t) (.notificationCap (specPtr n) cb cr) true σ = + Fl4SpecA64.maybeReturnSc (specPtr n) (specPtr t) (updK σ2 (specPtr n) (.notification + { x with ntfnObj := .waitingNtfn l })) := by + have hnt : (specPtr n) ≠ (specPtr t) := by intro e; rw [e, hkt] at hk; cases hk + rw [receiveSignal_notActive hk hna, + receiveSignalBlocked_eq hk hkt hnt (prioOf σ2) (by rw [hσ2]; exact hq), hσ2, hl] + +/-- What blocking `t` on `n` changes (`BlockStep`), at the spec state after the notification update. -/ +theorem block_step {σ σ2 : Fl4SpecA64.State Unit} {t n : U64} {x : Fl4SpecA64.Notification} {xt : Fl4SpecA64.Tcb} + (hk : σ.kheap (specPtr n) = some (.notification x)) (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hkt2 : σ2.kheap (specPtr t) = some (.tcb { xt with tcbState := .blockedOnNotification (specPtr n) })) + (hkoth : ∀ p, p ≠ specPtr t → σ2.kheap p = σ.kheap p) + (hsa2 : σ2.schedulerAction = σ.schedulerAction ∨ σ2.schedulerAction = .chooseNewThread) + (hct2 : σ2.curTime = σ.curTime) (hrq2 : σ2.releaseQueue = σ.releaseQueue) (y : Fl4SpecA64.Notification) : + BlockStep σ (updK σ2 (specPtr n) (.notification y)) (specPtr t) (specPtr n) := by + have hnt : (specPtr n) ≠ (specPtr t) := by intro e; rw [e, hkt] at hk; cases hk + refine ⟨hsa2, hct2, hrq2, fun p h1 h2 => ?_, ⟨x, y, hk, by rw [updK_kheap, Function.update_self]⟩, + ⟨xt, { xt with tcbState := .blockedOnNotification (specPtr n) }, hkt, + by rw [updK_kheap, Function.update_of_ne (Ne.symm hnt), hkt2], rfl, rfl⟩⟩ + rw [updK_kheap, Function.update_of_ne h2, hkoth p h1] + +/-- **The blocking receive after `schedule_tcb`**: the priority-ordered insertion, the notification made waiting, +then `maybe_return_sched_context` (`σ2`: the spec state `set_thread_state` leaves; a separate declaration, so the +kernel checks it apart from the state-word part, see `wake_spine`). -/ +theorem block_tail (hH : SchedHypsA st N T SR) (hO : SchedOpsA st ar N T SR DL) (hP : NPlaced T N) + (s s2 s3 : S) (a : A) (σ σ2 : Fl4SpecA64.State Unit) {t n : U64} (ht : t ∈ T) (hn : n ∈ N) + {x : Fl4SpecA64.Notification} {xt : Fl4SpecA64.Tcb} (hk : σ.kheap (specPtr n) = some (.notification x)) + (hkt : σ.kheap (specPtr t) = some (.tcb xt)) (hna : ∀ b, x.ntfnObj ≠ .activeNtfn b) + (hσ2 : stsAct (updK σ (specPtr t) (.tcb { xt with tcbState := .blockedOnNotification (specPtr n) })) (specPtr t) = + σ2) + (hk2 : σ2.kheap (specPtr n) = some (.notification x)) + (hkt2 : σ2.kheap (specPtr t) = some (.tcb { xt with tcbState := .blockedOnNotification (specPtr n) })) + (hkoth : ∀ p, p ≠ specPtr t → σ2.kheap p = σ.kheap p) + (hsa2 : σ2.schedulerAction = σ.schedulerAction ∨ σ2.schedulerAction = .chooseNewThread) + (hct2 : σ2.curTime = σ.curTime) (hrq2 : σ2.releaseQueue = σ.releaseQueue) (hid2 : σ2.idleThread = σ.idleThread) + {xs : List U64} (hQ : QueueRel (L.mem s) T .Ep (qOf (L.mem s) n) xs) (hxs : x.ntfnObj.ntfnQueue = xs.map specPtr) + (htxs : t ∉ xs) (hsorted : PrioSorted (fun y => L.mem s (prioA y)) xs) + (hwp : ∀ y ∈ T, y ≠ t → L.mem s2 (prioA y) = L.mem s (prioA y)) + (hf3 : FootEq N T (L.mem s2) (L.mem s3)) (hR3 : NtfnStateRel N T (L.mem s3) σ2) (hS3 : SR s3 σ2) + (cb : Fl4SpecA64.Badge) (cr : Fl4SpecA64.CapRights) (hAR : ActReady σ) (hnidle : (specPtr t) ≠ σ.idleThread) : + (do + let s4 ← notification.ntfn_append st s3 n t + let s5 ← notification.maybe_return_sched_context st s4 n t + ok (s5, a)) ⦃ r => + ¬ (Fl4SpecA64.receiveSignal (specPtr t) (.notificationCap (specPtr n) cb cr) true σ).2 ∧ + ∃ σ', ((), σ') ∈ (Fl4SpecA64.receiveSignal (specPtr t) (.notificationCap (specPtr n) cb cr) true σ).1 ∧ + NtfnStateRel N T (L.mem r.1) σ' ∧ SR r.1 σ' ⦄ := by + have hnt : (specPtr n) ≠ (specPtr t) := by intro e; rw [e, hkt] at hk; cases hk + refine WP.spec_bind (block_append hP s s2 s3 σ2 ht hn hk2 hkt2 hQ hxs htxs hsorted hwp hf3 hR3) ?_ + rintro s4 ⟨hR4, hW4⟩ + have hsorted3 : PrioSorted (fun y => L.mem s3 (prioA y)) xs := prioSorted_congr hsorted (fun y hy => + (hf3.tfields hP (hQ.mem y hy)).prio.trans (hwp y (hQ.mem y hy) (fun e => htxs (e ▸ hy)))) + have hq : ∀ y ∈ Fl4SpecA64.ntfnQueueOf x, ∃ z, σ2.kheap y = some (.tcb z) ∧ z.tcbPriority = prioOf σ2 y := by + intro y hy + have hy' : y ∈ x.ntfnObj.ntfnQueue := hy + rw [hxs] at hy' + obtain ⟨y', hy'x, rfl⟩ := List.mem_map.1 hy' + obtain ⟨z, hz, -⟩ := hR3.tcb y' (hQ.mem y' hy'x) + exact ⟨z, hz, by simp [prioOf, hz]⟩ + have hlist := specIns_map (σ2 := σ2) hsorted3 (hR3.at_tcb ht hkt2).prio (fun y hy => by + obtain ⟨z, hz, hzr⟩ := hR3.tcb y (hQ.mem y hy) + exact ⟨z, hz, hzr.prio⟩) + have hq' : Fl4SpecA64.ntfnQueueOf x = xs.map specPtr := hxs + rw [block_spec_eq hk hkt hna hσ2 hq + (l := (insPrio (fun y => L.mem s3 (prioA y)) (L.mem s3 (prioA t)) t xs).map specPtr) (by rw [hq']; exact hlist) cb cr] + have hSR4 := hH.stable _ _ _ _ hS3 hW4 (NtfnStep.ntfnK + (y := { x with ntfnObj := .waitingNtfn ((insPrio (fun y => L.mem s3 (prioA y)) (L.mem s3 (prioA t)) t xs).map specPtr) }) + hk2 rfl rfl) (Endp.NoBlockE.ntfnK _ _ _) + refine WP.spec_bind (hO.returnSc s4 σ _ t n hR4 hSR4 ht hn hAR + (block_step hk hkt hkt2 hkoth hsa2 hct2 hrq2 _) (by + show specPtr t ≠ σ2.idleThread + rw [hid2]; exact hnidle)) ?_ + rintro s5 ⟨hnf, σ', hm, hf, hv, hS⟩ + simp only [WP.spec_ok] + exact ⟨hnf, σ', hm, hR4.frame hP hf hv, hS⟩ + +/-- **Blocking receive** on an idle or waiting notification. -/ +theorem receive_signal_block (hH : SchedHypsA st N T SR) (hO : SchedOpsA st ar N T SR DL) (hP : NPlaced T N) + (s : S) (a : A) (σ : Fl4SpecA64.State Unit) (hR : NtfnStateRel N T (L.mem s) σ) (hSR : SR s σ) + {t : U64} (ht : t ∈ T) {xt : Fl4SpecA64.Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hnb : ∀ m, xt.tcbState ≠ .blockedOnNotification m) + {n : U64} (hn : n ∈ N) {x : Fl4SpecA64.Notification} (hk : σ.kheap (specPtr n) = some (.notification x)) + (hna : ∀ b, x.ntfnObj ≠ .activeNtfn b) + (hsort : ∀ xs, QueueRel (L.mem s) T .Ep (qOf (L.mem s) n) xs → PrioSorted (fun y => L.mem s (prioA y)) xs) + (cb : Fl4SpecA64.Badge) (cr : Fl4SpecA64.CapRights) (hAR : ActReady σ) (hNQ : NotQueued σ (specPtr t)) + (hRB : RelIfBound σ (specPtr t)) (hsane : ∀ u, σ.schedulerAction = .switchThread u → u ≠ (specPtr t)) + (hnidle : (specPtr t) ≠ σ.idleThread) : + notification.receive_signal st ar s a t n true ⦃ r => + ¬ (Fl4SpecA64.receiveSignal (specPtr t) (.notificationCap (specPtr n) cb cr) true σ).2 ∧ + ∃ σ', ((), σ') ∈ (Fl4SpecA64.receiveSignal (specPtr t) (.notificationCap (specPtr n) cb cr) true σ).1 ∧ + NtfnStateRel N T (L.mem r.1) σ' ∧ SR r.1 σ' ⦄ := by + have hx := hR.at_ntfn hn hk + have hxt := hR.at_tcb ht hkt + have hnm := not_member hR ht hkt hnb + -- the queue + obtain ⟨xs, hQ, hxs, hB, hst⟩ : ∃ xs, QueueRel (L.mem s) T .Ep (qOf (L.mem s) n) xs ∧ + x.ntfnObj.ntfnQueue = xs.map specPtr ∧ (∀ y ∈ xs, BlockedOn (L.mem s) n y) ∧ + (stOf (L.mem s) n = obj.NTFN_IDLE ∨ stOf (L.mem s) n = obj.NTFN_WAITING) := by + have := hx.obj + cases h : x.ntfnObj with + | idleNtfn => + rw [h] at this + exact ⟨[], this.2, rfl, by simp, Or.inl (stOf_idle (T := T) (h ▸ hx.obj) rfl)⟩ + | waitingNtfn q => + rw [h] at this + obtain ⟨h1, xs, -, rfl, h2, h3⟩ := this + exact ⟨xs, h2, rfl, h3, Or.inr (stOf_waiting (T := T) (h ▸ hx.obj) rfl)⟩ + | activeNtfn b => exact absurd h (hna b) + have htxs : t ∉ xs := fun h => hnm n hn x hk (by rw [hxs]; exact List.mem_map_of_mem h) + have hsorted := hsort xs hQ + -- step 1: the state and block-object words + set x1 : Fl4SpecA64.Tcb := { xt with tcbState := .blockedOnNotification (specPtr n) } + set σ1 := updK σ (specPtr t) (.tcb x1) + set s2 := L.wr (L.wr s (stA t) sched.TS_BLOCKED_ON_NTFN) (tBlockA t) n + obtain ⟨hw1, hoth1⟩ := twr_wr (st := st) hP ht (K := 3328#u64) (Or.inl rfl) s sched.TS_BLOCKED_ON_NTFN + obtain ⟨hw2, hoth2⟩ := twr_wr (st := st) hP ht (K := 3336#u64) (Or.inr (Or.inl rfl)) + (L.wr s (stA t) sched.TS_BLOCKED_ON_NTFN) n + rw [← stA_eq] at hw1 hoth1 + rw [← tBlockA_eq] at hw2 hoth2 + have hw := hw1.trans hw2 + have hR1 : NtfnStateRel N T (L.mem s2) σ1 := by + refine hR.upd_tcb ht _ hw.ntfn hw.tcb hw.links (Or.inl hnm) ⟨?_, ?_, ?_, ?_⟩ + · show (L.mem s2 (stA t)).val = 6 + rw [stA_eq, hoth2 _ (by simp) (by simp) (by simp), ← stA_eq, rd_wr_eq (foot_st hP ht).1, six_val] + · intro m hm + cases hm + show specPtr (L.mem s2 (tBlockA t)) = (specPtr n) + rw [rd_wr_eq (foot_blk hP ht).1] + · show (L.mem s2 (badgeRegA t)).bv = badgeOf xt + rw [badgeRegA_eq, hoth2 _ (by simp) (by simp) (by simp), hoth1 _ (by simp) (by simp) (by simp), + ← badgeRegA_eq]; exact hxt.badge + · show (L.mem s2 (prioA t)).val = xt.tcbPriority.toNat + rw [prioA_eq', hoth2 _ (by simp) (by simp) (by simp), hoth1 _ (by simp) (by simp) (by simp), + ← prioA_eq']; exact hxt.prio + have hSR1 := hH.block _ _ _ _ t hSR hNQ hRB hsane hnidle hw.foot (NtfnStep.tcb hkt (.blockedOnNotification (specPtr n)) xt.tcbArch) + (fun p x y h1 h2 hp => by + simp only [Function.update_apply, hp, ↓reduceIte] at h2; rw [h1] at h2; cases h2; exact ⟨rfl, id⟩) + have hnt : (specPtr n) ≠ (specPtr t) := by intro e; rw [e, hkt] at hk; cases hk + -- `stsAct` names `specPtr t`: generalized away, with the facts used about it + obtain ⟨σ2, hσ2⟩ : ∃ σ2, stsAct σ1 (specPtr t) = σ2 := ⟨_, rfl⟩ + have hkh2 : σ2.kheap = σ1.kheap := by rw [← hσ2, stsAct_kheap] + have hk2 : σ2.kheap (specPtr n) = some (.notification x) := by + rw [hkh2]; simp [σ1, hnt, hk] + have hkt2 : σ2.kheap (specPtr t) = some (.tcb x1) := by rw [hkh2]; simp [σ1] + have hsa2 : σ2.schedulerAction = σ1.schedulerAction ∨ σ2.schedulerAction = .chooseNewThread := by + rw [← hσ2]; exact stsAct_sa _ _ + have hct2 : σ2.curTime = σ1.curTime := by rw [← hσ2]; exact stsAct_curTime _ _ + have hrq2 : σ2.releaseQueue = σ1.releaseQueue := by rw [← hσ2]; exact stsAct_releaseQueue _ _ + have hid2 : σ2.idleThread = σ1.idleThread := by rw [← hσ2]; exact stsAct_idleThread _ _ + have hkv2 : KView σ1 σ2 := by rw [← hσ2]; exact KView.stsAct _ _ + -- step 2: `schedule_tcb` + have key : (do + let s1 ← obj.Tcb.set_state st t s sched.TS_BLOCKED_ON_NTFN + let s2 ← obj.Tcb.set_block_obj st t s1 n + let s3 ← sched.schedule_tcb st s2 t + let s4 ← notification.ntfn_append st s3 n t + let s5 ← notification.maybe_return_sched_context st s4 n t + ok (s5, a)) ⦃ r => + ¬ (Fl4SpecA64.receiveSignal (specPtr t) (.notificationCap (specPtr n) cb cr) true σ).2 ∧ + ∃ σ', ((), σ') ∈ (Fl4SpecA64.receiveSignal (specPtr t) (.notificationCap (specPtr n) cb cr) true σ).1 ∧ + NtfnStateRel N T (L.mem r.1) σ' ∧ SR r.1 σ' ⦄ := by + step + step + subst s1_post s2_post + refine WP.spec_bind (hO.scheduleTcb _ σ1 t hR1 hSR1 ht) ?_ + rintro s3 ⟨hf3, hS3⟩ + rw [hσ2] at hS3 + have hR3 : NtfnStateRel N T (L.mem s3) σ2 := hR1.frame hP hf3 hkv2 + exact block_tail hH hO hP s _ s3 a σ σ2 ht hn hk hkt hna hσ2 hk2 hkt2 + (fun p hp => by rw [hkh2]; simp [σ1, hp]) hsa2 hct2 hrq2 hid2 hQ hxs htxs hsorted + (fun y hyT hyt => (hw.tcb y hyT hyt).prio) hf3 hR3 hS3 cb cr hAR hnidle + unfold notification.receive_signal + step + rw [st_post, epState_eq] at * + rcases hst with hst | hst + · simp only [hst, ↓reduceIte] + exact key + · simp only [hst, ntfn_consts.1, ntfn_consts.2.1, show (1#u64 : U64) ≠ 0#u64 by decide, ↓reduceIte] + exact key + +end + +end + +end Fl4ProofsA64.P3.Notif + +namespace Fl4ProofsA64.P3.Endp +open Fl4SpecA64 Fl4Spec Classical Fl4ProofsA64.P3.Notif + +noncomputable section + +variable {Z : Type} + +/-- `tcb_ep_append` on TCBs: the ordered insertion by the spec's priorities (x86 `P3/EpSpec`'s, deferred from +`P3/EpSpecA`). -/ +theorem tcbEpAppend_eq {σ : State Z} {t p : ObjRef} {x : Endpoint} {xt : Tcb} + (hk : σ.kheap p = some (.endpoint x)) (hkt : σ.kheap t = some (.tcb xt)) (g : ObjRef → Priority) + (hq : ∀ y ∈ x.epQueue, ∃ x, σ.kheap y = some (.tcb x) ∧ x.tcbPriority = g y) (isRecv : Bool) : + tcbEpAppend (Z := Z) t p isRecv σ = ({((), updK σ p (.endpoint + (updateEpQueue x (specIns x.epQueue g xt.tcbPriority t) isRecv)))}, False) := by + unfold tcbEpAppend + rw [nd_bind_one (getEndpoint_eq hk), nd_pure_bind', nd_bind_one (tcbAppend_eq hkt _ g hq)] + exact setEndpoint_eq _ hk + +end + +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/NtfnCancelAllA.lean b/proofs/Fl4ProofsA64/P3/NtfnCancelAllA.lean new file mode 100644 index 000000000..ad7fb05ff --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/NtfnCancelAllA.lean @@ -0,0 +1,320 @@ +/- +P3 (aarch64, A64-P3-IPC gen 5, ruling item 3): **`cancel_all_signals` refines `Fl4SpecA64.cancelAllSignals`** +(`Z = Unit`; a notification's finalisation) over `NtfnStateRel` and the notification scheduler interface +`SchedHypsA` / `SchedOpsA` (with gen 5's `restartTail` / `reschedule` fields), the spec not failing (x86 +`P3/IpcCancelSignals`' loop, on `NtfnStateRel ∧ SR` as gen 4's `cancel_all_rel`). FL4's AArch64 +`cancel_all_signals` and its loop are the x86 text. + +FL4 walks the notification's queue from its head; each step reads the successor, dequeues the thread +(`ntfn_dequeue`), sets it `Restart` (`set_thread_state`: the state word, then `schedule_tcb`), then runs +`notification::unblock_sporadic` and `possible_switch_to` (`rustRestartTailN`). The spec runs +`remove_and_restart_ntfn_queued_thread` over the queue (`mapM_x`), then `reschedule_required`. + +* `rrn_eq` (the spec's step: the restart tail from the state `set_thread_state t Restart` leaves), + `afterDequeue_obj`, `NInv` (the loop invariant: the queue still to visit, the spec's outcomes from it among the + whole's, the whole failing only if the rest does), `sig_tail` (the restart tail, a separate declaration at the + `stsAct` state), `sig_deq` (one step), `sig_loop`, **`cancel_all_signals_rel`**. + +Each waiter is blocked on the notification (`BlockedOn`, code 6), so its restart is `NoBlockE`'s runnable branch from +`inClassR`. Hypotheses: `NPlaced`, `n ∈ N`, the interface; no named premise. + +(gen 7, ruling item 3) `cancel_all_signals_rel` takes `hon`: every waiter of the notification is on the current core +(`SchedOpsA.restartTail`'s precondition); `NInv` carries it for the threads still to visit, across each step by +`SchedOpsA.coreKeep`. +-/ +import Fl4ProofsA64.P3.NtfnWakeA +import Fl4ProofsA64.P3.EpCancelA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Notif +open fl4_core_a64 Fl4ProofsA64.P1 + +noncomputable section + +section Spec +open Fl4SpecA64 + +/-- One step of the spec's loop. -/ +abbrev rrn (n : U64) (t : ObjRef) : SMonad Unit Unit := removeAndRestartNtfnQueuedThread t (specPtr n) + +/-- **The spec's step**: after the dequeue, the restart tail from the state `set_thread_state t Restart` leaves. -/ +theorem rrn_eq {σ' σd : State Unit} {t n : ObjRef} {xt : Tcb} + (hdq : tcbNtfnDequeue (Z := Unit) t n σ' = ({((), σd)}, False)) (hktd : σd.kheap t = some (.tcb xt)) : + removeAndRestartNtfnQueuedThread (Z := Unit) t n σ' = + Endp.specRestartTail t (stsAct (updK σd t (.tcb { xt with tcbState := .restart })) t) := by + unfold removeAndRestartNtfnQueuedThread + rw [nd_bind_one hdq, nd_bind_one (setThreadState_eq .restart hktd)] + rfl + +/-- The notification after dequeuing the head `t0` of its waiting queue `t0 :: rest'`. -/ +theorem afterDequeue_obj {y : Notification} {t0 : U64} {rest' : List U64} + (hw : y.ntfnObj = .waitingNtfn ((t0 :: rest').map specPtr)) (hnd : (t0 :: rest').Nodup) : + rest' ≠ [] → (afterDequeue y t0).ntfnObj = .waitingNtfn (rest'.map specPtr) := by + intro hne + have hrm : removeAll (specPtr t0) (ntfnQueueOf y) = rest'.map specPtr := by + show removeAll (specPtr t0) y.ntfnObj.ntfnQueue = _ + rw [hw] + exact (removeAll_map hnd t0).trans (by rw [List.erase_cons_head]) + show (match removeAll (specPtr t0) (ntfnQueueOf y) with + | [] => Ntfn.idleNtfn + | _ => Ntfn.waitingNtfn (removeAll (specPtr t0) (ntfnQueueOf y))) = _ + rw [hrm] + obtain ⟨h, tl, e⟩ := List.exists_cons_of_ne_nil hne + subst e + rfl + +end Spec + +section Loop +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] {N T : Set U64} + {SR : S → Fl4SpecA64.State Unit → Prop} {DL : S → U64 → Prop} + +variable (st N T SR) in +/-- The loop's invariant: the queue still to visit `rest` (the notification waiting on it while nonempty), FL4's +cursor at its head, the spec's outcomes from it among the whole loop's, the whole failing only if the rest does; +(gen 7) the threads still to visit on the current core. -/ +def NInv (n : U64) (σ0 : Fl4SpecA64.State Unit) (q0 : List Fl4SpecA64.ObjRef) (x : S × U64 × U64) : Prop := + ∃ σ' y, ∃ rest : List U64, NtfnStateRel N T (L.mem x.1) σ' ∧ SR x.1 σ' ∧ + σ'.kheap (specPtr n) = some (.notification y) ∧ + (rest ≠ [] → y.ntfnObj = .waitingNtfn (rest.map specPtr)) ∧ x.2.1 = rest.headD 0#u64 ∧ + rest.length ≤ x.2.2.val ∧ (∀ t ∈ rest, t ∈ T → L.mem x.1 (affA t) = L.cpu x.1) ∧ + (∀ z, z ∈ (Fl4Spec.NonDet.mapMx (rrn n) (rest.map specPtr) σ').1 → + z ∈ (Fl4Spec.NonDet.mapMx (rrn n) q0 σ0).1) ∧ + ((Fl4Spec.NonDet.mapMx (rrn n) q0 σ0).2 → (Fl4Spec.NonDet.mapMx (rrn n) (rest.map specPtr) σ').2) + +/-- The restart tail of the dequeued head, from the state `set_thread_state t0 Restart` leaves, re-establishing the +invariant (a separate declaration at the `stsAct` state, see `EpCancelA.restart_spine`). -/ +theorem sig_tail (hO : SchedOpsA st ar N T SR DL) (hP : NPlaced T N) {n : U64} + (σ0 : Fl4SpecA64.State Unit) (q0 : List Fl4SpecA64.ObjRef) {s2 : S} {σ' : Fl4SpecA64.State Unit} + {y : Fl4SpecA64.Notification} {rest' : List U64} {t0 nx fuel1 : U64} + (hky : σ'.kheap (specPtr n) = some (.notification y)) + (hw : y.ntfnObj = .waitingNtfn ((t0 :: rest').map specPtr)) (hnd : (t0 :: rest').Nodup) (ht0T : t0 ∈ T) + {xt : Fl4SpecA64.Tcb} + (hktd : (updK σ' (specPtr n) (.notification (afterDequeue y t0))).kheap (specPtr t0) = some (.tcb xt)) + (hq1 : ∀ n' ∈ N, ∀ z, (updK σ' (specPtr n) (.notification (afterDequeue y t0))).kheap (specPtr n') = + some (.notification z) → specPtr t0 ∉ z.ntfnObj.ntfnQueue) + (hR2 : NtfnStateRel N T (L.mem s2) (stsAct (updK (updK σ' (specPtr n) (.notification (afterDequeue y t0))) + (specPtr t0) (.tcb { xt with tcbState := .restart })) (specPtr t0))) + (hS2 : SR s2 (stsAct (updK (updK σ' (specPtr n) (.notification (afterDequeue y t0))) + (specPtr t0) (.tcb { xt with tcbState := .restart })) (specPtr t0))) + (hdq : Fl4SpecA64.tcbNtfnDequeue (Z := Unit) (specPtr t0) (specPtr n) σ' = + ({((), updK σ' (specPtr n) (.notification (afterDequeue y t0)))}, False)) + (hnx : nx = rest'.headD 0#u64) (hlen : rest'.length ≤ fuel1.val) + (hon : ∀ t ∈ t0 :: rest', t ∈ T → L.mem s2 (affA t) = L.cpu s2) + (hNQ : NotQueued (stsAct (updK (updK σ' (specPtr n) (.notification (afterDequeue y t0))) + (specPtr t0) (.tcb { xt with tcbState := .restart })) (specPtr t0)) (specPtr t0)) + (hinc : ∀ z, z ∈ (Fl4Spec.NonDet.mapMx (rrn n) ((t0 :: rest').map specPtr) σ').1 → + z ∈ (Fl4Spec.NonDet.mapMx (rrn n) q0 σ0).1) + (hfail : (Fl4Spec.NonDet.mapMx (rrn n) q0 σ0).2 → + (Fl4Spec.NonDet.mapMx (rrn n) ((t0 :: rest').map specPtr) σ').2) : + (do + let s3 ← notification.unblock_sporadic st s2 t0 + let s4 ← sched.possible_switch_to st s3 t0 + ok (ControlFlow.cont (s4, nx, fuel1) : ControlFlow (S × U64 × U64) S)) ⦃ r => + ∃ x' : S × U64 × U64, r = (ControlFlow.cont x' : ControlFlow (S × U64 × U64) S) ∧ + NInv st N T SR n σ0 q0 x' ∧ x'.2.2 = fuel1 ⦄ := by + have hte : specPtr t0 ≠ specPtr n := by + intro h; rw [h, updK_kheap, Function.update_self] at hktd; cases hktd + have hk2 : (stsAct (updK (updK σ' (specPtr n) (.notification (afterDequeue y t0))) (specPtr t0) + (.tcb { xt with tcbState := .restart })) (specPtr t0)).kheap (specPtr t0) = + some (.tcb { xt with tcbState := .restart }) := by + rw [stsAct_kheap, updK_kheap, Function.update_self] + have hkn2 : (stsAct (updK (updK σ' (specPtr n) (.notification (afterDequeue y t0))) (specPtr t0) + (.tcb { xt with tcbState := .restart })) (specPtr t0)).kheap (specPtr n) = + some (.notification (afterDequeue y t0)) := by + rw [stsAct_kheap, updK_kheap, Function.update_of_ne hte.symm, updK_kheap, Function.update_self] + have hq2 : ∀ n' ∈ N, ∀ z, (stsAct (updK (updK σ' (specPtr n) (.notification (afterDequeue y t0))) (specPtr t0) + (.tcb { xt with tcbState := .restart })) (specPtr t0)).kheap (specPtr n') = + some (.notification z) → specPtr t0 ∉ z.ntfnObj.ntfnQueue := by + intro n' hn' z hz + have hne : specPtr n' ≠ specPtr t0 := by intro h; rw [h, hk2] at hz; cases hz + rw [stsAct_kheap, updK_kheap, Function.update_of_ne hne] at hz + exact hq1 n' hn' z hz + have hrr : Fl4SpecA64.removeAndRestartNtfnQueuedThread (Z := Unit) (specPtr t0) (specPtr n) σ' = _ := + rrn_eq hdq hktd + have hprog : (do + let s3 ← notification.unblock_sporadic st s2 t0 + let s4 ← sched.possible_switch_to st s3 t0 + ok (ControlFlow.cont (s4, nx, fuel1) : ControlFlow (S × U64 × U64) S)) = + (rustRestartTailN st s2 t0 >>= fun s4 => ok (ControlFlow.cont (s4, nx, fuel1) : ControlFlow (S × U64 × U64) S)) := by + first + | (simp only [rustRestartTailN, bind_assoc]; done) + | (unfold rustRestartTailN; rw [bind_assoc_eq]) + | (unfold rustRestartTailN; simp only [Std.bind_tc_eq, Std.bind_assoc]) + | rfl + rw [hprog] + refine WP.spec_bind (hO.restartTail s2 _ t0 _ hR2 hS2 ht0T hk2 rfl hq2 (hon t0 List.mem_cons_self ht0T) hNQ) ?_ + rintro s4 ⟨hnf, σ'', hres, hf, hv, hS4⟩ + rw [WP.spec_ok] + refine ⟨_, rfl, ⟨σ'', afterDequeue y t0, rest', hR2.frame hP hf hv, hS4, hv.1 _ _ hkn2, + afterDequeue_obj hw hnd, hnx, hlen, + fun t ht hT => (hO.coreKeep _ _ _ _ hS2 hS4).onCore hT (hon t (List.mem_cons_of_mem _ ht) hT), + fun z hz => hinc z (Endp.mapMx_cons_mem (g := rrn n) (l := rest'.map specPtr) (by (try simp only [rrn]); rw [hrr]; exact hres) z hz), + fun hw' => Det.detAt_bind_fail ((Det.det_removeAndRestartNtfn _ _).at σ') (by (try simp only [rrn]); rw [hrr]; exact hnf) + (by (try simp only [rrn]); rw [hrr]; exact hres) (hfail hw')⟩, rfl⟩ + +/-- **One dequeue-and-restart step of the loop** (FL4's step after the successor read). -/ +theorem sig_deq (hH : SchedHypsA st N T SR) (hO : SchedOpsA st ar N T SR DL) (hP : NPlaced T N) {n : U64} + (hn : n ∈ N) (σ0 : Fl4SpecA64.State Unit) (q0 : List Fl4SpecA64.ObjRef) {s : S} {σ' : Fl4SpecA64.State Unit} + {y : Fl4SpecA64.Notification} {rest' : List U64} {t0 nx fuel1 : U64} + (hR : NtfnStateRel N T (L.mem s) σ') (hSR : SR s σ') (hky : σ'.kheap (specPtr n) = some (.notification y)) + (hw : y.ntfnObj = .waitingNtfn ((t0 :: rest').map specPtr)) (hst : stOf (L.mem s) n = 1#u64) + (hQ : QueueRel (L.mem s) T .Ep (qOf (L.mem s) n) (t0 :: rest')) + (hB : ∀ z ∈ t0 :: rest', BlockedOn (L.mem s) n z) + (hnx : nx = rest'.headD 0#u64) (hlen : rest'.length ≤ fuel1.val) + (hon : ∀ t ∈ t0 :: rest', t ∈ T → L.mem s (affA t) = L.cpu s) + (hinc : ∀ z, z ∈ (Fl4Spec.NonDet.mapMx (rrn n) ((t0 :: rest').map specPtr) σ').1 → + z ∈ (Fl4Spec.NonDet.mapMx (rrn n) q0 σ0).1) + (hfail : (Fl4Spec.NonDet.mapMx (rrn n) q0 σ0).2 → + (Fl4Spec.NonDet.mapMx (rrn n) ((t0 :: rest').map specPtr) σ').2) : + (do + let s1 ← notification.ntfn_dequeue st s n t0 + let s2 ← sched.set_thread_state st s1 t0 sched.TS_RESTART + let s3 ← notification.unblock_sporadic st s2 t0 + let s4 ← sched.possible_switch_to st s3 t0 + ok (ControlFlow.cont (s4, nx, fuel1) : ControlFlow (S × U64 × U64) S)) ⦃ r => + ∃ x' : S × U64 × U64, r = (ControlFlow.cont x' : ControlFlow (S × U64 × U64) S) ∧ + NInv st N T SR n σ0 q0 x' ∧ x'.2.2 = fuel1 ⦄ := by + have hd : t0 ∈ t0 :: rest' := List.mem_cons_self + have ht0T : t0 ∈ T := hQ.mem t0 hd + obtain ⟨xt, hkt, hxt⟩ := hR.tcb t0 ht0T + obtain ⟨m6, hm6⟩ : ∃ m, xt.tcbState = .blockedOnNotification m := by + have := hxt.st + rw [(hB t0 hd).1] at this + exact tsCode_six' this.symm + have hmem : specPtr t0 ∈ Fl4SpecA64.ntfnQueueOf y := by + show specPtr t0 ∈ y.ntfnObj.ntfnQueue + rw [hw]; simp [Fl4SpecA64.Ntfn.ntfnQueue] + have hdq : Fl4SpecA64.tcbNtfnDequeue (Z := Unit) (specPtr t0) (specPtr n) σ' = + ({((), updK σ' (specPtr n) (.notification (afterDequeue y t0)))}, False) := tcbNtfnDequeue_eq hky hmem + have hte : specPtr t0 ≠ specPtr n := by intro h; rw [h, hky] at hkt; cases hkt + have hktd : (updK σ' (specPtr n) (.notification (afterDequeue y t0))).kheap (specPtr t0) = some (.tcb xt) := by + rw [updK_kheap, Function.update_of_ne hte, hkt] + refine WP.spec_bind (ntfn_dequeue_full hP s hn t0 _ hQ hd) ?_ + rintro s1 ⟨h1, h2, h3⟩ + obtain ⟨hR1, hq1⟩ := rel_after_dequeue hR hn hky hw hQ hB hd h1 h2 h3 hst + have hSR1 : SR s1 (updK σ' (specPtr n) (.notification (afterDequeue y t0))) := + hH.stable _ _ _ _ hSR h3.foot (NtfnStep.ntfnK (y := afterDequeue y t0) hky rfl rfl) (Endp.NoBlockE.ntfnK _ _ _) + refine WP.spec_bind (set_thread_state_ntfn hH hO hP s1 _ hR1 hSR1 ht0T hktd hq1 .restart (fun _ h => by cases h) + sched.TS_RESTART (by rw [tsVals.2.1]; rfl) + (Endp.NoBlockE.updT hktd _ (Or.inr (Or.inl ⟨rfl, (by rw [hm6]; exact fun h => by cases h), + Or.inl (Or.inr (Or.inl ⟨m6, hm6⟩))⟩)))) ?_ + rintro s2 ⟨hR2, hS2⟩ + exact sig_tail hO hP σ0 q0 hky hw hQ.nodup ht0T hktd hq1 hR2 hS2 hdq hnx hlen + (fun t ht hT => (hO.coreKeep _ _ _ _ hSR hS2).onCore hT (hon t ht hT)) + ((hH.notQueued s1 _ t0 xt hSR1 hktd (by rw [hm6]; rfl)).sts _ _ _) hinc hfail + +set_option maxHeartbeats 4000000 in +/-- **The loop of `cancel_all_signals`** on `NtfnStateRel ∧ SR`. -/ +theorem sig_loop (hH : SchedHypsA st N T SR) (hO : SchedOpsA st ar N T SR DL) (hP : NPlaced T N) + {n : U64} (hn : n ∈ N) (σ0 : Fl4SpecA64.State Unit) (q0 : List Fl4SpecA64.ObjRef) (x0 : S × U64 × U64) + (h0 : NInv st N T SR n σ0 q0 x0) : + notification.cancel_all_signals_loop st x0.1 n x0.2.1 x0.2.2 ⦃ s' => + ¬ (Fl4Spec.NonDet.mapMx (rrn n) q0 σ0).2 ∧ + ∃ σf, ((), σf) ∈ (Fl4Spec.NonDet.mapMx (rrn n) q0 σ0).1 ∧ NtfnStateRel N T (L.mem s') σf ∧ SR s' σf ⦄ := by + unfold notification.cancel_all_signals_loop + apply loop.spec_decr_nat (measure := fun x : S × U64 × U64 => x.2.2.val) + (inv := NInv st N T SR n σ0 q0) _ _ x0 ?_ h0 + rintro ⟨s, t, fuel⟩ ⟨σ', y, rest, hR, hSR, hky, hw, ht, hlen, hon, hinc, hfail⟩ + simp only at hR hSR hky hw ht hlen hon hinc hfail ⊢ + unfold notification.cancel_all_signals_loop.body + step with obj.Tcb.is_null.spec as ⟨bn, hbn⟩ + cases rest with + | nil => + simp only [List.headD_nil] at ht + subst ht + simp only [hbn, decide_true, ↓reduceIte, WP.spec_ok] + exact ⟨fun h => hfail h, σ', hinc _ rfl, hR, hSR⟩ + | cons t0 rest' => + simp only [List.headD_cons] at ht + subst t + have hw' := hw (List.cons_ne_nil _ _) + have hobj := (hR.at_ntfn hn hky).obj + rw [hw'] at hobj + obtain ⟨hst, xs, -, hxs, hQ, hB⟩ := hobj + have hxs' : xs = t0 :: rest' := + (List.map_injective_iff.2 (fun a b h => specPtr_inj h) hxs).symm + subst hxs' + have ht0T : t0 ∈ T := hQ.mem t0 (by simp) + have ht00 : t0 ≠ 0#u64 := hQ.ne0 hP.tcb t0 (by simp) + have hf0 : fuel > 0#u64 := by + simp only [List.length_cons] at hlen + show (0#u64 : U64).val < fuel.val; simp; omega + simp only [hbn, ht00, decide_false, Bool.false_eq_true, ↓reduceIte, hf0] + step as ⟨fuel1, hfuel1⟩ + have hseg := hQ.seg + rw [show t0 :: rest' = [] ++ t0 :: rest' from rfl, Seg_mid] at hseg + obtain ⟨-, -, hnext, -⟩ := hseg + unfold tcbqueue.next_of + step with tcbqueue.next.spec as ⟨nx, hnx⟩ + try simp only [bind_ok] + refine WP.spec_mono (sig_deq hH hO hP hn σ0 q0 hR hSR hky hw' hst hQ hB (by rw [hnx, hnext]) + (by rw [hfuel1]; simp only [List.length_cons] at hlen; scalar_tac) hon hinc hfail) ?_ + rintro r ⟨x', rfl, hI, hf⟩ + exact ⟨hI, by rw [hf, hfuel1]; scalar_tac⟩ + +set_option maxHeartbeats 4000000 in +/-- **`cancel_all_signals` refines `cancel_all_signals`** on `NtfnStateRel ∧ SR`, the spec not failing. -/ +theorem cancel_all_signals_rel (hH : SchedHypsA st N T SR) (hO : SchedOpsA st ar N T SR DL) (hP : NPlaced T N) + (s : S) (σ : Fl4SpecA64.State Unit) (hR : NtfnStateRel N T (L.mem s) σ) (hSR : SR s σ) {n : U64} (hn : n ∈ N) + (hon : ∀ z, σ.kheap (specPtr n) = some (.notification z) → ∀ t ∈ T, specPtr t ∈ z.ntfnObj.ntfnQueue → + L.mem s (affA t) = L.cpu s) : + notification.cancel_all_signals st s n ⦃ s' => ¬ (Fl4SpecA64.cancelAllSignals (Z := Unit) (specPtr n) σ).2 ∧ + ∃ σ', ((), σ') ∈ (Fl4SpecA64.cancelAllSignals (Z := Unit) (specPtr n) σ).1 ∧ + NtfnStateRel N T (L.mem s') σ' ∧ SR s' σ' ⦄ := by + obtain ⟨y, hky, hy⟩ := hR.ntfn n hn + unfold notification.cancel_all_signals + step with obj.Ntfn.state.spec as ⟨i, hi⟩ + rw [epState_eq] at hi + cases hw : y.ntfnObj with + | waitingNtfn q => + have hsw : i = obj.NTFN_WAITING := by rw [hi]; exact stOf_waiting hy.obj hw + have hsp : Fl4SpecA64.cancelAllSignals (Z := Unit) (specPtr n) σ = + (Fl4Spec.NonDet.mapMx (rrn n) q >>= fun _ => Fl4SpecA64.rescheduleRequired) σ := by + unfold Fl4SpecA64.cancelAllSignals + rw [nd_bind_one (getNotification_eq hky), hw] + simp only [hsw, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + have hobj := hy.obj + rw [hw] at hobj + obtain ⟨-, xs, -, hqx, hQ, -⟩ := hobj + step with notification.get_queue.spec as ⟨qq, hqq⟩ + have hfuel : layout.WALK_FUEL.val = 2 ^ 64 - 1 := by simp [layout.WALK_FUEL, U64.rMax] + have hI0 : NInv st N T SR n σ q (s, qq.head, layout.WALK_FUEL) := + ⟨σ, y, xs, hR, hSR, hky, fun _ => by rw [hw, hqx], by rw [hqq, epQ_eq, hQ.head], + by rw [hfuel]; exact Endp.len_le_max xs hQ.nodup (hQ.ne0 hP.tcb), + fun t ht hT => hon y hky t hT (by rw [hw]; show specPtr t ∈ q; rw [hqx]; exact List.mem_map.2 ⟨t, ht, rfl⟩), + fun z hz => by rw [hqx]; exact hz, fun h => by rw [← hqx]; exact h⟩ + refine WP.spec_bind (sig_loop hH hO hP hn σ q _ hI0) ?_ + rintro s1 ⟨hnf, σf, hmf, hRf, hSf⟩ + refine WP.spec_mono (hO.reschedule s1 σf hRf hSf) ?_ + rintro s2 ⟨hnr2, σr, hrs, hf2, hv2, hS2⟩ + refine ⟨?_, σr, ?_, hRf.frame hP hf2 hv2, hS2⟩ + · rw [hsp] + exact fun h => hnr2 (Det.detAt_bind_fail + ((Det.det_mapMx (fun a => Det.det_removeAndRestartNtfn a _) q).at σ) hnf hmf h) + rw [hsp] + exact ⟨((), σf), hmf, hrs⟩ + | idleNtfn => + have hsw : i = obj.NTFN_IDLE := by rw [hi]; exact stOf_idle hy.obj hw + have hsp : Fl4SpecA64.cancelAllSignals (Z := Unit) (specPtr n) σ = ({((), σ)}, False) := by + unfold Fl4SpecA64.cancelAllSignals + rw [nd_bind_one (getNotification_eq hky), hw] + rfl + simp only [hsw, ntfn_consts.1, ntfn_consts.2.1, show (0#u64 : U64) ≠ 1#u64 by decide, bne_iff_ne, ne_eq, + not_false_eq_true, ↓reduceIte, WP.spec_ok] + exact ⟨by rw [hsp]; exact fun h => h, σ, by rw [hsp]; rfl, hR, hSR⟩ + | activeNtfn b => + have hsw : i = obj.NTFN_ACTIVE := by rw [hi]; exact stOf_active hy.obj hw + have hsp : Fl4SpecA64.cancelAllSignals (Z := Unit) (specPtr n) σ = ({((), σ)}, False) := by + unfold Fl4SpecA64.cancelAllSignals + rw [nd_bind_one (getNotification_eq hky), hw] + rfl + simp only [hsw, ntfn_consts.2.1, ntfn_consts.2.2, show (2#u64 : U64) ≠ 1#u64 by decide, bne_iff_ne, ne_eq, + not_false_eq_true, ↓reduceIte, WP.spec_ok] + exact ⟨by rw [hsp]; exact fun h => h, σ, by rw [hsp]; rfl, hR, hSR⟩ + +end Loop + +end + +end Fl4ProofsA64.P3.Notif diff --git a/proofs/Fl4ProofsA64/P3/NtfnJoinA.lean b/proofs/Fl4ProofsA64/P3/NtfnJoinA.lean new file mode 100644 index 000000000..7e6b3ffe2 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/NtfnJoinA.lean @@ -0,0 +1,77 @@ +/- +P3 (aarch64, A64-P3-IPC gen 1): **how the notification relation joins `JAtZA`** (no x86 counterpart: x86's +`NtfnStateRel` sits beside `JAtZB` with its own ghost `N`). + +`P3/NtfnRelA` restates two JointA definitions so that `JointA` can later import it; here they are shown equal, and +its placement hypothesis `NPlaced` is derived from `JAtZA`'s `PlaceA`: + +* `tsCode_eqA : Notif.tsCode = tsCodeA`, `optRef_eqA : Notif.optRef = optRefA`: `TcbRel1.st` reads the word + `SchedRelA.tcb` reads, with the same code; +* `nPlaced_ofA`: `NPlaced Z.T N` at `J : JAtZA` for notifications that are `th` objects of kind `other 64` + (`PlaceA.align` / `fit` / `apart`), given that the TCBs are nonzero (`hnz`, the same decode-site fact as + `FrontEndExtraA`'s `hnz`, to be folded into `PlaceA` at S3's `par/a64-p3del` merge); +* `NtfnStateRel.tcbSt` / `.tcbPrio`: at `JAtZA`, `TcbRel1`'s state and priority fields follow from `SchedRelA.tcb` + / `SchedRestA.prio` for the same spec TCB, so only `blk` and `badge` are new when the relation becomes a `JAtZA` + field. + +Decode-site hypotheses: `hnz` (TCBs nonzero), `hN` (each notification is an `other 64` object of `th`). No named +premise. +-/ +import Fl4ProofsA64.P3.NtfnRecvA +import Fl4ProofsA64.P3.JointA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Notif +open fl4_core_a64 Fl4ProofsA64.P1 + +noncomputable section + +theorem tsCode_eqA : tsCode = tsCodeA := by + funext x + cases x with + | ext e => cases e; rfl + | _ => rfl + +theorem optRef_eqA : optRef = optRefA := by + funext w; rfl + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- **`NPlaced` from `PlaceA`**: TCBs of `th` (nonzero, `hnz`) and notifications that are `other 64` objects of +`th`. -/ +theorem nPlaced_ofA (J : JAtZA (L := L) AR Z s a σ m) (hnz : ∀ t ∈ Z.T, t ≠ 0#u64) {N : Set U64} + (hN : ∀ n ∈ N, Z.th n = some (.other 64)) : NPlaced Z.T N := by + have hT : ∀ t ∈ Z.T, Z.th t = some .tcb := fun t ht => (J.thT t).2 ht + refine ⟨fun t ht => ⟨fun h0 => hnz t ht (UScalar.eq_of_val_eq (by simpa using h0)), ?_, ?_⟩, + fun n hn => ⟨?_, ?_⟩, fun n hn t ht => ?_⟩ + · have := J.place.align t _ (hT t ht); simpa [ObjKindA.size] using this + · have := J.place.fit t _ (hT t ht); simpa [ObjKindA.size] using this + · have := J.place.align n _ (hN n hn); simpa [ObjKindA.size] using this + · have := J.place.fit n _ (hN n hn); simpa [ObjKindA.size] using this + · have hne : t ≠ n := by + intro e; subst e + have h1 := hN _ hn; rw [hT _ ht] at h1; cases h1 + have := J.place.apart t n _ _ (hT t ht) (hN n hn) hne + simpa [ObjKindA.size] using this + +/-- At `JAtZA`, `TcbRel1.st` is `SchedRelA.tcb`'s state code. -/ +theorem NtfnStateRel.tcbSt (J : JAtZA (L := L) AR Z s a σ m) {t : U64} (ht : t ∈ Z.T) {x : Fl4SpecA64.Tcb} + (hk : σ.kheap (specPtr t) = some (.tcb x)) : (L.mem s (stA t)).val = tsCode x.tcbState := by + obtain ⟨y, hy, h1, -⟩ := J.sch.tcb t ht + rw [hk] at hy; cases hy + rw [tsCode_eqA]; exact h1 + +/-- At `JAtZA`, `TcbRel1.prio` is `SchedRestA.prio`. -/ +theorem NtfnStateRel.tcbPrio (J : JAtZA (L := L) AR Z s a σ m) {t : U64} (ht : t ∈ Z.T) {x : Fl4SpecA64.Tcb} + (hk : σ.kheap (specPtr t) = some (.tcb x)) : (L.mem s (prioA t)).val = x.tcbPriority.toNat := by + obtain ⟨y, hy, -, -, h⟩ := J.rest.prio t ht + rw [hk] at hy; cases hy + exact h + +end + +end + +end Fl4ProofsA64.P3.Notif diff --git a/proofs/Fl4ProofsA64/P3/NtfnQueueA.lean b/proofs/Fl4ProofsA64/P3/NtfnQueueA.lean new file mode 100644 index 000000000..b139bd3ab --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/NtfnQueueA.lean @@ -0,0 +1,183 @@ +/- +P3 (aarch64, A64-P3-IPC gen 1, ported from x86), notifications: the queue operations with their frames. + +P1 (QueueEp.lean) proves that `ntfn_dequeue` and `ntfn_append` refine +`List.erase` and the priority-ordered insertion on the queue. The +refinement proofs also need what they leave alone: here the same proofs, +with the frame added (`ntfn_dequeue_full`, `ntfn_append_full`). +-/ +import Fl4ProofsA64.P3.NtfnSendA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Notif +open fl4_core_a64 Fl4ProofsA64.P1 + +noncomputable section + +section +variable {S : Type _} {st : machine.Store S} [L : LawfulStore st] {N T : Set U64} + +/-- What writes to `Ep` links of TCBs in `W` leave alone. -/ +theorem framed_keeps (hP : NPlaced T N) {W : List U64} (hW : ∀ w ∈ W, w ∈ T) {s s1 : S} + (hf : Framed st .Ep W s s1) : + (∀ n ∈ N, NWordsEq (L.mem s) (L.mem s1) n) ∧ (∀ y ∈ T, TFieldsEq (L.mem s) (L.mem s1) y) ∧ + (∀ y ∈ T, y ∉ W → TLinksEq (L.mem s) (L.mem s1) y) ∧ WritesIn st s s1 (NWrite N T) := by + have hWa : ∀ w ∈ W, TcbAddr w := fun w hw => hP.tcb w (hW w hw) + have hn : ∀ n ∈ N, ∀ K : U64, K.val < 40 → K.val % 8 = 0 → + L.mem s1 (core.num.U64.wrapping_add n K) = L.mem s (core.num.U64.wrapping_add n K) := by + intro n hn K h1 h2 + refine hf.2.2 _ (nword hP hn h1 h2).1 fun w hw => ⟨?_, ?_⟩ + · rw [nextEp_eq] + exact (tn_ne (hWa w hw) (hP.ntfn n hn) (hP.disj n hn w (hW w hw)) (by simp) (by omega)).symm + · rw [prevEp_eq] + exact (tn_ne (hWa w hw) (hP.ntfn n hn) (hP.disj n hn w (hW w hw)) (by simp) (by omega)).symm + have ht : ∀ y ∈ T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → K ≠ 3496#u64 → K ≠ 3504#u64 → + L.mem s1 (core.num.U64.wrapping_add y K) = L.mem s (core.num.U64.wrapping_add y K) := by + intro y hy K h1 h2 h3 h4 + refine hf.2.2 _ (tword hP hy h1 h2) fun w hw => ⟨?_, ?_⟩ + · rw [nextEp_eq, Ne, tt_eq_iff (hP.tcb y hy) (hWa w hw) h1 (by simp)]; exact fun h => h3 h.2 + · rw [prevEp_eq, Ne, tt_eq_iff (hP.tcb y hy) (hWa w hw) h1 (by simp)]; exact fun h => h4 h.2 + refine ⟨fun n hn' => ⟨?_, ?_, ?_, ?_, ?_⟩, fun y hy => ⟨?_, ?_, ?_, ?_⟩, fun y hy hyW => ⟨?_, ?_⟩, ?_⟩ + · rw [epHeadA_eq]; exact hn n hn' _ (by simp) (by simp) + · rw [epTSA_eq]; exact hn n hn' _ (by simp) (by simp) + · rw [nBadgeA_eq]; exact hn n hn' _ (by simp) (by simp) + · rw [nBoundA_eq]; exact hn n hn' _ (by simp) (by simp) + · rw [nScA_eq]; exact hn n hn' _ (by simp) (by simp) + · rw [stA_eq]; exact ht y hy _ (by simp) (by simp) (by simp) (by simp) + · rw [tBlockA_eq]; exact ht y hy _ (by simp) (by simp) (by simp) (by simp) + · rw [badgeRegA_eq]; exact ht y hy _ (by simp) (by simp) (by simp) (by simp) + · rw [prioA_eq']; exact ht y hy _ (by simp) (by simp) (by simp) (by simp) + · exact hf.2.2.prev_eq hWa (hP.tcb y hy) (fun _ => hyW) + · exact hf.2.2.next_eq hWa (hP.tcb y hy) (fun _ => hyW) + · refine hf.writesIn.mono fun b ⟨w, hw, hb⟩ => Or.inr ⟨w, hW w hw, ?_⟩ + rcases hb with hb | hb + · exact Or.inr (Or.inr (Or.inr (Or.inl hb))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inl hb)))) + +/-- `set_queue` on the notification `n`: the two words written are its head +and tail/state words. -/ +theorem set_queue_keeps (hP : NPlaced T N) {n : U64} (hn : n ∈ N) (s : S) (h v : U64) : + let s' := L.wr (L.wr s (epHeadA n) h) (epTSA n) v + NWr st N T n s s' ∧ L.mem s' (nBadgeA n) = L.mem s (nBadgeA n) ∧ + L.mem s' (nBoundA n) = L.mem s (nBoundA n) ∧ L.mem s' (nScA n) = L.mem s (nScA n) ∧ + L.mem s' (epHeadA n) = h ∧ L.mem s' (epTSA n) = v := by + intro s' + have hna := hP.ntfn n hn + have h1 : NWr st N T n s (L.wr s (epHeadA n) h) := by rw [epHeadA_eq]; exact nwr_wr hP hn (by simp) (by simp) _ _ + have h2 : NWr st N T n (L.wr s (epHeadA n) h) s' := by + show NWr st N T n _ (L.wr _ (epTSA n) v); rw [epTSA_eq]; exact nwr_wr hP hn (by simp) (by simp) _ _ + have keep : ∀ K : U64, K.val < 40 → K.val % 8 = 0 → K ≠ 0#u64 → K ≠ 8#u64 → + L.mem s' (core.num.U64.wrapping_add n K) = L.mem s (core.num.U64.wrapping_add n K) := by + intro K h1 h2 h3 h4 + show L.mem (L.wr (L.wr s (epHeadA n) h) (epTSA n) v) _ = _ + rw [epTSA_eq, epHeadA_eq, + rd_wr_ne (nword hP hn (by simp) (by simp)).1 (nword hP hn h1 h2).1 (nwords_ne hna (by simp) (by omega) (Ne.symm h4)), + rd_wr_ne (nword hP hn (by simp) (by simp)).1 (nword hP hn h1 h2).1 (nwords_ne hna (by simp) (by omega) (Ne.symm h3))] + refine ⟨h1.trans h2, ?_, ?_, ?_, ?_, ?_⟩ + · rw [nBadgeA_eq]; exact keep _ (by simp) (by simp) (by simp) (by simp) + · rw [nBoundA_eq]; exact keep _ (by simp) (by simp) (by simp) (by simp) + · rw [nScA_eq]; exact keep _ (by simp) (by simp) (by simp) (by simp) + · show L.mem (L.wr (L.wr s (epHeadA n) h) (epTSA n) v) _ = _ + rw [epTSA_eq, epHeadA_eq, + rd_wr_ne (nword hP hn (by simp) (by simp)).1 (nword hP hn (by simp) (by simp)).1 + (nwords_ne hna (by simp) (by simp) (by simp)), rd_wr_eq (nword hP hn (by simp) (by simp)).1] + · exact rd_wr_eq (foot_ts hP hn).1 + +/-- Everything `ntfn_dequeue` and `ntfn_append` keep, for the TCBs `W` +whose links they write. -/ +structure QKeeps (st : machine.Store S) [L : LawfulStore st] (N T : Set U64) (n : U64) (W : List U64) + (s s' : S) : Prop where + ntfn : ∀ n' ∈ N, n' ≠ n → NWordsEq (L.mem s) (L.mem s') n' + badge : L.mem s' (nBadgeA n) = L.mem s (nBadgeA n) + bound : L.mem s' (nBoundA n) = L.mem s (nBoundA n) + sc : L.mem s' (nScA n) = L.mem s (nScA n) + tcb : ∀ y ∈ T, TFieldsEq (L.mem s) (L.mem s') y + links : ∀ y ∈ T, y ∉ W → TLinksEq (L.mem s) (L.mem s') y + foot : WritesIn st s s' (NWrite N T) + /-- Memory is unchanged but for `n`'s head and tail words and the `Ep` + links of `W`. -/ + agree : ∀ b, Aligned b → (∀ w ∈ W, b ≠ nextA .Ep w ∧ b ≠ prevA .Ep w) → b ≠ epHeadA n → b ≠ epTSA n → + L.mem s' b = L.mem s b + +theorem qkeeps_of (hP : NPlaced T N) {n : U64} (hn : n ∈ N) {W : List U64} (hW : ∀ w ∈ W, w ∈ T) {s s1 : S} + (hf : Framed st .Ep W s s1) (h v : U64) : + QKeeps st N T n W s (L.wr (L.wr s1 (epHeadA n) h) (epTSA n) v) := by + obtain ⟨f1, f2, f3, f4⟩ := framed_keeps hP hW hf + obtain ⟨g, g1, g2, g3, -, -⟩ := set_queue_keeps (st := st) hP hn s1 h v + exact ⟨fun n' hn' e => (f1 n' hn').trans (g.ntfn n' hn' e), g1.trans (f1 n hn).badge, + g2.trans (f1 n hn).bound, g3.trans (f1 n hn).sc, fun y hy => (f2 y hy).trans (g.tcb y hy).1, + fun y hy hyW => (f3 y hy hyW).trans (g.tcb y hy).2, + (f4.trans g.foot).mono fun _ h => by rcases h with h | h <;> exact h, + fun b hb hW' hh ht => by + rw [rd_wr_ne (foot_ts hP hn).1 hb (Ne.symm ht), rd_wr_ne (foot_head hP hn).1 hb (Ne.symm hh)] + exact hf.2.2 b hb hW'⟩ + +/-- `ntfn_dequeue` of a member, with its frame. -/ +theorem ntfn_dequeue_full (hP : NPlaced T N) (s : S) {n : U64} (hn : n ∈ N) (t : obj.Tcb) (xs : List U64) + (h : QueueRel (L.mem s) T .Ep (qOf (L.mem s) n) xs) (ht : t ∈ xs) : + notification.ntfn_dequeue st s n t ⦃ s' => + QueueRel (L.mem s') T .Ep (qOf (L.mem s') n) (xs.erase t) ∧ + stOf (L.mem s') n = (if xs.erase t = [] then 0#u64 else stOf (L.mem s) n) ∧ + QKeeps st N T n xs s s' ⦄ := by + have hE : EpPlaced T n := + ⟨by have := (hP.ntfn n hn).align; omega, by have := (hP.ntfn n hn).fits; omega, + fun t ht => by have := hP.disj n hn t ht; omega⟩ + have hT := hP.tcb + unfold notification.ntfn_dequeue + step* + subst q_post + refine WP.spec_bind (tcbqueue.remove.rel s .Ep _ t xs hT h ht) ?_ + rintro ⟨q1, s1⟩ h1 + simp only [WP.uncurry'_pair] at h1 + obtain ⟨hr1, hf, -, -⟩ := h1 + have hW : ∀ w ∈ xs, w ∈ T := h.mem + step* + · subst s'_post + have hemp : xs.erase t = [] := (hr1.empty_iff hT).1 (by simpa [b_post] using ‹b = true›) + obtain ⟨h1, h2⟩ := ep_set_queue (state := obj.NTFN_IDLE) hT hE hr1 + refine ⟨h1, ?_, qkeeps_of hP hn hW hf _ _⟩ + rw [← epState_eq, h2]; simp only [hemp, ↓reduceIte] + unfold obj.NTFN_IDLE + exact UScalar.eq_of_val_eq (by simp) + · subst s'_post + have hne : xs.erase t ≠ [] := by + rw [Ne, ← hr1.empty_iff hT]; simpa [b_post] using ‹¬b = true› + obtain ⟨h1, h2⟩ := ep_set_queue (state := st) hT hE hr1 + refine ⟨h1, ?_, qkeeps_of hP hn hW hf _ _⟩ + rw [← epState_eq, h2]; simp only [hne, ↓reduceIte]; rw [st_post] + apply eq_of_bv + simp only [LawfulStore.epState, UScalar.bv_and, BitVec.and_assoc, BitVec.and_self, stOf] + +/-- `ntfn_append`, with its frame. -/ +theorem ntfn_append_full (hP : NPlaced T N) (s : S) {n : U64} (hn : n ∈ N) (t : obj.Tcb) (xs : List U64) + (h : QueueRel (L.mem s) T .Ep (qOf (L.mem s) n) xs) (ht : t ∈ T) (hnt : t ∉ xs) : + notification.ntfn_append st s n t ⦃ s' => + QueueRel (L.mem s') T .Ep (qOf (L.mem s') n) + (insPrio (fun y => L.mem s (prioA y)) (L.mem s (prioA t)) t xs) ∧ + stOf (L.mem s') n = 1#u64 ∧ QKeeps st N T n (t :: xs) s s' ⦄ := by + have hE : EpPlaced T n := + ⟨by have := (hP.ntfn n hn).align; omega, by have := (hP.ntfn n hn).fits; omega, + fun t ht => by have := hP.disj n hn t ht; omega⟩ + have hT := hP.tcb + unfold notification.ntfn_append + step* + subst q_post + refine WP.spec_bind (tcbqueue.append_by_prio.rel s .Ep _ t xs hT h ht hnt) ?_ + rintro ⟨q1, s1⟩ h1 + simp only [WP.uncurry'_pair] at h1 + obtain ⟨hr1, hf⟩ := h1 + have hW : ∀ w ∈ t :: xs, w ∈ T := by + intro w hw; rcases List.mem_cons.1 hw with rfl | hw; exacts [ht, h.mem w hw] + step* + subst_vars + obtain ⟨h1, h2⟩ := ep_set_queue (state := obj.NTFN_WAITING) hT hE hr1 + refine ⟨h1, ?_, qkeeps_of hP hn hW hf _ _⟩ + rw [← epState_eq, h2] + unfold obj.NTFN_WAITING + decide + +end + +end + +end Fl4ProofsA64.P3.Notif diff --git a/proofs/Fl4ProofsA64/P3/NtfnRecvA.lean b/proofs/Fl4ProofsA64/P3/NtfnRecvA.lean new file mode 100644 index 000000000..2fc818472 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/NtfnRecvA.lean @@ -0,0 +1,205 @@ +/- +P3 (aarch64, A64-P3-IPC gen 1): **`receive_signal` polling refines `receive_signal`** of `Fl4SpecA64` (`Z = Unit`; +x86 `P3/NtfnRecv`'s hypothesis-free part, ported): + +* `receive_signal_poll`: polling (`blocking = false`) a notification that is not active writes 0 to the badge + register (l4v `do_nbrecv_failed_transfer`); single spec outcome, no hypothesis on other subsystems; +* the store-side pieces of the active case: `set_badge_reg_rel` (the badge register written), `set_idle_rel` (an + active notification made idle), and the spec's unfolding `receiveSignal_cap` / `receiveSignal_notActive`. + +The active receive (`maybe_donate_sched_context`, the sporadic unblock check), the blocking receive (`schedule_tcb`, +`ntfn_append`, `maybe_return_sched_context`) and `complete_signal` involve the scheduler / SC relation (x86 +`RecvHyps` over an abstract `SR`): gen 2, against `JAtZA`'s `SchedRelA` / `SchedRestA` instead of an abstract `SR`. + +The receive's cap is a notification cap to `n` (`Cap.notificationCap (specPtr n) _ _`). No named premise. +-/ +import Fl4ProofsA64.P3.NtfnQueueA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Notif +open fl4_core_a64 Fl4ProofsA64.P1 + +noncomputable section + +section SpecDefs +open Fl4SpecA64 + +/-- The spec's tail of `receive_signal` on an active notification (x86 `specRecvTail`). -/ +def specRecvTail (t n : ObjRef) : SMonad Unit Unit := do + maybeDonateSc t n + let scPtr ← getTcbObjRef Tcb.tcbSchedContext t + ifSporadicCurScTestRefillUnblockCheck scPtr + +theorem badgeOf_setReg (x : Tcb) (v : Word) : badgeOf (setReg x badge_register v) = v := by + simp [badgeOf, setReg, setRegUc, archTcbContextSet, archTcbContextGet] + +theorem setReg_state (x : Tcb) (r : Register) (v : Word) : (setReg x r v).tcbState = x.tcbState := rfl + +theorem setReg_prio (x : Tcb) (r : Register) (v : Word) : (setReg x r v).tcbPriority = x.tcbPriority := rfl + +end SpecDefs + +/-! ## Single writes -/ + +section +variable {S : Type _} {st : machine.Store S} [L : LawfulStore st] {N T : Set U64} + +/-- Writing the badge register of a TCB. -/ +theorem set_badge_reg_rel (hP : NPlaced T N) {s : S} {σ : Fl4SpecA64.State Unit} + (hR : NtfnStateRel N T (L.mem s) σ) {t : U64} (ht : t ∈ T) {x : Fl4SpecA64.Tcb} + (hk : σ.kheap (specPtr t) = some (.tcb x)) (v : U64) : + NtfnStateRel N T (L.mem (L.wr s (badgeRegA t) v)) (updK σ (specPtr t) (.tcb (setReg x Fl4SpecA64.badge_register v.bv))) ∧ + WritesIn st s (L.wr s (badgeRegA t) v) (NWrite N T) ∧ + NtfnStep σ (updK σ (specPtr t) (.tcb (setReg x Fl4SpecA64.badge_register v.bv))) := by + have hx := hR.at_tcb ht hk + obtain ⟨hw, hoth⟩ := twr_wr (st := st) hP ht (K := 256#u64) (Or.inr (Or.inr rfl)) s v + rw [← badgeRegA_eq] at hw hoth + refine ⟨hR.upd_tcb ht _ hw.ntfn hw.tcb hw.links (Or.inr ⟨?_, ?_⟩) ⟨?_, ?_, ?_, ?_⟩, hw.foot, + NtfnStep.tcb hk x.tcbState _⟩ + · rw [stA_eq, hoth _ (by simp) (by simp) (by simp)] + · rw [tBlockA_eq, hoth _ (by simp) (by simp) (by simp)] + · show (L.mem _ (stA t)).val = tsCode (setReg x _ _).tcbState + rw [setReg_state, stA_eq, hoth _ (by simp) (by simp) (by simp), ← stA_eq]; exact hx.st + · intro n' hn' + rw [setReg_state] at hn' + rw [tBlockA_eq, hoth _ (by simp) (by simp) (by simp), ← tBlockA_eq]; exact hx.blk n' hn' + · show (L.mem _ (badgeRegA t)).bv = badgeOf (setReg x _ _) + rw [badgeOf_setReg, rd_wr_eq (foot_breg hP ht).1] + · show (L.mem _ (prioA t)).val = (setReg x _ _).tcbPriority.toNat + rw [setReg_prio, prioA_eq', hoth _ (by simp) (by simp) (by simp), ← prioA_eq']; exact hx.prio + +theorem zero_and3' : (obj.NTFN_IDLE &&& 3#u64) = 0#u64 := by rw [ntfn_consts.1]; decide + +/-- Making an active notification idle (`set_state(NTFN_IDLE)`). -/ +theorem set_idle_rel (hP : NPlaced T N) {s : S} {σ : Fl4SpecA64.State Unit} + (hR : NtfnStateRel N T (L.mem s) σ) {n : U64} (hn : n ∈ N) {x : Fl4SpecA64.Notification} + (hk : σ.kheap (specPtr n) = some (.notification x)) {b0 : Fl4SpecA64.Badge} (hact : x.ntfnObj = .activeNtfn b0) : + let s' := L.wr s (epTSA n) ((L.mem s (epTSA n) &&& ~~~3#u64) ||| (obj.NTFN_IDLE &&& 3#u64)) + NtfnStateRel N T (L.mem s') (updK σ (specPtr n) (.notification (Fl4SpecA64.ntfnObjUpdate (fun _ => .idleNtfn) x))) ∧ + WritesIn st s s' (NWrite N T) ∧ + NtfnStep σ (updK σ (specPtr n) (.notification (Fl4SpecA64.ntfnObjUpdate (fun _ => .idleNtfn) x))) := by + intro s' + have hna := hP.ntfn n hn + have h : NWr st N T n s s' := by + show NWr st N T n s (L.wr s (epTSA n) _); rw [epTSA_eq]; exact nwr_wr hP hn (by simp) (by simp) _ _ + have hx := hR.at_ntfn hn hk + have hobj := hx.obj + rw [hact] at hobj + obtain ⟨-, hq, -⟩ := hobj + rw [queueRel_nil_iff] at hq + have ne_ts : ∀ K : U64, K.val < 40 → K.val % 8 = 0 → K ≠ 8#u64 → + L.mem s' (core.num.U64.wrapping_add n K) = L.mem s (core.num.U64.wrapping_add n K) := by + intro K hK hK8 hne + show L.mem (L.wr _ (epTSA n) _) _ = _ + rw [epTSA_eq] + exact rd_wr_ne (nword hP hn (by simp) (by simp)).1 (nword hP hn hK hK8).1 + (nwords_ne hna (by simp) (by omega) (Ne.symm hne)) + have e_head : L.mem s' (epHeadA n) = L.mem s (epHeadA n) := by rw [epHeadA_eq, ne_ts _ (by simp) (by simp) (by simp)] + have e_ts : L.mem s' (epTSA n) = (L.mem s (epTSA n) &&& ~~~3#u64) ||| (obj.NTFN_IDLE &&& 3#u64) := + rd_wr_eq (foot_ts hP hn).1 + have e_bound : L.mem s' (nBoundA n) = L.mem s (nBoundA n) := by + rw [nBoundA_eq, ne_ts _ (by simp) (by simp) (by simp)] + have e_sc : L.mem s' (nScA n) = L.mem s (nScA n) := by rw [nScA_eq, ne_ts _ (by simp) (by simp) (by simp)] + refine ⟨hR.upd_ntfn hn _ h.ntfn (fun y hy => (h.tcb y hy).1) (fun y hy _ => (h.tcb y hy).2) ⟨?_, ?_, ?_, ?_⟩, + h.foot, NtfnStep.ntfn hk rfl rfl⟩ + · show NtfnObjRel _ T n .idleNtfn + refine ⟨?_, ?_⟩ + · rw [stOf, e_ts, ts_state]; exact zero_and3' + · rw [queueRel_nil_iff] + refine ⟨?_, ?_⟩ + · show L.mem s' (epHeadA n) = 0#u64 + rw [e_head]; exact hq.1 + · show L.mem s' (epTSA n) &&& ~~~3#u64 = 0#u64 + rw [e_ts, ts_end]; exact hq.2 + · show x.ntfnBoundTcb = _; rw [e_bound]; exact hx.bound + · rw [e_bound]; exact hx.bound_mem + · show x.ntfnSc = _; rw [e_sc]; exact hx.sc + +end + +/-! ## `receive_signal` -/ + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] {N T : Set U64} + +/-- The spec's `receive_signal` on a notification cap to `p`, unfolded to the +notification. -/ +theorem receiveSignal_cap {σ : Fl4SpecA64.State Unit} {p t : Fl4SpecA64.ObjRef} {x : Fl4SpecA64.Notification} + (hk : σ.kheap p = some (.notification x)) (b : Fl4SpecA64.Badge) (r : Fl4SpecA64.CapRights) (blk : Bool) : + Fl4SpecA64.receiveSignal (Z := Unit) t (.notificationCap p b r) blk σ = + (match x.ntfnObj with + | .activeNtfn badge => do + Fl4SpecA64.asUser t <| Fl4SpecA64.setRegister Fl4SpecA64.badge_register badge + Fl4SpecA64.setNotification p <| Fl4SpecA64.ntfnObjUpdate (fun _ => .idleNtfn) x + specRecvTail t p + | _ => Fl4SpecA64.receiveSignalBlocked t p blk) σ := by + unfold Fl4SpecA64.receiveSignal + dsimp only + rw [nd_pure_bind', nd_bind_one (getNotification_eq hk)] + rfl + +/-- `receive_signal` on a notification that is not active is +`receive_signal_blocked`. -/ +theorem receiveSignal_notActive {σ : Fl4SpecA64.State Unit} {p t : Fl4SpecA64.ObjRef} {x : Fl4SpecA64.Notification} + (hk : σ.kheap p = some (.notification x)) (hna : ∀ b, x.ntfnObj ≠ .activeNtfn b) (b : Fl4SpecA64.Badge) + (r : Fl4SpecA64.CapRights) (blk : Bool) : + Fl4SpecA64.receiveSignal (Z := Unit) t (.notificationCap p b r) blk σ = Fl4SpecA64.receiveSignalBlocked t p blk σ := by + rw [receiveSignal_cap hk] + revert hna + cases x.ntfnObj with + | activeNtfn b' => intro h; exact absurd rfl (h b') + | _ => intro _; rfl + +/-- **Polling** a notification that is not active: the badge register gets +0. -/ +theorem receive_signal_poll (hP : NPlaced T N) (s : S) (a : A) (σ : Fl4SpecA64.State Unit) + (hR : NtfnStateRel N T (L.mem s) σ) {t : U64} (ht : t ∈ T) {n : U64} (hn : n ∈ N) + {x : Fl4SpecA64.Notification} (hk : σ.kheap (specPtr n) = some (.notification x)) + (hna : ∀ b, x.ntfnObj ≠ .activeNtfn b) (cb : Fl4SpecA64.Badge) (cr : Fl4SpecA64.CapRights) : + notification.receive_signal st ar s a t n false ⦃ r => + r.2 = a ∧ SingleRel (st := st) N T s σ (Fl4SpecA64.receiveSignal (specPtr t) (.notificationCap (specPtr n) cb cr) false σ) r.1 ⦄ := by + have hx := hR.at_ntfn hn hk + obtain ⟨xt, hkt, -⟩ := hR.tcb t ht + have hspec : Fl4SpecA64.receiveSignal (Z := Unit) (specPtr t) (.notificationCap (specPtr n) cb cr) false σ = + ({((), updK σ (specPtr t) (.tcb (setReg xt Fl4SpecA64.badge_register 0)))}, False) := by + rw [receiveSignal_cap hk] + have : (match x.ntfnObj with + | .activeNtfn badge => (do + Fl4SpecA64.asUser (specPtr t) <| Fl4SpecA64.setRegister Fl4SpecA64.badge_register badge + Fl4SpecA64.setNotification (specPtr n) <| Fl4SpecA64.ntfnObjUpdate (fun _ => .idleNtfn) x + specRecvTail (specPtr t) (specPtr n) : Fl4SpecA64.SMonad Unit Unit) + | _ => Fl4SpecA64.receiveSignalBlocked (specPtr t) (specPtr n) false) = Fl4SpecA64.receiveSignalBlocked (specPtr t) (specPtr n) false := by + cases h : x.ntfnObj with + | activeNtfn b => exact absurd h (hna b) + | _ => rfl + rw [this] + unfold Fl4SpecA64.receiveSignalBlocked Fl4SpecA64.doNbrecvFailedTransfer + simp only [Bool.false_eq_true, ↓reduceIte] + rw [nd_bind_one (asUser_setRegister_eq _ _ hkt)] + rfl + have hst : stOf (L.mem s) n = obj.NTFN_IDLE ∨ stOf (L.mem s) n = obj.NTFN_WAITING := by + have := hx.obj + cases h : x.ntfnObj with + | idleNtfn => exact Or.inl (stOf_idle (T := T) (h ▸ this) rfl) + | waitingNtfn q => exact Or.inr (stOf_waiting (T := T) (h ▸ this) rfl) + | activeNtfn b => exact absurd h (hna b) + obtain ⟨hR', hW, hS⟩ := set_badge_reg_rel hP hR ht hkt 0#u64 + unfold notification.receive_signal + step + rw [st_post, epState_eq] at * + rcases hst with hst | hst + · simp only [hst, ↓reduceIte, Bool.false_eq_true] + step + subst s1_post + exact ⟨_, hspec, hR', hS, hW⟩ + · simp only [hst, ntfn_consts.1, ntfn_consts.2.1, show (1#u64 : U64) ≠ 0#u64 by decide, ↓reduceIte, + Bool.false_eq_true] + step + subst s1_post + exact ⟨_, hspec, hR', hS, hW⟩ + +end + +end + +end Fl4ProofsA64.P3.Notif diff --git a/proofs/Fl4ProofsA64/P3/NtfnRelA.lean b/proofs/Fl4ProofsA64/P3/NtfnRelA.lean new file mode 100644 index 000000000..bfe2c32d1 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/NtfnRelA.lean @@ -0,0 +1,583 @@ +/- +P3 (aarch64, A64-P3-IPC gen 1): **the A64 notification relation `NtfnStateRel`** (x86 `P3/NtfnRel`, ported). + +FL4's AArch64 notification (`arch/aarch64/layout.rs` `ntfn`, 64 bytes) has the x86 layout: the queue head, the +queue end with the state (`NTFN_IDLE` 0, `NTFN_WAITING` 1, `NTFN_ACTIVE` 2) in bits 0-1, the pending badge, the +bound TCB (0 for none) and the SC (0 for none) at +0 / +8 / +16 / +24 / +32. The spec's (`Fl4SpecA64`, l4v +Structures_A `notification`) is `ntfn_obj` (`IdleNtfn | WaitingNtfn queue | ActiveNtfn badge`), `ntfn_bound_tcb` +and `ntfn_sc`. + +**Naming (the only change from x86 beyond the renaming).** Every object is named in the spec by **`specPtr p`** +(`P3/PtrA`, `@[irreducible]`, A64-P3 S3 gen 12), not `p.bv`: the notification at `n` is `σ.kheap (specPtr n)`, a +waiting queue is `xs.map specPtr`, a blocked TCB's block-object word names `specPtr (m (tBlockA t))`, a nonzero +bound-TCB / SC word `w` is `some (specPtr w)` (`optRef`, the same as JointA's `optRefA`). Badges are data and stay +`.bv`. The TCB words are A64 P1's (`stA` = `tcb::STATE` +3328, the x86 offsets; `BLOCK_OBJ` +3336, `PRIO` +3448, +`EP_NEXT` / `EP_PREV` +3496 / +3504); the badge register is `reg::BADGE = X0` (l4v AArch64 `badge_register = X0`), +`tcb::REGS + 8 * 0` = +256, as on x86 (RDI is register 0 there too). `tsCode` is JointA's `tsCodeA` (FL4's `TS_*`, +`ext runningVM` = 8); it is restated here so that this module does not import `JointA` (so that `JAtZA` can later +import it: `P3/NtfnJoinA` proves the two agree). + +Ghost state: `T`, the TCB addresses, and `N`, the notification addresses. `NPlaced T N`: TCBs are placed (A64 P1 +`TcbPlaced`), notifications are 64-byte aligned and do not wrap, and do not overlap a TCB. + +The relation (`NtfnStateRel N T m σ`, on memory `m`): + +* every `n ∈ N` is a notification `x` at `specPtr n` with + - `NtfnObjRel`: idle ↔ state 0 and the queue (A64 P1 `QueueRel`, `Ep` links) is empty; active `b` ↔ state 2, + empty queue, badge word `b`; waiting `q` ↔ state 1 and the queue holds TCBs `xs ≠ []` with `q = xs.map + specPtr`, each in thread state `TS_BLOCKED_ON_NTFN` blocked on `n` (l4v `valid_ntfn`); + - `ntfn_bound_tcb` and `ntfn_sc` are the bound-TCB and SC words (`optRef`), and a nonzero bound TCB is in `T`; +* every `t ∈ T` is a TCB `x` at `specPtr t` with (`TcbRel1`): the state word codes `tcb_state` (`tsCode`), for + `BlockedOnNotification n` the block-object word names `n`, the badge register is the spec's `badge_register`, the + priority word is `tcb_priority`. + +**How it joins `JAtZA`** (not done here: `JointA` is S3's file): as a field `ntfn : NtfnStateRel Z.N Z.T (L.mem s) σ` +with a new ghost `N` in `XSetsA` (or `N := {p | Z.th p = some .ntfn}`), and `NPlaced Z.T Z.N` from `PlaceA` (TCBs +4 KiB aligned) plus the typed heap's notification alignment. `TcbRel1.st` / `.prio` then duplicate `SchedRelA.tcb` / +`SchedRestA.prio` (same words, `tsCode = tsCodeA`), `blk` / `badge` are new. + +The relation reads memory only in the footprint `NFoot N T` and the spec's heap only through `KView`, so it is kept +by any change outside them (`NtfnStateRel.frame`). + +No named premise. No `Fl4SpecA64` edit. +-/ +import Fl4ProofsA64.P1.QueueCallers +import Fl4ProofsA64.P3.PtrA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Notif +open fl4_core_a64 Fl4ProofsA64.P1 + +noncomputable section + + +/-! ## TCB words (the state word is A64 P1's `stA`) -/ + +/-- The block-object word of the TCB at `t` (layout.rs `tcb::BLOCK_OBJ`). -/ +def tBlockA (t : U64) : U64 := core.num.U64.wrapping_add t 3336#u64 + +theorem tsVals : sched.TS_RUNNING = 1#u64 ∧ sched.TS_RESTART = 2#u64 ∧ sched.TS_BLOCKED_ON_RECV = 3#u64 ∧ + sched.TS_BLOCKED_ON_SEND = 4#u64 ∧ sched.TS_BLOCKED_ON_NTFN = 6#u64 := by + unfold sched.TS_RUNNING sched.TS_RESTART sched.TS_BLOCKED_ON_RECV sched.TS_BLOCKED_ON_SEND sched.TS_BLOCKED_ON_NTFN + exact ⟨rfl, rfl, rfl, rfl, rfl⟩ + +theorem tfield_val {y K : U64} (hy : TcbAddr y) (hK : K.val < 4096) : + (core.num.U64.wrapping_add y K).val = y.val + K.val := by + have := hy.fits + simp [core.num.U64.wrapping_add_val_eq, U64.size_def, U64.numBits]; omega + +theorem tfield_ne {x y K1 K2 : U64} (hx : TcbAddr x) (hy : TcbAddr y) (h1 : K1.val < 4096) (h2 : K2.val < 4096) + (hne : x = y → K1.val ≠ K2.val) : core.num.U64.wrapping_add x K1 ≠ core.num.U64.wrapping_add y K2 := by + intro e + have hv := congrArg UScalar.val e + rw [tfield_val hx h1, tfield_val hy h2] at hv + have := hx.align; have := hy.align + by_cases hxy : x = y + · subst hxy; exact hne rfl (by omega) + · have : x.val ≠ y.val := fun e' => hxy (UScalar.eq_of_val_eq e') + omega + +/-! ## Word addresses -/ + +def nBadgeA (n : U64) : U64 := core.num.U64.wrapping_add n layout.ntfn.BADGE +def nBoundA (n : U64) : U64 := core.num.U64.wrapping_add n layout.ntfn.BOUND_TCB +def nScA (n : U64) : U64 := core.num.U64.wrapping_add n layout.ntfn.SC +/-- The badge register (`reg::BADGE` = X0) of the TCB at `t`. -/ +def badgeRegA (t : U64) : U64 := + core.num.U64.wrapping_add (core.num.U64.wrapping_add t layout.tcb.REGS) + (core.num.U64.wrapping_mul layout.reg.BADGE 8#u64) + +theorem nBadgeA_eq (n : U64) : nBadgeA n = core.num.U64.wrapping_add n 16#u64 := by + unfold nBadgeA layout.ntfn.BADGE; rfl +theorem nBoundA_eq (n : U64) : nBoundA n = core.num.U64.wrapping_add n 24#u64 := by + unfold nBoundA layout.ntfn.BOUND_TCB; rfl +theorem nScA_eq (n : U64) : nScA n = core.num.U64.wrapping_add n 32#u64 := by + unfold nScA layout.ntfn.SC; rfl +theorem epHeadA_eq (n : U64) : epHeadA n = core.num.U64.wrapping_add n 0#u64 := by + unfold epHeadA layout.ep.HEAD; rfl +theorem epTSA_eq (n : U64) : epTSA n = core.num.U64.wrapping_add n 8#u64 := by + unfold epTSA layout.ep.TAIL_STATE; rfl +theorem badgeRegA_eq (t : U64) : badgeRegA t = core.num.U64.wrapping_add t 256#u64 := by + unfold badgeRegA layout.tcb.REGS layout.reg.BADGE layout.reg.X0 + apply UScalar.eq_of_val_eq + simp [core.num.U64.wrapping_add_val_eq, core.num.U64.wrapping_mul_val_eq] + +/-! ## Store specs of the field accessors -/ + +section +variable {S : Type _} {st : machine.Store S} [L : LawfulStore st] + +@[step] theorem obj.Ntfn.badge.spec (s : S) (n : obj.Ntfn) : + obj.Ntfn.badge st n s ⦃ v => v = L.mem s (nBadgeA n) ⦄ := by + unfold obj.Ntfn.badge; step*; simp_all [nBadgeA] +@[step] theorem obj.Ntfn.set_badge.spec (s : S) (n : obj.Ntfn) (v : U64) : + obj.Ntfn.set_badge st n s v ⦃ s' => s' = L.wr s (nBadgeA n) v ⦄ := by + unfold obj.Ntfn.set_badge; step*; simp_all [nBadgeA] +@[step] theorem obj.Ntfn.bound_tcb.spec (s : S) (n : obj.Ntfn) : + obj.Ntfn.bound_tcb st n s ⦃ v => v = L.mem s (nBoundA n) ⦄ := by + unfold obj.Ntfn.bound_tcb; step*; simp_all [nBoundA] +@[step] theorem obj.Ntfn.sc.spec (s : S) (n : obj.Ntfn) : + obj.Ntfn.sc st n s ⦃ v => v = L.mem s (nScA n) ⦄ := by + unfold obj.Ntfn.sc; step*; simp_all [nScA] +@[step] theorem obj.Ntfn.set_state.spec (s : S) (n : obj.Ntfn) (v : U64) : + obj.Ntfn.set_state st n s v ⦃ s' => + s' = L.wr s (epTSA n) ((L.mem s (epTSA n) &&& ~~~3#u64) ||| (v &&& 3#u64)) ⦄ := by + unfold obj.Ntfn.set_state obj.Ntfn.tail obj.Ntfn.tail_state obj.Ntfn.set_tail_state + step* + subst_vars + have e : i1 = (L.mem s (core.num.U64.wrapping_add n layout.ntfn.TAIL_STATE) &&& ~~~3#u64) ||| (v &&& 3#u64) := + eq_of_bv (by simp only [i1_post1, i_post1, UScalar.bv_or, UScalar.bv_and]) + simp only [epTSA, ← ntfn_ts_eq, e] +@[step] theorem notification.set_active.spec (s : S) (n : obj.Ntfn) (b : U64) : + notification.set_active st s n b ⦃ s' => s' = L.wr (L.wr s (epTSA n) + ((L.mem s (epTSA n) &&& ~~~3#u64) ||| (obj.NTFN_ACTIVE &&& 3#u64))) (nBadgeA n) b ⦄ := by + unfold notification.set_active; step* +@[step] theorem obj.Tcb.set_reg_badge.spec (s : S) (t : obj.Tcb) (v : U64) : + obj.Tcb.set_reg st t s layout.reg.BADGE v ⦃ s' => s' = L.wr s (badgeRegA t) v ⦄ := by + unfold obj.Tcb.set_reg; step*; simp_all [badgeRegA] +@[step] theorem obj.Tcb.set_block_obj.spec (s : S) (t : obj.Tcb) (v : U64) : + obj.Tcb.set_block_obj st t s v ⦃ s' => s' = L.wr s (tBlockA t) v ⦄ := by + unfold obj.Tcb.set_block_obj; step*; simp_all [tBlockA] + +end + +end + +/-! ## Placement -/ + +/-- A notification address: 64-byte aligned, and the object does not wrap. -/ +structure NAddr (n : U64) : Prop where + align : n.val % 64 = 0 + fits : n.val + 64 ≤ 2 ^ 64 + +/-- Hypothesis on the ghost sets: TCBs are placed (P1 `TcbPlaced`), +notifications are placed and do not overlap a TCB. -/ +structure NPlaced (T N : Set U64) : Prop where + tcb : TcbPlaced T + ntfn : ∀ n ∈ N, NAddr n + disj : ∀ n ∈ N, ∀ t ∈ T, t.val + 4096 ≤ n.val ∨ n.val + 64 ≤ t.val + +theorem nfield_val {n K : U64} (hn : NAddr n) (hK : K.val < 64) : + (core.num.U64.wrapping_add n K).val = n.val + K.val := by + have := hn.fits + simp [core.num.U64.wrapping_add_val_eq, U64.size_def, U64.numBits]; omega + +theorem nfield_aligned {n K : U64} (hn : NAddr n) (hK : K.val < 64) (hK8 : K.val % 8 = 0) : + Aligned (core.num.U64.wrapping_add n K) := by + have := hn.align + unfold Aligned; rw [nfield_val hn hK]; omega + +theorem tt_eq_iff {x y K1 K2 : U64} (hx : TcbAddr x) (hy : TcbAddr y) (h1 : K1.val < 4096) + (h2 : K2.val < 4096) : + core.num.U64.wrapping_add x K1 = core.num.U64.wrapping_add y K2 ↔ x = y ∧ K1 = K2 := by + constructor + · intro e + by_cases hxy : x = y + · subst hxy + refine ⟨rfl, UScalar.eq_of_val_eq ?_⟩ + have := congrArg UScalar.val e + rw [tfield_val hx h1, tfield_val hx h2] at this; omega + · exact absurd e (tfield_ne hx hy h1 h2 (fun h => absurd h hxy)) + · rintro ⟨rfl, rfl⟩; rfl + +theorem nn_eq_iff {x y K1 K2 : U64} (hx : NAddr x) (hy : NAddr y) (h1 : K1.val < 64) (h2 : K2.val < 64) : + core.num.U64.wrapping_add x K1 = core.num.U64.wrapping_add y K2 ↔ x = y ∧ K1 = K2 := by + constructor + · intro e + have := congrArg UScalar.val e + rw [nfield_val hx h1, nfield_val hy h2] at this + have := hx.align; have := hy.align + have hxy : x.val = y.val := by omega + exact ⟨UScalar.eq_of_val_eq hxy, UScalar.eq_of_val_eq (by omega)⟩ + · rintro ⟨rfl, rfl⟩; rfl + +theorem tn_ne {t n K1 K2 : U64} (ht : TcbAddr t) (hn : NAddr n) (hd : t.val + 4096 ≤ n.val ∨ n.val + 64 ≤ t.val) + (h1 : K1.val < 4096) (h2 : K2.val < 64) : + core.num.U64.wrapping_add t K1 ≠ core.num.U64.wrapping_add n K2 := by + intro e + have := congrArg UScalar.val e + rw [tfield_val ht h1, nfield_val hn h2] at this; omega + +/-- The named addresses as offsets (for `simp`). -/ +theorem tBlockA_eq (t : U64) : tBlockA t = core.num.U64.wrapping_add t 3336#u64 := rfl +theorem prioA_eq' (t : U64) : prioA t = core.num.U64.wrapping_add t 3448#u64 := rfl +theorem nextEp_eq (t : U64) : nextA .Ep t = core.num.U64.wrapping_add t 3496#u64 := rfl +theorem prevEp_eq (t : U64) : prevA .Ep t = core.num.U64.wrapping_add t 3504#u64 := rfl + +/-! ## The relation -/ + +section Rel +open Fl4SpecA64 + +/-- The queue of the notification at `n` (`notification::get_queue`). -/ +def qOf (m : U64 → U64) (n : U64) : tcbqueue.Queue := ⟨m (epHeadA n), m (epTSA n) &&& ~~~3#u64⟩ +/-- The state bits of the notification at `n` (`Ntfn::state`). -/ +def stOf (m : U64 → U64) (n : U64) : U64 := m (epTSA n) &&& 3#u64 + +theorem epQ_eq {S : Type _} {st : machine.Store S} [L : LawfulStore st] (s : S) (n : U64) : + L.epQ s n = qOf (L.mem s) n := rfl +theorem epState_eq {S : Type _} {st : machine.Store S} [L : LawfulStore st] (s : S) (n : U64) : + L.epState s n = stOf (L.mem s) n := rfl + +/-- A pointer word as an optional `obj_ref` (0 is none), named by `specPtr` (JointA's `optRefA`). -/ +def optRef (w : U64) : Option ObjRef := if w = 0#u64 then none else some (specPtr w) + +/-- FL4's thread-state word for a spec thread state (sched.rs `TS_*`; JointA's `tsCodeA`). -/ +def tsCode : ThreadState → Nat + | .inactive => 0 + | .running => 1 + | .restart => 2 + | .blockedOnReceive _ _ _ => 3 + | .blockedOnSend _ _ => 4 + | .blockedOnReply _ => 5 + | .blockedOnNotification _ => 6 + | .idleThreadState => 7 + | .ext .runningVM => 8 + +/-- The badge register of a spec TCB's saved user context. -/ +def badgeOf (x : Tcb) : Word := x.tcbArch.tcbContext.userRegs badge_register + +/-- The TCB at `t` in memory `m` and the spec's TCB `x`: state, the +notification it is blocked on, badge register, priority. -/ +structure TcbRel1 (m : U64 → U64) (t : U64) (x : Tcb) : Prop where + st : (m (stA t)).val = tsCode x.tcbState + blk : ∀ n, x.tcbState = .blockedOnNotification n → specPtr (m (tBlockA t)) = n + badge : (m (badgeRegA t)).bv = badgeOf x + prio : (m (prioA t)).val = x.tcbPriority.toNat + +/-- A TCB blocked on the notification at `n` (state word and block object). -/ +def BlockedOn (m : U64 → U64) (n y : U64) : Prop := (m (stA y)).val = 6 ∧ m (tBlockA y) = n + +/-- `ntfn_obj` of the notification at `n`. -/ +def NtfnObjRel (m : U64 → U64) (T : Set U64) (n : U64) : Ntfn → Prop + | .idleNtfn => stOf m n = 0#u64 ∧ QueueRel m T .Ep (qOf m n) [] + | .activeNtfn b => stOf m n = 2#u64 ∧ QueueRel m T .Ep (qOf m n) [] ∧ (m (nBadgeA n)).bv = b + | .waitingNtfn q => stOf m n = 1#u64 ∧ ∃ xs : List U64, xs ≠ [] ∧ q = xs.map specPtr ∧ + QueueRel m T .Ep (qOf m n) xs ∧ ∀ y ∈ xs, BlockedOn m n y + +/-- The notification at `n` in memory `m` and the spec's notification `x`. -/ +structure NtfnRel1 (m : U64 → U64) (T : Set U64) (n : U64) (x : Notification) : Prop where + obj : NtfnObjRel m T n x.ntfnObj + bound : x.ntfnBoundTcb = optRef (m (nBoundA n)) + bound_mem : m (nBoundA n) ≠ 0#u64 → m (nBoundA n) ∈ T + sc : x.ntfnSc = optRef (m (nScA n)) + +/-- The notification part of the state relation. -/ +structure NtfnStateRel {Z : Type} (N T : Set U64) (m : U64 → U64) (σ : State Z) : Prop where + ntfn : ∀ n ∈ N, ∃ x, σ.kheap (specPtr n) = some (.notification x) ∧ NtfnRel1 m T n x + tcb : ∀ t ∈ T, ∃ x, σ.kheap (specPtr t) = some (.tcb x) ∧ TcbRel1 m t x + +/-- The words the relation reads. -/ +def NFoot (N T : Set U64) (a : U64) : Prop := + (∃ n ∈ N, n.val ≤ a.val ∧ a.val < n.val + 40) ∨ + (∃ t ∈ T, a = stA t ∨ a = tBlockA t ∨ a = badgeRegA t ∨ a = prioA t ∨ a = nextA .Ep t ∨ a = prevA .Ep t) + +/-- The words the notification code writes: the footprint without the +priorities (which the scheduler relation may read); (gen 6, ruling (b)) and +the fault-type word (`cancel_ipc`'s `clear_fault` before `cancel_signal`; EpRelA's +`tFaultA`, offset 3376, outside `NFoot`). -/ +def NWrite (N T : Set U64) (a : U64) : Prop := + (∃ n ∈ N, n.val ≤ a.val ∧ a.val < n.val + 40) ∨ + (∃ t ∈ T, a = stA t ∨ a = tBlockA t ∨ a = badgeRegA t ∨ a = nextA .Ep t ∨ a = prevA .Ep t ∨ + a = core.num.U64.wrapping_add t 3376#u64) + +/-- (gen 6) A written word is in the footprint or a fault-type word. -/ +theorem NWrite.foot {N T : Set U64} {a : U64} : + NWrite N T a → NFoot N T a ∨ ∃ t ∈ T, a = core.num.U64.wrapping_add t 3376#u64 := by + rintro (h | ⟨t, ht, h | h | h | h | h | h⟩) + · exact Or.inl (Or.inl h) + · exact Or.inl (Or.inr ⟨t, ht, Or.inl h⟩) + · exact Or.inl (Or.inr ⟨t, ht, Or.inr (Or.inl h)⟩) + · exact Or.inl (Or.inr ⟨t, ht, Or.inr (Or.inr (Or.inl h))⟩) + · exact Or.inl (Or.inr ⟨t, ht, Or.inr (Or.inr (Or.inr (Or.inr (Or.inl h))))⟩) + · exact Or.inl (Or.inr ⟨t, ht, Or.inr (Or.inr (Or.inr (Or.inr (Or.inr h))))⟩) + · exact Or.inr ⟨t, ht, h⟩ + +/-- What the relation reads of the spec's heap: notifications, and TCBs' +state, badge register and priority. -/ +def KView {Z : Type} (σ σ' : State Z) : Prop := + (∀ p x, σ.kheap p = some (.notification x) → σ'.kheap p = some (.notification x)) ∧ + (∀ p x, σ.kheap p = some (.tcb x) → ∃ y, σ'.kheap p = some (.tcb y) ∧ + y.tcbState = x.tcbState ∧ badgeOf y = badgeOf x ∧ y.tcbPriority = x.tcbPriority) + +/-- What the notification code changes in the spec's state: the `ntfn_obj` +of notifications and the state, user context and (gen 6, ruling (b): +`cancel_ipc`'s fault clear) fault of TCBs; nothing else. -/ +def NtfnStep {Z : Type} (σ σ' : State Z) : Prop := + σ'.exst = σ.exst ∧ { σ'.toAbstractState with kheap := σ.kheap } = σ.toAbstractState ∧ + ∀ p, σ'.kheap p = σ.kheap p ∨ + (∃ x y, σ.kheap p = some (.notification x) ∧ σ'.kheap p = some (.notification y) ∧ + y.ntfnBoundTcb = x.ntfnBoundTcb ∧ y.ntfnSc = x.ntfnSc) ∨ + (∃ x y, σ.kheap p = some (.tcb x) ∧ σ'.kheap p = some (.tcb y) ∧ + y = { x with tcbState := y.tcbState, tcbArch := y.tcbArch, tcbFault := y.tcbFault }) + +end Rel + +/-! ## Frames -/ + +section Frame +open Fl4SpecA64 +variable {Z : Type} + +/-- The five words of the notification at `n` agree. -/ +structure NWordsEq (m m' : U64 → U64) (n : U64) : Prop where + head : m' (epHeadA n) = m (epHeadA n) + ts : m' (epTSA n) = m (epTSA n) + badge : m' (nBadgeA n) = m (nBadgeA n) + bound : m' (nBoundA n) = m (nBoundA n) + sc : m' (nScA n) = m (nScA n) + +/-- The related fields of the TCB at `t` agree. -/ +structure TFieldsEq (m m' : U64 → U64) (t : U64) : Prop where + st : m' (stA t) = m (stA t) + blk : m' (tBlockA t) = m (tBlockA t) + badge : m' (badgeRegA t) = m (badgeRegA t) + prio : m' (prioA t) = m (prioA t) + +/-- The `Ep` links of the TCB at `t` agree. -/ +def TLinksEq (m m' : U64 → U64) (t : U64) : Prop := + m' (prevA .Ep t) = m (prevA .Ep t) ∧ m' (nextA .Ep t) = m (nextA .Ep t) + +theorem queueRel_congr_links {m m' : U64 → U64} {T : Set U64} {q xs} (h : QueueRel m T .Ep q xs) + (hl : ∀ y ∈ xs, TLinksEq m m' y) : QueueRel m' T .Ep q xs := + ⟨h.nodup, h.mem, h.head, h.last, h.seg.congr hl⟩ + +theorem qOf_congr {m m' : U64 → U64} {n : U64} (h : NWordsEq m m' n) : qOf m' n = qOf m n := by + simp only [qOf, h.head, h.ts] +theorem stOf_congr {m m' : U64 → U64} {n : U64} (h : NWordsEq m m' n) : stOf m' n = stOf m n := by + simp only [stOf, h.ts] + +theorem TcbRel1.frame {m m' : U64 → U64} {t : U64} {x : Tcb} (h : TcbRel1 m t x) (he : TFieldsEq m m' t) : + TcbRel1 m' t x := + ⟨he.st ▸ h.st, fun n hn => he.blk ▸ h.blk n hn, he.badge ▸ h.badge, he.prio ▸ h.prio⟩ + +/-- What a notification needs of a member of its queue: state, block object +and links. -/ +def MemberEq (m m' : U64 → U64) (y : U64) : Prop := + m' (stA y) = m (stA y) ∧ m' (tBlockA y) = m (tBlockA y) ∧ TLinksEq m m' y + +theorem NtfnObjRel.frame {m m' : U64 → U64} {T : Set U64} {n : U64} {o : Ntfn} (h : NtfnObjRel m T n o) + (hn : NWordsEq m m' n) + (hy : ∀ y ∈ T, specPtr y ∈ o.ntfnQueue → MemberEq m m' y) : NtfnObjRel m' T n o := by + cases o with + | idleNtfn => + obtain ⟨h1, h2⟩ := h + exact ⟨stOf_congr hn ▸ h1, qOf_congr hn ▸ queueRel_congr_links h2 (by simp)⟩ + | activeNtfn b => + obtain ⟨h1, h2, h3⟩ := h + exact ⟨stOf_congr hn ▸ h1, qOf_congr hn ▸ queueRel_congr_links h2 (by simp), hn.badge ▸ h3⟩ + | waitingNtfn q => + obtain ⟨h1, xs, hne, rfl, h2, h3⟩ := h + have hy' : ∀ y ∈ xs, MemberEq m m' y := fun y hyx => + hy y (h2.mem y hyx) (by simp only [Ntfn.ntfnQueue]; exact List.mem_map_of_mem hyx) + refine ⟨stOf_congr hn ▸ h1, xs, hne, rfl, qOf_congr hn ▸ queueRel_congr_links h2 (fun y hyx => (hy' y hyx).2.2), + fun y hyx => ?_⟩ + obtain ⟨e1, e2⟩ := h3 y hyx + exact ⟨(hy' y hyx).1 ▸ e1, (hy' y hyx).2.1 ▸ e2⟩ + +theorem NtfnRel1.frame {m m' : U64 → U64} {T : Set U64} {n : U64} {x : Notification} (h : NtfnRel1 m T n x) + (hn : NWordsEq m m' n) + (hy : ∀ y ∈ T, specPtr y ∈ x.ntfnObj.ntfnQueue → MemberEq m m' y) : NtfnRel1 m' T n x := + ⟨h.obj.frame hn hy, hn.bound ▸ h.bound, hn.bound ▸ h.bound_mem, hn.sc ▸ h.sc⟩ + +/-- Members of a waiting queue are TCBs blocked on the notification. -/ +theorem NtfnObjRel.blocked {m : U64 → U64} {T : Set U64} {n : U64} {o : Ntfn} (h : NtfnObjRel m T n o) + {y : U64} (hy : specPtr y ∈ o.ntfnQueue) : y ∈ T ∧ BlockedOn m n y := by + cases o with + | idleNtfn => simp [Ntfn.ntfnQueue] at hy + | activeNtfn b => simp [Ntfn.ntfnQueue] at hy + | waitingNtfn q => + obtain ⟨-, xs, -, rfl, h2, h3⟩ := h + simp only [Ntfn.ntfnQueue, List.mem_map] at hy + obtain ⟨z, hz, e⟩ := hy + have : z = y := specPtr_inj e + subst this + exact ⟨h2.mem z hz, h3 z hz⟩ + +theorem bv_inj {x y : U64} (h : x ≠ y) : specPtr x ≠ specPtr y := fun e => h (specPtr_inj e) + +/-- Updating the notification at `n`: the other notifications and the TCBs +keep their relation when their words agree (the links of a TCB only need to +agree if it is not blocked on `n`). -/ +theorem NtfnStateRel.upd_ntfn {N T : Set U64} {m m' : U64 → U64} {σ : State Z} (hR : NtfnStateRel N T m σ) + {n : U64} (hn : n ∈ N) (x' : Notification) + (hN : ∀ n' ∈ N, n' ≠ n → NWordsEq m m' n') + (hT : ∀ y ∈ T, TFieldsEq m m' y) + (hL : ∀ y ∈ T, m (tBlockA y) ≠ n → TLinksEq m m' y) + (hx : NtfnRel1 m' T n x') : + NtfnStateRel N T m' { σ with kheap := Function.update σ.kheap (specPtr n) (some (.notification x')) } := by + obtain ⟨x, hk, -⟩ := hR.ntfn n hn + refine ⟨fun n' hn' => ?_, fun t ht => ?_⟩ + · by_cases e : n' = n + · subst e; exact ⟨x', by simp, hx⟩ + · obtain ⟨y, hky, hy⟩ := hR.ntfn n' hn' + refine ⟨y, by simp [bv_inj e, hky], hy.frame (hN n' hn' e) fun z hz hq => ?_⟩ + obtain ⟨-, -, hb⟩ := hy.obj.blocked hq + exact ⟨(hT z hz).st, (hT z hz).blk, hL z hz (by rw [hb]; exact e)⟩ + · obtain ⟨y, hky, hy⟩ := hR.tcb t ht + have : specPtr t ≠ specPtr n := by intro e; rw [e, hk] at hky; cases hky + exact ⟨y, by simp [this, hky], hy.frame (hT t ht)⟩ + +/-- Updating the TCB at `t`: the notifications and other TCBs keep their +relation when their words agree; `t`'s state and block object may only +change if `t` is in no notification queue. -/ +theorem NtfnStateRel.upd_tcb {N T : Set U64} {m m' : U64 → U64} {σ : State Z} (hR : NtfnStateRel N T m σ) + {t : U64} (ht : t ∈ T) (x' : Tcb) + (hN : ∀ n ∈ N, NWordsEq m m' n) + (hT : ∀ y ∈ T, y ≠ t → TFieldsEq m m' y) + (hL : ∀ y ∈ T, TLinksEq m m' y) + (hq : (∀ n ∈ N, ∀ z, σ.kheap (specPtr n) = some (.notification z) → specPtr t ∉ z.ntfnObj.ntfnQueue) ∨ + (m' (stA t) = m (stA t) ∧ m' (tBlockA t) = m (tBlockA t))) + (hx : TcbRel1 m' t x') : + NtfnStateRel N T m' { σ with kheap := Function.update σ.kheap (specPtr t) (some (.tcb x')) } := by + obtain ⟨x, hk, -⟩ := hR.tcb t ht + refine ⟨fun n hn => ?_, fun y hy => ?_⟩ + · obtain ⟨z, hkz, hz⟩ := hR.ntfn n hn + have : specPtr n ≠ specPtr t := by intro e; rw [e, hk] at hkz; cases hkz + refine ⟨z, by simp [this, hkz], hz.frame (hN n hn) fun y hy hq' => ?_⟩ + by_cases e : y = t + · subst e + rcases hq with hq | ⟨e1, e2⟩ + · exact absurd hq' (hq n hn z hkz) + · exact ⟨e1, e2, hL y hy⟩ + · exact ⟨(hT y hy e).st, (hT y hy e).blk, hL y hy⟩ + · by_cases e : y = t + · subst e; exact ⟨x', by simp, hx⟩ + · obtain ⟨z, hkz, hz⟩ := hR.tcb y hy + exact ⟨z, by simp [bv_inj e, hkz], hz.frame (hT y hy e)⟩ + +end Frame + +/-! ## The footprint -/ + +section Foot +open Fl4SpecA64 +variable {Z : Type} {N T : Set U64} + +/-- The notification words: aligned, in the footprint. -/ +theorem nword {n K : U64} (hP : NPlaced T N) (hn : n ∈ N) (hK : K.val < 40) (hK8 : K.val % 8 = 0) : + Aligned (core.num.U64.wrapping_add n K) ∧ NFoot N T (core.num.U64.wrapping_add n K) := by + have ha := hP.ntfn n hn + refine ⟨nfield_aligned ha (by omega) hK8, Or.inl ⟨n, hn, ?_⟩⟩ + rw [nfield_val ha (by omega)]; omega + +theorem tword {t K : U64} (hP : NPlaced T N) (ht : t ∈ T) (hK : K.val < 4096) (hK8 : K.val % 8 = 0) : + Aligned (core.num.U64.wrapping_add t K) := + field_aligned (hP.tcb t ht) hK hK8 + +theorem foot_head {n : U64} (hP : NPlaced T N) (hn : n ∈ N) : Aligned (epHeadA n) ∧ NFoot N T (epHeadA n) := by + rw [epHeadA_eq]; exact nword hP hn (by simp) (by simp) +theorem foot_ts {n : U64} (hP : NPlaced T N) (hn : n ∈ N) : Aligned (epTSA n) ∧ NFoot N T (epTSA n) := by + rw [epTSA_eq]; exact nword hP hn (by simp) (by simp) +theorem foot_badge {n : U64} (hP : NPlaced T N) (hn : n ∈ N) : Aligned (nBadgeA n) ∧ NFoot N T (nBadgeA n) := by + rw [nBadgeA_eq]; exact nword hP hn (by simp) (by simp) +theorem foot_bound {n : U64} (hP : NPlaced T N) (hn : n ∈ N) : Aligned (nBoundA n) ∧ NFoot N T (nBoundA n) := by + rw [nBoundA_eq]; exact nword hP hn (by simp) (by simp) +theorem foot_sc {n : U64} (hP : NPlaced T N) (hn : n ∈ N) : Aligned (nScA n) ∧ NFoot N T (nScA n) := by + rw [nScA_eq]; exact nword hP hn (by simp) (by simp) + +theorem foot_st {t : U64} (hP : NPlaced T N) (ht : t ∈ T) : Aligned (stA t) ∧ NFoot N T (stA t) := + ⟨by rw [stA_eq]; exact tword hP ht (by simp) (by simp), Or.inr ⟨t, ht, by simp⟩⟩ +theorem foot_blk {t : U64} (hP : NPlaced T N) (ht : t ∈ T) : Aligned (tBlockA t) ∧ NFoot N T (tBlockA t) := + ⟨by rw [tBlockA_eq]; exact tword hP ht (by simp) (by simp), Or.inr ⟨t, ht, by simp⟩⟩ +theorem foot_breg {t : U64} (hP : NPlaced T N) (ht : t ∈ T) : Aligned (badgeRegA t) ∧ NFoot N T (badgeRegA t) := + ⟨by rw [badgeRegA_eq]; exact tword hP ht (by simp) (by simp), Or.inr ⟨t, ht, by simp⟩⟩ +theorem foot_prio {t : U64} (hP : NPlaced T N) (ht : t ∈ T) : Aligned (prioA t) ∧ NFoot N T (prioA t) := + ⟨by rw [prioA_eq']; exact tword hP ht (by simp) (by simp), Or.inr ⟨t, ht, by simp⟩⟩ +theorem foot_next {t : U64} (hP : NPlaced T N) (ht : t ∈ T) : Aligned (nextA .Ep t) ∧ NFoot N T (nextA .Ep t) := + ⟨nextA_aligned (hP.tcb t ht) .Ep, Or.inr ⟨t, ht, by simp⟩⟩ +theorem foot_prev {t : U64} (hP : NPlaced T N) (ht : t ∈ T) : Aligned (prevA .Ep t) ∧ NFoot N T (prevA .Ep t) := + ⟨prevA_aligned (hP.tcb t ht) .Ep, Or.inr ⟨t, ht, by simp⟩⟩ + +/-- Memory that agrees on the footprint. -/ +def FootEq (N T : Set U64) (m m' : U64 → U64) : Prop := ∀ a, Aligned a → NFoot N T a → m' a = m a + +theorem FootEq.nwords {m m' : U64 → U64} (hP : NPlaced T N) (h : FootEq N T m m') {n : U64} (hn : n ∈ N) : + NWordsEq m m' n := + ⟨h _ (foot_head hP hn).1 (foot_head hP hn).2, h _ (foot_ts hP hn).1 (foot_ts hP hn).2, + h _ (foot_badge hP hn).1 (foot_badge hP hn).2, h _ (foot_bound hP hn).1 (foot_bound hP hn).2, + h _ (foot_sc hP hn).1 (foot_sc hP hn).2⟩ + +theorem FootEq.tfields {m m' : U64 → U64} (hP : NPlaced T N) (h : FootEq N T m m') {t : U64} (ht : t ∈ T) : + TFieldsEq m m' t := + ⟨h _ (foot_st hP ht).1 (foot_st hP ht).2, h _ (foot_blk hP ht).1 (foot_blk hP ht).2, + h _ (foot_breg hP ht).1 (foot_breg hP ht).2, h _ (foot_prio hP ht).1 (foot_prio hP ht).2⟩ + +theorem FootEq.tlinks {m m' : U64 → U64} (hP : NPlaced T N) (h : FootEq N T m m') {t : U64} (ht : t ∈ T) : + TLinksEq m m' t := + ⟨h _ (foot_prev hP ht).1 (foot_prev hP ht).2, h _ (foot_next hP ht).1 (foot_next hP ht).2⟩ + +/-- The relation only reads the footprint and `KView`. -/ +theorem NtfnStateRel.frame {m m' : U64 → U64} {σ σ' : State Z} (hR : NtfnStateRel N T m σ) + (hP : NPlaced T N) (hm : FootEq N T m m') (hk : KView σ σ') : NtfnStateRel N T m' σ' := by + refine ⟨fun n hn => ?_, fun t ht => ?_⟩ + · obtain ⟨x, hkx, hx⟩ := hR.ntfn n hn + exact ⟨x, hk.1 _ _ hkx, hx.frame (hm.nwords hP hn) fun y hy _ => + ⟨(hm.tfields hP hy).st, (hm.tfields hP hy).blk, hm.tlinks hP hy⟩⟩ + · obtain ⟨x, hkx, hx⟩ := hR.tcb t ht + obtain ⟨y, hky, e1, e2, e3⟩ := hk.2 _ _ hkx + have hx' := hx.frame (hm.tfields hP ht) + exact ⟨y, hky, ⟨e1 ▸ hx'.st, fun n hn => hx'.blk n (e1 ▸ hn), e2 ▸ hx'.badge, e3 ▸ hx'.prio⟩⟩ + +theorem KView.refl (σ : State Z) : KView σ σ := + ⟨fun _ _ h => h, fun _ x h => ⟨x, h, rfl, rfl, rfl⟩⟩ + +theorem KView.trans {σ₁ σ₂ σ₃ : State Z} (h₁ : KView σ₁ σ₂) (h₂ : KView σ₂ σ₃) : KView σ₁ σ₃ := + ⟨fun p x h => h₂.1 p x (h₁.1 p x h), fun p x h => by + obtain ⟨y, hy, e1, e2, e3⟩ := h₁.2 p x h + obtain ⟨z, hz, f1, f2, f3⟩ := h₂.2 p y hy + exact ⟨z, hz, f1.trans e1, f2.trans e2, f3.trans e3⟩⟩ + +theorem NtfnStep.refl (σ : State Z) : NtfnStep σ σ := ⟨rfl, rfl, fun _ => Or.inl rfl⟩ + +theorem NtfnStep.trans {σ₁ σ₂ σ₃ : State Z} (h₁ : NtfnStep σ₁ σ₂) (h₂ : NtfnStep σ₂ σ₃) : NtfnStep σ₁ σ₃ := by + obtain ⟨a1, b1, c1⟩ := h₁ + obtain ⟨a2, b2, c2⟩ := h₂ + refine ⟨a2.trans a1, ?_, fun p => ?_⟩ + · rw [← b1]; rw [← b2] + · rcases c1 p with e1 | ⟨x, y, hx, hy, f1, f2⟩ | ⟨x, y, hx, hy, f⟩ <;> + rcases c2 p with e2 | ⟨x', y', hx', hy', f1', f2'⟩ | ⟨x', y', hx', hy', f'⟩ + · exact Or.inl (e2.trans e1) + · rw [e1] at hx'; exact Or.inr (Or.inl ⟨x', y', hx', hy', f1', f2'⟩) + · rw [e1] at hx'; exact Or.inr (Or.inr ⟨x', y', hx', hy', f'⟩) + · rw [← e2] at hy; exact Or.inr (Or.inl ⟨x, y, hx, hy, f1, f2⟩) + · rw [hy] at hx'; cases hx' + exact Or.inr (Or.inl ⟨x, y', hx, hy', f1'.trans f1, f2'.trans f2⟩) + · rw [hy] at hx'; cases hx' + · rw [← e2] at hy; exact Or.inr (Or.inr ⟨x, y, hx, hy, f⟩) + · rw [hy] at hx'; cases hx' + · rw [hy] at hx'; cases hx' + refine Or.inr (Or.inr ⟨x, y', hx, hy', ?_⟩) + rw [f', f] + +/-- Writing a notification object with the same bound TCB and SC. -/ +theorem NtfnStep.ntfn {σ : State Z} {p : ObjRef} {x y : Notification} (hk : σ.kheap p = some (.notification x)) + (h1 : y.ntfnBoundTcb = x.ntfnBoundTcb) (h2 : y.ntfnSc = x.ntfnSc) : + NtfnStep σ { σ with kheap := Function.update σ.kheap p (some (.notification y)) } := by + refine ⟨rfl, rfl, fun q => ?_⟩ + by_cases e : q = p + · subst e; exact Or.inr (Or.inl ⟨x, y, hk, by simp, h1, h2⟩) + · exact Or.inl (by simp [e]) + +/-- Writing a TCB's state and user context. -/ +theorem NtfnStep.tcb {σ : State Z} {p : ObjRef} {x : Tcb} (hk : σ.kheap p = some (.tcb x)) (ts : ThreadState) + (a : ArchTcb) : + NtfnStep σ { σ with kheap := Function.update σ.kheap p (some (.tcb { x with tcbState := ts, tcbArch := a })) } := by + refine ⟨rfl, rfl, fun q => ?_⟩ + by_cases e : q = p + · subst e; exact Or.inr (Or.inr ⟨x, { x with tcbState := ts, tcbArch := a }, hk, by simp, rfl⟩) + · exact Or.inl (by simp [e]) + +/-- (gen 6) Writing a TCB's state, user context and fault (`cancel_ipc`'s fault clear). -/ +theorem NtfnStep.tcbF {σ : State Z} {p : ObjRef} {x : Tcb} (hk : σ.kheap p = some (.tcb x)) (ts : ThreadState) + (a : ArchTcb) (f : Option Fault) : + NtfnStep σ { σ with kheap := Function.update σ.kheap p (some (.tcb { x with tcbState := ts, tcbArch := a, tcbFault := f })) } := by + refine ⟨rfl, rfl, fun q => ?_⟩ + by_cases e : q = p + · subst e + exact Or.inr (Or.inr ⟨x, { x with tcbState := ts, tcbArch := a, tcbFault := f }, hk, by simp, rfl⟩) + · exact Or.inl (by simp [e]) + +end Foot + +end Fl4ProofsA64.P3.Notif diff --git a/proofs/Fl4ProofsA64/P3/NtfnSendA.lean b/proofs/Fl4ProofsA64/P3/NtfnSendA.lean new file mode 100644 index 000000000..135abb4df --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/NtfnSendA.lean @@ -0,0 +1,334 @@ +/- +P3 (aarch64, A64-P3-IPC gen 1): **`notification::send_signal` refines `send_signal`** of `Fl4SpecA64` (`Z = Unit`) +in the cases that involve no other subsystem (x86 `P3/NtfnSend`, ported; FL4's AArch64 `send_signal` is the x86 +text, the spec's `sendSignal` the same l4v definition): + +* `send_signal_idle_unbound`: an idle notification without a bound TCB becomes active with the badge; +* `send_signal_idle_bound_idle`: an idle notification whose bound TCB is not blocked on a receive (and not running a + VCPU, `ext runningVM`: FL4 then reports to the VCPU, `vcpu.signal_completes` / `kick_guest`) becomes active; +* `send_signal_active`: on an active notification, the badge is OR-ed into the pending one. + +In each, from states related by `NtfnStateRel` (with `NPlaced`), the spec has exactly one outcome and does not fail, +and FL4's final state is related to it; FL4 writes only words of the footprint (`WritesIn … NWrite`), the spec only +changes `ntfn_obj` (`NtfnStep`). The waiting case and the bound-receiver case wake a thread (scheduler, x86 +`NtfnSched` / `NtfnWake`): gen 2. Every object named by `specPtr` (`P3/NtfnRelA`). + +Case hypotheses (decode-site style, as x86's): `hidle` / `hact` / `hb` / `hrb` / `hvm` select the case; no named +premise. +-/ +import Fl4ProofsA64.P3.NtfnWriteA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Notif +open fl4_core_a64 Fl4ProofsA64.P1 + +noncomputable section + +/-! ## Reading the relation -/ + +section +variable {Z : Type} {N T : Set U64} {m : U64 → U64} {σ : Fl4SpecA64.State Z} + +theorem NtfnStateRel.at_ntfn (hR : NtfnStateRel N T m σ) {n : U64} (hn : n ∈ N) {x : Fl4SpecA64.Notification} + (hk : σ.kheap (specPtr n) = some (.notification x)) : NtfnRel1 m T n x := by + obtain ⟨y, hky, hy⟩ := hR.ntfn n hn + rw [hk] at hky; cases hky; exact hy + +theorem NtfnStateRel.at_tcb (hR : NtfnStateRel N T m σ) {t : U64} (ht : t ∈ T) {x : Fl4SpecA64.Tcb} + (hk : σ.kheap (specPtr t) = some (.tcb x)) : TcbRel1 m t x := by + obtain ⟨y, hky, hy⟩ := hR.tcb t ht + rw [hk] at hky; cases hky; exact hy + +theorem optRef_zero : optRef 0#u64 = none := by simp [optRef] +theorem optRef_ne {w : U64} (h : w ≠ 0#u64) : optRef w = some (specPtr w) := by simp [optRef, h] +theorem optRef_none {w : U64} (h : optRef w = none) : w = 0#u64 := by + by_contra e; rw [optRef_ne e] at h; cases h +theorem optRef_some {w : U64} {p : Fl4SpecA64.ObjRef} (h : optRef w = some p) : w ≠ 0#u64 ∧ (specPtr w) = p := by + by_cases e : w = 0#u64 + · rw [e, optRef_zero] at h; cases h + · rw [optRef_ne e] at h; cases h; exact ⟨e, rfl⟩ + +/-- A queue relation to the empty list only says the queue is empty. -/ +theorem queueRel_nil_iff {m : U64 → U64} {T : Set U64} {l : tcbqueue.Link} {q : tcbqueue.Queue} : + QueueRel m T l q [] ↔ q.head = 0#u64 ∧ q.end = 0#u64 := + ⟨fun h => ⟨h.head, h.last⟩, fun ⟨h1, h2⟩ => ⟨List.nodup_nil, by simp, h1, h2, trivial⟩⟩ + +end + +theorem ntfn_consts : obj.NTFN_IDLE = 0#u64 ∧ obj.NTFN_WAITING = 1#u64 ∧ obj.NTFN_ACTIVE = 2#u64 := by + unfold obj.NTFN_IDLE obj.NTFN_WAITING obj.NTFN_ACTIVE; exact ⟨rfl, rfl, rfl⟩ + +theorem two_and3 : (2#u64 &&& 3#u64) = 2#u64 := by decide +theorem zero_and3 : (0#u64 &&& 3#u64) = 0#u64 := by decide + +/-! ## The store side: writing the state and the badge -/ + +section +variable {S : Type _} {st : machine.Store S} [L : LawfulStore st] {N T : Set U64} + +/-- `set_active` on an idle notification: related to the spec's +`set_notification` of an active notification with the badge. -/ +theorem set_active_rel (hP : NPlaced T N) {s : S} {σ : Fl4SpecA64.State Unit} + (hR : NtfnStateRel N T (L.mem s) σ) {n : U64} (hn : n ∈ N) {x : Fl4SpecA64.Notification} + (hk : σ.kheap (specPtr n) = some (.notification x)) (hidle : x.ntfnObj = .idleNtfn) (b : U64) : + let s' := L.wr (L.wr s (epTSA n) ((L.mem s (epTSA n) &&& ~~~3#u64) ||| (obj.NTFN_ACTIVE &&& 3#u64))) + (nBadgeA n) b + NtfnStateRel N T (L.mem s') + (updK σ (specPtr n) (.notification (Fl4SpecA64.ntfnObjUpdate (fun _ => .activeNtfn b.bv) x))) ∧ + WritesIn st s s' (NWrite N T) := by + intro s' + have hna := hP.ntfn n hn + have h1 : NWr st N T n s (L.wr s (epTSA n) ((L.mem s (epTSA n) &&& ~~~3#u64) ||| (obj.NTFN_ACTIVE &&& 3#u64))) := by + rw [epTSA_eq]; exact nwr_wr hP hn (by simp) (by simp) _ _ + have h2 : NWr st N T n (L.wr s (epTSA n) ((L.mem s (epTSA n) &&& ~~~3#u64) ||| (obj.NTFN_ACTIVE &&& 3#u64))) s' := by + show NWr st N T n _ (L.wr _ (nBadgeA n) b) + rw [nBadgeA_eq]; exact nwr_wr hP hn (by simp) (by simp) _ _ + have h := h1.trans h2 + have hx := hR.at_ntfn hn hk + have hobj := hx.obj + rw [hidle] at hobj + obtain ⟨-, hq⟩ := hobj + rw [queueRel_nil_iff] at hq + -- the words of `n` after the two writes + have ne_b : ∀ K : U64, K.val < 40 → K.val % 8 = 0 → K ≠ 16#u64 → + L.mem s' (core.num.U64.wrapping_add n K) = + L.mem (L.wr s (epTSA n) ((L.mem s (epTSA n) &&& ~~~3#u64) ||| (obj.NTFN_ACTIVE &&& 3#u64))) + (core.num.U64.wrapping_add n K) := by + intro K hK hK8 hne + show L.mem (L.wr _ (nBadgeA n) b) _ = _ + rw [nBadgeA_eq] + exact rd_wr_ne (nword hP hn (by simp) (by simp)).1 (nword hP hn hK hK8).1 + (nwords_ne hna (by simp) (by omega) (Ne.symm hne)) + have ne_ts : ∀ K : U64, K.val < 40 → K.val % 8 = 0 → K ≠ 8#u64 → + L.mem (L.wr s (epTSA n) ((L.mem s (epTSA n) &&& ~~~3#u64) ||| (obj.NTFN_ACTIVE &&& 3#u64))) + (core.num.U64.wrapping_add n K) = L.mem s (core.num.U64.wrapping_add n K) := by + intro K hK hK8 hne + rw [epTSA_eq] + exact rd_wr_ne (nword hP hn (by simp) (by simp)).1 (nword hP hn hK hK8).1 + (nwords_ne hna (by simp) (by omega) (Ne.symm hne)) + have e_head : L.mem s' (epHeadA n) = L.mem s (epHeadA n) := by + rw [epHeadA_eq, ne_b _ (by simp) (by simp) (by simp), ne_ts _ (by simp) (by simp) (by simp)] + have e_ts : L.mem s' (epTSA n) = (L.mem s (epTSA n) &&& ~~~3#u64) ||| (obj.NTFN_ACTIVE &&& 3#u64) := by + rw [epTSA_eq, ne_b _ (by simp) (by simp) (by simp), ← epTSA_eq] + exact rd_wr_eq (foot_ts hP hn).1 + have e_badge : L.mem s' (nBadgeA n) = b := rd_wr_eq (foot_badge hP hn).1 + have e_bound : L.mem s' (nBoundA n) = L.mem s (nBoundA n) := by + rw [nBoundA_eq, ne_b _ (by simp) (by simp) (by simp), ne_ts _ (by simp) (by simp) (by simp)] + have e_sc : L.mem s' (nScA n) = L.mem s (nScA n) := by + rw [nScA_eq, ne_b _ (by simp) (by simp) (by simp), ne_ts _ (by simp) (by simp) (by simp)] + refine ⟨hR.upd_ntfn hn _ h.ntfn (fun y hy => (h.tcb y hy).1) (fun y hy _ => (h.tcb y hy).2) ⟨?_, ?_, ?_, ?_⟩, h.foot⟩ + · show NtfnObjRel _ T n (.activeNtfn b.bv) + refine ⟨?_, ?_, by rw [e_badge]⟩ + · rw [stOf, e_ts, ts_state, ntfn_consts.2.2]; exact two_and3 + · rw [queueRel_nil_iff] + refine ⟨?_, ?_⟩ + · show L.mem s' (epHeadA n) = 0#u64 + rw [e_head]; exact hq.1 + · show L.mem s' (epTSA n) &&& ~~~3#u64 = 0#u64 + rw [e_ts, ts_end]; exact hq.2 + · show x.ntfnBoundTcb = _; rw [e_bound]; exact hx.bound + · rw [e_bound]; exact hx.bound_mem + · show x.ntfnSc = _; rw [e_sc]; exact hx.sc + +/-- Writing the badge word of an active notification (`send_signal` on an +active notification). -/ +theorem set_badge_rel (hP : NPlaced T N) {s : S} {σ : Fl4SpecA64.State Unit} + (hR : NtfnStateRel N T (L.mem s) σ) {n : U64} (hn : n ∈ N) {x : Fl4SpecA64.Notification} + (hk : σ.kheap (specPtr n) = some (.notification x)) {b0 : Fl4SpecA64.Badge} (hact : x.ntfnObj = .activeNtfn b0) + (b : U64) : + let s' := L.wr s (nBadgeA n) b + NtfnStateRel N T (L.mem s') + (updK σ (specPtr n) (.notification (Fl4SpecA64.ntfnObjUpdate (fun _ => .activeNtfn b.bv) x))) ∧ + WritesIn st s s' (NWrite N T) := by + intro s' + have hna := hP.ntfn n hn + have h : NWr st N T n s s' := by show NWr st N T n s (L.wr s _ b); rw [nBadgeA_eq]; exact nwr_wr hP hn (by simp) (by simp) _ _ + have hx := hR.at_ntfn hn hk + have hobj := hx.obj + rw [hact] at hobj + obtain ⟨hs, hq, -⟩ := hobj + have ne_b : ∀ K : U64, K.val < 40 → K.val % 8 = 0 → K ≠ 16#u64 → + L.mem s' (core.num.U64.wrapping_add n K) = L.mem s (core.num.U64.wrapping_add n K) := by + intro K hK hK8 hne + show L.mem (L.wr _ (nBadgeA n) b) _ = _ + rw [nBadgeA_eq] + exact rd_wr_ne (nword hP hn (by simp) (by simp)).1 (nword hP hn hK hK8).1 + (nwords_ne hna (by simp) (by omega) (Ne.symm hne)) + have hnw : NWordsEq (L.mem s) (L.mem s') n ∨ True := Or.inr trivial + clear hnw + have e_head : L.mem s' (epHeadA n) = L.mem s (epHeadA n) := by rw [epHeadA_eq, ne_b _ (by simp) (by simp) (by simp)] + have e_ts : L.mem s' (epTSA n) = L.mem s (epTSA n) := by rw [epTSA_eq, ne_b _ (by simp) (by simp) (by simp)] + have e_bound : L.mem s' (nBoundA n) = L.mem s (nBoundA n) := by + rw [nBoundA_eq, ne_b _ (by simp) (by simp) (by simp)] + have e_sc : L.mem s' (nScA n) = L.mem s (nScA n) := by rw [nScA_eq, ne_b _ (by simp) (by simp) (by simp)] + have hqe : qOf (L.mem s') n = qOf (L.mem s) n := by simp only [qOf, e_head, e_ts] + refine ⟨hR.upd_ntfn hn _ h.ntfn (fun y hy => (h.tcb y hy).1) (fun y hy _ => (h.tcb y hy).2) ⟨?_, ?_, ?_, ?_⟩, h.foot⟩ + · show NtfnObjRel _ T n (.activeNtfn b.bv) + refine ⟨?_, ?_, by rw [rd_wr_eq (foot_badge hP hn).1]⟩ + · simp only [stOf, e_ts]; exact hs + · rw [hqe, queueRel_nil_iff]; exact queueRel_nil_iff.1 hq + · show x.ntfnBoundTcb = _; rw [e_bound]; exact hx.bound + · rw [e_bound]; exact hx.bound_mem + · show x.ntfnSc = _; rw [e_sc]; exact hx.sc + +end + +/-! ## `send_signal` -/ + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] {N T : Set U64} + +theorem stOf_idle {m : U64 → U64} {n : U64} {o : Fl4SpecA64.Ntfn} (h : NtfnObjRel m T n o) (ho : o = .idleNtfn) : + stOf m n = obj.NTFN_IDLE := by subst ho; rw [h.1, ntfn_consts.1] +theorem stOf_active {m : U64 → U64} {n : U64} {o : Fl4SpecA64.Ntfn} {b : Fl4SpecA64.Badge} (h : NtfnObjRel m T n o) + (ho : o = .activeNtfn b) : stOf m n = obj.NTFN_ACTIVE := by subst ho; rw [h.1, ntfn_consts.2.2] +theorem stOf_waiting {m : U64 → U64} {n : U64} {o : Fl4SpecA64.Ntfn} {q : List Fl4SpecA64.ObjRef} + (h : NtfnObjRel m T n o) (ho : o = .waitingNtfn q) : stOf m n = obj.NTFN_WAITING := by + subst ho; rw [h.1, ntfn_consts.2.1] + +/-- The outcome of `send_signal` on an idle notification, or one whose bound +TCB is not receiving: active with the badge. -/ +theorem sendSignal_activate {σ : Fl4SpecA64.State Unit} {p : Fl4SpecA64.ObjRef} {x : Fl4SpecA64.Notification} + (hk : σ.kheap p = some (.notification x)) (hidle : x.ntfnObj = .idleNtfn) + (hb : x.ntfnBoundTcb = none ∨ + ∃ t tcb, x.ntfnBoundTcb = some t ∧ σ.kheap t = some (.tcb tcb) ∧ Fl4SpecA64.receiveBlocked tcb.tcbState = false) + (badge : Fl4SpecA64.Badge) : + Fl4SpecA64.sendSignal (Z := Unit) p badge σ = + ({((), updK σ p (.notification (Fl4SpecA64.ntfnObjUpdate (fun _ => .activeNtfn badge) x)))}, False) := by + unfold Fl4SpecA64.sendSignal + rw [nd_bind_one (getNotification_eq hk)] + rcases hb with hb | ⟨t, tcb, hb, hkt, hr⟩ + · simp only [hidle, hb] + exact setNotification_eq _ hk + · simp only [hidle, hb] + rw [nd_bind_one (getThreadState_eq hkt)] + simp only [hr, Bool.false_eq_true, ↓reduceIte] + exact setNotification_eq _ hk + +theorem sendSignal_active_eq {σ : Fl4SpecA64.State Unit} {p : Fl4SpecA64.ObjRef} {x : Fl4SpecA64.Notification} + (hk : σ.kheap p = some (.notification x)) {b0 : Fl4SpecA64.Badge} (hact : x.ntfnObj = .activeNtfn b0) + (badge : Fl4SpecA64.Badge) : + Fl4SpecA64.sendSignal (Z := Unit) p badge σ = + ({((), updK σ p (.notification + (Fl4SpecA64.ntfnObjUpdate (fun _ => .activeNtfn (Fl4SpecA64.combineNtfnBadges badge b0)) x)))}, False) := by + unfold Fl4SpecA64.sendSignal + rw [nd_bind_one (getNotification_eq hk)] + simp only [hact] + exact setNotification_eq _ hk + +/-- What the simple refinements conclude. -/ +def SingleRel (N T : Set U64) (s : S) (σ : Fl4SpecA64.State Unit) (spec : Set (Unit × Fl4SpecA64.State Unit) × Prop) + (s' : S) : Prop := + ∃ σ', spec = ({((), σ')}, False) ∧ NtfnStateRel N T (L.mem s') σ' ∧ NtfnStep σ σ' ∧ + WritesIn st s s' (NWrite N T) + +/-- **Idle, no bound TCB**: the notification becomes active with the badge. -/ +theorem send_signal_idle_unbound (hP : NPlaced T N) (s : S) (a : A) (σ : Fl4SpecA64.State Unit) + (hR : NtfnStateRel N T (L.mem s) σ) {n : U64} (hn : n ∈ N) {x : Fl4SpecA64.Notification} + (hk : σ.kheap (specPtr n) = some (.notification x)) (hidle : x.ntfnObj = .idleNtfn) + (hb : x.ntfnBoundTcb = none) (badge : U64) : + notification.send_signal st ar s a n badge ⦃ r => + r.2 = a ∧ SingleRel (st := st) N T s σ (Fl4SpecA64.sendSignal (specPtr n) badge.bv σ) r.1 ⦄ := by + have hx := hR.at_ntfn hn hk + have h0 : L.mem s (nBoundA n) = 0#u64 := optRef_none (hx.bound ▸ hb) + unfold notification.send_signal + step + have : st = obj.NTFN_IDLE := by rw [st_post]; exact stOf_idle hx.obj hidle + simp only [this, ↓reduceIte] + step + step + simp only [b_post, i_post, h0, decide_true, ↓reduceIte] + step + subst s1_post + obtain ⟨hR', hW⟩ := set_active_rel hP hR hn hk hidle badge + exact ⟨_, sendSignal_activate hk hidle (Or.inl hb) badge.bv, hR', NtfnStep.ntfn hk rfl rfl, hW⟩ + +/-- **Idle, bound TCB not receiving**: the notification becomes active with +the badge. -/ +theorem send_signal_idle_bound_idle (hP : NPlaced T N) (s : S) (a : A) (σ : Fl4SpecA64.State Unit) + (hR : NtfnStateRel N T (L.mem s) σ) {n : U64} (hn : n ∈ N) {x : Fl4SpecA64.Notification} + (hk : σ.kheap (specPtr n) = some (.notification x)) (hidle : x.ntfnObj = .idleNtfn) + {t : Fl4SpecA64.ObjRef} {tcb : Fl4SpecA64.Tcb} (hb : x.ntfnBoundTcb = some t) (hkt : σ.kheap t = some (.tcb tcb)) + (hrb : Fl4SpecA64.receiveBlocked tcb.tcbState = false) + (hvm : tcb.tcbState ≠ .ext .runningVM) (badge : U64) : + notification.send_signal st ar s a n badge ⦃ r => + r.2 = a ∧ SingleRel (st := st) N T s σ (Fl4SpecA64.sendSignal (specPtr n) badge.bv σ) r.1 ⦄ := by + have hx := hR.at_ntfn hn hk + obtain ⟨hb0, hbt⟩ := optRef_some (hx.bound ▸ hb) + have hbT := hx.bound_mem hb0 + have hy := hR.at_tcb hbT (hbt ▸ hkt) + have hns : L.mem s (stA (L.mem s (nBoundA n))) ≠ sched.TS_BLOCKED_ON_RECV := by + intro e + have h3 := hy.st + rw [e, tsVals.2.2.1] at h3 + revert hrb h3 + cases tcb.tcbState with + | ext e => cases e <;> simp [tsCode] + | _ => simp [tsCode, Fl4SpecA64.receiveBlocked] + have hnvm : L.mem s (stA (L.mem s (nBoundA n))) ≠ sched.TS_RUNNING_VM := by + intro e + have h3 := hy.st + rw [e] at h3 + simp only [sched.TS_RUNNING_VM] at h3 + cases hst : tcb.tcbState with + | ext e => cases e; exact hvm hst + | _ => simp_all [tsCode] + unfold notification.send_signal + step + have : st = obj.NTFN_IDLE := by rw [st_post]; exact stOf_idle hx.obj hidle + simp only [this, ↓reduceIte] + step + step + simp only [b_post, i_post, hb0, decide_false, Bool.false_eq_true, ↓reduceIte] + step + simp only [i1_post, hns, hnvm, ↓reduceIte] + step + subst s1_post + obtain ⟨hR', hW⟩ := set_active_rel hP hR hn hk hidle badge + have hst' : L.mem (L.wr (L.wr s (epTSA n) + ((L.mem s (epTSA n) &&& ~~~3#u64) ||| (obj.NTFN_ACTIVE &&& 3#u64))) (nBadgeA n) badge) + (stA (L.mem s (nBoundA n))) = L.mem s (stA (L.mem s (nBoundA n))) := by + have h1 := nwr_wr (L := L) hP (K := 8#u64) hn (by simp) (by simp) s + ((L.mem s (epTSA n) &&& ~~~3#u64) ||| (obj.NTFN_ACTIVE &&& 3#u64)) + rw [← epTSA_eq] at h1 + have h2 := nwr_wr (L := L) hP (K := 16#u64) hn (by simp) (by simp) + (L.wr s (epTSA n) ((L.mem s (epTSA n) &&& ~~~3#u64) ||| (obj.NTFN_ACTIVE &&& 3#u64))) badge + rw [← nBadgeA_eq] at h2 + exact ((h1.trans h2).tcb _ hbT).1.st + step with obj.Tcb.state.spec as ⟨vm_state, hvs⟩ + simp only [hvs, hst', hnvm, decide_false, Bool.false_eq_true, ↓reduceIte, bind_ok, WP.spec_ok] + exact ⟨trivial, _, sendSignal_activate hk hidle (Or.inr ⟨t, tcb, hb, hkt, hrb⟩) badge.bv, hR', + NtfnStep.ntfn hk rfl rfl, hW⟩ + +/-- **Active**: the badge is OR-ed into the pending one. -/ +theorem send_signal_active (hP : NPlaced T N) (s : S) (a : A) (σ : Fl4SpecA64.State Unit) + (hR : NtfnStateRel N T (L.mem s) σ) {n : U64} (hn : n ∈ N) {x : Fl4SpecA64.Notification} + (hk : σ.kheap (specPtr n) = some (.notification x)) {b0 : Fl4SpecA64.Badge} (hact : x.ntfnObj = .activeNtfn b0) + (badge : U64) : + notification.send_signal st ar s a n badge ⦃ r => + r.2 = a ∧ SingleRel (st := st) N T s σ (Fl4SpecA64.sendSignal (specPtr n) badge.bv σ) r.1 ⦄ := by + have hx := hR.at_ntfn hn hk + have hb0 : (L.mem s (nBadgeA n)).bv = b0 := by + have := hx.obj; rw [hact] at this; exact this.2.2 + unfold notification.send_signal + step + have : st = obj.NTFN_ACTIVE := by rw [st_post]; exact stOf_active hx.obj hact + simp only [this, ntfn_consts.1, ntfn_consts.2.1, ntfn_consts.2.2] + simp only [show (2#u64 : U64) ≠ 0#u64 by decide, show (2#u64 : U64) ≠ 1#u64 by decide, ↓reduceIte] + step + step + step + subst s1_post + obtain ⟨hR', hW⟩ := set_badge_rel hP hR hn hk hact b + have e : b.bv = Fl4SpecA64.combineNtfnBadges badge.bv b0 := by + have h := b_post1 + rw [i_post] at h + exact h.trans ((BitVec.or_comm _ _).trans (congrArg (badge.bv ||| ·) hb0)) + rw [e] at hR' + exact ⟨_, sendSignal_active_eq hk hact badge.bv, hR', NtfnStep.ntfn hk rfl rfl, hW⟩ + +end + +end + +end Fl4ProofsA64.P3.Notif diff --git a/proofs/Fl4ProofsA64/P3/NtfnSpecA.lean b/proofs/Fl4ProofsA64/P3/NtfnSpecA.lean new file mode 100644 index 000000000..1370d5d03 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/NtfnSpecA.lean @@ -0,0 +1,156 @@ +/- +P3 (aarch64, A64-P3-IPC gen 1, ported from x86), notifications: the spec's accessors on related objects. + +On a state where `p` is a notification (resp. a TCB), `get_notification`, +`set_notification`, `get_thread_state`, `thread_set`, `as_user` with +`setRegister`, and `set_thread_state_act` have exactly one outcome and do +not fail; the lemmas here compute it. `tcb_ntfn_dequeue` on a queue member +and `tcb_append` on TCBs too. +-/ +import Fl4ProofsA64.P3.NtfnRelA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Notif +open Fl4SpecA64 Fl4Spec Classical + +noncomputable section + +section +variable {σ α β : Type} +@[simp] theorem nd_pure_apply' (a : α) (s : σ) : (pure a : NonDet σ α) s = ({(a, s)}, False) := rfl +theorem nd_bind_apply' (m : NonDet σ α) (f : α → NonDet σ β) (s : σ) : + (m >>= f) s = ({r | ∃ x ∈ (m s).1, r ∈ (f x.1 x.2).1}, (m s).2 ∨ ∃ x ∈ (m s).1, (f x.1 x.2).2) := rfl +/-- Binding after a computation with a single outcome. -/ +theorem nd_bind_one {m : NonDet σ α} {s s' : σ} {a : α} (h : m s = ({(a, s')}, False)) (f : α → NonDet σ β) : + (m >>= f) s = f a s' := by + rw [nd_bind_apply', h] + ext <;> simp +theorem nd_bind_one' {m : NonDet σ α} {s s' : σ} {a : α} (h : m s = ({(a, s')}, False)) (f : α → NonDet σ β) : + NonDet.bind m f s = f a s' := nd_bind_one h f +theorem nd_pure_bind' (a : α) (f : α → NonDet σ β) (s : σ) : (pure a >>= f) s = f a s := by + rw [nd_bind_apply']; ext <;> simp +theorem nd_gets_bind (g : σ → α) (f : α → NonDet σ β) (s : σ) : (NonDet.gets g >>= f) s = f (g s) s := by + rw [nd_bind_apply']; ext <;> simp [NonDet.gets] +end + +variable {Z : Type} + +/-- The state with object `obj` at `p`. -/ +def updK (σ : State Z) (p : ObjRef) (obj : KernelObject) : State Z := + { σ with kheap := Function.update σ.kheap p (some obj) } + +@[simp] theorem updK_kheap (σ : State Z) (p : ObjRef) (obj : KernelObject) : + (updK σ p obj).kheap = Function.update σ.kheap p (some obj) := rfl + +theorem getObject_eq {σ : State Z} {p : ObjRef} {o : KernelObject} (hk : σ.kheap p = some o) : + getObject (Z := Z) p σ = ({(o, σ)}, False) := by + simp [getObject, NonDet.getsThe, readObject, hk, NonDet.assertOpt]; rfl + +theorem setObject_eq {σ : State Z} {p : ObjRef} {o o' : KernelObject} (hk : σ.kheap p = some o) + (ht : aType o = aType o') : setObject (Z := Z) p o' σ = ({((), updK σ p o')}, False) := by + unfold setObject + show (getObject p >>= _) σ = _ + rw [nd_bind_one (getObject_eq hk)] + show (NonDet.assert _ >>= _) σ = _ + rw [nd_bind_one (show NonDet.assert (σ := State Z) (aType o = aType o') σ = ({((), σ)}, False) by + simp [NonDet.assert, ht])] + show (NonDet.get >>= _) σ = _ + rw [nd_bind_one (show (NonDet.get : NonDet (State Z) (State Z)) σ = ({(σ, σ)}, False) from rfl)] + rfl + +theorem partialInv_ntfn (x : Notification) : partialInv KernelObject.notification (.notification x) = some x := by + have hu : ∃! y, KernelObject.notification y = .notification x := ⟨x, rfl, fun y h => by cases h; rfl⟩ + unfold partialInv The + simp only [hu, ↓reduceIte, ↓reduceDIte] + have h := (Classical.choose_spec hu).1 + injection h with h + rw [h] + +theorem getNotification_eq {σ : State Z} {p : ObjRef} {x : Notification} + (hk : σ.kheap p = some (.notification x)) : getNotification (Z := Z) p σ = ({(x, σ)}, False) := by + show getSimpleKo KernelObject.notification p σ = _ + unfold getSimpleKo + show (getObject p >>= _) σ = _ + rw [nd_bind_one (getObject_eq hk)] + show (NonDet.assert _ >>= _) σ = _ + rw [nd_bind_one (show NonDet.assert (σ := State Z) (isSimpleType (.notification x)) σ = ({((), σ)}, False) by + simp [NonDet.assert, isSimpleType, aType])] + simp only [partialInv_ntfn]; rfl + +theorem setNotification_eq {σ : State Z} {p : ObjRef} {x : Notification} (y : Notification) + (hk : σ.kheap p = some (.notification x)) : + setNotification (Z := Z) p y σ = ({((), updK σ p (.notification y))}, False) := by + show setSimpleKo KernelObject.notification p y σ = _ + unfold setSimpleKo + show (getObject p >>= _) σ = _ + rw [nd_bind_one (getObject_eq hk)] + show (NonDet.assert _ >>= _) σ = _ + rw [nd_bind_one (show NonDet.assert (σ := State Z) (isSimpleType (.notification x)) σ = ({((), σ)}, False) by + simp [NonDet.assert, isSimpleType, aType])] + show (NonDet.assert _ >>= _) σ = _ + rw [nd_bind_one (show NonDet.assert (σ := State Z) + (partialInv KernelObject.notification (.notification x) ≠ none) σ = ({((), σ)}, False) by + simp [NonDet.assert, partialInv_ntfn])] + exact setObject_eq hk rfl + +theorem getTcb_eq {σ : State Z} {p : ObjRef} {x : Tcb} (hk : σ.kheap p = some (.tcb x)) : + NonDet.getsThe (getTcb (Z := Z) p) σ = ({(x, σ)}, False) := by + simp [NonDet.getsThe, getTcb, readObject, hk, NonDet.assertOpt]; rfl + +theorem threadGet_eq {γ : Type} {σ : State Z} {p : ObjRef} {x : Tcb} (f : Tcb → γ) + (hk : σ.kheap p = some (.tcb x)) : threadGet (Z := Z) f p σ = ({(f x, σ)}, False) := by + unfold threadGet + show (NonDet.getsThe _ >>= _) σ = _ + rw [nd_bind_one (getTcb_eq hk)]; rfl + +theorem getThreadState_eq {σ : State Z} {p : ObjRef} {x : Tcb} (hk : σ.kheap p = some (.tcb x)) : + getThreadState (Z := Z) p σ = ({(x.tcbState, σ)}, False) := threadGet_eq _ hk + +theorem threadSet_eq {σ : State Z} {p : ObjRef} {x : Tcb} (f : Tcb → Tcb) (hk : σ.kheap p = some (.tcb x)) : + threadSet (Z := Z) f p σ = ({((), updK σ p (.tcb (f x)))}, False) := by + unfold threadSet + show (NonDet.getsThe _ >>= _) σ = _ + rw [nd_bind_one (getTcb_eq hk)] + exact setObject_eq hk rfl + +/-- The user context `uc` with register `r` set to `v`. -/ +def setRegUc (uc : UserContext) (r : Register) (v : Word) : UserContext := + ⟨uc.userFpuState, Function.update uc.userRegs r v⟩ + +/-- The TCB `x` with register `r` of its user context set to `v`. -/ +def setReg (x : Tcb) (r : Register) (v : Word) : Tcb := + { x with tcbArch := archTcbContextSet (setRegUc (archTcbContextGet x.tcbArch) r v) x.tcbArch } + +theorem asUser_setRegister_eq {σ : State Z} {p : ObjRef} {x : Tcb} (r : Register) (v : Word) + (hk : σ.kheap p = some (.tcb x)) : + asUser (Z := Z) p (setRegister r v) σ = ({((), updK σ p (.tcb (setReg x r v)))}, False) := by + unfold asUser + show (NonDet.getsThe _ >>= _) σ = _ + rw [nd_bind_one (getTcb_eq hk)] + show ((pure _ : NonDet (State Z) UserContext) >>= _) σ = _ + rw [nd_pure_bind'] + show (NonDet.selectF _ >>= _) σ = _ + rw [nd_bind_one (show NonDet.selectF (σ := State Z) (setRegister r v (archTcbContextGet x.tcbArch)) σ = + ({(((), ⟨(archTcbContextGet x.tcbArch).userFpuState, + Function.update (archTcbContextGet x.tcbArch).userRegs r v⟩), σ)}, False) by + simp only [NonDet.selectF, setRegister, NonDet.modify] + ext ⟨a, b⟩ <;> simp)] + show ((pure _ : NonDet (State Z) Tcb) >>= _) σ = _ + rw [nd_pure_bind'] + exact (nd_bind_one (setObject_eq (o' := KernelObject.tcb (setReg x r v)) hk rfl) _).trans rfl + +/-- The outcome of `set_thread_state_act t`. -/ +def stsAct (σ : State Z) (t : ObjRef) : State Z := + if t = σ.curThread ∧ σ.schedulerAction = .resumeCurThread ∧ ¬ schedulable t σ = true + then { σ with schedulerAction := .chooseNewThread } else σ + +theorem setThreadStateAct_eq (σ : State Z) (t : ObjRef) : + setThreadStateAct (Z := Z) t σ = ({((), stsAct σ t)}, False) := by + unfold setThreadStateAct + simp only [nd_gets_bind] + unfold stsAct NonDet.when + split_ifs <;> rfl + +end + +end Fl4ProofsA64.P3.Notif diff --git a/proofs/Fl4ProofsA64/P3/NtfnWakeA.lean b/proofs/Fl4ProofsA64/P3/NtfnWakeA.lean new file mode 100644 index 000000000..469335044 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/NtfnWakeA.lean @@ -0,0 +1,531 @@ +/- +P3 (aarch64, A64-P3-IPC gen 3): **`send_signal` wakes a thread**, refining `Fl4SpecA64.sendSignal` (`Z = Unit`) +over the scheduler interface `SchedHypsA` / `SchedOpsA` (`P3/SchedOpsA`); x86 `P3/NtfnSched`'s wake-up and +`P3/NtfnWake`, ported (FL4's AArch64 `send_signal`, `set_thread_state`, `ntfn_dequeue` are the x86 text): + +* `set_thread_state_running`, `wake_refines`: the wake-up of a TCB in no notification queue (state word, then + `schedule_tcb`; the badge register `X0`; then the scheduler tail `rustWakeTail` ↔ `specWakeTail`); +* `send_signal_waiting`: on a waiting notification, the first waiter is dequeued (the notification becomes idle if + it was the last), set running, gets the badge, then the scheduler tail; +* `IpcCancelA.send_signal_idle_bound_recv`: a bound receiver is cancelled with the endpoint facts, then woken. + +From states related by `NtfnStateRel` and `SR`, the spec does not fail and every FL4 outcome is related +(`NtfnStateRel ∧ SR`) to one of the spec's. Case hypotheses only (`hw` / `hidle` / `hb` / `hrb`), the placement +`NPlaced`, and the interface; no named premise. + +(gen 7, ruling item 3) The woken thread is on the current core (`hon`, at the entry state: `SchedOpsA.wakeTail`'s +local-core precondition, carried to the tail by the law `SchedOpsA.coreKeep`). + +(gen 8, ruling Q1) The donation clause `DL s n` (`SchedOpsA`'s `donLocal`) is taken at the entry state (`hdl` of +`send_signal_waiting` / `send_signal_idle_bound_recv`) and carried to `wakeTail` by one `SchedOpsA.donLocalKeep` in +`wake_after_state`: the notification's SC word is kept (`nsc_word`: `NtfnRel1.sc` at both ends) because the spec keeps +its SC pointer on the way (`NScK`: the dequeue, the TCB writes, `set_thread_state_act`, and `cancel_ipc` of the bound +receiver, `cancelIpc_recv_nscK`, every outcome of which writes only the TCB, the endpoint and the reply: `NKeep`). + +Kernel cost: the wake-up is split at the spec state `set_thread_state` leaves (`wake_spine` / `wake_from_state` / +`wake_after_state`): checked as one term, the composite proof's kernel check ran away in memory (> 20 GiB; the +`stsAct` state names `specPtr t`); each part checks in a few GiB. +-/ +import Fl4ProofsA64.P3.SchedOpsA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Notif +open fl4_core_a64 Fl4ProofsA64.P1 + +noncomputable section + +/-- The spec's wake-up after the state and the badge register (stated for any object names). -/ +theorem specWake_eq {σ : Fl4SpecA64.State Unit} {t n : Fl4SpecA64.ObjRef} {x : Fl4SpecA64.Tcb} + (hk : σ.kheap t = some (.tcb x)) (badge : Fl4SpecA64.Word) : + specWake t n badge σ = specWakeTail t n (updK (stsAct (updK σ t (.tcb { x with tcbState := .running })) t) t + (.tcb (setReg { x with tcbState := .running } Fl4SpecA64.badge_register badge))) := by + have hk2 : (stsAct (updK σ t (.tcb { x with tcbState := .running })) t).kheap t = + some (.tcb { x with tcbState := .running }) := by + rw [stsAct_kheap, updK_kheap, Function.update_self] + unfold specWake + rw [nd_bind_one (setThreadState_eq _ hk), nd_bind_one (asUser_setRegister_eq _ _ hk2)] + +/-! ## (gen 8) The notifications' SC pointers along the spec's steps (the donation clause) -/ + +section NSc +open Fl4SpecA64 + +/-- (gen 8) Every notification of `σ` is one of `σ'`, with the same SC pointer. -/ +def NScK (σ σ' : State Unit) : Prop := + ∀ p z, σ.kheap p = some (.notification z) → ∃ z', σ'.kheap p = some (.notification z') ∧ z'.ntfnSc = z.ntfnSc + +theorem NScK.refl (σ : State Unit) : NScK σ σ := fun _ z h => ⟨z, h, rfl⟩ + +theorem NScK.trans {σ1 σ2 σ3 : State Unit} (h1 : NScK σ1 σ2) (h2 : NScK σ2 σ3) : NScK σ1 σ3 := fun p z h => by + obtain ⟨z2, h2', e2⟩ := h1 p z h + obtain ⟨z3, h3, e3⟩ := h2 p z2 h2' + exact ⟨z3, h3, e3.trans e2⟩ + +theorem NScK.of_kheap {σ σ' : State Unit} (h : σ'.kheap = σ.kheap) : NScK σ σ' := fun _ z hp => ⟨z, h ▸ hp, rfl⟩ + +/-- Writing over an object that is not a notification. -/ +theorem NScK.upd {σ : State Unit} {p : ObjRef} (hp : ∀ z, σ.kheap p ≠ some (.notification z)) (o : KernelObject) : + NScK σ (updK σ p o) := fun q z hq => by + have hqp : q ≠ p := fun e => hp z (e ▸ hq) + exact ⟨z, by rw [updK_kheap, Function.update_of_ne hqp, hq], rfl⟩ + +/-- Replacing a notification by one with the same SC pointer. -/ +theorem NScK.updN {σ : State Unit} {p : ObjRef} {x : Notification} (hp : σ.kheap p = some (.notification x)) + (y : Notification) (hy : y.ntfnSc = x.ntfnSc) : NScK σ (updK σ p (.notification y)) := fun q z hq => by + by_cases e : q = p + · subst e + rw [hp] at hq; cases hq + exact ⟨y, by rw [updK_kheap, Function.update_self], hy⟩ + · exact ⟨z, by rw [updK_kheap, Function.update_of_ne e, hq], rfl⟩ + +/-- (gen 8) Every outcome of `m` keeps `NScK`. -/ +def NKeep {α : Type} (m : SMonad Unit α) : Prop := ∀ σ r, r ∈ (m σ).1 → NScK σ r.2 + +theorem nk_bind {α β : Type} {m : SMonad Unit α} {f : α → SMonad Unit β} (hm : NKeep m) + (hf : ∀ a, NKeep (f a)) : NKeep (m >>= f) := fun σ r hr => by + rw [nd_bind_apply'] at hr + obtain ⟨x, hx, hr⟩ := hr + exact (hm σ x hx).trans (hf x.1 x.2 r hr) + +theorem nk_pure {α : Type} (a : α) : NKeep (pure a : SMonad Unit α) := fun σ r hr => by + have e : r = (a, σ) := hr + rw [e]; exact NScK.refl σ + +theorem nk_fail {α : Type} : NKeep (Fl4Spec.NonDet.fail : SMonad Unit α) := fun _ _ hr => hr.elim + +theorem nk_assert (P : Prop) : NKeep (Fl4Spec.NonDet.assert P : SMonad Unit Unit) := fun σ r hr => by + have e : P ∧ r = ((), σ) := hr + rw [e.2]; exact NScK.refl σ + +theorem nk_getsThe {α : Type} (f : State Unit → Option α) : NKeep (Fl4Spec.NonDet.getsThe f) := fun σ r hr => by + have h : r ∈ (Fl4Spec.NonDet.assertOpt (f σ) σ).1 := hr + cases e : f σ with + | none => rw [e] at h; exact h.elim + | some a => + rw [e] at h + have e' : r = (a, σ) := h + rw [e']; exact NScK.refl σ + +theorem nk_getSimpleKo {α : Type} [Nonempty α] (C : α → KernelObject) (p : ObjRef) : + NKeep (getSimpleKo (Z := Unit) C p) := by + unfold getSimpleKo + refine nk_bind (nk_getsThe _) fun k => nk_bind (nk_assert _) fun _ => ?_ + split + · exact nk_pure _ + · exact nk_fail + +/-- `set_object` never writes over a notification unless it writes one (`aType` checked). -/ +theorem nk_setObject {p : ObjRef} {o : KernelObject} (ho : aType o ≠ .aNTFN) : + NKeep (setObject (Z := Unit) p o) := fun σ r hr => by + cases hk : σ.kheap p with + | none => + unfold setObject at hr + rw [nd_bind_apply'] at hr + obtain ⟨x, hx, -⟩ := hr + have h : x ∈ (Fl4Spec.NonDet.assertOpt (σ.kheap p) σ).1 := hx + rw [hk] at h; exact h.elim + | some k => + by_cases ht : aType k = aType o + · rw [setObject_eq hk ht] at hr + have e : r = ((), updK σ p o) := hr + rw [e] + refine NScK.upd (fun z hz => ho ?_) o + rw [hk] at hz; cases hz; exact ht.symm + · unfold setObject at hr + rw [nd_bind_apply', getObject_eq hk] at hr + obtain ⟨x, hx, hr⟩ := hr + have e : x = (k, σ) := hx + subst e + rw [nd_bind_apply'] at hr + obtain ⟨y, hy, -⟩ := hr + have hy' : aType k = aType o ∧ y = ((), σ) := hy + exact absurd hy'.1 ht + +theorem nk_setSimpleKo {α : Type} [Nonempty α] (C : α → KernelObject) (p : ObjRef) (v : α) + (hC : aType (C v) ≠ .aNTFN) : NKeep (setSimpleKo (Z := Unit) C p v) := by + unfold setSimpleKo + exact nk_bind (nk_getsThe _) fun _ => nk_bind (nk_assert _) fun _ => + nk_bind (nk_assert _) fun _ => nk_setObject hC + +theorem nk_threadSet (f : Tcb → Tcb) (t : ObjRef) : NKeep (threadSet (Z := Unit) f t) := by + unfold threadSet + exact nk_bind (nk_getsThe _) fun _ => nk_setObject (by simp [aType]) + +theorem nk_threadGet {γ : Type} (f : Tcb → γ) (t : ObjRef) : NKeep (threadGet (Z := Unit) f t) := by + unfold threadGet + exact nk_bind (nk_getsThe _) fun _ => nk_pure _ + +theorem nk_setThreadState (t : ObjRef) (ts : ThreadState) : NKeep (setThreadState (Z := Unit) t ts) := by + unfold setThreadState + refine nk_bind (nk_threadSet _ _) fun _ σ r hr => ?_ + rw [setThreadStateAct_eq] at hr + have e : r = ((), stsAct σ t) := hr + rw [e]; exact NScK.of_kheap (stsAct_kheap σ t) + +theorem nk_tcbEpDequeue (t e : ObjRef) : NKeep (tcbEpDequeue (Z := Unit) t e) := by + unfold tcbEpDequeue + exact nk_bind (nk_getSimpleKo _ _) fun _ => nk_bind (nk_pure _) fun _ => + nk_bind (nk_assert _) fun _ => nk_bind (nk_pure _) fun _ => nk_bind (nk_pure _) fun _ => + nk_setSimpleKo _ _ _ (by simp [aType]) + +theorem nk_replyUnlinkTcb (t r : ObjRef) : NKeep (replyUnlinkTcb (Z := Unit) t r) := by + unfold replyUnlinkTcb + exact nk_bind (nk_getSimpleKo _ _) fun _ => nk_bind (nk_assert _) fun _ => + nk_bind (nk_threadGet _ _) fun _ => nk_bind (nk_assert _) fun _ => + nk_bind (nk_bind (nk_getSimpleKo _ _) fun _ => nk_setSimpleKo _ _ _ (by simp [aType])) fun _ => + nk_setThreadState _ _ + +theorem nk_blockedCancelIpc (ts : ThreadState) (t : ObjRef) (ro : Option ObjRef) : + NKeep (blockedCancelIpc (Z := Unit) ts t ro) := by + cases ro with + | none => + unfold blockedCancelIpc + first + | exact nk_bind (nk_getsThe _) fun _ => nk_bind (nk_tcbEpDequeue _ _) fun _ => nk_setThreadState _ _ + | exact nk_bind (nk_getsThe _) fun _ => nk_bind (nk_tcbEpDequeue _ _) fun _ => + nk_bind (nk_pure _) fun _ => nk_setThreadState _ _ + | some r => + unfold blockedCancelIpc + exact nk_bind (nk_getsThe _) fun _ => nk_bind (nk_tcbEpDequeue _ _) fun _ => + nk_bind (nk_replyUnlinkTcb _ _) fun _ => nk_setThreadState _ _ + +/-- (gen 8) **`cancel_ipc` of a receiver keeps every notification and its SC pointer** (it writes the TCB, the +endpoint and the reply only). -/ +theorem cancelIpc_recv_nscK {σ σ' : State Unit} {t : ObjRef} {x : Tcb} (hk : σ.kheap t = some (.tcb x)) + (hrb : receiveBlocked x.tcbState = true) (hc : cancelIpc (Z := Unit) t σ = ({((), σ')}, False)) : NScK σ σ' := by + obtain ⟨e, ro, d, hst⟩ : ∃ e ro d, x.tcbState = .blockedOnReceive e ro d := by + generalize x.tcbState = ts at hrb ⊢ + cases ts <;> first | exact ⟨_, _, _, rfl⟩ | simp [receiveBlocked] at hrb + have h1 : cancelIpc (Z := Unit) t σ = + blockedCancelIpc (Z := Unit) x.tcbState t ro (updK σ t (.tcb { x with tcbFault := none })) := by + unfold cancelIpc + rw [nd_bind_one (getThreadState_eq hk), nd_bind_one (threadSet_eq _ hk), hst] + have hmem : ((), σ') ∈ (cancelIpc (Z := Unit) t σ).1 := by rw [hc]; rfl + rw [h1] at hmem + exact (NScK.upd (fun z h => by rw [hk] at h; cases h) _).trans (nk_blockedCancelIpc _ _ _ _ _ hmem) + +end NSc + +theorem optRef_inj {a b : U64} (h : optRef a = optRef b) : a = b := by + by_cases ha : a = 0#u64 <;> by_cases hb : b = 0#u64 + · rw [ha, hb] + · simp [optRef, ha, hb] at h + · simp [optRef, ha, hb] at h + · simp only [optRef, ha, hb, ↓reduceIte, Option.some.injEq] at h + exact specPtr_inj h + +/-- (gen 8) **The notification's SC word is kept** between two related states whose spec states agree on its SC +pointer (`NtfnRel1.sc` at both ends). -/ +theorem nsc_word {N T : Set U64} {m m' : U64 → U64} {σ σ' : Fl4SpecA64.State Unit} (hR : NtfnStateRel N T m σ) + (hR' : NtfnStateRel N T m' σ') {n : U64} (hn : n ∈ N) (hK : NScK σ σ') : m' (nScA n) = m (nScA n) := by + obtain ⟨x, hx, hr⟩ := hR.ntfn n hn + obtain ⟨x', hx', hr'⟩ := hR'.ntfn n hn + obtain ⟨z', hz', e⟩ := hK _ _ hx + rw [hx'] at hz'; cases hz' + have h := hr'.sc + rw [e, hr.sc] at h + exact optRef_inj h.symm + +section Wake +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] {N T : Set U64} + {SR : S → Fl4SpecA64.State Unit → Prop} {DL : S → U64 → Prop} + +/-- (gen 5) The state word of a TCB in no notification queue, set to a state blocked on no notification (`code`), +for a step that blocks no thread: related, and `SR` kept. -/ +theorem set_thread_state_ntfn (hH : SchedHypsA st N T SR) (hO : SchedOpsA st ar N T SR DL) (hP : NPlaced T N) (s : S) + (σ : Fl4SpecA64.State Unit) (hR : NtfnStateRel N T (L.mem s) σ) (hSR : SR s σ) {t : U64} (ht : t ∈ T) + {x : Fl4SpecA64.Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hq : ∀ n ∈ N, ∀ z, σ.kheap (specPtr n) = some (.notification z) → (specPtr t) ∉ z.ntfnObj.ntfnQueue) + (ts : Fl4SpecA64.ThreadState) (hts : ∀ n, ts ≠ .blockedOnNotification n) (code : U64) + (hcode : code.val = tsCode ts) (hNB : Endp.NoBlockE σ (updK σ (specPtr t) (.tcb { x with tcbState := ts }))) : + sched.set_thread_state st s t code ⦃ s' => + NtfnStateRel N T (L.mem s') (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := ts })) (specPtr t)) ∧ + SR s' (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := ts })) (specPtr t)) ⦄ := by + unfold sched.set_thread_state + step + subst s1_post + have hx := hR.at_tcb ht hk + obtain ⟨hw, hoth⟩ := twr_wr (st := st) hP ht (K := 3328#u64) (Or.inl rfl) s code + rw [← stA_eq] at hw hoth + have hR1 : NtfnStateRel N T (L.mem (L.wr s (stA t) code)) + (updK σ (specPtr t) (.tcb { x with tcbState := ts })) := by + refine hR.upd_tcb ht _ hw.ntfn hw.tcb hw.links (Or.inl hq) ⟨?_, ?_, ?_, ?_⟩ + · rw [rd_wr_eq (foot_st hP ht).1]; exact hcode + · intro n hn; exact absurd hn (hts n) + · show (L.mem _ (badgeRegA t)).bv = badgeOf x + rw [badgeRegA_eq, hoth _ (by simp) (by simp) (by simp), ← badgeRegA_eq]; exact hx.badge + · show (L.mem _ (prioA t)).val = x.tcbPriority.toNat + rw [prioA_eq', hoth _ (by simp) (by simp) (by simp), ← prioA_eq']; exact hx.prio + have hSR1 := hH.stable _ _ _ _ hSR hw.foot (NtfnStep.tcb hk ts x.tcbArch) hNB + refine WP.spec_mono (hO.scheduleTcb _ _ t hR1 hSR1 ht) ?_ + rintro s2 ⟨hf, hS⟩ + exact ⟨hR1.frame hP hf (KView.stsAct _ _), hS⟩ + +/-- The state word of a TCB in no notification queue, set to running: +related, and `SR` kept. -/ +theorem set_thread_state_running (hH : SchedHypsA st N T SR) (hO : SchedOpsA st ar N T SR DL) (hP : NPlaced T N) (s : S) + (σ : Fl4SpecA64.State Unit) (hR : NtfnStateRel N T (L.mem s) σ) (hSR : SR s σ) {t : U64} (ht : t ∈ T) + {x : Fl4SpecA64.Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hq : ∀ n ∈ N, ∀ z, σ.kheap (specPtr n) = some (.notification z) → (specPtr t) ∉ z.ntfnObj.ntfnQueue) + (hcls : inClassR x.tcbState x.tcbFault ∨ RelIfBound σ (specPtr t)) (hni : x.tcbState ≠ .idleThreadState) : + sched.set_thread_state st s t sched.TS_RUNNING ⦃ s' => + NtfnStateRel N T (L.mem s') (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := .running })) (specPtr t)) ∧ + SR s' (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := .running })) (specPtr t)) ⦄ := + set_thread_state_ntfn hH hO hP s σ hR hSR ht hk hq .running (fun _ h => by cases h) sched.TS_RUNNING + (by rw [tsVals.1]; rfl) (Endp.NoBlockE.updT hk _ (Or.inr (Or.inl ⟨rfl, hni, hcls⟩))) + +/-- The wake-up after the state word: the badge register, then the scheduler tail (`σ2`: the spec state after +`set_thread_state t Running`). -/ +theorem wake_after_state (hH : SchedHypsA st N T SR) (hO : SchedOpsA st ar N T SR DL) (hP : NPlaced T N) (s2 : S) + (a : A) (σ2 : Fl4SpecA64.State Unit) (hR2 : NtfnStateRel N T (L.mem s2) σ2) (hSR2 : SR s2 σ2) {t n : U64} + (ht : t ∈ T) (hn : n ∈ N) {x : Fl4SpecA64.Tcb} (hk2 : σ2.kheap (specPtr t) = some (.tcb x)) + (hrun : x.tcbState = .running) (hNQ : NotQueued σ2 (specPtr t)) (badge : U64) + (hon : L.mem s2 (affA t) = L.cpu s2) {s0 : S} {σ0 : Fl4SpecA64.State Unit} + (hR0 : NtfnStateRel N T (L.mem s0) σ0) (hSR0 : SR s0 σ0) (hdl : DL s0 n) (hK : NScK σ0 σ2) : + (do + let s3 ← obj.Tcb.set_reg st t s2 layout.reg.BADGE badge + rustWakeTail st ar s3 a t n) ⦃ r => + ¬ (specWakeTail (specPtr t) (specPtr n) (updK σ2 (specPtr t) (.tcb (setReg x Fl4SpecA64.badge_register + badge.bv)))).2 ∧ + ∃ σ', ((), σ') ∈ (specWakeTail (specPtr t) (specPtr n) (updK σ2 (specPtr t) (.tcb (setReg x + Fl4SpecA64.badge_register badge.bv)))).1 ∧ NtfnStateRel N T (L.mem r.1) σ' ∧ SR r.1 σ' ⦄ := by + step + subst s3_post + obtain ⟨hR3, hW3, hS3⟩ := set_badge_reg_rel hP hR2 ht hk2 badge + have hSR3 := hH.stable _ _ _ _ hSR2 hW3 hS3 (Endp.NoBlockE.tcbK hk2 (setReg x Fl4SpecA64.badge_register badge.bv) rfl rfl) + have hk3 : (updK σ2 (specPtr t) (.tcb (setReg x Fl4SpecA64.badge_register badge.bv))).kheap (specPtr t) = + some (.tcb (setReg x Fl4SpecA64.badge_register badge.bv)) := by rw [updK_kheap, Function.update_self] + have hNQ3 : NotQueued (updK σ2 (specPtr t) (.tcb (setReg x Fl4SpecA64.badge_register badge.bv))) (specPtr t) := + hNQ + have hK3 : NScK σ0 (updK σ2 (specPtr t) (.tcb (setReg x Fl4SpecA64.badge_register badge.bv))) := + hK.trans (NScK.upd (fun z h => by rw [hk2] at h; cases h) _) + refine WP.spec_mono (hO.wakeTail _ a _ t n _ hR3 hSR3 ht hn hk3 hrun hNQ3 + ((hO.coreKeep _ _ _ _ hSR2 hSR3).onCore ht hon) + (hO.donLocalKeep _ _ _ _ n hn hR0 hR3 hSR0 hSR3 (nsc_word hR0 hR3 hn hK3) hdl)) ?_ + rintro r ⟨hnf, σ', hmem, hf, hv, hS⟩ + exact ⟨hnf, σ', hmem, hR3.frame hP hf hv, hS⟩ + +/-- The wake-up after `set_thread_state t Running`, at the spec state that operation leaves (its `NotQueued` and +TCB facts from `σ`'s). -/ +theorem wake_from_state (hH : SchedHypsA st N T SR) (hO : SchedOpsA st ar N T SR DL) (hP : NPlaced T N) (s : S) (a : A) + (σ : Fl4SpecA64.State Unit) (hSR : SR s σ) {t n : U64} (ht : t ∈ T) + (hn : n ∈ N) {x : Fl4SpecA64.Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) (badge : U64) + (hnr : Fl4SpecA64.runnable x.tcbState = false) (hon : L.mem s (affA t) = L.cpu s) {s0 : S} + {σ0 : Fl4SpecA64.State Unit} (hR0 : NtfnStateRel N T (L.mem s0) σ0) (hSR0 : SR s0 σ0) (hdl : DL s0 n) + (hK : NScK σ0 σ) (s2 : S) + (hR2 : NtfnStateRel N T (L.mem s2) (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := .running })) + (specPtr t))) + (hSR2 : SR s2 (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := .running })) (specPtr t))) : + (do + let s3 ← obj.Tcb.set_reg st t s2 layout.reg.BADGE badge + rustWakeTail st ar s3 a t n) ⦃ r => + ¬ (specWakeTail (specPtr t) (specPtr n) (updK (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := .running })) + (specPtr t)) (specPtr t) (.tcb (setReg { x with tcbState := .running } Fl4SpecA64.badge_register badge.bv)))).2 ∧ + ∃ σ', ((), σ') ∈ (specWakeTail (specPtr t) (specPtr n) (updK (stsAct (updK σ (specPtr t) + (.tcb { x with tcbState := .running })) (specPtr t)) (specPtr t) (.tcb (setReg { x with tcbState := .running } + Fl4SpecA64.badge_register badge.bv)))).1 ∧ NtfnStateRel N T (L.mem r.1) σ' ∧ SR r.1 σ' ⦄ := by + have h0 := hH.notQueued s σ t x hSR hk hnr + have hNQ : NotQueued (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := .running })) (specPtr t)) (specPtr t) := + ⟨fun d p => by rw [stsAct_readyQueues]; exact h0.1 d p, by rw [stsAct_releaseQueue]; exact h0.2⟩ + have hk2 : (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := .running })) (specPtr t)).kheap (specPtr t) = + some (.tcb { x with tcbState := .running }) := by rw [stsAct_kheap, updK_kheap, Function.update_self] + exact wake_after_state hH hO hP s2 a _ hR2 hSR2 ht hn hk2 rfl hNQ badge + ((hO.coreKeep _ _ _ _ hSR hSR2).onCore ht hon) hR0 hSR0 hdl + (hK.trans ((NScK.upd (fun z h => by rw [hk] at h; cases h) _).trans (NScK.of_kheap (stsAct_kheap _ _)))) + +/-- The wake-up up to the state word, with the rest (`wake_from_state`) as a parameter: a separate declaration, so +the kernel checks the two halves independently (checked together, the composite term's check ran away in memory). -/ +theorem wake_spine (hH : SchedHypsA st N T SR) (hO : SchedOpsA st ar N T SR DL) (hP : NPlaced T N) (s : S) (a : A) + (σ : Fl4SpecA64.State Unit) (hR : NtfnStateRel N T (L.mem s) σ) (hSR : SR s σ) {t n : U64} (ht : t ∈ T) + {x : Fl4SpecA64.Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hq : ∀ n ∈ N, ∀ z, σ.kheap (specPtr n) = some (.notification z) → (specPtr t) ∉ z.ntfnObj.ntfnQueue) (badge : U64) + (hcls : inClassR x.tcbState x.tcbFault ∨ RelIfBound σ (specPtr t)) + (hni : x.tcbState ≠ .idleThreadState) + (hcut : ∀ s2, NtfnStateRel N T (L.mem s2) (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := .running })) + (specPtr t)) → SR s2 (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := .running })) (specPtr t)) → + (do + let s3 ← obj.Tcb.set_reg st t s2 layout.reg.BADGE badge + rustWakeTail st ar s3 a t n) ⦃ r => + ¬ (specWakeTail (specPtr t) (specPtr n) (updK (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := .running })) + (specPtr t)) (specPtr t) (.tcb (setReg { x with tcbState := .running } Fl4SpecA64.badge_register badge.bv)))).2 ∧ + ∃ σ', ((), σ') ∈ (specWakeTail (specPtr t) (specPtr n) (updK (stsAct (updK σ (specPtr t) + (.tcb { x with tcbState := .running })) (specPtr t)) (specPtr t) (.tcb (setReg { x with tcbState := .running } + Fl4SpecA64.badge_register badge.bv)))).1 ∧ NtfnStateRel N T (L.mem r.1) σ' ∧ SR r.1 σ' ⦄) : + rustWake st ar s a t n badge ⦃ r => ¬ (specWake (specPtr t) (specPtr n) badge.bv σ).2 ∧ + ∃ σ', ((), σ') ∈ (specWake (specPtr t) (specPtr n) badge.bv σ).1 ∧ NtfnStateRel N T (L.mem r.1) σ' ∧ SR r.1 σ' ⦄ := by + rw [specWake_eq hk badge.bv] + unfold rustWake + refine WP.spec_bind (set_thread_state_running hH hO hP s σ hR hSR ht hk hq hcls hni) ?_ + rintro s2 ⟨hR2, hSR2⟩ + exact hcut s2 hR2 hSR2 + +/-- The wake-up of a TCB in no notification queue refines the spec's. -/ +theorem wake_refines (hH : SchedHypsA st N T SR) (hO : SchedOpsA st ar N T SR DL) (hP : NPlaced T N) (s : S) (a : A) + (σ : Fl4SpecA64.State Unit) (hR : NtfnStateRel N T (L.mem s) σ) (hSR : SR s σ) {t n : U64} (ht : t ∈ T) + (hn : n ∈ N) {x : Fl4SpecA64.Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hq : ∀ n ∈ N, ∀ z, σ.kheap (specPtr n) = some (.notification z) → (specPtr t) ∉ z.ntfnObj.ntfnQueue) (badge : U64) + (hcls : inClassR x.tcbState x.tcbFault ∨ RelIfBound σ (specPtr t)) (hnr : Fl4SpecA64.runnable x.tcbState = false) + (hni : x.tcbState ≠ .idleThreadState) (hon : L.mem s (affA t) = L.cpu s) {s0 : S} + {σ0 : Fl4SpecA64.State Unit} (hR0 : NtfnStateRel N T (L.mem s0) σ0) (hSR0 : SR s0 σ0) (hdl : DL s0 n) + (hK : NScK σ0 σ) : + rustWake st ar s a t n badge ⦃ r => ¬ (specWake (specPtr t) (specPtr n) badge.bv σ).2 ∧ + ∃ σ', ((), σ') ∈ (specWake (specPtr t) (specPtr n) badge.bv σ).1 ∧ NtfnStateRel N T (L.mem r.1) σ' ∧ SR r.1 σ' ⦄ := + wake_spine hH hO hP s a σ hR hSR ht hk hq badge hcls hni + (fun s2 hR2 hSR2 => wake_from_state hH hO hP s a σ hSR ht hn hk badge hnr hon hR0 hSR0 hdl hK s2 hR2 hSR2) + +end Wake + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] {N T : Set U64} + {SR : S → Fl4SpecA64.State Unit → Prop} {DL : S → U64 → Prop} + +/-- The spec's notification after dequeuing `d` from the waiting queue +`xs`. -/ +def afterDequeue (x : Fl4SpecA64.Notification) (d : U64) : Fl4SpecA64.Notification := + Fl4SpecA64.ntfnObjUpdate (fun _ => + match Fl4SpecA64.removeAll (specPtr d) (Fl4SpecA64.ntfnQueueOf x) with + | [] => Fl4SpecA64.Ntfn.idleNtfn + | _ => Fl4SpecA64.Ntfn.waitingNtfn (Fl4SpecA64.removeAll (specPtr d) (Fl4SpecA64.ntfnQueueOf x))) x + +/-- After `ntfn_dequeue` of the head of a waiting notification: related to +the spec's `tcb_ntfn_dequeue`, and the dequeued TCB is in no queue. -/ +theorem rel_after_dequeue {s s1 : S} {σ : Fl4SpecA64.State Unit} + (hR : NtfnStateRel N T (L.mem s) σ) {n : U64} (hn : n ∈ N) {x : Fl4SpecA64.Notification} + (hk : σ.kheap (specPtr n) = some (.notification x)) {xs : List U64} (hxs : x.ntfnObj = .waitingNtfn (xs.map specPtr)) + (hQ : QueueRel (L.mem s) T .Ep (qOf (L.mem s) n) xs) (hB : ∀ y ∈ xs, BlockedOn (L.mem s) n y) + {d : U64} (hd : d ∈ xs) + (h1 : QueueRel (L.mem s1) T .Ep (qOf (L.mem s1) n) (xs.erase d)) + (h2 : stOf (L.mem s1) n = (if xs.erase d = [] then 0#u64 else stOf (L.mem s) n)) + (h3 : QKeeps st N T n xs s s1) (hst : stOf (L.mem s) n = 1#u64) : + NtfnStateRel N T (L.mem s1) (updK σ (specPtr n) (.notification (afterDequeue x d))) ∧ + ∀ n' ∈ N, ∀ z, (updK σ (specPtr n) (.notification (afterDequeue x d))).kheap (specPtr n') = some (.notification z) → + (specPtr d) ∉ z.ntfnObj.ntfnQueue := by + have hx := hR.at_ntfn hn hk + have hrm : Fl4SpecA64.removeAll (specPtr d) (Fl4SpecA64.ntfnQueueOf x) = (xs.erase d).map specPtr := by + show Fl4SpecA64.removeAll (specPtr d) x.ntfnObj.ntfnQueue = _ + rw [hxs]; exact removeAll_map hQ.nodup d + have hobj : NtfnObjRel (L.mem s1) T n (afterDequeue x d).ntfnObj := by + show NtfnObjRel _ T n (match Fl4SpecA64.removeAll (specPtr d) (Fl4SpecA64.ntfnQueueOf x) with + | [] => Fl4SpecA64.Ntfn.idleNtfn + | _ => Fl4SpecA64.Ntfn.waitingNtfn (Fl4SpecA64.removeAll (specPtr d) (Fl4SpecA64.ntfnQueueOf x))) + rw [hrm] + cases he : xs.erase d with + | nil => + rw [he] at h1 h2 + exact ⟨by rw [h2]; rfl, h1⟩ + | cons y ys => + rw [he] at h1 h2 + simp only [List.map_cons] + refine ⟨by rw [h2]; simpa using hst, y :: ys, List.cons_ne_nil _ _, rfl, h1, fun z hz => ?_⟩ + have hz' : z ∈ xs := by + have : z ∈ xs.erase d := by rw [he]; exact hz + exact List.mem_of_mem_erase this + obtain ⟨e1, e2⟩ := hB z hz' + exact ⟨(h3.tcb z (hQ.mem z hz')).st ▸ e1, (h3.tcb z (hQ.mem z hz')).blk ▸ e2⟩ + have hR1 := hR.upd_ntfn hn (afterDequeue x d) h3.ntfn h3.tcb + (fun y hy hb => h3.links y hy (fun hyx => hb (hB y hyx).2)) + ⟨hobj, by rw [h3.bound]; exact hx.bound, by rw [h3.bound]; exact hx.bound_mem, + by rw [h3.sc]; exact hx.sc⟩ + refine ⟨hR1, fun n' hn' z hz hdz => ?_⟩ + by_cases e : n' = n + · subst e + simp only [updK_kheap, Function.update_self, Option.some.injEq, Fl4SpecA64.KernelObject.notification.injEq] at hz + subst hz + have := hobj + revert hdz + show (specPtr d) ∉ (match Fl4SpecA64.removeAll (specPtr d) (Fl4SpecA64.ntfnQueueOf x) with + | [] => Fl4SpecA64.Ntfn.idleNtfn + | _ => Fl4SpecA64.Ntfn.waitingNtfn (Fl4SpecA64.removeAll (specPtr d) (Fl4SpecA64.ntfnQueueOf x))).ntfnQueue + rw [hrm] + have hnd : d ∉ xs.erase d := hQ.nodup.not_mem_erase + cases he : xs.erase d with + | nil => simp [Fl4SpecA64.Ntfn.ntfnQueue] + | cons y ys => + rw [he] at hnd + simp only [List.map_cons, Fl4SpecA64.Ntfn.ntfnQueue] + intro hm + rw [← List.map_cons] at hm + obtain ⟨w, hw, hwd⟩ := List.mem_map.1 hm + exact hnd (specPtr_inj hwd ▸ hw) + · have hz0 : σ.kheap (specPtr n') = some (.notification z) := by + simpa [updK_kheap, bv_inj e] using hz + obtain ⟨-, -, hb'⟩ := (hR.at_ntfn hn' hz0).obj.blocked hdz + exact e (hb'.symm.trans (hB d hd).2) + +theorem the_head {α : Type} [Nonempty α] (a : α) (l : List α) : Fl4SpecA64.the (a :: l).head? = a := rfl + +/-- **Waiting**: the first waiter is woken with the badge. -/ +theorem send_signal_waiting (hH : SchedHypsA st N T SR) (hO : SchedOpsA st ar N T SR DL) (hP : NPlaced T N) (s : S) (a : A) + (σ : Fl4SpecA64.State Unit) (hR : NtfnStateRel N T (L.mem s) σ) (hSR : SR s σ) {n : U64} (hn : n ∈ N) + {x : Fl4SpecA64.Notification} (hk : σ.kheap (specPtr n) = some (.notification x)) {q : List Fl4SpecA64.ObjRef} + (hw : x.ntfnObj = .waitingNtfn q) (badge : U64) + (hon : ∀ d ∈ T, specPtr d ∈ q → L.mem s (affA d) = L.cpu s) (hdl : DL s n) : + notification.send_signal st ar s a n badge ⦃ r => ¬ (Fl4SpecA64.sendSignal (specPtr n) badge.bv σ).2 ∧ + ∃ σ', ((), σ') ∈ (Fl4SpecA64.sendSignal (specPtr n) badge.bv σ).1 ∧ NtfnStateRel N T (L.mem r.1) σ' ∧ SR r.1 σ' ⦄ := by + have hx := hR.at_ntfn hn hk + have hobj := hx.obj + rw [hw] at hobj + obtain ⟨hst, xs, hne, rfl, hQ, hB⟩ := hobj + obtain ⟨d, rest, rfl⟩ := List.exists_cons_of_ne_nil hne + have hd : d ∈ d :: rest := List.mem_cons_self + have hdT : d ∈ T := hQ.mem d hd + obtain ⟨xd, hkd, -⟩ := hR.tcb d hdT + -- the spec + have hmem : (specPtr d) ∈ Fl4SpecA64.ntfnQueueOf x := by + show (specPtr d) ∈ x.ntfnObj.ntfnQueue + rw [hw]; simp [Fl4SpecA64.Ntfn.ntfnQueue] + set σ1 := updK σ (specPtr n) (.notification (afterDequeue x d)) + have hspec : Fl4SpecA64.sendSignal (specPtr n) badge.bv σ = specWake (specPtr d) (specPtr n) badge.bv σ1 := by + unfold Fl4SpecA64.sendSignal + rw [nd_bind_one (getNotification_eq hk)] + simp only [hw] + unfold Fl4SpecA64.updateWaitingNtfn + rw [nd_bind_one (assert_true (by simp) σ), nd_pure_bind', List.map_cons, the_head, + nd_bind_one (tcbNtfnDequeue_eq hk hmem)] + rfl + rw [hspec] + -- FL4 + unfold notification.send_signal + step + have hsw : st = obj.NTFN_WAITING := by rw [st_post]; exact stOf_waiting (q := _) hx.obj hw + simp only [hsw, ntfn_consts.1, ntfn_consts.2.1, show (1#u64 : U64) ≠ 0#u64 by decide, ↓reduceIte] + step + have hqh : q.head = d := by rw [q_post, epQ_eq, hQ.head]; rfl + rw [hqh] + refine WP.spec_bind (ntfn_dequeue_full hP s hn d _ hQ hd) ?_ + rintro s1 ⟨h1, h2, h3⟩ + obtain ⟨hR1, hq1⟩ := rel_after_dequeue hR hn hk hw hQ hB hd h1 h2 h3 hst + have hSR1 : SR s1 σ1 := hH.stable _ _ _ _ hSR h3.foot (NtfnStep.ntfnK (y := afterDequeue x d) hk rfl rfl) + (Endp.NoBlockE.ntfnK _ _ _) + have hk1 : σ1.kheap (specPtr d) = some (.tcb xd) := by + have : (specPtr d) ≠ (specPtr n) := by intro e; rw [e, hk] at hkd; cases hkd + simp [σ1, this, hkd] + have hcls : inClassR xd.tcbState xd.tcbFault ∨ RelIfBound σ1 (specPtr d) := by + left; right; left + have h6 := (hB d hd).1 + have := (hR.at_tcb hdT hkd).st + rw [h6] at this + exact tsCode_six' this.symm + have hnr : Fl4SpecA64.runnable xd.tcbState = false := by + obtain ⟨m, hm⟩ : ∃ m, xd.tcbState = .blockedOnNotification m := by + have h6 := (hB d hd).1 + have := (hR.at_tcb hdT hkd).st + rw [h6] at this + exact tsCode_six' this.symm + rw [hm]; rfl + have hni : xd.tcbState ≠ .idleThreadState := by + obtain ⟨m, hm⟩ : ∃ m, xd.tcbState = .blockedOnNotification m := by + have h6 := (hB d hd).1 + have := (hR.at_tcb hdT hkd).st + rw [h6] at this + exact tsCode_six' this.symm + rw [hm]; exact fun h => by cases h + exact wake_refines hH hO hP s1 a σ1 hR1 hSR1 hdT hn hk1 hq1 badge hcls hnr hni + ((hO.coreKeep _ _ _ _ hSR hSR1).onCore hdT (hon d hdT (by simp))) hR hSR hdl + (NScK.updN hk (afterDequeue x d) rfl) + +end + +end + +end Fl4ProofsA64.P3.Notif diff --git a/proofs/Fl4ProofsA64/P3/NtfnWakeFootA.lean b/proofs/Fl4ProofsA64/P3/NtfnWakeFootA.lean new file mode 100644 index 000000000..3280fef8e --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/NtfnWakeFootA.lean @@ -0,0 +1,262 @@ +/- +P3 (aarch64, A64-P3 Stage-3 integration, D8/T1 step 2): **the footprint twins of the notification wake chain** +(`set_thread_state_ntfn`, `set_thread_state_running`, `wake_after_state`, `wake_from_state`, `wake_spine`, +`wake_refines`, `send_signal_waiting`), generated from `NtfnWakeA` by `WS/handoff/tools/gen_ntfnwakefoot.py`: the +same proofs with `hF : SchedFootNA`, `WP.spec_and` at each scheduler operation and `FpNA` in the post. The ratified +statements are unchanged (the originals have other users). + +No named premise. +-/ +import Fl4ProofsA64.P3.NtfnWakeA +import Fl4ProofsA64.P3.SchedFootNA +import Fl4ProofsA64.P3.EpFootHelpA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Notif +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4ProofsA64.P3.Sched + +noncomputable section + +theorem ssz_updK_ntfn (σ : Fl4SpecA64.State Unit) (p : Fl4SpecA64.ObjRef) (x : Fl4SpecA64.Notification) : + SSzA σ (updK σ p (.notification x)) := fun q y n h => by + rw [updK_kheap] at h + by_cases e : q = p + · subst e; rw [Function.update_self] at h; cases h + · rw [Function.update_of_ne e] at h; exact ⟨y, h⟩ + +section Wake +open Fl4SpecA64 +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] {N T : Set U64} + {SR : S → Fl4SpecA64.State Unit → Prop} {DL : S → U64 → Prop} + {Wr : Fl4SpecA64.State Unit → S → U64 → Prop} + +theorem set_thread_state_ntfnF (hH : SchedHypsA st N T SR) (hO : SchedOpsA st ar N T SR DL) (hF : SchedFootNA st ar N T SR DL Wr) (hP : NPlaced T N) (s : S) + (σ : Fl4SpecA64.State Unit) (hR : NtfnStateRel N T (L.mem s) σ) (hSR : SR s σ) {t : U64} (ht : t ∈ T) + {x : Fl4SpecA64.Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hq : ∀ n ∈ N, ∀ z, σ.kheap (specPtr n) = some (.notification z) → (specPtr t) ∉ z.ntfnObj.ntfnQueue) + (ts : Fl4SpecA64.ThreadState) (hts : ∀ n, ts ≠ .blockedOnNotification n) (code : U64) + (hcode : code.val = tsCode ts) (hNB : Endp.NoBlockE σ (updK σ (specPtr t) (.tcb { x with tcbState := ts }))) : + sched.set_thread_state st s t code ⦃ s' => + NtfnStateRel N T (L.mem s') (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := ts })) (specPtr t)) ∧ + SR s' (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := ts })) (specPtr t)) ∧ + FpNA st N T Wr σ s s' ⦄ := by + unfold sched.set_thread_state + step with obj.Tcb.set_state.spec as ⟨s1, s1_post⟩ + subst s1_post + have hx := hR.at_tcb ht hk + obtain ⟨hw, hoth⟩ := twr_wr (st := st) hP ht (K := 3328#u64) (Or.inl rfl) s code + rw [← stA_eq] at hw hoth + have hR1 : NtfnStateRel N T (L.mem (L.wr s (stA t) code)) + (updK σ (specPtr t) (.tcb { x with tcbState := ts })) := by + refine hR.upd_tcb ht _ hw.ntfn hw.tcb hw.links (Or.inl hq) ⟨?_, ?_, ?_, ?_⟩ + · rw [rd_wr_eq (foot_st hP ht).1]; exact hcode + · intro n hn; exact absurd hn (hts n) + · show (L.mem _ (badgeRegA t)).bv = badgeOf x + rw [badgeRegA_eq, hoth _ (by simp) (by simp) (by simp), ← badgeRegA_eq]; exact hx.badge + · show (L.mem _ (prioA t)).val = x.tcbPriority.toNat + rw [prioA_eq', hoth _ (by simp) (by simp) (by simp), ← prioA_eq']; exact hx.prio + have hSR1 := hH.stable _ _ _ _ hSR hw.foot (NtfnStep.tcb hk ts x.tcbArch) hNB + refine WP.spec_mono (WP.spec_and (hO.scheduleTcb _ _ t hR1 hSR1 ht) (hF.scheduleTcb _ _ t hR1 hSR1 ht)) ?_ + rintro s2 ⟨⟨hf, hS⟩, hw2⟩ + exact ⟨hR1.frame hP hf (KView.stsAct _ _), hS, + FpNA.trans hF.back (FpNA.of_nw hw.foot) (Endp.ssz_updK_tcb σ _ _) (FpNA.of_wr hw2)⟩ + +theorem set_thread_state_runningF (hH : SchedHypsA st N T SR) (hO : SchedOpsA st ar N T SR DL) (hF : SchedFootNA st ar N T SR DL Wr) (hP : NPlaced T N) (s : S) + (σ : Fl4SpecA64.State Unit) (hR : NtfnStateRel N T (L.mem s) σ) (hSR : SR s σ) {t : U64} (ht : t ∈ T) + {x : Fl4SpecA64.Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hq : ∀ n ∈ N, ∀ z, σ.kheap (specPtr n) = some (.notification z) → (specPtr t) ∉ z.ntfnObj.ntfnQueue) + (hcls : inClassR x.tcbState x.tcbFault ∨ RelIfBound σ (specPtr t)) (hni : x.tcbState ≠ .idleThreadState) : + sched.set_thread_state st s t sched.TS_RUNNING ⦃ s' => + NtfnStateRel N T (L.mem s') (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := .running })) (specPtr t)) ∧ + SR s' (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := .running })) (specPtr t)) ∧ + FpNA st N T Wr σ s s' ⦄ := + set_thread_state_ntfnF hH hO hF hP s σ hR hSR ht hk hq .running (fun _ h => by cases h) sched.TS_RUNNING + (by rw [tsVals.1]; rfl) (Endp.NoBlockE.updT hk _ (Or.inr (Or.inl ⟨rfl, hni, hcls⟩))) + +theorem wake_after_stateF (hH : SchedHypsA st N T SR) (hO : SchedOpsA st ar N T SR DL) (hF : SchedFootNA st ar N T SR DL Wr) (hP : NPlaced T N) (s2 : S) + (a : A) (σ2 : Fl4SpecA64.State Unit) (hR2 : NtfnStateRel N T (L.mem s2) σ2) (hSR2 : SR s2 σ2) {t n : U64} + (ht : t ∈ T) (hn : n ∈ N) {x : Fl4SpecA64.Tcb} (hk2 : σ2.kheap (specPtr t) = some (.tcb x)) + (hrun : x.tcbState = .running) (hNQ : NotQueued σ2 (specPtr t)) (badge : U64) + (hon : L.mem s2 (affA t) = L.cpu s2) {s0 : S} {σ0 : Fl4SpecA64.State Unit} + (hR0 : NtfnStateRel N T (L.mem s0) σ0) (hSR0 : SR s0 σ0) (hdl : DL s0 n) (hK : NScK σ0 σ2) : + (do + let s3 ← obj.Tcb.set_reg st t s2 layout.reg.BADGE badge + rustWakeTail st ar s3 a t n) ⦃ r => + ¬ (specWakeTail (specPtr t) (specPtr n) (updK σ2 (specPtr t) (.tcb (setReg x Fl4SpecA64.badge_register + badge.bv)))).2 ∧ + ∃ σ', ((), σ') ∈ (specWakeTail (specPtr t) (specPtr n) (updK σ2 (specPtr t) (.tcb (setReg x + Fl4SpecA64.badge_register badge.bv)))).1 ∧ NtfnStateRel N T (L.mem r.1) σ' ∧ SR r.1 σ' ∧ FpNA st N T Wr σ2 s2 r.1 ⦄ := by + step with Endp.obj.Tcb.set_reg.spec as ⟨s3, s3_post⟩ + subst s3_post + obtain ⟨hR3, hW3, hS3⟩ := set_badge_reg_rel hP hR2 ht hk2 badge + have hSR3 := hH.stable _ _ _ _ hSR2 hW3 hS3 (Endp.NoBlockE.tcbK hk2 (setReg x Fl4SpecA64.badge_register badge.bv) rfl rfl) + have hk3 : (updK σ2 (specPtr t) (.tcb (setReg x Fl4SpecA64.badge_register badge.bv))).kheap (specPtr t) = + some (.tcb (setReg x Fl4SpecA64.badge_register badge.bv)) := by rw [updK_kheap, Function.update_self] + have hNQ3 : NotQueued (updK σ2 (specPtr t) (.tcb (setReg x Fl4SpecA64.badge_register badge.bv))) (specPtr t) := + hNQ + have hK3 : NScK σ0 (updK σ2 (specPtr t) (.tcb (setReg x Fl4SpecA64.badge_register badge.bv))) := + hK.trans (NScK.upd (fun z h => by rw [hk2] at h; cases h) _) + have hcore := (hO.coreKeep _ _ _ _ hSR2 hSR3).onCore ht hon + have hdl3 := hO.donLocalKeep _ _ _ _ n hn hR0 hR3 hSR0 hSR3 (nsc_word hR0 hR3 hn hK3) hdl + refine WP.spec_mono (WP.spec_and (hO.wakeTail _ a _ t n _ hR3 hSR3 ht hn hk3 hrun hNQ3 hcore hdl3) + (hF.wakeTail _ a _ t n _ hR3 hSR3 ht hn hk3 hrun hNQ3 hcore hdl3)) ?_ + rintro r ⟨⟨hnf, σ', hmem, hf, hv, hS⟩, hw⟩ + exact ⟨hnf, σ', hmem, hR3.frame hP hf hv, hS, + FpNA.trans hF.back (FpNA.of_nw hW3) (Endp.ssz_updK_tcb σ2 _ _) (FpNA.of_wr hw)⟩ + +theorem wake_from_stateF (hH : SchedHypsA st N T SR) (hO : SchedOpsA st ar N T SR DL) (hF : SchedFootNA st ar N T SR DL Wr) (hP : NPlaced T N) (s : S) (a : A) + (σ : Fl4SpecA64.State Unit) (hSR : SR s σ) {t n : U64} (ht : t ∈ T) + (hn : n ∈ N) {x : Fl4SpecA64.Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) (badge : U64) + (hnr : Fl4SpecA64.runnable x.tcbState = false) (hon : L.mem s (affA t) = L.cpu s) {s0 : S} + {σ0 : Fl4SpecA64.State Unit} (hR0 : NtfnStateRel N T (L.mem s0) σ0) (hSR0 : SR s0 σ0) (hdl : DL s0 n) + (hK : NScK σ0 σ) (s2 : S) + (hR2 : NtfnStateRel N T (L.mem s2) (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := .running })) + (specPtr t))) + (hSR2 : SR s2 (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := .running })) (specPtr t))) : + (do + let s3 ← obj.Tcb.set_reg st t s2 layout.reg.BADGE badge + rustWakeTail st ar s3 a t n) ⦃ r => + ¬ (specWakeTail (specPtr t) (specPtr n) (updK (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := .running })) + (specPtr t)) (specPtr t) (.tcb (setReg { x with tcbState := .running } Fl4SpecA64.badge_register badge.bv)))).2 ∧ + ∃ σ', ((), σ') ∈ (specWakeTail (specPtr t) (specPtr n) (updK (stsAct (updK σ (specPtr t) + (.tcb { x with tcbState := .running })) (specPtr t)) (specPtr t) (.tcb (setReg { x with tcbState := .running } + Fl4SpecA64.badge_register badge.bv)))).1 ∧ NtfnStateRel N T (L.mem r.1) σ' ∧ SR r.1 σ' ∧ + FpNA st N T Wr (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := .running })) (specPtr t)) s2 r.1 ⦄ := by + have h0 := hH.notQueued s σ t x hSR hk hnr + have hNQ : NotQueued (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := .running })) (specPtr t)) (specPtr t) := + ⟨fun d p => by rw [stsAct_readyQueues]; exact h0.1 d p, by rw [stsAct_releaseQueue]; exact h0.2⟩ + have hk2 : (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := .running })) (specPtr t)).kheap (specPtr t) = + some (.tcb { x with tcbState := .running }) := by rw [stsAct_kheap, updK_kheap, Function.update_self] + exact wake_after_stateF hH hO hF hP s2 a _ hR2 hSR2 ht hn hk2 rfl hNQ badge + ((hO.coreKeep _ _ _ _ hSR hSR2).onCore ht hon) hR0 hSR0 hdl + (hK.trans ((NScK.upd (fun z h => by rw [hk] at h; cases h) _).trans (NScK.of_kheap (stsAct_kheap _ _)))) + +theorem wake_spineF (hH : SchedHypsA st N T SR) (hO : SchedOpsA st ar N T SR DL) (hF : SchedFootNA st ar N T SR DL Wr) (hP : NPlaced T N) (s : S) (a : A) + (σ : Fl4SpecA64.State Unit) (hR : NtfnStateRel N T (L.mem s) σ) (hSR : SR s σ) {t n : U64} (ht : t ∈ T) + {x : Fl4SpecA64.Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hq : ∀ n ∈ N, ∀ z, σ.kheap (specPtr n) = some (.notification z) → (specPtr t) ∉ z.ntfnObj.ntfnQueue) (badge : U64) + (hcls : inClassR x.tcbState x.tcbFault ∨ RelIfBound σ (specPtr t)) + (hni : x.tcbState ≠ .idleThreadState) + (hcut : ∀ s2, NtfnStateRel N T (L.mem s2) (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := .running })) + (specPtr t)) → SR s2 (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := .running })) (specPtr t)) → + (do + let s3 ← obj.Tcb.set_reg st t s2 layout.reg.BADGE badge + rustWakeTail st ar s3 a t n) ⦃ r => + ¬ (specWakeTail (specPtr t) (specPtr n) (updK (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := .running })) + (specPtr t)) (specPtr t) (.tcb (setReg { x with tcbState := .running } Fl4SpecA64.badge_register badge.bv)))).2 ∧ + ∃ σ', ((), σ') ∈ (specWakeTail (specPtr t) (specPtr n) (updK (stsAct (updK σ (specPtr t) + (.tcb { x with tcbState := .running })) (specPtr t)) (specPtr t) (.tcb (setReg { x with tcbState := .running } + Fl4SpecA64.badge_register badge.bv)))).1 ∧ NtfnStateRel N T (L.mem r.1) σ' ∧ SR r.1 σ' ∧ + FpNA st N T Wr (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := .running })) (specPtr t)) s2 r.1 ⦄) : + rustWake st ar s a t n badge ⦃ r => ¬ (specWake (specPtr t) (specPtr n) badge.bv σ).2 ∧ + ∃ σ', ((), σ') ∈ (specWake (specPtr t) (specPtr n) badge.bv σ).1 ∧ NtfnStateRel N T (L.mem r.1) σ' ∧ SR r.1 σ' ∧ + FpNA st N T Wr σ s r.1 ⦄ := by + rw [specWake_eq hk badge.bv] + unfold rustWake + refine WP.spec_bind (set_thread_state_runningF hH hO hF hP s σ hR hSR ht hk hq hcls hni) ?_ + rintro s2 ⟨hR2, hSR2, hfp2⟩ + refine WP.spec_mono (hcut s2 hR2 hSR2) ?_ + rintro r ⟨hnf, σ', hm, h1, h2, h3⟩ + exact ⟨hnf, σ', hm, h1, h2, + FpNA.trans hF.back hfp2 ((Endp.ssz_updK_tcb σ _ _).trans (Endp.ssz_stsAct _ _)) h3⟩ + +theorem wake_refinesF (hH : SchedHypsA st N T SR) (hO : SchedOpsA st ar N T SR DL) (hF : SchedFootNA st ar N T SR DL Wr) (hP : NPlaced T N) (s : S) (a : A) + (σ : Fl4SpecA64.State Unit) (hR : NtfnStateRel N T (L.mem s) σ) (hSR : SR s σ) {t n : U64} (ht : t ∈ T) + (hn : n ∈ N) {x : Fl4SpecA64.Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hq : ∀ n ∈ N, ∀ z, σ.kheap (specPtr n) = some (.notification z) → (specPtr t) ∉ z.ntfnObj.ntfnQueue) (badge : U64) + (hcls : inClassR x.tcbState x.tcbFault ∨ RelIfBound σ (specPtr t)) (hnr : Fl4SpecA64.runnable x.tcbState = false) + (hni : x.tcbState ≠ .idleThreadState) (hon : L.mem s (affA t) = L.cpu s) {s0 : S} + {σ0 : Fl4SpecA64.State Unit} (hR0 : NtfnStateRel N T (L.mem s0) σ0) (hSR0 : SR s0 σ0) (hdl : DL s0 n) + (hK : NScK σ0 σ) : + rustWake st ar s a t n badge ⦃ r => ¬ (specWake (specPtr t) (specPtr n) badge.bv σ).2 ∧ + ∃ σ', ((), σ') ∈ (specWake (specPtr t) (specPtr n) badge.bv σ).1 ∧ NtfnStateRel N T (L.mem r.1) σ' ∧ SR r.1 σ' ∧ + FpNA st N T Wr σ s r.1 ⦄ := + wake_spineF hH hO hF hP s a σ hR hSR ht hk hq badge hcls hni + (fun s2 hR2 hSR2 => wake_from_stateF hH hO hF hP s a σ hSR ht hn hk badge hnr hon hR0 hSR0 hdl hK s2 hR2 hSR2) + +end Wake + +section +open Fl4SpecA64 +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] {N T : Set U64} + {SR : S → Fl4SpecA64.State Unit → Prop} {DL : S → U64 → Prop} + {Wr : Fl4SpecA64.State Unit → S → U64 → Prop} + +theorem send_signal_waitingF (hH : SchedHypsA st N T SR) (hO : SchedOpsA st ar N T SR DL) (hF : SchedFootNA st ar N T SR DL Wr) (hP : NPlaced T N) (s : S) (a : A) + (σ : Fl4SpecA64.State Unit) (hR : NtfnStateRel N T (L.mem s) σ) (hSR : SR s σ) {n : U64} (hn : n ∈ N) + {x : Fl4SpecA64.Notification} (hk : σ.kheap (specPtr n) = some (.notification x)) {q : List Fl4SpecA64.ObjRef} + (hw : x.ntfnObj = .waitingNtfn q) (badge : U64) + (hon : ∀ d ∈ T, specPtr d ∈ q → L.mem s (affA d) = L.cpu s) (hdl : DL s n) : + notification.send_signal st ar s a n badge ⦃ r => ¬ (Fl4SpecA64.sendSignal (specPtr n) badge.bv σ).2 ∧ + ∃ σ', ((), σ') ∈ (Fl4SpecA64.sendSignal (specPtr n) badge.bv σ).1 ∧ NtfnStateRel N T (L.mem r.1) σ' ∧ SR r.1 σ' ∧ + FpNA st N T Wr σ s r.1 ⦄ := by + have hx := hR.at_ntfn hn hk + have hobj := hx.obj + rw [hw] at hobj + obtain ⟨hst, xs, hne, rfl, hQ, hB⟩ := hobj + obtain ⟨d, rest, rfl⟩ := List.exists_cons_of_ne_nil hne + have hd : d ∈ d :: rest := List.mem_cons_self + have hdT : d ∈ T := hQ.mem d hd + obtain ⟨xd, hkd, -⟩ := hR.tcb d hdT + -- the spec + have hmem : (specPtr d) ∈ Fl4SpecA64.ntfnQueueOf x := by + show (specPtr d) ∈ x.ntfnObj.ntfnQueue + rw [hw]; simp [Fl4SpecA64.Ntfn.ntfnQueue] + set σ1 := updK σ (specPtr n) (.notification (afterDequeue x d)) + have hspec : Fl4SpecA64.sendSignal (specPtr n) badge.bv σ = specWake (specPtr d) (specPtr n) badge.bv σ1 := by + unfold Fl4SpecA64.sendSignal + rw [nd_bind_one (getNotification_eq hk)] + simp only [hw] + unfold Fl4SpecA64.updateWaitingNtfn + rw [nd_bind_one (assert_true (by simp) σ), nd_pure_bind', List.map_cons, the_head, + nd_bind_one (tcbNtfnDequeue_eq hk hmem)] + rfl + rw [hspec] + -- FL4 + unfold notification.send_signal + step with obj.Ntfn.state.spec as ⟨nst, nst_post⟩ + have hsw : nst = obj.NTFN_WAITING := by rw [nst_post]; exact stOf_waiting (q := _) hx.obj hw + simp only [hsw, ntfn_consts.1, ntfn_consts.2.1, show (1#u64 : U64) ≠ 0#u64 by decide, ↓reduceIte] + step with notification.get_queue.spec as ⟨q, q_post⟩ + have hqh : q.head = d := by rw [q_post, epQ_eq, hQ.head]; rfl + rw [hqh] + refine WP.spec_bind (ntfn_dequeue_full hP s hn d _ hQ hd) ?_ + rintro s1 ⟨h1, h2, h3⟩ + obtain ⟨hR1, hq1⟩ := rel_after_dequeue hR hn hk hw hQ hB hd h1 h2 h3 hst + have hSR1 : SR s1 σ1 := hH.stable _ _ _ _ hSR h3.foot (NtfnStep.ntfnK (y := afterDequeue x d) hk rfl rfl) + (Endp.NoBlockE.ntfnK _ _ _) + have hk1 : σ1.kheap (specPtr d) = some (.tcb xd) := by + have : (specPtr d) ≠ (specPtr n) := by intro e; rw [e, hk] at hkd; cases hkd + simp [σ1, this, hkd] + have hcls : inClassR xd.tcbState xd.tcbFault ∨ RelIfBound σ1 (specPtr d) := by + left; right; left + have h6 := (hB d hd).1 + have := (hR.at_tcb hdT hkd).st + rw [h6] at this + exact tsCode_six' this.symm + have hnr : Fl4SpecA64.runnable xd.tcbState = false := by + obtain ⟨m, hm⟩ : ∃ m, xd.tcbState = .blockedOnNotification m := by + have h6 := (hB d hd).1 + have := (hR.at_tcb hdT hkd).st + rw [h6] at this + exact tsCode_six' this.symm + rw [hm]; rfl + have hni : xd.tcbState ≠ .idleThreadState := by + obtain ⟨m, hm⟩ : ∃ m, xd.tcbState = .blockedOnNotification m := by + have h6 := (hB d hd).1 + have := (hR.at_tcb hdT hkd).st + rw [h6] at this + exact tsCode_six' this.symm + rw [hm]; exact fun h => by cases h + refine WP.spec_mono (wake_refinesF hH hO hF hP s1 a σ1 hR1 hSR1 hdT hn hk1 hq1 badge hcls hnr hni + ((hO.coreKeep _ _ _ _ hSR hSR1).onCore hdT (hon d hdT (by simp))) hR hSR hdl + (NScK.updN hk (afterDequeue x d) rfl)) ?_ + rintro r ⟨hnf, σ', hm, h1', h2', h3'⟩ + exact ⟨hnf, σ', hm, h1', h2', + FpNA.trans hF.back (FpNA.of_nw h3.foot) (ssz_updK_ntfn σ (specPtr n) (afterDequeue x d)) h3'⟩ + +end + +end + +end Fl4ProofsA64.P3.Notif diff --git a/proofs/Fl4ProofsA64/P3/NtfnWriteA.lean b/proofs/Fl4ProofsA64/P3/NtfnWriteA.lean new file mode 100644 index 000000000..652be8785 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/NtfnWriteA.lean @@ -0,0 +1,163 @@ +/- +P3 (aarch64, A64-P3-IPC gen 1, ported from x86), notifications: what single word writes leave alone. + +A write to a word of the notification `n` leaves the other notifications' +words and all TCB words alone; a write to a field of the TCB `t` leaves the +notifications' words, the other TCBs' words and `t`'s other fields alone +(`NPlaced`: notifications are 64-byte aligned, TCBs 4 KiB aligned, and +the two do not overlap). +-/ +import Fl4ProofsA64.P3.NtfnSpecA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Notif +open fl4_core_a64 Fl4ProofsA64.P1 + +noncomputable section + +section +variable {S : Type _} {st : machine.Store S} [L : LawfulStore st] {N T : Set U64} + +theorem rd_wr_ne {s : S} {a c v : U64} (ha : Aligned a) (hc : Aligned c) (h : a ≠ c) : + L.mem (L.wr s a v) c = L.mem s c := + L.mem_wr_ne ha hc (fun e => h (UScalar.eq_of_val_eq e)) + +theorem rd_wr_eq {s : S} {a v : U64} (ha : Aligned a) : L.mem (L.wr s a v) a = v := L.mem_wr_same ha + +/-! ## Agreement is transitive -/ + +theorem NWordsEq.trans {m₁ m₂ m₃ : U64 → U64} {n : U64} (h₁ : NWordsEq m₁ m₂ n) (h₂ : NWordsEq m₂ m₃ n) : + NWordsEq m₁ m₃ n := + ⟨h₂.head.trans h₁.head, h₂.ts.trans h₁.ts, h₂.badge.trans h₁.badge, h₂.bound.trans h₁.bound, h₂.sc.trans h₁.sc⟩ +theorem TFieldsEq.trans {m₁ m₂ m₃ : U64 → U64} {t : U64} (h₁ : TFieldsEq m₁ m₂ t) (h₂ : TFieldsEq m₂ m₃ t) : + TFieldsEq m₁ m₃ t := + ⟨h₂.st.trans h₁.st, h₂.blk.trans h₁.blk, h₂.badge.trans h₁.badge, h₂.prio.trans h₁.prio⟩ +theorem TLinksEq.trans {m₁ m₂ m₃ : U64 → U64} {t : U64} (h₁ : TLinksEq m₁ m₂ t) (h₂ : TLinksEq m₂ m₃ t) : + TLinksEq m₁ m₃ t := + ⟨h₂.1.trans h₁.1, h₂.2.trans h₁.2⟩ +theorem NWordsEq.rfl' {m : U64 → U64} {n : U64} : NWordsEq m m n := ⟨rfl, rfl, rfl, rfl, rfl⟩ +theorem TFieldsEq.rfl' {m : U64 → U64} {t : U64} : TFieldsEq m m t := ⟨rfl, rfl, rfl, rfl⟩ +theorem TLinksEq.rfl' {m : U64 → U64} {t : U64} : TLinksEq m m t := ⟨rfl, rfl⟩ + +/-! ## Writes to notification words -/ + +/-- What a write to word `K` of the notification `n` leaves alone. -/ +structure NWr (st : machine.Store S) [L : LawfulStore st] (N T : Set U64) (n : U64) (s s' : S) : Prop where + ntfn : ∀ n' ∈ N, n' ≠ n → NWordsEq (L.mem s) (L.mem s') n' + tcb : ∀ y ∈ T, TFieldsEq (L.mem s) (L.mem s') y ∧ TLinksEq (L.mem s) (L.mem s') y + foot : WritesIn st s s' (NWrite N T) + +theorem NWr.trans {n : U64} {s₁ s₂ s₃ : S} (h₁ : NWr st N T n s₁ s₂) (h₂ : NWr st N T n s₂ s₃) : + NWr st N T n s₁ s₃ := + ⟨fun n' hn' e => (h₁.ntfn n' hn' e).trans (h₂.ntfn n' hn' e), + fun y hy => ⟨(h₁.tcb y hy).1.trans (h₂.tcb y hy).1, (h₁.tcb y hy).2.trans (h₂.tcb y hy).2⟩, + (h₁.foot.trans h₂.foot).mono (fun _ h => by rcases h with h | h <;> exact h)⟩ + +theorem nwr_wr (hP : NPlaced T N) {n K : U64} (hn : n ∈ N) (hK : K.val < 40) (hK8 : K.val % 8 = 0) + (s : S) (v : U64) : NWr st N T n s (L.wr s (core.num.U64.wrapping_add n K) v) := by + have hna := hP.ntfn n hn + obtain ⟨ha, hf⟩ := nword hP hn hK hK8 + have hnn : ∀ n' ∈ N, n' ≠ n → ∀ K' : U64, K'.val < 40 → K'.val % 8 = 0 → + L.mem (L.wr s (core.num.U64.wrapping_add n K) v) (core.num.U64.wrapping_add n' K') = + L.mem s (core.num.U64.wrapping_add n' K') := by + intro n' hn' e K' h1 h2 + refine rd_wr_ne ha (nword hP hn' h1 h2).1 ?_ + rw [Ne, nn_eq_iff hna (hP.ntfn n' hn') (by omega) (by omega)] + exact fun h => e h.1.symm + have htn : ∀ y ∈ T, ∀ K' : U64, K'.val < 4096 → K'.val % 8 = 0 → + L.mem (L.wr s (core.num.U64.wrapping_add n K) v) (core.num.U64.wrapping_add y K') = + L.mem s (core.num.U64.wrapping_add y K') := by + intro y hy K' h1 h2 + exact rd_wr_ne ha (tword hP hy h1 h2) (tn_ne (hP.tcb y hy) hna (hP.disj n hn y hy) h1 (by omega)).symm + have hf' : NWrite N T (core.num.U64.wrapping_add n K) := by + refine Or.inl ⟨n, hn, ?_⟩ + rw [nfield_val hna (by omega)]; omega + refine ⟨fun n' hn' e => ?_, fun y hy => ?_, (writesIn_wr ha).mono fun b hb => hb ▸ hf'⟩ + · refine ⟨?_, ?_, ?_, ?_, ?_⟩ + · rw [epHeadA_eq]; exact hnn n' hn' e _ (by simp) (by simp) + · rw [epTSA_eq]; exact hnn n' hn' e _ (by simp) (by simp) + · rw [nBadgeA_eq]; exact hnn n' hn' e _ (by simp) (by simp) + · rw [nBoundA_eq]; exact hnn n' hn' e _ (by simp) (by simp) + · rw [nScA_eq]; exact hnn n' hn' e _ (by simp) (by simp) + · refine ⟨⟨?_, ?_, ?_, ?_⟩, ?_, ?_⟩ + · rw [stA_eq]; exact htn y hy _ (by simp) (by simp) + · rw [tBlockA_eq]; exact htn y hy _ (by simp) (by simp) + · rw [badgeRegA_eq]; exact htn y hy _ (by simp) (by simp) + · rw [prioA_eq']; exact htn y hy _ (by simp) (by simp) + · rw [prevEp_eq]; exact htn y hy _ (by simp) (by simp) + · rw [nextEp_eq]; exact htn y hy _ (by simp) (by simp) + +/-- Words of one notification are distinct. -/ +theorem nwords_ne {n K1 K2 : U64} (hn : NAddr n) (h1 : K1.val < 64) (h2 : K2.val < 64) (hne : K1 ≠ K2) : + core.num.U64.wrapping_add n K1 ≠ core.num.U64.wrapping_add n K2 := by + rw [Ne, nn_eq_iff hn hn h1 h2]; exact fun h => hne h.2 + +/-! ## Writes to TCB fields -/ + +/-- What a write to a field (not a link) of the TCB `t` leaves alone. -/ +structure TWr (st : machine.Store S) [L : LawfulStore st] (N T : Set U64) (t : U64) (s s' : S) : Prop where + ntfn : ∀ n ∈ N, NWordsEq (L.mem s) (L.mem s') n + tcb : ∀ y ∈ T, y ≠ t → TFieldsEq (L.mem s) (L.mem s') y + links : ∀ y ∈ T, TLinksEq (L.mem s) (L.mem s') y + foot : WritesIn st s s' (NWrite N T) + +theorem TWr.trans {t : U64} {s₁ s₂ s₃ : S} (h₁ : TWr st N T t s₁ s₂) (h₂ : TWr st N T t s₂ s₃) : + TWr st N T t s₁ s₃ := + ⟨fun n hn => (h₁.ntfn n hn).trans (h₂.ntfn n hn), + fun y hy e => (h₁.tcb y hy e).trans (h₂.tcb y hy e), + fun y hy => (h₁.links y hy).trans (h₂.links y hy), + (h₁.foot.trans h₂.foot).mono (fun _ h => by rcases h with h | h <;> exact h)⟩ + +/-- A write to field `K` of the TCB `t` (one of the state, block-object and +badge-register words). -/ +theorem twr_wr (hP : NPlaced T N) {t K : U64} (ht : t ∈ T) + (hK : K = 3328#u64 ∨ K = 3336#u64 ∨ K = 256#u64) (s : S) (v : U64) : + TWr st N T t s (L.wr s (core.num.U64.wrapping_add t K) v) ∧ + ∀ K' : U64, K'.val < 4096 → K'.val % 8 = 0 → K' ≠ K → + L.mem (L.wr s (core.num.U64.wrapping_add t K) v) (core.num.U64.wrapping_add t K') = + L.mem s (core.num.U64.wrapping_add t K') := by + have hta := hP.tcb t ht + have hK1 : K.val < 4096 := by rcases hK with rfl | rfl | rfl <;> simp + have hK8 : K.val % 8 = 0 := by rcases hK with rfl | rfl | rfl <;> simp + have ha := tword hP ht hK1 hK8 + have hf : NWrite N T (core.num.U64.wrapping_add t K) := by + refine Or.inr ⟨t, ht, ?_⟩ + rcases hK with rfl | rfl | rfl + · exact Or.inl (stA_eq t).symm + · exact Or.inr (Or.inl (tBlockA_eq t).symm) + · exact Or.inr (Or.inr (Or.inl (badgeRegA_eq t).symm)) + have htt : ∀ y ∈ T, ∀ K' : U64, K'.val < 4096 → K'.val % 8 = 0 → (y = t → K' ≠ K) → + L.mem (L.wr s (core.num.U64.wrapping_add t K) v) (core.num.U64.wrapping_add y K') = + L.mem s (core.num.U64.wrapping_add y K') := by + intro y hy K' h1 h2 hne + refine rd_wr_ne ha (tword hP hy h1 h2) ?_ + rw [Ne, tt_eq_iff hta (hP.tcb y hy) hK1 h1] + rintro ⟨rfl, rfl⟩; exact hne rfl rfl + have hnt : ∀ n ∈ N, ∀ K' : U64, K'.val < 40 → K'.val % 8 = 0 → + L.mem (L.wr s (core.num.U64.wrapping_add t K) v) (core.num.U64.wrapping_add n K') = + L.mem s (core.num.U64.wrapping_add n K') := by + intro n hn K' h1 h2 + exact rd_wr_ne ha (nword hP hn h1 h2).1 (tn_ne hta (hP.ntfn n hn) (hP.disj n hn t ht) hK1 (by omega)) + have hl : ∀ K' : U64, K' = 3496#u64 ∨ K' = 3504#u64 → K' ≠ K := by + rintro K' (rfl | rfl) <;> rcases hK with rfl | rfl | rfl <;> simp + refine ⟨⟨fun n hn => ?_, fun y hy e => ?_, fun y hy => ?_, (writesIn_wr ha).mono fun b hb => hb ▸ hf⟩, + fun K' h1 h2 hne => htt t ht K' h1 h2 (fun _ => hne)⟩ + · refine ⟨?_, ?_, ?_, ?_, ?_⟩ + · rw [epHeadA_eq]; exact hnt n hn _ (by simp) (by simp) + · rw [epTSA_eq]; exact hnt n hn _ (by simp) (by simp) + · rw [nBadgeA_eq]; exact hnt n hn _ (by simp) (by simp) + · rw [nBoundA_eq]; exact hnt n hn _ (by simp) (by simp) + · rw [nScA_eq]; exact hnt n hn _ (by simp) (by simp) + · refine ⟨?_, ?_, ?_, ?_⟩ + · rw [stA_eq]; exact htt y hy _ (by simp) (by simp) (fun h => absurd h e) + · rw [tBlockA_eq]; exact htt y hy _ (by simp) (by simp) (fun h => absurd h e) + · rw [badgeRegA_eq]; exact htt y hy _ (by simp) (by simp) (fun h => absurd h e) + · rw [prioA_eq']; exact htt y hy _ (by simp) (by simp) (fun h => absurd h e) + · exact ⟨by rw [prevEp_eq]; exact htt y hy _ (by simp) (by simp) (fun _ => hl _ (Or.inr rfl)), + by rw [nextEp_eq]; exact htt y hy _ (by simp) (by simp) (fun _ => hl _ (Or.inl rfl))⟩ + +end + +end + +end Fl4ProofsA64.P3.Notif diff --git a/proofs/Fl4ProofsA64/P3/OuterEntryA.lean b/proofs/Fl4ProofsA64/P3/OuterEntryA.lean new file mode 100644 index 000000000..9f31b3352 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/OuterEntryA.lean @@ -0,0 +1,1462 @@ +import Fl4ProofsA64.P3.ScScheduleA +import Fl4ProofsA64.P3.FrontEndExtraA +import Fl4ProofsA64.P3.IpcScReplyTransferA +import Fl4ProofsA64.P1.FastDecline + +/-! +The serial AArch64 entry shell on the combined JOIN and MCS base. + +* [P] Reuse the frozen MCS clock, budget, timer and root agreements. Supply + the full saved fault words and the actual IRQ take/classification with + its architecture successor, independently of the earlier pending probe. +* [S] Use one admitted local serial view. The fast-enabled syscall theorem + uses P1 fast-hit agreement and concrete decline. The native vector adapter assumes + the SVC, FP, guest and other early branches have been classified separately. +* [J] Project typed slots at actual lookups, the invocation result and + reached IPC continuations, and MCS budget and finish records at their + own successors. Runnable, active-outgoing and inactive-outgoing + preemption phases each use their own projections. +-/ + +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3.OuterEntry + +open Fl4Spec Fl4SpecA64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4ProofsA64.P3.Sched + +noncomputable section + +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +private theorem withReachedA {α : Type} {program : Result α} {post : α → Prop} + (h : program ⦃ post ⦄) : program ⦃ r => program = ok r ∧ post r ⦄ := by + obtain ⟨r, eq, hr⟩ := spec_existsA h + rw [eq, WP.spec_ok] + exact ⟨rfl, hr⟩ + +/-- A CSpace lookup used by receive yields a related held cap or the +precise lookup fault. The other concrete exception constructors cannot +be returned by this lookup. -/ +def RecvLookupRelA : core.result.Result capability.Cap error.Exc → Except LookupFailure Cap → Prop + | .Ok c, .ok cap => CapRelA c cap + | .Err (.Lookup lf), .error fault => LookupFaultRelA lf fault + | _, _ => False + +theorem lookup_cap_recv_refinesA (th : TypedHeapA) (s : S) (σ : State Unit) + (hR : StateRelA th (L.mem s) σ) (hV : SlotsValidA th (L.mem s)) (t : U64) + (ht : th t = some .tcb) (cptr : U64) : + cspace.lookup_cap st s t cptr ⦃ r => + SingleA ((lookupCap (Z := Unit) (specPtr t) (toBl cptr.bv)).run σ) σ (RecvLookupRelA r) ⦄ := by + unfold cspace.lookup_cap + refine WP.spec_bind (lookup_slot_refinesA th s σ hR hV t ht cptr) ?_ + rintro result ⟨e, he, hrel, hslot⟩ + cases result with + | Ok result => + cases e with + | error _ => exact hrel.elim + | ok found => + obtain ⟨slot, rest⟩ := found + obtain ⟨haddr, -⟩ := hrel + obtain ⟨p, hp, hkind, -⟩ := hslot + obtain ⟨cap, hcap, hget⟩ := hR.slot_capA hp hkind + dsimp only + rw [Fl4ProofsA64.P1.W7.R.ST.capA_eq, bind_ok, WP.spec_ok, haddr] + refine ⟨.ok cap, ?_, hcap⟩ + unfold lookupCap + rw [run_bind_okA he] + exact run_liftEA hget + | Err lf => + cases e with + | ok _ => exact hrel.elim + | error fault => + rw [WP.spec_ok] + exact ⟨.error fault, by unfold lookupCap; rw [run_bind_errorA he], hrel⟩ + +private theorem recvLookupFaultA {m : LFMonad Unit Cap} {σ : State Unit} {e : LookupFailure} + (h : m.run σ = ({(.error e, σ)}, False)) (p : Word) : + (capFaultOnFailure p true m).run σ = ({(.error (.capFault p true e), σ)}, False) := by + unfold capFaultOnFailure + rw [run_handleE_errA h] + rfl + +private theorem recvLookupCapA {m : LFMonad Unit Cap} {σ : State Unit} {cap : Cap} + (h : m.run σ = ({(.ok cap, σ)}, False)) (p : Word) : + (capFaultOnFailure p true m).run σ = ({(.ok cap, σ)}, False) := + run_handleE_okA h + +private theorem recvLiftMemA {α : Type} {σ τ : State Unit} {v : α} {f : SMonad Unit α} + (h : (v, τ) ∈ (f σ).1) : (.ok v, τ) ∈ ((NonDetE.liftE (ε := Fault) f).run σ).1 := by + unfold NonDetE.liftE + rw [ExceptT.run_lift] + exact mem_bindA.2 ⟨(v, τ), h, rfl⟩ + +private theorem capRegisterReadA {σ : State Unit} {t : U64} {x : Tcb} + (hcur : σ.curThread = specPtr t) (hk : σ.kheap (specPtr t) = some (.tcb x)) (r : Register) : + getCapReg (Z := Unit) r σ = ({(toBl (x.tcbArch.tcbContext.userRegs r), σ)}, False) := by + unfold getCapReg NonDet.liftM + rw [nd_gets_bindSc, hcur, nd_bind_singleA' (asUser_getRegister_eqA hk r)] + rfl + +/-- [J] The selected IPC branch has its queue, reply, notification and +fault-delivery projections. They are supplied at the state of receive, +including after an earlier invocation for a combined syscall. -/ +structure RecvBranchesA (Rel : S → A → State Unit → Prop) (t : U64) (blocking : Bool) + (s : S) (a : A) (σ : State Unit) : Prop where + endpoint : ∀ p b send grant grantReply rc, + cspace.lookup_cap st s t (L.mem s (regWA t layout.reg.CAP)) = + ok (.Ok (.Endpoint p b send true grant grantReply)) → + (rc = .Null ∨ ∃ r g, rc = .Reply r g ∧ + cspace.lookup_cap st s t (L.mem s (regWA t layout.reg.REPLY)) = ok (.Ok rc)) → + endpoint.receive_ipc st ar s a t (.Endpoint p b send true grant grantReply) blocking rc ⦃ r => + ∃ τ, ((), τ) ∈ (receiveIpc (Z := Unit) (specPtr t) + (absCapA (.Endpoint p b send true grant grantReply)) blocking (absCapA rc) σ).1 ∧ Rel r.1 r.2 τ ⦄ + bound : ∀ p b send, cspace.lookup_cap st s t (L.mem s (regWA t layout.reg.CAP)) = + ok (.Ok (.Notification p b send true)) → + getNtfnObjRef (Z := Unit) Notification.ntfnBoundTcb (specPtr p) σ = + ({(optRefA (L.mem s (Notif.nBoundA p)), σ)}, False) + signal : ∀ p b send, cspace.lookup_cap st s t (L.mem s (regWA t layout.reg.CAP)) = + ok (.Ok (.Notification p b send true)) → + notification.receive_signal st ar s a t p blocking ⦃ r => + ∃ τ, ((), τ) ∈ (receiveSignal (Z := Unit) (specPtr t) + (absCapA (.Notification p b send true)) blocking σ).1 ∧ Rel r.1 r.2 τ ⦄ + fault : ∀ ptr lf e, LookupFaultRelA lf e → + faults.handle_fault st ar s a t (.Cap ptr true lf) ⦃ r => + ∃ τ, ((), τ) ∈ (handleFault (Z := Unit) (specPtr t) (.capFault ptr.bv true e) σ).1 ∧ Rel r.1 r.2 τ ⦄ + +set_option maxHeartbeats 4500000 in +/-- Receive's two register reads and CSpace lookups refine the port. The +selected IPC or fault operation supplies the joint successor. -/ +theorem handle_recv_refinesA (Rel : S → A → State Unit → Prop) (th : TypedHeapA) (s : S) + (a : A) (σ : State Unit) (blocking canReply : Bool) (t : U64) + (ht : th t = some .tcb) (hct : sched.cur_thread st s = ok t) (hcur : σ.curThread = specPtr t) + (hR : StateRelA th (L.mem s) σ) (hV : SlotsValidA th (L.mem s)) + {x : Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + (capReg : (L.mem s (regWA t layout.reg.CAP)).bv = x.tcbArch.tcbContext.userRegs cap_register) + (replyReg : (L.mem s (regWA t layout.reg.REPLY)).bv = x.tcbArch.tcbContext.userRegs replyRegister) + (leaves : RecvBranchesA (st := st) (ar := ar) Rel t blocking s a σ) : + syscall.handle_recv st ar s a blocking canReply ⦃ r => + ∃ τ, ((), τ) ∈ (handleRecv (Z := Unit) blocking canReply σ).1 ∧ Rel r.1 r.2 τ ⦄ := by + let cp := L.mem s (regWA t layout.reg.CAP) + let rp := L.mem s (regWA t layout.reg.REPLY) + have capRead : NonDet.liftM dataToCptr (asUser (Z := Unit) (specPtr t) (getRegister cap_register)) σ = + ({(toBl cp.bv, σ)}, False) := by + unfold NonDet.liftM + rw [nd_bind_singleA' (asUser_getRegister_eqA hk _), ← capReg] + rfl + have replyRead : getCapReg (Z := Unit) replyRegister σ = ({(toBl rp.bv, σ)}, False) := by + rw [capRegisterReadA hcur hk, ← replyReg] + have currentRead : (NonDet.gets fun s : State Unit => s.curThread) σ = + ({(specPtr t, σ)}, False) := by + change ({(σ.curThread, σ)}, False) = _ + rw [hcur] + have miss : LookupFaultRelA (.MissingCapability 0#u64) (.missingCapability 0) := rfl + have fault : ∀ p lf e, LookupFaultRelA lf e → + (∀ τ, ((), τ) ∈ (handleFault (Z := Unit) (specPtr t) (.capFault p.bv true e) σ).1 → + ((), τ) ∈ (handleRecv (Z := Unit) blocking canReply σ).1) → + syscall.cap_fault_recv st ar s a p lf ⦃ r => + ∃ τ, ((), τ) ∈ (handleRecv (Z := Unit) blocking canReply σ).1 ∧ Rel r.1 r.2 τ ⦄ := by + intro p lf e h go + unfold syscall.cap_fault_recv + rw [hct, bind_ok] + exact WP.spec_mono (leaves.fault p lf e h) fun r ⟨τ, hm, hRel⟩ => ⟨τ, go τ hm, hRel⟩ + unfold syscall.handle_recv + rw [hct, bind_ok, tcb_reg_eqA, bind_ok] + refine WP.spec_bind (withReachedA (lookup_cap_recv_refinesA th s σ hR hV t ht cp)) ?_ + rintro r ⟨seen, e, he, hr⟩ + cases r with + | Err exception => + cases e with + | ok _ => cases exception <;> exact hr.elim + | error lf => + cases exception with + | Lookup lf' => + refine fault cp lf' lf hr ?_ + intro τ hm + rw [handleRecv, nd_gets_bindSc, hcur, nd_bind_oneSc capRead] + apply mem_bindA.2 + refine ⟨(.error (.capFault cp.bv true lf), σ), ?_, hm⟩ + simp only [ofBl_toBlA] + rw [run_bind_errorA (recvLookupFaultA he cp.bv)] + rfl + | _ => exact hr.elim + | Ok c => + cases e with + | error _ => exact hr.elim + | ok ac => + obtain ⟨wellFormed, hceq⟩ := hr + have hcap0 : (capFaultOnFailure (ofBl (toBl cp.bv)) true + (lookupCap (Z := Unit) (specPtr t) (toBl cp.bv))).run σ = ({(.ok ac, σ)}, False) := + recvLookupCapA he _ + clear wellFormed he + cases c + case Endpoint p badge send recv grant grantReply => + obtain rfl : absCapA (.Endpoint p badge send recv grant grantReply) = ac := hceq + have hcap := hcap0 + have rights : (AllowRecv ∈ rightsOfA send recv grant grantReply) ↔ recv = true := by + simp [AllowRecv, rightsOfA] + cases recv + · simp only [Bool.false_eq_true, ↓reduceIte] + refine fault cp _ _ miss ?_ + intro τ hm + rw [handleRecv, nd_gets_bindSc, hcur, nd_bind_oneSc capRead] + apply mem_bindA.2 + refine ⟨(.error (.capFault cp.bv true (.missingCapability 0)), σ), ?_, hm⟩ + rw [run_bind_okA hcap] + simp [absCapA, AllowRecv, rightsOfA, ofBl_toBlA] + · simp only [↓reduceIte] + cases canReply + · simp only [Bool.false_eq_true, ↓reduceIte] + refine WP.spec_mono (leaves.endpoint p badge send grant grantReply .Null seen (Or.inl rfl)) ?_ + rintro r ⟨τ, hm, hRel⟩ + refine ⟨τ, ?_, hRel⟩ + rw [handleRecv, nd_gets_bindSc, hcur, nd_bind_oneSc capRead] + apply mem_bindA.2 + refine ⟨(.ok (), τ), ?_, rfl⟩ + rw [run_bind_okA hcap] + dsimp only [absCapA] + rw [if_pos (rights.2 rfl)] + simp only [Bool.false_eq_true, ↓reduceIte] + rw [run_bind_okA (run_returnOkA _ _)] + exact recvLiftMemA hm + · simp only [↓reduceIte, tcb_reg_eqA, bind_ok] + refine WP.spec_bind (withReachedA (lookup_cap_recv_refinesA th s σ hR hV t ht rp)) ?_ + rintro r' ⟨replySeen, e', he', hr'⟩ + cases r' with + | Err exception => + cases e' with + | ok _ => cases exception <;> exact hr'.elim + | error lf => + cases exception with + | Lookup lf' => + refine fault rp lf' lf hr' ?_ + intro τ hm + rw [handleRecv, nd_gets_bindSc, hcur, nd_bind_oneSc capRead] + apply mem_bindA.2 + refine ⟨(.error (.capFault rp.bv true lf), σ), ?_, hm⟩ + rw [run_bind_okA hcap] + dsimp only [absCapA] + rw [if_pos (rights.2 rfl)] + simp only [↓reduceIte] + have hReply : (lookupReply (Z := Unit)).run σ = + ({(.error (.capFault rp.bv true lf), σ)}, False) := by + unfold lookupReply + rw [run_bind_okA (run_liftEA replyRead), run_bind_okA (run_liftEA currentRead), + ofBl_toBlA, run_bind_errorA (recvLookupFaultA he' rp.bv)] + rw [run_bind_errorA hReply] + rfl + | _ => exact hr'.elim + | Ok rc => + cases e' with + | error _ => exact hr'.elim + | ok arc => + obtain ⟨replyWellFormed, hceq'⟩ := hr' + have hrp := recvLookupCapA he' rp.bv + clear replyWellFormed he' + cases rc + case Reply p' grant' => + obtain rfl : absCapA (.Reply p' grant') = arc := hceq' + refine WP.spec_mono + (leaves.endpoint p badge send grant grantReply _ seen (Or.inr ⟨p', grant', rfl, replySeen⟩)) ?_ + rintro r ⟨τ, hm, hRel⟩ + refine ⟨τ, ?_, hRel⟩ + rw [handleRecv, nd_gets_bindSc, hcur, nd_bind_oneSc capRead] + apply mem_bindA.2 + refine ⟨(.ok (), τ), ?_, rfl⟩ + rw [run_bind_okA hcap] + dsimp only [absCapA] + rw [if_pos (rights.2 rfl)] + simp only [↓reduceIte] + have hReply : (lookupReply (Z := Unit)).run σ = ({(.ok (absCapA (.Reply p' grant')), σ)}, False) := by + unfold lookupReply + rw [run_bind_okA (run_liftEA replyRead), run_bind_okA (run_liftEA currentRead), + ofBl_toBlA, run_bind_okA hrp] + rfl + rw [run_bind_okA hReply] + exact recvLiftMemA hm + all_goals + refine fault rp _ _ miss ?_ + intro τ hm + rw [handleRecv, nd_gets_bindSc, hcur, nd_bind_oneSc capRead] + apply mem_bindA.2 + refine ⟨(.error (.capFault rp.bv true (.missingCapability 0)), σ), ?_, hm⟩ + rw [run_bind_okA hcap] + dsimp only [absCapA] + rw [if_pos (rights.2 rfl)] + simp only [↓reduceIte] + have hReply : (lookupReply (Z := Unit)).run σ = + ({(.error (.capFault rp.bv true (.missingCapability 0)), σ)}, False) := by + unfold lookupReply + rw [run_bind_okA (run_liftEA replyRead), run_bind_okA (run_liftEA currentRead), + ofBl_toBlA, run_bind_okA hrp] + rcases CapEqA.eq_or_pt hceq' with rfl | ⟨p'', a'', v'', rfl⟩ <;> rfl + rw [run_bind_errorA hReply] + rfl + case Notification p badge send recv => + obtain rfl : absCapA (.Notification p badge send recv) = ac := hceq + have hcap := hcap0 + have rights : (AllowRecv ∈ rightsOfA send recv false false) ↔ recv = true := by + simp [AllowRecv, rightsOfA] + refine WP.spec_bind (Notif.obj.Ntfn.bound_tcb.spec s p) ?_ + rintro bound rfl + let word := L.mem s (Notif.nBoundA p) + have fits : (optRefA word = some (specPtr t) ∨ optRefA word = none) ↔ (word = t ∨ word = 0#u64) := by + by_cases hw : word = 0#u64 + · simp [optRefA, hw] + · rw [optRefA, if_neg hw] + constructor + · rintro (h | h) + · exact Or.inl (specPtr_inj (Option.some.inj h)) + · cases h + · rintro (rfl | h) + · exact Or.inl rfl + · exact (hw h).elim + cases recv + · simp only [Bool.false_eq_true, ↓reduceIte] + refine fault cp _ _ miss ?_ + intro τ hm + rw [handleRecv, nd_gets_bindSc, hcur, nd_bind_oneSc capRead] + apply mem_bindA.2 + refine ⟨(.error (.capFault cp.bv true (.missingCapability 0)), σ), ?_, hm⟩ + rw [run_bind_okA hcap] + simp [absCapA, AllowRecv, rightsOfA, ofBl_toBlA] + · have boundRead := run_liftEA (ε := Fault) (leaves.bound p badge send seen) + by_cases hb : word = t ∨ word = 0#u64 + · have cpass : (if (L.mem s (Notif.nBoundA p) != 0#u64) = true then + if (L.mem s (Notif.nBoundA p) != t) = true then + syscall.cap_fault_recv st ar s a cp (.MissingCapability 0#u64) + else notification.receive_signal st ar s a t p blocking + else notification.receive_signal st ar s a t p blocking) = + notification.receive_signal st ar s a t p blocking := by + rcases hb with h | h + · simp only [show L.mem s (Notif.nBoundA p) = t from h, bne_self_eq_false, + Bool.false_eq_true, ↓reduceIte, ite_self] + · simp only [show L.mem s (Notif.nBoundA p) = 0#u64 from h, + bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + rw [if_pos rfl, cpass] + refine WP.spec_mono (leaves.signal p badge send seen) ?_ + rintro r ⟨τ, hm, hRel⟩ + refine ⟨τ, ?_, hRel⟩ + rw [handleRecv, nd_gets_bindSc, hcur, nd_bind_oneSc capRead] + apply mem_bindA.2 + refine ⟨(.ok (), τ), ?_, rfl⟩ + rw [run_bind_okA hcap] + dsimp only [absCapA] + rw [if_pos (rights.2 rfl), run_bind_okA boundRead, if_pos (fits.2 hb)] + exact recvLiftMemA hm + · have h0 : L.mem s (Notif.nBoundA p) ≠ 0#u64 := fun h => hb (Or.inr h) + have ht' : L.mem s (Notif.nBoundA p) ≠ t := fun h => hb (Or.inl h) + have b0 : (L.mem s (Notif.nBoundA p) != 0#u64) = true := by simp [h0] + have bt : (L.mem s (Notif.nBoundA p) != t) = true := by simp [ht'] + rw [if_pos rfl, if_pos b0, if_pos bt] + refine fault cp _ _ miss ?_ + intro τ hm + rw [handleRecv, nd_gets_bindSc, hcur, nd_bind_oneSc capRead] + apply mem_bindA.2 + refine ⟨(.error (.capFault cp.bv true (.missingCapability 0)), σ), ?_, hm⟩ + rw [run_bind_okA hcap] + dsimp only [absCapA] + rw [if_pos (rights.2 rfl), run_bind_okA boundRead, if_neg (fun h => hb (fits.1 h)), ofBl_toBlA] + rfl + all_goals + refine fault cp _ _ miss ?_ + intro τ hm + rw [handleRecv, nd_gets_bindSc, hcur, nd_bind_oneSc capRead] + apply mem_bindA.2 + refine ⟨(.error (.capFault cp.bv true (.missingCapability 0)), σ), ?_, hm⟩ + rw [run_bind_okA hcap0] + rcases CapEqA.eq_or_pt hceq with rfl | ⟨p', a', v', rfl⟩ <;> simp [absCapA, ofBl_toBlA] + +private theorem restoreYieldConsumedA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {σ : State Unit} (H : SchedSRA st Z m κ s σ) {sc : U64} (hc : sc ∈ Z.C) (v : U64) : + obj.Sc.set_consumed st sc s v ⦃ s' => ∃ τ, + setScObjRef (Z := Unit) scConsumedUpdate (scNameA m sc) v.bv σ = ({((), τ)}, False) ∧ + SchedSRA st Z m κ s' τ ∧ τ.machineState = σ.machineState ⦄ := by + obtain ⟨x, n, hk, R⟩ := H.scr.sc sc hc + refine WP.spec_mono (obj.Sc.set_consumed.specSc s sc v) ?_ + rintro s' rfl + let τ := updKSc σ (scNameA m sc) (.schedContext { x with scConsumed := v.bv } n) + obtain ⟨rel, writes⟩ := R.set_consumed v + have core : scCoreA sc = scWA sc 16#u64 := by unfold scCoreA scWA layout.sc.CORE; rfl + have keep : L.mem (L.wr s (scWA sc 8#u64) v) (scCoreA sc) = L.mem s (scCoreA sc) := by + rw [core] + apply L.mem_wr_ne (R.hdr (by simp : (8#u64).val < 96) (by simp)).1 + (R.hdr (by simp : (16#u64).val < 96) (by simp)).1 + have fit := R.bounds.2.2 + rw [scWA_val (sc := sc) (K := 8#u64) (by simp; omega), + scWA_val (sc := sc) (K := 16#u64) (by simp; omega)] + simp + refine ⟨τ, ?_, H.scObjA hc hk writes rel rfl rfl rfl rfl keep, rfl⟩ + exact updateSchedContext_eqA hk _ + +/-- [J] The successful native budget branch supplies the current SC +placement and ownership, including the ordinary nonidle SC selector. +[P] Use the same charge bounds and clock frame as the MCS budget proof. -/ +theorem handle_yield_refinesA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (E : YieldQueueRelA (st := st) Z m κ s σ) (I : CurInvA σ) (Idle : ActIdleA σ) + (place : EntryBudgetPlaceA st m κ s σ) (nonidle : σ.curSc ≠ idleScPtr) + (a : A) {Clock : A → MachineState → Prop} (clock : Clock a σ.machineState) + (timeout : EntryTimeoutFromA (st := st) (ar := ar) Z m κ Clock s σ) + (minimum : fl4MinBudget = MIN_BUDGET) (minArch : Fl4MinBudgetAgreeA ar a) + (limit : sporadic.max_release_time ar a ⦃ v => v.bv = fl4MaxReleaseTime ⦄) : + syscall.handle_yield st ar s a ⦃ r => ∃ τ Q R, + fl4HandleYield (Z := Unit) σ = ({((), τ)}, False) ∧ + SchedSRA st {Z with Q := Q, R := R} m κ r.1 τ ∧ Clock r.2 τ.machineState ⦄ := by + let sc := L.cscW s + have hc : sc ∈ Z.C := E.sched.cscC + have hcur : σ.curSc = scNameA m sc := E.sched.currentScA + obtain ⟨x, n, hk, R⟩ := E.sched.scr.sc sc hc + have hx := R.strong (I.active nonidle _ _ (by rwa [hcur])) + have ne := refills_neL hx.wf + have hxne : x.scRefills ≠ [] := by simpa [hx.refills] using ne + unfold syscall.handle_yield + refine WP.spec_bind (sched.cur_sc.spec s) ?_ + intro csc hcsc + change csc = sc at hcsc + subst csc + refine WP.spec_bind (obj.Sc.consumed.specSc s sc) ?_ + intro old hOld + refine WP.spec_bind (sched.consumed.specA s) ?_ + intro elapsed hElapsed + simp only [lift, bind_ok] + let total := core.num.U64.wrapping_add old elapsed + have elapsedEq : (L.mem s (L.nodeA s layout.node.CONSUMED)).bv = σ.consumedTime := + E.sched.rest.consumed.symm + have hTotal : total.bv = x.scConsumed + σ.consumedTime := by + unfold total + rw [core.num.U64.wrapping_add_bv_eq, hOld, ← R.consumed, hElapsed, elapsedEq] + refine WP.spec_bind (sporadic.head.spec s sc) ?_ + intro head hHead + have hAmount : (refillHd x).rAmount = head.amount.bv := by + rw [refillHd_mapA hx.refills ne, ← head_eqA hx.wf, ← hHead] + rfl + refine WP.spec_bind (charge_budget_entry_refinesA E I Idle place a clock timeout minimum minArch limit + head.amount false) ?_ + rintro ⟨s1, a1⟩ ⟨τ1, Q, Rq, hCharge, H, hClock, -⟩ + refine WP.spec_bind (restoreYieldConsumedA H.sched hc total) ?_ + rintro s2 ⟨τ, hRestore, after, machine⟩ + rw [WP.spec_ok] + refine ⟨τ, Q, Rq, ?_, after, ?_⟩ + · unfold fl4HandleYield + rw [nd_gets_bindSc, hcur, nd_bind_oneSc (getSchedContext_eqA hk), nd_gets_bindSc, + nd_bind_oneSc (getRefillHead_eqA hk hxne), hAmount, nd_bind_oneSc hCharge, ← hTotal] + exact hRestore + · rw [machine] + exact hClock + +/-- An operation has an actual abstract successor related to its concrete +system and architecture return. -/ +def StepPostA (Rel : S → A → State Unit → Prop) (operation : SMonad Unit Unit) + (σ : State Unit) (r : S × A) : Prop := + ∃ τ, ((), τ) ∈ (operation σ).1 ∧ Rel r.1 r.2 τ + +/-- [J] Build the existing MCS phase inputs from a reached state before +using this theorem. The post uses the actual scheduler and activation +result and the updated queue and current-owner projections. -/ +theorem finish_entry_refinesA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {a : A} {σ : State Unit} {timer : A → Nat → DeadlineViewA} + {AR : A → HwRootA → Prop} (arch : ArchSwFA st ar) (view : State Unit → Fl4TimerView) + (inputs : ScheduleInputsA (st := st) Z m κ s a σ ar timer view AR) + (activation : ∀ (r : S × A) τ Q R Tcur Ccur, + sched.schedule st ar s a = ok r → + ((), τ) ∈ (fl4Schedule layout.NUM_DOMAIN_SCHEDULES.val view σ).1 → + YieldQueueRelA (st := st) {Z with Q := Q, R := R, curOf := Tcur, curScOf := Ccur} m κ r.1 τ → + ScheduleFinishInputsA (st := st) {Z with Q := Q, R := R, curOf := Tcur, curScOf := Ccur} + m κ r.1 r.2 τ ar) : + syscall.finish st ar s a ⦃ r => + StepPostA (fun s' _ τ => ∃ Q R Tcur Ccur, + YieldQueueRelA (st := st) {Z with Q := Q, R := R, curOf := Tcur, curScOf := Ccur} m κ s' τ) + (fl4Finish layout.NUM_DOMAIN_SCHEDULES.val view) σ r ⦄ := by + refine WP.spec_mono (finish_refinesA arch view inputs activation) ?_ + rintro r ⟨τ, Q, R, Tcur, Ccur, h, post⟩ + exact ⟨τ, h, Q, R, Tcur, Ccur, post⟩ + +/-- [J] The finish producer is applied to a body outcome at the state +which the body actually returned. -/ +private theorem serialFinishBindA + {body : Result (S × A)} {op : SMonad Unit Unit} {σ : State Unit} + {Rel Final : S → A → State Unit → Prop} {limit : Nat} {view : State Unit → Fl4TimerView} + (step : body ⦃ r => StepPostA Rel op σ r ⦄) + (finish : ∀ r τ, body = ok r → ((), τ) ∈ (op σ).1 → Rel r.1 r.2 τ → + syscall.finish st ar r.1 r.2 ⦃ out => StepPostA Final (fl4Finish limit view) τ out ⦄) : + (do let (s', a') ← body; syscall.finish st ar s' a') ⦃ r => + StepPostA Final (op >>= fun _ => fl4Finish limit view) σ r ⦄ := by + refine WP.spec_bind (withReachedA step) ?_ + rintro ⟨s', a'⟩ ⟨eq, τ, bodyMem, H⟩ + refine WP.spec_mono (finish (s', a') τ eq bodyMem H) ?_ + rintro r ⟨υ, tail, out⟩ + exact ⟨υ, mem_bindA.2 ⟨((), τ), bodyMem, tail⟩, out⟩ + +/-- [J] The prefix comes from `budget_ok_refinesA` with the contract's +clock and scheduler projections. The fault leaf is used only at an +actual successful prefix, with its full concrete and abstract payload. -/ +theorem budgetFault_refinesA {s : S} {a : A} {σ : State Unit} + {Rel : S → A → State Unit → Prop} {fault : Fault} {concrete : error.Fault} + (budget : syscall.budget_ok st ar s a ⦃ r => ∃ τ, + fl4BudgetOk (Z := Unit) σ = ({(r.1, τ)}, False) ∧ Rel r.2.1 r.2.2 τ ⦄) + (deliver : ∀ s' a' τ, syscall.budget_ok st ar s a = ok (true, s', a') → + fl4BudgetOk (Z := Unit) σ = ({(true, τ)}, False) → Rel s' a' τ → + (do let t ← sched.cur_thread st s'; faults.handle_fault st ar s' a' t concrete) ⦃ r => + StepPostA Rel (do let t ← NonDet.gets fun k : State Unit => k.curThread; handleFault t fault) τ r ⦄) : + (do + let (b, s', a') ← syscall.budget_ok st ar s a + if b then do + let t ← sched.cur_thread st s' + faults.handle_fault st ar s' a' t concrete + else ok (s', a')) ⦃ r => StepPostA Rel (fl4BudgetFault fault) σ r ⦄ := by + refine WP.spec_bind (withReachedA budget) ?_ + rintro ⟨b, s', a'⟩ ⟨eq, τ, bspec, H⟩ + cases b + · refine (WP.spec_ok _).2 ⟨τ, ?_, H⟩ + rw [fl4BudgetFault, nd_bind_oneSc bspec] + rfl + · refine WP.spec_mono (deliver s' a' τ eq bspec H) ?_ + rintro r ⟨υ, mem, rel⟩ + refine ⟨υ, ?_, rel⟩ + rw [fl4BudgetFault, nd_bind_oneSc bspec] + exact mem + +/-- [J] A user exception uses the full words and the reached fault and +finish projections. The port's masked event is an agreement case. -/ +theorem userExceptionEntry_refinesA {s : S} {a : A} {σ : State Unit} (number code : U64) + {Rel Final : S → A → State Unit → Prop} (limit : Nat) (view : State Unit → Fl4TimerView) + (budget : syscall.budget_ok st ar s a ⦃ r => ∃ τ, + fl4BudgetOk (Z := Unit) σ = ({(r.1, τ)}, False) ∧ Rel r.2.1 r.2.2 τ ⦄) + (deliver : ∀ s' a' τ, syscall.budget_ok st ar s a = ok (true, s', a') → + fl4BudgetOk (Z := Unit) σ = ({(true, τ)}, False) → Rel s' a' τ → + (do let t ← sched.cur_thread st s'; faults.handle_fault st ar s' a' t (.UserException number code)) ⦃ r => + StepPostA Rel (do let t ← NonDet.gets fun k : State Unit => k.curThread + handleFault t (.userException number.bv code.bv)) τ r ⦄) + (finish : ∀ s' a' τ, ((), τ) ∈ (fl4BudgetFault (Z := Unit) (.userException number.bv code.bv) σ).1 → + Rel s' a' τ → syscall.finish st ar s' a' ⦃ r => StepPostA Final (fl4Finish limit view) τ r ⦄) : + syscall.handle_user_exception st ar s a number code ⦃ r => + StepPostA Final (fl4BudgetFault (.userException number.bv code.bv) >>= fun _ => fl4Finish limit view) σ r ⦄ := by + unfold syscall.handle_user_exception + have h := serialFinishBindA (budgetFault_refinesA budget deliver) + (fun r τ _ mem rel => finish r.1 r.2 τ mem rel) + obtain ⟨⟨b, s', a'⟩, budgetEq, _⟩ := spec_existsA budget + simp only [budgetEq, bind_ok] at h ⊢ + exact h + +private theorem vmFaultWordsA (addr esr : U64) : + (do + let status ← esr >>> 26#i32 + let ec ← lift (status &&& 63#u64) + vspace.vm_fault addr esr (ec = 32#u64)) = + ok (.VmFault addr esr (decide (((esr.bv >>> 26) &&& 63) = 32))) := by + rw [shr_eqA esr 26#i32 (by decide) (by decide), bind_ok] + simp only [lift, bind_ok, vspace.vm_fault] + congr 2 + apply Bool.eq_iff_iff.2 + simp only [decide_eq_true_eq] + exact ⟨fun h => congrArg UScalar.bv h, P1.eq_of_bv⟩ + +/-- [J] A native abort follows the actual budget and IRQ-independent +finish path. ESR and FAR/ELR observations enter with their full widths. -/ +theorem vmFaultEntry_refinesA {s : S} {a : A} {σ : State Unit} (addr esr : U64) + {Rel Final : S → A → State Unit → Prop} (limit : Nat) (view : State Unit → Fl4TimerView) + (budget : syscall.budget_ok st ar s a ⦃ r => ∃ τ, + fl4BudgetOk (Z := Unit) σ = ({(r.1, τ)}, False) ∧ Rel r.2.1 r.2.2 τ ⦄) + (deliver : ∀ s' a' τ, syscall.budget_ok st ar s a = ok (true, s', a') → + fl4BudgetOk (Z := Unit) σ = ({(true, τ)}, False) → Rel s' a' τ → + (do let t ← sched.cur_thread st s' + faults.handle_fault st ar s' a' t (.VmFault addr esr (decide (((esr.bv >>> 26) &&& 63) = 32)))) ⦃ r => + StepPostA Rel (do let t ← NonDet.gets fun k : State Unit => k.curThread + handleFault t (fl4VmFault addr.bv esr.bv)) τ r ⦄) + (finish : ∀ s' a' τ, ((), τ) ∈ (fl4BudgetFault (Z := Unit) (fl4VmFault addr.bv esr.bv) σ).1 → + Rel s' a' τ → syscall.finish st ar s' a' ⦃ r => StepPostA Final (fl4Finish limit view) τ r ⦄) : + syscall.handle_vm_fault st ar s a addr esr ⦃ r => + StepPostA Final (fl4BudgetFault (fl4VmFault addr.bv esr.bv) >>= fun _ => fl4Finish limit view) σ r ⦄ := by + unfold syscall.handle_vm_fault + have step (s' : S) (a' : A) : + (do + let t ← sched.cur_thread st s' + let i ← esr >>> 26#i32 + let ec ← lift (i &&& 63#u64) + let fault ← vspace.vm_fault addr esr (ec = 32#u64) + faults.handle_fault st ar s' a' t fault) = + (do + let t ← sched.cur_thread st s' + faults.handle_fault st ar s' a' t (.VmFault addr esr (decide (((esr.bv >>> 26) &&& 63) = 32)))) := by + apply congrArg (fun k => Std.bind (sched.cur_thread st s') k) + funext t + have reassoc : + Std.bind (do let i ← esr >>> 26#i32 + let ec ← lift (i &&& 63#u64) + vspace.vm_fault addr esr (ec = 32#u64)) + (fun fault => faults.handle_fault st ar s' a' t fault) = + (do let i ← esr >>> 26#i32 + let ec ← lift (i &&& 63#u64) + let fault ← vspace.vm_fault addr esr (ec = 32#u64) + faults.handle_fault st ar s' a' t fault) := by + rw [Std.bind_assoc] + apply congrArg (Std.bind (esr >>> 26#i32)) + funext i + rw [Std.bind_assoc] + exact reassoc.symm.trans (by rw [vmFaultWordsA, bind_ok]) + have h := serialFinishBindA (budgetFault_refinesA budget deliver) + (fun r τ _ mem rel => finish r.1 r.2 τ mem rel) + obtain ⟨⟨b, s', a'⟩, budgetEq, _⟩ := spec_existsA budget + simp only [budgetEq, bind_ok] at h ⊢ + cases b + · exact h + · exact (congrArg (fun p : Result (S × A) => + (do let (sf, af) ← p; syscall.finish st ar sf af) ⦃ r => + StepPostA Final (fl4BudgetFault (fl4VmFault addr.bv esr.bv) >>= fun _ => fl4Finish limit view) σ r ⦄) + (step s' a')).mpr h + +/-- [J] The timestamp and plain budget prefix supplies the reached IRQ +state. The service contract consumes the observed IRQ on both Boolean +outcomes before entering the common finish. -/ +theorem interruptEntry_refinesA {s : S} {a : A} {σ : State Unit} (irq : U64) + {Rel Final : S → A → State Unit → Prop} (limit : Nat) (view : State Unit → Fl4TimerView) + (service : SMonad Unit Unit) + (budget : (do let (s', a') ← sched.update_timestamp st ar s a; sched.check_budget st ar s' a') ⦃ r => + ∃ τ, ((fl4UpdateTimestamp (Z := Unit)) >>= fun _ => fl4CheckBudget) σ = ({(r.1, τ)}, False) ∧ + Rel r.2.1 r.2.2 τ ⦄) + (handle : ∀ b s' a' τ, + (do let (s0, a0) ← sched.update_timestamp st ar s a; sched.check_budget st ar s0 a0) = + ok (b, s', a') → + ((fl4UpdateTimestamp (Z := Unit)) >>= fun _ => fl4CheckBudget) σ = ({(b, τ)}, False) → + Rel s' a' τ → interrupt.handle_interrupt st ar s' a' irq ⦃ r => StepPostA Rel service τ r ⦄) + (finish : ∀ s' a' τ, ((), τ) ∈ (fl4AcceptedIrqBody service σ).1 → + Rel s' a' τ → syscall.finish st ar s' a' ⦃ r => StepPostA Final (fl4Finish limit view) τ r ⦄) : + syscall.handle_interrupt_entry st ar s a irq ⦃ r => + StepPostA Final (fl4AcceptedIrqBody service >>= fun _ => fl4Finish limit view) σ r ⦄ := by + have body : (do + let (_, s', a') ← do + let (s0, a0) ← sched.update_timestamp st ar s a + sched.check_budget st ar s0 a0 + interrupt.handle_interrupt st ar s' a' irq) ⦃ r => StepPostA Rel (fl4AcceptedIrqBody service) σ r ⦄ := by + refine WP.spec_bind (withReachedA budget) ?_ + rintro ⟨b, s', a'⟩ ⟨budgetEq, τ, bspec, H⟩ + refine WP.spec_mono (handle b s' a' τ budgetEq bspec H) ?_ + rintro r ⟨υ, mem, related⟩ + refine ⟨υ, ?_, related⟩ + unfold fl4AcceptedIrqBody + rw [← nd_bind_assocSc, nd_bind_oneSc bspec] + exact mem + unfold syscall.handle_interrupt_entry + have h := serialFinishBindA body (fun r τ _ mem rel => finish r.1 r.2 τ mem rel) + obtain ⟨⟨b, s', a'⟩, budgetEq, _⟩ := spec_existsA budget + obtain ⟨⟨s0, a0⟩, timestamp, checked⟩ := bind_eq_ok budgetEq + change sched.check_budget st ar s0 a0 = ok (b, s', a') at checked + simp only [timestamp, bind_ok] at h ⊢ + change (do let (_, s2, a2) ← sched.check_budget st ar s0 a0 + let (s3, a3) ← interrupt.handle_interrupt st ar s2 a2 irq + syscall.finish st ar s3 a3) ⦃ _ ⦄ + change (do + let (s2, a2) ← (do let (_, s1, a1) ← sched.check_budget st ar s0 a0 + interrupt.handle_interrupt st ar s1 a1 irq) + syscall.finish st ar s2 a2) ⦃ _ ⦄ at h + simp only [checked, bind_ok] at h ⊢ + exact h + +private theorem settle_ok_memA {pending : SMonad Unit Unit} {op : PMonad Unit Unit} + {σ τ : State Unit} (h : (.ok (), τ) ∈ (op.run σ).1) : + ((), τ) ∈ (fl4SettleDispatch pending op σ).1 := + mem_bindA.2 ⟨(.ok (), τ), h, rfl⟩ + +private theorem settle_error_memA {pending : SMonad Unit Unit} {op : PMonad Unit Unit} + {σ τ υ : State Unit} (h : (.error (), τ) ∈ (op.run σ).1) + (tail : ((), υ) ∈ (fl4PreemptionPath pending τ).1) : + ((), υ) ∈ (fl4SettleDispatch pending op σ).1 := + mem_bindA.2 ⟨(.error (), τ), h, tail⟩ + +/-- Encode the standard eleven syscall cases using the extracted ABI. -/ +def syscallNumberA : Syscall → I64 + | .sysCall => fl4_abi.syscall.SysCall + | .sysReplyRecv => fl4_abi.syscall.SysReplyRecv + | .sysNBSendRecv => fl4_abi.syscall.SysNBSendRecv + | .sysNBSendWait => fl4_abi.syscall.SysNBSendWait + | .sysSend => fl4_abi.syscall.SysSend + | .sysNBSend => fl4_abi.syscall.SysNBSend + | .sysRecv => fl4_abi.syscall.SysRecv + | .sysNBRecv => fl4_abi.syscall.SysNBRecv + | .sysWait => fl4_abi.syscall.SysWait + | .sysNBWait => fl4_abi.syscall.SysNBWait + | .sysYield => fl4_abi.syscall.SysYield + +theorem syscallNumberInRangeA (call : Syscall) : + ¬syscallNumberA call < fl4_abi.syscall.SYSCALL_MIN ∧ + ¬fl4_abi.syscall.SYSCALL_MAX < syscallNumberA call := by + cases call <;> simp only [syscallNumberA, fl4_abi.syscall.SYSCALL_MIN, fl4_abi.syscall.SYSCALL_MAX, + fl4_abi.syscall.SysCall, fl4_abi.syscall.SysReplyRecv, fl4_abi.syscall.SysNBSendRecv, + fl4_abi.syscall.SysNBSendWait, fl4_abi.syscall.SysSend, fl4_abi.syscall.SysNBSend, + fl4_abi.syscall.SysRecv, fl4_abi.syscall.SysNBRecv, fl4_abi.syscall.SysWait, + fl4_abi.syscall.SysNBWait, fl4_abi.syscall.SysYield] <;> decide + +private theorem syscallNumber_injA (x y : Syscall) : syscallNumberA x = syscallNumberA y ↔ x = y := by + constructor + · intro h + cases x <;> cases y <;> try rfl + all_goals exfalso + all_goals simp only [syscallNumberA, fl4_abi.syscall.SysCall, fl4_abi.syscall.SysReplyRecv, + fl4_abi.syscall.SysNBSendRecv, fl4_abi.syscall.SysNBSendWait, fl4_abi.syscall.SysSend, + fl4_abi.syscall.SysNBSend, fl4_abi.syscall.SysRecv, fl4_abi.syscall.SysNBRecv, + fl4_abi.syscall.SysWait, fl4_abi.syscall.SysNBWait, fl4_abi.syscall.SysYield] at h + all_goals revert h + all_goals decide + · exact congrArg syscallNumberA + +/-- Which first-phase leaf the proof may use for this request. The +dispatcher theorem checks these selectors against the extracted code +and the abstract dispatcher table. -/ +inductive DispatchInvokeAtA (cap reply dest : U64) : + Syscall → Bool → Bool → Bool → Bool → U64 → Prop where + | send : DispatchInvokeAtA cap reply dest .sysSend false true false false cap + | nbsend : DispatchInvokeAtA cap reply dest .sysNBSend false false false false cap + | call : DispatchInvokeAtA cap reply dest .sysCall true true true false cap + | replyRecv : DispatchInvokeAtA cap reply dest .sysReplyRecv false false true true reply + | nbsendRecv : DispatchInvokeAtA cap reply dest .sysNBSendRecv false false true true dest + | nbsendWait : DispatchInvokeAtA cap reply dest .sysNBSendWait false false true true reply + +/-- Which standalone receive leaf the proof may use. -/ +inductive DispatchRecvAtA : Syscall → Bool → Bool → Prop where + | recv : DispatchRecvAtA .sysRecv true true + | nbrecv : DispatchRecvAtA .sysNBRecv false true + | wait : DispatchRecvAtA .sysWait true false + | nbwait : DispatchRecvAtA .sysNBWait false false + +/-- Combined receive's reply-register use is selected by the syscall. -/ +inductive DispatchCombinedAtA : Syscall → Bool → Prop where + | replyRecv : DispatchCombinedAtA .sysReplyRecv true + | nbsendRecv : DispatchCombinedAtA .sysNBSendRecv true + | nbsendWait : DispatchCombinedAtA .sysNBSendWait false + +/-- [J] A concrete invocation returns the same success or preemption as +the chosen dispatcher and relates the corresponding successor. The +post-preemption and combined receive hooks consume only reached results. -/ +structure DispatchBranchesA (st : machine.Store S) (ar : machine.Arch A) + (s : S) (a : A) (σ : State Unit) + (call : Syscall) (cap reply dest : U64) + (invoke : Bool → Bool → Bool → Bool → CapRef → PMonad Unit Unit) + (pending : SMonad Unit Unit) (InvRel : Except Unit Unit → S → A → State Unit → Prop) + (Rel : S → A → State Unit → Prop) : Prop where + inv : ∀ calling blocking donate first (cptr : U64), + DispatchInvokeAtA cap reply dest call calling blocking donate first cptr → + syscall.handle_invocation st ar s a calling blocking donate first cptr ⦃ r => + ∃ e τ, (e, τ) ∈ ((invoke calling blocking donate first (toBl cptr.bv)).run σ).1 ∧ + r.1 = (match e with | .ok _ => .Ok () | .error _ => .Err .Preempted) ∧ + InvRel e r.2.1 r.2.2 τ ⦄ + done : ∀ calling blocking donate first (cptr : U64) s' a' τ, + DispatchInvokeAtA cap reply dest call calling blocking donate first cptr → + first = false → + syscall.handle_invocation st ar s a calling blocking donate first cptr = ok (.Ok (), s', a') → + (.ok (), τ) ∈ ((invoke calling blocking donate first (toBl cptr.bv)).run σ).1 → + InvRel (.ok ()) s' a' τ → Rel s' a' τ + preempt : ∀ calling blocking donate first (cptr : U64) s' a' τ, + DispatchInvokeAtA cap reply dest call calling blocking donate first cptr → + syscall.handle_invocation st ar s a calling blocking donate first cptr = ok (.Err .Preempted, s', a') → + (.error (), τ) ∈ ((invoke calling blocking donate first (toBl cptr.bv)).run σ).1 → + InvRel (.error ()) s' a' τ → + syscall.after_invocation st ar s' a' (.Err .Preempted) ⦃ r => + r.1 = false ∧ StepPostA Rel (fl4PreemptionPath pending) τ r.2 ⦄ + combined : ∀ (cptr : U64) (receiveReply : Bool) s' a' τ, + DispatchInvokeAtA cap reply dest call false false true true cptr → + DispatchCombinedAtA call receiveReply → + syscall.handle_invocation st ar s a false false true true cptr = ok (.Ok (), s', a') → + (.ok (), τ) ∈ ((invoke false false true true (toBl cptr.bv)).run σ).1 → + InvRel (.ok ()) s' a' τ → + syscall.handle_recv st ar s' a' true receiveReply ⦃ r => StepPostA Rel (handleRecv true receiveReply) τ r ⦄ + recv : ∀ blocking receiveReply, DispatchRecvAtA call blocking receiveReply → + syscall.handle_recv st ar s a blocking receiveReply ⦃ r => + StepPostA Rel (handleRecv blocking receiveReply) σ r ⦄ + yield : call = .sysYield → syscall.handle_yield st ar s a ⦃ r => StepPostA Rel fl4HandleYield σ r ⦄ + +private theorem dispatchInvokeOnlyA + {s : S} {a : A} {σ : State Unit} {invoke pending InvRel Rel} + {call : Syscall} {cap reply dest : U64} + (B : DispatchBranchesA st ar s a σ call cap reply dest invoke pending InvRel Rel) + {calling blocking donate first : Bool} {cp : U64} + (allowed : DispatchInvokeAtA cap reply dest call calling blocking donate first cp) (last : first = false) : + (do let (e, s1, a1) ← syscall.handle_invocation st ar s a calling blocking donate first cp + let (_, s2, a2) ← syscall.after_invocation st ar s1 a1 e + ok (s2, a2)) ⦃ r => + StepPostA Rel (fl4SettleDispatch pending (invoke calling blocking donate first (toBl cp.bv))) σ r ⦄ := by + refine WP.spec_bind (withReachedA (B.inv calling blocking donate first cp allowed)) ?_ + rintro ⟨r, s1, a1⟩ ⟨eq, e, τ, mem, rEq, R⟩ + cases e with + | ok u => + cases u + change r = .Ok () at rEq + subst r + change (do let (_, s2, a2) ← syscall.after_invocation st ar s1 a1 (.Ok ()) + ok (s2, a2)) ⦃ _ ⦄ + rw [show syscall.after_invocation st ar s1 a1 (.Ok ()) = ok (true, s1, a1) from rfl, bind_ok] + refine (WP.spec_ok _).2 ⟨τ, settle_ok_memA mem, ?_⟩ + exact B.done calling blocking donate first cp s1 a1 τ allowed last eq mem R + | error u => + cases u + change r = .Err .Preempted at rEq + subst r + refine WP.spec_bind (B.preempt calling blocking donate first cp s1 a1 τ allowed eq mem R) ?_ + rintro ⟨b, s2, a2⟩ ⟨_, υ, tail, related⟩ + exact (WP.spec_ok _).2 ⟨υ, settle_error_memA mem tail, related⟩ + +private theorem dispatchCombinedA + {s : S} {a : A} {σ : State Unit} {invoke pending InvRel Rel} + {call : Syscall} {cap replyPtr dest cp : U64} {reply : Bool} + (B : DispatchBranchesA st ar s a σ call cap replyPtr dest invoke pending InvRel Rel) + (allowed : DispatchInvokeAtA cap replyPtr dest call false false true true cp) + (receive : DispatchCombinedAtA call reply) : + (do let (e, s1, a1) ← syscall.handle_invocation st ar s a false false true true cp + let (go, s2, a2) ← syscall.after_invocation st ar s1 a1 e + if go then syscall.handle_recv st ar s2 a2 true reply else ok (s2, a2)) ⦃ r => + StepPostA Rel (fl4SettleDispatch pending (do + invoke false false true true (toBl cp.bv) + withoutPreemption (handleRecv true reply))) σ r ⦄ := by + refine WP.spec_bind (withReachedA (B.inv false false true true cp allowed)) ?_ + rintro ⟨r, s1, a1⟩ ⟨eq, e, τ, mem, rEq, R⟩ + cases e with + | ok u => + cases u + change r = .Ok () at rEq + subst r + change (do let (b, s2, a2) ← syscall.after_invocation st ar s1 a1 (.Ok ()) + if b then syscall.handle_recv st ar s2 a2 true reply else ok (s2, a2)) ⦃ _ ⦄ + rw [show syscall.after_invocation st ar s1 a1 (.Ok ()) = ok (true, s1, a1) from rfl, bind_ok] + change syscall.handle_recv st ar s1 a1 true reply ⦃ _ ⦄ + refine WP.spec_mono (B.combined cp reply s1 a1 τ allowed receive eq mem R) ?_ + rintro out ⟨υ, tail, related⟩ + refine ⟨υ, settle_ok_memA ?_, related⟩ + rw [ExceptT.run_bind] + exact mem_bindA.2 ⟨(.ok (), τ), mem, liftE_memA tail⟩ + | error u => + cases u + change r = .Err .Preempted at rEq + subst r + have errorMem : (.error (), τ) ∈ ((do + invoke false false true true (toBl cp.bv) + withoutPreemption (handleRecv (Z := Unit) true reply) : PMonad Unit Unit).run σ).1 := by + rw [ExceptT.run_bind] + exact mem_bindA.2 ⟨(.error (), τ), mem, rfl⟩ + refine WP.spec_bind (B.preempt false false true true cp s1 a1 τ allowed eq mem R) ?_ + rintro ⟨b, s2, a2⟩ ⟨stopped, υ, tail, related⟩ + change b = false at stopped + subst b + exact (WP.spec_ok _).2 ⟨υ, settle_error_memA errorMem tail, related⟩ + +/-- [J] The CAP, REPLY and destination registers come from the current +TCB and the same abstract context. This theorem matches the eleven +serial cases, including the three conditional receive continuations. -/ +theorem dispatchSyscall_refinesA {s : S} {a : A} {σ : State Unit} {invoke pending InvRel Rel} + (call : Syscall) {t : U64} {x : Tcb} (hct : sched.cur_thread st s = ok t) + (B : DispatchBranchesA st ar s a σ call + (L.mem s (regWA t layout.reg.CAP)) + (L.mem s (regWA t layout.reg.REPLY)) + (L.mem s (regWA t layout.reg.NBSEND_RECV_DEST)) invoke pending InvRel Rel) + (hcur : σ.curThread = specPtr t) (hk : σ.kheap (specPtr t) = some (.tcb x)) + (cap : x.tcbArch.tcbContext.userRegs cap_register = (L.mem s (regWA t layout.reg.CAP)).bv) + (reply : x.tcbArch.tcbContext.userRegs replyRegister = (L.mem s (regWA t layout.reg.REPLY)).bv) + (dest : x.tcbArch.tcbContext.userRegs nbsendRecvDest = (L.mem s (regWA t layout.reg.NBSEND_RECV_DEST)).bv) : + syscall.dispatch_syscall st ar s a (syscallNumberA call) ⦃ r => + StepPostA Rel (fl4SettleDispatch pending (fl4DispatchSyscall invoke call)) σ r ⦄ := by + have capRead := run_liftEA (ε := Unit) (capRegisterReadA hcur hk cap_register) + have replyRead := run_liftEA (ε := Unit) (capRegisterReadA hcur hk replyRegister) + have destRead := run_liftEA (ε := Unit) (capRegisterReadA hcur hk nbsendRecvDest) + rw [cap] at capRead + rw [reply] at replyRead + rw [dest] at destRead + unfold syscall.dispatch_syscall + rw [hct, bind_ok, tcb_reg_eqA, bind_ok, tcb_reg_eqA, bind_ok] + simp only [← syscallNumberA.eq_1, ← syscallNumberA.eq_2, ← syscallNumberA.eq_3, + ← syscallNumberA.eq_4, ← syscallNumberA.eq_5, ← syscallNumberA.eq_6, ← syscallNumberA.eq_7, + ← syscallNumberA.eq_8, ← syscallNumberA.eq_9, ← syscallNumberA.eq_10, ← syscallNumberA.eq_11, + syscallNumber_injA] + cases call <;> simp only [fl4DispatchSyscall, fl4HandleSyscallWith, + reduceCtorEq, ↓reduceIte] + case sysRecv | sysNBRecv | sysWait | sysNBWait => + refine WP.spec_mono (B.recv _ _ (by constructor)) ?_ + rintro r ⟨τ, mem, related⟩ + exact ⟨τ, settle_ok_memA (liftE_memA mem), related⟩ + case sysYield => + exact WP.spec_mono (B.yield rfl) fun r ⟨τ, mem, related⟩ => + ⟨τ, settle_ok_memA (liftE_memA mem), related⟩ + case sysSend | sysNBSend | sysCall => + refine WP.spec_mono (dispatchInvokeOnlyA B (by constructor) rfl) ?_ + rintro r ⟨τ, mem, related⟩ + refine ⟨τ, ?_, related⟩ + obtain ⟨step, body, after⟩ := mem_bindA.1 mem + refine mem_bindA.2 ⟨step, ?_, after⟩ + rw [run_bind_okA capRead] + exact body + case sysReplyRecv | sysNBSendWait => + refine WP.spec_mono (dispatchCombinedA B (by constructor) (by constructor)) ?_ + rintro r ⟨τ, mem, related⟩ + refine ⟨τ, ?_, related⟩ + obtain ⟨step, body, after⟩ := mem_bindA.1 mem + refine mem_bindA.2 ⟨step, ?_, after⟩ + unfold withoutPreemption + rw [run_bind_okA replyRead] + exact body + case sysNBSendRecv => + rw [tcb_reg_eqA, bind_ok] + refine WP.spec_mono (dispatchCombinedA B (by constructor) (by constructor)) ?_ + rintro r ⟨τ, mem, related⟩ + refine ⟨τ, ?_, related⟩ + obtain ⟨step, body, after⟩ := mem_bindA.1 mem + refine mem_bindA.2 ⟨step, ?_, after⟩ + unfold withoutPreemption + rw [run_bind_okA destRead] + exact body + +private theorem liftedSingletonA {α : Type} {m : SMonad Unit α} {σ τ : State Unit} {v : α} + (h : m σ = ({(v, τ)}, False)) : + (NonDetE.liftE (ε := Unit) m).run σ = ({(.ok v, τ)}, False) := by + unfold NonDetE.liftE + rw [ExceptT.run_lift] + change (m >>= fun v => NonDet.pure (Except.ok v : Except Unit α)) σ = _ + rw [nd_bind_oneSc h] + rfl + +private theorem budgetSettledA {σ τ : State Unit} {b : Bool} {invoke pending} {call : Syscall} + (h : fl4BudgetOk (Z := Unit) σ = ({(b, τ)}, False)) : + fl4SettleDispatch pending (fl4SerialSyscallBody invoke call) σ = + (if b then fl4SettleDispatch pending (fl4DispatchSyscall invoke call) τ + else ({((), τ)}, False)) := by + have runs : (fl4SerialSyscallBody invoke call).run σ = + (NonDetE.whenE (b = true) (fl4DispatchSyscall invoke call)).run τ := by + unfold fl4SerialSyscallBody + rw [ExceptT.run_bind] + exact nd_bind_oneSc (liftedSingletonA h) _ + calc + _ = fl4SettleDispatch pending (NonDetE.whenE (b = true) (fl4DispatchSyscall invoke call)) τ := by + simp only [fl4SettleDispatch, NonDetE.catch, NonDet.bind, runs] + _ = _ := by + cases b + · unfold fl4SettleDispatch NonDetE.catch + change (NonDet.pure (Except.ok () : Except Unit Unit) >>= _) τ = _ + have one : NonDet.pure (Except.ok () : Except Unit Unit) τ = ({(.ok (), τ)}, False) := rfl + rw [nd_bind_oneSc one] + rfl + · rfl + +/-- [J] Dispatch occurs at the actual successful budget successor, or +after the observed fast decline on the same abstract state. Finish +consumes the body successor even when the budget check declined. -/ +theorem knownSyscallCore_refinesA {s : S} {a : A} {σ : State Unit} {t : U64} + (call : Syscall) (fast : Bool) {invoke pending} {Rel Final : S → A → State Unit → Prop} + (limit : Nat) (view : State Unit → Fl4TimerView) + (hct : sched.cur_thread st s = ok t) + {flags : U64} {sPC : S} {τPC : State Unit} + (writePC : obj.Tcb.set_reg st t s layout.reg.FAULT_IP + (core.num.U64.wrapping_sub (L.mem s (regWA t layout.reg.NEXT_IP)) 4#u64) = ok sPC) + (pc : fl4SyscallFaultIP (Z := Unit) σ.curThread σ = ({((), τPC)}, False)) + (readFlags : obj.Tcb.flags st t sPC = ok flags) + (untrapped : ¬ ((flags &&& fl4_abi.consts.tcb_flag.TRAP_SYSCALLS) != 0#u64) = true) + (budget : syscall.budget_ok st ar sPC a ⦃ r => ∃ τ, + fl4BudgetOk (Z := Unit) τPC = ({(r.1, τ)}, False) ∧ Rel r.2.1 r.2.2 τ ⦄) + (dispatch : ∀ sb ab sd ad τ, + syscall.budget_ok st ar sPC a = ok (true, sb, ab) → + fl4BudgetOk (Z := Unit) τPC = ({(true, τ)}, False) → + ((sd, ad) = (sb, ab) ∨ + (fast = true ∧ call = .sysReplyRecv ∧ fastpath.reply_recv st ar sb ab = ok (false, sd, ad))) → + Rel sd ad τ → syscall.dispatch_syscall st ar sd ad (syscallNumberA call) ⦃ r => + StepPostA Rel (fl4SettleDispatch pending (fl4DispatchSyscall invoke call)) τ r ⦄) + (decline : fast = true → call = .sysReplyRecv → ∀ s' a' τ, + syscall.budget_ok st ar sPC a = ok (true, s', a') → + fl4BudgetOk (Z := Unit) τPC = ({(true, τ)}, False) → Rel s' a' τ → + fastpath.reply_recv st ar s' a' ⦃ r => r.1 = false ∧ Rel r.2.1 r.2.2 τ ⦄) + (finish : ∀ s' a' τ, + ((), τ) ∈ (fl4SettleDispatch pending (fl4SerialSyscallBody invoke call) τPC).1 → + Rel s' a' τ → syscall.finish st ar s' a' ⦃ r => + StepPostA Final (fl4Finish limit view) τ r ⦄) : + syscall.syscall st ar s a (syscallNumberA call) fast ⦃ r => + r.1 = false ∧ StepPostA Final (fl4SerialSyscallEntry limit view pending invoke call) σ r.2 ⦄ := by + have finishAt (s' : S) (a' : A) (τ : State Unit) + (mem : ((), τ) ∈ (fl4SettleDispatch pending (fl4SerialSyscallBody invoke call) τPC).1) + (rel : Rel s' a' τ) : + (do let (ss, aa) ← syscall.finish st ar s' a' + ok (false, ss, aa)) ⦃ r => + r.1 = false ∧ StepPostA Final (fl4SerialSyscallEntry limit view pending invoke call) σ r.2 ⦄ := by + refine WP.spec_bind (finish s' a' τ mem rel) ?_ + rintro ⟨ss, aa⟩ ⟨υ, tail, post⟩ + refine (WP.spec_ok _).2 ⟨rfl, υ, ?_, post⟩ + unfold fl4SerialSyscallEntry fl4CallSerial + rw [nd_gets_bindSc, nd_bind_oneSc pc] + exact mem_bindA.2 ⟨((), τ), mem, tail⟩ + have dispatchAt (sb : S) (ab : A) (s' : S) (a' : A) (τ : State Unit) + (eq : syscall.budget_ok st ar sPC a = ok (true, sb, ab)) + (b : fl4BudgetOk (Z := Unit) τPC = ({(true, τ)}, False)) + (route : (s', a') = (sb, ab) ∨ + (fast = true ∧ call = .sysReplyRecv ∧ fastpath.reply_recv st ar sb ab = ok (false, s', a'))) + (rel : Rel s' a' τ) : + (do let (sd, ad) ← syscall.dispatch_syscall st ar s' a' (syscallNumberA call) + let (ss, aa) ← syscall.finish st ar sd ad + ok (false, ss, aa)) ⦃ r => + r.1 = false ∧ StepPostA Final (fl4SerialSyscallEntry limit view pending invoke call) σ r.2 ⦄ := by + refine WP.spec_bind (dispatch sb ab s' a' τ eq b route rel) ?_ + rintro ⟨sd, ad⟩ ⟨υ, mem, rel'⟩ + refine finishAt sd ad υ ?_ rel' + rw [budgetSettledA b] + exact mem + unfold syscall.syscall + rw [hct, bind_ok, tcb_reg_eqA, bind_ok] + simp only [lift, bind_ok, writePC, readFlags] + have bounds := syscallNumberInRangeA call + rw [if_neg untrapped, if_neg bounds.1, if_neg bounds.2] + refine WP.spec_bind (withReachedA budget) ?_ + rintro ⟨go, s', a'⟩ ⟨eq, τ, b, rel⟩ + cases go + · refine finishAt s' a' τ ?_ rel + rw [budgetSettledA b] + rfl + · by_cases hf : fast = true + · by_cases hr : call = .sysReplyRecv + · have num : syscallNumberA call = fl4_abi.syscall.SysReplyRecv := by rw [hr]; rfl + simp only [hf, num, ↓reduceIte] + refine WP.spec_bind (withReachedA (decline hf hr s' a' τ eq b rel)) ?_ + rintro ⟨hit, s'', a''⟩ ⟨observed, noHit, rel'⟩ + change hit = false at noHit + subst hit + change (do + let (sd, ad) ← syscall.dispatch_syscall st ar s'' a'' fl4_abi.syscall.SysReplyRecv + let (ss, aa) ← syscall.finish st ar sd ad + ok (false, ss, aa)) ⦃ _ ⦄ + rw [← num] + exact dispatchAt s' a' s'' a'' τ eq b (Or.inr ⟨hf, hr, observed⟩) rel' + · have num : syscallNumberA call ≠ fl4_abi.syscall.SysReplyRecv := fun h => + hr ((syscallNumber_injA call .sysReplyRecv).1 h) + simp only [hf, num, ↓reduceIte] + exact dispatchAt s' a' s' a' τ eq b (Or.inl rfl) rel + · simp only [hf, ↓reduceIte] + exact dispatchAt s' a' s' a' τ eq b (Or.inl rfl) rel + +/-- The public syscall wrappers drop the fast-path result. The serial +hardware dispatcher can use the same core result with the Boolean +preserved via `syscall.handle_syscall_fast`. -/ +theorem syscallReturn_refinesA {s : S} {a : A} {number : I64} (fast : Bool) {Post : S × A → Prop} + (core : syscall.syscall st ar s a number fast ⦃ r => Post r.2 ⦄) : + (if fast then syscall.handle_syscall st ar s a number + else syscall.handle_syscall_general st ar s a number) ⦃ Post ⦄ := by + cases fast <;> simp only [Bool.false_eq_true, ↓reduceIte, syscall.handle_syscall, syscall.handle_syscall_general] + all_goals + refine WP.spec_bind core ?_ + rintro ⟨_, s', a'⟩ h + exact (WP.spec_ok (s', a')).2 h + +/-- [J] With fast paths disabled, the word selected by the caller (raw +trapped X8 or bit-cast signed X7) reaches the ordinary unknown-syscall +fault handler. Both budget outcomes use the same finish. -/ +theorem unknownSyscallFault_refinesA {s : S} {a : A} {σ : State Unit} (number : U64) + {Rel Final : S → A → State Unit → Prop} (limit : Nat) (view : State Unit → Fl4TimerView) + (budget : syscall.budget_ok st ar s a ⦃ r => ∃ τ, + fl4BudgetOk (Z := Unit) σ = ({(r.1, τ)}, False) ∧ Rel r.2.1 r.2.2 τ ⦄) + (deliver : ∀ s' a' τ, syscall.budget_ok st ar s a = ok (true, s', a') → + fl4BudgetOk (Z := Unit) σ = ({(true, τ)}, False) → Rel s' a' τ → + (do let t ← sched.cur_thread st s'; faults.handle_fault st ar s' a' t (.UnknownSyscall number)) ⦃ r => + StepPostA Rel (do let t ← NonDet.gets fun k : State Unit => k.curThread + handleFault t (.unknownSyscallException number.bv)) τ r ⦄) + (finish : ∀ s' a' τ, ((), τ) ∈ (fl4BudgetFault (Z := Unit) (.unknownSyscallException number.bv) σ).1 → + Rel s' a' τ → syscall.finish st ar s' a' ⦃ r => StepPostA Final (fl4Finish limit view) τ r ⦄) : + syscall.unknown_syscall_fault st ar s a number false ⦃ r => r.1 = false ∧ + StepPostA Final (fl4BudgetFault (.unknownSyscallException number.bv) >>= fun _ => fl4Finish limit view) σ r.2 ⦄ := by + have close (s' : S) (a' : A) (τ : State Unit) + (mem : ((), τ) ∈ (fl4BudgetFault (Z := Unit) (.unknownSyscallException number.bv) σ).1) + (related : Rel s' a' τ) : + (do let (s'', a'') ← syscall.finish st ar s' a' + ok (false, s'', a'')) ⦃ r => r.1 = false ∧ + StepPostA Final (fl4BudgetFault (.unknownSyscallException number.bv) >>= fun _ => fl4Finish limit view) σ r.2 ⦄ := by + refine WP.spec_bind (finish s' a' τ mem related) ?_ + rintro ⟨s'', a''⟩ ⟨υ, tail, final⟩ + exact (WP.spec_ok (false, s'', a'')).2 ⟨rfl, υ, mem_bindA.2 ⟨((), τ), mem, tail⟩, final⟩ + unfold syscall.unknown_syscall_fault + refine WP.spec_bind (withReachedA budget) ?_ + rintro ⟨go, s', a'⟩ ⟨seen, τ, bspec, rel⟩ + cases go + · refine close s' a' τ ?_ rel + rw [fl4BudgetFault, nd_bind_oneSc bspec] + rfl + · change (do let t ← sched.cur_thread st s' + let (s'', a'') ← faults.handle_fault st ar s' a' t (.UnknownSyscall number) + let (sf, af) ← syscall.finish st ar s'' a'' + ok (false, sf, af)) ⦃ _ ⦄ + obtain ⟨r, run, υ, delivered, rRel⟩ := spec_existsA (deliver s' a' τ seen bspec rel) + obtain ⟨t, current, fault⟩ := bind_eq_ok run + change faults.handle_fault st ar s' a' t (.UnknownSyscall number) = ok r at fault + rw [current, bind_ok, fault, bind_ok] + obtain ⟨sf, af⟩ := r + refine close sf af υ ?_ rRel + rw [fl4BudgetFault, nd_bind_oneSc bspec] + exact delivered + +/-- [J] The saved NextIP is projected from the current TCB. The wrapper +writes just FaultIP, using modulo-64-bit subtraction of the SVC width. -/ +theorem syscallFaultIP_eqA {s : S} {σ : State Unit} {t : U64} {x : Tcb} + (hcur : σ.curThread = specPtr t) (hk : σ.kheap (specPtr t) = some (.tcb x)) + (next : x.tcbArch.tcbContext.userRegs nextInstructionRegister = + (L.mem s (regWA t layout.reg.NEXT_IP)).bv) : + let ip := core.num.U64.wrapping_sub (L.mem s (regWA t layout.reg.NEXT_IP)) 4#u64 + obj.Tcb.set_reg st t s layout.reg.FAULT_IP ip = ok (L.wr s (regA t layout.reg.FAULT_IP) ip) ∧ + fl4SyscallFaultIP (Z := Unit) σ.curThread σ = + ({((), updKSc σ (specPtr t) (.tcb (yieldRegA x faultRegister ip.bv)))}, False) := by + dsimp only + refine ⟨Endp.set_reg_eqA _ _ _ _, ?_⟩ + rw [hcur, core.num.U64.wrapping_sub_bv_eq, ← next, ← yieldAsUserRegA hk] + unfold fl4SyscallFaultIP asUser + have get : NonDet.getsThe (getTcb (Z := Unit) (specPtr t)) σ = ({(x, σ)}, False) := by + simp [NonDet.getsThe, getTcb, readObject, hk, NonDet.assertOpt] + rfl + simp only [nd_bind_oneSc get, nd_pure_bindSc] + have regs : + (NonDet.bind (getRegister nextInstructionRegister) fun n => setRegister faultRegister (n - 4)) + (archTcbContextGet x.tcbArch) = + setRegister faultRegister (x.tcbArch.tcbContext.userRegs nextInstructionRegister - 4) + (archTcbContextGet x.tcbArch) := by + change (getRegister nextInstructionRegister >>= _) (archTcbContextGet x.tcbArch) = _ + rw [getRegister, nd_gets_bindSc] + rfl + rw [regs] <;> rfl + +/-- The pending IRQ reported by the platform is independent of any +earlier budget preemption probe. An absent IRQ keeps the poll's state. -/ +def pendingIrqA (poll : SMonad Unit (Option Word)) (service : Word → SMonad Unit Unit) : + SMonad Unit Unit := do + let irq ← poll + match irq with + | none => pure () + | some irq => service irq + +/-- [P] The take operation relates the concrete IAR/filtering observation +to the abstract IRQ option and carries its architecture successor. +[J] Service consumes that exact observation and successor. -/ +theorem pendingIrq_refinesA {s : S} {a : A} {σ : State Unit} + {poll : SMonad Unit (Option Word)} {service : Word → SMonad Unit Unit} + {Rel : S → A → State Unit → Prop} + (take : ar.take_pending_irq a ⦃ r => + ∃ τ, (if r.1 = syscall.NO_IRQ then none else some r.1.bv, τ) ∈ (poll σ).1 ∧ Rel s r.2 τ ⦄) + (handle : ∀ irq a' τ, ar.take_pending_irq a = ok (irq, a') → + irq ≠ syscall.NO_IRQ → (some irq.bv, τ) ∈ (poll σ).1 → Rel s a' τ → + interrupt.handle_interrupt st ar s a' irq ⦃ r => StepPostA Rel (service irq.bv) τ r ⦄) : + (do let (irq, a') ← ar.take_pending_irq a + syscall.check_interrupt st ar s a' irq) ⦃ r => + StepPostA Rel (pendingIrqA poll service) σ r ⦄ := by + refine WP.spec_bind (withReachedA take) ?_ + rintro ⟨irq, a'⟩ ⟨obs, τ, mem, rel⟩ + change ((if irq = syscall.NO_IRQ then none else some irq.bv), τ) ∈ (poll σ).1 at mem + change syscall.check_interrupt st ar s a' irq ⦃ _ ⦄ + unfold syscall.check_interrupt + by_cases h : irq = syscall.NO_IRQ + · subst irq + have guard : ¬ ((syscall.NO_IRQ != syscall.NO_IRQ) = true) := by simp + rw [if_neg guard, WP.spec_ok] + simp only [↓reduceIte] at mem + exact ⟨τ, mem_bindA.2 ⟨(none, τ), mem, rfl⟩, rel⟩ + · have guard : (irq != syscall.NO_IRQ) = true := by simpa using h + rw [if_pos guard] + rw [if_neg h] at mem + refine WP.spec_mono (handle irq a' τ obs h mem rel) ?_ + rintro r ⟨υ, tail, rel'⟩ + exact ⟨υ, mem_bindA.2 ⟨(some irq.bv, τ), mem, tail⟩, rel'⟩ + +/-- [J] At a partial invocation successor the runnable branch uses the +existing plain budget check. An active outgoing SC uses timeout-disabled +charge. The inactive branch resets the node accumulator. +[P] The pending IRQ is taken once, after each of those paths. -/ +theorem afterInvocationPreempt_refinesA {s : S} {a : A} {σ : State Unit} + {pending : SMonad Unit Unit} {Rel Final : S → A → State Unit → Prop} + {t : U64} (thread : sched.cur_thread st s = ok t) + (eligible : sched.is_schedulable st s t = ok (schedulable σ.curThread σ)) + (check : schedulable σ.curThread σ = true → sched.check_budget st ar s a ⦃ r => + ∃ τ, (r.1, τ) ∈ (fl4CheckBudget σ).1 ∧ Rel r.2.1 r.2.2 τ ⦄) + (readSC : schedulable σ.curThread σ = false → ∃ (sc : U64) (active : Bool), + sched.cur_sc st s = ok sc ∧ sporadic.sc_active st s sc = ok active ∧ + getScActive σ.curSc σ = ({(active, σ)}, False)) + (consumed : sched.consumed st s = ok ⟨σ.consumedTime⟩) + (charge : schedulable σ.curThread σ = false → + getScActive σ.curSc σ = ({(true, σ)}, False) → + sched.charge_budget st ar s a ⟨σ.consumedTime⟩ false ⦃ r => + StepPostA Rel (fl4ChargeBudget σ.consumedTime false) σ r ⦄) + (clear : schedulable σ.curThread σ = false → + getScActive σ.curSc σ = ({(false, σ)}, False) → + (do let cpu ← st.cur_cpu s + let s' ← obj.node_set st s cpu layout.node.CONSUMED 0#u64 + ok (s', a)) ⦃ r => Rel r.1 r.2 {σ with consumedTime := 0} ⦄) + (take : ∀ r τ, syscall.mcs_preemption_point st ar s a = ok r → Rel r.1 r.2 τ → + (do let (irq, a') ← ar.take_pending_irq r.2 + syscall.check_interrupt st ar r.1 a' irq) ⦃ out => StepPostA Final pending τ out ⦄) : + syscall.after_invocation st ar s a (.Err .Preempted) ⦃ r => + r.1 = false ∧ StepPostA Final (fl4PreemptionPath pending) σ r.2 ⦄ := by + have continueFrom (r : S × A) (τ : State Unit) + (seen : syscall.mcs_preemption_point st ar s a = ok r) (related : Rel r.1 r.2 τ) + (advance : ∀ υ, ((), υ) ∈ (pending τ).1 → ((), υ) ∈ (fl4PreemptionPath pending σ).1) : + syscall.after_invocation st ar s a (.Err .Preempted) ⦃ out => + out.1 = false ∧ StepPostA Final (fl4PreemptionPath pending) σ out.2 ⦄ := by + obtain ⟨s', a'⟩ := r + unfold syscall.after_invocation + rw [seen, bind_ok] + change (do let (irq, ap) ← ar.take_pending_irq a' + let (sf, af) ← syscall.check_interrupt st ar s' ap irq + ok (false, sf, af)) ⦃ _ ⦄ + obtain ⟨out, taken, υ, tail, related'⟩ := spec_existsA (take (s', a') τ seen related) + obtain ⟨⟨irq, a2⟩, hw, checked⟩ := bind_eq_ok taken + change syscall.check_interrupt st ar s' a2 irq = ok out at checked + rw [hw, bind_ok] + change (do let (sf, af) ← syscall.check_interrupt st ar s' a2 irq + ok (false, sf, af)) ⦃ _ ⦄ + rw [checked, bind_ok] + obtain ⟨sf, af⟩ := out + exact (WP.spec_ok (false, sf, af)).2 ⟨rfl, υ, advance υ tail, related'⟩ + have mcsEq : syscall.mcs_preemption_point st ar s a = + (if schedulable σ.curThread σ then + (do let (_, s', a') ← sched.check_budget st ar s a; ok (s', a')) + else do + let sc ← sched.cur_sc st s + let active ← sporadic.sc_active st s sc + if active then + let c ← sched.consumed st s + sched.charge_budget st ar s a c false + else + let cpu ← st.cur_cpu s + let s' ← obj.node_set st s cpu layout.node.CONSUMED 0#u64 + ok (s', a)) := by + unfold syscall.mcs_preemption_point + rw [thread, bind_ok, eligible, bind_ok] + cases e : schedulable σ.curThread σ + · obtain ⟨sc, active, hsc, hactive, specActive⟩ := readSC e + simp only [e, Bool.false_eq_true, ↓reduceIte] at mcsEq + rw [hsc, bind_ok, hactive, bind_ok] at mcsEq + cases active + · simp only [Bool.false_eq_true, ↓reduceIte] at mcsEq + obtain ⟨r, eq, related⟩ := spec_existsA (clear e specActive) + refine continueFrom r {σ with consumedTime := 0} (mcsEq.trans eq) related ?_ + intro υ mem + unfold fl4PreemptionPath + rw [nd_gets_bindSc, nd_gets_bindSc] + simp only [e, Bool.false_eq_true, ↓reduceIte] + rw [nd_gets_bindSc, nd_bind_oneSc specActive] + exact mem_bindA.2 ⟨((), {σ with consumedTime := 0}), rfl, mem⟩ + · simp only [↓reduceIte] at mcsEq + rw [consumed, bind_ok] at mcsEq + obtain ⟨r, eq, τ, charged, related⟩ := spec_existsA (charge e specActive) + refine continueFrom r τ (mcsEq.trans eq) related ?_ + intro υ mem + unfold fl4PreemptionPath + rw [nd_gets_bindSc, nd_gets_bindSc] + simp only [e, Bool.false_eq_true, ↓reduceIte] + rw [nd_gets_bindSc, nd_bind_oneSc specActive] + change ((), υ) ∈ ((NonDet.gets (fun k : State Unit => k.consumedTime) >>= fun c => + fl4ChargeBudget c false >>= fun _ => pending) σ).1 + rw [nd_gets_bindSc] + exact mem_bindA.2 ⟨((), τ), charged, mem⟩ + · obtain ⟨⟨b, s', a'⟩, eq, τ, checked, related⟩ := spec_existsA (check e) + simp only [e, ↓reduceIte, eq, bind_ok] at mcsEq + refine continueFrom (s', a') τ mcsEq related ?_ + intro υ mem + unfold fl4PreemptionPath + rw [nd_gets_bindSc, nd_gets_bindSc] + simp only [e, ↓reduceIte] + exact mem_bindA.2 ⟨(b, τ), checked, mem⟩ + +/-- [P] Reuse P1's existing fast-hit agreement at the serial entry. +A decline has the exact general result. A hit preserves the general +postcondition and retains the observed true indicator for root repair. -/ +theorem syscallFastResult_refinesA {s : S} {a : A} {number : I64} {Post : S × A → Prop} + (totalStore : Fl4ProofsA64.P0.StoreTotal st) (totalArch : Fl4ProofsA64.P0.ArchTotal ar) + (hit : FastAcc st ar s a number Post) + (slow : syscall.handle_syscall_general st ar s a number ⦃ Post ⦄) : + syscall.handle_syscall_fast st ar s a number ⦃ r => Post r.2 ⦄ := by + obtain ⟨⟨b, s', a'⟩, run, _⟩ := spec_existsA + (Fl4ProofsA64.P0.syscall.handle_syscall_fast.p0 st ar s a number totalStore totalArch) + rw [run, WP.spec_ok] + cases b + · have internal : syscall.syscall st ar s a number true = ok (false, s', a') := run + have general : syscall.handle_syscall_general st ar s a number = ok (s', a') := by + unfold syscall.handle_syscall_general + rw [syscall_decline st ar internal, bind_ok] + rfl + rwa [general, WP.spec_ok] at slow + · exact hit slow s' a' run + +/-- [J] The trapped SVC arm reads raw X8 after installing FaultIP. +Its payload does not depend on the signed ABI number in X7. -/ +theorem trappedSyscallCore_refinesA {s : S} {a : A} {σ : State Unit} {t : U64} + {sPC : S} {τPC : State Unit} {flags : U64} + (number : I64) (fast : Bool) {Final : S → A → State Unit → Prop} + (limit : Nat) (view : State Unit → Fl4TimerView) + (hct : sched.cur_thread st s = ok t) + (writePC : obj.Tcb.set_reg st t s layout.reg.FAULT_IP + (core.num.U64.wrapping_sub (L.mem s (regWA t layout.reg.NEXT_IP)) 4#u64) = ok sPC) + (pc : fl4SyscallFaultIP (Z := Unit) σ.curThread σ = ({((), τPC)}, False)) + (readFlags : obj.Tcb.flags st t sPC = ok flags) + (trapped : ((flags &&& fl4_abi.consts.tcb_flag.TRAP_SYSCALLS) != 0#u64) = true) + (fault : syscall.unknown_syscall_fault st ar sPC a (L.mem sPC (regWA t layout.reg.X8)) fast ⦃ r => + r.1 = false ∧ StepPostA Final + (fl4BudgetFault (.unknownSyscallException (L.mem sPC (regWA t layout.reg.X8)).bv) >>= + fun _ => fl4Finish limit view) τPC r.2 ⦄) : + syscall.syscall st ar s a number fast ⦃ r => r.1 = false ∧ StepPostA Final (do + let ct ← NonDet.gets (fun k : State Unit => k.curThread) + fl4SyscallFaultIP ct + fl4BudgetFault (.unknownSyscallException (L.mem sPC (regWA t layout.reg.X8)).bv) + fl4Finish limit view) σ r.2 ⦄ := by + unfold syscall.syscall + rw [hct, bind_ok, tcb_reg_eqA, bind_ok] + simp only [lift, bind_ok, writePC, readFlags, trapped, ↓reduceIte, tcb_reg_eqA] + refine WP.spec_mono fault ?_ + rintro r ⟨slow, υ, mem, related⟩ + refine ⟨slow, υ, ?_, related⟩ + rw [nd_gets_bindSc, nd_bind_oneSc pc] + exact mem + +/-- [S] Beyond the signed API range, exclude only the selected special +numbers. The remaining unknown-syscall path bit-casts the original X7 +word, preserving every bit before fault delivery. -/ +theorem ordinaryUnknownRoute_eqA {s : S} {a : A} {t : U64} (number : I64) (fast : Bool) + (hct : sched.cur_thread st s = ok t) + (put : number ≠ fl4_abi.syscall.SysDebugPutChar) + (dump : number ≠ fl4_abi.syscall.SysDebugDumpScheduler) + (debug : syscall.debug_syscall st ar s a t number = ok (false, s, a)) + (tls : number ≠ fl4_abi.syscall.SysSetTLSBase) + (vm : number ≠ fl4_abi.syscall.SysVMEnter) : + syscall.handle_unknown_syscall st ar s a number fast = + syscall.unknown_syscall_fault st ar s a (IScalar.hcast .U64 number) fast := by + unfold syscall.handle_unknown_syscall + simp only [hct, bind_ok, put, dump, debug, tls, vm, fl4_abi.config.PRINTING, + fl4_abi.config.DEBUG_BUILD, fl4_abi.config.SET_TLS_BASE_SELF, ↓reduceIte, lift] + rfl + +/-- The native synchronous slow dispatch in `entry.rs`. The vector +selects using the full shifted ESR. The VM fault encoder independently +masks the EC field to distinguish data and instruction payloads. -/ +def nativeSyncCoreA (st : machine.Store S) (ar : machine.Arch A) + (s : S) (a : A) (esr far elr : U64) : Result (S × A) := + if esr.bv >>> 26 = 0x24 then syscall.handle_vm_fault st ar s a far esr + else if esr.bv >>> 26 = 0x20 then syscall.handle_vm_fault st ar s a elr esr + else syscall.handle_user_exception st ar s a esr 0#u64 + +/-- [S] The vector admitted a native synchronous slow entry after the +SVC and enabled-FP alternatives were handled. [P] ESR, FAR and saved +ELR are the full observed words. [J] Each concrete leaf uses the +corresponding payload at this admitted state. -/ +theorem nativeSyncEntry_refinesA {s : S} {a : A} {σ : State Unit} + (esr far elr : U64) {Final : S → A → State Unit → Prop} + (limit : Nat) (view : State Unit → Fl4TimerView) + (dataAbort : esr.bv >>> 26 = 0x24 → + syscall.handle_vm_fault st ar s a far esr ⦃ r => + StepPostA Final (fl4BudgetFault (fl4VmFault far.bv esr.bv) >>= fun _ => fl4Finish limit view) σ r ⦄) + (insnAbort : esr.bv >>> 26 = 0x20 → + syscall.handle_vm_fault st ar s a elr esr ⦃ r => + StepPostA Final (fl4BudgetFault (fl4VmFault elr.bv esr.bv) >>= fun _ => fl4Finish limit view) σ r ⦄) + (userExn : esr.bv >>> 26 ≠ 0x24 → esr.bv >>> 26 ≠ 0x20 → + syscall.handle_user_exception st ar s a esr 0#u64 ⦃ r => + StepPostA Final (fl4BudgetFault (.userException esr.bv 0) >>= fun _ => fl4Finish limit view) σ r ⦄) : + nativeSyncCoreA st ar s a esr far elr ⦃ r => + StepPostA Final (fl4BudgetFault (fl4NativeSyncFault esr.bv far.bv elr.bv) >>= fun _ => fl4Finish limit view) σ r ⦄ := by + unfold nativeSyncCoreA fl4NativeSyncFault + by_cases dataSel : esr.bv >>> 26 = 0x24 + · simp only [dataSel, ↓reduceIte] + exact dataAbort dataSel + · by_cases insnSel : esr.bv >>> 26 = 0x20 + · simp only [dataSel, insnSel, ↓reduceIte] + exact insnAbort insnSel + · simp only [dataSel, insnSel, ↓reduceIte] + exact userExn dataSel insnSel + +/-- The root-cap pair compared by the Rust native dispatch adapters. +The post-body read is guarded by the saved TCB still being current. -/ +def readEntryRootsA (st : machine.Store S) (s : S) (t : U64) : + Result (capability.Cap × capability.Cap) := do + let vslot ← obj.Tcb.slot t layout.tcb.SLOT_VTABLE + let vcap ← obj.Slot.cap st vslot s + let hslot ← obj.Tcb.slot t layout.tcb.SLOT_HIGH_VSPACE + let hcap ← obj.Slot.cap st hslot s + ok (vcap, hcap) + +/-- Same-thread root repair around the admitted SVC and synchronous +fault bodies. Normal IRQ entry has its separate return adapter. -/ +noncomputable def repairEntryRootsA (st : machine.Store S) (ar : machine.Arch A) + (old : U64) (roots : capability.Cap × capability.Cap) (s : S) (a : A) : Result (S × A) := by + classical + exact do + let current ← sched.cur_thread st s + if current = old then + let now ← readEntryRootsA st s old + if now = roots then ok (s, a) else vspace.set_vm_root st ar s a old + else ok (s, a) + +open Classical in +/-- [J] On return from the body, compare both observed root caps on the +same thread. [P] Reinstallation uses the selected thread's reached +root view and the ordered root writes from the architecture contract. -/ +theorem repairEntryRoots_refinesA {s : S} {a : A} {σ : State Unit} + {old current : U64} {roots newRoots : capability.Cap × capability.Cap} + {reinstall : SMonad Unit Unit} {Final : S → A → State Unit → Prop} + (thread : sched.cur_thread st s = ok current) + (readRoots : current = old → readEntryRootsA st s old = ok newRoots) + (stay : (current = old → newRoots = roots) → Final s a σ) + (writeRoots : current = old → newRoots ≠ roots → + vspace.set_vm_root st ar s a old ⦃ r => StepPostA Final reinstall σ r ⦄) : + repairEntryRootsA st ar old roots s a ⦃ r => + StepPostA Final (if current = old ∧ newRoots ≠ roots then reinstall else pure ()) σ r ⦄ := by + classical + unfold repairEntryRootsA + rw [thread, bind_ok] + by_cases hct : current = old + · rw [if_pos hct, readRoots hct, bind_ok] + by_cases same : newRoots = roots + · rw [if_pos same] + exact (WP.spec_ok (s, a)).2 ⟨σ, by simp [same] <;> rfl, stay fun _ => same⟩ + · rw [if_neg same] + rw [if_pos (show current = old ∧ newRoots ≠ roots from ⟨hct, same⟩)] + exact writeRoots hct same + · rw [if_neg hct] + exact (WP.spec_ok (s, a)).2 ⟨σ, by simp [hct] <;> rfl, stay fun h => (hct h).elim⟩ + +/-- The dispatch adapter snapshots both root caps, runs the selected +body and repairs the roots before returning its fast-path indicator. -/ +def withEntryRootsA {α : Type} (st : machine.Store S) (ar : machine.Arch A) + (s : S) (a : A) (body : S → A → Result (α × S × A)) : Result (α × S × A) := do + let old ← sched.cur_thread st s + let roots ← readEntryRootsA st s old + let (result, s', a') ← body s a + let (s'', a'') ← repairEntryRootsA st ar old roots s' a' + ok (result, s'', a'') + +/-- [J] The input root read and the result's root repair are tied to the +body's actual output. [P] Return preserves the body's indicator and uses +the post-repair hardware state. -/ +theorem withEntryRoots_refinesA {α : Type} {s : S} {a : A} {σ : State Unit} + {old : U64} {roots : capability.Cap × capability.Cap} + {body : S → A → Result (α × S × A)} {op : SMonad Unit α} + {Rel Final : S → A → State Unit → Prop} (returnRoots : State Unit → SMonad Unit Unit) + (thread : sched.cur_thread st s = ok old) + (readRoots : readEntryRootsA st s old = ok roots) + (runBody : body s a ⦃ r => + ∃ τ, (r.1, τ) ∈ (op σ).1 ∧ Rel r.2.1 r.2.2 τ ⦄) + (repair : ∀ (r : α × S × A) τ, body s a = ok r → (r.1, τ) ∈ (op σ).1 → + Rel r.2.1 r.2.2 τ → repairEntryRootsA st ar old roots r.2.1 r.2.2 ⦃ out => + StepPostA Final (returnRoots τ) τ out ⦄) : + withEntryRootsA st ar s a body ⦃ r => + ∃ τ υ, (r.1, τ) ∈ (op σ).1 ∧ + ((), υ) ∈ (returnRoots τ τ).1 ∧ Final r.2.1 r.2.2 υ ⦄ := by + unfold withEntryRootsA + rw [thread, bind_ok, readRoots, bind_ok] + refine WP.spec_bind (withReachedA runBody) ?_ + rintro ⟨result, s', a'⟩ ⟨seen, τ, mem, rel⟩ + refine WP.spec_bind (repair (result, s', a') τ seen mem rel) ?_ + rintro ⟨s'', a''⟩ ⟨υ, tail, final⟩ + exact (WP.spec_ok (result, s'', a'')).2 ⟨τ, υ, mem, tail, final⟩ + +/-- [P] The admitted vector supplies the GIC result and the post-IAR +architecture state. [S] A normal IRQ enters the serial core; an absent +classification leaves the post-IAR state before the hardware return. -/ +theorem nativeIrqAdapter_refinesA {s : S} {a : A} {σ : State Unit} + (irq : Option U64) {Final : S → A → State Unit → Prop} + (limit : Nat) (view : State Unit → Fl4TimerView) (service : U64 → SMonad Unit Unit) + (nonePost : irq = none → Final s a σ) + (irqPost : ∀ number, irq = some number → + syscall.handle_interrupt_entry st ar s a number ⦃ r => + StepPostA Final (fl4AcceptedIrqBody (service number) >>= fun _ => fl4Finish limit view) σ r ⦄) : + (match irq with + | none => ok (s, a) + | some number => syscall.handle_interrupt_entry st ar s a number) ⦃ r => + StepPostA Final (match irq with + | none => pure () + | some number => fl4AcceptedIrqBody (service number) >>= fun _ => fl4Finish limit view) σ r ⦄ := by + cases irq with + | none => exact (WP.spec_ok (s, a)).2 ⟨σ, rfl, nonePost rfl⟩ + | some number => exact irqPost number rfl + +end + +end Fl4ProofsA64.P3.OuterEntry diff --git a/proofs/Fl4ProofsA64/P3/PtCapBootA.lean b/proofs/Fl4ProofsA64/P3/PtCapBootA.lean new file mode 100644 index 000000000..682582388 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/PtCapBootA.lean @@ -0,0 +1,103 @@ +/- +P3 (aarch64, A64-P3 S3 gen 29, ruling 00:45Z item 3): **`PtCapInA` (`P3/FinKeepA`) at the boot witnesses `w0` and +`w1`** (x86 `EptCore.capTbl` at `JBootCs.w0` / `JBoot1Cs.w1`). + +* `w0`: every relation slot is null (`boot_null`), so no slot holds a mapped page-table cap: **`ptCapIn_w0A`**, at + the concrete layout **`ptCapIn_w0A_ex`** (the `Z` and memory of `boot_jEntA_ex`). +* `w1`: the only mapped page-table caps are the root CNode's tail slots 0–2, the root task's level-1 / -2 / -3 tables + (`capOf_ptA`: their pointers are `L1b`, `L2b`, `L3b`), which are `w1Tb` tables at depth 1, 2, 3 (`lvL1` …): + **`ptCapIn_w1A`**, at the concrete layout **`ptCapIn_w1A_ex`** (the `Z` and memory of `w1_jEntA_ex`). + +No named premise. New module; no existing statement changed. +-/ +import Fl4ProofsA64.P3.FinBootA +import Fl4ProofsA64.P3.BootAddrA +import Fl4ProofsA64.P3.FinKeepA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P1.W7.R.ST.BootWit + +noncomputable section + +/-! ## `w0` -/ + +section w0 +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {kb ib now : U64} {nc : Nat} + +/-- **`PtCapInA` at `w0`** (module docstring). -/ +theorem ptCapIn_w0A {s : S} (hL : BootLayA ib nc) (hW : BootWordsA st s kb ib now nc) : + PtCapInA (bootZA kb ib nc) (L.mem s) := by + intro y hy ptr asid va e + rw [boot_null hL hW hy] at e + cases e + +end w0 + +/-- **`PtCapInA` at A64-Boot's concrete `w0`** (the ghosts and memory of `boot_jEntA_ex`). -/ +theorem ptCapIn_w0A_ex (r now mt : U64) : + PtCapInA (bootZA 18446603337294938112#u64 18446603338368679936#u64 128) (bL.mem (bootS 18446603337294938112#u64 18446603338368679936#u64 128 r now mt)) := by + have hi : (18446603338368679936#u64 : U64).val = 18446603338368679936 := by simp + have hk : (18446603337294938112#u64 : U64).val = 18446603337294938112 := by simp + exact ptCapIn_w0A (bootLayA_of bootLayC_ex (Or.inl (by rw [hi]; omega))) + (bootWordsA_w0 (r := r) (mt := mt) bootLayC_ex (Or.inl (by rw [hi]; omega)) (Or.inl (by rw [hk]; omega))) + +/-! ## `w1` -/ + +/-- **The mapped page-table caps of the root CNode name the root task's level-1 / -2 / -3 tables.** -/ +theorem capOf_ptA {nc i : Nat} {ptr asid va : U64} (e : capOf nc i = .PageTable ptr true asid va) : + ptr = bwv L1b ∨ ptr = bwv L2b ∨ ptr = bwv L3b := by + unfold capOf at e + by_cases h1 : i < 16 + · rw [slotC_lo h1] at e + unfold fixC at e + split_ifs at e <;> first | cases e | simp [frC] at e + by_cases h2 : i < 16 + nc + · rw [slotC_sch (by omega) h2] at e; cases e + rw [slotC_hi (by omega)] at e + unfold tailC at e + split_ifs at e <;> first + | (cases e; first | exact Or.inl rfl | exact Or.inr (Or.inl rfl) | exact Or.inr (Or.inr rfl)) + | cases e + | simp [frC] at e + +/-- The three tables are `w1Tb` tables below the root. -/ +theorem w1_ptTabA {ptr : U64} (h : ptr = bwv L1b ∨ ptr = bwv L2b ∨ ptr = bwv L3b) : + ptr ∈ w1Tb ∧ 1 ≤ w1Lv ptr := by + rcases h with rfl | rfl | rfl + · exact ⟨Or.inr (Or.inl vL1), by rw [lvL1 vL1]⟩ + · exact ⟨Or.inr (Or.inr (Or.inl vL2)), by rw [lvL2 vL2]; omega⟩ + · exact ⟨Or.inr (Or.inr (Or.inr vL3)), by rw [lvL3 vL3]; omega⟩ + +section w1 +variable {kb ib r now mt sl : U64} {nc : Nat} {uw : U64 → U64} + +/-- **`PtCapInA` at `w1`** (module docstring). -/ +theorem ptCapIn_w1A (hL : BootLayA ib nc) (hK : BootKsA kb ib nc) (P : BootLay1 kb ib nc) : + PtCapInA (w1ZA kb ib nc) (bL.mem (w1S kb ib nc r now mt sl uw)) := by + intro y hy ptr asid va e + have hy' : y ∈ w1OS kb ib nc := by rw [← w1OS_eqA hL hK P]; exact hy + change capA bst (w1S kb ib nc r now mt sl uw) y = _ at e + refine Or.inl (w1_ptTabA ?_) + rcases w1_capS P hy' with h | ⟨i, -, -, h⟩ | ⟨-, h⟩ | ⟨-, h⟩ | ⟨-, h⟩ | ⟨i, -, -, -, h⟩ <;> rw [h] at e + · cases e + · exact capOf_ptA e + · exact capOf_ptA e + · exact capOf_ptA e + · cases e + · cases e + +end w1 + +/-- **`PtCapInA` at A64-Boot's concrete `w1`** (the ghosts and memory of `w1_jEntA_ex`). -/ +theorem ptCapIn_w1A_ex (r now mt sl : U64) (uw : U64 → U64) : + PtCapInA (w1ZA 18446603337294938112#u64 18446603338368679936#u64 128) + (bL.mem (w1S 18446603337294938112#u64 18446603338368679936#u64 128 r now mt sl uw)) := by + have hi : (18446603338368679936#u64 : U64).val = 18446603338368679936 := by simp + have hk : (18446603337294938112#u64 : U64).val = 18446603337294938112 := by simp + have P := bootLay1C_ex + exact ptCapIn_w1A (bootLayA_of P.w0 (Or.inl (by rw [hi]; omega))) (bootKsA_of P.w0 (Or.inl (by rw [hk]; omega))) P + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/PtrA.lean b/proofs/Fl4ProofsA64/P3/PtrA.lean new file mode 100644 index 000000000..14dd72348 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/PtrA.lean @@ -0,0 +1,179 @@ +/- +P3 (aarch64, A64-P3 VS gen 2): **the pointer base shift `specPtr`** (Architect ruling 13:38Z, Q1) — the uniform +additive renaming of FL4's kernel-window pointers into l4v's AArch64 kernel window, so that l4v's +`ptrFromPAddr` / `addrFromPPtr` (`Fl4SpecA64/Machine`, `Platform.AARCH64`) compute on FL4's objects exactly. + +FL4's kernel window starts at `PPTR_BASE = 0xffff_8000_0000_0000` (`layout.rs`, `fl4PptrBaseA`); l4v's at +`pptrBase = 0x80_0000_0000` (2^39) with `paddrBase = 0`, so `pptrBaseOffset = pptrBase`. The shift is +`pptrShiftA = pptrBase - PPTR_BASE` (mod 2^64) **`= 0x0000_8080_0000_0000`** (2^47 + 2^39, `pptrShiftA_eq`; the +ruling's figure `0x0001_0080_0000_0000` is 2^48 + 2^39, which would be the shift from a base of +`0xffff_0000_0000_0000`; the ruling's formula with the real constants gives this one). It is a multiple of 2^39. + +* `specPtr p = p.bv + pptrShiftA`, `fl4Ptr q = q - pptrShiftA`: **`fl4Ptr_specPtr`**, **`specPtr_fl4Ptr`** + (a bijection of `U64` and `Word`), `specPtr_inj`; +* **`specPtr_wa`**: `specPtr (p ⊕ k) = specPtr p + k` (FL4's `wrapping_add`), `specPtr_toNat`; +* **`specPtr_mod`**: alignment kept for every power `2^n`, `n ≤ 39` (`specPtr_aligned`: l4v's `is_aligned`); +* **`addrFromPPtr_specPtr`**: `addrFromPPtr (specPtr p) = p - PPTR_BASE` (FL4's `layout::paddr`); + **`ptrFromPAddr_eq`**: `ptrFromPAddr pa = specPtr (pa + PPTR_BASE)` (FL4's `layout::pptr`); both exact in + 64-bit arithmetic, no side condition; +* **The windows** (`specPtr_window`): an FL4 kernel-window pointer `p` (`PPTR_BASE ≤ p < PPTR_TOP`) lands in + l4v's `[pptrBase, pptrTop)` **iff** its physical address `p - PPTR_BASE` is below l4v's `paddrTop = + 2^39 - 2^30`. FL4's window is 127 TiB (`PPTR_TOP = 0xffff_ff00_0000_0000`), l4v's 511 GiB, so the full + window does **not** map into l4v's (`specPtr_window_not`: the pointer of physical address `paddrTop` is + FL4-window but lands at `pptrTop`). The identities above do not need the window; the open design question is + in the handoff (an l4v-side `pspace_in_kernel_window` would need FL4's RAM below `paddrTop`, a [P]). + +No named premise. No `Fl4SpecA64` edit. +-/ +import Fl4ProofsA64.P1.QueueCapRt +import Fl4SpecA64 +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-- `wrapping_add` by value (gen 12: here, so that `CapRelA` can import this module; `FrameA.wa_valm` is this lemma). -/ +theorem wa_valP (x k : U64) : (wa x k).val = (x.val + k.val) % 2 ^ 64 := by + simp only [core.num.U64.wrapping_add_val_eq, UScalar.size_UScalarTyU64, U64.size_def, U64.numBits, + UScalarTy.U64_numBits_eq] + +/-- FL4's `PPTR_BASE` as a spec word. -/ +def fl4PptrBaseA : Fl4SpecA64.Word := 0xffff800000000000#64 + +/-- FL4's `PPTR_TOP` as a spec word. -/ +def fl4PptrTopA : Fl4SpecA64.Word := 0xffffff0000000000#64 + +theorem pptrBase_fl4A : layout.PPTR_BASE.bv = fl4PptrBaseA := by + unfold layout.PPTR_BASE fl4PptrBaseA; rfl + +/-- **The shift** (module docstring): l4v's `pptrBase` minus FL4's `PPTR_BASE`. -/ +def pptrShiftA : Fl4SpecA64.Word := Fl4SpecA64.pptrBase - fl4PptrBaseA + +theorem pptrShiftA_eq : pptrShiftA = 0x0000808000000000#64 := by + unfold pptrShiftA Fl4SpecA64.pptrBase fl4PptrBaseA; decide + +theorem pptrShiftA_toNat : pptrShiftA.toNat = 141287244169216 := by + rw [pptrShiftA_eq]; rfl + +/-- **An FL4 pointer as l4v's** (module docstring). -/ +def specPtr (p : U64) : Fl4SpecA64.Word := p.bv + pptrShiftA + +/-- **l4v's pointer as FL4's** (module docstring). -/ +def fl4Ptr (q : Fl4SpecA64.Word) : U64 := ⟨q - pptrShiftA⟩ + +theorem specPtr_def (p : U64) : specPtr p = p.bv + pptrShiftA := rfl + +theorem fl4Ptr_def (q : Fl4SpecA64.Word) : fl4Ptr q = ⟨q - pptrShiftA⟩ := rfl + +theorem fl4Ptr_specPtr (p : U64) : fl4Ptr (specPtr p) = p := by + apply UScalar.eq_of_val_eq + show (p.bv + pptrShiftA - pptrShiftA).toNat = p.bv.toNat + rw [BitVec.add_sub_cancel] + +theorem specPtr_fl4Ptr (q : Fl4SpecA64.Word) : specPtr (fl4Ptr q) = q := by + show q - pptrShiftA + pptrShiftA = q + rw [BitVec.sub_add_cancel] + +theorem specPtr_inj {p q : U64} (h : specPtr p = specPtr q) : p = q := by + rw [← fl4Ptr_specPtr p, h, fl4Ptr_specPtr] + +theorem specPtr_iff {p q : U64} : specPtr p = specPtr q ↔ p = q := ⟨specPtr_inj, fun h => h ▸ rfl⟩ + +theorem specPtr_toNat (p : U64) : (specPtr p).toNat = (p.val + 141287244169216) % 2 ^ 64 := by + unfold specPtr + rw [BitVec.toNat_add, pptrShiftA_toNat] + rfl + +/-- (gen 12) The shift commutes with a spec-side offset of a raw word. -/ +theorem specPtr_mk_add (x k : BitVec 64) : specPtr ⟨x + k⟩ = specPtr ⟨x⟩ + k := by + show x + k + pptrShiftA = x + pptrShiftA + k + rw [BitVec.add_assoc, BitVec.add_comm k, ← BitVec.add_assoc] + +/-- (gen 12) **The FL4 pointer l4v's `idle_sc_ptr` names**: `0xffff_8000_0000_3000` (physical `0x3000`). -/ +theorem fl4Ptr_idleSc : (fl4Ptr Fl4SpecA64.idleScPtr).val = 18446603336221208576 := by decide + +theorem specPtr_eq_idleSc {p : U64} : specPtr p = Fl4SpecA64.idleScPtr ↔ p.val = 18446603336221208576 := by + constructor + · intro h; rw [← fl4Ptr_specPtr p, h]; exact fl4Ptr_idleSc + · intro h + rw [show p = fl4Ptr Fl4SpecA64.idleScPtr from UScalar.eq_of_val_eq (h.trans fl4Ptr_idleSc.symm), + specPtr_fl4Ptr] + +/-- **The shift commutes with offsets** (module docstring). -/ +theorem specPtr_wa (p k : U64) : specPtr (wa p k) = specPtr p + k.bv := by + apply BitVec.eq_of_toNat_eq + rw [specPtr_toNat, BitVec.toNat_add, specPtr_toNat, wa_valP] + have : k.bv.toNat = k.val := rfl + rw [this] + omega + +/-- **Alignment is kept** below 2^39 (module docstring). -/ +theorem specPtr_mod (p : U64) {n : Nat} (hn : n ≤ 39) : (specPtr p).toNat % 2 ^ n = p.val % 2 ^ n := by + rw [specPtr_toNat] + have h1 : 2 ^ n ∣ 2 ^ 64 := Nat.pow_dvd_pow 2 (by omega) + have h2 : 2 ^ n ∣ 141287244169216 := + Nat.dvd_trans (Nat.pow_dvd_pow 2 hn) ⟨257, by decide⟩ + rw [Nat.mod_mod_of_dvd _ h1, Nat.add_mod, Nat.mod_eq_zero_of_dvd h2, Nat.add_zero, Nat.mod_mod] + +theorem specPtr_aligned (p : U64) {n : Nat} (hn : n ≤ 39) : + Fl4SpecA64.isAligned (specPtr p) n ↔ p.val % 2 ^ n = 0 := by + unfold Fl4SpecA64.isAligned + rw [Nat.dvd_iff_mod_eq_zero, specPtr_mod p hn] + +/-- **l4v's `addrFromPPtr` of a shifted pointer** is FL4's physical address (module docstring). -/ +theorem addrFromPPtr_specPtr (p : U64) : Fl4SpecA64.addrFromPPtr (specPtr p) = p.bv - fl4PptrBaseA := by + unfold Fl4SpecA64.addrFromPPtr Fl4SpecA64.pptrBaseOffset Fl4SpecA64.paddrBase specPtr pptrShiftA + generalize Fl4SpecA64.pptrBase = b + bv_omega + +/-- **l4v's `ptrFromPAddr`** is the shift of FL4's `pptr` (module docstring). -/ +theorem ptrFromPAddr_eq (pa : Fl4SpecA64.Paddr) : + Fl4SpecA64.ptrFromPAddr pa = specPtr ⟨pa + fl4PptrBaseA⟩ := by + unfold Fl4SpecA64.ptrFromPAddr Fl4SpecA64.pptrBaseOffset Fl4SpecA64.paddrBase specPtr pptrShiftA + show pa + (Fl4SpecA64.pptrBase - 0) = pa + fl4PptrBaseA + (Fl4SpecA64.pptrBase - fl4PptrBaseA) + generalize Fl4SpecA64.pptrBase = b + bv_omega + +/-- `ptrFromPAddr` of FL4's `pptr` argument: FL4's `layout::pptr pa = pa ⊕ PPTR_BASE`. -/ +theorem ptrFromPAddr_wa (pa : U64) : + Fl4SpecA64.ptrFromPAddr pa.bv = specPtr (wa pa layout.PPTR_BASE) := by + rw [ptrFromPAddr_eq] + apply congrArg specPtr + apply UScalar.eq_of_val_eq + rw [wa_valP] + show (pa.bv + fl4PptrBaseA).toNat = _ + rw [BitVec.toNat_add, ← pptrBase_fl4A] + rfl + +theorem pptrBase_toNatA : Fl4SpecA64.pptrBase.toNat = 549755813888 := by unfold Fl4SpecA64.pptrBase; rfl + +theorem pptrTop_toNatA : Fl4SpecA64.pptrTop.toNat = 1098437885952 := by unfold Fl4SpecA64.pptrTop; rfl + +/-- **The windows** (module docstring): an FL4 kernel-window pointer is in l4v's window iff its physical address is +below `paddrTop = 2^39 - 2^30`. -/ +theorem specPtr_window (p : U64) (h1 : 18446603336221196288 ≤ p.val) (h2 : p.val < 18446742974197923840) : + (Fl4SpecA64.pptrBase.toNat ≤ (specPtr p).toNat ∧ (specPtr p).toNat < Fl4SpecA64.pptrTop.toNat) ↔ + p.val - 18446603336221196288 < 549755813888 - 1073741824 := by + rw [specPtr_toNat, pptrBase_toNatA, pptrTop_toNatA] + omega + +/-- **FL4's window does not fit l4v's** (module docstring): the FL4-window pointer of physical address `paddrTop` +lands on l4v's `pptrTop`. -/ +theorem specPtr_window_not : + (specPtr ⟨fl4PptrBaseA + 548682072064#64⟩).toNat = Fl4SpecA64.pptrTop.toNat ∧ + (⟨fl4PptrBaseA + 548682072064#64⟩ : U64).val < 18446742974197923840 := by + rw [specPtr_toNat, pptrTop_toNatA] + exact ⟨by decide, by decide⟩ + +/-! (gen 12) `specPtr` / `fl4Ptr` are opaque to the unifier: a `kheap` lookup or a `Function.update` at a shifted +pointer would otherwise evaluate `(p + shift) % 2^64` symbolically (maximum recursion); proofs use the lemmas above +(`specPtr_def`, `fl4Ptr_def`). -/ +attribute [irreducible] specPtr fl4Ptr + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/ReplyCallerA.lean b/proofs/Fl4ProofsA64/P3/ReplyCallerA.lean new file mode 100644 index 000000000..9a7f080e1 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ReplyCallerA.lean @@ -0,0 +1,50 @@ +/- +P3 (AArch64, delete hooks W3 Reply): **the caller of a reply is blocked on reply or on receive** (A64 P1's +`ReplyInv0.back`, in the record's `BXI`), so the final Reply arm of `finalise_cap_object` for a thread in any other +state is unreachable. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3BA +import Fl4ProofsA64.P3.DelEpRecA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4Spec Fl4SpecA64 Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Sched + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} [J7PpA st J7] {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- **The caller of a reply** (module docstring). -/ +theorem JStage3BA.replyCallerA + (J : JStage3BA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {p : U64} (hH : H p = some .reply) (h0 : L.mem s (Endp.rTcbA p) ≠ 0#u64) : + L.mem s (Endp.rTcbA p) ∈ Z.T ∧ + (L.mem s (Fl4ProofsA64.P1.stA (L.mem s (Endp.rTcbA p))) = sched.TS_BLOCKED_ON_REPLY ∨ + L.mem s (Fl4ProofsA64.P1.stA (L.mem s (Endp.rTcbA p))) = sched.TS_BLOCKED_ON_RECV) := by + have blk := J.blkA + have R := blk.2.2.1.2.1 + have hp : p ∈ H.replies := hH + have b := R.back p hp h0 + have ht : L.mem s (Endp.rTcbA p) ∈ H.tcbs := b.2.1 + exact ⟨J.tcbs ▸ ht, b.2.2.2⟩ + +/-- `JStage3BA.replyCallerA` at `JStage3TA`. -/ +theorem JStage3TA.replyCallerA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {p : U64} (hH : H p = some .reply) (h0 : L.mem s (Endp.rTcbA p) ≠ 0#u64) : + L.mem s (Endp.rTcbA p) ∈ Z.T ∧ + (L.mem s (Fl4ProofsA64.P1.stA (L.mem s (Endp.rTcbA p))) = sched.TS_BLOCKED_ON_REPLY ∨ + L.mem s (Fl4ProofsA64.P1.stA (L.mem s (Endp.rTcbA p))) = sched.TS_BLOCKED_ON_RECV) := + J.toBA.replyCallerA hH h0 + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/ReplyCancelFootA.lean b/proofs/Fl4ProofsA64/P3/ReplyCancelFootA.lean new file mode 100644 index 000000000..c992d1766 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ReplyCancelFootA.lean @@ -0,0 +1,236 @@ +/- +P3 (AArch64, delete hooks W3 Reply): **`cancel_ipc` of a receiver holding a reply object**, with a precise footprint and +the spec side's keeps. + +* `cancel_ipc_recvF`: the writes are the thread's fault word, the endpoint finaliser's words (`EpSigWA`) and the + scheduler words (`Wr`). `cancel_ipc_footA` is too coarse for the delete loop's record (it lists the buffer words). +* `cancelRecv_specA`: the spec's single outcome, as `EpSpecKeepA` after the fault clear, and the loop invariants. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.DelEpLoopA +import Fl4ProofsA64.P3.CancelIpcKeepA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +section Fl4 +variable {S A : Type} {st : machine.Store S} [L : LawfulStore st] {ar : machine.Arch A} +variable {Z : XSetsA} {m : McState} {T E R : Set U64} {Bf : Set (U64 × Word)} {SR : S → State Unit → Prop} + +/-- **The footprint of `cancel_ipc` of a receiver** (module docstring). -/ +theorem cancel_ipc_recvF {Wr : State Unit → S → U64 → Prop} + (hO : SchedOpsEA st T E R Bf SR) (hF : SchedFootE0A st T E R Bf SR Wr) + (hP : EPlaced T E R Bf) (s : S) (a : A) (σ : State Unit) + (hRE : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) {t : U64} (ht : t ∈ T) {x : Tcb} + (hk : σ.kheap (specPtr t) = some (.tcb x)) {e r : U64} (he : e ∈ E) {z : Endpoint} + (hkz : σ.kheap (specPtr e) = some (.endpoint z)) (hq : specPtr t ∈ z.epQueue) (hbe : L.mem s (tBlockA t) = e) + (hr : r ∈ R) {xr : Reply} (hkr : σ.kheap (specPtr r) = some (.reply xr)) + {d : ReceiverPayload} (hs : x.tcbState = .blockedOnReceive (specPtr e) (some (specPtr r)) d) + {Q : Set U64} (htQ : t ∈ Q) (hQs : ∀ w ∈ T, L.mem s (tBlockA w) = e → w ∈ Q) : + ipc.cancel_ipc st ar s a t ⦃ out => WritesIn st s out.1 + (fun b => b = tFaultA t ∨ EpSigWA R Q e b ∨ Wr σ s b) ⦄ := by + have hta := hP.tcb t ht + have hx := hRE.at_tcb ht hk + obtain ⟨hR1, hW1, hS1, hag⟩ := clear_fault_rel hP s σ hRE ht hk + have vf := val_fault hta + have hb1 : L.mem (L.wr s (tFaultA t) 0#u64) (tBlockA t) = e := + (hag _ (a_blk hta).1 (by dsimp only; rw [val_blk hta, vf]; omega)).trans hbe + have wF : WritesIn st s (L.wr s (tFaultA t) 0#u64) (fun b => b = tFaultA t) := writesIn_wr (a_fault hta).1 + unfold ipc.cancel_ipc faults.clear_fault + step with obj.Tcb.set_fault_type.spec as ⟨s1, hs1⟩ + have e1 : s1 = L.wr s (tFaultA t) 0#u64 := by rw [hs1]; unfold fl4_abi.consts.fault.NullFault; rfl + subst e1 + step with obj.Tcb.state.spec as ⟨w, hw⟩ + have hw' : w.val = tsCode x.tcbState := by + rw [hw, hag _ (a_st hta).1 (by dsimp only; rw [val_st hta, vf]; omega)]; exact hx.st + have hwR : w = sched.TS_BLOCKED_ON_RECV := + UScalar.eq_of_val_eq (by rw [hw', hs, tsVals.2.2.1]; rfl) + have hSR4 : sched.TS_BLOCKED_ON_RECV ≠ sched.TS_BLOCKED_ON_SEND := by rw [tsVals.2.2.1, tsVals.2.2.2.1]; decide + rw [if_neg (by rw [hwR]; exact hSR4), if_pos hwR] + -- the state after the fault clear + have hte : specPtr e ≠ specPtr t := by intro h; rw [h, hk] at hkz; cases hkz + have hrt : specPtr r ≠ specPtr t := by intro h; rw [h, hk] at hkr; cases hkr + have hre : specPtr r ≠ specPtr e := by intro h; rw [h, hkz] at hkr; cases hkr + have hk1 : (updK σ (specPtr t) (.tcb { x with tcbFault := none })).kheap (specPtr t) = + some (.tcb { x with tcbFault := none }) := by rw [updK_kheap, Function.update_self] + have hkz1 : (updK σ (specPtr t) (.tcb { x with tcbFault := none })).kheap (specPtr e) = some (.endpoint z) := by + rw [updK_kheap, Function.update_of_ne hte, hkz] + have hkr1 : (updK σ (specPtr t) (.tcb { x with tcbFault := none })).kheap (specPtr r) = some (.reply xr) := by + rw [updK_kheap, Function.update_of_ne hrt, hkr] + have hSR1 : SR (L.wr s (tFaultA t) 0#u64) (updK σ (specPtr t) (.tcb { x with tcbFault := none })) := + hO.stable _ _ _ _ hSR hW1 hS1 (NoBlockE.updT hk _ (Or.inl ⟨rfl, fun _ => + Or.inr (Or.inr (Or.inr ⟨_, _, _, hs⟩))⟩)) + have z1 : SSzA σ (updK σ (specPtr t) (.tcb { x with tcbFault := none })) := ssz_updK_tcb _ _ _ + unfold epArmProg at * + have hep : obj.Tcb.block_obj st t (L.wr s (tFaultA t) 0#u64) ⦃ i => i = e ⦄ := by + step with obj.Tcb.block_obj.spec as ⟨i, hi⟩ + exact hi.trans hb1 + refine WP.spec_bind hep ?_ + rintro i rfl + obtain ⟨xs, hQr, hxs, hB⟩ := (hR1.at_ep he hkz1).queue + have htx : t ∈ xs := by + have hq' := hq + rw [hxs, List.mem_map] at hq' + obtain ⟨y, hy, ey⟩ := hq' + rw [← specPtr_inj ey]; exact hy + have hxsQ : ∀ v ∈ xs, v ∈ Q := fun v hv => by + have vT := hQr.mem v hv + have va := hP.tcb v vT + refine hQs v vT ?_ + rw [← (hB v hv).1] + by_cases ev : v = t + · rw [ev]; exact (hag _ (a_blk hta).1 (by dsimp only; rw [val_blk hta, vf]; omega)).symm + · refine (hag _ (a_blk va).1 ?_).symm + have := tcb_disj hP ht vT ev + dsimp only; rw [val_blk va, vf]; omega + refine WP.spec_bind (WP.spec_and (ep_dequeue_rel hO hP _ _ hR1 hSR1 he hkz1 hq) + (ep_dequeue_sigE hP _ he t xs hQr htx (Q := Q) hxsQ)) ?_ + rintro s2 ⟨⟨-, hR2, hSR2, hA2⟩, w2⟩ + have z2 : SSzA σ (updK (updK σ (specPtr t) (.tcb { x with tcbFault := none })) (specPtr i) + (.endpoint (epDeq z (specPtr t)))) := z1.trans (ssz_of_epStep (EpStep.ep hkz1)) + have hkd : (updK (updK σ (specPtr t) (.tcb { x with tcbFault := none })) (specPtr i) + (.endpoint (epDeq z (specPtr t)))).kheap (specPtr t) = some (.tcb { x with tcbFault := none }) := by + rw [updK_kheap, Function.update_of_ne hte.symm, hk1] + have hkyd : (updK (updK σ (specPtr t) (.tcb { x with tcbFault := none })) (specPtr i) + (.endpoint (epDeq z (specPtr t)))).kheap (specPtr i) = some (.endpoint (epDeq z (specPtr t))) := by + rw [updK_kheap, Function.update_self] + have hkrd : (updK (updK σ (specPtr t) (.tcb { x with tcbFault := none })) (specPtr i) + (.endpoint (epDeq z (specPtr t)))).kheap (specPtr r) = some (.reply xr) := by + rw [updK_kheap, Function.update_of_ne hre, hkr1] + have hb2 : L.mem s2 (tBlockA t) = i := (hA2 _ (a_blk hta).1 ⟨t, ht, (a_blk hta).2⟩).trans hb1 + have hq2 := dequeued_not_queued hR2 he hb2 hkyd (epDeq_not_mem hxs hQr.nodup t) + have hrec := (hR2.at_tcb ht hkd).recv (specPtr i) (some (specPtr r)) d hs + have hrep : obj.Tcb.reply_obj st t s2 ⦃ v => v = r ⦄ := by + step with obj.Tcb.reply_obj.spec as ⟨v, hv⟩ + have hv' : v = L.mem s2 (tReplyA t) := hv + have h1 := hrec.1 + rw [← hv'] at h1 + have v0 : v ≠ 0#u64 := fun e0 => by rw [e0, optRef_zero] at h1; cases h1 + rw [optRef_ne v0] at h1 + exact specPtr_inj (Option.some.inj h1) + have r0 : r ≠ 0#u64 := fun e0 => (hP.rep r hr).2.1 (by rw [e0]; rfl) + simp only [hwR, ↓reduceIte, Std.bind_assoc] + refine WP.spec_bind hrep ?_ + rintro v rfl + have vn : (v != 0#u64) = true := by simpa using r0 + simp only [vn, ↓reduceIte] + have hblk : runnable x.tcbState = false ∧ x.tcbState ≠ .idleThreadState := by + rw [hs]; exact ⟨rfl, fun h => by cases h⟩ + refine WP.spec_bind (reply_unlink_sigE hO hF hP hSR2 hR2 (WritesIn.refl _ _) (EpStep.refl _) ht hkd hkd + (OnlyT.refl _ _) hblk hq2 hr hkrd (Q := Q) (e := i) htQ) ?_ + rintro s3 ⟨hR3, hSR3, w3⟩ + have hk3 : (updK (updK (updK σ (specPtr t) (.tcb { x with tcbFault := none })) (specPtr i) + (.endpoint (epDeq z (specPtr t)))) (specPtr v) (.reply { xr with replyTcb := none })).kheap (specPtr t) = + some (.tcb { x with tcbFault := none }) := by rw [updK_kheap, Function.update_of_ne hrt.symm, hkd] + obtain ⟨hk4, hq4, -, -⟩ := sts_facts hk3 (notq_updR (E := E) hq2 v { xr with replyTcb := none }) .inactive + have z3 := z2.trans ((ssz_updK_reply _ (specPtr v) { xr with replyTcb := none }).trans + ((ssz_updK_tcb _ (specPtr t) { { x with tcbFault := none } with tcbState := .inactive }).trans + (ssz_stsAct _ (specPtr t)))) + refine WP.spec_bind (set_thread_state_sigE hO hF hP hSR3 _ _ hR3 (WritesIn.refl _ _) (EpStep.refl _) ht hk4 hq4 + .inactive (by decide) sched.TS_INACTIVE (by unfold sched.TS_INACTIVE; rfl) + (NoBlockE.updT hk4 _ (Or.inl ⟨rfl, fun h => h⟩)) (Q := Q) (e := i) htQ) ?_ + rintro s4 ⟨-, -, w4⟩ + rw [WP.spec_ok] + have k2 : L.ksb s2 = L.ksb s := w2.1.trans wF.1 + have k3 : L.ksb s3 = L.ksb s := w3.1.trans k2 + refine (WritesIn.trans (st := st) wF (WritesIn.trans (st := st) w2 + (WritesIn.trans (st := st) w3 w4))).mono fun b h => ?_ + rcases h with h | h | (h | h) | (h | h) + · exact Or.inl h + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr (hF.back z2 k2 h)) + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr (hF.back z3 k3 h)) + +end Fl4 + +/-- `set_thread_state`'s action step leaves no pending switch. -/ +theorem stsAct_noSwA (σ : State Unit) (t : ObjRef) (h : ∀ u, σ.schedulerAction ≠ .switchThread u) : + ∀ u, (stsAct σ t).schedulerAction ≠ .switchThread u := by + intro u + unfold stsAct + split + · exact fun e => by cases e + · exact h u + +/-- **The spec side of `cancel_ipc` of a receiver holding a reply object** with the frame of the other objects. -/ +theorem cancelRecv_specKA {σ : State Unit} {t e r : ObjRef} {x : Tcb} (hk : σ.kheap t = some (.tcb x)) + {z : Endpoint} (hkz : σ.kheap e = some (.endpoint z)) (hq : t ∈ z.epQueue) + {xr : Reply} (hkr : σ.kheap r = some (.reply xr)) (hback : xr.replyTcb = some t) + {d : ReceiverPayload} (hs : x.tcbState = .blockedOnReceive e (some r) d) : + ∃ σ', cancelIpc (Z := Unit) t σ = ({((), σ')}, False) ∧ + EpSpecKeepA (updK σ t (.tcb { x with tcbFault := none })) σ' ∧ + σ'.kheap r = some (.reply { xr with replyTcb := none }) ∧ + (∀ mc n, LoopInvA (updK σ t (.tcb { x with tcbFault := none })) mc n → + OffCurA (updK σ t (.tcb { x with tcbFault := none })) mc n t → LoopInvA σ' mc n) ∧ + ((∀ u, σ.schedulerAction ≠ .switchThread u) → ∀ u, σ'.schedulerAction ≠ .switchThread u) ∧ + (∀ q, q ≠ t → q ≠ e → q ≠ r → σ'.kheap q = σ.kheap q) ∧ + σ'.curThread = σ.curThread ∧ σ'.idleThread = σ.idleThread ∧ σ'.curSc = σ.curSc := by + have het : e ≠ t := by intro h; rw [h, hk] at hkz; cases hkz + have hrt : r ≠ t := by intro h; rw [h, hk] at hkr; cases hkr + have hre : r ≠ e := by intro h; rw [h, hkz] at hkr; cases hkr + let σ1 := updK σ t (.tcb { x with tcbFault := none }) + have hkz1 : σ1.kheap e = some (.endpoint z) := by + simp only [σ1, updK_kheap, Function.update_of_ne het]; exact hkz + have hdq := tcbEpDequeue_eq (Z := Unit) hkz1 hq + let σ2 := updK σ1 e (.endpoint (epDeq z t)) + have hk2 : σ2.kheap t = some (.tcb { x with tcbFault := none }) := by + simp only [σ2, σ1, updK_kheap, Function.update_of_ne het.symm, Function.update_self] + have hkr2 : σ2.kheap r = some (.reply xr) := by + simp only [σ2, σ1, updK_kheap, Function.update_of_ne hre, Function.update_of_ne hrt]; exact hkr + have hun := replyUnlinkTcb_eq hkr2 hback hk2 (ep := e) (d := d) (by exact hs) + let σ3 := updK σ2 r (.reply { xr with replyTcb := none }) + have hk3 : σ3.kheap t = some (.tcb { x with tcbFault := none }) := by + simp only [σ3, updK_kheap, Function.update_of_ne hrt.symm]; exact hk2 + let x1 : Tcb := { { x with tcbFault := none } with tcbState := .inactive } + let σ4 := stsAct (updK σ3 t (.tcb x1)) t + have hk4 : σ4.kheap t = some (.tcb x1) := by simp only [σ4, stsAct_kheap, updK_kheap, Function.update_self] + have hnn : (∀ n, x.tcbState ≠ .blockedOnNotification n) ∧ ∀ r, x.tcbState ≠ .blockedOnReply r := + ⟨(fun n h => by rw [hs] at h; cases h), (fun n h => by rw [hs] at h; cases h)⟩ + refine ⟨_, cancelIpc_blocked hk (some r) (Or.inr ⟨e, d, hs⟩) (bci_reply (by rw [hs]; rfl) hdq hun hk4), + ?_, ?_, ?_, fun h => stsAct_noSwA (updK σ4 t (.tcb { x1 with tcbState := .inactive })) t + (stsAct_noSwA (updK σ3 t (.tcb x1)) t h), + fun q q1 q2 q3 => by + simp only [σ4, σ3, σ2, σ1, stsAct_kheap, updK_kheap, Function.update_of_ne q1, Function.update_of_ne q2, + Function.update_of_ne q3], + by simp only [σ4]; unfold stsAct; split_ifs <;> rfl, by simp only [σ4]; unfold stsAct; split_ifs <;> rfl, + by simp only [σ4]; unfold stsAct; split_ifs <;> rfl⟩ + · have k1 : EpSpecKeepA σ1 σ2 := EpSpecKeepA.of_upd hkz1 (o' := .endpoint (epDeq z t)) ⟨_, rfl⟩ + have k2 : EpSpecKeepA σ2 σ3 := EpSpecKeepA.of_upd hkr2 (o' := .reply { xr with replyTcb := none }) ⟨_, rfl, rfl⟩ + have k3 : EpSpecKeepA σ3 σ4 := epk_sts hk3 hnn (Or.inr rfl) + have k4 : EpSpecKeepA σ4 (stsAct (updK σ4 t (.tcb { x1 with tcbState := .inactive })) t) := + epk_sts hk4 ⟨(fun n h => by cases h), (fun r h => by cases h)⟩ (Or.inr rfl) + exact (k1.trans k2).trans (k3.trans k4) + · rw [stsAct_kheap, updK_kheap, Function.update_of_ne hrt] + simp only [σ4, stsAct_kheap, updK_kheap, Function.update_of_ne hrt, σ3, Function.update_self] + · intro mc n h o + have hblk : runnable x.tcbState = false := by rw [hs]; rfl + have h2 : LoopInvA σ2 mc n := h.updEp hkz1 + have h3 : LoopInvA σ3 mc n := h2.updReply hkr2 + have o3 : OffCurA σ3 mc n t := (o.updKA _ _).updKA _ _ + have h4 : LoopInvA σ4 mc n := h3.setBlk hk3 (by exact hblk) o3 (Or.inr rfl) (fun e => by cases e) + have o4 : OffCurA σ4 mc n t := (o3.updKA _ _).stsA _ + exact h4.setBlk hk4 rfl o4 (Or.inr rfl) (fun e => by cases e) + +/-- **The spec side of `cancel_ipc` of a receiver holding a reply object** (module docstring). -/ +theorem cancelRecv_specA {σ : State Unit} {t e r : ObjRef} {x : Tcb} (hk : σ.kheap t = some (.tcb x)) + {z : Endpoint} (hkz : σ.kheap e = some (.endpoint z)) (hq : t ∈ z.epQueue) + {xr : Reply} (hkr : σ.kheap r = some (.reply xr)) (hback : xr.replyTcb = some t) + {d : ReceiverPayload} (hs : x.tcbState = .blockedOnReceive e (some r) d) : + ∃ σ', cancelIpc (Z := Unit) t σ = ({((), σ')}, False) ∧ + EpSpecKeepA (updK σ t (.tcb { x with tcbFault := none })) σ' ∧ + σ'.kheap r = some (.reply { xr with replyTcb := none }) ∧ + (∀ mc n, LoopInvA (updK σ t (.tcb { x with tcbFault := none })) mc n → + OffCurA (updK σ t (.tcb { x with tcbFault := none })) mc n t → LoopInvA σ' mc n) ∧ + ((∀ u, σ.schedulerAction ≠ .switchThread u) → ∀ u, σ'.schedulerAction ≠ .switchThread u) := by + obtain ⟨σ', h1, h2, h3, h4, h5, -⟩ := cancelRecv_specKA hk hkz hq hkr hback hs + exact ⟨σ', h1, h2, h3, h4, h5⟩ + +end + +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/ReplyHookA.lean b/proofs/Fl4ProofsA64/P3/ReplyHookA.lean new file mode 100644 index 000000000..39b1933e1 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ReplyHookA.lean @@ -0,0 +1,181 @@ +/- +P3 (AArch64, delete hooks W3 Reply): **the Reply finaliser hook at a state**: the `reply_remove` arm +(`finHookReplyA_removeA`) and the assembly over all arms (`finHookReplyA`): non-final or null thread (no-op), caller +blocked on receive (`cancel_ipc`), caller blocked on reply (`reply_remove`); no other state (`replyCallerA`). + +[P] `hA`, `hCs`, `hmb`, `hmax`, `htu`; [S] `h1`. New module. +-/ +import Fl4ProofsA64.P3.ReplyHookRecvArmA +import Fl4ProofsA64.P3.ReplyHookRemoveA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 Fl4ProofsA64.P3.Endp +open Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Notif + +local notation "wa" => core.num.U64.wrapping_add + +noncomputable section + +section +variable {m : Fl4SpecA64.McState} +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} {ia ie : Nat} + {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} + {J5 J7 : JSlotA S} [J7PpA st J7] {s : S} {a : A} {σ : State Unit} {m : McState} + +/-- **The Reply finaliser hook at a state, `reply_remove` arm** (`reply_remove`; module docstring). -/ +theorem finHookReplyA_removeA {ipa : U64} (s0 : S) (hA : P0.ArchTotal ar) (h1 : fl4A64Platform.numNodes = 1) + (Rv : S → A → State Unit → MdbGhostA → U64 → Prop) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + {G : MdbGhostA} {sl : CslotPtr} {p : U64} {g : Bool} {ac : Fl4SpecA64.Cap} {f : Bool} + (D : DStateA st Z.th OS s σ G) + (W : DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st (J7R2A st ar ipa))) s a σ G) + (hsl : AbsSlotA Z.th sl) (hc : capA st s (cslotAddrA sl) = .Reply p g) + (hg : getCap (Z := Unit) sl σ = ({(ac, σ)}, False)) (hac : CapEqA (.Reply p g) ac) + (hf : f = true ↔ isFinalCap' ac σ) (hft : f = true) + (h0 : L.mem s (Endp.rTcbA p) ≠ 0#u64) + (hrply : L.mem s (Fl4ProofsA64.P1.stA (L.mem s (Endp.rTcbA p))) = sched.TS_BLOCKED_ON_REPLY) : + objects.finalise_cap st ar s a (.Reply p g) f false ⦃ r => + r.1.1 = .Null ∧ CleanCapA r.1.2 ∧ CapWfA r.1.2 ∧ + ∃ σ' G', (∃ d : Bool, (d = true ↔ isFinalCap' ac σ) ∧ + ((absCapA r.1.1, absCapA r.1.2), σ') ∈ (fl4FinaliseCap (Z := Unit) ac d σ).1) ∧ + DStateA st Z.th OS r.2.1 σ' G' ∧ capA st r.2.1 (cslotAddrA sl) = .Reply p g ∧ + (∀ y ∈ OS, y ≠ cslotAddrA sl → capA st r.2.1 y = capA st s y ∨ + (capA st r.2.1 y = .Null ∧ ¬ IsZombieA (capA st s y))) ∧ + WFA (DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st (J7R2A st ar ipa)))) + (DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st J7R2tA))) + r.1.1 (cslotAddrA sl) r.2.1 r.2.2 σ' G' ∧ + (RmOKA r.1.1 (cslotAddrA sl) → DelRdXA st ar Z H OS ipa Rv r.2.1 r.2.2 σ' G' (cslotAddrA sl)) ∧ + (∀ q zt k, r.1.1 = .Zombie q zt k → DelRzA st r.2.1 r.2.2 σ' G' (cslotAddrA sl)) ⦄ := by + subst hft + have eac : ac = absCapA (.Reply p g) := hac.symm + obtain ⟨hcore, ⟨Q, R, hw, Ac, Bf, stack, J, X⟩, hPA, hZT, hBH, hCT, hFx⟩ := W + have hOSx : cslotAddrA sl ∈ OS := D.mem_slot hsl + have hH : H p = some .reply := (J.capTy _ hOSx).2.2.1 p g hc + have hp : p ∈ Rp := J.ipcHeapCov.reply p hH + have unread : ¬ S1aR2Reads (capability.Cap.Reply p g) := fun h => h + have ht : L.mem s (Endp.rTcbA p) ∈ Z.T := (J.replyCallerA hH h0).1 + obtain ⟨x, hk, hx⟩ := J.ep.tcb _ ht + obtain ⟨ro, hs⟩ := tsCode_five (ts := x.tcbState) (by + rw [← hx.st, hrply]; unfold sched.TS_BLOCKED_ON_REPLY; rfl) + have ero : ro = specPtr p := by + have back := J.blkA.2.2.1.2.1.back p hH (by show L.mem s (Endp.rTcbA p) ≠ 0#u64; exact h0) + have hip : L.mem s (tReplyA (L.mem s (Endp.rTcbA p))) = p := back.2.2.1 + rw [← hx.reply ro hs, hip] + subst ero + obtain ⟨xr, hkr, hrb⟩ := J.ep.reply p hp + have hback : xr.replyTcb = some (specPtr (L.mem s (Endp.rTcbA p))) := by rw [hrb, optRef_ne h0] + rw [fin_cap_eq_obj (by simp)] + have eprog : objects.finalise_cap_object st ar s a (.Reply p g) true = (do + let (s1, a1) ← ipc.reply_remove st ar s a p (L.mem s (Endp.rTcbA p)) + ok ((capability.Cap.Null, capability.Cap.Null), s1, a1)) := by + have et : obj.Reply.tcb st p s = ok (L.mem s (Endp.rTcbA p)) := + eq_of_spec_fd (obj.Reply.tcb.spec (L := L) s p) + have en : obj.Tcb.is_null (L.mem s (Endp.rTcbA p)) = ok false := + (eq_of_spec_fd (obj.Tcb.is_null.spec _)).trans (by simp [h0]) + have es : obj.Tcb.state st (L.mem s (Endp.rTcbA p)) s = ok sched.TS_BLOCKED_ON_REPLY := + (eq_of_spec_fd (obj.Tcb.state.spec (L := L) s _)).trans (congrArg ok hrply) + simp only [objects.finalise_cap_object, ↓reduceIte, et, en, es, bind_ok, Bool.false_eq_true] + rw [eprog] + refine WP.spec_bind (J.replyRemove_recA (ar := ar) hA hH h0 hrply hCs h1) ?_ + rintro ⟨s', a'⟩ ⟨ha, σ', Q', R', stack', hsp, J', k, kb, cpu, F, vc, hz, off, k6⟩ + dsimp only at ha J' k kb cpu vc hz k6 ⊢ + subst ha + refine (WP.spec_ok ((capability.Cap.Null, capability.Cap.Null), s', a')).2 ?_ + have caps : ∀ y ∈ OS, capA st s' y = capA st s y := fun y hy => k.capA hy + have hc' : capA st s' (cslotAddrA sl) = .Reply p g := (caps _ hOSx).trans hc + have hmem : ((Fl4SpecA64.Cap.nullCap, Fl4SpecA64.Cap.nullCap), σ') ∈ + (fl4FinaliseCap (Z := Unit) ac true σ).1 := by + rw [eac] + simp only [absCapA] + rw [fl4FinaliseCap_plainA (by intro h; exact h)] + refine mem_bindA.2 ⟨((), σ'), ?_, rfl⟩ + refine mem_bindA.2 ⟨(some (specPtr (L.mem s (Endp.rTcbA p))), σ), by rw [getReplyTcb_eq hkr, hback]; rfl, ?_⟩ + dsimp only + refine mem_bindA.2 ⟨(x.tcbState, σ), by rw [getThreadState_eq hk]; rfl, ?_⟩ + dsimp only + rw [hs] + dsimp only + rw [hsp] + exact rfl + have D' : DStateA st Z.th OS s' σ' G := DStateA.keepOSA (Z := { Z with G := G, Q := Q', R := R', hw := hw }) D J' k + -- the reply is on no stack any more + have hdet' : ∀ q y n', σ'.kheap q = some (.schedContext y n') → specPtr p ∉ y.scReplies := by + intro q y n' hq hm + obtain ⟨c, hcc, rfl⟩ := J'.scCov q y n' hq + obtain ⟨⟨x1, k1, hx1, hL⟩, -⟩ := J'.reply.stackRel c hcc + rw [hq] at hx1 + cases hx1 + rw [hL, List.mem_map] at hm + obtain ⟨r, hr, e⟩ := hm + exact off c hcc (specPtr_inj e ▸ hr) + have rd : (DelRdA st Rv s' a' σ' G (cslotAddrA sl) ∧ RdThA st OS s' (cslotAddrA sl)) ∧ + J7LA st (J7R2xA st ar ipa (cslotAddrA sl)) { Z with G := G, Q := Q', R := R', hw := hw } H OS s' σ' m := by + refine ⟨⟨⟨fun r g' h _ => ?_, fun r g' h => (by rw [hc'] at h; cases h), + fun v h => (by rw [hc'] at h; cases h)⟩, rdThA_of_ne fun t h => by rw [hc'] at h; cases h⟩, + J'.j7.pinUnreadA (by rw [hc']; exact unread)⟩ + rw [hc'] at h + cases h + exact ⟨hdet', hz⟩ + have W' : DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st J7R2tA)) s' a' σ' G := + ⟨⟨kb.trans hcore.1, cpu.trans hcore.2⟩, ⟨Q', R', hw, Ac, Bf, stack', J'.mapJ7A fun _ _ => trivial, (k6 D.sub (X.ghostA G Q R hw)).ghostA Z.G Z.Q Z.R Z.hw⟩, + ParentAdjA.keepOS D.lk k hPA, + ZombieTcbNoVcpuA.keepA caps J.zombieTcb (fun t ht => vc t (J.tcbs ▸ ht)) hZT, + bufHold_sameA st hBH caps, ctOff_sameA st hCT kb caps, hFx.keepA st D.sub caps⟩ + exact ⟨rfl, trivial, trivial, σ', G, ⟨true, hf, hmem⟩, D', hc', fun y hy _ => Or.inl (caps y hy), + ⟨fun h => absurd (Or.inl rfl) h, fun _ => W'⟩, fun _ => ⟨rd.1, m, rd.2⟩, fun _ _ _ h => by cases h⟩ + +/-- **The Reply finaliser hook at a state** (module docstring). -/ +theorem finHookReplyA {ipa : U64} (s0 : S) + (Rv : S → A → State Unit → MdbGhostA → U64 → Prop) (hA : P0.ArchTotal ar) + (h1 : fl4A64Platform.numNodes = 1) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {G : MdbGhostA} {sl : CslotPtr} {p : U64} {g : Bool} {ac : Fl4SpecA64.Cap} {f : Bool} + (D : DStateA st Z.th OS s σ G) + (W : DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st (J7R2A st ar ipa))) s a σ G) + (hsl : AbsSlotA Z.th sl) (hc : capA st s (cslotAddrA sl) = .Reply p g) + (hg : getCap (Z := Unit) sl σ = ({(ac, σ)}, False)) (hac : CapEqA (.Reply p g) ac) + (hf : f = true ↔ isFinalCap' ac σ) : + objects.finalise_cap st ar s a (.Reply p g) f false ⦃ r => + r.1.1 = .Null ∧ CleanCapA r.1.2 ∧ CapWfA r.1.2 ∧ + ∃ σ' G', (∃ d : Bool, (d = true ↔ isFinalCap' ac σ) ∧ + ((absCapA r.1.1, absCapA r.1.2), σ') ∈ (fl4FinaliseCap (Z := Unit) ac d σ).1) ∧ + DStateA st Z.th OS r.2.1 σ' G' ∧ capA st r.2.1 (cslotAddrA sl) = .Reply p g ∧ + (∀ y ∈ OS, y ≠ cslotAddrA sl → capA st r.2.1 y = capA st s y ∨ + (capA st r.2.1 y = .Null ∧ ¬ IsZombieA (capA st s y))) ∧ + WFA (DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st (J7R2A st ar ipa)))) + (DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st J7R2tA))) + r.1.1 (cslotAddrA sl) r.2.1 r.2.2 σ' G' ∧ + (RmOKA r.1.1 (cslotAddrA sl) → DelRdXA st ar Z H OS ipa Rv r.2.1 r.2.2 σ' G' (cslotAddrA sl)) ∧ + (∀ q zt k, r.1.1 = .Zombie q zt k → DelRzA st r.2.1 r.2.2 σ' G' (cslotAddrA sl)) ⦄ := by + have hOSx : cslotAddrA sl ∈ OS := D.mem_slot hsl + by_cases hno : f = false ∨ L.mem s (Endp.rTcbA p) = 0#u64 + · refine finHookReplyA_noopA (ar := ar) (AR := AR) (Z := Z) s0 Rv D W hsl hc hg hac hf hno fun ft => ?_ + obtain ⟨-, ⟨Q, R, hw, Ac, Bf, stack, J, -⟩, -⟩ := W + refine J.reply_offStacksA J.vr fun h => ?_ + rcases hno with h0 | h0 + · rw [ft] at h0; cases h0 + · exact h.tcb h0 + have hft : f = true := by + cases f + · exact absurd (Or.inl rfl) hno + · rfl + have h0 : L.mem s (Endp.rTcbA p) ≠ 0#u64 := fun e => hno (Or.inr e) + have two : L.mem s (Fl4ProofsA64.P1.stA (L.mem s (Endp.rTcbA p))) = sched.TS_BLOCKED_ON_REPLY ∨ + L.mem s (Fl4ProofsA64.P1.stA (L.mem s (Endp.rTcbA p))) = sched.TS_BLOCKED_ON_RECV := by + have W' := W + obtain ⟨-, ⟨Q, R, hw, Ac, Bf, stack, J, -⟩, -⟩ := W' + exact (J.replyCallerA ((J.capTy _ hOSx).2.2.1 p g hc) h0).2 + rcases two with hr | hr + · exact finHookReplyA_removeA (ar := ar) (AR := AR) (Z := Z) s0 hA h1 Rv hCs D W hsl hc hg hac hf hft h0 hr + · exact finHookReplyA_recvA (ar := ar) (AR := AR) (Z := Z) s0 hA Rv hCs hmb hmax htu D W hsl hc hg hac hf hft h0 hr + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/ReplyHookNoopA.lean b/proofs/Fl4ProofsA64/P3/ReplyHookNoopA.lean new file mode 100644 index 000000000..fde62f5dc --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ReplyHookNoopA.lean @@ -0,0 +1,107 @@ +/- +P3 (AArch64, delete hooks W3 Reply): **the Reply finaliser's no-op arms** (`finHookReplyA_noopA`): a non-final cap, or a +final cap whose reply object has no thread. The conclusion is that of `finHookEpA_atA`, in the same `W` / `Rd` / `Rz`. + +The final arm with a thread in any state is **not** a no-op of the spec (`cancel_ipc` clears the fault first), so a +thread state other than blocked-on-reply / blocked-on-receive has to be excluded by the reply invariant, not stuttered. + +`hdet` (a final reply is on no SC stack) is the part of `DelRdA` the record does not give for a reply with no thread: +it holds from A64 P1's `ValidReplies`, which `JStage3TA` does not carry. New module. +-/ +import Fl4ProofsA64.P3.DelEpHookA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 Fl4ProofsA64.P3.Endp +open Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Notif + +local notation "wa" => core.num.U64.wrapping_add + +noncomputable section + +section +variable {m : Fl4SpecA64.McState} +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Pp Tb : Set U64} {Lv : U64 → Nat} {ia ie : Nat} + {N E Rp : Set U64} {κ : U64 → U64} {n : Nat} + +/-- **The Reply finaliser hook at a state, no-op arms** (module docstring). -/ +theorem finHookReplyA_noopA {ipa : U64} (s0 : S) + (Rv : S → A → State Unit → MdbGhostA → U64 → Prop) + {s : S} {a : A} {σ : State Unit} {G : MdbGhostA} {sl : CslotPtr} {p : U64} {g : Bool} + {ac : Fl4SpecA64.Cap} {f : Bool} + (D : DStateA st Z.th OS s σ G) + (W : DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st (J7R2A st ar ipa))) s a σ G) + (hsl : AbsSlotA Z.th sl) (hc : capA st s (cslotAddrA sl) = .Reply p g) + (hg : getCap (Z := Unit) sl σ = ({(ac, σ)}, False)) (hac : CapEqA (.Reply p g) ac) + (hf : f = true ↔ isFinalCap' ac σ) (hno : f = false ∨ L.mem s (Endp.rTcbA p) = 0#u64) + (hdet : f = true → ∀ q y k, σ.kheap q = some (.schedContext y k) → specPtr p ∉ y.scReplies) : + objects.finalise_cap st ar s a (.Reply p g) f false ⦃ r => + r.1.1 = .Null ∧ CleanCapA r.1.2 ∧ CapWfA r.1.2 ∧ + ∃ σ' G', (∃ d : Bool, (d = true ↔ isFinalCap' ac σ) ∧ + ((absCapA r.1.1, absCapA r.1.2), σ') ∈ (fl4FinaliseCap (Z := Unit) ac d σ).1) ∧ + DStateA st Z.th OS r.2.1 σ' G' ∧ capA st r.2.1 (cslotAddrA sl) = .Reply p g ∧ + (∀ y ∈ OS, y ≠ cslotAddrA sl → capA st r.2.1 y = capA st s y ∨ + (capA st r.2.1 y = .Null ∧ ¬ IsZombieA (capA st s y))) ∧ + WFA (DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st (J7R2A st ar ipa)))) + (DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st J7R2tA))) + r.1.1 (cslotAddrA sl) r.2.1 r.2.2 σ' G' ∧ + (RmOKA r.1.1 (cslotAddrA sl) → DelRdXA st ar Z H OS ipa Rv r.2.1 r.2.2 σ' G' (cslotAddrA sl)) ∧ + (∀ q zt k, r.1.1 = .Zombie q zt k → DelRzA st r.2.1 r.2.2 σ' G' (cslotAddrA sl)) ⦄ := by + have eac : ac = absCapA (.Reply p g) := hac.symm + obtain ⟨hcore, ⟨Q, R, hw, Ac, Bf, stack, J, X⟩, hPA, hZT, hBH, hCT, hFx⟩ := W + have hOSx : cslotAddrA sl ∈ OS := D.mem_slot hsl + have hH : H p = some .reply := (J.capTy _ hOSx).2.2.1 p g hc + have hp : p ∈ Rp := J.ipcHeapCov.reply p hH + have unread : ¬ S1aR2Reads (capability.Cap.Reply p g) := fun h => h + have rd : DelRdA st Rv s a σ G (cslotAddrA sl) ∧ RdThA st OS s (cslotAddrA sl) := by + refine ⟨⟨fun r g' h hfin => ?_, fun r g' h => (by rw [hc] at h; cases h), fun v h => (by rw [hc] at h; cases h)⟩, + rdThA_of_ne fun t h => by rw [hc] at h; cases h⟩ + rw [hc] at h hfin + cases h + have ft : f = true := hf.2 (by rw [eac]; exact hfin) + refine ⟨hdet ft, ?_⟩ + rcases hno with h0 | h0 + · rw [ft] at h0; cases h0 + · exact h0 + have j7x := J.j7.pinUnreadA (by rw [hc]; exact unread) + rw [fin_cap_eq_obj (by simp)] + have e : objects.finalise_cap_object st ar s a (.Reply p g) f = + ok ((capability.Cap.Null, capability.Cap.Null), s, a) := by + rcases hno with rfl | h0 + · rfl + cases f + · rfl + have et : obj.Reply.tcb st p s = ok 0#u64 := + (eq_of_spec_fd (obj.Reply.tcb.spec (L := L) s p)).trans (congrArg ok h0) + have en : obj.Tcb.is_null 0#u64 = ok true := + (eq_of_spec_fd (obj.Tcb.is_null.spec 0#u64)).trans (by simp) + simp only [objects.finalise_cap_object, ↓reduceIte, et, en, bind_ok] + have hnoop : ((Fl4SpecA64.Cap.nullCap, Fl4SpecA64.Cap.nullCap), σ) ∈ + (fl4FinaliseCap (Z := Unit) ac f σ).1 := by + rw [eac] + simp only [absCapA] + rw [fl4FinaliseCap_plainA (by intro h; exact h)] + refine mem_bindA.2 ⟨((), σ), ?_, rfl⟩ + rcases hno with rfl | h0 + · exact rfl + cases f + · exact rfl + obtain ⟨x, hk, hx⟩ := J.ep.reply p hp + rw [h0, optRef_zero] at hx + refine mem_bindA.2 ⟨(x.replyTcb, σ), by rw [getReplyTcb_eq hk]; rfl, ?_⟩ + dsimp only + rw [hx] + exact rfl + rw [e, WP.spec_ok] + refine ⟨rfl, trivial, trivial, σ, G, ⟨f, hf, hnoop⟩, D, hc, fun y _ _ => Or.inl rfl, ?_, ?_, + fun _ _ _ h => by cases h⟩ + · exact ⟨fun h => absurd (Or.inl rfl) h, fun _ => ⟨hcore, ⟨Q, R, hw, Ac, Bf, stack, + J.mapJ7A fun _ _ => trivial, X⟩, hPA, hZT, hBH, hCT, hFx⟩⟩ + · exact fun _ => ⟨rd, m, j7x⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/ReplyHookRecvA.lean b/proofs/Fl4ProofsA64/P3/ReplyHookRecvA.lean new file mode 100644 index 000000000..7e021400e --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ReplyHookRecvA.lean @@ -0,0 +1,496 @@ +/- +P3 (AArch64, delete hooks W3 Reply): **the record across the final Reply finaliser, receive arm** (`cancel_ipc` of a +thread blocked on receive with this reply object). + +The receiver is in its endpoint's spec queue by the record's `blkQ`. + +New module. +-/ +import Fl4ProofsA64.P3.Stage3BA +import Fl4ProofsA64.P3.Stage3QKeepPresA +import Fl4ProofsA64.P3.ReplyCancelFootA +import Fl4ProofsA64.P3.Stage3CancelFootA +import Fl4ProofsA64.P3.ReplyCallerA +import Fl4ProofsA64.P3.DelEpHookA +import Fl4ProofsA64.P3.CancelSchedA +import Fl4ProofsA64.P3.DelBlkKeepA +import Fl4ProofsA64.P3.Stage3CancelRecvPInvA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 Fl4ProofsA64.P3.Endp +open Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Notif + +local notation "wa" => core.num.U64.wrapping_add + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} [J7PpA st J7] {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- **`BlkOK` after `cancel_ipc` of a thread blocked on a receive or a reply** (the receive arm of A64 P1's `fin_reply.blk`, which needs no +`RunVcpuInv`). -/ +theorem JStage3BA.cancelRecv_blkA {ar : machine.Arch A} + (J : JStage3BA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {i : U64} (hiT : i ∈ H.tcbs) : + ipc.cancel_ipc st ar s a i ⦃ r => + BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac r.1 ⦄ := by + obtain ⟨⟨Q, R, h0⟩, hB, -, hS, hSg⟩ := J.blkA + have hX := J.blkA.2.2.1 + refine WP.spec_mono (WP.spec_and (ipc.cancel_ipc.all s a i Q R h0 hiT hB hX hSg.sub) + (Fl4ProofsA64.P1.K7.ipc.cancel_ipc.kw7 s a i h0 hiT hX.1 hSg.sub (fun hs => by + obtain ⟨h1, h2⟩ := hX.2.1.reply i hiT hs + exact ⟨h1, h2, hX.2.1.next _ h1, hX.2.1.prev _ h1⟩) (fun hs h0' => (hX.2.1.recv i hiT hs h0').1))) ?_ + rintro ⟨s1, a1⟩ ⟨⟨h1, hB1, hX1, -, -, -, k1, -⟩, f1⟩ + exact ⟨⟨Q, R, h1⟩, hB1, hX1, hS.keepT hX k1, + ⟨hSg.sub, fun b hb => by rw [k1.ksb]; exact hSg.ks b hb, fun o h1' h2' => by rw [k1.ksb]; exact hSg.ksΩ o h1' h2'⟩⟩ + +/-- `JStage3BA.cancelRecv_blkA` at `JStage3TA`. -/ +theorem JStage3TA.cancelRecv_blkA {ar : machine.Arch A} + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {i : U64} (hiT : i ∈ H.tcbs) : + ipc.cancel_ipc st ar s a i ⦃ r => + BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac r.1 ⦄ := + J.toBA.cancelRecv_blkA hiT + +/-- A TCB word at an offset the receive arm does not write. -/ +theorem JStage3BA.tcbWord_offRecvA + (J : JStage3BA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {e : U64} (he : e ∈ E) {Q : Set U64} (hQ : Q ⊆ Z.T) {tc t K : U64} (htc : tc ∈ Z.T) (ht : t ∈ Z.T) + (hK : K.val < 4096) + (h7 : K.val ≠ 3328 ∧ K.val ≠ 3496 ∧ K.val ≠ 3504 ∧ K.val ≠ 3464 ∧ K.val ≠ 3480 ∧ K.val ≠ 3488 ∧ + K.val ≠ 3592) (hf : K.val ≠ 3376) : + ¬ (wa t K = tFaultA tc ∨ EpSigWA Rp Q e (wa t K) ∨ MWrA (st := st) Z m σ s (wa t K)) := by + rintro (h | h) + swap + · exact J.tcbWord_offEpA he hQ ht hK h7 h + have hfit := (J.sr.core.tcbs t ht).fits + have hv : (wa t K).val = t.val + K.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have h' := congrArg (fun w : U64 => w.val) h + simp only [hv, val_fault (J.iplaced.e.tcb tc htc)] at h' + by_cases ett : t = tc + · subst ett; omega + · have := tcb_disj J.iplaced.e ht htc (Ne.symm ett) + omega + +/-- `JStage3BA.tcbWord_offRecvA` at `JStage3TA`. -/ +theorem JStage3TA.tcbWord_offRecvA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {e : U64} (he : e ∈ E) {Q : Set U64} (hQ : Q ⊆ Z.T) {tc t K : U64} (htc : tc ∈ Z.T) (ht : t ∈ Z.T) + (hK : K.val < 4096) + (h7 : K.val ≠ 3328 ∧ K.val ≠ 3496 ∧ K.val ≠ 3504 ∧ K.val ≠ 3464 ∧ K.val ≠ 3480 ∧ K.val ≠ 3488 ∧ + K.val ≠ 3592) (hf : K.val ≠ 3376) : + ¬ (wa t K = tFaultA tc ∨ EpSigWA Rp Q e (wa t K) ∨ MWrA (st := st) Z m σ s (wa t K)) := + J.toBA.tcbWord_offRecvA he hQ htc ht hK h7 hf + +/-- The receive arm's write set is a scheduler / IPC-object write set. -/ +theorem JStage3BA.recvW_stepNA + (J : JStage3BA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {e : U64} (he : e ∈ E) {Q : Set U64} (hQ : Q ⊆ Z.T) {t b : U64} (ht : t ∈ Z.T) + (h : b = tFaultA t ∨ EpSigWA Rp Q e b ∨ MWrA (st := st) Z m σ s b) : + SchedStepNWA (st := st) N E Rp Z m σ s b := by + rcases h with h | h + swap + · exact J.epW_stepNA he hQ h + have hfit := (J.sr.core.tcbs t ht).fits + have hv : ∀ K : U64, K.val < 4096 → (wa t K).val = t.val + K.val := fun K hK => by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have eb : b = wa t 3376#u64 := h + have e1 := hv 3376#u64 (by simp) + have o3376 : (3376#u64 : U64).val = 3376 := by simp + refine Or.inl (Or.inr ⟨t, ht, ?_⟩) + rw [eb] + refine ⟨by omega, by omega, fun h => ?_, fun h => ?_⟩ + · have := congrArg (fun w : U64 => w.val) h + simp only [e1, hv 3664#u64 (by simp)] at this + simp at this + · unfold runVcpuA at h + have := congrArg (fun w : U64 => w.val) h + simp only [e1, hv 3672#u64 (by simp)] at this + simp at this + +/-- `JStage3BA.recvW_stepNA` at `JStage3TA`. -/ +theorem JStage3TA.recvW_stepNA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {e : U64} (he : e ∈ E) {Q : Set U64} (hQ : Q ⊆ Z.T) {t b : U64} (ht : t ∈ Z.T) + (h : b = tFaultA t ∨ EpSigWA Rp Q e b ∨ MWrA (st := st) Z m σ s b) : + SchedStepNWA (st := st) N E Rp Z m σ s b := + J.toBA.recvW_stepNA he hQ ht h + +set_option maxHeartbeats 2000000 in +/-- **The record across the final Reply finaliser, receive arm** with the write set, +the TCB frames and the spec facts a following wake needs. -/ +theorem JStage3BA.replyRecv_recFA [J7PpKA st J7] {ar : machine.Arch A} (hA : P0.ArchTotal ar) + (J : JStage3BA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + (h7 : ∀ (Q : U64 → U64 → List U64) (R : U64 → List U64) (s : S) (σ : Fl4SpecA64.State Unit), + J7 Z H OS s σ m → J7 { Z with Q := Q, R := R } H OS s σ m) + {p : U64} (hH : H p = some .reply) (h0 : L.mem s (Endp.rTcbA p) ≠ 0#u64) + (hrecv : L.mem s (Fl4ProofsA64.P1.stA (L.mem s (Endp.rTcbA p))) = sched.TS_BLOCKED_ON_RECV) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) : + ipc.cancel_ipc st ar s a (L.mem s (Endp.rTcbA p)) ⦃ r => + r.2 = a ∧ ∃ σ' Q R, + cancelIpc (Z := Unit) (specPtr (L.mem s (Endp.rTcbA p))) σ = ({((), σ')}, False) ∧ + JStage3BA st AR { Z with Q := Q, R := R } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + J7 r.1 a σ' m ∧ + OSKeep st OS s r.1 ∧ L.ksb r.1 = L.ksb s ∧ L.cpu r.1 = L.cpu s ∧ SchedFrameA σ σ' ∧ + (∀ t ∈ Z.T, L.mem r.1 (wa t 3664#u64) = L.mem s (wa t 3664#u64)) ∧ + L.mem r.1 (Endp.rTcbA p) = 0#u64 ∧ + IpcSchedRelA st Z m κ r.1 σ' ∧ + WritesIn st s r.1 (SchedStepNWA (st := st) N E Rp Z m σ s) ∧ + (∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → + (K.val ≠ 3328 ∧ K.val ≠ 3496 ∧ K.val ≠ 3504 ∧ K.val ≠ 3464 ∧ K.val ≠ 3480 ∧ K.val ≠ 3488 ∧ + K.val ≠ 3592) → K.val ≠ 3376 → L.mem r.1 (wa u K) = L.mem s (wa u K)) ∧ + QKeepA σ σ' ∧ + (∃ x', σ'.kheap (specPtr (L.mem s (Endp.rTcbA p))) = some (.tcb x') ∧ x'.tcbState = .inactive) ∧ + RelIfBound σ' (specPtr (L.mem s (Endp.rTcbA p))) ∧ + (∀ q, q ≠ specPtr (L.mem s (Endp.rTcbA p)) → (∀ z, σ.kheap q ≠ some (.endpoint z)) → + (∀ y, σ.kheap q ≠ some (.reply y)) → σ'.kheap q = σ.kheap q) ∧ + σ'.curThread = σ.curThread ∧ σ'.idleThread = σ.idleThread ∧ σ'.curSc = σ.curSc ∧ + WritesIn st s r.1 (CancelFootA st Rp Z.T s (L.mem s (Endp.rTcbA p))) ⦄ := by + have rvi : objects.finalise_cap_object st ar s a (.Reply p false) true ⦃ r => RunVcpuInv st H.tcbs r.2.1 ⦄ := + Fl4ProofsA64.P1.fin_reply.rvi hA s a p false true J.blkA (show p ∈ H.replies from hH) (J.tcbs ▸ J.runVcpu) + obtain ⟨t, et⟩ : ∃ t, L.mem s (Endp.rTcbA p) = t := ⟨_, rfl⟩ + have eprog : objects.finalise_cap_object st ar s a (.Reply p false) true = (do + let (s1, a1) ← ipc.cancel_ipc st ar s a t + ok ((capability.Cap.Null, capability.Cap.Null), s1, a1)) := by + have e1 : obj.Reply.tcb st p s = ok t := (eq_of_spec_fd (obj.Reply.tcb.spec (L := L) s p)).trans (congrArg ok et) + have en : obj.Tcb.is_null t = ok false := + (eq_of_spec_fd (obj.Tcb.is_null.spec _)).trans (by rw [← et]; simp [h0]) + have es : obj.Tcb.state st t s = ok sched.TS_BLOCKED_ON_RECV := + (eq_of_spec_fd (obj.Tcb.state.spec (L := L) s _)).trans (congrArg ok (et ▸ hrecv)) + have ne : sched.TS_BLOCKED_ON_RECV ≠ sched.TS_BLOCKED_ON_REPLY := by + unfold sched.TS_BLOCKED_ON_RECV sched.TS_BLOCKED_ON_REPLY; decide + simp only [objects.finalise_cap_object, ↓reduceIte, e1, en, es, bind_ok, if_neg ne, Bool.false_eq_true] + rw [eprog] at rvi + rw [et] at h0 hrecv ⊢ + have hp : p ∈ Rp := J.ipcHeapCov.reply p hH + have ht : t ∈ Z.T := by rw [← et]; exact (J.replyCallerA hH (by rw [et]; exact h0)).1 + have htH : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + have hc := J.curCore_lt hCs + have hP := J.iplaced + have hta := hP.e.tcb t ht + obtain ⟨x, hk, hx⟩ := J.ep.tcb t ht + have h3 : tsCode x.tcbState = 3 := by + rw [← hx.st, hrecv]; unfold sched.TS_BLOCKED_ON_RECV; rfl + obtain ⟨e', ro, d, hs⟩ := tsCode_three h3 + have R := J.blkA.2.2.1.2.1 + have back := R.back p hH (by show L.mem s (Endp.rTcbA p) ≠ 0#u64; rw [et]; exact h0) + have hip : L.mem s (tReplyA t) = p := by rw [← et]; exact back.2.2.1 + have p0 : p ≠ 0#u64 := back.1 + have ero : ro = some (specPtr p) := by + rw [← (hx.recv e' ro d hs).1, hip, optRef_ne p0] + subst ero + have hb : epBlocked x.tcbState = some e' := by rw [hs]; rfl + obtain ⟨heE, z, hkz, hq⟩ := J.blkQ.ep t ht x hk e' hb + obtain ⟨e, ee⟩ : ∃ e, L.mem s (tBlockA t) = e := ⟨_, rfl⟩ + have es : specPtr e = e' := by rw [← ee]; exact hx.blk e' hb + subst es + rw [ee] at heE + obtain ⟨xr, hkr, hrb⟩ := J.ep.reply p hp + have hback : xr.replyTcb = some (specPtr t) := by rw [hrb, et, optRef_ne h0] + have hrep : RepOKA σ (specPtr t) := J.repOKA ht + let Q : Set U64 := {q | q ∈ Z.T ∧ L.mem s (tBlockA q) = e} + have hQ : Q ⊆ Z.T := fun _ h => h.1 + have htQ : t ∈ Q := ⟨ht, ee⟩ + have ops := J.schedOpsA (ar := ar) hc hmb hmax htu + have hSched : CancelScheduleA st N Z.T (IpcSchedRelA st Z m κ) := cancelScheduleA_of_placement J.ipcPlace + have rel := cancel_ipc_rel (ar := ar) ops.1 hSched ops.2.2.1 hP s a σ J.ntfn J.ep J.ipcSchedA ht hk + (fun r h => by rw [hs] at h; cases h) + (fun e1 h => by + rw [hb] at h + cases h + exact ⟨by rw [ee]; exact heE, z, hkz, hq⟩) + (fun n' h => by rw [hs] at h; cases h) hrep + have fp := cancel_ipc_recvF (ar := ar) (Wr := fun _ s b => SKsA (L.ksb s) b) (J.schedOpsEA (ar := ar) hc hmb hmax htu) + (schedFootE0A_ks (st := st) (Z := Z) (m := m) (κ := κ) (E := E) (R := Rp) (Bf := Bf)) + hP.e s a σ J.ep J.ipcSchedA ht hk heE hkz hq ee hp hkr hs (Q := Q) htQ (fun w hw h => ⟨hw, h⟩) + obtain ⟨σ', hsp, K, hrn, hinv, hns, hoth, hct, hit, hcs⟩ := cancelRecv_specKA hk hkz hq hkr hback hs + have of := cancelIpc_of_rel (ar := ar) ops.1 hSched ops.2.2.1 hP s a σ J.ntfn J.ep J.ipcSchedA ht hk + (by rw [hs]; rfl) hrep (by rw [ee]; exact heE) (fun e1 h => by + rw [hb] at h + cases h + exact ⟨z, hkz, hq⟩) + have blk := J.cancelRecv_blkA (ar := ar) htH + have rv : ipc.cancel_ipc st ar s a t ⦃ r => RunVcpuInv st H.tcbs r.1 ⦄ := by + obtain ⟨⟨s1, a1⟩, hr, -⟩ := spec_existsA rel + rw [hr, bind_ok] at rvi + rw [hr] + exact (WP.spec_ok _).2 ((WP.spec_ok ((capability.Cap.Null, capability.Cap.Null), s1, a1)).1 rvi) + refine WP.spec_mono (WP.spec_and (WP.spec_and (WP.spec_and blk rv) of) (WP.spec_and rel fp)) ?_ + rintro ⟨s', a'⟩ ⟨⟨⟨blk', rv'⟩, σ3, x3, hsp3, -, -, -, hk3, hin3, hrb3⟩, ⟨ha, σ2, hsp2, ntfn, E', SR'⟩, wF⟩ + dsimp only at blk' rv' ha E' SR' ntfn wF hk3 ⊢ + have e2 : σ2 = σ' := by + have := hsp2.symm.trans hsp + simpa using congrArg Prod.fst this + subst e2 + have e3 : σ3 = σ2 := by + have := hsp3.symm.trans hsp + simpa using congrArg Prod.fst this + rw [e3] at hk3 hrb3 + have F : SchedFrameA σ σ2 := + cancelIpc_presA (specPtr t) σ ((), σ2) (by rw [hsp]; exact Set.mem_singleton _) + -- the spec state after the fault clear + have hk1 : (updK σ (specPtr t) (.tcb { x with tcbFault := none })).kheap (specPtr t) = + some (.tcb { x with tcbFault := none }) := by rw [updK_kheap, Function.update_self] + have k1 : ∀ q o, (∀ y, o ≠ .tcb y) → σ.kheap q = some o → + (updK σ (specPtr t) (.tcb { x with tcbFault := none })).kheap q = some o := fun q o ho h => by + have ne : q ≠ specPtr t := fun e0 => by rw [e0, hk] at h; cases h; exact ho x rfl + rw [updK_kheap, Function.update_of_ne ne]; exact h + have k1b : ∀ q o, (∀ y, o ≠ .tcb y) → + (updK σ (specPtr t) (.tcb { x with tcbFault := none })).kheap q = some o → σ.kheap q = some o := + fun q o ho h => by + have ne : q ≠ specPtr t := fun e0 => by rw [e0, hk1] at h; cases h; exact ho _ rfl + rw [updK_kheap, Function.update_of_ne ne] at h; exact h + have k1t : ∀ q y, σ.kheap q = some (.tcb y) → ∃ y1, + (updK σ (specPtr t) (.tcb { x with tcbFault := none })).kheap q = some (.tcb y1) ∧ + y1.tcbMcpriority = y.tcbMcpriority ∧ y1.tcbYieldTo = y.tcbYieldTo ∧ y1.tcbIpcframe = y.tcbIpcframe ∧ + y1.tcbIpcBuffer = y.tcbIpcBuffer ∧ (y.tcbFault = none → y1.tcbFault = none) ∧ + y1.tcbFlags = y.tcbFlags := fun q y h => by + by_cases e0 : q = specPtr t + · have ey : y = x := by + rw [e0, hk] at h + exact (KernelObject.tcb.inj (Option.some.inj h)).symm + rw [ey] + exact ⟨{ x with tcbFault := none }, by rw [e0]; exact hk1, rfl, rfl, rfl, rfl, fun _ => rfl, rfl⟩ + · exact ⟨y, by rw [updK_kheap, Function.update_of_ne e0]; exact h, rfl, rfl, rfl, rfl, id, rfl⟩ + have TK : ∀ q y, σ.kheap q = some (.tcb y) → ∃ y2, σ2.kheap q = some (.tcb y2) ∧ + y2.tcbIpcframe = y.tcbIpcframe ∧ y2.tcbIpcBuffer = y.tcbIpcBuffer ∧ + (y.tcbFault = none → y2.tcbFault = none) := fun q y h => by + obtain ⟨y1, h1, -, -, f, b, g, -⟩ := k1t q y h + obtain ⟨y2, h2, k⟩ := K.tcb h1 + exact ⟨y2, h2, k.frame.trans f, k.buf.trans b, fun e0 => k.fault.trans (g e0)⟩ + have wC : WritesIn st s s' (CancelFootA st Rp Z.T s t) := by + refine wF.mono fun b h => ?_ + rcases h with h | h | h + · exact Or.inl h + · refine Or.inr (Or.inl ?_) + rw [ee] + exact h + · exact Or.inr (Or.inr h) + have wE : WritesIn st s s' (fun b => b = tFaultA t ∨ EpSigWA Rp Q e b ∨ MWrA (st := st) Z m σ s b) := + wF.mono fun b h => by + rcases h with h | h | h + · exact Or.inl h + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr (Or.inr (Or.inl h))) + have w : WritesIn st s s' (SchedStepNWA (st := st) N E Rp Z m σ s) := + wE.mono fun _ h => J.recvW_stepNA heE hQ ht h + have tw : ∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → + (K.val ≠ 3328 ∧ K.val ≠ 3496 ∧ K.val ≠ 3504 ∧ K.val ≠ 3464 ∧ K.val ≠ 3480 ∧ K.val ≠ 3488 ∧ + K.val ≠ 3592) → K.val ≠ 3376 → L.mem s' (wa u K) = L.mem s (wa u K) := fun u hu K h1 h2 h3 h4 => + wE.mem_eq (field_aligned (J.sr.core.tcbs u hu) h1 h2) (J.tcbWord_offRecvA heE hQ ht hu h1 h3 h4) + obtain ⟨Q', R', H3⟩ := SR' + have sq : SchedQueuesInv st H.tcbs Z.Cs H.omega H s' Q' R' := by + obtain ⟨Q0, R0, h0'⟩ := blk'.1 + exact SchedQueuesInv.reQA h0' (by rw [J.tcbs]; exact H3.core) + have kb : L.ksb s' = L.ksb s := w.1 + have poolOff : PoolOff st H Z.Cs OS s' := fun i => by + have h0' := J.poolOff i + rw [vspace.asid_table.eq] at h0' ⊢ + have v := AtOff.v i + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.ent.ksw + have ev : (wa (L.ksb s) (AtOff i)).val = (L.ksb s).val + (AtOff i).val := by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have e0 : L.mem s' (wa (L.ksb s') (AtOff i)) = L.mem s (wa (L.ksb s) (AtOff i)) := by + rw [kb] + refine w.mem_eq ?_ (J.asid_offStepNA (by omega) (by omega)) + have h32 := J.j.1.ksbAl + show (wa (L.ksb s) (AtOff i)).val % 8 = 0 + omega + rw [e0] + refine (WP.spec_ok _).2 fun hne asid => ?_ + have q := (WP.spec_ok _).1 h0' hne asid + exact ⟨q.al, q.om, q.sg, q.os, RegionOffNodes.ksb kb q.ks⟩ + have reply : ReplyStackRelA (L.mem s') Rp Z.C σ2 (scNameA m) stack := by + have R1 : ReplyStackRelA (L.mem s) Rp Z.C (updK σ (specPtr t) (.tcb { x with tcbFault := none })) + (scNameA m) stack := + J.reply.frame (fun _ _ => ⟨rfl, rfl⟩) + (fun _ _ y k h => ⟨y, k, k1 _ _ (fun _ e0 => by cases e0) h, rfl⟩) + (fun _ _ y h => ⟨y, k1b _ _ (fun _ e0 => by cases e0) h, rfl⟩) + refine R1.epFr K fun r hr => ?_ + obtain ⟨r32, -, rfit⟩ := hP.e.rep r hr + have one : ∀ o : U64, 8 ≤ o.val → o.val < 32 → o.val % 8 = 0 → L.mem s' (wa r o) = L.mem s (wa r o) := by + intro o o0 o1 o2 + have v : (wa r o).val = r.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + refine wE.mem_eq ?_ ?_ + · show (wa r o).val % 8 = 0 + omega + rintro (h | h) + · have h' := congrArg (fun w : U64 => w.val) h + simp only [v, val_fault hta] at h' + have := hP.e.rt r hr t ht + omega + · exact J.reply_offEpA heE hQ hr (by omega) (by omega) h + have n16 : (layout.reply.NEXT).val = 16 := by unfold layout.reply.NEXT; rfl + have p8 : (layout.reply.PREV).val = 8 := by unfold layout.reply.PREV; rfl + exact ⟨one _ (by omega) (by omega) (by omega), one _ (by omega) (by omega) (by omega)⟩ + have mcp : McpRelA Z.T m (L.mem s') σ2 := by + have M1 : McpRelA Z.T m (L.mem s) (updK σ (specPtr t) (.tcb { x with tcbFault := none })) := fun u hu => by + obtain ⟨y, hy, e0, hf⟩ := J.mcp u hu + obtain ⟨y1, h1, m1, -, -, -, -, fl⟩ := k1t _ _ hy + exact ⟨y1, h1, by rw [m1]; exact e0, fun g => (hf g).keep rfl fl⟩ + exact M1.epFr K fun u hu => ⟨tw u hu 3440#u64 (by simp) (by simp) (by simp) (by simp), + tw u hu 3456#u64 (by simp) (by simp) (by simp) (by simp)⟩ + have yr : YieldRelA Z.T Z.C m (L.mem s') σ2 := by + have Y1 : YieldRelA Z.T Z.C m (L.mem s) (updK σ (specPtr t) (.tcb { x with tcbFault := none })) := by + refine ⟨fun u hu => ?_, fun u hu h => ?_, fun c hcc y k hy q hq' => ?_⟩ + · obtain ⟨y, hy, e0⟩ := J.yieldRel.tcb u hu + obtain ⟨y1, h1, -, m1, -⟩ := k1t _ _ hy + exact ⟨y1, h1, by rw [m1]; exact e0⟩ + · obtain ⟨hcc, y, k, hy, e0⟩ := J.yieldRel.toSc u hu h + exact ⟨hcc, y, k, k1 _ _ (fun _ e1 => by cases e1) hy, e0⟩ + · exact J.yieldRel.frm c hcc y k (k1b _ _ (fun _ e1 => by cases e1) hy) q hq' + exact Y1.epFr K fun u hu => tw u hu 3472#u64 (by simp) (by simp) (by simp) (by simp) + have buf : BufCover st Z.T Bf s' σ2 := + J.bufCover_wA wE (fun u hu K hK => J.tcbWord_offRecvA heE hQ ht hu (by omega) (by omega) (by omega)) + (fun q y h => by + obtain ⟨y2, hy, b, c, -⟩ := TK q y h + exact ⟨y2, hy, b, c⟩) + have bufAl : UtBufAlA Z.T σ2 := fun u hu y hy => by + obtain ⟨x0, hx0, -⟩ := J.ntfn.tcb u hu + obtain ⟨y', hy', -, e0, -⟩ := TK _ _ hx0 + rw [hy] at hy'; cases hy' + rw [e0]; exact J.bufAl u hu x0 hx0 + have idle : IdleNoFaultA m σ2 := fun c hc' => by + obtain ⟨x0, hx0, hf⟩ := J.idleNoFault c hc' + obtain ⟨y, hy, -, -, g⟩ := TK _ _ hx0 + exact ⟨y, hy, g hf⟩ + have scCov : ScCovA Z.C m σ2 := fun q y k h => by + obtain ⟨y', hy⟩ := F.2.1 q y k h + exact J.scCov q y' k hy + have inv : SPairA σ2 m n := SPairA.cancelRecv ⟨J.sinv, J.sidle⟩ hk hs hkz hq hrep + (by rw [hsp]; exact Set.mem_singleton _) + have hvr : ∀ sc ∈ Z.C, ∀ q ∈ stack sc, RBlk st s' q := by + refine J.vr_specA blk' (fun _ _ h => h) (fun sc _ q _ c _ _ x2 o hx2 hs2 => ?_) E' + (fun c hc => tw c hc 3360#u64 (by simp) (by simp) (by simp) (by simp)) + have ne : specPtr c ≠ specPtr t := fun e0 => by + rw [e0, hk] at hx2 + rw [← KernelObject.tcb.inj (Option.some.inj hx2), hs] at hs2 + cases hs2 + have h1 : (updK σ (specPtr t) (.tcb { x with tcbFault := none })).kheap (specPtr c) = some (.tcb x2) := by + rw [updK_kheap, Function.update_of_ne ne]; exact hx2 + obtain ⟨y, hy, k⟩ := K.tcb h1 + refine ⟨y, hy, ?_⟩ + rcases k.st with e | ⟨⟨-, nr⟩, -⟩ + · exact e + · exact absurd hs2 (nr o) + have j := J.j57_stepNWA w σ2 + have bq : BlkQueuedA Z.T E N (L.mem s') σ2 := by + obtain ⟨M, dq⟩ := cancelIpc_recv_blkA hk hkz hq hs (σ' := σ2) (by rw [hsp]; exact Set.mem_singleton _) + refine J.blkQ.finA M (fun u hu => ?_) (fun u hu => ?_) (fun y hy r hr => ?_) (fun y q hn => ?_) + · obtain ⟨x0, hx0, -⟩ := J.ntfn.tcb u hu + exact ⟨x0, hx0⟩ + · rw [Notif.tBlockA_eq]; exact tw u hu 3336#u64 (by simp) (by simp) (by simp) (by simp) + · rw [hkz] at hy; cases hy; exact dq r hr + · rw [hkz] at hn; cases hn + have J' := JStage3BA.of_schedWrN (blkQ := bq) J (Q := Q') (Rl := R') (curOf := Z.curOf) (curScOf := Z.curScOf) (s' := s') + (a' := a) (σ' := σ2) w F.1 F.2.1 + (Sched.qk_of (m := cancelIpc (Z := Unit) (specPtr t)) (by rw [hsp]; exact Set.mem_singleton _)) F.2.2 J.j.1.ar (fun _ _ => rfl) bufAl (J.ent_stepNWA w blk') sq poolOff H3 + scCov E' ntfn reply buf idle mcp yr inv.sinv inv.sidle (J.tcbs ▸ rv') hvr j.1 (h7 Q' R' _ _ j.2) + have hz : L.mem s' (Endp.rTcbA p) = 0#u64 := by + obtain ⟨xr', hkr', hrb'⟩ := E'.reply p hp + obtain ⟨y2, hy2⟩ : ∃ y : Fl4SpecA64.Reply, y.replyTcb = none ∧ σ2.kheap (specPtr p) = some (.reply y) := + ⟨_, rfl, hrn⟩ + rw [hy2.2] at hkr' + cases hkr' + rw [hy2.1] at hrb' + by_contra hne + rw [optRef_ne hne] at hrb' + cases hrb' + exact ⟨ha, σ2, Q', R', hsp, ha ▸ J', J.sigW_osKeepA w, kb, w.2.1, F, + fun u hu => tw u hu 3664#u64 (by simp) (by simp) (by simp) (by simp), hz, ⟨Q', R', H3⟩, w, tw, + Sched.qk_of (m := cancelIpc (Z := Unit) (specPtr t)) (by rw [hsp]; exact Set.mem_singleton _), + ⟨x3, hk3, hin3⟩, hrb3, + fun q q1 q2 q3 => hoth q q1 (fun c => q2 z (c ▸ hkz)) (fun c => q3 xr (c ▸ hkr)), hct, hit, hcs, wC⟩ + +/-- `cancel_ipc` of a receiver starts no switch and keeps `SActA`. -/ +theorem JStage3TA.cancelRecv_noSwA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {t : U64} (ht : t ∈ Z.T) {x : Fl4SpecA64.Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + {e : ObjRef} {ro : Option ObjRef} {d : ReceiverPayload} (hs : x.tcbState = .blockedOnReceive e ro d) + {σ' : Fl4SpecA64.State Unit} (h : ((), σ') ∈ (cancelIpc (Z := Unit) (specPtr t) σ).1) : + (∀ u, σ'.schedulerAction ≠ .switchThread u) ∧ SActA σ' := by + have hb : epBlocked x.tcbState = some e := by rw [hs]; rfl + obtain ⟨-, z, hkz, hq⟩ := J.blkQ.ep t ht x hk e hb + have I := cancelIpc_invA (m := m) (n := n) ⟨J.sinv, J.sidle, J.sact⟩ J.noSwitch hk + (fun e1 c => by rw [hb] at c; cases c; exact ⟨z, hkz, hq⟩) (fun n' c => by rw [hs] at c; cases c) + (J.repOKA ht) (r := ((), σ')) h + exact ⟨I.2, I.1.sact⟩ + +set_option maxHeartbeats 2000000 in +/-- `JStage3BA.replyRecv_recFA` at `JStage3TA` (no switch is started). **Receive arm** with the write set, +the TCB frames and the spec facts a following wake needs. -/ +theorem JStage3TA.replyRecv_recFA [J7PpKA st J7] {ar : machine.Arch A} (hA : P0.ArchTotal ar) + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + (h7 : ∀ (Q : U64 → U64 → List U64) (R : U64 → List U64) (s : S) (σ : Fl4SpecA64.State Unit), + J7 Z H OS s σ m → J7 { Z with Q := Q, R := R } H OS s σ m) + {p : U64} (hH : H p = some .reply) (h0 : L.mem s (Endp.rTcbA p) ≠ 0#u64) + (hrecv : L.mem s (Fl4ProofsA64.P1.stA (L.mem s (Endp.rTcbA p))) = sched.TS_BLOCKED_ON_RECV) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) : + ipc.cancel_ipc st ar s a (L.mem s (Endp.rTcbA p)) ⦃ r => + r.2 = a ∧ ∃ σ' Q R, + cancelIpc (Z := Unit) (specPtr (L.mem s (Endp.rTcbA p))) σ = ({((), σ')}, False) ∧ + JStage3TA st AR { Z with Q := Q, R := R } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + J7 r.1 a σ' m ∧ + OSKeep st OS s r.1 ∧ L.ksb r.1 = L.ksb s ∧ L.cpu r.1 = L.cpu s ∧ SchedFrameA σ σ' ∧ + (∀ t ∈ Z.T, L.mem r.1 (wa t 3664#u64) = L.mem s (wa t 3664#u64)) ∧ + L.mem r.1 (Endp.rTcbA p) = 0#u64 ∧ + IpcSchedRelA st Z m κ r.1 σ' ∧ + WritesIn st s r.1 (SchedStepNWA (st := st) N E Rp Z m σ s) ∧ + (∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → + (K.val ≠ 3328 ∧ K.val ≠ 3496 ∧ K.val ≠ 3504 ∧ K.val ≠ 3464 ∧ K.val ≠ 3480 ∧ K.val ≠ 3488 ∧ + K.val ≠ 3592) → K.val ≠ 3376 → L.mem r.1 (wa u K) = L.mem s (wa u K)) ∧ + QKeepA σ σ' ∧ + (∃ x', σ'.kheap (specPtr (L.mem s (Endp.rTcbA p))) = some (.tcb x') ∧ x'.tcbState = .inactive) ∧ + RelIfBound σ' (specPtr (L.mem s (Endp.rTcbA p))) ∧ + (∀ q, q ≠ specPtr (L.mem s (Endp.rTcbA p)) → (∀ z, σ.kheap q ≠ some (.endpoint z)) → + (∀ y, σ.kheap q ≠ some (.reply y)) → σ'.kheap q = σ.kheap q) ∧ + σ'.curThread = σ.curThread ∧ σ'.idleThread = σ.idleThread ∧ σ'.curSc = σ.curSc ∧ + WritesIn st s r.1 (CancelFootA st Rp Z.T s (L.mem s (Endp.rTcbA p))) ⦄ := by + have ht : L.mem s (Endp.rTcbA p) ∈ Z.T := (J.replyCallerA hH h0).1 + refine WP.spec_mono (J.toBA.replyRecv_recFA (ar := ar) hA h7 hH h0 hrecv hCs hmb hmax htu) ?_ + rintro r ⟨ha, σ', Q, R, h1, h2, rest⟩ + obtain ⟨x, hk, hx⟩ := J.ep.tcb _ ht + have h3 : tsCode x.tcbState = 3 := by + rw [← hx.st, hrecv]; unfold sched.TS_BLOCKED_ON_RECV; rfl + obtain ⟨e', ro, d, hs⟩ := tsCode_three h3 + have N := J.cancelRecv_noSwA ht hk hs (σ' := σ') (by rw [h1]; exact Set.mem_singleton _) + exact ⟨ha, σ', Q, R, h1, h2.toTA N.1 N.2, rest⟩ + +set_option maxHeartbeats 1000000 in +/-- **The record across the final Reply finaliser, receive arm** (module docstring). -/ +theorem JStage3TA.replyRecv_recA {ar : machine.Arch A} {ipa : U64} (hA : P0.ArchTotal ar) + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7LA st (J7R2A st ar ipa)) s a σ m) + {p : U64} (hH : H p = some .reply) (h0 : L.mem s (Endp.rTcbA p) ≠ 0#u64) + (hrecv : L.mem s (Fl4ProofsA64.P1.stA (L.mem s (Endp.rTcbA p))) = sched.TS_BLOCKED_ON_RECV) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) : + ipc.cancel_ipc st ar s a (L.mem s (Endp.rTcbA p)) ⦃ r => + r.2 = a ∧ ∃ σ' Q R, + cancelIpc (Z := Unit) (specPtr (L.mem s (Endp.rTcbA p))) σ = ({((), σ')}, False) ∧ + JStage3TA st AR { Z with Q := Q, R := R } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7LA st (J7R2A st ar ipa)) r.1 a σ' m ∧ + OSKeep st OS s r.1 ∧ L.ksb r.1 = L.ksb s ∧ L.cpu r.1 = L.cpu s ∧ SchedFrameA σ σ' ∧ + (∀ t ∈ Z.T, L.mem r.1 (wa t 3664#u64) = L.mem s (wa t 3664#u64)) ∧ + L.mem r.1 (Endp.rTcbA p) = 0#u64 ∧ + Std6KeepA st Z H OS Bf s σ r.1 σ' ⦄ := by + refine WP.spec_mono (J.replyRecv_recFA (ar := ar) hA (fun _ _ _ _ h => h) hH h0 hrecv hCs hmb hmax htu) ?_ + rintro r ⟨ha, σ', Q, R, h1, h2, h3, h4, h5, h6, h7, h8, -, w, -⟩ + exact ⟨ha, σ', Q, R, h1, h2, h3, h4, h5, h6, h7, h8, fun {_} sub h => Std6GA.schedStepA J h sub w h3 h6⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/ReplyHookRecvArmA.lean b/proofs/Fl4ProofsA64/P3/ReplyHookRecvArmA.lean new file mode 100644 index 000000000..93b9f69e0 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ReplyHookRecvArmA.lean @@ -0,0 +1,158 @@ +/- +P3 (AArch64, delete hooks W3 Reply): **the Reply finaliser hook at a state, receive arm** (`finHookReplyA_recvA`), +and the weak `valid_replies` it and the null-thread arm use. + +`StackBlkA` is the record's `vr`; blocked ⇒ queued is the record's `blkQ`. +[P] `hCs`, `hmb`, `hmax`, `htu`; [S] `h1`. New module. +-/ +import Fl4ProofsA64.P3.ReplyHookRecvA +import Fl4ProofsA64.P3.ReplyHookNoopA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 Fl4ProofsA64.P3.Endp +open Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Notif + +local notation "wa" => core.num.U64.wrapping_add + +noncomputable section + +section +variable {m : Fl4SpecA64.McState} +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} {ia ie : Nat} + {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} + {J5 J7 : JSlotA S} [J7PpA st J7] {s : S} {a : A} {σ : State Unit} {m : McState} + +/-- **Weak `valid_replies`** (TEMPORARY [J]): every reply on a reply stack is blocked on reply. -/ +def StackBlkA (st : machine.Store S) [L : LawfulStore st] (C : Set U64) (stack : U64 → List U64) (s : S) : Prop := + ∀ sc ∈ C, ∀ r ∈ stack sc, RBlk st s r + +/-- A reply that is not blocked on reply is on no spec reply stack. -/ +theorem JStage3TA.reply_offStacksA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (hvr : StackBlkA st Z.C stack s) {p : U64} (hnb : ¬ RBlk st s p) : + ∀ q y k, σ.kheap q = some (.schedContext y k) → specPtr p ∉ y.scReplies := by + intro q y k hq hm + obtain ⟨c, hc, rfl⟩ := J.scCov q y k hq + obtain ⟨⟨x, k', hx, hL⟩, -⟩ := J.reply.stackRel c hc + rw [hq] at hx + cases hx + rw [hL, List.mem_map] at hm + obtain ⟨r, hr, e⟩ := hm + exact hnb (specPtr_inj e ▸ hvr c hc r hr) + +/-- **The Reply finaliser hook at a state, receive arm** (module docstring). -/ +theorem finHookReplyA_recvA {ipa : U64} (s0 : S) (hA : P0.ArchTotal ar) + (Rv : S → A → State Unit → MdbGhostA → U64 → Prop) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {G : MdbGhostA} {sl : CslotPtr} {p : U64} {g : Bool} {ac : Fl4SpecA64.Cap} {f : Bool} + (D : DStateA st Z.th OS s σ G) + (W : DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st (J7R2A st ar ipa))) s a σ G) + (hsl : AbsSlotA Z.th sl) (hc : capA st s (cslotAddrA sl) = .Reply p g) + (hg : getCap (Z := Unit) sl σ = ({(ac, σ)}, False)) (hac : CapEqA (.Reply p g) ac) + (hf : f = true ↔ isFinalCap' ac σ) (hft : f = true) + (h0 : L.mem s (Endp.rTcbA p) ≠ 0#u64) + (hrecv : L.mem s (Fl4ProofsA64.P1.stA (L.mem s (Endp.rTcbA p))) = sched.TS_BLOCKED_ON_RECV) : + objects.finalise_cap st ar s a (.Reply p g) f false ⦃ r => + r.1.1 = .Null ∧ CleanCapA r.1.2 ∧ CapWfA r.1.2 ∧ + ∃ σ' G', (∃ d : Bool, (d = true ↔ isFinalCap' ac σ) ∧ + ((absCapA r.1.1, absCapA r.1.2), σ') ∈ (fl4FinaliseCap (Z := Unit) ac d σ).1) ∧ + DStateA st Z.th OS r.2.1 σ' G' ∧ capA st r.2.1 (cslotAddrA sl) = .Reply p g ∧ + (∀ y ∈ OS, y ≠ cslotAddrA sl → capA st r.2.1 y = capA st s y ∨ + (capA st r.2.1 y = .Null ∧ ¬ IsZombieA (capA st s y))) ∧ + WFA (DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st (J7R2A st ar ipa)))) + (DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st J7R2tA))) + r.1.1 (cslotAddrA sl) r.2.1 r.2.2 σ' G' ∧ + (RmOKA r.1.1 (cslotAddrA sl) → DelRdXA st ar Z H OS ipa Rv r.2.1 r.2.2 σ' G' (cslotAddrA sl)) ∧ + (∀ q zt k, r.1.1 = .Zombie q zt k → DelRzA st r.2.1 r.2.2 σ' G' (cslotAddrA sl)) ⦄ := by + subst hft + have eac : ac = absCapA (.Reply p g) := hac.symm + obtain ⟨hcore, ⟨Q, R, hw, Ac, Bf, stack, J, X⟩, hPA, hZT, hBH, hCT, hFx⟩ := W + have hvr : StackBlkA st Z.C stack s := J.vr + have hOSx : cslotAddrA sl ∈ OS := D.mem_slot hsl + have hH : H p = some .reply := (J.capTy _ hOSx).2.2.1 p g hc + have hp : p ∈ Rp := J.ipcHeapCov.reply p hH + have unread : ¬ S1aR2Reads (capability.Cap.Reply p g) := fun h => h + have hnb : ¬ RBlk st s p := fun h => by + have := h.blk + change L.mem s (Fl4ProofsA64.P1.stA (L.mem s (Endp.rTcbA p))) = sched.TS_BLOCKED_ON_REPLY at this + rw [hrecv] at this + unfold sched.TS_BLOCKED_ON_RECV sched.TS_BLOCKED_ON_REPLY at this + simp at this + have hdet := J.reply_offStacksA hvr hnb + have ht : L.mem s (Endp.rTcbA p) ∈ Z.T := (J.replyCallerA hH h0).1 + obtain ⟨x, hk, hx⟩ := J.ep.tcb _ ht + obtain ⟨e', ro, d, hs⟩ := tsCode_three (ts := x.tcbState) (by + rw [← hx.st, hrecv]; unfold sched.TS_BLOCKED_ON_RECV; rfl) + obtain ⟨xr, hkr, hrb⟩ := J.ep.reply p hp + have hback : xr.replyTcb = some (specPtr (L.mem s (Endp.rTcbA p))) := by rw [hrb, optRef_ne h0] + rw [fin_cap_eq_obj (by simp)] + have eprog : objects.finalise_cap_object st ar s a (.Reply p g) true = (do + let (s1, a1) ← ipc.cancel_ipc st ar s a (L.mem s (Endp.rTcbA p)) + ok ((capability.Cap.Null, capability.Cap.Null), s1, a1)) := by + have et : obj.Reply.tcb st p s = ok (L.mem s (Endp.rTcbA p)) := + eq_of_spec_fd (obj.Reply.tcb.spec (L := L) s p) + have en : obj.Tcb.is_null (L.mem s (Endp.rTcbA p)) = ok false := + (eq_of_spec_fd (obj.Tcb.is_null.spec _)).trans (by simp [h0]) + have es : obj.Tcb.state st (L.mem s (Endp.rTcbA p)) s = ok sched.TS_BLOCKED_ON_RECV := + (eq_of_spec_fd (obj.Tcb.state.spec (L := L) s _)).trans (congrArg ok hrecv) + have ne : sched.TS_BLOCKED_ON_RECV ≠ sched.TS_BLOCKED_ON_REPLY := by + unfold sched.TS_BLOCKED_ON_RECV sched.TS_BLOCKED_ON_REPLY; decide + simp only [objects.finalise_cap_object, ↓reduceIte, et, en, es, bind_ok, if_neg ne, Bool.false_eq_true] + rw [eprog] + refine WP.spec_bind (J.replyRecv_recA (ar := ar) hA hH h0 hrecv hCs hmb hmax htu) ?_ + rintro ⟨s', a'⟩ ⟨ha, σ', Q', R', hsp, J', k, kb, cpu, F, vc, hz, k6⟩ + dsimp only at ha J' k kb cpu vc hz k6 ⊢ + subst ha + refine (WP.spec_ok ((capability.Cap.Null, capability.Cap.Null), s', a')).2 ?_ + have caps : ∀ y ∈ OS, capA st s' y = capA st s y := fun y hy => k.capA hy + have hc' : capA st s' (cslotAddrA sl) = .Reply p g := (caps _ hOSx).trans hc + have hmem : ((Fl4SpecA64.Cap.nullCap, Fl4SpecA64.Cap.nullCap), σ') ∈ + (fl4FinaliseCap (Z := Unit) ac true σ).1 := by + rw [eac] + simp only [absCapA] + rw [fl4FinaliseCap_plainA (by intro h; exact h)] + refine mem_bindA.2 ⟨((), σ'), ?_, rfl⟩ + refine mem_bindA.2 ⟨(some (specPtr (L.mem s (Endp.rTcbA p))), σ), by rw [getReplyTcb_eq hkr, hback]; rfl, ?_⟩ + dsimp only + refine mem_bindA.2 ⟨(x.tcbState, σ), by rw [getThreadState_eq hk]; rfl, ?_⟩ + dsimp only + rw [hs] + dsimp only + rw [hsp] + exact rfl + have D' : DStateA st Z.th OS s' σ' G := DStateA.keepOSA (Z := { Z with G := G, Q := Q', R := R', hw := hw }) D J' k + -- the reply is still on no stack + have hdet' : ∀ q y n', σ'.kheap q = some (.schedContext y n') → specPtr p ∉ y.scReplies := by + intro q y n' hq hm + obtain ⟨c, hcc, rfl⟩ := J'.scCov q y n' hq + obtain ⟨⟨x1, k1, hx1, hL⟩, -⟩ := J'.reply.stackRel c hcc + rw [hq] at hx1 + cases hx1 + obtain ⟨⟨x2, k2, hx2, hL2⟩, -⟩ := J.reply.stackRel c hcc + exact hdet _ x2 k2 hx2 (by rw [hL2, ← hL]; exact hm) + have rd : (DelRdA st Rv s' a' σ' G (cslotAddrA sl) ∧ RdThA st OS s' (cslotAddrA sl)) ∧ + J7LA st (J7R2xA st ar ipa (cslotAddrA sl)) { Z with G := G, Q := Q', R := R', hw := hw } H OS s' σ' m := by + refine ⟨⟨⟨fun r g' h _ => ?_, fun r g' h => (by rw [hc'] at h; cases h), + fun v h => (by rw [hc'] at h; cases h)⟩, rdThA_of_ne fun t h => by rw [hc'] at h; cases h⟩, + J'.j7.pinUnreadA (by rw [hc']; exact unread)⟩ + rw [hc'] at h + cases h + exact ⟨hdet', hz⟩ + have W' : DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st J7R2tA)) s' a' σ' G := + ⟨⟨kb.trans hcore.1, cpu.trans hcore.2⟩, ⟨Q', R', hw, Ac, Bf, stack, J'.mapJ7A fun _ _ => trivial, (k6 D.sub (X.ghostA G Q R hw)).ghostA Z.G Z.Q Z.R Z.hw⟩, + ParentAdjA.keepOS D.lk k hPA, + ZombieTcbNoVcpuA.keepA caps J.zombieTcb (fun t ht => vc t (J.tcbs ▸ ht)) hZT, + bufHold_sameA st hBH caps, ctOff_sameA st hCT kb caps, hFx.keepA st D.sub caps⟩ + exact ⟨rfl, trivial, trivial, σ', G, ⟨true, hf, hmem⟩, D', hc', fun y hy _ => Or.inl (caps y hy), + ⟨fun h => absurd (Or.inl rfl) h, fun _ => W'⟩, fun _ => ⟨rd.1, m, rd.2⟩, fun _ _ _ h => by cases h⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/ReplyHookRemoveA.lean b/proofs/Fl4ProofsA64/P3/ReplyHookRemoveA.lean new file mode 100644 index 000000000..3d01caf05 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ReplyHookRemoveA.lean @@ -0,0 +1,474 @@ +/- +P3 (AArch64, delete hooks W3 Reply): **the record across the final Reply finaliser, `reply_remove` arm** (the caller +is blocked on this reply). The reply stack shrinks (`of_schedWrSN`); the donation's local facts are the one-core ones. + +[P] `hA`, `hCs`; [S] `h1`. New module. +-/ +import Fl4ProofsA64.P3.DelRec6A +import Fl4ProofsA64.P3.Stage3QKeepPresA +import Fl4ProofsA64.P3.ReplyHookRecvA +import Fl4ProofsA64.P3.ReplyHookStallA +import Fl4ProofsA64.P3.ReplyNotIdleA +import Fl4ProofsA64.P3.ReplyRemoveFootQA +import Fl4ProofsA64.P3.ReplyRemoveInvA +import Fl4ProofsA64.P3.Stage3SchedWrSTA +import Fl4ProofsA64.P3.DelBlkKeepA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 Fl4ProofsA64.P3.Endp +open Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Notif + +local notation "wa" => core.num.U64.wrapping_add + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} [J7PpA st J7] {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- A TCB word is not written by the scheduler / SC steps of `reply_remove`, nor is it one of its reply words. -/ +theorem JStage3TA.tcbWord_offRemA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {p t u K : U64} (hp : p ∈ Rp) (ht : t ∈ Z.T) (hu : u ∈ Z.T) (hK : K.val < 4096) + (h7 : K.val ≠ 3328 ∧ K.val ≠ 3464 ∧ K.val ≠ 3480 ∧ K.val ≠ 3488 ∧ K.val ≠ 3592) : + ¬ RemWrA (st := st) Z m σ s Rp p t (wa u K) := by + have hfit := (J.sr.core.tcbs u hu).fits + have hv : (wa u K).val = u.val + K.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have huH : H u = some .tcb := by + have : u ∈ H.tcbs := by rw [J.tcbs]; exact hu + exact this + have offI : ¬ IpcObjWA N E Rp (wa u K) := + J.ipc_off_hobjA (v := u) (kv := .tcb) huH (by simp) ⟨by omega, by simp only [HKind.bits]; omega⟩ + have hP := J.iplaced + rintro (h | h | h | h) + · obtain ⟨q, hq, e⟩ := h + refine offI (Or.inr (Or.inr ⟨q, hq, ?_⟩)) + rcases e with e | e + · rw [e, (replyNextPlaceA (Z := Z) hP.e hq).2]; omega + · rw [e, (replyPrevPlaceA (Z := Z) hP.e hq).2]; omega + · refine offI (Or.inr (Or.inr ⟨p, hp, ?_⟩)) + rw [h, Endp.rTcbA_val]; omega + · have h' := congrArg (fun w : U64 => w.val) h + simp only [hv, Endp.val_st (hP.e.tcb t ht)] at h' + by_cases e : u = t + · subst e; omega + · have := Endp.tcb_disj hP.e ht hu e + omega + · exact J.tcbWord_offMA hu hK h7.2 h + +/-- The arm's write set is a scheduler / IPC-object write set. -/ +theorem JStage3TA.remW_stepNA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {p t b : U64} (hp : p ∈ Rp) (ht : t ∈ Z.T) (h : RemWrA (st := st) Z m σ s Rp p t b) : + SchedStepNWA (st := st) N E Rp Z m σ s b := by + have hP := J.iplaced + rcases h with h | h | h | h + · obtain ⟨q, hq, e⟩ := h + refine Or.inr (Or.inr (Or.inr ⟨q, hq, ?_⟩)) + rcases e with e | e + · rw [e, (replyNextPlaceA (Z := Z) hP.e hq).2]; omega + · rw [e, (replyPrevPlaceA (Z := Z) hP.e hq).2]; omega + · refine Or.inr (Or.inr (Or.inr ⟨p, hp, ?_⟩)) + rw [h, Endp.rTcbA_val]; omega + · have hfit := (J.sr.core.tcbs t ht).fits + have hv : ∀ K : U64, K.val < 4096 → (wa t K).val = t.val + K.val := fun K hK => by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have e1 := hv 3328#u64 (by simp) + have eb : b = wa t 3328#u64 := by + apply UScalar.eq_of_val_eq + rw [h, Endp.val_st (hP.e.tcb t ht), e1]; rfl + have o : (3328#u64 : U64).val = 3328 := by simp + refine Or.inl (Or.inr ⟨t, ht, ?_⟩) + rw [eb] + refine ⟨by omega, by omega, fun h => ?_, fun h => ?_⟩ + · have := congrArg (fun w : U64 => w.val) h + simp only [e1, hv 3664#u64 (by simp)] at this + simp at this + · unfold runVcpuA at h + have := congrArg (fun w : U64 => w.val) h + simp only [e1, hv 3672#u64 (by simp)] at this + simp at this + · exact Or.inl (Or.inl h) + +/-- **The notification relation across `reply_remove`**: the caller is no notification waiter. -/ +theorem JStage3TA.ntfn_remFrA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {s' : S} {σ' : Fl4SpecA64.State Unit} {p t : U64} (hp : p ∈ Rp) (ht : t ∈ Z.T) + (h5 : (L.mem s (Fl4ProofsA64.P1.stA t)).val = 5) + (w : WritesIn st s s' (RemWrA (st := st) Z m σ s Rp p t)) + (K : RemKeepA σ σ') + (Kt : ∀ q y, q ≠ specPtr t → σ.kheap q = some (.tcb y) → + ∃ y', σ'.kheap q = some (.tcb y') ∧ y'.tcbState = y.tcbState) + (Ki : ∃ y, σ'.kheap (specPtr t) = some (.tcb y) ∧ y.tcbState = .inactive) + (E' : EpStateRel Z.T E Rp Bf (L.mem s') σ') : + NtfnStateRel N Z.T (L.mem s') σ' := by + have hP := J.iplaced + have tk : ∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → + (K.val ≠ 3328 ∧ K.val ≠ 3464 ∧ K.val ≠ 3480 ∧ K.val ≠ 3488 ∧ K.val ≠ 3592) → + L.mem s' (wa u K) = L.mem s (wa u K) := fun u hu K h1 h2 h3 => + w.mem_eq (field_aligned (J.sr.core.tcbs u hu) h1 h2) (J.tcbWord_offRemA hp ht hu h1 h3) + have nw : ∀ q ∈ N, ∀ K : U64, K.val < 40 → K.val % 8 = 0 → L.mem s' (wa q K) = L.mem s (wa q K) := by + intro q hq K h1 h2 + have hna := hP.n.ntfn q hq + have hv : (wa q K).val = q.val + K.val := Notif.nfield_val hna (by omega) + refine w.mem_eq (Notif.nfield_aligned hna (by omega) h2) ?_ + have hw : IpcObjWA N E Rp (wa q K) := Or.inl ⟨q, hq, by omega, by omega⟩ + rintro (h | h | h | h) + · obtain ⟨r, hr, e⟩ := h + have := hP.nr q hq r hr + rcases e with e | e + · have : (wa q K).val = r.val + 16 := by rw [e, (replyNextPlaceA (Z := Z) hP.e hr).2] + omega + · have : (wa q K).val = r.val + 8 := by rw [e, (replyPrevPlaceA (Z := Z) hP.e hr).2] + omega + · have : (wa q K).val = p.val := by rw [h, Endp.rTcbA_val] + have := hP.nr q hq p hp + omega + · have := hP.n.disj q hq t ht + have : (wa q K).val = t.val + 3328 := by rw [h, Endp.val_st (hP.e.tcb t ht)] + omega + · exact J.ipc_offMA hw h + have stOff : ∀ u ∈ Z.T, u ≠ t → L.mem s' (Fl4ProofsA64.P1.stA u) = L.mem s (Fl4ProofsA64.P1.stA u) := by + intro u hu ne + have hua := hP.e.tcb u hu + have vu := Endp.val_st hua + refine w.mem_eq (Endp.a_st hua).1 ?_ + have huH : H u = some .tcb := by + have : u ∈ H.tcbs := by rw [J.tcbs]; exact hu + exact this + have offI : ¬ IpcObjWA N E Rp (Fl4ProofsA64.P1.stA u) := + J.ipc_off_hobjA (v := u) (kv := .tcb) huH (by simp) ⟨by omega, by simp only [HKind.bits]; omega⟩ + rintro (h | h | h | h) + · obtain ⟨q, hq, e⟩ := h + refine offI (Or.inr (Or.inr ⟨q, hq, ?_⟩)) + rcases e with e | e + · rw [e, (replyNextPlaceA (Z := Z) hP.e hq).2]; omega + · rw [e, (replyPrevPlaceA (Z := Z) hP.e hq).2]; omega + · refine offI (Or.inr (Or.inr ⟨p, hp, ?_⟩)) + rw [h, Endp.rTcbA_val]; omega + · have h' := congrArg (fun w : U64 => w.val) h + simp only [vu, Endp.val_st (hP.e.tcb t ht)] at h' + have := Endp.tcb_disj hP.e ht hu ne + omega + · have e3328 : Fl4ProofsA64.P1.stA u = wa u 3328#u64 := by + apply UScalar.eq_of_val_eq + have hfit := (J.sr.core.tcbs u hu).fits + have : (wa u 3328#u64).val = u.val + 3328 := by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by simp; omega) + rw [vu, this] + rw [e3328] at h + exact J.tcbWord_offMA hu (K := 3328#u64) (by simp) (by simp) h + refine ⟨fun q hq => ?_, fun u hu => ?_⟩ + · obtain ⟨x, hx, hr⟩ := J.ntfn.ntfn q hq + refine ⟨x, K.2.1 _ _ hx (fun _ _ e => by cases e) (fun _ e => by cases e) (fun _ e => by cases e), + hr.frame ?_ fun y hy hm => ?_⟩ + · refine ⟨?_, ?_, ?_, ?_, ?_⟩ + · rw [Notif.epHeadA_eq]; exact nw q hq _ (by simp) (by simp) + · rw [Notif.epTSA_eq]; exact nw q hq _ (by simp) (by simp) + · rw [Notif.nBadgeA_eq]; exact nw q hq _ (by simp) (by simp) + · rw [Notif.nBoundA_eq]; exact nw q hq _ (by simp) (by simp) + · rw [Notif.nScA_eq]; exact nw q hq _ (by simp) (by simp) + · obtain ⟨hyT, h6, -⟩ := hr.obj.blocked hm + have ne : y ≠ t := by + intro e + rw [e] at h6 + change (L.mem s (Fl4ProofsA64.P1.stA t)).val = 6 at h6 + omega + refine ⟨stOff y hyT ne, ?_, ⟨?_, ?_⟩⟩ + · rw [Notif.tBlockA_eq]; exact tk y hyT _ (by simp) (by simp) (by simp) + · rw [Notif.prevEp_eq]; exact tk y hyT _ (by simp) (by simp) (by simp) + · rw [Notif.nextEp_eq]; exact tk y hyT _ (by simp) (by simp) (by simp) + · obtain ⟨x, hx, hr⟩ := J.ntfn.tcb u hu + obtain ⟨x', hx', he⟩ := E'.tcb u hu + obtain ⟨y, stt, sc, hy, ey⟩ := K.2.2 _ _ hx + rw [hx'] at hy + cases hy + refine ⟨x', hx', ?_⟩ + have badge : L.mem s' (badgeRegA u) = L.mem s (badgeRegA u) := by + rw [badgeRegA_eq]; exact tk u hu 256#u64 (by simp) (by simp) (by simp) + refine ⟨he.st, fun nn hn => ?_, ?_, ?_⟩ + · by_cases e : u = t + · subst e + obtain ⟨z, hz, hi⟩ := Ki + rw [hx'] at hz + cases hz + rw [hi] at hn + cases hn + · obtain ⟨z, hz, es⟩ := Kt _ _ (fun h => e (specPtr_inj h)) hx + rw [hx'] at hz + cases hz + rw [Notif.tBlockA_eq, tk u hu _ (by simp) (by simp) (by simp), ← Notif.tBlockA_eq] + exact hr.blk nn (es ▸ hn) + · rw [badge, hr.badge, ey]; rfl + · rw [Notif.prioA_eq', tk u hu _ (by simp) (by simp) (by simp), ← Notif.prioA_eq', hr.prio, ey] + +/-- **`BlkOK` after the Reply finaliser, `reply_remove` arm** (A64 P1's `fin_reply.blk`). -/ +theorem JStage3TA.replyRemove_blkA {ar : machine.Arch A} (hA : P0.ArchTotal ar) + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {p : U64} (hH : H p = some .reply) : + objects.finalise_cap_object st ar s a (.Reply p false) true ⦃ r => + BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac r.2.1 ⦄ := by + have hp : p ∈ H.replies := hH + refine WP.spec_mono (Fl4ProofsA64.P1.K7.fin_reply.blk hA s a p false true J.blkA hp (J.tcbs ▸ J.runVcpu)) ?_ + intro r h + exact h.2.1 + +set_option maxHeartbeats 2000000 in +/-- **The record across the final Reply finaliser, `reply_remove` arm** (module docstring). -/ +theorem JStage3TA.replyRemove_recA {ar : machine.Arch A} {ipa : U64} (hA : P0.ArchTotal ar) + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7LA st (J7R2A st ar ipa)) s a σ m) + {p : U64} (hH : H p = some .reply) (h0 : L.mem s (Endp.rTcbA p) ≠ 0#u64) + (hrply : L.mem s (Fl4ProofsA64.P1.stA (L.mem s (Endp.rTcbA p))) = sched.TS_BLOCKED_ON_REPLY) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) : + ipc.reply_remove st ar s a p (L.mem s (Endp.rTcbA p)) ⦃ r => + r.2 = a ∧ ∃ σ' Q R stack', + replyRemove (Z := Unit) (specPtr (L.mem s (Endp.rTcbA p))) (specPtr p) σ = ({((), σ')}, False) ∧ + JStage3TA st AR { Z with Q := Q, R := R } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack' κ n (J5A st) + (J7LA st (J7R2A st ar ipa)) r.1 a σ' m ∧ + OSKeep st OS s r.1 ∧ L.ksb r.1 = L.ksb s ∧ L.cpu r.1 = L.cpu s ∧ SchedFrameA σ σ' ∧ + (∀ t ∈ Z.T, L.mem r.1 (wa t 3664#u64) = L.mem s (wa t 3664#u64)) ∧ + L.mem r.1 (Endp.rTcbA p) = 0#u64 ∧ (∀ c ∈ Z.C, p ∉ stack' c) ∧ + Std6KeepA st Z H OS Bf s σ r.1 σ' ⦄ := by + have blk := J.replyRemove_blkA (ar := ar) hA hH + have rvi : objects.finalise_cap_object st ar s a (.Reply p false) true ⦃ r => RunVcpuInv st H.tcbs r.2.1 ⦄ := + Fl4ProofsA64.P1.fin_reply.rvi hA s a p false true J.blkA (show p ∈ H.replies from hH) (J.tcbs ▸ J.runVcpu) + obtain ⟨t, et⟩ : ∃ t, L.mem s (Endp.rTcbA p) = t := ⟨_, rfl⟩ + have eprog : objects.finalise_cap_object st ar s a (.Reply p false) true = (do + let (s1, a1) ← ipc.reply_remove st ar s a p t + ok ((capability.Cap.Null, capability.Cap.Null), s1, a1)) := by + have e1 : obj.Reply.tcb st p s = ok t := (eq_of_spec_fd (obj.Reply.tcb.spec (L := L) s p)).trans (congrArg ok et) + have en : obj.Tcb.is_null t = ok false := + (eq_of_spec_fd (obj.Tcb.is_null.spec _)).trans (by rw [← et]; simp [h0]) + have es : obj.Tcb.state st t s = ok sched.TS_BLOCKED_ON_REPLY := + (eq_of_spec_fd (obj.Tcb.state.spec (L := L) s _)).trans (congrArg ok (et ▸ hrply)) + simp only [objects.finalise_cap_object, ↓reduceIte, e1, en, es, bind_ok, Bool.false_eq_true] + rw [eprog] at blk rvi + rw [et] at h0 hrply ⊢ + have hp : p ∈ Rp := J.ipcHeapCov.reply p hH + have back := J.blkA.2.2.1.2.1.back p hH (by show L.mem s (Endp.rTcbA p) ≠ 0#u64; rw [et]; exact h0) + have ht : t ∈ Z.T := by rw [← et]; exact (J.replyCallerA hH (by rw [et]; exact h0)).1 + have hip : L.mem s (tReplyA t) = p := by rw [← et]; exact back.2.2.1 + have hc := J.curCore_lt hCs + have hP := J.iplaced + obtain ⟨x, hk, hx⟩ := J.ep.tcb t ht + have h5 : (L.mem s (Fl4ProofsA64.P1.stA t)).val = 5 := by + rw [hrply]; unfold sched.TS_BLOCKED_ON_REPLY; rfl + have h5c : tsCode x.tcbState = 5 := by rw [← hx.st]; exact h5 + obtain ⟨ro, hs⟩ := tsCode_five h5c + have ero : ro = specPtr p := by rw [← hx.reply ro hs, hip] + subst ero + obtain ⟨xr, hkr, hrb⟩ := J.ep.reply p hp + have hback : xr.replyTcb = some (specPtr t) := by rw [hrb, et, optRef_ne h0] + have hst : x.tcbState = .blockedOnReply (specPtr p) ∨ + ∃ ep d, x.tcbState = .blockedOnReceive ep (some (specPtr p)) d := Or.inl hs + have hncl : ¬ inClassRSc x.tcbState x.tcbFault := by + rw [hs] + rintro (h | ⟨_, h⟩ | ⟨_, _, h, _⟩ | ⟨_, _, _, h⟩) + · cases h + · cases h + · cases h + · cases h + have hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue := by + intro e he z hz hm + obtain ⟨z0, hz0, rel⟩ := J.ep.ep e he + rw [hz] at hz0 + cases hz0 + have h5' : (L.mem s (Fl4ProofsA64.P1.stA t)).val = 5 := h5 + cases z with + | idleEP => simp [Endpoint.epQueue] at hm + | sendEP q => + obtain ⟨-, xs, -, rfl, -, hq⟩ := rel + have := (hq t (mem_map_specPtrSc.1 hm)).1 + omega + | recvEP q => + obtain ⟨-, xs, -, rfl, -, hq⟩ := rel + have := (hq t (mem_map_specPtrSc.1 hm)).1 + omega + have hsc0 : ∀ sc ∈ Z.C, (stack sc).head? = some p → sc ≠ 0#u64 := fun sc hc _ => J.scNZ sc hc + have hlocal : ∀ sc ∈ Z.C, (stack sc).head? = some p → x.tcbSchedContext = none → + ReplyPopLocalQA (st := st) (ar := ar) (Z := Z) (m := m) a s σ sc t p := by + intro sc hsc hh _ + obtain ⟨rest, hl⟩ : ∃ rest, stack sc = p :: rest := by + cases ee : stack sc with + | nil => rw [ee] at hh; cases hh + | cons d l => + rw [ee] at hh + cases hh + exact ⟨l, rfl⟩ + obtain ⟨⟨y, k, hy, hL⟩, -⟩ := J.reply.stackRel sc hsc + have cur : ∀ c < fl4A64Platform.numNodes, c = m.curCore := fun c h => + (Nat.lt_one_iff.1 (h1 ▸ h)).trans (Nat.lt_one_iff.1 (h1 ▸ J.curCore_lt hCs)).symm + refine ⟨notIdleSc_of_repliesA J.sidle cur hy (by rw [hL, hl]; simp), ?_, ?_⟩ + · rw [J.onCore_single hCs h1 ht, J.sr.scCore sc hsc] + have a1 := J.scCpu sc hsc + have a2 := hCs _ J.j.1.sch.cpuCs + exact UScalar.eq_of_val_eq (by omega) + · by_cases hz : L.mem s (scTcbA sc) = 0#u64 + · exact Or.inl hz + have hscH : sc ∈ H.scs := J.scs.symm ▸ hsc + have hX := J.blkA.2.2.1.2.2 + have hf : L.mem s (scTcbA sc) ∈ Z.T := J.tcbs ▸ hX.yt.1 sc hscH hz + exact Or.inr ⟨_, hf, hX.tsc sc hscH (J.scNZ sc hsc) hz, J.onCore_single hCs h1 hf, + fun s1 hrun => J.replyStallA hCs h1 hsc hl hf hrun⟩ + have rel := reply_remove_relQA_sub (st := st) (ar := ar) a J.sr J.ipcPlace hP.e J.ep J.reply hp ht + (J.scCov.reply _) hk hkr hback hst hq hsc0 hlocal hncl + have fp := reply_remove_footQA (st := st) (ar := ar) a J.sr J.ipcPlace hP.e J.ep J.reply hp ht + hk hkr hback hst hq (J.scCov.reply _) hsc0 hlocal hncl + have blk' : ipc.reply_remove st ar s a p t ⦃ r => + BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac r.1 ⦄ := by + obtain ⟨⟨s1, a1⟩, hr, -⟩ := spec_existsA rel + rw [hr, bind_ok] at blk + rw [hr] + exact (WP.spec_ok _).2 ((WP.spec_ok ((capability.Cap.Null, capability.Cap.Null), s1, a1)).1 blk) + have rv' : ipc.reply_remove st ar s a p t ⦃ r => RunVcpuInv st H.tcbs r.1 ⦄ := by + obtain ⟨⟨s1, a1⟩, hr, -⟩ := spec_existsA rel + rw [hr, bind_ok] at rvi + rw [hr] + exact (WP.spec_ok _).2 ((WP.spec_ok ((capability.Cap.Null, capability.Cap.Null), s1, a1)).1 rvi) + refine WP.spec_mono (WP.spec_and (WP.spec_and blk' rv') (WP.spec_and rel fp)) ?_ + rintro ⟨s', a'⟩ ⟨⟨bk, rv⟩, ⟨ha, σ1, stack', hsp1, SR', E', S', iso, hsub⟩, wF, σ', hsp, K⟩ + dsimp only at bk rv ha E' S' iso wF ⊢ + have e2 : σ1 = σ' := by + have := hsp1.symm.trans hsp + simpa using congrArg Prod.fst this + subst e2 + have hm : ((), σ1) ∈ (replyRemove (Z := Unit) (specPtr t) (specPtr p) σ).1 := by + rw [hsp]; exact Set.mem_singleton _ + have F : SchedFrameA σ σ1 := + (SPresInst.pres : SPresA (replyRemove (Z := Unit) (specPtr t) (specPtr p))) σ ((), σ1) hm + obtain ⟨inv, scK, Kt, Ki, Kr, nsw⟩ := replyRemove_stepA (m := m) (n := n) ⟨J.sinv, J.sidle, J.sact⟩ hk hst hm + have w : WritesIn st s s' (SchedStepNWA (st := st) N E Rp Z m σ s) := + wF.mono fun _ h => J.remW_stepNA hp ht h + have tw : ∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → + (K.val ≠ 3328 ∧ K.val ≠ 3464 ∧ K.val ≠ 3480 ∧ K.val ≠ 3488 ∧ K.val ≠ 3592) → + L.mem s' (wa u K) = L.mem s (wa u K) := fun u hu K h1 h2 h3 => + wF.mem_eq (field_aligned (J.sr.core.tcbs u hu) h1 h2) (J.tcbWord_offRemA hp ht hu h1 h3) + have TK : ∀ q y, σ.kheap q = some (.tcb y) → ∃ y2, σ1.kheap q = some (.tcb y2) ∧ + y2.tcbIpcframe = y.tcbIpcframe ∧ y2.tcbIpcBuffer = y.tcbIpcBuffer ∧ y2.tcbFault = y.tcbFault ∧ + y2.tcbMcpriority = y.tcbMcpriority ∧ y2.tcbYieldTo = y.tcbYieldTo ∧ y2.tcbFlags = y.tcbFlags := fun q y h => by + obtain ⟨y2, stt, sc, hy, ey⟩ := K.2.2 _ _ h + exact ⟨y2, hy, by rw [ey], by rw [ey], by rw [ey], by rw [ey], by rw [ey], by rw [ey]⟩ + obtain ⟨Q', R', H3⟩ := SR' + have sq : SchedQueuesInv st H.tcbs Z.Cs H.omega H s' Q' R' := by + obtain ⟨Q0, R0, h0'⟩ := bk.1 + exact SchedQueuesInv.reQA h0' (by rw [J.tcbs]; exact H3.core) + have kb : L.ksb s' = L.ksb s := w.1 + have poolOff : PoolOff st H Z.Cs OS s' := fun i => by + have h0' := J.poolOff i + rw [vspace.asid_table.eq] at h0' ⊢ + have v := AtOff.v i + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.ent.ksw + have ev : (wa (L.ksb s) (AtOff i)).val = (L.ksb s).val + (AtOff i).val := by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have e0 : L.mem s' (wa (L.ksb s') (AtOff i)) = L.mem s (wa (L.ksb s) (AtOff i)) := by + rw [kb] + refine w.mem_eq ?_ (J.asid_offStepNA (by omega) (by omega)) + have h32 := J.j.1.ksbAl + show (wa (L.ksb s) (AtOff i)).val % 8 = 0 + omega + rw [e0] + refine (WP.spec_ok _).2 fun hne asid => ?_ + have q := (WP.spec_ok _).1 h0' hne asid + exact ⟨q.al, q.om, q.sg, q.os, RegionOffNodes.ksb kb q.ks⟩ + have ntfn := J.ntfn_remFrA hp ht h5 wF K Kt Ki E' + have mcp : McpRelA Z.T m (L.mem s') σ1 := fun u hu => by + obtain ⟨y, hy, e0, hf⟩ := J.mcp u hu + obtain ⟨y2, h2, -, -, -, mc, -, mf⟩ := TK _ _ hy + refine ⟨y2, h2, ?_, fun g => (hf g).keep (tw u hu 3456#u64 (by simp) (by simp) (by simp)) mf⟩ + rw [mc] + rw [show mcpA u = wa u 3440#u64 from rfl] at e0 ⊢ + rw [tw u hu 3440#u64 (by simp) (by simp) (by simp)] + exact e0 + have yw : ∀ u ∈ Z.T, L.mem s' (yieldA u) = L.mem s (yieldA u) := fun u hu => by + rw [show yieldA u = wa u 3472#u64 from rfl] + exact tw u hu 3472#u64 (by simp) (by simp) (by simp) + have yr : YieldRelA Z.T Z.C m (L.mem s') σ1 := by + refine ⟨fun u hu => ?_, fun u hu h => ?_, fun c hcc y k hy q hq' => ?_⟩ + · obtain ⟨y, hy, e0⟩ := J.yieldRel.tcb u hu + obtain ⟨y2, h2, -, -, -, -, yt, -⟩ := TK _ _ hy + exact ⟨y2, h2, by rw [yt, yw u hu]; exact e0⟩ + · rw [yw u hu] at h ⊢ + obtain ⟨hcc, y, k, hy, e0⟩ := J.yieldRel.toSc u hu h + obtain ⟨y', hy', ey⟩ := scK _ _ _ hy + exact ⟨hcc, y', k, hy', ey.trans e0⟩ + · obtain ⟨y0, hy0⟩ := F.2.1 _ _ _ hy + obtain ⟨y', hy', ey⟩ := scK _ _ _ hy0 + rw [hy] at hy' + cases hy' + obtain ⟨u, hu, e1, e3⟩ := J.yieldRel.frm c hcc y0 k hy0 q (ey ▸ hq') + exact ⟨u, hu, e1, by rw [yw u hu]; exact e3⟩ + have buf : BufCover st Z.T Bf s' σ1 := + J.bufCover_wA wF (fun u hu K hK => J.tcbWord_offRemA hp ht hu (by omega) (by omega)) + (fun q y h => by + obtain ⟨y2, hy, b, c, -⟩ := TK q y h + exact ⟨y2, hy, b, c⟩) + have bufAl : UtBufAlA Z.T σ1 := fun u hu y hy => by + obtain ⟨x0, hx0, -⟩ := J.ntfn.tcb u hu + obtain ⟨y', hy', -, e0, -⟩ := TK _ _ hx0 + rw [hy] at hy'; cases hy' + rw [e0]; exact J.bufAl u hu x0 hx0 + have idle : IdleNoFaultA m σ1 := fun c hc' => by + obtain ⟨x0, hx0, hf⟩ := J.idleNoFault c hc' + obtain ⟨y, hy, -, -, g, -⟩ := TK _ _ hx0 + exact ⟨y, hy, g.trans hf⟩ + have scCov : ScCovA Z.C m σ1 := fun q y k h => by + obtain ⟨y', hy⟩ := F.2.1 q y k h + exact J.scCov q y' k hy + have hvr : ∀ sc ∈ Z.C, ∀ q ∈ stack' sc, RBlk st s' q := by + refine J.vr_specA bk hsub (fun sc hsc q hq c hcT ec x o hx hs => ?_) E' + (fun c hc => tw c hc 3360#u64 (by simp) (by simp) (by simp)) + have ne : c ≠ t := by + intro e + have mR : q ∈ Rp := (J.reply.stackRel sc hsc).2.1 q (hsub sc q hq) + have old := J.vr sc hsc q (hsub sc q hq) + have bk0 := J.blkA.2.2.1.2.1.back q (J.utIpc.reply q mR) old.tcb + have eq0 : L.mem s (Fl4ProofsA64.P1.replyA c) = q := by rw [ec]; exact bk0.2.2.1 + have eq1 : L.mem s (tReplyA t) = q := by + rw [← e, ← eq0] + unfold Fl4ProofsA64.P1.replyA tReplyA; rfl + rw [hip] at eq1 + exact iso.1 sc hsc (eq1 ▸ hq) + obtain ⟨y, hy, es⟩ := Kt _ _ (fun h => ne (specPtr_inj h)) hx + exact ⟨y, hy, es⟩ + have j := J.j57_stepNWA w σ1 + have bq : BlkQueuedA Z.T E N (L.mem s') σ1 := by + have M : BlkMonoA (specPtr p) σ σ1 := by + refine ⟨fun u y hy => ?_, fun r y _ hy => ?_, fun r y _ hy => ?_⟩ + · by_cases eu : u = specPtr t + · obtain ⟨z, hz, hi⟩ := Ki + exact ⟨z, by rw [eu]; exact hz, Or.inr (hi ▸ noBlk_inactiveA)⟩ + · obtain ⟨z, hz, es⟩ := Kt _ _ eu hy + exact ⟨z, hz, Or.inl es⟩ + · exact K.2.1 _ _ hy (fun _ _ e => by cases e) (fun _ e => by cases e) (fun _ e => by cases e) + · exact ⟨y, K.2.1 _ _ hy (fun _ _ e => by cases e) (fun _ e => by cases e) (fun _ e => by cases e), rfl⟩ + refine J.blkQ.finA M (fun u hu => ?_) (fun u hu => ?_) (fun y hy => ?_) (fun y q hn => ?_) + · obtain ⟨x0, hx0, -⟩ := J.ntfn.tcb u hu + exact ⟨x0, hx0⟩ + · rw [Notif.tBlockA_eq]; exact tw u hu 3336#u64 (by simp) (by simp) (by simp) + · rw [hkr] at hy; cases hy + · rw [hkr] at hn; cases hn + have J' := JStage3TA.of_schedWrSN (blkQ := bq) J (stack' := stack') hsub (Q := Q') (Rl := R') (curOf := Z.curOf) + (curScOf := Z.curScOf) (s' := s') (a' := a) (σ' := σ1) w F.1 F.2.1 (Sched.qk_of hm) F.2.2 J.j.1.ar (fun _ _ => rfl) bufAl + (J.ent_stepNWA w bk) sq poolOff H3 scCov E' ntfn S' buf idle mcp yr inv.sinv inv.sidle (J.tcbs ▸ rv) hvr (nsw J.noSwitch) + inv.sact j.1 j.2 + have hz : L.mem s' (Endp.rTcbA p) = 0#u64 := by + obtain ⟨xr', hkr', hrb'⟩ := E'.reply p hp + obtain ⟨z, hz, ez⟩ := Kr + rw [hz] at hkr' + cases hkr' + rw [ez] at hrb' + by_contra hne + rw [optRef_ne hne] at hrb' + cases hrb' + exact ⟨ha, σ1, Q', R', stack', hsp, ha ▸ J', J.sigW_osKeepA w, kb, w.2.1, F, + fun u hu => tw u hu 3664#u64 (by simp) (by simp) (by simp), hz, iso.1, + fun {_} sub h => Std6GA.schedStepA J h sub w (J.sigW_osKeepA w) F⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/ReplyHookStallA.lean b/proofs/Fl4ProofsA64/P3/ReplyHookStallA.lean new file mode 100644 index 000000000..11e8d19b1 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ReplyHookStallA.lean @@ -0,0 +1,163 @@ +/- +P3 (AArch64, delete hooks W3 Reply): **the stall words of `reply_pop` across its prefix** on one core. + +* `replyPopPrepare_keepA`: the prefix `replyPopPrepareA` keeps every aligned word off the SC write set and off the + reply objects. +* `JStage3TA.runCaller_offA`: a VCPU's `RUN_CALLER` word is such a word. +* `JStage3TA.replyStallA`: the donation's stall hypothesis of `ReplyPopLocalQA` at [S] `numNodes = 1`. + +Premises: [P] `hCs`; [S] `h1`. New module. +-/ +import Fl4ProofsA64.P3.ReplyStallA +import Fl4ProofsA64.P3.ReplyRemoveQA +import Fl4ProofsA64.P3.DelNtfnRecA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4SpecA64 Sched + +local notation "wa" => core.num.U64.wrapping_add + +noncomputable section + +section Store +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + +/-- **The pop prefix writes SC words and reply words only.** -/ +theorem replyPopPrepare_keepA {s : S} {σ : State Unit} {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {sc r : U64} (hc : sc ∈ Z.C) {rest : List U64} (hl : stack sc = r :: rest) : + replyPopPrepareA (st := st) s r ⦃ s' => ∀ b, Aligned b → ¬ MWrA (st := st) Z m σ s b → + (∀ q ∈ R, ¬ (q.val ≤ b.val ∧ b.val < q.val + 32)) → L.mem s' b = L.mem s b ⦄ := by + obtain ⟨ra, nx, pv⟩ := replyHeadWordsA H PE HS hc hl + obtain ⟨⟨x, n, hk, hL⟩, hm, hn, _⟩ := HS.stackRel sc hc + have hr : r ∈ R := hm r (by simp [hl]) + obtain ⟨y, hky, hy, hh, hp, ht⟩ := HS.popFacts PE HE hc (by rw [hl]; rfl) + have hPopp : ∀ x n, σ.kheap (scNameA m sc) = some (.schedContext x n) → + x.scReplies.tail.head? = optRefS (rest.headD 0#u64) := by + rw [← pv] + exact hp + have offR : ∀ (s1 : S) (q v b : U64), q ∈ R → Aligned b → + (∀ q ∈ R, ¬ (q.val ≤ b.val ∧ b.val < q.val + 32)) → + L.mem (L.wr s1 (rNextA q) v) b = L.mem s1 b := by + intro s1 q v b hq hb off + have na := replyNextPlaceA (Z := Z) PE hq + refine L.mem_wr_ne na.1 hb fun e => off q hq ?_ + have := na.2 + omega + unfold replyPopPrepareA + refine WP.spec_bind (obj.Reply.next.spec s r) ?_ + rintro a rfl + rw [nx] + refine WP.spec_bind (obj.Reply.prev.spec s r) ?_ + rintro a rfl + rw [pv, nextRawEq, bind_ok, ra] + refine WP.spec_bind (reply_pop_scA H hc hky hy hh hPopp ht) ?_ + rintro s1 ⟨z, k, -, -, -, w1, -⟩ + have available : ∀ d ∈ rest, d ∈ R := fun d hd => hm d (by rw [hl]; exact List.mem_cons_of_mem _ hd) + have step : (if rest.headD 0#u64 != 0#u64 then obj.Reply.set_next_raw st (rest.headD 0#u64) s1 (sc ||| 1#u64) + else ok s1) ⦃ s2 => ∀ b, Aligned b → (∀ q ∈ R, ¬ (q.val ≤ b.val ∧ b.val < q.val + 32)) → + L.mem s2 b = L.mem s1 b ⦄ := by + cases rest with + | nil => exact (WP.spec_ok _).2 fun _ _ _ => rfl + | cons q rest => + have hq := available q (by simp) + have hn : q ≠ 0#u64 := fun e => (PE.rep q hq).2.1 (by rw [e]; rfl) + simp only [List.headD_cons, show (q != 0#u64) = true from by simpa using hn, ↓reduceIte, + setNextRawEq, WP.spec_ok] + exact fun b hb off => offR s1 q _ b hq hb off + refine WP.spec_bind step ?_ + intro s2 k2 + rw [setNextZeroEq, WP.spec_ok] + intro b hb hM off + rw [offR s2 r _ b hr hb off, k2 b hb off] + exact w1.mem_eq hb hM + +end Store +end +end Fl4ProofsA64.P3.Endp + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Endp Fl4SpecA64 + +local notation "wa" => core.num.U64.wrapping_add + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 : JSlotA S} {R : Set U64 → JSlotA S} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} + +/-- **A VCPU's `RUN_CALLER` word is written by no SC step, and is in no reply object.** -/ +theorem JStage3TA.runCaller_offA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7LA st R) s a σ m) + {v : U64} (hv : v ∈ Z.V) : + Aligned (wa v 24#u64) ∧ ¬ MWrA (st := st) Z m σ s (wa v 24#u64) ∧ + (∀ q ∈ Rp, ¬ (q.val ≤ (wa v 24#u64).val ∧ (wa v 24#u64).val < q.val + 32)) := by + obtain ⟨Pt, Ub, -, -, arm, -, -, -, -, -, apart, -⟩ := J.j7 + have hH := arm.vh v hv + have hfit := J.ent.heap.fits v _ hH + have hal := J.ent.heap.align v _ hH + simp only [HKind.bits] at hfit hal + have o24 : (24#u64 : U64).val = 24 := by simp + have ev : (wa v 24#u64).val = v.val + 24 := by + rw [wa_valm, o24]; exact Nat.mod_eq_of_lt (by omega) + have al : Aligned (wa v 24#u64) := by show _ % 8 = 0; rw [ev]; omega + have inObj : InObj v (.other 12) (wa v 24#u64) := by + unfold InObj; simp only [HKind.bits]; omega + have hsc : ∀ c ∈ H.scs, ¬ InSC c (H.scBits c) (wa v 24#u64) := by + rintro c ⟨z, hz⟩ ⟨c1, c2⟩ + have e : H.scBits c = z := by unfold Heap.scBits; rw [hz] + have ne : v ≠ c := fun h => by rw [h, hz] at hH; cases hH + have := J.ent.heap.disj v _ c _ hH hz ne + simp only [HKind.bits] at this + rw [e] at c2 + omega + have hg : ∀ g : U64, g.val < 223488 → wa (L.ksb s) g ≠ wa v 24#u64 := by + intro g hg e + exact apart _ ⟨v, hv, 24#u64, by omega, by omega, rfl⟩ g hg e + have offI : ¬ IpcObjWA N E Rp (wa v 24#u64) := J.ipc_off_hobjA hH (by simp) inObj + have off := J.omega_offStepA ⟨al, v, _, hH, Or.inr (Or.inr (Or.inl ⟨12, rfl⟩)), inObj⟩ hsc hg + exact ⟨al, fun h => off (Or.inl h), fun q hq h => offI (Or.inr (Or.inr ⟨q, hq, h⟩))⟩ + +/-- **The stall of the donor after the pop prefix is the no-op on one core** (module docstring). -/ +theorem JStage3TA.replyStallA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7LA st R) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + {sc r f : U64} (hc : sc ∈ Z.C) {rest : List U64} (hl : stack sc = r :: rest) (hf : f ∈ Z.T) + {s1 : S} (hrun : replyPopPrepareA (st := st) s r = ok s1) : + smp.remote_tcb_stall st ar s1 a f = ok (s1, a) := by + obtain ⟨s', hr', x, k, hk, -, H1, -, -, -, vc⟩ := + spec_existsA (replyPopPrepareRefA J.sr J.ipcPlace J.iplaced.e J.ep J.reply hc hl) + rw [hrun] at hr' + cases ok_inj_ff hr' + obtain ⟨s'', hr'', keep⟩ := spec_existsA (replyPopPrepare_keepA J.sr J.iplaced.e J.ep J.reply hc hl) + rw [hrun] at hr'' + cases ok_inj_ff hr'' + have aff : ∀ t ∈ Z.T, L.mem s1 (affA t) = L.cpu s1 := fun t ht => by + rw [J.sr.aff_eq H1 ht ht, J.sr.cpu_eq H1] + exact J.onCore_single hCs h1 ht + have vf : L.mem s1 (wa f 3664#u64) = L.mem s (wa f 3664#u64) := vc f hf + refine stall_single_noopA a (aff f hf) fun h0 h2 => ?_ + rw [vf] at h0 h2 ⊢ + rcases J.vcRun.tcb f hf with e | hv + · exact absurd e h0 + obtain ⟨al, hM, hR⟩ := J.runCaller_offA hv + have ew := keep _ al hM hR + rw [ew] at h2 ⊢ + rcases J.vcRun.vcpu _ hv with e | ht + · exact absurd e h2 + exact aff _ ht + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/ReplyLiveJointA.lean b/proofs/Fl4ProofsA64/P3/ReplyLiveJointA.lean new file mode 100644 index 000000000..79eeb6643 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ReplyLiveJointA.lean @@ -0,0 +1,419 @@ +/- +P3 (AArch64): the live Reply joint producer. + +* `ReplyMidA`: the joint state after `reply_remove` and the refill test (scheduler, endpoint + and reply-stack relations, the spec frame from the entry, the transfer facts, the receiver + `Inactive`). **`replyRemoveThenUnblockA`**: the removal, then the refill test from the + reached state (`replyRemoveUnblockJointBorA` split at the removal). +* **`replyNormalJointA`** (recipe steps 1–2): `do_ipc_transfer_anyA` from the joint state + (fault-free sender, no extra-cap insert), then `set_thread_state_exAnyA` (Running) into + `SchedSRB` at the receiver; the receiver is in no spec queue. +* `ReplyTailReadyA`, **`replyTailJointA`** (step 3): `replyTailExA` back to `IpcSchedRelA`, or the + caller's timeout-send relation. +* **`do_reply_transfer_jointA`** (steps 4–5): `do_reply_transfer_liveA` instantiated with the + above; the fault arm is caller-selected (`faultArm`, through `replyFaultBody_refinesA`). + +[S] / [J] as `replyRemoveUnblockJointBorA`: `hlocal`, `unb`, no `hirqR` / `hirqU` premise (R-hirq: `IrqSlotsKeepA.of_replyFoot`) +(`IrqSlotsKeepA`, ruled 17:49Z, discharged at the Stage-3 merge). The extra-cap insert of a +granting transfer is the named Stage-3 obligation (`IpcExtraCapTransferA`); `hcaps` excludes it. +-/ +import Fl4ProofsA64.P3.ReplyRemoveBorA +import Fl4ProofsA64.P3.IrqSlotsFootA +import Fl4ProofsA64.P3.ReplyTailExA +import Fl4ProofsA64.P3.ReplyLiveSpecA +import Fl4ProofsA64.P3.IpcScReplyBodyA +import Fl4ProofsA64.P3.EpFaultXferA +import Fl4ProofsA64.P3.Stage3ReplyWA +import Fl4ProofsA64.P3.SpecFrameIpcA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4SpecA64 Sched + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-- The joint state after the removal and the refill test (module docstring). -/ +def ReplyMidA {S : Type} (st : machine.Store S) [L : LawfulStore st] (Z : XSetsA) (m : McState) + (κ : U64 → U64) (E R : Set U64) (Bf : Set (U64 × Word)) (s : S) (σ : State Unit) (r t : U64) + (s2 : S) (σ2 : State Unit) : Prop := + ∃ Q1 R1 stack', SchedSRA st { Z with Q := Q1, R := R1 } m κ s2 σ2 ∧ + EpStateRel Z.T E R Bf (L.mem s2) σ2 ∧ + ReplyStackRelA (L.mem s2) R Z.C σ2 (scNameA m) stack' ∧ ReplyIsolatedA (L.mem s2) Z.C stack' r ∧ + RemKeepA σ σ2 ∧ StateRelA Z.th (L.mem s2) σ2 ∧ SlotsValidA Z.th (L.mem s2) ∧ + BufCover st Z.T Bf s2 σ2 ∧ BufNativeA Z.T (L.mem s2) ∧ + (∃ y2, σ2.kheap (specPtr t) = some (.tcb y2) ∧ y2.tcbState = .inactive) ∧ + WritesIn st s s2 (ReplyFootW (st := st) Z m σ s E R Bf) ∧ SSzA σ σ2 + +private theorem reachedLA {α : Type} {r : Result α} {P : α → Prop} (step : r ⦃ P ⦄) : + r ⦃ v => r = ok v ∧ P v ⦄ := by + obtain ⟨v, eq, post⟩ := spec_ex_fd step + rw [eq, WP.spec_ok] + exact ⟨rfl, post⟩ + +section Store +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + +/-- **The removal, then the refill test from the reached state.** -/ +theorem replyRemoveThenUnblockA {s : S} {σ : State Unit} (a : A) {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {r t : U64} (hr : r ∈ R) (ht : t ∈ Z.T) + {xt : Tcb} {xr : Reply} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hkr : σ.kheap (specPtr r) = some (.reply xr)) (hb : xr.replyTcb = some (specPtr t)) + (hst : xt.tcbState = .blockedOnReply (specPtr r)) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) + (cov : ScReplyCovA Z.C m σ (specPtr r)) + (hsc0 : ∀ sc ∈ Z.C, (stack sc).head? = some r → sc ≠ 0#u64) + (hlocal : ∀ sc ∈ Z.C, (stack sc).head? = some r → xt.tcbSchedContext = none → + ReplyPopLocalA (st := st) (Z := Z) (m := m) s σ sc t) + (unb : ∀ s1 σ1 Q1 R1, SchedSRA st { Z with Q := Q1, R := R1 } m κ s1 σ1 → RemKeepA σ σ1 → + (L.mem s1 (scA t) ≠ 0#u64 → L.mem s1 (scCoreA (L.mem s1 (scA t))) = L.cpu s1) ∧ + (specPtr t ∈ σ1.releaseQueue → ∀ y n, + σ1.kheap (scNameA m (L.mem s1 (scA t))) = some (.schedContext y n) → + refillReady σ1.curTime (refillHd y) = false)) + (hoff : ∀ y, IsSlotA Z.th y → ¬ EWrite Z.T E R Bf y ∧ ¬ EWrite Z.T E R Bf (wa y 8#u64)) + (hSt : StateRelA Z.th (L.mem s) σ) (hV : SlotsValidA Z.th (L.mem s)) + (hC : BufCover st Z.T Bf s σ) (hNative : BufNativeA Z.T (L.mem s)) : + ipc.reply_remove st ar s a r t ⦃ out => ∃ σ1, + replyRemove (Z := Unit) (specPtr t) (specPtr r) σ = ({((), σ1)}, False) ∧ out.2 = a ∧ + replyUnblockProgramA st out.1 t ⦃ s2 => ∃ σ2, + (getTcbObjRef (Z := Unit) Tcb.tcbSchedContext (specPtr t) >>= + ifSporadicCurScTestRefillUnblockCheck) σ1 = ({((), σ2)}, False) ∧ + ReplyMidA st Z m κ E R Bf s σ r t s2 σ2 ⦄ ⦄ := by + have hcls : ¬ inClassRSc xt.tcbState xt.tcbFault := by + rw [hst] + simp [inClassRSc, runnable] + have hTcb : ∀ u ∈ Z.T, ∃ x0, σ.kheap (specPtr u) = some (.tcb x0) := fun u hu => by + obtain ⟨x, hx, -⟩ := HE.tcb u hu + exact ⟨x, hx⟩ + refine WP.spec_mono (reachedLA (WP.spec_and + (reply_remove_relBorA a H P PE HE HS hr ht cov hkt hkr hb (Or.inl hst) hq hsc0 hlocal hcls) + (reply_remove_footBorA a H P PE HE HS hr ht hkt hkr hb (Or.inl hst) hq cov hsc0 hlocal hcls))) ?_ + rintro ⟨s1, a1⟩ ⟨hreach, ⟨ha, σ1, stack', hu1, ⟨Q1, R1, H1⟩, E1, S1, iso1⟩, w1, σ1', hu1', keep1⟩ + have e1 : σ1' = σ1 := det_outA hu1' hu1 + subst e1 + have X1 := replyXferFactsA H.toSchedSRN PE P hoff w1 keep1 (Fl4ProofsA64.P3.Sched.IrqSlotsKeepA.of_replyFoot H.toSchedSRN PE P w1) hSt hV hC hNative hTcb + obtain ⟨hon, hrq⟩ := unb s1 σ1' Q1 R1 H1 keep1 + obtain ⟨x1, hk1, -⟩ := E1.tcb t ht + obtain ⟨yi, hyi, hyst⟩ := replyRemove_inactiveA hu1 + refine ⟨σ1', hu1, ha, ?_⟩ + refine WP.spec_mono (reachedLA (replyUnblockFrameA H1 P PE E1 S1 ht hr iso1 hk1 hon hrq)) ?_ + rintro s2 ⟨hreach2, σ2, hu2, H2, E2, S2', iso2, F, -, hmem, -⟩ + have keep2 : RemKeepA σ1' σ2 := RemKeepA.of_skeep F.keep hmem + have w2 : WritesIn st s1 s2 (ReplyFootW (st := st) { Z with Q := Q1, R := R1 } m σ1' s1 E R Bf) := + F.wr.mono fun b hb => Or.inr (Or.inl hb) + have hTcb1 : ∀ u ∈ Z.T, ∃ x0, σ1'.kheap (specPtr u) = some (.tcb x0) := fun u hu => by + obtain ⟨x, hx, -⟩ := E1.tcb u hu + exact ⟨x, hx⟩ + have X2 := replyXferFactsA (Z := { Z with Q := Q1, R := R1 }) H1.toSchedSRN PE P hoff w2 keep2 + (Fl4ProofsA64.P3.Sched.IrqSlotsKeepA.of_replyFoot H1.toSchedSRN PE P w2) X1.1 X1.2.1 X1.2.2.1 X1.2.2.2 hTcb1 + obtain ⟨y2, hy2, o2, ey2⟩ := F.keep.tcb _ _ hyi + have hz1 : SSzA σ σ1' := (SPresInst.pres (m := replyRemove (Z := Unit) (specPtr t) (specPtr r))) σ ((), σ1') + (by rw [hu1]; exact Set.mem_singleton _) |>.2.1 + refine ⟨σ2, hu2, Q1, R1, stack', H2, E2, S2', iso2, keep1.trans keep2, X2.1, X2.2.1, X2.2.2.1, + X2.2.2.2, ⟨y2, hy2, ?_⟩, ReplyFootW.trans rfl rfl w1 hz1 w2, hz1.trans F.sz⟩ + rw [ey2] + exact hyst + +/-- **Recipe steps 1–2: the normal Reply body from the joint state** (module docstring). -/ +theorem replyNormalJointA {s2 : S} {a : A} {σ2 : State Unit} {sender t : U64} {grant : Bool} + (H2 : SchedSRA st Z m κ s2 σ2) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (E2 : EpStateRel Z.T E R Bf (L.mem s2) σ2) + (hSt : StateRelA Z.th (L.mem s2) σ2) (hV : SlotsValidA Z.th (L.mem s2)) + (hC : BufCover st Z.T Bf s2 σ2) (hNative : BufNativeA Z.T (L.mem s2)) + (hoff : ∀ y, IsSlotA Z.th y → ¬ EWrite Z.T E R Bf y ∧ ¬ EWrite Z.T E R Bf (wa y 8#u64)) + (hs : sender ∈ Z.T) (ht : t ∈ Z.T) (hne : sender ≠ t) + {xs y2 : Tcb} (hks : σ2.kheap (specPtr sender) = some (.tcb xs)) + (hky : σ2.kheap (specPtr t) = some (.tcb y2)) (hy2 : y2.tcbState = .inactive) + (hfs : xs.tcbFault = none) + (hcaps : grant = false ∨ ipcBufOfA false xs = none ∨ + unat (dataToMessageInfo (xs.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0) + (hu : ∀ rp, ipcBufOfA true y2 = some rp → ∀ k, k < 128 → inUserFrame (bufAddr rp k) σ2) : + replyBodyProgramA st ar s2 a sender t fl4_abi.consts.fault.NullFault grant ⦃ r => ∃ σ4, + ((), σ4) ∈ ((fl4DoIpcTransfer (Z := Unit) (specPtr sender) none 0 grant (specPtr t) >>= fun _ => + setThreadState (specPtr t) .running) σ2).1 ∧ r.2 = a ∧ + ∃ Q R' x, SchedSRB st { Z with Q := Q, R := R' } m κ r.1 σ4 (specPtr t) ∧ + StEqA Z.T (L.mem r.1) σ4 ∧ σ4.kheap (specPtr t) = some (.tcb x) ∧ + x.tcbState = .running ∧ x.tcbFault = y2.tcbFault ∧ + (∀ d p, specPtr t ∉ σ4.readyQueues d p) ∧ specPtr t ∉ σ4.releaseQueue ∧ + WritesIn st s2 r.1 (ReplyW (st := st) N E R Bf Z m σ2 s2) ∧ SSzA σ2 σ4 ⦄ := by + refine replyNormalBody_refinesA (st := st) (ar := ar) (s := s2) (a := a) (σ := σ2) (sender := sender) + (t := t) (grant := grant) (M := fun s1 σ1 => + XferPostA (st := st) (L := L) Z.T E R Bf (IpcSchedRelA st Z m κ) s2 σ2 t y2 s1 σ1) + (R := fun s4 a4 σ4 => a4 = a ∧ + ∃ Q R' x, SchedSRB st { Z with Q := Q, R := R' } m κ s4 σ4 (specPtr t) ∧ + StEqA Z.T (L.mem s4) σ4 ∧ σ4.kheap (specPtr t) = some (.tcb x) ∧ + x.tcbState = .running ∧ x.tcbFault = y2.tcbFault ∧ + (∀ d p, specPtr t ∉ σ4.readyQueues d p) ∧ specPtr t ∉ σ4.releaseQueue ∧ + WritesIn st s2 s4 (ReplyW (st := st) N E R Bf Z m σ2 s2) ∧ SSzA σ2 σ4) hks hky hfs ?_ ?_ + · refine WP.spec_mono (do_ipc_transfer_anyA (ar := ar) (schedOpsEAOfPlacementA (st := st) (m := m) (κ := κ) P) + PE H2.place s2 a σ2 E2 ⟨Z.Q, Z.R, H2⟩ hSt hV hC hNative hs ht hne ((H2.thT sender).2 hs) + ((H2.thT t).2 ht) hks hky hu hoff 0#u64 0#u64 grant (Or.inl ⟨hfs, hcaps⟩) none) ?_ + rintro s1 ⟨σ1, he, hpost⟩ + have hm : ((), σ1) ∈ + (doIpcTransfer (Z := Unit) (specPtr sender) none (0#u64 : U64).bv grant (specPtr t) σ2).1 := by + rw [he] + exact Set.mem_singleton _ + exact ⟨σ1, hm, hpost⟩ + · rintro s3 σ3 ⟨E3, ⟨Q3, R3, H3⟩, -, w3, es3, -, x3, hx3, ex3⟩ + have hst3 : StEqA Z.T (L.mem s3) σ3 := fun u hu x hx => (E3.at_tcb hu hx).st + have hx3s : x3.tcbState = .inactive := (congrArg Tcb.tcbState ex3).trans hy2 + have hnq := H3.notQueued hx3 (by rw [hx3s]; rfl) + refine WP.spec_mono (set_thread_state_exAnyA H3 hst3 ht hx3 (ts := .running) rfl + (v := sched.TS_RUNNING) (by unfold sched.TS_RUNNING; rfl)) ?_ + rintro s4 ⟨σ4, he4, hk4, HB, hst4, ws⟩ + have e4 : σ4 = stsActA (updKSc σ3 (specPtr t) (.tcb { x3 with tcbState := .running })) (specPtr t) := + det_outA he4 (setThreadState_stsActExA hx3 _) + have hq4 : σ4.readyQueues = σ3.readyQueues ∧ σ4.releaseQueue = σ3.releaseQueue := by + rw [e4] + unfold stsActA + split <;> exact ⟨rfl, rfl⟩ + have hz23 : SSzA σ2 σ3 := ssz_of_epStep es3 + have hz34 : SSzA σ3 σ4 := by + intro p y n h + have hkh : σ4.kheap = Function.update σ3.kheap (specPtr t) (some (.tcb { x3 with tcbState := .running })) := by + rw [e4] + unfold stsActA + split <;> rfl + rw [hkh] at h + by_cases e : p = specPtr t + · subst e; rw [Function.update_self] at h; cases h + · rw [Function.update_of_ne e] at h; exact ⟨y, h⟩ + have w3' : WritesIn st s2 s3 (ReplyW (st := st) N E R Bf Z m σ2 s2) := + w3.mono fun b hb => replyNormalW_wA PE H2.toSchedSRN ht (Or.inl hb) + have ws' : WritesIn st s3 s4 (ReplyW (st := st) N E R Bf Z m σ3 s3) := + ws.mono fun b hb => ReplyW.zc (Z := Z) (Z' := { Z with Q := Q3, R := R3 }) rfl rfl + (replyNormalW_wA (Z := { Z with Q := Q3, R := R3 }) PE H3.toSchedSRN ht (Or.inr hb)) + refine ⟨σ4, by rw [he4]; exact Set.mem_singleton _, rfl, Q3, R3, { x3 with tcbState := .running }, + HB, hst4, by rw [hk4, updKSc_kheap, Function.update_self], rfl, + (by have h := congrArg Tcb.tcbFault ex3; exact h), fun d p => by rw [hq4.1]; exact hnq.1 d p, by rw [hq4.2]; exact hnq.2, + ReplyW.trans w3' hz23 ws', hz23.trans hz34⟩ + +end Store + +/-- What the SC tail needs at its start: the woken receiver's relation, its state words, its +TCB runnable or outside `inClassR`, in no spec queue, and the original fault tag. -/ +def ReplyTailReadyA {S : Type} (st : machine.Store S) [L : LawfulStore st] (Z : XSetsA) (m : McState) + (κ : U64 → U64) (t : U64) (s3 : S) (σ3 : State Unit) (ft : U64) (fault : Option Fault) : Prop := + ∃ Q R' x, SchedSRB st { Z with Q := Q, R := R' } m κ s3 σ3 (specPtr t) ∧ StEqA Z.T (L.mem s3) σ3 ∧ + σ3.kheap (specPtr t) = some (.tcb x) ∧ + (runnable x.tcbState = true ∨ ¬ inClassRSc x.tcbState x.tcbFault) ∧ + (∀ d p, specPtr t ∉ σ3.readyQueues d p) ∧ specPtr t ∉ σ3.releaseQueue ∧ + (ft != fl4_abi.consts.fault.Timeout) = !fault.elim false isTimeoutFault + +section Store2 +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + +/-- **Recipe step 3: the SC tail from `ReplyTailReadyA`.** [S] `tailS`: the receiver and its SC +on the current core (`hon` / `hcpu`); caller: its SC active, the actual timeout-handler test. +The FL4 timeout send is the caller's continuation (`timeout`, from `SchedSRB`). -/ +theorem replyTailJointA {s3 : S} {a3 : A} {σ3 : State Unit} {t ft : U64} {fault : Option Fault} + (ht : t ∈ Z.T) (hmb : Fl4MinBudgetAgreeA ar a3) + (hidle : ∀ c < fl4A64Platform.numNodes, specPtr t ≠ m.idleThreadOf c) + (tailS : ∀ (s4 : S) (σ4 : State Unit) Q4 R4 (x : Tcb), + SchedSRB st { Z with Q := Q4, R := R4 } m κ s4 σ4 (specPtr t) → + σ4.kheap (specPtr t) = some (.tcb x) → + L.mem s4 (affA t) = L.cpu s4 ∧ + (L.mem s4 (scA t) ≠ 0#u64 → L.mem s4 (scCoreA (L.mem s4 (scA t))) = L.cpu s4) ∧ + (∀ sc y n, L.mem s4 (scA t) = sc → sc ≠ 0#u64 → + σ4.kheap (scNameA m sc) = some (.schedContext y n) → scActive y = true) ∧ + faults.valid_timeout_handler st s4 t = ok (isEpCap x.tcbTimeoutHandler)) + {RT : S → A → State Unit → Prop} + (timeout : ∀ (s4 : S) (a4 : A) (σ4 : State Unit) Q4 R4 (x : Tcb) (sc : U64) (y : SchedContext) + (n : Nat) (badge : U64), + SchedSRB st { Z with Q := Q4, R := R4 } m κ s4 σ4 (specPtr t) → + σ4.kheap (specPtr t) = some (.tcb x) → L.mem s4 (scA t) = sc → sc ≠ 0#u64 → + σ4.kheap (scNameA m sc) = some (.schedContext y n) → badge.bv = y.scBadge → + isEpCap x.tcbTimeoutHandler = true → + faults.handle_timeout st ar s4 a4 t (.Timeout badge) ⦃ r => + ∃ σ', ((), σ') ∈ (fl4HandleTimeout (Z := Unit) (specPtr t) (.timeout badge.bv) σ4).1 ∧ + RT r.1 r.2 σ' ∧ WritesIn st s4 r.1 (ReplyW (st := st) N E R Bf Z m σ4 s4) ∧ SSzA σ4 σ' ⦄) + (T : ReplyTailReadyA st Z m κ t s3 σ3 ft fault) : + replyTailProgramA st ar s3 a3 t ft ⦃ out => ∃ σ4, + ((), σ4) ∈ (replyTailSpecA (specPtr t) fault σ3).1 ∧ + (IpcSchedRelA st Z m κ out.1 σ4 ∨ RT out.1 out.2 σ4) ∧ + WritesIn st s3 out.1 (ReplyW (st := st) N E R Bf Z m σ3 s3) ∧ SSzA σ3 σ4 ⦄ := by + obtain ⟨Q, R', x, HB, hst, hkx, hcls, hnqs, hnrel, hf⟩ := T + obtain ⟨hon, hcpu, hact, hv⟩ := tailS s3 σ3 Q R' x HB hkx + exact replyTailExA (R := fun s a σ => (IpcSchedRelA st Z m κ s σ ∨ RT s a σ) ∧ + WritesIn st s3 s (ReplyW (st := st) N E R Bf Z m σ3 s3) ∧ SSzA σ3 σ) + HB hst ht hon hmb fault hkx hcls hidle hact hv hf hcpu hnqs hnrel + (fun H => ⟨Or.inl ⟨Q, R', H⟩, WritesIn.refl _ _, SSzA.refl _⟩) + (fun _ _ Q' H' hk w => ⟨Or.inl ⟨Q', R', H'⟩, w.mono fun b hb => ReplyW.zc + (Z := Z) (Z' := { Z with Q := Q, R := R' }) rfl rfl + (replySWrA_wA (Z := { Z with Q := Q, R := R' }) (N := N) (E := E) (R := R) (Bf := Bf) (m := m) hb), + SSzA.of_kheap hk⟩) + (fun _ _ R'' H' hk _ w => ⟨Or.inl ⟨Q, R'', H'⟩, w.mono fun b hb => ReplyW.zc + (Z := Z) (Z' := { Z with Q := Q, R := R' }) rfl rfl + (replyPostponeW_wA (Z := { Z with Q := Q, R := R' }) (N := N) (E := E) (R := R) (Bf := Bf) + HB.toSchedSRN ht hb), SSzA.of_kheap hk⟩) + (fun sc y n badge h1 h2 h3 h4 h5 _ => + WP.spec_mono (timeout s3 a3 σ3 Q R' x sc y n badge HB hkx h1 h2 h3 h4 h5) + fun _ ⟨σ', hm, hR, hw, hz⟩ => ⟨σ', hm, Or.inr hR, hw, hz⟩) + +/-- **The live Reply joint producer** (recipe steps 1–5): `ipc.do_reply_transfer` at a live +reply whose receiver is `BlockedOnReply` refines FL4's `do_reply_transfer`, ending in the +scheduler relation or the caller's timeout-send relation. + +[S] `hlocal`, `unb`, `tailS` (locality scopes); no `hirqR` / `hirqU` premise (R-hirq: `IrqSlotsKeepA.of_replyFoot`) +(`IrqSlotsKeepA`, ruled 17:49Z, discharged at the Stage-3 merge); [P] `hmb` (the platform's +minimum budget). Caller-selected: the fault arm `faultArm` (payload and Restart/Inactive leaves +through `replyFaultBody_refinesA`), the timeout send `timeout`; caller premises: `hu` (the +receiver's IPC buffer frames), `hfs` / `hcaps` (a fault-free sender without an extra-cap insert; +the insert is the named Stage-3 obligation `IpcExtraCapTransferA`), `hidle`. -/ +theorem do_reply_transfer_jointA {s : S} {a : A} {σ : State Unit} {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {sender r t : U64} {grant : Bool} (hr : r ∈ R) (hs : sender ∈ Z.T) (ht : t ∈ Z.T) + (hne : sender ≠ t) + {xt xs : Tcb} {xr : Reply} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hkr : σ.kheap (specPtr r) = some (.reply xr)) (hb : xr.replyTcb = some (specPtr t)) + (hst : xt.tcbState = .blockedOnReply (specPtr r)) + (hks : σ.kheap (specPtr sender) = some (.tcb xs)) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) + (cov : ScReplyCovA Z.C m σ (specPtr r)) + (hsc0 : ∀ sc ∈ Z.C, (stack sc).head? = some r → sc ≠ 0#u64) + (hlocal : ∀ sc ∈ Z.C, (stack sc).head? = some r → xt.tcbSchedContext = none → + ReplyPopLocalA (st := st) (Z := Z) (m := m) s σ sc t) + (unb : ∀ s1 σ1 Q1 R1, SchedSRA st { Z with Q := Q1, R := R1 } m κ s1 σ1 → RemKeepA σ σ1 → + (L.mem s1 (scA t) ≠ 0#u64 → L.mem s1 (scCoreA (L.mem s1 (scA t))) = L.cpu s1) ∧ + (specPtr t ∈ σ1.releaseQueue → ∀ y n, + σ1.kheap (scNameA m (L.mem s1 (scA t))) = some (.schedContext y n) → + refillReady σ1.curTime (refillHd y) = false)) + (hoff : ∀ y, IsSlotA Z.th y → ¬ EWrite Z.T E R Bf y ∧ ¬ EWrite Z.T E R Bf (wa y 8#u64)) + (hSt : StateRelA Z.th (L.mem s) σ) (hV : SlotsValidA Z.th (L.mem s)) + (hC : BufCover st Z.T Bf s σ) (hNative : BufNativeA Z.T (L.mem s)) + (hfs : xs.tcbFault = none) + (hcaps : grant = false ∨ ipcBufOfA false xs = none ∨ + unat (dataToMessageInfo (xs.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0) + (hu : ∀ rp, ipcBufOfA true xt = some rp → ∀ k, k < 128 → inUserFrame (bufAddr rp k) σ) + (hmb : ∀ a', Fl4MinBudgetAgreeA ar a') + (hidle : ∀ c < fl4A64Platform.numNodes, specPtr t ≠ m.idleThreadOf c) + (tailS : ∀ (s4 : S) (σ4 : State Unit) Q4 R4 (x : Tcb), + SchedSRB st { Z with Q := Q4, R := R4 } m κ s4 σ4 (specPtr t) → + σ4.kheap (specPtr t) = some (.tcb x) → + L.mem s4 (affA t) = L.cpu s4 ∧ + (L.mem s4 (scA t) ≠ 0#u64 → L.mem s4 (scCoreA (L.mem s4 (scA t))) = L.cpu s4) ∧ + (∀ sc y n, L.mem s4 (scA t) = sc → sc ≠ 0#u64 → + σ4.kheap (scNameA m sc) = some (.schedContext y n) → scActive y = true) ∧ + faults.valid_timeout_handler st s4 t = ok (isEpCap x.tcbTimeoutHandler)) + {RT : S → A → State Unit → Prop} + (timeout : ∀ (s4 : S) (a4 : A) (σ4 : State Unit) Q4 R4 (x : Tcb) (sc : U64) (y : SchedContext) + (n : Nat) (badge : U64), + SchedSRB st { Z with Q := Q4, R := R4 } m κ s4 σ4 (specPtr t) → + σ4.kheap (specPtr t) = some (.tcb x) → L.mem s4 (scA t) = sc → sc ≠ 0#u64 → + σ4.kheap (scNameA m sc) = some (.schedContext y n) → badge.bv = y.scBadge → + isEpCap x.tcbTimeoutHandler = true → + faults.handle_timeout st ar s4 a4 t (.Timeout badge) ⦃ r => + ∃ σ', ((), σ') ∈ (fl4HandleTimeout (Z := Unit) (specPtr t) (.timeout badge.bv) σ4).1 ∧ + RT r.1 r.2 σ' ∧ WritesIn st s4 r.1 (ReplyW (st := st) N E R Bf Z m σ4 s4) ∧ SSzA σ4 σ' ⦄) + (faultArm : ∀ (s2 : S) (a1 : A) (σ2 : State Unit) (y2 : Tcb) (ft : U64) (f : Fault), + ReplyMidA st Z m κ E R Bf s σ r t s2 σ2 → σ2.kheap (specPtr t) = some (.tcb y2) → + ft = L.mem s2 (tFaultA t) → ft ≠ fl4_abi.consts.fault.NullFault → y2.tcbFault = some f → + replyBodyProgramA st ar s2 a1 sender t ft grant ⦃ out => ∃ σ3, + ((), σ3) ∈ (replyFaultBodyA (specPtr sender) (specPtr t) f σ2).1 ∧ + ReplyTailReadyA st Z m κ t out.1 σ3 ft (some f) ∧ + WritesIn st s2 out.1 (ReplyW (st := st) N E R Bf Z m σ2 s2) ∧ SSzA σ2 σ3 ⦄) : + ipc.do_reply_transfer st ar s a sender r grant ⦃ out => ∃ σ4, + ((), σ4) ∈ (fl4DoReplyTransfer (Z := Unit) (specPtr sender) (specPtr r) grant σ).1 ∧ + (IpcSchedRelA st Z m κ out.1 σ4 ∨ RT out.1 out.2 σ4) ∧ + WritesIn st s out.1 (ReplyW (st := st) N E R Bf Z m σ s) ∧ SSzA σ σ4 ⦄ := by + have hrt : xr.replyTcb = optRef (L.mem s (rTcbA r)) := by + obtain ⟨x, hx, h⟩ := HE.reply r hr + rw [hkr] at hx + cases hx + exact h + obtain ⟨hw0, hwt⟩ := optRef_some (hrt.symm.trans hb) + have hw : L.mem s (rTcbA r) = t := specPtr_inj hwt + have htr : obj.Reply.tcb st r s = ok t := + (eq_of_spec_fd (obj.Reply.tcb.spec (st := st) s r)).trans (by rw [hw]) + have hn : t ≠ 0#u64 := hw ▸ hw0 + have hrel : TcbRelE R (L.mem s) t xt := by + obtain ⟨x, hx, h⟩ := HE.tcb t ht + rw [hkt] at hx + cases hx + exact h + have hsw : L.mem s (stA t) = sched.TS_BLOCKED_ON_REPLY := by + apply UScalar.eq_of_val_eq + rw [hrel.st, hst] + unfold sched.TS_BLOCKED_ON_REPLY + rfl + have hsr : obj.Tcb.state st t s = ok sched.TS_BLOCKED_ON_REPLY := + (eq_of_spec_fd (P1.obj.Tcb.state.spec (st := st) s t)).trans (by rw [hsw]) + refine do_reply_transfer_liveA + (M1 := fun s1 a1 σ1 => a1 = a ∧ replyUnblockProgramA st s1 t ⦃ s2 => ∃ σ2, + (getTcbObjRef (Z := Unit) Tcb.tcbSchedContext (specPtr t) >>= + ifSporadicCurScTestRefillUnblockCheck) σ1 = ({((), σ2)}, False) ∧ + ReplyMidA st Z m κ E R Bf s σ r t s2 σ2 ⦄) + (M2 := fun s2 a1 σ2 => a1 = a ∧ ReplyMidA st Z m κ E R Bf s σ r t s2 σ2) + (M3 := fun s3 _ σ3 ft fault => ReplyTailReadyA st Z m κ t s3 σ3 ft fault ∧ + WritesIn st s s3 (ReplyW (st := st) N E R Bf Z m σ s) ∧ SSzA σ σ3) + (R := fun s' a' σ' => (IpcSchedRelA st Z m κ s' σ' ∨ RT s' a' σ') ∧ + WritesIn st s s' (ReplyW (st := st) N E R Bf Z m σ s) ∧ SSzA σ σ') + hkr hb hkt hst htr hn hsr ?_ ?_ ?_ ?_ + · refine WP.spec_mono (replyRemoveThenUnblockA a H P PE HE HS hr ht hkt hkr hb hst hq cov hsc0 hlocal + unb hoff hSt hV hC hNative) ?_ + rintro out ⟨σ1, h1, ha, hU⟩ + exact ⟨σ1, h1, ha, hU⟩ + · rintro s1 a1 σ1 ⟨ha, hU⟩ + refine WP.spec_mono hU ?_ + rintro s2 ⟨σ2, h2, hM⟩ + exact ⟨σ2, h2, ha, hM⟩ + · rintro s2 a1 σ2 ft ⟨-, hM⟩ hft + obtain ⟨Q1, R1, stack', H2, E2, S2, iso2, keep, hSt2, hV2, hC2, hN2, ⟨y2, hy2, hy2s⟩, wm, szm⟩ := id hM + have wm' : WritesIn st s s2 (ReplyW (st := st) N E R Bf Z m σ s) := + wm.mono fun b hb => replyFootW_wA PE hb + have hrel2 : TcbRelE R (L.mem s2) t y2 := by + obtain ⟨x, hx, h⟩ := E2.tcb t ht + rw [hy2] at hx + cases hx + exact h + by_cases h0 : ft = fl4_abi.consts.fault.NullFault + · have hf0 : y2.tcbFault = none := hrel2.fault0.1 (by rw [← hft, h0]; unfold fl4_abi.consts.fault.NullFault; rfl) + obtain ⟨ys, st1, sc1, hys, eys⟩ := keep.2.2 _ xs hks + obtain ⟨yt, st2, sc2, hyt, eyt⟩ := keep.2.2 _ xt hkt + have e2 : yt = y2 := by + rw [hy2] at hyt + exact (KernelObject.tcb.inj (Option.some.inj hyt)).symm + subst e2 + subst eys + have hu2 : ∀ rp, ipcBufOfA true yt = some rp → ∀ k, k < 128 → inUserFrame (bufAddr rp k) σ2 := by + intro rp hrp k hk + rw [eyt] at hrp + have hin := hu rp hrp k hk + unfold inUserFrame at hin ⊢ + obtain ⟨sz, hsz⟩ := hin + exact ⟨sz, keep.2.1 _ _ hsz (fun _ _ e => by cases e) (fun _ e => by cases e) (fun _ e => by cases e)⟩ + subst h0 + refine WP.spec_mono (replyNormalJointA (Z := { Z with Q := Q1, R := R1 }) H2 P PE E2 hSt2 hV2 hC2 hN2 + hoff hs ht hne hys hy2 hy2s hfs hcaps hu2) ?_ + rintro out ⟨σ4, hm, -, Q, R', x, HB, hst4, hk4, hx4, -, hnqs, hnrel, wn, szn⟩ + refine ⟨σ4, yt, hy2, Or.inl ⟨hf0, hm⟩, ⟨Q, R', x, HB, hst4, hk4, + Or.inl (by rw [hx4]; rfl), hnqs, hnrel, ?_⟩, + ReplyW.transZ (Z' := { Z with Q := Q1, R := R1 }) rfl rfl wm' szm wn, szm.trans szn⟩ + rw [hf0] + unfold fl4_abi.consts.fault.NullFault fl4_abi.consts.fault.Timeout + decide + · obtain ⟨f, hf⟩ : ∃ f, y2.tcbFault = some f := by + cases e : y2.tcbFault with + | none => exact absurd (by rw [hft, hrel2.fault0.2 e]; unfold fl4_abi.consts.fault.NullFault; rfl) h0 + | some f => exact ⟨f, rfl⟩ + refine WP.spec_mono (faultArm s2 a1 σ2 y2 ft f hM hy2 hft h0 hf) ?_ + rintro out ⟨σ3, hm, hT, wf, zf⟩ + exact ⟨σ3, y2, hy2, Or.inr ⟨f, hf, hm⟩, by rw [hf]; exact ⟨hT, ReplyW.trans wm' szm wf, szm.trans zf⟩⟩ + · rintro s3 a2 σ3 ft fault ⟨hT, wT, zT⟩ + refine WP.spec_mono (replyTailJointA ht (hmb a2) hidle tailS timeout hT) ?_ + rintro out ⟨σ4, hm, hR, wt, zt⟩ + exact ⟨σ4, hm, hR, ReplyW.trans wT zT wt, zT.trans zt⟩ + +end Store2 + +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/ReplyLiveSpecA.lean b/proofs/Fl4ProofsA64/P3/ReplyLiveSpecA.lean new file mode 100644 index 000000000..9b44b4eb7 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ReplyLiveSpecA.lean @@ -0,0 +1,65 @@ +/- +P3 (AArch64): spec facts of the live Reply path. + +* `setThreadState_memA`: every outcome of `set_thread_state t ts` has `t` a TCB in state `ts`. +* `replyUnlinkTcb_memA`, **`replyRemove_inactiveA`**: every outcome of l4v `reply_remove t r` + ends with `reply_unlink_tcb t r`, so `t` is `Inactive` there. + +No named premise. +-/ +import Fl4ProofsA64.P3.SpecFrA +import Fl4ProofsA64.P3.ReplyRemoveKeepA + +namespace Fl4ProofsA64.P3.Endp +open Fl4Spec Fl4SpecA64 Fl4ProofsA64.P3.Notif + +noncomputable section + +theorem setThreadState_memA {σ σ1 : State Unit} {t : ObjRef} {ts : ThreadState} + (h : ((), σ1) ∈ (setThreadState (Z := Unit) t ts σ).1) : + ∃ y, σ1.kheap t = some (.tcb y) ∧ y.tcbState = ts := by + unfold setThreadState at h + obtain ⟨x, hx, h2⟩ := mem_bindA.1 h + unfold threadSet at hx + obtain ⟨y, hy, hx2⟩ := mem_bindA.1 hx + change y ∈ (NonDet.assertOpt (getTcb t σ) σ).1 at hy + cases hg : getTcb t σ with + | none => rw [hg] at hy; exact absurd hy (Set.notMem_empty _) + | some tcb => + rw [hg] at hy + obtain rfl : y = (tcb, σ) := hy + obtain ⟨-, -, e⟩ := setObject_mem hx2 + rw [setThreadStateAct_eq] at h2 + have e1 : σ1 = stsAct x.2 t := congrArg Prod.snd (Set.mem_singleton_iff.mp h2) + refine ⟨{ tcb with tcbState := ts }, ?_, rfl⟩ + rw [e1, stsAct_kheap, e] + simp + +theorem replyUnlinkTcb_memA {σ σ1 : State Unit} {t r : ObjRef} + (h : ((), σ1) ∈ (replyUnlinkTcb (Z := Unit) t r σ).1) : + ∃ y, σ1.kheap t = some (.tcb y) ∧ y.tcbState = .inactive := by + unfold replyUnlinkTcb at h + obtain ⟨_, -, h⟩ := mem_bindA.1 h + obtain ⟨_, -, h⟩ := mem_bindA.1 h + obtain ⟨_, -, h⟩ := mem_bindA.1 h + obtain ⟨_, -, h⟩ := mem_bindA.1 h + obtain ⟨_, -, h⟩ := mem_bindA.1 h + exact setThreadState_memA h + +/-- **Every outcome of `reply_remove t r` has `t` `Inactive`.** -/ +theorem replyRemove_inactiveA {σ σ1 : State Unit} {t r : ObjRef} + (h : replyRemove (Z := Unit) t r σ = ({((), σ1)}, False)) : + ∃ y, σ1.kheap t = some (.tcb y) ∧ y.tcbState = .inactive := by + have hm : ((), σ1) ∈ (replyRemove (Z := Unit) t r σ).1 := by rw [h]; exact Set.mem_singleton _ + unfold replyRemove at hm + obtain ⟨_, -, hm⟩ := mem_bindA.1 hm + obtain ⟨_, -, hm⟩ := mem_bindA.1 hm + obtain ⟨⟨o, σ2⟩, -, hm⟩ := mem_bindA.1 hm + cases o with + | none => + repeat (first | exact replyUnlinkTcb_memA hm | (obtain ⟨_, -, hm⟩ := mem_bindA.1 hm)) + | some p => + repeat (first | exact replyUnlinkTcb_memA hm | (obtain ⟨_, -, hm⟩ := mem_bindA.1 hm)) + +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/ReplyNotIdleA.lean b/proofs/Fl4ProofsA64/P3/ReplyNotIdleA.lean new file mode 100644 index 000000000..8b73e6710 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ReplyNotIdleA.lean @@ -0,0 +1,32 @@ +/- +P3 (AArch64, delete hooks W3 Reply): **the SC at the head of a reply stack is no idle SC** under [S] +`numNodes = 1` (`NotIdleScA`, a premise of the donation). The only core is `m.curCore`, whose idle SC `scNameA` renames +to `idle_sc_ptr`; and the idle SC has an empty reply list (`SIdleA.f4`). + +No named premise beyond [S] `h1`. New module. +-/ +import Fl4ProofsA64.P3.DelNtfnRecA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +/-- **A SC object with a nonempty reply list is not idle** (module docstring). -/ +theorem notIdleSc_of_repliesA {σ : State Unit} {m : McState} {n : Nat} (I : SIdleA σ m n) {sc : U64} + (hcur : ∀ c < fl4A64Platform.numNodes, c = m.curCore) {y : SchedContext} {k : Nat} + (hk : σ.kheap (scNameA m sc) = some (.schedContext y k)) (hne : y.scReplies ≠ []) : + NotIdleScA m (scNameA m sc) := by + have h1 : scNameA m sc ≠ idleScPtr := fun e => hne (I.f4 y k (e ▸ hk)) + refine ⟨h1, fun c hc e => ?_⟩ + rw [hcur c hc] at e + unfold scNameA at e h1 + split_ifs at e h1 with g + · exact h1 rfl + · exact g e + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/ReplyPopFootA.lean b/proofs/Fl4ProofsA64/P3/ReplyPopFootA.lean new file mode 100644 index 000000000..23d5b0396 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ReplyPopFootA.lean @@ -0,0 +1,205 @@ +/- +P3 (AArch64): the concrete write footprint of the head reply pop. + +The pop writes the SC's reply word, the reply link words, the optional +donation's scheduler and SC words, and the final TCB unlink. Each step +uses the relational lemma the pop already proved to reach its state, so +the footprint holds under the same hypotheses as `reply_pop_relA`. +-/ +import Fl4ProofsA64.P3.ReplyRemoveFootA +import Fl4ProofsA64.P3.IpcScPopTopA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4SpecA64 Sched + +noncomputable section + +/-- The reply link words (`NEXT` and `PREV`) of the tracked replies. -/ +def RLinkW (R : Set U64) (b : U64) : Prop := ∃ q ∈ R, b = rNextA q ∨ b = rPrevA q + +private theorem popKeepsQueuesFA {σ : State Unit} {p r : ObjRef} {x : SchedContext} {n : Nat} : + (popStA σ p x n r).readyQueues = σ.readyQueues ∧ + (popStA σ p x n r).releaseQueue = σ.releaseQueue := by + have keep (s : State Unit) (q : ObjRef) (o : Option ObjRef) : + (rscA s q o).readyQueues = s.readyQueues ∧ (rscA s q o).releaseQueue = s.releaseQueue := by + rw [rscA_fields] + exact ⟨rfl, rfl⟩ + let rep := repStA σ p x n x.scReplies.tail + let fixed := fixHdA rep p x.scReplies.tail + have e : popStA σ p x n r = rscA fixed r none := rfl + have frame : fixed.readyQueues = σ.readyQueues ∧ fixed.releaseQueue = σ.releaseQueue := by + unfold fixed + cases h : x.scReplies.tail with + | nil => exact ⟨rfl, rfl⟩ + | cons d l => exact keep rep d (some p) + rw [e] + exact ⟨(keep fixed r none).1.trans frame.1, (keep fixed r none).2.trans frame.2⟩ + +section Store +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + +/-- The scheduler action word lies in the scheduler-word footprint. -/ +theorem schedAction_swrA {s : S} {σ : State Unit} (H : SchedSRA st Z m κ s σ) {b : U64} + (h : b = L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) : SWrA (st := st) Z s b := by + have hcpu : L.cpu s ∈ Z.Cs := H.sch.cpuCs + have ho : layout.node.SCHED_ACTION.val < 4288 := by unfold layout.node.SCHED_ACTION; decide + have := H.toSchedSRN.node_sks hcpu ho + left + rw [h] + exact this + +/-- All words a reply removal may write. -/ +def ReplyFootW (Z : XSetsA) (m : McState) (σ : State Unit) (s : S) (E R : Set U64) (Bf : Set (U64 × Word)) + (b : U64) : Prop := + EWrite Z.T E R Bf b ∨ MWrA (st := st) Z m σ s b ∨ RLinkW R b + +theorem popLocalAfterFA {s s' : S} {σ : State Unit} {sc r t : U64} {x : SchedContext} {n : Nat} + (H : SchedSRA st Z m κ s σ) + (H' : SchedSRA st Z m κ s' (popStA σ (scNameA m sc) x n (specPtr r))) + (hc : sc ∈ Z.C) (ht : t ∈ Z.T) (hsc0 : sc ≠ 0#u64) + (hk : σ.kheap (scNameA m sc) = some (.schedContext x n)) + (hv : ∀ f ∈ Z.T, L.mem s' (core.num.U64.wrapping_add f 3664#u64) = + L.mem s (core.num.U64.wrapping_add f 3664#u64)) + (hs : ReplyPopLocalA (st := st) (Z := Z) (m := m) s σ sc t) : + ReplyPopLocalA (st := st) (Z := Z) (m := m) s' (popStA σ (scNameA m sc) x n (specPtr r)) sc t := by + obtain ⟨ni, mig, src⟩ := hs + refine ⟨ni, by rw [H.aff_eq H' ht ht, H'.scCore sc hc, ← H.scCore sc hc]; exact mig, ?_⟩ + rcases src with unb | ⟨f, hf, fs, fon, vcpu, fq⟩ + · obtain ⟨y, k, hy, ry⟩ := H.scr.sc sc hc + rw [hk] at hy + cases hy + obtain ⟨z, j, hz, rz⟩ := H'.scr.sc sc hc + rw [popHeapScA] at hz + cases hz + have he := rz.tcb.symm.trans ry.tcb + rw [← scTcbA_eqSc, unb, optRefS_zero] at he + apply Or.inl + by_contra h + rw [optRefS, if_neg h] at he + cases he + · obtain ⟨xf, hxf, exf⟩ := H.sch.tcb f hf + obtain ⟨yf, hyf, eyf⟩ := H'.sch.tcb f hf + rw [popStA_keepsTcb (r := specPtr r) hk hxf] at hyf + cases hyf + have hfs : L.mem s' (scA f) = sc := by + apply optScA_eq_of (m := m) + · rw [← eyf, exf, fs] + · rw [optScA, if_neg hsc0] + exact fun h => ni.1 (Option.some.inj h) + obtain ⟨rq, rl⟩ := popKeepsQueuesFA (σ := σ) (p := scNameA m sc) (r := specPtr r) (x := x) (n := n) + exact Or.inr ⟨f, hf, hfs, by rw [H.aff_eq H' hf hf, H.cpu_eq H']; exact fon, (hv f hf).trans vcpu, + by rw [rq]; exact fq.1, by rw [rl]; exact fq.2⟩ + +/-- The prefix of the head pop writes the SC reply word and reply link words. -/ +theorem replyPopPrepFootA {s : S} {σ : State Unit} {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {sc r : U64} (hc : sc ∈ Z.C) {rest : List U64} (hl : stack sc = r :: rest) : + replyPopPrepareA (st := st) s r ⦃ s' => ∃ x n, σ.kheap (scNameA m sc) = some (.schedContext x n) ∧ + SSzA σ (popStA σ (scNameA m sc) x n (specPtr r)) ∧ + WritesIn st s s' (fun b => MWrA (st := st) Z m σ s b ∨ RLinkW R b) ⦄ := by + obtain ⟨ra, nx, pv⟩ := replyHeadWordsA H PE HS hc hl + obtain ⟨⟨x, n, hk, hL⟩, hm, hn, _⟩ := HS.stackRel sc hc + have hr : r ∈ R := hm r (by simp [hl]) + obtain ⟨y, hky, hy, hh, hp, ht⟩ := HS.popFacts PE HE hc (by rw [hl]; rfl) + have hPopp : ∀ x n, σ.kheap (scNameA m sc) = some (.schedContext x n) → + x.scReplies.tail.head? = optRefS (rest.headD 0#u64) := by + rw [← pv] + exact hp + unfold replyPopPrepareA + refine WP.spec_bind (obj.Reply.next.spec s r) ?_ + rintro a rfl + rw [nx] + refine WP.spec_bind (obj.Reply.prev.spec s r) ?_ + rintro a rfl + rw [pv, nextRawEq, bind_ok, ra] + refine WP.spec_bind (reply_pop_scA H hc hky hy hh hPopp ht) ?_ + rintro s1 ⟨z, k, hzk, hU, H1, w1, hsz⟩ + rw [hk] at hzk + cases hzk + have available : ∀ d ∈ rest, d ∈ R := fun d hd => hm d (by rw [hl]; exact List.mem_cons_of_mem _ hd) + have na := replyNextPlaceA (Z := Z) PE hr + by_cases hz0 : rest.headD 0#u64 = 0#u64 + · have hb : (rest.headD 0#u64 != 0#u64) = false := by rw [hz0]; exact bne_self_eq_false _ + simp only [hb, Bool.false_eq_true, ↓reduceIte, bind_ok] + rw [setNextZeroEq, WP.spec_ok] + refine ⟨x, n, hk, hsz, ?_⟩ + refine (w1.trans (writesIn_wr na.1)).mono fun b hb => ?_ + rcases hb with h | h + · exact Or.inl h + · exact Or.inr ⟨r, hr, Or.inl h⟩ + · have hprev : rest.headD 0#u64 ∈ R := by + cases rest with + | nil => exact absurd rfl hz0 + | cons d tl => exact available d (by simp) + have pa := replyNextPlaceA (Z := Z) PE hprev + have hb : (rest.headD 0#u64 != 0#u64) = true := bne_iff_ne.mpr hz0 + simp only [hb, ↓reduceIte, setNextRawEq, bind_ok] + rw [setNextZeroEq, WP.spec_ok] + refine ⟨x, n, hk, hsz, ?_⟩ + refine ((w1.trans (writesIn_wr pa.1)).trans (writesIn_wr na.1)).mono fun b hb => ?_ + rcases hb with (h | h) | h + · exact Or.inl h + · exact Or.inr ⟨_, hprev, Or.inl h⟩ + · exact Or.inr ⟨r, hr, Or.inl h⟩ + +/-- The optional SC donation writes at most the SC and scheduler words. -/ +theorem replyPopDonateFootA {s : S} {σ : State Unit} (a : A) + (H : SchedSRA st Z m κ s σ) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) {r sc t : U64} (hc : sc ∈ Z.C) (hsc0 : sc ≠ 0#u64) + (ht : t ∈ Z.T) {xt : Tcb} (hk : σ.kheap (specPtr t) = some (.tcb xt)) + {l : List ObjRef} (hh : l.head? = some (specPtr r)) + (hlocal : xt.tcbSchedContext = none → ReplyPopLocalA (st := st) (Z := Z) (m := m) s σ sc t) : + (if L.mem s (scA t) = 0#u64 then schedcontext.donate st ar s a sc t else ok (s, a)) ⦃ out => + WritesIn st s out.1 (MWrA (st := st) Z m σ s) ⦄ := by + obtain ⟨y, hy, hsct⟩ := H.sch.tcb t ht + rw [hk] at hy + cases hy + by_cases h0 : L.mem s (scA t) = 0#u64 + · have hxt : xt.tcbSchedContext = none := by rw [hsct, h0]; simp [optScA] + obtain ⟨hni, hmig, hfrom⟩ := hlocal hxt + rcases hfrom with hUnb | ⟨f, hf, hfs, hfon, hvc, hq⟩ + · refine WP.spec_mono (reply_pop_donUnbA H a hc ht hsc0 hUnb hni hmig hk hh) ?_ + rintro out ⟨-, σ', -, -, F⟩ + exact F.wr + · have hstall := stall_keepA (ar := ar) a (PE.tcb f hf) hfon hvc (WritesIn.refl (st := st) s (fun _ => False)) + (fun h => h) (fun h => h) + refine WP.spec_mono (reply_pop_donFromA H (popStateWords HE) a hc hsc0 hf hfs ht hfon hq.1 hq.2 hstall + hmig hni hk hh) ?_ + rintro out ⟨-, σ', Q', -, -, F⟩ + exact F.wr + · rw [if_neg h0] + exact (WP.spec_ok _).2 (WritesIn.refl _ _) + +/-- The final PREV clear and unlink write the reply words, the state word and the action word. -/ +theorem finishPoppedFootA {s : S} {σ : State Unit} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) {r t : U64} (hr : r ∈ R) (ht : t ∈ Z.T) + {x : Tcb} {xr : Reply} (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hkr : σ.kheap (specPtr r) = some (.reply xr)) + (hst : x.tcbState = .blockedOnReply (specPtr r) ∨ + ∃ ep d, x.tcbState = .blockedOnReceive ep (some (specPtr r)) d) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) : + (obj.Reply.set_prev st r s 0#u64 >>= fun s1 => ipc.reply_unlink st s1 r t) ⦃ s' => + WritesIn st s s' (fun b => EWrite Z.T E R Bf b ∨ + b = L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION ∨ b = rPrevA r) ⦄ := by + rw [setPrevZeroEq, Std.bind_tc_ok] + have pa := replyPrevPlaceA (Z := Z) PE hr + obtain ⟨H1, FE⟩ := writeLinkFrames (v := 0#u64) H P PE hr pa.1 (by omega) (by omega) + have wp : WritesIn st s (L.wr s (rPrevA r) 0#u64) (· = rPrevA r) := writesIn_wr pa.1 + refine WP.spec_mono (replyUnlinkFootA H1 P PE (HE.frame PE FE (KView.refl _)) ht hr hk hkr hst hq) ?_ + intro s' w + refine (wp.trans w).mono fun b hb => ?_ + rcases hb with h | h | h + · exact Or.inr (Or.inr h) + · exact Or.inl h + · right; left + rw [h, nodeAt_of_ksb wp.1, wp.2.1] + +end Store +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/ReplyRemoveAllFootA.lean b/proofs/Fl4ProofsA64/P3/ReplyRemoveAllFootA.lean new file mode 100644 index 000000000..55ced62b1 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ReplyRemoveAllFootA.lean @@ -0,0 +1,104 @@ +/- +P3 (AArch64): the concrete write footprint of `reply_remove` as one statement. + +Dispatch is the same as `reply_remove_relA`: a reply on no SC stack is a +detached splice, a reply at an SC's stack head is a pop, and a reply below +the head is a cut. The post is `WritesIn` over `ReplyFootW`, the endpoint +words, the MCS (SC and scheduler) words and the reply link words. The +hypotheses are those of `reply_remove_relA`. +-/ +import Fl4ProofsA64.P3.ReplyRemoveTopFootA +import Fl4ProofsA64.P3.ReplyRemoveKeepA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4SpecA64 Sched + +noncomputable section + +section Store +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + +/-- Head arm: `reply_remove` is `reply_pop` when the NEXT word's head bit is set. -/ +theorem replyRemoveHeadFootA {s : S} {σ : State Unit} (a : A) {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {sc r t : U64} (hc : sc ∈ Z.C) (hsc0 : sc ≠ 0#u64) (ht : t ∈ Z.T) + {rest : List U64} (hl : stack sc = r :: rest) + {xt : Tcb} {xr : Reply} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hkr : σ.kheap (specPtr r) = some (.reply xr)) (hb : xr.replyTcb = some (specPtr t)) + (hst : xt.tcbState = .blockedOnReply (specPtr r) ∨ + ∃ ep d, xt.tcbState = .blockedOnReceive ep (some (specPtr r)) d) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) + (cov : ScReplyCovA Z.C m σ (specPtr r)) + (hlocal : xt.tcbSchedContext = none → ReplyPopLocalA (st := st) (Z := Z) (m := m) s σ sc t) + (hpost : ∀ σ', ((replyUnlinkSc (Z := Unit) (scNameA m sc) (specPtr r)) >>= fun _ => + schedContextDonate (scNameA m sc) (specPtr t)) σ = ({((), σ')}, False) → RelIfBoundSc σ' (specPtr t)) : + ipc.reply_remove st ar s a r t ⦃ out => + WritesIn st s out.1 (ReplyFootW (st := st) Z m σ s E R Bf) ∧ ∃ σ', + replyRemove (Z := Unit) (specPtr t) (specPtr r) σ = ({((), σ')}, False) ∧ RemKeepA σ σ' ⦄ := by + obtain ⟨rwNext, decNext, _⟩ := replyHeadWordsA H PE HS hc hl + have nx := (eq_of_spec_fd (obj.Reply.next.spec (L := L) s r)).trans (congrArg ok decNext) + have hh := nx_head1 (L.mem s (rNextA r)) + rw [decNext, ← rwNext] at hh + rw [replyRemoveHeadProgramA (ar := ar) s a r t sc nx hsc0 hh] + obtain ⟨⟨x, n, hk, hL⟩, _⟩ := HS.stackRel sc hc + have hm : r ∈ stack sc := by simp [hl] + have hSome := replyRemoveSomeA hkr hb (HS.scWithReply_some hc hm cov) hk hkt + refine WP.spec_mono (replyPopFootA a H P PE HE HS hc hsc0 ht hl hkt hkr hb hst hq hlocal hpost) ?_ + rintro out ⟨w, σ', hpop, hkeep⟩ + refine ⟨w, σ', ?_, hkeep⟩ + rw [hSome, hL] + exact hpop + +/-- **`reply_remove` writes only `ReplyFootW`.** Same hypotheses as `reply_remove_relA`. -/ +theorem reply_remove_footA {s : S} {σ : State Unit} (a : A) {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {r t : U64} (hr : r ∈ R) (ht : t ∈ Z.T) + {xt : Tcb} {xr : Reply} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hkr : σ.kheap (specPtr r) = some (.reply xr)) (hb : xr.replyTcb = some (specPtr t)) + (hst : xt.tcbState = .blockedOnReply (specPtr r) ∨ + ∃ ep d, xt.tcbState = .blockedOnReceive ep (some (specPtr r)) d) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) + (cov : ScReplyCovA Z.C m σ (specPtr r)) + (hsc0 : ∀ sc ∈ Z.C, (stack sc).head? = some r → sc ≠ 0#u64) + (hlocal : ∀ sc ∈ Z.C, (stack sc).head? = some r → xt.tcbSchedContext = none → + ReplyPopLocalA (st := st) (Z := Z) (m := m) s σ sc t) + (hpost : ∀ sc ∈ Z.C, (stack sc).head? = some r → ∀ σ', + ((replyUnlinkSc (Z := Unit) (scNameA m sc) (specPtr r)) >>= fun _ => + schedContextDonate (scNameA m sc) (specPtr t)) σ = ({((), σ')}, False) → + RelIfBoundSc σ' (specPtr t)) : + ipc.reply_remove st ar s a r t ⦃ out => + WritesIn st s out.1 (ReplyFootW (st := st) Z m σ s E R Bf) ∧ ∃ σ', + replyRemove (Z := Unit) (specPtr t) (specPtr r) σ = ({((), σ')}, False) ∧ RemKeepA σ σ' ⦄ := by + classical + by_cases hd : ∀ sc ∈ Z.C, r ∉ stack sc + · exact replyRemoveDetachedFootA a H P PE HE HS hr ht hd hkt hkr hst hq hb cov + · have hLive : ∃ sc ∈ Z.C, r ∈ stack sc := by push_neg at hd; exact hd + obtain ⟨sc, hc, hm⟩ := hLive + obtain ⟨k, hk, he⟩ := List.getElem_of_mem hm + cases k with + | zero => + have hh : (stack sc).head? = some r := by + rw [List.head?_eq_getElem?, List.getElem?_eq_getElem hk, he] + obtain ⟨rest, hl⟩ : ∃ rest, stack sc = r :: rest := by + cases ee : stack sc with + | nil => rw [ee] at hh; cases hh + | cons d l => + rw [ee] at hh + cases hh + exact ⟨l, rfl⟩ + exact replyRemoveHeadFootA a H P PE HE HS hc (hsc0 sc hc hh) ht hl hkt hkr hb hst hq cov + (hlocal sc hc hh) (hpost sc hc hh) + | succ k => + subst he + exact replyRemoveCutFootA a H P PE HE HS hc ht hk hkt hkr hst hq hb cov + +end Store +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/ReplyRemoveBorA.lean b/proofs/Fl4ProofsA64/P3/ReplyRemoveBorA.lean new file mode 100644 index 000000000..cedd2e48e --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ReplyRemoveBorA.lean @@ -0,0 +1,554 @@ +/- +P3 (AArch64): reply removal of a receiver blocked on the reply, without the donation premise. + +The head-pop arm of `reply_remove_relA` / `reply_remove_footA` (and so +`replyRemoveUnblockJointA`) takes `hpost`: `RelIfBoundSc` of the receiver right after the +donation of the popped SC. It is needed only when the receiver is blocked on receive (in +`inClassR`); for the live Reply (the receiver is `BlockedOnReply`) the donated SC need not +be released (seL4 MCS `doReplyTransfer` tests it in the SC tail). These siblings take +`hcls : ¬ inClassRSc` of the receiver instead: the donation step then keeps `SchedSRA` +because `released_ipc_queues` says nothing of a TCB outside `inClassR` +(`SchedSRB.toA_ncA`). Statements are otherwise those of the originals (suffix `BorA`). + +[S] / [J] as the originals: `hlocal` (local donation scope), `unb` (refill-test locality), +`hirqR` / `hirqU` are gone (R-hirq, Architect 01:46Z): `IrqSlotsKeepA.of_replyFoot` on the footprints `w1` / `w2` computed here. +-/ +import Fl4ProofsA64.P3.IpcScRemoveTopA +import Fl4ProofsA64.P3.ReplyRemoveAllFootA +import Fl4ProofsA64.P3.ReplyXferFactsA +import Fl4ProofsA64.P3.IrqSlotsFootA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +/-- `SchedSRB` at `p` is `SchedSRA` when `p` is a TCB outside `inClassR`. -/ +theorem SchedSRB.toA_ncA {S : Type} {st : machine.Store S} [L : LawfulStore st] + {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} + {σ : Fl4SpecA64.State Unit} {p : Fl4SpecA64.ObjRef} + (H : SchedSRB st Z m κ s σ p) {y : Fl4SpecA64.Tcb} + (hy : σ.kheap p = some (.tcb y)) (hc : ¬ inClassRSc y.tcbState y.tcbFault) : + SchedSRA st Z m κ s σ := + ⟨H.toSchedSRN, fun u x hx hcx => by + by_cases e : u = p + · subst e + rw [hy] at hx + cases hx + exact absurd hcx hc + · exact H.rdEx u x e hx hcx⟩ + +end Fl4ProofsA64.P3.Sched + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4SpecA64 Sched + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +private theorem unboundDonationListsBorA {σ σ' : State Unit} {p t q : ObjRef} {x y : SchedContext} {n k : Nat} + {xt : Tcb} (hp : σ.kheap p = some (.schedContext x n)) (hx : x.scTcb = none) + (ht : σ.kheap t = some (.tcb xt)) + (h : schedContextDonate (Z := Unit) p t σ = ({((), σ')}, False)) + (hq : σ.kheap q = some (.schedContext y k)) : + ∃ y', σ'.kheap q = some (.schedContext y' k) ∧ y'.scReplies = y.scReplies := by + have e : σ' = donStA σ p t x n xt := by + have hs : ((), σ') ∈ (schedContextDonate (Z := Unit) p t σ).1 := by + rw [h] + exact Set.mem_singleton _ + rw [donate_specA hp ht hx] at hs + exact congrArg Prod.snd (Set.mem_singleton_iff.mp hs) + rw [e, donStA, updKSc_kheap, Function.update_of_ne (by intro e; rw [e, ht] at hq; cases hq), updKSc_kheap] + by_cases he : q = p + · subst q + rw [hp] at hq + cases hq + rw [Function.update_self] + exact ⟨_, rfl, rfl⟩ + · rw [Function.update_of_ne he] + exact ⟨y, hq, rfl⟩ + +private theorem reachedBorA {α : Type} {r : Result α} {P : α → Prop} (step : r ⦃ P ⦄) : + r ⦃ v => r = ok v ∧ P v ⦄ := by + obtain ⟨v, eq, post⟩ := spec_ex_fd step + rw [eq, WP.spec_ok] + exact ⟨rfl, post⟩ + +section Store +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + +private theorem popDonateFrameBorA {s : S} {σ : State Unit} {stack : U64 → List U64} + (H : SchedSRN st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + (HS : ReplyStackRelA (PopLinkMemA (L.mem s) r) R Z.C σ (scNameA m) stack) + (hr : r ∈ R) {s' : S} {σ' : State Unit} (F : MFrA (st := st) Z m s s' σ σ') + (hu : σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory) + (hsc : ∀ c ∈ Z.C, ∀ x n, σ.kheap (scNameA m c) = some (.schedContext x n) → + ∃ y k, σ'.kheap (scNameA m c) = some (.schedContext y k) ∧ y.scReplies = x.scReplies) : + EpStateRel Z.T E R Bf (L.mem s') σ' ∧ + ReplyStackRelA (PopLinkMemA (L.mem s') r) R Z.C σ' (scNameA m) stack ∧ + (∀ q ∈ R, (PopLinkMemA (L.mem s') r) (rNextA q) = (PopLinkMemA (L.mem s) r) (rNextA q) ∧ + (PopLinkMemA (L.mem s') r) (rPrevA q) = (PopLinkMemA (L.mem s) r) (rPrevA q)) := by + obtain ⟨FW, KW⟩ := epFrameOfMcsA H P F hu + have hm : ∀ q ∈ R, (PopLinkMemA (L.mem s') r) (rNextA q) = (PopLinkMemA (L.mem s) r) (rNextA q) ∧ + (PopLinkMemA (L.mem s') r) (rPrevA q) = (PopLinkMemA (L.mem s) r) (rPrevA q) := by + intro q hq + have hn := replyNextPlaceA (Z := Z) PE hq + have hp := replyPrevPlaceA (Z := Z) PE hq + have next := F.wr.mem_eq hn.1 (replyWordOffSchedA H P hq (by omega) (by omega)).2 + have prev := F.wr.mem_eq hp.1 (replyWordOffSchedA H P hq (by omega) (by omega)).2 + unfold PopLinkMemA + constructor + · by_cases e : rNextA q = rPrevA r + · simp [e] + · simp only [Function.update_of_ne e, next] + · by_cases e : rPrevA q = rPrevA r + · simp [e] + · simp only [Function.update_of_ne e, prev] + refine ⟨HE.frame PE FW KW, HS.frame hm hsc ?_, hm⟩ + intro q hq x hx + obtain ⟨y, hy, _⟩ := HE.reply q hq + have keep := F.keep.obj _ _ hy (fun _ _ eq => by cases eq) (fun _ eq => by cases eq) + exact ⟨y, hy, congrArg Reply.replySc (KernelObject.reply.inj (Option.some.inj (hx.symm.trans keep)))⟩ + +/-- The caller-SC test and optional donation, on the state after popping the SC's list. -/ +theorem replyPopDonateRefBorA {s : S} {σ : State Unit} (a : A) {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + (HS : ReplyStackRelA (PopLinkMemA (L.mem s) r) R Z.C σ (scNameA m) stack) + (hi : ReplyIsolatedA (PopLinkMemA (L.mem s) r) Z.C stack r) + {sc t : U64} (hc : sc ∈ Z.C) (hsc0 : sc ≠ 0#u64) (ht : t ∈ Z.T) (hr : r ∈ R) + {xt : Tcb} (hk : σ.kheap (specPtr t) = some (.tcb xt)) + {l : List ObjRef} (hh : l.head? = some (specPtr r)) + (hlocal : xt.tcbSchedContext = none → ReplyPopLocalA (st := st) (Z := Z) (m := m) s σ sc t) + (hcls : ¬ inClassRSc xt.tcbState xt.tcbFault) : + (if L.mem s (scA t) = 0#u64 then schedcontext.donate st ar s a sc t else ok (s, a)) ⦃ out => + out.2 = a ∧ ∃ σ' Q', + Fl4Spec.NonDet.when (the l.head? = specPtr r ∧ xt.tcbSchedContext = none) + (schedContextDonate (Z := Unit) (scNameA m sc) (specPtr t)) σ = ({((), σ')}, False) ∧ + SchedSRA st { Z with Q := Q' } m κ out.1 σ' ∧ + EpStateRel Z.T E R Bf (L.mem out.1) σ' ∧ + ReplyStackRelA (PopLinkMemA (L.mem out.1) r) R Z.C σ' (scNameA m) stack ∧ + ReplyIsolatedA (PopLinkMemA (L.mem out.1) r) Z.C stack r ∧ SKeepA σ σ' ⦄ := by + obtain ⟨y, hy, hsct⟩ := H.sch.tcb t ht + rw [hk] at hy + cases hy + by_cases h0 : L.mem s (scA t) = 0#u64 + · have hxt : xt.tcbSchedContext = none := by rw [hsct, h0]; simp [optScA] + obtain ⟨hni, hmig, hfrom⟩ := hlocal hxt + have hwhen : the l.head? = specPtr r ∧ xt.tcbSchedContext = none := + ⟨by rw [hh]; rfl, hxt⟩ + obtain ⟨x, n, hx, hxrel⟩ := H.scr.sc sc hc + have finish (out : S × A) (σ' : State Unit) (Q' : U64 → U64 → List U64) + (harch : out.2 = a) (HL : SchedSRB st { Z with Q := Q' } m κ out.1 σ' (specPtr t)) + (F : MFrA (st := st) Z m s out.1 σ σ') + (hD : schedContextDonate (Z := Unit) (scNameA m sc) (specPtr t) σ = ({((), σ')}, False)) + (scKeep : ∀ q y k, σ.kheap q = some (.schedContext y k) → + ∃ y', σ'.kheap q = some (.schedContext y' k) ∧ y'.scReplies = y.scReplies) : + out.2 = a ∧ ∃ σ' Q', + Fl4Spec.NonDet.when (the l.head? = specPtr r ∧ xt.tcbSchedContext = none) + (schedContextDonate (Z := Unit) (scNameA m sc) (specPtr t)) σ = ({((), σ')}, False) ∧ + SchedSRA st { Z with Q := Q' } m κ out.1 σ' ∧ EpStateRel Z.T E R Bf (L.mem out.1) σ' ∧ + ReplyStackRelA (PopLinkMemA (L.mem out.1) r) R Z.C σ' (scNameA m) stack ∧ + ReplyIsolatedA (PopLinkMemA (L.mem out.1) r) Z.C stack r ∧ SKeepA σ σ' := by + have hm : ((), σ') ∈ (schedContextDonate (Z := Unit) (scNameA m sc) (specPtr t) σ).1 := by + rw [hD] + exact Set.mem_singleton _ + have hu := schedContextDonateKeepsUserMemory _ _ _ _ hm + obtain ⟨HE', HS', frame⟩ := popDonateFrameBorA H.toSchedSRN P PE HE HS hr F hu (fun q _ y k hy => by + obtain ⟨y', hky', hL⟩ := scKeep (scNameA m q) y k hy + exact ⟨y', k, hky', hL⟩) + refine ⟨harch, σ', Q', ?_, (by obtain ⟨y, hy, o, rfl⟩ := F.keep.tcb _ _ hk; exact HL.toA_ncA hy hcls), HE', HS', ?_, F.keep⟩ + · unfold Fl4Spec.NonDet.when + rw [if_pos hwhen] + exact hD + · exact ⟨hi.1, (frame r hr).1.trans hi.2.1, (frame r hr).2.trans hi.2.2⟩ + rcases hfrom with hUnb | ⟨f, hf, hfs, hfon, hvc, hq⟩ + · refine WP.spec_mono (reply_pop_donUnbA H a hc ht hsc0 hUnb hni hmig hk hh) ?_ + rintro out ⟨harch, σ', hD, HB, F⟩ + unfold Fl4Spec.NonDet.when at hD + rw [if_pos hwhen] at hD + have hx0 : x.scTcb = none := by rw [hxrel.tcb, ← scTcbA_eqSc, hUnb, optRefS_zero] + exact finish out σ' Z.Q harch HB F hD (fun _ _ _ => unboundDonationListsBorA hx hx0 hk hD) + · have hstall := stall_keepA (ar := ar) a (PE.tcb f hf) hfon hvc (WritesIn.refl (st := st) s (fun _ => False)) + (fun h => h) (fun h => h) + refine WP.spec_mono (reply_pop_donFromA H (popStateWords HE) a hc hsc0 hf hfs ht hfon hq.1 hq.2 hstall + hmig hni hk hh) ?_ + rintro out ⟨harch, σ', Q', hD, HB, F⟩ + unfold Fl4Spec.NonDet.when at hD + rw [if_pos hwhen] at hD + obtain ⟨xf, hxf, esf⟩ := H.sch.tcb f hf + have eSc : xf.tcbSchedContext = some (scNameA m sc) := by rw [esf, hfs, optScA, if_neg hsc0]; rfl + obtain ⟨z, k, hz, zf⟩ := H.inv.back (specPtr f) xf _ hxf eSc + rw [hx] at hz + cases hz + obtain ⟨xf', hx', dom, _⟩ := H.rest.prio f hf + rw [hxf] at hx' + cases hx' + have hft : specPtr f ≠ specPtr t := fun e => hsc0 (by rw [← hfs, specPtr_inj e, h0]) + have hm : ((), σ') ∈ (schedContextDonate (Z := Unit) (scNameA m sc) (specPtr t) σ).1 := by + rw [hD] + exact Set.mem_singleton _ + exact finish out σ' Q' harch HB F hD (fun _ _ _ => donationKeepsReplyLists hx zf hxf hk hft dom hq hm) + · rw [if_neg h0, WP.spec_ok] + have hn : xt.tcbSchedContext ≠ none := by rw [hsct]; simp [optScA, h0] + refine ⟨rfl, σ, Z.Q, ?_, H, HE, HS, hi, SKeepA.refl _⟩ + unfold Fl4Spec.NonDet.when + rw [if_neg (fun e => hn e.2)] + rfl + +theorem reply_pop_relBorA {s : S} {σ : State Unit} (a : A) {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {sc r t : U64} (hc : sc ∈ Z.C) (hsc0 : sc ≠ 0#u64) (ht : t ∈ Z.T) + {rest : List U64} (hl : stack sc = r :: rest) + {xt : Tcb} {xr : Reply} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hkr : σ.kheap (specPtr r) = some (.reply xr)) (hb : xr.replyTcb = some (specPtr t)) + (hst : xt.tcbState = .blockedOnReply (specPtr r) ∨ + ∃ ep d, xt.tcbState = .blockedOnReceive ep (some (specPtr r)) d) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) + (hlocal : xt.tcbSchedContext = none → ReplyPopLocalA (st := st) (Z := Z) (m := m) s σ sc t) + (hcls : ¬ inClassRSc xt.tcbState xt.tcbFault) : + ipc.reply_pop st ar s a r t ⦃ out => out.2 = a ∧ ∃ σ', + replyPopSpecA (scNameA m sc) (specPtr t) (specPtr r) ((stack sc).map specPtr) xt.tcbSchedContext σ = + ({((), σ')}, False) ∧ + IpcSchedRelA st Z m κ out.1 σ' ∧ EpStateRel Z.T E R Bf (L.mem out.1) σ' ∧ + ReplyStackRelA (L.mem out.1) R Z.C σ' (scNameA m) (Function.update stack sc rest) ∧ + ReplyIsolatedA (L.mem out.1) Z.C (Function.update stack sc rest) r ⦄ := by + have hr := (HS.stackRel sc hc).2.1 r (by simp [hl]) + have nx : obj.Reply.next st r s = ok sc := + eq_of_spec_fd (WP.spec_mono (obj.Reply.next.spec s r) fun _ h => h.trans (replyHeadWordsA H PE HS hc hl).2.1) + rw [reply_pop_programA (ar := ar) s a r t sc nx] + refine WP.spec_bind (replyPopPrepareRefA H P PE HE HS hc hl) ?_ + rintro s1 ⟨x, n, hk, hU, H1, E1, S1, iso1, v1⟩ + let σ1 := popStA σ (scNameA m sc) x n (specPtr r) + have kt1 : σ1.kheap (specPtr t) = some (.tcb xt) := popStA_keepsTcb hk hkt + have h1 : ((stack sc).map specPtr).head? = some (specPtr r) := by rw [hl]; rfl + refine WP.spec_bind (obj.Tcb.sc.spec s1 t) ?_ + rintro i rfl + refine WP.spec_bind (replyPopDonateRefBorA (ar := ar) a H1 P PE E1 S1 iso1 hc hsc0 ht hr kt1 h1 + (fun hn => popLocalAfterFA H H1 hc ht hsc0 hk v1 (hlocal hn)) hcls) ?_ + rintro ⟨s2, a2⟩ ⟨ha, σ2, Q2, hD, H2, E2, S2, iso2, keep⟩ + obtain ⟨x2, kt2, c, ex2⟩ := keep.tcb _ _ kt1 + have kp := popStA_epView hk (specPtr r) + obtain ⟨y, kr1, yr⟩ := kp.2.1 _ _ hkr + have kr2 := keep.obj _ _ kr1 (fun _ _ e => (by cases e)) (fun _ e => (by cases e)) + have hq2 : ∀ e ∈ E, ∀ z, σ2.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue := by + intro e he z hz + obtain ⟨z0, hz0, _⟩ := HE.ep e he + have hz2 := keep.obj _ _ (kp.1 _ _ hz0) (fun _ _ e => (by cases e)) (fun _ e => (by cases e)) + rw [hz] at hz2 + cases hz2 + exact hq e he _ hz0 + refine WP.spec_bind (finishPoppedReplyA H2 P PE E2 hr ht S2 iso2 kt2 kr2 (yr.trans hb) + (by rw [ex2]; exact hst) hq2) ?_ + rintro s3 ⟨σ3, hF, H3, E3, S3, iso3⟩ + rw [WP.spec_ok] + refine ⟨ha, σ3, ?_, ⟨Q2, Z.R, H3⟩, E3, S3, iso3⟩ + unfold replyPopSpecA + rw [nd_bind_oneSc hU, nd_bind_oneSc hD] + exact hF + +private theorem replyRemoveAtHeadBorA {s : S} {σ : State Unit} (a : A) {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {sc r t : U64} (hc : sc ∈ Z.C) (hsc0 : sc ≠ 0#u64) (ht : t ∈ Z.T) + {rest : List U64} (hl : stack sc = r :: rest) + (cov : ScReplyCovA Z.C m σ (specPtr r)) + {xt : Tcb} {xr : Reply} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hkr : σ.kheap (specPtr r) = some (.reply xr)) (hb : xr.replyTcb = some (specPtr t)) + (hst : xt.tcbState = .blockedOnReply (specPtr r) ∨ + ∃ ep d, xt.tcbState = .blockedOnReceive ep (some (specPtr r)) d) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) + (hlocal : xt.tcbSchedContext = none → ReplyPopLocalA (st := st) (Z := Z) (m := m) s σ sc t) + (hcls : ¬ inClassRSc xt.tcbState xt.tcbFault) : + ipc.reply_remove st ar s a r t ⦃ out => out.2 = a ∧ ∃ σ', + replyRemove (Z := Unit) (specPtr t) (specPtr r) σ = ({((), σ')}, False) ∧ + IpcSchedRelA st Z m κ out.1 σ' ∧ EpStateRel Z.T E R Bf (L.mem out.1) σ' ∧ + ReplyStackRelA (L.mem out.1) R Z.C σ' (scNameA m) (Function.update stack sc rest) ∧ + ReplyIsolatedA (L.mem out.1) Z.C (Function.update stack sc rest) r ⦄ := by + obtain ⟨rwNext, decNext, _⟩ := replyHeadWordsA H PE HS hc hl + have nx := (eq_of_spec_fd (obj.Reply.next.spec (L := L) s r)).trans (congrArg ok decNext) + have hh := nx_head1 (L.mem s (rNextA r)) + rw [decNext, ← rwNext] at hh + rw [replyRemoveHeadProgramA (ar := ar) s a r t sc nx hsc0 hh] + obtain ⟨⟨x, n, hk, hL⟩, _⟩ := HS.stackRel sc hc + have hm : r ∈ stack sc := by simp [hl] + rw [replyRemoveSomeA hkr hb (HS.scWithReply_some hc hm cov) hk hkt, hL] + exact reply_pop_relBorA (ar := ar) a H P PE HE HS hc hsc0 ht hl hkt hkr hb hst hq hlocal hcls + +/-- Refine l4v reply_remove with the caller's [P] reply-specific SC coverage. +The head case takes the same [S] donation facts as reply_pop_relBorA. -/ +theorem reply_remove_relBorA {s : S} {σ : State Unit} (a : A) {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {r t : U64} (hr : r ∈ R) (ht : t ∈ Z.T) (cov : ScReplyCovA Z.C m σ (specPtr r)) + {xt : Tcb} {xr : Reply} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hkr : σ.kheap (specPtr r) = some (.reply xr)) (hb : xr.replyTcb = some (specPtr t)) + (hst : xt.tcbState = .blockedOnReply (specPtr r) ∨ + ∃ ep d, xt.tcbState = .blockedOnReceive ep (some (specPtr r)) d) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) + (hsc0 : ∀ sc ∈ Z.C, (stack sc).head? = some r → sc ≠ 0#u64) + (hlocal : ∀ sc ∈ Z.C, (stack sc).head? = some r → xt.tcbSchedContext = none → + ReplyPopLocalA (st := st) (Z := Z) (m := m) s σ sc t) + (hcls : ¬ inClassRSc xt.tcbState xt.tcbFault) : + ipc.reply_remove st ar s a r t ⦃ out => out.2 = a ∧ ∃ σ' stack', + replyRemove (Z := Unit) (specPtr t) (specPtr r) σ = ({((), σ')}, False) ∧ + IpcSchedRelA st Z m κ out.1 σ' ∧ EpStateRel Z.T E R Bf (L.mem out.1) σ' ∧ + ReplyStackRelA (L.mem out.1) R Z.C σ' (scNameA m) stack' ∧ + ReplyIsolatedA (L.mem out.1) Z.C stack' r ⦄ := by + classical + by_cases hd : ∀ sc ∈ Z.C, r ∉ stack sc + · refine WP.spec_mono (replyRemoveDetachedRefA (ar := ar) a ⟨Z.Q, Z.R, H⟩ P PE HE HS hr ht hd + cov hkt hkr hb hst hq) ?_ + rintro out ⟨ha, σ', hu, hS, hE, hR, hi⟩ + exact ⟨ha, σ', stack, hu, hS, hE, hR, hi⟩ + · have hLive : ∃ sc ∈ Z.C, r ∈ stack sc := by push_neg at hd; exact hd + obtain ⟨sc, hc, hm⟩ := hLive + obtain ⟨k, hk, he⟩ := List.getElem_of_mem hm + cases k with + | zero => + have hh : (stack sc).head? = some r := by + rw [List.head?_eq_getElem?, List.getElem?_eq_getElem hk, he] + obtain ⟨rest, hl⟩ : ∃ rest, stack sc = r :: rest := by + cases ee : stack sc with + | nil => rw [ee] at hh; cases hh + | cons d l => + rw [ee] at hh + cases hh + exact ⟨l, rfl⟩ + refine WP.spec_mono (replyRemoveAtHeadBorA (ar := ar) a H P PE HE HS hc (hsc0 sc hc hh) ht hl cov + hkt hkr hb hst hq (hlocal sc hc hh) hcls) ?_ + rintro out ⟨ha, σ', hu, hS, hE, hR, hi⟩ + exact ⟨ha, σ', Function.update stack sc rest, hu, hS, hE, hR, hi⟩ + | succ k => + refine WP.spec_mono (replyRemoveCutRefA (ar := ar) a ⟨Z.Q, Z.R, H⟩ P PE HE HS hc ht hk he cov + hkt hkr hb hst hq) ?_ + rintro out ⟨ha, σ', hu, hS, hE, hR, hi⟩ + exact ⟨ha, σ', Function.update stack sc ((stack sc).take (k + 1)), hu, hS, hE, hR, hi⟩ + +/-- Head arm: the pop footprint. -/ +theorem replyPopFootBorA {s : S} {σ : State Unit} (a : A) {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {sc r t : U64} (hc : sc ∈ Z.C) (hsc0 : sc ≠ 0#u64) (ht : t ∈ Z.T) + {rest : List U64} (hl : stack sc = r :: rest) + {xt : Tcb} {xr : Reply} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hkr : σ.kheap (specPtr r) = some (.reply xr)) (hb : xr.replyTcb = some (specPtr t)) + (hst : xt.tcbState = .blockedOnReply (specPtr r) ∨ + ∃ ep d, xt.tcbState = .blockedOnReceive ep (some (specPtr r)) d) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) + (hlocal : xt.tcbSchedContext = none → ReplyPopLocalA (st := st) (Z := Z) (m := m) s σ sc t) + (hcls : ¬ inClassRSc xt.tcbState xt.tcbFault) : + ipc.reply_pop st ar s a r t ⦃ out => + WritesIn st s out.1 (ReplyFootW (st := st) Z m σ s E R Bf) ∧ ∃ σ', + replyPopSpecA (scNameA m sc) (specPtr t) (specPtr r) ((stack sc).map specPtr) xt.tcbSchedContext σ = + ({((), σ')}, False) ∧ RemKeepA σ σ' ⦄ := by + have hr := (HS.stackRel sc hc).2.1 r (by simp [hl]) + have nx : obj.Reply.next st r s = ok sc := + eq_of_spec_fd (WP.spec_mono (obj.Reply.next.spec s r) fun _ h => h.trans (replyHeadWordsA H PE HS hc hl).2.1) + rw [reply_pop_programA (ar := ar) s a r t sc nx] + refine WP.spec_bind (WP.spec_and (replyPopPrepareRefA H P PE HE HS hc hl) + (replyPopPrepFootA H PE HE HS hc hl)) ?_ + rintro s1 ⟨⟨x, n, hk, hU, H1, E1, S1, iso1, v1⟩, ⟨x', n', hk', hsz, w1⟩⟩ + rw [hk] at hk' + cases hk' + let σ1 := popStA σ (scNameA m sc) x n (specPtr r) + have kt1 : σ1.kheap (specPtr t) = some (.tcb xt) := popStA_keepsTcb hk hkt + have h1 : ((stack sc).map specPtr).head? = some (specPtr r) := by rw [hl]; rfl + have local1 : xt.tcbSchedContext = none → ReplyPopLocalA (st := st) (Z := Z) (m := m) s1 σ1 sc t := + fun hn => popLocalAfterFA H H1 hc ht hsc0 hk v1 (hlocal hn) + refine WP.spec_bind (obj.Tcb.sc.spec s1 t) ?_ + rintro i rfl + refine WP.spec_bind (WP.spec_and (replyPopDonateRefBorA (ar := ar) a H1 P PE E1 S1 iso1 hc hsc0 ht hr kt1 h1 + local1 hcls) + (replyPopDonateFootA (ar := ar) a H1 PE E1 hc hsc0 ht kt1 h1 local1)) ?_ + rintro ⟨s2, a2⟩ ⟨⟨ha, σ2, Q2, hD, H2, E2, S2, iso2, keep⟩, w2⟩ + obtain ⟨x2, kt2, c, ex2⟩ := keep.tcb _ _ kt1 + have kp := popStA_epView hk (specPtr r) + obtain ⟨y, kr1, yr⟩ := kp.2.1 _ _ hkr + have kr2 := keep.obj _ _ kr1 (fun _ _ e => (by cases e)) (fun _ e => (by cases e)) + have hq2 : ∀ e ∈ E, ∀ z, σ2.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue := by + intro e he z hz + obtain ⟨z0, hz0, _⟩ := HE.ep e he + have hz2 := keep.obj _ _ (kp.1 _ _ hz0) (fun _ _ e => (by cases e)) (fun _ e => (by cases e)) + rw [hz] at hz2 + cases hz2 + exact hq e he _ hz0 + have hmem2 : σ2.machineState.underlyingMemory = σ1.machineState.underlyingMemory := by + unfold Fl4Spec.NonDet.when at hD + by_cases hc' : (the ((stack sc).map specPtr).head? = specPtr r ∧ xt.tcbSchedContext = none) + · rw [if_pos hc'] at hD + exact schedContextDonateKeepsUserMemory _ _ _ _ (by rw [hD]; exact Set.mem_singleton _) + · rw [if_neg hc'] at hD + have e : σ2 = σ1 := det_outA hD (show Fl4Spec.NonDet.pure () σ1 = ({((), σ1)}, False) from rfl) + rw [e] + have hst2 : x2.tcbState = .blockedOnReply (specPtr r) ∨ + ∃ ep d, x2.tcbState = .blockedOnReceive ep (some (specPtr r)) d := by rw [ex2]; exact hst + refine WP.spec_bind (WP.spec_and + (finishPoppedReplyA H2 P PE E2 hr ht S2 iso2 kt2 kr2 (yr.trans hb) hst2 hq2) + (finishPoppedFootA H2 P PE E2 hr ht kt2 kr2 hst2 hq2)) ?_ + rintro s3 ⟨⟨σ3, hF, -⟩, w3⟩ + have w12 := w1.trans (w2.mono fun _ h => h) + have w123 := w12.trans w3 + refine (WP.spec_ok _).2 ⟨?_, σ3, ?_, ?_⟩ + · refine w123.mono fun b hb => ?_ + rcases hb with ((h | h) | h) | h | h | h + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr h) + · exact Or.inr (Or.inl (mwr_back rfl rfl hsz w1.1 h)) + · exact Or.inl h + · exact Or.inr (Or.inl (Or.inr (schedAction_swrA H (nodeAt_cvtA w12 h)))) + · exact Or.inr (Or.inr ⟨r, hr, Or.inr h⟩) + · unfold replyPopSpecA + rw [nd_bind_oneSc hU, nd_bind_oneSc hD] + exact hF + · have eU := unlinkTcbSpecFA kt2 kr2 (yr.trans hb) hst2 + have e3 := det_outA hF eU + rw [e3] + exact ((RemKeepA.of_popSt hk).trans (RemKeepA.of_skeep keep hmem2)).trans (RemKeepA.of_unlink kt2 kr2) + +/-- Head arm: `reply_remove` is `reply_pop` when the NEXT word's head bit is set. -/ +theorem replyRemoveHeadFootBorA {s : S} {σ : State Unit} (a : A) {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {sc r t : U64} (hc : sc ∈ Z.C) (hsc0 : sc ≠ 0#u64) (ht : t ∈ Z.T) + {rest : List U64} (hl : stack sc = r :: rest) + {xt : Tcb} {xr : Reply} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hkr : σ.kheap (specPtr r) = some (.reply xr)) (hb : xr.replyTcb = some (specPtr t)) + (hst : xt.tcbState = .blockedOnReply (specPtr r) ∨ + ∃ ep d, xt.tcbState = .blockedOnReceive ep (some (specPtr r)) d) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) + (cov : ScReplyCovA Z.C m σ (specPtr r)) + (hlocal : xt.tcbSchedContext = none → ReplyPopLocalA (st := st) (Z := Z) (m := m) s σ sc t) + (hcls : ¬ inClassRSc xt.tcbState xt.tcbFault) : + ipc.reply_remove st ar s a r t ⦃ out => + WritesIn st s out.1 (ReplyFootW (st := st) Z m σ s E R Bf) ∧ ∃ σ', + replyRemove (Z := Unit) (specPtr t) (specPtr r) σ = ({((), σ')}, False) ∧ RemKeepA σ σ' ⦄ := by + obtain ⟨rwNext, decNext, _⟩ := replyHeadWordsA H PE HS hc hl + have nx := (eq_of_spec_fd (obj.Reply.next.spec (L := L) s r)).trans (congrArg ok decNext) + have hh := nx_head1 (L.mem s (rNextA r)) + rw [decNext, ← rwNext] at hh + rw [replyRemoveHeadProgramA (ar := ar) s a r t sc nx hsc0 hh] + obtain ⟨⟨x, n, hk, hL⟩, _⟩ := HS.stackRel sc hc + have hm : r ∈ stack sc := by simp [hl] + have hSome := replyRemoveSomeA hkr hb (HS.scWithReply_some hc hm cov) hk hkt + refine WP.spec_mono (replyPopFootBorA a H P PE HE HS hc hsc0 ht hl hkt hkr hb hst hq hlocal hcls) ?_ + rintro out ⟨w, σ', hpop, hkeep⟩ + refine ⟨w, σ', ?_, hkeep⟩ + rw [hSome, hL] + exact hpop + +/-- **`reply_remove` writes only `ReplyFootW`.** Same hypotheses as `reply_remove_relBorA`. -/ +theorem reply_remove_footBorA {s : S} {σ : State Unit} (a : A) {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {r t : U64} (hr : r ∈ R) (ht : t ∈ Z.T) + {xt : Tcb} {xr : Reply} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hkr : σ.kheap (specPtr r) = some (.reply xr)) (hb : xr.replyTcb = some (specPtr t)) + (hst : xt.tcbState = .blockedOnReply (specPtr r) ∨ + ∃ ep d, xt.tcbState = .blockedOnReceive ep (some (specPtr r)) d) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) + (cov : ScReplyCovA Z.C m σ (specPtr r)) + (hsc0 : ∀ sc ∈ Z.C, (stack sc).head? = some r → sc ≠ 0#u64) + (hlocal : ∀ sc ∈ Z.C, (stack sc).head? = some r → xt.tcbSchedContext = none → + ReplyPopLocalA (st := st) (Z := Z) (m := m) s σ sc t) + (hcls : ¬ inClassRSc xt.tcbState xt.tcbFault) : + ipc.reply_remove st ar s a r t ⦃ out => + WritesIn st s out.1 (ReplyFootW (st := st) Z m σ s E R Bf) ∧ ∃ σ', + replyRemove (Z := Unit) (specPtr t) (specPtr r) σ = ({((), σ')}, False) ∧ RemKeepA σ σ' ⦄ := by + classical + by_cases hd : ∀ sc ∈ Z.C, r ∉ stack sc + · exact replyRemoveDetachedFootA a H P PE HE HS hr ht hd hkt hkr hst hq hb cov + · have hLive : ∃ sc ∈ Z.C, r ∈ stack sc := by push_neg at hd; exact hd + obtain ⟨sc, hc, hm⟩ := hLive + obtain ⟨k, hk, he⟩ := List.getElem_of_mem hm + cases k with + | zero => + have hh : (stack sc).head? = some r := by + rw [List.head?_eq_getElem?, List.getElem?_eq_getElem hk, he] + obtain ⟨rest, hl⟩ : ∃ rest, stack sc = r :: rest := by + cases ee : stack sc with + | nil => rw [ee] at hh; cases hh + | cons d l => + rw [ee] at hh + cases hh + exact ⟨l, rfl⟩ + exact replyRemoveHeadFootBorA a H P PE HE HS hc (hsc0 sc hc hh) ht hl hkt hkr hb hst hq cov + (hlocal sc hc hh) hcls + | succ k => + subst he + exact replyRemoveCutFootA a H P PE HE HS hc ht hk hkt hkr hst hq hb cov + +theorem replyRemoveUnblockJointBorA {s : S} {σ : State Unit} (a : A) {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {r t : U64} (hr : r ∈ R) (ht : t ∈ Z.T) + {xt : Tcb} {xr : Reply} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hkr : σ.kheap (specPtr r) = some (.reply xr)) (hb : xr.replyTcb = some (specPtr t)) + (hst : xt.tcbState = .blockedOnReply (specPtr r) ∨ + ∃ ep d, xt.tcbState = .blockedOnReceive ep (some (specPtr r)) d) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) + (cov : ScReplyCovA Z.C m σ (specPtr r)) + (hsc0 : ∀ sc ∈ Z.C, (stack sc).head? = some r → sc ≠ 0#u64) + (hlocal : ∀ sc ∈ Z.C, (stack sc).head? = some r → xt.tcbSchedContext = none → + ReplyPopLocalA (st := st) (Z := Z) (m := m) s σ sc t) + (hcls : ¬ inClassRSc xt.tcbState xt.tcbFault) + (unb : ∀ s1 σ1 Q1 R1, SchedSRA st { Z with Q := Q1, R := R1 } m κ s1 σ1 → RemKeepA σ σ1 → + (L.mem s1 (scA t) ≠ 0#u64 → L.mem s1 (scCoreA (L.mem s1 (scA t))) = L.cpu s1) ∧ + (specPtr t ∈ σ1.releaseQueue → ∀ y n, + σ1.kheap (scNameA m (L.mem s1 (scA t))) = some (.schedContext y n) → + refillReady σ1.curTime (refillHd y) = false)) + (hoff : ∀ y, IsSlotA Z.th y → ¬ EWrite Z.T E R Bf y ∧ ¬ EWrite Z.T E R Bf (wa y 8#u64)) + (hSt : StateRelA Z.th (L.mem s) σ) (hV : SlotsValidA Z.th (L.mem s)) + (hC : BufCover st Z.T Bf s σ) (hNative : BufNativeA Z.T (L.mem s)) : + (do + let (s1, a1) ← ipc.reply_remove st ar s a r t + let s2 ← replyUnblockProgramA st s1 t + ok (s2, a1)) ⦃ out => ∃ Q1 R1 stack' σ1 σ2, + replyRemove (Z := Unit) (specPtr t) (specPtr r) σ = ({((), σ1)}, False) ∧ + (getTcbObjRef (Z := Unit) Tcb.tcbSchedContext (specPtr t) >>= + ifSporadicCurScTestRefillUnblockCheck) σ1 = ({((), σ2)}, False) ∧ + SchedSRA st { Z with Q := Q1, R := R1 } m κ out.1 σ2 ∧ + EpStateRel Z.T E R Bf (L.mem out.1) σ2 ∧ + ReplyStackRelA (L.mem out.1) R Z.C σ2 (scNameA m) stack' ∧ + ReplyIsolatedA (L.mem out.1) Z.C stack' r ∧ out.2 = a ∧ RemKeepA σ σ2 ∧ + StateRelA Z.th (L.mem out.1) σ2 ∧ SlotsValidA Z.th (L.mem out.1) ∧ + BufCover st Z.T Bf out.1 σ2 ∧ BufNativeA Z.T (L.mem out.1) ⦄ := by + have hTcb : ∀ u ∈ Z.T, ∃ x0, σ.kheap (specPtr u) = some (.tcb x0) := fun u hu => by + obtain ⟨x, hx, -⟩ := HE.tcb u hu + exact ⟨x, hx⟩ + refine WP.spec_bind (reachedBorA (WP.spec_and + (reply_remove_relBorA a H P PE HE HS hr ht cov hkt hkr hb hst hq hsc0 hlocal hcls) + (reply_remove_footBorA a H P PE HE HS hr ht hkt hkr hb hst hq cov hsc0 hlocal hcls))) ?_ + rintro ⟨s1, a1⟩ ⟨hreach, ⟨ha, σ1, stack', hu1, ⟨Q1, R1, H1⟩, E1, S1, iso1⟩, w1, σ1', hu1', keep1⟩ + have e1 : σ1' = σ1 := det_outA hu1' hu1 + subst e1 + have X1 := replyXferFactsA H.toSchedSRN PE P hoff w1 keep1 (Fl4ProofsA64.P3.Sched.IrqSlotsKeepA.of_replyFoot H.toSchedSRN PE P w1) hSt hV hC hNative hTcb + obtain ⟨hon, hrq⟩ := unb s1 σ1' Q1 R1 H1 keep1 + obtain ⟨x1, hk1, -⟩ := E1.tcb t ht + refine WP.spec_bind (reachedBorA (replyUnblockFrameA H1 P PE E1 S1 ht hr iso1 hk1 hon hrq)) ?_ + rintro s2 ⟨hreach2, σ2, hu2, H2, E2, S2', iso2, F, hcur, hmem, hsc⟩ + have keep2 : RemKeepA σ1' σ2 := RemKeepA.of_skeep F.keep hmem + have w2 : WritesIn st s1 s2 (ReplyFootW (st := st) { Z with Q := Q1, R := R1 } m σ1' s1 E R Bf) := + F.wr.mono fun b hb => Or.inr (Or.inl hb) + have hTcb1 : ∀ u ∈ Z.T, ∃ x0, σ1'.kheap (specPtr u) = some (.tcb x0) := fun u hu => by + obtain ⟨x, hx, -⟩ := E1.tcb u hu + exact ⟨x, hx⟩ + have X2 := replyXferFactsA (Z := { Z with Q := Q1, R := R1 }) H1.toSchedSRN PE P hoff w2 keep2 + (Fl4ProofsA64.P3.Sched.IrqSlotsKeepA.of_replyFoot H1.toSchedSRN PE P w2) X1.1 X1.2.1 X1.2.2.1 X1.2.2.2 hTcb1 + refine (WP.spec_ok _).2 ⟨Q1, R1, stack', σ1', σ2, hu1, hu2, H2, E2, S2', iso2, ha, + keep1.trans keep2, X2.1, X2.2.1, X2.2.2.1, X2.2.2.2⟩ + +end Store +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/ReplyRemoveFootA.lean b/proofs/Fl4ProofsA64/P3/ReplyRemoveFootA.lean new file mode 100644 index 000000000..e837983ba --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ReplyRemoveFootA.lean @@ -0,0 +1,79 @@ +/- +P3 (AArch64): concrete write footprints of the reply removal arms. + +The relational posts of `reply_remove_relA` carry no footprint. This module +states each arm's concrete `WritesIn` over the exact words it writes, so that +the next transfer can derive its slot, buffer and TCB-cap facts from the +pre-state. Nothing is assumed: each footprint uses the same hypotheses as +the matching relational lemma. +-/ +import Fl4ProofsA64.P3.IpcScPopA +import Fl4ProofsA64.P3.IpcScRemoveTopA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4SpecA64 Sched + +noncomputable section + +section Store +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + +/-- The unlink writes the reply's TCB word, the caller's state word, and at +most the current core's scheduler-action word. -/ +theorem replyUnlinkFootA {s : S} {σ : State Unit} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) {t r : U64} (ht : t ∈ Z.T) (hr : r ∈ R) + {x : Tcb} {xr : Reply} (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hkr : σ.kheap (specPtr r) = some (.reply xr)) + (hblock : x.tcbState = .blockedOnReply (specPtr r) ∨ + ∃ ep d, x.tcbState = .blockedOnReceive ep (some (specPtr r)) d) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) : + ipc.reply_unlink st s r t ⦃ s' => + WritesIn st s s' (fun b => EWrite Z.T E R Bf b ∨ + b = L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) ⦄ := by + have hrt : specPtr r ≠ specPtr t := by intro e; rw [e, hk] at hkr; cases hkr + have hta := PE.tcb t ht + have hra := (a_rtcb PE hr).1 + set σ1 := updK σ (specPtr r) (.reply { xr with replyTcb := none }) with e1 + set σ2 := updK σ1 (specPtr t) (.tcb { x with tcbState := .inactive }) with e2 + have hkt1 : σ1.kheap (specPtr t) = some (.tcb x) := by + rw [e1, updK_kheap, Function.update_of_ne hrt.symm, hk] + have hkt2 : σ2.kheap (specPtr t) = some (.tcb { x with tcbState := .inactive }) := by + rw [e2, updK_kheap, Function.update_self] + have hcase : runnable x.tcbState = false ∧ x.tcbState ≠ .idleThreadState := by + rcases hblock with h | ⟨ep, d, h⟩ <;> rw [h] <;> exact ⟨rfl, by intro e; cases e⟩ + unfold ipc.reply_unlink + refine WP.spec_bind (obj.Reply.set_tcb.spec s r 0#u64) ?_ + rintro s1 rfl + have he1 : EpStateRel Z.T E R Bf (L.mem (L.wr s (rTcbA r) 0#u64)) σ1 := + HE.upd_reply PE hr _ ((agree_wr (L := L) (s := s) (v := 0#u64) hra).mono fun b hb => by rw [rTcbA_val]; exact hb.2) + (by rw [rd_wr_eq hra, optRef_zero]) + have hw1 : WritesIn st s (L.wr s (rTcbA r) 0#u64) (EWrite Z.T E R Bf) := + (writesIn_wr hra).mono fun b hb => by subst b; exact Or.inr (Or.inl ⟨r, hr, rTcbA_val r⟩) + unfold sched.set_thread_state + refine WP.spec_bind (obj.Tcb.set_state.spec _ t sched.TS_INACTIVE) ?_ + rintro s2 rfl + obtain ⟨he2, hw2, hs2⟩ := write_state_rel PE _ σ1 he1 ht hkt1 + (notq_updR hq r { xr with replyTcb := none }) .inactive (by decide) sched.TS_INACTIVE + (by rw [ts_inactive]; rfl) + have hOnly : OnlyT σ σ2 (specPtr t) := + (OnlyT.refl σ _).updO _ (fun _ h => by cases h) + |>.updP _ + have hNB := NoBlockE.only hk hkt2 hOnly (Or.inr (Or.inr ⟨rfl, hcase⟩)) + have H2 := H.stable (WritesIn.ew hw1 hw2) (fun _ hb => endpWriteOffRead H.toSchedSRN P hb) + (endpSchedStep ((EpStep.reply hkr none).trans hs2)) hNB + refine WP.spec_mono (schedule_tcb_refinesA H2 t (popStateWords he2)) ?_ + rintro s3 ⟨hwa, -⟩ + have hw12 := WritesIn.ew hw1 hw2 + refine (hw12.trans hwa).mono fun b hb => ?_ + rcases hb with h | h + · exact Or.inl h + · right + rw [h, nodeAt_of_ksb hw12.1, hw12.2.1] + +end Store +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/ReplyRemoveFootQA.lean b/proofs/Fl4ProofsA64/P3/ReplyRemoveFootQA.lean new file mode 100644 index 000000000..ae2657aab --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ReplyRemoveFootQA.lean @@ -0,0 +1,420 @@ +/- +P3 (AArch64, delete hooks W3 Reply): **the write footprint of `reply_remove` with the SC holder queued or not** +(`reply_remove_footQA`) and a **precise** write set `RemWrA` (`ReplyFootW` lists the buffer and TCB field words, +too coarse for the delete loop's record): `reply_remove_footA`'s arms with `ReplyPopLocalQA` and no `hpost`. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.ReplyRemoveQA +import Fl4ProofsA64.P3.ReplyRemoveAllFootA +import Fl4ProofsA64.P3.CancelIpcFootA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4SpecA64 Sched + +noncomputable section + +section Store +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + +/-- The words `reply_remove` writes, precisely: reply links, the reply's `TCB` word, the caller's state word and the +scheduler words. -/ +def RemWrA (Z : XSetsA) (m : McState) (σ : State Unit) (s : S) (R : Set U64) (r t b : U64) : Prop := + RLinkW R b ∨ b = rTcbA r ∨ b = stA t ∨ MWrA (st := st) Z m σ s b + +theorem actAlignedA {s : S} {σ : State Unit} (H : SchedSRA st Z m κ s σ) : + Aligned (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) := + nodeAt_aligned (H.core.nodes _ H.sch.cpuCs) (by simp [layout.node.SCHED_ACTION]) + (by simp [layout.node.SCHED_ACTION]) + +/-- `reply_unlink`'s three words. -/ +theorem replyUnlinkPreciseA (s : S) {r t : U64} (hra : Aligned (rTcbA r)) (hst : Aligned (stA t)) + (hAct : Aligned (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION)) : + ipc.reply_unlink st s r t ⦃ s' => WritesIn st s s' + (fun b => b = rTcbA r ∨ b = stA t ∨ b = L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) ⦄ := by + unfold ipc.reply_unlink + refine WP.spec_bind (obj.Reply.set_tcb.spec s r 0#u64) ?_ + intro s1 hs1 + rw [hs1] + have w1 : WritesIn st s (L.wr s (rTcbA r) 0#u64) (· = rTcbA r) := writesIn_wr hra + have hA : L.nodeAt (L.wr s (rTcbA r) 0#u64) (L.cpu (L.wr s (rTcbA r) 0#u64)) layout.node.SCHED_ACTION = + L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION := by rw [nodeAt_of_ksb w1.1, w1.2.1] + refine WP.spec_mono (sts_footA _ t sched.TS_INACTIVE hst (by rw [hA]; exact hAct)) ?_ + intro s2 w2 + refine (w1.trans w2).mono fun b h => ?_ + rcases h with h | h | h + · exact Or.inl h + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr (h.trans hA)) + +theorem remW_of_arm {s : S} {σ : State Unit} (H : SchedSRA st Z m κ s σ) {r t b : U64} + (h : RLinkW R b ∨ b = rTcbA r ∨ b = stA t ∨ b = L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) : + RemWrA (st := st) Z m σ s R r t b := by + rcases h with h | h | h | h + · exact Or.inl h + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr (Or.inl h)) + · exact Or.inr (Or.inr (Or.inr (Or.inr (schedAction_swrA H h)))) + +theorem replySpliceArmPreciseA {s : S} {σ : State Unit} (a : A) + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) {r t nx pv : U64} (hr : r ∈ R) (ht : t ∈ Z.T) + (hn : nx ≠ 0#u64 → nx ∈ R) (hp : pv ≠ 0#u64 → pv ∈ R) : + (do + let s1 ← replySplicePrepareA (st := st) s r nx pv + let s2 ← ipc.reply_unlink st s1 r t + ok (s2, a)) ⦃ out => WritesIn st s out.1 (fun b => RLinkW R b ∨ b = rTcbA r ∨ b = stA t ∨ + b = L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) ⦄ := by + refine WP.spec_bind (replySplicePrepareFootA PE s hr hn hp) ?_ + intro s1 w1 + have hA : L.nodeAt s1 (L.cpu s1) layout.node.SCHED_ACTION = L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION := by + rw [nodeAt_of_ksb w1.1, w1.2.1] + refine WP.spec_bind (replyUnlinkPreciseA s1 (a_rtcb PE hr).1 (a_st (PE.tcb t ht)).1 + (by rw [hA]; exact actAlignedA H)) ?_ + intro s2 w2 + refine (WP.spec_ok _).2 ((w1.trans w2).mono fun b hb => ?_) + rcases hb with h | h | h | h + · exact Or.inl h + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr (Or.inl h)) + · exact Or.inr (Or.inr (Or.inr (h.trans hA))) + +theorem finishPoppedPreciseA {s : S} {σ : State Unit} (H : SchedSRA st Z m κ s σ) (PE : EPlaced Z.T E R Bf) + {r t : U64} (hr : r ∈ R) (ht : t ∈ Z.T) : + (obj.Reply.set_prev st r s 0#u64 >>= fun s1 => ipc.reply_unlink st s1 r t) ⦃ s' => + WritesIn st s s' (fun b => b = rPrevA r ∨ b = rTcbA r ∨ b = stA t ∨ + b = L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) ⦄ := by + rw [setPrevZeroEq, Std.bind_tc_ok] + have pa := replyPrevPlaceA (Z := Z) PE hr + have wp : WritesIn st s (L.wr s (rPrevA r) 0#u64) (· = rPrevA r) := writesIn_wr pa.1 + have hA : L.nodeAt (L.wr s (rPrevA r) 0#u64) (L.cpu (L.wr s (rPrevA r) 0#u64)) layout.node.SCHED_ACTION = + L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION := by rw [nodeAt_of_ksb wp.1, wp.2.1] + refine WP.spec_mono (replyUnlinkPreciseA _ (a_rtcb PE hr).1 (a_st (PE.tcb t ht)).1 + (by rw [hA]; exact actAlignedA H)) ?_ + intro s' w + refine (wp.trans w).mono fun b hb => ?_ + rcases hb with h | h | h | h + · exact Or.inl h + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr (Or.inl h)) + · exact Or.inr (Or.inr (Or.inr (h.trans hA))) + +/-- Detached splice arm. -/ +theorem replyRemoveDetachedPreciseA {s : S} {σ : State Unit} (a : A) {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {r t : U64} (hr : r ∈ R) (ht : t ∈ Z.T) (hd : ∀ sc ∈ Z.C, r ∉ stack sc) + {xt : Tcb} {xr : Reply} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hkr : σ.kheap (specPtr r) = some (.reply xr)) + (hst : xt.tcbState = .blockedOnReply (specPtr r) ∨ + ∃ ep d, xt.tcbState = .blockedOnReceive ep (some (specPtr r)) d) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) + (hb : xr.replyTcb = some (specPtr t)) (cov : ScReplyCovA Z.C m σ (specPtr r)) : + ipc.reply_remove st ar s a r t ⦃ out => + WritesIn st s out.1 (RemWrA (st := st) Z m σ s R r t) ∧ ∃ σ', + replyRemove (Z := Unit) (specPtr t) (specPtr r) σ = ({((), σ')}, False) ∧ RemKeepA σ σ' ⦄ := by + obtain ⟨hh, hn, hp⟩ := HS.detached r hr hd + have hnR : L.mem s (rNextA r) ≠ 0#u64 → L.mem s (rNextA r) ∈ R := fun h => (hn h).1 + have hpR : L.mem s (rPrevA r) ≠ 0#u64 → L.mem s (rPrevA r) ∈ R := fun h => (hp h).1 + have en := (eq_of_spec_fd (obj.Reply.next.spec (L := L) s r)).trans + (congrArg ok (replyMaskFA (Z := Z) PE _ hnR)) + have ep := (eq_of_spec_fd (obj.Reply.prev.spec (L := L) s r)).trans + (congrArg ok (replyMaskFA (Z := Z) PE _ hpR)) + rw [replyRemoveSpliceProgramA (ar := ar) s a r t _ _ en ep hh] + obtain ⟨σ2, hu, hkeep⟩ := replyUnlinkKeepA hkt hkr hb hst + refine WP.spec_mono (replySpliceArmPreciseA a H P PE HE hr ht hnR hpR) ?_ + intro out w + exact ⟨w.mono fun b hb' => remW_of_arm H hb', σ2, + (replyRemoveNoneA hkr hb (HS.scWithReply_none hd cov)).trans hu, hkeep⟩ + +private theorem cutReadNextPA {s : S} {l : List U64} {sc : U64} + (hW : ReplyStackWordsA (L.mem s) sc l) (hR : ∀ d ∈ l, d ∈ R) (PE : EPlaced Z.T E R Bf) + {k : Nat} (hk : k + 1 < l.length) : + obj.Reply.next st l[k + 1] s = ok l[k] ∧ headBit (L.mem s (rNextA l[k + 1])) = false := by + have hraw : L.mem s (rNextA l[k + 1]) = l.getD k 0#u64 := (hW (k + 1) hk).2 + rw [List.getD_eq_getElem _ _ (by omega)] at hraw + have he := replyPointerEvenA (Z := Z) PE (hR _ (List.getElem_mem (by omega : k < l.length))) + exact ⟨(eq_of_spec_fd (obj.Reply.next.spec s _)).trans (by rw [hraw, mask1_of_even he]), + by rw [hraw]; exact headBitEvenA he⟩ + +private theorem cutReadPrevPA {s : S} {l : List U64} {sc : U64} + (hW : ReplyStackWordsA (L.mem s) sc l) (hR : ∀ d ∈ l, d ∈ R) (PE : EPlaced Z.T E R Bf) + {k : Nat} (hk : k < l.length) : + obj.Reply.prev st l[k] s = ok (l.getD (k + 1) 0#u64) ∧ + (l.getD (k + 1) 0#u64 ≠ 0#u64 → l.getD (k + 1) 0#u64 ∈ R) := by + have hraw := (hW k hk).1 + have hpa : l.getD (k + 1) 0#u64 ≠ 0#u64 → l.getD (k + 1) 0#u64 ∈ R := by + intro h + by_cases hlt : k + 1 < l.length + · rw [List.getD_eq_getElem _ _ hlt] + exact hR _ (List.getElem_mem hlt) + · rw [List.getD_eq_default _ _ (by omega)] at h + exact (h rfl).elim + refine ⟨(eq_of_spec_fd (obj.Reply.prev.spec s _)).trans ?_, hpa⟩ + rw [hraw] + by_cases h : l.getD (k + 1) 0#u64 = 0#u64 + · rw [h, zero_or_masked] + · rw [mask1_of_even (replyPointerEvenA (Z := Z) PE (hpa h))] + +/-- Cut arm. -/ +theorem replyRemoveCutPreciseA {s : S} {σ : State Unit} (a : A) {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {sc t : U64} (hc : sc ∈ Z.C) (ht : t ∈ Z.T) + {k : Nat} (hk : k + 1 < (stack sc).length) + {xt : Tcb} {xr : Reply} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hkr : σ.kheap (specPtr (stack sc)[k + 1]) = some (.reply xr)) + (hst : xt.tcbState = .blockedOnReply (specPtr (stack sc)[k + 1]) ∨ + ∃ ep d, xt.tcbState = .blockedOnReceive ep (some (specPtr (stack sc)[k + 1])) d) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) + (hb : xr.replyTcb = some (specPtr t)) (cov : ScReplyCovA Z.C m σ (specPtr (stack sc)[k + 1])) : + ipc.reply_remove st ar s a (stack sc)[k + 1] t ⦃ out => + WritesIn st s out.1 (RemWrA (st := st) Z m σ s R (stack sc)[k + 1] t) ∧ ∃ σ', + replyRemove (Z := Unit) (specPtr t) (specPtr (stack sc)[k + 1]) σ = ({((), σ')}, False) ∧ + RemKeepA σ σ' ⦄ := by + obtain ⟨⟨x, n, hx, hL⟩, hR, hnd, hW⟩ := HS.stackRel sc hc + have hrM : (stack sc)[k + 1] ∈ stack sc := List.getElem_mem hk + have hrR := hR _ hrM + have hprevR : (stack sc)[k] ∈ R := hR _ (List.getElem_mem (by omega : k < (stack sc).length)) + have h0 : 0 < (stack sc).length := by omega + have hHead : (stack sc).head? = some (stack sc)[0] := by + rw [List.head?_eq_getElem?, List.getElem?_eq_getElem h0] + have hne : (stack sc)[0] ≠ (stack sc)[k + 1] := by + intro e + have j := (List.nodup_iff_injective_get.1 hnd) (a₁ := ⟨0, h0⟩) (a₂ := ⟨k + 1, hk⟩) + (by simpa using e) + exact Nat.ne_of_lt (Nat.zero_lt_succ k) (congrArg Fin.val j) + have hrs : xr.replySc = none := by + cases ho : xr.replySc with + | none => rfl + | some p => + obtain ⟨c, hc', _, hh⟩ := (HS.owner _ hrR xr hkr p).1 ho + have e := HS.unique c hc' sc hc _ (List.mem_of_mem_head? hh) hrM + subst c + exact (hne (Option.some.inj (hHead.symm.trans hh))).elim + have hh : x.scReplies.head? = some (specPtr (stack sc)[0]) := by rw [hL, List.head?_map, hHead]; rfl + have hnePtr : specPtr (stack sc)[0] ≠ specPtr (stack sc)[k + 1] := fun e => hne (specPtr_inj e) + obtain ⟨hU, HS1, F⟩ := reply_remove_cutA H hc hkr hrs hx hh hnePtr + let σ1 := repStA σ (scNameA m sc) x n (x.scReplies.takeWhile fun r => decide (r ≠ specPtr (stack sc)[k + 1])) + have hof q (he : q ≠ scNameA m sc) : σ1.kheap q = σ.kheap q := repStA_ne he + have kt1 : σ1.kheap (specPtr t) = some (.tcb xt) := by + rw [hof _ (by intro e; rw [e, hx] at hkt; cases hkt)] + exact hkt + have kr1 : σ1.kheap (specPtr (stack sc)[k + 1]) = some (.reply xr) := by + rw [hof _ (by intro e; rw [e, hx] at hkr; cases hkr)] + exact hkr + have hq1 : ∀ e ∈ E, ∀ z, σ1.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue := by + intro e he z hz + obtain ⟨z0, hz0, _⟩ := HE.ep e he + rw [hof _ (by intro e'; rw [e', hx] at hz0; cases hz0), hz0] at hz + cases hz + exact hq e he _ hz0 + have E1 := HE.frame PE (fun _ _ _ => rfl) (epFrameOfMcsA H.toSchedSRN P F rfl).2 + obtain ⟨en, eb⟩ := cutReadNextPA (Z := Z) hW hR PE hk + obtain ⟨ep, hp⟩ := cutReadPrevPA (Z := Z) hW hR PE hk + rw [replyRemoveSpliceProgramA (ar := ar) s a _ t _ _ en ep eb] + have heq : replyRemove (Z := Unit) (specPtr t) (specPtr (stack sc)[k + 1]) σ = + replyUnlinkTcb (Z := Unit) (specPtr t) (specPtr (stack sc)[k + 1]) σ1 := by + rw [replyRemoveSomeA hkr hb (HS.scWithReply_some hc hrM cov) hx hkt, cutPopSpecFA hh hnePtr hU] + obtain ⟨σ2, hu, hkeep⟩ := replyUnlinkKeepA kt1 kr1 hb hst + refine WP.spec_mono (replySpliceArmPreciseA a HS1 P PE E1 hrR ht (fun _ => hprevR) hp) ?_ + intro out w + refine ⟨w.mono fun b hb' => ?_, σ2, heq.trans hu, + (RemKeepA.of_repSt _ hx).trans hkeep⟩ + rcases hb' with h | h | h | h + · exact Or.inl h + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr (Or.inl h)) + · -- the action word of the state the splice starts from + exact Or.inr (Or.inr (Or.inr (Or.inr (schedAction_swrA H h)))) + +/-- The donation's writes, the holder queued or not: the scheduler words. -/ +theorem replyPopDonateFootQA {s : S} {σ : State Unit} (a : A) + (H : SchedSRA st Z m κ s σ) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) {r sc t : U64} (hc : sc ∈ Z.C) (hsc0 : sc ≠ 0#u64) + (ht : t ∈ Z.T) {xt : Tcb} (hk : σ.kheap (specPtr t) = some (.tcb xt)) + {l : List ObjRef} (hh : l.head? = some (specPtr r)) + (hlocal : xt.tcbSchedContext = none → ReplyPopDonQA (st := st) (ar := ar) (Z := Z) (m := m) a s σ sc t) : + (if L.mem s (scA t) = 0#u64 then schedcontext.donate st ar s a sc t else ok (s, a)) ⦃ out => + WritesIn st s out.1 (MWrA (st := st) Z m σ s) ⦄ := by + obtain ⟨y, hy, hsct⟩ := H.sch.tcb t ht + rw [hk] at hy + cases hy + by_cases h0 : L.mem s (scA t) = 0#u64 + · have hxt : xt.tcbSchedContext = none := by rw [hsct, h0]; simp [optScA] + obtain ⟨hni, hmig, hfrom⟩ := hlocal hxt + rcases hfrom with hUnb | ⟨f, hf, hfs, hfon, hstall⟩ + · refine WP.spec_mono (reply_pop_donUnbA H a hc ht hsc0 hUnb hni hmig hk hh) ?_ + rintro out ⟨-, σ', -, -, F⟩ + exact F.wr + · refine WP.spec_mono (reply_pop_donFromQA H (popStateWords HE) a hc hsc0 hf hfs ht hfon hstall + hmig hni hk hh) ?_ + rintro out ⟨-, σ', Q', R', -, -, F, -⟩ + exact F.wr + · rw [if_neg h0, WP.spec_ok] + exact WritesIn.refl _ _ + +theorem reply_pop_footQA {s : S} {σ : State Unit} (a : A) {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {sc r t : U64} (hc : sc ∈ Z.C) (hsc0 : sc ≠ 0#u64) (ht : t ∈ Z.T) + {rest : List U64} (hl : stack sc = r :: rest) + {xt : Tcb} {xr : Reply} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hkr : σ.kheap (specPtr r) = some (.reply xr)) (hb : xr.replyTcb = some (specPtr t)) + (hst : xt.tcbState = .blockedOnReply (specPtr r) ∨ + ∃ ep d, xt.tcbState = .blockedOnReceive ep (some (specPtr r)) d) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) + (hlocal : xt.tcbSchedContext = none → ReplyPopLocalQA (st := st) (ar := ar) (Z := Z) (m := m) a s σ sc t r) + (hncl : ¬ inClassRSc xt.tcbState xt.tcbFault) : + ipc.reply_pop st ar s a r t ⦃ out => + WritesIn st s out.1 (RemWrA (st := st) Z m σ s R r t) ∧ ∃ σ', + replyPopSpecA (scNameA m sc) (specPtr t) (specPtr r) ((stack sc).map specPtr) xt.tcbSchedContext σ = + ({((), σ')}, False) ∧ RemKeepA σ σ' ⦄ := by + have hr := (HS.stackRel sc hc).2.1 r (by simp [hl]) + have nx : obj.Reply.next st r s = ok sc := + eq_of_spec_fd (WP.spec_mono (obj.Reply.next.spec s r) fun _ h => h.trans (replyHeadWordsA H PE HS hc hl).2.1) + rw [reply_pop_programA (ar := ar) s a r t sc nx] + obtain ⟨s1, hrun, x, n, hk, hU, H1, E1, S1, iso1, v1⟩ := spec_existsA (replyPopPrepareRefA H P PE HE HS hc hl) + obtain ⟨s1', hrun', x', n', hk', hsz, w1⟩ := spec_existsA (replyPopPrepFootA (st := st) H PE HE HS hc hl) + rw [hrun] at hrun' + cases ok_inj_ff hrun' + rw [hrun, bind_ok] + rw [hk] at hk' + cases hk' + let σ1 := popStA σ (scNameA m sc) x n (specPtr r) + have kt1 : σ1.kheap (specPtr t) = some (.tcb xt) := popStA_keepsTcb hk hkt + have h1 : ((stack sc).map specPtr).head? = some (specPtr r) := by rw [hl]; rfl + have local1 : xt.tcbSchedContext = none → ReplyPopDonQA (st := st) (ar := ar) (Z := Z) (m := m) a s1 σ1 sc t := + fun hn => popDonAfterQA a H H1 hc ht hsc0 hk hrun (hlocal hn) + refine WP.spec_bind (obj.Tcb.sc.spec s1 t) ?_ + rintro i rfl + refine WP.spec_bind (WP.spec_and (replyPopDonateQA (ar := ar) a H1 P PE E1 S1 iso1 hc hsc0 ht hr kt1 h1 + local1 hncl) (replyPopDonateFootQA (ar := ar) a H1 PE E1 hc hsc0 ht kt1 h1 local1)) ?_ + rintro ⟨s2, a2⟩ ⟨⟨ha, σ2, Q2, R2, hD, H2, E2, S2, iso2, keep⟩, w2⟩ + obtain ⟨x2, kt2, c, ex2⟩ := keep.tcb _ _ kt1 + have kp := popStA_epView hk (specPtr r) + obtain ⟨y, kr1, yr⟩ := kp.2.1 _ _ hkr + have kr2 := keep.obj _ _ kr1 (fun _ _ e => (by cases e)) (fun _ e => (by cases e)) + have hq2 : ∀ e ∈ E, ∀ z, σ2.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue := by + intro e he z hz + obtain ⟨z0, hz0, _⟩ := HE.ep e he + have hz2 := keep.obj _ _ (kp.1 _ _ hz0) (fun _ _ e => (by cases e)) (fun _ e => (by cases e)) + rw [hz] at hz2 + cases hz2 + exact hq e he _ hz0 + have hmem2 : σ2.machineState.underlyingMemory = σ1.machineState.underlyingMemory := by + unfold Fl4Spec.NonDet.when at hD + by_cases hc' : (the ((stack sc).map specPtr).head? = specPtr r ∧ xt.tcbSchedContext = none) + · rw [if_pos hc'] at hD + exact schedContextDonateKeepsUserMemory _ _ _ _ (by rw [hD]; exact Set.mem_singleton _) + · rw [if_neg hc'] at hD + have e : σ2 = σ1 := det_outA hD (show Fl4Spec.NonDet.pure () σ1 = ({((), σ1)}, False) from rfl) + rw [e] + have hst2 : x2.tcbState = .blockedOnReply (specPtr r) ∨ + ∃ ep d, x2.tcbState = .blockedOnReceive ep (some (specPtr r)) d := by rw [ex2]; exact hst + refine WP.spec_bind (WP.spec_and + (finishPoppedReplyA H2 P PE E2 hr ht S2 iso2 kt2 kr2 (yr.trans hb) hst2 hq2) + (finishPoppedPreciseA H2 PE hr ht)) ?_ + rintro s3 ⟨⟨σ3, hF, -⟩, w3⟩ + have w12 := w1.trans (w2.mono fun _ h => h) + have w123 := w12.trans w3 + refine (WP.spec_ok _).2 ⟨?_, σ3, ?_, ?_⟩ + · refine w123.mono fun b hb => ?_ + rcases hb with ((h | h) | h) | h | h | h | h + · exact Or.inr (Or.inr (Or.inr h)) + · exact Or.inl h + · exact Or.inr (Or.inr (Or.inr (mwr_back rfl rfl hsz w1.1 h))) + · exact Or.inl ⟨r, hr, Or.inr h⟩ + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr (Or.inl h)) + · exact Or.inr (Or.inr (Or.inr (Or.inr (schedAction_swrA H (nodeAt_cvtA w12 h))))) + · unfold replyPopSpecA + rw [nd_bind_oneSc hU, nd_bind_oneSc hD] + exact hF + · have eU := unlinkTcbSpecFA kt2 kr2 (yr.trans hb) hst2 + have e3 := det_outA hF eU + rw [e3] + exact ((RemKeepA.of_popSt hk).trans (RemKeepA.of_skeep keep hmem2)).trans (RemKeepA.of_unlink kt2 kr2) + +/-- Head arm: `reply_remove` is `reply_pop` when the NEXT word's head bit is set. -/ +theorem replyRemoveHeadFootQA {s : S} {σ : State Unit} (a : A) {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {sc r t : U64} (hc : sc ∈ Z.C) (hsc0 : sc ≠ 0#u64) (ht : t ∈ Z.T) + {rest : List U64} (hl : stack sc = r :: rest) + {xt : Tcb} {xr : Reply} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hkr : σ.kheap (specPtr r) = some (.reply xr)) (hb : xr.replyTcb = some (specPtr t)) + (hst : xt.tcbState = .blockedOnReply (specPtr r) ∨ + ∃ ep d, xt.tcbState = .blockedOnReceive ep (some (specPtr r)) d) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) + (cov : ScReplyCovA Z.C m σ (specPtr r)) + (hlocal : xt.tcbSchedContext = none → ReplyPopLocalQA (st := st) (ar := ar) (Z := Z) (m := m) a s σ sc t r) + (hncl : ¬ inClassRSc xt.tcbState xt.tcbFault) : + ipc.reply_remove st ar s a r t ⦃ out => + WritesIn st s out.1 (RemWrA (st := st) Z m σ s R r t) ∧ ∃ σ', + replyRemove (Z := Unit) (specPtr t) (specPtr r) σ = ({((), σ')}, False) ∧ RemKeepA σ σ' ⦄ := by + obtain ⟨rwNext, decNext, _⟩ := replyHeadWordsA H PE HS hc hl + have nx := (eq_of_spec_fd (obj.Reply.next.spec (L := L) s r)).trans (congrArg ok decNext) + have hh := nx_head1 (L.mem s (rNextA r)) + rw [decNext, ← rwNext] at hh + rw [replyRemoveHeadProgramA (ar := ar) s a r t sc nx hsc0 hh] + obtain ⟨⟨x, n, hk, hL⟩, _⟩ := HS.stackRel sc hc + have hm : r ∈ stack sc := by simp [hl] + have hSome := replyRemoveSomeA hkr hb (HS.scWithReply_some hc hm cov) hk hkt + refine WP.spec_mono (reply_pop_footQA a H P PE HE HS hc hsc0 ht hl hkt hkr hb hst hq hlocal hncl) ?_ + rintro out ⟨w, σ', hpop, hkeep⟩ + refine ⟨w, σ', ?_, hkeep⟩ + rw [hSome, hL] + exact hpop + +/-- **`reply_remove` writes only `ReplyFootW`.** Same hypotheses as `reply_remove_relA`. -/ +theorem reply_remove_footQA {s : S} {σ : State Unit} (a : A) {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {r t : U64} (hr : r ∈ R) (ht : t ∈ Z.T) + {xt : Tcb} {xr : Reply} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hkr : σ.kheap (specPtr r) = some (.reply xr)) (hb : xr.replyTcb = some (specPtr t)) + (hst : xt.tcbState = .blockedOnReply (specPtr r) ∨ + ∃ ep d, xt.tcbState = .blockedOnReceive ep (some (specPtr r)) d) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) + (cov : ScReplyCovA Z.C m σ (specPtr r)) + (hsc0 : ∀ sc ∈ Z.C, (stack sc).head? = some r → sc ≠ 0#u64) + (hlocal : ∀ sc ∈ Z.C, (stack sc).head? = some r → xt.tcbSchedContext = none → + ReplyPopLocalQA (st := st) (ar := ar) (Z := Z) (m := m) a s σ sc t r) + (hncl : ¬ inClassRSc xt.tcbState xt.tcbFault) : + ipc.reply_remove st ar s a r t ⦃ out => + WritesIn st s out.1 (RemWrA (st := st) Z m σ s R r t) ∧ ∃ σ', + replyRemove (Z := Unit) (specPtr t) (specPtr r) σ = ({((), σ')}, False) ∧ RemKeepA σ σ' ⦄ := by + classical + by_cases hd : ∀ sc ∈ Z.C, r ∉ stack sc + · exact replyRemoveDetachedPreciseA a H P PE HE HS hr ht hd hkt hkr hst hq hb cov + · have hLive : ∃ sc ∈ Z.C, r ∈ stack sc := by push_neg at hd; exact hd + obtain ⟨sc, hc, hm⟩ := hLive + obtain ⟨k, hk, he⟩ := List.getElem_of_mem hm + cases k with + | zero => + have hh : (stack sc).head? = some r := by + rw [List.head?_eq_getElem?, List.getElem?_eq_getElem hk, he] + obtain ⟨rest, hl⟩ : ∃ rest, stack sc = r :: rest := by + cases ee : stack sc with + | nil => rw [ee] at hh; cases hh + | cons d l => + rw [ee] at hh + cases hh + exact ⟨l, rfl⟩ + exact replyRemoveHeadFootQA a H P PE HE HS hc (hsc0 sc hc hh) ht hl hkt hkr hb hst hq cov + (hlocal sc hc hh) hncl + | succ k => + subst he + exact replyRemoveCutPreciseA a H P PE HE HS hc ht hk hkt hkr hst hq hb cov + +end Store +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/ReplyRemoveInvA.lean b/proofs/Fl4ProofsA64/P3/ReplyRemoveInvA.lean new file mode 100644 index 000000000..0f26183d5 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ReplyRemoveInvA.lean @@ -0,0 +1,893 @@ +/- +P3 (AArch64, delete hooks W3 Reply): **`SInvXA ∧ SIdleA ∧ SActA` across the spec's `reply_remove`** +(`replyRemove_stepA`), on the spec definitions alone: the head pop donates the SC (the holder is dequeued, possibly +rescheduled), the cut and the unlink touch reply words and the caller's state. Also keeps every SC's `scYieldFrom`. + +* `DObjA`: object changes off the idle thread's SC binding and off `idle_sc_ptr`'s binding / replies. +* `LoopInvA.heapD`: the three invariants across such changes with queues only shrinking. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.DelEpLoopA +import Fl4ProofsA64.P3.DelRvVrA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open Fl4Spec Fl4SpecA64 Fl4ProofsA64.P3.Sched + +noncomputable section + +/-- The object changes of `reply_remove`, at pointer `p` with idle thread `i`. -/ +inductive DObjA (i p : ObjRef) : Option KernelObject → Option KernelObject → Prop + | same (o : Option KernelObject) : DObjA i p o o + | tcb (x y : Tcb) (hs : y.tcbState = x.tcbState) (hm : tcbCnodeMap y = tcbCnodeMap x) + (hv : y.tcbArch.tcbVcpu = x.tcbArch.tcbVcpu) + (hi : p = i → y.tcbSchedContext = x.tcbSchedContext) (hfl : y.tcbFault = x.tcbFault) : + DObjA i p (some (.tcb x)) (some (.tcb y)) + | sc (x y : SchedContext) (k : Nat) (hr : y.scRefillMax = x.scRefillMax) (hf : y.scRefills = x.scRefills) + (hi : p = idleScPtr → y.scTcb = x.scTcb ∧ y.scReplies = x.scReplies) + (hn : y.scTcb = x.scTcb ∨ y.scTcb ≠ some i) : + DObjA i p (some (.schedContext x k)) (some (.schedContext y k)) + | reply (a b : Reply) : DObjA i p (some (.reply a)) (some (.reply b)) + +/-- (record edit #8b-C2, item 11) A TCB newly bound to the current SC is in no release queue (or no time is +consumed): what `ZeroConsA` needs of a step that rebinds SCs. -/ +def NewScTcbA (σ σ' : State Unit) : Prop := + ∀ p x y, σ.kheap p = some (.tcb x) → σ'.kheap p = some (.tcb y) → y.tcbSchedContext = some σ.curSc → + x.tcbSchedContext = some σ.curSc ∨ p ∉ σ.releaseQueue ∨ σ.consumedTime = 0 + +theorem NewScTcbA.of_nt {σ σ' : State Unit} {p : ObjRef} (hk : ∀ q, q ≠ p → σ'.kheap q = σ.kheap q) + (hp : ∀ x, σ.kheap p ≠ some (.tcb x)) : NewScTcbA σ σ' := fun q x y hx hy hs => by + by_cases e : q = p + · subst e; exact absurd hx (hp x) + · rw [hk q e, hx] at hy + have := KernelObject.tcb.inj (Option.some.inj hy) + subst this + exact Or.inl hs + +theorem NewScTcbA.of_kheap {σ σ' : State Unit} (hk : σ'.kheap = σ.kheap) : NewScTcbA σ σ' := + fun q x y hx hy hs => by + rw [hk, hx] at hy + have := KernelObject.tcb.inj (Option.some.inj hy) + subst this + exact Or.inl hs + +/-- **The three invariants across object changes `DObjA`, queues only shrinking.** -/ +theorem LoopInvA.heapD {σ σ' : State Unit} {m : McState} {n : Nat} (h : LoopInvA σ m n) + (e : σ' = { σ with kheap := σ'.kheap, readyQueues := σ'.readyQueues, releaseQueue := σ'.releaseQueue, + reprogramTimer := σ'.reprogramTimer }) + (hq : ∀ d p y, y ∈ σ'.readyQueues d p → y ∈ σ.readyQueues d p) + (hl : ∀ y, y ∈ σ'.releaseQueue → y ∈ σ.releaseQueue) (hz : NewScTcbA σ σ') + (R : ∀ p, DObjA σ.idleThread p (σ.kheap p) (σ'.kheap p)) : LoopInvA σ' m n := by + have hcur : σ'.curThread = σ.curThread := by rw [e] + have hidle : σ'.idleThread = σ.idleThread := by rw [e] + have hcsc : σ'.curSc = σ.curSc := by rw [e] + have hact : σ'.schedulerAction = σ.schedulerAction := by rw [e] + have hnode : σ'.interruptIrqNode = σ.interruptIrqNode := by rw [e] + have tcbF : ∀ p x, σ.kheap p = some (.tcb x) → ∃ y, σ'.kheap p = some (.tcb y) ∧ y.tcbState = x.tcbState ∧ + (p = σ.idleThread → y.tcbSchedContext = x.tcbSchedContext) := fun p x hp => by + have hR := R p + rw [hp] at hR + generalize eb : σ'.kheap p = b at hR + cases hR with + | same => exact ⟨x, rfl, rfl, fun _ => rfl⟩ + | tcb _ y hs _ _ hi _ => exact ⟨y, rfl, hs, hi⟩ + have tcbB : ∀ p y, σ'.kheap p = some (.tcb y) → ∃ x, σ.kheap p = some (.tcb x) ∧ y.tcbState = x.tcbState ∧ + y.tcbFault = x.tcbFault := + fun p y hp => by + have hR := R p + rw [hp] at hR + generalize ea : σ.kheap p = a at hR + cases hR with + | same => exact ⟨y, rfl, rfl, rfl⟩ + | tcb x _ hs _ _ _ hf => exact ⟨x, rfl, hs, hf⟩ + have scF : ∀ p x k, σ.kheap p = some (.schedContext x k) → ∃ y, σ'.kheap p = some (.schedContext y k) ∧ + (p = idleScPtr → y.scTcb = x.scTcb ∧ y.scReplies = x.scReplies) := fun p x k hp => by + have hR := R p + rw [hp] at hR + generalize eb : σ'.kheap p = b at hR + cases hR with + | same => exact ⟨x, rfl, fun _ => ⟨rfl, rfl⟩⟩ + | sc _ y _ _ _ hi _ => exact ⟨y, rfl, hi⟩ + have scB : ∀ p y k, σ'.kheap p = some (.schedContext y k) → ∃ x, σ.kheap p = some (.schedContext x k) ∧ + y.scRefillMax = x.scRefillMax ∧ y.scRefills = x.scRefills ∧ + (p = idleScPtr → y.scReplies = x.scReplies) ∧ (y.scTcb = x.scTcb ∨ y.scTcb ≠ some σ.idleThread) := + fun p y k hp => by + have hR := R p + rw [hp] at hR + generalize ea : σ.kheap p = a at hR + cases hR with + | same => exact ⟨y, rfl, rfl, rfl, fun _ => rfl, Or.inl rfl⟩ + | sc x _ _ hr hf hi hn => exact ⟨x, rfl, hr, hf, fun h => (hi h).2, hn⟩ + have hct : ∀ k p, capTableAt k p σ' ↔ capTableAt k p σ := fun k p => by + have hR := R p + unfold capTableAt + generalize σ.kheap p = a at hR ⊢ + generalize σ'.kheap p = b at hR ⊢ + cases hR <;> simp + have capB : ∀ p o' b c, σ'.kheap p = some o' → capOf o' b = some c → + ∃ o, σ.kheap p = some o ∧ capOf o b = some c := fun p o' b c hp hc => by + have hR := R p + rw [hp] at hR + generalize ea : σ.kheap p = a at hR + cases hR with + | same => exact ⟨o', rfl, hc⟩ + | tcb x y _ hm _ _ _ => exact ⟨_, rfl, by simpa [capOf, hm] using hc⟩ + | sc => cases hc + | reply => cases hc + have specCap : ∀ sl, specCapAtA σ' sl = specCapAtA σ sl := fun sl => by + have hR := R sl.1 + unfold specCapAtA + generalize σ.kheap sl.1 = a at hR ⊢ + generalize σ'.kheap sl.1 = b at hR ⊢ + cases hR with + | same => rfl + | tcb x y _ hm _ _ _ => simp only [hm] + | sc => rfl + | reply => rfl + have idleVt : ∀ c < n, vtCapA σ' (m.idleThreadOf c) = some .nullCap ∧ vcpuOfA σ' (m.idleThreadOf c) = none := by + intro c hc + obtain ⟨x, hx, -⟩ := h.sidle.f3 c hc + have hR := R (m.idleThreadOf c) + rw [hx] at hR + generalize eb : σ'.kheap (m.idleThreadOf c) = b at hR + cases hR with + | same => exact idleVtSameA (by rw [eb, hx]) (h.sidle.idleSlot c hc) (h.sidle.idleBound c hc) + | tcb _ y _ hm hv _ _ => + exact idleVtKeepA hx eb (Option.some.inj (show some y.tcbVtable = some x.tcbVtable from + congrFun hm (tcbCnodeIndex 1))) hv (h.sidle.idleSlot c hc) (h.sidle.idleBound c hc) + have S := h.sinv + refine + { sinv := + { mc := S.mc + view := by unfold McView; rw [hidle]; exact S.view + curTcb := by + obtain ⟨x, hx⟩ := S.curTcb + obtain ⟨y, hy, -⟩ := tcbF _ _ hx + exact ⟨y, by rw [hcur]; exact hy⟩ + idleTcb := by + obtain ⟨x, hx, hs⟩ := S.idleTcb + obtain ⟨y, hy, es, -⟩ := tcbF _ _ hx + exact ⟨y, by rw [hidle]; exact hy, es.trans hs⟩ + onlyIdle := fun t y hy hs => by + obtain ⟨x, hx, es, -⟩ := tcbB t y hy + exact S.onlyIdle t x hx (es.symm.trans hs) + idleSc := by + obtain ⟨x, y, k, h1, h2, h3, h4⟩ := S.idleSc + obtain ⟨x', h1', -, ex⟩ := tcbF _ _ h1 + obtain ⟨y', h3', ey⟩ := scF _ _ _ h3 + exact ⟨x', y', k, by rw [hidle]; exact h1', (ex rfl).trans h2, h3', by rw [hidle]; exact (ey rfl).1.trans h4⟩ + curSc := by + obtain ⟨y, k, hy⟩ := S.curSc + obtain ⟨y', h', -⟩ := scF _ _ _ hy + exact ⟨y', k, by rw [hcsc]; exact h'⟩ + ctQ := fun hres d p hm => S.ctQ (hact.symm.trans hres) d p (by rw [hcur] at hm; exact hq _ _ _ hm) + idleQ := fun d p c hc hm => S.idleQ d p c hc (hq _ _ _ hm) + rq := fun d p t ht => by + obtain ⟨x, hx, hr⟩ := S.rq d p t (hq _ _ _ ht) + obtain ⟨y, hy, es, -⟩ := tcbF _ _ hx + exact ⟨y, hy, by rw [es]; exact hr⟩ + rls := fun t ht => by + obtain ⟨x, hx⟩ := S.rls t (hl _ ht) + obtain ⟨y, hy, -⟩ := tcbF _ _ hx + exact ⟨y, hy⟩ + rpos := fun p y k hy a => by + obtain ⟨x, hx, er, ef, -⟩ := scB p y k hy + rw [ef] + exact S.rpos p x k hx (by unfold scActive at a ⊢; rw [← er]; exact a) + zc := fun t y hy hs hr => by + obtain ⟨x, hx, -⟩ := tcbB t y hy + rw [hcsc] at hs + rw [show σ'.consumedTime = σ.consumedTime by rw [e]] + rcases hz t x y hx hy hs with e1 | e1 | e1 + · exact S.zc t x hx e1 (hl t hr) + · exact absurd (hl t hr) e1 + · exact e1 + rlsRun := fun t ht => by + obtain ⟨x, hx, hr⟩ := S.rlsRun t (hl _ ht) + obtain ⟨y, hy, es, -⟩ := tcbF _ _ hx + exact ⟨y, hy, by rw [es]; exact hr⟩ + curScReady := fun hc y k hy a => by + rw [show σ'.consumedTime = σ.consumedTime by rw [e]] at hc + rw [hcsc] at hy + obtain ⟨x, hx, er, ef, -⟩ := scB _ y k hy + rw [show σ'.curTime = σ.curTime by rw [e]] + have a0 : scActive x = true := by unfold scActive at a ⊢; rw [← er]; exact a + have := S.curScReady hc x k hx a0 + unfold refillHd at this ⊢ + rw [ef]; exact this + faultSt := fun t y hy f => by + obtain ⟨x, hx, es, ef⟩ := tcbB t y hy + rw [es]; exact S.faultSt t x hx (by rw [← ef]; exact f) + irqN := by + obtain ⟨⟨⟨hinj, hcn⟩, hdom⟩, hsl, href⟩ := S.irqN + refine ⟨⟨⟨?_, fun irq hi => ?_⟩, fun irq hi k hk => ?_⟩, fun irq hi o b c ho hc => ?_, + fun p o b c ho hc => ?_⟩ + · rw [hnode]; exact hinj + · rw [hnode]; exact (hct _ _).2 (hcn irq hi) + · rw [hnode] at hk; exact hdom irq hi k ((hct _ _).1 hk) + · rw [hnode] at ho + obtain ⟨o0, h0, hc0⟩ := capB _ _ _ _ ho hc + exact hsl irq hi o0 b c h0 hc0 + · rw [hnode] + obtain ⟨o0, h0, hc0⟩ := capB _ _ _ _ ho hc + exact href p o0 b c h0 hc0 } + sidle := + { f3 := fun c hc => by + obtain ⟨x, hx, hs⟩ := h.sidle.f3 c hc + obtain ⟨y, hy, es, -⟩ := tcbF _ _ hx + exact ⟨y, hy, es.trans hs⟩ + f4 := fun y k hy => by + obtain ⟨x, hx, -, -, er, -⟩ := scB _ y k hy + rw [er rfl]; exact h.sidle.f4 x k hx + f5 := fun p y k hy he => by + obtain ⟨x, hx, -, -, -, en⟩ := scB p y k hy + rw [hidle] at he + rcases en with en | en + · exact h.sidle.f5 p x k hx (en.symm.trans he) + · exact absurd he en + f6 := h.sidle.f6.of_eq specCap + f7 := fun y k hy => by + obtain ⟨x, hx, er, ef, -⟩ := scB _ y k hy + have a := h.sidle.f7 x k hx + have a1 := a.1 + unfold scActive at a1 ⊢ + unfold refillHd at a ⊢ + rw [er, ef, show σ'.curTime = σ.curTime by rw [e]] + exact ⟨a1, a.2⟩ + idleSlot := fun c hc => (idleVt c hc).1 + idleBound := fun c hc => (idleVt c hc).2 } + sact := + { f1 := fun u eu => by + rw [hact] at eu; rw [hcur, hidle]; exact h.sact.f1 u eu + f2 := by + obtain ⟨x, hx, hs⟩ := h.sact.f2 + obtain ⟨y, hy, es, -⟩ := tcbF _ _ hx + exact ⟨y, by rw [hcur]; exact hy, by rw [es]; exact hs⟩ } } + +/-- What one step of `reply_remove` keeps. -/ +structure RStepA (m : McState) (n : Nat) (σ σ' : State Unit) : Prop where + inv : LoopInvA σ' m n + ns : NoSwA σ → NoSwA σ' + cur : σ'.curThread = σ.curThread + idle : σ'.idleThread = σ.idleThread + st : ∀ q x, σ.kheap q = some (.tcb x) → ∃ y, σ'.kheap q = some (.tcb y) ∧ y.tcbState = x.tcbState + /-- (record edit #8b-C2, item 11: refills and their count kept, for `curScReady` / readiness at the resume.) -/ + sc : ∀ q x k, σ.kheap q = some (.schedContext x k) → + ∃ y, σ'.kheap q = some (.schedContext y k) ∧ y.scYieldFrom = x.scYieldFrom ∧ y.scRefills = x.scRefills ∧ + y.scRefillMax = x.scRefillMax + csc : σ'.curSc = σ.curSc + +theorem RStepA.refl {m : McState} {n : Nat} {σ : State Unit} (h : LoopInvA σ m n) : RStepA m n σ σ := + ⟨h, id, rfl, rfl, fun _ x hx => ⟨x, hx, rfl⟩, fun _ x _ hx => ⟨x, hx, rfl, rfl, rfl⟩, rfl⟩ + +theorem RStepA.trans {m : McState} {n : Nat} {σ σ1 σ2 : State Unit} (h1 : RStepA m n σ σ1) + (h2 : RStepA m n σ1 σ2) : RStepA m n σ σ2 := by + refine ⟨h2.inv, fun h => h2.ns (h1.ns h), h2.cur.trans h1.cur, h2.idle.trans h1.idle, fun q x hx => ?_, fun q x k hx => ?_, + h2.csc.trans h1.csc⟩ + · obtain ⟨y, hy, e⟩ := h1.st q x hx + obtain ⟨z, hz, e'⟩ := h2.st q y hy + exact ⟨z, hz, e'.trans e⟩ + · obtain ⟨y, hy, e, e2, e3⟩ := h1.sc q x k hx + obtain ⟨z, hz, e', e2', e3'⟩ := h2.sc q y k hy + exact ⟨z, hz, e'.trans e, e2'.trans e2, e3'.trans e3⟩ + +/-- A queue / action step that keeps the invariants keeps the heap. -/ +theorem RStepA.of_queue {m : McState} {n : Nat} {P : ObjRef → Prop} {σ σ' : State Unit} + (h : LoopInvA σ' m n) (Q : QueueE P σ σ') (hs : σ'.schedulerAction = .chooseNewThread) : + RStepA m n σ σ' := by + have e := Q.1 + have hk : σ'.kheap = σ.kheap := by rw [e] + exact ⟨h, (fun _ u c => by rw [hs] at c; cases c), by rw [e], by rw [e], fun _ x hx => ⟨x, by rw [hk]; exact hx, rfl⟩, + fun _ x _ hx => ⟨x, by rw [hk]; exact hx, rfl, rfl, rfl⟩, by rw [e]⟩ + +/-- One TCB's SC binding, off the idle thread. -/ +theorem RStepA.tcbSc {m : McState} {n : Nat} {σ : State Unit} (h : LoopInvA σ m n) {t : ObjRef} {x : Tcb} + (hk : σ.kheap t = some (.tcb x)) (ht : t ≠ σ.idleThread) (v : Option ObjRef) + (hz : v = some σ.curSc → t ∈ σ.releaseQueue → σ.consumedTime = 0) : + RStepA m n σ { σ with kheap := Function.update σ.kheap t (some (.tcb { x with tcbSchedContext := v })) } := by + refine ⟨h.heapD rfl (fun _ _ _ a => a) (fun _ a => a) (fun q x0 y0 hx0 hy0 hs0 => ?_) fun q => ?_, id, rfl, rfl, + fun q y hy => ?_, fun q y k hy => ?_, rfl⟩ + · by_cases e : q = t + · subst e + rw [hk] at hx0 + cases hx0 + change Function.update σ.kheap q _ q = _ at hy0 + rw [Function.update_self] at hy0 + have := KernelObject.tcb.inj (Option.some.inj hy0) + subst this + by_cases hq : q ∈ σ.releaseQueue + · exact Or.inr (Or.inr (hz hs0 hq)) + · exact Or.inr (Or.inl hq) + · change Function.update σ.kheap t _ q = _ at hy0 + rw [Function.update_of_ne e, hx0] at hy0 + have := KernelObject.tcb.inj (Option.some.inj hy0) + subst this + exact Or.inl hs0 + · show DObjA σ.idleThread q (σ.kheap q) (Function.update σ.kheap t _ q) + by_cases e : q = t + · subst e + rw [Function.update_self, hk] + exact .tcb _ _ rfl rfl rfl (fun e => absurd e ht) rfl + · rw [Function.update_of_ne e]; exact .same _ + · show ∃ z, Function.update σ.kheap t _ q = some (.tcb z) ∧ _ + by_cases e : q = t + · subst e + rw [hk] at hy + cases hy + exact ⟨_, Function.update_self _ _ _, rfl⟩ + · exact ⟨y, by rw [Function.update_of_ne e]; exact hy, rfl⟩ + · have e : q ≠ t := fun e => by rw [e, hk] at hy; cases hy + exact ⟨y, by show Function.update σ.kheap t _ q = _; rw [Function.update_of_ne e]; exact hy, rfl, rfl, rfl⟩ + +/-- One SC off `idle_sc_ptr`: the binding (not to the idle thread) and the replies. -/ +theorem RStepA.updSc {m : McState} {n : Nat} {σ : State Unit} (h : LoopInvA σ m n) {p : ObjRef} + {x y : SchedContext} {k : Nat} (hk : σ.kheap p = some (.schedContext x k)) (hp : p ≠ idleScPtr) + (hr : y.scRefillMax = x.scRefillMax) (hf : y.scRefills = x.scRefills) (hy : y.scYieldFrom = x.scYieldFrom) + (hn : y.scTcb = x.scTcb ∨ y.scTcb ≠ some σ.idleThread) : + RStepA m n σ { σ with kheap := Function.update σ.kheap p (some (.schedContext y k)) } := by + refine ⟨h.heapD rfl (fun _ _ _ a => a) (fun _ a => a) (NewScTcbA.of_nt (p := p) + (fun q e => Function.update_of_ne e _ _) (fun x0 e => by rw [hk] at e; cases e)) fun q => ?_, id, rfl, rfl, fun q z hz => ?_, fun q z j hz => ?_, rfl⟩ + · show DObjA σ.idleThread q (σ.kheap q) (Function.update σ.kheap p _ q) + by_cases e : q = p + · subst e + rw [Function.update_self, hk] + exact .sc _ _ _ hr hf (fun e => absurd e hp) hn + · rw [Function.update_of_ne e]; exact .same _ + · have e : q ≠ p := fun e => by rw [e, hk] at hz; cases hz + exact ⟨z, by show Function.update σ.kheap p _ q = _; rw [Function.update_of_ne e]; exact hz, rfl⟩ + · show ∃ w, Function.update σ.kheap p _ q = some (.schedContext w j) ∧ _ + by_cases e : q = p + · subst e + rw [hk] at hz + cases hz + exact ⟨_, Function.update_self _ _ _, hy, hf, hr⟩ + · exact ⟨z, by rw [Function.update_of_ne e]; exact hz, rfl, rfl, rfl⟩ + +theorem getReply_eqPres (p : ObjRef) : PresR (@Eq (State Unit)) (getReply (Z := Unit) p) := by + show PresR (@Eq (State Unit)) (getSimpleKo KernelObject.reply p) + unfold getSimpleKo + refine PresR.bind (getObject_eqPres p) (fun _ => ?_) + eqpres + +/-- Writing a reply object. -/ +theorem setReply_stepA {m : McState} {n : Nat} (p : ObjRef) (y' : Reply) {σ : State Unit} (h : LoopInvA σ m n) + {r : Unit × State Unit} (hr : r ∈ (setSimpleKo (Z := Unit) KernelObject.reply p y' σ).1) : + RStepA m n σ r.2 := by + unfold setSimpleKo at hr + obtain ⟨o, ho, hr⟩ := eqPres_mem (getObject_eqPres p) hr + obtain ⟨_, -, hr⟩ := eqPres_mem (PresR.assert _) hr + obtain ⟨_, hpi, hr⟩ := eqPres_mem (PresR.assert _) hr + obtain ⟨ko, hko, e⟩ := setObject_memA hr + have hgo : σ.kheap p = some o := by + have ho' : (o, σ) ∈ (NonDet.assertOpt (σ.kheap p) σ).1 := ho + cases hk : σ.kheap p with + | none => rw [hk] at ho'; exact absurd ho' (Set.notMem_empty _) + | some o' => + rw [hk] at ho' + have := congrArg Prod.fst (show (o, σ) = (o', σ) from ho') + exact congrArg some this.symm + have hn : ∃ a, o = .reply a := by + have h1 := hpi.1 + unfold partialInv at h1 + split_ifs at h1 with hu + · obtain ⟨a, ha, -⟩ := hu + exact ⟨a, ha.symm⟩ + · exact absurd rfl h1 + obtain ⟨a, ea⟩ := hn + rw [ea] at hgo + rw [e] + refine ⟨h.heapD rfl (fun _ _ _ x => x) (fun _ x => x) (NewScTcbA.of_nt (p := p) + (fun q e => Function.update_of_ne e _ _) (fun x0 e => by rw [hgo] at e; cases e)) fun q => ?_, id, rfl, rfl, fun q z hz => ?_, fun q z j hz => ?_, rfl⟩ + · show DObjA σ.idleThread q (σ.kheap q) (Function.update σ.kheap p _ q) + by_cases eq : q = p + · subst eq; rw [Function.update_self, hgo]; exact .reply _ _ + · rw [Function.update_of_ne eq]; exact .same _ + · have eq : q ≠ p := fun eq => by rw [eq, hgo] at hz; cases hz + exact ⟨z, by show Function.update σ.kheap p _ q = _; rw [Function.update_of_ne eq]; exact hz, rfl⟩ + · have eq : q ≠ p := fun eq => by rw [eq, hgo] at hz; cases hz + exact ⟨z, by show Function.update σ.kheap p _ q = _; rw [Function.update_of_ne eq]; exact hz, rfl, rfl, rfl⟩ + +theorem setReplyObjRef_stepA {m : McState} {n : Nat} (u : (Option ObjRef → Option ObjRef) → Reply → Reply) + (p : ObjRef) (v : Option ObjRef) {σ : State Unit} (h : LoopInvA σ m n) + {r : Unit × State Unit} (hr : r ∈ (setReplyObjRef (Z := Unit) u p v σ).1) : RStepA m n σ r.2 := by + unfold setReplyObjRef updateSkObjRef at hr + obtain ⟨x, -, hr⟩ := eqPres_mem (getReply_eqPres p) hr + exact setReply_stepA p _ h hr + +theorem threadSet_memA (f : Tcb → Tcb) (t : ObjRef) {σ : State Unit} {r : Unit × State Unit} + (hr : r ∈ (threadSet (Z := Unit) f t σ).1) : + ∃ x, σ.kheap t = some (.tcb x) ∧ + r.2 = { σ with kheap := Function.update σ.kheap t (some (.tcb (f x))) } := by + obtain ⟨y1, hy1, h⟩ := hr + have hy1' : y1 ∈ (NonDet.assertOpt (getTcb (Z := Unit) t σ) σ).1 := hy1 + cases hg : getTcb (Z := Unit) t σ with + | none => rw [hg] at hy1'; exact absurd hy1' (Set.notMem_empty _) + | some x => + rw [hg] at hy1' + have e1 : y1 = (x, σ) := hy1' + subst e1 + obtain ⟨ko, -, hr2⟩ := setObject_memA h + exact ⟨x, getTcb_kheapA hg, hr2⟩ + +/-- `tcb_sched_action tcb_sched_dequeue`. -/ +theorem tcbSchedDeq_stepA {m : McState} {n : Nat} (t : ObjRef) {σ : State Unit} (h : LoopInvA σ m n) + {r : Unit × State Unit} (hr : r ∈ (tcbSchedAction (Z := Unit) tcbSchedDequeue t σ).1) : RStepA m n σ r.2 := by + unfold tcbSchedAction at hr + obtain ⟨d, -, hr⟩ := eqPres_mem (threadGet_eqPres _ t) hr + obtain ⟨pr, -, hr⟩ := eqPres_mem (threadGet_eqPres _ t) hr + have hq : PresR (@Eq (State Unit)) (getTcbQueue (Z := Unit) d pr) := by unfold getTcbQueue; eqpres + obtain ⟨q, hq', hr⟩ := eqPres_mem hq hr + have eq : q = σ.readyQueues d pr := by + unfold getTcbQueue at hq' + obtain ⟨x, hx, hq'⟩ := mem_bindA.1 hq' + have ex : x = (σ.readyQueues, σ) := hx + subst ex + exact congrArg Prod.fst (show (q, σ) = (σ.readyQueues d pr, σ) from hq') + obtain ⟨q', hq'', hr⟩ := mem_bindA.1 hr + have eq' : q' = (tcbSchedDequeue t q, σ) := hq'' + subst eq' + have er : r = ((), { σ with readyQueues := fun d' p => + if d' = d ∧ p = pr then tcbSchedDequeue t q else σ.readyQueues d' p }) := hr + rw [er] + refine ⟨h.heapD rfl (fun d' p y hy => ?_) (fun _ a => a) (NewScTcbA.of_kheap rfl) (fun _ => .same _), id, rfl, rfl, + fun _ x hx => ⟨x, hx, rfl⟩, fun _ x _ hx => ⟨x, hx, rfl, rfl, rfl⟩, rfl⟩ + simp only at hy + split_ifs at hy with hh + · unfold tcbSchedDequeue at hy + rw [hh.1, hh.2, ← eq] + exact (List.mem_filter.1 hy).1 + · exact hy + +theorem tcbReleaseRemove_stepA {m : McState} {n : Nat} (t : ObjRef) {σ : State Unit} (h : LoopInvA σ m n) + {r : Unit × State Unit} (hr : r ∈ (tcbReleaseRemove (Z := Unit) t σ).1) : RStepA m n σ r.2 := by + unfold tcbReleaseRemove at hr + obtain ⟨qs, hqs, hr⟩ := eqPres_mem (PresR.gets _) hr + have eqs : qs = σ.releaseQueue := congrArg Prod.fst (show (qs, σ) = (σ.releaseQueue, σ) from hqs) + obtain ⟨u, hu, hr⟩ := mem_bindA.1 hr + have h1 : u.2 = { σ with reprogramTimer := u.2.reprogramTimer } := by + unfold NonDet.when at hu + split_ifs at hu + · have : u = ((), { σ with reprogramTimer := true }) := hu + rw [this] + · have : u = ((), σ) := hu + rw [this] + obtain ⟨q', hq', hr⟩ := mem_bindA.1 hr + have eq' : q' = (tcbSchedDequeue t qs, u.2) := hq' + subst eq' + have er : r = ((), { u.2 with releaseQueue := tcbSchedDequeue t qs }) := hr + have er2 : r.2 = { σ with reprogramTimer := u.2.reprogramTimer, releaseQueue := tcbSchedDequeue t qs } := by + rw [er] + show ({ u.2 with releaseQueue := tcbSchedDequeue t qs } : State Unit) = _ + rw [h1] + rw [er2] + refine ⟨h.heapD rfl (fun _ _ _ a => a) (fun y hy => ?_) (NewScTcbA.of_kheap rfl) (fun _ => .same _), id, rfl, rfl, + fun _ x hx => ⟨x, hx, rfl⟩, fun _ x _ hx => ⟨x, hx, rfl, rfl, rfl⟩, rfl⟩ + change y ∈ tcbSchedDequeue t qs at hy + unfold tcbSchedDequeue at hy + rw [← eqs] + exact (List.mem_filter.1 hy).1 + +/-- `tcb_release_remove` filters the thread out of the release queue (and may set the timer flag). -/ +theorem tcbReleaseRemove_memA (t : ObjRef) {σ : State Unit} {r : Unit × State Unit} + (hr : r ∈ (tcbReleaseRemove (Z := Unit) t σ).1) : + ∃ b, r.2 = { σ with reprogramTimer := b, releaseQueue := tcbSchedDequeue t σ.releaseQueue } := by + unfold tcbReleaseRemove at hr + obtain ⟨qs, hqs, hr⟩ := eqPres_mem (PresR.gets _) hr + have eqs : qs = σ.releaseQueue := congrArg Prod.fst (show (qs, σ) = (σ.releaseQueue, σ) from hqs) + obtain ⟨u, hu, hr⟩ := mem_bindA.1 hr + have h1 : u.2 = { σ with reprogramTimer := u.2.reprogramTimer } := by + unfold NonDet.when at hu + split_ifs at hu + · have : u = ((), { σ with reprogramTimer := true }) := hu + rw [this] + · have : u = ((), σ) := hu + rw [this] + obtain ⟨q', hq', hr⟩ := mem_bindA.1 hr + have eq' : q' = (tcbSchedDequeue t qs, u.2) := hq' + subst eq' + have er : r = ((), { u.2 with releaseQueue := tcbSchedDequeue t qs }) := hr + refine ⟨u.2.reprogramTimer, ?_⟩ + rw [er] + show ({ u.2 with releaseQueue := tcbSchedDequeue t qs } : State Unit) = _ + rw [h1, eqs] + +theorem testReschedule_stepA {m : McState} {n : Nat} (t : ObjRef) {σ : State Unit} (h : LoopInvA σ m n) + {r : Unit × State Unit} (hr : r ∈ (testReschedule (Z := Unit) t σ).1) : RStepA m n σ r.2 := by + unfold testReschedule at hr + obtain ⟨cur, -, hr⟩ := eqPres_mem (PresR.gets _) hr + obtain ⟨act, -, hr⟩ := eqPres_mem (PresR.gets _) hr + obtain ⟨flag, -, hr⟩ := eqPres_mem (PresR.pure _) hr + unfold NonDet.when at hr + split_ifs at hr + · exact RStepA.of_queue (h.reschedule hr) (rescheduleRequired_memA hr).1 (rescheduleRequired_memA hr).2 + · have : r = ((), σ) := hr + rw [this] + exact RStepA.refl h + +/-- `tcb_sched_action tcb_sched_dequeue` leaves the release queue, the heap, the consumed time and the current SC +alone. -/ +theorem tcbSchedDeq_rlA (t : ObjRef) {σ : State Unit} {r : Unit × State Unit} + (hr : r ∈ (tcbSchedAction (Z := Unit) tcbSchedDequeue t σ).1) : + r.2.releaseQueue = σ.releaseQueue ∧ r.2.consumedTime = σ.consumedTime ∧ r.2.curSc = σ.curSc ∧ + r.2.kheap = σ.kheap := by + unfold tcbSchedAction at hr + obtain ⟨d, -, hr⟩ := eqPres_mem (threadGet_eqPres _ t) hr + obtain ⟨pr, -, hr⟩ := eqPres_mem (threadGet_eqPres _ t) hr + have hq : PresR (@Eq (State Unit)) (getTcbQueue (Z := Unit) d pr) := by unfold getTcbQueue; eqpres + obtain ⟨q, hq', hr⟩ := eqPres_mem hq hr + obtain ⟨q', hq'', hr⟩ := mem_bindA.1 hr + have eq' : q' = (tcbSchedDequeue t q, σ) := hq'' + subst eq' + have er : r = ((), { σ with readyQueues := fun d' p => + if d' = d ∧ p = pr then tcbSchedDequeue t q else σ.readyQueues d' p }) := hr + rw [er] + exact ⟨rfl, rfl, rfl, rfl⟩ + +/-- `test_reschedule` leaves the release queue alone. -/ +theorem testReschedule_rlA (t : ObjRef) {σ : State Unit} {r : Unit × State Unit} + (hr : r ∈ (testReschedule (Z := Unit) t σ).1) : r.2.releaseQueue = σ.releaseQueue := by + unfold testReschedule at hr + obtain ⟨cur, -, hr⟩ := eqPres_mem (PresR.gets _) hr + obtain ⟨act, -, hr⟩ := eqPres_mem (PresR.gets _) hr + obtain ⟨flag, -, hr⟩ := eqPres_mem (PresR.pure _) hr + unfold NonDet.when at hr + split_ifs at hr + · have e := (rescheduleRequired_memA hr).1.1 + rw [e] + · have : r = ((), σ) := hr + rw [this] + +/-- **`sched_context_donate`** of an SC that is not `idle_sc_ptr` to a thread that is not idle and in no release +queue; (record edit #8b-C2, item 11) afterwards the thread's SC is `p` and `p`'s thread is the thread. -/ +theorem donate_stepA {m : McState} {n : Nat} {p t : ObjRef} {σ : State Unit} (h : LoopInvA σ m n) + (hp : p ≠ idleScPtr) (ht : t ≠ σ.idleThread) (hnr : t ∉ σ.releaseQueue) + {r : Unit × State Unit} (hr : r ∈ (schedContextDonate (Z := Unit) p t σ).1) : + RStepA m n σ r.2 ∧ (∃ x, r.2.kheap t = some (.tcb x) ∧ x.tcbSchedContext = some p) ∧ + (∀ y k, r.2.kheap p = some (.schedContext y k) → y.scTcb = some t) := by + unfold schedContextDonate at hr + have hg : PresR (@Eq (State Unit)) (getScObjRef (Z := Unit) SchedContext.scTcb p) := by + unfold getScObjRef + refine PresR.bind (getSchedContext_eqPres p) (fun _ => ?_) + eqpres + obtain ⟨fo, hfo, hr⟩ := eqPres_mem hg hr + have hfrom : ∀ f, fo = some f → f ≠ σ.idleThread := by + intro f ef ei + unfold getScObjRef at hfo + obtain ⟨y, hy, hfo⟩ := eqPres_mem (getSchedContext_eqPres p) hfo + have efo : fo = y.scTcb := congrArg Prod.fst (show (fo, σ) = (y.scTcb, σ) from hfo) + have : ∃ k, σ.kheap p = some (.schedContext y k) := by + unfold getSchedContext at hy + obtain ⟨o, ho, hy⟩ := eqPres_mem (getObject_eqPres p) hy + have ho' : (o, σ) ∈ (NonDet.assertOpt (σ.kheap p) σ).1 := ho + cases hk : σ.kheap p with + | none => rw [hk] at ho'; exact absurd ho' (Set.notMem_empty _) + | some o' => + rw [hk] at ho' + have eo : o = o' := congrArg Prod.fst (show (o, σ) = (o', σ) from ho') + subst eo + cases o with + | schedContext z k => + have : y = z := congrArg Prod.fst (show (y, σ) = (z, σ) from hy) + exact ⟨k, by rw [this]⟩ + | _ => exact absurd hy (Set.notMem_empty _) + obtain ⟨k, hk⟩ := this + exact hp (h.sidle.f5 p y k hk (by rw [← efo, ef, ei])) + obtain ⟨u, hu, hr⟩ := mem_bindA.1 hr + have S1 : RStepA m n σ u.2 ∧ ∀ y ∈ u.2.releaseQueue, y ∈ σ.releaseQueue := by + unfold NonDet.when at hu + split_ifs at hu with hne + swap + · have : u = ((), σ) := hu + rw [this] + exact ⟨RStepA.refl h, fun _ hh => hh⟩ + obtain ⟨f, hf, hu⟩ := eqPres_mem (PresR.assertOpt _) hu + have ef : fo = some f := by + cases hfo' : fo with + | none => exact absurd hfo' hne + | some g => + rw [hfo'] at hf + have : f = g := congrArg Prod.fst (show (f, σ) = (g, σ) from hf) + rw [this] + have hfi := hfrom f ef + obtain ⟨a, ha, hu⟩ := mem_bindA.1 hu + have A := tcbSchedDeq_stepA f h ha + obtain ⟨b, hb, hu⟩ := mem_bindA.1 hu + have B := tcbReleaseRemove_stepA f A.inv hb + obtain ⟨c, hc, hu⟩ := mem_bindA.1 hu + have C : RStepA m n b.2 c.2 := by + unfold setTcbObjRef at hc + obtain ⟨x, hx, ec⟩ := threadSet_memA _ _ hc + rw [ec] + exact RStepA.tcbSc B.inv hx (by rw [B.idle, A.idle]; exact hfi) none (fun h0 => by cases h0) + have D := testReschedule_stepA f C.inv hu + refine ⟨A.trans (B.trans (C.trans D)), fun y hy => ?_⟩ + have sC : c.2.releaseQueue = b.2.releaseQueue := by + unfold setTcbObjRef at hc + obtain ⟨x, hx, ec⟩ := threadSet_memA _ _ hc + rw [ec] + obtain ⟨bb, hbb⟩ := tcbReleaseRemove_memA f hb + rw [testReschedule_rlA f hu, sC, hbb] at hy + change y ∈ tcbSchedDequeue f a.2.releaseQueue at hy + unfold tcbSchedDequeue at hy + have := (List.mem_filter.1 hy).1 + rw [(tcbSchedDeq_rlA f ha).1] at this + exact this + obtain ⟨v, hv, hr⟩ := mem_bindA.1 hr + unfold setScObjRef at hv + obtain ⟨x2, k2, hk2, ev⟩ := updateSchedContext_memA _ _ hv + have S2 : RStepA m n u.2 v.2 := by + rw [ev] + exact RStepA.updSc S1.1.inv hk2 hp rfl rfl rfl (Or.inr fun e => by + rw [S1.1.idle] at e + exact ht (Option.some.inj e)) + have shr : ∀ y ∈ v.2.releaseQueue, y ∈ σ.releaseQueue := fun y hy => by + have : v.2.releaseQueue = u.2.releaseQueue := by rw [ev] + rw [this] at hy + exact S1.2 y hy + unfold setTcbObjRef at hr + obtain ⟨x, hx, er⟩ := threadSet_memA _ _ hr + have S3 : RStepA m n v.2 r.2 := by + rw [er] + exact RStepA.tcbSc S2.inv hx (by rw [S2.idle, S1.1.idle]; exact ht) (some p) (fun _ hq => absurd (shr t hq) hnr) + have hpt : p ≠ t := fun e => by + obtain ⟨y', h1⟩ : ∃ y', v.2.kheap p = some (.schedContext y' k2) := + ⟨_, by rw [ev]; exact Function.update_self ..⟩ + rw [e, hx] at h1 + cases h1 + refine ⟨S1.1.trans (S2.trans S3), ⟨{ x with tcbSchedContext := some p }, ?_, rfl⟩, fun y k hy => ?_⟩ + · rw [er]; exact Function.update_self .. + · rw [er] at hy + change Function.update v.2.kheap t _ p = _ at hy + rw [Function.update_of_ne hpt, ev] at hy + change Function.update u.2.kheap p _ p = _ at hy + rw [Function.update_self] at hy + have := Option.some.inj hy + injection this with h1 h2 + rw [← h1] + rfl + +/-- **`reply_unlink_sc`** of an SC that is not `idle_sc_ptr`. -/ +theorem replyUnlinkSc_stepA {m : McState} {n : Nat} {p rp : ObjRef} {σ : State Unit} (h : LoopInvA σ m n) + (hp : p ≠ idleScPtr) + {r : Unit × State Unit} (hr : r ∈ (replyUnlinkSc (Z := Unit) p rp σ).1) : RStepA m n σ r.2 := by + unfold replyUnlinkSc at hr + have hg : PresR (@Eq (State Unit)) (NonDet.liftM SchedContext.scReplies (getSchedContext (Z := Unit) p)) := by + unfold NonDet.liftM + refine PresR.bind (getSchedContext_eqPres p) (fun _ => ?_) + eqpres + obtain ⟨l, -, hr⟩ := eqPres_mem hg hr + obtain ⟨rep, -, hr⟩ := eqPres_mem (getReply_eqPres rp) hr + have upd : ∀ (g : List ObjRef → List ObjRef) {τ : State Unit} (_ : LoopInvA τ m n) {w : Unit × State Unit}, + w ∈ (updateSchedContext (Z := Unit) p (scRepliesUpdate g) τ).1 → RStepA m n τ w.2 := by + intro g τ ht w hw + obtain ⟨x, k, hk, ew⟩ := updateSchedContext_memA _ _ hw + rw [ew] + exact RStepA.updSc ht hk hp rfl rfl rfl (Or.inl rfl) + split_ifs at hr + · obtain ⟨_, -, hr⟩ := eqPres_mem (PresR.assert _) hr + obtain ⟨a, ha, hr⟩ := mem_bindA.1 hr + have A := upd _ h ha + cases htl : l.tail with + | nil => + simp only [htl] at hr + exact A.trans (setReplyObjRef_stepA _ _ _ A.inv hr) + | cons r' rest => + simp only [htl] at hr + obtain ⟨b, hb, hr⟩ := mem_bindA.1 hr + have B := setReplyObjRef_stepA _ _ _ A.inv hb + exact A.trans (B.trans (setReplyObjRef_stepA _ _ _ B.inv hr)) + · obtain ⟨_, -, hr⟩ := eqPres_mem (PresR.assert _) hr + exact upd _ h hr + +/-- **`reply_unlink_tcb`** of a thread that is neither current nor idle. -/ +theorem replyUnlinkTcb_stepA {m : McState} {n : Nat} {t rp : ObjRef} {σ : State Unit} (h : LoopInvA σ m n) + {x : Tcb} (hk : σ.kheap t = some (.tcb x)) (hb : runnable x.tcbState = false) (o : OffCurA σ m n t) + {r : Unit × State Unit} (hr : r ∈ (replyUnlinkTcb (Z := Unit) t rp σ).1) : + LoopInvA r.2 m n ∧ (∀ q y k, σ.kheap q = some (.schedContext y k) → r.2.kheap q = some (.schedContext y k)) ∧ + (∀ q y, q ≠ t → σ.kheap q = some (.tcb y) → r.2.kheap q = some (.tcb y)) ∧ + (∃ y, r.2.kheap t = some (.tcb y) ∧ y.tcbState = .inactive) ∧ + (∃ z, r.2.kheap rp = some (.reply z) ∧ z.replyTcb = none) ∧ (NoSwA σ → NoSwA r.2) := by + unfold replyUnlinkTcb at hr + obtain ⟨rep, -, hr⟩ := eqPres_mem (getReply_eqPres rp) hr + obtain ⟨_, -, hr⟩ := eqPres_mem (PresR.assert _) hr + have hg : PresR (@Eq (State Unit)) (getThreadState (Z := Unit) t) := by + unfold getThreadState + exact threadGet_eqPres _ t + obtain ⟨ts, -, hr⟩ := eqPres_mem hg hr + obtain ⟨_, -, hr⟩ := eqPres_mem (PresR.assert _) hr + obtain ⟨a, ha, hr⟩ := mem_bindA.1 hr + have ha0 := ha + unfold setReplyObjRef updateSkObjRef at ha0 + obtain ⟨xr, -, ha1⟩ := eqPres_mem (getReply_eqPres rp) ha0 + have keepA : ∀ q o, σ.kheap q = some o → (∀ z, o ≠ .reply z) → a.2.kheap q = some o := by + intro q o hq no + have ha2 := ha1 + unfold setSimpleKo at ha2 + obtain ⟨ko, hko, ha2⟩ := eqPres_mem (getObject_eqPres rp) ha2 + obtain ⟨_, -, ha2⟩ := eqPres_mem (PresR.assert _) ha2 + obtain ⟨_, hpi, ha2⟩ := eqPres_mem (PresR.assert _) ha2 + obtain ⟨_, -, e⟩ := setObject_memA ha2 + have ne : q ≠ rp := by + intro e0 + have ho' : (ko, σ) ∈ (NonDet.assertOpt (σ.kheap rp) σ).1 := hko + rw [← e0, hq] at ho' + have eo : ko = o := congrArg Prod.fst (show (ko, σ) = (o, σ) from ho') + have h1 := hpi.1 + unfold partialInv at h1 + split_ifs at h1 with hu + · obtain ⟨a', ha', -⟩ := hu + exact no a' (eo ▸ ha'.symm) + · exact h1 rfl + rw [e] + show Function.update σ.kheap rp _ q = _ + rw [Function.update_of_ne ne] + exact hq + have wrA : ∃ z, a.2.kheap rp = some (.reply z) ∧ z.replyTcb = none := by + have ha2 := ha1 + unfold setSimpleKo at ha2 + obtain ⟨ko, -, ha2⟩ := eqPres_mem (getObject_eqPres rp) ha2 + obtain ⟨_, -, ha2⟩ := eqPres_mem (PresR.assert _) ha2 + obtain ⟨_, -, ha2⟩ := eqPres_mem (PresR.assert _) ha2 + obtain ⟨_, -, e⟩ := setObject_memA ha2 + refine ⟨replyTcbUpdate (fun _ => none) xr, ?_, rfl⟩ + rw [e] + exact Function.update_self _ _ _ + have A := setReplyObjRef_stepA _ _ _ h ha + have hka : a.2.kheap t = some (.tcb x) := keepA _ _ hk (fun _ e => by cases e) + have oa : OffCurA a.2 m n t := ⟨by rw [A.cur]; exact o.cur, by rw [A.idle]; exact o.idle, o.idles⟩ + unfold setThreadState at hr + obtain ⟨b, hbm, hr⟩ := mem_bindA.1 hr + obtain ⟨y, hy, eb⟩ := threadSet_memA _ _ hbm + rw [hka] at hy + cases hy + have K := A.inv.setBlk hka hb oa (ts := .inactive) (Or.inr rfl) (fun e => by cases e) + have ebst : b.2 = Notif.updK a.2 t (.tcb { x with tcbState := .inactive }) := eb + have noact : Notif.stsAct b.2 t = b.2 := by + unfold Notif.stsAct + rw [if_neg] + intro c + apply oa.cur + rw [ebst] at c + exact c.1 + have er : r.2 = b.2 := by + unfold setThreadStateAct at hr + obtain ⟨cur, hcur, hr⟩ := eqPres_mem (PresR.gets _) hr + have ecur : cur = b.2.curThread := congrArg Prod.fst (show (cur, b.2) = (b.2.curThread, b.2) from hcur) + obtain ⟨act, -, hr⟩ := eqPres_mem (PresR.gets _) hr + obtain ⟨sch, -, hr⟩ := eqPres_mem (PresR.gets _) hr + unfold NonDet.when at hr + split_ifs at hr with c + · exfalso + apply oa.cur + rw [c.1, ecur, ebst] + rfl + · exact congrArg Prod.snd (show r = ((), b.2) from hr) + rw [← ebst, noact] at K + have off : ∀ q, q ≠ t → r.2.kheap q = a.2.kheap q := fun q ne => by + rw [er, ebst] + show Function.update a.2.kheap t _ q = _ + rw [Function.update_of_ne ne] + have ns : NoSwA σ → NoSwA r.2 := fun h0 u c => by + rw [er, ebst] at c + exact A.ns h0 u c + refine ⟨by rw [er]; exact K, fun q y k hq => ?_, fun q y ne hq => ?_, ?_, ?_, ns⟩ + · have A' := keepA q _ hq (fun _ e => by cases e) + have ne : q ≠ t := fun e0 => by rw [e0, hka] at A'; cases A' + rw [off q ne]; exact A' + · rw [off q ne]; exact keepA q _ hq (fun _ e => by cases e) + · refine ⟨{ x with tcbState := .inactive }, ?_, rfl⟩ + rw [er, ebst] + exact Function.update_self _ _ _ + · obtain ⟨z, hz, ez⟩ := wrA + have ne : rp ≠ t := fun e0 => by rw [e0, hka] at hz; cases hz + exact ⟨z, by rw [off rp ne]; exact hz, ez⟩ + +/-- **The three invariants and every `sc_yield_from` across `reply_remove`** of a caller blocked on this reply. -/ +theorem replyRemove_stepA {m : McState} {n : Nat} {t rp : ObjRef} {σ : State Unit} (h : LoopInvA σ m n) + {x : Tcb} (hk : σ.kheap t = some (.tcb x)) + (hst : x.tcbState = .blockedOnReply rp ∨ ∃ ep d, x.tcbState = .blockedOnReceive ep (some rp) d) + {r : Unit × State Unit} (hr : r ∈ (replyRemove (Z := Unit) t rp σ).1) : + LoopInvA r.2 m n ∧ (∀ q y k, σ.kheap q = some (.schedContext y k) → + ∃ y', r.2.kheap q = some (.schedContext y' k) ∧ y'.scYieldFrom = y.scYieldFrom) ∧ + (∀ q y, q ≠ t → σ.kheap q = some (.tcb y) → ∃ y', r.2.kheap q = some (.tcb y') ∧ y'.tcbState = y.tcbState) ∧ + (∃ y, r.2.kheap t = some (.tcb y) ∧ y.tcbState = .inactive) ∧ + (∃ z, r.2.kheap rp = some (.reply z) ∧ z.replyTcb = none) ∧ (NoSwA σ → NoSwA r.2) := by + have blk : x.tcbState ≠ .running ∧ x.tcbState ≠ .restart ∧ x.tcbState ≠ .inactive ∧ + x.tcbState ≠ .idleThreadState ∧ runnable x.tcbState = false := by + rcases hst with e | ⟨_, _, e⟩ <;> rw [e] <;> + exact ⟨(fun h => by cases h), (fun h => by cases h), (fun h => by cases h), (fun h => by cases h), rfl⟩ + have off : OffCurA σ m n t := by + refine ⟨fun e => ?_, fun e => ?_, fun c hc e => ?_⟩ + · obtain ⟨z, hz, hz'⟩ := h.sact.f2 + rw [← e, hk] at hz + have := KernelObject.tcb.inj (Option.some.inj hz) + subst this + rcases hz' with a | a | a | a + · exact blk.1 a + · exact blk.2.1 a + · exact blk.2.2.1 a + · exact blk.2.2.2.1 a + · obtain ⟨z, hz, hz'⟩ := h.sinv.idleTcb + rw [← e, hk] at hz + have := KernelObject.tcb.inj (Option.some.inj hz) + subst this + exact blk.2.2.2.1 hz' + · obtain ⟨z, hz, hz'⟩ := h.sidle.f3 c hc + rw [e, hk] at hz + have := KernelObject.tcb.inj (Option.some.inj hz) + subst this + exact blk.2.2.2.1 hz' + unfold replyRemove at hr + obtain ⟨rep, -, hr⟩ := eqPres_mem (getReply_eqPres rp) hr + obtain ⟨_, -, hr⟩ := eqPres_mem (PresR.assert _) hr + obtain ⟨so, hso, hr⟩ := eqPres_mem (PresR.gets _) hr + have eso : so = scWithReply rp σ := congrArg Prod.fst (show (so, σ) = (scWithReply rp σ, σ) from hso) + have fin : ∀ τ : State Unit, RStepA m n σ τ → ∀ w ∈ (replyUnlinkTcb (Z := Unit) t rp τ).1, + LoopInvA w.2 m n ∧ (∀ q y k, σ.kheap q = some (.schedContext y k) → + ∃ y', w.2.kheap q = some (.schedContext y' k) ∧ y'.scYieldFrom = y.scYieldFrom) ∧ + (∀ q y, q ≠ t → σ.kheap q = some (.tcb y) → ∃ y', w.2.kheap q = some (.tcb y') ∧ y'.tcbState = y.tcbState) ∧ + (∃ y, w.2.kheap t = some (.tcb y) ∧ y.tcbState = .inactive) ∧ + (∃ z, w.2.kheap rp = some (.reply z) ∧ z.replyTcb = none) ∧ (NoSwA σ → NoSwA w.2) := by + intro τ S w hw + obtain ⟨y, hy, es⟩ := S.st _ _ hk + have oy : OffCurA τ m n t := ⟨by rw [S.cur]; exact off.cur, by rw [S.idle]; exact off.idle, off.idles⟩ + obtain ⟨I, K, Kt, hin, hre, nsw⟩ := replyUnlinkTcb_stepA S.inv hy (by rw [es]; exact blk.2.2.2.2) oy hw + refine ⟨I, fun q z k hz => ?_, fun q z ne hz => ?_, hin, hre, fun h0 => nsw (S.ns h0)⟩ + · obtain ⟨z', hz', e⟩ := S.sc q z k hz + exact ⟨z', K q z' k hz', e.1⟩ + · obtain ⟨z', hz', e⟩ := S.st q z hz + exact ⟨z', Kt q z' ne hz', e⟩ + cases so with + | none => exact fin σ (RStepA.refl h) r hr + | some p => + dsimp only at hr + have hp : p ≠ idleScPtr := by + intro ep + have hw := eso.symm + unfold scWithReply partialInv at hw + split_ifs at hw with hun + have hsp := (Classical.choose_spec hun).1 + have eThe := Option.some.inj hw + unfold The at eThe + rw [dif_pos hun] at eThe + have hsat : ∃ sc k, σ.kheap p = some (.schedContext sc k) ∧ rp ∈ sc.scReplies := by + rw [← eThe] + simpa only [decide_eq_true_eq] using hsp + obtain ⟨sc, k, hsc, hm⟩ := hsat + rw [ep] at hsc + rw [h.sidle.f4 sc k hsc] at hm + cases hm + have hg : PresR (@Eq (State Unit)) + (NonDet.liftM SchedContext.scReplies (getSchedContext (Z := Unit) p)) := by + unfold NonDet.liftM + refine PresR.bind (getSchedContext_eqPres p) (fun _ => ?_) + eqpres + obtain ⟨l, -, hr⟩ := eqPres_mem hg hr + have hg2 : PresR (@Eq (State Unit)) (getTcbObjRef (Z := Unit) Tcb.tcbSchedContext t) := by + unfold getTcbObjRef + exact threadGet_eqPres _ t + obtain ⟨cs, -, hr⟩ := eqPres_mem hg2 hr + obtain ⟨a, ha, hr⟩ := mem_bindA.1 hr + have A := replyUnlinkSc_stepA h hp ha + have hnr : t ∉ a.2.releaseQueue := fun hq => by + obtain ⟨xa, hxa, hrun⟩ := A.inv.sinv.rlsRun t hq + obtain ⟨ya, hya, esa⟩ := A.st t x hk + rw [hxa] at hya + have := KernelObject.tcb.inj (Option.some.inj hya) + subst this + rw [esa, blk.2.2.2.2] at hrun + cases hrun + obtain ⟨w, hw, hr⟩ := mem_bindA.1 hr + have B : RStepA m n a.2 w.2 := by + unfold NonDet.when at hw + split_ifs at hw + · exact (donate_stepA A.inv hp (by rw [A.idle]; exact off.idle) hnr hw).1 + · have : w = ((), a.2) := hw + rw [this] + exact RStepA.refl A.inv + exact fin w.2 (A.trans B) r hr + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/ReplyRemoveKeepA.lean b/proofs/Fl4ProofsA64/P3/ReplyRemoveKeepA.lean new file mode 100644 index 000000000..e1b764d87 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ReplyRemoveKeepA.lean @@ -0,0 +1,191 @@ +/- +P3 (AArch64): the spec frame of a reply removal. + +`RemKeepA σ σ'` is what `reply_remove` leaves untouched in the spec state: +the user memory, every object that is not an SC, a TCB or a reply, and +every TCB field except its state and SC. The concrete footprint +(`reply_remove_footA`) says which words are written; this says which spec +objects change. Together they carry the IPC buffers, caps and slots to +the transfer that follows the removal. +-/ +import Fl4ProofsA64.P3.IpcScPopA +import Fl4ProofsA64.P3.ScSWrA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4SpecA64 Sched + +noncomputable section + +/-- What a reply removal or refill test keeps in the spec state. -/ +def RemKeepA (σ σ' : State Unit) : Prop := + σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory ∧ + (∀ p o, σ.kheap p = some o → (∀ y n, o ≠ .schedContext y n) → (∀ x, o ≠ .tcb x) → + (∀ x, o ≠ .reply x) → σ'.kheap p = some o) ∧ + (∀ p x, σ.kheap p = some (.tcb x) → ∃ y st sc, σ'.kheap p = some (.tcb y) ∧ + y = { x with tcbState := st, tcbSchedContext := sc }) + +theorem RemKeepA.refl (σ : State Unit) : RemKeepA σ σ := + ⟨rfl, fun _ _ h _ _ _ => h, fun _ x h => ⟨x, x.tcbState, x.tcbSchedContext, h, by cases x; rfl⟩⟩ + +theorem RemKeepA.trans {σ1 σ2 σ3 : State Unit} (h1 : RemKeepA σ1 σ2) (h2 : RemKeepA σ2 σ3) : + RemKeepA σ1 σ3 := by + obtain ⟨m1, o1, t1⟩ := h1 + obtain ⟨m2, o2, t2⟩ := h2 + refine ⟨m2.trans m1, fun p o hp a b c => ?_, fun p x hp => ?_⟩ + · exact o2 p o (o1 p o hp a b c) a b c + · obtain ⟨y, st1, sc1, hy, rfl⟩ := t1 p x hp + obtain ⟨z, st2, sc2, hz, rfl⟩ := t2 p _ hy + exact ⟨_, st2, sc2, hz, rfl⟩ + +/-- An update of one SC to an SC of the same size. -/ +theorem RemKeepA.updSc {σ σ' : State Unit} {p : ObjRef} {x y : SchedContext} {n : Nat} + (hk : σ.kheap p = some (.schedContext x n)) (hm : σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory) + (hh : σ'.kheap = Function.update σ.kheap p (some (.schedContext y n))) : RemKeepA σ σ' := by + refine ⟨hm, fun q o hq a b c => ?_, fun q x' hq => ?_⟩ + · by_cases e : q = p + · subst e + rw [hk] at hq + cases hq + exact absurd rfl (a x n) + · rw [hh, Function.update_of_ne e] + exact hq + · have e : q ≠ p := by + intro e + subst e + rw [hk] at hq + cases hq + exact ⟨x', x'.tcbState, x'.tcbSchedContext, by rw [hh, Function.update_of_ne e]; exact hq, + by cases x'; rfl⟩ + +/-- An update of one reply object to a reply object. -/ +theorem RemKeepA.updRep {σ σ' : State Unit} {p : ObjRef} {x y : Reply} + (hk : σ.kheap p = some (.reply x)) (hm : σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory) + (hh : σ'.kheap = Function.update σ.kheap p (some (.reply y))) : RemKeepA σ σ' := by + refine ⟨hm, fun q o hq a b c => ?_, fun q x' hq => ?_⟩ + · by_cases e : q = p + · subst e + rw [hk] at hq + cases hq + exact absurd rfl (c x) + · rw [hh, Function.update_of_ne e] + exact hq + · have e : q ≠ p := by + intro e + subst e + rw [hk] at hq + cases hq + exact ⟨x', x'.tcbState, x'.tcbSchedContext, by rw [hh, Function.update_of_ne e]; exact hq, + by cases x'; rfl⟩ + +/-- A change of one TCB's state and SC. -/ +theorem RemKeepA.updTcb {σ σ' : State Unit} {p : ObjRef} {x : Tcb} {st : ThreadState} + {sc : Option ObjRef} (hk : σ.kheap p = some (.tcb x)) + (hm : σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory) + (hh : σ'.kheap = Function.update σ.kheap p + (some (.tcb { x with tcbState := st, tcbSchedContext := sc }))) : RemKeepA σ σ' := by + refine ⟨hm, fun q o hq a b c => ?_, fun q x' hq => ?_⟩ + · by_cases e : q = p + · subst e + rw [hk] at hq + cases hq + exact absurd rfl (b x) + · rw [hh, Function.update_of_ne e] + exact hq + · by_cases e : q = p + · subst e + rw [hk] at hq + cases hq + exact ⟨_, st, sc, by rw [hh, Function.update_self], rfl⟩ + · exact ⟨x', x'.tcbState, x'.tcbSchedContext, by rw [hh, Function.update_of_ne e]; exact hq, + by cases x'; rfl⟩ + +theorem RemKeepA.of_kheap {σ σ' : State Unit} (hm : σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory) + (hh : σ'.kheap = σ.kheap) : RemKeepA σ σ' := by + refine ⟨hm, fun q o hq _ _ _ => by rw [hh]; exact hq, fun q x hq => ?_⟩ + exact ⟨x, x.tcbState, x.tcbSchedContext, by rw [hh]; exact hq, by cases x; rfl⟩ + +theorem RemKeepA.of_stsAct (σ : State Unit) (t : ObjRef) : RemKeepA σ (stsAct σ t) := + RemKeepA.of_kheap (by unfold stsAct; split_ifs <;> rfl) (stsAct_kheap σ t) + +/-- A scheduler step that keeps every TCB but its SC and every non-SC object, with the user memory. -/ +theorem RemKeepA.of_skeep {σ σ' : State Unit} (h : SKeepA σ σ') + (hm : σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory) : RemKeepA σ σ' := by + refine ⟨hm, fun p o hp a b c => ?_, fun p x hp => ?_⟩ + · exact h.obj p o hp a b + · obtain ⟨y, hy, sc, rfl⟩ := h.tcb p x hp + exact ⟨_, x.tcbState, sc, hy, rfl⟩ + +theorem RemKeepA.of_rsc (σ : State Unit) (p : ObjRef) (v : Option ObjRef) : RemKeepA σ (rscA σ p v) := by + unfold rscA + split + · rename_i x hx + exact RemKeepA.updRep hx rfl rfl + · exact RemKeepA.refl σ + +theorem RemKeepA.of_fixHd (σ : State Unit) (N : ObjRef) (l : List ObjRef) : RemKeepA σ (fixHdA σ N l) := by + cases l with + | nil => exact RemKeepA.refl σ + | cons h t => exact RemKeepA.of_rsc σ h (some N) + +/-- The pop of the head reply off an SC's stack. -/ +theorem RemKeepA.of_popSt {σ : State Unit} {N R : ObjRef} {xs : SchedContext} {ns : Nat} + (hk : σ.kheap N = some (.schedContext xs ns)) : RemKeepA σ (popStA σ N xs ns R) := by + rw [popStA_def] + refine RemKeepA.trans (RemKeepA.updSc (σ' := repStA σ N xs ns xs.scReplies.tail) + (y := { xs with scReplies := xs.scReplies.tail }) hk rfl rfl) ?_ + refine RemKeepA.trans (RemKeepA.of_fixHd _ _ _) (RemKeepA.of_rsc _ _ _) + +/-- The cut of a live stack. -/ +theorem RemKeepA.of_repSt {σ : State Unit} {N : ObjRef} {xs : SchedContext} {ns : Nat} (l : List ObjRef) + (hk : σ.kheap N = some (.schedContext xs ns)) : RemKeepA σ (repStA σ N xs ns l) := + RemKeepA.updSc (y := { xs with scReplies := l }) hk rfl rfl + +/-- The unlink of the reply's TCB, as an explicit state. -/ +theorem RemKeepA.of_unlink {σ : State Unit} {t r : ObjRef} {x : Tcb} {xr : Reply} + (hk : σ.kheap t = some (.tcb x)) (hr : σ.kheap r = some (.reply xr)) : + RemKeepA σ (replyUnlinkStateA σ t r x xr) := by + have hrt : r ≠ t := by intro h; rw [h, hk] at hr; cases hr + unfold replyUnlinkStateA + have s1 : RemKeepA σ (updK σ r (.reply { xr with replyTcb := none })) := + RemKeepA.updRep (y := { xr with replyTcb := none }) hr rfl rfl + have hk1 : (updK σ r (.reply { xr with replyTcb := none })).kheap t = some (.tcb x) := by + rw [updK_kheap, Function.update_of_ne hrt.symm, hk] + have s2 := RemKeepA.updTcb (σ := updK σ r (.reply { xr with replyTcb := none })) + (σ' := updK (updK σ r (.reply { xr with replyTcb := none })) t (.tcb { x with tcbState := .inactive })) + (st := .inactive) (sc := x.tcbSchedContext) hk1 rfl (by rw [updK_kheap]) + exact (s1.trans s2).trans (RemKeepA.of_stsAct _ _) + +/-- The unlink's spec outcome (copy of the private lemma of the reply pop). -/ +theorem unlinkTcbSpecFA {σ : State Unit} {t r : ObjRef} {x : Tcb} {xr : Reply} + (hk : σ.kheap t = some (.tcb x)) (hr : σ.kheap r = some (.reply xr)) + (hback : xr.replyTcb = some t) + (hblock : x.tcbState = .blockedOnReply r ∨ ∃ ep d, x.tcbState = .blockedOnReceive ep (some r) d) : + replyUnlinkTcb (Z := Unit) t r σ = ({((), replyUnlinkStateA σ t r x xr)}, False) := by + have hrt : r ≠ t := by intro h; rw [h, hk] at hr; cases hr + have hk' : (updK σ r (.reply { xr with replyTcb := none })).kheap t = some (.tcb x) := by + rw [updK_kheap, Function.update_of_ne hrt.symm, hk] + unfold replyUnlinkTcb replyUnlinkStateA + rw [nd_bind_one (getReply_eq hr), nd_bind_one (assert_true hback σ), nd_bind_one (getThreadState_eq hk), + nd_bind_one (assert_true hblock σ), nd_bind_one (setReplyTcb_eq none hr)] + exact setThreadState_eq .inactive hk' + +/-- The unlink as a spec frame. -/ +theorem replyUnlinkKeepA {σ : State Unit} {t r : ObjRef} {x : Tcb} {xr : Reply} + (hk : σ.kheap t = some (.tcb x)) (hr : σ.kheap r = some (.reply xr)) + (hback : xr.replyTcb = some t) + (hblock : x.tcbState = .blockedOnReply r ∨ ∃ ep d, x.tcbState = .blockedOnReceive ep (some r) d) : + ∃ σ', replyUnlinkTcb (Z := Unit) t r σ = ({((), σ')}, False) ∧ RemKeepA σ σ' := + ⟨_, unlinkTcbSpecFA hk hr hback hblock, RemKeepA.of_unlink hk hr⟩ + +/-- Two outcomes of one spec operation agree. -/ +theorem det_outA {a b : State Unit} {X : (Set (Unit × State Unit) × Prop)} + (h1 : X = ({((), a)}, False)) (h2 : X = ({((), b)}, False)) : a = b := by + have h := h1.symm.trans h2 + have h' := congrArg Prod.fst h + have := Set.singleton_eq_singleton_iff.1 h' + exact (Prod.mk.inj this).2 + +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/ReplyRemoveQA.lean b/proofs/Fl4ProofsA64/P3/ReplyRemoveQA.lean new file mode 100644 index 000000000..2c34ed0b2 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ReplyRemoveQA.lean @@ -0,0 +1,448 @@ +/- +P3 (AArch64, delete hooks W3 Reply (a)/(d)): **`reply_remove` for the delete loop's Reply finaliser** — the callee +blocked on its reply (not in `inClassR`, so `SchedSRB.toA_ncA` closes `rd`: no `hpost`), and the SC holder possibly in a +ready or release queue (`sc_donate_fromQA`). + +* **`ReplyPopDonQA`** / **`ReplyPopLocalQA`**: the donation's local facts at the call / at entry (the holder's stall is + the no-op at the state after the pop prefix; `P3/ReplyStallA`). +* `reply_pop_donFromQA`, `replyPopDonateQA`, **`reply_pop_relQA`**, **`reply_remove_relQA`**: copies of + `P3/ScPopDonA`, `P3/IpcScPopDonateA`, `P3/IpcScPopTopA`, `P3/IpcScRemoveTopA` at those facts (their private lemmas + copied). + +No named premise ([S]-shaped hypotheses in `ReplyPopLocalQA`, discharged by the finaliser). New module. +-/ +import Fl4ProofsA64.P3.IpcScRemoveTopA +import Fl4ProofsA64.P3.ScDonQueuedA +import Fl4ProofsA64.P3.ReplyRemoveBorA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +/-- `reply_pop`'s donation branch, the SC bound to `f`, queued or not. -/ +theorem reply_pop_donFromQA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} + {σ : Fl4SpecA64.State Unit} (H : SchedSRA st Z m κ s σ) (hst : StEqA Z.T (L.mem s) σ) (a : A) + {sc f t : U64} (hsc : sc ∈ Z.C) (hsc0 : sc ≠ 0#u64) (hf : f ∈ Z.T) (hfs : L.mem s (scA f) = sc) + (ht : t ∈ Z.T) (hfon : L.mem s (affA f) = L.cpu s) + (hstall : smp.remote_tcb_stall st ar s a f = ok (s, a)) + (hmig : L.mem s (affA t) = L.mem s (scCoreA sc)) (hni : NotIdleScA m (scNameA m sc)) + {xt : Fl4SpecA64.Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + {l : List Fl4SpecA64.ObjRef} {R : Fl4SpecA64.ObjRef} (hhd : l.head? = some R) : + (if L.mem s (scA t) = 0#u64 then schedcontext.donate st ar s a sc t else ok (s, a)) ⦃ r => r.2 = a ∧ + ∃ σ' Q' R', Fl4Spec.NonDet.when (Fl4SpecA64.the l.head? = R ∧ xt.tcbSchedContext = none) + (Fl4SpecA64.schedContextDonate (Z := Unit) (scNameA m sc) (specPtr t)) σ = ({((), σ')}, False) ∧ + SchedSRB st { Z with Q := Q', R := R' } m κ r.1 σ' (specPtr t) ∧ MFrA (st := st) Z m s r.1 σ σ' ∧ + (∀ q y k, σ.kheap q = some (Fl4SpecA64.KernelObject.schedContext y k) → ∃ y', σ'.kheap q = some (Fl4SpecA64.KernelObject.schedContext y' k) ∧ y'.scReplies = y.scReplies) ⦄ := by + obtain ⟨xt0, hkt0, hsct⟩ := H.sch.tcb t ht + rw [hkt] at hkt0; cases hkt0 + have hthe : Fl4SpecA64.the l.head? = R := by rw [hhd]; rfl + by_cases h0 : L.mem s (scA t) = 0#u64 + · rw [if_pos h0] + have hxn : xt.tcbSchedContext = none := by rw [hsct, h0]; simp [optScA] + have hft : f ≠ t := fun e => hsc0 (by rw [← hfs, e, h0]) + refine WP.spec_mono (sc_donate_fromQA H hst a hsc hsc0 hf hfs ht h0 hft hfon hstall hmig hni) ?_ + rintro r ⟨h1, σ', Q', R', hD, H', F', K'⟩ + refine ⟨h1, σ', Q', R', ?_, H', F', K'⟩ + unfold Fl4Spec.NonDet.when; rw [if_pos ⟨hthe, hxn⟩]; exact hD + · rw [if_neg h0] + have hxn : xt.tcbSchedContext ≠ none := by rw [hsct]; simp [optScA, h0] + refine (WP.spec_ok _).2 ⟨rfl, σ, Z.Q, Z.R, ?_, H.toB _, MFrA.refl _ _ _ _, fun _ y _ h => ⟨y, h, rfl⟩⟩ + unfold Fl4Spec.NonDet.when; rw [if_neg (fun h => hxn h.2)]; rfl + +end + +end + +end Fl4ProofsA64.P3.Sched + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4SpecA64 Sched +noncomputable section + +private theorem unboundDonationListsA {σ σ' : State Unit} {p t q : ObjRef} {x y : SchedContext} {n k : Nat} + {xt : Tcb} (hp : σ.kheap p = some (.schedContext x n)) (hx : x.scTcb = none) + (ht : σ.kheap t = some (.tcb xt)) + (h : schedContextDonate (Z := Unit) p t σ = ({((), σ')}, False)) + (hq : σ.kheap q = some (.schedContext y k)) : + ∃ y', σ'.kheap q = some (.schedContext y' k) ∧ y'.scReplies = y.scReplies := by + have e : σ' = donStA σ p t x n xt := by + have hs : ((), σ') ∈ (schedContextDonate (Z := Unit) p t σ).1 := by + rw [h] + exact Set.mem_singleton _ + rw [donate_specA hp ht hx] at hs + exact congrArg Prod.snd (Set.mem_singleton_iff.mp hs) + rw [e, donStA, updKSc_kheap, Function.update_of_ne (by intro e; rw [e, ht] at hq; cases hq), updKSc_kheap] + by_cases he : q = p + · subst q + rw [hp] at hq + cases hq + rw [Function.update_self] + exact ⟨_, rfl, rfl⟩ + · rw [Function.update_of_ne he] + exact ⟨y, hq, rfl⟩ + +section Store +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + +/-- The donation's local facts at the state of the call. -/ +def ReplyPopDonQA (a : A) (s : S) (_σ : State Unit) (sc t : U64) : Prop := + NotIdleScA m (scNameA m sc) ∧ L.mem s (affA t) = L.mem s (scCoreA sc) ∧ + (L.mem s (scTcbA sc) = 0#u64 ∨ ∃ f ∈ Z.T, L.mem s (scA f) = sc ∧ L.mem s (affA f) = L.cpu s ∧ + smp.remote_tcb_stall st ar s a f = ok (s, a)) + +/-- The donation's local facts at `reply_pop`'s entry: the stall at the state after the pop prefix. -/ +def ReplyPopLocalQA (a : A) (s : S) (_σ : State Unit) (sc t r : U64) : Prop := + NotIdleScA m (scNameA m sc) ∧ L.mem s (affA t) = L.mem s (scCoreA sc) ∧ + (L.mem s (scTcbA sc) = 0#u64 ∨ ∃ f ∈ Z.T, L.mem s (scA f) = sc ∧ L.mem s (affA f) = L.cpu s ∧ + ∀ s1, replyPopPrepareA (st := st) s r = ok s1 → smp.remote_tcb_stall st ar s1 a f = ok (s1, a)) + +private theorem popDonateFrameA {s : S} {σ : State Unit} {stack : U64 → List U64} + (H : SchedSRN st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + (HS : ReplyStackRelA (PopLinkMemA (L.mem s) r) R Z.C σ (scNameA m) stack) + (hr : r ∈ R) {s' : S} {σ' : State Unit} (F : MFrA (st := st) Z m s s' σ σ') + (hu : σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory) + (hsc : ∀ c ∈ Z.C, ∀ x n, σ.kheap (scNameA m c) = some (.schedContext x n) → + ∃ y k, σ'.kheap (scNameA m c) = some (.schedContext y k) ∧ y.scReplies = x.scReplies) : + EpStateRel Z.T E R Bf (L.mem s') σ' ∧ + ReplyStackRelA (PopLinkMemA (L.mem s') r) R Z.C σ' (scNameA m) stack ∧ + (∀ q ∈ R, (PopLinkMemA (L.mem s') r) (rNextA q) = (PopLinkMemA (L.mem s) r) (rNextA q) ∧ + (PopLinkMemA (L.mem s') r) (rPrevA q) = (PopLinkMemA (L.mem s) r) (rPrevA q)) := by + obtain ⟨FW, KW⟩ := epFrameOfMcsA H P F hu + have hm : ∀ q ∈ R, (PopLinkMemA (L.mem s') r) (rNextA q) = (PopLinkMemA (L.mem s) r) (rNextA q) ∧ + (PopLinkMemA (L.mem s') r) (rPrevA q) = (PopLinkMemA (L.mem s) r) (rPrevA q) := by + intro q hq + have hn := replyNextPlaceA (Z := Z) PE hq + have hp := replyPrevPlaceA (Z := Z) PE hq + have next := F.wr.mem_eq hn.1 (replyWordOffSchedA H P hq (by omega) (by omega)).2 + have prev := F.wr.mem_eq hp.1 (replyWordOffSchedA H P hq (by omega) (by omega)).2 + unfold PopLinkMemA + constructor + · by_cases e : rNextA q = rPrevA r + · simp [e] + · simp only [Function.update_of_ne e, next] + · by_cases e : rPrevA q = rPrevA r + · simp [e] + · simp only [Function.update_of_ne e, prev] + refine ⟨HE.frame PE FW KW, HS.frame hm hsc ?_, hm⟩ + intro q hq x hx + obtain ⟨y, hy, _⟩ := HE.reply q hq + have keep := F.keep.obj _ _ hy (fun _ _ eq => by cases eq) (fun _ eq => by cases eq) + exact ⟨y, hy, congrArg Reply.replySc (KernelObject.reply.inj (Option.some.inj (hx.symm.trans keep)))⟩ + +/-- The caller-SC test and optional donation, on the state after popping the SC's list. -/ +theorem replyPopDonateQA {s : S} {σ : State Unit} (a : A) {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + (HS : ReplyStackRelA (PopLinkMemA (L.mem s) r) R Z.C σ (scNameA m) stack) + (hi : ReplyIsolatedA (PopLinkMemA (L.mem s) r) Z.C stack r) + {sc t : U64} (hc : sc ∈ Z.C) (hsc0 : sc ≠ 0#u64) (ht : t ∈ Z.T) (hr : r ∈ R) + {xt : Tcb} (hk : σ.kheap (specPtr t) = some (.tcb xt)) + {l : List ObjRef} (hh : l.head? = some (specPtr r)) + (hlocal : xt.tcbSchedContext = none → ReplyPopDonQA (st := st) (ar := ar) (Z := Z) (m := m) a s σ sc t) + (hncl : ¬ inClassRSc xt.tcbState xt.tcbFault) : + (if L.mem s (scA t) = 0#u64 then schedcontext.donate st ar s a sc t else ok (s, a)) ⦃ out => + out.2 = a ∧ ∃ σ' Q' R', + Fl4Spec.NonDet.when (the l.head? = specPtr r ∧ xt.tcbSchedContext = none) + (schedContextDonate (Z := Unit) (scNameA m sc) (specPtr t)) σ = ({((), σ')}, False) ∧ + SchedSRA st { Z with Q := Q', R := R' } m κ out.1 σ' ∧ + EpStateRel Z.T E R Bf (L.mem out.1) σ' ∧ + ReplyStackRelA (PopLinkMemA (L.mem out.1) r) R Z.C σ' (scNameA m) stack ∧ + ReplyIsolatedA (PopLinkMemA (L.mem out.1) r) Z.C stack r ∧ SKeepA σ σ' ⦄ := by + obtain ⟨y, hy, hsct⟩ := H.sch.tcb t ht + rw [hk] at hy + cases hy + by_cases h0 : L.mem s (scA t) = 0#u64 + · have hxt : xt.tcbSchedContext = none := by rw [hsct, h0]; simp [optScA] + obtain ⟨hni, hmig, hfrom⟩ := hlocal hxt + have hwhen : the l.head? = specPtr r ∧ xt.tcbSchedContext = none := + ⟨by rw [hh]; rfl, hxt⟩ + obtain ⟨x, n, hx, hxrel⟩ := H.scr.sc sc hc + have finish (out : S × A) (σ' : State Unit) (Q' : U64 → U64 → List U64) (R' : U64 → List U64) + (harch : out.2 = a) (HL : SchedSRB st { Z with Q := Q', R := R' } m κ out.1 σ' (specPtr t)) + (F : MFrA (st := st) Z m s out.1 σ σ') + (hD : schedContextDonate (Z := Unit) (scNameA m sc) (specPtr t) σ = ({((), σ')}, False)) + (scKeep : ∀ q y k, σ.kheap q = some (.schedContext y k) → + ∃ y', σ'.kheap q = some (.schedContext y' k) ∧ y'.scReplies = y.scReplies) : + out.2 = a ∧ ∃ σ' Q' R', + Fl4Spec.NonDet.when (the l.head? = specPtr r ∧ xt.tcbSchedContext = none) + (schedContextDonate (Z := Unit) (scNameA m sc) (specPtr t)) σ = ({((), σ')}, False) ∧ + SchedSRA st { Z with Q := Q', R := R' } m κ out.1 σ' ∧ EpStateRel Z.T E R Bf (L.mem out.1) σ' ∧ + ReplyStackRelA (PopLinkMemA (L.mem out.1) r) R Z.C σ' (scNameA m) stack ∧ + ReplyIsolatedA (PopLinkMemA (L.mem out.1) r) Z.C stack r ∧ SKeepA σ σ' := by + have hm : ((), σ') ∈ (schedContextDonate (Z := Unit) (scNameA m sc) (specPtr t) σ).1 := by + rw [hD] + exact Set.mem_singleton _ + have hu := schedContextDonateKeepsUserMemory _ _ _ _ hm + obtain ⟨HE', HS', frame⟩ := popDonateFrameA H.toSchedSRN P PE HE HS hr F hu (fun q _ y k hy => by + obtain ⟨y', hky', hL⟩ := scKeep (scNameA m q) y k hy + exact ⟨y', k, hky', hL⟩) + obtain ⟨x2, kt2, o, ex2⟩ := F.keep.tcb _ _ hk + have H2 : SchedSRA st { Z with Q := Q', R := R' } m κ out.1 σ' := + HL.toA_ncA kt2 (by rw [ex2]; exact hncl) + refine ⟨harch, σ', Q', R', ?_, H2, HE', HS', ?_, F.keep⟩ + · unfold Fl4Spec.NonDet.when + rw [if_pos hwhen] + exact hD + · exact ⟨hi.1, (frame r hr).1.trans hi.2.1, (frame r hr).2.trans hi.2.2⟩ + rcases hfrom with hUnb | ⟨f, hf, hfs, hfon, hstall⟩ + · refine WP.spec_mono (reply_pop_donUnbA H a hc ht hsc0 hUnb hni hmig hk hh) ?_ + rintro out ⟨harch, σ', hD, HB, F⟩ + unfold Fl4Spec.NonDet.when at hD + rw [if_pos hwhen] at hD + have hx0 : x.scTcb = none := by rw [hxrel.tcb, ← scTcbA_eqSc, hUnb, optRefS_zero] + exact finish out σ' Z.Q Z.R harch HB F hD (fun _ _ _ => unboundDonationListsA hx hx0 hk hD) + · refine WP.spec_mono (reply_pop_donFromQA H (popStateWords HE) a hc hsc0 hf hfs ht hfon hstall + hmig hni hk hh) ?_ + rintro out ⟨harch, σ', Q', R', hD, HB, F, K⟩ + unfold Fl4Spec.NonDet.when at hD + rw [if_pos hwhen] at hD + exact finish out σ' Q' R' harch HB F hD (fun q y k h => K q y k h) + · rw [if_neg h0, WP.spec_ok] + have hn : xt.tcbSchedContext ≠ none := by rw [hsct]; simp [optScA, h0] + refine ⟨rfl, σ, Z.Q, Z.R, ?_, H, HE, HS, hi, SKeepA.refl _⟩ + unfold Fl4Spec.NonDet.when + rw [if_neg (fun e => hn e.2)] + rfl + +/-- The donation's local facts at the state after the pop prefix. -/ +theorem popDonAfterQA (a : A) {s s' : S} {σ : State Unit} {sc r t : U64} {x : SchedContext} {n : Nat} + (H : SchedSRA st Z m κ s σ) + (H' : SchedSRA st Z m κ s' (popStA σ (scNameA m sc) x n (specPtr r))) + (hc : sc ∈ Z.C) (ht : t ∈ Z.T) (hsc0 : sc ≠ 0#u64) + (hk : σ.kheap (scNameA m sc) = some (.schedContext x n)) + (hrun : replyPopPrepareA (st := st) s r = ok s') + (hs : ReplyPopLocalQA (st := st) (ar := ar) (Z := Z) (m := m) a s σ sc t r) : + ReplyPopDonQA (st := st) (ar := ar) (Z := Z) (m := m) a s' (popStA σ (scNameA m sc) x n (specPtr r)) sc t := by + obtain ⟨ni, mig, src⟩ := hs + refine ⟨ni, by rw [H.aff_eq H' ht ht, H'.scCore sc hc, ← H.scCore sc hc]; exact mig, ?_⟩ + rcases src with unb | ⟨f, hf, fs, fon, stall⟩ + · obtain ⟨y, k, hy, ry⟩ := H.scr.sc sc hc + rw [hk] at hy + cases hy + obtain ⟨z, j, hz, rz⟩ := H'.scr.sc sc hc + rw [popHeapScA] at hz + cases hz + have he := rz.tcb.symm.trans ry.tcb + rw [← scTcbA_eqSc, unb, optRefS_zero] at he + apply Or.inl + by_contra h + rw [optRefS, if_neg h] at he + cases he + · obtain ⟨xf, hxf, exf⟩ := H.sch.tcb f hf + obtain ⟨yf, hyf, eyf⟩ := H'.sch.tcb f hf + rw [popStA_keepsTcb (r := specPtr r) hk hxf] at hyf + cases hyf + have hfs : L.mem s' (scA f) = sc := by + apply optScA_eq_of (m := m) + · rw [← eyf, exf, fs] + · rw [optScA, if_neg hsc0] + exact fun h => ni.1 (Option.some.inj h) + exact Or.inr ⟨f, hf, hfs, by rw [H.aff_eq H' hf hf, H.cpu_eq H']; exact fon, stall s' hrun⟩ + +/-- Full head pop, the caller blocked on the reply: no `hpost`, the SC holder queued or not. -/ +theorem reply_pop_relQA {s : S} {σ : State Unit} (a : A) {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {sc r t : U64} (hc : sc ∈ Z.C) (hsc0 : sc ≠ 0#u64) (ht : t ∈ Z.T) + {rest : List U64} (hl : stack sc = r :: rest) + {xt : Tcb} {xr : Reply} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hkr : σ.kheap (specPtr r) = some (.reply xr)) (hb : xr.replyTcb = some (specPtr t)) + (hst : xt.tcbState = .blockedOnReply (specPtr r) ∨ + ∃ ep d, xt.tcbState = .blockedOnReceive ep (some (specPtr r)) d) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) + (hlocal : xt.tcbSchedContext = none → ReplyPopLocalQA (st := st) (ar := ar) (Z := Z) (m := m) a s σ sc t r) + (hncl : ¬ inClassRSc xt.tcbState xt.tcbFault) : + ipc.reply_pop st ar s a r t ⦃ out => out.2 = a ∧ ∃ σ', + replyPopSpecA (scNameA m sc) (specPtr t) (specPtr r) ((stack sc).map specPtr) xt.tcbSchedContext σ = + ({((), σ')}, False) ∧ + IpcSchedRelA st Z m κ out.1 σ' ∧ EpStateRel Z.T E R Bf (L.mem out.1) σ' ∧ + ReplyStackRelA (L.mem out.1) R Z.C σ' (scNameA m) (Function.update stack sc rest) ∧ + ReplyIsolatedA (L.mem out.1) Z.C (Function.update stack sc rest) r ⦄ := by + have hr := (HS.stackRel sc hc).2.1 r (by simp [hl]) + have nx : obj.Reply.next st r s = ok sc := + eq_of_spec_fd (WP.spec_mono (obj.Reply.next.spec s r) fun _ h => h.trans (replyHeadWordsA H PE HS hc hl).2.1) + rw [reply_pop_programA (ar := ar) s a r t sc nx] + obtain ⟨s1, hrun, x, n, hk, hU, H1, E1, S1, iso1, v1⟩ := spec_existsA (replyPopPrepareRefA H P PE HE HS hc hl) + rw [hrun, bind_ok] + let σ1 := popStA σ (scNameA m sc) x n (specPtr r) + have kt1 : σ1.kheap (specPtr t) = some (.tcb xt) := popStA_keepsTcb hk hkt + have h1 : ((stack sc).map specPtr).head? = some (specPtr r) := by rw [hl]; rfl + refine WP.spec_bind (obj.Tcb.sc.spec s1 t) ?_ + rintro i rfl + refine WP.spec_bind (replyPopDonateQA (ar := ar) a H1 P PE E1 S1 iso1 hc hsc0 ht hr kt1 h1 + (fun hn => popDonAfterQA a H H1 hc ht hsc0 hk hrun (hlocal hn)) hncl) ?_ + rintro ⟨s2, a2⟩ ⟨ha, σ2, Q2, R2, hD, H2, E2, S2, iso2, keep⟩ + obtain ⟨x2, kt2, c, ex2⟩ := keep.tcb _ _ kt1 + have kp := popStA_epView hk (specPtr r) + obtain ⟨y, kr1, yr⟩ := kp.2.1 _ _ hkr + have kr2 := keep.obj _ _ kr1 (fun _ _ e => (by cases e)) (fun _ e => (by cases e)) + have hq2 : ∀ e ∈ E, ∀ z, σ2.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue := by + intro e he z hz + obtain ⟨z0, hz0, _⟩ := HE.ep e he + have hz2 := keep.obj _ _ (kp.1 _ _ hz0) (fun _ _ e => (by cases e)) (fun _ e => (by cases e)) + rw [hz] at hz2 + cases hz2 + exact hq e he _ hz0 + refine WP.spec_bind (finishPoppedReplyA H2 P PE E2 hr ht S2 iso2 kt2 kr2 (yr.trans hb) + (by rw [ex2]; exact hst) hq2) ?_ + rintro s3 ⟨σ3, hF, H3, E3, S3, iso3⟩ + rw [WP.spec_ok] + refine ⟨ha, σ3, ?_, ⟨Q2, R2, H3⟩, E3, S3, iso3⟩ + unfold replyPopSpecA + rw [nd_bind_oneSc hU, nd_bind_oneSc hD] + exact hF + +private theorem replyRemoveAtHeadQA {s : S} {σ : State Unit} (a : A) {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {sc r t : U64} (hc : sc ∈ Z.C) (hsc0 : sc ≠ 0#u64) (ht : t ∈ Z.T) + {rest : List U64} (hl : stack sc = r :: rest) + (cov : ScReplyCovA Z.C m σ (specPtr r)) + {xt : Tcb} {xr : Reply} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hkr : σ.kheap (specPtr r) = some (.reply xr)) (hb : xr.replyTcb = some (specPtr t)) + (hst : xt.tcbState = .blockedOnReply (specPtr r) ∨ + ∃ ep d, xt.tcbState = .blockedOnReceive ep (some (specPtr r)) d) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) + (hlocal : xt.tcbSchedContext = none → ReplyPopLocalQA (st := st) (ar := ar) (Z := Z) (m := m) a s σ sc t r) + (hncl : ¬ inClassRSc xt.tcbState xt.tcbFault) : + ipc.reply_remove st ar s a r t ⦃ out => out.2 = a ∧ ∃ σ', + replyRemove (Z := Unit) (specPtr t) (specPtr r) σ = ({((), σ')}, False) ∧ + IpcSchedRelA st Z m κ out.1 σ' ∧ EpStateRel Z.T E R Bf (L.mem out.1) σ' ∧ + ReplyStackRelA (L.mem out.1) R Z.C σ' (scNameA m) (Function.update stack sc rest) ∧ + ReplyIsolatedA (L.mem out.1) Z.C (Function.update stack sc rest) r ⦄ := by + obtain ⟨rwNext, decNext, _⟩ := replyHeadWordsA H PE HS hc hl + have nx := (eq_of_spec_fd (obj.Reply.next.spec (L := L) s r)).trans (congrArg ok decNext) + have hh := nx_head1 (L.mem s (rNextA r)) + rw [decNext, ← rwNext] at hh + rw [replyRemoveHeadProgramA (ar := ar) s a r t sc nx hsc0 hh] + obtain ⟨⟨x, n, hk, hL⟩, _⟩ := HS.stackRel sc hc + have hm : r ∈ stack sc := by simp [hl] + rw [replyRemoveSomeA hkr hb (HS.scWithReply_some hc hm cov) hk hkt, hL] + exact reply_pop_relQA (ar := ar) a H P PE HE HS hc hsc0 ht hl hkt hkr hb hst hq hlocal hncl + +/-- Refine l4v `reply_remove` for a callee outside `inClassR` (blocked on its reply): `reply_remove_relA` without +`hpost`, the SC holder queued or not. -/ +theorem reply_remove_relQA {s : S} {σ : State Unit} (a : A) {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {r t : U64} (hr : r ∈ R) (ht : t ∈ Z.T) (cov : ScReplyCovA Z.C m σ (specPtr r)) + {xt : Tcb} {xr : Reply} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hkr : σ.kheap (specPtr r) = some (.reply xr)) (hb : xr.replyTcb = some (specPtr t)) + (hst : xt.tcbState = .blockedOnReply (specPtr r) ∨ + ∃ ep d, xt.tcbState = .blockedOnReceive ep (some (specPtr r)) d) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) + (hsc0 : ∀ sc ∈ Z.C, (stack sc).head? = some r → sc ≠ 0#u64) + (hlocal : ∀ sc ∈ Z.C, (stack sc).head? = some r → xt.tcbSchedContext = none → + ReplyPopLocalQA (st := st) (ar := ar) (Z := Z) (m := m) a s σ sc t r) + (hncl : ¬ inClassRSc xt.tcbState xt.tcbFault) : + ipc.reply_remove st ar s a r t ⦃ out => out.2 = a ∧ ∃ σ' stack', + replyRemove (Z := Unit) (specPtr t) (specPtr r) σ = ({((), σ')}, False) ∧ + IpcSchedRelA st Z m κ out.1 σ' ∧ EpStateRel Z.T E R Bf (L.mem out.1) σ' ∧ + ReplyStackRelA (L.mem out.1) R Z.C σ' (scNameA m) stack' ∧ + ReplyIsolatedA (L.mem out.1) Z.C stack' r ⦄ := by + classical + by_cases hd : ∀ sc ∈ Z.C, r ∉ stack sc + · refine WP.spec_mono (replyRemoveDetachedRefA (ar := ar) a ⟨Z.Q, Z.R, H⟩ P PE HE HS hr ht hd + cov hkt hkr hb hst hq) ?_ + rintro out ⟨ha, σ', hu, hS, hE, hR, hi⟩ + exact ⟨ha, σ', stack, hu, hS, hE, hR, hi⟩ + · have hLive : ∃ sc ∈ Z.C, r ∈ stack sc := by push_neg at hd; exact hd + obtain ⟨sc, hc, hm⟩ := hLive + obtain ⟨k, hk, he⟩ := List.getElem_of_mem hm + cases k with + | zero => + have hh : (stack sc).head? = some r := by + rw [List.head?_eq_getElem?, List.getElem?_eq_getElem hk, he] + obtain ⟨rest, hl⟩ : ∃ rest, stack sc = r :: rest := by + cases ee : stack sc with + | nil => rw [ee] at hh; cases hh + | cons d l => + rw [ee] at hh + cases hh + exact ⟨l, rfl⟩ + refine WP.spec_mono (replyRemoveAtHeadQA (ar := ar) a H P PE HE HS hc (hsc0 sc hc hh) ht hl cov + hkt hkr hb hst hq (hlocal sc hc hh) hncl) ?_ + rintro out ⟨ha, σ', hu, hS, hE, hR, hi⟩ + exact ⟨ha, σ', Function.update stack sc rest, hu, hS, hE, hR, hi⟩ + | succ k => + refine WP.spec_mono (replyRemoveCutRefA (ar := ar) a ⟨Z.Q, Z.R, H⟩ P PE HE HS hc ht hk he cov + hkt hkr hb hst hq) ?_ + rintro out ⟨ha, σ', hu, hS, hE, hR, hi⟩ + exact ⟨ha, σ', Function.update stack sc ((stack sc).take (k + 1)), hu, hS, hE, hR, hi⟩ + +/-- `reply_remove_relQA` with the new stacks contained in the old ones. -/ +theorem reply_remove_relQA_sub {s : S} {σ : State Unit} (a : A) {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {r t : U64} (hr : r ∈ R) (ht : t ∈ Z.T) (cov : ScReplyCovA Z.C m σ (specPtr r)) + {xt : Tcb} {xr : Reply} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hkr : σ.kheap (specPtr r) = some (.reply xr)) (hb : xr.replyTcb = some (specPtr t)) + (hst : xt.tcbState = .blockedOnReply (specPtr r) ∨ + ∃ ep d, xt.tcbState = .blockedOnReceive ep (some (specPtr r)) d) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) + (hsc0 : ∀ sc ∈ Z.C, (stack sc).head? = some r → sc ≠ 0#u64) + (hlocal : ∀ sc ∈ Z.C, (stack sc).head? = some r → xt.tcbSchedContext = none → + ReplyPopLocalQA (st := st) (ar := ar) (Z := Z) (m := m) a s σ sc t r) + (hncl : ¬ inClassRSc xt.tcbState xt.tcbFault) : + ipc.reply_remove st ar s a r t ⦃ out => out.2 = a ∧ ∃ σ' stack', + replyRemove (Z := Unit) (specPtr t) (specPtr r) σ = ({((), σ')}, False) ∧ + IpcSchedRelA st Z m κ out.1 σ' ∧ EpStateRel Z.T E R Bf (L.mem out.1) σ' ∧ + ReplyStackRelA (L.mem out.1) R Z.C σ' (scNameA m) stack' ∧ + ReplyIsolatedA (L.mem out.1) Z.C stack' r ∧ (∀ c, ∀ q ∈ stack' c, q ∈ stack c) ⦄ := by + classical + by_cases hd : ∀ sc ∈ Z.C, r ∉ stack sc + · refine WP.spec_mono (replyRemoveDetachedRefA (ar := ar) a ⟨Z.Q, Z.R, H⟩ P PE HE HS hr ht hd + cov hkt hkr hb hst hq) ?_ + rintro out ⟨ha, σ', hu, hS, hE, hR, hi⟩ + exact ⟨ha, σ', stack, hu, hS, hE, hR, hi, fun _ _ h => h⟩ + · have hLive : ∃ sc ∈ Z.C, r ∈ stack sc := by push_neg at hd; exact hd + obtain ⟨sc, hc, hm⟩ := hLive + obtain ⟨k, hk, he⟩ := List.getElem_of_mem hm + cases k with + | zero => + have hh : (stack sc).head? = some r := by + rw [List.head?_eq_getElem?, List.getElem?_eq_getElem hk, he] + obtain ⟨rest, hl⟩ : ∃ rest, stack sc = r :: rest := by + cases ee : stack sc with + | nil => rw [ee] at hh; cases hh + | cons d l => + rw [ee] at hh + cases hh + exact ⟨l, rfl⟩ + refine WP.spec_mono (replyRemoveAtHeadQA (ar := ar) a H P PE HE HS hc (hsc0 sc hc hh) ht hl cov + hkt hkr hb hst hq (hlocal sc hc hh) hncl) ?_ + rintro out ⟨ha, σ', hu, hS, hE, hR, hi⟩ + refine ⟨ha, σ', Function.update stack sc rest, hu, hS, hE, hR, hi, fun c q hq => ?_⟩ + by_cases e : c = sc + · subst e + rw [Function.update_self] at hq + rw [hl]; exact List.mem_cons_of_mem _ hq + · rwa [Function.update_of_ne e] at hq + | succ k => + refine WP.spec_mono (replyRemoveCutRefA (ar := ar) a ⟨Z.Q, Z.R, H⟩ P PE HE HS hc ht hk he cov + hkt hkr hb hst hq) ?_ + rintro out ⟨ha, σ', hu, hS, hE, hR, hi⟩ + refine ⟨ha, σ', Function.update stack sc ((stack sc).take (k + 1)), hu, hS, hE, hR, hi, fun c q hq => ?_⟩ + by_cases e : c = sc + · subst e + rw [Function.update_self] at hq + exact List.mem_of_mem_take hq + · rwa [Function.update_of_ne e] at hq + +end Store +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/ReplyRemoveTopFootA.lean b/proofs/Fl4ProofsA64/P3/ReplyRemoveTopFootA.lean new file mode 100644 index 000000000..3fe6d9b39 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ReplyRemoveTopFootA.lean @@ -0,0 +1,339 @@ +/- +P3 (AArch64): the concrete write footprint of the whole reply removal. + +`reply_remove_footA` takes the hypotheses of `reply_remove_relA` and states +`WritesIn` over `ReplyFootW`: the endpoint words, the MCS (SC and scheduler) +words, and the reply link words. The three arms (head pop, live-stack cut, +detached splice) each reuse the relational lemma that already reached the +state in which the next concrete step runs. +-/ +import Fl4ProofsA64.P3.ReplyPopFootA +import Fl4ProofsA64.P3.ReplyRemoveKeepA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4SpecA64 Sched + +noncomputable section + +section Store +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + +theorem replyMaskFA (PE : EPlaced Z.T E R Bf) (r : U64) + (hr : r ≠ 0#u64 → r ∈ R) : r &&& ~~~1#u64 = r := by + apply mask1_of_even + by_cases hz : r = 0#u64 + · subst r; decide + · exact replyPointerEvenA PE (hr hz) + +theorem nodeAt_cvtA {s s' : S} {B : U64 → Prop} (w : WritesIn st s s' B) {b o : U64} + (h : b = L.nodeAt s' (L.cpu s') o) : b = L.nodeAt s (L.cpu s) o := by + rw [h, nodeAt_of_ksb w.1, w.2.1] + +theorem cutPopSpecFA {σ σ1 : State Unit} {p t r h : ObjRef} {l : List ObjRef} {o : Option ObjRef} + (hh : l.head? = some h) (hne : h ≠ r) + (hu : replyUnlinkSc (Z := Unit) p r σ = ({((), σ1)}, False)) : + replyPopSpecA p t r l o σ = replyUnlinkTcb t r σ1 := by + unfold replyPopSpecA + rw [nd_bind_oneSc hu, hh] + simp only [Fl4Spec.NonDet.when, show the (some h) = h from rfl, hne, false_and, ↓reduceIte] + exact nd_bind_oneSc (show Fl4Spec.NonDet.pure () σ1 = ({((), σ1)}, False) from rfl) _ + +/-- A copy of the link-clear step: write zero into one reply's NEXT or PREV word. -/ +def clearLinkFA (s : S) (next : Bool) (r : U64) : Result S := + if (r != 0#u64) = true then + if next then obj.Reply.set_next st r s 0#u64 false else obj.Reply.set_prev st r s 0#u64 + else ok s + +theorem clearLinkFootA (PE : EPlaced Z.T E R Bf) (s : S) (next : Bool) {r : U64} + (hr : r ≠ 0#u64 → r ∈ R) : + clearLinkFA (st := st) s next r ⦃ s' => WritesIn st s s' (RLinkW R) ⦄ := by + by_cases h0 : r = 0#u64 + · unfold clearLinkFA + rw [show (r != 0#u64) = false by simp [h0]] + exact (WP.spec_ok _).2 (WritesIn.refl _ _) + · have hR := hr h0 + unfold clearLinkFA + rw [show (r != 0#u64) = true by simpa using h0] + cases next + · rw [setPrevZeroEq] + refine (WP.spec_ok _).2 ((writesIn_wr (replyPrevPlaceA (Z := Z) PE hR).1).mono fun b hb => ?_) + exact ⟨r, hR, Or.inr hb⟩ + · rw [setNextZeroEq] + refine (WP.spec_ok _).2 ((writesIn_wr (replyNextPlaceA (Z := Z) PE hR).1).mono fun b hb => ?_) + exact ⟨r, hR, Or.inl hb⟩ + +/-- The splice prefix writes only reply link words. -/ +theorem replySplicePrepareFootA (PE : EPlaced Z.T E R Bf) (s : S) {r nx pv : U64} (hr : r ∈ R) + (hn : nx ≠ 0#u64 → nx ∈ R) (hp : pv ≠ 0#u64 → pv ∈ R) : + replySplicePrepareA (st := st) s r nx pv ⦃ s' => WritesIn st s s' (RLinkW R) ⦄ := by + have hr0 : r ≠ 0#u64 := fun e => (PE.rep r hr).2.1 (by rw [e]; rfl) + show (do + let s1 ← clearLinkFA (st := st) s false nx + let s2 ← clearLinkFA (st := st) s1 true pv + let s3 ← obj.Reply.set_prev st r s2 0#u64 + obj.Reply.set_next st r s3 0#u64 false) ⦃ s' => WritesIn st s s' (RLinkW R) ⦄ + refine WP.spec_bind (clearLinkFootA PE s false hn) ?_ + intro s1 w1 + refine WP.spec_bind (clearLinkFootA PE s1 true hp) ?_ + intro s2 w2 + rw [setPrevZeroEq, bind_ok, setNextZeroEq] + refine (WP.spec_ok _).2 ?_ + have w3 := writesIn_wr (st := st) (s := s2) (v := 0#u64) (replyPrevPlaceA (Z := Z) PE hr).1 + have w4 := writesIn_wr (st := st) (s := L.wr s2 (rPrevA r) 0#u64) (v := 0#u64) + (replyNextPlaceA (Z := Z) PE hr).1 + refine (((w1.trans w2).trans w3).trans w4).mono fun b hb => ?_ + rcases hb with ((h | h) | h) | h + · exact h + · exact h + · exact ⟨r, hr, Or.inr h⟩ + · exact ⟨r, hr, Or.inl h⟩ + +/-- A splice arm: prefix link clears, then the unlink at the (possibly cut) spec state. -/ +theorem replySpliceArmFootA {s : S} {σ : State Unit} (a : A) + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) {r t nx pv : U64} (hr : r ∈ R) (ht : t ∈ Z.T) + (hn : nx ≠ 0#u64 → nx ∈ R) (hp : pv ≠ 0#u64 → pv ∈ R) + {x : Tcb} {xr : Reply} (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hkr : σ.kheap (specPtr r) = some (.reply xr)) + (hst : x.tcbState = .blockedOnReply (specPtr r) ∨ + ∃ ep d, x.tcbState = .blockedOnReceive ep (some (specPtr r)) d) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) : + (do + let s1 ← replySplicePrepareA (st := st) s r nx pv + let s2 ← ipc.reply_unlink st s1 r t + ok (s2, a)) ⦃ out => WritesIn st s out.1 (fun b => RLinkW R b ∨ EWrite Z.T E R Bf b ∨ + b = L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) ⦄ := by + refine WP.spec_bind (WP.spec_and (replySplicePrepareRefA H P PE HE hr hn hp) + (replySplicePrepareFootA PE s hr hn hp)) ?_ + rintro s1 ⟨⟨H1, E1, -⟩, w1⟩ + refine WP.spec_bind (replyUnlinkFootA H1 P PE E1 ht hr hk hkr hst hq) ?_ + intro s2 w2 + refine (WP.spec_ok _).2 ?_ + refine (w1.trans w2).mono fun b hb => ?_ + rcases hb with h | h | h + · exact Or.inl h + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr (nodeAt_cvtA w1 h)) + +theorem footW_of_arm {s : S} {σ : State Unit} (H : SchedSRA st Z m κ s σ) {b : U64} + (h : RLinkW R b ∨ EWrite Z.T E R Bf b ∨ b = L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) : + ReplyFootW (st := st) Z m σ s E R Bf b := by + rcases h with h | h | h + · exact Or.inr (Or.inr h) + · exact Or.inl h + · exact Or.inr (Or.inl (Or.inr (schedAction_swrA H h))) + +/-- Detached splice arm. -/ +theorem replyRemoveDetachedFootA {s : S} {σ : State Unit} (a : A) {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {r t : U64} (hr : r ∈ R) (ht : t ∈ Z.T) (hd : ∀ sc ∈ Z.C, r ∉ stack sc) + {xt : Tcb} {xr : Reply} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hkr : σ.kheap (specPtr r) = some (.reply xr)) + (hst : xt.tcbState = .blockedOnReply (specPtr r) ∨ + ∃ ep d, xt.tcbState = .blockedOnReceive ep (some (specPtr r)) d) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) + (hb : xr.replyTcb = some (specPtr t)) (cov : ScReplyCovA Z.C m σ (specPtr r)) : + ipc.reply_remove st ar s a r t ⦃ out => + WritesIn st s out.1 (ReplyFootW (st := st) Z m σ s E R Bf) ∧ ∃ σ', + replyRemove (Z := Unit) (specPtr t) (specPtr r) σ = ({((), σ')}, False) ∧ RemKeepA σ σ' ⦄ := by + obtain ⟨hh, hn, hp⟩ := HS.detached r hr hd + have hnR : L.mem s (rNextA r) ≠ 0#u64 → L.mem s (rNextA r) ∈ R := fun h => (hn h).1 + have hpR : L.mem s (rPrevA r) ≠ 0#u64 → L.mem s (rPrevA r) ∈ R := fun h => (hp h).1 + have en := (eq_of_spec_fd (obj.Reply.next.spec (L := L) s r)).trans + (congrArg ok (replyMaskFA (Z := Z) PE _ hnR)) + have ep := (eq_of_spec_fd (obj.Reply.prev.spec (L := L) s r)).trans + (congrArg ok (replyMaskFA (Z := Z) PE _ hpR)) + rw [replyRemoveSpliceProgramA (ar := ar) s a r t _ _ en ep hh] + obtain ⟨σ2, hu, hkeep⟩ := replyUnlinkKeepA hkt hkr hb hst + refine WP.spec_mono (replySpliceArmFootA a H P PE HE hr ht hnR hpR hkt hkr hst hq) ?_ + intro out w + exact ⟨w.mono fun b hb' => footW_of_arm H hb', σ2, + (replyRemoveNoneA hkr hb (HS.scWithReply_none hd cov)).trans hu, hkeep⟩ + +private theorem cutReadNextFA {s : S} {l : List U64} {sc : U64} + (hW : ReplyStackWordsA (L.mem s) sc l) (hR : ∀ d ∈ l, d ∈ R) (PE : EPlaced Z.T E R Bf) + {k : Nat} (hk : k + 1 < l.length) : + obj.Reply.next st l[k + 1] s = ok l[k] ∧ headBit (L.mem s (rNextA l[k + 1])) = false := by + have hraw : L.mem s (rNextA l[k + 1]) = l.getD k 0#u64 := (hW (k + 1) hk).2 + rw [List.getD_eq_getElem _ _ (by omega)] at hraw + have he := replyPointerEvenA (Z := Z) PE (hR _ (List.getElem_mem (by omega : k < l.length))) + exact ⟨(eq_of_spec_fd (obj.Reply.next.spec s _)).trans (by rw [hraw, mask1_of_even he]), + by rw [hraw]; exact headBitEvenA he⟩ + +private theorem cutReadPrevFA {s : S} {l : List U64} {sc : U64} + (hW : ReplyStackWordsA (L.mem s) sc l) (hR : ∀ d ∈ l, d ∈ R) (PE : EPlaced Z.T E R Bf) + {k : Nat} (hk : k < l.length) : + obj.Reply.prev st l[k] s = ok (l.getD (k + 1) 0#u64) ∧ + (l.getD (k + 1) 0#u64 ≠ 0#u64 → l.getD (k + 1) 0#u64 ∈ R) := by + have hraw := (hW k hk).1 + have hpa : l.getD (k + 1) 0#u64 ≠ 0#u64 → l.getD (k + 1) 0#u64 ∈ R := by + intro h + by_cases hlt : k + 1 < l.length + · rw [List.getD_eq_getElem _ _ hlt] + exact hR _ (List.getElem_mem hlt) + · rw [List.getD_eq_default _ _ (by omega)] at h + exact (h rfl).elim + refine ⟨(eq_of_spec_fd (obj.Reply.prev.spec s _)).trans ?_, hpa⟩ + rw [hraw] + by_cases h : l.getD (k + 1) 0#u64 = 0#u64 + · rw [h, zero_or_masked] + · rw [mask1_of_even (replyPointerEvenA (Z := Z) PE (hpa h))] + +/-- Cut arm. -/ +theorem replyRemoveCutFootA {s : S} {σ : State Unit} (a : A) {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {sc t : U64} (hc : sc ∈ Z.C) (ht : t ∈ Z.T) + {k : Nat} (hk : k + 1 < (stack sc).length) + {xt : Tcb} {xr : Reply} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hkr : σ.kheap (specPtr (stack sc)[k + 1]) = some (.reply xr)) + (hst : xt.tcbState = .blockedOnReply (specPtr (stack sc)[k + 1]) ∨ + ∃ ep d, xt.tcbState = .blockedOnReceive ep (some (specPtr (stack sc)[k + 1])) d) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) + (hb : xr.replyTcb = some (specPtr t)) (cov : ScReplyCovA Z.C m σ (specPtr (stack sc)[k + 1])) : + ipc.reply_remove st ar s a (stack sc)[k + 1] t ⦃ out => + WritesIn st s out.1 (ReplyFootW (st := st) Z m σ s E R Bf) ∧ ∃ σ', + replyRemove (Z := Unit) (specPtr t) (specPtr (stack sc)[k + 1]) σ = ({((), σ')}, False) ∧ + RemKeepA σ σ' ⦄ := by + obtain ⟨⟨x, n, hx, hL⟩, hR, hnd, hW⟩ := HS.stackRel sc hc + have hrM : (stack sc)[k + 1] ∈ stack sc := List.getElem_mem hk + have hrR := hR _ hrM + have hprevR : (stack sc)[k] ∈ R := hR _ (List.getElem_mem (by omega : k < (stack sc).length)) + have h0 : 0 < (stack sc).length := by omega + have hHead : (stack sc).head? = some (stack sc)[0] := by + rw [List.head?_eq_getElem?, List.getElem?_eq_getElem h0] + have hne : (stack sc)[0] ≠ (stack sc)[k + 1] := by + intro e + have j := (List.nodup_iff_injective_get.1 hnd) (a₁ := ⟨0, h0⟩) (a₂ := ⟨k + 1, hk⟩) + (by simpa using e) + exact Nat.ne_of_lt (Nat.zero_lt_succ k) (congrArg Fin.val j) + have hrs : xr.replySc = none := by + cases ho : xr.replySc with + | none => rfl + | some p => + obtain ⟨c, hc', _, hh⟩ := (HS.owner _ hrR xr hkr p).1 ho + have e := HS.unique c hc' sc hc _ (List.mem_of_mem_head? hh) hrM + subst c + exact (hne (Option.some.inj (hHead.symm.trans hh))).elim + have hh : x.scReplies.head? = some (specPtr (stack sc)[0]) := by rw [hL, List.head?_map, hHead]; rfl + have hnePtr : specPtr (stack sc)[0] ≠ specPtr (stack sc)[k + 1] := fun e => hne (specPtr_inj e) + obtain ⟨hU, HS1, F⟩ := reply_remove_cutA H hc hkr hrs hx hh hnePtr + let σ1 := repStA σ (scNameA m sc) x n (x.scReplies.takeWhile fun r => decide (r ≠ specPtr (stack sc)[k + 1])) + have hof q (he : q ≠ scNameA m sc) : σ1.kheap q = σ.kheap q := repStA_ne he + have kt1 : σ1.kheap (specPtr t) = some (.tcb xt) := by + rw [hof _ (by intro e; rw [e, hx] at hkt; cases hkt)] + exact hkt + have kr1 : σ1.kheap (specPtr (stack sc)[k + 1]) = some (.reply xr) := by + rw [hof _ (by intro e; rw [e, hx] at hkr; cases hkr)] + exact hkr + have hq1 : ∀ e ∈ E, ∀ z, σ1.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue := by + intro e he z hz + obtain ⟨z0, hz0, _⟩ := HE.ep e he + rw [hof _ (by intro e'; rw [e', hx] at hz0; cases hz0), hz0] at hz + cases hz + exact hq e he _ hz0 + have E1 := HE.frame PE (fun _ _ _ => rfl) (epFrameOfMcsA H.toSchedSRN P F rfl).2 + obtain ⟨en, eb⟩ := cutReadNextFA (Z := Z) hW hR PE hk + obtain ⟨ep, hp⟩ := cutReadPrevFA (Z := Z) hW hR PE hk + rw [replyRemoveSpliceProgramA (ar := ar) s a _ t _ _ en ep eb] + have heq : replyRemove (Z := Unit) (specPtr t) (specPtr (stack sc)[k + 1]) σ = + replyUnlinkTcb (Z := Unit) (specPtr t) (specPtr (stack sc)[k + 1]) σ1 := by + rw [replyRemoveSomeA hkr hb (HS.scWithReply_some hc hrM cov) hx hkt, cutPopSpecFA hh hnePtr hU] + obtain ⟨σ2, hu, hkeep⟩ := replyUnlinkKeepA kt1 kr1 hb hst + refine WP.spec_mono (replySpliceArmFootA a HS1 P PE E1 hrR ht (fun _ => hprevR) hp kt1 kr1 hst hq1) ?_ + intro out w + exact ⟨w.mono fun b hb' => footW_of_arm H hb', σ2, heq.trans hu, + (RemKeepA.of_repSt _ hx).trans hkeep⟩ + +/-- Head arm: the pop footprint. -/ +theorem replyPopFootA {s : S} {σ : State Unit} (a : A) {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {sc r t : U64} (hc : sc ∈ Z.C) (hsc0 : sc ≠ 0#u64) (ht : t ∈ Z.T) + {rest : List U64} (hl : stack sc = r :: rest) + {xt : Tcb} {xr : Reply} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hkr : σ.kheap (specPtr r) = some (.reply xr)) (hb : xr.replyTcb = some (specPtr t)) + (hst : xt.tcbState = .blockedOnReply (specPtr r) ∨ + ∃ ep d, xt.tcbState = .blockedOnReceive ep (some (specPtr r)) d) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) + (hlocal : xt.tcbSchedContext = none → ReplyPopLocalA (st := st) (Z := Z) (m := m) s σ sc t) + (hpost : ∀ σ', ((replyUnlinkSc (Z := Unit) (scNameA m sc) (specPtr r)) >>= fun _ => + schedContextDonate (scNameA m sc) (specPtr t)) σ = ({((), σ')}, False) → RelIfBoundSc σ' (specPtr t)) : + ipc.reply_pop st ar s a r t ⦃ out => + WritesIn st s out.1 (ReplyFootW (st := st) Z m σ s E R Bf) ∧ ∃ σ', + replyPopSpecA (scNameA m sc) (specPtr t) (specPtr r) ((stack sc).map specPtr) xt.tcbSchedContext σ = + ({((), σ')}, False) ∧ RemKeepA σ σ' ⦄ := by + have hr := (HS.stackRel sc hc).2.1 r (by simp [hl]) + have nx : obj.Reply.next st r s = ok sc := + eq_of_spec_fd (WP.spec_mono (obj.Reply.next.spec s r) fun _ h => h.trans (replyHeadWordsA H PE HS hc hl).2.1) + rw [reply_pop_programA (ar := ar) s a r t sc nx] + refine WP.spec_bind (WP.spec_and (replyPopPrepareRefA H P PE HE HS hc hl) + (replyPopPrepFootA H PE HE HS hc hl)) ?_ + rintro s1 ⟨⟨x, n, hk, hU, H1, E1, S1, iso1, v1⟩, ⟨x', n', hk', hsz, w1⟩⟩ + rw [hk] at hk' + cases hk' + let σ1 := popStA σ (scNameA m sc) x n (specPtr r) + have kt1 : σ1.kheap (specPtr t) = some (.tcb xt) := popStA_keepsTcb hk hkt + have h1 : ((stack sc).map specPtr).head? = some (specPtr r) := by rw [hl]; rfl + have local1 : xt.tcbSchedContext = none → ReplyPopLocalA (st := st) (Z := Z) (m := m) s1 σ1 sc t := + fun hn => popLocalAfterFA H H1 hc ht hsc0 hk v1 (hlocal hn) + refine WP.spec_bind (obj.Tcb.sc.spec s1 t) ?_ + rintro i rfl + refine WP.spec_bind (WP.spec_and (replyPopDonateRefA (ar := ar) a H1 P PE E1 S1 iso1 hc hsc0 ht hr kt1 h1 + local1 (fun σ2 hD => hpost σ2 (by rw [nd_bind_oneSc hU, hD]))) + (replyPopDonateFootA (ar := ar) a H1 PE E1 hc hsc0 ht kt1 h1 local1)) ?_ + rintro ⟨s2, a2⟩ ⟨⟨ha, σ2, Q2, hD, H2, E2, S2, iso2, keep⟩, w2⟩ + obtain ⟨x2, kt2, c, ex2⟩ := keep.tcb _ _ kt1 + have kp := popStA_epView hk (specPtr r) + obtain ⟨y, kr1, yr⟩ := kp.2.1 _ _ hkr + have kr2 := keep.obj _ _ kr1 (fun _ _ e => (by cases e)) (fun _ e => (by cases e)) + have hq2 : ∀ e ∈ E, ∀ z, σ2.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue := by + intro e he z hz + obtain ⟨z0, hz0, _⟩ := HE.ep e he + have hz2 := keep.obj _ _ (kp.1 _ _ hz0) (fun _ _ e => (by cases e)) (fun _ e => (by cases e)) + rw [hz] at hz2 + cases hz2 + exact hq e he _ hz0 + have hmem2 : σ2.machineState.underlyingMemory = σ1.machineState.underlyingMemory := by + unfold Fl4Spec.NonDet.when at hD + by_cases hc' : (the ((stack sc).map specPtr).head? = specPtr r ∧ xt.tcbSchedContext = none) + · rw [if_pos hc'] at hD + exact schedContextDonateKeepsUserMemory _ _ _ _ (by rw [hD]; exact Set.mem_singleton _) + · rw [if_neg hc'] at hD + have e : σ2 = σ1 := det_outA hD (show Fl4Spec.NonDet.pure () σ1 = ({((), σ1)}, False) from rfl) + rw [e] + have hst2 : x2.tcbState = .blockedOnReply (specPtr r) ∨ + ∃ ep d, x2.tcbState = .blockedOnReceive ep (some (specPtr r)) d := by rw [ex2]; exact hst + refine WP.spec_bind (WP.spec_and + (finishPoppedReplyA H2 P PE E2 hr ht S2 iso2 kt2 kr2 (yr.trans hb) hst2 hq2) + (finishPoppedFootA H2 P PE E2 hr ht kt2 kr2 hst2 hq2)) ?_ + rintro s3 ⟨⟨σ3, hF, -⟩, w3⟩ + have w12 := w1.trans (w2.mono fun _ h => h) + have w123 := w12.trans w3 + refine (WP.spec_ok _).2 ⟨?_, σ3, ?_, ?_⟩ + · refine w123.mono fun b hb => ?_ + rcases hb with ((h | h) | h) | h | h | h + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr h) + · exact Or.inr (Or.inl (mwr_back rfl rfl hsz w1.1 h)) + · exact Or.inl h + · exact Or.inr (Or.inl (Or.inr (schedAction_swrA H (nodeAt_cvtA w12 h)))) + · exact Or.inr (Or.inr ⟨r, hr, Or.inr h⟩) + · unfold replyPopSpecA + rw [nd_bind_oneSc hU, nd_bind_oneSc hD] + exact hF + · have eU := unlinkTcbSpecFA kt2 kr2 (yr.trans hb) hst2 + have e3 := det_outA hF eU + rw [e3] + exact ((RemKeepA.of_popSt hk).trans (RemKeepA.of_skeep keep hmem2)).trans (RemKeepA.of_unlink kt2 kr2) + +end Store +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/ReplyRemoveUnblockJointA.lean b/proofs/Fl4ProofsA64/P3/ReplyRemoveUnblockJointA.lean new file mode 100644 index 000000000..3f59d037f --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ReplyRemoveUnblockJointA.lean @@ -0,0 +1,100 @@ +/- +P3 (AArch64): the live Reply's removal and refill test as one joint step. + +`replyRemoveUnblockJointA` runs `reply_remove` and the reply refill test and returns the +joint state the transfer needs: the scheduler, endpoint and reply-stack relations, both spec +outcomes, the spec frame, and the typed-heap, slot, IPC-buffer and native-frame facts. + +[S] `unb`: the local-SC and release-queue premises of the refill test at the reached state. +R-hirq (Architect 01:46Z): no `hirqR` / `hirqU` premise; each concrete step leaves the IRQ-node slot region +unchanged by `IrqSlotsKeepA.of_replyFoot` on its footprint. +-/ +import Fl4ProofsA64.P3.ReplyXferFactsA +import Fl4ProofsA64.P3.IrqSlotsFootA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4SpecA64 Sched + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +private theorem reachedA {α : Type} {r : Result α} {P : α → Prop} (step : r ⦃ P ⦄) : + r ⦃ v => r = ok v ∧ P v ⦄ := by + obtain ⟨v, eq, post⟩ := spec_ex_fd step + rw [eq, WP.spec_ok] + exact ⟨rfl, post⟩ + +section Store +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + +theorem replyRemoveUnblockJointA {s : S} {σ : State Unit} (a : A) {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {r t : U64} (hr : r ∈ R) (ht : t ∈ Z.T) + {xt : Tcb} {xr : Reply} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hkr : σ.kheap (specPtr r) = some (.reply xr)) (hb : xr.replyTcb = some (specPtr t)) + (hst : xt.tcbState = .blockedOnReply (specPtr r) ∨ + ∃ ep d, xt.tcbState = .blockedOnReceive ep (some (specPtr r)) d) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) + (cov : ScReplyCovA Z.C m σ (specPtr r)) + (hsc0 : ∀ sc ∈ Z.C, (stack sc).head? = some r → sc ≠ 0#u64) + (hlocal : ∀ sc ∈ Z.C, (stack sc).head? = some r → xt.tcbSchedContext = none → + ReplyPopLocalA (st := st) (Z := Z) (m := m) s σ sc t) + (hpost : ∀ sc ∈ Z.C, (stack sc).head? = some r → ∀ σ', + ((replyUnlinkSc (Z := Unit) (scNameA m sc) (specPtr r)) >>= fun _ => + schedContextDonate (scNameA m sc) (specPtr t)) σ = ({((), σ')}, False) → + RelIfBoundSc σ' (specPtr t)) + (unb : ∀ s1 σ1 Q1 R1, SchedSRA st { Z with Q := Q1, R := R1 } m κ s1 σ1 → RemKeepA σ σ1 → + (L.mem s1 (scA t) ≠ 0#u64 → L.mem s1 (scCoreA (L.mem s1 (scA t))) = L.cpu s1) ∧ + (specPtr t ∈ σ1.releaseQueue → ∀ y n, + σ1.kheap (scNameA m (L.mem s1 (scA t))) = some (.schedContext y n) → + refillReady σ1.curTime (refillHd y) = false)) + (hoff : ∀ y, IsSlotA Z.th y → ¬ EWrite Z.T E R Bf y ∧ ¬ EWrite Z.T E R Bf (wa y 8#u64)) + (hSt : StateRelA Z.th (L.mem s) σ) (hV : SlotsValidA Z.th (L.mem s)) + (hC : BufCover st Z.T Bf s σ) (hNative : BufNativeA Z.T (L.mem s)) : + (do + let (s1, a1) ← ipc.reply_remove st ar s a r t + let s2 ← replyUnblockProgramA st s1 t + ok (s2, a1)) ⦃ out => ∃ Q1 R1 stack' σ1 σ2, + replyRemove (Z := Unit) (specPtr t) (specPtr r) σ = ({((), σ1)}, False) ∧ + (getTcbObjRef (Z := Unit) Tcb.tcbSchedContext (specPtr t) >>= + ifSporadicCurScTestRefillUnblockCheck) σ1 = ({((), σ2)}, False) ∧ + SchedSRA st { Z with Q := Q1, R := R1 } m κ out.1 σ2 ∧ + EpStateRel Z.T E R Bf (L.mem out.1) σ2 ∧ + ReplyStackRelA (L.mem out.1) R Z.C σ2 (scNameA m) stack' ∧ + ReplyIsolatedA (L.mem out.1) Z.C stack' r ∧ out.2 = a ∧ RemKeepA σ σ2 ∧ + StateRelA Z.th (L.mem out.1) σ2 ∧ SlotsValidA Z.th (L.mem out.1) ∧ + BufCover st Z.T Bf out.1 σ2 ∧ BufNativeA Z.T (L.mem out.1) ⦄ := by + have hTcb : ∀ u ∈ Z.T, ∃ x0, σ.kheap (specPtr u) = some (.tcb x0) := fun u hu => by + obtain ⟨x, hx, -⟩ := HE.tcb u hu + exact ⟨x, hx⟩ + refine WP.spec_bind (reachedA (WP.spec_and + (reply_remove_relA a H P PE HE HS hr ht cov hkt hkr hb hst hq hsc0 hlocal hpost) + (reply_remove_footA a H P PE HE HS hr ht hkt hkr hb hst hq cov hsc0 hlocal hpost))) ?_ + rintro ⟨s1, a1⟩ ⟨hreach, ⟨ha, σ1, stack', hu1, ⟨Q1, R1, H1⟩, E1, S1, iso1⟩, w1, σ1', hu1', keep1⟩ + have e1 : σ1' = σ1 := det_outA hu1' hu1 + subst e1 + have X1 := replyXferFactsA H.toSchedSRN PE P hoff w1 keep1 (Fl4ProofsA64.P3.Sched.IrqSlotsKeepA.of_replyFoot H.toSchedSRN PE P w1) hSt hV hC hNative hTcb + obtain ⟨hon, hrq⟩ := unb s1 σ1' Q1 R1 H1 keep1 + obtain ⟨x1, hk1, -⟩ := E1.tcb t ht + refine WP.spec_bind (reachedA (replyUnblockFrameA H1 P PE E1 S1 ht hr iso1 hk1 hon hrq)) ?_ + rintro s2 ⟨hreach2, σ2, hu2, H2, E2, S2', iso2, F, hcur, hmem, hsc⟩ + have keep2 : RemKeepA σ1' σ2 := RemKeepA.of_skeep F.keep hmem + have w2 : WritesIn st s1 s2 (ReplyFootW (st := st) { Z with Q := Q1, R := R1 } m σ1' s1 E R Bf) := + F.wr.mono fun b hb => Or.inr (Or.inl hb) + have hTcb1 : ∀ u ∈ Z.T, ∃ x0, σ1'.kheap (specPtr u) = some (.tcb x0) := fun u hu => by + obtain ⟨x, hx, -⟩ := E1.tcb u hu + exact ⟨x, hx⟩ + have X2 := replyXferFactsA (Z := { Z with Q := Q1, R := R1 }) H1.toSchedSRN PE P hoff w2 keep2 + (Fl4ProofsA64.P3.Sched.IrqSlotsKeepA.of_replyFoot H1.toSchedSRN PE P w2) X1.1 X1.2.1 X1.2.2.1 X1.2.2.2 hTcb1 + refine (WP.spec_ok _).2 ⟨Q1, R1, stack', σ1', σ2, hu1, hu2, H2, E2, S2', iso2, ha, + keep1.trans keep2, X2.1, X2.2.1, X2.2.2.1, X2.2.2.2⟩ + +end Store +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/ReplyRunExA.lean b/proofs/Fl4ProofsA64/P3/ReplyRunExA.lean new file mode 100644 index 000000000..0609d2e80 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ReplyRunExA.lean @@ -0,0 +1,452 @@ +/- +P3 (AArch64): waking a reply receiver whose SC may not be released. + +seL4 MCS `doReplyTransfer` sets the receiver Running (or Restart) before it tests +`refill_ready ∧ refill_sufficient`; the late branch (postpone or timeout) runs exactly when +the receiver's SC is not released. Between the wake and the SC tail l4v keeps +`released_ipc_queues` at every TCB but the receiver (`released_ipc_queues_except`, +`valid_sched_except_blocked`): here `SchedSRB … (specPtr t)` (`ScPostA`'s `RdExA`). + +* `SchedSRA.stableExA`: `SchedSRA.stableR`'s body with `RdExA` after instead of `RdA`. +* `setStateStepExA`: writing a runnable state to a non-runnable TCB is an `SStepA` step + keeping the queues runnable and `RdExA` at that TCB. +* **`set_thread_state_exA`**: FL4 `set_thread_state t v` of a non-runnable TCB that is not + the current thread refines `setThreadState t ts` (`ts` runnable) into `SchedSRB … t`, + one outcome, the spec state `updKSc σ t …`, the STATE word the only write. + +No named premise. +-/ +import Fl4ProofsA64.P3.ScSetStateA +import Fl4ProofsA64.P3.ScPostA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- `SchedSRA.stableR` with `RdA` after relaxed at one TCB `p`. -/ +theorem SchedSRA.stableExA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s s' : S} + {σ σ' : Fl4SpecA64.State Unit} + (H : SchedSRA st Z m κ s σ) {W : U64 → Prop} (hw : WritesIn st s s' W) (hW : ∀ a, W a → ¬ SReadA st Z m s σ a) + (hS : SStepA σ σ') (hrun : RunQA σ') {p : Fl4SpecA64.ObjRef} (hrd : RdExA σ' p) : + SchedSRB st Z m κ s' σ' p := by + have keep : ∀ a, Aligned a → SReadA st Z m s σ a → L.mem s' a = L.mem s a := + fun a ha hr => hw.mem_eq ha (fun h => hW a h hr) + have hT := H.core.tcbs + have tK : ∀ t ∈ Z.T, ∀ K : U64, (K = 3520#u64 ∨ K = 3448#u64 ∨ K = 3432#u64 ∨ K = 3464#u64 ∨ K = 3592#u64 ∨ + K = 3480#u64 ∨ K = 3488#u64) → L.mem s' (wa t K) = L.mem s (wa t K) := by + intro t ht K hK + refine keep _ ?_ (Or.inr (Or.inr (Or.inr (Or.inl ⟨t, ht, K, hK, rfl⟩)))) + rcases hK with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> exact field_aligned (hT t ht) (by simp) (by simp) + have nK : ∀ c ∈ Z.Cs, ∀ o : U64, o.val < 4288 → o.val % 8 = 0 → + L.mem s' (L.nodeAt s c o) = L.mem s (L.nodeAt s c o) := + fun c hc o ho h8 => keep _ (nodeAt_aligned (H.core.nodes c hc) ho h8) (Or.inl ⟨c, hc, o, ho, rfl⟩) + have hkb := H.ksb_al + have hf := H.ksFit + have gK : ∀ K : U64, (K = 8#u64 ∨ K = 16#u64) → L.mem s' (wa (L.ksb s) K) = L.mem s (wa (L.ksb s) K) := by + intro K hK + have hK' : K.val = 8 ∨ K.val = 16 := by rcases hK with rfl | rfl <;> simp + have hv : (wa (L.ksb s) K).val = (L.ksb s).val + K.val := by rw [wa_valm]; unfold ksSizeA at hf; omega + refine keep _ (by unfold Aligned; rw [hv]; omega) ?_ + rcases hK with rfl | rfl + · exact Or.inr (Or.inl rfl) + · exact Or.inr (Or.inr (Or.inl rfl)) + have hcore : SchedCoreA st Z.T Z.Cs s' Z.Q Z.R := + H.core.agree hw.1 (fun y hy => ⟨tK y hy 3480#u64 (by simp), tK y hy 3488#u64 (by simp)⟩) + (fun c hc o h1 h2 _ => nK c hc o h1 h2) (fun y hy => tK y hy 3592#u64 (by simp)) + (fun y hy => tK y hy 3520#u64 (by simp)) (fun y hy => tK y hy 3448#u64 (by simp)) + have hkc : ∀ c ∈ Z.C, σ'.kheap (scNameA m c) = σ.kheap (scNameA m c) := fun c hc => by + obtain ⟨x, n, hk, -⟩ := H.scr.sc c hc + exact hS.sc _ x n hk + exact ⟨H.toSchedSRN.rebuildQueues (Q' := Z.Q) hw.1 hw.2.1 (fun c hc o h1 h2 _ => nK c hc o (by omega) h2) gK + (fun t ht K hK => tK t ht K (by rcases hK with h | h | h | h <;> simp [h])) + (fun a ha hf => keep a ha (Or.inr (Or.inr (Or.inr (Or.inr hf))))) + hkc + (fun _ _ x hx => hS.tcb _ x hx) hS.fix + (by + rw [hS.act, nK _ H.sch.cpuCs layout.node.SCHED_ACTION (by simp [layout.node.SCHED_ACTION]) + (by simp [layout.node.SCHED_ACTION])] + exact H.sch.act) + (fun u hu => H.actOn u (by rw [← hS.act]; exact hu)) (H.inv.step hS) + hcore (fun p hp => by rw [hS.rq]; exact H.rest.rq p hp) (fun d p hd => by rw [hS.rq]; exact H.rest.rqDom d p hd) + hS.rel hrun, hrd⟩ + +end + +section Spec +open Fl4SpecA64 + +/-- Writing a runnable state to a non-runnable TCB: an `SStepA` step, the queues stay +runnable and `released_ipc_queues` holds at every other TCB. -/ +theorem setStateStepExA {σ : State Unit} {p : ObjRef} {x : Tcb} + (hk : σ.kheap p = some (.tcb x)) {ts : ThreadState} (hn : runnable ts = true) + (hrun : RunQA σ) (hrd : RdA σ) : + SStepA σ (updKSc σ p (.tcb { x with tcbState := ts })) ∧ + RunQA (updKSc σ p (.tcb { x with tcbState := ts })) ∧ + RdExA (updKSc σ p (.tcb { x with tcbState := ts })) p := by + let σ' := updKSc σ p (.tcb { x with tcbState := ts }) + have hh : σ'.kheap p = some (.tcb { x with tcbState := ts }) := by simp [σ', updKSc] + have far : ∀ q, q ≠ p → σ'.kheap q = σ.kheap q := fun q hq => by simp [σ', updKSc, hq] + have hS : SStepA σ σ' := by + refine ⟨⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩, rfl, rfl, rfl, + fun q y n hq => far q (fun e => by rw [e, hk] at hq; cases hq), + fun q y hq => ?_, fun q y n hq => (far q (fun e => by rw [e, hh] at hq; cases hq)).symm, + fun q y hq => ?_, fun q y hq => ?_⟩ + · by_cases e : q = p + · subst q + rw [hk] at hq + cases hq + exact ⟨_, hh, rfl, rfl, rfl⟩ + · exact ⟨y, (far _ e).trans hq, rfl, rfl, rfl⟩ + · by_cases e : q = p + · exact ⟨x, e.symm ▸ hk⟩ + · exact ⟨y, (far _ e).symm.trans hq⟩ + · have hq' := far q (fun e => by rw [e, hh] at hq; cases hq) + exact ⟨y, hq'.symm.trans hq, rfl⟩ + have k : ∀ u, (∃ y, σ.kheap u = some (.tcb y) ∧ runnable y.tcbState = true) → + ∃ y, σ'.kheap u = some (.tcb y) ∧ runnable y.tcbState = true := by + rintro u ⟨y, hy, hr⟩ + by_cases e : u = p + · subst u + exact ⟨_, hh, hn⟩ + · exact ⟨y, (far _ e).trans hy, hr⟩ + refine ⟨hS, ⟨fun d q u hu => k u (hrun.1 d q u hu), fun u hu => k u (hrun.2 u hu)⟩, ?_⟩ + intro u y hu hy hc + have hy0 : σ.kheap u = some (.tcb y) := (far _ hu).symm.trans hy + exact (hrd u y hy0 hc).step hS hy0 + +theorem setThreadState_stsActExA {σ : State Unit} {p : ObjRef} {x : Tcb} + (hk : σ.kheap p = some (.tcb x)) (ts : ThreadState) : + setThreadState (Z := Unit) p ts σ = + ({((), stsActA (updKSc σ p (.tcb { x with tcbState := ts })) p)}, False) := by + unfold setThreadState + rw [nd_bind_oneSc (threadSet_eqSc _ hk)] + unfold setThreadStateAct + repeat rw [nd_gets_bindSc] + unfold stsActA Fl4Spec.NonDet.when + split + · rfl + · rfl + +end Spec + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +open Fl4SpecA64 + +theorem stateOffExA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRN st Z m κ s σ) {t : U64} (ht : t ∈ Z.T) : ¬ SReadA st Z m s σ (stA t) := by + have htA := H.core.tcbs t ht + have hfit := htA.fits + have hst : (stA t).val = t.val + 3328 := by + rw [stA_eq, wa_valm] + have hk : (3328#u64 : U64).val = 3328 := by simp + omega + have hk := H.tcb_ks ht + have hks : ∀ b : U64, (L.ksb s).val ≤ b.val → b.val < (L.ksb s).val + ksSizeA → b ≠ stA t := by + intro b hb hb' e + rw [e, hst] at hb hb' + omega + have glob : ∀ k : U64, k.val < 223488 → core.num.U64.wrapping_add (L.ksb s) k ≠ stA t := by + intro k ho + have hfit := H.ksFit + unfold ksSizeA at hfit + have hv : (core.num.U64.wrapping_add (L.ksb s) k).val = (L.ksb s).val + k.val := by + rw [wa_valm] + omega + exact hks _ (by rw [hv]; omega) (by unfold ksSizeA; rw [hv]; omega) + intro hr + rcases hr with ⟨c, hc, o, ho, e⟩ | e | e | ⟨u, hu, k, hK, e⟩ | hf + · obtain ⟨n, -, hn, hcs⟩ := H.csN + have hc' := (hcs _).1 hc + have hv := nodeAt_valSc H.ksFit (show c.val < 128 by omega) ho + exact hks _ (by rw [hv]; omega) (by unfold ksSizeA; rw [hv]; omega) e.symm + · exact glob _ (by simp) e.symm + · exact glob _ (by simp) e.symm + · rw [stA_eq] at e + rcases hK with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> + exact field_ne htA (H.core.tcbs u hu) (by simp) (by simp) (fun _ => by simp) e + · have hn := H.scp.th_out hf ((H.thT t).2 ht) + simp only [ObjKindA.size] at hn + omega + +/-- `SchedSRA.set_action` for `SchedSRB` (the action word is outside `RdExA`). -/ +theorem SchedSRB.set_actionExA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} {σ : Fl4SpecA64.State Unit} + {p : Fl4SpecA64.ObjRef} (H : SchedSRB st Z m κ s σ p) (w : U64) {a : Fl4SpecA64.SchedulerAction} (ha : a = actOfA w) + (hon : ∀ u, a = .switchThread (specPtr u) → u ∈ Z.T ∧ L.mem s (affA u) = L.cpu s) : + SchedSRB st Z m κ (L.wr s (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) w) + { σ with schedulerAction := a } p := by + obtain ⟨n, -, hn, hcs⟩ := H.csN + have hcl : ∀ c ∈ Z.Cs, c.val < 128 := fun c hc => by have := (hcs c).1 hc; omega + have hf := H.ksFit + have hkb := H.ksb_al + have hcpu0 := hcl _ H.sch.cpuCs + have hsa : layout.node.SCHED_ACTION = 16#u64 := by unfold layout.node.SCHED_ACTION; rfl + have hAv : (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION).val = + (L.ksb s).val + 223488 + (L.cpu s).val * 4288 + 16 := by + rw [nodeAt_valSc (o := layout.node.SCHED_ACTION) hf hcpu0 (by rw [hsa]; simp), hsa]; simp + have hA : Aligned (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) := by unfold Aligned; rw [hAv]; omega + have keep : ∀ b, Aligned b → b.val ≠ (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION).val → + L.mem (L.wr s (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) w) b = L.mem s b := + fun b hb hne => L.mem_wr_ne hA hb (fun e => hne e.symm) + have nodeK : ∀ c ∈ Z.Cs, ∀ o : U64, o.val < 4288 → o.val % 8 = 0 → o.val ≠ 16 → + L.mem (L.wr s (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) w) (L.nodeAt s c o) = L.mem s (L.nodeAt s c o) := by + intro c hc o ho h8 h16 + have hv := nodeAt_valSc (s := s) hf (hcl c hc) ho + refine keep _ (by unfold Aligned; rw [hv]; omega) ?_ + rw [hv, hAv]; omega + have inT : ∀ t ∈ Z.T, ∀ b : U64, Aligned b → t.val ≤ b.val → b.val < t.val + 4096 → + L.mem (L.wr s (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) w) b = L.mem s b := by + intro t ht b hb h1 h2 + have := H.tcb_ks ht + refine keep _ hb ?_ + rw [hAv]; unfold ksSizeA at this; omega + have hT := H.core.tcbs + have tcbK : ∀ t ∈ Z.T, ∀ K : U64, K.val + 8 ≤ 4096 → K.val % 8 = 0 → + L.mem (L.wr s (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) w) (wa t K) = L.mem s (wa t K) := by + intro t ht K hK h8 + have hfit := (hT t ht).fits + have hal := (hT t ht).align + have hv : (wa t K).val = t.val + K.val := by rw [wa_valm]; omega + exact inT t ht _ (by unfold Aligned; rw [hv]; omega) (by omega) (by omega) + have ksbK : ∀ K : U64, K.val < 223488 → K.val % 8 = 0 → + L.mem (L.wr s (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) w) (wa (L.ksb s) K) = L.mem s (wa (L.ksb s) K) := by + intro K hK h8 + have hv : (wa (L.ksb s) K).val = (L.ksb s).val + K.val := by rw [wa_valm]; unfold ksSizeA at hf; omega + refine keep _ (by unfold Aligned; rw [hv]; omega) ?_ + rw [hv, hAv]; omega + have hcore : SchedCoreA st Z.T Z.Cs (L.wr s (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) w) Z.Q Z.R := + H.core.agree (L.ksb_wr _ _ _) + (fun y hy => ⟨inT y hy _ (nextA_aligned (hT y hy) _) (nextA_inside (hT y hy) _).1 + (by have := (nextA_inside (hT y hy) .Sched).2; omega), + inT y hy _ (prevA_aligned (hT y hy) _) (prevA_inside (hT y hy) _).1 + (by have := (prevA_inside (hT y hy) .Sched).2; omega)⟩) + (fun c hc o h1 h2 h3 => nodeK c hc o h1 h2 (by omega)) + (fun y hy => tcbK y hy 3592#u64 (by simp) (by simp)) + (fun y hy => tcbK y hy 3520#u64 (by simp) (by simp)) + (fun y hy => tcbK y hy 3448#u64 (by simp) (by simp)) + have hsc : ∀ b, Aligned b → ScFootA Z.C m σ b → + L.mem (L.wr s (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) w) b = L.mem s b := by + intro b hb hfb + refine keep b hb ?_ + have h1 := H.scp.ks_out hfb + unfold ksSizeA at h1; rw [hAv]; omega + exact ⟨H.toSchedSRN.rebuildQueues (Q' := Z.Q) (L.ksb_wr _ _ _) (L.cpu_wr _ _ _) + (fun c hc o h1 h2 h3 => nodeK c hc o (by omega) h2 h3) + (fun K hK => by rcases hK with rfl | rfl <;> exact ksbK _ (by simp) (by simp)) + (fun t ht K hK => by rcases hK with rfl | rfl | rfl | rfl <;> exact tcbK t ht _ (by simp) (by simp)) + hsc (fun _ _ => rfl) + (SchedSRA.tcb_keep rfl) ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ + (by show a = _; rw [L.mem_wr_same hA]; exact ha) hon (ScInvA.of_kheap H.inv rfl rfl) hcore + (fun p hp => H.rest.rq p hp) H.rest.rqDom rfl ⟨H.run.1, H.run.2⟩, + fun u x hu hx hc => H.rdEx u x hu hx hc⟩ + +/-- **Waking a non-current, non-runnable TCB into `SchedSRB`** (module docstring). -/ +theorem set_thread_state_exA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRA st Z m κ s σ) (hst : StEqA Z.T (L.mem s) σ) {t : U64} (ht : t ∈ Z.T) + (hcur : t ≠ L.curW s) {x : Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + {ts : ThreadState} (hn : runnable ts = true) {v : U64} (hv : v.val = tsCodeA ts) : + sched.set_thread_state st s t v ⦃ s' => ∃ σ', + setThreadState (Z := Unit) (specPtr t) ts σ = ({((), σ')}, False) ∧ + σ' = updKSc σ (specPtr t) (.tcb { x with tcbState := ts }) ∧ + SchedSRB st Z m κ s' σ' (specPtr t) ∧ StEqA Z.T (L.mem s') σ' ∧ + WritesIn st s s' (· = stA t) ⦄ := by + let σ1 := updKSc σ (specPtr t) (.tcb { x with tcbState := ts }) + have hal : Aligned (stA t) := by + rw [stA_eq] + exact field_aligned (H.core.tcbs t ht) (by simp) (by simp) + have hread := stateOffExA H.toSchedSRN ht + have w1 : WritesIn st s (L.wr s (stA t) v) (· = stA t) := writesIn_wr hal + obtain ⟨hS, hrun, hrd⟩ := setStateStepExA hk hn H.run H.rd + have H1 : SchedSRB st Z m κ (L.wr s (stA t) v) σ1 (specPtr t) := + H.stableExA w1 (fun _ e => e ▸ hread) hS hrun hrd + have ht1 : StEqA Z.T (L.mem (L.wr s (stA t) v)) σ1 := by + intro u hu y hy + by_cases e : u = t + · subst u + change (updKSc σ (specPtr t) (.tcb { x with tcbState := ts })).kheap (specPtr t) = some (.tcb y) at hy + rw [updKSc_kheap, Function.update_self] at hy + cases hy + rw [L.mem_wr_same hal] + exact hv + · have hne : specPtr u ≠ specPtr t := fun eq => e (specPtr_inj eq) + have hy0 : σ.kheap (specPtr u) = some (.tcb y) := by + change (updKSc σ (specPtr t) (.tcb { x with tcbState := ts })).kheap (specPtr u) = some (.tcb y) at hy + rwa [updKSc_kheap, Function.update_of_ne hne] at hy + have hne' : stA u ≠ stA t := by + rw [stA_eq, stA_eq] + exact field_ne (H.core.tcbs u hu) (H.core.tcbs t ht) (by simp) (by simp) (fun e' => absurd e' e) + have ha : Aligned (stA u) := by + rw [stA_eq] + exact field_aligned (H.core.tcbs u hu) (by simp) (by simp) + rw [w1.mem_eq ha hne'] + exact hst u hu y hy0 + have hcurT : σ1.curThread = specPtr (L.curW (L.wr s (stA t) v)) := H1.sch.curThread + have hcw : L.curW (L.wr s (stA t) v) = L.curW s := + specPtr_inj (hcurT.symm.trans H.sch.curThread) + have hspec : setThreadState (Z := Unit) (specPtr t) ts σ = ({((), σ1)}, False) := by + rw [setThreadState_stsActExA hk] + rw [stsActA_eq_self (by + intro h + apply hcur + exact specPtr_inj (h.1.trans H.sch.curThread))] + unfold sched.set_thread_state + refine WP.spec_bind (obj.Tcb.set_state.spec s t v) ?_ + intro s₂ hs₂ + rw [hs₂] + unfold sched.schedule_tcb + refine WP.spec_bind (sched.cur_thread.spec _) ?_ + rintro t1 rfl + refine WP.spec_bind (obj.Tcb.eq.spec _ _) ?_ + rintro b rfl + have e1 : t ≠ L.mem (L.wr s (stA t) v) (L.nodeAt (L.wr s (stA t) v) (L.cpu (L.wr s (stA t) v)) + layout.node.CUR_THREAD) := by + intro h + apply hcur + rw [← hcw] + exact h + rw [show decide (t = L.mem (L.wr s (stA t) v) (L.nodeAt (L.wr s (stA t) v) (L.cpu (L.wr s (stA t) v)) + layout.node.CUR_THREAD)) = false from decide_eq_false e1] + simp only [Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + exact ⟨σ1, hspec, rfl, H1, ht1, w1⟩ + +/-- `schedule_tcb_refinesA` for `SchedSRB`. -/ +theorem schedule_tcb_exA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} {σ : Fl4SpecA64.State Unit} + {p : ObjRef} (H : SchedSRB st Z m κ s σ p) (t : U64) (hst : StEqA Z.T (L.mem s) σ) : + sched.schedule_tcb st s t ⦃ s' => WritesIn st s s' (· = L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) ∧ + SchedSRB st Z m κ s' (stsActA σ (specPtr t)) p ⦄ := by + have hcur : σ.curThread = specPtr (L.curW s) := H.sch.curThread + have hres0 : sched.ACTION_RESUME_CURRENT = 0#u64 := by unfold sched.ACTION_RESUME_CURRENT; rfl + unfold sched.schedule_tcb + refine WP.spec_bind (sched.cur_thread.spec s) ?_ + rintro t1 rfl + refine WP.spec_bind (obj.Tcb.eq.spec _ _) ?_ + rintro b rfl + by_cases e1 : t = L.curW s + · subst e1 + simp only [show decide (L.curW s = L.mem s (L.nodeAt s (L.cpu s) layout.node.CUR_THREAD)) = true from + decide_eq_true rfl, ↓reduceIte] + refine WP.spec_bind (sched.action.spec s) ?_ + rintro i rfl + by_cases e2 : L.actionW s = sched.ACTION_RESUME_CURRENT + · have hres : σ.schedulerAction = .resumeCurThread := by + rw [H.sch.act, e2, hres0]; simp [actOfA] + simp only [e2, ↓reduceIte] + rw [is_schedulable_eqA, bind_ok, schedulable_eqA H.toSchedSRN H.curT (H.relOn H.curT H.curOn) hst] + by_cases e3 : Fl4SpecA64.schedulable (specPtr (L.curW s)) σ = true + · simp only [e3, ↓reduceIte, WP.spec_ok] + refine ⟨WritesIn.refl _ _, ?_⟩ + rw [stsActA_eq_self (by intro h; exact h.2.2 e3)]; exact H + · simp only [e3, Bool.false_eq_true, ↓reduceIte] + refine WP.spec_mono (sched.set_action.spec s _) ?_ + rintro s' rfl + have e4 : stsActA σ (specPtr (L.curW s)) = { σ with schedulerAction := .chooseNewThread } := by + unfold stsActA + rw [if_pos ⟨hcur.symm, hres, e3⟩] + rw [e4] + refine ⟨writesIn_wr (s := s) (v := sched.ACTION_CHOOSE_NEW) ?_, H.set_actionExA _ ?_ fun u h => by cases h⟩ + · obtain ⟨n, -, hn, hcs⟩ := H.csN + have hc := (hcs _).1 H.sch.cpuCs + unfold Aligned + have hsa : layout.node.SCHED_ACTION = 16#u64 := by unfold layout.node.SCHED_ACTION; rfl + rw [nodeAt_valSc (o := layout.node.SCHED_ACTION) H.ksFit (by omega) (by rw [hsa]; simp)] + have hkb : (L.ksb s).val % 8 = 0 := by + have h0 : (0#u64 : U64).val < 5376 := by simp + have h1 := H.place.align _ _ (H.place.irq 0#u64 h0) + rw [irqSlotA_val H.ksFit h0, show ObjKindA.size (.cnode 0) = 32 from rfl, + show (0#u64 : U64).val = 0 from rfl] at h1 + omega + simp [layout.node.SCHED_ACTION]; omega + · rw [show sched.ACTION_CHOOSE_NEW = 1#u64 by unfold sched.ACTION_CHOOSE_NEW; rfl]; simp [actOfA] + · simp only [e2, ↓reduceIte, WP.spec_ok] + refine ⟨WritesIn.refl _ _, ?_⟩ + have hnr : σ.schedulerAction ≠ .resumeCurThread := by + intro h; rw [H.sch.act] at h + apply e2; rw [hres0] + unfold actOfA at h + by_cases w0 : L.actionW s = 0#u64 + · exact w0 + · rw [if_neg w0] at h; split_ifs at h + rw [stsActA_eq_self (by intro h; exact hnr h.2.1)]; exact H + · rw [show decide (t = L.mem s (L.nodeAt s (L.cpu s) layout.node.CUR_THREAD)) = false from decide_eq_false e1] + simp only [Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + refine ⟨WritesIn.refl _ _, ?_⟩ + rw [stsActA_eq_self (by intro h; exact e1 (specPtr_inj (h.1.trans hcur)))]; exact H + +private theorem stsActA_kheapExA (σ : State Unit) (p : ObjRef) : (stsActA σ p).kheap = σ.kheap := by + unfold stsActA + split <;> rfl + +/-- **Waking a non-runnable TCB into `SchedSRB`**, the current thread or not: the +scheduler-action update of `setThreadState` is `stsActA`. -/ +theorem set_thread_state_exAnyA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRA st Z m κ s σ) (hst : StEqA Z.T (L.mem s) σ) {t : U64} (ht : t ∈ Z.T) + {x : Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + {ts : ThreadState} (hn : runnable ts = true) {v : U64} (hv : v.val = tsCodeA ts) : + sched.set_thread_state st s t v ⦃ s' => ∃ σ', + setThreadState (Z := Unit) (specPtr t) ts σ = ({((), σ')}, False) ∧ + σ'.kheap = (updKSc σ (specPtr t) (.tcb { x with tcbState := ts })).kheap ∧ + SchedSRB st Z m κ s' σ' (specPtr t) ∧ StEqA Z.T (L.mem s') σ' ∧ + WritesIn st s s' (fun b => b = stA t ∨ b = L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) ⦄ := by + let σ1 := updKSc σ (specPtr t) (.tcb { x with tcbState := ts }) + have hal : Aligned (stA t) := by + rw [stA_eq] + exact field_aligned (H.core.tcbs t ht) (by simp) (by simp) + have hread := stateOffExA H.toSchedSRN ht + have w1 : WritesIn st s (L.wr s (stA t) v) (· = stA t) := writesIn_wr hal + obtain ⟨hS, hrun, hrd⟩ := setStateStepExA hk hn H.run H.rd + have H1 : SchedSRB st Z m κ (L.wr s (stA t) v) σ1 (specPtr t) := + H.stableExA w1 (fun _ e => e ▸ hread) hS hrun hrd + have ht1 : StEqA Z.T (L.mem (L.wr s (stA t) v)) σ1 := by + intro u hu y hy + by_cases e : u = t + · subst u + change (updKSc σ (specPtr t) (.tcb { x with tcbState := ts })).kheap (specPtr t) = some (.tcb y) at hy + rw [updKSc_kheap, Function.update_self] at hy + cases hy + rw [L.mem_wr_same hal] + exact hv + · have hne : specPtr u ≠ specPtr t := fun eq => e (specPtr_inj eq) + have hy0 : σ.kheap (specPtr u) = some (.tcb y) := by + change (updKSc σ (specPtr t) (.tcb { x with tcbState := ts })).kheap (specPtr u) = some (.tcb y) at hy + rwa [updKSc_kheap, Function.update_of_ne hne] at hy + have hne' : stA u ≠ stA t := by + rw [stA_eq, stA_eq] + exact field_ne (H.core.tcbs u hu) (H.core.tcbs t ht) (by simp) (by simp) (fun e' => absurd e' e) + have ha : Aligned (stA u) := by + rw [stA_eq] + exact field_aligned (H.core.tcbs u hu) (by simp) (by simp) + rw [w1.mem_eq ha hne'] + exact hst u hu y hy0 + unfold sched.set_thread_state + refine WP.spec_bind (obj.Tcb.set_state.spec s t v) ?_ + intro s₂ hs₂ + rw [hs₂] + refine WP.spec_mono (schedule_tcb_exA H1 t ht1) ?_ + rintro s' ⟨w2, H2⟩ + refine ⟨stsActA σ1 (specPtr t), setThreadState_stsActExA hk ts, stsActA_kheapExA _ _, H2, ?_, ?_⟩ + · intro u hu y hy + rw [stsActA_kheapExA] at hy + have al : Aligned (stA u) := by + rw [stA_eq] + exact field_aligned (H.core.tcbs u hu) (by simp) (by simp) + have hr1 := stateOffExA H1.toSchedSRN hu + have hne : stA u ≠ L.nodeAt (L.wr s (stA t) v) (L.cpu (L.wr s (stA t) v)) layout.node.SCHED_ACTION := by + intro e + exact hr1 (Or.inl ⟨_, H1.sch.cpuCs, _, by simp [layout.node.SCHED_ACTION], e⟩) + rw [w2.mem_eq al hne] + exact ht1 u hu y hy + · refine (w1.trans w2).mono fun b h => h.elim Or.inl (fun e => Or.inr ?_) + rw [e, nodeAt_of_ksb w1.1, w1.2.1] + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ReplyStackPopAllA.lean b/proofs/Fl4ProofsA64/P3/ReplyStackPopAllA.lean new file mode 100644 index 000000000..c4d53a403 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ReplyStackPopAllA.lean @@ -0,0 +1,226 @@ +/- +P3 (AArch64): the reply-stack relation after `reply_remove_tcb` of the head reply. + +**`ReplyStackRelA.popAll`**: unlike `reply_pop`, the older replies do not move up: the SC's +stack becomes empty and every reply below the removed head is detached. +**`takeWhileSpecPtrPA`**: cutting a mapped stack at its `k`-th entry takes the first `k`. + +No named premise. +-/ +import Fl4ProofsA64.P3.IpcScPopLinksA + +open Aeneas Aeneas.Std +namespace Fl4ProofsA64.P3.Endp +open Fl4ProofsA64.P1 Fl4SpecA64 +noncomputable section + +theorem takeWhileSpecPtrPA (l : List U64) (hnd : l.Nodup) (k : Nat) (hk : k < l.length) : + (l.map specPtr).takeWhile (fun x => decide (x ≠ specPtr l[k])) = (l.take k).map specPtr := by + induction l generalizing k with + | nil => simp at hk + | cons d l ih => + cases k with + | zero => simp + | succ k => + have hk' : k < l.length := by simpa using hk + have hne : specPtr d ≠ specPtr l[k] := fun e => + (List.nodup_cons.1 hnd).1 (specPtr_inj e ▸ List.getElem_mem hk') + simp only [List.map_cons, List.getElem_cons_succ, List.take_succ_cons] + rw [List.takeWhile_cons_of_pos (by simpa using hne), ih (List.nodup_cons.1 hnd).2 k hk'] + +/-- **The call-stack relation after dropping the whole stack of `sc` from its head `r`.** -/ +theorem ReplyStackRelA.popAll {R C : Set U64} {mem mem' : U64 → U64} {σ σ' : State Unit} + {scRef : U64 → ObjRef} {Lst : U64 → List U64} + (h : ReplyStackRelA mem R C σ scRef Lst) (hR0 : ∀ d ∈ R, d ≠ 0#u64) (hEv : ∀ d ∈ R, d.val % 2 = 0) + {sc r : U64} (hsc : sc ∈ C) {rest : List U64} (hL : Lst sc = r :: rest) + (hmP : ∀ d ∈ R, d ≠ r → mem' (rPrevA d) = mem (rPrevA d)) + (hmN : ∀ d ∈ R, d ≠ r → d ≠ rest.headD 0#u64 → mem' (rNextA d) = mem (rNextA d)) + (hmH : rest.headD 0#u64 ≠ 0#u64 → mem' (rNextA (rest.headD 0#u64)) = 0#u64) + (hmPr : mem' (rPrevA r) = 0#u64) (hmNr : mem' (rNextA r) = 0#u64) + {y : SchedContext} {n : Nat} (hk : σ.kheap (scRef sc) = some (.schedContext y n)) + (hk' : σ'.kheap (scRef sc) = some (.schedContext { y with scReplies := [] } n)) + (hkq : ∀ q ∈ C, q ≠ sc → σ'.kheap (scRef q) = σ.kheap (scRef q)) + {xr : Reply} (hkr : σ.kheap (specPtr r) = some (.reply xr)) + (hkr' : σ'.kheap (specPtr r) = some (.reply { xr with replySc := none })) + (hRep : ∀ d ∈ R, ∃ xd, σ.kheap (specPtr d) = some (.reply xd)) + (hkd : ∀ d ∈ R, d ≠ r → σ'.kheap (specPtr d) = σ.kheap (specPtr d)) : + ReplyStackRelA mem' R C σ' scRef (Function.update Lst sc []) ∧ + ReplyIsolatedA mem' C (Function.update Lst sc []) r := by + set Lst' := Function.update Lst sc [] with hLst' + have hL'sc : Lst' sc = [] := by simp [Lst'] + have hL'q : ∀ q, q ≠ sc → Lst' q = Lst q := fun q hq => by simp [Lst', hq] + obtain ⟨⟨y0, n0, hk0, hy0⟩, hLR, hLnd, hLw⟩ := h.stackRel sc hsc + have hnd' := hLnd + rw [hL] at hnd' + have hrrest : r ∉ rest := (List.nodup_cons.1 hnd').1 + have hmemsc : ∀ d q, q ∈ C → d ∈ Lst q → d ∈ Lst sc → q = sc := fun d q hq h1 h2 => + h.unique q hq sc hsc d h1 h2 + have hsub : ∀ q ∈ C, ∀ d ∈ Lst' q, q ≠ sc ∧ d ∈ Lst q := by + intro q hq d hd + by_cases e : q = sc + · subst e; rw [hL'sc] at hd; cases hd + · rw [hL'q q e] at hd; exact ⟨e, hd⟩ + have hlen : (Lst sc).length = rest.length + 1 := by rw [hL]; rfl + have hget : ∀ i (hi : i < (Lst sc).length) j, i = j + 1 → ∀ (hj : j < rest.length), (Lst sc)[i] = rest[j] := by + intro i hi j e hj + subst e + simp [hL] + have hget0 : ∀ (h0 : 0 < (Lst sc).length), (Lst sc)[0] = r := by intro h0; simp [hL] + -- the words of the older replies after the drop + have hrest : ∀ j (hj : j < rest.length), + rest[j] ∈ R ∧ rest[j] ≠ r ∧ mem' (rPrevA rest[j]) = rest.getD (j + 1) 0#u64 ∧ + mem' (rNextA rest[j]) = (0#u64 :: rest).getD j 0#u64 := by + intro j hj + have hmR : rest[j] ∈ R := hLR _ (by rw [hL]; exact List.mem_cons_of_mem _ (List.getElem_mem hj)) + have hne : rest[j] ≠ r := fun e => hrrest (e ▸ List.getElem_mem hj) + have hi : j + 1 < (Lst sc).length := by omega + obtain ⟨g1, g2₀⟩ := hLw (j + 1) hi + have g2 : mem (rNextA (Lst sc)[j + 1]) = (Lst sc).getD j 0#u64 := g2₀ + rw [hget (j + 1) hi j rfl hj] at g1 g2 + refine ⟨hmR, hne, ?_, ?_⟩ + · rw [hmP _ hmR hne, g1, hL] + simp + · cases j with + | zero => + have e : rest.headD 0#u64 = rest[0] := by + cases rest with + | nil => simp at hj + | cons a l => rfl + have := hmH (by rw [e]; exact hR0 _ hmR) + rwa [e] at this + | succ i => + have hne0 : rest[i + 1] ≠ rest.headD 0#u64 := by + have e : rest.headD 0#u64 = rest[0]'(by omega) := by + cases rest with + | nil => simp at hj + | cons a l => rfl + rw [e] + intro e' + have := (List.nodup_iff_injective_get.1 (List.nodup_cons.1 hnd').2) (a₁ := ⟨i + 1, hj⟩) + (a₂ := ⟨0, by omega⟩) (by simpa using e') + simp at this + rw [hmN _ hmR hne hne0, g2, hL] + simp + have hdet' : ∀ d, d ∈ Lst sc → ∀ q ∈ C, d ∉ Lst' q := by + intro d hd q hq h' + obtain ⟨e, h2⟩ := hsub q hq d h' + exact e (hmemsc d q hq h2 hd) + have hdetold : ∀ d, (∀ q ∈ C, d ∉ Lst q) → ∀ q ∈ C, d ∉ Lst' q := + fun d hd q hq h' => hd q hq (hsub q hq d h').2 + refine ⟨⟨fun q hq => ?_, fun q hq q' hq' d h1 h2 => ?_, fun d hd x' hx' p => ?_, fun d hd hdd => ?_⟩, ?_⟩ + · by_cases e : q = sc + · subst e + refine ⟨⟨_, n, hk', by rw [hL'sc]; rfl⟩, by rw [hL'sc]; simp, by rw [hL'sc]; exact List.nodup_nil, ?_⟩ + rw [hL'sc] + intro i hi + cases hi + · rw [hL'q q e] + obtain ⟨⟨yq, nq, hkq0, hyq⟩, hqR, hqnd, hqw⟩ := h.stackRel q hq + refine ⟨⟨yq, nq, by rw [hkq q hq e]; exact hkq0, hyq⟩, hqR, hqnd, fun i hi => ?_⟩ + have hm := List.getElem_mem hi + have hnL : (Lst q)[i] ∉ Lst sc := fun h' => e (hmemsc _ q hq hm h') + have hne : (Lst q)[i] ≠ r := fun e' => hnL (by rw [e', hL]; simp) + have hne0 : (Lst q)[i] ≠ rest.headD 0#u64 := by + intro e' + cases hrc : rest with + | nil => + rw [hrc] at e' + exact hR0 _ (hqR _ hm) e' + | cons a l => + rw [hrc] at e' + exact hnL (by rw [e', hL, hrc]; simp) + rw [hmP _ (hqR _ hm) hne, hmN _ (hqR _ hm) hne hne0] + exact hqw i hi + · obtain ⟨-, g1⟩ := hsub q hq d h1 + obtain ⟨-, g2⟩ := hsub q' hq' d h2 + exact h.unique q hq q' hq' d g1 g2 + · obtain ⟨x, hkx⟩ := hRep d hd + have hxs := h.owner d hd x hkx + by_cases e : d = r + · subst e + rw [hkr'] at hx' + cases hx' + refine ⟨fun hp => (by cases hp), fun ⟨q, hq, _, hhd⟩ => ?_⟩ + exact absurd (List.mem_of_mem_head? hhd) (hdet' d (by rw [hL]; simp) q hq) + · rw [hkd d hd e, hkx] at hx' + cases hx' + rw [hxs p] + constructor + · rintro ⟨q, hq, hqp, hhd⟩ + by_cases e' : q = sc + · subst e' + rw [hL] at hhd + exact absurd (Option.some.inj hhd).symm e + · exact ⟨q, hq, hqp, by rw [hL'q q e']; exact hhd⟩ + · rintro ⟨q, hq, hqp, hhd⟩ + by_cases e' : q = sc + · subst e'; rw [hL'sc] at hhd; cases hhd + · exact ⟨q, hq, hqp, by rw [hL'q q e'] at hhd; exact hhd⟩ + · by_cases e : d = r + · subst e + rw [hmNr, hmPr] + exact ⟨rfl, fun h' => absurd rfl h', fun h' => absurd rfl h'⟩ + by_cases hm : d ∈ rest + · obtain ⟨j, hj, he⟩ := List.getElem_of_mem hm + subst he + obtain ⟨-, -, gp, gn⟩ := hrest j hj + have inL : ∀ i (hi : i < rest.length), rest[i] ∈ Lst sc := fun i hi => by + rw [hL]; exact List.mem_cons_of_mem _ (List.getElem_mem hi) + refine ⟨?_, fun hne => ?_, fun hne => ?_⟩ + · rw [gn] + cases j with + | zero => exact headBitEvenA (r := 0#u64) (by simp) + | succ i => + show headBit (rest.getD i 0#u64) = false + rw [List.getD_eq_getElem _ _ (by omega)] + exact headBitEvenA (hEv _ (hrest i (by omega)).1) + · cases j with + | zero => rw [gn] at hne; exact absurd rfl hne + | succ i => + have hi : i < rest.length := by omega + have e1 : mem' (rNextA rest[i + 1]) = rest[i] := by + rw [gn]; exact List.getD_eq_getElem _ _ hi + rw [e1] + refine ⟨(hrest i hi).1, hdet' _ (inL i hi), ?_⟩ + rw [(hrest i hi).2.2.1] + exact List.getD_eq_getElem _ _ hj + · by_cases hj1 : j + 1 < rest.length + · have e1 : mem' (rPrevA rest[j]) = rest[j + 1] := by + rw [gp]; exact List.getD_eq_getElem _ _ hj1 + rw [e1] + refine ⟨(hrest (j + 1) hj1).1, hdet' _ (inL (j + 1) hj1), ?_⟩ + rw [(hrest (j + 1) hj1).2.2.2] + exact List.getD_eq_getElem _ _ hj + · rw [gp, List.getD_eq_default _ _ (by omega)] at hne + exact absurd rfl hne + · -- an old detached reply + have hnL : d ∉ Lst sc := by + rw [hL] + intro h' + rcases List.mem_cons.1 h' with rfl | h' + · exact e rfl + · exact hm h' + have hdo : ∀ q ∈ C, d ∉ Lst q := by + intro q hq hdq + by_cases e' : q = sc + · subst e'; exact hnL hdq + · exact hdd q hq (by rw [hL'q q e']; exact hdq) + -- a detached reply is not a live one + have notL : ∀ z, (∀ q ∈ C, z ∉ Lst q) → z ≠ r ∧ z ∉ rest := fun z hz => + ⟨fun e' => hz sc hsc (by rw [e', hL]; simp), fun h' => hz sc hsc (by rw [hL]; exact List.mem_cons_of_mem _ h')⟩ + have notHead : ∀ z ∈ R, z ∉ rest → z ≠ rest.headD 0#u64 := by + intro z hz hn e' + cases hrc : rest with + | nil => rw [hrc] at e'; exact hR0 z hz e' + | cons a l => rw [hrc] at e' hn; exact hn (by rw [e']; simp) + obtain ⟨t1, t2, t3⟩ := h.detached d hd hdo + rw [hmP d hd e, hmN d hd e (notHead d hd hm)] + refine ⟨t1, fun hne => ?_, fun hne => ?_⟩ + · obtain ⟨g1, g2, g3⟩ := t2 hne + exact ⟨g1, hdetold _ g2, by rw [hmP _ g1 (notL _ g2).1]; exact g3⟩ + · obtain ⟨g1, g2, g3⟩ := t3 hne + exact ⟨g1, hdetold _ g2, by rw [hmN _ g1 (notL _ g2).1 (notHead _ g1 (notL _ g2).2)]; exact g3⟩ + · exact ⟨hdet' r (by rw [hL]; simp), hmNr, hmPr⟩ + +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/ReplyStallA.lean b/proofs/Fl4ProofsA64/P3/ReplyStallA.lean new file mode 100644 index 000000000..37b701532 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ReplyStallA.lean @@ -0,0 +1,51 @@ +/- +P3 (AArch64, delete hooks W3 Reply (b)): **`remote_tcb_stall` is the no-op on one core**, a bound VCPU allowed: the +thread, and the `RUN_CALLER` of its VCPU if any, are on the current core. `P3/ScPopDonA`'s `stall_keepA` asks for no +bound VCPU. The typing of `RUN_CALLER` (`0` or a thread) that gives `hrc` from [S] `numNodes = 1` is the caller's. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.ScPopDonA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +/-- **The stall of a local thread whose run caller, if any, is local** (module docstring). -/ +theorem stall_single_noopA (a : A) {s : S} {f : U64} (haff : L.mem s (affA f) = L.cpu s) + (hrc : L.mem s (wa f 3664#u64) ≠ 0#u64 → L.mem s (wa (L.mem s (wa f 3664#u64)) 24#u64) ≠ 0#u64 → + L.mem s (affA (L.mem s (wa (L.mem s (wa f 3664#u64)) 24#u64))) = L.cpu s) : + smp.remote_tcb_stall st ar s a f = ok (s, a) := by + have eaff : ∀ t, obj.Tcb.affinity st t s = ok (L.mem s (affA t)) := fun t => + eq_of_spec_fd (obj.Tcb.affinity.spec (L := L) s t) + have ev : vcpu.bound st s f = ok (L.mem s (wa f 3664#u64)) := by + unfold vcpu.bound + rw [eq_of_spec_fd Fl4ProofsA64.P0.Consts.layout.tcb.VCPU.spec] + simp only [bind_ok, lift, L.read_eq] + by_cases hv0 : L.mem s (wa f 3664#u64) = 0#u64 + · exact stall_none ar a (c := L.cpu s) (by rw [eaff, haff]) (L.cur_cpu_eq s) (by rw [ev, hv0]) + have erc : vcpu.RUN_CALLER = (24#u64 : U64) := by + unfold vcpu.RUN_CALLER + rfl + unfold smp.remote_tcb_stall + simp only [eaff, haff, L.cur_cpu_eq, ev, erc, lift, bind_ok, uncurry_apply_pair, L.read_eq, bne_self_eq_false, + Bool.false_eq_true, ↓reduceIte, bne_iff_ne, ne_eq, hv0, not_false_eq_true] + simp only [not_true_eq_false, ↓reduceIte, bind_ok, uncurry_apply_pair] + rw [ev, bind_ok, if_pos hv0] + by_cases hc0 : L.mem s (wa (L.mem s (wa f 3664#u64)) 24#u64) = 0#u64 + · rw [if_neg (not_not.2 hc0)] + · rw [if_pos hc0, eaff, bind_ok, if_neg (not_not.2 (hrc hv0 hc0))] + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ReplyTailExA.lean b/proofs/Fl4ProofsA64/P3/ReplyTailExA.lean new file mode 100644 index 000000000..43c7e58a9 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ReplyTailExA.lean @@ -0,0 +1,242 @@ +/- +P3 (AArch64): the live Reply's SC tail from the woken receiver's relation. + +After `doReplyTransfer` sets the receiver `t` Running, l4v's `released_ipc_queues` holds at +every TCB but `t` (`SchedSRB … (specPtr t)`, `ReplyRunExA`). **`replyTailExA`** runs FL4's +SC tail (`replyTailProgramA`) from there and restores `SchedSRA` in each branch: + +* unbound SC: the tail skips; `t` has no SC, so `RelIfBoundSc` at `t` is vacuous; +* eligible refill: `releasedSc` from the ready test and `ScInvA.suff` (an active non-idle + SC's head is sufficient), then `possible_switch_to_refinesA`; +* late refill, postpone: `postpone_refinesA` puts `t` in the release queue (`SchedSRA` after); +* late refill, timeout: the caller's `timeout` continuation starts from `SchedSRB … t` (the + FL4 timeout send blocks `t` with a timeout fault, outside `inClassR`). + +No named premise. Hypotheses: `hidle` (`t` is no core's idle thread), `active` (a bound SC is +active), `hv` / `hf` (the actual timeout-handler test and original fault tag), `hcls` (`t` +runnable, or outside `inClassR` when the tail skips), `hnqs` / `hnrel` (`t` in no spec +queue), `hon` / `hcpu` (`t` and its SC on the current core, the [S] scope of +`postpone_refinesA`). +-/ +import Fl4ProofsA64.P3.IpcScReplyTransferA +import Fl4ProofsA64.P3.ScResumeA +import Fl4ProofsA64.P3.ReplyRunExA +import Fl4ProofsA64.P3.ScWakeDonA +import Fl4ProofsA64.P3.ReplyRemoveBorA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Sched Notif Fl4Spec Fl4SpecA64 + +noncomputable section + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +private theorem replyRunnableExA {s : S} {σ : State Unit} {T : Set U64} {t : U64} {x : Tcb} + (H : StEqA T (L.mem s) σ) (ht : t ∈ T) (hk : σ.kheap (specPtr t) = some (.tcb x)) : + sched.is_runnable st s t = ok (runnable x.tcbState) := by + have hc := H t ht x hk + have hr := runnable_iffA hc + have hread := eq_of_spec_fd (P1.obj.Tcb.state.spec (st := st) s t) + unfold sched.is_runnable + rw [hread, bind_ok] + by_cases h : runnable x.tcbState = true + · obtain h0 | h1 | h2 := hr.2 h + · simp [h, h0] + · simp [h, h1] + · simp [h, h2] + · have h0 : L.mem s (stA t) ≠ sched.TS_RUNNING := fun e => h (hr.1 (Or.inl e)) + have h1 : L.mem s (stA t) ≠ sched.TS_RESTART := fun e => h (hr.1 (Or.inr (Or.inl e))) + have h2 : L.mem s (stA t) ≠ sched.TS_RUNNING_VM := fun e => h (hr.1 (Or.inr (Or.inr e))) + rw [if_neg h0, if_neg h1, decide_eq_false h2, Bool.eq_false_of_not_eq_true h] + +/-- `fl4ReplyBudgetTestA` from `SchedSRN` (it reads no `rd`). -/ +private theorem fl4ReplyBudgetTestExA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {a : A} {σ : State Unit} (H : SchedSRN st Z m κ s σ) + {sc : U64} (hc : sc ∈ Z.C) {x : SchedContext} {n : Nat} + (hk : σ.kheap (scNameA m sc) = some (.schedContext x n)) (ha : scActive x = true) + (hm : Fl4MinBudgetAgreeA ar a) : + (do + let ready ← sporadic.ready st s sc + if ready then sporadic.sufficient st ar s a sc 0#u64 else ok false) ⦃ b => + b = (refillReady σ.curTime (refillHd x) && fl4RefillSufficient 0 (refillHd x)) ⦄ := by + refine WP.spec_bind (ready_refinesA H.scr H.sch.time hc hk ha) ?_ + intro b hb + rw [getScRefillReady_eqA hk (H.inv.ne hk ha)] at hb + have he := (nd_one_injA hb).1 + cases b + · have hready : refillReady σ.curTime (refillHd x) = false := he + simp only [Bool.false_eq_true, ↓reduceIte, hready, Bool.false_and, WP.spec_ok] + · have hready : refillReady σ.curTime (refillHd x) = true := he + simp only [↓reduceIte, hready, Bool.true_and] + exact WP.spec_mono (fl4_sufficient_refinesA a H.scr hc hk ha 0#u64 hm) fun _ h => h.symm + +/-- **`replyTailExA` telling the non-timeout continuations that the timeout branch is not taken** +(`¬ ReplyTmoCondA`). -/ +theorem replyTailExTA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {a : A} {σ : State Unit} {t ft : U64} (H : SchedSRB st Z m κ s σ (specPtr t)) + (hst : StEqA Z.T (L.mem s) σ) (ht : t ∈ Z.T) + (hon : L.mem s (affA t) = L.cpu s) (hmb : Fl4MinBudgetAgreeA ar a) + (fault : Option Fault) {x : Tcb} (hkx : σ.kheap (specPtr t) = some (.tcb x)) + (hcls : runnable x.tcbState = true ∨ ¬ inClassRSc x.tcbState x.tcbFault) + (hidle : ∀ c < fl4A64Platform.numNodes, specPtr t ≠ m.idleThreadOf c) + (active : ∀ sc y n, L.mem s (scA t) = sc → sc ≠ 0#u64 → + σ.kheap (scNameA m sc) = some (.schedContext y n) → scActive y = true) + (hv : faults.valid_timeout_handler st s t = ok (isEpCap x.tcbTimeoutHandler)) + (hf : (ft != fl4_abi.consts.fault.Timeout) = !fault.elim false isTimeoutFault) + (hcpu : L.mem s (scA t) ≠ 0#u64 → L.mem s (scCoreA (L.mem s (scA t))) = L.cpu s) + (hnqs : ∀ d p, specPtr t ∉ σ.readyQueues d p) (hnrel : specPtr t ∉ σ.releaseQueue) + {R : S → A → State Unit → Prop} + (same : ¬ ReplyTmoCondA σ (specPtr t) fault → SchedSRA st Z m κ s σ → R s a σ) + (afterSwitch : ¬ ReplyTmoCondA σ (specPtr t) fault → ∀ s' σ' Q', SchedSRA st { Z with Q := Q' } m κ s' σ' → + σ'.kheap = σ.kheap → WritesIn st s s' (SWrA (st := st) Z s) → R s' a σ') + (afterPostpone : ¬ ReplyTmoCondA σ (specPtr t) fault → ∀ s' σ' R', SchedSRA st { Z with R := R' } m κ s' σ' → + σ'.kheap = σ.kheap → specPtr t ∈ σ'.releaseQueue → + WritesIn st s s' (fun b => L.QFootN s (t :: Z.R (L.cpu s)) (L.cpu s) RelOffs t b ∨ + b = L.nodeAt s (L.cpu s) layout.node.REPROGRAM) → R s' a σ') + (timeout : ∀ sc y n (badge : U64), L.mem s (scA t) = sc → sc ≠ 0#u64 → + σ.kheap (scNameA m sc) = some (.schedContext y n) → badge.bv = y.scBadge → + isEpCap x.tcbTimeoutHandler = true → fault.elim false isTimeoutFault = false → + runnable x.tcbState = true → faults.handle_timeout st ar s a t (.Timeout badge) ⦃ r => + ∃ σ', ((), σ') ∈ (fl4HandleTimeout (Z := Unit) (specPtr t) (.timeout badge.bv) σ).1 ∧ + R r.1 r.2 σ' ⦄) : + replyTailProgramA st ar s a t ft ⦃ out => + ∃ σ', ((), σ') ∈ (replyTailSpecA (specPtr t) fault σ).1 ∧ R out.1 out.2 σ' ⦄ := by + obtain ⟨x0, hx, hxs⟩ := H.sch.tcb t ht + have e0 : x0 = x := by + rw [hkx] at hx + exact (KernelObject.tcb.inj (Option.some.inj hx)).symm + rw [e0] at hxs + let sc := L.mem s (scA t) + have hread : obj.Tcb.sc st t s = ok sc := eq_of_spec_fd (obj.Tcb.sc.spec (st := st) s t) + unfold replyTailProgramA + rw [hread, bind_ok] + refine WP.spec_bind (P1.obj.Sc.is_null.spec sc) ?_ + rintro _ rfl + by_cases h0 : sc = 0#u64 + · simp only [h0, decide_true, ↓reduceIte, WP.spec_ok] + have hs : x.tcbSchedContext = none := by rw [hxs, ← show sc = L.mem s (scA t) from rfl, h0]; rfl + refine ⟨σ, by rw [replyTailSpec_skipA hkx (Or.inl hs)]; rfl, same (fun ⟨x', p, y', k', hx', hp, _⟩ => by + rw [hkx] at hx'; cases hx'; rw [hs] at hp; cases hp) (H.toA ?_)⟩ + intro x' scp y n h1 h2 + rw [hkx] at h1 + cases h1 + rw [hs] at h2 + cases h2 + simp only [decide_eq_false h0, Bool.false_eq_true, ↓reduceIte] + rw [replyRunnableExA hst ht hkx, bind_ok] + cases hrun' : runnable x.tcbState + · simp only [Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + have hnc : ¬ inClassRSc x.tcbState x.tcbFault := hcls.resolve_left (by rw [hrun']; decide) + exact ⟨σ, by rw [replyTailSpec_skipA hkx (Or.inr hrun')]; rfl, same (fun ⟨x', p, y', k', hx', _, hr, _⟩ => by + rw [hkx] at hx'; cases hx'; rw [hrun'] at hr; cases hr) (H.toA_ncA hkx hnc)⟩ + have hrun : runnable x.tcbState = true := hrun' + simp only [↓reduceIte] + have hq0 : (L.mem s (inqA t) &&& sched.Q_QUEUED) = 0#u64 := H.toSchedSRN.qBit0 ht hon hnqs + have hr0 : (L.mem s (inqA t) &&& sched.Q_RELEASE) = 0#u64 := (H.relOn ht hon).2 hnrel + have hc : sc ∈ Z.C := H.rest.scMem t ht h0 + obtain ⟨y, n, hy, hyR⟩ := H.scr.sc sc hc + have hxs' : x.tcbSchedContext = some (scNameA m sc) := by + change x.tcbSchedContext = some (scNameA m (L.mem s (scA t))) + rw [hxs, optScA, if_neg h0] + rfl + have hya := active sc y n rfl h0 hy + have htw : L.mem s (scTcbA sc) = t := by + obtain ⟨y', n', hy', hb'⟩ := H.inv.back _ x _ hkx hxs' + rw [hy] at hy' + cases hy' + have e := hyR.tcb + rw [hb', ← scTcbA_eqSc] at e + by_cases hw0 : L.mem s (scTcbA sc) = 0#u64 + · rw [hw0, optRefS_zero] at e + cases e + · rw [optRefS_ne hw0] at e + exact specPtr_inj (Option.some.inj e).symm + have hspec := replyTailSpec_runA hkx hxs' hrun hy fault + refine WP.spec_bind (fl4ReplyBudgetTestExA H.toSchedSRN hc hy hya hmb) ?_ + intro eligible he + cases eligible + · simp only [Bool.false_eq_true, ↓reduceIte] + have nh : ¬(refillReady σ.curTime (refillHd y) = true ∧ fl4RefillSufficient 0 (refillHd y) = true) := by + intro h + apply Bool.false_ne_true + rw [h.1, h.2] at he + exact he + have hb : obj.Sc.badge st sc s = ok (L.mem s (scWA sc 48#u64)) := by + unfold obj.Sc.badge scWA layout.sc.BADGE + simp only [lift, bind_ok, L.read_eq] + have hta : (L.rmax s sc).val ≠ 0 := by + rw [hyR.max]; simp [scActive, activeSc] at hya; omega + have late := replyLate_refinesA (st := st) (ar := ar) (s := s) (a := a) (t := t) (sc := sc) (ft := ft) + (q := scNameA m sc) (y := y) (fault := fault) (R := R) hkx hv hf hb (by rw [hyR.badge]) + (fun h1 h2 => timeout sc y n _ rfl h0 hy (by rw [hyR.badge]) h1 h2 hrun) + (fun hnc => by + refine WP.spec_mono (postpone_refinesA (st := st) H hc ht hon htw rfl h0 (hcpu h0) hq0 hr0 hta hnqs + (fun x' hx' => by rw [hkx] at hx'; cases hx'; exact hrun)) ?_ + rintro s' ⟨σ', R', e, hk, -, -, -, hrel, -, -, w, HS⟩ + exact ⟨σ', by rw [e]; rfl, afterPostpone (fun ⟨x', p, y', k', hx', _, _, _, _, he, hf⟩ => by + rw [hkx] at hx'; cases hx'; exact hnc ⟨he, hf⟩) s' σ' R' HS hk hrel w⟩) + refine WP.spec_mono late ?_ + rintro out ⟨σ', hm, hR⟩ + exact ⟨σ', by rw [hspec, if_neg nh]; exact hm, hR⟩ + · simp only [↓reduceIte] + have hh : refillReady σ.curTime (refillHd y) = true ∧ fl4RefillSufficient 0 (refillHd y) = true := + by simpa only [Bool.and_eq_true] using he.symm + have HA : SchedSRA st Z m κ s σ := H.toA (by + intro x' scp y' n' h1 h2 h3 _ _ + rw [hkx] at h1 + cases h1 + rw [hxs'] at h2 + cases h2 + rw [hy] at h3 + cases h3 + exact ⟨hh.1, ScInvA.suff H.inv hy hya (H.inv.tcbScNotIdle _ x _ hkx hxs' hidle)⟩) + refine WP.spec_bind (possible_switch_to_refinesA HA t ht hon (fun x' hx' => by + rw [hkx] at hx'; cases hx'; exact hrun) hst) ?_ + rintro s' ⟨σ', Q', hs, hk, hw, H'⟩ + refine (WP.spec_ok _).2 ⟨σ', ?_, afterSwitch (fun ⟨x', p, y', k', hx', hp, _, hy', hn, _⟩ => by + rw [hkx] at hx'; cases hx'; rw [hxs'] at hp; cases hp; rw [hy] at hy'; cases hy'; exact hn hh) + s' σ' Q' H' hk hw⟩ + rw [hspec, if_pos hh, hs] + rfl + + +/-- **The SC tail from the woken receiver's relation** (module docstring). -/ +theorem replyTailExA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {a : A} {σ : State Unit} {t ft : U64} (H : SchedSRB st Z m κ s σ (specPtr t)) + (hst : StEqA Z.T (L.mem s) σ) (ht : t ∈ Z.T) + (hon : L.mem s (affA t) = L.cpu s) (hmb : Fl4MinBudgetAgreeA ar a) + (fault : Option Fault) {x : Tcb} (hkx : σ.kheap (specPtr t) = some (.tcb x)) + (hcls : runnable x.tcbState = true ∨ ¬ inClassRSc x.tcbState x.tcbFault) + (hidle : ∀ c < fl4A64Platform.numNodes, specPtr t ≠ m.idleThreadOf c) + (active : ∀ sc y n, L.mem s (scA t) = sc → sc ≠ 0#u64 → + σ.kheap (scNameA m sc) = some (.schedContext y n) → scActive y = true) + (hv : faults.valid_timeout_handler st s t = ok (isEpCap x.tcbTimeoutHandler)) + (hf : (ft != fl4_abi.consts.fault.Timeout) = !fault.elim false isTimeoutFault) + (hcpu : L.mem s (scA t) ≠ 0#u64 → L.mem s (scCoreA (L.mem s (scA t))) = L.cpu s) + (hnqs : ∀ d p, specPtr t ∉ σ.readyQueues d p) (hnrel : specPtr t ∉ σ.releaseQueue) + {R : S → A → State Unit → Prop} + (same : SchedSRA st Z m κ s σ → R s a σ) + (afterSwitch : ∀ s' σ' Q', SchedSRA st { Z with Q := Q' } m κ s' σ' → + σ'.kheap = σ.kheap → WritesIn st s s' (SWrA (st := st) Z s) → R s' a σ') + (afterPostpone : ∀ s' σ' R', SchedSRA st { Z with R := R' } m κ s' σ' → + σ'.kheap = σ.kheap → specPtr t ∈ σ'.releaseQueue → + WritesIn st s s' (fun b => L.QFootN s (t :: Z.R (L.cpu s)) (L.cpu s) RelOffs t b ∨ + b = L.nodeAt s (L.cpu s) layout.node.REPROGRAM) → R s' a σ') + (timeout : ∀ sc y n (badge : U64), L.mem s (scA t) = sc → sc ≠ 0#u64 → + σ.kheap (scNameA m sc) = some (.schedContext y n) → badge.bv = y.scBadge → + isEpCap x.tcbTimeoutHandler = true → fault.elim false isTimeoutFault = false → + faults.handle_timeout st ar s a t (.Timeout badge) ⦃ r => + ∃ σ', ((), σ') ∈ (fl4HandleTimeout (Z := Unit) (specPtr t) (.timeout badge.bv) σ).1 ∧ + R r.1 r.2 σ' ⦄) : + replyTailProgramA st ar s a t ft ⦃ out => + ∃ σ', ((), σ') ∈ (replyTailSpecA (specPtr t) fault σ).1 ∧ R out.1 out.2 σ' ⦄ := + replyTailExTA H hst ht hon hmb fault hkx hcls hidle active hv hf hcpu hnqs hnrel (fun _ => same) + (fun _ => afterSwitch) (fun _ => afterPostpone) + (fun sc y n b e1 e2 e3 e4 e5 e6 _ => timeout sc y n b e1 e2 e3 e4 e5 e6) + +end +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/ReplyUnblockFrameA.lean b/proofs/Fl4ProofsA64/P3/ReplyUnblockFrameA.lean new file mode 100644 index 000000000..5832c9fdf --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ReplyUnblockFrameA.lean @@ -0,0 +1,66 @@ +/- +P3 (AArch64): the reply's refill test keeps the endpoint relation and the +reply stack, and writes only the MCS words. + +[S] The local-SC and release-queue premises are those of +`replyUnblock_refinesA`. The post carries the relational frames and the +footprint that the following IPC transfer needs. +-/ +import Fl4ProofsA64.P3.ReplyRemoveAllFootA +import Fl4ProofsA64.P3.IpcScEndpointRefillA +import Fl4ProofsA64.P3.IpcScReplyTransferA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4SpecA64 Sched + +noncomputable section + +section Store +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + +/-- The reply's refill test on the receiver's SC. -/ +theorem replyUnblockFrameA {s : S} {σ : State Unit} {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {t r : U64} (ht : t ∈ Z.T) (hr : r ∈ R) (hi : ReplyIsolatedA (L.mem s) Z.C stack r) + {x : Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hon : L.mem s (scA t) ≠ 0#u64 → L.mem s (scCoreA (L.mem s (scA t))) = L.cpu s) + (hrq : specPtr t ∈ σ.releaseQueue → ∀ y n, + σ.kheap (scNameA m (L.mem s (scA t))) = some (.schedContext y n) → + refillReady σ.curTime (refillHd y) = false) : + replyUnblockProgramA st s t ⦃ s' => ∃ σ', + (getTcbObjRef (Z := Unit) Tcb.tcbSchedContext (specPtr t) >>= + ifSporadicCurScTestRefillUnblockCheck) σ = ({((), σ')}, False) ∧ + SchedSRA st Z m κ s' σ' ∧ EpStateRel Z.T E R Bf (L.mem s') σ' ∧ + ReplyStackRelA (L.mem s') R Z.C σ' (scNameA m) stack ∧ + ReplyIsolatedA (L.mem s') Z.C stack r ∧ MFrA (st := st) Z m s s' σ σ' ∧ + σ'.curThread = σ.curThread ∧ σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory ∧ + (∀ q y n, σ.kheap q = some (.schedContext y n) → + ∃ y', σ'.kheap q = some (.schedContext y' n) ∧ y'.scReplies = y.scReplies) ⦄ := by + refine WP.spec_mono (replyUnblock_refinesA H ht hon hrq) ?_ + rintro s' ⟨σ', eq, H', F⟩ + have hA : ifSporadicCurScTestRefillUnblockCheck (Z := Unit) x.tcbSchedContext σ = ({((), σ')}, False) := by + rw [nd_bind_oneSc (getTcbObjRef_eq _ hk)] at eq + exact eq + have hres : ((), σ') ∈ (ifSporadicCurScTestRefillUnblockCheck (Z := Unit) x.tcbSchedContext σ).1 := by + rw [hA] + exact Set.mem_singleton _ + have preserve := condUnblockReplyListsA false (fun p hp => by + obtain ⟨y, n, hy, _⟩ := H.inv.back (specPtr t) x p hk hp + exact ⟨y, n, hy⟩) hres + have fm := F.mfr H.toSchedSRN + obtain ⟨FE, VIEW⟩ := epFrameOfMcsA H.toSchedSRN P fm preserve.mem + have links := mcsReplyLinksA H.toSchedSRN P PE fm r hr + refine ⟨σ', eq, H', HE.frame PE FE VIEW, mcsReplyFrameA H.toSchedSRN P PE HE HS fm preserve.sc, + ⟨hi.1, links.1.trans hi.2.1, links.2.trans hi.2.2⟩, fm, F.cur, preserve.mem, ?_⟩ + intro q y n hq + obtain ⟨y', hy', e⟩ := preserve.sc q y n hq + exact ⟨y', hy', e⟩ + +end Store +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/ReplyXferFactsA.lean b/proofs/Fl4ProofsA64/P3/ReplyXferFactsA.lean new file mode 100644 index 000000000..53672a2cc --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ReplyXferFactsA.lean @@ -0,0 +1,113 @@ +/- +P3 (AArch64): the transfer facts after a reply removal. + +`replyXferFactsA` derives the typed-heap, slot, IPC-buffer and native-frame facts that +`do_ipc_transfer_anyA` needs at the post-removal state from the pre-state facts, the concrete +footprint `ReplyFootW` and the spec frame `RemKeepA`. The IRQ-node region is carried by the +temporary [J] `IrqSlotsKeepA` of `ReplyXferKeepA`. +-/ +import Fl4ProofsA64.P3.ReplyXferKeepA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4SpecA64 Sched + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section Store +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + +/-- The words the IPC-buffer lookup reads are unchanged by the removal. -/ +theorem ibKeepA {s s1 : S} {σ : State Unit} + (H : SchedSRN st Z m κ s σ) (PE : EPlaced Z.T E R Bf) (P : IpcSchedPlacementA Z.th N E R Bf) + (w : WritesIn st s s1 (ReplyFootW (st := st) Z m σ s E R Bf)) {t : U64} (ht : t ∈ Z.T) : + IbSame (L := L) s s1 t := by + have hta := PE.tcb t ht + have hk := (H.thT t).2 ht + have hnI : ¬ IrqNodeA (L.ksb s) t := fun h => by + have := H.place.irqK hk h + cases this + have off (K : U64) (hKl : K.val < 4096) (hK : K.val = 3512 ∨ K.val = 64 ∨ K.val = 72) + (hE : ¬ EWrite Z.T E R Bf (wa t K)) (hal : Aligned (wa t K)) : + L.mem s1 (wa t K) = L.mem s (wa t K) := by + have hsz : ObjKindA.tcb.size = 4096 := rfl + refine w.mem_eq hal (objWordOffW H PE P hk (by intro n h; cases h) (by rw [tw_val hta hKl]; omega) + (by rw [tw_val hta hKl, hsz]; omega) (fun _ K' hK' => by rw [tw_val hta hKl]; omega) hE hnI) + refine ⟨off 3512#u64 (by decide) (by decide) + (not_ew_offA PE ht (K := 3512#u64) (by simp) (by unfold TWOffN; simp) (by simp) (by simp)) + (field_aligned hta (by simp) (by simp)), ?_, ?_⟩ + · rw [sbWordA_eq] + exact off 64#u64 (by decide) (by decide) + (not_ew_offA PE ht (K := 64#u64) (by simp) (by unfold TWOffN; simp) (by simp) (by simp)) + (field_aligned hta (by simp) (by simp)) + · rw [sbWordA8_eq] + exact off 72#u64 (by decide) (by decide) + (not_ew_offA PE ht (K := 72#u64) (by simp) (by unfold TWOffN; simp) (by simp) (by simp)) + (field_aligned hta (by simp) (by simp)) + +/-- `StateRelA` across a removal that keeps the slot words and changes TCBs only in state and SC. -/ +theorem stateRelKeepA {th : TypedHeapA} {kb : U64} (hPl : PlaceA th kb) {mem mem' : U64 → U64} + {σ σ1 : State Unit} (h : StateRelA th mem σ) + (hslot : ∀ y, IsSlotA th y → mem' y = mem y ∧ mem' (wa y 8#u64) = mem (wa y 8#u64)) + (keep : RemKeepA σ σ1) : StateRelA th mem' σ1 where + cnode p r hp := by + obtain ⟨cs, h1, h2, h3⟩ := h.cnode p r hp + refine ⟨cs, keep.2.1 _ _ h1 (fun y n e => by cases e) (fun x e => by cases e) (fun x e => by cases e), + h2, fun idx hl => ?_⟩ + have hv := cn_slotV hPl hp hl + have hsl : IsSlotA th (slotAddrA (specPtr p) (ofBl idx)) := + ⟨p, Or.inl ⟨r, hp, blvA idx, hv.2.1, hv.1⟩⟩ + obtain ⟨ac, e, hr⟩ := h3 idx hl + have hs := hslot _ hsl + refine ⟨ac, e, ?_⟩ + unfold capAtA + rw [hs.1, hs.2] + exact hr + tcb t ht := by + obtain ⟨x, h1, h2⟩ := h.tcb t ht + obtain ⟨y, st', sc', hy, rfl⟩ := keep.2.2 _ x h1 + refine ⟨_, hy, fun i hi => ?_⟩ + obtain ⟨ac, e, hr⟩ := h2 i hi + have hs := hslot _ (⟨t, Or.inr ⟨ht, i, hi, rfl⟩⟩ : IsSlotA th (slotAddrA (specPtr t) (BitVec.ofNat 64 i))) + refine ⟨ac, e, ?_⟩ + unfold capAtA + rw [hs.1, hs.2] + exact hr + +/-- `BufCover` across a removal. -/ +theorem bufCoverKeepA {s s1 : S} {σ σ1 : State Unit} {T : Set U64} {Bf' : Set (U64 × Word)} + (hC : BufCover st T Bf' s σ) (ib : ∀ t ∈ T, IbSame (L := L) s s1 t) + (hTcb : ∀ t ∈ T, ∃ x0, σ.kheap (specPtr t) = some (.tcb x0)) (keep : RemKeepA σ σ1) : + BufCover st T Bf' s1 σ1 := by + intro t ht x b sb w hx hb hl + obtain ⟨x0, h0⟩ := hTcb t ht + obtain ⟨y, st', sc', hy, ey⟩ := keep.2.2 _ x0 h0 + have exy : x = y := KernelObject.tcb.inj (Option.some.inj (hx.symm.trans hy)) + subst exy + rw [lookup_ipc_buffer_sameA (ib t ht) b] at hl + rw [ey] at hb + exact hC t ht x0 b sb w h0 hb hl + +/-- **The transfer facts at the post-removal state.** -/ +theorem replyXferFactsA {s s1 : S} {σ σ1 : State Unit} + (H : SchedSRN st Z m κ s σ) (PE : EPlaced Z.T E R Bf) (P : IpcSchedPlacementA Z.th N E R Bf) + (hoff : ∀ y, IsSlotA Z.th y → ¬ EWrite Z.T E R Bf y ∧ ¬ EWrite Z.T E R Bf (wa y 8#u64)) + (w : WritesIn st s s1 (ReplyFootW (st := st) Z m σ s E R Bf)) (keep : RemKeepA σ σ1) + (hirq : IrqSlotsKeepA (st := st) s s1) + (hSt : StateRelA Z.th (L.mem s) σ) (hV : SlotsValidA Z.th (L.mem s)) + (hC : BufCover st Z.T Bf s σ) (hNative : BufNativeA Z.T (L.mem s)) + (hTcb : ∀ t ∈ Z.T, ∃ x0, σ.kheap (specPtr t) = some (.tcb x0)) : + StateRelA Z.th (L.mem s1) σ1 ∧ SlotsValidA Z.th (L.mem s1) ∧ BufCover st Z.T Bf s1 σ1 ∧ + BufNativeA Z.T (L.mem s1) := by + have hslot := slotWordsKeepA H PE P hoff w hirq + have ib := fun t (ht : t ∈ Z.T) => ibKeepA H PE P w ht + refine ⟨stateRelKeepA H.place hSt hslot keep, slotsValid_xferA hV hslot, + bufCoverKeepA hC ib hTcb keep, hNative.frame fun t ht => ⟨(ib t ht).2.1, (ib t ht).2.2⟩⟩ + +end Store +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/ReplyXferKeepA.lean b/proofs/Fl4ProofsA64/P3/ReplyXferKeepA.lean new file mode 100644 index 000000000..5f6a3ac90 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ReplyXferKeepA.lean @@ -0,0 +1,151 @@ +/- +P3 (AArch64): the words a reply removal leaves alone. + +From the footprint `ReplyFootW` of `reply_remove_footA` (and the MCS footprint of the refill +test), every CNode and TCB slot word and the TCB words the IPC-buffer lookup reads are unchanged. +The one exception is the 5,376 IRQ-node slots, which lie inside the scheduler write block. + +[J] `IrqSlotsKeepA` (TEMPORARY, ruled 17:49Z): the removal leaves every word of +`[ksb + IRQ_NODE, ksb + IRQ_NODE + 5376*32)` unchanged. It is a concrete fact of the +scheduler leaves (they write the cores' node words `>= ksb + 223488`, the TCB/SC words and +`CUR_DOMAIN..WORK_UNITS <= ksb + 48`; `fl4-core/src/layout.rs:137-170`). It must be discharged +as a concrete lemma at the Stage-3 merge and must not survive as a top-level premise. +-/ +import Fl4ProofsA64.P3.ReplyRemoveAllFootA +import Fl4ProofsA64.P3.ReplyUnblockFrameA +import Fl4ProofsA64.P3.BufNativeFrA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4SpecA64 Sched + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section Store +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + +/-- [J] (temporary) The step leaves the IRQ-node slot region unchanged. -/ +def IrqSlotsKeepA (s s1 : S) : Prop := + ∀ b : U64, Aligned b → (L.ksb s).val + 43232 ≤ b.val → b.val < (L.ksb s).val + 215264 → + L.mem s1 b = L.mem s b + +/-- A word inside a placed CNode or TCB object that is not an IRQ node is off the footprint. -/ +theorem objWordOffW {s : S} {σ : State Unit} + (H : SchedSRN st Z m κ s σ) (PE : EPlaced Z.T E R Bf) (P : IpcSchedPlacementA Z.th N E R Bf) + {q b : U64} {k : ObjKindA} (hk : Z.th q = some k) (hko : ∀ n, k ≠ .other n) + (hlo : q.val ≤ b.val) (hhi : b.val < q.val + k.size) + (hoffk : k = .tcb → ∀ K : Nat, (K = 3464 ∨ K = 3480 ∨ K = 3488 ∨ K = 3592) → b.val - q.val ≠ K) + (hE : ¬ EWrite Z.T E R Bf b) (hnI : ¬ IrqNodeA (L.ksb s) q) : + ¬ ReplyFootW (st := st) Z m σ s E R Bf b := by + have hK : b.val < (L.ksb s).val ∨ (L.ksb s).val + ksSizeA ≤ b.val := by + have h := H.place.ks q k hk hnI + omega + have hS : ¬ ScFootA Z.C m σ b := by + intro h + have hd := H.scp.th_out h hk + omega + have hT : ∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → + (K.val = 3464 ∨ K.val = 3480 ∨ K.val = 3488 ∨ K.val = 3592) → b ≠ wa u K := by + intro u hu K hKl hKc e + have hv := Notif.tfield_val (H.core.tcbs u hu) hKl + have hty := (H.thT u).2 hu + have ev := congrArg UScalar.val e + rw [hv] at ev + by_cases huq : u = q + · subst huq + rw [hk] at hty + cases hty + exact hoffk rfl K.val hKc (by omega) + · have h := H.place.apart q u k .tcb hk hty (Ne.symm huq) + have h' : q.val + k.size ≤ u.val ∨ u.val + 4096 ≤ q.val := h + omega + rintro (h | h | ⟨r', hr', h | h⟩) + · exact hE h + · exact (not_mwrA (st := st) (Z := Z) (m := m) (σ := σ) (s := s) hS + (fun u hu => ⟨hT u hu 3464#u64 (by decide) (by decide), hT u hu 3480#u64 (by decide) (by decide), + hT u hu 3488#u64 (by decide) (by decide), hT u hu 3592#u64 (by decide) (by decide)⟩) hK) h + · obtain ⟨na, nv⟩ := replyNextPlaceA (Z := Z) PE hr' + have hthr := P.reply r' hr' + have hne : q ≠ r' := by + intro e + subst e + rw [hk] at hthr + exact hko 32 (Option.some.inj hthr) + have ap := H.place.apart q r' k (.other 32) hk hthr hne + have ap' : q.val + k.size ≤ r'.val ∨ r'.val + 32 ≤ q.val := ap + have ev := congrArg UScalar.val h + rw [nv] at ev + omega + · obtain ⟨pa, pv⟩ := replyPrevPlaceA (Z := Z) PE hr' + have hthr := P.reply r' hr' + have hne : q ≠ r' := by + intro e + subst e + rw [hk] at hthr + exact hko 32 (Option.some.inj hthr) + have ap := H.place.apart q r' k (.other 32) hk hthr hne + have ap' : q.val + k.size ≤ r'.val ∨ r'.val + 32 ≤ q.val := ap + have ev := congrArg UScalar.val h + rw [pv] at ev + omega + +/-- Every slot word the transfer's relations read is unchanged by the removal. -/ +theorem slotWordsKeepA {s s1 : S} {σ : State Unit} + (H : SchedSRN st Z m κ s σ) (PE : EPlaced Z.T E R Bf) (P : IpcSchedPlacementA Z.th N E R Bf) + (hoff : ∀ y, IsSlotA Z.th y → ¬ EWrite Z.T E R Bf y ∧ ¬ EWrite Z.T E R Bf (wa y 8#u64)) + (w : WritesIn st s s1 (ReplyFootW (st := st) Z m σ s E R Bf)) (hirq : IrqSlotsKeepA (st := st) s s1) : + ∀ y, IsSlotA Z.th y → L.mem s1 y = L.mem s y ∧ L.mem s1 (wa y 8#u64) = L.mem s (wa y 8#u64) := by + intro y hy0 + have hal := IsSlotA.al32 H.place hy0 + have hyA : Aligned y := by unfold Aligned; omega + have hy8A : Aligned (wa y 8#u64) := wa_al hyA (by simp) + have hE := hoff y hy0 + have hfit := H.ksFit + -- the object of the slot + have obj : ∃ (q : U64) (k : ObjKindA) (j : Nat), Z.th q = some k ∧ (∀ n, k ≠ .other n) ∧ 32 * (j + 1) ≤ k.size ∧ + y.val = q.val + 32 * j ∧ (k = .tcb → j < tcbSlotsA) := by + rcases hy0 with ⟨p, ⟨r, hr, i, hi, rfl⟩ | ⟨ht, i, hi, rfl⟩⟩ + · obtain ⟨k, hk, hs, hv⟩ := slotVA_of H.place hr (j := i) (by rw [cnode_size]; omega) + rw [hr] at hk + cases hk + refine ⟨p, .cnode r, i, hr, ?_, hs, hv, ?_⟩ + · intro n h; cases h + · intro h; cases h + · obtain ⟨k, hk, hs, hv⟩ := tcb_slotV H.place ht hi + rw [ht] at hk + cases hk + refine ⟨p, .tcb, i, ht, ?_, hs, hv, fun _ => hi⟩ + intro n h; cases h + obtain ⟨q, k, j, hk, hko, hs, hv, hj⟩ := obj + have hf := H.place.fit q k hk + have hy8v : (wa y 8#u64).val = y.val + 8 := by + rw [wa_valm] + have : (8#u64 : U64).val = 8 := by decide + omega + have key (b : U64) (hbA : Aligned b) (d : Nat) (hd : d = 0 ∨ d = 8) (hbv : b.val = y.val + d) + (hEb : ¬ EWrite Z.T E R Bf b) : L.mem s1 b = L.mem s b := by + by_cases hirqObj : IrqNodeA (L.ksb s) q + · obtain ⟨ii, hii, hq⟩ := hirqObj + have hqv : q.val = (L.ksb s).val + 43232 + 32 * ii.val := by + rw [hq] + exact irqSlotA_val hfit hii + have hsz : k.size = 32 := by + have := H.place.irq ii hii + rw [← hq, hk] at this + cases Option.some.inj this + rfl + apply hirq b hbA <;> omega + · refine w.mem_eq hbA (objWordOffW H PE P hk hko (by omega) (by omega) ?_ hEb hirqObj) + intro ht K hK + have := hj ht + unfold tcbSlotsA at this + rcases hd with rfl | rfl <;> rcases hK with rfl | rfl | rfl | rfl <;> omega + exact ⟨key y hyA 0 (Or.inl rfl) (by omega) hE.1, key _ hy8A 8 (Or.inr rfl) hy8v hE.2⟩ + +end Store +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/RestartA.lean b/proofs/Fl4ProofsA64/P3/RestartA.lean new file mode 100644 index 000000000..7edd35c9f --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/RestartA.lean @@ -0,0 +1,726 @@ +/- +P3 (aarch64, A64-P3 S3 gen 10, ruling 13:44Z item 1): **the caller's restart keeps `JEntA`** — the A64 form of x86's +`P3/Restart` (`setThreadState_eq`, `restart_rel`). The CNode invocations set the caller to `Restart` before they +change slots (FL4 `decode_copy` / `decode_rotate`, the spec in `handle_invocation`); the A64 leaves take a whole +`JEntA`, so this proves once that the restart keeps it (x86 carried only the CSpace part, with `SlotsSame` explicit). + +* Spec side, **`setThreadState_eqA`**: for a TCB of the heap whose restart leaves it schedulable (or that is not the + current thread under `resume_cur_thread`), l4v's `set_thread_state` has the single outcome `setObjA σ t (tcb …)`: + the TCB's `tcb_state` replaced, nothing else (no scheduler action change). `schedulable_ofA`: a runnable TCB bound + to an active SC, outside the release queue, is schedulable. +* FL4 side, **`restart_wrA`**: `set_thread_state t TS_RESTART` of a bound thread with an active SC, out of the release + queue, is the one `STATE` write (`schedule_tcb` does nothing: `is_schedA`, A64 P1's `schedule_tcb_sch`). +* **`JEntA.jxRestartA`**: `JAtZAX` across that write and that spec update — the footprint words other than the + thread's `STATE` word keep their values (each kind apart from `stA t`: node words by `NodePlaced`, TCB fields by + `TcbAddr`, SC / IRQ / global words as `Ω` words, slot words by `PlaceA`, VCPU words by `hvc`), the thread's state + code becomes `Restart`'s. +* **`SInvXA.restartA`**: `SInvXA` across the spec update (the caller is not the idle thread, `Restart` is runnable, + the IRQ node untouched: `SpecIrqA.updTcbA`). +* **`JEntA.restartA`**: the whole step: FL4's restart is the `STATE` write, the spec's has the single outcome, and + `JEntA` holds afterwards (A64 P1's `SysL` / `SymL` across the restart by `sched.set_thread_state.dfR` and + `SysL.df`, `BXI` and the queue ghosts by `sts_all`, `EntryInvA`'s `UserOK` / `VsTy` by the frame), with `SInvXA` + carried and every slot word kept. + +Case hypotheses of `JEntA.restartA` (decode-site facts, not premises; P1's own decode lemmas take the first four): +`SysPlace` and `SymL` at the decode (`P`, `Y`, as A64 P1's `restartS`), the caller runnable (`hrun`, `DecPre.run`), +bound to an active SC out of the release queue (`hcur`, P1's `restart_hB` shape); and two placement facts the entry +relation does not carry: the VSpace words `VsTy` reads are aligned and not the caller's `STATE` word (`hvs`; x86 +`TopPlaced.disj`), the VCPU words of `Z.V` are not the caller's `STATE` word (`hvc`; x86 `TopPlaced.vo`). + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.CnodeCapsA +import Fl4ProofsA64.P1.FastRR +import Fl4ProofsA64.P1.MdbIrqC +import Fl4ProofsA64.P1.MdbArmTab +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## The spec's `set_thread_state` -/ + +theorem nd_bind_oneA {σ α β : Type} {m : NonDet σ α} {s s' : σ} {a : α} (h : m s = ({(a, s')}, False)) + (f : α → NonDet σ β) : (m >>= f) s = f a s' := by + simp only [nd_bind_applyA, h, Set.mem_singleton_iff, exists_eq_left, false_or] + ext <;> simp + +/-- A runnable TCB bound to an active SC, outside the release queue, is schedulable (l4v `schedulable`). -/ +theorem schedulable_ofA {σ : State Unit} {t : ObjRef} {x : Tcb} (hk : σ.kheap t = some (.tcb x)) + (hr : runnable x.tcbState = true) {sc : ObjRef} (hs : x.tcbSchedContext = some sc) {y : SchedContext} {n : Nat} + (hy : σ.kheap sc = some (.schedContext y n)) (ha : 0 < y.scRefillMax) (hq : t ∉ σ.releaseQueue) : + schedulable t σ = true := by + simp [schedulable, optPred, optMap, tcbsOfKh, tcbOf, scsOfKh, scOf, hk, hr, hs, hy, activeSc, ha, hq] + +/-- **l4v's `set_thread_state` of a TCB** (x86 `setThreadState_eq`): when the update leaves the thread schedulable +(or it is not the current thread under `resume_cur_thread`), the single outcome is the TCB with its state replaced. -/ +theorem setThreadState_eqA (σ : State Unit) (t : ObjRef) (x : Tcb) (hk : σ.kheap t = some (.tcb x)) + (ts : ThreadState) + (hs : t = σ.curThread → σ.schedulerAction = .resumeCurThread → + schedulable t (setObjA σ t (.tcb { x with tcbState := ts })) = true) : + setThreadState (Z := Unit) t ts σ = ({((), setObjA σ t (.tcb { x with tcbState := ts }))}, False) := by + have hg : NonDet.getsThe (getTcb (Z := Unit) t) σ = ({(x, σ)}, False) := by + simp [NonDet.getsThe, getTcb, readObject, hk, NonDet.assertOpt]; rfl + have ho : getObject (Z := Unit) t σ = ({(.tcb x, σ)}, False) := by + simp [getObject, NonDet.getsThe, readObject, hk, NonDet.assertOpt]; rfl + have ha : (NonDet.assert (aType (.tcb x) = aType (.tcb { x with tcbState := ts })) : + NonDet (State Unit) Unit) σ = ({((), σ)}, False) := by + simp [NonDet.assert, aType] + have h1 : threadSet (Z := Unit) (fun tcb => { tcb with tcbState := ts }) t σ = + ({((), setObjA σ t (.tcb { x with tcbState := ts }))}, False) := by + unfold threadSet + rw [nd_bind_singleA hg] + unfold setObject + rw [nd_bind_singleA ho, nd_bind_singleA ha, nd_bind_singleA (show NonDet.get σ = ({(σ, σ)}, False) from rfl)] + rfl + unfold setThreadState + rw [nd_bind_oneA h1] + unfold setThreadStateAct + rw [nd_bind_singleA (show NonDet.gets _ _ = _ from rfl), nd_bind_singleA (show NonDet.gets _ _ = _ from rfl), + nd_bind_singleA (show NonDet.gets _ _ = _ from rfl)] + unfold NonDet.when + split + · rename_i h + obtain ⟨h1, h2, h3⟩ := h + exact absurd (hs h1 h2) h3 + · rfl + +/-! ## FL4's restart -/ + +section fl4 +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- `is_schedulable` of a runnable thread bound to an active SC outside the release queue (A64 P2's `is_sched_of`; +the P2 timer files are outside this line's build). -/ +theorem is_schedA {X : S} {t : U64} (h1 : Runnable (L.mem X (stA t))) (h2 : L.mem X (scA t) ≠ 0#u64) + (h3 : (L.mem X (inqA t) &&& sched.Q_RELEASE) = 0#u64) (h4 : L.rmax X (L.mem X (scA t)) ≠ 0#u64) : + sched.is_schedulable st X t = ok true := by + unfold Runnable at h1 + unfold sched.is_schedulable + rw [eq_of_spec_fd (sched.is_runnable.spec (L := L) X t), decide_eq_true h1] + simp only [bind_ok, ↓reduceIte] + rw [eq_of_spec_fd (obj.Tcb.sc.spec (L := L) X t)] + simp only [bind_ok] + rw [eq_of_spec_fd (obj.Sc.is_null.spec _), decide_eq_false h2] + simp only [bind_ok, Bool.false_eq_true, ↓reduceIte] + unfold sporadic.sc_active + rw [eq_of_spec_fd (obj.Sc.refill_max.spec (L := L) X _)] + have h4' : L.rmax X (L.mem X (scA t)) > 0#u64 := by + have : (L.rmax X (L.mem X (scA t))).val ≠ 0 := fun e => h4 (UScalar.eq_of_val_eq (by simpa using e)) + scalar_tac + simp only [bind_ok, h4', decide_true, ↓reduceIte] + rw [eq_of_spec_fd (sched.in_release_queue.spec (L := L) X t)] + simp [h3] + +theorem maxA_eqA (c : U64) : maxA c = wa c 72#u64 := by unfold maxA layout.sc.REFILL_MAX; rfl + +/-- **FL4's restart of a bound thread with an active SC, out of the release queue, is its `STATE` write.** -/ +theorem restart_wrA {s : S} {t : U64} (hta : TcbAddr t) + (hsc : L.mem s (scA t) ≠ 0#u64) (hrm : L.rmax s (L.mem s (scA t)) ≠ 0#u64) + (hiq : (L.mem s (inqA t) &&& sched.Q_RELEASE) = 0#u64) + (hma : Aligned (maxA (L.mem s (scA t)))) (hmx : stA t ≠ maxA (L.mem s (scA t))) : + sched.set_thread_state st s t sched.TS_RESTART = ok (L.wr s (stA t) sched.TS_RESTART) := by + have sA : Aligned (stA t) := by rw [stA_eq]; exact field_aligned hta (by simp) (by simp) + have aS : Aligned (scA t) := field_aligned (K := 3464#u64) hta (by simp) (by simp) + have aQ : Aligned (inqA t) := field_aligned (K := 3592#u64) hta (by simp) (by simp) + have nsc : stA t ≠ scA t := by + rw [stA_eq]; exact field_ne (K2 := 3464#u64) hta hta (by simp) (by simp) (fun _ => by simp) + have niq : stA t ≠ inqA t := by + rw [stA_eq]; exact field_ne (K2 := 3592#u64) hta hta (by simp) (by simp) (fun _ => by simp) + have hs : sched.is_schedulable st (L.wr s (stA t) sched.TS_RESTART) t = ok true := by + apply is_schedA + · rw [L.mem_wr_same sA]; exact runnable_restart + · rw [mem_wr_ne' sA aS nsc]; exact hsc + · rw [mem_wr_ne' sA aQ niq]; exact hiq + · rw [mem_wr_ne' sA aS nsc] + show L.mem _ (maxA _) ≠ 0#u64 + rw [mem_wr_ne' sA hma hmx]; exact hrm + unfold sched.set_thread_state + rw [eq_of_spec_fd (obj.Tcb.set_state.spec (L := L) s t _)] + simp only [bind_ok] + exact schedule_tcb_sch _ _ hs + +end fl4 + +/-! ## The relation across the restart -/ + +section rel +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-- The spec update of the restart: the TCB at `t` with its state replaced. -/ +abbrev stUpdA (σ : State Unit) (t : ObjRef) (x : Tcb) (ts : ThreadState) : State Unit := + setObjA σ t (.tcb { x with tcbState := ts }) + +theorem stUpd_keep {σ : State Unit} {t : ObjRef} {x : Tcb} {ts : ThreadState} {p : ObjRef} {o : KernelObject} + (hk : σ.kheap t = some (.tcb x)) (h : σ.kheap p = some o) (ho : ∀ y, o ≠ .tcb y) : + (stUpdA σ t x ts).kheap p = some o := by + have e : p ≠ t := fun e => by subst e; rw [hk] at h; exact ho x (Option.some.inj h).symm + simp [setObjA, Function.update_of_ne e, h] + +theorem stUpd_back {σ : State Unit} {t : ObjRef} {x : Tcb} {ts : ThreadState} {p : ObjRef} {o : KernelObject} + (h : (stUpdA σ t x ts).kheap p = some o) (ho : ∀ y, o ≠ .tcb y) : σ.kheap p = some o := by + by_cases e : p = t + · subst e; simp [setObjA] at h; exact absurd h.symm (ho _) + · simpa [setObjA, Function.update_of_ne e] using h + +/-- The footprint words other than the thread's `STATE` word are apart from it (module docstring). -/ +structure StOffA (Z : XSetsA) (s : S) (t : U64) : Prop where + node : ∀ c ∈ Z.Cs, ∀ o : U64, o.val < 4288 → L.nodeAt s c o ≠ stA t + fld : ∀ y ∈ Z.T, ∀ K : U64, K.val < 4096 → (y = t → K.val ≠ 3328) → wa y K ≠ stA t + om : ∀ b, (b = wa (L.ksb s) 8#u64 ∨ b = wa (L.ksb s) 16#u64 ∨ (∃ i : U64, i.val < 5376 ∧ b = irqWordA (L.ksb s) i) ∨ + ∃ c ∈ Z.C, b = wa c 24#u64 ∨ b = wa c 72#u64) → b ≠ stA t + slot : ∀ y, SlotWA Z.th y → ∀ K : U64, K.val < 32 → wa y K ≠ stA t + vc : ∀ v ∈ Z.V, v ≠ stA t ∧ wa v 24#u64 ≠ stA t + /-- (gen 12) The VSpace relation's words. -/ + vs : ∀ b, VsFootA Z.Pp Z.S1 Z.Y (L.ksb s) b → b ≠ stA t + /-- (S3 gen 18) The Stage-2 / high-window relation's words. -/ + ext : ∀ b, ExtFootE Z.X Z.Y Z.T2 b → b ≠ stA t + +/-- A slot word of a placed typed heap by value, with its object. -/ +theorem slotW_valA {th : TypedHeapA} {kb : U64} (hP : PlaceA th kb) {y : U64} (hy : SlotWA th y) : + ∃ q k j, th q = some k ∧ 32 * (j + 1) ≤ k.size ∧ y.val = q.val + 32 * j ∧ (k = .tcb → j < 7) := by + rcases hy with ⟨p, ⟨r, hr, i, hi, rfl⟩ | ⟨ht, i, hi, rfl⟩⟩ | ⟨p, r, hr, idx, hl, rfl⟩ + · obtain ⟨k, hk, h1, h2⟩ := slotVA_of hP hr (j := i) (by rw [cnode_size]; omega) + exact ⟨p, k, i, hk, h1, h2, fun e => by rw [hr] at hk; cases hk; cases e⟩ + · obtain ⟨k, hk, h1, h2⟩ := tcb_slotV hP ht hi + exact ⟨p, k, i, hk, h1, h2, fun _ => by unfold tcbSlotsA at hi; omega⟩ + · obtain ⟨-, -, k, hk, h1, h2⟩ := cn_slotV hP hr hl + exact ⟨p, k, _, hk, h1, h2, fun e => by rw [hr] at hk; cases hk; cases e⟩ + +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {s : S} {a : A} {σ : State Unit} {m : McState} + +/-- **The footprint is apart from the current thread's `STATE` word** at an entry state, given the VCPU words' (`hvc`). -/ +theorem JEntA.stOffA (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + (hvc : ∀ v ∈ Z.V, stA (L.curW s) ≠ v ∧ stA (L.curW s) ≠ wa v 24#u64) : StOffA (L := L) Z s (L.curW s) := by + have q := E.sq + obtain ⟨-, -, hX, -, hS⟩ := E.sys.ent.bxi + have hT : ∀ y, y ∈ Z.T → y ∈ H.tcbs := fun y hy => by rw [E.tcbs]; exact hy + have hct : L.curW s ∈ H.tcbs := q.curT + have hta : TcbAddr (L.curW s) := q.tcbs _ hct + have hst : stA (L.curW s) = wa (L.curW s) 3328#u64 := stA_eq _ + have hΩ : ∀ b ∈ H.omega, b ≠ stA (L.curW s) := fun b hb e => by + have := out_ne_field (q.omega b hb) hct hta (K := 3328#u64) (by simp) + rw [← hst, e] at this; exact this rfl + have hks : ∀ o : U64, o.val < 223488 → o.val % 8 = 0 → wa (L.ksb s) o ≠ stA (L.curW s) := + fun o h1 h2 => hΩ _ (hS.ksΩ o h1 h2) + refine ⟨fun c hc o ho => ?_, fun y hy K hK hne => ?_, fun b hb => ?_, fun y hy K hK => ?_, fun v hv => ?_, + fun b hb => ?_, fun b hb => ?_⟩ + · rw [hst]; exact node_ne_field (q.nodes c hc) ho hct hta (by simp) + · rw [hst]; exact field_ne (q.tcbs _ (hT y hy)) hta hK (by simp) (fun e => hne e) + · rcases hb with rfl | rfl | ⟨i, hi, rfl⟩ | ⟨c, hc, rfl | rfl⟩ + · exact hks _ (by simp) (by simp) + · exact hks _ (by simp) (by simp) + · exact hks (irqOffA i) (by rw [irqOffA_val hi]; omega) (by rw [irqOffA_val hi]; omega) + · rw [← E.scs] at hc + have e : scTcbA c = wa c 24#u64 := by unfold scTcbA layout.sc.TCB; rfl + rw [← e]; exact hΩ _ (hS.sub _ (hX.1.fixW _ hc).1) + · rw [← E.scs] at hc; rw [← maxA_eqA]; exact hΩ _ (hS.sub _ (hS.all _ hc).2.1) + · obtain ⟨p, k, j, hp, hj, hyv, hk7⟩ := slotW_valA E.j.1.place hy + have hf := E.j.1.place.fit p k hp + have ht : Z.th (L.curW s) = some .tcb := (E.j.1.thT _).2 (by rw [← E.tcbs]; exact hct) + intro e + have ev := congrArg UScalar.val e + rw [hst, tcb_wadd_val hta (by simp), wa_valm] at ev + have e1 : (y.val + K.val) % 2 ^ 64 = y.val + K.val := Nat.mod_eq_of_lt (by omega) + rw [e1] at ev + have h3328 : (3328#u64 : U64).val = 3328 := by simp + rw [h3328] at ev + by_cases ep : p = L.curW s + · subst ep + rw [ht] at hp; cases hp + have := hk7 rfl; omega + · have hsz : (ObjKindA.tcb).size = 4096 := rfl + rcases E.j.1.place.apart p (L.curW s) k .tcb hp ht ep with h | h <;> omega + · obtain ⟨h1, h2⟩ := hvc v hv; exact ⟨fun e => h1 e.symm, fun e => h2 e.symm⟩ + · intro e + have ev := tcb_wadd_val hta (o := 3328#u64) (by simp) + rw [← hst, ← e] at ev + have h3328 : (3328#u64 : U64).val = 3328 := by simp + rw [h3328] at ev + rcases (E.ap.vs b hb).2 _ hct with h | h <;> omega + · intro e + have ev := tcb_wadd_val hta (o := 3328#u64) (by simp) + rw [← hst, ← e] at ev + have h3328 : (3328#u64 : U64).val = 3328 := by simp + rw [h3328] at ev + rcases (E.ap.ext b hb).2 _ hct with h | h <;> omega + +/-- `JAtZAX` across the restart's `STATE` write and its spec update (module docstring). -/ +theorem JAtZAX.of_stA {t : U64} (J : JAtZAX (L := L) AR Z s a σ m) (O : StOffA (L := L) Z s t) (ht : t ∈ Z.T) + {x : Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) {ts : ThreadState} {v : U64} (hv : v.val = tsCodeA ts) + (hal : ∀ b, FootA (L := L) Z s b → Aligned b) (sA : Aligned (stA t)) : + JAtZAX (L := L) AR Z (L.wr s (stA t) v) a (stUpdA σ (specPtr t) x ts) m := by + obtain ⟨J, V, X, Y⟩ := J + set s2 := L.wr s (stA t) v + set σ1 := stUpdA σ (specPtr t) x ts + have kb : L.ksb s2 = L.ksb s := L.ksb_wr _ _ _ + have kc : L.cpu s2 = L.cpu s := L.cpu_wr _ _ _ + have hm : ∀ w, FootA (L := L) Z s w → w ≠ stA t → L.mem s2 w = L.mem s w := + fun w hw hn => mem_wr_ne' sA (hal w hw) (fun e => hn e.symm) + have hnA : ∀ c ∈ Z.Cs, ∀ o, FootNA (L := L) Z s (L.nodeAt s c o) → o.val < 4288 → + L.mem s2 (L.nodeAt s2 c o) = L.mem s (L.nodeAt s c o) := fun c hc o hf ho => by + rw [nodeAt_ksb kb]; exact hm _ (.nslot _ hf) (O.node c hc o ho) + have hN : ∀ c ∈ Z.Cs, L.mem s2 (L.nodeAt s2 c layout.node.CUR_THREAD) = L.mem s (L.nodeAt s c layout.node.CUR_THREAD) ∧ + L.mem s2 (L.nodeAt s2 c layout.node.IDLE_THREAD) = L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) ∧ + L.mem s2 (L.nodeAt s2 c layout.node.CUR_SC) = L.mem s (L.nodeAt s c layout.node.CUR_SC) ∧ + L.mem s2 (L.nodeAt s2 c layout.node.IDLE_SC) = L.mem s (L.nodeAt s c layout.node.IDLE_SC) := fun c hc => + ⟨hnA c hc _ (.node _ _ hc (Or.inl rfl)) (by simp [layout.node.CUR_THREAD]), + hnA c hc _ (.node _ _ hc (Or.inr (Or.inl rfl))) (by simp [layout.node.IDLE_THREAD]), + hnA c hc _ (.node _ _ hc (Or.inr (Or.inr (Or.inl rfl)))) (by simp [layout.node.CUR_SC]), + hnA c hc _ (.node _ _ hc (Or.inr (Or.inr (Or.inr rfl)))) (by simp [layout.node.IDLE_SC])⟩ + have hcpu := J.sch.cpuCs + have hcur : ∀ o, (o = layout.node.SCHED_ACTION ∨ o = layout.node.CUR_TIME ∨ o = layout.node.CONSUMED ∨ + o = layout.node.REPROGRAM) → L.mem s2 (L.nodeAt s2 (L.cpu s2) o) = L.mem s (L.nodeAt s (L.cpu s) o) := by + intro o ho + rw [kc] + have hf : FootNA (L := L) Z s (L.nodeAt s (L.cpu s) o) := by + rcases ho with rfl | rfl | rfl | rfl + · exact .cur _ (Or.inl rfl) + · exact .cur _ (Or.inr rfl) + · exact .cur2 _ (Or.inl rfl) + · exact .cur2 _ (Or.inr rfl) + have ho' : o.val < 4288 := by + rcases ho with rfl | rfl | rfl | rfl <;> + simp [layout.node.SCHED_ACTION, layout.node.CUR_TIME, layout.node.CONSUMED, layout.node.REPROGRAM] + exact hnA _ hcpu o hf ho' + have hF : ∀ y ∈ Z.T, ∀ K : U64, K.val < 4096 → (y = t → K.val ≠ 3328) → FootNA (L := L) Z s (wa y K) → + L.mem s2 (wa y K) = L.mem s (wa y K) := fun y hy K hK hne hf => hm _ (.nslot _ hf) (O.fld y hy K hK hne) + have hTW : ∀ y ∈ Z.T, L.mem s2 (scA y) = L.mem s (scA y) ∧ L.mem s2 (affA y) = L.mem s (affA y) ∧ + L.mem s2 (wa y 3664#u64) = L.mem s (wa y 3664#u64) ∧ L.mem s2 (runVcpuA y) = L.mem s (runVcpuA y) ∧ + L.mem s2 (prioA y) = L.mem s (prioA y) ∧ L.mem s2 (domA y) = L.mem s (domA y) := fun y hy => + ⟨hF y hy 3464#u64 (by simp) (fun _ => by simp) (.tcb _ _ hy (Or.inr (Or.inl rfl))), + hF y hy 3520#u64 (by simp) (fun _ => by simp) (.tcb _ _ hy (Or.inr (Or.inr (Or.inl rfl)))), + hF y hy 3664#u64 (by simp) (fun _ => by simp) (.tcb _ _ hy (Or.inr (Or.inr (Or.inr (Or.inl rfl))))), + hF y hy 3672#u64 (by simp) (fun _ => by simp) (.tcb _ _ hy (Or.inr (Or.inr (Or.inr (Or.inr rfl))))), + hF y hy 3448#u64 (by simp) (fun _ => by simp) (.tcb2 _ _ hy (Or.inl rfl)), + hF y hy 3432#u64 (by simp) (fun _ => by simp) (.tcb2 _ _ hy (Or.inr rfl))⟩ + have hSt : ∀ y ∈ Z.T, y ≠ t → L.mem s2 (stA y) = L.mem s (stA y) := fun y hy hne => by + rw [stA_eq]; exact hF y hy 3328#u64 (by simp) (fun e => absurd e hne) (by rw [← stA_eq]; exact .tcb _ _ hy (Or.inl rfl)) + have hst2 : L.mem s2 (stA t) = v := L.mem_wr_same sA + have hOm : ∀ b, (b = wa (L.ksb s) 8#u64 ∨ b = wa (L.ksb s) 16#u64 ∨ + (∃ i : U64, i.val < 5376 ∧ b = irqWordA (L.ksb s) i) ∨ ∃ c ∈ Z.C, b = wa c 24#u64 ∨ b = wa c 72#u64) → + FootNA (L := L) Z s b → L.mem s2 b = L.mem s b := fun b hb hf => hm _ (.nslot _ hf) (O.om b hb) + have hSl : ∀ y, SlotWA Z.th y → L.mem s2 y = L.mem s y ∧ L.mem s2 (wa y 8#u64) = L.mem s (wa y 8#u64) ∧ + L.mem s2 (wa y 16#u64) = L.mem s (wa y 16#u64) ∧ L.mem s2 (wa y 24#u64) = L.mem s (wa y 24#u64) := by + intro y hy + have e0 : wa y 0#u64 = y := by + apply UScalar.eq_of_val_eq; rw [wa_valm]; simp + refine ⟨?_, hm _ (.slot _ _ hy (Or.inr (Or.inl rfl))) (O.slot y hy 8#u64 (by simp)), + hm _ (.slot _ _ hy (Or.inr (Or.inr (Or.inl rfl)))) (O.slot y hy 16#u64 (by simp)), + hm _ (.slot _ _ hy (Or.inr (Or.inr (Or.inr rfl)))) (O.slot y hy 24#u64 (by simp))⟩ + exact hm _ (.slot _ _ hy (Or.inl rfl)) (by have := O.slot y hy 0#u64 (by simp); rwa [e0] at this) + have hcap : ∀ y, SlotWA Z.th y → capAtA (L.mem s2) y = capAtA (L.mem s) y := fun y hy => by + obtain ⟨e1, e2, -, -⟩ := hSl y hy; unfold capAtA; rw [e1, e2] + have hkt : σ1.kheap (specPtr t) = some (.tcb { x with tcbState := ts }) := by simp [σ1, setObjA] + have hkn : ∀ y, y ≠ t → σ1.kheap (specPtr y) = σ.kheap (specPtr y) := fun y hy => by + simp [σ1, setObjA, Function.update_of_ne (bv_ne hy)] + have hsc : ∀ c ∈ Z.C, σ1.kheap (scRenA m c) = σ.kheap (scRenA m c) := fun c hc => by + obtain ⟨y, n, e, -⟩ := J.sch.sc c hc + rw [e]; exact stUpd_keep hk e (fun _ h => by cases h) + have hTk : ∀ y ∈ Z.T, ∀ x', σ.kheap (specPtr y) = some (.tcb x') → ∃ x'', σ1.kheap (specPtr y) = some (.tcb x'') ∧ + x''.tcbSchedContext = x'.tcbSchedContext ∧ x''.tcbDomain = x'.tcbDomain ∧ + x''.tcbPriority = x'.tcbPriority ∧ x''.tcbArch = x'.tcbArch ∧ x''.tcbExt = x'.tcbExt ∧ + (y ≠ t → x'' = x') := fun y hy x' e => by + by_cases ey : y = t + · subst ey; rw [hk] at e; cases e + exact ⟨_, hkt, rfl, rfl, rfl, rfl, rfl, fun h => absurd rfl h⟩ + · exact ⟨x', by rw [hkn y ey]; exact e, rfl, rfl, rfl, rfl, rfl, fun _ => rfl⟩ + have st' : StateRelA Z.th (L.mem s2) σ1 := by + refine ⟨fun p r hp => ?_, fun y hy => ?_⟩ + · obtain ⟨cs, h1, h2, h3⟩ := J.st.cnode p r hp + refine ⟨cs, stUpd_keep hk h1 (fun _ h => by cases h), h2, fun idx hl => ?_⟩ + obtain ⟨ac, e, hr⟩ := h3 idx hl + exact ⟨ac, e, by rw [hcap _ (Or.inr ⟨p, r, hp, idx, hl, rfl⟩)]; exact hr⟩ + · obtain ⟨x', h1, h2⟩ := J.st.tcb y hy + have hyT : y ∈ Z.T := (J.thT y).1 hy + by_cases ey : y = t + · subst ey; rw [hk] at h1; cases h1 + refine ⟨_, hkt, fun i hi => ?_⟩ + obtain ⟨ac, e, hr⟩ := h2 i hi + exact ⟨ac, e, by rw [hcap _ (Or.inl ⟨y, Or.inr ⟨hy, i, hi, rfl⟩⟩)]; exact hr⟩ + · refine ⟨x', by rw [hkn y ey]; exact h1, fun i hi => ?_⟩ + obtain ⟨ac, e, hr⟩ := h2 i hi + exact ⟨ac, e, by rw [hcap _ (Or.inl ⟨y, Or.inr ⟨hy, i, hi, rfl⟩⟩)]; exact hr⟩ + have cdt' : CdtRelA st Z.th s2 σ1 Z.G := + CdtRelA.mkA (treeA_of_mem J.cdt.tree fun y hy => hSl y (Or.inl hy)) J.cdt.cdt J.cdt.cdt_slot + (fun sl hs => by + have := J.cdt.orig sl hs + unfold revA at this ⊢; rw [(hSl _ (absSlot_w hs)).2.2.2]; exact this) + J.cdt.par_slot J.cdt.po + have sch' : SchedRelA (L := L) Z.T Z.C Z.Cs Z.curOf Z.curScOf s2 σ1 m := + { core := by rw [kc]; exact J.sch.core + cpuCs := by rw [kc]; exact J.sch.cpuCs + curOfW := fun c hc => by rw [(hN c hc).1]; exact J.sch.curOfW c hc + curScOfW := fun c hc => by rw [(hN c hc).2.2.1]; exact J.sch.curScOfW c hc + cur := J.sch.cur + csc := J.sch.csc + idle := by + unfold LawfulStore.idleW; rw [kc, (hN _ hcpu).2.1]; exact J.sch.idle + idleOf := fun c hc => by rw [(hN c hc).2.1]; exact J.sch.idleOf c hc + idleScOf := fun c hc => by rw [(hN c hc).2.2.2]; exact J.sch.idleScOf c hc + act := by unfold LawfulStore.actionW; rw [hcur _ (Or.inl rfl)]; exact J.sch.act + time := by + unfold LawfulStore.curTime LawfulStore.nodeA; rw [hcur _ (Or.inr (Or.inl rfl))]; exact J.sch.time + aff := fun y hy => by rw [(hTW y hy).2.1]; exact J.sch.aff y hy + affT := J.sch.affT + tcb := fun y hy => by + obtain ⟨x', e, h1, h2⟩ := J.sch.tcb y hy + obtain ⟨x'', e', f1, -, -, -, -, f6⟩ := hTk y hy x' e + refine ⟨x'', e', ?_, by rw [(hTW y hy).1, f1]; exact h2⟩ + by_cases ey : y = t + · subst ey; rw [hk] at e; cases e; rw [hkt] at e'; cases e'; rw [hst2]; exact hv + · rw [hSt y hy ey, f6 ey]; exact h1 + sc := fun c hc => by + rw [hsc c hc, hOm _ (Or.inr (Or.inr (Or.inr ⟨c, hc, Or.inl rfl⟩))) (.sc _ hc)]; exact J.sch.sc c hc + scOff := J.sch.scOff } + have irq' : IrqRelA (L.mem s2) (L.ksb s2) σ1 := + ⟨fun i hi => by + rw [kb, hOm _ (Or.inr (Or.inr (Or.inl ⟨i, hi, rfl⟩))) (.irq _ hi)]; exact J.irq.st i hi, + fun i hi => by rw [kb]; exact J.irq.node i hi, + fun i hi => by rw [kb]; exact J.irq.nodeHi i hi⟩ + have rest' : SchedRestA (L := L) Z s2 σ1 m := + { rq := by rw [kc]; exact J.rest.rq + rqDom := J.rest.rqDom + rel := by rw [kc]; exact J.rest.rel + consumed := by rw [hcur _ (Or.inr (Or.inr (Or.inl rfl)))]; exact J.rest.consumed + repr := by rw [hcur _ (Or.inr (Or.inr (Or.inr rfl)))]; exact J.rest.repr + curDom := by + rw [kb, hOm _ (Or.inl rfl) (.glob _ (Or.inl rfl))]; exact J.rest.curDom + domTime := by + rw [kb, hOm _ (Or.inr (Or.inl rfl)) (.glob _ (Or.inr rfl))]; exact J.rest.domTime + prio := fun y hy => by + obtain ⟨x', e, h1, h2, h3⟩ := J.rest.prio y hy + obtain ⟨x'', e', -, f3, f4, -⟩ := hTk y hy x' e + exact ⟨x'', e', by rw [f3]; exact h1, by rw [(hTW y hy).2.2.2.2.2]; exact h2, + by rw [(hTW y hy).2.2.2.2.1, f4]; exact h3⟩ + scMem := fun y hy => by rw [(hTW y hy).1]; exact J.rest.scMem y hy + refillMax := fun c hc => by + rw [hsc c hc, hOm _ (Or.inr (Or.inr (Or.inr ⟨c, hc, Or.inr rfl⟩))) (.sc2 _ hc)] + exact J.rest.refillMax c hc } + have J' : JAtZA (L := L) AR Z s2 a σ1 m := + { st := st' + cdt := cdt' + sch := sch' + ar := J.ar + place := by rw [kb]; exact J.place + irq := irq' + rest := rest' + thT := J.thT + csN := J.csN + idleT := fun c hc => by rw [(hN c hc).2.1]; exact J.idleT c hc + idleC := fun c hc => by rw [(hN c hc).2.2.2]; exact J.idleC c hc + curT := fun c hc => by rw [(hN c hc).1]; exact J.curT c hc + cscC := fun c hc => by rw [(hN c hc).2.2.1]; exact J.cscC c hc + ksFit := by rw [kb]; exact J.ksFit + scOffD := J.scOffD + cv := fun y hy => by rw [hcap y (Or.inl hy)]; exact J.cv y hy + pml := fun y hy => by rw [hcap y (Or.inl hy)]; exact J.pml y hy + vs := by + rw [kb] + refine (J.vs.of_mem fun b hb => hm b (.nslot _ (.vs b hb)) (O.vs b hb)).of_spec rfl fun p hp => ?_ + by_cases ep : p = specPtr t + · subst ep; exact absurd hk (J.vs.not_tcb hp) + · simp [σ1, setObjA, Function.update_of_ne ep] + ext := by + refine (J.ext.of_mem fun b hb => hm b (.nslot _ (.ext b hb)) (O.ext b hb)).of_spec rfl fun q hq => ?_ + by_cases ep : specPtr q = specPtr t + · rcases hq with hq | hq + · exact absurd (by rw [ep]; exact hk) (J.vs.not_tcb (Or.inl ⟨q, hq, rfl⟩)) + · exact absurd (by rw [ep]; exact hk) (J.ext.not_tcb hq x) + · simp [σ1, setObjA, Function.update_of_ne ep] } + have hVw : ∀ w ∈ Z.V, L.mem s2 w = L.mem s w ∧ L.mem s2 (wa w 24#u64) = L.mem s (wa w 24#u64) := fun w hw => + ⟨hm _ (.nslot _ (.vcpu _ _ hw (Or.inl rfl))) (O.vc w hw).1, + hm _ (.nslot _ (.vcpu _ _ hw (Or.inr rfl))) (O.vc w hw).2⟩ + refine ⟨J', ⟨fun w hw => ?_, fun p y e => V.back p y (stUpd_back e (fun _ h => by cases h)), fun y hy => ?_, + fun y hy p e => V.cap y (slotW_isSlot J.place (Or.inl hy)) p (by rw [← hcap y (Or.inl hy)]; exact e)⟩, + ⟨fun r hr => ?_, fun p es e => X.back p es (stUpd_back e (fun _ h => by cases h)), + fun y hy p l mp asid va e => X.cap y hy p l mp asid va (by rw [← hcap y (Or.inl hy)]; exact e)⟩, + ⟨fun r hr => ?_, fun p l1 e => Y.back p l1 (stUpd_back e (fun _ h => by cases h)), + fun y hy p mp asid e => Y.cap y hy p mp asid (by rw [← hcap y (Or.inl hy)]; exact e)⟩⟩ + · obtain ⟨x', e, h2⟩ := V.obj w hw + rw [(hVw w hw).1, (hVw w hw).2] + exact ⟨x', stUpd_keep hk e (fun _ h => by cases h), h2⟩ + · obtain ⟨x', e, h1, h2⟩ := V.tcb y hy + obtain ⟨x'', e', -, -, -, f5, f6, -⟩ := hTk y hy x' e + rw [(hTW y hy).2.2.1, (hTW y hy).2.2.2.1] + exact ⟨x'', e', by rw [f5]; exact h1, by rw [f6]; exact h2⟩ + · obtain ⟨es, e⟩ := X.obj r hr + exact ⟨es, stUpd_keep hk e (fun _ h => by cases h)⟩ + · obtain ⟨l1, e⟩ := Y.obj r hr + exact ⟨l1, stUpd_keep hk e (fun _ h => by cases h)⟩ + +end rel + +/-! ## `SInvXA` across the restart -/ + +/-- `SpecIrqA` across a TCB replaced by a TCB with the same caps. -/ +theorem SpecIrqA.updTcbA {σ : State Unit} {t : ObjRef} {x x' : Tcb} (h : SpecIrqA σ) + (hk : σ.kheap t = some (.tcb x)) (hc : tcbCnodeMap x' = tcbCnodeMap x) : + SpecIrqA (setObjA σ t (.tcb x')) := by + have hct : ∀ n p, capTableAt n p (setObjA σ t (.tcb x')) ↔ capTableAt n p σ := fun n p => by + unfold capTableAt + by_cases e : p = t + · subst e; simp [setObjA, hk] + · simp [setObjA, Function.update_of_ne e] + obtain ⟨⟨⟨hinj, hcn⟩, hdom⟩, hsl, href⟩ := h + refine ⟨⟨⟨hinj, fun irq hi => (hct _ _).2 (hcn irq hi)⟩, fun irq hi n hn => hdom irq hi n ((hct _ _).1 hn)⟩, + fun irq hi o b c e hc' => ?_, fun p o b c e hc' => ?_⟩ + · by_cases ep : σ.interruptIrqNode irq = t + · obtain ⟨cs, hcs, -⟩ := hcn irq hi + rw [ep, hk] at hcs; cases hcs + · exact hsl irq hi o b c (by simpa [setObjA, Function.update_of_ne ep] using e) hc' + · by_cases ep : p = t + · subst ep + simp [setObjA] at e; subst e + exact href p (.tcb x) b c hk (by simpa [capOf, hc] using hc') + · exact href p o b c (by simpa [setObjA, Function.update_of_ne ep] using e) hc' + +/-- **`SInvXA` across the restart's spec update** (module docstring): the thread is not the idle thread, the new state +is runnable and not `IdleThreadState`. -/ +theorem SInvXA.restartA {σ : State Unit} {m : McState} {n : Nat} (h : SInvXA σ m n) {t : ObjRef} {x : Tcb} + (hk : σ.kheap t = some (.tcb x)) (hti : t ≠ σ.idleThread) {ts : ThreadState} (hr : runnable ts = true) + (hni : ts ≠ .idleThreadState) (hfn : x.tcbFault = none) : SInvXA (stUpdA σ t x ts) m n := by + have hkt : (stUpdA σ t x ts).kheap t = some (.tcb { x with tcbState := ts }) := by simp [setObjA] + have hkn : ∀ p, p ≠ t → (stUpdA σ t x ts).kheap p = σ.kheap p := fun p hp => by + simp [setObjA, Function.update_of_ne hp] + have tcbK : ∀ p y, σ.kheap p = some (.tcb y) → ∃ y', (stUpdA σ t x ts).kheap p = some (.tcb y') ∧ + y'.tcbSchedContext = y.tcbSchedContext ∧ (p ≠ t → y' = y) ∧ (p = t → y'.tcbState = ts) := fun p y e => by + by_cases ep : p = t + · subst ep; rw [hk] at e; cases e; exact ⟨_, hkt, rfl, fun h => absurd rfl h, fun _ => rfl⟩ + · exact ⟨y, by rw [hkn p ep]; exact e, rfl, fun _ => rfl, fun h => absurd h ep⟩ + have scK : ∀ p y k, (stUpdA σ t x ts).kheap p = some (.schedContext y k) ↔ σ.kheap p = some (.schedContext y k) := + fun p y k => ⟨fun e => stUpd_back e (fun _ h => by cases h), fun e => stUpd_keep hk e (fun _ h => by cases h)⟩ + have CF := CurFactsA.back (σ := σ) (σ' := stUpdA σ t x ts) ⟨h.zc, h.rlsRun, h.curScReady⟩ (fun _ hq => hq) rfl rfl rfl + (fun p x' e => by + by_cases ep : p = t + · subst ep; rw [hkt] at e; cases e; exact ⟨x, hk, rfl⟩ + · rw [hkn p ep] at e; exact ⟨x', e, rfl⟩) + (fun p y e hm => by + obtain ⟨y', e', -, f, g⟩ := tcbK p y e + refine ⟨y', e', fun hy => ?_⟩ + by_cases ep : p = t + · rw [g ep]; exact hr + · rw [f ep]; exact hy) + (fun y k e => ⟨y, (scK _ y k).1 (by exact e), fun a => ⟨a, id⟩⟩) + refine + { mc := h.mc + view := h.view + curTcb := ?_ + idleTcb := ?_ + onlyIdle := fun p y e e2 => ?_ + idleSc := ?_ + curSc := by obtain ⟨y, k, e⟩ := h.curSc; exact ⟨y, k, (scK _ y k).2 e⟩ + ctQ := h.ctQ + idleQ := h.idleQ + rq := fun d p y hy => ?_ + rls := fun y hy => ?_ + rpos := fun p y k e a => h.rpos p y k ((scK p y k).1 e) a + zc := CF.1 + rlsRun := CF.2.1 + curScReady := CF.2.2 + faultSt := fun p y e f => by + by_cases ep : p = t + · subst ep; rw [hkt] at e; cases e; exact absurd hfn f + · rw [hkn p ep] at e; exact h.faultSt p y e f + irqN := h.irqN.updTcbA hk rfl } + · obtain ⟨y, e⟩ := h.curTcb + obtain ⟨y', e', -⟩ := tcbK _ y e + exact ⟨y', e'⟩ + · obtain ⟨y, e, e2⟩ := h.idleTcb + obtain ⟨y', e', -, f, -⟩ := tcbK _ y e + exact ⟨y', e', by rw [f hti.symm]; exact e2⟩ + · by_cases ep : p = t + · subst ep; rw [hkt] at e; cases e; exact absurd e2 hni + · rw [hkn p ep] at e; exact h.onlyIdle p y e e2 + · obtain ⟨y, z, k, e1, e2, e3, e4⟩ := h.idleSc + obtain ⟨y', e1', f1, -⟩ := tcbK _ y e1 + exact ⟨y', z, k, e1', by rw [f1]; exact e2, (scK _ z k).2 e3, e4⟩ + · obtain ⟨z, e, e2⟩ := h.rq d p y hy + obtain ⟨y', e', -, f, g⟩ := tcbK y z e + by_cases ep : y = t + · exact ⟨y', e', by rw [g ep]; exact hr⟩ + · exact ⟨y', e', by rw [f ep]; exact e2⟩ + · obtain ⟨z, e⟩ := h.rls y hy + obtain ⟨y', e', -⟩ := tcbK y z e + exact ⟨y', e'⟩ + +/-! ## The whole restart -/ + +section top +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {s : S} {a : A} {σ : State Unit} {m : McState} + +/-- (record edit #8b-D (b)) A TCB whose state word is runnable has no fault (`SInvXA.faultSt`). -/ +theorem faultNone_of_runA {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} {n : Nat} (hI : SInvXA σ m n) + {p : Fl4SpecA64.ObjRef} {x : Fl4SpecA64.Tcb} {v : Nat} (hk : σ.kheap p = some (.tcb x)) + (hst : v = tsCodeA x.tcbState) (hv : v = 1 ∨ v = 2 ∨ v = 8) : x.tcbFault = Option.none := by + refine FaultStOkA.none hI.faultSt hk fun hF => ?_ + subst hst + rcases hF with ⟨_, _, e⟩ | ⟨_, e⟩ | e <;> rw [e] at hv <;> simp only [tsCodeA] at hv <;> omega + +/-- **The caller's restart keeps `JEntA`, over the two write facts** (`restartA`'s body): FL4's restart is the +`STATE` write and the spec's has the single outcome `stUpdA`, however they are known. -/ +theorem JEntA.restartCoreA (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + {Rp Nt : Set U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} {slot : U64} + (P : SysPlace st H Z.Cs H.frame Rp Nt OS s extra slot) (Y : SymL st H Z.Cs s) + (hrun : Runnable (L.mem s (stA (L.curW s)))) + (hvs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → Aligned b ∧ b ≠ stA (L.curW s)) + (hvc : ∀ v ∈ Z.V, stA (L.curW s) ≠ v ∧ stA (L.curW s) ≠ wa v 24#u64) + (heq : sched.set_thread_state st s (L.curW s) sched.TS_RESTART = ok (L.wr s (stA (L.curW s)) sched.TS_RESTART)) + (hsetx : ∀ x, σ.kheap (specPtr (L.curW s)) = some (.tcb x) → + setThreadState (Z := Unit) (specPtr (L.curW s)) .restart σ = + ({((), stUpdA σ (specPtr (L.curW s)) x .restart)}, False)) : + ∃ x, σ.kheap σ.curThread = some (.tcb x) ∧ + sched.set_thread_state st s (L.curW s) sched.TS_RESTART = ok (L.wr s (stA (L.curW s)) sched.TS_RESTART) ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie (L.wr s (stA (L.curW s)) sched.TS_RESTART) a + (stUpdA σ σ.curThread x .restart) m ∧ + SymL st H Z.Cs (L.wr s (stA (L.curW s)) sched.TS_RESTART) ∧ + OSKeep st OS s (L.wr s (stA (L.curW s)) sched.TS_RESTART) ∧ + (∀ y, SlotWA Z.th y → capAtA (L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART)) y = capAtA (L.mem s) y) ∧ + (∀ n, SInvXA σ m n → SInvXA (stUpdA σ σ.curThread x .restart) m n) := by + have q := E.sq + obtain ⟨-, hB, hX, hSg, hS⟩ := E.sys.ent.bxi + set t := L.curW s with ht_def + have hct : t ∈ H.tcbs := q.curT + have hta : TcbAddr t := q.tcbs t hct + have htZ : t ∈ Z.T := by rw [← E.tcbs]; exact hct + have hcT : σ.curThread = (specPtr t) := E.j.1.sch.curThread + obtain ⟨x, hk, hst, hscx⟩ := E.j.1.sch.tcb t htZ + have O := E.stOffA hvc + have sA : Aligned (stA t) := by rw [stA_eq]; exact field_aligned hta (by simp) (by simp) + have hset := hsetx x hk + -- FL4 invariants + have hnb := runnable_ok hrun + have hall := sts_all (Sg := H.frame) s t sched.TS_RESTART Z.Q Z.R q hB hX hS hSg hct not_blocked_restart + restart_ne (Or.inr hnb.1) (hX.noref hta hnb.2 (fun e => hnb.1 (Or.inr (Or.inl e)))) + ⟨fun _ _ _ _ _ => runnable_restart, fun _ _ _ => runnable_restart, fun _ => runnable_restart⟩ + rw [heq, WP.spec_ok] at hall + obtain ⟨q2, hB2, hX2, hS2, hSg2, -, -⟩ := hall + have q2' := q2.congrX empty_congr + have hdf := sched.set_thread_state.dfR s t (BXI.blk E.sys.ent.bxi) hct hrun + rw [heq, WP.spec_ok] at hdf + obtain ⟨-, f1, l1⟩ := hdf + have d : DF st H Z.Cs t ∅ s (L.wr s (stA t) sched.TS_RESTART) := DF.ofKL f1 l1 (tc_le _ _) + have hKo : ∀ o : U64, o.val ∈ (∅ : Set Nat) → o.val < 224 → ∀ y ∈ OS, + ¬ (y.val ≤ (wa (L.ksb s) o).val ∧ (wa (L.ksb s) o).val < y.val + 32) := + fun _ h => absurd h (Set.notMem_empty _) + obtain ⟨B2, Y2⟩ := SysL.df P E.sys.ent.bxi hrun hKo E.sys.sysl Y d + have kb : L.ksb (L.wr s (stA t) sched.TS_RESTART) = L.ksb s := L.ksb_wr _ _ _ + have hal := FootA.aligned E.j.1 (bxi_nodes E.sys.ent.bxi) E.scAl E.ap.vcAl + have hJ := JAtZAX.of_stA E.j O htZ hk (ts := .restart) (v := sched.TS_RESTART) (by simp [sched.TS_RESTART, tsCodeA]) + hal sA + have hcap : ∀ y, SlotWA Z.th y → capAtA (L.mem (L.wr s (stA t) sched.TS_RESTART)) y = capAtA (L.mem s) y := by + intro y hy + have e0 : wa y 0#u64 = y := by + apply UScalar.eq_of_val_eq; rw [wa_valm]; simp + have n0 : stA t ≠ y := fun e => O.slot y hy 0#u64 (by simp) (by rw [e0]; exact e.symm) + have n8 : stA t ≠ wa y 8#u64 := fun e => O.slot y hy 8#u64 (by simp) e.symm + unfold capAtA + rw [mem_wr_ne' sA (hal _ (.slot _ _ hy (Or.inl rfl))) n0, + mem_wr_ne' sA (hal _ (.slot _ _ hy (Or.inr (Or.inl rfl)))) n8] + have hOS := DF.osk P E.sys.ent.bxi hKo d + have hTable (i : U64) : + L.mem (L.wr s (stA t) sched.TS_RESTART) (wa (L.ksb s) (AtOff i)) = L.mem s (wa (L.ksb s) (AtOff i)) := by + have h := hvs _ (.inr (asW_at _ i)) + exact mem_wr_ne' sA h.1 (Ne.symm h.2) + rw [hcT] + refine ⟨x, hk, heq, hset, ?_, Y2, hOS, hcap, fun k hI => ?_⟩ + · refine ⟨⟨⟨E.sys.ent.heap, ⟨⟨Z.Q, Z.R, q2'⟩, hB2, hX2, hSg2, hS2⟩, E.sys.ent.os, + E.sys.ent.user.ksb kb, ?_, by rw [kb]; exact E.sys.ent.ksw⟩, B2⟩, hJ, E.tcbs, E.scs, q2', ?_, + E.pp, E.s1, (by rw [kb]; exact E.ko), E.ptH, E.tabInj, poolOff_keepTableA (Z := Z) E.poolOff kb hTable, + E.objAlloc.keep hOS, E.idleAnchor.keep hOS, by rw [kb]; exact E.vsVc⟩ + · have k : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → + L.mem (L.wr s (stA t) sched.TS_RESTART) b = L.mem s b := fun b hb => by + have h := hvs b hb + exact mem_wr_ne' sA h.1 (fun e => h.2 e.symm) + exact (utVsPt_keepA E.sys.ent.vs kb k).1 + · rw [kb]; exact E.ap + · refine hI.restartA hk ?_ rfl (by intro h; cases h) (faultNone_of_runA hI hk hst (by + rcases hrun with e | e | e <;> rw [e] + · exact Or.inl Fl4ProofsA64.P0.Consts.sched.TS_RUNNING.val_eq + · exact Or.inr (Or.inl Fl4ProofsA64.P0.Consts.sched.TS_RESTART.val_eq) + · exact Or.inr (Or.inr Fl4ProofsA64.P0.Consts.sched.TS_RUNNING_VM.val_eq))) + intro e + obtain ⟨y, ey, ey2⟩ := hI.idleTcb + rw [← e, hk] at ey + cases ey + rw [ey2] at hst + rcases hrun with e1 | e1 | e1 <;> rw [e1] at hst <;> + simp [sched.TS_RUNNING, sched.TS_RESTART, sched.TS_RUNNING_VM, tsCodeA] at hst + +/-- **The caller's restart keeps `JEntA`** (module docstring; x86 `restart_rel`): FL4's restart is the caller's +`STATE` write, the spec's `set_thread_state Restart` has the single outcome `stUpdA`, and `JEntA` holds between them, +with `SymL`, every `OS` slot and every slot of the relation kept, and `SInvXA` carried. -/ +theorem JEntA.restartA (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + {Rp Nt : Set U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} {slot : U64} + (P : SysPlace st H Z.Cs H.frame Rp Nt OS s extra slot) (Y : SymL st H Z.Cs s) + (hrun : Runnable (L.mem s (stA (L.curW s)))) + (hcur : (L.mem s (inqA (L.curW s)) &&& sched.Q_RELEASE) = 0#u64 ∧ L.mem s (scA (L.curW s)) ≠ 0#u64 ∧ + L.mem s (scA (L.curW s)) ∈ Ac) + (hvs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → Aligned b ∧ b ≠ stA (L.curW s)) + (hvc : ∀ v ∈ Z.V, stA (L.curW s) ≠ v ∧ stA (L.curW s) ≠ wa v 24#u64) : + ∃ x, σ.kheap σ.curThread = some (.tcb x) ∧ + sched.set_thread_state st s (L.curW s) sched.TS_RESTART = ok (L.wr s (stA (L.curW s)) sched.TS_RESTART) ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie (L.wr s (stA (L.curW s)) sched.TS_RESTART) a + (stUpdA σ σ.curThread x .restart) m ∧ + SymL st H Z.Cs (L.wr s (stA (L.curW s)) sched.TS_RESTART) ∧ + OSKeep st OS s (L.wr s (stA (L.curW s)) sched.TS_RESTART) ∧ + (∀ y, SlotWA Z.th y → capAtA (L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART)) y = capAtA (L.mem s) y) ∧ + (∀ n, SInvXA σ m n → SInvXA (stUpdA σ σ.curThread x .restart) m n) := by + have q := E.sq + obtain ⟨-, hB, hX, hSg, hS⟩ := E.sys.ent.bxi + set t := L.curW s with ht_def + have hct : t ∈ H.tcbs := q.curT + have hta : TcbAddr t := q.tcbs t hct + have htZ : t ∈ Z.T := by rw [← E.tcbs]; exact hct + have O := E.stOffA hvc + have sA : Aligned (stA t) := by rw [stA_eq]; exact field_aligned hta (by simp) (by simp) + -- the SC + set w := L.mem s (scA t) + have hw0 : w ≠ 0#u64 := hcur.2.1 + have hwC : w ∈ Z.C := E.j.1.rest.scMem t htZ hw0 + have hwH : w ∈ H.scs := by rw [E.scs]; exact hwC + have hrm : L.rmax s w ≠ 0#u64 := (hS.act w hwH).1 hcur.2.2 + have hmΩ : maxA w ∈ H.omega := hS.sub _ (hS.all _ hwH).2.1 + have hma : Aligned (maxA w) := (q.omega _ hmΩ).1 + have hmx : stA t ≠ maxA w := fun e => O.om _ (Or.inr (Or.inr (Or.inr ⟨w, hwC, Or.inr (maxA_eqA w)⟩))) e.symm + have heq := restart_wrA (L := L) hta hw0 hrm hcur.1 hma hmx + -- the spec + obtain ⟨x, hk, -, hscx⟩ := E.j.1.sch.tcb t htZ + have hq : (specPtr t) ∉ σ.releaseQueue := by + rw [E.j.1.rest.rel] + intro hm + obtain ⟨r, hr, er⟩ := List.mem_map.1 hm + have e : r = t := specPtr_inj er + subst e + have hcpu : L.cpu s ∈ Z.Cs := q.cur + have haf := q.inR _ hcpu _ hr + exact ((q.released _ hct).2 (by rw [haf]; exact hr)) hcur.1 + obtain ⟨y, n, ey, eyr⟩ := E.j.1.rest.refillMax w hwC + have hsch : schedulable (specPtr t) (stUpdA σ (specPtr t) x .restart) = true := by + refine schedulable_ofA (x := { x with tcbState := .restart }) (by simp [setObjA]) rfl + (sc := scRenA m w) + (by show x.tcbSchedContext = _; rw [hscx]; unfold optScA; simp [show L.mem s (scA t) = w from rfl, hw0]) + (stUpd_keep hk ey (fun _ h => by cases h)) ?_ hq + rw [eyr] + have e2 : L.rmax s w = L.mem s (wa w 72#u64) := by unfold LawfulStore.rmax; rw [maxA_eqA] + rw [e2] at hrm + exact Nat.pos_of_ne_zero (fun e => hrm (UScalar.eq_of_val_eq (by simpa using e))) + refine JEntA.restartCoreA E P Y hrun hvs hvc heq (fun x' hk' => ?_) + rw [hk] at hk' + obtain rfl := KernelObject.tcb.inj (Option.some.inj hk') + exact setThreadState_eqA σ (specPtr t) x hk .restart (fun _ _ => hsch) + +end top + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/SInvXA.lean b/proofs/Fl4ProofsA64/P3/SInvXA.lean new file mode 100644 index 000000000..9335229da --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/SInvXA.lean @@ -0,0 +1,172 @@ +/- +P3 (aarch64, A64-P3 S3 gen 1): **`SInvXA`, the core spec-side invariant of the A64 refinement** — the shape of +x86's `SInvX σ := SInvW σ ∧ RPos σ` (P3/XTopJointKEntry35), with only the core clauses this gen, each l4v's +(`Invariants_AI` / `DetSchedInvs_AI`, MCS) or its multi-core form, over the view `σ` and `m : McState` of `n` cores. + +| field | l4v / x86 | statement | +|---|---|---| +| `mc` | `Ext/MultiCore` `McState.WF` | `m` is a multi-core state of `n` cores | +| `view` | `Ext/MultiCore` `McView` | `σ.idle_thread` is core `m.curCore`'s idle thread | +| `curTcb` | `cur_tcb` | the current thread is a TCB | +| `idleTcb` | `valid_idle` (state part) | the idle thread is a TCB in `IdleThreadState` | +| `onlyIdle` | **`only_idle`, multi-core** (x86 `OnlyIdle`) | a TCB in `IdleThreadState` is **some** core's idle thread | +| `idleSc` | `valid_idle` (SC part) | the idle thread's SC is `idle_sc_ptr`, an SC bound to it | +| `curSc` | `cur_sc_tcb` (existence) | the current SC is an SC | +| `ctQ` | `ct_not_in_q` (x86 `SInvX.ctQ`) | the current thread is in no ready queue | +| `idleQ` | `valid_idle` / `idle_thread_not_queued`, multi-core | no core's idle thread is in a ready queue | +| `rq` | `valid_ready_qs` (core) | ready-queue members are runnable TCBs | +| `rls` | `valid_release_q` (core) | release-queue members are TCBs | +| `rpos` | `valid_refills` (x86 `RPos`'s non-empty part; Stage-3: active only, `ScInvA.ne`) | every active SC has a refill | +| `zc`, `rlsRun`, `curScReady` | (record edit #8b-C2, item 11) l4v `cur_sc_in_release_q_imp_zero_consumed`, `valid_release_q` (runnable) | `ZeroConsA`, `RlsRunA`, `CurRdyA` below | +| `irqN` | (gen 4) `Ext/IrqNode` `IrqNodeFl4` + x86 `SpecGlob` / `SpecIrqNode` | `SpecIrqA` below | + +**Why `onlyIdle` is multi-core.** x86's `OnlyIdle` (`t` in `IdleThreadState` ⇒ `t = σ.idleThread`) is false at +FL4's boot layout with two cores (jboot gen 6, `idle2_not_sinvX`: both idle TCBs are typed, each view sees the other +idle TCB). `onlyIdle` here asks only that such a TCB be one of the `n` cores' idle threads (`m.idleThreadOf c`, +`c < n`), which holds at every core count. + +**`SpecIrqA σ`** (gen 4, ruling 10:33Z item 5; x86 `SpecGlob`'s `irq` / `ref` with IntInv's `SpecIrqNode`): the +spec's IRQ node in FL4's configuration, pure spec: `node` (`Fl4SpecA64.IrqNodeFl4`: a one-slot CNode for each of FL4's +5376 IRQs, injective there, and no CNode for the IRQs above), `slot` (the IRQ-node CNodes' slots hold null or +notification caps, x86 `SpecIrqNode`), `ref` (no cap of any object is a CNode cap naming an IRQ-node CNode of FL4's +IRQs, x86 `SpecGlob.ref` restricted to CNode caps). Preservation: `P3/SpecFrA` (`NdIrq`). + +No named premise. New definitions only; preservation is S4+. +-/ +import Fl4SpecA64 + +namespace Fl4ProofsA64.P3 + +noncomputable section + +/-- (gen 4) A cap an IRQ-node slot may hold: null or a notification cap (x86 `SpecIrqNode`). -/ +def IrqCapA (a : Fl4SpecA64.Cap) : Prop := a = .nullCap ∨ ∃ r b rs, a = .notificationCap r b rs + +/-- (gen 4) A CNode cap naming the IRQ-node CNode of one of FL4's IRQs, at the IRQ node `nd` (x86 `NodeFree`'s +negation, CNode caps only). -/ +def NodeCapA (nd : Fl4SpecA64.Irq → Fl4SpecA64.ObjRef) (a : Fl4SpecA64.Cap) : Prop := + ∃ irq : Fl4SpecA64.Irq, irq.toNat < Fl4SpecA64.fl4IrqCount ∧ ∃ n g, a = .cnodeCap (nd irq) n g + +/-- (gen 4) **The spec's IRQ node in FL4's configuration** (module docstring). -/ +structure SpecIrqA (σ : Fl4SpecA64.State Unit) : Prop where + node : Fl4SpecA64.IrqNodeFl4 σ + slot : ∀ irq : Fl4SpecA64.Irq, irq.toNat < Fl4SpecA64.fl4IrqCount → ∀ o b a, + σ.kheap (σ.interruptIrqNode irq) = some o → Fl4SpecA64.capOf o b = some a → IrqCapA a + ref : ∀ p o b a, σ.kheap p = some o → Fl4SpecA64.capOf o b = some a → ¬ NodeCapA σ.interruptIrqNode a + +/-- (record edit #8b-C2, item 11) l4v `cur_sc_in_release_q_imp_zero_consumed`: a thread bound to the current SC that is in +the release queue means no time is consumed yet. -/ +def ZeroConsA (σ : Fl4SpecA64.State Unit) : Prop := + ∀ t x, σ.kheap t = some (.tcb x) → x.tcbSchedContext = some σ.curSc → t ∈ σ.releaseQueue → σ.consumedTime = 0 + +/-- (item 11) Every release-queue member is a runnable TCB (l4v `valid_release_q`'s runnable part). -/ +def RlsRunA (σ : Fl4SpecA64.State Unit) : Prop := + ∀ t ∈ σ.releaseQueue, ∃ x, σ.kheap t = some (.tcb x) ∧ Fl4SpecA64.runnable x.tcbState = true + +/-- (item 11, Architect ruling (C)) With time consumed, the current SC's head refill is ready (`refillReady` only: +`mergeRefill` keeps the head's `rTime`, so the finaliser's refill checks keep it). -/ +def CurRdyA (σ : Fl4SpecA64.State Unit) : Prop := + σ.consumedTime ≠ 0 → ∀ y k, σ.kheap σ.curSc = some (.schedContext y k) → Fl4SpecA64.scActive y = true → + Fl4SpecA64.refillReady σ.curTime (Fl4SpecA64.refillHd y) = true + +theorem refillReady_monoA {t t' : Fl4SpecA64.Time} {r : Fl4SpecA64.Refill} + (h : Fl4SpecA64.refillReady t r = true) (hm : t ≤ t') : Fl4SpecA64.refillReady t' r = true := by + simp only [Fl4SpecA64.refillReady, Fl4SpecA64.refillReady', decide_eq_true_eq] at h ⊢ + exact BitVec.le_trans h hm + +/-- `ZeroConsA` across a step that keeps the consumed time and the current SC, only shrinks the release queue, and +whose every TCB was a TCB with the same SC binding. -/ +theorem ZeroConsA.back {σ σ' : Fl4SpecA64.State Unit} (h : ZeroConsA σ) + (hq : ∀ t, t ∈ σ'.releaseQueue → t ∈ σ.releaseQueue) (hc : σ'.consumedTime = σ.consumedTime) + (hs : σ'.curSc = σ.curSc) + (hb : ∀ t x', σ'.kheap t = some (.tcb x') → ∃ x, σ.kheap t = some (.tcb x) ∧ + x.tcbSchedContext = x'.tcbSchedContext) : ZeroConsA σ' := fun t x' hx e hr => by + obtain ⟨x, hx0, es⟩ := hb t x' hx + rw [hc] + exact h t x hx0 (by rw [es, e, hs]) (hq t hr) + +/-- `RlsRunA` across a step that only shrinks the release queue and keeps every queued runnable TCB runnable. -/ +theorem RlsRunA.back {σ σ' : Fl4SpecA64.State Unit} (h : RlsRunA σ) + (hq : ∀ t, t ∈ σ'.releaseQueue → t ∈ σ.releaseQueue) + (hf : ∀ t x, σ.kheap t = some (.tcb x) → t ∈ σ'.releaseQueue → ∃ x', σ'.kheap t = some (.tcb x') ∧ + (Fl4SpecA64.runnable x.tcbState = true → Fl4SpecA64.runnable x'.tcbState = true)) : RlsRunA σ' := fun t ht' => by + obtain ⟨x, hx, hr⟩ := h t (hq t ht') + obtain ⟨x', hx', f⟩ := hf t x hx ht' + exact ⟨x', hx', f hr⟩ + +/-- **The three item-11 facts across a step** that keeps the consumed time, the current SC and the time, only +shrinks the release queue, and keeps every TCB's SC binding, every queued runnable TCB runnable, and the current +SC's activity / readiness. -/ +theorem CurFactsA.back {σ σ' : Fl4SpecA64.State Unit} (h : ZeroConsA σ ∧ RlsRunA σ ∧ CurRdyA σ) + (hq : ∀ t, t ∈ σ'.releaseQueue → t ∈ σ.releaseQueue) (hc : σ'.consumedTime = σ.consumedTime) + (hs : σ'.curSc = σ.curSc) (ht : σ'.curTime = σ.curTime) + (hb : ∀ t x', σ'.kheap t = some (.tcb x') → ∃ x, σ.kheap t = some (.tcb x) ∧ + x.tcbSchedContext = x'.tcbSchedContext) + (hf : ∀ t x, σ.kheap t = some (.tcb x) → t ∈ σ'.releaseQueue → ∃ x', σ'.kheap t = some (.tcb x') ∧ + (Fl4SpecA64.runnable x.tcbState = true → Fl4SpecA64.runnable x'.tcbState = true)) + (hsc : ∀ y k, σ'.kheap σ.curSc = some (.schedContext y k) → ∃ y0, σ.kheap σ.curSc = some (.schedContext y0 k) ∧ + (Fl4SpecA64.scActive y = true → Fl4SpecA64.scActive y0 = true ∧ + (Fl4SpecA64.refillReady σ.curTime (Fl4SpecA64.refillHd y0) = true → + Fl4SpecA64.refillReady σ.curTime (Fl4SpecA64.refillHd y) = true))) : + ZeroConsA σ' ∧ RlsRunA σ' ∧ CurRdyA σ' := + ⟨h.1.back hq hc hs hb, h.2.1.back hq hf, fun hne y k hy a => by + rw [hc] at hne + rw [hs] at hy + obtain ⟨y0, hy0, fx⟩ := hsc y k hy + obtain ⟨fa, fr⟩ := fx a + rw [ht] + exact fr (h.2.2 hne y0 k hy0 fa)⟩ + +/-- (record edit #8b-D (b), ruling 13:24Z) **The thread states a faulted thread can be in**: blocked on its fault +send, blocked on the reply to it, or inactive (`set_fault` goes straight to `send_ipc`; `cancel_ipc` and +`do_reply_transfer` clear the fault before a thread runs again; no fault flag on `BlockedOnSend`). -/ +def FaultStA (ts : Fl4SpecA64.ThreadState) : Prop := + (∃ e d, ts = .blockedOnSend e d) ∨ (∃ r, ts = .blockedOnReply r) ∨ ts = .inactive + +/-- (record edit #8b-D (b)) Every faulted TCB is in a `FaultStA` state. -/ +def FaultStOkA (σ : Fl4SpecA64.State Unit) : Prop := + ∀ t x, σ.kheap t = some (.tcb x) → x.tcbFault ≠ none → FaultStA x.tcbState + +theorem FaultStA.notRun {ts : Fl4SpecA64.ThreadState} (h : FaultStA ts) : Fl4SpecA64.runnable ts = false := by + rcases h with ⟨_, _, rfl⟩ | ⟨_, rfl⟩ | rfl <;> rfl + +/-- **`faultNotRun`**: a faulted TCB is not runnable. -/ +theorem FaultStOkA.faultNotRun {σ : Fl4SpecA64.State Unit} (h : FaultStOkA σ) : + ∀ t x, σ.kheap t = some (.tcb x) → x.tcbFault ≠ none → Fl4SpecA64.runnable x.tcbState = false := + fun t x hx hf => (h t x hx hf).notRun + +/-- A TCB in a state outside `FaultStA` has no fault. -/ +theorem FaultStOkA.none {σ : Fl4SpecA64.State Unit} (h : FaultStOkA σ) {t : Fl4SpecA64.ObjRef} + {x : Fl4SpecA64.Tcb} (hx : σ.kheap t = some (.tcb x)) (hs : ¬ FaultStA x.tcbState) : x.tcbFault = none := + Classical.byContradiction fun hf => hs (h t x hx hf) + +/-- **The core spec invariant of the A64 refinement** (module docstring). -/ +structure SInvXA (σ : Fl4SpecA64.State Unit) (m : Fl4SpecA64.McState) (n : Nat) : Prop where + mc : m.WF n + view : Fl4SpecA64.McView σ m + curTcb : ∃ x, σ.kheap σ.curThread = some (.tcb x) + idleTcb : ∃ x, σ.kheap σ.idleThread = some (.tcb x) ∧ x.tcbState = .idleThreadState + onlyIdle : ∀ t x, σ.kheap t = some (.tcb x) → x.tcbState = .idleThreadState → ∃ c < n, t = m.idleThreadOf c + idleSc : ∃ x y k, σ.kheap σ.idleThread = some (.tcb x) ∧ x.tcbSchedContext = some Fl4SpecA64.idleScPtr ∧ + σ.kheap Fl4SpecA64.idleScPtr = some (.schedContext y k) ∧ y.scTcb = some σ.idleThread + curSc : ∃ y k, σ.kheap σ.curSc = some (.schedContext y k) + /-- (record edit #8) Weakened: only while the scheduler action is `resume_cur_thread`. -/ + ctQ : σ.schedulerAction = .resumeCurThread → ∀ d p, σ.curThread ∉ σ.readyQueues d p + idleQ : ∀ d p, ∀ c < n, m.idleThreadOf c ∉ σ.readyQueues d p + rq : ∀ d p t, t ∈ σ.readyQueues d p → ∃ x, σ.kheap t = some (.tcb x) ∧ Fl4SpecA64.runnable x.tcbState = true + rls : ∀ t ∈ σ.releaseQueue, ∃ x, σ.kheap t = some (.tcb x) + rpos : ∀ p y k, σ.kheap p = some (.schedContext y k) → Fl4SpecA64.scActive y = true → y.scRefills ≠ [] + /-- (gen 4) The IRQ node (`SpecIrqA`). -/ + irqN : SpecIrqA σ + /-- (record edit #8b-C2, item 11) `cur_sc_in_release_q_imp_zero_consumed` (`ZeroConsA`). -/ + zc : ZeroConsA σ + /-- (item 11) Release-queue members are runnable (`RlsRunA`). -/ + rlsRun : RlsRunA σ + /-- (item 11, ruling (C)) With time consumed the current SC's head is ready (`CurRdyA`). -/ + curScReady : CurRdyA σ + /-- (record edit #8b-D (b)) A faulted TCB is blocked on send / reply or inactive (`FaultStOkA`). -/ + faultSt : FaultStOkA σ + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/ScActivateA.lean b/proofs/Fl4ProofsA64/P3/ScActivateA.lean new file mode 100644 index 000000000..ab9773879 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScActivateA.lean @@ -0,0 +1,345 @@ +import Fl4ProofsA64.P3.ScYieldRunA +import Fl4ProofsA64.P3.ScCfgChargeA +import Fl4ProofsA64.P3.FrontEndExtraA +import Fl4SpecA64.Ext.Entry + +/-! Activate the selected thread after schedule. [J] The current TCB is +in one of the four activation states and its saved FaultIP is related. +[P] The FL4 ticks-to-microseconds conversion supplies the yield reply. -/ + +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3.Sched + +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +private theorem faultIpAddrA (t : U64) : + regWA t layout.reg.FAULT_IP = core.num.U64.wrapping_add t 528#u64 := by + unfold regWA layout.tcb.REGS layout.reg.FAULT_IP + rw [show core.num.U64.wrapping_mul 34#u64 8#u64 = 272#u64 from by decide] + apply eq_of_bv + repeat rw [core.num.U64.wrapping_add_bv_eq] + change t.bv + (256 : BitVec 64) + 272 = t.bv + 528 + rw [BitVec.add_assoc] + rfl + +private theorem faultIpKeepA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s s' : S} {σ : State Unit} (H : SchedSRN st Z m κ s σ) {t u : U64} + (ht : t ∈ Z.T) (hu : u ∈ Z.T) + (w : WritesIn st s s' (fun b => MWrA (st := st) Z m σ s b ∨ ScConsumedRegsA u b ∨ b = yieldA u)) : + L.mem s' (regWA t layout.reg.FAULT_IP) = L.mem s (regWA t layout.reg.FAULT_IP) := by + rw [faultIpAddrA] + refine w.mem_eq (field_aligned (H.core.tcbs t ht) (by decide) (by decide)) ?_ + have off := tcbLowOffA (K := 528#u64) H ht (by decide) + have ne {v K : U64} (hv : v ∈ Z.T) (hK : K.val < 4096) (hn : (528#u64 : U64).val ≠ K.val) : + core.num.U64.wrapping_add t 528#u64 ≠ core.num.U64.wrapping_add v K := + field_ne (H.core.tcbs t ht) (H.core.tcbs v hv) (by decide) hK (fun _ => hn) + rintro (h | h | h) + · exact (not_mwrA (fun e => off.2 (Or.inr (Or.inr (Or.inr (Or.inr e))))) + (fun v hv => by + rw [scA_eq, nextA_eq', prevA_eq', inqA_eq] + exact ⟨ne hv (by decide) (by decide), ne hv (by decide) (by decide), + ne hv (by decide) (by decide), ne hv (by decide) (by decide)⟩) off.1) h + · rcases h with h | h | h <;> exact ne hu (by decide) (by decide) h + · change core.num.U64.wrapping_add t 528#u64 = core.num.U64.wrapping_add u 3472#u64 at h + exact ne hu (by decide) (by decide) h + +theorem activateWritePcA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {σ : State Unit} (H : YieldQueueRelA (st := st) Z m κ s σ) {t : U64} (ht : t ∈ Z.T) + {x : Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) (v : U64) {offset index : U64} (reg : Register) + (pcOffset : offset = 512#u64 ∨ offset = 528#u64) + (wordOffset : offset.val = 256 + 8 * index.val) (indexBound : index.val < 384) : + obj.Tcb.set_reg st t s index v ⦃ s' => + let τ := updKSc σ (specPtr t) (.tcb (yieldRegA x reg v.bv)) + YieldQueueRelA (st := st) Z m κ s' τ ⦄ := by + have small : offset.val < 4096 := by rcases pcOffset with rfl | rfl <;> decide + have aligned : offset.val % 8 = 0 := by rcases pcOffset with rfl | rfl <;> decide + have al : Aligned (core.num.U64.wrapping_add t offset) := field_aligned (H.sched.core.tcbs t ht) small aligned + refine WP.spec_mono (setRegFrameA s (H.sched.core.tcbs t ht) wordOffset indexBound al) ?_ + intro s' w + let z := yieldRegA x reg v.bv + let τ := updKSc σ (specPtr t) (.tcb z) + have tcb (u : ObjRef) (y : Tcb) (hy : τ.kheap u = some (.tcb y)) : + ∃ y', σ.kheap u = some (.tcb y') ∧ y.tcbState = y'.tcbState := by + by_cases e : u = specPtr t + · rw [e, updKSc_same] at hy; cases hy; exact ⟨x, e.symm ▸ hk, rfl⟩ + · exact ⟨y, by rwa [updKSc_kheap, Function.update_of_ne e] at hy, rfl⟩ + have step := yieldTcbStepA (z := z) hk rfl rfl rfl rfl rfl + have keep {u K : U64} (hu : u ∈ Z.T) (hK : K = 3328#u64 ∨ K = 3440#u64 ∨ K = 3472#u64) : + L.mem s' (core.num.U64.wrapping_add u K) = L.mem s (core.num.U64.wrapping_add u K) := by + have he : K.val < 4096 := by rcases hK with rfl | rfl | rfl <;> decide + have al' : Aligned (core.num.U64.wrapping_add u K) := field_aligned (H.sched.core.tcbs u hu) he + (by rcases hK with rfl | rfl | rfl <;> decide) + refine w.mem_eq al' (field_ne (H.sched.core.tcbs u hu) (H.sched.core.tcbs t ht) he small ?_) + intro _ + rcases hK with rfl | rfl | rfl <;> rcases pcOffset with rfl | rfl <;> decide + have keepF {u : U64} (hu : u ∈ Z.T) : L.mem s' (flagsA u) = L.mem s (flagsA u) := + w.mem_eq (field_aligned (H.sched.core.tcbs u hu) (K := 3456#u64) (by decide) (by decide)) + (field_ne (H.sched.core.tcbs u hu) (H.sched.core.tcbs t ht) (K1 := 3456#u64) (by decide) small + (fun _ => by rcases pcOffset with rfl | rfl <;> decide)) + have low : offset.val ≤ 3328 := by rcases pcOffset with rfl | rfl <;> decide + have sch : SchedSRA st Z m κ s' τ := + H.sched.stable w (fun _ e => e.symm ▸ (tcbLowOffA H.sched.toSchedSRN ht (K := offset) low).2) + step.1 step.2 + have hs : StEqA Z.T (L.mem s') τ := by + intro u hu y hy + obtain ⟨y', hp, hstate⟩ := tcb _ _ hy + rw [stA_eq, keep hu (Or.inl rfl), ← stA_eq, hstate] + exact H.states _ hu _ hp + have view (u : U64) (hu : u ∈ Z.T) (y : Tcb) (hy : σ.kheap (specPtr u) = some (.tcb y)) : + ∃ y', τ.kheap (specPtr u) = some (.tcb y') ∧ y'.tcbMcpriority = y.tcbMcpriority ∧ y'.tcbYieldTo = y.tcbYieldTo ∧ + y'.tcbFlags = y.tcbFlags := by + by_cases e : u = t + · subst u + rw [hk] at hy; cases hy + exact ⟨z, updKSc_same _ _ _, rfl, rfl, rfl⟩ + · exact ⟨y, by rwa [updKSc_kheap, Function.update_of_ne (fun h => e (specPtr_inj h))], rfl, rfl, rfl⟩ + obtain ⟨ms, ys⟩ := yieldFrameA H.mcp H.links (fun _ hu => ⟨keep hu (Or.inr (Or.inl rfl)), keepF hu⟩) + (fun _ hu => keep hu (Or.inr (Or.inr rfl))) view (fun c hc => by + obtain ⟨y, n, hy, _⟩ := H.sched.scr.sc c hc + have ne : scNameA m c ≠ specPtr t := fun e => by rw [e, hk] at hy; cases hy + exact ⟨y, n, y, hy, by rwa [updKSc_kheap, Function.update_of_ne ne], rfl⟩) + exact ⟨sch, hs, ms, ys⟩ + +/-- [J] Activation runs only on a selected runnable or idle thread. -/ +def ActivateStateOKA (ts : ThreadState) : Prop := + ts = .running ∨ ts = .restart ∨ ts = .idleThreadState ∨ ts = .ext .runningVM + +def activatedStateA : ThreadState → ThreadState + | .restart => .running + | state => state + +set_option maxRecDepth 4096 in +theorem activate_state_refinesA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {σ : State Unit} (H : YieldQueueRelA (st := st) Z m κ s σ) (a : A) + {t : U64} (ht : t ∈ Z.T) {x : Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + (legal : ActivateStateOKA x.tcbState) + (pc : (L.mem s (regWA t layout.reg.FAULT_IP)).bv = x.tcbArch.tcbContext.userRegs .faultIP) : + (do + let ts ← obj.Tcb.state st t s + if ts = sched.TS_RESTART then + let saved ← obj.Tcb.reg st t s layout.reg.FAULT_IP + let s' ← obj.Tcb.set_reg st t s layout.reg.NEXT_IP saved + let s'' ← sched.set_thread_state st s' t sched.TS_RUNNING + ok (s'', a) + else ok (s, a)) ⦃ r => r.2 = a ∧ ∃ τ, + fl4ActivateState (Z := Unit) (specPtr t) σ = ({((), τ)}, False) ∧ + YieldQueueRelA (st := st) Z m κ r.1 τ ∧ τ.curThread = σ.curThread ∧ + ∃ y, τ.kheap (specPtr t) = some (.tcb y) ∧ + y.tcbState = activatedStateA x.tcbState ⦄ := by + have code := H.states t ht x hk + have stRead : L.mem s (stA t) = sched.TS_RESTART ↔ x.tcbState = .restart := by + have e : (u v : U64) → (u = v ↔ u.val = v.val) := + fun _ _ => ⟨congrArg UScalar.val, UScalar.eq_of_val_eq⟩ + rw [e, code] + unfold sched.TS_RESTART + rcases legal with h | h | h | h <;> rw [h] <;> decide + refine WP.spec_bind (obj.Tcb.state.spec s t) ?_ + intro ts hts + rw [hts] + by_cases hR : x.tcbState = .restart + · rw [if_pos (stRead.2 hR)] + rw [tcb_reg_eqA, bind_ok] + refine WP.spec_bind (activateWritePcA H ht hk _ .nextIP (Or.inl rfl) + (by simp [layout.reg.NEXT_IP, layout.reg.ELR_EL1]) (by simp [layout.reg.NEXT_IP, layout.reg.ELR_EL1])) ?_ + intro s' H' + have codeR : sched.TS_RUNNING.val = tsCodeA .running := by unfold sched.TS_RUNNING tsCodeA; rfl + refine WP.spec_bind (yieldRunnableA H' ht (updKSc_same _ _ _) (by change runnable x.tcbState = _; rw [hR]; rfl) + (by rfl : runnable ThreadState.running = true) codeR) ?_ + rintro s'' ⟨τ, he, H'', hheap, hcur, _, _, _⟩ + rw [WP.spec_ok] + let y := { yieldRegA x .nextIP (L.mem s (regWA t layout.reg.FAULT_IP)).bv with tcbState := .running } + refine ⟨rfl, τ, ?_, H'', hcur, y, ?_, ?_⟩ + · unfold fl4ActivateState getThreadState + rw [nd_bind_oneSc (threadGet_eqSc _ hk), hR] + dsimp only + unfold getRestartPC setNextPC + rw [nd_bind_oneSc (asUser_getRegister_eqA hk .faultIP), ← pc, nd_bind_oneSc (yieldAsUserRegA hk .nextIP _)] + exact he + · rw [hheap, updKSc_same] + · rw [hR]; rfl + · rw [if_neg (fun h => hR (stRead.1 h)), WP.spec_ok] + refine ⟨rfl, σ, ?_, H, rfl, x, hk, ?_⟩ + · unfold fl4ActivateState getThreadState + rw [nd_bind_oneSc (threadGet_eqSc _ hk)] + rcases legal with e | e | e | e + · rw [e]; rfl + · exact (hR e).elim + · rw [e]; rfl + · rw [e]; rfl + · rcases legal with e | e | e | e + · rw [e]; rfl + · exact (hR e).elim + · rw [e]; rfl + · rw [e]; rfl + +def activationYieldA (t : ObjRef) : SMonad Unit Unit := do + let yt ← getTcbObjRef Tcb.tcbYieldTo t + NonDet.when (yt ≠ none) (fl4CompleteYieldTo t) + +set_option maxRecDepth 4096 in +theorem activate_yield_refinesA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {σ : State Unit} (H : YieldQueueRelA (st := st) Z m κ s σ) (a : A) {t : U64} + (ht : t ∈ Z.T) (hC0 : ∀ c ∈ Z.C, c ≠ 0#u64) + {x : Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + (htu : Fl4TicksToUsAgreeA ar a) : + (do + let i ← obj.Tcb.yield_to st t s + if i != 0#u64 then schedcontext.complete_yield_to st ar s a t else ok (s, a)) ⦃ r => r.2 = a ∧ + ∃ τ x', activationYieldA (specPtr t) σ = ({((), τ)}, False) ∧ + YieldQueueRelA (st := st) Z m κ r.1 τ ∧ + τ.curThread = σ.curThread ∧ + τ.kheap (specPtr t) = some (.tcb x') ∧ x'.tcbState = x.tcbState ∧ + x'.tcbArch.tcbContext.userRegs .faultIP = x.tcbArch.tcbContext.userRegs .faultIP ∧ + L.mem r.1 (regWA t layout.reg.FAULT_IP) = L.mem s (regWA t layout.reg.FAULT_IP) ⦄ := by + obtain ⟨x0, h0, hv⟩ := H.links.tcb t ht + rw [hk] at h0; cases h0 + let sc := L.mem s (yieldA t) + rw [eq_of_spec_fd (obj.Tcb.yield_to.spec s t), bind_ok] + change (if sc != 0#u64 then _ else _) ⦃ _ ⦄ + by_cases hsc : sc = 0#u64 + · rw [if_neg (by simp [hsc]), WP.spec_ok] + refine ⟨rfl, σ, x, ?_, H, rfl, hk, rfl, rfl, rfl⟩ + unfold activationYieldA getTcbObjRef + rw [nd_bind_oneSc (threadGet_eqSc _ hk), hv] + change (NonDet.when (optScA m sc ≠ none) _) σ = _ + simp [hsc, optScA, NonDet.when] + rfl + · rw [if_pos (bne_iff_ne.2 hsc)] + obtain ⟨hc, y, n, hy, hFrom⟩ := H.links.toSc t ht hsc + have eq : L.mem s (scYieldFromA sc) = t := by + obtain ⟨y0, n0, e, R⟩ := H.sched.scr.sc sc hc + rw [hy] at e; cases e + have e := hFrom + have haddr : scYieldFromA sc = scWA sc 56#u64 := by unfold scYieldFromA scWA layout.sc.YIELD_FROM; rfl + rw [R.yieldFrom, ← haddr] at e + have en : L.mem s (scYieldFromA sc) ≠ 0#u64 := by + intro h; rw [h, optRefS_zero] at e; cases e + rw [optRefS_ne en] at e + exact specPtr_inj (Option.some.inj e) + have t0 : t ≠ 0#u64 := fun h => (H.sched.core.tcbs t ht).ne0 (by rw [h]; rfl) + refine WP.spec_mono (yield_complete_liveWithA H.sched H.states H.mcp H.links a hc hC0 t0 eq + fl4MaxConsumedTicks fl4TicksToUs rfl htu) ?_ + rintro r ⟨arch, τ, _, H', E', M', Y', _, hCur, _, tcb, _, w, he, -⟩ + obtain ⟨x', hk', _, hs, hp, _⟩ := tcb t ht x hk + refine ⟨arch, τ, x', ?_, ⟨H', E', M', Y'⟩, hCur, hk', hs, hp, faultIpKeepA H.sched.toSchedSRN ht ht w⟩ + unfold activationYieldA getTcbObjRef + rw [nd_bind_oneSc (threadGet_eqSc _ hk), hv] + change (NonDet.when (optScA m sc ≠ none) _) σ = _ + unfold NonDet.when + rw [if_pos (by simp [optScA, hsc])] + exact he + +set_option maxRecDepth 4096 in +/-- Completion precedes Restart, Running, idle, or RunningVM dispatch. -/ +theorem activate_thread_refinesA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {σ : State Unit} (H : YieldQueueRelA (st := st) Z m κ s σ) (a : A) + (hC0 : ∀ c ∈ Z.C, c ≠ 0#u64) (htu : Fl4TicksToUsAgreeA ar a) + {x : Tcb} (hk : σ.kheap σ.curThread = some (.tcb x)) (legal : ActivateStateOKA x.tcbState) + (pc : (L.mem s (regWA (L.curW s) layout.reg.FAULT_IP)).bv = x.tcbArch.tcbContext.userRegs .faultIP) : + sched.activate_thread st ar s a ⦃ r => r.2 = a ∧ ∃ τ, + fl4ActivateThread (Z := Unit) σ = ({((), τ)}, False) ∧ + YieldQueueRelA (st := st) Z m κ r.1 τ ∧ τ.curThread = σ.curThread ∧ + ∃ y, τ.kheap τ.curThread = some (.tcb y) ∧ + y.tcbState = activatedStateA x.tcbState ⦄ := by + let t := L.curW s + have ht := H.sched.curT + have htσ := H.sched.sch.curThread + rw [htσ] at hk + unfold sched.activate_thread + refine WP.spec_bind (sched.cur_thread.spec s) ?_ + intro ct hct + rw [show ct = t from hct, ← cfgResultAssocA] + refine WP.spec_bind (activate_yield_refinesA H a ht hC0 hk htu) ?_ + rintro ⟨s1, a1⟩ ⟨ea, σ1, x1, he, H1, hC1, h1, hS, hP, hWord⟩ + simp only [uncurry_apply_pair] + change a1 = a at ea + subst a1 + refine WP.spec_mono (activate_state_refinesA H1 a ht h1 (hS.symm ▸ legal) + (by rw [hWord, hP]; exact pc)) ?_ + rintro r ⟨arch, τ, hτ, H2, hC2, y, hy, hState⟩ + have hCur := hC2.trans hC1 + refine ⟨arch, τ, ?_, H2, hCur, y, ?_, ?_⟩ + · unfold fl4ActivateThread + rw [nd_gets_bindSc, htσ] + rw [← nd_bind_assocSc] + change (activationYieldA (specPtr t) >>= fun _ => fl4ActivateState (specPtr t)) σ = _ + rw [nd_bind_oneSc he] + exact hτ + · rw [hCur, htσ]; exact hy + · rw [hS] at hState + exact hState + +/-- The post-activation VCPU check is a concrete and abstract stutter +when the selected TCB is not in RunningVM. -/ +theorem vcpu_check_off_vmA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} + {σ : State Unit} (H : YieldQueueRelA (st := st) Z m κ s σ) (a : A) + {t : U64} (ht : t ∈ Z.T) {x : Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + (native : x.tcbState ≠ .ext .runningVM) : + vcpu.check st ar s a t = ok (s, a) ∧ + fl4VcpuCheck (Z := Unit) (specPtr t) σ = ({((), σ)}, False) := by + have ts := H.states t ht x hk + have code : (L.mem s (stA t)) ≠ sched.TS_RUNNING_VM := by + intro e + have ev : tsCodeA x.tcbState = 8 := by + rw [← ts, e] + unfold sched.TS_RUNNING_VM + rfl + apply native + revert ev + cases x.tcbState with + | ext q => cases q; simp [tsCodeA] + | _ => simp [tsCodeA] + constructor + · unfold vcpu.check + rw [eq_of_spec_fd (obj.Tcb.state.spec s t), bind_ok, if_pos (bne_iff_ne.2 code)] + · unfold fl4VcpuCheck getThreadState + rw [nd_bind_oneSc (threadGet_eqSc _ hk), NonDet.when, if_neg native] + rfl + +/-- The part of `syscall.finish` after schedule on Running, Restart, +or idle. The VCPU check uses the selected TCB's new state. -/ +theorem finish_after_schedule_off_vmA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {σ : State Unit} (H : YieldQueueRelA (st := st) Z m κ s σ) (a : A) + (hC0 : ∀ c ∈ Z.C, c ≠ 0#u64) (htu : Fl4TicksToUsAgreeA ar a) + {x : Tcb} (hk : σ.kheap σ.curThread = some (.tcb x)) (legal : ActivateStateOKA x.tcbState) + (pc : (L.mem s (regWA (L.curW s) layout.reg.FAULT_IP)).bv = x.tcbArch.tcbContext.userRegs .faultIP) + (native : x.tcbState ≠ .ext .runningVM) : + (do + let (s1, a1) ← sched.activate_thread st ar s a + let t ← sched.cur_thread st s1 + vcpu.check st ar s1 a1 t) ⦃ r => r.2 = a ∧ ∃ τ, + fl4FinishAfterSchedule (Z := Unit) σ = ({((), τ)}, False) ∧ + YieldQueueRelA (st := st) Z m κ r.1 τ ⦄ := by + refine WP.spec_bind (activate_thread_refinesA H a hC0 htu hk legal pc) ?_ + rintro ⟨s1, a1⟩ ⟨ha, τ, hActivate, H1, _, y, hy, hState⟩ + change a1 = a at ha + subst a1 + refine WP.spec_bind (sched.cur_thread.spec s1) ?_ + intro ct hct + let t := L.curW s1 + change ct = t at hct + subst ct + have hy' : τ.kheap (specPtr t) = some (.tcb y) := by + rw [← H1.sched.sch.curThread]; exact hy + have native' : y.tcbState ≠ .ext .runningVM := by + rw [hState] + rcases legal with h | h | h | h + · rw [h]; decide + · rw [h]; decide + · rw [h]; decide + · exact (native h).elim + obtain ⟨run, spec⟩ := vcpu_check_off_vmA H1 a H1.sched.curT hy' native' + rw [run, WP.spec_ok] + refine ⟨rfl, τ, ?_, H1⟩ + unfold fl4FinishAfterSchedule + rw [nd_bind_oneSc hActivate, nd_gets_bindSc, H1.sched.sch.curThread] + exact spec + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScAppendA.lean b/proofs/Fl4ProofsA64/P3/ScAppendA.lean new file mode 100644 index 000000000..1a0d1181f --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScAppendA.lean @@ -0,0 +1,131 @@ +import Fl4ProofsA64.P3.ScEnqA +import Fl4ProofsA64.P3.ScSWrA + +/-! AArch64 local ready-queue append, used at the end of a timeslice and in schedule. -/ + +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3.Sched + +open Fl4SpecA64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- The queue ghosts after appending to a TCB's affinity and priority queue. -/ +def appendQA (s : S) (t : U64) (Q : U64 → U64 → List U64) : U64 → U64 → List U64 := + let c := L.mem s (affA t) + let p := L.mem s (prioA t) + if (L.mem s (inqA t) &&& sched.Q_QUEUED) ≠ 0#u64 then Q + else Function.update Q c (Function.update (Q c) p (Q c p ++ [t])) + +theorem append_coreA {T Cs : Set U64} {Q : U64 → U64 → List U64} {R : U64 → List U64} + {s : S} (H : SchedCoreA st T Cs s Q R) (hcpu : L.cpu s ∈ Cs) {t : U64} (ht : t ∈ T) + (hr : (L.mem s (inqA t) &&& sched.Q_QUEUED) = 0#u64 → (L.mem s (inqA t) &&& sched.Q_RELEASE) = 0#u64) : + sched.append st s t ⦃ s' => SchedCoreA st T Cs s' (appendQA (st := st) s t Q) R ∧ + WritesIn st s s' (L.QFootS s (t :: Q (L.mem s (affA t)) (L.mem s (prioA t))) (L.mem s (affA t)) (136 ≤ ·) t) ⦄ := by + have hc := H.aff t ht + have hp := H.prio t ht + have hN := H.nodes _ hc + have hN' := H.nodes _ hcpu + have hReady := H.ready _ hc + have a := sched.append.ready s t _ H.tcbs ht hp hN hN' hReady (fun h0 p hp => H.notQ ht h0 hc hp) + have f := sched.append.footS s t _ H.tcbs ht hp hN hN' (hReady.1.rel _ hp) (fun h0 => H.notQ ht h0 hc hp) + refine WP.spec_mono (WP.spec_and a f) ?_ + rintro s' ⟨hR, wS, hFlag⟩ + have w := wS.mono fun _ h => h.toQFoot + refine ⟨?_, wS⟩ + unfold appendQA + dsimp only + by_cases hq : (L.mem s (inqA t) &&& sched.Q_QUEUED) ≠ 0#u64 + · rw [if_pos hq] at hR hFlag ⊢ + have htq := (H.queued t ht).1 hq + have step := core_ready_step (W := t :: Q _ _) (xs' := Q (L.mem s (affA t)) (L.mem s (prioA t))) (tIn := True) + H hcpu ht rfl rfl (fun _ hw => List.mem_cons.1 hw) + (fun _ c' hc' hm => H.QR hc hp htq hc' hm) w (by rw [Function.update_eq_self]; exact hR) + (fun y => by by_cases e : y = t <;> simp [e, htq]) (by rw [hFlag]; simpa using hq) (by rw [hFlag]) + (fun _ => H.QR hc hp htq hc) + simpa [Function.update_eq_self] using step + · have hq0 := not_not.1 hq + rw [if_neg hq] at hR hFlag ⊢ + exact core_ready_step (W := t :: Q _ _) (tIn := True) H hcpu ht rfl rfl (fun _ hw => List.mem_cons.1 hw) + (fun _ c' hc' => H.notR ht (hr hq0) hc') w hR + (fun y => by by_cases e : y = t <;> simp [e]) + (by rw [hFlag, or_qq_qq]; simpa using qq_ne) (by rw [hFlag, or_qq_qr]) + (fun _ => H.notR ht (hr hq0) hc) + +/-- Append a runnable TCB to the local ready queue, leaving the other +scheduler state and all kernel objects in place. -/ +theorem append_refines_localA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRA st Z m κ s σ) {t : U64} (ht : t ∈ Z.T) (hon : L.mem s (affA t) = L.cpu s) + (hr : (L.mem s (inqA t) &&& sched.Q_QUEUED) = 0#u64 → (L.mem s (inqA t) &&& sched.Q_RELEASE) = 0#u64) + (hRun : ∀ x, σ.kheap (specPtr t) = some (.tcb x) → runnable x.tcbState = true) : + sched.append st s t ⦃ s' => ∃ τ, + tcbSchedAction (Z := Unit) tcbSchedAppend (specPtr t) σ = ({((), τ)}, False) ∧ + τ.kheap = σ.kheap ∧ τ.releaseQueue = σ.releaseQueue ∧ τ.schedulerAction = σ.schedulerAction ∧ + SFixA σ τ ∧ SchedSRA st { Z with Q := appendQA (st := st) s t Z.Q } m κ s' τ ∧ + WritesIn st s s' (SWrA (st := st) Z s) ⦄ := by + have hc := H.core.aff t ht + have hp := H.core.prio t ht + obtain ⟨x, hk, hd, -, hpr⟩ := H.rest.prio t ht + have hW : ∀ u ∈ t :: Z.Q (L.mem s (affA t)) (L.mem s (prioA t)), u ∈ Z.T := by + intro u hu + rcases List.mem_cons.1 hu with rfl | hu + · exact ht + · exact H.core.memT hc hp hu + refine WP.spec_mono (append_coreA H.core H.sch.cpuCs ht hr) ?_ + rintro s' ⟨hCore, wS⟩ + have w := wS.mono fun _ h => h.toQFoot + have hSpec := tcbSchedAction_eqA hk hd tcbSchedAppend + let τ := setRqA σ x.tcbPriority (tcbSchedAppend (specPtr t) (σ.readyQueues 0 x.tcbPriority)) + have fix : SFixA σ τ := ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ + refine ⟨τ, hSpec, rfl, rfl, rfl, fix, ?_, (wS.qfootN_of hon).mono fun _ b => H.toSchedSRN.qfoot_swr hW hc ht b⟩ + have hdq := ofNat_prio_eqA hpr + have Q := H.rest.rq _ hp + rw [hdq] at Q + by_cases hq : (L.mem s (inqA t) &&& sched.Q_QUEUED) ≠ 0#u64 + · have htq := (H.core.queued t ht).1 hq + have same : tcbSchedAppend (specPtr t) (σ.readyQueues 0 x.tcbPriority) = + σ.readyQueues 0 x.tcbPriority := by + unfold tcbSchedAppend + rw [if_neg (by rw [not_not, Q, ← hon, mem_map_specPtrSc]; exact htq)] + have e : τ = σ := by unfold τ; rw [same, setRqA_self] + have q : appendQA (st := st) s t Z.Q = Z.Q := by unfold appendQA; rw [if_pos hq] + rw [q] at hCore + rw [e, q] + exact H.qfoot hW hc ht w rfl (SFixA.refl σ) rfl hCore H.rest.rq H.rest.rqDom rfl H.run + · have htq := H.core.notQ ht (not_not.1 hq) hc hp + have e : tcbSchedAppend (specPtr t) (σ.readyQueues 0 x.tcbPriority) = + σ.readyQueues 0 x.tcbPriority ++ [specPtr t] := by + unfold tcbSchedAppend + rw [if_pos (by rw [Q, ← hon, mem_map_specPtrSc]; exact htq)] + have q : appendQA (st := st) s t Z.Q = Function.update Z.Q (L.mem s (affA t)) + (Function.update (Z.Q (L.mem s (affA t))) (L.mem s (prioA t)) + (Z.Q (L.mem s (affA t)) (L.mem s (prioA t)) ++ [t])) := by + unfold appendQA + rw [if_neg hq] + rw [q] at hCore ⊢ + refine H.qfoot hW hc ht w rfl fix rfl hCore (fun p hp' => ?_) (fun d p hn => ?_) rfl + ⟨fun d p u hu => ?_, H.run.2⟩ + · show (if (0 : Domain) = 0 ∧ BitVec.ofNat 8 p.val = x.tcbPriority then _ else _) = _ + rw [hon, Function.update_self] + by_cases pe : p = L.mem s (prioA t) + · subst pe + rw [if_pos ⟨rfl, hdq⟩, Function.update_self, e, Q, List.map_append, List.map_cons, List.map_nil] + · rw [if_neg (fun h => pe (ofNat_prio_injA hp' hp (h.2.trans hdq.symm))), Function.update_of_ne pe] + exact H.rest.rq p hp' + · show (if d = 0 ∧ p = x.tcbPriority then _ else _) = _ + rw [if_neg (fun h => hn h.1)] + exact H.rest.rqDom d p hn + · change u ∈ (if d = 0 ∧ p = x.tcbPriority then _ else _) at hu + split_ifs at hu + · rw [e] at hu + rcases List.mem_append.1 hu with h | h + · exact H.run.1 0 x.tcbPriority u h + · obtain rfl := List.mem_singleton.1 h + exact ⟨x, hk, hRun x hk⟩ + · exact H.run.1 d p u hu + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScArchSwA.lean b/proofs/Fl4ProofsA64/P3/ScArchSwA.lean new file mode 100644 index 000000000..1a8f8ae2a --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScArchSwA.lean @@ -0,0 +1,336 @@ +import Fl4ProofsA64.P3.VsTieA +import Fl4SpecA64.Ext.ARMVCPURun + +/-! +AArch64 scheduler architecture boundary. [J] `VsRootOKA` relates the +address-space lookup to the typed heap, and `IdleVtA` identifies the idle +TCB. [P] `ArchSwFA` relates root writes at execution and the per-core +hardware observations at return. The incoming FPU/VCPU image and ownership +conditions are recorded by `FpuOwnOKA` and the return view. + +Record edit F4: `execIdle` / `execThread` also take the four record facts `JAtZAX` lacks, so the statement is +satisfiable: [J] `K` the `KERNEL_ROOT` word is the spec's global user VSpace (`NdOKA.kroot`, `JStage3A.kroot`), `c0` the +single core is core 0 (`numNodes = 1`), `Y0` no high window is at address 0, `R` `RunLinkA` (`VcRunA.run` / `.vtcb`). +The scheduler layer derives them from the record (`ArchTieA`, `JStage3A.archRestA`, `ArchRestA.keepMemA`). +-/ + +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3.Sched + +open Fl4SpecA64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +def highCapA (σ : State Unit) (t : ObjRef) : Option Cap := + match σ.kheap t with + | some (.tcb x) => tcbCnodeMap x (tcbCnodeIndex 6) + | _ => none + +def highRootA (σ : State Unit) (t : ObjRef) : Option ObjRef := + match highCapA σ t with + | some (.archObjectCap (.ext (.highVSpaceCap root (some asid)))) => + if vspaceForAsid asid σ = some root ∧ rootKindAt σ root = .high then some root else none + | _ => none + +def runningVmA (σ : State Unit) (t : ObjRef) : Prop := + ∃ x, σ.kheap t = some (.tcb x) ∧ x.tcbState = .ext .runningVM + +/-- The two pointers read by the native RunningVM path. The reciprocal +caller and bound-guest links are invariants of the joint relation. -/ +def runGuestA (σ : State Unit) (t : ObjRef) : Option (ObjRef × ObjRef) := do + let .tcb x ← σ.kheap t | none + let v ← x.tcbExt.tcbRunVcpu + let vc ← vcpusOf σ v + let guest ← vc.vcpuTcb + pure (v, guest) + +def globalRootA (σ : State Unit) : HwRootA := + ⟨σ.archState.armUsGlobalVspace, none⟩ + +/-- [J] A successful mapped Stage-1 lookup is typed. The two run pointers +denote the matching VCPU and its bound TCB when they are present. -/ +structure VsRootOKA (σ : State Unit) : Prop where + s1 : ∀ t root asid va, + vtCapA σ t = some (.archObjectCap (.pageTableCap root .vsRootPtT (some (asid, va)))) → + vspaceForAsid asid σ = some root → rootKindAt σ root = .stage1 + userAsid : ∀ t asid, (vmRootA σ t).asid = some asid → asid ≠ 0 + bound : ∀ t v, vcpuOfA σ t = some v → + ∃ x, vcpusOf σ v = some x ∧ x.vcpuTcb = some t + run : ∀ t v g, runningVmA σ t → runGuestA σ t = some (v, g) → + ∃ x, vcpusOf σ v = some x ∧ x.vcpuExt.runCaller = some t ∧ vcpuOfA σ g = some v + +/-- [J] The idle TCB has an empty user vtable and no guest links. -/ +structure IdleVtA (σ : State Unit) : Prop where + tcb : ∃ x, σ.kheap σ.idleThread = some (.tcb x) ∧ x.tcbState = .idleThreadState + slot : vtCapA σ σ.idleThread = some .nullCap + bound : vcpuOfA σ σ.idleThread = none + run : runGuestA σ σ.idleThread = none + +inductive RootWriteA where + | stage1 (target : HwRootA) + | high (target : Option ObjRef) (asid : Option Asid) + | stage2 (target : Option HwRootA) + +structure RootViewA where + stage1 : HwRootA + high : Option (ObjRef × Asid) + stage2 : Option HwRootA + +def RootViewA.write (old : RootViewA) : RootWriteA → RootViewA + | .stage1 v => + { old with + stage1 := v + high := if old.stage1.root = v.root ∧ old.stage1.asid = v.asid then old.high else none } + | .high v a => { old with high := v.bind fun p => a.map fun asid => (p, asid) } + | .stage2 v => { old with stage2 := v } + +def kernelRootWritesA (σ : State Unit) : List RootWriteA := + [.high none none, .stage1 (globalRootA σ)] + +/-- The high window is tagged with the incoming Stage-1 ASID. A missing +RunningVM pointer leaves VTTBR as it was. A bad guest root writes zero. -/ +def threadRootWritesA (σ : State Unit) (t : ObjRef) : List RootWriteA := by + classical + exact if (vcpuOfA σ t).isSome then + kernelRootWritesA σ ++ [.stage2 (vmS2RootA σ t)] + else + let root := vmRootA σ t + let high := if root.asid.isNone ∨ runningVmA σ t then none else highRootA σ t + let writes := match high with + | none => [.high none root.asid, .stage1 root] + | some p => [.stage1 root, .high (some p) root.asid] + writes ++ if runningVmA σ t then + match runGuestA σ t with + | some (_, guest) => [.stage2 (vmS2RootA σ guest)] + | none => [] + else [] + +theorem nativeRoot_typedA {σ : State Unit} (h : VsRootOKA σ) (t pt : ObjRef) + (asid : Asid) (va : VSpaceRef) + (hc : vtCapA σ t = some (.archObjectCap (.pageTableCap pt .vsRootPtT (some (asid, va))))) + (hv : vspaceForAsid asid σ = some pt) : + vmRootA σ t = ⟨pt, some asid⟩ ∧ rootKindAt σ pt = .stage1 := by + refine ⟨?_, h.s1 _ _ _ _ hc hv⟩ + simp [vmRootA, hc, hv] + +theorem kernelRoot_viewA (σ : State Unit) (before : RootViewA) : + (kernelRootWritesA σ).foldl RootViewA.write before = ⟨globalRootA σ, none, before.stage2⟩ := by + simp [kernelRootWritesA, RootViewA.write] + +theorem boundRoot_viewA {σ : State Unit} {t v : ObjRef} (h : vcpuOfA σ t = some v) (before : RootViewA) : + (threadRootWritesA σ t).foldl RootViewA.write before = ⟨globalRootA σ, none, vmS2RootA σ t⟩ := by + simp [threadRootWritesA, h, kernelRootWritesA, RootViewA.write] + +/-- The native RunningVM path clears the high window and installs the +guest's checked Stage-2 root. -/ +theorem runningVmRoot_viewA {σ : State Unit} {t v g : ObjRef} + (native : vcpuOfA σ t = none) (run : runningVmA σ t) + (link : runGuestA σ t = some (v, g)) (before : RootViewA) : + (threadRootWritesA σ t).foldl RootViewA.write before = + ⟨vmRootA σ t, none, vmS2RootA σ g⟩ := by + simp [threadRootWritesA, native, run, link, RootViewA.write] + +/-- A missing run pointer suppresses the high window without rewriting +VTTBR before the guest-check recovery. -/ +theorem missingRunRoot_viewA {σ : State Unit} {t : ObjRef} + (native : vcpuOfA σ t = none) (run : runningVmA σ t) + (link : runGuestA σ t = none) (before : RootViewA) : + (threadRootWritesA σ t).foldl RootViewA.write before = + ⟨vmRootA σ t, none, before.stage2⟩ := by + simp [threadRootWritesA, native, run, link, RootViewA.write] + +inductive A64ReturnModeA where + | el1 | vhe | nvhe + deriving DecidableEq + +inductive FpuTrapA where + | enabled | el0 | el0el1 + +structure FpuCoreA where + owner : Option ObjRef + saved : ObjRef → FpuState + live : FpuState + uses : ObjRef → BitVec 8 + trap : FpuTrapA + guestTrap : Bool + +/-- [P] A live FPU owner is a TCB and cannot own two physical cores. -/ +def FpuOwnOKA (σ : State Unit) (fpu : Nat → FpuCoreA) : Prop := + (∀ core t, (fpu core).owner = some t → ∃ x, σ.kheap t = some (.tcb x)) ∧ + (∀ i j t, (fpu i).owner = some t → (fpu j).owner = some t → i = j) + +def fpuModeA (before : FpuCoreA) (mode : A64ReturnModeA) (guest enabled : Bool) : FpuCoreA := + { before with + trap := if enabled then .enabled else if guest && mode == .vhe then .el0el1 else .el0 + guestTrap := if guest && mode == .nvhe then !enabled else before.guestTrap } + +def switchFpuA (before : FpuCoreA) (mode : A64ReturnModeA) (guest disabled : Bool) + (t : ObjRef) : FpuCoreA := + if disabled then fpuModeA before mode guest false + else if before.owner = some t then fpuModeA before mode guest true + else if (before.uses t).toNat < 3 then fpuModeA before mode guest false + else + let saved := match before.owner with + | some old => Function.update before.saved old before.live + | none => before.saved + let after : FpuCoreA := { + before with + owner := some t + saved := saved + live := before.saved t + uses := Function.update before.uses t (before.uses t + 1) + } + fpuModeA after mode guest true + +theorem switchFpu_lazyA (before : FpuCoreA) (mode : A64ReturnModeA) (guest : Bool) + (t : ObjRef) (ne : before.owner ≠ some t) (h : (before.uses t).toNat < 3) : + (switchFpuA before mode guest false t).owner = before.owner ∧ + (switchFpuA before mode guest false t).live = before.live ∧ + (switchFpuA before mode guest false t).saved = before.saved := by + simp [switchFpuA, ne, h, fpuModeA] + +theorem switchFpu_eagerA (before : FpuCoreA) (mode : A64ReturnModeA) (guest : Bool) + (t old : ObjRef) (own : before.owner = some old) (ne : old ≠ t) (h : 3 ≤ (before.uses t).toNat) : + (switchFpuA before mode guest false t).owner = some t ∧ + (switchFpuA before mode guest false t).live = before.saved t ∧ + (switchFpuA before mode guest false t).saved old = before.live := by + simp [switchFpuA, own, ne, Nat.not_lt.mpr h, fpuModeA] + +inductive VcpuReturnA where + | keep | suspend (owner : ObjRef) | resume (owner : ObjRef) | refreshPpi (owner : ObjRef) + | load (owner : ObjRef) (save : Option (ObjRef × Bool)) + | enterNvhe (owner : ObjRef) + +def vcpuReturnA (mode : A64ReturnModeA) (owner : Option ObjRef) (active : Bool) + (target : Option ObjRef) : VcpuReturnA := + match mode with + | .el1 => .keep + | .nvhe => match target with | none => .keep | some v => .enterNvhe v + | .vhe => + match target with + | none => + match owner with + | some old => if active then .suspend old else .keep + | none => .keep + | some v => + if owner = some v then + if active then .refreshPpi v else .resume v + else .load v (owner.map fun old => (old, active)) + +/-- The TCB whose user context is restored, and its VCPU when returning +to a guest. An idle return has neither. -/ +def returnTargetA (σ : State Unit) : Option (ObjRef × Bool) × Option ObjRef := by + classical + let t := σ.curThread + exact if t = σ.idleThread then (none, none) else + let guest := if runningVmA σ t then runGuestA σ t else none + let (v, selected) := match guest with + | some (v, g) => (some v, g) + | none => (vcpuOfA σ t, t) + let flags := match σ.kheap selected with + | some (.tcb x) => decide (TcbFlag.fpuDisabled ∈ x.tcbFlags) + | _ => false + (some (selected, flags), v) + +theorem returnTarget_idleA {σ : State Unit} (idle : σ.curThread = σ.idleThread) : + returnTargetA σ = (none, none) := by simp [returnTargetA, idle] + +/-- The saved and live portions of the VCPU hardware view. The HCR and +PPI fields describe return after the EL2 transition. -/ +structure VcpuCoreViewA where + owner : Option ObjRef + active : Bool + saved : ObjRef → VcpuState + live : VcpuState + guestHcr : Option Word + maskVgic : Bool + maskVtimer : Bool + +/-- [P] The assembly path saves only the overwritten owner image and +loads from the selected object's saved view. A suspend preserves the +VHE owner and saves its timer and VGIC state before deactivation. -/ +def VcpuReturnFrameA (before after : VcpuCoreViewA) (action : VcpuReturnA) : Prop := + match action with + | .keep => after.owner = before.owner ∧ after.active = before.active ∧ + after.saved = before.saved ∧ after.live = before.live + | .suspend owner => after.owner = some owner ∧ after.active = false ∧ + (after.saved owner).vcpuRegs .vcpuRegCNTV_CTL = before.live.vcpuRegs .vcpuRegCNTV_CTL ∧ + (after.saved owner).vcpuRegs .vcpuRegCNTVOFF = before.live.vcpuRegs .vcpuRegCNTVOFF ∧ + (after.saved owner).vcpuVgic.vgicHcr = before.live.vcpuVgic.vgicHcr ∧ + ∀ v, v ≠ owner → after.saved v = before.saved v + | .resume owner => after.owner = some owner ∧ after.active = true ∧ after.saved = before.saved ∧ + after.live.vcpuRegs .vcpuRegCNTV_CTL = (before.saved owner).vcpuRegs .vcpuRegCNTV_CTL ∧ + after.live.vcpuRegs .vcpuRegCNTVOFF = (before.saved owner).vcpuRegs .vcpuRegCNTVOFF ∧ + after.live.vcpuVgic.vgicHcr = (before.saved owner).vcpuVgic.vgicHcr + | .refreshPpi owner => after.owner = some owner ∧ after.active = true ∧ + after.saved = before.saved ∧ after.live = before.live ∧ after.maskVgic = false + | .load owner save => after.owner = some owner ∧ after.active = true ∧ + (∀ v, save.map Prod.fst ≠ some v → after.saved v = before.saved v) ∧ + after.live.vcpuRegs .vcpuRegCNTV_CTL = (before.saved owner).vcpuRegs .vcpuRegCNTV_CTL ∧ + after.live.vcpuRegs .vcpuRegCNTVOFF = (before.saved owner).vcpuRegs .vcpuRegCNTVOFF + | .enterNvhe owner => + after.owner = before.owner ∧ after.active = before.active ∧ + after.live.vcpuRegs .vcpuRegSCTLR = (before.saved owner).vcpuRegs .vcpuRegSCTLR + +section tie +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- [J] (record edit F4) **The run links of a thread**: a nonzero `RUN_VCPU` word is a record VCPU and that VCPU's nonzero +TCB word is a record thread (`VcRunA.run` / `.vtcb` of `JStage3A.vcRun`). `JAtZAX` gives those words' meaning only for +members of `Z.V` / `Z.T`, while FL4's `set_vm_root` of a running thread reads the guest's VTABLE slot from raw memory. -/ +def RunLinkA (Z : XSetsA) (s : S) (t : U64) : Prop := + L.mem s (runVcpuA t) ≠ 0#u64 → L.mem s (runVcpuA t) ∈ Z.V ∧ + (L.mem s (L.mem s (runVcpuA t)) ≠ 0#u64 → L.mem s (L.mem s (runVcpuA t)) ∈ Z.T) + +/-- **The four facts about the store `set_vm_root` / `set_kernel_vm_root` read that `JAtZAX` does not give** +(record edit F4; each a record fact of `JStage3A`): the `KERNEL_ROOT` word is the spec's global user VSpace (`K`, +`JStage3A.kroot`), the single core is core 0 (`c0`), no high window is at address 0 (`Y0`: FL4 reads `0` as "no +window") and the run links (`R`). -/ +structure ArchRestA (Z : XSetsA) (s : S) : Prop where + c0 : (L.cpu s).val = 0 + Y0 : ∀ y ∈ Z.Y, y ≠ 0#u64 + R : ∀ t ∈ Z.T, RunLinkA (L := L) Z s t + +/-- `ArchRestA` with the `KERNEL_ROOT` tie `K` (`NdOKA.kroot`, from the record's `domOK`). -/ +structure ArchTieA (Z : XSetsA) (s : S) (σ : Fl4SpecA64.State Unit) : Prop extends ArchRestA (L := L) Z s where + K : specPtr (L.mem s (krA (L := L) s)) = σ.archState.armUsGlobalVspace + +end tie + +/-- [P] The concrete backend realizes ordered root writes and their +per-core register view. Firmware/assembly return uses the computed FPU +frame and VCPU action on the selected core. -/ +structure ArchSwFA {S A : Type} (st : machine.Store S) (ar : machine.Arch A) [L : LawfulStore st] where + roots : A → Nat → RootViewA + rootWrites : A → A → Nat → List RootWriteA → Prop + rootFrame : ∀ a a' c writes, rootWrites a a' c writes → + roots a' c = writes.foldl RootViewA.write (roots a c) ∧ ∀ d, d ≠ c → roots a' d = roots a d + execIdle : ∀ s a c σ Z m AR, JAtZAX (L := L) AR Z s a σ m → + (L.cpu s).val = c → IdleVtA σ → + specPtr (L.mem s (krA (L := L) s)) = σ.archState.armUsGlobalVspace → (L.cpu s).val = 0 → + vspace.set_kernel_vm_root st ar s a ⦃ r => r.1 = s ∧ + rootWrites a r.2 c (kernelRootWritesA σ) ⦄ + execThread : ∀ s a c σ t Z m AR, JAtZAX (L := L) AR Z s a σ m → + (L.cpu s).val = c → t ∈ Z.T → VsRootOKA σ → + (∀ y ∈ Z.Y, y ≠ 0#u64) → specPtr (L.mem s (krA (L := L) s)) = σ.archState.armUsGlobalVspace → + RunLinkA (L := L) Z s t → (L.cpu s).val = 0 → + vspace.set_vm_root st ar s a t ⦃ r => r.1 = s ∧ + rootWrites a r.2 c (threadRootWritesA σ (specPtr t)) ⦄ + fpu : A → Nat → FpuCoreA + vcpu : A → Nat → VcpuCoreViewA + returnStep : A → A → Nat → A64ReturnModeA → State Unit → Prop + returnFrame : ∀ a a' c mode σ, FpuOwnOKA σ (fpu a) → returnStep a a' c mode σ → + let (thread, guest) := returnTargetA σ + let old := vcpu a c + fpu a' c = (match thread with + | none => fpu a c + | some (t, disabled) => switchFpuA (fpu a c) mode guest.isSome disabled t) ∧ + VcpuReturnFrameA old (vcpu a' c) (vcpuReturnA mode old.owner old.active guest) ∧ + (guest.isNone → (vcpu a' c).guestHcr = none) ∧ + (guest.isSome → (roots a' c).high = none) ∧ + ∀ d, d ≠ c → fpu a' d = fpu a d ∧ vcpu a' d = vcpu a d ∧ roots a' d = roots a d + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScAwakenA.lean b/proofs/Fl4ProofsA64/P3/ScAwakenA.lean new file mode 100644 index 000000000..fa11380b7 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScAwakenA.lean @@ -0,0 +1,236 @@ +import Fl4ProofsA64.P3.ScRelRmA +import Fl4ProofsA64.P3.ScYieldQueueA +import Fl4ProofsA64.P3.ScResumeA +import Fl4SpecA64.Ext.SchedTail + +/-! +Drain the ready release queue after the FL4 TCB timer pass. [J] Idle +threads are never runnable in any core's view. The timer pass itself +uses the notification signal and timer observations at its boundary. +-/ + +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3.Sched + +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +theorem readReleaseEmptyA {σ : State Unit} (hq : σ.releaseQueue = []) : + readReleaseQNonEmptyAndReady (Z := Unit) σ = some false := by + show Option.bind (Lookup.asks (fun s : State Unit => s.releaseQueue) σ) (fun rq => + (if rq = [] then Lookup.oreturn false else readTcbRefillReady (the rq.head?)) σ) = _ + simp [Lookup.asks, hq, Lookup.oreturn] + +theorem readReleaseHeadA {σ : State Unit} {t : ObjRef} {rest : List ObjRef} (hq : σ.releaseQueue = t :: rest) + {x : Tcb} {p : ObjRef} {y : SchedContext} {n : Nat} (hk : σ.kheap t = some (.tcb x)) + (hsc : x.tcbSchedContext = some p) (hy : σ.kheap p = some (.schedContext y n)) (hne : y.scRefills ≠ []) : + readReleaseQNonEmptyAndReady (Z := Unit) σ = some (refillReady σ.curTime (refillHd y)) := by + have ht : getTcb (Z := Unit) t σ = some x := by simp [getTcb, readObject, hk] + have hlink : readTcbObjRef (Z := Unit) Tcb.tcbSchedContext t σ = some (some p) := by + change Option.bind (getTcb (Z := Unit) t σ) (fun a => Lookup.oreturn a.tcbSchedContext σ) = _ + rw [ht, ← hsc] + rfl + have hd : readScRefillReady (Z := Unit) p σ = some (refillReady σ.curTime (refillHd y)) := by + show Option.bind (readRefillHead p σ) (fun h => Option.bind (Lookup.asks (fun s : State Unit => s.curTime) σ) + (fun ct => Lookup.oreturn (refillReady ct h) σ)) = _ + rw [readRefillHead_eqA hy hne] + rfl + have hread : readTcbRefillReady (Z := Unit) t σ = some (refillReady σ.curTime (refillHd y)) := by + show Option.bind (readTcbObjRef Tcb.tcbSchedContext t σ) (fun o => Option.bind (Lookup.oassertOpt o σ) + (fun p => Option.bind (readScRefillReady p σ) (fun r => Lookup.oreturn r σ))) = _ + rw [hlink] + show Option.bind (readScRefillReady p σ) (fun r => Lookup.oreturn r σ) = _ + rw [hd] + rfl + show Option.bind (Lookup.asks (fun s : State Unit => s.releaseQueue) σ) (fun rq => + (if rq = [] then Lookup.oreturn false else readTcbRefillReady (the rq.head?)) σ) = _ + simp only [Lookup.asks, hq, Option.bind_some, reduceCtorEq, ↓reduceIte, List.head?_cons, the_someA] + exact hread + +theorem wakeReleaseStepA {σ τ : State Unit} {t : ObjRef} {rest : List ObjRef} + (hq : σ.releaseQueue = t :: rest) + (he : possibleSwitchTo (Z := Unit) t (releaseRemoveStA σ t) = ({((), τ)}, False)) : + tcbReleaseDequeue (Z := Unit) σ = ({((), τ)}, False) := by + unfold tcbReleaseDequeue + rw [nd_gets_bindSc, hq, nd_bind_oneSc (assert_trueSc (by simp) σ)] + simp only [List.head?_cons, the_someA] + rw [nd_bind_oneSc (tcbReleaseRemove_eqA σ t)] + exact he + +/-- [J] The frozen idle invariant for the per-core idle threads. It is +insensitive to changes of scheduler words and queues. -/ +def IdleThreadsA (m : McState) (σ : State Unit) : Prop := + ∀ c < fl4A64Platform.numNodes, ∀ x, σ.kheap (m.idleThreadOf c) = some (.tcb x) → + runnable x.tcbState = false + +theorem releaseLenA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {σ : State Unit} (H : SchedSRN st Z m κ s σ) : + σ.releaseQueue.length < layout.WALK_FUEL.val := by + let r := Z.R (L.cpu s) + have rel := H.core.release _ H.sch.cpuCs + rw [H.rest.rel, List.length_map, walk_fuel_val] + have h01 : ∀ t ∈ r, t ≠ 0#u64 ∧ t ≠ 1#u64 := fun t ht => tcb_ne01Sc (H.core.tcbs t (rel.mem t ht)) + have hnd : (0#u64 :: 1#u64 :: r).Nodup := by + refine List.nodup_cons.2 ⟨?_, List.nodup_cons.2 ⟨fun hm => (h01 _ hm).2 rfl, rel.nodup⟩⟩ + intro hm + rcases List.mem_cons.1 hm with e | hm + · exact (by decide : (0#u64 : U64) ≠ 1#u64) e + · exact (h01 _ hm).1 rfl + have le := nodup_length_le hnd + simp only [List.length_cons] at le + change r.length < 2^64 - 1 + omega + +set_option maxHeartbeats 600000 in +/-- The concrete release walk drains exactly the ready prefix. Its +strict fuel margin follows from placement and the queue's distinct +non-sentinel TCB addresses. -/ +theorem awaken_loop_refinesA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {σ : State Unit} (H : YieldQueueRelA (st := st) Z m κ s σ) (idle : IdleThreadsA m σ) : + sched.awaken_loop st s layout.WALK_FUEL ⦃ s' => ∃ τ Q' R', + awaken (Z := Unit) σ = ({((), τ)}, False) ∧ + YieldQueueRelA (st := st) {Z with Q := Q', R := R'} m κ s' τ ∧ + τ.kheap = σ.kheap ∧ (∀ t ∈ τ.releaseQueue, t ∈ σ.releaseQueue) ⦄ := by + unfold sched.awaken_loop + apply loop.spec_decr_nat (measure := fun x => x.2.val) (inv := fun x => ∃ ρ Q R, + YieldQueueRelA (st := st) {Z with Q := Q, R := R} m κ x.1 ρ ∧ + ρ.releaseQueue.length < x.2.val ∧ awaken (Z := Unit) σ = awaken (Z := Unit) ρ ∧ + ρ.kheap = σ.kheap ∧ (∀ t ∈ ρ.releaseQueue, t ∈ σ.releaseQueue)) + · rintro ⟨sk, fuel⟩ ⟨ρ, Q, R, Hk, len, loopEq, heap, sub⟩ + dsimp only at * + unfold sched.awaken_loop.body + have pos : fuel > 0#u64 := by rw [gt_iff_val]; simp; omega + rw [if_pos pos] + obtain ⟨prev, dec, val⟩ := spec_ex_fd (U64.sub_spec (x := fuel) (y := 1#u64) (by scalar_tac)) + rw [dec, bind_ok, L.cur_cpu_eq, bind_ok, eq_of_spec_fd (obj.node_get.spec sk _ _), bind_ok] + have rel := Hk.sched.core.release _ Hk.sched.sch.cpuCs + have words : L.mem sk (L.nodeAt sk (L.cpu sk) layout.node.RELEASE_HEAD) = (R (L.cpu sk)).headD 0#u64 := rel.head + rw [words] + cases list : R (L.cpu sk) with + | nil => + rw [eq_of_spec_fd (obj.Tcb.is_null.spec _)] + simp only [List.headD_nil, decide_true, bind_ok, ↓reduceIte, WP.spec_ok] + have empty : ρ.releaseQueue = [] := by + have e : ρ.releaseQueue = (R (L.cpu sk)).map specPtr := Hk.sched.rest.rel + rw [e, list] + rfl + refine ⟨ρ, Q, R, ?_, Hk, heap, sub⟩ + rw [loopEq] + unfold awaken + exact wl_doneSc (by rw [readReleaseEmptyA empty]; simp [the_someA]) + | cons t rest => + have tr : t ∈ R (L.cpu sk) := by rw [list]; exact List.mem_cons_self + have ht : t ∈ Z.T := rel.mem _ tr + have hon : L.mem sk (affA t) = L.cpu sk := Hk.sched.core.inR _ Hk.sched.sch.cpuCs _ tr + have t0 := (tcb_ne01Sc (Hk.sched.core.tcbs t ht)).1 + have null : obj.Tcb.is_null t = ok false := by + rw [eq_of_spec_fd (obj.Tcb.is_null.spec t)] + simp [t0] + simp only [List.headD_cons, null, bind_ok, Bool.false_eq_true, ↓reduceIte] + have q : ρ.releaseQueue = specPtr t :: rest.map specPtr := by + have e : ρ.releaseQueue = (R (L.cpu sk)).map specPtr := Hk.sched.rest.rel + rw [e, list] + rfl + have hm : specPtr t ∈ ρ.releaseQueue := by rw [q]; exact List.mem_cons_self + obtain ⟨x, p, y, n, hx, hsc, hy, active⟩ := Hk.sched.inv.relSc _ hm + obtain ⟨x', hx', hword⟩ := Hk.sched.sch.tcb t ht + rw [hx] at hx'; cases hx' + have c0 : L.mem sk (scA t) ≠ 0#u64 := by + intro e + rw [hword, optScA, if_pos e] at hsc + cases hsc + let sc := L.mem sk (scA t) + have scEq : p = scNameA m sc := by + rw [hword, optScA, if_neg c0] at hsc + exact (Option.some.inj hsc).symm + subst p + rw [eq_of_spec_fd (obj.Tcb.sc.spec sk t), bind_ok] + refine WP.spec_bind (ready_refinesA Hk.sched.scr Hk.sched.sch.time (Hk.sched.rest.scMem t ht c0) hy active) ?_ + intro b hb + rw [getScRefillReady_eqA hy (Hk.sched.inv.ne hy active)] at hb + have ready : refillReady ρ.curTime (refillHd y) = b := + (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst hb))).1 + have read := readReleaseHeadA q hx hsc hy (Hk.sched.inv.ne hy active) + cases b with + | false => + simp only [Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + refine ⟨ρ, Q, R, ?_, Hk, heap, sub⟩ + rw [loopEq] + unfold awaken + exact wl_doneSc (by rw [read, ready]; simp [the_someA]) + | true => + simp only [↓reduceIte] + let mid := releaseRemoveStA ρ (specPtr t) + have run : runnable x.tcbState = true := by + obtain ⟨w, hw, hr⟩ := Hk.sched.run.2 _ hm + rw [hx] at hw; cases hw; exact hr + have ni : NotIdleScA m (scNameA m sc) := by + refine Hk.sched.inv.tcbScNotIdle _ x _ hx hsc ?_ + intro c hc e + have no := idle c hc x (by rw [heap] at hx; rwa [← e]) + rw [no] at run + cases run + refine WP.spec_bind (release_remove_refines_localA Hk.sched ht hon) ?_ + rintro s1 ⟨R1, _, H1, W1⟩ + have a1 : SchedSRA st {Z with Q := Q, R := R1} m κ s1 mid := + H1.toA (fun x1 p1 y1 n1 hx1 hsc1 hy1 _ _ => by + change ρ.kheap (specPtr t) = some (.tcb x1) at hx1 + rw [hx] at hx1; cases hx1 + rw [hsc] at hsc1; cases hsc1 + change ρ.kheap (scNameA m sc) = some (.schedContext y1 n1) at hy1 + rw [hy] at hy1; cases hy1 + exact ⟨ready, Hk.sched.inv.suff hy active ni⟩) + have local1 : L.mem s1 (affA t) = L.cpu s1 := yieldAffKeepA Hk.sched a1 ht ht hon + obtain ⟨S1, M1, Y1⟩ := yieldQueueFrameA Hk.sched Hk.states Hk.mcp Hk.links (σ' := mid) rfl W1.wr + refine WP.spec_bind (possible_switch_to_refines_releaseA a1 t ht local1 + (fun w hw => by change ρ.kheap (specPtr t) = _ at hw; rw [hx] at hw; cases hw; exact run) S1) ?_ + rintro s2 ⟨τ, Q2, sw, heap2, rel2, W2, H2⟩ + rw [WP.spec_ok] + obtain ⟨S2, M2, Y2⟩ := yieldQueueFrameA a1 S1 M1 Y1 heap2 (W2.mono fun _ w => Or.inr w) + have tail : mid.releaseQueue = rest.map specPtr := by + have nodup : (R (L.cpu sk)).Nodup := rel.nodup + rw [list, List.nodup_cons] at nodup + change tcbSchedDequeue _ ρ.releaseQueue = _ + rw [q] + unfold tcbSchedDequeue + simp only [List.filter_cons, ne_eq, not_true_eq_false, decide_false] + exact List.filter_eq_self.2 fun u hu => decide_eq_true (fun e => + nodup.1 (mem_map_specPtrSc.1 (e ▸ hu))) + refine ⟨⟨τ, Q2, R1, ⟨H2, S2, M2, Y2⟩, ?_, ?_, heap2.trans heap, ?_⟩, ?_⟩ + · rw [rel2, tail, List.length_map] + rw [q, List.length_cons, List.length_map] at len + scalar_tac + · rw [loopEq] + unfold awaken + exact wl_stepSc (by rw [read, ready, the_someA]) (wakeReleaseStepA q sw) + · intro u hu + rw [rel2, tail] at hu + exact sub u (by rw [q]; exact List.mem_cons_of_mem _ hu) + · scalar_tac + · exact ⟨σ, Z.Q, Z.R, H, releaseLenA H.sched.toSchedSRN, rfl, rfl, fun _ h => h⟩ + +/-- [J] The FL4 timer-fire leaf delivers the notification outcomes, +records the updated timer queue, and re-establishes the local scheduler +view. The release walk then runs once on that result. -/ +theorem awaken_refinesA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {a : A} {σ : State Unit} + (fire : timer.fire st ar s a ⦃ r => ∃ σ1 Q R, + fl4TimerFires σ σ1 ∧ YieldQueueRelA (st := st) {Z with Q := Q, R := R} m κ r.1 σ1 ∧ IdleThreadsA m σ1 ⦄) : + sched.awaken st ar s a ⦃ r => ∃ σ1 τ Q' R', + fl4TimerFires σ σ1 ∧ awaken (Z := Unit) σ1 = ({((), τ)}, False) ∧ + YieldQueueRelA (st := st) {Z with Q := Q', R := R'} m κ r.1 τ ⦄ := by + unfold sched.awaken + refine WP.spec_bind fire ?_ + rintro ⟨s1, a1⟩ ⟨σ1, Q, R, fires, H1, idle⟩ + refine WP.spec_bind (awaken_loop_refinesA H1 idle) ?_ + rintro s2 ⟨τ, Q', R', waking, H2, _, _⟩ + rw [WP.spec_ok] + exact ⟨σ1, τ, Q', R', fires, waking, H2⟩ + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScBindCoreA.lean b/proofs/Fl4ProofsA64/P3/ScBindCoreA.lean new file mode 100644 index 000000000..690553065 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScBindCoreA.lean @@ -0,0 +1,29 @@ +/- +P3 (AArch64): the affinity effect of the cross-core thread Bind branch. +FL4's `bind_tcb` migrates the TCB to its new SC's core. The x86 +`ScBind.bind_tcb_refines` handles a single core, where migration is a no-op. +The AArch64 proof needs the changed affinity in the multicore view. +-/ +import Fl4ProofsA64.P3.ScSwitchA +import Fl4ProofsA64.P1.ScAff +open Aeneas Aeneas.Std + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +/-- A successful binding that moves a TCB to a different core cannot keep the +same `m.tcbAffinity`. The new SC binding and `ScAff` describe the migration +post-state, with the SC's core still fixed by `κ`. -/ +theorem sc_bind_migration_no_same_viewA {S : Type} {st : machine.Store S} [L : LawfulStore st] + {Z Z' : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s s' : S} + {σ σ' : Fl4SpecA64.State Unit} (H : SchedSRA st Z m κ s σ) + {sc t : U64} (ht : t ∈ Z.T) (ht' : t ∈ Z'.T) (hc : sc ∈ Z.C) (hc' : sc ∈ Z'.C) + (hne : L.mem s (affA t) ≠ L.mem s (scCoreA sc)) (h0 : sc ≠ 0#u64) + (hbind : L.mem s' (scA t) = sc) (ha : ScAff st Z'.T s') : + ¬ SchedSRA st Z' m κ s' σ' := by + intro H' + have hm := ha.core_eq ht' (by rw [hbind]; exact h0) + rw [hbind, H.aff_eq H' ht ht', H'.scCore sc hc', ← H.scCore sc hc] at hm + exact hne hm + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScBindExecA.lean b/proofs/Fl4ProofsA64/P3/ScBindExecA.lean new file mode 100644 index 000000000..019a89715 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScBindExecA.lean @@ -0,0 +1,126 @@ +/- +P3 (AArch64): the resume and ready-queue steps of local thread Bind. +A runnable target uses the existing resume refinement. For a non-runnable +target the decoder's release check restores the released-IPC-queues invariant. +-/ +import Fl4ProofsA64.P3.ScBindUnblockA +import Fl4ProofsA64.P3.ScTailA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4SpecA64 + +noncomputable section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +/-- Resume the newly bound local thread, including the decoder-checked blocked case. -/ +theorem bind_resumeA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + {sc t : U64} (H : SchedSRB st Z m κ s σ (specPtr t)) (a : A) + (hmb : sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hc : sc ∈ Z.C) (ht : t ∈ Z.T) (h0 : sc ≠ 0#u64) (hon : L.mem s (affA t) = L.cpu s) + (hcpu : L.mem s (scCoreA sc) = L.cpu s) (hni : NotIdleScA m (scNameA m sc)) + {x : Tcb} {y : SchedContext} {n : Nat} + (hkt : σ.kheap (specPtr t) = some (.tcb x)) (hks : σ.kheap (scNameA m sc) = some (.schedContext y n)) + (hsw : L.mem s (scA t) = sc) (hts : x.tcbSchedContext = some (scNameA m sc)) (hstc : y.scTcb = some (specPtr t)) + (hnq : ∀ d p, specPtr t ∉ σ.readyQueues d p) (hnr : specPtr t ∉ σ.releaseQueue) + (hst : StEqA Z.T (L.mem s) σ) + (hb : runnable x.tcbState = false → inClassRSc x.tcbState x.tcbFault → + scActive y = true → refillReady σ.curTime (refillHd y) = true) : + schedcontext.resume st ar s a sc ⦃ r => r.2 = a ∧ ∃ σ' R', + schedContextResume (Z := Unit) (scNameA m sc) σ = ({((), σ')}, False) ∧ σ'.kheap = σ.kheap ∧ + SchedSRA st { Z with R := R' } m κ r.1 σ' ∧ StEqA Z.T (L.mem r.1) σ' ∧ + MFrA (st := st) Z m s r.1 σ σ' ⦄ := by + have ht0 := (tcb_ne01Sc (H.core.tcbs t ht)).1 + obtain ⟨y', n', hky, hy⟩ := H.scr.sc sc hc + rw [hks] at hky; cases hky + have htw : L.mem s (scTcbA sc) = t := by + have he := hy.tcb + rw [← scTcbA_eqSc, hstc] at he + by_cases e : L.mem s (scTcbA sc) = 0#u64 + · rw [e, optRefS_zero] at he + cases he + · rw [optRefS_ne e] at he + exact specPtr_inj (Option.some.inj he).symm + have hr0 := (H.relOn ht hon).2 hnr + by_cases hrun : runnable x.tcbState = true + · refine WP.spec_mono (resume_refinesA H a hmb hc ht hon htw hsw h0 hcpu hni + (H.qBit0 ht hon hnq) hr0 hnq (fun z hz => by rw [hkt] at hz; cases hz; exact hrun) hst) ?_ + rintro ⟨s', a'⟩ ⟨ea, σ', R', hr, hk, -, -, -, -, hw, wf, H'⟩ + have w : WritesIn st s s' (SWrA (st := st) Z s) := wf.mono fun _ h => + h.elim (fun hh => H.qfoot_swr (fun u hu => by + rcases List.mem_cons.1 hu with rfl | hm + · exact ht + · exact H.core.memTR H.sch.cpuCs hm) H.sch.cpuCs ht hh) (fun e => by rw [e]; exact H.repr_swr) + exact ⟨ea, σ', R', hr, hk, H', fun u hu z hz => by rw [hw u hu]; exact hst u hu z (by rw [← hk]; exact hz), + MFrA.of_sw hk w⟩ + · have hr : runnable x.tcbState = false := (Bool.not_eq_true _) ▸ hrun + have hsa : schedulable (specPtr t) σ ≠ true := fun h => hrun ((schedulable_partsA h).1 _ hkt) + have hs0 : schedulable (specPtr t) σ = false := (Bool.not_eq_true _) ▸ hsa + have e : sched.is_schedulable st s t = ok false := by + rw [is_schedulable_eqA, schedulable_eqA H.toSchedSRN ht (H.relOn ht hon) hst, hs0] + have hf : SchedSRA st Z m κ s σ := + ⟨H.toSchedSRN, fun u z hz hzC => by + by_cases hu : u = specPtr t + · subst u + rw [hkt] at hz; cases hz + intro z p sy sn hz hp hy' ha _ + rw [hkt] at hz; cases hz + rw [hts] at hp; cases hp + rw [hks] at hy'; cases hy' + exact ⟨hb hr hzC ha, H.inv.suff hks ha hni⟩ + · exact H.rdEx u z hu hz hzC⟩ + unfold schedcontext.resume + rw [eq_of_spec_fd (obj.Sc.is_null.spec sc), bind_ok, decide_eq_false h0] + simp only [Bool.false_eq_true, ↓reduceIte] + rw [eq_of_spec_fd (obj.Sc.tcb.spec (L := L) s sc), bind_ok, htw, + eq_of_spec_fd (obj.Tcb.is_null.spec t), bind_ok, decide_eq_false ht0] + simp only [Bool.false_eq_true, ↓reduceIte, e, bind_ok, WP.spec_ok] + refine ⟨True.intro, σ, Z.R, ?_, rfl, hf, hst, MFrA.refl Z m s σ⟩ + rw [resume_spec_eqA hks hstc hkt (H.inv.ne hks) (fun h => (hsa h).elim) (hnq _ _), + if_neg (fun h => hsa h.1)] + +/-- The final enqueue and reschedule agree with the abstract conditional. -/ +theorem bind_enqueueA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRA st Z m κ s σ) {t : U64} (ht : t ∈ Z.T) (hon : L.mem s (affA t) = L.cpu s) + (hst : StEqA Z.T (L.mem s) σ) : + (do + let b ← sched.is_schedulable st s t + if b then + let s' ← sched.enqueue st s t + sched.reschedule_required st s' + else ok s) ⦃ s' => ∃ σ' Q', + (do + let b ← Fl4Spec.NonDet.gets (schedulable (Z := Unit) (specPtr t)) + Fl4Spec.NonDet.when b (do + tcbSchedAction tcbSchedEnqueue (specPtr t) + rescheduleRequired)) σ = ({((), σ')}, False) ∧ + SchedSRA st { Z with Q := Q' } m κ s' σ' ∧ MFrA (st := st) Z m s s' σ σ' ⦄ := by + rw [is_schedulable_eqA, bind_ok, schedulable_eqA H.toSchedSRN ht (H.relOn ht hon) hst] + by_cases hb : schedulable (specPtr t) σ = true + · rw [if_pos hb] + obtain ⟨hr, hnr⟩ := schedulable_partsA hb + refine WP.spec_bind (enqueue_refines_localA H t ht hon (fun _ => (H.relOn ht hon).2 hnr) hr) ?_ + rintro s1 ⟨σ1, he, hk, -, -, w, H1⟩ + have hst1 : StEqA Z.T (L.mem s1) σ1 := by + intro u hu z hz + rw [hk] at hz + rw [show L.mem s1 (stA u) = L.mem s (stA u) from ?_] + · exact hst u hu z hz + · rw [stA_eq] + exact field_qfoot H.core.tcbs + (fun q hq => (List.mem_cons.1 hq).elim (fun e => e.symm ▸ ht) + (H.core.memT (H.core.aff t ht) (H.core.prio t ht))) + (H.core.nodes _ (H.core.aff t ht)) (H.core.nodes _ H.sch.cpuCs) ht (w.mono fun _ h => h.toQFoot) + hu (by simp) (by simp) (by simp) (by simp) + refine WP.spec_mono (reschedule_required_refinesA H1 hst1) ?_ + rintro s2 ⟨σ2, Q', hres, hk2, -, w2, H2⟩ + refine ⟨σ2, Q', ?_, H2, (MFrA.of_sw hk (w.mono fun _ h => H.toSchedSRN.enq_swr ht h)).trans + (MFrA.of_sw hk2 w2) rfl rfl⟩ + rw [nd_gets_bindSc, Fl4Spec.NonDet.when, if_pos hb, nd_bind_oneSc he, hres] + · rw [if_neg hb, WP.spec_ok] + refine ⟨σ, Z.Q, ?_, H, MFrA.refl Z m s σ⟩ + rw [nd_gets_bindSc, Fl4Spec.NonDet.when, if_neg hb]; rfl + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScBindLinkA.lean b/proofs/Fl4ProofsA64/P3/ScBindLinkA.lean new file mode 100644 index 000000000..0132a68e7 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScBindLinkA.lean @@ -0,0 +1,114 @@ +/- +P3 (AArch64): the two thread-Bind link writes. Bind writes the TCB first, +whereas donation writes the SC first. Both words are aligned and disjoint, +so the existing donation relation and frame apply. +-/ +import Fl4ProofsA64.P3.ScBindStallA +import Fl4ProofsA64.P3.ScWakeDonA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4SpecA64 + +noncomputable section + +section Spec +/-- Bind installs the same abstract links as donation when the SC is unbound. -/ +theorem bind_links_specA {σ : State Unit} {p u : ObjRef} {xs : SchedContext} {ns : Nat} {xt : Tcb} + (hks : σ.kheap p = some (.schedContext xs ns)) (hkt : σ.kheap u = some (.tcb xt)) : + (do + setTcbObjRef (Z := Unit) tcbSchedContextUpdate u (some p) + setScObjRef scTcbUpdate p (some u)) σ = ({((), donStA σ p u xs ns xt)}, False) := by + have hne : p ≠ u := fun e => by rw [e, hkt] at hks; cases hks + have h1 : (updKSc σ u (.tcb { xt with tcbSchedContext := some p })).kheap p = + some (.schedContext xs ns) := by rw [updKSc_kheap, Function.update_of_ne hne]; exact hks + change (threadSet (Z := Unit) _ u >>= fun _ => updateSchedContext p _) σ = _ + rw [nd_bind_oneSc (threadSet_eqSc _ hkt)] + change updateSchedContext (Z := Unit) p (fun x => { x with scTcb := some u }) + (updKSc σ u (.tcb { xt with tcbSchedContext := some p })) = _ + rw [updateSchedContext_eqA h1] + congr 2 + apply congrArg (fun st : State Unit => ((), st)) + change { σ with kheap := Function.update (Function.update σ.kheap u _) p _ } = + { σ with kheap := Function.update (Function.update σ.kheap p _) u _ } + rw [Function.update_comm (Ne.symm hne)] + +end Spec + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +local notation "wa" => core.num.U64.wrapping_add + +/-- Memory-equivalent representations of a scheduler state differ in no +aligned field the scheduler relation reads. -/ +theorem SchedSRN.sameWordsA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s s' : S} {σ : State Unit} + (H : SchedSRN st Z m κ s σ) (w : WritesIn st s s' (fun _ => False)) : + SchedSRN st Z m κ s' σ := by + have tw : ∀ t ∈ Z.T, ∀ k : U64, k.val < 4096 → k.val % 8 = 0 → + L.mem s' (wa t k) = L.mem s (wa t k) := + fun t ht k hk ha => w.mem_eq (field_aligned (H.core.tcbs t ht) hk ha) id + have nw : ∀ c ∈ Z.Cs, ∀ k : U64, k.val < 4288 → k.val % 8 = 0 → + L.mem s' (L.nodeAt s c k) = L.mem s (L.nodeAt s c k) := + fun c hc k hk ha => w.mem_eq (nodeAt_aligned (H.core.nodes c hc) hk ha) id + have hcore := H.core.agree w.1 (fun t ht => ⟨tw t ht 3480#u64 (by simp) (by simp), + tw t ht 3488#u64 (by simp) (by simp)⟩) + (fun c hc k hk ha _ => nw c hc k hk ha) (fun t ht => tw t ht 3592#u64 (by simp) (by simp)) + (fun t ht => tw t ht 3520#u64 (by simp) (by simp)) (fun t ht => tw t ht 3448#u64 (by simp) (by simp)) + have gw : ∀ K : U64, (K = 8#u64 ∨ K = 16#u64) → L.mem s' (wa (L.ksb s) K) = L.mem s (wa (L.ksb s) K) := by + intro K hK + have ha := H.ksb_al + have hf := H.ksFit + unfold ksSizeA at hf + apply w.mem_eq _ id + unfold Aligned + rcases hK with rfl | rfl <;> rw [wa_valm] <;> simp <;> omega + exact H.rebuildG (Q' := Z.Q) (R' := Z.R) w.1 w.2.1 + (fun c hc k hk ha _ _ => nw c hc k (by omega) ha) gw + (fun t ht k hk => by rcases hk with rfl | rfl | rfl <;> exact tw t ht _ (by simp) (by simp)) + (H.scr.frame (fun _ ha _ => w.mem_eq ha id) (fun _ _ => rfl)) H.scp + (fun _ _ x hx => ⟨x, hx, rfl, rfl⟩) + (fun t ht => by rw [show scA t = wa t 3464#u64 from rfl, tw t ht _ (by simp) (by simp)]; exact H.sch.tcb t ht) + (fun t ht => by rw [show scA t = wa t 3464#u64 from rfl, tw t ht _ (by simp) (by simp)]; exact H.rest.scMem t ht) + ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ + (by rw [w.mem_eq H.reprA.1 id]; exact H.rest.repr) + (by rw [nw _ H.sch.cpuCs layout.node.SCHED_ACTION (by simp [layout.node.SCHED_ACTION]) + (by simp [layout.node.SCHED_ACTION])]; exact H.sch.act) + H.actOn H.inv hcore H.rest.rq H.rest.rqDom H.rest.rel H.run + (fun _ hc => w.mem_eq (H.scCoreFoot hc).1 id) + +/-- The bound pair and scheduler relation immediately after Bind's writes. -/ +theorem bind_linksA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRA st Z m κ s σ) (hst : StEqA Z.T (L.mem s) σ) + {sc t : U64} (hc : sc ∈ Z.C) (ht : t ∈ Z.T) (h0 : sc ≠ 0#u64) + (hsc0 : L.mem s (scTcbA sc) = 0#u64) (ht0 : L.mem s (scA t) = 0#u64) + (hni : NotIdleScA m (scNameA m sc)) + {xs : SchedContext} {ns : Nat} {xt : Tcb} + (hks : σ.kheap (scNameA m sc) = some (.schedContext xs ns)) (hkt : σ.kheap (specPtr t) = some (.tcb xt)) : + let s' := L.wr (L.wr s (scA t) sc) (scTcbA sc) t + let σ' := donStA σ (scNameA m sc) (specPtr t) xs ns xt + SchedSRB st Z m κ s' σ' (specPtr t) ∧ StEqA Z.T (L.mem s') σ' ∧ + L.mem s' (scA t) = sc ∧ L.mem s' (scTcbA sc) = t ∧ MFrA (st := st) Z m s s' σ σ' := by + dsimp only + obtain ⟨ha, hb, hne⟩ := H.donAddr hc ht + have H1 := H.donateB hc ht h0 hsc0 ht0 hni hks hkt + have W := wr_swap (st := st) (s := s) (v := sc) (w := t) hb ha hne + have wf := ((writesIn_wr (st := st) (s := s) (v := sc) hb).trans (writesIn_wr (v := t) ha)).mono + (fun _ h => Or.symm h) + obtain ⟨-, -, tk, -⟩ := H.donW hc ht wf + have hNT : scNameA m sc ≠ specPtr t := fun e => by rw [e, hkt] at hks; cases hks + refine ⟨⟨H1.toSchedSRN.sameWordsA W, H1.rdEx⟩, ?_, + by rw [L.mem_wr_ne ha hb (Ne.symm hne), L.mem_wr_same hb], L.mem_wr_same ha, ⟨wf.mono (fun _ h => H.mwr_two hc ht h), + (SKeepA.two (donStA_kheap hNT) hks hkt).1, (SKeepA.two (donStA_kheap hNT) hks hkt).2⟩⟩ + intro u hu x hx + rw [stA_eq, tk u hu 3328#u64 (by simp) (by simp) (fun _ => by simp), ← stA_eq] + rw [donStA_kheap hNT] at hx + split_ifs at hx with e1 e2 + · cases hx; exact hst u hu xt (by rw [e1]; exact hkt) + · cases hx + · exact hst u hu x hx + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScBindStallA.lean b/proofs/Fl4ProofsA64/P3/ScBindStallA.lean new file mode 100644 index 000000000..1429621ce --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScBindStallA.lean @@ -0,0 +1,87 @@ +/- +P3 (AArch64): the same-core thread Bind still has an AArch64 VCPU stall +branch in the decoder before restart. In the binding operation itself, +migration is a no-op under the same-core scope (x86 ScBind.bind_tcb_refines, +seL4 src/object/schedcontext.c:320 at c98d70ad). + +The branch equations below expose the architectural call and IPI update. +The read equalities select concrete cases and introduce no named premises. +-/ +import Fl4ProofsA64.P3.ScSWrA +import Fl4ProofsA64.P3.ScSporA +import Fl4ProofsA64.P1.QueueDecSc +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +/-- [S] A migration to the TCB's existing core is the identity. -/ +theorem sc_migrate_same_coreA {S A : Type} {st : machine.Store S} {ar : machine.Arch A} + {s : S} {a : A} {t cpu : U64} (haff : obj.Tcb.affinity st t s = ok cpu) : + smp.migrate_tcb st ar s a t cpu = ok (s, a) := by + unfold smp.migrate_tcb + simp only [haff, bind_ok, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + +/-- A TCB with local affinity can have a bound VCPU whose RUN_CALLER is +current on a remote core. This branch stalls the remote core and updates +its pending reschedule bit. -/ +theorem sc_stall_local_guest_eqA {S A : Type} {st : machine.Store S} {ar : machine.Arch A} + {s : S} {a : A} {t cpu v caller remote : U64} + (haff : obj.Tcb.affinity st t s = ok cpu) (hcpu : st.cur_cpu s = ok cpu) + (hv : vcpu.bound st s t = ok v) (hv0 : v ≠ 0#u64) + (hcaller : st.read s (core.num.U64.wrapping_add v vcpu.RUN_CALLER) = ok caller) + (hcaller0 : caller ≠ 0#u64) (hcaff : obj.Tcb.affinity st caller s = ok remote) + (hremote : remote ≠ cpu) + (hrun : obj.node_get st s remote layout.node.CUR_THREAD = ok caller) : + smp.remote_tcb_stall st ar s a t = (do + let a' ← ar.remote_stall a remote + let s' ← smp.add_pending st s remote + ok (s', a')) := by + unfold smp.remote_tcb_stall smp.remote_reschedule + simp only [haff, hcpu, hv, hcaller, hcaff, hrun, lift, bind_ok, uncurry_apply_pair, + bne_iff_ne, ne_eq, not_true_eq_false, not_false_eq_true, hv0, hcaller0, hremote, ↓reduceIte] + +/-- The successful Bind decoder arm for a thread that is neither bound +nor blocked. The target stall precedes restart and the binding operation. -/ +theorem sc_decode_bind_thread_unblocked_eqA {S A : Type} + {st : machine.Store S} {ar : machine.Arch A} {s : S} {a : A} {sc t ct : U64} + {call : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hs : obj.Sc.tcb st sc s = ok 0#u64) (hc : sched.cur_thread st s = ok ct) + (he : schedcontext.extra_cap st s extra 0#usize = ok (some (.Thread t))) + (ht : obj.Tcb.sc st t s = ok 0#u64) + (hb : sched.is_blocked st s t = ok false) : + schedcontext.decode st ar s a fl4_abi.label.SchedContextBind sc call extra = (do + let (s1, a1) ← smp.remote_tcb_stall st ar s a t + let s2 ← sched.set_thread_state st s1 ct sched.TS_RESTART + let (s3, a2) ← schedcontext.bind_tcb st ar s2 a1 sc t + ok (core.result.Result.Ok (), s3, a2)) := by + unfold schedcontext.decode + simp (config := { decide := true }) only [fl4_abi.label.SchedContextBind, + fl4_abi.label.SchedContextConsumed, hs, hc, he, ht, hb, bind_ok, + uncurry_apply_pair, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + +/-- A witnessed architecture change at the guest's remote RUN_CALLER +rules out reducing the same-core decoder's stall to identity. -/ +theorem sc_stall_local_guest_neA {S A : Type} {st : machine.Store S} {ar : machine.Arch A} + {s : S} {a a' : A} {t remote : U64} + (hstall : smp.remote_tcb_stall st ar s a t = (do + let a1 ← ar.remote_stall a remote + let s1 ← smp.add_pending st s remote + ok (s1, a1))) + (ha : ar.remote_stall a remote = ok a') (hne : a' ≠ a) : + smp.remote_tcb_stall st ar s a t ≠ ok (s, a) := by + rw [hstall, ha, bind_ok] + intro h + obtain ⟨s', _, hp⟩ := bind_eq_ok h + exact hne (congrArg Prod.snd (ok_inj_ff hp)) + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScBindTcbA.lean b/proofs/Fl4ProofsA64/P3/ScBindTcbA.lean new file mode 100644 index 000000000..e0c386eff --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScBindTcbA.lean @@ -0,0 +1,193 @@ +/- +P3 (AArch64): execution of thread Bind on the current core. +[S] Both the TCB and the SC are local. This is the same-core branch of +seL4 c98d70ad, src/object/schedcontext.c:320. +[J] At the decoder entry the ready-queue exclusion is supplied by BoundInv.q. +-/ +import Fl4ProofsA64.P3.ScBindExecA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4SpecA64 + +noncomputable section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +local notation "wa" => core.num.U64.wrapping_add + +private theorem bind_unblock_scA {m : McState} {σ σ' : State Unit} (H : ScInvA m σ) + {p : ObjRef} {y : SchedContext} {n : Nat} (hk : σ.kheap p = some (.schedContext y n)) + (he : ifSporadicActiveCurScTestRefillUnblockCheck (Z := Unit) (some p) σ = ({((), σ')}, False)) : + ∃ z, σ'.kheap p = some (.schedContext z n) ∧ z.scTcb = y.scTcb ∧ scActive z = scActive y := by + rw [bind_unblock_guardA hk] at he + split_ifs at he with hc + · have hm : ((), σ') ∈ (refillUnblockCheck (Z := Unit) p σ).1 := by rw [he]; rfl + rcases unblock_resultA hk (H.ne hk hc.2.1) hm with rfl | ⟨z, e, -, -, hz, -, -⟩ + · exact ⟨y, hk, rfl, rfl⟩ + · refine ⟨z, by rw [e, updKSc_kheap, Function.update_self], ?_, ?_⟩ + · rw [hz] + · rw [hz] + · have e := congrArg Prod.fst he + have es := (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 e)).2 + subst σ' + exact ⟨y, hk, rfl, rfl⟩ + +private theorem result_bind_assocA {α β γ : Type} (r : Result α) + (f : α → Result β) (g : β → Result γ) : + Std.bind (Std.bind r f) g = Std.bind r (fun x => Std.bind (f x) g) := by + cases r with + | ret x => simp only [bind_ok] + | vis i k => simp only [bind_vis]; congr 1; funext y; cases i; exact PEmpty.elim y + | div => simp only [bind_div] + +private theorem bind_bodyA {s : S} {a : A} {sc t : U64} : + schedcontext.bind_tcb st ar s a sc t = (do + let s1 ← obj.Tcb.set_sc st t s sc + let s2 ← obj.Sc.set_tcb st sc s1 t + let c ← obj.Sc.cpu st sc s2 + let (s3, a1) ← smp.migrate_tcb st ar s2 a t c + let s4 ← bindUnblockA st s3 sc + let (s5, a2) ← schedcontext.resume st ar s4 a1 sc + let b ← sched.is_schedulable st s5 t + if b then + let s6 ← sched.enqueue st s5 t + let s7 ← sched.reschedule_required st s6 + ok (s7, a2) + else ok (s5, a2)) := by + unfold schedcontext.bind_tcb bindUnblockA + simp only [result_bind_assocA] + +/-- Local thread Bind after decoding and setting the caller to Restart. +[S] Both the target affinity and SC core equal the current core. +[J] The caller supplies ready-queue exclusion for a runnable unbound TCB +from `BoundInv.q`. The blocked-target refill condition is checked by decode. -/ +theorem sc_bind_tcb_localA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRA st Z m κ s σ) (hst : StEqA Z.T (L.mem s) σ) + (a : A) (hmb : sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + {t sc : U64} (ht : t ∈ Z.T) (hc : sc ∈ Z.C) (h0 : sc ≠ 0#u64) + (ht0 : L.mem s (scA t) = 0#u64) (hs0 : L.mem s (scTcbA sc) = 0#u64) + (hon : L.mem s (affA t) = L.cpu s) (hcpu : L.mem s (scCoreA sc) = L.cpu s) + (hni : NotIdleScA m (scNameA m sc)) + {xt : Tcb} {xs : SchedContext} {n : Nat} + (hkt : σ.kheap (specPtr t) = some (.tcb xt)) (hks : σ.kheap (scNameA m sc) = some (.schedContext xs n)) + (hnq : runnable xt.tcbState = true → ∀ d p, specPtr t ∉ σ.readyQueues d p) + (hb : runnable xt.tcbState = false → inClassRSc xt.tcbState xt.tcbFault → + scActive xs = true → refillReady σ.curTime (refillHd xs) = true) : + schedcontext.bind_tcb st ar s a sc t ⦃ r => r.2 = a ∧ ∃ σ' Q' R', + schedContextBindTcb (Z := Unit) (scNameA m sc) (specPtr t) σ = ({((), σ')}, False) ∧ + SchedSRA st { Z with Q := Q', R := R' } m κ r.1 σ' ∧ MFrA (st := st) Z m s r.1 σ σ' ⦄ := by + let p := scNameA m sc + let u := specPtr t + let s2 := L.wr (L.wr s (scA t) sc) (scTcbA sc) t + let σ2 := donStA σ p u xs n xt + let y : SchedContext := { xs with scTcb := some u } + let x : Tcb := { xt with tcbSchedContext := some p } + have hne : p ≠ u := by + intro e + change σ.kheap p = _ at hks + rw [e, hkt] at hks + cases hks + have hkt2 : σ2.kheap u = some (.tcb x) := by rw [donStA_kheap hne, if_pos rfl] + have hks2 : σ2.kheap p = some (.schedContext y n) := by rw [donStA_kheap hne, if_neg hne, if_pos rfl] + obtain ⟨H2, hst2, hlink, -, F2⟩ := bind_linksA H hst hc ht h0 hs0 ht0 hni hks hkt + have H2' : SchedSRB st Z m κ s2 σ2 u := H2 + have hon2 : L.mem s2 (affA t) = L.cpu s2 := by + have h := H.sch.aff t ht + have h' := H2'.sch.aff t ht + rw [h] at h' + have e := UScalar.eq_of_val_eq (Option.some.inj h').symm + have ec := UScalar.eq_of_val_eq (H2'.sch.core.symm.trans H.sch.core) + exact e.trans (hon.trans ec.symm) + have hc2 : L.mem s2 (scCoreA sc) = L.cpu s2 := by + rw [H2'.scCore sc hc, ← H.scCore sc hc, hcpu] + exact UScalar.eq_of_val_eq (H.sch.core.symm.trans H2'.sch.core) + have hnq2 : ∀ d q, u ∉ σ2.readyQueues d q := by + intro d q h + obtain ⟨z, hz, hr⟩ := H.run.1 d q u h + rw [hkt] at hz; cases hz + exact hnq hr d q h + have hnr2 : u ∉ σ2.releaseQueue := by + intro h + obtain ⟨z, p', _, _, hz, hp, -, -⟩ := H.inv.relSc u h + obtain ⟨x', hx', hopt⟩ := H.sch.tcb t ht + rw [hkt] at hx'; cases hx' + rw [hkt] at hz; cases hz + rw [hopt, ht0, optScA, if_pos rfl] at hp + cases hp + rw [bind_bodyA] + refine WP.spec_bind (set_sc_specSc s t sc) ?_ + rintro s1 rfl + refine WP.spec_bind (set_tcb_specSc _ sc t) ?_ + rintro s₂ rfl + refine WP.spec_bind (obj.Sc.cpu.spec s2 sc) ?_ + rintro c rfl + have e : L.mem s2 (scCoreA sc) = L.mem s2 (affA t) := + hc2.trans hon2.symm + rw [e, sc_migrate_same_coreA (eq_of_spec_fd (obj.Tcb.affinity.spec s2 t)), bind_ok] + refine WP.spec_bind (bind_unblockA H2' hc h0 hnr2 hni hks2 hkt2 rfl rfl) ?_ + rintro s4 ⟨σ4, he4, H4, F4, hread⟩ + obtain ⟨-, -, k⟩ := H2'.keepW F4.wr (o0 := layout.node.REPROGRAM) + (by simp [layout.node.REPROGRAM]) (fun _ h => h) + have hon4 : L.mem s4 (affA t) = L.cpu s4 := by + rw [F4.wr.2.1, show affA t = wa t 3520#u64 from rfl, k t ht 3520#u64 (by simp) (by simp)] + exact hon2 + have hst4 : StEqA Z.T (L.mem s4) σ4 := by + intro b hb z hz + obtain ⟨z0, hz0, -⟩ := H2'.sch.tcb b hb + rw [F4.obj _ _ hz0 (fun _ _ h => by cases h)] at hz + cases hz + rw [stA_eq, k b hb 3328#u64 (by simp) (by simp), ← stA_eq] + exact hst2 b hb _ hz0 + have k4 := F4.obj u (.tcb x) hkt2 (fun _ _ h => by cases h) + have hc4 : L.mem s4 (scCoreA sc) = L.cpu s4 := by + rw [H4.scCore sc hc, ← H2'.scCore sc hc, hc2, F4.wr.2.1] + have ht4 : L.mem s4 (scA t) = sc := by + change L.mem s4 (wa t 3464#u64) = sc + rw [k t ht 3464#u64 (by simp) (by simp)] + exact hlink + obtain ⟨z, hz, hzt, hza⟩ := bind_unblock_scA H2'.inv hks2 he4 + refine WP.spec_bind (bind_resumeA H4 a hmb hc ht h0 hon4 hc4 hni k4 hz ht4 rfl hzt + (by rw [F4.rq]; exact hnq2) (by rw [F4.rel]; exact hnr2) hst4 + (fun hr hC ha => hread (hb hr hC (hza ▸ ha)) _ _ hz)) ?_ + rintro ⟨s5, a2⟩ ⟨ea, σ5, R', he5, -, H5, hst5, F5⟩ + change a2 = a at ea + subst a2 + have hon5 : L.mem s5 (affA t) = L.cpu s5 := by + have h' := H5.sch.aff t ht + rw [H4.sch.aff t ht] at h' + exact (UScalar.eq_of_val_eq (Option.some.inj h').symm).trans + (hon4.trans (UScalar.eq_of_val_eq (H4.sch.core.symm.trans H5.sch.core))) + have enq := bind_enqueueA H5 ht hon5 hst5 + have hpost : ∀ σ', (do + let b ← Fl4Spec.NonDet.gets (schedulable (Z := Unit) u) + Fl4Spec.NonDet.when b (do + tcbSchedAction tcbSchedEnqueue u + rescheduleRequired)) σ5 = ({((), σ')}, False) → + schedContextBindTcb (Z := Unit) p u σ = ({((), σ')}, False) := by + intro σ' he + unfold schedContextBindTcb + rw [← nd_bind_assocSc, nd_bind_oneSc (bind_links_specA hks hkt), + nd_bind_oneSc he4, nd_bind_oneSc he5] + exact he + change (do + let b ← sched.is_schedulable st s5 t + if b then + let s6 ← sched.enqueue st s5 t + let s7 ← sched.reschedule_required st s6 + ok (s7, a) + else ok (s5, a)) ⦃ _ ⦄ + by_cases hs : schedulable u σ5 = true + · rw [is_schedulable_eqA, bind_ok, schedulable_eqA H5.toSchedSRN ht (H5.relOn ht hon5) hst5, if_pos hs] + rw [is_schedulable_eqA, bind_ok, schedulable_eqA H5.toSchedSRN ht (H5.relOn ht hon5) hst5, if_pos hs] at enq + rw [← result_bind_assocA] + refine WP.spec_bind enq ?_ + rintro s7 ⟨σ', Q', he, HF, F7⟩ + rw [WP.spec_ok] + refine ⟨rfl, σ', Q', R', hpost σ' he, HF, ?_⟩ + exact (((F2.trans (F4.mfr H2'.toSchedSRN) rfl rfl).trans F5 rfl rfl).trans F7 rfl rfl) + · rw [is_schedulable_eqA, bind_ok, schedulable_eqA H5.toSchedSRN ht (H5.relOn ht hon5) hst5, if_neg hs, WP.spec_ok] + refine ⟨rfl, σ5, Z.Q, R', hpost σ5 ?_, H5, (F2.trans (F4.mfr H2'.toSchedSRN) rfl rfl).trans F5 rfl rfl⟩ + rw [nd_gets_bindSc, Fl4Spec.NonDet.when, if_neg hs]; rfl + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScBindTopA.lean b/proofs/Fl4ProofsA64/P3/ScBindTopA.lean new file mode 100644 index 000000000..ee1941eda --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScBindTopA.lean @@ -0,0 +1,340 @@ +/- +P3 (AArch64): the successful local thread Bind decoder and invocation. +[S] Target TCB and SC on the current core, with the initial target stall +covered by the no-op stall contract. The same-core migration corresponds +to seL4 c98d70ad, src/object/schedcontext.c:320. +[J] A runnable unbound target is absent from every ready queue. The +Stage-3 join supplies the exclusion from JEntA.sys.ent.bxi.2.1 (BoundInv.q). +The notification capability arm is sc_decode_ntfn_invokeA specialized to Bind. +-/ +import Fl4ProofsA64.P3.ScBindTcbA +import Fl4ProofsA64.P3.ScInvokeTopA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4SpecA64 Fl4Spec + +noncomputable section + +set_option maxHeartbeats 400000 + +private theorem bind_getReleasedA {σ : State Unit} {p : ObjRef} {x : SchedContext} {n : Nat} + (hk : σ.kheap p = some (.schedContext x n)) (hne : scActive x = true → x.scRefills ≠ []) : + getScReleased (Z := Unit) p σ = ({(scReleased σ.curTime x, σ)}, False) := by + have hr : scActive x = true → readScRefillReady (Z := Unit) p σ = some (refillReady σ.curTime (refillHd x)) := by + intro ha + show Option.bind (readRefillHead p σ) (fun h => Option.bind (Lookup.asks (fun s : State Unit => s.curTime) σ) + (fun ct => Lookup.oreturn (refillReady ct h) σ)) = _ + rw [readRefillHead_eqA hk (hne ha)]; rfl + have ha : readScActive (Z := Unit) p σ = some (scActive x) := by + show Option.bind (readSchedContext p σ) (fun sc => Lookup.oreturn (scActive sc) σ) = _ + rw [readSchedContext_eqA hk]; rfl + have e : readScReleased (Z := Unit) p σ = some (scReleased σ.curTime x) := by + show Option.bind (readScActive p σ) (fun a => (if a then readScRefillReady p else Lookup.oreturn false) σ) = _ + rw [ha] + cases h : scActive x + · simp [scReleased, h, Lookup.oreturn] + · simp [scReleased, h, hr h] + simp [getScReleased, NonDet.getsThe, e, NonDet.assertOpt]; rfl + +/-- The spec decode of Bind of an unbound thread to an unbound SC. -/ +theorem decodeBind_threadA {σ : State Unit} {p u : ObjRef} {x : Tcb} {y : SchedContext} {n : Nat} + (ht : σ.kheap u = some (.tcb x)) (hs : σ.kheap p = some (.schedContext y n)) + (ht0 : x.tcbSchedContext = none) (hs0 : y.scTcb = none) + (hn : scActive y = true → y.scRefills ≠ []) + (hb : ipcQueuedThreadState x.tcbState = true → scReleased σ.curTime y = true) + (rest : List Cap) : + (decodeSchedContextBind (Z := Unit) p (.threadCap u :: rest)).run σ = + ({(.ok (.invokeSchedContextBind p (.threadCap u)), σ)}, False) := by + unfold decodeSchedContextBind + simp only [List.length_cons, Nat.succ_ne_zero, NonDetE.whenE, ↓reduceIte, NonDetE.returnOk, ndE_pure_bindA, + List.head?_cons, the_someA] + rw [run_bind_okA (run_liftEA (getSchedContext_eqA hs))] + simp only [hs0, ne_eq, not_true_eq_false, NonDetE.whenE, ↓reduceIte, NonDetE.returnOk, ndE_pure_bindA] + unfold getTcbObjRef getThreadState + rw [run_bind_okA (run_liftEA (threadGet_eqSc Tcb.tcbSchedContext ht))] + simp only [ht0, ne_eq, not_true_eq_false, NonDetE.whenE, ↓reduceIte, NonDetE.returnOk, ndE_pure_bindA] + rw [run_bind_okA (run_liftEA (threadGet_eqSc Tcb.tcbState ht)), run_bind_okA (run_liftEA (bind_getReleasedA hs hn))] + have hno : ¬ (ipcQueuedThreadState x.tcbState = true ∧ ¬ scReleased σ.curTime y = true) := + fun h => h.2 (hb h.1) + rw [if_neg hno, ndE_pure_bindA] + rfl + +private theorem bind_restart_eqA {σ : State Unit} {p : ObjRef} {x : Tcb} + (hk : σ.kheap p = some (.tcb x)) : + setThreadState (Z := Unit) p .restart σ = + ({((), stsActA (updKSc σ p (.tcb { x with tcbState := .restart })) p)}, False) := by + unfold setThreadState + rw [nd_bind_oneSc (threadSet_eqSc _ hk)] + unfold setThreadStateAct + repeat rw [nd_gets_bindSc] + unfold stsActA NonDet.when + split <;> rfl + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +theorem bind_isBlockedA {s : S} {t : U64} {ts : ThreadState} + (hst : (L.mem s (stA t)).val = tsCodeA ts) : + sched.is_blocked st s t = ok (ipcQueuedThreadState ts) := by + unfold sched.is_blocked + rw [eq_of_spec_fd (obj.Tcb.state.spec s t), bind_ok] + have e : ∀ k : U64, L.mem s (stA t) = k ↔ tsCodeA ts = k.val := + fun k => ⟨fun h => h ▸ hst.symm, fun h => UScalar.eq_of_val_eq (hst.trans h)⟩ + have e5 : sched.TS_BLOCKED_ON_REPLY = 5#u64 := by unfold sched.TS_BLOCKED_ON_REPLY; rfl + obtain ⟨-, -, h3, h4, h6⟩ := ts_vals + simp only [e, h3, h4, h6, e5] + cases ts <;> simp [tsCodeA, ipcQueuedThreadState] + +theorem bind_scReleasedA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRN st Z m κ s σ) {sc : U64} (hc : sc ∈ Z.C) {x : SchedContext} {n : Nat} + (hk : σ.kheap (scNameA m sc) = some (.schedContext x n)) : + sporadic.sc_released st s sc = ok (scReleased σ.curTime x) := by + obtain ⟨y, k, hy, hx⟩ := H.scr.sc sc hc + rw [hk] at hy; cases hy + have ha : sporadic.sc_active st s sc = ok (scActive x) := by + unfold sporadic.sc_active + rw [eq_of_spec_fd (obj.Sc.refill_max.spec (L := L) s sc), bind_ok] + congr 1 + apply decide_eq_decide.2 + rw [gt_iff_valSc, hx.max] + rfl + unfold sporadic.sc_released + rw [ha, bind_ok] + by_cases h : scActive x = true + · rw [if_pos h, scReleased, h, Bool.true_and] + have hReady := getScRefillReady_eqA hk (H.inv.ne hk h) + exact eq_of_spec_fd (WP.spec_mono (ready_refinesA H.scr H.sch.time hc hk h) fun b hb => + (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst (hReady.symm.trans hb)))).1.symm) + · have h0 : scActive x = false := (Bool.not_eq_true _) ▸ h + rw [if_neg h, scReleased, h0, Bool.false_and] + +theorem bind_decode_bodyA {s : S} {a : A} {sc ct t : U64} + {call : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} {b : Bool} + (hs : obj.Sc.tcb st sc s = ok 0#u64) (hc : sched.cur_thread st s = ok ct) + (hcap : schedcontext.extra_cap st s extra 0#usize = ok (some (.Thread t))) + (ht : obj.Tcb.sc st t s = ok 0#u64) (hb : sched.is_blocked st s t = ok b) + (hr : b = true → sporadic.sc_released st s sc = ok true) + (hstall : smp.remote_tcb_stall st ar s a t = ok (s, a)) : + schedcontext.decode st ar s a fl4_abi.label.SchedContextBind sc call extra = (do + let s1 ← sched.set_thread_state st s ct sched.TS_RESTART + let (s2, a2) ← schedcontext.bind_tcb st ar s1 a sc t + ok (core.result.Result.Ok (), s2, a2)) := by + cases b + · rw [sc_decode_bind_thread_unblocked_eqA hs hc hcap ht hb, hstall, bind_ok] + rfl + · unfold schedcontext.decode + simp (config := { decide := true }) only [fl4_abi.label.SchedContextBind, + fl4_abi.label.SchedContextConsumed, hs, hc, hcap, ht, hb, hr rfl, hstall, bind_ok, + uncurry_apply_pair, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte, Bool.not_true] + +private theorem bind_inClass_blockedA {x : Tcb} + (hr : runnable x.tcbState = false) (hc : inClassRSc x.tcbState x.tcbFault) : + ipcQueuedThreadState x.tcbState = true := by + rcases hc with h | ⟨u, h⟩ | ⟨e, d, h, -⟩ | ⟨e, r, d, h⟩ + · rw [h] at hr; cases hr + · rw [h]; rfl + · rw [h]; rfl + · rw [h]; rfl + +private theorem bind_decode_finishA {s s1 s2 : S} {a a2 : A} {sc ct t : U64} + (hR : sched.set_thread_state st s ct sched.TS_RESTART = ok s1) + (hB : schedcontext.bind_tcb st ar s1 a sc t = ok (s2, a2)) (ha : a2 = a) : + (do + let q ← sched.set_thread_state st s ct sched.TS_RESTART + let (s', a') ← schedcontext.bind_tcb st ar q a sc t + ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s', a')) = + ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s2, a) := by + rw [hR, bind_ok, hB, bind_ok] + exact congrArg (fun ax => ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s2, ax)) ha + +private theorem bind_postA {α : Type} {r : Result α} {x : α} + (he : r = ok x) {P : α → Prop} (h : P x) : r ⦃ P ⦄ := by + rw [he] + exact (WP.spec_ok x).2 h + +-- The equations above expose the concrete steps needed by the composition. +attribute [local irreducible] schedcontext.decode schedcontext.bind_tcb sched.set_thread_state + +private theorem bind_after_restartA + {Z : XSetsA} {m : McState} {κ : U64 → U64} {s s1 : S} {σ σ1 : State Unit} + (H : SchedSRA st Z m κ s σ) (H1 : SchedSRA st Z m κ s1 σ1) (hst1 : StEqA Z.T (L.mem s1) σ1) + (a : A) (hmb : sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + {sc t ct : U64} (ht : t ∈ Z.T) (hc : sc ∈ Z.C) (h0 : sc ≠ 0#u64) + {x xc : Tcb} {y : SchedContext} {n : Nat} + (hkt : σ.kheap (specPtr t) = some (.tcb x)) (hkc : σ.kheap (specPtr ct) = some (.tcb xc)) + (hk : σ.kheap (scNameA m sc) = some (.schedContext y n)) + (hxn : x.tcbSchedContext = none) (hyn : y.scTcb = none) (hr : runnable xc.tcbState = true) + (hon : L.mem s (affA t) = L.cpu s) (hcpu : L.mem s (scCoreA sc) = L.cpu s) + (hni : NotIdleScA m (scNameA m sc)) + (hnq : runnable x.tcbState = true → ∀ d p, specPtr t ∉ σ.readyQueues d p) + (hb : ipcQueuedThreadState x.tcbState = true → scReleased σ.curTime y = true) + (hR : setThreadState (Z := Unit) (specPtr ct) .restart σ = ({((), σ1)}, False)) + (hh : σ1.kheap = (updKSc σ (specPtr ct) (.tcb { xc with tcbState := .restart })).kheap) + (w1 : WritesIn st s s1 (fun b => SWrA (st := st) Z s b ∨ b = stA ct)) : + ∃ s2 σ' Q' R', schedcontext.bind_tcb st ar s1 a sc t = ok (s2, a) ∧ + invokeSchedContext (Z := Unit) (.invokeSchedContextBind (scNameA m sc) (.threadCap (specPtr t))) σ1 = + ({([], σ')}, False) ∧ SchedSRA st { Z with Q := Q', R := R' } m κ s2 σ' ∧ + WritesIn st s s2 (fun b => MWrA (st := st) Z m σ s b ∨ b = stA ct) := by + let p := scNameA m sc + let u := specPtr t + let c := specPtr ct + have e := (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 + (congrArg Prod.fst (hR.symm.trans (bind_restart_eqA hkc))))).2 + have hqr : σ1.readyQueues = σ.readyQueues := by + rw [e] + unfold stsActA + split <;> rfl + have htr : σ1.curTime = σ.curTime := by + rw [e] + unfold stsActA + split <;> rfl + have hne : p ≠ c := fun he => by rw [show scNameA m sc = specPtr ct from he, hkc] at hk; cases hk + have hks1 : σ1.kheap p = some (.schedContext y n) := by + rw [hh, updKSc_kheap, Function.update_of_ne hne] + exact hk + let x1 : Tcb := if u = c then { x with tcbState := .restart } else x + have hx1 : σ1.kheap u = some (.tcb x1) ∧ x1.tcbSchedContext = none ∧ + (runnable x1.tcbState = true → runnable x.tcbState = true) ∧ + (ipcQueuedThreadState x1.tcbState = true → ipcQueuedThreadState x.tcbState = true) := by + by_cases eu : u = c + · have hu : specPtr t = specPtr ct := eu + have eq : x = xc := KernelObject.tcb.inj + (Option.some.inj ((show σ.kheap (specPtr ct) = some (.tcb x) from hu ▸ hkt).symm.trans hkc)) + have hv : x1 = { x with tcbState := .restart } := if_pos eu + have hk' : σ1.kheap u = some (.tcb { x with tcbState := .restart }) := by + change σ1.kheap (specPtr t) = _ + rw [hu, hh, updKSc_kheap, Function.update_self, eq] + rw [hv] + exact ⟨hk', hxn, fun _ => by rwa [eq], fun h => Bool.noConfusion h⟩ + · have hv : x1 = x := if_neg eu + have hk' : σ1.kheap u = some (.tcb x) := by + rw [hh, updKSc_kheap, Function.update_of_ne eu] + exact hkt + rw [hv] + exact ⟨hk', hxn, id, id⟩ + obtain ⟨hkT, hxn1, hRun, hBlock⟩ := hx1 + have hword : L.mem s1 (scA t) = 0#u64 := by + obtain ⟨z, hz, hopt⟩ := H1.sch.tcb t ht + rw [hkT] at hz; cases hz + unfold optScA at hopt + by_contra hh' + rw [if_neg hh', hxn1] at hopt + cases hopt + have hscWord : L.mem s1 (scTcbA sc) = 0#u64 := by + obtain ⟨z, k, hz, hzR⟩ := H1.scr.sc sc hc + rw [hks1] at hz; cases hz + have he := hzR.tcb + rw [hyn, ← scTcbA_eqSc] at he + by_contra hnon + rw [optRefS_ne hnon] at he + cases he + have hnq1 : runnable x1.tcbState = true → ∀ d q, u ∉ σ1.readyQueues d q := by + intro h d q + rw [hqr] + exact hnq (hRun h) d q + have hb1 : runnable x1.tcbState = false → inClassRSc x1.tcbState x1.tcbFault → + scActive y = true → refillReady σ1.curTime (refillHd y) = true := by + intro h c' a' + have e := hb (hBlock (bind_inClass_blockedA h c')) + rw [scReleased, a', Bool.true_and] at e + rw [htr] + exact e + have hon1 : L.mem s1 (affA t) = L.cpu s1 := by rw [H.aff_eq H1 ht ht, H.cpu_eq H1]; exact hon + have hcpu1 : L.mem s1 (scCoreA sc) = L.cpu s1 := by + rw [H1.scCore sc hc, ← H.scCore sc hc, H.cpu_eq H1]; exact hcpu + have sz : SSzA σ σ1 := by + intro q z k hq + rw [hh, updKSc_kheap] at hq + by_cases eq : q = c + · rw [eq, Function.update_self] at hq + cases hq + · rw [Function.update_of_ne eq] at hq + exact ⟨z, hq⟩ + have hBind := @sc_bind_tcb_localA S A st ar L Z m κ s1 σ1 H1 hst1 a hmb t sc ht hc h0 + hword hscWord hon1 hcpu1 hni x1 y n hkT hks1 hnq1 hb1 + obtain ⟨r, hStepB, hP⟩ := spec_ex_fd hBind + rcases r with ⟨s2, a2⟩ + rcases hP with ⟨ea, σ', Q', R', hI, HF, F2⟩ + have hInvoke : invokeSchedContext (Z := Unit) (.invokeSchedContextBind p (.threadCap u)) σ1 = + ({([], σ')}, False) := by + change (do schedContextBindTcb (Z := Unit) p u; pure ([] : List Data)) σ1 = _ + rw [nd_bind_oneSc hI] + rfl + have hw : WritesIn st s s2 (fun b => MWrA (st := st) Z m σ s b ∨ b = stA ct) := by + have w : WritesIn st s1 s2 (MWrA (st := st) Z m σ1 s1) := F2.wr + refine (w1.trans w).mono ?_ + intro b hb' + rcases hb' with (hs | ht') | he + · exact Or.inl (Or.inr hs) + · exact Or.inr ht' + · exact Or.inl (mwr_back (Z := Z) (Z' := Z) rfl rfl sz w1.1 he) + exact ⟨s2, σ', Q', R', hStepB.trans (congrArg (fun ax => ok (s2, ax)) ea), hInvoke, HF, hw⟩ + +/-- Successful same-core [S] thread Bind through its concrete and abstract decoders. +[J] The ready-queue exclusion for the runnable unbound target comes from +`BoundInv.q` at the Stage-3 join. The blocked target's release condition +expresses the successful decoder check. -/ +theorem sc_decode_bind_thread_localA + {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRA st Z m κ s σ) (hst : StEqA Z.T (L.mem s) σ) + (a : A) (hmb : sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + {sc t ct : U64} (hc : sc ∈ Z.C) (h0 : sc ≠ 0#u64) (ht : t ∈ Z.T) (hct : ct ∈ Z.T) + {x xc : Tcb} {y : SchedContext} {n : Nat} + (hkt : σ.kheap (specPtr t) = some (.tcb x)) (hkc : σ.kheap (specPtr ct) = some (.tcb xc)) + (hk : σ.kheap (scNameA m sc) = some (.schedContext y n)) + (hr : runnable xc.tcbState = true) + (hsc0 : L.mem s (scTcbA sc) = 0#u64) (ht0 : L.mem s (scA t) = 0#u64) + (hon : L.mem s (affA t) = L.cpu s) (hcpu : L.mem s (scCoreA sc) = L.cpu s) + (hni : NotIdleScA m (scNameA m sc)) + (hstall : smp.remote_tcb_stall st ar s a t = ok (s, a)) + (hnq : runnable x.tcbState = true → ∀ d p, specPtr t ∉ σ.readyQueues d p) + (hb : ipcQueuedThreadState x.tcbState = true → scReleased σ.curTime y = true) + (hcur : sched.cur_thread st s = ok ct) + {call : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hcap : schedcontext.extra_cap st s extra 0#usize = ok (some (.Thread t))) + {label : Data} (hl : genInvocationType label = .schedContextBind) (rest : List Cap) : + schedcontext.decode st ar s a fl4_abi.label.SchedContextBind sc call extra ⦃ r => + r.1 = .Ok () ∧ r.2.2 = a ∧ ∃ σ1 σ' Q' R', + (decodeSchedContextInvocation (Z := Unit) label (scNameA m sc) (.threadCap (specPtr t) :: rest)).run σ = + ({(.ok (.invokeSchedContextBind (scNameA m sc) (.threadCap (specPtr t))), σ)}, False) ∧ + setThreadState (Z := Unit) (specPtr ct) .restart σ = ({((), σ1)}, False) ∧ + invokeSchedContext (Z := Unit) (.invokeSchedContextBind (scNameA m sc) (.threadCap (specPtr t))) σ1 = + ({([], σ')}, False) ∧ + SchedSRA st { Z with Q := Q', R := R' } m κ r.2.1 σ' ∧ + WritesIn st s r.2.1 (fun b => MWrA (st := st) Z m σ s b ∨ b = stA ct) ⦄ := by + let p := scNameA m sc + let u := specPtr t + let c := specPtr ct + obtain ⟨x0, hx0, hxo⟩ := H.sch.tcb t ht + rw [hkt] at hx0; cases hx0 + have hxn : x.tcbSchedContext = none := by rw [hxo, ht0, optScA, if_pos rfl] + obtain ⟨y0, k0, hky, hy⟩ := H.scr.sc sc hc + rw [hk] at hky; cases hky + have hyn : y.scTcb = none := by rw [hy.tcb, ← scTcbA_eqSc, hsc0, optRefS_zero] + have hd := decodeBind_threadA hkt hk hxn hyn (H.inv.ne hk) hb rest + have hrc : ipcQueuedThreadState x.tcbState = true → sporadic.sc_released st s sc = ok true := + fun e => (bind_scReleasedA H.toSchedSRN hc hk).trans (congrArg ok (hb e)) + have hD := bind_decode_bodyA (call := call) ((eq_of_spec_fd (obj.Sc.tcb.spec (L := L) s sc)).trans (congrArg ok hsc0)) + hcur hcap ((eq_of_spec_fd (obj.Tcb.sc.spec (L := L) s t)).trans (congrArg ok ht0)) + (bind_isBlockedA (hst t ht x hkt)) hrc hstall + have hv : sched.TS_RESTART.val = tsCodeA .restart := by rw [ts_vals.2.1]; rfl + obtain ⟨s1, hStepR, σ1, hR, H1, hst1, hh, w1⟩ := + spec_ex_fd (sc_set_runnable_stateA H hst hct hkc hr (ts := .restart) rfl hv) + obtain ⟨s2, σ', Q', R', hStepB, hInvoke, HF, hw⟩ := + bind_after_restartA H H1 hst1 a hmb ht hc h0 hkt hkc hk hxn hyn hr hon hcpu hni hnq hb hR hh w1 + have hStep : schedcontext.decode st ar s a fl4_abi.label.SchedContextBind sc call extra = + ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s2, a) := + hD.trans (bind_decode_finishA hStepR hStepB rfl) + have hDecode : (decodeSchedContextInvocation (Z := Unit) label p (.threadCap u :: rest)).run σ = + ({(.ok (.invokeSchedContextBind p (.threadCap u)), σ)}, False) := by + unfold decodeSchedContextInvocation + rw [hl] + exact hd + exact bind_postA hStep ⟨rfl, rfl, σ1, σ', Q', R', hDecode, hR, hInvoke, HF, hw⟩ + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScBindUnblockA.lean b/proofs/Fl4ProofsA64/P3/ScBindUnblockA.lean new file mode 100644 index 000000000..2c64a6124 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScBindUnblockA.lean @@ -0,0 +1,162 @@ +/- +P3 (AArch64): the sporadic unblock between Bind's link writes and resume. +The temporary Bind relation leaves release-readiness of the target open. +When the refill is already ready, it is sufficient and the full relation +can run the unblock. A future refill gives the identity branch. +-/ +import Fl4ProofsA64.P3.ScBindLinkA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4SpecA64 + +noncomputable section + +/-- The abstract test before Bind's sporadic unblock. -/ +theorem bind_unblock_guardA {σ : State Unit} {p : ObjRef} {x : SchedContext} {n : Nat} + (hk : σ.kheap p = some (.schedContext x n)) : + ifSporadicActiveCurScTestRefillUnblockCheck (Z := Unit) (some p) σ = + if x.scSporadic = true ∧ scActive x = true ∧ p ≠ σ.curSc then refillUnblockCheck p σ + else ({((), σ)}, False) := by + unfold ifSporadicActiveCurScTestRefillUnblockCheck ifCondRefillUnblockCheck + simp only [Fl4Spec.NonDet.maybeM] + rw [nd_bind_oneSc (getSchedContext_eqA hk), nd_gets_bindSc, nd_pure_bindSc] + by_cases h1 : x.scSporadic = true <;> by_cases h2 : scActive x = true <;> by_cases h3 : p = σ.curSc <;> + simp [Fl4Spec.NonDet.when, Fl4Spec.NonDet.pure, h1, h2, h3] + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +private theorem bind_unblock_futureA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRN st Z m κ s σ) {sc : U64} (hc : sc ∈ Z.C) {x : SchedContext} {n : Nat} + (hk : σ.kheap (scNameA m sc) = some (.schedContext x n)) (ha : scActive x = true) + (hr : refillReady σ.curTime (refillHd x) = false) : + sporadic.unblock_check st s sc = ok s ∧ + refillUnblockCheck (Z := Unit) (scNameA m sc) σ = ({((), σ)}, False) := by + have hn := H.inv.ne hk ha + have hspec := getScRefillReady_eqA (σ := σ) hk hn + rw [hr] at hspec + have hread : sporadic.ready st s sc = ok false := eq_of_spec_fd <| + WP.spec_mono (ready_refinesA H.scr H.sch.time hc hk ha) fun b hb => + (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst (hb.symm.trans hspec)))).1 + constructor + · unfold sporadic.unblock_check + obtain ⟨b, eq, -⟩ := spec_ex_fd (is_round_robin_refinesA H.scr hc hk) + rw [eq, bind_ok] + cases b <;> simp [hread, bind_ok] + · unfold refillUnblockCheck + rw [nd_bind_oneSc (isRoundRobin_eqA hk), nd_bind_oneSc hspec] + simp [Fl4Spec.NonDet.when, Fl4Spec.NonDet.pure] + +/-- The concrete test and step from the body of `schedcontext.bind_tcb`. -/ +def bindUnblockA (st : machine.Store S) (s : S) (sc : U64) : Result S := do + let spor ← sporadic.sc_sporadic st s sc + if spor then + let active ← sporadic.sc_active st s sc + if active then + let cur ← sched.cur_sc st s + let other ← core.cmp.PartialEq.ne.trait_default obj.Sc.Insts.CoreCmpPartialEqSc sc cur + if other then sporadic.unblock_check st s sc else ok s + else ok s + else ok s + +/-- Relate the sporadic unblock after Bind has installed the reciprocal pair. -/ +theorem bind_unblockA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + {t sc : U64} (H : SchedSRB st Z m κ s σ (specPtr t)) (hc : sc ∈ Z.C) (h0 : sc ≠ 0#u64) + (hn : specPtr t ∉ σ.releaseQueue) (hni : NotIdleScA m (scNameA m sc)) + {x : SchedContext} {n : Nat} {xt : Tcb} (hk : σ.kheap (scNameA m sc) = some (.schedContext x n)) + (hkt : σ.kheap (specPtr t) = some (.tcb xt)) (hb : x.scTcb = some (specPtr t)) + (hbt : xt.tcbSchedContext = some (scNameA m sc)) : + bindUnblockA st s sc ⦃ s' => ∃ σ', + ifSporadicActiveCurScTestRefillUnblockCheck (Z := Unit) (some (scNameA m sc)) σ = ({((), σ')}, False) ∧ + SchedSRB st Z m κ s' σ' (specPtr t) ∧ UnbFrA (st := st) Z m s s' σ σ' ∧ + (refillReady σ.curTime (refillHd x) = true → ∀ y k, σ'.kheap (scNameA m sc) = some (.schedContext y k) → + refillReady σ'.curTime (refillHd y) = true) ⦄ := by + obtain ⟨x', n', hk', hx⟩ := H.scr.sc sc hc + rw [hk] at hk' + cases hk' + have hsf := bind_unblock_guardA hk + have hcur : σ.curSc = scNameA m (L.cscW s) := by + rw [← H.sch.csc, H.sch.core] + exact H.sch.curScOfW _ H.sch.cpuCs + have scName_ne : sc ≠ L.cscW s → scNameA m sc ≠ σ.curSc := by + intro he eq + rw [hcur] at eq + exact he (scRenA_inj m (H.sch.scOff _ hc) (H.sch.scOff _ H.cscC) eq) + have hspor : sporadic.sc_sporadic st s sc = ok x.scSporadic := by + rw [sc_sporadic_eq, if_neg h0, hx.sporadic] + congr 1 + have e : sporA sc = scWA sc 64#u64 := by unfold sporA scWA layout.sc.SPORADIC; rfl + rw [e] + by_cases h : L.mem s (scWA sc 64#u64) = 0#u64 <;> simp [h] + have hactive : sporadic.sc_active st s sc = ok (scActive x) := by + unfold sporadic.sc_active + rw [eq_of_spec_fd (obj.Sc.refill_max.spec (L := L) s sc), bind_ok] + congr 1 + unfold scActive activeSc + apply decide_eq_decide.2 + rw [gt_iff_valSc, hx.max] + rfl + have finish : ∀ s' σ', + refillUnblockCheck (Z := Unit) (scNameA m sc) σ = ({((), σ')}, False) → + SchedSRB st Z m κ s' σ' (specPtr t) → UnbFrA (st := st) Z m s s' σ σ' → + scActive x = true → ∀ hr : refillReady σ.curTime (refillHd x) = true, + ∀ y k, σ'.kheap (scNameA m sc) = some (.schedContext y k) → refillReady σ'.curTime (refillHd y) = true := by + intro s' σ' he H' F ha hr y k hy + have hm : ((), σ') ∈ (refillUnblockCheck (Z := Unit) (scNameA m sc) σ).1 := by rw [he]; rfl + rcases unblock_resultA hk (H.inv.ne hk ha) hm with rfl | ⟨z, e, -, hz, -⟩ + · rw [hk] at hy; cases hy; exact hr + · rw [e, updKSc_kheap, Function.update_self] at hy + cases hy + rw [F.time] + exact ready_nowA σ _ hz + have skip : ∀ y k, σ.kheap (scNameA m sc) = some (.schedContext y k) → + refillReady σ.curTime (refillHd x) = true → refillReady σ.curTime (refillHd y) = true := by + intro y k he hr + rw [hk] at he + cases he + exact hr + unfold bindUnblockA + rw [hspor, bind_ok] + by_cases hs : x.scSporadic = true + · rw [if_pos hs, hactive, bind_ok] + by_cases ha : scActive x = true + · rw [if_pos ha, eq_of_spec_fd (sched.cur_sc.spec (L := L) s), bind_ok, sc_ne_eqA, bind_ok] + change (if decide (sc ≠ L.cscW s) = true then sporadic.unblock_check st s sc else ok s) ⦃ _ ⦄ + by_cases he : sc = L.cscW s + · have he' : scNameA m sc = σ.curSc := by rw [hcur, he] + rw [if_neg (fun h => (of_decide_eq_true h) he), WP.spec_ok] + refine ⟨σ, ?_, H, UnbFrA.refl Z m s σ, fun hr y k hy => skip y k hy hr⟩ + rw [hsf, if_neg (fun h => h.2.2 he')] + · rw [decide_eq_true he, if_pos rfl] + have hg := hsf.trans (if_pos ⟨hs, ha, scName_ne he⟩) + by_cases hr : refillReady σ.curTime (refillHd x) = true + · have hrib : RelIfBoundSc σ (specPtr t) := by + intro q p y k hq hp hy hyA _ + rw [hkt] at hq; cases hq + rw [hbt] at hp; cases hp + rw [hk] at hy; cases hy + exact ⟨hr, H.inv.suff hk ha hni⟩ + have hnrel : ∀ u ∈ σ.releaseQueue, ∀ z, σ.kheap u = some (.tcb z) → z.tcbSchedContext ≠ some (scNameA m sc) := by + intro u hu z hz hzsc + obtain ⟨y, k, hy, hyt⟩ := H.inv.back u z _ hz hzsc + rw [hk] at hy; cases hy + exact hn ((Option.some.inj (hyt.symm.trans hb)) ▸ hu) + refine WP.spec_mono ((H.toA hrib).unblock_checkA hc hk ha (fun _ _ => hnrel)) ?_ + rintro s' ⟨σ', hspec, H', F⟩ + exact ⟨σ', hg.trans hspec, H'.toB (specPtr t), F, finish s' σ' hspec (H'.toB _) F ha⟩ + · obtain ⟨eq, hspec⟩ := bind_unblock_futureA H.toSchedSRN hc hk ha ((Bool.not_eq_true _) ▸ hr) + rw [eq, WP.spec_ok] + exact ⟨σ, hg.trans hspec, H, UnbFrA.refl Z m s σ, fun e => (hr e).elim⟩ + · rw [if_neg ha, WP.spec_ok] + refine ⟨σ, ?_, H, UnbFrA.refl Z m s σ, fun hr y k hy => skip y k hy hr⟩ + rw [hsf, if_neg (fun h => ha h.2.1)] + · rw [if_neg hs, WP.spec_ok] + refine ⟨σ, ?_, H, UnbFrA.refl Z m s σ, fun hr y k hy => skip y k hy hr⟩ + rw [hsf, if_neg (fun h => hs h.1)] + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScBootA.lean b/proofs/Fl4ProofsA64/P3/ScBootA.lean new file mode 100644 index 000000000..2adc1907f --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScBootA.lean @@ -0,0 +1,152 @@ +/- +P3 (aarch64, A64-P3-MCS gen 8): **`ScInvA` at boot** (`w0`: `bootSA` / `bootMA`; `w1`: `w1SA` / `w1MA`), with +idle SCs excluded from `ScInvA.rr` (ruling 00:50Z: l4v's boot idle SC, `JBootA.idleScObjA`, has the single refill +`⟨0, 0⟩` and is never round-robin charged; `NotIdleScA`). + +* `bootKA_scA`: a boot SC has period 0, refills, and is some core's idle SC (`¬ NotIdleScA`), given + `nc ≤ numNodes` (`hnc`: the booted cores are platform cores; `NotIdleScA` quantifies below + `fl4A64Platform.numNodes`, S3's boot below `nc`). +* `bootKA_backA`: an idle TCB's SC is bound back to it (x86 `Inv.back` at boot). +* **`boot_scInvA`**: `ScInvA (bootMA ib nc) (bootSA kb ib nc now mt)` (no notification, empty release queue). +* **`w1_scInvA`**: `ScInvA (w1MA ib nc) (w1SA kb ib nc now mt sl)`; `w1` adds the root TCB and the root SC + (`rootScA now sl`: period 0, refills `⟨now, sl⟩, ⟨now, 0⟩`), whose `RRokA` needs `MIN_BUDGET ≤ sl` (`hsl`, the + boot time slice). + +Gen 11 (ruling 02:05Z): `ScInvA.tcbScNotIdle` at both: vacuous at `w0` (its TCBs are the idle threads); at `w1` +the root TCB's SC is not idle (**`w1_rootSc_notIdleA`**), which needs every platform core booted (`hnn : +numNodes ≤ nc`: otherwise an unbooted core `c ∈ [nc, numNodes)`'s idle-SC address `idle_base + 8192 c + 4096` may +be the root SC's, which `BootLayA` / `BootLay1` allow). + +No named premise; `hnc` / `hnn` / `hsl` are hypotheses on the boot constants (open question in the handoff). +-/ +import Fl4ProofsA64.P3.ScGlueA +import Fl4ProofsA64.P3.JEntW1A +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P1.W7.R.ST.BootWit + +noncomputable section + +section +variable {kb ib now mt sl : U64} {nc : Nat} + +/-- A boot SC has period 0 and refills, and is an idle SC. -/ +theorem bootKA_scA (hnc : nc ≤ Fl4SpecA64.fl4A64Platform.numNodes) {p : Fl4SpecA64.ObjRef} + {y : Fl4SpecA64.SchedContext} {n : Nat} (h : bootKA kb ib nc now mt p = some (.schedContext y n)) : + y.scPeriod = 0 ∧ y.scRefills ≠ [] ∧ ¬ NotIdleScA (bootMA ib nc) p := by + unfold bootKA at h + split_ifs at h with h1 h2 h3 + · cases h; exact ⟨rfl, by simp [idleScObjA], fun hn => hn.1 h1⟩ + · cases h + · cases h + obtain ⟨c, -, hc, rfl⟩ := h3 + exact ⟨rfl, by simp [idleScObjA], fun hn => hn.2 c (by omega) rfl⟩ + · cases h + +/-- An idle TCB's SC is bound back to it. -/ +theorem bootKA_backA (hL : BootLayA ib nc) {u q : Fl4SpecA64.ObjRef} {x : Fl4SpecA64.Tcb} + (hx : bootKA kb ib nc now mt u = some (.tcb x)) (hq : x.tcbSchedContext = some q) : + ∃ y n, bootKA kb ib nc now mt q = some (.schedContext y n) ∧ y.scTcb = some u := by + rcases bootKA_cases hL hx with ⟨c, hc, rfl, -⟩ | ⟨_, e⟩ | e + · rw [bootKA_tcb hL hc] at hx; cases hx; cases hq + by_cases h0 : c = 0 + · subst h0; rw [bootRen0 hL]; exact ⟨_, _, bootKA_sc0 hL, rfl⟩ + · rw [bootRen hL (Nat.pos_of_ne_zero h0) hc] + exact ⟨_, _, bootKA_sc hL (Nat.pos_of_ne_zero h0) hc, rfl⟩ + · cases e + · cases e + +/-- **`ScInvA` at `w0`** (module docstring). -/ +theorem boot_scInvA (hL : BootLayA ib nc) (hnc : nc ≤ Fl4SpecA64.fl4A64Platform.numNodes) : + ScInvA (bootMA ib nc) (bootSA kb ib nc now mt) where + refills _ _ _ h _ hp := absurd (bootKA_scA hnc h).1 hp + rr _ _ _ h _ _ := ⟨(bootKA_scA hnc h).2.1, fun hn => absurd hn (bootKA_scA hnc h).2.2⟩ + ntfnIdle _ _ h := by rcases bootKA_cases hL h with ⟨_, _, _, _, e⟩ | ⟨_, e⟩ | e <;> cases e + ntfnIdleOf _ _ h := by rcases bootKA_cases hL h with ⟨_, _, _, _, e⟩ | ⟨_, e⟩ | e <;> cases e + relSc _ hu := by cases hu + sorted := List.Pairwise.nil + back _ _ _ hx hp := bootKA_backA hL hx hp + tcbScNotIdle _ _ _ hx _ hi := by + rcases bootKA_cases hL hx with ⟨c, hc, rfl, -⟩ | ⟨_, e⟩ | e + · exact (hi c (by omega) rfl).elim + · cases e + · cases e + +/-- The root SC's refills are `RRokA` when the time slice holds `MIN_BUDGET`. -/ +theorem rootScA_rr (hsl : Fl4SpecA64.MIN_BUDGET ≤ sl.bv) : RRokA (rootScA now sl) := by + refine ⟨by simp [rootScA], ?_, ?_⟩ + · simp only [rootScA, List.map_cons, List.map_nil, List.sum_cons, List.sum_nil] + have := sl.bv.isLt; simpa using this + · exact hsl + +/-- (gen 11) **The root SC is no idle SC** at `w1`, given that every platform core was booted (`hnn`): it lies above +the idle area (`BootLay1.ibR`) and is not `idle_sc_ptr`. -/ +theorem w1_rootSc_notIdleA (hL : BootLayA ib nc) (P : BootLay1 kb ib nc) + (hnn : Fl4SpecA64.fl4A64Platform.numNodes ≤ nc) : NotIdleScA (w1MA ib nc) (specPtr (bwv SCb)) := by + refine ⟨fun h => ?_, fun c hc h => ?_⟩ + · have := specPtr_eq_idleSc.1 h; rw [vSC] at this; omega + · have hc' : c < nc := by omega + have e := congrArg UScalar.val (specPtr_inj (p := bwv SCb) (q := idleSA ib c) h) + rw [vSC, idleSA_val hL hc'] at e + have := P.ibR + omega + +/-- **`ScInvA` at `w1`** (module docstring; gen 11: + `hnn` for `tcbScNotIdle`). -/ +theorem w1_scInvA (hL : BootLayA ib nc) (P : BootLay1 kb ib nc) (hnc : nc ≤ Fl4SpecA64.fl4A64Platform.numNodes) + (hnn : Fl4SpecA64.fl4A64Platform.numNodes ≤ nc) + (hsl : Fl4SpecA64.MIN_BUDGET ≤ sl.bv) : ScInvA (w1MA ib nc) (w1SA kb ib nc now mt sl) where + refills p y n h _ hp := by + change w1KA kb ib nc now mt sl p = _ at h + rcases w1KA_cases hL h with e | e | e | e | ⟨-, e⟩ | e | ⟨h0, -⟩ + all_goals try (cases e; done) + · cases e; exact absurd rfl hp + · exact absurd (bootKA_scA hnc h0).1 hp + rr p y n h _ _ := by + change w1KA kb ib nc now mt sl p = _ at h + rcases w1KA_cases hL h with e | e | e | e | ⟨-, e⟩ | e | ⟨h0, -⟩ + all_goals try (cases e; done) + · cases e; exact ⟨by simp [rootScA], fun _ => rootScA_rr hsl⟩ + · exact ⟨(bootKA_scA hnc h0).2.1, fun hn => absurd hn (bootKA_scA hnc h0).2.2⟩ + ntfnIdle _ _ h := by + change w1KA kb ib nc now mt sl _ = _ at h + rcases w1KA_cases hL h with e | e | e | e | ⟨-, e⟩ | e | ⟨-, ⟨_, _, _, _, e⟩ | ⟨_, e⟩ | e⟩ <;> cases e + ntfnIdleOf _ _ h := by + change w1KA kb ib nc now mt sl _ = _ at h + rcases w1KA_cases hL h with e | e | e | e | ⟨-, e⟩ | e | ⟨-, ⟨_, _, _, _, e⟩ | ⟨_, e⟩ | e⟩ <;> cases e + relSc _ hu := by cases hu + sorted := List.Pairwise.nil + back u x q hx hq := by + change w1KA kb ib nc now mt sl u = _ at hx + change ∃ y n, w1KA kb ib nc now mt sl q = _ ∧ _ + unfold w1KA at hx + split_ifs at hx with h1 h2 h3 h4 h5 h6 + · cases hx + · cases hx; cases hq + have eu : u = specPtr (bwv RTb) := by + rw [← specPtr_fl4Ptr u]; congr 1; exact UScalar.eq_of_val_eq (h2.trans vRT.symm) + subst eu; exact ⟨_, _, w1KA_sc, rfl⟩ + · cases hx + · cases hx + · cases hx + · cases hx + · rcases bootKA_cases hL hx with ⟨c, hc, rfl, -⟩ | ⟨_, e⟩ | e + · rw [bootKA_tcb hL hc] at hx; cases hx; cases hq + by_cases e0 : c = 0 + · subst e0; rw [bootRen0 hL]; exact ⟨_, _, w1KA_sc0 hL, rfl⟩ + · rw [bootRen hL (Nat.pos_of_ne_zero e0) hc] + exact ⟨_, _, w1KA_scc hL P (Nat.pos_of_ne_zero e0) hc, rfl⟩ + · cases e + · cases e + tcbScNotIdle u x q hx hq hi := by + change w1KA kb ib nc now mt sl u = _ at hx + rcases w1KA_cases hL hx with e | e | e | e | ⟨-, e⟩ | e | ⟨-, ⟨c, hc, rfl, -⟩ | ⟨_, e⟩ | e⟩ + all_goals try (cases e; done) + · cases e; cases hq; exact w1_rootSc_notIdleA hL P hnn + · exact (hi c (by omega) rfl).elim + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScBudgetA.lean b/proofs/Fl4ProofsA64/P3/ScBudgetA.lean new file mode 100644 index 000000000..5ea196225 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScBudgetA.lean @@ -0,0 +1,665 @@ +/- +P3 (aarch64, A64-P3-MCS gen 2): **`sporadic::budget_check` refines l4v `refill_budget_check`** on `ScStateRelA` +(x86 `P3/ScBudget`'s `loop0_refines` / `loop1_refines` and `P3/ScBudgetTop`'s `budget_check_refines`, ported). + +FL4's AArch64 `sporadic::budget_check` and its two loops are the x86 text (extraction diffed: SAME) and +`Fl4SpecA64.refillBudgetCheck` / `handleOverrun` / `headInsufficientLoop` the x86 port's, so the x86 proofs port +with `RingAtA` / `schedUsedLA` / `chargeLA` (`P3/ScRingA`) and the SC named `p` (= `scNameA m sc` at the use): +* `lsum` / `Pos` (x86 `lsum` / `Pos`): only the refill amounts matter for the simulation; they are positive (each + round consumes usage, FL4's fuel `U64::MAX` never runs out) and their sum is fixed and below `2 ^ 64`. +* **`loop0_refinesWithA`** (x86 `loop0_refines`): `budget_check_loop0` is `handle_overrun`. +* **`loop1_refinesWithA`** (x86 `loop1_refines`): `budget_check_loop1` is `head_insufficient_loop`. +* **`budget_check_refinesA`** (x86 `budget_check_refines`): the whole `budget_check` of the current SC is + `refill_budget_check`, with a single spec outcome; `ScStateRelA` and `ScPlacedA` hold after, FL4 writes inside the + SC's object, and `BudgetPostA` (x86 `BudgetPost`) describes the new refills. + +No named premise. Hypotheses, as x86's `budget_check_refines`: the current SC (`hcs`: FL4's `CUR_SC` word; `hcur`: +the spec's `cur_sc`, = `SchedRelA.curScW` at `JAtZA`) is a configured SC of `C` (`hc`, `hk`, `ha`) that is not +round-robin (`hrr`, seL4 asserts it); its amounts are positive, sum below `2 ^ 64` and to at least `MIN_BUDGET` +(`hpos` / `hsum` / `hmin`, l4v `valid_refills`); the platform's `MAX_RELEASE_TIME` / `MIN_BUDGET` are the spec's +(`hmrt` / `hmb`, x86's platform facts; `hmb` is gen 1's, both to move into the `[P]` platform record at the join). +-/ +import Fl4ProofsA64.P3.ScRingA +import Fl4SpecA64.Ext.SchedBudget +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +/-! ## Amounts (x86 `ScBudget`) -/ + +/-- The sum of the amounts (x86 `lsum`). -/ +def lsumA (rs : List sporadic.Refill) : Nat := (rs.map fun r => r.amount.val).sum + +/-- All amounts are positive (x86 `Pos`). -/ +def PosA (rs : List sporadic.Refill) : Prop := ∀ r ∈ rs, 0 < r.amount.val + +theorem lsumA_eq_map (rs : List sporadic.Refill) : + lsumA rs = ((rs.map toRefillA).map fun r => r.rAmount.toNat).sum := by + simp [lsumA, toRefillA, Function.comp]; rfl + +theorem lsumA_append (a b : List sporadic.Refill) : lsumA (a ++ b) = lsumA a + lsumA b := by + simp [lsumA] + +theorem lsumA_cons (r : sporadic.Refill) (a : List sporadic.Refill) : lsumA (r :: a) = r.amount.val + lsumA a := by + simp [lsumA] + +theorem lsumA_single (r : sporadic.Refill) : lsumA [r] = r.amount.val := by simp [lsumA] + +theorem le_lsumA {rs : List sporadic.Refill} {r : sporadic.Refill} (h : r ∈ rs) : r.amount.val ≤ lsumA rs := by + induction rs with + | nil => cases h + | cons a l ih => + rw [lsumA_cons] + rcases List.mem_cons.1 h with rfl | h + · omega + · have := ih h; omega + +theorem getLastD_eqA {rs : List sporadic.Refill} (hne : rs ≠ []) : rs.getLastD zRA = rs.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_getLast hne]; rfl + +theorem split_lastA {rs : List sporadic.Refill} (hne : rs ≠ []) : rs = rs.dropLast ++ [rs.getLastD zRA] := by + rw [getLastD_eqA hne, List.dropLast_concat_getLast] + +theorem lsum_schedUsedLA {m : Nat} {rs : List sporadic.Refill} (hne : rs ≠ []) {nw : sporadic.Refill} + (hno : (rs.getLastD zRA).amount.val + nw.amount.val < 2 ^ 64) : + lsumA (schedUsedLA m rs nw) = lsumA rs + nw.amount.val := by + have e := split_lastA hne + have hl : lsumA rs = lsumA rs.dropLast + (rs.getLastD zRA).amount.val := by + conv_lhs => rw [e] + rw [lsumA_append, lsumA_single] + unfold schedUsedLA + dsimp only + split_ifs + · rw [lsumA_append, lsumA_single]; simp only; rw [wadd_val hno]; omega + · rw [lsumA_append, lsumA_single]; simp only; rw [wadd_val hno]; omega + · rw [lsumA_append, lsumA_single] + +theorem pos_schedUsedLA {m : Nat} {rs : List sporadic.Refill} (hP : PosA rs) (hne : rs ≠ []) + {nw : sporadic.Refill} (hno : (rs.getLastD zRA).amount.val + nw.amount.val < 2 ^ 64) (hn : 0 < nw.amount.val) : + PosA (schedUsedLA m rs nw) := by + have hd : ∀ r ∈ rs.dropLast, 0 < r.amount.val := fun r hr => hP r (List.dropLast_subset _ hr) + unfold schedUsedLA + dsimp only + split_ifs + · intro r hr + rcases List.mem_append.1 hr with hr | hr + · exact hd r hr + · simp only [List.mem_singleton] at hr; subst hr; simp only; rw [wadd_val hno]; omega + · intro r hr + rcases List.mem_append.1 hr with hr | hr + · exact hd r hr + · simp only [List.mem_singleton] at hr; subst hr; simp only; rw [wadd_val hno]; omega + · intro r hr + rcases List.mem_append.1 hr with hr | hr + · exact hP r hr + · simp only [List.mem_singleton] at hr; subst hr; exact hn + +section +variable {S : Type _} {st : machine.Store S} [L : LawfulStore st] + +theorem refills_neL {s : S} {sc : obj.Sc} {sb : Nat} (hW : RefillsWF st s sc sb) : L.refills s sc ≠ [] := by + rw [refills_consA hW]; simp + +theorem single_iffA {s : S} {sc : obj.Sc} {sb : Nat} (hW : RefillsWF st s sc sb) : + L.rhead s sc = L.rtail s sc ↔ (L.refills s sc).length = 1 := by + rw [hW.single_iff, refills_lengthA] + +/-- The spec state with the SC at `p` given FL4's refills of `sc` (x86 `specAt`). -/ +abbrev specAtA (σ : Fl4SpecA64.State Unit) (p : Fl4SpecA64.ObjRef) (sc : U64) (x : Fl4SpecA64.SchedContext) + (n : Nat) (s : S) : Fl4SpecA64.State Unit := + updKSc σ p (.schedContext { x with scRefills := (L.refills s sc).map toRefillA } n) + +/-! ## The overrun loop -/ + +/-- **The overrun loop refines `handle_overrun`** (x86 `loop0_refines`). -/ +theorem headRefillOverrunWith_eqA (limit : Fl4SpecA64.Ticks) + {σ : Fl4SpecA64.State Unit} {p : Fl4SpecA64.ObjRef} {x : Fl4SpecA64.SchedContext} {n : Nat} + {u : Fl4SpecA64.Ticks} (hk : σ.kheap p = some (.schedContext x n)) (hne : x.scRefills ≠ []) : + Fl4SpecA64.headRefillOverrunWith (Z := Unit) limit p u σ = + some (decide ((Fl4SpecA64.refillHd x).rAmount ≤ u ∧ (Fl4SpecA64.refillHd x).rTime < limit)) := by + show Option.bind (Fl4SpecA64.readRefillHead p σ) (fun _ => Fl4Spec.Lookup.oreturn _ σ) = _ + rw [readRefillHead_eqA hk hne]; rfl + +theorem loop0_refinesWithA {s0 : S} {σ0 : Fl4SpecA64.State Unit} {p : Fl4SpecA64.ObjRef} {sc : U64} + {x : Fl4SpecA64.SchedContext} {n : Nat} + (hk : σ0.kheap p = some (.schedContext x n)) (hx : ScRel1A st s0 sc x n) + (limit : Fl4SpecA64.Ticks) (mrt period : U64) (hmrt : mrt.bv = limit) (hper : L.rperiod s0 sc = period) + (hpos : PosA (L.refills s0 sc)) (hsum : lsumA (L.refills s0 sc) < 2 ^ 64) + (usage fuel : U64) (hu : usage.val ≤ fuel.val) : + sporadic.budget_check_loop0 st s0 sc mrt period usage fuel ⦃ s1 u1 => + RingAtA st s0 s1 sc n ∧ PosA (LawfulStore.refills (st := st) s1 sc) ∧ + lsumA (LawfulStore.refills (st := st) s1 sc) = lsumA (L.refills s0 sc) ∧ + ¬ (((LawfulStore.refills (st := st) s1 sc).headD zRA).amount ≤ u1 ∧ + ((LawfulStore.refills (st := st) s1 sc).headD zRA).time < mrt) ∧ + Fl4SpecA64.handleOverrunWith (Z := Unit) limit p usage.bv σ0 = + ({(u1.bv, specAtA (st := st) σ0 p sc x n s1)}, False) ⦄ := by + have hkk : ∀ s : S, (specAtA (st := st) σ0 p sc x n s).kheap p = + some (.schedContext { x with scRefills := (L.refills s sc).map toRefillA } n) := fun s => updKSc_same _ _ _ + have hper' : x.scPeriod = period.bv := by rw [hx.period, hper] + have h00 : specAtA (st := st) σ0 p sc x n s0 = σ0 := by + show updKSc σ0 p (.schedContext { x with scRefills := (L.refills s0 sc).map toRefillA } n) = σ0 + rw [← hx.refills]; exact updKSc_selfA hk + unfold sporadic.budget_check_loop0 + apply loop.spec_decr_nat (measure := fun y => y.2.2.val) + (inv := fun y => RingAtA st s0 y.1 sc n ∧ PosA (L.refills y.1 sc) ∧ + lsumA (L.refills y.1 sc) = lsumA (L.refills s0 sc) ∧ y.2.1.val ≤ y.2.2.val ∧ + Fl4SpecA64.handleOverrunWith (Z := Unit) limit p usage.bv σ0 = + Fl4SpecA64.handleOverrunWith (Z := Unit) limit p y.2.1.bv (specAtA (st := st) σ0 p sc x n y.1)) + · rintro ⟨x0, x1, x2⟩ ⟨hR, hP, hS, hle, hsp⟩ + dsimp only at hR hP hS hle hsp + have hW := hR.wf + have hne := refills_neL hW + have hne' : ((L.refills x0 sc).map toRefillA) ≠ [] := by simpa using hne + set hd := (L.refills x0 sc).headD zRA with hhd + have hdm : hd ∈ L.refills x0 sc := by + obtain ⟨a, l, e⟩ := List.exists_cons_of_ne_nil hne; rw [hhd, e]; exact List.mem_cons_self + have hpos0 := hP hd hdm + have hC : Fl4SpecA64.the (Fl4SpecA64.headRefillOverrunWith (Z := Unit) limit p x1.bv + (specAtA (st := st) σ0 p sc x n x0)) = true ↔ (hd.amount ≤ x1 ∧ hd.time < mrt) := by + rw [headRefillOverrunWith_eqA limit (hkk x0) hne', refillHd_mapA rfl hne, the_someA, decide_eq_true_iff, ← hhd] + simp only [toRefillA, ← u64_le_bvA, ← hmrt, ← u64_lt_bvA] + have fin : ¬ (hd.amount ≤ x1 ∧ hd.time < mrt) → + RingAtA st s0 x0 sc n ∧ PosA (L.refills x0 sc) ∧ lsumA (L.refills x0 sc) = lsumA (L.refills s0 sc) ∧ + ¬ (((L.refills x0 sc).headD zRA).amount ≤ x1 ∧ ((L.refills x0 sc).headD zRA).time < mrt) ∧ + Fl4SpecA64.handleOverrunWith (Z := Unit) limit p usage.bv σ0 = + ({(x1.bv, specAtA (st := st) σ0 p sc x n x0)}, False) := + fun hnc => ⟨hR, hP, hS, hnc, by + rw [hsp]; unfold Fl4SpecA64.handleOverrunWith + exact wl_doneSc (C := fun usage s => Fl4SpecA64.the (Fl4SpecA64.headRefillOverrunWith (Z := Unit) limit p usage s) = true) + (B := fun usage => Fl4SpecA64.chargeEntireHeadRefill p usage) (fun h => hnc (hC.1 h))⟩ + unfold sporadic.budget_check_loop0.body + refine WP.spec_bind (sporadic.head.spec _ _) ?_ + rintro h h_post + rw [head_eqA hW, ← hhd] at h_post + subst h_post + split + · rename_i hA + split + · rename_i hT + split + · -- a head with no budget: ruled out, refill amounts are positive + rename_i hz + exfalso + rw [hz] at hpos0; simp at hpos0 + · split + · rename_i hf + exfalso + have : x1.val = 0 := by rw [hf] at hle; simpa using hle + rw [UScalar.le_equiv] at hA; omega + · rename_i hf + step as ⟨fuel1, fuel1_post⟩ + refine WP.spec_bind (sporadic.single.spec _ _) ?_ + rintro b b_post + have hA' : hd.amount.val ≤ x1.val := by rw [UScalar.le_equiv] at hA; exact hA + have hu1 : (core.num.U64.wrapping_sub x1 hd.amount).val = x1.val - hd.amount.val := wsub_val hA' + have hspc := chargeEntire_eqA (hkk x0) rfl hne (per := period) hper' x1.bv + have hstep : Fl4SpecA64.handleOverrunWith (Z := Unit) limit p x1.bv (specAtA (st := st) σ0 p sc x n x0) = + Fl4SpecA64.handleOverrunWith (Z := Unit) limit p (x1.bv - (toRefillA hd).rAmount) + (updKSc σ0 p (.schedContext + { x with scRefills := (chargeLA x.scRefillMax period (L.refills x0 sc)).map toRefillA } n)) := by + unfold Fl4SpecA64.handleOverrunWith + rw [wl_stepSc (C := fun usage s => + Fl4SpecA64.the (Fl4SpecA64.headRefillOverrunWith (Z := Unit) limit p usage s) = true) + (B := fun usage => Fl4SpecA64.chargeEntireHeadRefill p usage) (hC.2 ⟨hA, hT⟩) hspc, updKSc_updKSc] + have hcont : ∀ s1 : S, RingAtA st s0 s1 sc n → + L.refills s1 sc = chargeLA x.scRefillMax period (L.refills x0 sc) → + PosA (L.refills s1 sc) → lsumA (L.refills s1 sc) = lsumA (L.refills s0 sc) → + RingAtA st s0 s1 sc n ∧ PosA (L.refills s1 sc) ∧ lsumA (L.refills s1 sc) = lsumA (L.refills s0 sc) ∧ + (core.num.U64.wrapping_sub x1 hd.amount).val ≤ fuel1.val ∧ + Fl4SpecA64.handleOverrunWith (Z := Unit) limit p usage.bv σ0 = Fl4SpecA64.handleOverrunWith (Z := Unit) limit p + (core.num.U64.wrapping_sub x1 hd.amount).bv (specAtA (st := st) σ0 p sc x n s1) ∧ + fuel1.val < x2.val := by + intro s1 h1 hl1 hp1 hs1 + refine ⟨h1, hp1, hs1, ?_, ?_, ?_⟩ + · rw [hu1, fuel1_post]; omega + · rw [hsp, hstep, core.num.U64.wrapping_sub_bv_eq] + show _ = Fl4SpecA64.handleOverrunWith (Z := Unit) limit p _ + (updKSc σ0 p (.schedContext { x with scRefills := (L.refills s1 sc).map toRefillA } n)) + rw [hl1]; rfl + · rw [fuel1_post] + have : x2.val ≠ 0 := fun e => hf (UScalar.eq_of_val_eq (by simpa using e)) + omega + split + · -- a single refill: a period later + rename_i hb + have h1 : (L.refills x0 sc).length = 1 := (single_iffA hW).1 (by rw [b_post] at hb; simpa using hb) + step as ⟨i1, i1_post⟩ + refine WP.spec_bind (set_head_ringA hR _) ?_ + rintro s1 ⟨hR1, hl1⟩ + obtain ⟨a, l, e⟩ := List.exists_cons_of_ne_nil hne + have hl : l = [] := by rw [e] at h1; simpa using h1 + subst hl + have hl1' : L.refills s1 sc = chargeLA x.scRefillMax period (L.refills x0 sc) := by + rw [hl1, e]; unfold chargeLA; simp only [List.length_cons, List.length_nil, ↓reduceIte, + List.headD_cons, List.tail_cons, i1_post] + rw [e] at hhd; simp only [List.headD_cons] at hhd; rw [hhd] + simp only [lift, bind_ok, WP.spec_ok] + refine hcont s1 hR1 hl1' ?_ ?_ + · rw [hl1, e]; intro r hr + simp only [List.tail_cons, List.mem_singleton] at hr; subst hr + simpa using hpos0 + · have ha : hd = a := by rw [hhd, e]; rfl + rw [hl1, ← hS, e]; simp [lsumA, ha] + · -- pop the head and schedule it a period later + rename_i hb + have hneq : L.rhead x0 sc ≠ L.rtail x0 sc := by rw [b_post] at hb; simpa using hb + have h2 : (L.refills x0 sc).length ≠ 1 := fun e => hneq ((single_iffA hW).2 e) + obtain ⟨a, l, e⟩ := List.exists_cons_of_ne_nil hne + have hlne : l ≠ [] := by rintro rfl; exact h2 (by rw [e]; rfl) + have ha : hd = a := by rw [hhd, e]; rfl + have hno : (l.getLastD zRA).amount.val + a.amount.val < 2 ^ 64 := by + have hS' := hS + rw [e, lsumA_cons] at hS' + have h3 := le_lsumA (rs := l) (r := l.getLastD zRA) + (by rw [getLastD_eqA hlne]; exact List.getLast_mem hlne) + omega + refine WP.spec_bind (Pₘ := fun s3 => RingAtA st s0 s3 sc n ∧ + L.refills s3 sc = chargeLA x.scRefillMax period (L.refills x0 sc) ∧ PosA (L.refills s3 sc) ∧ + lsumA (L.refills s3 sc) = lsumA (L.refills s0 sc)) ?_ ?_ + · refine WP.spec_bind (pop_head_ringA hR hneq) ?_ + rintro ⟨old, s2⟩ ⟨hold, hR2, hl2⟩ + rw [← hhd] at hold + subst hold + step as ⟨i1, i1_post⟩ + refine WP.spec_mono (schedule_used_ringA hR2 _) ?_ + rintro s3 ⟨hR3, hl3⟩ + have htl : L.refills s2 sc = l := by rw [hl2, e]; rfl + refine ⟨hR3, ?_, ?_, ?_⟩ + · rw [hl3, htl, hR2.mx, hx.max, e] + unfold chargeLA + simp only [List.headD_cons, List.tail_cons, i1_post, ← ha] + rw [if_neg (by simpa using hlne)] + · rw [hl3, htl] + exact pos_schedUsedLA (fun r hr => hP r (by rw [e]; exact List.mem_cons_of_mem _ hr)) hlne + (by rw [ha]; simpa using hno) (by simpa using hpos0) + · rw [hl3, htl, lsum_schedUsedLA hlne (by rw [ha]; simpa using hno), ← hS, e, lsumA_cons, ← ha] + simp only; omega + · rintro s3 ⟨hR3, hl3', hp3, hs3⟩ + simp only [lift, bind_ok, WP.spec_ok] + exact hcont s3 hR3 hl3' hp3 hs3 + · rename_i hT + simp only [WP.spec_ok] + exact fin fun h => hT h.2 + · rename_i hA + simp only [WP.spec_ok] + exact fin fun h => hA h.1 + · exact ⟨RingAtA.refl hx.wf, hpos, rfl, hu, by rw [h00]⟩ + +/-! ## The insufficient-head loop -/ + +theorem refillHeadInsufficientWith_eqA (minimum : Fl4SpecA64.Ticks) {σ : Fl4SpecA64.State Unit} {p : Fl4SpecA64.ObjRef} + {x : Fl4SpecA64.SchedContext} {n : Nat} (hk : σ.kheap p = some (.schedContext x n)) (hne : x.scRefills ≠ []) : + Fl4SpecA64.refillHeadInsufficientWith (Z := Unit) minimum p σ = + some (decide ((Fl4SpecA64.refillHd x).rAmount < minimum)) := by + show Option.bind (Fl4SpecA64.readRefillHead p σ) (fun h => Fl4Spec.Lookup.oreturn _ σ) = _ + rw [readRefillHead_eqA hk hne]; rfl + +/-- `merge_nonoverlapping_head_refill` on the list (x86 `mergeL`). -/ +def mergeLA (rs : List sporadic.Refill) : List sporadic.Refill := + ⟨core.num.U64.wrapping_sub (rs.tail.headD zRA).time (rs.headD zRA).amount, + core.num.U64.wrapping_add (rs.tail.headD zRA).amount (rs.headD zRA).amount⟩ :: rs.tail.tail + +/-- `merge_nonoverlapping_head_refill` on a related SC (x86 `mergeNH_eq`). -/ +theorem mergeNH_eqA {σ : Fl4SpecA64.State Unit} {p : Fl4SpecA64.ObjRef} {x : Fl4SpecA64.SchedContext} {n : Nat} + (hk : σ.kheap p = some (.schedContext x n)) {rs : List sporadic.Refill} (hx : x.scRefills = rs.map toRefillA) + (h2 : 2 ≤ rs.length) : + Fl4SpecA64.mergeNonoverlappingHeadRefill (Z := Unit) p σ = + ({((), updKSc σ p (.schedContext { x with scRefills := (mergeLA rs).map toRefillA } n))}, False) := by + obtain ⟨a, b, l, rfl⟩ : ∃ a b l, rs = a :: b :: l := by + match rs, h2 with + | a :: b :: l, _ => exact ⟨a, b, l, rfl⟩ + have hne' : x.scRefills ≠ [] := by rw [hx]; simp + unfold Fl4SpecA64.mergeNonoverlappingHeadRefill + rw [nd_bind_oneSc (refillPopHead_eqA hk hne'), nd_bind_oneSc (updateRefillHd_eqA (updKSc_same _ _ _) _), + updateRefillHd_eqA (updKSc_same _ _ _), updKSc_updKSc, updKSc_updKSc] + congr 4 + simp [hx, mergeLA, Fl4SpecA64.refillHd, Fl4SpecA64.the, toRefillA, core.num.U64.wrapping_add_bv_eq, + core.num.U64.wrapping_sub_bv_eq] + +/-- **The insufficient-head loop refines `head_insufficient_loop`** (x86 `loop1_refines`), for positive amounts +whose sum is at least the minimum budget. -/ +theorem loop1_refinesWithA {s0 s1 : S} {σ0 : Fl4SpecA64.State Unit} {p : Fl4SpecA64.ObjRef} {sc : U64} + {x : Fl4SpecA64.SchedContext} {n : Nat} + (h1 : RingAtA st s0 s1 sc n) (hpos : PosA (L.refills s1 sc)) (hsum : lsumA (L.refills s1 sc) < 2 ^ 64) + (minimum : Fl4SpecA64.Ticks) (mb : U64) (hmb : mb.bv = minimum) (hmin : mb.val ≤ lsumA (L.refills s1 sc)) + (fuel2 : U64) (hf : (L.refills s1 sc).length ≤ fuel2.val) : + sporadic.budget_check_loop1 st s1 sc mb fuel2 ⦃ s2 => RingAtA st s0 s2 sc n ∧ + PosA (LawfulStore.refills (st := st) s2 sc) ∧ + lsumA (LawfulStore.refills (st := st) s2 sc) = lsumA (L.refills s1 sc) ∧ + ¬ ((LawfulStore.refills (st := st) s2 sc).headD zRA).amount < mb ∧ + Fl4SpecA64.headInsufficientLoopWith (Z := Unit) minimum p (specAtA (st := st) σ0 p sc x n s1) = + ({((), specAtA (st := st) σ0 p sc x n s2)}, False) ⦄ := by + have hkk : ∀ s : S, (specAtA (st := st) σ0 p sc x n s).kheap p = + some (.schedContext { x with scRefills := (L.refills s sc).map toRefillA } n) := fun s => updKSc_same _ _ _ + unfold sporadic.budget_check_loop1 + apply loop.spec_decr_nat (measure := fun y => y.2.val) + (inv := fun y => RingAtA st s0 y.1 sc n ∧ PosA (L.refills y.1 sc) ∧ + lsumA (L.refills y.1 sc) = lsumA (L.refills s1 sc) ∧ (L.refills y.1 sc).length ≤ y.2.val ∧ + Fl4SpecA64.headInsufficientLoopWith (Z := Unit) minimum p (specAtA (st := st) σ0 p sc x n s1) = + Fl4SpecA64.headInsufficientLoopWith (Z := Unit) minimum p (specAtA (st := st) σ0 p sc x n y.1)) + · rintro ⟨x0, x1⟩ ⟨hR, hP, hS, hle, hsp⟩ + dsimp only at hR hP hS hle hsp + have hW := hR.wf + have hne := refills_neL hW + have hne' : ((L.refills x0 sc).map toRefillA) ≠ [] := by simpa using hne + set hd := (L.refills x0 sc).headD zRA with hhd + have hC : Fl4SpecA64.the (Fl4SpecA64.refillHeadInsufficientWith (Z := Unit) minimum p + (specAtA (st := st) σ0 p sc x n x0)) = true ↔ hd.amount < mb := by + rw [refillHeadInsufficientWith_eqA minimum (hkk x0) hne', refillHd_mapA rfl hne, the_someA, decide_eq_true_iff, ← hhd] + simp only [toRefillA, ← hmb, ← u64_lt_bvA] + have fin : ¬ hd.amount < mb → RingAtA st s0 x0 sc n ∧ PosA (L.refills x0 sc) ∧ + lsumA (L.refills x0 sc) = lsumA (L.refills s1 sc) ∧ ¬ ((L.refills x0 sc).headD zRA).amount < mb ∧ + Fl4SpecA64.headInsufficientLoopWith (Z := Unit) minimum p (specAtA (st := st) σ0 p sc x n s1) = + ({((), specAtA (st := st) σ0 p sc x n x0)}, False) := fun hnc => ⟨hR, hP, hS, by rw [← hhd]; exact hnc, by + rw [hsp]; unfold Fl4SpecA64.headInsufficientLoopWith + exact wl_doneSc (C := fun _ s => Fl4SpecA64.the (Fl4SpecA64.refillHeadInsufficientWith (Z := Unit) minimum p s) = true) + (B := fun _ => Fl4SpecA64.mergeNonoverlappingHeadRefill p) (fun h => hnc (hC.1 h))⟩ + unfold sporadic.budget_check_loop1.body + refine WP.spec_bind (sporadic.head.spec _ _) ?_ + rintro r r_post + rw [head_eqA hW, ← hhd] at r_post + subst r_post + split + · rename_i hlt + refine WP.spec_bind (sporadic.single.spec _ _) ?_ + rintro b b_post + split + · rename_i hb + exfalso + have h1' : (L.refills x0 sc).length = 1 := (single_iffA hW).1 (by rw [b_post] at hb; simpa using hb) + obtain ⟨a, l, e⟩ := List.exists_cons_of_ne_nil hne + have hl : l = [] := by rw [e] at h1'; simpa using h1' + subst hl + have ha : hd = a := by rw [hhd, e]; rfl + have : lsumA (L.refills x0 sc) = a.amount.val := by rw [e]; simp [lsumA] + rw [UScalar.lt_equiv] at hlt + rw [ha] at hlt; omega + · rename_i hb + have hneq : L.rhead x0 sc ≠ L.rtail x0 sc := by rw [b_post] at hb; simpa using hb + have h2 : (L.refills x0 sc).length ≠ 1 := fun e => hneq ((single_iffA hW).2 e) + split + · rename_i hfp + step as ⟨fuel21, fuel21_post⟩ + have hx1 : 0 < x1.val := by have := hfp; rw [GT.gt, UScalar.lt_equiv] at this; simpa using this + obtain ⟨a, c, l, e⟩ : ∃ a c l, L.refills x0 sc = a :: c :: l := by + match h : L.refills x0 sc with + | [] => exact absurd h hne + | [a] => exact absurd (by rw [h]; rfl) h2 + | a :: c :: l => exact ⟨a, c, l, rfl⟩ + have ha : hd = a := by rw [hhd, e]; rfl + have hno : c.amount.val + a.amount.val < 2 ^ 64 := by + have := hS; rw [e, lsumA_cons, lsumA_cons] at this; omega + refine WP.spec_bind (pop_head_ringA hR hneq) ?_ + rintro ⟨old, s2⟩ ⟨hold, hR2, hl2⟩ + rw [← hhd] at hold + subst hold + have htl : L.refills s2 sc = c :: l := by rw [hl2, e]; rfl + refine WP.spec_bind (sporadic.head.spec _ _) ?_ + rintro h h_post + rw [head_eqA hR2.wf, htl] at h_post + simp only [List.headD_cons] at h_post + subst h + step as ⟨i1, i1_post⟩ + step as ⟨i2, i2_post⟩ + refine WP.spec_bind (set_head_ringA hR2 _) ?_ + rintro s3 ⟨hR3, hl3⟩ + have hl3' : L.refills s3 sc = mergeLA (L.refills x0 sc) := by + rw [hl3, htl, e]; simp only [mergeLA, List.tail_cons, List.headD_cons, i1_post, i2_post, ← ha] + simp only [WP.spec_ok] + refine ⟨hR3, ?_, ?_, ?_, ?_, ?_⟩ + · rw [hl3']; intro q hq + rw [e] at hq; simp only [mergeLA, List.tail_cons, List.headD_cons, List.mem_cons] at hq + rcases hq with rfl | hq + · simp only; rw [wadd_val hno]; have := hP c (by rw [e]; simp); omega + · exact hP q (by rw [e]; simp [hq]) + · rw [hl3', ← hS, e]; simp only [mergeLA, List.tail_cons, List.headD_cons, lsumA_cons] + rw [wadd_val hno]; omega + · rw [hl3', e, fuel21_post]; rw [e] at hle + simp only [mergeLA, List.tail_cons, List.length_cons] at hle ⊢ + omega + · rw [hsp]; unfold Fl4SpecA64.headInsufficientLoopWith + rw [wl_stepSc (C := fun _ s => Fl4SpecA64.the (Fl4SpecA64.refillHeadInsufficientWith (Z := Unit) minimum p s) = true) + (B := fun _ => Fl4SpecA64.mergeNonoverlappingHeadRefill p) (hC.2 hlt) + (mergeNH_eqA (hkk x0) rfl (by rw [e]; simp)), updKSc_updKSc] + show _ = Fl4Spec.NonDet.whileLoop _ _ () + (updKSc σ0 p (.schedContext { x with scRefills := (L.refills s3 sc).map toRefillA } n)) + rw [hl3'] + · rw [fuel21_post]; omega + · rename_i hfp + exfalso + have : x1.val = 0 := by + rw [GT.gt, UScalar.lt_equiv] at hfp; simp at hfp; omega + have := hW.size_pos; rw [← refills_lengthA] at this; omega + · rename_i hlt + simp only [WP.spec_ok] + exact fin hlt + · exact ⟨h1, hpos, rfl, hf, rfl⟩ + +end + +/-! ## `refill_budget_check` (x86 `ScBudgetTop`) -/ + +/-- The spec's `refill_budget_check` after `handle_overrun` (x86 `specTail`). -/ +def specTailWithA (limit minimum : Fl4SpecA64.Ticks) (scPtr : Fl4SpecA64.ObjRef) (usage' : Fl4SpecA64.Ticks) : Fl4SpecA64.SMonad Unit Unit := do + let head ← Fl4SpecA64.getRefillHead scPtr + Fl4Spec.NonDet.when (usage' > 0 ∧ head.rTime < limit) <| do + let sc ← Fl4SpecA64.getSchedContext scPtr + let used ← pure { rTime := head.rTime + sc.scPeriod, rAmount := usage' : Fl4SpecA64.Refill } + Fl4SpecA64.updateRefillHd scPtr fun r => { r with rAmount := head.rAmount - usage' } + Fl4SpecA64.updateRefillHd scPtr fun r => { r with rTime := head.rTime + usage' } + Fl4SpecA64.scheduleUsed scPtr used + Fl4SpecA64.headInsufficientLoopWith minimum scPtr + +theorem refillBudgetCheckWith_eqA (limit minimum : Fl4SpecA64.Ticks) {σ : Fl4SpecA64.State Unit} {p : Fl4SpecA64.ObjRef} {x : Fl4SpecA64.SchedContext} + {n : Nat} (hcur : σ.curSc = p) (hk : σ.kheap p = some (.schedContext x n)) (hrr : x.scPeriod ≠ 0) + (u : Fl4SpecA64.Ticks) : + Fl4SpecA64.refillBudgetCheckWith (Z := Unit) limit minimum u σ = (Fl4SpecA64.handleOverrunWith limit p u >>= fun u' => specTailWithA limit minimum p u') σ := by + unfold Fl4SpecA64.refillBudgetCheckWith + rw [nd_gets_bindSc, hcur, nd_bind_oneSc (isRoundRobin_eqA hk), + nd_bind_oneSc (assert_trueSc (by simpa using hrr) σ)] + rfl + +/-- The charge after the overrun loop, on the lists (x86 `postBody_eq`). -/ +theorem postBody_eqA {σ : Fl4SpecA64.State Unit} {p : Fl4SpecA64.ObjRef} {y : Fl4SpecA64.SchedContext} {n : Nat} + (hk : σ.kheap p = some (.schedContext y n)) {hd : sporadic.Refill} {tl : List sporadic.Refill} + (hx : y.scRefills = (hd :: tl).map toRefillA) {per : U64} (hper : y.scPeriod = per.bv) + (head : Fl4SpecA64.Refill) (hh : head = toRefillA hd) (u : U64) : + (do + let sc ← Fl4SpecA64.getSchedContext p + let used ← pure { rTime := head.rTime + sc.scPeriod, rAmount := u.bv : Fl4SpecA64.Refill } + Fl4SpecA64.updateRefillHd p fun r => { r with rAmount := head.rAmount - u.bv } + Fl4SpecA64.updateRefillHd p fun r => { r with rTime := head.rTime + u.bv } + Fl4SpecA64.scheduleUsed (Z := Unit) p used) σ = + ({((), updKSc σ p (.schedContext { y with scRefills := (schedUsedLA y.scRefillMax + (⟨core.num.U64.wrapping_add hd.time u, core.num.U64.wrapping_sub hd.amount u⟩ :: tl) + ⟨core.num.U64.wrapping_add hd.time per, u⟩).map toRefillA } n))}, False) := by + rw [nd_bind_oneSc (getSchedContext_eqA hk), nd_pure_bindSc, nd_bind_oneSc (updateRefillHd_eqA hk _)] + rw [nd_bind_oneSc (updateRefillHd_eqA (updKSc_same _ _ _) _), updKSc_updKSc] + have hused : ({ rTime := head.rTime + y.scPeriod, rAmount := u.bv } : Fl4SpecA64.Refill) = + toRefillA ⟨core.num.U64.wrapping_add hd.time per, u⟩ := by + rw [hh, hper]; simp [toRefillA, core.num.U64.wrapping_add_bv_eq] + rw [hused, scheduleUsed_eqA (updKSc_same _ _ _) + (rs := ⟨core.num.U64.wrapping_add hd.time u, core.num.U64.wrapping_sub hd.amount u⟩ :: tl) + (by simp [hx, hh, toRefillA, Fl4SpecA64.the, core.num.U64.wrapping_add_bv_eq, core.num.U64.wrapping_sub_bv_eq]) + (by simp), updKSc_updKSc] + +/-- What `refill_budget_check` leaves (x86 `BudgetPost`): the SC at `p` with new refills, non-empty, positive, +summing below `2 ^ 64`, the head holding at least `MIN_BUDGET`. -/ +def BudgetPostWithA (minimum : Fl4SpecA64.Ticks) (σ σ' : Fl4SpecA64.State Unit) (p : Fl4SpecA64.ObjRef) : Prop := + ∀ y m, σ.kheap p = some (.schedContext y m) → ∃ y', σ' = updKSc σ p (.schedContext y' m) ∧ + y' = { y with scRefills := y'.scRefills } ∧ y'.scRefills ≠ [] ∧ + (∀ r ∈ y'.scRefills, 0 < r.rAmount.toNat) ∧ (y'.scRefills.map fun r => r.rAmount.toNat).sum < 2 ^ 64 ∧ + minimum ≤ (Fl4SpecA64.refillHd y').rAmount + +abbrev BudgetPostA := BudgetPostWithA Fl4SpecA64.MIN_BUDGET + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +/-- **`budget_check` refines `refill_budget_check`** (x86 `budget_check_refines`, on `ScStateRelA`). -/ +theorem budget_check_refinesWithA (limit minimum : Fl4SpecA64.Ticks) {th : TypedHeapA} {ksb : U64} {C : Set U64} {m : Fl4SpecA64.McState} {s : S} (a : A) + {σ : Fl4SpecA64.State Unit} (h : ScStateRelA st C m s σ) (P : ScPlacedA th ksb C m σ) {sc : U64} + (hcs : L.cscW s = sc) (hcur : σ.curSc = scNameA m sc) (hc : sc ∈ C) {x : Fl4SpecA64.SchedContext} {n : Nat} + (hk : σ.kheap (scNameA m sc) = some (.schedContext x n)) (ha : Fl4SpecA64.scActive x = true) + (hmrt : sporadic.max_release_time ar a ⦃ mrt => mrt.bv = limit ⦄) + (hmb : sporadic.min_budget ar a ⦃ mb => mb.bv = minimum ⦄) + (hrr : L.rperiod s sc ≠ 0#u64) (hpos : PosA (L.refills s sc)) (hsum : lsumA (L.refills s sc) < 2 ^ 64) + (hmin : minimum.toNat ≤ lsumA (L.refills s sc)) (usage : U64) : + sporadic.budget_check st ar s a usage ⦃ s' => ∃ σ', Fl4SpecA64.refillBudgetCheckWith (Z := Unit) limit minimum usage.bv σ = + ({((), σ')}, False) ∧ ScStateRelA st C m s' σ' ∧ ScPlacedA th ksb C m σ' ∧ WritesIn st s s' (ScObjA sc n) ∧ + BudgetPostWithA minimum σ σ' (scNameA m sc) ∧ RingAtA st s s' sc n ⦄ := by + obtain ⟨x', n', hk', hxw⟩ := h.sc sc hc + rw [hk] at hk'; cases hk' + have hx := hxw.strong ha + have hrr' : x.scPeriod ≠ 0 := by + rw [hx.period]; intro e; exact hrr (eq_of_bv (by rw [e]; rfl)) + have hkk : ∀ s' : S, (specAtA (st := st) σ (scNameA m sc) sc x n s').kheap (scNameA m sc) = + some (.schedContext { x with scRefills := (L.refills s' sc).map toRefillA } n) := fun s' => updKSc_same _ _ _ + have hspec := refillBudgetCheckWith_eqA limit minimum (σ := σ) hcur hk hrr' usage.bv + unfold sporadic.budget_check + refine WP.spec_bind (LawfulStore.cur_cpu_spec s) ?_ + rintro i rfl + refine WP.spec_bind (obj.node_get.spec s _ _) ?_ + rintro i1 hi1 + obtain rfl : sc = i1 := (hi1.trans hcs).symm + refine WP.spec_bind hmrt ?_ + intro mrt hmrt' + refine WP.spec_bind (obj.Sc.period.spec s sc) ?_ + rintro period period_post + have hu : usage.val ≤ layout.WALK_FUEL.val := by + have := usage.hBounds; rw [walk_fuel_val]; simp [U64.size] at this ⊢; omega + refine WP.spec_bind (loop0_refinesWithA hk hx limit mrt period hmrt' period_post.symm hpos hsum usage _ hu) ?_ + rintro ⟨s1, u1⟩ ⟨hR1, hP1, hS1, hex, hov⟩ + have hW1 := hR1.wf + have hne1 := refills_neL hW1 + refine WP.spec_bind (sporadic.head.spec _ _) ?_ + rintro hh0 h_post + rw [head_eqA hW1] at h_post + subst h_post + have hne1' : ((L.refills s1 sc).map toRefillA) ≠ [] := by simpa using hne1 + obtain ⟨q, l, e⟩ := List.exists_cons_of_ne_nil hne1 + generalize hhd : (L.refills s1 sc).headD zRA = hd at * + have hq : hd = q := by rw [← hhd, e]; rfl + have hdm : hd ∈ L.refills s1 sc := by rw [hq, e]; exact List.mem_cons_self + have hcond : (u1.bv > 0 ∧ (toRefillA hd).rTime < limit) ↔ (u1 > 0#u64 ∧ hd.time < mrt) := by + simp only [toRefillA, ← hmrt', ← u64_lt_bvA, GT.gt] + rw [u64_lt_bvA]; rfl + -- the charge after the overrun loop + refine WP.spec_bind (Pₘ := fun s2 => RingAtA st s s2 sc n ∧ PosA (L.refills s2 sc) ∧ + lsumA (L.refills s2 sc) = lsumA (L.refills s sc) ∧ + specTailWithA limit minimum (scNameA m sc) u1.bv (specAtA (st := st) σ (scNameA m sc) sc x n s1) = + Fl4SpecA64.headInsufficientLoopWith (Z := Unit) minimum (scNameA m sc) + (specAtA (st := st) σ (scNameA m sc) sc x n s2)) ?_ ?_ + · by_cases hc1 : u1 > 0#u64 ∧ hd.time < mrt + · have hgt : hd.amount > u1 := by + by_contra hle + refine hex ⟨?_, hc1.2⟩ + rw [GT.gt, UScalar.lt_equiv] at hle; rw [UScalar.le_equiv]; omega + simp only [hc1.1, hc1.2, hgt, ↓reduceIte] + step as ⟨i2, i2_post⟩ + step as ⟨i3, i3_post⟩ + step as ⟨i4, i4_post⟩ + refine WP.spec_bind (set_head_ringA hR1 _) ?_ + rintro s3 ⟨hR3, hl3⟩ + refine WP.spec_mono (schedule_used_ringA hR3 _) ?_ + rintro s4 ⟨hR4, hl4⟩ + have hgt' : u1.val < hd.amount.val := by rw [GT.gt, UScalar.lt_equiv] at hgt; exact hgt + have hu0 : 0 < u1.val := by have := hc1.1; rw [GT.gt, UScalar.lt_equiv] at this; simpa using this + have hi4 : i4.val = hd.amount.val - u1.val := by rw [i4_post]; exact wsub_val (by omega) + have hl3' : L.refills s3 sc = ⟨i3, i4⟩ :: l := by rw [hl3, e]; rfl + have hS1' : lsumA (L.refills s1 sc) = hd.amount.val + lsumA l := by rw [e, lsumA_cons, hq] + have hno : ((⟨i3, i4⟩ :: l : List sporadic.Refill).getLastD zRA).amount.val + u1.val < 2 ^ 64 := by + by_cases hl : l = [] + · subst hl; simp only [List.getLastD_cons, List.getLastD_nil] + rw [hi4]; have := le_lsumA hdm; rw [hS1] at this; omega + · rw [getLastD_eqA (by simp), List.getLast_cons hl] + have := le_lsumA (rs := l) (List.getLast_mem hl) + rw [hS1] at hS1'; omega + refine ⟨hR4, ?_, ?_, ?_⟩ + · rw [hl4, hl3'] + refine pos_schedUsedLA (fun r hr => ?_) (by simp) hno hu0 + rcases List.mem_cons.1 hr with rfl | hr + · show 0 < i4.val; omega + · exact hP1 r (by rw [e]; exact List.mem_cons_of_mem _ hr) + · rw [hl4, hl3', lsum_schedUsedLA (by simp) hno, lsumA_cons, ← hS1, hS1'] + simp only; omega + · unfold specTailWithA + rw [nd_bind_oneSc (getRefillHead_eqA (hkk s1) hne1'), refillHd_mapA rfl hne1, hhd, Fl4Spec.NonDet.when, + if_pos (hcond.2 hc1), nd_bind_oneSc (postBody_eqA (hkk s1) (hd := q) (tl := l) (by rw [e]) + (per := period) (by rw [hx.period, period_post]) (toRefillA hd) (by rw [hq]) u1), updKSc_updKSc] + show Fl4SpecA64.headInsufficientLoopWith minimum (scNameA m sc) + (updKSc σ (scNameA m sc) (.schedContext { x with scRefills := _ } n)) = + Fl4SpecA64.headInsufficientLoopWith minimum (scNameA m sc) + (updKSc σ (scNameA m sc) (.schedContext { x with scRefills := (L.refills s4 sc).map toRefillA } n)) + rw [hl4, hl3', hR3.mx, hx.max, i2_post, i3_post, i4_post, hq] + · have hfin : specTailWithA limit minimum (scNameA m sc) u1.bv (specAtA (st := st) σ (scNameA m sc) sc x n s1) = + Fl4SpecA64.headInsufficientLoopWith (Z := Unit) minimum (scNameA m sc) + (specAtA (st := st) σ (scNameA m sc) sc x n s1) := by + unfold specTailWithA + rw [nd_bind_oneSc (getRefillHead_eqA (hkk s1) hne1'), refillHd_mapA rfl hne1, hhd, Fl4Spec.NonDet.when, + if_neg (fun h => hc1 (hcond.1 h))] + exact nd_pure_bindSc _ _ _ + by_cases hu0 : u1 > 0#u64 + · have ht' : ¬ hd.time < mrt := fun h => hc1 ⟨hu0, h⟩ + rw [if_pos hu0, if_neg ht'] + exact (WP.spec_ok _).2 ⟨hR1, hP1, hS1, hfin⟩ + · rw [if_neg hu0] + exact (WP.spec_ok _).2 ⟨hR1, hP1, hS1, hfin⟩ + · rintro s2 ⟨hR2, hP2, hS2, ht2⟩ + refine WP.spec_bind hmb ?_ + intro mb hmb' + refine WP.spec_bind (obj.Sc.refill_max.spec s2 sc) ?_ + rintro i2 i2_post + step as ⟨fuel2, fuel2_post⟩ + have hf2 : (L.refills s2 sc).length ≤ fuel2.val := by + have h1 := hR2.wf.size_le; have h2 := hR2.wf.le_abs + have h3 := absMax_lt (n + Fl4SpecA64.minSchedContextBits) + rw [fuel2_post, refills_lengthA, wadd_val (by rw [i2_post]; simp; omega), i2_post]; simp; omega + have hmbv : mb.val = minimum.toNat := by rw [← hmb']; rfl + refine WP.spec_mono (loop1_refinesWithA (σ0 := σ) (p := scNameA m sc) (x := x) hR2 hP2 (by rw [hS2]; exact hsum) minimum mb + hmb' (by rw [hS2, hmbv]; exact hmin) fuel2 hf2) ?_ + rintro s3 ⟨hR3, hp3, hs3, hm3, hl⟩ + obtain ⟨hS3, hP3, hw3⟩ := h.ringAtA P hc hk hx hR3 + refine ⟨_, by rw [hspec, nd_bind_oneSc hov, ht2, hl], hS3, hP3, hw3, (fun y m' hy => ?_), hR3⟩ + rw [hk] at hy; cases hy + have hne3 := refills_neL hR3.wf + refine ⟨_, rfl, rfl, by simpa using hne3, fun r hr => ?_, ?_, ?_⟩ + · obtain ⟨q, hq, rfl⟩ := List.mem_map.1 hr + simpa [toRefillA] using hp3 q hq + · rw [← lsumA_eq_map, hs3, hS2]; exact hsum + · obtain ⟨q, l, e⟩ := List.exists_cons_of_ne_nil hne3 + have hh : Fl4SpecA64.refillHd { x with scRefills := (L.refills s3 sc).map toRefillA } = toRefillA q := by + unfold Fl4SpecA64.refillHd; simp [e]; rfl + rw [hh, ← hmb'] + rw [e] at hm3; simp only [List.headD_cons] at hm3 + rw [u64_lt_bvA] at hm3 + show mb.bv ≤ q.amount.bv + rw [BitVec.le_def]; rw [BitVec.lt_def] at hm3; omega + + +/-- The fixed-constant variant used by the existing l4v refinement. -/ +theorem budget_check_refinesA {th : TypedHeapA} {ksb : U64} {C : Set U64} {m : Fl4SpecA64.McState} {s : S} (a : A) + {σ : Fl4SpecA64.State Unit} (h : ScStateRelA st C m s σ) (P : ScPlacedA th ksb C m σ) {sc : U64} + (hcs : L.cscW s = sc) (hcur : σ.curSc = scNameA m sc) (hc : sc ∈ C) {x : Fl4SpecA64.SchedContext} {n : Nat} + (hk : σ.kheap (scNameA m sc) = some (.schedContext x n)) (ha : Fl4SpecA64.scActive x = true) + (hmrt : sporadic.max_release_time ar a ⦃ mrt => mrt.bv = Fl4SpecA64.MAX_RELEASE_TIME ⦄) + (hmb : sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hrr : L.rperiod s sc ≠ 0#u64) (hpos : PosA (L.refills s sc)) (hsum : lsumA (L.refills s sc) < 2 ^ 64) + (hmin : Fl4SpecA64.MIN_BUDGET.toNat ≤ lsumA (L.refills s sc)) (usage : U64) : + sporadic.budget_check st ar s a usage ⦃ s' => ∃ σ', Fl4SpecA64.refillBudgetCheck (Z := Unit) usage.bv σ = + ({((), σ')}, False) ∧ ScStateRelA st C m s' σ' ∧ ScPlacedA th ksb C m σ' ∧ WritesIn st s s' (ScObjA sc n) ∧ + BudgetPostA σ σ' (scNameA m sc) ⦄ := by + refine WP.spec_mono + (budget_check_refinesWithA Fl4SpecA64.MAX_RELEASE_TIME Fl4SpecA64.MIN_BUDGET + a h P hcs hcur hc hk ha hmrt hmb hrr hpos hsum hmin usage) ?_ + rintro s' ⟨σ', he, hrel, hplaced, hw, hp, _⟩ + exact ⟨σ', he, hrel, hplaced, hw, hp⟩ + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScBudgetTopA.lean b/proofs/Fl4ProofsA64/P3/ScBudgetTopA.lean new file mode 100644 index 000000000..c989150c9 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScBudgetTopA.lean @@ -0,0 +1,304 @@ +import Fl4ProofsA64.P3.ScEntryBudgetA + +/-! The raw-clock budget prefix at syscall and interrupt entry. The +clock, FL4 timer limits and single domain are [P] agreements. +The entry invariants and the timeout CSpace/IPC leaf are [J] facts. -/ + +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3.Sched + +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +/-- The clock survives the check, and successful checks establish offset +capacity. A failed check clears consumption and either requests a new +thread or keeps a nonrunnable current thread. -/ +structure CheckBudgetPostA (Clock : A → MachineState → Prop) + (a : A) (σ : State Unit) (b : Bool) (τ : State Unit) : Prop where + clock : Clock a τ.machineState + machine : τ.machineState = σ.machineState + time : τ.curTime = σ.curTime + idle : ActIdleA τ + success : b = true → τ = σ ∧ CurInvA τ ∧ OffReadyA τ ∧ OffOKA τ + failure : b = false → τ.consumedTime = 0 ∧ (τ.schedulerAction = .chooseNewThread ∨ + CurInvA τ ∧ ∀ t, τ.kheap τ.curThread = some (.tcb t) → runnable t.tcbState ≠ true) + +set_option maxRecDepth 4096 in +theorem check_budget_entry_refinesA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : YieldQueueRelA (st := st) Z m κ s σ) (I : CurInvA σ) (R : OffReadyA σ) (Idle : ActIdleA σ) + (P : EntryBudgetPlaceA st m κ s σ) (a : A) {Clock : A → MachineState → Prop} + (clk : Clock a σ.machineState) (T : EntryTimeoutFromA (st := st) (ar := ar) Z m κ Clock s σ) + (hag : fl4MinBudget = MIN_BUDGET) (hMin : Fl4MinBudgetAgreeA ar a) + (hLimit : sporadic.max_release_time ar a ⦃ v => v.bv = fl4MaxReleaseTime ⦄) + (hnd : numDomains = 1) : + sched.check_budget st ar s a ⦃ r => ∃ τ Q' R', + fl4CheckBudget (Z := Unit) σ = ({(r.1, τ)}, False) ∧ + YieldQueueRelA (st := st) { Z with Q := Q', R := R' } m κ r.2.1 τ ∧ + CheckBudgetPostA Clock r.2.2 σ r.1 τ ⦄ := by + let used := L.mem s (L.nodeA s layout.node.CONSUMED) + have hUsed : σ.consumedTime = used.bv := H.sched.rest.consumed + unfold sched.check_budget + rw [eq_of_spec_fd (sched.cur_sc.spec s), bind_ok, eq_of_spec_fd (sched.consumed.specA s), bind_ok] + have dec := entryBudgetDecisionA H.sched.toSchedSRN I P.cores a hMin + rw [eq_of_spec_fd (sched.cur_sc.spec s), bind_ok, eq_of_spec_fd (sched.consumed.specA s), bind_ok] at dec + refine WP.spec_bind dec ?_ + rintro b ⟨e, good⟩ + have spec : + fl4CheckBudget (Z := Unit) σ = (if b then + NonDet.gets (fun s : State Unit => !isCurDomainExpired s) + else fl4ChargeBudget used.bv true >>= fun _ => (pure false : SMonad Unit Bool)) σ := by + unfold fl4CheckBudget + rw [nd_gets_bindSc, nd_gets_bindSc, nd_bind_oneSc e] + split_ifs + · rw [nd_gets_bindSc] + rfl + · rw [nd_gets_bindSc, hUsed] + cases b + · simp only [Bool.false_eq_true, ↓reduceIte] + refine WP.spec_bind (charge_budget_entry_refinesA H I Idle P a clk T hag hMin hLimit used true) ?_ + rintro ⟨s1, a1⟩ ⟨τ, Q', R', h, H', C, ht, hm, hc, hi, ha⟩ + change ok (false, s1, a1) ⦃ _ ⦄ + rw [WP.spec_ok] + refine ⟨τ, Q', R', ?_, H', ⟨C, hm, ht, hi, by simp, fun _ => ⟨hc, ha⟩⟩⟩ + rw [spec, if_neg (by decide : ¬ false = true), nd_bind_oneSc h] + rfl + · simp only [↓reduceIte, is_cur_domain_expired_eqA, bind_ok, WP.spec_ok] + refine ⟨σ, Z.Q, Z.R, ?_, H, ⟨clk, rfl, rfl, Idle, fun _ => ⟨rfl, I, R, good rfl R⟩, by simp⟩⟩ + rw [spec, if_pos rfl] + simp [NonDet.gets, NonDet.pure, isCurDomainExpired, hnd] + +private def budgetRestartA (result : Bool) : SMonad Unit Bool := do + let cur ← NonDet.gets fun s : State Unit => s.curThread + let st ← getThreadState cur + NonDet.when (¬result ∧ runnable st) <| setThreadState cur .restart + return result + +set_option maxRecDepth 4096 in +private theorem budgetRestart_refinesA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ τ : State Unit} + (H : YieldQueueRelA (st := st) Z m κ s τ) (a : A) {Clock : A → MachineState → Prop} (b : Bool) + (P : CheckBudgetPostA Clock a σ b τ) : + (do + let ct ← sched.cur_thread st s + if b then ok (b, s, a) + else + let runnable ← sched.is_runnable st s ct + if runnable then + let s' ← sched.set_thread_state st s ct sched.TS_RESTART + ok (b, s', a) + else ok (b, s, a)) ⦃ r => ∃ ω, + budgetRestartA b τ = ({(r.1, ω)}, False) ∧ + YieldQueueRelA (st := st) Z m κ r.2.1 ω ∧ CheckBudgetPostA Clock r.2.2 σ r.1 ω ⦄ := by + let ct := L.curW s + have ht := H.sched.curT + obtain ⟨x, hk, _⟩ := H.sched.sch.tcb ct ht + have hCt := H.sched.sch.curThread + have state : getThreadState (Z := Unit) τ.curThread τ = ({(x.tcbState, τ)}, False) := by + unfold getThreadState + rw [hCt, threadGet_eqSc _ hk] + have spec : + budgetRestartA b τ = + (NonDet.when (¬b ∧ runnable x.tcbState) (setThreadState τ.curThread .restart) >>= + fun _ => (pure b : SMonad Unit Bool)) τ := by + unfold budgetRestartA + rw [nd_gets_bindSc, nd_bind_oneSc state] + refine WP.spec_bind (sched.cur_thread.spec s) ?_ + intro t he + rw [show t = ct from he] + cases b + · simp only [Bool.false_eq_true, ↓reduceIte] + rw [eq_of_spec_fd (sched.is_runnable.spec (L := L) s ct), bind_ok] + have c := H.states ct ht x hk + have er : decide (L.mem s (stA ct) = sched.TS_RUNNING ∨ L.mem s (stA ct) = sched.TS_RESTART ∨ + L.mem s (stA ct) = sched.TS_RUNNING_VM) = runnable x.tcbState := by + apply Bool.eq_iff_iff.2 + rw [decide_eq_true_eq, runnable_iffA c] + rw [er] + by_cases hRun : runnable x.tcbState = true + · rw [if_pos hRun] + obtain ⟨zero, phase⟩ := P.failure rfl + have choose : τ.schedulerAction = .chooseNewThread := + phase.resolve_right (fun h => h.2 x (by rwa [hCt]) hRun) + have code : sched.TS_RESTART.val = tsCodeA .restart := by unfold sched.TS_RESTART tsCodeA; rfl + refine WP.spec_bind (yieldRunnableA H ht hk hRun (by rfl : runnable ThreadState.restart = true) code) ?_ + rintro s' ⟨ω, hω, E, hK, hct, hRel, _, _⟩ + rw [WP.spec_ok] + have eq : ω = updKSc τ (specPtr ct) (.tcb { x with tcbState := .restart }) := by + have eh := yieldSetStateSpecA hk .restart + have q := (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst (hω.symm.trans eh)))).2 + rw [q] + unfold stsActA + rw [if_neg (by + intro h + have e := h.2.1 + change τ.schedulerAction = .resumeCurThread at e + rw [choose] at e; cases e)] + have idle : ActIdleA ω := by + rw [eq] + obtain ⟨i, hi, hb, hs, hr⟩ := P.idle.2 + have ne : τ.idleThread ≠ specPtr ct := by + intro h + rw [h, hk] at hi; cases hi + rw [hs] at hRun; cases hRun + refine ⟨(fun u hu => by change τ.schedulerAction = _ at hu; rw [choose] at hu; cases hu), + ⟨i, ?_, hb, hs, hr⟩⟩ + change (updKSc τ (specPtr ct) _).kheap τ.idleThread = _ + rwa [updKSc_kheap, Function.update_of_ne ne] + refine ⟨ω, ?_, E, ⟨?_, ?_, ?_, idle, by simp, fun _ => ⟨?_, Or.inl ?_⟩⟩⟩ + · rw [spec, NonDet.when, if_pos ⟨by decide, hRun⟩, hCt, nd_bind_oneSc hω] + rfl + · rw [eq]; exact P.clock + · rw [eq]; exact P.machine + · rw [eq]; exact P.time + · rw [eq]; exact zero + · rw [eq]; exact choose + · rw [if_neg hRun, WP.spec_ok] + refine ⟨τ, ?_, H, P⟩ + rw [spec, NonDet.when, if_neg (fun h => hRun h.2), nd_bind_oneSc (show + NonDet.pure () τ = ({((), τ)}, False) from rfl)] + rfl + · simp only [↓reduceIte, WP.spec_ok] + refine ⟨τ, ?_, H, P⟩ + rw [spec, NonDet.when, if_neg (by simp), nd_bind_oneSc (show + NonDet.pure () τ = ({((), τ)}, False) from rfl)] + rfl + +set_option maxRecDepth 4096 in +theorem check_budget_restart_entry_refinesA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : YieldQueueRelA (st := st) Z m κ s σ) (I : CurInvA σ) (R : OffReadyA σ) (Idle : ActIdleA σ) + (P : EntryBudgetPlaceA st m κ s σ) (a : A) {Clock : A → MachineState → Prop} + (clk : Clock a σ.machineState) (T : EntryTimeoutFromA (st := st) (ar := ar) Z m κ Clock s σ) + (hag : fl4MinBudget = MIN_BUDGET) (hMin : Fl4MinBudgetAgreeA ar a) + (hLimit : sporadic.max_release_time ar a ⦃ v => v.bv = fl4MaxReleaseTime ⦄) + (hnd : numDomains = 1) : + sched.check_budget_restart st ar s a ⦃ r => ∃ τ Q' R', + fl4CheckBudgetRestart (Z := Unit) σ = ({(r.1, τ)}, False) ∧ + YieldQueueRelA (st := st) { Z with Q := Q', R := R' } m κ r.2.1 τ ∧ + CheckBudgetPostA Clock r.2.2 σ r.1 τ ⦄ := by + unfold sched.check_budget_restart + refine WP.spec_bind (check_budget_entry_refinesA H I R Idle P a clk T hag hMin hLimit hnd) ?_ + rintro ⟨b, s1, a1⟩ ⟨τ, Q', R', he, H1, post⟩ + simp only [uncurry_apply_pair] + refine WP.spec_mono (budgetRestart_refinesA H1 a1 b post) ?_ + rintro r ⟨ω, hω, H2, p⟩ + refine ⟨ω, Q', R', ?_, H2, p⟩ + change (fl4CheckBudget >>= budgetRestartA) σ = _ + rw [nd_bind_oneSc he] + exact hω + +set_option maxRecDepth 4096 in +private theorem timestamp_budget_prefixA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : YieldQueueRelA (st := st) Z m κ s σ) (I : CurInvA σ) (R : OffReadyA σ) (Idle : ActIdleA σ) + (P : EntryBudgetPlaceA st m κ s σ) (a : A) {Clock : A → MachineState → Prop} + (now : NowAgreeA ar Clock) (clk : Clock a σ.machineState) + (V : ValidMachineTimeA σ) (C : CounterStepA σ) + (T : EntryTimeoutFromA (st := st) (ar := ar) Z m κ Clock s σ) + (hag : fl4MinBudget = MIN_BUDGET) (hMin : ∀ a', Fl4MinBudgetAgreeA ar a') + (hLimit : ∀ a', sporadic.max_release_time ar a' ⦃ v => v.bv = fl4MaxReleaseTime ⦄) + (hnd : numDomains = 1) (restart : Bool) : + (do + let (s1, a1) ← sched.update_timestamp st ar s a + if restart then sched.check_budget_restart st ar s1 a1 else sched.check_budget st ar s1 a1) ⦃ r => + ∃ σ1 τ Q' R', + fl4UpdateTimestamp (Z := Unit) σ = ({((), σ1)}, False) ∧ + (if restart then fl4CheckBudgetRestart (Z := Unit) else fl4CheckBudget) σ1 = ({(r.1, τ)}, False) ∧ + YieldQueueRelA (st := st) { Z with Q := Q', R := R' } m κ r.2.1 τ ∧ + CheckBudgetPostA Clock r.2.2 σ1 r.1 τ ∧ ValidMachineTimeA τ ⦄ := by + refine WP.spec_bind (update_timestamp_refinesA H a now clk hnd V C I R Idle) ?_ + rintro ⟨s1, a1⟩ ⟨σ1, e1, H1, clk1, v1, i1, r1, idle1, _, _, w1⟩ + simp only [uncurry_apply_pair] + have eq : σ1 = + fl4TimestampState (rawNextA σ.machineState) { σ with machineState := rawStateA σ.machineState } := + (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst (e1.symm.trans (fl4UpdateTimestamp_eqA σ))))).2 + have hct : σ1.curThread = σ.curThread := by rw [eq]; rfl + have hsc : σ1.curSc = σ.curSc := by rw [eq]; rfl + have hrq : σ1.readyQueues = σ.readyQueues := by rw [eq]; rfl + have eSc : L.cscW s1 = L.cscW s := by + apply scRenA_inj m (H1.sched.sch.scOff _ H1.sched.cscC) (H.sched.sch.scOff _ H.sched.cscC) + exact H1.sched.currentScA.symm.trans (hsc.trans H.sched.currentScA) + have P1 : EntryBudgetPlaceA st m κ s1 σ1 := by + refine ⟨P.cores, ?_, ?_, ?_⟩ + · rw [eSc, H.sched.cpu_eq H1.sched] + exact P.scCore + · rw [hsc] + exact P.notIdle + · rw [hct, hrq] + exact P.notReady + have T1 : EntryTimeoutFromA (st := st) (ar := ar) Z m κ Clock s1 σ1 := + fun s' a' v w Hv Sv Mv Yv Cv tv run => + T s' a' v (w1.trans w rfl rfl) Hv Sv Mv Yv Cv (tv.trans hct) run + have collect (r : Bool × S × A) (τ : State Unit) (Q' : U64 → U64 → List U64) (R' : U64 → List U64) + (he : (if restart then fl4CheckBudgetRestart (Z := Unit) else fl4CheckBudget) σ1 = ({(r.1, τ)}, False)) + (Hv : YieldQueueRelA (st := st) { Z with Q := Q', R := R' } m κ r.2.1 τ) + (post : CheckBudgetPostA Clock r.2.2 σ1 r.1 τ) : + ∃ σ1 τ Q' R', + fl4UpdateTimestamp (Z := Unit) σ = ({((), σ1)}, False) ∧ + (if restart then fl4CheckBudgetRestart (Z := Unit) else fl4CheckBudget) σ1 = ({(r.1, τ)}, False) ∧ + YieldQueueRelA (st := st) { Z with Q := Q', R := R' } m κ r.2.1 τ ∧ + CheckBudgetPostA Clock r.2.2 σ1 r.1 τ ∧ ValidMachineTimeA τ := by + refine ⟨σ1, τ, Q', R', e1, he, Hv, post, ?_⟩ + unfold ValidMachineTimeA at v1 ⊢ + rw [post.time, post.machine] + exact v1 + cases restart + · simp only [Bool.false_eq_true, ↓reduceIte] + refine WP.spec_mono (check_budget_entry_refinesA H1 i1 r1 idle1 P1 a1 clk1 T1 hag (hMin _) (hLimit _) hnd) ?_ + rintro r ⟨τ, Q', R', he, Hv, p⟩ + exact collect r τ Q' R' he Hv p + · simp only [↓reduceIte] + refine WP.spec_mono (check_budget_restart_entry_refinesA H1 i1 r1 idle1 P1 a1 clk1 T1 hag (hMin _) (hLimit _) hnd) ?_ + rintro r ⟨τ, Q', R', he, Hv, p⟩ + exact collect r τ Q' R' he Hv p + +set_option maxRecDepth 4096 in +/-- The actual syscall and user-fault prefix, including Restart. -/ +theorem budget_ok_refinesA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : YieldQueueRelA (st := st) Z m κ s σ) (I : CurInvA σ) (R : OffReadyA σ) (Idle : ActIdleA σ) + (P : EntryBudgetPlaceA st m κ s σ) (a : A) {Clock : A → MachineState → Prop} + (now : NowAgreeA ar Clock) (clk : Clock a σ.machineState) + (V : ValidMachineTimeA σ) (C : CounterStepA σ) + (T : EntryTimeoutFromA (st := st) (ar := ar) Z m κ Clock s σ) + (hag : fl4MinBudget = MIN_BUDGET) (hMin : ∀ a', Fl4MinBudgetAgreeA ar a') + (hLimit : ∀ a', sporadic.max_release_time ar a' ⦃ v => v.bv = fl4MaxReleaseTime ⦄) + (hnd : numDomains = 1) : + syscall.budget_ok st ar s a ⦃ r => ∃ σ1 τ Q' R', + fl4BudgetOk (Z := Unit) σ = ({(r.1, τ)}, False) ∧ + fl4UpdateTimestamp (Z := Unit) σ = ({((), σ1)}, False) ∧ + YieldQueueRelA (st := st) { Z with Q := Q', R := R' } m κ r.2.1 τ ∧ + CheckBudgetPostA Clock r.2.2 σ1 r.1 τ ∧ ValidMachineTimeA τ ⦄ := by + unfold syscall.budget_ok + refine WP.spec_mono (by + simpa only [↓reduceIte] using timestamp_budget_prefixA H I R Idle P a now clk V C T hag hMin hLimit hnd true) ?_ + rintro r ⟨σ1, τ, Q', R', e1, e2, Hv, post, valid⟩ + refine ⟨σ1, τ, Q', R', ?_, e1, Hv, post, valid⟩ + unfold fl4BudgetOk + rw [nd_bind_oneSc e1] + exact e2 + +set_option maxRecDepth 4096 in +/-- An interrupt uses the same observation and light budget check. -/ +theorem irq_budget_prefix_refinesA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : YieldQueueRelA (st := st) Z m κ s σ) (I : CurInvA σ) (R : OffReadyA σ) (Idle : ActIdleA σ) + (P : EntryBudgetPlaceA st m κ s σ) (a : A) {Clock : A → MachineState → Prop} + (now : NowAgreeA ar Clock) (clk : Clock a σ.machineState) + (V : ValidMachineTimeA σ) (C : CounterStepA σ) + (T : EntryTimeoutFromA (st := st) (ar := ar) Z m κ Clock s σ) + (hag : fl4MinBudget = MIN_BUDGET) (hMin : ∀ a', Fl4MinBudgetAgreeA ar a') + (hLimit : ∀ a', sporadic.max_release_time ar a' ⦃ v => v.bv = fl4MaxReleaseTime ⦄) + (hnd : numDomains = 1) : + (do let (s1, a1) ← sched.update_timestamp st ar s a; sched.check_budget st ar s1 a1) ⦃ r => + ∃ σ1 τ Q' R', (fl4UpdateTimestamp >>= fun _ => fl4CheckBudget (Z := Unit)) σ = ({(r.1, τ)}, False) ∧ + fl4UpdateTimestamp (Z := Unit) σ = ({((), σ1)}, False) ∧ + YieldQueueRelA (st := st) { Z with Q := Q', R := R' } m κ r.2.1 τ ∧ + CheckBudgetPostA Clock r.2.2 σ1 r.1 τ ∧ ValidMachineTimeA τ ⦄ := by + refine WP.spec_mono (by + simpa only [Bool.false_eq_true, ↓reduceIte] using timestamp_budget_prefixA H I R Idle P a now clk V C T hag hMin hLimit hnd false) ?_ + rintro r ⟨σ1, τ, Q', R', e1, e2, Hv, post, valid⟩ + exact ⟨σ1, τ, Q', R', by rw [nd_bind_oneSc e1]; exact e2, e1, Hv, post, valid⟩ + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScCancelYieldA.lean b/proofs/Fl4ProofsA64/P3/ScCancelYieldA.lean new file mode 100644 index 000000000..74ad2e371 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScCancelYieldA.lean @@ -0,0 +1,101 @@ +/- +P3 (AArch64): `schedcontext::cancel_yield_to` refines l4v `sched_context_cancel_yield_to`. + +**`cancel_yield_to_refinesA`**: with the reciprocal yield relation `YieldRelA`, FL4's +`cancel_yield_to t` (clear the SC's `YIELD_FROM` then the TCB's `YIELD_TO`, when set) refines +`schedContextCancelYieldTo (specPtr t)`: one outcome, `SchedSRA`, `StEqA`, `McpRelA`, +`YieldRelA` and `SSzA` after (`yield_pair_wordsA` with `link := false`), the TCB's state, SC and +registers unchanged, the writes in `MWrA` or the TCB's yield word. + +No named premise. +-/ +import Fl4ProofsA64.P3.ScYieldCompleteA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4SpecA64 Fl4Spec + +noncomputable section +local notation "wa" => core.num.U64.wrapping_add + +private theorem yieldCancel_eqCA {σ : State Unit} {p q : ObjRef} {x : Tcb} {y : SchedContext} {n : Nat} + (hk : σ.kheap q = some (.tcb x)) (hs : σ.kheap p = some (.schedContext y n)) + (ht : x.tcbYieldTo = some p) : + schedContextCancelYieldTo (Z := Unit) q σ = ({((), yieldPairStA σ p q x y n false)}, False) := by + have hne : q ≠ p := fun e => by rw [e, hs] at hk; cases hk + unfold schedContextCancelYieldTo getTcbObjRef + rw [nd_bind_oneSc (threadGet_eqSc _ hk), ht] + change (updateSchedContext p _ >>= _) σ = _ + rw [nd_bind_oneSc (updateSchedContext_eqA hs _)] + exact threadSet_eqSc _ (by rwa [updKSc_kheap, Function.update_of_ne hne]) + +private theorem yieldCancel_noneCA {σ : State Unit} {q : ObjRef} {x : Tcb} + (hk : σ.kheap q = some (.tcb x)) (ht : x.tcbYieldTo = none) : + schedContextCancelYieldTo (Z := Unit) q σ = ({((), σ)}, False) := by + unfold schedContextCancelYieldTo getTcbObjRef + rw [nd_bind_oneSc (threadGet_eqSc _ hk), ht] + rfl + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **`cancel_yield_to` refines `sched_context_cancel_yield_to`** (module docstring). -/ +theorem cancel_yield_to_refinesA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRA st Z m κ s σ) (hst : StEqA Z.T (L.mem s) σ) + (M : McpRelA Z.T m (L.mem s) σ) (Y : YieldRelA Z.T Z.C m (L.mem s) σ) (hC0 : ∀ c ∈ Z.C, c ≠ 0#u64) + {t : U64} (ht : t ∈ Z.T) : + schedcontext.cancel_yield_to st s t ⦃ s' => ∃ σ', + schedContextCancelYieldTo (Z := Unit) (specPtr t) σ = ({((), σ')}, False) ∧ + SchedSRA st Z m κ s' σ' ∧ StEqA Z.T (L.mem s') σ' ∧ McpRelA Z.T m (L.mem s') σ' ∧ + YieldRelA Z.T Z.C m (L.mem s') σ' ∧ SSzA σ σ' ∧ + σ'.curThread = σ.curThread ∧ σ'.releaseQueue = σ.releaseQueue ∧ + σ'.readyQueues = σ.readyQueues ∧ σ'.schedulerAction = σ.schedulerAction ∧ + (∀ u x, σ.kheap u = some (.tcb x) → ∃ x', σ'.kheap u = some (.tcb x') ∧ + x' = { x with tcbYieldTo := x'.tcbYieldTo }) ∧ + (∀ u o, σ.kheap u = some o → (∀ y n, o ≠ .schedContext y n) → (∀ x, o ≠ .tcb x) → + σ'.kheap u = some o) ∧ + WritesIn st s s' (fun b => MWrA (st := st) Z m σ s b ∨ b = yieldA t) ⦄ := by + have ht0 : t ≠ 0#u64 := fun e => (H.core.tcbs t ht).ne0 (by rw [e]; rfl) + obtain ⟨x, hx, hxt⟩ := Y.tcb t ht + unfold schedcontext.cancel_yield_to + rw [eq_of_spec_fd (obj.Tcb.is_null.spec t), bind_ok, decide_eq_false ht0] + simp only [Bool.false_eq_true, ↓reduceIte] + rw [eq_of_spec_fd (obj.Tcb.yield_to.spec s t), bind_ok] + by_cases h0 : L.mem s (yieldA t) = 0#u64 + · rw [h0] + simp only [bne_self_eq_false, Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + have hn : x.tcbYieldTo = none := by rw [hxt, h0]; simp [optScA] + refine ⟨σ, yieldCancel_noneCA hx hn, H, hst, M, Y, SSzA.refl σ, rfl, rfl, rfl, rfl, + fun u x hu => ⟨x, hu, (by cases x; rfl : x = { x with tcbYieldTo := x.tcbYieldTo })⟩, fun _ _ h _ _ => h, WritesIn.refl _ _⟩ + · obtain ⟨hc, y, n, hy, hyf⟩ := Y.toSc t ht h0 + let sc := L.mem s (yieldA t) + have hcn := hC0 sc hc + have hyt : x.tcbYieldTo = some (scNameA m sc) := by + rw [hxt]; simp [optScA, h0, sc, scNameA_eq] + rw [if_pos (bne_iff_ne.2 h0)] + rw [eq_of_spec_fd (obj.Sc.set_yield_from.spec s sc _), bind_ok, + eq_of_spec_fd (obj.Tcb.set_yield_to.spec _ t _), WP.spec_ok] + obtain ⟨H3, S3, M3, Y3, hz3, W3⟩ := yield_pair_wordsA H hst M Y hc ht hC0 hx hy (Or.inl ⟨rfl, hyf⟩) false + have hne : specPtr t ≠ scNameA m sc := fun e => by rw [e, hy] at hx; cases hx + refine ⟨yieldPairStA σ (scNameA m sc) (specPtr t) x y n false, yieldCancel_eqCA hx hy hyt, + H3, S3, M3, Y3, hz3, rfl, rfl, rfl, rfl, ?_, ?_, W3⟩ + · intro u x' hu + by_cases e : u = specPtr t + · subst u + rw [hx] at hu + cases hu + refine ⟨{ x with tcbYieldTo := none }, ?_, rfl⟩ + simp [yieldPairStA, updKSc_kheap] + · have ep : u ≠ scNameA m sc := fun he => by rw [he, hy] at hu; cases hu + refine ⟨x', ?_, (by cases x'; rfl : x' = { x' with tcbYieldTo := x'.tcbYieldTo })⟩ + simp only [yieldPairStA, updKSc_kheap, Function.update_of_ne e, Function.update_of_ne ep] + exact hu + · intro u o hu h1 h2 + have e : u ≠ specPtr t := fun he => by rw [he, hx] at hu; cases hu; exact h2 x rfl + have ep : u ≠ scNameA m sc := fun he => by rw [he, hy] at hu; cases hu; exact h1 y n rfl + simp only [yieldPairStA, updKSc_kheap, Function.update_of_ne e, Function.update_of_ne ep] + exact hu + +end +end +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScCancelYieldBA.lean b/proofs/Fl4ProofsA64/P3/ScCancelYieldBA.lean new file mode 100644 index 000000000..f8654986c --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScCancelYieldBA.lean @@ -0,0 +1,476 @@ +/- +P3 (AArch64): `schedcontext::cancel_yield_to` from the relaxed relation `SchedSRB … p0`. + +Rust `sched::suspend` runs `cancel_yield_to` after `dequeue` / `release_remove`, where only +`released_ipc_queues` except at the suspended TCB holds. + +* `SchedSRB.scObjBA`, `yield_pair_wordsBA`: the `SchedSRB` forms of `SchedSRA.scObjA` and + `yield_pair_wordsA` (the `RdA` field becomes `RdExA … p0`). +* **`cancel_yield_to_refinesB`**: `cancel_yield_to_refinesA` over `SchedSRB … p0`. + +No named premise. +-/ +import Fl4ProofsA64.P3.ScCancelYieldA +import Fl4ProofsA64.P3.ScConsA +import Fl4ProofsA64.P3.TcbInactiveExA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4SpecA64 Fl4Spec + +noncomputable section +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- `SchedSRA.scObjA` from the relaxed relation at `p0`. -/ +theorem SchedSRB.scObjBA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s s' : S} + {σ : Fl4SpecA64.State Unit} {p0 : Fl4SpecA64.ObjRef} (H : SchedSRB st Z m κ s σ p0) {sc : U64} (hc : sc ∈ Z.C) + {x x' : Fl4SpecA64.SchedContext} {n : Nat} (hk : σ.kheap (scNameA m sc) = some (.schedContext x n)) + (hw : WritesIn st s s' (ScObjA sc n)) (hx' : ScRelWA st (IdleScA m sc) s' sc x' n) + (e1 : x'.scRefills = x.scRefills) (e2 : x'.scRefillMax = x.scRefillMax) (e3 : x'.scPeriod = x.scPeriod) + (e4 : x'.scTcb = x.scTcb) (hcc : L.mem s' (scCoreA sc) = L.mem s (scCoreA sc)) : + SchedSRB st Z m κ s' (updKSc σ (scNameA m sc) (.schedContext x' n)) p0 := by + set σ' := updKSc σ (scNameA m sc) (.schedContext x' n) with hσ' + have hr56 : layout.node.REPROGRAM.val = 56 := by unfold layout.node.REPROGRAM; rfl + have hw' : WritesIn st s s' (fun b => ScFootA Z.C m σ b ∨ b = L.nodeAt s (L.cpu s) layout.node.REPROGRAM) := + hw.mono fun b hb => Or.inl ⟨sc, hc, x, n, hk, hb⟩ + obtain ⟨nK, gK, tK⟩ := H.keepW hw' (by rw [hr56]; omega) (fun b hb => hb) + have hkN' : σ'.kheap (scNameA m sc) = some (.schedContext x' n) := by simp [σ', updKSc] + have hne : ∀ q o, σ.kheap q = some o → (∀ y k, o ≠ .schedContext y k) → q ≠ scNameA m sc := + fun q o hq ho e => by rw [e, hk] at hq; cases hq; exact ho x n rfl + have hobj : ∀ q o, σ.kheap q = some o → (∀ y k, o ≠ .schedContext y k) → σ'.kheap q = some o := + fun q o hq ho => by simp [σ', updKSc, hne q o hq ho, hq] + have hkt : ∀ q u, σ.kheap q = some (.tcb u) → σ'.kheap q = some (.tcb u) := + fun q u hq => hobj q _ hq (fun _ _ h => by cases h) + have hkt' : ∀ q u, σ'.kheap q = some (.tcb u) → σ.kheap q = some (.tcb u) := by + intro q u hq + by_cases e : q = scNameA m sc + · rw [e] at hq; simp [σ', updKSc] at hq + · simpa [σ', updKSc, e] using hq + have hSC : ∀ p y k, σ'.kheap p = some (.schedContext y k) → + σ.kheap p = some (.schedContext y k) ∨ (p = scNameA m sc ∧ y = x' ∧ k = n) := by + intro p y k hp + by_cases e : p = scNameA m sc + · rw [e] at hp; simp [σ', updKSc] at hp; exact Or.inr ⟨e, hp.1.symm, hp.2.symm⟩ + · left; simpa [σ', updKSc, e] using hp + have hact' : Fl4SpecA64.scActive x' = Fl4SpecA64.scActive x := by + simp [Fl4SpecA64.scActive, Fl4SpecA64.activeSc, e2] + have hhd : Fl4SpecA64.refillHd x' = Fl4SpecA64.refillHd x := by simp [Fl4SpecA64.refillHd, e1] + -- `specRtA` of a TCB bound to an active SC + have hrt : ∀ u xu p y k, σ.kheap u = some (.tcb xu) → xu.tcbSchedContext = some p → + σ.kheap p = some (.schedContext y k) → Fl4SpecA64.scActive y = true → specRtA σ' u = specRtA σ u := by + intro u xu p y k hu hp hy ha + have hne0 := H.inv.ne hy ha + rw [specRtA_of hu hp hy hne0] + by_cases e : p = scNameA m sc + · subst e; rw [hk] at hy; cases hy + rw [specRtA_of (hkt _ _ hu) hp hkN' (by rw [e1]; exact hne0), hhd] + · rw [specRtA_of (hkt _ _ hu) hp (by simpa [σ', updKSc, e] using hy) hne0] + have hinv : ScInvA m σ' := by + refine ⟨fun p y k hy ha hp => ?_, fun p y k hy ha hp => ?_, fun q z hz => H.inv.ntfnIdle q z ?_, + fun q z hz => H.inv.ntfnIdleOf q z ?_, fun u hu => ?_, ?_, fun u xu p hu hp => ?_, + fun u xu p hu hp => H.inv.tcbScNotIdle u xu p (hkt' _ _ hu) hp⟩ + · rcases hSC p y k hy with h | ⟨-, ey, -⟩ + · exact H.inv.refills p y k h ha hp + · rw [ey] at ha hp ⊢ + have := H.inv.refills _ x n hk (by rw [← hact']; exact ha) (by rw [← e3]; exact hp) + unfold RefOKA at this ⊢; rw [e1, hhd]; exact this + · rcases hSC p y k hy with h | ⟨ep, ey, -⟩ + · exact H.inv.rr p y k h ha hp + · subst ep; rw [ey] at ha hp ⊢ + have := H.inv.rr _ x n hk (by rw [← hact']; exact ha) (by rw [← e3]; exact hp) + unfold RRokA at this ⊢; rw [e1, hhd]; exact this + · by_cases e : q = scNameA m sc + · rw [e] at hz; simp [σ', updKSc] at hz + · simpa [σ', updKSc, e] using hz + · by_cases e : q = scNameA m sc + · rw [e] at hz; simp [σ', updKSc] at hz + · simpa [σ', updKSc, e] using hz + · obtain ⟨xu, p, y, k, h1, h2, h3, h4⟩ := H.inv.relSc u hu + by_cases e : p = scNameA m sc + · rw [e, hk] at h3; cases h3 + exact ⟨xu, p, x', n, hkt _ _ h1, h2, by rw [e]; exact hkN', by rw [hact']; exact h4⟩ + · exact ⟨xu, p, y, k, hkt _ _ h1, h2, by simpa [σ', updKSc, e] using h3, h4⟩ + · show σ.releaseQueue.Pairwise _ + refine List.Pairwise.imp_of_mem (fun {a b} ha hb hab => ?_) H.inv.sorted + obtain ⟨xa, pa, ya, ka, a1, a2, a3, a4⟩ := H.inv.relSc a ha + obtain ⟨xb, pb, yb, kb, b1, b2, b3, b4⟩ := H.inv.relSc b hb + rw [hrt a xa pa ya ka a1 a2 a3 a4, hrt b xb pb yb kb b1 b2 b3 b4]; exact hab + · obtain ⟨y, k, hy, hyu⟩ := H.inv.back u xu p (hkt' _ _ hu) hp + by_cases e : p = scNameA m sc + · subst e; rw [hk] at hy; cases hy + exact ⟨x', n, by simp [σ', updKSc], by rw [e4]; exact hyu⟩ + · exact ⟨y, k, by simpa [σ', updKSc, e] using hy, hyu⟩ + have hrd : RdExA σ' p0 := by + intro u xu hnp hu hc x0 scp y k h1 h2 h3 h4 h5 + have hu0 := hkt' _ _ hu + have h10 := hkt' _ _ h1 + by_cases e : scp = scNameA m sc + · rw [e, hkN'] at h3; cases h3 + have := H.rdEx u xu hnp hu0 hc x0 _ x n h10 (by rw [h2, e]) hk (by rw [← hact']; exact h4) h5 + unfold releasedSc at this ⊢; rw [hhd]; exact this + · exact H.rdEx u xu hnp hu0 hc x0 scp y k h10 h2 (by simpa [σ', updKSc, e] using h3) h4 h5 + have hkc : ∀ c ∈ Z.C, ∀ y k, σ.kheap (scNameA m c) = some (.schedContext y k) → + ∃ y', σ'.kheap (scNameA m c) = some (.schedContext y' k) := by + intro c _ y k hy + by_cases e : scNameA m c = scNameA m sc + · rw [e, hk] at hy; cases hy; exact ⟨x', by rw [e]; simp [σ', updKSc]⟩ + · exact ⟨y, by simpa [σ', updKSc, e] using hy⟩ + obtain ⟨hS, hP⟩ := H.scr.upd H.scp hc hk hw hx' + have tw : ∀ t ∈ Z.T, ∀ K : U64, (K = 3520#u64 ∨ K = 3448#u64 ∨ K = 3432#u64 ∨ K = 3464#u64 ∨ K = 3592#u64 ∨ + K = 3480#u64 ∨ K = 3488#u64) → L.mem s' (wa t K) = L.mem s (wa t K) := fun t ht K hK => + tK t ht K (by rcases hK with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> simp) + (by rcases hK with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> simp) + have hcore : SchedCoreA st Z.T Z.Cs s' Z.Q Z.R := + H.core.agree hw.1 (fun u hu => ⟨tw u hu 3480#u64 (by simp), tw u hu 3488#u64 (by simp)⟩) + (fun c hc o h1 h2 h3 => nK c hc o h1 h2 (by rw [hr56]; omega)) (fun u hu => tw u hu 3592#u64 (by simp)) + (fun u hu => tw u hu 3520#u64 (by simp)) (fun u hu => tw u hu 3448#u64 (by simp)) + refine ⟨H.toSchedSRN.rebuildSC (Q' := Z.Q) hw.1 hw.2.1 (fun c hc o h1 h2 h3 h4 => nK c hc o (by omega) h2 (by rw [hr56]; exact h4)) + (fun K hK => gK K (by rcases hK with rfl | rfl <;> simp) (by rcases hK with rfl | rfl <;> simp)) + (fun t ht K hK => tw t ht K (by rcases hK with h | h | h | h <;> simp [h])) hS hP + (fun t ht u hu => ⟨u, hkt _ u hu, rfl, rfl, rfl⟩) ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ + (by + show σ.reprogramTimer = _ + rw [hw.mem_eq H.reprA.1 (fun hb => H.reprA.2 _ ⟨sc, hc, x, n, hk, hb⟩ rfl)] + exact H.rest.repr) + (by + show σ.schedulerAction = _ + rw [nK _ H.sch.cpuCs layout.node.SCHED_ACTION (by simp [layout.node.SCHED_ACTION]) + (by simp [layout.node.SCHED_ACTION]) (by rw [hr56]; simp [layout.node.SCHED_ACTION])] + exact H.sch.act) + H.actOn hinv hcore (fun p hp => H.rest.rq p hp) H.rest.rqDom rfl + ⟨fun d p u hu => let ⟨w, h1, h2⟩ := H.run.1 d p u hu; ⟨w, hkt _ w h1, h2⟩, + fun u hu => let ⟨w, h1, h2⟩ := H.run.2 u hu; ⟨w, hkt _ w h1, h2⟩⟩ + (fun c' hc' => by + by_cases e : c' = sc + · rw [e]; exact hcc + obtain ⟨y', k, hky, hy⟩ := H.scr.sc c' hc' + have h16 := hy.hdr (K := 16#u64) (by simp) (by simp) + have hcE : scCoreA c' = scWA c' 16#u64 := by unfold scCoreA scWA layout.sc.CORE; rfl + refine hw.mem_eq (H.scCoreFoot hc').1 (fun ho => ?_) + rw [hcE] at ho; exact H.scp.disj c' hc' sc hc e y' k x n hky hk _ (hy.in_obj h16.2) ho), hrd⟩ + +private theorem scRel_set_yieldBA {ii : Prop} {s : S} {sc : U64} {xs : SchedContext} {ns : Nat} + (h : ScRelWA st ii s sc xs ns) (v : U64) : + let s' := L.wr s (scWA sc 56#u64) v + ScRelWA st ii s' sc { xs with scYieldFrom := optRefS v } ns ∧ WritesIn st s s' (ScObjA sc ns) ∧ L.mem s' (scCoreA sc) = L.mem s (scCoreA sc) := by + intro s' + obtain ⟨hR, hp, hm, hw, hwr, hf⟩ := h.wr_hdr (K := 56#u64) (by simp [HdrKA]) v + have keep : ∀ k : U64, k.val < 96 → k.val % 8 = 0 → k.val ≠ 56 → + L.mem s' (scWA sc k) = L.mem s (scWA sc k) := + fun k h1 h2 h3 => hw k h1 h2 (fun e => h3 (by subst e; simp)) + refine ⟨⟨h.pl, by rw [hp]; exact h.period, by rw [keep 8#u64 (by simp) (by simp) (by simp)]; exact h.consumed, + by rw [keep 24#u64 (by simp) (by simp) (by simp)]; exact h.tcb, + by rw [keep 32#u64 (by simp) (by simp) (by simp)]; exact h.reply, by rw [keep 40#u64 (by simp) (by simp) (by simp)]; exact h.ntfn, + by rw [keep 48#u64 (by simp) (by simp) (by simp)]; exact h.badge, + by rw [hwr], + by rw [keep 64#u64 (by simp) (by simp) (by simp)]; exact h.sporadic, + fun h0 => by rw [hm]; exact h.spor (by rwa [keep 64#u64 (by simp) (by simp) (by simp)] at h0), + fun h0 => by rw [hm]; exact h.idle h0, by rw [hm]; exact h.max, fun ha => ?_⟩, hf, ?_⟩ + swap + · rw [show scCoreA sc = scWA sc 16#u64 by unfold scCoreA scWA layout.sc.CORE; rfl] + exact hw 16#u64 (by decide) (by decide) (by decide) + have ha' : (L.rmax s sc).val ≠ 0 := by rwa [hm] at ha + have h1 := h.act ha' + exact ⟨(hR ha').1, by rw [hp]; exact h1.period, + by rw [keep 8#u64 (by simp) (by simp) (by simp)]; exact h1.consumed, + by rw [keep 24#u64 (by simp) (by simp) (by simp)]; exact h1.tcb, + by rw [keep 32#u64 (by simp) (by simp) (by simp)]; exact h1.reply, by rw [keep 40#u64 (by simp) (by simp) (by simp)]; exact h1.ntfn, + by rw [keep 48#u64 (by simp) (by simp) (by simp)]; exact h1.badge, + by rw [hwr], + by rw [keep 64#u64 (by simp) (by simp) (by simp)]; exact h1.sporadic, + by rw [hm]; exact h1.max, by rw [(hR ha').2]; exact h1.refills⟩ + +private theorem yieldPairRelBA {T C : Set U64} {m : McState} {s s' : S} {σ : State Unit} + (Y : YieldRelA T C m (L.mem s) σ) {sc t : U64} (hc : sc ∈ C) (ht : t ∈ T) + (nonzero : ∀ c ∈ C, c ≠ 0#u64) {x : Tcb} {y : SchedContext} {n : Nat} + (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hs : σ.kheap (scNameA m sc) = some (.schedContext y n)) + (hi : ∀ c ∈ C, scNameA m c = scNameA m sc → c = sc) + (old : (L.mem s (yieldA t) = sc ∧ y.scYieldFrom = some (specPtr t)) ∨ + (L.mem s (yieldA t) = 0#u64 ∧ y.scYieldFrom = none)) + (link : Bool) (hm : L.mem s' (yieldA t) = if link then sc else 0#u64) + (keep : ∀ u ∈ T, u ≠ t → L.mem s' (yieldA u) = L.mem s (yieldA u)) : + YieldRelA T C m (L.mem s') (yieldPairStA σ (scNameA m sc) (specPtr t) x y n link) := by + have hc0 := nonzero sc hc + let p := scNameA m sc + let q := specPtr t + let z := {x with tcbYieldTo := if link then some p else none} + let w := {y with scYieldFrom := if link then some q else none} + let σ' := yieldPairStA σ p q x y n link + change σ.kheap p = some (.schedContext y n) at hs + change σ.kheap q = some (.tcb x) at hk + have epq : p ≠ q := by intro e; rw [e, hk] at hs; cases hs + have hq : σ'.kheap q = some (.tcb z) := updKSc_same _ _ _ + have hp : σ'.kheap p = some (.schedContext w n) := by + simp [σ', yieldPairStA, Function.update_of_ne epq, w] + have far : ∀ a, a ≠ p → a ≠ q → σ'.kheap a = σ.kheap a := by + intro a ap aq + simp [σ', yieldPairStA, Function.update_of_ne ap, Function.update_of_ne aq] + have othTCB {u a : _} (h : σ.kheap (specPtr u) = some (.tcb a)) : + specPtr u ≠ p := by intro e; rw [e, hs] at h; cases h + have othSC {c v k : _} (h : σ.kheap (scNameA m c) = some (.schedContext v k)) : + scNameA m c ≠ q := by intro e; rw [e, hk] at h; cases h + have handsOff {u : U64} (hu : u ∈ T) (hne : u ≠ t) : L.mem s (yieldA u) ≠ sc := by + intro e + obtain ⟨_, v, k, h, hlink⟩ := Y.toSc u hu (by rw [e]; exact hc0) + rw [e, hs] at h + cases h + rcases old with ⟨_, h'⟩ | ⟨_, h'⟩ + · rw [h'] at hlink + exact hne (specPtr_inj (Option.some.inj hlink).symm) + · rw [h'] at hlink + cases hlink + refine ⟨?_, ?_, ?_⟩ + · intro u hu + by_cases e : u = t + · subst u + refine ⟨z, hq, ?_⟩ + rw [hm] + cases link <;> simp [z, p, optScA, hc0, scNameA_eq] + · obtain ⟨a, ha, he⟩ := Y.tcb u hu + rw [keep u hu e] + exact ⟨a, (far _ (othTCB ha) (fun h => e (specPtr_inj h))).trans ha, he⟩ + · intro u hu h0 + by_cases e : u = t + · subst u + rw [hm] at h0 ⊢ + cases link + · exact (h0 rfl).elim + · exact ⟨hc, w, n, hp, rfl⟩ + · rw [keep u hu e] at h0 ⊢ + obtain ⟨hd, v, k, hv, hy⟩ := Y.toSc u hu h0 + exact ⟨hd, v, k, (far _ (fun h => handsOff hu e (hi _ hd h)) (othSC hv)).trans hv, hy⟩ + · intro c cd v k hv a ha + by_cases e : c = sc + · subst c + rw [hp] at hv + cases hv + cases link + · cases ha + · exact ⟨t, ht, Option.some.inj ha, hm⟩ + · have qp : scNameA m c ≠ p := fun h => e (hi _ cd h) + have qq : scNameA m c ≠ q := by + intro h + rw [h, hq] at hv + cases hv + obtain ⟨u, hu, hu_a, hu_c⟩ := Y.frm c cd v k ((far _ qp qq).symm.trans hv) a ha + have hut : u ≠ t := by + intro eq + subst u + rcases old with ⟨h, _⟩ | ⟨h, _⟩ + · exact e (hu_c.symm.trans h) + · exact nonzero c cd (hu_c.symm.trans h) + exact ⟨u, hu, hu_a, (keep u hu hut).trans hu_c⟩ + +/-- `yield_pair_wordsA` from the relaxed relation at `p0`. -/ +theorem yield_pair_wordsBA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + {p0 : ObjRef} (H : SchedSRB st Z m κ s σ p0) (hst : StEqA Z.T (L.mem s) σ) + (M : McpRelA Z.T m (L.mem s) σ) (Y : YieldRelA Z.T Z.C m (L.mem s) σ) + {sc t : U64} (hc : sc ∈ Z.C) (ht : t ∈ Z.T) (hC0 : ∀ c ∈ Z.C, c ≠ 0#u64) + {x : Tcb} {y : SchedContext} {n : Nat} + (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hs : σ.kheap (scNameA m sc) = some (.schedContext y n)) + (old : (L.mem s (yieldA t) = sc ∧ y.scYieldFrom = some (specPtr t)) ∨ + (L.mem s (yieldA t) = 0#u64 ∧ y.scYieldFrom = none)) (link : Bool) : + let s' := L.wr (L.wr s (scYieldFromA sc) (if link then t else 0#u64)) + (yieldA t) (if link then sc else 0#u64) + let σ' := yieldPairStA σ (scNameA m sc) (specPtr t) x y n link + SchedSRB st Z m κ s' σ' p0 ∧ StEqA Z.T (L.mem s') σ' ∧ + McpRelA Z.T m (L.mem s') σ' ∧ YieldRelA Z.T Z.C m (L.mem s') σ' ∧ + SSzA σ σ' ∧ WritesIn st s s' (fun b => MWrA (st := st) Z m σ s b ∨ b = yieldA t) := by + intro s' σ' + let p := scNameA m sc + let q := specPtr t + let v := if link then t else 0#u64 + let u := if link then sc else 0#u64 + let y' := {y with scYieldFrom := if link then some q else none} + let x' := {x with tcbYieldTo := if link then some p else none} + let s1 := L.wr s (scYieldFromA sc) v + let σ1 := updKSc σ p (.schedContext y' n) + obtain ⟨y0, n0, hy0, hr⟩ := H.scr.sc sc hc + rw [hs] at hy0 + cases hy0 + change σ.kheap p = some (.schedContext y n) at hs + change σ.kheap q = some (.tcb x) at hk + have hne : q ≠ p := fun e => by rw [e, hs] at hk; cases hk + have ht0 : t ≠ 0#u64 := fun e => (H.core.tcbs t ht).ne0 (by rw [e]; rfl) + have hopt : optRefS v = y'.scYieldFrom := by + cases link + · exact optRefS_zero + · exact optRefS_ne ht0 + obtain ⟨hr', hw1, hco⟩ := scRel_set_yieldBA hr v + have haddr : scYieldFromA sc = scWA sc 56#u64 := by unfold scYieldFromA scWA layout.sc.YIELD_FROM; rfl + rw [← haddr] at hr' hw1 hco + rw [hopt] at hr' + change ScRelWA st (IdleScA m sc) s1 sc y' n at hr' + have H1 : SchedSRB st Z m κ s1 σ1 p0 := H.scObjBA hc hs hw1 hr' rfl rfl rfl rfl hco + have hk1 : σ1.kheap q = some (.tcb x) := by + simpa [σ1, Function.update_of_ne hne] using hk + have step := yieldTcbStepA (z := x') hk1 rfl rfl rfl rfl rfl + have hw1' : WritesIn st s s1 (fun b => ScFootA Z.C m σ b ∨ b = L.nodeAt s (L.cpu s) 0#u64) := + hw1.mono fun b hb => Or.inl ⟨sc, hc, y, n, hs, hb⟩ + have keep1 := (H.keepW hw1' (by decide) (fun _ h => h)).2.2 + have htAddr := H1.core.tcbs t ht + have hal : Aligned (yieldA t) := field_aligned htAddr (by decide) (by decide) + have hw2 : WritesIn st s1 s' (· = yieldA t) := writesIn_wr hal + have hrd1 : RdExA (updKSc σ1 q (.tcb x')) p0 := by + intro w y hw hy hc + by_cases e : w = q + · subst e + rw [updKSc_same] at hy + cases hy + exact (H1.rdEx _ x hw hk1 hc).step step.1 hk1 + · have hy0 : σ1.kheap w = some (.tcb y) := by rwa [updKSc_kheap, Function.update_of_ne e] at hy + exact (H1.rdEx w y hw hy0 hc).step step.1 hy0 + have H' : SchedSRB st Z m κ s' σ' p0 := + H1.stableExBA hw2 (fun _ e => e ▸ yieldWordOffReadA H1.toSchedSRN ht) step.1 + (H1.run.step step.1 step.2) hrd1 + have keep2 {d K : U64} (hd : d ∈ Z.T) (hb : K.val < 4096) (ha : K.val % 8 = 0) + (he : d = t → K.val ≠ 3472) : L.mem s' (wa d K) = L.mem s (wa d K) := by + rw [hw2.mem_eq (field_aligned (H1.core.tcbs d hd) hb ha)] + · exact keep1 d hd K (by omega) ha + · exact field_ne (H1.core.tcbs d hd) htAddr hb (by decide) he + have tcbFrames : ∀ d ∈ Z.T, ∀ z, σ.kheap (specPtr d) = some (.tcb z) → + ∃ z', σ'.kheap (specPtr d) = some (.tcb z') ∧ z'.tcbState = z.tcbState ∧ z'.tcbMcpriority = z.tcbMcpriority ∧ + z'.tcbFlags = z.tcbFlags := by + intro d hd z hz + by_cases e : d = t + · subst d + rw [hk] at hz + cases hz + exact ⟨x', updKSc_same _ _ _, rfl, rfl, rfl⟩ + · have hn : specPtr d ≠ p := fun e' => by rw [e', hs] at hz; cases hz + have hq : specPtr d ≠ q := fun e' => e (specPtr_inj e') + refine ⟨z, ?_, rfl, rfl, rfl⟩ + change (updKSc σ1 q (.tcb x')).kheap (specPtr d) = _ + rw [updKSc_kheap, Function.update_of_ne hq] + change (updKSc σ p (.schedContext y' n)).kheap (specPtr d) = _ + rwa [updKSc_kheap, Function.update_of_ne hn] + refine ⟨H', ?_, ?_, ?_, ?_, (hw1.trans hw2).mono (fun b hb => hb.elim (fun hb => Or.inl (Or.inl ⟨sc, hc, y, n, hs, hb⟩)) Or.inr)⟩ + · intro d hd z hz + obtain ⟨z0, hk0, -⟩ := H.sch.tcb d hd + obtain ⟨z', hk', he, -⟩ := tcbFrames d hd z0 hk0 + rw [hz] at hk' + cases hk' + rw [he, stA_eq, keep2 hd (by decide) (by decide) (fun _ => by decide), ← stA_eq] + exact hst d hd z0 hk0 + · intro d hd + obtain ⟨z, hz, hm, hf⟩ := M d hd + obtain ⟨z', hz', _, he, hfl⟩ := tcbFrames d hd z hz + exact ⟨z', hz', by rw [he, mcpA, keep2 hd (by decide) (by decide) (fun _ => by decide), ← mcpA]; exact hm, + fun g => (hf g).keep (by unfold flagsA; exact keep2 hd (by decide) (by decide) (fun _ => by decide)) hfl⟩ + · exact yieldPairRelBA Y hc ht hC0 hk hs (fun c h => H.scp.inj c h sc hc) old link + (L.mem_wr_same hal) (fun d hd hd' => keep2 hd (by decide) (by decide) (fun h => (hd' h).elim)) + · intro d z k h + have hq : d ≠ q := fun e => by rw [e, show σ'.kheap q = some (.tcb x') from updKSc_same _ _ _] at h; cases h + change (updKSc σ1 q (.tcb x')).kheap d = _ at h + rw [updKSc_kheap, Function.update_of_ne hq] at h + by_cases ep : d = p + · subst d + rw [updKSc_same] at h + cases h + exact ⟨y, hs⟩ + · exact ⟨z, by simpa [σ1, Function.update_of_ne ep] using h⟩ + +end + +private theorem yieldCancel_eqBA {σ : State Unit} {p q : ObjRef} {x : Tcb} {y : SchedContext} {n : Nat} + (hk : σ.kheap q = some (.tcb x)) (hs : σ.kheap p = some (.schedContext y n)) + (ht : x.tcbYieldTo = some p) : + schedContextCancelYieldTo (Z := Unit) q σ = ({((), yieldPairStA σ p q x y n false)}, False) := by + have hne : q ≠ p := fun e => by rw [e, hs] at hk; cases hk + unfold schedContextCancelYieldTo getTcbObjRef + rw [nd_bind_oneSc (threadGet_eqSc _ hk), ht] + change (updateSchedContext p _ >>= _) σ = _ + rw [nd_bind_oneSc (updateSchedContext_eqA hs _)] + exact threadSet_eqSc _ (by rwa [updKSc_kheap, Function.update_of_ne hne]) + +private theorem yieldCancel_noneBA {σ : State Unit} {q : ObjRef} {x : Tcb} + (hk : σ.kheap q = some (.tcb x)) (ht : x.tcbYieldTo = none) : + schedContextCancelYieldTo (Z := Unit) q σ = ({((), σ)}, False) := by + unfold schedContextCancelYieldTo getTcbObjRef + rw [nd_bind_oneSc (threadGet_eqSc _ hk), ht] + rfl + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **`cancel_yield_to` from the relaxed relation** (module docstring). -/ +theorem cancel_yield_to_refinesB {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + {p0 : ObjRef} (H : SchedSRB st Z m κ s σ p0) (hst : StEqA Z.T (L.mem s) σ) + (M : McpRelA Z.T m (L.mem s) σ) (Y : YieldRelA Z.T Z.C m (L.mem s) σ) (hC0 : ∀ c ∈ Z.C, c ≠ 0#u64) + {t : U64} (ht : t ∈ Z.T) : + schedcontext.cancel_yield_to st s t ⦃ s' => ∃ σ', + schedContextCancelYieldTo (Z := Unit) (specPtr t) σ = ({((), σ')}, False) ∧ + SchedSRB st Z m κ s' σ' p0 ∧ StEqA Z.T (L.mem s') σ' ∧ McpRelA Z.T m (L.mem s') σ' ∧ + YieldRelA Z.T Z.C m (L.mem s') σ' ∧ SSzA σ σ' ∧ + σ'.curThread = σ.curThread ∧ σ'.releaseQueue = σ.releaseQueue ∧ + σ'.readyQueues = σ.readyQueues ∧ σ'.schedulerAction = σ.schedulerAction ∧ + (∀ u x, σ.kheap u = some (.tcb x) → ∃ x', σ'.kheap u = some (.tcb x') ∧ + x' = { x with tcbYieldTo := x'.tcbYieldTo }) ∧ + (∀ u o, σ.kheap u = some o → (∀ y n, o ≠ .schedContext y n) → (∀ x, o ≠ .tcb x) → + σ'.kheap u = some o) ∧ + WritesIn st s s' (fun b => MWrA (st := st) Z m σ s b ∨ b = yieldA t) ∧ + σ'.machineState = σ.machineState ∧ + (∀ u y n, σ.kheap u = some (.schedContext y n) → ∃ y', σ'.kheap u = some (.schedContext y' n) ∧ + y' = { y with scYieldFrom := y'.scYieldFrom }) ⦄ := by + have ht0 : t ≠ 0#u64 := fun e => (H.core.tcbs t ht).ne0 (by rw [e]; rfl) + obtain ⟨x, hx, hxt⟩ := Y.tcb t ht + unfold schedcontext.cancel_yield_to + rw [eq_of_spec_fd (obj.Tcb.is_null.spec t), bind_ok, decide_eq_false ht0] + simp only [Bool.false_eq_true, ↓reduceIte] + rw [eq_of_spec_fd (obj.Tcb.yield_to.spec s t), bind_ok] + by_cases h0 : L.mem s (yieldA t) = 0#u64 + · rw [h0] + simp only [bne_self_eq_false, Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + have hn : x.tcbYieldTo = none := by rw [hxt, h0]; simp [optScA] + refine ⟨σ, yieldCancel_noneBA hx hn, H, hst, M, Y, SSzA.refl σ, rfl, rfl, rfl, rfl, + fun u x hu => ⟨x, hu, (by cases x; rfl : x = { x with tcbYieldTo := x.tcbYieldTo })⟩, fun _ _ h _ _ => h, WritesIn.refl _ _, + rfl, fun u y n h => ⟨y, h, (by cases y; rfl : y = { y with scYieldFrom := y.scYieldFrom })⟩⟩ + · obtain ⟨hc, y, n, hy, hyf⟩ := Y.toSc t ht h0 + let sc := L.mem s (yieldA t) + have hcn := hC0 sc hc + have hyt : x.tcbYieldTo = some (scNameA m sc) := by + rw [hxt]; simp [optScA, h0, sc, scNameA_eq] + rw [if_pos (bne_iff_ne.2 h0)] + rw [eq_of_spec_fd (obj.Sc.set_yield_from.spec s sc _), bind_ok, + eq_of_spec_fd (obj.Tcb.set_yield_to.spec _ t _), WP.spec_ok] + obtain ⟨H3, S3, M3, Y3, hz3, W3⟩ := yield_pair_wordsBA H hst M Y hc ht hC0 hx hy (Or.inl ⟨rfl, hyf⟩) false + have hne : specPtr t ≠ scNameA m sc := fun e => by rw [e, hy] at hx; cases hx + refine ⟨yieldPairStA σ (scNameA m sc) (specPtr t) x y n false, yieldCancel_eqBA hx hy hyt, + H3, S3, M3, Y3, hz3, rfl, rfl, rfl, rfl, ?_, ?_, W3, rfl, ?_⟩ + · intro u x' hu + by_cases e : u = specPtr t + · subst u + rw [hx] at hu + cases hu + refine ⟨{ x with tcbYieldTo := none }, ?_, rfl⟩ + simp [yieldPairStA, updKSc_kheap] + · have ep : u ≠ scNameA m sc := fun he => by rw [he, hy] at hu; cases hu + refine ⟨x', ?_, (by cases x'; rfl : x' = { x' with tcbYieldTo := x'.tcbYieldTo })⟩ + simp only [yieldPairStA, updKSc_kheap, Function.update_of_ne e, Function.update_of_ne ep] + exact hu + · intro u o hu h1 h2 + have e : u ≠ specPtr t := fun he => by rw [he, hx] at hu; cases hu; exact h2 x rfl + have ep : u ≠ scNameA m sc := fun he => by rw [he, hy] at hu; cases hu; exact h1 y n rfl + simp only [yieldPairStA, updKSc_kheap, Function.update_of_ne e, Function.update_of_ne ep] + exact hu + · intro u y' n' hu + have e : u ≠ specPtr t := fun he => by rw [he, hx] at hu; cases hu + by_cases ep : u = scNameA m sc + · subst u + rw [hy] at hu + cases hu + refine ⟨{ y with scYieldFrom := none }, ?_, rfl⟩ + simp [yieldPairStA, updKSc_kheap, Function.update_of_ne e] + · refine ⟨y', ?_, (by cases y'; rfl : y' = { y' with scYieldFrom := y'.scYieldFrom })⟩ + simp only [yieldPairStA, updKSc_kheap, Function.update_of_ne e, Function.update_of_ne ep] + exact hu + + +end +end +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScCfgChargeA.lean b/proofs/Fl4ProofsA64/P3/ScCfgChargeA.lean new file mode 100644 index 000000000..cf72411a0 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScCfgChargeA.lean @@ -0,0 +1,532 @@ +import Fl4ProofsA64.P3.ScCfgEntryA +import Fl4ProofsA64.P3.ScCfgStateA + +/-! The ring updates of the current SC before ConfigureFlags. -/ +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3.Sched +open Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +/-- `refill_budget_check_round_robin` on the list: `c` moves from the head +to the tail. -/ +def rrChargeLA (rs : List sporadic.Refill) (c : U64) : List sporadic.Refill := + let l : List sporadic.Refill := + ⟨(rs.headD zRA).time, core.num.U64.wrapping_sub (rs.headD zRA).amount c⟩ :: rs.tail + l.dropLast ++ [⟨(l.getLastD zRA).time, core.num.U64.wrapping_add (l.getLastD zRA).amount c⟩] + +section Spec +open Fl4SpecA64 Fl4Spec + +theorem rrCharge_eqA {σ : State Unit} {p : ObjRef} {x : SchedContext} {n : Nat} + (hcur : σ.curSc = p) (hk : σ.kheap p = some (.schedContext x n)) {rs : List sporadic.Refill} + (hx : x.scRefills = rs.map toRefillA) (hne : rs ≠ []) (c : U64) : + refillBudgetCheckRoundRobin (Z := Unit) c.bv σ = + ({((), updKSc σ p (.schedContext { x with scRefills := (rrChargeLA rs c).map toRefillA } n))}, False) := by + unfold refillBudgetCheckRoundRobin + rw [nd_gets_bindSc, hcur, nd_bind_oneSc (updateRefillHd_eqA hk _)] + have hk1 : ∀ z : SchedContext, (updKSc σ p (.schedContext z n)).kheap p = some (.schedContext z n) := fun z => updKSc_same _ _ _ + rw [updateRefillTl_eqA (hk1 _), updKSc_updKSc] + congr 4 + obtain ⟨a, l, rfl⟩ := List.exists_cons_of_ne_nil hne + rw [hx] + cases l with + | nil => simp [rrChargeLA, the, toRefillA, zRA, core.num.U64.wrapping_add_bv_eq, core.num.U64.wrapping_sub_bv_eq] + | cons b l => + simp only [List.map_cons, List.head?_cons, the_someA, List.tail_cons] + rw [← List.map_cons, the_getLast_cons_mapA _ (by simp)] + have hl : ∀ q : sporadic.Refill, (q :: b :: l).getLastD zRA = (b :: l).getLastD zRA := fun q => by + simp [List.getLastD_eq_getLast?, List.getLast?_cons_cons] + simp [rrChargeLA, toRefillA, hl, core.num.U64.wrapping_add_bv_eq, core.num.U64.wrapping_sub_bv_eq, + List.dropLast_cons_of_ne_nil] + +theorem rrChargeLA_split (a l : sporadic.Refill) (i : List sporadic.Refill) (c : U64) : + rrChargeLA (a :: (i ++ [l])) c = + ⟨a.time, core.num.U64.wrapping_sub a.amount c⟩ :: (i ++ [⟨l.time, core.num.U64.wrapping_add l.amount c⟩]) := by + unfold rrChargeLA + simp only [List.headD_cons, List.tail_cons] + have e : ∀ b : sporadic.Refill, (b :: (i ++ [l])).getLastD zRA = l := fun b => by + rw [← List.cons_append, List.getLastD_eq_getLast?, List.getLast?_concat]; rfl + rw [e, ← List.cons_append, List.dropLast_concat]; rfl + +/-- A list of at least two refills is a head, a middle and a last. -/ +theorem cfgRrSplitA {x : SchedContext} {rs : List sporadic.Refill} (hx : x.scRefills = rs.map toRefillA) + (h : 2 ≤ x.scRefills.length) : ∃ a i l, rs = a :: (i ++ [l]) := by + rw [hx, List.length_map] at h + match rs, h with + | a :: t, h => + rcases List.eq_nil_or_concat t with e | ⟨i, l, e⟩ + · subst e; simp at h + · exact ⟨a, i, l, by rw [e, List.concat_eq_append]⟩ + +/-- The round-robin charge keeps `RRokA` when the head pays the charge and +keeps `MIN_BUDGET`. -/ +theorem rrChargeLA_ok {x : SchedContext} {rs : List sporadic.Refill} (hx : x.scRefills = rs.map toRefillA) + (h : RRokA x) (c : U64) (hc : c.val < (rs.headD zRA).amount.val) + (hm : MIN_BUDGET.toNat ≤ (rs.headD zRA).amount.val - c.val) : + RRokA { x with scRefills := (rrChargeLA rs c).map toRefillA } := by + obtain ⟨a, i, l, rfl⟩ := cfgRrSplitA hx h.1 + obtain ⟨h1, h2, h3⟩ := h + rw [hx] at h1 h2 + simp [toRefillA, List.sum_append] at h2 hc hm + have hsub := wsub_val (x := a.amount) (y := c) (by omega) + have hav : l.amount.val + c.val < 2 ^ 64 := by omega + have hw := wadd_val (x := l.amount) (y := c) hav + refine ⟨?_, ?_, ?_⟩ + · simp [rrChargeLA_split] + · simp [rrChargeLA_split, toRefillA, List.sum_append]; omega + · unfold refillHd + simp [rrChargeLA_split, toRefillA, the] + rw [BitVec.le_def] + show _ ≤ (core.num.U64.wrapping_sub a.amount c).val + rw [hsub]; exact hm + +theorem cfg_min_budget_posA : (0 : Ticks) < MIN_BUDGET := MIN_BUDGET_pos' + +/-- A sufficient refill pays the usage and keeps `MIN_BUDGET`. -/ +theorem suff_natA {u : Ticks} {r : Refill} (h : refillSufficient u r = true) : + u.toNat < r.rAmount.toNat ∧ MIN_BUDGET.toNat ≤ r.rAmount.toNat - u.toNat := by + unfold refillSufficient refillCapacity at h + simp only [decide_eq_true_eq] at h + have hm : 0 < MIN_BUDGET.toNat := by have := cfg_min_budget_posA; rw [BitVec.lt_def] at this; simpa using this + by_cases hlt : r.rAmount < u + · rw [if_pos hlt] at h; rw [BitVec.le_def] at h; simp at h; omega + · rw [if_neg hlt] at h + have hle : u ≤ r.rAmount := by rw [BitVec.not_lt] at hlt; exact hlt + rw [BitVec.le_def] at h hle + rw [BitVec.toNat_sub_of_le (by rw [BitVec.le_def]; exact hle)] at h + omega + +end Spec + +/-- `schedule_used` keeps the head's time; it keeps its amount, or (for a +single refill) adds the new amount. -/ +theorem schedUsedLA_head {m : Nat} (h2 : 2 ≤ m) (a : sporadic.Refill) (tl : List sporadic.Refill) + (nw : sporadic.Refill) : ((schedUsedLA m (a :: tl) nw).headD zRA).time = a.time ∧ + (((schedUsedLA m (a :: tl) nw).headD zRA).amount = a.amount ∨ + ((schedUsedLA m (a :: tl) nw).headD zRA).amount = core.num.U64.wrapping_add a.amount nw.amount) := by + unfold schedUsedLA + cases tl with + | nil => + simp only [List.getLastD_cons, List.getLastD_nil, List.dropLast_singleton, List.nil_append, + List.length_singleton] + by_cases h3 : nw.time ≤ core.num.U64.wrapping_add a.time a.amount + · rw [if_pos h3]; exact ⟨rfl, Or.inr rfl⟩ + · rw [if_neg h3, if_neg (by omega)]; exact ⟨rfl, Or.inl rfl⟩ + | cons b l => + simp only [List.dropLast_cons_of_ne_nil (List.cons_ne_nil b l)] + split_ifs <;> exact ⟨rfl, Or.inl rfl⟩ + +theorem schedUsedLA_ne {m : Nat} {rs : List sporadic.Refill} {nw : sporadic.Refill} : schedUsedLA m rs nw ≠ [] := by + unfold schedUsedLA; dsimp only; split_ifs <;> simp + +section Periodic +open Fl4SpecA64 Fl4Spec + +/-- **The head after a sufficient `refill_budget_check`.** -/ +theorem fl4RefillBudgetCheck_headA {σ σ' : Fl4SpecA64.State Unit} {p : Fl4SpecA64.ObjRef} {x : Fl4SpecA64.SchedContext} {n : Nat} + (hcur : σ.curSc = p) (hk : σ.kheap p = some (.schedContext x n)) {rs : List sporadic.Refill} + (hx : x.scRefills = rs.map toRefillA) (hne : rs ≠ []) {per : U64} (hper : x.scPeriod = per.bv) + (hp0 : x.scPeriod ≠ 0) (hm2 : 2 ≤ x.scRefillMax) (u : U64) + (hsuf : Fl4SpecA64.refillSufficient u.bv (Fl4SpecA64.refillHd x) = true) + (hagree : Fl4SpecA64.fl4MinBudget = Fl4SpecA64.MIN_BUDGET) + (h : Fl4SpecA64.fl4RefillBudgetCheck (Z := Unit) u.bv σ = ({((), σ')}, False)) : + ∀ y m, σ'.kheap p = some (.schedContext y m) → Fl4SpecA64.MIN_BUDGET ≤ (Fl4SpecA64.refillHd y).rAmount ∧ + ((Fl4SpecA64.refillHd y).rTime = (Fl4SpecA64.refillHd x).rTime ∨ + (Fl4SpecA64.refillHd y).rTime = (Fl4SpecA64.refillHd x).rTime + u.bv) := by + obtain ⟨hd, tl, rfl⟩ := List.exists_cons_of_ne_nil hne + have hH : Fl4SpecA64.refillHd x = toRefillA hd := refillHd_mapA hx hne + have hne' : x.scRefills ≠ [] := by rw [hx]; simp + obtain ⟨hlt, hmin⟩ := suff_natA hsuf + rw [hH] at hlt hmin + simp only [toRefillA] at hlt hmin + have hav : hd.amount.bv.toNat = hd.amount.val := rfl + have huv : u.bv.toNat = u.val := rfl + rw [hav, huv] at hlt hmin + unfold Fl4SpecA64.fl4RefillBudgetCheck at h + rw [hagree, refillBudgetCheckWith_eqA Fl4SpecA64.fl4MaxReleaseTime Fl4SpecA64.MIN_BUDGET hcur hk hp0] at h + have hov : ¬ Fl4SpecA64.the (Fl4SpecA64.headRefillOverrunWith (Z := Unit) Fl4SpecA64.fl4MaxReleaseTime p u.bv σ) = true := by + simp only [headRefillOverrunWith_eqA Fl4SpecA64.fl4MaxReleaseTime hk hne', hH, Fl4SpecA64.the, toRefillA, decide_eq_true_eq, not_and, BitVec.le_def] + intro h1; rw [hav, huv] at h1; omega + unfold Fl4SpecA64.handleOverrunWith at h + rw [nd_bind_oneSc (wl_doneSc (C := fun usage s => Fl4SpecA64.the (Fl4SpecA64.headRefillOverrunWith Fl4SpecA64.fl4MaxReleaseTime p usage s) = true) + (B := fun usage => Fl4SpecA64.chargeEntireHeadRefill p usage) hov)] at h + unfold specTailWithA at h + rw [nd_bind_oneSc (getRefillHead_eqA hk hne'), hH, Fl4Spec.NonDet.when] at h + have hmin' : ¬ (toRefillA hd).rAmount < Fl4SpecA64.MIN_BUDGET := by + simp only [toRefillA]; rw [BitVec.lt_def, hav]; omega + split_ifs at h with hc + · rw [nd_bind_oneSc (postBody_eqA hk (hd := hd) (tl := tl) hx hper (toRefillA hd) rfl u)] at h + set l := schedUsedLA x.scRefillMax (⟨core.num.U64.wrapping_add hd.time u, core.num.U64.wrapping_sub hd.amount u⟩ :: tl) + ⟨core.num.U64.wrapping_add hd.time per, u⟩ with hl + have hk1 : (updKSc σ p (.schedContext { x with scRefills := l.map toRefillA } n)).kheap p = + some (.schedContext { x with scRefills := l.map toRefillA } n) := by exact updKSc_same _ _ _ + have hlne : l ≠ [] := by rw [hl]; exact schedUsedLA_ne + obtain ⟨ht, ha⟩ := schedUsedLA_head hm2 ⟨core.num.U64.wrapping_add hd.time u, core.num.U64.wrapping_sub hd.amount u⟩ + tl ⟨core.num.U64.wrapping_add hd.time per, u⟩ + rw [← hl] at ht ha + have hlam : Fl4SpecA64.MIN_BUDGET.toNat ≤ (l.headD zRA).amount.val := by + rcases ha with ha | ha <;> rw [ha] + · rw [wsub_val (by omega)]; omega + · have hb : hd.amount.val < 2 ^ 64 := by scalar_tac + dsimp only + rw [wadd_val (by rw [wsub_val (by omega)]; omega), wsub_val (by omega)]; omega + have hinsuf : ¬ Fl4SpecA64.the (Fl4SpecA64.refillHeadInsufficientWith (Z := Unit) Fl4SpecA64.MIN_BUDGET p + (updKSc σ p (.schedContext { x with scRefills := l.map toRefillA } n))) = true := by + simp only [refillHeadInsufficientWith_eqA Fl4SpecA64.MIN_BUDGET hk1 (by simpa using hlne), + refillHd_mapA (x := { x with scRefills := l.map toRefillA }) rfl hlne, Fl4SpecA64.the, toRefillA, + decide_eq_true_eq, BitVec.lt_def] + have e : (l.headD zRA).amount.bv.toNat = (l.headD zRA).amount.val := rfl + rw [e]; omega + unfold Fl4SpecA64.headInsufficientLoopWith at h + rw [wl_doneSc (C := fun _ s => Fl4SpecA64.the (Fl4SpecA64.refillHeadInsufficientWith Fl4SpecA64.MIN_BUDGET p s) = true) + (B := fun _ => Fl4SpecA64.mergeNonoverlappingHeadRefill p) hinsuf] at h + rw [show σ' = _ from by simpa only [Prod.mk.injEq, Set.singleton_eq_singleton_iff, and_true, true_and] using h.symm] + intro y m hy + rw [hk1] at hy; cases hy + rw [refillHd_mapA (x := { x with scRefills := l.map toRefillA }) rfl hlne] + refine ⟨by rw [BitVec.le_def]; exact hlam, Or.inr ?_⟩ + rw [hH]; simp only [toRefillA]; rw [ht, core.num.U64.wrapping_add_bv_eq] + · rw [nd_bind_oneSc (show Fl4Spec.NonDet.pure (σ := Fl4SpecA64.State Unit) () σ = ({((), σ)}, False) from rfl)] at h + have hinsuf : ¬ Fl4SpecA64.the (Fl4SpecA64.refillHeadInsufficientWith (Z := Unit) Fl4SpecA64.MIN_BUDGET p σ) = true := by + simp only [refillHeadInsufficientWith_eqA Fl4SpecA64.MIN_BUDGET hk hne', hH, Fl4SpecA64.the, decide_eq_true_eq]; exact hmin' + unfold Fl4SpecA64.headInsufficientLoopWith at h + rw [wl_doneSc (C := fun _ s => Fl4SpecA64.the (Fl4SpecA64.refillHeadInsufficientWith Fl4SpecA64.MIN_BUDGET p s) = true) + (B := fun _ => Fl4SpecA64.mergeNonoverlappingHeadRefill p) hinsuf] at h + rw [show σ' = _ from by simpa only [Prod.mk.injEq, Set.singleton_eq_singleton_iff, and_true, true_and] using h.symm] + intro y m hy + rw [hk] at hy; cases hy + refine ⟨?_, Or.inl rfl⟩ + rw [hH]; simp only [toRefillA]; rw [BitVec.le_def, hav]; omega + +end Periodic + +section Concrete +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **FL4's round-robin charge** is `rrChargeLA` on the list. -/ +theorem rr_charge_wordsA {s0 s : S} {sc : U64} {n : Nat} (h : RingAtA st s0 s sc n) (c : U64) : + (do + let hi ← obj.Sc.refill_head st sc s + let ti ← obj.Sc.refill_tail st sc s + let h ← sporadic.get st s sc hi + let i ← lift (core.num.U64.wrapping_sub h.amount c) + let s4 ← sporadic.put st s sc hi { h with amount := i } + let t ← sporadic.get st s4 sc ti + let i1 ← lift (core.num.U64.wrapping_add t.amount c) + sporadic.put st s4 sc ti { t with amount := i1 }) ⦃ s' => RingAtA st s0 s' sc n ∧ + L.refills s' sc = rrChargeLA (L.refills s sc) c ⦄ := by + step with obj.Sc.refill_head.spec as ⟨hi, hhi⟩ + step with obj.Sc.refill_tail.spec as ⟨ti, hti⟩ + step with sporadic.get.spec as ⟨hr, hhr⟩ + simp only [lift, bind_ok] + step with sporadic.put.spec as ⟨s4, hs4⟩ + step with sporadic.get.spec as ⟨tr, htr⟩ + try simp only [lift, bind_ok] + refine WP.spec_mono (sporadic.put.spec s4 sc ti _) ?_ + rintro s5 rfl + subst hhi hti hhr hs4 htr + have hW := h.wf + obtain ⟨h8, hb, hfit⟩ := hW.bounds + have hhl := hW.head_lt + set r1 : sporadic.Refill := ⟨(L.refill s sc (L.rhead s sc)).time, core.num.U64.wrapping_sub + (L.refill s sc (L.rhead s sc)).amount c⟩ with hr1 + have h1 : RingAtA st s0 (L.putS s sc (L.rhead s sc) r1) sc n := h.putS hhl r1 + have e1 : L.refills (L.putS s sc (L.rhead s sc) r1) sc = r1 :: (L.refills s sc).tail := by + rw [refills_ofA (hW.size_putS hhl) (fun k hk => hW.rf_set_head (by have := hW.size_le; omega)), + refills_set_headA hW] + have et : L.rtail (L.putS s sc (L.rhead s sc) r1) sc = L.rtail s sc := rtail_putS h8 (by omega) + rw [← et] + refine ⟨h1.putS h1.wf.tail_lt _, ?_⟩ + rw [refills_set_tailA h1.wf, refills_tail_eqA h1.wf, e1] + unfold rrChargeLA + rw [← head_eqA hW] + +/-- The conditional ring charge in `commit_time` for the bound current SC. +The post-state head remains ready even before ConfigureFlags resets the ring. -/ +theorem cfgCommitRingA {A : Type} {ar : machine.Arch A} + {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} + {σ : Fl4SpecA64.State Unit} {u : Fl4SpecA64.ObjRef} (H : SchedSRB st Z m κ s σ u) + (a : A) {sc : U64} (hc : sc ∈ Z.C) (hcs : L.cscW s = sc) + (hcur : σ.curSc = scNameA m sc) {x : Fl4SpecA64.SchedContext} {n : Nat} + (hk : σ.kheap (scNameA m sc) = some (.schedContext x n)) + (ha : Fl4SpecA64.scActive x = true) (hi : NotIdleScA m (scNameA m sc)) + (ht : x.scTcb = none ∨ x.scTcb = some u) (hnr : u ∉ σ.releaseQueue) (hoff : OffOKA σ) (hore : OffReadyA σ) + (hagree : Fl4SpecA64.fl4MinBudget = Fl4SpecA64.MIN_BUDGET) + (hlimit : sporadic.max_release_time ar a ⦃ v => v.bv = Fl4SpecA64.fl4MaxReleaseTime ⦄) + (hmin : sporadic.min_budget ar a ⦃ v => v.bv = Fl4SpecA64.fl4MinBudget ⦄) + (c : U64) (hco : c.bv = σ.consumedTime) : + (if c > 0#u64 then do + let b ← sporadic.is_round_robin st s sc + if b then do + let hi ← obj.Sc.refill_head st sc s + let ti ← obj.Sc.refill_tail st sc s + let h ← sporadic.get st s sc hi + let i ← lift (core.num.U64.wrapping_sub h.amount c) + let s4 ← sporadic.put st s sc hi { h with amount := i } + let t ← sporadic.get st s4 sc ti + let i1 ← lift (core.num.U64.wrapping_add t.amount c) + sporadic.put st s4 sc ti { t with amount := i1 } + else sporadic.budget_check st ar s a c + else ok s) ⦃ s' => ∃ y, + let σ' := updKSc σ (scNameA m sc) (.schedContext y n) + Fl4Spec.NonDet.when (0 < c.bv) (do + let robin ← Fl4SpecA64.isRoundRobin (Z := Unit) (scNameA m sc) + if robin then Fl4SpecA64.refillBudgetCheckRoundRobin c.bv else Fl4SpecA64.fl4RefillBudgetCheck c.bv) σ = + ({((), σ')}, False) ∧ + SchedSRB st Z m κ s' σ' u ∧ y = { x with scRefills := y.scRefills } ∧ + Fl4SpecA64.refillReady σ.curTime (Fl4SpecA64.refillHd y) = true ∧ WritesIn st s s' (ScObjA sc n) ⦄ := by + obtain ⟨x0, n0, hk0, hxw⟩ := H.scr.sc sc hc + rw [hk] at hk0; cases hk0 + have hx := hxw.strong ha + have hne := refills_neL hx.wf + have hmin' : sporadic.min_budget ar a ⦃ v => v.bv = Fl4SpecA64.MIN_BUDGET ⦄ := by + simpa only [hagree] using hmin + have hkCur : σ.kheap σ.curSc = some (.schedContext x n) := by rw [hcur]; exact hk + have hready0 : Fl4SpecA64.refillReady σ.curTime (Fl4SpecA64.refillHd x) = true := by + have h := hore x n hkCur + change decide ((Fl4SpecA64.refillHd x).rTime + (0 : Fl4SpecA64.Ticks) ≤ σ.curTime) = true + have hz : (Fl4SpecA64.refillHd x).rTime + (0 : Fl4SpecA64.Ticks) = (Fl4SpecA64.refillHd x).rTime := by bv_omega + rw [hz] + apply decide_eq_true + rw [BitVec.le_def]; omega + have rebuild (s' : S) (y : Fl4SpecA64.SchedContext) + (hSC : ScStateRelA st Z.C m s' (updKSc σ (scNameA m sc) (.schedContext y n))) + (hw : WritesIn st s s' (ScObjA sc n)) (hR : RingAtA st s s' sc n) + (hy : y = { x with scRefills := y.scRefills }) + (hp : y.scPeriod ≠ 0 → RefOKA y) (hr : y.scPeriod = 0 → RRokA y) : + SchedSRB st Z m κ s' (updKSc σ (scNameA m sc) (.schedContext y n)) u := by + obtain ⟨y0, n0, hk0, hrel⟩ := hSC.sc sc hc + have hk1 := updKSc_same σ (scNameA m sc) (.schedContext y n) + rw [hk1] at hk0; cases hk0 + refine H.scReconfA hc hk hw hrel (by rw [hy]) ht hnr hp hr ?_ + have e : scCoreA sc = scWA sc 16#u64 := by unfold scCoreA scWA layout.sc.CORE; rfl + rw [e]; exact hR.hdr (by simp) (by simp) + by_cases hc0 : c > 0#u64 + · rw [if_pos hc0] + have hlt : 0 < c.bv := (u64_lt_bvA 0#u64 c).1 hc0 + obtain ⟨hcap, hroff, hread⟩ := hoff (hco ▸ hlt) x n hkCur (fun _ => ha) + rw [← hco, hagree] at hcap + have hsuff : Fl4SpecA64.refillSufficient c.bv (Fl4SpecA64.refillHd x) = true := + decide_eq_true hcap + refine WP.spec_bind (is_round_robin_refinesA H.scr hc hk) ?_ + intro b hb + have spec (y : Fl4SpecA64.SchedContext) + (he : (if b = true then Fl4SpecA64.refillBudgetCheckRoundRobin (Z := Unit) c.bv + else Fl4SpecA64.fl4RefillBudgetCheck c.bv) σ = + ({((), updKSc σ (scNameA m sc) (.schedContext y n))}, False)) : + Fl4Spec.NonDet.when (0 < c.bv) (do + let robin ← Fl4SpecA64.isRoundRobin (Z := Unit) (scNameA m sc) + if robin then Fl4SpecA64.refillBudgetCheckRoundRobin c.bv else Fl4SpecA64.fl4RefillBudgetCheck c.bv) σ = + ({((), updKSc σ (scNameA m sc) (.schedContext y n))}, False) := by + unfold Fl4Spec.NonDet.when + rw [if_pos hlt, nd_bind_oneSc hb]; exact he + cases hb0 : b + · simp only [Bool.false_eq_true, ↓reduceIte] + have hper : x.scPeriod ≠ 0 := by + intro e + rw [isRoundRobin_eqA hk, e, hb0] at hb + have h : ({(true, σ)} : Set (Bool × Fl4SpecA64.State Unit)) = {(false, σ)} := by + simpa only [decide_true, Prod.mk.injEq, and_true] using hb + simpa using h + have hper' : L.rperiod s sc ≠ 0#u64 := by + intro e; apply hper; rw [hx.period, e]; rfl + obtain ⟨_, hp, hsum, hmh⟩ := H.inv.refills _ _ _ hk ha hper + have hp' : PosA (L.refills s sc) := by + intro v hv + exact hp (toRefillA v) (by rw [hx.refills]; exact List.mem_map_of_mem hv) + have hsum' : lsumA (L.refills s sc) < 2 ^ 64 := by rwa [lsumA_eq_map, ← hx.refills] + have minh : Fl4SpecA64.MIN_BUDGET.toNat ≤ lsumA (L.refills s sc) := by + rw [BitVec.le_def, refillHd_mapA hx.refills hne] at hmh + obtain ⟨v, vs, e⟩ := List.exists_cons_of_ne_nil hne + rw [e, List.headD_cons] at hmh; rw [e, lsumA_cons] + exact Nat.le_trans hmh (Nat.le_add_right _ _) + refine WP.spec_mono (budget_check_refinesWithA Fl4SpecA64.fl4MaxReleaseTime Fl4SpecA64.MIN_BUDGET + a H.scr H.scp hcs hcur hc hk ha hlimit hmin' hper' hp' hsum' minh c) ?_ + rintro s' ⟨σ', en, hS, _, hw, hP, hR⟩ + obtain ⟨y, rfl, hy, hny, hpy, hsy, hmy⟩ := hP x n hk + have hfinal : Fl4SpecA64.fl4RefillBudgetCheck (Z := Unit) c.bv σ = + ({((), updKSc σ (scNameA m sc) (.schedContext y n))}, False) := by + unfold Fl4SpecA64.fl4RefillBudgetCheck; rw [hagree]; exact en + have hm2 : 2 ≤ x.scRefillMax := by rw [← hx.max]; simpa using hx.wf.min_le + have time := (fl4RefillBudgetCheck_headA hcur hk hx.refills hne hx.period hper hm2 c hsuff hagree hfinal + y n (updKSc_same _ _ _)).2 + replace hroff : Fl4SpecA64.refillReady' c.bv σ.curTime (Fl4SpecA64.refillHd x) = true := by rwa [hco] + have hready : Fl4SpecA64.refillReady σ.curTime (Fl4SpecA64.refillHd y) = true := by + change decide ((Fl4SpecA64.refillHd y).rTime + (0 : Fl4SpecA64.Ticks) ≤ σ.curTime) = true + have hz : (Fl4SpecA64.refillHd y).rTime + (0 : Fl4SpecA64.Ticks) = (Fl4SpecA64.refillHd y).rTime := by bv_omega + rw [hz] + rcases time with e | e + · have hz' : (Fl4SpecA64.refillHd x).rTime + (0 : Fl4SpecA64.Ticks) = (Fl4SpecA64.refillHd x).rTime := by bv_omega + change decide ((Fl4SpecA64.refillHd x).rTime + (0 : Fl4SpecA64.Ticks) ≤ σ.curTime) = true at hready0 + rw [hz'] at hready0 + simpa only [e] using hready0 + · change decide ((Fl4SpecA64.refillHd x).rTime + c.bv ≤ σ.curTime) = true at hroff + rw [e]; exact hroff + refine ⟨y, spec y (by simpa only [hb0, Bool.false_eq_true, ↓reduceIte] using hfinal), + rebuild _ y hS hw hR hy (fun _ => ⟨hny, hpy, hsy, hmy⟩) (fun e => ?_), hy, hready, hw⟩ + have hpy : y.scPeriod = x.scPeriod := by + have e := congrArg Fl4SpecA64.SchedContext.scPeriod hy + exact e + exact (hper (hpy.symm.trans e)).elim + · simp only [↓reduceIte] + have hper : x.scPeriod = 0 := by + rw [isRoundRobin_eqA hk, hb0] at hb + have e : decide (x.scPeriod = 0) = true := by + simpa only [Prod.mk.injEq, Set.singleton_eq_singleton_iff, and_true] using hb + exact of_decide_eq_true e + refine WP.spec_mono (rr_charge_wordsA (RingAtA.refl hx.wf) c) ?_ + rintro s' ⟨hR, hl⟩ + obtain ⟨hS, _, hw⟩ := H.scr.ringAtA H.scp hc hk hx hR + let y := { x with scRefills := (rrChargeLA (L.refills s sc) c).map toRefillA } + have hy : y = { x with scRefills := y.scRefills } := rfl + rw [hl] at hS + have hh : Fl4SpecA64.refillHd x = toRefillA ((L.refills s sc).headD zRA) := refillHd_mapA hx.refills hne + obtain ⟨hc1, hc2⟩ := suff_natA hsuff + rw [hh] at hc1 hc2 + have rr := (H.inv.rr _ _ _ hk ha hper).2 hi + have hrr := rrChargeLA_ok hx.refills rr c hc1 hc2 + obtain ⟨first, mid, last, e⟩ := cfgRrSplitA hx.refills rr.1 + have hhead : (Fl4SpecA64.refillHd y).rTime = (Fl4SpecA64.refillHd x).rTime := by + unfold y + rw [hh, e, rrChargeLA_split]; rfl + exact ⟨y, spec y (by simpa only [hb0, ↓reduceIte] using rrCharge_eqA hcur hk hx.refills hne c), + rebuild _ y hS hw hR hy (fun e => (e hper).elim) (fun _ => hrr), hy, + by + change decide ((Fl4SpecA64.refillHd y).rTime + (0 : Fl4SpecA64.Ticks) ≤ σ.curTime) = true + rw [hhead] + exact hready0, hw⟩ + · rw [if_neg hc0, WP.spec_ok] + have hz : ¬ 0 < c.bv := fun e => hc0 ((u64_lt_bvA 0#u64 c).2 e) + rw [Fl4Spec.NonDet.when, if_neg hz] + refine ⟨x, ?_, ?_, rfl, hready0, WritesIn.refl _ _⟩ <;> rw [updKSc_selfA hk] + · rfl + · exact H + +theorem cfgResultAssocA {α β γ : Type} (r : Result α) + (f : α → Result β) (g : β → Result γ) : + Std.bind (Std.bind r f) g = Std.bind r (fun x => Std.bind (f x) g) := by + cases r with + | ret x => simp only [bind_ok] + | vis i k => simp only [bind_vis]; congr 1; funext y; cases i; exact PEmpty.elim y + | div => simp only [bind_div] + +/-- The bound, nonidle current SC path of the time commit, including +the SC's consumed word and the local node's consumed-time reset. -/ +theorem cfg_commit_timeA {A : Type} {ar : machine.Arch A} + {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} + {σ : Fl4SpecA64.State Unit} {u : Fl4SpecA64.ObjRef} (H : SchedSRB st Z m κ s σ u) + (a : A) {sc : U64} (hc : sc ∈ Z.C) (hcs : L.cscW s = sc) + (hcur : σ.curSc = scNameA m sc) {x : Fl4SpecA64.SchedContext} {n : Nat} + (hk : σ.kheap (scNameA m sc) = some (.schedContext x n)) + (ha : Fl4SpecA64.scActive x = true) (hi : NotIdleScA m (scNameA m sc)) + (ht : x.scTcb = none ∨ x.scTcb = some u) (hnr : u ∉ σ.releaseQueue) + (hoff : OffOKA σ) (hore : OffReadyA σ) + (hagree : Fl4SpecA64.fl4MinBudget = Fl4SpecA64.MIN_BUDGET) + (hlimit : sporadic.max_release_time ar a ⦃ v => v.bv = Fl4SpecA64.fl4MaxReleaseTime ⦄) + (hmin : sporadic.min_budget ar a ⦃ v => v.bv = Fl4SpecA64.fl4MinBudget ⦄) : + sched.commit_time st ar s a ⦃ s' => ∃ y, + let σ' := { updKSc σ (scNameA m sc) (.schedContext y n) with consumedTime := 0 } + Fl4SpecA64.fl4CommitTime σ = ({((), σ')}, False) ∧ + SchedSRB st Z m κ s' σ' u ∧ + y = { x with scRefills := y.scRefills, scConsumed := x.scConsumed + σ.consumedTime } ∧ + Fl4SpecA64.refillReady σ.curTime (Fl4SpecA64.refillHd y) = true ∧ + WritesIn st s s' (fun b => ScObjA sc n b ∨ b = L.nodeA s layout.node.CONSUMED) ⦄ := by + unfold sched.commit_time + refine WP.spec_bind (sched.cur_sc.spec s) ?_ + intro v hv + rw [hv.trans hcs] + refine WP.spec_bind (sc_active_refinesA H.scr hc hk) ?_ + intro active e + rw [getScActive_eqA hk, ha] at e + have ev : active = true := by simpa [Prod.mk.injEq] using (congrArg Prod.fst e).symm + subst active + simp only [↓reduceIte] + have hid : m.idleScOf m.curCore = specPtr (L.mem s (L.nodeA s layout.node.IDLE_SC)) := by + rw [H.sch.core] + exact H.sch.idleScOf _ H.sch.cpuCs + rw [eq_of_spec_fd (sched.idle_sc.specA (st := st) s), bind_ok, sc_ne_eqA, bind_ok, + idle_testA hid (H.sch.scOff _ hc), decide_eq_true hi.1] + simp only [↓reduceIte] + rw [eq_of_spec_fd (sched.consumed.specA (st := st) s), bind_ok, cfgResultAssocA] + let c := L.mem s (L.nodeA s layout.node.CONSUMED) + have hcons : c.bv = σ.consumedTime := H.rest.consumed.symm + refine WP.spec_bind (cfgCommitRingA H a hc hcs hcur hk ha hi ht hnr hoff hore hagree hlimit hmin c hcons) ?_ + rintro s1 ⟨z, hz, H1, hfields, hready, hw1⟩ + let σ1 := updKSc σ (scNameA m sc) (.schedContext z n) + have hk1 : σ1.kheap (scNameA m sc) = some (.schedContext z n) := updKSc_same _ _ _ + obtain ⟨z0, n0, hk0, R⟩ := H1.scr.sc sc hc + change σ1.kheap _ = _ at hk0 + rw [hk1] at hk0; cases hk0 + rw [cfgResultAssocA] + refine WP.spec_bind (obj.Sc.consumed.specSc s1 sc) ?_ + rintro old eOld + simp only [lift, bind_ok] + let total := core.num.U64.wrapping_add old c + have eTotal : total.bv = z.scConsumed + σ.consumedTime := by + unfold total + rw [core.num.U64.wrapping_add_bv_eq, eOld, ← R.consumed, hcons] + refine WP.spec_bind (obj.Sc.set_consumed.specSc s1 sc total) ?_ + rintro s2 rfl + obtain ⟨R2, hw2⟩ := R.set_consumed total + let y := { z with scConsumed := total.bv } + have hcore : L.mem (L.wr s1 (scWA sc 8#u64) total) (scCoreA sc) = L.mem s1 (scCoreA sc) := by + have e16 : scCoreA sc = scWA sc 16#u64 := by unfold scCoreA scWA layout.sc.CORE; rfl + rw [e16] + apply L.mem_wr_ne (R.hdr (by simp : (8#u64).val < 96) (by simp)).1 (R.hdr (by simp : (16#u64).val < 96) (by simp)).1 + intro eq + have hh : (scWA sc 8#u64).val = (scWA sc 16#u64).val := eq + have hf := R.bounds.2.2 + have ea := scWA_val (sc := sc) (K := 8#u64) (by simp; omega) + have eb := scWA_val (sc := sc) (K := 16#u64) (by simp; omega) + rw [ea, eb] at hh + simp at hh + have hza : Fl4SpecA64.scActive z = true := by + unfold Fl4SpecA64.scActive Fl4SpecA64.activeSc at ha ⊢ + have e := congrArg Fl4SpecA64.SchedContext.scRefillMax hfields + rwa [e] + have hzT : z.scTcb = none ∨ z.scTcb = some u := by + have e := congrArg Fl4SpecA64.SchedContext.scTcb hfields + change z.scTcb = x.scTcb at e + rw [e] + exact ht + have H2 := H1.scReconfA hc hk1 hw2 R2 rfl hzT hnr + (fun h => show RefOKA y from + (by + have Href := H1.inv.refills _ z n hk1 hza (show z.scPeriod ≠ 0 from h) + exact Href)) + (fun h => show RRokA y from + (by + have Hrr := (H1.inv.rr _ z n hk1 hza (show z.scPeriod = 0 from h)).2 hi + exact Hrr)) hcore + refine WP.spec_mono (sched.set_consumed.spec _ 0#u64) ?_ + rintro s3 rfl + obtain ⟨H3, hw3⟩ := H2.clearConsumedA + rw [updKSc_updKSc] at H3 + refine ⟨y, ?_, H3, ?_, hready, ((hw1.trans1 hw2).trans hw3).mono ?_⟩ + · unfold Fl4SpecA64.fl4CommitTime + rw [nd_gets_bindSc, hcur, nd_bind_oneSc (getScActive_eqA hk)] + rw [Fl4Spec.NonDet.when, if_pos ⟨ha, hi.1⟩, nd_bind_assocSc, nd_gets_bindSc] + repeat rw [nd_bind_assocSc] + rw [← hcons, nd_bind_oneSc hz, nd_bind_oneSc (updateSchedContext_eqA hk1 _)] + rw [updKSc_updKSc, hcons, ← eTotal] + rfl + · have e := congrArg (fun w : Fl4SpecA64.SchedContext => { w with scConsumed := total.bv }) hfields + have ec := congrArg Fl4SpecA64.SchedContext.scConsumed hfields + rw [eTotal, show z.scConsumed = x.scConsumed from ec] at e + unfold y + rw [eTotal, show z.scConsumed = x.scConsumed from ec] + exact e + · intro b hb + rcases hb with h | h + · exact Or.inl h + · refine Or.inr (h.trans ?_) + unfold LawfulStore.nodeA + rw [hw2.2.1, hw1.2.1, nodeAt_of_ksb hw2.1, nodeAt_of_ksb hw1.1] + +end Concrete + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScCfgDecodeA.lean b/proofs/Fl4ProofsA64/P3/ScCfgDecodeA.lean new file mode 100644 index 000000000..9448cd336 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScCfgDecodeA.lean @@ -0,0 +1,268 @@ +import Fl4ProofsA64.P3.ScCfgPlatA +import Fl4ProofsA64.P1.QueueSchedCtl +import Fl4ProofsA64.P3.CnodeCopyA +import Fl4ProofsA64.P3.ScRingA + +/-! The AArch64 ConfigureFlags argument decoder, factored before Restart. +[P] `ScCfgTimeAgreeA` supplies the timer and error-payload agreements. +-/ + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched + +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +/-- FL4's errors and the spec's, with `invalid_capability`. -/ +def ScCfgErrA (e : error.Exc) (e' : Fl4SpecA64.SyscallError) : Prop := + SysErrRelA e e' ∨ ∃ n : U64, e = .Sys (.InvalidCapability n) ∧ e' = .invalidCapability n.val + +/-- After a decode, either FL4 returned the spec's error with nothing +changed, or the spec decodes an invocation and `Q` relates it to FL4's +result. -/ +def ScCfgPostA {α : Type} (σ : Fl4SpecA64.State Unit) (m : Fl4SpecA64.SEMonad Unit α) (s : S) (a : A) + (Q : α → core.result.Result Unit error.Exc × S × A → Prop) + (res : core.result.Result Unit error.Exc × S × A) : Prop := + (∃ e e', res = (.Err e, s, a) ∧ m.run σ = ({(.error e', σ)}, False) ∧ ScCfgErrA e e') ∨ + (∃ inv, m.run σ = ({(.ok inv, σ)}, False) ∧ Q inv res) + +theorem ScCfgPostA.err {α : Type} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.SEMonad Unit α} {s : S} {a : A} {Q} + {e : error.Exc} {e' : Fl4SpecA64.SyscallError} + (h : m.run σ = ({(.error e', σ)}, False)) (he : ScCfgErrA e e') : + (ok ((.Err e, s, a) : core.result.Result Unit error.Exc × S × A)) ⦃ res => ScCfgPostA σ m s a Q res ⦄ := by + rw [WP.spec_ok]; exact Or.inl ⟨e, e', rfl, h, he⟩ + +theorem cfg_ucast_idA (x : BitVec 64) : Fl4SpecA64.ucast 64 x = x := by simp [Fl4SpecA64.ucast] + +theorem cfg_minUs_eqA {a : A} (hT : ScCfgTimeAgreeA ar a) : Fl4SpecA64.MIN_BUDGET_US = (20#u64 : U64).bv := by + unfold Fl4SpecA64.MIN_BUDGET_US; rw [hT.wcetUs]; rfl + +theorem cfg_isSc_absCapA (c : capability.Cap) : + Fl4SpecA64.isSchedContextCap (absCapA c) = (match c with | .SchedContext .. => true | _ => false) := by + cases c <;> rfl + +/-- The cap's valid size bounds in the abstract and concrete layout. -/ +theorem cfg_refill_maxA (sb : U64) (h64 : sb.val < 64) : + sporadic.refill_absolute_max sb ⦃ m => m.val = Fl4SpecA64.maxNumRefills sb.val ⦄ := by + refine WP.spec_mono (sporadic.refill_absolute_max.val sb) ?_ + intro m hm + rw [hm] + unfold absMax Fl4SpecA64.maxNumRefills Fl4SpecA64.sizeofSchedContextT Fl4SpecA64.wordSize Fl4SpecA64.refillSizeBytes + rw [if_neg (by omega)] + +/-- **`decode_sched_control` refines `decode_sched_control_invocation_flags`**: +FL4's errors are the spec's, and on success the spec's invocation is the one +`configure_flags` gets (`Q`), with the decoder's facts. -/ +theorem cfg_decode_sched_controlA (th : TypedHeapA) (s : S) (a : A) (hT : ScCfgTimeAgreeA ar a) + (σ : Fl4SpecA64.State Unit) (lbl : U64) (slbl : Fl4SpecA64.Data) + (hlbl : Fl4SpecA64.genInvocationType slbl = .schedControlConfigureFlags ↔ + lbl = fl4_abi.label.SchedControlConfigureFlags) + (cpu length buf ct : U64) (hct : sched.cur_thread st s = ok ct) (args : List U64) + (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s ct buf i = ok x) + (extra : Aeneas.Std.Array obj.Slot 3#usize) (x0 : U64) (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) + (excaps : List Fl4SpecA64.Cap) (hex : ExtraRelA th (L.mem s) x0 excaps) + (hsb : x0 ≠ 0#u64 → ∀ p sb, capAtA (L.mem s) x0 = .SchedContext p sb → sb.val < 64) + (Q : Fl4SpecA64.SchedControlInvocation → core.result.Result Unit error.Exc × S × A → Prop) + (hQ : ∀ (p sb bu pu er bd fl budget period : U64), x0 ≠ 0#u64 → capAtA (L.mem s) x0 = .SchedContext p sb → + args[0]? = some bu → args[1]? = some pu → args[2]? = some er → args[3]? = some bd → args[4]? = some fl → + budget.bv = Fl4SpecA64.fl4UsToTicks bu.bv → period.bv = Fl4SpecA64.fl4UsToTicks pu.bv → + Fl4SpecA64.fl4MinBudget ≤ budget.bv → budget.val ≤ period.val → er.val + 2 ≤ Fl4SpecA64.maxNumRefills sb.val → + (do + let s1 ← sched.set_thread_state st s ct sched.TS_RESTART + let (s2, a1) ← schedcontext.configure_flags st ar s1 a p cpu budget period + (core.num.U64.wrapping_add er sporadic.MIN_REFILLS) bd fl + ok (core.result.Result.Ok (), s2, a1)) ⦃ res => + Q (.invokeSchedControlConfigureFlags (specPtr p) (Fl4SpecA64.fl4UsToTicks bu.bv) (Fl4SpecA64.fl4UsToTicks pu.bv) + (er.val + 2) bd.bv fl.bv) res ⦄) : + schedcontext.decode_sched_control st ar s a lbl cpu length buf extra ⦃ res => + ScCfgPostA σ (Fl4SpecA64.decodeFl4SchedControlInvocation (Z := Unit) slbl (args.map UScalar.bv) excaps) + s a Q res ⦄ := by + unfold schedcontext.decode_sched_control Fl4SpecA64.decodeFl4SchedControlInvocation + by_cases hl : lbl = fl4_abi.label.SchedControlConfigureFlags + swap + · have : (lbl != fl4_abi.label.SchedControlConfigureFlags) = true := by simpa using hl + simp only [this, ↓reduceIte, error.illegal, bind_ok] + apply ScCfgPostA.err (e := .Sys .IllegalOperation) (e' := .illegalOperation) _ (Or.inl trivial) + have : ¬ Fl4SpecA64.genInvocationType slbl = .schedControlConfigureFlags := fun h => hl (hlbl.1 h) + simp only [Fl4Spec.NonDetE.unlessE, this, ↓reduceIte, Fl4Spec.NonDetE.throwError, ndE_throw_bindA]; rfl + have hne : (lbl != fl4_abi.label.SchedControlConfigureFlags) = false := by simpa using hl + have hsl' : Fl4SpecA64.genInvocationType slbl = .schedControlConfigureFlags := hlbl.2 hl + simp only [hne, Bool.false_eq_true, ↓reduceIte, Fl4Spec.NonDetE.unlessE, hsl', ndE_pure_bindA, hct, bind_ok] + unfold schedcontext.extra_cap + have h03 : ¬ (0#usize ≥ 3#usize) := by decide + simp only [h03, ↓reduceIte, hx0, bind_ok, obj.Slot.is_null] + rcases hex with ⟨rfl, rfl⟩ | ⟨hx0n, -, a0, rest0, hca0, rfl⟩ + · simp only [decide_true, ↓reduceIte, bind_ok, error.truncated] + apply ScCfgPostA.err (e := .Sys .TruncatedMessage) (e' := .truncatedMessage) _ (Or.inl trivial) + simp only [List.length_nil, Fl4Spec.NonDetE.whenE, ↓reduceIte, Fl4Spec.NonDetE.throwError, ndE_throw_bindA] + rfl + generalize hc0 : capAtA (L.mem s) x0 = c0 at * + have hcRead : obj.Slot.cap st x0 s = ok c0 := by + rw [Fl4ProofsA64.P1.W7.R.ST.capA_eq, ← capAtA_eq, hc0] + simp only [hx0n, decide_false, Bool.false_eq_true, ↓reduceIte, hcRead, bind_ok, List.length_cons, + Fl4Spec.NonDetE.whenE, Nat.add_one_ne_zero, ndE_pure_bindA] + by_cases h5 : length < 5#u64 + · simp only [h5, ↓reduceIte, error.truncated, bind_ok] + apply ScCfgPostA.err (e := .Sys .TruncatedMessage) (e' := .truncatedMessage) _ (Or.inl trivial) + have : (args.map UScalar.bv).length < Fl4SpecA64.timeArgLen * 2 + 3 := by + simp only [List.length_map, Fl4SpecA64.timeArgLen]; scalar_tac + simp only [this, ↓reduceIte, Fl4Spec.NonDetE.throwError, ndE_throw_bindA]; rfl + have h5' : ¬ (args.map UScalar.bv).length < Fl4SpecA64.timeArgLen * 2 + 3 := by + simp only [List.length_map, Fl4SpecA64.timeArgLen]; scalar_tac + simp only [h5, ↓reduceIte, h5', ndE_pure_bindA] + obtain ⟨bu, pu, er, bd, fl, rest, rfl⟩ : ∃ bu pu er bd fl rest, args = bu :: pu :: er :: bd :: fl :: rest := by + match args with + | bu :: pu :: er :: bd :: fl :: rest => exact ⟨bu, pu, er, bd, fl, rest, rfl⟩ + | [] | [_] | [_, _] | [_, _, _] | [_, _, _, _] => exfalso; simp at hlen; scalar_tac + have h20 : sporadic.min_budget_us = ok 20#u64 := by unfold sporadic.min_budget_us; rfl + obtain ⟨mx, hmax, hmx⟩ := hT.maxPeriod + rw [hmax] + simp only [bind_ok] + simp only [h20, bind_ok, hargs 0#u64 bu (by simp), hargs 1#u64 pu (by simp), hargs 2#u64 er (by simp), + hargs 3#u64 bd (by simp), hargs 4#u64 fl (by simp)] + simp only [List.map_cons, Fl4Spec.NonDetE.returnOk, ndE_pure_bindA, Fl4SpecA64.parseTimeArgA, Fl4SpecA64.timeArgLen, + List.getElem?_cons_zero, List.getElem?_cons_succ, Fl4SpecA64.the, cfg_ucast_idA, List.head?_cons] + obtain ⟨hwf, hce⟩ := hca0 + obtain ⟨hsc0, hsc1⟩ : Fl4SpecA64.isSchedContextCap a0 = Fl4SpecA64.isSchedContextCap (absCapA c0) ∧ + (∀ p sb, c0 = .SchedContext p sb → a0 = absCapA c0) := by + by_cases hpt : ∃ p asid va, c0 = .PageTable p true asid va + · obtain ⟨p, asid, va, e⟩ := hpt + subst e + obtain ⟨asid', va', e', -⟩ := CapEqA.pt_mapped_iff.1 hce + subst e' + exact ⟨rfl, fun _ _ h => by cases h⟩ + · have e := hce.eq_abs (fun p asid va h => hpt ⟨p, asid, va, h⟩) + exact ⟨by rw [e], fun _ _ _ => e.symm⟩ + have hmin : Fl4SpecA64.MIN_BUDGET_US = (20#u64 : U64).bv := cfg_minUs_eqA hT + -- the budget and period against the maximum + by_cases g1 : bu > mx + · have g1' : bu.bv > Fl4SpecA64.fl4MaxPeriodUs := by rw [← hmx]; exact (u64_lt_bvA mx bu).1 g1 + simp only [g1, ↓reduceIte, error.range, bind_ok] + refine ScCfgPostA.err (e := .Sys (.RangeError 20#u64 mx)) + (e' := .rangeError Fl4SpecA64.MIN_BUDGET_US Fl4SpecA64.fl4MaxPeriodUs) ?_ (Or.inl ⟨hmin, hmx.symm⟩) + simp only [g1', ↓reduceIte, Fl4Spec.NonDetE.throwError, ndE_throw_bindA]; rfl + have g1' : ¬ bu.bv > Fl4SpecA64.fl4MaxPeriodUs := by rw [← hmx]; exact fun h => g1 ((u64_lt_bvA mx bu).2 h) + simp only [g1, g1', ↓reduceIte, ndE_pure_bindA] + refine WP.spec_bind (hT.usToTicks bu) ?_ + intro budget hbud + by_cases g2 : pu > mx + · have g2' : pu.bv > Fl4SpecA64.fl4MaxPeriodUs := by rw [← hmx]; exact (u64_lt_bvA mx pu).1 g2 + simp only [g2, ↓reduceIte, error.range, bind_ok] + refine ScCfgPostA.err (e := .Sys (.RangeError 20#u64 mx)) + (e' := .rangeError Fl4SpecA64.MIN_BUDGET_US Fl4SpecA64.fl4MaxPeriodUs) ?_ (Or.inl ⟨hmin, hmx.symm⟩) + simp only [g2', ↓reduceIte, Fl4Spec.NonDetE.throwError, ndE_throw_bindA]; rfl + have g2' : ¬ pu.bv > Fl4SpecA64.fl4MaxPeriodUs := by rw [← hmx]; exact fun h => g2 ((u64_lt_bvA mx pu).2 h) + simp only [g2, g2', ↓reduceIte, ndE_pure_bindA] + refine WP.spec_bind (hT.usToTicks pu) ?_ + intro period hper + refine WP.spec_bind hT.minBudget ?_ + intro mb hmb + have c3 : budget < mb ↔ Fl4SpecA64.fl4UsToTicks bu.bv < Fl4SpecA64.fl4MinBudget := by + rw [u64_lt_bvA, hbud, hmb] + have c4 : period < mb ↔ Fl4SpecA64.fl4UsToTicks pu.bv < Fl4SpecA64.fl4MinBudget := by + rw [u64_lt_bvA, hper, hmb] + have c5 : budget > period ↔ Fl4SpecA64.fl4UsToTicks pu.bv < Fl4SpecA64.fl4UsToTicks bu.bv := by + rw [gt_iff_lt, u64_lt_bvA, hbud, hper] + by_cases g3 : budget < mb + · simp only [g3, ↓reduceIte, error.range, bind_ok] + refine ScCfgPostA.err (e := .Sys (.RangeError 20#u64 mx)) + (e' := .rangeError Fl4SpecA64.MIN_BUDGET_US Fl4SpecA64.fl4MaxPeriodUs) ?_ (Or.inl ⟨hmin, hmx.symm⟩) + simp only [c3.1 g3, ↓reduceIte, Fl4Spec.NonDetE.throwError, ndE_throw_bindA]; rfl + simp only [g3, (not_congr c3).1 g3, ↓reduceIte, ndE_pure_bindA] + by_cases g4 : period < mb + · simp only [g4, ↓reduceIte, error.range, bind_ok] + refine ScCfgPostA.err (e := .Sys (.RangeError 20#u64 mx)) + (e' := .rangeError Fl4SpecA64.MIN_BUDGET_US Fl4SpecA64.fl4MaxPeriodUs) ?_ (Or.inl ⟨hmin, hmx.symm⟩) + simp only [c4.1 g4, ↓reduceIte, Fl4Spec.NonDetE.throwError, ndE_throw_bindA]; rfl + simp only [g4, (not_congr c4).1 g4, ↓reduceIte, ndE_pure_bindA] + by_cases g5 : budget > period + · simp only [g5, ↓reduceIte, error.range, bind_ok] + refine ScCfgPostA.err (e := .Sys (.RangeError 20#u64 pu)) + (e' := .rangeError Fl4SpecA64.MIN_BUDGET_US pu.bv) ?_ (Or.inl ⟨hmin, rfl⟩) + simp only [c5.1 g5, ↓reduceIte, Fl4Spec.NonDetE.throwError, ndE_throw_bindA]; rfl + simp only [g5, (not_congr c5).1 g5, ↓reduceIte, ndE_pure_bindA] + have hmbB : Fl4SpecA64.fl4MinBudget ≤ budget.bv := by + rw [← hmb]; exact BitVec.not_lt.1 (fun h => g3 ((u64_lt_bvA budget mb).2 h)) + have hbp : budget.val ≤ period.val := by + have := (not_congr ((u64_lt_bvA period budget))).1 g5 + rw [BitVec.not_lt, BitVec.le_def, UScalar.bv_toNat, UScalar.bv_toNat] at this; exact this + -- The cap's size determines the refill bound. + cases c0 + case SchedContext p sb => + dsimp only + obtain rfl := hsc1 p sb rfl + have h7 : 7 ≤ sb.val := hwf + have h64 := hsb hx0n p sb rfl + refine WP.spec_bind (cfg_refill_maxA sb h64) ?_ + intro mr hmr + simp only [lift, bind_ok] + have hN2 : 2 ≤ Fl4SpecA64.maxNumRefills sb.val := by + unfold Fl4SpecA64.maxNumRefills Fl4SpecA64.sizeofSchedContextT Fl4SpecA64.wordSize Fl4SpecA64.refillSizeBytes + have : 128 ≤ 2 ^ sb.val := by + calc 128 = 2 ^ 7 := by norm_num + _ ≤ 2 ^ sb.val := Nat.pow_le_pow_right (by norm_num) h7 + omega + have hm2 : (sporadic.MIN_REFILLS).val = 2 := by unfold sporadic.MIN_REFILLS; rfl + have hsz : UScalar.size .U64 = 2 ^ 64 := by simp [UScalar.size_def, UScalarTy.numBits] + have hNb : Fl4SpecA64.maxNumRefills sb.val < 2 ^ 64 := by rw [← hmr]; exact mr.hBounds + have hi : (core.num.U64.wrapping_sub mr sporadic.MIN_REFILLS).val = Fl4SpecA64.maxNumRefills sb.val - 2 := by + rw [core.num.U64.wrapping_sub_val_eq, hm2, hmr, hsz] + rw [show Fl4SpecA64.maxNumRefills sb.val + (2 ^ 64 - 2) = (Fl4SpecA64.maxNumRefills sb.val - 2) + 2 ^ 64 by omega, + Nat.add_mod_right, Nat.mod_eq_of_lt (by omega)] + have hA : (Fl4Spec.NonDetE.assertE (Fl4SpecA64.MIN_REFILLS ≤ Fl4SpecA64.maxNumRefills sb.val) : + Fl4SpecA64.SEMonad Unit Unit).run σ = ({(.ok (), σ)}, False) := by + apply run_liftEA + show (({x | Fl4SpecA64.MIN_REFILLS ≤ Fl4SpecA64.maxNumRefills sb.val ∧ x = ((), σ)}, + ¬ Fl4SpecA64.MIN_REFILLS ≤ Fl4SpecA64.maxNumRefills sb.val) : Set (Unit × Fl4SpecA64.State Unit) × Prop) = + ({((), σ)}, False) + have : Fl4SpecA64.MIN_REFILLS ≤ Fl4SpecA64.maxNumRefills sb.val := hN2 + simp [this] + have hcap : Fl4SpecA64.maxRefillsCap (absCapA (.SchedContext p sb)) = Fl4SpecA64.maxNumRefills sb.val := by + show Fl4SpecA64.maxNumRefills (Fl4SpecA64.minSchedContextBits + (sb.val - Fl4SpecA64.minSchedContextBits)) = _ + rw [Nat.add_sub_cancel' hwf] + simp only [cfg_isSc_absCapA, not_true_eq_false, ↓reduceIte, ndE_pure_bindA, hcap] + by_cases g6 : er > core.num.U64.wrapping_sub mr sporadic.MIN_REFILLS + · have g6' : Fl4SpecA64.unat er.bv > Fl4SpecA64.maxNumRefills sb.val - Fl4SpecA64.MIN_REFILLS := by + have : (core.num.U64.wrapping_sub mr sporadic.MIN_REFILLS).val < er.val := g6 + simp only [Fl4SpecA64.unat, UScalar.bv_toNat, Fl4SpecA64.MIN_REFILLS]; omega + simp only [g6, ↓reduceIte, error.range, bind_ok] + refine ScCfgPostA.err (e := .Sys (.RangeError 0#u64 (core.num.U64.wrapping_sub mr sporadic.MIN_REFILLS))) + (e' := .rangeError 0 (BitVec.ofNat 64 (Fl4SpecA64.maxNumRefills sb.val - Fl4SpecA64.MIN_REFILLS))) + ?_ (Or.inl ⟨rfl, ?_⟩) + · rw [run_bind_okA hA] + simp only [g6', ↓reduceIte, Fl4Spec.NonDetE.throwError, ndE_throw_bindA]; rfl + · apply BitVec.eq_of_toNat_eq + rw [UScalar.bv_toNat, hi, BitVec.toNat_ofNat, Fl4SpecA64.MIN_REFILLS, Nat.mod_eq_of_lt] + omega + · have g6' : ¬ Fl4SpecA64.unat er.bv > Fl4SpecA64.maxNumRefills sb.val - Fl4SpecA64.MIN_REFILLS := by + have : ¬ (core.num.U64.wrapping_sub mr sporadic.MIN_REFILLS).val < er.val := g6 + simp only [Fl4SpecA64.unat, UScalar.bv_toNat, Fl4SpecA64.MIN_REFILLS]; omega + have hle : er.val + 2 ≤ Fl4SpecA64.maxNumRefills sb.val := by + have : ¬ (core.num.U64.wrapping_sub mr sporadic.MIN_REFILLS).val < er.val := g6 + omega + simp only [g6, ↓reduceIte] + refine WP.spec_mono (hQ p sb bu pu er bd fl budget period hx0n rfl rfl rfl rfl rfl rfl hbud hper hmbB hbp hle) ?_ + intro res h + refine Or.inr ⟨_, ?_, h⟩ + rw [run_bind_okA hA] + have g6'' : ¬ er.val > Fl4SpecA64.maxNumRefills sb.val - 2 := by omega + simp only [g6'', g6', ↓reduceIte, ndE_pure_bindA, Fl4SpecA64.unat, UScalar.bv_toNat, Fl4SpecA64.MIN_REFILLS] + rfl + all_goals + simp only [error.invalid_cap, bind_ok] + refine ScCfgPostA.err (e' := .invalidCapability 1) ?_ (Or.inr ⟨1#u64, rfl, rfl⟩) + simp only [hsc0, cfg_isSc_absCapA, Bool.false_eq_true, not_false_eq_true, ↓reduceIte, Fl4Spec.NonDetE.throwError, + ndE_throw_bindA] + rfl + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScCfgEntryA.lean b/proofs/Fl4ProofsA64/P3/ScCfgEntryA.lean new file mode 100644 index 000000000..f39bfee6f --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScCfgEntryA.lean @@ -0,0 +1,141 @@ +import Fl4ProofsA64.P3.ScBootA +import Fl4ProofsA64.P3.ScSwitchA +import Fl4ProofsA64.P3.JEntW1A + +/-! +[J] The current SC and offset invariants used by the x86 ConfigureFlags and +entry proofs, for the AArch64 current-core view. The idle SC has the view's +reserved name `idleScPtr`. The boot views establish the predicates below. +-/ + +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3.Sched + +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4ProofsA64.P1.W7.R.ST.BootWit + +noncomputable section + +/-- [J] The current TCB is bound to the current SC and is outside the IPC +and release queues. An ordinary current SC is active. -/ +structure CurInvA (σ : State Unit) : Prop where + tcb : ∃ t, σ.kheap σ.curThread = some (.tcb t) ∧ t.tcbSchedContext = some σ.curSc ∧ + ipcQueuedThreadState t.tcbState = false + sc : ∃ y n, σ.kheap σ.curSc = some (.schedContext y n) + active : σ.curSc ≠ idleScPtr → ∀ y n, σ.kheap σ.curSc = some (.schedContext y n) → scActive y = true + notRel : σ.curThread ∉ σ.releaseQueue + +/-- [J] The current head, displaced by the elapsed consumption without +wraparound, is still ready. This is the pre-charge part of `OffOKA`. -/ +def OffReadyA (σ : State Unit) : Prop := + ∀ y n, σ.kheap σ.curSc = some (.schedContext y n) → + (refillHd y).rTime.toNat + σ.consumedTime.toNat ≤ σ.curTime.toNat + +/-- [J] A positive offset can be billed, leaves the FL4 minimum, and +keeps the head ready before and after moving it. -/ +def OffOKA (σ : State Unit) : Prop := + 0 < σ.consumedTime → ∀ y n, σ.kheap σ.curSc = some (.schedContext y n) → + (σ.curSc ≠ idleScPtr → scActive y = true) → + fl4MinBudget ≤ refillCapacity σ.consumedTime (refillHd y) ∧ + refillReady' σ.consumedTime σ.curTime (refillHd y) = true ∧ + refillReady σ.curTime (refillHd y) = true + +/-- [J] The switch target is schedulable and the idle thread has its +distinguished SC, idle state, and no release-queue membership. -/ +def ActIdleA (σ : State Unit) : Prop := + (∀ t, σ.schedulerAction = .switchThread t → schedulable t σ = true) ∧ + ∃ t, σ.kheap σ.idleThread = some (.tcb t) ∧ t.tcbSchedContext = some idleScPtr ∧ + t.tcbState = .idleThreadState ∧ σ.idleThread ∉ σ.releaseQueue + +theorem OffReadyA.ok {σ : State Unit} (h : OffReadyA σ) + (hs : ∀ y n, σ.kheap σ.curSc = some (.schedContext y n) → + fl4MinBudget ≤ refillCapacity σ.consumedTime (refillHd y)) : OffOKA σ := by + intro _ y n hy _ + have h1 := h y n hy + refine ⟨hs y n hy, ?_, ?_⟩ + · simp only [refillReady', decide_eq_true_eq, BitVec.le_def, BitVec.toNat_add] + have hmax := σ.curTime.isLt + rw [Nat.mod_eq_of_lt (by omega)]; exact h1 + · change decide ((refillHd y).rTime + (0 : Ticks) ≤ σ.curTime) = true + have h0 : (refillHd y).rTime + (0 : Ticks) = (refillHd y).rTime := by bv_omega + rw [h0] + apply decide_eq_true + rw [BitVec.le_def] + omega + +theorem CurInvA.congr {σ σ' : State Unit} (h : CurInvA σ) (hk : σ'.kheap = σ.kheap) + (ht : σ'.curThread = σ.curThread) (hc : σ'.curSc = σ.curSc) (hr : σ'.releaseQueue = σ.releaseQueue) : + CurInvA σ' := by + obtain ⟨t, hkt, hsc, hb⟩ := h.tcb + refine ⟨⟨t, by rw [hk, ht]; exact hkt, by rw [hc]; exact hsc, hb⟩, ?_, ?_, ?_⟩ + · simpa only [hk, hc] using h.sc + · simpa only [hk, hc] using h.active + · simpa only [ht, hr] using h.notRel + +theorem OffOKA.congr {σ σ' : State Unit} (h : OffOKA σ) + (hc : σ'.curSc = σ.curSc) (ht : σ'.curTime = σ.curTime) (hu : σ'.consumedTime = σ.consumedTime) + (hk : σ'.kheap σ.curSc = σ.kheap σ.curSc) : OffOKA σ' := by + unfold OffOKA at * + simpa only [hc, ht, hu, hk] using h + +private theorem idleCurAtBootA {σ : State Unit} (hcur : σ.curThread = σ.idleThread) + (hsc : σ.curSc = idleScPtr) {t : Tcb} (ht : σ.kheap σ.idleThread = some (.tcb t)) + (hb : t.tcbSchedContext = some idleScPtr) (hs : t.tcbState = .idleThreadState) + (hk : ∃ y n, σ.kheap idleScPtr = some (.schedContext y n)) (hr : σ.releaseQueue = []) : CurInvA σ := by + refine ⟨⟨t, by rw [hcur]; exact ht, by rw [hsc]; exact hb, by rw [hs]; rfl⟩, ?_, ?_, ?_⟩ + · simpa only [hsc] using hk + · intro e; exact (e hsc).elim + · simp [hr] + +private theorem offAtBootA {σ : State Unit} {y : SchedContext} {n : Nat} + (hy : σ.kheap σ.curSc = some (.schedContext y n)) (hc : σ.consumedTime = 0) + (ht : (refillHd y).rTime.toNat ≤ σ.curTime.toNat) : OffOKA σ ∧ OffReadyA σ := by + refine ⟨fun hu => ?_, ?_⟩ + · rw [hc] at hu; exact (BitVec.lt_irrefl _ hu).elim + · intro z k hz + rw [hy] at hz; cases hz + simp only [hc] + simpa using ht + +theorem w0_curTimingA {kb ib now mt : U64} {nc : Nat} (hL : BootLayA ib nc) : + CurInvA (bootSA kb ib nc now mt) ∧ OffOKA (bootSA kb ib nc now mt) ∧ + OffReadyA (bootSA kb ib nc now mt) ∧ ActIdleA (bootSA kb ib nc now mt) := by + have ht := bootKA_tcb (kb := kb) (now := now) (mt := mt) hL hL.nc0 + have hsc := bootKA_sc0 (kb := kb) (now := now) (mt := mt) hL + have hb : (idleTcbA (scRenA (bootMA ib nc) (idleSA ib 0))).tcbSchedContext = some idleScPtr := by + rw [bootRen0 hL]; rfl + have h := offAtBootA (σ := bootSA kb ib nc now mt) hsc rfl (Nat.le_refl _) + refine ⟨idleCurAtBootA rfl rfl ht hb rfl ⟨_, _, hsc⟩ rfl, h.1, h.2, ?_⟩ + refine ⟨fun t e => ?_, ⟨_, ht, hb, rfl, by simp [bootSA, initASt]⟩⟩ + change SchedulerAction.resumeCurThread = .switchThread t at e + cases e + +theorem w1_curTimingA {kb ib now mt sl : U64} {nc : Nat} (hL : BootLayA ib nc) (P : BootLay1 kb ib nc) : + CurInvA (w1SA kb ib nc now mt sl) ∧ OffOKA (w1SA kb ib nc now mt sl) ∧ + OffReadyA (w1SA kb ib nc now mt sl) ∧ ActIdleA (w1SA kb ib nc now mt sl) := by + have ht := w1KA_idle (now := now) (mt := mt) (sl := sl) hL P hL.nc0 + have hsc := w1KA_sc0 (now := now) (mt := mt) (sl := sl) (kb := kb) hL + have hb : (idleTcbA (scRenA (bootMA ib nc) (idleSA ib 0))).tcbSchedContext = some idleScPtr := by + rw [bootRen0 hL]; rfl + have h := offAtBootA (σ := w1SA kb ib nc now mt sl) hsc rfl (Nat.le_refl _) + refine ⟨idleCurAtBootA rfl rfl ht hb rfl ⟨_, _, hsc⟩ rfl, h.1, h.2, ?_⟩ + refine ⟨fun t e => ?_, ⟨_, ht, hb, rfl, by simp [w1SA, bootSA, initASt]⟩⟩ + change SchedulerAction.switchThread (specPtr (bwv RTb)) = .switchThread t at e + cases e + have hkt := w1KA_rt (kb := kb) (ib := ib) (nc := nc) (now := now) (mt := mt) (sl := sl) + have hks := w1KA_sc (kb := kb) (ib := ib) (nc := nc) (now := now) (mt := mt) (sl := sl) + classical + unfold schedulable + apply decide_eq_true + have ht0 : tcbsOf (w1SA kb ib nc now mt sl) (specPtr (bwv RTb)) = some rootTcbA := by + show tcbsOfKh (w1KA kb ib nc now mt sl) (specPtr (bwv RTb)) = _ + simp [tcbsOfKh, optMap, tcbOf, hkt] + have hs0 : scsOf (w1SA kb ib nc now mt sl) (specPtr (bwv SCb)) = some (rootScA now sl) := by + show scsOfKh (w1KA kb ib nc now mt sl) (specPtr (bwv SCb)) = _ + simp [scsOfKh, optMap, scOf, hks] + have hr0 : (w1SA kb ib nc now mt sl).releaseQueue = [] := rfl + simp [optPred, optMapSome, optMap, ht0, hs0, rootTcbA, rootScA, scActive, activeSc, hr0, runnable] + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScCfgFinishA.lean b/proofs/Fl4ProofsA64/P3/ScCfgFinishA.lean new file mode 100644 index 000000000..48464fdc8 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScCfgFinishA.lean @@ -0,0 +1,153 @@ +import Fl4ProofsA64.P3.ScCfgEntryA +import Fl4ProofsA64.P3.ScCfgResumeA +import Fl4ProofsA64.P3.ScYieldFrameA + +/-! The current-SC, offset, and action obligations at the end of ConfigureFlags. -/ + +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3.Sched + +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +set_option maxRecDepth 2048 in +theorem cfgKeepStatesA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s s' : S} {σ τ : State Unit} + (H : SchedSRN st Z m κ s σ) (T : StEqA Z.T (L.mem s) σ) + (W : MFrA (st := st) Z m s s' σ τ) + (htcb : ∀ u x, τ.kheap u = some (.tcb x) → σ.kheap u = some (.tcb x)) : + StEqA Z.T (L.mem s') τ := by + intro t ht x hk + rw [stA_eq] + have ha : Aligned (core.num.U64.wrapping_add t 3328#u64) := + field_aligned (H.core.tcbs t ht) (by decide : (3328#u64 : U64).val < 4096) + (by decide : (3328#u64 : U64).val % 8 = 0) + rw [W.wr.mem_eq ha (yieldTcbNotMWrA H ht (Or.inl rfl))] + simpa only [stA_eq] using T t ht x (htcb _ _ hk) + +theorem cfgScFrameA {Z : XSetsA} {m : McState} {s s' : S} {σ : State Unit} {sc : U64} + {x y : SchedContext} {n : Nat} (hc : sc ∈ Z.C) + (hk : σ.kheap (scNameA m sc) = some (.schedContext x n)) (w : WritesIn st s s' (ScObjA sc n)) : + MFrA (st := st) Z m s s' σ (updKSc σ (scNameA m sc) (.schedContext y n)) := + cfgPrepFrameA hc hk rfl (w.mono fun _ b => Or.inl ⟨sc, hc, x, n, hk, b⟩) + +theorem cfgSchedulableA {σ : State Unit} {u p : ObjRef} {t : Tcb} {x : SchedContext} {n : Nat} + (ht : σ.kheap u = some (.tcb t)) (hb : t.tcbSchedContext = some p) + (hs : σ.kheap p = some (.schedContext x n)) (hr : runnable t.tcbState = true) + (ha : scActive x = true) (hQ : u ∉ σ.releaseQueue) : schedulable u σ = true := by + classical + unfold schedulable + apply decide_eq_true + simp [tcbsOfKh, scsOfKh, optPred, optMap, optMapSome, tcbOf, scOf, ht, hb, hs, hr, ha, hQ] + +theorem cfgTimingAfterA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ τ : State Unit} + (H : SchedSRA st Z m κ s σ) + (I : CurInvA σ) (O : OffOKA σ) (R : OffReadyA σ) (Ac : ActIdleA σ) + {p : ObjRef} {x y : SchedContext} {n : Nat} + (hk : σ.kheap p = some (.schedContext x n)) (Hk : τ.kheap = (updKSc σ p (.schedContext y n)).kheap) + (hBind : ∀ u t, x.scTcb = some u → σ.kheap u = some (.tcb t) → t.tcbSchedContext = some p) + (hni : p ≠ idleScPtr) (hy : scActive y = true) + (ht : τ.curThread = σ.curThread) (hi : τ.idleThread = σ.idleThread) (hc : τ.curSc = σ.curSc) + (hTime : τ.curTime = σ.curTime) + (hUsed : τ.consumedTime = if σ.curSc = p then 0 else σ.consumedTime) + (hCurrent : σ.curSc = p → refillReady σ.curTime (refillHd y) = true) + (hReady : refillReady σ.curTime (refillHd x) = true → refillReady σ.curTime (refillHd y) = true) + (hRel : ∀ u ∈ τ.releaseQueue, u ∈ σ.releaseQueue ∨ x.scTcb = some u) + (hLate : ∀ u, x.scTcb = some u → u ∈ τ.releaseQueue → refillReady σ.curTime (refillHd y) ≠ true) + (hAct : ∀ u, τ.schedulerAction = .switchThread u → + σ.schedulerAction = .switchThread u ∨ ∃ t, σ.kheap u = some (.tcb t) ∧ + t.tcbSchedContext = some p ∧ runnable t.tcbState = true ∧ u ∉ τ.releaseQueue) : + CurInvA τ ∧ OffOKA τ ∧ OffReadyA τ ∧ ActIdleA τ := by + have keep (u : ObjRef) (t : Tcb) : τ.kheap u = some (.tcb t) ↔ σ.kheap u = some (.tcb t) := by + rw [Hk] + exact cfgTcbKeepA hk u t + have atp : τ.kheap p = some (.schedContext y n) := Hk ▸ updKSc_same _ _ _ + have far (u : ObjRef) (h : u ≠ p) : τ.kheap u = σ.kheap u := by + rw [Hk, updKSc_kheap, Function.update_of_ne h] + obtain ⟨t, hkt, htc, htipc⟩ := I.tcb + have noCurrent : τ.curThread ∉ τ.releaseQueue := by + rw [ht] + intro h + rcases hRel _ h with h' | h' + · exact I.notRel h' + · have e := Option.some.inj (htc.symm.trans (hBind _ _ h' hkt)) + exact hLate _ h' h (hCurrent e) + have scCurrent : ∃ z j, τ.kheap τ.curSc = some (.schedContext z j) := by + rw [hc] + by_cases e : σ.curSc = p + · exact ⟨y, n, e.symm ▸ atp⟩ + · rw [far _ e] + exact I.sc + have activeCurrent : τ.curSc ≠ idleScPtr → + ∀ z j, τ.kheap τ.curSc = some (.schedContext z j) → scActive z = true := by + rw [hc] + intro ne z j hz + by_cases e : σ.curSc = p + · rw [e, atp] at hz + cases hz + exact hy + · rw [far _ e] at hz + exact I.active ne _ _ hz + have offOK : OffOKA τ := by + by_cases e : σ.curSc = p + · intro h + rw [hUsed, if_pos e] at h + exact (BitVec.lt_irrefl _ h).elim + · exact O.congr hc hTime (hUsed.trans (if_neg e)) (far _ e) + have offReady : OffReadyA τ := by + intro z j hz + rw [hc] at hz + rw [hTime, hUsed] + by_cases e : σ.curSc = p + · rw [e, atp] at hz + cases hz + rw [if_pos e] + have h := hCurrent e + change decide ((refillHd y).rTime + (0 : Ticks) ≤ σ.curTime) = true at h + have hzero : (refillHd y).rTime + (0 : Ticks) = (refillHd y).rTime := by bv_omega + rw [hzero] at h + have hLe := of_decide_eq_true h + rw [BitVec.le_def] at hLe + simpa using hLe + · rw [if_neg e] + rw [far _ e] at hz + exact R _ _ hz + refine ⟨⟨⟨t, by rw [ht]; exact (keep _ _).2 hkt, by rw [hc]; exact htc, htipc⟩, + scCurrent, activeCurrent, noCurrent⟩, offOK, offReady, ?_, ?_⟩ + · intro u hu + rcases hAct u hu with ho | ⟨z, hz, hzb, hr, hQ⟩ + · have hs := Ac.1 u ho + obtain ⟨z, q, hz, hzb, -, -⟩ := H.actReady u ho hs + obtain ⟨v, j, hv, hvo⟩ := H.inv.back _ _ _ hz hzb + have parts := schedulable_partsA hs + have hr := parts.1 _ hz + have hvA : scActive v = true := by + unfold schedulable at hs + simp only [decide_eq_true_eq] at hs + simpa [tcbsOfKh, scsOfKh, optPred, optMap, tcbOf, scOf, hz, hzb, hv] using hs.2.1 + have hQ : u ∉ τ.releaseQueue := by + intro hQ + rcases hRel u hQ with h | h + · exact parts.2 h + · have e := Option.some.inj (hzb.symm.trans (hBind _ _ h hz)) + subst q + rw [hk] at hv + cases hv + exact hLate _ h hQ (hReady (H.rd _ _ hz (Or.inl hr) _ _ _ _ hz hzb hk hvA parts.2).1) + by_cases e : q = p + · exact cfgSchedulableA ((keep _ _).2 hz) (hzb.trans (congrArg some e)) atp hr hy hQ + · exact cfgSchedulableA ((keep _ _).2 hz) hzb ((far _ e).trans hv) hr hvA hQ + · exact cfgSchedulableA ((keep _ _).2 hz) hzb atp hr hy hQ + · obtain ⟨z, hz, hzb, hiS, hiR⟩ := Ac.2 + rw [hi] + refine ⟨z, (keep _ _).2 hz, hzb, hiS, fun h => ?_⟩ + rcases hRel _ h with h' | h' + · exact hiR h' + · exact hni (Option.some.inj ((hBind _ _ h' hz).symm.trans hzb)) + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScCfgHeaderA.lean b/proofs/Fl4ProofsA64/P3/ScCfgHeaderA.lean new file mode 100644 index 000000000..c5ec41c3a --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScCfgHeaderA.lean @@ -0,0 +1,142 @@ +import Fl4ProofsA64.P3.ScCfgStateA +import Fl4ProofsA64.P3.ScConsA +import Fl4ProofsA64.P1.QueueScBindV + +/-! ConfigureFlags changes the concrete SC core and the abstract badge +and sporadic flag. All three writes frame the refill ring. -/ + +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +private theorem cfgCoreWordA (sc : U64) : scCoreA sc = scWA sc 16#u64 := by unfold scCoreA scWA layout.sc.CORE; rfl + +private theorem cfgHeaderA {s : S} {sc : U64} {i : Prop} {x : SchedContext} {n : Nat} + (R : ScRelWA st i s sc x n) (ha : scActive x = true) + (k v : U64) (hk : k = 16#u64 ∨ k = 48#u64 ∨ k = 64#u64) : + let s' := L.wr s (scWA sc k) v + let y := { x with + scBadge := (if k = 48#u64 then v.bv else x.scBadge) + scSporadic := if k = 64#u64 then decide (v ≠ 0#u64) else x.scSporadic } + ScRelWA st i s' sc y n ∧ WritesIn st s s' (ScObjA sc n) ∧ + L.mem s' (scCoreA sc) = (if k = 16#u64 then v else L.mem s (scCoreA sc)) := by + let s' := L.wr s (scWA sc k) v + have R1 := R.strong ha + obtain ⟨al, h2, hf⟩ := R.bounds + have hv : k.val = 16 ∨ k.val = 48 ∨ k.val = 64 := by rcases hk with rfl | rfl | rfl <;> simp + have e := scWA_val (sc := sc) (K := k) (by omega) + have h := R.hdr (K := k) (by omega) (by rcases hk with rfl | rfl | rfl <;> simp) + have wm : WritesIn st s s' (ScObjA sc n) := + (writesIn_wr h.1).mono fun _ hb => hb ▸ R.in_obj h.2 + have eqs (o : U64) (ho : o.val < 96) (hal : o.val % 8 = 0) : + L.mem s' (scWA sc o) = if k = o then v else L.mem s (scWA sc o) := by + by_cases eq : k = o + · subst o; rw [if_pos rfl]; exact L.mem_wr_same h.1 + · rw [if_neg eq] + apply L.mem_wr_ne h.1 (R.hdr ho hal).1 + rw [e, scWA_val (by omega)] + exact fun ee => eq (UScalar.eq_of_val_eq (by omega)) + have hb : + (if k = 48#u64 then v.bv else x.scBadge) = (L.mem s' (scWA sc 48#u64)).bv := by + rw [eqs _ (by simp) (by simp)] + split <;> first | rfl | exact R.badge + have hs : + (if k = 64#u64 then decide (v ≠ 0#u64) else x.scSporadic) = + decide (L.mem s' (scWA sc 64#u64) ≠ 0#u64) := by + rw [eqs _ (by simp) (by simp)] + split <;> first | rfl | exact R.sporadic + have q := R1.cfgWords (s' := s') (fun a ha' _ h16 h48 h64 => by + apply L.mem_wr_ne h.1 ha' + rw [e] + rcases hv with h' | h' | h' <;> rw [h'] <;> omega) _ _ hb hs + have hcore : L.mem s' (scCoreA sc) = if k = 16#u64 then v else L.mem s (scCoreA sc) := by + rw [cfgCoreWordA] + exact eqs _ (by simp) (by simp) + exact ⟨q.toW, wm, hcore⟩ + +/-- Writing the target's core changes the core map at that SC only. -/ +theorem cfg_set_coreA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + {u : ObjRef} (H : SchedSRB st Z m κ s σ u) {sc : U64} (hc : sc ∈ Z.C) + {x : SchedContext} {n : Nat} (hk : σ.kheap (scNameA m sc) = some (.schedContext x n)) + (ha : scActive x = true) (hni : NotIdleScA m (scNameA m sc)) + (ho : x.scTcb = none ∨ x.scTcb = some u) (hnr : u ∉ σ.releaseQueue) (cpu : U64) : + obj.Sc.set_cpu st sc s cpu ⦃ s' => + SchedSRB st Z m (Function.update κ sc cpu) s' σ u ∧ WritesIn st s s' (ScObjA sc n) ⦄ := by + obtain ⟨y, j, hy, R⟩ := H.scr.sc sc hc + rw [hk] at hy; cases hy + refine WP.spec_mono (obj.Sc.set_cpu.wr s sc cpu) ?_ + rintro s' rfl + obtain ⟨R', w, hco⟩ := cfgHeaderA R ha 16#u64 cpu (Or.inl rfl) + have e : { x with + scBadge := (if (16#u64 : U64) = 48#u64 then cpu.bv else x.scBadge) + scSporadic := if (16#u64 : U64) = 64#u64 then decide (cpu ≠ 0#u64) else x.scSporadic } = x := by + rw [if_neg (by decide), if_neg (by decide)] + try (cases x; rfl) + rw [e] at R' + have H' := H.scReconfCoreA hc hk w R' rfl ho hnr (fun e => H.inv.refills _ _ _ hk ha e) + (fun e => (H.inv.rr _ _ _ hk ha e).2 hni) cpu (by simpa only [if_true] using hco) + rw [updKSc_selfA hk] at H' + rw [← cfgCoreWordA] at H' w + exact ⟨H', w⟩ + +theorem cfgSporadicBitA (flags : U64) : + (flags &&& fl4_abi.consts.sc_flag.SPORADIC != 0#u64) = testBit flags.bv scSporadicFlag := by + have h1 : (flags.bv &&& 1#64).toNat = flags.bv.toNat % 2 := by + rw [BitVec.toNat_and]; simp [Nat.and_one_is_mod] + have h2 : ((flags.bv &&& 1#64) ≠ 0#64) ↔ flags.bv.toNat % 2 = 1 := by + rw [ne_eq, BitVec.toNat_eq, h1]; simp + have h3 : (flags &&& fl4_abi.consts.sc_flag.SPORADIC != 0#u64) = decide ((flags.bv &&& 1#64) ≠ 0#64) := by + unfold fl4_abi.consts.sc_flag.SPORADIC + rw [bne, decide_not]; congr 1 + rw [h3, scSporadicFlag, show testBit flags.bv 0 = flags.bv.toNat.testBit 0 from rfl, Nat.testBit_zero] + simp only [h2] + +theorem cfg_badge_flagsA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRA st Z m κ s σ) {sc : U64} (hc : sc ∈ Z.C) + {x : SchedContext} {n : Nat} (hk : σ.kheap (scNameA m sc) = some (.schedContext x n)) + (ha : scActive x = true) (badge flags : U64) : + (do + let s1 ← obj.Sc.set_badge st sc s badge + if flags &&& fl4_abi.consts.sc_flag.SPORADIC != 0#u64 then obj.Sc.set_sporadic st sc s1 1#u64 + else obj.Sc.set_sporadic st sc s1 0#u64) ⦃ s' => + let y := { x with scBadge := badge.bv, scSporadic := testBit flags.bv scSporadicFlag } + SchedSRA st Z m κ s' (updKSc σ (scNameA m sc) (.schedContext y n)) ∧ WritesIn st s s' (ScObjA sc n) ⦄ := by + obtain ⟨y, j, hy, R⟩ := H.scr.sc sc hc + rw [hk] at hy; cases hy + refine WP.spec_bind (obj.Sc.set_badge.wr s sc badge) ?_ + rintro s1 rfl + have eb : core.num.U64.wrapping_add sc layout.sc.BADGE = scWA sc 48#u64 := by unfold layout.sc.BADGE scWA; rfl + have ep : core.num.U64.wrapping_add sc layout.sc.SPORADIC = scWA sc 64#u64 := by unfold layout.sc.SPORADIC scWA; rfl + rw [eb] + obtain ⟨R1, w1, c1⟩ := cfgHeaderA R ha 48#u64 badge (Or.inr (Or.inl rfl)) + rw [if_pos rfl, if_neg (by decide : (48#u64 : U64) ≠ 64#u64)] at R1 + rw [if_neg (by decide : (48#u64 : U64) ≠ 16#u64)] at c1 + let y1 := { x with scBadge := badge.bv } + have ha1 : scActive y1 = true := ha + have step (v : U64) (hf : decide (v ≠ 0#u64) = testBit flags.bv scSporadicFlag) : + obj.Sc.set_sporadic st sc (L.wr s (scWA sc 48#u64) badge) v ⦃ s' => + SchedSRA st Z m κ s' (updKSc σ (scNameA m sc) + (.schedContext { x with scBadge := badge.bv, scSporadic := testBit flags.bv scSporadicFlag } n)) ∧ + WritesIn st s s' (ScObjA sc n) ⦄ := by + refine WP.spec_mono (obj.Sc.set_sporadic.wr _ _ _) ?_ + rintro s' rfl + obtain ⟨R2, w2, c2⟩ := cfgHeaderA (x := y1) R1 ha1 64#u64 v (Or.inr (Or.inr rfl)) + rw [if_neg (by decide : (64#u64 : U64) ≠ 48#u64), if_pos rfl, hf] at R2 + rw [if_neg (by decide : (64#u64 : U64) ≠ 16#u64)] at c2 + rw [ep] + exact ⟨H.scObjA hc hk (w1.trans1 w2) R2 rfl rfl rfl rfl (c2.trans c1), w1.trans1 w2⟩ + rw [cfgSporadicBitA] + cases h : testBit flags.bv scSporadicFlag + · simp only [Bool.false_eq_true, ↓reduceIte] + simpa only [h] using step 0#u64 (by rw [h]; rfl) + · simp only [↓reduceIte] + simpa only [h] using step 1#u64 (by rw [h]; rfl) + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScCfgPlatA.lean b/proofs/Fl4ProofsA64/P3/ScCfgPlatA.lean new file mode 100644 index 000000000..3e2e9e015 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScCfgPlatA.lean @@ -0,0 +1,46 @@ +import Fl4ProofsA64.P0.Totality +import Fl4SpecA64.Ext.SchedControl + +/-! Platform agreement for AArch64 scheduling-context configuration. + +[P] `ScMaxPeriodA ar a` relates the maximum returned by the architecture +to `Fl4SpecA64.fl4MaxPeriodUs`. The `ScCfgTimeAgreeA` [P] fields also +relate the architecture's conversion and minimum to the FL4 spec and +fix the kernel WCET used in microsecond error payloads. +-/ + +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3.Sched + +/-- [P] The architecture reports the FL4 spec's maximum period in microseconds. -/ +abbrev ScMaxPeriodA {A : Type} (ar : machine.Arch A) (a : A) : Prop := + ∃ v : U64, sporadic.max_period_us ar a = ok v ∧ v.bv = Fl4SpecA64.fl4MaxPeriodUs + +/-- [P] Platform agreement for ConfigureFlags' argument decoder. -/ +structure ScCfgTimeAgreeA {A : Type} (ar : machine.Arch A) (a : A) : Prop where + maxPeriod : ScMaxPeriodA ar a + usToTicks : ∀ us, ar.us_to_ticks a us ⦃ v => v.bv = Fl4SpecA64.fl4UsToTicks us.bv ⦄ + minBudget : sporadic.min_budget ar a ⦃ v => v.bv = Fl4SpecA64.fl4MinBudget ⦄ + wcetUs : Fl4SpecA64.kernelWCETUs = 10 + +/-- The release limit follows from the platform's period and conversion +agreements. -/ +theorem ScCfgTimeAgreeA.maxRelease {A : Type} {ar : machine.Arch A} {a : A} + (h : ScCfgTimeAgreeA ar a) : + sporadic.max_release_time ar a ⦃ v => v.bv = Fl4SpecA64.fl4MaxReleaseTime ⦄ := by + obtain ⟨m, hm, hv⟩ := h.maxPeriod + unfold sporadic.max_release_time + rw [hm, bind_ok] + apply WP.spec_bind (h.usToTicks m) + intro r hr + simp only [lift, bind_ok] + apply (WP.spec_ok _).2 + change (core.num.U64.wrapping_sub core.num.U64.MAX (core.num.U64.wrapping_mul r 5#u64)).bv = + Fl4SpecA64.fl4MaxReleaseTime + rw [core.num.U64.wrapping_sub_bv_eq, core.num.U64.wrapping_mul_bv_eq, hr, hv] + unfold Fl4SpecA64.fl4MaxReleaseTime + rw [BitVec.mul_comm (Fl4SpecA64.fl4UsToTicks _)] + congr 1 + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScCfgPrepA.lean b/proofs/Fl4ProofsA64/P3/ScCfgPrepA.lean new file mode 100644 index 000000000..d74b8e05c --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScCfgPrepA.lean @@ -0,0 +1,160 @@ +import Fl4ProofsA64.P3.ScCfgChargeA +import Fl4ProofsA64.P3.ScCfgPlatA +import Fl4ProofsA64.P3.ScDeqA +import Fl4ProofsA64.P3.ScRelRmA + +/-! The local ConfigureFlags prelude removes the owner from both queues, +then commits its pending consumption when reconfiguring the current SC. -/ + +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3.Sched +open Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4SpecA64 Fl4Spec + +noncomputable section + +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +/-- The prelude on the bound-owner arm. -/ +def cfgPrepareA (t p : ObjRef) : SMonad Unit Unit := do + tcbReleaseRemove t + tcbSchedAction tcbSchedDequeue t + let cur ← NonDet.gets fun s : State Unit => s.curSc + NonDet.when (cur = p) fl4CommitTime + +theorem SchedSRN.currentScA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRN st Z m κ s σ) : σ.curSc = scNameA m (L.cscW s) := by + rw [← H.sch.csc, H.sch.core] + exact H.sch.curScOfW _ H.sch.cpuCs + +theorem cfgPrepFrameA {Z : XSetsA} {m : McState} {s s' : S} {σ : State Unit} + {sc : U64} (hc : sc ∈ Z.C) {x y : SchedContext} {n : Nat} + (hk : σ.kheap (scNameA m sc) = some (.schedContext x n)) {σ' : State Unit} + (h : σ'.kheap = (updKSc σ (scNameA m sc) (.schedContext y n)).kheap) + (w : WritesIn st s s' (MWrA (st := st) Z m σ s)) : + MFrA (st := st) Z m s s' σ σ' := by + refine ⟨w, SKeepA.of_obj ?_, SSzA.of_upd hk h⟩ + intro q o hq ho + have ne : q ≠ scNameA m sc := by + intro e + rw [e, hk] at hq + exact ho x n (Option.some.inj hq.symm) + rw [h, updKSc_kheap, Function.update_of_ne ne] + exact hq + +set_option maxHeartbeats 900000 in +theorem cfg_prepare_boundA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRA st Z m κ s σ) (a : A) {sc t : U64} (hc : sc ∈ Z.C) (ht : t ∈ Z.T) + (hon : L.mem s (affA t) = L.cpu s) + (hstall : smp.remote_tcb_stall st ar s a t = ok (s, a)) + {x : SchedContext} {n : Nat} (hk : σ.kheap (scNameA m sc) = some (.schedContext x n)) + (hown : x.scTcb = some (specPtr t)) (hni : NotIdleScA m (scNameA m sc)) + (I : CurInvA σ) (O : OffOKA σ) (R : OffReadyA σ) + (hagree : fl4MinBudget = MIN_BUDGET) (hT : ScCfgTimeAgreeA ar a) : + (do + let (s2, a2) ← smp.remote_tcb_stall st ar s a t + let s3 ← sched.release_remove st s2 t + let s4 ← sched.dequeue st s3 t + let s5 ← sched.cur_sc st s4 + let b ← obj.Sc.Insts.CoreCmpPartialEqSc.eq s5 sc + let s6 ← if b then sched.commit_time st ar s4 a2 else ok s4 + ok (s6, a2)) ⦃ r => r.2 = a ∧ ∃ σ' Q' R' y, + cfgPrepareA (specPtr t) (scNameA m sc) σ = ({((), σ')}, False) ∧ + SchedSRB st { Z with Q := Q', R := R' } m κ r.1 σ' (specPtr t) ∧ + σ'.kheap = (updKSc σ (scNameA m sc) (.schedContext y n)).kheap ∧ + y.scTcb = x.scTcb ∧ y.scYieldFrom = x.scYieldFrom ∧ + σ'.curSc = σ.curSc ∧ σ'.curThread = σ.curThread ∧ σ'.idleThread = σ.idleThread ∧ + σ'.curTime = σ.curTime ∧ σ'.schedulerAction = σ.schedulerAction ∧ + σ'.consumedTime = (if σ.curSc = scNameA m sc then 0 else σ.consumedTime) ∧ + (σ.curSc = scNameA m sc → refillReady σ.curTime (refillHd y) = true) ∧ + (refillReady σ.curTime (refillHd x) = true → refillReady σ.curTime (refillHd y) = true) ∧ + (∀ d p, specPtr t ∉ σ'.readyQueues d p) ∧ specPtr t ∉ σ'.releaseQueue ∧ + (∀ u ∈ σ'.releaseQueue, u ∈ σ.releaseQueue) ∧ + MFrA (st := st) Z m s r.1 σ σ' ⦄ := by + rw [hstall] + simp only [bind_ok, uncurry_apply_pair] + refine WP.spec_bind (release_remove_refines_localA H ht hon) ?_ + rintro s1 ⟨R', -, H1, W1⟩ + let σ1 := releaseRemoveStA σ (specPtr t) + have hCpu1 : L.cpu s1 = L.cpu s := W1.wr.2.1 + have hAff1 : L.mem s1 (affA t) = L.cpu s1 := by + rw [hCpu1, ← hon] + exact UScalar.eq_of_val_eq (Option.some.inj ((H1.sch.aff t ht).symm.trans (H.sch.aff t ht))) + refine WP.spec_bind (dequeue_refines_nodeA H1.toSchedSRN ht hAff1) ?_ + rintro s2 ⟨σ2, hd, hk2, hr2, hf2, ha2, -, H2, W2⟩ + have hdq : ∀ d p, specPtr t ∉ σ2.readyQueues d p := H1.toSchedSRN.ready_after_dequeueA ht H2 + have hrel : specPtr t ∉ σ2.releaseQueue := by rw [hr2]; exact releaseRemoveStA_notMem σ _ + have hrelSub : ∀ u ∈ σ2.releaseQueue, u ∈ σ.releaseQueue := fun u hu => by + rw [hr2] at hu + exact (List.mem_filter.1 hu).1 + have H2' : SchedSRB st _ m κ s2 σ2 (specPtr t) := ⟨H2, fun u z hu hz hin => by + rw [hk2] at hz + have e := H1.rdEx u z hu hz hin + intro x' p y' n' hx' hxp hy' hay hnr + rw [hk2] at hx' hy' + rw [hr2] at hnr + unfold releasedSc at e ⊢ + rw [hf2.curTime] + exact e x' p y' n' hx' hxp hy' hay hnr⟩ + have hK : σ2.kheap = σ.kheap := hk2 + have hc2 : σ2.curSc = σ.curSc := hf2.curSc + have ht2 : σ2.curThread = σ.curThread := hf2.curThread + have hi2 : σ2.idleThread = σ.idleThread := hf2.idleThread + have hm2 : σ2.curTime = σ.curTime := hf2.curTime + have hn2 : σ2.consumedTime = σ.consumedTime := hf2.consumedTime + have ha2' : σ2.schedulerAction = σ.schedulerAction := ha2 + have noIPC : OffOKA σ2 := O.congr hc2 hm2 hn2 (by rw [hK]) + have ord : OffReadyA σ2 := by intro z k he; rw [hc2, hK] at he; rw [hm2, hn2]; exact R z k he + have hUpdate : ∀ (σ' : State Unit), (NonDet.when (σ2.curSc = scNameA m sc) fl4CommitTime) σ2 = + ({((), σ')}, False) → cfgPrepareA (specPtr t) (scNameA m sc) σ = ({((), σ')}, False) := by + intro σ' h + unfold cfgPrepareA + rw [nd_bind_oneSc (tcbReleaseRemove_eqA σ _), nd_bind_oneSc hd, nd_gets_bindSc] + exact h + have hW := W1.trans W2 rfl rfl + have hread : sched.cur_sc st s2 = ok (L.cscW s2) := eq_of_spec_fd (sched.cur_sc.spec s2) + rw [hread, bind_ok] + have eSc : obj.Sc.Insts.CoreCmpPartialEqSc.eq (L.cscW s2) sc = ok (decide (L.cscW s2 = sc)) := by + unfold obj.Sc.Insts.CoreCmpPartialEqSc.eq + simp + rw [eSc, bind_ok] + have eq : L.cscW s2 = sc ↔ σ.curSc = scNameA m sc := + ⟨fun he => by rw [← hc2, H2.currentScA, he], + fun he => H2.scp.inj _ H2.cscC _ hc (H2.currentScA.symm.trans (hc2.trans he))⟩ + by_cases he : L.cscW s2 = sc + · have hcur := eq.1 he + have hk' : σ2.kheap (scNameA m sc) = some (.schedContext x n) := hK ▸ hk + have hct' : σ2.curSc = scNameA m sc := hc2.trans hcur + have ha : scActive x = true := I.active (by rw [hcur]; exact hni.1) x n (by rw [hcur]; exact hk) + rw [decide_eq_true he] + simp only [↓reduceIte] + refine WP.spec_bind (cfg_commit_timeA H2' a hc he hct' hk' ha hni (Or.inr hown) hrel noIPC ord hagree hT.maxRelease hT.minBudget) ?_ + rintro s3 ⟨y, en, H3, fields, ready, W3⟩ + let σ3 := { updKSc σ2 (scNameA m sc) (.schedContext y n) with consumedTime := 0 } + have wf : MFrA (st := st) _ m s2 s3 σ2 σ3 := + cfgPrepFrameA hc hk' rfl (W3.mono fun b hb => hb.elim (fun h => Or.inl ⟨sc, hc, x, n, hk', h⟩) + (fun h => Or.inr (Or.inl (h ▸ H2.node_sks H2.sch.cpuCs (by unfold layout.node.CONSUMED; decide))))) + rw [WP.spec_ok] + refine ⟨rfl, σ3, _, R', y, hUpdate σ3 (by rw [NonDet.when, if_pos hct']; exact en), H3, ?_, + ?_, ?_, hc2, ht2, hi2, hm2, ha2', ?_, fun _ => ?_, fun _ => ?_, hdq, hrel, hrelSub, hW.trans wf rfl rfl⟩ + · show Function.update σ2.kheap _ _ = Function.update σ.kheap _ _ + rw [hK] + · have e := congrArg SchedContext.scTcb fields + exact e + · have e := congrArg SchedContext.scYieldFrom fields + exact e + · rw [if_pos hcur] + · simpa only [hm2] using ready + · simpa only [hm2] using ready + · have hcur : σ.curSc ≠ scNameA m sc := fun h => he (eq.2 h) + rw [decide_eq_false he] + simp only [Bool.false_eq_true, ↓reduceIte, bind_ok, WP.spec_ok] + refine ⟨True.intro, σ2, _, R', x, hUpdate σ2 (by rw [NonDet.when, if_neg (fun e => hcur (hc2.symm.trans e))]; rfl), + H2', ?_, rfl, rfl, hc2, ht2, hi2, hm2, ha2', ?_, (fun e => (hcur e).elim), + id, hdq, hrel, hrelSub, hW⟩ + · rw [updKSc_selfA hk]; exact hK + · rw [if_neg hcur]; exact hn2 + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScCfgRefillA.lean b/proofs/Fl4ProofsA64/P3/ScCfgRefillA.lean new file mode 100644 index 000000000..c383499fb --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScCfgRefillA.lean @@ -0,0 +1,493 @@ +import Fl4ProofsA64.P3.ScCheckA +import Fl4ProofsA64.P3.ScSchedA + +/-! The exact abstract refill updates used by local ConfigureFlags. +The initial refill starts at the view's current time. +-/ + +namespace Fl4ProofsA64.P3.Sched + +open Fl4SpecA64 Fl4Spec + +noncomputable section + +/-- The refills `refill_new` leaves: the budget now, and an empty tail for a +round-robin SC. -/ +def cfgNewRefillsA (now b per : Ticks) : List Refill := + if per = 0 then [{ rTime := now, rAmount := b }, { rTime := now, rAmount := 0 }] + else [{ rTime := now, rAmount := b }] + +/-- The SC after `refill_new`. -/ +def cfgNewScA (x : SchedContext) (now : Ticks) (mr : Nat) (b per : Ticks) : SchedContext := + { x with scPeriod := per, scBudget := b, scRefillMax := mr, scRefills := cfgNewRefillsA now b per } + +theorem cfgRefillNew_eqA {σ : State Unit} {p : ObjRef} {x : SchedContext} {n : Nat} + (hk : σ.kheap p = some (.schedContext x n)) (mr : Nat) (b per : Ticks) : + refillNew (Z := Unit) p mr b per σ = ({((), updKSc σ p (.schedContext (cfgNewScA x σ.curTime mr b per) n))}, False) := by + have k : ∀ (z : SchedContext) (σ' : State Unit), (updKSc σ' p (.schedContext z n)).kheap p = some (.schedContext z n) := + fun z σ' => updKSc_same σ' p (.schedContext z n) + unfold refillNew + rw [nd_gets_bindSc, nd_bind_oneSc (updateSchedContext_eqA hk _), nd_bind_oneSc (updateSchedContext_eqA (k _ _) _), + nd_bind_oneSc (updateSchedContext_eqA (k _ _) _), nd_pure_bindSc, nd_bind_oneSc (updateSchedContext_eqA (k _ _) _)] + simp only [updKSc_updKSc] + unfold maybeAddEmptyTail + rw [nd_bind_oneSc (isRoundRobin_eqA (k _ _))] + unfold NonDet.when + by_cases hp : per = 0 + · simp only [scRefillsUpdate, scRefillMaxUpdate, scBudgetUpdate, scPeriodUpdate, hp, decide_true, ↓reduceIte] + rw [nd_bind_oneSc (getRefillHead_eqA (k _ _) (by simp)), nd_pure_bindSc, refillAddTail_eqA (k _ _), updKSc_updKSc] + simp [cfgNewScA, cfgNewRefillsA, refillHd, Fl4SpecA64.the, hp] + · simp only [scRefillsUpdate, scRefillMaxUpdate, scBudgetUpdate, scPeriodUpdate, hp, decide_false, + Bool.false_eq_true, ↓reduceIte] + unfold cfgNewScA cfgNewRefillsA + rw [if_neg hp] + rfl + +/-- The head refill after `refill_update`'s move to the current time (spec). -/ +def cfgUpdateHeadA (H : Refill) (now : Ticks) : Refill := if refillReady now H = true then { H with rTime := now } else H + +theorem cfgUpdateHeadA_amount (H : Refill) (now : Ticks) : (cfgUpdateHeadA H now).rAmount = H.rAmount := by + unfold cfgUpdateHeadA; split <;> rfl + +/-- The ring after `refill_update`, from the (moved) head. -/ +def cfgUpdateListA (H : Refill) (np nb : Ticks) : List Refill := + if nb ≤ H.rAmount then + (if np = 0 then [{ H with rAmount := nb }, { rTime := H.rTime, rAmount := 0 }] else [{ H with rAmount := nb }]) + else [H, { rTime := H.rTime + np, rAmount := nb - H.rAmount }] + +/-- `refill_update` on the spec's list. -/ +def cfgUpdatedRefillsA (H : Refill) (now np nb : Ticks) : List Refill := cfgUpdateListA (cfgUpdateHeadA H now) np nb + +/-- The SC with a new ring and configuration. -/ +def cfgUpdateScA (x : SchedContext) (l : List Refill) (np nb : Ticks) (nm : Nat) : SchedContext := + { x with scRefills := l, scRefillMax := nm, scPeriod := np, scBudget := nb } + +/-- The SC after `refill_update`. -/ +def cfgUpdatedScA (x : SchedContext) (now np nb : Ticks) (nm : Nat) : SchedContext := + cfgUpdateScA x (cfgUpdatedRefillsA (refillHd x) now np nb) np nb nm + +theorem cfgRefillUpdate_eqA {σ : State Unit} {p : ObjRef} {x : SchedContext} {n : Nat} + (hk : σ.kheap p = some (.schedContext x n)) (np nb : Ticks) (nm : Nat) : + refillUpdate (Z := Unit) p np nb nm σ = ({((), updKSc σ p (.schedContext (cfgUpdatedScA x σ.curTime np nb nm) n))}, False) := by + have k : ∀ (z : SchedContext) (σ' : State Unit), (updKSc σ' p (.schedContext z n)).kheap p = some (.schedContext z n) := + fun z σ' => updKSc_same σ' p (.schedContext z n) + have hne : ∀ y : SchedContext, ∀ r : Refill, y.scRefills = [r] → y.scRefills ≠ [] := fun y r h => by simp [h] + -- the rest, once the head is known + have tail : ∀ (H : Refill) (σ1 : State Unit) (y : SchedContext), + σ1.kheap p = some (.schedContext y n) → y.scRefills = [H] → y.scPeriod = np → + σ1 = updKSc σ p (.schedContext (cfgUpdateScA x [H] np nb nm) n) → + (do + let head ← getRefillHead (Z := Unit) p + if nb ≤ head.rAmount + then do + updateRefillHd p fun refill => { refill with rAmount := nb } + maybeAddEmptyTail p + else do + let unused ← pure (nb - head.rAmount) + let new ← pure { rTime := head.rTime + np, rAmount := unused : Refill } + refillAddTail p new) σ1 = ({((), updKSc σ p (.schedContext (cfgUpdateScA x (cfgUpdateListA H np nb) np nb nm) n))}, False) := by + intro H σ1 y hy hyr hyp e1 + subst e1 + have ey : y = cfgUpdateScA x [H] np nb nm := by + simp only [updKSc_same, Option.some.injEq, KernelObject.schedContext.injEq] at hy + exact hy.1.symm + subst ey + have hd : refillHd (cfgUpdateScA x [H] np nb nm) = H := rfl + rw [nd_bind_oneSc (getRefillHead_eqA hy (hne _ H hyr)), hd] + unfold cfgUpdateListA + by_cases hb : nb ≤ H.rAmount + · rw [if_pos hb, if_pos hb, nd_bind_oneSc (updateRefillHd_eqA hy _)] + unfold maybeAddEmptyTail + rw [nd_bind_oneSc (isRoundRobin_eqA (k _ _))] + unfold Fl4Spec.NonDet.when + by_cases hp : np = 0 + · rw [if_pos hp] + split + · rw [nd_bind_oneSc (getRefillHead_eqA (k _ _) (by simp [cfgUpdateScA])), nd_pure_bindSc, + refillAddTail_eqA (k _ _), updKSc_updKSc, updKSc_updKSc] + rfl + · rename_i hp0; exact absurd (decide_eq_true hp) hp0 + · rw [if_neg hp] + split + · rename_i hp0; exact absurd (of_decide_eq_true hp0) hp + · rw [updKSc_updKSc]; rfl + · rw [if_neg hb, if_neg hb, nd_pure_bindSc, nd_pure_bindSc, refillAddTail_eqA hy, updKSc_updKSc] + rfl + unfold refillUpdate + rw [nd_bind_oneSc (updateSchedContext_eqA hk _), nd_bind_oneSc (updateSchedContext_eqA (k _ _) _), + nd_bind_oneSc (updateSchedContext_eqA (k _ _) _), nd_bind_oneSc (updateSchedContext_eqA (k _ _) _)] + simp only [updKSc_updKSc] + rw [nd_bind_oneSc (getScRefillReady_eqA (k _ _) (hne _ (refillHd x) rfl))] + unfold cfgUpdatedScA cfgUpdatedRefillsA cfgUpdateHeadA Fl4Spec.NonDet.when + split + · rename_i h0 + have hr : refillReady σ.curTime (refillHd x) = true := h0 + rw [if_pos hr, nd_bind_assocSc, nd_gets_bindSc, nd_bind_oneSc (updateRefillHd_eqA (k _ _) _), updKSc_updKSc] + exact tail _ _ _ (k _ _) rfl rfl rfl + · rename_i h0 + have hr : ¬ refillReady σ.curTime (refillHd x) = true := h0 + rw [if_neg hr, nd_bind_oneSc (show Fl4Spec.NonDet.pure (σ := State Unit) () _ = _ from rfl)] + exact tail _ _ _ (k _ _) rfl rfl rfl + +theorem cfgNewSc_okA (x : SchedContext) (now : Ticks) (mr : Nat) {b : Ticks} (per : Ticks) (hb : MIN_BUDGET ≤ b) : + (per ≠ 0 → RefOKA (cfgNewScA x now mr b per)) ∧ (per = 0 → RRokA (cfgNewScA x now mr b per)) := by + have hpos : 0 < b.toNat := by + have h0 : 0 < MIN_BUDGET := MIN_BUDGET_pos'; rw [BitVec.lt_def] at h0; rw [BitVec.le_def] at hb; omega + have hlt := b.isLt + refine ⟨fun hp => ?_, fun hp => ?_⟩ + · unfold RefOKA cfgNewScA cfgNewRefillsA; rw [if_neg hp] + refine ⟨by simp, fun r hr => ?_, by simpa using hlt, by unfold refillHd; exact hb⟩ + simp only [List.mem_singleton] at hr; subst hr; exact hpos + · unfold RRokA cfgNewScA cfgNewRefillsA; rw [if_pos hp] + refine ⟨by simp, by simpa using hlt, by unfold refillHd; exact hb⟩ + +/-- The refills `refill_update` leaves are valid for a periodic SC whose old +head held at least `MIN_BUDGET`, for a budget of at least `MIN_BUDGET`. -/ +theorem cfgUpdateSc_okA (x : SchedContext) {H : Refill} (now : Ticks) {np nb : Ticks} (nm : Nat) + (hH : MIN_BUDGET ≤ H.rAmount) (hb : MIN_BUDGET ≤ nb) (hnp : np ≠ 0) : + RefOKA (cfgUpdateScA x (cfgUpdatedRefillsA H now np nb) np nb nm) := by + have hm0 : 0 < MIN_BUDGET := MIN_BUDGET_pos'; rw [BitVec.lt_def] at hm0 + rw [BitVec.le_def] at hH hb + unfold RefOKA cfgUpdateScA cfgUpdatedRefillsA cfgUpdateListA + have ha : (cfgUpdateHeadA H now).rAmount = H.rAmount := cfgUpdateHeadA_amount H now + by_cases hc : nb ≤ (cfgUpdateHeadA H now).rAmount + · rw [if_pos hc, if_neg hnp] + refine ⟨by simp, fun r hr => ?_, by simpa using nb.isLt, by unfold refillHd; rw [BitVec.le_def]; exact hb⟩ + simp only [List.mem_singleton] at hr; subst hr; show 0 < nb.toNat; omega + · rw [if_neg hc] + have hc' : H.rAmount.toNat < nb.toNat := by rw [← ha]; rw [BitVec.le_def] at hc; omega + have hsub : (nb - (cfgUpdateHeadA H now).rAmount).toNat = nb.toNat - H.rAmount.toNat := by + rw [BitVec.toNat_sub_of_le (by rw [BitVec.le_def, ha]; omega), ha] + refine ⟨by simp, fun r hr => ?_, ?_, by + unfold refillHd; show MIN_BUDGET ≤ (cfgUpdateHeadA H now).rAmount; rw [BitVec.le_def, ha]; exact hH⟩ + · simp only [List.mem_cons, List.not_mem_nil, or_false] at hr + rcases hr with rfl | rfl + · rw [ha]; omega + · show 0 < (nb - (cfgUpdateHeadA H now).rAmount).toNat; rw [hsub]; omega + · simp only [List.map_cons, List.map_nil, List.sum_cons, List.sum_nil] + show (cfgUpdateHeadA H now).rAmount.toNat + ((nb - (cfgUpdateHeadA H now).rAmount).toNat + 0) < 2 ^ 64 + rw [hsub, ha]; have := nb.isLt; omega + +section Concrete + +open Aeneas Aeneas.Std Result fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- The concrete ring after resetting an SC's refills. -/ +def cfgNewRingA (now batch period : U64) : List sporadic.Refill := + if period = 0#u64 then [⟨now, batch⟩, ⟨now, 0#u64⟩] else [⟨now, batch⟩] + +/-- **FL4's `refill_new`**: writes inside the object, keeps the header words +other than the period, the ring size and the ring indices, and leaves the +ring `cfgNewRingA`. -/ +theorem cfg_refill_new_wordsA {s : S} {sc : U64} {n : Nat} (hP : Placed sc (n + Fl4SpecA64.minSchedContextBits)) (m b p : U64) + (hm2 : 2 ≤ m.val) (hmx : m.val ≤ absMax (n + Fl4SpecA64.minSchedContextBits)) + (hct : Aligned (L.nodeA s layout.node.CUR_TIME) ∧ ¬ ScObjA sc n (L.nodeA s layout.node.CUR_TIME)) : + sporadic.refill_new st s sc m b p ⦃ s' => WritesIn st s s' (ScObjA sc n) ∧ RefillsWF st s' sc (n + Fl4SpecA64.minSchedContextBits) ∧ + L.rperiod s' sc = p ∧ L.rmax s' sc = m ∧ L.refills s' sc = cfgNewRingA (L.curTime s) b p ∧ + ∀ K : U64, K.val < 96 → K.val % 8 = 0 → K.val ≠ 0 → K.val ≠ 72 → K.val ≠ 80 → K.val ≠ 88 → + L.mem s' (scWA sc K) = L.mem s (scWA sc K) ⦄ := by + obtain ⟨h8, hb, hfit⟩ := hP.bounds hm2 hmx + have hpA : (periodA sc).val = sc.val := by + rw [periodA_eqS, scWA_val (by change sc.val + 0 < 2^64; omega)] + simp + have hmA : (maxA sc).val = sc.val + 72 := by + rw [maxA_eqS, scWA_val (by change sc.val + 72 < 2^64; omega)] + rfl + have hhA : (headA sc).val = sc.val + 80 := by + rw [headA_eqS, scWA_val (by change sc.val + 80 < 2^64; omega)] + rfl + have htA : (tailA sc).val = sc.val + 88 := by + rw [tailA_eqS, scWA_val (by change sc.val + 88 < 2^64; omega)] + rfl + let RW : U64 → Prop := fun a => ScObjA sc n a ∧ ¬ ∃ K : U64, K.val < 96 ∧ K.val % 8 = 0 ∧ K.val ≠ 0 ∧ + K.val ≠ 72 ∧ K.val ≠ 80 ∧ K.val ≠ 88 ∧ a = scWA sc K + have hRW : ∀ a : U64, sc.val ≤ a.val → a.val < sc.val + 96 + 16 * m.val → + (a.val = sc.val ∨ a.val = sc.val + 72 ∨ a.val = sc.val + 80 ∨ a.val = sc.val + 88 ∨ sc.val + 96 ≤ a.val) → + RW a := by + intro a h1 h2 h3 + refine ⟨⟨h1, by omega⟩, ?_⟩ + rintro ⟨K, hK, -, h0, h72, h80, h88, rfl⟩ + rw [scWA_val (by omega)] at h3; omega + have wH : ∀ {s0 : S} {a v : U64}, (a = periodA sc ∨ a = maxA sc ∨ a = headA sc ∨ a = tailA sc) → + WritesIn st s0 (L.wr s0 a v) RW := by + intro s0 a v ha + have hv : a.val = sc.val ∨ a.val = sc.val + 72 ∨ a.val = sc.val + 80 ∨ a.val = sc.val + 88 := by + rcases ha with rfl | rfl | rfl | rfl <;> omega + have hal : Aligned a := by unfold Aligned; rcases hv with h | h | h | h <;> rw [h] <;> omega + refine (writesIn_wr hal).mono fun b' hb' => ?_ + subst hb'; exact hRW _ (by omega) (by omega) (by omega) + have wR : ∀ {s0 s1 : S} {k : Nat}, L.rmax s0 sc = m → RingAtA st s0 s1 sc k → WritesIn st s0 s1 RW := by + intro s0 s1 k hm h + refine h.w.mono fun b' hb' => ?_ + rcases hb' with rfl | rfl | ⟨h1, h2⟩ + · exact hRW _ (by omega) (by omega) (by omega) + · exact hRW _ (by omega) (by omega) (by omega) + · rw [hm] at h2; exact hRW _ (by omega) h2 (by omega) + have hfin : ∀ {s' : S}, WritesIn st s s' RW → WritesIn st s s' (ScObjA sc n) ∧ + ∀ K : U64, K.val < 96 → K.val % 8 = 0 → K.val ≠ 0 → K.val ≠ 72 → K.val ≠ 80 → K.val ≠ 88 → + L.mem s' (scWA sc K) = L.mem s (scWA sc K) := by + intro s' hw + refine ⟨hw.mono fun _ h => h.1, fun K h1 h2 h3 h4 h5 h6 => hw.mem_eq ?_ fun h => h.2 ⟨K, h1, h2, h3, h4, h5, h6, rfl⟩⟩ + unfold Aligned; rw [scWA_val (by omega)]; omega + have hctRW : ¬ RW (L.nodeA s layout.node.CUR_TIME) := fun h => hct.2 h.1 + unfold sporadic.refill_new + step with obj.Sc.set_period.spec as ⟨s1, s1_post⟩ + step with obj.Sc.set_refill_head.spec as ⟨s2, s2_post⟩ + step with obj.Sc.set_refill_tail.spec as ⟨s3, s3_post⟩ + step with obj.Sc.set_refill_max.spec as ⟨s4, s4_post⟩ + have hW4 : RefillsWF st s4 sc (n + Fl4SpecA64.minSchedContextBits) := by subst s4_post s3_post s2_post; exact RefillsWF.reset hP (by simpa using hm2) hmx + have hh4 : L.rhead s4 sc = 0#u64 := by + subst s4_post s3_post s2_post; rw [rhead_wr_max h8, rhead_wr_tail h8, rhead_wr_head h8] + have hm4 : L.rmax s4 sc = m := by subst s4_post; rw [rmax_wr_max h8] + have hp4 : L.rperiod s4 sc = p := by + subst s4_post s3_post s2_post s1_post; rw [rperiod_wr_max h8, rperiod_wr_tail h8, rperiod_wr_head h8, + rperiod_wr_period h8] + have hs4 : L.size s4 sc = 1 := by + subst s4_post s3_post s2_post + simp only [LawfulStore.size, rhead_wr_max h8, rhead_wr_tail h8, rhead_wr_head h8, + rtail_wr_max h8, rtail_wr_tail h8]; simp [ringN] + have hw4 : WritesIn st s s4 RW := by + have w1 : WritesIn st s s1 RW := by rw [s1_post]; exact wH (Or.inl rfl) + have w2 : WritesIn st s1 s2 RW := by rw [s2_post]; exact wH (Or.inr (Or.inr (Or.inl rfl))) + have w3 : WritesIn st s2 s3 RW := by rw [s3_post]; exact wH (Or.inr (Or.inr (Or.inr rfl))) + have w4 : WritesIn st s3 s4 RW := by rw [s4_post]; exact wH (Or.inr (Or.inl rfl)) + exact WritesIn.trans1 (WritesIn.trans1 (WritesIn.trans1 w1 w2) w3) w4 + have hct4 : L.curTime s4 = L.curTime s := by + simp only [LawfulStore.curTime] + rw [show L.nodeA s4 layout.node.CUR_TIME = L.nodeA s layout.node.CUR_TIME by + simp only [LawfulStore.nodeA, LawfulStore.nodeAt, hw4.1, hw4.2.1]] + exact hw4.mem_eq hct.1 hctRW + step with sporadic.cur_time.spec as ⟨now, now_post⟩ + rw [hct4] at now_post + subst now_post + have h0 : (0#u64).val < (L.rmax s4 sc).val := by rw [hm4]; simp; omega + step with sporadic.put.spec as ⟨s5, s5_post⟩ + subst s5_post + set s5 := L.putS s4 sc 0#u64 ⟨L.curTime s, b⟩ with hs5d + have h5 : RingAtA st s4 s5 sc n := (RingAtA.refl hW4).putS h0 _ + have hm5 : L.rmax s5 sc = m := h5.mx.trans hm4 + have hrs5 : L.refills s5 sc = [⟨L.curTime s, b⟩] := by + have hsz : L.size s5 sc = 1 := by rw [hs5d, hW4.size_putS h0, hs4] + unfold LawfulStore.refills; rw [hsz] + simp only [List.range_one, List.map_cons, List.map_nil, List.cons.injEq, and_true] + rw [LawfulStore.rf, h5.wf.ix_zero, hs5d, rhead_putS h8 (by simp; omega), hh4, refill_putS_same h8] + have per : ∀ {s' : S}, RingAtA st s4 s' sc n → L.rperiod s' sc = p := by + intro s' h' + have := h'.hdr (K := 0#u64) (by simp) (by simp) + simp only [LawfulStore.rperiod, periodA_eqS] at hp4 ⊢; rw [this]; exact hp4 + unfold sporadic.maybe_add_empty_tail + step with sporadic.is_round_robin.spec as ⟨rr, rr_post⟩ + rw [per h5] at rr_post + split + · rename_i hrr + have hp0 : p = 0#u64 := by rw [rr_post] at hrr; simpa using hrr + step with sporadic.head.spec as ⟨r, r_post⟩ + have er : r = ⟨L.curTime s, b⟩ := by rw [r_post, head_eqA h5.wf, hrs5]; rfl + have hsz5 : L.size s5 sc = 1 := by rw [← refills_lengthA, hrs5]; rfl + refine WP.spec_mono (add_tail_ringA h5 (by rw [hsz5, h5.mx, hm4]; omega) _) ?_ + rintro s' ⟨h', e'⟩ + obtain ⟨hw, hK⟩ := hfin (WritesIn.trans1 hw4 (wR hm4 h')) + refine ⟨hw, h'.wf, per h', h'.mx.trans hm4, ?_, hK⟩ + rw [e', hrs5, er]; unfold cfgNewRingA; rw [if_pos hp0]; rfl + · rename_i hrr + have hp0 : ¬ p = 0#u64 := by rw [rr_post] at hrr; simpa using hrr + rw [WP.spec_ok] + obtain ⟨hw, hK⟩ := hfin (WritesIn.trans1 hw4 (wR hm4 h5)) + refine ⟨hw, h5.wf, per h5, hm5, ?_, hK⟩ + rw [hrs5]; unfold cfgNewRingA; rw [if_neg hp0] + +/-- The head refill after `refill_update`'s move to the current time. -/ +def cfgUpdateHeadWordA (h : sporadic.Refill) (now : U64) : sporadic.Refill := + if h.time ≤ now then ⟨now, h.amount⟩ else h + +/-- `refill_update` on the list. -/ +def cfgUpdatedRingA (h : sporadic.Refill) (now p b : U64) : List sporadic.Refill := + let h' := cfgUpdateHeadWordA h now + if b ≤ h'.amount then + (if p = 0#u64 then [⟨h'.time, b⟩, ⟨h'.time, 0#u64⟩] else [⟨h'.time, b⟩]) + else [h', ⟨core.num.U64.wrapping_add h'.time p, core.num.U64.wrapping_sub b h'.amount⟩] + +/-- **FL4's `refill_update`**: writes inside the object, keeps the header +words other than the period, the ring size and the ring indices, and leaves +the ring `cfgUpdatedRingA`. -/ +theorem cfg_refill_update_wordsA {s : S} {sc : U64} {n : Nat} (hW : RefillsWF st s sc (n + Fl4SpecA64.minSchedContextBits)) (p b m : U64) + (hm2 : 2 ≤ m.val) (hmx : m.val ≤ absMax (n + Fl4SpecA64.minSchedContextBits)) + (hct : Aligned (L.nodeA s layout.node.CUR_TIME) ∧ ¬ ScObjA sc n (L.nodeA s layout.node.CUR_TIME)) : + sporadic.refill_update st s sc p b m ⦃ s' => WritesIn st s s' (ScObjA sc n) ∧ RefillsWF st s' sc (n + Fl4SpecA64.minSchedContextBits) ∧ + L.rperiod s' sc = p ∧ L.rmax s' sc = m ∧ + L.refills s' sc = cfgUpdatedRingA (L.refill s sc (L.rhead s sc)) (L.curTime s) p b ∧ + ∀ K : U64, K.val < 96 → K.val % 8 = 0 → K.val ≠ 0 → K.val ≠ 72 → K.val ≠ 80 → K.val ≠ 88 → + L.mem s' (scWA sc K) = L.mem s (scWA sc K) ⦄ := by + have hP := hW.placed + obtain ⟨h8, hb, hfit⟩ := hP.bounds hm2 hmx + obtain ⟨-, hpA, hmA, hhA, htA, -⟩ := hW.inside + have hi0 : sc.val + 96 + 16 * (0#u64).val + 16 ≤ 2 ^ 64 := by simp; omega + -- the words `refill_update` writes + let RW : U64 → Prop := fun a => ScObjA sc n a ∧ ¬ ∃ K : U64, K.val < 96 ∧ K.val % 8 = 0 ∧ K.val ≠ 0 ∧ + K.val ≠ 72 ∧ K.val ≠ 80 ∧ K.val ≠ 88 ∧ a = scWA sc K + have hRW : ∀ a : U64, sc.val ≤ a.val → a.val < sc.val + 96 + 16 * m.val → + (a.val = sc.val ∨ a.val = sc.val + 72 ∨ a.val = sc.val + 80 ∨ a.val = sc.val + 88 ∨ sc.val + 96 ≤ a.val) → + RW a := by + intro a h1 h2 h3 + refine ⟨⟨h1, by omega⟩, ?_⟩ + rintro ⟨K, hK, -, h0, h72, h80, h88, rfl⟩ + rw [scWA_val (by omega)] at h3; omega + have wH : ∀ {s0 : S} {a v : U64}, (a = periodA sc ∨ a = maxA sc ∨ a = headA sc ∨ a = tailA sc) → + WritesIn st s0 (L.wr s0 a v) RW := by + intro s0 a v ha + have hv : a.val = sc.val ∨ a.val = sc.val + 72 ∨ a.val = sc.val + 80 ∨ a.val = sc.val + 88 := by + rcases ha with rfl | rfl | rfl | rfl <;> omega + have hal : Aligned a := by unfold Aligned; rcases hv with h | h | h | h <;> rw [h] <;> omega + refine (writesIn_wr hal).mono fun b' hb' => ?_ + subst hb'; exact hRW _ (by omega) (by omega) (by omega) + -- the ring of size `m` from `s0`: slots below `m` + have wR : ∀ {s0 s1 : S} {k : Nat}, L.rmax s0 sc = m → RingAtA st s0 s1 sc k → WritesIn st s0 s1 RW := by + intro s0 s1 k hm h + refine h.w.mono fun b' hb' => ?_ + rcases hb' with rfl | rfl | ⟨h1, h2⟩ + · exact hRW _ (by omega) (by omega) (by omega) + · exact hRW _ (by omega) (by omega) (by omega) + · rw [hm] at h2; exact hRW _ (by omega) h2 (by omega) + have hfin : ∀ {s' : S}, WritesIn st s s' RW → WritesIn st s s' (ScObjA sc n) ∧ + ∀ K : U64, K.val < 96 → K.val % 8 = 0 → K.val ≠ 0 → K.val ≠ 72 → K.val ≠ 80 → K.val ≠ 88 → + L.mem s' (scWA sc K) = L.mem s (scWA sc K) := by + intro s' hw + refine ⟨hw.mono fun _ h => h.1, fun K h1 h2 h3 h4 h5 h6 => hw.mem_eq ?_ fun h => h.2 ⟨K, h1, h2, h3, h4, h5, h6, rfl⟩⟩ + unfold Aligned; rw [scWA_val (by omega)]; omega + have hctRW : ¬ RW (L.nodeA s layout.node.CUR_TIME) := fun h => hct.2 h.1 + unfold sporadic.refill_update + step with sporadic.head.spec as ⟨h, h_post⟩ + step with sporadic.put.spec as ⟨s1, s1_post⟩ + step with obj.Sc.set_refill_head.spec as ⟨s2, s2_post⟩ + step with obj.Sc.set_refill_tail.spec as ⟨s3, s3_post⟩ + step with obj.Sc.set_refill_max.spec as ⟨s4, s4_post⟩ + step with obj.Sc.set_period.spec as ⟨s5, s5_post⟩ + have hW4 : RefillsWF st s4 sc (n + Fl4SpecA64.minSchedContextBits) := by subst s4_post s3_post s2_post; exact RefillsWF.reset hP (by simpa using hm2) hmx + have hW5 : RefillsWF st s5 sc (n + Fl4SpecA64.minSchedContextBits) := by subst s5_post; exact hW4.wr_period + have hh5 : L.rhead s5 sc = 0#u64 := by + subst s5_post s4_post s3_post s2_post + rw [rhead_wr_period h8, rhead_wr_max h8, rhead_wr_tail h8, rhead_wr_head h8] + have hm5 : L.rmax s5 sc = m := by + subst s5_post s4_post; rw [rmax_wr_period h8, rmax_wr_max h8] + have hp5 : L.rperiod s5 sc = p := by subst s5_post; rw [rperiod_wr_period h8] + have hs5 : L.size s5 sc = 1 := by + subst s5_post s4_post s3_post s2_post + simp only [LawfulStore.size, rhead_wr_period h8, rhead_wr_max h8, rhead_wr_tail h8, rhead_wr_head h8, + rtail_wr_period h8, rtail_wr_max h8, rtail_wr_tail h8]; simp [ringN] + have hr5 : L.refill s5 sc 0#u64 = h := by + subst s5_post s4_post s3_post s2_post s1_post + rw [refill_wr_hdr' h8 hi0 (Or.inl rfl), refill_wr_hdr' h8 hi0 (Or.inr (Or.inl rfl)), + refill_wr_hdr' h8 hi0 (Or.inr (Or.inr (Or.inr rfl))), refill_wr_hdr' h8 hi0 (Or.inr (Or.inr (Or.inl rfl))), + refill_putS_same h8] + have hw5 : WritesIn st s s5 RW := by + have w1 : WritesIn st s s1 RW := by + obtain ⟨-, -, -, -, -, hin⟩ := hW.inside + have h0 : (0#u64).val < (L.rmax s sc).val := by have := hW.min_le; simp at this ⊢; omega + obtain ⟨e1, e2, -⟩ := hin _ h0 + have a1 : Aligned (rtA sc 0#u64) := by unfold Aligned; rw [e1]; omega + have a2 : Aligned (raA sc 0#u64) := by unfold Aligned; rw [e2]; omega + rw [s1_post] + refine ((writesIn_wr (st := st) (s := s) (v := h.time) a1).trans + (writesIn_wr (st := st) (s := L.wr s (rtA sc 0#u64) h.time) (v := h.amount) a2)).mono fun b' hb' => ?_ + rcases hb' with rfl | rfl + · exact hRW _ (by rw [e1]; omega) (by rw [e1]; simp; omega) (by rw [e1]; omega) + · exact hRW _ (by rw [e2]; omega) (by rw [e2]; simp; omega) (by rw [e2]; omega) + have w2 : WritesIn st s1 s2 RW := by rw [s2_post]; exact wH (Or.inr (Or.inr (Or.inl rfl))) + have w3 : WritesIn st s2 s3 RW := by rw [s3_post]; exact wH (Or.inr (Or.inr (Or.inr rfl))) + have w4 : WritesIn st s3 s4 RW := by rw [s4_post]; exact wH (Or.inr (Or.inl rfl)) + have w5 : WritesIn st s4 s5 RW := by rw [s5_post]; exact wH (Or.inl rfl) + exact WritesIn.trans1 (WritesIn.trans1 (WritesIn.trans1 (WritesIn.trans1 w1 w2) w3) w4) w5 + have hct5 : L.curTime s5 = L.curTime s := by + simp only [LawfulStore.curTime] + rw [show L.nodeA s5 layout.node.CUR_TIME = L.nodeA s layout.node.CUR_TIME by + simp only [LawfulStore.nodeA, LawfulStore.nodeAt, hw5.1, hw5.2.1]] + exact hw5.mem_eq hct.1 hctRW + have hrs5 : L.refills s5 sc = [h] := by + unfold LawfulStore.refills; rw [hs5] + simp only [List.range_one, List.map_cons, List.map_nil, List.cons.injEq, and_true] + rw [LawfulStore.rf, hW5.ix_zero, hh5, hr5] + step with sporadic.ready.spec as ⟨rb, rb_post⟩ + rw [hh5, hr5, hct5] at rb_post + have h0 : (0#u64).val < (L.rmax s5 sc).val := by rw [hm5]; simp; omega + -- after the move to the current time + have key : (if rb = true then do + let now ← sporadic.cur_time st s5 + let r ← sporadic.head st s5 sc + sporadic.set_head st s5 sc { r with time := now } + else ok s5) ⦃ s6 => RingAtA st s5 s6 sc n ∧ L.refills s6 sc = [cfgUpdateHeadWordA h (L.curTime s)] ⦄ := by + split + · rename_i hrb + step with sporadic.cur_time.spec as ⟨now, now_post⟩ + step with sporadic.head.spec as ⟨r, r_post⟩ + rw [hh5, hr5] at r_post + refine WP.spec_mono (set_head_ringA (RingAtA.refl hW5) _) ?_ + rintro s6 ⟨h6, e6⟩ + refine ⟨h6, ?_⟩ + rw [e6, hrs5] + have : h.time ≤ L.curTime s := by rw [rb_post] at hrb; simpa using hrb + unfold cfgUpdateHeadWordA; rw [if_pos this, now_post, hct5, r_post]; rfl + · rename_i hrb + rw [WP.spec_ok] + refine ⟨RingAtA.refl hW5, ?_⟩ + have : ¬ h.time ≤ L.curTime s := by rw [rb_post] at hrb; simpa using hrb + rw [hrs5]; unfold cfgUpdateHeadWordA; rw [if_neg this] + refine WP.spec_bind key ?_ + rintro s6 ⟨h6, e6⟩ + have hW6 := h6.wf + have hm6 : L.rmax s6 sc = m := h6.mx.trans hm5 + have per : ∀ {s' : S}, RingAtA st s5 s' sc n → L.rperiod s' sc = p := by + intro s' h' + have := h'.hdr (K := 0#u64) (by simp) (by simp) + simp only [LawfulStore.rperiod, periodA_eqS] at hp5 ⊢; rw [this]; exact hp5 + step with sporadic.head.spec as ⟨hd, hd_post⟩ + have ehd : hd = cfgUpdateHeadWordA h (L.curTime s) := by + rw [hd_post, head_eqA hW6, e6]; rfl + split + · rename_i hge + refine WP.spec_bind (set_head_ringA h6 _) ?_ + rintro s7 ⟨h7, e7⟩ + rw [e6] at e7 + simp only [List.tail_cons] at e7 + have hsz7 : L.size s7 sc = 1 := by rw [← refills_lengthA, e7]; rfl + unfold sporadic.maybe_add_empty_tail + step with sporadic.is_round_robin.spec as ⟨rr, rr_post⟩ + rw [per h7] at rr_post + split + · rename_i hrr + have hp0 : p = 0#u64 := by rw [rr_post] at hrr; simpa using hrr + step with sporadic.head.spec as ⟨r, r_post⟩ + have er : r = ⟨hd.time, b⟩ := by rw [r_post, head_eqA h7.wf, e7]; rfl + refine WP.spec_mono (add_tail_ringA h7 (by rw [hsz7, h7.mx, hm5]; omega) _) ?_ + rintro s' ⟨h', e'⟩ + obtain ⟨hw, hK⟩ := hfin (WritesIn.trans1 hw5 (wR hm5 h')) + refine ⟨hw, h'.wf, per h', h'.mx.trans hm5, ?_, hK⟩ + rw [e', e7, er, ← h_post] + unfold cfgUpdatedRingA; rw [← ehd, if_pos hge, if_pos hp0]; rfl + · rename_i hrr + have hp0 : ¬ p = 0#u64 := by rw [rr_post] at hrr; simpa using hrr + rw [WP.spec_ok] + obtain ⟨hw, hK⟩ := hfin (WritesIn.trans1 hw5 (wR hm5 h7)) + refine ⟨hw, h7.wf, per h7, h7.mx.trans hm5, ?_, hK⟩ + rw [e7, ← h_post] + unfold cfgUpdatedRingA; rw [← ehd, if_pos hge, if_neg hp0] + · rename_i hlt + simp only [lift, bind_ok] + step as ⟨i1, i1_post⟩ + have hlt' : hd.amount.val < b.val := by simp only [ge_iff_le, UScalar.le_equiv, not_le] at hlt; exact hlt + have ei1 : i1 = core.num.U64.wrapping_sub b hd.amount := + UScalar.eq_of_val_eq (by rw [wsub_val (by omega)]; omega) + have hsz6 : L.size s6 sc = 1 := by rw [← refills_lengthA, e6]; rfl + refine WP.spec_mono (add_tail_ringA h6 (by rw [hsz6, hm6]; omega) _) ?_ + rintro s' ⟨h', e'⟩ + obtain ⟨hw, hK⟩ := hfin (WritesIn.trans1 hw5 (wR hm5 h')) + refine ⟨hw, h'.wf, per h', h'.mx.trans hm5, ?_, hK⟩ + rw [e', e6, ei1, ← h_post] + unfold cfgUpdatedRingA; rw [← ehd, if_neg (fun h' => hlt h')]; rfl + +end Concrete + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScCfgResumeA.lean b/proofs/Fl4ProofsA64/P3/ScCfgResumeA.lean new file mode 100644 index 000000000..336be8f56 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScCfgResumeA.lean @@ -0,0 +1,285 @@ +import Fl4ProofsA64.P3.ScBindExecA +import Fl4ProofsA64.P3.ScBindStallA +import Fl4ProofsA64.P3.ScCfgPrepA +import Fl4SpecA64.Ext.SchedBudget + +/-! Restore the bound owner's readiness and perform the local switch +request after configuring its scheduling context. -/ + +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +def cfgResumeBoundA (p t : ObjRef) : SMonad Unit Unit := do + fl4SchedContextResume p + let sts ← getThreadState t + let ct ← NonDet.gets fun s : State Unit => s.curThread + if t = ct then rescheduleRequired + else NonDet.when (runnable sts) (possibleSwitchTo t) + +private theorem cfgTcbEqA (t u : U64) : + obj.Tcb.Insts.CoreCmpPartialEqTcb.eq t u = ok (decide (t = u)) := by + unfold obj.Tcb.Insts.CoreCmpPartialEqTcb.eq + simp + +private theorem cfgTcbNeA (t u : U64) : + core.cmp.PartialEq.ne.trait_default obj.Tcb.Insts.CoreCmpPartialEqTcb t u = ok (decide (t ≠ u)) := by + unfold core.cmp.PartialEq.ne.trait_default core.cmp.PartialEq.ne.default + simp [obj.Tcb.Insts.CoreCmpPartialEqTcb.eq] + exact ⟨fun e => e ▸ rfl, UScalar.eq_of_val_eq⟩ + +private theorem cfgOutcomeA {σ τ : State Unit} + (h : ({((), σ)}, False) = (({((), τ)}, False) : Set (Unit × State Unit) × Prop)) : + σ = τ := + (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst h))).2 + +private theorem cfgSwitchFixA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ σ' : State Unit} + (H : SchedSRA st Z m κ s σ) {t : U64} (ht : t ∈ Z.T) + (h : possibleSwitchTo (Z := Unit) (specPtr t) σ = ({((), σ')}, False)) + : SFixA σ σ' ∧ σ'.releaseQueue = σ.releaseQueue ∧ + (∀ u, σ'.schedulerAction = .switchThread u → + σ.schedulerAction = .switchThread u ∨ (u = specPtr t ∧ specPtr t ∉ σ'.releaseQueue)) := by + obtain ⟨x, hx, hd, -, -⟩ := H.rest.prio t ht + have d : ¬ x.tcbDomain ≠ σ.curDomain := by rw [hd, H.rest.curDom.1]; exact fun h => h rfl + rw [possibleSwitchTo_unfoldA hx] at h + split at h + next cond => + try rw [if_neg d] at h + split at h + next cond' => + -- Derive the exact abstract reschedule from the action. + obtain ⟨τ, eq, fix, kh, rh, action, _⟩ := rescheduleResultA H + have hx' : τ.kheap (specPtr t) = some (.tcb x) := kh ▸ hx + rw [nd_bind_oneSc eq, tcbSchedAction_eqA hx' hd tcbSchedEnqueue] at h + cases cfgOutcomeA h + refine ⟨⟨fix.curThread, fix.curSc, fix.idleThread, fix.curTime, fix.consumedTime, fix.reprogramTimer, + fix.curDomain, fix.domainTime⟩, rh, fun u hu => ?_⟩ + rw [show (setRqA τ _ _).schedulerAction = τ.schedulerAction from rfl, action] at hu + cases hu + next => + change ({((), { σ with schedulerAction := .switchThread (specPtr t) })}, False) = + ({((), σ')}, False) at h + cases cfgOutcomeA h + exact ⟨⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩, rfl, fun u e => Or.inr ⟨(SchedulerAction.switchThread.inj e).symm, by + simpa [inReleaseQueue] using cond.2⟩⟩ + next => + change ({((), σ)}, False) = ({((), σ')}, False) at h + cases cfgOutcomeA h + exact ⟨SFixA.refl σ, rfl, fun _ e => Or.inl e⟩ + +private theorem cfgResumeFixA {σ σ' : State Unit} {m : McState} (I : ScInvA m σ) + {t p : ObjRef} {x : Tcb} {y : SchedContext} {n : Nat} + (ht : σ.kheap t = some (.tcb x)) (hy : σ.kheap p = some (.schedContext y n)) + (hs : x.tcbSchedContext = some p) (ho : y.scTcb = some t) + (ha : scActive y = true) (hi : NotIdleScA m p) (hn : ∀ d q, t ∉ σ.readyQueues d q) + (hd : x.tcbDomain = 0) + (h : schedContextResume (Z := Unit) p σ = ({((), σ')}, False)) : + SFixA σ { σ' with reprogramTimer := σ.reprogramTimer } ∧ + σ'.schedulerAction = σ.schedulerAction ∧ + (∀ u ∈ σ'.releaseQueue, u ∈ σ.releaseQueue ∨ u = t) ∧ + (t ∈ σ'.releaseQueue → t ∉ σ.releaseQueue → refillReady σ.curTime (refillHd y) ≠ true) := by + rw [resume_spec_eqA hy ho ht (I.ne hy) (fun h => ⟨(schedulable_partsA h).1 _ ht, ha⟩) (hn _ _)] at h + by_cases e : schedulable t σ = true ∧ ¬(refillReady σ.curTime (refillHd y) = true ∧ + refillSufficient 0 (refillHd y) = true) + · rw [if_pos e] at h + have deq : tcbSchedAction (Z := Unit) tcbSchedDequeue t σ = ({((), σ)}, False) := by + rw [tcbSchedAction_eqA ht hd, tcbSchedDequeue, List.filter_eq_self.2 (fun v hv => by + simpa using fun eq : t = v => hn _ _ (eq ▸ hv)), setRqA_self] + have hr : ∀ u ∈ σ.releaseQueue, readTcbReadyTime (Z := Unit) u σ = some (specRtA σ u) := by + intro u hu + obtain ⟨z, p', r, k, h1, h2, h3, h4⟩ := I.relSc u hu + rw [readTcbReadyTime_eqA h1 h2 h3 (I.ne h3 h4), specRtA_of h1 h2 h3 (I.ne h3 h4)] + have hti : readTcbReadyTime (Z := Unit) t σ = some (specRtA σ t) := by + rw [readTcbReadyTime_eqA ht hs hy (I.ne hy ha), specRtA_of ht hs hy (I.ne hy ha)] + obtain ⟨b, hq⟩ := tcbReleaseEnqueue_eqA t (specRtA σ) hti hr + unfold postpone at h + rw [nd_bind_oneSc (getScTcb_eqSc hy), ho, + nd_bind_oneSc (show NonDet.assertOpt (some t) σ = ({(t, σ)}, False) from rfl), + nd_bind_oneSc deq, nd_bind_oneSc hq] at h + have hstate : ({((), setRelA σ (specInsTA σ.releaseQueue (specRtA σ) t) true)}, False) = ({((), σ')}, False) := h + cases cfgOutcomeA hstate + refine ⟨⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩, rfl, ?_, fun _ _ he => e.2 ⟨he, I.suff hy ha hi⟩⟩ + intro u hu + change u ∈ specInsTA σ.releaseQueue (specRtA σ) t at hu + unfold specInsTA at hu + rcases List.mem_append.1 hu with hu | hu + · exact Or.inl ((List.takeWhile_sublist _).subset hu) + · rcases List.mem_cons.1 hu with rfl | hu + · exact Or.inr rfl + · exact Or.inl ((List.dropWhile_sublist _).subset hu) + · rw [if_neg e] at h + cases cfgOutcomeA h + exact ⟨⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩, rfl, fun u hu => Or.inl hu, fun ht hn => (hn ht).elim⟩ + +set_option maxHeartbeats 1100000 in +theorem cfg_resume_boundA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + {sc t cpu : U64} (H : SchedSRB st Z m κ s σ (specPtr t)) (a : A) + (hmb : sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) (hagree : fl4MinBudget = MIN_BUDGET) + (hc : sc ∈ Z.C) (ht : t ∈ Z.T) (h0 : sc ≠ 0#u64) + (hcpu : cpu = L.cpu s) (hon : L.mem s (affA t) = L.cpu s) + (hco : L.mem s (scCoreA sc) = L.cpu s) (hni : NotIdleScA m (scNameA m sc)) + {x : Tcb} {y : SchedContext} {n : Nat} + (hkt : σ.kheap (specPtr t) = some (.tcb x)) (hks : σ.kheap (scNameA m sc) = some (.schedContext y n)) + (hts : x.tcbSchedContext = some (scNameA m sc)) (hstc : y.scTcb = some (specPtr t)) + (hactive : scActive y = true) + (hnq : ∀ d p, specPtr t ∉ σ.readyQueues d p) (hnr : specPtr t ∉ σ.releaseQueue) + (hst : StEqA Z.T (L.mem s) σ) + (hb : runnable x.tcbState = false → inClassRSc x.tcbState x.tcbFault → + scActive y = true → refillReady σ.curTime (refillHd y) = true) : + (do + let (s5, a3) ← smp.migrate_tcb st ar s a t cpu + let (s6, a4) ← schedcontext.resume st ar s5 a3 sc + let i1 ← st.cur_cpu s6 + let (s7, a5) ← + if cpu = i1 then do + let run ← sched.is_runnable st s6 t + let s8 ← if run then do + let ct ← sched.cur_thread st s6 + let b ← core.cmp.PartialEq.ne.trait_default obj.Tcb.Insts.CoreCmpPartialEqTcb t ct + if b then sched.possible_switch_to st s6 t else ok s6 + else ok s6 + ok (s8, a4) + else do + let b ← sched.is_schedulable st s6 t + if b then + let s8 ← sched.enqueue st s6 t + smp.remote_reschedule st ar s8 a4 cpu + else + let s8 ← sched.set_reprogram_on st s6 cpu + smp.remote_reschedule st ar s8 a4 cpu + let t0 ← sched.cur_thread st s7 + let b ← obj.Tcb.Insts.CoreCmpPartialEqTcb.eq t t0 + let s8 ← if b then sched.reschedule_required st s7 else ok s7 + ok (s8, a5)) ⦃ r => r.2 = a ∧ ∃ σ' Q' R', + cfgResumeBoundA (scNameA m sc) (specPtr t) σ = ({((), σ')}, False) ∧ + SchedSRA st { Z with Q := Q', R := R' } m κ r.1 σ' ∧ + σ'.kheap = σ.kheap ∧ SFixA σ { σ' with reprogramTimer := σ.reprogramTimer } ∧ + (∀ u ∈ σ'.releaseQueue, u ∈ σ.releaseQueue ∨ u = specPtr t) ∧ + (specPtr t ∈ σ'.releaseQueue → refillReady σ.curTime (refillHd y) ≠ true) ∧ + (∀ u, σ'.schedulerAction = .switchThread u → + σ.schedulerAction = .switchThread u ∨ (u = specPtr t ∧ runnable x.tcbState = true ∧ specPtr t ∉ σ'.releaseQueue)) ∧ MFrA (st := st) Z m s r.1 σ σ' ⦄ := by + have haff : obj.Tcb.affinity st t s = ok cpu := + (eq_of_spec_fd (obj.Tcb.affinity.spec s t)).trans (congrArg ok (hon.trans hcpu.symm)) + rw [sc_migrate_same_coreA haff] + simp only [bind_ok, uncurry_apply_pair] + have hsw : L.mem s (scA t) = sc := by + obtain ⟨z, hz, h⟩ := H.sch.tcb t ht + rw [hkt] at hz; cases hz + have hn : L.mem s (scA t) ≠ 0#u64 := by + intro e + rw [e] at h + simp [optScA, hts] at h + have hm := H.rest.scMem t ht hn + have he : scNameA m (L.mem s (scA t)) = scNameA m sc := by + unfold optScA at h + rw [if_neg hn, hts] at h + exact (Option.some.inj h).symm + exact H.scp.inj _ hm _ hc he + refine WP.spec_bind (bind_resumeA H a hmb hc ht h0 hon hco hni hkt hks hsw hts hstc hnq hnr hst hb) ?_ + rintro ⟨s1, a1⟩ ⟨ea, σ1, R', hresume, hK1, H1, hst1, W1⟩ + change a1 = a at ea + subst a1 + simp only [uncurry_apply_pair] + have hd := (H.rest.prio t ht) + obtain ⟨x0, he0, hd0, -, -⟩ := hd + rw [hkt] at he0; cases he0 + obtain ⟨fix1, act1, rel1, late1⟩ := cfgResumeFixA H.inv hkt hks hts hstc + (ha := by + -- The selected and configured SC is active. + exact hactive) hni hnq hd0 hresume + have hkt1 : σ1.kheap (specPtr t) = some (.tcb x) := hK1 ▸ hkt + have hon1 : L.mem s1 (affA t) = L.cpu s1 := by + rw [W1.wr.2.1, ← hon] + exact UScalar.eq_of_val_eq (Option.some.inj ((H1.sch.aff t ht).symm.trans (H.sch.aff t ht))) + have hCpu1 : st.cur_cpu s1 = ok cpu := (L.cur_cpu_eq s1).trans (congrArg ok (W1.wr.2.1.trans hcpu.symm)) + rw [hCpu1, bind_ok, if_pos rfl] + have hread : sched.cur_thread st s1 = ok (L.curW s1) := eq_of_spec_fd (sched.cur_thread.spec s1) + have hRun : sched.is_runnable st s1 t = ok (runnable x.tcbState) := by + rw [eq_of_spec_fd (sched.is_runnable.spec (L := L) s1 t)] + have eq : decide (L.mem s1 (stA t) = sched.TS_RUNNING ∨ L.mem s1 (stA t) = sched.TS_RESTART ∨ + L.mem s1 (stA t) = sched.TS_RUNNING_VM) = decide (runnable x.tcbState = true) := + decide_eq_decide.mpr (runnable_iffA (hst1 t ht x hkt1)) + rw [eq] + simp + have he : cfgResumeBoundA (scNameA m sc) (specPtr t) σ = + (if specPtr t = σ1.curThread then rescheduleRequired (Z := Unit) else + NonDet.when (runnable x.tcbState) (possibleSwitchTo (specPtr t))) σ1 := by + unfold cfgResumeBoundA + rw [fl4SchedContextResume_eq_l4v hagree, nd_bind_oneSc hresume] + unfold getThreadState + rw [ + nd_bind_oneSc (threadGet_eqSc Tcb.tcbState hkt1), nd_gets_bindSc] + -- The concrete branch reads the same current thread on the local view. + have htail : + ∀ (s2 : S), L.curW s2 = L.curW s1 → + t ≠ L.curW s1 → (do + let ct ← sched.cur_thread st s2 + let b ← obj.Tcb.Insts.CoreCmpPartialEqTcb.eq t ct + let s' ← if b then sched.reschedule_required st s2 else ok s2 + ok (s', a)) = ok (s2, a) := by + intro s2 e ne + have hr : sched.cur_thread st s2 = ok (L.curW s1) := + (eq_of_spec_fd (sched.cur_thread.spec s2)).trans (congrArg ok e) + rw [hr, bind_ok, cfgTcbEqA, bind_ok, decide_eq_false ne] + simp only [Bool.false_eq_true, ↓reduceIte, bind_ok] + rw [hRun, bind_ok] + by_cases htcur : t = L.curW s1 + · have htc : specPtr t = σ1.curThread := by rw [H1.sch.curThread, htcur] + have eq : (if runnable x.tcbState then do + let t1 ← sched.cur_thread st s1 + let b2 ← core.cmp.PartialEq.ne.trait_default obj.Tcb.Insts.CoreCmpPartialEqTcb t t1 + if b2 then sched.possible_switch_to st s1 t else ok s1 + else ok s1) = ok s1 := by + cases h : runnable x.tcbState + · rfl + · simp only [↓reduceIte] + rw [hread, bind_ok, cfgTcbNeA, bind_ok, decide_eq_false (fun h => h htcur)] + rfl + rw [eq] + simp only [bind_ok, uncurry_apply_pair] + rw [hread, bind_ok, cfgTcbEqA, bind_ok, decide_eq_true htcur] + simp only [↓reduceIte] + refine WP.spec_bind (reschedule_required_refinesA H1 hst1) ?_ + rintro s2 ⟨σ2, Q', h2, hk, rel2, w2, H2⟩ + rw [WP.spec_ok] + obtain ⟨τ, hτ, fix2, -, -, act2, _⟩ := rescheduleResultA H1 + have e2 : τ = σ2 := cfgOutcomeA (hτ.symm.trans h2) + subst τ + exact ⟨rfl, σ2, Q', R', he.trans (by rw [if_pos htc]; exact h2), H2, hk.trans hK1, + ⟨fix2.curThread.trans fix1.curThread, fix2.curSc.trans fix1.curSc, fix2.idleThread.trans fix1.idleThread, + fix2.curTime.trans fix1.curTime, fix2.consumedTime.trans fix1.consumedTime, rfl, + fix2.curDomain.trans fix1.curDomain, fix2.domainTime.trans fix1.domainTime⟩, + fun u hu => rel1 u (rel2 ▸ hu), fun h => late1 (rel2 ▸ h) hnr, + (fun u hu => by rw [act2] at hu; cases hu), W1.trans (MFrA.of_sw hk w2) rfl rfl⟩ + · have htc : specPtr t ≠ σ1.curThread := by rw [H1.sch.curThread]; exact fun e => htcur (specPtr_inj e) + cases h : runnable x.tcbState + · simp only [Bool.false_eq_true, ↓reduceIte, bind_ok, uncurry_apply_pair, htail _ rfl htcur, WP.spec_ok] + exact ⟨True.intro, σ1, Z.Q, R', he.trans (by rw [if_neg htc, NonDet.when, if_neg (by simp [h])]; rfl), H1, hK1, fix1, rel1, fun h => late1 h hnr, fun _ e => Or.inl (act1 ▸ e), W1⟩ + · rw [if_pos rfl, hread, bind_ok, cfgTcbNeA, bind_ok, decide_eq_true htcur] + simp only [↓reduceIte] + repeat rw [cfgResultAssocA] + refine WP.spec_bind (possible_switch_to_refinesA H1 t ht hon1 (fun z hz => by rw [hkt1] at hz; cases hz; exact h) hst1) ?_ + rintro s2 ⟨σ2, Q', h2, hk, w2, H2⟩ + obtain ⟨fix2, rel2, act2⟩ := cfgSwitchFixA H1 ht h2 + have hcur : L.curW s2 = L.curW s1 := by + have e := fix2.curThread + rw [H2.sch.curThread, H1.sch.curThread] at e + exact specPtr_inj e + simp only [bind_ok, uncurry_apply_pair, htail _ hcur htcur, WP.spec_ok] + exact ⟨True.intro, σ2, Q', R', he.trans (by rw [if_neg htc, NonDet.when, if_pos h]; exact h2), H2, + hk.trans hK1, + ⟨fix2.curThread.trans fix1.curThread, fix2.curSc.trans fix1.curSc, fix2.idleThread.trans fix1.idleThread, + fix2.curTime.trans fix1.curTime, fix2.consumedTime.trans fix1.consumedTime, rfl, + fix2.curDomain.trans fix1.curDomain, fix2.domainTime.trans fix1.domainTime⟩, + fun u hu => rel1 u (rel2 ▸ hu), fun h => late1 (rel2 ▸ h) hnr, + fun u hu => (act2 u hu).imp (fun e => act1 ▸ e) (fun e => ⟨e.1, True.intro, e.2⟩), + W1.trans (MFrA.of_sw hk w2) rfl rfl⟩ + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScCfgRingA.lean b/proofs/Fl4ProofsA64/P3/ScCfgRingA.lean new file mode 100644 index 000000000..51517d0cf --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScCfgRingA.lean @@ -0,0 +1,142 @@ +import Fl4ProofsA64.P3.ScCfgStateA + +/-! ConfigureFlags' two refill paths under the local scheduler relation. -/ + +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +private theorem cfgReadyWordA (h : sporadic.Refill) (now : U64) : + refillReady now.bv (toRefillA h) = decide (h.time ≤ now) := by + change decide (h.time.bv + (0 : Ticks) ≤ now.bv) = _ + have e : h.time.bv + (0 : Ticks) = h.time.bv := by bv_omega + rw [e] + exact decide_eq_decide.2 (u64_le_bvA _ _).symm + +private theorem cfgNewRing_mapA (now b p : U64) : + (cfgNewRingA now b p).map toRefillA = cfgNewRefillsA now.bv b.bv p.bv := by + have e : p.bv = 0 ↔ p = 0#u64 := ⟨fun h => eq_of_bv h, fun h => h ▸ rfl⟩ + unfold cfgNewRingA cfgNewRefillsA + by_cases h : p = 0#u64 + · rw [if_pos h, if_pos (e.2 h)]; rfl + · rw [if_neg h, if_neg (fun h' => h (e.1 h'))]; rfl + +private theorem cfgUpdatedRing_mapA (h : sporadic.Refill) (now p b : U64) : + (cfgUpdatedRingA h now p b).map toRefillA = cfgUpdatedRefillsA (toRefillA h) now.bv p.bv b.bv := by + have hd : cfgUpdateHeadA (toRefillA h) now.bv = toRefillA (cfgUpdateHeadWordA h now) := by + unfold cfgUpdateHeadA cfgUpdateHeadWordA + rw [cfgReadyWordA] + by_cases e : h.time ≤ now + · simp only [e, decide_true, ↓reduceIte]; rfl + · simp only [e, decide_false, Bool.false_eq_true, ↓reduceIte] + unfold cfgUpdatedRefillsA cfgUpdatedRingA cfgUpdateListA + rw [hd] + dsimp only + generalize cfgUpdateHeadWordA h now = h' + have bp : b ≤ h'.amount ↔ b.bv ≤ (toRefillA h').rAmount := u64_le_bvA _ _ + have ep : p.bv = 0 ↔ p = 0#u64 := ⟨fun h => eq_of_bv h, fun h => h ▸ rfl⟩ + by_cases h1 : b ≤ h'.amount + · rw [if_pos h1, if_pos (bp.1 h1)] + by_cases h2 : p = 0#u64 + · rw [if_pos h2, if_pos (ep.2 h2)]; rfl + · rw [if_neg h2, if_neg (fun h => h2 (ep.1 h))]; rfl + · rw [if_neg h1, if_neg (fun h => h1 (bp.2 h))] + simp [toRefillA, core.num.U64.wrapping_add_bv_eq, core.num.U64.wrapping_sub_bv_eq] + +/-- Replacing the period, size and ring keeps the rest of the SC related. -/ +private theorem cfgRingRelA {s s' : S} {sc : U64} {ii : Prop} {x : SchedContext} {n : Nat} + (R : ScRelWA st ii s sc x n) (p b m : U64) (r : List Refill) + (H : RefillsWF st s' sc (n + minSchedContextBits)) + (hp : L.rperiod s' sc = p) (hm : L.rmax s' sc = m) (hr : r = (L.refills s' sc).map toRefillA) + (hh : ∀ K : U64, K.val < 96 → K.val % 8 = 0 → K.val ≠ 0 → K.val ≠ 72 → K.val ≠ 80 → K.val ≠ 88 → + L.mem s' (scWA sc K) = L.mem s (scWA sc K)) : + ScRelWA st ii s' sc (cfgUpdateScA x r p.bv b.bv m.val) n := by + have hn : (L.rmax s' sc).val ≠ 0 := by + have e : 2 ≤ (L.rmax s' sc).val := by simpa using H.min_le + omega + have hper : (cfgUpdateScA x r p.bv b.bv m.val).scPeriod = (L.rperiod s' sc).bv := by rw [hp]; rfl + have hc : x.scConsumed = (L.mem s' (scWA sc 8#u64)).bv := by rw [hh _ (by simp) (by simp) (by simp) (by simp) (by simp) (by simp)]; exact R.consumed + have ht : x.scTcb = optRefS (L.mem s' (scWA sc 24#u64)) := by rw [hh _ (by simp) (by simp) (by simp) (by simp) (by simp) (by simp)]; exact R.tcb + have hpR : x.scReplies.head? = optRefS (L.mem s' (scWA sc 32#u64)) := by rw [hh _ (by simp) (by simp) (by simp) (by simp) (by simp) (by simp)]; exact R.reply + have hnt : x.scNtfn = optRefS (L.mem s' (scWA sc 40#u64)) := by rw [hh _ (by simp) (by simp) (by simp) (by simp) (by simp) (by simp)]; exact R.ntfn + have hb : x.scBadge = (L.mem s' (scWA sc 48#u64)).bv := by rw [hh _ (by simp) (by simp) (by simp) (by simp) (by simp) (by simp)]; exact R.badge + have hy : x.scYieldFrom = optRefS (L.mem s' (scWA sc 56#u64)) := by rw [hh _ (by simp) (by simp) (by simp) (by simp) (by simp) (by simp)]; exact R.yieldFrom + have hs : x.scSporadic = decide (L.mem s' (scWA sc 64#u64) ≠ 0#u64) := by rw [hh _ (by simp) (by simp) (by simp) (by simp) (by simp) (by simp)]; exact R.sporadic + have hmax : (L.rmax s' sc).val = m.val := congrArg (fun v : U64 => v.val) hm + exact ⟨R.pl, hper, hc, ht, hpR, hnt, hb, hy, hs, fun _ => hn, fun _ => hn, hmax, + fun _ => ⟨H, hper, hc, ht, hpR, hnt, hb, hy, hs, hmax, hr⟩⟩ + +private theorem cfgTimeWordA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRN st Z m κ s σ) {sc : U64} (hc : sc ∈ Z.C) {x : SchedContext} {n : Nat} + (hk : σ.kheap (scNameA m sc) = some (.schedContext x n)) : + Aligned (L.nodeA s layout.node.CUR_TIME) ∧ ¬ ScObjA sc n (L.nodeA s layout.node.CUR_TIME) := by + obtain ⟨nn, -, hn, hm⟩ := H.csN + have c := (hm _).1 H.sch.cpuCs + have off : layout.node.CUR_TIME.val = 48 := by unfold layout.node.CUR_TIME; rfl + have hr : (L.cpu s).val < 128 := by omega + have e := nodeAt_valSc (s := s) H.ksFit hr + (by rw [off]; decide : layout.node.CUR_TIME.val < 4288) + have a := H.ksb_al + change Aligned (L.nodeAt s (L.cpu s) layout.node.CUR_TIME) ∧ _ + refine ⟨by unfold Aligned; rw [e, off]; omega, ?_⟩ + intro ha + have b := (H.scp.out sc hc x n hk _ ha).2 + unfold LawfulStore.nodeA at b + rw [e, off] at b + unfold ksSizeA at b + omega + +theorem cfg_refill_new_refinesA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + {u : ObjRef} (H : SchedSRB st Z m κ s σ u) {sc : U64} (hc : sc ∈ Z.C) {x : SchedContext} {n : Nat} + (hk : σ.kheap (scNameA m sc) = some (.schedContext x n)) + (ho : x.scTcb = none ∨ x.scTcb = some u) (hn : u ∉ σ.releaseQueue) + (max b p : U64) (h2 : 2 ≤ max.val) (hmax : max.val ≤ absMax (n + minSchedContextBits)) + (hb : MIN_BUDGET ≤ b.bv) : + sporadic.refill_new st s sc max b p ⦃ s' => + let y := cfgNewScA x σ.curTime max.val b.bv p.bv + SchedSRB st Z m κ s' (updKSc σ (scNameA m sc) (.schedContext y n)) u ∧ + WritesIn st s s' (ScObjA sc n) ⦄ := by + obtain ⟨x0, n0, hk0, R⟩ := H.scr.sc sc hc + rw [hk] at hk0; cases hk0 + refine WP.spec_mono (cfg_refill_new_wordsA R.pl max b p h2 hmax (cfgTimeWordA H.toSchedSRN hc hk)) ?_ + rintro s' ⟨hw, wf, hp, hm, hl, hh⟩ + have hr : cfgNewRefillsA σ.curTime b.bv p.bv = (L.refills s' sc).map toRefillA := by + rw [hl, cfgNewRing_mapA, H.sch.time] + have hcore : L.mem s' (scCoreA sc) = L.mem s (scCoreA sc) := by + rw [show scCoreA sc = scWA sc 16#u64 by unfold scCoreA scWA layout.sc.CORE; rfl] + exact hh 16#u64 (by simp) (by simp) (by simp) (by simp) (by simp) (by simp) + obtain ⟨hp', hr'⟩ := cfgNewSc_okA x σ.curTime max.val p.bv hb + exact ⟨H.scReconfA hc hk hw (cfgRingRelA R p b max _ wf hp hm hr hh) rfl ho hn hp' hr' hcore, hw⟩ + +theorem cfg_refill_update_refinesA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + {u : ObjRef} (H : SchedSRB st Z m κ s σ u) {sc : U64} (hc : sc ∈ Z.C) {x : SchedContext} {n : Nat} + (hk : σ.kheap (scNameA m sc) = some (.schedContext x n)) (ha : scActive x = true) + (ho : x.scTcb = none ∨ x.scTcb = some u) (hn : u ∉ σ.releaseQueue) + (p b max : U64) (h2 : 2 ≤ max.val) (hmax : max.val ≤ absMax (n + minSchedContextBits)) + (hp0 : p.bv ≠ 0) (hb : MIN_BUDGET ≤ b.bv) (hx : MIN_BUDGET ≤ (refillHd x).rAmount) : + sporadic.refill_update st s sc p b max ⦃ s' => + let y := cfgUpdatedScA x σ.curTime p.bv b.bv max.val + SchedSRB st Z m κ s' (updKSc σ (scNameA m sc) (.schedContext y n)) u ∧ + WritesIn st s s' (ScObjA sc n) ⦄ := by + obtain ⟨x0, n0, hk0, R⟩ := H.scr.sc sc hc + rw [hk] at hk0; cases hk0 + have R1 := R.strong ha + refine WP.spec_mono (cfg_refill_update_wordsA R1.wf p b max h2 hmax (cfgTimeWordA H.toSchedSRN hc hk)) ?_ + rintro s' ⟨hw, wf, hp, hm, hl, hh⟩ + have hr : cfgUpdatedRefillsA (refillHd x) σ.curTime p.bv b.bv = (L.refills s' sc).map toRefillA := by + rw [hl, cfgUpdatedRing_mapA, refillHd_eqA R1.wf R1.refills, H.sch.time] + have hcore : L.mem s' (scCoreA sc) = L.mem s (scCoreA sc) := by + rw [show scCoreA sc = scWA sc 16#u64 by unfold scCoreA scWA layout.sc.CORE; rfl] + exact hh 16#u64 (by simp) (by simp) (by simp) (by simp) (by simp) (by simp) + have hpos := cfgUpdateSc_okA x σ.curTime max.val hx hb hp0 + exact ⟨H.scReconfA hc hk hw (cfgRingRelA R p b max _ wf hp hm hr hh) rfl ho hn + (fun _ => hpos) (fun h => (hp0 h).elim) hcore, hw⟩ + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScCfgRunA.lean b/proofs/Fl4ProofsA64/P3/ScCfgRunA.lean new file mode 100644 index 000000000..8b460be9f --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScCfgRunA.lean @@ -0,0 +1,427 @@ +import Fl4ProofsA64.P3.ScCfgFinishA +import Fl4ProofsA64.P3.ScCfgHeaderA +import Fl4ProofsA64.P3.ScCfgSelectA + +/-! +Local ConfigureFlags, including billing the current SC before reconfiguration. +[P] SC timer conversions and minimum agreement. +[J] Current timing and action predicates, with reciprocal SC ownership. +[S] Same-core control and a bound owner whose stall operation is an identity. +-/ + +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3.Sched + +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +private def cfgPreOptionA (p : ObjRef) (owner : Option ObjRef) : SMonad Unit Unit := + NonDet.when (owner ≠ none) <| do + let t ← NonDet.assertOpt owner + cfgPrepareA t p + +private def cfgResumeOptionA (p : ObjRef) (owner : Option ObjRef) : SMonad Unit Unit := + NonDet.when (owner ≠ none) <| do + fl4SchedContextResume p + let t ← NonDet.assertOpt owner + let s ← getThreadState t + let ct ← NonDet.gets fun σ : State Unit => σ.curThread + if t = ct then rescheduleRequired + else NonDet.when (runnable s) (possibleSwitchTo t) + +private theorem cfgBindPureA {X Y : Type} (a : X) (f : X → SMonad Unit Y) : + (NonDet.pure a >>= f) = f a := funext (nd_pure_bindSc a f) + +private theorem cfgPreBoundEqA (p t : ObjRef) : + cfgPreOptionA p (some t) = cfgPrepareA t p := by + unfold cfgPreOptionA NonDet.when + rw [if_pos (Option.some_ne_none t), NonDet.assertOpt, cfgBindPureA] + +private theorem cfgResumeBoundEqA (p t : ObjRef) : + cfgResumeOptionA p (some t) = cfgResumeBoundA p t := by + unfold cfgResumeOptionA NonDet.when + rw [if_pos (Option.some_ne_none t)] + simp only [NonDet.assertOpt, cfgBindPureA, cfgResumeBoundA, NonDet.when] + +private theorem cfgInvokePhasesA {σ σ1 σ2 σ3 σ4 : State Unit} {p : ObjRef} {x y1 : SchedContext} + {n : Nat} (hk : σ.kheap p = some (.schedContext x n)) + (hs1 : σ1.kheap p = some (.schedContext y1 n)) (b per : Ticks) + (num : Nat) (badge flags : U64) + (h1 : cfgPreOptionA p x.scTcb σ = ({((), σ1)}, False)) + (h2 : cfgRefillSelectA p x.scTcb b per num σ1 = ({((), σ2)}, False)) + (h3 : cfgResumeOptionA p x.scTcb σ2 = ({((), σ3)}, False)) + (h4 : (do + setScObjRef (Z := Unit) scBadgeUpdate p badge.bv + setScObjRef scSporadicUpdate p (testBit flags.bv scSporadicFlag)) σ3 = ({((), σ4)}, False)) + (ht : ∀ t, x.scTcb = some t → ∃ z, σ1.kheap t = some (.tcb z)) : + invokeFl4SchedControlConfigureFlags (.invokeSchedControlConfigureFlags p b per num badge.bv flags.bv) σ = + ({((), σ4)}, False) := by + have tail : ((do + cfgResumeOptionA p x.scTcb + setScObjRef scBadgeUpdate p badge.bv + setScObjRef scSporadicUpdate p (testBit flags.bv scSporadicFlag)) : SMonad Unit Unit) σ2 = + ({((), σ4)}, False) := by + rw [nd_bind_oneSc h3] + exact h4 + unfold invokeFl4SchedControlConfigureFlags + dsimp only + rw [nd_bind_oneSc (getSchedContext_eqA hk)] + unfold cfgPreOptionA cfgPrepareA at h1 + rw [nd_bind_oneSc h1] + unfold cfgRefillSelectA at h2 + unfold cfgResumeOptionA at tail + by_cases e : per = b + · rw [if_pos e] at h2 ⊢ + rw [nd_bind_oneSc h2] + exact tail + · rw [if_neg e] at h2 ⊢ + rw [nd_bind_oneSc (getScActive_eqA hs1)] at h2 ⊢ + split_ifs at h2 ⊢ with h + · obtain ⟨t, he⟩ := Option.ne_none_iff_exists'.1 h.2 + obtain ⟨z, hz⟩ := ht t he + rw [he, nd_bind_oneSc (show NonDet.assertOpt (some t) σ1 = ({(t, σ1)}, False) from rfl)] at h2 ⊢ + unfold getThreadState at h2 ⊢ + rw [nd_bind_oneSc (threadGet_eqSc Tcb.tcbState hz)] at h2 ⊢ + split_ifs at h2 ⊢ <;> rw [nd_bind_oneSc h2] <;> rw [he] at tail <;> exact tail + · rw [nd_bind_oneSc h2] + exact tail + +private theorem cfgDoneFieldsA {σ : State Unit} {p : ObjRef} {x : SchedContext} {n : Nat} + (hk : σ.kheap p = some (.schedContext x n)) (badge flags : U64) : + (do + setScObjRef (Z := Unit) scBadgeUpdate p badge.bv + setScObjRef scSporadicUpdate p (testBit flags.bv scSporadicFlag)) σ = + ({((), updKSc σ p (.schedContext + { x with scBadge := badge.bv, scSporadic := testBit flags.bv scSporadicFlag } n))}, False) := by + dsimp only [setScObjRef] + rw [nd_bind_oneSc (updateSchedContext_eqA hk _), + updateSchedContext_eqA (updKSc_same _ _ _) _, updKSc_updKSc] + rfl + +/-- [J]/[S] A bound ConfigureFlags owner has a reciprocal binding on this +core, and the approved local stall leaves both states unchanged. -/ +def CfgOwnerLocalA (Z : XSetsA) (m : McState) (s : S) (a : A) (σ : State Unit) + (sc : U64) : Prop := + let t := L.mem s (scTcbA sc) + t ≠ 0#u64 → t ∈ Z.T ∧ L.mem s (affA t) = L.cpu s ∧ + L.mem s (scCoreA sc) = L.cpu s ∧ + (∀ z, σ.kheap (specPtr t) = some (.tcb z) → z.tcbSchedContext = some (scNameA m sc)) ∧ + smp.remote_tcb_stall st ar s a t = ok (s, a) + +set_option maxHeartbeats 1600000 in +set_option maxRecDepth 4096 in +theorem cfg_configure_flagsA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRA st Z m κ s σ) (S0 : StEqA Z.T (L.mem s) σ) + (M : McpRelA Z.T m (L.mem s) σ) (Y : YieldRelA Z.T Z.C m (L.mem s) σ) + (I : CurInvA σ) (O : OffOKA σ) (R : OffReadyA σ) (Ac : ActIdleA σ) (a : A) + (hAgree : fl4MinBudget = MIN_BUDGET) (hT : ScCfgTimeAgreeA ar a) + {sc cpu : U64} (hc : sc ∈ Z.C) (hnz : sc ≠ 0#u64) (hcpu : cpu = L.cpu s) + (hni : NotIdleScA m (scNameA m sc)) (hLocal : CfgOwnerLocalA (st := st) (ar := ar) Z m s a σ sc) + {x : SchedContext} {n : Nat} (hk : σ.kheap (scNameA m sc) = some (.schedContext x n)) + (budget period num badge flags : U64) + (hb : fl4MinBudget ≤ budget.bv) (hbp : budget.val ≤ period.val) + (h2 : 2 ≤ num.val) (hm : num.val ≤ absMax (n + minSchedContextBits)) : + schedcontext.configure_flags st ar s a sc cpu budget period num badge flags ⦃ r => + r.2 = a ∧ ∃ τ Q' R', + invokeFl4SchedControlConfigureFlags + (.invokeSchedControlConfigureFlags (scNameA m sc) budget.bv period.bv num.val badge.bv flags.bv) + σ = ({((), τ)}, False) ∧ + SchedSRA st {Z with Q := Q', R := R'} m (Function.update κ sc cpu) r.1 τ ∧ + StEqA Z.T (L.mem r.1) τ ∧ McpRelA Z.T m (L.mem r.1) τ ∧ YieldRelA Z.T Z.C m (L.mem r.1) τ ∧ + CurInvA τ ∧ OffOKA τ ∧ OffReadyA τ ∧ ActIdleA τ ∧ τ.curThread = σ.curThread ∧ τ.curSc = σ.curSc ∧ + MFrA (st := st) Z m s r.1 σ τ ⦄ := by + let p := scNameA m sc + let t := L.mem s (scTcbA sc) + let u := if t = 0#u64 then p else specPtr t + let k' := Function.update κ sc cpu + obtain ⟨x', n', h', R0⟩ := H.scr.sc sc hc + rw [hk] at h' + cases h' + have hOwn : x.scTcb = optRefS t := by rw [R0.tcb, ← scTcbA_eqSc] + have hBind : ∀ v z, x.scTcb = some v → σ.kheap v = some (.tcb z) → z.tcbSchedContext = some p := by + intro v z hv hz + by_cases h0 : t = 0#u64 + · rw [hOwn, h0, optRefS_zero] at hv; cases hv + · rw [hOwn, optRefS_ne h0] at hv + cases hv + exact (hLocal h0).2.2.2.1 z hz + unfold schedcontext.configure_flags + rw [eq_of_spec_fd (obj.Sc.tcb.spec s sc), bind_ok, eq_of_spec_fd (obj.Tcb.is_null.spec t), bind_ok] + have prep : + (if decide (t = 0#u64) then ok (s, a) else do + let (s2, a2) ← smp.remote_tcb_stall st ar s a t + let s3 ← sched.release_remove st s2 t + let s4 ← sched.dequeue st s3 t + let s5 ← sched.cur_sc st s4 + let b ← obj.Sc.Insts.CoreCmpPartialEqSc.eq s5 sc + let s6 ← if b then sched.commit_time st ar s4 a2 else ok s4 + ok (s6, a2)) ⦃ r => r.2 = a ∧ ∃ σ' Q' R' y, + cfgPreOptionA p x.scTcb σ = ({((), σ')}, False) ∧ + SchedSRB st {Z with Q := Q', R := R'} m κ r.1 σ' u ∧ + σ'.kheap = (updKSc σ p (.schedContext y n)).kheap ∧ + y.scTcb = x.scTcb ∧ y.scYieldFrom = x.scYieldFrom ∧ + σ'.curSc = σ.curSc ∧ σ'.curThread = σ.curThread ∧ σ'.idleThread = σ.idleThread ∧ + σ'.curTime = σ.curTime ∧ σ'.schedulerAction = σ.schedulerAction ∧ + σ'.consumedTime = (if σ.curSc = p then 0 else σ.consumedTime) ∧ + (σ.curSc = p → refillReady σ.curTime (refillHd y) = true) ∧ + (refillReady σ.curTime (refillHd x) = true → refillReady σ.curTime (refillHd y) = true) ∧ + (∀ d p', u ∉ σ'.readyQueues d p') ∧ u ∉ σ'.releaseQueue ∧ + (∀ v ∈ σ'.releaseQueue, v ∈ σ.releaseQueue) ∧ MFrA (st := st) Z m s r.1 σ σ' ⦄ := by + by_cases h0 : t = 0#u64 + · have hx0 : x.scTcb = none := by rw [hOwn, h0, optRefS_zero] + have hu : u = p := if_pos h0 + have ne : σ.curSc ≠ p := by + intro e + obtain ⟨z, hz, hb, -⟩ := I.tcb + obtain ⟨y, j, hs, hy⟩ := H.inv.back _ _ _ hz hb + rw [e, hk] at hs + cases hs + rw [hx0] at hy + cases hy + rw [decide_eq_true h0, if_pos rfl, WP.spec_ok] + refine ⟨rfl, σ, Z.Q, Z.R, x, ?_, H.toB u, ?_, rfl, rfl, rfl, rfl, rfl, rfl, rfl, ?_, ?_, + id, ?_, ?_, fun _ h => h, MFrA.refl Z m s σ⟩ + · simp [hx0, cfgPreOptionA, NonDet.when]; rfl + · rw [updKSc_selfA hk] + · rw [if_neg ne] + · exact fun e => (ne e).elim + · intro d pr h + rw [hu] at h + obtain ⟨z, hz, -⟩ := H.run.1 d pr p h + rw [hk] at hz + cases hz + · intro h + rw [hu] at h + obtain ⟨z, hz, -⟩ := H.run.2 p h + rw [hk] at hz + cases hz + · obtain ⟨ht, hon, -, -, hstall⟩ := hLocal h0 + have hxT : x.scTcb = some (specPtr t) := hOwn.trans (optRefS_ne h0) + have hu : u = specPtr t := if_neg h0 + rw [decide_eq_false h0, if_neg Bool.false_ne_true] + refine WP.spec_mono (cfg_prepare_boundA H a hc ht hon hstall hk hxT hni I O R hAgree hT) ?_ + rintro r ⟨ea, σ', Q', R', y, e, H', hK, hy, hyf, hC, hCt, hI, hT', hA, hN, + hr, hReady, hQ, hR, hSub, w⟩ + refine ⟨ea, σ', Q', R', y, ?_, hu ▸ H', hK, hy, hyf, hC, hCt, hI, hT', hA, + hN, hr, hReady, fun d pr => hu ▸ hQ d pr, hu ▸ hR, hSub, w⟩ + · rw [hxT, cfgPreBoundEqA]; exact e + refine WP.spec_bind prep ?_ + rintro ⟨s1, a1⟩ ⟨ea1, σ1, Q1, R1, y1, ePrep, H1, hK1, hOwn1, hY1, hC1, hCt1, hI1, hTime1, hA1, + hUsed1, hCur1, hReady1, hQ1, hR1, hSub1, W1⟩ + change a1 = a at ea1 + subst a1 + simp only [uncurry_apply_pair] + have hs1 : σ1.kheap p = some (.schedContext y1 n) := hK1 ▸ updKSc_same _ _ _ + have ht1 : ∀ v z, σ1.kheap v = some (.tcb z) ↔ σ.kheap v = some (.tcb z) := by + intro v z + rw [hK1] + exact cfgTcbKeepA hk v z + have S1 := cfgKeepStatesA H.toSchedSRN S0 W1 (fun v z => (ht1 v z).1) + have hc1 : ∀ h : t ≠ 0#u64, t ∈ Z.T ∧ u = specPtr t ∧ L.mem s1 (scCoreA sc) = cpu := by + intro h + obtain ⟨ht, -, hco, -, -⟩ := hLocal h + exact ⟨ht, if_neg h, (H1.scCore _ hc).trans ((H.scCore _ hc).symm.trans (hco.trans hcpu.symm))⟩ + have hb' : MIN_BUDGET ≤ budget.bv := hAgree ▸ hb + refine WP.spec_bind (cfg_refill_select_refinesA H1 S1 hc hs1 hni t cpu budget period num + (hOwn1.trans hOwn) hc1 hR1 hb' hbp h2 hm) ?_ + rintro s2 ⟨y2, eSel, H2, hOwn2, hY2, ha2, ready2, w2⟩ + let σ2 := updKSc σ1 p (.schedContext y2 n) + have hs2 : σ2.kheap p = some (.schedContext y2 n) := updKSc_same _ _ _ + have ht2 (v z) : σ2.kheap v = some (.tcb z) ↔ σ.kheap v = some (.tcb z) := + (cfgTcbKeepA hs1 v z).trans (ht1 v z) + have W2 : MFrA (st := st) {Z with Q := Q1, R := R1} m s1 s2 σ1 σ2 := cfgScFrameA hc hs1 w2 + have W12 := W1.trans W2 rfl rfl + have ho2 : y2.scTcb = none ∨ y2.scTcb = some u := by + rw [hOwn2, hOwn1, hOwn] + by_cases h0 : t = 0#u64 + · left; rw [h0, optRefS_zero] + · right; rw [optRefS_ne h0, (hc1 h0).2.1] + refine WP.spec_bind (cfg_set_coreA H2 hc hs2 ha2 hni ho2 hR1 cpu) ?_ + rintro s3 ⟨H3, w3⟩ + have W3 : MFrA (st := st) {Z with Q := Q1, R := R1} m s2 s3 σ2 σ2 := by + have F : MFrA (st := st) {Z with Q := Q1, R := R1} m s2 s3 σ2 (updKSc σ2 p (.schedContext y2 n)) := + cfgScFrameA (y := y2) hc hs2 w3 + rw [updKSc_selfA hs2] at F + exact F + have W13 := W12.trans W3 rfl rfl + have S3 := cfgKeepStatesA H.toSchedSRN S0 W13 (fun v z => (ht2 v z).1) + have hcpu3 : cpu = L.cpu s3 := hcpu.trans W13.wr.2.1.symm + have hco3 : L.mem s3 (scCoreA sc) = L.cpu s3 := by + rw [H3.scCore _ hc, Function.update_self, hcpu3] + -- Restore schedulability on the bound arm before writing the two final fields. + have hResume : + (if decide (t = 0#u64) then ok (s3, a) else do + let (s5, a3) ← smp.migrate_tcb st ar s3 a t cpu + let (s6, a4) ← schedcontext.resume st ar s5 a3 sc + let i1 ← st.cur_cpu s6 + let (s7, a5) ← + if cpu = i1 then do + let run ← sched.is_runnable st s6 t + let s8 ← if run then do + let ct ← sched.cur_thread st s6 + let b ← core.cmp.PartialEq.ne.trait_default obj.Tcb.Insts.CoreCmpPartialEqTcb t ct + if b then sched.possible_switch_to st s6 t else ok s6 + else ok s6 + ok (s8, a4) + else do + let run ← sched.is_schedulable st s6 t + if run then + let s8 ← sched.enqueue st s6 t + smp.remote_reschedule st ar s8 a4 cpu + else + let s8 ← sched.set_reprogram_on st s6 cpu + smp.remote_reschedule st ar s8 a4 cpu + let ct ← sched.cur_thread st s7 + let b ← obj.Tcb.Insts.CoreCmpPartialEqTcb.eq t ct + let s8 ← if b then sched.reschedule_required st s7 else ok s7 + ok (s8, a5)) ⦃ r => r.2 = a ∧ ∃ σ3 Q3 R3, + cfgResumeOptionA p x.scTcb σ2 = ({((), σ3)}, False) ∧ + SchedSRA st {Z with Q := Q3, R := R3} m k' r.1 σ3 ∧ + σ3.kheap = σ2.kheap ∧ SFixA σ2 {σ3 with reprogramTimer := σ2.reprogramTimer} ∧ + (∀ v ∈ σ3.releaseQueue, v ∈ σ.releaseQueue ∨ x.scTcb = some v) ∧ + (∀ v, x.scTcb = some v → v ∈ σ3.releaseQueue → + refillReady σ.curTime (refillHd y2) ≠ true) ∧ + (∀ v, σ3.schedulerAction = .switchThread v → + σ.schedulerAction = .switchThread v ∨ ∃ z, σ.kheap v = some (.tcb z) ∧ + z.tcbSchedContext = some p ∧ runnable z.tcbState = true ∧ v ∉ σ3.releaseQueue) ∧ + MFrA (st := st) {Z with Q := Q1, R := R1} m s3 r.1 σ2 σ3 ⦄ := by + by_cases h0 : t = 0#u64 + · have hx0 : x.scTcb = none := by rw [hOwn, h0, optRefS_zero] + have hu : u = p := if_pos h0 + have rd : RelIfBoundSc σ2 u := by + intro v q z j hv + rw [hu, hs2] at hv; cases hv + rw [decide_eq_true h0, if_pos rfl, WP.spec_ok] + refine ⟨rfl, σ2, Q1, R1, ?_, H3.toA rd, rfl, SFixA.refl σ2, + (fun v hv => Or.inl (hSub1 v hv)), ?_, ?_, MFrA.refl _ _ _ _⟩ + · simp [hx0, cfgResumeOptionA, NonDet.when] + rfl + · intro v hv + rw [hx0] at hv; cases hv + · intro v hv + exact Or.inl (hA1.symm.trans hv) + · have hxT : x.scTcb = some (specPtr t) := hOwn.trans (optRefS_ne h0) + have hu : u = specPtr t := (hc1 h0).2.1 + have ht := (hc1 h0).1 + have H3' : SchedSRB st {Z with Q := Q1, R := R1} m k' s3 σ2 (specPtr t) := hu ▸ H3 + obtain ⟨z, hz, -⟩ := H3.sch.tcb t ht + have hz0 := (ht2 _ _).1 hz + have hzb := hBind _ _ hxT hz0 + have hon3 : L.mem s3 (affA t) = L.cpu s3 := by + rw [W13.wr.2.1, ← (hLocal h0).2.1] + exact UScalar.eq_of_val_eq (Option.some.inj ((H3.sch.aff t ht).symm.trans (H.sch.aff t ht))) + have hmb := hT.minBudget + simp only [hAgree] at hmb + rw [decide_eq_false h0, if_neg Bool.false_ne_true] + refine WP.spec_mono (cfg_resume_boundA H3' a hmb hAgree hc ht hnz hcpu3 hon3 hco3 hni hz hs2 hzb + (hOwn2.trans (hOwn1.trans hxT)) ha2 (fun d pr => hu ▸ hQ1 d pr) (hu ▸ hR1) S3 ?_) ?_ + · intro hNot _ _ + exact ready2 (Or.inr fun v hv hRun => by + have e : v = z := KernelObject.tcb.inj (Option.some.inj (((ht1 _ _).2 hz0).symm.trans hv)).symm + exact Bool.false_ne_true (hNot.symm.trans (e ▸ hRun))) + · rintro r ⟨ea, σ3, Q3, R3, eq, H4, hk3, fix, rel, late, act, W4⟩ + refine ⟨ea, σ3, Q3, R3, by rw [hxT, cfgResumeBoundEqA]; exact eq, H4, hk3, fix, ?_, ?_, ?_, W4⟩ + · intro v hv + rcases rel v hv with e | e + · exact Or.inl (hSub1 v e) + · exact Or.inr (hxT.trans (congrArg some e.symm)) + · intro v hv e + have h := Option.some.inj (hxT.symm.trans hv) + subst v + simpa only [show σ2.curTime = σ1.curTime from rfl, hTime1] using late e + · intro v hv + rcases act v hv with h | ⟨rfl, hr, hn⟩ + · exact Or.inl (hA1 ▸ h) + · exact Or.inr ⟨z, hz0, hzb, hr, hn⟩ + refine WP.spec_bind hResume ?_ + rintro ⟨s4, a2⟩ ⟨ea2, σ3, Q3, R3, eRes, H4, hK3, fix, hRel, hLate, hAct, W4⟩ + change a2 = a at ea2 + subst a2 + simp only [uncurry_apply_pair] + have hs3 : σ3.kheap p = some (.schedContext y2 n) := hK3 ▸ hs2 + have cfgTail : + (do + let s5 ← obj.Sc.set_badge st sc s4 badge + if flags &&& fl4_abi.consts.sc_flag.SPORADIC != 0#u64 then obj.Sc.set_sporadic st sc s5 1#u64 + else obj.Sc.set_sporadic st sc s5 0#u64) ⦃ s' => + let y := { y2 with scBadge := badge.bv, scSporadic := testBit flags.bv scSporadicFlag } + SchedSRA st {Z with Q := Q3, R := R3} m k' s' (updKSc σ3 p (.schedContext y n)) ∧ + WritesIn st s4 s' (ScObjA sc n) ⦄ := + cfg_badge_flagsA H4 hc hs3 ha2 badge flags + have eTail : ((do + let s5 ← obj.Sc.set_badge st sc s4 badge + let v ← lift (flags &&& fl4_abi.consts.sc_flag.SPORADIC) + if v != 0#u64 then + let s6 ← obj.Sc.set_sporadic st sc s5 1#u64 + ok (s6, a) + else + let s6 ← obj.Sc.set_sporadic st sc s5 0#u64 + ok (s6, a)) : Result (S × A)) = + (do + let s5 ← (do + let s6 ← obj.Sc.set_badge st sc s4 badge + if flags &&& fl4_abi.consts.sc_flag.SPORADIC != 0#u64 then obj.Sc.set_sporadic st sc s6 1#u64 + else obj.Sc.set_sporadic st sc s6 0#u64) + ok (s5, a)) := by + simp only [cfgResultAssocA, lift, bind_ok] + by_cases e : ((flags &&& fl4_abi.consts.sc_flag.SPORADIC) != 0#u64) = true + · simp only [if_pos e] + · simp only [if_neg e] + rw [eTail] + refine WP.spec_bind cfgTail ?_ + rintro s5 ⟨H5, w5⟩ + rw [WP.spec_ok] + let y := { y2 with scBadge := badge.bv, scSporadic := testBit flags.bv scSporadicFlag } + let τ := updKSc σ3 p (.schedContext y n) + have W5 : MFrA (st := st) {Z with Q := Q3, R := R3} m s4 s5 σ3 τ := cfgScFrameA hc hs3 w5 + have W := (W13.trans W4 rfl rfl).trans W5 rfl rfl + have hFinal : τ.kheap = (updKSc σ p (.schedContext y n)).kheap := by + simp only [τ, updKSc_kheap, hK3, σ2, hK1] + rw [Function.update_idem, Function.update_idem] + have tb (v z) : τ.kheap v = some (.tcb z) ↔ σ.kheap v = some (.tcb z) := by + rw [hFinal] + exact cfgTcbKeepA hk v z + have eInvoke : invokeFl4SchedControlConfigureFlags + (.invokeSchedControlConfigureFlags p budget.bv period.bv num.val badge.bv flags.bv) σ = + ({((), τ)}, False) := by + refine cfgInvokePhasesA hk hs1 budget.bv period.bv num.val badge flags ePrep + (by rw [← hOwn1]; exact eSel) eRes (cfgDoneFieldsA hs3 badge flags) ?_ + intro v hv + by_cases h0 : t = 0#u64 + · rw [hOwn, h0, optRefS_zero] at hv; cases hv + · rw [hOwn, optRefS_ne h0] at hv + cases hv + obtain ⟨z, hz, -⟩ := H1.sch.tcb t (hc1 h0).1 + exact ⟨z, hz⟩ + have mp := yieldFrameA M Y + (fun v hv => ⟨yieldWordKeepA H.toSchedSRN H.curT (W.wr.mono (fun _ e => Or.inl e)) hv (Or.inr (Or.inl rfl)), + yieldFlagsKeepA H.toSchedSRN H.curT (W.wr.mono (fun _ e => Or.inl e)) hv⟩) + (fun v hv => yieldWordKeepA H.toSchedSRN H.curT (W.wr.mono (fun _ e => Or.inl e)) hv (Or.inr (Or.inr rfl))) + (fun v _ z hz => ⟨z, (tb _ _).2 hz, rfl, rfl, rfl⟩) (by + intro c cc + obtain ⟨z, j, hz, -⟩ := H.scr.sc c cc + by_cases e : scNameA m c = p + · rw [e, hk] at hz + cases hz + refine ⟨x, n, y, by rw [e]; exact hk, ?_, hY2.trans hY1⟩ + rw [e, hFinal] + exact updKSc_same _ _ _ + · refine ⟨z, j, z, hz, ?_, rfl⟩ + rw [hFinal, updKSc_kheap, Function.update_of_ne e] + exact hz) + have timing := cfgTimingAfterA H I O R Ac hk hFinal hBind hni.1 ha2 + (fix.curThread.trans hCt1) (fix.idleThread.trans hI1) (fix.curSc.trans hC1) + (fix.curTime.trans hTime1) (fix.consumedTime.trans hUsed1) + (fun e => by rw [← hTime1]; exact ready2 (Or.inl (by rw [hTime1]; exact hCur1 e))) + (fun e => by rw [← hTime1]; exact ready2 (Or.inl (by rw [hTime1]; exact hReady1 e))) + hRel hLate hAct + exact ⟨rfl, τ, Q3, R3, eInvoke, H5, cfgKeepStatesA H.toSchedSRN S0 W (fun v z => (tb v z).1), + mp.1, mp.2, timing.1, timing.2.1, timing.2.2.1, timing.2.2.2, fix.curThread.trans hCt1, fix.curSc.trans hC1, W⟩ + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScCfgSelectA.lean b/proofs/Fl4ProofsA64/P3/ScCfgSelectA.lean new file mode 100644 index 000000000..c5891c229 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScCfgSelectA.lean @@ -0,0 +1,185 @@ +import Fl4ProofsA64.P3.ScCfgRingA + +/-! ConfigureFlags selects a round-robin reset, a periodic update, or a +periodic reset. The current SC's post-update head remains ready. -/ + +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +def cfgRefillSelectA (p : ObjRef) (owner : Option ObjRef) (b per : Ticks) (num : Nat) : + SMonad Unit Unit := + if per = b then refillNew p MIN_REFILLS b 0 + else do + let active ← getScActive p + if active ∧ owner ≠ none then + let t ← NonDet.assertOpt owner + let sts ← getThreadState t + if runnable sts then refillUpdate p per b num else refillNew p num b per + else refillNew p num b per + +private theorem cfgReadyAtNowA (r : Refill) (now : Ticks) : + refillReady now { r with rTime := now } = true := by + change decide (now + (0 : Ticks) ≤ now) = true + apply decide_eq_true + bv_omega + +private theorem cfgNewReadyA (x : SchedContext) (now b p : Ticks) (num : Nat) : + refillReady now (refillHd (cfgNewScA x now num b p)) = true := by + unfold cfgNewScA cfgNewRefillsA + split <;> exact cfgReadyAtNowA ⟨now, b⟩ now + +private theorem cfgUpdateReadyA (x : SchedContext) (now p b : Ticks) (num : Nat) + (h : refillReady now (refillHd x) = true) : + refillReady now (refillHd (cfgUpdatedScA x now p b num)) = true := by + unfold cfgUpdatedScA cfgUpdatedRefillsA cfgUpdateHeadA + rw [if_pos h] + unfold cfgUpdateScA cfgUpdateListA + split + · split <;> exact cfgReadyAtNowA ⟨now, b⟩ now + · exact cfgReadyAtNowA (refillHd x) now + +set_option maxHeartbeats 1000000 in +theorem cfg_refill_select_refinesA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + {u : ObjRef} (H : SchedSRB st Z m κ s σ u) (S0 : StEqA Z.T (L.mem s) σ) + {sc : U64} (hc : sc ∈ Z.C) {x : SchedContext} {n : Nat} + (hk : σ.kheap (scNameA m sc) = some (.schedContext x n)) (hni : NotIdleScA m (scNameA m sc)) + (t cpu budget period num : U64) (howner : x.scTcb = optRefS t) + (ht : t ≠ 0#u64 → t ∈ Z.T ∧ u = specPtr t ∧ L.mem s (scCoreA sc) = cpu) + (hnr : u ∉ σ.releaseQueue) + (hb : MIN_BUDGET ≤ budget.bv) (hbp : budget.val ≤ period.val) + (h2 : 2 ≤ num.val) (hm : num.val ≤ absMax (n + minSchedContextBits)) : + (if budget = period then sporadic.refill_new st s sc sporadic.MIN_REFILLS budget 0#u64 + else do + let c ← obj.Sc.cpu st sc s + if cpu = c then + let active ← sporadic.sc_active st s sc + if active then + if decide (t = 0#u64) then sporadic.refill_new st s sc num budget period + else do + let run ← sched.is_runnable st s t + if run then sporadic.refill_update st s sc period budget num + else sporadic.refill_new st s sc num budget period + else sporadic.refill_new st s sc num budget period + else sporadic.refill_new st s sc num budget period) ⦃ s' => ∃ y, + let σ' := updKSc σ (scNameA m sc) (.schedContext y n) + cfgRefillSelectA (scNameA m sc) x.scTcb budget.bv period.bv num.val σ = ({((), σ')}, False) ∧ + SchedSRB st Z m κ s' σ' u ∧ + y.scTcb = x.scTcb ∧ y.scYieldFrom = x.scYieldFrom ∧ scActive y = true ∧ + ((refillReady σ.curTime (refillHd x) = true ∨ + (∀ xt, σ.kheap (specPtr t) = some (.tcb xt) → runnable xt.tcbState ≠ true)) → + refillReady σ.curTime (refillHd y) = true) ∧ + WritesIn st s s' (ScObjA sc n) ⦄ := by + let p := scNameA m sc + have ho : x.scTcb = none ∨ x.scTcb = some u := by + by_cases e : t = 0#u64 + · left; rw [howner, e, optRefS_zero] + · right; rw [howner, optRefS_ne e, (ht e).2.1] + let post (s' : S) := ∃ y, + let σ' := updKSc σ p (.schedContext y n) + cfgRefillSelectA p x.scTcb budget.bv period.bv num.val σ = ({((), σ')}, False) ∧ + SchedSRB st Z m κ s' σ' u ∧ + y.scTcb = x.scTcb ∧ y.scYieldFrom = x.scYieldFrom ∧ scActive y = true ∧ + ((refillReady σ.curTime (refillHd x) = true ∨ + (∀ xt, σ.kheap (specPtr t) = some (.tcb xt) → runnable xt.tcbState ≠ true)) → + refillReady σ.curTime (refillHd y) = true) ∧ + WritesIn st s s' (ScObjA sc n) + have fresh (c per : U64) (hn2 : 2 ≤ c.val) (hm : c.val ≤ absMax (n + minSchedContextBits)) + (h : cfgRefillSelectA p x.scTcb budget.bv period.bv num.val σ = refillNew (Z := Unit) p c.val budget.bv per.bv σ) : + sporadic.refill_new st s sc c budget per ⦃ s' => post s' ⦄ := by + refine WP.spec_mono (cfg_refill_new_refinesA H hc hk ho hnr c budget per hn2 hm hb) ?_ + rintro s' ⟨Hs, w⟩ + exact ⟨_, h.trans (cfgRefillNew_eqA hk c.val budget.bv per.bv), Hs, rfl, rfl, + decide_eq_true (show 0 < c.val by omega), fun _ => cfgNewReadyA x σ.curTime budget.bv per.bv c.val, w⟩ + by_cases he : budget = period + · rw [if_pos he] + have hn : sporadic.MIN_REFILLS.val = 2 := by unfold sporadic.MIN_REFILLS; rfl + refine fresh _ _ (by rw [hn]) (by rw [hn]; omega) ?_ + unfold cfgRefillSelectA + rw [if_pos (by rw [he]), hn] + rfl + · rw [if_neg he] + have hne : period.bv ≠ budget.bv := fun hp => he (eq_of_bv hp.symm) + have hp : period.bv ≠ 0 := by + intro hp + rw [BitVec.le_def] at hb + have hm0 : (0 : Ticks) < Fl4SpecA64.MIN_BUDGET := Fl4SpecA64.MIN_BUDGET_pos' + rw [BitVec.lt_def] at hm0 + change 0 < Fl4SpecA64.MIN_BUDGET.toNat at hm0 + have ep : period.val = period.bv.toNat := rfl + have eb : budget.val = budget.bv.toNat := rfl + rw [ep, hp, eb] at hbp + change budget.bv.toNat ≤ 0 at hbp + omega + have spec : cfgRefillSelectA p x.scTcb budget.bv period.bv num.val σ = + (if scActive x ∧ x.scTcb ≠ none then + do + let t ← NonDet.assertOpt x.scTcb + let sts ← getThreadState t + if runnable sts then refillUpdate (Z := Unit) p period.bv budget.bv num.val else refillNew p num.val budget.bv period.bv + else refillNew (Z := Unit) p num.val budget.bv period.bv) σ := by + unfold cfgRefillSelectA + rw [if_neg hne, nd_bind_oneSc (getScActive_eqA hk)] + rw [eq_of_spec_fd (obj.Sc.cpu.spec s sc), bind_ok] + by_cases h0 : t = 0#u64 + · have hx0 : x.scTcb = none := by rw [howner, h0, optRefS_zero] + have sf : cfgRefillSelectA p x.scTcb budget.bv period.bv num.val σ = + refillNew (Z := Unit) p num.val budget.bv period.bv σ := by + rw [spec, if_neg (fun e => e.2 hx0)] + by_cases e : cpu = L.mem s (scCoreA sc) + · rw [if_pos e] + refine WP.spec_bind (sc_active_refinesA H.scr hc hk) ?_ + intro b _ + have hnull : decide (t = 0#u64) = true := decide_eq_true h0 + cases b <;> simp only [hnull, Bool.false_eq_true, ↓reduceIte] <;> exact fresh _ _ h2 hm sf + · rw [if_neg e]; exact fresh _ _ h2 hm sf + · obtain ⟨htT, -, hcore⟩ := ht h0 + have hxT : x.scTcb = some (specPtr t) := by rw [howner, optRefS_ne h0] + obtain ⟨xt, hkt, -⟩ := H.sch.tcb t htT + rw [if_pos hcore.symm] + refine WP.spec_bind (sc_active_refinesA H.scr hc hk) ?_ + intro b eb + rw [getScActive_eqA hk] at eb + have e : scActive x = b := (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst eb))).1 + by_cases ha : scActive x = true + · rw [ha] at e + rw [← e, decide_eq_false h0] + simp only [Bool.false_eq_true, ↓reduceIte] + have sf := spec + rw [if_pos ⟨ha, by rw [hxT]; simp⟩, hxT, nd_bind_oneSc (show NonDet.assertOpt (some (specPtr t)) σ = _ from rfl)] + at sf + unfold getThreadState at sf + rw [nd_bind_oneSc (threadGet_eqSc Tcb.tcbState hkt)] at sf + have eRun := eq_of_spec_fd (sched.is_runnable.spec (L := L) s t) + rw [eRun, (decide_eq_decide.2 (runnable_iffA (S0 t htT xt hkt))), bind_ok] + by_cases hr : runnable xt.tcbState = true + · rw [show decide (runnable xt.tcbState = true) = true from decide_eq_true hr, if_pos rfl] + have oldMin : MIN_BUDGET ≤ (refillHd x).rAmount := by + by_cases hp' : x.scPeriod = 0 + · exact ((H.inv.rr _ _ _ hk ha hp').2 hni).2.2 + · exact (H.inv.refills _ _ _ hk ha hp').2.2.2 + refine WP.spec_mono (cfg_refill_update_refinesA H hc hk ha ho hnr period budget num h2 hm hp hb oldMin) ?_ + rintro s' ⟨Hs, w⟩ + rw [if_pos hr] at sf + refine ⟨_, by rw [hxT]; exact sf.trans (cfgRefillUpdate_eqA hk period.bv budget.bv num.val), Hs, rfl, rfl, + decide_eq_true (show 0 < num.val by omega), ?_, w⟩ + rintro (h | h) + · exact cfgUpdateReadyA x σ.curTime period.bv budget.bv num.val h + · exact (h xt hkt hr).elim + · rw [decide_eq_false hr] + simp only [Bool.false_eq_true, ↓reduceIte] + rw [if_neg hr] at sf + exact fresh _ _ h2 hm (by rw [hxT]; exact sf) + · have hfalse : scActive x = false := Bool.eq_false_iff.mpr ha + rw [← e, hfalse] + simp only [Bool.false_eq_true, ↓reduceIte] + exact fresh _ _ h2 hm (spec.trans (by rw [if_neg (fun h : scActive x = true ∧ x.scTcb ≠ none => ha h.1)])) + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScCfgStateA.lean b/proofs/Fl4ProofsA64/P3/ScCfgStateA.lean new file mode 100644 index 000000000..93e6e2c40 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScCfgStateA.lean @@ -0,0 +1,268 @@ +import Fl4ProofsA64.P3.ScCfgRefillA +import Fl4ProofsA64.P3.ScSporA +import Fl4ProofsA64.P3.ScPostA + +/-! +Reconfiguring an SC after removing its owner from the release queue. +The temporary scheduler relation relaxes refill readiness at that owner, +as in the donation block. The caller restores readiness through resume. +-/ + +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3.Sched + +open Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4SpecA64 Fl4Spec + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +theorem cfgTcbKeepA {σ : State Unit} {p : ObjRef} {x y : SchedContext} {n : Nat} + (h : σ.kheap p = some (.schedContext x n)) (q : ObjRef) (t : Tcb) : + (updKSc σ p (.schedContext y n)).kheap q = some (.tcb t) ↔ σ.kheap q = some (.tcb t) := by + by_cases e : q = p + · subst e; simp [updKSc, h] + · simp [updKSc, e] + +/-- Outside the removed owner, replacing the SC's period and ring keeps the +release-order and readiness obligations. The new ring satisfies the SC +invariant for its new period. -/ +theorem cfgScInvA {σ : State Unit} {m : McState} (H : ScInvA m σ) (u : ObjRef) + {p : ObjRef} {x y : SchedContext} {n : Nat} (hk : σ.kheap p = some (.schedContext x n)) + (ht : y.scTcb = x.scTcb) (ho : x.scTcb = none ∨ x.scTcb = some u) (hr : u ∉ σ.releaseQueue) + (hper : y.scPeriod ≠ 0 → RefOKA y) (hrr : y.scPeriod = 0 → RRokA y) : + ScInvA m (updKSc σ p (.schedContext y n)) := by + let σ' := updKSc σ p (.schedContext y n) + have noOwner {v : ObjRef} (hv : v ∈ σ.releaseQueue) (e : x.scTcb = some v) : False := by + rcases ho with h | h + · rw [h] at e; cases e + · exact hr ((Option.some.inj (h.symm.trans e)).symm ▸ hv) + have relKeep (v : ObjRef) (hv : v ∈ σ.releaseQueue) : + ∃ t q z k, σ.kheap v = some (.tcb t) ∧ t.tcbSchedContext = some q ∧ + σ.kheap q = some (.schedContext z k) ∧ scActive z = true ∧ q ≠ p := by + obtain ⟨t, q, z, k, ht, hsc, hobj, ha⟩ := H.relSc v hv + refine ⟨t, q, z, k, ht, hsc, hobj, ha, ?_⟩ + intro he + obtain ⟨z', k', hobj', hback⟩ := H.back v t q ht hsc + rw [he, hk] at hobj'; cases hobj' + exact noOwner hv hback + have nextAt (v : ObjRef) (hv : v ∈ σ.releaseQueue) : specRtA σ' v = specRtA σ v := by + obtain ⟨t, q, z, k, ht, hsc, hobj, ha, he⟩ := relKeep v hv + have hne := H.ne hobj ha + rw [specRtA_of ht hsc hobj hne] + exact specRtA_of ((cfgTcbKeepA (y := y) hk _ _).2 ht) hsc (by simpa [σ', updKSc, he] using hobj) hne + refine ⟨?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_⟩ + · intro q z k hz ha hp + by_cases e : q = p + · subst e + simp only [σ', updKSc, Function.update_self, Option.some.injEq, KernelObject.schedContext.injEq] at hz + obtain ⟨rfl, rfl⟩ := hz + exact hper hp + · exact H.refills q z k (by simpa [σ', updKSc, e] using hz) ha hp + · intro q z k hz ha hp + by_cases e : q = p + · subst e + simp only [σ', updKSc, Function.update_self, Option.some.injEq, KernelObject.schedContext.injEq] at hz + obtain ⟨rfl, rfl⟩ := hz + have h := hrr hp + refine ⟨?_, fun _ => h⟩ + intro e + have := h.1 + rw [e] at this; simp at this + · exact H.rr q z k (by simpa [σ', updKSc, e] using hz) ha hp + · intro q z hz + apply H.ntfnIdle q z + by_cases e : q = p + · subst e; simp [σ', updKSc] at hz + · simpa [σ', updKSc, e] using hz + · intro q z hz + apply H.ntfnIdleOf q z + by_cases e : q = p + · subst e; simp [σ', updKSc] at hz + · simpa [σ', updKSc, e] using hz + · intro v hv + obtain ⟨t, q, z, k, ht, hsc, hobj, ha, he⟩ := relKeep v hv + exact ⟨t, q, z, k, (cfgTcbKeepA hk _ _).2 ht, hsc, by simpa [σ', updKSc, he] using hobj, ha⟩ + · refine List.Pairwise.imp_of_mem (fun {a b} ha hb hab => ?_) H.sorted + rw [nextAt a ha, nextAt b hb]; exact hab + · intro q t v hq hv + obtain ⟨z, k, hz, hb⟩ := H.back q t v ((cfgTcbKeepA hk _ _).1 hq) hv + by_cases e : v = p + · subst e + rw [hk] at hz; cases hz + exact ⟨y, n, updKSc_same _ _ _, ht.trans hb⟩ + · exact ⟨z, k, by simpa [σ', updKSc, e] using hz, hb⟩ + · intro q t v hq hv + exact H.tcbScNotIdle q t v ((cfgTcbKeepA hk _ _).1 hq) hv + +theorem cfgRdExA {σ : State Unit} {m : McState} (H : ScInvA m σ) {u p : ObjRef} + (R : RdExA σ u) {x y : SchedContext} {n : Nat} (hk : σ.kheap p = some (.schedContext x n)) + (ho : x.scTcb = none ∨ x.scTcb = some u) : + RdExA (updKSc σ p (.schedContext y n)) u := by + intro v t hv ht hcl z q sc' k hz hsc hobj ha hr + have htn := (cfgTcbKeepA hk _ _).1 ht + have hzn := (cfgTcbKeepA hk _ _).1 hz + by_cases e : q = p + · subst q + obtain ⟨w, i, h, hb⟩ := H.back v z p hzn hsc + rw [hk] at h; cases h + rcases ho with h0 | h1 + · rw [h0] at hb; cases hb + · exact (hv (Option.some.inj (hb.symm.trans h1))).elim + · apply R v t hv htn hcl z q sc' k hzn hsc (by simpa [updKSc, e] using hobj) ha hr + +theorem SchedSRB.scReconfCoreA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s s' : S} {σ : State Unit} {u : ObjRef} + (H : SchedSRB st Z m κ s σ u) {sc : U64} (hc : sc ∈ Z.C) {x y : SchedContext} {n : Nat} + (hk : σ.kheap (scNameA m sc) = some (.schedContext x n)) + (hw : WritesIn st s s' (ScObjA sc n)) (hy : ScRelWA st (IdleScA m sc) s' sc y n) + (ht : y.scTcb = x.scTcb) (ho : x.scTcb = none ∨ x.scTcb = some u) (hr : u ∉ σ.releaseQueue) + (hper : y.scPeriod ≠ 0 → RefOKA y) (hrr : y.scPeriod = 0 → RRokA y) + (v : U64) (hcc : L.mem s' (scCoreA sc) = v) : + SchedSRB st Z m (Function.update κ sc v) s' (updKSc σ (scNameA m sc) (.schedContext y n)) u := by + let σ' := updKSc σ (scNameA m sc) (.schedContext y n) + have hcW : ∀ b, ScObjA sc n b → ScFootA Z.C m σ b := fun b h => ⟨sc, hc, x, n, hk, h⟩ + have hkT : ∀ q t, σ.kheap q = some (.tcb t) → σ'.kheap q = some (.tcb t) := + fun q t h => (cfgTcbKeepA hk q t).2 h + have n56 : layout.node.REPROGRAM.val = 56 := by unfold layout.node.REPROGRAM; rfl + have n16 : layout.node.SCHED_ACTION.val = 16 := by unfold layout.node.SCHED_ACTION; rfl + obtain ⟨nK, gK, tK⟩ := H.keepW hw (o0 := layout.node.REPROGRAM) (by rw [n56]; decide) + (fun b h => Or.inl (hcW b h)) + have tw (t : U64) (ht : t ∈ Z.T) (K : U64) + (h : K = 3520#u64 ∨ K = 3448#u64 ∨ K = 3432#u64 ∨ K = 3464#u64 ∨ K = 3592#u64 ∨ K = 3480#u64 ∨ K = 3488#u64) : + L.mem s' (wa t K) = L.mem s (wa t K) := + tK t ht K (by rcases h with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> simp) + (by rcases h with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> simp) + obtain ⟨hscS, hscP⟩ := H.scr.upd H.scp hc hk hw hy + have hcore : SchedCoreA st Z.T Z.Cs s' Z.Q Z.R := + H.core.agree hw.1 (fun t ht => ⟨tw t ht 3480#u64 (by simp), tw t ht 3488#u64 (by simp)⟩) + (fun c hc o ho h8 hne => nK c hc o ho h8 (by rw [n56]; omega)) + (fun t ht => tw t ht 3592#u64 (by simp)) (fun t ht => tw t ht 3520#u64 (by simp)) + (fun t ht => tw t ht 3448#u64 (by simp)) + have g := H.rebuildNodeG (κ' := Function.update κ sc v) (Q' := Z.Q) (R' := Z.R) (σ' := σ') hw.1 hw.2.1 + (fun c hc o h1 h2 _ _ h4 => nK c hc o (by omega) h2 (by rwa [n56])) + (fun K hK => gK K (by rcases hK with rfl | rfl <;> decide) (by rcases hK with rfl | rfl <;> decide)) + (fun t ht K hK => tw t ht K (by rcases hK with e | e | e <;> simp [e])) hscS hscP + (fun t _ x hx => ⟨x, hkT _ x hx, rfl, rfl⟩) + (fun t ht => by + obtain ⟨x, hx, e⟩ := H.sch.tcb t ht + rw [scA_eq, tw t ht 3464#u64 (by simp), ← scA_eq] + exact ⟨x, hkT _ x hx, e⟩) + (fun t ht => by rw [scA_eq, tw t ht 3464#u64 (by simp), ← scA_eq]; exact H.rest.scMem t ht) + ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ + (by + show σ.consumedTime = (L.mem s' (L.nodeAt s (L.cpu s) layout.node.CONSUMED)).bv + have e : layout.node.CONSUMED.val = 40 := by unfold layout.node.CONSUMED; rfl + rw [nK _ H.sch.cpuCs layout.node.CONSUMED (by rw [e] <;> decide) + (by rw [e] <;> decide) (by rw [e, n56] <;> decide)] + exact H.rest.consumed) + (by + show σ.reprogramTimer = _ + rw [hw.mem_eq H.reprA.1 (fun hb => H.reprA.2 _ (hcW _ hb) rfl)] + exact H.rest.repr) + (by + show σ.schedulerAction = _ + rw [nK _ H.sch.cpuCs layout.node.SCHED_ACTION (by rw [n16]; decide) + (by rw [n16] <;> decide) (by rw [n16, n56] <;> decide)] + exact H.sch.act) + H.actOn (cfgScInvA H.inv u hk ht ho hr hper hrr) hcore H.rest.rq H.rest.rqDom H.rest.rel + ⟨fun d p t h => let ⟨x, hx, hu⟩ := H.run.1 d p t h; ⟨x, hkT _ x hx, hu⟩, + fun t h => let ⟨x, hx, hu⟩ := H.run.2 t h; ⟨x, hkT _ x hx, hu⟩⟩ + (fun sc' hc' => by + by_cases e : sc' = sc + · subst e; rw [Function.update_self]; exact hcc + obtain ⟨x', k, hkx, hrel⟩ := H.scr.sc sc' hc' + have h16 := hrel.hdr (K := 16#u64) (by decide) (by decide) + have eq : scCoreA sc' = scWA sc' 16#u64 := by unfold scCoreA scWA layout.sc.CORE; rfl + rw [Function.update_of_ne e, ← H.scCore sc' hc'] + refine hw.mem_eq (H.scCoreFoot hc').1 (fun hb => ?_) + rw [eq] at hb + exact H.scp.disj sc' hc' sc hc e x' k x n hkx hk _ (hrel.in_obj h16.2) hb) + exact ⟨g, cfgRdExA H.inv H.rdEx hk ho⟩ + +theorem SchedSRB.scReconfA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s s' : S} {σ : State Unit} {u : ObjRef} + (H : SchedSRB st Z m κ s σ u) {sc : U64} (hc : sc ∈ Z.C) {x y : SchedContext} {n : Nat} + (hk : σ.kheap (scNameA m sc) = some (.schedContext x n)) + (hw : WritesIn st s s' (ScObjA sc n)) (hy : ScRelWA st (IdleScA m sc) s' sc y n) + (ht : y.scTcb = x.scTcb) (ho : x.scTcb = none ∨ x.scTcb = some u) (hr : u ∉ σ.releaseQueue) + (hper : y.scPeriod ≠ 0 → RefOKA y) (hrr : y.scPeriod = 0 → RRokA y) + (hcc : L.mem s' (scCoreA sc) = L.mem s (scCoreA sc)) : + SchedSRB st Z m κ s' (updKSc σ (scNameA m sc) (.schedContext y n)) u := by + have H' := H.scReconfCoreA hc hk hw hy ht ho hr hper hrr (κ sc) (hcc.trans (H.scCore sc hc)) + rw [Function.update_eq_self] at H' + exact H' + +/-- Clearing the current node's consumed-time word is a scheduler-only +write. The abstract update clears exactly the same field. -/ +theorem SchedSRB.clearConsumedA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {σ : State Unit} {u : ObjRef} (H : SchedSRB st Z m κ s σ u) : + let addr := L.nodeAt s (L.cpu s) layout.node.CONSUMED + let s' := L.wr s addr 0#u64 + SchedSRB st Z m κ s' { σ with consumedTime := 0 } u ∧ WritesIn st s s' (· = addr) := by + dsimp only + let addr := L.nodeAt s (L.cpu s) layout.node.CONSUMED + let s' := L.wr s addr 0#u64 + obtain ⟨nn, -, hn, hc⟩ := H.csN + have hcl := (hc _).1 H.sch.cpuCs + have c40 : layout.node.CONSUMED.val = 40 := by unfold layout.node.CONSUMED; rfl + have repr56 : layout.node.REPROGRAM.val = 56 := by unfold layout.node.REPROGRAM; rfl + have act16 : layout.node.SCHED_ACTION.val = 16 := by unfold layout.node.SCHED_ACTION; rfl + have he := nodeAt_valSc (s := s) H.ksFit (by omega : (L.cpu s).val < 128) + (by rw [c40]; decide : layout.node.CONSUMED.val < 4288) + have ka := H.ksb_al + have al : Aligned addr := by unfold addr Aligned; rw [he, c40]; omega + have nonSc : ¬ ScFootA Z.C m σ addr := by + intro hf + have hx := H.scp.ks_out hf + unfold addr at hx + rw [he, c40] at hx + unfold ksSizeA at hx + omega + have w : WritesIn st s s' (· = addr) := writesIn_wr al + obtain ⟨nK, gK, tK⟩ := H.keepW w (by rw [c40]; decide) (fun _ h => Or.inr h) + have scKeep : ∀ b, Aligned b → ScFootA Z.C m σ b → L.mem s' b = L.mem s b := + fun b hb hf => w.mem_eq hb (fun e => nonSc (e ▸ hf)) + have tw (t : U64) (ht : t ∈ Z.T) (K : U64) + (h : K = 3520#u64 ∨ K = 3448#u64 ∨ K = 3432#u64 ∨ K = 3464#u64 ∨ K = 3592#u64 ∨ K = 3480#u64 ∨ K = 3488#u64) : + L.mem s' (wa t K) = L.mem s (wa t K) := + tK t ht K (by rcases h with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> simp) + (by rcases h with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> simp) + have hcore : SchedCoreA st Z.T Z.Cs s' Z.Q Z.R := + H.core.agree w.1 (fun t ht => ⟨tw t ht 3480#u64 (by simp), tw t ht 3488#u64 (by simp)⟩) + (fun c hc o ho h8 hne => nK c hc o ho h8 (by rw [c40]; omega)) + (fun t ht => tw t ht 3592#u64 (by simp)) (fun t ht => tw t ht 3520#u64 (by simp)) + (fun t ht => tw t ht 3448#u64 (by simp)) + have g := H.rebuildNodeG (Q' := Z.Q) (R' := Z.R) (σ' := { σ with consumedTime := 0 }) w.1 w.2.1 + (fun c hc o h1 h2 _ h3 _ => nK c hc o (by omega) h2 (by rwa [c40])) + (fun K hK => gK K (by rcases hK with rfl | rfl <;> simp) (by rcases hK with rfl | rfl <;> simp)) + (fun t ht K hK => tw t ht K (by rcases hK with e | e | e <;> simp [e])) + (H.scr.frame scKeep (fun _ _ => rfl)) (ScPlacedA.of_kheap H.scp (fun _ _ => rfl)) + (fun t ht x hx => ⟨x, hx, rfl, rfl⟩) + (fun t ht => by + obtain ⟨x, hx, e⟩ := H.sch.tcb t ht + rw [scA_eq, tw t ht 3464#u64 (by simp), ← scA_eq] + exact ⟨x, hx, e⟩) + (fun t ht => by rw [scA_eq, tw t ht 3464#u64 (by simp), ← scA_eq]; exact H.rest.scMem t ht) + ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ + (by show (0 : Ticks) = (L.mem s' addr).bv; rw [L.mem_wr_same al]; rfl) + (by + show σ.reprogramTimer = _ + rw [nK _ H.sch.cpuCs layout.node.REPROGRAM (by rw [repr56] <;> decide) + (by rw [repr56] <;> decide) (by rw [repr56, c40] <;> decide)] + exact H.rest.repr) + (by + show σ.schedulerAction = _ + rw [nK _ H.sch.cpuCs layout.node.SCHED_ACTION (by rw [act16] <;> decide) + (by rw [act16] <;> decide) (by rw [act16, c40] <;> decide)] + exact H.sch.act) + H.actOn (H.inv.of_kheap rfl rfl) hcore H.rest.rq H.rest.rqDom H.rest.rel H.run + (fun c hc => (scKeep _ (H.scCoreFoot hc).1 (H.scCoreFoot hc).2).trans (H.scCore c hc)) + exact ⟨⟨g, H.rdEx⟩, w⟩ + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScCfgTopA.lean b/proofs/Fl4ProofsA64/P3/ScCfgTopA.lean new file mode 100644 index 000000000..f12ec6175 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScCfgTopA.lean @@ -0,0 +1,209 @@ +import Fl4ProofsA64.P3.ScCfgDecodeA +import Fl4ProofsA64.P3.ScCfgRunA +import Fl4ProofsA64.P3.ScYieldRunA +import Fl4ProofsA64.P3.ScIdleA + +/-! +ConfigureFlags at the scheduling-control decoder, including its Restart +write and the subsequent invocation. The [J] cap facts tie the decoded +object, its size, and an optional reciprocal owner to the scheduler view. +[S] The scheduling-control cap selects this core and the optional owner's +stall is inert at the point of the call. [P] The timer agrees with Ext. +-/ + +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3.Sched + +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +theorem cfgRestartTimingA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ σ' : State Unit} + (H : SchedSRA st Z m κ s σ) (I : CurInvA σ) (O : OffOKA σ) (R : OffReadyA σ) (Ac : ActIdleA σ) + {t : ObjRef} {x : Tcb} (ht : σ.curThread = t) (hk : σ.kheap t = some (.tcb x)) + (hr : runnable x.tcbState = true) + (h : setThreadState (Z := Unit) t .restart σ = ({((), σ')}, False)) : + σ' = updKSc σ t (.tcb { x with tcbState := .restart }) ∧ + CurInvA σ' ∧ OffOKA σ' ∧ OffReadyA σ' ∧ ActIdleA σ' := by + let τ := updKSc σ t (.tcb { x with tcbState := .restart }) + have keep (q : ObjRef) (hq : q ≠ t) : τ.kheap q = σ.kheap q := by + rw [show τ.kheap = _ from updKSc_kheap _ _ _, Function.update_of_ne hq] + obtain ⟨xt, hkt, hxb, -⟩ := I.tcb + rw [ht, hk] at hkt + cases hkt + obtain ⟨y, n, hy⟩ := I.sc + have hsct : σ.curSc ≠ t := fun e => by rw [e, hk] at hy; cases hy + obtain ⟨idle, hIdle, hiB, hiS, hiR⟩ := Ac.2 + have hit : σ.idleThread ≠ t := by + intro e + rw [e, hk] at hIdle + cases hIdle + rw [hiS] at hr + cases hr + have hci : σ.curSc ≠ idleScPtr := by + intro e + obtain ⟨z, j, hz, hzo⟩ := H.inv.back _ _ _ hIdle hiB + obtain ⟨z', j', hz', hzo'⟩ := H.inv.back _ _ _ hk hxb + rw [e, hz] at hz' + cases hz' + exact hit (Option.some.inj (hzo.symm.trans hzo')) + have ha := I.active hci _ _ hy + have hτ := cfgSchedulableA (σ := τ) (updKSc_same _ _ _) hxb ((keep _ hsct).trans hy) + (by rfl) ha (by change t ∉ σ.releaseQueue; rw [← ht]; exact I.notRel) + have eq : σ' = τ := by + rw [yieldSetStateSpecA hk] at h + have e := (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst h))).2 + exact e.symm.trans (stsActA_eq_self (fun a => a.2.2 hτ)) + refine ⟨eq, ?_⟩ + rw [eq] + have hSC (z j) (h : τ.kheap τ.curSc = some (.schedContext z j)) : + σ.kheap σ.curSc = some (.schedContext z j) := by simpa only [show τ.curSc = σ.curSc from rfl, keep _ hsct] using h + refine ⟨⟨⟨{ x with tcbState := .restart }, by rw [show τ.curThread = σ.curThread from rfl, ht]; exact updKSc_same _ _ _, + hxb, rfl⟩, ⟨y, n, (keep _ hsct).trans hy⟩, fun e z j hz => I.active e z j (hSC z j hz), I.notRel⟩, + O.congr rfl rfl rfl (keep _ hsct), fun z j hz => R z j (hSC z j hz), ?_, ?_⟩ + · intro u hu + by_cases eu : u = t + · simpa only [eu] using hτ + · have hOld := Ac.1 u hu + obtain ⟨z, p, hz, hb, -, -⟩ := H.actReady u hu hOld + obtain ⟨sc, j, hsc, -⟩ := H.inv.back u z p hz hb + have hpt : p ≠ t := fun e => by rw [e, hk] at hsc; cases hsc + have runs := schedulable_partsA hOld + have act : scActive sc = true := by + unfold schedulable at hOld + simp only [decide_eq_true_eq] at hOld + simpa [tcbsOfKh, scsOfKh, optPred, optMap, tcbOf, scOf, hz, hb, hsc] using hOld.2.1 + exact cfgSchedulableA ((keep _ eu).trans hz) hb ((keep _ hpt).trans hsc) (runs.1 _ hz) act runs.2 + · exact ⟨idle, (keep _ hit).trans hIdle, hiB, hiS, hiR⟩ + +/-- [J] The passed scheduling-context cap names a placed nonidle SC +of the decoded size, with the reverse link to a placed local owner. -/ +structure CfgCapTargetA (Z : XSetsA) (m : McState) (s : S) (σ : State Unit) (p sb : U64) : Prop where + mem : p ∈ Z.C + nonzero : p ≠ 0#u64 + notIdle : NotIdleScA m (scNameA m p) + size : ∃ x n, σ.kheap (scNameA m p) = some (.schedContext x n) ∧ sb.val = n + minSchedContextBits + owner : ∀ t, L.mem s (scTcbA p) = t → t ≠ 0#u64 → + t ∈ Z.T ∧ L.mem s (affA t) = L.cpu s ∧ L.mem s (scCoreA p) = L.cpu s ∧ + (∀ x, σ.kheap (specPtr t) = some (.tcb x) → x.tcbSchedContext = some (scNameA m p)) + +set_option maxHeartbeats 1600000 in +set_option maxRecDepth 4096 in +theorem cfg_decode_localA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : YieldQueueRelA (st := st) Z m κ s σ) + (I : CurInvA σ) (O : OffOKA σ) (R : OffReadyA σ) (Ac : ActIdleA σ) + (a : A) (hT : ScCfgTimeAgreeA ar a) (hAgree : fl4MinBudget = MIN_BUDGET) + (lbl : U64) (slbl : Data) + (hlbl : genInvocationType slbl = .schedControlConfigureFlags ↔ lbl = fl4_abi.label.SchedControlConfigureFlags) + (cpu length buf ct : U64) (hcpu : cpu = L.cpu s) (hct : sched.cur_thread st s = ok ct) + (hctT : ct ∈ Z.T) + (hr : ∀ x, σ.kheap (specPtr ct) = some (.tcb x) → runnable x.tcbState = true) + (args : List U64) (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s ct buf i = ok x) + (extra : Aeneas.Std.Array obj.Slot 3#usize) (x0 : U64) (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) + (excaps : List Cap) (hex : ExtraRelA Z.th (L.mem s) x0 excaps) + (hsb : x0 ≠ 0#u64 → ∀ p sb, capAtA (L.mem s) x0 = .SchedContext p sb → sb.val < 64) + (hCap : x0 ≠ 0#u64 → ∀ p sb, capAtA (L.mem s) x0 = .SchedContext p sb → + CfgCapTargetA (st := st) Z m s σ p sb) + (hStall : ∀ s1, sched.set_thread_state st s ct sched.TS_RESTART = ok s1 → + x0 ≠ 0#u64 → ∀ p sb, capAtA (L.mem s) x0 = .SchedContext p sb → + ∀ t, L.mem s1 (scTcbA p) = t → t ≠ 0#u64 → smp.remote_tcb_stall st ar s1 a t = ok (s1, a)) : + schedcontext.decode_sched_control st ar s a lbl cpu length buf extra ⦃ res => + ScCfgPostA σ (decodeFl4SchedControlInvocation (Z := Unit) slbl (args.map UScalar.bv) excaps) s a + (fun inv r => r.1 = .Ok () ∧ r.2.2 = a ∧ ∃ σ1 τ p Q' R', + setThreadState (Z := Unit) (specPtr ct) .restart σ = ({((), σ1)}, False) ∧ + invokeFl4SchedControlConfigureFlags inv σ1 = ({((), τ)}, False) ∧ + YieldQueueRelA (st := st) {Z with Q := Q', R := R'} m (Function.update κ p cpu) r.2.1 τ ∧ + CurInvA τ ∧ OffOKA τ ∧ OffReadyA τ ∧ ActIdleA τ ∧ + WritesIn st s r.2.1 (fun b => MWrA (st := st) Z m σ s b ∨ b = stA ct)) res ⦄ := by + refine cfg_decode_sched_controlA Z.th s a hT σ lbl slbl hlbl cpu length buf ct hct args hlen hargs extra x0 hx0 + excaps hex hsb _ ?_ + intro p sb bu pu er bd fl budget period hX hC _ _ _ _ _ hBu hPe hMb hBp hRef + obtain ⟨Hc, hp0, hni, ⟨y, n, hk, hSz⟩, hOwner⟩ := hCap hX p sb hC + have hCT : σ.curThread = specPtr ct := H.sched.sch.curThread.trans + (congrArg specPtr (ok_inj_ff ((eq_of_spec_fd (sched.cur_thread.spec s)).symm.trans hct))) + obtain ⟨x, hkT, -⟩ := H.sched.sch.tcb ct hctT + have htS : sched.TS_RESTART.val = tsCodeA .restart := by rw [ts_vals.2.1]; rfl + let num := core.num.U64.wrapping_add er sporadic.MIN_REFILLS + have sb64 := hsb hX p sb hC + have maxEq : Fl4SpecA64.maxNumRefills sb.val = absMax sb.val := by + unfold absMax Fl4SpecA64.maxNumRefills Fl4SpecA64.sizeofSchedContextT Fl4SpecA64.wordSize Fl4SpecA64.refillSizeBytes + rw [if_neg (by omega)] + have maxBound : absMax sb.val < 2 ^ 64 := by + unfold absMax + rw [if_neg (by omega)] + have hp := Nat.pow_lt_pow_right (by decide : 1 < 2) sb64 + omega + have vNum : num.val = er.val + 2 := by + have v2 : sporadic.MIN_REFILLS.val = 2 := by unfold sporadic.MIN_REFILLS; rfl + rw [maxEq] at hRef + unfold num + rw [wadd_val (by rw [v2]; omega), v2] + have hNumMax : num.val ≤ absMax (n + minSchedContextBits) := by rw [vNum, ← hSz, ← maxEq]; exact hRef + have hNumMin : 2 ≤ num.val := by rw [vNum]; omega + -- Retain the successful result equation to discharge the concrete no-stall condition. + have Step : + sched.set_thread_state st s ct sched.TS_RESTART ⦃ s1 => + (sched.set_thread_state st s ct sched.TS_RESTART = ok s1) ∧ ∃ σ1, + setThreadState (Z := Unit) (specPtr ct) .restart σ = ({((), σ1)}, False) ∧ + YieldQueueRelA (st := st) Z m κ s1 σ1 ∧ SSzA σ σ1 ∧ + WritesIn st s s1 (fun b => SWrA (st := st) Z s b ∨ b = stA ct) ⦄ := by + obtain ⟨s1, eq, σ1, e1, H1, -, -, -, sz, w⟩ := spec_existsA + (yieldRunnableA H hctT hkT (hr _ hkT) (ts := .restart) rfl htS) + rw [eq, WP.spec_ok] + exact ⟨rfl, σ1, e1, H1, sz, w⟩ + refine WP.spec_bind Step ?_ + rintro s1 ⟨es1, σ1, e1, H1, sz, w⟩ + obtain ⟨eq, I1, O1, R1, Ac1⟩ := cfgRestartTimingA H.sched I O R Ac hCT hkT (hr _ hkT) e1 + have hkp : scNameA m p ≠ specPtr ct := fun e => by rw [e, hkT] at hk; cases hk + have hk1 : σ1.kheap (scNameA m p) = some (.schedContext y n) := by + rwa [eq, updKSc_kheap, Function.update_of_ne hkp] + have pcpu : cpu = L.cpu s1 := hcpu.trans w.2.1.symm + have hLoc : CfgOwnerLocalA (st := st) (ar := ar) Z m s1 a σ1 p := by + intro hn + let t := L.mem s1 (scTcbA p) + change t ≠ 0#u64 at hn + have hWord : L.mem s (scTcbA p) = t := by + obtain ⟨y0, n0, e0, Rel0⟩ := H.sched.scr.sc p Hc + obtain ⟨y1, n1, e1, Rel1⟩ := H1.sched.scr.sc p Hc + rw [hk] at e0; cases e0 + rw [hk1] at e1; cases e1 + have e : optRefS (L.mem s (scTcbA p)) = optRefS t := by + rw [scTcbA_eqSc, ← Rel0.tcb] + simpa only [t, scTcbA_eqSc] using Rel1.tcb + by_cases nz : L.mem s (scTcbA p) = 0#u64 + · rw [nz, optRefS_zero, optRefS_ne hn] at e; cases e + · rw [optRefS_ne nz, optRefS_ne hn] at e + exact specPtr_inj (Option.some.inj e) + obtain ⟨ht, hon, co, hb⟩ := hOwner t hWord hn + refine ⟨ht, ?_, ?_, ?_, hStall s1 es1 hX p sb hC t rfl hn⟩ + · rw [w.2.1, ← hon] + exact UScalar.eq_of_val_eq (Option.some.inj ((H1.sched.sch.aff t ht).symm.trans (H.sched.sch.aff t ht))) + · rw [H1.sched.scCore _ Hc, ← H.sched.scCore _ Hc, co, w.2.1] + · intro z hz + change σ1.kheap (specPtr t) = some (.tcb z) at hz + by_cases e : t = ct + · rw [e, eq, updKSc_same] at hz + cases hz + exact hb x (by rw [e]; exact hkT) + · rw [eq, updKSc_kheap, Function.update_of_ne (fun f => e (specPtr_inj f))] at hz + exact hb z hz + refine WP.spec_bind (cfg_configure_flagsA H1.sched H1.states H1.mcp H1.links I1 O1 R1 Ac1 a hAgree hT + Hc hp0 pcpu hni hLoc hk1 budget period num bd fl hMb hBp hNumMin hNumMax) ?_ + rintro ⟨s2, a2⟩ ⟨ea, τ, Q', R', e, H2, St2, M2, Y2, I2, O2, R2, Ac2, -, -, W2⟩ + simp only [uncurry_apply_pair, WP.spec_ok] + have ec := scNameA_eq_of_notIdle hni + refine ⟨True.intro, ea, σ1, τ, p, Q', R', e1, ?_, ⟨H2, St2, M2, Y2⟩, I2, O2, R2, Ac2, ?_⟩ + · simpa only [ec, hBu, hPe, vNum] using e + · refine (w.trans W2.wr).mono fun b e => ?_ + rcases e with (e | e) | e + · exact Or.inl (Or.inr e) + · exact Or.inr e + · exact Or.inl (mwr_back rfl rfl sz w.1 e) + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScCheckA.lean b/proofs/Fl4ProofsA64/P3/ScCheckA.lean new file mode 100644 index 000000000..afa090fcc --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScCheckA.lean @@ -0,0 +1,363 @@ +/- +P3 (aarch64, A64-P3-MCS gen 2): **`charge_budget`'s head, and `check_budget` / `check_budget_restart` on the +sufficient branch**, on `ScStateRelA` (x86 `P3/ScCharge6`'s `chargeHead` / `charge_head_refines`, `P3/ScCharge7`'s +`check_budget_refines` / `check_budget_restart_refines` on their sufficient branch; ported), and their `JAtZA` forms. + +FL4's AArch64 `sched::{charge_budget, check_budget, check_budget_restart, is_cur_domain_expired}` are the x86 text +(extraction diffed: SAME); `Fl4SpecA64.chargeBudget` differs from the x86 port only in its idle test (the constant +`idle_sc_ptr` for x86's `idle_sc` field), so: +* **`chargeHeadA`** / `chargeTailA` / `chargeBudget_eqA` (x86 `chargeHead` / `chargeTail` / `chargeBudget_eq`): + `charge_budget` is its head (the SC charge, up to the consumed-time reset) then its tail. +* **`charge_head_refinesA`** (x86 `charge_head_refines`, the SC part): FL4's head (after the idle test, `b`) is + `chargeHeadA`: a round-robin SC is `refill_reset_rr` (`rr_reset_refinesA`), any other `refill_budget_check` + (`budget_check_refinesA`), then the consumed update (`consumed_refinesA`); single spec outcome, `ScStateRelA` / + `ScPlacedA` after, writes inside the SC's object. **`idle_testA`**: FL4's test `CUR_SC ≠ IDLE_SC` is the spec's + `cur_sc ≠ idle_sc_ptr` (`hidle` / `hoff`: `SchedRelA.idleScOf` / `.scOff` at `JAtZA`). +* **`check_budget_sufficientA`** / **`check_budget_restart_sufficientA`** (x86 `check_budget_refines` / + `check_budget_restart_refines`, sufficient branch): when the spec's head refill is sufficient for the consumed + time (`hsuf`, the case selector), FL4's `check_budget` returns `true` with the state unchanged, as the spec. +* **`…J`** forms at `JAtZA`: the current SC, the consumed time and the idle SC come from `SchedRelA` / `SchedRestA`. + +No named premise. Hypotheses as x86's: the case selectors (`b`, `hsuf`), `hact` (a non-idle current SC is active, +l4v `cur_sc_active`), `hpos` / `hsum` / `hmin` (l4v `valid_refills` of the current SC, for `refill_budget_check`), +`hkt` (the current thread is a TCB), and the platform facts `hmrt` / `hmb` / `hnd` (x86's `numDomains = 1`: FL4 is +built with one domain), all three for the `[P]` platform record at the join. +-/ +import Fl4ProofsA64.P3.ScBudgetA +import Fl4ProofsA64.P3.ScJoinA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +/-! ## The spec side -/ + +section +variable {σ α β γ : Type} + +theorem nd_bind_assocSc (m : Fl4Spec.NonDet σ α) (f : α → Fl4Spec.NonDet σ β) (g : β → Fl4Spec.NonDet σ γ) + (s : σ) : ((m >>= f) >>= g) s = (m >>= fun a => f a >>= g) s := by + simp only [nd_bind_applySc] + refine Prod.ext (Set.ext fun r => ?_) (propext ?_) + · simp only [Set.mem_setOf_eq]; constructor + · rintro ⟨x, ⟨y, hy, hx⟩, hr⟩; exact ⟨y, hy, x, hx, hr⟩ + · rintro ⟨y, hy, x, hx, hr⟩; exact ⟨x, ⟨y, hy, hx⟩, hr⟩ + · simp only [Set.mem_setOf_eq]; constructor + · rintro ((h | ⟨y, hy, h⟩) | ⟨x, ⟨y, hy, hx⟩, h⟩) + · exact Or.inl h + · exact Or.inr ⟨y, hy, Or.inl h⟩ + · exact Or.inr ⟨y, hy, Or.inr ⟨x, hx, h⟩⟩ + · rintro (h | ⟨y, hy, h | ⟨x, hx, h⟩⟩) + · exact Or.inl (Or.inl h) + · exact Or.inl (Or.inr ⟨y, hy, h⟩) + · exact Or.inr ⟨x, ⟨y, hy, hx⟩, h⟩ + +end + +section Spec +open Fl4SpecA64 + +/-- `charge_budget` up to the consumed-time reset (x86 `chargeHead`; the idle test reads `idle_sc_ptr`). -/ +def chargeHeadA (consumed : Ticks) : SMonad Unit Unit := do + let cscPtr ← Fl4Spec.NonDet.gets fun s : State Unit => s.curSc + Fl4Spec.NonDet.when (cscPtr ≠ idleScPtr) <| do + let robin ← isRoundRobin cscPtr + if robin + then refillResetRr cscPtr + else refillBudgetCheck consumed + updateSchedContext cscPtr (fun sc => { sc with scConsumed := sc.scConsumed + consumed }) + +/-- The rest of `charge_budget` (x86 `chargeTail`). -/ +def chargeTailA (canTimeout : Bool) : SMonad Unit Unit := do + Fl4Spec.NonDet.modify fun s : State Unit => { s with consumedTime := 0 } + let ct ← Fl4Spec.NonDet.gets fun s : State Unit => s.curThread + let sched ← Fl4Spec.NonDet.gets (schedulable ct) + Fl4Spec.NonDet.when sched <| do + endTimeslice canTimeout + rescheduleRequired + Fl4Spec.NonDet.modify fun s : State Unit => { s with reprogramTimer := true } + +/-- `charge_budget` is its head then its tail (x86 `chargeBudget_eq`). -/ +theorem chargeBudget_eqA (consumed : Ticks) (can : Bool) (σ : State Unit) : + chargeBudget (Z := Unit) consumed can σ = (chargeHeadA consumed >>= fun _ => chargeTailA can) σ := by + unfold chargeHeadA chargeBudget + simp only [nd_bind_assocSc, nd_gets_bindSc] + all_goals rfl + +theorem threadGet_eqSc {γ : Type} {σ : State Unit} {t : ObjRef} {x : Tcb} (f : Tcb → γ) + (hk : σ.kheap t = some (.tcb x)) : threadGet (Z := Unit) f t σ = ({(f x, σ)}, False) := by + unfold threadGet + rw [nd_bind_oneSc (show Fl4Spec.NonDet.getsThe (getTcb (Z := Unit) t) σ = ({(x, σ)}, False) by + simp [Fl4Spec.NonDet.getsThe, getTcb, readObject, hk, Fl4Spec.NonDet.assertOpt]; rfl)] + rfl + +end Spec + +/-! ## FL4's reads -/ + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +theorem sched.idle_sc.specA (s : S) : + sched.idle_sc st s ⦃ v => v = L.mem s (L.nodeA s layout.node.IDLE_SC) ⦄ := by + unfold sched.idle_sc + refine WP.spec_bind (LawfulStore.cur_cpu_spec s) ?_ + rintro c rfl + first + | exact obj.node_get.spec s _ _ + | exact WP.spec_bind (obj.node_get.spec s _ _) fun v hv => (WP.spec_ok _).2 hv + +theorem sched.consumed.specA (s : S) : + sched.consumed st s ⦃ v => v = L.mem s (L.nodeA s layout.node.CONSUMED) ⦄ := by + unfold sched.consumed + refine WP.spec_bind (LawfulStore.cur_cpu_spec s) ?_ + rintro c rfl + first + | exact obj.node_get.spec s _ _ + | exact WP.spec_bind (obj.node_get.spec s _ _) fun v hv => (WP.spec_ok _).2 hv + +theorem sc_ne_eqA (a b : obj.Sc) : + core.cmp.PartialEq.ne.trait_default obj.Sc.Insts.CoreCmpPartialEqSc a b = ok (decide (a ≠ b)) := by + unfold core.cmp.PartialEq.ne.trait_default core.cmp.PartialEq.ne.default + simp [obj.Sc.Insts.CoreCmpPartialEqSc.eq] + exact ⟨fun e => e ▸ rfl, UScalar.eq_of_val_eq⟩ + +/-- FL4 is built with one domain (x86 `is_cur_domain_expired_eq`). -/ +theorem is_cur_domain_expired_eqA (s : S) : sched.is_cur_domain_expired st s = ok false := by + unfold sched.is_cur_domain_expired + rw [show layout.NUM_DOMAINS = 1#u64 by unfold layout.NUM_DOMAINS fl4_abi.config.NUM_DOMAINS; rfl] + rfl + +/-- **FL4's idle test is the spec's**: `CUR_SC ≠ IDLE_SC` iff the renamed current SC is not `idle_sc_ptr`. -/ +theorem idle_testA {m : Fl4SpecA64.McState} {s : S} {sc : U64} + (hidle : m.idleScOf m.curCore = specPtr (L.mem s (L.nodeA s layout.node.IDLE_SC))) + (hoff : specPtr sc ≠ m.idleScOf m.curCore → specPtr sc ≠ Fl4SpecA64.idleScPtr) : + decide (sc ≠ L.mem s (L.nodeA s layout.node.IDLE_SC)) = decide (scNameA m sc ≠ Fl4SpecA64.idleScPtr) := by + apply decide_eq_decide.2 + unfold scNameA + by_cases e : specPtr sc = m.idleScOf m.curCore + · rw [if_pos e] + have : sc = L.mem s (L.nodeA s layout.node.IDLE_SC) := specPtr_inj (by rw [e, hidle]) + exact ⟨fun h => absurd this h, fun h => absurd rfl h⟩ + · rw [if_neg e] + exact ⟨fun _ => hoff e, fun _ h => e (by rw [h, hidle])⟩ + +/-! ## `charge_budget`'s head -/ + +/-- **`charge_budget`'s head refines `chargeHeadA`** (x86 `charge_head_refines`, on `ScStateRelA`). -/ +theorem charge_head_refinesA {th : TypedHeapA} {ksb : U64} {C : Set U64} {m : Fl4SpecA64.McState} {s : S} (a : A) + {σ : Fl4SpecA64.State Unit} (h : ScStateRelA st C m s σ) (P : ScPlacedA th ksb C m σ) {sc : U64} + (hcs : L.cscW s = sc) (hcur : σ.curSc = scNameA m sc) (hc : sc ∈ C) {x : Fl4SpecA64.SchedContext} {n : Nat} + (hk : σ.kheap (scNameA m sc) = some (.schedContext x n)) + (hact : scNameA m sc ≠ Fl4SpecA64.idleScPtr → Fl4SpecA64.scActive x = true) + (hmrt : sporadic.max_release_time ar a ⦃ mrt => mrt.bv = Fl4SpecA64.MAX_RELEASE_TIME ⦄) + (hmb : sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hpos : PosA (L.refills s sc)) (hsum : lsumA (L.refills s sc) < 2 ^ 64) + (hmin : Fl4SpecA64.MIN_BUDGET.toNat ≤ lsumA (L.refills s sc)) (used : U64) (b : Bool) + (hb : b = decide (scNameA m sc ≠ Fl4SpecA64.idleScPtr)) : + (if b then do + let b1 ← sporadic.is_round_robin st s sc + let s3 ← if b1 then do + let hi ← obj.Sc.refill_head st sc s + let ti ← obj.Sc.refill_tail st sc s + let h ← sporadic.get st s sc hi + let t ← sporadic.get st s sc ti + let i ← lift (core.num.U64.wrapping_add h.amount t.amount) + let s4 ← sporadic.put st s sc hi { h with amount := i } + sporadic.put st s4 sc ti { t with amount := 0#u64 } + else sporadic.budget_check st ar s a used + let i ← obj.Sc.consumed st sc s3 + let total ← lift (core.num.U64.wrapping_add i used) + obj.Sc.set_consumed st sc s3 total + else ok s) ⦃ s2 => ∃ σ2, chargeHeadA used.bv σ = ({((), σ2)}, False) ∧ + ScStateRelA st C m s2 σ2 ∧ ScPlacedA th ksb C m σ2 ∧ WritesIn st s s2 (ScObjA sc n) ⦄ := by + have hspec : chargeHeadA used.bv σ = Fl4Spec.NonDet.when (scNameA m sc ≠ Fl4SpecA64.idleScPtr) (do + let robin ← Fl4SpecA64.isRoundRobin (Z := Unit) (scNameA m sc) + if robin + then Fl4SpecA64.refillResetRr (scNameA m sc) + else Fl4SpecA64.refillBudgetCheck used.bv + Fl4SpecA64.updateSchedContext (scNameA m sc) + (fun y => { y with scConsumed := y.scConsumed + used.bv })) σ := by + unfold chargeHeadA; rw [nd_gets_bindSc, hcur] + by_cases hid : scNameA m sc ≠ Fl4SpecA64.idleScPtr + · have hb' : b = true := by rw [hb]; simpa using hid + simp only [hb', ↓reduceIte] + obtain ⟨x', n', hk', hxw⟩ := h.sc sc hc + rw [hk] at hk'; cases hk' + have hx := hxw.strong (hact hid) + refine WP.spec_bind (is_round_robin_refinesA h hc hk) ?_ + intro rr hrrS + -- the consumed update, from the state after the refill update + have fin : ∀ (s3 : S) (σ3 : Fl4SpecA64.State Unit) (y : Fl4SpecA64.SchedContext), + (if rr = true then Fl4SpecA64.refillResetRr (Z := Unit) (scNameA m sc) + else Fl4SpecA64.refillBudgetCheck used.bv) σ = ({((), σ3)}, False) → + ScStateRelA st C m s3 σ3 → ScPlacedA th ksb C m σ3 → + σ3.kheap (scNameA m sc) = some (.schedContext y n) → WritesIn st s s3 (ScObjA sc n) → + (do + let i ← obj.Sc.consumed st sc s3 + let total ← lift (core.num.U64.wrapping_add i used) + obj.Sc.set_consumed st sc s3 total) ⦃ s2 => ∃ σ2, chargeHeadA used.bv σ = ({((), σ2)}, False) ∧ + ScStateRelA st C m s2 σ2 ∧ ScPlacedA th ksb C m σ2 ∧ WritesIn st s s2 (ScObjA sc n) ⦄ := by + intro s3 σ3 y h3 hS3 hP3 hk3 hw3 + refine WP.spec_mono (consumed_refinesA hS3 hP3 hc hk3 used) ?_ + rintro s2 ⟨σ2, e2, hS2, hP2, hw2⟩ + refine ⟨σ2, ?_, hS2, hP2, (hw3.trans hw2).mono fun b hb => hb.elim id id⟩ + rw [hspec, Fl4Spec.NonDet.when, if_pos hid, nd_bind_oneSc hrrS] + rcases Bool.eq_false_or_eq_true rr with hr | hr <;> subst hr <;> + simp only [Bool.false_eq_true, ↓reduceIte] at h3 ⊢ <;> rw [nd_bind_oneSc h3] <;> exact e2 + rcases Bool.eq_false_or_eq_true rr with hr | hr <;> subst hr + · simp only [↓reduceIte] + refine WP.spec_bind (rr_reset_refinesA h P hc hk (hact hid)) ?_ + rintro s3 ⟨σ3, e3, hS3, hP3, hw3⟩ + have e3' := e3 + rw [refillResetRr_eqA hk hx.refills (refills_neL hx.wf)] at e3' + simp only [Prod.mk.injEq, Set.singleton_eq_singleton_iff, and_true, true_and] at e3' + subst e3' + exact fin s3 _ _ (by rw [if_pos rfl]; exact e3) hS3 hP3 (updKSc_same _ _ _) hw3 + · simp only [Bool.false_eq_true, ↓reduceIte] + have hper : x.scPeriod ≠ 0 := by + intro e0; rw [isRoundRobin_eqA hk, e0] at hrrS; simp at hrrS + have hrr : L.rperiod s sc ≠ 0#u64 := by + intro e; apply hper; rw [hx.period, e]; rfl + refine WP.spec_bind (budget_check_refinesA a h P hcs hcur hc hk (hact hid) hmrt hmb hrr hpos hsum hmin + used) ?_ + rintro s3 ⟨σ3, e3, hS3, hP3, hw3, hpost⟩ + obtain ⟨y', rfl, -, -⟩ := hpost x n hk + exact fin s3 _ y' (by rw [if_neg (by simp)]; exact e3) hS3 hP3 (updKSc_same _ _ _) hw3 + · have hb' : b = false := by rw [hb]; simpa using hid + simp only [hb', Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + refine ⟨σ, ?_, h, P, WritesIn.refl _ _⟩ + rw [hspec]; unfold Fl4Spec.NonDet.when; rw [if_neg hid]; rfl + +/-! ## `check_budget` on the sufficient branch -/ + +/-- **`check_budget` with a sufficient head refill** (x86 `check_budget_refines`, sufficient branch): FL4 returns +`true` and keeps its state, as `check_budget` does. -/ +theorem check_budget_sufficientA {C : Set U64} {m : Fl4SpecA64.McState} {s : S} (a : A) + {σ : Fl4SpecA64.State Unit} (h : ScStateRelA st C m s σ) {sc : U64} (hc : sc ∈ C) (hcs : L.cscW s = sc) + (hcur : σ.curSc = scNameA m sc) + (hcons : σ.consumedTime = (L.mem s (L.nodeA s layout.node.CONSUMED)).bv) + {x : Fl4SpecA64.SchedContext} {n : Nat} (hk : σ.kheap (scNameA m sc) = some (.schedContext x n)) + (ha : Fl4SpecA64.scActive x = true) + (hmb : sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) (hnd : Fl4SpecA64.numDomains = 1) + (hsuf : Fl4SpecA64.getScRefillSufficient (Z := Unit) (scNameA m sc) σ.consumedTime σ = ({(true, σ)}, False)) : + sched.check_budget st ar s a ⦃ r => r = (true, s, a) ∧ + Fl4SpecA64.checkBudget (Z := Unit) σ = ({(true, σ)}, False) ⦄ := by + unfold sched.check_budget + refine WP.spec_bind (sched.cur_sc.spec s) ?_ + rintro c hc' + obtain rfl : c = sc := by rw [hc']; exact hcs + refine WP.spec_bind (sched.consumed.specA s) ?_ + rintro u hu + refine WP.spec_bind (sufficient_refinesA a h hc hk ha u hmb) ?_ + intro b hb + have hub : u.bv = σ.consumedTime := by rw [hu, hcons] + rw [hub, hsuf] at hb + simp only [Prod.mk.injEq, Set.singleton_eq_singleton_iff, and_true] at hb + subst hb + simp only [↓reduceIte, is_cur_domain_expired_eqA, bind_ok, WP.spec_ok] + refine ⟨by simp, ?_⟩ + unfold Fl4SpecA64.checkBudget + rw [nd_gets_bindSc, nd_gets_bindSc, hcur, nd_bind_oneSc hsuf, if_pos rfl, nd_gets_bindSc] + simp only [Fl4SpecA64.isCurDomainExpired, hnd] + rfl + +/-- **`check_budget_restart` with a sufficient head refill** (x86 `check_budget_restart_refines`, sufficient +branch): `true`, nothing restarted. -/ +theorem check_budget_restart_sufficientA {C : Set U64} {m : Fl4SpecA64.McState} {s : S} (a : A) + {σ : Fl4SpecA64.State Unit} (h : ScStateRelA st C m s σ) {sc : U64} (hc : sc ∈ C) (hcs : L.cscW s = sc) + (hcur : σ.curSc = scNameA m sc) + (hcons : σ.consumedTime = (L.mem s (L.nodeA s layout.node.CONSUMED)).bv) + {x : Fl4SpecA64.SchedContext} {n : Nat} (hk : σ.kheap (scNameA m sc) = some (.schedContext x n)) + (ha : Fl4SpecA64.scActive x = true) + (hmb : sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) (hnd : Fl4SpecA64.numDomains = 1) + (hsuf : Fl4SpecA64.getScRefillSufficient (Z := Unit) (scNameA m sc) σ.consumedTime σ = ({(true, σ)}, False)) + {xt : Fl4SpecA64.Tcb} (hkt : σ.kheap σ.curThread = some (.tcb xt)) : + sched.check_budget_restart st ar s a ⦃ r => r = (true, s, a) ∧ + Fl4SpecA64.checkBudgetRestart (Z := Unit) σ = ({(true, σ)}, False) ⦄ := by + unfold sched.check_budget_restart + refine WP.spec_bind (check_budget_sufficientA a h hc hcs hcur hcons hk ha hmb hnd hsuf) ?_ + rintro ⟨r, s1, a1⟩ ⟨e, hcb⟩ + simp only [Prod.mk.injEq] at e + obtain ⟨rfl, rfl, rfl⟩ := e + step with sched.cur_thread.spec as ⟨ct, _hct⟩ + simp only [↓reduceIte, WP.spec_ok] + refine ⟨trivial, ?_⟩ + unfold Fl4SpecA64.checkBudgetRestart + rw [nd_bind_oneSc hcb, nd_gets_bindSc, nd_bind_oneSc (show Fl4SpecA64.getThreadState (Z := Unit) σ.curThread σ = + ({(xt.tcbState, σ)}, False) from threadGet_eqSc _ hkt)] + simp only [Fl4Spec.NonDet.when, not_true_eq_false, false_and, ↓reduceIte] + exact (nd_pure_bindSc () (fun _ => pure true) σ).trans rfl + +end + +/-! ## At `JAtZA` -/ + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +/-- The current core is one of `Z.Cs`, and its node words are the view's (`SchedRelA.core` / `.cpuCs`). -/ +theorem idleScW_J {AR : A → HwRootA → Prop} {Z : XSetsA} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} (J : JAtZA (L := L) AR Z s a σ m) : + m.idleScOf m.curCore = specPtr (L.mem s (L.nodeA s layout.node.IDLE_SC)) := by + rw [J.sch.core]; exact J.sch.idleScOf _ J.sch.cpuCs + +/-- **`charge_budget`'s head at `JAtZA`**: FL4's head from the current SC read is `chargeHeadA`. -/ +theorem charge_head_refinesJ {AR : A → HwRootA → Prop} {Z : XSetsA} {s : S} (a : A) {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} (J : JAtZA (L := L) AR Z s a σ m) (h : ScStateRelA st Z.C m s σ) + (P : ScPlacedA Z.th (L.ksb s) Z.C m σ) {x : Fl4SpecA64.SchedContext} {n : Nat} + (hk : σ.kheap (scNameA m (L.cscW s)) = some (.schedContext x n)) + (hact : scNameA m (L.cscW s) ≠ Fl4SpecA64.idleScPtr → Fl4SpecA64.scActive x = true) + (hmrt : sporadic.max_release_time ar a ⦃ mrt => mrt.bv = Fl4SpecA64.MAX_RELEASE_TIME ⦄) + (hmb : sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hpos : PosA (L.refills s (L.cscW s))) (hsum : lsumA (L.refills s (L.cscW s)) < 2 ^ 64) + (hmin : Fl4SpecA64.MIN_BUDGET.toNat ≤ lsumA (L.refills s (L.cscW s))) (used : U64) : + (do + let sc ← sched.cur_sc st s + let s1 ← sched.idle_sc st s + let b ← core.cmp.PartialEq.ne.trait_default obj.Sc.Insts.CoreCmpPartialEqSc sc s1 + if b then do + let b1 ← sporadic.is_round_robin st s sc + let s3 ← if b1 then do + let hi ← obj.Sc.refill_head st sc s + let ti ← obj.Sc.refill_tail st sc s + let h ← sporadic.get st s sc hi + let t ← sporadic.get st s sc ti + let i ← lift (core.num.U64.wrapping_add h.amount t.amount) + let s4 ← sporadic.put st s sc hi { h with amount := i } + sporadic.put st s4 sc ti { t with amount := 0#u64 } + else sporadic.budget_check st ar s a used + let i ← obj.Sc.consumed st sc s3 + let total ← lift (core.num.U64.wrapping_add i used) + obj.Sc.set_consumed st sc s3 total + else ok s) ⦃ s2 => ∃ σ2, chargeHeadA used.bv σ = ({((), σ2)}, False) ∧ + ScStateRelA st Z.C m s2 σ2 ∧ ScPlacedA Z.th (L.ksb s) Z.C m σ2 ∧ + WritesIn st s s2 (ScObjA (L.cscW s) n) ⦄ := by + have hc : L.cscW s ∈ Z.C := J.cscC _ J.sch.cpuCs + refine WP.spec_bind (sched.cur_sc.spec s) ?_ + rintro c hc' + obtain rfl : c = L.cscW s := hc' + refine WP.spec_bind (sched.idle_sc.specA s) ?_ + rintro i hi + rw [sc_ne_eqA, bind_ok, hi, idle_testA (idleScW_J J) (J.sch.scOff _ hc)] + exact charge_head_refinesA a h P rfl J.sch.curScW hc hk hact hmrt hmb hpos hsum hmin used _ rfl + +/-- **`check_budget` with a sufficient head refill, at `JAtZA`.** -/ +theorem check_budget_sufficientJ {AR : A → HwRootA → Prop} {Z : XSetsA} {s : S} (a : A) + {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} (J : JAtZA (L := L) AR Z s a σ m) + (h : ScStateRelA st Z.C m s σ) {x : Fl4SpecA64.SchedContext} {n : Nat} + (hk : σ.kheap (scNameA m (L.cscW s)) = some (.schedContext x n)) (ha : Fl4SpecA64.scActive x = true) + (hmb : sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) (hnd : Fl4SpecA64.numDomains = 1) + (hsuf : Fl4SpecA64.getScRefillSufficient (Z := Unit) σ.curSc σ.consumedTime σ = ({(true, σ)}, False)) : + sched.check_budget st ar s a ⦃ r => r = (true, s, a) ∧ + Fl4SpecA64.checkBudget (Z := Unit) σ = ({(true, σ)}, False) ⦄ := + check_budget_sufficientA (sc := L.cscW s) a h (J.cscC _ J.sch.cpuCs) rfl J.sch.curScW J.rest.consumed hk ha + hmb hnd (by rw [show scNameA m (L.cscW s) = σ.curSc from J.sch.curScW.symm]; exact hsuf) + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScChooseA.lean b/proofs/Fl4ProofsA64/P3/ScChooseA.lean new file mode 100644 index 000000000..4e1789f7b --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScChooseA.lean @@ -0,0 +1,114 @@ +import Fl4ProofsA64.P3.ScSchedWordsA + +/-! The local selector composes the already-frozen root observations +with ready-queue changes and the current-thread cursor. -/ + +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3.Sched + +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +set_option maxHeartbeats 70000 + +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +variable {A : Type} {ar : machine.Arch A} + +/-- Root installation, ready dequeue, and local current-thread write in +their concrete execution order. The outgoing SC remains current. -/ +theorem switch_to_thread_refinesA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {a : A} {σ : State Unit} {AR : A → HwRootA → Prop} + (H : YieldQueueRelA (st := st) Z m κ s σ) + (J : JAtZAX (L := L) AR Z s a σ m) (P : ArchSwFA st ar) (V : VsRootOKA σ) (T : ArchTieA (L := L) Z s σ) + {t : U64} (ht : t ∈ Z.T) (hon : L.mem s (affA t) = L.cpu s) : + sched.switch_to_thread st ar s a t ⦃ r => ∃ τ Q, + fl4SwitchToThread (Z := Unit) (specPtr t) σ = ({((), τ)}, False) ∧ + P.rootWrites a r.2 (L.cpu s).val (threadRootWritesA σ (specPtr t)) ∧ + YieldQueueRelA (st := st) + {Z with Q := Q, curOf := Function.update Z.curOf m.curCore (specPtr t)} m κ r.1 τ ∧ + MFrA (st := st) Z m s r.1 σ τ ⦄ := by + unfold sched.switch_to_thread + refine WP.spec_bind (P.execThread s a (L.cpu s).val σ t Z m AR J rfl ht V T.Y0 T.K (T.R t ht) T.c0) ?_ + rintro ⟨s0, a1⟩ ⟨same, roots⟩ + change s0 = s at same + subst s0 + simp only [uncurry_apply_pair] + refine WP.spec_bind (yieldDequeueA H ht hon) ?_ + rintro s1 ⟨σ1, Q, he, _, _, H1, F1⟩ + have local1 := yieldAffKeepA H.sched H1.sched ht ht hon + refine WP.spec_bind (sched.set_cur_thread.spec s1 t) ?_ + rintro s2 rfl + rw [WP.spec_ok] + obtain ⟨H2, F2⟩ := setCurrentThreadWriteA H1 ht local1 + refine ⟨{σ1 with curThread := specPtr t}, Q, ?_, roots, H2, F1.trans F2 rfl rfl⟩ + unfold fl4SwitchToThread + rw [nd_bind_oneSc he] + rfl + +/-- Idle selection writes the global root and installs the local idle TCB. -/ +theorem switch_to_idle_refinesA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {a : A} {σ : State Unit} {AR : A → HwRootA → Prop} + (H : YieldQueueRelA (st := st) Z m κ s σ) + (J : JAtZAX (L := L) AR Z s a σ m) (P : ArchSwFA st ar) (V : IdleVtA σ) (T : ArchTieA (L := L) Z s σ) + (ht : L.idleW s ∈ Z.T) (hon : L.mem s (affA (L.idleW s)) = L.cpu s) : + sched.switch_to_idle_thread st ar s a ⦃ r => + P.rootWrites a r.2 (L.cpu s).val (kernelRootWritesA σ) ∧ + YieldQueueRelA (st := st) {Z with curOf := Function.update Z.curOf m.curCore σ.idleThread} + m κ r.1 {σ with curThread := σ.idleThread} ∧ + MFrA (st := st) Z m s r.1 σ {σ with curThread := σ.idleThread} ⦄ := by + refine WP.spec_mono (switch_to_idle_thread_wordsA J P V T) ?_ + rintro ⟨s', a'⟩ ⟨trace, write⟩ + change s' = _ at write + subst s' + have idle : σ.idleThread = specPtr (L.idleW s) := H.sched.sch.idle + have state := congrArg (fun t => {σ with curThread := t}) idle + have cursors := congrArg (fun t => {Z with curOf := Function.update Z.curOf m.curCore t}) idle + rw [state, cursors] + exact ⟨trace, setCurrentThreadWriteA H ht hon⟩ + +/-- Equivalent idle and highest-priority choices, including their +selected-thread cursor and ordered architecture writes. -/ +theorem choose_thread_refinesA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {a : A} {σ : State Unit} {AR : A → HwRootA → Prop} + (H : YieldQueueRelA (st := st) Z m κ s σ) + (J : JAtZAX (L := L) AR Z s a σ m) (P : ArchSwFA st ar) (V : VsRootOKA σ) (I : IdleVtA σ) (T : ArchTieA (L := L) Z s σ) + (ht : L.idleW s ∈ Z.T) (hon : L.mem s (affA (L.idleW s)) = L.cpu s) : + sched.choose_thread st ar s a ⦃ r => ∃ τ Q, + fl4ChooseThread (Z := Unit) σ = ({((), τ)}, False) ∧ + P.rootWrites a r.2 (L.cpu s).val + (if ∀ p, σ.readyQueues σ.curDomain p = [] then kernelRootWritesA σ else threadRootWritesA σ τ.curThread) ∧ + YieldQueueRelA (st := st) + {Z with Q := Q, curOf := Function.update Z.curOf m.curCore τ.curThread} m κ r.1 τ ∧ + MFrA (st := st) Z m s r.1 σ τ ⦄ := by + classical + refine choose_thread_casesA H.sched.toSchedSRN _ ?_ ?_ + · intro empty + refine WP.spec_mono (switch_to_idle_refinesA H J P I T ht hon) ?_ + rintro r ⟨trace, rel, frame⟩ + refine ⟨_, Z.Q, ?_, ?_, rel, frame⟩ + · unfold fl4ChooseThread + rw [nd_bind_oneSc (show NonDet.get σ = ({(σ, σ)}, False) from rfl), if_pos empty] + rfl + · simpa only [if_pos empty] using trace + · intro t ht hon' ne selected + refine WP.spec_mono (switch_to_thread_refinesA H J P V T ht hon') ?_ + rintro r ⟨τ, Q, abs, trace, rel, frame⟩ + have cur : τ.curThread = specPtr t := by + have := rel.sched.sch.cur + change Function.update Z.curOf m.curCore (specPtr t) m.curCore = τ.curThread at this + rw [Function.update_self] at this + exact this.symm + refine ⟨τ, Q, ?_, ?_, ?_, frame⟩ + · unfold fl4ChooseThread + rw [nd_bind_oneSc (show NonDet.get σ = ({(σ, σ)}, False) from rfl), if_neg ne, ← selected] + exact abs + · simpa only [if_neg ne, cur] using trace + · rw [cur] + exact rel + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScConsA.lean b/proofs/Fl4ProofsA64/P3/ScConsA.lean new file mode 100644 index 000000000..cdc7d768a --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScConsA.lean @@ -0,0 +1,323 @@ +/- +P3 (aarch64, A64-P3-MCS gen 9): **`schedcontext::update_consumed` refines `sched_context_update_consumed`** on +`SchedSRA` (x86 `P3/ScConsumed`'s `update_consumed_refines`, ported; ruling 01:09Z gen-9 item 1, the standalone op +the IPC line's timeout-fault message reads). + +* **`SchedSRA.scObjA`** (x86 `Inv.scUpd` with `SchedRel.set_sc_consumed`'s relation part): replacing one related SC's + object by one with the same refills, refill max, period and TCB, FL4 writing inside that SC object only (the `CORE` + word kept), keeps `SchedSRA` (same `Z`). +* **`update_consumed_refinesA`** (x86 `update_consumed_refines`): for a related SC, the single never-failing outcome + `(v.bv, σ')` of `schedContextUpdateConsumed`, `SchedSRA` after, FL4 writing the SC's `CONSUMED` word only, and the + spec changing only that SC's `scConsumed` (`σ' = updKSc σ …`, so every other object, the machine state and every + scalar are kept). +* **`update_consumed_tcbA`**: the same for the SC of a TCB `t ∈ Z.T` whose `SC` word is not null (the shape of the + IPC line's `TimeoutScOpA`, ruling item 1's fallback statement). + +* `fl4_update_consumed_refinesA`: the same update on the FL4 cap and + conversion, using [P] `Fl4TicksToUsAgreeA`. + +Legacy hypotheses (x86's): `hmax : maxTicksToUs = U64::MAX` (FL4 caps the consumed time +at `U64::MAX` ticks; the spec's `maxTicksToUs` is opaque — see the gen-9 handoff: C seL4's AArch64 +`getMaxTicksToUs()` is `UINT64_MAX / ticks-per-µs`, so this fixes the opaque constant to FL4's choice), `htu` +(FL4's `ticks_to_us` is the spec's `ticksToUs`). +-/ +import Fl4ProofsA64.P3.ScTailA +import Fl4SpecA64.Ext.SchedControl +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **One SC object replaced, its refills / max / period / TCB kept** (module docstring). -/ +theorem SchedSRA.scObjA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s s' : S} + {σ : Fl4SpecA64.State Unit} (H : SchedSRA st Z m κ s σ) {sc : U64} (hc : sc ∈ Z.C) + {x x' : Fl4SpecA64.SchedContext} {n : Nat} (hk : σ.kheap (scNameA m sc) = some (.schedContext x n)) + (hw : WritesIn st s s' (ScObjA sc n)) (hx' : ScRelWA st (IdleScA m sc) s' sc x' n) + (e1 : x'.scRefills = x.scRefills) (e2 : x'.scRefillMax = x.scRefillMax) (e3 : x'.scPeriod = x.scPeriod) + (e4 : x'.scTcb = x.scTcb) (hcc : L.mem s' (scCoreA sc) = L.mem s (scCoreA sc)) : + SchedSRA st Z m κ s' (updKSc σ (scNameA m sc) (.schedContext x' n)) := by + set σ' := updKSc σ (scNameA m sc) (.schedContext x' n) with hσ' + have hr56 : layout.node.REPROGRAM.val = 56 := by unfold layout.node.REPROGRAM; rfl + have hw' : WritesIn st s s' (fun b => ScFootA Z.C m σ b ∨ b = L.nodeAt s (L.cpu s) layout.node.REPROGRAM) := + hw.mono fun b hb => Or.inl ⟨sc, hc, x, n, hk, hb⟩ + obtain ⟨nK, gK, tK⟩ := H.keepW hw' (by rw [hr56]; omega) (fun b hb => hb) + have hkN' : σ'.kheap (scNameA m sc) = some (.schedContext x' n) := by simp [σ', updKSc] + have hne : ∀ q o, σ.kheap q = some o → (∀ y k, o ≠ .schedContext y k) → q ≠ scNameA m sc := + fun q o hq ho e => by rw [e, hk] at hq; cases hq; exact ho x n rfl + have hobj : ∀ q o, σ.kheap q = some o → (∀ y k, o ≠ .schedContext y k) → σ'.kheap q = some o := + fun q o hq ho => by simp [σ', updKSc, hne q o hq ho, hq] + have hkt : ∀ q u, σ.kheap q = some (.tcb u) → σ'.kheap q = some (.tcb u) := + fun q u hq => hobj q _ hq (fun _ _ h => by cases h) + have hkt' : ∀ q u, σ'.kheap q = some (.tcb u) → σ.kheap q = some (.tcb u) := by + intro q u hq + by_cases e : q = scNameA m sc + · rw [e] at hq; simp [σ', updKSc] at hq + · simpa [σ', updKSc, e] using hq + have hSC : ∀ p y k, σ'.kheap p = some (.schedContext y k) → + σ.kheap p = some (.schedContext y k) ∨ (p = scNameA m sc ∧ y = x' ∧ k = n) := by + intro p y k hp + by_cases e : p = scNameA m sc + · rw [e] at hp; simp [σ', updKSc] at hp; exact Or.inr ⟨e, hp.1.symm, hp.2.symm⟩ + · left; simpa [σ', updKSc, e] using hp + have hact' : Fl4SpecA64.scActive x' = Fl4SpecA64.scActive x := by + simp [Fl4SpecA64.scActive, Fl4SpecA64.activeSc, e2] + have hhd : Fl4SpecA64.refillHd x' = Fl4SpecA64.refillHd x := by simp [Fl4SpecA64.refillHd, e1] + -- `specRtA` of a TCB bound to an active SC + have hrt : ∀ u xu p y k, σ.kheap u = some (.tcb xu) → xu.tcbSchedContext = some p → + σ.kheap p = some (.schedContext y k) → Fl4SpecA64.scActive y = true → specRtA σ' u = specRtA σ u := by + intro u xu p y k hu hp hy ha + have hne0 := H.inv.ne hy ha + rw [specRtA_of hu hp hy hne0] + by_cases e : p = scNameA m sc + · subst e; rw [hk] at hy; cases hy + rw [specRtA_of (hkt _ _ hu) hp hkN' (by rw [e1]; exact hne0), hhd] + · rw [specRtA_of (hkt _ _ hu) hp (by simpa [σ', updKSc, e] using hy) hne0] + have hinv : ScInvA m σ' := by + refine ⟨fun p y k hy ha hp => ?_, fun p y k hy ha hp => ?_, fun q z hz => H.inv.ntfnIdle q z ?_, + fun q z hz => H.inv.ntfnIdleOf q z ?_, fun u hu => ?_, ?_, fun u xu p hu hp => ?_, + fun u xu p hu hp => H.inv.tcbScNotIdle u xu p (hkt' _ _ hu) hp⟩ + · rcases hSC p y k hy with h | ⟨-, ey, -⟩ + · exact H.inv.refills p y k h ha hp + · rw [ey] at ha hp ⊢ + have := H.inv.refills _ x n hk (by rw [← hact']; exact ha) (by rw [← e3]; exact hp) + unfold RefOKA at this ⊢; rw [e1, hhd]; exact this + · rcases hSC p y k hy with h | ⟨ep, ey, -⟩ + · exact H.inv.rr p y k h ha hp + · subst ep; rw [ey] at ha hp ⊢ + have := H.inv.rr _ x n hk (by rw [← hact']; exact ha) (by rw [← e3]; exact hp) + unfold RRokA at this ⊢; rw [e1, hhd]; exact this + · by_cases e : q = scNameA m sc + · rw [e] at hz; simp [σ', updKSc] at hz + · simpa [σ', updKSc, e] using hz + · by_cases e : q = scNameA m sc + · rw [e] at hz; simp [σ', updKSc] at hz + · simpa [σ', updKSc, e] using hz + · obtain ⟨xu, p, y, k, h1, h2, h3, h4⟩ := H.inv.relSc u hu + by_cases e : p = scNameA m sc + · rw [e, hk] at h3; cases h3 + exact ⟨xu, p, x', n, hkt _ _ h1, h2, by rw [e]; exact hkN', by rw [hact']; exact h4⟩ + · exact ⟨xu, p, y, k, hkt _ _ h1, h2, by simpa [σ', updKSc, e] using h3, h4⟩ + · show σ.releaseQueue.Pairwise _ + refine List.Pairwise.imp_of_mem (fun {a b} ha hb hab => ?_) H.inv.sorted + obtain ⟨xa, pa, ya, ka, a1, a2, a3, a4⟩ := H.inv.relSc a ha + obtain ⟨xb, pb, yb, kb, b1, b2, b3, b4⟩ := H.inv.relSc b hb + rw [hrt a xa pa ya ka a1 a2 a3 a4, hrt b xb pb yb kb b1 b2 b3 b4]; exact hab + · obtain ⟨y, k, hy, hyu⟩ := H.inv.back u xu p (hkt' _ _ hu) hp + by_cases e : p = scNameA m sc + · subst e; rw [hk] at hy; cases hy + exact ⟨x', n, by simp [σ', updKSc], by rw [e4]; exact hyu⟩ + · exact ⟨y, k, by simpa [σ', updKSc, e] using hy, hyu⟩ + have hrd : RdA σ' := by + intro u xu hu hc x0 scp y k h1 h2 h3 h4 h5 + have hu0 := hkt' _ _ hu + have h10 := hkt' _ _ h1 + by_cases e : scp = scNameA m sc + · rw [e, hkN'] at h3; cases h3 + have := H.rd u xu hu0 hc x0 _ x n h10 (by rw [h2, e]) hk (by rw [← hact']; exact h4) h5 + unfold releasedSc at this ⊢; rw [hhd]; exact this + · exact H.rd u xu hu0 hc x0 scp y k h10 h2 (by simpa [σ', updKSc, e] using h3) h4 h5 + have hkc : ∀ c ∈ Z.C, ∀ y k, σ.kheap (scNameA m c) = some (.schedContext y k) → + ∃ y', σ'.kheap (scNameA m c) = some (.schedContext y' k) := by + intro c _ y k hy + by_cases e : scNameA m c = scNameA m sc + · rw [e, hk] at hy; cases hy; exact ⟨x', by rw [e]; simp [σ', updKSc]⟩ + · exact ⟨y, by simpa [σ', updKSc, e] using hy⟩ + obtain ⟨hS, hP⟩ := H.scr.upd H.scp hc hk hw hx' + have tw : ∀ t ∈ Z.T, ∀ K : U64, (K = 3520#u64 ∨ K = 3448#u64 ∨ K = 3432#u64 ∨ K = 3464#u64 ∨ K = 3592#u64 ∨ + K = 3480#u64 ∨ K = 3488#u64) → L.mem s' (wa t K) = L.mem s (wa t K) := fun t ht K hK => + tK t ht K (by rcases hK with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> simp) + (by rcases hK with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> simp) + have hcore : SchedCoreA st Z.T Z.Cs s' Z.Q Z.R := + H.core.agree hw.1 (fun u hu => ⟨tw u hu 3480#u64 (by simp), tw u hu 3488#u64 (by simp)⟩) + (fun c hc o h1 h2 h3 => nK c hc o h1 h2 (by rw [hr56]; omega)) (fun u hu => tw u hu 3592#u64 (by simp)) + (fun u hu => tw u hu 3520#u64 (by simp)) (fun u hu => tw u hu 3448#u64 (by simp)) + refine H.rebuildS (Q' := Z.Q) hw.1 hw.2.1 (fun c hc o h1 h2 h3 h4 => nK c hc o (by omega) h2 (by rw [hr56]; exact h4)) + (fun K hK => gK K (by rcases hK with rfl | rfl <;> simp) (by rcases hK with rfl | rfl <;> simp)) + (fun t ht K hK => tw t ht K (by rcases hK with h | h | h | h <;> simp [h])) hS hP + (fun t ht u hu => ⟨u, hkt _ u hu, rfl, rfl, rfl⟩) ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ + (by + show σ.reprogramTimer = _ + rw [hw.mem_eq H.reprA.1 (fun hb => H.reprA.2 _ ⟨sc, hc, x, n, hk, hb⟩ rfl)] + exact H.rest.repr) + (by + show σ.schedulerAction = _ + rw [nK _ H.sch.cpuCs layout.node.SCHED_ACTION (by simp [layout.node.SCHED_ACTION]) + (by simp [layout.node.SCHED_ACTION]) (by rw [hr56]; simp [layout.node.SCHED_ACTION])] + exact H.sch.act) + H.actOn hrd hinv hcore (fun p hp => H.rest.rq p hp) H.rest.rqDom rfl + ⟨fun d p u hu => let ⟨w, h1, h2⟩ := H.run.1 d p u hu; ⟨w, hkt _ w h1, h2⟩, + fun u hu => let ⟨w, h1, h2⟩ := H.run.2 u hu; ⟨w, hkt _ w h1, h2⟩⟩ + (fun c' hc' => by + by_cases e : c' = sc + · rw [e]; exact hcc + obtain ⟨y', k, hky, hy⟩ := H.scr.sc c' hc' + have h16 := hy.hdr (K := 16#u64) (by simp) (by simp) + have hcE : scCoreA c' = scWA c' 16#u64 := by unfold scCoreA scWA layout.sc.CORE; rfl + refine hw.mem_eq (H.scCoreFoot hc').1 (fun ho => ?_) + rw [hcE] at ho; exact H.scp.disj c' hc' sc hc e y' k x n hky hk _ (hy.in_obj h16.2) ho) + +end + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +/-- `updateConsumedWith_refinesA` for any relation `Rel` closed under replacing the SC object (Lift 2b: also +`SchedSRB`, `SchedSRB.scObjBA`). -/ +theorem updateConsumedWith_genA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} + {σ : Fl4SpecA64.State Unit} (H : SchedSRN st Z m κ s σ) {Rel : S → Fl4SpecA64.State Unit → Prop} (a : A) {sc : U64} (hc : sc ∈ Z.C) + (hcl : ∀ {s' : S} {x x' : Fl4SpecA64.SchedContext} {n : Nat}, σ.kheap (scNameA m sc) = some (.schedContext x n) → + WritesIn st s s' (ScObjA sc n) → ScRelWA st (IdleScA m sc) s' sc x' n → x'.scRefills = x.scRefills → + x'.scRefillMax = x.scRefillMax → x'.scPeriod = x.scPeriod → x'.scTcb = x.scTcb → + L.mem s' (scCoreA sc) = L.mem s (scCoreA sc) → Rel s' (updKSc σ (scNameA m sc) (.schedContext x' n))) + (cap : Fl4SpecA64.Ticks) (toUs : Fl4SpecA64.Ticks → Fl4SpecA64.Word64) + (hmax : cap = core.num.U64.MAX.bv) + (htu : ∀ v, ar.ticks_to_us a v ⦃ r => r.bv = toUs v.bv ⦄) : + schedcontext.update_consumed st ar s a sc ⦃ r => ∃ x n v, + σ.kheap (scNameA m sc) = some (.schedContext x n) ∧ + Fl4SpecA64.schedContextUpdateConsumedWith (Z := Unit) cap toUs (scNameA m sc) σ = + ({(r.1.bv, updKSc σ (scNameA m sc) (.schedContext { x with scConsumed := v.bv } n))}, False) ∧ + r.2 = L.wr s (scWA sc 8#u64) v ∧ WritesIn st s r.2 (ScObjA sc n) ∧ + Rel r.2 (updKSc σ (scNameA m sc) (.schedContext { x with scConsumed := v.bv } n)) ⦄ := by + obtain ⟨x, n, hk, hx⟩ := H.scr.sc sc hc + have hsp : Fl4SpecA64.schedContextUpdateConsumedWith (Z := Unit) cap toUs (scNameA m sc) σ = + (if cap ≤ x.scConsumed then + (do + Fl4SpecA64.setScObjRef Fl4SpecA64.scConsumedUpdate (scNameA m sc) (x.scConsumed - cap) + return toUs cap) σ + else (do + Fl4SpecA64.setScObjRef Fl4SpecA64.scConsumedUpdate (scNameA m sc) 0 + return toUs x.scConsumed) σ) := by + unfold Fl4SpecA64.schedContextUpdateConsumedWith + rw [nd_bind_oneSc (getSchedContext_eqA hk), nd_pure_bindSc] + split_ifs <;> rfl + have fin : ∀ (v : U64) (w : Fl4SpecA64.Ticks), v.bv = w → ∀ (r : U64) (tm : Fl4SpecA64.Ticks), r.bv = tm → + (Fl4SpecA64.setScObjRef Fl4SpecA64.scConsumedUpdate (scNameA m sc) w >>= fun _ => pure tm) σ = + ({(r.bv, updKSc σ (scNameA m sc) (.schedContext { x with scConsumed := v.bv } n))}, False) ∧ + WritesIn st s (L.wr s (scWA sc 8#u64) v) (ScObjA sc n) ∧ + Rel (L.wr s (scWA sc 8#u64) v) + (updKSc σ (scNameA m sc) (.schedContext { x with scConsumed := v.bv } n)) := by + intro v w hv r tm hr + obtain ⟨hx', hw⟩ := hx.set_consumed v + have h8 := hx.hdr (K := 8#u64) (by simp) (by simp) + have h16 := hx.hdr (K := 16#u64) (by simp) (by simp) + obtain ⟨-, -, hfit⟩ := hx.bounds + have hcc : L.mem (L.wr s (scWA sc 8#u64) v) (scCoreA sc) = L.mem s (scCoreA sc) := by + have hcE : scCoreA sc = scWA sc 16#u64 := by unfold scCoreA scWA layout.sc.CORE; rfl + rw [hcE] + refine L.mem_wr_ne h8.1 h16.1 ?_ + rw [scWA_val (sc := sc) (K := 8#u64) (by simp; omega), scWA_val (sc := sc) (K := 16#u64) (by simp; omega)] + simp + refine ⟨?_, hw, hcl hk hw hx' rfl rfl rfl rfl hcc⟩ + show (Fl4SpecA64.updateSchedContext (scNameA m sc) _ >>= _) σ = _ + rw [nd_bind_oneSc (updateSchedContext_eqA hk _), hr, hv]; rfl + unfold schedcontext.update_consumed + refine WP.spec_bind (obj.Sc.consumed.specSc s sc) ?_ + rintro cv rfl + have hxc : x.scConsumed = (L.mem s (scWA sc 8#u64)).bv := hx.consumed + by_cases hge : L.mem s (scWA sc 8#u64) ≥ core.num.U64.MAX + · simp only [hge, ↓reduceIte] + have hmv : (core.num.U64.MAX).val = 2 ^ 64 - 1 := by simp [core.num.U64.MAX, U64.rMax] + have hcm : L.mem s (scWA sc 8#u64) = core.num.U64.MAX := by + have := (L.mem s (scWA sc 8#u64)).hBounds + scalar_tac + rw [hcm] at hxc ⊢ + step as ⟨i, hi⟩ + have hi0 : i = 0#u64 := by scalar_tac + subst hi0 + refine WP.spec_bind (obj.Sc.set_consumed.specSc s sc _) ?_ + rintro s1 rfl + refine WP.spec_bind (htu _) ?_ + intro r hr + rw [WP.spec_ok] + obtain ⟨h1, h2, h3⟩ := fin 0#u64 (x.scConsumed - cap) + (by rw [hxc, hmax, BitVec.sub_self]; rfl) r _ (by rw [hr, ← hmax]) + refine ⟨x, n, 0#u64, hk, ?_, rfl, h2, h3⟩ + rw [hsp, if_pos (by rw [hxc, hmax]; exact BitVec.le_refl _)] + exact h1 + · simp only [hge, ↓reduceIte] + refine WP.spec_bind (obj.Sc.set_consumed.specSc s sc _) ?_ + rintro s1 rfl + refine WP.spec_bind (htu _) ?_ + intro r hr + rw [WP.spec_ok] + obtain ⟨h1, h2, h3⟩ := fin 0#u64 0 rfl r _ (by rw [hr, ← hxc]) + refine ⟨x, n, 0#u64, hk, ?_, rfl, h2, h3⟩ + rw [hsp, if_neg (by rw [hxc, hmax, ← u64_le_bvSc]; exact hge)] + exact h1 + +/-- The SC update is independent of the tick conversion supplied by the platform. -/ +theorem updateConsumedWith_refinesA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} + {σ : Fl4SpecA64.State Unit} (H : SchedSRA st Z m κ s σ) (a : A) {sc : U64} (hc : sc ∈ Z.C) + (cap : Fl4SpecA64.Ticks) (toUs : Fl4SpecA64.Ticks → Fl4SpecA64.Word64) + (hmax : cap = core.num.U64.MAX.bv) + (htu : ∀ v, ar.ticks_to_us a v ⦃ r => r.bv = toUs v.bv ⦄) : + schedcontext.update_consumed st ar s a sc ⦃ r => ∃ x n v, + σ.kheap (scNameA m sc) = some (.schedContext x n) ∧ + Fl4SpecA64.schedContextUpdateConsumedWith (Z := Unit) cap toUs (scNameA m sc) σ = + ({(r.1.bv, updKSc σ (scNameA m sc) (.schedContext { x with scConsumed := v.bv } n))}, False) ∧ + r.2 = L.wr s (scWA sc 8#u64) v ∧ WritesIn st s r.2 (ScObjA sc n) ∧ + SchedSRA st Z m κ r.2 (updKSc σ (scNameA m sc) (.schedContext { x with scConsumed := v.bv } n)) ⦄ := + updateConsumedWith_genA H.toSchedSRN a hc (fun hk hw hx' e1 e2 e3 e4 hcc => H.scObjA hc hk hw hx' e1 e2 e3 e4 hcc) + cap toUs hmax htu + +/-- **`update_consumed` refines `sched_context_update_consumed`** (module docstring). -/ +theorem update_consumed_refinesA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} + {σ : Fl4SpecA64.State Unit} (H : SchedSRA st Z m κ s σ) (a : A) {sc : U64} (hc : sc ∈ Z.C) + (hmax : Fl4SpecA64.maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ v, ar.ticks_to_us a v ⦃ r => r.bv = Fl4SpecA64.ticksToUs v.bv ⦄) : + schedcontext.update_consumed st ar s a sc ⦃ r => ∃ x n v, + σ.kheap (scNameA m sc) = some (.schedContext x n) ∧ + Fl4SpecA64.schedContextUpdateConsumed (Z := Unit) (scNameA m sc) σ = + ({(r.1.bv, updKSc σ (scNameA m sc) (.schedContext { x with scConsumed := v.bv } n))}, False) ∧ + r.2 = L.wr s (scWA sc 8#u64) v ∧ WritesIn st s r.2 (ScObjA sc n) ∧ + SchedSRA st Z m κ r.2 (updKSc σ (scNameA m sc) (.schedContext { x with scConsumed := v.bv } n)) ⦄ := by + simpa only [Fl4SpecA64.schedContextUpdateConsumedWith_eq_l4v] using + updateConsumedWith_refinesA H a hc Fl4SpecA64.maxTicksToUs Fl4SpecA64.ticksToUs hmax htu + +/-- [P] AArch64 `Arch.ticks_to_us` returns the FL4 conversion of its input. -/ +def Fl4TicksToUsAgreeA (ar : machine.Arch A) (a : A) : Prop := + ∀ v, ar.ticks_to_us a v ⦃ r => r.bv = Fl4SpecA64.fl4TicksToUs v.bv ⦄ + +/-- Concrete consumed-time read/reset against the FL4 timer and cap. -/ +theorem fl4_update_consumed_refinesA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} + {σ : Fl4SpecA64.State Unit} (H : SchedSRA st Z m κ s σ) (a : A) {sc : U64} (hc : sc ∈ Z.C) + (htu : Fl4TicksToUsAgreeA ar a) : + schedcontext.update_consumed st ar s a sc ⦃ r => ∃ x n v, + σ.kheap (scNameA m sc) = some (.schedContext x n) ∧ + Fl4SpecA64.fl4SchedContextUpdateConsumed (Z := Unit) (scNameA m sc) σ = + ({(r.1.bv, updKSc σ (scNameA m sc) (.schedContext { x with scConsumed := v.bv } n))}, False) ∧ + r.2 = L.wr s (scWA sc 8#u64) v ∧ WritesIn st s r.2 (ScObjA sc n) ∧ + SchedSRA st Z m κ r.2 (updKSc σ (scNameA m sc) (.schedContext { x with scConsumed := v.bv } n)) ⦄ := by + have hcap : Fl4SpecA64.fl4MaxConsumedTicks = core.num.U64.MAX.bv := rfl + exact updateConsumedWith_refinesA H a hc _ _ hcap htu + +/-- **`update_consumed` of a TCB's SC** (module docstring; ruling item 1's statement). -/ +theorem update_consumed_tcbA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} + {σ : Fl4SpecA64.State Unit} (H : SchedSRA st Z m κ s σ) (a : A) {t : U64} (ht : t ∈ Z.T) + (h0 : L.mem s (scA t) ≠ 0#u64) + (hmax : Fl4SpecA64.maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ v, ar.ticks_to_us a v ⦃ r => r.bv = Fl4SpecA64.ticksToUs v.bv ⦄) : + (∃ x, σ.kheap (specPtr t) = some (.tcb x) ∧ x.tcbSchedContext = some (scNameA m (L.mem s (scA t)))) ∧ + schedcontext.update_consumed st ar s a (L.mem s (scA t)) ⦃ r => ∃ x n v, + σ.kheap (scNameA m (L.mem s (scA t))) = some (.schedContext x n) ∧ + Fl4SpecA64.schedContextUpdateConsumed (Z := Unit) (scNameA m (L.mem s (scA t))) σ = + ({(r.1.bv, updKSc σ (scNameA m (L.mem s (scA t))) (.schedContext { x with scConsumed := v.bv } n))}, False) ∧ + r.2 = L.wr s (scWA (L.mem s (scA t)) 8#u64) v ∧ WritesIn st s r.2 (ScObjA (L.mem s (scA t)) n) ∧ + SchedSRA st Z m κ r.2 + (updKSc σ (scNameA m (L.mem s (scA t))) (.schedContext { x with scConsumed := v.bv } n)) ⦄ := by + obtain ⟨x, hkt, hsct⟩ := H.sch.tcb t ht + refine ⟨⟨x, hkt, by rw [hsct]; unfold optScA; rw [if_neg h0]; rfl⟩, ?_⟩ + exact update_consumed_refinesA H a (H.rest.scMem t ht h0) hmax htu + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScConsumeMsgA.lean b/proofs/Fl4ProofsA64/P3/ScConsumeMsgA.lean new file mode 100644 index 000000000..5460438ce --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScConsumeMsgA.lean @@ -0,0 +1,314 @@ +/- +P3 (AArch64): the Consumed scheduler-context invocation with a call reply. +The single result word goes in the first message register, so the concrete +reply frame has three caller-register words and no IPC-buffer writes. +-/ +import Fl4ProofsA64.P3.ScConsumeOpA +import Fl4ProofsA64.P3.ScInvokeTopA +import Fl4ProofsA64.P3.EpXferA +import Fl4ProofsA64.P1.QueueFinal +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section +open Fl4SpecA64 + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +local notation "wa" => core.num.U64.wrapping_add + +/-- The aligned words written by a one-word AArch64 call reply. -/ +def ScConsumedRegsA (t b : U64) : Prop := + b = wa t 256#u64 ∨ b = wa t 264#u64 ∨ b = wa t 272#u64 + +theorem tcbLowOffA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {σ : State Unit} (H : SchedSRN st Z m κ s σ) {t K : U64} + (ht : t ∈ Z.T) (hK : K.val ≤ 3328) : + ((wa t K).val < (L.ksb s).val ∨ (L.ksb s).val + ksSizeA ≤ (wa t K).val) ∧ + ¬ SReadA st Z m s σ (wa t K) := by + have htA := H.core.tcbs t ht + have hfit := htA.fits + have haddr := field_val htA (show K.val < 4096 by omega) + have hks := H.tcb_ks ht + have hks' : (wa t K).val < (L.ksb s).val ∨ (L.ksb s).val + ksSizeA ≤ (wa t K).val := by + rw [haddr]; omega + have glob : ∀ k : U64, k.val < ksSizeA → wa (L.ksb s) k ≠ wa t K := by + intro k hk eq + have hfit := H.ksFit + have hval : (wa (L.ksb s) k).val = (L.ksb s).val + k.val := by rw [wa_valm]; omega + rw [← eq, hval] at hks' + omega + exact ⟨hks', fun h => by + rcases h with ⟨c, hc, o, ho, e⟩ | e | e | ⟨u, hu, k, hk, e⟩ | h + · have hin := H.node_in hc ho + rw [← e] at hin + omega + · exact glob _ (by simp [ksSizeA]) e.symm + · exact glob _ (by simp [ksSizeA]) e.symm + · have hne : K.val ≠ k.val := by + rcases hk with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> simp <;> omega + exact field_ne htA (H.core.tcbs u hu) (by omega) + (by rcases hk with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> simp) (fun _ => hne) e + · have hn := H.scp.th_out h ((H.thT t).2 ht) + simp only [ObjKindA.size] at hn + rw [haddr] at hn + omega⟩ + +theorem setRegEqA (s : S) {t r K v : U64} (ht : TcbAddr t) + (hK : K.val = 256 + 8 * r.val) (hr : r.val < 384) : + obj.Tcb.set_reg st t s r v ⦃ s' => s' = L.wr s (wa t K) v ⦄ := + WP.spec_mono (obj.Tcb.set_reg.wrK s t r v ht hr) fun _ ⟨k, hk, e⟩ => by + rw [e, show k = K from UScalar.eq_of_val_eq (hk.trans hK.symm)] + +theorem setRegFrameA (s : S) {t r K v : U64} (ht : TcbAddr t) + (hK : K.val = 256 + 8 * r.val) (hr : r.val < 384) (ha : Aligned (wa t K)) : + obj.Tcb.set_reg st t s r v ⦃ s' => WritesIn st s s' (· = wa t K) ⦄ := + WP.spec_mono (obj.Tcb.set_reg.wrK s t r v ht hr) fun _ ⟨k, hk, e⟩ => by + have eq : k = K := UScalar.eq_of_val_eq (hk.trans hK.symm) + rw [e, eq] + exact writesIn_wr ha + +private theorem consumedRegsKeepA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s s' : S} {σ : State Unit} (H : SchedSRA st Z m κ s σ) (hs : StEqA Z.T (L.mem s) σ) + {t : U64} (ht : t ∈ Z.T) (w : WritesIn st s s' (ScConsumedRegsA t)) : + SchedSRA st Z m κ s' σ ∧ StEqA Z.T (L.mem s') σ := by + have hS : SStepA σ σ := ⟨SFixA.refl _, rfl, rfl, rfl, fun _ _ _ _ => rfl, + fun _ x hx => ⟨x, hx, rfl, rfl, rfl⟩, fun _ _ _ _ => rfl, + fun _ x hx => ⟨x, hx⟩, fun _ x hx => ⟨x, hx, rfl⟩⟩ + refine ⟨H.stableR w ?_ hS H.run H.rd, ?_⟩ + · rintro b (rfl | rfl | rfl) <;> exact (tcbLowOffA H.toSchedSRN ht (by simp)).2 + · intro u hu x hx + have ha : Aligned (stA u) := by + rw [stA_eq] + exact field_aligned (H.core.tcbs u hu) (by simp) (by simp) + rw [w.mem_eq ha] + · exact hs u hu x hx + · unfold ScConsumedRegsA + rw [stA_eq] + rintro (h | h | h) <;> + exact field_ne (H.core.tcbs u hu) (H.core.tcbs t ht) (by simp) (by simp) (fun _ => by simp) h + +private theorem consumedStEqA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s s' : S} {σ σ' : State Unit} (H : SchedSRA st Z m κ s σ) + (hs : StEqA Z.T (L.mem s) σ) (F : MFrA (st := st) Z m s s' σ σ') : + StEqA Z.T (L.mem s') σ' := by + intro u hu y hy + obtain ⟨x, hx, -⟩ := H.sch.tcb u hu + obtain ⟨x', hx', o, he⟩ := F.keep.tcb _ _ hx + rw [hy] at hx' + cases hx' + have hT := H.core.tcbs u hu + have hK := tcbLowOffA (K := 3328#u64) H.toSchedSRN hu (by simp) + simp only [← stA_eq] at hK + have hal : Aligned (stA u) := by rw [stA_eq]; exact field_aligned hT (by simp) (by simp) + have hoff : ¬ MWrA (st := st) Z m σ s (stA u) := by + refine not_mwrA (fun h => hK.2 (Or.inr (Or.inr (Or.inr (Or.inr h))))) ?_ hK.1 + intro v hv + have ht := H.core.tcbs v hv + rw [stA_eq] + refine ⟨?_, ?_, ?_, ?_⟩ + · exact field_ne hT ht (K2 := 3464#u64) (by simp) (by simp) (fun _ => by simp) + · exact field_ne hT ht (K2 := 3480#u64) (by simp) (by simp) (fun _ => by simp) + · exact field_ne hT ht (K2 := 3488#u64) (by simp) (by simp) (fun _ => by simp) + · exact field_ne hT ht (K2 := 3592#u64) (by simp) (by simp) (fun _ => by simp) + rw [F.wr.mem_eq hal hoff, he] + exact hs u hu x hx + +/-- Relate the SC update and its single returned time word. The additional +concrete writes for a call are restricted to the caller's X0, X1 and X2. -/ +theorem sc_consumed_msg_frameWithA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} + {σ : State Unit} (H : SchedSRA st Z m κ s σ) (hst : StEqA Z.T (L.mem s) σ) + (a : A) {sc t : U64} (hc : sc ∈ Z.C) (ht : t ∈ Z.T) (call : Bool) + (cap : Ticks) (toUs : Ticks → Word64) (hmax : cap = core.num.U64.MAX.bv) + (htu : ∀ v, ar.ticks_to_us a v ⦃ r => r.bv = toUs v.bv ⦄) : + schedcontext.set_consumed st ar s a sc t call ⦃ s' => ∃ (tm : Ticks) (σ' : State Unit), + returnConsumedWith (Z := Unit) cap toUs (scNameA m sc) σ = + ({(wordsFromTime tm, σ')}, False) ∧ + SchedSRA st Z m κ s' σ' ∧ StEqA Z.T (L.mem s') σ' ∧ SSzA σ σ' ∧ + (∀ u x, σ.kheap (specPtr u) = some (.tcb x) → σ'.kheap (specPtr u) = some (.tcb x)) ∧ + WritesIn st s s' (fun b => MWrA (st := st) Z m σ s b ∨ (call = true ∧ ScConsumedRegsA t b)) ∧ + (call = true → L.mem s' (wa t 256#u64) = 0#u64 ∧ + (L.mem s' (wa t 264#u64)).bv = messageInfoToData ⟨natToLen 1, 0, 0, 0⟩ ∧ + (L.mem s' (wa t 272#u64)).bv = ucast 64 tm) ⦄ := by + unfold schedcontext.set_consumed + refine WP.spec_bind (updateConsumedWith_refinesA H a hc cap toUs hmax htu) ?_ + rintro ⟨time, s1⟩ ⟨xs, n, v, hks, hc', -, hw, H1⟩ + let σ1 := updKSc σ (scNameA m sc) (.schedContext { xs with scConsumed := v.bv } n) + have hkeep : ∀ q o, σ.kheap q = some o → (∀ x k, o ≠ .schedContext x k) → σ1.kheap q = some o := by + intro q o hq hno + have hne : q ≠ scNameA m sc := fun e => by rw [e, hks] at hq; exact hno _ _ (Option.some.inj hq).symm + rw [show σ1.kheap = _ from updKSc_kheap _ _ _, Function.update_of_ne hne] + exact hq + have F : MFrA (st := st) Z m s s1 σ σ1 := + ⟨hw.mono fun _ h => mwr_scObj hc hks h, SKeepA.of_obj hkeep, SSzA.of_upd hks (updKSc_kheap _ _ _)⟩ + have hi : returnConsumedWith (Z := Unit) cap toUs (scNameA m sc) σ = + ({(wordsFromTime time.bv, σ1)}, False) := by + unfold returnConsumedWith + rw [nd_bind_oneSc hc'] + rfl + have hs1 := consumedStEqA H hst F + have hk : ∀ u x, σ.kheap (specPtr u) = some (.tcb x) → σ1.kheap (specPtr u) = some (.tcb x) := + fun u x h => hkeep (specPtr u) (.tcb x) h (fun _ _ e => by cases e) + try simp only [WP.uncurry'_pair] + split + · rename_i hcall + refine WP.spec_bind (P0.msg.lookup_ipc_buffer.p0 st s1 true t L.total) fun buf _ => ?_ + have htA := H.core.tcbs t ht + have hal : ∀ k : U64, k.val < 4096 → k.val % 8 = 0 → Aligned (wa t k) := + fun _ hk ha => field_aligned htA hk ha + refine WP.spec_bind (setRegEqA (K := 256#u64) s1 htA + (by simp [layout.reg.BADGE, layout.reg.X0]) (by simp [layout.reg.BADGE, layout.reg.X0])) fun s2 e0 => ?_ + have w0 : WritesIn st s1 s2 (· = wa t 256#u64) := by + rw [e0]; exact writesIn_wr (hal _ (by simp) (by simp)) + unfold msg.set_mr + simp only [show (0#u64 < msg.N_MSG_REGISTERS) = True from by simp [msg.N_MSG_REGISTERS], + if_true, msg.msg_register, Aeneas.Std.bind_ok] + refine WP.spec_bind (Pₘ := fun r : U64 × S => r.1 = core.num.U64.wrapping_add 0#u64 1#u64 ∧ + r.2 = L.wr s2 (wa t 272#u64) time) ?_ ?_ + · refine WP.spec_bind (setRegEqA (K := 272#u64) s2 htA + (by simp [layout.reg.X2]) (by simp [layout.reg.X2])) fun s3 e2 => ?_ + simp only [lift, Aeneas.Std.bind_ok, WP.spec_ok] + exact ⟨trivial, e2⟩ + rintro ⟨len, s3⟩ ⟨elen, e2⟩ + try simp only [WP.uncurry'_pair] + dsimp only at elen e2 + have w2 : WritesIn st s2 s3 (· = wa t 272#u64) := by + rw [e2]; exact writesIn_wr (hal _ (by simp) (by simp)) + unfold msginfo.MessageInfo.new msg.set_msg_info + simp only [Aeneas.Std.bind_ok] + have hlen : len.val = 1 := by rw [elen]; rfl + refine WP.spec_bind (Endp.to_word_specA 0#u64 len (by omega)) fun w hw => ?_ + refine WP.spec_mono (setRegEqA (K := 264#u64) s3 htA + (by simp [layout.reg.MSG_INFO, layout.reg.X1]) (by simp [layout.reg.MSG_INFO, layout.reg.X1])) fun s4 e1 => ?_ + have w1 : WritesIn st s3 s4 (· = wa t 264#u64) := by + rw [e1]; exact writesIn_wr (hal _ (by simp) (by simp)) + have wr : WritesIn st s1 s4 (ScConsumedRegsA t) := + (w0.trans (w2.trans w1)).mono fun _ h => by + rcases h with e | e | e + · exact Or.inl e + · exact Or.inr (Or.inr e) + · exact Or.inr (Or.inl e) + obtain ⟨H4, hst4⟩ := consumedRegsKeepA H1 hs1 ht wr + have a256 : Aligned (wa t 256#u64) := hal _ (by simp) (by simp) + have a264 : Aligned (wa t 264#u64) := hal _ (by simp) (by simp) + have a272 : Aligned (wa t 272#u64) := hal _ (by simp) (by simp) + have ne : ∀ i j : U64, i.val < 4096 → j.val < 4096 → i.val ≠ j.val → wa t i ≠ wa t j := fun i j hi' hj h e => by + have := congrArg UScalar.val e + rw [tcb_wadd_val htA hi', tcb_wadd_val htA hj] at this + omega + have v256 : L.mem s4 (wa t 256#u64) = 0#u64 := by + rw [w1.mem_eq a256 (ne _ _ (by simp) (by simp) (by simp)), + w2.mem_eq a256 (ne _ _ (by simp) (by simp) (by simp)), e0] + exact L.mem_wr_same a256 + have v272 : L.mem s4 (wa t 272#u64) = time := by + rw [w1.mem_eq a272 (ne _ _ (by simp) (by simp) (by simp)), e2] + exact L.mem_wr_same a272 + have v264 : L.mem s4 (wa t 264#u64) = w := by + rw [e1]; exact L.mem_wr_same a264 + refine ⟨time.bv, σ1, hi, H4, hst4, F.sz, hk, + (F.wr.trans wr).mono fun _ h => h.imp id (fun e => ⟨hcall, e⟩), fun _ => ⟨v256, ?_, ?_⟩⟩ + · have e1' : len.bv = natToLen 1 := by + rw [show len = 1#u64 from UScalar.eq_of_val_eq (by rw [hlen]; rfl)]; rfl + have z0 : (0#u64 : U64).bv &&& Fl4SpecA64.mask Fl4SpecA64.msgLabelBits = 0 := by + rw [show (0#u64 : U64).bv = 0#64 from rfl, BitVec.zero_and]; rfl + rw [v264, hw, e1', z0] + · rw [v272] + exact (BitVec.setWidth_eq _).symm + · rename_i hcall + exact (WP.spec_ok _).2 ⟨time.bv, σ1, hi, H1, hs1, F.sz, hk, F.wr.mono fun _ h => Or.inl h, + fun h => absurd h hcall⟩ + +/-- The legacy Consumed invocation uses the port's cap and conversion. -/ +theorem sc_consumed_msg_frameA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} + {σ : State Unit} (H : SchedSRA st Z m κ s σ) (hst : StEqA Z.T (L.mem s) σ) + (a : A) {sc t : U64} (hc : sc ∈ Z.C) (ht : t ∈ Z.T) (call : Bool) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) : + schedcontext.set_consumed st ar s a sc t call ⦃ s' => ∃ (tm : Ticks) (σ' : State Unit), + invokeSchedContext (Z := Unit) (.invokeSchedContextConsumed (scNameA m sc)) σ = + ({(wordsFromTime tm, σ')}, False) ∧ + SchedSRA st Z m κ s' σ' ∧ StEqA Z.T (L.mem s') σ' ∧ SSzA σ σ' ∧ + (∀ u x, σ.kheap (specPtr u) = some (.tcb x) → σ'.kheap (specPtr u) = some (.tcb x)) ∧ + WritesIn st s s' (fun b => MWrA (st := st) Z m σ s b ∨ (call = true ∧ ScConsumedRegsA t b)) ⦄ := by + refine WP.spec_mono (sc_consumed_msg_frameWithA H hst a hc ht call _ _ hmax htu) ?_ + rintro s' ⟨tm, τ, he, hs, hSt, sz, k, w, _⟩ + rw [returnConsumedWith_eq_l4v] at he + refine ⟨tm, τ, ?_, hs, hSt, sz, k, w⟩ + change returnConsumed (Z := Unit) (scNameA m sc) σ = _ + exact he + +/-- The concrete Consumed call, from the initial stall through Restart, +SC consumption and the caller's Running transition. -/ +theorem sc_decode_consumed_callA (hA : P0.ArchTotal ar) + {Z : XSetsA} {m : McState} {κ : U64 → U64} {s s1 : S} {σ : State Unit} + (H : SchedSRA st Z m κ s σ) (hst : StEqA Z.T (L.mem s) σ) + {a a1 : A} {sc target ct : U64} (hc : sc ∈ Z.C) (hct : ct ∈ Z.T) + {xc : Tcb} (hkc : σ.kheap (specPtr ct) = some (.tcb xc)) (hr : runnable xc.tcbState = true) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ v, ar.ticks_to_us a1 v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (ht : obj.Sc.tcb st sc s = ok target) + (he : (if target != 0#u64 then smp.remote_tcb_stall st ar s a target else ok (s, a)) = ok (s1, a1)) + (hq : target != 0#u64 → smp.remote_tcb_stall st ar s a target = ok (s, a)) + (hcur : sched.cur_thread st s1 = ok ct) + (call : Bool) (extra : Aeneas.Std.Array obj.Slot 3#usize) {label : Data} + (hl : genInvocationType label = .schedContextConsumed) (caps : List Cap) : + schedcontext.decode st ar s a fl4_abi.label.SchedContextConsumed sc call extra ⦃ r => + r.1 = .Ok () ∧ r.2.2 = a1 ∧ ∃ tm σ2 σ3 σ4, + (decodeSchedContextInvocation (Z := Unit) label (scNameA m sc) caps).run σ = + ({(.ok (.invokeSchedContextConsumed (scNameA m sc)), σ)}, False) ∧ + setThreadState (Z := Unit) (specPtr ct) .restart σ = ({((), σ2)}, False) ∧ + invokeSchedContext (Z := Unit) (.invokeSchedContextConsumed (scNameA m sc)) σ2 = + ({(wordsFromTime tm, σ3)}, False) ∧ + setThreadState (Z := Unit) (specPtr ct) .running σ3 = ({((), σ4)}, False) ∧ + SchedSRA st Z m κ r.2.1 σ4 ∧ StEqA Z.T (L.mem r.2.1) σ4 ∧ + WritesIn st s r.2.1 (fun b => MWrA (st := st) Z m σ s b ∨ b = stA ct ∨ + (call = true ∧ ScConsumedRegsA ct b)) ⦄ := by + obtain ⟨H1, hs1, F0⟩ := sc_stall_entryA hA H hst he hq + rw [sc_decode_consumed_eqA, ht, bind_ok, he, bind_ok] + change (do + let c ← sched.cur_thread st s1 + let s2 ← sched.set_thread_state st s1 c sched.TS_RESTART + let s3 ← schedcontext.set_consumed st ar s2 a1 sc c call + let s4 ← sched.set_thread_state st s3 c sched.TS_RUNNING + ok (core.result.Result.Ok (), s4, a1)) ⦃ _ => _ ⦄ + rw [hcur, bind_ok] + have hvr : sched.TS_RESTART.val = tsCodeA .restart := by rw [ts_vals.2.1]; rfl + have hvn : sched.TS_RUNNING.val = tsCodeA .running := by rw [ts_vals.1]; rfl + refine WP.spec_bind (sc_set_runnable_stateA H1 hs1 hct hkc hr (ts := .restart) rfl hvr) ?_ + rintro s2 ⟨σ2, hR, H2, hs2, hk2, w1⟩ + have hkc2 : σ2.kheap (specPtr ct) = some (.tcb { xc with tcbState := .restart }) := by + rw [hk2, updKSc_kheap, Function.update_self] + have hsz2 : SSzA σ σ2 := by + intro p x n h + rw [hk2, updKSc_kheap] at h + by_cases eq : p = specPtr ct + · rw [eq, Function.update_self] at h + cases h + · rw [Function.update_of_ne eq] at h + exact ⟨x, h⟩ + refine WP.spec_bind (sc_consumed_msg_frameA (ar := ar) H2 hs2 a1 hc hct call hmax htu) ?_ + rintro s3 ⟨tm, σ3, hI, H3, hs3, hsz3, hkeep, w2⟩ + refine WP.spec_bind + (sc_set_runnable_stateA H3 hs3 hct (hkeep ct _ hkc2) rfl (ts := .running) rfl hvn) ?_ + rintro s4 ⟨σ4, hN, H4, hs4, -, w3⟩ + refine (WP.spec_ok _).2 ⟨rfl, rfl, tm, σ2, σ3, σ4, decodeSchedContextConsumed_eqA hl caps, + hR, hI, hN, H4, hs4, ?_⟩ + have w02 := F0.wr.trans w1 + have w03 := w02.trans w2 + refine (w03.trans w3).mono fun b h => ?_ + rcases h with ((h | h | e) | h | e) | h | e + · exact Or.inl h + · exact Or.inl (Or.inr (swr_ksb rfl F0.wr.1 h)) + · exact Or.inr (Or.inl e) + · exact Or.inl (mwr_back rfl rfl hsz2 w02.1 h) + · exact Or.inr (Or.inr e) + · exact Or.inl (Or.inr (swr_ksb rfl w03.1 h)) + · exact Or.inr (Or.inl e) + +end + +end +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScConsumeOpA.lean b/proofs/Fl4ProofsA64/P3/ScConsumeOpA.lean new file mode 100644 index 000000000..944122f9d --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScConsumeOpA.lean @@ -0,0 +1,62 @@ +/- +P3 (AArch64): the SC half of the Consumed invocation, porting the consumption +case of x86 `ScJointConsumed.sc_consumedReplyJ`. The boolean selects the no-message +path. The caller's restart/running transitions are external to the SC step. +No named premises beyond the existing time-conversion premises of `ScFrameA`. +-/ +import Fl4ProofsA64.P3.ScFrameA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +/-- The literal AArch64 decoder arm, including its stall, restart, and return to Running. -/ +theorem sc_decode_consumed_eqA {S A : Type} (st : machine.Store S) (ar : machine.Arch A) + (s : S) (a : A) (sc : U64) (call : Bool) (extra : Aeneas.Std.Array obj.Slot 3#usize) : + schedcontext.decode st ar s a fl4_abi.label.SchedContextConsumed sc call extra = (do + let t ← obj.Sc.tcb st sc s + let (s1, a1) ← if t != 0#u64 then smp.remote_tcb_stall st ar s a t else ok (s, a) + let ct ← sched.cur_thread st s1 + let s2 ← sched.set_thread_state st s1 ct sched.TS_RESTART + let s3 ← schedcontext.set_consumed st ar s2 a1 sc ct call + let s4 ← sched.set_thread_state st s3 ct sched.TS_RUNNING + ok (core.result.Result.Ok (), s4, a1)) := by + unfold schedcontext.decode + simp only [↓reduceIte] + +theorem decodeSchedContextConsumed_eqA {σ : Fl4SpecA64.State Unit} {sc : Fl4SpecA64.ObjRef} + {label : Fl4SpecA64.Data} (hl : Fl4SpecA64.genInvocationType label = .schedContextConsumed) + (caps : List Fl4SpecA64.Cap) : + (Fl4SpecA64.decodeSchedContextInvocation (Z := Unit) label sc caps).run σ = + ({(.ok (.invokeSchedContextConsumed sc), σ)}, False) := by + unfold Fl4SpecA64.decodeSchedContextInvocation + rw [hl] + rfl + +/-- In the no-message path, FL4 charges the SC and the spec returns the +converted time as message words. The SC post-state is exactly the same. -/ +theorem sc_consumed_no_msgA {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} {σ : Fl4SpecA64.State Unit} + (H : SchedSRA st Z m κ s σ) (a : A) {sc : U64} (hc : sc ∈ Z.C) (t : U64) + (hmax : Fl4SpecA64.maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ v, ar.ticks_to_us a v ⦃ r => r.bv = Fl4SpecA64.ticksToUs v.bv ⦄) : + schedcontext.set_consumed st ar s a sc t false ⦃ s' => ∃ (tm : Fl4SpecA64.Ticks) (σ' : Fl4SpecA64.State Unit), + Fl4SpecA64.invokeSchedContext (Z := Unit) (.invokeSchedContextConsumed (scNameA m sc)) σ = + ({(Fl4SpecA64.wordsFromTime tm, σ')}, False) ∧ + SchedSRA st Z m κ s' σ' ∧ MFrA (st := st) Z m s s' σ σ' ⦄ := by + unfold schedcontext.set_consumed + refine WP.spec_bind (update_consumed_frameA H a hc hmax htu) ?_ + rintro ⟨time, s'⟩ ⟨σ', hc', H', F, -⟩ + change (ok s' : Result S) ⦃ _ => _ ⦄ + apply (WP.spec_ok _).2 + refine ⟨time.bv, σ', ?_, H', F⟩ + change Fl4SpecA64.returnConsumed (Z := Unit) (scNameA m sc) σ = _ + unfold Fl4SpecA64.returnConsumed + rw [nd_bind_oneSc hc'] + rfl + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScCoreA.lean b/proofs/Fl4ProofsA64/P3/ScCoreA.lean new file mode 100644 index 000000000..1fad8cdc4 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScCoreA.lean @@ -0,0 +1,291 @@ +/- +P3 (aarch64, A64-P3-MCS gen 3): **the multi-core scheduler-queue relation** `SchedCoreA` (ruling 22:14Z: gen 2's +`relCur` generalised; x86 `P3/SchedRel`'s `SchedCore` for every core, A64 P1's `SchedQueuesInvX` without its +thread-state, action and endpoint parts). + +* **`SchedCoreA st T Cs s Q R`** — the scheduler's own words on every core `c ∈ Cs` (`Cs` = the cores `< numNodes`, + `JAtZA.csN`), with ghost lists `Q c p` (ready queue of priority `p` on core `c`) and `R c` (release queue of `c`): + queue relations, bitmaps, `IN_QUEUES` bits (`Q_QUEUED` / `Q_RELEASE`), affinities, priorities, node and TCB + placement (x86 `SchedCore`'s `tcbs` / `node` / `aff` / `prio` / `ready` / `release` / `inQ` / `queued` / + `released` / `excl`, every core). It leaves out what P1 also states about thread states (`runQ` / `runR` / + `actOK`), the action word and the endpoint queues (`eps`), as x86 does: the IPC code changes those. + **`SchedQueuesInvX.toCoreA`**: it is a projection of A64 P1's invariant (the join). +* `SchedCoreA.memT` / `memTR` / `notQ` / `notR` / `QR` (P1's, over `SchedCoreA`); **`SchedCoreA.relBit`**: a thread of + core `c` has its release bit iff it is in `R c` (gen 2's `relCur` at any thread on its core). +* **`SchedCoreA.agree`** (P1 `inv_of_agree'`): a change that keeps the `Sched` links, the queue words of every node, + and the `IN_QUEUES` / affinity / priority fields keeps `SchedCoreA`. +* **`core_ready_step`** (P1 `inv_ready_step`) and **`sched.enqueue.coreA`** (P1 `sched.enqueue.inv` + `.foot`): + `enqueue` keeps `SchedCoreA` with the ghost list `enqQA` (the thread prepended to its core's queue of its + priority unless queued), writing inside `QFoot`. + +No named premise. +-/ +import Fl4ProofsA64.P3.ScCheckA +import Fl4ProofsA64.P1.QueueSchedInv +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +section +variable {S : Type} (st : machine.Store S) [L : LawfulStore st] + +/-- **The scheduler queues of every core** (module docstring; x86 `SchedCore`, all cores). -/ +structure SchedCoreA (T Cs : Set U64) (s : S) (Q : U64 → U64 → List U64) (R : U64 → List U64) : Prop where + tcbs : TcbPlaced T + nodes : ∀ c ∈ Cs, NodePlaced st T s c + aff : ∀ t ∈ T, L.mem s (affA t) ∈ Cs + prio : ∀ t ∈ T, (L.mem s (prioA t)).val < 256 + ready : ∀ c ∈ Cs, ReadyQueues st T s c (Q c) + release : ∀ c ∈ Cs, QueueRel (L.mem s) T .Sched (L.relQ s c) (R c) + inQ : ∀ c ∈ Cs, ∀ p : U64, p.val < 256 → ∀ t ∈ Q c p, L.mem s (affA t) = c ∧ L.mem s (prioA t) = p + inR : ∀ c ∈ Cs, ∀ t ∈ R c, L.mem s (affA t) = c + queued : ∀ t ∈ T, ((L.mem s (inqA t) &&& sched.Q_QUEUED) ≠ 0#u64 ↔ + t ∈ Q (L.mem s (affA t)) (L.mem s (prioA t))) + released : ∀ t ∈ T, ((L.mem s (inqA t) &&& sched.Q_RELEASE) ≠ 0#u64 ↔ t ∈ R (L.mem s (affA t))) + excl : ∀ t ∈ T, t ∈ Q (L.mem s (affA t)) (L.mem s (prioA t)) → t ∉ R (L.mem s (affA t)) + +/-- The ghost lists after `enqueue t` (P1 `sched.enqueue.inv`'s): `t` prepended to its core's queue of its +priority, unless it is queued already. -/ +def enqQA (s : S) (t : U64) (Q : U64 → U64 → List U64) : U64 → U64 → List U64 := + if (L.mem s (inqA t) &&& sched.Q_QUEUED) ≠ 0#u64 then Q + else Function.update Q (L.mem s (affA t)) (Function.update (Q (L.mem s (affA t))) (L.mem s (prioA t)) + (t :: Q (L.mem s (affA t)) (L.mem s (prioA t)))) + +end + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {T Cs : Set U64} {s : S} + {Q : U64 → U64 → List U64} {R : U64 → List U64} + +/-- **`SchedCoreA` is a projection of A64 P1's queue invariant** (the join). -/ +theorem _root_.Fl4ProofsA64.P1.SchedQueuesInvX.toCoreA {Ω X : Set U64} {H : Heap} (h : SchedQueuesInvX st T Cs Ω H X s Q R) : + SchedCoreA st T Cs s Q R := + ⟨h.tcbs, h.nodes, h.aff, h.prio, h.ready, h.release, h.inQ, h.inR, h.queued, h.released, h.excl⟩ + +namespace SchedCoreA + +theorem memT (h : SchedCoreA st T Cs s Q R) {c p : U64} (hc : c ∈ Cs) (hp : p.val < 256) {y : U64} + (hy : y ∈ Q c p) : y ∈ T := ((h.ready c hc).1.rel p hp).mem y hy + +theorem memTR (h : SchedCoreA st T Cs s Q R) {c : U64} (hc : c ∈ Cs) {y : U64} (hy : y ∈ R c) : y ∈ T := + (h.release c hc).mem y hy + +theorem notQ (h : SchedCoreA st T Cs s Q R) {t : U64} (ht : t ∈ T) + (h0 : (L.mem s (inqA t) &&& sched.Q_QUEUED) = 0#u64) {c p : U64} (hc : c ∈ Cs) (hp : p.val < 256) : + t ∉ Q c p := by + intro hm + obtain ⟨e1, e2⟩ := h.inQ c hc p hp t hm + exact ((h.queued t ht).2 (by rw [e1, e2]; exact hm)) h0 + +theorem notR (h : SchedCoreA st T Cs s Q R) {t : U64} (ht : t ∈ T) + (h0 : (L.mem s (inqA t) &&& sched.Q_RELEASE) = 0#u64) {c : U64} (hc : c ∈ Cs) : t ∉ R c := by + intro hm + have e1 := h.inR c hc t hm + exact ((h.released t ht).2 (by rw [e1]; exact hm)) h0 + +theorem QR (h : SchedCoreA st T Cs s Q R) {c p : U64} (hc : c ∈ Cs) (hp : p.val < 256) {y : U64} + (hy : y ∈ Q c p) {c' : U64} (hc' : c' ∈ Cs) : y ∉ R c' := by + intro hr + obtain ⟨e1, e2⟩ := h.inQ c hc p hp y hy + have e3 := h.inR c' hc' y hr + apply h.excl y (h.memT hc hp hy) + · rw [e1, e2]; exact hy + · rw [e3]; exact hr + +/-- **The release bit of a thread on core `c` is its membership of `R c`** (gen 2's `relCur`, at any thread). -/ +theorem relBit (h : SchedCoreA st T Cs s Q R) {t : U64} (ht : t ∈ T) {c : U64} (hc : L.mem s (affA t) = c) : + (L.mem s (inqA t) &&& sched.Q_RELEASE) = 0#u64 ↔ t ∉ R c := by + rw [← hc] + constructor + · intro h0 hm; exact ((h.released t ht).2 hm) h0 + · intro hn; by_contra h1; exact hn ((h.released t ht).1 h1) + +/-- **`SchedCoreA` only reads the queues' words** (P1 `inv_of_agree'`, priorities kept). -/ +theorem agree {s' : S} (h : SchedCoreA st T Cs s Q R) (hk : L.ksb s' = L.ksb s) + (hlink : ∀ y ∈ T, L.mem s' (nextA .Sched y) = L.mem s (nextA .Sched y) ∧ + L.mem s' (prevA .Sched y) = L.mem s (prevA .Sched y)) + (hnode : ∀ c ∈ Cs, ∀ o : U64, o.val < 4288 → o.val % 8 = 0 → (o.val = 64 ∨ o.val = 72 ∨ 136 ≤ o.val) → + L.mem s' (L.nodeAt s c o) = L.mem s (L.nodeAt s c o)) + (hinq : ∀ y ∈ T, L.mem s' (inqA y) = L.mem s (inqA y)) + (haff : ∀ y ∈ T, L.mem s' (affA y) = L.mem s (affA y)) + (hprio : ∀ y ∈ T, L.mem s' (prioA y) = L.mem s (prioA y)) : SchedCoreA st T Cs s' Q R := by + have hT := h.tcbs + have hn : ∀ c ∈ Cs, ∀ o : U64, o.val < 4288 → o.val % 8 = 0 → (o.val = 64 ∨ o.val = 72 ∨ 136 ≤ o.val) → + L.mem s' (L.nodeAt s' c o) = L.mem s (L.nodeAt s c o) := by + intro c hc o h1 h2 h3; rw [nodeAt_of_ksb hk]; exact hnode c hc o h1 h2 h3 + have hrel : ∀ {q xs}, QueueRel (L.mem s) T .Sched q xs → QueueRel (L.mem s') T .Sched q xs := fun hq => + ⟨hq.nodup, hq.mem, hq.head, hq.last, hq.seg.congr fun y hy => + ⟨(hlink y (hq.mem y hy)).2, (hlink y (hq.mem y hy)).1⟩⟩ + refine ⟨hT, fun c hc => (h.nodes c hc).ksb hk, fun y hy => (haff y hy) ▸ h.aff y hy, + fun y hy => (hprio y hy) ▸ h.prio y hy, fun c hc => ?_, fun c hc => ?_, ?_, ?_, ?_, ?_, ?_⟩ + · have hv : ViewsEq st s s' c := by + refine ⟨fun p hp => ?_, ?_, fun g hg => ?_⟩ + · simp only [LawfulStore.rqQ] + rw [hn c hc _ (by rw [rqOff_val hp]; omega) (by rw [rqOff_val hp]; omega) (by rw [rqOff_val hp]; omega), + hn c hc _ (by rw [rqTailOff_val hp]; omega) (by rw [rqTailOff_val hp]; omega) + (by rw [rqTailOff_val hp]; omega)] + · exact hn c hc _ (by rw [l1Off_val]; omega) (by rw [l1Off_val]) (by rw [l1Off_val]; omega) + · exact hn c hc _ (by rw [l2Off_val hg]; omega) (by rw [l2Off_val hg]; omega) (by rw [l2Off_val hg]; omega) + refine ⟨(h.ready c hc).1.agree hv.1 fun p hp y hy => ?_, (h.ready c hc).2.views hv⟩ + exact hlink y (((h.ready c hc).1.rel p hp).mem y hy) + · have e : L.relQ s' c = L.relQ s c := by + simp only [LawfulStore.relQ] + rw [hn c hc _ (by rw [relh_val]; omega) (by rw [relh_val]) (by rw [relh_val]; simp), + hn c hc _ (by rw [relt_val]; omega) (by rw [relt_val]) (by rw [relt_val]; omega)] + rw [e]; exact hrel (h.release c hc) + · intro c hc p hp y hy + have := h.inQ c hc p hp y hy + have hyT := h.memT hc hp hy + exact ⟨(haff y hyT).trans this.1, (hprio y hyT).trans this.2⟩ + · intro c hc y hy + exact (haff y (h.memTR hc hy)).trans (h.inR c hc y hy) + · intro y hy + rw [hinq y hy, haff y hy, hprio y hy]; exact h.queued y hy + · intro y hy + rw [hinq y hy, haff y hy]; exact h.released y hy + · intro y hy hq + rw [haff y hy, hprio y hy] at hq + rw [haff y hy] + exact h.excl y hy hq + +end SchedCoreA + +/-! ## A step on one ready queue (P1 `inv_ready_step`) -/ + +/-- **`SchedCoreA` after an operation that replaces the ready queue `(c, p)` of thread `t` by `xs'`**, touching only +the footprint `QFoot` of `W` (P1 `inv_ready_step`, the queue part). -/ +theorem core_ready_step {s' : S} {t c p : U64} {W xs' : List U64} {tIn : Prop} + (h : SchedCoreA st T Cs s Q R) (hcur : L.cpu s ∈ Cs) (ht : t ∈ T) (hca : L.mem s (affA t) = c) + (hpa : L.mem s (prioA t) = p) (hW1 : ∀ w ∈ W, w = t ∨ w ∈ Q c p) (hWt : t ∈ W → ∀ c' ∈ Cs, t ∉ R c') + (hw : WritesIn st s s' (L.QFoot s W c (136 ≤ ·) t)) + (hRc : ReadyQueues st T s' c (Function.update (Q c) p xs')) + (hmem : ∀ y, y ∈ xs' ↔ (y = t ∧ tIn) ∨ (y ≠ t ∧ y ∈ Q c p)) + (hqq : (L.mem s' (inqA t) &&& sched.Q_QUEUED) ≠ 0#u64 ↔ tIn) + (hqr : L.mem s' (inqA t) &&& sched.Q_RELEASE = L.mem s (inqA t) &&& sched.Q_RELEASE) + (hnr : tIn → t ∉ R c) : + SchedCoreA st T Cs s' (Function.update Q c (Function.update (Q c) p xs')) R := by + have hT := h.tcbs + have hc : c ∈ Cs := hca ▸ h.aff t ht + have hp : p.val < 256 := hpa ▸ h.prio t ht + have hN := h.nodes c hc + have hN' := h.nodes _ hcur + have hW : ∀ w ∈ W, w ∈ T := fun w hw' => by + rcases hW1 w hw' with rfl | hq + · exact ht + · exact h.memT hc hp hq + have haff : ∀ y ∈ T, L.mem s' (affA y) = L.mem s (affA y) := fun y hy => aff_qfoot hT hW hN hN' ht hw hy + have hprio : ∀ y ∈ T, L.mem s' (prioA y) = L.mem s (prioA y) := fun y hy => prio_qfoot hT hW hN hN' ht hw hy + have hinq : ∀ y ∈ T, y ≠ t → L.mem s' (inqA y) = L.mem s (inqA y) := + fun y hy hyt => inq_qfoot hT hW hN hN' ht hw hy hyt + have hWQ : ∀ c' ∈ Cs, c' ≠ c → ∀ p' : U64, p'.val < 256 → ∀ y ∈ Q c' p', y ∉ W := by + intro c' hc' hne p' hp' y hy hyW + have e := (h.inQ c' hc' p' hp' y hy).1 + rcases hW1 y hyW with rfl | hq + · exact hne (e.symm.trans hca) + · exact hne (e.symm.trans (h.inQ c hc p hp y hq).1) + have hWR : ∀ c' ∈ Cs, ∀ y ∈ R c', y ∉ W := by + intro c' hc' y hy hyW + rcases hW1 y hyW with rfl | hq + · exact hWt hyW c' hc' hy + · exact h.QR hc hp hq hc' hy + refine ⟨hT, fun c' hc' => (h.nodes c' hc').ksb hw.1, fun y hy => (haff y hy) ▸ h.aff y hy, + fun y hy => (hprio y hy) ▸ h.prio y hy, fun c' hc' => ?_, fun c' hc' => ?_, ?_, ?_, ?_, ?_, ?_⟩ + · by_cases e : c' = c + · subst e; rw [Function.update_self]; exact hRc + · rw [Function.update_of_ne e] + exact (h.ready c' hc').qfoot hT hW hN hN' ht hw (h.nodes c' hc') (fun e' => absurd e' e) + (hWQ c' hc' e) + · rw [relQ_qfoot hT hW hN hN' ht hw (h.nodes c' hc') (fun _ => by omega)] + exact (h.release c' hc').qfoot hT hW hN hN' ht hw (fun _ => hWR c' hc') + · intro c' hc' p' hp' y hy + rcases mem_upd2.1 hy with ⟨rfl, rfl, hy'⟩ | ⟨-, hy'⟩ + · rcases (hmem y).1 hy' with ⟨rfl, -⟩ | ⟨-, hq⟩ + · exact ⟨(haff y ht).trans hca, (hprio y ht).trans hpa⟩ + · have := h.inQ c' hc p' hp y hq + exact ⟨(haff y (h.memT hc hp hq)).trans this.1, (hprio y (h.memT hc hp hq)).trans this.2⟩ + · have := h.inQ c' hc' p' hp' y hy' + exact ⟨(haff y (h.memT hc' hp' hy')).trans this.1, (hprio y (h.memT hc' hp' hy')).trans this.2⟩ + · intro c' hc' y hy + exact (haff y (h.memTR hc' hy)).trans (h.inR c' hc' y hy) + · intro y hy + rw [haff y hy, hprio y hy] + by_cases e : y = t + · subst e + rw [hqq, hca, hpa, Function.update_self, Function.update_self, hmem] + simp + · rw [hinq y hy e, h.queued y hy, mem_upd2] + constructor + · intro hq + by_cases e2 : L.mem s (affA y) = c ∧ L.mem s (prioA y) = p + · obtain ⟨e3, e4⟩ := e2 + left; refine ⟨e3, e4, (hmem y).2 (Or.inr ⟨e, ?_⟩)⟩ + rw [← e3, ← e4]; exact hq + · exact Or.inr ⟨e2, hq⟩ + · rintro (⟨e3, e4, hq⟩ | ⟨-, hq⟩) + · rcases (hmem y).1 hq with ⟨e5, -⟩ | ⟨-, hq'⟩ + · exact absurd e5 e + · rw [e3, e4]; exact hq' + · exact hq + · intro y hy + rw [haff y hy] + by_cases e : y = t + · subst e; rw [hqr]; exact h.released y hy + · rw [hinq y hy e]; exact h.released y hy + · intro y hy hq + rw [haff y hy] at hq ⊢ + rw [hprio y hy] at hq + rcases mem_upd2.1 hq with ⟨e3, e4, hq'⟩ | ⟨e3, hq'⟩ + · rcases (hmem y).1 hq' with ⟨rfl, hin⟩ | ⟨-, hq''⟩ + · rw [e3]; exact hnr hin + · exact h.excl y hy (by rw [e3, e4]; exact hq'') + · exact h.excl y hy hq' + +/-! ## `enqueue` (P1 `sched.enqueue.inv`) -/ + +/-- **`enqueue` keeps `SchedCoreA`** with the ghost lists `enqQA`, writing inside `QFoot` of its queue (P1 +`sched.enqueue.inv` + `sched.enqueue.foot`), for a thread that is queued already or not released. -/ +theorem sched.enqueue.coreA (s : S) (t : obj.Tcb) (h : SchedCoreA st T Cs s Q R) (hcur : L.cpu s ∈ Cs) + (ht : t ∈ T) + (hr : (L.mem s (inqA t) &&& sched.Q_QUEUED) = 0#u64 → (L.mem s (inqA t) &&& sched.Q_RELEASE) = 0#u64) : + sched.enqueue st s t ⦃ s' => SchedCoreA st T Cs s' (enqQA st s t Q) R ∧ + WritesIn st s s' (L.QFootS s (t :: Q (L.mem s (affA t)) (L.mem s (prioA t))) (L.mem s (affA t)) (136 ≤ ·) t) ∧ + L.mem s' (inqA t) = (if (L.mem s (inqA t) &&& sched.Q_QUEUED) ≠ 0#u64 then L.mem s (inqA t) + else L.mem s (inqA t) ||| sched.Q_QUEUED) ⦄ := by + have hT := h.tcbs + have hc := h.aff t ht + have hp := h.prio t ht + have hN := h.nodes _ hc + have hN' := h.nodes _ hcur + have hR1 := h.ready _ hc + have h1 := sched.enqueue.ready s t _ hT ht hp hN hN' hR1 (fun h0 p hp => h.notQ ht h0 hc hp) + have h2 := sched.enqueue.footS s t _ hT ht hp hN hN' (hR1.1.rel _ hp) (fun h0 => h.notQ ht h0 hc hp) + refine WP.spec_mono (WP.spec_and h1 h2) ?_ + rintro s' ⟨hRc, hwS, hinq⟩ + have hw := hwS.mono fun _ h => h.toQFoot + refine ⟨?_, hwS, hinq⟩ + unfold enqQA + by_cases hqd : (L.mem s (inqA t) &&& sched.Q_QUEUED) ≠ 0#u64 + · rw [if_pos hqd] at hRc hinq ⊢ + have htq := (h.queued t ht).1 hqd + have := core_ready_step (W := t :: Q _ _) (xs' := Q (L.mem s (affA t)) (L.mem s (prioA t))) (tIn := True) + h hcur ht rfl rfl (fun w hw => List.mem_cons.1 hw) + (fun _ c' hc' hm => h.QR hc hp htq hc' hm) hw (by rw [Function.update_eq_self]; exact hRc) + (fun y => by by_cases e : y = t <;> simp [e, htq]) (by rw [hinq]; simpa using hqd) (by rw [hinq]) + (fun _ => h.QR hc hp htq hc) + simpa [Function.update_eq_self] using this + · have hq0 : (L.mem s (inqA t) &&& sched.Q_QUEUED) = 0#u64 := not_not.1 hqd + rw [if_neg hqd] at hRc hinq ⊢ + exact core_ready_step (W := t :: Q _ _) (tIn := True) h hcur ht rfl rfl (fun w hw => List.mem_cons.1 hw) + (fun _ c' hc' => h.notR ht (hr hq0) hc') hw hRc + (fun y => by by_cases e : y = t <;> simp [e]) + (by rw [hinq, or_qq_qq]; simpa using qq_ne) (by rw [hinq, or_qq_qr]) + (fun _ => h.notR ht (hr hq0) hc) + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScDeadlineA.lean b/proofs/Fl4ProofsA64/P3/ScDeadlineA.lean new file mode 100644 index 000000000..2bdfca3df --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScDeadlineA.lean @@ -0,0 +1,225 @@ +import Fl4ProofsA64.P3.ScBudgetTopA +import Fl4ProofsA64.P3.ScArchSwA +import Fl4SpecA64.Ext.SchedTail + +/-! +The local AArch64 timer decision. [J] `SniReadsA` relates the selected +TCB, refill head, and earliest local timer and release timestamps. +[P] `SniHypA` relates the architecture deadline operation, including +the physical match for logical zero. +-/ + +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3.Sched + +open Fl4SpecA64 Fl4ProofsA64.P1 + +noncomputable section + +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +/-- The release queue inputs to the deadline computation. -/ +structure DeadlineInputsA where + amount : U64 + release : Option U64 + earliest : U64 + current : U64 + idle : U64 + +/-- [J] The values read by the concrete timer path. A nonempty release +queue supplies the exact refill timestamp of its head. -/ +structure SniReadsA (s : S) (input : DeadlineInputsA) : Prop where + current : sched.cur_thread st s = ok input.current + idle : sched.idle_thread st s = ok input.idle + amount : (do + let c ← obj.Tcb.sc st input.current s + let refill ← sporadic.head st s c + ok refill.amount) = ok input.amount + release : let t := L.mem s (L.nodeAt s (L.cpu s) layout.node.RELEASE_HEAD) + match input.release with + | none => t = 0#u64 + | some left => t ≠ 0#u64 ∧ sched.ready_time st s t = ok left + earliest : timer.earliest st s = ok input.earliest + +structure DeadlineViewA where + backend : A64ReturnModeA + logical : Time + physical : Time + +/-- [P] The platform conversion and timer compare write on the current +core, with an unchanged view of other cores. -/ +structure SniHypA (ar : machine.Arch A) (timer : A → Nat → DeadlineViewA) (a : A) (core : Nat) : Prop where + precision : ∃ ticks, ar.us_to_ticks a 1#u64 = ok ticks ∧ ticks.bv = fl4UsToTicks 1 + program : ∀ deadline, ar.set_deadline a deadline ⦃ a' => + (timer a' core).backend = (timer a core).backend ∧ + (timer a' core).logical = deadline.bv ∧ + (timer a' core).physical = fl4ProgrammedDeadline deadline.bv ∧ + ∀ c, c ≠ core → timer a' c = timer a c ⦄ + +def deadlineWordA (now amount : U64) (release : Option U64) (precision earliest : U64) : U64 := + let expiry := core.num.U64.wrapping_add now amount + let expiry := match release with + | none => expiry + | some v => if v < expiry then v else expiry + let deadline := core.num.U64.wrapping_sub expiry precision + if earliest ≠ 0#u64 ∧ earliest < deadline then earliest else deadline + +theorem deadlineWord_eqA (now amount : U64) (release : Option U64) (precision earliest : U64) : + (deadlineWordA now amount release precision earliest).bv = + fl4Deadline now.bv amount.bv none (release.map UScalar.bv) precision.bv earliest.bv := by + have eq0 : earliest ≠ 0#u64 ↔ earliest.bv ≠ (0 : Time) := + not_congr ⟨fun h => congrArg UScalar.bv h, eq_of_bv⟩ + cases release with + | none => + simp only [deadlineWordA, fl4Deadline, Option.map_none] + simp only [← core.num.U64.wrapping_add_bv_eq, ← core.num.U64.wrapping_sub_bv_eq, + ← (u64_lt_bvA _ _), ← eq0] + split <;> rfl + | some release => + simp only [deadlineWordA, fl4Deadline, Option.map_some, ← core.num.U64.wrapping_add_bv_eq, + ← (u64_lt_bvA _ _)] + split <;> simp_all only [← core.num.U64.wrapping_sub_bv_eq, ← eq0, ← (u64_lt_bvA _ _)] <;> split <;> rfl + +private def writeTimerA (s : S) (a : A) (deadline : U64) : Result (S × A) := do + let s1 ← obj.node_set st s (L.cpu s) layout.node.ARMED deadline + let a1 ← ar.set_deadline a deadline + ok (s1, a1) + +private theorem timerDecisionA {s : S} {a : A} {input : DeadlineInputsA} {precision : U64} + (h : SniReadsA (st := st) s input) (conv : ar.us_to_ticks a 1#u64 = ok precision) : + let now := L.curTime s + let armed := L.mem s (L.nodeAt s (L.cpu s) layout.node.ARMED) + let deadline := deadlineWordA now input.amount input.release precision input.earliest + sched.set_next_interrupt st ar s a = + if now < armed ∧ (armed = deadline ∨ (armed < deadline ∧ input.current ≠ input.idle)) then ok (s, a) + else writeTimerA (st := st) (ar := ar) s a deadline := by + let now := L.curTime s + let ct := input.current + let expr := core.num.U64.wrapping_add now input.amount + let t := L.mem s (L.nodeAt s (L.cpu s) layout.node.RELEASE_HEAD) + let armed := L.mem s (L.nodeAt s (L.cpu s) layout.node.ARMED) + let deadline := deadlineWordA now input.amount input.release precision input.earliest + have ntime : sched.cur_time st s = ok now := by + unfold sched.cur_time + rw [L.cur_cpu_eq, bind_ok, eq_of_spec_fd (obj.node_get.spec s _ _)] + rfl + have amt : ∃ sc refill, obj.Tcb.sc st ct s = ok sc ∧ sporadic.head st s sc = ok refill ∧ + refill.amount = input.amount := + ⟨_, _, eq_of_spec_fd (obj.Tcb.sc.spec s ct), eq_of_spec_fd (sporadic.head.spec s _), by + have he := h.amount + rw [eq_of_spec_fd (obj.Tcb.sc.spec s ct), bind_ok, eq_of_spec_fd (sporadic.head.spec s _), bind_ok] at he + simpa only [ok.injEq] using he⟩ + obtain ⟨sc, refill, hsc, hr, hamt⟩ := amt + have domain : ¬ layout.NUM_DOMAINS > 1#u64 := by + simp [layout.NUM_DOMAINS, fl4_abi.config.NUM_DOMAINS] + have rel : + (if decide (t = 0#u64) then ok expr else do + let left ← sched.ready_time st s t + if left < expr then ok left else ok expr) = + ok (match input.release with + | none => expr + | some x => if x < expr then x else expr) := by + have read := h.release + cases e : input.release with + | none => + rw [e] at read + change t = 0#u64 at read + rw [read] + rfl + | some x => + rw [e] at read + change t ≠ 0#u64 ∧ sched.ready_time st s t = ok x at read + rw [decide_eq_false read.1] + rw [read.2] + by_cases hlt : x < expr <;> simp only [bind_ok, hlt, if_true, if_false, Bool.false_eq_true, ↓reduceIte] + have td (v : U64) : + (if input.earliest != 0#u64 then if input.earliest < v then ok input.earliest else ok v else ok v) = + ok (if input.earliest ≠ 0#u64 ∧ input.earliest < v then input.earliest else v) := by + by_cases h0 : input.earliest = 0#u64 + · simp [h0] + by_cases hlt : input.earliest < v <;> simp [bne_iff_ne.2 h0, h0, hlt] + unfold sched.set_next_interrupt + rw [ntime, bind_ok, h.current, bind_ok, hsc, bind_ok, hr, bind_ok, hamt] + simp only [lift, bind_ok, if_neg domain] + rw [L.cur_cpu_eq, bind_ok, eq_of_spec_fd (obj.node_get.spec s _ _), bind_ok, + eq_of_spec_fd (obj.Tcb.is_null.spec _), bind_ok, rel, bind_ok, conv, bind_ok] + rw [h.earliest, bind_ok, td, bind_ok, eq_of_spec_fd (obj.node_get.spec s _ _), bind_ok] + change (if now < armed then + if armed = deadline then ok (s, a) + else if armed < deadline then do + let idle ← sched.idle_thread st s + let ne ← core.cmp.PartialEq.ne.trait_default obj.Tcb.Insts.CoreCmpPartialEqTcb ct idle + if ne then ok (s, a) else writeTimerA (st := st) (ar := ar) s a deadline + else writeTimerA (st := st) (ar := ar) s a deadline + else writeTimerA (st := st) (ar := ar) s a deadline) = + if now < armed ∧ (armed = deadline ∨ (armed < deadline ∧ ct ≠ input.idle)) then ok (s, a) + else writeTimerA (st := st) (ar := ar) s a deadline + by_cases active : now < armed + · rw [if_pos active] + by_cases eq : armed = deadline + · rw [if_pos eq, if_pos ⟨active, Or.inl eq⟩] + rw [if_neg eq] + by_cases earlier : armed < deadline + · rw [if_pos earlier, h.idle, bind_ok] + simp only [core.cmp.PartialEq.ne.trait_default, core.cmp.PartialEq.ne.default, + obj.Tcb.Insts.CoreCmpPartialEqTcb.eq, bind_ok] + by_cases idle : ct = input.idle + · simp [active, eq, earlier, idle, writeTimerA] + · simp [active, eq, earlier, idle] + · simp [active, eq, earlier, writeTimerA] + · simp [active, writeTimerA] + +def DeadlineOutcomeA (timer : A → Nat → DeadlineViewA) (s : S) (a : A) + (deadline : Time) (r : S × A) : Prop := + ∃ value : U64, value.bv = deadline ∧ + r.1 = L.wr s (L.nodeAt s (L.cpu s) layout.node.ARMED) value ∧ + (timer r.2 (L.cpu s).val).backend = (timer a (L.cpu s).val).backend ∧ + (timer r.2 (L.cpu s).val).logical = deadline ∧ + (timer r.2 (L.cpu s).val).physical = fl4ProgrammedDeadline deadline ∧ + ∀ c, c ≠ (L.cpu s).val → timer r.2 c = timer a c + +/-- Keep an outstanding compare or write ARMED and the exact logical +deadline on the local core. -/ +theorem set_next_interrupt_wordsA {s : S} {a : A} {input : DeadlineInputsA} + (h : SniReadsA (st := st) s input) {timer : A → Nat → DeadlineViewA} + (P : SniHypA ar timer a (L.cpu s).val) : + let now := L.curTime s + let armed := L.mem s (L.nodeAt s (L.cpu s) layout.node.ARMED) + let d := fl4Deadline now.bv input.amount.bv none (input.release.map UScalar.bv) + (fl4UsToTicks 1) input.earliest.bv + sched.set_next_interrupt st ar s a ⦃ r => + if fl4KeepsDeadline now.bv armed.bv d (decide (input.current = input.idle)) then r = (s, a) + else DeadlineOutcomeA (st := st) timer s a d r ⦄ := by + obtain ⟨precision, conv, ticks⟩ := P.precision + let now := L.curTime s + let armed := L.mem s (L.nodeAt s (L.cpu s) layout.node.ARMED) + let deadline := deadlineWordA now input.amount input.release precision input.earliest + let d := fl4Deadline now.bv input.amount.bv none (input.release.map UScalar.bv) + (fl4UsToTicks 1) input.earliest.bv + let keep := now < armed ∧ (armed = deadline ∨ (armed < deadline ∧ input.current ≠ input.idle)) + have e : deadline.bv = d := (deadlineWord_eqA _ _ _ _ _).trans (by rw [ticks]) + have eqa : armed.bv = deadline.bv ↔ armed = deadline := + ⟨eq_of_bv, fun h => congrArg UScalar.bv h⟩ + have keeps : fl4KeepsDeadline now.bv armed.bv d (decide (input.current = input.idle)) ↔ keep := by + rw [← e] + simp only [keep, fl4KeepsDeadline, ← (u64_lt_bvA _ _), eqa, decide_eq_false_iff_not] + change _ ⦃ r => if fl4KeepsDeadline now.bv armed.bv d _ then _ else _ ⦄ + rw [timerDecisionA h conv] + change (if keep then ok (s, a) else writeTimerA (st := st) (ar := ar) s a deadline) ⦃ _ ⦄ + by_cases pending : keep + · simp only [if_pos pending, WP.spec_ok, if_pos (keeps.2 pending)] + · simp only [if_neg pending, if_neg (fun h => pending (keeps.1 h))] + unfold writeTimerA + refine WP.spec_bind (obj.node_set.spec s _ _ _) ?_ + intro s1 hs + refine WP.spec_bind (P.program deadline) ?_ + intro a1 ha + rw [WP.spec_ok] + change DeadlineOutcomeA (st := st) timer s a d (s1, a1) + rw [← e] + exact ⟨deadline, rfl, hs, ha⟩ + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScDecodeOpA.lean b/proofs/Fl4ProofsA64/P3/ScDecodeOpA.lean new file mode 100644 index 000000000..0cb006e71 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScDecodeOpA.lean @@ -0,0 +1,127 @@ +/- +P3 (AArch64): successful SC Bind, Unbind and UnbindObject decoder arms. +The branch calculations port x86 ScDecodeSc.bind_branch_refines, +unbind_invoke_refines and unbindobj_branch_refines. The first stall is +kept explicit, including the architecture state it returns. +-/ +import Fl4ProofsA64.P3.ScNtfnLinkA +import Fl4ProofsA64.P3.ScUnbObjA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} + {s s1 : S} {a a1 : A} {sc t ct : U64} {call : Bool} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + +theorem sc_decode_unbind_eqA + (ht : obj.Sc.tcb st sc s = ok t) + (hs : (if t != 0#u64 then smp.remote_tcb_stall st ar s a t else ok (s, a)) = ok (s1, a1)) + (hct : sched.cur_thread st s1 = ok ct) + {t' : U64} (ht' : obj.Sc.tcb st sc s1 = ok t') (hne : t' ≠ ct) : + schedcontext.decode st ar s a fl4_abi.label.SchedContextUnbind sc call extra = (do + let s2 ← sched.set_thread_state st s1 ct sched.TS_RESTART + let (s3, a2) ← schedcontext.unbind_all_tcbs st ar s2 a1 sc + let s4 ← schedcontext.unbind_ntfn st s3 sc + let s5 ← schedcontext.unbind_reply st s4 sc + ok (core.result.Result.Ok (), s5, a2)) := by + unfold schedcontext.decode + rw [ht, bind_ok, hs] + simp (config := { decide := true }) only [hct, ht', bind_ok, uncurry_apply_pair, fl4_abi.label.SchedContextConsumed, + fl4_abi.label.SchedContextBind, fl4_abi.label.SchedContextUnbindObject, fl4_abi.label.SchedContextUnbind, + hne, ↓reduceIte] + +theorem sc_decode_unbind_object_thread_eqA + (ht : obj.Sc.tcb st sc s = ok t) + (hs : (if t != 0#u64 then smp.remote_tcb_stall st ar s a t else ok (s, a)) = ok (s1, a1)) + (hct : sched.cur_thread st s1 = ok ct) + (he : schedcontext.extra_cap st s1 extra 0#usize = ok (some (.Thread t))) + (ht' : obj.Sc.tcb st sc s1 = ok t) (hne : t ≠ ct) : + schedcontext.decode st ar s a fl4_abi.label.SchedContextUnbindObject sc call extra = (do + let s2 ← sched.set_thread_state st s1 ct sched.TS_RESTART + let s3 ← schedcontext.unbind_tcb st s2 sc + ok (core.result.Result.Ok (), s3, a1)) := by + unfold schedcontext.decode + rw [ht, bind_ok, hs] + simp (config := { decide := true }) only [hct, he, ht', bind_ok, uncurry_apply_pair, + fl4_abi.label.SchedContextConsumed, fl4_abi.label.SchedContextBind, fl4_abi.label.SchedContextUnbindObject, + bne_self_eq_false, Bool.false_eq_true, hne, ↓reduceIte] + +theorem sc_decode_unbind_object_ntfn_eqA + (ht : obj.Sc.tcb st sc s = ok t) + (hs : (if t != 0#u64 then smp.remote_tcb_stall st ar s a t else ok (s, a)) = ok (s1, a1)) + (hct : sched.cur_thread st s1 = ok ct) {n badge : U64} {send recv : Bool} + (he : schedcontext.extra_cap st s1 extra 0#usize = ok (some (.Notification n badge send recv))) + (hn : obj.Sc.ntfn st sc s1 = ok n) : + schedcontext.decode st ar s a fl4_abi.label.SchedContextUnbindObject sc call extra = (do + let s2 ← sched.set_thread_state st s1 ct sched.TS_RESTART + let s3 ← schedcontext.unbind_ntfn st s2 sc + ok (core.result.Result.Ok (), s3, a1)) := by + unfold schedcontext.decode + rw [ht, bind_ok, hs] + simp (config := { decide := true }) only [hct, he, hn, bind_ok, uncurry_apply_pair, + fl4_abi.label.SchedContextConsumed, fl4_abi.label.SchedContextBind, fl4_abi.label.SchedContextUnbindObject, + bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + +theorem sc_decode_bind_ntfn_eqA + (ht : obj.Sc.tcb st sc s = ok t) + (hs : (if t != 0#u64 then smp.remote_tcb_stall st ar s a t else ok (s, a)) = ok (s1, a1)) + (hct : sched.cur_thread st s1 = ok ct) {n badge : U64} {send recv : Bool} + (he : schedcontext.extra_cap st s1 extra 0#usize = ok (some (.Notification n badge send recv))) + (hn : obj.Sc.ntfn st sc s1 = ok 0#u64) (hn' : obj.Ntfn.sc st n s1 = ok 0#u64) : + schedcontext.decode st ar s a fl4_abi.label.SchedContextBind sc call extra = (do + let s2 ← sched.set_thread_state st s1 ct sched.TS_RESTART + let s3 ← schedcontext.bind_ntfn st s2 sc n + ok (core.result.Result.Ok (), s3, a1)) := by + unfold schedcontext.decode + rw [ht, bind_ok, hs] + simp (config := { decide := true }) only [hct, he, hn, hn', bind_ok, uncurry_apply_pair, + fl4_abi.label.SchedContextConsumed, fl4_abi.label.SchedContextBind, + bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + +end + +noncomputable section +open Fl4SpecA64 Fl4Spec + +theorem decodeUnbind_eqA {σ : State Unit} {sc : ObjRef} {xs : SchedContext} {ns : Nat} + (hk : σ.kheap sc = some (.schedContext xs ns)) (hne : xs.scTcb ≠ some σ.curThread) + {label : Data} (hl : genInvocationType label = .schedContextUnbind) (caps : List Cap) : + (decodeSchedContextInvocation (Z := Unit) label sc caps).run σ = + ({(.ok (.invokeSchedContextUnbind sc), σ)}, False) := by + unfold decodeSchedContextInvocation + rw [hl, run_bind_okA (run_liftEA (getSchedContext_eqA hk))] + have hget : NonDet.gets (fun s : State Unit => s.curThread) σ = ({(σ.curThread, σ)}, False) := rfl + rw [run_bind_okA (run_liftEA hget)] + simp [NonDetE.whenE, hne, NonDetE.returnOk] + +theorem decodeBind_ntfn_eqA {σ : State Unit} {sc n : ObjRef} + {xs : SchedContext} {ns : Nat} {xn : Notification} + (hks : σ.kheap sc = some (.schedContext xs ns)) (hkn : σ.kheap n = some (.notification xn)) + (hs : xs.scNtfn = none) (hn : xn.ntfnSc = none) (badge : Badge) (rs : CapRights) (rest : List Cap) : + (decodeSchedContextBind (Z := Unit) sc (.notificationCap n badge rs :: rest)).run σ = + ({(.ok (.invokeSchedContextBind sc (.notificationCap n badge rs)), σ)}, False) := by + unfold decodeSchedContextBind + simp only [List.length_cons, Nat.succ_ne_zero, NonDetE.whenE, ↓reduceIte, NonDetE.returnOk, ndE_pure_bindA, + List.head?_cons, the_someA] + rw [run_bind_okA (run_liftEA (getSchedContext_eqA hks))] + simp only [hs, ne_eq, not_true_eq_false, NonDetE.whenE, ↓reduceIte, NonDetE.returnOk, ndE_pure_bindA] + rw [run_bind_okA (run_liftEA (getNtfnSc_eqSc hkn))] + simp [NonDetE.whenE, hn, NonDetE.returnOk] + +theorem decodeUnbindObject_ntfn_eqA {σ : State Unit} {sc n : ObjRef} {xs : SchedContext} {ns : Nat} + (hk : σ.kheap sc = some (.schedContext xs ns)) (h : xs.scNtfn = some n) + (badge : Badge) (rs : CapRights) (rest : List Cap) : + (decodeSchedContextUnbindObject (Z := Unit) sc (.notificationCap n badge rs :: rest)).run σ = + ({(.ok (.invokeSchedContextUnbindObject sc (.notificationCap n badge rs)), σ)}, False) := by + unfold decodeSchedContextUnbindObject + simp only [List.length_cons, Nat.succ_ne_zero, NonDetE.whenE, ↓reduceIte, NonDetE.returnOk, ndE_pure_bindA, + List.head?_cons, the_someA] + rw [run_bind_okA (run_liftEA (getScNtfn_eqA hk))] + simp [NonDetE.whenE, h, NonDetE.returnOk] + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScDeqA.lean b/proofs/Fl4ProofsA64/P3/ScDeqA.lean new file mode 100644 index 000000000..a68f0dd6a --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScDeqA.lean @@ -0,0 +1,136 @@ +/- +P3 (AArch64): ready dequeue on the current core, used by SC unbind. +`sched.dequeue.coreA` uses A64 P1's ready-list and write-footprint proofs. +`dequeue_refines_localA` ports x86 P3's `dequeue_refines`; its frame is `MFrA`. +The affinity equality selects the current-core view. No named premises. +-/ +import Fl4ProofsA64.P3.ScDeqCoreA +import Fl4ProofsA64.P3.ScSwitchA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +theorem deqQA_updateA {T Cs : Set U64} {s : S} {Q : U64 → U64 → List U64} {R : U64 → List U64} + (H : SchedCoreA st T Cs s Q R) {t : U64} (ht : t ∈ T) : + let c := L.mem s (affA t) + let p := L.mem s (prioA t) + deqQA (st := st) s t Q = Function.update Q c (Function.update (Q c) p ((Q c p).erase t)) := by + let c := L.mem s (affA t) + let p := L.mem s (prioA t) + change deqQA (st := st) s t Q = Function.update Q c (Function.update (Q c) p ((Q c p).erase t)) + unfold deqQA + split + · rfl + · rename_i hb + have he := List.erase_of_not_mem (H.notQ ht (not_not.1 hb) (H.aff t ht) (H.prio t ht)) + rw [he, Function.update_eq_self, Function.update_eq_self] + +theorem deqQA_notMemA {T Cs : Set U64} {s : S} {Q : U64 → U64 → List U64} {R : U64 → List U64} + (H : SchedCoreA st T Cs s Q R) {t c p : U64} (ht : t ∈ T) (hc : c ∈ Cs) (hp : p.val < 256) : + t ∉ deqQA (st := st) s t Q c p := by + rw [deqQA_updateA H ht] + by_cases e : c = L.mem s (affA t) + · subst c + rw [Function.update_self] + by_cases ep : p = L.mem s (prioA t) + · subst p + rw [Function.update_self] + exact fun h => (((H.ready _ hc).1.rel _ hp).nodup.mem_erase_iff.1 h).1 rfl + · rw [Function.update_of_ne ep] + exact fun hm => ep (H.inQ _ hc _ hp t hm).2.symm + · rw [Function.update_of_ne e] + exact fun hm => e (H.inQ _ hc _ hp t hm).1.symm + +theorem SchedSRN.ready_after_dequeueA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s s' : S} + {σ σ' : Fl4SpecA64.State Unit} (H : SchedSRN st Z m κ s σ) {t : U64} (ht : t ∈ Z.T) + (H' : SchedSRN st { Z with Q := deqQA (st := st) s t Z.Q } m κ s' σ') : + ∀ d p, specPtr t ∉ σ'.readyQueues d p := by + intro d p + by_cases hd : d = 0 + · subst d + let p' : U64 := UScalar.mk (BitVec.ofNat 64 p.toNat) + have hp' : p'.val = p.toNat := by + change p.toNat % 2^64 = p.toNat + exact Nat.mod_eq_of_lt (by have := p.isLt; omega) + have e : BitVec.ofNat 8 p'.val = p := by + apply BitVec.eq_of_toNat_eq + rw [BitVec.toNat_ofNat, hp'] + exact Nat.mod_eq_of_lt p.isLt + have hq := H'.rest.rq p' (by rw [hp']; exact p.isLt) + have hcpu : L.cpu s' = L.cpu s := UScalar.eq_of_val_eq (H'.sch.core.symm.trans H.sch.core) + rw [← e, hq, mem_map_specPtrSc, hcpu] + exact deqQA_notMemA H.core ht H.sch.cpuCs (by rw [hp']; exact p.isLt) + · rw [H'.rest.rqDom _ _ hd] + exact List.not_mem_nil + +theorem SchedSRA.ready_after_dequeueA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s s' : S} + {σ σ' : Fl4SpecA64.State Unit} (H : SchedSRA st Z m κ s σ) {t : U64} (ht : t ∈ Z.T) + (H' : SchedSRA st { Z with Q := deqQA (st := st) s t Z.Q } m κ s' σ') : + ∀ d p, specPtr t ∉ σ'.readyQueues d p := + H.toSchedSRN.ready_after_dequeueA ht H'.toSchedSRN + +set_option maxHeartbeats 500000 in +/-- Dequeue refines the removal of the TCB from its spec ready queue (x86 `dequeue_refines`). -/ +theorem dequeue_refines_nodeA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} + {σ : Fl4SpecA64.State Unit} (H : SchedSRN st Z m κ s σ) {t : U64} (ht : t ∈ Z.T) + (hon : L.mem s (affA t) = L.cpu s) : + sched.dequeue st s t ⦃ s' => ∃ σ', Fl4SpecA64.tcbSchedAction (Z := Unit) Fl4SpecA64.tcbSchedDequeue (specPtr t) σ = + ({((), σ')}, False) ∧ σ'.kheap = σ.kheap ∧ σ'.releaseQueue = σ.releaseQueue ∧ SFixA σ σ' ∧ σ'.schedulerAction = σ.schedulerAction ∧ + (∀ u ∈ Z.T, L.mem s' (scA u) = L.mem s (scA u)) ∧ + SchedSRN st { Z with Q := deqQA (st := st) s t Z.Q } m κ s' σ' ∧ MFrA (st := st) Z m s s' σ σ' ⦄ := by + let c := L.mem s (affA t) + let p := L.mem s (prioA t) + have hc : c ∈ Z.Cs := H.core.aff t ht + have hp : p.val < 256 := H.core.prio t ht + have hW : ∀ w ∈ Z.Q c p, w ∈ Z.T := fun _ hw => H.core.memT hc hp hw + obtain ⟨x, hk, hd, -, hpr⟩ := H.rest.prio t ht + have hpe := ofNat_prio_eqA hpr + have hq := H.rest.rq p hp + rw [hpe, ← hon] at hq + refine WP.spec_mono (sched.dequeue.coreA H.core H.sch.cpuCs ht) ?_ + rintro s' ⟨hcore, hwN⟩ + have hw := hwN.mono fun _ h => h.toQFoot + have hdeq := tcbSchedDequeue_mapA ((H.core.ready c hc).1.rel p hp).nodup t + let σ' := setRqA σ x.tcbPriority (Fl4SpecA64.tcbSchedDequeue (specPtr t) (σ.readyQueues 0 x.tcbPriority)) + have hS : SchedSRN st { Z with Q := deqQA (st := st) s t Z.Q } m κ s' σ' := by + refine H.qfoot (σ' := σ') hW hc ht hw rfl ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ rfl hcore ?_ ?_ rfl ?_ + · intro p' hp' + change (if (0 : Fl4SpecA64.Domain) = 0 ∧ BitVec.ofNat 8 p'.val = x.tcbPriority then _ else _) = _ + rw [deqQA_updateA H.core ht, ← hon, Function.update_self] + by_cases e : p' = p + · subst p' + rw [if_pos ⟨rfl, hpe⟩, hq, hdeq, Function.update_self] + · rw [if_neg (fun h => e (ofNat_prio_injA hp' hp (h.2.trans hpe.symm))), Function.update_of_ne e, hon] + exact H.rest.rq p' hp' + · intro d p' hd' + change (if d = 0 ∧ p' = x.tcbPriority then _ else _) = _ + rw [if_neg (fun h => hd' h.1)] + exact H.rest.rqDom d p' hd' + · refine ⟨fun d p' u hu => ?_, H.run.2⟩ + change u ∈ (if d = 0 ∧ p' = x.tcbPriority then _ else _) at hu + split_ifs at hu with e + · obtain ⟨rfl, rfl⟩ := e + exact H.run.1 _ _ _ (List.mem_filter.1 hu).1 + · exact H.run.1 _ _ _ hu + exact ⟨σ', tcbSchedAction_eqA hk hd _, rfl, rfl, ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩, rfl, + fun u hu => sc_qfoot H.core.tcbs hW (H.core.nodes c hc) (H.core.nodes _ H.sch.cpuCs) ht hw hu, + hS, MFrA.of_sw rfl (hwN.mono fun _ hb => H.qfoot_swr hW hc ht hb)⟩ + +theorem dequeue_refines_localA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} + {σ : Fl4SpecA64.State Unit} (H : SchedSRA st Z m κ s σ) {t : U64} (ht : t ∈ Z.T) + (hon : L.mem s (affA t) = L.cpu s) : + sched.dequeue st s t ⦃ s' => ∃ σ', Fl4SpecA64.tcbSchedAction (Z := Unit) Fl4SpecA64.tcbSchedDequeue (specPtr t) σ = + ({((), σ')}, False) ∧ σ'.kheap = σ.kheap ∧ σ'.releaseQueue = σ.releaseQueue ∧ + (∀ u ∈ Z.T, L.mem s' (scA u) = L.mem s (scA u)) ∧ + SchedSRA st { Z with Q := deqQA (st := st) s t Z.Q } m κ s' σ' ∧ MFrA (st := st) Z m s s' σ σ' ⦄ := by + refine WP.spec_mono (dequeue_refines_nodeA H.toSchedSRN ht hon) ?_ + rintro s' ⟨σ', e, hk, hr, hf, -, hsc, H', F⟩ + exact ⟨σ', e, hk, hr, hsc, ⟨H', RdA.of_eq H.rd hk hf.curTime hr⟩, F⟩ + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScDeqCoreA.lean b/proofs/Fl4ProofsA64/P3/ScDeqCoreA.lean new file mode 100644 index 000000000..69633b742 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScDeqCoreA.lean @@ -0,0 +1,108 @@ +/- +P3 (AArch64): ready dequeue on the current core, used by SC unbind. +`sched.dequeue.coreA` uses A64 P1's ready-list and write-footprint proofs. +`dequeue_refines_localA` ports x86 P3's `dequeue_refines`; its frame is `MFrA`. +The affinity equality selects the current-core view. No named premises. +-/ +import Fl4ProofsA64.P3.ScSWrA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- The ghost ready lists after removing `t` from its priority on its core. -/ +def deqQA (s : S) (t : U64) (Q : U64 → U64 → List U64) : U64 → U64 → List U64 := + if (L.mem s (inqA t) &&& sched.Q_QUEUED) ≠ 0#u64 then + Function.update Q (L.mem s (affA t)) (Function.update (Q (L.mem s (affA t))) (L.mem s (prioA t)) + ((Q (L.mem s (affA t)) (L.mem s (prioA t))).erase t)) + else Q + +theorem deq_ready_coreA {T Cs : Set U64} {s s' : S} {Q : U64 → U64 → List U64} {R : U64 → List U64} + (H : SchedCoreA st T Cs s Q R) (hcur : L.cpu s ∈ Cs) {t : U64} (ht : t ∈ T) + {c p : U64} (hc : L.mem s (affA t) = c) (hp : L.mem s (prioA t) = p) + (hr : ReadyQueues st T s' c (Function.update (Q c) p ((Q c p).erase t))) + (hw : WritesIn st s s' (L.QFoot s (Q c p) c (136 ≤ ·) t)) + (hinq : L.mem s' (inqA t) = L.mem s (inqA t) &&& ~~~sched.Q_QUEUED) : + SchedCoreA st T Cs s' (Function.update Q c (Function.update (Q c) p ((Q c p).erase t))) R := by + have hc' : c ∈ Cs := hc ▸ H.aff t ht + have hp' : p.val < 256 := hp ▸ H.prio t ht + have hnd := ((H.ready c hc').1.rel p hp').nodup + have hmem : ∀ y, y ∈ (Q c p).erase t ↔ (y = t ∧ False) ∨ (y ≠ t ∧ y ∈ Q c p) := by + intro y + rw [hnd.mem_erase_iff] + simp [and_comm] + have hqq : (L.mem s' (inqA t) &&& sched.Q_QUEUED) = 0#u64 := by rw [hinq, andn_qq_qq] + have hqr : L.mem s' (inqA t) &&& sched.Q_RELEASE = L.mem s (inqA t) &&& sched.Q_RELEASE := by rw [hinq, andn_qq_qr] + exact core_ready_step (tIn := False) H hcur ht hc hp (fun _ hy => Or.inr hy) + (fun hm _ hc'' => H.QR hc' hp' hm hc'') hw hr hmem + (iff_false_intro fun h => h hqq) hqr False.elim + +theorem sched.dequeue.coreA {T Cs : Set U64} {s : S} {Q : U64 → U64 → List U64} {R : U64 → List U64} + (H : SchedCoreA st T Cs s Q R) (hcur : L.cpu s ∈ Cs) {t : U64} (ht : t ∈ T) : + sched.dequeue st s t ⦃ s' => SchedCoreA st T Cs s' (deqQA (st := st) s t Q) R ∧ + WritesIn st s s' (L.QFootN s (Q (L.mem s (affA t)) (L.mem s (prioA t))) (L.mem s (affA t)) (136 ≤ ·) t) ⦄ := by + have hT := H.tcbs + have hc := H.aff t ht + have hp := H.prio t ht + have hN := H.nodes _ hc + have hN' := H.nodes _ hcur + have hR := H.ready _ hc + have hmem := (H.queued t ht).1 + have hd := sched.dequeue.ready s t _ hT ht hp hN hR hmem + have hf := sched.dequeue.footN s t _ hT ht hp hN hN' (hR.1.rel _ hp) hmem + refine WP.spec_mono (WP.spec_and hd hf) ?_ + rintro s' ⟨hready, hwN, hinq⟩ + have hw := hwN.mono fun _ h => h.toQFoot + refine ⟨?_, hwN⟩ + unfold deqQA + by_cases hb : (L.mem s (inqA t) &&& sched.Q_QUEUED) ≠ 0#u64 + · rw [if_pos hb] at hready hinq ⊢ + exact deq_ready_coreA H hcur ht rfl rfl hready hw hinq + · let c := L.mem s (affA t) + let p := L.mem s (prioA t) + have h0 := not_not.1 hb + have htq := H.notQ ht h0 hc hp + rw [if_neg hb] at hready hinq ⊢ + have hcore := core_ready_step (c := c) (p := p) (W := Q c p) (xs' := Q c p) (tIn := False) H hcur ht rfl rfl + (fun _ hw => Or.inr hw) (fun h => absurd h htq) hw + (by rw [Function.update_eq_self]; exact hready) + (fun y => by + constructor + · intro hy + refine Or.inr ⟨?_, hy⟩ + intro e + subst y + exact htq hy + · rintro (⟨_, h⟩ | ⟨_, h⟩) + · exact h.elim + · exact h) + (by rw [hinq]; exact iff_false_intro hb) (by rw [hinq]) False.elim + simpa only [Function.update_eq_self] using hcore + +section Spec +open Fl4SpecA64 + +private theorem filter_erase_map {α β : Type} [BEq α] [LawfulBEq α] [DecidableEq β] + (f : α → β) (hf : Function.Injective f) {xs : List α} (h : xs.Nodup) (t : α) : + (xs.map f).filter (fun a => decide (f t ≠ a)) = (xs.erase t).map f := by + rw [List.filter_map, List.Nodup.erase_eq_filter h] + congr 1 + apply List.filter_congr + intro a _ + rw [Bool.eq_iff_iff] + change (decide (f t ≠ f a) = true) ↔ (a != t) = true + rw [decide_eq_true_eq, bne_iff_ne] + exact ⟨fun h e => h (congrArg f e.symm), fun h e => h (hf e).symm⟩ + +theorem tcbSchedDequeue_mapA {xs : List U64} (h : xs.Nodup) (t : U64) : + tcbSchedDequeue (specPtr t) (xs.map specPtr) = (xs.erase t).map specPtr := + filter_erase_map specPtr (fun _ _ e => specPtr_inj e) h t + +end Spec + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScDomKeepA.lean b/proofs/Fl4ProofsA64/P3/ScDomKeepA.lean new file mode 100644 index 000000000..76d758f4e --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScDomKeepA.lean @@ -0,0 +1,71 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2b): **the concrete domain-schedule words across the schedule's non-advancing +steps**. `NdOKA` is kept by any scheduler frame (`MFrA`) whose spec step keeps the domain position, start and table +(`DomKeepA`, from the `QInst` instances below, moved here from `P3/Stage3SchedTailQA`). The advancing step is +`next_domain_refinesA` (`P3/ScDomainA`). + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3QKeepPresA +import Fl4ProofsA64.P3.Stage3SchedWrA +import Fl4ProofsA64.P3.SpecFrameSchedA +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec Classical Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +instance (eop : DetExtMonad Unit) : QInst (doExtendedOp (Z := Unit) eop) := + ⟨by + unfold doExtendedOp + exact PresR.bind PresR.get fun _ => PresR.bind (PresR.selectF _) fun _ => QPresA.modify_fix _ (fun _ => rfl) fun _ => ⟨rfl, rfl, rfl, rfl⟩⟩ + +instance : QInst (fl4ChooseThread (Z := Unit)) := ⟨by unfold fl4ChooseThread; qpres⟩ +instance (t : ObjRef) : QInst (fl4SwitchToThread (Z := Unit) t) := ⟨by unfold fl4SwitchToThread; qpres⟩ +instance (p : CslotPtr) : QInst (getCap (Z := Unit) p) := ⟨by unfold getCap; qpres⟩ +instance (q : List ObjRef) : QInst (fl4ReleaseDeadline (Z := Unit) q) := ⟨by unfold fl4ReleaseDeadline; qpres⟩ +instance (timer : State Unit → Fl4TimerView) : QInst (fl4SetNextInterrupt (Z := Unit) timer) := + ⟨by unfold fl4SetNextInterrupt; qpres⟩ +instance : QInst (tcbReleaseDequeue (Z := Unit)) := ⟨by unfold tcbReleaseDequeue; qpres⟩ +instance : QInst (awaken (Z := Unit)) := ⟨by unfold awaken; qpres⟩ +instance : QInst (checkDomainTime (Z := Unit)) := ⟨by unfold checkDomainTime; qpres⟩ + +instance (p : Option ObjRef) : QInst (ifConstantBandwidthRefillUnblockCheck (Z := Unit) p) := + ⟨by unfold ifConstantBandwidthRefillUnblockCheck; qpres⟩ +instance : QInst (fl4CommitTime (Z := Unit)) := ⟨by unfold fl4CommitTime; qpres⟩ +instance : QInst (fl4SwitchSchedContext (Z := Unit)) := ⟨by unfold fl4SwitchSchedContext; qpres⟩ +instance (timer : State Unit → Fl4TimerView) : QInst (fl4ScAndTimer (Z := Unit) timer) := + ⟨by unfold fl4ScAndTimer; qpres⟩ + +/-- A spec step with a `QInst` instance keeps the domain schedule. -/ +theorem domKeep_ofQA {α : Type} (m : SMonad Unit α) [h : QInst m] {σ τ : State Unit} {x : α} + (e : m σ = ({(x, τ)}, False)) : DomKeepA σ τ := + (h.pres σ (x, τ) (by rw [e]; exact Set.mem_singleton _)).dom + +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- A scheduler-step word is off the domain-schedule words. -/ +theorem stepW_domA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRN st Z m κ s σ) {b : U64} (h : SchedStepWA (st := st) Z m σ s b) : ¬ DomW (L.ksb s) b := by + intro hd + have hksz : ksSizeA = 788736 := rfl + unfold DomW at hd + rcases stepW_casesA H h with h | h | ⟨t, ht, t1, t2, -⟩ + · obtain ⟨c, hc, x, n, hx, ha⟩ := h + have := (H.scp.out c hc x n hx _ ha).2 + omega + · unfold SKsA at h; omega + · have := H.tcb_ks ht + omega + +/-- (record edit #8b-B1) `NdOKA` across a scheduler frame whose spec step keeps the domain schedule; the scheduler +relation is taken at the pre state. -/ +theorem NdOKA.keepF {Z : XSetsA} {m : McState} {κ : U64 → U64} {s s' : S} {σ σ' : State Unit} + (N : NdOKA (st := st) s σ) (D : DomKeepA σ σ') (H : SchedSRN st Z m κ s σ) + (F : MFrA (st := st) Z m s s' σ σ') : NdOKA (st := st) s' σ' := + N.keepS H D F.wr.1 F.wr.2.1 fun b hb hd => F.wr.2.2 b hb fun h => stepW_domA H (Or.inl h) hd + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScDomainA.lean b/proofs/Fl4ProofsA64/P3/ScDomainA.lean new file mode 100644 index 000000000..a8574dae5 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScDomainA.lean @@ -0,0 +1,771 @@ +import Fl4ProofsA64.P3.ScBudgetTopA +import Fl4SpecA64.Ext.SchedTail +import Fl4SpecA64.Init +import Fl4ProofsA64.P3.ScChooseA + +/-! +Local next-domain writes and silent IPI dispatch. [J] `NdOKA` relates +the concrete array, limit, running index and start index to the spec's +domain schedule, and (record edit F1) the node-count word `NUM_NODES` to the platform's `numNodes`, and (record edit +F4) the `KERNEL_ROOT` word to the spec's global user VSpace (`armUsGlobalVspace`). The +zero-duration marker applies after the bounds +check. [S] The silent tail requires both IPI words to be zero. +-/ + +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3.Sched + +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +def domIndexWordA (s : S) : U64 := + L.mem s (core.num.U64.wrapping_add (L.ksb s) layout.ks.DOM_SCHED_IDX) + +def domStartWordA (s : S) : U64 := + L.mem s (core.num.U64.wrapping_add (L.ksb s) layout.ks.DOM_SCHED_START) + +private def domainWriteA (s : S) (off value : U64) : S := + L.wr s (core.num.U64.wrapping_add (L.ksb s) off) value + +def nextDomainPrefixA (s : S) (index : U64) : S := + let s1 := domainWriteA (st := st) s layout.ks.DOM_SCHED_IDX index + let s2 := L.wr s1 (L.nodeA s1 layout.node.REPROGRAM) 1#u64 + domainWriteA (st := st) s2 layout.ks.WORK_UNITS 0#u64 + +/-- [J] The finite concrete schedule words match the abstract entries. +The prefix writes preserve the selected domain entry. -/ +structure NdOKA (s : S) (σ : State Unit) where + index : (domIndexWordA (st := st) s).val = σ.domainIndex + start : (domStartWordA (st := st) s).val = σ.domainStartIndex + table : ∀ i : U64, i < layout.NUM_DOMAIN_SCHEDULES → + ∃ (p : Domain × DomainDuration) (d w : U64), σ.domainList[i.val]? = some p ∧ + d.bv = ucast 64 p.1 ∧ w.bv = ucast 64 p.2 ∧ + sched.dom_sched_duration st s i = ok w ∧ + sched.dom_sched_domain st (nextDomainPrefixA (st := st) s i) i = ok d ∧ + sched.dom_sched_duration st (nextDomainPrefixA (st := st) s i) i = ok w + startBound : domStartWordA (st := st) s < layout.NUM_DOMAIN_SCHEDULES + /-- The one-domain build's table is initialized at boot and preserved by + the domain invocations (x86 `DomListOK.dom`). -/ + dom : ∀ e ∈ σ.domainList, e.1 = 0 + /-- The spec table has FL4's number of entries (boot's two-entry list; the spec writes it only by `List.set`). -/ + len : σ.domainList.length = layout.NUM_DOMAIN_SCHEDULES.val + /-- (record edit F1) The kernel-state block's node-count word `NUM_NODES` (offset 0) is the platform's `numNodes` + (boot writes it; no step writes it: `DomW` covers it, and the scheduler write set `SKsA` starts at offset 8). -/ + nodes : (L.mem s (core.num.U64.wrapping_add (L.ksb s) 0#u64)).val = fl4A64Platform.numNodes + /-- (record edit F4) **The `KERNEL_ROOT` word (offset 48, in `DomW`) is the spec's global user VSpace**: + `vspace::kernel_root` reads it in `set_kernel_vm_root`, so `execIdle` / `execThread`'s `kernelRootWritesA σ` is what + FL4 installs. Written once at boot, kept by every step off `DomW` and by every spec step (`DomKeepA.gvs`). -/ + kroot : specPtr (L.mem s (krA (L := L) s)) = σ.archState.armUsGlobalVspace + +theorem krA_eq48A {s : S} : krA (L := L) s = core.num.U64.wrapping_add (L.ksb s) 48#u64 := by + unfold krA + rw [show layout.ks.KERNEL_ROOT = 48#u64 from by unfold layout.ks.KERNEL_ROOT; rfl] + +theorem NdOKA.entry_domain_zero {s : S} {σ : State Unit} + (H : NdOKA (st := st) s σ) {i : Nat} {e : Domain × DomainDuration} + (entry : σ.domainList[i]? = some e) : e.1 = 0 := + H.dom e (List.mem_of_getElem? entry) + +/-- The default schedule and its end marker both name domain zero. -/ +theorem initial_domain_list_zeroA (e : Domain × DomainDuration) + (h : e ∈ (initASt (Z := Unit)).domainList) : e.1 = 0 := by + simpa [initASt] using (show e ∈ ([(0, -1), (0, 0)] : List (Domain × DomainDuration)) → e.1 = 0 from by + intro h' + simp only [List.mem_cons, List.not_mem_nil, or_false] at h' + rcases h' with rfl | rfl <;> rfl) h + +/-- Both AArch64 boot views inherit the single-domain schedule. -/ +theorem boot_domain_list_zeroA (kb ib : U64) (nc : Nat) (now mt slice : U64) + (e : Domain × DomainDuration) : + (e ∈ (bootSA kb ib nc now mt).domainList → e.1 = 0) ∧ + (e ∈ (w1SA kb ib nc now mt slice).domainList → e.1 = 0) := + ⟨initial_domain_list_zeroA e, initial_domain_list_zeroA e⟩ + +/-- `DomainScheduleConfigure` checks the domain argument against +`NUM_DOMAINS`. `DomainScheduleSetStart` changes only the cursors. -/ +theorem domain_list_set_zeroA {entries : List (Domain × DomainDuration)} + (h : ∀ e ∈ entries, e.1 = 0) (i : Nat) (duration : DomainDuration) : + ∀ e ∈ entries.set i (0, duration), e.1 = 0 := by + intro e he + induction entries generalizing i with + | nil => simp at he + | cons x xs ih => + cases i with + | zero => + simp only [List.set_cons_zero, List.mem_cons] at he + rcases he with rfl | he + · rfl + · exact h e (List.mem_cons_of_mem _ he) + | succ i => + simp only [List.set_cons_succ, List.mem_cons] at he + rcases he with rfl | he + · exact h e (List.mem_cons_self ..) + · exact ih (fun x hx => h x (List.mem_cons_of_mem _ hx)) i he + +set_option maxHeartbeats 600000 in +private theorem domainAdvanceA {s : S} {σ : State Unit} (H : NdOKA (st := st) s σ) : + let i := core.num.U64.wrapping_add (domIndexWordA (st := st) s) 1#u64 + (if i ≥ layout.NUM_DOMAIN_SCHEDULES then ok (domStartWordA (st := st) s) else do + let dur ← sched.dom_sched_duration st s i + if dur = 0#u64 then ok (domStartWordA (st := st) s) else ok i) ⦃ k => + k < layout.NUM_DOMAIN_SCHEDULES ∧ + k.val = fl4NextDomainIndex layout.NUM_DOMAIN_SCHEDULES.val σ.domainIndex σ.domainStartIndex σ.domainList ⦄ := by + let i := core.num.U64.wrapping_add (domIndexWordA (st := st) s) 1#u64 + have hi : i.val = (σ.domainIndex + 1) % 2^64 := by + simp [i, core.num.U64.wrapping_add_val_eq, H.index, U64.size_def, U64.numBits] + have eq : i ≥ layout.NUM_DOMAIN_SCHEDULES ↔ layout.NUM_DOMAIN_SCHEDULES.val ≤ i.val := Iff.rfl + change (if i ≥ layout.NUM_DOMAIN_SCHEDULES then _ else _) ⦃ _ ⦄ + unfold fl4NextDomainIndex + rw [← hi] + dsimp only + by_cases b : i ≥ layout.NUM_DOMAIN_SCHEDULES + · simpa only [if_pos b, if_pos (eq.1 b), WP.spec_ok] using And.intro H.startBound H.start + · have ib : i < layout.NUM_DOMAIN_SCHEDULES := by + change ¬layout.NUM_DOMAIN_SCHEDULES.val ≤ i.val at b + exact Nat.lt_of_not_ge b + obtain ⟨p, d, w, hp, _, hv, hd, _, _⟩ := H.table i ib + have z : w = 0#u64 ↔ p.2 = 0 := by + constructor + · intro e + have e' := congrArg BitVec.toNat hv + rw [e] at e' + have lt : p.2.toNat < 2 ^ 64 := + Nat.lt_of_lt_of_le p.2.isLt (Nat.pow_le_pow_right (by decide) (Nat.sub_le _ _)) + apply BitVec.eq_of_toNat_eq + rw [BitVec.toNat_setWidth] at e' + change 0 = p.2.toNat % 2^64 at e' + rw [Nat.mod_eq_of_lt lt] at e' + exact e'.symm + · intro e + apply eq_of_bv + simpa [e] using hv + rw [if_neg b, if_neg (fun h => b (eq.2 h)), hd, bind_ok, hp] + change (if w = 0#u64 then _ else _) ⦃ k => _ ∧ UScalar.val k = (if p.2 = 0 then _ else _) ⦄ + by_cases h0 : w = 0#u64 + · simp only [if_pos h0, if_pos (z.1 h0), WP.spec_ok] + exact ⟨H.startBound, H.start⟩ + · simp only [if_neg h0, if_neg (fun e => h0 (z.2 e)), WP.spec_ok] + exact ⟨ib, rfl⟩ + +/-- The concrete configure decoder's successful one-domain guard fixes its +domain argument before the table write. -/ +theorem domain_configure_word_zeroA {d : U64} (valid : d < layout.NUM_DOMAINS) : d = 0#u64 := by + apply UScalar.eq_of_val_eq + change d.val < layout.NUM_DOMAINS.val at valid + have one : layout.NUM_DOMAINS.val = 1 := by + unfold layout.NUM_DOMAINS fl4_abi.config.NUM_DOMAINS + rfl + rw [one] at valid + have zero : (0#u64 : U64).val = 0 := rfl + omega + +/-- The abstract configure operation produces the domain-list field. -/ +theorem domain_schedule_configure_zeroA {σ τ : State Unit} {i : Nat} {w : DomainDuration} + (h : ∀ e ∈ σ.domainList, e.1 = 0) + (step : ((), τ) ∈ (domainScheduleConfigure i 0 w σ).1) : + ∀ e ∈ τ.domainList, e.1 = 0 := by + change ((), τ) ∈ ({((), {σ with domainList := σ.domainList.set i (0, w)})} : + Set (Unit × State Unit)) at step + have eq := (Prod.mk.inj (Set.mem_singleton_iff.1 step)).2 + rw [eq] + exact domain_list_set_zeroA h i w + +set_option maxHeartbeats 600000 in +/-- The five concrete next-domain writes and the matching choice in the +abstract domain list, including the zero-duration sentinel. -/ +theorem next_domain_wordsA {s : S} {σ : State Unit} (H : NdOKA (st := st) s σ) : + sched.next_domain st s ⦃ s' => ∃ k d w : U64, + let i := fl4NextDomainIndex layout.NUM_DOMAIN_SCHEDULES.val σ.domainIndex σ.domainStartIndex σ.domainList + k.val = i ∧ + (∃ p, σ.domainList[i]? = some p ∧ p.1 = 0 ∧ d = 0#u64 ∧ w.bv = ucast 64 p.2) ∧ + s' = domainWriteA (st := st) (domainWriteA (st := st) (nextDomainPrefixA (st := st) s k) layout.ks.CUR_DOMAIN d) + layout.ks.DOMAIN_TIME w ⦄ := by + unfold sched.next_domain + rw [eq_of_spec_fd (obj.ks_get.spec s _), bind_ok] + simp only [lift, bind_ok, eq_of_spec_fd (obj.ks_get.spec s layout.ks.DOM_SCHED_START)] + refine WP.spec_bind (domainAdvanceA H) ?_ + rintro i ⟨hi, hIndex⟩ + obtain ⟨p, d, w, hp, hd, hw, _, frD, frW⟩ := H.table i hi + have p0 := H.entry_domain_zero hp + have d0 : d = 0#u64 := eq_of_bv (by rw [hd, p0]; rfl) + rw [eq_of_spec_fd (obj.ks_set.spec s _ _), bind_ok, eq_of_spec_fd (sched.set_reprogram.spec _), bind_ok, + eq_of_spec_fd (obj.ks_set.spec _ _ _), bind_ok] + change (do + let d ← sched.dom_sched_domain st (nextDomainPrefixA (st := st) s i) i + let t ← sched.dom_sched_duration st (nextDomainPrefixA (st := st) s i) i + let s4 ← obj.ks_set st (nextDomainPrefixA (st := st) s i) layout.ks.CUR_DOMAIN d + obj.ks_set st s4 layout.ks.DOMAIN_TIME t) ⦃ _ ⦄ + rw [frD, bind_ok, frW, bind_ok, eq_of_spec_fd (obj.ks_set.spec _ _ _), bind_ok, + eq_of_spec_fd (obj.ks_set.spec _ _ _), WP.spec_ok] + exact ⟨i, d, w, hIndex, ⟨p, hIndex ▸ hp, p0, d0, hw⟩, rfl⟩ + +/-- The Unit view has no deterministic work counter. The remaining +changes are the new domain position and time and its timer request. -/ +theorem fl4NextDomain_unitA (limit : Nat) (σ : State Unit) : + let index := fl4NextDomainIndex limit σ.domainIndex σ.domainStartIndex σ.domainList + let entry := Fl4SpecA64.the σ.domainList[index]? + fl4NextDomain (Z := Unit) limit σ = + ({((), { σ with + domainIndex := index + curDomain := entry.1 + domainTime := ucast 64 entry.2 + reprogramTimer := true })}, False) := by + let index := fl4NextDomainIndex limit σ.domainIndex σ.domainStartIndex σ.domainList + let entry := Fl4SpecA64.the σ.domainList[index]? + let q : State Unit := { σ with + domainIndex := index + curDomain := entry.1 + domainTime := ucast 64 entry.2 + reprogramTimer := true } + change fl4NextDomain limit σ = ({((), q)}, False) + unfold fl4NextDomain + rw [nd_bind_oneSc (show NonDet.modify _ σ = ({((), q)}, False) from rfl)] + unfold doExtendedOp + rw [nd_bind_oneSc (show NonDet.get q = ({(q, q)}, False) from rfl)] + have select : NonDet.selectF (NonDet.mkEf (NonDet.pure () q)) q = ({(((), q), q)}, False) := by + apply Prod.ext + · ext ⟨⟨u, s⟩, s'⟩ + simp [NonDet.pure, NonDet.selectF, NonDet.mkEf, Prod.mk.injEq] + · simp [NonDet.pure, NonDet.selectF, NonDet.mkEf] + change (NonDet.selectF (NonDet.mkEf (NonDet.pure () q)) >>= _) q = _ + rw [nd_bind_oneSc select] + rfl + +private theorem domainGlobalValA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {σ : State Unit} (H : SchedSRN st Z m κ s σ) {k : U64} (hk : k.val < 224) : + (wa (L.ksb s) k).val = (L.ksb s).val + k.val := by + have fits := H.ksFit + have bound : 224 ≤ ksSizeA := by unfold ksSizeA; decide + rw [wa_valm] + exact Nat.mod_eq_of_lt (by omega) + +set_option maxHeartbeats 300000 in +/-- Frame the low global writes in `next_domain`. The current-domain +word remains zero, and the domain time and index reflect the entry. -/ +theorem writeDomainGlobalA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {σ : State Unit} (H : YieldQueueRelA (st := st) Z m κ s σ) + {o : U64} (ho : o.val < 224) (hal : o.val % 8 = 0) (v : U64) + (zero : o = 8#u64 → v = 0#u64) (index : Nat) : + let s' := domainWriteA (st := st) s o v + let τ := {σ with + domainIndex := index + domainTime := if o = 16#u64 then v.bv else σ.domainTime} + YieldQueueRelA (st := st) Z m κ s' τ ∧ MFrDA (st := st) Z m s s' σ τ := by + let addr := wa (L.ksb s) o + let s' := L.wr s addr v + let τ := {σ with + domainIndex := index + domainTime := if o = 16#u64 then v.bv else σ.domainTime} + have hold := H.sched.toSchedSRN + have value : addr.val = (L.ksb s).val + o.val := domainGlobalValA hold ho + have align : Aligned addr := by unfold Aligned; rw [value]; have := hold.ksb_al; omega + have inside : (L.ksb s).val ≤ addr.val ∧ addr.val < (L.ksb s).val + ksSizeA := by + rw [value] + have : 224 ≤ ksSizeA := by unfold ksSizeA; decide + omega + have insideN : SKsA (L.ksb s) addr ∨ DomW (L.ksb s) addr := by + unfold SKsA DomW; rw [value]; omega + have W : WritesIn st s s' (MWrDA (st := st) Z m σ s) := + (writesIn_wr align).mono fun _ e => by + subst e + exact insideN.elim (fun h => Or.inl (Or.inr (Or.inl h))) Or.inr + have nK (c : U64) (hc : c ∈ Z.Cs) (k : U64) (hk : k.val < 4288) (ha : k.val % 8 = 0) : + L.mem s' (L.nodeAt s c k) = L.mem s (L.nodeAt s c k) := by + have cl : c.val < 128 := by + obtain ⟨n, _, hn, cs⟩ := hold.csN + have := (cs c).1 hc + omega + have nval := nodeAt_valSc (s := s) hold.ksFit cl hk + have na : Aligned (L.nodeAt s c k) := by unfold Aligned; rw [nval]; have := hold.ksb_al; omega + exact L.mem_wr_ne align na (by rw [value, nval]; omega) + have tK (t : U64) (ht : t ∈ Z.T) (k : U64) (hk : k.val < 4096) (ha : k.val % 8 = 0) : + L.mem s' (wa t k) = L.mem s (wa t k) := by + have tAlign := (hold.core.tcbs t ht).align + have tFit := (hold.core.tcbs t ht).fits + have tv : (wa t k).val = t.val + k.val := by rw [wa_valm]; omega + have ta : Aligned (wa t k) := by unfold Aligned; rw [tv]; omega + exact L.mem_wr_ne align ta (by rw [value, tv]; have := hold.tcb_ks ht; omega) + have global (k : U64) (hk : k.val < 224) (ha : k.val % 8 = 0) : + L.mem s' (wa (L.ksb s) k) = if o = k then v else L.mem s (wa (L.ksb s) k) := by + by_cases e : o = k + · subst k + rw [if_pos rfl] + exact L.mem_wr_same align + · rw [if_neg e] + have kv := domainGlobalValA hold hk + exact L.mem_wr_ne align (by unfold Aligned; rw [kv]; have := hold.ksb_al; omega) + (by rw [value, kv]; intro eq; exact e (UScalar.eq_of_val_eq (by omega))) + have d0 : τ.curDomain = 0 ∧ L.mem s' (wa (L.ksb s) 8#u64) = 0#u64 := by + refine ⟨hold.rest.curDom.1, ?_⟩ + rw [global _ (by decide) (by decide)] + split + · exact zero ‹_› + · exact hold.rest.curDom.2 + have dt : τ.domainTime = (L.mem s' (wa (L.ksb s) 16#u64)).bv := by + rw [global _ (by decide) (by decide)] + change (if o = 16#u64 then v.bv else σ.domainTime) = _ + split + · rfl + · exact hold.rest.domTime + have core : SchedCoreA st Z.T Z.Cs s' Z.Q Z.R := + hold.core.agree W.1 + (fun t ht => ⟨tK t ht 3480#u64 (by decide) (by decide), tK t ht 3488#u64 (by decide) (by decide)⟩) + (fun c hc k hk ha _ => nK c hc k hk ha) + (fun t ht => tK t ht 3592#u64 (by decide) (by decide)) + (fun t ht => tK t ht 3520#u64 (by decide) (by decide)) + (fun t ht => tK t ht 3448#u64 (by decide) (by decide)) + have sc : ScStateRelA st Z.C m s' τ := + ⟨(hold.scr.wr_ks hold.scp align inside v).sc⟩ + have scWord (t : U64) (ht : t ∈ Z.T) : L.mem s' (scA t) = L.mem s (scA t) := by + rw [scA_eq] + exact tK t ht _ (by decide) (by decide) + have R : SchedSRN st Z m κ s' τ := by + refine hold.rebuildNodeDomainTimeA (Q' := Z.Q) (R' := Z.R) W.1 W.2.1 + (fun c hc k hk ha _ _ _ _ => nK c hc k (by omega) ha) d0 dt + (fun t ht k e => tK t ht k (by rcases e with rfl | rfl | rfl <;> decide) + (by rcases e with rfl | rfl | rfl <;> decide)) + sc (hold.scp.of_kheap (fun _ _ => rfl)) (fun _ _ x hx => ⟨x, hx, rfl, rfl⟩) + ?_ ?_ rfl rfl rfl ?_ ?_ ?_ ?_ hold.actOn (hold.inv.of_kheap rfl rfl) core + hold.rest.rq hold.rest.rqDom hold.rest.rel hold.run ?_ + · intro t ht + obtain ⟨x, hx, he⟩ := hold.sch.tcb t ht + exact ⟨x, hx, (congrArg (optScA m) (scWord t ht)).symm ▸ he⟩ + · intro t ht + rw [scWord t ht] + exact hold.rest.scMem t ht + · rw [nK _ hold.sch.cpuCs _ (by simp [layout.node.CUR_TIME]) (by simp [layout.node.CUR_TIME])] + exact hold.sch.time + · rw [nK _ hold.sch.cpuCs _ (by simp [layout.node.CONSUMED]) (by simp [layout.node.CONSUMED])] + exact hold.rest.consumed + · rw [nK _ hold.sch.cpuCs _ (by simp [layout.node.REPROGRAM]) (by simp [layout.node.REPROGRAM])] + exact hold.rest.repr + · rw [nK _ hold.sch.cpuCs _ (by simp [layout.node.SCHED_ACTION]) (by simp [layout.node.SCHED_ACTION])] + exact hold.sch.act + · intro c hc + exact (L.mem_wr_ne align (hold.scCoreFoot hc).1 (fun eq => by + have outside := hold.scp.ks_out (hold.scCoreFoot hc).2 + change addr.val = (scCoreA c).val at eq + rcases outside with h | h <;> omega)).trans (hold.scCore c hc) + obtain ⟨states, mcp, links⟩ := yieldQueueFrameDA (σ' := τ) H.sched H.states H.mcp H.links rfl W + exact ⟨⟨⟨R, RdA.of_eq H.sched.rd rfl rfl rfl⟩, states, mcp, links⟩, ⟨W, SKeepA.of_kheap rfl, SSzA.of_kheap rfl⟩⟩ + + + +private theorem domKsValA {k off : U64} (fit : k.val + ksSizeA ≤ 2 ^ 64) (h : off.val < 224) : + (core.num.U64.wrapping_add k off).val = k.val + off.val := by + unfold ksSizeA at fit + rw [wa_valm] + omega + +/-- The domain-advance prefix writes leave the table words alone. -/ +private theorem domPrefixTableA {s : S} (fit : (L.ksb s).val + ksSizeA ≤ 2 ^ 64) (al : (L.ksb s).val % 8 = 0) + (cpu : (L.cpu s).val < 128) (i : U64) {b : U64} (ba : b.val % 8 = 0) + (lo : (L.ksb s).val + 192 ≤ b.val) (hi : b.val < (L.ksb s).val + 224) : + L.mem (nextDomainPrefixA (st := st) s i) b = L.mem s b := by + have v24 := domKsValA (off := layout.ks.DOM_SCHED_IDX) fit (by simp [layout.ks.DOM_SCHED_IDX]) + have v40 := domKsValA (off := layout.ks.WORK_UNITS) fit (by simp [layout.ks.WORK_UNITS]) + have e24 : (layout.ks.DOM_SCHED_IDX).val = 24 := by simp [layout.ks.DOM_SCHED_IDX] + have e40 : (layout.ks.WORK_UNITS).val = 40 := by simp [layout.ks.WORK_UNITS] + have vn := nodeAt_valSc (s := s) (c := L.cpu s) (o := layout.node.REPROGRAM) fit cpu + (by simp [layout.node.REPROGRAM]) + have er : (layout.node.REPROGRAM).val = 56 := by simp [layout.node.REPROGRAM] + unfold nextDomainPrefixA domainWriteA + dsimp only + simp only [LawfulStore.nodeA, L.cpu_wr, L.ksb_wr, nodeAt_of_ksb (L.ksb_wr _ _ _)] + rw [L.mem_wr_ne, L.mem_wr_ne, L.mem_wr_ne] + all_goals first + | (unfold Aligned; omega) + | omega + +private theorem domReadsA {x y : S} + (h : ∀ off : U64, 192 ≤ off.val → off.val < 224 → off.val % 8 = 0 → + L.mem x (core.num.U64.wrapping_add (L.ksb x) off) = L.mem y (core.num.U64.wrapping_add (L.ksb y) off)) + {i : U64} (hi : i < layout.NUM_DOMAIN_SCHEDULES) : + sched.dom_sched_duration st x i = sched.dom_sched_duration st y i ∧ + sched.dom_sched_domain st x i = sched.dom_sched_domain st y i := by + have two : (layout.NUM_DOMAIN_SCHEDULES).val = 2 := by + simp [layout.NUM_DOMAIN_SCHEDULES, fl4_abi.config.NUM_DOMAIN_SCHEDULES] + have : i.val = 0 ∨ i.val = 1 := by + have : i.val < layout.NUM_DOMAIN_SCHEDULES.val := hi + omega + have cases : i = 0#u64 ∨ i = 1#u64 := by + rcases this with e | e + · exact Or.inl (UScalar.eq_of_val_eq (by simp [e])) + · exact Or.inr (UScalar.eq_of_val_eq (by simp [e])) + have table : layout.ks.DOM_SCHEDULE = ok 192#u64 := by + unfold layout.ks.DOM_SCHEDULE layout.WORD + rfl + have get (z : S) (off : U64) : obj.ks_get st z off = ok (L.mem z (core.num.U64.wrapping_add (L.ksb z) off)) := + eq_of_spec_fd (obj.ks_get.spec z off) + rcases cases with rfl | rfl <;> + refine ⟨?_, ?_⟩ <;> + simp only [sched.dom_sched_duration, sched.dom_sched_domain, table, lift, bind_ok, get] <;> + exact congrArg ok (h _ (by decide) (by decide) (by decide)) + +/-- `NdOKA` after a move of the schedule position: the spec table and start are kept, the new position word matches, +and the start and table words are as before. -/ +theorem NdOKA.moveIdx {s s1 : S} {σ σ1 : State Unit} (H : NdOKA (st := st) s σ) + (hs : σ1.domainStartIndex = σ.domainStartIndex) (hl : σ1.domainList = σ.domainList) + (hg : σ1.archState.armUsGlobalVspace = σ.archState.armUsGlobalVspace) + (hk : L.ksb s1 = L.ksb s) (hc : L.cpu s1 = L.cpu s) + (hi : (domIndexWordA (st := st) s1).val = σ1.domainIndex) + (hw : ∀ b : U64, Aligned b → (b.val = (L.ksb s).val ∨ b.val = (L.ksb s).val + 32 ∨ + b.val = (L.ksb s).val + 48 ∨ ((L.ksb s).val + 192 ≤ b.val ∧ b.val < (L.ksb s).val + 224)) → + L.mem s1 b = L.mem s b) + (fit : (L.ksb s).val + ksSizeA ≤ 2 ^ 64) (al : (L.ksb s).val % 8 = 0) (cpu : (L.cpu s).val < 128) : + NdOKA (st := st) s1 σ1 := by + have head (off : U64) (lo : off.val = 32 ∨ off.val = 48 ∨ 192 ≤ off.val) (hi : off.val < 224) (a8 : off.val % 8 = 0) : + L.mem s1 (core.num.U64.wrapping_add (L.ksb s1) off) = L.mem s (core.num.U64.wrapping_add (L.ksb s) off) := by + rw [hk] + have v := domKsValA fit hi + exact hw _ (by unfold Aligned; omega) (by omega) + have n0 : L.mem s1 (core.num.U64.wrapping_add (L.ksb s1) 0#u64) = + L.mem s (core.num.U64.wrapping_add (L.ksb s) 0#u64) := by + rw [hk] + have z : (0#u64 : U64).val = 0 := rfl + have v := domKsValA fit (off := 0#u64) (by rw [z]; omega) + exact hw _ (by unfold Aligned; omega) (by omega) + have start : domStartWordA (st := st) s1 = domStartWordA (st := st) s := + head _ (by simp [layout.ks.DOM_SCHED_START]) (by simp [layout.ks.DOM_SCHED_START]) (by simp [layout.ks.DOM_SCHED_START]) + have kroot : specPtr (L.mem s1 (krA (L := L) s1)) = σ1.archState.armUsGlobalVspace := by + rw [krA_eq48A, head 48#u64 (Or.inr (Or.inl (by decide))) (by decide) (by decide), hg, ← krA_eq48A] + exact H.kroot + refine ⟨hi, by rw [start, hs]; exact H.start, ?_, by rw [start]; exact H.startBound, by rw [hl]; exact H.dom, + by rw [hl]; exact H.len, by rw [n0]; exact H.nodes, kroot⟩ + intro i hi + obtain ⟨p, d, w, hp, hd, hw, r1, r2, r3⟩ := H.table i hi + have fit1 : (L.ksb s1).val + ksSizeA ≤ 2 ^ 64 := by rw [hk]; exact fit + have al1 : (L.ksb s1).val % 8 = 0 := by rw [hk]; exact al + have cpu1 : (L.cpu s1).val < 128 := by rw [hc]; exact cpu + have now := domReadsA (st := st) (x := s1) (y := s) (fun off lo h a => head off (Or.inr (Or.inr lo)) h a) hi + have pk (z : S) (j : U64) : L.ksb (nextDomainPrefixA (st := st) z j) = L.ksb z := by + unfold nextDomainPrefixA domainWriteA + simp only [L.ksb_wr] + have pre := domReadsA (st := st) (x := nextDomainPrefixA (st := st) s1 i) + (y := nextDomainPrefixA (st := st) s i) (fun off lo h a => by + rw [pk, pk] + have v := domKsValA fit h + have v1 := domKsValA fit1 h + rw [domPrefixTableA fit1 al1 cpu1 i (by rw [v1]; omega) (by rw [v1]; omega) (by rw [v1]; omega), + domPrefixTableA fit al cpu i (by rw [v]; omega) (by rw [v]; omega) (by rw [v]; omega)] + exact head off (Or.inr (Or.inr lo)) h a) hi + exact ⟨p, d, w, by rw [hl]; exact hp, hd, hw, now.1.trans r1, pre.2.trans r2, pre.1.trans r3⟩ + +/-- The header words after the five `next_domain` writes: the position word is the new index; the start and table +words are as before. -/ +private theorem nextDomainHeadA {s : S} (fit : (L.ksb s).val + ksSizeA ≤ 2 ^ 64) (al : (L.ksb s).val % 8 = 0) + (cpu : (L.cpu s).val < 128) (k d w : U64) : + let s' := domainWriteA (st := st) (domainWriteA (st := st) (nextDomainPrefixA (st := st) s k) layout.ks.CUR_DOMAIN d) + layout.ks.DOMAIN_TIME w + L.ksb s' = L.ksb s ∧ L.cpu s' = L.cpu s ∧ domIndexWordA (st := st) s' = k ∧ + ∀ b : U64, Aligned b → (b.val = (L.ksb s).val ∨ b.val = (L.ksb s).val + 32 ∨ b.val = (L.ksb s).val + 48 ∨ + ((L.ksb s).val + 192 ≤ b.val ∧ b.val < (L.ksb s).val + 224)) → L.mem s' b = L.mem s b := by + have v8 := domKsValA (off := layout.ks.CUR_DOMAIN) fit (by simp [layout.ks.CUR_DOMAIN]) + have v16 := domKsValA (off := layout.ks.DOMAIN_TIME) fit (by simp [layout.ks.DOMAIN_TIME]) + have v24 := domKsValA (off := layout.ks.DOM_SCHED_IDX) fit (by simp [layout.ks.DOM_SCHED_IDX]) + have v40 := domKsValA (off := layout.ks.WORK_UNITS) fit (by simp [layout.ks.WORK_UNITS]) + have e8 : (layout.ks.CUR_DOMAIN).val = 8 := by simp [layout.ks.CUR_DOMAIN] + have e16 : (layout.ks.DOMAIN_TIME).val = 16 := by simp [layout.ks.DOMAIN_TIME] + have e24 : (layout.ks.DOM_SCHED_IDX).val = 24 := by simp [layout.ks.DOM_SCHED_IDX] + have e40 : (layout.ks.WORK_UNITS).val = 40 := by simp [layout.ks.WORK_UNITS] + have vn := nodeAt_valSc (s := s) (c := L.cpu s) (o := layout.node.REPROGRAM) fit cpu + (by simp [layout.node.REPROGRAM]) + have er : (layout.node.REPROGRAM).val = 56 := by simp [layout.node.REPROGRAM] + intro s' + have kb : L.ksb s' = L.ksb s := by + unfold s' nextDomainPrefixA domainWriteA; simp only [L.ksb_wr] + have cp : L.cpu s' = L.cpu s := by + unfold s' nextDomainPrefixA domainWriteA; simp only [L.cpu_wr] + refine ⟨kb, cp, ?_, fun b ba hb => ?_⟩ + · unfold domIndexWordA + rw [kb] + unfold s' nextDomainPrefixA domainWriteA + dsimp only + simp only [LawfulStore.nodeA, L.cpu_wr, L.ksb_wr, nodeAt_of_ksb (L.ksb_wr _ _ _)] + rw [L.mem_wr_ne, L.mem_wr_ne, L.mem_wr_ne, L.mem_wr_ne, L.mem_wr_same] + all_goals first + | (unfold Aligned; omega) + | (intro e; have := congrArg UScalar.val e; omega) + | omega + · have ba8 : b.val % 8 = 0 := ba + unfold s' nextDomainPrefixA domainWriteA + dsimp only + simp only [LawfulStore.nodeA, L.cpu_wr, L.ksb_wr, nodeAt_of_ksb (L.ksb_wr _ _ _)] + rw [L.mem_wr_ne, L.mem_wr_ne, L.mem_wr_ne, L.mem_wr_ne, L.mem_wr_ne] + all_goals first + | (unfold Aligned; omega) + | (intro e; have := congrArg UScalar.val e; omega) + | omega + +set_option maxHeartbeats 500000 in +/-- Relate the full advancement, including its work counter reset, +timer request and the zero-domain selection. -/ +theorem next_domain_refinesA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {σ : State Unit} (H : YieldQueueRelA (st := st) Z m κ s σ) (N : NdOKA (st := st) s σ) : + sched.next_domain st s ⦃ s' => ∃ τ, + fl4NextDomain (Z := Unit) layout.NUM_DOMAIN_SCHEDULES.val σ = ({((), τ)}, False) ∧ + YieldQueueRelA (st := st) Z m κ s' τ ∧ MFrDA (st := st) Z m s s' σ τ ∧ NdOKA (st := st) s' τ ⦄ := by + refine WP.spec_mono (next_domain_wordsA N) ?_ + rintro s' ⟨i, d, w, index, ⟨p, hp, p0, rfl, hw⟩, rfl⟩ + have aIdx : layout.ks.DOM_SCHED_IDX = 24#u64 := by unfold layout.ks.DOM_SCHED_IDX; rfl + have aWork : layout.ks.WORK_UNITS = 40#u64 := by unfold layout.ks.WORK_UNITS; rfl + have aDom : layout.ks.CUR_DOMAIN = 8#u64 := by unfold layout.ks.CUR_DOMAIN; rfl + have aTime : layout.ks.DOMAIN_TIME = 16#u64 := by unfold layout.ks.DOMAIN_TIME; rfl + obtain ⟨R1, F1⟩ := writeDomainGlobalA H (o := layout.ks.DOM_SCHED_IDX) + (by rw [aIdx]; decide) (by rw [aIdx]; decide) i (fun e => ((by decide : (24#u64 : U64) ≠ 8#u64) (aIdx.symm.trans e)).elim) i.val + obtain ⟨R2, F2⟩ := writeSchedulerTimerA R1 (o := layout.node.REPROGRAM) (Or.inl rfl) 1#u64 + obtain ⟨R3, F3⟩ := writeDomainGlobalA R2 (o := layout.ks.WORK_UNITS) + (by rw [aWork]; decide) (by rw [aWork]; decide) 0#u64 (by intro; rfl) i.val + obtain ⟨R4, F4⟩ := writeDomainGlobalA R3 (o := layout.ks.CUR_DOMAIN) + (by rw [aDom]; decide) (by rw [aDom]; decide) 0#u64 (by intro; rfl) i.val + obtain ⟨R5, F5⟩ := writeDomainGlobalA R4 (o := layout.ks.DOMAIN_TIME) + (by rw [aTime]; decide) (by rw [aTime]; decide) w (fun e => ((by decide : (16#u64 : U64) ≠ 8#u64) (aTime.symm.trans e)).elim) i.val + have fit := H.sched.toSchedSRN.ksFit + have al := H.sched.toSchedSRN.ksb_al + have cpu : (L.cpu s).val < 128 := by + obtain ⟨n, -, hn, hcs⟩ := H.sched.toSchedSRN.csN + exact lt_of_lt_of_le ((hcs _).1 H.sched.toSchedSRN.sch.cpuCs) hn + have head := nextDomainHeadA (st := st) fit al cpu i 0#u64 w + refine ⟨_, ?_, R5, (((F1.transA F2 rfl rfl).trans F3 rfl rfl).trans F4 rfl rfl).trans F5 rfl rfl, + N.moveIdx rfl rfl rfl head.1 head.2.1 (congrArg (fun x : U64 => x.val) head.2.2.1) head.2.2.2 fit al cpu⟩ + rw [fl4NextDomain_unitA] + dsimp only + rw [hp, ← index] + rw [the_someA, ← hw, p0, H.sched.rest.curDom.1] + simp only [aTime, if_pos rfl, if_true, show decide ((1#u64 : U64) ≠ 0#u64) = true by decide] + +/-- [P] Under the one-domain platform agreement the expiration check +has no effect. The separate choose-new branch can still advance a +zero-time domain. -/ +theorem checkDomainTime_single_specA (σ : State Unit) (domains : numDomains = 1) : + checkDomainTime (Z := Unit) σ = ({((), σ)}, False) := by + unfold checkDomainTime + rw [nd_gets_bindSc] + have before : isCurDomainExpired σ = false := by simp [isCurDomainExpired, domains] + rw [before] + rfl + +/-- Split the concrete choose-new operation once at its domain-time +read. The zero branch supplies all writes and the selected domain table +entry to the choice continuation. -/ +theorem choose_new_thread_stepsA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRN st Z m κ s σ) (a : A) (P : S × A → Prop) + (stay : σ.domainTime ≠ 0 → sched.choose_thread st ar s a ⦃ P ⦄) + (next : σ.domainTime = 0 → NdOKA (st := st) s σ) + (advance : ∀ s' k d w, + σ.domainTime = 0 → + (let i := fl4NextDomainIndex layout.NUM_DOMAIN_SCHEDULES.val σ.domainIndex σ.domainStartIndex σ.domainList + k.val = i ∧ + (∃ p, σ.domainList[i]? = some p ∧ p.1 = 0 ∧ d = 0#u64 ∧ w.bv = ucast 64 p.2) ∧ + s' = domainWriteA (st := st) (domainWriteA (st := st) (nextDomainPrefixA (st := st) s k) layout.ks.CUR_DOMAIN d) + layout.ks.DOMAIN_TIME w) → + sched.choose_thread st ar s' a ⦃ P ⦄) : + sched.schedule_choose_new_thread st ar s a ⦃ P ⦄ := by + unfold sched.schedule_choose_new_thread sched.domain_time + rw [eq_of_spec_fd (obj.ks_get.spec s _), bind_ok] + rw [show layout.ks.DOMAIN_TIME = 16#u64 from by unfold layout.ks.DOMAIN_TIME; rfl] + let word := L.mem s (core.num.U64.wrapping_add (L.ksb s) 16#u64) + change (Std.bind (if word = 0#u64 then sched.next_domain st s else ok s) + (fun s' => sched.choose_thread st ar s' a)) ⦃ P ⦄ + have eq : word = 0#u64 ↔ σ.domainTime = 0 := by + rw [H.rest.domTime] + exact ⟨congrArg UScalar.bv, eq_of_bv⟩ + by_cases zero : word = 0#u64 + · rw [if_pos zero] + refine WP.spec_bind (next_domain_wordsA (next (eq.1 zero))) ?_ + rintro s' ⟨k, d, w, h⟩ + exact advance s' k d w (eq.1 zero) h + · rw [if_neg zero, bind_ok] + exact stay (fun hz => zero (eq.2 hz)) + + +/-- The concrete domain-schedule words (position, start and table, all in the kernel-state header after +`DOMAIN_TIME`) and the node-count word at offset 0 (record edit F1: the range starts at the block base, so it also +covers `CUR_DOMAIN` / `DOMAIN_TIME`) are kept: what the `domain` premises of the selector may also assume. -/ +structure DomWordsA (s s' : S) : Prop where + ksb : L.ksb s' = L.ksb s + cpu : L.cpu s' = L.cpu s + word : ∀ b : U64, Aligned b → (L.ksb s).val ≤ b.val → b.val < (L.ksb s).val + 224 → L.mem s' b = L.mem s b + +theorem DomWordsA.refl (s : S) : DomWordsA (st := st) s s := ⟨rfl, rfl, fun _ _ _ _ => rfl⟩ + +theorem DomWordsA.trans {s s1 s2 : S} (h1 : DomWordsA (st := st) s s1) (h2 : DomWordsA (st := st) s1 s2) : + DomWordsA (st := st) s s2 := + ⟨h2.ksb.trans h1.ksb, h2.cpu.trans h1.cpu, + fun b a lo hi => (h2.word b a (by rw [h1.ksb]; exact lo) (by rw [h1.ksb]; exact hi)).trans (h1.word b a lo hi)⟩ + +/-- `NdOKA.keep` asking only for the words `NdOKA` reads: the schedule position (24), start (32) and table +`[192, 224)`; `WORK_UNITS` (40) and the other global words may change. -/ +theorem NdOKA.keepR {s s1 : S} {σ σ1 : State Unit} (H : NdOKA (st := st) s σ) + (D : DomKeepA σ σ1) (hk : L.ksb s1 = L.ksb s) (hc : L.cpu s1 = L.cpu s) + (hw : ∀ b : U64, Aligned b → DomW (L.ksb s) b → L.mem s1 b = L.mem s b) (fit : (L.ksb s).val + ksSizeA ≤ 2 ^ 64) + (al : (L.ksb s).val % 8 = 0) (cpu : (L.cpu s).val < 128) : NdOKA (st := st) s1 σ1 := by + have head (off : U64) (lo : off.val = 24 ∨ off.val = 32 ∨ off.val = 48 ∨ 192 ≤ off.val) (hi : off.val < 224) + (a8 : off.val % 8 = 0) : + L.mem s1 (core.num.U64.wrapping_add (L.ksb s1) off) = L.mem s (core.num.U64.wrapping_add (L.ksb s) off) := by + rw [hk] + have v := domKsValA fit hi + exact hw _ (by unfold Aligned; omega) (by unfold DomW; omega) + have index : domIndexWordA (st := st) s1 = domIndexWordA (st := st) s := + head _ (by simp [layout.ks.DOM_SCHED_IDX]) (by simp [layout.ks.DOM_SCHED_IDX]) (by simp [layout.ks.DOM_SCHED_IDX]) + have start : domStartWordA (st := st) s1 = domStartWordA (st := st) s := + head _ (by simp [layout.ks.DOM_SCHED_START]) (by simp [layout.ks.DOM_SCHED_START]) (by simp [layout.ks.DOM_SCHED_START]) + have n0 : L.mem s1 (core.num.U64.wrapping_add (L.ksb s1) 0#u64) = + L.mem s (core.num.U64.wrapping_add (L.ksb s) 0#u64) := by + rw [hk] + have z : (0#u64 : U64).val = 0 := rfl + have v := domKsValA fit (off := 0#u64) (by rw [z]; omega) + exact hw _ (by unfold Aligned; omega) (by unfold DomW; omega) + have kroot : specPtr (L.mem s1 (krA (L := L) s1)) = σ1.archState.armUsGlobalVspace := by + rw [krA_eq48A, head 48#u64 (Or.inr (Or.inr (Or.inl (by decide)))) (by decide) (by decide), D.gvs, + ← krA_eq48A] + exact H.kroot + refine ⟨by rw [index, D.index]; exact H.index, by rw [start, D.start]; exact H.start, ?_, + by rw [start]; exact H.startBound, by rw [D.table]; exact H.dom, by rw [D.table]; exact H.len, + by rw [n0]; exact H.nodes, kroot⟩ + intro i hi + obtain ⟨p, d, w, hp, hd, hw, r1, r2, r3⟩ := H.table i hi + have fit1 : (L.ksb s1).val + ksSizeA ≤ 2 ^ 64 := by rw [hk]; exact fit + have al1 : (L.ksb s1).val % 8 = 0 := by rw [hk]; exact al + have cpu1 : (L.cpu s1).val < 128 := by rw [hc]; exact cpu + have now := domReadsA (st := st) (x := s1) (y := s) (fun off lo h a => head off (Or.inr (Or.inr (Or.inr lo))) h a) hi + have pk (z : S) (j : U64) : L.ksb (nextDomainPrefixA (st := st) z j) = L.ksb z := by + unfold nextDomainPrefixA domainWriteA + simp only [L.ksb_wr] + have pre := domReadsA (st := st) (x := nextDomainPrefixA (st := st) s1 i) + (y := nextDomainPrefixA (st := st) s i) (fun off lo h a => by + rw [pk, pk] + have v := domKsValA fit h + have v1 := domKsValA fit1 h + rw [domPrefixTableA fit1 al1 cpu1 i (by rw [v1]; omega) (by rw [v1]; omega) (by rw [v1]; omega), + domPrefixTableA fit al cpu i (by rw [v]; omega) (by rw [v]; omega) (by rw [v]; omega)] + exact head off (Or.inr (Or.inr (Or.inr lo))) h a) hi + exact ⟨p, d, w, by rw [D.table]; exact hp, hd, hw, now.1.trans r1, pre.2.trans r2, pre.1.trans r3⟩ + + +/-- (record edit #8b-B1) A domain-schedule word is a header word `wa k o` (`o < 224`) of its block. -/ +theorem DomW.wa_exists {k b : U64} (fit : k.val + ksSizeA ≤ 2 ^ 64) (h : DomW k b) : + ∃ o : U64, o.val < 224 ∧ core.num.U64.wrapping_add k o = b := by + have hksz : ksSizeA = 788736 := rfl + unfold DomW at h + have e : (⟨BitVec.ofNat 64 (b.val - k.val)⟩ : U64).val = b.val - k.val := by + show (BitVec.ofNat 64 _).toNat = _ + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + refine ⟨⟨BitVec.ofNat 64 (b.val - k.val)⟩, by rw [e]; omega, ?_⟩ + exact UScalar.eq_of_val_eq (by rw [wa_valm, e]; rw [Nat.mod_eq_of_lt (by omega)]; omega) + +/-- (record edit #8b-B1) `NdOKA.keepR` from the scheduler relation at the earlier state (its block base is aligned and +fits, its core is a kernel core): what a step off the domain-schedule words (`DomW`) keeps. -/ +theorem NdOKA.keepS {Z : XSetsA} {m : McState} {κ : U64 → U64} {s s1 : S} {σ σ1 : State Unit} + (H : SchedSRN st Z m κ s σ) (N : NdOKA (st := st) s σ) (D : DomKeepA σ σ1) (hk : L.ksb s1 = L.ksb s) + (hc : L.cpu s1 = L.cpu s) (hw : ∀ b : U64, Aligned b → DomW (L.ksb s) b → L.mem s1 b = L.mem s b) : + NdOKA (st := st) s1 σ1 := by + obtain ⟨n, -, hn, hcs⟩ := H.csN + exact N.keepR D hk hc hw H.ksFit H.ksb_al (lt_of_lt_of_le ((hcs _).1 H.sch.cpuCs) hn) + +/-- `NdOKA` follows from `NdOKA` at an earlier state when the spec position and table and the concrete domain +words are kept. -/ +theorem NdOKA.keep {s s1 : S} {σ σ1 : State Unit} (H : NdOKA (st := st) s σ) + (D : DomKeepA σ σ1) (W : DomWordsA (st := st) s s1) (fit : (L.ksb s).val + ksSizeA ≤ 2 ^ 64) + (al : (L.ksb s).val % 8 = 0) (cpu : (L.cpu s).val < 128) : NdOKA (st := st) s1 σ1 := + H.keepR D W.ksb W.cpu (fun b a h => W.word b a (by unfold DomW at h; omega) (by unfold DomW at h; omega)) fit al cpu + +/-- (record edit F1) **The kernel's node count read from the record**: `ks_get` of `NUM_NODES` (offset 0) is the +platform's `numNodes` at any state with `NdOKA`. -/ +theorem NdOKA.nodesGet {s : S} {σ : State Unit} (N : NdOKA (st := st) s σ) {nodes : U64} + (h : obj.ks_get st s layout.ks.NUM_NODES = ok nodes) : nodes.val = fl4A64Platform.numNodes := by + have e : layout.ks.NUM_NODES = 0#u64 := by unfold layout.ks.NUM_NODES; rfl + rw [e, eq_of_spec_fd (obj.ks_get.spec s 0#u64)] at h + rw [← ok_inj_ff h] + exact N.nodes + +/-- (record edit #8b-B) **`NdOKA` from the boot schedule words**: position and start `0`, entry 0 = +`(0, 2^56 - 1)`, entry 1 = `(0, 0)`, the spec at `initASt`'s schedule. -/ +theorem NdOKA.of_bootWordsA {s : S} {σ : State Unit} + (hi : σ.domainIndex = 0) (hst : σ.domainStartIndex = 0) (hl : σ.domainList = [(0, -1), (0, 0)]) + (w24 : L.mem s (core.num.U64.wrapping_add (L.ksb s) 24#u64) = 0#u64) + (w32 : L.mem s (core.num.U64.wrapping_add (L.ksb s) 32#u64) = 0#u64) + (w192 : L.mem s (core.num.U64.wrapping_add (L.ksb s) 192#u64) = 0#u64) + (w200 : L.mem s (core.num.U64.wrapping_add (L.ksb s) 200#u64) = 72057594037927935#u64) + (w208 : L.mem s (core.num.U64.wrapping_add (L.ksb s) 208#u64) = 0#u64) + (w216 : L.mem s (core.num.U64.wrapping_add (L.ksb s) 216#u64) = 0#u64) + (w0 : (L.mem s (core.num.U64.wrapping_add (L.ksb s) 0#u64)).val = fl4A64Platform.numNodes) + (w48 : specPtr (L.mem s (core.num.U64.wrapping_add (L.ksb s) 48#u64)) = σ.archState.armUsGlobalVspace) + (fit : (L.ksb s).val + ksSizeA ≤ 2 ^ 64) (al : (L.ksb s).val % 8 = 0) (cpu : (L.cpu s).val < 128) : + NdOKA (st := st) s σ := by + have a24 : layout.ks.DOM_SCHED_IDX = 24#u64 := by unfold layout.ks.DOM_SCHED_IDX; rfl + have a32 : layout.ks.DOM_SCHED_START = 32#u64 := by unfold layout.ks.DOM_SCHED_START; rfl + have idx : domIndexWordA (st := st) s = 0#u64 := by unfold domIndexWordA; rw [a24]; exact w24 + have start : domStartWordA (st := st) s = 0#u64 := by unfold domStartWordA; rw [a32]; exact w32 + have two : (layout.NUM_DOMAIN_SCHEDULES).val = 2 := by + simp [layout.NUM_DOMAIN_SCHEDULES, fl4_abi.config.NUM_DOMAIN_SCHEDULES] + refine ⟨by rw [idx, hi]; rfl, by rw [start, hst]; rfl, ?_, ?_, ?_, by + rw [hl]; simp [layout.NUM_DOMAIN_SCHEDULES, fl4_abi.config.NUM_DOMAIN_SCHEDULES], w0, by + rw [krA_eq48A]; exact w48⟩ + rotate_left + · rw [start] + show (0#u64 : U64).val < layout.NUM_DOMAIN_SCHEDULES.val + rw [two]; decide + · intro e he + rw [hl] at he + simp only [List.mem_cons, List.not_mem_nil, or_false] at he + rcases he with rfl | rfl <;> rfl + intro i hlt + have table : layout.ks.DOM_SCHEDULE = ok 192#u64 := by + unfold layout.ks.DOM_SCHEDULE layout.WORD + rfl + have get (z : S) (off : U64) : obj.ks_get st z off = ok (L.mem z (core.num.U64.wrapping_add (L.ksb z) off)) := + eq_of_spec_fd (obj.ks_get.spec z off) + have pk (j : U64) : L.ksb (nextDomainPrefixA (st := st) s j) = L.ksb s := by + unfold nextDomainPrefixA domainWriteA + simp only [L.ksb_wr] + have pre (j : U64) (off : U64) (lo : 192 ≤ off.val) (h : off.val < 224) (a8 : off.val % 8 = 0) : + L.mem (nextDomainPrefixA (st := st) s j) + (core.num.U64.wrapping_add (L.ksb (nextDomainPrefixA (st := st) s j)) off) = + L.mem s (core.num.U64.wrapping_add (L.ksb s) off) := by + rw [pk] + have v := domKsValA fit h + exact domPrefixTableA fit al cpu j (by rw [v]; omega) (by rw [v]; omega) (by rw [v]; omega) + have : i.val = 0 ∨ i.val = 1 := by + have : i.val < layout.NUM_DOMAIN_SCHEDULES.val := hlt + omega + have cases : i = 0#u64 ∨ i = 1#u64 := by + rcases this with e | e + · exact Or.inl (UScalar.eq_of_val_eq (by simp [e])) + · exact Or.inr (UScalar.eq_of_val_eq (by simp [e])) + have o00 : core.num.U64.wrapping_add 192#u64 (core.num.U64.wrapping_mul 0#u64 16#u64) = 192#u64 := by decide + have o01 : core.num.U64.wrapping_add (192#u64 : U64) 8#u64 = 200#u64 := by decide + have o10 : core.num.U64.wrapping_add 192#u64 (core.num.U64.wrapping_mul 1#u64 16#u64) = 208#u64 := by decide + have o11 : core.num.U64.wrapping_add (208#u64 : U64) 8#u64 = 216#u64 := by decide + rcases cases with rfl | rfl + · refine ⟨(0, -1), 0#u64, 72057594037927935#u64, by rw [hl]; rfl, by decide, by decide, ?_, ?_, ?_⟩ + · simp only [sched.dom_sched_duration, table, lift, bind_ok, get, o00, o01, w200] + · simp only [sched.dom_sched_domain, table, lift, bind_ok, get, o00] + rw [pre _ 192#u64 (by decide) (by decide) (by decide), w192] + · simp only [sched.dom_sched_duration, table, lift, bind_ok, get, o00, o01] + rw [pre _ 200#u64 (by decide) (by decide) (by decide), w200] + · refine ⟨(0, 0), 0#u64, 0#u64, by rw [hl]; rfl, by decide, by decide, ?_, ?_, ?_⟩ + · simp only [sched.dom_sched_duration, table, lift, bind_ok, get, o10, o11, w216] + · simp only [sched.dom_sched_domain, table, lift, bind_ok, get, o10] + rw [pre _ 208#u64 (by decide) (by decide) (by decide), w208] + · simp only [sched.dom_sched_duration, table, lift, bind_ok, get, o10, o11] + rw [pre _ 216#u64 (by decide) (by decide) (by decide), w216] + +theorem send_pending_silentA (s : S) (a : A) + (lo : L.mem s (L.nodeAt s (L.cpu s) layout.node.IPI_PENDING) = 0#u64) + (hi : L.mem s (L.nodeAt s (L.cpu s) layout.node.IPI_PENDING_HI) = 0#u64) : + smp.send_pending st ar s a = ok (s, a) := by + unfold smp.send_pending + rw [L.cur_cpu_eq, bind_ok, eq_of_spec_fd (obj.node_get.spec s _ _), bind_ok, + eq_of_spec_fd (obj.node_get.spec s _ _), bind_ok, lo, hi] + simp only [lift, bind_ok, show (0#u64 ||| 0#u64) = 0#u64 from by decide, show (0#u64 != 0#u64) = false from by decide, Bool.false_eq_true, ↓reduceIte] + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScDomainChooseA.lean b/proofs/Fl4ProofsA64/P3/ScDomainChooseA.lean new file mode 100644 index 000000000..d093c86f0 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScDomainChooseA.lean @@ -0,0 +1,89 @@ +import Fl4ProofsA64.P3.ScDomainA +import Fl4ProofsA64.P3.SpecFrameSchedA +import Fl4ProofsA64.P3.ScDomKeepA + +/-! +The choose-new-thread phase with the domain advance (split from `P3/ScDomainA`, which `P3/Stage3A` imports for +`NdOKA`, so that it need not import `P3/SpecFrameSchedA`). The roots premise sees the spec frame `SchedFrameA` of the +domain step. +-/ + +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3.Sched + +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +/-- The domain row and architecture reads are consumed at the actual +choose-new phase. If domain time is zero, root selection sees the domain +successor, including its updated time and timer request. -/ +theorem schedule_choose_new_refinesA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {σ : State Unit} {a : A} {AR : A → HwRootA → Prop} + (H : YieldQueueRelA (st := st) Z m κ s σ) (P : ArchSwFA st ar) + (N : NdOKA (st := st) s σ) + (roots : ∀ s1 σ1, + YieldQueueRelA (st := st) Z m κ s1 σ1 → MFrDA (st := st) Z m s s1 σ σ1 → SchedFrameA σ σ1 → + JAtZAX (L := L) AR Z s1 a σ1 m ∧ VsRootOKA σ1 ∧ IdleVtA σ1 ∧ + L.mem s1 (affA (L.idleW s1)) = L.cpu s1 ∧ ArchRestA (L := L) Z s1) : + sched.schedule_choose_new_thread st ar s a ⦃ r => ∃ τ Q writes, + fl4ChooseNewThread (Z := Unit) layout.NUM_DOMAIN_SCHEDULES.val σ = ({((), τ)}, False) ∧ + P.rootWrites a r.2 (L.cpu s).val writes ∧ + YieldQueueRelA (st := st) {Z with Q := Q, curOf := Function.update Z.curOf m.curCore τ.curThread} + m κ r.1 τ ∧ MFrDA (st := st) Z m s r.1 σ τ ∧ + (NdOKA (st := st) s σ → NdOKA (st := st) r.1 τ) ⦄ := by + unfold sched.schedule_choose_new_thread sched.domain_time + rw [eq_of_spec_fd (obj.ks_get.spec s _), bind_ok, + show layout.ks.DOMAIN_TIME = 16#u64 from by unfold layout.ks.DOMAIN_TIME; rfl] + let word := L.mem s (wa (L.ksb s) 16#u64) + have wordEq : word = 0#u64 ↔ σ.domainTime = 0 := by + rw [H.sched.rest.domTime] + exact ⟨congrArg UScalar.bv, eq_of_bv⟩ + change (Std.bind (if word = 0#u64 then sched.next_domain st s else ok s) + (fun s' => sched.choose_thread st ar s' a)) ⦃ _ ⦄ + have choose (s1 : S) (σ1 : State Unit) (R : YieldQueueRelA (st := st) Z m κ s1 σ1) + (frame : MFrDA (st := st) Z m s s1 σ σ1) + (domain : (NonDet.when (σ.domainTime = 0) (fl4NextDomain layout.NUM_DOMAIN_SCHEDULES.val)) σ = + ({((), σ1)}, False)) + (nd : NdOKA (st := st) s σ → NdOKA (st := st) s1 σ1) : + sched.choose_thread st ar s1 a ⦃ r => ∃ τ Q writes, + fl4ChooseNewThread (Z := Unit) layout.NUM_DOMAIN_SCHEDULES.val σ = ({((), τ)}, False) ∧ + P.rootWrites a r.2 (L.cpu s).val writes ∧ + YieldQueueRelA (st := st) {Z with Q := Q, curOf := Function.update Z.curOf m.curCore τ.curThread} + m κ r.1 τ ∧ MFrDA (st := st) Z m s r.1 σ τ ∧ (NdOKA (st := st) s σ → NdOKA (st := st) r.1 τ) ⦄ := by + have stepped : SchedFrameA σ σ1 := by + by_cases z : σ.domainTime = 0 + · rw [NonDet.when, if_pos z] at domain + exact (inferInstance : SPresInst (fl4NextDomain (Z := Unit) layout.NUM_DOMAIN_SCHEDULES.val)).pres σ ((), σ1) + (by rw [domain]; exact Set.mem_singleton _) + · rw [NonDet.when, if_neg z] at domain + have e : σ1 = σ := + (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst domain))).2.symm + rw [e] + exact PreO.refl σ + obtain ⟨J, V, I, localIdle, T⟩ := roots s1 σ1 R frame stepped + have K := (nd N).kroot + refine WP.spec_mono (choose_thread_refinesA R J P V I ⟨T, K⟩ (J.1.idleT _ R.sched.sch.cpuCs) localIdle) ?_ + rintro r ⟨τ, Q, abs, trace, related, after⟩ + rw [frame.wr.2.1] at trace + refine ⟨τ, Q, _, ?_, trace, related, frame.transA after rfl rfl, + fun N0 => NdOKA.keepF (st := st) (nd N0) (domKeep_ofQA _ abs) R.sched.toSchedSRN after⟩ + unfold fl4ChooseNewThread + rw [nd_gets_bindSc, nd_bind_oneSc domain] + exact abs + by_cases h : word = 0#u64 + · rw [if_pos h] + refine WP.spec_bind (next_domain_refinesA H N) ?_ + rintro s1 ⟨σ1, abs, R, frame, nd1⟩ + exact choose s1 σ1 R frame (by rw [NonDet.when, if_pos (wordEq.1 h)]; exact abs) (fun _ => nd1) + · rw [if_neg h, bind_ok] + exact choose s σ H (MFrDA.refl _ _ _ _) (by rw [NonDet.when, if_neg (mt wordEq.2 h)]; rfl) id + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScDonA.lean b/proofs/Fl4ProofsA64/P3/ScDonA.lean new file mode 100644 index 000000000..5f48e6409 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScDonA.lean @@ -0,0 +1,191 @@ +/- +P3 (aarch64, A64-P3-MCS gen 4): **`maybe_donate_sched_context` without a donation** and **the wake-up's switch +step** over `SchedSRA` (x86 `P3/ScDonate`'s `maybe_donate_nodonate`; the `is_schedulable` / `possible_switch_to` +step of x86 `SchedHyps.wakeTail`'s proofs, `P3/SchedSwitch`), the pieces of the IPC line's `SchedOpsA.wakeTail` +that need no donation. + +* **`maybe_donate_nodonateA`** (x86 `maybe_donate_nodonate`): when the thread has an SC, or the notification has + none, or the notification's SC is bound to a TCB, FL4 and the spec change nothing. +* **`switch_if_refinesA`**: `let b ← is_schedulable t; if b then possible_switch_to t` refines + `do sched ← gets (schedulable t); when sched (possible_switch_to t)` for a target on the current core + (`possible_switch_to_refinesA`; ruling 22:45Z item 1: the l4v contracts take the local-core precondition). + +The notification's `SC` word is related by **`NtfnScEqA`** (a projection of the IPC line's `NtfnStateRel`'s +`ntfn` / `NtfnRelA.sc`, passed like `StEqA`; renamed by `optScA`, see the handoff's open question on `optRef`). +No named premise. +-/ +import Fl4ProofsA64.P3.ScSwitchA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-- A notification's `SC` word (= the IPC line's `NtfnRelA.nScA`, `rfl`). -/ +def nScSc (n : U64) : U64 := wa n layout.ntfn.SC + +/-! ## The spec -/ + +section Spec +open Fl4SpecA64 + +/-- (x86 `partialInv_ntfn`) -/ +theorem partialInv_ntfnSc (x : Notification) : + partialInv KernelObject.notification (.notification x) = some x := by + have hu : ∃! y, KernelObject.notification y = .notification x := ⟨x, rfl, fun y h => by cases h; rfl⟩ + unfold partialInv The + simp only [hu, ↓reduceIte, ↓reduceDIte] + have h := (Classical.choose_spec hu).1 + injection h with h + rw [h] + +theorem getSimpleKo_ntfnSc {σ : State Unit} {p : ObjRef} {z : Notification} + (hk : σ.kheap p = some (.notification z)) : + getSimpleKo (Z := Unit) KernelObject.notification p σ = ({(z, σ)}, False) := by + unfold getSimpleKo + rw [nd_bind_oneSc (getObject_eqSc hk)] + rw [nd_bind_oneSc (show Fl4Spec.NonDet.assert (σ := State Unit) (isSimpleType (.notification z)) σ = + ({((), σ)}, False) by simp [Fl4Spec.NonDet.assert, isSimpleType, aType])] + simp only [partialInv_ntfnSc]; rfl + +theorem getNtfnSc_eqSc {σ : State Unit} {p : ObjRef} {z : Notification} + (hk : σ.kheap p = some (.notification z)) : + getNtfnObjRef (Z := Unit) Notification.ntfnSc p σ = ({(z.ntfnSc, σ)}, False) := by + show getSkObjRef KernelObject.notification Notification.ntfnSc p σ = _ + unfold getSkObjRef + rw [nd_bind_oneSc (getSimpleKo_ntfnSc hk)]; rfl + +theorem getScTcb_eqSc {σ : State Unit} {p : ObjRef} {y : SchedContext} {n : Nat} + (hk : σ.kheap p = some (.schedContext y n)) : + getScObjRef (Z := Unit) SchedContext.scTcb p σ = ({(y.scTcb, σ)}, False) := by + unfold getScObjRef + rw [nd_bind_oneSc (getSchedContext_eqA hk)]; rfl + +end Spec + +/-! ## FL4 -/ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +theorem ntfn_sc_specSc (s : S) (n : obj.Ntfn) : obj.Ntfn.sc st n s ⦃ v => v = L.mem s (nScSc n) ⦄ := by + unfold obj.Ntfn.sc; step*; simp_all [nScSc] + +theorem scTcbA_eqSc (c : U64) : scTcbA c = scWA c 24#u64 := by unfold scTcbA scWA layout.sc.TCB; rfl + +/-- **The notification's `SC` word is its spec `ntfn_sc`** (renamed; a projection of the IPC line's +`NtfnStateRel`). -/ +def NtfnScEqA (m : Fl4SpecA64.McState) (n : U64) (mem : U64 → U64) (σ : Fl4SpecA64.State Unit) : Prop := + ∃ z, σ.kheap (specPtr n) = some (.notification z) ∧ z.ntfnSc = optScA m (mem (nScSc n)) + +variable {A : Type} {ar : machine.Arch A} + +/-- **No donation** (x86 `maybe_donate_nodonate`): the thread has an SC, or the notification has none, or the +notification's SC is bound to a TCB. -/ +theorem maybe_donate_nodonateA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} {σ : Fl4SpecA64.State Unit} + (H : SchedSRA st Z m κ s σ) (a : A) (t n : U64) (ht : t ∈ Z.T) (hN : NtfnScEqA m n (L.mem s) σ) + (hno : L.mem s (scA t) ≠ 0#u64 ∨ L.mem s (nScSc n) = 0#u64 ∨ + (L.mem s (nScSc n) ∈ Z.C ∧ L.mem s (scTcbA (L.mem s (nScSc n))) ≠ 0#u64)) : + notification.maybe_donate_sched_context st ar s a t n ⦃ r => + r = (s, a) ∧ Fl4SpecA64.maybeDonateSc (Z := Unit) (specPtr t) (specPtr n) σ = ({((), σ)}, False) ⦄ := by + obtain ⟨x, hkt, hsct⟩ := H.sch.tcb t ht + obtain ⟨z, hkn, hzs⟩ := hN + have pre : + Fl4SpecA64.maybeDonateSc (Z := Unit) (specPtr t) (specPtr n) σ = + (if L.mem s (scA t) = 0#u64 then Fl4Spec.NonDet.bind (Fl4SpecA64.getNtfnObjRef (Z := Unit) Fl4SpecA64.Notification.ntfnSc (specPtr n)) + (Fl4Spec.NonDet.maybeM fun scPtr => do + let scTcb ← Fl4SpecA64.getScObjRef Fl4SpecA64.SchedContext.scTcb scPtr + Fl4Spec.NonDet.when (scTcb = none) <| do + Fl4SpecA64.schedContextDonate scPtr (specPtr t) + Fl4SpecA64.schedContextResume scPtr) + else Fl4Spec.NonDet.pure ()) σ := by + unfold Fl4SpecA64.maybeDonateSc Fl4SpecA64.getTcbObjRef + rw [nd_bind_oneSc (threadGet_eqSc _ hkt), hsct] + simp only [Fl4Spec.NonDet.when] + have e : (optScA m (L.mem s (scA t)) = none) ↔ L.mem s (scA t) = 0#u64 := by + unfold optScA; split_ifs with h <;> simp [h] + by_cases hp : L.mem s (scA t) = 0#u64 + · rw [if_pos (e.2 hp), if_pos hp] + · rw [if_neg (fun h => hp (e.1 h)), if_neg hp] + unfold notification.maybe_donate_sched_context + refine WP.spec_bind (obj.Tcb.sc.spec s t) ?_ + rintro i rfl + by_cases h0 : L.mem s (scA t) = 0#u64 + · rw [if_pos h0] + refine WP.spec_bind (ntfn_sc_specSc s n) ?_ + rintro i1 rfl + refine WP.spec_bind (obj.Sc.is_null.spec _) ?_ + rintro b rfl + have hsp := pre + rw [if_pos h0] at hsp + by_cases h2 : L.mem s (nScSc n) = 0#u64 + · rw [if_pos (decide_eq_true h2), WP.spec_ok] + refine ⟨rfl, hsp.trans ?_⟩ + refine (nd_bind_oneSc (getNtfnSc_eqSc hkn) _).trans ?_ + rw [hzs, h2, show optScA m 0#u64 = none by simp [optScA]] + rfl + · rw [if_neg (by simpa using h2)] + refine WP.spec_bind (obj.Sc.tcb.spec s _) ?_ + rintro i2 rfl + obtain ⟨hC, h3⟩ : L.mem s (nScSc n) ∈ Z.C ∧ L.mem s (scTcbA (L.mem s (nScSc n))) ≠ 0#u64 := by + rcases hno with h | h | h + · exact absurd h0 h + · exact absurd h h2 + · exact h + rw [if_neg h3, WP.spec_ok] + refine ⟨rfl, hsp.trans ?_⟩ + obtain ⟨y, k, hky, hr⟩ := H.scr.sc _ hC + refine (nd_bind_oneSc (getNtfnSc_eqSc hkn) _).trans ?_ + rw [hzs, show optScA m (L.mem s (nScSc n)) = some (scNameA m (L.mem s (nScSc n))) by + unfold optScA; rw [if_neg h2]; rfl] + show (do + let scTcb ← Fl4SpecA64.getScObjRef Fl4SpecA64.SchedContext.scTcb (scNameA m (L.mem s (nScSc n))) + Fl4Spec.NonDet.when (scTcb = none) <| do + Fl4SpecA64.schedContextDonate (scNameA m (L.mem s (nScSc n))) (specPtr t) + Fl4SpecA64.schedContextResume (scNameA m (L.mem s (nScSc n)))) σ = _ + rw [nd_bind_oneSc (getScTcb_eqSc hky), hr.tcb, ← scTcbA_eqSc, optRefS_ne h3] + simp only [Fl4Spec.NonDet.when] + rw [if_neg (by simp)] + rfl + · rw [if_neg h0, WP.spec_ok] + refine ⟨rfl, ?_⟩ + rw [pre, if_neg h0] + rfl + +/-- **The wake-up's switch step** for a target on the current core: `is_schedulable` then `possible_switch_to` +refines `gets (schedulable t)` then `when … possible_switch_to t` (single spec outcome, heap kept; gen 10: FL4 writes +in `SWrA`). -/ +theorem switch_if_refinesA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} {σ : Fl4SpecA64.State Unit} + (H : SchedSRA st Z m κ s σ) (t : U64) (ht : t ∈ Z.T) (hon : L.mem s (affA t) = L.cpu s) + (hst : StEqA Z.T (L.mem s) σ) : + (do + let b ← sched.is_schedulable st s t + if b then sched.possible_switch_to st s t else ok s) ⦃ s' => ∃ σ' Q', + (do + let b ← Fl4Spec.NonDet.gets (Fl4SpecA64.schedulable (Z := Unit) (specPtr t)) + Fl4Spec.NonDet.when (b = true) (Fl4SpecA64.possibleSwitchTo (Z := Unit) (specPtr t))) σ = + ({((), σ')}, False) ∧ σ'.kheap = σ.kheap ∧ WritesIn st s s' (SWrA (st := st) Z s) ∧ + SchedSRA st { Z with Q := Q' } m κ s' σ' ⦄ := by + rw [is_schedulable_eqA, bind_ok, schedulable_eqA H.toSchedSRN ht (H.relOn ht hon) hst] + by_cases hs : Fl4SpecA64.schedulable (specPtr t) σ = true + · rw [if_pos hs] + refine WP.spec_mono (possible_switch_to_refinesA H t ht hon (schedulable_partsA hs).1 hst) ?_ + rintro s' ⟨σ', Q', h1, h2, hw, H'⟩ + refine ⟨σ', Q', ?_, h2, hw, H'⟩ + rw [nd_gets_bindSc] + simp only [Fl4Spec.NonDet.when] + rw [if_pos hs]; exact h1 + · rw [if_neg hs, WP.spec_ok] + refine ⟨σ, Z.Q, ?_, rfl, WritesIn.refl _ _, H⟩ + rw [nd_gets_bindSc] + simp only [Fl4Spec.NonDet.when] + rw [if_neg hs]; rfl + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScDonBA.lean b/proofs/Fl4ProofsA64/P3/ScDonBA.lean new file mode 100644 index 000000000..c01e32484 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScDonBA.lean @@ -0,0 +1,441 @@ +/- +P3 (aarch64, A64-P3-MCS gen 7): **the donation step `SchedSRA → SchedSRB`** (ruling 00:22Z gen-7 item 1; x86 +`P3/ScReturn`'s `ScRelW.wr_hdr` / `SchedRel.set_sc_tcb` / `SchedRel.set_tcb_sc` and `P3/ScDonateRef`'s +`donate_refines` relation part, ported). + +* `HdrKA`, **`ScRelWA.wr_hdr`** (x86 `ScRelW.wr_hdr`, with x86 `ScRel1.wr_hdr` inline): a write to a non-ring + header word of a related SC keeps its ring, refill max, period and the other header words. +* **`ScRelWA.set_tcb`** / **`ScStateRelA.set_sc_tcb`** (x86 `SchedRel.set_sc_tcb`, SC part): writing the SC's `TCB` + word is the spec's `sc_tcb := optRef v`; the SC relation and its placement hold after. +* `SchedSRN.donW`: what the two donation writes (the SC's `TCB` word, the TCB's `SC` word) keep: every node word, + the kernel-state block, every other TCB word, every other SC word. +* `donStA_kheap`: the spec heap after `sched_context_donate` (`donStA`, P3/ScDonRefA). +* **`SchedSRA.donateB`** (x86 `donate_refines`'s relation, `SchedRel.set_sc_tcb` + `set_tcb_sc` + `ScInv` kept): + from `SchedSRA`, an SC `sc ∈ Z.C` bound to no TCB and a TCB `t ∈ Z.T` bound to no SC, the two writes + `sc.TCB := t`, `t.SC := sc` relate to `donStA` by **`SchedSRB … (specPtr t)`** (`rd` everywhere but at `t`: the + donated SC may be unreleased until `sched_context_resume`, ruling 23:44Z item 1). `ScInvA` is kept: `t` is not + released (it had no SC, `relSc`), the SC was bound to nobody (`back`), so no release-queue member and no other + binding reads the changed objects. + +No named premise. Hypotheses: `hsc` / `ht` (related objects), `hsc0` (the SC word is not null: the case selector of +`maybe_donate_sched_context`), `htw0` / `hts0` (bound to nobody: case selectors), `hks` / `hkt` (the spec objects). +-/ +import Fl4ProofsA64.P3.ScResumeA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## Header words of an SC -/ + +section +variable {S : Type _} {st : machine.Store S} [L : LawfulStore st] + +/-- A non-ring header word (x86 `HdrK`). -/ +def HdrKA (K : U64) : Prop := + K = 8#u64 ∨ K = 24#u64 ∨ K = 32#u64 ∨ K = 40#u64 ∨ K = 48#u64 ∨ K = 56#u64 ∨ K = 64#u64 + +theorem HdrKA.vals {K : U64} (hK : HdrKA K) : K.val < 72 ∧ 8 ≤ K.val ∧ K.val % 8 = 0 := by + rcases hK with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> simp + +/-- **A write to a non-ring header word** (x86 `ScRelW.wr_hdr`, with x86 `ScRel1.wr_hdr` inline for the ring). -/ +theorem ScRelWA.wr_hdr {ii : Prop} {s : S} {sc : U64} {x : Fl4SpecA64.SchedContext} {n : Nat} + (h : ScRelWA st ii s sc x n) {K : U64} (hK : HdrKA K) (v : U64) : + let s' := L.wr s (scWA sc K) v + ((L.rmax s sc).val ≠ 0 → RefillsWF st s' sc (n + Fl4SpecA64.minSchedContextBits) ∧ + L.refills s' sc = L.refills s sc) ∧ + L.rperiod s' sc = L.rperiod s sc ∧ L.rmax s' sc = L.rmax s sc ∧ + (∀ K' : U64, K'.val < 96 → K'.val % 8 = 0 → K' ≠ K → L.mem s' (scWA sc K') = L.mem s (scWA sc K')) ∧ + L.mem s' (scWA sc K) = v ∧ WritesIn st s s' (ScObjA sc n) := by + intro s' + obtain ⟨h8, h2, hfit⟩ := h.bounds + obtain ⟨hK1, hK2, hK3⟩ := hK.vals + have ha := h.hdr (K := K) (by omega) hK3 + have hv := scWA_val (sc := sc) (K := K) (by omega) + have hne : ∀ b : U64, Aligned b → b.val ≠ (scWA sc K).val → L.mem s' b = L.mem s b := + fun b hb hb' => L.mem_wr_ne ha.1 hb (Ne.symm hb') + have hw : ∀ K' : U64, K'.val < 96 → K'.val % 8 = 0 → K' ≠ K → L.mem s' (scWA sc K') = L.mem s (scWA sc K') := by + intro K' h1 h2' h3 + refine hne _ (h.hdr h1 h2').1 ?_ + rw [scWA_val (by omega), hv] + exact fun e => h3 (UScalar.eq_of_val_eq (by omega)) + have em : L.rmax s' sc = L.rmax s sc := by + simp only [LawfulStore.rmax, maxA_eqS]; exact hw _ (by simp) (by simp) (fun e => by subst e; simp at hK1) + have ep : L.rperiod s' sc = L.rperiod s sc := by + simp only [LawfulStore.rperiod, periodA_eqS]; exact hw _ (by simp) (by simp) (fun e => by subst e; simp at hK2) + refine ⟨fun h0 => ?_, ep, em, hw, L.mem_wr_same ha.1, + (writesIn_wr ha.1).mono fun b e => by subst e; exact h.in_obj ha.2⟩ + have h1 := h.act h0 + have eh : L.rhead s' sc = L.rhead s sc := by + simp only [LawfulStore.rhead, headA_eqS]; exact hw _ (by simp) (by simp) (fun e => by subst e; simp at hK1) + have et : L.rtail s' sc = L.rtail s sc := by + simp only [LawfulStore.rtail, tailA_eqS]; exact hw _ (by simp) (by simp) (fun e => by subst e; simp at hK1) + have hW' : RefillsWF st s' sc (n + Fl4SpecA64.minSchedContextBits) := + h1.wf.of_hdr em (by rw [eh]; exact h1.wf.head_lt) (by rw [et]; exact h1.wf.tail_lt) + have hsz : L.size s' sc = L.size s sc := by simp only [LawfulStore.size, em, eh, et] + refine ⟨hW', ?_⟩ + simp only [LawfulStore.refills, hsz] + refine List.map_congr_left fun k _ => ?_ + have hix : L.ix s' sc k = L.ix s sc k := by simp only [LawfulStore.ix, em, eh] + obtain ⟨-, -, -, -, -, hin⟩ := h1.wf.inside + obtain ⟨g1, g2, -⟩ := hin _ (h1.wf.ix_lt k) + unfold LawfulStore.rf LawfulStore.refill + rw [hix, hne _ (by unfold Aligned; rw [g1]; omega) (by rw [g1, hv]; omega), + hne _ (by unfold Aligned; rw [g2]; omega) (by rw [g2, hv]; omega)] + +/-- **Writing an SC's `TCB` word** (x86 `SchedRel.set_sc_tcb`, the SC's relation). -/ +theorem ScRelWA.set_tcb {ii : Prop} {s : S} {sc : U64} {x : Fl4SpecA64.SchedContext} {n : Nat} + (h : ScRelWA st ii s sc x n) (v : U64) : + ScRelWA st ii (L.wr s (scWA sc 24#u64) v) sc { x with scTcb := optRefS v } n ∧ + WritesIn st s (L.wr s (scWA sc 24#u64) v) (ScObjA sc n) := by + obtain ⟨hst, ep, em, hw, hv, hwr⟩ := h.wr_hdr (K := 24#u64) (Or.inr (Or.inl rfl)) v + have k : ∀ K : U64, K.val < 96 → K.val % 8 = 0 → K.val ≠ 24 → + L.mem (L.wr s (scWA sc 24#u64) v) (scWA sc K) = L.mem s (scWA sc K) := + fun K h1 h2 h3 => hw K h1 h2 (fun e => h3 (by subst e; simp)) + refine ⟨⟨h.pl, by rw [ep]; exact h.period, by rw [k 8#u64 (by simp) (by simp) (by simp)]; exact h.consumed, + by rw [hv], by rw [k 32#u64 (by simp) (by simp) (by simp)]; exact h.reply, + by rw [k 40#u64 (by simp) (by simp) (by simp)]; exact h.ntfn, + by rw [k 48#u64 (by simp) (by simp) (by simp)]; exact h.badge, + by rw [k 56#u64 (by simp) (by simp) (by simp)]; exact h.yieldFrom, + by rw [k 64#u64 (by simp) (by simp) (by simp)]; exact h.sporadic, + fun h0 => by rw [em]; exact h.spor (by rwa [k 64#u64 (by simp) (by simp) (by simp)] at h0), + fun h0 => by rw [em]; exact h.idle h0, by rw [em]; exact h.max, fun h0 => ?_⟩, hwr⟩ + have h0' : (L.rmax s sc).val ≠ 0 := by rwa [em] at h0 + have h1 := h.act h0' + exact ⟨(hst h0').1, by rw [ep]; exact h1.period, by rw [k 8#u64 (by simp) (by simp) (by simp)]; exact h1.consumed, + by rw [hv], by rw [k 32#u64 (by simp) (by simp) (by simp)]; exact h1.reply, + by rw [k 40#u64 (by simp) (by simp) (by simp)]; exact h1.ntfn, + by rw [k 48#u64 (by simp) (by simp) (by simp)]; exact h1.badge, + by rw [k 56#u64 (by simp) (by simp) (by simp)]; exact h1.yieldFrom, + by rw [k 64#u64 (by simp) (by simp) (by simp)]; exact h1.sporadic, by rw [em]; exact h1.max, + by rw [(hst h0').2]; exact h1.refills⟩ + +/-- **Writing an SC's `TCB` word keeps the SC relation and its placement** (x86 `SchedRel.set_sc_tcb`, SC part). -/ +theorem ScStateRelA.set_sc_tcb {th : TypedHeapA} {ksb : U64} {C : Set U64} {m : Fl4SpecA64.McState} {s : S} + {σ : Fl4SpecA64.State Unit} (h : ScStateRelA st C m s σ) (P : ScPlacedA th ksb C m σ) {sc : U64} (hc : sc ∈ C) + {x : Fl4SpecA64.SchedContext} {n : Nat} (hk : σ.kheap (scNameA m sc) = some (.schedContext x n)) (v : U64) : + ScStateRelA st C m (L.wr s (scTcbA sc) v) + (updKSc σ (scNameA m sc) (.schedContext { x with scTcb := optRefS v } n)) ∧ + ScPlacedA th ksb C m (updKSc σ (scNameA m sc) (.schedContext { x with scTcb := optRefS v } n)) := by + obtain ⟨x', n', hk', hx⟩ := h.sc sc hc + rw [hk] at hk'; cases hk' + rw [scTcbA_eqSc] + obtain ⟨h1, hw⟩ := hx.set_tcb v + exact h.upd P hc hk hw h1 + +end + +/-! ## The spec heap after the donation -/ + +section Spec +open Fl4SpecA64 + +theorem donStA_kheap {σ : State Unit} {N T : ObjRef} {xs : SchedContext} {ns : Nat} {xt : Tcb} (hNT : N ≠ T) + (p : ObjRef) : + (donStA σ N T xs ns xt).kheap p = if p = T then some (.tcb { xt with tcbSchedContext := some N }) + else if p = N then some (.schedContext { xs with scTcb := some T } ns) else σ.kheap p := by + by_cases h1 : p = T + · subst h1; simp [donStA, updKSc] + · by_cases h2 : p = N + · subst h2; simp [donStA, updKSc, h1] + · simp [donStA, updKSc, h1, h2] + +end Spec + +/-! ## The two donation writes -/ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **What the donation's two writes keep** (the SC's `TCB` word, the TCB's `SC` word): every node word, the +kernel-state block, every other TCB word, every SC word but the `TCB` word written. -/ +theorem SchedSRN.donW {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s s' : S} {σ : Fl4SpecA64.State Unit} + (H : SchedSRN st Z m κ s σ) {sc t : U64} (hsc : sc ∈ Z.C) (ht : t ∈ Z.T) + (hw : WritesIn st s s' (fun b => b = scTcbA sc ∨ b = scA t)) : + (∀ c ∈ Z.Cs, ∀ o : U64, o.val < 4288 → o.val % 8 = 0 → L.mem s' (L.nodeAt s c o) = L.mem s (L.nodeAt s c o)) ∧ + (∀ K : U64, K.val < 223488 → K.val % 8 = 0 → L.mem s' (wa (L.ksb s) K) = L.mem s (wa (L.ksb s) K)) ∧ + (∀ y ∈ Z.T, ∀ K : U64, K.val + 8 ≤ 4096 → K.val % 8 = 0 → (y = t → K.val ≠ 3464) → + L.mem s' (wa y K) = L.mem s (wa y K)) ∧ + (∀ a, Aligned a → ScFootA Z.C m σ a → a.val ≠ (scTcbA sc).val → L.mem s' a = L.mem s a) := by + obtain ⟨nn, -, hn, hcs⟩ := H.csN + have hcl : ∀ c ∈ Z.Cs, c.val < 128 := fun c hc => by have := (hcs c).1 hc; omega + have hf := H.ksFit + have hkb := H.ksb_al + obtain ⟨x, n, hk, hx⟩ := H.scr.sc sc hsc + have h24 := hx.hdr (K := 24#u64) (by simp) (by simp) + have hF : ScFootA Z.C m σ (scTcbA sc) := by rw [scTcbA_eqSc]; exact ⟨sc, hsc, x, n, hk, hx.in_obj h24.2⟩ + have hFk := H.scp.ks_out hF + have hFt : ∀ y ∈ Z.T, (scTcbA sc).val < y.val ∨ y.val + 4096 ≤ (scTcbA sc).val := fun y hy => by + have := H.scp.th_out hF ((H.thT y).2 hy); simpa [ObjKindA.size] using this + have hT := H.core.tcbs t ht + have hTf := hT.fits + have hTa := hT.align + have hv2 : (scA t).val = t.val + 3464 := by + show (wa t 3464#u64).val = _ + rw [wa_valm]; have : (3464#u64 : U64).val = 3464 := by simp + omega + have htk := H.tcb_ks ht + have keep : ∀ b, Aligned b → b.val ≠ (scTcbA sc).val → b.val ≠ (scA t).val → L.mem s' b = L.mem s b := + fun b hb h1 h2 => hw.mem_eq hb (fun hb' => hb'.elim (fun e => h1 (by rw [e])) (fun e => h2 (by rw [e]))) + unfold ksSizeA at hf hFk htk + refine ⟨fun c hc o ho h8 => ?_, fun K hK h8 => ?_, fun y hy K hK h8 hyt => ?_, fun a ha hfa hne => ?_⟩ + · have hv := nodeAt_valSc (s := s) H.ksFit (hcl c hc) ho + have hc' := hcl c hc + exact keep _ (by unfold Aligned; rw [hv]; omega) (by rw [hv]; omega) (by rw [hv, hv2]; omega) + · have hv : (wa (L.ksb s) K).val = (L.ksb s).val + K.val := by rw [wa_valm]; omega + exact keep _ (by unfold Aligned; rw [hv]; omega) (by rw [hv]; omega) (by rw [hv, hv2]; omega) + · have hY := H.core.tcbs y hy + have hYf := hY.fits + have hYa := hY.align + have hv : (wa y K).val = y.val + K.val := by rw [wa_valm]; omega + have hyF := hFt y hy + refine keep _ (by unfold Aligned; rw [hv]; omega) (by rw [hv]; omega) (by + rw [hv, hv2] + by_cases e : y = t + · have := hyt e; subst e; omega + · have : y.val ≠ t.val := fun e' => e (UScalar.eq_of_val_eq e') + omega) + · have := H.scp.th_out hfa ((H.thT t).2 ht) + simp only [ObjKindA.size] at this + exact keep a ha hne (by rw [hv2]; omega) + +/-- (gen 11) **`SchedSRN` after a step writing at most an SC's `TCB` word and a TCB's `SC` word** (the shared tail of +`donateG`, `unbindA` and `unbindTA`): the spec step keeps the scheduler scalars, the action and the queues, the TCBs +keep state, domain and priority; the SC relation and placement, the TCBs' SC words and `ScInvA` after are given. -/ +theorem SchedSRA.rebuild2 {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s s' : S} + {σ σ2 : Fl4SpecA64.State Unit} (H : SchedSRA st Z m κ s σ) {sc t : U64} (hsc : sc ∈ Z.C) (ht : t ∈ Z.T) + (hw : WritesIn st s s' (fun b => b = scTcbA sc ∨ b = scA t)) (hfix : SFixA σ σ2) + (hact : σ2.schedulerAction = σ.schedulerAction) (hrq : σ2.readyQueues = σ.readyQueues) + (hrel : σ2.releaseQueue = σ.releaseQueue) + (hS2 : ScStateRelA st Z.C m s' σ2) (hP2 : ScPlacedA Z.th (L.ksb s) Z.C m σ2) + (hTk : ∀ u x, σ.kheap u = some (.tcb x) → ∃ x', σ2.kheap u = some (.tcb x') ∧ + x'.tcbState = x.tcbState ∧ x'.tcbDomain = x.tcbDomain ∧ x'.tcbPriority = x.tcbPriority) + (htcbSc : ∀ u ∈ Z.T, ∃ x, σ2.kheap (specPtr u) = some (.tcb x) ∧ + x.tcbSchedContext = optScA m (L.mem s' (scA u))) + (hscMem : ∀ u ∈ Z.T, L.mem s' (scA u) ≠ 0#u64 → L.mem s' (scA u) ∈ Z.C) (hinv : ScInvA m σ2) : + SchedSRN st Z m κ s' σ2 := by + obtain ⟨nK, gK, tK, sK⟩ := H.donW hsc ht hw + obtain ⟨xs, ns, hks, hx⟩ := H.scr.sc sc hsc + have h24 := hx.hdr (K := 24#u64) (by simp) (by simp) + have hcc : ∀ c ∈ Z.C, (scCoreA c).val ≠ (scTcbA sc).val := by + intro c hc e + have e' : scCoreA c = scTcbA sc := UScalar.eq_of_val_eq e + obtain ⟨y, k, hy, hyr⟩ := H.scr.sc c hc + have h16 := hyr.hdr (K := 16#u64) (by simp) (by simp) + have hcE : scCoreA c = scWA c 16#u64 := by unfold scCoreA scWA layout.sc.CORE; rfl + by_cases ec : c = sc + · rw [ec] at e' + obtain ⟨-, -, hfit⟩ := hx.bounds + have v1 := scWA_val (sc := sc) (K := 16#u64) (by simp; omega) + have v2 := scWA_val (sc := sc) (K := 24#u64) (by simp; omega) + have := congrArg UScalar.val e' + rw [show scCoreA sc = scWA sc 16#u64 by unfold scCoreA scWA layout.sc.CORE; rfl, scTcbA_eqSc, v1, v2] at this + simp at this + · refine H.scp.disj c hc sc hsc ec y k xs ns hy hks (scCoreA c) (by rw [hcE]; exact hyr.in_obj h16.2) ?_ + rw [e', scTcbA_eqSc]; exact hx.in_obj h24.2 + exact H.toSchedSRN.rebuildG (Q' := Z.Q) (R' := Z.R) (s' := s') (σ' := σ2) hw.1 hw.2.1 + (fun c hc o h1 h2 _ _ => nK c hc o (by omega) h2) + (fun K hK => gK K (by rcases hK with rfl | rfl <;> simp) (by rcases hK with rfl | rfl <;> simp)) + (fun y hy K hK => tK y hy K (by rcases hK with rfl | rfl | rfl <;> simp) + (by rcases hK with rfl | rfl | rfl <;> simp) (fun _ => by rcases hK with rfl | rfl | rfl <;> simp)) + hS2 hP2 + (fun u hu x hx => by obtain ⟨x', h1, -, h2, h3⟩ := hTk _ x hx; exact ⟨x', h1, h2, h3⟩) + htcbSc hscMem + ⟨hfix.curThread, hfix.curSc, hfix.idleThread, hfix.curTime, hfix.consumedTime, rfl, hfix.curDomain, + hfix.domainTime⟩ + (by + rw [hfix.reprogramTimer, nK _ H.sch.cpuCs layout.node.REPROGRAM (by simp [layout.node.REPROGRAM]) + (by simp [layout.node.REPROGRAM])] + exact H.rest.repr) + (by + rw [hact, nK _ H.sch.cpuCs layout.node.SCHED_ACTION (by simp [layout.node.SCHED_ACTION]) + (by simp [layout.node.SCHED_ACTION])] + exact H.sch.act) + (fun u h => H.actOn u (by rw [← hact]; exact h)) hinv + (H.core.agree hw.1 + (fun u hu => ⟨tK u hu 3480#u64 (by simp) (by simp) (fun _ => by simp), + tK u hu 3488#u64 (by simp) (by simp) (fun _ => by simp)⟩) + (fun c hc o h1 h2 _ => nK c hc o h1 h2) (fun u hu => tK u hu 3592#u64 (by simp) (by simp) (fun _ => by simp)) + (fun u hu => tK u hu 3520#u64 (by simp) (by simp) (fun _ => by simp)) + (fun u hu => tK u hu 3448#u64 (by simp) (by simp) (fun _ => by simp))) + (by rw [hrq]; exact H.rest.rq) (by rw [hrq]; exact H.rest.rqDom) (by rw [hrel]; exact H.rest.rel) + ⟨fun d p u hu => by + obtain ⟨x, h1, h2⟩ := H.run.1 d p u (by rw [← hrq]; exact hu) + obtain ⟨x', h1', e1, -, -⟩ := hTk u x h1 + exact ⟨x', h1', by rw [e1]; exact h2⟩, + fun u hu => by + obtain ⟨x, h1, h2⟩ := H.run.2 u (by rw [← hrel]; exact hu) + obtain ⟨x', h1', e1, -, -⟩ := hTk u x h1 + exact ⟨x', h1', by rw [e1]; exact h2⟩⟩ + (fun c hc => sK _ (H.scCoreFoot hc).1 (H.scCoreFoot hc).2 (hcc c hc)) + +/-- **The donation step, general form** (gen 11): `donateB` with "no TCB is bound to the SC" (`hfree`) for "the +SC's `TCB` word is null" (the SC of `sched_context_donate`'s `from` branch still names `from` after `from`'s +unbinding), and (ruling 02:05Z) the donated SC not idle (`hni`, for `ScInvA.tcbScNotIdle`). -/ +theorem SchedSRA.donateG {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} {σ : Fl4SpecA64.State Unit} + (H : SchedSRA st Z m κ s σ) {sc t : U64} (hsc : sc ∈ Z.C) (ht : t ∈ Z.T) (hsc0 : sc ≠ 0#u64) + (hfree : ∀ u x, σ.kheap u = some (.tcb x) → x.tcbSchedContext ≠ some (scNameA m sc)) + (hts0 : L.mem s (scA t) = 0#u64) (hni : NotIdleScA m (scNameA m sc)) + {xs : Fl4SpecA64.SchedContext} {ns : Nat} {xt : Fl4SpecA64.Tcb} + (hks : σ.kheap (scNameA m sc) = some (.schedContext xs ns)) (hkt : σ.kheap (specPtr t) = some (.tcb xt)) : + SchedSRB st Z m κ (L.wr (L.wr s (scTcbA sc) t) (scA t) sc) + (donStA σ (scNameA m sc) (specPtr t) xs ns xt) (specPtr t) := by + have ht0 : t ≠ 0#u64 := fun e => (H.core.tcbs t ht).ne0 (by rw [e]; rfl) + obtain ⟨x', n', hk', hx⟩ := H.scr.sc sc hsc + rw [hks] at hk'; cases hk' + have hxt0 : xt.tcbSchedContext = none := by + obtain ⟨y, hy, hys⟩ := H.sch.tcb t ht + rw [hkt] at hy; cases hy; rw [hys, hts0]; simp [optScA] + have hNT : scNameA m sc ≠ specPtr t := fun e => by rw [e, hkt] at hks; cases hks + set σ2 := donStA σ (scNameA m sc) (specPtr t) xs ns xt with hσ2 + have hk2 : ∀ p, p ≠ specPtr t → p ≠ scNameA m sc → σ2.kheap p = σ.kheap p := fun p h1 h2 => by + rw [hσ2, donStA_kheap hNT, if_neg h1, if_neg h2] + have hk2T : σ2.kheap (specPtr t) = some (.tcb { xt with tcbSchedContext := some (scNameA m sc) }) := by + rw [hσ2, donStA_kheap hNT, if_pos rfl] + have hk2N : σ2.kheap (scNameA m sc) = some (.schedContext { xs with scTcb := some (specPtr t) } ns) := by + rw [hσ2, donStA_kheap hNT, if_neg hNT, if_pos rfl] + -- the spec facts + have hTq : specPtr t ∉ σ.releaseQueue := fun hm => by + obtain ⟨x, p, y, n, h1, h2, -, -⟩ := H.inv.relSc _ hm + rw [hkt] at h1; cases h1; rw [hxt0] at h2; cases h2 + have hpN : ∀ u x p, σ.kheap u = some (.tcb x) → x.tcbSchedContext = some p → + p ≠ specPtr t ∧ p ≠ scNameA m sc := by + intro u x p hu hp + obtain ⟨y, n, hy, hyu⟩ := H.inv.back u x p hu hp + refine ⟨fun e => ?_, fun e => ?_⟩ + · rw [e, hkt] at hy; cases hy + · exact hfree u x hu (by rw [← e]; exact hp) + have hTk : ∀ u x, σ.kheap u = some (.tcb x) → ∃ x', σ2.kheap u = some (.tcb x') ∧ + x'.tcbState = x.tcbState ∧ x'.tcbDomain = x.tcbDomain ∧ x'.tcbPriority = x.tcbPriority := by + intro u x hu + by_cases e1 : u = specPtr t + · rw [e1, hkt] at hu; cases hu; rw [e1]; exact ⟨_, hk2T, rfl, rfl, rfl⟩ + · have e2 : u ≠ scNameA m sc := fun e => by rw [e, hks] at hu; cases hu + exact ⟨x, by rw [hk2 u e1 e2]; exact hu, rfl, rfl, rfl⟩ + have hSC : ∀ p y n, σ2.kheap p = some (.schedContext y n) → + σ.kheap p = some (.schedContext y n) ∨ + (p = scNameA m sc ∧ y = { xs with scTcb := some (specPtr t) } ∧ n = ns) := by + intro p y n hp + by_cases e1 : p = specPtr t + · rw [e1, hk2T] at hp; cases hp + · by_cases e2 : p = scNameA m sc + · rw [e2, hk2N] at hp; cases hp; exact Or.inr ⟨e2, rfl, rfl⟩ + · rw [hk2 p e1 e2] at hp; exact Or.inl hp + have hNf : ∀ q z, σ2.kheap q = some (.notification z) → σ.kheap q = some (.notification z) := by + intro q z hq + by_cases e1 : q = specPtr t + · rw [e1, hk2T] at hq; cases hq + · by_cases e2 : q = scNameA m sc + · rw [e2, hk2N] at hq; cases hq + · rwa [hk2 q e1 e2] at hq + have hinv : ScInvA m σ2 := by + have hR := H.inv.relKeep (σ' := σ2) rfl + (fun u hu x hx => ⟨x, by + rw [hk2 u (fun e => hTq (e ▸ hu)) (fun e => by rw [e, hks] at hx; cases hx)]; exact hx, rfl⟩) + (fun u _ x p hx hp => hk2 p (hpN u x p hx hp).1 (hpN u x p hx hp).2) + refine ⟨fun p y n hy ha hp => ?_, fun p y n hy ha hp => ?_, fun q z hz => H.inv.ntfnIdle q z (hNf q z hz), + fun q z hz => H.inv.ntfnIdleOf q z (hNf q z hz), hR.1, hR.2, fun u x p hx hp => ?_, + fun u x p hx hp hi => ?_⟩ + · rcases hSC p y n hy with h | ⟨rfl, rfl, rfl⟩ + · exact H.inv.refills p y n h ha hp + · exact H.inv.refills _ xs _ hks ha hp + · rcases hSC p y n hy with h | ⟨rfl, rfl, rfl⟩ + · exact H.inv.rr p y n h ha hp + · exact H.inv.rr _ xs _ hks ha hp + · by_cases e1 : u = specPtr t + · rw [e1, hk2T] at hx; cases hx; cases hp; rw [e1]; exact ⟨_, ns, hk2N, rfl⟩ + · have e2 : u ≠ scNameA m sc := fun e => by rw [e, hk2N] at hx; cases hx + rw [hk2 u e1 e2] at hx + obtain ⟨y, n, hy, hyu⟩ := H.inv.back u x p hx hp + exact ⟨y, n, by rw [hk2 p (hpN u x p hx hp).1 (hpN u x p hx hp).2]; exact hy, hyu⟩ + · by_cases e1 : u = specPtr t + · rw [e1, hk2T] at hx; cases hx; cases hp; exact hni + · have e2 : u ≠ scNameA m sc := fun e => by rw [e, hk2N] at hx; cases hx + rw [hk2 u e1 e2] at hx + exact H.inv.tcbScNotIdle u x p hx hp hi + -- the FL4 writes + have h24 := hx.hdr (K := 24#u64) (by simp) (by simp) + have hA1 : Aligned (scTcbA sc) := by rw [scTcbA_eqSc]; exact h24.1 + have hT := H.core.tcbs t ht + have hA2 : Aligned (scA t) := by + have hTf := hT.fits; have hTa := hT.align + unfold Aligned; show (wa t 3464#u64).val % 8 = 0 + rw [wa_valm]; have : (3464#u64 : U64).val = 3464 := by simp + omega + have hw : WritesIn st s (L.wr (L.wr s (scTcbA sc) t) (scA t) sc) (fun b => b = scTcbA sc ∨ b = scA t) := + (writesIn_wr hA1).trans (writesIn_wr hA2) + obtain ⟨nK, gK, tK, sK⟩ := H.donW hsc ht hw + obtain ⟨hS1, hP1⟩ := H.scr.set_sc_tcb H.scp hsc hks t + rw [optRefS_ne ht0] at hS1 hP1 + have hkc : ∀ c ∈ Z.C, σ2.kheap (scNameA m c) = + (updKSc σ (scNameA m sc) (.schedContext { xs with scTcb := some (specPtr t) } ns)).kheap (scNameA m c) := by + intro c hc + obtain ⟨y, k, hy, -⟩ := hS1.sc c hc + have hne : scNameA m c ≠ specPtr t := fun e => by + rw [e, updKSc_kheap, Function.update_of_ne (Ne.symm hNT), hkt] at hy; cases hy + rw [hy]; rw [hσ2, donStA_kheap hNT, if_neg hne] + by_cases e2 : scNameA m c = scNameA m sc + · rw [if_pos e2, ← hy, e2, updKSc_kheap, Function.update_self] + · rw [if_neg e2, ← hy, updKSc_kheap, Function.update_of_ne e2] + have hS2 : ScStateRelA st Z.C m (L.wr (L.wr s (scTcbA sc) t) (scA t) sc) σ2 := + hS1.frame (fun a ha hfa => L.mem_wr_ne hA2 ha (by + have := hP1.th_out hfa ((H.thT t).2 ht) + have hTf := hT.fits + simp only [ObjKindA.size] at this + show (wa t 3464#u64).val ≠ a.val + rw [wa_valm]; have : (3464#u64 : U64).val = 3464 := by simp + omega)) hkc + have hP2 : ScPlacedA Z.th (L.ksb s) Z.C m σ2 := hP1.of_kheap hkc + have N2 := H.rebuild2 (σ2 := σ2) hsc ht hw ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ rfl rfl rfl hS2 hP2 hTk + (fun u hu => by + by_cases e : u = t + · subst e + refine ⟨_, hk2T, ?_⟩ + rw [L.mem_wr_same hA2]; unfold optScA; rw [if_neg hsc0]; rfl + · have hne : specPtr u ≠ specPtr t := fun h => e (specPtr_inj h) + obtain ⟨x, h1, h3⟩ := H.sch.tcb u hu + have e2 : specPtr u ≠ scNameA m sc := fun h => by rw [h, hks] at h1; cases h1 + refine ⟨x, by rw [hk2 _ hne e2]; exact h1, ?_⟩ + rw [show scA u = wa u 3464#u64 from rfl, tK u hu 3464#u64 (by simp) (by simp) + (fun h => absurd h e)] + exact h3) + (fun u hu h => by + by_cases e : u = t + · subst e; rw [L.mem_wr_same hA2]; exact hsc + · rw [show scA u = wa u 3464#u64 from rfl, tK u hu 3464#u64 (by simp) (by simp) + (fun h => absurd h e)] at h ⊢ + exact H.rest.scMem u hu h) + hinv + refine ⟨N2, fun u x hu hx hc => ?_⟩ + have huN : u ≠ scNameA m sc := fun e => by rw [e, hk2N] at hx; cases hx + rw [hk2 u hu huN] at hx + intro x' scp y k h1 h2 h3 h4 h5 + rw [hk2 u hu huN] at h1 + obtain ⟨hp1, hp2⟩ := hpN u x' scp h1 h2 + rw [hk2 scp hp1 hp2] at h3 + exact H.rd u x hx hc x' scp y k h1 h2 h3 h4 h5 + + +/-- **The donation step** (module docstring; x86 `donate_refines`'s relation part; gen 11: via `donateG`, + `hni`). -/ +theorem SchedSRA.donateB {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} {σ : Fl4SpecA64.State Unit} + (H : SchedSRA st Z m κ s σ) {sc t : U64} (hsc : sc ∈ Z.C) (ht : t ∈ Z.T) (hsc0 : sc ≠ 0#u64) + (htw0 : L.mem s (scTcbA sc) = 0#u64) (hts0 : L.mem s (scA t) = 0#u64) (hni : NotIdleScA m (scNameA m sc)) + {xs : Fl4SpecA64.SchedContext} {ns : Nat} {xt : Fl4SpecA64.Tcb} + (hks : σ.kheap (scNameA m sc) = some (.schedContext xs ns)) (hkt : σ.kheap (specPtr t) = some (.tcb xt)) : + SchedSRB st Z m κ (L.wr (L.wr s (scTcbA sc) t) (scA t) sc) + (donStA σ (scNameA m sc) (specPtr t) xs ns xt) (specPtr t) := by + refine H.donateG hsc ht hsc0 (fun u x hu hp => ?_) hts0 hni hks hkt + obtain ⟨x', n', hk', hx⟩ := H.scr.sc sc hsc + rw [hks] at hk'; cases hk' + obtain ⟨y, n, hy, hyu⟩ := H.inv.back u x _ hu hp + rw [hks] at hy; cases hy + rw [hx.tcb, ← scTcbA_eqSc, htw0, optRefS_zero] at hyu; cases hyu +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScDonFromA.lean b/proofs/Fl4ProofsA64/P3/ScDonFromA.lean new file mode 100644 index 000000000..f6d294657 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScDonFromA.lean @@ -0,0 +1,439 @@ +/- +P3 (aarch64, A64-P3-MCS gen 11): **`sched_context_donate` of a bound SC** on `SchedSRA` (ruling 02:05Z gen-11 +item 3: the donation of `reply_push`; x86 `P3/XTopJointPushDon`'s `testReschedule_curN` / `donate_curN_eq` (spec) +and `P3/ScCallPushRef`'s `push_donate_refines` (its donation half), ported). + +* `unbTStA` / `unbTStA_kheap`: the spec state after `tcb_sched_context := none` at one TCB. +* `setRqA_selfA`, **`tcbReleaseRemove_notA`** (x86 `tcbReleaseRemove_not`), **`testReschedule_posA`** / + **`testReschedule_negA`** (x86 `testReschedule_curN`, here for any action): the spec pieces. +* **`donate_fromSpecA`** (x86 `donate_curN_eq`, any scheduler action): l4v `sched_context_donate p d` of an SC + bound to `f` (in no queue) is the dequeue / release-remove no-ops, `f`'s unbinding, `test_reschedule f` (whose + outcome `σR` is given) and the binding to `d` (`donStA σR`). +* **`SchedSRA.unbindTA`** (x86 `SchedRel.set_tcb_sc` with `v = 0`, the TCB half of `unbindA`): `f.SC := 0` of a + thread in no release queue keeps `SchedSRA` (same `Z`), the spec unbinding only the TCB (`unbTStA`; the SC still + names `f`); `SchedSRN.unbT_mfr`: its frame `MFrA`. +* **`sc_donate_fromA`** (x86 `push_donate_refines`' donation): FL4's `schedcontext::donate sc d` of an SC bound to + `f` (`f.SC = sc`) against l4v `sched_context_donate`: single spec outcome, `SchedSRB … (specPtr d)` after (`rd` + everywhere but at `d`, as `donateB`) and the frame `MFrA`. The `test_reschedule` of FL4 (`f` current, or the switch + target) is matched case by case (`reschedule_required_refinesA`). + +No named premise. Hypotheses: `hsc` / `hf` / `hd` (related objects), `hsc0`, `hfs` (`f` is bound to `sc`), `hds0` +(`d` has no SC: `reply_push`'s case selector), `hfd`; `hfon` (`f` on the current core), `hnqs` / `hnr` (`f` in no +queue: IPC's `NotQueued` of the blocked caller, `SchedOpsEA.notQueued`), `hst` (`StEqA`); `hstall` (case selector: +FL4's `remote_tcb_stall f` stalls nothing, A64 P1 `stall_none` for a local thread without a bound VCPU); `hmig` (`d` +on the SC's core: `migrate_tcb` is the no-op, as `donate_localA`); `hni` (the SC not idle, for +`ScInvA.tcbScNotIdle`). +-/ +import Fl4ProofsA64.P3.ScRetA +import Fl4ProofsA64.P3.ScIdleA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section Spec +open Fl4SpecA64 + +/-- The spec state after `tcb_sched_context := none` at `T` alone (the SC keeps its `sc_tcb`). -/ +def unbTStA (σ : State Unit) (T : ObjRef) (xt : Tcb) : State Unit := + updKSc σ T (.tcb { xt with tcbSchedContext := none }) + +theorem unbTStA_kheap (σ : State Unit) (T : ObjRef) (xt : Tcb) (p : ObjRef) : + (unbTStA σ T xt).kheap p = if p = T then some (.tcb { xt with tcbSchedContext := none }) else σ.kheap p := by + unfold unbTStA; rw [updKSc_kheap, Function.update_apply] + +theorem setRqA_selfA (σ : State Unit) (p : Priority) : setRqA σ p (σ.readyQueues 0 p) = σ := by + have h : (fun d' p' => if d' = 0 ∧ p' = p then σ.readyQueues 0 p else σ.readyQueues d' p') = σ.readyQueues := by + funext d' p' + by_cases e : d' = 0 ∧ p' = p + · rw [if_pos e, e.1, e.2] + · rw [if_neg e] + unfold setRqA; rw [h] + +/-- `tcb_release_remove` of a thread in no release queue (x86 `tcbReleaseRemove_not`). -/ +theorem tcbReleaseRemove_notA {σ : State Unit} {t : ObjRef} (ht : t ∉ σ.releaseQueue) : + tcbReleaseRemove (Z := Unit) t σ = ({((), σ)}, False) := by + unfold tcbReleaseRemove + rw [nd_gets_bindSc] + unfold Fl4Spec.NonDet.when + rw [if_neg (fun h => ht (by + obtain ⟨h1, h2⟩ := h + obtain ⟨a, l, e⟩ := List.exists_cons_of_ne_nil h1 + rw [e] at h2 ⊢; simp only [List.head?_cons, the_someA] at h2; rw [h2]; exact List.mem_cons_self))] + rw [nd_bind_oneSc (show Fl4Spec.NonDet.pure (σ := State Unit) () σ = ({((), σ)}, False) from rfl), nd_pure_bindSc] + unfold setReleaseQueue tcbSchedDequeue + rw [List.filter_eq_self.2 fun a ha => by + have : t ≠ a := fun e => ht (e ▸ ha) + simpa using this] + rfl + +/-- `test_reschedule f` when `f` is current or the switch target (x86 `testReschedule_curN`). -/ +theorem testReschedule_posA {σ : State Unit} {f : ObjRef} + (h : f = σ.curThread ∨ σ.schedulerAction = .switchThread f) : + testReschedule (Z := Unit) f σ = rescheduleRequired (Z := Unit) σ := by + unfold testReschedule + rw [nd_gets_bindSc, nd_gets_bindSc, nd_pure_bindSc] + unfold Fl4Spec.NonDet.when + rw [if_pos] + rcases h with h | h + · exact Or.inl h + · right; rw [h]; try exact rfl + +/-- `test_reschedule f` when `f` is neither current nor the switch target: nothing. -/ +theorem testReschedule_negA {σ : State Unit} {f : ObjRef} (h1 : f ≠ σ.curThread) + (h2 : σ.schedulerAction ≠ .switchThread f) : testReschedule (Z := Unit) f σ = ({((), σ)}, False) := by + unfold testReschedule + rw [nd_gets_bindSc, nd_gets_bindSc, nd_pure_bindSc] + unfold Fl4Spec.NonDet.when + rw [if_neg] + · rfl + rintro (h | h) + · exact h1 h + · rcases hsa : σ.schedulerAction with _ | p | _ <;> rw [hsa] at h h2 + all_goals first | exact (show False from h).elim | exact h2 (by rw [show f = p from h]) + +/-- **`sched_context_donate` of an SC bound to a thread in no queue** (module docstring; x86 `donate_curN_eq`). -/ +theorem donate_fromSpecA {σ σR : State Unit} {p d f : ObjRef} {y : SchedContext} {n : Nat} {xf xd : Tcb} + (hkp : σ.kheap p = some (.schedContext y n)) (hyf : y.scTcb = some f) (hkf : σ.kheap f = some (.tcb xf)) + (hdom : xf.tcbDomain = 0) (hnqs : ∀ q, f ∉ σ.readyQueues 0 q) (hnr : f ∉ σ.releaseQueue) + (hR : testReschedule (Z := Unit) f (unbTStA σ f xf) = ({((), σR)}, False)) + (hkR : σR.kheap = (unbTStA σ f xf).kheap) (hkd : σ.kheap d = some (.tcb xd)) (hfd : f ≠ d) : + schedContextDonate (Z := Unit) p d σ = ({((), donStA σR p d y n xd)}, False) := by + have hpf : p ≠ f := fun e => by rw [e, hkf] at hkp; cases hkp + have hpd : p ≠ d := fun e => by rw [e, hkd] at hkp; cases hkp + have hg : getScObjRef (Z := Unit) SchedContext.scTcb p σ = ({(y.scTcb, σ)}, False) := by + unfold getScObjRef; rw [nd_bind_oneSc (getSchedContext_eqA hkp)]; rfl + have hQ : setRqA σ xf.tcbPriority (tcbSchedDequeue f (σ.readyQueues 0 xf.tcbPriority)) = σ := by + have h1 : tcbSchedDequeue f (σ.readyQueues 0 xf.tcbPriority) = σ.readyQueues 0 xf.tcbPriority := by + unfold tcbSchedDequeue + exact List.filter_eq_self.2 fun a ha => by + have : f ≠ a := fun e => hnqs _ (e ▸ ha) + simpa using this + rw [h1, setRqA_selfA] + have hkp1 : σR.kheap p = some (.schedContext y n) := by rw [hkR, unbTStA_kheap, if_neg hpf]; exact hkp + have hkd1 : (updKSc σR p (.schedContext { y with scTcb := some d } n)).kheap d = some (.tcb xd) := by + rw [updKSc_kheap, Function.update_of_ne (Ne.symm hpd), hkR, unbTStA_kheap, if_neg (Ne.symm hfd)]; exact hkd + unfold schedContextDonate + rw [nd_bind_oneSc hg, hyf] + unfold Fl4Spec.NonDet.when + rw [if_pos (by simp)] + have hin : (do + let fromTptr ← Fl4Spec.NonDet.assertOpt (some f) + tcbSchedAction tcbSchedDequeue fromTptr + tcbReleaseRemove fromTptr + setTcbObjRef tcbSchedContextUpdate fromTptr none + testReschedule fromTptr : SMonad Unit Unit) σ = ({((), σR)}, False) := by + rw [nd_bind_oneSc (show Fl4Spec.NonDet.assertOpt (σ := State Unit) (some f) σ = ({(f, σ)}, False) from rfl)] + rw [nd_bind_oneSc (tcbSchedAction_eqA hkf hdom _), hQ] + rw [nd_bind_oneSc (tcbReleaseRemove_notA hnr)] + show (threadSet _ f >>= _) σ = _ + rw [nd_bind_oneSc (threadSet_eqSc _ hkf)] + exact hR + rw [nd_bind_oneSc hin] + show (updateSchedContext p _ >>= _) σR = _ + rw [nd_bind_oneSc (updateSchedContext_eqA hkp1 _)] + rw [show scTcbUpdate (fun _ => some d) y = { y with scTcb := some d } from rfl] + show threadSet _ d _ = _ + rw [threadSet_eqSc _ hkd1] + rfl + +end Spec + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **The TCB half of the unbinding** (module docstring; x86 `SchedRel.set_tcb_sc` with `v = 0`). -/ +theorem SchedSRA.unbindTA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} + {σ : Fl4SpecA64.State Unit} (H : SchedSRA st Z m κ s σ) {sc t : U64} (hsc : sc ∈ Z.C) (ht : t ∈ Z.T) + (hsc0 : sc ≠ 0#u64) (hts : L.mem s (scA t) = sc) (hnr : specPtr t ∉ σ.releaseQueue) {xt : Fl4SpecA64.Tcb} + (hkt : σ.kheap (specPtr t) = some (.tcb xt)) : + WritesIn st s (L.wr s (scA t) 0#u64) (fun b => b = scA t) ∧ + SchedSRA st Z m κ (L.wr s (scA t) 0#u64) (unbTStA σ (specPtr t) xt) := by + set σ2 := unbTStA σ (specPtr t) xt with hσ2 + have hk2 : ∀ p, p ≠ specPtr t → σ2.kheap p = σ.kheap p := fun p h => by + rw [hσ2, unbTStA_kheap, if_neg h] + have hk2T : σ2.kheap (specPtr t) = some (.tcb { xt with tcbSchedContext := none }) := by + rw [hσ2, unbTStA_kheap, if_pos rfl] + -- the SC of a binding is not `t` + have hpT : ∀ u x p, σ.kheap u = some (.tcb x) → x.tcbSchedContext = some p → p ≠ specPtr t := by + intro u x p hu hp e + obtain ⟨y, n, hy, -⟩ := H.inv.back u x p hu hp + rw [e, hkt] at hy; cases hy + have hTk : ∀ u x, σ.kheap u = some (.tcb x) → ∃ x', σ2.kheap u = some (.tcb x') ∧ + x'.tcbState = x.tcbState ∧ x'.tcbDomain = x.tcbDomain ∧ x'.tcbPriority = x.tcbPriority := by + intro u x hu + by_cases e1 : u = specPtr t + · rw [e1, hkt] at hu; cases hu; rw [e1]; exact ⟨_, hk2T, rfl, rfl, rfl⟩ + · exact ⟨x, by rw [hk2 u e1]; exact hu, rfl, rfl, rfl⟩ + have hB : ∀ q o, σ2.kheap q = some o → (∀ x, o ≠ .tcb x) → σ.kheap q = some o := by + intro q o hq ho + by_cases e1 : q = specPtr t + · rw [e1, hk2T] at hq; cases hq; exact absurd rfl (ho _) + · rwa [hk2 q e1] at hq + have hinv : ScInvA m σ2 := by + have hR := H.inv.relKeep (σ' := σ2) rfl + (fun u hu x hx => ⟨x, by rw [hk2 u (fun e => hnr (e ▸ hu))]; exact hx, rfl⟩) + (fun u _ x p hx hp => hk2 p (hpT u x p hx hp)) + refine ⟨fun p y n hy => H.inv.refills p y n (hB _ _ hy (fun _ h => by cases h)), + fun p y n hy => H.inv.rr p y n (hB _ _ hy (fun _ h => by cases h)), + fun q z hz => H.inv.ntfnIdle q z (hB _ _ hz (fun _ h => by cases h)), + fun q z hz => H.inv.ntfnIdleOf q z (hB _ _ hz (fun _ h => by cases h)), hR.1, hR.2, + fun u x p hx hp => ?_, fun u x p hx hp hi => ?_⟩ + · by_cases e1 : u = specPtr t + · rw [e1, hk2T] at hx; cases hx; cases hp + · rw [hk2 u e1] at hx + obtain ⟨y, n, hy, hyu⟩ := H.inv.back u x p hx hp + exact ⟨y, n, by rw [hk2 p (hpT u x p hx hp)]; exact hy, hyu⟩ + · by_cases e1 : u = specPtr t + · rw [e1, hk2T] at hx; cases hx; cases hp + · rw [hk2 u e1] at hx + exact H.inv.tcbScNotIdle u x p hx hp hi + -- the FL4 write + have hT := H.core.tcbs t ht + have hTf := hT.fits + have hTa := hT.align + have hv2 : (scA t).val = t.val + 3464 := by + show (wa t 3464#u64).val = _ + rw [wa_valm]; have : (3464#u64 : U64).val = 3464 := by simp + omega + have hA2 : Aligned (scA t) := by unfold Aligned; rw [hv2]; omega + set s' := L.wr s (scA t) 0#u64 with hs' + have hw : WritesIn st s s' (fun b => b = scA t) := writesIn_wr hA2 + have hw2 : WritesIn st s s' (fun b => b = scTcbA sc ∨ b = scA t) := hw.mono fun _ h => Or.inr h + obtain ⟨-, -, tK, -⟩ := H.donW hsc ht hw2 + have hkc : ∀ c ∈ Z.C, σ2.kheap (scNameA m c) = σ.kheap (scNameA m c) := fun c hc => by + obtain ⟨y, k, hy, -⟩ := H.scr.sc c hc + exact hk2 _ (fun e => by rw [e, hkt] at hy; cases hy) + have hS2 : ScStateRelA st Z.C m s' σ2 := + H.scr.frame (fun a ha hfa => hw.mem_eq ha (fun e => by + have := H.scp.th_out hfa ((H.thT t).2 ht) + simp only [ObjKindA.size] at this + rw [e, hv2] at this; omega)) hkc + have hP2 : ScPlacedA Z.th (L.ksb s) Z.C m σ2 := H.scp.of_kheap hkc + have hst0 : L.mem s' (scA t) = 0#u64 := by rw [hs', L.mem_wr _ _ _ _ hA2 hA2, if_pos rfl] + have N2 := H.rebuild2 (σ2 := σ2) hsc ht hw2 ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ rfl rfl rfl hS2 hP2 hTk + (fun u hu => by + by_cases e : u = t + · subst e; refine ⟨_, hk2T, ?_⟩; rw [hst0]; simp [optScA] + · have hne : specPtr u ≠ specPtr t := fun h => e (specPtr_inj h) + obtain ⟨x, h1, h3⟩ := H.sch.tcb u hu + refine ⟨x, by rw [hk2 _ hne]; exact h1, ?_⟩ + rw [show scA u = wa u 3464#u64 from rfl, tK u hu 3464#u64 (by simp) (by simp) (fun h => absurd h e)] + exact h3) + (fun u hu h => by + by_cases e : u = t + · subst e; rw [hst0] at h; exact absurd rfl h + · rw [show scA u = wa u 3464#u64 from rfl, tK u hu 3464#u64 (by simp) (by simp) + (fun h => absurd h e)] at h ⊢ + exact H.rest.scMem u hu h) + hinv + refine ⟨hw, N2, fun u x hx hc => ?_⟩ + intro x' scp y k h1 h2 h3 h4 h5 + by_cases e1 : u = specPtr t + · rw [e1, hk2T] at h1; cases h1; cases h2 + rw [hk2 u e1] at hx h1 + rw [hk2 scp (hpT u x' scp h1 h2)] at h3 + exact H.rd u x hx hc x' scp y k h1 h2 h3 h4 h5 + +/-- The TCB half of the unbinding as the frame `MFrA`. -/ +theorem SchedSRN.unbT_mfr {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s s' : S} + {σ : Fl4SpecA64.State Unit} (_H : SchedSRN st Z m κ s σ) {t : U64} (ht : t ∈ Z.T) + (hw : WritesIn st s s' (fun b => b = scA t)) {xt : Fl4SpecA64.Tcb} + (hkt : σ.kheap (specPtr t) = some (.tcb xt)) : MFrA (st := st) Z m s s' σ (unbTStA σ (specPtr t) xt) := by + refine ⟨hw.mono fun _ e => Or.inr (Or.inr ⟨t, ht, Or.inl e⟩), ⟨fun p o hp h3 h4 => ?_, fun p x hp => ?_⟩, + fun p y n hp => ?_⟩ + · rw [unbTStA_kheap, if_neg (fun e => by subst e; rw [hkt] at hp; cases hp; exact h4 xt rfl)]; exact hp + · by_cases e : p = specPtr t + · subst e; rw [hkt] at hp; cases hp + exact ⟨_, by rw [unbTStA_kheap, if_pos rfl], none, rfl⟩ + · exact ⟨x, by rw [unbTStA_kheap, if_neg e]; exact hp, x.tcbSchedContext, rfl⟩ + · rw [unbTStA_kheap] at hp + by_cases e : p = specPtr t + · rw [if_pos e] at hp; cases hp + · rw [if_neg e] at hp; exact ⟨y, hp⟩ + +end + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +/-- **`schedcontext::donate` of a bound SC refines `sched_context_donate`** (module docstring). -/ +theorem sc_donate_fromA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} + {σ : Fl4SpecA64.State Unit} (H : SchedSRA st Z m κ s σ) (hst : StEqA Z.T (L.mem s) σ) (a : A) + {sc f d : U64} (hsc : sc ∈ Z.C) (hsc0 : sc ≠ 0#u64) (hf : f ∈ Z.T) (hfs : L.mem s (scA f) = sc) + (hd : d ∈ Z.T) (hds0 : L.mem s (scA d) = 0#u64) (hfd : f ≠ d) (hfon : L.mem s (affA f) = L.cpu s) + (hnqs : ∀ d p, specPtr f ∉ σ.readyQueues d p) (hnr : specPtr f ∉ σ.releaseQueue) + (hstall : smp.remote_tcb_stall st ar s a f = ok (s, a)) + (hmig : L.mem s (affA d) = L.mem s (scCoreA sc)) (hni : NotIdleScA m (scNameA m sc)) : + schedcontext.donate st ar s a sc d ⦃ r => r.2 = a ∧ ∃ σ' Q', + Fl4SpecA64.schedContextDonate (Z := Unit) (scNameA m sc) (specPtr d) σ = ({((), σ')}, False) ∧ + SchedSRB st { Z with Q := Q' } m κ r.1 σ' (specPtr d) ∧ MFrA (st := st) Z m s r.1 σ σ' ⦄ := by + obtain ⟨xs, ns, hks, hx⟩ := H.scr.sc sc hsc + obtain ⟨xf, hkf, hxf⟩ := H.sch.tcb f hf + obtain ⟨xd, hkd, hxd⟩ := H.sch.tcb d hd + have hTf := H.core.tcbs f hf + have hf0 : f ≠ 0#u64 := fun e => hTf.ne0 (by rw [e]; rfl) + have hf1 : f ≠ 1#u64 := fun e => by have := hTf.align; rw [e] at this; simp at this + have hfd' : specPtr f ≠ specPtr d := fun e => hfd (specPtr_inj e) + have hxfs : xf.tcbSchedContext = some (scNameA m sc) := by + rw [hxf, hfs]; unfold optScA; rw [if_neg hsc0]; rfl + have hyf : xs.scTcb = some (specPtr f) := by + obtain ⟨y, n, hy, hyu⟩ := H.inv.back _ xf _ hkf hxfs + rw [hks] at hy; cases hy; exact hyu + have hwf : L.mem s (scTcbA sc) = f := by + have h := hx.tcb + rw [hyf, ← scTcbA_eqSc] at h + by_cases e : L.mem s (scTcbA sc) = 0#u64 + · rw [e, optRefS_zero] at h; cases h + · rw [optRefS_ne e] at h; exact (specPtr_inj (Option.some.inj h)).symm + have hdom : xf.tcbDomain = 0 := by + obtain ⟨x, h1, h2, -⟩ := H.rest.prio f hf; rw [hkf] at h1; cases h1; exact h2 + have hq0 := H.toSchedSRN.qBit0 hf hfon hnqs + have hr0 : (L.mem s (inqA f) &&& sched.Q_RELEASE) = 0#u64 := by + by_contra h + have hm := (H.core.released f hf).1 h + rw [hfon] at hm + exact hnr (by rw [H.rest.rel]; exact List.mem_map_of_mem hm) + -- the unbinding of `f` + set s1 := L.wr s (scA f) 0#u64 with hs1 + set σ1 := unbTStA σ (specPtr f) xf with hσ1 + obtain ⟨hwU, H1⟩ := H.unbindTA hsc hf hsc0 hfs hnr hkf + have F1 : MFrA (st := st) Z m s s1 σ σ1 := H.toSchedSRN.unbT_mfr hf hwU hkf + have hst1 : StEqA Z.T (L.mem s1) σ1 := by + obtain ⟨-, -, tK, -⟩ := H.donW hsc hf (hwU.mono fun _ h => Or.inr h) + refine hst.keep (fun u hu => by rw [stA_eq, tK u hu 3328#u64 (by simp) (by simp) (fun _ => by simp)]) + (fun u x hx => ?_) + rw [hσ1, unbTStA_kheap] at hx + by_cases e : u = specPtr f + · rw [if_pos e] at hx; cases hx; exact ⟨xf, by rw [e]; exact hkf, rfl⟩ + · rw [if_neg e] at hx; exact ⟨x, hx, rfl⟩ + -- the end of the `from` branch + have fin : ∀ (s6 : S) (σR : Fl4SpecA64.State Unit) (Q1 : U64 → U64 → List U64), + SchedSRA st { Z with Q := Q1 } m κ s6 σR → + Fl4SpecA64.testReschedule (Z := Unit) (specPtr f) σ1 = ({((), σR)}, False) → σR.kheap = σ1.kheap → + MFrA (st := st) Z m s1 s6 σ1 σR → + (do + let s2 ← obj.Sc.set_tcb st sc s6 d + let s3 ← obj.Tcb.set_sc st d s2 sc + let i1 ← obj.Sc.cpu st sc s3 + smp.migrate_tcb st ar s3 a d i1) ⦃ r => r.2 = a ∧ ∃ σ' Q', + Fl4SpecA64.schedContextDonate (Z := Unit) (scNameA m sc) (specPtr d) σ = ({((), σ')}, False) ∧ + SchedSRB st { Z with Q := Q' } m κ r.1 σ' (specPtr d) ∧ MFrA (st := st) Z m s r.1 σ σ' ⦄ := by + intro s6 σR Q1 H6 hR hkR F6 + have hNf : scNameA m sc ≠ specPtr f := fun e => by rw [e, hkf] at hks; cases hks + have hks6 : σR.kheap (scNameA m sc) = some (.schedContext xs ns) := by + rw [hkR, hσ1, unbTStA_kheap, if_neg hNf]; exact hks + have hkd6 : σR.kheap (specPtr d) = some (.tcb xd) := by + rw [hkR, hσ1, unbTStA_kheap, if_neg (Ne.symm hfd')]; exact hkd + have hds6 : L.mem s6 (scA d) = 0#u64 := by + obtain ⟨x6, h6, e6⟩ := H6.sch.tcb d hd + rw [hkd6] at h6; cases h6 + rw [hds0] at hxd + by_contra hne + rw [hxd] at e6; unfold optScA at e6; rw [if_neg hne] at e6; simp at e6 + have hfree : ∀ u x, σR.kheap u = some (.tcb x) → x.tcbSchedContext ≠ some (scNameA m sc) := by + intro u x hu hp + rw [hkR, hσ1, unbTStA_kheap] at hu + by_cases e : u = specPtr f + · rw [if_pos e] at hu; cases hu; cases hp + · rw [if_neg e] at hu + obtain ⟨y, n, hy, hyu⟩ := H.inv.back u x _ hu hp + rw [hks] at hy; cases hy; rw [hyf] at hyu; cases hyu; exact e rfl + have N7 := H6.donateG hsc hd hsc0 hfree hds6 hni hks6 hkd6 + obtain ⟨hA1, hA2, -⟩ := H6.toSchedSRN.donAddr hsc hd + have hw7 : WritesIn st s6 (L.wr (L.wr s6 (scTcbA sc) d) (scA d) sc) (fun b => b = scTcbA sc ∨ b = scA d) := + (writesIn_wr hA1).trans (writesIn_wr hA2) + have hNd : scNameA m sc ≠ specPtr d := fun e => by rw [e, hkd] at hks; cases hks + have hK7 := SKeepA.two (ys := { xs with scTcb := some (specPtr d) }) (o := some (scNameA m sc)) + (fun p => donStA_kheap hNd p) hks6 hkd6 + have F7 : MFrA (st := st) { Z with Q := Q1 } m s6 (L.wr (L.wr s6 (scTcbA sc) d) (scA d) sc) σR + (donStA σR (scNameA m sc) (specPtr d) xs ns xd) := + ⟨hw7.mono fun _ hb => H6.toSchedSRN.mwr_two hsc hd hb, hK7.1, hK7.2⟩ + refine WP.spec_bind (set_tcb_specSc s6 sc d) ?_ + rintro s2 rfl + refine WP.spec_bind (set_sc_specSc _ d sc) ?_ + rintro s3 rfl + refine WP.spec_bind (obj.Sc.cpu.spec _ sc) ?_ + rintro cpu rfl + have hmig3 : L.mem (L.wr (L.wr s6 (scTcbA sc) d) (scA d) sc) (affA d) = + L.mem (L.wr (L.wr s6 (scTcbA sc) d) (scA d) sc) (scCoreA sc) := by + have e1 := N7.sch.aff d hd + have e2 := H.sch.aff d hd + rw [e2] at e1; replace e1 := Option.some.inj e1 + rw [N7.scCore sc hsc, ← H.scCore sc hsc, ← hmig] + exact (UScalar.eq_of_val_eq e1).symm + unfold smp.migrate_tcb + refine WP.spec_bind (obj.Tcb.affinity.spec _ d) ?_ + rintro i rfl + rw [hmig3] + simp only [bne_self_eq_false, Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + refine ⟨by trivial, _, Q1, donate_fromSpecA hks hyf hkf hdom (fun q => hnqs 0 q) hnr hR hkR hkd hfd', N7, ?_⟩ + exact (F1.trans F6 rfl rfl).trans F7 rfl rfl + -- the program + unfold schedcontext.donate + refine WP.spec_bind (obj.Sc.tcb.spec s sc) ?_ + rintro i rfl + rw [hwf] + refine WP.spec_bind (obj.Tcb.is_null.spec _) ?_ + rintro b rfl + rw [decide_eq_false hf0] + simp only [Bool.false_eq_true, ↓reduceIte] + refine WP.spec_bind (Pₘ := fun r => r.2 = a ∧ ∃ σR Q1, SchedSRA st { Z with Q := Q1 } m κ r.1 σR ∧ + Fl4SpecA64.testReschedule (Z := Unit) (specPtr f) σ1 = ({((), σR)}, False) ∧ σR.kheap = σ1.kheap ∧ + MFrA (st := st) Z m s1 r.1 σ1 σR) ?_ ?_ + · refine WP.spec_bind (Pₘ := fun r => r = (s, a)) (by rw [hstall]; exact (WP.spec_ok _).2 rfl) ?_ + rintro ⟨s2, a2⟩ e + simp only [Prod.mk.injEq] at e + obtain ⟨e1, e2⟩ := e + subst s2 a2 + refine WP.spec_bind (sched.dequeue.noop s f hq0) ?_ + rintro s3 e3 + subst s3 + refine WP.spec_bind (sched.release_remove.noop s f hr0) ?_ + rintro s4 e4 + subst s4 + refine WP.spec_bind (set_sc_specSc s f 0#u64) ?_ + rintro s5 rfl + refine WP.spec_bind (sched.cur_thread.spec _) ?_ + rintro ct rfl + refine WP.spec_bind (Pₘ := fun b1 => b1 = decide (f = L.curW s1)) + (by unfold obj.Tcb.Insts.CoreCmpPartialEqTcb.eq; exact (WP.spec_ok _).2 rfl) ?_ + rintro b1 rfl + have hct : σ1.curThread = specPtr (L.curW s1) := H1.sch.curThread + have resch : ∀ (_hpos : specPtr f = σ1.curThread ∨ σ1.schedulerAction = .switchThread (specPtr f)), + sched.reschedule_required st s1 ⦃ s6 => ∃ σR Q1, SchedSRA st { Z with Q := Q1 } m κ s6 σR ∧ + Fl4SpecA64.testReschedule (Z := Unit) (specPtr f) σ1 = ({((), σR)}, False) ∧ σR.kheap = σ1.kheap ∧ + MFrA (st := st) Z m s1 s6 σ1 σR ⦄ := fun hpos => + WP.spec_mono (reschedule_required_refinesA H1 hst1) fun _ ⟨σR, Q1, h1, hk1, _, hw1, H6⟩ => + ⟨σR, Q1, H6, (testReschedule_posA hpos).trans h1, hk1, MFrA.of_sw hk1 hw1⟩ + by_cases ec : f = L.curW s1 + · rw [decide_eq_true ec] + simp only [↓reduceIte] + exact WP.spec_bind (resch (Or.inl (by rw [hct, ← ec]))) fun _ h => (WP.spec_ok _).2 ⟨rfl, h⟩ + · rw [decide_eq_false ec] + simp only [Bool.false_eq_true, ↓reduceIte] + refine WP.spec_bind (Pₘ := fun t1 => ∃ σR Q1, SchedSRA st { Z with Q := Q1 } m κ t1 σR ∧ + Fl4SpecA64.testReschedule (Z := Unit) (specPtr f) σ1 = ({((), σR)}, False) ∧ σR.kheap = σ1.kheap ∧ + MFrA (st := st) Z m s1 t1 σ1 σR) ?_ fun _ h => (WP.spec_ok _).2 ⟨rfl, h⟩ + refine WP.spec_bind (sched.action.spec _) ?_ + rintro i1 rfl + by_cases ea : f = L.actionW s1 + · rw [if_pos ea] + refine resch (Or.inr ?_) + rw [H1.sch.act, ← ea]; unfold actOfA; rw [if_neg hf0, if_neg hf1] + · rw [if_neg ea] + exact (WP.spec_ok _).2 ⟨σ1, Z.Q, H1, testReschedule_negA (fun e => ec (specPtr_inj (e.trans hct))) + (fun e => ea (specPtr_inj (actOfA_switchA (H1.sch.act.symm.trans e)).2.2)), rfl, MFrA.refl Z m s1 σ1⟩ + · rintro ⟨s6, a6⟩ ⟨ea6, σR, Q1, H6, hR, hkR, F6⟩ + obtain rfl : a6 = a := ea6 + exact fin s6 σR Q1 H6 hR hkR F6 + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScDonQueuedA.lean b/proofs/Fl4ProofsA64/P3/ScDonQueuedA.lean new file mode 100644 index 000000000..f575268bd --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScDonQueuedA.lean @@ -0,0 +1,474 @@ +/- +P3 (AArch64, delete hooks W3 Reply (a)): **`sched_context_donate` of an SC bound to a holder that may be queued**. +FL4's `donate` dequeues and release-removes the holder first; `P3/ScDonFromA`'s `sc_donate_fromA` assumes it is in no +queue. Here the two removals are run through `dequeue_refines_localA` / `release_remove_refines_localA`. + +* **`donate_fromQSpecA`**: the spec side at the removals' outcomes. +* `SchedSRN.rebuild2N` (`rebuild2` from `SchedSRN`), **`SchedSRB.unbindTB`** (`unbindTA` when the release removal has + left `rd` open at the holder: unbound, it needs none). +* **`sc_donate_fromQA`**. + +No named premise (hypotheses as `sc_donate_fromA`'s, without `hnqs` / `hnr`). New module. +-/ +import Fl4ProofsA64.P3.ScDonFromA +import Fl4ProofsA64.P3.ScDeqA +import Fl4ProofsA64.P3.ScRelRmA +import Fl4ProofsA64.P3.CnodeCapsA +import Fl4ProofsA64.P3.ScConsumeMsgA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section Spec +open Fl4SpecA64 + +/-- **`sched_context_donate` of an SC bound to `f`, queued or not**: the removals take `σ` to `releaseRemoveStA σq f`, +then as `donate_fromSpecA`. -/ +theorem donate_fromQSpecA {σ σq σR : State Unit} {p d f : ObjRef} {y : SchedContext} {n : Nat} {xf xd : Tcb} + (hkp : σ.kheap p = some (.schedContext y n)) (hyf : y.scTcb = some f) (hkf : σ.kheap f = some (.tcb xf)) + (hdom : xf.tcbDomain = 0) + (hdq : tcbSchedAction (Z := Unit) tcbSchedDequeue f σ = ({((), σq)}, False)) (hkq : σq.kheap = σ.kheap) + (hnr : f ∉ (releaseRemoveStA σq f).releaseQueue) + (hR : testReschedule (Z := Unit) f (unbTStA (releaseRemoveStA σq f) f xf) = ({((), σR)}, False)) + (hkR : σR.kheap = (unbTStA (releaseRemoveStA σq f) f xf).kheap) (hkd : σ.kheap d = some (.tcb xd)) + (hfd : f ≠ d) : + schedContextDonate (Z := Unit) p d σ = ({((), donStA σR p d y n xd)}, False) := by + have hkr : (releaseRemoveStA σq f).kheap = σ.kheap := hkq + generalize hσr : releaseRemoveStA σq f = σr at hnr hR hkR hkr + have hpf : p ≠ f := fun e => by rw [e, hkf] at hkp; cases hkp + have hpd : p ≠ d := fun e => by rw [e, hkd] at hkp; cases hkp + have hg : getScObjRef (Z := Unit) SchedContext.scTcb p σ = ({(y.scTcb, σ)}, False) := by + unfold getScObjRef; rw [nd_bind_oneSc (getSchedContext_eqA hkp)]; rfl + have hkfr : σr.kheap f = some (.tcb xf) := by rw [hkr]; exact hkf + have hkp1 : σR.kheap p = some (.schedContext y n) := by + rw [hkR, unbTStA_kheap, if_neg hpf, hkr]; exact hkp + have hkd1 : (updKSc σR p (.schedContext { y with scTcb := some d } n)).kheap d = some (.tcb xd) := by + rw [updKSc_kheap, Function.update_of_ne (Ne.symm hpd), hkR, unbTStA_kheap, if_neg (Ne.symm hfd), hkr] + exact hkd + unfold schedContextDonate + rw [nd_bind_oneSc hg, hyf] + unfold Fl4Spec.NonDet.when + rw [if_pos (by simp)] + have hin : (do + let fromTptr ← Fl4Spec.NonDet.assertOpt (some f) + tcbSchedAction tcbSchedDequeue fromTptr + tcbReleaseRemove fromTptr + setTcbObjRef tcbSchedContextUpdate fromTptr none + testReschedule fromTptr : SMonad Unit Unit) σ = ({((), σR)}, False) := by + rw [nd_bind_oneSc (show Fl4Spec.NonDet.assertOpt (σ := State Unit) (some f) σ = ({(f, σ)}, False) from rfl)] + rw [nd_bind_oneSc hdq] + rw [nd_bind_oneSc (show tcbReleaseRemove (Z := Unit) f σq = ({((), σr)}, False) by + rw [← hσr]; exact tcbReleaseRemove_eqA σq f)] + show (threadSet _ f >>= _) σr = _ + rw [nd_bind_oneSc (threadSet_eqSc _ hkfr)] + exact hR + rw [nd_bind_oneSc hin] + show (updateSchedContext p _ >>= _) σR = _ + rw [nd_bind_oneSc (updateSchedContext_eqA hkp1 _)] + rw [show scTcbUpdate (fun _ => some d) y = { y with scTcb := some d } from rfl] + show threadSet _ d _ = _ + rw [threadSet_eqSc _ hkd1] + rfl + +end Spec + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +theorem SchedSRN.rebuild2N {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s s' : S} + {σ σ2 : Fl4SpecA64.State Unit} (H : SchedSRN st Z m κ s σ) {sc t : U64} (hsc : sc ∈ Z.C) (ht : t ∈ Z.T) + (hw : WritesIn st s s' (fun b => b = scTcbA sc ∨ b = scA t)) (hfix : SFixA σ σ2) + (hact : σ2.schedulerAction = σ.schedulerAction) (hrq : σ2.readyQueues = σ.readyQueues) + (hrel : σ2.releaseQueue = σ.releaseQueue) + (hS2 : ScStateRelA st Z.C m s' σ2) (hP2 : ScPlacedA Z.th (L.ksb s) Z.C m σ2) + (hTk : ∀ u x, σ.kheap u = some (.tcb x) → ∃ x', σ2.kheap u = some (.tcb x') ∧ + x'.tcbState = x.tcbState ∧ x'.tcbDomain = x.tcbDomain ∧ x'.tcbPriority = x.tcbPriority) + (htcbSc : ∀ u ∈ Z.T, ∃ x, σ2.kheap (specPtr u) = some (.tcb x) ∧ + x.tcbSchedContext = optScA m (L.mem s' (scA u))) + (hscMem : ∀ u ∈ Z.T, L.mem s' (scA u) ≠ 0#u64 → L.mem s' (scA u) ∈ Z.C) (hinv : ScInvA m σ2) : + SchedSRN st Z m κ s' σ2 := by + obtain ⟨nK, gK, tK, sK⟩ := H.donW hsc ht hw + obtain ⟨xs, ns, hks, hx⟩ := H.scr.sc sc hsc + have h24 := hx.hdr (K := 24#u64) (by simp) (by simp) + have hcc : ∀ c ∈ Z.C, (scCoreA c).val ≠ (scTcbA sc).val := by + intro c hc e + have e' : scCoreA c = scTcbA sc := UScalar.eq_of_val_eq e + obtain ⟨y, k, hy, hyr⟩ := H.scr.sc c hc + have h16 := hyr.hdr (K := 16#u64) (by simp) (by simp) + have hcE : scCoreA c = scWA c 16#u64 := by unfold scCoreA scWA layout.sc.CORE; rfl + by_cases ec : c = sc + · rw [ec] at e' + obtain ⟨-, -, hfit⟩ := hx.bounds + have v1 := scWA_val (sc := sc) (K := 16#u64) (by simp; omega) + have v2 := scWA_val (sc := sc) (K := 24#u64) (by simp; omega) + have := congrArg UScalar.val e' + rw [show scCoreA sc = scWA sc 16#u64 by unfold scCoreA scWA layout.sc.CORE; rfl, scTcbA_eqSc, v1, v2] at this + simp at this + · refine H.scp.disj c hc sc hsc ec y k xs ns hy hks (scCoreA c) (by rw [hcE]; exact hyr.in_obj h16.2) ?_ + rw [e', scTcbA_eqSc]; exact hx.in_obj h24.2 + exact H.rebuildG (Q' := Z.Q) (R' := Z.R) (s' := s') (σ' := σ2) hw.1 hw.2.1 + (fun c hc o h1 h2 _ _ => nK c hc o (by omega) h2) + (fun K hK => gK K (by rcases hK with rfl | rfl <;> simp) (by rcases hK with rfl | rfl <;> simp)) + (fun y hy K hK => tK y hy K (by rcases hK with rfl | rfl | rfl <;> simp) + (by rcases hK with rfl | rfl | rfl <;> simp) (fun _ => by rcases hK with rfl | rfl | rfl <;> simp)) + hS2 hP2 + (fun u hu x hx => by obtain ⟨x', h1, -, h2, h3⟩ := hTk _ x hx; exact ⟨x', h1, h2, h3⟩) + htcbSc hscMem + ⟨hfix.curThread, hfix.curSc, hfix.idleThread, hfix.curTime, hfix.consumedTime, rfl, hfix.curDomain, + hfix.domainTime⟩ + (by + rw [hfix.reprogramTimer, nK _ H.sch.cpuCs layout.node.REPROGRAM (by simp [layout.node.REPROGRAM]) + (by simp [layout.node.REPROGRAM])] + exact H.rest.repr) + (by + rw [hact, nK _ H.sch.cpuCs layout.node.SCHED_ACTION (by simp [layout.node.SCHED_ACTION]) + (by simp [layout.node.SCHED_ACTION])] + exact H.sch.act) + (fun u h => H.actOn u (by rw [← hact]; exact h)) hinv + (H.core.agree hw.1 + (fun u hu => ⟨tK u hu 3480#u64 (by simp) (by simp) (fun _ => by simp), + tK u hu 3488#u64 (by simp) (by simp) (fun _ => by simp)⟩) + (fun c hc o h1 h2 _ => nK c hc o h1 h2) (fun u hu => tK u hu 3592#u64 (by simp) (by simp) (fun _ => by simp)) + (fun u hu => tK u hu 3520#u64 (by simp) (by simp) (fun _ => by simp)) + (fun u hu => tK u hu 3448#u64 (by simp) (by simp) (fun _ => by simp))) + (by rw [hrq]; exact H.rest.rq) (by rw [hrq]; exact H.rest.rqDom) (by rw [hrel]; exact H.rest.rel) + ⟨fun d p u hu => by + obtain ⟨x, h1, h2⟩ := H.run.1 d p u (by rw [← hrq]; exact hu) + obtain ⟨x', h1', e1, -, -⟩ := hTk u x h1 + exact ⟨x', h1', by rw [e1]; exact h2⟩, + fun u hu => by + obtain ⟨x, h1, h2⟩ := H.run.2 u (by rw [← hrel]; exact hu) + obtain ⟨x', h1', e1, -, -⟩ := hTk u x h1 + exact ⟨x', h1', by rw [e1]; exact h2⟩⟩ + (fun c hc => sK _ (H.scCoreFoot hc).1 (H.scCoreFoot hc).2 (hcc c hc)) + +/-- `unbindTA` from `SchedSRB` at the unbound thread (module docstring). -/ +theorem SchedSRB.unbindTB {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} + {σ : Fl4SpecA64.State Unit} {sc t : U64} (H : SchedSRB st Z m κ s σ (specPtr t)) (hsc : sc ∈ Z.C) (ht : t ∈ Z.T) + (hsc0 : sc ≠ 0#u64) (hts : L.mem s (scA t) = sc) (hnr : specPtr t ∉ σ.releaseQueue) {xt : Fl4SpecA64.Tcb} + (hkt : σ.kheap (specPtr t) = some (.tcb xt)) : + WritesIn st s (L.wr s (scA t) 0#u64) (fun b => b = scA t) ∧ + SchedSRA st Z m κ (L.wr s (scA t) 0#u64) (unbTStA σ (specPtr t) xt) := by + set σ2 := unbTStA σ (specPtr t) xt with hσ2 + have hk2 : ∀ p, p ≠ specPtr t → σ2.kheap p = σ.kheap p := fun p h => by + rw [hσ2, unbTStA_kheap, if_neg h] + have hk2T : σ2.kheap (specPtr t) = some (.tcb { xt with tcbSchedContext := none }) := by + rw [hσ2, unbTStA_kheap, if_pos rfl] + -- the SC of a binding is not `t` + have hpT : ∀ u x p, σ.kheap u = some (.tcb x) → x.tcbSchedContext = some p → p ≠ specPtr t := by + intro u x p hu hp e + obtain ⟨y, n, hy, -⟩ := H.inv.back u x p hu hp + rw [e, hkt] at hy; cases hy + have hTk : ∀ u x, σ.kheap u = some (.tcb x) → ∃ x', σ2.kheap u = some (.tcb x') ∧ + x'.tcbState = x.tcbState ∧ x'.tcbDomain = x.tcbDomain ∧ x'.tcbPriority = x.tcbPriority := by + intro u x hu + by_cases e1 : u = specPtr t + · rw [e1, hkt] at hu; cases hu; rw [e1]; exact ⟨_, hk2T, rfl, rfl, rfl⟩ + · exact ⟨x, by rw [hk2 u e1]; exact hu, rfl, rfl, rfl⟩ + have hB : ∀ q o, σ2.kheap q = some o → (∀ x, o ≠ .tcb x) → σ.kheap q = some o := by + intro q o hq ho + by_cases e1 : q = specPtr t + · rw [e1, hk2T] at hq; cases hq; exact absurd rfl (ho _) + · rwa [hk2 q e1] at hq + have hinv : ScInvA m σ2 := by + have hR := H.inv.relKeep (σ' := σ2) rfl + (fun u hu x hx => ⟨x, by rw [hk2 u (fun e => hnr (e ▸ hu))]; exact hx, rfl⟩) + (fun u _ x p hx hp => hk2 p (hpT u x p hx hp)) + refine ⟨fun p y n hy => H.inv.refills p y n (hB _ _ hy (fun _ h => by cases h)), + fun p y n hy => H.inv.rr p y n (hB _ _ hy (fun _ h => by cases h)), + fun q z hz => H.inv.ntfnIdle q z (hB _ _ hz (fun _ h => by cases h)), + fun q z hz => H.inv.ntfnIdleOf q z (hB _ _ hz (fun _ h => by cases h)), hR.1, hR.2, + fun u x p hx hp => ?_, fun u x p hx hp hi => ?_⟩ + · by_cases e1 : u = specPtr t + · rw [e1, hk2T] at hx; cases hx; cases hp + · rw [hk2 u e1] at hx + obtain ⟨y, n, hy, hyu⟩ := H.inv.back u x p hx hp + exact ⟨y, n, by rw [hk2 p (hpT u x p hx hp)]; exact hy, hyu⟩ + · by_cases e1 : u = specPtr t + · rw [e1, hk2T] at hx; cases hx; cases hp + · rw [hk2 u e1] at hx + exact H.inv.tcbScNotIdle u x p hx hp hi + -- the FL4 write + have hT := H.core.tcbs t ht + have hTf := hT.fits + have hTa := hT.align + have hv2 : (scA t).val = t.val + 3464 := by + show (wa t 3464#u64).val = _ + rw [wa_valm]; have : (3464#u64 : U64).val = 3464 := by simp + omega + have hA2 : Aligned (scA t) := by unfold Aligned; rw [hv2]; omega + set s' := L.wr s (scA t) 0#u64 with hs' + have hw : WritesIn st s s' (fun b => b = scA t) := writesIn_wr hA2 + have hw2 : WritesIn st s s' (fun b => b = scTcbA sc ∨ b = scA t) := hw.mono fun _ h => Or.inr h + obtain ⟨-, -, tK, -⟩ := H.toSchedSRN.donW hsc ht hw2 + have hkc : ∀ c ∈ Z.C, σ2.kheap (scNameA m c) = σ.kheap (scNameA m c) := fun c hc => by + obtain ⟨y, k, hy, -⟩ := H.scr.sc c hc + exact hk2 _ (fun e => by rw [e, hkt] at hy; cases hy) + have hS2 : ScStateRelA st Z.C m s' σ2 := + H.scr.frame (fun a ha hfa => hw.mem_eq ha (fun e => by + have := H.scp.th_out hfa ((H.thT t).2 ht) + simp only [ObjKindA.size] at this + rw [e, hv2] at this; omega)) hkc + have hP2 : ScPlacedA Z.th (L.ksb s) Z.C m σ2 := H.scp.of_kheap hkc + have hst0 : L.mem s' (scA t) = 0#u64 := by rw [hs', L.mem_wr _ _ _ _ hA2 hA2, if_pos rfl] + have N2 := H.toSchedSRN.rebuild2N (σ2 := σ2) hsc ht hw2 ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ rfl rfl rfl hS2 hP2 hTk + (fun u hu => by + by_cases e : u = t + · subst e; refine ⟨_, hk2T, ?_⟩; rw [hst0]; simp [optScA] + · have hne : specPtr u ≠ specPtr t := fun h => e (specPtr_inj h) + obtain ⟨x, h1, h3⟩ := H.sch.tcb u hu + refine ⟨x, by rw [hk2 _ hne]; exact h1, ?_⟩ + rw [show scA u = wa u 3464#u64 from rfl, tK u hu 3464#u64 (by simp) (by simp) (fun h => absurd h e)] + exact h3) + (fun u hu h => by + by_cases e : u = t + · subst e; rw [hst0] at h; exact absurd rfl h + · rw [show scA u = wa u 3464#u64 from rfl, tK u hu 3464#u64 (by simp) (by simp) + (fun h => absurd h e)] at h ⊢ + exact H.rest.scMem u hu h) + hinv + refine ⟨hw, N2, fun u x hx hc => ?_⟩ + intro x' scp y k h1 h2 h3 h4 h5 + by_cases e1 : u = specPtr t + · rw [e1, hk2T] at h1; cases h1; cases h2 + rw [hk2 u e1] at hx h1 + rw [hk2 scp (hpT u x' scp h1 h2)] at h3 + exact H.rdEx u x e1 hx hc x' scp y k h1 h2 h3 h4 h5 + +/-- The state words are kept by a scheduler frame. -/ +theorem donQStEqA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s s' : S} {σ σ' : Fl4SpecA64.State Unit} + (H : SchedSRA st Z m κ s σ) (hs : StEqA Z.T (L.mem s) σ) (F : MFrA (st := st) Z m s s' σ σ') : + StEqA Z.T (L.mem s') σ' := by + intro u hu y hy + obtain ⟨x, hx, -⟩ := H.sch.tcb u hu + obtain ⟨x', hx', o, he⟩ := F.keep.tcb _ _ hx + rw [hy] at hx' + cases hx' + have hT := H.core.tcbs u hu + have hK := tcbLowOffA (K := 3328#u64) H.toSchedSRN hu (by simp) + simp only [← stA_eq] at hK + have hal : Aligned (stA u) := by rw [stA_eq]; exact field_aligned hT (by simp) (by simp) + have hoff : ¬ MWrA (st := st) Z m σ s (stA u) := by + refine not_mwrA (fun h => hK.2 (Or.inr (Or.inr (Or.inr (Or.inr h))))) ?_ hK.1 + intro v hv + have ht := H.core.tcbs v hv + rw [stA_eq] + refine ⟨?_, ?_, ?_, ?_⟩ + · exact field_ne hT ht (K2 := 3464#u64) (by simp) (by simp) (fun _ => by simp) + · exact field_ne hT ht (K2 := 3480#u64) (by simp) (by simp) (fun _ => by simp) + · exact field_ne hT ht (K2 := 3488#u64) (by simp) (by simp) (fun _ => by simp) + · exact field_ne hT ht (K2 := 3592#u64) (by simp) (by simp) (fun _ => by simp) + rw [F.wr.mem_eq hal hoff, he] + exact hs u hu x hx + +end + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +/-- **`schedcontext::donate` of a bound SC, the holder queued or not** (module docstring). -/ +theorem sc_donate_fromQA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} + {σ : Fl4SpecA64.State Unit} (H : SchedSRA st Z m κ s σ) (hst : StEqA Z.T (L.mem s) σ) (a : A) + {sc f d : U64} (hsc : sc ∈ Z.C) (hsc0 : sc ≠ 0#u64) (hf : f ∈ Z.T) (hfs : L.mem s (scA f) = sc) + (hd : d ∈ Z.T) (hds0 : L.mem s (scA d) = 0#u64) (hfd : f ≠ d) (hfon : L.mem s (affA f) = L.cpu s) + (hstall : smp.remote_tcb_stall st ar s a f = ok (s, a)) + (hmig : L.mem s (affA d) = L.mem s (scCoreA sc)) (hni : NotIdleScA m (scNameA m sc)) : + schedcontext.donate st ar s a sc d ⦃ r => r.2 = a ∧ ∃ σ' Q' R', + Fl4SpecA64.schedContextDonate (Z := Unit) (scNameA m sc) (specPtr d) σ = ({((), σ')}, False) ∧ + SchedSRB st { Z with Q := Q', R := R' } m κ r.1 σ' (specPtr d) ∧ MFrA (st := st) Z m s r.1 σ σ' ∧ + (∀ q y k, σ.kheap q = some (Fl4SpecA64.KernelObject.schedContext y k) → ∃ y', σ'.kheap q = some (Fl4SpecA64.KernelObject.schedContext y' k) ∧ y'.scReplies = y.scReplies) ⦄ := by + obtain ⟨xs, ns, hks, hx⟩ := H.scr.sc sc hsc + obtain ⟨xf, hkf, hxf⟩ := H.sch.tcb f hf + obtain ⟨xd, hkd, hxd⟩ := H.sch.tcb d hd + have hTf := H.core.tcbs f hf + have hf0 : f ≠ 0#u64 := fun e => hTf.ne0 (by rw [e]; rfl) + have hf1 : f ≠ 1#u64 := fun e => by have := hTf.align; rw [e] at this; simp at this + have hfd' : specPtr f ≠ specPtr d := fun e => hfd (specPtr_inj e) + have hxfs : xf.tcbSchedContext = some (scNameA m sc) := by + rw [hxf, hfs]; unfold optScA; rw [if_neg hsc0]; rfl + have hyf : xs.scTcb = some (specPtr f) := by + obtain ⟨y, n, hy, hyu⟩ := H.inv.back _ xf _ hkf hxfs + rw [hks] at hy; cases hy; exact hyu + have hwf : L.mem s (scTcbA sc) = f := by + have h := hx.tcb + rw [hyf, ← scTcbA_eqSc] at h + by_cases e : L.mem s (scTcbA sc) = 0#u64 + · rw [e, optRefS_zero] at h; cases h + · rw [optRefS_ne e] at h; exact (specPtr_inj (Option.some.inj h)).symm + have hdom : xf.tcbDomain = 0 := by + obtain ⟨x, h1, h2, -⟩ := H.rest.prio f hf; rw [hkf] at h1; cases h1; exact h2 + -- the dequeue and release removal of `f` + have hdeq := dequeue_refines_localA (st := st) H hf hfon + obtain ⟨sq, hsq, σq, hdq, hkq, hrq, hscq, Hq, W1⟩ := spec_existsA hdeq + set Zq : XSetsA := { Z with Q := deqQA (st := st) s f Z.Q } with hZq + have honq : L.mem sq (affA f) = L.cpu sq := by + rw [W1.wr.2.1, ← hfon] + exact UScalar.eq_of_val_eq (Option.some.inj ((Hq.sch.aff f hf).symm.trans (H.sch.aff f hf))) + obtain ⟨sr, hsr, R', hscr, Hr, W2⟩ := spec_existsA (release_remove_refines_localA (st := st) Hq hf honq) + set Zr : XSetsA := { Zq with R := R' } with hZr + set σr := releaseRemoveStA σq (specPtr f) with hσr + have hkr : σr.kheap = σ.kheap := by rw [hσr]; exact hkq + have hfsr : L.mem sr (scA f) = sc := by rw [hscr f hf, hscq f hf]; exact hfs + have hnr : specPtr f ∉ σr.releaseQueue := releaseRemoveStA_notMem σq _ + have hkfr : σr.kheap (specPtr f) = some (.tcb xf) := by rw [hkr]; exact hkf + have Wr : MFrA (st := st) Z m s sr σ σr := W1.trans W2 rfl rfl + have hstr : StEqA Z.T (L.mem sr) σr := donQStEqA H hst Wr + -- the unbinding of `f` + set s1 := L.wr sr (scA f) 0#u64 with hs1 + set σ1 := unbTStA σr (specPtr f) xf with hσ1 + obtain ⟨hwU, H1⟩ := Hr.unbindTB hsc hf hsc0 hfsr hnr hkfr + have F1 : MFrA (st := st) Zr m sr s1 σr σ1 := Hr.toSchedSRN.unbT_mfr hf hwU hkfr + have hst1 : StEqA Z.T (L.mem s1) σ1 := by + obtain ⟨-, -, tK, -⟩ := Hr.toSchedSRN.donW hsc hf (hwU.mono fun _ h => Or.inr h) + refine hstr.keep (fun u hu => by rw [stA_eq, tK u hu 3328#u64 (by simp) (by simp) (fun _ => by simp)]) + (fun u x hx => ?_) + rw [hσ1, unbTStA_kheap] at hx + by_cases e : u = specPtr f + · rw [if_pos e] at hx; cases hx; exact ⟨xf, by rw [e]; exact hkfr, rfl⟩ + · rw [if_neg e] at hx; exact ⟨x, hx, rfl⟩ + -- the end of the `from` branch + have fin : ∀ (s6 : S) (σR : Fl4SpecA64.State Unit) (Q1 : U64 → U64 → List U64), + SchedSRA st { Zr with Q := Q1 } m κ s6 σR → + Fl4SpecA64.testReschedule (Z := Unit) (specPtr f) σ1 = ({((), σR)}, False) → σR.kheap = σ1.kheap → + MFrA (st := st) Zr m s1 s6 σ1 σR → + (do + let s2 ← obj.Sc.set_tcb st sc s6 d + let s3 ← obj.Tcb.set_sc st d s2 sc + let i1 ← obj.Sc.cpu st sc s3 + smp.migrate_tcb st ar s3 a d i1) ⦃ r => r.2 = a ∧ ∃ σ' Q' R', + Fl4SpecA64.schedContextDonate (Z := Unit) (scNameA m sc) (specPtr d) σ = ({((), σ')}, False) ∧ + SchedSRB st { Z with Q := Q', R := R' } m κ r.1 σ' (specPtr d) ∧ MFrA (st := st) Z m s r.1 σ σ' ∧ + (∀ q y k, σ.kheap q = some (Fl4SpecA64.KernelObject.schedContext y k) → ∃ y', σ'.kheap q = some (Fl4SpecA64.KernelObject.schedContext y' k) ∧ y'.scReplies = y.scReplies) ⦄ := by + intro s6 σR Q1 H6 hR hkR F6 + have hNf : scNameA m sc ≠ specPtr f := fun e => by rw [e, hkf] at hks; cases hks + have hks6 : σR.kheap (scNameA m sc) = some (.schedContext xs ns) := by + rw [hkR, hσ1, unbTStA_kheap, if_neg hNf, hkr]; exact hks + have hkd6 : σR.kheap (specPtr d) = some (.tcb xd) := by + rw [hkR, hσ1, unbTStA_kheap, if_neg (Ne.symm hfd'), hkr]; exact hkd + have hds6 : L.mem s6 (scA d) = 0#u64 := by + obtain ⟨x6, h6, e6⟩ := H6.sch.tcb d hd + rw [hkd6] at h6; cases h6 + rw [hds0] at hxd + by_contra hne + rw [hxd] at e6; unfold optScA at e6; rw [if_neg hne] at e6; simp at e6 + have hfree : ∀ u x, σR.kheap u = some (.tcb x) → x.tcbSchedContext ≠ some (scNameA m sc) := by + intro u x hu hp + rw [hkR, hσ1, unbTStA_kheap] at hu + by_cases e : u = specPtr f + · rw [if_pos e] at hu; cases hu; cases hp + · rw [if_neg e, hkr] at hu + obtain ⟨y, n, hy, hyu⟩ := H.inv.back u x _ hu hp + rw [hks] at hy; cases hy; rw [hyf] at hyu; cases hyu; exact e rfl + have N7 := H6.donateG hsc hd hsc0 hfree hds6 hni hks6 hkd6 + obtain ⟨hA1, hA2, -⟩ := H6.toSchedSRN.donAddr hsc hd + have hw7 : WritesIn st s6 (L.wr (L.wr s6 (scTcbA sc) d) (scA d) sc) (fun b => b = scTcbA sc ∨ b = scA d) := + (writesIn_wr hA1).trans (writesIn_wr hA2) + have hNd : scNameA m sc ≠ specPtr d := fun e => by rw [e, hkd] at hks; cases hks + have hK7 := SKeepA.two (ys := { xs with scTcb := some (specPtr d) }) (o := some (scNameA m sc)) + (fun p => donStA_kheap hNd p) hks6 hkd6 + have F7 : MFrA (st := st) { Zr with Q := Q1 } m s6 (L.wr (L.wr s6 (scTcbA sc) d) (scA d) sc) σR + (donStA σR (scNameA m sc) (specPtr d) xs ns xd) := + ⟨hw7.mono fun _ hb => H6.toSchedSRN.mwr_two hsc hd hb, hK7.1, hK7.2⟩ + refine WP.spec_bind (set_tcb_specSc s6 sc d) ?_ + rintro s2 rfl + refine WP.spec_bind (set_sc_specSc _ d sc) ?_ + rintro s3 rfl + refine WP.spec_bind (obj.Sc.cpu.spec _ sc) ?_ + rintro cpu rfl + have hmig3 : L.mem (L.wr (L.wr s6 (scTcbA sc) d) (scA d) sc) (affA d) = + L.mem (L.wr (L.wr s6 (scTcbA sc) d) (scA d) sc) (scCoreA sc) := by + have e1 := N7.sch.aff d hd + have e2 := H.sch.aff d hd + rw [e2] at e1; replace e1 := Option.some.inj e1 + rw [N7.scCore sc hsc, ← H.scCore sc hsc, ← hmig] + exact (UScalar.eq_of_val_eq e1).symm + unfold smp.migrate_tcb + refine WP.spec_bind (obj.Tcb.affinity.spec _ d) ?_ + rintro i rfl + rw [hmig3] + simp only [bne_self_eq_false, Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + have hnqr : ∀ q, specPtr f ∉ σr.readyQueues 0 q := fun q => by + have := H.toSchedSRN.ready_after_dequeueA hf Hq.toSchedSRN 0 q + rw [hσr]; exact this + refine ⟨by trivial, _, Q1, R', donate_fromQSpecA hks hyf hkf hdom (by rw [hdq]) hkq hnr hR hkR hkd hfd', N7, ?_, ?_⟩ + · exact ((Wr.trans F1 rfl rfl).trans F6 rfl rfl).trans F7 rfl rfl + intro q y k hq + have qd : q ≠ specPtr d := fun e => by rw [e, hkd] at hq; cases hq + have qf : q ≠ specPtr f := fun e => by rw [e, hkf] at hq; cases hq + rw [donStA_kheap hNd q, if_neg qd] + by_cases e : q = scNameA m sc + · rw [if_pos e] + rw [e, hks] at hq + cases hq + exact ⟨_, rfl, rfl⟩ + · rw [if_neg e, hkR, hσ1, unbTStA_kheap, if_neg qf, hkr] + exact ⟨y, hq, rfl⟩ + -- the program + unfold schedcontext.donate + refine WP.spec_bind (obj.Sc.tcb.spec s sc) ?_ + rintro i rfl + rw [hwf] + refine WP.spec_bind (obj.Tcb.is_null.spec _) ?_ + rintro b rfl + rw [decide_eq_false hf0] + simp only [Bool.false_eq_true, ↓reduceIte] + refine WP.spec_bind (Pₘ := fun r => r.2 = a ∧ ∃ σR Q1, SchedSRA st { Zr with Q := Q1 } m κ r.1 σR ∧ + Fl4SpecA64.testReschedule (Z := Unit) (specPtr f) σ1 = ({((), σR)}, False) ∧ σR.kheap = σ1.kheap ∧ + MFrA (st := st) Zr m s1 r.1 σ1 σR) ?_ ?_ + · refine WP.spec_bind (Pₘ := fun r => r = (s, a)) (by rw [hstall]; exact (WP.spec_ok _).2 rfl) ?_ + rintro ⟨s2, a2⟩ e + simp only [Prod.mk.injEq] at e + obtain ⟨e1, e2⟩ := e + subst s2 a2 + refine WP.spec_bind (Pₘ := fun x => x = sq) (by rw [hsq]; exact (WP.spec_ok _).2 rfl) ?_ + intro x hx + subst x + refine WP.spec_bind (Pₘ := fun x => x = sr) (by rw [hsr]; exact (WP.spec_ok _).2 rfl) ?_ + intro x hx + subst x + refine WP.spec_bind (set_sc_specSc sr f 0#u64) ?_ + rintro s5 rfl + refine WP.spec_bind (sched.cur_thread.spec _) ?_ + rintro ct rfl + refine WP.spec_bind (Pₘ := fun b1 => b1 = decide (f = L.curW s1)) + (by unfold obj.Tcb.Insts.CoreCmpPartialEqTcb.eq; exact (WP.spec_ok _).2 rfl) ?_ + rintro b1 rfl + have hct : σ1.curThread = specPtr (L.curW s1) := H1.sch.curThread + have resch : ∀ (_hpos : specPtr f = σ1.curThread ∨ σ1.schedulerAction = .switchThread (specPtr f)), + sched.reschedule_required st s1 ⦃ s6 => ∃ σR Q1, SchedSRA st { Zr with Q := Q1 } m κ s6 σR ∧ + Fl4SpecA64.testReschedule (Z := Unit) (specPtr f) σ1 = ({((), σR)}, False) ∧ σR.kheap = σ1.kheap ∧ + MFrA (st := st) Zr m s1 s6 σ1 σR ⦄ := fun hpos => + WP.spec_mono (reschedule_required_refinesA H1 hst1) fun _ ⟨σR, Q1, h1, hk1, _, hw1, H6⟩ => + ⟨σR, Q1, H6, (testReschedule_posA hpos).trans h1, hk1, MFrA.of_sw hk1 hw1⟩ + by_cases ec : f = L.curW s1 + · rw [decide_eq_true ec] + simp only [↓reduceIte] + exact WP.spec_bind (resch (Or.inl (by rw [hct, ← ec]))) fun _ h => (WP.spec_ok _).2 ⟨rfl, h⟩ + · rw [decide_eq_false ec] + simp only [Bool.false_eq_true, ↓reduceIte] + refine WP.spec_bind (Pₘ := fun t1 => ∃ σR Q1, SchedSRA st { Zr with Q := Q1 } m κ t1 σR ∧ + Fl4SpecA64.testReschedule (Z := Unit) (specPtr f) σ1 = ({((), σR)}, False) ∧ σR.kheap = σ1.kheap ∧ + MFrA (st := st) Zr m s1 t1 σ1 σR) ?_ fun _ h => (WP.spec_ok _).2 ⟨rfl, h⟩ + refine WP.spec_bind (sched.action.spec _) ?_ + rintro i1 rfl + by_cases ea : f = L.actionW s1 + · rw [if_pos ea] + refine resch (Or.inr ?_) + rw [H1.sch.act, ← ea]; unfold actOfA; rw [if_neg hf0, if_neg hf1] + · rw [if_neg ea] + exact (WP.spec_ok _).2 ⟨σ1, Zr.Q, H1, testReschedule_negA (fun e => ec (specPtr_inj (e.trans hct))) + (fun e => ea (specPtr_inj (actOfA_switchA (H1.sch.act.symm.trans e)).2.2)), rfl, MFrA.refl Zr m s1 σ1⟩ + · rintro ⟨s6, a6⟩ ⟨ea6, σR, Q1, H6, hR, hkR, F6⟩ + obtain rfl : a6 = a := ea6 + exact fin s6 σR Q1 H6 hR hkR F6 + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScDonRefA.lean b/proofs/Fl4ProofsA64/P3/ScDonRefA.lean new file mode 100644 index 000000000..5a6abd13a --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScDonRefA.lean @@ -0,0 +1,101 @@ +/- +P3 (aarch64, A64-P3-MCS gen 5): **the donation step of `maybe_donate_sched_context`, local case** (ruling 23:14Z +gen-5 item 3, first part; x86 `P3/ScDonateRef`'s `donSt` / `donate_refines`, the program and spec computations). + +* **`donate_localA`**: FL4's `schedcontext::donate sc t` of an SC bound to no TCB (`SC.TCB = 0`) writes the SC's + `TCB` word and `t`'s `SC` word and nothing else when `t`'s affinity is the SC's core after the writes (`hmig`: + `migrate_tcb` is the zero-side-effect no-op of `smp.rs:14–15`, ruling 23:14Z item 1). +* **`donStA`** / **`donate_specA`** (x86 `donSt`, the spec half of `donate_refines`): l4v `sched_context_donate` of + an SC bound to no TCB has the single outcome binding the SC and the thread. +* `threadSet_eqSc`, `set_sc_specSc`, `set_tcb_specSc`: accessors. + +The relation after the donation and `sched_context_resume` (x86 `ScResume`, `ScRelease`'s `postpone`) is gen 6: +`SchedSRA.rd` does not hold between the two steps (the donated SC may be unreleased until `resume` postpones it). + +No named premise. Hypotheses: `htw0` (the case selector: the SC is bound to no TCB), `hmig` (the local case). +-/ +import Fl4ProofsA64.P3.ScTailA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +section Spec +open Fl4SpecA64 +variable {σ : State Unit} + +theorem threadSet_eqSc {t : ObjRef} {x : Tcb} (f : Tcb → Tcb) (hk : σ.kheap t = some (.tcb x)) : + threadSet (Z := Unit) f t σ = ({((), updKSc σ t (.tcb (f x)))}, False) := by + unfold threadSet + rw [nd_bind_oneSc (show Fl4Spec.NonDet.getsThe (getTcb (Z := Unit) t) σ = ({(x, σ)}, False) by + simp [Fl4Spec.NonDet.getsThe, getTcb, readObject, hk, Fl4Spec.NonDet.assertOpt]; rfl)] + exact setObject_eqSc hk rfl + +/-- The spec's state after `sched_context_donate` of an SC bound to no TCB (x86 `donSt`). -/ +def donStA (σ : State Unit) (sc t : ObjRef) (xs : SchedContext) (ns : Nat) (xt : Tcb) : State Unit := + updKSc (updKSc σ sc (.schedContext { xs with scTcb := some t } ns)) t (.tcb { xt with tcbSchedContext := some sc }) + +/-- **`sched_context_donate` of an SC bound to no TCB** (x86 `donate_refines`, spec half). -/ +theorem donate_specA {sc t : ObjRef} {xs : SchedContext} {ns : Nat} {xt : Tcb} + (hks : σ.kheap sc = some (.schedContext xs ns)) (hkt : σ.kheap t = some (.tcb xt)) (h0 : xs.scTcb = none) : + schedContextDonate (Z := Unit) sc t σ = ({((), donStA σ sc t xs ns xt)}, False) := by + have hst : sc ≠ t := by intro e; rw [e, hkt] at hks; cases hks + have hg : getScObjRef (Z := Unit) SchedContext.scTcb sc σ = ({(xs.scTcb, σ)}, False) := by + unfold getScObjRef; rw [nd_bind_oneSc (getSchedContext_eqA hks)]; rfl + have hkt1 : (updKSc σ sc (.schedContext { xs with scTcb := some t } ns)).kheap t = some (.tcb xt) := by + simp [updKSc, Ne.symm hst, hkt] + unfold schedContextDonate + rw [nd_bind_oneSc hg, h0] + simp only [Fl4Spec.NonDet.when, ne_eq, not_true_eq_false, ↓reduceIte] + rw [nd_bind_oneSc (show Fl4Spec.NonDet.pure (σ := State Unit) () σ = ({((), σ)}, False) from rfl)] + show (updateSchedContext sc _ >>= _) σ = _ + rw [nd_bind_oneSc (updateSchedContext_eqA hks _)] + rw [show scTcbUpdate (fun _ => some t) xs = { xs with scTcb := some t } from rfl] + show threadSet _ t _ = _ + rw [threadSet_eqSc _ hkt1] + rfl + +end Spec + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +theorem set_sc_specSc (s : S) (t : obj.Tcb) (v : U64) : + obj.Tcb.set_sc st t s v ⦃ s' => s' = L.wr s (scA t) v ⦄ := by + unfold obj.Tcb.set_sc; step*; simp_all [scA] + +theorem set_tcb_specSc (s : S) (sc : obj.Sc) (v : U64) : + obj.Sc.set_tcb st sc s v ⦃ s' => s' = L.wr s (scTcbA sc) v ⦄ := by + unfold obj.Sc.set_tcb; step*; simp_all [scTcbA] + +/-- **`donate` of an SC bound to no TCB, local case** (module docstring; x86 `donate_refines`, program half). -/ +theorem donate_localA (s : S) (a : A) (sc t : U64) (htw0 : L.mem s (scTcbA sc) = 0#u64) + (hmig : L.mem (L.wr (L.wr s (scTcbA sc) t) (scA t) sc) (affA t) = + L.mem (L.wr (L.wr s (scTcbA sc) t) (scA t) sc) (scCoreA sc)) : + schedcontext.donate st ar s a sc t ⦃ r => r = (L.wr (L.wr s (scTcbA sc) t) (scA t) sc, a) ⦄ := by + unfold schedcontext.donate + refine WP.spec_bind (obj.Sc.tcb.spec s sc) ?_ + rintro i rfl + rw [htw0] + refine WP.spec_bind (obj.Tcb.is_null.spec _) ?_ + rintro b rfl + simp only [eq_self_iff_true, decide_true, ↓reduceIte, bind_ok] + refine WP.spec_bind (set_tcb_specSc s sc t) ?_ + rintro s2 rfl + refine WP.spec_bind (set_sc_specSc _ t sc) ?_ + rintro s3 rfl + refine WP.spec_bind (obj.Sc.cpu.spec _ sc) ?_ + rintro cpu rfl + unfold smp.migrate_tcb + refine WP.spec_bind (obj.Tcb.affinity.spec _ t) ?_ + rintro i rfl + rw [hmig] + simp only [bne_self_eq_false, Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScEnqA.lean b/proofs/Fl4ProofsA64/P3/ScEnqA.lean new file mode 100644 index 000000000..bf965d4e7 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScEnqA.lean @@ -0,0 +1,240 @@ +/- +P3 (aarch64, A64-P3-MCS gen 3): **`enqueue` refines `tcb_sched_action tcb_sched_enqueue`** over `SchedSRA`, on the +current core and on another (x86 `P3/SchedQueue`'s `tcbSchedAction_eq` / `SchedRel.qstep` / `enqueue_refines`, +ported to every core). + +* **`tcbSchedAction_eqA`** (x86 `tcbSchedAction_eq`) and `setRqA` (x86 `setRq`): the spec's `tcb_sched_action f t` of + a TCB of domain 0 sets its ready queue `(0, prio)` to `f t q`. +* **`SchedSRA.qfoot`** (x86 `SchedRel.qstep`'s frame): an FL4 step writing inside a queue footprint `QFoot` (one + core's queue words, the `Sched` links of `W`, `t`'s `IN_QUEUES`, the current core's IPI words) with the spec heap, + scalars, action and release queue kept keeps `SchedSRA` with the new lists, given the new `SchedCoreA` and the + spec's ready queues of the current core. +* **`enqueue_refines_localA`** (x86 `enqueue_refines`): for `t` on the current core (`aff t = cpu`), FL4's `enqueue t` + has the spec's single outcome of `tcb_sched_action tcb_sched_enqueue t`, and `SchedSRA` holds after with the lists + `enqQA`. +* **`enqueue_remoteA`** (new): for `t` on another core, FL4 enqueues `t` on that core's queue (`Z.Q (aff t)`, a list + outside the current core's view) and `SchedSRA` holds with the spec state unchanged: the view of the current core + does not see another core's ready queues (`SchedRestA.rq` reads `Z.Q (L.cpu s)` only). + +Hypotheses in the x86 style: `ht` (`t ∈ Z.T`), `hr` (seL4's precondition: queued or not released), `hrun` (the +thread is runnable: l4v `valid_ready_qs`, which `RunQA` keeps), `hon` / `hoff` (the core case). No named premise. +-/ +import Fl4ProofsA64.P3.ScStableA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## The spec's `tcb_sched_action` -/ + +section Spec +open Fl4SpecA64 + +/-- The spec's state with ready queue `(0, p)` set to `q` (l4v `set_tcb_queue`; x86 `setRq`). -/ +def setRqA (σ : State Unit) (p : Priority) (q : List ObjRef) : State Unit := + { σ with readyQueues := fun d' p' => if d' = 0 ∧ p' = p then q else σ.readyQueues d' p' } + +/-- `tcb_sched_action` of a TCB of domain 0 (x86 `tcbSchedAction_eq`). -/ +theorem tcbSchedAction_eqA {σ : State Unit} {t : ObjRef} {x : Tcb} (hk : σ.kheap t = some (.tcb x)) + (hd : x.tcbDomain = 0) (f : ObjRef → List ObjRef → List ObjRef) : + tcbSchedAction (Z := Unit) f t σ = + ({((), setRqA σ x.tcbPriority (f t (σ.readyQueues 0 x.tcbPriority)))}, False) := by + unfold tcbSchedAction + rw [nd_bind_oneSc (threadGet_eqSc _ hk), nd_bind_oneSc (threadGet_eqSc _ hk)] + unfold getTcbQueue + rw [nd_bind_oneSc (nd_bind_oneSc (show (Fl4Spec.NonDet.gets fun s : State Unit => s.readyQueues) σ = + ({(σ.readyQueues, σ)}, False) from rfl) _), nd_pure_bindSc] + simp only [hd] + rfl + +theorem ofNat_prio_eqA {p : U64} {x : Tcb} (h : p.val = x.tcbPriority.toNat) : + BitVec.ofNat 8 p.val = x.tcbPriority := by + apply BitVec.eq_of_toNat_eq + simp only [BitVec.toNat_ofNat, h] + exact Nat.mod_eq_of_lt x.tcbPriority.isLt + +theorem ofNat_prio_injA {p q : U64} (hp : p.val < 256) (hq : q.val < 256) + (h : BitVec.ofNat 8 p.val = BitVec.ofNat 8 q.val) : p = q := by + apply UScalar.eq_of_val_eq + have := congrArg BitVec.toNat h + simp only [BitVec.toNat_ofNat] at this + rw [Nat.mod_eq_of_lt (by omega), Nat.mod_eq_of_lt (by omega)] at this + exact this + +/-- `set_tcb_queue` of the queue it holds changes nothing. -/ +theorem setRqA_self (σ : State Unit) (p : Priority) : setRqA σ p (σ.readyQueues 0 p) = σ := by + unfold setRqA + have : (fun d' p' => if d' = 0 ∧ p' = p then σ.readyQueues 0 p else σ.readyQueues d' p') = σ.readyQueues := by + funext d' p' + by_cases e : d' = 0 ∧ p' = p + · rw [if_pos e, e.1, e.2] + · rw [if_neg e] + rw [this] + +end Spec + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-! ## Writes inside a queue footprint -/ + +/-- **`SchedSRA` after a step writing inside a queue footprint** (module docstring; x86 `SchedRel.qstep`'s frame). -/ +theorem SchedSRN.qfoot {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s s' : S} {σ σ' : Fl4SpecA64.State Unit} + (H : SchedSRN st Z m κ s σ) {W : List U64} {c t : U64} (hW : ∀ w ∈ W, w ∈ Z.T) (hc : c ∈ Z.Cs) (ht : t ∈ Z.T) + (hw : WritesIn st s s' (L.QFoot s W c (136 ≤ ·) t)) {Q' : U64 → U64 → List U64} + (hk : σ'.kheap = σ.kheap) (hfix : SFixA σ σ') (hact : σ'.schedulerAction = σ.schedulerAction) + (hcore : SchedCoreA st Z.T Z.Cs s' Q' Z.R) + (hrq : ∀ p : U64, p.val < 256 → σ'.readyQueues 0 (BitVec.ofNat 8 p.val) = (Q' (L.cpu s) p).map specPtr) + (hrqd : ∀ d p, d ≠ 0 → σ'.readyQueues d p = []) + (hrel : σ'.releaseQueue = σ.releaseQueue) (hrun : RunQA σ') : + SchedSRN st { Z with Q := Q' } m κ s' σ' := by + have hT := H.core.tcbs + have hcur := H.sch.cpuCs + have hN := H.core.nodes c hc + have hN' := H.core.nodes _ hcur + obtain ⟨-, -, out⟩ := ext_qfoot hT hW hN hN' ht hc hcur hw + obtain ⟨n, -, hn, hcs⟩ := H.csN + have hcl : ∀ c ∈ Z.Cs, c.val < 128 := fun c hc => by have := (hcs c).1 hc; omega + have hf := H.ksFit + have hkb := H.ksb_al + have nodeIn : ∀ c' ∈ Z.Cs, (L.ksb s).val + 223488 ≤ L.nodeBase s c' ∧ + L.nodeBase s c' + 4288 ≤ (L.ksb s).val + ksSizeA := fun c' hc' => by + have := hcl c' hc'; unfold LawfulStore.nodeBase ksSizeA; omega + have nK : ∀ c' ∈ Z.Cs, ∀ o : U64, o.val < 120 → o.val % 8 = 0 → + L.mem s' (L.nodeAt s c' o) = L.mem s (L.nodeAt s c' o) := by + intro c' hc' o ho h8 + have := node_qfoot hT hW hN hN' ht hw (H.core.nodes c' hc') (o := o) (by omega) h8 + (fun _ => show ¬ 136 ≤ o.val by omega) (fun _ => by omega) + rwa [nodeAt_of_ksb hw.1] at this + have tK : ∀ y ∈ Z.T, ∀ K : U64, (K = 3520#u64 ∨ K = 3448#u64 ∨ K = 3432#u64 ∨ K = 3464#u64) → + L.mem s' (wa y K) = L.mem s (wa y K) := by + intro y hy K hK + have hK' : K.val = 3520 ∨ K.val = 3448 ∨ K.val = 3432 ∨ K.val = 3464 := by + rcases hK with rfl | rfl | rfl | rfl <;> simp + exact field_qfoot hT hW hN hN' ht hw hy (by omega) (by omega) (by omega) (fun _ => by omega) + refine H.rebuildQueues hw.1 hw.2.1 (fun c' hc' o h1 h2 _ => nK c' hc' o h1 h2) (fun K hK => ?_) tK + (fun a ha hfa => out a ⟨ha, fun y hy => ?_, fun c' hc' => ?_⟩) + (fun c' _ => by rw [hk]) (SchedSRA.tcb_keep hk) hfix ?_ + (fun u hu => H.actOn u (by rw [← hact]; exact hu)) + (ScInvA.of_kheap H.inv hk hrel) hcore hrq hrqd hrel hrun + · have hK' : K.val = 8 ∨ K.val = 16 := by rcases hK with rfl | rfl <;> simp + have hv : (wa (L.ksb s) K).val = (L.ksb s).val + K.val := by rw [wa_valm]; unfold ksSizeA at hf; omega + refine out _ ⟨by unfold Aligned; rw [hv]; omega, fun y hy => ?_, fun c' hc' => ?_⟩ + · have := H.tcb_ks hy; rw [hv]; unfold ksSizeA at this; omega + · have := nodeIn c' hc'; rw [hv]; omega + · have := H.scp.th_out hfa ((H.thT y).2 hy); simpa [ObjKindA.size] using this + · have := H.scp.ks_out hfa; have := nodeIn c' hc'; omega + · rw [hact, nK _ hcur layout.node.SCHED_ACTION (by simp [layout.node.SCHED_ACTION]) + (by simp [layout.node.SCHED_ACTION])] + exact H.sch.act + +theorem SchedSRA.qfoot {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s s' : S} {σ σ' : Fl4SpecA64.State Unit} + (H : SchedSRA st Z m κ s σ) {W : List U64} {c t : U64} (hW : ∀ w ∈ W, w ∈ Z.T) (hc : c ∈ Z.Cs) (ht : t ∈ Z.T) + (hw : WritesIn st s s' (L.QFoot s W c (136 ≤ ·) t)) {Q' : U64 → U64 → List U64} + (hk : σ'.kheap = σ.kheap) (hfix : SFixA σ σ') (hact : σ'.schedulerAction = σ.schedulerAction) + (hcore : SchedCoreA st Z.T Z.Cs s' Q' Z.R) + (hrq : ∀ p : U64, p.val < 256 → σ'.readyQueues 0 (BitVec.ofNat 8 p.val) = (Q' (L.cpu s) p).map specPtr) + (hrqd : ∀ d p, d ≠ 0 → σ'.readyQueues d p = []) + (hrel : σ'.releaseQueue = σ.releaseQueue) (hrun : RunQA σ') : + SchedSRA st { Z with Q := Q' } m κ s' σ' := + ⟨H.toSchedSRN.qfoot hW hc ht hw hk hfix hact hcore hrq hrqd hrel hrun, + RdA.of_eq H.rd hk hfix.curTime hrel⟩ + +/-! ## `enqueue` -/ + +/-- **`enqueue` on the current core refines `tcb_sched_action tcb_sched_enqueue`** (x86 `enqueue_refines`). -/ +theorem enqueue_refines_localA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} {σ : Fl4SpecA64.State Unit} + (H : SchedSRA st Z m κ s σ) (t : U64) (ht : t ∈ Z.T) (hon : L.mem s (affA t) = L.cpu s) + (hr : (L.mem s (inqA t) &&& sched.Q_QUEUED) = 0#u64 → (L.mem s (inqA t) &&& sched.Q_RELEASE) = 0#u64) + (hrun : ∀ x, σ.kheap (specPtr t) = some (.tcb x) → Fl4SpecA64.runnable x.tcbState = true) : + sched.enqueue st s t ⦃ s' => ∃ σ', Fl4SpecA64.tcbSchedAction (Z := Unit) Fl4SpecA64.tcbSchedEnqueue (specPtr t) σ = + ({((), σ')}, False) ∧ σ'.kheap = σ.kheap ∧ σ'.releaseQueue = σ.releaseQueue ∧ + σ'.schedulerAction = σ.schedulerAction ∧ + WritesIn st s s' (L.QFootN s (t :: Z.Q (L.mem s (affA t)) (L.mem s (prioA t))) (L.mem s (affA t)) (136 ≤ ·) t) ∧ + SchedSRA st { Z with Q := enqQA st s t Z.Q } m κ s' σ' ⦄ := by + have hc := H.core.aff t ht + have hp := H.core.prio t ht + obtain ⟨x, hk, hd, -, hpr⟩ := H.rest.prio t ht + have hW : ∀ w ∈ t :: Z.Q (L.mem s (affA t)) (L.mem s (prioA t)), w ∈ Z.T := by + intro w hw'; rcases List.mem_cons.1 hw' with rfl | hw' + · exact ht + · exact H.core.memT hc hp hw' + refine WP.spec_mono (sched.enqueue.coreA s t H.core H.sch.cpuCs ht hr) ?_ + rintro s' ⟨hcore, hwS, -⟩ + have hw := hwS.mono fun _ h => h.toQFoot + have hspec := tcbSchedAction_eqA hk hd Fl4SpecA64.tcbSchedEnqueue + have hpe := ofNat_prio_eqA hpr + have hq0 := H.rest.rq _ hp + rw [hpe] at hq0 + refine ⟨_, hspec, rfl, rfl, rfl, hwS.qfootN_of hon, ?_⟩ + by_cases hqd : (L.mem s (inqA t) &&& sched.Q_QUEUED) ≠ 0#u64 + · have htq := (H.core.queued t ht).1 hqd + have e : Fl4SpecA64.tcbSchedEnqueue (specPtr t) (σ.readyQueues 0 x.tcbPriority) = σ.readyQueues 0 x.tcbPriority := by + unfold Fl4SpecA64.tcbSchedEnqueue + rw [if_neg (by rw [not_not, hq0, ← hon, mem_map_specPtrSc]; exact htq)] + rw [e, setRqA_self] + have eQ : enqQA st s t Z.Q = Z.Q := by unfold enqQA; rw [if_pos hqd] + rw [eQ] at hcore ⊢ + exact H.qfoot hW hc ht hw rfl (SFixA.refl σ) rfl hcore H.rest.rq H.rest.rqDom rfl H.run + · have hq0' : (L.mem s (inqA t) &&& sched.Q_QUEUED) = 0#u64 := not_not.1 hqd + have htq := H.core.notQ ht hq0' hc hp + have e : Fl4SpecA64.tcbSchedEnqueue (specPtr t) (σ.readyQueues 0 x.tcbPriority) = + specPtr t :: σ.readyQueues 0 x.tcbPriority := by + unfold Fl4SpecA64.tcbSchedEnqueue + rw [if_pos (by rw [hq0, ← hon, mem_map_specPtrSc]; exact htq)] + rw [e] + have eQ : enqQA st s t Z.Q = Function.update Z.Q (L.mem s (affA t)) (Function.update (Z.Q (L.mem s (affA t))) + (L.mem s (prioA t)) (t :: Z.Q (L.mem s (affA t)) (L.mem s (prioA t)))) := by + unfold enqQA; rw [if_neg hqd] + rw [eQ] at hcore ⊢ + refine H.qfoot hW hc ht hw rfl ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ rfl hcore (fun p hp' => ?_) + (fun d p hd' => ?_) rfl ⟨fun d p y hy => ?_, H.run.2⟩ + · show (if (0 : Fl4SpecA64.Domain) = 0 ∧ BitVec.ofNat 8 p.val = x.tcbPriority then _ else _) = _ + rw [hon, Function.update_self] + by_cases ep : p = L.mem s (prioA t) + · subst ep + rw [if_pos ⟨rfl, hpe⟩, Function.update_self, List.map_cons, hq0] + · rw [if_neg (fun h => ep (ofNat_prio_injA hp' hp (h.2.trans hpe.symm))), Function.update_of_ne ep] + exact H.rest.rq p hp' + · show (if d = 0 ∧ p = x.tcbPriority then _ else _) = _ + rw [if_neg (fun h => hd' h.1)]; exact H.rest.rqDom d p hd' + · change y ∈ (if d = 0 ∧ p = x.tcbPriority then _ else _) at hy + split_ifs at hy with hdp + · rcases List.mem_cons.1 hy with rfl | hy + · exact ⟨x, hk, hrun x hk⟩ + · exact H.run.1 0 x.tcbPriority y hy + · exact H.run.1 d p y hy + +/-- **`enqueue` on another core** (module docstring): FL4 enqueues on that core's queue; the current core's view is +unchanged. -/ +theorem enqueue_remoteA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} {σ : Fl4SpecA64.State Unit} + (H : SchedSRA st Z m κ s σ) (t : U64) (ht : t ∈ Z.T) (hoff : L.mem s (affA t) ≠ L.cpu s) + (hr : (L.mem s (inqA t) &&& sched.Q_QUEUED) = 0#u64 → (L.mem s (inqA t) &&& sched.Q_RELEASE) = 0#u64) : + sched.enqueue st s t ⦃ s' => + WritesIn st s s' (L.QFootS s (t :: Z.Q (L.mem s (affA t)) (L.mem s (prioA t))) (L.mem s (affA t)) (136 ≤ ·) t) ∧ + SchedSRA st { Z with Q := enqQA st s t Z.Q } m κ s' σ ⦄ := by + have hc := H.core.aff t ht + have hp := H.core.prio t ht + have hW : ∀ w ∈ t :: Z.Q (L.mem s (affA t)) (L.mem s (prioA t)), w ∈ Z.T := by + intro w hw'; rcases List.mem_cons.1 hw' with rfl | hw' + · exact ht + · exact H.core.memT hc hp hw' + refine WP.spec_mono (sched.enqueue.coreA s t H.core H.sch.cpuCs ht hr) ?_ + rintro s' ⟨hcore, hwS, -⟩ + have hw := hwS.mono fun _ h => h.toQFoot + have eQ : ∀ p, enqQA st s t Z.Q (L.cpu s) p = Z.Q (L.cpu s) p := fun p => by + unfold enqQA; split_ifs + · rfl + · rw [Function.update_of_ne (Ne.symm hoff)] + exact ⟨hwS, H.qfoot hW hc ht hw rfl (SFixA.refl σ) rfl hcore (fun p hp' => by rw [eQ]; exact H.rest.rq p hp') + H.rest.rqDom rfl H.run⟩ + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScEntryBudgetA.lean b/proofs/Fl4ProofsA64/P3/ScEntryBudgetA.lean new file mode 100644 index 000000000..4fa992cef --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScEntryBudgetA.lean @@ -0,0 +1,804 @@ +import Fl4ProofsA64.P3.ScEntryEndA +import Fl4ProofsA64.P3.ScTimeA +import Fl4ProofsA64.P3.ScYieldRunA + +/-! AArch64 entry budget checks after the raw timestamp. The clock, +platform minimum and release limit are [P] agreements. Current +placement, queue exclusion and the timeout IPC boundary are [J] facts. +The action can be choose-new after exhaustion even while the outgoing +thread is still current. -/ + +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3.Sched + +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +theorem entryActiveA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRN st Z m κ s σ) (I : CurInvA σ) (hw : m.WF fl4A64Platform.numNodes) + {x : SchedContext} {n : Nat} + (hc : σ.kheap (scNameA m (L.cscW s)) = some (.schedContext x n)) : scActive x = true := by + have hcur := H.currentScA + by_cases hni : σ.curSc ≠ idleScPtr + · exact I.active hni _ _ (by rwa [hcur]) + · have hid : specPtr (L.cscW s) = m.idleScOf m.curCore := by + by_contra e + have he := not_not.1 hni + rw [hcur, scNameA, if_neg e] at he + exact H.sch.scOff (L.cscW s) H.cscC e he + obtain ⟨y, j, hy, R⟩ := H.scr.sc _ H.cscC + change σ.kheap (scNameA m (L.cscW s)) = _ at hy + rw [hc] at hy; cases hy + have e := R.idle (IdleScA.of_view hw hid) + unfold scActive activeSc + rw [R.max] at e + simp [e, Nat.pos_of_ne_zero e] + +/-- The concrete budget decision reads the current node's usage and +compares the saturating head capacity with the FL4 minimum. -/ +theorem entryBudgetDecisionA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRN st Z m κ s σ) (I : CurInvA σ) (hw : m.WF fl4A64Platform.numNodes) + (a : A) (min : Fl4MinBudgetAgreeA ar a) : + (do + let sc ← sched.cur_sc st s + let c ← sched.consumed st s + sporadic.sufficient st ar s a sc c) ⦃ b => + fl4GetScRefillSufficient (Z := Unit) σ.curSc σ.consumedTime σ = ({(b, σ)}, False) ∧ + (b = true → OffReadyA σ → OffOKA σ) ⦄ := by + obtain ⟨x, n, hk, R⟩ := H.scr.sc (L.cscW s) H.cscC + have ha := entryActiveA H I hw hk + have hne := H.inv.ne hk ha + refine WP.spec_bind (sched.cur_sc.spec s) ?_ + rintro sc rfl + refine WP.spec_bind (sched.consumed.specA s) ?_ + rintro used rfl + refine WP.spec_mono (fl4_sufficient_refinesA a H.scr H.cscC hk ha _ min) ?_ + intro b h + change fl4RefillSufficient (L.mem s (L.nodeAt s (L.cpu s) layout.node.CONSUMED)).bv (refillHd x) = b at h + refine ⟨?_, ?_⟩ + · rw [H.currentScA, H.rest.consumed, fl4GetScRefillSufficient_eqA hk hne, h] + · intro hb hR + refine hR.ok (fun y k hy => ?_) + rw [H.currentScA, hk] at hy; cases hy + have e : fl4RefillSufficient σ.consumedTime (refillHd x) = true := by + rw [H.rest.consumed, h, hb] + exact of_decide_eq_true e + +private theorem entryRescheduleA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : YieldQueueRelA (st := st) Z m κ s σ) : + (do + let s' ← sched.reschedule_required st s + sched.set_reprogram st s') ⦃ s' => ∃ τ Q', + (do + rescheduleRequired (Z := Unit) + NonDet.modify fun v : State Unit => { v with reprogramTimer := true }) σ = ({((), τ)}, False) ∧ + YieldQueueRelA (st := st) { Z with Q := Q' } m κ s' τ ∧ + τ.kheap = σ.kheap ∧ τ.releaseQueue = σ.releaseQueue ∧ τ.idleThread = σ.idleThread ∧ + τ.machineState = σ.machineState ∧ τ.curTime = σ.curTime ∧ τ.consumedTime = σ.consumedTime ∧ + τ.schedulerAction = .chooseNewThread ⦄ := by + obtain ⟨v, he, fix, hkv, hrv, ha, hm⟩ := rescheduleResultA H.sched + refine WP.spec_bind (yieldRescheduleA H) ?_ + rintro s' ⟨τ, Q', hτ, _, _, H', _⟩ + have e : τ = v := (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 + (congrArg Prod.fst (hτ.symm.trans he)))).2 + subst τ + refine WP.spec_mono (sched.set_reprogram.spec s') ?_ + rintro s'' rfl + let τ := { v with reprogramTimer := true } + have out : SchedSRA st { Z with Q := Q' } m κ (L.wr s' (L.nodeA s' layout.node.REPROGRAM) 1#u64) τ := + ⟨H'.sched.toSchedSRN.set_repr, H'.sched.rd.of_eq rfl rfl rfl⟩ + have w : WritesIn st s' (L.wr s' (L.nodeA s' layout.node.REPROGRAM) 1#u64) + (MWrA (st := st) { Z with Q := Q' } m v s') := + (writesIn_wr (v := 1#u64) H'.sched.toSchedSRN.reprA.1).mono + (fun _ h => Or.inr (h ▸ H'.sched.toSchedSRN.repr_swr)) + obtain ⟨E, M, Y⟩ := yieldQueueFrameA H'.sched H'.states H'.mcp H'.links (show τ.kheap = v.kheap from rfl) w + refine ⟨τ, Q', ?_, ⟨out, E, M, Y⟩, hkv, hrv, fix.idleThread, hm, fix.curTime, fix.consumedTime, ha⟩ + rw [nd_bind_oneSc he] + rfl + +/-- `entryRescheduleA` with the frame of `reschedule_required` and the REPROGRAM word. -/ +theorem entryRescheduleFrA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : YieldQueueRelA (st := st) Z m κ s σ) : + (do + let s' ← sched.reschedule_required st s + sched.set_reprogram st s') ⦃ s' => ∃ τ Q', + (do + rescheduleRequired (Z := Unit) + NonDet.modify fun v : State Unit => { v with reprogramTimer := true }) σ = ({((), τ)}, False) ∧ + YieldQueueRelA (st := st) { Z with Q := Q' } m κ s' τ ∧ + τ.kheap = σ.kheap ∧ τ.releaseQueue = σ.releaseQueue ∧ τ.idleThread = σ.idleThread ∧ + τ.machineState = σ.machineState ∧ τ.curTime = σ.curTime ∧ τ.consumedTime = σ.consumedTime ∧ + τ.schedulerAction = .chooseNewThread ∧ MFrA (st := st) Z m s s' σ τ ⦄ := by + obtain ⟨v, he, fix, hkv, hrv, ha, hm⟩ := rescheduleResultA H.sched + refine WP.spec_bind (yieldRescheduleA H) ?_ + rintro s' ⟨τ, Q', hτ, _, _, H', f⟩ + have e : τ = v := (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 + (congrArg Prod.fst (hτ.symm.trans he)))).2 + subst τ + refine WP.spec_mono (sched.set_reprogram.spec s') ?_ + rintro s'' rfl + let τ := { v with reprogramTimer := true } + have out : SchedSRA st { Z with Q := Q' } m κ (L.wr s' (L.nodeA s' layout.node.REPROGRAM) 1#u64) τ := + ⟨H'.sched.toSchedSRN.set_repr, H'.sched.rd.of_eq rfl rfl rfl⟩ + have w : WritesIn st s' (L.wr s' (L.nodeA s' layout.node.REPROGRAM) 1#u64) + (MWrA (st := st) { Z with Q := Q' } m v s') := + (writesIn_wr (v := 1#u64) H'.sched.toSchedSRN.reprA.1).mono + (fun _ h => Or.inr (h ▸ H'.sched.toSchedSRN.repr_swr)) + obtain ⟨E, M, Y⟩ := yieldQueueFrameA H'.sched H'.states H'.mcp H'.links (show τ.kheap = v.kheap from rfl) w + refine ⟨τ, Q', ?_, ⟨out, E, M, Y⟩, hkv, hrv, fix.idleThread, hm, fix.curTime, fix.consumedTime, ha, + f.trans (Z' := { Z with Q := Q' }) ⟨w, SKeepA.of_kheap rfl, SSzA.of_kheap rfl⟩ rfl rfl⟩ + rw [nd_bind_oneSc he] + rfl + +theorem entryScFrameA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s s' : S} {σ τ : State Unit} (H : YieldQueueRelA (st := st) Z m κ s σ) + {sc : U64} (hc : sc ∈ Z.C) {x y : SchedContext} {n : Nat} + (hk : σ.kheap (scNameA m sc) = some (.schedContext x n)) + (hK : τ.kheap = (updKSc σ (scNameA m sc) (.schedContext y n)).kheap) + (hY : y.scYieldFrom = x.scYieldFrom) (w : MFrA (st := st) Z m s s' σ τ) : + StEqA Z.T (L.mem s') τ ∧ McpRelA Z.T m (L.mem s') τ ∧ YieldRelA Z.T Z.C m (L.mem s') τ := by + have keep (u : ObjRef) (t : Tcb) : τ.kheap u = some (.tcb t) ↔ σ.kheap u = some (.tcb t) := by + rw [hK] + exact cfgTcbKeepA hk u t + refine ⟨cfgKeepStatesA H.sched.toSchedSRN H.states w (fun u t => (keep u t).1), ?_⟩ + have word {t K : U64} (h : t ∈ Z.T) (hK' : K = 3440#u64 ∨ K = 3472#u64) := + yieldWordKeepA H.sched.toSchedSRN H.sched.curT (w.wr.mono fun _ e => Or.inl e) h (Or.inr hK') + exact yieldFrameA H.mcp H.links + (fun t ht => ⟨word ht (Or.inl rfl), + yieldFlagsKeepA H.sched.toSchedSRN H.sched.curT (w.wr.mono fun _ e => Or.inl e) ht⟩) + (fun t ht => word ht (Or.inr rfl)) + (fun t _ z hz => ⟨z, (keep _ _).2 hz, rfl, rfl, rfl⟩) (by + intro c hC + obtain ⟨z, j, hz, _⟩ := H.sched.scr.sc c hC + by_cases e : scNameA m c = scNameA m sc + · rw [e, hk] at hz; cases hz + exact ⟨x, n, y, e.symm ▸ hk, by rw [hK, e, updKSc_same], hY⟩ + · exact ⟨z, j, z, hz, by rwa [hK, updKSc_kheap, Function.update_of_ne e], rfl⟩) + +theorem entryCurInvA {σ : State Unit} (I : CurInvA σ) {p : ObjRef} {x y : SchedContext} {n : Nat} + (hcur : σ.curSc = p) (hk : σ.kheap p = some (.schedContext x n)) (ha : scActive y = true) : + CurInvA { updKSc σ p (.schedContext y n) with consumedTime := 0 } := by + obtain ⟨t, ht, hb, hi⟩ := I.tcb + have h := updKSc_same σ p (.schedContext y n) + refine ⟨⟨t, (cfgTcbKeepA hk _ _).2 ht, hb, hi⟩, ⟨y, n, hcur.symm ▸ h⟩, ?_, I.notRel⟩ + intro _ z j hz + change (updKSc σ p (.schedContext y n)).kheap σ.curSc = _ at hz + rw [hcur, h] at hz; cases hz + exact ha + +private theorem entryActiveIdleA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRA st Z m κ s σ) (I : ActIdleA σ) {p : ObjRef} {x y : SchedContext} {n : Nat} + (hk : σ.kheap p = some (.schedContext x n)) (ha : scActive y = scActive x) : + ActIdleA { updKSc σ p (.schedContext y n) with consumedTime := 0 } := by + let τ := { updKSc σ p (.schedContext y n) with consumedTime := 0 } + have keep (u : ObjRef) (t : Tcb) : τ.kheap u = some (.tcb t) ↔ σ.kheap u = some (.tcb t) := cfgTcbKeepA hk u t + obtain ⟨idle, hIdle, hB, hState, hR⟩ := I.2 + refine ⟨?_, ⟨idle, (keep _ _).2 hIdle, hB, hState, hR⟩⟩ + intro u hAct + have hs := I.1 u hAct + obtain ⟨t, q, ht, hb, _, _⟩ := H.actReady u hAct hs + obtain ⟨z, j, hz, _⟩ := H.inv.back u t q ht hb + have run := schedulable_partsA hs + have act : scActive z = true := by + unfold schedulable at hs + simp only [decide_eq_true_eq] at hs + simpa [tcbsOfKh, scsOfKh, optPred, optMap, tcbOf, scOf, ht, hb, hz] using hs.2.1 + by_cases e : q = p + · rw [e, hk] at hz; cases hz + have hnew : τ.kheap q = some (.schedContext y n) := by + rw [e] + exact updKSc_same _ _ _ + exact cfgSchedulableA ((keep _ _).2 ht) hb hnew + (run.1 _ ht) (ha.trans act) run.2 + · have old : τ.kheap q = some (.schedContext z j) := by + change (updKSc σ p (.schedContext y n)).kheap q = _ + rwa [updKSc_kheap, Function.update_of_ne e] + exact cfgSchedulableA ((keep _ _).2 ht) hb old (run.1 _ ht) act run.2 + +/-- [J] The current SC belongs to this core. The current TCB is outside +the ready queues at a budget entry boundary. -/ +structure EntryBudgetPlaceA (st : machine.Store S) [L : LawfulStore st] (m : McState) (κ : U64 → U64) (s : S) (σ : State Unit) : Prop where + cores : m.WF fl4A64Platform.numNodes + scCore : κ (L.cscW s) = L.cpu s + notIdle : σ.curSc ≠ idleScPtr → NotIdleScA m σ.curSc + notReady : ∀ d p, σ.curThread ∉ σ.readyQueues d p + +/-- [J] Obtain the timeout leaf after the SC and scheduler-word writes +of the prefix. The CSpace slots and the typed TCB/SC placements have the +frame needed by the timeout proof. -/ +def EntryTimeoutFromA (Z : XSetsA) (m : McState) (κ : U64 → U64) + (Clock : A → MachineState → Prop) (s : S) (σ : State Unit) : Prop := + ∀ s' a' τ, MFrA (st := st) Z m s s' σ τ → + SchedSRB st Z m κ s' τ τ.curThread → StEqA Z.T (L.mem s') τ → + McpRelA Z.T m (L.mem s') τ → YieldRelA Z.T Z.C m (L.mem s') τ → + Clock a' τ.machineState → τ.curThread = σ.curThread → + schedulable τ.curThread τ = true → + EntryTimeoutA (st := st) (ar := ar) Z m κ Clock s' a' τ (L.curW s') + +/-- [J] **The validity read of the timeout leaf, from the prefix** (`EntryTimeoutFromA` without the `send` field): what +the `can = false` / `NoTimeoutA` consumers read; discharged from the record (`P3/Stage3EntryValidA`). -/ +def EntryValidFromA (Z : XSetsA) (m : McState) (κ : U64 → U64) + (Clock : A → MachineState → Prop) (s : S) (σ : State Unit) : Prop := + ∀ s' a' τ, MFrA (st := st) Z m s s' σ τ → + SchedSRB st Z m κ s' τ τ.curThread → StEqA Z.T (L.mem s') τ → + McpRelA Z.T m (L.mem s') τ → YieldRelA Z.T Z.C m (L.mem s') τ → + Clock a' τ.machineState → τ.curThread = σ.curThread → + schedulable τ.curThread τ = true → + EntryValidA (st := st) s' τ (L.curW s') + +theorem EntryTimeoutFromA.toValid {Z : XSetsA} {m : McState} {κ : U64 → U64} {Clock : A → MachineState → Prop} + {s : S} {σ : State Unit} (T : EntryTimeoutFromA (st := st) (ar := ar) Z m κ Clock s σ) : + EntryValidFromA (st := st) Z m κ Clock s σ := + fun s' a' τ F H E M Y c h sch => (T s' a' τ F H E M Y c h sch).valid + +set_option maxRecDepth 4096 in +set_option maxHeartbeats 1300000 in +/-- Budget exhaustion settles the current SC and resets the local +accumulator. It then ends the timeslice only for a schedulable current +thread, requests a new selection, and marks the timer for reprogramming. -/ +theorem charge_budget_entry_refinesA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (E : YieldQueueRelA (st := st) Z m κ s σ) (I : CurInvA σ) (Idle : ActIdleA σ) + (P : EntryBudgetPlaceA st m κ s σ) (a : A) {Clock : A → MachineState → Prop} + (clk : Clock a σ.machineState) (T : EntryTimeoutFromA (st := st) (ar := ar) Z m κ Clock s σ) + (hag : fl4MinBudget = MIN_BUDGET) (hMin : Fl4MinBudgetAgreeA ar a) + (hLimit : sporadic.max_release_time ar a ⦃ v => v.bv = fl4MaxReleaseTime ⦄) + (used : U64) (can : Bool) : + sched.charge_budget st ar s a used can ⦃ r => ∃ τ Q' R', + fl4ChargeBudget (Z := Unit) used.bv can σ = ({((), τ)}, False) ∧ + YieldQueueRelA (st := st) { Z with Q := Q', R := R' } m κ r.1 τ ∧ + Clock r.2 τ.machineState ∧ τ.curTime = σ.curTime ∧ τ.machineState = σ.machineState ∧ + τ.consumedTime = 0 ∧ ActIdleA τ ∧ (τ.schedulerAction = .chooseNewThread ∨ + CurInvA τ ∧ ∀ t, τ.kheap τ.curThread = some (.tcb t) → runnable t.tcbState ≠ true) ⦄ := by + let H := E.sched + let sc := L.cscW s + let p := scNameA m sc + have hsc : sc ∈ Z.C := H.cscC + have hcur : σ.curSc = p := H.currentScA + obtain ⟨x, n, hk, R⟩ := H.scr.sc sc hsc + have ha := entryActiveA H.toSchedSRN I P.cores hk + have hid : m.idleScOf m.curCore = specPtr (L.mem s (L.nodeA s layout.node.IDLE_SC)) := by + rw [H.sch.core] + exact H.sch.idleScOf _ H.sch.cpuCs + have hb : decide (sc ≠ L.mem s (L.nodeA s layout.node.IDLE_SC)) = decide (σ.curSc ≠ idleScPtr) := by + rw [hcur] + exact idle_testA hid (H.sch.scOff sc hsc) + unfold sched.charge_budget + refine WP.spec_bind (sched.cur_sc.spec s) ?_ + intro c hc + rw [show c = sc from hc] + rw [eq_of_spec_fd (sched.idle_sc.specA (st := st) s), bind_ok, sc_ne_eqA, bind_ok] + refine WP.spec_bind (entryChargeHead_refinesA H I a hag hLimit hMin hsc rfl hcur hk P.notIdle used _ hb) ?_ + rintro s1 ⟨y, hHead, H1, hOwn, hAct, hY, w1⟩ + let σ1 := updKSc σ p (.schedContext y n) + change SchedSRB st Z m κ s1 σ1 σ.curThread at H1 + refine WP.spec_bind (sched.set_consumed.spec s1 0#u64) ?_ + rintro s2 rfl + let τ := { σ1 with consumedTime := 0 } + let s2 := L.wr s1 (L.nodeA s1 layout.node.CONSUMED) 0#u64 + obtain ⟨H2, w2⟩ := H1.clearConsumedA + change SchedSRB st Z m κ s2 τ τ.curThread at H2 + have frame1 : MFrA (st := st) Z m s s1 σ σ1 := cfgScFrameA (st := st) hsc hk w1 + have frame2 : MFrA (st := st) Z m s1 s2 σ1 τ := + MFrA.of_sw rfl (w2.mono fun _ e => Or.inl (e ▸ H1.node_sks H1.sch.cpuCs (by + unfold layout.node.CONSUMED; decide))) + have frame : MFrA (st := st) Z m s s2 σ τ := frame1.trans frame2 rfl rfl + obtain ⟨E2, M2, Y2⟩ := entryScFrameA E hsc hk (show τ.kheap = σ1.kheap from rfl) hY frame + have h2 : τ.kheap p = some (.schedContext y n) := updKSc_same _ _ _ + have A2 := entryActiveIdleA H Idle hk hAct + have I2 := entryCurInvA I hcur hk (hAct.trans ha) + obtain ⟨t, hkt, hB, hIPC⟩ := I2.tcb + let ct := L.curW s2 + have hCt : τ.curThread = specPtr ct := H2.sch.curThread + have hkt' : τ.kheap (specPtr ct) = some (.tcb t) := by rwa [← hCt] + have hR := (H2.relOn H2.curT H2.curOn).2 (hCt ▸ I2.notRel) + refine WP.spec_bind (sched.cur_thread.spec s2) ?_ + intro c hc + rw [show c = ct from hc, is_schedulable_eqA, schedulable_eqA H2.toSchedSRN H2.curT + (H2.relOn H2.curT H2.curOn) E2, bind_ok] + by_cases hRun : schedulable (specPtr ct) τ = true + · rw [if_pos hRun] + have hcs : L.cscW s2 = sc := by + apply scRenA_inj m (H2.sch.scOff _ H2.cscC) (H.sch.scOff _ hsc) + exact H2.currentScA.symm.trans hcur + have hon : L.mem s2 (scCoreA sc) = L.cpu s2 := by + rw [H2.scCore sc (hcs ▸ H2.cscC), P.scCore] + exact UScalar.eq_of_val_eq (H.sch.core.symm.trans H2.sch.core) + have hq : (L.mem s2 (inqA ct) &&& sched.Q_QUEUED) = 0#u64 := by + by_contra hn + have q := (H2.core.queued ct H2.curT).1 hn + rw [H2.curOn] at q + obtain ⟨z, hz, _, _, hp⟩ := H2.rest.prio ct H2.curT + have ee := ofNat_prio_eqA hp + have e := H2.rest.rq _ (H2.core.prio _ H2.curT) + rw [ee] at e + exact P.notReady 0 z.tcbPriority (by + change τ.curThread ∈ τ.readyQueues 0 z.tcbPriority + rw [hCt, e, mem_map_specPtrSc] + exact q) + have hBack : t.tcbSchedContext = some p := hcur ▸ hB + have own : L.mem s2 (scTcbA sc) = ct := by + obtain ⟨z, j, hz, rel⟩ := H2.scr.sc sc hsc + change τ.kheap p = _ at hz + rw [h2] at hz; cases hz + obtain ⟨z, j, hz, ho⟩ := H2.inv.back _ _ _ hkt' hBack + rw [h2] at hz; cases hz + rw [rel.tcb, ← scTcbA_eqSc] at ho + have he : L.mem s2 (scTcbA sc) ≠ 0#u64 := by + intro e; rw [e, optRefS_zero] at ho; cases ho + rw [optRefS_ne he] at ho + exact specPtr_inj (Option.some.inj ho) + obtain ⟨t', eT, hWord⟩ := H2.sch.tcb ct H2.curT + rw [hkt'] at eT; cases eT + have hz : L.mem s2 (scA ct) ≠ 0#u64 := by + intro e + rw [hBack, e, optScA, if_pos rfl] at hWord + cases hWord + have hWordS : L.mem s2 (scA ct) = sc := by + apply scRenA_inj m (H2.sch.scOff _ (H2.rest.scMem ct H2.curT hz)) (H.sch.scOff _ hsc) + rw [hBack, optScA, if_neg hz] at hWord + exact (Option.some.inj hWord).symm + have sc0 : sc ≠ 0#u64 := hWordS ▸ hz + have T2 := T s2 a τ frame H2 E2 M2 Y2 clk rfl (hCt.symm ▸ hRun) + refine WP.spec_bind (end_timeslice_refinesA (σ := τ) (hCt ▸ H2) a clk A2 E2 M2 Y2 H2.curT hsc sc0 rfl hcs + hcur H2.curOn hon own hWordS hq (fun d p => hCt ▸ P.notReady d p) + hkt' hBack hRun h2 (hAct.trans ha) hag hMin T2 can) ?_ + rintro ⟨s3, a3⟩ ⟨σ3, Q3, R3, he, H3, span⟩ + simp only [uncurry_apply_pair] + rw [← cfgResultAssocA] + refine WP.spec_bind (entryRescheduleA H3) ?_ + rintro s4 ⟨σ4, Q4, he', H4, hk4, hr4, hi4, hm4, ht4, hc4, ha4⟩ + rw [WP.spec_ok] + have hIdle : ActIdleA σ4 := by + refine ⟨(fun u hu => by rw [ha4] at hu; cases hu), ?_⟩ + simpa only [hk4, hr4, hi4] using span.idle + refine ⟨σ4, Q4, R3, ?_, H4, hm4.symm ▸ span.witness, ht4.trans span.time, + hm4.trans span.machine, hc4.trans span.consumed, hIdle, Or.inl ha4⟩ + unfold fl4ChargeBudget + rw [nd_gets_bindSc, nd_bind_oneSc hHead] + rw [nd_bind_oneSc (show NonDet.modify (fun s : State Unit => { s with consumedTime := 0 }) σ1 = + ({((), τ)}, False) from rfl)] + repeat rw [nd_gets_bindSc] + rw [NonDet.when, if_pos (hCt.symm ▸ hRun)] + rw [nd_bind_oneSc he] + exact he' + · rw [if_neg hRun, WP.spec_ok] + have nonRun : runnable t.tcbState ≠ true := by + intro r + exact hRun (cfgSchedulableA hkt' (hcur ▸ hB) h2 r (hAct.trans ha) (hCt ▸ I2.notRel)) + have H3 : SchedSRA st Z m κ s2 τ := ⟨H2.toSchedSRN, fun u z hz hClass => by + by_cases e : u = τ.curThread + · rw [e, hkt] at hz; cases hz + rcases hClass with h | ⟨q, h⟩ | ⟨ep, d, h, _⟩ | ⟨ep, r, d, h⟩ + · exact (nonRun h).elim + all_goals rw [h] at hIPC; cases hIPC + · exact H2.rdEx u z e hz hClass⟩ + refine ⟨τ, Z.Q, Z.R, ?_, ⟨H3, E2, M2, Y2⟩, clk, rfl, rfl, rfl, A2, Or.inr ⟨I2, ?_⟩⟩ + swap + · intro z hz + rw [hkt] at hz; cases hz + exact nonRun + unfold fl4ChargeBudget + rw [nd_gets_bindSc, nd_bind_oneSc hHead] + rw [nd_bind_oneSc (show NonDet.modify (fun s : State Unit => { s with consumedTime := 0 }) σ1 = + ({((), τ)}, False) from rfl)] + repeat rw [nd_gets_bindSc] + rw [NonDet.when, if_neg (fun h => hRun (hCt ▸ h))] + rfl + +set_option maxRecDepth 4096 in +set_option maxHeartbeats 1300000 in +/-- **`charge_budget_entry_refinesA` at `can = false` with the frame** (the partial-invocation successor of +`mcs_preemption_point`): the same post plus `MFrA Z m s r.1 σ τ`. The timeout leaf `T` is read for validity only. -/ +theorem charge_budget_falseFrA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (E : YieldQueueRelA (st := st) Z m κ s σ) (I : CurInvA σ) (Idle : ActIdleA σ) + (P : EntryBudgetPlaceA st m κ s σ) (a : A) {Clock : A → MachineState → Prop} + (clk : Clock a σ.machineState) (T : EntryValidFromA (st := st) Z m κ Clock s σ) + (hag : fl4MinBudget = MIN_BUDGET) (hMin : Fl4MinBudgetAgreeA ar a) + (hLimit : sporadic.max_release_time ar a ⦃ v => v.bv = fl4MaxReleaseTime ⦄) + (used : U64) : + sched.charge_budget st ar s a used false ⦃ r => ∃ τ Q' R', + fl4ChargeBudget (Z := Unit) used.bv false σ = ({((), τ)}, False) ∧ + YieldQueueRelA (st := st) { Z with Q := Q', R := R' } m κ r.1 τ ∧ + Clock r.2 τ.machineState ∧ τ.curTime = σ.curTime ∧ τ.machineState = σ.machineState ∧ + τ.consumedTime = 0 ∧ ActIdleA τ ∧ (τ.schedulerAction = .chooseNewThread ∨ + CurInvA τ ∧ ∀ t, τ.kheap τ.curThread = some (.tcb t) → runnable t.tcbState ≠ true) ∧ + MFrA (st := st) Z m s r.1 σ τ ⦄ := by + let H := E.sched + let sc := L.cscW s + let p := scNameA m sc + have hsc : sc ∈ Z.C := H.cscC + have hcur : σ.curSc = p := H.currentScA + obtain ⟨x, n, hk, R⟩ := H.scr.sc sc hsc + have ha := entryActiveA H.toSchedSRN I P.cores hk + have hid : m.idleScOf m.curCore = specPtr (L.mem s (L.nodeA s layout.node.IDLE_SC)) := by + rw [H.sch.core] + exact H.sch.idleScOf _ H.sch.cpuCs + have hb : decide (sc ≠ L.mem s (L.nodeA s layout.node.IDLE_SC)) = decide (σ.curSc ≠ idleScPtr) := by + rw [hcur] + exact idle_testA hid (H.sch.scOff sc hsc) + unfold sched.charge_budget + refine WP.spec_bind (sched.cur_sc.spec s) ?_ + intro c hc + rw [show c = sc from hc] + rw [eq_of_spec_fd (sched.idle_sc.specA (st := st) s), bind_ok, sc_ne_eqA, bind_ok] + refine WP.spec_bind (entryChargeHead_refinesA H I a hag hLimit hMin hsc rfl hcur hk P.notIdle used _ hb) ?_ + rintro s1 ⟨y, hHead, H1, hOwn, hAct, hY, w1⟩ + let σ1 := updKSc σ p (.schedContext y n) + change SchedSRB st Z m κ s1 σ1 σ.curThread at H1 + refine WP.spec_bind (sched.set_consumed.spec s1 0#u64) ?_ + rintro s2 rfl + let τ := { σ1 with consumedTime := 0 } + let s2 := L.wr s1 (L.nodeA s1 layout.node.CONSUMED) 0#u64 + obtain ⟨H2, w2⟩ := H1.clearConsumedA + change SchedSRB st Z m κ s2 τ τ.curThread at H2 + have frame1 : MFrA (st := st) Z m s s1 σ σ1 := cfgScFrameA (st := st) hsc hk w1 + have frame2 : MFrA (st := st) Z m s1 s2 σ1 τ := + MFrA.of_sw rfl (w2.mono fun _ e => Or.inl (e ▸ H1.node_sks H1.sch.cpuCs (by + unfold layout.node.CONSUMED; decide))) + have frame : MFrA (st := st) Z m s s2 σ τ := frame1.trans frame2 rfl rfl + obtain ⟨E2, M2, Y2⟩ := entryScFrameA E hsc hk (show τ.kheap = σ1.kheap from rfl) hY frame + have h2 : τ.kheap p = some (.schedContext y n) := updKSc_same _ _ _ + have A2 := entryActiveIdleA H Idle hk hAct + have I2 := entryCurInvA I hcur hk (hAct.trans ha) + obtain ⟨t, hkt, hB, hIPC⟩ := I2.tcb + let ct := L.curW s2 + have hCt : τ.curThread = specPtr ct := H2.sch.curThread + have hkt' : τ.kheap (specPtr ct) = some (.tcb t) := by rwa [← hCt] + have hR := (H2.relOn H2.curT H2.curOn).2 (hCt ▸ I2.notRel) + refine WP.spec_bind (sched.cur_thread.spec s2) ?_ + intro c hc + rw [show c = ct from hc, is_schedulable_eqA, schedulable_eqA H2.toSchedSRN H2.curT + (H2.relOn H2.curT H2.curOn) E2, bind_ok] + by_cases hRun : schedulable (specPtr ct) τ = true + · rw [if_pos hRun] + have hcs : L.cscW s2 = sc := by + apply scRenA_inj m (H2.sch.scOff _ H2.cscC) (H.sch.scOff _ hsc) + exact H2.currentScA.symm.trans hcur + have hon : L.mem s2 (scCoreA sc) = L.cpu s2 := by + rw [H2.scCore sc (hcs ▸ H2.cscC), P.scCore] + exact UScalar.eq_of_val_eq (H.sch.core.symm.trans H2.sch.core) + have hq : (L.mem s2 (inqA ct) &&& sched.Q_QUEUED) = 0#u64 := by + by_contra hn + have q := (H2.core.queued ct H2.curT).1 hn + rw [H2.curOn] at q + obtain ⟨z, hz, _, _, hp⟩ := H2.rest.prio ct H2.curT + have ee := ofNat_prio_eqA hp + have e := H2.rest.rq _ (H2.core.prio _ H2.curT) + rw [ee] at e + exact P.notReady 0 z.tcbPriority (by + change τ.curThread ∈ τ.readyQueues 0 z.tcbPriority + rw [hCt, e, mem_map_specPtrSc] + exact q) + have hBack : t.tcbSchedContext = some p := hcur ▸ hB + have own : L.mem s2 (scTcbA sc) = ct := by + obtain ⟨z, j, hz, rel⟩ := H2.scr.sc sc hsc + change τ.kheap p = _ at hz + rw [h2] at hz; cases hz + obtain ⟨z, j, hz, ho⟩ := H2.inv.back _ _ _ hkt' hBack + rw [h2] at hz; cases hz + rw [rel.tcb, ← scTcbA_eqSc] at ho + have he : L.mem s2 (scTcbA sc) ≠ 0#u64 := by + intro e; rw [e, optRefS_zero] at ho; cases ho + rw [optRefS_ne he] at ho + exact specPtr_inj (Option.some.inj ho) + obtain ⟨t', eT, hWord⟩ := H2.sch.tcb ct H2.curT + rw [hkt'] at eT; cases eT + have hz : L.mem s2 (scA ct) ≠ 0#u64 := by + intro e + rw [hBack, e, optScA, if_pos rfl] at hWord + cases hWord + have hWordS : L.mem s2 (scA ct) = sc := by + apply scRenA_inj m (H2.sch.scOff _ (H2.rest.scMem ct H2.curT hz)) (H.sch.scOff _ hsc) + rw [hBack, optScA, if_neg hz] at hWord + exact (Option.some.inj hWord).symm + have sc0 : sc ≠ 0#u64 := hWordS ▸ hz + have T2 := T s2 a τ frame H2 E2 M2 Y2 clk rfl (hCt.symm ▸ hRun) + refine WP.spec_bind (end_timeslice_noSendA (σ := τ) (hCt ▸ H2) a clk A2 E2 M2 Y2 H2.curT hsc sc0 rfl hcs + hcur H2.curOn hon own hWordS hq (fun d p => hCt ▸ P.notReady d p) + hkt' hBack hRun h2 (hAct.trans ha) hag hMin T2) ?_ + rintro ⟨s3, a3⟩ ⟨σ3, Q3, R3, he, H3, span, fr⟩ + simp only [uncurry_apply_pair] + rw [← cfgResultAssocA] + refine WP.spec_bind (entryRescheduleFrA H3) ?_ + rintro s4 ⟨σ4, Q4, he', H4, hk4, hr4, hi4, hm4, ht4, hc4, ha4, f4⟩ + rw [WP.spec_ok] + have hIdle : ActIdleA σ4 := by + refine ⟨(fun u hu => by rw [ha4] at hu; cases hu), ?_⟩ + simpa only [hk4, hr4, hi4] using span.idle + refine ⟨σ4, Q4, R3, ?_, H4, hm4.symm ▸ span.witness, ht4.trans span.time, + hm4.trans span.machine, hc4.trans span.consumed, hIdle, Or.inl ha4, + (frame.trans fr rfl rfl).trans f4 rfl rfl⟩ + unfold fl4ChargeBudget + rw [nd_gets_bindSc, nd_bind_oneSc hHead] + rw [nd_bind_oneSc (show NonDet.modify (fun s : State Unit => { s with consumedTime := 0 }) σ1 = + ({((), τ)}, False) from rfl)] + repeat rw [nd_gets_bindSc] + rw [NonDet.when, if_pos (hCt.symm ▸ hRun)] + rw [nd_bind_oneSc he] + exact he' + · rw [if_neg hRun, WP.spec_ok] + have nonRun : runnable t.tcbState ≠ true := by + intro r + exact hRun (cfgSchedulableA hkt' (hcur ▸ hB) h2 r (hAct.trans ha) (hCt ▸ I2.notRel)) + have H3 : SchedSRA st Z m κ s2 τ := ⟨H2.toSchedSRN, fun u z hz hClass => by + by_cases e : u = τ.curThread + · rw [e, hkt] at hz; cases hz + rcases hClass with h | ⟨q, h⟩ | ⟨ep, d, h, _⟩ | ⟨ep, r, d, h⟩ + · exact (nonRun h).elim + all_goals rw [h] at hIPC; cases hIPC + · exact H2.rdEx u z e hz hClass⟩ + refine ⟨τ, Z.Q, Z.R, ?_, ⟨H3, E2, M2, Y2⟩, clk, rfl, rfl, rfl, A2, Or.inr ⟨I2, ?_⟩, frame⟩ + swap + · intro z hz + rw [hkt] at hz; cases hz + exact nonRun + unfold fl4ChargeBudget + rw [nd_gets_bindSc, nd_bind_oneSc hHead] + rw [nd_bind_oneSc (show NonDet.modify (fun s : State Unit => { s with consumedTime := 0 }) σ1 = + ({((), τ)}, False) from rfl)] + repeat rw [nd_gets_bindSc] + rw [NonDet.when, if_neg (fun h => hRun (hCt ▸ h))] + rfl + + +/-- `rescheduleRequired` then the timer flag (the tail of `fl4ChargeBudget`'s schedulable branch). -/ +def reschedReprogA : SMonad Unit Unit := do + rescheduleRequired + NonDet.modify fun v : State Unit => { v with reprogramTimer := true } + +/-- The spec steps of a budget charge without the timeout send: the head charge and the consumed reset, then for a +schedulable current thread the requeue (`entryRequeueA`) and `rescheduleRequired` with the timer flag. -/ +def ChargeStepsA (σ τ : State Unit) : Prop := + ∃ y n, (∀ z k, σ.kheap σ.curSc = some (.schedContext z k) → y.scTcb = z.scTcb) ∧ ((schedulable σ.curThread { updKSc σ σ.curSc (.schedContext y n) with consumedTime := 0 } = true ∧ + ∃ σ3, entryRequeueA σ.curSc σ.curThread { updKSc σ σ.curSc (.schedContext y n) with consumedTime := 0 } = + ({((), σ3)}, False) ∧ + reschedReprogA σ3 = ({((), τ)}, False)) ∨ + (schedulable σ.curThread { updKSc σ σ.curSc (.schedContext y n) with consumedTime := 0 } = false ∧ + τ = { updKSc σ σ.curSc (.schedContext y n) with consumedTime := 0 })) ∧ + ((∀ z k, σ.kheap idleScPtr = some (.schedContext z k) → (refillHd z).rTime ≤ σ.curTime) → + ∀ z k, (updKSc σ σ.curSc (.schedContext y n)).kheap idleScPtr = some (.schedContext z k) → + (refillHd z).rTime ≤ σ.curTime) + +/-- The SC fields and TCBs a budget charge keeps (only the charged SC's refills and consumed time change). -/ +def ScChargeKeepA (σ τ : State Unit) : Prop := + (∀ q z k, σ.kheap q = some (.schedContext z k) → ∃ z', τ.kheap q = some (.schedContext z' k) ∧ + z'.scReplies = z.scReplies ∧ z'.scYieldFrom = z.scYieldFrom ∧ z'.scTcb = z.scTcb ∧ + z'.scRefillMax = z.scRefillMax ∧ (scActive z = true → z.scRefills ≠ [] → z'.scRefills ≠ [])) ∧ + (∀ q x, σ.kheap q = some (.tcb x) → τ.kheap q = some (.tcb x)) + +theorem scChargeKeep_updA {σ : State Unit} {p : ObjRef} {x y : SchedContext} {n : Nat} + (hk : σ.kheap p = some (.schedContext x n)) (h1 : y.scReplies = x.scReplies) + (h2 : y.scYieldFrom = x.scYieldFrom) (h3 : y.scTcb = x.scTcb) (h4 : y.scRefillMax = x.scRefillMax) + (h5 : scActive x = true → y.scRefills ≠ []) {τ : State Unit} (hτ : τ.kheap = (updKSc σ p (.schedContext y n)).kheap) : ScChargeKeepA σ τ := by + refine ⟨fun q z k hz => ?_, fun q z hz => ?_⟩ + · by_cases e : q = p + · subst e + rw [hk] at hz; cases hz + exact ⟨y, by rw [hτ]; exact updKSc_same _ _ _, h1, h2, h3, h4, fun hA _ => h5 hA⟩ + · exact ⟨z, by rw [hτ, updKSc_kheap, Function.update_of_ne e]; exact hz, rfl, rfl, rfl, rfl, fun _ h => h⟩ + · have e : q ≠ p := fun e => by subst e; rw [hk] at hz; cases hz + rw [hτ, updKSc_kheap, Function.update_of_ne e]; exact hz + +/-- The spec's timeout send is not taken by `fl4ChargeBudget used true` at `σ`: after the head charge, the current +thread is not schedulable, or the current SC is round robin, the current thread runs a VM, or its timeout handler is +not valid (`NoSendAtA`). -/ +def NoTimeoutA (used : Ticks) (σ : State Unit) : Prop := + ∀ σ1, fl4ChargeHead (Z := Unit) σ.curSc used σ = ({((), σ1)}, False) → + schedulable σ.curThread { σ1 with consumedTime := 0 } = true → + ∀ x y n, σ1.kheap σ.curThread = some (.tcb x) → σ1.kheap σ.curSc = some (.schedContext y n) → + NoSendAtA { σ1 with consumedTime := 0 } σ.curThread y x + +set_option maxRecDepth 4096 in +set_option maxHeartbeats 1600000 in +/-- **`charge_budget` without the timeout send, with the frame** (`charge_budget_falseFrA` at any `can`, `can = true` +when the spec's send condition fails, `NoTimeoutA`): both spec outcomes (`can`, `false`) agree, and the SC fields the +record reads are kept (`ScChargeKeepA`). -/ +theorem charge_budget_noSendFrA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (E : YieldQueueRelA (st := st) Z m κ s σ) (I : CurInvA σ) (Idle : ActIdleA σ) + (P : EntryBudgetPlaceA st m κ s σ) (a : A) {Clock : A → MachineState → Prop} + (clk : Clock a σ.machineState) (T : EntryValidFromA (st := st) Z m κ Clock s σ) + (hag : fl4MinBudget = MIN_BUDGET) (hMin : Fl4MinBudgetAgreeA ar a) + (hLimit : sporadic.max_release_time ar a ⦃ v => v.bv = fl4MaxReleaseTime ⦄) + (used : U64) (can : Bool) (hns : can = true → NoTimeoutA used.bv σ) : + sched.charge_budget st ar s a used can ⦃ r => ∃ τ Q' R', + fl4ChargeBudget (Z := Unit) used.bv can σ = ({((), τ)}, False) ∧ + fl4ChargeBudget (Z := Unit) used.bv false σ = ({((), τ)}, False) ∧ + YieldQueueRelA (st := st) { Z with Q := Q', R := R' } m κ r.1 τ ∧ + Clock r.2 τ.machineState ∧ τ.curTime = σ.curTime ∧ τ.machineState = σ.machineState ∧ + τ.consumedTime = 0 ∧ ActIdleA τ ∧ (τ.schedulerAction = .chooseNewThread ∨ + CurInvA τ ∧ ∀ t, τ.kheap τ.curThread = some (.tcb t) → runnable t.tcbState ≠ true) ∧ + MFrA (st := st) Z m s r.1 σ τ ∧ ScChargeKeepA σ τ ∧ ChargeStepsA σ τ ∧ r.2 = a ⦄ := by + let H := E.sched + let sc := L.cscW s + let p := scNameA m sc + have hsc : sc ∈ Z.C := H.cscC + have hcur : σ.curSc = p := H.currentScA + obtain ⟨x, n, hk, R⟩ := H.scr.sc sc hsc + have ha := entryActiveA H.toSchedSRN I P.cores hk + have hid : m.idleScOf m.curCore = specPtr (L.mem s (L.nodeA s layout.node.IDLE_SC)) := by + rw [H.sch.core] + exact H.sch.idleScOf _ H.sch.cpuCs + have hb : decide (sc ≠ L.mem s (L.nodeA s layout.node.IDLE_SC)) = decide (σ.curSc ≠ idleScPtr) := by + rw [hcur] + exact idle_testA hid (H.sch.scOff sc hsc) + unfold sched.charge_budget + refine WP.spec_bind (sched.cur_sc.spec s) ?_ + intro c hc + rw [show c = sc from hc] + rw [eq_of_spec_fd (sched.idle_sc.specA (st := st) s), bind_ok, sc_ne_eqA, bind_ok] + refine WP.spec_bind (entryChargeHeadEqA H I a hag hLimit hMin hsc rfl hcur hk P.notIdle used _ hb) ?_ + rintro s1 ⟨y, hHead, H1, hOwn, hAct, hY, hRp, hMx, w1⟩ + have hNe : scActive x = true → y.scRefills ≠ [] := fun hA => by + obtain ⟨z0, n0, hz0, R1⟩ := H1.scr.sc sc hsc + change (updKSc σ p (.schedContext y n)).kheap (scNameA m sc) = _ at hz0 + rw [updKSc_same] at hz0; cases hz0 + have hx := R1.strong (hAct.trans hA) + have ne := refills_neL hx.wf + simpa [hx.refills] using ne + have hTcbY : ∀ z k, σ.kheap σ.curSc = some (.schedContext z k) → y.scTcb = z.scTcb := fun z k hz => by + rw [hcur] at hz + have e := hz.symm.trans hk + cases e; exact hOwn + have KS : ScChargeKeepA σ (updKSc σ p (.schedContext y n)) := + scChargeKeep_updA hk hRp hY hOwn hMx hNe rfl + have hRdy : (∀ z k, σ.kheap idleScPtr = some (.schedContext z k) → (refillHd z).rTime ≤ σ.curTime) → + ∀ z k, (updKSc σ σ.curSc (.schedContext y n)).kheap idleScPtr = some (.schedContext z k) → + (refillHd z).rTime ≤ σ.curTime := fun g z k hz => by + by_cases e : σ.curSc = idleScPtr + · have q := hHead + rw [e] at q + unfold fl4ChargeHead NonDet.when at q + rw [if_neg (not_not.2 rfl)] at q + have eq : σ = updKSc σ p (.schedContext y n) := + (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst q))).2 + rw [e, show idleScPtr = p from e.symm.trans hcur, ← eq] at hz + rw [← show idleScPtr = p from e.symm.trans hcur] at hz + exact g z k hz + · rw [updKSc_kheap, Function.update_of_ne (Ne.symm e)] at hz + exact g z k hz + let σ1 := updKSc σ p (.schedContext y n) + change SchedSRB st Z m κ s1 σ1 σ.curThread at H1 + refine WP.spec_bind (sched.set_consumed.spec s1 0#u64) ?_ + rintro s2 rfl + let τ := { σ1 with consumedTime := 0 } + let s2 := L.wr s1 (L.nodeA s1 layout.node.CONSUMED) 0#u64 + obtain ⟨H2, w2⟩ := H1.clearConsumedA + change SchedSRB st Z m κ s2 τ τ.curThread at H2 + have frame1 : MFrA (st := st) Z m s s1 σ σ1 := cfgScFrameA (st := st) hsc hk w1 + have frame2 : MFrA (st := st) Z m s1 s2 σ1 τ := + MFrA.of_sw rfl (w2.mono fun _ e => Or.inl (e ▸ H1.node_sks H1.sch.cpuCs (by + unfold layout.node.CONSUMED; decide))) + have frame : MFrA (st := st) Z m s s2 σ τ := frame1.trans frame2 rfl rfl + obtain ⟨E2, M2, Y2⟩ := entryScFrameA E hsc hk (show τ.kheap = σ1.kheap from rfl) hY frame + have h2 : τ.kheap p = some (.schedContext y n) := updKSc_same _ _ _ + have A2 := entryActiveIdleA H Idle hk hAct + have I2 := entryCurInvA I hcur hk (hAct.trans ha) + obtain ⟨t, hkt, hB, hIPC⟩ := I2.tcb + let ct := L.curW s2 + have hCt : τ.curThread = specPtr ct := H2.sch.curThread + have hkt' : τ.kheap (specPtr ct) = some (.tcb t) := by rwa [← hCt] + have hR := (H2.relOn H2.curT H2.curOn).2 (hCt ▸ I2.notRel) + refine WP.spec_bind (sched.cur_thread.spec s2) ?_ + intro c hc + rw [show c = ct from hc, is_schedulable_eqA, schedulable_eqA H2.toSchedSRN H2.curT + (H2.relOn H2.curT H2.curOn) E2, bind_ok] + by_cases hRun : schedulable (specPtr ct) τ = true + · rw [if_pos hRun] + have hcs : L.cscW s2 = sc := by + apply scRenA_inj m (H2.sch.scOff _ H2.cscC) (H.sch.scOff _ hsc) + exact H2.currentScA.symm.trans hcur + have hon : L.mem s2 (scCoreA sc) = L.cpu s2 := by + rw [H2.scCore sc (hcs ▸ H2.cscC), P.scCore] + exact UScalar.eq_of_val_eq (H.sch.core.symm.trans H2.sch.core) + have hq : (L.mem s2 (inqA ct) &&& sched.Q_QUEUED) = 0#u64 := by + by_contra hn + have q := (H2.core.queued ct H2.curT).1 hn + rw [H2.curOn] at q + obtain ⟨z, hz, _, _, hp⟩ := H2.rest.prio ct H2.curT + have ee := ofNat_prio_eqA hp + have e := H2.rest.rq _ (H2.core.prio _ H2.curT) + rw [ee] at e + exact P.notReady 0 z.tcbPriority (by + change τ.curThread ∈ τ.readyQueues 0 z.tcbPriority + rw [hCt, e, mem_map_specPtrSc] + exact q) + have hBack : t.tcbSchedContext = some p := hcur ▸ hB + have own : L.mem s2 (scTcbA sc) = ct := by + obtain ⟨z, j, hz, rel⟩ := H2.scr.sc sc hsc + change τ.kheap p = _ at hz + rw [h2] at hz; cases hz + obtain ⟨z, j, hz, ho⟩ := H2.inv.back _ _ _ hkt' hBack + rw [h2] at hz; cases hz + rw [rel.tcb, ← scTcbA_eqSc] at ho + have he : L.mem s2 (scTcbA sc) ≠ 0#u64 := by + intro e; rw [e, optRefS_zero] at ho; cases ho + rw [optRefS_ne he] at ho + exact specPtr_inj (Option.some.inj ho) + obtain ⟨t', eT, hWord⟩ := H2.sch.tcb ct H2.curT + rw [hkt'] at eT; cases eT + have hz : L.mem s2 (scA ct) ≠ 0#u64 := by + intro e + rw [hBack, e, optScA, if_pos rfl] at hWord + cases hWord + have hWordS : L.mem s2 (scA ct) = sc := by + apply scRenA_inj m (H2.sch.scOff _ (H2.rest.scMem ct H2.curT hz)) (H.sch.scOff _ hsc) + rw [hBack, optScA, if_neg hz] at hWord + exact (Option.some.inj hWord).symm + have sc0 : sc ≠ 0#u64 := hWordS ▸ hz + have T2 := T s2 a τ frame H2 E2 M2 Y2 clk rfl (hCt.symm ▸ hRun) + have NS : can = true → NoSendAtA τ (specPtr ct) y t := fun hc => by + have q := hns hc _ hHead (by rw [show σ.curThread = specPtr ct from hCt]; exact hRun) t y n hkt + (by rw [hcur]; exact h2) + rw [← hCt]; exact q + refine WP.spec_bind (end_timeslice_noSendTA (σ := τ) (hCt ▸ H2) a clk A2 E2 M2 Y2 H2.curT hsc sc0 rfl hcs + hcur H2.curOn hon own hWordS hq (fun d p => hCt ▸ P.notReady d p) + hkt' hBack hRun h2 (hAct.trans ha) hag hMin T2 can NS) ?_ + rintro ⟨s3, a3⟩ ⟨σ3, Q3, R3, he, heF, H3, span, fr, hk3, hReq, ha3⟩ + simp only [uncurry_apply_pair] + rw [← cfgResultAssocA] + refine WP.spec_bind (entryRescheduleFrA H3) ?_ + rintro s4 ⟨σ4, Q4, he', H4, hk4, hr4, hi4, hm4, ht4, hc4, ha4, f4⟩ + rw [WP.spec_ok] + have hIdle : ActIdleA σ4 := by + refine ⟨(fun u hu => by rw [ha4] at hu; cases hu), ?_⟩ + simpa only [hk4, hr4, hi4] using span.idle + refine ⟨σ4, Q4, R3, ?_, ?_, H4, hm4.symm ▸ span.witness, ht4.trans span.time, + hm4.trans span.machine, hc4.trans span.consumed, hIdle, Or.inl ha4, + (frame.trans fr rfl rfl).trans f4 rfl rfl, ⟨fun q z k hz => ?_, fun q z hz => ?_⟩, ⟨y, n, hTcbY, Or.inl ⟨?_, σ3, ?_, he'⟩, hRdy⟩, ha3⟩ + · unfold fl4ChargeBudget + rw [nd_gets_bindSc, nd_bind_oneSc hHead] + rw [nd_bind_oneSc (show NonDet.modify (fun s : State Unit => { s with consumedTime := 0 }) σ1 = + ({((), τ)}, False) from rfl)] + repeat rw [nd_gets_bindSc] + rw [NonDet.when, if_pos (hCt.symm ▸ hRun)] + rw [nd_bind_oneSc he] + exact he' + · unfold fl4ChargeBudget + rw [nd_gets_bindSc, nd_bind_oneSc hHead] + rw [nd_bind_oneSc (show NonDet.modify (fun s : State Unit => { s with consumedTime := 0 }) σ1 = + ({((), τ)}, False) from rfl)] + repeat rw [nd_gets_bindSc] + rw [NonDet.when, if_pos (hCt.symm ▸ hRun)] + rw [nd_bind_oneSc heF] + exact he' + · rw [hk4, hk3]; exact KS.1 q z k hz + · rw [hk4, hk3]; exact KS.2 q z hz + · have hRun' : schedulable σ.curThread τ = true := by + rw [show σ.curThread = specPtr ct from hCt]; exact hRun + rw [hcur]; exact hRun' + · rw [hcur, show σ.curThread = specPtr ct from hCt]; exact hReq + · rw [if_neg hRun, WP.spec_ok] + have nonRun : runnable t.tcbState ≠ true := by + intro r + exact hRun (cfgSchedulableA hkt' (hcur ▸ hB) h2 r (hAct.trans ha) (hCt ▸ I2.notRel)) + have H3 : SchedSRA st Z m κ s2 τ := ⟨H2.toSchedSRN, fun u z hz hClass => by + by_cases e : u = τ.curThread + · rw [e, hkt] at hz; cases hz + rcases hClass with h | ⟨q, h⟩ | ⟨ep, d, h, _⟩ | ⟨ep, r, d, h⟩ + · exact (nonRun h).elim + all_goals rw [h] at hIPC; cases hIPC + · exact H2.rdEx u z e hz hClass⟩ + have spec (c : Bool) : fl4ChargeBudget (Z := Unit) used.bv c σ = ({((), τ)}, False) := by + unfold fl4ChargeBudget + rw [nd_gets_bindSc, nd_bind_oneSc hHead] + rw [nd_bind_oneSc (show NonDet.modify (fun s : State Unit => { s with consumedTime := 0 }) σ1 = + ({((), τ)}, False) from rfl)] + repeat rw [nd_gets_bindSc] + rw [NonDet.when, if_neg (fun h => hRun (hCt ▸ h))] + rfl + refine ⟨τ, Z.Q, Z.R, spec can, spec false, ⟨H3, E2, M2, Y2⟩, clk, rfl, rfl, rfl, A2, Or.inr ⟨I2, ?_⟩, frame, + KS, ⟨y, n, hTcbY, Or.inr ⟨?_, by rw [hcur]⟩, hRdy⟩, rfl⟩ + · intro z hz + rw [hkt] at hz; cases hz + exact nonRun + · have hRun' : ¬ schedulable σ.curThread τ = true := by + rw [show σ.curThread = specPtr ct from hCt]; exact hRun + rw [hcur]; exact Bool.eq_false_iff.2 hRun' + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScEntryChargeA.lean b/proofs/Fl4ProofsA64/P3/ScEntryChargeA.lean new file mode 100644 index 000000000..a179cf584 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScEntryChargeA.lean @@ -0,0 +1,399 @@ +import Fl4ProofsA64.P3.ScCfgChargeA + +/-! The refill update of entry-time budget exhaustion. Only the current +TCB can lose its readiness while its ring is being charged. -/ + +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3.Sched + +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +private theorem rrResetSplitA (a l : sporadic.Refill) (i : List sporadic.Refill) : + rrResetLA (a :: (i ++ [l])) = + ⟨a.time, core.num.U64.wrapping_add a.amount l.amount⟩ :: (i ++ [⟨l.time, 0#u64⟩]) := by + unfold rrResetLA + simp only [List.headD_cons, List.tail_cons] + have e : (a :: (i ++ [l])).getLastD zRA = l := by + rw [← List.cons_append, List.getLastD_eq_getLast?, List.getLast?_concat]; rfl + rw [e, ← List.cons_append, List.dropLast_concat]; rfl + +private theorem rrResetOkA {x : SchedContext} {rs : List sporadic.Refill} + (hx : x.scRefills = rs.map toRefillA) (h : RRokA x) : + RRokA { x with scRefills := (rrResetLA rs).map toRefillA } := by + obtain ⟨a, i, l, rfl⟩ := cfgRrSplitA hx h.1 + obtain ⟨_, hSum, hMin⟩ := h + unfold refillHd at hMin + rw [hx] at hSum hMin + simp [toRefillA, List.sum_append, the] at hSum hMin + have hFit : a.amount.val + l.amount.val < 2 ^ 64 := by omega + have hw := wadd_val (x := a.amount) (y := l.amount) hFit + refine ⟨?_, ?_, ?_⟩ + · simp [rrResetSplitA] + · simp [rrResetSplitA, toRefillA, List.sum_append]; omega + · unfold refillHd + simp [rrResetSplitA, toRefillA, the] + rw [BitVec.le_def] at hMin ⊢ + change _ ≤ (core.num.U64.wrapping_add a.amount l.amount).val + rw [hw] + exact Nat.le_trans hMin (Nat.le_add_right _ _) + +/-- `entryChargeRingA` for any bound thread: the SC is unbound or bound to `u`, which is outside the release queue (the +relation excepts `u`'s readiness). -/ +theorem entryChargeRingOwnA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + {u : ObjRef} (H : SchedSRB st Z m κ s σ u) (a : A) + {sc : U64} (hc : sc ∈ Z.C) (hcs : L.cscW s = sc) (hcur : σ.curSc = scNameA m sc) + {x : SchedContext} {n : Nat} (hk : σ.kheap (scNameA m sc) = some (.schedContext x n)) + (ha : scActive x = true) (hi : NotIdleScA m (scNameA m sc)) + (ht : x.scTcb = none ∨ x.scTcb = some u) (hnr : u ∉ σ.releaseQueue) + (hag : fl4MinBudget = MIN_BUDGET) + (hlimit : sporadic.max_release_time ar a ⦃ v => v.bv = fl4MaxReleaseTime ⦄) + (hmin : sporadic.min_budget ar a ⦃ v => v.bv = fl4MinBudget ⦄) + (c : U64) : + (do + let rr ← sporadic.is_round_robin st s sc + if rr then do + let hi ← obj.Sc.refill_head st sc s + let ti ← obj.Sc.refill_tail st sc s + let h ← sporadic.get st s sc hi + let t ← sporadic.get st s sc ti + let i ← lift (core.num.U64.wrapping_add h.amount t.amount) + let s4 ← sporadic.put st s sc hi { h with amount := i } + sporadic.put st s4 sc ti { t with amount := 0#u64 } + else sporadic.budget_check st ar s a c) ⦃ s' => ∃ y, + let τ := updKSc σ (scNameA m sc) (.schedContext y n) + (do + let rr ← isRoundRobin (Z := Unit) (scNameA m sc) + if rr then refillResetRr (scNameA m sc) else fl4RefillBudgetCheck c.bv) σ = ({((), τ)}, False) ∧ + SchedSRB st Z m κ s' τ u ∧ + y = { x with scRefills := y.scRefills } ∧ WritesIn st s s' (ScObjA sc n) ⦄ := by + obtain ⟨x0, n0, hk0, hxw⟩ := H.scr.sc sc hc + rw [hk] at hk0; cases hk0 + have hx := hxw.strong ha + have hne := refills_neL hx.wf + have hmin' : sporadic.min_budget ar a ⦃ v => v.bv = MIN_BUDGET ⦄ := by + simpa only [hag] using hmin + have rebuild (s' : S) (y : SchedContext) + (hS : ScStateRelA st Z.C m s' (updKSc σ (scNameA m sc) (.schedContext y n))) + (w : WritesIn st s s' (ScObjA sc n)) + (hy : y = { x with scRefills := y.scRefills }) + (hp : y.scPeriod ≠ 0 → RefOKA y) (hr : y.scPeriod = 0 → RRokA y) + (hCore : L.mem s' (scCoreA sc) = L.mem s (scCoreA sc)) : + SchedSRB st Z m κ s' (updKSc σ (scNameA m sc) (.schedContext y n)) u := by + obtain ⟨y0, n0, e, R⟩ := hS.sc sc hc + rw [updKSc_same] at e; cases e + exact H.scReconfA hc hk w R (by rw [hy]) ht hnr hp hr hCore + refine WP.spec_bind (is_round_robin_refinesA H.scr hc hk) ?_ + intro b hb + have spec (y : SchedContext) + (e : (if b = true then refillResetRr (Z := Unit) (scNameA m sc) else fl4RefillBudgetCheck c.bv) σ = + ({((), updKSc σ (scNameA m sc) (.schedContext y n))}, False)) : + (do + let rr ← isRoundRobin (Z := Unit) (scNameA m sc) + if rr then refillResetRr (scNameA m sc) else fl4RefillBudgetCheck c.bv) σ = + ({((), updKSc σ (scNameA m sc) (.schedContext y n))}, False) := by + rw [nd_bind_oneSc hb] + exact e + cases hB : b + · simp only [Bool.false_eq_true, ↓reduceIte] + have hper : x.scPeriod ≠ 0 := by + intro e + rw [isRoundRobin_eqA hk, e, hB] at hb + have contrad := congrArg Prod.fst hb + simpa only [decide_true, Set.singleton_eq_singleton_iff, Prod.mk.injEq, Bool.true_eq_false, false_and] using contrad + have hper' : L.rperiod s sc ≠ 0#u64 := by + intro e; apply hper; rw [hx.period, e]; rfl + obtain ⟨_, hp, hsum, hmh⟩ := H.inv.refills _ _ _ hk ha hper + have hp' : PosA (L.refills s sc) := fun v hv => + hp (toRefillA v) (by rw [hx.refills]; exact List.mem_map_of_mem hv) + have hsum' : lsumA (L.refills s sc) < 2 ^ 64 := by rwa [lsumA_eq_map, ← hx.refills] + have minh : MIN_BUDGET.toNat ≤ lsumA (L.refills s sc) := by + rw [BitVec.le_def, refillHd_mapA hx.refills hne] at hmh + obtain ⟨v, vs, e⟩ := List.exists_cons_of_ne_nil hne + rw [e, List.headD_cons] at hmh; rw [e, lsumA_cons] + exact Nat.le_trans hmh (Nat.le_add_right _ _) + refine WP.spec_mono (budget_check_refinesWithA fl4MaxReleaseTime MIN_BUDGET + a H.scr H.scp hcs hcur hc hk ha hlimit hmin' hper' hp' hsum' minh c) ?_ + rintro s' ⟨σ', en, hS, _, w, hP, hR⟩ + obtain ⟨y, rfl, hy, hny, hpy, hsy, hmy⟩ := hP x n hk + have hfinal : fl4RefillBudgetCheck (Z := Unit) c.bv σ = + ({((), updKSc σ (scNameA m sc) (.schedContext y n))}, False) := by + unfold fl4RefillBudgetCheck + rw [hag] + exact en + have hCore : L.mem s' (scCoreA sc) = L.mem s (scCoreA sc) := by + have e : scCoreA sc = scWA sc 16#u64 := by unfold scCoreA scWA layout.sc.CORE; rfl + rw [e] + exact hR.hdr (by simp) (by simp) + refine ⟨y, spec y (by simpa only [hB, Bool.false_eq_true, ↓reduceIte] using hfinal), + rebuild _ y hS w hy (fun _ => ⟨hny, hpy, hsy, hmy⟩) (fun e => ?_) hCore, hy, w⟩ + exact (hper ((congrArg SchedContext.scPeriod hy).symm.trans e)).elim + · simp only [↓reduceIte] + have hper : x.scPeriod = 0 := by + rw [isRoundRobin_eqA hk, hB] at hb + have e : decide (x.scPeriod = 0) = true := by + simpa only [Prod.mk.injEq, Set.singleton_eq_singleton_iff, and_true] using hb + exact of_decide_eq_true e + refine WP.spec_mono (rr_reset_coreA H.scr H.scp hc hk ha) ?_ + rintro s' ⟨τ, e, hS, _, w, hCore⟩ + let y := { x with scRefills := (rrResetLA (L.refills s sc)).map toRefillA } + have e' : refillResetRr (Z := Unit) (scNameA m sc) σ = + ({((), updKSc σ (scNameA m sc) (.schedContext y n))}, False) := + refillResetRr_eqA hk hx.refills hne + have hτ := e'.symm.trans e + simp only [Prod.mk.injEq, Set.singleton_eq_singleton_iff, and_true, true_and] at hτ + subst τ + have hr := rrResetOkA hx.refills ((H.inv.rr _ _ _ hk ha hper).2 hi) + have core : scCoreA sc = scWA sc 16#u64 := by unfold scCoreA scWA layout.sc.CORE; rfl + rw [← core] at hCore + refine ⟨y, spec y (by simpa only [hB, ↓reduceIte] using e'), + rebuild _ y hS w rfl (fun hp => (hp hper).elim) (fun _ => hr) hCore, rfl, w⟩ + +/-- The concrete ring update takes the RR reset or the periodic FL4 +charge and preserves the scheduler relation except for the current +TCB's readiness. Both conditional monads have a single outcome. -/ +theorem entryChargeRingA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRB st Z m κ s σ σ.curThread) (a : A) + {sc : U64} (hc : sc ∈ Z.C) (hcs : L.cscW s = sc) (hcur : σ.curSc = scNameA m sc) + {x : SchedContext} {n : Nat} (hk : σ.kheap (scNameA m sc) = some (.schedContext x n)) + (ha : scActive x = true) (hi : NotIdleScA m (scNameA m sc)) + (ht : x.scTcb = some σ.curThread) (hnr : σ.curThread ∉ σ.releaseQueue) + (hag : fl4MinBudget = MIN_BUDGET) + (hlimit : sporadic.max_release_time ar a ⦃ v => v.bv = fl4MaxReleaseTime ⦄) + (hmin : sporadic.min_budget ar a ⦃ v => v.bv = fl4MinBudget ⦄) + (c : U64) : + (do + let rr ← sporadic.is_round_robin st s sc + if rr then do + let hi ← obj.Sc.refill_head st sc s + let ti ← obj.Sc.refill_tail st sc s + let h ← sporadic.get st s sc hi + let t ← sporadic.get st s sc ti + let i ← lift (core.num.U64.wrapping_add h.amount t.amount) + let s4 ← sporadic.put st s sc hi { h with amount := i } + sporadic.put st s4 sc ti { t with amount := 0#u64 } + else sporadic.budget_check st ar s a c) ⦃ s' => ∃ y, + let τ := updKSc σ (scNameA m sc) (.schedContext y n) + (do + let rr ← isRoundRobin (Z := Unit) (scNameA m sc) + if rr then refillResetRr (scNameA m sc) else fl4RefillBudgetCheck c.bv) σ = ({((), τ)}, False) ∧ + SchedSRB st Z m κ s' τ σ.curThread ∧ + y = { x with scRefills := y.scRefills } ∧ WritesIn st s s' (ScObjA sc n) ⦄ := + entryChargeRingOwnA (u := σ.curThread) H a hc hcs hcur hk ha hi (Or.inr ht) hnr hag hlimit hmin c + +/-- `entryConsumedA` for any bound thread: the SC is unbound or bound to `u`, which is outside the release queue (the +relation excepts `u`'s readiness). -/ +theorem entryConsumedOwnA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + {u : ObjRef} (H : SchedSRB st Z m κ s σ u) {sc : U64} (hc : sc ∈ Z.C) + {x : SchedContext} {n : Nat} (hk : σ.kheap (scNameA m sc) = some (.schedContext x n)) + (ha : scActive x = true) (hi : NotIdleScA m (scNameA m sc)) + (ht : x.scTcb = none ∨ x.scTcb = some u) (hnr : u ∉ σ.releaseQueue) (used : U64) : + (do + let i ← obj.Sc.consumed st sc s + let total ← lift (core.num.U64.wrapping_add i used) + obj.Sc.set_consumed st sc s total) ⦃ s' => + let τ := updKSc σ (scNameA m sc) (.schedContext { x with scConsumed := x.scConsumed + used.bv } n) + updateSchedContext (Z := Unit) (scNameA m sc) (fun y => { y with scConsumed := y.scConsumed + used.bv }) σ = + ({((), τ)}, False) ∧ + SchedSRB st Z m κ s' τ u ∧ WritesIn st s s' (ScObjA sc n) ⦄ := by + obtain ⟨x0, n0, hk0, R⟩ := H.scr.sc sc hc + rw [hk] at hk0; cases hk0 + refine WP.spec_bind (obj.Sc.consumed.specSc s sc) ?_ + intro old he + simp only [lift, bind_ok] + let total := core.num.U64.wrapping_add old used + have hTotal : total.bv = x.scConsumed + used.bv := by + unfold total + rw [core.num.U64.wrapping_add_bv_eq, he, ← R.consumed] + refine WP.spec_mono (obj.Sc.set_consumed.specSc s sc total) ?_ + rintro s' rfl + obtain ⟨rel, w⟩ := R.set_consumed total + have hCore : L.mem (L.wr s (scWA sc 8#u64) total) (scCoreA sc) = L.mem s (scCoreA sc) := by + have e : scCoreA sc = scWA sc 16#u64 := by unfold scCoreA scWA layout.sc.CORE; rfl + rw [e] + apply L.mem_wr_ne (R.hdr (by simp : (8#u64).val < 96) (by simp)).1 (R.hdr (by simp : (16#u64).val < 96) (by simp)).1 + have hf := R.bounds.2.2 + rw [scWA_val (sc := sc) (K := 8#u64) (by simp; omega), + scWA_val (sc := sc) (K := 16#u64) (by simp; omega)] + simp + have hS := H.scReconfA hc hk w rel rfl ht hnr (fun h => show RefOKA x from H.inv.refills _ x n hk ha h) + (fun h => show RRokA x from (H.inv.rr _ x n hk ha h).2 hi) hCore + rw [hTotal] at hS + exact ⟨updateSchedContext_eqA hk _, hS, w⟩ + +/-- Charge the SC's consumed word without changing the ring or ownership. -/ +theorem entryConsumedA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRB st Z m κ s σ σ.curThread) {sc : U64} (hc : sc ∈ Z.C) + {x : SchedContext} {n : Nat} (hk : σ.kheap (scNameA m sc) = some (.schedContext x n)) + (ha : scActive x = true) (hi : NotIdleScA m (scNameA m sc)) + (ht : x.scTcb = some σ.curThread) (hnr : σ.curThread ∉ σ.releaseQueue) (used : U64) : + (do + let i ← obj.Sc.consumed st sc s + let total ← lift (core.num.U64.wrapping_add i used) + obj.Sc.set_consumed st sc s total) ⦃ s' => + let τ := updKSc σ (scNameA m sc) (.schedContext { x with scConsumed := x.scConsumed + used.bv } n) + updateSchedContext (Z := Unit) (scNameA m sc) (fun y => { y with scConsumed := y.scConsumed + used.bv }) σ = + ({((), τ)}, False) ∧ + SchedSRB st Z m κ s' τ σ.curThread ∧ WritesIn st s s' (ScObjA sc n) ⦄ := + entryConsumedOwnA (u := σ.curThread) H hc hk ha hi (Or.inr ht) hnr used + +/-- Split charging at the node reset so that the postponed current TCB +can be absent from the intermediate ready relation. -/ +theorem entryChargeHead_refinesA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRA st Z m κ s σ) (I : CurInvA σ) (a : A) (hag : fl4MinBudget = MIN_BUDGET) + (hlimit : sporadic.max_release_time ar a ⦃ v => v.bv = fl4MaxReleaseTime ⦄) + (hmin : sporadic.min_budget ar a ⦃ v => v.bv = fl4MinBudget ⦄) + {sc : U64} (hc : sc ∈ Z.C) (hcs : L.cscW s = sc) (hcur : σ.curSc = scNameA m sc) + {x : SchedContext} {n : Nat} (hk : σ.kheap (scNameA m sc) = some (.schedContext x n)) + (hni : σ.curSc ≠ idleScPtr → NotIdleScA m σ.curSc) (c : U64) (b : Bool) + (hb : b = decide (σ.curSc ≠ idleScPtr)) : + (if b then do + let rr ← sporadic.is_round_robin st s sc + let s3 ← if rr then do + let hi ← obj.Sc.refill_head st sc s + let ti ← obj.Sc.refill_tail st sc s + let h ← sporadic.get st s sc hi + let t ← sporadic.get st s sc ti + let i ← lift (core.num.U64.wrapping_add h.amount t.amount) + let s4 ← sporadic.put st s sc hi { h with amount := i } + sporadic.put st s4 sc ti { t with amount := 0#u64 } + else sporadic.budget_check st ar s a c + let i ← obj.Sc.consumed st sc s3 + let total ← lift (core.num.U64.wrapping_add i c) + obj.Sc.set_consumed st sc s3 total + else ok s) ⦃ s' => ∃ y, + let τ := updKSc σ (scNameA m sc) (.schedContext y n) + fl4ChargeHead σ.curSc c.bv σ = ({((), τ)}, False) ∧ SchedSRB st Z m κ s' τ σ.curThread ∧ + y.scTcb = x.scTcb ∧ scActive y = scActive x ∧ y.scYieldFrom = x.scYieldFrom ∧ + WritesIn st s s' (ScObjA sc n) ⦄ := by + rw [hb] + by_cases hi : σ.curSc ≠ idleScPtr + · rw [decide_eq_true hi] + simp only [↓reduceIte, cfgResultAssocA] + have hActive := I.active hi x n (hcur.symm ▸ hk) + have hNotIdle := hcur ▸ hni hi + obtain ⟨t, hkt, ht, -⟩ := I.tcb + obtain ⟨z, j, hz, hzt⟩ := H.inv.back _ _ _ hkt ht + rw [hcur, hk] at hz; cases hz + rw [← cfgResultAssocA] + refine WP.spec_bind (entryChargeRingA (H.toB _) a hc hcs hcur hk hActive hNotIdle hzt I.notRel + hag hlimit hmin c) ?_ + rintro s' ⟨y, he, hS, hy, w⟩ + let τ := updKSc σ (scNameA m sc) (.schedContext y n) + have hMax : y.scRefillMax = x.scRefillMax := by + have e := congrArg SchedContext.scRefillMax hy + exact e + have hY : scActive y = scActive x := by + unfold scActive activeSc + rw [hMax] + have htY : y.scTcb = x.scTcb := by + have e := congrArg SchedContext.scTcb hy + exact e + have hyf : y.scYieldFrom = x.scYieldFrom := by + have e := congrArg SchedContext.scYieldFrom hy + exact e + refine WP.spec_mono (entryConsumedA hS hc (updKSc_same _ _ _) (hY.trans hActive) hNotIdle + (htY.trans hzt) I.notRel c) ?_ + rintro s'' ⟨hU, hS', w'⟩ + rw [updKSc_updKSc] at hS' + refine ⟨{ y with scConsumed := y.scConsumed + c.bv }, ?_, hS', htY, hY, hyf, + (w.trans w').mono fun _ h => h.elim id id⟩ + unfold fl4ChargeHead + rw [NonDet.when, if_pos hi, hcur] + rw [nd_bind_oneSc (isRoundRobin_eqA hk)] at he ⊢ + split_ifs at he ⊢ <;> rw [nd_bind_oneSc he] <;> dsimp only <;> rw [hU, updKSc_updKSc] + · rw [decide_eq_false hi] + simp only [Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + refine ⟨x, ?_, ?_, rfl, rfl, rfl, WritesIn.refl _ _⟩ + · unfold fl4ChargeHead + rw [NonDet.when, if_neg hi, updKSc_selfA hk] + rfl + · rw [updKSc_selfA hk] + exact H.toB _ + + +/-- **`entryChargeHead_refinesA` with the SC's reply list and refill maximum kept** (the charge rewrites only the +refills and the consumed time). -/ +theorem entryChargeHeadEqA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRA st Z m κ s σ) (I : CurInvA σ) (a : A) (hag : fl4MinBudget = MIN_BUDGET) + (hlimit : sporadic.max_release_time ar a ⦃ v => v.bv = fl4MaxReleaseTime ⦄) + (hmin : sporadic.min_budget ar a ⦃ v => v.bv = fl4MinBudget ⦄) + {sc : U64} (hc : sc ∈ Z.C) (hcs : L.cscW s = sc) (hcur : σ.curSc = scNameA m sc) + {x : SchedContext} {n : Nat} (hk : σ.kheap (scNameA m sc) = some (.schedContext x n)) + (hni : σ.curSc ≠ idleScPtr → NotIdleScA m σ.curSc) (c : U64) (b : Bool) + (hb : b = decide (σ.curSc ≠ idleScPtr)) : + (if b then do + let rr ← sporadic.is_round_robin st s sc + let s3 ← if rr then do + let hi ← obj.Sc.refill_head st sc s + let ti ← obj.Sc.refill_tail st sc s + let h ← sporadic.get st s sc hi + let t ← sporadic.get st s sc ti + let i ← lift (core.num.U64.wrapping_add h.amount t.amount) + let s4 ← sporadic.put st s sc hi { h with amount := i } + sporadic.put st s4 sc ti { t with amount := 0#u64 } + else sporadic.budget_check st ar s a c + let i ← obj.Sc.consumed st sc s3 + let total ← lift (core.num.U64.wrapping_add i c) + obj.Sc.set_consumed st sc s3 total + else ok s) ⦃ s' => ∃ y, + let τ := updKSc σ (scNameA m sc) (.schedContext y n) + fl4ChargeHead σ.curSc c.bv σ = ({((), τ)}, False) ∧ SchedSRB st Z m κ s' τ σ.curThread ∧ + y.scTcb = x.scTcb ∧ scActive y = scActive x ∧ y.scYieldFrom = x.scYieldFrom ∧ + y.scReplies = x.scReplies ∧ y.scRefillMax = x.scRefillMax ∧ + WritesIn st s s' (ScObjA sc n) ⦄ := by + rw [hb] + by_cases hi : σ.curSc ≠ idleScPtr + · rw [decide_eq_true hi] + simp only [↓reduceIte, cfgResultAssocA] + have hActive := I.active hi x n (hcur.symm ▸ hk) + have hNotIdle := hcur ▸ hni hi + obtain ⟨t, hkt, ht, -⟩ := I.tcb + obtain ⟨z, j, hz, hzt⟩ := H.inv.back _ _ _ hkt ht + rw [hcur, hk] at hz; cases hz + rw [← cfgResultAssocA] + refine WP.spec_bind (entryChargeRingA (H.toB _) a hc hcs hcur hk hActive hNotIdle hzt I.notRel + hag hlimit hmin c) ?_ + rintro s' ⟨y, he, hS, hy, w⟩ + let τ := updKSc σ (scNameA m sc) (.schedContext y n) + have hMax : y.scRefillMax = x.scRefillMax := by + have e := congrArg SchedContext.scRefillMax hy + exact e + have hY : scActive y = scActive x := by + unfold scActive activeSc + rw [hMax] + have htY : y.scTcb = x.scTcb := by + have e := congrArg SchedContext.scTcb hy + exact e + have hyf : y.scYieldFrom = x.scYieldFrom := by + have e := congrArg SchedContext.scYieldFrom hy + exact e + refine WP.spec_mono (entryConsumedA hS hc (updKSc_same _ _ _) (hY.trans hActive) hNotIdle + (htY.trans hzt) I.notRel c) ?_ + rintro s'' ⟨hU, hS', w'⟩ + rw [updKSc_updKSc] at hS' + have hRp : y.scReplies = x.scReplies := by + have e := congrArg SchedContext.scReplies hy + exact e + refine ⟨{ y with scConsumed := y.scConsumed + c.bv }, ?_, hS', htY, hY, hyf, hRp, hMax, + (w.trans w').mono fun _ h => h.elim id id⟩ + unfold fl4ChargeHead + rw [NonDet.when, if_pos hi, hcur] + rw [nd_bind_oneSc (isRoundRobin_eqA hk)] at he ⊢ + split_ifs at he ⊢ <;> rw [nd_bind_oneSc he] <;> dsimp only <;> rw [hU, updKSc_updKSc] + · rw [decide_eq_false hi] + simp only [Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + refine ⟨x, ?_, ?_, rfl, rfl, rfl, rfl, rfl, WritesIn.refl _ _⟩ + · unfold fl4ChargeHead + rw [NonDet.when, if_neg hi, updKSc_selfA hk] + rfl + · rw [updKSc_selfA hk] + exact H.toB _ + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScEntryEndA.lean b/proofs/Fl4ProofsA64/P3/ScEntryEndA.lean new file mode 100644 index 000000000..0ea8453f3 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScEntryEndA.lean @@ -0,0 +1,541 @@ +import Fl4ProofsA64.P3.ScEntryChargeA +import Fl4ProofsA64.P3.ScAppendA +import Fl4ProofsA64.P3.ScCfgFinishA +import Fl4ProofsA64.P3.ScYieldQueueA + +/-! End the local timeslice after a failed budget check. The ready- and +release-queue cases restore the temporary exception for the current TCB. -/ + +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3.Sched + +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +def entryRequeueA (p t : ObjRef) : SMonad Unit Unit := do + let ready ← getScRefillReady p + let sufficient ← fl4GetScRefillSufficient p 0 + if ready ∧ sufficient then tcbSchedAction tcbSchedAppend t else postpone p + +theorem fl4GetScRefillSufficient_eqA {σ : State Unit} {p : ObjRef} {x : SchedContext} {n : Nat} + (hk : σ.kheap p = some (.schedContext x n)) (hne : x.scRefills ≠ []) (u : Ticks) : + fl4GetScRefillSufficient (Z := Unit) p u σ = ({(fl4RefillSufficient u (refillHd x), σ)}, False) := by + have h : Option.bind (readRefillHead p σ) (fun head => Lookup.oreturn (fl4RefillSufficient u head) σ) = + some (fl4RefillSufficient u (refillHd x)) := by + rw [readRefillHead_eqA hk hne] + rfl + change NonDet.assertOpt (Option.bind (readRefillHead p σ) + (fun head => Lookup.oreturn (fl4RefillSufficient u head) σ)) σ = _ + rw [h] + rfl + +private theorem entryReadyCurrentA {σ : State Unit} {t p : ObjRef} {x : Tcb} {y : SchedContext} {n : Nat} + (hk : σ.kheap t = some (.tcb x)) (hb : x.tcbSchedContext = some p) + (hky : σ.kheap p = some (.schedContext y n)) (ready : releasedSc σ y) : + RelIfBoundSc σ t := by + intro z q w k hz hq hw _ _ + rw [hk] at hz; cases hz + rw [hb] at hq; cases hq + rw [hky] at hw; cases hw + exact ready + +/-- With no timeout send, the ready head appends the current TCB and +the late head postpones it. The physical SC and TCB are local. -/ +theorem entryRequeue_refinesA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + {t sc : U64} (H : SchedSRB st Z m κ s σ (specPtr t)) (a : A) + (E : StEqA Z.T (L.mem s) σ) (M : McpRelA Z.T m (L.mem s) σ) + (Y : YieldRelA Z.T Z.C m (L.mem s) σ) + (ht : t ∈ Z.T) (hc : sc ∈ Z.C) (hsc0 : sc ≠ 0#u64) + (hon : L.mem s (affA t) = L.cpu s) (hcore : L.mem s (scCoreA sc) = L.cpu s) + (how : L.mem s (scTcbA sc) = t) (hsc : L.mem s (scA t) = sc) + (hq : (L.mem s (inqA t) &&& sched.Q_QUEUED) = 0#u64) + (hnq : ∀ d p, specPtr t ∉ σ.readyQueues d p) + {x : Tcb} (hkx : σ.kheap (specPtr t) = some (.tcb x)) + (hb : x.tcbSchedContext = some (scNameA m sc)) (hs : schedulable (specPtr t) σ = true) + {y : SchedContext} {n : Nat} (hky : σ.kheap (scNameA m sc) = some (.schedContext y n)) + (ha : scActive y = true) (hag : fl4MinBudget = MIN_BUDGET) (hmin : Fl4MinBudgetAgreeA ar a) : + (do + let b ← sporadic.ready st s sc + if b then do + let b1 ← sporadic.sufficient st ar s a sc 0#u64 + if b1 then let s1 ← sched.append st s t; ok (s1, a) + else let s1 ← sched.postpone st s sc; ok (s1, a) + else let s1 ← sched.postpone st s sc; ok (s1, a)) ⦃ r => ∃ τ Q' R', + entryRequeueA (scNameA m sc) (specPtr t) σ = ({((), τ)}, False) ∧ + YieldQueueRelA (st := st) { Z with Q := Q', R := R' } m κ r.1 τ ∧ + r.2 = a ∧ τ.machineState = σ.machineState ∧ τ.curTime = σ.curTime ∧ + τ.consumedTime = σ.consumedTime ∧ τ.kheap = σ.kheap ∧ τ.idleThread = σ.idleThread ∧ + MFrA (st := st) Z m s r.1 σ τ ⦄ := by + obtain ⟨hrun, hnr⟩ := schedulable_partsA hs + have hr0 := (H.relOn ht hon).2 hnr + obtain ⟨y0, n0, hy0, hRel⟩ := H.scr.sc sc hc + rw [hky] at hy0; cases hy0 + have hta := (hRel.strong ha).rmax_ne0 + have hne := H.inv.ne hky ha + have eS := fl4GetScRefillSufficient_eqA hky hne 0 + have eR := getScRefillReady_eqA hky hne + have post (e : ¬ (refillReady σ.curTime (refillHd y) = true ∧ fl4RefillSufficient 0 (refillHd y) = true)) : + (do let s1 ← sched.postpone st s sc; ok (s1, a)) ⦃ r => ∃ τ Q' R', + entryRequeueA (scNameA m sc) (specPtr t) σ = ({((), τ)}, False) ∧ + YieldQueueRelA (st := st) { Z with Q := Q', R := R' } m κ r.1 τ ∧ + r.2 = a ∧ τ.machineState = σ.machineState ∧ τ.curTime = σ.curTime ∧ + τ.consumedTime = σ.consumedTime ∧ τ.kheap = σ.kheap ∧ τ.idleThread = σ.idleThread ∧ MFrA (st := st) Z m s r.1 σ τ ⦄ := by + refine WP.spec_bind (postponeTime_refinesA H hc ht hon how hsc hsc0 hcore hq hr0 hta hnq hrun) ?_ + rintro s' ⟨τ, R', he, hK, -, hT, -, -, -, -, w, hS, hM, hC, hI⟩ + rw [WP.spec_ok] + have hw : WritesIn st s s' (SWrA (st := st) Z s) := by + apply w.mono + intro z hz + rcases hz with hz | rfl + · exact H.qfoot_swr (fun v hv => (List.mem_cons.1 hv).elim (· ▸ ht) (fun e => (H.relAct v e).1)) + H.sch.cpuCs ht hz + · exact H.repr_swr + have frame := MFrA.of_sw (m := m) hK hw + obtain ⟨E', M', Y'⟩ := yieldQueueFrameNA H.toSchedSRN E M Y hK frame.wr + refine ⟨τ, Z.Q, R', ?_, ⟨hS, E', M', Y'⟩, rfl, hM, hT, hC, hK, hI, frame⟩ + unfold entryRequeueA + rw [nd_bind_oneSc eR, nd_bind_oneSc eS, if_neg e] + exact he + refine WP.spec_bind (ready_refinesA H.scr H.sch.time hc hky ha) ?_ + intro b eb + rw [eR] at eb + have bEq := (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst eb))).1 + rw [← bEq] + by_cases hR : refillReady σ.curTime (refillHd y) = true + · rw [if_pos hR] + refine WP.spec_bind (fl4_sufficient_refinesA a H.scr hc hky ha 0#u64 hmin) ?_ + intro b' eb' + have bEq' : fl4RefillSufficient 0 (refillHd y) = b' := eb' + rw [← bEq'] + by_cases hB : fl4RefillSufficient 0 (refillHd y) = true + · rw [if_pos hB] + have rel : releasedSc σ y := ⟨hR, by rwa [← fl4RefillSufficient_eq_l4v hag]⟩ + have Hfull := H.toA (entryReadyCurrentA hkx hb hky rel) + refine WP.spec_bind (append_refines_localA Hfull ht hon (fun _ => hr0) hrun) ?_ + rintro s' ⟨τ, he, hk, -, -, fix, hS, w⟩ + rw [WP.spec_ok] + have frame := MFrA.of_sw (m := m) hk w + obtain ⟨E', M', Y'⟩ := yieldQueueFrameA Hfull E M Y hk frame.wr + refine ⟨τ, _, Z.R, ?_, ⟨hS, E', M', Y'⟩, rfl, ?_, fix.curTime, fix.consumedTime, hk, fix.idleThread, frame⟩ + · unfold entryRequeueA + rw [nd_bind_oneSc eR, nd_bind_oneSc eS, if_pos ⟨hR, hB⟩] + exact he + · rw [tcbSchedAction_eqA hkx (by + obtain ⟨z, hz, hd, -, -⟩ := H.rest.prio t ht + rw [hkx] at hz; cases hz; exact hd) tcbSchedAppend] at he + have e := (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst he))).2 + rw [← e] + rfl + · rw [if_neg hB] + exact post (fun h => hB h.2) + · rw [if_neg hR] + exact post (fun h => hR h.1) + +/-- The logical clock and idle object carried by budget exhaustion. -/ +structure EndTimeA (Clock : A → MachineState → Prop) (σ : State Unit) (a : A) (τ : State Unit) : Prop where + witness : Clock a τ.machineState + time : τ.curTime = σ.curTime + machine : τ.machineState = σ.machineState + consumed : τ.consumedTime = σ.consumedTime + idle : ∃ t, τ.kheap τ.idleThread = some (.tcb t) ∧ t.tcbSchedContext = some idleScPtr ∧ + t.tcbState = .idleThreadState ∧ τ.idleThread ∉ τ.releaseQueue + +private theorem endTimeIdleA {σ τ : State Unit} (I : ActIdleA σ) (run : RunQA τ) + (hk : τ.kheap = σ.kheap) (hi : τ.idleThread = σ.idleThread) : + ∃ t, τ.kheap τ.idleThread = some (.tcb t) ∧ t.tcbSchedContext = some idleScPtr ∧ + t.tcbState = .idleThreadState ∧ τ.idleThread ∉ τ.releaseQueue := by + obtain ⟨t, ht, hb, hs, _⟩ := I.2 + have e := (show τ.kheap τ.idleThread = σ.kheap σ.idleThread by rw [hk, hi]).trans ht + refine ⟨t, e, hb, hs, ?_⟩ + intro hm + obtain ⟨z, hz, hr⟩ := run.2 _ hm + rw [e] at hz; cases hz + rw [hs] at hr + cases hr + +/-- [J] The CSpace read and IPC send at a timeout boundary. The send +starts in the same state as the validity test and carries the scheduler +and clock relations after the timeout IPC. Only this leaf uses the IPC line. -/ +structure EntryTimeoutA (Z : XSetsA) (m : McState) (κ : U64 → U64) + (Clock : A → MachineState → Prop) (s : S) (a : A) (σ : State Unit) (t : U64) : Prop where + valid : faults.valid_timeout_handler st s t ⦃ b => + isValidTimeoutHandler (Z := Unit) (specPtr t) σ = ({(b, σ)}, False) ⦄ + send : ∀ badge : U64, + isValidTimeoutHandler (Z := Unit) (specPtr t) σ = ({(true, σ)}, False) → + faults.handle_timeout st ar s a t (.Timeout badge) ⦃ r => ∃ τ Q' R', + fl4HandleTimeout (Z := Unit) (specPtr t) (.timeout badge.bv) σ = ({((), τ)}, False) ∧ + YieldQueueRelA (st := st) { Z with Q := Q', R := R' } m κ r.1 τ ∧ EndTimeA Clock σ r.2 τ ⦄ + +/-- [J] **The validity read of the timeout leaf** (`EntryTimeoutA.valid`): the part of `EntryTimeoutA` every +`can = false` / `NoTimeoutA` consumer uses; its `send` field is dead there. -/ +abbrev EntryValidA (s : S) (σ : State Unit) (t : U64) : Prop := + faults.valid_timeout_handler st s t ⦃ b => isValidTimeoutHandler (Z := Unit) (specPtr t) σ = ({(b, σ)}, False) ⦄ + +theorem EntryTimeoutA.toValid {Z : XSetsA} {m : McState} {κ : U64 → U64} {Clock : A → MachineState → Prop} {s : S} + {a : A} {σ : State Unit} {t : U64} (T : EntryTimeoutA (st := st) (ar := ar) Z m κ Clock s a σ t) : + EntryValidA (st := st) s σ t := T.valid + +set_option maxRecDepth 2048 in +theorem end_timeslice_refinesA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + {t sc : U64} (H : SchedSRB st Z m κ s σ (specPtr t)) (a : A) + {Clock : A → MachineState → Prop} (clock : Clock a σ.machineState) (I : ActIdleA σ) + (E : StEqA Z.T (L.mem s) σ) (M : McpRelA Z.T m (L.mem s) σ) (Y : YieldRelA Z.T Z.C m (L.mem s) σ) + (ht : t ∈ Z.T) (hc : sc ∈ Z.C) (hsc0 : sc ≠ 0#u64) + (hct : L.curW s = t) (hcs : L.cscW s = sc) (hcur : σ.curSc = scNameA m sc) + (hon : L.mem s (affA t) = L.cpu s) (hcore : L.mem s (scCoreA sc) = L.cpu s) + (how : L.mem s (scTcbA sc) = t) (hsc : L.mem s (scA t) = sc) + (hq : (L.mem s (inqA t) &&& sched.Q_QUEUED) = 0#u64) (hnq : ∀ d p, specPtr t ∉ σ.readyQueues d p) + {x : Tcb} (hkx : σ.kheap (specPtr t) = some (.tcb x)) + (hb : x.tcbSchedContext = some (scNameA m sc)) (hs : schedulable (specPtr t) σ = true) + {y : SchedContext} {n : Nat} (hky : σ.kheap (scNameA m sc) = some (.schedContext y n)) + (ha : scActive y = true) (hag : fl4MinBudget = MIN_BUDGET) (hmin : Fl4MinBudgetAgreeA ar a) + (T : EntryTimeoutA (st := st) (ar := ar) Z m κ Clock s a σ t) (can : Bool) : + sched.end_timeslice st ar s a can ⦃ r => ∃ τ Q' R', + fl4EndTimeslice (Z := Unit) can σ = ({((), τ)}, False) ∧ + YieldQueueRelA (st := st) { Z with Q := Q', R := R' } m κ r.1 τ ∧ EndTimeA Clock σ r.2 τ ⦄ := by + have hTC := H.sch.curThread.trans (congrArg specPtr hct) + obtain ⟨y0, n0, hy0, R⟩ := H.scr.sc sc hc + rw [hky] at hy0; cases hy0 + have code : L.mem s (stA t) = sched.TS_RUNNING_VM ↔ x.tcbState = .ext .runningVM := by + have e : sched.TS_RUNNING_VM = 8#u64 := by unfold sched.TS_RUNNING_VM; rfl + have eqVal (u v : U64) : u = v ↔ u.val = v.val := + ⟨congrArg UScalar.val, UScalar.eq_of_val_eq⟩ + rw [e, eqVal, E t ht x hkx] + cases h : x.tcbState with + | ext q => cases q; simp [tsCodeA] + | _ => simp [tsCodeA] + let check := if x.tcbState = .ext .runningVM then NonDet.pure false + else isValidTimeoutHandler (Z := Unit) (specPtr t) + have guard : (do + let word ← obj.Tcb.state st t s + if word != sched.TS_RUNNING_VM then faults.valid_timeout_handler st s t else ok false) ⦃ b => + check σ = ({(b, σ)}, False) ⦄ := by + refine WP.spec_bind (obj.Tcb.state.spec s t) ?_ + intro w hw + rw [hw] + by_cases hv : x.tcbState = .ext .runningVM + · have eq := code.2 hv + simp [eq, check, hv, NonDet.pure] + · have ne := fun e => hv (code.1 e) + simpa only [bne_iff_ne, if_pos ne, check, if_neg hv] using T.valid + have spec (v : Bool) (hv : check σ = ({(v, σ)}, False)) : + fl4EndTimeslice (Z := Unit) can σ = + (if can = true ∧ ¬ decide (y.scPeriod = 0) = true ∧ v = true then + fl4HandleTimeout (specPtr t) (.timeout y.scBadge) σ + else entryRequeueA (scNameA m sc) (specPtr t) σ) := by + unfold fl4EndTimeslice + rw [nd_gets_bindSc, hcur, nd_bind_oneSc (isRoundRobin_eqA hky), nd_gets_bindSc, hTC] + unfold getThreadState + rw [nd_bind_oneSc (threadGet_eqSc _ hkx)] + let next (b : Bool) : SMonad Unit Unit := + if can = true ∧ ¬ decide (y.scPeriod = 0) = true ∧ b = true then do + let sc ← getSchedContext (scNameA m sc) + fl4HandleTimeout (specPtr t) (.timeout sc.scBadge) + else entryRequeueA (scNameA m sc) (specPtr t) + have ec : (match x.tcbState with + | .ext .runningVM => (pure false : SMonad Unit Bool) >>= next + | _ => isValidTimeoutHandler (Z := Unit) (specPtr t) >>= next) = (check >>= next) := by + cases ht : x.tcbState <;> simp [check, ht] + rename_i t + cases t + rfl + change (match x.tcbState with + | .ext .runningVM => (pure false : SMonad Unit Bool) >>= next + | _ => isValidTimeoutHandler (Z := Unit) (specPtr t) >>= next) σ = _ + rw [ec, nd_bind_oneSc hv] + unfold next + split_ifs + · rw [nd_bind_oneSc (getSchedContext_eqA hky)] + · rfl + have queues := entryRequeue_refinesA H a E M Y ht hc hsc0 hon hcore how hsc hq hnq hkx hb hs hky ha hag hmin + unfold sched.end_timeslice + refine WP.spec_bind (sched.cur_sc.spec s) ?_ + intro c ec + rw [ec.trans hcs] + refine WP.spec_bind (sched.cur_thread.spec s) ?_ + intro v ev + rw [ev.trans hct] + refine WP.spec_bind (is_round_robin_refinesA H.scr hc hky) ?_ + intro rr hrr + rw [← cfgResultAssocA] + refine WP.spec_bind guard ?_ + intro b hb' + rw [isRoundRobin_eqA hky] at hrr + have e := (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst hrr))).1 + rw [← e] + have noTimeout (hn : ¬ (can = true ∧ ¬ decide (y.scPeriod = 0) = true ∧ b = true)) := + WP.spec_mono queues (fun (r : S × A) ⟨τ, Q', R', he, hS, ha', hm, ht', hC, hk, hI, _⟩ => + (show ∃ τ Q' R', fl4EndTimeslice (Z := Unit) can σ = ({((), τ)}, False) ∧ + YieldQueueRelA (st := st) { Z with Q := Q', R := R' } m κ r.1 τ ∧ EndTimeA Clock σ r.2 τ from + ⟨τ, Q', R', (spec b hb').trans (by rw [if_neg hn]; exact he), hS, + ⟨by rw [ha', hm]; exact clock, ht', hm, hC, endTimeIdleA I hS.sched.run hk hI⟩⟩)) + by_cases hCan : can = true + · rw [if_pos hCan] + by_cases hR : decide (y.scPeriod = 0) = true + · rw [if_pos hR] + exact noTimeout (fun h => h.2.1 hR) + · rw [if_neg hR] + by_cases hB : b = true + · rw [if_pos hB] + refine WP.spec_bind (show obj.Sc.badge st sc s ⦃ d => d = L.mem s (scWA sc 48#u64) ⦄ from by + unfold obj.Sc.badge scWA layout.sc.BADGE + simp only [lift, bind_ok, L.read_eq, WP.spec_ok]) ?_ + intro d hd + have hD : d.bv = y.scBadge := by rw [hd, R.badge] + have hv : isValidTimeoutHandler (Z := Unit) (specPtr t) σ = ({(true, σ)}, False) := by + have hx : x.tcbState ≠ .ext .runningVM := by + intro hVM + simp [check, hVM, hB, NonDet.pure, Prod.mk.injEq] at hb' + simpa only [check, if_neg hx, hB] using hb' + refine WP.spec_mono (T.send d hv) ?_ + rintro r ⟨τ, Q', R', he, hS, cT⟩ + refine ⟨τ, Q', R', ?_, hS, cT⟩ + rw [spec b hb', if_pos ⟨hCan, hR, hB⟩, ← hD] + exact he + · rw [if_neg hB] + exact noTimeout (fun h => hB h.2.2) + · rw [if_neg hCan] + exact noTimeout (fun h => hCan h.1) + +set_option maxRecDepth 2048 in +/-- **`end_timeslice_refinesA` at `can = false`, with the frame** (queue footprint of `entryRequeue_refinesA`): the +timeout leaf is read (validity) but never sent. -/ +theorem end_timeslice_noSendA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + {t sc : U64} (H : SchedSRB st Z m κ s σ (specPtr t)) (a : A) + {Clock : A → MachineState → Prop} (clock : Clock a σ.machineState) (I : ActIdleA σ) + (E : StEqA Z.T (L.mem s) σ) (M : McpRelA Z.T m (L.mem s) σ) (Y : YieldRelA Z.T Z.C m (L.mem s) σ) + (ht : t ∈ Z.T) (hc : sc ∈ Z.C) (hsc0 : sc ≠ 0#u64) + (hct : L.curW s = t) (hcs : L.cscW s = sc) (hcur : σ.curSc = scNameA m sc) + (hon : L.mem s (affA t) = L.cpu s) (hcore : L.mem s (scCoreA sc) = L.cpu s) + (how : L.mem s (scTcbA sc) = t) (hsc : L.mem s (scA t) = sc) + (hq : (L.mem s (inqA t) &&& sched.Q_QUEUED) = 0#u64) (hnq : ∀ d p, specPtr t ∉ σ.readyQueues d p) + {x : Tcb} (hkx : σ.kheap (specPtr t) = some (.tcb x)) + (hb : x.tcbSchedContext = some (scNameA m sc)) (hs : schedulable (specPtr t) σ = true) + {y : SchedContext} {n : Nat} (hky : σ.kheap (scNameA m sc) = some (.schedContext y n)) + (ha : scActive y = true) (hag : fl4MinBudget = MIN_BUDGET) (hmin : Fl4MinBudgetAgreeA ar a) + (T : EntryValidA (st := st) s σ t) : + sched.end_timeslice st ar s a false ⦃ r => ∃ τ Q' R', + fl4EndTimeslice (Z := Unit) false σ = ({((), τ)}, False) ∧ + YieldQueueRelA (st := st) { Z with Q := Q', R := R' } m κ r.1 τ ∧ EndTimeA Clock σ r.2 τ ∧ + MFrA (st := st) Z m s r.1 σ τ ⦄ := by + have hTC := H.sch.curThread.trans (congrArg specPtr hct) + obtain ⟨y0, n0, hy0, R⟩ := H.scr.sc sc hc + rw [hky] at hy0; cases hy0 + have code : L.mem s (stA t) = sched.TS_RUNNING_VM ↔ x.tcbState = .ext .runningVM := by + have e : sched.TS_RUNNING_VM = 8#u64 := by unfold sched.TS_RUNNING_VM; rfl + have eqVal (u v : U64) : u = v ↔ u.val = v.val := + ⟨congrArg UScalar.val, UScalar.eq_of_val_eq⟩ + rw [e, eqVal, E t ht x hkx] + cases h : x.tcbState with + | ext q => cases q; simp [tsCodeA] + | _ => simp [tsCodeA] + let check := if x.tcbState = .ext .runningVM then NonDet.pure false + else isValidTimeoutHandler (Z := Unit) (specPtr t) + have guard : (do + let word ← obj.Tcb.state st t s + if word != sched.TS_RUNNING_VM then faults.valid_timeout_handler st s t else ok false) ⦃ b => + check σ = ({(b, σ)}, False) ⦄ := by + refine WP.spec_bind (obj.Tcb.state.spec s t) ?_ + intro w hw + rw [hw] + by_cases hv : x.tcbState = .ext .runningVM + · have eq := code.2 hv + simp [eq, check, hv, NonDet.pure] + · have ne := fun e => hv (code.1 e) + simpa only [bne_iff_ne, if_pos ne, check, if_neg hv] using T + have spec (v : Bool) (hv : check σ = ({(v, σ)}, False)) : + fl4EndTimeslice (Z := Unit) false σ = + (if false = true ∧ ¬ decide (y.scPeriod = 0) = true ∧ v = true then + fl4HandleTimeout (specPtr t) (.timeout y.scBadge) σ + else entryRequeueA (scNameA m sc) (specPtr t) σ) := by + unfold fl4EndTimeslice + rw [nd_gets_bindSc, hcur, nd_bind_oneSc (isRoundRobin_eqA hky), nd_gets_bindSc, hTC] + unfold getThreadState + rw [nd_bind_oneSc (threadGet_eqSc _ hkx)] + let next (b : Bool) : SMonad Unit Unit := + if false = true ∧ ¬ decide (y.scPeriod = 0) = true ∧ b = true then do + let sc ← getSchedContext (scNameA m sc) + fl4HandleTimeout (specPtr t) (.timeout sc.scBadge) + else entryRequeueA (scNameA m sc) (specPtr t) + have ec : (match x.tcbState with + | .ext .runningVM => (pure false : SMonad Unit Bool) >>= next + | _ => isValidTimeoutHandler (Z := Unit) (specPtr t) >>= next) = (check >>= next) := by + cases ht : x.tcbState <;> simp [check, ht] + rename_i t + cases t + rfl + change (match x.tcbState with + | .ext .runningVM => (pure false : SMonad Unit Bool) >>= next + | _ => isValidTimeoutHandler (Z := Unit) (specPtr t) >>= next) σ = _ + rw [ec, nd_bind_oneSc hv] + unfold next + split_ifs <;> first | rfl | (exfalso; simp_all) + have queues := entryRequeue_refinesA H a E M Y ht hc hsc0 hon hcore how hsc hq hnq hkx hb hs hky ha hag hmin + unfold sched.end_timeslice + refine WP.spec_bind (sched.cur_sc.spec s) ?_ + intro c ec + rw [ec.trans hcs] + refine WP.spec_bind (sched.cur_thread.spec s) ?_ + intro v ev + rw [ev.trans hct] + refine WP.spec_bind (is_round_robin_refinesA H.scr hc hky) ?_ + intro rr hrr + rw [← cfgResultAssocA] + refine WP.spec_bind guard ?_ + intro b hb' + rw [isRoundRobin_eqA hky] at hrr + have e := (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst hrr))).1 + rw [← e] + have noTimeout (hn : ¬ (false = true ∧ ¬ decide (y.scPeriod = 0) = true ∧ b = true)) := + WP.spec_mono queues (fun (r : S × A) ⟨τ, Q', R', he, hS, ha', hm, ht', hC, hk, hI, fr⟩ => + (show ∃ τ Q' R', fl4EndTimeslice (Z := Unit) false σ = ({((), τ)}, False) ∧ + YieldQueueRelA (st := st) { Z with Q := Q', R := R' } m κ r.1 τ ∧ EndTimeA Clock σ r.2 τ ∧ + MFrA (st := st) Z m s r.1 σ τ from + ⟨τ, Q', R', (spec b hb').trans (by rw [if_neg hn]; exact he), hS, + ⟨by rw [ha', hm]; exact clock, ht', hm, hC, endTimeIdleA I hS.sched.run hk hI⟩, fr⟩)) + simp only [Bool.false_eq_true, false_and, ↓reduceIte] + exact noTimeout (by simp) + +/-- The spec's timeout send of `fl4EndTimeslice` is not taken at `σ` for the current TCB `t` and SC `y`: the SC is +round robin, the thread runs a VM, or its timeout handler is not valid. -/ +def NoSendAtA (σ : State Unit) (t : ObjRef) (y : SchedContext) (x : Tcb) : Prop := + y.scPeriod = 0 ∨ x.tcbState = .ext .runningVM ∨ isValidTimeoutHandler (Z := Unit) t σ = ({(false, σ)}, False) + +private theorem nd_true_ne_falseA {σ : State Unit} : + (({(true, σ)}, False) : Set (Bool × State Unit) × Prop) ≠ ({(false, σ)}, False) := fun h => by + have e := (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst h))).1 + cases e + +set_option maxRecDepth 2048 in +/-- **`end_timeslice` at any `can` without the timeout send, with the frame**: `end_timeslice_noSendA` where `can = true` +is allowed when the spec's send condition fails (`NoSendAtA`). Both spec outcomes (`can` and `false`) are the requeue. -/ +theorem end_timeslice_noSendTA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + {t sc : U64} (H : SchedSRB st Z m κ s σ (specPtr t)) (a : A) + {Clock : A → MachineState → Prop} (clock : Clock a σ.machineState) (I : ActIdleA σ) + (E : StEqA Z.T (L.mem s) σ) (M : McpRelA Z.T m (L.mem s) σ) (Y : YieldRelA Z.T Z.C m (L.mem s) σ) + (ht : t ∈ Z.T) (hc : sc ∈ Z.C) (hsc0 : sc ≠ 0#u64) + (hct : L.curW s = t) (hcs : L.cscW s = sc) (hcur : σ.curSc = scNameA m sc) + (hon : L.mem s (affA t) = L.cpu s) (hcore : L.mem s (scCoreA sc) = L.cpu s) + (how : L.mem s (scTcbA sc) = t) (hsc : L.mem s (scA t) = sc) + (hq : (L.mem s (inqA t) &&& sched.Q_QUEUED) = 0#u64) (hnq : ∀ d p, specPtr t ∉ σ.readyQueues d p) + {x : Tcb} (hkx : σ.kheap (specPtr t) = some (.tcb x)) + (hb : x.tcbSchedContext = some (scNameA m sc)) (hs : schedulable (specPtr t) σ = true) + {y : SchedContext} {n : Nat} (hky : σ.kheap (scNameA m sc) = some (.schedContext y n)) + (ha : scActive y = true) (hag : fl4MinBudget = MIN_BUDGET) (hmin : Fl4MinBudgetAgreeA ar a) + (T : EntryValidA (st := st) s σ t) (can : Bool) + (hns : can = true → NoSendAtA σ (specPtr t) y x) : + sched.end_timeslice st ar s a can ⦃ r => ∃ τ Q' R', + fl4EndTimeslice (Z := Unit) can σ = ({((), τ)}, False) ∧ + fl4EndTimeslice (Z := Unit) false σ = ({((), τ)}, False) ∧ + YieldQueueRelA (st := st) { Z with Q := Q', R := R' } m κ r.1 τ ∧ EndTimeA Clock σ r.2 τ ∧ + MFrA (st := st) Z m s r.1 σ τ ∧ τ.kheap = σ.kheap ∧ + entryRequeueA (scNameA m sc) (specPtr t) σ = ({((), τ)}, False) ∧ r.2 = a ⦄ := by + have hTC := H.sch.curThread.trans (congrArg specPtr hct) + obtain ⟨y0, n0, hy0, R⟩ := H.scr.sc sc hc + rw [hky] at hy0; cases hy0 + have code : L.mem s (stA t) = sched.TS_RUNNING_VM ↔ x.tcbState = .ext .runningVM := by + have e : sched.TS_RUNNING_VM = 8#u64 := by unfold sched.TS_RUNNING_VM; rfl + have eqVal (u v : U64) : u = v ↔ u.val = v.val := + ⟨congrArg UScalar.val, UScalar.eq_of_val_eq⟩ + rw [e, eqVal, E t ht x hkx] + cases h : x.tcbState with + | ext q => cases q; simp [tsCodeA] + | _ => simp [tsCodeA] + let check := if x.tcbState = .ext .runningVM then NonDet.pure false + else isValidTimeoutHandler (Z := Unit) (specPtr t) + have guard : (do + let word ← obj.Tcb.state st t s + if word != sched.TS_RUNNING_VM then faults.valid_timeout_handler st s t else ok false) ⦃ b => + check σ = ({(b, σ)}, False) ⦄ := by + refine WP.spec_bind (obj.Tcb.state.spec s t) ?_ + intro w hw + rw [hw] + by_cases hv : x.tcbState = .ext .runningVM + · have eq := code.2 hv + simp [eq, check, hv, NonDet.pure] + · have ne := fun e => hv (code.1 e) + simpa only [bne_iff_ne, if_pos ne, check, if_neg hv] using T + have spec (c : Bool) (v : Bool) (hv : check σ = ({(v, σ)}, False)) : + fl4EndTimeslice (Z := Unit) c σ = + (if c = true ∧ ¬ decide (y.scPeriod = 0) = true ∧ v = true then + fl4HandleTimeout (specPtr t) (.timeout y.scBadge) σ + else entryRequeueA (scNameA m sc) (specPtr t) σ) := by + unfold fl4EndTimeslice + rw [nd_gets_bindSc, hcur, nd_bind_oneSc (isRoundRobin_eqA hky), nd_gets_bindSc, hTC] + unfold getThreadState + rw [nd_bind_oneSc (threadGet_eqSc _ hkx)] + let next (b : Bool) : SMonad Unit Unit := + if c = true ∧ ¬ decide (y.scPeriod = 0) = true ∧ b = true then do + let sc ← getSchedContext (scNameA m sc) + fl4HandleTimeout (specPtr t) (.timeout sc.scBadge) + else entryRequeueA (scNameA m sc) (specPtr t) + have ec : (match x.tcbState with + | .ext .runningVM => (pure false : SMonad Unit Bool) >>= next + | _ => isValidTimeoutHandler (Z := Unit) (specPtr t) >>= next) = (check >>= next) := by + cases ht : x.tcbState <;> simp [check, ht] + rename_i t + cases t + rfl + change (match x.tcbState with + | .ext .runningVM => (pure false : SMonad Unit Bool) >>= next + | _ => isValidTimeoutHandler (Z := Unit) (specPtr t) >>= next) σ = _ + rw [ec, nd_bind_oneSc hv] + unfold next + split_ifs + · rw [nd_bind_oneSc (getSchedContext_eqA hky)] + · rfl + have queues := entryRequeue_refinesA H a E M Y ht hc hsc0 hon hcore how hsc hq hnq hkx hb hs hky ha hag hmin + unfold sched.end_timeslice + refine WP.spec_bind (sched.cur_sc.spec s) ?_ + intro c ec + rw [ec.trans hcs] + refine WP.spec_bind (sched.cur_thread.spec s) ?_ + intro v ev + rw [ev.trans hct] + refine WP.spec_bind (is_round_robin_refinesA H.scr hc hky) ?_ + intro rr hrr + rw [← cfgResultAssocA] + refine WP.spec_bind guard ?_ + intro b hb' + rw [isRoundRobin_eqA hky] at hrr + have e := (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst hrr))).1 + rw [← e] + have noTimeout (hn : ¬ (can = true ∧ ¬ decide (y.scPeriod = 0) = true ∧ b = true)) := + WP.spec_mono queues (fun (r : S × A) ⟨τ, Q', R', he, hS, ha', hm, ht', hC, hk, hI, fr⟩ => + (show ∃ τ Q' R', fl4EndTimeslice (Z := Unit) can σ = ({((), τ)}, False) ∧ + fl4EndTimeslice (Z := Unit) false σ = ({((), τ)}, False) ∧ + YieldQueueRelA (st := st) { Z with Q := Q', R := R' } m κ r.1 τ ∧ EndTimeA Clock σ r.2 τ ∧ + MFrA (st := st) Z m s r.1 σ τ ∧ τ.kheap = σ.kheap ∧ + entryRequeueA (scNameA m sc) (specPtr t) σ = ({((), τ)}, False) ∧ r.2 = a from + ⟨τ, Q', R', (spec can b hb').trans (by rw [if_neg hn]; exact he), + (spec false b hb').trans (by rw [if_neg (fun h => Bool.false_ne_true h.1)]; exact he), hS, + ⟨by rw [ha', hm]; exact clock, ht', hm, hC, endTimeIdleA I hS.sched.run hk hI⟩, fr, hk, he, ha'⟩)) + by_cases hCan : can = true + · rw [if_pos hCan] + by_cases hR : decide (y.scPeriod = 0) = true + · rw [if_pos hR] + exact noTimeout (fun h => h.2.1 hR) + · rw [if_neg hR] + by_cases hB : b = true + · exfalso + have hx : x.tcbState ≠ .ext .runningVM := by + intro hVM + simp [check, hVM, hB, NonDet.pure, Prod.mk.injEq] at hb' + have hv : isValidTimeoutHandler (Z := Unit) (specPtr t) σ = ({(true, σ)}, False) := by + simpa only [check, if_neg hx, hB] using hb' + rcases hns hCan with h | h | h + · exact hR (decide_eq_true h) + · exact hx h + · exact nd_true_ne_falseA (hv.symm.trans h) + · rw [if_neg hB] + exact noTimeout (fun h => hB h.2.2) + · rw [if_neg hCan] + exact noTimeout (fun h => hCan h.1) + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScEntrySendA.lean b/proofs/Fl4ProofsA64/P3/ScEntrySendA.lean new file mode 100644 index 000000000..e58f956d1 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScEntrySendA.lean @@ -0,0 +1,326 @@ +/- +P3 (aarch64, A64-P3 Stage 3, kernel-entry composition, step 2): **the budget charge's timeout send to a waiting +receiver, with its frame** (`end_timeslice … true` / `charge_budget … true` taking `handle_timeout`). + +* **`end_timeslice_sendRecvA`**: at `can = true`, an SC that is not round robin and a current thread not running a VM + whose timeout handler is an endpoint cap with a receiver waiting: the program runs `handle_timeout` + (`P3/Stage3TimeoutSendBA`'s `handle_timeout_recvBA`), and `fl4EndTimeslice true` is the spec's `fl4HandleTimeout` + (one outcome). +* **`charge_budget_sendRecvFrA`**: `charge_budget_noSendFrA`'s sibling on that branch: the charge head, the send, then + `reschedule_required` / `set_reprogram`. Exports the spec steps (`ChargeSendStepsA`), `YieldQueueRelA`, + `EpStateRel` and the write set from the entry state (`MFrA`'s, the current thread's fault words and `IpcFpA`). + +Named premises: `handle_timeout_recvBA`'s [P] `hmax`, `htu`, `hconv`; its case selectors (`hslot`, `hcap`, `hgc`: the +timeout handler cap; `he`, `hk`, `hd`, `hkd`, `hon`: the receiver waiting on the current core) and record inputs. New +module. +-/ +import Fl4ProofsA64.P3.ScEntryBudgetA +import Fl4ProofsA64.P3.Stage3TimeoutSendBA +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3.Sched + +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + +noncomputable section + +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +set_option maxRecDepth 2048 in +set_option maxHeartbeats 1000000 in +/-- **`end_timeslice … true` reaching `handle_timeout`**: at an SC that is not round robin and a current thread not +running a VM whose timeout handler is an endpoint cap, the program is `handle_timeout` with the SC's badge, and the +spec is `fl4HandleTimeout`. -/ +theorem end_timeslice_timeoutA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} {t sc : U64} (a : A) + (H : SchedSRB st Z m κ s σ (specPtr t)) (E0 : StEqA Z.T (L.mem s) σ) + (ht : t ∈ Z.T) (hc : sc ∈ Z.C) + (hct : L.curW s = t) (hcs : L.cscW s = sc) (hcur : σ.curSc = scNameA m sc) + {x : Tcb} (hkx : σ.kheap (specPtr t) = some (.tcb x)) + (hnvm : x.tcbState ≠ .ext .runningVM) + {y : SchedContext} {n : Nat} (hky : σ.kheap (scNameA m sc) = some (.schedContext y n)) + (hrr : y.scPeriod ≠ 0) + {slot ep hbg : U64} {sd rv g gr : Bool} + (hslot : obj.Tcb.slot t layout.tcb.SLOT_TIMEOUT_HANDLER = ok slot) + (hcap : obj.Slot.cap st slot s = ok (.Endpoint ep hbg sd rv g gr)) + (hgc : getCap (Z := Unit) (getTcbTimeoutHandlerPtr (specPtr t)) σ = + ({(.endpointCap (specPtr ep) hbg.bv (rightsOfA sd rv g gr), σ)}, False)) + {Post : S × A → Prop} + (TMO : ∀ bd : U64, bd.bv = y.scBadge → faults.handle_timeout st ar s a t (.Timeout bd) ⦃ r => Post r ⦄) : + sched.end_timeslice st ar s a true ⦃ r => Post r ⦄ ∧ + fl4EndTimeslice (Z := Unit) true σ = fl4HandleTimeout (Z := Unit) (specPtr t) (.timeout y.scBadge) σ := by + have hTC := H.sch.curThread.trans (congrArg specPtr hct) + obtain ⟨y0, n0, hy0, Rs⟩ := H.scr.sc sc hc + rw [hky] at hy0; cases hy0 + have code : L.mem s (stA t) = sched.TS_RUNNING_VM ↔ x.tcbState = .ext .runningVM := by + have e : sched.TS_RUNNING_VM = 8#u64 := by unfold sched.TS_RUNNING_VM; rfl + have eqVal (u v : U64) : u = v ↔ u.val = v.val := + ⟨congrArg UScalar.val, UScalar.eq_of_val_eq⟩ + rw [e, eqVal, E0 t ht x hkx] + cases h : x.tcbState with + | ext q => cases q; simp [tsCodeA] + | _ => simp [tsCodeA] + have hv : isValidTimeoutHandler (Z := Unit) (specPtr t) σ = ({(true, σ)}, False) := by + unfold isValidTimeoutHandler + rw [nd_pure_bindSc, nd_bind_oneSc hgc] + rfl + have spec : fl4EndTimeslice (Z := Unit) true σ = fl4HandleTimeout (specPtr t) (.timeout y.scBadge) σ := by + unfold fl4EndTimeslice + rw [nd_gets_bindSc, hcur, nd_bind_oneSc (isRoundRobin_eqA hky), nd_gets_bindSc, hTC] + unfold getThreadState + rw [nd_bind_oneSc (threadGet_eqSc _ hkx)] + let next (b : Bool) : SMonad Unit Unit := + if true = true ∧ ¬ decide (y.scPeriod = 0) = true ∧ b = true then do + let sc ← getSchedContext (scNameA m sc) + fl4HandleTimeout (specPtr t) (.timeout sc.scBadge) + else entryRequeueA (scNameA m sc) (specPtr t) + have ec : (match x.tcbState with + | .ext .runningVM => (pure false : SMonad Unit Bool) >>= next + | _ => isValidTimeoutHandler (Z := Unit) (specPtr t) >>= next) = + (isValidTimeoutHandler (Z := Unit) (specPtr t) >>= next) := by + cases hts : x.tcbState with + | ext q => cases q; exact absurd hts hnvm + | _ => rfl + change (match x.tcbState with + | .ext .runningVM => (pure false : SMonad Unit Bool) >>= next + | _ => isValidTimeoutHandler (Z := Unit) (specPtr t) >>= next) σ = _ + rw [ec, nd_bind_oneSc hv] + unfold next + rw [if_pos ⟨rfl, by simpa using hrr, rfl⟩, nd_bind_oneSc (getSchedContext_eqA hky)] + have guard : (do + let word ← obj.Tcb.state st t s + if word != sched.TS_RUNNING_VM then faults.valid_timeout_handler st s t else ok false) ⦃ b => + b = true ⦄ := by + refine WP.spec_bind (obj.Tcb.state.spec s t) ?_ + intro w hw + rw [hw] + have ne : L.mem s (stA t) ≠ sched.TS_RUNNING_VM := fun e => hnvm (code.1 e) + rw [if_pos (bne_iff_ne.2 ne)] + unfold faults.valid_timeout_handler + rw [hslot, bind_ok, hcap, bind_ok] + exact (WP.spec_ok _).2 rfl + refine ⟨?_, spec⟩ + unfold sched.end_timeslice + refine WP.spec_bind (sched.cur_sc.spec s) ?_ + intro c ec + rw [ec.trans hcs] + refine WP.spec_bind (sched.cur_thread.spec s) ?_ + intro v ev + rw [ev.trans hct] + refine WP.spec_bind (is_round_robin_refinesA H.scr hc hky) ?_ + intro rr hrr' + rw [← cfgResultAssocA] + refine WP.spec_bind guard ?_ + intro b hb' + subst hb' + rw [isRoundRobin_eqA hky] at hrr' + have e := (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst hrr'))).1 + rw [← e] + have hR : ¬ decide (y.scPeriod = 0) = true := by simpa using hrr + simp only [↓reduceIte, hR, Bool.false_eq_true] + refine WP.spec_bind (show obj.Sc.badge st sc s ⦃ d => d = L.mem s (scWA sc 48#u64) ⦄ from by + unfold obj.Sc.badge scWA layout.sc.BADGE + simp only [lift, bind_ok, L.read_eq, WP.spec_ok]) ?_ + intro bd hbd + have hBd : bd.bv = y.scBadge := by rw [hbd, Rs.badge] + exact TMO bd hBd + +set_option maxRecDepth 2048 in +set_option maxHeartbeats 1000000 in +/-- **`end_timeslice` taking the timeout send to a waiting receiver** (module docstring). -/ +theorem end_timeslice_sendRecvA {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} + {Bf : Set (U64 × Word)} {s : S} {σ : State Unit} {t sc : U64} (a : A) + (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + {kb : U64} (hPl : PlaceA Z.th kb) (hSt : StateRelA Z.th (L.mem s) σ) (hV : SlotsValidA Z.th (L.mem s)) + (hC : BufCover st Z.T Bf s σ) (hNative : BufNativeA Z.T (L.mem s)) + (hoff : ∀ y, IsSlotA Z.th y → ¬ EWrite Z.T E R Bf y ∧ ¬ EWrite Z.T E R Bf (core.num.U64.wrapping_add y 8#u64)) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) (hconv : Fl4TicksToUsLegacyAgree) + (H : SchedSRB st Z m κ s σ (specPtr t)) (E0 : StEqA Z.T (L.mem s) σ) (hM : McpRelA Z.T m (L.mem s) σ) + (hY : YieldRelA Z.T Z.C m (L.mem s) σ) + (ht : t ∈ Z.T) (hc : sc ∈ Z.C) (hsc0 : sc ≠ 0#u64) + (hct : L.curW s = t) (hcs : L.cscW s = sc) (hcur : σ.curSc = scNameA m sc) (hname : scNameA m sc = specPtr sc) + (hsc : L.mem s (scA t) = sc) + {x : Tcb} (hkx : σ.kheap (specPtr t) = some (.tcb x)) (hb : x.tcbSchedContext = some (scNameA m sc)) + (hnvm : x.tcbState ≠ .ext .runningVM) (hRun : SndRunA σ (specPtr t) x) (hD : DonA σ (specPtr t) x) + {y : SchedContext} {n : Nat} (hky : σ.kheap (scNameA m sc) = some (.schedContext y n)) + (hrr : y.scPeriod ≠ 0) + {slot ep hbg : U64} {sd rv g gr : Bool} + (hslot : obj.Tcb.slot t layout.tcb.SLOT_TIMEOUT_HANDLER = ok slot) + (hcap : obj.Slot.cap st slot s = ok (.Endpoint ep hbg sd rv g gr)) + (hgc : getCap (Z := Unit) (getTcbTimeoutHandlerPtr (specPtr t)) σ = + ({(.endpointCap (specPtr ep) hbg.bv (rightsOfA sd rv g gr), σ)}, False)) + (he : ep ∈ E) {d : U64} {q : List ObjRef} + (hk : σ.kheap (specPtr ep) = some (.endpoint (.recvEP (specPtr d :: q)))) + (hd : d ∈ Z.T) {xd : Tcb} (hkd : σ.kheap (specPtr d) = some (.tcb xd)) + (hu : ∀ rp, ipcBufOfA true xd = some rp → ∀ k, k < 128 → inUserFrame (bufAddr rp k) σ) + (hal : ∀ rb, ipcBufOfA true xd = some rb → (bufAddr rb 0).toNat % 8 = 0) + (hrep : RepOKA σ (specPtr d)) (hon : L.mem s (affA d) = L.cpu s) + (hni : ∀ c < fl4A64Platform.numNodes, specPtr t ≠ m.idleThreadOf c) : + sched.end_timeslice st ar s a true ⦃ r => r.2 = a ∧ ∃ τ, + fl4EndTimeslice (Z := Unit) true σ = ({((), τ)}, False) ∧ + fl4HandleTimeout (Z := Unit) (specPtr t) (.timeout y.scBadge) σ = ({((), τ)}, False) ∧ + sendIpc (Z := Unit) true false hbg.bv g gr false (specPtr t) (specPtr ep) + (updK σ (specPtr t) (.tcb { x with tcbFault := some (.timeout y.scBadge) })) = ({((), τ)}, False) ∧ + (∃ Q' R', YieldQueueRelA (st := st) { Z with Q := Q', R := R' } m κ r.1 τ) ∧ + EpStateRel Z.T E R Bf (L.mem r.1) τ ∧ + WritesIn st s r.1 (fun b => FaultWA t b ∨ Endp.TmoFpA st Z m R Bf σ s t ep d b) ∧ SSzA σ τ ⦄ := by + -- the handler's inputs in the `specPtr` form + have hsc' : x.tcbSchedContext = some (specPtr (L.mem s (scA t))) := by rw [hb, hname, hsc] + have hky' : σ.kheap (specPtr (L.mem s (scA t))) = some (.schedContext y n) := by rw [hsc, ← hname]; exact hky + have hsc0' : L.mem s (scA t) ≠ 0#u64 := by rw [hsc]; exact hsc0 + have tail := fun bd (hBd : bd.bv = y.scBadge) => + handle_timeout_recvBA (st := st) (ar := ar) a P PE HE hPl hSt hV hC hNative hoff hmax htu hconv H hM hY ht hkx + hRun hD hslot hcap hgc he hk hd hkd hu hal hrep hon hni hsc0' hsc' hky' bd + have G := fun {Post : S × A → Prop} (TMO : ∀ bd : U64, bd.bv = y.scBadge → + faults.handle_timeout st ar s a t (.Timeout bd) ⦃ r => Post r ⦄) => + end_timeslice_timeoutA (st := st) (ar := ar) a H E0 ht hc hct hcs hcur hkx hnvm hky hrr hslot hcap hgc TMO + have spec := (G (Post := fun _ => True) fun bd hBd => WP.spec_mono (tail bd hBd) fun _ _ => trivial).2 + refine (G fun bd hBd => WP.spec_mono (tail bd hBd) ?_).1 + rintro r ⟨ha, τ, hsp, hsi, hQ, HE', hw, hz⟩ + rw [hBd] at hsp hsi + exact ⟨ha, τ, spec.trans hsp, hsp, hsi, hQ, HE', hw, hz⟩ + +variable (st) in +/-- **The facts after `charge_budget`'s head** (the charge and the consumed reset) that the timeout send gets. -/ +structure ChargeHeadA (Z : XSetsA) (m : McState) (κ : U64 → U64) (used : Ticks) (s : S) (σ : State Unit) (s2 : S) + (τ : State Unit) : Prop where + head : ∃ y n, fl4ChargeHead (Z := Unit) σ.curSc used σ = ({((), updKSc σ σ.curSc (.schedContext y n))}, False) ∧ + τ = { updKSc σ σ.curSc (.schedContext y n) with consumedTime := 0 } + frame : MFrA (st := st) Z m s s2 σ τ + sr : SchedSRB st Z m κ s2 τ τ.curThread + stq : StEqA Z.T (L.mem s2) τ + mcp : McpRelA Z.T m (L.mem s2) τ + yr : YieldRelA Z.T Z.C m (L.mem s2) τ + keep : ScChargeKeepA σ τ + run : schedulable τ.curThread τ = true + cur : τ.curThread = σ.curThread + csc : τ.curSc = σ.curSc + act : ∀ z k, σ.kheap σ.curSc = some (.schedContext z k) → scActive z = true + +set_option maxRecDepth 4096 in +set_option maxHeartbeats 1600000 in +/-- **`charge_budget … true` taking the timeout send, with the frame** (module docstring): the head as +`charge_budget_noSendFrA`, `end_timeslice` by the callback `SEND` at the head's state, then `reschedule_required` and +`set_reprogram` (`entryRescheduleFrA`). The current thread is schedulable after the head (`hsch`). -/ +theorem charge_budget_sendFrA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (E : YieldQueueRelA (st := st) Z m κ s σ) (I : CurInvA σ) (Idle : ActIdleA σ) + (P : EntryBudgetPlaceA st m κ s σ) (a : A) + (hag : fl4MinBudget = MIN_BUDGET) (hMin : Fl4MinBudgetAgreeA ar a) + (hLimit : sporadic.max_release_time ar a ⦃ v => v.bv = fl4MaxReleaseTime ⦄) (used : U64) + (hsch : ∀ σ1, fl4ChargeHead (Z := Unit) σ.curSc used.bv σ = ({((), σ1)}, False) → + schedulable σ.curThread { σ1 with consumedTime := 0 } = true) + {Post : S → State Unit → Prop} + (SEND : ∀ s2 τ (sc : U64) (x : Tcb) (y : SchedContext) (n : Nat), ChargeHeadA st Z m κ used.bv s σ s2 τ → + sc ∈ Z.C → L.cscW s2 = sc → σ.curSc = scNameA m sc → sc ≠ 0#u64 → L.mem s2 (scA (L.curW s2)) = sc → + τ.kheap (specPtr (L.curW s2)) = some (.tcb x) → x.tcbSchedContext = some (scNameA m sc) → + τ.kheap (scNameA m sc) = some (.schedContext y n) → + sched.end_timeslice st ar s2 a true ⦃ r => r.2 = a ∧ ∃ τ3, + fl4EndTimeslice (Z := Unit) true τ = ({((), τ3)}, False) ∧ + (∃ Q' R', YieldQueueRelA (st := st) { Z with Q := Q', R := R' } m κ r.1 τ3) ∧ Post r.1 τ3 ⦄) : + sched.charge_budget st ar s a used true ⦃ r => r.2 = a ∧ ∃ s2 τ s3 τ3 τ4 Q3 R3 Q4, + ChargeHeadA st Z m κ used.bv s σ s2 τ ∧ + fl4EndTimeslice (Z := Unit) true τ = ({((), τ3)}, False) ∧ + YieldQueueRelA (st := st) { Z with Q := Q3, R := R3 } m κ s3 τ3 ∧ Post s3 τ3 ∧ + reschedReprogA τ3 = ({((), τ4)}, False) ∧ + fl4ChargeBudget (Z := Unit) used.bv true σ = ({((), τ4)}, False) ∧ + YieldQueueRelA (st := st) { Z with Q := Q4, R := R3 } m κ r.1 τ4 ∧ + τ4.kheap = τ3.kheap ∧ τ4.releaseQueue = τ3.releaseQueue ∧ τ4.idleThread = τ3.idleThread ∧ + τ4.machineState = τ3.machineState ∧ τ4.curTime = τ3.curTime ∧ τ4.consumedTime = τ3.consumedTime ∧ + τ4.schedulerAction = .chooseNewThread ∧ + MFrA (st := st) { Z with Q := Q3, R := R3 } m s3 r.1 τ3 τ4 ⦄ := by + let H := E.sched + let sc := L.cscW s + let p := scNameA m sc + have hsc : sc ∈ Z.C := H.cscC + have hcur : σ.curSc = p := H.currentScA + obtain ⟨x, n, hk, R⟩ := H.scr.sc sc hsc + have ha := entryActiveA H.toSchedSRN I P.cores hk + have hid : m.idleScOf m.curCore = specPtr (L.mem s (L.nodeA s layout.node.IDLE_SC)) := by + rw [H.sch.core] + exact H.sch.idleScOf _ H.sch.cpuCs + have hb : decide (sc ≠ L.mem s (L.nodeA s layout.node.IDLE_SC)) = decide (σ.curSc ≠ idleScPtr) := by + rw [hcur] + exact idle_testA hid (H.sch.scOff sc hsc) + unfold sched.charge_budget + refine WP.spec_bind (sched.cur_sc.spec s) ?_ + intro c hc + rw [show c = sc from hc] + rw [eq_of_spec_fd (sched.idle_sc.specA (st := st) s), bind_ok, sc_ne_eqA, bind_ok] + refine WP.spec_bind (entryChargeHeadEqA H I a hag hLimit hMin hsc rfl hcur hk P.notIdle used _ hb) ?_ + rintro s1 ⟨y, hHead, H1, hOwn, hAct, hY, hRp, hMx, w1⟩ + have hNe : scActive x = true → y.scRefills ≠ [] := fun hA => by + obtain ⟨z0, n0, hz0, R1⟩ := H1.scr.sc sc hsc + change (updKSc σ p (.schedContext y n)).kheap (scNameA m sc) = _ at hz0 + rw [updKSc_same] at hz0; cases hz0 + have hx := R1.strong (hAct.trans hA) + have ne := refills_neL hx.wf + simpa [hx.refills] using ne + have KS : ScChargeKeepA σ (updKSc σ p (.schedContext y n)) := + scChargeKeep_updA hk hRp hY hOwn hMx hNe rfl + let σ1 := updKSc σ p (.schedContext y n) + change SchedSRB st Z m κ s1 σ1 σ.curThread at H1 + refine WP.spec_bind (sched.set_consumed.spec s1 0#u64) ?_ + rintro s2 rfl + let τ := { σ1 with consumedTime := 0 } + let s2 := L.wr s1 (L.nodeA s1 layout.node.CONSUMED) 0#u64 + obtain ⟨H2, w2⟩ := H1.clearConsumedA + change SchedSRB st Z m κ s2 τ τ.curThread at H2 + have frame1 : MFrA (st := st) Z m s s1 σ σ1 := cfgScFrameA (st := st) hsc hk w1 + have frame2 : MFrA (st := st) Z m s1 s2 σ1 τ := + MFrA.of_sw rfl (w2.mono fun _ e => Or.inl (e ▸ H1.node_sks H1.sch.cpuCs (by + unfold layout.node.CONSUMED; decide))) + have frame : MFrA (st := st) Z m s s2 σ τ := frame1.trans frame2 rfl rfl + obtain ⟨E2, M2, Y2⟩ := entryScFrameA E hsc hk (show τ.kheap = σ1.kheap from rfl) hY frame + have h2 : τ.kheap p = some (.schedContext y n) := updKSc_same _ _ _ + have I2 := entryCurInvA I hcur hk (hAct.trans ha) + obtain ⟨t, hkt, hB, hIPC⟩ := I2.tcb + let ct := L.curW s2 + have hCt : τ.curThread = specPtr ct := H2.sch.curThread + have hkt' : τ.kheap (specPtr ct) = some (.tcb t) := by rwa [← hCt] + refine WP.spec_bind (sched.cur_thread.spec s2) ?_ + intro c hc + rw [show c = ct from hc, is_schedulable_eqA, schedulable_eqA H2.toSchedSRN H2.curT + (H2.relOn H2.curT H2.curOn) E2, bind_ok] + have hRun : schedulable (specPtr ct) τ = true := by + rw [← hCt] + exact hsch σ1 hHead + rw [if_pos hRun] + have hcs : L.cscW s2 = sc := by + apply scRenA_inj m (H2.sch.scOff _ H2.cscC) (H.sch.scOff _ hsc) + exact H2.currentScA.symm.trans hcur + have hBack : t.tcbSchedContext = some p := hcur ▸ hB + obtain ⟨t', eT, hWord⟩ := H2.sch.tcb ct H2.curT + rw [hkt'] at eT; cases eT + have hz : L.mem s2 (scA ct) ≠ 0#u64 := by + intro e + rw [hBack, e, optScA, if_pos rfl] at hWord + cases hWord + have hWordS : L.mem s2 (scA ct) = sc := by + apply scRenA_inj m (H2.sch.scOff _ (H2.rest.scMem ct H2.curT hz)) (H.sch.scOff _ hsc) + rw [hBack, optScA, if_neg hz] at hWord + exact (Option.some.inj hWord).symm + have sc0 : sc ≠ 0#u64 := hWordS ▸ hz + have CH : ChargeHeadA st Z m κ used.bv s σ s2 τ := + ⟨⟨y, n, by have h := hHead; rw [show scNameA m sc = σ.curSc from hcur.symm] at h; exact h, by rw [hcur]⟩, frame, H2, E2, M2, Y2, KS, hCt.symm ▸ hRun, rfl, rfl, + fun z k hz => by rw [hcur, hk] at hz; cases hz; exact ha⟩ + refine WP.spec_bind (SEND s2 τ sc t y n CH hsc hcs hcur sc0 hWordS hkt' hBack h2) ?_ + rintro ⟨s3, a3⟩ ⟨ha3, τ3, he, ⟨Q3, R3, H3⟩, hP⟩ + simp only [uncurry_apply_pair] + rw [← cfgResultAssocA] + refine WP.spec_bind (entryRescheduleFrA H3) ?_ + rintro s4 ⟨τ4, Q4, he', H4, hk4, hr4, hi4, hm4, ht4, hc4, ha4, f4⟩ + rw [WP.spec_ok] + refine ⟨ha3, s2, τ, s3, τ3, τ4, Q3, R3, Q4, CH, he, H3, hP, he', ?_, H4, hk4, hr4, hi4, hm4, ht4, hc4, ha4, f4⟩ + unfold fl4ChargeBudget + rw [nd_gets_bindSc, nd_bind_oneSc hHead] + rw [nd_bind_oneSc (show NonDet.modify (fun s : State Unit => { s with consumedTime := 0 }) σ1 = + ({((), τ)}, False) from rfl)] + repeat rw [nd_gets_bindSc] + rw [NonDet.when, if_pos (hCt.symm ▸ hRun)] + rw [nd_bind_oneSc he] + exact he' + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScFinishVcpuA.lean b/proofs/Fl4ProofsA64/P3/ScFinishVcpuA.lean new file mode 100644 index 000000000..266e6a8c9 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScFinishVcpuA.lean @@ -0,0 +1,314 @@ +import Fl4ProofsA64.P3.ScActivateA +import Fl4ProofsA64.P3.ScArchSwA + +/-! +The post-activation VCPU dispatch. [J] The ordinary notification +observations relate the selected thread's bound notification and its +state and badge. [J] `S4FinishHookA` supplies run completion after the +notification becomes idle. Run transfer fields, buffer windows and +mask-dependent hardware state belong to S4. +-/ + +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3.Sched + +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +/-- [J] The S4 run-completion refinement at the active notification +branch. Its inputs are already past the concrete and abstract idle writes. -/ +def S4FinishHookA (ar : machine.Arch A) (s : S) (a : A) (σ : State Unit) + (t badge : U64) (Post : S × A → State Unit → Prop) : Prop := + vcpu.complete_run st ar s a t fl4_abi.consts.vcpu_run_stop.INTERRUPT badge 0#u64 0#u64 ⦃ r => + ∃ τ, fl4CompleteRun (Z := Unit) (specPtr t) .interrupt badge.bv 0 0 σ = ({((), τ)}, False) ∧ Post r τ ⦄ + +private theorem runVmCodeA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : YieldQueueRelA (st := st) Z m κ s σ) {t : U64} (ht : t ∈ Z.T) + {x : Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) (vm : x.tcbState = .ext .runningVM) : + L.mem s (stA t) = sched.TS_RUNNING_VM := by + apply UScalar.eq_of_val_eq + rw [H.states t ht x hk, vm] + unfold sched.TS_RUNNING_VM + rfl + +private theorem boundVcpuCheckA (s : S) (a : A) (t : U64) + (vm : L.mem s (stA t) = sched.TS_RUNNING_VM) (live : L.mem s (runVcpuA t) ≠ 0#u64) : + vcpu.check st ar s a t = (do + let n ← obj.Tcb.bound_ntfn st t s + if n != 0#u64 then + let state ← obj.Ntfn.state st n s + if state = obj.NTFN_ACTIVE then + let badge ← obj.Ntfn.badge st n s + let s' ← obj.Ntfn.set_state st n s obj.NTFN_IDLE + vcpu.complete_run st ar s' a t fl4_abi.consts.vcpu_run_stop.INTERRUPT badge 0#u64 0#u64 + else ok (s, a) + else ok (s, a)) := by + unfold vcpu.check + rw [eq_of_spec_fd (obj.Tcb.state.spec s t), bind_ok, if_neg (fun h => (bne_iff_ne.1 h) vm)] + rw [eq_of_spec_fd Fl4ProofsA64.P0.Consts.layout.tcb.RUN_VCPU.spec] + simp only [bind_ok, lift, L.read_eq] + exact if_neg live + +/-- [J] The ordinary bound-notification projection from the post-schedule +TCB and IPC relations. Only the active state needs a badge. -/ +def FinishNotificationA (s : S) (σ : State Unit) (t : U64) (x : Tcb) : Prop := + x.tcbBoundNotification = optRefA (L.mem s (boundA t)) ∧ + ∀ n, L.mem s (boundA t) = n → n ≠ 0#u64 → + ∃ ntfn, σ.kheap (specPtr n) = some (.notification ntfn) ∧ + match ntfn.ntfnObj with + | .activeNtfn badge => L.epState s n = obj.NTFN_ACTIVE ∧ (L.mem s (ntfnBdA n)).bv = badge + | _ => L.epState s n ≠ obj.NTFN_ACTIVE + +private theorem checkLiveSpecA {σ : State Unit} {t : U64} {x : Tcb} + (hk : σ.kheap (specPtr t) = some (.tcb x)) (vm : x.tcbState = .ext .runningVM) + {v : ObjRef} (live : x.tcbExt.tcbRunVcpu = some v) : + fl4VcpuCheck (Z := Unit) (specPtr t) σ = (do + let nOpt ← getTcbObjRef Tcb.tcbBoundNotification (specPtr t) + match nOpt with + | some n => do + let ntfn ← getNotification n + match ntfn.ntfnObj with + | .activeNtfn badge => do + setNotification n { ntfn with ntfnObj := .idleNtfn } + fl4CompleteRun (specPtr t) .interrupt badge 0 0 + | _ => pure () + | none => pure ()) σ := by + unfold fl4VcpuCheck getThreadState getRunVcpu + rw [nd_bind_oneSc (threadGet_eqSc _ hk), vm, NonDet.when, if_pos rfl, + nd_bind_oneSc (threadGet_eqSc _ hk), live] + rfl + +private theorem finishGetNotificationA {σ : State Unit} {n : ObjRef} {x : Notification} + (hk : σ.kheap n = some (.notification x)) : getNotification (Z := Unit) n σ = ({(x, σ)}, False) := by + exact getSimpleKo_ntfnSc hk + +private theorem finishSetNotificationA {σ : State Unit} {n : ObjRef} {x : Notification} + (hk : σ.kheap n = some (.notification x)) : + setNotification (Z := Unit) n {x with ntfnObj := .idleNtfn} σ = + ({((), updKSc σ n (.notification {x with ntfnObj := .idleNtfn}))}, False) := by + unfold setNotification setSimpleKo + rw [nd_bind_oneSc (getObject_eqSc hk)] + have simple : NonDet.assert (σ := State Unit) (isSimpleType (.notification x)) σ = ({((), σ)}, False) := by + simp [NonDet.assert, isSimpleType, aType] + rw [nd_bind_oneSc simple, partialInv_ntfnSc] + have present : NonDet.assert (σ := State Unit) (some x ≠ none) σ = ({((), σ)}, False) := by + simp [NonDet.assert] + rw [nd_bind_oneSc present] + exact setObject_eqSc hk rfl + +/-- A live run without a pending bound signal stutters. An active signal +is consumed locally and handed to S4 at the precise completion call. -/ +theorem vcpu_check_liveA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {σ : State Unit} (H : YieldQueueRelA (st := st) Z m κ s σ) + (V : VcpuCoreA (L := L) Z s σ) (a : A) + {t : U64} (ht : t ∈ Z.T) {x : Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + (vm : x.tcbState = .ext .runningVM) (live : L.mem s (runVcpuA t) ≠ 0#u64) + (N : FinishNotificationA (st := st) s σ t x) (Post : S × A → State Unit → Prop) + (keep : Post (s, a) σ) + (complete : ∀ n ntfn badge s', L.mem s (boundA t) = n → + σ.kheap (specPtr n) = some (.notification ntfn) → ntfn.ntfnObj = .activeNtfn badge.bv → + obj.Ntfn.set_state st n s obj.NTFN_IDLE = ok s' → + S4FinishHookA (st := st) ar s' a (updKSc σ (specPtr n) (.notification {ntfn with ntfnObj := .idleNtfn})) + t badge Post) : + vcpu.check st ar s a t ⦃ r => ∃ τ, + fl4VcpuCheck (Z := Unit) (specPtr t) σ = ({((), τ)}, False) ∧ Post r τ ⦄ := by + obtain ⟨x', hx', _, hrun⟩ := V.tcb t ht + rw [hk] at hx'; cases hx' + have hspec := checkLiveSpecA hk vm (by simpa [optRefA, live] using hrun) + have hbound : getTcbObjRef (Z := Unit) Tcb.tcbBoundNotification (specPtr t) σ = + ({(optRefA (L.mem s (boundA t)), σ)}, False) := by + unfold getTcbObjRef + rw [threadGet_eqSc _ hk, N.1] + rw [boundVcpuCheckA s a t (runVmCodeA H ht hk vm) live, + eq_of_spec_fd (obj.Tcb.bound_ntfn.val s t), bind_ok] + let n := L.mem s (boundA t) + by_cases unbound : n = 0#u64 + · rw [if_neg (by rw [bne_iff_ne]; exact not_not.2 unbound), WP.spec_ok] + refine ⟨σ, ?_, keep⟩ + rw [hspec, nd_bind_oneSc hbound] + simp only [optRefA, show L.mem s (boundA t) = 0#u64 from unbound, if_true] + rfl + · rw [if_pos (bne_iff_ne.2 unbound)] + obtain ⟨ntfn, hn, view⟩ := N.2 n rfl unbound + rw [eq_of_spec_fd (obj.Ntfn.state.spec s n), bind_ok] + have spec : fl4VcpuCheck (Z := Unit) (specPtr t) σ = (do + match ntfn.ntfnObj with + | .activeNtfn badge => + setNotification (specPtr n) {ntfn with ntfnObj := .idleNtfn} + fl4CompleteRun (specPtr t) .interrupt badge 0 0 + | _ => pure ()) σ := by + rw [hspec, nd_bind_oneSc hbound] + have nz : L.mem s (boundA t) ≠ 0#u64 := unbound + simp only [optRefA, if_neg nz] + change (getNotification (specPtr n) >>= _) σ = _ + rw [nd_bind_oneSc (finishGetNotificationA hn)] + cases state : ntfn.ntfnObj with + | activeNtfn b => + rw [state] at view + rw [if_pos view.1, eq_of_spec_fd (obj.Ntfn.badge.val s n), bind_ok] + let badge := L.mem s (ntfnBdA n) + have setIdle := eq_of_spec_fd (obj.Ntfn.set_state.val (st := st) s n obj.NTFN_IDLE) + rw [setIdle, bind_ok] + refine WP.spec_mono (complete n ntfn badge _ rfl hn (state.trans (congrArg Ntfn.activeNtfn view.2.symm)) setIdle) ?_ + rintro r ⟨τ, hτ, post⟩ + refine ⟨τ, ?_, post⟩ + rw [spec, state] + change (setNotification (specPtr n) {ntfn with ntfnObj := .idleNtfn} >>= + fun _ => fl4CompleteRun (specPtr t) .interrupt b 0 0) σ = _ + rw [nd_bind_oneSc (finishSetNotificationA hn), ← view.2] + exact hτ + | idleNtfn => + rw [state] at view + rw [if_neg view, WP.spec_ok] + exact ⟨σ, by rw [spec, state]; rfl, keep⟩ + | waitingNtfn q => + rw [state] at view + rw [if_neg view, WP.spec_ok] + exact ⟨σ, by rw [spec, state]; rfl, keep⟩ + +private theorem copyNextPcA {σ : State Unit} {p : ObjRef} {x : Tcb} + (hk : σ.kheap p = some (.tcb x)) : + (asUser (Z := Unit) p do + let ip ← getRegister nextInstructionRegister + setRegister faultRegister ip) σ = + ({((), updKSc σ p (.tcb (yieldRegA x .faultIP (x.tcbArch.tcbContext.userRegs .nextIP))))}, False) := by + let ip := x.tcbArch.tcbContext.userRegs .nextIP + rw [← yieldAsUserRegA hk .faultIP ip] + unfold asUser + rw [nd_bind_oneSc (getTcb_oneA hk), nd_pure_bindSc] + rw [nd_bind_oneSc (getTcb_oneA hk), nd_pure_bindSc] + have e : (getRegister nextInstructionRegister >>= setRegister faultRegister) + (archTcbContextGet x.tcbArch) = setRegister .faultIP ip (archTcbContextGet x.tcbArch) := by + unfold getRegister nextInstructionRegister faultRegister + rw [nd_gets_bindSc] + rfl + rw [e] + +/-- [J] The ordinary saved user-register relation supplies NEXT_IP. +A RunningVM caller with no linked VCPU resumes at that PC as Running. -/ +theorem vcpu_check_missing_runA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {σ : State Unit} (H : YieldQueueRelA (st := st) Z m κ s σ) + (V : VcpuCoreA (L := L) Z s σ) (a : A) + {t : U64} (ht : t ∈ Z.T) {x : Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + (vm : x.tcbState = .ext .runningVM) (missing : L.mem s (runVcpuA t) = 0#u64) + (pc : (L.mem s (regWA t layout.reg.NEXT_IP)).bv = x.tcbArch.tcbContext.userRegs .nextIP) : + vcpu.check st ar s a t ⦃ r => r.2 = a ∧ ∃ τ, + fl4VcpuCheck (Z := Unit) (specPtr t) σ = ({((), τ)}, False) ∧ + YieldQueueRelA (st := st) Z m κ r.1 τ ∧ τ.curThread = σ.curThread ∧ + ∃ y, τ.kheap (specPtr t) = some (.tcb y) ∧ + y.tcbState = .running ∧ y.tcbArch.tcbContext.userRegs .faultIP = + x.tcbArch.tcbContext.userRegs .nextIP ⦄ := by + obtain ⟨x', hx', _, hrun⟩ := V.tcb t ht + rw [hk] at hx'; cases hx' + have hn : x.tcbExt.tcbRunVcpu = none := by simpa [optRefA, missing] using hrun + have code := runVmCodeA H ht hk vm + unfold vcpu.check + rw [eq_of_spec_fd (obj.Tcb.state.spec s t), bind_ok, if_neg (fun h => (bne_iff_ne.1 h) code), + eq_of_spec_fd Fl4ProofsA64.P0.Consts.layout.tcb.RUN_VCPU.spec] + simp only [bind_ok, lift, L.read_eq] + change (if L.mem s (runVcpuA t) = 0#u64 then _ else _) ⦃ _ ⦄ + rw [if_pos missing, tcb_reg_eqA, bind_ok] + let ip := L.mem s (regWA t layout.reg.NEXT_IP) + let z := yieldRegA x .faultIP ip.bv + refine WP.spec_bind (activateWritePcA H ht hk ip .faultIP (Or.inr rfl) + (by simp [layout.reg.FAULT_IP]) (by simp [layout.reg.FAULT_IP])) ?_ + intro s' H' + have codeR : sched.TS_RUNNING.val = tsCodeA .running := by unfold sched.TS_RUNNING tsCodeA; rfl + refine WP.spec_bind (yieldRunnableA H' ht (updKSc_same _ _ _) + (by change runnable x.tcbState = true; rw [vm]; rfl) rfl codeR) ?_ + rintro s'' ⟨τ, he, H'', hheap, hcur, _, _, _⟩ + rw [WP.spec_ok] + refine ⟨rfl, τ, ?_, H'', hcur, {z with tcbState := .running}, ?_, rfl, ?_⟩ + · unfold fl4VcpuCheck getThreadState getRunVcpu + rw [nd_bind_oneSc (threadGet_eqSc _ hk), vm, NonDet.when, if_pos rfl, + nd_bind_oneSc (threadGet_eqSc _ hk), hn] + change ((asUser (specPtr t) (getRegister nextInstructionRegister >>= setRegister faultRegister)) >>= + fun _ => setThreadState (specPtr t) .running) σ = _ + rw [nd_bind_oneSc (copyNextPcA hk), ← pc] + exact he + · rw [hheap, updKSc_same] + · change (yieldRegA x .faultIP ip.bv).tcbArch.tcbContext.userRegs .faultIP = _ + simp [yieldRegA, archTcbSetRegisters, archTcbGetRegisters, ip, pc] + + +/-- [J] The views required at the post-activation RunningVM check. The +PC is needed only for recovery, and bound-notification data is needed +only when the run link is present. -/ +structure FinishVcpuReadyA (Z : XSetsA) (m : McState) (κ : U64 → U64) + (s : S) (a : A) (σ : State Unit) (x : Tcb) (ar : machine.Arch A) : Prop where + links : VcpuCoreA (L := L) Z s σ + pc : L.mem s (runVcpuA (L.curW s)) = 0#u64 → + (L.mem s (regWA (L.curW s) layout.reg.NEXT_IP)).bv = x.tcbArch.tcbContext.userRegs .nextIP + notification : L.mem s (runVcpuA (L.curW s)) ≠ 0#u64 → FinishNotificationA (st := st) s σ (L.curW s) x + /-- [J] The S4 hook is used only after the active notification is consumed. -/ + complete : L.mem s (runVcpuA (L.curW s)) ≠ 0#u64 → + ∀ n ntfn badge s', L.mem s (boundA (L.curW s)) = n → + σ.kheap (specPtr n) = some (.notification ntfn) → ntfn.ntfnObj = .activeNtfn badge.bv → + obj.Ntfn.set_state st n s obj.NTFN_IDLE = ok s' → + S4FinishHookA (st := st) ar s' a (updKSc σ (specPtr n) (.notification {ntfn with ntfnObj := .idleNtfn})) + (L.curW s) badge (fun r τ => YieldQueueRelA (st := st) Z m κ r.1 τ) + +/-- [J] The post-activation projections supply the local register, +notification, and run-link reads on RunningVM. Native activation uses +the same top without these guest inputs. -/ +theorem finish_after_schedule_refinesA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {σ : State Unit} (H : YieldQueueRelA (st := st) Z m κ s σ) (a : A) + (hC0 : ∀ c ∈ Z.C, c ≠ 0#u64) (htu : Fl4TicksToUsAgreeA ar a) + {x : Tcb} (hk : σ.kheap σ.curThread = some (.tcb x)) (legal : ActivateStateOKA x.tcbState) + (pc : (L.mem s (regWA (L.curW s) layout.reg.FAULT_IP)).bv = x.tcbArch.tcbContext.userRegs .faultIP) + (guest : ∀ s1 σ1 y, + sched.activate_thread st ar s a = ok (s1, a) → + fl4ActivateThread (Z := Unit) σ = ({((), σ1)}, False) → + σ1.kheap σ1.curThread = some (.tcb y) → y.tcbState = .ext .runningVM → + FinishVcpuReadyA (st := st) Z m κ s1 a σ1 y ar) : + (do + let (s1, a1) ← sched.activate_thread st ar s a + let t ← sched.cur_thread st s1 + vcpu.check st ar s1 a1 t) ⦃ r => ∃ τ, + fl4FinishAfterSchedule (Z := Unit) σ = ({((), τ)}, False) ∧ + YieldQueueRelA (st := st) Z m κ r.1 τ ⦄ := by + let act := sched.activate_thread st ar s a + have hp := activate_thread_refinesA H a hC0 htu hk legal pc + obtain ⟨r, er, post⟩ := spec_ex_fd hp + change act = ok r at er + obtain ⟨s1, a1⟩ := r + obtain ⟨ha, σ1, hActivate, H1, _, y, hy, _⟩ := post + change a1 = a at ha + subst a1 + change (act >>= _) ⦃ _ ⦄ + have exactAct : act ⦃ v => v = (s1, a) ⦄ := by rw [er, WP.spec_ok] + refine WP.spec_bind exactAct ?_ + intro v hv + subst v + simp only [uncurry_apply_pair] + rw [eq_of_spec_fd (sched.cur_thread.spec s1), bind_ok] + let t := L.curW s1 + change vcpu.check st ar s1 a t ⦃ _ ⦄ + have cur := H1.sched.sch.curThread + have hy' : σ1.kheap (specPtr t) = some (.tcb y) := by rw [← cur]; exact hy + have glue {τ : State Unit} (h : fl4VcpuCheck (Z := Unit) (specPtr t) σ1 = ({((), τ)}, False)) : + fl4FinishAfterSchedule (Z := Unit) σ = ({((), τ)}, False) := by + unfold fl4FinishAfterSchedule + rw [nd_bind_oneSc hActivate, nd_gets_bindSc, cur] + exact h + by_cases vm : y.tcbState = .ext .runningVM + · have V := guest s1 σ1 y er hActivate hy vm + by_cases missing : L.mem s1 (runVcpuA t) = 0#u64 + · refine WP.spec_mono (vcpu_check_missing_runA H1 V.links a H1.sched.curT hy' vm missing (V.pc missing)) ?_ + rintro r ⟨_, τ, hτ, Hτ, _⟩ + exact ⟨τ, glue hτ, Hτ⟩ + · refine WP.spec_mono (vcpu_check_liveA H1 V.links a H1.sched.curT hy' vm missing + (V.notification missing) (fun r τ => YieldQueueRelA (st := st) Z m κ r.1 τ) H1 (V.complete missing)) ?_ + rintro r ⟨τ, hτ, Hτ⟩ + exact ⟨τ, glue hτ, Hτ⟩ + · obtain ⟨run, spec⟩ := vcpu_check_off_vmA (ar := ar) (s := s1) H1 a H1.sched.curT hy' vm + rw [run, WP.spec_ok] + exact ⟨σ1, glue spec, H1⟩ + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScFrameA.lean b/proofs/Fl4ProofsA64/P3/ScFrameA.lean new file mode 100644 index 000000000..1316dbddb --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScFrameA.lean @@ -0,0 +1,106 @@ +/- +P3 (aarch64, A64-P3-MCS gen 9, gen 10): **the FL4 write set and the spec frame of the MCS tail operations** (ruling 01:09Z +gen-9 item 2: explicit `WritesIn` + pure spec-frame facts, so that the IPC join glue `P3/IpcScGlueA` only has to show +IPC's footprints off the write set and read `KView` off `SKeepA`). + +* **`MWrA (st := st) Z m σ s`** — every word an MCS tail writes: the SC objects of `Z.C` (`ScFootA`), the kernel-state block + (`[ksb, ksb + ksSizeA)`: the per-core node words, the queues' heads and bitmaps, `REPROGRAM`, the action word), and + the scheduler words of the TCBs of `Z.T` (`SC`, the scheduler-queue links, `IN_QUEUES`). IPC's `NFoot` / `EFoot` + words (notification / endpoint / reply objects, a TCB's state, block, badge register, priority, IPC-queue links, + fault) are off it: **`SchedSRN.not_mwr`** reduces that to "off the SC footprint, off every TCB of `Z.T` up to the + scheduler words, off the block" (the placement IPC's `NPlaced` / `EPlaced` and `JAtZA`'s `PlaceA` give). +* Gen 10: `MWrA`, `SchedSRN.node_in` moved to `P3/ScSWrA` (with `SKeepA`, `SSzA` and the bundle `MFrA`), + `SchedSRN.mwr_don` / `mwr_two` to `P3/ScWakeDonA`. Here: **`mwr_scObj`** (one SC object) and the frame versions of + the gen-9 contracts, **`update_consumed_frameA`** (`MFrA`) and **`maybe_return_frameA`** (`WritesIn … MWrA`, + `SKeepA`, `SSzA` of the whole operation, the final `reschedule_required` included). The end-to-end frames of + `wakeTail` / `restartTail` / `recvTail` / `completeTail` are `wake_tail_frameA` (`P3/ScGlueA`), + `restart_tail_frameA` (`P3/ScTailA`), `recv_tail_frameA` / `complete_tail_frameA` (`P3/ScRecvA`). + +No named premise. +-/ +import Fl4ProofsA64.P3.ScRecvA +import Fl4ProofsA64.P3.ScConsA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **A word off the SC footprint, the scheduler words of the TCBs and the block is off `MWrA`.** -/ +theorem not_mwrA {Z : XSetsA} {m : Fl4SpecA64.McState} {σ : Fl4SpecA64.State Unit} {s : S} {b : U64} + (hnS : ¬ ScFootA Z.C m σ b) + (hnT : ∀ u ∈ Z.T, b ≠ scA u ∧ b ≠ nextA .Sched u ∧ b ≠ prevA .Sched u ∧ b ≠ inqA u) + (hnK : b.val < (L.ksb s).val ∨ (L.ksb s).val + ksSizeA ≤ b.val) : ¬ MWrA (st := st) Z m σ s b := by + rintro (h | h | ⟨u, hu, e | e | e | e⟩) + · exact hnS h + · have := h.ks; omega + · exact (hnT u hu).1 e + · exact (hnT u hu).2.1 e + · exact (hnT u hu).2.2.1 e + · exact (hnT u hu).2.2.2 e + +/-- **A word off every TCB of `Z.T` (the whole 4 KiB object), the block and the SC footprint is off `MWrA`** +(the shape the IPC objects' placement gives). -/ +theorem SchedSRN.not_mwr {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} {σ : Fl4SpecA64.State Unit} + (H : SchedSRN st Z m κ s σ) {b : U64} (hnS : ¬ ScFootA Z.C m σ b) + (hnT : ∀ u ∈ Z.T, b.val < u.val ∨ u.val + 4096 ≤ b.val) + (hnK : b.val < (L.ksb s).val ∨ (L.ksb s).val + ksSizeA ≤ b.val) : ¬ MWrA (st := st) Z m σ s b := by + have tw : ∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → b ≠ wa u K := by + intro u hu K hK e + have hT := H.core.tcbs u hu + have hfit := hT.fits + have hv : (wa u K).val = u.val + K.val := by rw [wa_valm]; omega + have := hnT u hu + rw [e, hv] at this; omega + exact not_mwrA hnS (fun u hu => ⟨tw u hu 3464#u64 (by simp), tw u hu 3480#u64 (by simp), + tw u hu 3488#u64 (by simp), tw u hu 3592#u64 (by simp)⟩) hnK + +/-- One related SC object is in `MWrA`. -/ +theorem mwr_scObj {Z : XSetsA} {m : Fl4SpecA64.McState} {σ : Fl4SpecA64.State Unit} {s : S} {sc b : U64} + (hsc : sc ∈ Z.C) {x : Fl4SpecA64.SchedContext} {n : Nat} + (hk : σ.kheap (scNameA m sc) = some (.schedContext x n)) (hb : ScObjA sc n b) : MWrA (st := st) Z m σ s b := + Or.inl ⟨sc, hsc, x, n, hk, hb⟩ + +end + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +/-- **`update_consumed` with its frame** (module docstring; gen 10: the bundle `MFrA`). -/ +theorem update_consumed_frameA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} + {σ : Fl4SpecA64.State Unit} (H : SchedSRA st Z m κ s σ) (a : A) {sc : U64} (hc : sc ∈ Z.C) + (hmax : Fl4SpecA64.maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ v, ar.ticks_to_us a v ⦃ r => r.bv = Fl4SpecA64.ticksToUs v.bv ⦄) : + schedcontext.update_consumed st ar s a sc ⦃ r => ∃ σ', + Fl4SpecA64.schedContextUpdateConsumed (Z := Unit) (scNameA m sc) σ = ({(r.1.bv, σ')}, False) ∧ + SchedSRA st Z m κ r.2 σ' ∧ MFrA (st := st) Z m s r.2 σ σ' ∧ σ' = { σ with kheap := σ'.kheap } ⦄ := by + refine WP.spec_mono (update_consumed_refinesA H a hc hmax htu) ?_ + rintro r ⟨x, n, v, hk, hsp, -, hw, H'⟩ + refine ⟨_, hsp, H', ⟨hw.mono fun b hb => mwr_scObj hc hk hb, SKeepA.of_obj fun q o hq ho => ?_, + SSzA.of_upd hk rfl⟩, rfl⟩ + have hne : q ≠ scNameA m sc := fun e => by rw [e, hk] at hq; cases hq; exact ho x n rfl + simp [updKSc, hne, hq] + +/-- **`maybe_return_sched_context` with its frame** (module docstring; gen 10: the whole operation, the final +`reschedule_required` included, from `maybe_return_localA`'s bundle `MFrA`). -/ +theorem maybe_return_frameA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} + {σ : Fl4SpecA64.State Unit} (H : SchedSRA st Z m κ s σ) (hst : StEqA Z.T (L.mem s) σ) {t n : U64} + (ht : t ∈ Z.T) (hN : NtfnScEqA m n (L.mem s) σ) + (hnr : ∀ x, σ.kheap (specPtr t) = some (.tcb x) → Fl4SpecA64.runnable x.tcbState = false) : + notification.maybe_return_sched_context st s n t ⦃ s' => ∃ σ' Q', + Fl4SpecA64.maybeReturnSc (Z := Unit) (specPtr n) (specPtr t) σ = ({((), σ')}, False) ∧ + SchedSRA st { Z with Q := Q' } m κ s' σ' ∧ WritesIn st s s' (MWrA (st := st) Z m σ s) ∧ SKeepA σ σ' ∧ + SSzA σ σ' ⦄ := + WP.spec_mono (maybe_return_localA H hst ht hN hnr) fun _ ⟨σ', Q', h1, h2, F⟩ => ⟨σ', Q', h1, h2, F.wr, F.keep, F.sz⟩ + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScGlueA.lean b/proofs/Fl4ProofsA64/P3/ScGlueA.lean new file mode 100644 index 000000000..82740b16a --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScGlueA.lean @@ -0,0 +1,165 @@ +/- +P3 (aarch64, A64-P3-MCS gen 7): **the laws `coreKeep` / `donLocalKeep`, the whole local `wakeTail`, and the SC +placement at boot** (ruling 00:22Z gen-7 items 3–4). + +* **`SchedSRA.coreKeepA`** (IPC's `SchedOpsA.coreKeep` / `Endp.SchedOpsEA.coreKeep`, `CoreKeep` inline): two states + related by `SchedSRA` at the same `m` (the join's `SR = ∃ Q' R', SchedSRA st {Z with Q := Q', R := R'} m κ`) + agree on the current core and on every affinity word. +* **`DonLocalA`** (the MCS instance of IPC's `donLocal`, ruling gen 8 Q1): the notification's SC word, if not null, + names a related SC whose `CORE` word is the current core. Membership of `Z.C` is part of it (the SC relation + needs it, and nothing else in `SR` relates a notification's SC to `Z.C`). +* **`SchedSRA.donLocalKeepA`** (IPC's `SchedOpsA.donLocalKeep`): through the fixed map `κ` (`SchedSRN.scCore`) and the + fixed current core, `DonLocalA` passes between related states agreeing on the notification's SC word. +* **`wake_tail_localA`** (`SchedOpsA.wakeTail`, local case, every case): `wake_tail_nodonateA` or + `wake_tail_donateA`, selected by the words; `DonLocalA` supplies the donation's placement. (Gen 10) it is the + corollary of **`wake_tail_frameA`**, the same with the frame `MFrA` (`P3/ScSWrA`). +* **`boot_scPlacedA`** (item 3; x86 `SchedRel.scOut` / `scDisj` at boot): at A64-Boot's layout (`w0` and `w1` share + the idle area and the kernel-state block; `w1` adds no SC) the idle SCs (`bootCA`, 128-byte objects at + `idle_base + 8192 c + 4096`) have distinct names, are disjoint and lie off the idle TCBs, the IRQ node and the block. + +No named premise. Hypotheses: as `wake_tail_*A`; `hL` / `hK` (the boot layouts, `[J]` premises of `jbootA`). +-/ +import Fl4ProofsA64.P3.ScWakeDonA +import Fl4ProofsA64.P3.JBootA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +/-- **`coreKeep` for `SchedSRA`** (module docstring). -/ +theorem SchedSRA.coreKeepA {Z Z' : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s s' : S} + {σ σ' : Fl4SpecA64.State Unit} (H : SchedSRA st Z m κ s σ) (H' : SchedSRA st Z' m κ s' σ') (hT : Z'.T = Z.T) : + L.cpu s' = L.cpu s ∧ ∀ u ∈ Z.T, L.mem s' (affA u) = L.mem s (affA u) := + ⟨H.cpu_eq H', fun u hu => H.aff_eq H' hu (by rw [hT]; exact hu)⟩ + +/-- **The MCS `donLocal`** (module docstring). -/ +def DonLocalA (C : Set U64) (s : S) (n : U64) : Prop := + ∀ sc, L.mem s (nScSc n) = sc → sc ≠ 0#u64 → sc ∈ C ∧ L.mem s (scCoreA sc) = L.cpu s + +/-- **`donLocalKeep` for `SchedSRA`** (module docstring). -/ +theorem SchedSRA.donLocalKeepA {Z Z' : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s s' : S} + {σ σ' : Fl4SpecA64.State Unit} (H : SchedSRA st Z m κ s σ) (H' : SchedSRA st Z' m κ s' σ') (hC : Z'.C = Z.C) + {n : U64} (hn : L.mem s' (nScSc n) = L.mem s (nScSc n)) (hd : DonLocalA (st := st) Z.C s n) : + DonLocalA (st := st) Z.C s' n := by + intro sc e h0 + obtain ⟨hsc, hc⟩ := hd sc (hn.symm.trans e) h0 + refine ⟨hsc, ?_⟩ + rw [H'.scCore sc (by rw [hC]; exact hsc), ← H.scCore sc hsc, hc, H.cpu_eq H'] + +/-- **The local wake tail with its frame** (`SchedOpsA.wakeTail`, local case; gen 10: `MFrA`). -/ +theorem wake_tail_frameA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} + {σ : Fl4SpecA64.State Unit} (H : SchedSRA st Z m κ s σ) (a : A) + (hmb : sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) {t n : U64} (ht : t ∈ Z.T) + (hon : L.mem s (affA t) = L.cpu s) (hN : NtfnScEqA m n (L.mem s) σ) (hst : StEqA Z.T (L.mem s) σ) + (hnqs : ∀ d p, specPtr t ∉ σ.readyQueues d p) (hnq : specPtr t ∉ σ.releaseQueue) + (hrunt : ∀ x, σ.kheap (specPtr t) = some (.tcb x) → Fl4SpecA64.runnable x.tcbState = true) + (hd : DonLocalA (st := st) Z.C s n) : + (do + let (s4, a2) ← notification.maybe_donate_sched_context st ar s a t n + let b1 ← sched.is_schedulable st s4 t + let s5 ← if b1 then sched.possible_switch_to st s4 t else ok s4 + let s6 ← notification.unblock_sporadic st s5 t + ok (s6, a2)) ⦃ r => r.2 = a ∧ ∃ σ' Q' R', + (do + Fl4SpecA64.maybeDonateSc (Z := Unit) (specPtr t) (specPtr n) + let b ← Fl4Spec.NonDet.gets (Fl4SpecA64.schedulable (Z := Unit) (specPtr t)) + Fl4Spec.NonDet.when b <| Fl4SpecA64.possibleSwitchTo (specPtr t) + let scopt ← Fl4SpecA64.getTcbObjRef Fl4SpecA64.Tcb.tcbSchedContext (specPtr t) + Fl4SpecA64.ifSporadicCurScTestRefillUnblockCheck scopt) σ = ({((), σ')}, False) ∧ + SchedSRA st { Z with Q := Q', R := R' } m κ r.1 σ' ∧ MFrA (st := st) Z m s r.1 σ σ' ⦄ := by + by_cases h0 : L.mem s (scA t) = 0#u64 + · by_cases h1 : L.mem s (nScSc n) = 0#u64 + · exact WP.spec_mono (wake_tail_nodonateA H a ht hon hst hN hnq (Or.inr (Or.inl h1))) + fun r ⟨e1, σ', Q', e2, H', F⟩ => ⟨e1, σ', Q', Z.R, e2, H', F⟩ + · obtain ⟨hC, hc⟩ := hd _ rfl h1 + by_cases h2 : L.mem s (scTcbA (L.mem s (nScSc n))) = 0#u64 + · exact wake_tail_donateA H a hmb ht hon hN hst hnqs hnq hrunt h0 rfl h1 hC h2 hc + · exact WP.spec_mono (wake_tail_nodonateA H a ht hon hst hN hnq (Or.inr (Or.inr ⟨hC, h2⟩))) + fun r ⟨e1, σ', Q', e2, H', F⟩ => ⟨e1, σ', Q', Z.R, e2, H', F⟩ + · exact WP.spec_mono (wake_tail_nodonateA H a ht hon hst hN hnq (Or.inl h0)) + fun r ⟨e1, σ', Q', e2, H', F⟩ => ⟨e1, σ', Q', Z.R, e2, H', F⟩ + +/-- **The local wake tail** (`SchedOpsA.wakeTail`, local case; module docstring). -/ +theorem wake_tail_localA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} + {σ : Fl4SpecA64.State Unit} (H : SchedSRA st Z m κ s σ) (a : A) + (hmb : sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) {t n : U64} (ht : t ∈ Z.T) + (hon : L.mem s (affA t) = L.cpu s) (hN : NtfnScEqA m n (L.mem s) σ) (hst : StEqA Z.T (L.mem s) σ) + (hnqs : ∀ d p, specPtr t ∉ σ.readyQueues d p) (hnq : specPtr t ∉ σ.releaseQueue) + (hrunt : ∀ x, σ.kheap (specPtr t) = some (.tcb x) → Fl4SpecA64.runnable x.tcbState = true) + (hd : DonLocalA (st := st) Z.C s n) : + (do + let (s4, a2) ← notification.maybe_donate_sched_context st ar s a t n + let b1 ← sched.is_schedulable st s4 t + let s5 ← if b1 then sched.possible_switch_to st s4 t else ok s4 + let s6 ← notification.unblock_sporadic st s5 t + ok (s6, a2)) ⦃ r => r.2 = a ∧ ∃ σ' Q' R', + (do + Fl4SpecA64.maybeDonateSc (Z := Unit) (specPtr t) (specPtr n) + let b ← Fl4Spec.NonDet.gets (Fl4SpecA64.schedulable (Z := Unit) (specPtr t)) + Fl4Spec.NonDet.when b <| Fl4SpecA64.possibleSwitchTo (specPtr t) + let scopt ← Fl4SpecA64.getTcbObjRef Fl4SpecA64.Tcb.tcbSchedContext (specPtr t) + Fl4SpecA64.ifSporadicCurScTestRefillUnblockCheck scopt) σ = ({((), σ')}, False) ∧ + SchedSRA st { Z with Q := Q', R := R' } m κ r.1 σ' ⦄ := + WP.spec_mono (wake_tail_frameA H a hmb ht hon hN hst hnqs hnq hrunt hd) + fun _ ⟨e1, σ', Q', R', e2, H', _⟩ => ⟨e1, σ', Q', R', e2, H'⟩ + +end + +/-! ## The SC placement at boot -/ + +/-- **`ScPlacedA` at boot** (module docstring). -/ +theorem boot_scPlacedA {kb ib now mt : U64} {nc : Nat} (hL : BootLayA ib nc) (hK : BootKsA kb ib nc) : + ScPlacedA (bootThA kb ib nc) kb (bootCA ib nc) (bootMA ib nc) (bootSA kb ib nc now mt) := by + have hname : ∀ i, i < nc → scNameA (bootMA ib nc) (idleSA ib i) = + if i = 0 then Fl4SpecA64.idleScPtr else specPtr (idleSA ib i) := by + intro i hi + by_cases e : i = 0 + · subst e; rw [if_pos rfl]; exact bootRen0 hL + · rw [if_neg e]; exact bootRen hL (Nat.pos_of_ne_zero e) hi + have hsz : ∀ p x n, (bootSA kb ib nc now mt).kheap p = some (.schedContext x n) → n = 0 := by + intro p x n h + rw [bootSA_kheap] at h + unfold bootKA at h + split_ifs at h <;> cases h <;> rfl + have h128 : 2 ^ (0 + Fl4SpecA64.minSchedContextBits) = 128 := by unfold Fl4SpecA64.minSchedContextBits; rfl + have hf := hL.fit + refine ⟨?_, ?_, ?_⟩ + · rintro _ ⟨i, hi, rfl⟩ _ ⟨j, hj, rfl⟩ e + rw [hname i hi, hname j hj] at e + by_cases ei : i = 0 <;> by_cases ej : j = 0 + · rw [ei, ej] + · rw [if_pos ei, if_neg ej] at e; exact absurd e.symm (idleSA_ne_sc hL hj) + · rw [if_neg ei, if_pos ej] at e; exact absurd e (idleSA_ne_sc hL hi) + · rw [if_neg ei, if_neg ej] at e; rw [idleSA_inj hL hi hj e] + · rintro _ ⟨i, hi, rfl⟩ _ ⟨j, hj, rfl⟩ hne x n x' n' h1 h2 a a1 a2 + have e1 := hsz _ _ _ h1 + have e2 := hsz _ _ _ h2 + subst e1 e2 + unfold ScObjA at a1 a2 + rw [h128, idleSA_val hL hi] at a1 + rw [h128, idleSA_val hL hj] at a2 + have : i ≠ j := fun e => hne (by rw [e]) + omega + · rintro _ ⟨i, hi, rfl⟩ x n h1 a a1 + have e1 := hsz _ _ _ h1 + subst e1 + unfold ScObjA at a1 + rw [h128, idleSA_val hL hi] at a1 + have hap := hK.apart + unfold ksSizeA at hap ⊢ + refine ⟨fun p k hp => ?_, by omega⟩ + rcases bootTh_loc hL hK hp with ⟨c, hc, -, hv, -, hs⟩ | ⟨-, q, hq, hv, -, hs⟩ + · rw [hv, hs] + by_cases e : c = i + · subst e; omega + · omega + · rw [hv, hs]; omega + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScIdleA.lean b/proofs/Fl4ProofsA64/P3/ScIdleA.lean new file mode 100644 index 000000000..6107c6418 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScIdleA.lean @@ -0,0 +1,67 @@ +/- +P3 (aarch64, A64-P3-MCS gen 10): **the ruled `ScInvA.tcbScNotIdle` is false at boot** (ruling 01:39Z gen-10 item 2, +stop-first), and the one-line SC-name lemma it was meant to feed. + +* **`boot_tcbSc_idleA`**: in the boot state `bootSA` (and the same in l4v's `init_A_st`, `Init_A`: the idle thread + has `tcb_sched_context = Some idle_sc_ptr`) the idle thread of core 0 is a TCB bound to an idle SC. So "a + TCB-bound SC is never an idle SC" is not an invariant (it fails at `w0`, so `boot_scInvA` would not close); it + holds for the TCBs that are not idle threads (proposed restriction in the handoff). +* **`scNameA_eq_of_notIdle`**: an SC whose spec name is `NotIdleScA` is named by its address (`scNameA m sc = + specPtr sc`); this is the one line the join glue needs for `TimeoutScOpA`, from `NotIdleScA` of the thread's SC. + +Gen 11 (ruling 02:05Z): the restricted field is `ScInvA.tcbScNotIdle` (`P3/ScSchedA`); **`ScInvA.scName_of_notIdle`** +/ **`SchedSRA.scName_of_notIdle`** give `scNameA m sc = specPtr sc` for a thread that is no core's idle thread. + +No named premise. +-/ +import Fl4ProofsA64.P3.ScBootA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P1.W7.R.ST.BootWit + +noncomputable section + +/-- **An SC whose spec name is not idle is named by its address** (module docstring). -/ +theorem scNameA_eq_of_notIdle {m : Fl4SpecA64.McState} {sc : U64} (h : NotIdleScA m (scNameA m sc)) : + scNameA m sc = specPtr sc := by + unfold scNameA at h ⊢ + by_cases e : specPtr sc = m.idleScOf m.curCore + · rw [if_pos e] at h; exact absurd rfl h.1 + · rw [if_neg e] + +/-- **At boot a TCB is bound to an idle SC** (module docstring): the unrestricted `tcbScNotIdle` fails at `w0`. -/ +theorem boot_tcbSc_idleA {kb ib now mt : U64} {nc : Nat} (hL : BootLayA ib nc) + (hnc : nc ≤ Fl4SpecA64.fl4A64Platform.numNodes) : + ∃ u x p, (bootSA kb ib nc now mt).kheap u = some (.tcb x) ∧ x.tcbSchedContext = some p ∧ + ¬ NotIdleScA (bootMA ib nc) p := by + have hx := bootKA_tcb (kb := kb) (now := now) (mt := mt) hL hL.nc0 + obtain ⟨y, n, hy, -⟩ := bootKA_backA hL hx rfl + exact ⟨_, _, _, hx, rfl, (bootKA_scA hnc hy).2.2⟩ + +/-- (gen 11, ruling 02:05Z) **A non-idle thread's bound SC is named by its address** (`ScInvA.tcbScNotIdle` with +`scNameA_eq_of_notIdle`). -/ +theorem ScInvA.scName_of_notIdle {m : Fl4SpecA64.McState} {σ : Fl4SpecA64.State Unit} (h : ScInvA m σ) + {u : Fl4SpecA64.ObjRef} {x : Fl4SpecA64.Tcb} {sc : U64} (hx : σ.kheap u = some (.tcb x)) + (hp : x.tcbSchedContext = some (scNameA m sc)) + (hi : ∀ c < Fl4SpecA64.fl4A64Platform.numNodes, u ≠ m.idleThreadOf c) : scNameA m sc = specPtr sc := + scNameA_eq_of_notIdle (h.tcbScNotIdle u x _ hx hp hi) + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- (gen 11) **The SC word of a related thread that is no idle thread names its SC by address** (the join glue's +`scNameA m sc = specPtr sc` for `TimeoutScOpA`, from `SchedSRA`). -/ +theorem SchedSRA.scName_of_notIdle {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} + {σ : Fl4SpecA64.State Unit} (H : SchedSRA st Z m κ s σ) {t : U64} (ht : t ∈ Z.T) (h0 : L.mem s (scA t) ≠ 0#u64) + (hi : ∀ c < Fl4SpecA64.fl4A64Platform.numNodes, specPtr t ≠ m.idleThreadOf c) : + scNameA m (L.mem s (scA t)) = specPtr (L.mem s (scA t)) := by + obtain ⟨x, hx, hp⟩ := H.sch.tcb t ht + refine H.inv.scName_of_notIdle hx ?_ hi + rw [hp]; unfold optScA; rw [if_neg h0]; rfl + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScInstallA.lean b/proofs/Fl4ProofsA64/P3/ScInstallA.lean new file mode 100644 index 000000000..36a799a36 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScInstallA.lean @@ -0,0 +1,206 @@ +import Fl4ProofsA64.P3.ScSchedChargeA + +/-! The two scheduling-context switch branches. The outgoing SC is +charged before installing the selected TCB's SC. The phase inputs are +the [J] selected and outgoing SC views and the [P] tick agreement. -/ + +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3.Sched + +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +private theorem noContextStepA (σ : State Unit) : + (NonDet.pure () : SMonad Unit Unit) σ = ({((), σ)}, False) := rfl + +private theorem runIncomingUnblockA {σ τ : State Unit} {p : ObjRef} {x : SchedContext} {n : Nat} + (hk : σ.kheap p = some (.schedContext x n)) + (step : (NonDet.when (¬x.scSporadic) (refillUnblockCheck p)) σ = ({((), τ)}, False)) : + ifConstantBandwidthRefillUnblockCheck (Z := Unit) (some p) σ = ({((), τ)}, False) := by + unfold ifConstantBandwidthRefillUnblockCheck ifCondRefillUnblockCheck + simp only [NonDet.maybeM] + rw [nd_bind_oneSc (getSchedContext_eqA hk), nd_gets_bindSc, nd_pure_bindSc] + cases h : x.scSporadic <;> simpa [NonDet.when, h] using step + +/-- Refresh the incoming constant-bandwidth SC after requesting a timer +interrupt. The [J] active and release facts apply to this selected SC. -/ +theorem schedule_refresh_incomingA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {σ : State Unit} (H : YieldQueueRelA (st := st) Z m κ s σ) + {c : U64} (hc : c ∈ Z.C) {x : SchedContext} {n : Nat} + (hk : σ.kheap (scNameA m c) = some (.schedContext x n)) + (live : ¬x.scSporadic → scActive x = true ∧ + ∀ t ∈ σ.releaseQueue, ∀ y, σ.kheap t = some (.tcb y) → y.tcbSchedContext ≠ some (scNameA m c)) : + (do + let s1 ← sched.set_reprogram st s + let cb ← sporadic.sc_constant_bandwidth st s1 c + if cb then sporadic.unblock_check st s1 c else ok s1) ⦃ s' => + ∃ τ, ((NonDet.modify fun r : State Unit => {r with reprogramTimer := true}) >>= fun _ => + ifConstantBandwidthRefillUnblockCheck (some (scNameA m c))) σ = ({((), τ)}, False) ∧ + YieldQueueRelA (st := st) Z m κ s' τ ∧ MFrA (st := st) Z m s s' σ τ ⦄ := by + refine WP.spec_bind (sched.set_reprogram.spec s) ?_ + rintro s1 rfl + let τ := {σ with reprogramTimer := true} + let s1 := L.wr s (L.nodeA s layout.node.REPROGRAM) 1#u64 + have out := writeSchedulerTimerA H (o := layout.node.REPROGRAM) (Or.inl rfl) 1#u64 + have yes : (1#u64 : U64) ≠ 0#u64 := by decide + simp only [if_pos rfl, decide_eq_true yes] at out + have R : YieldQueueRelA (st := st) Z m κ s1 τ := out.1 + have frame : MFrA (st := st) Z m s s1 σ τ := out.2 + have shk : τ.kheap (scNameA m c) = some (.schedContext x n) := hk + obtain ⟨x', n', e, field⟩ := R.sched.scr.sc c hc + rw [shk] at e; cases e + have head : + (NonDet.modify fun r : State Unit => {r with reprogramTimer := true}) σ = ({((), τ)}, False) := rfl + unfold sporadic.sc_constant_bandwidth obj.Sc.sporadic + simp only [lift, bind_ok, L.read_eq, layout.sc.SPORADIC, scWA] + by_cases bw : ¬ x.scSporadic + · have isZero : L.mem s1 (scWA c 64#u64) = 0#u64 := by + by_contra nz + exact bw (field.sporadic.trans (decide_eq_true nz)) + change (if decide (L.mem s1 (scWA c 64#u64) = 0#u64) then _ else _) ⦃ _ ⦄ + simp only [isZero, decide_true, ↓reduceIte] + obtain ⟨active, nonrelease⟩ := live bw + refine WP.spec_mono (scheduler_unblock_refinesA R hc shk active nonrelease) ?_ + rintro s' ⟨ρ, abs, related, fm⟩ + refine ⟨ρ, ?_, related, frame.trans fm rfl rfl⟩ + rw [nd_bind_oneSc head] + exact runIncomingUnblockA shk (by rw [NonDet.when, if_pos bw]; exact abs) + · have nz : L.mem s1 (scWA c 64#u64) ≠ 0#u64 := by + intro isZero + apply bw + rw [field.sporadic, isZero] + decide + change (if decide (L.mem s1 (scWA c 64#u64) = 0#u64) then _ else _) ⦃ _ ⦄ + simp only [decide_eq_false nz, Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + refine ⟨τ, ?_, R, frame⟩ + rw [nd_bind_oneSc head] + exact runIncomingUnblockA shk (by rw [NonDet.when, if_neg bw]; rfl) + +/-- [J] The outgoing-SC offset and owner at the actual charging +boundary, asked only when the current SC is charged (active, not the +idle SC, with consumed time): an arm that unbinds or deactivates the +current SC owes nothing. Selection may have changed the current TCB. -/ +def OutgoingChargeA (m : McState) (σ : State Unit) : Prop := + ∀ x n, σ.kheap σ.curSc = some (.schedContext x n) → + scActive x = true → σ.curSc ≠ idleScPtr → σ.consumedTime ≠ 0 → + OffOKA σ ∧ OffReadyA σ ∧ NotIdleScA m σ.curSc ∧ + ∃ owner, (x.scTcb = none ∨ x.scTcb = some owner) ∧ owner ∉ σ.releaseQueue + +private theorem schedule_outgoingA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {σ : State Unit} (H : YieldQueueRelA (st := st) Z m κ s σ) + (a : A) (charge : σ.reprogramTimer = true → OutgoingChargeA m σ) + (agree : fl4MinBudget = MIN_BUDGET) (ticks : ScCfgTimeAgreeA ar a) : + (do + let cpu ← st.cur_cpu s + let prog ← obj.node_get st s cpu layout.node.REPROGRAM + if prog != 0#u64 then sched.commit_time st ar s a else ok s) ⦃ s' => + ∃ τ, (NonDet.when σ.reprogramTimer (fl4CommitTime (Z := Unit))) σ = ({((), τ)}, False) ∧ + YieldQueueRelA (st := st) Z m κ s' τ ∧ MFrA (st := st) Z m s s' σ τ ⦄ := by + rw [L.cur_cpu_eq, bind_ok, eq_of_spec_fd (obj.node_get.spec s _ _), bind_ok] + have read : (L.mem s (L.nodeAt s (L.cpu s) layout.node.REPROGRAM) != 0#u64) = σ.reprogramTimer := by + rw [H.sched.rest.repr] + generalize L.mem s (L.nodeAt s (L.cpu s) layout.node.REPROGRAM) = w + apply Bool.eq_iff_iff.2 + exact bne_iff_ne.trans (Iff.of_eq decide_eq_true_eq).symm + rw [read] + by_cases prog : σ.reprogramTimer = true + · rw [if_pos prog] + obtain ⟨x, n, hk, _⟩ := H.sched.scr.sc (L.cscW s) H.sched.cscC + rw [← H.sched.toSchedSRN.currentScA] at hk + by_cases quiet : scActive x = false ∨ σ.curSc = idleScPtr ∨ σ.consumedTime = 0 + · refine WP.spec_mono (commit_time_quietA H a hk quiet) ?_ + rintro s' ⟨τ, he, H1, F⟩ + exact ⟨τ, by rw [NonDet.when, if_pos prog]; exact he, H1, F⟩ + · have active : scActive x = true := by + cases h : scActive x + · exact (quiet (Or.inl h)).elim + · rfl + have notIdle : σ.curSc ≠ idleScPtr := fun h => quiet (Or.inr (Or.inl h)) + have used : σ.consumedTime ≠ 0 := fun h => quiet (Or.inr (Or.inr h)) + obtain ⟨O, R, ordinary, t, own, nonrelease⟩ := charge prog x n hk active notIdle used + refine WP.spec_mono (commit_time_activeA H hk active ordinary own nonrelease O R agree ticks) ?_ + rintro s' ⟨τ, he, H1, F⟩ + exact ⟨τ, by rw [NonDet.when, if_pos prog]; exact he, H1, F⟩ + · rw [if_neg prog, WP.spec_ok] + exact ⟨σ, by rw [NonDet.when, if_neg prog]; rfl, H, MFrA.refl _ _ _ _⟩ + +private theorem installGetScA {σ : State Unit} {x : Tcb} {p : ObjRef} + (hk : σ.kheap σ.curThread = some (.tcb x)) (link : x.tcbSchedContext = some p) : + (getTcbObjRef (Z := Unit) Tcb.tcbSchedContext σ.curThread >>= NonDet.assertOpt) σ = ({(p, σ)}, False) := by + unfold getTcbObjRef + rw [nd_bind_oneSc (threadGet_eqSc _ hk), link] + rfl + +/-- The abstract state that is ready for the outgoing SC charge. -/ +def scheduleIncomingStepA (σ : State Unit) (target : ObjRef) : SMonad Unit Unit := + NonDet.when (target ≠ σ.curSc) (do + NonDet.modify fun r : State Unit => {r with reprogramTimer := true} + ifConstantBandwidthRefillUnblockCheck (some target)) + +/-- Both SC branches refine the full context switch. The selected link +supplies the incoming SC. The outgoing billing predicates are required +only at the reached reprogram branch. -/ +theorem switch_sched_context_refinesA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {σ : State Unit} {a : A} + (H : YieldQueueRelA (st := st) Z m κ s σ) + {t : Tcb} (ht : σ.kheap σ.curThread = some (.tcb t)) + {c : U64} (hc : c ∈ Z.C) (word : L.mem s (scA (L.curW s)) = c) + (link : t.tcbSchedContext = some (scNameA m c)) + (incoming : ∀ x n, σ.kheap (scNameA m c) = some (.schedContext x n) → ¬x.scSporadic → + scActive x = true ∧ + ∀ t ∈ σ.releaseQueue, ∀ y, σ.kheap t = some (.tcb y) → y.tcbSchedContext ≠ some (scNameA m c)) + (outgoing : ∀ s1 σ1, YieldQueueRelA (st := st) Z m κ s1 σ1 → MFrA (st := st) Z m s s1 σ σ1 → + scheduleIncomingStepA σ (scNameA m c) σ = ({((), σ1)}, False) → + σ1.reprogramTimer = true → OutgoingChargeA m σ1) + (agree : fl4MinBudget = MIN_BUDGET) (ticks : ScCfgTimeAgreeA ar a) : + sched.switch_sched_context st ar s a ⦃ r => ∃ τ, + fl4SwitchSchedContext (Z := Unit) σ = ({((), τ)}, False) ∧ r.2 = a ∧ + YieldQueueRelA (st := st) {Z with curScOf := Function.update Z.curScOf m.curCore (scNameA m c)} + m κ r.1 τ ∧ MFrA (st := st) Z m s r.1 σ τ ⦄ := by + let p := scNameA m c + let refresh := scheduleIncomingStepA σ p + have same : L.cscW s = c ↔ p = σ.curSc := by + rw [H.sched.toSchedSRN.currentScA] + exact ⟨fun h => congrArg (scNameA m) h.symm, fun h => H.sched.scp.inj _ H.sched.cscC _ hc h.symm⟩ + unfold LawfulStore.curW at word + unfold sched.switch_sched_context + rw [eq_of_spec_fd (sched.cur_thread.spec s), bind_ok, eq_of_spec_fd (obj.Tcb.sc.spec s _), + bind_ok, word, eq_of_spec_fd (sched.cur_sc.spec s), bind_ok, sc_ne_eqA, bind_ok] + have prep : (if decide (L.cscW s ≠ c) then + (do + let s1 ← sched.set_reprogram st s + let cb ← sporadic.sc_constant_bandwidth st s1 c + if cb then sporadic.unblock_check st s1 c else ok s1) + else ok s) ⦃ s1 => ∃ σ1, refresh σ = ({((), σ1)}, False) ∧ + YieldQueueRelA (st := st) Z m κ s1 σ1 ∧ MFrA (st := st) Z m s s1 σ σ1 ⦄ := by + by_cases h : L.cscW s = c + · simp only [decide_eq_false (not_not.2 h), Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + exact ⟨σ, by dsimp only [refresh, scheduleIncomingStepA]; rw [NonDet.when, if_neg (not_not.2 (same.1 h))]; rfl, H, MFrA.refl _ _ _ _⟩ + · simp only [decide_eq_true h, ↓reduceIte] + obtain ⟨x, n, hk, _⟩ := H.sched.scr.sc c hc + refine WP.spec_mono (schedule_refresh_incomingA H hc hk (incoming x n hk)) ?_ + rintro s1 ⟨σ1, e, rel, frame⟩ + exact ⟨σ1, by dsimp only [refresh, scheduleIncomingStepA]; rw [NonDet.when, if_pos (mt same.2 h)]; exact e, rel, frame⟩ + refine WP.spec_bind prep ?_ + rintro s1 ⟨σ1, hr, H1, F1⟩ + rw [← cfgResultAssocA, ← cfgResultAssocA] + have account := schedule_outgoingA H1 a (outgoing s1 σ1 H1 F1 hr) agree ticks + rw [← cfgResultAssocA] at account + refine WP.spec_bind account ?_ + rintro s2 ⟨σ2, hb, H2, F2⟩ + rw [L.cur_cpu_eq, bind_ok, eq_of_spec_fd (obj.node_set.spec s2 _ _ _), bind_ok, WP.spec_ok] + have write := setCurrentScWriteA H2 hc + refine ⟨{σ2 with curSc := p}, ?_, rfl, write.1, (F1.trans F2 rfl rfl).trans write.2 rfl rfl⟩ + dsimp only [refresh, scheduleIncomingStepA, p] at hr + unfold fl4SwitchSchedContext + rw [nd_bind_oneSc (show NonDet.get σ = ({(σ, σ)}, False) from rfl), + nd_bind_oneSc (installGetScA ht link), nd_bind_oneSc hr, nd_gets_bindSc, nd_bind_oneSc hb] + rfl + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScInvokeRestartA.lean b/proofs/Fl4ProofsA64/P3/ScInvokeRestartA.lean new file mode 100644 index 000000000..f7a97da05 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScInvokeRestartA.lean @@ -0,0 +1,171 @@ +/- +P3 (AArch64): restart and the local invocation arms. Ports x86 +ScDecodeSc.bind_ntfn_invoke and unbind_ntfn_invoke with the caller's STATE +word and the notification's SC word explicit. +-/ +import Fl4ProofsA64.P3.ScNtfnLinkA +import Fl4ProofsA64.P3.ScSetStateA +import Fl4ProofsA64.P3.ScUnbObjA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section +open Fl4SpecA64 + +private theorem restartScFrameA {σ σ' : State Unit} {t : ObjRef} {x : Tcb} + (hk : σ.kheap t = some (.tcb x)) + (hk' : σ'.kheap = (updKSc σ t (.tcb { x with tcbState := .restart })).kheap) : + (∀ p y n, σ'.kheap p = some (.schedContext y n) ↔ σ.kheap p = some (.schedContext y n)) ∧ + (∀ p y, σ'.kheap p = some (.notification y) ↔ σ.kheap p = some (.notification y)) := by + have keep : ∀ p o, (∀ y, o ≠ .tcb y) → (σ'.kheap p = some o ↔ σ.kheap p = some o) := by + intro p o ho + rw [hk', updKSc_kheap] + by_cases e : p = t + · subst p + rw [Function.update_self, hk] + constructor <;> intro he <;> exact (ho _ (Option.some.inj he).symm).elim + · rw [Function.update_of_ne e] + exact ⟨fun p y n => keep p _ (fun _ e => by cases e), fun p y => keep p _ (fun _ e => by cases e)⟩ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +private theorem sRead_sc_heapA {Z : XSetsA} {m : McState} {s s' : S} {σ σ' : State Unit} + (hks : L.ksb s' = L.ksb s) + (hK : ∀ p y n, σ'.kheap p = some (.schedContext y n) → σ.kheap p = some (.schedContext y n)) {a : U64} : + SReadA st Z m s' σ' a → SReadA st Z m s σ a := by + intro h + rcases h with ⟨c, hc, o, ho, e⟩ | h | h | h | ⟨c, hc, y, n, hk, ho⟩ + · exact Or.inl ⟨c, hc, o, ho, e.trans (nodeAt_of_ksb hks c o)⟩ + · exact Or.inr (Or.inl (by rwa [hks] at h)) + · exact Or.inr (Or.inr (Or.inl (by rwa [hks] at h))) + · exact Or.inr (Or.inr (Or.inr (Or.inl h))) + · exact Or.inr (Or.inr (Or.inr (Or.inr ⟨c, hc, y, n, hK _ _ _ hk, ho⟩))) + +/-- The successful notification Bind and UnbindObject execution arms, +starting at their common Restart operation. -/ +theorem sc_ntfn_invokeA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRA st Z m κ s σ) (hst : StEqA Z.T (L.mem s) σ) + {ct sc n : U64} (hct : ct ∈ Z.T) (hc : sc ∈ Z.C) (hsc0 : sc ≠ 0#u64) (hn0 : n ≠ 0#u64) + {xc : Tcb} (hkc : σ.kheap (specPtr ct) = some (.tcb xc)) (hr : runnable xc.tcbState = true) + {xn : Notification} (hkn : σ.kheap (specPtr n) = some (.notification xn)) + (hal : Aligned (nScSc n)) (hout : ¬ SReadA st Z m s σ (nScSc n)) + (bind : Bool) (hni : bind = true → NotIdleScA m (scNameA m sc)) + (hn : bind = false → L.mem s (scWA sc 40#u64) = n) (badge : Badge) (rights : CapRights) : + (do + let s1 ← sched.set_thread_state st s ct sched.TS_RESTART + if bind then schedcontext.bind_ntfn st s1 sc n else schedcontext.unbind_ntfn st s1 sc) ⦃ s' => + ∃ σ1 σ', setThreadState (Z := Unit) (specPtr ct) .restart σ = ({((), σ1)}, False) ∧ + invokeSchedContext (Z := Unit) (if bind then + .invokeSchedContextBind (scNameA m sc) (.notificationCap (specPtr n) badge rights) + else .invokeSchedContextUnbindObject (scNameA m sc) (.notificationCap (specPtr n) badge rights)) σ1 = + ({([], σ')}, False) ∧ + SchedSRA st Z m κ s' σ' ∧ NtfnScEqA m n (L.mem s') σ' ∧ + WritesIn st s s' (fun a => MWrA (st := st) Z m σ s a ∨ a = stA ct ∨ a = nScSc n) ⦄ := by + have hv : sched.TS_RESTART.val = tsCodeA .restart := by rw [ts_vals.2.1]; rfl + refine WP.spec_bind (sc_set_runnable_stateA H hst hct hkc hr (ts := .restart) rfl hv) ?_ + rintro s1 ⟨σ1, he, H1, hs1, hk1, w1⟩ + obtain ⟨scK, ntK⟩ := restartScFrameA hkc hk1 + have hkN := (ntK _ _).2 hkn + have hout1 : ¬ SReadA st Z m s1 σ1 (nScSc n) := + fun h => hout (sRead_sc_heapA w1.1 (fun p y k => (scK p y k).1) h) + have hz : SSzA σ σ1 := fun p y k h => ⟨y, (scK p y k).1 h⟩ + have finish : ∀ {s' : S}, + WritesIn st s1 s' (fun b => MWrA (st := st) Z m σ1 s1 b ∨ b = nScSc n) → + WritesIn st s s' (fun b => MWrA (st := st) Z m σ s b ∨ b = stA ct ∨ b = nScSc n) := by + intro s' w2 + exact (w1.trans w2).mono fun b h => h.elim (fun h => h.elim (fun x => Or.inl (Or.inr x)) (fun e => Or.inr (Or.inl e))) fun h => + h.imp (mwr_back rfl rfl hz w1.1) Or.inr + cases bind + · simp only [Bool.false_eq_true, ↓reduceIte] + obtain ⟨xs, ns, hks, hx⟩ := H.scr.sc sc hc + have hk1s := (scK _ _ _).2 hks + obtain ⟨x1, n1, hks1, hx1⟩ := H1.scr.sc sc hc + rw [hk1s] at hks1 + cases hks1 + have hn1 : L.mem s1 (scWA sc 40#u64) = n := by + have e := hx1.ntfn.symm.trans hx.ntfn + rw [hn rfl, optRefS_ne hn0] at e + by_cases hz : L.mem s1 (scWA sc 40#u64) = 0#u64 + · rw [hz, optRefS_zero] at e + cases e + · rw [optRefS_ne hz] at e + exact specPtr_inj (Option.some.inj e) + refine WP.spec_mono (sc_unbind_ntfn_someA H1 hc hsc0 hn0 hal hout1 hn1 hkN) ?_ + rintro s' ⟨σ', hu, H', hn', w2⟩ + refine ⟨σ1, σ', he, ?_, H', hn', finish w2⟩ + change (do schedContextUnbindNtfn (Z := Unit) (scNameA m sc); pure ([] : List Data)) σ1 = _ + rw [nd_bind_oneSc hu] + rfl + · simp only [↓reduceIte] + refine WP.spec_mono (sc_bind_ntfnA H1 hc hsc0 hn0 hal hout1 (hni rfl) hkN) ?_ + rintro s' ⟨σ', hu, H', hn', w2⟩ + refine ⟨σ1, σ', he, ?_, H', hn', finish w2⟩ + change (do schedContextBindNtfn (Z := Unit) (scNameA m sc) (specPtr n); pure ([] : List Data)) σ1 = _ + rw [nd_bind_oneSc hu] + rfl + +/-- Restart the issuer, then execute the local thread UnbindObject arm. -/ +theorem sc_unbind_object_restartA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRA st Z m κ s σ) (hst : StEqA Z.T (L.mem s) σ) + {ct sc t : U64} (hct : ct ∈ Z.T) (hc : sc ∈ Z.C) (hsc0 : sc ≠ 0#u64) (ht : t ∈ Z.T) + {xc : Tcb} (hkc : σ.kheap (specPtr ct) = some (.tcb xc)) (hr : runnable xc.tcbState = true) + (hne : t ≠ ct) (hb : L.mem s (scTcbA sc) = t) (hs : L.mem s (scA t) = sc) + (hon : L.mem s (affA t) = L.cpu s) : + (do + let s1 ← sched.set_thread_state st s ct sched.TS_RESTART + schedcontext.unbind_tcb st s1 sc) ⦃ s' => ∃ σ1 σ' Q' R', + setThreadState (Z := Unit) (specPtr ct) .restart σ = ({((), σ1)}, False) ∧ + invokeSchedContext (Z := Unit) (.invokeSchedContextUnbindObject (scNameA m sc) (.threadCap (specPtr t))) σ1 = + ({([], σ')}, False) ∧ + SchedSRA st { Z with Q := Q', R := R' } m κ s' σ' ∧ + WritesIn st s s' (fun a => MWrA (st := st) Z m σ s a ∨ a = stA ct) ⦄ := by + have hv : sched.TS_RESTART.val = tsCodeA .restart := by rw [ts_vals.2.1]; rfl + refine WP.spec_bind (sc_set_runnable_stateA H hst hct hkc hr (ts := .restart) rfl hv) ?_ + rintro s1 ⟨σ1, he, H1, hst1, hk1, w1⟩ + obtain ⟨scK, -⟩ := restartScFrameA hkc hk1 + have hz : SSzA σ σ1 := fun p y k h => ⟨y, (scK p y k).1 h⟩ + obtain ⟨xs, n, hks, hx⟩ := H.scr.sc sc hc + have hks1 := (scK _ _ _).2 hks + obtain ⟨xs1, n1, hks1', hx1⟩ := H1.scr.sc sc hc + rw [hks1] at hks1' + cases hks1' + have ht0 := (tcb_ne01Sc (H.core.tcbs t ht)).1 + have hb1 : L.mem s1 (scTcbA sc) = t := by + have h := hx1.tcb.symm.trans hx.tcb + rw [← scTcbA_eqSc, hb, optRefS_ne ht0] at h + by_cases hz : L.mem s1 (scTcbA sc) = 0#u64 + · rw [hz, optRefS_zero] at h + cases h + · rw [optRefS_ne hz] at h + exact specPtr_inj (Option.some.inj h) + obtain ⟨xt, htK, htSc⟩ := H.sch.tcb t ht + obtain ⟨yt, hyK, hySc⟩ := H1.sch.tcb t ht + have hp : specPtr t ≠ specPtr ct := fun h => hne (specPtr_inj h) + rw [hk1, updKSc_kheap, Function.update_of_ne hp, htK] at hyK + cases hyK + have hs1 : L.mem s1 (scA t) = sc := by + have h := hySc.symm.trans (htSc.trans (congrArg (optScA m) hs)) + unfold optScA at h + rw [if_neg hsc0] at h + by_cases h0 : L.mem s1 (scA t) = 0#u64 + · rw [if_pos h0] at h + cases h + · rw [if_neg h0] at h + exact scRenA_inj m (H1.sch.scOff _ (H1.rest.scMem t ht h0)) (H.sch.scOff _ hc) (Option.some.inj h) + have hon1 : L.mem s1 (affA t) = L.cpu s1 := by + rw [H.aff_eq H1 ht ht, H.cpu_eq H1] + exact hon + refine WP.spec_mono (sc_unbind_object_threadA H1 hst1 hc hsc0 ht hb1 hs1 hon1) ?_ + rintro s' ⟨σ', Q', R', hu, H', F⟩ + refine ⟨σ1, σ', Q', R', he, hu, H', (w1.trans F.wr).mono ?_⟩ + intro b hb' + exact hb'.elim (fun h => h.imp Or.inr id) (fun h => Or.inl (mwr_back rfl rfl hz w1.1 h)) + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScInvokeTopA.lean b/proofs/Fl4ProofsA64/P3/ScInvokeTopA.lean new file mode 100644 index 000000000..666ab8ded --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScInvokeTopA.lean @@ -0,0 +1,144 @@ +/- +P3 (AArch64): successful local decoder and execution arms, with the +initial stall, caller Restart, and invocation kept at separate states. +-/ +import Fl4ProofsA64.P3.ScDecodeOpA +import Fl4ProofsA64.P3.ScInvokeRestartA +import Fl4ProofsA64.P3.ScBindStallA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section +open Fl4SpecA64 + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +theorem sc_stall_entryA (hA : P0.ArchTotal ar) + {Z : XSetsA} {m : McState} {κ : U64 → U64} {s s1 : S} {σ : State Unit} + (H : SchedSRA st Z m κ s σ) (hst : StEqA Z.T (L.mem s) σ) {a a1 : A} {t : U64} + (he : (if t != 0#u64 then smp.remote_tcb_stall st ar s a t else ok (s, a)) = ok (s1, a1)) + (hq : t != 0#u64 → smp.remote_tcb_stall st ar s a t = ok (s, a)) : + SchedSRA st Z m κ s1 σ ∧ StEqA Z.T (L.mem s1) σ ∧ MFrA (st := st) Z m s s1 σ σ := by + by_cases ht : t != 0#u64 + · have e := hq ht + rw [if_pos ht, e] at he + obtain ⟨rfl, rfl⟩ := Prod.mk.inj (ok_inj_ff he) + exact ⟨H, hst, MFrA.refl Z m s σ⟩ + · rw [if_neg ht] at he + obtain ⟨rfl, rfl⟩ := Prod.mk.inj (ok_inj_ff he) + exact ⟨H, hst, MFrA.refl Z m s σ⟩ + +/-- The successful local-thread UnbindObject decoder, from its entry +state through the initial stall, Restart and the scheduling updates. -/ +theorem sc_decode_unbind_object_threadA (hA : P0.ArchTotal ar) + {Z : XSetsA} {m : McState} {κ : U64 → U64} {s s1 : S} {σ : State Unit} + (H : SchedSRA st Z m κ s σ) (hst : StEqA Z.T (L.mem s) σ) + {a a1 : A} {sc t ct : U64} (hc : sc ∈ Z.C) (h0 : sc ≠ 0#u64) + (ht : t ∈ Z.T) (hct : ct ∈ Z.T) (hne : t ≠ ct) + {xc : Tcb} (hkc : σ.kheap (specPtr ct) = some (.tcb xc)) (hr : runnable xc.tcbState = true) + (htSc : obj.Sc.tcb st sc s = ok t) + (he : (if t != 0#u64 then smp.remote_tcb_stall st ar s a t else ok (s, a)) = ok (s1, a1)) + (hq : t != 0#u64 → smp.remote_tcb_stall st ar s a t = ok (s, a)) + (hc' : sched.cur_thread st s1 = ok ct) (htSc' : obj.Sc.tcb st sc s1 = ok t) + (hb : L.mem s1 (scA t) = sc) (hon : L.mem s1 (affA t) = L.cpu s1) + {call : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hcap : schedcontext.extra_cap st s1 extra 0#usize = ok (some (.Thread t))) + {label : Data} (hl : genInvocationType label = .schedContextUnbindObject) (rest : List Cap) : + schedcontext.decode st ar s a fl4_abi.label.SchedContextUnbindObject sc call extra ⦃ r => + r.1 = .Ok () ∧ r.2.2 = a1 ∧ ∃ σ1 σ' Q' R', + (decodeSchedContextInvocation (Z := Unit) label (scNameA m sc) (.threadCap (specPtr t) :: rest)).run σ = + ({(.ok (.invokeSchedContextUnbindObject (scNameA m sc) (.threadCap (specPtr t))), σ)}, False) ∧ + setThreadState (Z := Unit) (specPtr ct) .restart σ = ({((), σ1)}, False) ∧ + invokeSchedContext (Z := Unit) (.invokeSchedContextUnbindObject (scNameA m sc) (.threadCap (specPtr t))) σ1 = + ({([], σ')}, False) ∧ + SchedSRA st { Z with Q := Q', R := R' } m κ r.2.1 σ' ∧ + WritesIn st s r.2.1 (fun b => MWrA (st := st) Z m σ s b ∨ b = stA ct) ⦄ := by + obtain ⟨H1, hst1, F⟩ := sc_stall_entryA hA H hst he hq + have ht0 := (tcb_ne01Sc (H.core.tcbs t ht)).1 + have hd := ok_inj_ff ((eq_of_spec_fd (obj.Sc.tcb.spec (L := L) s1 sc)).symm.trans htSc') + have hct1 : L.curW s1 = ct := ok_inj_ff ((eq_of_spec_fd (sched.cur_thread.spec (L := L) s1)).symm.trans hc') + obtain ⟨xs, n, hk, hx⟩ := H1.scr.sc sc hc + have hsc : xs.scTcb = some (specPtr t) := by rw [hx.tcb, ← scTcbA_eqSc, hd, optRefS_ne ht0] + have htn : specPtr t ≠ σ.curThread := by rw [H1.sch.curThread, hct1]; exact fun e => hne (specPtr_inj e) + have hD := decodeUnbindObject_threadA hk hsc htn rest + rw [sc_decode_unbind_object_thread_eqA htSc he hc' hcap htSc' hne] + obtain ⟨sf, hF, σ1, σ', Q', R', hR, hI, H', w⟩ := + spec_ex_fd (sc_unbind_object_restartA H1 hst1 hct hc h0 ht hkc hr hne hd hb hon) + obtain ⟨sr, hsR, hsF⟩ := bind_eq_ok hF + rw [hsR, bind_ok, hsF, bind_ok, WP.spec_ok] + refine ⟨rfl, rfl, σ1, σ', Q', R', ?_, hR, hI, H', ?_⟩ + · unfold decodeSchedContextInvocation + rw [hl] + exact hD + · exact (F.wr.trans w).mono fun b h => h.elim Or.inl fun h => h.imp (mwr_back rfl rfl F.sz F.wr.1) id + +/-- The successful notification Bind and UnbindObject paths through the +concrete decoder, the abstract decoder, Restart and both link writes. -/ +theorem sc_decode_ntfn_invokeA (hA : P0.ArchTotal ar) + {Z : XSetsA} {m : McState} {κ : U64 → U64} {s s1 : S} {σ : State Unit} + (H : SchedSRA st Z m κ s σ) (hst : StEqA Z.T (L.mem s) σ) + {a a1 : A} {sc t ct n badge : U64} {send recv : Bool} (hc : sc ∈ Z.C) (h0 : sc ≠ 0#u64) (hn0 : n ≠ 0#u64) + (hct : ct ∈ Z.T) {xc : Tcb} (hkc : σ.kheap (specPtr ct) = some (.tcb xc)) (hr : runnable xc.tcbState = true) + (htSc : obj.Sc.tcb st sc s = ok t) + (he : (if t != 0#u64 then smp.remote_tcb_stall st ar s a t else ok (s, a)) = ok (s1, a1)) + (hq : t != 0#u64 → smp.remote_tcb_stall st ar s a t = ok (s, a)) + (hcur : sched.cur_thread st s1 = ok ct) + (hN : NtfnScEqA m n (L.mem s1) σ) + (hal : Aligned (nScSc n)) (hout : ¬ SReadA st Z m s1 σ (nScSc n)) + (b : Bool) (hni : b = true → NotIdleScA m (scNameA m sc)) + (hn : L.mem s1 (scWA sc 40#u64) = if b then 0#u64 else n) + (hns : b = true → L.mem s1 (nScSc n) = 0#u64) + {call : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hcap : schedcontext.extra_cap st s1 extra 0#usize = ok (some (.Notification n badge send recv))) + {label : Data} (hl : genInvocationType label = if b then .schedContextBind else .schedContextUnbindObject) + (rest : List Cap) : + let cap := Cap.notificationCap (specPtr n) badge.bv (rightsOfA send recv false false) + let iv : SchedContextInvocation := if b then .invokeSchedContextBind (scNameA m sc) cap else .invokeSchedContextUnbindObject (scNameA m sc) cap + schedcontext.decode st ar s a (if b then fl4_abi.label.SchedContextBind else fl4_abi.label.SchedContextUnbindObject) + sc call extra ⦃ r => r.1 = .Ok () ∧ r.2.2 = a1 ∧ ∃ σ1 σ', + (decodeSchedContextInvocation (Z := Unit) label (scNameA m sc) (cap :: rest)).run σ = + ({(.ok iv, σ)}, False) ∧ + setThreadState (Z := Unit) (specPtr ct) .restart σ = ({((), σ1)}, False) ∧ + invokeSchedContext (Z := Unit) iv σ1 = ({([], σ')}, False) ∧ + SchedSRA st Z m κ r.2.1 σ' ∧ NtfnScEqA m n (L.mem r.2.1) σ' ∧ + WritesIn st s r.2.1 (fun w => MWrA (st := st) Z m σ s w ∨ w = stA ct ∨ w = nScSc n) ⦄ := by + intro cap iv + obtain ⟨H1, hst1, F⟩ := sc_stall_entryA hA H hst he hq + obtain ⟨xs, ns, hks, hx⟩ := H1.scr.sc sc hc + obtain ⟨xn, hkn, hxn⟩ := hN + have hd : (decodeSchedContextInvocation (Z := Unit) label (scNameA m sc) (cap :: rest)).run σ = + ({(.ok iv, σ)}, False) := by + unfold decodeSchedContextInvocation + rw [hl] + cases b + · exact decodeUnbindObject_ntfn_eqA hks (by simp [hx.ntfn, hn, optRefS, hn0]) _ _ rest + · exact decodeBind_ntfn_eqA hks hkn (by simp [hx.ntfn, hn, optRefS]) + (by simp [hxn, hns rfl, optScA]) _ _ rest + have hP : schedcontext.decode st ar s a + (if b then fl4_abi.label.SchedContextBind else fl4_abi.label.SchedContextUnbindObject) sc call extra = + (do + let sr ← sched.set_thread_state st s1 ct sched.TS_RESTART + let sf ← if b then schedcontext.bind_ntfn st sr sc n else schedcontext.unbind_ntfn st sr sc + ok (core.result.Result.Ok (), sf, a1)) := by + have hNr := (eq_of_spec_fd (obj.Sc.ntfn.valA (L := L) s1 sc)).trans (congrArg ok hn) + cases b + · exact sc_decode_unbind_object_ntfn_eqA htSc he hcur hcap hNr + · have hN0 := (eq_of_spec_fd (ntfn_sc_specSc (L := L) s1 n)).trans (congrArg ok (hns rfl)) + exact sc_decode_bind_ntfn_eqA htSc he hcur hcap hNr hN0 + rw [hP] + obtain ⟨sf, hF, σ1, σ', hR, hI, H', hN', w⟩ := + spec_ex_fd (sc_ntfn_invokeA H1 hst1 hct hc h0 hn0 hkc hr hkn hal hout b hni + (fun hb => by rw [hn, hb]; rfl) badge.bv (rightsOfA send recv false false)) + obtain ⟨sr, hsR, hsF⟩ := bind_eq_ok hF + rw [hsR, bind_ok, hsF, bind_ok, WP.spec_ok] + exact ⟨rfl, rfl, σ1, σ', hd, hR, hI, H', hN', + (F.wr.trans w).mono fun q h => h.elim Or.inl fun h => h.imp (mwr_back rfl rfl F.sz F.wr.1) id⟩ + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScJoinA.lean b/proofs/Fl4ProofsA64/P3/ScJoinA.lean new file mode 100644 index 000000000..9ad912275 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScJoinA.lean @@ -0,0 +1,89 @@ +/- +P3 (aarch64, A64-P3-MCS gen 1): **how `ScStateRelA` meets `JAtZA`** (no x86 counterpart; the IPC line's `NtfnJoinA` +shape). Imports `JointA`; nothing imports this module but the root, so `JointA` can still import `P3/ScRelA` at the +coordinated join. + +* `scNameA_eq : scNameA = scRenA`, `optRefS_eq : optRefS = optRefA` (both `rfl`): the relation names SCs and + pointer words as `SchedRelA` does. +* `scNameA_inj_of`: `ScPlacedA.inj` is `SchedRelA.scOff` (with `scRenA_inj`), so at `JAtZA` it is not new content. +* `ScStateRelA.schedSc` / `.refillMax`: `SchedRelA.sc` (each SC's `sc_tcb`) and `SchedRestA.refillMax` are + projections of `ScStateRelA`: at the join they can be dropped from `SchedRelA` / `SchedRestA` (or kept and + derived). +* `ScStateRelA.wr_th` / `.wr_ks`: a write to a word of an object of `th` (a TCB, a CNode slot) or of the + kernel-state block (a node word) keeps the relation, by `ScPlacedA`; so every existing A64 leaf whose writes are in + `th` objects or the kernel block keeps `ScStateRelA` once `FrameA`'s footprint carries it. +* `ready_refinesJ`: `ready_refinesA` at `JAtZA`, its clock hypothesis discharged by `SchedRelA.time`. + +No named premise. +-/ +import Fl4ProofsA64.P3.ScRefillA +import Fl4ProofsA64.P3.JointA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +theorem scNameA_eq : scNameA = scRenA := rfl + +theorem optRefS_eq : optRefS = optRefA := rfl + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **`ScPlacedA.inj` from the scheduler relation** (`SchedRelA.scOff`, `scRenA_inj`). -/ +theorem scNameA_inj_of {T C Cs : Set U64} {curOf curScOf : Fl4SpecA64.CoreId → Fl4SpecA64.ObjRef} {s : S} + {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} (hS : SchedRelA (L := L) T C Cs curOf curScOf s σ m) : + ∀ c ∈ C, ∀ c' ∈ C, scNameA m c = scNameA m c' → c = c' := + fun c hc c' hc' e => scRenA_inj m (hS.scOff c hc) (hS.scOff c' hc') e + +/-- `SchedRelA.sc` is a projection of `ScStateRelA`. -/ +theorem ScStateRelA.schedSc {C : Set U64} {m : Fl4SpecA64.McState} {s : S} {σ : Fl4SpecA64.State Unit} + (h : ScStateRelA st C m s σ) : + ∀ c ∈ C, ∃ y n, σ.kheap (scRenA m c) = some (.schedContext y n) ∧ y.scTcb = optRefA (L.mem s (wa c 24#u64)) := + fun c hc => by + obtain ⟨x, n, hk, hx⟩ := h.sc c hc + exact ⟨x, n, hk, hx.tcb⟩ + +/-- `SchedRestA.refillMax` is a projection of `ScStateRelA`. -/ +theorem ScStateRelA.refillMax {C : Set U64} {m : Fl4SpecA64.McState} {s : S} {σ : Fl4SpecA64.State Unit} + (h : ScStateRelA st C m s σ) : + ∀ c ∈ C, ∃ y n, σ.kheap (scRenA m c) = some (.schedContext y n) ∧ + y.scRefillMax = (L.mem s (wa c 72#u64)).val := + fun c hc => by + obtain ⟨x, n, hk, hx⟩ := h.sc c hc + exact ⟨x, n, hk, by rw [← hx.max, LawfulStore.rmax, maxA_eqS]; rfl⟩ + +/-- **A write inside an object of `th` keeps the SC relation.** -/ +theorem ScStateRelA.wr_th {th : TypedHeapA} {ksb : U64} {C : Set U64} {m : Fl4SpecA64.McState} {s : S} + {σ : Fl4SpecA64.State Unit} (h : ScStateRelA st C m s σ) (P : ScPlacedA th ksb C m σ) {p : U64} {k : ObjKindA} + (hp : th p = some k) {b : U64} (hb : Aligned b) (hin : p.val ≤ b.val ∧ b.val < p.val + k.size) (v : U64) : + ScStateRelA st C m (L.wr s b v) σ := + h.frameW (writesIn_wr hb) (fun a e hf => by subst e; rcases P.th_out hf hp with h1 | h1 <;> omega) + (fun _ _ => rfl) + +/-- **A write inside the kernel-state block keeps the SC relation.** -/ +theorem ScStateRelA.wr_ks {th : TypedHeapA} {ksb : U64} {C : Set U64} {m : Fl4SpecA64.McState} {s : S} + {σ : Fl4SpecA64.State Unit} (h : ScStateRelA st C m s σ) (P : ScPlacedA th ksb C m σ) {b : U64} + (hb : Aligned b) (hin : ksb.val ≤ b.val ∧ b.val < ksb.val + ksSizeA) (v : U64) : + ScStateRelA st C m (L.wr s b v) σ := + h.frameW (writesIn_wr hb) (fun a e hf => by subst e; rcases P.ks_out hf with h1 | h1 <;> omega) + (fun _ _ => rfl) + +/-- **`ready` at `JAtZA`**: the clock hypothesis is `SchedRelA.time`. -/ +theorem ready_refinesJ {A : Type} {AR : A → HwRootA → Prop} {Z : XSetsA} {s : S} {a : A} + {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} (J : JAtZA (L := L) AR Z s a σ m) + (h : ScStateRelA st Z.C m s σ) {sc : obj.Sc} (hc : sc ∈ Z.C) {x : Fl4SpecA64.SchedContext} {n : Nat} + (hk : σ.kheap (scNameA m sc) = some (.schedContext x n)) (ha : Fl4SpecA64.scActive x = true) : + sporadic.ready st s sc ⦃ b => + Fl4SpecA64.getScRefillReady (Z := Unit) (scNameA m sc) σ = ({(b, σ)}, False) ⦄ := + ready_refinesA h J.sch.time hc hk ha + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScNtfnLinkA.lean b/proofs/Fl4ProofsA64/P3/ScNtfnLinkA.lean new file mode 100644 index 000000000..fe6813386 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScNtfnLinkA.lean @@ -0,0 +1,297 @@ +/- +P3 (AArch64): the notification and SC updates in Bind and Unbind. +The spec calculations port x86 ScNtfnBind.bind_ntfn_refines and +unbind_ntfn_refines. The explicit notification effect is outside SKeepA. +-/ +import Fl4ProofsA64.P3.ScUnbInvokeA +import Fl4ProofsA64.P3.ScDonA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section +open Fl4SpecA64 + +/-- The SC and notification fields change together. -/ +def scNtfnLinkStA (σ : State Unit) (p q : ObjRef) (xs : SchedContext) (ns : Nat) + (xn : Notification) (bind : Bool) : State Unit := + updKSc (updKSc σ q (.notification { xn with ntfnSc := if bind then some p else none })) p + (.schedContext { xs with scNtfn := if bind then some q else none } ns) + +theorem setNtfnSc_eqA {σ : State Unit} {q : ObjRef} {x : Notification} + (hk : σ.kheap q = some (.notification x)) (v : Option ObjRef) : + setNtfnObjRef (Z := Unit) ntfnScUpdate q v σ = + ({((), updKSc σ q (.notification { x with ntfnSc := v }))}, False) := by + unfold setNtfnObjRef updateSkObjRef + rw [nd_bind_oneSc (getSimpleKo_ntfnSc hk)] + unfold setSimpleKo + rw [nd_bind_oneSc (getObject_eqSc hk)] + rw [nd_bind_oneSc (show Fl4Spec.NonDet.assert (σ := State Unit) (isSimpleType (.notification x)) σ = + ({((), σ)}, False) by simp [Fl4Spec.NonDet.assert, isSimpleType, aType])] + rw [nd_bind_oneSc (show Fl4Spec.NonDet.assert (σ := State Unit) + (partialInv KernelObject.notification (.notification x) ≠ none) σ = ({((), σ)}, False) by + simp [Fl4Spec.NonDet.assert, partialInv_ntfnSc])] + exact setObject_eqSc hk rfl + +theorem getScNtfn_eqA {σ : State Unit} {p : ObjRef} {xs : SchedContext} {ns : Nat} + (hk : σ.kheap p = some (.schedContext xs ns)) : + getScObjRef (Z := Unit) SchedContext.scNtfn p σ = ({(xs.scNtfn, σ)}, False) := by + unfold getScObjRef + rw [nd_bind_oneSc (getSchedContext_eqA hk)] + rfl + +private theorem link_pair_eqA {σ : State Unit} {p q : ObjRef} {xs : SchedContext} {ns : Nat} + {xn : Notification} (hs : σ.kheap p = some (.schedContext xs ns)) (hn : σ.kheap q = some (.notification xn)) + (b : Bool) : + (do + setNtfnObjRef (Z := Unit) ntfnScUpdate q (if b then some p else none) + setScObjRef (Z := Unit) scNtfnUpdate p (if b then some q else none)) σ = + ({((), scNtfnLinkStA σ p q xs ns xn b)}, False) := by + have hpq : p ≠ q := by intro e; rw [e, hn] at hs; cases hs + rw [nd_bind_oneSc (setNtfnSc_eqA hn _)] + exact updateSchedContext_eqA (by rwa [updKSc_kheap, Function.update_of_ne hpq]) _ + +theorem schedContextBindNtfn_eqA {σ : State Unit} {p q : ObjRef} {xs : SchedContext} {ns : Nat} + {xn : Notification} (hs : σ.kheap p = some (.schedContext xs ns)) (hn : σ.kheap q = some (.notification xn)) : + schedContextBindNtfn (Z := Unit) p q σ = ({((), scNtfnLinkStA σ p q xs ns xn true)}, False) := + link_pair_eqA hs hn true + +theorem schedContextUnbindNtfn_someA {σ : State Unit} {p q : ObjRef} {xs : SchedContext} {ns : Nat} + {xn : Notification} (hs : σ.kheap p = some (.schedContext xs ns)) (hn : σ.kheap q = some (.notification xn)) + (hh : xs.scNtfn = some q) : + schedContextUnbindNtfn (Z := Unit) p σ = ({((), scNtfnLinkStA σ p q xs ns xn false)}, False) := by + unfold schedContextUnbindNtfn + rw [nd_bind_oneSc (getScNtfn_eqA hs), hh] + exact link_pair_eqA hs hn false + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +private theorem scRel_set_ntfnA {ii : Prop} {s : S} {sc : U64} {xs : SchedContext} {ns : Nat} + (h : ScRelWA st ii s sc xs ns) (v : U64) : + let s' := L.wr s (scWA sc 40#u64) v + ScRelWA st ii s' sc { xs with scNtfn := optRefS v } ns ∧ WritesIn st s s' (ScObjA sc ns) := by + intro s' + obtain ⟨hR, hp, hm, hw, hwr, hf⟩ := h.wr_hdr (K := 40#u64) (Or.inr (Or.inr (Or.inr (Or.inl rfl)))) v + have keep : ∀ k : U64, k.val < 96 → k.val % 8 = 0 → k.val ≠ 40 → + L.mem s' (scWA sc k) = L.mem s (scWA sc k) := + fun k h1 h2 h3 => hw k h1 h2 (fun e => h3 (by subst e; simp)) + refine ⟨⟨h.pl, by rw [hp]; exact h.period, by rw [keep 8#u64 (by simp) (by simp) (by simp)]; exact h.consumed, + by rw [keep 24#u64 (by simp) (by simp) (by simp)]; exact h.tcb, + by rw [keep 32#u64 (by simp) (by simp) (by simp)]; exact h.reply, by rw [hwr], + by rw [keep 48#u64 (by simp) (by simp) (by simp)]; exact h.badge, + by rw [keep 56#u64 (by simp) (by simp) (by simp)]; exact h.yieldFrom, + by rw [keep 64#u64 (by simp) (by simp) (by simp)]; exact h.sporadic, + fun h0 => by rw [hm]; exact h.spor (by rwa [keep 64#u64 (by simp) (by simp) (by simp)] at h0), + fun h0 => by rw [hm]; exact h.idle h0, by rw [hm]; exact h.max, fun ha => ?_⟩, hf⟩ + have ha' : (L.rmax s sc).val ≠ 0 := by rwa [hm] at ha + have h1 := h.act ha' + exact ⟨(hR ha').1, by rw [hp]; exact h1.period, + by rw [keep 8#u64 (by simp) (by simp) (by simp)]; exact h1.consumed, + by rw [keep 24#u64 (by simp) (by simp) (by simp)]; exact h1.tcb, + by rw [keep 32#u64 (by simp) (by simp) (by simp)]; exact h1.reply, by rw [hwr], + by rw [keep 48#u64 (by simp) (by simp) (by simp)]; exact h1.badge, + by rw [keep 56#u64 (by simp) (by simp) (by simp)]; exact h1.yieldFrom, + by rw [keep 64#u64 (by simp) (by simp) (by simp)]; exact h1.sporadic, + by rw [hm]; exact h1.max, by rw [(hR ha').2]; exact h1.refills⟩ + +private theorem ntfnLinkFramesA {σ : State Unit} {q : ObjRef} {x : Notification} + (h : σ.kheap q = some (.notification x)) (v : Option ObjRef) : + let σ' := updKSc σ q (.notification { x with ntfnSc := v }) + (∀ p y n, σ'.kheap p = some (.schedContext y n) ↔ σ.kheap p = some (.schedContext y n)) ∧ + (∀ p y, σ'.kheap p = some (.tcb y) ↔ σ.kheap p = some (.tcb y)) := by + intro σ' + have kept : ∀ p o, (∀ z, o ≠ .notification z) → (σ'.kheap p = some o ↔ σ.kheap p = some o) := by + intro p o ho + by_cases e : p = q + · subst p + rw [h] + have hq : σ'.kheap q = some (.notification { x with ntfnSc := v }) := by simp [σ', updKSc] + rw [hq] + constructor <;> intro e <;> exact (ho _ (Option.some.inj e).symm).elim + · simp only [σ', updKSc_kheap, Function.update_of_ne e] + exact ⟨fun p y n => kept p _ (fun _ e => by cases e), fun p y => kept p _ (fun _ e => by cases e)⟩ + +private theorem scInv_set_ntfnA {m : McState} {σ : State Unit} (H : ScInvA m σ) + {q : ObjRef} {x : Notification} (hk : σ.kheap q = some (.notification x)) + (v : Option ObjRef) (hv : ∀ p, v = some p → NotIdleScA m p) : + ScInvA m (updKSc σ q (.notification { x with ntfnSc := v })) := by + let σ' := updKSc σ q (.notification { x with ntfnSc := v }) + obtain ⟨scK, tK⟩ := ntfnLinkFramesA hk v + have hn : ∀ p y, σ'.kheap p = some (.notification y) → + (p = q ∧ y.ntfnSc = v) ∨ σ.kheap p = some (.notification y) := by + intro p y h + by_cases e : p = q + · subst p + rw [show σ'.kheap q = some (.notification { x with ntfnSc := v }) by simp [σ', updKSc]] at h + cases h + exact Or.inl ⟨rfl, rfl⟩ + · exact Or.inr (by simpa [σ', updKSc_kheap, Function.update_of_ne e] using h) + have hrel := H.relKeep (σ' := σ') rfl + (fun u _ y hy => ⟨y, (tK u y).2 hy, rfl⟩) (fun u _ z p hu hz => by + obtain ⟨y, k, hy, -⟩ := H.back u z p hu hz + exact ((scK p y k).2 hy).trans hy.symm) + refine ⟨fun p y n h => H.refills p y n ((scK p y n).1 h), fun p y n h => H.rr p y n ((scK p y n).1 h), + ?_, ?_, hrel.1, hrel.2, ?_, fun u y p hy hp => H.tcbScNotIdle u y p ((tK u y).1 hy) hp⟩ + · intro p y hy + rcases hn p y hy with ⟨-, e⟩ | h + · intro hh; exact (hv _ (e.symm.trans hh)).1 rfl + · exact H.ntfnIdle p y h + · intro p y hy c hc + rcases hn p y hy with ⟨-, e⟩ | h + · intro hh; exact (hv _ (e.symm.trans hh)).2 c hc rfl + · exact H.ntfnIdleOf p y h c hc + · intro u y p hy hp + obtain ⟨z, k, h, hb⟩ := H.back u y p ((tK u y).1 hy) hp + exact ⟨z, k, (scK p z k).2 h, hb⟩ + +private theorem sched_ntfn_onlyA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRA st Z m κ s σ) {q : ObjRef} {xn : Notification} + (hk : σ.kheap q = some (.notification xn)) (v : Option ObjRef) + (hni : ∀ p, v = some p → NotIdleScA m p) : + SchedSRA st Z m κ s (updKSc σ q (.notification { xn with ntfnSc := v })) := by + let σ' := updKSc σ q (.notification { xn with ntfnSc := v }) + obtain ⟨sK, tK⟩ := ntfnLinkFramesA hk v + have hsc : ∀ c ∈ Z.C, σ'.kheap (scNameA m c) = σ.kheap (scNameA m c) := by + intro c hc + obtain ⟨x, n, hs, -⟩ := H.scr.sc c hc + exact ((sK _ x n).2 hs).trans hs.symm + have rd : RdA σ' := by + intro t x hx hcl u p y n hu hup hy ha hnr + exact H.rd t x ((tK t x).1 hx) hcl u p y n ((tK t u).1 hu) hup ((sK p y n).1 hy) ha hnr + have run : RunQA σ' := by + refine ⟨fun d p u hu => ?_, fun u hu => ?_⟩ + · obtain ⟨x, hx, hr⟩ := H.run.1 d p u hu + exact ⟨x, (tK u x).2 hx, hr⟩ + · obtain ⟨x, hx, hr⟩ := H.run.2 u hu + exact ⟨x, (tK u x).2 hx, hr⟩ + exact H.rebuild (Q' := Z.Q) rfl rfl (fun _ _ _ _ _ _ => rfl) (fun _ _ => rfl) (fun _ _ _ _ => rfl) + (fun _ _ _ => rfl) hsc (fun t _ x hx => ⟨x, (tK (specPtr t) x).2 hx, rfl, rfl, rfl⟩) + ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ H.sch.act H.actOn rd (scInv_set_ntfnA H.inv hk v hni) + H.core H.rest.rq H.rest.rqDom rfl run + +private theorem set_sc_ntfn_stepA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRA st Z m κ s σ) {sc : U64} (hc : sc ∈ Z.C) {xs : SchedContext} {ns : Nat} + (hk : σ.kheap (scNameA m sc) = some (.schedContext xs ns)) (n : U64) : + let s' := L.wr s (scWA sc 40#u64) n + let σ' := updKSc σ (scNameA m sc) (.schedContext { xs with scNtfn := optRefS n } ns) + SchedSRA st Z m κ s' σ' ∧ WritesIn st s s' (ScObjA sc ns) := by + intro s' σ' + obtain ⟨x, k, h, hx⟩ := H.scr.sc sc hc + rw [hk] at h + cases h + obtain ⟨hx', hw⟩ := scRel_set_ntfnA hx n + have e : scCoreA sc = scWA sc 16#u64 := by unfold scCoreA scWA layout.sc.CORE; rfl + have h40 := hx.hdr (K := 40#u64) (by simp) (by simp) + have h16 := hx.hdr (K := 16#u64) (by simp) (by simp) + obtain ⟨-, -, hfit⟩ := hx.bounds + have hcc : L.mem s' (scCoreA sc) = L.mem s (scCoreA sc) := by + rw [e] + refine L.mem_wr_ne h40.1 h16.1 ?_ + rw [scWA_val (sc := sc) (K := 40#u64) (by simp; omega), + scWA_val (sc := sc) (K := 16#u64) (by simp; omega)] + simp + exact ⟨H.scObjA hc hk hw hx' rfl rfl rfl rfl hcc, hw⟩ + +private theorem sc_ntfn_linksA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRA st Z m κ s σ) {sc n : U64} (hc : sc ∈ Z.C) (hsc0 : sc ≠ 0#u64) (hn0 : n ≠ 0#u64) + (hal : Aligned (nScSc n)) (hout : ¬ SReadA st Z m s σ (nScSc n)) + {xs : SchedContext} {ns : Nat} {xn : Notification} + (hks : σ.kheap (scNameA m sc) = some (.schedContext xs ns)) (hkn : σ.kheap (specPtr n) = some (.notification xn)) + (b : Bool) (hni : b = true → NotIdleScA m (scNameA m sc)) : + let s' := L.wr (L.wr s (nScSc n) (if b then sc else 0#u64)) (scWA sc 40#u64) (if b then n else 0#u64) + let σ' := scNtfnLinkStA σ (scNameA m sc) (specPtr n) xs ns xn b + SchedSRA st Z m κ s' σ' ∧ NtfnScEqA m n (L.mem s') σ' ∧ + WritesIn st s s' (fun a => MWrA (st := st) Z m σ s a ∨ a = nScSc n) := by + let nv := if b then sc else 0#u64 + let sv := if b then n else 0#u64 + let np := if b then some (scNameA m sc) else none + let sp := if b then some (specPtr n) else none + let s1 := L.wr s (nScSc n) nv + let σ1 := updKSc σ (specPtr n) (.notification { xn with ntfnSc := np }) + have eNp : optScA m nv = np := by cases b <;> simp [nv, np, optScA, hsc0, scNameA_eq] + have eSp : optRefS sv = sp := by cases b <;> simp [sv, sp, optRefS, hn0] + have w1 : WritesIn st s s1 (· = nScSc n) := writesIn_wr hal + have hS : SStepA σ σ := + ⟨SFixA.refl σ, rfl, rfl, rfl, fun _ _ _ _ => rfl, fun _ x hx => ⟨x, hx, rfl, rfl, rfl⟩, + fun _ _ _ _ => rfl, fun _ x hx => ⟨x, hx⟩, fun _ x hx => ⟨x, hx, rfl⟩⟩ + have H1 := H.stable w1 (fun _ e => e ▸ hout) hS + (fun q x y hx hy => by rw [hx] at hy; cases hy; exact Or.inl ⟨rfl, id⟩) + have H2 : SchedSRA st Z m κ s1 σ1 := sched_ntfn_onlyA H1 hkn np (by + intro p hp + cases b + · simp [np] at hp + · change some (scNameA m sc) = some p at hp + cases hp + exact hni rfl) + have hks1 := ((ntfnLinkFramesA hkn np).1 _ xs ns).2 hks + obtain ⟨H3, w3⟩ := set_sc_ntfn_stepA H2 hc hks1 sv + have hfoot : ¬ ScObjA sc ns (nScSc n) := fun hab => hout + (Or.inr (Or.inr (Or.inr (Or.inr ⟨sc, hc, xs, ns, hks, hab⟩)))) + have hpq : specPtr n ≠ scNameA m sc := by intro e; rw [e, hks] at hkn; cases hkn + refine ⟨?_, ?_, (w1.trans w3).mono fun a ha => ha.elim Or.inr (fun hh => Or.inl (mwr_scObj hc hks hh))⟩ + · change SchedSRA st Z m κ (L.wr s1 (scWA sc 40#u64) sv) + (updKSc σ1 (scNameA m sc) (.schedContext { xs with scNtfn := sp } ns)) + rw [← eSp] + exact H3 + · refine ⟨{ xn with ntfnSc := np }, ?_, ?_⟩ + · simp [scNtfnLinkStA, updKSc_kheap, hpq, np] + · change np = optScA m (L.mem (L.wr s1 (scWA sc 40#u64) sv) (nScSc n)) + rw [w3.mem_eq hal hfoot, L.mem_wr_same hal] + exact eNp.symm + +/-- Notification Bind writes both links, with the notification word explicit +in the footprint alongside the scheduler words. -/ +theorem sc_bind_ntfnA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRA st Z m κ s σ) {sc n : U64} (hc : sc ∈ Z.C) (hsc0 : sc ≠ 0#u64) (hn0 : n ≠ 0#u64) + (hal : Aligned (nScSc n)) (hout : ¬ SReadA st Z m s σ (nScSc n)) + (hni : NotIdleScA m (scNameA m sc)) {xn : Notification} + (hkn : σ.kheap (specPtr n) = some (.notification xn)) : + schedcontext.bind_ntfn st s sc n ⦃ s' => ∃ σ', + schedContextBindNtfn (Z := Unit) (scNameA m sc) (specPtr n) σ = ({((), σ')}, False) ∧ + SchedSRA st Z m κ s' σ' ∧ NtfnScEqA m n (L.mem s') σ' ∧ + WritesIn st s s' (fun a => MWrA (st := st) Z m σ s a ∨ a = nScSc n) ⦄ := by + obtain ⟨xs, ns, hks, -⟩ := H.scr.sc sc hc + unfold schedcontext.bind_ntfn + refine WP.spec_bind (obj.Ntfn.set_sc.wr s n sc) ?_ + rintro _ rfl + refine WP.spec_mono (obj.Sc.set_ntfn.wr _ sc n) ?_ + rintro _ rfl + refine ⟨_, schedContextBindNtfn_eqA hks hkn, ?_⟩ + simpa only [scNtfnA, ntfnScA, scWA, nScSc, layout.sc.NTFN, ↓reduceIte] using + sc_ntfn_linksA H hc hsc0 hn0 hal hout hks hkn true (fun _ => hni) + +/-- Live notification Unbind clears both links. -/ +theorem sc_unbind_ntfn_someA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRA st Z m κ s σ) {sc n : U64} (hc : sc ∈ Z.C) (hsc0 : sc ≠ 0#u64) (hn0 : n ≠ 0#u64) + (hal : Aligned (nScSc n)) (hout : ¬ SReadA st Z m s σ (nScSc n)) (hw : L.mem s (scWA sc 40#u64) = n) + {xn : Notification} (hkn : σ.kheap (specPtr n) = some (.notification xn)) : + schedcontext.unbind_ntfn st s sc ⦃ s' => ∃ σ', + schedContextUnbindNtfn (Z := Unit) (scNameA m sc) σ = ({((), σ')}, False) ∧ + SchedSRA st Z m κ s' σ' ∧ NtfnScEqA m n (L.mem s') σ' ∧ + WritesIn st s s' (fun a => MWrA (st := st) Z m σ s a ∨ a = nScSc n) ⦄ := by + obtain ⟨xs, ns, hks, hx⟩ := H.scr.sc sc hc + have hn : xs.scNtfn = some (specPtr n) := by rw [hx.ntfn, hw, optRefS_ne hn0] + unfold schedcontext.unbind_ntfn + refine WP.spec_bind (obj.Sc.is_null.spec sc) ?_ + rintro _ rfl + simp only [decide_eq_false hsc0, Bool.false_eq_true, ↓reduceIte] + refine WP.spec_bind (obj.Sc.ntfn.valA s sc) ?_ + rintro _ rfl + rw [hw] + rw [show (n != 0#u64) = true from bne_iff_ne.2 hn0] + simp only [↓reduceIte] + refine WP.spec_bind (obj.Ntfn.set_sc.wr (st := st) s n 0#u64) ?_ + rintro _ rfl + refine WP.spec_mono (obj.Sc.set_ntfn.wr _ sc 0#u64) ?_ + rintro _ rfl + refine ⟨_, schedContextUnbindNtfn_someA hks hkn hn, ?_⟩ + simpa only [scNtfnA, ntfnScA, scWA, nScSc, layout.sc.NTFN, Bool.false_eq_true, ↓reduceIte] using + sc_ntfn_linksA H hc hsc0 hn0 hal hout hks hkn false (by simp) + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScPopA.lean b/proofs/Fl4ProofsA64/P3/ScPopA.lean new file mode 100644 index 000000000..ebb8298a2 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScPopA.lean @@ -0,0 +1,271 @@ +/- +P3 (aarch64, A64-P3-MCS gen 13): **the SC half of `reply_pop` / `reply_remove` / `reply_remove_tcb`** on `SchedSRA` +(ruling 03:02Z gen-13 item 1; x86 `P3/ScCallPopRef`'s `reply_pop_refines` (its `replyUnlinkSc` computation `hfree` +and the SC `REPLY` write), `P3/ScCallRemove`'s `reply_remove_refinesU` (the cut case) and `P3/ScRemoveTcb`'s +`reply_remove_tcb_sc_refines` (`replyRemoveTcbSc`, its SC part), ported without x86's `CallRel`: on AArch64 the reply +words and the reply relation are IPC's). + +FL4 (`crates/fl4-core/src/ipc.rs:185–239`) writes the SC's `REPLY` word only when the reply is the head of the SC's +stack: `Sc(next).set_reply(s, prev)` in `reply_pop`, `Sc(next).set_reply(s, 0)` in `reply_remove_tcb`; a reply further +down leaves the SC untouched. l4v changes `sc_replies` in all three cases and the replies' `reply_sc`. + +* The spec: `fixHdA` / **`popStA`** / **`replyUnlinkSc_popA`** (l4v `reply_unlink_sc` of the stack's head: + `sc_replies := tl`, the new head's `reply_sc := Some N`, the popped reply's `reply_sc := None`); + **`replyUnlinkSc_cutA`** (`reply_unlink_sc` of a reply below the head: `sc_replies := takeWhile (≠ R)`, `repStA`); + **`cutRsc_eqA`** (`reply_remove_tcb`'s SC update and `reply_sc := None`), `takeWhile_headRA` / `takeWhile_headA`. +* **`reply_pop_scA`** (x86 `reply_pop_refines`, its SC-word part): FL4 `obj.Sc.set_reply sc v` (`v` the popped + reply's `PREV`, the stack's second element) against `reply_unlink_sc N R`: single outcome `popStA`, `SchedSRA` after, + FL4 writing in `MWrA`, `SSzA`. +* **`reply_remove_tcb_scA`** (x86 `reply_remove_tcb_sc_refines`, `cut0`): FL4 `obj.Sc.set_reply sc 0` of the head + against `reply_remove_tcb`'s `sc_replies := takeWhile (≠ R)` (= `[]`) and `reply_sc := None`. +* **`ScRelWA.same_reply`**, **`SchedSRA.cutA`** (x86 `CallRel.cut`'s SC side): a spec reply list with the same head + keeps `SchedSRA` with FL4 unchanged, frame `MFrA`; **`reply_remove_cutA`** / **`reply_remove_tcb_cutA`**: the + non-head cases of `reply_remove` (`reply_unlink_sc`) and `reply_remove_tcb`. + +No named premise. Hypotheses (existing style, IPC's reply relation supplies them): `hc` (`sc ∈ Z.C`), `hkr` / `hrs` +(the reply `R` is a spec reply with its `reply_sc`), `hhd` (the stack's head), `hl` (the new head is the word written), +`htl` (the new head is a reply). +-/ +import Fl4ProofsA64.P3.ScReplyBA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +/-! ## The spec -/ + +section Spec +open Fl4SpecA64 + +/-- The new head's `reply_sc := Some N` of `reply_unlink_sc` (if the popped stack has one). -/ +def fixHdA (σ : State Unit) (N : ObjRef) : List ObjRef → State Unit + | [] => σ + | h :: _ => rscA σ h (some N) + +theorem fixHdA_nil (σ : State Unit) (N : ObjRef) : fixHdA σ N [] = σ := rfl + +theorem fixHdA_cons (σ : State Unit) (N h : ObjRef) (t : List ObjRef) : fixHdA σ N (h :: t) = rscA σ h (some N) := rfl + +theorem fixHdA_sc {σ : State Unit} {N : ObjRef} {l : List ObjRef} {q : ObjRef} {y : SchedContext} {n : Nat} : + (fixHdA σ N l).kheap q = some (.schedContext y n) ↔ σ.kheap q = some (.schedContext y n) := by + cases l with + | nil => exact Iff.rfl + | cons h t => exact rscA_sc + +/-- **The spec state after `reply_unlink_sc N R` of the stack's head `R`** (module docstring). -/ +def popStA (σ : State Unit) (N : ObjRef) (xs : SchedContext) (ns : Nat) (R : ObjRef) : State Unit := + rscA (fixHdA (repStA σ N xs ns xs.scReplies.tail) N xs.scReplies.tail) R none + +theorem popStA_def (σ : State Unit) (N : ObjRef) (xs : SchedContext) (ns : Nat) (R : ObjRef) : + popStA σ N xs ns R = rscA (fixHdA (repStA σ N xs ns xs.scReplies.tail) N xs.scReplies.tail) R none := rfl + +theorem scReplies_getA {σ : State Unit} {N : ObjRef} {xs : SchedContext} {ns : Nat} + (hks : σ.kheap N = some (.schedContext xs ns)) : + Fl4Spec.NonDet.liftM SchedContext.scReplies (getSchedContext (Z := Unit) N) σ = ({(xs.scReplies, σ)}, False) := by + unfold Fl4Spec.NonDet.liftM + exact nd_bind_oneSc (getSchedContext_eqA hks) _ + +theorem setReplySc_rscA {σ : State Unit} {R : ObjRef} {xr : Reply} (h : σ.kheap R = some (.reply xr)) + (v : Option ObjRef) : setReplyObjRef (Z := Unit) replyScUpdate R v σ = ({((), rscA σ R v)}, False) := by + rw [rscA_of h]; exact setReplySc_eqA h v + +theorem repStA_ne {σ : State Unit} {N : ObjRef} {xs : SchedContext} {ns : Nat} {l : List ObjRef} {q : ObjRef} + (h : q ≠ N) : (repStA σ N xs ns l).kheap q = σ.kheap q := by + unfold repStA; rw [updKSc_kheap, Function.update_of_ne h] + +/-- **l4v `reply_unlink_sc N R` of the stack's head** (x86 `reply_pop_refines`' `hfree`). -/ +theorem replyUnlinkSc_popA {σ : State Unit} {N R : ObjRef} {xs : SchedContext} {ns : Nat} {xr : Reply} + (hks : σ.kheap N = some (.schedContext xs ns)) (hkr : σ.kheap R = some (.reply xr)) + (hrs : xr.replySc = some N) (hhd : xs.scReplies.head? = some R) + (htl : ∀ h, xs.scReplies.tail.head? = some h → ∃ xh, σ.kheap h = some (.reply xh)) : + replyUnlinkSc (Z := Unit) N R σ = ({((), popStA σ N xs ns R)}, False) := by + have hNR : R ≠ N := fun e => by rw [e, hks] at hkr; cases hkr + have hgr : getReply (Z := Unit) R σ = ({(xr, σ)}, False) := getSimpleKo_replyA hkr + have hass : Fl4Spec.NonDet.assert (σ := State Unit) (xr.replySc = some N) σ = ({((), σ)}, False) := by + simp [Fl4Spec.NonDet.assert, hrs] + have hk1 : (repStA σ N xs ns xs.scReplies.tail).kheap R = some (.reply xr) := by rw [repStA_ne hNR]; exact hkr + unfold replyUnlinkSc popStA + rw [nd_bind_oneSc (scReplies_getA hks)] + try beta_reduce + rw [nd_bind_oneSc hgr] + try beta_reduce + rw [hhd, show the (some R) = R from rfl, if_pos rfl, nd_bind_oneSc hass, + nd_bind_oneSc (updateSchedContext_eqA hks _), + show updKSc σ N (.schedContext (scRepliesUpdate List.tail xs) ns) = repStA σ N xs ns xs.scReplies.tail from rfl] + revert hk1 + cases hT : xs.scReplies.tail with + | nil => + intro hk1 + rw [fixHdA_nil] + exact setReplySc_rscA hk1 none + | cons h t => + intro hk1 + obtain ⟨xh, hkh⟩ := htl h (by rw [hT]; rfl) + have hhN : h ≠ N := fun e => by rw [e, hks] at hkh; cases hkh + have hkh1 : (repStA σ N xs ns (h :: t)).kheap h = some (.reply xh) := by rw [repStA_ne hhN]; exact hkh + rw [fixHdA_cons] + refine (nd_bind_oneSc (setReplySc_eqA hkh1 (some N)) _).trans ?_ + rw [← rscA_of hkh1 (some N)] + by_cases e : R = h + · subst e + exact setReplySc_rscA (xr := { xh with replySc := some N }) + (by rw [rscA_of hkh1, updKSc_kheap, Function.update_self]) none + · exact setReplySc_rscA (by rw [rscA_ne _ e]; exact hk1) none + +theorem takeWhile_headRA {l : List ObjRef} {R : ObjRef} (hh : l.head? = some R) : + l.takeWhile (fun r => decide (r ≠ R)) = [] := by + cases l with + | nil => cases hh + | cons a t => cases hh; simp + +theorem takeWhile_headA {l : List ObjRef} {R h : ObjRef} (hh : l.head? = some h) (hne : h ≠ R) : + (l.takeWhile fun r => decide (r ≠ R)).head? = some h := by + cases l with + | nil => cases hh + | cons a t => cases hh; simp [List.takeWhile_cons, hne] + +/-- **l4v `reply_unlink_sc N R` of a reply below the head** (x86 `reply_remove_refinesU`, the cut case). -/ +theorem replyUnlinkSc_cutA {σ : State Unit} {N R : ObjRef} {xs : SchedContext} {ns : Nat} {xr : Reply} + (hks : σ.kheap N = some (.schedContext xs ns)) (hkr : σ.kheap R = some (.reply xr)) + (hrs : xr.replySc = none) {h : ObjRef} (hhd : xs.scReplies.head? = some h) (hne : h ≠ R) : + replyUnlinkSc (Z := Unit) N R σ = + ({((), repStA σ N xs ns (xs.scReplies.takeWhile fun r => decide (r ≠ R)))}, False) := by + have hgr : getReply (Z := Unit) R σ = ({(xr, σ)}, False) := getSimpleKo_replyA hkr + have hass : Fl4Spec.NonDet.assert (σ := State Unit) (xr.replySc = none) σ = ({((), σ)}, False) := by + simp [Fl4Spec.NonDet.assert, hrs] + unfold replyUnlinkSc + rw [nd_bind_oneSc (scReplies_getA hks)] + try beta_reduce + rw [nd_bind_oneSc hgr] + try beta_reduce + rw [hhd, show the (some h) = h from rfl, if_neg hne, nd_bind_oneSc hass] + exact updateSchedContext_eqA hks _ + +/-- **`reply_remove_tcb`'s SC update and `reply_sc := None`** (x86 `replyRemoveTcbSc`, its last two steps). -/ +theorem cutRsc_eqA {σ : State Unit} {N R : ObjRef} {xs : SchedContext} {ns : Nat} {xr : Reply} + (hks : σ.kheap N = some (.schedContext xs ns)) (hkr : σ.kheap R = some (.reply xr)) : + (updateSchedContext (Z := Unit) N (scRepliesUpdate (List.takeWhile fun r => decide (r ≠ R))) >>= fun _ => + setReplyObjRef (Z := Unit) replyScUpdate R none) σ = + ({((), rscA (repStA σ N xs ns (xs.scReplies.takeWhile fun r => decide (r ≠ R))) R none)}, False) := by + have hNR : R ≠ N := fun e => by rw [e, hks] at hkr; cases hkr + rw [nd_bind_oneSc (updateSchedContext_eqA hks _)] + exact setReplySc_rscA (σ := repStA σ N xs ns (xs.scReplies.takeWhile fun r => decide (r ≠ R))) + (by rw [repStA_ne hNR]; exact hkr) none + +end Spec + +/-! ## FL4 against the spec -/ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- `fixHdA` keeps `SchedSRA` (FL4 unchanged). -/ +theorem SchedSRA.fixHdA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} + {σ : Fl4SpecA64.State Unit} (H : SchedSRA st Z m κ s σ) (N : Fl4SpecA64.ObjRef) (l : List Fl4SpecA64.ObjRef) : + SchedSRA st Z m κ s (fixHdA σ N l) := by + cases l with + | nil => exact H + | cons h t => exact H.rscA h (some N) + +/-- **The SC-word step of `reply_pop`** (module docstring; x86 `reply_pop_refines`). -/ +theorem reply_pop_scA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} + {σ : Fl4SpecA64.State Unit} (H : SchedSRA st Z m κ s σ) {sc v : U64} (hc : sc ∈ Z.C) + {R : Fl4SpecA64.ObjRef} {xr : Fl4SpecA64.Reply} (hkr : σ.kheap R = some (.reply xr)) + (hrs : xr.replySc = some (scNameA m sc)) + (hhd : ∀ x n, σ.kheap (scNameA m sc) = some (.schedContext x n) → x.scReplies.head? = some R) + (hl : ∀ x n, σ.kheap (scNameA m sc) = some (.schedContext x n) → x.scReplies.tail.head? = optRefS v) + (htl : ∀ x n h, σ.kheap (scNameA m sc) = some (.schedContext x n) → x.scReplies.tail.head? = some h → + ∃ xh, σ.kheap h = some (.reply xh)) : + obj.Sc.set_reply st sc s v ⦃ s' => ∃ x n, σ.kheap (scNameA m sc) = some (.schedContext x n) ∧ + Fl4SpecA64.replyUnlinkSc (Z := Unit) (scNameA m sc) R σ = ({((), popStA σ (scNameA m sc) x n R)}, False) ∧ + SchedSRA st Z m κ s' (popStA σ (scNameA m sc) x n R) ∧ + WritesIn st s s' (MWrA (st := st) Z m σ s) ∧ SSzA σ (popStA σ (scNameA m sc) x n R) ⦄ := by + obtain ⟨x, n, hk, -⟩ := H.scr.sc sc hc + refine WP.spec_mono (obj.Sc.set_reply.spec s sc v) ?_ + rintro s' rfl + refine ⟨x, n, hk, replyUnlinkSc_popA hk hkr hrs (hhd x n hk) (fun h e => htl x n h hk e), ?_⟩ + rw [popStA_def] + obtain ⟨H2, F2⟩ := H.setReplyA hc hk v x.scReplies.tail (hl x n hk) + exact ⟨(H2.fixHdA _ _).rscA R none, F2.wr, + (F2.sz.trans fun p y k h => ⟨y, fixHdA_sc.1 h⟩).trans fun p y k h => ⟨y, rscA_sc.1 h⟩⟩ + +/-- **The SC-word step of `reply_remove_tcb` at the head** (module docstring; x86 `reply_remove_tcb_sc_refines`, +`CallRel.cut0`). -/ +theorem reply_remove_tcb_scA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} + {σ : Fl4SpecA64.State Unit} (H : SchedSRA st Z m κ s σ) {sc : U64} (hc : sc ∈ Z.C) + {R : Fl4SpecA64.ObjRef} {xr : Fl4SpecA64.Reply} (hkr : σ.kheap R = some (.reply xr)) + (hhd : ∀ x n, σ.kheap (scNameA m sc) = some (.schedContext x n) → x.scReplies.head? = some R) : + obj.Sc.set_reply st sc s 0#u64 ⦃ s' => ∃ x n, σ.kheap (scNameA m sc) = some (.schedContext x n) ∧ + (Fl4SpecA64.updateSchedContext (Z := Unit) (scNameA m sc) + (Fl4SpecA64.scRepliesUpdate (List.takeWhile fun r => decide (r ≠ R))) >>= fun _ => + Fl4SpecA64.setReplyObjRef (Z := Unit) Fl4SpecA64.replyScUpdate R none) σ = + ({((), rscA (repStA σ (scNameA m sc) x n []) R none)}, False) ∧ + SchedSRA st Z m κ s' (rscA (repStA σ (scNameA m sc) x n []) R none) ∧ + WritesIn st s s' (MWrA (st := st) Z m σ s) ∧ SSzA σ (rscA (repStA σ (scNameA m sc) x n []) R none) ⦄ := by + obtain ⟨x, n, hk, -⟩ := H.scr.sc sc hc + refine WP.spec_mono (obj.Sc.set_reply.spec s sc 0#u64) ?_ + rintro s' rfl + have hE := cutRsc_eqA hk hkr + rw [takeWhile_headRA (hhd x n hk)] at hE + obtain ⟨H2, F2⟩ := H.setReplyA hc hk 0#u64 [] (by rw [optRefS_zero]; rfl) + exact ⟨x, n, hk, hE, H2.rscA R none, F2.wr, F2.sz.trans fun p y k h => ⟨y, rscA_sc.1 h⟩⟩ + +/-- A spec reply list with the same head relates to the same FL4 SC. -/ +theorem ScRelWA.same_reply {ii : Prop} {s : S} {sc : U64} {x : Fl4SpecA64.SchedContext} {n : Nat} + (h : ScRelWA st ii s sc x n) (l : List Fl4SpecA64.ObjRef) (hl : l.head? = x.scReplies.head?) : + ScRelWA st ii s sc { x with scReplies := l } n := + ⟨h.pl, h.period, h.consumed, h.tcb, hl.trans h.reply, h.ntfn, h.badge, h.yieldFrom, h.sporadic, h.spor, h.idle, + h.max, fun h0 => ⟨(h.act h0).wf, (h.act h0).period, (h.act h0).consumed, (h.act h0).tcb, + hl.trans (h.act h0).reply, (h.act h0).ntfn, (h.act h0).badge, (h.act h0).yieldFrom, (h.act h0).sporadic, + (h.act h0).max, (h.act h0).refills⟩⟩ + +/-- **A spec reply list with the same head keeps `SchedSRA`, FL4 unchanged** (x86 `CallRel.cut`, its SC side). -/ +theorem SchedSRA.cutA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} + {σ : Fl4SpecA64.State Unit} (H : SchedSRA st Z m κ s σ) {sc : U64} (hc : sc ∈ Z.C) + {x : Fl4SpecA64.SchedContext} {n : Nat} (hk : σ.kheap (scNameA m sc) = some (.schedContext x n)) + (l : List Fl4SpecA64.ObjRef) (hl : l.head? = x.scReplies.head?) : + SchedSRA st Z m κ s (repStA σ (scNameA m sc) x n l) ∧ + MFrA (st := st) Z m s s σ (repStA σ (scNameA m sc) x n l) := by + obtain ⟨x0, n0, hk0, hx⟩ := H.scr.sc sc hc + rw [hk] at hk0; cases hk0 + refine ⟨H.scObjA hc hk (WritesIn.refl s _) (hx.same_reply l hl) rfl rfl rfl rfl rfl, + WritesIn.refl s _, SKeepA.of_obj fun q o hq ho => ?_, SSzA.of_upd hk rfl⟩ + have hne : q ≠ scNameA m sc := fun e => by rw [e, hk] at hq; cases hq; exact ho x n rfl + simp [repStA, updKSc, hne, hq] + +/-- **`reply_remove` of a reply below its SC's head** (`reply_unlink_sc`, cut case): FL4 leaves the SC. -/ +theorem reply_remove_cutA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} + {σ : Fl4SpecA64.State Unit} (H : SchedSRA st Z m κ s σ) {sc : U64} (hc : sc ∈ Z.C) + {R : Fl4SpecA64.ObjRef} {xr : Fl4SpecA64.Reply} (hkr : σ.kheap R = some (.reply xr)) (hrs : xr.replySc = none) + {x : Fl4SpecA64.SchedContext} {n : Nat} (hk : σ.kheap (scNameA m sc) = some (.schedContext x n)) + {h : Fl4SpecA64.ObjRef} (hhd : x.scReplies.head? = some h) (hne : h ≠ R) : + Fl4SpecA64.replyUnlinkSc (Z := Unit) (scNameA m sc) R σ = + ({((), repStA σ (scNameA m sc) x n (x.scReplies.takeWhile fun r => decide (r ≠ R)))}, False) ∧ + SchedSRA st Z m κ s (repStA σ (scNameA m sc) x n (x.scReplies.takeWhile fun r => decide (r ≠ R))) ∧ + MFrA (st := st) Z m s s σ (repStA σ (scNameA m sc) x n (x.scReplies.takeWhile fun r => decide (r ≠ R))) := + ⟨replyUnlinkSc_cutA hk hkr hrs hhd hne, + H.cutA hc hk _ ((takeWhile_headA hhd hne).trans hhd.symm)⟩ + +/-- **`reply_remove_tcb` of a reply below its SC's head**: FL4 leaves the SC; the spec cuts the list and clears the +reply's `reply_sc`. -/ +theorem reply_remove_tcb_cutA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} + {σ : Fl4SpecA64.State Unit} (H : SchedSRA st Z m κ s σ) {sc : U64} (hc : sc ∈ Z.C) + {R : Fl4SpecA64.ObjRef} {xr : Fl4SpecA64.Reply} (hkr : σ.kheap R = some (.reply xr)) + {x : Fl4SpecA64.SchedContext} {n : Nat} (hk : σ.kheap (scNameA m sc) = some (.schedContext x n)) + {h : Fl4SpecA64.ObjRef} (hhd : x.scReplies.head? = some h) (hne : h ≠ R) : + (Fl4SpecA64.updateSchedContext (Z := Unit) (scNameA m sc) + (Fl4SpecA64.scRepliesUpdate (List.takeWhile fun r => decide (r ≠ R))) >>= fun _ => + Fl4SpecA64.setReplyObjRef (Z := Unit) Fl4SpecA64.replyScUpdate R none) σ = + ({((), rscA (repStA σ (scNameA m sc) x n (x.scReplies.takeWhile fun r => decide (r ≠ R))) R none)}, False) ∧ + SchedSRA st Z m κ s (rscA (repStA σ (scNameA m sc) x n (x.scReplies.takeWhile fun r => decide (r ≠ R))) R none) ∧ + SSzA σ (rscA (repStA σ (scNameA m sc) x n (x.scReplies.takeWhile fun r => decide (r ≠ R))) R none) := by + obtain ⟨H2, F2⟩ := H.cutA hc hk _ ((takeWhile_headA hhd hne).trans hhd.symm) + exact ⟨cutRsc_eqA hk hkr, H2.rscA R none, F2.sz.trans fun p y k h => ⟨y, rscA_sc.1 h⟩⟩ + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScPopDonA.lean b/proofs/Fl4ProofsA64/P3/ScPopDonA.lean new file mode 100644 index 000000000..e23632e1a --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScPopDonA.lean @@ -0,0 +1,145 @@ +/- +P3 (aarch64, A64-P3-MCS gen 13): **`reply_pop`'s donation back to the caller** on `SchedSRA` / `SchedSRB` (ruling +03:02Z gen-13 items 1–2; x86 `P3/ScCallPopRef`'s `donate_unbound_refines` and `reply_pop_refines` (its donation +branch), ported). + +FL4's `reply_pop` (`crates/fl4-core/src/ipc.rs:197–199`) donates the popped SC to the caller `t` when `t` has none: +`if t.sc(s) == 0 { schedcontext::donate(s, a, Sc(next), t) }`; l4v `reply_remove` does +`when (the replies.head? = r ∧ callerSc = None) (sched_context_donate sc caller)`. The SC is either bound to the +replier `f` (the usual `reply_pop` of `do_reply_transfer`) or to no thread (the replier lost it). + +* **`sc_donate_unbA`** (x86 `donate_unbound_refines`): FL4 `schedcontext::donate sc t` of an SC bound to no TCB, + local case, against l4v `sched_context_donate`: single outcome, `SchedSRB … (specPtr t)`, frame `MFrA` + (`donate_localA` + `SchedSRA.donateB` + `donate_specA` + `SchedSRN.don_mfr`). +* **`reply_pop_donFromA`** / **`reply_pop_donUnbA`**: FL4's `if t.sc == 0 { donate }` (the extracted `reply_pop`'s + branch after `obj.Tcb.sc`) against l4v's `when (the l.head? = R ∧ tcb_sched_context = None) (sched_context_donate)`, + for an SC bound to `f` (`sc_donate_fromA`) and for an unbound SC; the branch without a donation keeps `SchedSRA` + (`SchedSRA.toB`). `RelIfBoundSc` at `specPtr t` after the donation (for `SchedSRB.toA`) is the join's obligation + (ruling 03:02Z item 2; x86 `hrel`). +* **`stall_keepA`** (item 2): FL4's `remote_tcb_stall f` stalls nothing (A64 P1 `stall_none`) at any state reached by + writes off `f`'s `AFFINITY` and `VCPU` words from one where `f` is on the current core with no bound VCPU; so + `sc_donate_fromA`'s `hstall` at `reply_push`'s donation follows from its start (all of `reply_push`'s lines 170–179 + write elsewhere). + +No named premise. Hypotheses: those of `sc_donate_fromA` (bound case) / `SchedSRA.donateB` (unbound case: `htw0`, +`hni`, `hmig` the local case), `hkt` (the caller's spec TCB), `hhd` (the stack's head is the popped reply). +-/ +import Fl4ProofsA64.P3.ScPopA +import Fl4ProofsA64.P1.LocalTimerSet +import Fl4ProofsA64.P1.FastDecline +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +/-- **`sched_context_donate` of an SC bound to no TCB** (module docstring; x86 `donate_unbound_refines`). -/ +theorem sc_donate_unbA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} + {σ : Fl4SpecA64.State Unit} (H : SchedSRA st Z m κ s σ) (a : A) {sc t : U64} (hsc : sc ∈ Z.C) (ht : t ∈ Z.T) + (hsc0 : sc ≠ 0#u64) (htw0 : L.mem s (scTcbA sc) = 0#u64) (hts0 : L.mem s (scA t) = 0#u64) + (hni : NotIdleScA m (scNameA m sc)) (hmig : L.mem s (affA t) = L.mem s (scCoreA sc)) : + schedcontext.donate st ar s a sc t ⦃ r => r.2 = a ∧ ∃ σ', + Fl4SpecA64.schedContextDonate (Z := Unit) (scNameA m sc) (specPtr t) σ = ({((), σ')}, False) ∧ + SchedSRB st Z m κ r.1 σ' (specPtr t) ∧ MFrA (st := st) Z m s r.1 σ σ' ⦄ := by + obtain ⟨xs, ns, hks, hxs⟩ := H.scr.sc sc hsc + obtain ⟨xt, hkt, -⟩ := H.sch.tcb t ht + have hx0 : xs.scTcb = none := by rw [hxs.tcb, ← scTcbA_eqSc, htw0, optRefS_zero] + have hNT : scNameA m sc ≠ specPtr t := fun e => by rw [e, hkt] at hks; cases hks + obtain ⟨hA1, hA2, -⟩ := H.donAddr hsc ht + have H2 := H.donateB hsc ht hsc0 htw0 hts0 hni hks hkt + have hwD : WritesIn st s (L.wr (L.wr s (scTcbA sc) t) (scA t) sc) (fun b => b = scTcbA sc ∨ b = scA t) := + (writesIn_wr hA1).trans (writesIn_wr hA2) + obtain ⟨-, -, tK, -⟩ := H.donW hsc ht hwD + have hmig2 : L.mem (L.wr (L.wr s (scTcbA sc) t) (scA t) sc) (affA t) = + L.mem (L.wr (L.wr s (scTcbA sc) t) (scA t) sc) (scCoreA sc) := by + rw [show affA t = wa t 3520#u64 from rfl, tK t ht 3520#u64 (by simp) (by simp) (fun _ => by simp), + H2.scCore sc hsc, ← H.scCore sc hsc] + exact hmig + refine WP.spec_mono (donate_localA s a sc t htw0 hmig2) ?_ + rintro r rfl + exact ⟨rfl, _, donate_specA hks hkt hx0, H2, + H.toSchedSRN.don_mfr hsc ht (hwD.mono fun b hb => hb.elim Or.inl fun e => Or.inr (Or.inl e)) + (SKeepA.two (fun p => donStA_kheap hNT p) hks hkt)⟩ + +/-- **`reply_pop`'s donation branch, the SC bound to the replier `f`** (module docstring). -/ +theorem reply_pop_donFromA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} + {σ : Fl4SpecA64.State Unit} (H : SchedSRA st Z m κ s σ) (hst : StEqA Z.T (L.mem s) σ) (a : A) + {sc f t : U64} (hsc : sc ∈ Z.C) (hsc0 : sc ≠ 0#u64) (hf : f ∈ Z.T) (hfs : L.mem s (scA f) = sc) + (ht : t ∈ Z.T) (hfon : L.mem s (affA f) = L.cpu s) + (hnqs : ∀ d p, specPtr f ∉ σ.readyQueues d p) (hnr : specPtr f ∉ σ.releaseQueue) + (hstall : smp.remote_tcb_stall st ar s a f = ok (s, a)) + (hmig : L.mem s (affA t) = L.mem s (scCoreA sc)) (hni : NotIdleScA m (scNameA m sc)) + {xt : Fl4SpecA64.Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + {l : List Fl4SpecA64.ObjRef} {R : Fl4SpecA64.ObjRef} (hhd : l.head? = some R) : + (if L.mem s (scA t) = 0#u64 then schedcontext.donate st ar s a sc t else ok (s, a)) ⦃ r => r.2 = a ∧ + ∃ σ' Q', Fl4Spec.NonDet.when (Fl4SpecA64.the l.head? = R ∧ xt.tcbSchedContext = none) + (Fl4SpecA64.schedContextDonate (Z := Unit) (scNameA m sc) (specPtr t)) σ = ({((), σ')}, False) ∧ + SchedSRB st { Z with Q := Q' } m κ r.1 σ' (specPtr t) ∧ MFrA (st := st) Z m s r.1 σ σ' ⦄ := by + obtain ⟨xt0, hkt0, hsct⟩ := H.sch.tcb t ht + rw [hkt] at hkt0; cases hkt0 + have hthe : Fl4SpecA64.the l.head? = R := by rw [hhd]; rfl + by_cases h0 : L.mem s (scA t) = 0#u64 + · rw [if_pos h0] + have hxn : xt.tcbSchedContext = none := by rw [hsct, h0]; simp [optScA] + have hft : f ≠ t := fun e => hsc0 (by rw [← hfs, e, h0]) + refine WP.spec_mono (sc_donate_fromA H hst a hsc hsc0 hf hfs ht h0 hft hfon hnqs hnr hstall hmig hni) ?_ + rintro r ⟨h1, σ', Q', hD, H', F'⟩ + refine ⟨h1, σ', Q', ?_, H', F'⟩ + unfold Fl4Spec.NonDet.when; rw [if_pos ⟨hthe, hxn⟩]; exact hD + · rw [if_neg h0] + have hxn : xt.tcbSchedContext ≠ none := by rw [hsct]; simp [optScA, h0] + refine (WP.spec_ok _).2 ⟨rfl, σ, Z.Q, ?_, H.toB _, MFrA.refl _ _ _ _⟩ + unfold Fl4Spec.NonDet.when; rw [if_neg (fun h => hxn h.2)]; rfl + +/-- **`reply_pop`'s donation branch, the SC bound to no thread** (module docstring). -/ +theorem reply_pop_donUnbA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} + {σ : Fl4SpecA64.State Unit} (H : SchedSRA st Z m κ s σ) (a : A) {sc t : U64} (hsc : sc ∈ Z.C) (ht : t ∈ Z.T) + (hsc0 : sc ≠ 0#u64) (htw0 : L.mem s (scTcbA sc) = 0#u64) (hni : NotIdleScA m (scNameA m sc)) + (hmig : L.mem s (affA t) = L.mem s (scCoreA sc)) + {xt : Fl4SpecA64.Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + {l : List Fl4SpecA64.ObjRef} {R : Fl4SpecA64.ObjRef} (hhd : l.head? = some R) : + (if L.mem s (scA t) = 0#u64 then schedcontext.donate st ar s a sc t else ok (s, a)) ⦃ r => r.2 = a ∧ + ∃ σ', Fl4Spec.NonDet.when (Fl4SpecA64.the l.head? = R ∧ xt.tcbSchedContext = none) + (Fl4SpecA64.schedContextDonate (Z := Unit) (scNameA m sc) (specPtr t)) σ = ({((), σ')}, False) ∧ + SchedSRB st Z m κ r.1 σ' (specPtr t) ∧ MFrA (st := st) Z m s r.1 σ σ' ⦄ := by + obtain ⟨xt0, hkt0, hsct⟩ := H.sch.tcb t ht + rw [hkt] at hkt0; cases hkt0 + have hthe : Fl4SpecA64.the l.head? = R := by rw [hhd]; rfl + by_cases h0 : L.mem s (scA t) = 0#u64 + · rw [if_pos h0] + have hxn : xt.tcbSchedContext = none := by rw [hsct, h0]; simp [optScA] + refine WP.spec_mono (sc_donate_unbA H a hsc ht hsc0 htw0 h0 hni hmig) ?_ + rintro r ⟨h1, σ', hD, H', F'⟩ + refine ⟨h1, σ', ?_, H', F'⟩ + unfold Fl4Spec.NonDet.when; rw [if_pos ⟨hthe, hxn⟩]; exact hD + · rw [if_neg h0] + have hxn : xt.tcbSchedContext ≠ none := by rw [hsct]; simp [optScA, h0] + refine (WP.spec_ok _).2 ⟨rfl, σ, ?_, H.toB _, MFrA.refl _ _ _ _⟩ + unfold Fl4Spec.NonDet.when; rw [if_neg (fun h => hxn h.2)]; rfl + +/-- **`remote_tcb_stall f` stalls nothing after writes off `f`'s `AFFINITY` and `VCPU` words** (module docstring; +A64 P1 `stall_none` at the later state). -/ +theorem stall_keepA (a : A) {s s' : S} {B : U64 → Prop} {f : U64} (hT : TcbAddr f) + (haff : L.mem s (affA f) = L.cpu s) (hv : L.mem s (wa f 3664#u64) = 0#u64) + (hw : WritesIn st s s' B) (h1 : ¬ B (affA f)) (h2 : ¬ B (wa f 3664#u64)) : + smp.remote_tcb_stall st ar s' a f = ok (s', a) := by + have ha : Aligned (affA f) := field_aligned hT (by simp) (by simp) + have hb : Aligned (wa f 3664#u64) := field_aligned hT (by simp) (by simp) + refine stall_none ar a (c := L.cpu s') ?_ (L.cur_cpu_eq s') ?_ + · rw [eq_of_spec_fd (obj.Tcb.affinity.spec (L := L) s' f), hw.mem_eq ha h1, haff, hw.2.1] + · unfold vcpu.bound + rw [eq_of_spec_fd Fl4ProofsA64.P0.Consts.layout.tcb.VCPU.spec] + simp only [bind_ok, lift, L.read_eq] + rw [hw.mem_eq hb h2, hv] + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScPostA.lean b/proofs/Fl4ProofsA64/P3/ScPostA.lean new file mode 100644 index 000000000..3a0c5606b --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScPostA.lean @@ -0,0 +1,331 @@ +/- +P3 (aarch64, A64-P3-MCS gen 6): **`postpone` on the donation block's relation** (ruling 23:44Z gen-6 item 1; x86 +`P3/ScResume`'s `postpone_refines`, ported). + +* **`RdExA σ p`** / **`SchedSRB st Z m κ s σ p`** (ruling 23:44Z item 1): `SchedSRN` (= `SchedSRA` without `rd`) with + l4v `released_ipc_queues` at every TCB but `p` — the relation between `sched_context_donate` and + `sched_context_resume`. **`SchedSRA.toB`**, **`SchedSRB.toA`** (`rd` back from `RelIfBoundSc` at `p`). +* `SchedSRN.scOut` (an SC word lies outside the TCBs and the nodes), **`SchedSRN.set_repr`** (x86 + `SchedRel.set_reprogram`: writing the current core's `REPROGRAM` word sets `reprogram_timer`), + **`SchedSRN.relEnq`** (x86 `SchedRel.rstep` after `release_enqueue_refines`: the relation with the new release list, + `reprogram_timer` read off the word). +* **`postpone_refinesA`** (x86 `postpone_refines`): FL4's `postpone sc` of a local thread `t` bound both ways to an + active SC `sc` of the current core, in no queue, refines l4v `postpone`: single outcome, the heap, action, clock and + ready queues kept, `t` in the release queue, and `SchedSRA` (with `rd`: `t` is released) with the new release lists. + +No named premise. Hypotheses: `hsc` / `ht` / `htw` / `hts` (the binding), `hon` / `hcpu` (the local case), +`hq0` / `hr0` / `hnqs` (in no queue), `hta` (active), `hrunt` (runnable). +-/ +import Fl4ProofsA64.P3.ScRelEnqA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section RdEx +open Fl4SpecA64 + +/-- l4v `released_ipc_queues` at every TCB but `p` (ruling 23:44Z item 1). -/ +def RdExA (σ : State Unit) (p : ObjRef) : Prop := + ∀ u x, u ≠ p → σ.kheap u = some (.tcb x) → inClassRSc x.tcbState x.tcbFault → RelIfBoundSc σ u + +end RdEx + +section +variable {S : Type} (st : machine.Store S) [L : LawfulStore st] + +/-- **The donation block's relation** (module docstring). -/ +structure SchedSRB (Z : XSetsA) (m : Fl4SpecA64.McState) (κ : U64 → U64) (s : S) (σ : Fl4SpecA64.State Unit) + (p : Fl4SpecA64.ObjRef) : Prop extends SchedSRN st Z m κ s σ where + rdEx : RdExA σ p + +end + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +theorem SchedSRA.toB {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} {σ : Fl4SpecA64.State Unit} + (H : SchedSRA st Z m κ s σ) (p : Fl4SpecA64.ObjRef) : SchedSRB st Z m κ s σ p := + ⟨H.toSchedSRN, fun u x _ hx hc => H.rd u x hx hc⟩ + +theorem SchedSRB.toA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} {σ : Fl4SpecA64.State Unit} + {p : Fl4SpecA64.ObjRef} (H : SchedSRB st Z m κ s σ p) (hp : RelIfBoundSc σ p) : SchedSRA st Z m κ s σ := + ⟨H.toSchedSRN, fun u x hx hc => by + by_cases e : u = p + · subst e; exact hp + · exact H.rdEx u x e hx hc⟩ + +/-- An SC word lies outside the TCBs and the nodes. -/ +theorem SchedSRN.scOut {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} {σ : Fl4SpecA64.State Unit} + (H : SchedSRN st Z m κ s σ) {a : U64} (ha : Aligned a) (hf : ScFootA Z.C m σ a) : + OutsideObjs st Z.T Z.Cs s a := by + obtain ⟨n, -, hn, hcs⟩ := H.csN + have hks := H.scp.ks_out hf + refine ⟨ha, fun t ht => ?_, fun c hc => ?_⟩ + · have := H.scp.th_out hf ((H.thT t).2 ht) + simp only [ObjKindA.size] at this + norm_num; omega + · have := (hcs c).1 hc + unfold LawfulStore.nodeBase; unfold ksSizeA at hks; omega + +/-- **Writing the current core's `REPROGRAM` word** (x86 `SchedRel.set_reprogram`): `reprogram_timer` set. -/ +theorem SchedSRN.set_repr {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} {σ : Fl4SpecA64.State Unit} + (H : SchedSRN st Z m κ s σ) : + SchedSRN st Z m κ (L.wr s (L.nodeAt s (L.cpu s) layout.node.REPROGRAM) 1#u64) { σ with reprogramTimer := true } := by + obtain ⟨hA, hout⟩ := H.reprA + have hr56 : layout.node.REPROGRAM.val = 56 := by unfold layout.node.REPROGRAM; rfl + have hw : WritesIn st s (L.wr s (L.nodeAt s (L.cpu s) layout.node.REPROGRAM) 1#u64) + (fun b => ScFootA Z.C m σ b ∨ b = L.nodeAt s (L.cpu s) layout.node.REPROGRAM) := + (writesIn_wr hA).mono fun b hb => Or.inr hb + obtain ⟨nK, gK, tK⟩ := H.keepW hw (by rw [hr56]; omega) (fun b hb => hb) + have hsc : ∀ a, Aligned a → ScFootA Z.C m σ a → + L.mem (L.wr s (L.nodeAt s (L.cpu s) layout.node.REPROGRAM) 1#u64) a = L.mem s a := + fun a ha hf => L.mem_wr_ne hA ha (fun e => hout a hf (by rw [e])) + have tw : ∀ t ∈ Z.T, ∀ K : U64, (K = 3520#u64 ∨ K = 3448#u64 ∨ K = 3432#u64 ∨ K = 3464#u64 ∨ K = 3592#u64 ∨ + K = 3480#u64 ∨ K = 3488#u64) → + L.mem (L.wr s (L.nodeAt s (L.cpu s) layout.node.REPROGRAM) 1#u64) (wa t K) = L.mem s (wa t K) := + fun t ht K hK => + tK t ht K (by rcases hK with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> simp) + (by rcases hK with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> simp) + have hcore : SchedCoreA st Z.T Z.Cs (L.wr s (L.nodeAt s (L.cpu s) layout.node.REPROGRAM) 1#u64) Z.Q Z.R := + H.core.agree hw.1 (fun u hu => ⟨tw u hu 3480#u64 (by simp), tw u hu 3488#u64 (by simp)⟩) + (fun c hc o h1 h2 h3 => nK c hc o h1 h2 (by rw [hr56]; omega)) (fun u hu => tw u hu 3592#u64 (by simp)) + (fun u hu => tw u hu 3520#u64 (by simp)) (fun u hu => tw u hu 3448#u64 (by simp)) + have scK : ∀ t ∈ Z.T, L.mem (L.wr s (L.nodeAt s (L.cpu s) layout.node.REPROGRAM) 1#u64) (scA t) = + L.mem s (scA t) := fun t ht => tw t ht 3464#u64 (by simp) + exact H.rebuildG (Q' := Z.Q) (R' := Z.R) hw.1 hw.2.1 + (fun c hc o h1 h2 h3 h4 => nK c hc o (by omega) h2 (by rw [hr56]; exact h4)) + (fun K hK => gK K (by rcases hK with rfl | rfl <;> simp) (by rcases hK with rfl | rfl <;> simp)) + (fun t ht K hK => tw t ht K (by rcases hK with h | h | h <;> simp [h])) + (H.scr.frame hsc (fun _ _ => rfl)) (ScPlacedA.of_kheap H.scp (fun _ _ => rfl)) + (fun t ht x hx => ⟨x, hx, rfl, rfl⟩) + (fun t ht => by obtain ⟨x, h1, h3⟩ := H.sch.tcb t ht; exact ⟨x, h1, by rw [scK t ht]; exact h3⟩) + (fun t ht h => by rw [scK t ht] at h ⊢; exact H.rest.scMem t ht h) + ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ + (by show true = _; rw [L.mem_wr_same hA]; simp) + (by + show σ.schedulerAction = _ + rw [nK _ H.sch.cpuCs layout.node.SCHED_ACTION (by simp [layout.node.SCHED_ACTION]) + (by simp [layout.node.SCHED_ACTION]) (by rw [hr56]; simp [layout.node.SCHED_ACTION])] + exact H.sch.act) + H.actOn (H.inv.of_kheap rfl rfl) hcore H.rest.rq H.rest.rqDom H.rest.rel H.run + (fun c hc => hsc _ (H.scCoreFoot hc).1 (H.scCoreFoot hc).2) + +/-- **`ScInvA` with `t` inserted into the release queue by ready time** (x86 `RelOK` after `release_enqueue`). -/ +theorem SchedSRN.invRel {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} {σ σ' : Fl4SpecA64.State Unit} {t : U64} + (H : SchedSRN st Z m κ s σ) (hk : σ'.kheap = σ.kheap) + (hr : σ'.releaseQueue = (relIns (L.rtime s) (L.rtime s t) t (Z.R (L.cpu s))).map specPtr) + (ht : t ∈ Z.T) (h0 : L.mem s (scA t) ≠ 0#u64) (hta : (L.rmax s (L.mem s (scA t))).val ≠ 0) : + ScInvA m σ' := by + have hrtv : ∀ v ∈ relIns (L.rtime s) (L.rtime s t) t (Z.R (L.cpu s)), + specRtA σ' (specPtr v) = (L.rtime s v).bv := by + intro v hv + unfold specRtA + rcases mem_relIns.1 hv with e | hv' + · rw [e, H.readyTimeK hk ht h0 hta]; rfl + · rw [H.readyTimeK hk (H.relAct v hv').1 (H.relAct v hv').2.1 (H.relAct v hv').2.2]; rfl + refine ⟨fun p y n hy => H.inv.refills p y n (by rw [← hk]; exact hy), + fun p y n hy => H.inv.rr p y n (by rw [← hk]; exact hy), + fun q x hx => H.inv.ntfnIdle q x (by rw [← hk]; exact hx), + fun q x hx => H.inv.ntfnIdleOf q x (by rw [← hk]; exact hx), fun u hu => ?_, ?_, + fun u x p hx hp => by rw [hk] at hx ⊢; exact H.inv.back u x p hx hp, + fun u x p hx hp => H.inv.tcbScNotIdle u x p (by rw [← hk]; exact hx) hp⟩ + · rw [hr] at hu + obtain ⟨v, hv, rfl⟩ := List.mem_map.1 hu + rw [hk] + rcases mem_relIns.1 hv with e | hv' + · rw [e] + obtain ⟨x, hkx, hsc⟩ := H.sch.tcb t ht + obtain ⟨y, k, hky, hy⟩ := H.scr.sc _ (H.rest.scMem t ht h0) + exact ⟨x, _, y, k, hkx, by rw [hsc]; unfold optScA; rw [if_neg h0]; rfl, hky, hy.active_of_rmax hta⟩ + · exact H.inv.relSc _ (by rw [H.rest.rel]; exact List.mem_map_of_mem hv') + · have hs := relIns_sorted (L.rtime s) (L.rtime s t) t _ H.relSorted rfl + unfold TimeSorted at hs + rw [hr, List.pairwise_map] + refine List.Pairwise.imp_of_mem (fun {a b} ha hb hab => ?_) hs + rw [hrtv a ha, hrtv b hb] + exact hab + +/-- **The relation after `release_enqueue`** (x86 `SchedRel.rstep` in `release_enqueue_refines`): the new release +list of the current core, the spec's release queue its image, `reprogram_timer` read off FL4's word. -/ +theorem SchedSRN.relEnq {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s s2 : S} {σ : Fl4SpecA64.State Unit} + (H : SchedSRN st Z m κ s σ) {t : U64} (ht : t ∈ Z.T) (h0 : L.mem s (scA t) ≠ 0#u64) + (hta : (L.rmax s (L.mem s (scA t))).val ≠ 0) + (hrunt : ∀ x, σ.kheap (specPtr t) = some (.tcb x) → Fl4SpecA64.runnable x.tcbState = true) (b : Bool) + (hcore2 : SchedCoreA st Z.T Z.Cs s2 Z.Q + (Function.update Z.R (L.cpu s) (relIns (L.rtime s) (L.rtime s t) t (Z.R (L.cpu s))))) + (hw2N : WritesIn st s s2 (L.QFootN s (t :: Z.R (L.cpu s)) (L.cpu s) RelOffs t)) : + SchedSRN st { Z with R := Function.update Z.R (L.cpu s) (relIns (L.rtime s) (L.rtime s t) t (Z.R (L.cpu s))) } + m κ s2 + { setRelA σ ((relIns (L.rtime s) (L.rtime s t) t (Z.R (L.cpu s))).map specPtr) b with + reprogramTimer := decide (L.mem s2 (L.nodeAt s (L.cpu s) layout.node.REPROGRAM) ≠ 0#u64) } := by + have hw2 := hw2N.mono fun _ h => h.toQFoot + have hT := H.core.tcbs + have hcur := H.sch.cpuCs + have hN := H.core.nodes _ hcur + have hW : ∀ w ∈ t :: Z.R (L.cpu s), w ∈ Z.T := fun w hw => by + rcases List.mem_cons.1 hw with rfl | hw + · exact ht + · exact (H.relAct w hw).1 + obtain ⟨-, -, out⟩ := ext_qfoot hT hW hN hN ht hcur hcur hw2 + obtain ⟨n, -, hn, hcs⟩ := H.csN + have hcl : ∀ c ∈ Z.Cs, c.val < 128 := fun c hc => by have := (hcs c).1 hc; omega + have hf := H.ksFit + have hkb := H.ksb_al + have nodeIn : ∀ c' ∈ Z.Cs, (L.ksb s).val + 223488 ≤ L.nodeBase s c' ∧ + L.nodeBase s c' + 4288 ≤ (L.ksb s).val + ksSizeA := fun c' hc' => by + have := hcl c' hc'; unfold LawfulStore.nodeBase ksSizeA; omega + have nK : ∀ c' ∈ Z.Cs, ∀ o : U64, o.val < 56 → o.val % 8 = 0 → + L.mem s2 (L.nodeAt s c' o) = L.mem s (L.nodeAt s c' o) := by + intro c' hc' o ho h8 + have := node_qfoot hT hW hN hN ht hw2 (H.core.nodes c' hc') (o := o) (by omega) h8 + (fun _ => by simp only [RelOffs]; omega) (fun _ => by omega) + rwa [nodeAt_of_ksb hw2.1] at this + have tK : ∀ y ∈ Z.T, ∀ K : U64, (K = 3520#u64 ∨ K = 3448#u64 ∨ K = 3432#u64 ∨ K = 3464#u64) → + L.mem s2 (wa y K) = L.mem s (wa y K) := by + intro y hy K hK + have hK' : K.val = 3520 ∨ K.val = 3448 ∨ K.val = 3432 ∨ K.val = 3464 := by + rcases hK with rfl | rfl | rfl | rfl <;> simp + exact field_qfoot hT hW hN hN ht hw2 hy (by omega) (by omega) (by omega) (fun _ => by omega) + have scK : ∀ y ∈ Z.T, L.mem s2 (scA y) = L.mem s (scA y) := fun y hy => tK y hy 3464#u64 (by simp) + refine H.rebuildG hw2.1 hw2.2.1 (fun c hc o h1 h2 _ _ => nK c hc o h1 h2) (fun K hK => ?_) + (fun y hy K hK => tK y hy K (by rcases hK with h | h | h <;> simp [h])) + (H.scr.frame (fun a ha hfa => out a (H.scOut ha hfa)) (fun _ _ => rfl)) + (ScPlacedA.of_kheap H.scp (fun _ _ => rfl)) (fun y hy x hx => ⟨x, hx, rfl, rfl⟩) + (fun y hy => by obtain ⟨x, h1, h3⟩ := H.sch.tcb y hy; exact ⟨x, h1, by rw [scK y hy]; exact h3⟩) + (fun y hy h => by rw [scK y hy] at h ⊢; exact H.rest.scMem y hy h) + ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ rfl ?_ H.actOn ?_ hcore2 H.rest.rq H.rest.rqDom + (by simp [setRelA]) ?_ (fun c hc => out _ (H.scOut (H.scCoreFoot hc).1 (H.scCoreFoot hc).2)) + · have hK' : K.val = 8 ∨ K.val = 16 := by rcases hK with rfl | rfl <;> simp + have hv : (wa (L.ksb s) K).val = (L.ksb s).val + K.val := by rw [wa_valm]; unfold ksSizeA at hf; omega + refine out _ ⟨by unfold Aligned; rw [hv]; omega, fun y hy => ?_, fun c' hc' => ?_⟩ + · have := H.tcb_ks hy; rw [hv]; unfold ksSizeA at this; omega + · have := nodeIn c' hc'; rw [hv]; omega + · show σ.schedulerAction = _ + rw [nK _ hcur layout.node.SCHED_ACTION (by simp [layout.node.SCHED_ACTION]) + (by simp [layout.node.SCHED_ACTION])] + exact H.sch.act + · exact H.invRel rfl rfl ht h0 hta + · refine ⟨H.run.1, fun u hu => ?_⟩ + obtain ⟨v, hv, rfl⟩ := List.mem_map.1 hu + rcases mem_relIns.1 hv with e | hv' + · rw [e] + obtain ⟨x, hk, -⟩ := H.sch.tcb t ht + exact ⟨x, hk, hrunt x hk⟩ + · exact H.run.2 _ (by rw [H.rest.rel]; exact List.mem_map_of_mem hv') + +/-- **`postpone` refines `postpone`** (x86 `postpone_refines`; module docstring). -/ +theorem postponeTime_refinesA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} {σ : Fl4SpecA64.State Unit} + {sc t : U64} (H : SchedSRB st Z m κ s σ (specPtr t)) (hsc : sc ∈ Z.C) (ht : t ∈ Z.T) + (hon : L.mem s (affA t) = L.cpu s) (htw : L.mem s (scTcbA sc) = t) (hts : L.mem s (scA t) = sc) + (hsc0 : sc ≠ 0#u64) (hcpu : L.mem s (scCoreA sc) = L.cpu s) + (hq0 : (L.mem s (inqA t) &&& sched.Q_QUEUED) = 0#u64) (hr0 : (L.mem s (inqA t) &&& sched.Q_RELEASE) = 0#u64) + (hta : (L.rmax s sc).val ≠ 0) (hnqs : ∀ d p, specPtr t ∉ σ.readyQueues d p) + (hrunt : ∀ x, σ.kheap (specPtr t) = some (.tcb x) → Fl4SpecA64.runnable x.tcbState = true) : + sched.postpone st s sc ⦃ s' => ∃ σ' R', Fl4SpecA64.postpone (Z := Unit) (scNameA m sc) σ = ({((), σ')}, False) ∧ + σ'.kheap = σ.kheap ∧ σ'.schedulerAction = σ.schedulerAction ∧ σ'.curTime = σ.curTime ∧ + σ'.readyQueues = σ.readyQueues ∧ specPtr t ∈ σ'.releaseQueue ∧ + (∀ u, u ∈ σ.releaseQueue → u ∈ σ'.releaseQueue) ∧ (∀ y ∈ Z.T, L.mem s' (stA y) = L.mem s (stA y)) ∧ + WritesIn st s s' (fun b => L.QFootN s (t :: Z.R (L.cpu s)) (L.cpu s) RelOffs t b ∨ b = L.nodeAt s (L.cpu s) layout.node.REPROGRAM) ∧ + SchedSRA st { Z with R := R' } m κ s' σ' ∧ σ'.machineState = σ.machineState ∧ σ'.consumedTime = σ.consumedTime ∧ + σ'.idleThread = σ.idleThread ⦄ := by + have ht0 : t ≠ 0#u64 := fun e => (H.core.tcbs t ht).ne0 (by rw [e]; rfl) + obtain ⟨xt, hkt, hd0, -, -⟩ := H.rest.prio t ht + obtain ⟨xs, ns, hks, hxs⟩ := H.scr.sc sc hsc + have h0 : L.mem s (scA t) ≠ 0#u64 := by rw [hts]; exact hsc0 + have hta' : (L.rmax s (L.mem s (scA t))).val ≠ 0 := by rw [hts]; exact hta + unfold sched.postpone + refine WP.spec_bind (obj.Sc.tcb.spec s sc) ?_ + rintro i rfl + rw [htw] + refine WP.spec_bind (sched.dequeue.noop s t hq0) ?_ + rintro s1 e1 + rw [e1] + refine WP.spec_bind (release_enqueue_refinesA H.toSchedSRN ht hon h0 hq0 hr0 hta') ?_ + rintro s2 ⟨b, he, hcore2, hw2, -⟩ + have hW : ∀ w ∈ t :: Z.R (L.cpu s), w ∈ Z.T := fun w hw => by + rcases List.mem_cons.1 hw with rfl | hw + · exact ht + · exact (H.relAct w hw).1 + have hN := H.core.nodes _ H.sch.cpuCs + obtain ⟨hstq, -, out⟩ := ext_qfoot H.core.tcbs hW hN hN ht H.sch.cpuCs H.sch.cpuCs (hw2.mono fun _ h => h.toQFoot) + have hcA := H.scCoreFoot hsc + have hcpu2 : L.mem s2 (scCoreA sc) = L.cpu s := by rw [out _ (H.scOut hcA.1 hcA.2)]; exact hcpu + refine WP.spec_bind (obj.Sc.cpu.spec s2 sc) ?_ + rintro cpu rfl + rw [hcpu2] + refine WP.spec_mono (sched.set_reprogram_on.spec s2 (L.cpu s)) ?_ + rintro s3 rfl + have N2 := H.toSchedSRN.relEnq ht h0 hta' hrunt b hcore2 hw2 + have hst3 : ∀ y ∈ Z.T, L.mem (L.wr s2 (L.nodeAt s2 (L.cpu s) layout.node.REPROGRAM) 1#u64) (stA y) = + L.mem s (stA y) := by + have hr56 : layout.node.REPROGRAM.val = 56 := by unfold layout.node.REPROGRAM; rfl + have hA2 := N2.reprA.1 + rw [hw2.2.1] at hA2 + obtain ⟨-, -, tK⟩ := N2.keepW (writesIn_wr hA2) (o0 := layout.node.REPROGRAM) (by rw [hr56]; omega) + (fun b hb => Or.inr (by rw [hb, hw2.2.1])) + intro y hy + rw [stA_eq, tK y hy 3328#u64 (by simp) (by simp), ← stA_eq] + exact (hstq y hy).1 + have hA3 : Aligned (L.nodeAt s2 (L.cpu s) layout.node.REPROGRAM) := by + have h := N2.reprA.1; rw [hw2.2.1] at h; exact h + have hw3 : WritesIn st s (L.wr s2 (L.nodeAt s2 (L.cpu s) layout.node.REPROGRAM) 1#u64) + (fun b => L.QFootN s (t :: Z.R (L.cpu s)) (L.cpu s) RelOffs t b ∨ b = L.nodeAt s (L.cpu s) layout.node.REPROGRAM) := + (hw2.trans (writesIn_wr hA3)).mono fun b hb => hb.imp id (fun e => by rw [e, nodeAt_of_ksb hw2.1]) + have N3 := N2.set_repr + rw [hw2.2.1] at N3 + set q := (relIns (L.rtime s) (L.rtime s t) t (Z.R (L.cpu s))).map specPtr with hq + have htq : specPtr t ∈ q := List.mem_map_of_mem (mem_relIns.2 (Or.inl rfl)) + have hsub : ∀ u, u ∈ σ.releaseQueue → u ∈ q := fun u hu => by + rw [H.rest.rel] at hu + obtain ⟨v, hv, rfl⟩ := List.mem_map.1 hu + exact List.mem_map_of_mem (mem_relIns.2 (Or.inr hv)) + refine ⟨{ setRelA σ q b with reprogramTimer := true }, + Function.update Z.R (L.cpu s) (relIns (L.rtime s) (L.rtime s t) t (Z.R (L.cpu s))), ?_, rfl, rfl, rfl, rfl, + htq, hsub, hst3, hw3, ?_, rfl, rfl, rfl⟩ + · have hdq : Fl4SpecA64.tcbSchedDequeue (specPtr t) (σ.readyQueues 0 xt.tcbPriority) = + σ.readyQueues 0 xt.tcbPriority := by + unfold Fl4SpecA64.tcbSchedDequeue + exact List.filter_eq_self.2 (fun a ha => by + simp only [ne_eq, decide_eq_true_eq]; rintro rfl; exact hnqs 0 _ ha) + have hg : Fl4SpecA64.getScObjRef (Z := Unit) Fl4SpecA64.SchedContext.scTcb (scNameA m sc) σ = + ({(xs.scTcb, σ)}, False) := by + unfold Fl4SpecA64.getScObjRef; rw [nd_bind_oneSc (getSchedContext_eqA hks)]; rfl + unfold Fl4SpecA64.postpone + rw [nd_bind_oneSc hg, hxs.tcb, ← scTcbA_eqSc, htw, optRefS_ne ht0, + nd_bind_oneSc (show Fl4Spec.NonDet.assertOpt (σ := Fl4SpecA64.State Unit) (some (specPtr t)) σ = + ({(specPtr t, σ)}, False) from rfl), + nd_bind_oneSc (tcbSchedAction_eqA hkt hd0 _), hdq, setRqA_self, nd_bind_oneSc he] + rfl + · refine SchedSRB.toA ⟨N3, fun u x hu hx hc => ?_⟩ (fun _ _ _ _ _ _ _ _ hn => absurd htq hn) + intro x' scp y k h1 h2 h3 h4 h5 + exact H.rdEx u x hu hx hc x' scp y k h1 h2 h3 h4 (fun hm => h5 (hsub u hm)) + +/-- Postpone with the scheduler and release-queue postconditions. -/ +theorem postpone_refinesA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} {σ : Fl4SpecA64.State Unit} + {sc t : U64} (H : SchedSRB st Z m κ s σ (specPtr t)) (hsc : sc ∈ Z.C) (ht : t ∈ Z.T) + (hon : L.mem s (affA t) = L.cpu s) (htw : L.mem s (scTcbA sc) = t) (hts : L.mem s (scA t) = sc) + (hsc0 : sc ≠ 0#u64) (hcpu : L.mem s (scCoreA sc) = L.cpu s) + (hq0 : (L.mem s (inqA t) &&& sched.Q_QUEUED) = 0#u64) (hr0 : (L.mem s (inqA t) &&& sched.Q_RELEASE) = 0#u64) + (hta : (L.rmax s sc).val ≠ 0) (hnqs : ∀ d p, specPtr t ∉ σ.readyQueues d p) + (hrunt : ∀ x, σ.kheap (specPtr t) = some (.tcb x) → Fl4SpecA64.runnable x.tcbState = true) : + sched.postpone st s sc ⦃ s' => ∃ σ' R', Fl4SpecA64.postpone (Z := Unit) (scNameA m sc) σ = ({((), σ')}, False) ∧ + σ'.kheap = σ.kheap ∧ σ'.schedulerAction = σ.schedulerAction ∧ σ'.curTime = σ.curTime ∧ + σ'.readyQueues = σ.readyQueues ∧ specPtr t ∈ σ'.releaseQueue ∧ + (∀ u, u ∈ σ.releaseQueue → u ∈ σ'.releaseQueue) ∧ (∀ y ∈ Z.T, L.mem s' (stA y) = L.mem s (stA y)) ∧ + WritesIn st s s' (fun b => L.QFootN s (t :: Z.R (L.cpu s)) (L.cpu s) RelOffs t b ∨ b = L.nodeAt s (L.cpu s) layout.node.REPROGRAM) ∧ + SchedSRA st { Z with R := R' } m κ s' σ' ⦄ := by + refine WP.spec_mono (postponeTime_refinesA H hsc ht hon htw hts hsc0 hcpu hq0 hr0 hta hnqs hrunt) ?_ + rintro s' ⟨σ', R', e, hk, ha, ht', hq, hr, hsub, hs, w, hS, _, _, _⟩ + exact ⟨σ', R', e, hk, ha, ht', hq, hr, hsub, hs, w, hS⟩ + + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScRecvA.lean b/proofs/Fl4ProofsA64/P3/ScRecvA.lean new file mode 100644 index 000000000..c71890960 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScRecvA.lean @@ -0,0 +1,399 @@ +/- +P3 (aarch64, A64-P3-MCS gen 9): **the SC tails of `receive_signal` and `complete_signal`** on `SchedSRA` (ruling +01:09Z gen-9 item 1; the IPC line's `SchedOpsA.recvTail` / `.completeTail`, x86 `P3/ScTails`' `recvTail_refines` / +`completeTail_refines` through `DonateHyp`). + +* **`rustRecvTailA` / `rustCompleteTailA`** and **`specRecvTailA` / `specCompleteTailA`**: the IPC line's + `rustRecvTail` / `rustCompleteTail` / `specRecvTail` / `specCompleteTail` (`P3/SchedOpsA` on `par/a64-p3-ipc`), the + same texts (`rfl` at the join), with the FL4 bodies after the donation named (`recvBodyA`, `completeBodyA`). +* **`recv_body_eqA`**: FL4's body after the donation is `notification::unblock_sporadic t` (the `cur_sc` test and + the sporadic test commute; for a null SC both are no-ops). **`complete_body_eqA`**: the complete body is the same + guarded by `t.SC = n.SC`. **`complete_spec_eqA`**: the spec's complete body is `if_sporadic_cur_sc_test_refill_ + unblock_check` of `t`'s SC guarded by `scOpt = ntfn_sc`. +* **`optScA_eq_of`**: the renamed SC words are injective off `idle_sc_ptr` (`ScInvA.ntfnIdle` excludes it for a + notification's word). +* **`SchedSRN.offW`**: a word off every TCB of `Z.T`, off the kernel-state block and off the SC footprint lies off + the donation's write set (gen 9's `maybe_donate_localA` `WritesIn`). +* **`recv_tail_localA`** / **`complete_tail_localA`** (`SchedOpsA.recvTail` / `.completeTail`, local case, every + donation case selected by the words as `wake_tail_localA`): single spec outcome, `SchedSRA` after with new release + lists. (Gen 10) corollaries of **`recv_tail_frameA`** / **`complete_tail_frameA`**, the same with the frame `MFrA` + (`P3/ScSWrA`). + +No named premise. Hypotheses: as `wake_tail_localA` (`hon` / `hd`, `DonLocalA` unfolded: **stop-first finding, gen 9** — IPC's +`recvTail` / `completeTail` contracts take neither; the receiver is the current thread at both call sites, so `hon` +is `SchedSRA.curOn`, and the donation clause is the one `wakeTail` takes since IPC gen 8); for `complete_tail_localA` +the placement of the notification's `SC` word (`hnA` / `hnS` / `hnT` / `hnK`: aligned, off the SC footprint, every +TCB of `Z.T` and the kernel-state block — IPC's `NPlaced` with `Z.th`), read across the donation. +-/ +import Fl4ProofsA64.P3.ScRetA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## The texts -/ + +section Defs +variable {S A : Type} (st : machine.Store S) (ar : machine.Arch A) + +/-- FL4's receive tail after the donation (the IPC line's `rustRecvTail` body). -/ +def recvBodyA (s3 : S) (a1 : A) (t : obj.Tcb) : Result (S × A) := do + let i ← obj.Tcb.sc st t s3 + let s4 ← sched.cur_sc st s3 + let b1 ← core.cmp.PartialEq.ne.trait_default obj.Sc.Insts.CoreCmpPartialEqSc i s4 + if b1 + then + let b2 ← sporadic.sc_sporadic st s3 i + if b2 + then + let s5 ← sporadic.unblock_check st s3 i + ok (s5, a1) + else ok (s3, a1) + else ok (s3, a1) + +/-- The IPC line's `rustRecvTail`. -/ +def rustRecvTailA (s : S) (a : A) (t : obj.Tcb) (n : U64) : Result (S × A) := do + let (s3, a1) ← notification.maybe_donate_sched_context st ar s a t n + recvBodyA st s3 a1 t + +/-- FL4's complete tail after the donation (the IPC line's `rustCompleteTail` body). -/ +def completeBodyA (s3 : S) (a1 : A) (t : obj.Tcb) (n : U64) : Result (S × A) := do + let i1 ← obj.Tcb.sc st t s3 + let b2 ← sporadic.sc_sporadic st s3 i1 + if b2 + then + let i2 ← obj.Ntfn.sc st n s3 + if i1 = i2 + then + let s4 ← sched.cur_sc st s3 + let b3 ← core.cmp.PartialEq.ne.trait_default obj.Sc.Insts.CoreCmpPartialEqSc i1 s4 + if b3 + then + let s5 ← sporadic.unblock_check st s3 i1 + ok (s5, a1) + else ok (s3, a1) + else ok (s3, a1) + else ok (s3, a1) + +/-- The IPC line's `rustCompleteTail`. -/ +def rustCompleteTailA (s : S) (a : A) (t : obj.Tcb) (n : U64) : Result (S × A) := do + let (s3, a1) ← notification.maybe_donate_sched_context st ar s a t n + completeBodyA st s3 a1 t n + +end Defs + +section Spec +open Fl4SpecA64 + +/-- The IPC line's `specRecvTail` (`receive_signal`'s active branch after the badge). -/ +def specRecvTailA (tptr ntfnptr : ObjRef) : SMonad Unit Unit := do + maybeDonateSc tptr ntfnptr + let scPtr ← getTcbObjRef Tcb.tcbSchedContext tptr + ifSporadicCurScTestRefillUnblockCheck scPtr + +/-- The spec's complete body after the donation. -/ +def completeSpecBodyA (scOpt : Option ObjRef) (ntfnptr : ObjRef) : SMonad Unit Unit := + Fl4Spec.NonDet.maybeM (fun scp => do + let sc ← getSchedContext scp + Fl4Spec.NonDet.when sc.scSporadic <| do + let ntfnScp ← getNtfnObjRef Notification.ntfnSc ntfnptr + let curScPtr ← Fl4Spec.NonDet.gets fun s : State Unit => s.curSc + Fl4Spec.NonDet.when (scOpt = ntfnScp ∧ scp ≠ curScPtr) <| refillUnblockCheck scp) + scOpt + +/-- The IPC line's `specCompleteTail`. -/ +def specCompleteTailA (tptr ntfnptr : ObjRef) : SMonad Unit Unit := do + maybeDonateSc tptr ntfnptr + let scOpt ← getTcbObjRef Tcb.tcbSchedContext tptr + completeSpecBodyA scOpt ntfnptr + +/-- **The spec's complete body** (module docstring). -/ +theorem complete_spec_eqA {σ : State Unit} {n : ObjRef} {z : Notification} + (hkn : σ.kheap n = some (.notification z)) (scOpt : Option ObjRef) + (hsc : ∀ p, scOpt = some p → ∃ y k, σ.kheap p = some (.schedContext y k)) : + completeSpecBodyA scOpt n σ = + if scOpt = z.ntfnSc then ifCondRefillUnblockCheck (Z := Unit) scOpt (some false) (some false) σ + else ({((), σ)}, False) := by + unfold completeSpecBodyA + cases scOpt with + | none => + unfold ifCondRefillUnblockCheck + simp only [Fl4Spec.NonDet.maybeM] + split_ifs <;> rfl + | some p => + obtain ⟨y, k, hy⟩ := hsc p rfl + rw [ifCond_eqA hy false] + simp only [Fl4Spec.NonDet.maybeM] + rw [nd_bind_oneSc (getSchedContext_eqA hy)] + by_cases e1 : y.scSporadic = true + · simp only [Fl4Spec.NonDet.when, e1, if_true] + rw [nd_bind_oneSc (getNtfnSc_eqSc hkn), nd_gets_bindSc] + by_cases e2 : some p = z.ntfnSc <;> by_cases e3 : p = σ.curSc <;> + simp [Fl4Spec.NonDet.pure, e1, e2, e3] + · simp [Fl4Spec.NonDet.when, Fl4Spec.NonDet.pure, e1] + +end Spec + +/-- **Renamed SC words are injective off `idle_sc_ptr`** (module docstring). -/ +theorem optScA_eq_of {m : Fl4SpecA64.McState} {w1 w2 : U64} (h : optScA m w1 = optScA m w2) + (hi : optScA m w2 ≠ some Fl4SpecA64.idleScPtr) : w1 = w2 := by + unfold optScA at h hi + by_cases e2 : w2 = 0#u64 + · rw [if_pos e2] at h + by_cases e1 : w1 = 0#u64 + · rw [e1, e2] + · rw [if_neg e1] at h; cases h + · rw [if_neg e2] at h hi + by_cases e1 : w1 = 0#u64 + · rw [if_pos e1] at h; cases h + · rw [if_neg e1] at h + exact scRenA_eq_of (Option.some.inj h) (by rw [Option.some.inj h]; exact fun e => hi (by rw [e])) + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +/-- **The receive body is the sporadic unblock** (module docstring). -/ +theorem recv_body_eqA (s3 : S) (a1 : A) (t : obj.Tcb) : + recvBodyA st s3 a1 t = (do let s5 ← notification.unblock_sporadic st s3 t; ok (s5, a1)) := by + unfold recvBodyA notification.unblock_sporadic + rw [eq_of_spec_fd (obj.Tcb.sc.spec s3 t), eq_of_spec_fd (sched.cur_sc.spec s3)] + simp only [bind_ok, sc_ne_eqA, sc_sporadic_eq] + split_ifs <;> simp_all + +/-- **The complete body is the guarded sporadic unblock** (module docstring). -/ +theorem complete_body_eqA (s3 : S) (a1 : A) (t n : U64) : + completeBodyA st s3 a1 t n = if L.mem s3 (scA t) = L.mem s3 (nScSc n) then + (do let s5 ← notification.unblock_sporadic st s3 t; ok (s5, a1)) else ok (s3, a1) := by + unfold completeBodyA notification.unblock_sporadic + rw [eq_of_spec_fd (obj.Tcb.sc.spec s3 t), eq_of_spec_fd (ntfn_sc_specSc s3 n), + eq_of_spec_fd (sched.cur_sc.spec s3)] + simp only [bind_ok, sc_ne_eqA, sc_sporadic_eq] + split_ifs <;> simp_all + +/-- **A word off the TCBs, the block and the SC footprint is off the donation's write set** (module docstring). -/ +theorem SchedSRN.offW {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} {σ : Fl4SpecA64.State Unit} + (H : SchedSRN st Z m κ s σ) {b sc t : U64} (hsc : sc ∈ Z.C) (ht : t ∈ Z.T) + (hnS : ¬ ScFootA Z.C m σ b) (hnT : ∀ u ∈ Z.T, b.val < u.val ∨ u.val + 4096 ≤ b.val) + (hnK : b.val < (L.ksb s).val ∨ (L.ksb s).val + ksSizeA ≤ b.val) : + ¬ (b = scTcbA sc ∨ b = scA t ∨ L.QFootN s (t :: Z.R (L.cpu s)) (L.cpu s) RelOffs t b ∨ + b = L.nodeAt s (L.cpu s) layout.node.REPROGRAM) := by + obtain ⟨nn, -, hn, hcs⟩ := H.csN + have hcp : (L.cpu s).val < 128 := by have := (hcs _).1 H.sch.cpuCs; omega + have hf := H.ksFit + have tw : ∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → b ≠ wa u K := by + intro u hu K hK e + have hT := H.core.tcbs u hu + have hfit := hT.fits + have hv : (wa u K).val = u.val + K.val := by rw [wa_valm]; omega + have := hnT u hu + rw [e, hv] at this; omega + have nw : ∀ o : U64, o.val < 4288 → b ≠ L.nodeAt s (L.cpu s) o := by + intro o ho e + have hv := nodeAt_valSc (s := s) hf hcp ho + unfold ksSizeA at hf hnK + rw [e, hv] at hnK; omega + have hW : ∀ w ∈ t :: Z.R (L.cpu s), w ∈ Z.T := fun w hw => by + rcases List.mem_cons.1 hw with rfl | hw + · exact ht + · exact (H.relAct w hw).1 + rintro (e | e | (⟨w, hw, e | e⟩ | ⟨o, -, ho, -, e⟩ | e) | e) + · obtain ⟨x, n, hk, hx⟩ := H.scr.sc sc hsc + have h24 := hx.hdr (K := 24#u64) (by simp) (by simp) + exact hnS (by rw [e, scTcbA_eqSc]; exact ⟨sc, hsc, x, n, hk, hx.in_obj h24.2⟩) + · exact tw t ht 3464#u64 (by simp) e + · exact tw w (hW w hw) 3480#u64 (by simp) e + · exact tw w (hW w hw) 3488#u64 (by simp) e + · exact nw o ho e + · exact tw t ht 3592#u64 (by simp) e + · exact nw _ (by simp [layout.node.REPROGRAM]) e + +/-- **The receive tail, local case** (module docstring; gen 10: with the frame `MFrA`). -/ +theorem recv_tail_frameA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} + {σ : Fl4SpecA64.State Unit} (H : SchedSRA st Z m κ s σ) (a : A) + (hmb : sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) {t n : U64} (ht : t ∈ Z.T) + (hon : L.mem s (affA t) = L.cpu s) (hN : NtfnScEqA m n (L.mem s) σ) (hst : StEqA Z.T (L.mem s) σ) + (hnqs : ∀ d p, specPtr t ∉ σ.readyQueues d p) (hnq : specPtr t ∉ σ.releaseQueue) + (hrunt : ∀ x, σ.kheap (specPtr t) = some (.tcb x) → Fl4SpecA64.runnable x.tcbState = true) + (hd : ∀ sc, L.mem s (nScSc n) = sc → sc ≠ 0#u64 → sc ∈ Z.C ∧ L.mem s (scCoreA sc) = L.cpu s) : + rustRecvTailA st ar s a t n ⦃ r => r.2 = a ∧ ∃ σ' R', + specRecvTailA (specPtr t) (specPtr n) σ = ({((), σ')}, False) ∧ + SchedSRA st { Z with R := R' } m κ r.1 σ' ∧ MFrA (st := st) Z m s r.1 σ σ' ⦄ := by + have tail : ∀ s3 a3 (σ3 : Fl4SpecA64.State Unit) R3, SchedSRA st { Z with R := R3 } m κ s3 σ3 → + MFrA (st := st) Z m s s3 σ σ3 → + (specPtr t ∈ σ3.releaseQueue → ∀ y k, σ3.kheap (scNameA m (L.mem s3 (scA t))) = some (.schedContext y k) → + Fl4SpecA64.refillReady σ3.curTime (Fl4SpecA64.refillHd y) = false) → + Fl4SpecA64.maybeDonateSc (Z := Unit) (specPtr t) (specPtr n) σ = ({((), σ3)}, False) → a3 = a → + recvBodyA st s3 a3 t ⦃ r => r.2 = a ∧ ∃ σ' R', + specRecvTailA (specPtr t) (specPtr n) σ = ({((), σ')}, False) ∧ + SchedSRA st { Z with R := R' } m κ r.1 σ' ∧ MFrA (st := st) Z m s r.1 σ σ' ⦄ := by + intro s3 a3 σ3 R3 H3 F3 hrq3 hd3 ha3 + obtain ⟨x3, hkt3, hsc3⟩ := H3.sch.tcb t ht + rw [recv_body_eqA] + refine WP.spec_bind (unblock_sporadic_SRA H3 ht false hrq3) ?_ + rintro s5 ⟨σ', h3, H', F⟩ + rw [WP.spec_ok] + refine ⟨ha3, σ', R3, ?_, H', F3.trans (F.mfr H3.toSchedSRN) rfl rfl⟩ + have h3' : Fl4SpecA64.ifSporadicCurScTestRefillUnblockCheck (Z := Unit) (optScA m (L.mem s3 (scA t))) σ3 = + ({((), σ')}, False) := h3 + unfold specRecvTailA Fl4SpecA64.getTcbObjRef + rw [nd_bind_oneSc hd3, nd_bind_oneSc (threadGet_eqSc _ hkt3), hsc3] + exact h3' + unfold rustRecvTailA + have nodon : (L.mem s (scA t) ≠ 0#u64 ∨ L.mem s (nScSc n) = 0#u64 ∨ + (L.mem s (nScSc n) ∈ Z.C ∧ L.mem s (scTcbA (L.mem s (nScSc n))) ≠ 0#u64)) → + (do + let (s3, a1) ← notification.maybe_donate_sched_context st ar s a t n + recvBodyA st s3 a1 t) ⦃ r => r.2 = a ∧ ∃ σ' R', + specRecvTailA (specPtr t) (specPtr n) σ = ({((), σ')}, False) ∧ + SchedSRA st { Z with R := R' } m κ r.1 σ' ∧ MFrA (st := st) Z m s r.1 σ σ' ⦄ := by + intro hno + refine WP.spec_bind (maybe_donate_nodonateA H a t n ht hN hno) ?_ + rintro r ⟨rfl, hd0⟩ + exact let_pairSc s a (fun s3 a1 => recvBodyA st s3 a1 t) _ (tail s a σ Z.R H (MFrA.refl Z m s σ) (fun h => absurd h hnq) hd0 rfl) + by_cases h0 : L.mem s (scA t) = 0#u64 + · by_cases h1 : L.mem s (nScSc n) = 0#u64 + · exact nodon (Or.inr (Or.inl h1)) + · obtain ⟨hC, hc⟩ := hd _ rfl h1 + by_cases h2 : L.mem s (scTcbA (L.mem s (nScSc n))) = 0#u64 + · refine WP.spec_bind (maybe_donate_localA H a hmb ht hon hN hst hnqs hnq hrunt h0 rfl h1 hC h2 hc) ?_ + rintro r ⟨hra, σ3, R', hd3, -, hrel3, -, hTsc3, -, -, -, hK3, hwM, H3⟩ + obtain ⟨s3, a3⟩ := r + dsimp only at hra H3 hwM + refine let_pairSc s3 a3 (fun s3 a1 => recvBodyA st s3 a1 t) _ + (tail s3 a3 σ3 R' H3 (H.toSchedSRN.don_mfr hC ht hwM hK3) ?_ hd3 hra) + intro hm y k hy + obtain ⟨x3, hkt3, hsc3⟩ := H3.sch.tcb t ht + rw [scNameA_of_optSc (hsc3.symm.trans (hTsc3 x3 hkt3))] at hy + exact hrel3 hm y k hy + · exact nodon (Or.inr (Or.inr ⟨hC, h2⟩)) + · exact nodon (Or.inl h0) + +/-- **The receive tail, local case** (module docstring). -/ +theorem recv_tail_localA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} + {σ : Fl4SpecA64.State Unit} (H : SchedSRA st Z m κ s σ) (a : A) + (hmb : sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) {t n : U64} (ht : t ∈ Z.T) + (hon : L.mem s (affA t) = L.cpu s) (hN : NtfnScEqA m n (L.mem s) σ) (hst : StEqA Z.T (L.mem s) σ) + (hnqs : ∀ d p, specPtr t ∉ σ.readyQueues d p) (hnq : specPtr t ∉ σ.releaseQueue) + (hrunt : ∀ x, σ.kheap (specPtr t) = some (.tcb x) → Fl4SpecA64.runnable x.tcbState = true) + (hd : ∀ sc, L.mem s (nScSc n) = sc → sc ≠ 0#u64 → sc ∈ Z.C ∧ L.mem s (scCoreA sc) = L.cpu s) : + rustRecvTailA st ar s a t n ⦃ r => r.2 = a ∧ ∃ σ' R', + specRecvTailA (specPtr t) (specPtr n) σ = ({((), σ')}, False) ∧ + SchedSRA st { Z with R := R' } m κ r.1 σ' ⦄ := + WP.spec_mono (recv_tail_frameA H a hmb ht hon hN hst hnqs hnq hrunt hd) + fun _ ⟨e1, σ', R', e2, H', _⟩ => ⟨e1, σ', R', e2, H'⟩ + +/-- **The complete tail, local case** (module docstring; gen 10: with the frame `MFrA`). -/ +theorem complete_tail_frameA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} + {σ : Fl4SpecA64.State Unit} (H : SchedSRA st Z m κ s σ) (a : A) + (hmb : sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) {t n : U64} (ht : t ∈ Z.T) + (hon : L.mem s (affA t) = L.cpu s) (hN : NtfnScEqA m n (L.mem s) σ) (hst : StEqA Z.T (L.mem s) σ) + (hnqs : ∀ d p, specPtr t ∉ σ.readyQueues d p) (hnq : specPtr t ∉ σ.releaseQueue) + (hrunt : ∀ x, σ.kheap (specPtr t) = some (.tcb x) → Fl4SpecA64.runnable x.tcbState = true) + (hd : ∀ sc, L.mem s (nScSc n) = sc → sc ≠ 0#u64 → sc ∈ Z.C ∧ L.mem s (scCoreA sc) = L.cpu s) + (hnA : Aligned (nScSc n)) (hnS : ¬ ScFootA Z.C m σ (nScSc n)) + (hnT : ∀ u ∈ Z.T, (nScSc n).val < u.val ∨ u.val + 4096 ≤ (nScSc n).val) + (hnK : (nScSc n).val < (L.ksb s).val ∨ (L.ksb s).val + ksSizeA ≤ (nScSc n).val) : + rustCompleteTailA st ar s a t n ⦃ r => r.2 = a ∧ ∃ σ' R', + specCompleteTailA (specPtr t) (specPtr n) σ = ({((), σ')}, False) ∧ + SchedSRA st { Z with R := R' } m κ r.1 σ' ∧ MFrA (st := st) Z m s r.1 σ σ' ⦄ := by + have tail : ∀ s3 a3 (σ3 : Fl4SpecA64.State Unit) R3, SchedSRA st { Z with R := R3 } m κ s3 σ3 → + MFrA (st := st) Z m s s3 σ σ3 → + (specPtr t ∈ σ3.releaseQueue → ∀ y k, σ3.kheap (scNameA m (L.mem s3 (scA t))) = some (.schedContext y k) → + Fl4SpecA64.refillReady σ3.curTime (Fl4SpecA64.refillHd y) = false) → + Fl4SpecA64.maybeDonateSc (Z := Unit) (specPtr t) (specPtr n) σ = ({((), σ3)}, False) → a3 = a → + NtfnScEqA m n (L.mem s3) σ3 → + completeBodyA st s3 a3 t n ⦃ r => r.2 = a ∧ ∃ σ' R', + specCompleteTailA (specPtr t) (specPtr n) σ = ({((), σ')}, False) ∧ + SchedSRA st { Z with R := R' } m κ r.1 σ' ∧ MFrA (st := st) Z m s r.1 σ σ' ⦄ := by + intro s3 a3 σ3 R3 H3 F3 hrq3 hd3 ha3 hN3 + obtain ⟨x3, hkt3, hsc3⟩ := H3.sch.tcb t ht + obtain ⟨z3, hkn3, hz3⟩ := hN3 + have hscx : ∀ p, optScA m (L.mem s3 (scA t)) = some p → ∃ y k, σ3.kheap p = some (.schedContext y k) := + fun p hp => let ⟨y, k, hy, _⟩ := H3.inv.back _ x3 p hkt3 (hsc3.trans hp); ⟨y, k, hy⟩ + have hsp : specCompleteTailA (specPtr t) (specPtr n) σ = + (if optScA m (L.mem s3 (scA t)) = z3.ntfnSc then Fl4SpecA64.ifCondRefillUnblockCheck (Z := Unit) + (optScA m (L.mem s3 (scA t))) (some false) (some false) σ3 else ({((), σ3)}, False)) := by + unfold specCompleteTailA Fl4SpecA64.getTcbObjRef + rw [nd_bind_oneSc hd3, nd_bind_oneSc (threadGet_eqSc _ hkt3), hsc3, complete_spec_eqA hkn3 _ hscx] + rw [complete_body_eqA] + by_cases ew : L.mem s3 (scA t) = L.mem s3 (nScSc n) + · rw [if_pos ew] + refine WP.spec_bind (unblock_sporadic_SRA H3 ht false hrq3) ?_ + rintro s5 ⟨σ', h3, H', F⟩ + rw [WP.spec_ok] + refine ⟨ha3, σ', R3, ?_, H', F3.trans (F.mfr H3.toSchedSRN) rfl rfl⟩ + rw [hsp, if_pos (by rw [hz3, ew])] + exact h3 + · rw [if_neg ew, WP.spec_ok] + refine ⟨ha3, σ3, R3, ?_, H3, F3⟩ + rw [hsp, if_neg (fun h => ew (optScA_eq_of (h.trans hz3) + (by rw [← hz3]; exact H3.inv.ntfnIdle _ z3 hkn3)))] + unfold rustCompleteTailA + have nodon : (L.mem s (scA t) ≠ 0#u64 ∨ L.mem s (nScSc n) = 0#u64 ∨ + (L.mem s (nScSc n) ∈ Z.C ∧ L.mem s (scTcbA (L.mem s (nScSc n))) ≠ 0#u64)) → + (do + let (s3, a1) ← notification.maybe_donate_sched_context st ar s a t n + completeBodyA st s3 a1 t n) ⦃ r => r.2 = a ∧ ∃ σ' R', + specCompleteTailA (specPtr t) (specPtr n) σ = ({((), σ')}, False) ∧ + SchedSRA st { Z with R := R' } m κ r.1 σ' ∧ MFrA (st := st) Z m s r.1 σ σ' ⦄ := by + intro hno + refine WP.spec_bind (maybe_donate_nodonateA H a t n ht hN hno) ?_ + rintro r ⟨rfl, hd0⟩ + exact let_pairSc s a (fun s3 a1 => completeBodyA st s3 a1 t n) _ + (tail s a σ Z.R H (MFrA.refl Z m s σ) (fun h => absurd h hnq) hd0 rfl hN) + by_cases h0 : L.mem s (scA t) = 0#u64 + · by_cases h1 : L.mem s (nScSc n) = 0#u64 + · exact nodon (Or.inr (Or.inl h1)) + · obtain ⟨hC, hc⟩ := hd _ rfl h1 + by_cases h2 : L.mem s (scTcbA (L.mem s (nScSc n))) = 0#u64 + · obtain ⟨z, hkn, hzs⟩ := hN + refine WP.spec_bind (maybe_donate_localA H a hmb ht hon ⟨z, hkn, hzs⟩ hst hnqs hnq hrunt h0 rfl h1 hC h2 hc) ?_ + rintro r ⟨hra, σ3, R', hd3, -, hrel3, -, hTsc3, -, hkO, -, hK3, hwM, H3⟩ + obtain ⟨s3, a3⟩ := r + dsimp only at hra H3 hwM + have hnw : L.mem s3 (nScSc n) = L.mem s (nScSc n) := + hwM.2.2 _ hnA (H.toSchedSRN.offW hC ht hnS hnT hnK) + have hN3 : NtfnScEqA m n (L.mem s3) σ3 := by + refine ⟨z, ?_, by rw [hnw]; exact hzs⟩ + rw [hkO _ (fun e => by + obtain ⟨xt, hkt, -⟩ := H.sch.tcb t ht + rw [← e, hkn] at hkt; cases hkt) + (fun e => by + obtain ⟨xs, ns, hks, -⟩ := H.scr.sc _ hC + rw [← e, hkn] at hks; cases hks)] + exact hkn + refine let_pairSc s3 a3 (fun s3 a1 => completeBodyA st s3 a1 t n) _ + (tail s3 a3 σ3 R' H3 (H.toSchedSRN.don_mfr hC ht hwM hK3) ?_ hd3 hra hN3) + intro hm y k hy + obtain ⟨x3, hkt3, hsc3⟩ := H3.sch.tcb t ht + rw [scNameA_of_optSc (hsc3.symm.trans (hTsc3 x3 hkt3))] at hy + exact hrel3 hm y k hy + · exact nodon (Or.inr (Or.inr ⟨hC, h2⟩)) + · exact nodon (Or.inl h0) + +/-- **The complete tail, local case** (module docstring). -/ +theorem complete_tail_localA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} + {σ : Fl4SpecA64.State Unit} (H : SchedSRA st Z m κ s σ) (a : A) + (hmb : sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) {t n : U64} (ht : t ∈ Z.T) + (hon : L.mem s (affA t) = L.cpu s) (hN : NtfnScEqA m n (L.mem s) σ) (hst : StEqA Z.T (L.mem s) σ) + (hnqs : ∀ d p, specPtr t ∉ σ.readyQueues d p) (hnq : specPtr t ∉ σ.releaseQueue) + (hrunt : ∀ x, σ.kheap (specPtr t) = some (.tcb x) → Fl4SpecA64.runnable x.tcbState = true) + (hd : ∀ sc, L.mem s (nScSc n) = sc → sc ≠ 0#u64 → sc ∈ Z.C ∧ L.mem s (scCoreA sc) = L.cpu s) + (hnA : Aligned (nScSc n)) (hnS : ¬ ScFootA Z.C m σ (nScSc n)) + (hnT : ∀ u ∈ Z.T, (nScSc n).val < u.val ∨ u.val + 4096 ≤ (nScSc n).val) + (hnK : (nScSc n).val < (L.ksb s).val ∨ (L.ksb s).val + ksSizeA ≤ (nScSc n).val) : + rustCompleteTailA st ar s a t n ⦃ r => r.2 = a ∧ ∃ σ' R', + specCompleteTailA (specPtr t) (specPtr n) σ = ({((), σ')}, False) ∧ + SchedSRA st { Z with R := R' } m κ r.1 σ' ⦄ := + WP.spec_mono (complete_tail_frameA H a hmb ht hon hN hst hnqs hnq hrunt hd hnA hnS hnT hnK) + fun _ ⟨e1, σ', R', e2, H', _⟩ => ⟨e1, σ', R', e2, H'⟩ + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScRefillA.lean b/proofs/Fl4ProofsA64/P3/ScRefillA.lean new file mode 100644 index 000000000..1cdd854cd --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScRefillA.lean @@ -0,0 +1,364 @@ +/- +P3 (aarch64, A64-P3-MCS gen 1): **the refill-ring operations and the `charge_budget` leaves against +`Fl4SpecA64`**, at `ScStateRelA` / `ScPlacedA` (`P3/ScRelA`). + +FL4's AArch64 `sporadic::{sc_active, is_round_robin, ready, capacity, sufficient, pop_head, get, put}` and +`sched::charge_budget`'s SC part are the x86 text (A64 P1's `Refill` re-checks them), so the x86 leaves port with +the objects named `scNameA m sc` and the relation `ScStateRelA` in place of x86's `SchedRel`: +* **`sc_active_refinesA`** (new; x86 reads `sc_active` through `ScRelW.active_of_rmax`): `sporadic::sc_active` is + l4v's `get_sc_active`. +* **`is_round_robin_refinesA`** (x86 `ScChargeG`'s `hrr`): `sporadic::is_round_robin` is l4v's `is_round_robin`. +* **`ready_refinesA`** (x86 `ScDonate.ready_refines`): `sporadic::ready` is l4v's `get_sc_refill_ready` of an active + SC, given the view's clock is FL4's current core's (`htime`, = `SchedRelA.time` at `JAtZA`). +* **`sufficient_refinesA`** (x86 `ScDonate.sufficient_refines`): `sporadic::sufficient` is + `get_sc_refill_sufficient`, given the platform's `MIN_BUDGET` is the spec's (`hmb`, x86's hypothesis of the same + shape, the platform fact x86 carries as `minBudget`; A64 P2's `WcetConst` makes the word constant). +* **`pop_head_refinesA`** (x86 `ScRing.pop_head_ring` + `SchedRel.ringAt` + `ScSpec.refillPopHead_eq`): on a ring of + at least two refills (`hne`, l4v's callers pop only then), `sporadic::pop_head` is `refill_pop_head`; the relation + and the placement hold after, FL4 writes only the SC's head index. +* **`rr_reset_refinesA`** (x86 `ScCharge6.rr_reset_refines` + `ScCharge2.refillResetRr_eq`): `charge_budget`'s + round-robin branch (the tail's amount moves to the head) is `refill_reset_rr`. +* **`consumed_refinesA`** (x86 `ScCharge1.SchedRel.set_sc_consumed`, `consumed_refines`): `charge_budget`'s + `sc.consumed += used` is `update_sched_context … sc_consumed_update (+ used)`. +Each leaf on a writing operation returns the spec's single outcome, `ScStateRelA` and `ScPlacedA` at it, and FL4's +writes inside the SC's object (`WritesIn … (ScObjA sc n)`, so `ScPlacedA.th_out` / `.ks_out` frame every other +relation). + +No named premise. Case hypotheses only: `hc` (`sc ∈ C`), `hk` (the spec object), `ha` (active), `hne` (two refills); +`htime` / `hmb` as above. +-/ +import Fl4ProofsA64.P3.ScSpecA +import Fl4SpecA64.Ext.SchedControl +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +/-! ## Words -/ + +theorem gt_iff_valSc {a b : U64} : a > b ↔ b.val < a.val := by + simp [GT.gt, UScalar.lt_equiv] + +theorem u64_le_bvSc (a b : U64) : a ≤ b ↔ a.bv ≤ b.bv := by + rw [UScalar.le_equiv, BitVec.le_def]; rfl + +theorem u64_eq_of_bvSc {a b : U64} (h : a.bv = b.bv) : a = b := by + cases a; cases b; cases h; rfl + +theorem obj.Sc.consumed.specSc (s : S) (sc : obj.Sc) : + obj.Sc.consumed st sc s ⦃ v => v = L.mem s (scWA sc 8#u64) ⦄ := by + unfold obj.Sc.consumed; step*; simp_all [scWA, layout.sc.CONSUMED] + +theorem obj.Sc.set_consumed.specSc (s : S) (sc : obj.Sc) (v : U64) : + obj.Sc.set_consumed st sc s v ⦃ s' => s' = L.wr s (scWA sc 8#u64) v ⦄ := by + unfold obj.Sc.set_consumed; step*; simp_all [scWA, layout.sc.CONSUMED] + +/-- Writing a ring slot leaves the header words (x86 `hdr_putS_head'`). -/ +theorem mem_putS_hdrSc {s : S} {sc : obj.Sc} {sb : Nat} (hW : RefillsWF st s sc sb) {i : U64} + (hi : i.val < (L.rmax s sc).val) (r : sporadic.Refill) {K : U64} (hK : K.val < 96) (hK8 : K.val % 8 = 0) : + L.mem (L.putS s sc i r) (scWA sc K) = L.mem s (scWA sc K) := by + obtain ⟨h8, hb, hfit⟩ := hW.bounds + obtain ⟨-, -, -, -, -, hin⟩ := hW.inside + obtain ⟨g1, g2, -⟩ := hin i hi + have hv := scWA_val (sc := sc) (K := K) (by omega) + unfold LawfulStore.putS + rw [L.mem_wr_ne (by unfold Aligned; rw [g2]; omega) (by unfold Aligned; rw [hv]; omega) (by rw [g2, hv]; omega), + L.mem_wr_ne (by unfold Aligned; rw [g1]; omega) (by unfold Aligned; rw [hv]; omega) (by rw [g1, hv]; omega)] + +/-- Writing a ring slot writes inside the object. -/ +theorem writesIn_putSSc {s : S} {sc : obj.Sc} {n : Nat} (hW : RefillsWF st s sc (n + Fl4SpecA64.minSchedContextBits)) + {i : U64} (hi : i.val < (L.rmax s sc).val) (r : sporadic.Refill) : + WritesIn st s (L.putS s sc i r) (ScObjA sc n) := by + obtain ⟨h8, hb, hfit⟩ := hW.bounds + obtain ⟨-, -, -, -, -, hin⟩ := hW.inside + obtain ⟨g1, g2, -⟩ := hin i hi + have a1 : Aligned (rtA sc i) := by unfold Aligned; rw [g1]; omega + have a2 : Aligned (raA sc i) := by unfold Aligned; rw [g2]; omega + refine ((writesIn_wr (st := st) (s := s) (v := r.time) a1).trans + (writesIn_wr (st := st) (s := L.wr s (rtA sc i) r.time) (v := r.amount) a2)).mono fun b hb' => ?_ + rcases hb' with rfl | rfl + · exact ⟨by rw [g1]; omega, by rw [g1]; omega⟩ + · exact ⟨by rw [g2]; omega, by rw [g2]; omega⟩ + +/-! ## Reads -/ + +/-- **`sc_active` is `get_sc_active`.** -/ +theorem sc_active_refinesA {C : Set U64} {m : Fl4SpecA64.McState} {s : S} {σ : Fl4SpecA64.State Unit} + (h : ScStateRelA st C m s σ) {sc : obj.Sc} (hc : sc ∈ C) {x : Fl4SpecA64.SchedContext} {n : Nat} + (hk : σ.kheap (scNameA m sc) = some (.schedContext x n)) : + sporadic.sc_active st s sc ⦃ b => Fl4SpecA64.getScActive (Z := Unit) (scNameA m sc) σ = ({(b, σ)}, False) ⦄ := by + obtain ⟨x', n', hk', hx⟩ := h.sc sc hc + rw [hk] at hk'; cases hk' + unfold sporadic.sc_active + step with obj.Sc.refill_max.spec as ⟨i, hi⟩ + rw [getScActive_eqA hk] + simp only [Fl4SpecA64.scActive, Fl4SpecA64.activeSc, ← hx.max, hi] + congr 3 + +/-- **`is_round_robin` is `is_round_robin`.** -/ +theorem is_round_robin_refinesA {C : Set U64} {m : Fl4SpecA64.McState} {s : S} {σ : Fl4SpecA64.State Unit} + (h : ScStateRelA st C m s σ) {sc : obj.Sc} (hc : sc ∈ C) {x : Fl4SpecA64.SchedContext} {n : Nat} + (hk : σ.kheap (scNameA m sc) = some (.schedContext x n)) : + sporadic.is_round_robin st s sc ⦃ b => + Fl4SpecA64.isRoundRobin (Z := Unit) (scNameA m sc) σ = ({(b, σ)}, False) ⦄ := by + obtain ⟨x', n', hk', hx⟩ := h.sc sc hc + rw [hk] at hk'; cases hk' + refine WP.spec_mono (sporadic.is_round_robin.spec s sc) ?_ + rintro b rfl + rw [isRoundRobin_eqA hk] + congr 3 + refine decide_eq_decide.2 ?_ + rw [hx.period] + exact ⟨fun e => u64_eq_of_bvSc (by rw [e]; rfl), fun e => by rw [e]; rfl⟩ + +/-- **`ready` is `get_sc_refill_ready`** (x86 `ready_refines`). -/ +theorem ready_refinesA {C : Set U64} {m : Fl4SpecA64.McState} {s : S} {σ : Fl4SpecA64.State Unit} + (h : ScStateRelA st C m s σ) (htime : σ.curTime = (L.curTime s).bv) {sc : obj.Sc} (hc : sc ∈ C) + {x : Fl4SpecA64.SchedContext} {n : Nat} (hk : σ.kheap (scNameA m sc) = some (.schedContext x n)) + (ha : Fl4SpecA64.scActive x = true) : + sporadic.ready st s sc ⦃ b => + Fl4SpecA64.getScRefillReady (Z := Unit) (scNameA m sc) σ = ({(b, σ)}, False) ⦄ := by + obtain ⟨x', n', hk', hx⟩ := h.sc sc hc + rw [hk] at hk'; cases hk' + replace hx := hx.strong ha + have hW := hx.wf + have hne : x.scRefills ≠ [] := by rw [hx.refills]; exact refills_neA hW + refine WP.spec_mono (sporadic.ready.spec s sc) ?_ + rintro b rfl + rw [getScRefillReady_eqA hk hne, refillHd_eqA hW hx.refills, htime] + congr 3 + unfold Fl4SpecA64.refillReady Fl4SpecA64.refillReady' + simp only [toRefillA] + refine decide_eq_decide.2 ?_ + rw [u64_le_bvSc] + exact iff_of_eq (congrArg (· ≤ _) (BitVec.add_zero _)) + +private theorem sufficientWith_refinesA {C : Set U64} {m : Fl4SpecA64.McState} {s : S} (a : A) {σ : Fl4SpecA64.State Unit} + (h : ScStateRelA st C m s σ) {sc : obj.Sc} (hc : sc ∈ C) {x : Fl4SpecA64.SchedContext} {n : Nat} + (hk : σ.kheap (scNameA m sc) = some (.schedContext x n)) (ha : Fl4SpecA64.scActive x = true) (usage : U64) + (minimum : Fl4SpecA64.Ticks) (hmb : sporadic.min_budget ar a ⦃ mb => mb.bv = minimum ⦄) : + sporadic.sufficient st ar s a sc usage ⦃ b => + decide (minimum ≤ Fl4SpecA64.refillCapacity usage.bv (Fl4SpecA64.refillHd x)) = b ⦄ := by + obtain ⟨x', n', hk', hx⟩ := h.sc sc hc + rw [hk] at hk'; cases hk' + replace hx := hx.strong ha + have hW := hx.wf + have hd : Fl4SpecA64.refillHd x = toRefillA (L.refill s sc (L.rhead s sc)) := refillHd_eqA hW hx.refills + unfold sporadic.sufficient sporadic.capacity + step with sporadic.head.spec as ⟨r, r_post⟩ + rw [hd, ← r_post] + by_cases hu : usage > r.amount + · simp only [hu, ↓reduceIte, bind_ok] + refine WP.spec_bind hmb ?_ + intro mb hmb' + simp only [WP.spec_ok] + have hlt : r.amount.bv < usage.bv := by + have := (gt_iff_valSc).1 hu; rw [BitVec.lt_def]; exact this + unfold Fl4SpecA64.refillCapacity + simp only [toRefillA, hlt, ↓reduceIte] + refine decide_eq_decide.2 ?_ + rw [← hmb', GE.ge, UScalar.le_equiv, BitVec.le_def]; rfl + · simp only [hu, ↓reduceIte] + have hle : usage.val ≤ r.amount.val := by have := (gt_iff_valSc).not.1 hu; omega + have hsub : (r.amount - usage) ⦃ d => d.val = r.amount.val - usage.val ⦄ := by step* + refine WP.spec_bind hsub ?_ + intro d hd' + refine WP.spec_bind hmb ?_ + intro mb hmb' + simp only [WP.spec_ok] + have hnlt : ¬ r.amount.bv < usage.bv := by rw [BitVec.lt_def]; exact Nat.not_lt.2 hle + unfold Fl4SpecA64.refillCapacity + simp only [toRefillA, hnlt, ↓reduceIte] + refine decide_eq_decide.2 ?_ + have hdb : d.bv = r.amount.bv - usage.bv := by + apply BitVec.eq_of_toNat_eq + rw [BitVec.toNat_sub_of_le (by rw [BitVec.le_def]; exact hle)] + exact hd' + rw [← hmb', ← hdb, GE.ge, UScalar.le_equiv, BitVec.le_def]; rfl + +/-- **`sufficient` is `get_sc_refill_sufficient`** (x86 `sufficient_refines`), given the platform's minimum budget is +the spec's. -/ +theorem sufficient_refinesA {C : Set U64} {m : Fl4SpecA64.McState} {s : S} (a : A) {σ : Fl4SpecA64.State Unit} + (h : ScStateRelA st C m s σ) {sc : obj.Sc} (hc : sc ∈ C) {x : Fl4SpecA64.SchedContext} {n : Nat} + (hk : σ.kheap (scNameA m sc) = some (.schedContext x n)) (ha : Fl4SpecA64.scActive x = true) (usage : U64) + (hmb : sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) : + sporadic.sufficient st ar s a sc usage ⦃ b => + Fl4SpecA64.getScRefillSufficient (Z := Unit) (scNameA m sc) usage.bv σ = ({(b, σ)}, False) ⦄ := by + have hne : x.scRefills ≠ [] := by + obtain ⟨y, j, hy, R⟩ := h.sc sc hc + rw [hk] at hy + cases hy + have R' := R.strong ha + rw [R'.refills] + exact refills_neA R'.wf + refine WP.spec_mono (sufficientWith_refinesA a h hc hk ha usage Fl4SpecA64.MIN_BUDGET hmb) ?_ + intro b hb + rw [getScRefillSufficient_eqA hk hne] + unfold Fl4SpecA64.refillSufficient + rw [hb] + +/-- [P] AArch64's concrete minimum-budget operation agrees with the FL4 timer. -/ +def Fl4MinBudgetAgreeA (ar : machine.Arch A) (a : A) : Prop := + sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.fl4MinBudget ⦄ + +/-- The FL4 comparison used by the live Reply and timeout tail. -/ +theorem fl4_sufficient_refinesA {C : Set U64} {m : Fl4SpecA64.McState} {s : S} (a : A) {σ : Fl4SpecA64.State Unit} + (h : ScStateRelA st C m s σ) {sc : obj.Sc} (hc : sc ∈ C) {x : Fl4SpecA64.SchedContext} {n : Nat} + (hk : σ.kheap (scNameA m sc) = some (.schedContext x n)) (ha : Fl4SpecA64.scActive x = true) (usage : U64) + (hmb : Fl4MinBudgetAgreeA ar a) : + sporadic.sufficient st ar s a sc usage ⦃ b => + Fl4SpecA64.fl4RefillSufficient usage.bv (Fl4SpecA64.refillHd x) = b ⦄ := + sufficientWith_refinesA a h hc hk ha usage Fl4SpecA64.fl4MinBudget hmb + +/-! ## Writes -/ + +/-- **`pop_head` is `refill_pop_head`** on a ring of at least two refills (x86 `pop_head_ring`, `SchedRel.ringAt`, +`refillPopHead_eq`). -/ +theorem pop_head_refinesA {th : TypedHeapA} {ksb : U64} {C : Set U64} {m : Fl4SpecA64.McState} {s : S} + {σ : Fl4SpecA64.State Unit} (h : ScStateRelA st C m s σ) (P : ScPlacedA th ksb C m σ) {sc : obj.Sc} + (hc : sc ∈ C) {x : Fl4SpecA64.SchedContext} {n : Nat} (hk : σ.kheap (scNameA m sc) = some (.schedContext x n)) + (ha : Fl4SpecA64.scActive x = true) (hne : L.rhead s sc ≠ L.rtail s sc) : + sporadic.pop_head st s sc ⦃ r s' => ∃ σ', Fl4SpecA64.refillPopHead (Z := Unit) (scNameA m sc) σ = + ({(toRefillA r, σ')}, False) ∧ ScStateRelA st C m s' σ' ∧ ScPlacedA th ksb C m σ' ∧ + WritesIn st s s' (ScObjA sc n) ⦄ := by + obtain ⟨x', n', hk', hx⟩ := h.sc sc hc + rw [hk] at hk'; cases hk' + have h1 := hx.strong ha + have hW := h1.wf + have hne' : x.scRefills ≠ [] := by rw [h1.refills]; exact refills_neA hW + refine WP.spec_mono (sporadic.pop_head.eq s sc) ?_ + rintro ⟨r, s'⟩ ⟨rfl, hs'⟩ + obtain ⟨h8, hb, hfit⟩ := hW.bounds + obtain ⟨-, -, -, hh, -, -⟩ := hW.inside + have hah : Aligned (headA sc) := by unfold Aligned; rw [hh]; omega + have hW' : RefillsWF st s' sc (n + Fl4SpecA64.minSchedContextBits) := by + rw [hs']; exact hW.wr_head (nextIdx_lt hW.head_lt) + have hw : WritesIn st s s' (ScObjA sc n) := by + rw [hs']; exact (writesIn_wr hah).mono fun b e => by subst e; exact ⟨by rw [hh]; omega, by rw [hh]; omega⟩ + have hhdr : ∀ K : U64, K.val < 80 → K.val % 8 = 0 → L.mem s' (scWA sc K) = L.mem s (scWA sc K) := by + intro K hK hK8 + have hv := scWA_val (sc := sc) (K := K) (by omega) + rw [hs'] + exact L.mem_wr_ne hah (by unfold Aligned; rw [hv]; omega) (by rw [hh, hv]; omega) + have hx1 := h1.ring hW' hhdr + have el : (L.refills s' sc).map toRefillA = x.scRefills.tail := by + rw [refills_popA hW hne hs', h1.refills]; simp + rw [el] at hx1 + obtain ⟨hS', hP'⟩ := h.upd P hc hk hw hx1.toW + refine ⟨_, ?_, hS', hP', hw⟩ + rw [refillPopHead_eqA hk hne', refillHd_eqA hW h1.refills] + +/-- **`charge_budget`'s round-robin branch is `refill_reset_rr`** (x86 `rr_reset_refines`, `refillResetRr_eq`). -/ +theorem rr_reset_coreA {th : TypedHeapA} {ksb : U64} {C : Set U64} {m : Fl4SpecA64.McState} {s : S} + {σ : Fl4SpecA64.State Unit} (h : ScStateRelA st C m s σ) (P : ScPlacedA th ksb C m σ) {sc : obj.Sc} + (hc : sc ∈ C) {x : Fl4SpecA64.SchedContext} {n : Nat} (hk : σ.kheap (scNameA m sc) = some (.schedContext x n)) + (ha : Fl4SpecA64.scActive x = true) : + (do + let hi ← obj.Sc.refill_head st sc s + let ti ← obj.Sc.refill_tail st sc s + let h ← sporadic.get st s sc hi + let t ← sporadic.get st s sc ti + let i ← lift (core.num.U64.wrapping_add h.amount t.amount) + let s4 ← sporadic.put st s sc hi { h with amount := i } + sporadic.put st s4 sc ti { t with amount := 0#u64 }) ⦃ s' => ∃ σ', + Fl4SpecA64.refillResetRr (Z := Unit) (scNameA m sc) σ = ({((), σ')}, False) ∧ ScStateRelA st C m s' σ' ∧ + ScPlacedA th ksb C m σ' ∧ WritesIn st s s' (ScObjA sc n) ∧ + L.mem s' (scWA sc 16#u64) = L.mem s (scWA sc 16#u64) ⦄ := by + obtain ⟨x', n', hk', hx⟩ := h.sc sc hc + rw [hk] at hk'; cases hk' + have h1 := hx.strong ha + have hW := h1.wf + step with obj.Sc.refill_head.spec as ⟨hi, hhi⟩ + step with obj.Sc.refill_tail.spec as ⟨ti, hti⟩ + step with sporadic.get.spec as ⟨hr, hhr⟩ + step with sporadic.get.spec as ⟨tr, htr⟩ + simp only [lift, bind_ok] + step with sporadic.put.spec as ⟨s4, hs4⟩ + refine WP.spec_mono (sporadic.put.spec s4 sc ti _) ?_ + rintro s5 rfl + subst hhi hti hhr htr hs4 + obtain ⟨h8, hb, hfit⟩ := hW.bounds + have hhl := hW.head_lt + set r1 : sporadic.Refill := ⟨(L.refill s sc (L.rhead s sc)).time, core.num.U64.wrapping_add + (L.refill s sc (L.rhead s sc)).amount (L.refill s sc (L.rtail s sc)).amount⟩ with hr1 + set r2 : sporadic.Refill := ⟨(L.refill s sc (L.rtail s sc)).time, 0#u64⟩ with hr2 + have W1 : RefillsWF st (L.putS s sc (L.rhead s sc) r1) sc (n + Fl4SpecA64.minSchedContextBits) := hW.putS hhl + have e1 : L.refills (L.putS s sc (L.rhead s sc) r1) sc = r1 :: (L.refills s sc).tail := by + rw [refills_ofA (hW.size_putS hhl) (fun k hk => hW.rf_set_head (by have := hW.size_le; omega)), + refills_set_headA hW] + have hm1 : L.rmax (L.putS s sc (L.rhead s sc) r1) sc = L.rmax s sc := rmax_putS h8 (by omega) + have et : L.rtail (L.putS s sc (L.rhead s sc) r1) sc = L.rtail s sc := rtail_putS h8 (by omega) + have htl : (L.rtail s sc).val < (L.rmax (L.putS s sc (L.rhead s sc) r1) sc).val := by rw [hm1]; exact hW.tail_lt + have ep : L.putS (L.putS s sc (L.rhead s sc) r1) sc (L.rtail s sc) r2 = + L.putS (L.putS s sc (L.rhead s sc) r1) sc (L.rtail (L.putS s sc (L.rhead s sc) r1) sc) r2 := by rw [et] + have W2 := W1.putS (r := r2) htl + have hrs : L.refills (L.putS (L.putS s sc (L.rhead s sc) r1) sc (L.rtail s sc) r2) sc = + rrResetLA (L.refills s sc) := by + rw [ep, refills_set_tailA W1 r2, e1] + unfold rrResetLA + rw [← head_eqA hW, ← refills_tail_eqA hW] + have hhdr : ∀ K : U64, K.val < 80 → K.val % 8 = 0 → + L.mem (L.putS (L.putS s sc (L.rhead s sc) r1) sc (L.rtail s sc) r2) (scWA sc K) = L.mem s (scWA sc K) := + fun K hK hK8 => (mem_putS_hdrSc W1 htl r2 (by omega) hK8).trans (mem_putS_hdrSc hW hhl r1 (by omega) hK8) + have hw : WritesIn st s (L.putS (L.putS s sc (L.rhead s sc) r1) sc (L.rtail s sc) r2) (ScObjA sc n) := + ((writesIn_putSSc hW hhl r1).trans (writesIn_putSSc W1 htl r2)).mono fun b hb' => by + rcases hb' with e | e <;> exact e + have hx1 := h1.ring W2 hhdr + rw [hrs] at hx1 + obtain ⟨hS', hP'⟩ := h.upd P hc hk hw hx1.toW + have hne : L.refills s sc ≠ [] := by rw [refills_consA hW]; simp + exact ⟨_, refillResetRr_eqA hk h1.refills hne, hS', hP', hw, hhdr _ (by simp) (by simp)⟩ + +/-- The refill reset preserves the SC relation and the SC object footprint. -/ +theorem rr_reset_refinesA {th : TypedHeapA} {ksb : U64} {C : Set U64} {m : Fl4SpecA64.McState} {s : S} + {σ : Fl4SpecA64.State Unit} (h : ScStateRelA st C m s σ) (P : ScPlacedA th ksb C m σ) {sc : obj.Sc} + (hc : sc ∈ C) {x : Fl4SpecA64.SchedContext} {n : Nat} (hk : σ.kheap (scNameA m sc) = some (.schedContext x n)) + (ha : Fl4SpecA64.scActive x = true) : + (do + let hi ← obj.Sc.refill_head st sc s + let ti ← obj.Sc.refill_tail st sc s + let h ← sporadic.get st s sc hi + let t ← sporadic.get st s sc ti + let i ← lift (core.num.U64.wrapping_add h.amount t.amount) + let s4 ← sporadic.put st s sc hi { h with amount := i } + sporadic.put st s4 sc ti { t with amount := 0#u64 }) ⦃ s' => ∃ σ', + Fl4SpecA64.refillResetRr (Z := Unit) (scNameA m sc) σ = ({((), σ')}, False) ∧ ScStateRelA st C m s' σ' ∧ + ScPlacedA th ksb C m σ' ∧ WritesIn st s s' (ScObjA sc n) ⦄ := by + refine WP.spec_mono (rr_reset_coreA h P hc hk ha) ?_ + rintro s' ⟨σ', he, hs, hp, hw, _⟩ + exact ⟨σ', he, hs, hp, hw⟩ + +/-- **`charge_budget`'s `consumed` update is `update_sched_context … sc_consumed`** (x86 `SchedRel.set_sc_consumed`, +`consumed_refines`). -/ +theorem consumed_refinesA {th : TypedHeapA} {ksb : U64} {C : Set U64} {m : Fl4SpecA64.McState} {s : S} + {σ : Fl4SpecA64.State Unit} (h : ScStateRelA st C m s σ) (P : ScPlacedA th ksb C m σ) {sc : obj.Sc} + (hc : sc ∈ C) {x : Fl4SpecA64.SchedContext} {n : Nat} (hk : σ.kheap (scNameA m sc) = some (.schedContext x n)) + (used : U64) : + (do + let i ← obj.Sc.consumed st sc s + let total ← lift (core.num.U64.wrapping_add i used) + obj.Sc.set_consumed st sc s total) ⦃ s' => ∃ σ', + Fl4SpecA64.updateSchedContext (Z := Unit) (scNameA m sc) + (fun y => { y with scConsumed := y.scConsumed + used.bv }) σ = ({((), σ')}, False) ∧ + ScStateRelA st C m s' σ' ∧ ScPlacedA th ksb C m σ' ∧ WritesIn st s s' (ScObjA sc n) ⦄ := by + obtain ⟨x', n', hk', hx⟩ := h.sc sc hc + rw [hk] at hk'; cases hk' + step with obj.Sc.consumed.specSc as ⟨i, hi⟩ + simp only [lift, bind_ok] + refine WP.spec_mono (obj.Sc.set_consumed.specSc s sc _) ?_ + rintro s' rfl + obtain ⟨hx', hw⟩ := hx.set_consumed (core.num.U64.wrapping_add i used) + obtain ⟨hS', hP'⟩ := h.upd P hc hk hw hx' + refine ⟨_, ?_, hS', hP', hw⟩ + rw [updateSchedContext_eqA hk, core.num.U64.wrapping_add_bv_eq, hi, ← hx.consumed] + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScRelA.lean b/proofs/Fl4ProofsA64/P3/ScRelA.lean new file mode 100644 index 000000000..b4389de4c --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScRelA.lean @@ -0,0 +1,471 @@ +/- +P3 (aarch64, A64-P3-MCS gen 1): **the scheduling-context relation `ScStateRelA`** and its placement `ScPlacedA` +(x86 `P3/ScRel`'s `ScRel1` / `ScRelW`, `P3/SchedRel`'s `SchedSpec.scs` and `SchedRel.scOut` / `scDisj`, ported). + +FL4's AArch64 SC layout and refill ring are x86's (`layout.rs` `sc`: `PERIOD` 0, `CONSUMED` 8, `CORE` 16, `TCB` 24, +`REPLY` 32, `NTFN` 40, `BADGE` 48, `YIELD_FROM` 56, `SPORADIC` 64, `REFILL_MAX` 72, `REFILL_HEAD` 80, `REFILL_TAIL` +88, then 16-byte refills), and A64 P1's `Refill` (`RefillsWF`, `L.refills`) is the x86 text re-checked, so x86's +`ScRel1` / `ScRelW` port with these changes by hand: +* **objects named by `specPtr`**: an SC `c` is the spec's object at `scNameA m c` (= JointA's `scRenA m c`, the + view's idle SC renamed to l4v's `idle_sc_ptr`; `scNameA_eq` in `P3/ScJoinA`), its TCB / notification / yield-from / + reply words are `optRefS` (= JointA's `optRefA`, `specPtr`, 0 ↔ none); +* **the reply link** (new, ruling 21:07Z item 1): `sc_replies`' head is FL4's `REPLY` word (FL4 keeps the head of the + reply stack in the SC and links the rest through the replies, `ipc.rs` `reply_push` / `reply_pop` / + `reply_remove_tcb`, as l4v's `reply_push` conses onto `sc_replies` and `reply_pop` / `reply_remove_tcb` drop the + head); the rest of the stack is the reply relation's (IPC line); +* `ScRelWA`'s idle clause takes the proposition "this is **some core's** idle SC" (`IdleScA m c`: `∃ k < + numNodes, m.idleScOf k = specPtr c`; gen 2, ruling 21:31Z Q3: every core's idle SC, not only the view's) in place + of x86's `sc.bv = σ.idleSc`. +Not related, as on x86: `sc_budget` (FL4 does not store it), FL4's `CORE` word (no spec counterpart: the spec state is +one core's view, `Ext/MultiCore` has no SC affinity). + +* `ScRel1A s sc x n` (x86 `ScRel1`): the full relation of a configured SC (`RefillsWF`, header words, ring as list). +* `ScRelWA ii s sc x n` (x86 `ScRelW`): any SC of `C`, configured or not (`act`: the full relation when + `REFILL_MAX ≠ 0`, the spec's `sc_active`). +* **`ScStateRelA C m s σ`** (x86 `SchedSpec.scs`): every SC of `C` is the spec's `.schedContext x n` at + `scNameA m c`, related by `ScRelWA`. Self-contained (ruling 21:07Z): outside `JAtZA` / `XSetsA` until the + coordinated join (handoff `a64p3mcs.md`). +* **`ScPlacedA th ksb C m σ`** (x86 `SchedRel.scOut` / `scDisj`, `ScTopCfg.ScPlaced`): the spec names of `C` are + distinct (`inj`), the SC objects (`2 ^ (n + 7)` bytes, `ScObjA`) are pairwise disjoint (`disj`) and lie off every + object of the typed heap `th` and off the kernel-state block (`out`). The SCs are not objects of `th` (JointA's + `InDomA`; at boot `bootThA` has none), so placement is its own record, like the IPC line's `NPlaced` / `EPlaced`. +* `ScFootA C m σ` (x86 `KFoot`'s SC arm): the words of the related SC objects. **`ScStateRelA.frame`**: the relation + survives any FL4 step that keeps those words and any spec step that keeps the SC objects; + `ScPlacedA.th_out` / `.ks_out`: TCB, slot and kernel-block words are not SC words (so the CSpace / TCB leaves keep + it). **`ScStateRelA.upd`**: writes inside one SC's object and its spec object replaced (same size) by a related + one keep the relation and the placement. + +No named premise. Imports only A64 P1 and `P3/StateRelA` (typed heap, `ksSizeA`, `specPtr`), **not `JointA`**, so +`JointA` can import it when `ScStateRelA` becomes a `JAtZA` field. +-/ +import Fl4ProofsA64.P3.StateRelA +import Fl4ProofsA64.P1.Refill +import Fl4ProofsA64.P1.QueueInv +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +/-! ## Names -/ + +/-- **The spec name of an FL4 SC in the view of core `m.curCore`** (= JointA's `scRenA`, `ScJoinA.scNameA_eq`): that +core's idle SC is l4v's `idle_sc_ptr`, every other SC its `specPtr`. -/ +def scNameA (m : Fl4SpecA64.McState) (p : U64) : Fl4SpecA64.ObjRef := + if specPtr p = m.idleScOf m.curCore then Fl4SpecA64.idleScPtr else specPtr p + +/-- **`c` is the idle SC of one of the `numNodes` cores** (gen 2, ruling 21:31Z Q3; x86 `sc.bv = σ.idleSc`). -/ +def IdleScA (m : Fl4SpecA64.McState) (c : U64) : Prop := + ∃ k, k < Fl4SpecA64.fl4A64Platform.numNodes ∧ m.idleScOf k = specPtr c + +/-- The view's own idle SC is an idle SC (the view's core is online, `McState.WF`). -/ +theorem IdleScA.of_view {m : Fl4SpecA64.McState} {c : U64} + (hw : m.WF Fl4SpecA64.fl4A64Platform.numNodes) (h : specPtr c = m.idleScOf m.curCore) : IdleScA m c := + ⟨m.curCore, hw.curCore_lt, h.symm⟩ + +/-- An FL4 pointer word as an optional spec reference (= JointA's `optRefA`, `ScJoinA.optRefS_eq`; x86 `optRef`). -/ +def optRefS (w : U64) : Option Fl4SpecA64.ObjRef := if w = 0#u64 then none else some (specPtr w) + +theorem optRefS_zero : optRefS 0#u64 = none := by simp [optRefS] + +theorem optRefS_ne {w : U64} (h : w ≠ 0#u64) : optRefS w = some (specPtr w) := by simp [optRefS, h] + +section +variable {S : Type _} (st : machine.Store S) [L : LawfulStore st] + +/-- Word `K` of the SC at `sc` (x86 `scW`). -/ +def scWA (sc K : U64) : U64 := core.num.U64.wrapping_add sc K + +/-- FL4's refill as the spec's (x86 `toRefill`). -/ +def toRefillA (r : sporadic.Refill) : Fl4SpecA64.Refill := ⟨r.time.bv, r.amount.bv⟩ + +/-- The words of the SC at `sc` the relation reads (x86 `ScIn`). -/ +def ScInA (s : S) (sc a : U64) : Prop := sc.val ≤ a.val ∧ a.val < sc.val + 96 + 16 * (L.rmax s sc).val + +/-- The words of the SC object at `sc` of the spec's size `n` (`2 ^ (n + 7)` bytes, l4v `obj_bits`; x86 `ScObj`). -/ +def ScObjA (sc : U64) (n : Nat) (a : U64) : Prop := + sc.val ≤ a.val ∧ a.val < sc.val + 2 ^ (n + Fl4SpecA64.minSchedContextBits) + +/-- **The SC at `sc` and the spec's `.schedContext x n`** (x86 `ScRel1`, + `reply`). -/ +structure ScRel1A (s : S) (sc : U64) (x : Fl4SpecA64.SchedContext) (n : Nat) : Prop where + wf : RefillsWF st s sc (n + Fl4SpecA64.minSchedContextBits) + period : x.scPeriod = (L.rperiod s sc).bv + consumed : x.scConsumed = (L.mem s (scWA sc 8#u64)).bv + tcb : x.scTcb = optRefS (L.mem s (scWA sc 24#u64)) + reply : x.scReplies.head? = optRefS (L.mem s (scWA sc 32#u64)) + ntfn : x.scNtfn = optRefS (L.mem s (scWA sc 40#u64)) + badge : x.scBadge = (L.mem s (scWA sc 48#u64)).bv + yieldFrom : x.scYieldFrom = optRefS (L.mem s (scWA sc 56#u64)) + sporadic : x.scSporadic = decide (L.mem s (scWA sc 64#u64) ≠ 0#u64) + max : (L.rmax s sc).val = x.scRefillMax + refills : x.scRefills = (L.refills s sc).map toRefillA + +/-- **The relation of any SC** (x86 `ScRelW`, + `reply`): the header words, the object placed, `spor` (a sporadic SC +is active), the idle SC (`ii`: it is some core's idle SC, `IdleScA`) configured, and the full `ScRel1A` when active. -/ +structure ScRelWA (ii : Prop) (s : S) (sc : U64) (x : Fl4SpecA64.SchedContext) (n : Nat) : Prop where + pl : Placed sc (n + Fl4SpecA64.minSchedContextBits) + period : x.scPeriod = (L.rperiod s sc).bv + consumed : x.scConsumed = (L.mem s (scWA sc 8#u64)).bv + tcb : x.scTcb = optRefS (L.mem s (scWA sc 24#u64)) + reply : x.scReplies.head? = optRefS (L.mem s (scWA sc 32#u64)) + ntfn : x.scNtfn = optRefS (L.mem s (scWA sc 40#u64)) + badge : x.scBadge = (L.mem s (scWA sc 48#u64)).bv + yieldFrom : x.scYieldFrom = optRefS (L.mem s (scWA sc 56#u64)) + sporadic : x.scSporadic = decide (L.mem s (scWA sc 64#u64) ≠ 0#u64) + spor : L.mem s (scWA sc 64#u64) ≠ 0#u64 → (L.rmax s sc).val ≠ 0 + idle : ii → (L.rmax s sc).val ≠ 0 + max : (L.rmax s sc).val = x.scRefillMax + act : (L.rmax s sc).val ≠ 0 → ScRel1A st s sc x n + +end + +/-! ## The one-object relation (x86 `ScRel`, ported) -/ + +section +variable {S : Type _} {st : machine.Store S} [L : LawfulStore st] + +theorem scWA_val {sc K : U64} (h : sc.val + K.val < 2 ^ 64) : (scWA sc K).val = sc.val + K.val := by + simp [scWA, core.num.U64.wrapping_add_val_eq, U64.size_def, U64.numBits]; omega + +theorem maxA_eqS (sc : U64) : maxA sc = scWA sc 72#u64 := by unfold maxA scWA layout.sc.REFILL_MAX; rfl +theorem headA_eqS (sc : U64) : headA sc = scWA sc 80#u64 := by unfold headA scWA layout.sc.REFILL_HEAD; rfl +theorem tailA_eqS (sc : U64) : tailA sc = scWA sc 88#u64 := by unfold tailA scWA layout.sc.REFILL_TAIL; rfl +theorem periodA_eqS (sc : U64) : periodA sc = scWA sc 0#u64 := by unfold periodA scWA layout.sc.PERIOD; rfl + +theorem ScRel1A.rmax_ne0 {s : S} {sc : U64} {x : Fl4SpecA64.SchedContext} {n : Nat} (h : ScRel1A st s sc x n) : + (L.rmax s sc).val ≠ 0 := by + have := h.wf.min_le; simp at this; omega + +/-- Facts about the placement of a related SC (x86 `ScRel1.bounds`). -/ +theorem ScRel1A.bounds {s : S} {sc : U64} {x : Fl4SpecA64.SchedContext} {n : Nat} (h : ScRel1A st s sc x n) : + sc.val % 8 = 0 ∧ 2 ≤ (L.rmax s sc).val ∧ sc.val + 96 + 16 * (L.rmax s sc).val ≤ 2 ^ 64 := by + obtain ⟨h8, hm, hfit⟩ := h.wf.bounds + have := h.wf.min_le; simp at this + exact ⟨h8, this, by omega⟩ + +/-- A configured SC's relation is the weak one (x86 `ScRel1.toW`). -/ +theorem ScRel1A.toW {ii : Prop} {s : S} {sc : U64} {x : Fl4SpecA64.SchedContext} {n : Nat} (h : ScRel1A st s sc x n) : + ScRelWA st ii s sc x n := + ⟨h.wf.placed, h.period, h.consumed, h.tcb, h.reply, h.ntfn, h.badge, h.yieldFrom, h.sporadic, + fun _ => h.rmax_ne0, fun _ => h.rmax_ne0, h.max, fun _ => h⟩ + +/-- An active SC (`sc_active`): the full relation (x86 `ScRelW.strong`). -/ +theorem ScRelWA.strong {ii : Prop} {s : S} {sc : U64} {x : Fl4SpecA64.SchedContext} {n : Nat} + (h : ScRelWA st ii s sc x n) (ha : Fl4SpecA64.scActive x = true) : ScRel1A st s sc x n := + h.act (by rw [h.max]; intro e; simp [Fl4SpecA64.scActive, Fl4SpecA64.activeSc, e] at ha) + +theorem ScRelWA.active_of_rmax {ii : Prop} {s : S} {sc : U64} {x : Fl4SpecA64.SchedContext} {n : Nat} + (h : ScRelWA st ii s sc x n) (hr : (L.rmax s sc).val ≠ 0) : Fl4SpecA64.scActive x = true := by + unfold Fl4SpecA64.scActive Fl4SpecA64.activeSc; rw [← h.max]; simp; omega + +/-- (x86 `ScRelW.bounds`) -/ +theorem ScRelWA.bounds {ii : Prop} {s : S} {sc : U64} {x : Fl4SpecA64.SchedContext} {n : Nat} + (h : ScRelWA st ii s sc x n) : + sc.val % 8 = 0 ∧ 96 + 16 * (L.rmax s sc).val ≤ 2 ^ (n + Fl4SpecA64.minSchedContextBits) ∧ + sc.val + 96 + 16 * (L.rmax s sc).val ≤ 2 ^ 64 := by + by_cases h0 : (L.rmax s sc).val = 0 + · have h7 : 2 ^ 7 ≤ 2 ^ (n + Fl4SpecA64.minSchedContextBits) := + Nat.pow_le_pow_right (by norm_num) (by simp [Fl4SpecA64.minSchedContextBits]) + have hfit := h.pl.fits + have hal := h.pl.align + have hd : 8 ∣ 2 ^ (n + Fl4SpecA64.minSchedContextBits) := by + have : 2 ^ (n + Fl4SpecA64.minSchedContextBits) = 2 ^ 3 * 2 ^ (n + Fl4SpecA64.minSchedContextBits - 3) := by + rw [← Nat.pow_add]; congr 1; simp [Fl4SpecA64.minSchedContextBits]; omega + rw [this]; exact Dvd.dvd.mul_right (by norm_num) _ + refine ⟨?_, by rw [h0]; omega, by rw [h0]; omega⟩ + rw [← Nat.mod_mod_of_dvd sc.val hd, hal] + · obtain ⟨h8, hm, hfit⟩ := (h.act h0).wf.bounds + exact ⟨h8, hm, by omega⟩ + +/-- A header word (offset `K < 96`, a multiple of 8): aligned and read by the relation (x86 `ScRelW.hdr`). -/ +theorem ScRelWA.hdr {ii : Prop} {s : S} {sc : U64} {x : Fl4SpecA64.SchedContext} {n : Nat} + (h : ScRelWA st ii s sc x n) {K : U64} (hK : K.val < 96) (hK8 : K.val % 8 = 0) : + Aligned (scWA sc K) ∧ ScInA st s sc (scWA sc K) := by + obtain ⟨h8, h2, hfit⟩ := h.bounds + have hv := scWA_val (sc := sc) (K := K) (by omega) + exact ⟨by unfold Aligned; rw [hv]; omega, by unfold ScInA; rw [hv]; omega⟩ + +/-- The words the relation reads lie in the object (x86 `ScRelW.in_obj`). -/ +theorem ScRelWA.in_obj {ii : Prop} {s : S} {sc : U64} {x : Fl4SpecA64.SchedContext} {n : Nat} + (h : ScRelWA st ii s sc x n) {a : U64} (ha : ScInA st s sc a) : ScObjA sc n a := by + obtain ⟨h8, hm, hfit⟩ := h.bounds + unfold ScInA at ha; exact ⟨ha.1, by omega⟩ + +/-- Memory that agrees on the words of the SC at `sc` (x86 `ScAgree`). -/ +def ScAgreeA (st : machine.Store S) [L : LawfulStore st] (s s' : S) (sc : U64) : Prop := + ∀ a, Aligned a → ScInA st s sc a → L.mem s' a = L.mem s a + +/-- Frame the ring and unchanged header words when the core, badge and +sporadic flag may be rewritten. -/ +theorem ScRel1A.cfgWords {s s' : S} {sc : U64} {x : Fl4SpecA64.SchedContext} {n : Nat} (h : ScRel1A st s sc x n) + (ha : ∀ a, Aligned a → ScInA st s sc a → a.val ≠ sc.val + 16 → a.val ≠ sc.val + 48 → + a.val ≠ sc.val + 64 → L.mem s' a = L.mem s a) (badge : Fl4SpecA64.Word64) (sporadic : Bool) + (hb : badge = (L.mem s' (scWA sc 48#u64)).bv) + (hs : sporadic = decide (L.mem s' (scWA sc 64#u64) ≠ 0#u64)) : + ScRel1A st s' sc { x with scBadge := badge, scSporadic := sporadic } n := by + obtain ⟨h8, h2, hfit⟩ := h.bounds + have hw : ∀ K : U64, K.val < 96 → K.val % 8 = 0 → K.val ≠ 16 → K.val ≠ 48 → K.val ≠ 64 → + L.mem s' (scWA sc K) = L.mem s (scWA sc K) := by + intro K h1 h2 h16 h48 h64 + have hA := (h.toW (ii := True)).hdr h1 h2 + apply ha _ hA.1 hA.2 <;> rw [scWA_val (by omega)] <;> omega + have em : L.rmax s' sc = L.rmax s sc := by + simp only [LawfulStore.rmax, maxA_eqS]; exact hw _ (by simp) (by simp) (by simp) (by simp) (by simp) + have eh : L.rhead s' sc = L.rhead s sc := by + simp only [LawfulStore.rhead, headA_eqS]; exact hw _ (by simp) (by simp) (by simp) (by simp) (by simp) + have et : L.rtail s' sc = L.rtail s sc := by + simp only [LawfulStore.rtail, tailA_eqS]; exact hw _ (by simp) (by simp) (by simp) (by simp) (by simp) + have ep : L.rperiod s' sc = L.rperiod s sc := by + simp only [LawfulStore.rperiod, periodA_eqS]; exact hw _ (by simp) (by simp) (by simp) (by simp) (by simp) + have hW' : RefillsWF st s' sc (n + Fl4SpecA64.minSchedContextBits) := + h.wf.of_hdr em (by rw [eh]; exact h.wf.head_lt) (by rw [et]; exact h.wf.tail_lt) + have hsz : L.size s' sc = L.size s sc := by simp only [LawfulStore.size, em, eh, et] + have hrf : ∀ k, L.rf s' sc k = L.rf s sc k := by + intro k + have hix : L.ix s' sc k = L.ix s sc k := by simp only [LawfulStore.ix, em, eh] + have hk := h.wf.ix_lt k + obtain ⟨-, -, -, -, -, hin⟩ := h.wf.inside + obtain ⟨h1, h2', h3⟩ := hin _ hk + unfold LawfulStore.rf LawfulStore.refill + rw [hix, ha _ (by unfold Aligned; rw [h1]; omega) (by unfold ScInA; rw [h1]; omega) + (by rw [h1]; omega) (by rw [h1]; omega) (by rw [h1]; omega), + ha _ (by unfold Aligned; rw [h2']; omega) (by unfold ScInA; rw [h2']; omega) + (by rw [h2']; omega) (by rw [h2']; omega) (by rw [h2']; omega)] + have hrs : L.refills s' sc = L.refills s sc := by + simp only [LawfulStore.refills, hsz] + exact List.map_congr_left (fun k _ => hrf k) + exact ⟨hW', by rw [ep]; exact h.period, by rw [hw _ (by simp) (by simp) (by simp) (by simp) (by simp)]; exact h.consumed, + by rw [hw _ (by simp) (by simp) (by simp) (by simp) (by simp)]; exact h.tcb, by rw [hw _ (by simp) (by simp) (by simp) (by simp) (by simp)]; exact h.reply, + by rw [hw _ (by simp) (by simp) (by simp) (by simp) (by simp)]; exact h.ntfn, + hb, by rw [hw _ (by simp) (by simp) (by simp) (by simp) (by simp)]; exact h.yieldFrom, + hs, by rw [em]; exact h.max, by rw [hrs]; exact h.refills⟩ + +/-- The full relation only reads `ScInA` (x86 `ScRel1.agree`). -/ +theorem ScRel1A.agree {s s' : S} {sc : U64} {x : Fl4SpecA64.SchedContext} {n : Nat} (h : ScRel1A st s sc x n) + (ha : ScAgreeA st s s' sc) : ScRel1A st s' sc x n := by + have h8 := h.toW (ii := True) + have b := h8.hdr (K := 48#u64) (by simp) (by simp) + have q := h8.hdr (K := 64#u64) (by simp) (by simp) + have r := h.cfgWords (fun a ha' hA _ _ _ => ha a ha' hA) x.scBadge x.scSporadic + (by rw [ha _ b.1 b.2]; exact h.badge) + (by rw [ha _ q.1 q.2]; exact h.sporadic) + cases x + exact r + +/-- The weak relation only reads `ScInA` (x86 `ScRelW.agree`). -/ +theorem ScRelWA.agree {ii : Prop} {s s' : S} {sc : U64} {x : Fl4SpecA64.SchedContext} {n : Nat} + (h : ScRelWA st ii s sc x n) (ha : ScAgreeA st s s' sc) : ScRelWA st ii s' sc x n := by + have hw : ∀ K : U64, K.val < 96 → K.val % 8 = 0 → L.mem s' (scWA sc K) = L.mem s (scWA sc K) := + fun K h1 h2 => ha _ (h.hdr h1 h2).1 (h.hdr h1 h2).2 + have em : L.rmax s' sc = L.rmax s sc := by + simp only [LawfulStore.rmax, maxA_eqS]; exact hw _ (by simp) (by simp) + have ep : L.rperiod s' sc = L.rperiod s sc := by + simp only [LawfulStore.rperiod, periodA_eqS]; exact hw _ (by simp) (by simp) + exact ⟨h.pl, by rw [ep]; exact h.period, by rw [hw _ (by simp) (by simp)]; exact h.consumed, + by rw [hw _ (by simp) (by simp)]; exact h.tcb, by rw [hw _ (by simp) (by simp)]; exact h.reply, + by rw [hw _ (by simp) (by simp)]; exact h.ntfn, + by rw [hw _ (by simp) (by simp)]; exact h.badge, by rw [hw _ (by simp) (by simp)]; exact h.yieldFrom, + by rw [hw _ (by simp) (by simp)]; exact h.sporadic, + fun h0 => by rw [em]; exact h.spor (by rwa [hw _ (by simp) (by simp)] at h0), + fun h0 => by rw [em]; exact h.idle h0, by rw [em]; exact h.max, + fun h0 => (h.act (by rwa [em] at h0)).agree ha⟩ + +/-- **After a ring step** (x86 `ScRel1.ring`): the header words below the ring indices kept and the ring well formed, +the relation holds of the SC with FL4's new refills. -/ +theorem ScRel1A.ring {s s' : S} {sc : U64} {x : Fl4SpecA64.SchedContext} {n : Nat} (h : ScRel1A st s sc x n) + (hW : RefillsWF st s' sc (n + Fl4SpecA64.minSchedContextBits)) + (hw : ∀ K : U64, K.val < 80 → K.val % 8 = 0 → L.mem s' (scWA sc K) = L.mem s (scWA sc K)) : + ScRel1A st s' sc { x with scRefills := (L.refills s' sc).map toRefillA } n := by + have ep : L.rperiod s' sc = L.rperiod s sc := by + simp only [LawfulStore.rperiod, periodA_eqS]; exact hw _ (by simp) (by simp) + have em : L.rmax s' sc = L.rmax s sc := by + simp only [LawfulStore.rmax, maxA_eqS]; exact hw _ (by simp) (by simp) + exact ⟨hW, by rw [ep]; exact h.period, by rw [hw _ (by simp) (by simp)]; exact h.consumed, + by rw [hw _ (by simp) (by simp)]; exact h.tcb, by rw [hw _ (by simp) (by simp)]; exact h.reply, + by rw [hw _ (by simp) (by simp)]; exact h.ntfn, + by rw [hw _ (by simp) (by simp)]; exact h.badge, by rw [hw _ (by simp) (by simp)]; exact h.yieldFrom, + by rw [hw _ (by simp) (by simp)]; exact h.sporadic, by rw [em]; exact h.max, rfl⟩ + +/-- **Writing the `CONSUMED` word** (x86 `SchedRel.set_sc_consumed`'s SC part, `ScRelW.wr_hdr` at `K = 8`). -/ +theorem ScRelWA.set_consumed {ii : Prop} {s : S} {sc : U64} {x : Fl4SpecA64.SchedContext} {n : Nat} + (h : ScRelWA st ii s sc x n) (v : U64) : + ScRelWA st ii (L.wr s (scWA sc 8#u64) v) sc { x with scConsumed := v.bv } n ∧ + WritesIn st s (L.wr s (scWA sc 8#u64) v) (ScObjA sc n) := by + obtain ⟨h8, h2, hfit⟩ := h.bounds + have ha := h.hdr (K := 8#u64) (by simp) (by simp) + have hv8 : (scWA sc 8#u64).val = sc.val + 8 := by + have := scWA_val (sc := sc) (K := 8#u64) (by simp; omega); simpa using this + set s' := L.wr s (scWA sc 8#u64) v + have hw : ∀ K : U64, K.val < 96 → K.val % 8 = 0 → K.val ≠ 8 → L.mem s' (scWA sc K) = L.mem s (scWA sc K) := by + intro K h1 h2 h3 + refine L.mem_wr_ne ha.1 (h.hdr h1 h2).1 ?_ + rw [hv8, scWA_val (sc := sc) (K := K) (by omega)]; omega + have hww : WritesIn st s s' (ScObjA sc n) := + (writesIn_wr ha.1).mono fun b e => by subst e; exact h.in_obj ha.2 + have em : L.rmax s' sc = L.rmax s sc := by + simp only [LawfulStore.rmax, maxA_eqS]; exact hw _ (by simp) (by simp) (by simp) + have ep : L.rperiod s' sc = L.rperiod s sc := by + simp only [LawfulStore.rperiod, periodA_eqS]; exact hw _ (by simp) (by simp) (by simp) + have eh : L.rhead s' sc = L.rhead s sc := by + simp only [LawfulStore.rhead, headA_eqS]; exact hw _ (by simp) (by simp) (by simp) + have et : L.rtail s' sc = L.rtail s sc := by + simp only [LawfulStore.rtail, tailA_eqS]; exact hw _ (by simp) (by simp) (by simp) + have e8 : L.mem s' (scWA sc 8#u64) = v := L.mem_wr_same ha.1 + refine ⟨⟨h.pl, by rw [ep]; exact h.period, by rw [e8], by rw [hw _ (by simp) (by simp) (by simp)]; exact h.tcb, + by rw [hw _ (by simp) (by simp) (by simp)]; exact h.reply, by rw [hw _ (by simp) (by simp) (by simp)]; exact h.ntfn, + by rw [hw _ (by simp) (by simp) (by simp)]; exact h.badge, + by rw [hw _ (by simp) (by simp) (by simp)]; exact h.yieldFrom, + by rw [hw _ (by simp) (by simp) (by simp)]; exact h.sporadic, + fun h0 => by rw [em]; exact h.spor (by rwa [hw _ (by simp) (by simp) (by simp)] at h0), + fun h0 => by rw [em]; exact h.idle h0, by rw [em]; exact h.max, fun h0 => ?_⟩, hww⟩ + have h1 := h.act (by rwa [em] at h0) + have hW' : RefillsWF st s' sc (n + Fl4SpecA64.minSchedContextBits) := + h1.wf.of_hdr em (by rw [eh]; exact h1.wf.head_lt) (by rw [et]; exact h1.wf.tail_lt) + have hrs : L.refills s' sc = L.refills s sc := by + obtain ⟨-, -, -, -, -, hin⟩ := h1.wf.inside + have hsz : L.size s' sc = L.size s sc := by simp only [LawfulStore.size, em, eh, et] + simp only [LawfulStore.refills, hsz] + refine List.map_congr_left (fun k _ => ?_) + have hix : L.ix s' sc k = L.ix s sc k := by simp only [LawfulStore.ix, em, eh] + obtain ⟨g1, g2, g3⟩ := hin _ (h1.wf.ix_lt k) + unfold LawfulStore.rf LawfulStore.refill + rw [hix, L.mem_wr_ne ha.1 (by unfold Aligned; rw [g1]; omega) (by rw [g1, hv8]; omega), + L.mem_wr_ne ha.1 (by unfold Aligned; rw [g2]; omega) (by rw [g2, hv8]; omega)] + exact ⟨hW', by rw [ep]; exact h.period, by rw [e8], by rw [hw _ (by simp) (by simp) (by simp)]; exact h.tcb, + by rw [hw _ (by simp) (by simp) (by simp)]; exact h.reply, by rw [hw _ (by simp) (by simp) (by simp)]; exact h.ntfn, + by rw [hw _ (by simp) (by simp) (by simp)]; exact h.badge, + by rw [hw _ (by simp) (by simp) (by simp)]; exact h.yieldFrom, + by rw [hw _ (by simp) (by simp) (by simp)]; exact h.sporadic, by rw [em]; exact h.max, + by rw [hrs]; exact h1.refills⟩ + +end + +/-! ## The state relation and its placement -/ + +section +variable {S : Type _} (st : machine.Store S) [L : LawfulStore st] + +/-- **The scheduling-context relation** (module docstring; x86 `SchedSpec.scs`). -/ +structure ScStateRelA (C : Set U64) (m : Fl4SpecA64.McState) (s : S) (σ : Fl4SpecA64.State Unit) : Prop where + sc : ∀ c ∈ C, ∃ x n, σ.kheap (scNameA m c) = some (.schedContext x n) ∧ + ScRelWA st (IdleScA m c) s c x n + +end + +/-- **The placement of the SCs** (module docstring; x86 `SchedRel.scOut` / `scDisj`). -/ +structure ScPlacedA (th : TypedHeapA) (ksb : U64) (C : Set U64) (m : Fl4SpecA64.McState) + (σ : Fl4SpecA64.State Unit) : Prop where + /-- Distinct SCs have distinct spec names (JointA's `SchedRelA.scOff` with `scRenA_inj`). -/ + inj : ∀ c ∈ C, ∀ c' ∈ C, scNameA m c = scNameA m c' → c = c' + /-- Distinct SC objects do not overlap (x86 `scDisj`). -/ + disj : ∀ c ∈ C, ∀ c' ∈ C, c ≠ c' → ∀ x n x' n', σ.kheap (scNameA m c) = some (.schedContext x n) → + σ.kheap (scNameA m c') = some (.schedContext x' n') → ∀ a, ScObjA c n a → ¬ ScObjA c' n' a + /-- An SC object lies off every object of `th` and off the kernel-state block (x86 `scOut`). -/ + out : ∀ c ∈ C, ∀ x n, σ.kheap (scNameA m c) = some (.schedContext x n) → ∀ a, ScObjA c n a → + (∀ p k, th p = some k → a.val < p.val ∨ p.val + k.size ≤ a.val) ∧ + (a.val < ksb.val ∨ ksb.val + ksSizeA ≤ a.val) + +/-- **The SC footprint** (x86 `KFoot`'s SC arm): the words of the related SC objects. -/ +def ScFootA (C : Set U64) (m : Fl4SpecA64.McState) (σ : Fl4SpecA64.State Unit) (a : U64) : Prop := + ∃ c ∈ C, ∃ x n, σ.kheap (scNameA m c) = some (.schedContext x n) ∧ ScObjA c n a + +/-- The state with object `o` at `p` (x86 `updK`). -/ +def updKSc (σ : Fl4SpecA64.State Unit) (p : Fl4SpecA64.ObjRef) (o : Fl4SpecA64.KernelObject) : + Fl4SpecA64.State Unit := + { σ with kheap := Function.update σ.kheap p (some o) } + +@[simp] theorem updKSc_kheap (σ : Fl4SpecA64.State Unit) (p : Fl4SpecA64.ObjRef) (o : Fl4SpecA64.KernelObject) : + (updKSc σ p o).kheap = Function.update σ.kheap p (some o) := rfl + +section +variable {S : Type _} {st : machine.Store S} [L : LawfulStore st] + +/-- **The frame of the SC relation**: an FL4 step that keeps the SC words and a spec step that keeps the SC objects +keep it. -/ +theorem ScStateRelA.frame {C : Set U64} {m : Fl4SpecA64.McState} {s s' : S} {σ σ' : Fl4SpecA64.State Unit} + (h : ScStateRelA st C m s σ) (hm : ∀ a, Aligned a → ScFootA C m σ a → L.mem s' a = L.mem s a) + (hk : ∀ c ∈ C, σ'.kheap (scNameA m c) = σ.kheap (scNameA m c)) : ScStateRelA st C m s' σ' := by + refine ⟨fun c hc => ?_⟩ + obtain ⟨x, n, hkx, hx⟩ := h.sc c hc + exact ⟨x, n, by rw [hk c hc]; exact hkx, hx.agree fun a ha hin => hm a ha ⟨c, hc, x, n, hkx, hx.in_obj hin⟩⟩ + +/-- The frame, from a write set off the SC footprint. -/ +theorem ScStateRelA.frameW {C : Set U64} {m : Fl4SpecA64.McState} {s s' : S} {σ σ' : Fl4SpecA64.State Unit} + (h : ScStateRelA st C m s σ) {W : U64 → Prop} (hw : WritesIn st s s' W) (hW : ∀ a, W a → ¬ ScFootA C m σ a) + (hk : ∀ c ∈ C, σ'.kheap (scNameA m c) = σ.kheap (scNameA m c)) : ScStateRelA st C m s' σ' := + h.frame (fun a ha hf => hw.mem_eq ha fun hw' => hW a hw' hf) hk + +/-- The placement only reads the SC objects' sizes. -/ +theorem ScPlacedA.of_kheap {th : TypedHeapA} {ksb : U64} {C : Set U64} {m : Fl4SpecA64.McState} + {σ σ' : Fl4SpecA64.State Unit} (P : ScPlacedA th ksb C m σ) + (hk : ∀ c ∈ C, σ'.kheap (scNameA m c) = σ.kheap (scNameA m c)) : ScPlacedA th ksb C m σ' := + ⟨P.inj, fun c hc c' hc' hne x n x' n' h1 h2 => P.disj c hc c' hc' hne x n x' n' (by rw [← hk c hc]; exact h1) + (by rw [← hk c' hc']; exact h2), + fun c hc x n h1 => P.out c hc x n (by rw [← hk c hc]; exact h1)⟩ + +/-- The footprint only reads the SC objects' sizes. -/ +theorem ScFootA.of_kheap {C : Set U64} {m : Fl4SpecA64.McState} {σ σ' : Fl4SpecA64.State Unit} {a : U64} + (h : ScFootA C m σ a) (hk : ∀ c ∈ C, σ'.kheap (scNameA m c) = σ.kheap (scNameA m c)) : ScFootA C m σ' a := by + obtain ⟨c, hc, x, n, h1, h2⟩ := h + exact ⟨c, hc, x, n, by rw [hk c hc]; exact h1, h2⟩ + +/-- **An SC word is off every object of `th`** (so off every TCB and every CNode slot). -/ +theorem ScPlacedA.th_out {th : TypedHeapA} {ksb : U64} {C : Set U64} {m : Fl4SpecA64.McState} + {σ : Fl4SpecA64.State Unit} (P : ScPlacedA th ksb C m σ) {a : U64} (ha : ScFootA C m σ a) {p : U64} + {k : ObjKindA} (hp : th p = some k) : a.val < p.val ∨ p.val + k.size ≤ a.val := by + obtain ⟨c, hc, x, n, h1, h2⟩ := ha + exact (P.out c hc x n h1 a h2).1 p k hp + +/-- **An SC word is off the kernel-state block** (so off every node word). -/ +theorem ScPlacedA.ks_out {th : TypedHeapA} {ksb : U64} {C : Set U64} {m : Fl4SpecA64.McState} + {σ : Fl4SpecA64.State Unit} (P : ScPlacedA th ksb C m σ) {a : U64} (ha : ScFootA C m σ a) : + a.val < ksb.val ∨ ksb.val + ksSizeA ≤ a.val := by + obtain ⟨c, hc, x, n, h1, h2⟩ := ha + exact (P.out c hc x n h1 a h2).2 + +/-- **One SC updated** (x86 `SchedRel.sc_updateW`'s SC part): FL4 writes inside the object of `c ∈ C` only, the +spec's object at `scNameA m c` is replaced by one of the same size related to the new state; the relation and the +placement hold after. -/ +theorem ScStateRelA.upd {th : TypedHeapA} {ksb : U64} {C : Set U64} {m : Fl4SpecA64.McState} {s s' : S} + {σ : Fl4SpecA64.State Unit} (h : ScStateRelA st C m s σ) (P : ScPlacedA th ksb C m σ) {c : U64} (hc : c ∈ C) + {x x' : Fl4SpecA64.SchedContext} {n : Nat} (hk : σ.kheap (scNameA m c) = some (.schedContext x n)) + (hw : WritesIn st s s' (ScObjA c n)) (hx' : ScRelWA st (IdleScA m c) s' c x' n) : + ScStateRelA st C m s' (updKSc σ (scNameA m c) (.schedContext x' n)) ∧ + ScPlacedA th ksb C m (updKSc σ (scNameA m c) (.schedContext x' n)) := by + have hne : ∀ c' ∈ C, c' ≠ c → scNameA m c' ≠ scNameA m c := fun c' hc' h1 e => h1 (P.inj c' hc' c hc e) + have hkk : ∀ c' ∈ C, (updKSc σ (scNameA m c) (.schedContext x' n)).kheap (scNameA m c') = + if c' = c then some (.schedContext x' n) else σ.kheap (scNameA m c') := by + intro c' hc' + by_cases e : c' = c + · subst e; simp + · rw [if_neg e]; simp [Function.update_of_ne (hne c' hc' e)] + refine ⟨⟨fun c' hc' => ?_⟩, ⟨P.inj, fun c1 h1 c2 h2 h12 y1 n1 y2 n2 k1 k2 a a1 a2 => ?_, + fun c1 h1 y1 n1 k1 a a1 => ?_⟩⟩ + · rw [hkk c' hc'] + by_cases e : c' = c + · subst e; rw [if_pos rfl]; exact ⟨x', n, rfl, hx'⟩ + · rw [if_neg e] + obtain ⟨y, n', hky, hy⟩ := h.sc c' hc' + refine ⟨y, n', hky, hy.agree fun a ha hin => hw.mem_eq ha fun hc0 => ?_⟩ + exact P.disj c' hc' c hc e y n' x n hky hk a (hy.in_obj hin) hc0 + · have back : ∀ c0 ∈ C, ∀ y0 n0, (updKSc σ (scNameA m c) (.schedContext x' n)).kheap (scNameA m c0) = + some (.schedContext y0 n0) → ∃ z, σ.kheap (scNameA m c0) = some (.schedContext z n0) := by + intro c0 hc0 y0 n0 hk0 + rw [hkk c0 hc0] at hk0 + by_cases e : c0 = c + · subst e; rw [if_pos rfl] at hk0; cases hk0; exact ⟨x, hk⟩ + · rw [if_neg e] at hk0; exact ⟨y0, hk0⟩ + obtain ⟨z1, g1⟩ := back c1 h1 y1 n1 k1 + obtain ⟨z2, g2⟩ := back c2 h2 y2 n2 k2 + exact P.disj c1 h1 c2 h2 h12 z1 n1 z2 n2 g1 g2 a a1 a2 + · rw [hkk c1 h1] at k1 + by_cases e : c1 = c + · subst e; rw [if_pos rfl] at k1; cases k1; exact P.out c1 h1 x n hk a a1 + · rw [if_neg e] at k1; exact P.out c1 h1 y1 n1 k1 a a1 + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScRelEnqA.lean b/proofs/Fl4ProofsA64/P3/ScRelEnqA.lean new file mode 100644 index 000000000..52bdcf873 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScRelEnqA.lean @@ -0,0 +1,352 @@ +/- +P3 (aarch64, A64-P3-MCS gen 6): **the release queue** (ruling 23:44Z gen-6 item 1; x86 `P3/ScRelease`, ported). + +* **`core_release_step`** (A64 P1 `inv_release_step`, its queue part, over `SchedCoreA`) and + **`sched.release_enqueue.coreA`** (P1 `sched.release_enqueue.inv` + `.foot`): `release_enqueue t` keeps + `SchedCoreA` with `t` inserted into its core's release list by `relIns`, writing inside `QFoot … RelOffs`. +* **`specInsTA`** / **`readyTimesAppend_eqA`** / **`tcbReleaseEnqueue_eqA`** (x86 `specInsT` / `readyTimesAppend_eq` / + `tcbReleaseEnqueue_eq`): the spec's `tcb_release_enqueue` is the ordered insertion by ready time. +* **`SchedSRN.readyTime`** (x86 `readyTime_rel`): a TCB of `Z.T` bound to an active SC has the spec ready time of + FL4's `ready_time`. +* **`release_enqueue_refinesA`** (x86 `release_enqueue_refines`): on `SchedSRN` (the release queue sorted and its + members bound to active SCs by `ScInvA`), FL4's `release_enqueue t` of a local thread in no queue bound to an active + SC refines `tcb_release_enqueue t` up to `reprogram_timer`; the spec's queue is FL4's new release list. + +No named premise. Hypotheses: `ht`, `hon` (the local case), `h0` / `hq0` / `hr0` / `hta` (x86's: an SC, in no queue, +active). +-/ +import Fl4ProofsA64.P3.ScDonRefA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +/-! ## `SchedCoreA` across a release-queue step -/ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {T Cs : Set U64} {s : S} + {Q : U64 → U64 → List U64} {R : U64 → List U64} + +/-- **`SchedCoreA` after an operation that replaces the release list of core `c` by `ys'`** (A64 P1 +`inv_release_step`, the queue part). -/ +theorem core_release_step {s' : S} {t c : U64} {W ys' : List U64} {tIn : Prop} + (h : SchedCoreA st T Cs s Q R) (hcur : L.cpu s ∈ Cs) (ht : t ∈ T) (hca : L.mem s (affA t) = c) + (hW1 : ∀ w ∈ W, w = t ∨ w ∈ R c) (hWt : t ∈ W → ∀ c' ∈ Cs, ∀ p' : U64, p'.val < 256 → t ∉ Q c' p') + (hw : WritesIn st s s' (L.QFoot s W c RelOffs t)) + (hRc : QueueRel (L.mem s') T .Sched (L.relQ s' c) ys') + (hmem : ∀ y, y ∈ ys' ↔ (y = t ∧ tIn) ∨ (y ≠ t ∧ y ∈ R c)) + (hqr : (L.mem s' (inqA t) &&& sched.Q_RELEASE) ≠ 0#u64 ↔ tIn) + (hqq : L.mem s' (inqA t) &&& sched.Q_QUEUED = L.mem s (inqA t) &&& sched.Q_QUEUED) + (hnq : tIn → t ∉ Q c (L.mem s (prioA t))) : + SchedCoreA st T Cs s' Q (Function.update R c ys') := by + have hT := h.tcbs + have hc : c ∈ Cs := hca ▸ h.aff t ht + have hN := h.nodes c hc + have hN' := h.nodes _ hcur + have hW : ∀ w ∈ W, w ∈ T := fun w hw' => by + rcases hW1 w hw' with rfl | hq + · exact ht + · exact h.memTR hc hq + have haff : ∀ y ∈ T, L.mem s' (affA y) = L.mem s (affA y) := fun y hy => aff_qfoot hT hW hN hN' ht hw hy + have hprio : ∀ y ∈ T, L.mem s' (prioA y) = L.mem s (prioA y) := fun y hy => prio_qfoot hT hW hN hN' ht hw hy + have hinq : ∀ y ∈ T, y ≠ t → L.mem s' (inqA y) = L.mem s (inqA y) := + fun y hy hyt => inq_qfoot hT hW hN hN' ht hw hy hyt + have hWQ : ∀ c' ∈ Cs, ∀ p' : U64, p'.val < 256 → ∀ y ∈ Q c' p', y ∉ W := by + intro c' hc' p' hp' y hy hyW + rcases hW1 y hyW with rfl | hq + · exact hWt hyW c' hc' p' hp' hy + · exact h.QR hc' hp' hy hc hq + have hWR : ∀ c' ∈ Cs, c' ≠ c → ∀ y ∈ R c', y ∉ W := by + intro c' hc' hne y hy hyW + have e := h.inR c' hc' y hy + rcases hW1 y hyW with rfl | hq + · exact hne (e.symm.trans hca) + · exact hne (e.symm.trans (h.inR c hc y hq)) + refine ⟨hT, fun c' hc' => (h.nodes c' hc').ksb hw.1, fun y hy => (haff y hy) ▸ h.aff y hy, + fun y hy => (hprio y hy) ▸ h.prio y hy, fun c' hc' => ?_, fun c' hc' => ?_, ?_, ?_, ?_, ?_, ?_⟩ + · exact (h.ready c' hc').qfoot hT hW hN hN' ht hw (h.nodes c' hc') + (fun _ o ho _ => by simp only [RelOffs]; omega) (hWQ c' hc') + · by_cases e : c' = c + · subst e; rw [Function.update_self]; exact hRc + · rw [Function.update_of_ne e, relQ_qfoot hT hW hN hN' ht hw (h.nodes c' hc') (fun e' => absurd e' e)] + exact (h.release c' hc').qfoot hT hW hN hN' ht hw (fun _ => hWR c' hc' e) + · intro c' hc' p' hp' y hy + have := h.inQ c' hc' p' hp' y hy + exact ⟨(haff y (h.memT hc' hp' hy)).trans this.1, (hprio y (h.memT hc' hp' hy)).trans this.2⟩ + · intro c' hc' y hy + rcases mem_upd1.1 hy with ⟨rfl, hy'⟩ | ⟨-, hy'⟩ + · rcases (hmem y).1 hy' with ⟨rfl, -⟩ | ⟨-, hq⟩ + · exact (haff y ht).trans hca + · exact (haff y (h.memTR hc hq)).trans (h.inR c' hc y hq) + · exact (haff y (h.memTR hc' hy')).trans (h.inR c' hc' y hy') + · intro y hy + rw [haff y hy, hprio y hy] + by_cases e : y = t + · subst e; rw [hqq]; exact h.queued y hy + · rw [hinq y hy e]; exact h.queued y hy + · intro y hy + rw [haff y hy] + by_cases e : y = t + · subst e + rw [hqr, hca, Function.update_self, hmem] + simp + · rw [hinq y hy e, h.released y hy, mem_upd1] + constructor + · intro hq + by_cases e2 : L.mem s (affA y) = c + · left; refine ⟨e2, (hmem y).2 (Or.inr ⟨e, ?_⟩)⟩ + rw [← e2]; exact hq + · exact Or.inr ⟨e2, hq⟩ + · rintro (⟨e3, hq⟩ | ⟨-, hq⟩) + · rcases (hmem y).1 hq with ⟨e5, -⟩ | ⟨-, hq'⟩ + · exact absurd e5 e + · rw [e3]; exact hq' + · exact hq + · intro y hy hq hr + rw [haff y hy, hprio y hy] at hq + rw [haff y hy] at hr + rcases mem_upd1.1 hr with ⟨e3, hr'⟩ | ⟨-, hr'⟩ + · rcases (hmem y).1 hr' with ⟨rfl, hin⟩ | ⟨-, hr''⟩ + · rw [hca] at hq; exact hnq hin hq + · exact h.excl y hy hq (by rw [e3]; exact hr'') + · exact h.excl y hy hq hr' + +/-- **`release_enqueue` keeps `SchedCoreA`** (A64 P1 `sched.release_enqueue.inv` + `.foot`), for a thread in no +queue: its core's release list becomes `relIns` of the ready times. -/ +theorem sched.release_enqueue.coreA (t : obj.Tcb) (h : SchedCoreA st T Cs s Q R) (hcur : L.cpu s ∈ Cs) + (ht : t ∈ T) (hq0 : (L.mem s (inqA t) &&& sched.Q_QUEUED) = 0#u64) + (hr0 : (L.mem s (inqA t) &&& sched.Q_RELEASE) = 0#u64) : + sched.release_enqueue st s t ⦃ s' => SchedCoreA st T Cs s' Q (Function.update R (L.mem s (affA t)) + (relIns (L.rtime s) (L.rtime s t) t (R (L.mem s (affA t))))) ∧ + WritesIn st s s' (L.QFootS s (t :: R (L.mem s (affA t))) (L.mem s (affA t)) RelOffs t) ∧ + L.mem s' (inqA t) = L.mem s (inqA t) ||| sched.Q_RELEASE ⦄ := by + have hT := h.tcbs + have hc := h.aff t ht + have hN := h.nodes _ hc + have hN' := h.nodes _ hcur + have hn := h.notR ht hr0 hc + have h1 := sched.release_enqueue.rel s t _ hT ht hn hN hN' (h.release _ hc) + have h2 := sched.release_enqueue.footS s t _ hT ht hn hN hN' (h.release _ hc) + refine WP.spec_mono (WP.spec_and h1 h2) ?_ + rintro s' ⟨hRc, hwS, hinq⟩ + have hw := hwS.mono fun _ h => h.toQFoot + refine ⟨core_release_step (W := t :: R _) (tIn := True) h hcur ht rfl (fun w hw => List.mem_cons.1 hw) + (fun _ c' hc' p' hp' => h.notQ ht hq0 hc' hp') hw hRc + (fun y => by rw [mem_relIns]; by_cases e : y = t <;> simp [e]) ?_ ?_ + (fun _ => h.notQ ht hq0 hc (h.prio t ht)), hwS, hinq⟩ + · rw [hinq, or_qr_qr]; simpa using qr_ne + · rw [hinq, or_qr_qq] + +end + +/-! ## The spec's `tcb_release_enqueue` (x86 `ScRelease`, spec half) -/ + +section Spec +open Fl4SpecA64 Fl4Spec + +theorem nd_bind_one'Sc {σ α β : Type} {m : NonDet σ α} {s s' : σ} {a : α} (h : m s = ({(a, s')}, False)) + (f : α → NonDet σ β) : NonDet.bind m f s = f a s' := nd_bind_oneSc h f + +theorem getsThe_someSc {α : Type} {σ : State Unit} {f : State Unit → Option α} {a : α} (h : f σ = some a) : + NonDet.getsThe f σ = ({(a, σ)}, False) := by + simp [NonDet.getsThe, h, NonDet.assertOpt]; rfl + +theorem mapM_oneSc {α β : Type} {σ : State Unit} (f : α → NonDet (State Unit) β) (g : α → β) : + ∀ ys : List α, (∀ y ∈ ys, f y σ = ({(g y, σ)}, False)) → NonDet.mapM f ys σ = ({(ys.map g, σ)}, False) + | [], _ => rfl + | y :: ys, h => by + unfold NonDet.mapM + rw [nd_bind_one'Sc (h y List.mem_cons_self), + nd_bind_one'Sc (mapM_oneSc f g ys fun z hz => h z (List.mem_cons_of_mem _ hz))] + rfl + +theorem zip_takeWhileSc {α β : Type} (g : α → β) (p : α × β → Bool) : + ∀ ts : List α, ((ts.zip (ts.map g)).takeWhile p).map Prod.fst = ts.takeWhile (fun y => p (y, g y)) + | [] => rfl + | a :: rs => by + simp only [List.map_cons, List.zip_cons_cons, List.takeWhile_cons] + split <;> simp [zip_takeWhileSc g p rs] + +theorem zip_dropWhileSc {α β : Type} (g : α → β) (p : α × β → Bool) : + ∀ ts : List α, ((ts.zip (ts.map g)).dropWhile p).map Prod.fst = ts.dropWhile (fun y => p (y, g y)) + | [] => rfl + | a :: rs => by + simp only [List.map_cons, List.zip_cons_cons, List.dropWhile_cons] + split + · exact zip_dropWhileSc g p rs + · rw [List.map_cons, List.map_fst_zip (by simp)] + +/-- The spec's ordered insertion of `t` (ready time `g t`) into `qs` by the ready times `g` (x86 `specInsT`). -/ +def specInsTA (qs : List ObjRef) (g : ObjRef → Time) (t : ObjRef) : List ObjRef := + qs.takeWhile (fun y => @decide (g y ≤ g t) (Classical.propDecidable _)) ++ + t :: qs.dropWhile (fun y => @decide (g y ≤ g t) (Classical.propDecidable _)) + +theorem readyTimesAppend_eqA {σ : State Unit} (t : ObjRef) (qs : List ObjRef) (g : ObjRef → Time) + (ht : readTcbReadyTime (Z := Unit) t σ = some (g t)) + (hq : ∀ y ∈ qs, readTcbReadyTime (Z := Unit) y σ = some (g y)) : + readyTimesAppend (Z := Unit) t qs σ = ({(specInsTA qs g t, σ)}, False) := by + unfold readyTimesAppend orderedInsert + rw [nd_bind_oneSc (getsThe_someSc ht), + nd_bind_oneSc (mapM_oneSc (fun t' => NonDet.getsThe (readTcbReadyTime (Z := Unit) t')) g qs + (fun y hy => getsThe_someSc (hq y hy))), + nd_pure_bindSc, nd_pure_bindSc] + show ({(_, σ)}, False) = _ + congr + simp only [List.map_append, List.map_cons, zip_takeWhileSc, zip_dropWhileSc, List.append_assoc, + List.singleton_append, specInsTA] + +/-- The spec state with the release queue replaced and `reprogram_timer` set to `b` (x86 `setRel`). -/ +def setRelA (σ : State Unit) (q : List ObjRef) (b : Bool) : State Unit := + { σ with releaseQueue := q, reprogramTimer := b } + +theorem tcbReleaseEnqueue_eqA {σ : State Unit} (t : ObjRef) (g : ObjRef → Time) + (ht : readTcbReadyTime (Z := Unit) t σ = some (g t)) + (hq : ∀ y ∈ σ.releaseQueue, readTcbReadyTime (Z := Unit) y σ = some (g y)) : + ∃ b, tcbReleaseEnqueue (Z := Unit) t σ = ({((), setRelA σ (specInsTA σ.releaseQueue g t) b)}, False) := by + unfold tcbReleaseEnqueue + rw [nd_gets_bindSc, nd_bind_oneSc (readyTimesAppend_eqA t _ g ht hq)] + unfold setReleaseQueue + rw [nd_bind_oneSc (show NonDet.modify (fun s : State Unit => { s with releaseQueue := specInsTA σ.releaseQueue g t }) + σ = ({((), { σ with releaseQueue := specInsTA σ.releaseQueue g t })}, False) from rfl)] + simp only [NonDet.when] + split + · exact ⟨true, rfl⟩ + · exact ⟨σ.reprogramTimer, rfl⟩ + +end Spec + +/-! ## `release_enqueue` refines `tcb_release_enqueue` -/ + +theorem takeWhile_map_spSc (P : Fl4SpecA64.ObjRef → Bool) (Q : U64 → Bool) : + ∀ R : List U64, (∀ y ∈ R, P (specPtr y) = Q y) → (R.map specPtr).takeWhile P = (R.takeWhile Q).map specPtr + | [], _ => rfl + | a :: R, h => by + simp only [List.map_cons, List.takeWhile_cons, h a List.mem_cons_self] + split + · rw [List.map_cons, takeWhile_map_spSc P Q R fun y hy => h y (List.mem_cons_of_mem _ hy)] + · rfl + +theorem dropWhile_map_spSc (P : Fl4SpecA64.ObjRef → Bool) (Q : U64 → Bool) : + ∀ R : List U64, (∀ y ∈ R, P (specPtr y) = Q y) → (R.map specPtr).dropWhile P = (R.dropWhile Q).map specPtr + | [], _ => rfl + | a :: R, h => by + simp only [List.map_cons, List.dropWhile_cons, h a List.mem_cons_self] + split + · exact dropWhile_map_spSc P Q R fun y hy => h y (List.mem_cons_of_mem _ hy) + · rfl + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **The spec ready time of a TCB bound to an active SC is FL4's `ready_time`** (x86 `readyTime_rel`). -/ +theorem SchedSRN.readyTimeK {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} {σ σ' : Fl4SpecA64.State Unit} + (H : SchedSRN st Z m κ s σ) (hk' : σ'.kheap = σ.kheap) {u : U64} (hu : u ∈ Z.T) (h0 : L.mem s (scA u) ≠ 0#u64) + (ha : (L.rmax s (L.mem s (scA u))).val ≠ 0) : + Fl4SpecA64.readTcbReadyTime (Z := Unit) (specPtr u) σ' = some (L.rtime s u).bv := by + obtain ⟨x, hk, hsc⟩ := H.sch.tcb u hu + obtain ⟨y, n, hky, hy⟩ := H.scr.sc _ (H.rest.scMem u hu h0) + rw [← hk'] at hk hky + replace hy := hy.act ha + rw [readTcbReadyTime_eqA hk (by rw [hsc]; unfold optScA; rw [if_neg h0]; rfl) hky + (by rw [hy.refills]; exact refills_neA hy.wf), refillHd_eqA hy.wf hy.refills] + rfl + +theorem SchedSRN.readyTime {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} {σ : Fl4SpecA64.State Unit} + (H : SchedSRN st Z m κ s σ) {u : U64} (hu : u ∈ Z.T) (h0 : L.mem s (scA u) ≠ 0#u64) + (ha : (L.rmax s (L.mem s (scA u))).val ≠ 0) : + Fl4SpecA64.readTcbReadyTime (Z := Unit) (specPtr u) σ = some (L.rtime s u).bv := + H.readyTimeK rfl hu h0 ha + +/-- A TCB of `Z.T` bound to an active spec SC has a nonzero SC word whose `REFILL_MAX` is nonzero. -/ +theorem SchedSRN.act_of {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} {σ : Fl4SpecA64.State Unit} + (H : SchedSRN st Z m κ s σ) {u : U64} (hu : u ∈ Z.T) {x : Fl4SpecA64.Tcb} + (hk : σ.kheap (specPtr u) = some (.tcb x)) {p : Fl4SpecA64.ObjRef} (hp : x.tcbSchedContext = some p) + {y : Fl4SpecA64.SchedContext} {n : Nat} (hky : σ.kheap p = some (.schedContext y n)) + (ha : Fl4SpecA64.scActive y = true) : + L.mem s (scA u) ≠ 0#u64 ∧ (L.rmax s (L.mem s (scA u))).val ≠ 0 := by + obtain ⟨x', hk', hsc⟩ := H.sch.tcb u hu + rw [hk] at hk'; cases hk' + rw [hsc] at hp + have h0 : L.mem s (scA u) ≠ 0#u64 := fun e => by rw [e] at hp; simp [optScA] at hp + refine ⟨h0, ?_⟩ + have hp' : scNameA m (L.mem s (scA u)) = p := by + unfold optScA at hp; rw [if_neg h0] at hp; injection hp + obtain ⟨y', n', hky', hy⟩ := H.scr.sc _ (H.rest.scMem u hu h0) + rw [hp', hky] at hky'; cases hky' + rw [hy.max] + simp [Fl4SpecA64.scActive, Fl4SpecA64.activeSc] at ha + omega + +/-- The members of the current core's release list are TCBs of `Z.T` bound to active SCs (`ScInvA.relSc`). -/ +theorem SchedSRN.relAct {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} {σ : Fl4SpecA64.State Unit} + (H : SchedSRN st Z m κ s σ) : + ∀ u ∈ Z.R (L.cpu s), u ∈ Z.T ∧ L.mem s (scA u) ≠ 0#u64 ∧ (L.rmax s (L.mem s (scA u))).val ≠ 0 := by + intro u hu + have hT := H.core.memTR H.sch.cpuCs hu + obtain ⟨x, p, y, n, hk, hp, hky, ha⟩ := + H.inv.relSc (specPtr u) (by rw [H.rest.rel]; exact List.mem_map_of_mem hu) + exact ⟨hT, H.act_of hT hk hp hky ha⟩ + +/-- **The current core's release list is sorted by FL4's ready times** (`ScInvA.sorted` read through +`SchedSRN.readyTime`). -/ +theorem SchedSRN.relSorted {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} {σ : Fl4SpecA64.State Unit} + (H : SchedSRN st Z m κ s σ) : TimeSorted (L.rtime s) (Z.R (L.cpu s)) := by + have hrt : ∀ u ∈ Z.R (L.cpu s), Fl4SpecA64.readTcbReadyTime (Z := Unit) (specPtr u) σ = some (L.rtime s u).bv := + fun u hu => H.readyTime (H.relAct u hu).1 (H.relAct u hu).2.1 (H.relAct u hu).2.2 + have := H.inv.sorted + rw [H.rest.rel, List.pairwise_map] at this + refine List.Pairwise.imp_of_mem (fun {a b} ha hb hab => ?_) this + unfold specRtA at hab + rw [hrt a ha, hrt b hb] at hab + exact hab + +/-- **`release_enqueue` refines `tcb_release_enqueue`** (x86 `release_enqueue_refines`; module docstring), for a +thread on the current core with an active SC in no queue. -/ +theorem release_enqueue_refinesA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} {σ : Fl4SpecA64.State Unit} + (H : SchedSRN st Z m κ s σ) {t : U64} (ht : t ∈ Z.T) (hon : L.mem s (affA t) = L.cpu s) + (h0 : L.mem s (scA t) ≠ 0#u64) (hq0 : (L.mem s (inqA t) &&& sched.Q_QUEUED) = 0#u64) + (hr0 : (L.mem s (inqA t) &&& sched.Q_RELEASE) = 0#u64) (hta : (L.rmax s (L.mem s (scA t))).val ≠ 0) : + sched.release_enqueue st s t ⦃ s' => ∃ b, + Fl4SpecA64.tcbReleaseEnqueue (Z := Unit) (specPtr t) σ = ({((), setRelA σ + ((relIns (L.rtime s) (L.rtime s t) t (Z.R (L.cpu s))).map specPtr) b)}, False) ∧ + SchedCoreA st Z.T Z.Cs s' Z.Q + (Function.update Z.R (L.cpu s) (relIns (L.rtime s) (L.rtime s t) t (Z.R (L.cpu s)))) ∧ + WritesIn st s s' (L.QFootN s (t :: Z.R (L.cpu s)) (L.cpu s) RelOffs t) ∧ + L.mem s' (inqA t) = L.mem s (inqA t) ||| sched.Q_RELEASE ⦄ := by + have hRel := H.rest.rel + have hrt : ∀ u ∈ Z.R (L.cpu s), Fl4SpecA64.readTcbReadyTime (Z := Unit) (specPtr u) σ = some (L.rtime s u).bv := + fun u hu => H.readyTime (H.relAct u hu).1 (H.relAct u hu).2.1 (H.relAct u hu).2.2 + have hsort := H.relSorted + set g : Fl4SpecA64.ObjRef → Fl4SpecA64.Time := fun p => (L.rtime s (fl4Ptr p)).bv with hg + have hgs : ∀ u, g (specPtr u) = (L.rtime s u).bv := fun u => by + show (L.rtime s (fl4Ptr (specPtr u))).bv = _ + rw [fl4Ptr_specPtr] + obtain ⟨b, he⟩ := tcbReleaseEnqueue_eqA (σ := σ) (specPtr t) g (by rw [hgs]; exact H.readyTime ht h0 hta) (by + rw [hRel]; intro y hy + obtain ⟨u, hu, rfl⟩ := List.mem_map.1 hy + rw [hgs]; exact hrt u hu) + have hP : ∀ y ∈ Z.R (L.cpu s), @decide (g (specPtr y) ≤ g (specPtr t)) (Classical.propDecidable _) = + decide (L.rtime s y ≤ L.rtime s t) := + fun y _ => by rw [hgs, hgs]; exact decide_eq_decide.2 (u64_le_bvSc _ _).symm + have hq' : specInsTA σ.releaseQueue g (specPtr t) = + (relIns (L.rtime s) (L.rtime s t) t (Z.R (L.cpu s))).map specPtr := by + rw [relIns_eq_sorted _ _ t _ hsort, hRel] + unfold specInsTA + rw [takeWhile_map_spSc (fun y => @decide (g y ≤ g (specPtr t)) (Classical.propDecidable _)) + (fun y => decide (L.rtime s y ≤ L.rtime s t)) _ hP, + dropWhile_map_spSc (fun y => @decide (g y ≤ g (specPtr t)) (Classical.propDecidable _)) + (fun y => decide (L.rtime s y ≤ L.rtime s t)) _ hP] + simp + refine WP.spec_mono (sched.release_enqueue.coreA t H.core H.sch.cpuCs ht hq0 hr0) ?_ + rintro s' ⟨hc', hw, hinq⟩ + have hwN := hw.qfootN_of hon + rw [hon] at hc' hwN + exact ⟨b, by rw [he, hq'], hc', hwN, hinq⟩ + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScRelRmA.lean b/proofs/Fl4ProofsA64/P3/ScRelRmA.lean new file mode 100644 index 000000000..b44f87ecf --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScRelRmA.lean @@ -0,0 +1,287 @@ +/- +P3 (AArch64): current-core release removal, used by SC unbind. +Ports x86 `ScAwaken.release_remove_repr` and `ScConfig.release_remove_refines`. +The thread removed may still have an unreleased SC until the binding is cleared, +so the intermediate relation is `SchedSRB` at that thread. No named premises. +-/ +import Fl4ProofsA64.P3.ScDeqA +import Fl4ProofsA64.P3.ScPostA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +section Spec +open Fl4SpecA64 + +def releaseRemoveStA (σ : State Unit) (t : ObjRef) : State Unit := + setRelA σ (tcbSchedDequeue t σ.releaseQueue) + (if σ.releaseQueue.head? = some t then true else σ.reprogramTimer) + +theorem tcbReleaseRemove_eqA (σ : State Unit) (t : ObjRef) : + tcbReleaseRemove (Z := Unit) t σ = ({((), releaseRemoveStA σ t)}, False) := by + unfold tcbReleaseRemove + rw [nd_gets_bindSc] + have hh : (σ.releaseQueue ≠ [] ∧ the σ.releaseQueue.head? = t) ↔ σ.releaseQueue.head? = some t := by + cases e : σ.releaseQueue with + | nil => simp + | cons a xs => simp [the_someA] + unfold Fl4Spec.NonDet.when releaseRemoveStA + by_cases hhead : σ.releaseQueue.head? = some t + · rw [if_pos (hh.2 hhead), if_pos hhead] + have hset : Fl4Spec.NonDet.modify (fun s : State Unit => { s with reprogramTimer := true }) σ = + ({((), { σ with reprogramTimer := true })}, False) := rfl + rw [nd_bind_oneSc hset, nd_pure_bindSc] + rfl + · rw [if_neg (fun h => hhead (hh.1 h)), if_neg hhead] + rw [nd_bind_oneSc (show Fl4Spec.NonDet.pure (σ := State Unit) () σ = ({((), σ)}, False) from rfl), + nd_pure_bindSc] + rfl + +theorem releaseRemoveStA_notMem (σ : State Unit) (t : ObjRef) : + t ∉ (releaseRemoveStA σ t).releaseQueue := by + intro hm + exact of_decide_eq_true (List.mem_filter.1 hm).2 rfl + +theorem releaseRemoveStA_of_notMem {σ : State Unit} {t : ObjRef} (hn : t ∉ σ.releaseQueue) : + releaseRemoveStA σ t = σ := by + have hhead : σ.releaseQueue.head? ≠ some t := fun h => hn (List.mem_of_head? h) + unfold releaseRemoveStA tcbSchedDequeue + rw [if_neg hhead, List.filter_eq_self.2 fun u hu => by simpa using fun e : t = u => hn (e ▸ hu)] + rfl + +theorem ScInvA.releaseRemove {m : McState} {σ : State Unit} (h : ScInvA m σ) (t : ObjRef) : + ScInvA m (releaseRemoveStA σ t) := by + refine ⟨h.refills, h.rr, h.ntfnIdle, h.ntfnIdleOf, fun u hu => h.relSc u (List.mem_filter.1 hu).1, ?_, + h.back, h.tcbScNotIdle⟩ + have hf := h.sorted.filter (fun u => decide (t ≠ u)) + refine List.Pairwise.imp_of_mem (fun {a b} ha hb hab => ?_) hf + have keep (u : ObjRef) (hu : u ∈ σ.releaseQueue) : specRtA (releaseRemoveStA σ t) u = specRtA σ u := by + obtain ⟨x, p, y, n, h1, h2, h3, h4⟩ := h.relSc u hu + have hh := h.ne h3 h4 + rw [specRtA_of (σ := releaseRemoveStA σ t) h1 h2 h3 hh, specRtA_of h1 h2 h3 hh] + rw [keep a (List.mem_filter.1 ha).1, keep b (List.mem_filter.1 hb).1] + exact hab + +end Spec + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +local notation "wa" => core.num.U64.wrapping_add +private def relLinks (o : Nat) : Prop := o = 64 ∨ o = 72 + +/-- Removing the queue links and flag after the timer decision leaves the timer word alone. -/ +private theorem relRemoveLinksA {T : Set U64} (s : S) (c t : U64) (xs : List U64) + (hT : TcbPlaced T) (ht : t ∈ T) (hN : NodePlaced st T s c) + (h : QueueRel (L.mem s) T .Sched (L.relQ s c) xs) (htx : t ∈ xs) : + (do + let q ← sched.q_get st s c layout.node.RELEASE_HEAD + let (q', s') ← tcbqueue.remove st s tcbqueue.Link.Sched q t + let s'' ← sched.q_set st s' c layout.node.RELEASE_HEAD q' + sched.set_flag st s'' t sched.Q_RELEASE false) ⦃ r => WritesIn st s r (L.QFootN s xs c relLinks t) ⦄ := by + refine WP.spec_bind (sched.q_get.spec s c layout.node.RELEASE_HEAD) ?_ + intro q hq + have he : q = L.relQ s c := hq + refine WP.spec_bind (tcbqueue.remove.rel s .Sched q t xs hT (he ▸ h) htx) ?_ + rintro ⟨q', s'⟩ hq' + simp only [WP.uncurry'_pair] at hq' + obtain ⟨-, hf, -, -⟩ := hq' + refine WP.spec_bind (sched.q_set.spec s' c layout.node.RELEASE_HEAD q') ?_ + intro s'' hs'' + have hw : WritesIn st s' s'' (L.NodeOf s c relLinks) := by + rw [hs'', nodeAt_of_ksb hf.writesIn.1, nodeAt_of_ksb hf.writesIn.1] + exact ((writesIn_node (P := relLinks) hN (by rw [relh_val]; simp [relLinks]) (by rw [relh_val]; omega) + (by rw [relh_val])).trans (writesIn_node (P := relLinks) hN (by rw [relt_val]; simp [relLinks]) + (by rw [relt_val]; omega) (by rw [relt_val]))).mono fun _ h => h.elim id id + refine WP.spec_mono (sched.set_flag.spec s'' t sched.Q_RELEASE false) ?_ + intro r hr + simp only [Bool.false_eq_true, ite_false] at hr + have wi : WritesIn st s'' r (· = inqA t) := by rw [hr]; exact writesIn_inq (hT t ht) + refine ((hf.writesIn.trans hw).trans wi).mono fun _ h => ?_ + rcases h with (h | h) | h + · exact Or.inl h + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr h) + +/-- If the removed thread heads its release queue, set REPROGRAM. Otherwise keep it. +Ports x86 `release_remove_repr` and `release_remove_norepr`. -/ +theorem sched.release_remove.reprogramA {T : Set U64} (s : S) (t : U64) (xs : List U64) + (hT : TcbPlaced T) (ht : t ∈ T) (hN : NodePlaced st T s (L.mem s (affA t))) + (hN' : NodePlaced st T s (L.cpu s)) (h : QueueRel (L.mem s) T .Sched (L.relQ s (L.mem s (affA t))) xs) + (hb : (L.mem s (inqA t) &&& sched.Q_RELEASE) ≠ 0#u64) (htx : t ∈ xs) : + sched.release_remove st s t ⦃ s' => + L.mem s' (L.nodeAt s' (L.mem s (affA t)) layout.node.REPROGRAM) = + if xs.head? = some t then 1#u64 else L.mem s (L.nodeAt s (L.mem s (affA t)) layout.node.REPROGRAM) ⦄ := by + let c := L.mem s (affA t) + have hr : layout.node.REPROGRAM.val = 56 := repr_val + have hcond : L.mem s (L.nodeAt s c layout.node.RELEASE_HEAD) = t ↔ xs.head? = some t := by + change (L.relQ s c).head = t ↔ _ + rw [h.head] + obtain ⟨u, ys, e⟩ := List.exists_cons_of_ne_nil (List.ne_nil_of_mem htx) + rw [e] + simp only [List.headD_cons, List.head?_cons, Option.some.injEq] + unfold sched.release_remove + refine WP.spec_bind (sched.in_release_queue.spec s t) ?_ + rintro _ rfl + rw [if_pos (decide_eq_true hb)] + refine WP.spec_bind (obj.Tcb.affinity.spec s t) ?_ + rintro _ rfl + refine WP.spec_bind (obj.node_get.spec s c layout.node.RELEASE_HEAD) ?_ + intro i hi + rw [hi] + have tail (s1 : S) (hk : L.ksb s1 = L.ksb s) (hc : L.cpu s1 = L.cpu s) + (hq : QueueRel (L.mem s1) T .Sched (L.relQ s1 c) xs) : + (do + let q ← sched.q_get st s1 c layout.node.RELEASE_HEAD + let (q', s') ← tcbqueue.remove st s1 tcbqueue.Link.Sched q t + let s'' ← sched.q_set st s' c layout.node.RELEASE_HEAD q' + sched.set_flag st s'' t sched.Q_RELEASE false) ⦃ r => + L.mem r (L.nodeAt r c layout.node.REPROGRAM) = L.mem s1 (L.nodeAt s1 c layout.node.REPROGRAM) ⦄ := by + have hNc := hN.ksb hk + have hNcur : NodePlaced st T s1 (L.cpu s1) := by rw [hc]; exact hN'.ksb hk + refine WP.spec_mono (relRemoveLinksA s1 c t xs hT ht hNc hq htx) ?_ + intro r hw + exact node_qfoot hT hq.mem hNc hNcur ht (hw.mono fun _ h => h.toQFoot) hNc + (by rw [hr]; omega) (by rw [hr]) (fun _ => by rw [hr]; simp [relLinks]) (fun _ => by rw [hr]; omega) + by_cases hhead : xs.head? = some t + · rw [if_pos (hcond.2 hhead)] + refine WP.spec_bind (sched.set_reprogram_on.spec s c) ?_ + rintro s1 rfl + have hk := L.ksb_wr s (L.nodeAt s c layout.node.REPROGRAM) 1#u64 + have hc := L.cpu_wr s (L.nodeAt s c layout.node.REPROGRAM) 1#u64 + have hq : QueueRel (L.mem (L.wr s (L.nodeAt s c layout.node.REPROGRAM) 1#u64)) T .Sched + (L.relQ (L.wr s (L.nodeAt s c layout.node.REPROGRAM) 1#u64) c) xs := by + rw [relQ_wr_node hN hN (by rw [hr]; omega) (by rw [hr]) (fun _ => by rw [hr]; omega)] + exact h.wr_node hT hN (by rw [hr]; omega) (by rw [hr]) + refine WP.spec_mono (tail _ hk hc hq) ?_ + intro r he + rw [if_pos hhead, he, nodeAt_of_ksb hk, L.mem_wr_same (nodeAt_aligned hN (by rw [hr]; omega) (by rw [hr]))] + · rw [if_neg (fun he => hhead (hcond.1 he))] + simp only [bind_ok] + refine WP.spec_mono (tail s rfl rfl h) ?_ + intro r he + rw [if_neg hhead] + exact he + +set_option maxHeartbeats 1200000 in +/-- Rebuild the scheduler relation after removing one entry from the current release queue. +Only the removed thread can lose the released-or-queued condition. -/ +theorem SchedSRA.releaseRemoveQ {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s s' : S} + {σ : Fl4SpecA64.State Unit} (H : SchedSRA st Z m κ s σ) {t : U64} (ht : t ∈ Z.T) (hon : L.mem s (affA t) = L.cpu s) + (hw : WritesIn st s s' (L.QFoot s (Z.R (L.cpu s)) (L.cpu s) RelOffs t)) + (hcore : SchedCoreA st Z.T Z.Cs s' Z.Q (Function.update Z.R (L.cpu s) ((Z.R (L.cpu s)).erase t))) + (hrep : (releaseRemoveStA σ (specPtr t)).reprogramTimer = + decide (L.mem s' (L.nodeAt s (L.cpu s) layout.node.REPROGRAM) ≠ 0#u64)) : + (∀ u ∈ Z.T, L.mem s' (scA u) = L.mem s (scA u)) ∧ + SchedSRB st { Z with R := Function.update Z.R (L.cpu s) ((Z.R (L.cpu s)).erase t) } m κ s' + (releaseRemoveStA σ (specPtr t)) (specPtr t) := by + let σ' := releaseRemoveStA σ (specPtr t) + have hcur := H.sch.cpuCs + have hN := H.core.nodes _ hcur + have hQ := H.core.release _ hcur + have hT := H.core.tcbs + have hW := hQ.mem + obtain ⟨-, -, out⟩ := ext_qfoot hT hW hN hN ht hcur hcur hw + have scK (u : U64) (hu : u ∈ Z.T) : L.mem s' (scA u) = L.mem s (scA u) := + sc_qfoot hT hW hN hN ht hw hu + have nK (c : U64) (hc : c ∈ Z.Cs) (o : U64) (ho : o.val < 56) (ha : o.val % 8 = 0) : + L.mem s' (L.nodeAt s c o) = L.mem s (L.nodeAt s c o) := by + have hn := node_qfoot hT hW hN hN ht hw (H.core.nodes c hc) (o := o) (by omega) ha + (fun _ => by simp only [RelOffs]; omega) (fun _ => by omega) + rwa [nodeAt_of_ksb hw.1] at hn + have tK (u : U64) (hu : u ∈ Z.T) (K : U64) (hK : K = 3520#u64 ∨ K = 3448#u64 ∨ K = 3432#u64) : + L.mem s' (wa u K) = L.mem s (wa u K) := by + have hv : K.val = 3520 ∨ K.val = 3448 ∨ K.val = 3432 := by rcases hK with rfl | rfl | rfl <;> simp + exact field_qfoot hT hW hN hN ht hw hu (by omega) (by omega) (by omega) (fun _ => by omega) + have href : σ'.releaseQueue = ((Z.R (L.cpu s)).erase t).map specPtr := by + change Fl4SpecA64.tcbSchedDequeue (specPtr t) σ.releaseQueue = _ + rw [H.rest.rel, tcbSchedDequeue_mapA hQ.nodup] + have hscK (a : U64) (ha : Aligned a) (hf : ScFootA Z.C m σ a) : L.mem s' a = L.mem s a := + out a (H.toSchedSRN.scOut ha hf) + refine ⟨scK, ⟨?_, fun u x hne hk hc y p z ns hy hp hz ha hn => ?_⟩⟩ + · refine H.toSchedSRN.rebuildG (Q' := Z.Q) hw.1 hw.2.1 (fun c hc o ho ha _ _ => nK c hc o ho ha) + (fun K hK => ?_) tK (H.scr.frame hscK (fun _ _ => rfl)) (H.scp.of_kheap (fun _ _ => rfl)) + (fun _ _ x hx => ⟨x, hx, rfl, rfl⟩) + (fun u hu => by obtain ⟨x, h1, h2⟩ := H.sch.tcb u hu; exact ⟨x, h1, by rw [scK u hu]; exact h2⟩) + (fun u hu h => by rw [scK u hu] at h ⊢; exact H.rest.scMem u hu h) + ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ hrep ?_ H.actOn (H.inv.releaseRemove _) hcore H.rest.rq + H.rest.rqDom (by rw [Function.update_self]; exact href) + ⟨H.run.1, fun u hu => H.run.2 u (List.mem_filter.1 hu).1⟩ + (fun c hc => hscK _ (H.scCoreFoot hc).1 (H.scCoreFoot hc).2) + · have hKv : K.val = 8 ∨ K.val = 16 := by rcases hK with rfl | rfl <;> simp + have hf := H.ksFit + have hv : (wa (L.ksb s) K).val = (L.ksb s).val + K.val := by rw [wa_valm]; unfold ksSizeA at hf; omega + have hkb := H.ksb_al + refine out _ ⟨by unfold Aligned; rw [hv]; omega, fun y hy => ?_, fun c hc => ?_⟩ + · have := H.tcb_ks hy; rw [hv]; unfold ksSizeA at this; omega + · rw [hv]; unfold LawfulStore.nodeBase; omega + · change σ.schedulerAction = _ + rw [nK _ hcur layout.node.SCHED_ACTION (by simp [layout.node.SCHED_ACTION]) + (by simp [layout.node.SCHED_ACTION])] + exact H.sch.act + · have hno : u ∉ σ.releaseQueue := fun hu => hn (List.mem_filter.2 ⟨hu, by simpa using Ne.symm hne⟩) + exact H.rd u x hk hc y p z ns hy hp hz ha hno + +set_option maxHeartbeats 1200000 in +/-- Release-queue removal on the calling core, through the unlink's intermediate `SchedSRB`. -/ +theorem release_remove_refines_localA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} + {σ : Fl4SpecA64.State Unit} (H : SchedSRA st Z m κ s σ) {t : U64} (ht : t ∈ Z.T) + (hon : L.mem s (affA t) = L.cpu s) : + sched.release_remove st s t ⦃ s' => ∃ R', + (∀ u ∈ Z.T, L.mem s' (scA u) = L.mem s (scA u)) ∧ + SchedSRB st { Z with R := R' } m κ s' (releaseRemoveStA σ (specPtr t)) (specPtr t) ∧ + MFrA (st := st) Z m s s' σ (releaseRemoveStA σ (specPtr t)) ⦄ := by + have hcur := H.sch.cpuCs + have hc := H.core.aff t ht + have hT := H.core.tcbs + have hN := H.core.nodes _ hc + have hN' := H.core.nodes _ hcur + have hrel := H.core.release _ hc + have hm := (H.core.released t ht).1 + by_cases hb : (L.mem s (inqA t) &&& sched.Q_RELEASE) ≠ 0#u64 + · refine WP.spec_mono (WP.spec_and (WP.spec_and + (sched.release_remove.rel s t _ hT ht hN hrel hm) (sched.release_remove.footN s t _ hT ht hN hN' hrel hm)) + (sched.release_remove.reprogramA s t _ hT ht hN hN' hrel hb (hm hb))) ?_ + rintro s' ⟨⟨hq, hwN, hinq⟩, hrep⟩ + have hw := hwN.mono fun _ h => h.toQFoot + rw [if_pos hb] at hq hinq + have hcore := core_release_step (tIn := False) H.core hcur ht rfl (fun _ hw => Or.inr hw) + (fun hmem _ hc' _ hp hy => H.core.QR hc' hp hy hc hmem) hw hq + (fun u => by rw [hrel.nodup.mem_erase_iff]; simp [and_comm]) + (by rw [hinq, andn_qr_qr]; simp) (by rw [hinq, andn_qr_qq]) False.elim + rw [hon] at hcore hw hrep + have hdec : (releaseRemoveStA σ (specPtr t)).reprogramTimer = + decide (L.mem s' (L.nodeAt s (L.cpu s) layout.node.REPROGRAM) ≠ 0#u64) := by + rw [← nodeAt_of_ksb hw.1, hrep] + have hhead : σ.releaseQueue.head? = some (specPtr t) ↔ (Z.R (L.cpu s)).head? = some t := by + rw [H.rest.rel] + cases Z.R (L.cpu s) with + | nil => simp + | cons u us => simp only [List.map_cons, List.head?_cons, Option.some.injEq]; exact ⟨specPtr_inj, congrArg specPtr⟩ + change (if σ.releaseQueue.head? = some (specPtr t) then true else σ.reprogramTimer) = _ + by_cases hh : (Z.R (L.cpu s)).head? = some t + · rw [if_pos (hhead.2 hh), if_pos hh] + simp + · rw [if_neg (fun h => hh (hhead.1 h)), if_neg hh] + exact H.rest.repr + obtain ⟨hsc, H'⟩ := H.releaseRemoveQ ht hon hw hcore hdec + have hwN' := hwN + rw [hon] at hwN' + exact ⟨_, hsc, H', MFrA.of_sw rfl (hwN'.mono fun _ h => H.toSchedSRN.qfoot_swr + (H.core.release _ hcur).mem hcur ht h)⟩ + · have hb0 := not_not.1 hb + have hn := (H.relOn ht hon).1 hb0 + refine WP.spec_mono (sched.release_remove.noop s t hb0) ?_ + intro s' hs' + subst s' + rw [releaseRemoveStA_of_notMem hn] + exact ⟨Z.R, fun _ _ => rfl, H.toB _, MFrA.refl Z m s σ⟩ + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScReplyA.lean b/proofs/Fl4ProofsA64/P3/ScReplyA.lean new file mode 100644 index 000000000..18946f936 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScReplyA.lean @@ -0,0 +1,233 @@ +/- +P3 (aarch64, A64-P3-MCS gen 12): **the SC half of `bind_sc_reply`** on `SchedSRA` (ruling 02:39Z gen-12 item 1: FL4 +`sc.REPLY := r` against l4v `sc_replies := r # _`; x86 `P3/ScCallPushRef`'s `push_donate_refines`, its `set_reply` / +`bindScReply` part, and `P3/ScConsumed`'s `Inv.scUpd` route, ported). + +FL4 keeps the head of an SC's reply stack in the SC's `REPLY` word and links the rest through the replies' own words +(IPC's footprint); l4v keeps the whole stack in `sc_replies` and points each reply back with `reply_sc`. The relation +(`ScRelWA.reply`, gen 1) relates the head only, so: + +* **`ScRelWA.set_reply`** / `scReplyA_eqS` (x86 `ScRelW.wr_hdr` at `K = 32`): writing the `REPLY` word `v` relates the SC + to the spec's SC with any reply list `l` whose head is `optRefS v`. +* `repStA` / **`setScReplies_eqA`**: l4v `set_sc_obj_ref sc_replies_update` has the single outcome `repStA`. +* **`SchedSRA.setReplyA`** (the SC step): `SchedSRA` after the write and `repStA`, with the frame `MFrA` (a corollary of + gen 9's `SchedSRA.scObjA`: refills, refill max, period and TCB of the SC kept, the `CORE` word kept). +* **`sc_set_replyA`**: the program `obj.Sc.set_reply` against `set_sc_obj_ref sc_replies_update`. +* The reply objects (l4v `reply_sc`; IPC's objects, outside `SKeepA`): `setReplySc_eqA` (l4v `set_reply_obj_ref + reply_sc_update` of a reply object), `rscA` (that update, the identity on a non-reply), **`SStepA.replyA`** (a reply + object update is an `SStepA` step blocking nothing) and **`SchedSRA.rscA`** (it keeps `SchedSRA`, FL4 unchanged). +* **`bindScReply_eqA`** / `bindStA`: l4v `bind_sc_reply N R` of an SC whose head reply (if any) and `R` are reply objects + has the single outcome `bindStA` (`bindStA_kheapN`, `bindStA_kheapR`, `bindStA_kheapH`, `bindStA_kheap`: its heap). +* **`bind_sc_replyA`** (the SC half of `reply_push`'s `bind_sc_reply`): FL4 `obj.Sc.set_reply sc r` (`r ≠ 0`) against + l4v `bind_sc_reply (scNameA m sc) (specPtr r)`: single outcome `bindStA`, `SchedSRA` after, FL4 writing in `MWrA`, + the spec changing that SC's `sc_replies` and the two replies' `reply_sc` only (`SSzA`, the TCBs kept). + +No named premise. Hypotheses: `hc` (`sc ∈ Z.C`), `hl` (the new list's head is the word written). +-/ +import Fl4ProofsA64.P3.ScDonFromA +import Fl4ProofsA64.P3.ScFrameA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## The spec -/ + +section Spec +open Fl4SpecA64 + +/-- The spec state after `sc_replies := l` at the SC `N`. -/ +def repStA (σ : State Unit) (N : ObjRef) (xs : SchedContext) (ns : Nat) (l : List ObjRef) : State Unit := + updKSc σ N (.schedContext { xs with scReplies := l } ns) + +/-- l4v `set_sc_obj_ref sc_replies_update N l`. -/ +theorem setScReplies_eqA {σ : State Unit} {N : ObjRef} {xs : SchedContext} {ns : Nat} + (hk : σ.kheap N = some (.schedContext xs ns)) (l : List ObjRef) : + setScObjRef (Z := Unit) scRepliesUpdate N l σ = ({((), repStA σ N xs ns l)}, False) := + updateSchedContext_eqA hk _ + +theorem partialInv_replyA (x : Reply) : partialInv KernelObject.reply (.reply x) = some x := by + have hu : ∃! y, KernelObject.reply y = .reply x := ⟨x, rfl, fun y h => by cases h; rfl⟩ + unfold partialInv The + simp only [hu, ↓reduceIte, ↓reduceDIte] + have h := (Classical.choose_spec hu).1 + injection h with h + rw [h] + +theorem getSimpleKo_replyA {σ : State Unit} {p : ObjRef} {x : Reply} (hk : σ.kheap p = some (.reply x)) : + getSimpleKo (Z := Unit) KernelObject.reply p σ = ({(x, σ)}, False) := by + unfold getSimpleKo + rw [nd_bind_oneSc (getObject_eqSc hk)] + rw [nd_bind_oneSc (show Fl4Spec.NonDet.assert (σ := State Unit) (isSimpleType (.reply x)) σ = + ({((), σ)}, False) by simp [Fl4Spec.NonDet.assert, isSimpleType, aType])] + simp only [partialInv_replyA]; rfl + +/-- l4v `set_reply_obj_ref reply_sc_update p v` of a reply object. -/ +theorem setReplySc_eqA {σ : State Unit} {p : ObjRef} {x : Reply} (hk : σ.kheap p = some (.reply x)) + (v : Option ObjRef) : + setReplyObjRef (Z := Unit) replyScUpdate p v σ = + ({((), updKSc σ p (.reply { x with replySc := v }))}, False) := by + show updateSkObjRef KernelObject.reply replyScUpdate p v σ = _ + unfold updateSkObjRef + rw [nd_bind_oneSc (getSimpleKo_replyA hk)] + unfold setSimpleKo + rw [nd_bind_oneSc (getObject_eqSc hk)] + rw [nd_bind_oneSc (show Fl4Spec.NonDet.assert (σ := State Unit) (isSimpleType (.reply x)) σ = + ({((), σ)}, False) by simp [Fl4Spec.NonDet.assert, isSimpleType, aType])] + rw [nd_bind_oneSc (show Fl4Spec.NonDet.assert (σ := State Unit) + (partialInv KernelObject.reply (.reply x) ≠ none) σ = ({((), σ)}, False) by + simp [Fl4Spec.NonDet.assert, partialInv_replyA])] + exact setObject_eqSc hk rfl + +/-- `reply_sc := v` at `p` when `p` is a reply object (the identity otherwise). -/ +def rscA (σ : State Unit) (p : ObjRef) (v : Option ObjRef) : State Unit := + match σ.kheap p with + | some (.reply x) => updKSc σ p (.reply { x with replySc := v }) + | _ => σ + +theorem rscA_of {σ : State Unit} {p : ObjRef} {x : Reply} (hk : σ.kheap p = some (.reply x)) (v : Option ObjRef) : + rscA σ p v = updKSc σ p (.reply { x with replySc := v }) := by + unfold rscA; rw [hk] + +/-- `rscA` keeps every object but a reply at `p`, which stays a reply. -/ +theorem rscA_kheap (σ : State Unit) (p : ObjRef) (v : Option ObjRef) (q : ObjRef) : + ((rscA σ p v).kheap q = σ.kheap q) ∨ + (q = p ∧ ∃ x, σ.kheap p = some (.reply x) ∧ (rscA σ p v).kheap q = some (.reply { x with replySc := v })) := by + unfold rscA + split + · rename_i x hx + by_cases e : q = p + · subst e; exact Or.inr ⟨rfl, x, hx, by simp [updKSc]⟩ + · exact Or.inl (by simp [updKSc, e]) + · exact Or.inl rfl + +theorem rscA_ne {σ : State Unit} {p q : ObjRef} (v : Option ObjRef) (h : q ≠ p) : + (rscA σ p v).kheap q = σ.kheap q := by + rcases rscA_kheap σ p v q with e | ⟨e, -⟩ + · exact e + · exact absurd e h + +theorem rscA_fields (σ : State Unit) (p : ObjRef) (v : Option ObjRef) : rscA σ p v = { σ with kheap := (rscA σ p v).kheap } := by + unfold rscA; split <;> rfl + +/-- **A reply object's `reply_sc` update is an `SStepA` step blocking nothing.** -/ +theorem SStepA.replyA (σ : State Unit) (p : ObjRef) (v : Option ObjRef) : + SStepA σ (rscA σ p v) ∧ NoBlockESc σ (rscA σ p v) := by + have hk : ∀ q, (∀ x, σ.kheap q ≠ some (.reply x)) → (rscA σ p v).kheap q = σ.kheap q := by + intro q hq + rcases rscA_kheap σ p v q with e | ⟨rfl, x, hx, -⟩ + · exact e + · exact absurd hx (hq x) + have hk' : ∀ q o, (rscA σ p v).kheap q = some o → (∀ x, o ≠ .reply x) → σ.kheap q = some o := by + intro q o hq ho + rcases rscA_kheap σ p v q with e | ⟨-, x, -, e⟩ + · rw [← e]; exact hq + · rw [e] at hq; cases hq; exact absurd rfl (ho _) + have hf := rscA_fields σ p v + refine ⟨⟨?_, ?_, ?_, ?_, fun q y n hq => hk q (fun x h => by rw [hq] at h; cases h), + fun q x hq => ⟨x, by rw [hk q (fun x' h => by rw [hq] at h; cases h)]; exact hq, rfl, rfl, rfl⟩, + fun q y n hq => (hk' q _ hq (fun _ h => by cases h)).trans hq.symm, + fun q y hq => ⟨y, hk' q _ hq (fun _ h => by cases h)⟩, + fun q y hq => ⟨y, hk' q _ hq (fun _ h => by cases h), rfl⟩⟩, fun q x y h1 h2 => Or.inl ?_⟩ + · rw [hf]; exact ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ + · rw [hf] + · rw [hf] + · rw [hf] + · rw [hk' q _ h2 (fun _ h => by cases h)] at h1; cases h1; exact ⟨rfl, id⟩ + +end Spec + +/-! ## The `REPLY` word -/ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +theorem scReplyA_eqS (sc : U64) : scReplyA sc = scWA sc 32#u64 := by unfold scReplyA scWA layout.sc.REPLY; rfl + +/-- **Writing an SC's `REPLY` word** (x86 `ScRelW.wr_hdr` at `K = 32`): the spec's SC with any reply list headed by +`optRefS v`. -/ +theorem ScRelWA.set_reply {ii : Prop} {s : S} {sc : U64} {x : Fl4SpecA64.SchedContext} {n : Nat} + (h : ScRelWA st ii s sc x n) (v : U64) (l : List Fl4SpecA64.ObjRef) (hl : l.head? = optRefS v) : + ScRelWA st ii (L.wr s (scWA sc 32#u64) v) sc { x with scReplies := l } n ∧ + WritesIn st s (L.wr s (scWA sc 32#u64) v) (ScObjA sc n) := by + obtain ⟨hst, ep, em, hw, hv, hwr⟩ := h.wr_hdr (K := 32#u64) (Or.inr (Or.inr (Or.inl rfl))) v + have k : ∀ K : U64, K.val < 96 → K.val % 8 = 0 → K.val ≠ 32 → + L.mem (L.wr s (scWA sc 32#u64) v) (scWA sc K) = L.mem s (scWA sc K) := + fun K h1 h2 h3 => hw K h1 h2 (fun e => h3 (by subst e; simp)) + refine ⟨⟨h.pl, by rw [ep]; exact h.period, by rw [k 8#u64 (by simp) (by simp) (by simp)]; exact h.consumed, + by rw [k 24#u64 (by simp) (by simp) (by simp)]; exact h.tcb, by rw [hv]; exact hl, + by rw [k 40#u64 (by simp) (by simp) (by simp)]; exact h.ntfn, + by rw [k 48#u64 (by simp) (by simp) (by simp)]; exact h.badge, + by rw [k 56#u64 (by simp) (by simp) (by simp)]; exact h.yieldFrom, + by rw [k 64#u64 (by simp) (by simp) (by simp)]; exact h.sporadic, + fun h0 => by rw [em]; exact h.spor (by rwa [k 64#u64 (by simp) (by simp) (by simp)] at h0), + fun h0 => by rw [em]; exact h.idle h0, by rw [em]; exact h.max, fun h0 => ?_⟩, hwr⟩ + have h0' : (L.rmax s sc).val ≠ 0 := by rwa [em] at h0 + have h1 := h.act h0' + exact ⟨(hst h0').1, by rw [ep]; exact h1.period, by rw [k 8#u64 (by simp) (by simp) (by simp)]; exact h1.consumed, + by rw [k 24#u64 (by simp) (by simp) (by simp)]; exact h1.tcb, by rw [hv]; exact hl, + by rw [k 40#u64 (by simp) (by simp) (by simp)]; exact h1.ntfn, + by rw [k 48#u64 (by simp) (by simp) (by simp)]; exact h1.badge, + by rw [k 56#u64 (by simp) (by simp) (by simp)]; exact h1.yieldFrom, + by rw [k 64#u64 (by simp) (by simp) (by simp)]; exact h1.sporadic, by rw [em]; exact h1.max, + by rw [(hst h0').2]; exact h1.refills⟩ + +/-- **The SC step** (module docstring): `SchedSRA` and `MFrA` after `sc.REPLY := v` / `sc_replies := l`. -/ +theorem SchedSRA.setReplyA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} + {σ : Fl4SpecA64.State Unit} (H : SchedSRA st Z m κ s σ) {sc : U64} (hc : sc ∈ Z.C) + {x : Fl4SpecA64.SchedContext} {n : Nat} (hk : σ.kheap (scNameA m sc) = some (.schedContext x n)) + (v : U64) (l : List Fl4SpecA64.ObjRef) (hl : l.head? = optRefS v) : + SchedSRA st Z m κ (L.wr s (scReplyA sc) v) (repStA σ (scNameA m sc) x n l) ∧ + MFrA (st := st) Z m s (L.wr s (scReplyA sc) v) σ (repStA σ (scNameA m sc) x n l) := by + obtain ⟨x0, n0, hk0, hx⟩ := H.scr.sc sc hc + rw [hk] at hk0; cases hk0 + rw [scReplyA_eqS] + obtain ⟨hx', hw⟩ := hx.set_reply v l hl + have h32 := hx.hdr (K := 32#u64) (by simp) (by simp) + have h16 := hx.hdr (K := 16#u64) (by simp) (by simp) + obtain ⟨-, -, hfit⟩ := hx.bounds + have hcc : L.mem (L.wr s (scWA sc 32#u64) v) (scCoreA sc) = L.mem s (scCoreA sc) := by + rw [show scCoreA sc = scWA sc 16#u64 by unfold scCoreA scWA layout.sc.CORE; rfl] + refine L.mem_wr_ne h32.1 h16.1 ?_ + rw [scWA_val (sc := sc) (K := 32#u64) (by simp; omega), scWA_val (sc := sc) (K := 16#u64) (by simp; omega)] + simp + refine ⟨H.scObjA hc hk hw hx' rfl rfl rfl rfl hcc, hw.mono fun b hb => mwr_scObj hc hk hb, + SKeepA.of_obj fun q o hq ho => ?_, SSzA.of_upd hk rfl⟩ + have hne : q ≠ scNameA m sc := fun e => by rw [e, hk] at hq; cases hq; exact ho x n rfl + simp [repStA, updKSc, hne, hq] + +/-- **`obj.Sc.set_reply` against `set_sc_obj_ref sc_replies_update`** (module docstring). -/ +theorem sc_set_replyA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} + {σ : Fl4SpecA64.State Unit} (H : SchedSRA st Z m κ s σ) {sc : U64} (hc : sc ∈ Z.C) (v : U64) + (l : List Fl4SpecA64.ObjRef) (hl : l.head? = optRefS v) : + obj.Sc.set_reply st sc s v ⦃ s' => ∃ σ', + Fl4SpecA64.setScObjRef (Z := Unit) Fl4SpecA64.scRepliesUpdate (scNameA m sc) l σ = ({((), σ')}, False) ∧ + SchedSRA st Z m κ s' σ' ∧ MFrA (st := st) Z m s s' σ σ' ⦄ := by + obtain ⟨x, n, hk, -⟩ := H.scr.sc sc hc + refine WP.spec_mono (obj.Sc.set_reply.spec s sc v) ?_ + rintro s' rfl + exact ⟨_, setScReplies_eqA hk l, H.setReplyA hc hk v l hl⟩ + +/-- **A reply object's `reply_sc` update keeps `SchedSRA`** (FL4 unchanged). -/ +theorem SchedSRA.rscA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} + {σ : Fl4SpecA64.State Unit} (H : SchedSRA st Z m κ s σ) (p : Fl4SpecA64.ObjRef) (v : Option Fl4SpecA64.ObjRef) : + SchedSRA st Z m κ s (rscA σ p v) := + H.stable (WritesIn.refl s fun _ => False) (fun _ h => h.elim) (SStepA.replyA σ p v).1 (SStepA.replyA σ p v).2 + +/-- The reply update keeps every SC object (so `SSzA`, the SC names' objects and `MWrA`). -/ +theorem rscA_sc {σ : Fl4SpecA64.State Unit} {p : Fl4SpecA64.ObjRef} {v : Option Fl4SpecA64.ObjRef} + {q : Fl4SpecA64.ObjRef} {y : Fl4SpecA64.SchedContext} {n : Nat} : + (rscA σ p v).kheap q = some (.schedContext y n) ↔ σ.kheap q = some (.schedContext y n) := by + rcases rscA_kheap σ p v q with e | ⟨rfl, x, hx, e⟩ + · rw [e] + · rw [e, hx]; constructor <;> intro h <;> cases h + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScReplyBA.lean b/proofs/Fl4ProofsA64/P3/ScReplyBA.lean new file mode 100644 index 000000000..79b046f15 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScReplyBA.lean @@ -0,0 +1,126 @@ +/- +P3 (aarch64, A64-P3-MCS gen 12): **l4v `bind_sc_reply` against FL4's `sc.REPLY := r`** on `SchedSRA` (ruling 02:39Z +gen-12 item 1; x86 `P3/ScCallPushRef`'s `push_donate_refines`, its `bind_sc_reply` part, ported). + +* `unlHdA` / **`bindStA`** / **`bindScReply_eqA`**: l4v `bind_sc_reply N R` of an SC whose head reply (if any) and `R` + are reply objects has the single outcome `bindStA` (the head's `reply_sc := None`, `sc_replies := R # _`, `R`'s + `reply_sc := Some N`). +* **`bind_sc_replyA`** (the SC half of `reply_push`'s `bind_sc_reply`): FL4 `obj.Sc.set_reply sc r` (`r ≠ 0`) against + l4v `bind_sc_reply (scNameA m sc) (specPtr r)`: single outcome `bindStA`, `SchedSRA` after, FL4 writing in `MWrA`, + the SC objects keeping their sizes (`SSzA`). The spec's reply objects change (`reply_sc`), so the spec frame is not + `SKeepA` (IPC's reply relation owns them); FL4 writes the SC's `REPLY` word only. + +No named premise. Hypotheses: `hc` (`sc ∈ Z.C`), `hr0` (`r ≠ 0`: the reply pushed), `hkr` / `hhd` (the spec reply +objects: `R` and the stack's head are replies; IPC's reply relation supplies them). +-/ +import Fl4ProofsA64.P3.ScReplyA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +/-! ## `bind_sc_reply` -/ + +section Spec +open Fl4SpecA64 + +/-- The head reply's unlinking of `bind_sc_reply` (`reply_sc := none` at the stack's head, if any). -/ +def unlHdA (σ : State Unit) : List ObjRef → State Unit + | [] => σ + | h :: _ => rscA σ h none + +/-- **The spec state after `bind_sc_reply N R`** (module docstring). -/ +def bindStA (σ : State Unit) (N : ObjRef) (xs : SchedContext) (ns : Nat) (R : ObjRef) : State Unit := + rscA (repStA (unlHdA σ xs.scReplies) N xs ns (R :: xs.scReplies)) R (some N) + +theorem bindStA_def (σ : State Unit) (N : ObjRef) (xs : SchedContext) (ns : Nat) (R : ObjRef) : + bindStA σ N xs ns R = rscA (repStA (unlHdA σ xs.scReplies) N xs ns (R :: xs.scReplies)) R (some N) := rfl + +theorem unlHdA_cons (σ : State Unit) (h : ObjRef) (t : List ObjRef) : unlHdA σ (h :: t) = rscA σ h none := rfl + +/-- **l4v `bind_sc_reply N R`** (module docstring). -/ +theorem bindScReply_eqA {σ : State Unit} {N R : ObjRef} {xs : SchedContext} {ns : Nat} {xr : Reply} + (hks : σ.kheap N = some (.schedContext xs ns)) (hkr : σ.kheap R = some (.reply xr)) + (hhd : ∀ h, xs.scReplies.head? = some h → ∃ xh, σ.kheap h = some (.reply xh)) : + bindScReply (Z := Unit) N R σ = ({((), bindStA σ N xs ns R)}, False) := by + have hNR : N ≠ R := fun e => by rw [e, hkr] at hks; cases hks + have hg : Fl4Spec.NonDet.liftM SchedContext.scReplies (getSchedContext (Z := Unit) N) σ = + ({(xs.scReplies, σ)}, False) := by + unfold Fl4Spec.NonDet.liftM + exact nd_bind_oneSc (getSchedContext_eqA hks) _ + have fin : ∀ σ1 : State Unit, σ1.kheap N = some (.schedContext xs ns) → + (∃ xr1, σ1.kheap R = some (.reply xr1)) → + (setScObjRef (Z := Unit) scRepliesUpdate N (R :: xs.scReplies) >>= fun _ => + setReplyObjRef (Z := Unit) replyScUpdate R (some N)) σ1 = + ({((), rscA (repStA σ1 N xs ns (R :: xs.scReplies)) R (some N))}, False) := by + rintro σ1 hk1 ⟨xr1, hkr1⟩ + rw [nd_bind_oneSc (setScReplies_eqA hk1 _)] + have hkr2 : (repStA σ1 N xs ns (R :: xs.scReplies)).kheap R = some (.reply xr1) := by + unfold repStA; rw [updKSc_kheap, Function.update_of_ne (Ne.symm hNR)]; exact hkr1 + rw [setReplySc_eqA hkr2, rscA_of hkr2] + unfold bindScReply bindStA + rw [nd_bind_oneSc hg] + try beta_reduce + cases hL : xs.scReplies with + | nil => + rw [hL] at fin + exact fin σ hks ⟨xr, hkr⟩ + | cons h t => + rw [hL] at fin + obtain ⟨xh, hkh⟩ := hhd h (by rw [hL]; rfl) + have hhN : h ≠ N := fun e => by rw [e, hks] at hkh; cases hkh + refine (nd_bind_oneSc (setReplySc_eqA hkh none) _).trans ?_ + rw [← rscA_of hkh none] + refine fin _ (by rw [rscA_ne _ (Ne.symm hhN)]; exact hks) ?_ + by_cases e : R = h + · subst e; rw [rscA_of hkh]; exact ⟨_, by rw [updKSc_kheap, Function.update_self]⟩ + · exact ⟨xr, by rw [rscA_ne _ e]; exact hkr⟩ + +theorem unlHdA_sc {σ : State Unit} {l : List ObjRef} {q : ObjRef} {y : SchedContext} {n : Nat} : + (unlHdA σ l).kheap q = some (.schedContext y n) ↔ σ.kheap q = some (.schedContext y n) := by + cases l with + | nil => exact Iff.rfl + | cons h t => exact rscA_sc + +end Spec + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **The SC half of `bind_sc_reply`** (module docstring). -/ +theorem bind_sc_replyA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} + {σ : Fl4SpecA64.State Unit} (H : SchedSRA st Z m κ s σ) {sc r : U64} (hc : sc ∈ Z.C) (hr0 : r ≠ 0#u64) + {xr : Fl4SpecA64.Reply} (hkr : σ.kheap (specPtr r) = some (.reply xr)) + (hhd : ∀ x n h, σ.kheap (scNameA m sc) = some (.schedContext x n) → x.scReplies.head? = some h → + ∃ xh, σ.kheap h = some (.reply xh)) : + obj.Sc.set_reply st sc s r ⦃ s' => ∃ x n, σ.kheap (scNameA m sc) = some (.schedContext x n) ∧ + Fl4SpecA64.bindScReply (Z := Unit) (scNameA m sc) (specPtr r) σ = + ({((), bindStA σ (scNameA m sc) x n (specPtr r))}, False) ∧ + SchedSRA st Z m κ s' (bindStA σ (scNameA m sc) x n (specPtr r)) ∧ + WritesIn st s s' (MWrA (st := st) Z m σ s) ∧ SSzA σ (bindStA σ (scNameA m sc) x n (specPtr r)) ⦄ := by + obtain ⟨x, n, hk, -⟩ := H.scr.sc sc hc + refine WP.spec_mono (obj.Sc.set_reply.spec s sc r) ?_ + rintro s' rfl + have hB := bindScReply_eqA hk hkr (fun h e => hhd x n h hk e) + refine ⟨x, n, hk, ?_⟩ + rw [bindStA_def] at hB ⊢ + have H1 : ∀ l, SchedSRA st Z m κ s (unlHdA σ l) := fun l => by + cases l with + | nil => exact H + | cons h t => rw [unlHdA_cons]; exact H.rscA h none + have hk1 : (unlHdA σ x.scReplies).kheap (scNameA m sc) = some (.schedContext x n) := unlHdA_sc.2 hk + obtain ⟨H2, F2⟩ := (H1 x.scReplies).setReplyA hc hk1 r (specPtr r :: x.scReplies) + (by rw [optRefS_ne hr0]; rfl) + have hz1 : SSzA σ (unlHdA σ x.scReplies) := fun p y k h => ⟨y, unlHdA_sc.1 h⟩ + exact ⟨hB, + H2.rscA (specPtr r) (some (scNameA m sc)), + F2.wr.mono fun b hb => mwr_back rfl rfl hz1 rfl hb, + (hz1.trans F2.sz).trans fun p y k h => ⟨y, rscA_sc.1 h⟩⟩ + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScRestartUnbA.lean b/proofs/Fl4ProofsA64/P3/ScRestartUnbA.lean new file mode 100644 index 000000000..7ba6193a2 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScRestartUnbA.lean @@ -0,0 +1,270 @@ +/- +P3 (AArch64): the sporadic unblock from the relaxed relation `SchedSRB … p0` (released IPC +queues at every TCB but `p0`), the siblings of `SchedSRA.unblock_checkA` and +`unblock_sporadic_SRA` that `sched.restart` needs after its `Restart` write. + +* `unblock_invB`: `refill_unblock_check` keeps `ScInvA` and `RdExA` (the SRA proof, `RdExA` for + `RdA`). +* `SchedSRB.rebuildSB`, `SchedSRB.unblock_checkB`, `unblock_sporadic_SRB`: the SRA statements + with `SchedSRB … p0` before and after; frame `UnbFrA` unchanged. + +No named premise. +-/ +import Fl4ProofsA64.P3.ScTailA +import Fl4ProofsA64.P3.ScPostA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section Spec +open Fl4SpecA64 + +/-- **`refill_unblock_check` of an active SC keeps `ScInvA` and `RdExA`** (x86 `Inv.unblock`'s `rd`, `refills`, +`rr`, `ntfnSc` parts; with `SchedSRA`'s `RefOKA` the merge cannot wrap, ruling 23:14Z item 3). -/ +theorem unblock_invB {m : McState} {σ σ' : State Unit} (hI : ScInvA m σ) {p0 : ObjRef} (hR : RdExA σ p0) {p : ObjRef} + {y : SchedContext} {n : Nat} (hk : σ.kheap p = some (.schedContext y n)) (ha : scActive y = true) + (hnr : refillReady σ.curTime (refillHd y) = true → y.scPeriod ≠ 0 → + ∀ u ∈ σ.releaseQueue, ∀ x, σ.kheap u = some (.tcb x) → x.tcbSchedContext ≠ some p) {r : Unit} + (hm : (r, σ') ∈ (refillUnblockCheck (Z := Unit) p σ).1) : ScInvA m σ' ∧ RdExA σ' p0 := by + have hne : y.scRefills ≠ [] := hI.ne hk ha + rcases unblock_resultA hk hne hm with rfl | ⟨z, rfl, hzR, hzt, hzy, hper, hrdy⟩ + · exact ⟨hI, hR⟩ + have hkp : ∀ q, q ≠ p → (updKSc { σ with reprogramTimer := true } p (.schedContext z n)).kheap q = σ.kheap q := + fun q hq => by simp [updKSc, hq] + have hkz : (updKSc { σ with reprogramTimer := true } p (.schedContext z n)).kheap p = some (.schedContext z n) := + updKSc_same _ _ _ + have hzact : scActive z = scActive y := by rw [hzy] + have hzper : z.scPeriod = y.scPeriod := by rw [hzy] + have hzOK : RefOKA z := hzR (hI.refills p y n hk ha hper) + have back : ∀ q o, (updKSc { σ with reprogramTimer := true } p (.schedContext z n)).kheap q = some o → + (∀ w k, o ≠ .schedContext w k) → σ.kheap q = some o := fun q o hq ho => by + by_cases e : q = p + · subst e; rw [hkz] at hq; cases hq; exact absurd rfl (ho z n) + · rw [hkp q e] at hq; exact hq + have hnr' := hnr hrdy hper + have hRQ := hI.relKeep (σ' := updKSc { σ with reprogramTimer := true } p (.schedContext z n)) rfl + (fun u _ x hx => ⟨x, by rw [hkp u (fun e => by rw [e, hk] at hx; cases hx)]; exact hx, rfl⟩) + (fun u hu x q hx hq => hkp q (fun e => hnr' u hu x hx (e ▸ hq))) + refine ⟨⟨fun q w k hw => ?_, fun q w k hw => ?_, fun q x hx => ?_, fun q x hx => ?_, hRQ.1, hRQ.2, + fun u x q hx hq => ?_, fun u x q hx hq => hI.tcbScNotIdle u x q (back u _ hx (fun _ _ h => by cases h)) hq⟩, + fun u x hu hx hc => ?_⟩ + · by_cases e : q = p + · subst e; rw [hkz] at hw; cases hw; intro _ _; exact hzOK + · rw [hkp q e] at hw; exact hI.refills q w k hw + · by_cases e : q = p + · subst e; rw [hkz] at hw; cases hw; intro _ hp0; exact absurd (hzper.symm.trans hp0) hper + · rw [hkp q e] at hw; exact hI.rr q w k hw + · exact hI.ntfnIdle q x (back q _ hx (fun _ _ h => by cases h)) + · exact hI.ntfnIdleOf q x (back q _ hx (fun _ _ h => by cases h)) + · have hx0 := back u _ hx (fun _ _ h => by cases h) + obtain ⟨w, k, hw, hb⟩ := hI.back u x q hx0 hq + by_cases e : q = p + · subst e; rw [hk] at hw; cases hw + exact ⟨z, n, hkz, by rw [hzy]; exact hb⟩ + · exact ⟨w, k, by rw [hkp q e]; exact hw, hb⟩ + · have hx0 := back u _ hx (fun _ _ h => by cases h) + intro x' scp w k h1 h2 h3 h4 h5 + rw [hx] at h1; cases h1 + by_cases e : scp = p + · subst e; rw [hkz] at h3; cases h3 + exact ⟨ready_nowA _ _ hzt, sufficient_of_refOKA hzOK⟩ + · rw [hkp scp e] at h3 + exact hR u x hu hx0 hc x scp w k hx0 h2 h3 h4 h5 + + +/-- **`refill_unblock_check` changes only the refills of an SC** (the other SC fields, hence `scReplies`, +`scYieldFrom`, `scRefillMax`, are kept). -/ +def ScRefOnlyA (σ σ' : State Unit) : Prop := + ∀ q y n, σ.kheap q = some (.schedContext y n) → + ∃ y', σ'.kheap q = some (.schedContext y' n) ∧ y' = { y with scRefills := y'.scRefills } + +theorem ScRefOnlyA.refl (σ : State Unit) : ScRefOnlyA σ σ := fun _ y _ h => ⟨y, h, rfl⟩ + +theorem unblockRefOnlyA {σ σ' : State Unit} {p : ObjRef} {y : SchedContext} {n : Nat} + (hk : σ.kheap p = some (.schedContext y n)) (hne : y.scRefills ≠ []) {r : Unit} + (hm : (r, σ') ∈ (refillUnblockCheck (Z := Unit) p σ).1) : ScRefOnlyA σ σ' := by + rcases unblock_resultA hk hne hm with rfl | ⟨z, rfl, -, -, hzy, -, -⟩ + · exact ScRefOnlyA.refl _ + · intro q w k hq + by_cases e : q = p + · subst e + rw [hk] at hq + cases hq + exact ⟨z, updKSc_same _ _ _, hzy⟩ + · exact ⟨w, by simpa [updKSc, e] using hq, rfl⟩ + +end Spec + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +theorem SchedSRB.rebuildSB {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s s' : S} {σ σ' : Fl4SpecA64.State Unit} + {p0 : Fl4SpecA64.ObjRef} (H : SchedSRB st Z m κ s σ p0) {Q' : U64 → U64 → List U64} + (hks : L.ksb s' = L.ksb s) (hcpu : L.cpu s' = L.cpu s) + (hnode : ∀ c ∈ Z.Cs, ∀ o : U64, o.val < 120 → o.val % 8 = 0 → o.val ≠ 16 → o.val ≠ 56 → + L.mem s' (L.nodeAt s c o) = L.mem s (L.nodeAt s c o)) + (hglob : ∀ K : U64, (K = 8#u64 ∨ K = 16#u64) → L.mem s' (wa (L.ksb s) K) = L.mem s (wa (L.ksb s) K)) + (htf : ∀ t ∈ Z.T, ∀ K : U64, (K = 3520#u64 ∨ K = 3448#u64 ∨ K = 3432#u64 ∨ K = 3464#u64) → + L.mem s' (wa t K) = L.mem s (wa t K)) + (hscS : ScStateRelA st Z.C m s' σ') (hscP : ScPlacedA Z.th (L.ksb s) Z.C m σ') + (htcb : ∀ t ∈ Z.T, ∀ x, σ.kheap (specPtr t) = some (.tcb x) → ∃ y, σ'.kheap (specPtr t) = some (.tcb y) ∧ + y.tcbSchedContext = x.tcbSchedContext ∧ y.tcbDomain = x.tcbDomain ∧ y.tcbPriority = x.tcbPriority) + (hfix : SFixA σ { σ' with reprogramTimer := σ.reprogramTimer }) + (hrepr : σ'.reprogramTimer = decide (L.mem s' (L.nodeAt s (L.cpu s) layout.node.REPROGRAM) ≠ 0#u64)) + (hact : σ'.schedulerAction = actOfA (L.mem s' (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION))) + (hactOn : ∀ u, σ'.schedulerAction = .switchThread (specPtr u) → u ∈ Z.T ∧ L.mem s (affA u) = L.cpu s) + (hrd : RdExA σ' p0) (hinv : ScInvA m σ') + (hcore : SchedCoreA st Z.T Z.Cs s' Q' Z.R) + (hrq : ∀ p : U64, p.val < 256 → σ'.readyQueues 0 (BitVec.ofNat 8 p.val) = (Q' (L.cpu s) p).map specPtr) + (hrqd : ∀ d p, d ≠ 0 → σ'.readyQueues d p = []) + (hrel : σ'.releaseQueue = σ.releaseQueue) (hrun : RunQA σ') + (hscCore : ∀ c ∈ Z.C, L.mem s' (scCoreA c) = L.mem s (scCoreA c)) : + SchedSRB st { Z with Q := Q' } m κ s' σ' p0 := + ⟨H.toSchedSRN.rebuildSC hks hcpu hnode hglob htf hscS hscP htcb hfix hrepr hact hactOn + hinv hcore hrq hrqd hrel hrun hscCore, hrd⟩ + +/-- **`unblock_check` on `SchedSRB`** (ruling 23:14Z gen-5 item 1; gen 4's `unblock_check_refinesA` lifted): the +single outcome of `refill_unblock_check`, `SchedSRA` after, the frame `UnbFrA`. -/ +theorem SchedSRB.unblock_checkB {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} {σ : Fl4SpecA64.State Unit} + {p0 : Fl4SpecA64.ObjRef} (H : SchedSRB st Z m κ s σ p0) {sc : U64} (hc : sc ∈ Z.C) {x : Fl4SpecA64.SchedContext} {n : Nat} + (hk : σ.kheap (scNameA m sc) = some (.schedContext x n)) (ha : Fl4SpecA64.scActive x = true) + (hnr : Fl4SpecA64.refillReady σ.curTime (Fl4SpecA64.refillHd x) = true → x.scPeriod ≠ 0 → + ∀ u ∈ σ.releaseQueue, ∀ y, σ.kheap u = some (.tcb y) → y.tcbSchedContext ≠ some (scNameA m sc)) : + sporadic.unblock_check st s sc ⦃ s' => ∃ σ', Fl4SpecA64.refillUnblockCheck (Z := Unit) (scNameA m sc) σ = + ({((), σ')}, False) ∧ SchedSRB st Z m κ s' σ' p0 ∧ UnbFrA (st := st) Z m s s' σ σ' ∧ ScRefOnlyA σ σ' ⦄ := by + obtain ⟨hal, hout⟩ := H.reprA + refine WP.spec_mono (unblock_check_refinesA H.scr H.scp hc hk ha H.sch.time hal hout) ?_ + rintro s' ⟨σ', hsp, hS, hP, hw, hcc, ⟨e1, e2⟩ | ⟨y, -, rfl, hrep⟩⟩ + · rw [e1] at hsp; rw [e2]; exact ⟨σ, hsp, H, UnbFrA.refl Z m s σ, ScRefOnlyA.refl σ⟩ + obtain ⟨hI', hR'⟩ := unblock_invB H.inv H.rdEx hk ha hnr (r := ()) (by rw [hsp]; exact Set.mem_singleton _) + have hw' : WritesIn st s s' (fun b => ScFootA Z.C m σ b ∨ b = L.nodeAt s (L.cpu s) layout.node.REPROGRAM) := + hw.mono fun b hb => hb.imp (fun h => ⟨sc, hc, x, n, hk, h⟩) id + have hr56 : layout.node.REPROGRAM.val = 56 := by unfold layout.node.REPROGRAM; rfl + obtain ⟨nK, gK, tK⟩ := H.keepW hw' (by rw [hr56]; omega) (fun b hb => hb) + have hne : ∀ q o, σ.kheap q = some o → (∀ y n, o ≠ .schedContext y n) → q ≠ scNameA m sc := + fun q o hq ho e => by rw [e, hk] at hq; cases hq; exact ho x n rfl + have hobj : ∀ q o, σ.kheap q = some o → (∀ y n, o ≠ .schedContext y n) → + (({ updKSc σ (scNameA m sc) (.schedContext y n) with reprogramTimer := true } : + Fl4SpecA64.State Unit)).kheap q = some o := + fun q o hq ho => by simp [updKSc, hne q o hq ho, hq] + have tw : ∀ t ∈ Z.T, ∀ K : U64, (K = 3520#u64 ∨ K = 3448#u64 ∨ K = 3432#u64 ∨ K = 3464#u64 ∨ K = 3592#u64 ∨ + K = 3480#u64 ∨ K = 3488#u64) → L.mem s' (wa t K) = L.mem s (wa t K) := fun t ht K hK => + tK t ht K (by rcases hK with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> simp) + (by rcases hK with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> simp) + have hcore : SchedCoreA st Z.T Z.Cs s' Z.Q Z.R := + H.core.agree hw.1 (fun u hu => ⟨tw u hu 3480#u64 (by simp), tw u hu 3488#u64 (by simp)⟩) + (fun c hc o h1 h2 h3 => nK c hc o h1 h2 (by rw [hr56]; omega)) (fun u hu => tw u hu 3592#u64 (by simp)) + (fun u hu => tw u hu 3520#u64 (by simp)) (fun u hu => tw u hu 3448#u64 (by simp)) + have hkt : ∀ q u, σ.kheap q = some (.tcb u) → + (({ updKSc σ (scNameA m sc) (.schedContext y n) with reprogramTimer := true } : + Fl4SpecA64.State Unit)).kheap q = some (.tcb u) := fun q u hq => hobj q _ hq (fun _ _ h => by cases h) + refine ⟨_, hsp, ?_, ⟨hw', hobj, rfl, rfl, rfl, rfl, rfl, SSzA.of_upd hk rfl⟩, + unblockRefOnlyA hk (H.inv.ne hk ha) (r := ()) (by rw [hsp]; exact Set.mem_singleton _)⟩ + refine H.rebuildSB (Q' := Z.Q) hw.1 hw.2.1 (fun c hc o h1 h2 h3 h4 => nK c hc o (by omega) h2 (by rw [hr56]; exact h4)) + (fun K hK => gK K (by rcases hK with rfl | rfl <;> simp) (by rcases hK with rfl | rfl <;> simp)) + (fun t ht K hK => tw t ht K (by rcases hK with h | h | h | h <;> simp [h])) hS hP + (fun t ht u hu => ⟨u, hkt _ u hu, rfl, rfl, rfl⟩) ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ + (by show true = _; rw [hrep]; simp) + (by + show σ.schedulerAction = _ + rw [nK _ H.sch.cpuCs layout.node.SCHED_ACTION (by simp [layout.node.SCHED_ACTION]) + (by simp [layout.node.SCHED_ACTION]) (by rw [hr56]; simp [layout.node.SCHED_ACTION])] + exact H.sch.act) + H.actOn hR' hI' hcore (fun p hp => H.rest.rq p hp) H.rest.rqDom rfl + ⟨fun d p u hu => let ⟨w, h1, h2⟩ := H.run.1 d p u hu; ⟨w, hkt _ w h1, h2⟩, + fun u hu => let ⟨w, h1, h2⟩ := H.run.2 u hu; ⟨w, hkt _ w h1, h2⟩⟩ + (fun c' hc' => by + by_cases e : c' = sc + · rw [e]; exact hcc + obtain ⟨y', k, hky, hy⟩ := H.scr.sc c' hc' + have h16 := hy.hdr (K := 16#u64) (by simp) (by simp) + have hcE : scCoreA c' = scWA c' 16#u64 := by unfold scCoreA scWA layout.sc.CORE; rfl + refine hw.mem_eq (H.scCoreFoot hc').1 (fun hb => hb.elim (fun ho => ?_) (fun eb => ?_)) + · rw [hcE] at ho; exact H.scp.disj c' hc' sc hc e y' k x n hky hk _ (hy.in_obj h16.2) ho + · exact hout _ (H.scCoreFoot hc').2 (by rw [eb])) + + +theorem SchedSRB.curScWB {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} {σ : Fl4SpecA64.State Unit} + {p0 : Fl4SpecA64.ObjRef} (H : SchedSRB st Z m κ s σ p0) : + σ.curSc = scNameA m (L.mem s (L.nodeAt s (L.cpu s) layout.node.CUR_SC)) := by + rw [← H.sch.csc, H.sch.core]; exact H.sch.curScOfW _ H.sch.cpuCs + +/-- **`unblock_sporadic` on `SchedSRB`** (x86 `unblock_sporadic_refines`; module docstring). -/ +theorem unblock_sporadic_SRB {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} {σ : Fl4SpecA64.State Unit} + {p0 : Fl4SpecA64.ObjRef} (H : SchedSRB st Z m κ s σ p0) {t : U64} (ht : t ∈ Z.T) (asst : Bool) + (hrq : specPtr t ∈ σ.releaseQueue → ∀ y n, σ.kheap (scNameA m (L.mem s (scA t))) = some (.schedContext y n) → + Fl4SpecA64.refillReady σ.curTime (Fl4SpecA64.refillHd y) = false) : + notification.unblock_sporadic st s t ⦃ s' => ∃ σ', + Fl4SpecA64.ifCondRefillUnblockCheck (Z := Unit) (optScA m (L.mem s (scA t))) (some false) (some asst) σ = + ({((), σ')}, False) ∧ SchedSRB st Z m κ s' σ' p0 ∧ UnbFrA (st := st) Z m s s' σ σ' ∧ ScRefOnlyA σ σ' ⦄ := by + unfold notification.unblock_sporadic + refine WP.spec_bind (obj.Tcb.sc.spec s t) ?_ + rintro i rfl + rw [sc_sporadic_eq] + simp only [bind_ok] + by_cases h0 : L.mem s (scA t) = 0#u64 + · rw [if_pos h0] + simp only [Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + refine ⟨σ, ?_, H, UnbFrA.refl Z m s σ, ScRefOnlyA.refl σ⟩ + rw [h0, show optScA m 0#u64 = none by simp [optScA]]; rfl + rw [if_neg h0] + have hscC := H.rest.scMem t ht h0 + obtain ⟨x, n, hk, hx⟩ := H.scr.sc _ hscC + have hopt : optScA m (L.mem s (scA t)) = some (scNameA m (L.mem s (scA t))) := by + unfold optScA; rw [if_neg h0]; rfl + rw [hopt, ifCond_eqA hk asst] + have esp : sporA (L.mem s (scA t)) = scWA (L.mem s (scA t)) 64#u64 := by + unfold sporA scWA layout.sc.SPORADIC; rfl + rw [esp] + by_cases hsp : L.mem s (scWA (L.mem s (scA t)) 64#u64) ≠ 0#u64 + · have hb : (L.mem s (scWA (L.mem s (scA t)) 64#u64) != 0#u64) = true := by simpa using hsp + rw [hb] + simp only [↓reduceIte] + refine WP.spec_bind (sched.cur_sc.spec s) ?_ + rintro s1 rfl + rw [sc_ne_eqA] + simp only [bind_ok] + have hxs : x.scSporadic = true := by rw [hx.sporadic]; simpa using hsp + by_cases e : L.mem s (scA t) = L.mem s (L.nodeA s layout.node.CUR_SC) + · rw [decide_eq_false (fun h => h e)] + simp only [Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + refine ⟨σ, ?_, H, UnbFrA.refl Z m s σ, ScRefOnlyA.refl σ⟩ + rw [if_neg (fun h => h.2 (by rw [H.curScWB]; exact congrArg (scNameA m) e))] + · rw [decide_eq_true e] + simp only [↓reduceIte] + have ha : Fl4SpecA64.scActive x = true := by + have h1 := hx.spor hsp + rw [hx.max] at h1 + simp [Fl4SpecA64.scActive, Fl4SpecA64.activeSc]; omega + have hnr : Fl4SpecA64.refillReady σ.curTime (Fl4SpecA64.refillHd x) = true → x.scPeriod ≠ 0 → + ∀ u ∈ σ.releaseQueue, ∀ y, σ.kheap u = some (.tcb y) → + y.tcbSchedContext ≠ some (scNameA m (L.mem s (scA t))) := by + intro hrd _ u hu y hy hys + obtain ⟨xt, hkt, hsct⟩ := H.sch.tcb t ht + obtain ⟨w1, k1, hw1, hb1⟩ := H.inv.back u y _ hy hys + obtain ⟨w2, k2, hw2, hb2⟩ := H.inv.back _ xt _ hkt (hsct.trans hopt) + rw [hw1] at hw2; cases hw2 + rw [hb1] at hb2; cases hb2 + rw [hrq hu x n hk] at hrd; cases hrd + refine WP.spec_mono (H.unblock_checkB hscC hk ha hnr) ?_ + rintro s' ⟨σ', h1, H', F, G⟩ + refine ⟨σ', ?_, H', F, G⟩ + rw [if_pos ⟨hxs, fun h => e (H.scp.inj _ hscC _ H.cscC (h.trans H.curScWB))⟩, h1] + · have hb : (L.mem s (scWA (L.mem s (scA t)) 64#u64) != 0#u64) = false := by rw [not_not.1 hsp]; simp + rw [hb] + simp only [Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + refine ⟨σ, ?_, H, UnbFrA.refl Z m s σ, ScRefOnlyA.refl σ⟩ + rw [if_neg (fun h => by rw [hx.sporadic] at h; exact hsp (by simpa using h.1))] + + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScResumeA.lean b/proofs/Fl4ProofsA64/P3/ScResumeA.lean new file mode 100644 index 000000000..6b7cd1775 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScResumeA.lean @@ -0,0 +1,176 @@ +/- +P3 (aarch64, A64-P3-MCS gen 6): **`sched_context_resume`** on the donation block's relation (ruling 23:44Z gen-6 +item 1; x86 `P3/ScResume`'s `resume_spec_eq` / `resume_refines`, ported). + +* `sufficient_of_minA` / **`ScInvA.suff`**: with `ScInvA` (`RefOKA` / `RRokA`: the head holds `MIN_BUDGET`) an active + SC's head refill is always sufficient, so `resume` postpones exactly when the head is not ready. + (gen 8) Only for a non-idle SC (`NotIdleScA`, ruling 00:50Z: `ScInvA.rr` excludes idle SCs); `resume_refinesA` + takes `hni`, which `maybe_donate_localA` derives from `ScInvA.ntfnIdle` / `ntfnIdleOf`. +* **`resume_spec_eqA`** (x86 `resume_spec_eq`): l4v `sched_context_resume` of a bound SC. +* **`resume_refinesA`** (x86 `resume_refines`): FL4's `schedcontext::resume sc` of an SC bound both ways to a local + runnable thread `t` in no queue refines `sched_context_resume`: single outcome, heap / action / clock / ready + queues kept, and **`SchedSRA`** (with `rd`) after, from the donation block's `SchedSRB` (ruling 23:44Z item 1: the + relation relaxing `rd` at `t`); when `t` ends in the release queue its SC's head is not ready (for the sporadic + unblock that follows in `send_signal`). + +No named premise. Hypotheses: as `postpone_refinesA`, + `hst` (`StEqA`), `hmb` (the platform's `MIN_BUDGET`, as +`sufficient_refinesA`). +-/ +import Fl4ProofsA64.P3.ScPostA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +section Spec +open Fl4SpecA64 Fl4Spec + +theorem sufficient_of_minA {y : SchedContext} (h : MIN_BUDGET ≤ (refillHd y).rAmount) : + refillSufficient 0 (refillHd y) = true := by + unfold refillSufficient refillCapacity + have h0 : ¬ (refillHd y).rAmount < 0 := by rw [BitVec.lt_def]; simp + simp only [h0, ↓reduceIte, decide_eq_true_eq] + have e : (refillHd y).rAmount - 0 = (refillHd y).rAmount := by bv_omega + rw [e]; exact h + +/-- An active SC's head refill is sufficient (`RefOKA` / `RRokA` hold `MIN_BUDGET`). -/ +theorem ScInvA.suff {m : McState} {σ : State Unit} (h : ScInvA m σ) {p : ObjRef} {y : SchedContext} {n : Nat} + (hk : σ.kheap p = some (.schedContext y n)) (ha : scActive y = true) (hni : NotIdleScA m p) : + refillSufficient 0 (refillHd y) = true := by + by_cases hp : y.scPeriod = 0 + · exact sufficient_of_minA ((h.rr p y n hk ha hp).2 hni).2.2 + · exact sufficient_of_minA (h.refills p y n hk ha hp).2.2.2 + +/-- l4v `sched_context_resume` of a bound SC (x86 `resume_spec_eq`). -/ +theorem resume_spec_eqA {σ : State Unit} {p t : ObjRef} {x : SchedContext} {n : Nat} {xt : Tcb} + (hk : σ.kheap p = some (.schedContext x n)) (htc : x.scTcb = some t) (hkt : σ.kheap t = some (.tcb xt)) + (hne : scActive x = true → x.scRefills ≠ []) + (hrun : schedulable t σ = true → runnable xt.tcbState = true ∧ scActive x = true) + (hnq : t ∉ σ.readyQueues xt.tcbDomain xt.tcbPriority) : + schedContextResume (Z := Unit) p σ = + if schedulable t σ = true ∧ ¬(refillReady σ.curTime (refillHd x) = true ∧ + refillSufficient 0 (refillHd x) = true) then postpone p σ else ({((), σ)}, False) := by + unfold schedContextResume + rw [nd_bind_oneSc (getSchedContext_eqA hk), htc, + nd_bind_oneSc (show NonDet.assertOpt (σ := State Unit) (some t) σ = ({(t, σ)}, False) from rfl), nd_gets_bindSc] + by_cases hs : schedulable t σ = true + · obtain ⟨h1, h2⟩ := hrun hs + simp only [NonDet.when, hs, ↓reduceIte, true_and] + rw [nd_bind_oneSc (threadGet_eqSc _ hkt), nd_bind_oneSc (getScRefillReady_eqA hk (hne h2)), + nd_bind_oneSc (getScRefillSufficient_eqA hk (hne h2) 0)] + by_cases hc : refillReady σ.curTime (refillHd x) = true ∧ refillSufficient 0 (refillHd x) = true + · rw [if_neg (fun h => h.2.2 hc), if_neg (fun h => h hc)]; rfl + · rw [if_pos ⟨h1, h2, hc⟩, if_pos hc] + unfold getTcbQueue + rw [nd_bind_oneSc (threadGet_eqSc _ hkt), nd_bind_oneSc (threadGet_eqSc _ hkt), nd_bind_oneSc (nd_gets_bindSc _ _ σ), + nd_bind_oneSc (assert_trueSc hnq σ)] + · simp only [NonDet.when, hs, Bool.false_eq_true, ↓reduceIte, false_and]; rfl + +end Spec + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +/-- **`resume` refines `sched_context_resume`** (x86 `resume_refines`; module docstring). -/ +theorem resume_refinesA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} {σ : Fl4SpecA64.State Unit} + {sc t : U64} (H : SchedSRB st Z m κ s σ (specPtr t)) (a : A) + (hmb : sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) (hsc : sc ∈ Z.C) (ht : t ∈ Z.T) + (hon : L.mem s (affA t) = L.cpu s) (htw : L.mem s (scTcbA sc) = t) (hts : L.mem s (scA t) = sc) + (hsc0 : sc ≠ 0#u64) (hcpu : L.mem s (scCoreA sc) = L.cpu s) (hni : NotIdleScA m (scNameA m sc)) + (hq0 : (L.mem s (inqA t) &&& sched.Q_QUEUED) = 0#u64) (hr0 : (L.mem s (inqA t) &&& sched.Q_RELEASE) = 0#u64) + (hnqs : ∀ d p, specPtr t ∉ σ.readyQueues d p) + (hrunt : ∀ x, σ.kheap (specPtr t) = some (.tcb x) → Fl4SpecA64.runnable x.tcbState = true) + (hst : StEqA Z.T (L.mem s) σ) : + schedcontext.resume st ar s a sc ⦃ r => r.2 = a ∧ ∃ σ' R', + Fl4SpecA64.schedContextResume (Z := Unit) (scNameA m sc) σ = ({((), σ')}, False) ∧ + σ'.kheap = σ.kheap ∧ σ'.schedulerAction = σ.schedulerAction ∧ σ'.curTime = σ.curTime ∧ + σ'.readyQueues = σ.readyQueues ∧ + (specPtr t ∈ σ'.releaseQueue → ∀ y n, σ.kheap (scNameA m sc) = some (.schedContext y n) → + Fl4SpecA64.refillReady σ.curTime (Fl4SpecA64.refillHd y) = false) ∧ + (∀ y ∈ Z.T, L.mem r.1 (stA y) = L.mem s (stA y)) ∧ + WritesIn st s r.1 (fun b => L.QFootN s (t :: Z.R (L.cpu s)) (L.cpu s) RelOffs t b ∨ b = L.nodeAt s (L.cpu s) layout.node.REPROGRAM) ∧ SchedSRA st { Z with R := R' } m κ r.1 σ' ⦄ := by + have hrq : specPtr t ∉ σ.releaseQueue := (H.relOn ht hon).1 hr0 + have ht0 : t ≠ 0#u64 := fun e => (H.core.tcbs t ht).ne0 (by rw [e]; rfl) + obtain ⟨xt, hkt, hsct⟩ := H.sch.tcb t ht + obtain ⟨xs, ns, hks, hxs⟩ := H.scr.sc sc hsc + have hsct' : xt.tcbSchedContext = some (scNameA m sc) := by + rw [hsct, hts]; unfold optScA; rw [if_neg hsc0]; rfl + have htc : xs.scTcb = some (specPtr t) := by rw [hxs.tcb, ← scTcbA_eqSc, htw, optRefS_ne ht0] + have hrunx := hrunt xt hkt + have hfl : L.mem s (stA t) = sched.TS_RUNNING ∨ L.mem s (stA t) = sched.TS_RESTART ∨ + L.mem s (stA t) = sched.TS_RUNNING_VM := (runnable_iffA (hst t ht xt hkt)).2 hrunx + have hsched := schedulable_eqA H.toSchedSRN ht (H.relOn ht hon) hst + have hpos : L.rmax s (L.mem s (scA t)) > 0#u64 ↔ Fl4SpecA64.scActive xs = true := by + rw [hts, gt_iff_valSc] + simp only [Fl4SpecA64.scActive, Fl4SpecA64.activeSc, decide_eq_true_eq, ← hxs.max] + simp + have hsa : Fl4SpecA64.schedulable (specPtr t) σ = Fl4SpecA64.scActive xs := by + rw [← hsched] + by_cases ha : Fl4SpecA64.scActive xs = true + · rw [ha]; exact decide_eq_true ⟨hfl, by rw [hts]; exact hsc0, hpos.2 ha, hr0⟩ + · rw [Bool.not_eq_true] at ha; rw [ha] + exact decide_eq_false (fun ⟨_, _, h3, _⟩ => by rw [hpos.1 h3] at ha; cases ha) + have hspec := resume_spec_eqA hks htc hkt (fun ha => H.inv.ne hks ha) + (fun hs => ⟨hrunx, by rw [← hsa]; exact hs⟩) (hnqs _ _) + have hrib : (∀ y n, σ.kheap (scNameA m sc) = some (.schedContext y n) → Fl4SpecA64.scActive y = true → + releasedSc σ y) → RelIfBoundSc σ (specPtr t) := by + intro h x scp y n h1 h2 h3 h4 _ + rw [hkt] at h1; cases h1 + rw [hsct'] at h2; cases h2 + exact h y n h3 h4 + unfold schedcontext.resume + refine WP.spec_bind (obj.Sc.is_null.spec sc) ?_ + rintro b rfl + rw [decide_eq_false hsc0] + simp only [Bool.false_eq_true, ↓reduceIte] + refine WP.spec_bind (obj.Sc.tcb.spec s sc) ?_ + rintro i rfl + rw [htw] + refine WP.spec_bind (obj.Tcb.is_null.spec t) ?_ + rintro b rfl + rw [decide_eq_false ht0] + simp only [Bool.false_eq_true, ↓reduceIte] + rw [is_schedulable_eqA, bind_ok, hsched, hsa] + by_cases ha : Fl4SpecA64.scActive xs = true + · rw [if_pos ha] + have hne := H.inv.ne hks ha + have hsf := H.inv.suff hks ha hni + refine WP.spec_bind (ready_refinesA H.scr H.sch.time hsc hks ha) ?_ + intro b3 hb3 + rw [getScRefillReady_eqA hks hne] at hb3 + have eb3 : Fl4SpecA64.refillReady σ.curTime (Fl4SpecA64.refillHd xs) = b3 := + (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst hb3))).1 + by_cases h3 : b3 = true + · rw [if_pos h3] + refine WP.spec_bind (sufficient_refinesA a H.scr hsc hks ha 0#u64 hmb) ?_ + intro b4 hb4 + rw [getScRefillSufficient_eqA hks hne] at hb4 + have eb4 : Fl4SpecA64.refillSufficient (0#u64 : U64).bv (Fl4SpecA64.refillHd xs) = b4 := + (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst hb4))).1 + have h4 : b4 = true := by rw [← eb4]; exact hsf + rw [if_pos h4, WP.spec_ok] + refine ⟨rfl, σ, Z.R, ?_, rfl, rfl, rfl, rfl, fun h => absurd h hrq, fun _ _ => rfl, WritesIn.refl _ _, + H.toA (hrib fun y n hy _ => ?_)⟩ + · rw [hspec, if_neg (fun h => h.2 ⟨eb3.trans h3, hsf⟩)] + · rw [hks] at hy; cases hy; exact ⟨eb3.trans h3, hsf⟩ + · rw [if_neg h3] + have hta : (L.rmax s sc).val ≠ 0 := by + rw [hxs.max]; simp [Fl4SpecA64.scActive, Fl4SpecA64.activeSc] at ha; omega + refine WP.spec_bind (postpone_refinesA H hsc ht hon htw hts hsc0 hcpu hq0 hr0 hta hnqs hrunt) ?_ + rintro s1 ⟨σ', R', hp, hk', ha', hct', hrqs', -, -, hst1, hw1, H'⟩ + rw [WP.spec_ok] + refine ⟨rfl, σ', R', ?_, hk', ha', hct', hrqs', fun _ y n hy => ?_, hst1, hw1, H'⟩ + · rw [hspec, if_pos ⟨by rw [hsa]; exact ha, fun h => h3 (eb3.symm.trans h.1)⟩]; exact hp + · rw [hks] at hy; cases hy; rw [eb3]; simpa using h3 + · rw [if_neg ha, WP.spec_ok] + refine ⟨rfl, σ, Z.R, ?_, rfl, rfl, rfl, rfl, fun h => absurd h hrq, fun _ _ => rfl, WritesIn.refl _ _, + H.toA (hrib fun y n hy hay => ?_)⟩ + · rw [hspec, if_neg (fun h => ha (by rw [← hsa]; exact h.1))] + · rw [hks] at hy; cases hy; exact absurd hay ha +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScRetA.lean b/proofs/Fl4ProofsA64/P3/ScRetA.lean new file mode 100644 index 000000000..906ed4127 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScRetA.lean @@ -0,0 +1,376 @@ +/- +P3 (aarch64, A64-P3-MCS gen 9): **`maybe_return_sched_context` refines `maybe_return_sc`** on `SchedSRA` (ruling +01:09Z gen-9 item 1; x86 `P3/ScReturnRef`'s `maybe_return_refines`, the IPC line's `SchedOpsA.returnSc`). + +* **`SKeepA σ σ'`** (the spec frame of the MCS tails, ruling item 2; gen 10: moved to `P3/ScSWrA`): every object that is neither an SC nor a TCB is + kept, and every TCB is kept up to its `tcbSchedContext` (this gives the IPC relations' `KView`s' heap parts: + notifications, endpoints, replies, and each TCB's state / registers / priority / fault / bound notification). +* **`SchedSRA.unbindA`** (x86 `SchedRel.set_tcb_sc` + `set_sc_tcb` with `v = 0`, the mirror of gen 7's `donateB`): + `t.SC := 0` then `sc.TCB := 0` of a thread `t` bound to `sc` and in no release queue keeps `SchedSRA` (same `Z`), + the spec unbinding both (`unbStA`). +* **`scRenA_eq_of`**: two SC words with the same spec name, one of them not named `idle_sc_ptr`, are equal (used for + the notification's SC word, which need not be a related SC: `ScInvA.ntfnIdle` excludes the collision). +* **`maybe_return_localA`** (x86 `maybe_return_refines`): for `t ∈ Z.T` not runnable (blocked: IPC's `BlockStep`), + FL4's `maybe_return_sched_context n t` against l4v `maybe_return_sc n t`: single spec outcome, `SchedSRA` after + (new ready lists from `reschedule_required` when `t` is current), and (gen 10) the frame `MFrA` (`P3/ScSWrA`). + +No named premise. Hypotheses: `hst` (`StEqA`, for `reschedule_required`), `hN` (`NtfnScEqA`, a projection of +`NtfnStateRel`), `hnr` (`t` not runnable: IPC's `BlockStep` conjunct). +-/ +import Fl4ProofsA64.P3.ScWakeDonA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section Spec +open Fl4SpecA64 + +/-- The spec state after the unbinding (`tcb_sched_context := none` at `T`, then `sc_tcb := none` at `N`). -/ +def unbStA (σ : State Unit) (N T : ObjRef) (xs : SchedContext) (ns : Nat) (xt : Tcb) : State Unit := + updKSc (updKSc σ T (.tcb { xt with tcbSchedContext := none })) N (.schedContext { xs with scTcb := none } ns) + +theorem unbStA_kheap {σ : State Unit} {N T : ObjRef} {xs : SchedContext} {ns : Nat} {xt : Tcb} (hNT : N ≠ T) + (p : ObjRef) : + (unbStA σ N T xs ns xt).kheap p = if p = T then some (.tcb { xt with tcbSchedContext := none }) + else if p = N then some (.schedContext { xs with scTcb := none } ns) else σ.kheap p := by + by_cases h1 : p = T + · subst h1; simp [unbStA, updKSc, Ne.symm hNT] + · by_cases h2 : p = N + · subst h2; simp [unbStA, updKSc, h1] + · simp [unbStA, updKSc, h1, h2] + +theorem SKeepA.unb {σ : State Unit} {N T : ObjRef} {xs : SchedContext} {ns : Nat} {xt : Tcb} (hNT : N ≠ T) + (hkN : σ.kheap N = some (.schedContext xs ns)) (hkT : σ.kheap T = some (.tcb xt)) : + SKeepA σ (unbStA σ N T xs ns xt) ∧ SSzA σ (unbStA σ N T xs ns xt) := + SKeepA.two (unbStA_kheap hNT) hkN hkT + +end Spec + +/-- **Two SC words with the same spec name** (module docstring). -/ +theorem scRenA_eq_of {m : Fl4SpecA64.McState} {p q : U64} (h : scRenA m p = scRenA m q) + (hp : scRenA m p ≠ Fl4SpecA64.idleScPtr) : p = q := by + unfold scRenA at h hp + by_cases ep : specPtr p = m.idleScOf m.curCore + · rw [if_pos ep] at hp; exact absurd rfl hp + rw [if_neg ep] at h hp + by_cases eq : specPtr q = m.idleScOf m.curCore + · rw [if_pos eq] at h; exact absurd h hp + rw [if_neg eq] at h + exact specPtr_inj h + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **The unbinding step** (module docstring; mirror of `SchedSRA.donateB`). -/ +theorem SchedSRA.unbindA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} {σ : Fl4SpecA64.State Unit} + (H : SchedSRA st Z m κ s σ) {sc t : U64} (hsc : sc ∈ Z.C) (ht : t ∈ Z.T) (hsc0 : sc ≠ 0#u64) + (hts : L.mem s (scA t) = sc) (hnr : specPtr t ∉ σ.releaseQueue) + {xs : Fl4SpecA64.SchedContext} {ns : Nat} {xt : Fl4SpecA64.Tcb} + (hks : σ.kheap (scNameA m sc) = some (.schedContext xs ns)) (hkt : σ.kheap (specPtr t) = some (.tcb xt)) : + WritesIn st s (L.wr (L.wr s (scA t) 0#u64) (scTcbA sc) 0#u64) (fun b => b = scTcbA sc ∨ b = scA t) ∧ + SchedSRA st Z m κ (L.wr (L.wr s (scA t) 0#u64) (scTcbA sc) 0#u64) + (unbStA σ (scNameA m sc) (specPtr t) xs ns xt) := by + obtain ⟨x', n', hk', hx⟩ := H.scr.sc sc hsc + rw [hks] at hk'; cases hk' + have hxt : xt.tcbSchedContext = some (scNameA m sc) := by + obtain ⟨y, hy, hys⟩ := H.sch.tcb t ht + rw [hkt] at hy; cases hy; rw [hys, hts]; unfold optScA; rw [if_neg hsc0]; rfl + have hNT : scNameA m sc ≠ specPtr t := fun e => by rw [e, hkt] at hks; cases hks + have hxs : xs.scTcb = some (specPtr t) := by + obtain ⟨y, n, hy, hyu⟩ := H.inv.back _ xt _ hkt hxt + rw [hks] at hy; cases hy; exact hyu + set σ2 := unbStA σ (scNameA m sc) (specPtr t) xs ns xt with hσ2 + have hk2 : ∀ p, p ≠ specPtr t → p ≠ scNameA m sc → σ2.kheap p = σ.kheap p := fun p h1 h2 => by + rw [hσ2, unbStA_kheap hNT, if_neg h1, if_neg h2] + have hk2T : σ2.kheap (specPtr t) = some (.tcb { xt with tcbSchedContext := none }) := by + rw [hσ2, unbStA_kheap hNT, if_pos rfl] + have hk2N : σ2.kheap (scNameA m sc) = some (.schedContext { xs with scTcb := none } ns) := by + rw [hσ2, unbStA_kheap hNT, if_neg hNT, if_pos rfl] + -- every other binding avoids `t` and the SC + have hpN : ∀ u x p, σ.kheap u = some (.tcb x) → x.tcbSchedContext = some p → u ≠ specPtr t → + p ≠ specPtr t ∧ p ≠ scNameA m sc := by + intro u x p hu hp hut + obtain ⟨y, n, hy, hyu⟩ := H.inv.back u x p hu hp + refine ⟨fun e => ?_, fun e => ?_⟩ + · rw [e, hkt] at hy; cases hy + · rw [e, hks] at hy; cases hy; rw [hxs] at hyu; cases hyu; exact hut rfl + have hTk : ∀ u x, σ.kheap u = some (.tcb x) → ∃ x', σ2.kheap u = some (.tcb x') ∧ + x'.tcbState = x.tcbState ∧ x'.tcbDomain = x.tcbDomain ∧ x'.tcbPriority = x.tcbPriority := by + intro u x hu + by_cases e1 : u = specPtr t + · rw [e1, hkt] at hu; cases hu; rw [e1]; exact ⟨_, hk2T, rfl, rfl, rfl⟩ + · have e2 : u ≠ scNameA m sc := fun e => by rw [e, hks] at hu; cases hu + exact ⟨x, by rw [hk2 u e1 e2]; exact hu, rfl, rfl, rfl⟩ + have hSC : ∀ p y n, σ2.kheap p = some (.schedContext y n) → + σ.kheap p = some (.schedContext y n) ∨ (p = scNameA m sc ∧ y = { xs with scTcb := none } ∧ n = ns) := by + intro p y n hp + by_cases e1 : p = specPtr t + · rw [e1, hk2T] at hp; cases hp + · by_cases e2 : p = scNameA m sc + · rw [e2, hk2N] at hp; cases hp; exact Or.inr ⟨e2, rfl, rfl⟩ + · rw [hk2 p e1 e2] at hp; exact Or.inl hp + have hNf : ∀ q z, σ2.kheap q = some (.notification z) → σ.kheap q = some (.notification z) := by + intro q z hq + by_cases e1 : q = specPtr t + · rw [e1, hk2T] at hq; cases hq + · by_cases e2 : q = scNameA m sc + · rw [e2, hk2N] at hq; cases hq + · rwa [hk2 q e1 e2] at hq + have hinv : ScInvA m σ2 := by + have hR := H.inv.relKeep (σ' := σ2) rfl + (fun u hu x hx => ⟨x, by + rw [hk2 u (fun e => hnr (e ▸ hu)) (fun e => by rw [e, hks] at hx; cases hx)]; exact hx, rfl⟩) + (fun u hu x p hx hp => hk2 p (hpN u x p hx hp (fun e => hnr (e ▸ hu))).1 + (hpN u x p hx hp (fun e => hnr (e ▸ hu))).2) + refine ⟨fun p y n hy ha hp => ?_, fun p y n hy ha hp => ?_, fun q z hz => H.inv.ntfnIdle q z (hNf q z hz), + fun q z hz => H.inv.ntfnIdleOf q z (hNf q z hz), hR.1, hR.2, fun u x p hx hp => ?_, + fun u x p hx hp hi => ?_⟩ + · rcases hSC p y n hy with h | ⟨rfl, rfl, rfl⟩ + · exact H.inv.refills p y n h ha hp + · exact H.inv.refills _ xs _ hks ha hp + · rcases hSC p y n hy with h | ⟨rfl, rfl, rfl⟩ + · exact H.inv.rr p y n h ha hp + · exact H.inv.rr _ xs _ hks ha hp + · by_cases e1 : u = specPtr t + · rw [e1, hk2T] at hx; cases hx; cases hp + · have e2 : u ≠ scNameA m sc := fun e => by rw [e, hk2N] at hx; cases hx + rw [hk2 u e1 e2] at hx + obtain ⟨y, n, hy, hyu⟩ := H.inv.back u x p hx hp + obtain ⟨hp1, hp2⟩ := hpN u x p hx hp e1 + exact ⟨y, n, by rw [hk2 p hp1 hp2]; exact hy, hyu⟩ + · by_cases e1 : u = specPtr t + · rw [e1, hk2T] at hx; cases hx; cases hp + · have e2 : u ≠ scNameA m sc := fun e => by rw [e, hk2N] at hx; cases hx + rw [hk2 u e1 e2] at hx + exact H.inv.tcbScNotIdle u x p hx hp hi + -- the FL4 writes + have h24 := hx.hdr (K := 24#u64) (by simp) (by simp) + have hA1 : Aligned (scTcbA sc) := by rw [scTcbA_eqSc]; exact h24.1 + have hT := H.core.tcbs t ht + have hTf := hT.fits + have hTa := hT.align + have hv2 : (scA t).val = t.val + 3464 := by + show (wa t 3464#u64).val = _ + rw [wa_valm]; have : (3464#u64 : U64).val = 3464 := by simp + omega + have hA2 : Aligned (scA t) := by unfold Aligned; rw [hv2]; omega + set s' := L.wr (L.wr s (scA t) 0#u64) (scTcbA sc) 0#u64 with hs' + have hw : WritesIn st s s' (fun b => b = scTcbA sc ∨ b = scA t) := + ((writesIn_wr hA2).trans (writesIn_wr hA1)).mono fun b hb => hb.symm + obtain ⟨nK, gK, tK, sK⟩ := H.donW hsc ht hw + have hF : ScFootA Z.C m σ (scTcbA sc) := by rw [scTcbA_eqSc]; exact ⟨sc, hsc, xs, ns, hks, hx.in_obj h24.2⟩ + have hAne : (scA t).val ≠ (scTcbA sc).val := by + have := H.scp.th_out hF ((H.thT t).2 ht) + simp only [ObjKindA.size] at this + rw [hv2]; omega + obtain ⟨hS1, hP1⟩ := H.scr.set_sc_tcb H.scp hsc hks 0#u64 + rw [optRefS_zero] at hS1 hP1 + have hkc : ∀ c ∈ Z.C, σ2.kheap (scNameA m c) = + (updKSc σ (scNameA m sc) (.schedContext { xs with scTcb := none } ns)).kheap (scNameA m c) := by + intro c hc + obtain ⟨y, k, hy, -⟩ := hS1.sc c hc + have hne : scNameA m c ≠ specPtr t := fun e => by + rw [e, updKSc_kheap, Function.update_of_ne (Ne.symm hNT), hkt] at hy; cases hy + rw [hy]; rw [hσ2, unbStA_kheap hNT, if_neg hne] + by_cases e2 : scNameA m c = scNameA m sc + · rw [if_pos e2, ← hy, e2, updKSc_kheap, Function.update_self] + · rw [if_neg e2, ← hy, updKSc_kheap, Function.update_of_ne e2] + have hS2 : ScStateRelA st Z.C m s' σ2 := + hS1.frame (fun a ha hfa => by + have := hP1.th_out hfa ((H.thT t).2 ht) + simp only [ObjKindA.size] at this + have hna : scA t ≠ a := fun e => by rw [← e, hv2] at this; omega + rw [hs', L.mem_wr _ _ _ _ hA1 ha, L.mem_wr _ _ _ _ hA2 ha, if_neg hna, L.mem_wr _ _ _ _ hA1 ha]) hkc + have hP2 : ScPlacedA Z.th (L.ksb s) Z.C m σ2 := hP1.of_kheap hkc + have hst0 : L.mem s' (scA t) = 0#u64 := by + rw [hs', L.mem_wr _ _ _ _ hA1 hA2, if_neg (fun e => hAne (by rw [e])), L.mem_wr _ _ _ _ hA2 hA2, if_pos rfl] + have N2 := H.rebuild2 (σ2 := σ2) hsc ht hw ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ rfl rfl rfl hS2 hP2 hTk + (fun u hu => by + by_cases e : u = t + · subst e + refine ⟨_, hk2T, ?_⟩ + rw [hst0]; simp [optScA] + · have hne : specPtr u ≠ specPtr t := fun h => e (specPtr_inj h) + obtain ⟨x, h1, h3⟩ := H.sch.tcb u hu + have e2 : specPtr u ≠ scNameA m sc := fun h => by rw [h, hks] at h1; cases h1 + refine ⟨x, by rw [hk2 _ hne e2]; exact h1, ?_⟩ + rw [show scA u = wa u 3464#u64 from rfl, tK u hu 3464#u64 (by simp) (by simp) + (fun h => absurd h e)] + exact h3) + (fun u hu h => by + by_cases e : u = t + · subst e; rw [hst0] at h; exact absurd rfl h + · rw [show scA u = wa u 3464#u64 from rfl, tK u hu 3464#u64 (by simp) (by simp) + (fun h => absurd h e)] at h ⊢ + exact H.rest.scMem u hu h) + hinv + refine ⟨hw, N2, fun u x hx hc => ?_⟩ + intro x' scp y k h1 h2 h3 h4 h5 + by_cases e1 : u = specPtr t + · rw [e1, hk2T] at h1; cases h1; cases h2 + have huN : u ≠ scNameA m sc := fun e => by rw [e, hk2N] at hx; cases hx + rw [hk2 u e1 huN] at hx h1 + obtain ⟨hp1, hp2⟩ := hpN u x' scp h1 h2 e1 + rw [hk2 scp hp1 hp2] at h3 + exact H.rd u x hx hc x' scp y k h1 h2 h3 h4 h5 + +/-- `StEqA` survives a step keeping every state word, with the spec keeping every TCB's state. -/ +theorem StEqA.keep {T : Set U64} {s s' : S} {σ σ' : Fl4SpecA64.State Unit} (h : StEqA T (L.mem s) σ) + (hw : ∀ u ∈ T, L.mem s' (stA u) = L.mem s (stA u)) + (hk : ∀ u x, σ'.kheap u = some (.tcb x) → ∃ x0, σ.kheap u = some (.tcb x0) ∧ x0.tcbState = x.tcbState) : + StEqA T (L.mem s') σ' := fun u hu x hx => by + obtain ⟨x0, h0, e0⟩ := hk _ x hx + rw [hw u hu, ← e0]; exact h u hu x0 h0 + +end + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- SC return for any receiver absent from the release queue. -/ +theorem maybe_return_unqueuedA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} + {σ : Fl4SpecA64.State Unit} (H : SchedSRA st Z m κ s σ) (hst : StEqA Z.T (L.mem s) σ) {t n : U64} + (ht : t ∈ Z.T) (hN : NtfnScEqA m n (L.mem s) σ) + (hrq : specPtr t ∉ σ.releaseQueue) : + notification.maybe_return_sched_context st s n t ⦃ s' => ∃ σ' Q', + Fl4SpecA64.maybeReturnSc (Z := Unit) (specPtr n) (specPtr t) σ = ({((), σ')}, False) ∧ + SchedSRA st { Z with Q := Q' } m κ s' σ' ∧ MFrA (st := st) Z m s s' σ σ' ⦄ := by + obtain ⟨xt, hkt, hsct⟩ := H.sch.tcb t ht + obtain ⟨z, hkn, hzs⟩ := hN + have hspec : Fl4SpecA64.maybeReturnSc (Z := Unit) (specPtr n) (specPtr t) σ = + Fl4Spec.NonDet.when (optScA m (L.mem s (nScSc n)) = optScA m (L.mem s (scA t)) ∧ + optScA m (L.mem s (nScSc n)) ≠ none) (do + let scPtr ← Fl4Spec.NonDet.assertOpt (optScA m (L.mem s (nScSc n))) + Fl4SpecA64.setTcbObjRef Fl4SpecA64.tcbSchedContextUpdate (specPtr t) none + Fl4SpecA64.setScObjRef Fl4SpecA64.scTcbUpdate scPtr none + let ct ← Fl4Spec.NonDet.gets fun s : Fl4SpecA64.State Unit => s.curThread + Fl4Spec.NonDet.when (specPtr t = ct) Fl4SpecA64.rescheduleRequired) σ := by + unfold Fl4SpecA64.maybeReturnSc Fl4SpecA64.getTcbObjRef + rw [nd_bind_oneSc (getNtfnSc_eqSc hkn), nd_bind_oneSc (threadGet_eqSc _ hkt), hzs, hsct] + have none_out : Fl4SpecA64.maybeReturnSc (Z := Unit) (specPtr n) (specPtr t) σ = ({((), σ)}, False) → + ∃ σ' Q', Fl4SpecA64.maybeReturnSc (Z := Unit) (specPtr n) (specPtr t) σ = ({((), σ')}, False) ∧ + SchedSRA st { Z with Q := Q' } m κ s σ' ∧ MFrA (st := st) Z m s s σ σ' := + fun e => ⟨σ, Z.Q, e, H, MFrA.refl Z m s σ⟩ + unfold notification.maybe_return_sched_context + refine WP.spec_bind (ntfn_sc_specSc s n) ?_ + rintro i rfl + refine WP.spec_bind (obj.Sc.is_null.spec _) ?_ + rintro b rfl + by_cases h0 : L.mem s (nScSc n) = 0#u64 + · rw [if_pos (decide_eq_true h0), WP.spec_ok] + refine none_out ?_ + rw [hspec, h0, show optScA m 0#u64 = none by simp [optScA]] + simp [Fl4Spec.NonDet.when]; rfl + rw [if_neg (by simpa using h0)] + refine WP.spec_bind (obj.Tcb.sc.spec s t) ?_ + rintro i1 rfl + by_cases e : L.mem s (nScSc n) = L.mem s (scA t) + · rw [if_pos e, e] + obtain ⟨sc, hsc_def⟩ : ∃ sc, L.mem s (scA t) = sc := ⟨_, rfl⟩ + rw [hsc_def] + have hsc0 : sc ≠ 0#u64 := by rw [← hsc_def, ← e]; exact h0 + have hscC : sc ∈ Z.C := by rw [← hsc_def]; exact H.rest.scMem t ht (by rw [hsc_def]; exact hsc0) + obtain ⟨xs, ns, hks, -⟩ := H.scr.sc sc hscC + obtain ⟨hw, H2⟩ := H.unbindA hscC ht hsc0 hsc_def hrq hks hkt + have hNT : scNameA m sc ≠ specPtr t := fun e' => by rw [e', hkt] at hks; cases hks + have hK2 := SKeepA.unb hNT hks hkt + have F2 : MFrA (st := st) Z m s (L.wr (L.wr s (scA t) 0#u64) (scTcbA sc) 0#u64) σ + (unbStA σ (scNameA m sc) (specPtr t) xs ns xt) := + ⟨hw.mono fun b hb => H.toSchedSRN.mwr_two hscC ht hb, hK2.1, hK2.2⟩ + have hopt : optScA m sc = some (scNameA m sc) := by unfold optScA; rw [if_neg hsc0]; rfl + have hpre : (do + let scPtr ← Fl4Spec.NonDet.assertOpt (optScA m (L.mem s (nScSc n))) + Fl4SpecA64.setTcbObjRef Fl4SpecA64.tcbSchedContextUpdate (specPtr t) none + Fl4SpecA64.setScObjRef Fl4SpecA64.scTcbUpdate scPtr none + let ct ← Fl4Spec.NonDet.gets fun s : Fl4SpecA64.State Unit => s.curThread + Fl4Spec.NonDet.when (specPtr t = ct) Fl4SpecA64.rescheduleRequired) σ = + Fl4Spec.NonDet.when (specPtr t = σ.curThread) Fl4SpecA64.rescheduleRequired + (unbStA σ (scNameA m sc) (specPtr t) xs ns xt) := by + rw [e, hsc_def, hopt] + rw [nd_bind_oneSc (show Fl4Spec.NonDet.assertOpt (σ := Fl4SpecA64.State Unit) (some (scNameA m sc)) σ = + ({(scNameA m sc, σ)}, False) from rfl)] + rw [nd_bind_oneSc (threadSet_eqSc _ hkt)] + have hks1 : (updKSc σ (specPtr t) (.tcb (Fl4SpecA64.tcbSchedContextUpdate (fun _ => none) xt))).kheap + (scNameA m sc) = some (.schedContext xs ns) := by simp [updKSc, hNT, hks] + show (Fl4SpecA64.updateSchedContext (scNameA m sc) _ >>= _) _ = _ + rw [nd_bind_oneSc (updateSchedContext_eqA hks1 _), nd_gets_bindSc] + rfl + have hcond : optScA m (L.mem s (nScSc n)) = optScA m (L.mem s (scA t)) ∧ optScA m (L.mem s (nScSc n)) ≠ none := by + rw [e, hsc_def, hopt]; exact ⟨rfl, by simp⟩ + refine WP.spec_bind (set_sc_specSc s t 0#u64) ?_ + rintro s1 rfl + refine WP.spec_bind (set_tcb_specSc _ sc 0#u64) ?_ + rintro s2 rfl + refine WP.spec_bind (sched.cur_thread.spec _) ?_ + rintro t1 rfl + refine WP.spec_bind (obj.Tcb.eq.spec _ _) ?_ + rintro b1 rfl + have hcur2 : (unbStA σ (scNameA m sc) (specPtr t) xs ns xt).curThread = + specPtr (L.curW (L.wr (L.wr s (scA t) 0#u64) (scTcbA sc) 0#u64)) := H2.sch.curThread + have hcurσ : σ.curThread = (unbStA σ (scNameA m sc) (specPtr t) xs ns xt).curThread := rfl + split + · rename_i hb + have ec : t = L.curW (L.wr (L.wr s (scA t) 0#u64) (scTcbA sc) 0#u64) := of_decide_eq_true hb + have hst2 : StEqA Z.T (L.mem (L.wr (L.wr s (scA t) 0#u64) (scTcbA sc) 0#u64)) + (unbStA σ (scNameA m sc) (specPtr t) xs ns xt) := by + have hstw : ∀ u ∈ Z.T, L.mem (L.wr (L.wr s (scA t) 0#u64) (scTcbA sc) 0#u64) (stA u) = L.mem s (stA u) := by + obtain ⟨-, -, tK, -⟩ := H.donW hscC ht hw + intro u hu + rw [stA_eq, tK u hu 3328#u64 (by simp) (by simp) (fun _ => by simp), ← stA_eq] + refine StEqA.keep hst hstw (fun u x hx => ?_) + rw [unbStA_kheap hNT] at hx + by_cases e1 : u = specPtr t + · rw [if_pos e1] at hx; cases hx; exact ⟨xt, by rw [e1]; exact hkt, rfl⟩ + · rw [if_neg e1] at hx + by_cases e2 : u = scNameA m sc + · rw [if_pos e2] at hx; cases hx + · rw [if_neg e2] at hx; exact ⟨x, hx, rfl⟩ + obtain ⟨s3, hv3, σ3, Q3, hrr, hk3, -, hw3, H3⟩ := spec_ex_fd (reschedule_required_refinesA H2 hst2) + rw [hv3, WP.spec_ok] + refine ⟨σ3, Q3, ?_, H3, F2.trans (MFrA.of_sw hk3 hw3) rfl rfl⟩ + · rw [hspec, Fl4Spec.NonDet.when, if_pos hcond, hpre, Fl4Spec.NonDet.when, + if_pos (by rw [hcurσ, hcur2, ← ec])] + exact hrr + · rename_i hb + have ec : ¬ t = L.curW (L.wr (L.wr s (scA t) 0#u64) (scTcbA sc) 0#u64) := fun h => hb (decide_eq_true h) + rw [WP.spec_ok] + refine ⟨_, Z.Q, ?_, H2, F2⟩ + rw [hspec, Fl4Spec.NonDet.when, if_pos hcond, hpre, Fl4Spec.NonDet.when, + if_neg (by rw [hcurσ, hcur2]; exact fun h => ec (specPtr_inj h))] + rfl + · rw [if_neg e, WP.spec_ok] + refine none_out ?_ + rw [hspec, Fl4Spec.NonDet.when, if_neg] + · rfl + rintro ⟨h1, h2⟩ + apply e + have hne : L.mem s (scA t) ≠ 0#u64 := fun h => by + rw [h, show optScA m 0#u64 = none by simp [optScA]] at h1; exact h2 h1 + unfold optScA at h1 + rw [if_neg h0, if_neg hne] at h1 + refine scRenA_eq_of (Option.some.inj h1) (fun hi => H.inv.ntfnIdle _ z hkn ?_) + rw [hzs]; unfold optScA; rw [if_neg h0, hi] + +/-- SC return for a blocked receiver, which cannot belong to the release queue. -/ +theorem maybe_return_localA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} + {σ : Fl4SpecA64.State Unit} (H : SchedSRA st Z m κ s σ) (hst : StEqA Z.T (L.mem s) σ) {t n : U64} + (ht : t ∈ Z.T) (hN : NtfnScEqA m n (L.mem s) σ) + (hnr : ∀ x, σ.kheap (specPtr t) = some (.tcb x) → Fl4SpecA64.runnable x.tcbState = false) : + notification.maybe_return_sched_context st s n t ⦃ s' => ∃ σ' Q', + Fl4SpecA64.maybeReturnSc (Z := Unit) (specPtr n) (specPtr t) σ = ({((), σ')}, False) ∧ + SchedSRA st { Z with Q := Q' } m κ s' σ' ∧ MFrA (st := st) Z m s s' σ σ' ⦄ := + maybe_return_unqueuedA H hst ht hN fun hm => by + obtain ⟨x, hx, hr⟩ := H.run.2 _ hm + rw [hnr x hx] at hr + cases hr + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScRingA.lean b/proofs/Fl4ProofsA64/P3/ScRingA.lean new file mode 100644 index 000000000..e311699c9 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScRingA.lean @@ -0,0 +1,460 @@ +/- +P3 (aarch64, A64-P3-MCS gen 2): **the refill ring as a list** and **the spec's refill-list operations** on a related +SC, for `refill_budget_check` (x86 `P3/ScRing`, the rest of `P3/ScBudgetSpec`, `P3/ScFrame`'s `wl_done` / +`wl_step`; ported). + +FL4's AArch64 `sporadic::{set_head, set_tail, add_tail, pop_head, schedule_used}` are the x86 text (extraction +diffed: SAME) and A64 P1's `Refill` is the x86 text re-checked; `Fl4SpecA64`'s `SchedContext` refill operations are +the x86 port's (diffed). So the x86 lemmas port with `scWA` / `toRefillA` / `zRA` and `updKSc σ p (.schedContext x n)` +for x86's `updSc σ p x n`: +* **`RingAtA s0 s sc n`** (x86 `RingAt`): from `s0` to `s` only `sc`'s ring was written (its head / tail index words + and its refill slots, `RingWA`) and it is well formed. `set_head_ringA`, `set_tail_ringA`, `add_tail_ringA`, + `pop_head_ringA`, `schedule_used_ringA` (x86 `set_head_ring` …): the ring operations keep it and act on + `L.refills` as list operations (`schedUsedLA` for `schedule_used`, x86 `schedUsedL`). +* **`ScStateRelA.ringAtA`** (x86 `SchedRel.ringAt`, on `ScStateRelA`): after a ring step of a configured SC of `C`, + the relation and the placement hold for the spec state with that SC's refills replaced by FL4's list, and FL4 wrote + inside the SC's object (`RingWA ⊆ ScObjA`). +* Spec side (x86 `ScBudgetSpec`): `getRefills_eqA`, `refillSingle_eqA`, `refillFull_eqA`, `refillAddTail_eqA`, + **`scheduleUsed_eqA`** (`schedule_used` is `schedUsedLA`), `updKSc_selfA`, `headRefillOverrun_eqA`, + `chargeLA` / **`chargeEntire_eqA`** (`charge_entire_head_refill`); `wl_doneSc` / `wl_stepSc` (x86 `ScFrame`). + +No named premise. +-/ +import Fl4ProofsA64.P3.ScRefillA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +/-! ## The spec's while loop (x86 `ScFrame`) -/ + +section While +open Fl4Spec +variable {σ α : Type} {C : α → σ → Prop} {B : α → NonDet σ α} + +theorem reach_stuckSc {r : α} {s : σ} (h : ¬ C r s) {r1 : α} {s1 : σ} + (hr : NonDet.WhileReach C B r s r1 s1) : r1 = r ∧ s1 = s := by + induction hr with + | start => exact ⟨rfl, rfl⟩ + | step _ hc _ ih => obtain ⟨rfl, rfl⟩ := ih; exact absurd hc h + +/-- A loop whose condition fails returns at once (x86 `wl_done`). -/ +theorem wl_doneSc {r : α} {s : σ} (h : ¬ C r s) : NonDet.whileLoop C B r s = ({(r, s)}, False) := by + unfold NonDet.whileLoop + refine Prod.ext (Set.ext fun x => ⟨fun hx => ?_, fun hx => ?_⟩) (eq_false ?_) + · cases hx with + | done _ => rfl + | step hc _ _ => exact absurd hc h + · rw [Set.mem_singleton_iff] at hx; subst hx; exact NonDet.WhileResults.done h + · rintro (⟨r', s', hr, hc, -⟩ | ⟨f, hf0, hf⟩) + · obtain ⟨rfl, rfl⟩ := reach_stuckSc h hr; exact h hc + · have := (hf 0).1; rw [hf0] at this; exact h this + +theorem reach_detSc {r r' : α} {s s' : σ} (hc : C r s) (hb : B r s = ({(r', s')}, False)) {r1 : α} {s1 : σ} : + NonDet.WhileReach C B r s r1 s1 ↔ (r1 = r ∧ s1 = s) ∨ NonDet.WhileReach C B r' s' r1 s1 := by + constructor + · intro h + induction h with + | start => exact Or.inl ⟨rfl, rfl⟩ + | step h2 hc2 hm ih => + rcases ih with ⟨rfl, rfl⟩ | ih + · rw [hb] at hm + simp only [Set.mem_singleton_iff, Prod.mk.injEq] at hm + obtain ⟨rfl, rfl⟩ := hm + exact Or.inr NonDet.WhileReach.start + · exact Or.inr (NonDet.WhileReach.step ih hc2 hm) + · rintro (⟨rfl, rfl⟩ | h) + · exact .start + · have h0 : NonDet.WhileReach C B r s r' s' := + NonDet.WhileReach.step NonDet.WhileReach.start hc (by rw [hb]; rfl) + induction h with + | start => exact h0 + | step _ hc2 hm ih => exact NonDet.WhileReach.step ih hc2 hm + +/-- One deterministic round of a loop (x86 `wl_step`). -/ +theorem wl_stepSc {r r' : α} {s s' : σ} (hc : C r s) (hb : B r s = ({(r', s')}, False)) : + NonDet.whileLoop C B r s = NonDet.whileLoop C B r' s' := by + unfold NonDet.whileLoop + refine Prod.ext (Set.ext fun x => ⟨fun hx => ?_, fun hx => ?_⟩) (propext ⟨?_, ?_⟩) + · cases hx with + | done h => exact absurd hc h + | step _ hm hw => + rw [hb] at hm + simp only [Set.mem_singleton_iff, Prod.mk.injEq] at hm + obtain ⟨rfl, rfl⟩ := hm + exact hw + · exact NonDet.WhileResults.step hc (by rw [hb]; rfl) hx + · rintro (⟨r1, s1, hr, hc1, hf⟩ | ⟨f, hf0, hf⟩) + · rcases (reach_detSc hc hb).1 hr with ⟨rfl, rfl⟩ | hr' + · rw [hb] at hf; exact hf.elim + · exact Or.inl ⟨r1, s1, hr', hc1, hf⟩ + · right + refine ⟨fun n => f (n + 1), ?_, fun n => hf (n + 1)⟩ + have := (hf 0).2; rw [hf0, hb] at this + simpa using this + · rintro (⟨r1, s1, hr, hc1, hf⟩ | ⟨f, hf0, hf⟩) + · exact Or.inl ⟨r1, s1, (reach_detSc hc hb).2 (Or.inr hr), hc1, hf⟩ + · right + refine ⟨fun n => Nat.casesOn n (r, s) f, rfl, fun n => ?_⟩ + cases n with + | zero => exact ⟨hc, by show f 0 ∈ (B r s).1; rw [hf0, hb]; rfl⟩ + | succ n => exact hf n + +end While + +section +variable {σ α β : Type} + +theorem nd_pure_applySc (a : α) (s : σ) : (pure a : Fl4Spec.NonDet σ α) s = ({(a, s)}, False) := rfl + +theorem nd_pure_bindSc (a : α) (f : α → Fl4Spec.NonDet σ β) (s : σ) : (pure a >>= f) s = f a s := by + rw [nd_bind_applySc, nd_pure_applySc]; ext <;> simp + +theorem assert_trueSc {P : Prop} (h : P) (s : σ) : Fl4Spec.NonDet.assert P s = ({((), s)}, False) := by + simp [Fl4Spec.NonDet.assert, h] + +end + +/-! ## The ring (x86 `ScRing`) -/ + +section +variable {S : Type _} {st : machine.Store S} [L : LawfulStore st] + +/-- The words of `sc`'s ring: the head and tail indices and the slots (x86 `RingW`). -/ +def RingWA (st : machine.Store S) [L : LawfulStore st] (s0 : S) (sc b : U64) : Prop := + b = headA sc ∨ b = tailA sc ∨ (sc.val + 96 ≤ b.val ∧ b.val < sc.val + 96 + 16 * (L.rmax s0 sc).val) + +/-- Only `sc`'s ring changed since `s0`, and it is well formed (x86 `RingAt`). -/ +structure RingAtA (st : machine.Store S) [L : LawfulStore st] (s0 s : S) (sc : U64) (n : Nat) : Prop where + w : WritesIn st s0 s (RingWA st s0 sc) + wf : RefillsWF st s sc (n + Fl4SpecA64.minSchedContextBits) + mx : L.rmax s sc = L.rmax s0 sc + +theorem RingAtA.refl {s : S} {sc : U64} {n : Nat} (h : RefillsWF st s sc (n + Fl4SpecA64.minSchedContextBits)) : + RingAtA st s s sc n := + ⟨WritesIn.refl _ _, h, rfl⟩ + +theorem RingAtA.putS {s0 s : S} {sc : U64} {n : Nat} (h : RingAtA st s0 s sc n) {i : U64} + (hi : i.val < (L.rmax s sc).val) (r : sporadic.Refill) : RingAtA st s0 (L.putS s sc i r) sc n := by + obtain ⟨-, -, -, -, -, hin⟩ := h.wf.inside + obtain ⟨h1, h2, -⟩ := hin i hi + obtain ⟨h8, -, -⟩ := h.wf.bounds + have a1 : Aligned (rtA sc i) := by unfold Aligned; rw [h1]; omega + have a2 : Aligned (raA sc i) := by unfold Aligned; rw [h2]; omega + have w := (writesIn_wr (st := st) (s := s) (v := r.time) a1).trans + (writesIn_wr (st := st) (s := L.wr s (rtA sc i) r.time) (v := r.amount) a2) + have hm := h.mx + refine ⟨(h.w.trans w).mono fun b hb => ?_, h.wf.putS hi, ?_⟩ + · rcases hb with hb | rfl | rfl + · exact hb + · right; right; rw [h1, ← hm]; omega + · right; right; rw [h2, ← hm]; omega + · rw [← hm]; exact rmax_putS h8 (by have := h.wf.bounds; omega) + +theorem RingAtA.wr_head {s0 s : S} {sc : U64} {n : Nat} (h : RingAtA st s0 s sc n) {v : U64} + (hv : v.val < (L.rmax s sc).val) : RingAtA st s0 (L.wr s (headA sc) v) sc n := by + obtain ⟨-, -, -, hh, -, -⟩ := h.wf.inside + obtain ⟨h8, -, -⟩ := h.wf.bounds + have ah : Aligned (headA sc) := by unfold Aligned; rw [hh]; omega + exact ⟨(h.w.trans (writesIn_wr ah)).mono fun b hb => by rcases hb with hb | rfl; exact hb; exact Or.inl rfl, + h.wf.wr_head hv, by rw [rmax_wr_head h8]; exact h.mx⟩ + +theorem RingAtA.wr_tail {s0 s : S} {sc : U64} {n : Nat} (h : RingAtA st s0 s sc n) {v : U64} + (hv : v.val < (L.rmax s sc).val) : RingAtA st s0 (L.wr s (tailA sc) v) sc n := by + obtain ⟨-, -, -, -, ht, -⟩ := h.wf.inside + obtain ⟨h8, -, -⟩ := h.wf.bounds + have at' : Aligned (tailA sc) := by unfold Aligned; rw [ht]; omega + exact ⟨(h.w.trans (writesIn_wr at')).mono fun b hb => by + rcases hb with hb | rfl; exact hb; exact Or.inr (Or.inl rfl), + h.wf.wr_tail hv, by rw [rmax_wr_tail h8]; exact h.mx⟩ + +theorem refills_addA {s s' : S} {sc : obj.Sc} {sb : Nat} (hW : RefillsWF st s sc sb) + (hnf : L.size s sc < (L.rmax s sc).val) {r : sporadic.Refill} {nt : U64} + (hnt : nt = nextIdx (L.rmax s sc) (L.rtail s sc)) (hs' : s' = L.putS (L.wr s (tailA sc) nt) sc nt r) : + L.refills s' sc = L.refills s sc ++ [r] := by + obtain ⟨hn, hrf, -, -⟩ := hW.add hnf hnt hs' + rw [refills_ofA hn (fun k hk => hrf k (by omega))] + unfold LawfulStore.refills + rw [List.range_succ, List.map_append, List.map_singleton, if_pos rfl] + congr 1 + exact List.map_congr_left fun k hk => by rw [if_neg (by have := List.mem_range.1 hk; omega)] + +theorem set_head_ringA {s0 s : S} {sc : U64} {n : Nat} (h : RingAtA st s0 s sc n) (r : sporadic.Refill) : + sporadic.set_head st s sc r ⦃ s' => RingAtA st s0 s' sc n ∧ L.refills s' sc = r :: (L.refills s sc).tail ⦄ := by + refine WP.spec_mono (sporadic.set_head.eq s sc r) ?_ + rintro s' rfl + refine ⟨h.putS h.wf.head_lt r, ?_⟩ + rw [refills_ofA (h.wf.size_putS h.wf.head_lt) (fun k hk => h.wf.rf_set_head (by have := h.wf.size_le; omega)), + refills_set_headA h.wf] + +theorem set_tail_ringA {s0 s : S} {sc : U64} {n : Nat} (h : RingAtA st s0 s sc n) (r : sporadic.Refill) : + sporadic.set_tail st s sc r ⦃ s' => RingAtA st s0 s' sc n ∧ + L.refills s' sc = (L.refills s sc).dropLast ++ [r] ⦄ := by + refine WP.spec_mono (sporadic.set_tail.eq s sc r) ?_ + rintro s' rfl + exact ⟨h.putS h.wf.tail_lt r, refills_set_tailA h.wf r⟩ + +theorem add_tail_ringA {s0 s : S} {sc : U64} {n : Nat} (h : RingAtA st s0 s sc n) + (hnf : L.size s sc < (L.rmax s sc).val) (r : sporadic.Refill) : + sporadic.add_tail st s sc r ⦃ s' => RingAtA st s0 s' sc n ∧ L.refills s' sc = L.refills s sc ++ [r] ⦄ := by + refine WP.spec_mono (sporadic.add_tail.eq s sc r) ?_ + rintro s' rfl + obtain ⟨h8, -, -⟩ := h.wf.bounds + have h1 := h.wr_tail (v := nextIdx (L.rmax s sc) (L.rtail s sc)) (nextIdx_lt h.wf.tail_lt) + exact ⟨h1.putS (by rw [rmax_wr_tail h8]; exact nextIdx_lt h.wf.tail_lt) r, refills_addA h.wf hnf rfl rfl⟩ + +theorem pop_head_ringA {s0 s : S} {sc : U64} {n : Nat} (h : RingAtA st s0 s sc n) + (hne : L.rhead s sc ≠ L.rtail s sc) : + sporadic.pop_head st s sc ⦃ r s' => r = (L.refills s sc).headD zRA ∧ RingAtA st s0 s' sc n ∧ + L.refills s' sc = (L.refills s sc).tail ⦄ := by + refine WP.spec_mono (sporadic.pop_head.eq s sc) ?_ + rintro ⟨r, s'⟩ ⟨rfl, rfl⟩ + exact ⟨head_eqA h.wf, h.wr_head (nextIdx_lt h.wf.head_lt), refills_popA h.wf hne rfl⟩ + +/-- `schedule_used` on the list (x86 `schedUsedL`). -/ +def schedUsedLA (m : Nat) (rs : List sporadic.Refill) (new : sporadic.Refill) : List sporadic.Refill := + let t := rs.getLastD zRA + if new.time ≤ core.num.U64.wrapping_add t.time t.amount then + rs.dropLast ++ [⟨t.time, core.num.U64.wrapping_add t.amount new.amount⟩] + else if rs.length = m then + rs.dropLast ++ [⟨core.num.U64.wrapping_sub new.time t.amount, core.num.U64.wrapping_add t.amount new.amount⟩] + else rs ++ [new] + +theorem schedule_used_ringA {s0 s : S} {sc : U64} {n : Nat} (h : RingAtA st s0 s sc n) (new : sporadic.Refill) : + sporadic.schedule_used st s sc new ⦃ s' => RingAtA st s0 s' sc n ∧ + L.refills s' sc = schedUsedLA (L.rmax s sc).val (L.refills s sc) new ⦄ := by + have hW := h.wf + have htl := refills_tail_eqA hW + have hlen := refills_lengthA (st := st) s sc + unfold sporadic.schedule_used + refine WP.spec_bind (sporadic.tail.spec s sc) ?_ + rintro t rfl + rw [htl] + step as ⟨i, i_post⟩ + split + · rename_i hc + step as ⟨i1, i1_post⟩ + refine WP.spec_mono (set_tail_ringA h _) ?_ + rintro s' ⟨h', e⟩ + refine ⟨h', ?_⟩ + rw [e]; unfold schedUsedLA + rw [if_pos (by rw [← i_post]; exact hc), ← i1_post] + · rename_i hc + step with sporadic.full.spec as ⟨b, b_post⟩ + split + · rename_i hb + step as ⟨i1, i1_post⟩ + step as ⟨i2, i2_post⟩ + refine WP.spec_mono (set_tail_ringA h _) ?_ + rintro s' ⟨h', e⟩ + refine ⟨h', ?_⟩ + rw [e]; unfold schedUsedLA + have hf : (L.refills s sc).length = (L.rmax s sc).val := by + rw [hlen, ← hW.ringSize_val]; rw [b_post] at hb; have := of_decide_eq_true hb; rw [this] + rw [if_neg (by rw [← i_post]; exact hc), if_pos hf, ← i1_post, ← i2_post] + · rename_i hb + have hnf : L.size s sc < (L.rmax s sc).val := by + have := hW.size_le + rw [b_post] at hb; have hne := of_decide_eq_false (by simpa using hb) + rw [← hW.ringSize_val] + have : (ringSize (L.rmax s sc) (L.rhead s sc) (L.rtail s sc)).val ≠ (L.rmax s sc).val := + fun e => hne e + rw [hW.ringSize_val] at this ⊢; omega + refine WP.spec_mono (add_tail_ringA h hnf new) ?_ + rintro s' ⟨h', e⟩ + refine ⟨h', ?_⟩ + rw [e]; unfold schedUsedLA + rw [if_neg (by rw [← i_post]; exact hc), if_neg (by rw [hlen]; omega)] + +/-- A ring step keeps the header words below the head index (x86 `RingAt.hdr`). -/ +theorem RingAtA.hdr {s0 s : S} {sc : U64} {n : Nat} (h : RingAtA st s0 s sc n) {K : U64} (hK : K.val < 80) + (hK8 : K.val % 8 = 0) : L.mem s (scWA sc K) = L.mem s0 (scWA sc K) := by + obtain ⟨h8, hb, hfit⟩ := h.wf.bounds + obtain ⟨-, -, -, hh, ht, -⟩ := h.wf.inside + have hv := scWA_val (sc := sc) (K := K) (by omega) + refine h.w.mem_eq (by unfold Aligned; rw [hv]; omega) ?_ + rintro (e | e | ⟨e1, -⟩) + · have := congrArg UScalar.val e; rw [hv, hh] at this; omega + · have := congrArg UScalar.val e; rw [hv, ht] at this; omega + · rw [hv] at e1; omega + +theorem RingAtA.period {s0 s : S} {sc : U64} {n : Nat} (h : RingAtA st s0 s sc n) : + L.rperiod s sc = L.rperiod s0 sc := by + simp only [LawfulStore.rperiod, periodA_eqS] + exact h.hdr (by simp) (by simp) + +/-- The ring words of a related SC are words of its object. -/ +theorem RingWA.in_obj {ii : Prop} {s0 : S} {sc : U64} {x : Fl4SpecA64.SchedContext} {n : Nat} + (hx : ScRelWA st ii s0 sc x n) (hW : RefillsWF st s0 sc (n + Fl4SpecA64.minSchedContextBits)) {b : U64} + (hb : RingWA st s0 sc b) : ScObjA sc n b := by + obtain ⟨-, -, -, hh, ht, -⟩ := hW.inside + refine hx.in_obj ?_ + have hm : 0 < (L.rmax s0 sc).val := by have := hW.min_le; simp at this; omega + rcases hb with rfl | rfl | ⟨b1, b2⟩ + · exact ⟨by rw [hh]; omega, by rw [hh]; omega⟩ + · exact ⟨by rw [ht]; omega, by rw [ht]; omega⟩ + · exact ⟨by omega, b2⟩ + +/-- **The relation after ring steps** (x86 `SchedRel.ringAt`): the spec state with `sc`'s refills replaced by FL4's +list is related, placed, and FL4 wrote inside `sc`'s object. -/ +theorem ScStateRelA.ringAtA {th : TypedHeapA} {ksb : U64} {C : Set U64} {m : Fl4SpecA64.McState} {s0 s : S} + {σ : Fl4SpecA64.State Unit} (h : ScStateRelA st C m s0 σ) (P : ScPlacedA th ksb C m σ) {sc : U64} (hc : sc ∈ C) + {x : Fl4SpecA64.SchedContext} {n : Nat} (hk : σ.kheap (scNameA m sc) = some (.schedContext x n)) + (hx : ScRel1A st s0 sc x n) (hR : RingAtA st s0 s sc n) : + ScStateRelA st C m s (updKSc σ (scNameA m sc) + (.schedContext { x with scRefills := (L.refills s sc).map toRefillA } n)) ∧ + ScPlacedA th ksb C m (updKSc σ (scNameA m sc) + (.schedContext { x with scRefills := (L.refills s sc).map toRefillA } n)) ∧ + WritesIn st s0 s (ScObjA sc n) := by + have hw : WritesIn st s0 s (ScObjA sc n) := hR.w.mono fun b hb => RingWA.in_obj (hx.toW (ii := True)) hx.wf hb + have hx1 := hx.ring hR.wf fun K hK hK8 => hR.hdr hK hK8 + obtain ⟨h1, h2⟩ := h.upd P hc hk hw hx1.toW + exact ⟨h1, h2, hw⟩ + +end + +/-! ## The spec's refill-list operations (x86 `ScBudgetSpec`) -/ + +section Spec +open Fl4SpecA64 +variable {σ : State Unit} {p : ObjRef} {x : SchedContext} {n : Nat} + +theorem getRefills_eqA (hk : σ.kheap p = some (.schedContext x n)) : + getRefills (Z := Unit) p σ = ({(x.scRefills, σ)}, False) := by + unfold getRefills; rw [nd_bind_oneSc (getSchedContext_eqA hk)]; rfl + +theorem refillSingle_eqA (hk : σ.kheap p = some (.schedContext x n)) : + refillSingle (Z := Unit) p σ = ({(decide (x.scRefills.length = 1), σ)}, False) := by + unfold refillSingle refillSize + rw [nd_bind_oneSc (nd_bind_oneSc (getRefills_eqA hk) _)]; rfl + +theorem refillFull_eqA (hk : σ.kheap p = some (.schedContext x n)) : + refillFull (Z := Unit) p σ = ({(decide (x.scRefills.length = x.scRefillMax), σ)}, False) := by + unfold refillFull + rw [nd_bind_oneSc (getSchedContext_eqA hk)]; rfl + +theorem refillAddTail_eqA (hk : σ.kheap p = some (.schedContext x n)) (r : Refill) : + refillAddTail (Z := Unit) p r σ = + ({((), updKSc σ p (.schedContext { x with scRefills := x.scRefills ++ [r] } n))}, False) := + updateSchedContext_eqA hk _ + +theorem updKSc_selfA (hk : σ.kheap p = some (.schedContext x n)) : updKSc σ p (.schedContext x n) = σ := by + unfold updKSc + have : Function.update σ.kheap p (some (.schedContext x n)) = σ.kheap := by + funext q; by_cases e : q = p + · subst e; simp [hk] + · simp [e] + rw [this] + +theorem headRefillOverrun_eqA (hk : σ.kheap p = some (.schedContext x n)) (hne : x.scRefills ≠ []) (u : Ticks) : + headRefillOverrun (Z := Unit) p u σ = some (decide ((refillHd x).rAmount ≤ u ∧ + (refillHd x).rTime < MAX_RELEASE_TIME)) := by + show Option.bind (readRefillHead p σ) (fun h => Fl4Spec.Lookup.oreturn _ σ) = _ + rw [readRefillHead_eqA hk hne]; rfl + +end Spec + +section +variable {S : Type _} {st : machine.Store S} [L : LawfulStore st] + +theorem u64_le_bvA (a b : U64) : a ≤ b ↔ a.bv ≤ b.bv := by + rw [UScalar.le_equiv, BitVec.le_def]; rfl + +theorem u64_lt_bvA (a b : U64) : a < b ↔ a.bv < b.bv := by + rw [UScalar.lt_equiv, BitVec.lt_def]; rfl + +/-- **`schedule_used` refines `schedule_used`** on the lists (x86 `scheduleUsed_eq`). -/ +theorem scheduleUsed_eqA {σ : Fl4SpecA64.State Unit} {p : Fl4SpecA64.ObjRef} {x : Fl4SpecA64.SchedContext} {n : Nat} + (hk : σ.kheap p = some (.schedContext x n)) {rs : List sporadic.Refill} (hx : x.scRefills = rs.map toRefillA) + (hne : rs ≠ []) (nw : sporadic.Refill) : + Fl4SpecA64.scheduleUsed (Z := Unit) p (toRefillA nw) σ = + ({((), updKSc σ p (.schedContext + { x with scRefills := (schedUsedLA x.scRefillMax rs nw).map toRefillA } n))}, False) := by + have hne' : x.scRefills ≠ [] := by rw [hx]; simpa using hne + unfold Fl4SpecA64.scheduleUsed + rw [nd_bind_oneSc (getRefills_eqA hk), nd_bind_oneSc (assert_trueSc hne' σ), + nd_bind_oneSc (getRefillTail_eqA hk hne')] + have htl : Fl4SpecA64.refillTl x = toRefillA (rs.getLastD zRA) := by + unfold Fl4SpecA64.refillTl; rw [hx]; exact the_getLast_mapA hne + rw [htl] + set t := rs.getLastD zRA with ht + have hmerge : Fl4SpecA64.canMergeRefill (toRefillA t) (toRefillA nw) ↔ + nw.time ≤ core.num.U64.wrapping_add t.time t.amount := by + unfold Fl4SpecA64.canMergeRefill toRefillA + rw [u64_le_bvA, core.num.U64.wrapping_add_bv_eq] + have hd : (x.scRefills.dropLast) = (rs.dropLast).map toRefillA := by rw [hx, List.map_dropLast] + have hlast : Fl4SpecA64.the x.scRefills.getLast? = toRefillA t := by rw [hx]; exact the_getLast_mapA hne + unfold schedUsedLA + simp only [← ht] + by_cases hc : nw.time ≤ core.num.U64.wrapping_add t.time t.amount + · rw [if_pos (hmerge.2 hc), if_pos hc, updateRefillTl_eqA hk] + congr 4 + rw [hd, hlast, List.map_append, List.map_singleton] + simp [toRefillA, core.num.U64.wrapping_add_bv_eq] + · rw [if_neg (fun h => hc (hmerge.1 h)), if_neg hc, nd_bind_oneSc (refillFull_eqA hk)] + by_cases hf : rs.length = x.scRefillMax + · have hf' : x.scRefills.length = x.scRefillMax := by rw [hx, List.length_map]; exact hf + simp only [hf', decide_true, not_true_eq_false, ↓reduceIte, hf] + rw [nd_bind_oneSc (updateRefillTl_eqA hk _)] + rw [updateRefillTl_eqA (updKSc_same _ _ _), updKSc_updKSc] + congr 4 + simp only [List.dropLast_concat, List.getLast?_append, List.getLast?_singleton, Option.some_or] + rw [hd, hlast, List.map_append, List.map_singleton] + simp [Fl4SpecA64.the, toRefillA, core.num.U64.wrapping_add_bv_eq, core.num.U64.wrapping_sub_bv_eq] + · have hf' : ¬ x.scRefills.length = x.scRefillMax := by rw [hx, List.length_map]; exact hf + simp only [hf', decide_false, not_false_eq_true, ↓reduceIte, hf, Bool.false_eq_true] + rw [refillAddTail_eqA hk, hx, List.map_append, List.map_singleton] + +/-- `charge_entire_head_refill` on the list (x86 `chargeL`). -/ +def chargeLA (m : Nat) (per : U64) (rs : List sporadic.Refill) : List sporadic.Refill := + if rs.length = 1 then [⟨core.num.U64.wrapping_add (rs.headD zRA).time per, (rs.headD zRA).amount⟩] + else schedUsedLA m rs.tail ⟨core.num.U64.wrapping_add (rs.headD zRA).time per, (rs.headD zRA).amount⟩ + +/-- **`charge_entire_head_refill` on a related SC** (x86 `chargeEntire_eq`). -/ +theorem chargeEntire_eqA {σ : Fl4SpecA64.State Unit} {p : Fl4SpecA64.ObjRef} {x : Fl4SpecA64.SchedContext} {n : Nat} + (hk : σ.kheap p = some (.schedContext x n)) {rs : List sporadic.Refill} (hx : x.scRefills = rs.map toRefillA) + (hne : rs ≠ []) {per : U64} (hper : x.scPeriod = per.bv) (u : Fl4SpecA64.Ticks) : + Fl4SpecA64.chargeEntireHeadRefill (Z := Unit) p u σ = + ({(u - (toRefillA (rs.headD zRA)).rAmount, + updKSc σ p (.schedContext { x with scRefills := (chargeLA x.scRefillMax per rs).map toRefillA } n))}, + False) := by + have hne' : x.scRefills ≠ [] := by rw [hx]; simpa using hne + have hhd := refillHd_mapA hx hne + unfold Fl4SpecA64.chargeEntireHeadRefill + rw [nd_bind_oneSc (getRefillHead_eqA hk hne'), nd_bind_oneSc (getSchedContext_eqA hk), + nd_bind_oneSc (refillSingle_eqA hk), hhd, hper] + obtain ⟨a, l, rfl⟩ := List.exists_cons_of_ne_nil hne + unfold chargeLA + by_cases h1 : (a :: l).length = 1 + · have hl : l = [] := by simpa using h1 + subst hl + have h1' : (x.scRefills.length = 1) := by rw [hx]; rfl + simp only [h1', decide_true, ↓reduceIte, h1] + rw [nd_bind_oneSc (updateRefillHd_eqA hk _)] + show ({(_, _)}, False) = _ + congr 4 + rw [hx] + simp [toRefillA, Fl4SpecA64.the, core.num.U64.wrapping_add_bv_eq, hper] + · have h1' : ¬ (x.scRefills.length = 1) := by rw [hx, List.length_map]; exact h1 + simp only [h1', decide_false, Bool.false_eq_true, ↓reduceIte, h1] + rw [nd_bind_oneSc (refillPopHead_eqA hk hne'), hhd] + have hl : l ≠ [] := by rintro rfl; exact h1 rfl + have hx1 : ({ x with scRefills := x.scRefills.tail } : Fl4SpecA64.SchedContext).scRefills = l.map toRefillA := by + rw [hx]; rfl + have e := scheduleUsed_eqA (σ := updKSc σ p (.schedContext { x with scRefills := x.scRefills.tail } n)) (p := p) + (n := n) (updKSc_same _ _ _) hx1 hl ⟨core.num.U64.wrapping_add a.time per, a.amount⟩ + have et : toRefillA ⟨core.num.U64.wrapping_add a.time per, a.amount⟩ = + { rTime := (toRefillA a).rTime + per.bv, rAmount := (toRefillA a).rAmount } := by + simp [toRefillA, core.num.U64.wrapping_add_bv_eq] + simp only [List.headD_cons] + rw [← et, nd_bind_oneSc e, updKSc_updKSc] + simp only [h1, ↓reduceIte, List.headD_cons, List.tail_cons] + rw [hper]; rfl + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScSWrA.lean b/proofs/Fl4ProofsA64/P3/ScSWrA.lean new file mode 100644 index 000000000..3ebbd25ef --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScSWrA.lean @@ -0,0 +1,289 @@ +/- +P3 (aarch64, A64-P3-MCS gen 10): **the MCS write set and spec frame as one composable bundle** (ruling 01:39Z gen-10 +item 1), placed below `ScSwitchA` so that every scheduler step can state its frame. + +* **`SKeepA σ σ'`** (gen 9, moved here from `P3/ScRetA`): every object that is neither an SC nor a TCB is kept, every + TCB is kept up to its `tcbSchedContext`. +* **`SSzA σ σ'`** (new): every SC object of `σ'` was an SC object of `σ` of the same size. It is what carries the SC + footprint `ScFootA` (sized by the spec objects) from an intermediate state back to the start state. +* **`SWrA Z s`**: the scheduler words, i.e. the kernel-state block `[ksb, ksb + ksSizeA)` and the `SC` / scheduler-queue + link / `IN_QUEUES` words of the TCBs of `Z.T`; **`MWrA Z m σ s`** (gen 9, moved here from `P3/ScFrameA`, same + definition) `= ScFootA Z.C m σ ∨ SWrA Z s`. +* **`MFrA Z m s s' σ σ'`** = `WritesIn s s' (MWrA Z m σ s)` ∧ `SKeepA σ σ'` ∧ `SSzA σ σ'`; **`MFrA.trans`** composes + two steps whose views agree on `C` and `T` (only `Q` / `R` change along the MCS tails), via `mwr_back` + (`SSzA` + the kept `ksb`). +* Embeddings into `SWrA`: **`SchedSRN.qfoot_swr`** (a scheduler-queue footprint `QFoot` of TCBs of `Z.T` on a core of + `Z.Cs`), **`SchedSRN.enq_swr`** (`enqueue`'s), **`SchedSRN.act_wr`** (the write of the action word), + `SchedSRN.repr_swr` (`REPROGRAM`). + +No named premise. +-/ +import Fl4ProofsA64.P3.ScEnqA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +section Spec +open Fl4SpecA64 + +/-- **The spec frame of the MCS tails** (module docstring). -/ +structure SKeepA (σ σ' : State Unit) : Prop where + obj : ∀ p o, σ.kheap p = some o → (∀ y n, o ≠ .schedContext y n) → (∀ x, o ≠ .tcb x) → σ'.kheap p = some o + tcb : ∀ p x, σ.kheap p = some (.tcb x) → ∃ y, σ'.kheap p = some (.tcb y) ∧ + ∃ o, y = { x with tcbSchedContext := o } + +theorem SKeepA.refl (σ : State Unit) : SKeepA σ σ := + ⟨fun _ _ h _ _ => h, fun _ x h => ⟨x, h, x.tcbSchedContext, rfl⟩⟩ + +theorem SKeepA.trans {σ1 σ2 σ3 : State Unit} (h1 : SKeepA σ1 σ2) (h2 : SKeepA σ2 σ3) : SKeepA σ1 σ3 := by + refine ⟨fun p o hp h3 h4 => h2.obj p o (h1.obj p o hp h3 h4) h3 h4, fun p x hp => ?_⟩ + obtain ⟨y, hy, o, rfl⟩ := h1.tcb p x hp + obtain ⟨z, hz, o', rfl⟩ := h2.tcb p _ hy + exact ⟨_, hz, o', rfl⟩ + +theorem SKeepA.of_kheap {σ σ' : State Unit} (h : σ'.kheap = σ.kheap) : SKeepA σ σ' := by + refine ⟨fun p o hp _ _ => by rw [h]; exact hp, fun p x hp => ⟨x, by rw [h]; exact hp, x.tcbSchedContext, rfl⟩⟩ + +/-- A step that keeps every object but the SCs (`UnbFrA.obj`'s shape). -/ +theorem SKeepA.of_obj {σ σ' : State Unit} + (h : ∀ q o, σ.kheap q = some o → (∀ y n, o ≠ .schedContext y n) → σ'.kheap q = some o) : SKeepA σ σ' := + ⟨fun p o hp h3 _ => h p o hp h3, fun p x hp => ⟨x, h p _ hp (fun _ _ e => by cases e), x.tcbSchedContext, rfl⟩⟩ + +/-- **The SC objects keep their sizes** (module docstring). -/ +def SSzA (σ σ' : State Unit) : Prop := + ∀ p y n, σ'.kheap p = some (.schedContext y n) → ∃ x, σ.kheap p = some (.schedContext x n) + +theorem SSzA.refl (σ : State Unit) : SSzA σ σ := fun _ y _ h => ⟨y, h⟩ + +theorem SSzA.trans {σ1 σ2 σ3 : State Unit} (h1 : SSzA σ1 σ2) (h2 : SSzA σ2 σ3) : SSzA σ1 σ3 := fun p y n h => + let ⟨x, hx⟩ := h2 p y n h + h1 p x n hx + +theorem SSzA.of_kheap {σ σ' : State Unit} (h : σ'.kheap = σ.kheap) : SSzA σ σ' := + fun _ y _ hp => ⟨y, by rw [← h]; exact hp⟩ + +/-- One SC object replaced by one of the same size. -/ +theorem SSzA.of_upd {σ σ' : State Unit} {q : ObjRef} {x y : SchedContext} {n : Nat} + (hk : σ.kheap q = some (.schedContext x n)) + (h : σ'.kheap = Function.update σ.kheap q (some (.schedContext y n))) : SSzA σ σ' := by + intro p y' k hp + rw [h] at hp + by_cases e : p = q + · subst e; simp only [Function.update_self, Option.some.injEq, KernelObject.schedContext.injEq] at hp + obtain ⟨-, rfl⟩ := hp + exact ⟨x, hk⟩ + · rw [Function.update_of_ne e] at hp; exact ⟨y', hp⟩ + +/-- **One TCB rebound and one SC object replaced by one of the same size** (the donation's and the unbinding's spec +step): `SKeepA` and `SSzA`. -/ +theorem SKeepA.two {σ σ' : State Unit} {N T : ObjRef} {xs ys : SchedContext} {ns : Nat} {xt : Tcb} + {o : Option ObjRef} + (hk : ∀ p, σ'.kheap p = if p = T then some (.tcb { xt with tcbSchedContext := o }) + else if p = N then some (.schedContext ys ns) else σ.kheap p) + (hkN : σ.kheap N = some (.schedContext xs ns)) (hkT : σ.kheap T = some (.tcb xt)) : + SKeepA σ σ' ∧ SSzA σ σ' := by + refine ⟨⟨fun p o' hp h3 h4 => ?_, fun p x hp => ?_⟩, fun p y n hp => ?_⟩ + · rw [hk, if_neg (fun e => by subst e; rw [hkT] at hp; cases hp; exact h4 xt rfl), + if_neg (fun e => by subst e; rw [hkN] at hp; cases hp; exact h3 xs ns rfl)] + exact hp + · rw [hk] + by_cases e : p = T + · subst e; rw [hkT] at hp; cases hp; exact ⟨_, if_pos rfl, o, rfl⟩ + · have e2 : p ≠ N := fun e' => by subst e'; rw [hkN] at hp; cases hp + exact ⟨x, by rw [if_neg e, if_neg e2]; exact hp, x.tcbSchedContext, rfl⟩ + · rw [hk] at hp + by_cases e : p = T + · rw [if_pos e] at hp; cases hp + · rw [if_neg e] at hp + by_cases e2 : p = N + · rw [if_pos e2] at hp; cases hp; exact ⟨xs, by rw [e2]; exact hkN⟩ + · rw [if_neg e2] at hp; exact ⟨y, hp⟩ + +end Spec + +/-- The words `NdOKA` reads: the node count `NUM_NODES` `[0, 8)` (record edit F1), the schedule position and start +`[24, 40)`, the `KERNEL_ROOT` word `[48, 56)` (record edit F4) and the table `[192, 224)` of the kernel-state block `k` (record edit #8b-B1: moved here from +`P3/ScDomainA`). -/ +def DomW (k b : U64) : Prop := + (k.val ≤ b.val ∧ b.val < k.val + 8) ∨ (k.val + 24 ≤ b.val ∧ b.val < k.val + 40) ∨ + (k.val + 48 ≤ b.val ∧ b.val < k.val + 56) ∨ (k.val + 192 ≤ b.val ∧ b.val < k.val + 224) + +/-- **The kernel-state words a scheduler step may write** (Architect 23:50Z, G1; record edit #8b-B1 drops the +domain-schedule words `DomW`): the global words `[8, 24)`, `[40, 48)` and `[56, 192)` (domain, time, work counter; record edit F4 leaves the +`KERNEL_ROOT` word `[48, 56)` out) and the 128 +per-core node blocks `[223488, 772352)` (ready / release queues, action, reprogram); strictly off the IRQ-node slots +`[43232, 215264)`, `OS`, VSpace and frames, the ASID table and the domain-schedule words `DomW` (`NdOKA`'s). +`next_domain` writes the latter and uses the broader `MWrDA` / `MFrDA`. (Record edit B2: the two IPI-pair words of every +node block, offsets 120 and 128, are no scheduler write: a queue operation on a thread of the current core +writes none, `P1.LawfulStore.QFootN`; `ipiZero` is framed by it.) -/ +def SKsA (k b : U64) : Prop := + (k.val + 8 ≤ b.val ∧ b.val < k.val + 24) ∨ (k.val + 40 ≤ b.val ∧ b.val < k.val + 48) ∨ + (k.val + 56 ≤ b.val ∧ b.val < k.val + 192) ∨ + (k.val + 223488 ≤ b.val ∧ b.val < k.val + 772352 ∧ (b.val - k.val - 223488) % 4288 ≠ 120 ∧ + (b.val - k.val - 223488) % 4288 ≠ 128) + +theorem SKsA.ks {k b : U64} (h : SKsA k b) : k.val ≤ b.val ∧ b.val < k.val + ksSizeA := by + unfold ksSizeA; unfold SKsA at h; omega + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **The scheduler words** (module docstring). -/ +def SWrA (Z : XSetsA) (s : S) (b : U64) : Prop := + SKsA (L.ksb s) b ∨ + ∃ u ∈ Z.T, b = scA u ∨ b = nextA .Sched u ∨ b = prevA .Sched u ∨ b = inqA u + +/-- **The MCS write set** (gen 9's definition; module docstring). -/ +def MWrA (Z : XSetsA) (m : Fl4SpecA64.McState) (σ : Fl4SpecA64.State Unit) (s : S) (b : U64) : Prop := + ScFootA Z.C m σ b ∨ SWrA (st := st) Z s b + +theorem swr_ksb {Z Z' : XSetsA} (hT : Z'.T = Z.T) {s s1 : S} (hk : L.ksb s1 = L.ksb s) {b : U64} + (h : SWrA (st := st) Z' s1 b) : SWrA (st := st) Z s b := by + unfold SWrA at h ⊢; rw [hk, hT] at h; exact h + +theorem WritesIn.swr_trans {Z Z' : XSetsA} (hT : Z'.T = Z.T) {s s1 s2 : S} + (h1 : WritesIn st s s1 (SWrA (st := st) Z s)) (h2 : WritesIn st s1 s2 (SWrA (st := st) Z' s1)) : + WritesIn st s s2 (SWrA (st := st) Z s) := + (h1.trans h2).mono fun _ hb => hb.elim id (swr_ksb hT h1.1) + +/-- An `MWrA` word of a later state of the same `C` / `T` (sizes kept, `ksb` kept) is one of the earlier state. -/ +theorem mwr_back {Z Z' : XSetsA} (hC : Z'.C = Z.C) (hT : Z'.T = Z.T) {m : Fl4SpecA64.McState} + {σ σ1 : Fl4SpecA64.State Unit} {s s1 : S} (hz : SSzA σ σ1) (hk : L.ksb s1 = L.ksb s) {b : U64} + (h : MWrA (st := st) Z' m σ1 s1 b) : MWrA (st := st) Z m σ s b := by + rcases h with ⟨c, hc, x, n, hx, ho⟩ | h + · obtain ⟨x0, h0⟩ := hz _ x n hx + exact Or.inl ⟨c, by rw [← hC]; exact hc, x0, n, h0, ho⟩ + · exact Or.inr (swr_ksb hT hk h) + +/-- **The frame bundle** (module docstring). -/ +structure MFrA (Z : XSetsA) (m : Fl4SpecA64.McState) (s s' : S) (σ σ' : Fl4SpecA64.State Unit) : Prop where + wr : WritesIn st s s' (MWrA (st := st) Z m σ s) + keep : SKeepA σ σ' + sz : SSzA σ σ' + +theorem MFrA.refl (Z : XSetsA) (m : Fl4SpecA64.McState) (s : S) (σ : Fl4SpecA64.State Unit) : + MFrA (st := st) Z m s s σ σ := + ⟨WritesIn.refl _ _, SKeepA.refl σ, SSzA.refl σ⟩ + +theorem MFrA.trans {Z Z' : XSetsA} {m : Fl4SpecA64.McState} {s s1 s2 : S} {σ σ1 σ2 : Fl4SpecA64.State Unit} + (F1 : MFrA (st := st) Z m s s1 σ σ1) (F2 : MFrA (st := st) Z' m s1 s2 σ1 σ2) (hC : Z'.C = Z.C) + (hT : Z'.T = Z.T) : MFrA (st := st) Z m s s2 σ σ2 := + ⟨(F1.wr.trans F2.wr).mono fun _ hb => hb.elim id (mwr_back hC hT F1.sz F1.wr.1), F1.keep.trans F2.keep, + F1.sz.trans F2.sz⟩ + +/-- A scheduler step (heap kept, writing scheduler words). -/ +theorem MFrA.of_sw {Z : XSetsA} {m : Fl4SpecA64.McState} {s s' : S} {σ σ' : Fl4SpecA64.State Unit} + (hk : σ'.kheap = σ.kheap) (hw : WritesIn st s s' (SWrA (st := st) Z s)) : MFrA (st := st) Z m s s' σ σ' := + ⟨hw.mono fun _ h => Or.inr h, SKeepA.of_kheap hk, SSzA.of_kheap hk⟩ + +/-- **The write set of a scheduler step that advances the domain schedule** (`MWrA` and the words `DomW`). -/ +def MWrDA (Z : XSetsA) (m : Fl4SpecA64.McState) (σ : Fl4SpecA64.State Unit) (s : S) (b : U64) : Prop := + MWrA (st := st) Z m σ s b ∨ DomW (L.ksb s) b + +/-- **The frame bundle with the domain-schedule words** (sibling of `MFrA`, record edit #8b-B1). -/ +structure MFrDA (Z : XSetsA) (m : Fl4SpecA64.McState) (s s' : S) (σ σ' : Fl4SpecA64.State Unit) : Prop where + wr : WritesIn st s s' (MWrDA (st := st) Z m σ s) + keep : SKeepA σ σ' + sz : SSzA σ σ' + +theorem MFrA.toD {Z : XSetsA} {m : Fl4SpecA64.McState} {s s' : S} {σ σ' : Fl4SpecA64.State Unit} + (F : MFrA (st := st) Z m s s' σ σ') : MFrDA (st := st) Z m s s' σ σ' := + ⟨F.wr.mono fun _ h => Or.inl h, F.keep, F.sz⟩ + +theorem MFrDA.refl (Z : XSetsA) (m : Fl4SpecA64.McState) (s : S) (σ : Fl4SpecA64.State Unit) : + MFrDA (st := st) Z m s s σ σ := + (MFrA.refl Z m s σ).toD + +/-- An `MWrDA` word of a later state of the same `C` / `T` is one of the earlier state. -/ +theorem mwrd_back {Z Z' : XSetsA} (hC : Z'.C = Z.C) (hT : Z'.T = Z.T) {m : Fl4SpecA64.McState} + {σ σ1 : Fl4SpecA64.State Unit} {s s1 : S} (hz : SSzA σ σ1) (hk : L.ksb s1 = L.ksb s) {b : U64} + (h : MWrDA (st := st) Z' m σ1 s1 b) : MWrDA (st := st) Z m σ s b := + h.elim (fun h => Or.inl (mwr_back hC hT hz hk h)) (fun h => Or.inr (by rw [← hk]; exact h)) + +theorem MFrDA.trans {Z Z' : XSetsA} {m : Fl4SpecA64.McState} {s s1 s2 : S} {σ σ1 σ2 : Fl4SpecA64.State Unit} + (F1 : MFrDA (st := st) Z m s s1 σ σ1) (F2 : MFrDA (st := st) Z' m s1 s2 σ1 σ2) (hC : Z'.C = Z.C) + (hT : Z'.T = Z.T) : MFrDA (st := st) Z m s s2 σ σ2 := + ⟨(F1.wr.trans F2.wr).mono fun _ hb => hb.elim id (mwrd_back hC hT F1.sz F1.wr.1), F1.keep.trans F2.keep, + F1.sz.trans F2.sz⟩ + +/-- A step of the broader frame followed by a step of `MFrA`. -/ +theorem MFrDA.transA {Z Z' : XSetsA} {m : Fl4SpecA64.McState} {s s1 s2 : S} {σ σ1 σ2 : Fl4SpecA64.State Unit} + (F1 : MFrDA (st := st) Z m s s1 σ σ1) (F2 : MFrA (st := st) Z' m s1 s2 σ1 σ2) (hC : Z'.C = Z.C) + (hT : Z'.T = Z.T) : MFrDA (st := st) Z m s s2 σ σ2 := + F1.trans F2.toD hC hT + +/-- An `MFrA` step followed by a step of the broader frame. -/ +theorem MFrA.transD {Z Z' : XSetsA} {m : Fl4SpecA64.McState} {s s1 s2 : S} {σ σ1 σ2 : Fl4SpecA64.State Unit} + (F1 : MFrA (st := st) Z m s s1 σ σ1) (F2 : MFrDA (st := st) Z' m s1 s2 σ1 σ2) (hC : Z'.C = Z.C) + (hT : Z'.T = Z.T) : MFrDA (st := st) Z m s s2 σ σ2 := + F1.toD.trans F2 hC hT + +/-- The kernel-state block holds every node word of the cores of `Z.Cs` (gen 9, moved from `P3/ScFrameA`). -/ +theorem SchedSRN.node_in {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} {σ : Fl4SpecA64.State Unit} + (H : SchedSRN st Z m κ s σ) {c o : U64} (hc : c ∈ Z.Cs) (ho : o.val < 4288) : + (L.ksb s).val ≤ (L.nodeAt s c o).val ∧ (L.nodeAt s c o).val < (L.ksb s).val + ksSizeA := by + obtain ⟨nn, -, hn, hcs⟩ := H.csN + have hcl : c.val < 128 := by have := (hcs c).1 hc; omega + have hv := nodeAt_valSc (s := s) H.ksFit hcl ho + unfold ksSizeA; rw [hv]; omega + +/-- The node words of a core of `Z.Cs` are in the narrow scheduler block `SKsA`. -/ +theorem SchedSRN.node_sks {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} {σ : Fl4SpecA64.State Unit} + (H : SchedSRN st Z m κ s σ) {c o : U64} (hc : c ∈ Z.Cs) (ho : o.val < 4288) + (hI : o.val ≠ 120 ∧ o.val ≠ 128 := by first | omega | (simp only [global_simps]; decide) | simp [global_simps]) : + SKsA (L.ksb s) (L.nodeAt s c o) := by + obtain ⟨nn, -, hn, hcs⟩ := H.csN + have hcl : c.val < 128 := by have := (hcs c).1 hc; omega + have hv := nodeAt_valSc (s := s) H.ksFit hcl ho + unfold SKsA; rw [hv] + right; right; right + refine ⟨by omega, by omega, ?_, ?_⟩ <;> omega + +/-- **A scheduler-queue footprint is in `SWrA`** (module docstring). -/ +theorem SchedSRN.qfoot_swr {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} + {σ : Fl4SpecA64.State Unit} (H : SchedSRN st Z m κ s σ) {W : List U64} {c t b : U64} {P : Nat → Prop} + (hW : ∀ w ∈ W, w ∈ Z.T) (hc : c ∈ Z.Cs) (ht : t ∈ Z.T) (hb : L.QFootN s W c P t b) + (hP : ∀ o, P o → o ≠ 120 ∧ o ≠ 128 := by + intro o h; first | omega | (simp only at h; omega) | (unfold RelOffs at h; omega)) : + SWrA (st := st) Z s b := by + rcases hb with ⟨w, hw, e | e⟩ | ⟨o, hPo, ho, -, e⟩ | e + · exact Or.inr ⟨w, hW w hw, Or.inr (Or.inl e)⟩ + · exact Or.inr ⟨w, hW w hw, Or.inr (Or.inr (Or.inl e))⟩ + · rw [e]; exact Or.inl (H.node_sks hc ho (hP o hPo)) + · exact Or.inr ⟨t, ht, Or.inr (Or.inr (Or.inr e))⟩ + +/-- **`enqueue`'s footprint is in `SWrA`.** -/ +theorem SchedSRN.enq_swr {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} + {σ : Fl4SpecA64.State Unit} (H : SchedSRN st Z m κ s σ) {t b : U64} {P : Nat → Prop} (ht : t ∈ Z.T) + (hb : L.QFootN s (t :: Z.Q (L.mem s (affA t)) (L.mem s (prioA t))) (L.mem s (affA t)) P t b) + (hP : ∀ o, P o → o ≠ 120 ∧ o ≠ 128 := by + intro o h; first | omega | (simp only at h; omega) | (unfold RelOffs at h; omega)) : + SWrA (st := st) Z s b := by + refine H.qfoot_swr (fun w hw => ?_) (H.core.aff t ht) ht hb hP + rcases List.mem_cons.1 hw with rfl | hw + · exact ht + · exact H.core.memT (H.core.aff t ht) (H.core.prio t ht) hw + +/-- **The write of the action word is in `SWrA`.** -/ +theorem SchedSRN.act_wr {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} + {σ : Fl4SpecA64.State Unit} (H : SchedSRN st Z m κ s σ) (v : U64) : + WritesIn st s (L.wr s (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) v) (SWrA (st := st) Z s) := by + refine (writesIn_wr (nodeAt_aligned (H.core.nodes _ H.sch.cpuCs) (by simp [layout.node.SCHED_ACTION]) + (by simp [layout.node.SCHED_ACTION]))).mono fun b hb => ?_ + rw [hb] + exact Or.inl (H.node_sks H.sch.cpuCs (by simp [layout.node.SCHED_ACTION]) (by simp [layout.node.SCHED_ACTION])) + +/-- The `REPROGRAM` word is in `SWrA`. -/ +theorem SchedSRN.repr_swr {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} + {σ : Fl4SpecA64.State Unit} (H : SchedSRN st Z m κ s σ) : + SWrA (st := st) Z s (L.nodeAt s (L.cpu s) layout.node.REPROGRAM) := + Or.inl (H.node_sks H.sch.cpuCs (by simp [layout.node.REPROGRAM]) (by simp [layout.node.REPROGRAM])) + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScSchedA.lean b/proofs/Fl4ProofsA64/P3/ScSchedA.lean new file mode 100644 index 000000000..3cfd58479 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScSchedA.lean @@ -0,0 +1,1022 @@ +/- +P3 (aarch64, A64-P3-MCS gen 2, gen 3): **`schedule_tcb` refines `set_thread_state_act`** over the scheduler relation +with the SC relation (x86 `P3/SchedTcb`'s `schedulable_eq` / `SchedRel.set_action` / `schedule_tcb_refines`, +ported), the `scheduleTcb` contract of the IPC line's `SchedOpsA` / `SchedOpsEA`. + +* **`SchedSRA st Z m κ s σ`** — the `SR` the scheduler contracts are proved for (ruling 21:31Z item 2): `JAtZA`'s + scheduler part (`SchedRelA`, `SchedRestA`, and the placement facts of `JAtZA` they need: `thT`, `place`, `csN`, + `ksFit`, `curT`) ∧ `ScStateRelA` ∧ `ScPlacedA`, plus (gen 3, ruling 22:14Z) the multi-core queue relation + **`core : SchedCoreA`** (`P3/ScCoreA`, every core's ready / release lists and bits; x86 `SchedRel.lists`), `curOn` + (the current thread is on the current core: x86 `SchedCore.aff` at the current thread) and the spec's `runQ` / + `runR` (l4v `valid_ready_qs` / `valid_release_q`'s runnable part, x86 P1 `runQ` / `runR` read through `SchedRelA.tcb`). + Gen 2's `relCur` field is now **`SchedSRA.relCur`**, a projection (`SchedCoreA.relBit` at the current thread, + `curOn`, `SchedRestA.rel`). `JAtZA.toSchedSRA` builds it from `JAtZA` and A64 P1's `SchedQueuesInvX`. +* **`SchedSRA.rebuild`** (gen 3): `SchedSRA` after a step that keeps what the relation reads outside the queues + (node words below the IPI word of every core, the domain words, the TCBs' affinity / priority / domain / SC words, + the SC objects, the spec's scheduler scalars), with new queue lists `Q'`. +* **`stsActA`** — the outcome of l4v's `set_thread_state_act` (the IPC line's `NtfnSpecA.stsAct`, the same text: + `rfl` at the join). +* **`is_schedulable_eqA`** (FL4's `is_schedulable` as a decision of its words) and **`schedulable_eqA`** (x86 + `schedulable_eq`): it is the spec's `schedulable` of a thread of `Z.T` whose release bit is related. +* **`SchedSRA.set_action`** (x86 `SchedRel.set_action`): writing the current core's `SCHED_ACTION` word keeps + `SchedSRA` with the spec's action replaced by the word's (`actOfA`). +* **`schedule_tcb_refinesA`** (x86 `schedule_tcb_refines`, `SchedHyps.scheduleTcb`): `sched::schedule_tcb t` writes at + most the action word (`WritesIn … (· = action word)`, from which the IPC footprints' `FootEq` follows) and + `SchedSRA` holds of `stsActA σ (specPtr t)` after. + +Gen 4 (ruling 22:45Z): `sch` is the state-free **`SchedRelNoStA`** (`SchedRelA` without its state-code conjunct; +`SchedRelA.noSt` / `SchedRelNoStA.withSt` with **`StEqA`**), and `SchedSRA` gains **`rd : RdA σ`** and **`actOn`** (a +switch target is a TCB of the current core); `schedulable_eqA` / `schedule_tcb_refinesA` take `StEqA`. + +Gen 6 (ruling 23:44Z): `SchedSRA` is **`SchedSRN`** (every field but `rd`) extended with `rd`; **`SchedSRN.rebuildG`** +(gen 5's `rebuildS` without `rd`, new release lists `R'`, the TCBs' SC words given); `ScInvA` gains `relSc` / +`sorted` / `back` (x86 `Inv.rel` / `sorted` / `back`, **`specRtA`** = x86 `specRt`); `scPart` / `relOn` / +`ksb_al` / `tcb_ks` are stated on `SchedSRN`. + +No named premise. `curOn` is the one field that is not a projection of `JAtZA` / P1's queue invariant: FL4's +`migrate_tcb` may move the current thread's affinity mid-entry (open question for the join). +-/ +import Fl4ProofsA64.P3.ScCoreA +import Fl4ProofsA64.P3.FrameA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## The spec side -/ + +section Spec +open Fl4SpecA64 + +/-- The outcome of `set_thread_state_act t` (= the IPC line's `stsAct`). -/ +def stsActA (σ : State Unit) (t : ObjRef) : State Unit := + if t = σ.curThread ∧ σ.schedulerAction = .resumeCurThread ∧ ¬ schedulable t σ = true + then { σ with schedulerAction := .chooseNewThread } else σ + +theorem stsActA_eq_self {σ : State Unit} {t : ObjRef} + (h : ¬ (t = σ.curThread ∧ σ.schedulerAction = .resumeCurThread ∧ ¬ schedulable t σ = true)) : + stsActA σ t = σ := by + unfold stsActA; simp only [h, ↓reduceIte] + +/-- (gen 3) The scheduler scalars of the spec a step keeps. -/ +structure SFixA (σ σ' : State Unit) : Prop where + curThread : σ'.curThread = σ.curThread + curSc : σ'.curSc = σ.curSc + idleThread : σ'.idleThread = σ.idleThread + curTime : σ'.curTime = σ.curTime + consumedTime : σ'.consumedTime = σ.consumedTime + reprogramTimer : σ'.reprogramTimer = σ.reprogramTimer + curDomain : σ'.curDomain = σ.curDomain + domainTime : σ'.domainTime = σ.domainTime + +theorem SFixA.refl (σ : State Unit) : SFixA σ σ := ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ + +/-- The spec's queues hold runnable TCBs only (l4v `valid_ready_qs` / `valid_release_q`, runnable part). -/ +def RunQA (σ : State Unit) : Prop := + (∀ d p t, t ∈ σ.readyQueues d p → ∃ x, σ.kheap t = some (.tcb x) ∧ runnable x.tcbState = true) ∧ + (∀ t ∈ σ.releaseQueue, ∃ x, σ.kheap t = some (.tcb x) ∧ runnable x.tcbState = true) + +end Spec + +section SpecRd +open Fl4SpecA64 Fl4Spec + +/-- The IPC line's `released` (l4v `released_sc`), identical body (gen 4: moved here from `ScStableA`). -/ +def releasedSc (σ : State Unit) (y : SchedContext) : Prop := + refillReady σ.curTime (refillHd y) = true ∧ refillSufficient 0 (refillHd y) = true + +/-- The IPC line's `inClassR`, identical body. -/ +def inClassRSc (ts : ThreadState) (f : Option Fault) : Prop := + runnable ts = true ∨ (∃ n, ts = .blockedOnNotification n) ∨ + (∃ e d, ts = .blockedOnSend e d ∧ ∀ b, f ≠ some (.timeout b)) ∨ (∃ e r d, ts = .blockedOnReceive e r d) + +/-- The IPC line's `RelIfBound`, identical body. -/ +def RelIfBoundSc (σ : State Unit) (p : ObjRef) : Prop := + ∀ x scp y n, σ.kheap p = some (.tcb x) → x.tcbSchedContext = some scp → + σ.kheap scp = some (.schedContext y n) → scActive y = true → p ∉ σ.releaseQueue → releasedSc σ y + +/-- (gen 3, a `SchedSRA` field since gen 4; the IPC line's gen-6 `SchedOpsEA.rd`, x86 `ScInv.Inv.rd`, l4v +`released_ipc_queues`) A TCB in `inClassR` has a released SC if bound to an active one and not released. -/ +def RdA (σ : State Unit) : Prop := + ∀ u x, σ.kheap u = some (.tcb x) → inClassRSc x.tcbState x.tcbFault → RelIfBoundSc σ u + +/-- (gen 4) `RdA` reads the heap, the release queue and the clock only. -/ +theorem RdA.of_eq {σ σ' : State Unit} (h : RdA σ) (hk : σ'.kheap = σ.kheap) (ht : σ'.curTime = σ.curTime) + (hr : σ'.releaseQueue = σ.releaseQueue) : RdA σ' := by + intro u x hx hc x' scp y n h1 h2 h3 h4 h5 + rw [hk] at hx h1 h3 + rw [hr] at h5 + have := h u x hx hc x' scp y n h1 h2 h3 h4 h5 + unfold releasedSc at this ⊢ + rw [ht]; exact this + +/-- (gen 5, ruling 23:14Z item 3) An active non-round-robin SC's refills (x86 `ScInv.RefOK`, l4v `valid_refills`): +non-empty, positive amounts summing below `2 ^ 64`, the head holding at least `MIN_BUDGET`. -/ +def RefOKA (y : SchedContext) : Prop := + y.scRefills ≠ [] ∧ (∀ r ∈ y.scRefills, 0 < r.rAmount.toNat) ∧ + (y.scRefills.map fun r => r.rAmount.toNat).sum < 2 ^ 64 ∧ + Fl4SpecA64.MIN_BUDGET ≤ (Fl4SpecA64.refillHd y).rAmount + +/-- (gen 5) An active round-robin SC's refills (x86 `ScInv.RRok`, l4v `valid_refills`). -/ +def RRokA (y : SchedContext) : Prop := + 2 ≤ y.scRefills.length ∧ (y.scRefills.map fun r => r.rAmount.toNat).sum < 2 ^ 64 ∧ + Fl4SpecA64.MIN_BUDGET ≤ (Fl4SpecA64.refillHd y).rAmount + +/-- (gen 6) **The spec's ready time of a TCB** (x86 `ScRelease.specRt`): its SC's head refill's time. -/ +def specRtA (σ : State Unit) (u : ObjRef) : Time := the (readTcbReadyTime (Z := Unit) u σ) + +/-- (gen 6) x86 `readTcbReadyTime_eq`, ported. -/ +theorem readTcbReadyTime_eqA {σ : State Unit} {u p : ObjRef} {x : Tcb} {y : SchedContext} {n : Nat} + (hk : σ.kheap u = some (.tcb x)) (hsc : x.tcbSchedContext = some p) + (hky : σ.kheap p = some (.schedContext y n)) (hne : y.scRefills ≠ []) : + readTcbReadyTime (Z := Unit) u σ = some (refillHd y).rTime := by + have h0 : getTcb (Z := Unit) u σ = some x := by simp [getTcb, readObject, hk] + have h1 : threadRead (Z := Unit) Tcb.tcbSchedContext u σ = some (some p) := by + change Option.bind (getTcb (Z := Unit) u σ) (fun a => Lookup.oreturn a.tcbSchedContext σ) = _ + rw [h0, ← hsc]; rfl + have h2 : readReadyTime (Z := Unit) p σ = some (refillHd y).rTime := by + show Option.bind (readRefillHead p σ) (fun h => Lookup.oreturn h.rTime σ) = _ + rw [readRefillHead_eqA hky hne]; rfl + show Option.bind (threadRead Tcb.tcbSchedContext u σ) (fun o => Option.bind (Lookup.oassert (o ≠ none) σ) + (fun _ => readReadyTime (the o) σ)) = _ + rw [h1] + simp only [Option.bind_some, Lookup.oassert, ne_eq, reduceCtorEq, not_false_eq_true, ↓reduceIte, Lookup.oreturn] + exact h2 + +theorem specRtA_of {σ : State Unit} {u p : ObjRef} {x : Tcb} {y : SchedContext} {n : Nat} + (hk : σ.kheap u = some (.tcb x)) (hsc : x.tcbSchedContext = some p) + (hky : σ.kheap p = some (.schedContext y n)) (hne : y.scRefills ≠ []) : + specRtA σ u = (refillHd y).rTime := by + unfold specRtA; rw [readTcbReadyTime_eqA hk hsc hky hne]; rfl + +/-- (gen 8, ruling 00:50Z) **`p` names no idle SC**: neither `idle_sc_ptr` nor any core's idle-SC address. -/ +def NotIdleScA (m : Fl4SpecA64.McState) (p : ObjRef) : Prop := + p ≠ Fl4SpecA64.idleScPtr ∧ ∀ c < Fl4SpecA64.fl4A64Platform.numNodes, p ≠ m.idleScOf c + +/-- **(gen 5, ruling 23:14Z items 2–3; gen 6 + `relSc` / `sorted` / `back`) The spec-side SC invariant `SchedSRA` +carries**: x86 `ScInv.Inv.refills` / `rr` (`valid_refills`), no notification's SC is the view's idle SC, by either of +its names (`idle_sc_ptr`, and the current core's idle-SC address `m.idleScOf m.curCore`: the FL4 side of +`NtfnRelA.sc`'s word, read through `optRefA`); (gen 6, for `postpone`) x86 `Inv.rel` / `RelOK.sc` / `RelOK.act` +(l4v `valid_release_q`: the release queue's members are TCBs bound to an active SC), `Inv.sorted` (l4v +`sorted_release_q`) and `Inv.back` (l4v `sym_refs` of the TCB / SC binding). -/ +structure ScInvA (m : Fl4SpecA64.McState) (σ : State Unit) : Prop where + refills : ∀ p y n, σ.kheap p = some (.schedContext y n) → scActive y = true → y.scPeriod ≠ 0 → RefOKA y + /-- (gen 8, ruling 00:50Z) Idle SCs are excluded from `RRokA` (`NotIdleScA`: l4v's boot idle SC has a single + `⟨0, 0⟩` refill and is never charged, unblocked, donated or postponed); every active round-robin SC, idle or not, + has refills (x86 `Inv.ne`). -/ + rr : ∀ p y n, σ.kheap p = some (.schedContext y n) → scActive y = true → y.scPeriod = 0 → + y.scRefills ≠ [] ∧ (NotIdleScA m p → RRokA y) + ntfnIdle : ∀ q x, σ.kheap q = some (.notification x) → x.ntfnSc ≠ some Fl4SpecA64.idleScPtr + /-- (gen 8) Every core's idle SC (was: the view's), so a donated SC is `NotIdleScA`. -/ + ntfnIdleOf : ∀ q x, σ.kheap q = some (.notification x) → ∀ c < Fl4SpecA64.fl4A64Platform.numNodes, + x.ntfnSc ≠ some (m.idleScOf c) + /-- (gen 6) x86 `Inv.rel` without `runnable` (that is `RunQA`'s). -/ + relSc : ∀ u ∈ σ.releaseQueue, ∃ x p y n, σ.kheap u = some (.tcb x) ∧ x.tcbSchedContext = some p ∧ + σ.kheap p = some (.schedContext y n) ∧ scActive y = true + /-- (gen 6) x86 `Inv.sorted`. -/ + sorted : σ.releaseQueue.Pairwise (fun a b => specRtA σ a ≤ specRtA σ b) + /-- (gen 6) x86 `Inv.back`. -/ + back : ∀ u x p, σ.kheap u = some (.tcb x) → x.tcbSchedContext = some p → + ∃ y n, σ.kheap p = some (.schedContext y n) ∧ y.scTcb = some u + /-- (gen 11, ruling 02:05Z) **A TCB that is no core's idle thread is bound to no idle SC** (the restricted + `tcbScNotIdle`: at boot every idle thread is bound to its idle SC, `P3/ScIdleA.boot_tcbSc_idleA`; l4v + `init_A_st`). Pointer-based, so steps that keep every TCB's SC pointer keep it. -/ + tcbScNotIdle : ∀ u x p, σ.kheap u = some (.tcb x) → x.tcbSchedContext = some p → + (∀ c < Fl4SpecA64.fl4A64Platform.numNodes, u ≠ m.idleThreadOf c) → NotIdleScA m p + +/-- An active SC has refills (x86 `Inv.ne`, from `refills` / `rr`). -/ +theorem ScInvA.ne {m : Fl4SpecA64.McState} {σ : State Unit} (h : ScInvA m σ) {p : ObjRef} {y : SchedContext} + {n : Nat} (hk : σ.kheap p = some (.schedContext y n)) (ha : scActive y = true) : y.scRefills ≠ [] := by + by_cases hp : y.scPeriod = 0 + · exact (h.rr p y n hk ha hp).1 + · exact (h.refills p y n hk ha hp).1 + +/-- (gen 6) **The release-queue part of `ScInvA` across a step** that keeps the queue, each member's TCB's SC and +that SC's object (x86 `RelOK.transfer'`). -/ +theorem ScInvA.relKeep {m : Fl4SpecA64.McState} {σ σ' : State Unit} (h : ScInvA m σ) + (hr : σ'.releaseQueue = σ.releaseQueue) + (ht : ∀ u ∈ σ.releaseQueue, ∀ x, σ.kheap u = some (.tcb x) → + ∃ x', σ'.kheap u = some (.tcb x') ∧ x'.tcbSchedContext = x.tcbSchedContext) + (hs : ∀ u ∈ σ.releaseQueue, ∀ x p, σ.kheap u = some (.tcb x) → x.tcbSchedContext = some p → + σ'.kheap p = σ.kheap p) : + (∀ u ∈ σ'.releaseQueue, ∃ x p y n, σ'.kheap u = some (.tcb x) ∧ x.tcbSchedContext = some p ∧ + σ'.kheap p = some (.schedContext y n) ∧ scActive y = true) ∧ + σ'.releaseQueue.Pairwise (fun a b => specRtA σ' a ≤ specRtA σ' b) := by + have key : ∀ u ∈ σ.releaseQueue, ∃ x p y n, σ'.kheap u = some (.tcb x) ∧ x.tcbSchedContext = some p ∧ + σ'.kheap p = some (.schedContext y n) ∧ scActive y = true ∧ specRtA σ' u = specRtA σ u := by + intro u hu + obtain ⟨x, p, y, n, hk, hsc, hky, ha⟩ := h.relSc u hu + obtain ⟨x', hk', e⟩ := ht u hu x hk + have hky' : σ'.kheap p = some (.schedContext y n) := by rw [hs u hu x p hk hsc]; exact hky + have hne := h.ne hky ha + refine ⟨x', p, y, n, hk', by rw [e]; exact hsc, hky', ha, ?_⟩ + rw [specRtA_of hk' (by rw [e]; exact hsc) hky' hne, specRtA_of hk hsc hky hne] + refine ⟨fun u hu => ?_, ?_⟩ + · rw [hr] at hu + obtain ⟨x, p, y, n, h1, h2, h3, h4, -⟩ := key u hu + exact ⟨x, p, y, n, h1, h2, h3, h4⟩ + · rw [hr] + refine List.Pairwise.imp_of_mem (fun {a b} ha hb hab => ?_) h.sorted + obtain ⟨-, -, -, -, -, -, -, -, ea⟩ := key a ha + obtain ⟨-, -, -, -, -, -, -, -, eb⟩ := key b hb + rw [ea, eb]; exact hab + +/-- `ScInvA` reads the heap and the release queue only (gen 6: + `hr`). -/ +theorem ScInvA.of_kheap {m : Fl4SpecA64.McState} {σ σ' : State Unit} (h : ScInvA m σ) (hk : σ'.kheap = σ.kheap) + (hr : σ'.releaseQueue = σ.releaseQueue) : ScInvA m σ' := + have hR := h.relKeep hr (fun u _ x hx => ⟨x, by rw [hk]; exact hx, rfl⟩) (fun _ _ _ p _ _ => by rw [hk]) + ⟨fun p y n hy => h.refills p y n (by rw [← hk]; exact hy), fun p y n hy => h.rr p y n (by rw [← hk]; exact hy), + fun q x hx => h.ntfnIdle q x (by rw [← hk]; exact hx), fun q x hx => h.ntfnIdleOf q x (by rw [← hk]; exact hx), + hR.1, hR.2, fun u x p hx hp => by rw [hk] at hx ⊢; exact h.back u x p hx hp, + fun u x p hx hp => h.tcbScNotIdle u x p (by rw [← hk]; exact hx) hp⟩ + +end SpecRd + +theorem mem_map_specPtrSc {xs : List U64} {t : U64} : specPtr t ∈ xs.map specPtr ↔ t ∈ xs := by + rw [List.mem_map] + exact ⟨fun ⟨y, hy, e⟩ => (specPtr_inj e) ▸ hy, fun h => ⟨t, h, rfl⟩⟩ + +/-! ## The state-free scheduler relation (gen 4, ruling 22:45Z item 3) -/ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **`SchedRelA` without the thread-state code** (gen 4, ruling 22:45Z item 3; the A64 twin of x86 +`SchedSpec.tcb`, `SchedRel.lean:98`, which relates domain, SC and priority but not the state): `JointA`'s +`SchedRelA` with its `tcb` field's `stA ↔ tsCodeA` conjunct left out (that conjunct is the IPC relations' +`EpStateRel` / `NtfnStateRel` content; `StEqA`). `SchedRelA.noSt` projects to it, `SchedRelNoStA.withSt` rebuilds +`SchedRelA` from it and `StEqA`. -/ +structure SchedRelNoStA (T C Cs : Set U64) (curOf curScOf : Fl4SpecA64.CoreId → Fl4SpecA64.ObjRef) (s : S) + (σ : Fl4SpecA64.State Unit) (m : Fl4SpecA64.McState) : Prop where + core : m.curCore = (L.cpu s).val + cpuCs : L.cpu s ∈ Cs + curOfW : ∀ c ∈ Cs, curOf c.val = specPtr (L.mem s (L.nodeAt s c layout.node.CUR_THREAD)) + curScOfW : ∀ c ∈ Cs, curScOf c.val = scRenA m (L.mem s (L.nodeAt s c layout.node.CUR_SC)) + cur : curOf m.curCore = σ.curThread + csc : curScOf m.curCore = σ.curSc + idle : σ.idleThread = specPtr (L.idleW s) + idleOf : ∀ c ∈ Cs, m.idleThreadOf c.val = specPtr (L.mem s (L.nodeAt s c layout.node.IDLE_THREAD)) + idleScOf : ∀ c ∈ Cs, m.idleScOf c.val = specPtr (L.mem s (L.nodeAt s c layout.node.IDLE_SC)) + act : σ.schedulerAction = actOfA (L.actionW s) + time : σ.curTime = (L.curTime s).bv + aff : ∀ t ∈ T, m.tcbAffinity (specPtr t) = some (L.mem s (affA t)).val + affT : ∀ p c, m.tcbAffinity p = some c → ∃ t ∈ T, specPtr t = p + /-- Every TCB of `T` is a spec TCB with FL4's SC (renamed); its state is `StEqA`'s. -/ + tcb : ∀ t ∈ T, ∃ x, σ.kheap (specPtr t) = some (.tcb x) ∧ x.tcbSchedContext = optScA m (L.mem s (scA t)) + sc : ∀ c ∈ C, ∃ y n, σ.kheap (scRenA m c) = some (.schedContext y n) ∧ y.scTcb = optRefA (L.mem s (wa c 24#u64)) + scOff : ∀ c ∈ C, specPtr c ≠ m.idleScOf m.curCore → specPtr c ≠ Fl4SpecA64.idleScPtr + +/-- **FL4's state words are the spec's thread states** (gen 4, ruling 22:45Z item 3): the conjunct `SchedRelNoStA` +leaves out, a projection of `EpStateRel` / `NtfnStateRel` / `SchedRelA`. -/ +def StEqA (T : Set U64) (mem : U64 → U64) (σ : Fl4SpecA64.State Unit) : Prop := + ∀ t ∈ T, ∀ x, σ.kheap (specPtr t) = some (.tcb x) → (mem (stA t)).val = tsCodeA x.tcbState + +variable {T C Cs : Set U64} {curOf curScOf : Fl4SpecA64.CoreId → Fl4SpecA64.ObjRef} {s : S} + {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- `SchedRelA` projects to `SchedRelNoStA`. -/ +theorem SchedRelA.noSt (h : SchedRelA (L := L) T C Cs curOf curScOf s σ m) : + SchedRelNoStA (L := L) T C Cs curOf curScOf s σ m := + ⟨h.core, h.cpuCs, h.curOfW, h.curScOfW, h.cur, h.csc, h.idle, h.idleOf, h.idleScOf, h.act, h.time, h.aff, h.affT, + fun t ht => let ⟨x, a, _, c⟩ := h.tcb t ht; ⟨x, a, c⟩, h.sc, h.scOff⟩ + +/-- `SchedRelA` is `SchedRelNoStA` with `StEqA`. -/ +theorem SchedRelNoStA.withSt (h : SchedRelNoStA (L := L) T C Cs curOf curScOf s σ m) (hst : StEqA T (L.mem s) σ) : + SchedRelA (L := L) T C Cs curOf curScOf s σ m := + ⟨h.core, h.cpuCs, h.curOfW, h.curScOfW, h.cur, h.csc, h.idle, h.idleOf, h.idleScOf, h.act, h.time, h.aff, h.affT, + fun t ht => let ⟨x, a, c⟩ := h.tcb t ht; ⟨x, a, hst t ht x a, c⟩, h.sc, h.scOff⟩ + +/-- `StEqA` is a projection of `SchedRelA`. -/ +theorem SchedRelA.stEq (h : SchedRelA (L := L) T C Cs curOf curScOf s σ m) : StEqA T (L.mem s) σ := + fun t ht x hx => by + obtain ⟨x', h1, h2, -⟩ := h.tcb t ht + rw [hx] at h1 + injection h1 with h1; injection h1 with h1 + subst h1; exact h2 + +/-- The view's current thread is the current core's `CUR_THREAD` (`SchedRelA.curThread`). -/ +theorem SchedRelNoStA.curThread (h : SchedRelNoStA (L := L) T C Cs curOf curScOf s σ m) : + σ.curThread = specPtr (L.curW s) := by + rw [← h.cur, h.core]; exact h.curOfW _ h.cpuCs + +end + +/-! ## The relation -/ + +section +variable {S : Type} (st : machine.Store S) [L : LawfulStore st] + +/-- **(gen 6) `SchedSRA` without `rd`** (ruling 23:44Z item 1: the part of the relation that holds between +`sched_context_donate` and `sched_context_resume`; `SchedSRA` extends it with `rd`). -/ +structure SchedSRN (Z : XSetsA) (m : Fl4SpecA64.McState) (κ : U64 → U64) (s : S) (σ : Fl4SpecA64.State Unit) : + Prop where + /-- (gen 4: the state-free `SchedRelNoStA`, ruling 22:45Z item 3) -/ + sch : SchedRelNoStA (L := L) Z.T Z.C Z.Cs Z.curOf Z.curScOf s σ m + rest : SchedRestA (L := L) Z s σ m + scr : ScStateRelA st Z.C m s σ + scp : ScPlacedA Z.th (L.ksb s) Z.C m σ + /-- `JAtZA.thT`. -/ + thT : ∀ t, Z.th t = some .tcb ↔ t ∈ Z.T + /-- `JAtZA.place`. -/ + place : PlaceA Z.th (L.ksb s) + /-- `JAtZA.csN`. -/ + csN : ∃ n, 0 < n ∧ n ≤ 128 ∧ ∀ c : U64, c ∈ Z.Cs ↔ c.val < n + /-- `JAtZA.ksFit`. -/ + ksFit : (L.ksb s).val + ksSizeA ≤ 2 ^ 64 + /-- `JAtZA.curT` at the current core. -/ + curT : L.curW s ∈ Z.T + /-- (gen 3) The scheduler queues of every core (`SchedCoreA`; A64 P1's `SchedQueuesInvX` projected). -/ + core : SchedCoreA st Z.T Z.Cs s Z.Q Z.R + /-- (gen 3) The current thread is on the current core (x86 `SchedCore.aff` at the current thread). -/ + curOn : L.mem s (affA (L.curW s)) = L.cpu s + /-- (gen 3) The spec's queues hold runnable TCBs (A64 P1 `runQ` / `runR` through `SchedRelA.tcb`). -/ + run : RunQA σ + /-- (gen 4, ruling 22:45Z item 2) A switch target is a TCB of the current core (FL4's `possible_switch_to` writes + the action only for a target with `affinity = cur_cpu`). -/ + actOn : ∀ u, σ.schedulerAction = .switchThread (specPtr u) → u ∈ Z.T ∧ L.mem s (affA u) = L.cpu s + /-- (gen 5) `JAtZA.cscC` at the current core: the current SC is a related SC. -/ + cscC : L.mem s (L.nodeAt s (L.cpu s) layout.node.CUR_SC) ∈ Z.C + /-- (gen 5, ruling 23:14Z items 2–3) The spec-side SC invariant (`valid_refills`, notifications off the idle SC). -/ + inv : ScInvA m σ + /-- (gen 7, ruling 00:22Z item 4) **Every related SC's `CORE` word is the fixed map `κ`'s** (`scCoreOf`, the twin of + `m.tcbAffinity`: the SC's core has no spec counterpart, so the relation fixes it across the steps it relates; + `SchedSRA.donLocalKeepA`). -/ + scCore : ∀ c ∈ Z.C, L.mem s (scCoreA c) = κ c + /-- (Stage-3) `JAtZA.cscC` at **every** core: each node's current SC is a related SC (what `JAtZAX.of_sched` needs + for the other cores). -/ + cscAll : ∀ c ∈ Z.Cs, L.mem s (L.nodeAt s c layout.node.CUR_SC) ∈ Z.C + +/-- **The scheduler relation with the SC relation** (module docstring; the `SR` of `SchedOpsA`; gen 6: `SchedSRN` +with `rd`). -/ +structure SchedSRA (Z : XSetsA) (m : Fl4SpecA64.McState) (κ : U64 → U64) (s : S) (σ : Fl4SpecA64.State Unit) : + Prop + extends SchedSRN st Z m κ s σ where + /-- (gen 4, ruling 22:35Z / 22:45Z) l4v `released_ipc_queues` (x86 `ScInv.Inv.rd`; the IPC line's `rd`). -/ + rd : RdA σ + +end + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +/-- **`SchedSRA` from `JAtZA`**, the SC relation and placement, A64 P1's queue invariant (with `Z`'s lists), the +current thread on its core and the spec queues' runnable part. -/ +theorem JAtZA.toSchedSRA {AR : A → HwRootA → Prop} {Z : XSetsA} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} (J : JAtZA (L := L) AR Z s a σ m) (h : ScStateRelA st Z.C m s σ) + (P : ScPlacedA Z.th (L.ksb s) Z.C m σ) {Ω X : Set U64} {H : Heap} + (hq : SchedQueuesInvX st Z.T Z.Cs Ω H X s Z.Q Z.R) (hon : L.mem s (affA (L.curW s)) = L.cpu s) + (hrun : RunQA σ) (hrd : RdA σ) + (hact : ∀ u, σ.schedulerAction = .switchThread (specPtr u) → u ∈ Z.T ∧ L.mem s (affA u) = L.cpu s) + (hinv : ScInvA m σ) (hκ : ∀ c ∈ Z.C, L.mem s (scCoreA c) = κ c) : + SchedSRA st Z m κ s σ := + ⟨⟨SchedRelA.noSt J.sch, J.rest, h, P, J.thT, J.place, J.csN, J.ksFit, J.curT _ J.sch.cpuCs, hq.toCoreA, hon, hrun, + hact, J.cscC _ J.sch.cpuCs, hinv, hκ, J.cscC⟩, hrd⟩ + +/-- (gen 4) The action word, when it names a thread, names a TCB of the current core (`actOn`, read at the word). -/ +theorem SchedSRA.hsw {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} {σ : Fl4SpecA64.State Unit} + (H : SchedSRA st Z m κ s σ) : + L.actionW s ≠ 0#u64 → L.actionW s ≠ 1#u64 → L.actionW s ∈ Z.T ∧ L.mem s (affA (L.actionW s)) = L.cpu s := + fun e0 e1 => H.actOn _ (by rw [H.sch.act]; simp [actOfA, e0, e1]) + +/-- **A thread on the current core has its release bit iff it is in the spec's release queue.** -/ +theorem SchedSRN.relOn {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} {σ : Fl4SpecA64.State Unit} + (H : SchedSRN st Z m κ s σ) {t : U64} (ht : t ∈ Z.T) (hon : L.mem s (affA t) = L.cpu s) : + (L.mem s (inqA t) &&& sched.Q_RELEASE) = 0#u64 ↔ specPtr t ∉ σ.releaseQueue := by + rw [H.core.relBit ht hon, H.rest.rel, mem_map_specPtrSc] + +/-- **Gen 2's `relCur`, a projection**: the current thread's release bit is its membership of the release queue. -/ +theorem SchedSRA.relCur {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} {σ : Fl4SpecA64.State Unit} + (H : SchedSRA st Z m κ s σ) : + (L.mem s (inqA (L.curW s)) &&& sched.Q_RELEASE) = 0#u64 ↔ specPtr (L.curW s) ∉ σ.releaseQueue := + H.relOn H.curT H.curOn + +/-- Rebuild the scheduler while supplying the new domain-global words. +The thread cursors and the idle thread stay fixed. -/ +theorem SchedSRN.rebuildNodeDomainTimeA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ κ' : U64 → U64} {s s' : S} {σ σ' : Fl4SpecA64.State Unit} + (H : SchedSRN st Z m κ s σ) {Q' : U64 → U64 → List U64} {R' : U64 → List U64} + (hks : L.ksb s' = L.ksb s) (hcpu : L.cpu s' = L.cpu s) + (hnode : ∀ c ∈ Z.Cs, ∀ o : U64, o.val < 56 → o.val % 8 = 0 → o.val ≠ 16 → o.val ≠ 40 → o.val ≠ 48 → o.val ≠ 56 → + L.mem s' (L.nodeAt s c o) = L.mem s (L.nodeAt s c o)) + (hdom : σ'.curDomain = 0 ∧ L.mem s' (wa (L.ksb s) 8#u64) = 0#u64) + (hdomTime : σ'.domainTime = (L.mem s' (wa (L.ksb s) 16#u64)).bv) + (htf : ∀ t ∈ Z.T, ∀ K : U64, (K = 3520#u64 ∨ K = 3448#u64 ∨ K = 3432#u64) → + L.mem s' (wa t K) = L.mem s (wa t K)) + (hscS : ScStateRelA st Z.C m s' σ') (hscP : ScPlacedA Z.th (L.ksb s) Z.C m σ') + (htcb : ∀ t ∈ Z.T, ∀ x, σ.kheap (specPtr t) = some (.tcb x) → ∃ y, σ'.kheap (specPtr t) = some (.tcb y) ∧ + y.tcbDomain = x.tcbDomain ∧ y.tcbPriority = x.tcbPriority) + (htcbSc : ∀ t ∈ Z.T, ∃ x, σ'.kheap (specPtr t) = some (.tcb x) ∧ + x.tcbSchedContext = optScA m (L.mem s' (scA t))) + (hscMem : ∀ t ∈ Z.T, L.mem s' (scA t) ≠ 0#u64 → L.mem s' (scA t) ∈ Z.C) + (fCur : σ'.curThread = σ.curThread) (fCsc : σ'.curSc = σ.curSc) (fIdle : σ'.idleThread = σ.idleThread) + (htime : σ'.curTime = (L.mem s' (L.nodeAt s (L.cpu s) layout.node.CUR_TIME)).bv) + (hcons : σ'.consumedTime = (L.mem s' (L.nodeAt s (L.cpu s) layout.node.CONSUMED)).bv) + (hrepr : σ'.reprogramTimer = decide (L.mem s' (L.nodeAt s (L.cpu s) layout.node.REPROGRAM) ≠ 0#u64)) + (hact : σ'.schedulerAction = actOfA (L.mem s' (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION))) + (hactOn : ∀ u, σ'.schedulerAction = .switchThread (specPtr u) → u ∈ Z.T ∧ L.mem s (affA u) = L.cpu s) + (hinv : ScInvA m σ') + (hcore : SchedCoreA st Z.T Z.Cs s' Q' R') + (hrq : ∀ p : U64, p.val < 256 → σ'.readyQueues 0 (BitVec.ofNat 8 p.val) = (Q' (L.cpu s) p).map specPtr) + (hrqd : ∀ d p, d ≠ 0 → σ'.readyQueues d p = []) + (hrel : σ'.releaseQueue = (R' (L.cpu s)).map specPtr) (hrun : RunQA σ') + (hscCore : ∀ c ∈ Z.C, L.mem s' (scCoreA c) = κ' c) : + SchedSRN st { Z with Q := Q', R := R' } m κ' s' σ' := by + have nK : ∀ c ∈ Z.Cs, ∀ o : U64, o.val < 56 → o.val % 8 = 0 → o.val ≠ 16 → o.val ≠ 40 → o.val ≠ 48 → o.val ≠ 56 → + L.mem s' (L.nodeAt s' c o) = L.mem s (L.nodeAt s c o) := fun c hc o h1 h2 h3 h4 h5 h6 => by + rw [nodeAt_of_ksb hks]; exact hnode c hc o h1 h2 h3 h4 h5 h6 + have hct : L.curW s' = L.curW s := by + show L.mem _ (L.nodeAt _ (L.cpu _) _) = _ + rw [hcpu]; exact nK _ H.sch.cpuCs layout.node.CUR_THREAD (by simp [layout.node.CUR_THREAD]) + (by simp [layout.node.CUR_THREAD]) (by simp [layout.node.CUR_THREAD]) (by simp [layout.node.CUR_THREAD]) (by simp [layout.node.CUR_THREAD]) (by simp [layout.node.CUR_THREAD]) + refine + { sch := ?_, rest := ?_, scr := hscS, scp := ?_, thT := H.thT, place := by rw [hks]; exact H.place, + csN := H.csN, ksFit := by rw [hks]; exact H.ksFit, curT := by rw [hct]; exact H.curT, core := hcore, + curOn := ?_, run := hrun, actOn := fun u hu => ?_, cscC := ?_, inv := hinv, + scCore := hscCore, + cscAll := fun c hc => by + rw [nK c hc layout.node.CUR_SC (by simp [layout.node.CUR_SC]) (by simp [layout.node.CUR_SC]) + (by simp [layout.node.CUR_SC]) (by simp [layout.node.CUR_SC]) (by simp [layout.node.CUR_SC]) + (by simp [layout.node.CUR_SC])] + exact H.cscAll c hc } + · exact + { core := by rw [hcpu]; exact H.sch.core + cpuCs := by rw [hcpu]; exact H.sch.cpuCs + curOfW := fun c hc => by + rw [nK c hc layout.node.CUR_THREAD (by simp [layout.node.CUR_THREAD]) (by simp [layout.node.CUR_THREAD]) + (by simp [layout.node.CUR_THREAD]) (by simp [layout.node.CUR_THREAD]) (by simp [layout.node.CUR_THREAD]) (by simp [layout.node.CUR_THREAD])] + exact H.sch.curOfW c hc + curScOfW := fun c hc => by + rw [nK c hc layout.node.CUR_SC (by simp [layout.node.CUR_SC]) (by simp [layout.node.CUR_SC]) + (by simp [layout.node.CUR_SC]) (by simp [layout.node.CUR_SC]) (by simp [layout.node.CUR_SC]) (by simp [layout.node.CUR_SC])] + exact H.sch.curScOfW c hc + cur := by rw [fCur]; exact H.sch.cur + csc := by rw [fCsc]; exact H.sch.csc + idle := by + show σ'.idleThread = specPtr (L.mem _ (L.nodeAt _ (L.cpu _) layout.node.IDLE_THREAD)) + rw [hcpu, fIdle, nK _ H.sch.cpuCs layout.node.IDLE_THREAD (by simp [layout.node.IDLE_THREAD]) + (by simp [layout.node.IDLE_THREAD]) (by simp [layout.node.IDLE_THREAD]) (by simp [layout.node.IDLE_THREAD]) (by simp [layout.node.IDLE_THREAD]) (by simp [layout.node.IDLE_THREAD])] + exact H.sch.idle + idleOf := fun c hc => by + rw [nK c hc layout.node.IDLE_THREAD (by simp [layout.node.IDLE_THREAD]) (by simp [layout.node.IDLE_THREAD]) + (by simp [layout.node.IDLE_THREAD]) (by simp [layout.node.IDLE_THREAD]) (by simp [layout.node.IDLE_THREAD]) (by simp [layout.node.IDLE_THREAD])] + exact H.sch.idleOf c hc + idleScOf := fun c hc => by + rw [nK c hc layout.node.IDLE_SC (by simp [layout.node.IDLE_SC]) (by simp [layout.node.IDLE_SC]) + (by simp [layout.node.IDLE_SC]) (by simp [layout.node.IDLE_SC]) (by simp [layout.node.IDLE_SC]) (by simp [layout.node.IDLE_SC])] + exact H.sch.idleScOf c hc + act := by + show σ'.schedulerAction = actOfA (L.mem _ (L.nodeAt _ (L.cpu _) layout.node.SCHED_ACTION)) + rw [hcpu, nodeAt_of_ksb hks]; exact hact + time := by + show σ'.curTime = (L.mem _ (L.nodeAt _ (L.cpu _) layout.node.CUR_TIME)).bv + rw [hcpu, nodeAt_of_ksb hks] + exact htime + aff := fun t ht => by + rw [show affA t = wa t 3520#u64 from rfl, htf t ht 3520#u64 (by simp)]; exact H.sch.aff t ht + affT := H.sch.affT + tcb := htcbSc + sc := ScStateRelA.schedSc hscS + scOff := H.sch.scOff } + · exact + { rq := fun p hp => by rw [hcpu]; exact hrq p hp + rqDom := hrqd + rel := by rw [hcpu]; exact hrel + consumed := by rw [hcpu, nodeAt_of_ksb hks]; exact hcons + repr := by rw [hcpu, nodeAt_of_ksb hks]; exact hrepr + curDom := by rw [hks]; exact hdom + domTime := by rw [hks]; exact hdomTime + prio := fun t ht => by + obtain ⟨x, h1, h2, h3, h4⟩ := H.rest.prio t ht + obtain ⟨y, hy, e2, e3⟩ := htcb t ht x h1 + refine ⟨y, hy, by rw [e2]; exact h2, ?_, ?_⟩ + · rw [show domA t = wa t 3432#u64 from rfl, htf t ht 3432#u64 (by simp)]; exact h3 + · rw [show prioA t = wa t 3448#u64 from rfl, htf t ht 3448#u64 (by simp), e3]; exact h4 + scMem := hscMem + refillMax := ScStateRelA.refillMax hscS } + · rw [hks]; exact hscP + · rw [hct, hcpu, show affA (L.curW s) = wa (L.curW s) 3520#u64 from rfl, htf _ H.curT 3520#u64 (by simp)] + exact H.curOn + · obtain ⟨hu1, hu2⟩ := hactOn u hu + exact ⟨hu1, by rw [hcpu, show affA u = wa u 3520#u64 from rfl, htf _ hu1 3520#u64 (by simp)]; exact hu2⟩ + · show L.mem _ (L.nodeAt _ (L.cpu _) _) ∈ Z.C + rw [hcpu, nK _ H.sch.cpuCs layout.node.CUR_SC (by simp [layout.node.CUR_SC]) (by simp [layout.node.CUR_SC]) + (by simp [layout.node.CUR_SC]) (by simp [layout.node.CUR_SC]) (by simp [layout.node.CUR_SC]) (by simp [layout.node.CUR_SC])] + exact H.cscC + +/-- **(gen 6) The general rebuild of `SchedSRN`** (gen 5's `rebuildS` without `rd`, generalised for the donation +block): new ready and release lists `Q'` / `R'` with the spec's release queue `R' cpu`, and the TCBs' SC words given +(`htcbSc` / `hscMem`) instead of kept. -/ +theorem SchedSRN.rebuildNodeTimeA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ κ' : U64 → U64} {s s' : S} {σ σ' : Fl4SpecA64.State Unit} + (H : SchedSRN st Z m κ s σ) {Q' : U64 → U64 → List U64} {R' : U64 → List U64} + (hks : L.ksb s' = L.ksb s) (hcpu : L.cpu s' = L.cpu s) + (hnode : ∀ c ∈ Z.Cs, ∀ o : U64, o.val < 56 → o.val % 8 = 0 → o.val ≠ 16 → o.val ≠ 40 → o.val ≠ 48 → o.val ≠ 56 → + L.mem s' (L.nodeAt s c o) = L.mem s (L.nodeAt s c o)) + (hglob : ∀ K : U64, (K = 8#u64 ∨ K = 16#u64) → L.mem s' (wa (L.ksb s) K) = L.mem s (wa (L.ksb s) K)) + (htf : ∀ t ∈ Z.T, ∀ K : U64, (K = 3520#u64 ∨ K = 3448#u64 ∨ K = 3432#u64) → + L.mem s' (wa t K) = L.mem s (wa t K)) + (hscS : ScStateRelA st Z.C m s' σ') (hscP : ScPlacedA Z.th (L.ksb s) Z.C m σ') + (htcb : ∀ t ∈ Z.T, ∀ x, σ.kheap (specPtr t) = some (.tcb x) → ∃ y, σ'.kheap (specPtr t) = some (.tcb y) ∧ + y.tcbDomain = x.tcbDomain ∧ y.tcbPriority = x.tcbPriority) + (htcbSc : ∀ t ∈ Z.T, ∃ x, σ'.kheap (specPtr t) = some (.tcb x) ∧ + x.tcbSchedContext = optScA m (L.mem s' (scA t))) + (hscMem : ∀ t ∈ Z.T, L.mem s' (scA t) ≠ 0#u64 → L.mem s' (scA t) ∈ Z.C) + (hfix : SFixA σ { σ' with consumedTime := σ.consumedTime, curTime := σ.curTime, reprogramTimer := σ.reprogramTimer }) + (htime : σ'.curTime = (L.mem s' (L.nodeAt s (L.cpu s) layout.node.CUR_TIME)).bv) + (hcons : σ'.consumedTime = (L.mem s' (L.nodeAt s (L.cpu s) layout.node.CONSUMED)).bv) + (hrepr : σ'.reprogramTimer = decide (L.mem s' (L.nodeAt s (L.cpu s) layout.node.REPROGRAM) ≠ 0#u64)) + (hact : σ'.schedulerAction = actOfA (L.mem s' (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION))) + (hactOn : ∀ u, σ'.schedulerAction = .switchThread (specPtr u) → u ∈ Z.T ∧ L.mem s (affA u) = L.cpu s) + (hinv : ScInvA m σ') + (hcore : SchedCoreA st Z.T Z.Cs s' Q' R') + (hrq : ∀ p : U64, p.val < 256 → σ'.readyQueues 0 (BitVec.ofNat 8 p.val) = (Q' (L.cpu s) p).map specPtr) + (hrqd : ∀ d p, d ≠ 0 → σ'.readyQueues d p = []) + (hrel : σ'.releaseQueue = (R' (L.cpu s)).map specPtr) (hrun : RunQA σ') + (hscCore : ∀ c ∈ Z.C, L.mem s' (scCoreA c) = κ' c) : + SchedSRN st { Z with Q := Q', R := R' } m κ' s' σ' := by + have hdom : σ'.curDomain = 0 ∧ L.mem s' (wa (L.ksb s) 8#u64) = 0#u64 := by + rw [hfix.curDomain, hglob 8#u64 (Or.inl rfl)] + exact H.rest.curDom + have hdomTime : σ'.domainTime = (L.mem s' (wa (L.ksb s) 16#u64)).bv := by + rw [hfix.domainTime, hglob 16#u64 (Or.inr rfl)] + exact H.rest.domTime + exact H.rebuildNodeDomainTimeA hks hcpu hnode hdom hdomTime htf hscS hscP htcb htcbSc hscMem + hfix.curThread hfix.curSc hfix.idleThread htime hcons hrepr hact hactOn hinv hcore hrq hrqd hrel hrun hscCore + +theorem SchedSRN.rebuildNodeG {Z : XSetsA} {m : Fl4SpecA64.McState} {κ κ' : U64 → U64} {s s' : S} {σ σ' : Fl4SpecA64.State Unit} + (H : SchedSRN st Z m κ s σ) {Q' : U64 → U64 → List U64} {R' : U64 → List U64} + (hks : L.ksb s' = L.ksb s) (hcpu : L.cpu s' = L.cpu s) + (hnode : ∀ c ∈ Z.Cs, ∀ o : U64, o.val < 56 → o.val % 8 = 0 → o.val ≠ 16 → o.val ≠ 40 → o.val ≠ 56 → + L.mem s' (L.nodeAt s c o) = L.mem s (L.nodeAt s c o)) + (hglob : ∀ K : U64, (K = 8#u64 ∨ K = 16#u64) → L.mem s' (wa (L.ksb s) K) = L.mem s (wa (L.ksb s) K)) + (htf : ∀ t ∈ Z.T, ∀ K : U64, (K = 3520#u64 ∨ K = 3448#u64 ∨ K = 3432#u64) → + L.mem s' (wa t K) = L.mem s (wa t K)) + (hscS : ScStateRelA st Z.C m s' σ') (hscP : ScPlacedA Z.th (L.ksb s) Z.C m σ') + (htcb : ∀ t ∈ Z.T, ∀ x, σ.kheap (specPtr t) = some (.tcb x) → ∃ y, σ'.kheap (specPtr t) = some (.tcb y) ∧ + y.tcbDomain = x.tcbDomain ∧ y.tcbPriority = x.tcbPriority) + (htcbSc : ∀ t ∈ Z.T, ∃ x, σ'.kheap (specPtr t) = some (.tcb x) ∧ + x.tcbSchedContext = optScA m (L.mem s' (scA t))) + (hscMem : ∀ t ∈ Z.T, L.mem s' (scA t) ≠ 0#u64 → L.mem s' (scA t) ∈ Z.C) + (hfix : SFixA σ { σ' with consumedTime := σ.consumedTime, reprogramTimer := σ.reprogramTimer }) + (hcons : σ'.consumedTime = (L.mem s' (L.nodeAt s (L.cpu s) layout.node.CONSUMED)).bv) + (hrepr : σ'.reprogramTimer = decide (L.mem s' (L.nodeAt s (L.cpu s) layout.node.REPROGRAM) ≠ 0#u64)) + (hact : σ'.schedulerAction = actOfA (L.mem s' (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION))) + (hactOn : ∀ u, σ'.schedulerAction = .switchThread (specPtr u) → u ∈ Z.T ∧ L.mem s (affA u) = L.cpu s) + (hinv : ScInvA m σ') + (hcore : SchedCoreA st Z.T Z.Cs s' Q' R') + (hrq : ∀ p : U64, p.val < 256 → σ'.readyQueues 0 (BitVec.ofNat 8 p.val) = (Q' (L.cpu s) p).map specPtr) + (hrqd : ∀ d p, d ≠ 0 → σ'.readyQueues d p = []) + (hrel : σ'.releaseQueue = (R' (L.cpu s)).map specPtr) (hrun : RunQA σ') + (hscCore : ∀ c ∈ Z.C, L.mem s' (scCoreA c) = κ' c) : + SchedSRN st { Z with Q := Q', R := R' } m κ' s' σ' := by + refine H.rebuildNodeTimeA hks hcpu (fun c hc o h1 h2 h3 h4 _ h5 => hnode c hc o h1 h2 h3 h4 h5) + hglob htf hscS hscP htcb htcbSc hscMem + ⟨hfix.curThread, hfix.curSc, hfix.idleThread, rfl, rfl, rfl, hfix.curDomain, hfix.domainTime⟩ + ?_ hcons hrepr hact hactOn hinv hcore hrq hrqd hrel hrun hscCore + rw [hfix.curTime, hnode _ H.sch.cpuCs layout.node.CUR_TIME (by simp [layout.node.CUR_TIME]) + (by simp [layout.node.CUR_TIME]) (by simp [layout.node.CUR_TIME]) (by simp [layout.node.CUR_TIME]) + (by simp [layout.node.CUR_TIME])] + exact H.sch.time + +theorem SchedSRN.rebuildG {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s s' : S} {σ σ' : Fl4SpecA64.State Unit} + (H : SchedSRN st Z m κ s σ) {Q' : U64 → U64 → List U64} {R' : U64 → List U64} + (hks : L.ksb s' = L.ksb s) (hcpu : L.cpu s' = L.cpu s) + (hnode : ∀ c ∈ Z.Cs, ∀ o : U64, o.val < 56 → o.val % 8 = 0 → o.val ≠ 16 → o.val ≠ 56 → + L.mem s' (L.nodeAt s c o) = L.mem s (L.nodeAt s c o)) + (hglob : ∀ K : U64, (K = 8#u64 ∨ K = 16#u64) → L.mem s' (wa (L.ksb s) K) = L.mem s (wa (L.ksb s) K)) + (htf : ∀ t ∈ Z.T, ∀ K : U64, (K = 3520#u64 ∨ K = 3448#u64 ∨ K = 3432#u64) → + L.mem s' (wa t K) = L.mem s (wa t K)) + (hscS : ScStateRelA st Z.C m s' σ') (hscP : ScPlacedA Z.th (L.ksb s) Z.C m σ') + (htcb : ∀ t ∈ Z.T, ∀ x, σ.kheap (specPtr t) = some (.tcb x) → ∃ y, σ'.kheap (specPtr t) = some (.tcb y) ∧ + y.tcbDomain = x.tcbDomain ∧ y.tcbPriority = x.tcbPriority) + (htcbSc : ∀ t ∈ Z.T, ∃ x, σ'.kheap (specPtr t) = some (.tcb x) ∧ + x.tcbSchedContext = optScA m (L.mem s' (scA t))) + (hscMem : ∀ t ∈ Z.T, L.mem s' (scA t) ≠ 0#u64 → L.mem s' (scA t) ∈ Z.C) + (hfix : SFixA σ { σ' with reprogramTimer := σ.reprogramTimer }) + (hrepr : σ'.reprogramTimer = decide (L.mem s' (L.nodeAt s (L.cpu s) layout.node.REPROGRAM) ≠ 0#u64)) + (hact : σ'.schedulerAction = actOfA (L.mem s' (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION))) + (hactOn : ∀ u, σ'.schedulerAction = .switchThread (specPtr u) → u ∈ Z.T ∧ L.mem s (affA u) = L.cpu s) + (hinv : ScInvA m σ') + (hcore : SchedCoreA st Z.T Z.Cs s' Q' R') + (hrq : ∀ p : U64, p.val < 256 → σ'.readyQueues 0 (BitVec.ofNat 8 p.val) = (Q' (L.cpu s) p).map specPtr) + (hrqd : ∀ d p, d ≠ 0 → σ'.readyQueues d p = []) + (hrel : σ'.releaseQueue = (R' (L.cpu s)).map specPtr) (hrun : RunQA σ') + (hscCore : ∀ c ∈ Z.C, L.mem s' (scCoreA c) = L.mem s (scCoreA c)) : + SchedSRN st { Z with Q := Q', R := R' } m κ s' σ' := by + refine H.rebuildNodeG hks hcpu (fun c hc o ho h8 e16 _ e56 => hnode c hc o ho h8 e16 e56) + hglob htf hscS hscP htcb htcbSc hscMem + ⟨hfix.curThread, hfix.curSc, hfix.idleThread, hfix.curTime, rfl, rfl, hfix.curDomain, hfix.domainTime⟩ + ?_ hrepr hact hactOn hinv hcore hrq hrqd hrel hrun (fun c hc => (hscCore c hc).trans (H.scCore c hc)) + rw [hfix.consumedTime, hnode _ H.sch.cpuCs layout.node.CONSUMED (by simp [layout.node.CONSUMED]) + (by simp [layout.node.CONSUMED]) (by simp [layout.node.CONSUMED]) (by simp [layout.node.CONSUMED])] + exact H.rest.consumed + +/-- (gen 7) **A related SC's `CORE` word is an aligned word of the SC footprint.** -/ +theorem SchedSRN.scCoreFoot {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} + {σ : Fl4SpecA64.State Unit} (H : SchedSRN st Z m κ s σ) {c : U64} (hc : c ∈ Z.C) : + Aligned (scCoreA c) ∧ ScFootA Z.C m σ (scCoreA c) := by + obtain ⟨x, n, hk, hx⟩ := H.scr.sc c hc + have := hx.hdr (K := 16#u64) (by simp) (by simp) + rw [show scCoreA c = scWA c 16#u64 by unfold scCoreA scWA layout.sc.CORE; rfl] + exact ⟨this.1, c, hc, x, n, hk, hx.in_obj this.2⟩ + +/-- **(gen 5) `rebuild` with the SC relation, its placement and `reprogram_timer` given** (the frame of a step that +changes SC objects and the `REPROGRAM` word, `refill_unblock_check`): the node words below `IPI_PENDING` of every +core but the action and `REPROGRAM` words, the domain words, the TCBs' affinity / priority / domain / SC words, the +TCBs' SC / domain / priority (spec), the scheduler scalars but `reprogram_timer`; the rest is given. -/ +theorem SchedSRN.rebuildSC {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s s' : S} {σ σ' : Fl4SpecA64.State Unit} + (H : SchedSRN st Z m κ s σ) {Q' : U64 → U64 → List U64} + (hks : L.ksb s' = L.ksb s) (hcpu : L.cpu s' = L.cpu s) + (hnode : ∀ c ∈ Z.Cs, ∀ o : U64, o.val < 120 → o.val % 8 = 0 → o.val ≠ 16 → o.val ≠ 56 → + L.mem s' (L.nodeAt s c o) = L.mem s (L.nodeAt s c o)) + (hglob : ∀ K : U64, (K = 8#u64 ∨ K = 16#u64) → L.mem s' (wa (L.ksb s) K) = L.mem s (wa (L.ksb s) K)) + (htf : ∀ t ∈ Z.T, ∀ K : U64, (K = 3520#u64 ∨ K = 3448#u64 ∨ K = 3432#u64 ∨ K = 3464#u64) → + L.mem s' (wa t K) = L.mem s (wa t K)) + (hscS : ScStateRelA st Z.C m s' σ') (hscP : ScPlacedA Z.th (L.ksb s) Z.C m σ') + (htcb : ∀ t ∈ Z.T, ∀ x, σ.kheap (specPtr t) = some (.tcb x) → ∃ y, σ'.kheap (specPtr t) = some (.tcb y) ∧ + y.tcbSchedContext = x.tcbSchedContext ∧ y.tcbDomain = x.tcbDomain ∧ y.tcbPriority = x.tcbPriority) + (hfix : SFixA σ { σ' with reprogramTimer := σ.reprogramTimer }) + (hrepr : σ'.reprogramTimer = decide (L.mem s' (L.nodeAt s (L.cpu s) layout.node.REPROGRAM) ≠ 0#u64)) + (hact : σ'.schedulerAction = actOfA (L.mem s' (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION))) + (hactOn : ∀ u, σ'.schedulerAction = .switchThread (specPtr u) → u ∈ Z.T ∧ L.mem s (affA u) = L.cpu s) + (hinv : ScInvA m σ') + (hcore : SchedCoreA st Z.T Z.Cs s' Q' Z.R) + (hrq : ∀ p : U64, p.val < 256 → σ'.readyQueues 0 (BitVec.ofNat 8 p.val) = (Q' (L.cpu s) p).map specPtr) + (hrqd : ∀ d p, d ≠ 0 → σ'.readyQueues d p = []) + (hrel : σ'.releaseQueue = σ.releaseQueue) (hrun : RunQA σ') + (hscCore : ∀ c ∈ Z.C, L.mem s' (scCoreA c) = L.mem s (scCoreA c)) : + SchedSRN st { Z with Q := Q' } m κ s' σ' := + H.rebuildG (Q' := Q') (R' := Z.R) hks hcpu (fun c hc o h1 h2 h3 h4 => hnode c hc o (by omega) h2 h3 h4) + hglob + (fun t ht K hK => htf t ht K (by rcases hK with h | h | h <;> simp [h])) hscS hscP + (fun t ht x hx => let ⟨y, h1, _, h2, h3⟩ := htcb t ht x hx; ⟨y, h1, h2, h3⟩) + (fun t ht => by + obtain ⟨x, h1, h3⟩ := H.sch.tcb t ht + obtain ⟨y, hy, e1, -, -⟩ := htcb t ht x h1 + have e : L.mem s' (scA t) = L.mem s (scA t) := htf t ht 3464#u64 (by simp) + exact ⟨y, hy, by rw [e, e1]; exact h3⟩) + (fun t ht h => by + have e : L.mem s' (scA t) = L.mem s (scA t) := htf t ht 3464#u64 (by simp) + rw [e] at h ⊢; exact H.rest.scMem t ht h) + hfix hrepr hact hactOn hinv hcore hrq hrqd (by rw [hrel]; exact H.rest.rel) hrun hscCore + +theorem SchedSRA.rebuildS {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s s' : S} {σ σ' : Fl4SpecA64.State Unit} + (H : SchedSRA st Z m κ s σ) {Q' : U64 → U64 → List U64} + (hks : L.ksb s' = L.ksb s) (hcpu : L.cpu s' = L.cpu s) + (hnode : ∀ c ∈ Z.Cs, ∀ o : U64, o.val < 120 → o.val % 8 = 0 → o.val ≠ 16 → o.val ≠ 56 → + L.mem s' (L.nodeAt s c o) = L.mem s (L.nodeAt s c o)) + (hglob : ∀ K : U64, (K = 8#u64 ∨ K = 16#u64) → L.mem s' (wa (L.ksb s) K) = L.mem s (wa (L.ksb s) K)) + (htf : ∀ t ∈ Z.T, ∀ K : U64, (K = 3520#u64 ∨ K = 3448#u64 ∨ K = 3432#u64 ∨ K = 3464#u64) → + L.mem s' (wa t K) = L.mem s (wa t K)) + (hscS : ScStateRelA st Z.C m s' σ') (hscP : ScPlacedA Z.th (L.ksb s) Z.C m σ') + (htcb : ∀ t ∈ Z.T, ∀ x, σ.kheap (specPtr t) = some (.tcb x) → ∃ y, σ'.kheap (specPtr t) = some (.tcb y) ∧ + y.tcbSchedContext = x.tcbSchedContext ∧ y.tcbDomain = x.tcbDomain ∧ y.tcbPriority = x.tcbPriority) + (hfix : SFixA σ { σ' with reprogramTimer := σ.reprogramTimer }) + (hrepr : σ'.reprogramTimer = decide (L.mem s' (L.nodeAt s (L.cpu s) layout.node.REPROGRAM) ≠ 0#u64)) + (hact : σ'.schedulerAction = actOfA (L.mem s' (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION))) + (hactOn : ∀ u, σ'.schedulerAction = .switchThread (specPtr u) → u ∈ Z.T ∧ L.mem s (affA u) = L.cpu s) + (hrd : RdA σ') (hinv : ScInvA m σ') + (hcore : SchedCoreA st Z.T Z.Cs s' Q' Z.R) + (hrq : ∀ p : U64, p.val < 256 → σ'.readyQueues 0 (BitVec.ofNat 8 p.val) = (Q' (L.cpu s) p).map specPtr) + (hrqd : ∀ d p, d ≠ 0 → σ'.readyQueues d p = []) + (hrel : σ'.releaseQueue = σ.releaseQueue) (hrun : RunQA σ') + (hscCore : ∀ c ∈ Z.C, L.mem s' (scCoreA c) = L.mem s (scCoreA c)) : + SchedSRA st { Z with Q := Q' } m κ s' σ' := + ⟨H.toSchedSRN.rebuildSC hks hcpu hnode hglob htf hscS hscP htcb hfix hrepr hact hactOn + hinv hcore hrq hrqd hrel hrun hscCore, hrd⟩ + +/-- **`SchedSRA` after a step that keeps what it reads outside the queues** (module docstring): the node words +below `IPI_PENDING` (120) of every core but the action word, the domain words, the TCBs' affinity / priority / +domain / SC words, the SC objects (FL4) and their spec objects, the TCBs' SC / domain / priority with FL4's state +code (spec), the scheduler scalars; the action, the queues and their runnable part are given (gen 5: + `hinv`; a +wrapper of `rebuildS`). -/ +theorem SchedSRN.rebuildQueues {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s s' : S} {σ σ' : Fl4SpecA64.State Unit} + (H : SchedSRN st Z m κ s σ) {Q' : U64 → U64 → List U64} + (hks : L.ksb s' = L.ksb s) (hcpu : L.cpu s' = L.cpu s) + (hnode : ∀ c ∈ Z.Cs, ∀ o : U64, o.val < 120 → o.val % 8 = 0 → o.val ≠ 16 → + L.mem s' (L.nodeAt s c o) = L.mem s (L.nodeAt s c o)) + (hglob : ∀ K : U64, (K = 8#u64 ∨ K = 16#u64) → L.mem s' (wa (L.ksb s) K) = L.mem s (wa (L.ksb s) K)) + (htf : ∀ t ∈ Z.T, ∀ K : U64, (K = 3520#u64 ∨ K = 3448#u64 ∨ K = 3432#u64 ∨ K = 3464#u64) → + L.mem s' (wa t K) = L.mem s (wa t K)) + (hsc : ∀ a, Aligned a → ScFootA Z.C m σ a → L.mem s' a = L.mem s a) + (hkc : ∀ c ∈ Z.C, σ'.kheap (scNameA m c) = σ.kheap (scNameA m c)) + (htcb : ∀ t ∈ Z.T, ∀ x, σ.kheap (specPtr t) = some (.tcb x) → ∃ y, σ'.kheap (specPtr t) = some (.tcb y) ∧ + y.tcbSchedContext = x.tcbSchedContext ∧ y.tcbDomain = x.tcbDomain ∧ y.tcbPriority = x.tcbPriority) + (hfix : SFixA σ σ') + (hact : σ'.schedulerAction = actOfA (L.mem s' (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION))) + (hactOn : ∀ u, σ'.schedulerAction = .switchThread (specPtr u) → u ∈ Z.T ∧ L.mem s (affA u) = L.cpu s) + (hinv : ScInvA m σ') + (hcore : SchedCoreA st Z.T Z.Cs s' Q' Z.R) + (hrq : ∀ p : U64, p.val < 256 → σ'.readyQueues 0 (BitVec.ofNat 8 p.val) = (Q' (L.cpu s) p).map specPtr) + (hrqd : ∀ d p, d ≠ 0 → σ'.readyQueues d p = []) + (hrel : σ'.releaseQueue = σ.releaseQueue) (hrun : RunQA σ') : + SchedSRN st { Z with Q := Q' } m κ s' σ' := + H.rebuildSC hks hcpu (fun c hc o h1 h2 h3 _ => hnode c hc o h1 h2 h3) hglob htf (H.scr.frame hsc hkc) + (ScPlacedA.of_kheap H.scp hkc) htcb + ⟨hfix.curThread, hfix.curSc, hfix.idleThread, hfix.curTime, hfix.consumedTime, rfl, hfix.curDomain, + hfix.domainTime⟩ + (by + rw [hfix.reprogramTimer, hnode _ H.sch.cpuCs layout.node.REPROGRAM (by simp [layout.node.REPROGRAM]) + (by simp [layout.node.REPROGRAM]) (by simp [layout.node.REPROGRAM])] + exact H.rest.repr) + hact hactOn hinv hcore hrq hrqd hrel hrun + (fun c hc => hsc _ (H.scCoreFoot hc).1 (H.scCoreFoot hc).2) + +theorem SchedSRA.rebuild {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s s' : S} {σ σ' : Fl4SpecA64.State Unit} + (H : SchedSRA st Z m κ s σ) {Q' : U64 → U64 → List U64} + (hks : L.ksb s' = L.ksb s) (hcpu : L.cpu s' = L.cpu s) + (hnode : ∀ c ∈ Z.Cs, ∀ o : U64, o.val < 120 → o.val % 8 = 0 → o.val ≠ 16 → + L.mem s' (L.nodeAt s c o) = L.mem s (L.nodeAt s c o)) + (hglob : ∀ K : U64, (K = 8#u64 ∨ K = 16#u64) → L.mem s' (wa (L.ksb s) K) = L.mem s (wa (L.ksb s) K)) + (htf : ∀ t ∈ Z.T, ∀ K : U64, (K = 3520#u64 ∨ K = 3448#u64 ∨ K = 3432#u64 ∨ K = 3464#u64) → + L.mem s' (wa t K) = L.mem s (wa t K)) + (hsc : ∀ a, Aligned a → ScFootA Z.C m σ a → L.mem s' a = L.mem s a) + (hkc : ∀ c ∈ Z.C, σ'.kheap (scNameA m c) = σ.kheap (scNameA m c)) + (htcb : ∀ t ∈ Z.T, ∀ x, σ.kheap (specPtr t) = some (.tcb x) → ∃ y, σ'.kheap (specPtr t) = some (.tcb y) ∧ + y.tcbSchedContext = x.tcbSchedContext ∧ y.tcbDomain = x.tcbDomain ∧ y.tcbPriority = x.tcbPriority) + (hfix : SFixA σ σ') + (hact : σ'.schedulerAction = actOfA (L.mem s' (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION))) + (hactOn : ∀ u, σ'.schedulerAction = .switchThread (specPtr u) → u ∈ Z.T ∧ L.mem s (affA u) = L.cpu s) + (hrd : RdA σ') (hinv : ScInvA m σ') + (hcore : SchedCoreA st Z.T Z.Cs s' Q' Z.R) + (hrq : ∀ p : U64, p.val < 256 → σ'.readyQueues 0 (BitVec.ofNat 8 p.val) = (Q' (L.cpu s) p).map specPtr) + (hrqd : ∀ d p, d ≠ 0 → σ'.readyQueues d p = []) + (hrel : σ'.releaseQueue = σ.releaseQueue) (hrun : RunQA σ') : + SchedSRA st { Z with Q := Q' } m κ s' σ' := + ⟨H.toSchedSRN.rebuildQueues hks hcpu hnode hglob htf hsc hkc htcb hfix hact hactOn + hinv hcore hrq hrqd hrel hrun, hrd⟩ + +/-! ## `is_schedulable` is `schedulable` -/ + +theorem runnable_iffA {w : U64} {ts : Fl4SpecA64.ThreadState} (h : w.val = tsCodeA ts) : + (w = sched.TS_RUNNING ∨ w = sched.TS_RESTART ∨ w = sched.TS_RUNNING_VM) ↔ Fl4SpecA64.runnable ts = true := by + obtain ⟨e1, e2, -⟩ := ts_vals + have e8 : sched.TS_RUNNING_VM = 8#u64 := by unfold sched.TS_RUNNING_VM; rfl + rw [e1, e2, e8] + have v : ∀ k : U64, w = k ↔ w.val = k.val := fun k => ⟨fun h => h ▸ rfl, UScalar.eq_of_val_eq⟩ + rw [v, v, v, h] + cases ts <;> first + | (simp [tsCodeA, Fl4SpecA64.runnable]; done) + | (rename_i e; cases e; simp [tsCodeA, Fl4SpecA64.runnable, Fl4SpecA64.ExtThreadState.runnable]) + +theorem maxA_eqSc (c : U64) : maxA c = wa c 72#u64 := by unfold maxA layout.sc.REFILL_MAX; rfl + +/-- The release bit (x86 `rel_bit_iff`). -/ +theorem rel_bit_iffSc (x : U64) : (x &&& sched.Q_RELEASE = 0#u64) ↔ x.val &&& 2 = 0 := by + have e : sched.Q_RELEASE = 2#u64 := by unfold sched.Q_RELEASE; rfl + rw [e]; constructor + · intro h; have := congrArg UScalar.val h; simpa using this + · intro h; apply UScalar.eq_of_val_eq; simpa using h + +/-- FL4's `is_schedulable` as a decision of its words. -/ +theorem is_schedulable_eqA (s : S) (t : U64) : + sched.is_schedulable st s t = ok (decide ((L.mem s (stA t) = sched.TS_RUNNING ∨ L.mem s (stA t) = sched.TS_RESTART ∨ + L.mem s (stA t) = sched.TS_RUNNING_VM) ∧ L.mem s (scA t) ≠ 0#u64 ∧ + L.rmax s (L.mem s (scA t)) > 0#u64 ∧ (L.mem s (inqA t) &&& sched.Q_RELEASE) = 0#u64)) := by + unfold sched.is_schedulable + rw [eq_of_spec_fd (sched.is_runnable.spec (L := L) s t)] + simp only [bind_ok] + by_cases h1 : Runnable (L.mem s (stA t)) + · have h1' : L.mem s (stA t) = sched.TS_RUNNING ∨ L.mem s (stA t) = sched.TS_RESTART ∨ + L.mem s (stA t) = sched.TS_RUNNING_VM := h1 + rw [decide_eq_true h1'] + simp only [↓reduceIte] + rw [eq_of_spec_fd (obj.Tcb.sc.spec (L := L) s t)] + simp only [bind_ok] + rw [eq_of_spec_fd (obj.Sc.is_null.spec _)] + simp only [bind_ok] + by_cases h2 : L.mem s (scA t) = 0#u64 + · simp [h1', h2] + · simp only [decide_eq_false h2, Bool.false_eq_true, ↓reduceIte] + unfold sporadic.sc_active + rw [eq_of_spec_fd (obj.Sc.refill_max.spec (L := L) s _)] + simp only [bind_ok] + by_cases h3 : L.rmax s (L.mem s (scA t)) > 0#u64 + · simp only [h3, decide_true, ↓reduceIte] + rw [eq_of_spec_fd (sched.in_release_queue.spec (L := L) s t)] + simp [h1', h2, h3] + exact (rel_bit_iffSc _).symm + · simp [h1', h2, h3] + · have h1' : ¬ (L.mem s (stA t) = sched.TS_RUNNING ∨ L.mem s (stA t) = sched.TS_RESTART ∨ + L.mem s (stA t) = sched.TS_RUNNING_VM) := h1 + rw [decide_eq_false h1'] + simp [h1'] + +open Fl4Spec Fl4SpecA64 in +/-- (gen 4) `schedulable`'s SC part of a thread of `Z.T` is FL4's (no state word read). -/ +theorem SchedSRN.scPart {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} (H : SchedSRN st Z m κ s σ) {t : U64} + (ht : t ∈ Z.T) : optPred (fun sc => scActive sc = true) + (optMap (optMap (tcbsOf σ) Tcb.tcbSchedContext) (scsOf σ)) (specPtr t) ↔ + (L.mem s (scA t) ≠ 0#u64 ∧ L.rmax s (L.mem s (scA t)) > 0#u64) := by + obtain ⟨x, hk, h3⟩ := H.sch.tcb t ht + by_cases h0 : L.mem s (scA t) = 0#u64 + · have h3' : x.tcbSchedContext = none := by rw [h3, h0]; simp [optScA] + simp [optPred, optMap, tcbsOfKh, tcbOf, hk, h3', h0] + · have h3' : x.tcbSchedContext = some (scRenA m (L.mem s (scA t))) := by rw [h3]; simp [optScA, h0] + obtain ⟨y, n, hky, e⟩ := H.rest.refillMax _ (H.rest.scMem t ht h0) + have hr : L.rmax s (L.mem s (scA t)) > 0#u64 ↔ 0 < y.scRefillMax := by + rw [e, GT.gt, UScalar.lt_equiv, LawfulStore.rmax, maxA_eqSc]; simp + rw [hr] + simp [optPred, optMap, tcbsOfKh, tcbOf, hk, h3', scsOfKh, hky, scOf, scActive, activeSc, h0] + +open Fl4Spec Fl4SpecA64 in +/-- **`is_schedulable` computes `schedulable`** (x86 `schedulable_eq`), for a thread of `Z.T` whose release bit is +its membership of the release queue (gen 4: the state words from `StEqA`). -/ +theorem schedulable_eqA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} (H : SchedSRN st Z m κ s σ) {t : U64} + (ht : t ∈ Z.T) (hrel : (L.mem s (inqA t) &&& sched.Q_RELEASE) = 0#u64 ↔ specPtr t ∉ σ.releaseQueue) + (hst : StEqA Z.T (L.mem s) σ) : + decide ((L.mem s (stA t) = sched.TS_RUNNING ∨ L.mem s (stA t) = sched.TS_RESTART ∨ + L.mem s (stA t) = sched.TS_RUNNING_VM) ∧ L.mem s (scA t) ≠ 0#u64 ∧ L.rmax s (L.mem s (scA t)) > 0#u64 ∧ + (L.mem s (inqA t) &&& sched.Q_RELEASE) = 0#u64) = schedulable (specPtr t) σ := by + obtain ⟨x, hk, -⟩ := H.sch.tcb t ht + have h1 := hst t ht x hk + unfold schedulable + apply decide_eq_decide.2 + have p1 : optPred (fun ts => runnable ts = true) (optMapSome (tcbsOf σ) Tcb.tcbState) (specPtr t) ↔ + runnable x.tcbState = true := by + simp [optPred, optMapSome, optMap, tcbsOfKh, tcbOf, hk] + rw [p1, H.scPart ht, ← hrel, ← runnable_iffA h1] + exact ⟨fun ⟨a, b, c, d⟩ => ⟨a, ⟨b, c⟩, d⟩, fun ⟨a, ⟨b, c⟩, d⟩ => ⟨a, b, c, d⟩⟩ + +/-! ## Writing the action word -/ + +theorem nodeAt_valSc {s : S} {c o : U64} (hf : (L.ksb s).val + ksSizeA ≤ 2 ^ 64) (hc : c.val < 128) + (ho : o.val < 4288) : (L.nodeAt s c o).val = (L.ksb s).val + 223488 + c.val * 4288 + o.val := by + unfold ksSizeA at hf + have hk := (L.ksb s).hBounds + simp [LawfulStore.nodeAt, core.num.U64.wrapping_add_val_eq, core.num.U64.wrapping_mul_val_eq, + U64.size_def, U64.numBits, UScalar.size] at hk ⊢ + omega + +/-- The kernel-state block is word aligned (its first IRQ slot is a placed CNode). -/ +theorem SchedSRN.ksb_al {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} {σ : Fl4SpecA64.State Unit} + (H : SchedSRN st Z m κ s σ) : (L.ksb s).val % 8 = 0 := by + have h0 : (0#u64 : U64).val < 5376 := by simp + have h1 := H.place.align _ _ (H.place.irq 0#u64 h0) + rw [irqSlotA_val H.ksFit h0, show ObjKindA.size (.cnode 0) = 32 from rfl, + show (0#u64 : U64).val = 0 from rfl] at h1 + omega + +/-- A word inside a TCB of `Z.T` is off the kernel-state block. -/ +theorem SchedSRN.tcb_ks {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} {σ : Fl4SpecA64.State Unit} + (H : SchedSRN st Z m κ s σ) {t : U64} (ht : t ∈ Z.T) : + t.val + 4096 ≤ (L.ksb s).val ∨ (L.ksb s).val + ksSizeA ≤ t.val := by + have hth := (H.thT t).2 ht + have hout := H.place.ks _ _ hth (by + rintro ⟨i, hi, e⟩ + have h2 := H.place.irq i hi + rw [← e, hth] at h2 + injection h2 with h2 + cases h2) + simpa [ObjKindA.size] using hout + +/-- The TCB facts `rebuild` needs, when the spec heap is kept (gen 4: no state word). -/ +theorem SchedSRA.tcb_keep {Z : XSetsA} {σ σ' : Fl4SpecA64.State Unit} (hk : σ'.kheap = σ.kheap) : + ∀ t ∈ Z.T, ∀ x, σ.kheap (specPtr t) = some (.tcb x) → ∃ y, σ'.kheap (specPtr t) = some (.tcb y) ∧ + y.tcbSchedContext = x.tcbSchedContext ∧ y.tcbDomain = x.tcbDomain ∧ y.tcbPriority = x.tcbPriority := + fun _ _ x hx => ⟨x, by rw [hk]; exact hx, rfl, rfl, rfl⟩ + +/-- **Writing the current core's action word keeps `SchedSRA`** (x86 `SchedRel.set_action`), the spec's action +being the word's. -/ +theorem SchedSRA.set_action {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} {σ : Fl4SpecA64.State Unit} + (H : SchedSRA st Z m κ s σ) (w : U64) {a : Fl4SpecA64.SchedulerAction} (ha : a = actOfA w) + (hon : ∀ u, a = .switchThread (specPtr u) → u ∈ Z.T ∧ L.mem s (affA u) = L.cpu s) : + SchedSRA st Z m κ (L.wr s (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) w) + { σ with schedulerAction := a } := by + obtain ⟨n, -, hn, hcs⟩ := H.csN + have hcl : ∀ c ∈ Z.Cs, c.val < 128 := fun c hc => by have := (hcs c).1 hc; omega + have hf := H.ksFit + have hkb := H.ksb_al + have hcpu0 := hcl _ H.sch.cpuCs + have hsa : layout.node.SCHED_ACTION = 16#u64 := by unfold layout.node.SCHED_ACTION; rfl + have hAv : (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION).val = + (L.ksb s).val + 223488 + (L.cpu s).val * 4288 + 16 := by + rw [nodeAt_valSc (o := layout.node.SCHED_ACTION) hf hcpu0 (by rw [hsa]; simp), hsa]; simp + have hA : Aligned (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) := by unfold Aligned; rw [hAv]; omega + have keep : ∀ b, Aligned b → b.val ≠ (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION).val → + L.mem (L.wr s (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) w) b = L.mem s b := + fun b hb hne => L.mem_wr_ne hA hb (fun e => hne e.symm) + have nodeK : ∀ c ∈ Z.Cs, ∀ o : U64, o.val < 4288 → o.val % 8 = 0 → o.val ≠ 16 → + L.mem (L.wr s (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) w) (L.nodeAt s c o) = L.mem s (L.nodeAt s c o) := by + intro c hc o ho h8 h16 + have hv := nodeAt_valSc (s := s) hf (hcl c hc) ho + refine keep _ (by unfold Aligned; rw [hv]; omega) ?_ + rw [hv, hAv]; omega + have inT : ∀ t ∈ Z.T, ∀ b : U64, Aligned b → t.val ≤ b.val → b.val < t.val + 4096 → + L.mem (L.wr s (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) w) b = L.mem s b := by + intro t ht b hb h1 h2 + have := H.tcb_ks ht + refine keep _ hb ?_ + rw [hAv]; unfold ksSizeA at this; omega + have hT := H.core.tcbs + have tcbK : ∀ t ∈ Z.T, ∀ K : U64, K.val + 8 ≤ 4096 → K.val % 8 = 0 → + L.mem (L.wr s (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) w) (wa t K) = L.mem s (wa t K) := by + intro t ht K hK h8 + have hfit := (hT t ht).fits + have hal := (hT t ht).align + have hv : (wa t K).val = t.val + K.val := by rw [wa_valm]; omega + exact inT t ht _ (by unfold Aligned; rw [hv]; omega) (by omega) (by omega) + have ksbK : ∀ K : U64, K.val < 223488 → K.val % 8 = 0 → + L.mem (L.wr s (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) w) (wa (L.ksb s) K) = L.mem s (wa (L.ksb s) K) := by + intro K hK h8 + have hv : (wa (L.ksb s) K).val = (L.ksb s).val + K.val := by rw [wa_valm]; unfold ksSizeA at hf; omega + refine keep _ (by unfold Aligned; rw [hv]; omega) ?_ + rw [hv, hAv]; omega + have hcore : SchedCoreA st Z.T Z.Cs (L.wr s (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) w) Z.Q Z.R := + H.core.agree (L.ksb_wr _ _ _) + (fun y hy => ⟨inT y hy _ (nextA_aligned (hT y hy) _) (nextA_inside (hT y hy) _).1 + (by have := (nextA_inside (hT y hy) .Sched).2; omega), + inT y hy _ (prevA_aligned (hT y hy) _) (prevA_inside (hT y hy) _).1 + (by have := (prevA_inside (hT y hy) .Sched).2; omega)⟩) + (fun c hc o h1 h2 h3 => nodeK c hc o h1 h2 (by omega)) + (fun y hy => tcbK y hy 3592#u64 (by simp) (by simp)) + (fun y hy => tcbK y hy 3520#u64 (by simp) (by simp)) + (fun y hy => tcbK y hy 3448#u64 (by simp) (by simp)) + have hsc : ∀ b, Aligned b → ScFootA Z.C m σ b → + L.mem (L.wr s (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) w) b = L.mem s b := by + intro b hb hfb + refine keep b hb ?_ + have h1 := H.scp.ks_out hfb + unfold ksSizeA at h1; rw [hAv]; omega + exact H.rebuild (L.ksb_wr _ _ _) (L.cpu_wr _ _ _) + (fun c hc o h1 h2 h3 => nodeK c hc o (by omega) h2 h3) + (fun K hK => by rcases hK with rfl | rfl <;> exact ksbK _ (by simp) (by simp)) + (fun t ht K hK => by rcases hK with rfl | rfl | rfl | rfl <;> exact tcbK t ht _ (by simp) (by simp)) + hsc (fun _ _ => rfl) + (SchedSRA.tcb_keep rfl) ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ + (by show a = _; rw [L.mem_wr_same hA]; exact ha) hon (RdA.of_eq H.rd rfl rfl rfl) (ScInvA.of_kheap H.inv rfl rfl) hcore + (fun p hp => H.rest.rq p hp) H.rest.rqDom rfl ⟨H.run.1, H.run.2⟩ + +/-! ## `schedule_tcb` -/ + +/-- **`schedule_tcb` refines `set_thread_state_act`** (x86 `schedule_tcb_refines`; `SchedOpsA.scheduleTcb` / +`SchedOpsEA.scheduleTcb` with `SR := SchedSRA`): FL4 writes at most the current core's action word. -/ +theorem schedule_tcb_refinesA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} {σ : Fl4SpecA64.State Unit} + (H : SchedSRA st Z m κ s σ) (t : U64) (hst : StEqA Z.T (L.mem s) σ) : + sched.schedule_tcb st s t ⦃ s' => WritesIn st s s' (· = L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) ∧ + SchedSRA st Z m κ s' (stsActA σ (specPtr t)) ⦄ := by + have hcur : σ.curThread = specPtr (L.curW s) := H.sch.curThread + have hres0 : sched.ACTION_RESUME_CURRENT = 0#u64 := by unfold sched.ACTION_RESUME_CURRENT; rfl + unfold sched.schedule_tcb + refine WP.spec_bind (sched.cur_thread.spec s) ?_ + rintro t1 rfl + refine WP.spec_bind (obj.Tcb.eq.spec _ _) ?_ + rintro b rfl + by_cases e1 : t = L.curW s + · subst e1 + simp only [show decide (L.curW s = L.mem s (L.nodeAt s (L.cpu s) layout.node.CUR_THREAD)) = true from + decide_eq_true rfl, ↓reduceIte] + refine WP.spec_bind (sched.action.spec s) ?_ + rintro i rfl + by_cases e2 : L.actionW s = sched.ACTION_RESUME_CURRENT + · have hres : σ.schedulerAction = .resumeCurThread := by + rw [H.sch.act, e2, hres0]; simp [actOfA] + simp only [e2, ↓reduceIte] + rw [is_schedulable_eqA, bind_ok, schedulable_eqA H.toSchedSRN H.curT H.relCur hst] + by_cases e3 : Fl4SpecA64.schedulable (specPtr (L.curW s)) σ = true + · simp only [e3, ↓reduceIte, WP.spec_ok] + refine ⟨WritesIn.refl _ _, ?_⟩ + rw [stsActA_eq_self (by intro h; exact h.2.2 e3)]; exact H + · simp only [e3, Bool.false_eq_true, ↓reduceIte] + refine WP.spec_mono (sched.set_action.spec s _) ?_ + rintro s' rfl + have e4 : stsActA σ (specPtr (L.curW s)) = { σ with schedulerAction := .chooseNewThread } := by + unfold stsActA + rw [if_pos ⟨hcur.symm, hres, e3⟩] + rw [e4] + refine ⟨writesIn_wr (s := s) (v := sched.ACTION_CHOOSE_NEW) ?_, H.set_action _ ?_ fun u h => by cases h⟩ + · obtain ⟨n, -, hn, hcs⟩ := H.csN + have hc := (hcs _).1 H.sch.cpuCs + unfold Aligned + have hsa : layout.node.SCHED_ACTION = 16#u64 := by unfold layout.node.SCHED_ACTION; rfl + rw [nodeAt_valSc (o := layout.node.SCHED_ACTION) H.ksFit (by omega) (by rw [hsa]; simp)] + have hkb : (L.ksb s).val % 8 = 0 := by + have h0 : (0#u64 : U64).val < 5376 := by simp + have h1 := H.place.align _ _ (H.place.irq 0#u64 h0) + rw [irqSlotA_val H.ksFit h0, show ObjKindA.size (.cnode 0) = 32 from rfl, + show (0#u64 : U64).val = 0 from rfl] at h1 + omega + simp [layout.node.SCHED_ACTION]; omega + · rw [show sched.ACTION_CHOOSE_NEW = 1#u64 by unfold sched.ACTION_CHOOSE_NEW; rfl]; simp [actOfA] + · simp only [e2, ↓reduceIte, WP.spec_ok] + refine ⟨WritesIn.refl _ _, ?_⟩ + have hnr : σ.schedulerAction ≠ .resumeCurThread := by + intro h; rw [H.sch.act] at h + apply e2; rw [hres0] + unfold actOfA at h + by_cases w0 : L.actionW s = 0#u64 + · exact w0 + · rw [if_neg w0] at h; split_ifs at h + rw [stsActA_eq_self (by intro h; exact hnr h.2.1)]; exact H + · rw [show decide (t = L.mem s (L.nodeAt s (L.cpu s) layout.node.CUR_THREAD)) = false from decide_eq_false e1] + simp only [Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + refine ⟨WritesIn.refl _ _, ?_⟩ + rw [stsActA_eq_self (by intro h; exact e1 (specPtr_inj (h.1.trans hcur)))]; exact H + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScSchedChargeA.lean b/proofs/Fl4ProofsA64/P3/ScSchedChargeA.lean new file mode 100644 index 000000000..48810f4b6 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScSchedChargeA.lean @@ -0,0 +1,140 @@ +import Fl4ProofsA64.P3.ScSchedWordsA +import Fl4ProofsA64.P3.ScCfgPrepA + +/-! Preserve the independent thread-state, MCP and yield-link relations +through scheduling-context accounting. The positive path uses the +frozen outgoing-SC ownership and offset-readiness facts. -/ + +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3.Sched + +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +/-- Replacement of one SC preserves the yield pair when its yielder is +retained. All non-SC objects are unchanged. -/ +theorem yieldFrameSchedContextA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s s' : S} {σ τ : State Unit} (H : YieldQueueRelA (st := st) Z m κ s σ) + {c : U64} (hc : c ∈ Z.C) {x y : SchedContext} {n : Nat} + (hk : σ.kheap (scNameA m c) = some (.schedContext x n)) + (heap : τ.kheap = (updKSc σ (scNameA m c) (.schedContext y n)).kheap) + (fromEq : y.scYieldFrom = x.scYieldFrom) + (w : WritesIn st s s' (MWrA (st := st) Z m σ s)) + (rel : SchedSRA st Z m κ s' τ) : YieldQueueRelA (st := st) Z m κ s' τ ∧ MFrA (st := st) Z m s s' σ τ := by + let frame := cfgPrepFrameA hc hk heap w + have keep {t k : U64} (ht : t ∈ Z.T) (h : k = 3328#u64 ∨ k = 3440#u64 ∨ k = 3472#u64) := + yieldWordKeepA H.sched.toSchedSRN H.sched.curT (w.mono fun _ hw => Or.inl hw) ht h + have tcb : ∀ t z, σ.kheap t = some (.tcb z) → τ.kheap t = some (.tcb z) := by + intro t z ht + have ne : t ≠ scNameA m c := fun e => by rw [e, hk] at ht; cases ht + rw [heap, updKSc_kheap, Function.update_of_ne ne] + exact ht + have tcbOld : ∀ t z, τ.kheap t = some (.tcb z) → σ.kheap t = some (.tcb z) := by + intro t z ht + have ne : t ≠ scNameA m c := fun e => by + rw [heap, e, updKSc_same] at ht + cases ht + rwa [heap, updKSc_kheap, Function.update_of_ne ne] at ht + have states : StEqA Z.T (L.mem s') τ := H.states.keep + (fun t ht => by rw [stA_eq]; exact keep ht (Or.inl rfl)) + (fun t z hz => ⟨z, tcbOld t z hz, rfl⟩) + obtain ⟨mcp, links⟩ := yieldFrameA H.mcp H.links + (fun t ht => ⟨keep ht (Or.inr (Or.inl rfl)), + yieldFlagsKeepA H.sched.toSchedSRN H.sched.curT (w.mono fun _ hw => Or.inl hw) ht⟩) + (fun t ht => keep ht (Or.inr (Or.inr rfl))) (fun t _ z hz => ⟨z, tcb (specPtr t) z hz, rfl, rfl, rfl⟩) (fun k hk' => by + by_cases e : k = c + · subst k + exact ⟨x, n, y, hk, by rw [heap]; exact updKSc_same _ _ _, fromEq⟩ + · obtain ⟨b, i, he, _⟩ := H.sched.scr.sc k hk' + have far := fun h => e (H.sched.scp.inj _ hk' _ hc h) + exact ⟨b, i, b, he, by rw [heap, updKSc_kheap, Function.update_of_ne far]; exact he, rfl⟩) + exact ⟨⟨rel, states, mcp, links⟩, frame⟩ + +/-- Charge the outgoing SC before installing the incoming cursor. The +SC stays with its owner throughout this step, including an unbound SC. -/ +theorem commit_time_activeA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {σ : State Unit} (H : YieldQueueRelA (st := st) Z m κ s σ) {a : A} + {x : SchedContext} {n : Nat} (hk : σ.kheap σ.curSc = some (.schedContext x n)) + (active : scActive x = true) (ordinary : NotIdleScA m σ.curSc) + {owner : ObjRef} (own : x.scTcb = none ∨ x.scTcb = some owner) + (outside : owner ∉ σ.releaseQueue) (O : OffOKA σ) (R : OffReadyA σ) + (agree : fl4MinBudget = MIN_BUDGET) (ticks : ScCfgTimeAgreeA ar a) : + sched.commit_time st ar s a ⦃ s' => ∃ τ, + fl4CommitTime (Z := Unit) σ = ({((), τ)}, False) ∧ + YieldQueueRelA (st := st) Z m κ s' τ ∧ MFrA (st := st) Z m s s' σ τ ⦄ := by + let c := L.cscW s + have cur := H.sched.toSchedSRN.currentScA + have hk' : σ.kheap (scNameA m c) = some (.schedContext x n) := cur ▸ hk + have ordinary' : NotIdleScA m (scNameA m c) := cur ▸ ordinary + refine WP.spec_mono (cfg_commit_timeA (H.sched.toB owner) a H.sched.cscC rfl cur hk' + active ordinary' own outside O R agree ticks.maxRelease ticks.minBudget) ?_ + rintro s' ⟨y, abs, rel, fields, ready, w⟩ + let τ := {updKSc σ (scNameA m c) (.schedContext y n) with consumedTime := 0} + have hk'' : τ.kheap (scNameA m c) = some (.schedContext y n) := updKSc_same _ _ _ + have tcbOld {t : ObjRef} {z : Tcb} (hz : τ.kheap t = some (.tcb z)) : σ.kheap t = some (.tcb z) := by + have e : t ≠ scNameA m c := fun e => by rw [e, hk''] at hz; cases hz + change Function.update σ.kheap (scNameA m c) _ t = _ at hz + rwa [Function.update_of_ne e] at hz + have rd : RdA τ := by + intro t z hz ic + by_cases he : t = owner + · intro z' sc q n' hz' binding hsc a' hnr + by_cases e : sc = scNameA m c + · subst sc + rw [hk''] at hsc; cases hsc + exact ⟨ready, rel.inv.suff hk'' a' ordinary'⟩ + · change Function.update σ.kheap (scNameA m c) _ sc = _ at hsc + rw [Function.update_of_ne e] at hsc + exact H.sched.rd t z (tcbOld hz) ic z' sc q n' (tcbOld hz') binding hsc a' hnr + · exact rel.rdEx t z he hz ic + have sw := H.sched.node_sks (o := layout.node.CONSUMED) H.sched.sch.cpuCs (by simp [layout.node.CONSUMED]) + have wf : WritesIn st s s' (MWrA (st := st) Z m σ s) := w.mono fun b h => h.elim + (fun hb => Or.inl ⟨c, H.sched.cscC, x, n, hk', hb⟩) (fun hb => Or.inr (Or.inl (hb.symm ▸ sw))) + have yld := congrArg SchedContext.scYieldFrom fields + exact ⟨τ, abs, yieldFrameSchedContextA H H.sched.cscC hk' rfl yld wf ⟨rel.toSchedSRN, rd⟩⟩ + +/-- The idle, inactive, and zero-use branches preserve every SC field. -/ +theorem commit_time_quietA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {σ : State Unit} (H : YieldQueueRelA (st := st) Z m κ s σ) (a : A) + {x : SchedContext} {n : Nat} (hk : σ.kheap σ.curSc = some (.schedContext x n)) + (quiet : scActive x = false ∨ σ.curSc = idleScPtr ∨ σ.consumedTime = 0) : + sched.commit_time st ar s a ⦃ s' => ∃ τ, + fl4CommitTime (Z := Unit) σ = ({((), τ)}, False) ∧ + YieldQueueRelA (st := st) Z m κ s' τ ∧ MFrA (st := st) Z m s s' σ τ ⦄ := by + obtain ⟨step, abs⟩ := commit_time_no_chargeA H.sched a hk quiet + refine WP.spec_mono step ?_ + rintro s' ⟨hs, w⟩ + let τ := {σ with consumedTime := 0} + obtain ⟨states, mcp, links⟩ := yieldQueueFrameA (σ' := τ) H.sched H.states H.mcp H.links rfl w + exact ⟨τ, abs, ⟨hs, states, mcp, links⟩, ⟨w, SKeepA.of_kheap rfl, SSzA.of_kheap rfl⟩⟩ + +/-- The incoming SC's constant-bandwidth refresh preserves the owner's +yield link and any unrelated outstanding yield. -/ +theorem scheduler_unblock_refinesA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {σ : State Unit} (H : YieldQueueRelA (st := st) Z m κ s σ) + {c : U64} (hc : c ∈ Z.C) {x : SchedContext} {n : Nat} + (hk : σ.kheap (scNameA m c) = some (.schedContext x n)) (active : scActive x = true) + (nonrelease : ∀ t ∈ σ.releaseQueue, ∀ y, σ.kheap t = some (.tcb y) → y.tcbSchedContext ≠ some (scNameA m c)) : + sporadic.unblock_check st s c ⦃ s' => ∃ τ, + refillUnblockCheck (Z := Unit) (scNameA m c) σ = ({((), τ)}, False) ∧ + YieldQueueRelA (st := st) Z m κ s' τ ∧ MFrA (st := st) Z m s s' σ τ ⦄ := by + obtain ⟨aligned, outside⟩ := H.sched.reprA + refine WP.spec_mono (WP.spec_and (H.sched.unblock_checkA hc hk active (fun _ _ => nonrelease)) + (unblock_check_refinesA H.sched.scr H.sched.scp hc hk active H.sched.sch.time aligned outside)) ?_ + rintro s' ⟨⟨τ, abs, rel, frame⟩, ρ, spec, _, _, _, _, cases⟩ + rw [abs] at spec + have same := (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst spec))).2 + subst ρ + rcases cases with ⟨rfl, rfl⟩ | ⟨y, fields, rfl, _⟩ + · exact ⟨_, abs, H, MFrA.refl _ _ _ _⟩ + · have mfr := frame.mfr H.sched.toSchedSRN + have link := congrArg SchedContext.scYieldFrom fields + exact ⟨_, abs, yieldFrameSchedContextA H hc hk rfl link mfr.wr rel⟩ + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScSchedCtxA.lean b/proofs/Fl4ProofsA64/P3/ScSchedCtxA.lean new file mode 100644 index 000000000..a8ac0af7c --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScSchedCtxA.lean @@ -0,0 +1,186 @@ +import Fl4ProofsA64.P3.ScBudgetTopA +import Fl4ProofsA64.P3.ScConsA +import Fl4SpecA64.Ext.SchedTail + +/-! The no-reprogram context-switch branch. The selected TCB remains +bound to the current SC. The concrete same-value CUR_SC write is +explicit in the postcondition. -/ + +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3.Sched + +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P1 + +noncomputable section + +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +theorem check_domain_time_singleA (s : S) : sched.check_domain_time st s = ok s := by + have expired : sched.is_cur_domain_expired st s = ok false := by + unfold sched.is_cur_domain_expired layout.NUM_DOMAINS fl4_abi.config.NUM_DOMAINS + rfl + unfold sched.check_domain_time + rw [expired, bind_ok] + rfl + +private theorem clearSchedulerConsumedA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {σ : State Unit} (H : SchedSRA st Z m κ s σ) : + sched.set_consumed st s 0#u64 ⦃ s' => + SchedSRA st Z m κ s' {σ with consumedTime := 0} ∧ + WritesIn st s s' (MWrA (st := st) Z m σ s) ⦄ := by + refine WP.spec_mono (sched.set_consumed.spec s 0#u64) ?_ + rintro s' rfl + obtain ⟨h, w⟩ := (H.toB σ.curThread).clearConsumedA + have hin := H.node_sks (o := layout.node.CONSUMED) H.sch.cpuCs (by simp [layout.node.CONSUMED]) + refine ⟨⟨h.toSchedSRN, H.rd⟩, w.mono ?_⟩ + rintro b rfl + exact Or.inr (Or.inl hin) + +private theorem commitActiveReadA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {σ : State Unit} (H : SchedSRA st Z m κ s σ) + {x : SchedContext} {n : Nat} (hk : σ.kheap (scNameA m (L.cscW s)) = some (.schedContext x n)) : + sporadic.sc_active st s (L.cscW s) = ok (scActive x) := by + refine eq_of_spec_fd (WP.spec_mono (sc_active_refinesA H.scr H.cscC hk) ?_) + intro v e + rw [getScActive_eqA hk] at e + exact (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst e))).1.symm + +private theorem commitIdleReadA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {σ : State Unit} (H : SchedSRA st Z m κ s σ) : + decide (L.cscW s ≠ L.mem s (L.nodeA s layout.node.IDLE_SC)) = + decide (scNameA m (L.cscW s) ≠ idleScPtr) := by + have hid : m.idleScOf m.curCore = specPtr (L.mem s (L.nodeA s layout.node.IDLE_SC)) := by + rw [H.sch.core] + exact H.sch.idleScOf _ H.sch.cpuCs + exact idle_testA (sc := L.cscW s) hid (H.sch.scOff _ H.cscC) + +private theorem commitConsumedSelfA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {σ : State Unit} (H : SchedSRA st Z m κ s σ) + {x : SchedContext} {n : Nat} (hk : σ.kheap (scNameA m (L.cscW s)) = some (.schedContext x n)) : + obj.Sc.set_consumed st (L.cscW s) s (L.mem s (scWA (L.cscW s) 8#u64)) ⦃ + s' => SchedSRA st Z m κ s' σ ∧ WritesIn st s s' (MWrA (st := st) Z m σ s) ⦄ := by + let sc := L.cscW s + obtain ⟨x', n', hk', R⟩ := H.scr.sc sc H.cscC + rw [hk] at hk'; cases hk' + refine WP.spec_mono (obj.Sc.set_consumed.specSc s sc _) ?_ + rintro s' rfl + obtain ⟨R', W⟩ := R.set_consumed (L.mem s (scWA sc 8#u64)) + have e : {x with scConsumed := (L.mem s (scWA sc 8#u64)).bv} = x := by + rw [← R.consumed] + rw [e] at R' + have core : L.mem (L.wr s (scWA sc 8#u64) (L.mem s (scWA sc 8#u64))) (scCoreA sc) = L.mem s (scCoreA sc) := by + have off : scCoreA sc = scWA sc 16#u64 := by unfold scCoreA scWA layout.sc.CORE; rfl + rw [off] + apply L.mem_wr_ne (R.hdr (by decide : (8#u64).val < 96) (by decide)).1 + (R.hdr (by decide : (16#u64).val < 96) (by decide)).1 + have hf := R.bounds.2.2 + rw [scWA_val (sc := sc) (K := 8#u64) (by simp; omega), + scWA_val (sc := sc) (K := 16#u64) (by simp; omega)] + simp + have R0 := H.scObjA (sc := sc) H.cscC hk W R' rfl rfl rfl rfl core + have self : updKSc σ (scNameA m sc) (.schedContext x n) = σ := updKSc_selfA hk + rw [self] at R0 + exact ⟨R0, W.mono fun b hb => Or.inl ⟨sc, H.cscC, x, n, hk, hb⟩⟩ + +/-- The commit clears the local accumulator even on idle or inactive +SCs. With zero usage the active branch can write the same SC value +while its owner is still released. -/ +theorem commit_time_no_chargeA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {σ : State Unit} (H : SchedSRA st Z m κ s σ) (a : A) + {x : SchedContext} {n : Nat} (hk : σ.kheap σ.curSc = some (.schedContext x n)) + (quiet : scActive x = false ∨ σ.curSc = idleScPtr ∨ σ.consumedTime = 0) : + sched.commit_time st ar s a ⦃ s' => SchedSRA st Z m κ s' {σ with consumedTime := 0} ∧ + WritesIn st s s' (MWrA (st := st) Z m σ s) ⦄ ∧ + fl4CommitTime (Z := Unit) σ = ({((), {σ with consumedTime := 0})}, False) := by + have cur := H.toSchedSRN.currentScA + have hk' : σ.kheap (scNameA m (L.cscW s)) = some (.schedContext x n) := by rw [← cur]; exact hk + have b := commitActiveReadA H hk' + have clear := clearSchedulerConsumedA H + have unit : NonDet.pure () σ = ({((), σ)}, False) := rfl + constructor + · unfold sched.commit_time + have readCurrent : sched.cur_sc st s = ok (L.cscW s) := eq_of_spec_fd (sched.cur_sc.spec s) + rw [readCurrent, bind_ok, b, bind_ok] + cases active : scActive x with + | false => simpa only [Bool.false_eq_true, ↓reduceIte, bind_ok] using clear + | true => + simp only [↓reduceIte] + rw [eq_of_spec_fd (sched.idle_sc.specA (st := st) s), bind_ok, sc_ne_eqA, bind_ok, + commitIdleReadA H, ← cur] + by_cases idle : σ.curSc = idleScPtr + · rw [decide_eq_false (not_not.2 idle)] + simpa only [Bool.false_eq_true, ↓reduceIte, bind_ok] using clear + · have hz := (quiet.resolve_left (by rw [active]; decide)).resolve_left idle + have c0 : L.mem s (L.nodeA s layout.node.CONSUMED) = 0#u64 := eq_of_bv (H.rest.consumed.symm.trans hz) + rw [decide_eq_true idle] + simp only [↓reduceIte] + rw [eq_of_spec_fd (sched.consumed.specA (st := st) s), bind_ok, c0] + simp only [show ¬ (0#u64 : U64) > 0#u64 from by decide, ↓reduceIte, bind_ok] + rw [cfgResultAssocA, eq_of_spec_fd (obj.Sc.consumed.specSc s _), bind_ok] + simp only [lift, bind_ok] + have add0 : core.num.U64.wrapping_add (L.mem s (scWA (L.cscW s) 8#u64)) 0#u64 = + L.mem s (scWA (L.cscW s) 8#u64) := by + apply eq_of_bv + rw [core.num.U64.wrapping_add_bv_eq] + exact BitVec.add_zero _ + rw [add0] + refine WP.spec_bind (commitConsumedSelfA H hk') ?_ + rintro s1 ⟨H1, w1⟩ + refine WP.spec_mono (clearSchedulerConsumedA H1) ?_ + rintro s2 ⟨H2, w2⟩ + exact ⟨H2, (w1.trans w2).mono fun _ h => h.elim id (mwr_back rfl rfl (SSzA.refl _) w1.1)⟩ + · unfold fl4CommitTime + rw [nd_gets_bindSc, nd_bind_oneSc (getScActive_eqA hk), NonDet.when] + by_cases charge : scActive x = true ∧ σ.curSc ≠ idleScPtr + · have hz := (quiet.resolve_left (by rw [charge.1]; decide)).resolve_left charge.2 + rw [if_pos charge, nd_bind_assocSc, nd_gets_bindSc, hz, NonDet.when, + if_neg (by decide), nd_bind_assocSc, nd_bind_oneSc unit] + have zero : x.scConsumed + (0 : Ticks) = x.scConsumed := by bv_omega + have eq : { x with scConsumed := x.scConsumed + 0 } = x := by rw [zero] + rw [nd_bind_oneSc (updateSchedContext_eqA hk _), eq, updKSc_selfA hk] + rfl + · rw [if_neg charge, nd_bind_oneSc unit] + rfl + +/-- With the same selected SC and no outstanding timer request, the +only concrete operation is the same-value CUR_SC write. -/ +theorem switch_sched_context_sameA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {σ : State Unit} (H : SchedSRA st Z m κ s σ) (a : A) + {x : Tcb} (hk : σ.kheap σ.curThread = some (.tcb x)) + (live : x.tcbSchedContext = some σ.curSc) + (same : L.mem s (scA (L.curW s)) = L.cscW s) + (quiet : L.mem s (L.nodeAt s (L.cpu s) layout.node.REPROGRAM) = 0#u64) : + sched.switch_sched_context st ar s a = + ok (L.wr s (L.nodeAt s (L.cpu s) layout.node.CUR_SC) (L.cscW s), a) ∧ + fl4SwitchSchedContext (Z := Unit) σ = ({((), σ)}, False) := by + constructor + · have readCurrent : sched.cur_thread st s = ok (L.curW s) := + eq_of_spec_fd (sched.cur_thread.spec s) + have readTarget : obj.Tcb.sc st (L.curW s) s = ok (L.cscW s) := + (eq_of_spec_fd (obj.Tcb.sc.spec s (L.curW s))).trans (congrArg ok same) + have readOld : sched.cur_sc st s = ok (L.cscW s) := eq_of_spec_fd (sched.cur_sc.spec s) + unfold sched.switch_sched_context + rw [readCurrent, bind_ok, readTarget, bind_ok, readOld, bind_ok] + rw [sc_ne_eqA, bind_ok, decide_eq_false (not_not.2 rfl)] + simp only [Bool.false_eq_true, ↓reduceIte, bind_ok] + rw [L.cur_cpu_eq, bind_ok, eq_of_spec_fd (obj.node_get.spec s _ _), bind_ok, quiet] + simp only [show (0#u64 != 0#u64) = false from by decide, Bool.false_eq_true, ↓reduceIte, bind_ok] + rw [L.cur_cpu_eq, bind_ok, eq_of_spec_fd (obj.node_set.spec s _ _ _), bind_ok] + · have noReprogram : σ.reprogramTimer = false := by + rw [H.rest.repr, quiet] + rfl + unfold fl4SwitchSchedContext + rw [nd_bind_oneSc (show NonDet.get σ = ({(σ, σ)}, False) from rfl)] + unfold getTcbObjRef + rw [nd_bind_assocSc, nd_bind_oneSc (threadGet_eqSc _ hk), live] + change (NonDet.assertOpt (some σ.curSc) >>= _) σ = _ + rw [nd_bind_oneSc (show NonDet.assertOpt (some σ.curSc) σ = ({(σ.curSc, σ)}, False) from rfl)] + have unit : NonDet.pure () σ = ({((), σ)}, False) := rfl + rw [NonDet.when, if_neg (not_not.2 rfl), nd_bind_oneSc unit, nd_gets_bindSc, noReprogram] + rw [NonDet.when, if_neg (by decide), nd_bind_oneSc unit] + rfl + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScSchedWordsA.lean b/proofs/Fl4ProofsA64/P3/ScSchedWordsA.lean new file mode 100644 index 000000000..3375238df --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScSchedWordsA.lean @@ -0,0 +1,414 @@ +import Fl4ProofsA64.P3.ScSchedCtxA +import Fl4ProofsA64.P3.ScSelectA +import Fl4ProofsA64.P3.ScYieldQueueA + +/-! Frame the local scheduler's cursor and timer word writes. +The ready bitmap, release list and all TCB/SC objects keep their views. -/ + +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3.Sched + +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +local notation "wa" => core.num.U64.wrapping_add + +private theorem nodeWriteReadsA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {σ : State Unit} (H : SchedSRN st Z m κ s σ) {o : U64} (oh : o.val < 4288) + (aligned : o.val % 8 = 0) (v : U64) : + let addr := L.nodeA s o + let s' := L.wr s addr v + WritesIn st s s' (· = addr) ∧ + (∀ c ∈ Z.Cs, ∀ k : U64, k.val < 4288 → k.val % 8 = 0 → + L.mem s' (L.nodeAt s c k) = if c = L.cpu s ∧ k = o then v else L.mem s (L.nodeAt s c k)) ∧ + (L.ksb s).val ≤ addr.val ∧ addr.val < (L.ksb s).val + ksSizeA := by + dsimp only + obtain ⟨n, _, hn, cs⟩ := H.csN + have lt c (hc : c ∈ Z.Cs) : c.val < 128 := by have := (cs c).1 hc; omega + have ha := nodeAt_valSc (s := s) H.ksFit (lt _ H.sch.cpuCs) oh + have kb := H.ksb_al + have al : Aligned (L.nodeA s o) := by + change Aligned (L.nodeAt s (L.cpu s) o) + unfold Aligned + rw [ha] + omega + refine ⟨writesIn_wr al, ?_, ?_, ?_⟩ + · intro c hc k hk hka + by_cases eq : c = L.cpu s ∧ k = o + · rw [if_pos eq, eq.1, eq.2] + exact L.mem_wr_same al + · rw [if_neg eq] + have val := nodeAt_valSc (s := s) H.ksFit (lt _ hc) hk + apply L.mem_wr_ne al (by unfold Aligned; rw [val]; omega) + change (L.nodeAt s (L.cpu s) o).val ≠ (L.nodeAt s c k).val + rw [ha, val] + intro e + apply eq + exact ⟨UScalar.eq_of_val_eq (by omega), UScalar.eq_of_val_eq (by omega)⟩ + · change (L.ksb s).val ≤ (L.nodeAt s (L.cpu s) o).val + rw [ha]; omega + · change (L.nodeAt s (L.cpu s) o).val < _ + rw [ha] + have := lt (L.cpu s) H.sch.cpuCs + unfold ksSizeA; omega + +private def nodeGhostA (Z : XSetsA) (m : McState) (o v : U64) : XSetsA := + { Z with + curOf := if o = layout.node.CUR_THREAD then Function.update Z.curOf m.curCore (specPtr v) else Z.curOf + curScOf := if o = layout.node.CUR_SC then Function.update Z.curScOf m.curCore (scNameA m v) else Z.curScOf } + +private def nodeStateA (σ : State Unit) (m : McState) (o v : U64) : State Unit := + { σ with + curThread := if o = layout.node.CUR_THREAD then specPtr v else σ.curThread + curSc := if o = layout.node.CUR_SC then scNameA m v else σ.curSc + reprogramTimer := if o = layout.node.REPROGRAM then decide (v ≠ 0#u64) else σ.reprogramTimer } + +/-- [J] The selected words come from typed local threads and related SCs. +A cursor update only changes the local core's ghost cursor. The other +three writes keep both cursors. -/ +theorem SchedSRA.finishNodeWriteA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {σ : State Unit} (H : SchedSRA st Z m κ s σ) {o : U64} + (scope : o = layout.node.CUR_THREAD ∨ o = layout.node.CUR_SC ∨ o = layout.node.REPROGRAM ∨ o = layout.node.ARMED) + (v : U64) + (thread : o = layout.node.CUR_THREAD → v ∈ Z.T ∧ L.mem s (affA v) = L.cpu s) + (sc : o = layout.node.CUR_SC → v ∈ Z.C) : + let s' := L.wr s (L.nodeA s o) v + SchedSRA st (nodeGhostA Z m o v) m κ s' (nodeStateA σ m o v) ∧ + WritesIn st s s' (· = L.nodeA s o) := by + let s' := L.wr s (L.nodeA s o) v + have os : o.val = 0 ∨ o.val = 24 ∨ o.val = 56 ∨ o.val = 80 := by + rcases scope with e | e | e | e <;> + simp [e, layout.node.CUR_THREAD, layout.node.CUR_SC, layout.node.REPROGRAM, layout.node.ARMED] + obtain ⟨w, read, ain, aout⟩ := nodeWriteReadsA H.toSchedSRN (o := o) (by omega) (by omega) v + have ks := w.1 + have cpu := w.2.1 + have nr (k : U64) (h : k.val < 4288) (hal : k.val % 8 = 0) : + L.mem s' (L.nodeA s' k) = if k = o then v else L.mem s (L.nodeA s k) := by + unfold LawfulStore.nodeA + rw [cpu, nodeAt_of_ksb ks, read _ H.sch.cpuCs k h hal] + simp + obtain ⟨nK, gK, tK⟩ := H.toSchedSRN.keepW w (o0 := o) (by omega) (fun _ h => Or.inr h) + have tw (t : U64) (ht : t ∈ Z.T) (k : U64) (h : k.val ≤ 4088) (hal : k.val % 8 = 0) := + tK t ht k (by omega) hal + have hcore : SchedCoreA st Z.T Z.Cs s' Z.Q Z.R := + H.core.agree ks (fun t ht => ⟨tw t ht 3480#u64 (by decide) (by decide), tw t ht 3488#u64 (by decide) (by decide)⟩) + (fun c hc k h hal outside => nK c hc k h hal (by omega)) + (fun t ht => tw t ht 3592#u64 (by decide) (by decide)) (fun t ht => tw t ht 3520#u64 (by decide) (by decide)) + (fun t ht => tw t ht 3448#u64 (by decide) (by decide)) + simp only + let T := if o = layout.node.CUR_THREAD then Function.update Z.curOf m.curCore (specPtr v) else Z.curOf + let C := if o = layout.node.CUR_SC then Function.update Z.curScOf m.curCore (scNameA m v) else Z.curScOf + let τ := { σ with + curThread := if o = layout.node.CUR_THREAD then specPtr v else σ.curThread + curSc := if o = layout.node.CUR_SC then scNameA m v else σ.curSc + reprogramTimer := if o = layout.node.REPROGRAM then decide (v ≠ 0#u64) else σ.reprogramTimer } + change SchedSRA st {Z with curOf := T, curScOf := C} m κ s' τ ∧ _ + have nt (k : U64) (h : k.val < 4288) (hal : k.val % 8 = 0) (ne : k ≠ o) : + L.mem s' (L.nodeA s' k) = L.mem s (L.nodeA s k) := by rw [nr k h hal, if_neg ne] + have outside (k : U64) (hk : k.val ≠ 0 ∧ k.val ≠ 24 ∧ k.val ≠ 56 ∧ k.val ≠ 80) : k ≠ o := by + intro e + subst e + omega + have ntc (c : U64) (hc : c ∈ Z.Cs) (k : U64) (h : k.val < 4288) (hal : k.val % 8 = 0) (ne : k ≠ o) : + L.mem s' (L.nodeAt s' c k) = L.mem s (L.nodeAt s c k) := by + rw [nodeAt_of_ksb ks, read c hc k h hal, if_neg (fun e => ne e.2)] + have curRead : L.curW s' = if o = layout.node.CUR_THREAD then v else L.curW s := by + change L.mem s' (L.nodeA s' layout.node.CUR_THREAD) = _ + rw [nr _ (by simp [layout.node.CUR_THREAD]) (by simp [layout.node.CUR_THREAD])] + by_cases e : o = layout.node.CUR_THREAD <;> simp [e, Ne.symm] <;> rfl + have scRead : L.cscW s' = if o = layout.node.CUR_SC then v else L.cscW s := by + change L.mem s' (L.nodeA s' layout.node.CUR_SC) = _ + rw [nr _ (by simp [layout.node.CUR_SC]) (by simp [layout.node.CUR_SC])] + by_cases e : o = layout.node.CUR_SC <;> simp [e, Ne.symm] <;> rfl + have hcur : L.curW s' ∈ Z.T ∧ L.mem s (affA (L.curW s')) = L.cpu s := by + rw [curRead]; split + · exact thread ‹_› + · exact ⟨H.curT, H.curOn⟩ + have hnew : L.cscW s' ∈ Z.C := by + rw [scRead]; split + · exact sc ‹_› + · exact H.cscC + have hc : (L.cpu s).val < 128 := by + obtain ⟨n, _, hn, cs⟩ := H.csN + have := (cs _).1 H.sch.cpuCs + omega + have scs : ScStateRelA st Z.C m s' τ := by + have after := H.scr.wr_ks H.scp (b := L.nodeA s o) (by + have eq := nodeAt_valSc (s := s) (c := L.cpu s) (o := o) H.ksFit hc (by omega : o.val < 4288) + change (L.nodeAt s (L.cpu s) o).val % 8 = 0 + rw [eq] + have := H.ksb_al + omega) ⟨ain, aout⟩ v + exact ⟨after.sc⟩ + have rd : RdA τ := RdA.of_eq H.rd rfl rfl rfl + have actWord := nt layout.node.SCHED_ACTION (by simp [layout.node.SCHED_ACTION]) (by simp [layout.node.SCHED_ACTION]) + (outside _ (by simp [layout.node.SCHED_ACTION])) + have timeWord := nt layout.node.CUR_TIME (by simp [layout.node.CUR_TIME]) (by simp [layout.node.CUR_TIME]) + (outside _ (by simp [layout.node.CUR_TIME])) + have idleWord := nt layout.node.IDLE_THREAD (by simp [layout.node.IDLE_THREAD]) (by simp [layout.node.IDLE_THREAD]) + (outside _ (by simp [layout.node.IDLE_THREAD])) + refine ⟨⟨{ + sch := ?_ + rest := ?_ + scr := scs + scp := ?_ + thT := H.thT + place := by rw [ks]; exact H.place + csN := H.csN + ksFit := by rw [ks]; exact H.ksFit + curT := hcur.1 + core := hcore + curOn := ?_ + run := H.run + actOn := ?_ + cscC := hnew + inv := H.inv.of_kheap rfl rfl + scCore := ?_ + cscAll := fun c hc => by + rw [nodeAt_of_ksb ks, read c hc _ (by simp [layout.node.CUR_SC]) (by simp [layout.node.CUR_SC])] + split + · rename_i e; exact sc e.2.symm + · exact H.cscAll c hc }, rd⟩, w⟩ + · refine { + core := by rw [cpu]; exact H.sch.core + cpuCs := by rw [cpu]; exact H.sch.cpuCs + curOfW := ?_ + curScOfW := ?_ + cur := ?_ + csc := ?_ + idle := ?_ + idleOf := ?_ + idleScOf := ?_ + act := ?_ + time := ?_ + aff := ?_ + affT := H.sch.affT + tcb := ?_ + sc := ScStateRelA.schedSc scs + scOff := H.sch.scOff } + · intro c hc + change T c.val = _ + rw [nodeAt_of_ksb ks, read c hc _ (by simp [layout.node.CUR_THREAD]) (by simp [layout.node.CUR_THREAD])] + have ce : c.val = m.curCore ↔ c = L.cpu s := by + rw [H.sch.core]; exact ⟨UScalar.eq_of_val_eq, congrArg UScalar.val⟩ + unfold T + by_cases e : o = layout.node.CUR_THREAD + · rw [if_pos e] + by_cases hc' : c = L.cpu s + · rw [if_pos ⟨hc', e.symm⟩, (ce.2 hc'), Function.update_self] + · rw [if_neg (fun h => hc' h.1), Function.update_of_ne (fun h => hc' (ce.1 h))] + exact H.sch.curOfW c hc + · rw [if_neg e, if_neg (fun h => e h.2.symm)] + exact H.sch.curOfW c hc + · intro c hc + change C c.val = _ + rw [nodeAt_of_ksb ks, read c hc _ (by simp [layout.node.CUR_SC]) (by simp [layout.node.CUR_SC])] + have ce : c.val = m.curCore ↔ c = L.cpu s := by + rw [H.sch.core]; exact ⟨UScalar.eq_of_val_eq, congrArg UScalar.val⟩ + unfold C + by_cases e : o = layout.node.CUR_SC + · rw [if_pos e] + by_cases hc' : c = L.cpu s + · rw [if_pos ⟨hc', e.symm⟩, (ce.2 hc'), Function.update_self]; rfl + · rw [if_neg (fun h => hc' h.1), Function.update_of_ne (fun h => hc' (ce.1 h))] + exact H.sch.curScOfW c hc + · rw [if_neg e, if_neg (fun h => e h.2.symm)] + exact H.sch.curScOfW c hc + · change T m.curCore = τ.curThread + unfold T τ + by_cases e : o = layout.node.CUR_THREAD + · simp only [if_pos e, Function.update_self] + · simp only [if_neg e] + exact H.sch.cur + · change C m.curCore = τ.curSc + unfold C τ + by_cases e : o = layout.node.CUR_SC + · simp only [if_pos e, Function.update_self] + · simp only [if_neg e] + exact H.sch.csc + · change σ.idleThread = specPtr (L.mem s' (L.nodeA s' layout.node.IDLE_THREAD)) + rw [idleWord]; exact H.sch.idle + · intro c hc + rw [ntc c hc _ (by simp [layout.node.IDLE_THREAD]) (by simp [layout.node.IDLE_THREAD]) (outside _ (by simp [layout.node.IDLE_THREAD]))] + exact H.sch.idleOf c hc + · intro c hc + rw [ntc c hc _ (by simp [layout.node.IDLE_SC]) (by simp [layout.node.IDLE_SC]) (outside _ (by simp [layout.node.IDLE_SC]))] + exact H.sch.idleScOf c hc + · change σ.schedulerAction = actOfA (L.mem s' (L.nodeA s' layout.node.SCHED_ACTION)) + rw [actWord]; exact H.sch.act + · change σ.curTime = (L.mem s' (L.nodeA s' layout.node.CUR_TIME)).bv + rw [timeWord]; exact H.sch.time + · intro t ht + rw [show affA t = wa t 3520#u64 from rfl, tw t ht _ (by decide) (by decide)] + exact H.sch.aff t ht + · intro t ht + obtain ⟨x, hx, hsc⟩ := H.sch.tcb t ht + refine ⟨x, hx, ?_⟩ + rw [scA_eq, tw t ht _ (by decide) (by decide), ← scA_eq] + exact hsc + · refine { + rq := ?_ + rqDom := H.rest.rqDom + rel := ?_ + consumed := ?_ + repr := ?_ + curDom := ?_ + domTime := ?_ + prio := ?_ + scMem := ?_ + refillMax := ScStateRelA.refillMax scs } + · intro p hp + rw [cpu]; exact H.rest.rq p hp + · rw [cpu]; exact H.rest.rel + · change σ.consumedTime = (L.mem s' (L.nodeA s' layout.node.CONSUMED)).bv + rw [nt _ (by simp [layout.node.CONSUMED]) (by simp [layout.node.CONSUMED]) (outside _ (by simp [layout.node.CONSUMED]))] + exact H.rest.consumed + · change (if o = layout.node.REPROGRAM then decide (v ≠ 0#u64) else σ.reprogramTimer) = + decide (L.mem s' (L.nodeA s' layout.node.REPROGRAM) ≠ 0#u64) + rw [nr _ (by simp [layout.node.REPROGRAM]) (by simp [layout.node.REPROGRAM])] + by_cases e : o = layout.node.REPROGRAM + · simp [e] + · rw [if_neg e, if_neg (Ne.symm e)]; exact H.rest.repr + · rw [ks, gK 8#u64 (by decide) (by decide)]; exact H.rest.curDom + · rw [ks, gK 16#u64 (by decide) (by decide)]; exact H.rest.domTime + · intro t ht + obtain ⟨x, hx, d, wd, wp⟩ := H.rest.prio t ht + exact ⟨x, hx, d, (tw t ht 3432#u64 (by decide) (by decide)).trans wd, + (congrArg UScalar.val (tw t ht 3448#u64 (by decide) (by decide))).trans wp⟩ + · intro t ht + rw [scA_eq, tw t ht _ (by decide) (by decide), ← scA_eq] + exact H.rest.scMem t ht + · rw [ks] + exact H.scp.of_kheap (fun _ _ => rfl) + · rw [cpu, show affA (L.curW s') = wa (L.curW s') 3520#u64 from rfl, tw _ hcur.1 _ (by decide) (by decide)] + exact hcur.2 + · intro t eq + obtain ⟨ht, localT⟩ := H.actOn t eq + exact ⟨ht, by rw [cpu, show affA t = wa t 3520#u64 from rfl, tw t ht _ (by decide) (by decide)]; exact localT⟩ + · intro c hc + exact (w.mem_eq (H.scCoreFoot hc).1 (fun e => by + have out := H.scp.ks_out (H.scCoreFoot hc).2 + change scCoreA c = L.nodeA s o at e + rw [e] at out + omega)).trans (H.scCore c hc) + +private theorem finishNodeYieldA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {σ : State Unit} (H : YieldQueueRelA (st := st) Z m κ s σ) + {o : U64} (scope : o = layout.node.CUR_THREAD ∨ o = layout.node.CUR_SC ∨ o = layout.node.REPROGRAM ∨ o = layout.node.ARMED) + (v : U64) + (thread : o = layout.node.CUR_THREAD → v ∈ Z.T ∧ L.mem s (affA v) = L.cpu s) + (sc : o = layout.node.CUR_SC → v ∈ Z.C) : + let s' := L.wr s (L.nodeA s o) v + let τ := nodeStateA σ m o v + YieldQueueRelA (st := st) (nodeGhostA Z m o v) m κ s' τ ∧ MFrA (st := st) Z m s s' σ τ := by + obtain ⟨after, write⟩ := H.sched.finishNodeWriteA scope v thread sc + let τ := nodeStateA σ m o v + let s' := L.wr s (L.nodeA s o) v + have h : o.val < 4288 := by + rcases scope with rfl | rfl | rfl | rfl <;> + simp [layout.node.REPROGRAM, layout.node.ARMED, layout.node.CUR_THREAD, layout.node.CUR_SC] + have hI : o.val ≠ 120 ∧ o.val ≠ 128 := by + rcases scope with rfl | rfl | rfl | rfl <;> + simp [layout.node.REPROGRAM, layout.node.ARMED, layout.node.CUR_THREAD, layout.node.CUR_SC] + have inside := H.sched.node_sks (o := o) H.sched.sch.cpuCs h hI + have wf : WritesIn st s s' (SWrA (st := st) Z s) := by + refine write.mono ?_ + rintro b rfl + exact Or.inl inside + obtain ⟨states, mcp, links⟩ := yieldQueueFrameA (σ' := τ) H.sched H.states H.mcp H.links rfl + (wf.mono fun _ w => Or.inr w) + exact ⟨⟨after, states, mcp, links⟩, MFrA.of_sw rfl wf⟩ + +private theorem threadNodeOffsetsA : + layout.node.CUR_THREAD ≠ layout.node.CUR_SC ∧ layout.node.CUR_THREAD ≠ layout.node.REPROGRAM := by + have zero : layout.node.CUR_THREAD.val = 0 := by unfold layout.node.CUR_THREAD; rfl + have sc : layout.node.CUR_SC.val = 24 := by unfold layout.node.CUR_SC; rfl + have timer : layout.node.REPROGRAM.val = 56 := by unfold layout.node.REPROGRAM; rfl + constructor + · intro e + have v : layout.node.CUR_THREAD.val = layout.node.CUR_SC.val := congrArg UScalar.val e + omega + · intro e + have v : layout.node.CUR_THREAD.val = layout.node.REPROGRAM.val := congrArg UScalar.val e + omega + +private theorem threadNodeStateA (σ : State Unit) (m : McState) (t : U64) : + nodeStateA σ m layout.node.CUR_THREAD t = {σ with curThread := specPtr t} := by + unfold nodeStateA + rw [if_pos rfl, if_neg threadNodeOffsetsA.1, if_neg threadNodeOffsetsA.2] + +private theorem threadNodeCursorsA (Z : XSetsA) (m : McState) (t : U64) : + nodeGhostA Z m layout.node.CUR_THREAD t = + {Z with curOf := Function.update Z.curOf m.curCore (specPtr t)} := by + unfold nodeGhostA + rw [if_pos rfl, if_neg threadNodeOffsetsA.1] + +set_option maxHeartbeats 45000 in +/-- Preserve the scheduler and yield relations when installing a typed, +local thread in the current cursor. -/ +theorem setCurrentThreadWriteA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {σ : State Unit} (H : YieldQueueRelA (st := st) Z m κ s σ) + {t : U64} (ht : t ∈ Z.T) (hon : L.mem s (affA t) = L.cpu s) : + let s' := L.wr s (L.nodeA s layout.node.CUR_THREAD) t + let τ := {σ with curThread := specPtr t} + YieldQueueRelA (st := st) {Z with curOf := Function.update Z.curOf m.curCore (specPtr t)} m κ s' τ ∧ + MFrA (st := st) Z m s s' σ τ := by + have h := finishNodeYieldA H (o := layout.node.CUR_THREAD) (Or.inl rfl) t + (fun _ => ⟨ht, hon⟩) (fun e => (threadNodeOffsetsA.1 e).elim) + rw [threadNodeCursorsA, threadNodeStateA] at h + exact h + +private theorem scNodeTimerA : layout.node.CUR_SC ≠ layout.node.REPROGRAM := by + have sc : layout.node.CUR_SC.val = 24 := by unfold layout.node.CUR_SC; rfl + have timer : layout.node.REPROGRAM.val = 56 := by unfold layout.node.REPROGRAM; rfl + intro e + have v : layout.node.CUR_SC.val = layout.node.REPROGRAM.val := congrArg UScalar.val e + omega + +private theorem scNodeStateA (σ : State Unit) (m : McState) (c : U64) : + nodeStateA σ m layout.node.CUR_SC c = {σ with curSc := scNameA m c} := by + unfold nodeStateA + rw [if_neg (Ne.symm threadNodeOffsetsA.1), if_pos rfl, if_neg scNodeTimerA] + +private theorem scNodeCursorsA (Z : XSetsA) (m : McState) (c : U64) : + nodeGhostA Z m layout.node.CUR_SC c = + {Z with curScOf := Function.update Z.curScOf m.curCore (scNameA m c)} := by + unfold nodeGhostA + rw [if_neg (Ne.symm threadNodeOffsetsA.1), if_pos rfl] + +/-- Install the selected SC after completing the outgoing charge. -/ +theorem setCurrentScWriteA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {σ : State Unit} (H : YieldQueueRelA (st := st) Z m κ s σ) + {c : U64} (hc : c ∈ Z.C) : + let s' := L.wr s (L.nodeA s layout.node.CUR_SC) c + let τ := {σ with curSc := scNameA m c} + YieldQueueRelA (st := st) {Z with curScOf := Function.update Z.curScOf m.curCore (scNameA m c)} + m κ s' τ ∧ MFrA (st := st) Z m s s' σ τ := by + have h := finishNodeYieldA H (o := layout.node.CUR_SC) (Or.inr (Or.inl rfl)) c + (fun e => ((Ne.symm threadNodeOffsetsA.1) e).elim) (fun _ => hc) + rw [scNodeCursorsA, scNodeStateA] at h + exact h + +set_option maxHeartbeats 100000 in +set_option maxRecDepth 180 in +/-- The timer and armed words are in the current node and preserve the +ready queues, cursor, SCs, TCB states and yield links. -/ +theorem writeSchedulerTimerA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {σ : State Unit} (H : YieldQueueRelA (st := st) Z m κ s σ) + {o : U64} (scope : o = layout.node.REPROGRAM ∨ o = layout.node.ARMED) (v : U64) : + let s' := L.wr s (L.nodeA s o) v + let τ := {σ with reprogramTimer := if o = layout.node.REPROGRAM then decide (v ≠ 0#u64) else σ.reprogramTimer} + YieldQueueRelA (st := st) Z m κ s' τ ∧ MFrA (st := st) Z m s s' σ τ := by + have h : o ≠ layout.node.CUR_THREAD ∧ o ≠ layout.node.CUR_SC := by + rcases scope with rfl | rfl <;> simp [layout.node.REPROGRAM, layout.node.ARMED, + layout.node.CUR_THREAD, layout.node.CUR_SC] + have out := finishNodeYieldA H (Or.inr (Or.inr scope)) v (fun e => (h.1 e).elim) (fun e => (h.2 e).elim) + simpa only [nodeGhostA, nodeStateA, if_neg h.1, if_neg h.2] using out + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScScheduleA.lean b/proofs/Fl4ProofsA64/P3/ScScheduleA.lean new file mode 100644 index 000000000..638dc8f3b --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScScheduleA.lean @@ -0,0 +1,753 @@ +import Fl4ProofsA64.P3.ScAwakenA +import Fl4ProofsA64.P3.ScDomainChooseA +import Fl4ProofsA64.P3.ScScheduleTimerA +import Fl4ProofsA64.P3.ScInstallA +import Fl4ProofsA64.P3.ScFinishVcpuA +import Fl4ProofsA64.P3.ScAppendA + +/-! Composition of the local scheduler. The concrete action prefix is +factored at the outgoing enqueue, candidate comparison, and final +action reset. -/ + +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3.Sched + +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +/-- Offer the candidate to the ready queue before using the normal +selector. A priority tie appends instead of taking the queue head. -/ +private def enqueueAndChooseA (put : S → U64 → Result S) (s : S) (a : A) (t : U64) : Result (S × A) := do + let s1 ← put s t + let s2 ← sched.set_action st s1 sched.ACTION_CHOOSE_NEW + sched.schedule_choose_new_thread st ar s2 a + +private def equalOrSwitchA (s : S) (a : A) (t : U64) (p q : U64) (was : Bool) : Result (S × A) := + if was = true ∧ p = q then enqueueAndChooseA (st := st) (ar := ar) (sched.append st) s a t + else sched.switch_to_thread st ar s a t + +private def chooseCandidateA (s : S) (a : A) (t : U64) (was : Bool) : Result (S × A) := do + let ct := L.curW s + let idle := L.idleW s + let p := L.mem s (prioA t) + let q := L.mem s (prioA ct) + if ct = idle ∨ p < q then + let top ← sched.is_highest_prio st s (L.mem s (core.num.U64.wrapping_add (L.ksb s) 8#u64)) p + if top then equalOrSwitchA (st := st) (ar := ar) s a t p q was + else enqueueAndChooseA (st := st) (ar := ar) (sched.enqueue st) s a t + else equalOrSwitchA (st := st) (ar := ar) s a t p q was + +/-- The concrete action selection after the wake and expiry check. -/ +def scheduleSelectActionA (s : S) (a : A) : Result (S × A) := do + let t := L.actionW s + if t = sched.ACTION_RESUME_CURRENT then ok (s, a) else + let was ← sched.is_schedulable st s (L.curW s) + let s1 ← if was then sched.enqueue st s (L.curW s) else ok s + if t = sched.ACTION_CHOOSE_NEW then sched.schedule_choose_new_thread st ar s1 a + else chooseCandidateA (st := st) (ar := ar) s1 a t was + +def scheduleTailA (s : S) (a : A) : Result (S × A) := do + let s1 ← sched.set_action st s sched.ACTION_RESUME_CURRENT + let (s2, a2) ← sched.switch_sched_context st ar s1 a + let c ← st.cur_cpu s2 + let v ← obj.node_get st s2 c layout.node.REPROGRAM + let (s3, a3) ← if v != 0#u64 then do + let (s4, a4) ← sched.set_next_interrupt st ar s2 a2 + let s5 ← obj.node_set st s4 c layout.node.REPROGRAM 0#u64 + ok (s5, a4) + else ok (s2, a2) + smp.send_pending st ar s3 a3 + +private theorem candidateBranchesEqA (ct idle p q : U64) (was : Bool) (check : Result Bool) + (queue tail direct : Result (S × A)) : + (if ct = idle ∨ p < q then + (do + let b ← check + if b then (if was then if p = q then tail else direct else direct) else queue) + else if was then if p = q then tail else direct else direct) = + (if ct = idle ∨ p < q then + (do + let b ← check + if b then (if was = true ∧ p = q then tail else direct) else queue) + else if was = true ∧ p = q then tail else direct) := by + have tie : + (if was then if p = q then tail else direct else direct) = + (if was = true ∧ p = q then tail else direct) := by + cases was <;> simp only [Bool.false_eq_true, ↓reduceIte, false_and, true_and] + rw [tie] + +private theorem candidateValueEqA (s : S) (a : A) (t : U64) (was : Bool) : + (do + let ct ← sched.cur_thread st s + let idle ← sched.idle_thread st s + let b ← obj.Tcb.Insts.CoreCmpPartialEqTcb.eq ct idle + let fast ← if b then ok true else do + let p ← obj.Tcb.prio st t s + let q ← obj.Tcb.prio st ct s + if p < q then ok true else ok false + if fast then do + let d ← sched.cur_domain st s + let p ← obj.Tcb.prio st t s + let b' ← sched.is_highest_prio st s d p + if b' then + if was then do + let q ← obj.Tcb.prio st ct s + if p = q then enqueueAndChooseA (st := st) (ar := ar) (sched.append st) s a t + else sched.switch_to_thread st ar s a t + else sched.switch_to_thread st ar s a t + else enqueueAndChooseA (st := st) (ar := ar) (sched.enqueue st) s a t + else if was then do + let p ← obj.Tcb.prio st t s + let q ← obj.Tcb.prio st ct s + if p = q then enqueueAndChooseA (st := st) (ar := ar) (sched.append st) s a t + else sched.switch_to_thread st ar s a t + else sched.switch_to_thread st ar s a t) = chooseCandidateA (st := st) (ar := ar) s a t was := by + rw [eq_of_spec_fd (sched.cur_thread.spec s), bind_ok, eq_of_spec_fd (sched.idle_thread.spec' s), bind_ok] + unfold obj.Tcb.Insts.CoreCmpPartialEqTcb.eq + simp only [bind_ok, eq_of_spec_fd (obj.Tcb.prio.spec s _)] + unfold chooseCandidateA equalOrSwitchA + dsimp only + have dom : sched.cur_domain st s = ok (L.mem s (core.num.U64.wrapping_add (L.ksb s) 8#u64)) := + eq_of_spec_fd (sched.cur_domain.specA s) + have fast (p q : U64) (eq : Prop) [Decidable eq] : + (if decide eq then ok true else if p < q then ok true else ok false) = + ok (decide (eq ∨ p < q)) := by + by_cases e : eq <;> by_cases h : p < q <;> simp [e, h] + rw [fast, bind_ok] + simp only [decide_eq_true_eq, dom, bind_ok] + exact candidateBranchesEqA (L.curW s) (L.idleW s) _ _ was _ _ _ _ + +/-- The extracted schedule consists of the wake, selection, and final +context and timer phases. Candidate reads have been collapsed without +discarding their concrete outcomes. -/ +theorem schedule_phasesA (s : S) (a : A) : + sched.schedule st ar s a = + (do + let (s1, a1) ← sched.awaken st ar s a + let (s2, a2) ← scheduleSelectActionA (st := st) (ar := ar) s1 a1 + scheduleTailA (st := st) (ar := ar) s2 a2) := by + unfold sched.schedule + congr 1 + funext r + obtain ⟨s1, a1⟩ := r + simp only [uncurry_apply_pair, check_domain_time_singleA, bind_ok] + rw [eq_of_spec_fd (sched.action.spec s1), bind_ok] + unfold scheduleSelectActionA scheduleTailA + congr 1 + by_cases resume : L.actionW s1 = sched.ACTION_RESUME_CURRENT + · simp [resume] + · rw [if_neg resume, if_pos (bne_iff_ne.2 resume), eq_of_spec_fd (sched.cur_thread.spec s1), bind_ok] + congr 1 + funext was + congr 1 + funext s2 + by_cases choose : L.actionW s1 = sched.ACTION_CHOOSE_NEW + · simp only [if_pos choose] + · rw [if_neg choose, if_neg choose] + exact candidateValueEqA s2 a1 _ was + +theorem reachedResultA {α : Type} {r : Result α} {P : α → Prop} + (step : r ⦃ P ⦄) : r ⦃ v => r = ok v ∧ P v ⦄ := by + obtain ⟨v, eq, post⟩ := spec_ex_fd step + rw [eq, WP.spec_ok] + exact ⟨rfl, post⟩ + +private theorem setActionStepA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {σ : State Unit} (H : YieldQueueRelA (st := st) Z m κ s σ) + (w : U64) {act : SchedulerAction} (encode : act = actOfA w) + (noSwitch : ∀ t : ObjRef, act ≠ .switchThread t) : + sched.set_action st s w ⦃ s' => + YieldQueueRelA (st := st) Z m κ s' {σ with schedulerAction := act} ∧ + MFrA (st := st) Z m s s' σ {σ with schedulerAction := act} ⦄ := by + refine WP.spec_mono (sched.set_action.spec s w) ?_ + rintro s' rfl + let τ := {σ with schedulerAction := act} + have f := H.sched.toSchedSRN.act_wr w + obtain ⟨states, mcp, links⟩ := yieldQueueFrameA (σ' := τ) H.sched H.states H.mcp H.links rfl + (f.mono fun _ w => Or.inr w) + exact ⟨⟨H.sched.set_action w encode (fun t e => (noSwitch (specPtr t) e).elim), states, mcp, links⟩, + MFrA.of_sw rfl f⟩ + +private theorem schedulerPushA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {σ : State Unit} (H : YieldQueueRelA (st := st) Z m κ s σ) + {t : U64} (ht : t ∈ Z.T) (hon : L.mem s (affA t) = L.cpu s) + (run : schedulable (specPtr t) σ = true) (append : Bool) : + (if append then sched.append st s t else sched.enqueue st s t) ⦃ s' => + ∃ τ Q, tcbSchedAction (Z := Unit) (if append then tcbSchedAppend else tcbSchedEnqueue) (specPtr t) σ = + ({((), τ)}, False) ∧ + τ.kheap = σ.kheap ∧ τ.releaseQueue = σ.releaseQueue ∧ τ.schedulerAction = σ.schedulerAction ∧ SFixA σ τ ∧ + YieldQueueRelA (st := st) {Z with Q := Q} m κ s' τ ∧ MFrA (st := st) Z m s s' σ τ ∧ DomKeepA σ τ ⦄ := by + obtain ⟨runnable, rel⟩ := schedulable_partsA run + have linked : (L.mem s (inqA t) &&& sched.Q_RELEASE) = 0#u64 := (H.sched.relOn ht hon).2 rel + cases append + · simp only [Bool.false_eq_true, ↓reduceIte] + refine WP.spec_mono (enqueue_refines_localA H.sched t ht hon (fun _ => linked) runnable) ?_ + rintro s' ⟨τ, abs, heap, queue, action, w, hs⟩ + obtain ⟨x, hx, dom, _, _⟩ := H.sched.rest.prio t ht + have eq := tcbSchedAction_eqA hx dom tcbSchedEnqueue + rw [eq] at abs + have tau := (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst abs))).2 + have w' := w.mono fun _ h => H.sched.toSchedSRN.enq_swr ht h + obtain ⟨states, mcp, links⟩ := yieldQueueFrameA H.sched H.states H.mcp H.links heap + (w'.mono fun _ a => Or.inr a) + refine ⟨τ, _, eq.trans abs, heap, queue, action, ?_, ⟨hs, states, mcp, links⟩, MFrA.of_sw heap w', ?_⟩ + · rw [← tau] + exact ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ + · rw [← tau] + exact ⟨rfl, rfl, rfl, rfl⟩ + · simp only [↓reduceIte] + refine WP.spec_mono (append_refines_localA H.sched ht hon (fun _ => linked) runnable) ?_ + rintro s' ⟨τ, abs, heap, queue, action, fix, hs, w⟩ + obtain ⟨states, mcp, links⟩ := yieldQueueFrameA H.sched H.states H.mcp H.links heap + (w.mono fun _ a => Or.inr a) + refine ⟨τ, _, abs, heap, queue, action, fix, ⟨hs, states, mcp, links⟩, MFrA.of_sw heap w, ?_⟩ + obtain ⟨x, hx, dom, _, _⟩ := H.sched.rest.prio t ht + have eq := tcbSchedAction_eqA hx dom tcbSchedAppend + rw [eq] at abs + have tau := (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst abs))).2 + rw [← tau] + exact ⟨rfl, rfl, rfl, rfl⟩ + + +private def candidateThenChooseA (limit : Nat) (t : ObjRef) (tail : Bool) : SMonad Unit Unit := do + tcbSchedAction (if tail then tcbSchedAppend else tcbSchedEnqueue) t + setSchedulerAction .chooseNewThread + fl4ChooseNewThread limit + +open Classical in +private theorem candidateAbstractA {σ : State Unit} {t : ObjRef} {x y : Tcb} + (hx : σ.kheap t = some (.tcb x)) (hy : σ.kheap σ.curThread = some (.tcb y)) (was : Bool) + (idleNotRunnable : σ.curThread = σ.idleThread → was = false) (limit : Nat) : + fl4SelectCandidate limit t σ.curThread σ.idleThread σ.curDomain was σ = + (if (σ.curThread = σ.idleThread ∨ x.tcbPriority < y.tcbPriority) ∧ + ¬isHighestPrio σ.curDomain x.tcbPriority σ then candidateThenChooseA limit t false + else if was = true ∧ x.tcbPriority = y.tcbPriority then candidateThenChooseA limit t true + else fl4SwitchToThread t) σ := by + classical + unfold fl4SelectCandidate + rw [nd_bind_oneSc (threadGet_eqSc _ hx)] + by_cases idle : σ.curThread = σ.idleThread + · rw [if_pos idle, nd_pure_bindSc] + dsimp only + rw [nd_gets_bindSc, idleNotRunnable idle] + simp only [idle, true_or, true_and, Bool.false_eq_true, false_and, ↓reduceIte] + by_cases high : isHighestPrio σ.curDomain x.tcbPriority σ <;> + simp [high, candidateThenChooseA] + · rw [if_neg idle, nd_bind_oneSc (threadGet_eqSc _ hy)] + dsimp only + rw [nd_gets_bindSc] + by_cases high : (σ.curThread = σ.idleThread ∨ x.tcbPriority < y.tcbPriority) ∧ + ¬isHighestPrio σ.curDomain x.tcbPriority σ + · simp [high, candidateThenChooseA] + · by_cases tail : was = true ∧ x.tcbPriority = y.tcbPriority <;> + simp [high, tail, candidateThenChooseA] + +/-- The concrete candidate decision uses the abstract fast-fail and +priority-tie tests at the post-enqueue queue. Each continuation proves +the selected abstract branch. -/ +private theorem candidateCasesA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {σ : State Unit} (H : SchedSRA st Z m κ s σ) (a : A) + {t : U64} (ht : t ∈ Z.T) (was : Bool) (idleNotRunnable : σ.curThread = σ.idleThread → was = false) + (P : S × A → SMonad Unit Unit → Prop) + (onPush : ∀ tail : Bool, + enqueueAndChooseA (st := st) (ar := ar) (if tail then sched.append st else sched.enqueue st) s a t + ⦃ r => P r (candidateThenChooseA layout.NUM_DOMAIN_SCHEDULES.val (specPtr t) tail) ⦄) + (onSwitch : sched.switch_to_thread st ar s a t ⦃ r => P r (fl4SwitchToThread (specPtr t)) ⦄) + (transport : ∀ r (X Y : SMonad Unit Unit), X σ = Y σ → P r X → P r Y) : + chooseCandidateA (st := st) (ar := ar) s a t was + ⦃ r => P r (fl4SelectCandidate layout.NUM_DOMAIN_SCHEDULES.val (specPtr t) + σ.curThread σ.idleThread σ.curDomain was) ⦄ := by + classical + obtain ⟨x, hx, _, _, px⟩ := H.rest.prio _ ht + obtain ⟨y, hy, _, _, py⟩ := H.rest.prio _ H.curT + have hcur := H.sch.curThread + have hidle := H.sch.idle + have hxspec : BitVec.ofNat 8 (L.mem s (prioA t)).val = x.tcbPriority := by + rw [px] + simp + let lt := L.mem s (prioA t) < L.mem s (prioA (L.curW s)) + let eq := L.mem s (prioA t) = L.mem s (prioA (L.curW s)) + have ltEq : lt ↔ x.tcbPriority < y.tcbPriority := by + change (L.mem s (prioA t)).val < (L.mem s (prioA (L.curW s))).val ↔ _ + rw [px, py, BitVec.lt_def] + have eqEq : eq ↔ x.tcbPriority = y.tcbPriority := by + exact ⟨fun e => BitVec.eq_of_toNat_eq (px.symm.trans ((congrArg UScalar.val e).trans py)), + fun e => UScalar.eq_of_val_eq (px.trans ((congrArg BitVec.toNat e).trans py.symm))⟩ + have idleEq : L.curW s = L.idleW s ↔ σ.curThread = σ.idleThread := by + rw [hcur, hidle] + exact ⟨congrArg specPtr, specPtr_inj⟩ + let fast := σ.curThread = σ.idleThread ∨ x.tcbPriority < y.tcbPriority + let high := isHighestPrio σ.curDomain x.tcbPriority σ + let tie := was = true ∧ x.tcbPriority = y.tcbPriority + let X : SMonad Unit Unit := fl4SelectCandidate layout.NUM_DOMAIN_SCHEDULES.val (specPtr t) σ.curThread σ.idleThread σ.curDomain was + have abs := candidateAbstractA hx (by rw [hcur]; exact hy) was idleNotRunnable layout.NUM_DOMAIN_SCHEDULES.val + have next : ¬(fast ∧ ¬high) → + equalOrSwitchA (st := st) (ar := ar) s a t (L.mem s (prioA t)) (L.mem s (prioA (L.curW s))) was + ⦃ r => P r X ⦄ := by + intro skip + unfold equalOrSwitchA + have aligned : was = true ∧ eq ↔ tie := and_congr_right fun _ => eqEq + split + · rename_i test + refine WP.spec_mono (by simpa only [↓reduceIte] using onPush true) ?_ + intro r post + exact transport r _ X (by dsimp only [X]; rw [abs, if_neg skip, if_pos (aligned.1 test)]) post + · rename_i test + refine WP.spec_mono onSwitch ?_ + intro r post + exact transport r _ X (by dsimp only [X]; rw [abs, if_neg skip, if_neg (mt aligned.2 test)]) post + unfold chooseCandidateA + dsimp only + rw [H.rest.curDom.2] + have matching : (L.curW s = L.idleW s ∨ lt) ↔ fast := or_congr idleEq ltEq + split + · rename_i test + refine WP.spec_bind (is_highest_prio_refinesA H.toSchedSRN (H.core.prio _ ht)) ?_ + intro b hb + change b = @decide _ (Classical.propDecidable (isHighestPrio σ.curDomain + (BitVec.ofNat 8 (L.mem s (prioA t)).val) σ)) at hb + rw [hxspec] at hb + by_cases top : high + · have b1 : b = true := hb.trans (@decide_eq_true _ (Classical.propDecidable high) top) + rw [if_pos b1] + exact next (fun h => h.2 top) + · have b0 : b = false := hb.trans (@decide_eq_false _ (Classical.propDecidable high) top) + simp only [b0, Bool.false_eq_true, ↓reduceIte] + refine WP.spec_mono (by simpa only [Bool.false_eq_true, ↓reduceIte] using onPush false) ?_ + intro r post + exact transport r _ X (by dsimp only [X]; rw [abs, if_pos (show fast ∧ ¬high from ⟨matching.1 test, top⟩)]) post + · rename_i test + exact next (fun h => test (matching.2 h.1)) + +def selectResultA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + {a : A} (P : ArchSwFA st ar) (r : S × A) (step : SMonad Unit Unit) : Prop := + ∃ τ Q writes, step σ = ({((), τ)}, False) ∧ + (P.rootWrites a r.2 (L.cpu s).val writes ∨ (r.2 = a ∧ writes = [])) ∧ + YieldQueueRelA (st := st) {Z with Q := Q, curOf := Function.update Z.curOf m.curCore τ.curThread} + m κ r.1 τ ∧ MFrDA (st := st) Z m s r.1 σ τ ∧ (NdOKA (st := st) s σ → NdOKA (st := st) r.1 τ) + +private theorem selectResultEqA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + {a : A} (P : ArchSwFA st ar) (r : S × A) (X Y : SMonad Unit Unit) + (eq : X σ = Y σ) (post : selectResultA (Z := Z) (m := m) (κ := κ) (s := s) (σ := σ) (a := a) P r X) : + selectResultA (Z := Z) (m := m) (κ := κ) (s := s) (σ := σ) (a := a) P r Y := by + obtain ⟨τ, Q, writes, e, h⟩ := post + exact ⟨τ, Q, writes, eq.symm.trans e, h⟩ + +private theorem chooseWordA : (.chooseNewThread : SchedulerAction) = actOfA sched.ACTION_CHOOSE_NEW := by + unfold sched.ACTION_CHOOSE_NEW actOfA + decide + +private theorem resumeWordA : (.resumeCurThread : SchedulerAction) = actOfA sched.ACTION_RESUME_CURRENT := by + unfold sched.ACTION_RESUME_CURRENT actOfA + decide + +private theorem actionSetChooseA (σ : State Unit) : + setSchedulerAction .chooseNewThread σ = ({((), {σ with schedulerAction := .chooseNewThread})}, False) := rfl + +/-- [J] The root and domain facts are read at the state where the +selector uses them, after any enqueue and a zero-time domain advance. -/ +private theorem candidate_refinesA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {σ : State Unit} {a : A} {AR : A → HwRootA → Prop} + (H : YieldQueueRelA (st := st) Z m κ s σ) (P : ArchSwFA st ar) + {t : U64} (ht : t ∈ Z.T) (hon : L.mem s (affA t) = L.cpu s) + (run : schedulable (specPtr t) σ = true) (was : Bool) + (idleNotRunnable : σ.curThread = σ.idleThread → was = false) + (domain : ∀ Q s1 σ1, + YieldQueueRelA (st := st) {Z with Q := Q} m κ s1 σ1 → MFrA (st := st) Z m s s1 σ σ1 → + SchedFrameA σ σ1 → DomKeepA σ σ1 → NdOKA (st := st) s1 σ1) + (roots : ∀ Q s1 σ1, + YieldQueueRelA (st := st) {Z with Q := Q} m κ s1 σ1 → MFrDA (st := st) Z m s s1 σ σ1 → + SchedFrameA σ σ1 → JAtZAX (L := L) AR {Z with Q := Q} s1 a σ1 m ∧ VsRootOKA σ1 ∧ IdleVtA σ1 ∧ + L.mem s1 (affA (L.idleW s1)) = L.cpu s1 ∧ ArchRestA (L := L) {Z with Q := Q} s1) : + chooseCandidateA (st := st) (ar := ar) s a t was ⦃ r => + selectResultA (Z := Z) (m := m) (κ := κ) (s := s) (σ := σ) (a := a) P r + (fl4SelectCandidate layout.NUM_DOMAIN_SCHEDULES.val (specPtr t) σ.curThread σ.idleThread σ.curDomain was) ⦄ := by + apply candidateCasesA H.sched a ht was idleNotRunnable + · intro tail + unfold enqueueAndChooseA + have put : (if tail then sched.append st else sched.enqueue st) s t = + (if tail then sched.append st s t else sched.enqueue st s t) := by cases tail <;> rfl + rw [put] + refine WP.spec_bind (schedulerPushA H ht hon run tail) ?_ + rintro s1 ⟨σ1, Q1, push, _, _, _, _, H1, F1, D1⟩ + refine WP.spec_bind (setActionStepA H1 sched.ACTION_CHOOSE_NEW chooseWordA (by intro t eq; cases eq)) ?_ + rintro s2 ⟨H2, F2⟩ + have frame := F1.trans F2 rfl rfl + let σ2 := {σ1 with schedulerAction := .chooseNewThread} + have S1 : SchedFrameA σ σ1 := + (inferInstance : SPresInst (tcbSchedAction (Z := Unit) (if tail then tcbSchedAppend else tcbSchedEnqueue) + (specPtr t))).pres σ ((), σ1) (by rw [push]; exact Set.mem_singleton _) + have S2 : SchedFrameA σ σ2 := PreO.trans S1 (SchedFrameA.of_eq rfl rfl rfl rfl rfl rfl rfl rfl) + have D2 : DomKeepA σ σ2 := D1.trans ⟨rfl, rfl, rfl, rfl⟩ + have spec := schedule_choose_new_refinesA H2 P (domain Q1 s2 σ2 H2 frame S2 D2) + (fun s3 σ3 H3 F3 S3 => roots Q1 s3 σ3 H3 (frame.transD F3 rfl rfl) (PreO.trans S2 S3)) + refine WP.spec_mono spec ?_ + rintro r ⟨τ, Q, writes, abs, root, rel, frame', nd'⟩ + rw [frame.wr.2.1] at root + refine ⟨τ, Q, writes, ?_, Or.inl root, rel, frame.transD frame' rfl rfl, fun N0 => + nd' (NdOKA.keepF (st := st) (NdOKA.keepF (st := st) N0 D1 H.sched.toSchedSRN F1) + ⟨rfl, rfl, rfl, rfl⟩ H1.sched.toSchedSRN F2)⟩ + unfold candidateThenChooseA + rw [nd_bind_oneSc push, nd_bind_oneSc (actionSetChooseA σ1)] + exact abs + · obtain ⟨J, V, _, _, T⟩ := roots Z.Q s σ H (MFrDA.refl _ _ _ _) (PreO.refl σ) + have N := domain Z.Q s σ H (MFrA.refl _ _ _ _) (PreO.refl σ) (DomKeepA.refl σ) + refine WP.spec_mono (switch_to_thread_refinesA H J P V ⟨T, N.kroot⟩ ht hon) ?_ + rintro r ⟨τ, Q, abs, root, rel, frame⟩ + have selected : τ.curThread = specPtr t := by + rw [← rel.sched.sch.cur] + exact Function.update_self _ _ _ + refine ⟨τ, Q, _, abs, Or.inl root, ?_, frame.toD, + fun N0 => NdOKA.keepF (st := st) N0 (domKeep_ofQA _ abs) H.sched.toSchedSRN frame⟩ + rw [selected] + exact rel + · exact selectResultEqA (Z := Z) (m := m) (κ := κ) (s := s) (σ := σ) (a := a) P + +private theorem pushCurrentA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : YieldQueueRelA (st := st) Z m κ s σ) (was : Bool) (value : was = schedulable σ.curThread σ) : + (if was then sched.enqueue st s (L.curW s) else ok s) ⦃ s' => + ∃ τ Q, (NonDet.when was (tcbSchedAction (Z := Unit) tcbSchedEnqueue σ.curThread)) σ = + ({((), τ)}, False) ∧ τ.kheap = σ.kheap ∧ τ.releaseQueue = σ.releaseQueue ∧ + τ.schedulerAction = σ.schedulerAction ∧ SFixA σ τ ∧ + YieldQueueRelA (st := st) {Z with Q := Q} m κ s' τ ∧ MFrA (st := st) Z m s s' σ τ ∧ + SchedFrameA σ τ ∧ DomKeepA σ τ ⦄ := by + cases was + · simp only [Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + exact ⟨σ, Z.Q, rfl, rfl, rfl, rfl, SFixA.refl _, H, MFrA.refl _ _ _ _, PreO.refl σ, DomKeepA.refl σ⟩ + · have run : schedulable (specPtr (L.curW s)) σ = true := by + rw [← H.sched.sch.curThread] + exact value.symm + simp only [↓reduceIte] + refine WP.spec_mono (schedulerPushA H H.sched.curT H.sched.curOn run false) ?_ + rintro s' ⟨τ, Q, e, heap, release, act, fix, rel, frame, kept⟩ + refine ⟨τ, Q, ?_, heap, release, act, fix, rel, frame, + (inferInstance : SPresInst (tcbSchedAction (Z := Unit) tcbSchedEnqueue (specPtr (L.curW s)))).pres σ ((), τ) + (by rw [show tcbSchedAction (Z := Unit) tcbSchedEnqueue (specPtr (L.curW s)) σ = ({((), τ)}, False) from e] + exact Set.mem_singleton _), kept⟩ + simpa only [NonDet.when, ↓reduceIte, Bool.false_eq_true, H.sched.sch.curThread] using e + +/-- Select the next thread for each of the three action words. The +domain and root premises are the frozen [J] facts at the reached +input of those operations. -/ +theorem select_action_refinesA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {σ : State Unit} {a : A} {AR : A → HwRootA → Prop} + (H : YieldQueueRelA (st := st) Z m κ s σ) (P : ArchSwFA st ar) (AI : ActIdleA σ) + (domain : ∀ Q s1 σ1, + YieldQueueRelA (st := st) {Z with Q := Q} m κ s1 σ1 → MFrA (st := st) Z m s s1 σ σ1 → + SchedFrameA σ σ1 → DomKeepA σ σ1 → NdOKA (st := st) s1 σ1) + (roots : ∀ Q s1 σ1, + YieldQueueRelA (st := st) {Z with Q := Q} m κ s1 σ1 → MFrDA (st := st) Z m s s1 σ σ1 → + SchedFrameA σ σ1 → JAtZAX (L := L) AR {Z with Q := Q} s1 a σ1 m ∧ VsRootOKA σ1 ∧ IdleVtA σ1 ∧ + L.mem s1 (affA (L.idleW s1)) = L.cpu s1 ∧ ArchRestA (L := L) {Z with Q := Q} s1) : + scheduleSelectActionA (st := st) (ar := ar) s a ⦃ r => + selectResultA (Z := Z) (m := m) (κ := κ) (s := s) (σ := σ) (a := a) P r + (fl4SelectAction layout.NUM_DOMAIN_SCHEDULES.val) ⦄ := by + let w := L.actionW s + let limit := layout.NUM_DOMAIN_SCHEDULES.val + have act : σ.schedulerAction = actOfA w := H.sched.sch.act + have resume : σ.schedulerAction = .resumeCurThread ↔ w = sched.ACTION_RESUME_CURRENT := by + unfold sched.ACTION_RESUME_CURRENT + rw [act] + unfold actOfA + split + · simp [*] + · split <;> simp [*] + have get : NonDet.get σ = ({(σ, σ)}, False) := rfl + unfold scheduleSelectActionA + by_cases noWork : w = sched.ACTION_RESUME_CURRENT + · rw [if_pos noWork, WP.spec_ok] + have same : Function.update Z.curOf m.curCore σ.curThread = Z.curOf := by + rw [← H.sched.sch.cur, Function.update_eq_self] + have rel : YieldQueueRelA (st := st) + {Z with Q := Z.Q, curOf := Function.update Z.curOf m.curCore σ.curThread} m κ s σ := by + rw [same] + exact H + refine ⟨σ, Z.Q, [], ?_, Or.inr ⟨rfl, rfl⟩, rel, MFrDA.refl _ _ _ _, id⟩ + unfold fl4SelectAction + rw [nd_bind_oneSc get, NonDet.when, if_neg (not_not.2 (resume.2 noWork))] + rfl + · rw [if_neg noWork] + let was := schedulable σ.curThread σ + have runEq : sched.is_schedulable st s (L.curW s) = ok was := by + rw [is_schedulable_eqA] + have eq := schedulable_eqA H.sched.toSchedSRN H.sched.curT + (H.sched.relOn H.sched.curT H.sched.curOn) H.states + rw [eq] + dsimp only [was] + rw [H.sched.sch.curThread] + rw [runEq, bind_ok] + refine WP.spec_bind (pushCurrentA H was rfl) ?_ + rintro s1 ⟨σ1, Q1, enq, heap, released, action, fix, H1, F1, S1, D1⟩ + have abstractStep (branch : SMonad Unit Unit) {τ : State Unit} + (choose : (match σ.schedulerAction with + | .resumeCurThread => pure () + | .chooseNewThread => fl4ChooseNewThread limit + | .switchThread candidate => fl4SelectCandidate limit candidate σ.curThread σ.idleThread σ.curDomain was) = branch) + (post : branch σ1 = ({((), τ)}, False)) : + fl4SelectAction limit σ = ({((), τ)}, False) := by + unfold fl4SelectAction + rw [nd_bind_oneSc get, NonDet.when, if_pos (mt resume.1 noWork), nd_bind_oneSc enq] + exact (congrFun choose σ1).trans post + by_cases choose : w = sched.ACTION_CHOOSE_NEW + · rw [if_pos choose] + have hchoose : σ.schedulerAction = .chooseNewThread := (act.trans (congrArg actOfA choose)).trans chooseWordA.symm + refine WP.spec_mono (schedule_choose_new_refinesA H1 P (domain Q1 s1 σ1 H1 F1 S1 D1) + (fun s2 σ2 H2 F2 S2 => roots Q1 s2 σ2 H2 (F1.transD F2 rfl rfl) (PreO.trans S1 S2))) ?_ + rintro r ⟨τ, Q, writes, abs, root, rel, frame, nd⟩ + rw [F1.wr.2.1] at root + exact ⟨τ, Q, writes, abstractStep _ (by rw [hchoose]) abs, Or.inl root, rel, F1.transD frame rfl rfl, + fun N0 => nd (NdOKA.keepF (st := st) N0 D1 H.sched.toSchedSRN F1)⟩ + · rw [if_neg choose] + have no0 : w ≠ 0#u64 := by simpa only [sched.ACTION_RESUME_CURRENT] using noWork + have no1 : w ≠ 1#u64 := by simpa only [sched.ACTION_CHOOSE_NEW] using choose + have hswitch : σ.schedulerAction = .switchThread (specPtr w) := by + rw [act, actOfA, if_neg no0, if_neg no1] + obtain ⟨ht, hon⟩ := H1.sched.actOn w (action.trans hswitch) + have fresh : schedulable (specPtr w) σ1 = true := by + have stable : schedulable (specPtr w) σ1 = schedulable (specPtr w) σ := by + unfold schedulable + apply decide_eq_decide.2 + simp only [tcbsOf, scsOf] + rw [heap, released] + exact stable.trans (AI.1 _ hswitch) + have notIdle : σ1.curThread = σ1.idleThread → was = false := by + intro idle + cases h : was + · rfl + · obtain ⟨x, hk, _, hstate, _⟩ := AI.2 + have current : σ.curThread = σ.idleThread := by + rwa [fix.curThread, fix.idleThread] at idle + have awake : schedulable σ.curThread σ = true := h + obtain ⟨run, _⟩ := schedulable_partsA awake + have wrong := run x (current.symm ▸ hk) + rw [hstate] at wrong + cases wrong + refine WP.spec_mono (candidate_refinesA H1 P ht hon fresh was notIdle + (fun Q s2 σ2 H2 F2 S2 D2 => domain Q s2 σ2 H2 (F1.trans F2 rfl rfl) (PreO.trans S1 S2) (D1.trans D2)) + (fun Q s2 σ2 H2 F2 S2 => roots Q s2 σ2 H2 (F1.transD F2 rfl rfl) (PreO.trans S1 S2))) ?_ + rintro r ⟨τ, Q, writes, abs, root | silent, rel, frame, nd⟩ + · rw [F1.wr.2.1] at root + refine ⟨τ, Q, writes, abstractStep _ (by rw [hswitch]) ?_, Or.inl root, rel, F1.transD frame rfl rfl, + fun N0 => nd (NdOKA.keepF (st := st) N0 D1 H.sched.toSchedSRN F1)⟩ + simpa only [fix.curThread, fix.idleThread, fix.curDomain] using abs + · refine ⟨τ, Q, writes, abstractStep _ (by rw [hswitch]) ?_, Or.inr silent, rel, F1.transD frame rfl rfl, + fun N0 => nd (NdOKA.keepF (st := st) N0 D1 H.sched.toSchedSRN F1)⟩ + simpa only [fix.curThread, fix.idleThread, fix.curDomain] using abs + +/-- [J] The current TCB link and the incoming SC at the state produced +by thread selection. The outgoing SC remains current until its charge. -/ +structure ScheduleContextInputsA (Z : XSetsA) (m : McState) (s : S) (σ : State Unit) : Prop where + selected : ∃ t, σ.kheap σ.curThread = some (.tcb t) ∧ + let c := L.mem s (scA (L.curW s)) + c ∈ Z.C ∧ t.tcbSchedContext = some (scNameA m c) + refresh : ∀ x n, σ.kheap (scNameA m (L.mem s (scA (L.curW s)))) = some (.schedContext x n) → + ¬x.scSporadic → scActive x = true ∧ + ∀ t ∈ σ.releaseQueue, ∀ y, σ.kheap t = some (.tcb y) → + y.tcbSchedContext ≠ some (scNameA m (L.mem s (scA (L.curW s)))) + +private def resetActionA (σ : State Unit) : State Unit := + {σ with schedulerAction := .resumeCurThread} + +private theorem resetActionSpecA (σ : State Unit) : + setSchedulerAction .resumeCurThread σ = ({((), resetActionA σ)}, False) := rfl + +/-- The tail consumes the frozen outgoing and incoming SC projections, +the local timer observations, and the two zero pending-IPI words. -/ +structure ScheduleTailInputsA (Z : XSetsA) (m : McState) (κ : U64 → U64) (s : S) (a : A) + (σ : State Unit) (ar : machine.Arch A) (timer : A → Nat → DeadlineViewA) (view : State Unit → Fl4TimerView) : Prop where + context : ScheduleContextInputsA (st := st) Z m s σ + charge : ∀ s0 σ0 s1 σ1, YieldQueueRelA (st := st) Z m κ s0 σ0 → + σ0 = resetActionA σ → MFrA (st := st) Z m s s0 σ σ0 → + YieldQueueRelA (st := st) Z m κ s1 σ1 → MFrA (st := st) Z m s0 s1 σ0 σ1 → + scheduleIncomingStepA σ0 (scNameA m (L.mem s0 (scA (L.curW s0)))) σ0 = ({((), σ1)}, False) → + σ1.reprogramTimer = true → OutgoingChargeA m σ1 + ticks : ScCfgTimeAgreeA ar a + timer : ∀ C σ0 s0 s1 σ1, + sched.set_action st s sched.ACTION_RESUME_CURRENT = ok s0 → + sched.switch_sched_context st ar s0 a = ok (s1, a) → + fl4SwitchSchedContext (Z := Unit) σ0 = ({((), σ1)}, False) → + σ0 = resetActionA σ → MFrA (st := st) Z m s s1 σ σ1 → + YieldQueueRelA (st := st) {Z with curScOf := C} m κ s1 σ1 → + σ1.reprogramTimer = true → + ScheduleTimerInputsA (st := st) s1 σ1 view ∧ SniHypA ar timer a (L.cpu s1).val + silent : SilentScheduleIpiA (st := st) s + +private theorem selectResetKeepsContextA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s s1 : S} {σ : State Unit} (H1 : YieldQueueRelA (st := st) Z m κ s1 (resetActionA σ)) + (context : ScheduleContextInputsA (st := st) Z m s σ) : + ScheduleContextInputsA (st := st) Z m s1 (resetActionA σ) := by + obtain ⟨t, ht, hc, link⟩ := context.1 + obtain ⟨t', htb, fromWord⟩ := H1.sched.sch.tcb (L.curW s1) H1.sched.curT + have hh : (resetActionA σ).kheap (resetActionA σ).curThread = some (.tcb t) := ht + rw [← H1.sched.sch.curThread, hh] at htb + cases htb + let c := L.mem s (scA (L.curW s)) + have link1 : t.tcbSchedContext = some (scNameA m c) := link + have nz : L.mem s1 (scA (L.curW s1)) ≠ 0#u64 := by + intro e + rw [fromWord, optScA, if_pos e] at link1 + cases link1 + have names : scNameA m (L.mem s1 (scA (L.curW s1))) = scNameA m c := by + rw [fromWord, optScA, if_neg nz] at link1 + exact Option.some.inj link1 + have word : L.mem s1 (scA (L.curW s1)) = c := + H1.sched.scp.inj _ (H1.sched.rest.scMem _ H1.sched.curT nz) _ hc names + refine ⟨⟨t, ht, by rw [word]; exact hc, by rw [word]; exact link⟩, ?_⟩ + rw [word] + exact context.2 + +theorem schedule_tail_refinesA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {σ : State Unit} {a : A} {timer : A → Nat → DeadlineViewA} + (H : YieldQueueRelA (st := st) Z m κ s σ) (view : State Unit → Fl4TimerView) + (domains : numDomains = 1) (minAgree : fl4MinBudget = MIN_BUDGET) + (inputs : ScheduleTailInputsA (st := st) Z m κ s a σ ar timer view) : + scheduleTailA (st := st) (ar := ar) s a ⦃ r => ∃ τ C, + ((), τ) ∈ ((setSchedulerAction .resumeCurThread >>= fun _ => fl4ScAndTimer view) σ).1 ∧ + YieldQueueRelA (st := st) {Z with curScOf := C} m κ r.1 τ ∧ + MFrA (st := st) Z m s r.1 σ τ ⦄ := by + unfold scheduleTailA + refine WP.spec_bind (reachedResultA (setActionStepA H sched.ACTION_RESUME_CURRENT resumeWordA + (by intro t eq; cases eq))) ?_ + rintro s0 ⟨acted, H0, F0⟩ + let σ0 := resetActionA σ + let word := L.mem s0 (scA (L.curW s0)) + have ready := selectResetKeepsContextA H0 inputs.context + obtain ⟨t, ht, hc, link⟩ := ready.selected + have account := switch_sched_context_refinesA H0 ht hc rfl link ready.refresh + (fun s1 σ1 H1 F1 h => inputs.charge s0 σ0 s1 σ1 H0 rfl F0 H1 F1 h) minAgree inputs.ticks + refine WP.spec_bind (reachedResultA account) ?_ + rintro ⟨s1, a1⟩ ⟨switched, σ1, sw, same, H1, F1⟩ + change a1 = a at same + subst a1 + simp only [uncurry_apply_pair] + let C := Function.update Z.curScOf m.curCore (scNameA m word) + have F01 := F0.trans F1 rfl rfl + have time := inputs.timer C σ0 s0 s1 σ1 acted switched sw rfl F01 H1 + refine WP.spec_mono (schedule_timer_tail_refinesA H1 view domains + (fun h => (time h).1) (fun h => (time h).2) + (fun s2 σ2 _ F2 => H.sched.toSchedSRN.ipiKeepM (F01.trans F2 rfl rfl) inputs.silent)) ?_ + rintro r ⟨τ, step, H2, F2⟩ + refine ⟨τ, C, ?_, H2, F01.trans F2 rfl rfl⟩ + rw [nd_bind_oneSc (resetActionSpecA σ)] + unfold fl4ScAndTimer + apply mem_bindA.2 + refine ⟨((), σ1), sw.symm ▸ Set.mem_singleton _, ?_⟩ + rw [nd_gets_bindSc] + exact step + +/-- [J] The frozen selection projections. Each observation is tied to +the frame from the state following the wake step. -/ +structure ScheduleSelectionInputsA (Z : XSetsA) (m : McState) (κ : U64 → U64) (s : S) (a : A) + (σ : State Unit) (AR : A → HwRootA → Prop) : Prop where + action : ActIdleA σ + domain : ∀ Q s1 σ1, + YieldQueueRelA (st := st) {Z with Q := Q} m κ s1 σ1 → MFrA (st := st) Z m s s1 σ σ1 → + SchedFrameA σ σ1 → DomKeepA σ σ1 → NdOKA (st := st) s1 σ1 + roots : ∀ Q s1 σ1, + YieldQueueRelA (st := st) {Z with Q := Q} m κ s1 σ1 → MFrDA (st := st) Z m s s1 σ σ1 → + SchedFrameA σ σ1 → JAtZAX (L := L) AR {Z with Q := Q} s1 a σ1 m ∧ VsRootOKA σ1 ∧ IdleVtA σ1 ∧ + L.mem s1 (affA (L.idleW s1)) = L.cpu s1 ∧ ArchRestA (L := L) {Z with Q := Q} s1 + +/-- The phase inputs for scheduling on one core. [J] The exact timer +fire is followed by the local release walk. [J] Domain, root, and +context facts are used at the reached phase boundaries. [P] Architecture +root writes and timer programming use the existing platform contracts. -/ +structure ScheduleInputsA (Z : XSetsA) (m : McState) (κ : U64 → U64) (s : S) (a : A) + (σ : State Unit) (ar : machine.Arch A) (timer : A → Nat → DeadlineViewA) (view : State Unit → Fl4TimerView) + (AR : A → HwRootA → Prop) : Prop where + oneDomain : numDomains = 1 + minAgree : fl4MinBudget = MIN_BUDGET + fire : fl4_core_a64.timer.fire st ar s a ⦃ r => ∃ σ1 Q R, + fl4TimerFires σ σ1 ∧ YieldQueueRelA (st := st) {Z with Q := Q, R := R} m κ r.1 σ1 ∧ IdleThreadsA m σ1 ⦄ + selection : ∀ (r : S × A) σ1 σ2 Q R, + sched.awaken st ar s a = ok r → + fl4TimerFires σ σ1 → awaken (Z := Unit) σ1 = ({((), σ2)}, False) → + YieldQueueRelA (st := st) {Z with Q := Q, R := R} m κ r.1 σ2 → + ScheduleSelectionInputsA (st := st) {Z with Q := Q, R := R} m κ r.1 r.2 σ2 AR + tail : ∀ (start selected : S × A) σ1 σ2 σ3 Q R Q', + sched.awaken st ar s a = ok start → + scheduleSelectActionA (st := st) (ar := ar) start.1 start.2 = ok selected → + fl4TimerFires σ σ1 → awaken (Z := Unit) σ1 = ({((), σ2)}, False) → + fl4SelectAction layout.NUM_DOMAIN_SCHEDULES.val σ2 = ({((), σ3)}, False) → + MFrDA (st := st) {Z with Q := Q, R := R} m start.1 selected.1 σ2 σ3 → + YieldQueueRelA (st := st) {Z with Q := Q', R := R, curOf := Function.update Z.curOf m.curCore σ3.curThread} + m κ selected.1 σ3 → + ScheduleTailInputsA (st := st) {Z with Q := Q', R := R, curOf := Function.update Z.curOf m.curCore σ3.curThread} + m κ selected.1 selected.2 σ3 ar timer view + +/-- Full local scheduler against the FL4 scheduler spec. The +selection proof checks ordered roots before timer programming. -/ +theorem schedule_refinesA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {σ : State Unit} {a : A} {timer : A → Nat → DeadlineViewA} + {AR : A → HwRootA → Prop} (P : ArchSwFA st ar) (view : State Unit → Fl4TimerView) + (inputs : ScheduleInputsA (st := st) Z m κ s a σ ar timer view AR) : + sched.schedule st ar s a ⦃ r => ∃ τ Q R Tcur Ccur, + ((), τ) ∈ (fl4Schedule layout.NUM_DOMAIN_SCHEDULES.val view σ).1 ∧ + YieldQueueRelA (st := st) {Z with Q := Q, R := R, curOf := Tcur, curScOf := Ccur} m κ r.1 τ ⦄ := by + rw [schedule_phasesA] + refine WP.spec_bind (reachedResultA (awaken_refinesA inputs.fire)) ?_ + rintro ⟨s1, a1⟩ ⟨wake, σ1, σ2, Q, R, fires, waking, wakeRel⟩ + simp only [uncurry_apply_pair] + have steps := inputs.selection (s1, a1) σ1 σ2 Q R wake fires waking wakeRel + refine WP.spec_bind (reachedResultA (select_action_refinesA wakeRel P steps.action steps.domain steps.roots)) ?_ + rintro ⟨s2, a2⟩ ⟨selected, σ3, Q', _, selection, _, H2, frame, _⟩ + simp only [uncurry_apply_pair] + have ready := inputs.tail (s1, a1) (s2, a2) σ1 σ2 σ3 Q R Q' wake selected fires waking selection frame H2 + refine WP.spec_mono (schedule_tail_refinesA H2 view inputs.oneDomain inputs.minAgree ready) ?_ + rintro r ⟨τ, C, tail, H3, _⟩ + refine ⟨τ, Q', R, _, C, ?_, H3⟩ + unfold fl4Schedule + apply mem_bindA.2 + refine ⟨((), σ1), mem_fl4Awaken.2 fires, ?_⟩ + rw [nd_bind_oneSc waking, nd_bind_oneSc (checkDomainTime_single_specA σ2 inputs.oneDomain)] + unfold fl4SelectThread + rw [nd_bind_assocSc, nd_bind_oneSc selection] + exact tail + +/-- [J] Activation and the local VCPU check at the actual scheduler +successor. An active guest notification uses the existing S4 hook. -/ +structure ScheduleFinishInputsA (Z : XSetsA) (m : McState) (κ : U64 → U64) + (s : S) (a : A) (σ : State Unit) (ar : machine.Arch A) : Prop where + nonnull : ∀ c ∈ Z.C, c ≠ 0#u64 + ticks : Fl4TicksToUsAgreeA ar a + activation : ∃ x, σ.kheap σ.curThread = some (.tcb x) ∧ ActivateStateOKA x.tcbState ∧ + (L.mem s (regWA (L.curW s) layout.reg.FAULT_IP)).bv = x.tcbArch.tcbContext.userRegs .faultIP + guest : ∀ s1 σ1 y, + sched.activate_thread st ar s a = ok (s1, a) → + fl4ActivateThread (Z := Unit) σ = ({((), σ1)}, False) → + σ1.kheap σ1.curThread = some (.tcb y) → y.tcbState = .ext .runningVM → + FinishVcpuReadyA (st := st) Z m κ s1 a σ1 y ar + +/-- The complete syscall finish refines schedule, activation, and the +post-activation VCPU check in concrete execution order. -/ +theorem finish_refinesA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {σ : State Unit} {a : A} {timer : A → Nat → DeadlineViewA} + {AR : A → HwRootA → Prop} (P : ArchSwFA st ar) (view : State Unit → Fl4TimerView) + (inputs : ScheduleInputsA (st := st) Z m κ s a σ ar timer view AR) + (finish : ∀ (r : S × A) τ Q R Tcur Ccur, + sched.schedule st ar s a = ok r → + ((), τ) ∈ (fl4Schedule layout.NUM_DOMAIN_SCHEDULES.val view σ).1 → + YieldQueueRelA (st := st) {Z with Q := Q, R := R, curOf := Tcur, curScOf := Ccur} m κ r.1 τ → + ScheduleFinishInputsA (st := st) {Z with Q := Q, R := R, curOf := Tcur, curScOf := Ccur} + m κ r.1 r.2 τ ar) : + syscall.finish st ar s a ⦃ r => ∃ τ Q R Tcur Ccur, + ((), τ) ∈ (fl4Finish layout.NUM_DOMAIN_SCHEDULES.val view σ).1 ∧ + YieldQueueRelA (st := st) {Z with Q := Q, R := R, curOf := Tcur, curScOf := Ccur} m κ r.1 τ ⦄ := by + unfold syscall.finish + refine WP.spec_bind (reachedResultA (schedule_refinesA P view inputs)) ?_ + rintro ⟨s1, a1⟩ ⟨scheduled, τ, Q, R, Tcur, Ccur, step, H⟩ + simp only [uncurry_apply_pair] + obtain ⟨nonnull, ticks, ⟨x, htcb, legal, pc⟩, guest⟩ := finish (s1, a1) τ Q R Tcur Ccur scheduled step H + refine WP.spec_mono (finish_after_schedule_refinesA H a1 nonnull ticks htcb legal pc guest) ?_ + rintro r ⟨ρ, abs, Hρ⟩ + refine ⟨ρ, Q, R, Tcur, Ccur, ?_, Hρ⟩ + unfold fl4Finish + exact mem_bindA.2 ⟨((), τ), step, abs.symm ▸ Set.mem_singleton _⟩ + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScScheduleTimerA.lean b/proofs/Fl4ProofsA64/P3/ScScheduleTimerA.lean new file mode 100644 index 000000000..26607e283 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScScheduleTimerA.lean @@ -0,0 +1,256 @@ +import Fl4ProofsA64.P3.ScDeadlineA +import Fl4ProofsA64.P3.ScDomainA +import Fl4ProofsA64.P3.ScSchedWordsA +import Fl4ProofsA64.P3.SpecFrA +import Fl4ProofsA64.P3.VsUnmapA + +/-! The scheduler's timer phase. [P] `SniHypA` supplies the hardware +deadline operation. [J] The timer queue supplies the observed minimum at +this phase, and the installed current SC has a live refill head. -/ + +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3.Sched + +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +private theorem timerGetScA {σ : State Unit} {t : ObjRef} {x : Tcb} {p : ObjRef} + (ht : σ.kheap t = some (.tcb x)) (link : x.tcbSchedContext = some p) : + (getTcbObjRef (Z := Unit) Tcb.tcbSchedContext t >>= NonDet.assertOpt) σ = ({(p, σ)}, False) := by + unfold getTcbObjRef + rw [nd_bind_oneSc (threadGet_eqSc _ ht), link] + rfl + +private theorem deadlineReleaseA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {σ : State Unit} (H : SchedSRA st Z m κ s σ) : + ∃ release : Option U64, + (let t := L.mem s (L.nodeA s layout.node.RELEASE_HEAD) + match release with + | none => t = 0#u64 + | some left => t ≠ 0#u64 ∧ sched.ready_time st s t = ok left) ∧ + fl4ReleaseDeadline σ.releaseQueue σ = ({(release.map UScalar.bv, σ)}, False) := by + have core := H.core.release _ H.sch.cpuCs + have head : L.mem s (L.nodeA s layout.node.RELEASE_HEAD) = (Z.R (L.cpu s)).headD 0#u64 := core.head + cases list : Z.R (L.cpu s) with + | nil => + have empty : σ.releaseQueue = [] := by rw [H.rest.rel, list]; rfl + refine ⟨none, by rw [head, list]; rfl, ?_⟩ + unfold fl4ReleaseDeadline + rw [empty] + rfl + | cons t rest => + have q : σ.releaseQueue = specPtr t :: rest.map specPtr := by rw [H.rest.rel, list]; rfl + have inR : t ∈ Z.R (L.cpu s) := by rw [list]; exact List.mem_cons_self + have ht : t ∈ Z.T := core.mem _ inR + have t0 := (tcb_ne01Sc (H.core.tcbs t ht)).1 + have hq : specPtr t ∈ σ.releaseQueue := by rw [q]; exact List.mem_cons_self + obtain ⟨x, p, y, n, hx, link, hy, active⟩ := H.inv.relSc _ hq + obtain ⟨x', hx', word⟩ := H.sch.tcb t ht + rw [hx] at hx'; cases hx' + let sc := L.mem s (scA t) + have nz : sc ≠ 0#u64 := by + intro e + rw [word, optScA, if_pos e] at link + cases link + have nextScName : p = scNameA m sc := by + rw [word, optScA, if_neg nz] at link + exact (Option.some.inj link).symm + subst p + have hm := H.rest.scMem t ht nz + obtain ⟨y', k, eq, rel⟩ := H.scr.sc sc hm + rw [hy] at eq; cases eq + have strong := rel.strong active + have hd := refillHd_eqA strong.wf strong.refills + let raw := (L.refill s sc (L.rhead s sc)).time + refine ⟨some raw, ?_, ?_⟩ + · rw [head, list] + exact ⟨t0, eq_of_spec_fd (sched.ready_time.spec s t)⟩ + · unfold fl4ReleaseDeadline + rw [q] + change ((getTcbObjRef Tcb.tcbSchedContext (specPtr t) >>= NonDet.assertOpt) >>= _) σ = _ + rw [nd_bind_oneSc (timerGetScA hx link), nd_bind_oneSc (getRefillHead_eqA hy (H.inv.ne hy active)), hd] + rfl + +private theorem yieldMachineStateA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {σ : State Unit} (H : YieldQueueRelA (st := st) Z m κ s σ) (ms : MachineState) : + YieldQueueRelA (st := st) Z m κ s {σ with machineState := ms} := by + let R := H.sched + exact ⟨⟨{ + R.toSchedSRN with + sch := ⟨R.sch.core, R.sch.cpuCs, R.sch.curOfW, R.sch.curScOfW, R.sch.cur, R.sch.csc, R.sch.idle, + R.sch.idleOf, R.sch.idleScOf, R.sch.act, R.sch.time, R.sch.aff, R.sch.affT, R.sch.tcb, R.sch.sc, R.sch.scOff⟩ + rest := ⟨R.rest.rq, R.rest.rqDom, R.rest.rel, R.rest.consumed, R.rest.repr, R.rest.curDom, R.rest.domTime, + R.rest.prio, R.rest.scMem, R.rest.refillMax⟩ + scr := ⟨R.scr.sc⟩ + scp := R.scp.of_kheap (fun _ _ => rfl) + inv := R.inv.of_kheap rfl rfl }, R.rd⟩, H.states, H.mcp, ⟨H.links.tcb, H.links.toSc, H.links.frm⟩⟩ + +private theorem setDeadline_chooseA (σ : State Unit) (d : Time) : + ∃ ms, ((), {σ with machineState := ms}) ∈ (doMachineOp (Z := Unit) (setDeadline d) σ).1 := by + have ok : NdOkA MsA (doMachineOp (Z := Unit) (setDeadline d)) := + doMachineOp_okA (machineOpLift_okA _) + obtain ⟨⟨u, τ⟩, mem⟩ := (ok σ).2.1 + obtain ⟨ms, rfl⟩ := (ok σ).2.2 _ mem + cases u + exact ⟨ms, mem⟩ + +/-- The same deadline decision, concrete hardware observation and +scheduler relation after setting the timer. -/ +theorem set_next_interrupt_refinesA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {σ : State Unit} {a : A} (H : YieldQueueRelA (st := st) Z m κ s σ) + (domains : numDomains = 1) {timer : A → Nat → DeadlineViewA} + (P : SniHypA ar timer a (L.cpu s).val) (view : State Unit → Fl4TimerView) + {earliest : U64} (read : fl4_core_a64.timer.earliest st s = ok earliest) + (earliestEq : (view σ).earliest = earliest.bv) + (armedEq : (view σ).armed = (L.mem s (L.nodeA s layout.node.ARMED)).bv) + {x : Tcb} (tc : σ.kheap σ.curThread = some (.tcb x)) + (link : x.tcbSchedContext = some σ.curSc) + {y : SchedContext} {n : Nat} (sc : σ.kheap σ.curSc = some (.schedContext y n)) + (active : scActive y = true) : + sched.set_next_interrupt st ar s a ⦃ r => ∃ τ, + ((), τ) ∈ (fl4SetNextInterrupt view σ).1 ∧ + YieldQueueRelA (st := st) Z m κ r.1 τ ∧ MFrA (st := st) Z m s r.1 σ τ ⦄ := by + let scWord := L.mem s (scA (L.curW s)) + have cur : σ.curThread = specPtr (L.curW s) := by + rw [← H.sched.sch.cur, H.sched.sch.core] + exact H.sched.sch.curOfW _ H.sched.sch.cpuCs + obtain ⟨x', hx, hword⟩ := H.sched.sch.tcb (L.curW s) H.sched.curT + rw [← cur, tc] at hx; cases hx + have nz : scWord ≠ 0#u64 := by + intro e + rw [hword, optScA, if_pos e] at link + cases link + have nextScName : σ.curSc = scNameA m scWord := by + rw [hword, optScA, if_neg nz] at link + exact (Option.some.inj link).symm + obtain ⟨y', k, hk, rel⟩ := H.sched.scr.sc scWord (H.sched.rest.scMem _ H.sched.curT nz) + rw [← nextScName, sc] at hk; cases hk + have strong := rel.strong active + let amount := (L.refill s scWord (L.rhead s scWord)).amount + have amountEq : (refillHd y).rAmount = amount.bv := + congrArg Refill.rAmount (refillHd_eqA strong.wf strong.refills) + obtain ⟨release, releaseWord, releaseAbs⟩ := deadlineReleaseA H.sched + let inputs : DeadlineInputsA := ⟨amount, release, earliest, L.curW s, L.idleW s⟩ + have inputRel : SniReadsA (st := st) s inputs := by + refine ⟨eq_of_spec_fd (sched.cur_thread.spec s), eq_of_spec_fd (sched.idle_thread.spec' s), ?_, releaseWord, read⟩ + dsimp only [inputs] + rw [eq_of_spec_fd (obj.Tcb.sc.spec s _), bind_ok, eq_of_spec_fd (sporadic.head.spec s _), bind_ok] + let now := L.curTime s + let armed := L.mem s (L.nodeA s layout.node.ARMED) + let d := fl4Deadline now.bv amount.bv none (release.map UScalar.bv) (fl4UsToTicks 1) earliest.bv + let keep := fl4KeepsDeadline now.bv armed.bv d (decide (L.curW s = L.idleW s)) + have comparison : decide (σ.curThread = σ.idleThread) = decide (L.curW s = L.idleW s) := by + rw [cur, H.sched.sch.idle] + exact decide_eq_decide.2 ⟨specPtr_inj, congrArg specPtr⟩ + have σtime : σ.curTime = now.bv := H.sched.sch.time + have one : ¬ numDomains > 1 := by simp [domains] + have abs : fl4SetNextInterrupt view σ = (if keep then NonDet.pure () σ else doMachineOp (setDeadline d) σ) := by + unfold fl4SetNextInterrupt + rw [nd_bind_oneSc (show NonDet.get σ = ({(σ, σ)}, False) from rfl), + nd_bind_oneSc (timerGetScA tc link), nd_bind_oneSc (getRefillHead_eqA sc (H.sched.inv.ne sc active))] + rw [nd_bind_oneSc releaseAbs] + simp only [if_neg one, σtime, amountEq, earliestEq, armedEq, comparison] + change (if keep then NonDet.pure () else doMachineOp (setDeadline d)) σ = _ + by_cases h : keep + · simp only [if_pos h] + · simp only [if_neg h] + refine WP.spec_mono (set_next_interrupt_wordsA inputRel P) ?_ + intro r h + change (if keep then r = (s, a) else DeadlineOutcomeA (st := st) timer s a d r) at h + by_cases kept : keep + · rw [if_pos kept] at h + subst r + exact ⟨σ, by rw [abs, if_pos kept]; exact Set.mem_singleton _, H, MFrA.refl _ _ _ _⟩ + · rw [if_neg kept] at h + obtain ⟨value, _, hw, _⟩ := h + obtain ⟨ms, mem⟩ := setDeadline_chooseA σ d + let τ := {σ with machineState := ms} + have base : YieldQueueRelA (st := st) Z m κ s τ := yieldMachineStateA H ms + have noRepr : layout.node.ARMED ≠ layout.node.REPROGRAM := by + unfold layout.node.ARMED layout.node.REPROGRAM; decide + have out := writeSchedulerTimerA base (o := layout.node.ARMED) (Or.inr rfl) value + refine ⟨τ, by rw [abs, if_neg kept]; exact mem, ?_, ?_⟩ + · rw [hw] + let repr : Bool := + if layout.node.ARMED = layout.node.REPROGRAM then decide (value ≠ 0#u64) else τ.reprogramTimer + have eq : {τ with reprogramTimer := repr} = τ := by simp only [repr, if_neg noRepr] + exact eq ▸ out.1 + · rw [hw] + exact ⟨out.2.wr, SKeepA.of_kheap rfl, SSzA.of_kheap rfl⟩ + +/-- [J] The local timer-queue minimum and the installed live SC at the +point where the scheduler may program the timer. -/ +def ScheduleTimerInputsA (s : S) (σ : State Unit) (view : State Unit → Fl4TimerView) : Prop := + ∃ earliest, fl4_core_a64.timer.earliest st s = ok earliest ∧ (view σ).earliest = earliest.bv ∧ + (view σ).armed = (L.mem s (L.nodeA s layout.node.ARMED)).bv ∧ + ∃ x y n, σ.kheap σ.curThread = some (.tcb x) ∧ x.tcbSchedContext = some σ.curSc ∧ + σ.kheap σ.curSc = some (.schedContext y n) ∧ scActive y = true + +/-- [S] No pending local IPI is sent after the scheduler timer tail. -/ +def SilentScheduleIpiA (s : S) : Prop := + L.mem s (L.nodeAt s (L.cpu s) layout.node.IPI_PENDING) = 0#u64 ∧ + L.mem s (L.nodeAt s (L.cpu s) layout.node.IPI_PENDING_HI) = 0#u64 + +/-- The armed-deadline decision followed by reprogram-bit clearing and +the local silent send. In the quiet branch the timer is untouched. -/ +theorem schedule_timer_tail_refinesA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {σ : State Unit} {a : A} (H : YieldQueueRelA (st := st) Z m κ s σ) + {timer : A → Nat → DeadlineViewA} (view : State Unit → Fl4TimerView) (domains : numDomains = 1) + (input : σ.reprogramTimer = true → ScheduleTimerInputsA (st := st) s σ view) + (platform : σ.reprogramTimer = true → SniHypA ar timer a (L.cpu s).val) + (silent : ∀ s' τ, YieldQueueRelA (st := st) Z m κ s' τ → MFrA (st := st) Z m s s' σ τ → + SilentScheduleIpiA (st := st) s') : + (do + let cpu ← st.cur_cpu s + let prog ← obj.node_get st s cpu layout.node.REPROGRAM + let (s1, a1) ← if prog != 0#u64 then do + let (s2, a2) ← sched.set_next_interrupt st ar s a + let s3 ← obj.node_set st s2 cpu layout.node.REPROGRAM 0#u64 + ok (s3, a2) + else ok (s, a) + smp.send_pending st ar s1 a1) ⦃ r => ∃ τ, + ((), τ) ∈ ((NonDet.when σ.reprogramTimer (do + fl4SetNextInterrupt view + NonDet.modify fun r : State Unit => {r with reprogramTimer := false})) σ).1 ∧ + YieldQueueRelA (st := st) Z m κ r.1 τ ∧ MFrA (st := st) Z m s r.1 σ τ ⦄ := by + rw [L.cur_cpu_eq, bind_ok, eq_of_spec_fd (obj.node_get.spec s _ _), bind_ok] + have read : (L.mem s (L.nodeAt s (L.cpu s) layout.node.REPROGRAM) != 0#u64) = σ.reprogramTimer := by + rw [H.sched.rest.repr] + generalize L.mem s (L.nodeAt s (L.cpu s) layout.node.REPROGRAM) = w + apply Bool.eq_iff_iff.2 + exact bne_iff_ne.trans (Iff.of_eq decide_eq_true_eq).symm + rw [read] + by_cases h : σ.reprogramTimer = true + · rw [if_pos h, cfgResultAssocA] + obtain ⟨earliest, obs, eq, armed, x, y, n, ht, link, hs, alive⟩ := input h + refine WP.spec_bind (set_next_interrupt_refinesA H domains (platform h) view obs eq armed ht link hs alive) ?_ + rintro ⟨s2, a2⟩ ⟨ρ, abs, rel, frame⟩ + simp only [uncurry_apply_pair] + rw [← frame.wr.2.1, eq_of_spec_fd (obj.node_set.spec s2 _ _ _), bind_ok, bind_ok] + let s3 := L.wr s2 (L.nodeA s2 layout.node.REPROGRAM) 0#u64 + let τ := {ρ with reprogramTimer := false} + have out := writeSchedulerTimerA rel (o := layout.node.REPROGRAM) (Or.inl rfl) 0#u64 + simp only [if_pos rfl, ne_eq, not_true_eq_false, decide_false] at out + have hs3 : YieldQueueRelA (st := st) Z m κ s3 τ := out.1 + have fm : MFrA (st := st) Z m s s3 σ τ := frame.trans out.2 rfl rfl + obtain ⟨lo, hi⟩ := silent s3 τ hs3 fm + change smp.send_pending st ar s3 a2 ⦃ _ ⦄ + rw [send_pending_silentA (ar := ar) s3 a2 lo hi, WP.spec_ok] + refine ⟨τ, ?_, hs3, fm⟩ + rw [NonDet.when, if_pos h] + exact mem_bindA.2 ⟨((), ρ), abs, Set.mem_singleton _⟩ + · rw [if_neg h, bind_ok] + obtain ⟨lo, hi⟩ := silent s σ H (MFrA.refl _ _ _ _) + change smp.send_pending st ar s a ⦃ _ ⦄ + rw [send_pending_silentA (ar := ar) s a lo hi, WP.spec_ok] + refine ⟨σ, ?_, H, MFrA.refl _ _ _ _⟩ + rw [NonDet.when, if_neg h] + exact Set.mem_singleton _ + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScSelectA.lean b/proofs/Fl4ProofsA64/P3/ScSelectA.lean new file mode 100644 index 000000000..28ac68d5a --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScSelectA.lean @@ -0,0 +1,255 @@ +import Fl4ProofsA64.P3.ScArchSwA +import Fl4ProofsA64.P3.ScDeqA +import Fl4ProofsA64.P3.ScCfgChargeA +import Fl4SpecA64.Ext.SchedTail +import Fl4ProofsA64.P1.QueueChoose + +/-! +The selected-thread switch. [J] The selected TCB is typed and local. +[P] `ArchSwFA` accounts for roots while the extracted scheduler +dequeues and writes CUR_THREAD. The outgoing SC is still current at +this point. +-/ + +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3.Sched + +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +set_option maxHeartbeats 85000 + +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +/-- Switch the roots before dequeuing the selected TCB. CUR_THREAD is +the final write; the outgoing SC is billed later. -/ +theorem switch_to_thread_wordsA {Z : XSetsA} {m : McState} + {s : S} {a : A} {σ : State Unit} {AR : A → HwRootA → Prop} + (J : JAtZAX (L := L) AR Z s a σ m) (P : ArchSwFA st ar) (V : VsRootOKA σ) (T : ArchTieA (L := L) Z s σ) + {t : U64} (ht : t ∈ Z.T) (hD : ∃ s1, sched.dequeue st s t = ok s1) : + sched.switch_to_thread st ar s a t ⦃ r => ∃ s1, + sched.dequeue st s t = ok s1 ∧ + P.rootWrites a r.2 (L.cpu s).val (threadRootWritesA σ (specPtr t)) ∧ + r.1 = L.wr s1 (L.nodeAt s1 (L.cpu s1) layout.node.CUR_THREAD) t ⦄ := by + obtain ⟨s1, deq⟩ := hD + unfold sched.switch_to_thread + refine WP.spec_bind (P.execThread s a (L.cpu s).val σ t Z m AR J rfl ht V T.Y0 T.K (T.R t ht) T.c0) ?_ + rintro ⟨s0, a1⟩ ⟨e, trace⟩ + change s0 = s at e + subst s0 + simp only [uncurry_apply_pair] + rw [deq, bind_ok] + refine WP.spec_bind (sched.set_cur_thread.spec s1 t) ?_ + intro s2 hs + exact (WP.spec_ok _).2 ⟨s1, rfl, trace, hs⟩ + +/-- The idle path writes the kernel root and then CUR_THREAD. -/ +theorem switch_to_idle_thread_wordsA {Z : XSetsA} {m : McState} + {s : S} {a : A} {σ : State Unit} {AR : A → HwRootA → Prop} + (J : JAtZAX (L := L) AR Z s a σ m) (P : ArchSwFA st ar) (V : IdleVtA σ) (T : ArchTieA (L := L) Z s σ) : + sched.switch_to_idle_thread st ar s a ⦃ r => + P.rootWrites a r.2 (L.cpu s).val (kernelRootWritesA σ) ∧ + r.1 = L.wr s (L.nodeAt s (L.cpu s) layout.node.CUR_THREAD) (L.idleW s) ⦄ := by + unfold sched.switch_to_idle_thread + refine WP.spec_bind (sched.idle_thread.spec' s) ?_ + intro t ht + refine WP.spec_bind (P.execIdle s a (L.cpu s).val σ Z m AR J rfl V T.K T.c0) ?_ + rintro ⟨s1, a1⟩ ⟨e, trace⟩ + change s1 = s at e + subst s1 + refine WP.spec_bind (sched.set_cur_thread.spec s t) ?_ + intro s2 hs + rw [WP.spec_ok] + exact ⟨trace, hs.trans (congrArg _ ht)⟩ + +private abbrev queuePrioA (p : U64) : Fl4SpecA64.Priority := BitVec.ofNat 8 p.val + +private theorem prioWordA (q : Fl4SpecA64.Priority) : ∃ p : U64, p.val < 256 ∧ queuePrioA p = q := by + refine ⟨⟨BitVec.ofNat 64 q.toNat⟩, ?_, ?_⟩ + · have := q.isLt + show (BitVec.ofNat 64 q.toNat).toNat < 256 + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)]; exact this + · apply BitVec.eq_of_toNat_eq + show (BitVec.ofNat 8 (BitVec.ofNat 64 q.toNat).toNat).toNat = q.toNat + have := q.isLt + simp [BitVec.toNat_ofNat, Nat.mod_eq_of_lt this] + +private theorem queuePrio_leA {p q : U64} (hp : p.val < 256) (hq : q.val < 256) : + queuePrioA p ≤ queuePrioA q ↔ p.val ≤ q.val := by + rw [BitVec.le_def] + simp only [queuePrioA, BitVec.toNat_ofNat, Nat.mod_eq_of_lt hp, Nat.mod_eq_of_lt hq] + +private theorem queueMaxA {Q : Set Fl4SpecA64.Priority} {m : Fl4SpecA64.Priority} + (hm : m ∈ Q) (hle : ∀ x ∈ Q, x ≤ m) : Fl4SpecA64.setMax Q = m := by + have unique : ∃! x, x ∈ Q ∧ ∀ y ∈ Q, y ≤ x := + ⟨m, ⟨hm, hle⟩, fun y hy => BitVec.le_antisymm (hle y hy.1) (hy.2 m hm)⟩ + unfold Fl4SpecA64.setMax Fl4SpecA64.The + rw [dif_pos unique] + exact unique.unique (Classical.choose_spec unique).1 ⟨hm, hle⟩ + +theorem readyBitmapEmptyA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {σ : State Unit} (H : SchedSRN st Z m κ s σ) : + L.l1W s (L.cpu s) = 0#u64 ↔ ∀ p, σ.readyQueues σ.curDomain p = [] := by + let c := L.cpu s + have hB := (H.core.ready c H.sch.cpuCs).2 + rw [H.rest.curDom.1] + have all : (∀ prio, σ.readyQueues 0 prio = []) ↔ ∀ p : U64, p.val < 256 → Z.Q c p = [] := by + constructor + · intro h p hp + have z := h (queuePrioA p) + rw [H.rest.rq p hp] at z + exact List.map_eq_nil_iff.1 z + · intro h q + obtain ⟨p, hp, rfl⟩ := prioWordA q + rw [H.rest.rq p hp, h p hp] + rfl + rw [all] + constructor + · intro h0 p hp + by_contra hne + have h2 := (hB.l2 p hp).2 hne + have h1 := (hB.l1 (p.val / 64) (by omega)).2 ⟨by omega, fun e => by rw [e] at h2; simp at h2⟩ + rw [h0] at h1 + simp at h1 + · intro h + by_contra h0 + obtain ⟨i, hi, hb⟩ := (ne_zero_iff_bit _).1 h0 + obtain ⟨hi4, hl2⟩ := (hB.l1 i hi).1 hb + obtain ⟨j, hj, hbj⟩ := (ne_zero_iff_bit _).1 hl2 + have hp : (64 * i + j) < 256 := by omega + let p : U64 := ⟨BitVec.ofNat 64 (64 * i + j)⟩ + have hv : p.val = 64 * i + j := by + change (BitVec.ofNat 64 (64 * i + j)).toNat = _ + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)] + have hp' : p.val < 256 := by rw [hv]; exact hp + apply (hB.l2 p hp').1 _ (h p hp') + rw [hv, show (64 * i + j) / 64 = i by omega, show (64 * i + j) % 64 = j by omega] + exact hbj + +private theorem queueMaxWordA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {σ : State Unit} (H : SchedSRN st Z m κ s σ) {p : U64} (hp : p.val < 256) + (nonempty : Z.Q (L.cpu s) p ≠ []) + (maximal : ∀ q : U64, q.val < 256 → Z.Q (L.cpu s) q ≠ [] → q.val ≤ p.val) : + Fl4SpecA64.setMax {q | σ.readyQueues σ.curDomain q ≠ []} = queuePrioA p := by + rw [H.rest.curDom.1] + refine queueMaxA (by simpa only [Set.mem_setOf_eq, H.rest.rq p hp, Ne, List.map_eq_nil_iff] using nonempty) ?_ + intro q hq + obtain ⟨w, hw, rfl⟩ := prioWordA q + apply (queuePrio_leA hw hp).2 + apply maximal w hw + intro empty + apply hq + change σ.readyQueues 0 (queuePrioA w) = [] + rw [H.rest.rq w hw, empty] + rfl + +theorem choose_pick_refinesA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {σ : State Unit} (H : SchedSRN st Z m κ s σ) + (hne : L.l1W s (L.cpu s) ≠ 0#u64) : + (do + let prio ← sched.highest_prio st s 0#u64 + let off ← sched.rq_head_off 0#u64 prio + let q ← sched.q_get st s (L.cpu s) off + ok q.head) ⦃ t => t ∈ Z.T ∧ L.mem s (affA t) = L.cpu s ∧ + specPtr t = Fl4SpecA64.the (maxNonEmptyQueue (σ.readyQueues σ.curDomain)).head? ⦄ := by + have hB := H.core.ready _ H.sch.cpuCs + refine WP.spec_bind (sched.highest_prio.spec s 0#u64 _ (H.core.nodes _ H.sch.cpuCs) hB.2 hne) ?_ + rintro p ⟨hp, hpne, hmax⟩ + refine WP.spec_bind (sched.rq_head_off.spec 0#u64 p hp) ?_ + rintro off ⟨off_post, off_post1⟩ + refine WP.spec_bind (sched.q_get.spec s (L.cpu s) off) ?_ + intro q q_post + rw [WP.spec_ok] + have hq : q = L.rqQ s (L.cpu s) p := by rw [q_post, off_post1, off_post]; rfl + have hrel := hB.1.rel p hp + obtain ⟨t, rest, ht⟩ := List.exists_cons_of_ne_nil hpne + have hhead : q.head = t := by rw [hq, hrel.head, ht]; rfl + rw [hhead] + have hmem : t ∈ Z.Q (L.cpu s) p := by rw [ht]; exact List.mem_cons_self + refine ⟨H.core.memT H.sch.cpuCs hp hmem, (H.core.inQ _ H.sch.cpuCs p hp _ hmem).1, ?_⟩ + rw [H.rest.curDom.1] + have hmx := queueMaxWordA H hp hpne hmax + rw [H.rest.curDom.1] at hmx + unfold maxNonEmptyQueue + rw [hmx, H.rest.rq p hp, ht] + rfl + +private theorem bitmapDomainA : sched.bitmap_dom 0#u64 = ok 0#u64 := by + unfold sched.bitmap_dom layout.NUM_DOMAINS fl4_abi.config.NUM_DOMAINS + rfl + +private theorem localBitmapOffsetA : sched.l1_off 0#u64 = ok l1Off := by + unfold sched.l1_off l1Off layout.node.L1_BITMAP + simp only [lift, bind_ok] + congr 1 + +/-- The fast-fail bitmap test compares the candidate against the maximum +ready priority, with the empty ready queue accepted. -/ +theorem is_highest_prio_refinesA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {σ : State Unit} (H : SchedSRN st Z m κ s σ) {p : U64} (hp : p.val < 256) : + sched.is_highest_prio st s 0#u64 p ⦃ b => + b = @decide (isHighestPrio σ.curDomain (queuePrioA p) σ) (Classical.propDecidable _) ⦄ := by + unfold sched.is_highest_prio + rw [bitmapDomainA, bind_ok, L.cur_cpu_eq, bind_ok, localBitmapOffsetA, bind_ok, + eq_of_spec_fd (obj.node_get.spec s _ _), bind_ok] + change (if L.l1W s (L.cpu s) = 0#u64 then _ else _) ⦃ _ ⦄ + by_cases empty : L.l1W s (L.cpu s) = 0#u64 + · rw [if_pos empty, WP.spec_ok] + exact (@decide_eq_true _ (Classical.propDecidable _) (Or.inl ((readyBitmapEmptyA H).1 empty))).symm + · rw [if_neg empty] + have bitmap := (H.core.ready _ H.sch.cpuCs).2 + refine WP.spec_bind (sched.highest_prio.spec s 0#u64 _ (H.core.nodes _ H.sch.cpuCs) bitmap empty) ?_ + rintro q ⟨hq, nonempty, maximal⟩ + rw [WP.spec_ok] + have maxEq := queueMaxWordA H hq nonempty maximal + have nonemptySpec := mt (readyBitmapEmptyA H).2 empty + unfold isHighestPrio + rw [maxEq] + apply decide_eq_decide.2 + rw [or_iff_right nonemptySpec] + exact (queuePrio_leA hq hp).symm + +/-- The concrete dispatch uses the idle branch exactly when all ready +queues of the current domain are empty. Otherwise it switches to the +head of the highest nonempty queue. -/ +theorem choose_thread_casesA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {a : A} {σ : State Unit} (H : SchedSRN st Z m κ s σ) (P : S × A → Prop) + (idle : (∀ prio, σ.readyQueues σ.curDomain prio = []) → + sched.switch_to_idle_thread st ar s a ⦃ r => P r ⦄) + (run : ∀ t, t ∈ Z.T → L.mem s (affA t) = L.cpu s → + (¬ ∀ prio, σ.readyQueues σ.curDomain prio = []) → + specPtr t = Fl4SpecA64.the (maxNonEmptyQueue (σ.readyQueues σ.curDomain)).head? → + sched.switch_to_thread st ar s a t ⦃ r => P r ⦄) : + sched.choose_thread st ar s a ⦃ r => P r ⦄ := by + unfold sched.choose_thread + have one : ¬ layout.NUM_DOMAINS > 1#u64 := by + simp [layout.NUM_DOMAINS, fl4_abi.config.NUM_DOMAINS] + rw [if_neg one, bind_ok, L.cur_cpu_eq, bind_ok, bitmapDomainA, bind_ok, localBitmapOffsetA, bind_ok, + eq_of_spec_fd (obj.node_get.spec s _ _), bind_ok] + change (if (L.l1W s (L.cpu s) != 0#u64) = true then _ else _) ⦃ _ ⦄ + by_cases empty : L.l1W s (L.cpu s) = 0#u64 + · rw [empty] + simp only [show (0#u64 != 0#u64) = false from by decide, Bool.false_eq_true, ↓reduceIte] + exact idle ((readyBitmapEmptyA H).1 empty) + · rw [if_pos (bne_iff_ne.2 empty)] + let pri := sched.highest_prio st s 0#u64 + let read := fun p => sched.rq_head_off 0#u64 p + let queue := fun off => sched.q_get st s (L.cpu s) off + let choose := fun t => sched.switch_to_thread st ar s a t + have factored : + Std.bind pri (fun p => Std.bind (read p) (fun off => Std.bind (queue off) (fun q => choose q.head))) = + Std.bind (Std.bind pri (fun p => Std.bind (read p) (fun off => + Std.bind (queue off) (fun q => ok q.head)))) choose := by + simp only [cfgResultAssocA, bind_ok] + change (Std.bind pri (fun p => Std.bind (read p) (fun off => + Std.bind (queue off) (fun q => choose q.head)))) ⦃ _ ⦄ + rw [factored] + refine WP.spec_bind (choose_pick_refinesA H empty) ?_ + rintro t ⟨ht, hlocal, hp⟩ + exact run t ht hlocal (mt (readyBitmapEmptyA H).2 empty) hp + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScSetStateA.lean b/proofs/Fl4ProofsA64/P3/ScSetStateA.lean new file mode 100644 index 000000000..9045cc163 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScSetStateA.lean @@ -0,0 +1,212 @@ +/- +P3 (AArch64): the runnable caller's Restart and Running transitions. +This ports the scheduler part of x86 ScDecodeSc.restart_rels and +ScDecodeSc.running_after. The TCB's STATE write is explicit in the frame. +-/ +import Fl4ProofsA64.P3.ScUnbAllA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section +open Fl4SpecA64 + +private theorem stateOffA {S : Type} {st : machine.Store S} [L : LawfulStore st] + {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRN st Z m κ s σ) {t : U64} (ht : t ∈ Z.T) : + (∀ b, (L.ksb s).val ≤ b.val → b.val < (L.ksb s).val + ksSizeA → b ≠ stA t) ∧ + ¬ SReadA st Z m s σ (stA t) := by + have htA := H.core.tcbs t ht + have hfit := htA.fits + have hst : (stA t).val = t.val + 3328 := by + rw [stA_eq, wa_valm] + have hk : (3328#u64 : U64).val = 3328 := by simp + omega + have hk := H.tcb_ks ht + have hks : ∀ b : U64, (L.ksb s).val ≤ b.val → b.val < (L.ksb s).val + ksSizeA → b ≠ stA t := by + intro b hb hb' e + rw [e, hst] at hb hb' + omega + have glob : ∀ k : U64, k.val < 223488 → core.num.U64.wrapping_add (L.ksb s) k ≠ stA t := by + intro k ho + have hfit := H.ksFit + unfold ksSizeA at hfit + have hv : (core.num.U64.wrapping_add (L.ksb s) k).val = (L.ksb s).val + k.val := by + rw [wa_valm] + omega + exact hks _ (by rw [hv]; omega) (by unfold ksSizeA; rw [hv]; omega) + refine ⟨hks, ?_⟩ + intro hr + rcases hr with ⟨c, hc, o, ho, e⟩ | e | e | ⟨u, hu, k, hK, e⟩ | hf + · obtain ⟨n, -, hn, hcs⟩ := H.csN + have hc' := (hcs _).1 hc + have hv := nodeAt_valSc H.ksFit (show c.val < 128 by omega) ho + exact hks _ (by rw [hv]; omega) (by unfold ksSizeA; rw [hv]; omega) e.symm + · exact glob _ (by simp) e.symm + · exact glob _ (by simp) e.symm + · rw [stA_eq] at e + rcases hK with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> + exact field_ne htA (H.core.tcbs u hu) (by simp) (by simp) (fun _ => by simp) e + · have hn := H.scp.th_out hf ((H.thT t).2 ht) + simp only [ObjKindA.size] at hn + omega + +private theorem setStateStepA {σ : State Unit} {p : ObjRef} {x : Tcb} + (hk : σ.kheap p = some (.tcb x)) {ts : ThreadState} + (hr : runnable x.tcbState = true) (hn : runnable ts = true) : + SStepA σ (updKSc σ p (.tcb { x with tcbState := ts })) ∧ + NoBlockESc σ (updKSc σ p (.tcb { x with tcbState := ts })) := by + let σ' := updKSc σ p (.tcb { x with tcbState := ts }) + have hh : σ'.kheap p = some (.tcb { x with tcbState := ts }) := by simp [σ', updKSc] + have far : ∀ q, q ≠ p → σ'.kheap q = σ.kheap q := fun q hq => by simp [σ', updKSc, hq] + refine ⟨⟨⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩, rfl, rfl, rfl, + fun q y n hq => far q (fun e => by rw [e, hk] at hq; cases hq), + fun q y hq => ?_, fun q y n hq => (far q (fun e => by rw [e, hh] at hq; cases hq)).symm, + fun q y hq => ?_, fun q y hq => ?_⟩, ?_⟩ + · by_cases e : q = p + · subst q + rw [hk] at hq + cases hq + exact ⟨_, hh, rfl, rfl, rfl⟩ + · exact ⟨y, (far _ e).trans hq, rfl, rfl, rfl⟩ + · by_cases e : q = p + · exact ⟨x, e.symm ▸ hk⟩ + · exact ⟨y, (far _ e).symm.trans hq⟩ + · have hq' := far q (fun e => by rw [e, hh] at hq; cases hq) + exact ⟨y, hq'.symm.trans hq, rfl⟩ + · intro q y z h1 h2 + by_cases e : q = p + · subst q + rw [hk] at h1 + rw [hh] at h2 + cases h1 + cases h2 + have hni : x.tcbState ≠ .idleThreadState := by + intro e + rw [e] at hr + cases hr + exact Or.inr (Or.inl ⟨hn, hni, Or.inl (Or.inl hr)⟩) + · rw [far _ e, h1] at h2 + cases h2 + exact Or.inl ⟨rfl, id⟩ + +private theorem setThreadState_stsActA {σ : State Unit} {p : ObjRef} {x : Tcb} + (hk : σ.kheap p = some (.tcb x)) (ts : ThreadState) : + setThreadState (Z := Unit) p ts σ = + ({((), stsActA (updKSc σ p (.tcb { x with tcbState := ts })) p)}, False) := by + unfold setThreadState + rw [nd_bind_oneSc (threadSet_eqSc _ hk)] + unfold setThreadStateAct + repeat rw [nd_gets_bindSc] + unfold stsActA Fl4Spec.NonDet.when + split + · rfl + · rfl + +private theorem stateWordStepA {S : Type} {st : machine.Store S} [L : LawfulStore st] + {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRA st Z m κ s σ) (hst : StEqA Z.T (L.mem s) σ) + {t : U64} (ht : t ∈ Z.T) {p : ObjRef} (hp : ∀ u, specPtr u = p ↔ u = t) + {x : Tcb} (hk : σ.kheap p = some (.tcb x)) (hr : runnable x.tcbState = true) + {ts : ThreadState} (hn : runnable ts = true) {v : U64} (hv : v.val = tsCodeA ts) : + let σ1 := updKSc σ p (.tcb { x with tcbState := ts }) + let s1 := L.wr s (stA t) v + SchedSRA st Z m κ s1 σ1 ∧ StEqA Z.T (L.mem s1) σ1 ∧ WritesIn st s s1 (· = stA t) := by + let σ1 := updKSc σ p (.tcb { x with tcbState := ts }) + have hal : Aligned (stA t) := by + rw [stA_eq] + exact field_aligned (H.core.tcbs t ht) (by simp) (by simp) + obtain ⟨-, hread⟩ := stateOffA H.toSchedSRN ht + let s1 := L.wr s (stA t) v + have w1 : WritesIn st s s1 (· = stA t) := writesIn_wr hal + have hs := setStateStepA hk hr hn + have H1 : SchedSRA st Z m κ s1 σ1 := + H.stable w1 (fun _ e => e ▸ hread) hs.1 hs.2 + have ht1 : StEqA Z.T (L.mem s1) σ1 := by + intro u hu y hy + by_cases e : u = t + · subst u + change (updKSc σ p (.tcb { x with tcbState := ts })).kheap (specPtr t) = some (.tcb y) at hy + rw [(hp t).2 rfl, updKSc_kheap, Function.update_self] at hy + cases hy + rw [L.mem_wr_same hal] + exact hv + · have hne : specPtr u ≠ p := fun eq => e ((hp u).1 eq) + have hy0 : σ.kheap (specPtr u) = some (.tcb y) := by + change (updKSc σ p (.tcb { x with tcbState := ts })).kheap (specPtr u) = some (.tcb y) at hy + rwa [updKSc_kheap, Function.update_of_ne hne] at hy + have hne' : stA u ≠ stA t := by + rw [stA_eq, stA_eq] + exact field_ne (H.core.tcbs u hu) (H.core.tcbs t ht) (by simp) (by simp) (fun e' => absurd e' e) + have ha : Aligned (stA u) := by + rw [stA_eq] + exact field_aligned (H.core.tcbs u hu) (by simp) (by simp) + rw [w1.mem_eq ha hne'] + exact hst u hu y hy0 + exact ⟨H1, ht1, w1⟩ + +private theorem stsAct_kheapA (σ : State Unit) (p : ObjRef) : + (stsActA σ p).kheap = σ.kheap := by + unfold stsActA + split <;> rfl + +private theorem sc_schedule_stateA {S : Type} {st : machine.Store S} [L : LawfulStore st] + {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRA st Z m κ s σ) (hst : StEqA Z.T (L.mem s) σ) (t : U64) : + sched.schedule_tcb st s t ⦃ s' => ∃ σ', σ' = stsActA σ (specPtr t) ∧ + SchedSRA st Z m κ s' σ' ∧ StEqA Z.T (L.mem s') σ' ∧ + σ'.kheap = σ.kheap ∧ WritesIn st s s' (SWrA (st := st) Z s) ⦄ := by + have hb : SWrA (st := st) Z s (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) := by + obtain ⟨n, -, hn, hc⟩ := H.csN + have hcpu := (hc _).1 H.sch.cpuCs + exact Or.inl (H.node_sks H.sch.cpuCs (by simp [layout.node.SCHED_ACTION])) + refine WP.spec_mono (schedule_tcb_refinesA H t hst) ?_ + rintro s' ⟨w, H'⟩ + have hk := stsAct_kheapA σ (specPtr t) + refine ⟨_, rfl, H', ?_, hk, w.mono fun b e => e.symm ▸ hb⟩ + intro u hu y hy + rw [hk] at hy + have al : Aligned (stA u) := by + rw [stA_eq] + exact field_aligned (H.core.tcbs u hu) (by simp) (by simp) + have hread := (stateOffA H.toSchedSRN hu).2 + have hne : stA u ≠ L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION := by + intro e + exact hread (Or.inl ⟨_, H.sch.cpuCs, _, by simp [layout.node.SCHED_ACTION], e⟩) + rw [w.mem_eq al hne] + exact hst u hu y hy + +/-- Write a runnable state to a runnable TCB and apply the exact scheduler +action update. The concrete STATE word is outside MWrA. -/ +theorem sc_set_runnable_stateA {S : Type} {st : machine.Store S} [L : LawfulStore st] + {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRA st Z m κ s σ) (hst : StEqA Z.T (L.mem s) σ) {t : U64} (ht : t ∈ Z.T) + {x : Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) (hr : runnable x.tcbState = true) + {ts : ThreadState} (hn : runnable ts = true) {v : U64} (hv : v.val = tsCodeA ts) : + sched.set_thread_state st s t v ⦃ s' => ∃ σ', + setThreadState (Z := Unit) (specPtr t) ts σ = ({((), σ')}, False) ∧ + SchedSRA st Z m κ s' σ' ∧ StEqA Z.T (L.mem s') σ' ∧ + σ'.kheap = (updKSc σ (specPtr t) (.tcb { x with tcbState := ts })).kheap ∧ + WritesIn st s s' (fun b => SWrA (st := st) Z s b ∨ b = stA t) ⦄ := by + let p := specPtr t + let σ1 := updKSc σ p (.tcb { x with tcbState := ts }) + let s1 := L.wr s (stA t) v + have hp : ∀ u, specPtr u = p ↔ u = t := + fun u => ⟨specPtr_inj, fun e => congrArg specPtr e⟩ + obtain ⟨H1, ht1, w1⟩ := stateWordStepA H hst ht hp hk hr hn hv + unfold sched.set_thread_state + refine WP.spec_bind (obj.Tcb.set_state.spec s t v) ?_ + intro s₂ hs₂ + rw [hs₂] + refine WP.spec_mono (sc_schedule_stateA H1 ht1 t) ?_ + rintro s' ⟨σ', he, H', hs', hheap, w2⟩ + refine ⟨σ', ?_, H', hs', hheap, (w1.trans w2).mono ?_⟩ + · rw [he] + exact setThreadState_stsActA hk _ + · intro b h + exact h.elim Or.inr (fun h => Or.inl (swr_ksb rfl w1.1 h)) + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScSpecA.lean b/proofs/Fl4ProofsA64/P3/ScSpecA.lean new file mode 100644 index 000000000..417679189 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScSpecA.lean @@ -0,0 +1,288 @@ +/- +P3 (aarch64, A64-P3-MCS gen 1): **the spec's SC accessors on a related state** and **the refill ring as a list** +(x86 `P3/ScSpec`, the SC half of `P3/ScBudgetSpec`, `P3/ScCharge2`'s `refillResetRr_eq`, `P3/ScRing` / +`P3/ScUnblock`'s list lemmas; ported). + +`Fl4SpecA64`'s SC accessors (`KHeap`, `CSpaceAcc`, `SchedContext`, `Ipc.refillResetRr`) are the x86 port's text +(diffed: only `commitTime`'s idle-SC test reads the constant `idle_sc_ptr`), so x86's lemmas port with the namespace +changed and `updSc σ p x n` written `updKSc σ p (.schedContext x n)` (`P3/ScRelA`). On a state where `p` is +`.schedContext x n`, `get_sched_context`, `is_round_robin`, `get_refill_head` / `_tail`, `get_sc_refill_ready` / +`_sufficient`, `get_sc_active`, `update_sched_context`, `update_refill_hd` / `_tl`, `refill_pop_head` and +`refill_reset_rr` have exactly one outcome and do not fail. + +The generic monad helpers carry the suffix `Sc` (the IPC line's `NtfnSpecA` has its own `nd_bind_one` / `updK` in +namespace `Notif`; distinct names avoid an ambiguity when both namespaces are opened at the join). No named premise. +-/ +import Fl4ProofsA64.P3.ScRelA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +/-! ## Monad helpers (x86 `NtfnSpec`) -/ + +section +variable {σ α β : Type} + +theorem nd_bind_applySc (m : Fl4Spec.NonDet σ α) (f : α → Fl4Spec.NonDet σ β) (s : σ) : + (m >>= f) s = ({r | ∃ x ∈ (m s).1, r ∈ (f x.1 x.2).1}, (m s).2 ∨ ∃ x ∈ (m s).1, (f x.1 x.2).2) := rfl + +/-- Binding after a computation with a single outcome (x86 `nd_bind_one`). -/ +theorem nd_bind_oneSc {m : Fl4Spec.NonDet σ α} {s s' : σ} {a : α} (h : m s = ({(a, s')}, False)) + (f : α → Fl4Spec.NonDet σ β) : (m >>= f) s = f a s' := by + rw [nd_bind_applySc, h] + ext <;> simp + +theorem nd_gets_bindSc (g : σ → α) (f : α → Fl4Spec.NonDet σ β) (s : σ) : + (Fl4Spec.NonDet.gets g >>= f) s = f (g s) s := by + rw [nd_bind_applySc]; ext <;> simp [Fl4Spec.NonDet.gets] + +end + +/-! ## The SC accessors (x86 `ScSpec`, `ScBudgetSpec`) -/ + +section Spec +open Fl4SpecA64 +variable {σ : State Unit} {p : ObjRef} {x : SchedContext} {n : Nat} + +theorem getObject_eqSc {o : KernelObject} (hk : σ.kheap p = some o) : + getObject (Z := Unit) p σ = ({(o, σ)}, False) := by + simp [getObject, Fl4Spec.NonDet.getsThe, readObject, hk, Fl4Spec.NonDet.assertOpt]; rfl + +theorem setObject_eqSc {o o' : KernelObject} (hk : σ.kheap p = some o) (ht : aType o = aType o') : + setObject (Z := Unit) p o' σ = ({((), updKSc σ p o')}, False) := by + unfold setObject + show (getObject p >>= _) σ = _ + rw [nd_bind_oneSc (getObject_eqSc hk)] + show (Fl4Spec.NonDet.assert _ >>= _) σ = _ + rw [nd_bind_oneSc (show Fl4Spec.NonDet.assert (σ := State Unit) (aType o = aType o') σ = ({((), σ)}, False) by + simp [Fl4Spec.NonDet.assert, ht])] + show (Fl4Spec.NonDet.get >>= _) σ = _ + rw [nd_bind_oneSc (show (Fl4Spec.NonDet.get : Fl4Spec.NonDet (State Unit) (State Unit)) σ = ({(σ, σ)}, False) + from rfl)] + rfl + +theorem getSchedContext_eqA (hk : σ.kheap p = some (.schedContext x n)) : + getSchedContext (Z := Unit) p σ = ({(x, σ)}, False) := by + unfold getSchedContext + rw [nd_bind_oneSc (getObject_eqSc hk)]; rfl + +theorem readSchedContext_eqA (hk : σ.kheap p = some (.schedContext x n)) : + readSchedContext (Z := Unit) p σ = some x := by + show Option.bind (readObject p σ) (fun k => (match k with + | .schedContext sc _ => Fl4Spec.Lookup.oreturn sc + | _ => Fl4Spec.Lookup.ofail) σ) = _ + simp [readObject, hk, Fl4Spec.Lookup.oreturn] + +theorem isRoundRobin_eqA (hk : σ.kheap p = some (.schedContext x n)) : + isRoundRobin (Z := Unit) p σ = ({(decide (x.scPeriod = 0), σ)}, False) := by + unfold isRoundRobin + rw [nd_bind_oneSc (getSchedContext_eqA hk)]; rfl + +theorem readRefillHead_eqA (hk : σ.kheap p = some (.schedContext x n)) (hne : x.scRefills ≠ []) : + readRefillHead (Z := Unit) p σ = some (refillHd x) := by + show Option.bind (readSchedContext p σ) (fun sc => Option.bind (Fl4Spec.Lookup.oassert (sc.scRefills ≠ []) σ) + (fun _ => Fl4Spec.Lookup.oreturn (refillHd sc) σ)) = _ + rw [readSchedContext_eqA hk] + simp [Fl4Spec.Lookup.oassert, hne, Fl4Spec.Lookup.oreturn] + +theorem getRefillHead_eqA (hk : σ.kheap p = some (.schedContext x n)) (hne : x.scRefills ≠ []) : + getRefillHead (Z := Unit) p σ = ({(refillHd x, σ)}, False) := by + simp [getRefillHead, Fl4Spec.NonDet.getsThe, readRefillHead_eqA hk hne, Fl4Spec.NonDet.assertOpt]; rfl + +theorem getRefillTail_eqA (hk : σ.kheap p = some (.schedContext x n)) (hne : x.scRefills ≠ []) : + getRefillTail (Z := Unit) p σ = ({(refillTl x, σ)}, False) := by + have : readRefillTail (Z := Unit) p σ = some (refillTl x) := by + show Option.bind (readSchedContext p σ) (fun sc => Option.bind (Fl4Spec.Lookup.oassert (sc.scRefills ≠ []) σ) + (fun _ => Fl4Spec.Lookup.oreturn (refillTl sc) σ)) = _ + rw [readSchedContext_eqA hk] + simp [Fl4Spec.Lookup.oassert, hne, Fl4Spec.Lookup.oreturn] + simp [getRefillTail, Fl4Spec.NonDet.getsThe, this, Fl4Spec.NonDet.assertOpt]; rfl + +theorem getScRefillReady_eqA (hk : σ.kheap p = some (.schedContext x n)) (hne : x.scRefills ≠ []) : + getScRefillReady (Z := Unit) p σ = ({(refillReady σ.curTime (refillHd x), σ)}, False) := by + have : readScRefillReady (Z := Unit) p σ = some (refillReady σ.curTime (refillHd x)) := by + show Option.bind (readRefillHead p σ) (fun h => Option.bind (Fl4Spec.Lookup.asks + (fun s : State Unit => s.curTime) σ) (fun ct => Fl4Spec.Lookup.oreturn (refillReady ct h) σ)) = _ + rw [readRefillHead_eqA hk hne]; rfl + simp [getScRefillReady, Fl4Spec.NonDet.getsThe, this, Fl4Spec.NonDet.assertOpt]; rfl + +theorem getScRefillSufficient_eqA (hk : σ.kheap p = some (.schedContext x n)) (hne : x.scRefills ≠ []) + (u : Ticks) : + getScRefillSufficient (Z := Unit) p u σ = ({(refillSufficient u (refillHd x), σ)}, False) := by + have : readScRefillSufficient (Z := Unit) p u σ = some (refillSufficient u (refillHd x)) := by + show Option.bind (readRefillHead p σ) (fun h => Fl4Spec.Lookup.oreturn (refillSufficient u h) σ) = _ + rw [readRefillHead_eqA hk hne]; rfl + simp [getScRefillSufficient, Fl4Spec.NonDet.getsThe, this, Fl4Spec.NonDet.assertOpt]; rfl + +theorem getScActive_eqA (hk : σ.kheap p = some (.schedContext x n)) : + getScActive (Z := Unit) p σ = ({(scActive x, σ)}, False) := by + have : readScActive (Z := Unit) p σ = some (scActive x) := by + show Option.bind (readSchedContext p σ) (fun sc => Fl4Spec.Lookup.oreturn (scActive sc) σ) = _ + rw [readSchedContext_eqA hk]; rfl + simp [getScActive, Fl4Spec.NonDet.getsThe, this, Fl4Spec.NonDet.assertOpt]; rfl + +theorem updateSchedContext_eqA (hk : σ.kheap p = some (.schedContext x n)) (f : SchedContext → SchedContext) : + updateSchedContext (Z := Unit) p f σ = ({((), updKSc σ p (.schedContext (f x) n))}, False) := by + unfold updateSchedContext + rw [nd_bind_oneSc (getObject_eqSc hk)] + exact setObject_eqSc hk rfl + +theorem updateRefillHd_eqA (hk : σ.kheap p = some (.schedContext x n)) (f : Refill → Refill) : + updateRefillHd (Z := Unit) p f σ = + ({((), updKSc σ p (.schedContext { x with scRefills := f (the x.scRefills.head?) :: x.scRefills.tail } n))}, + False) := + updateSchedContext_eqA hk _ + +theorem updateRefillTl_eqA (hk : σ.kheap p = some (.schedContext x n)) (f : Refill → Refill) : + updateRefillTl (Z := Unit) p f σ = + ({((), updKSc σ p (.schedContext + { x with scRefills := x.scRefills.dropLast ++ [f (the x.scRefills.getLast?)] } n))}, False) := + updateSchedContext_eqA hk _ + +theorem refillPopHead_eqA (hk : σ.kheap p = some (.schedContext x n)) (hne : x.scRefills ≠ []) : + refillPopHead (Z := Unit) p σ = + ({(refillHd x, updKSc σ p (.schedContext { x with scRefills := x.scRefills.tail } n))}, False) := by + unfold refillPopHead + rw [nd_bind_oneSc (getRefillHead_eqA hk hne), nd_bind_oneSc (updateSchedContext_eqA hk _)]; rfl + +theorem updKSc_updKSc (σ : State Unit) (p : ObjRef) (o o' : KernelObject) : + updKSc (updKSc σ p o) p o' = updKSc σ p o' := by + simp [updKSc] + +theorem updKSc_same (σ : State Unit) (p : ObjRef) (o : KernelObject) : (updKSc σ p o).kheap p = some o := by + simp + +end Spec + +/-! ## The ring as a list (x86 `ScSpec`, `ScUnblock`, `ScRing`, `ScBudgetSpec`) -/ + +section +variable {S : Type _} {st : machine.Store S} [L : LawfulStore st] + +/-- A default refill (x86 `zR`). -/ +def zRA : sporadic.Refill := ⟨0#u64, 0#u64⟩ + +theorem refills_lengthA (s : S) (sc : obj.Sc) : (L.refills s sc).length = L.size s sc := by + simp [LawfulStore.refills] + +theorem refills_consA {s : S} {sc : obj.Sc} {sb : Nat} (hW : RefillsWF st s sc sb) : + L.refills s sc = L.rf s sc 0 :: (List.range (L.size s sc - 1)).map (fun k => L.rf s sc (k + 1)) := by + unfold LawfulStore.refills + obtain ⟨m, hm⟩ : ∃ m, L.size s sc = m + 1 := ⟨L.size s sc - 1, by have := hW.size_pos; omega⟩ + rw [hm, List.range_succ_eq_map] + simp + +theorem refills_neA {s : S} {sc : obj.Sc} {sb : Nat} (hW : RefillsWF st s sc sb) : + (L.refills s sc).map toRefillA ≠ [] := by + rw [refills_consA hW]; simp + +theorem rf0_eqA {s : S} {sc : obj.Sc} {sb : Nat} (hW : RefillsWF st s sc sb) : + L.rf s sc 0 = L.refill s sc (L.rhead s sc) := by rw [LawfulStore.rf, hW.ix_zero] + +theorem refillHd_eqA {s : S} {sc : obj.Sc} {sb : Nat} (hW : RefillsWF st s sc sb) {x : Fl4SpecA64.SchedContext} + (hx : x.scRefills = (L.refills s sc).map toRefillA) : + Fl4SpecA64.refillHd x = toRefillA (L.refill s sc (L.rhead s sc)) := by + unfold Fl4SpecA64.refillHd; rw [hx, refills_consA hW, ← rf0_eqA hW]; rfl + +theorem refills_ofA {s : S} {sc : obj.Sc} {m : Nat} {g : Nat → sporadic.Refill} (hm : L.size s sc = m) + (hg : ∀ k < m, L.rf s sc k = g k) : L.refills s sc = (List.range m).map g := by + unfold LawfulStore.refills; rw [hm] + exact List.map_congr_left fun k hk => hg k (List.mem_range.1 hk) + +theorem refills_set_headA {s : S} {sc : obj.Sc} {sb : Nat} (hW : RefillsWF st s sc sb) (r : sporadic.Refill) : + (List.range (L.size s sc)).map (fun k => if k = 0 then r else L.rf s sc k) = r :: (L.refills s sc).tail := by + rw [refills_consA hW] + obtain ⟨m, hm⟩ : ∃ m, L.size s sc = m + 1 := ⟨L.size s sc - 1, by have := hW.size_pos; omega⟩ + rw [hm, List.range_succ_eq_map]; simp + +theorem refills_tail_eqA {s : S} {sc : obj.Sc} {sb : Nat} (hW : RefillsWF st s sc sb) : + L.refill s sc (L.rtail s sc) = (L.refills s sc).getLastD zRA := by + unfold LawfulStore.refills + obtain ⟨m, hm⟩ : ∃ m, L.size s sc = m + 1 := ⟨L.size s sc - 1, by have := hW.size_pos; omega⟩ + rw [hm, List.range_succ, List.map_append, List.map_singleton, List.getLastD_eq_getLast?, + List.getLast?_append, List.getLast?_singleton] + simp only [Option.some_or] + rw [← hW.ix_last, hm, Nat.add_sub_cancel]; rfl + +theorem head_eqA {s : S} {sc : obj.Sc} {sb : Nat} (hW : RefillsWF st s sc sb) : + L.refill s sc (L.rhead s sc) = (L.refills s sc).headD zRA := by + rw [refills_consA hW, ← rf0_eqA hW]; rfl + +theorem refills_set_tailA {s : S} {sc : obj.Sc} {sb : Nat} (hW : RefillsWF st s sc sb) (r : sporadic.Refill) : + L.refills (L.putS s sc (L.rtail s sc) r) sc = (L.refills s sc).dropLast ++ [r] := by + rw [refills_ofA (hW.size_putS hW.tail_lt) (fun k hk => hW.rf_set_tail (by have := hW.size_le; omega))] + unfold LawfulStore.refills + obtain ⟨m, hm⟩ : ∃ m, L.size s sc = m + 1 := ⟨L.size s sc - 1, by have := hW.size_pos; omega⟩ + rw [hm, List.range_succ, List.map_append, List.map_append, List.map_singleton, List.map_singleton, + List.dropLast_concat, Nat.add_sub_cancel, if_pos rfl] + congr 1 + exact List.map_congr_left fun k hk => by rw [if_neg (by have := List.mem_range.1 hk; omega)] + +theorem refills_popA {s s' : S} {sc : obj.Sc} {sb : Nat} (hW : RefillsWF st s sc sb) + (hne : L.rhead s sc ≠ L.rtail s sc) + (hs' : s' = L.wr s (headA sc) (nextIdx (L.rmax s sc) (L.rhead s sc))) : + L.refills s' sc = (L.refills s sc).tail := by + obtain ⟨hn, hrf, -, -⟩ := hW.pop hne hs' + rw [refills_ofA hn (fun k _ => hrf k)] + unfold LawfulStore.refills + obtain ⟨m, hm⟩ : ∃ m, L.size s sc = m + 1 := ⟨L.size s sc - 1, by have := hW.size_pos; omega⟩ + rw [hm, Nat.add_sub_cancel, List.range_succ_eq_map] + simp [Function.comp] + +end + +/-! ## `refill_reset_rr` on the list (x86 `ScCharge2`) -/ + +/-- `refill_reset_rr` on the list: the tail's amount moves to the head (x86 `rrResetL`). -/ +def rrResetLA (rs : List sporadic.Refill) : List sporadic.Refill := + ((⟨(rs.headD zRA).time, core.num.U64.wrapping_add (rs.headD zRA).amount (rs.getLastD zRA).amount⟩ : + sporadic.Refill) :: rs.tail).dropLast ++ [⟨(rs.getLastD zRA).time, 0#u64⟩] + +theorem the_getLast_mapA {rs : List sporadic.Refill} (hne : rs ≠ []) : + Fl4SpecA64.the (rs.map toRefillA).getLast? = toRefillA (rs.getLastD zRA) := by + rw [List.getLast?_map, List.getLast?_eq_getLast hne] + simp [List.getLastD_eq_getLast?, List.getLast?_eq_getLast hne]; rfl + +theorem the_getLast_cons_mapA (X : Fl4SpecA64.Refill) {l : List sporadic.Refill} (hl : l ≠ []) : + Fl4SpecA64.the (X :: l.map toRefillA).getLast? = toRefillA (l.getLastD zRA) := by + obtain ⟨b, l', rfl⟩ := List.exists_cons_of_ne_nil hl + rw [List.map_cons, List.getLast?_cons_cons, ← List.map_cons] + exact the_getLast_mapA (by simp) + +theorem refillHd_mapA {x : Fl4SpecA64.SchedContext} {rs : List sporadic.Refill} + (hx : x.scRefills = rs.map toRefillA) (hne : rs ≠ []) : Fl4SpecA64.refillHd x = toRefillA (rs.headD zRA) := by + unfold Fl4SpecA64.refillHd; rw [hx] + obtain ⟨a, l, rfl⟩ := List.exists_cons_of_ne_nil hne; rfl + +theorem the_someA {α : Type} [Nonempty α] (x : α) : Fl4SpecA64.the (some x) = x := rfl + +/-- **`refill_reset_rr` on a related SC** (x86 `refillResetRr_eq`). -/ +theorem refillResetRr_eqA {σ : Fl4SpecA64.State Unit} {p : Fl4SpecA64.ObjRef} {x : Fl4SpecA64.SchedContext} + {n : Nat} (hk : σ.kheap p = some (.schedContext x n)) {rs : List sporadic.Refill} + (hx : x.scRefills = rs.map toRefillA) (hne : rs ≠ []) : + Fl4SpecA64.refillResetRr (Z := Unit) p σ = + ({((), updKSc σ p (.schedContext { x with scRefills := (rrResetLA rs).map toRefillA } n))}, False) := by + have hne' : x.scRefills ≠ [] := by rw [hx]; simpa using hne + have hd : Fl4SpecA64.refillHd x = toRefillA (rs.headD zRA) := refillHd_mapA hx hne + have tl : Fl4SpecA64.refillTl x = toRefillA (rs.getLastD zRA) := by + unfold Fl4SpecA64.refillTl; rw [hx]; exact the_getLast_mapA hne + unfold Fl4SpecA64.refillResetRr + rw [nd_bind_oneSc (getRefillHead_eqA hk hne'), nd_bind_oneSc (getRefillTail_eqA hk hne'), hd, tl, + nd_bind_oneSc (updateRefillHd_eqA hk _)] + rw [updateRefillTl_eqA (updKSc_same _ _ _), updKSc_updKSc] + congr 4 + obtain ⟨a, l, rfl⟩ := List.exists_cons_of_ne_nil hne + rw [hx] + cases l with + | nil => simp [rrResetLA, Fl4SpecA64.the, toRefillA, zRA, core.num.U64.wrapping_add_bv_eq] + | cons b l => + simp only [List.map_cons, List.head?_cons, the_someA, List.tail_cons] + rw [← List.map_cons, the_getLast_cons_mapA _ (by simp)] + simp [rrResetLA, toRefillA, core.num.U64.wrapping_add_bv_eq, List.dropLast_cons_of_ne_nil] + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScSporA.lean b/proofs/Fl4ProofsA64/P3/ScSporA.lean new file mode 100644 index 000000000..dadc9bac8 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScSporA.lean @@ -0,0 +1,401 @@ +/- +P3 (aarch64, A64-P3-MCS gen 5): **`refill_unblock_check` on `SchedSRA`** (ruling 23:14Z gen-5 item 1) and the +spec-side facts it needs (x86 `P3/ScInvU`, ported). + +* **`RefOKA.merge`**, **`merge_loop_resultA`**, **`unblock_resultA`** (x86 `RefOK.merge`, `merge_loop_result`, + `unblock_result`): an outcome of `refill_unblock_check` is the state itself, or the SC replaced by one whose refills + are `RefOKA` (if they were) with the head ready now; the merge keeps the sum (no wrap below `2 ^ 64`). +* **`unblock_invA`** (x86 `Inv.unblock`, its `rd` / `refills` / `rr` / `ntfnSc` parts; gen 6: `rel` / `sorted` / + `back` too, with x86's `hnr`: when the head is ready, no release-queue member is bound to the SC): `refill_unblock_check` of an + active SC keeps `ScInvA` and `RdA` (no case hypothesis: `SchedSRA` carries `RefOKA` / `RRokA`). +* **`SchedSRA.keepW`**: what a write inside the SC footprint and one node word of the current core keeps. +* **`SchedSRA.unblock_checkA`**: FL4's `unblock_check` of an active related SC has the single outcome of + `refill_unblock_check`, and `SchedSRA` holds after (gen 4's `unblock_check_refinesA` lifted), with the frame + **`UnbFrA`** (FL4 writes in the SC footprint and the `REPROGRAM` word; the spec keeps every non-SC object, the + action and the queues; gen 10: and the SC sizes, `SSzA`). + +No named premise. Hypotheses: `hc` / `hk` / `ha` (the SC is a related active SC). +-/ +import Fl4ProofsA64.P3.ScUnblockA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## The spec side (x86 `ScInvU`) -/ + +section Spec +open Fl4SpecA64 + +theorem sufficient_of_refOKA {y : SchedContext} (h : RefOKA y) : refillSufficient 0 (refillHd y) = true := by + unfold refillSufficient refillCapacity + have h0 : ¬ (refillHd y).rAmount < 0 := by rw [BitVec.lt_def]; simp + simp only [h0, ↓reduceIte, decide_eq_true_eq] + have e : (refillHd y).rAmount - 0 = (refillHd y).rAmount := by bv_omega + rw [e]; exact h.2.2.2 + +theorem ready_nowA (σ : State Unit) (r : Refill) (h : r.rTime = σ.curTime) : refillReady σ.curTime r = true := by + show refillReady' 0 σ.curTime r = true + unfold refillReady'; rw [h]; simp + +theorem sum_consA (r : Refill) (l : List Refill) : + ((r :: l).map fun r => r.rAmount.toNat).sum = r.rAmount.toNat + (l.map fun r => r.rAmount.toNat).sum := by simp + +/-- The refills after `merge_overlapping_refills` (x86 `mergeHd`). -/ +def mergeHdA (x : SchedContext) : List Refill := + mergeRefill (refillHd x) (the x.scRefills.tail.head?) :: x.scRefills.tail.tail + +/-- `merge_overlapping_refills` on the spec's list (x86 `merge_eq`). -/ +theorem merge_eqSA {σ : State Unit} {p : ObjRef} {x : SchedContext} {n : Nat} + (hk : σ.kheap p = some (.schedContext x n)) (hne : x.scRefills ≠ []) : + mergeOverlappingRefills (Z := Unit) p σ = + ({((), updKSc σ p (.schedContext { x with scRefills := mergeHdA x } n))}, False) := by + unfold mergeOverlappingRefills + rw [nd_bind_oneSc (refillPopHead_eqA hk hne), updateRefillHd_eqA (updKSc_same _ _ _), updKSc_updKSc] + rfl + +/-- The merge of the two head refills keeps `RefOKA` and the head's time (x86 `RefOK.merge`). -/ +theorem RefOKA.merge {y : SchedContext} (h : RefOKA y) (h2 : y.scRefills.length > 1) : + RefOKA { y with scRefills := mergeHdA y } ∧ + (refillHd { y with scRefills := mergeHdA y }).rTime = (refillHd y).rTime := by + obtain ⟨-, hpos, hsum, hmin⟩ := h + obtain ⟨a, b, l, e⟩ : ∃ a b l, y.scRefills = a :: b :: l := by + match h : y.scRefills, h2 with + | a :: b :: l, _ => exact ⟨a, b, l, rfl⟩ + have hd : refillHd y = a := by unfold refillHd; rw [e]; rfl + have hm : mergeHdA y = mergeRefill a b :: l := by unfold mergeHdA; rw [hd, e]; rfl + rw [e] at hpos hsum + simp only [sum_consA] at hsum + have hno : (b.rAmount + a.rAmount).toNat = b.rAmount.toNat + a.rAmount.toNat := by + rw [BitVec.toNat_add]; exact Nat.mod_eq_of_lt (by omega) + have hpa := hpos a (by simp) + have hpb := hpos b (by simp) + have hhd : refillHd { y with scRefills := mergeHdA y } = mergeRefill a b := by + unfold refillHd; simp only [hm]; rfl + refine ⟨⟨by simp [hm], fun r hr => ?_, ?_, ?_⟩, by rw [hhd, hd]; rfl⟩ + · simp only [hm, List.mem_cons] at hr + rcases hr with rfl | hr + · show 0 < (b.rAmount + a.rAmount).toNat; omega + · exact hpos r (by simp [hr]) + · simp only [hm, sum_consA] + show (b.rAmount + a.rAmount).toNat + _ < _; omega + · rw [hhd]; rw [hd] at hmin + show MIN_BUDGET ≤ b.rAmount + a.rAmount + rw [BitVec.le_def] at hmin ⊢; rw [hno]; omega + +theorem mergeHdA_time {y : SchedContext} (h2 : y.scRefills.length > 1) : + (refillHd { y with scRefills := mergeHdA y }).rTime = (refillHd y).rTime := by + obtain ⟨a, b, l, e⟩ : ∃ a b l, y.scRefills = a :: b :: l := by + match h : y.scRefills, h2 with + | a :: b :: l, _ => exact ⟨a, b, l, rfl⟩ + have hd : refillHd y = a := by unfold refillHd; rw [e]; rfl + unfold mergeHdA; rw [hd, e]; rfl + +/-- What `refill_head_overlapping_loop` leaves (x86 `merge_loop_result`). -/ +theorem merge_loop_resultA {σa : State Unit} {p : ObjRef} {y : SchedContext} {n : Nat} + (hk : σa.kheap p = some (.schedContext y n)) {r : Unit} {σ' : State Unit} + (hres : Fl4Spec.NonDet.WhileResults (fun _ s => the (refillHeadOverlapping (Z := Unit) p s) = true) + (fun _ => mergeOverlappingRefills (Z := Unit) p) () σa (r, σ')) : + ∃ z, σ' = updKSc σa p (.schedContext z n) ∧ (RefOKA y → RefOKA z) ∧ (refillHd z).rTime = (refillHd y).rTime ∧ + z = { y with scRefills := z.scRefills } ∧ (y.scRefills ≠ [] → z.scRefills ≠ []) := by + suffices key : ∀ (u : Unit) (σ1 : State Unit) (res : Unit × State Unit), + Fl4Spec.NonDet.WhileResults (fun _ s => the (refillHeadOverlapping (Z := Unit) p s) = true) + (fun _ => mergeOverlappingRefills (Z := Unit) p) u σ1 res → + ∀ z, σ1 = updKSc σa p (.schedContext z n) → (RefOKA y → RefOKA z) → + (refillHd z).rTime = (refillHd y).rTime → z = { y with scRefills := z.scRefills } → + (y.scRefills ≠ [] → z.scRefills ≠ []) → + ∃ z', res.2 = updKSc σa p (.schedContext z' n) ∧ (RefOKA y → RefOKA z') ∧ + (refillHd z').rTime = (refillHd y).rTime ∧ z' = { y with scRefills := z'.scRefills } ∧ + (y.scRefills ≠ [] → z'.scRefills ≠ []) by + have h0 : σa = updKSc σa p (.schedContext y n) := (updKSc_selfA hk).symm + exact key () σa (r, σ') hres y h0 id rfl rfl id + intro u σ1 res hw + induction hw with + | done _ => intro z h1 h2 h3 h4 h5; exact ⟨z, h1, h2, h3, h4, h5⟩ + | step hc hm _ ih => + intro z h1 h2 h3 h4 _ + subst h1 + have hkz : (updKSc σa p (.schedContext z n)).kheap p = some (.schedContext z n) := updKSc_same _ _ _ + have hlen : z.scRefills.length > 1 := by + have := hc + rw [refillHeadOverlapping_eqA hkz, the_someA, decide_eq_true_iff] at this + exact this.1 + rw [merge_eqSA hkz (by intro e; rw [e] at hlen; simp at hlen), Set.mem_singleton_iff] at hm + cases hm + refine ih { z with scRefills := mergeHdA z } (updKSc_updKSc _ _ _ _) + (fun hy => ((h2 hy).merge hlen).1) ((mergeHdA_time hlen).trans h3) ?_ (fun _ => by simp [mergeHdA]) + rw [h4] + +/-- **What `refill_unblock_check` leaves** (x86 `unblock_result`). -/ +theorem unblock_resultA {σ : State Unit} {p : ObjRef} {y : SchedContext} {n : Nat} + (hk : σ.kheap p = some (.schedContext y n)) (hne : y.scRefills ≠ []) {r : Unit} {σ' : State Unit} + (hm : (r, σ') ∈ (refillUnblockCheck (Z := Unit) p σ).1) : + σ' = σ ∨ + ∃ z, σ' = updKSc { σ with reprogramTimer := true } p (.schedContext z n) ∧ (RefOKA y → RefOKA z) ∧ + (refillHd z).rTime = σ.curTime ∧ z = { y with scRefills := z.scRefills } ∧ y.scPeriod ≠ 0 ∧ + refillReady σ.curTime (refillHd y) = true := by + unfold refillUnblockCheck at hm + rw [nd_bind_oneSc (isRoundRobin_eqA hk), nd_bind_oneSc (getScRefillReady_eqA hk hne)] at hm + simp only [Fl4Spec.NonDet.when] at hm + split_ifs at hm with hc + · rw [nd_gets_bindSc, nd_bind_oneSc (updateRefillHd_eqA hk _)] at hm + rw [nd_bind_oneSc (show Fl4Spec.NonDet.modify (fun s : State Unit => { s with reprogramTimer := true }) + (updKSc σ p (.schedContext + { y with scRefills := { the y.scRefills.head? with rTime := σ.curTime } :: y.scRefills.tail } n)) = + ({((), updKSc { σ with reprogramTimer := true } p (.schedContext + { y with scRefills := { the y.scRefills.head? with rTime := σ.curTime } :: y.scRefills.tail } n))}, False) + from rfl)] at hm + set y1 : SchedContext := + { y with scRefills := { the y.scRefills.head? with rTime := σ.curTime } :: y.scRefills.tail } + have hk1 : (updKSc { σ with reprogramTimer := true } p (.schedContext y1 n)).kheap p = + some (.schedContext y1 n) := updKSc_same _ _ _ + have hR1 : RefOKA y → RefOKA y1 := by + intro hR + obtain ⟨-, hpos, hsum, hmin⟩ := hR + obtain ⟨a, l, e⟩ := List.exists_cons_of_ne_nil hne + have hd : refillHd y = a := by unfold refillHd; rw [e]; rfl + have hy1 : y1.scRefills = { a with rTime := σ.curTime } :: l := by + show { the y.scRefills.head? with rTime := σ.curTime } :: y.scRefills.tail = _ + rw [e]; rfl + rw [e] at hpos hsum + refine ⟨by rw [hy1]; simp, fun q hq => ?_, ?_, ?_⟩ + · rw [hy1, List.mem_cons] at hq + rcases hq with rfl | hq + · exact hpos a (by simp) + · exact hpos q (by simp [hq]) + · rw [hy1]; simpa using hsum + · have : refillHd y1 = { a with rTime := σ.curTime } := by unfold refillHd; rw [hy1]; rfl + rw [this]; rw [hd] at hmin; exact hmin + have ht1 : (refillHd y1).rTime = σ.curTime := by + show (the ({ the y.scRefills.head? with rTime := σ.curTime } :: y.scRefills.tail).head?).rTime = _ + rfl + obtain ⟨z, h1, h2, h3, h4, -⟩ := merge_loop_resultA hk1 hm + refine Or.inr ⟨z, h1.trans (updKSc_updKSc _ _ _ _), fun hy => h2 (hR1 hy), h3.trans ht1, ?_, + by simpa using hc.2, by simpa using hc.1⟩ + rw [h4] + · have hm' : (r, σ') ∈ ({((), σ)} : Set (Unit × State Unit)) := hm + rw [Set.mem_singleton_iff] at hm' + cases hm' + exact Or.inl rfl + +/-- **`refill_unblock_check` of an active SC keeps `ScInvA` and `RdA`** (x86 `Inv.unblock`'s `rd`, `refills`, +`rr`, `ntfnSc` parts; with `SchedSRA`'s `RefOKA` the merge cannot wrap, ruling 23:14Z item 3). -/ +theorem unblock_invA {m : McState} {σ σ' : State Unit} (hI : ScInvA m σ) (hR : RdA σ) {p : ObjRef} + {y : SchedContext} {n : Nat} (hk : σ.kheap p = some (.schedContext y n)) (ha : scActive y = true) + (hnr : refillReady σ.curTime (refillHd y) = true → y.scPeriod ≠ 0 → + ∀ u ∈ σ.releaseQueue, ∀ x, σ.kheap u = some (.tcb x) → x.tcbSchedContext ≠ some p) {r : Unit} + (hm : (r, σ') ∈ (refillUnblockCheck (Z := Unit) p σ).1) : ScInvA m σ' ∧ RdA σ' := by + have hne : y.scRefills ≠ [] := hI.ne hk ha + rcases unblock_resultA hk hne hm with rfl | ⟨z, rfl, hzR, hzt, hzy, hper, hrdy⟩ + · exact ⟨hI, hR⟩ + have hkp : ∀ q, q ≠ p → (updKSc { σ with reprogramTimer := true } p (.schedContext z n)).kheap q = σ.kheap q := + fun q hq => by simp [updKSc, hq] + have hkz : (updKSc { σ with reprogramTimer := true } p (.schedContext z n)).kheap p = some (.schedContext z n) := + updKSc_same _ _ _ + have hzact : scActive z = scActive y := by rw [hzy] + have hzper : z.scPeriod = y.scPeriod := by rw [hzy] + have hzOK : RefOKA z := hzR (hI.refills p y n hk ha hper) + have back : ∀ q o, (updKSc { σ with reprogramTimer := true } p (.schedContext z n)).kheap q = some o → + (∀ w k, o ≠ .schedContext w k) → σ.kheap q = some o := fun q o hq ho => by + by_cases e : q = p + · subst e; rw [hkz] at hq; cases hq; exact absurd rfl (ho z n) + · rw [hkp q e] at hq; exact hq + have hnr' := hnr hrdy hper + have hRQ := hI.relKeep (σ' := updKSc { σ with reprogramTimer := true } p (.schedContext z n)) rfl + (fun u _ x hx => ⟨x, by rw [hkp u (fun e => by rw [e, hk] at hx; cases hx)]; exact hx, rfl⟩) + (fun u hu x q hx hq => hkp q (fun e => hnr' u hu x hx (e ▸ hq))) + refine ⟨⟨fun q w k hw => ?_, fun q w k hw => ?_, fun q x hx => ?_, fun q x hx => ?_, hRQ.1, hRQ.2, + fun u x q hx hq => ?_, fun u x q hx hq => hI.tcbScNotIdle u x q (back u _ hx (fun _ _ h => by cases h)) hq⟩, + fun u x hx hc => ?_⟩ + · by_cases e : q = p + · subst e; rw [hkz] at hw; cases hw; intro _ _; exact hzOK + · rw [hkp q e] at hw; exact hI.refills q w k hw + · by_cases e : q = p + · subst e; rw [hkz] at hw; cases hw; intro _ hp0; exact absurd (hzper.symm.trans hp0) hper + · rw [hkp q e] at hw; exact hI.rr q w k hw + · exact hI.ntfnIdle q x (back q _ hx (fun _ _ h => by cases h)) + · exact hI.ntfnIdleOf q x (back q _ hx (fun _ _ h => by cases h)) + · have hx0 := back u _ hx (fun _ _ h => by cases h) + obtain ⟨w, k, hw, hb⟩ := hI.back u x q hx0 hq + by_cases e : q = p + · subst e; rw [hk] at hw; cases hw + exact ⟨z, n, hkz, by rw [hzy]; exact hb⟩ + · exact ⟨w, k, by rw [hkp q e]; exact hw, hb⟩ + · have hx0 := back u _ hx (fun _ _ h => by cases h) + intro x' scp w k h1 h2 h3 h4 h5 + rw [hx] at h1; cases h1 + by_cases e : scp = p + · subst e; rw [hkz] at h3; cases h3 + exact ⟨ready_nowA _ _ hzt, sufficient_of_refOKA hzOK⟩ + · rw [hkp scp e] at h3 + exact hR u x hx0 hc x scp w k hx0 h2 h3 h4 h5 + +end Spec + +/-! ## The FL4 side -/ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **What a write inside the SC footprint and one node word of the current core keeps** (the node words of every +core but that one, the kernel-state block below the nodes, every word of a TCB of `Z.T`). -/ +theorem SchedSRN.keepNodeWordsA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s s' : S} {σ : Fl4SpecA64.State Unit} + (H : SchedSRN st Z m κ s σ) {W : U64 → Prop} (hw : WritesIn st s s' W) {Offsets : U64 → Prop} + (hoff : ∀ o, Offsets o → o.val < 4288) + (hW : ∀ b, W b → ScFootA Z.C m σ b ∨ ∃ o, Offsets o ∧ b = L.nodeAt s (L.cpu s) o) : + (∀ c ∈ Z.Cs, ∀ o : U64, o.val < 4288 → o.val % 8 = 0 → (∀ k, Offsets k → o.val ≠ k.val) → + L.mem s' (L.nodeAt s c o) = L.mem s (L.nodeAt s c o)) ∧ + (∀ K : U64, K.val < 223488 → K.val % 8 = 0 → L.mem s' (wa (L.ksb s) K) = L.mem s (wa (L.ksb s) K)) ∧ + (∀ t ∈ Z.T, ∀ K : U64, K.val + 8 ≤ 4096 → K.val % 8 = 0 → L.mem s' (wa t K) = L.mem s (wa t K)) := by + obtain ⟨nn, -, hn, hcs⟩ := H.csN + have hcl : ∀ c ∈ Z.Cs, c.val < 128 := fun c hc => by have := (hcs c).1 hc; omega + have hf := H.ksFit + have hkb := H.ksb_al + have hcp := hcl _ H.sch.cpuCs + have hA (o : U64) (ho : Offsets o) : + (L.nodeAt s (L.cpu s) o).val = (L.ksb s).val + 223488 + (L.cpu s).val * 4288 + o.val := + nodeAt_valSc hf hcp (hoff o ho) + have keep : ∀ b, Aligned b → ¬ ScFootA Z.C m σ b → (∀ o, Offsets o → b.val ≠ (L.nodeAt s (L.cpu s) o).val) → + L.mem s' b = L.mem s b := + fun b hb h1 h2 => hw.mem_eq hb fun hwb => (hW b hwb).elim h1 (fun ⟨o, ho, e⟩ => h2 o ho (by rw [e])) + have ksOff : ∀ b : U64, (L.ksb s).val ≤ b.val → b.val < (L.ksb s).val + ksSizeA → ¬ ScFootA Z.C m σ b := + fun b h1 h2 hfb => by have := H.scp.ks_out hfb; omega + unfold ksSizeA at hf ksOff + refine ⟨fun c hc o ho h8 hne => ?_, fun K hK h8 => ?_, fun t ht K hK h8 => ?_⟩ + · have hv := nodeAt_valSc (s := s) H.ksFit (hcl c hc) ho + have hc' := hcl c hc + exact keep _ (by unfold Aligned; rw [hv]; omega) (ksOff _ (by rw [hv]; omega) (by rw [hv]; omega)) + (by + intro k hk + have := hne k hk + have := hoff k hk + rw [hv, hA k hk] + omega) + · have hv : (wa (L.ksb s) K).val = (L.ksb s).val + K.val := by rw [wa_valm]; omega + exact keep _ (by unfold Aligned; rw [hv]; omega) (ksOff _ (by rw [hv]; omega) (by rw [hv]; omega)) + (by intro k hk; rw [hv, hA k hk]; omega) + · have hT := H.core.tcbs t ht + have hfit := hT.fits + have hal := hT.align + have hv : (wa t K).val = t.val + K.val := by rw [wa_valm]; omega + have hks := H.tcb_ks ht + unfold ksSizeA at hks + refine keep _ (by unfold Aligned; rw [hv]; omega) (fun hfb => ?_) (by intro k hk; have hklt := hoff k hk; rw [hv, hA k hk]; omega) + have := H.scp.th_out hfb ((H.thT t).2 ht) + simp only [ObjKindA.size] at this + omega + +theorem SchedSRN.keepW {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s s' : S} {σ : Fl4SpecA64.State Unit} + (H : SchedSRN st Z m κ s σ) {W : U64 → Prop} (hw : WritesIn st s s' W) {o0 : U64} (ho0 : o0.val < 4288) + (hW : ∀ b, W b → ScFootA Z.C m σ b ∨ b = L.nodeAt s (L.cpu s) o0) : + (∀ c ∈ Z.Cs, ∀ o : U64, o.val < 4288 → o.val % 8 = 0 → o.val ≠ o0.val → + L.mem s' (L.nodeAt s c o) = L.mem s (L.nodeAt s c o)) ∧ + (∀ K : U64, K.val < 223488 → K.val % 8 = 0 → L.mem s' (wa (L.ksb s) K) = L.mem s (wa (L.ksb s) K)) ∧ + (∀ t ∈ Z.T, ∀ K : U64, K.val + 8 ≤ 4096 → K.val % 8 = 0 → L.mem s' (wa t K) = L.mem s (wa t K)) := by + obtain ⟨hn, hg, ht⟩ := H.keepNodeWordsA (Offsets := fun o => o = o0) hw + (fun o e => e.symm ▸ ho0) (fun b hb => (hW b hb).imp id fun e => ⟨o0, rfl, e⟩) + exact ⟨fun c hc o ho h8 he => hn c hc o ho h8 (fun k ek => by rw [ek]; exact he), hg, ht⟩ + +/-- **The frame of the sporadic unblock** (gen 5): FL4 writes in the SC footprint and the current core's `REPROGRAM` +word; the spec keeps every object but the SCs, the action, the queues and the clock. -/ +structure UnbFrA (Z : XSetsA) (m : Fl4SpecA64.McState) (s s' : S) (σ σ' : Fl4SpecA64.State Unit) : Prop where + wr : WritesIn st s s' (fun b => ScFootA Z.C m σ b ∨ b = L.nodeAt s (L.cpu s) layout.node.REPROGRAM) + obj : ∀ q o, σ.kheap q = some o → (∀ y n, o ≠ .schedContext y n) → σ'.kheap q = some o + act : σ'.schedulerAction = σ.schedulerAction + rq : σ'.readyQueues = σ.readyQueues + rel : σ'.releaseQueue = σ.releaseQueue + time : σ'.curTime = σ.curTime + cur : σ'.curThread = σ.curThread + /-- (gen 10) The SC objects keep their sizes (`SSzA`, for `MFrA`). -/ + sz : SSzA σ σ' + +theorem UnbFrA.refl (Z : XSetsA) (m : Fl4SpecA64.McState) (s : S) (σ : Fl4SpecA64.State Unit) : + UnbFrA (st := st) Z m s s σ σ := + ⟨WritesIn.refl _ _, fun _ _ h _ => h, rfl, rfl, rfl, rfl, rfl, SSzA.refl σ⟩ + +/-- The current core's `REPROGRAM` word is aligned and off the SC footprint. -/ +theorem SchedSRN.reprA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} {σ : Fl4SpecA64.State Unit} + (H : SchedSRN st Z m κ s σ) : + Aligned (L.nodeAt s (L.cpu s) layout.node.REPROGRAM) ∧ + ∀ a, ScFootA Z.C m σ a → a.val ≠ (L.nodeAt s (L.cpu s) layout.node.REPROGRAM).val := by + obtain ⟨nn, -, hn, hcs⟩ := H.csN + have hcp : (L.cpu s).val < 128 := by have := (hcs _).1 H.sch.cpuCs; omega + have hf := H.ksFit + have hkb := H.ksb_al + have hr : layout.node.REPROGRAM = 56#u64 := by unfold layout.node.REPROGRAM; rfl + have hv : (L.nodeAt s (L.cpu s) layout.node.REPROGRAM).val = (L.ksb s).val + 223488 + (L.cpu s).val * 4288 + 56 := by + rw [nodeAt_valSc (o := layout.node.REPROGRAM) hf hcp (by rw [hr]; simp), hr]; simp + unfold ksSizeA at hf + refine ⟨by unfold Aligned; rw [hv]; omega, fun a ha => ?_⟩ + have := H.scp.ks_out ha + unfold ksSizeA at this + rw [hv]; omega + +/-- **`unblock_check` on `SchedSRA`** (ruling 23:14Z gen-5 item 1; gen 4's `unblock_check_refinesA` lifted): the +single outcome of `refill_unblock_check`, `SchedSRA` after, the frame `UnbFrA`. -/ +theorem SchedSRA.unblock_checkA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} {σ : Fl4SpecA64.State Unit} + (H : SchedSRA st Z m κ s σ) {sc : U64} (hc : sc ∈ Z.C) {x : Fl4SpecA64.SchedContext} {n : Nat} + (hk : σ.kheap (scNameA m sc) = some (.schedContext x n)) (ha : Fl4SpecA64.scActive x = true) + (hnr : Fl4SpecA64.refillReady σ.curTime (Fl4SpecA64.refillHd x) = true → x.scPeriod ≠ 0 → + ∀ u ∈ σ.releaseQueue, ∀ y, σ.kheap u = some (.tcb y) → y.tcbSchedContext ≠ some (scNameA m sc)) : + sporadic.unblock_check st s sc ⦃ s' => ∃ σ', Fl4SpecA64.refillUnblockCheck (Z := Unit) (scNameA m sc) σ = + ({((), σ')}, False) ∧ SchedSRA st Z m κ s' σ' ∧ UnbFrA (st := st) Z m s s' σ σ' ⦄ := by + obtain ⟨hal, hout⟩ := H.reprA + refine WP.spec_mono (unblock_check_refinesA H.scr H.scp hc hk ha H.sch.time hal hout) ?_ + rintro s' ⟨σ', hsp, hS, hP, hw, hcc, ⟨e1, e2⟩ | ⟨y, -, rfl, hrep⟩⟩ + · rw [e1] at hsp; rw [e2]; exact ⟨σ, hsp, H, UnbFrA.refl Z m s σ⟩ + obtain ⟨hI', hR'⟩ := unblock_invA H.inv H.rd hk ha hnr (r := ()) (by rw [hsp]; exact Set.mem_singleton _) + have hw' : WritesIn st s s' (fun b => ScFootA Z.C m σ b ∨ b = L.nodeAt s (L.cpu s) layout.node.REPROGRAM) := + hw.mono fun b hb => hb.imp (fun h => ⟨sc, hc, x, n, hk, h⟩) id + have hr56 : layout.node.REPROGRAM.val = 56 := by unfold layout.node.REPROGRAM; rfl + obtain ⟨nK, gK, tK⟩ := H.keepW hw' (by rw [hr56]; omega) (fun b hb => hb) + have hne : ∀ q o, σ.kheap q = some o → (∀ y n, o ≠ .schedContext y n) → q ≠ scNameA m sc := + fun q o hq ho e => by rw [e, hk] at hq; cases hq; exact ho x n rfl + have hobj : ∀ q o, σ.kheap q = some o → (∀ y n, o ≠ .schedContext y n) → + (({ updKSc σ (scNameA m sc) (.schedContext y n) with reprogramTimer := true } : + Fl4SpecA64.State Unit)).kheap q = some o := + fun q o hq ho => by simp [updKSc, hne q o hq ho, hq] + have tw : ∀ t ∈ Z.T, ∀ K : U64, (K = 3520#u64 ∨ K = 3448#u64 ∨ K = 3432#u64 ∨ K = 3464#u64 ∨ K = 3592#u64 ∨ + K = 3480#u64 ∨ K = 3488#u64) → L.mem s' (wa t K) = L.mem s (wa t K) := fun t ht K hK => + tK t ht K (by rcases hK with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> simp) + (by rcases hK with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> simp) + have hcore : SchedCoreA st Z.T Z.Cs s' Z.Q Z.R := + H.core.agree hw.1 (fun u hu => ⟨tw u hu 3480#u64 (by simp), tw u hu 3488#u64 (by simp)⟩) + (fun c hc o h1 h2 h3 => nK c hc o h1 h2 (by rw [hr56]; omega)) (fun u hu => tw u hu 3592#u64 (by simp)) + (fun u hu => tw u hu 3520#u64 (by simp)) (fun u hu => tw u hu 3448#u64 (by simp)) + have hkt : ∀ q u, σ.kheap q = some (.tcb u) → + (({ updKSc σ (scNameA m sc) (.schedContext y n) with reprogramTimer := true } : + Fl4SpecA64.State Unit)).kheap q = some (.tcb u) := fun q u hq => hobj q _ hq (fun _ _ h => by cases h) + refine ⟨_, hsp, ?_, ⟨hw', hobj, rfl, rfl, rfl, rfl, rfl, SSzA.of_upd hk rfl⟩⟩ + refine H.rebuildS (Q' := Z.Q) hw.1 hw.2.1 (fun c hc o h1 h2 h3 h4 => nK c hc o (by omega) h2 (by rw [hr56]; exact h4)) + (fun K hK => gK K (by rcases hK with rfl | rfl <;> simp) (by rcases hK with rfl | rfl <;> simp)) + (fun t ht K hK => tw t ht K (by rcases hK with h | h | h | h <;> simp [h])) hS hP + (fun t ht u hu => ⟨u, hkt _ u hu, rfl, rfl, rfl⟩) ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ + (by show true = _; rw [hrep]; simp) + (by + show σ.schedulerAction = _ + rw [nK _ H.sch.cpuCs layout.node.SCHED_ACTION (by simp [layout.node.SCHED_ACTION]) + (by simp [layout.node.SCHED_ACTION]) (by rw [hr56]; simp [layout.node.SCHED_ACTION])] + exact H.sch.act) + H.actOn hR' hI' hcore (fun p hp => H.rest.rq p hp) H.rest.rqDom rfl + ⟨fun d p u hu => let ⟨w, h1, h2⟩ := H.run.1 d p u hu; ⟨w, hkt _ w h1, h2⟩, + fun u hu => let ⟨w, h1, h2⟩ := H.run.2 u hu; ⟨w, hkt _ w h1, h2⟩⟩ + (fun c' hc' => by + by_cases e : c' = sc + · rw [e]; exact hcc + obtain ⟨y', k, hky, hy⟩ := H.scr.sc c' hc' + have h16 := hy.hdr (K := 16#u64) (by simp) (by simp) + have hcE : scCoreA c' = scWA c' 16#u64 := by unfold scCoreA scWA layout.sc.CORE; rfl + refine hw.mem_eq (H.scCoreFoot hc').1 (fun hb => hb.elim (fun ho => ?_) (fun eb => ?_)) + · rw [hcE] at ho; exact H.scp.disj c' hc' sc hc e y' k x n hky hk _ (hy.in_obj h16.2) ho + · exact hout _ (H.scCoreFoot hc').2 (by rw [eb])) + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScStableA.lean b/proofs/Fl4ProofsA64/P3/ScStableA.lean new file mode 100644 index 000000000..41ffe8242 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScStableA.lean @@ -0,0 +1,266 @@ +/- +P3 (aarch64, A64-P3-MCS gen 3): **`SchedSRA`'s frames**: the `stable` / `notQueued` laws of the IPC line's +`SchedHypsA` / `SchedOpsEA` for `SR := SchedSRA` (x86 `P3/SchedRel`'s `SchedRel.stable` / `stableW`, `P3/IpcSchedInst`'s +`SchedRel.stableE`, with `ScInv.Inv.stable`'s side condition). + +* **`NoBlockESc`** — the IPC line's gen-5 `NoBlockE` (ruling 22:14Z, the agreed 3-branch body, verbatim), with its + ingredients `releasedSc` / `inClassRSc` / `RelIfBoundSc` (the IPC line's `released` / `inClassR` / `RelIfBound`, + `P3/SchedOpsA`, identical bodies). IPC gen 5 has not committed `NoBlockE` yet; these local copies (an `Sc*A` file, not + an IPC-named one) are to be unified with the IPC definitions at the join (`rfl`: same bodies). +* **`SStepA σ σ'`** — what a step of the IPC code may change of what `SchedSRA` reads of the spec: nothing of the + scheduler scalars, action, queues and SC objects; TCBs keep their SC, domain and priority (the IPC line's + `NtfnStep` / `EpStep` both imply it: they change TCBs only by `{ x with tcbState, tcbArch }` and keep SC objects). +* **`SReadA Z m s σ a`** — the FL4 words `SchedSRA` reads, but the thread-state words: every word of every node of + `Z.Cs`, the domain words, the TCBs' affinity / priority / domain / SC / `IN_QUEUES` / `Sched`-link words, the SC + objects. The IPC code's write sets (`NWrite`, `EWrite`) are outside it (they write state / block / badge / + registers / `Ep` links / notification and endpoint words). +* **`SchedSRA.stable`** — FL4 writes outside `SReadA`, an `SStepA` spec step that blocks no thread + (`NoBlockESc`), FL4's state words related to the spec's states after (the IPC relations' `TcbRel1.st` / + `TcbRelE.st` at the end state) ⇒ `SchedSRA` after. `NoBlockESc` keeps the spec queues runnable (`RunQA`): branch 3 + (`Inactive` from a non-runnable state) never hits a queued thread. +* (gen 8) **`SchedSRA.stableR`** (the body of `stable`, taking `RunQA` / `RdA` after) and **`SchedSRA.block`** (the + `block` law, x86 `SchedHyps.block`): the blocked thread is in no queue and has `RelIfBound`; every other TCB keeps + its state with `inClassR` reflected back (stronger than IPC's `block` frame, which lets faults change: handoff). +* **`SchedSRA.notQueued`** — the `notQueued` law: a TCB that is not runnable is in no spec queue. +* **`RdA`** / **`RdA.step`** / `RelIfBoundSc.step` (Architect 22:35Z, for the IPC line's gen-6 `SchedOpsEA.rd`; x86 + `ScInv.Inv.rd`, l4v `released_ipc_queues`): a TCB in `inClassR` has `RelIfBound`; it survives every `SStepA` step + that blocks no thread (this is what `NoBlockESc`'s branch conditions are for). `RdA` becomes a `SchedSRA` field in + gen 4 (`rd` is then its projection). + +No named premise. +-/ +import Fl4ProofsA64.P3.ScSchedA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## The spec side: `NoBlockE` (local copy) and the step -/ + +section SpecDefs +open Fl4SpecA64 Fl4Spec + +/- `releasedSc` / `inClassRSc` / `RelIfBoundSc` / `RdA` are in `ScSchedA` since gen 4 (`RdA` is a `SchedSRA` field). -/ + +/-- **The IPC line's gen-5 `NoBlockE`** (ruling 22:14Z, the agreed 3-branch body). -/ +def NoBlockESc (σ σ' : State Unit) : Prop := + ∀ p x y, σ.kheap p = some (.tcb x) → σ'.kheap p = some (.tcb y) → + (y.tcbState = x.tcbState ∧ (inClassRSc y.tcbState y.tcbFault → inClassRSc x.tcbState x.tcbFault)) ∨ + (runnable y.tcbState = true ∧ x.tcbState ≠ .idleThreadState ∧ + (inClassRSc x.tcbState x.tcbFault ∨ RelIfBoundSc σ p)) ∨ + (y.tcbState = .inactive ∧ runnable x.tcbState = false ∧ x.tcbState ≠ .idleThreadState) + +/-- A runnable TCB stays runnable across a step that blocks no thread. -/ +theorem NoBlockESc.run_keep {σ σ' : State Unit} (hb : NoBlockESc σ σ') {p : ObjRef} {x y : Tcb} + (h1 : σ.kheap p = some (.tcb x)) (h2 : σ'.kheap p = some (.tcb y)) (hr : runnable x.tcbState = true) : + runnable y.tcbState = true := by + rcases hb p x y h1 h2 with ⟨e, -⟩ | ⟨e, -⟩ | ⟨-, e, -⟩ + · rw [e]; exact hr + · exact e + · rw [hr] at e; cases e + +/-- **What an IPC step may change of what `SchedSRA` reads of the spec** (module docstring). -/ +structure SStepA (σ σ' : State Unit) : Prop where + fix : Fl4ProofsA64.P3.Sched.SFixA σ σ' + act : σ'.schedulerAction = σ.schedulerAction + rq : σ'.readyQueues = σ.readyQueues + rel : σ'.releaseQueue = σ.releaseQueue + sc : ∀ p x n, σ.kheap p = some (.schedContext x n) → σ'.kheap p = σ.kheap p + tcb : ∀ p x, σ.kheap p = some (.tcb x) → ∃ y, σ'.kheap p = some (.tcb y) ∧ + y.tcbSchedContext = x.tcbSchedContext ∧ y.tcbDomain = x.tcbDomain ∧ y.tcbPriority = x.tcbPriority + /-- No SC object appears (gen 3, for `rd`). -/ + scBack : ∀ p x n, σ'.kheap p = some (.schedContext x n) → σ.kheap p = σ'.kheap p + /-- No TCB appears (gen 3, for `rd`). -/ + tcbBack : ∀ p y, σ'.kheap p = some (.tcb y) → ∃ x, σ.kheap p = some (.tcb x) + /-- (gen 5, for `ScInvA`) No notification appears, and a notification keeps its SC (IPC's `NtfnStep` / `EpStep`). -/ + ntfnBack : ∀ p y, σ'.kheap p = some (.notification y) → ∃ x, σ.kheap p = some (.notification x) ∧ + y.ntfnSc = x.ntfnSc + +/-- The spec queues stay runnable across an `SStepA` step that blocks no thread. -/ +theorem RunQA.step {σ σ' : State Unit} (h : Fl4ProofsA64.P3.Sched.RunQA σ) (hS : SStepA σ σ') + (hb : NoBlockESc σ σ') : Fl4ProofsA64.P3.Sched.RunQA σ' := by + have k : ∀ t, (∃ x, σ.kheap t = some (.tcb x) ∧ runnable x.tcbState = true) → + ∃ y, σ'.kheap t = some (.tcb y) ∧ runnable y.tcbState = true := by + rintro t ⟨x, hx, hr⟩ + obtain ⟨y, hy, -⟩ := hS.tcb t x hx + exact ⟨y, hy, hb.run_keep hx hy hr⟩ + refine ⟨fun d p t ht => k t (h.1 d p t ?_), fun t ht => k t (h.2 t ?_)⟩ + · rw [← hS.rq]; exact ht + · rw [← hS.rel]; exact ht + +/-- `RelIfBound` of a TCB survives an `SStepA` step. -/ +theorem RelIfBoundSc.step {σ σ' : State Unit} {p : ObjRef} {x0 : Tcb} (h : RelIfBoundSc σ p) (hS : SStepA σ σ') + (hk : σ.kheap p = some (.tcb x0)) : RelIfBoundSc σ' p := by + intro x scp y n h1 h2 h3 h4 h5 + obtain ⟨y0, hy0, e1, -⟩ := hS.tcb p x0 hk + rw [hy0] at h1 + injection h1 with h1; injection h1 with h1 + subst h1 + have h3' : σ.kheap scp = some (.schedContext y n) := by rw [hS.scBack scp y n h3]; exact h3 + have := h x0 scp y n hk (by rw [← e1]; exact h2) h3' h4 (by rw [← hS.rel]; exact h5) + unfold releasedSc at this ⊢ + rw [hS.fix.curTime]; exact this + +/-- **`RdA` survives an `SStepA` step that blocks no thread** (`NoBlockESc`'s three branches: the state kept with +`inClassR` reflected back, a wake from `inClassR` or with a released SC, a fall to `Inactive`, outside `inClassR`). -/ +theorem RdA.step {σ σ' : State Unit} (h : RdA σ) (hS : SStepA σ σ') (hb : NoBlockESc σ σ') : RdA σ' := by + intro u y hy hcls + obtain ⟨x, hx⟩ := hS.tcbBack u y hy + rcases hb u x y hx hy with ⟨-, hc⟩ | ⟨-, -, hc | hc⟩ | ⟨e, -, -⟩ + · exact (h u x hx (hc hcls)).step hS hx + · exact (h u x hx hc).step hS hx + · exact hc.step hS hx + · exfalso + rcases hcls with hc | ⟨_, hc⟩ | ⟨_, _, hc, -⟩ | ⟨_, _, _, hc⟩ <;> rw [e] at hc <;> simp [runnable] at hc + +/-- (gen 5) `ScInvA` survives an `SStepA` step (SC objects kept and none appear; notifications keep their SC). -/ +theorem ScInvA.step {m : Fl4SpecA64.McState} {σ σ' : State Unit} (h : Fl4ProofsA64.P3.Sched.ScInvA m σ) + (hS : SStepA σ σ') : + Fl4ProofsA64.P3.Sched.ScInvA m σ' := by + have hR := h.relKeep hS.rel (fun u _ x hx => let ⟨y, h1, h2, _⟩ := hS.tcb u x hx; ⟨y, h1, h2⟩) + (fun _ _ x p hx hp => by + obtain ⟨y, n, hy, -⟩ := h.back _ x p hx hp + exact hS.sc p y n hy) + refine ⟨fun p y n hy => ?_, fun p y n hy => ?_, fun q x hx => ?_, fun q x hx => ?_, hR.1, hR.2, + fun u y p hy hp => ?_, fun u y p hy hp => ?_⟩ + rotate_right 2 + · obtain ⟨x, hx⟩ := hS.tcbBack u y hy + obtain ⟨y', h1, h2, -⟩ := hS.tcb u x hx + rw [hy] at h1; cases h1 + obtain ⟨w, n, hw, hb⟩ := h.back u x p hx (by rw [← h2]; exact hp) + exact ⟨w, n, by rw [hS.sc p w n hw]; exact hw, hb⟩ + · obtain ⟨x, hx⟩ := hS.tcbBack u y hy + obtain ⟨y', h1, h2, -⟩ := hS.tcb u x hx + rw [hy] at h1; cases h1 + exact h.tcbScNotIdle u x p hx (by rw [← h2]; exact hp) + · exact h.refills p y n ((hS.scBack p y n hy).trans hy) + · exact h.rr p y n ((hS.scBack p y n hy).trans hy) + · obtain ⟨x0, h0, e⟩ := hS.ntfnBack q x hx; rw [e]; exact h.ntfnIdle q x0 h0 + · obtain ⟨x0, h0, e⟩ := hS.ntfnBack q x hx; rw [e]; exact h.ntfnIdleOf q x0 h0 + +end SpecDefs + +/-! ## The FL4 side -/ + +section +variable {S : Type} (st : machine.Store S) [L : LawfulStore st] + +/-- **The FL4 words `SchedSRA` reads, but the thread-state words** (module docstring). -/ +def SReadA (Z : XSetsA) (m : Fl4SpecA64.McState) (s : S) (σ : Fl4SpecA64.State Unit) (a : U64) : Prop := + (∃ c ∈ Z.Cs, ∃ o : U64, o.val < 4288 ∧ a = L.nodeAt s c o) ∨ a = wa (L.ksb s) 8#u64 ∨ + a = wa (L.ksb s) 16#u64 ∨ + (∃ t ∈ Z.T, ∃ K : U64, (K = 3520#u64 ∨ K = 3448#u64 ∨ K = 3432#u64 ∨ K = 3464#u64 ∨ K = 3592#u64 ∨ + K = 3480#u64 ∨ K = 3488#u64) ∧ a = wa t K) ∨ + ScFootA Z.C m σ a + +end + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- (gen 8) **`SchedSRA` across an `SStepA` step given the spec queues' runnability and `RdA` after** (the body of +`SchedSRA.stable`, shared with `SchedSRA.block`). -/ +theorem SchedSRA.stableR {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s s' : S} + {σ σ' : Fl4SpecA64.State Unit} + (H : SchedSRA st Z m κ s σ) {W : U64 → Prop} (hw : WritesIn st s s' W) (hW : ∀ a, W a → ¬ SReadA st Z m s σ a) + (hS : SStepA σ σ') (hrun : RunQA σ') (hrd : RdA σ') : SchedSRA st Z m κ s' σ' := by + have keep : ∀ a, Aligned a → SReadA st Z m s σ a → L.mem s' a = L.mem s a := + fun a ha hr => hw.mem_eq ha (fun h => hW a h hr) + have hT := H.core.tcbs + have tK : ∀ t ∈ Z.T, ∀ K : U64, (K = 3520#u64 ∨ K = 3448#u64 ∨ K = 3432#u64 ∨ K = 3464#u64 ∨ K = 3592#u64 ∨ + K = 3480#u64 ∨ K = 3488#u64) → L.mem s' (wa t K) = L.mem s (wa t K) := by + intro t ht K hK + refine keep _ ?_ (Or.inr (Or.inr (Or.inr (Or.inl ⟨t, ht, K, hK, rfl⟩)))) + rcases hK with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> exact field_aligned (hT t ht) (by simp) (by simp) + have nK : ∀ c ∈ Z.Cs, ∀ o : U64, o.val < 4288 → o.val % 8 = 0 → + L.mem s' (L.nodeAt s c o) = L.mem s (L.nodeAt s c o) := + fun c hc o ho h8 => keep _ (nodeAt_aligned (H.core.nodes c hc) ho h8) (Or.inl ⟨c, hc, o, ho, rfl⟩) + have hkb := H.ksb_al + have hf := H.ksFit + have gK : ∀ K : U64, (K = 8#u64 ∨ K = 16#u64) → L.mem s' (wa (L.ksb s) K) = L.mem s (wa (L.ksb s) K) := by + intro K hK + have hK' : K.val = 8 ∨ K.val = 16 := by rcases hK with rfl | rfl <;> simp + have hv : (wa (L.ksb s) K).val = (L.ksb s).val + K.val := by rw [wa_valm]; unfold ksSizeA at hf; omega + refine keep _ (by unfold Aligned; rw [hv]; omega) ?_ + rcases hK with rfl | rfl + · exact Or.inr (Or.inl rfl) + · exact Or.inr (Or.inr (Or.inl rfl)) + have hcore : SchedCoreA st Z.T Z.Cs s' Z.Q Z.R := + H.core.agree hw.1 (fun y hy => ⟨tK y hy 3480#u64 (by simp), tK y hy 3488#u64 (by simp)⟩) + (fun c hc o h1 h2 _ => nK c hc o h1 h2) (fun y hy => tK y hy 3592#u64 (by simp)) + (fun y hy => tK y hy 3520#u64 (by simp)) (fun y hy => tK y hy 3448#u64 (by simp)) + have hkc : ∀ c ∈ Z.C, σ'.kheap (scNameA m c) = σ.kheap (scNameA m c) := fun c hc => by + obtain ⟨x, n, hk, -⟩ := H.scr.sc c hc + exact hS.sc _ x n hk + exact H.rebuild (Q' := Z.Q) hw.1 hw.2.1 (fun c hc o h1 h2 _ => nK c hc o (by omega) h2) gK + (fun t ht K hK => tK t ht K (by rcases hK with h | h | h | h <;> simp [h])) + (fun a ha hf => keep a ha (Or.inr (Or.inr (Or.inr (Or.inr hf))))) + hkc + (fun _ _ x hx => hS.tcb _ x hx) hS.fix + (by + rw [hS.act, nK _ H.sch.cpuCs layout.node.SCHED_ACTION (by simp [layout.node.SCHED_ACTION]) + (by simp [layout.node.SCHED_ACTION])] + exact H.sch.act) + (fun u hu => H.actOn u (by rw [← hS.act]; exact hu)) hrd (H.inv.step hS) + hcore (fun p hp => by rw [hS.rq]; exact H.rest.rq p hp) (fun d p hd => by rw [hS.rq]; exact H.rest.rqDom d p hd) + hS.rel hrun + +/-- **`SchedHypsA.stable` / `SchedOpsEA.stable` for `SchedSRA`** (x86 `SchedRel.stable` / `stableE`; module +docstring): FL4 writes outside `SReadA`, the spec takes an `SStepA` step blocking no thread, FL4's state words are +the spec's states after. -/ +theorem SchedSRA.stable {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s s' : S} {σ σ' : Fl4SpecA64.State Unit} + (H : SchedSRA st Z m κ s σ) {W : U64 → Prop} (hw : WritesIn st s s' W) (hW : ∀ a, W a → ¬ SReadA st Z m s σ a) + (hS : SStepA σ σ') (hb : NoBlockESc σ σ') : SchedSRA st Z m κ s' σ' := + H.stableR hw hW hS (H.run.step hS hb) (H.rd.step hS hb) + +/-- (gen 8) **`SchedHypsA.block` for `SchedSRA`** (x86 `SchedHyps.block` via `SchedRel.stable`): blocking a thread +`p` that is in no spec queue and has `RelIfBound`, while every other TCB keeps its state with `inClassR` reflected +back (NoBlockE's first branch). The IPC contract's frame (`y.tcbState = x.tcbState` only, `NtfnStep` letting faults +change) does not suffice: a thread blocked on send with a timeout fault and no released SC whose fault is cleared +enters `inClassR` without `RelIfBound` (handoff, stop-first). -/ +theorem SchedSRA.block {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s s' : S} + {σ σ' : Fl4SpecA64.State Unit} + (H : SchedSRA st Z m κ s σ) {W : U64 → Prop} (hw : WritesIn st s s' W) (hW : ∀ a, W a → ¬ SReadA st Z m s σ a) + (hS : SStepA σ σ') {p : Fl4SpecA64.ObjRef} (hnq : (∀ d q, p ∉ σ.readyQueues d q) ∧ p ∉ σ.releaseQueue) + (hrib : RelIfBoundSc σ p) + (hk : ∀ u x y, σ.kheap u = some (.tcb x) → σ'.kheap u = some (.tcb y) → u ≠ p → + y.tcbState = x.tcbState ∧ (inClassRSc y.tcbState y.tcbFault → inClassRSc x.tcbState x.tcbFault)) : + SchedSRA st Z m κ s' σ' := by + have k : ∀ u, u ≠ p → (∃ x, σ.kheap u = some (.tcb x) ∧ Fl4SpecA64.runnable x.tcbState = true) → + ∃ y, σ'.kheap u = some (.tcb y) ∧ Fl4SpecA64.runnable y.tcbState = true := by + rintro u hu ⟨x, hx, hr⟩ + obtain ⟨y, hy, -⟩ := hS.tcb u x hx + exact ⟨y, hy, by rw [(hk u x y hx hy hu).1]; exact hr⟩ + refine H.stableR hw hW hS ⟨fun d q u hu => ?_, fun u hu => ?_⟩ ?_ + · rw [hS.rq] at hu + exact k u (fun e => hnq.1 d q (e ▸ hu)) (H.run.1 d q u hu) + · rw [hS.rel] at hu + exact k u (fun e => hnq.2 (e ▸ hu)) (H.run.2 u hu) + · intro u y hy hcls + obtain ⟨x, hx⟩ := hS.tcbBack u y hy + by_cases e : u = p + · subst e; exact hrib.step hS hx + · exact (H.rd u x hx ((hk u x y hx hy e).2 hcls)).step hS hx + +/-- **`SchedHypsA.notQueued` for `SchedSRA`**: a TCB that is not runnable is in no spec queue. -/ +theorem SchedSRA.notQueued {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} {σ : Fl4SpecA64.State Unit} + (H : SchedSRA st Z m κ s σ) {p : Fl4SpecA64.ObjRef} {x : Fl4SpecA64.Tcb} (hk : σ.kheap p = some (.tcb x)) + (hr : Fl4SpecA64.runnable x.tcbState = false) : (∀ d q, p ∉ σ.readyQueues d q) ∧ p ∉ σ.releaseQueue := by + have k : ∀ y, σ.kheap p = some (.tcb y) → Fl4SpecA64.runnable y.tcbState = true → False := by + intro y hy hr' + rw [hk] at hy + injection hy with hy; injection hy with hy + subst hy; rw [hr] at hr'; cases hr' + refine ⟨fun d q hm => ?_, fun hm => ?_⟩ + · obtain ⟨y, hy, hr'⟩ := H.run.1 d q p hm; exact k y hy hr' + · obtain ⟨y, hy, hr'⟩ := H.run.2 p hm; exact k y hy hr' + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScSwitchA.lean b/proofs/Fl4ProofsA64/P3/ScSwitchA.lean new file mode 100644 index 000000000..12f17809f --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScSwitchA.lean @@ -0,0 +1,444 @@ +/- +P3 (aarch64, A64-P3-MCS gen 3): **`reschedule_required` and `possible_switch_to` refine l4v's** over `SchedSRA` +(x86 `P3/SchedSwitch`'s `rescheduleRequired_noswitch` / `_notsched` / `_sched`, `possibleSwitchTo_unfold`, +`reschedule_required_refines`, `possible_switch_to_refines`, ported to the multi-core relation). + +* **`reschedule_required_refinesA`** (x86 `reschedule_required_refines`): FL4's `reschedule_required` has the spec's + single outcome; `SchedSRA` holds after (with the lists of the switch target's enqueue). +* **`possible_switch_to_refinesA`** (x86 `possible_switch_to_refines`): for a target on the current core, FL4's + `possible_switch_to t` has the spec's single outcome of `possible_switch_to t`. +* **`possible_switch_to_remoteA`** (new): for a target on another core FL4 enqueues it on that core (or does + nothing) and `SchedSRA` holds with the current core's view unchanged. +* **`possible_switch_to_remote_not_specA`** (new, the remote case's spec mismatch): for a target on another core with + an SC, not released, and the view's action `ResumeCurrentThread`, **no outcome of the spec's `possible_switch_to t` + in the current core's view is related to FL4's state after** (the spec, single-core, switches to `t`; FL4 enqueues + `t` on its own core). The remote case refines the spec only in the target core's view (`Ext/MultiCore`'s + per-core semantics, not modelled yet): it is stated as the view-preserving `possible_switch_to_remoteA`. + +Gen 4 (ruling 22:45Z items 2–3): `ActReadyA` is no longer a hypothesis but **`SchedSRA.actReady`** (from `SchedSRA.rd`, +`actOn`, `scPart` and the SC relation's non-empty refills), and gen 3's `hsw` is `SchedSRA.hsw` (the `actOn` field); +the lemmas that read `is_schedulable` take **`StEqA`** (FL4's state words; a projection of the IPC relations). +Remaining hypotheses: `hrun` (the target is runnable), `hon` / `hoff` (the core case). No named premise. +Gen 10 (ruling 01:39Z item 1): both refinements also give FL4's write set, `WritesIn s s' (SWrA Z s)` (the scheduler +words, `P3/ScSWrA`). +-/ +import Fl4ProofsA64.P3.ScSWrA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## The spec -/ + +section Spec +open Fl4SpecA64 + +/-- l4v `valid_sched_action`'s refill part (x86 `ActReady`, the IPC line's `ActReady`): a schedulable switch target +has an SC whose head refill is sufficient and ready. -/ +def ActReadyA (σ : State Unit) : Prop := + ∀ t, σ.schedulerAction = .switchThread t → schedulable t σ = true → + ∃ x scp, σ.kheap t = some (.tcb x) ∧ x.tcbSchedContext = some scp ∧ + getScRefillSufficient (Z := Unit) scp 0 σ = ({(true, σ)}, False) ∧ + getScRefillReady (Z := Unit) scp σ = ({(true, σ)}, False) + +open Fl4Spec in +/-- A schedulable thread is runnable and not released. -/ +theorem schedulable_partsA {σ : State Unit} {t : ObjRef} (h : schedulable t σ = true) : + (∀ x, σ.kheap t = some (.tcb x) → runnable x.tcbState = true) ∧ t ∉ σ.releaseQueue := by + unfold schedulable at h + simp only [decide_eq_true_eq] at h + have h' := h + refine ⟨fun x hx => ?_, h'.2.2⟩ + have := h'.1 + simpa [optPred, optMapSome, optMap, tcbsOfKh, tcbOf, hx] using this + +theorem rescheduleRequired_noswitchA {σ : State Unit} (h : ∀ t, σ.schedulerAction ≠ .switchThread t) : + rescheduleRequired (Z := Unit) σ = ({((), { σ with schedulerAction := .chooseNewThread })}, False) := by + unfold rescheduleRequired + rw [nd_gets_bindSc] + cases e : σ.schedulerAction with + | switchThread t => exact absurd e (h t) + | _ => rfl + +theorem rescheduleRequired_notschedA {σ : State Unit} {t : ObjRef} (h : σ.schedulerAction = .switchThread t) + (hs : schedulable t σ = false) : + rescheduleRequired (Z := Unit) σ = ({((), { σ with schedulerAction := .chooseNewThread })}, False) := by + unfold rescheduleRequired + rw [nd_gets_bindSc, h] + dsimp only + rw [nd_gets_bindSc, hs] + simp only [Fl4Spec.NonDet.when, Bool.false_eq_true, ↓reduceIte] + rw [nd_bind_oneSc (show Fl4Spec.NonDet.pure (σ := State Unit) () σ = ({((), σ)}, False) from rfl)] + rfl + +theorem rescheduleRequired_schedA {σ σ1 : State Unit} {t : ObjRef} (h : σ.schedulerAction = .switchThread t) + (hs : schedulable t σ = true) (hr : ActReadyA σ) + (he : tcbSchedAction (Z := Unit) tcbSchedEnqueue t σ = ({((), σ1)}, False)) : + rescheduleRequired (Z := Unit) σ = ({((), { σ1 with schedulerAction := .chooseNewThread })}, False) := by + obtain ⟨x, scp, hk, hsc, h1, h2⟩ := hr t h hs + have hm : (do + let scOpt ← threadGet (Z := Unit) Tcb.tcbSchedContext t + let scp ← Fl4Spec.NonDet.assertOpt scOpt + let sufficient ← getScRefillSufficient scp 0 + let ready ← getScRefillReady scp + Fl4Spec.NonDet.assert ((sufficient && ready) = true) + tcbSchedAction tcbSchedEnqueue t) σ = ({((), σ1)}, False) := by + rw [nd_bind_oneSc (threadGet_eqSc _ hk), hsc] + rw [nd_bind_oneSc (show Fl4Spec.NonDet.assertOpt (σ := State Unit) (some scp) σ = ({(scp, σ)}, False) from rfl), + nd_bind_oneSc h1, nd_bind_oneSc h2, nd_bind_oneSc (assert_trueSc (by rfl) σ)] + exact he + unfold rescheduleRequired + rw [nd_gets_bindSc, h] + dsimp only + rw [nd_gets_bindSc, hs] + simp only [Fl4Spec.NonDet.when, ↓reduceIte] + rw [nd_bind_oneSc hm] + rfl + +theorem possibleSwitchTo_unfoldA {σ : State Unit} {t : ObjRef} {x : Tcb} (hk : σ.kheap t = some (.tcb x)) : + possibleSwitchTo (Z := Unit) t σ = + (if x.tcbSchedContext ≠ none ∧ ¬ inReleaseQueue t σ = true then + (if x.tcbDomain ≠ σ.curDomain then tcbSchedAction tcbSchedEnqueue t + else if σ.schedulerAction ≠ .resumeCurThread then (do + rescheduleRequired + tcbSchedAction tcbSchedEnqueue t) + else setSchedulerAction (.switchThread t)) σ + else ({((), σ)}, False)) := by + unfold possibleSwitchTo getTcbObjRef + rw [nd_bind_oneSc (threadGet_eqSc _ hk), nd_gets_bindSc] + unfold Fl4Spec.NonDet.when + by_cases h : x.tcbSchedContext ≠ none ∧ ¬ inReleaseQueue t σ = true + · simp only [eq_true h, ↓reduceIte] + rw [nd_gets_bindSc, nd_bind_oneSc (threadGet_eqSc _ hk), nd_gets_bindSc] + · simp only [eq_false h, ↓reduceIte] + rfl + +end Spec + +/-! ## FL4 -/ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +theorem actOfA_switchA {w : U64} {t : Fl4SpecA64.ObjRef} (h : actOfA w = .switchThread t) : + w ≠ 0#u64 ∧ w ≠ 1#u64 ∧ t = specPtr w := by + unfold actOfA at h + split_ifs at h with h0 h1 + injection h with h + exact ⟨h0, h1, h.symm⟩ + +open Fl4Spec Fl4SpecA64 in +/-- **`ActReadyA` from `SchedSRA`** (gen 4, ruling 22:45Z item 2; x86 `ActReady` was a hypothesis): the switch +target is a TCB of `Z.T` (`actOn`); `schedulable` gives it runnable (so `inClassR`), not released, with an active SC +(`scPart`), which `RdA` (`rd`) makes released; the SC's refills are non-empty (`ScStateRelA`, `ScRel1A.refills`). -/ +theorem SchedSRA.actReady {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} (H : SchedSRA st Z m κ s σ) : + ActReadyA σ := by + intro t hact hs + have hact0 := hact + rw [H.sch.act] at hact + obtain ⟨-, -, rfl⟩ := actOfA_switchA hact + obtain ⟨hwT, -⟩ := H.actOn _ hact0 + obtain ⟨x, hk, hsc⟩ := H.sch.tcb _ hwT + obtain ⟨hrun, hnr⟩ := schedulable_partsA hs + have hs' := hs + unfold schedulable at hs' + simp only [decide_eq_true_eq] at hs' + obtain ⟨h0, hpos⟩ := (H.scPart hwT).1 hs'.2.1 + have hC := H.rest.scMem _ hwT h0 + obtain ⟨y, n, hky, hr⟩ := H.scr.sc _ hC + have h3 : x.tcbSchedContext = some (scNameA m (L.mem s (scA (L.actionW s)))) := by + rw [hsc]; unfold optScA; rw [if_neg h0]; rfl + have hyA : 0 < y.scRefillMax := by + simpa [optPred, optMap, tcbsOfKh, tcbOf, hk, h3, scsOfKh, hky, scOf, scActive, activeSc] using hs'.2.1 + have hya : scActive y = true := by simp [scActive, activeSc, hyA] + have hm : (L.rmax s (L.mem s (scA (L.actionW s)))).val ≠ 0 := by rw [hr.max]; omega + have hne : y.scRefills ≠ [] := by rw [(hr.act hm).refills]; exact refills_neA (hr.act hm).wf + obtain ⟨e1, e2⟩ := H.rd _ x hk (Or.inl (hrun x hk)) x _ y n hk h3 hky hya hnr + exact ⟨x, _, hk, h3, by rw [getScRefillSufficient_eqA hky hne, e2], + by rw [getScRefillReady_eqA hky hne, e1]⟩ + +theorem sched.cur_domain.specA (s : S) : + sched.cur_domain st s ⦃ v => v = L.mem s (wa (L.ksb s) 8#u64) ⦄ := by + unfold sched.cur_domain + rw [show layout.ks.CUR_DOMAIN = 8#u64 by unfold layout.ks.CUR_DOMAIN; rfl] + exact obj.ks_get.spec s _ + +theorem resume_valSc : sched.ACTION_RESUME_CURRENT = 0#u64 := by unfold sched.ACTION_RESUME_CURRENT; rfl +theorem choose_valSc : sched.ACTION_CHOOSE_NEW = 1#u64 := by unfold sched.ACTION_CHOOSE_NEW; rfl + +/-- Two `SchedSRA` states over the same `m` agree on a thread's affinity (`SchedRelA.aff`). -/ +theorem SchedSRA.aff_eq {Z Z' : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s s' : S} {σ σ' : Fl4SpecA64.State Unit} + (H : SchedSRA st Z m κ s σ) (H' : SchedSRA st Z' m κ s' σ') {t : U64} (ht : t ∈ Z.T) (ht' : t ∈ Z'.T) : + L.mem s' (affA t) = L.mem s (affA t) := by + have h1 := H.sch.aff t ht + have h2 := H'.sch.aff t ht' + rw [h1] at h2 + injection h2 with h2 + exact UScalar.eq_of_val_eq h2.symm + +/-- Two `SchedSRA` states over the same `m` are on the same core (`SchedRelA.core`). -/ +theorem SchedSRA.cpu_eq {Z Z' : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s s' : S} {σ σ' : Fl4SpecA64.State Unit} + (H : SchedSRA st Z m κ s σ) (H' : SchedSRA st Z' m κ s' σ') : L.cpu s' = L.cpu s := + UScalar.eq_of_val_eq (H'.sch.core.symm.trans H.sch.core) + +open Fl4SpecA64 in +/-- The singleton abstract reschedule result requests a new thread and keeps the logical clock. -/ +theorem rescheduleResultA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRA st Z m κ s σ) : + ∃ τ, rescheduleRequired (Z := Unit) σ = ({((), τ)}, False) ∧ + SFixA σ τ ∧ τ.kheap = σ.kheap ∧ τ.releaseQueue = σ.releaseQueue ∧ τ.schedulerAction = .chooseNewThread ∧ τ.machineState = σ.machineState := by + by_cases hs : ∃ u, σ.schedulerAction = .switchThread u + · obtain ⟨u, hu⟩ := hs + by_cases hs' : schedulable u σ = true + · obtain ⟨z, _, hz, -, -, -⟩ := H.actReady u hu hs' + have aa := hu + rw [H.sch.act] at aa + obtain ⟨-, -, rfl⟩ := actOfA_switchA aa + obtain ⟨hw, -⟩ := H.actOn _ hu + obtain ⟨v, he, hv, -, -⟩ := H.rest.prio _ hw + rw [he] at hz; cases hz + exact ⟨_, rescheduleRequired_schedA hu hs' H.actReady (tcbSchedAction_eqA he hv tcbSchedEnqueue), + ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩, rfl, rfl, rfl, rfl⟩ + · exact ⟨_, rescheduleRequired_notschedA hu (by simpa using hs'), + ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩, rfl, rfl, rfl, rfl⟩ + · exact ⟨_, rescheduleRequired_noswitchA (fun u hu => hs ⟨u, hu⟩), + ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩, rfl, rfl, rfl, rfl⟩ + +/-- **`reschedule_required` refines `reschedule_required`** (x86 `reschedule_required_refines`). -/ +theorem reschedule_required_refinesA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} {σ : Fl4SpecA64.State Unit} + (H : SchedSRA st Z m κ s σ) (hst : StEqA Z.T (L.mem s) σ) : + sched.reschedule_required st s ⦃ s' => ∃ σ' Q', Fl4SpecA64.rescheduleRequired (Z := Unit) σ = ({((), σ')}, False) ∧ + σ'.kheap = σ.kheap ∧ σ'.releaseQueue = σ.releaseQueue ∧ WritesIn st s s' (SWrA (st := st) Z s) ∧ + SchedSRA st { Z with Q := Q' } m κ s' σ' ⦄ := by + have fin : ∀ s1 (σ1 : Fl4SpecA64.State Unit) Q1, SchedSRA st { Z with Q := Q1 } m κ s1 σ1 → + Fl4SpecA64.rescheduleRequired (Z := Unit) σ = + ({((), { σ1 with schedulerAction := .chooseNewThread })}, False) → + σ1.kheap = σ.kheap → σ1.releaseQueue = σ.releaseQueue → WritesIn st s s1 (SWrA (st := st) Z s) → + sched.set_action st s1 sched.ACTION_CHOOSE_NEW ⦃ s' => ∃ σ' Q', + Fl4SpecA64.rescheduleRequired (Z := Unit) σ = ({((), σ')}, False) ∧ + σ'.kheap = σ.kheap ∧ σ'.releaseQueue = σ.releaseQueue ∧ WritesIn st s s' (SWrA (st := st) Z s) ∧ + SchedSRA st { Z with Q := Q' } m κ s' σ' ⦄ := by + intro s1 σ1 Q1 H1 hs hk hr hw1 + refine WP.spec_mono (sched.set_action.spec s1 _) ?_ + rintro s' rfl + exact ⟨_, Q1, hs, hk, hr, WritesIn.swr_trans rfl hw1 (H1.toSchedSRN.act_wr _), + H1.set_action _ (by rw [choose_valSc]; simp [actOfA]) fun u h => by cases h⟩ + unfold sched.reschedule_required + refine WP.spec_bind (sched.action.spec s) ?_ + rintro i rfl + by_cases e0 : L.actionW s = 0#u64 + · have hn : ∀ t, σ.schedulerAction ≠ .switchThread t := by + intro t h; rw [H.sch.act, e0] at h; simp [actOfA] at h + rw [if_neg (by rw [e0, resume_valSc]; simp), bind_ok] + exact fin s σ Z.Q H (rescheduleRequired_noswitchA hn) rfl rfl (WritesIn.refl _ _) + · rw [if_pos (bne_iff_ne.2 (by rwa [resume_valSc]))] + by_cases e1 : L.actionW s = 1#u64 + · have hn : ∀ t, σ.schedulerAction ≠ .switchThread t := by + intro t h; rw [H.sch.act, e1] at h; simp [actOfA] at h + rw [if_neg (by rw [e1, choose_valSc]; simp), bind_ok] + exact fin s σ Z.Q H (rescheduleRequired_noswitchA hn) rfl rfl (WritesIn.refl _ _) + · rw [if_pos (bne_iff_ne.2 (by rwa [choose_valSc]))] + obtain ⟨hwT, hon⟩ := H.hsw e0 e1 + have hact : σ.schedulerAction = .switchThread (specPtr (L.actionW s)) := by + rw [H.sch.act]; simp [actOfA, e0, e1] + rw [is_schedulable_eqA, bind_ok, schedulable_eqA H.toSchedSRN hwT (H.relOn hwT hon) hst] + by_cases hs : Fl4SpecA64.schedulable (specPtr (L.actionW s)) σ = true + · rw [if_pos hs] + obtain ⟨hrun, hnr⟩ := schedulable_partsA hs + refine WP.spec_bind (enqueue_refines_localA H _ hwT hon (fun _ => (H.relOn hwT hon).2 hnr) hrun) ?_ + rintro s1 ⟨σ1, hspec, hk1, hr1, -, hw1, H1⟩ + exact fin s1 σ1 _ H1 (rescheduleRequired_schedA hact hs H.actReady hspec) hk1 hr1 + (hw1.mono fun _ hb => H.toSchedSRN.enq_swr hwT hb) + · rw [if_neg hs, bind_ok] + exact fin s σ Z.Q H (rescheduleRequired_notschedA hact (by simpa using hs)) rfl rfl (WritesIn.refl _ _) + +theorem tcb_ne01Sc {t : U64} (h : TcbAddr t) : t ≠ 0#u64 ∧ t ≠ 1#u64 := by + have := h.ne0; have := h.align + refine ⟨fun e => by subst e; simp_all, fun e => by subst e; simp_all⟩ + +/-- **`possible_switch_to` refines `possible_switch_to`** for a target on the current core (x86 +`possible_switch_to_refines`). -/ +theorem possible_switch_to_refines_releaseA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} {σ : Fl4SpecA64.State Unit} + (H : SchedSRA st Z m κ s σ) (t : U64) (ht : t ∈ Z.T) (hon : L.mem s (affA t) = L.cpu s) + (hrun : ∀ x, σ.kheap (specPtr t) = some (.tcb x) → Fl4SpecA64.runnable x.tcbState = true) + (hst : StEqA Z.T (L.mem s) σ) : + sched.possible_switch_to st s t ⦃ s' => ∃ σ' Q', + Fl4SpecA64.possibleSwitchTo (Z := Unit) (specPtr t) σ = ({((), σ')}, False) ∧ σ'.kheap = σ.kheap ∧ σ'.releaseQueue = σ.releaseQueue ∧ + WritesIn st s s' (SWrA (st := st) Z s) ∧ SchedSRA st { Z with Q := Q' } m κ s' σ' ⦄ := by + obtain ⟨x, hk, hsc⟩ := H.sch.tcb t ht + obtain ⟨x', hk', hd, hdw, -⟩ := H.rest.prio t ht + rw [hk] at hk' + injection hk' with hk'; injection hk' with hk' + subst hk' + have hspec := possibleSwitchTo_unfoldA (t := specPtr t) hk + have hdom : ¬ x.tcbDomain ≠ σ.curDomain := by rw [hd, H.rest.curDom.1]; exact fun h => h rfl + unfold sched.possible_switch_to + refine WP.spec_bind (obj.Tcb.sc.spec s t) ?_ + rintro i rfl + by_cases h0 : L.mem s (scA t) = 0#u64 + · rw [if_pos h0, WP.spec_ok] + have e : x.tcbSchedContext = none := by rw [hsc, h0]; simp [optScA] + refine ⟨σ, Z.Q, ?_, rfl, rfl, WritesIn.refl _ _, H⟩ + rw [hspec, if_neg (by rw [e]; simp)] + · rw [if_neg h0] + refine WP.spec_bind (sched.in_release_queue.spec s t) ?_ + rintro b rfl + by_cases h1 : (L.mem s (inqA t) &&& sched.Q_RELEASE) ≠ 0#u64 + · rw [if_pos (decide_eq_true h1), WP.spec_ok] + have hin : specPtr t ∈ σ.releaseQueue := by + by_contra hn; exact h1 ((H.relOn ht hon).2 hn) + refine ⟨σ, Z.Q, ?_, rfl, rfl, WritesIn.refl _ _, H⟩ + rw [hspec, if_neg (by simp [Fl4SpecA64.inReleaseQueue, hin])] + · have h1' : (L.mem s (inqA t) &&& sched.Q_RELEASE) = 0#u64 := not_not.1 h1 + rw [if_neg (by simpa using h1)] + have hnR : specPtr t ∉ σ.releaseQueue := (H.relOn ht hon).1 h1' + have hcond : x.tcbSchedContext ≠ none ∧ ¬ Fl4SpecA64.inReleaseQueue (specPtr t) σ = true := by + rw [hsc]; simp [optScA, h0, Fl4SpecA64.inReleaseQueue, hnR] + refine WP.spec_bind (sched.cur_domain.specA s) ?_ + rintro i1 rfl + refine WP.spec_bind (obj.Tcb.domain.spec s t) ?_ + rintro i2 rfl + rw [H.rest.curDom.2, hdw] + simp only [bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + refine WP.spec_bind (obj.Tcb.affinity.spec s t) ?_ + rintro i3 rfl + refine WP.spec_bind (LawfulStore.cur_cpu_spec s) ?_ + rintro i4 rfl + rw [hon] + simp only [bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + refine WP.spec_bind (sched.action.spec s) ?_ + rintro i5 rfl + by_cases h5 : L.actionW s = 0#u64 + · have hres : σ.schedulerAction = .resumeCurThread := by rw [H.sch.act, h5]; simp [actOfA] + rw [if_neg (by rw [h5, resume_valSc]; simp)] + refine WP.spec_mono (sched.set_action.spec s t) ?_ + rintro s' rfl + obtain ⟨n0, n1⟩ := tcb_ne01Sc (H.core.tcbs t ht) + refine ⟨{ σ with schedulerAction := .switchThread (specPtr t) }, Z.Q, ?_, rfl, rfl, H.toSchedSRN.act_wr _, + H.set_action t (a := .switchThread (specPtr t)) (by simp [actOfA, n0, n1]) + fun u h => by injection h with h; rw [← specPtr_inj h]; exact ⟨ht, hon⟩⟩ + rw [hspec, if_pos hcond, if_neg hdom, if_neg (by rw [hres]; exact fun h => h rfl)] + rfl + · have hnres : σ.schedulerAction ≠ .resumeCurThread := by + intro e; rw [H.sch.act] at e; apply h5 + unfold actOfA at e + by_cases w0 : L.actionW s = 0#u64 + · exact w0 + · rw [if_neg w0] at e; split_ifs at e + rw [if_pos (bne_iff_ne.2 (by rwa [resume_valSc]))] + refine WP.spec_bind (reschedule_required_refinesA H hst) ?_ + rintro s1 ⟨σ1, Q1, hrr, hk1, hr1, hw1, H1⟩ + have hon1 : L.mem s1 (affA t) = L.cpu s1 := by rw [H.aff_eq H1 ht ht, H.cpu_eq H1]; exact hon + have hrel1 : (L.mem s1 (inqA t) &&& sched.Q_RELEASE) = 0#u64 := + (H1.relOn ht hon1).2 (by rw [hr1]; exact hnR) + refine WP.spec_mono (enqueue_refines_localA H1 t ht hon1 (fun _ => hrel1) + (fun y hy => hrun y (by rw [← hk1]; exact hy))) ?_ + rintro s2 ⟨σ2, he, hk2, hr2, -, hw2, H2⟩ + refine ⟨σ2, _, ?_, hk2.trans hk1, hr2.trans hr1, WritesIn.swr_trans rfl hw1 (hw2.mono fun _ hb => H1.toSchedSRN.enq_swr ht hb), H2⟩ + rw [hspec, if_pos hcond, if_neg hdom, if_pos hnres, nd_bind_oneSc hrr, he] + +theorem possible_switch_to_refinesA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} {σ : Fl4SpecA64.State Unit} + (H : SchedSRA st Z m κ s σ) (t : U64) (ht : t ∈ Z.T) (hon : L.mem s (affA t) = L.cpu s) + (hrun : ∀ x, σ.kheap (specPtr t) = some (.tcb x) → Fl4SpecA64.runnable x.tcbState = true) + (hst : StEqA Z.T (L.mem s) σ) : + sched.possible_switch_to st s t ⦃ s' => ∃ σ' Q', + Fl4SpecA64.possibleSwitchTo (Z := Unit) (specPtr t) σ = ({((), σ')}, False) ∧ σ'.kheap = σ.kheap ∧ + WritesIn st s s' (SWrA (st := st) Z s) ∧ SchedSRA st { Z with Q := Q' } m κ s' σ' ⦄ := by + refine WP.spec_mono (possible_switch_to_refines_releaseA H t ht hon hrun hst) ?_ + rintro s' ⟨σ', Q', he, hk, _, w, H'⟩ + exact ⟨σ', Q', he, hk, w, H'⟩ + +/-- **`possible_switch_to` for a target on another core** (module docstring): FL4 enqueues it on its own core or +does nothing; `SchedSRA` holds with the current core's view unchanged. -/ +theorem possible_switch_to_remoteA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} {σ : Fl4SpecA64.State Unit} + (H : SchedSRA st Z m κ s σ) (t : U64) (ht : t ∈ Z.T) (hoff : L.mem s (affA t) ≠ L.cpu s) : + sched.possible_switch_to st s t ⦃ s' => ∃ Q', SchedSRA st { Z with Q := Q' } m κ s' σ ∧ + ((L.mem s (scA t) ≠ 0#u64 ∧ (L.mem s (inqA t) &&& sched.Q_RELEASE) = 0#u64) → + L.actionW s' = L.actionW s) ⦄ := by + obtain ⟨-, -, -, hdw, -⟩ := H.rest.prio t ht + unfold sched.possible_switch_to + refine WP.spec_bind (obj.Tcb.sc.spec s t) ?_ + rintro i rfl + by_cases h0 : L.mem s (scA t) = 0#u64 + · rw [if_pos h0, WP.spec_ok] + exact ⟨Z.Q, H, fun h => absurd h0 h.1⟩ + · rw [if_neg h0] + refine WP.spec_bind (sched.in_release_queue.spec s t) ?_ + rintro b rfl + by_cases h1 : (L.mem s (inqA t) &&& sched.Q_RELEASE) ≠ 0#u64 + · rw [if_pos (decide_eq_true h1), WP.spec_ok] + exact ⟨Z.Q, H, fun h => absurd h.2 h1⟩ + · have h1' : (L.mem s (inqA t) &&& sched.Q_RELEASE) = 0#u64 := not_not.1 h1 + rw [if_neg (by simpa using h1)] + refine WP.spec_bind (sched.cur_domain.specA s) ?_ + rintro i1 rfl + refine WP.spec_bind (obj.Tcb.domain.spec s t) ?_ + rintro i2 rfl + rw [H.rest.curDom.2, hdw] + simp only [bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + refine WP.spec_bind (obj.Tcb.affinity.spec s t) ?_ + rintro i3 rfl + refine WP.spec_bind (LawfulStore.cur_cpu_spec s) ?_ + rintro i4 rfl + rw [if_pos (bne_iff_ne.2 hoff)] + refine WP.spec_mono (enqueue_remoteA H t ht hoff (fun _ => h1')) ?_ + rintro s' ⟨hw, H'⟩ + refine ⟨_, H', fun _ => ?_⟩ + have hT := H.core.tcbs + have hc := H.core.aff t ht + have hp := H.core.prio t ht + have hW : ∀ w ∈ t :: Z.Q (L.mem s (affA t)) (L.mem s (prioA t)), w ∈ Z.T := by + intro w hw'; rcases List.mem_cons.1 hw' with rfl | hw' + · exact ht + · exact H.core.memT hc hp hw' + have := node_qfoot hT hW (H.core.nodes _ hc) (H.core.nodes _ H.sch.cpuCs) ht (hw.mono fun _ h => h.toQFoot) (H.core.nodes _ H.sch.cpuCs) + (o := layout.node.SCHED_ACTION) (by simp [layout.node.SCHED_ACTION]) (by simp [layout.node.SCHED_ACTION]) + (fun _ => show ¬ 136 ≤ layout.node.SCHED_ACTION.val by simp [layout.node.SCHED_ACTION]) + (fun _ => by simp [layout.node.SCHED_ACTION]) + unfold LawfulStore.actionW + rw [hw.2.1]; exact this + +/-- **The remote case does not refine the spec's `possible_switch_to` in the current core's view** (module +docstring): with the view's action `ResumeCurrentThread`, a target on another core with an SC and not released, no +spec outcome is `SchedSRA`-related to FL4's state after. -/ +theorem possible_switch_to_remote_not_specA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} + {σ : Fl4SpecA64.State Unit} (H : SchedSRA st Z m κ s σ) (t : U64) (ht : t ∈ Z.T) + (hoff : L.mem s (affA t) ≠ L.cpu s) (h0 : L.mem s (scA t) ≠ 0#u64) + (h1 : (L.mem s (inqA t) &&& sched.Q_RELEASE) = 0#u64) (h5 : L.actionW s = 0#u64) + (hnR : specPtr t ∉ σ.releaseQueue) : + sched.possible_switch_to st s t ⦃ s' => ∀ (Z' : XSetsA) σ', ((), σ') ∈ (Fl4SpecA64.possibleSwitchTo (Z := Unit) + (specPtr t) σ).1 → ¬ SchedSRA st Z' m κ s' σ' ⦄ := by + obtain ⟨x, hk, hsc⟩ := H.sch.tcb t ht + obtain ⟨x', hk', hd, -, -⟩ := H.rest.prio t ht + rw [hk] at hk' + injection hk' with hk'; injection hk' with hk' + subst hk' + have hdom : ¬ x.tcbDomain ≠ σ.curDomain := by rw [hd, H.rest.curDom.1]; exact fun h => h rfl + have hres : σ.schedulerAction = .resumeCurThread := by rw [H.sch.act, h5]; simp [actOfA] + have hcond : x.tcbSchedContext ≠ none ∧ ¬ Fl4SpecA64.inReleaseQueue (specPtr t) σ = true := by + rw [hsc]; simp [optScA, h0, Fl4SpecA64.inReleaseQueue, hnR] + have hspec : Fl4SpecA64.possibleSwitchTo (Z := Unit) (specPtr t) σ = + ({((), { σ with schedulerAction := .switchThread (specPtr t) })}, False) := by + rw [possibleSwitchTo_unfoldA hk, if_pos hcond, if_neg hdom, if_neg (by rw [hres]; exact fun h => h rfl)] + rfl + refine WP.spec_mono (possible_switch_to_remoteA H t ht hoff) ?_ + rintro s' ⟨Q', H', hact⟩ Z' σ' hm H'' + rw [hspec] at hm + have e : σ' = { σ with schedulerAction := .switchThread (specPtr t) } := by + have := Set.mem_singleton_iff.1 hm; injection this + have a1 := H''.sch.act + have a2 := H'.sch.act + rw [hact ⟨h0, h1⟩] at a1 a2 + rw [e] at a1 + rw [← a2, hres] at a1 + cases a1 + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScTailA.lean b/proofs/Fl4ProofsA64/P3/ScTailA.lean new file mode 100644 index 000000000..c61cdbfc7 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScTailA.lean @@ -0,0 +1,278 @@ +/- +P3 (aarch64, A64-P3-MCS gen 5): **the sporadic unblock and the local restart tail** on `SchedSRA` (ruling 23:14Z +gen-5 item 2; x86 `P3/ScNtfn`'s `unblock_sporadic_refines`, `P3/IpcEpTail`). + +* **`ifCond_eqA`**: l4v `if_cond_refill_unblock_check (Some p) (Some False) (Some a)` (both the `assert` and the + `test` variant) of an SC is `refill_unblock_check p` when the SC is sporadic and not `cur_sc`, else nothing. +* **`unblock_sporadic_SRA`** (x86 `unblock_sporadic_refines`): FL4's `notification::unblock_sporadic t` (the + endpoint copy is the same body, `endpoint_unblock_sporadic_eqA`) refines `if_cond_refill_unblock_check` of `t`'s + SC on `SchedSRA`, single outcome, frame `UnbFrA`. +* **`restart_tail_refinesA`**: the local **`restartTail`** of the IPC line's `SchedOpsA` / `SchedOpsEA` + (`rustRestartTailN` / `rustRestartTail` against `Endp.specRestartTail`, the same texts inline here): for a runnable + thread on the current core (`affA t = cur_cpu`, ruling 23:14Z item 2), single spec outcome and `SchedSRA` after with + new lists `Q'`. + +Gen 10 (ruling 01:39Z item 1): **`UnbFrA.mfr`** (the unblock's frame as the bundle `MFrA` of `P3/ScSWrA`), +**`restart_tail_frameA`** (the restart tail with `MFrA`; `restart_tail_refinesA` is now its corollary) and +`wake_tail_nodonateA` with `MFrA`. + +No named premise. Hypotheses: `ht`, `hon` (the local case), `hrun`, `hst` (`StEqA`, ruled 22:45Z); gen 6: `hnq` +(`t` not in the release queue: IPC's `NotQueued`, so that the unblock keeps `ScInvA.sorted`) and, for +`unblock_sporadic_SRA`, `hrq` (if `t` is released its SC's head is not ready). +-/ +import Fl4ProofsA64.P3.ScSporA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section Spec +open Fl4SpecA64 + +/-- `if_cond_refill_unblock_check (Some p) (Some False) (Some a)` of an SC (module docstring). -/ +theorem ifCond_eqA {σ : State Unit} {p : ObjRef} {x : SchedContext} {n : Nat} + (hk : σ.kheap p = some (.schedContext x n)) (asst : Bool) : + ifCondRefillUnblockCheck (Z := Unit) (some p) (some false) (some asst) σ = + if x.scSporadic = true ∧ p ≠ σ.curSc then refillUnblockCheck (Z := Unit) p σ else ({((), σ)}, False) := by + unfold ifCondRefillUnblockCheck + simp only [Fl4Spec.NonDet.maybeM] + rw [nd_bind_oneSc (getSchedContext_eqA hk), nd_gets_bindSc, nd_pure_bindSc] + by_cases e1 : x.scSporadic = true <;> by_cases e2 : p = σ.curSc <;> cases asst <;> + simp [Fl4Spec.NonDet.when, Fl4Spec.NonDet.pure, e1, e2] + +end Spec + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- The endpoint copy of `unblock_sporadic` is the notification one. -/ +theorem endpoint_unblock_sporadic_eqA (s : S) (t : obj.Tcb) : + endpoint.unblock_sporadic st s t = notification.unblock_sporadic st s t := rfl + +/-- The view's current SC is the current core's `CUR_SC` word, renamed. -/ +theorem SchedSRA.curScW {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} {σ : Fl4SpecA64.State Unit} + (H : SchedSRA st Z m κ s σ) : σ.curSc = scNameA m (L.mem s (L.nodeAt s (L.cpu s) layout.node.CUR_SC)) := by + rw [← H.sch.csc, H.sch.core]; exact H.sch.curScOfW _ H.sch.cpuCs + +/-- **`unblock_sporadic` on `SchedSRA`** (x86 `unblock_sporadic_refines`; module docstring). -/ +theorem unblock_sporadic_SRA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} {σ : Fl4SpecA64.State Unit} + (H : SchedSRA st Z m κ s σ) {t : U64} (ht : t ∈ Z.T) (asst : Bool) + (hrq : specPtr t ∈ σ.releaseQueue → ∀ y n, σ.kheap (scNameA m (L.mem s (scA t))) = some (.schedContext y n) → + Fl4SpecA64.refillReady σ.curTime (Fl4SpecA64.refillHd y) = false) : + notification.unblock_sporadic st s t ⦃ s' => ∃ σ', + Fl4SpecA64.ifCondRefillUnblockCheck (Z := Unit) (optScA m (L.mem s (scA t))) (some false) (some asst) σ = + ({((), σ')}, False) ∧ SchedSRA st Z m κ s' σ' ∧ UnbFrA (st := st) Z m s s' σ σ' ⦄ := by + unfold notification.unblock_sporadic + refine WP.spec_bind (obj.Tcb.sc.spec s t) ?_ + rintro i rfl + rw [sc_sporadic_eq] + simp only [bind_ok] + by_cases h0 : L.mem s (scA t) = 0#u64 + · rw [if_pos h0] + simp only [Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + refine ⟨σ, ?_, H, UnbFrA.refl Z m s σ⟩ + rw [h0, show optScA m 0#u64 = none by simp [optScA]]; rfl + rw [if_neg h0] + have hscC := H.rest.scMem t ht h0 + obtain ⟨x, n, hk, hx⟩ := H.scr.sc _ hscC + have hopt : optScA m (L.mem s (scA t)) = some (scNameA m (L.mem s (scA t))) := by + unfold optScA; rw [if_neg h0]; rfl + rw [hopt, ifCond_eqA hk asst] + have esp : sporA (L.mem s (scA t)) = scWA (L.mem s (scA t)) 64#u64 := by + unfold sporA scWA layout.sc.SPORADIC; rfl + rw [esp] + by_cases hsp : L.mem s (scWA (L.mem s (scA t)) 64#u64) ≠ 0#u64 + · have hb : (L.mem s (scWA (L.mem s (scA t)) 64#u64) != 0#u64) = true := by simpa using hsp + rw [hb] + simp only [↓reduceIte] + refine WP.spec_bind (sched.cur_sc.spec s) ?_ + rintro s1 rfl + rw [sc_ne_eqA] + simp only [bind_ok] + have hxs : x.scSporadic = true := by rw [hx.sporadic]; simpa using hsp + by_cases e : L.mem s (scA t) = L.mem s (L.nodeA s layout.node.CUR_SC) + · rw [decide_eq_false (fun h => h e)] + simp only [Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + refine ⟨σ, ?_, H, UnbFrA.refl Z m s σ⟩ + rw [if_neg (fun h => h.2 (by rw [H.curScW]; exact congrArg (scNameA m) e))] + · rw [decide_eq_true e] + simp only [↓reduceIte] + have ha : Fl4SpecA64.scActive x = true := by + have h1 := hx.spor hsp + rw [hx.max] at h1 + simp [Fl4SpecA64.scActive, Fl4SpecA64.activeSc]; omega + have hnr : Fl4SpecA64.refillReady σ.curTime (Fl4SpecA64.refillHd x) = true → x.scPeriod ≠ 0 → + ∀ u ∈ σ.releaseQueue, ∀ y, σ.kheap u = some (.tcb y) → + y.tcbSchedContext ≠ some (scNameA m (L.mem s (scA t))) := by + intro hrd _ u hu y hy hys + obtain ⟨xt, hkt, hsct⟩ := H.sch.tcb t ht + obtain ⟨w1, k1, hw1, hb1⟩ := H.inv.back u y _ hy hys + obtain ⟨w2, k2, hw2, hb2⟩ := H.inv.back _ xt _ hkt (hsct.trans hopt) + rw [hw1] at hw2; cases hw2 + rw [hb1] at hb2; cases hb2 + rw [hrq hu x n hk] at hrd; cases hrd + refine WP.spec_mono (H.unblock_checkA hscC hk ha hnr) ?_ + rintro s' ⟨σ', h1, H', F⟩ + refine ⟨σ', ?_, H', F⟩ + rw [if_pos ⟨hxs, fun h => e (H.scp.inj _ hscC _ H.cscC (h.trans H.curScW))⟩, h1] + · have hb : (L.mem s (scWA (L.mem s (scA t)) 64#u64) != 0#u64) = false := by rw [not_not.1 hsp]; simp + rw [hb] + simp only [Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + refine ⟨σ, ?_, H, UnbFrA.refl Z m s σ⟩ + rw [if_neg (fun h => by rw [hx.sporadic] at h; exact hsp (by simpa using h.1))] + +/-- The facts at the end of a sporadic unblock that the switch step reads: `t`'s affinity, `StEqA`, and the spec TCB +of `t`. -/ +theorem UnbFrA.switch_pre {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s s' : S} {σ σ' : Fl4SpecA64.State Unit} + (H : SchedSRA st Z m κ s σ) (F : UnbFrA (st := st) Z m s s' σ σ') {t : U64} (ht : t ∈ Z.T) + (hon : L.mem s (affA t) = L.cpu s) (hst : StEqA Z.T (L.mem s) σ) : + L.mem s' (affA t) = L.cpu s' ∧ StEqA Z.T (L.mem s') σ' ∧ + (∀ x, σ.kheap (specPtr t) = some (.tcb x) → σ'.kheap (specPtr t) = some (.tcb x)) ∧ + (∀ x, σ'.kheap (specPtr t) = some (.tcb x) → σ.kheap (specPtr t) = some (.tcb x)) := by + have hr56 : layout.node.REPROGRAM.val = 56 := by unfold layout.node.REPROGRAM; rfl + obtain ⟨-, -, tK⟩ := H.keepW F.wr (o0 := layout.node.REPROGRAM) (by rw [hr56]; omega) (fun b hb => hb) + have back : ∀ u ∈ Z.T, ∀ y, σ'.kheap (specPtr u) = some (.tcb y) → σ.kheap (specPtr u) = some (.tcb y) := by + intro u hu y hy + obtain ⟨x0, hx0, -⟩ := H.sch.tcb u hu + rw [F.obj _ _ hx0 (fun _ _ h => by cases h)] at hy + rw [hx0, hy] + refine ⟨?_, fun u hu y hy => ?_, fun x hx => F.obj _ _ hx (fun _ _ h => by cases h), back t ht⟩ + · rw [F.wr.2.1, show affA t = wa t 3520#u64 from rfl, tK t ht 3520#u64 (by simp) (by simp)]; exact hon + · rw [stA_eq, tK u hu 3328#u64 (by simp) (by simp), ← stA_eq]; exact hst u hu y (back u hu y hy) + +/-- (gen 10) **The sporadic unblock's frame as the bundle `MFrA`** (`REPROGRAM` is a block word). -/ +theorem UnbFrA.mfr {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s s' : S} {σ σ' : Fl4SpecA64.State Unit} + (H : SchedSRN st Z m κ s σ) (F : UnbFrA (st := st) Z m s s' σ σ') : MFrA (st := st) Z m s s' σ σ' := + ⟨F.wr.mono fun _ hb => hb.elim Or.inl fun e => Or.inr (by rw [e]; exact H.repr_swr), SKeepA.of_obj F.obj, F.sz⟩ + +/-- **The local restart tail** (`SchedOpsA.restartTail` / `SchedOpsEA.restartTail`, local case; module docstring): +`unblock_sporadic t` then `possible_switch_to t` refines the spec's sporadic check then `possible_switch_to t`; +(gen 10) with the frame `MFrA`. -/ +theorem restart_tail_frameA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} {σ : Fl4SpecA64.State Unit} + (H : SchedSRA st Z m κ s σ) {t : U64} (ht : t ∈ Z.T) (hon : L.mem s (affA t) = L.cpu s) + (hrun : ∀ x, σ.kheap (specPtr t) = some (.tcb x) → Fl4SpecA64.runnable x.tcbState = true) + (hst : StEqA Z.T (L.mem s) σ) (hnq : specPtr t ∉ σ.releaseQueue) : + (do + let s2 ← notification.unblock_sporadic st s t + sched.possible_switch_to st s2 t) ⦃ s' => ∃ σ' Q', + (do + let scOpt ← Fl4SpecA64.getTcbObjRef (Z := Unit) Fl4SpecA64.Tcb.tcbSchedContext (specPtr t) + Fl4SpecA64.ifSporadicCurScAssertRefillUnblockCheck scOpt + Fl4SpecA64.possibleSwitchTo (specPtr t)) σ = ({((), σ')}, False) ∧ + SchedSRA st { Z with Q := Q' } m κ s' σ' ∧ MFrA (st := st) Z m s s' σ σ' ⦄ := by + obtain ⟨x, hkt, hsct⟩ := H.sch.tcb t ht + refine WP.spec_bind (unblock_sporadic_SRA H ht true (fun h => absurd h hnq)) ?_ + rintro s2 ⟨σ2, h2, H2, F⟩ + obtain ⟨hon2, hst2, -, hb⟩ := F.switch_pre H ht hon hst + refine WP.spec_mono (possible_switch_to_refinesA H2 t ht hon2 (fun y hy => hrun y (hb y hy)) hst2) ?_ + rintro s' ⟨σ', Q', h3, hk3, hw3, H'⟩ + refine ⟨σ', Q', ?_, H', (F.mfr H.toSchedSRN).trans (MFrA.of_sw hk3 hw3) rfl rfl⟩ + have h2' : Fl4SpecA64.ifSporadicCurScAssertRefillUnblockCheck (Z := Unit) (optScA m (L.mem s (scA t))) σ = + ({((), σ2)}, False) := h2 + unfold Fl4SpecA64.getTcbObjRef + rw [nd_bind_oneSc (threadGet_eqSc _ hkt), hsct, nd_bind_oneSc h2'] + exact h3 + +/-- **The local restart tail** (`SchedOpsA.restartTail` / `SchedOpsEA.restartTail`, local case; gen 5's statement, +from `restart_tail_frameA`). -/ +theorem restart_tail_refinesA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} {σ : Fl4SpecA64.State Unit} + (H : SchedSRA st Z m κ s σ) {t : U64} (ht : t ∈ Z.T) (hon : L.mem s (affA t) = L.cpu s) + (hrun : ∀ x, σ.kheap (specPtr t) = some (.tcb x) → Fl4SpecA64.runnable x.tcbState = true) + (hst : StEqA Z.T (L.mem s) σ) (hnq : specPtr t ∉ σ.releaseQueue) : + (do + let s2 ← notification.unblock_sporadic st s t + sched.possible_switch_to st s2 t) ⦃ s' => ∃ σ' Q', + (do + let scOpt ← Fl4SpecA64.getTcbObjRef (Z := Unit) Fl4SpecA64.Tcb.tcbSchedContext (specPtr t) + Fl4SpecA64.ifSporadicCurScAssertRefillUnblockCheck scOpt + Fl4SpecA64.possibleSwitchTo (specPtr t)) σ = ({((), σ')}, False) ∧ + SchedSRA st { Z with Q := Q' } m κ s' σ' ⦄ := + WP.spec_mono (restart_tail_frameA H ht hon hrun hst hnq) fun _ ⟨σ', Q', h1, h2, _⟩ => ⟨σ', Q', h1, h2⟩ + +/-- (gen 11, ruling 02:05Z item 2) **The local restart tail of a running or restarted thread** (`SchedOpsEA.restartTail` +widened to `Running ∨ Restart`, IPC's `RunTailOpA.runTail` included; the endpoint program text): a corollary of +`restart_tail_frameA` (whose `hrun` already covers both states). -/ +theorem restart_tail_localA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} {σ : Fl4SpecA64.State Unit} + (H : SchedSRA st Z m κ s σ) {t : U64} {x : Fl4SpecA64.Tcb} (ht : t ∈ Z.T) + (hkt : σ.kheap (specPtr t) = some (.tcb x)) + (hrs : x.tcbState = .running ∨ x.tcbState = .restart) (hon : L.mem s (affA t) = L.cpu s) + (hst : StEqA Z.T (L.mem s) σ) (hnq : specPtr t ∉ σ.releaseQueue) : + (do + let s2 ← endpoint.unblock_sporadic st s t + sched.possible_switch_to st s2 t) ⦃ s' => ∃ σ' Q', + (do + let scOpt ← Fl4SpecA64.getTcbObjRef (Z := Unit) Fl4SpecA64.Tcb.tcbSchedContext (specPtr t) + Fl4SpecA64.ifSporadicCurScAssertRefillUnblockCheck scOpt + Fl4SpecA64.possibleSwitchTo (specPtr t)) σ = ({((), σ')}, False) ∧ + SchedSRA st { Z with Q := Q' } m κ s' σ' ∧ MFrA (st := st) Z m s s' σ σ' ⦄ := + restart_tail_frameA H ht hon (fun y hy => by + rw [hkt] at hy; cases hy; rcases hrs with e | e <;> rw [e] <;> rfl) hst hnq + +/-- (gen 6) Two `SchedSRA` states of the same view with the same release lists have the same spec release +queue. -/ +theorem SchedSRA.relQ_eq {Z Z' : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s s' : S} {σ σ' : Fl4SpecA64.State Unit} + (H : SchedSRA st Z m κ s σ) (H' : SchedSRA st Z' m κ s' σ') (hR : Z'.R = Z.R) : + σ'.releaseQueue = σ.releaseQueue := by + rw [H'.rest.rel, H.rest.rel, hR, H.cpu_eq H'] + +variable {A : Type} {ar : machine.Arch A} + +/-- A `let` of a pair (x86 `TcbCtl.let_pair2`). -/ +theorem let_pairSc {α β γ : Type} (x : α) (y : β) (f : α → β → Result γ) (Q : γ → Prop) + (h : f x y ⦃ res => Q res ⦄) : (let (s1, a1) := (x, y); f s1 a1) ⦃ res => Q res ⦄ := h + +/-- **The local wake tail without a donation** (`SchedOpsA.wakeTail`, the no-donation case of ruling 23:14Z item 1; +x86 `IpcWake.wakeTail_top`'s composition): `maybe_donate_sched_context` (no donation: `hno`), the local switch, the +sporadic unblock, against the spec's `specWakeTail` (the IPC line's text, inline); (gen 10) with the frame `MFrA`. -/ +theorem wake_tail_nodonateA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} {σ : Fl4SpecA64.State Unit} + (H : SchedSRA st Z m κ s σ) (a : A) {t n : U64} (ht : t ∈ Z.T) (hon : L.mem s (affA t) = L.cpu s) + (hst : StEqA Z.T (L.mem s) σ) (hN : NtfnScEqA m n (L.mem s) σ) (hnq : specPtr t ∉ σ.releaseQueue) + (hno : L.mem s (scA t) ≠ 0#u64 ∨ L.mem s (nScSc n) = 0#u64 ∨ + (L.mem s (nScSc n) ∈ Z.C ∧ L.mem s (scTcbA (L.mem s (nScSc n))) ≠ 0#u64)) : + (do + let (s4, a2) ← notification.maybe_donate_sched_context st ar s a t n + let b1 ← sched.is_schedulable st s4 t + let s5 ← if b1 then sched.possible_switch_to st s4 t else ok s4 + let s6 ← notification.unblock_sporadic st s5 t + ok (s6, a2)) ⦃ r => r.2 = a ∧ ∃ σ' Q', + (do + Fl4SpecA64.maybeDonateSc (Z := Unit) (specPtr t) (specPtr n) + let b ← Fl4Spec.NonDet.gets (Fl4SpecA64.schedulable (Z := Unit) (specPtr t)) + Fl4Spec.NonDet.when b <| Fl4SpecA64.possibleSwitchTo (specPtr t) + let scopt ← Fl4SpecA64.getTcbObjRef Fl4SpecA64.Tcb.tcbSchedContext (specPtr t) + Fl4SpecA64.ifSporadicCurScTestRefillUnblockCheck scopt) σ = ({((), σ')}, False) ∧ + SchedSRA st { Z with Q := Q' } m κ r.1 σ' ∧ MFrA (st := st) Z m s r.1 σ σ' ⦄ := by + refine WP.spec_bind (maybe_donate_nodonateA H a t n ht hN hno) ?_ + rintro r ⟨rfl, hd⟩ + refine let_pairSc s a (fun s4 a2 => do + let b1 ← sched.is_schedulable st s4 t + let s5 ← if b1 then sched.possible_switch_to st s4 t else ok s4 + let s6 ← notification.unblock_sporadic st s5 t + ok (s6, a2)) _ ?_ + beta_reduce + have hsw := switch_if_refinesA H t ht hon hst + rw [is_schedulable_eqA] at hsw ⊢ + simp only [bind_ok] at hsw ⊢ + refine WP.spec_bind hsw ?_ + rintro s5 ⟨σ2, Q', hw2, hk2, hwr2, H2⟩ + obtain ⟨x2, hkt2, hsc2⟩ := H2.sch.tcb t ht + refine WP.spec_bind (unblock_sporadic_SRA H2 ht false (fun h => (hnq (by rw [← H.relQ_eq H2 rfl]; exact h)).elim)) ?_ + rintro s6 ⟨σ', h3, H', F⟩ + rw [WP.spec_ok] + refine ⟨rfl, σ', Q', ?_, H', (MFrA.of_sw hk2 hwr2).trans (F.mfr H2.toSchedSRN) rfl rfl⟩ + rw [nd_gets_bindSc] at hw2 + have h3' : Fl4SpecA64.ifSporadicCurScTestRefillUnblockCheck (Z := Unit) (optScA m (L.mem s5 (scA t))) σ2 = + ({((), σ')}, False) := h3 + unfold Fl4SpecA64.getTcbObjRef + rw [nd_bind_oneSc hd, nd_gets_bindSc, nd_bind_oneSc hw2, nd_bind_oneSc (threadGet_eqSc _ hkt2), hsc2] + exact h3' + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScTailEA.lean b/proofs/Fl4ProofsA64/P3/ScTailEA.lean new file mode 100644 index 000000000..791b689cf --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScTailEA.lean @@ -0,0 +1,62 @@ +/- +P3 (aarch64, A64-P3-MCS gen 12): **the MCS lemmas for IPC gen 13's two new `SchedOpsEA` operations** (ruling 02:39Z +gen-12 item 3; x86 `P3/IpcEpTail`'s `EpTailHyps.sporadicAssert` / `EpTailHyps.switchTo` instances from +`P3/ScNtfn`'s `unblock_sporadic_refines` and `P3/ScSwitch`'s `possible_switch_to_refines`, ported). + +* **`unblock_sporadic_blockedA`** (for `SchedOpsEA.unblockSporadic`): FL4's `endpoint::unblock_sporadic t` of a TCB + that is not runnable (so in no queue, `SchedSRA.notQueued`) against l4v + `if_sporadic_cur_sc_assert_refill_unblock_check` of its SC: single outcome, `SchedSRA` after (same `Z`) and the frame + `MFrA` (from gen 5's `unblock_sporadic_SRA` and gen 10's `UnbFrA.mfr`). +* **`switch_to_runA`** (for `SchedOpsEA.switchTo`): `sched::possible_switch_to t` of a TCB set `Running` on the current + core against l4v `possible_switch_to`: single outcome, `SchedSRA` after with new ready lists `Q'`, and `MFrA` (from + gen 4's `possible_switch_to_refinesA` and `MFrA.of_sw`). + +IPC's `FootEq` / `KView` follow from `MFrA` at the join (`SchedSRN.not_mwr` + `SKeepA`, as for the other tails). + +No named premise. Hypotheses: `ht`, `hkt` (the related TCB), `hnr` (not runnable) / `hrun` (`Running`), `hon` (the +local case), `hst` (`StEqA`, ruled 22:45Z). +-/ +import Fl4ProofsA64.P3.ScFrameA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **The sporadic unblock of a blocked thread** (module docstring). -/ +theorem unblock_sporadic_blockedA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} + {σ : Fl4SpecA64.State Unit} (H : SchedSRA st Z m κ s σ) {t : U64} (ht : t ∈ Z.T) {x : Fl4SpecA64.Tcb} + (hkt : σ.kheap (specPtr t) = some (.tcb x)) (hnr : Fl4SpecA64.runnable x.tcbState = false) : + endpoint.unblock_sporadic st s t ⦃ s' => ∃ σ', + Fl4SpecA64.ifSporadicCurScAssertRefillUnblockCheck (Z := Unit) x.tcbSchedContext σ = ({((), σ')}, False) ∧ + SchedSRA st Z m κ s' σ' ∧ MFrA (st := st) Z m s s' σ σ' ⦄ := by + obtain ⟨x', hk', hsct⟩ := H.sch.tcb t ht + rw [hkt] at hk'; cases hk' + have hnq := (H.notQueued hkt hnr).2 + rw [endpoint_unblock_sporadic_eqA] + refine WP.spec_mono (unblock_sporadic_SRA H ht true (fun h => absurd h hnq)) ?_ + rintro s' ⟨σ', h1, H', F⟩ + exact ⟨σ', by rw [hsct]; exact h1, H', F.mfr H.toSchedSRN⟩ + +/-- **`possible_switch_to` of a thread set `Running`** (module docstring). -/ +theorem switch_to_runA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} + {σ : Fl4SpecA64.State Unit} (H : SchedSRA st Z m κ s σ) {t : U64} (ht : t ∈ Z.T) {x : Fl4SpecA64.Tcb} + (hkt : σ.kheap (specPtr t) = some (.tcb x)) (hrun : x.tcbState = .running) + (hon : L.mem s (affA t) = L.cpu s) (hst : StEqA Z.T (L.mem s) σ) : + sched.possible_switch_to st s t ⦃ s' => ∃ σ' Q', + Fl4SpecA64.possibleSwitchTo (Z := Unit) (specPtr t) σ = ({((), σ')}, False) ∧ + SchedSRA st { Z with Q := Q' } m κ s' σ' ∧ MFrA (st := st) Z m s s' σ σ' ⦄ := by + refine WP.spec_mono (possible_switch_to_refinesA H t ht hon (fun y hy => by + rw [hkt] at hy; cases hy; rw [hrun]; rfl) hst) ?_ + rintro s' ⟨σ', Q', h1, hk, hw, H'⟩ + exact ⟨σ', Q', h1, H', MFrA.of_sw hk hw⟩ + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScTimeA.lean b/proofs/Fl4ProofsA64/P3/ScTimeA.lean new file mode 100644 index 000000000..5c102965c --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScTimeA.lean @@ -0,0 +1,260 @@ +import Fl4ProofsA64.P3.ScCfgFinishA +import Fl4ProofsA64.P3.FrontEndExtraA +import Fl4ProofsA64.P3.ScYieldQueueA +import Fl4SpecA64.Ext.Timestamp + +/-! +AArch64 entry timestamp. [P] `NowAgreeA` relates the raw counter +observation and successor architecture state. [J] `ValidMachineTimeA` +relates the recorded timestamp to the latest sample. [J] `CounterStepA` +orders the next raw sample in the current counter epoch. +-/ + +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3.Sched + +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +abbrev rawNextA (ms : MachineState) : Ticks := fl4RawCounterOracle (ms.timeState + 1) +abbrev rawStateA (ms : MachineState) : MachineState := + { ms with timeState := ms.timeState + 1, lastMachineTime := rawNextA ms } + +/-- [P] The raw architecture observation supplies the sample and advances +the clock relation by exactly one read. -/ +def NowAgreeA (ar : machine.Arch A) (Clock : A → MachineState → Prop) : Prop := + ∀ a ms, Clock a ms → ar.now a ⦃ r => r.1.bv = rawNextA ms ∧ Clock r.2 (rawStateA ms) ⦄ + +/-- [J] Current time is bounded by the last observation. The next read +consumes a separate same-epoch fact; it immediately re-establishes this one. -/ +def ValidMachineTimeA (σ : State Unit) : Prop := σ.curTime ≤ σ.machineState.lastMachineTime + +/-- [J] On this verified clock step the raw sample stays in the same epoch. -/ +def CounterStepA (σ : State Unit) : Prop := σ.machineState.lastMachineTime ≤ rawNextA σ.machineState + +theorem fl4DoReadRawCounterA (σ : State Unit) : + doMachineOp fl4ReadRawCounter σ = + ({(rawNextA σ.machineState, { σ with machineState := rawStateA σ.machineState })}, False) := by + unfold doMachineOp + rw [nd_gets_bindSc] + have sel : NonDet.selectF (fl4ReadRawCounter σ.machineState) σ = + ({((rawNextA σ.machineState, rawStateA σ.machineState), σ)}, False) := selectF_oneA _ _ + rw [nd_bind_oneSc sel] + dsimp only + rw [nd_modify_bindA] + rfl + +theorem fl4UpdateTimestamp_eqA (σ : State Unit) : + fl4UpdateTimestamp σ = + ({((), fl4TimestampState (rawNextA σ.machineState) { σ with machineState := rawStateA σ.machineState })}, False) := by + unfold fl4UpdateTimestamp + rw [nd_bind_oneSc (fl4DoReadRawCounterA σ)] + rfl + +theorem fl4UpdateTimestamp_eq_l4vA (σ : State Unit) (hnd : numDomains = 1) + (hNow : fl4ReadRawCounter σ.machineState = getCurrentTime σ.machineState) : + fl4UpdateTimestamp σ = updateTimeStamp σ := by + have hm : (doMachineOp (Z := Unit) getCurrentTime) σ = + ({(rawNextA σ.machineState, { σ with machineState := rawStateA σ.machineState })}, False) := by + have e := fl4DoReadRawCounterA σ + unfold doMachineOp at e ⊢ + rw [nd_gets_bindSc] at e ⊢ + rwa [← hNow] + unfold updateTimeStamp + rw [fl4UpdateTimestamp_eqA, nd_gets_bindSc, nd_bind_oneSc hm, nd_pure_bindSc, + nd_modify_bindA, nd_modify_bindA, nd_gets_bindSc] + simp [NonDet.when, fl4TimestampState, hnd] + rfl + +private theorem timeReadyMonoA {a b : Ticks} (h : a ≤ b) (r : Refill) + (hr : refillReady a r = true) : refillReady b r = true := + decide_eq_true (BitVec.le_trans (of_decide_eq_true hr) h) + +theorem RdA.timeMonoA {σ τ : State Unit} (R : RdA σ) (hk : τ.kheap = σ.kheap) + (hq : τ.releaseQueue = σ.releaseQueue) (h : σ.curTime ≤ τ.curTime) : RdA τ := by + intro u x hu hs z p y n hz hp hy ha hnr + rw [hk] at hu hz hy + rw [hq] at hnr + have e := R u x hu hs z p y n hz hp hy ha hnr + exact ⟨timeReadyMonoA h _ e.1, e.2⟩ + +theorem timestampOffsetA (σ : State Unit) (I : CurInvA σ) (R : OffReadyA σ) + (V : ValidMachineTimeA σ) (C : CounterStepA σ) : + OffReadyA (fl4TimestampState (rawNextA σ.machineState) { σ with machineState := rawStateA σ.machineState }) := by + let now := rawNextA σ.machineState + have hn : σ.curTime ≤ now := BitVec.le_trans V C + intro y n hy + change σ.kheap σ.curSc = some (.schedContext y n) at hy + have hSum := R y n hy + change (refillHd y).rTime.toNat + (σ.consumedTime + (now - σ.curTime)).toNat ≤ now.toNat + rw [BitVec.toNat_add, BitVec.toNat_sub_of_le hn] + have hnat := BitVec.le_def.1 hn + have hSize := now.isLt + rw [Nat.mod_eq_of_lt (by omega)] + omega + +private theorem clockNodeWordsA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRN st Z m κ s σ) : + Aligned (L.nodeA s layout.node.CUR_TIME) ∧ Aligned (L.nodeA s layout.node.CONSUMED) ∧ + L.nodeA s layout.node.CUR_TIME ≠ L.nodeA s layout.node.CONSUMED := by + obtain ⟨nn, -, hkn, pcs⟩ := H.csN + have hCpu : (L.cpu s).val < 128 := by have := (pcs _).1 H.sch.cpuCs; omega + have e40 : layout.node.CONSUMED.val = 40 := by unfold layout.node.CONSUMED; rfl + have e48 : layout.node.CUR_TIME.val = 48 := by unfold layout.node.CUR_TIME; rfl + have c := nodeAt_valSc H.ksFit hCpu (by rw [e40]; decide : layout.node.CONSUMED.val < 4288) + have t := nodeAt_valSc H.ksFit hCpu (by rw [e48]; decide : layout.node.CUR_TIME.val < 4288) + change Aligned (L.nodeAt s (L.cpu s) layout.node.CUR_TIME) ∧ + Aligned (L.nodeAt s (L.cpu s) layout.node.CONSUMED) ∧ + L.nodeAt s (L.cpu s) layout.node.CUR_TIME ≠ L.nodeAt s (L.cpu s) layout.node.CONSUMED + have ha := H.ksb_al + unfold Aligned at ha ⊢ + rw [c, t, e40, e48] + refine ⟨by omega, by omega, ?_⟩ + intro e + have ev := congrArg (fun k : U64 => k.val) e + rw [c, t, e40, e48] at ev + omega + +/-- A single-domain timestamp performs two node writes and returns the +architecture state advanced by the raw observation. -/ +theorem update_timestamp_wordsA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRN st Z m κ s σ) (a : A) + {Clock : A → MachineState → Prop} (P : NowAgreeA ar Clock) (hClock : Clock a σ.machineState) : + sched.update_timestamp st ar s a ⦃ r => + ∃ now, now.bv = rawNextA σ.machineState ∧ Clock r.2 (rawStateA σ.machineState) ∧ + r.1 = L.wr (L.wr s (L.nodeA s layout.node.CUR_TIME) now) (L.nodeA s layout.node.CONSUMED) + (core.num.U64.wrapping_add (L.mem s (L.nodeA s layout.node.CONSUMED)) + (core.num.U64.wrapping_sub now (L.curTime s))) ⦄ := by + obtain ⟨alT, alC, ne⟩ := clockNodeWordsA H + have ep : sched.cur_time st s = ok (L.curTime s) := by + unfold sched.cur_time LawfulStore.curTime LawfulStore.nodeA + rw [L.cur_cpu_eq, bind_ok, eq_of_spec_fd (obj.node_get.spec s _ _)] + unfold sched.update_timestamp + rw [ep, bind_ok] + refine WP.spec_bind (P a σ.machineState hClock) ?_ + rintro ⟨now, a1⟩ ⟨hn, hcl⟩ + simp only [uncurry_apply_pair] + rw [L.cur_cpu_eq, bind_ok, eq_of_spec_fd (obj.node_set.spec s _ _ _)] + simp only [bind_ok, lift] + let s1 := L.wr s (L.nodeA s layout.node.CUR_TIME) now + have addr : L.nodeA s1 layout.node.CONSUMED = L.nodeA s layout.node.CONSUMED := by + unfold LawfulStore.nodeA s1 + rw [L.cpu_wr, nodeAt_of_ksb (L.ksb_wr _ _ _)] + have c : sched.consumed st s1 = ok (L.mem s (L.nodeA s layout.node.CONSUMED)) := by + rw [eq_of_spec_fd (sched.consumed.specA s1), addr] + rw [L.mem_wr_ne alT alC (fun h => ne (UScalar.eq_of_val_eq h))] + change sched.consumed st (L.wr s (L.nodeAt s (L.cpu s) layout.node.CUR_TIME) now) = _ at c + rw [c, bind_ok] + change (sched.set_consumed st s1 _ >>= _) ⦃ _ ⦄ + refine WP.spec_bind (sched.set_consumed.spec s1 _) ?_ + rintro s2 rfl + simp only [layout.NUM_DOMAINS, fl4_abi.config.NUM_DOMAINS, + show ¬ ((1#u64 : U64) > 1#u64) from by decide, ↓reduceIte, bind_ok, WP.spec_ok] + exact ⟨now, hn, hcl, by rw [addr]⟩ + +set_option maxHeartbeats 800000 in +theorem update_timestamp_refinesA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (E : YieldQueueRelA (st := st) Z m κ s σ) (a : A) {Clock : A → MachineState → Prop} + (P : NowAgreeA ar Clock) (hClock : Clock a σ.machineState) (hnd : numDomains = 1) + (V : ValidMachineTimeA σ) (C : CounterStepA σ) + (I : CurInvA σ) (R : OffReadyA σ) (Ac : ActIdleA σ) : + sched.update_timestamp st ar s a ⦃ r => ∃ τ, + fl4UpdateTimestamp σ = ({((), τ)}, False) ∧ YieldQueueRelA (st := st) Z m κ r.1 τ ∧ + Clock r.2 τ.machineState ∧ ValidMachineTimeA τ ∧ + CurInvA τ ∧ OffReadyA τ ∧ ActIdleA τ ∧ + τ.curTime = rawNextA σ.machineState ∧ τ.consumedTime = σ.consumedTime + (τ.curTime - σ.curTime) ∧ + MFrA (st := st) Z m s r.1 σ τ ⦄ := by + let H := E.sched.toSchedSRN + refine WP.spec_mono (update_timestamp_wordsA H a P hClock) ?_ + rintro ⟨s', a'⟩ ⟨now, hNow, hCl, hS⟩ + change s' = _ at hS + subst s' + let tAddr := L.nodeA s layout.node.CUR_TIME + let cAddr := L.nodeA s layout.node.CONSUMED + let used := core.num.U64.wrapping_add (L.mem s cAddr) (core.num.U64.wrapping_sub now (L.curTime s)) + let s1 := L.wr s tAddr now + let s2 := L.wr s1 cAddr used + let τ := fl4TimestampState (rawNextA σ.machineState) { σ with machineState := rawStateA σ.machineState } + obtain ⟨alT, alC, ne⟩ := clockNodeWordsA H + have nv := fun e : tAddr.val = cAddr.val => ne (UScalar.eq_of_val_eq e) + have w : WritesIn st s s2 (fun b => b = tAddr ∨ b = cAddr) := + (writesIn_wr (st := st) (s := s) (v := now) alT).trans (writesIn_wr (st := st) (s := s1) (v := used) alC) + have oT : layout.node.CUR_TIME.val = 48 := by unfold layout.node.CUR_TIME; rfl + have oC : layout.node.CONSUMED.val = 40 := by unfold layout.node.CONSUMED; rfl + obtain ⟨nk, gK, tK⟩ := H.keepNodeWordsA (Offsets := fun o => o = layout.node.CUR_TIME ∨ o = layout.node.CONSUMED) + w (fun o h => by rcases h with rfl | rfl <;> simp [oT, oC]) + (fun b h => Or.inr (h.elim (fun e => ⟨_, Or.inl rfl, e⟩) (fun e => ⟨_, Or.inr rfl, e⟩))) + have nK (c : U64) (hc : c ∈ Z.Cs) (o : U64) (ho : o.val < 4288) (h8 : o.val % 8 = 0) + (h40 : o.val ≠ 40) (h48 : o.val ≠ 48) := + nk c hc o ho h8 (fun k h => by rcases h with rfl | rfl; rwa [oT]; rwa [oC]) + have tw (t : U64) (ht : t ∈ Z.T) (o : U64) + (ho : o = 3520#u64 ∨ o = 3448#u64 ∨ o = 3432#u64 ∨ o = 3464#u64 ∨ + o = 3592#u64 ∨ o = 3480#u64 ∨ o = 3488#u64) : + L.mem s2 (core.num.U64.wrapping_add t o) = L.mem s (core.num.U64.wrapping_add t o) := by + refine tK t ht o ?_ ?_ <;> rcases ho with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> decide + have keepSC : ∀ b, Aligned b → ScFootA Z.C m σ b → L.mem s2 b = L.mem s b := by + intro b hb hf + refine w.mem_eq hb ?_ + rintro (rfl | rfl) + · have he := H.node_in H.sch.cpuCs (o := layout.node.CUR_TIME) (by rw [oT]; decide) + have hh := H.scp.ks_out hf + change _ ≤ tAddr.val ∧ tAddr.val < _ at he + omega + · have he := H.node_in H.sch.cpuCs (o := layout.node.CONSUMED) (by rw [oC]; decide) + have hh := H.scp.ks_out hf + change _ ≤ cAddr.val ∧ cAddr.val < _ at he + omega + have hcore : SchedCoreA st Z.T Z.Cs s2 Z.Q Z.R := + H.core.agree w.1 (fun t ht => ⟨tw t ht 3480#u64 (by simp), tw t ht 3488#u64 (by simp)⟩) + (fun c hc o ho h8 hne => nK c hc o ho h8 (by omega) (by omega)) + (fun t ht => tw t ht 3592#u64 (by simp)) (fun t ht => tw t ht 3520#u64 (by simp)) + (fun t ht => tw t ht 3448#u64 (by simp)) + have hS2 := H.rebuildNodeTimeA (Q' := Z.Q) (R' := Z.R) (σ' := τ) (κ' := κ) w.1 w.2.1 + (fun c hc o h1 h2 _ h40 h48 _ => nK c hc o (by omega) h2 h40 h48) + (fun K hK => gK K (by rcases hK with rfl | rfl <;> decide) (by rcases hK with rfl | rfl <;> decide)) + (fun t ht K hK => tw t ht K (by rcases hK with rfl | rfl | rfl <;> simp)) + (H.scr.frame keepSC (fun _ _ => rfl)) (ScPlacedA.of_kheap H.scp (fun _ _ => rfl)) + (fun t ht x hx => ⟨x, hx, rfl, rfl⟩) + (fun t ht => by obtain ⟨x, hx, e⟩ := H.sch.tcb t ht; rw [scA_eq, tw t ht 3464#u64 (by simp), ← scA_eq]; exact ⟨x, hx, e⟩) + (fun t ht => by rw [scA_eq, tw t ht 3464#u64 (by simp), ← scA_eq]; exact H.rest.scMem t ht) + ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, by simp [τ, fl4TimestampState, hnd]⟩ + (by + change rawNextA σ.machineState = (L.mem s2 tAddr).bv + rw [L.mem_wr_ne alC alT (fun e => nv e.symm), L.mem_wr_same alT, hNow]) + (by + change σ.consumedTime + (rawNextA σ.machineState - σ.curTime) = (L.mem s2 cAddr).bv + rw [L.mem_wr_same alC] + unfold used + rw [core.num.U64.wrapping_add_bv_eq, core.num.U64.wrapping_sub_bv_eq, hNow, H.rest.consumed, H.sch.time] + rfl) + (by + change σ.reprogramTimer = _ + have e : layout.node.REPROGRAM.val = 56 := by unfold layout.node.REPROGRAM; rfl + rw [nK _ H.sch.cpuCs layout.node.REPROGRAM + (by rw [e] <;> decide) (by rw [e] <;> decide) (by rw [e] <;> decide) (by rw [e] <;> decide)] + exact H.rest.repr) + (by + change σ.schedulerAction = _ + have e : layout.node.SCHED_ACTION.val = 16 := by unfold layout.node.SCHED_ACTION; rfl + rw [nK _ H.sch.cpuCs layout.node.SCHED_ACTION + (by rw [e] <;> decide) (by rw [e] <;> decide) (by rw [e] <;> decide) (by rw [e] <;> decide)] + exact H.sch.act) + H.actOn (H.inv.of_kheap rfl rfl) hcore H.rest.rq H.rest.rqDom H.rest.rel H.run + (fun c hc => (keepSC _ (H.scCoreFoot hc).1 (H.scCoreFoot hc).2).trans (H.scCore c hc)) + have M : MFrA (st := st) Z m s s2 σ τ := by + refine ⟨w.mono (fun b h => Or.inr (Or.inl ?_)), SKeepA.of_kheap rfl, SSzA.of_kheap rfl⟩ + rcases h with rfl | rfl + · exact H.node_sks H.sch.cpuCs (by rw [oT]; decide) + · exact H.node_sks H.sch.cpuCs (by rw [oC]; decide) + obtain ⟨S2, M2, Y2⟩ := yieldQueueFrameA E.sched E.states E.mcp E.links (show τ.kheap = σ.kheap from rfl) M.wr + have rd : RdA τ := E.sched.rd.timeMonoA rfl rfl (BitVec.le_trans V C) + exact ⟨τ, fl4UpdateTimestamp_eqA σ, ⟨⟨hS2, rd⟩, S2, M2, Y2⟩, hCl, BitVec.le_refl _, + I.congr rfl rfl rfl rfl, timestampOffsetA σ I R V C, Ac, rfl, rfl, M⟩ + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScUnbAllA.lean b/proofs/Fl4ProofsA64/P3/ScUnbAllA.lean new file mode 100644 index 000000000..c62ccfda7 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScUnbAllA.lean @@ -0,0 +1,175 @@ +/- +P3 (AArch64): TCB unbind and unbind-all for a placed non-idle SC. +Ports x86 `ScUnbind.unbind_tcb_refines` and `unbind_all_tcbs_refines`. +The TCB-bound case uses the existing binding, locality, and non-stalling +selectors. An empty binding is a no-op. No named premises. +-/ +import Fl4ProofsA64.P3.ScUnbTcbA +import Fl4ProofsA64.P3.ScUnbEmptyA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +section Spec +open Fl4SpecA64 + +theorem schedContextUnbindTcb_eqA {σ σ1 σ2 : State Unit} {N T : ObjRef} {xs : SchedContext} {ns : Nat} {xt : Tcb} + (hks : σ.kheap N = some (.schedContext xs ns)) (hxt : xs.scTcb = some T) + (hks2 : σ2.kheap N = some (.schedContext xs ns)) (hkt2 : σ2.kheap T = some (.tcb xt)) + (hrs : Fl4Spec.NonDet.when (T = σ.curThread) (rescheduleRequired (Z := Unit)) σ = ({((), σ1)}, False)) + (hdq : tcbSchedAction (Z := Unit) tcbSchedDequeue T σ1 = ({((), σ2)}, False)) : + schedContextUnbindTcb (Z := Unit) N σ = + ({((), unbStA (releaseRemoveStA σ2 T) N T xs ns xt)}, False) := by + unfold schedContextUnbindTcb + rw [nd_bind_oneSc (getSchedContext_eqA hks), hxt, + nd_bind_oneSc (show Fl4Spec.NonDet.assertOpt (σ := State Unit) (some T) σ = ({(T, σ)}, False) from rfl), + nd_gets_bindSc, nd_bind_oneSc hrs, nd_bind_oneSc hdq, nd_bind_oneSc (tcbReleaseRemove_eqA σ2 T)] + have hne : N ≠ T := fun h => by rw [h, hkt2] at hks2; cases hks2 + have hkt3 : (releaseRemoveStA σ2 T).kheap T = some (.tcb xt) := hkt2 + rw [nd_bind_oneSc (threadSet_eqSc _ hkt3)] + have hks3 : (updKSc (releaseRemoveStA σ2 T) T (.tcb { xt with tcbSchedContext := none })).kheap N = + some (.schedContext xs ns) := by + rw [updKSc_kheap, Function.update_of_ne hne] + exact hks2 + exact updateSchedContext_eqA hks3 _ + +end Spec + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +/-- SC bindings read from either related state agree if their TCB heaps agree. -/ +theorem SchedSRA.scWordEqA {Z Z' : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s s' : S} + {σ σ' : Fl4SpecA64.State Unit} (H : SchedSRA st Z m κ s σ) (H' : SchedSRA st Z' m κ s' σ') + (hk : σ'.kheap = σ.kheap) {t sc : U64} (ht : t ∈ Z.T) (ht' : t ∈ Z'.T) (hsc : L.mem s (scA t) = sc) + (h0 : sc ≠ 0#u64) (hc : sc ∈ Z.C) : L.mem s' (scA t) = sc := by + obtain ⟨x, hx, b⟩ := H.sch.tcb t ht + obtain ⟨x', hx', b'⟩ := H'.sch.tcb t ht' + rw [hk, hx] at hx' + cases hx' + have e := b'.symm.trans (b.trans (congrArg (optScA m) hsc)) + unfold optScA at e + rw [if_neg h0] at e + by_cases hz : L.mem s' (scA t) = 0#u64 + · rw [if_pos hz] at e + cases e + · rw [if_neg hz] at e + exact scRenA_inj m (H'.sch.scOff _ (H'.rest.scMem t ht' hz)) (H.sch.scOff _ hc) (Option.some.inj e) + +set_option maxHeartbeats 600000 in +/-- Current-thread reschedule in the TCB-unbind sequence. -/ +theorem unbindRescheduleA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} + {σ : Fl4SpecA64.State Unit} (H : SchedSRA st Z m κ s σ) (hst : StEqA Z.T (L.mem s) σ) (t : U64) : + (if t = L.curW s then sched.reschedule_required st s else ok s) ⦃ s' => ∃ σ' Q', + Fl4Spec.NonDet.when (specPtr t = σ.curThread) (Fl4SpecA64.rescheduleRequired (Z := Unit)) σ = + ({((), σ')}, False) ∧ σ'.kheap = σ.kheap ∧ SchedSRA st { Z with Q := Q' } m κ s' σ' ∧ + MFrA (st := st) Z m s s' σ σ' ⦄ := by + by_cases h : t = L.curW s + · rw [if_pos h] + refine WP.spec_mono (reschedule_required_refinesA H hst) ?_ + rintro s' ⟨σ', Q', hr, hk, -, hw, H'⟩ + refine ⟨σ', Q', ?_, hk, H', MFrA.of_sw hk hw⟩ + unfold Fl4Spec.NonDet.when + rw [if_pos (by rw [H.sch.curThread, h])] + exact hr + · rw [if_neg h, WP.spec_ok] + refine ⟨σ, Z.Q, ?_, rfl, H, MFrA.refl Z m s σ⟩ + unfold Fl4Spec.NonDet.when + rw [if_neg (fun e => h (specPtr_inj (e.trans H.sch.curThread)))] + rfl + +set_option maxHeartbeats 1800000 in +/-- Bound branch of TCB unbind with the affected thread on the calling core. -/ +theorem sc_unbind_tcb_localA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} + {σ : Fl4SpecA64.State Unit} (H : SchedSRA st Z m κ s σ) (hst : StEqA Z.T (L.mem s) σ) + {sc t : U64} (hsc : sc ∈ Z.C) (hsc0 : sc ≠ 0#u64) + (ht : t ∈ Z.T) (htw : L.mem s (scTcbA sc) = t) (hts : L.mem s (scA t) = sc) + (hon : L.mem s (affA t) = L.cpu s) : + schedcontext.unbind_tcb st s sc ⦃ s' => ∃ σ' Q' R', + Fl4SpecA64.schedContextUnbindTcb (Z := Unit) (scNameA m sc) σ = ({((), σ')}, False) ∧ + SchedSRA st { Z with Q := Q', R := R' } m κ s' σ' ∧ MFrA (st := st) Z m s s' σ σ' ⦄ := by + obtain ⟨xs, ns, hks, hx⟩ := H.scr.sc sc hsc + obtain ⟨xt, hkt, -⟩ := H.sch.tcb t ht + have ht0 := (tcb_ne01Sc (H.core.tcbs t ht)).1 + have hxs : xs.scTcb = some (specPtr t) := by + rw [hx.tcb, ← scTcbA_eqSc, htw, optRefS_ne ht0] + unfold schedcontext.unbind_tcb + refine WP.spec_bind (obj.Sc.tcb.spec s sc) ?_ + rintro i rfl + rw [htw] + refine WP.spec_bind (obj.Tcb.is_null.spec t) ?_ + rintro b rfl + rw [if_neg (by simpa using ht0)] + refine WP.spec_bind (sched.cur_thread.spec s) ?_ + rintro ct rfl + unfold obj.Tcb.Insts.CoreCmpPartialEqTcb.eq + simp only [bind_ok, decide_eq_true_eq] + refine WP.spec_bind (unbindRescheduleA H hst t) ?_ + rintro s1 ⟨σ1, Q1, hr, hk1, H1, F1⟩ + have hon1 : L.mem s1 (affA t) = L.cpu s1 := by rw [H.aff_eq H1 ht ht, H.cpu_eq H1]; exact hon + have hts1 := H.scWordEqA H1 hk1 ht ht hts hsc0 hsc + refine WP.spec_bind (dequeue_refines_localA H1 ht hon1) ?_ + rintro s2 ⟨σ2, hdq, hk2, -, hsc2, H2, F2⟩ + have hnq := H1.ready_after_dequeueA ht H2 + have hon2 : L.mem s2 (affA t) = L.cpu s2 := by rw [H1.aff_eq H2 ht ht, H1.cpu_eq H2]; exact hon1 + refine WP.spec_bind (release_remove_refines_localA H2 ht hon2) ?_ + rintro s3 ⟨R3, hsc3, H3, F3⟩ + have hks2 : σ2.kheap (scNameA m sc) = some (.schedContext xs ns) := by rw [hk2, hk1]; exact hks + have hkt2 : σ2.kheap (specPtr t) = some (.tcb xt) := by rw [hk2, hk1]; exact hkt + have hts3 : L.mem s3 (scA t) = sc := (hsc3 t ht).trans ((hsc2 t ht).trans hts1) + obtain ⟨H4, F4⟩ := H3.unbindRemovedA hsc ht hsc0 hts3 hnq (releaseRemoveStA_notMem σ2 (specPtr t)) hks2 hkt2 + refine WP.spec_bind (set_sc_specSc s3 t 0#u64) ?_ + rintro s4 rfl + refine WP.spec_mono (set_tcb_specSc _ sc 0#u64) ?_ + rintro s5 rfl + exact ⟨_, _, _, schedContextUnbindTcb_eqA hks hxs hks2 hkt2 hr hdq, H4, + MFrA.trans (MFrA.trans (MFrA.trans F1 F2 rfl rfl) F3 rfl rfl) F4 rfl rfl⟩ + +set_option maxHeartbeats 1600000 in +/-- Unbind all TCBs for a non-idle SC. The bound branch is local and the +stall does nothing, as established by P1's `stall_none` and `stall_keepA`. -/ +theorem sc_unbind_all_tcbs_localA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} + {σ : Fl4SpecA64.State Unit} (H : SchedSRA st Z m κ s σ) (hst : StEqA Z.T (L.mem s) σ) (a : A) + {sc : U64} (hsc : sc ∈ Z.C) (hsc0 : sc ≠ 0#u64) (hni : scNameA m sc ≠ Fl4SpecA64.idleScPtr) + (hbind : ∀ t, L.mem s (scTcbA sc) = t → t ≠ 0#u64 → + t ∈ Z.T ∧ L.mem s (scA t) = sc ∧ L.mem s (affA t) = L.cpu s ∧ L.mem s (core.num.U64.wrapping_add t 3664#u64) = 0#u64) : + schedcontext.unbind_all_tcbs st ar s a sc ⦃ r => r.2 = a ∧ ∃ σ' Q' R', + Fl4SpecA64.schedContextUnbindAllTcbs (Z := Unit) (scNameA m sc) σ = ({((), σ')}, False) ∧ + SchedSRA st { Z with Q := Q', R := R' } m κ r.1 σ' ∧ MFrA (st := st) Z m s r.1 σ σ' ⦄ := by + obtain ⟨xs, ns, hk, hx⟩ := H.scr.sc sc hsc + let t := L.mem s (scTcbA sc) + have hxT : xs.scTcb = optRefS t := by + unfold t + rw [scTcbA_eqSc] + exact hx.tcb + unfold schedcontext.unbind_all_tcbs + refine WP.spec_bind (obj.Sc.tcb.spec s sc) ?_ + rintro i rfl + change (if (t != 0#u64) = true then _ else _) ⦃ _ => _ ⦄ + by_cases ht0 : t = 0#u64 + · rw [if_neg (by simp [ht0]), WP.spec_ok] + refine ⟨rfl, σ, Z.Q, Z.R, ?_, H, MFrA.refl Z m s σ⟩ + exact schedContextUnbindAllTcbs_noneA hk (by rw [hxT, ht0, optRefS_zero]) + · rw [if_pos (by simpa using ht0)] + obtain ⟨ht, hts, hon, hv⟩ := hbind t rfl ht0 + have hstall := stall_keepA (ar := ar) a (H.core.tcbs t ht) hon hv + (WritesIn.refl s fun _ => False) (by simp) (by simp) + rw [hstall, bind_ok] + refine WP.spec_bind (sc_unbind_tcb_localA H hst hsc hsc0 ht rfl hts hon) ?_ + rintro s' ⟨σ', Q', R', hu, H', F⟩ + rw [WP.spec_ok] + refine ⟨rfl, σ', Q', R', ?_, H', F⟩ + unfold Fl4SpecA64.schedContextUnbindAllTcbs + rw [nd_bind_oneSc (getSchedContext_eqA hk), hxT, optRefS_ne ht0] + unfold Fl4Spec.NonDet.when + rw [if_pos ⟨by simp, hni⟩] + exact hu + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScUnbComposeA.lean b/proofs/Fl4ProofsA64/P3/ScUnbComposeA.lean new file mode 100644 index 000000000..50f558a88 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScUnbComposeA.lean @@ -0,0 +1,235 @@ +/- +P3 (AArch64): Unbind execution, including a bound local TCB and a live +notification (x86 ScDecodeSc.unbind_invoke_refines). The notification +and head-reply words are explicit in the final frame. +-/ +import Fl4ProofsA64.P3.ScNtfnLinkA +import Fl4ProofsA64.P3.ScUnbAllA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section +open Fl4SpecA64 + +theorem scOpt_injA {a b : U64} (h : optRefS a = optRefS b) : a = b := by + by_cases ha : a = 0#u64 <;> by_cases hb : b = 0#u64 + · rw [ha, hb] + · rw [ha, optRefS_zero, optRefS_ne hb] at h + cases h + · rw [hb, optRefS_zero, optRefS_ne ha] at h + cases h + · rw [optRefS_ne ha, optRefS_ne hb] at h + exact specPtr_inj (Option.some.inj h) + +private theorem scUnbindAll_scA {σ σ' : State Unit} {p : ObjRef} {xs : SchedContext} {ns : Nat} + (hks : σ.kheap p = some (.schedContext xs ns)) (hni : p ≠ idleScPtr) + (hne : xs.scTcb ≠ some σ.curThread) + (ht : ∀ t, xs.scTcb = some t → ∃ xt, σ.kheap t = some (.tcb xt) ∧ xt.tcbDomain = 0) + (he : schedContextUnbindAllTcbs (Z := Unit) p σ = ({((), σ')}, False)) : + σ'.kheap p = some (.schedContext { xs with scTcb := none } ns) := by + cases h : xs.scTcb with + | none => + rw [schedContextUnbindAllTcbs_noneA hks h] at he + have e := congrArg Prod.snd (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst he)) + change σ = σ' at e + rw [← e] + have hx : { xs with scTcb := none } = xs := by cases xs; simp_all + rwa [hx] + | some t => + obtain ⟨xt, hk, hd⟩ := ht t h + have hct : t ≠ σ.curThread := fun e => hne (by rw [h, e]) + let σq := setRqA σ xt.tcbPriority (tcbSchedDequeue t (σ.readyQueues 0 xt.tcbPriority)) + have hrs : Fl4Spec.NonDet.when (t = σ.curThread) (rescheduleRequired (Z := Unit)) σ = + ({((), σ)}, False) := by + unfold Fl4Spec.NonDet.when + rw [if_neg hct] + rfl + have hdq := tcbSchedAction_eqA hk hd tcbSchedDequeue + have hU := schedContextUnbindTcb_eqA hks h (σ2 := σq) hks hk hrs hdq + unfold schedContextUnbindAllTcbs at he + rw [nd_bind_oneSc (getSchedContext_eqA hks), h, Fl4Spec.NonDet.when, if_pos ⟨by simp, hni⟩, hU] at he + have e := congrArg Prod.snd (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst he)) + have hp : p ≠ t := by intro e; rw [e, hk] at hks; cases hks + change unbStA (releaseRemoveStA σq t) p t xs ns xt = σ' at e + rw [← e, unbStA_kheap hp, if_neg hp, if_pos rfl] + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +theorem scRead_back_szA {Z : XSetsA} {m : McState} {s s' : S} {σ σ' : State Unit} + (hkb : L.ksb s' = L.ksb s) (hsz : SSzA σ σ') {b : U64} : + SReadA st Z m s' σ' b → SReadA st Z m s σ b := by + intro h + rcases h with ⟨c, hc, o, hk, he⟩ | h | h | h | ⟨sc, hc, y, k, hy, hb⟩ + · exact Or.inl ⟨c, hc, o, hk, he.trans (nodeAt_of_ksb hkb c o)⟩ + · exact Or.inr (Or.inl (by rwa [hkb] at h)) + · exact Or.inr (Or.inr (Or.inl (by rwa [hkb] at h))) + · exact Or.inr (Or.inr (Or.inr (Or.inl h))) + · obtain ⟨x, hx⟩ := hsz _ y k hy + exact Or.inr (Or.inr (Or.inr (Or.inr ⟨sc, hc, x, k, hx, hb⟩))) + +private theorem linkClear_frameA {σ σ' : State Unit} {p : ObjRef} {xs : SchedContext} {k : Nat} + (hk : σ.kheap p = some (.schedContext xs k)) + (hf : ∀ n, xs.scNtfn = some n → ∃ x, σ.kheap n = some (.notification x)) + (h : schedContextUnbindNtfn (Z := Unit) p σ = ({((), σ')}, False)) : + (∃ y, σ'.kheap p = some (.schedContext y k) ∧ y.scReplies = xs.scReplies) ∧ + (∀ r x, σ.kheap r = some (.reply x) → σ'.kheap r = some (.reply x)) ∧ SSzA σ σ' := by + cases hn : xs.scNtfn with + | none => + rw [schedContextUnbindNtfn_noneA hk hn] at h + have e := congrArg Prod.snd (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst h)) + change σ = σ' at e + subst σ' + exact ⟨⟨xs, hk, rfl⟩, fun _ _ hr => hr, SSzA.refl σ⟩ + | some q => + obtain ⟨xn, hkn⟩ := hf q hn + rw [schedContextUnbindNtfn_someA hk hkn hn] at h + have e := congrArg Prod.snd (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst h)) + change scNtfnLinkStA σ p q xs k xn false = σ' at e + subst σ' + have hpq : p ≠ q := by intro e; rw [e, hkn] at hk; cases hk + let σn := updKSc σ q (.notification { xn with ntfnSc := none }) + have hks : σn.kheap p = some (.schedContext xs k) := by simp [σn, updKSc, hpq, hk] + have hh : (scNtfnLinkStA σ p q xs k xn false).kheap = + Function.update σn.kheap p (some (.schedContext { xs with scNtfn := none } k)) := by + unfold scNtfnLinkStA + rw [updKSc_kheap] + rfl + refine ⟨⟨{ xs with scNtfn := none }, by rw [hh, Function.update_self], rfl⟩, ?_, ?_⟩ + · intro r x hr + have hrp : r ≠ p := by intro e; rw [e, hk] at hr; cases hr + have hrq : r ≠ q := by intro e; rw [e, hkn] at hr; cases hr + simpa [scNtfnLinkStA, updKSc, hrp, hrq] using hr + · refine SSzA.trans (σ2 := σn) ?_ (SSzA.of_upd hks hh) + intro r y n hr + by_cases he : r = q + · subst r + simp [σn, updKSc] at hr + · exact ⟨y, by simpa [σn, updKSc, he] using hr⟩ + +/-- The Unbind execution sequence after Restart. The original decode's +self-unbind rejection gives `hne`. The bound TCB uses gen 15's local +no-op-stall scope. -/ +theorem sc_unbind_invokeA {A : Type} {ar : machine.Arch A} + {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRA st Z m κ s σ) (hst : StEqA Z.T (L.mem s) σ) (a : A) + {sc : U64} (hc : sc ∈ Z.C) (h0 : sc ≠ 0#u64) (hni : scNameA m sc ≠ idleScPtr) + (hne : L.mem s (scTcbA sc) ≠ L.curW s) + (ht : ∀ t, L.mem s (scTcbA sc) = t → t ≠ 0#u64 → + t ∈ Z.T ∧ L.mem s (scA t) = sc ∧ L.mem s (affA t) = L.cpu s ∧ + L.mem s (core.num.U64.wrapping_add t 3664#u64) = 0#u64) + (hn : let n := L.mem s (scWA sc 40#u64); n ≠ 0#u64 → + (∃ x, σ.kheap (specPtr n) = some (.notification x)) ∧ + Aligned (nScSc n) ∧ ¬ SReadA st Z m s σ (nScSc n)) + (hkr : ∀ x k, σ.kheap (scNameA m sc) = some (.schedContext x k) → ∀ r, + x.scReplies.head? = some r → ∃ xr, σ.kheap r = some (.reply xr)) + (hrw : L.mem s (scReplyA sc) ≠ 0#u64 → Aligned (rNextA (L.mem s (scReplyA sc))) ∧ + ¬ SReadA st Z m s σ (rNextA (L.mem s (scReplyA sc)))) : + (do + let (s1, a1) ← schedcontext.unbind_all_tcbs st ar s a sc + let s2 ← schedcontext.unbind_ntfn st s1 sc + let s3 ← schedcontext.unbind_reply st s2 sc + ok (s3, a1)) ⦃ r => r.2 = a ∧ ∃ σ' Q' R', + invokeSchedContext (Z := Unit) (.invokeSchedContextUnbind (scNameA m sc)) σ = ({([], σ')}, False) ∧ + SchedSRA st { Z with Q := Q', R := R' } m κ r.1 σ' ∧ + WritesIn st s r.1 (fun b => MWrA (st := st) Z m σ s b ∨ + (L.mem s (scWA sc 40#u64) ≠ 0#u64 ∧ b = nScSc (L.mem s (scWA sc 40#u64))) ∨ + (L.mem s (scReplyA sc) ≠ 0#u64 ∧ b = rNextA (L.mem s (scReplyA sc)))) ∧ SSzA σ σ' ⦄ := by + obtain ⟨xs, k, hks, hxs⟩ := H.scr.sc sc hc + let t := L.mem s (scTcbA sc) + let n := L.mem s (scWA sc 40#u64) + let rp := L.mem s (scReplyA sc) + have htc : xs.scTcb = optRefS t := by rw [hxs.tcb, ← scTcbA_eqSc] + have hh : xs.scTcb ≠ some σ.curThread := by + rw [htc, H.sch.curThread] + by_cases e : t = 0#u64 + · rw [e, optRefS_zero] + simp + · rw [optRefS_ne e] + exact fun h => hne (specPtr_inj (Option.some.inj h)) + have htd : ∀ T, xs.scTcb = some T → ∃ xt, σ.kheap T = some (.tcb xt) ∧ xt.tcbDomain = 0 := by + intro T hT + have t0 : t ≠ 0#u64 := by + intro e + rw [htc, e, optRefS_zero] at hT + cases hT + rw [htc, optRefS_ne t0] at hT + obtain ⟨xt, hk, hd, -, -⟩ := H.rest.prio t (ht t rfl t0).1 + exact ⟨xt, Option.some.inj hT ▸ hk, hd⟩ + refine WP.spec_bind (sc_unbind_all_tcbs_localA H hst a hc h0 hni ht) ?_ + rintro ⟨s1, a1⟩ ⟨rfl, σ1, Q1, R1, hu, H1, F1⟩ + let Z1 := { Z with Q := Q1, R := R1 } + have hks1 := scUnbindAll_scA hks hni hh htd hu + obtain ⟨x1, k1, hx1K, hx1⟩ := H1.scr.sc sc hc + rw [hks1] at hx1K + cases hx1K + have n1 : L.mem s1 (scWA sc 40#u64) = n := scOpt_injA (hx1.ntfn.symm.trans hxs.ntfn) + have hp1 : ∀ p x, σ.kheap p = some (.reply x) → σ1.kheap p = some (.reply x) := + fun p x hk => F1.keep.obj p _ hk (fun _ _ e => by cases e) (fun _ e => by cases e) + have hn1 : ∀ p, ({ xs with scTcb := none }).scNtfn = some p → ∃ x, σ1.kheap p = some (.notification x) := by + intro p hp + change ({ xs with scTcb := none }).scNtfn = some p at hp + have e : optRefS n = some p := hxs.ntfn.symm.trans hp + have n0 : n ≠ 0#u64 := by intro e0; rw [e0, optRefS_zero] at e; cases e + rw [optRefS_ne n0] at e + obtain ⟨x, hk⟩ := (hn n0).1 + exact ⟨x, Option.some.inj e ▸ F1.keep.obj _ _ hk (fun _ _ e => by cases e) (fun _ e => by cases e)⟩ + have wpNtfn : schedcontext.unbind_ntfn st s1 sc ⦃ s2 => ∃ σ2, + schedContextUnbindNtfn (Z := Unit) (scNameA m sc) σ1 = ({((), σ2)}, False) ∧ + SchedSRA st Z1 m κ s2 σ2 ∧ + WritesIn st s1 s2 (fun b => MWrA (st := st) Z1 m σ1 s1 b ∨ (n ≠ 0#u64 ∧ b = nScSc n)) ⦄ := by + by_cases hn0 : n = 0#u64 + · rw [hn0] at n1 + refine WP.spec_mono (unbind_ntfn_noneA s1 sc n1) ?_ + rintro _ rfl + exact ⟨σ1, schedContextUnbindNtfn_noneA hks1 (by rw [hx1.ntfn, n1, optRefS_zero]), H1, WritesIn.refl _ _⟩ + · obtain ⟨⟨x, hk⟩, hal, hout⟩ := hn hn0 + have hkn := F1.keep.obj _ _ hk (fun _ _ e => by cases e) (fun _ e => by cases e) + have hf := sc_unbind_ntfn_someA H1 hc h0 hn0 hal + (fun hh => hout (scRead_back_szA F1.wr.1 F1.sz hh)) n1 hkn + refine WP.spec_mono hf ?_ + rintro _ ⟨σ2, h, H2, -, w⟩ + exact ⟨σ2, h, H2, w.mono fun _ h => h.imp id fun e => ⟨hn0, e⟩⟩ + refine WP.spec_bind wpNtfn ?_ + rintro s2 ⟨σ2, hU, H2, w2⟩ + obtain ⟨⟨x2, hks2, hr2⟩, hp2, hz2⟩ := linkClear_frameA hks1 hn1 hU + have hS := F1.sz.trans hz2 + have w12 : WritesIn st s s2 (fun b => MWrA (st := st) Z m σ s b ∨ (n ≠ 0#u64 ∧ b = nScSc n)) := + (F1.wr.trans w2).mono fun _ h => h.elim Or.inl fun h => h.imp (mwr_back rfl rfl F1.sz F1.wr.1) id + obtain ⟨x2', k2, hh2, hre⟩ := H2.scr.sc sc hc + rw [hks2] at hh2 + cases hh2 + have ep : L.mem s2 (scReplyA sc) = rp := by + rw [scReplyA_eqS] + apply scOpt_injA + rw [← hre.reply, hr2, hxs.reply, ← scReplyA_eqS] + refine WP.spec_bind (sc_unbind_replyA H2 hc h0 ?_ ?_) ?_ + · intro x k hx r hr + rw [hks2] at hx + cases hx + rw [hr2] at hr + obtain ⟨xr, hxr⟩ := hkr xs _ hks r hr + exact ⟨xr, hp2 _ _ (hp1 _ _ hxr)⟩ + · intro hp + rw [ep] at hp ⊢ + exact ⟨(hrw hp).1, fun h => (hrw hp).2 (scRead_back_szA w12.1 hS h)⟩ + · rintro s' ⟨x', k', hk', hs', H', w', hz'⟩ + rw [WP.spec_ok] + refine ⟨rfl, _, Q1, R1, ?_, H', ?_, hS.trans hz'⟩ + · change (do schedContextUnbindAllTcbs (Z := Unit) (scNameA m sc) + schedContextUnbindNtfn (scNameA m sc) + schedContextUnbindReply (scNameA m sc) + pure ([] : List Data)) σ = _ + rw [nd_bind_oneSc hu, nd_bind_oneSc hU, nd_bind_oneSc hs'] + rfl + · exact (w12.trans w').mono fun b h => h.elim + (fun h => h.imp id Or.inl) + (fun h => h.imp (mwr_back rfl rfl hS w12.1) (fun e => Or.inr (by rwa [ep] at e))) + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScUnbEmptyA.lean b/proofs/Fl4ProofsA64/P3/ScUnbEmptyA.lean new file mode 100644 index 000000000..12483732d --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScUnbEmptyA.lean @@ -0,0 +1,44 @@ +/- +P3 (AArch64): the empty-binding arm of SC unbind-all. +Ports the zero TCB case of x86 `ScUnbind.unbind_all_tcbs_refines`. +-/ +import Fl4ProofsA64.P3.ScUnbRepA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +theorem schedContextUnbindAllTcbs_noneA {σ : Fl4SpecA64.State Unit} {N : Fl4SpecA64.ObjRef} + {xs : Fl4SpecA64.SchedContext} {ns : Nat} (hk : σ.kheap N = some (.schedContext xs ns)) + (ht : xs.scTcb = none) : + Fl4SpecA64.schedContextUnbindAllTcbs (Z := Unit) N σ = ({((), σ)}, False) := by + unfold Fl4SpecA64.schedContextUnbindAllTcbs + rw [nd_bind_oneSc (getSchedContext_eqA hk), ht] + unfold Fl4Spec.NonDet.when + rw [if_neg (by simp)] + rfl + +/-- The zero TCB word avoids both stalling and unbinding. -/ +theorem sc_unbind_all_tcbs_emptyA {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} {σ : Fl4SpecA64.State Unit} + (H : SchedSRA st Z m κ s σ) (a : A) {sc : U64} (hc : sc ∈ Z.C) + (ht : L.mem s (scTcbA sc) = 0#u64) : + schedcontext.unbind_all_tcbs st ar s a sc ⦃ r => r = (s, a) ∧ + Fl4SpecA64.schedContextUnbindAllTcbs (Z := Unit) (scNameA m sc) σ = ({((), σ)}, False) ∧ + SchedSRA st Z m κ r.1 σ ∧ MFrA (st := st) Z m s r.1 σ σ ⦄ := by + obtain ⟨x, n, hk, hx⟩ := H.scr.sc sc hc + have hnone : x.scTcb = none := by + rw [← optRefS_zero, ← ht, scTcbA_eqSc] + exact hx.tcb + unfold schedcontext.unbind_all_tcbs + refine WP.spec_bind (obj.Sc.tcb.spec s sc) ?_ + rintro i rfl + rw [ht] + simp only [bne_self_eq_false, Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + exact ⟨trivial, schedContextUnbindAllTcbs_noneA hk hnone, H, MFrA.refl Z m s σ⟩ + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScUnbInvokeA.lean b/proofs/Fl4ProofsA64/P3/ScUnbInvokeA.lean new file mode 100644 index 000000000..23b580b75 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScUnbInvokeA.lean @@ -0,0 +1,90 @@ +/- +P3 (AArch64): the successful SchedContext Unbind execution arm when the +SC is bound to neither a TCB nor a notification at the caller restart. +Ports x86 `ScDecodeSc.unbind_invoke_refines`, with the ratified reply effect outside +the ordinary scheduler frame. The SC may still have a reply stack. +-/ +import Fl4ProofsA64.P3.ScUnbEmptyA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +theorem obj.Sc.ntfn.valA (s : S) (sc : U64) : + obj.Sc.ntfn st sc s ⦃ v => v = L.mem s (scWA sc 40#u64) ⦄ := by + unfold obj.Sc.ntfn + step* + simp_all [scWA, layout.sc.NTFN] + +theorem unbind_ntfn_noneA (s : S) (sc : U64) (h0 : L.mem s (scWA sc 40#u64) = 0#u64) : + schedcontext.unbind_ntfn st s sc ⦃ r => r = s ⦄ := by + unfold schedcontext.unbind_ntfn + refine WP.spec_bind (obj.Sc.is_null.spec sc) ?_ + rintro b rfl + split + · exact (WP.spec_ok _).2 rfl + · refine WP.spec_bind (obj.Sc.ntfn.valA s sc) ?_ + rintro i rfl + rw [h0] + simp only [bne_self_eq_false, Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + +theorem schedContextUnbindNtfn_noneA {σ : Fl4SpecA64.State Unit} {sc : Fl4SpecA64.ObjRef} + {x : Fl4SpecA64.SchedContext} {n : Nat} (hk : σ.kheap sc = some (.schedContext x n)) (h0 : x.scNtfn = none) : + Fl4SpecA64.schedContextUnbindNtfn (Z := Unit) sc σ = ({((), σ)}, False) := by + have h : Fl4SpecA64.getScObjRef (Z := Unit) Fl4SpecA64.SchedContext.scNtfn sc σ = ({(none, σ)}, False) := by + unfold Fl4SpecA64.getScObjRef + rw [nd_bind_oneSc (getSchedContext_eqA hk), h0] + rfl + unfold Fl4SpecA64.schedContextUnbindNtfn + rw [nd_bind_oneSc h] + rfl + +/-- The literal post-restart Unbind program, including its SC reply stack. -/ +theorem sc_unbind_invoke_empty_targetsA {A : Type} {ar : machine.Arch A} + {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} {σ : Fl4SpecA64.State Unit} + (H : SchedSRA st Z m κ s σ) (a : A) {sc : U64} (hc : sc ∈ Z.C) (h0 : sc ≠ 0#u64) + (ht : L.mem s (scTcbA sc) = 0#u64) (hn : L.mem s (scWA sc 40#u64) = 0#u64) + (hkr : ∀ x n, σ.kheap (scNameA m sc) = some (.schedContext x n) → ∀ R, x.scReplies.head? = some R → + ∃ xr, σ.kheap R = some (.reply xr)) + (hrw : L.mem s (scReplyA sc) ≠ 0#u64 → Aligned (rNextA (L.mem s (scReplyA sc))) ∧ + ¬ SReadA st Z m s σ (rNextA (L.mem s (scReplyA sc)))) : + (do + let (s1, a1) ← schedcontext.unbind_all_tcbs st ar s a sc + let s2 ← schedcontext.unbind_ntfn st s1 sc + let s3 ← schedcontext.unbind_reply st s2 sc + ok (s3, a1)) ⦃ r => r.2 = a ∧ ∃ σ', + Fl4SpecA64.invokeSchedContext (Z := Unit) (.invokeSchedContextUnbind (scNameA m sc)) σ = ({([], σ')}, False) ∧ + SchedSRA st Z m κ r.1 σ' ∧ + WritesIn st s r.1 (fun b => MWrA (st := st) Z m σ s b ∨ + (L.mem s (scReplyA sc) ≠ 0#u64 ∧ b = rNextA (L.mem s (scReplyA sc)))) ∧ + SSzA σ σ' ⦄ := by + obtain ⟨x, n, hk, hx⟩ := H.scr.sc sc hc + have hN : x.scNtfn = none := by rw [hx.ntfn, hn, optRefS_zero] + refine WP.spec_bind (sc_unbind_all_tcbs_emptyA (ar := ar) H a hc ht) ?_ + rintro r ⟨he, hU, -, -⟩ + subst r + change (do + let s2 ← schedcontext.unbind_ntfn st s sc + let s3 ← schedcontext.unbind_reply st s2 sc + ok (s3, a)) ⦃ _ => _ ⦄ + refine WP.spec_bind (unbind_ntfn_noneA s sc hn) ?_ + intro s2 he2 + subst s2 + refine WP.spec_bind (sc_unbind_replyA H hc h0 hkr hrw) ?_ + rintro s3 ⟨x', n', hk', hR, H', hw, hz⟩ + rw [WP.spec_ok] + refine ⟨rfl, _, ?_, H', hw, hz⟩ + change (do + Fl4SpecA64.schedContextUnbindAllTcbs (Z := Unit) (scNameA m sc) + Fl4SpecA64.schedContextUnbindNtfn (scNameA m sc) + Fl4SpecA64.schedContextUnbindReply (scNameA m sc) + pure ([] : List Fl4SpecA64.Data)) σ = _ + rw [nd_bind_oneSc hU, nd_bind_oneSc (schedContextUnbindNtfn_noneA hk hN), nd_bind_oneSc hR] + rfl + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScUnbObjA.lean b/proofs/Fl4ProofsA64/P3/ScUnbObjA.lean new file mode 100644 index 000000000..00bb4066e --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScUnbObjA.lean @@ -0,0 +1,56 @@ +/- +P3 (AArch64): SchedContext UnbindObject, successful local thread arm. +Ports the thread branch of x86 `ScDecodeSc.unbind_tcb_invoke`. +The abstract decoder reads a related thread cap. The execution lemma starts +after the caller restart and uses the existing TCB-unbind relation. +-/ +import Fl4ProofsA64.P3.ScUnbAllA +import Fl4ProofsA64.P3.LookupA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +section Spec +open Fl4SpecA64 Fl4Spec + +theorem decodeUnbindObject_threadA {σ : State Unit} {sc t : ObjRef} {x : SchedContext} {n : Nat} + (hk : σ.kheap sc = some (.schedContext x n)) (ht : x.scTcb = some t) (hcur : t ≠ σ.curThread) + (rest : List Cap) : + (decodeSchedContextUnbindObject (Z := Unit) sc (.threadCap t :: rest)).run σ = + ({(.ok (.invokeSchedContextUnbindObject sc (.threadCap t)), σ)}, False) := by + unfold decodeSchedContextUnbindObject + simp only [List.length_cons, Nat.succ_ne_zero, NonDetE.whenE, ↓reduceIte, NonDetE.returnOk, ndE_pure_bindA, + List.head?_cons, the_someA] + rw [run_bind_okA (run_liftEA (getScTcb_eqSc hk))] + rw [ht] + simp only [ne_eq, not_true_eq_false, NonDetE.whenE, ↓reduceIte, NonDetE.returnOk, ndE_pure_bindA] + have hget : NonDet.gets (fun s : State Unit => s.curThread) σ = ({(σ.curThread, σ)}, False) := rfl + rw [run_bind_okA (run_liftEA hget)] + simp [NonDetE.whenE, hcur, NonDetE.returnOk] + +end Spec + +theorem sc_unbind_object_threadA {S : Type} {st : machine.Store S} [L : LawfulStore st] + {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} {σ : Fl4SpecA64.State Unit} + (H : SchedSRA st Z m κ s σ) (hst : StEqA Z.T (L.mem s) σ) + {sc t : U64} (hsc : sc ∈ Z.C) (h0 : sc ≠ 0#u64) (ht : t ∈ Z.T) (hb : L.mem s (scTcbA sc) = t) + (hs : L.mem s (scA t) = sc) (hon : L.mem s (affA t) = L.cpu s) : + schedcontext.unbind_tcb st s sc ⦃ s' => ∃ σ' Q' R', + Fl4SpecA64.invokeSchedContext (Z := Unit) + (.invokeSchedContextUnbindObject (scNameA m sc) (.threadCap (specPtr t))) σ = ({([], σ')}, False) ∧ + SchedSRA st { Z with Q := Q', R := R' } m κ s' σ' ∧ MFrA (st := st) Z m s s' σ σ' ⦄ := by + refine WP.spec_mono (sc_unbind_tcb_localA H hst hsc h0 ht hb hs hon) ?_ + rintro s' ⟨σ', Q', R', hu, H', F⟩ + refine ⟨σ', Q', R', ?_, H', F⟩ + change (do + Fl4SpecA64.schedContextUnbindTcb (Z := Unit) (scNameA m sc) + pure ([] : List Fl4SpecA64.Data)) σ = _ + rw [nd_bind_oneSc hu] + rfl + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScUnbRepA.lean b/proofs/Fl4ProofsA64/P3/ScUnbRepA.lean new file mode 100644 index 000000000..65313e6cb --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScUnbRepA.lean @@ -0,0 +1,172 @@ +/- +P3 (aarch64, A64-P3-MCS gen 14): **`unbind_reply` refines `sched_context_unbind_reply`** on `SchedSRA` (ruling 03:19Z +gen-14 item 1; x86 `P3/ScCallUnbind`'s `unbind_reply_refines`, ported without x86's `CallRel`: on AArch64 the reply +words and the reply relation are IPC's). + +FL4 (`crates/fl4-core/src/schedcontext.rs:100–106`): if the SC's `REPLY` word `r` is not `0`, `r.NEXT := 0` (the head +reply's link to the SC) then `sc.REPLY := 0`. l4v (`sched_context_unbind_reply`, FL4's port in +`Fl4SpecA64/IpcCancel.lean`): unless `sc_replies = []`, the head reply's `reply_sc := None`, then `sc_replies := []`. + +* **`unbRepStA`** (the spec state after it) and **`schedContextUnbindReply_eqA`**: the single spec outcome, for an SC + whose head reply (if any) is a reply object. +* **`sc_unbind_replyA`** (x86 `unbind_reply_refines`): FL4's `unbind_reply sc` against `sched_context_unbind_reply + (scNameA m sc)`: single outcome `unbRepStA`, `SchedSRA` after (the reply word by `SchedSRA.stable`, the SC word by + `SchedSRA.setReplyA`), FL4 writing in `MWrA` and the head reply's `NEXT` word, `SSzA`. The head reply's `reply_sc` + changes (IPC's object), as in gen 13's `reply_remove_tcb_cutA`. +* **`unbRepStA_not_keep`**: clearing a head reply's live back-link violates `SKeepA`. Since `MFrA.keep` requires + `SKeepA`, the ordinary MCS frame cannot describe this step end to end. + +No named premise. Hypotheses (existing style): `hc` (`sc ∈ Z.C`), `hsc0`, `hkr` (the head of the SC's spec stack is a +reply object: IPC's reply relation), `hrw` (placement: the head reply's `NEXT` word is aligned and not a word +`SchedSRA` reads, `SReadA`; IPC's reply placement). +-/ +import Fl4ProofsA64.P3.ScPopDonA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +/-! ## The spec -/ + +section Spec +open Fl4SpecA64 + +/-- **The spec state after `sched_context_unbind_reply N`** (module docstring). -/ +def unbRepStA (σ : State Unit) (N : ObjRef) (xs : SchedContext) (ns : Nat) : State Unit := + match xs.scReplies with + | [] => σ + | R :: _ => repStA (rscA σ R none) N xs ns [] + +theorem unbRepStA_nil {σ : State Unit} {N : ObjRef} {xs : SchedContext} {ns : Nat} (h : xs.scReplies = []) : + unbRepStA σ N xs ns = σ := by + unfold unbRepStA; rw [h] + +theorem unbRepStA_cons {σ : State Unit} {N : ObjRef} {xs : SchedContext} {ns : Nat} {R : ObjRef} {l : List ObjRef} + (h : xs.scReplies = R :: l) : unbRepStA σ N xs ns = repStA (rscA σ R none) N xs ns [] := by + unfold unbRepStA; rw [h] + +/-- **l4v `sched_context_unbind_reply N`** (module docstring). -/ +theorem schedContextUnbindReply_eqA {σ : State Unit} {N : ObjRef} {xs : SchedContext} {ns : Nat} + (hks : σ.kheap N = some (.schedContext xs ns)) + (hkr : ∀ R, xs.scReplies.head? = some R → ∃ xr, σ.kheap R = some (.reply xr)) : + schedContextUnbindReply (Z := Unit) N σ = ({((), unbRepStA σ N xs ns)}, False) := by + unfold schedContextUnbindReply + rw [nd_bind_oneSc (scReplies_getA hks)] + try beta_reduce + cases hL : xs.scReplies with + | nil => + rw [unbRepStA_nil hL] + unfold Fl4Spec.NonDet.unless + rw [if_pos rfl]; rfl + | cons R l => + obtain ⟨xr, hkR⟩ := hkr R (by rw [hL]; rfl) + rw [unbRepStA_cons hL] + unfold Fl4Spec.NonDet.unless + rw [if_neg (List.cons_ne_nil R l)] + try rw [List.head?_cons] + rw [show the (some R) = R from rfl, nd_bind_oneSc (setReplySc_rscA hkR none)] + exact setScReplies_eqA (rscA_sc.2 hks) [] + +/-- A live reply back-link changes at the head, which the usual MCS spec frame forbids. -/ +theorem unbRepStA_not_keep {σ : State Unit} {N R : ObjRef} {xs : SchedContext} {ns : Nat} + {tail : List ObjRef} {xr : Reply} (hkN : σ.kheap N = some (.schedContext xs ns)) + (hkR : σ.kheap R = some (.reply xr)) (hhead : xs.scReplies = R :: tail) + (hlink : xr.replySc = some N) : ¬ SKeepA σ (unbRepStA σ N xs ns) := by + intro keep + have hne : R ≠ N := by + intro e + rw [e, hkN] at hkR + cases hkR + have hsame := keep.obj R (.reply xr) hkR (fun _ _ e => by cases e) (fun _ e => by cases e) + have hclear : (unbRepStA σ N xs ns).kheap R = some (.reply { xr with replySc := none }) := by + rw [unbRepStA_cons hhead, repStA, updKSc_kheap, Function.update_of_ne hne, + rscA_of hkR, updKSc_kheap, Function.update_self] + have e := congrArg Reply.replySc (KernelObject.reply.inj (Option.some.inj (hclear.symm.trans hsame))) + change none = xr.replySc at e + rw [hlink] at e + cases e + +end Spec + +/-! ## FL4 against the spec -/ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- `unbRepStA` keeps every SC object's size (`SSzA`). -/ +theorem unbRepStA_sz {σ : Fl4SpecA64.State Unit} {N : Fl4SpecA64.ObjRef} {xs : Fl4SpecA64.SchedContext} {ns : Nat} + (hks : σ.kheap N = some (.schedContext xs ns)) : SSzA σ (unbRepStA σ N xs ns) := by + cases hL : xs.scReplies with + | nil => + rw [unbRepStA_nil hL] + exact SSzA.refl σ + | cons R tail => + rw [unbRepStA_cons hL] + have hz : SSzA σ (rscA σ R none) := fun _ y _ h => ⟨y, rscA_sc.1 h⟩ + exact SSzA.trans hz (SSzA.of_upd (rscA_sc.2 hks) rfl) + +/-- **`unbind_reply` refines `sched_context_unbind_reply`** (module docstring; x86 `unbind_reply_refines`). -/ +theorem sc_unbind_replyA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} + {σ : Fl4SpecA64.State Unit} (H : SchedSRA st Z m κ s σ) {sc : U64} (hc : sc ∈ Z.C) (hsc0 : sc ≠ 0#u64) + (hkr : ∀ x n, σ.kheap (scNameA m sc) = some (.schedContext x n) → ∀ R, x.scReplies.head? = some R → + ∃ xr, σ.kheap R = some (.reply xr)) + (hrw : L.mem s (scReplyA sc) ≠ 0#u64 → Aligned (rNextA (L.mem s (scReplyA sc))) ∧ + ¬ SReadA st Z m s σ (rNextA (L.mem s (scReplyA sc)))) : + schedcontext.unbind_reply st s sc ⦃ s' => ∃ x n, σ.kheap (scNameA m sc) = some (.schedContext x n) ∧ + Fl4SpecA64.schedContextUnbindReply (Z := Unit) (scNameA m sc) σ = + ({((), unbRepStA σ (scNameA m sc) x n)}, False) ∧ + SchedSRA st Z m κ s' (unbRepStA σ (scNameA m sc) x n) ∧ + WritesIn st s s' (fun b => MWrA (st := st) Z m σ s b ∨ + (L.mem s (scReplyA sc) ≠ 0#u64 ∧ b = rNextA (L.mem s (scReplyA sc)))) ∧ + SSzA σ (unbRepStA σ (scNameA m sc) x n) ⦄ := by + obtain ⟨x, n, hk, hx⟩ := H.scr.sc sc hc + have hE := schedContextUnbindReply_eqA hk (hkr x n hk) + have hrel : x.scReplies.head? = optRefS (L.mem s (scReplyA sc)) := by rw [scReplyA_eqS]; exact hx.reply + unfold schedcontext.unbind_reply + refine WP.spec_bind (obj.Sc.is_null.spec _) ?_ + rintro b rfl + rw [if_neg (by simpa using hsc0)] + refine WP.spec_bind (obj.Sc.reply.val s sc) ?_ + rintro w rfl + by_cases hw0 : L.mem s (scReplyA sc) = 0#u64 + · have hb : ¬ ((L.mem s (scReplyA sc) != 0#u64) = true) := by simp [hw0] + rw [if_neg hb, WP.spec_ok] + rw [hw0, optRefS_zero] at hrel + refine ⟨x, n, hk, hE, ?_⟩ + rw [unbRepStA_nil (List.head?_eq_none_iff.1 hrel)] + exact ⟨H, WritesIn.refl _ _, SSzA.refl σ⟩ + · have hb : (L.mem s (scReplyA sc) != 0#u64) = true := by simpa using hw0 + rw [if_pos hb] + obtain ⟨hal, hrd⟩ := hrw hw0 + rw [optRefS_ne hw0] at hrel + obtain ⟨R, l, hL⟩ : ∃ R l, x.scReplies = R :: l := by + cases e : x.scReplies with + | nil => rw [e] at hrel; cases hrel + | cons R l => exact ⟨R, l, rfl⟩ + have hR : R = specPtr (L.mem s (scReplyA sc)) := by + rw [hL] at hrel; exact Option.some.inj hrel + refine WP.spec_bind (obj.Reply.set_next.val0 s _ 0#u64) ?_ + rintro s1 rfl + refine WP.spec_mono (obj.Sc.set_reply.spec _ sc 0#u64) ?_ + rintro s2 rfl + have hw1 : WritesIn st s (L.wr s (rNextA (L.mem s (scReplyA sc))) ((0#u64 &&& ~~~1#u64) ||| 0#u64)) + (· = rNextA (L.mem s (scReplyA sc))) := writesIn_wr hal + have H1 := H.stable hw1 (fun a e => by rw [e]; exact hrd) + (SStepA.replyA σ (specPtr (L.mem s (scReplyA sc))) none).1 + (SStepA.replyA σ (specPtr (L.mem s (scReplyA sc))) none).2 + have hz1 : SSzA σ (rscA σ (specPtr (L.mem s (scReplyA sc))) none) := fun p y k h => ⟨y, rscA_sc.1 h⟩ + obtain ⟨H2, F2⟩ := H1.setReplyA hc (rscA_sc.2 hk) 0#u64 [] (by rw [optRefS_zero]; rfl) + refine ⟨x, n, hk, hE, ?_⟩ + rw [unbRepStA_cons hL, hR] + refine ⟨H2, (hw1.trans F2.wr).mono fun b hb => ?_, SSzA.trans hz1 F2.sz⟩ + rcases hb with hb | hb + · exact Or.inr ⟨hw0, hb⟩ + · exact Or.inl (mwr_back rfl rfl hz1 hw1.1 hb) + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScUnbTcbA.lean b/proofs/Fl4ProofsA64/P3/ScUnbTcbA.lean new file mode 100644 index 000000000..b935ad59f --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScUnbTcbA.lean @@ -0,0 +1,169 @@ +/- +P3 (AArch64): SC unbinding after queue removal. Mirrors x86 +`ScUnbind.InvBut.unbound` and reuses A64 `SchedSRA.unbindA`. +The spec-only state replacement in the proof is on a TCB in neither queue. +It lets the state-free relation reuse the existing unbind lemma during the +brief interval in which this TCB can still have an unreleased SC. +No named premises. +-/ +import Fl4ProofsA64.P3.ScRelRmA +import Fl4ProofsA64.P3.ScPopDonA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +section Spec +open Fl4SpecA64 + +private def tcbStateSt (σ : State Unit) (p : ObjRef) (x : Tcb) (ts : ThreadState) : State Unit := + updKSc σ p (.tcb { x with tcbState := ts }) + +private theorem tcbStateSt_step {σ : State Unit} {p : ObjRef} {x : Tcb} + (hk : σ.kheap p = some (.tcb x)) (ts : ThreadState) : SStepA σ (tcbStateSt σ p x ts) := by + let σ' := tcbStateSt σ p x ts + have hhere : σ'.kheap p = some (.tcb { x with tcbState := ts }) := by simp [σ', tcbStateSt] + have hfar : ∀ q, q ≠ p → σ'.kheap q = σ.kheap q := fun q hq => by simp [σ', tcbStateSt, hq] + refine ⟨⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩, rfl, rfl, rfl, fun q y n hq => ?_, fun q y hq => ?_, + fun q y n hq => ?_, fun q y hq => ?_, fun q y hq => ?_⟩ + · exact hfar _ (fun e => by rw [e, hk] at hq; cases hq) + · by_cases e : q = p + · subst q + rw [hk] at hq + cases hq + exact ⟨_, hhere, rfl, rfl, rfl⟩ + · exact ⟨y, (hfar _ e).trans hq, rfl, rfl, rfl⟩ + · exact (hfar q (fun e => by rw [e, hhere] at hq; cases hq)).symm + · by_cases e : q = p + · exact ⟨x, e.symm ▸ hk⟩ + · exact ⟨y, (hfar _ e).symm.trans hq⟩ + · have hq' := hfar q (fun e => by rw [e, hhere] at hq; cases hq) + exact ⟨y, hq'.symm.trans hq, rfl⟩ + +private theorem tcbStateSt_run {σ : State Unit} {p : ObjRef} {x : Tcb} (ts : ThreadState) (h : RunQA σ) + (hnq : ∀ d q, p ∉ σ.readyQueues d q) (hnr : p ∉ σ.releaseQueue) : RunQA (tcbStateSt σ p x ts) := by + have keep : ∀ q, q ≠ p → (tcbStateSt σ p x ts).kheap q = σ.kheap q := by + intro q hq + simp [tcbStateSt, hq] + refine ⟨fun d q u hu => ?_, fun u hu => ?_⟩ + · rw [keep u (fun e => hnq d q (e ▸ hu))] + exact h.1 d q u hu + · rw [keep u (fun e => hnr (e ▸ hu))] + exact h.2 u hu + +private theorem tcbStateSt_rdEx {σ : State Unit} {p : ObjRef} {x : Tcb} (hk : σ.kheap p = some (.tcb x)) + (ts : ThreadState) (h : RdExA σ p) : RdExA (tcbStateSt σ p x ts) p := by + intro u y hn hy hc + have h0 : σ.kheap u = some (.tcb y) := by simpa [tcbStateSt, hn] using hy + exact (h u y hn h0 hc).step (tcbStateSt_step hk ts) h0 + +end Spec + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- A spec-only step on the parts of an object outside the scheduler relation. -/ +private theorem SchedSRN.specStep {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} + {σ σ' : Fl4SpecA64.State Unit} (H : SchedSRN st Z m κ s σ) (hS : SStepA σ σ') (hRun : RunQA σ') : + SchedSRN st Z m κ s σ' := by + have hkc : ∀ c ∈ Z.C, σ'.kheap (scNameA m c) = σ.kheap (scNameA m c) := by + intro c hc + obtain ⟨y, n, hk, -⟩ := H.scr.sc c hc + exact hS.sc _ _ _ hk + have hScr := H.scr.frame (s' := s) (fun _ _ _ => rfl) hkc + refine H.rebuildG (Q' := Z.Q) (R' := Z.R) rfl rfl (fun _ _ _ _ _ _ _ => rfl) (fun _ _ => rfl) + (fun _ _ _ _ => rfl) hScr (H.scp.of_kheap hkc) (fun t _ x hk => ?_) (fun t ht => ?_) + H.rest.scMem ⟨hS.fix.curThread, hS.fix.curSc, hS.fix.idleThread, hS.fix.curTime, hS.fix.consumedTime, rfl, + hS.fix.curDomain, hS.fix.domainTime⟩ + (hS.fix.reprogramTimer.trans H.rest.repr) (hS.act.trans H.sch.act) (fun u hu => H.actOn u (hS.act.symm ▸ hu)) + (H.inv.step hS) H.core (fun p hp => by rw [hS.rq]; exact H.rest.rq p hp) + (fun d p hd => by rw [hS.rq]; exact H.rest.rqDom d p hd) (hS.rel.trans H.rest.rel) hRun (fun _ _ => rfl) + · obtain ⟨y, hy, -, hd, hp⟩ := hS.tcb _ x hk + exact ⟨y, hy, hd, hp⟩ + · obtain ⟨x, hx, hsc⟩ := H.sch.tcb t ht + obtain ⟨y, hy, e, -⟩ := hS.tcb _ x hx + exact ⟨y, hy, e.trans hsc⟩ + +set_option maxHeartbeats 1400000 in +/-- Clear both links after queue removal, restoring the released-or-queued condition. +This is the counterpart of x86 `InvBut.unbound` and the link writes in `unbind_tcb_refines`. -/ +theorem SchedSRB.unbindRemovedA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} + {σ : Fl4SpecA64.State Unit} {sc t : U64} (H : SchedSRB st Z m κ s σ (specPtr t)) + (hsc : sc ∈ Z.C) (ht : t ∈ Z.T) (hsc0 : sc ≠ 0#u64) (hts : L.mem s (scA t) = sc) + (hnq : ∀ d p, specPtr t ∉ σ.readyQueues d p) (hnr : specPtr t ∉ σ.releaseQueue) + {xs : Fl4SpecA64.SchedContext} {ns : Nat} {xt : Fl4SpecA64.Tcb} + (hks : σ.kheap (scNameA m sc) = some (.schedContext xs ns)) (hkt : σ.kheap (specPtr t) = some (.tcb xt)) : + let s' := L.wr (L.wr s (scA t) 0#u64) (scTcbA sc) 0#u64 + let σ' := unbStA σ (scNameA m sc) (specPtr t) xs ns xt + SchedSRA st Z m κ s' σ' ∧ MFrA (st := st) Z m s s' σ σ' := by + let tptr := specPtr t + let scptr := scNameA m sc + let xt0 := { xt with tcbState := Fl4SpecA64.ThreadState.inactive } + let σ0 := tcbStateSt σ tptr xt .inactive + let s' := L.wr (L.wr s (scA t) 0#u64) (scTcbA sc) 0#u64 + let σ1 := unbStA σ0 scptr tptr xs ns xt0 + have hne : scptr ≠ tptr := by + intro e + change scNameA m sc = specPtr t at e + rw [e, hkt] at hks + cases hks + have hk0 : σ0.kheap tptr = some (.tcb xt0) := by simp [σ0, tcbStateSt, xt0] + have hks0 : σ0.kheap scptr = some (.schedContext xs ns) := by + unfold σ0 tcbStateSt + rw [updKSc_kheap, Function.update_of_ne hne] + exact hks + have hS0 := tcbStateSt_step hkt .inactive + have hRd0 := tcbStateSt_rdEx hkt .inactive H.rdEx + have Hr0 : SchedSRA st Z m κ s σ0 := ⟨H.toSchedSRN.specStep hS0 (tcbStateSt_run _ H.run hnq hnr), by + intro u y hy hc + by_cases e : u = tptr + · subst u + rw [hk0] at hy + cases hy + exfalso + rcases hc with h | ⟨_, h⟩ | ⟨_, _, h, -⟩ | ⟨_, _, _, h⟩ <;> + simp [xt0, Fl4SpecA64.runnable] at h + · exact hRd0 u y e hy hc⟩ + obtain ⟨hw, H1⟩ := Hr0.unbindA hsc ht hsc0 hts hnr hks0 hk0 + have hk1 : σ1.kheap tptr = some (.tcb { xt0 with tcbSchedContext := none }) := by + change (unbStA σ0 scptr tptr xs ns xt0).kheap tptr = _ + rw [unbStA_kheap hne, if_pos rfl] + let σ2 := tcbStateSt σ1 tptr { xt0 with tcbSchedContext := none } xt.tcbState + have hk2 : σ2.kheap tptr = some (.tcb { xt with tcbSchedContext := none }) := by + simp [σ2, tcbStateSt, xt0] + have hS2 := tcbStateSt_step hk1 xt.tcbState + have H2 : SchedSRB st Z m κ s' σ2 tptr := + ⟨H1.toSchedSRN.specStep hS2 (tcbStateSt_run _ H1.run hnq hnr), tcbStateSt_rdEx hk1 _ (H1.toB tptr).rdEx⟩ + have hr2 : RelIfBoundSc σ2 tptr := by + intro x p y n hx hscp + rw [hk2] at hx + cases hx + cases hscp + have he : σ2 = unbStA σ scptr tptr xs ns xt := by + have hkEq : Function.update σ1.kheap tptr (some (.tcb { xt with tcbSchedContext := none })) = + (unbStA σ scptr tptr xs ns xt).kheap := by + funext p + by_cases hp : p = tptr + · subst p + rw [Function.update_self, unbStA_kheap hne, if_pos rfl] + · rw [Function.update_of_ne hp] + change (unbStA σ0 scptr tptr xs ns xt0).kheap p = _ + rw [unbStA_kheap hne, unbStA_kheap hne, if_neg hp, if_neg hp] + by_cases hnp : p = scptr + · simp only [if_pos hnp] + · simp only [if_neg hnp] + unfold σ0 tcbStateSt + rw [updKSc_kheap, Function.update_of_ne hp] + change { σ with kheap := Function.update σ1.kheap tptr (some (.tcb { xt with tcbSchedContext := none })) } = + { σ with kheap := (unbStA σ scptr tptr xs ns xt).kheap } + rw [hkEq] + have F := SKeepA.unb hne hks hkt + exact ⟨he ▸ H2.toA hr2, ⟨hw.mono fun b hb => H.toSchedSRN.mwr_two hsc ht hb, F.1, F.2⟩⟩ + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScUnbTopA.lean b/proofs/Fl4ProofsA64/P3/ScUnbTopA.lean new file mode 100644 index 000000000..6b945b726 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScUnbTopA.lean @@ -0,0 +1,193 @@ +/- +P3 (AArch64): Unbind through the concrete and abstract decoders. +The TCB scope is gen 15's local no-op-stall case. The earlier decoder +stall is framed by the existing pending-IPI proof and may change the +architecture state. +-/ +import Fl4ProofsA64.P3.ScUnbComposeA +import Fl4ProofsA64.P3.ScInvokeTopA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section +open Fl4SpecA64 + +private theorem restart_objectA {σ σ' : State Unit} {p : ObjRef} {xc : Tcb} + (hc : σ.kheap p = some (.tcb xc)) + (hk : σ'.kheap = (updKSc σ p (.tcb { xc with tcbState := .restart })).kheap) + {q : ObjRef} {o : KernelObject} (ho : ∀ x, o ≠ .tcb x) : + σ'.kheap q = some o ↔ σ.kheap q = some o := by + rw [hk, updKSc_kheap] + by_cases h : q = p + · subst q + rw [Function.update_self, hc] + constructor <;> intro e <;> exact (ho _ (Option.some.inj e).symm).elim + · rw [Function.update_of_ne h] + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +local notation "wa" => core.num.U64.wrapping_add + +private theorem restart_vcpu_wordA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s s' : S} {σ : State Unit} (H : SchedSRA st Z m κ s σ) + {t ct : U64} (ht : t ∈ Z.T) (hc : ct ∈ Z.T) + (w : WritesIn st s s' (fun a => SWrA (st := st) Z s a ∨ a = stA ct)) : + L.mem s' (wa t 3664#u64) = L.mem s (wa t 3664#u64) := by + have htA := H.core.tcbs t ht + have hfit := htA.fits + have hk := H.tcb_ks ht + have hval : (wa t 3664#u64).val = t.val + 3664 := by + rw [wa_valm] + have : (3664#u64 : U64).val = 3664 := by simp + omega + refine w.mem_eq (field_aligned htA (by simp) (by simp)) ?_ + intro h + change SWrA (st := st) Z s (wa t 3664#u64) ∨ wa t 3664#u64 = stA ct at h + unfold SWrA at h + rcases h with (h | ⟨u, hu, h⟩) | he + · obtain ⟨h1, h2⟩ := h.ks + rw [hval] at h1 h2 + omega + · have huA := H.core.tcbs u hu + rcases h with e | e | e | e + · change wa t 3664#u64 = wa u 3464#u64 at e + exact field_ne htA huA (by simp) (by simp) (fun _ => by simp) e + · change wa t 3664#u64 = wa u 3480#u64 at e + exact field_ne htA huA (by simp) (by simp) (fun _ => by simp) e + · change wa t 3664#u64 = wa u 3488#u64 at e + exact field_ne htA huA (by simp) (by simp) (fun _ => by simp) e + · change wa t 3664#u64 = wa u 3592#u64 at e + exact field_ne htA huA (by simp) (by simp) (fun _ => by simp) e + · rw [stA_eq] at he + exact field_ne htA (H.core.tcbs ct hc) (by simp) (by simp) (fun _ => by simp) he + +/-- Full successful local Unbind through decode, Restart and all three +unlink steps. The bound-TCB branch uses the existing local/non-VCPU scope. -/ +theorem sc_decode_unbind_localA (hA : P0.ArchTotal ar) + {Z : XSetsA} {m : McState} {κ : U64 → U64} {s s1 : S} {σ : State Unit} + (H : SchedSRA st Z m κ s σ) (hst : StEqA Z.T (L.mem s) σ) + {a a1 : A} {sc t ct : U64} (hc : sc ∈ Z.C) (h0 : sc ≠ 0#u64) (hni : scNameA m sc ≠ idleScPtr) + (hct : ct ∈ Z.T) {xc : Tcb} (hkc : σ.kheap (specPtr ct) = some (.tcb xc)) (hr : runnable xc.tcbState = true) + (htSc : obj.Sc.tcb st sc s = ok t) + (he : (if t != 0#u64 then smp.remote_tcb_stall st ar s a t else ok (s, a)) = ok (s1, a1)) + (hq : t != 0#u64 → smp.remote_tcb_stall st ar s a t = ok (s, a)) + (hcur : sched.cur_thread st s1 = ok ct) + (hne : L.mem s1 (scTcbA sc) ≠ ct) + (hbind : ∀ u, L.mem s1 (scTcbA sc) = u → u ≠ 0#u64 → u ∈ Z.T ∧ + L.mem s1 (scA u) = sc ∧ L.mem s1 (affA u) = L.cpu s1 ∧ L.mem s1 (wa u 3664#u64) = 0#u64) + (hn : let n := L.mem s1 (scWA sc 40#u64); n ≠ 0#u64 → + (∃ x, σ.kheap (specPtr n) = some (.notification x)) ∧ + Aligned (nScSc n) ∧ ¬ SReadA st Z m s1 σ (nScSc n)) + (hkr : ∀ x k, σ.kheap (scNameA m sc) = some (.schedContext x k) → ∀ r, + x.scReplies.head? = some r → ∃ xr, σ.kheap r = some (.reply xr)) + (hrr : let r := L.mem s1 (scReplyA sc); r ≠ 0#u64 → + Aligned (rNextA r) ∧ ¬ SReadA st Z m s1 σ (rNextA r)) + {call : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} {label : Data} + (hl : genInvocationType label = .schedContextUnbind) (caps : List Cap) : + schedcontext.decode st ar s a fl4_abi.label.SchedContextUnbind sc call extra ⦃ r => + r.1 = .Ok () ∧ r.2.2 = a1 ∧ ∃ σ2 σ' Q' R', + (decodeSchedContextInvocation (Z := Unit) label (scNameA m sc) caps).run σ = + ({(.ok (.invokeSchedContextUnbind (scNameA m sc)), σ)}, False) ∧ + setThreadState (Z := Unit) (specPtr ct) .restart σ = ({((), σ2)}, False) ∧ + invokeSchedContext (Z := Unit) (.invokeSchedContextUnbind (scNameA m sc)) σ2 = ({([], σ')}, False) ∧ + SchedSRA st { Z with Q := Q', R := R' } m κ r.2.1 σ' ∧ + WritesIn st s r.2.1 (fun b => MWrA (st := st) Z m σ s b ∨ b = stA ct ∨ + (L.mem s1 (scWA sc 40#u64) ≠ 0#u64 ∧ b = nScSc (L.mem s1 (scWA sc 40#u64))) ∨ + (L.mem s1 (scReplyA sc) ≠ 0#u64 ∧ b = rNextA (L.mem s1 (scReplyA sc)))) ⦄ := by + obtain ⟨H1, hst1, F0⟩ := sc_stall_entryA hA H hst he hq + have hct1 : L.curW s1 = ct := ok_inj_ff ((eq_of_spec_fd (sched.cur_thread.spec (L := L) s1)).symm.trans hcur) + obtain ⟨xs, k, hks, hx⟩ := H1.scr.sc sc hc + have hD := decodeUnbind_eqA hks (show xs.scTcb ≠ some σ.curThread from by + rw [hx.tcb, ← scTcbA_eqSc, H1.sch.curThread, hct1] + by_cases e : L.mem s1 (scTcbA sc) = 0#u64 + · rw [e, optRefS_zero] + simp + · rw [optRefS_ne e] + exact fun h => hne (specPtr_inj (Option.some.inj h))) hl caps + rw [sc_decode_unbind_eqA htSc he hcur (eq_of_spec_fd (obj.Sc.tcb.spec (L := L) s1 sc)) hne] + have hv : sched.TS_RESTART.val = tsCodeA .restart := by rw [ts_vals.2.1]; rfl + refine WP.spec_bind (sc_set_runnable_stateA H1 hst1 hct hkc hr (ts := .restart) rfl hv) ?_ + rintro s2 ⟨σ2, hR, H2, hst2, hk2, w1⟩ + have hsc2 := (restart_objectA hkc hk2 (q := scNameA m sc) (o := .schedContext xs k) (fun _ e => by cases e)).2 hks + obtain ⟨y, k₂, hks2, hx2⟩ := H2.scr.sc sc hc + rw [hsc2] at hks2 + cases hks2 + have ht2 : L.mem s2 (scTcbA sc) = L.mem s1 (scTcbA sc) := by + apply scOpt_injA + rw [scTcbA_eqSc, ← hx2.tcb, hx.tcb] + have hn2 : L.mem s2 (scWA sc 40#u64) = L.mem s1 (scWA sc 40#u64) := + scOpt_injA (hx2.ntfn.symm.trans hx.ntfn) + have hp2 : L.mem s2 (scReplyA sc) = L.mem s1 (scReplyA sc) := by + rw [scReplyA_eqS] + exact scOpt_injA (hx2.reply.symm.trans hx.reply) + have hct2 : L.curW s2 = L.curW s1 := by + apply specPtr_inj + rw [← H2.sch.curThread, ← H1.sch.curThread, ← H2.sch.cur, ← H1.sch.cur] + have hsz : SSzA σ σ2 := fun p x n h => + ⟨x, (restart_objectA hkc hk2 (fun _ e => by cases e)).1 h⟩ + have hb2 : ∀ u, L.mem s2 (scTcbA sc) = u → u ≠ 0#u64 → u ∈ Z.T ∧ + L.mem s2 (scA u) = sc ∧ L.mem s2 (affA u) = L.cpu s2 ∧ L.mem s2 (wa u 3664#u64) = 0#u64 := by + intro u h1 h0' + have e : L.mem s1 (scTcbA sc) = u := ht2.symm.trans h1 + obtain ⟨hu, hsc, hon, hvc⟩ := hbind u e h0' + refine ⟨hu, ?_, by rw [H1.aff_eq H2 hu hu, H1.cpu_eq H2]; exact hon, + (restart_vcpu_wordA H1 hu hct w1).trans hvc⟩ + obtain ⟨xu, hk1u, hS⟩ := H1.sch.tcb u hu + obtain ⟨yu, hk2u, hS'⟩ := H2.sch.tcb u hu + have hneU : specPtr u ≠ specPtr ct := fun p => hne (e.trans (specPtr_inj p)) + rw [hk2, updKSc_kheap, Function.update_of_ne hneU, hk1u] at hk2u + cases hk2u + have h := hS'.symm.trans (hS.trans (congrArg (optScA m) hsc)) + unfold optScA at h + rw [if_neg h0] at h + by_cases hz : L.mem s2 (scA u) = 0#u64 + · rw [if_pos hz] at h + cases h + · rw [if_neg hz] at h + exact scRenA_inj m (H2.sch.scOff _ (H2.rest.scMem u hu hz)) (H1.sch.scOff _ hc) (Option.some.inj h) + have hrun := sc_unbind_invokeA (ar := ar) H2 hst2 a1 hc h0 hni (by rwa [ht2, hct2, hct1]) hb2 + (by + dsimp only + rw [hn2] + intro e + obtain ⟨⟨x, hk⟩, hal, hout⟩ := hn e + exact ⟨⟨x, (restart_objectA hkc hk2 (fun _ e => by cases e)).2 hk⟩, hal, + fun h => hout (scRead_back_szA w1.1 hsz h)⟩) + (fun x n hx r hr => by + have hx0 := (restart_objectA hkc hk2 (fun _ e => by cases e)).1 hx + obtain ⟨xr, hk⟩ := hkr x n hx0 r hr + exact ⟨xr, (restart_objectA hkc hk2 (fun _ e => by cases e)).2 hk⟩) + (by + rw [hp2] + intro e + exact ⟨(hrr e).1, fun h => (hrr e).2 (scRead_back_szA w1.1 hsz h)⟩) + obtain ⟨⟨sf, af⟩, hF, hAf, σ', Q', R', hI, H', w, -⟩ := spec_ex_fd hrun + obtain ⟨⟨s3, a3⟩, hU, hRest⟩ := bind_eq_ok hF + obtain ⟨s4, hN, hReply⟩ := bind_eq_ok hRest + obtain ⟨s5, hRp, hOk⟩ := bind_eq_ok hReply + have ho := ok_inj_ff hOk + rcases Prod.mk.inj ho with ⟨rfl, rfl⟩ + rw [hU, bind_ok] + change (do + let u ← schedcontext.unbind_ntfn st s3 sc + let v ← schedcontext.unbind_reply st u sc + ok (core.result.Result.Ok (), v, a3)) ⦃ _ => _ ⦄ + rw [hN, bind_ok, hRp, bind_ok, WP.spec_ok] + refine ⟨rfl, hAf, σ2, σ', Q', R', hD, hR, hI, H', ?_⟩ + have w02 := F0.wr.trans w1 + refine (w02.trans w).mono fun b h => ?_ + rcases h with (h | h | e) | h | e | e + · exact Or.inl h + · exact Or.inl (Or.inr (swr_ksb rfl F0.wr.1 h)) + · exact Or.inr (Or.inl e) + · exact Or.inl (mwr_back rfl rfl hsz w02.1 h) + · exact Or.inr (Or.inr (Or.inl (by rwa [hn2] at e))) + · exact Or.inr (Or.inr (Or.inr (by rwa [hp2] at e))) + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScUnblockA.lean b/proofs/Fl4ProofsA64/P3/ScUnblockA.lean new file mode 100644 index 000000000..566d69c92 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScUnblockA.lean @@ -0,0 +1,312 @@ +/- +P3 (aarch64, A64-P3-MCS gen 4): **the merge loop of `refill_unblock_check`** on the refill ring (x86 +`P3/ScUnblock`'s `overlap_iff`, `merge_eq`, `unblock_loop_refines`, ported to `RingAtA` / `specAtA` in the style of +`ScBudgetA.loop1_refinesA`). + +* **`refillHeadOverlapping_eqA`**, **`overlap_iffA`** (x86 `overlap_iff`): the spec's `refill_head_overlapping` of a + related SC is FL4's `head_overlapping`. +* **`mergeOA`** / **`mergeO_eqA`** (x86 `mergeHd` / `merge_eq`): `merge_overlapping_refills` on the list. +* **`unblock_loop_refinesA`** (x86 `unblock_loop_refines`): FL4's `unblock_check_loop` (fuel at least the ring + size) keeps `RingAtA` and refines `refill_head_overlapping_loop` on `specAtA` (single outcome). +* **`unblock_check_refinesA`** (x86 `unblock_check_refines`): FL4's `sporadic::unblock_check` of an active related SC + has the single outcome of l4v `refill_unblock_check`, keeping `ScStateRelA` / `ScPlacedA`; FL4 writes inside the SC + and the current core's `REPROGRAM` word (`hal` / `hout`: placement, from `SchedSRA`'s `scp.ks_out` and the node + layout at the `SchedSRA` lift, gen 5). + +No named premise. Hypotheses: the ring / fuel bound, `htime` (= `SchedRelNoStA.time`), `hal` / `hout` (placement). +-/ +import Fl4ProofsA64.P3.ScDonA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## The spec -/ + +section Spec +open Fl4SpecA64 +variable {σ : State Unit} {p : ObjRef} {x : SchedContext} {n : Nat} + +theorem refillHeadOverlapping_eqA (hk : σ.kheap p = some (.schedContext x n)) : + refillHeadOverlapping (Z := Unit) p σ = some (decide (x.scRefills.length > 1 ∧ + (the x.scRefills.tail.head?).rTime ≤ (refillHd x).rTime + (refillHd x).rAmount)) := by + show Option.bind (readSchedContext p σ) (fun sc => Fl4Spec.Lookup.oreturn _ σ) = _ + rw [readSchedContext_eqA hk]; rfl + +end Spec + +/-- `merge_overlapping_refills` on FL4's list (x86 `mergeHd`): the head absorbs the next refill's amount. -/ +def mergeOA (rs : List sporadic.Refill) : List sporadic.Refill := + { rs.headD zRA with amount := wa (rs.tail.headD zRA).amount (rs.headD zRA).amount } :: rs.tail.tail + +/-- **`merge_overlapping_refills` on a related SC** (x86 `merge_eq`). -/ +theorem mergeO_eqA {σ : Fl4SpecA64.State Unit} {p : Fl4SpecA64.ObjRef} {x : Fl4SpecA64.SchedContext} {n : Nat} + (hk : σ.kheap p = some (.schedContext x n)) {rs : List sporadic.Refill} (hx : x.scRefills = rs.map toRefillA) + (h2 : 2 ≤ rs.length) : + Fl4SpecA64.mergeOverlappingRefills (Z := Unit) p σ = + ({((), updKSc σ p (.schedContext { x with scRefills := (mergeOA rs).map toRefillA } n))}, False) := by + obtain ⟨a, b, l, rfl⟩ : ∃ a b l, rs = a :: b :: l := by + match rs, h2 with + | a :: b :: l, _ => exact ⟨a, b, l, rfl⟩ + have hne' : x.scRefills ≠ [] := by rw [hx]; simp + unfold Fl4SpecA64.mergeOverlappingRefills + rw [nd_bind_oneSc (refillPopHead_eqA hk hne'), updateRefillHd_eqA (updKSc_same _ _ _), updKSc_updKSc] + congr 4 + simp [hx, mergeOA, Fl4SpecA64.refillHd, Fl4SpecA64.the, Fl4SpecA64.mergeRefill, toRefillA, + core.num.U64.wrapping_add_bv_eq] + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **The spec's overlap test is FL4's** (x86 `overlap_iff`). -/ +theorem overlap_iffA {s : S} {sc : obj.Sc} {sb : Nat} (hW : RefillsWF st s sc sb) {σ : Fl4SpecA64.State Unit} + {p : Fl4SpecA64.ObjRef} {x : Fl4SpecA64.SchedContext} {n : Nat} (hk : σ.kheap p = some (.schedContext x n)) + (hx : x.scRefills = (L.refills s sc).map toRefillA) : + Fl4SpecA64.the (Fl4SpecA64.refillHeadOverlapping (Z := Unit) p σ) = true ↔ L.headOverlapping s sc = true := by + rw [refillHeadOverlapping_eqA hk, the_someA, decide_eq_true_iff, hx, List.length_map, refills_lengthA] + unfold LawfulStore.headOverlapping + by_cases e : L.rhead s sc = L.rtail s sc + · have := (hW.single_iff).1 e + simp [e, this] + · have h2 := hW.size_two e + have e0 : L.refill s sc (L.rhead s sc) = L.rf s sc 0 := (rf0_eqA hW).symm + have e1 : L.refill s sc (nextIdx (L.rmax s sc) (L.rhead s sc)) = L.rf s sc 1 := by + rw [LawfulStore.rf]; congr 1; apply UScalar.eq_of_val_eq + rw [nextIdx_val hW.head_lt, hW.ix_val] + have hl : (L.refills s sc).map toRefillA = toRefillA (L.rf s sc 0) :: toRefillA (L.rf s sc 1) :: + ((List.range (L.size s sc - 2)).map (fun k => toRefillA (L.rf s sc (k + 2)))) := by + unfold LawfulStore.refills + obtain ⟨m, hm⟩ : ∃ m, L.size s sc = m + 2 := ⟨L.size s sc - 2, by omega⟩ + rw [hm, List.range_succ_eq_map, List.range_succ_eq_map] + simp [Function.comp] + have hd : Fl4SpecA64.refillHd x = toRefillA (L.rf s sc 0) := by + unfold Fl4SpecA64.refillHd; rw [hx, hl]; rfl + simp only [e, ite_false, e0, e1, decide_eq_true_iff, hd] + rw [hl] + simp only [List.tail_cons, List.head?_cons, the_someA, toRefillA, u64_le_bvA, + core.num.U64.wrapping_add_bv_eq, List.length_cons, List.length_map, List.length_range] + constructor + · exact fun h => h.2 + · exact fun h => ⟨by omega, h⟩ + +/-- **The merge loop refines `refill_head_overlapping_loop`** (x86 `unblock_loop_refines`), on a ring with fuel at +least its size. -/ +theorem unblock_loop_refinesA {s0 s1 : S} {σ0 : Fl4SpecA64.State Unit} {p : Fl4SpecA64.ObjRef} {sc : U64} + {x : Fl4SpecA64.SchedContext} {n : Nat} (h1 : RingAtA st s0 s1 sc n) (fuel : U64) + (hf : (L.refills s1 sc).length ≤ fuel.val) : + sporadic.unblock_check_loop st s1 sc fuel ⦃ s2 => RingAtA st s0 s2 sc n ∧ + Fl4SpecA64.refillHeadOverlappingLoop (Z := Unit) p (specAtA (st := st) σ0 p sc x n s1) = + ({((), specAtA (st := st) σ0 p sc x n s2)}, False) ⦄ := by + have hkk : ∀ s : S, (specAtA (st := st) σ0 p sc x n s).kheap p = + some (.schedContext { x with scRefills := (L.refills s sc).map toRefillA } n) := fun s => updKSc_same _ _ _ + unfold sporadic.unblock_check_loop + apply loop.spec_decr_nat (measure := fun y => y.2.val) + (inv := fun y => RingAtA st s0 y.1 sc n ∧ (L.refills y.1 sc).length ≤ y.2.val ∧ + Fl4SpecA64.refillHeadOverlappingLoop (Z := Unit) p (specAtA (st := st) σ0 p sc x n s1) = + Fl4SpecA64.refillHeadOverlappingLoop (Z := Unit) p (specAtA (st := st) σ0 p sc x n y.1)) + · rintro ⟨x0, x1⟩ ⟨hR, hle, hsp⟩ + dsimp only at hR hle hsp + have hW := hR.wf + have hC : Fl4SpecA64.the (Fl4SpecA64.refillHeadOverlapping (Z := Unit) p + (specAtA (st := st) σ0 p sc x n x0)) = true ↔ L.headOverlapping x0 sc = true := + overlap_iffA hW (hkk x0) rfl + unfold sporadic.unblock_check_loop.body + refine WP.spec_bind (sporadic.head_overlapping.spec _ _) ?_ + rintro b rfl + split + · rename_i hov + have hneq : L.rhead x0 sc ≠ L.rtail x0 sc := by + intro e; simp [LawfulStore.headOverlapping, e] at hov + have h2 : 2 ≤ (L.refills x0 sc).length := by rw [refills_lengthA]; exact hW.size_two hneq + split + · rename_i hfp + step as ⟨fuel21, fuel21_post⟩ + have hx1 : 0 < x1.val := by have := hfp; rw [GT.gt, UScalar.lt_equiv] at this; simpa using this + obtain ⟨a, c, l, e⟩ : ∃ a c l, L.refills x0 sc = a :: c :: l := by + match h : L.refills x0 sc with + | [] => rw [h] at h2; simp at h2 + | [a] => rw [h] at h2; simp at h2 + | a :: c :: l => exact ⟨a, c, l, rfl⟩ + refine WP.spec_bind (pop_head_ringA hR hneq) ?_ + rintro ⟨old, s2⟩ ⟨hold, hR2, hl2⟩ + have hold' : old = a := by rw [hold, e]; rfl + subst hold' + have htl : L.refills s2 sc = c :: l := by rw [hl2, e]; rfl + refine WP.spec_bind (sporadic.head.spec _ _) ?_ + rintro h h_post + rw [head_eqA hR2.wf, htl] at h_post + simp only [List.headD_cons] at h_post + subst h_post + step as ⟨i, i_post⟩ + refine WP.spec_bind (set_head_ringA hR2 _) ?_ + rintro s3 ⟨hR3, hl3⟩ + have hl3' : L.refills s3 sc = mergeOA (L.refills x0 sc) := by + rw [hl3, htl, e]; simp only [mergeOA, List.tail_cons, List.headD_cons, i_post] + simp only [WP.spec_ok] + refine ⟨hR3, ?_, ?_, ?_⟩ + · rw [hl3', e, fuel21_post]; rw [e] at hle + simp only [mergeOA, List.tail_cons, List.length_cons] at hle ⊢ + omega + · rw [hsp]; unfold Fl4SpecA64.refillHeadOverlappingLoop + rw [wl_stepSc (C := fun _ s => Fl4SpecA64.the (Fl4SpecA64.refillHeadOverlapping (Z := Unit) p s) = true) + (B := fun _ => Fl4SpecA64.mergeOverlappingRefills p) (hC.2 hov) + (mergeO_eqA (hkk x0) rfl h2), updKSc_updKSc] + show _ = Fl4Spec.NonDet.whileLoop _ _ () + (updKSc σ0 p (.schedContext { x with scRefills := (L.refills s3 sc).map toRefillA } n)) + rw [hl3'] + · rw [fuel21_post]; omega + · rename_i hfp + exfalso + have : x1.val = 0 := by + rw [GT.gt, UScalar.lt_equiv] at hfp; simp at hfp; omega + omega + · rename_i hov + simp only [WP.spec_ok] + refine ⟨hR, ?_⟩ + rw [hsp]; unfold Fl4SpecA64.refillHeadOverlappingLoop + exact wl_doneSc (C := fun _ s => Fl4SpecA64.the (Fl4SpecA64.refillHeadOverlapping (Z := Unit) p s) = true) + (B := fun _ => Fl4SpecA64.mergeOverlappingRefills p) (fun h => hov (hC.1 h)) + · exact ⟨h1, hf, rfl⟩ + +/-- The SC footprint after one SC's refills change is the footprint before. -/ +theorem ScFootA.back {C : Set U64} {m : Fl4SpecA64.McState} {σ : Fl4SpecA64.State Unit} {sc : U64} + {x y : Fl4SpecA64.SchedContext} {n : Nat} (hk : σ.kheap (scNameA m sc) = some (.schedContext x n)) {a : U64} + (h : ScFootA C m (updKSc σ (scNameA m sc) (.schedContext y n)) a) : ScFootA C m σ a := by + obtain ⟨c, hc, x2, n2, hk2, ho⟩ := h + by_cases e : scNameA m c = scNameA m sc + · rw [e, updKSc_same] at hk2 + injection hk2 with hk2; injection hk2 with _ hn + exact ⟨c, hc, x, n, by rw [e]; exact hk, by rw [hn]; exact ho⟩ + · rw [updKSc_kheap, Function.update_of_ne e] at hk2 + exact ⟨c, hc, x2, n2, hk2, ho⟩ + +/-- **`unblock_check` refines `refill_unblock_check`** (x86 `unblock_check_refines`), on the SC relation of an +active SC of `C`, given the view's clock is FL4's (`htime`) and the current core's `REPROGRAM` word (`addr`) aligned +and off every related SC (placement). After: the SC relation and placement, FL4 wrote inside the SC and `addr`, and +either nothing changed (gen 5: on either side) or the spec is `σ` with the SC's refills replaced and +`reprogram_timer` set, `addr` holding 1. -/ +theorem unblock_check_refinesA {th : TypedHeapA} {ksb : U64} {C : Set U64} {m : Fl4SpecA64.McState} {s : S} + {σ : Fl4SpecA64.State Unit} (h : ScStateRelA st C m s σ) (P : ScPlacedA th ksb C m σ) {sc : U64} (hc : sc ∈ C) + {x : Fl4SpecA64.SchedContext} {n : Nat} (hk : σ.kheap (scNameA m sc) = some (.schedContext x n)) + (ha : Fl4SpecA64.scActive x = true) (htime : σ.curTime = (L.curTime s).bv) + (hal : Aligned (L.nodeAt s (L.cpu s) layout.node.REPROGRAM)) + (hout : ∀ a, ScFootA C m σ a → a.val ≠ (L.nodeAt s (L.cpu s) layout.node.REPROGRAM).val) : + sporadic.unblock_check st s sc ⦃ s' => ∃ σ', Fl4SpecA64.refillUnblockCheck (Z := Unit) (scNameA m sc) σ = + ({((), σ')}, False) ∧ ScStateRelA st C m s' σ' ∧ ScPlacedA th ksb C m σ' ∧ + WritesIn st s s' (fun b => ScObjA sc n b ∨ b = L.nodeAt s (L.cpu s) layout.node.REPROGRAM) ∧ + L.mem s' (scCoreA sc) = L.mem s (scCoreA sc) ∧ + ((σ' = σ ∧ s' = s) ∨ ∃ y : Fl4SpecA64.SchedContext, y = { x with scRefills := y.scRefills } ∧ + σ' = { updKSc σ (scNameA m sc) (.schedContext y n) with reprogramTimer := true } ∧ + L.mem s' (L.nodeAt s (L.cpu s) layout.node.REPROGRAM) = 1#u64) ⦄ := by + obtain ⟨x', n', hk', hx⟩ := h.sc sc hc + rw [hk] at hk'; cases hk' + have hx1 := hx.strong ha + have hW := hx1.wf + have hne : x.scRefills ≠ [] := by rw [hx1.refills]; exact refills_neA hW + unfold sporadic.unblock_check + refine WP.spec_bind (is_round_robin_refinesA h hc hk) ?_ + rintro b hb + by_cases hrob : b = true + · rw [if_pos hrob, WP.spec_ok] + refine ⟨σ, ?_, h, P, WritesIn.refl _ _, rfl, Or.inl ⟨rfl, rfl⟩⟩ + unfold Fl4SpecA64.refillUnblockCheck + rw [nd_bind_oneSc hb, nd_bind_oneSc (getScRefillReady_eqA hk hne)] + simp only [Fl4Spec.NonDet.when] + rw [if_neg (by simp [hrob])]; rfl + · rw [if_neg hrob] + refine WP.spec_bind (ready_refinesA h htime hc hk ha) ?_ + rintro b1 hb1 + have hspec0 : ∀ σ', Fl4SpecA64.refillUnblockCheck (Z := Unit) (scNameA m sc) σ = σ' ↔ + (Fl4Spec.NonDet.when (b1 = true ∧ ¬ b = true) (do + let ct ← Fl4Spec.NonDet.gets fun s : Fl4SpecA64.State Unit => s.curTime + Fl4SpecA64.updateRefillHd (scNameA m sc) fun r => { r with rTime := ct } + Fl4Spec.NonDet.modify fun s : Fl4SpecA64.State Unit => { s with reprogramTimer := true } + Fl4SpecA64.refillHeadOverlappingLoop (scNameA m sc))) σ = σ' := by + intro σ' + unfold Fl4SpecA64.refillUnblockCheck + rw [nd_bind_oneSc hb, nd_bind_oneSc hb1] + by_cases hrd : b1 = true + · rw [if_pos hrd] + refine WP.spec_bind (sporadic.cur_time.spec s) ?_ + rintro now rfl + refine WP.spec_bind (sporadic.head.spec s sc) ?_ + rintro hd rfl + refine WP.spec_bind (set_head_ringA (RingAtA.refl hW) _) ?_ + rintro s1 ⟨hR1, hl1⟩ + obtain ⟨hS1, hP1, hw1⟩ := h.ringAtA P hc hk hx1 hR1 + refine WP.spec_bind (LawfulStore.cur_cpu_spec s1) ?_ + rintro cpu rfl + refine WP.spec_bind (obj.node_set.spec s1 _ _ _) ?_ + rintro s2 rfl + have eaddr : L.nodeAt s1 (L.cpu s1) layout.node.REPROGRAM = L.nodeAt s (L.cpu s) layout.node.REPROGRAM := by + rw [nodeAt_of_ksb hR1.w.1, hR1.w.2.1] + rw [eaddr] + set addr := L.nodeAt s (L.cpu s) layout.node.REPROGRAM with haddr + set x1 : Fl4SpecA64.SchedContext := { x with scRefills := (L.refills s1 sc).map toRefillA } with hx1d + set σ1 := updKSc σ (scNameA m sc) (.schedContext x1 n) with hσ1 + set σ2 : Fl4SpecA64.State Unit := { σ1 with reprogramTimer := true } with hσ2 + have hw2 : WritesIn st s1 (L.wr s1 addr 1#u64) (· = addr) := writesIn_wr hal + have hS2 : ScStateRelA st C m (L.wr s1 addr 1#u64) σ2 := + hS1.frame (fun a ha hf => hw2.mem_eq ha (fun e => hout a (ScFootA.back hk hf) (by rw [e]))) + (fun _ _ => rfl) + have hP2 : ScPlacedA th ksb C m σ2 := ScPlacedA.of_kheap hP1 (fun _ _ => rfl) + have hk2 : σ2.kheap (scNameA m sc) = some (.schedContext x1 n) := updKSc_same _ _ _ + obtain ⟨x2, n2, hk2', hx2⟩ := hS2.sc sc hc + rw [hk2] at hk2'; cases hk2' + have hx2' := hx2.strong ha + refine WP.spec_bind (obj.Sc.refill_max.spec _ _) ?_ + rintro i rfl + step as ⟨fuel, fuel_post⟩ + have hfuel : (L.refills (L.wr s1 addr 1#u64) sc).length ≤ fuel.val := by + have := hx2'.wf.size_le; have := hx2'.wf.le_abs + have := absMax_lt (n + Fl4SpecA64.minSchedContextBits) + have h1v : (1#u64 : U64).val = 1 := by simp + rw [refills_lengthA, fuel_post, wadd_val (by rw [h1v]; omega), h1v]; omega + refine WP.spec_mono (unblock_loop_refinesA (σ0 := σ2) (p := scNameA m sc) (x := x1) (n := n) + (RingAtA.refl hx2'.wf) fuel hfuel) ?_ + rintro s3 ⟨hR3, hl3⟩ + obtain ⟨hS3, hP3, hw3⟩ := hS2.ringAtA hP2 hc hk2 hx2' hR3 + have e2 : specAtA (st := st) σ2 (scNameA m sc) sc x1 n (L.wr s1 addr 1#u64) = σ2 := by + show updKSc σ2 _ (.schedContext { x1 with scRefills := _ } n) = σ2 + rw [← hx2'.refills]; exact updKSc_selfA hk2 + rw [e2] at hl3 + have hnot : ¬ ScObjA sc n addr := fun ho => hout addr ⟨sc, hc, x, n, hk, ho⟩ rfl + have hcF := hx.hdr (K := 16#u64) (by simp) (by simp) + have hcE : scCoreA sc = scWA sc 16#u64 := by unfold scCoreA scWA layout.sc.CORE; rfl + refine ⟨_, ?_, hS3, hP3, ?_, ?_, Or.inr ⟨{ x with scRefills := (L.refills s3 sc).map toRefillA }, rfl, + by simp only [specAtA, hσ2, hσ1, updKSc, Function.update_idem] <;> rfl, ?_⟩⟩ + · rw [hspec0] + simp only [Fl4Spec.NonDet.when] + rw [if_pos ⟨hrd, hrob⟩, nd_gets_bindSc, nd_bind_oneSc (updateRefillHd_eqA hk _)] + rw [nd_bind_oneSc (m := Fl4Spec.NonDet.modify fun s : Fl4SpecA64.State Unit => { s with reprogramTimer := true }) + rfl] + have ex : { x with scRefills := { Fl4SpecA64.the x.scRefills.head? with rTime := σ.curTime } :: + x.scRefills.tail } = x1 := by + rw [hx1d]; congr 1 + rw [hl1, hx1.refills, refills_consA hW] + simp only [List.map_cons, List.tail_cons, List.head?_cons, the_someA] + rw [← rf0_eqA hW, htime]; rfl + rw [ex]; exact hl3 + · exact ((hw1.trans hw2).trans hw3).mono fun b hb => by + rcases hb with (hb | hb) | hb + · exact Or.inl hb + · exact Or.inr hb + · exact Or.inl hb + · rw [hcE, hR3.hdr (by simp) (by simp), L.mem_wr_ne hal hcF.1 + (fun e => hout _ ⟨sc, hc, x, n, hk, hx.in_obj hcF.2⟩ (by rw [e])), hR1.hdr (by simp) (by simp)] + · rw [hw3.mem_eq hal hnot]; exact L.mem_wr_same hal + · rw [if_neg hrd, WP.spec_ok] + refine ⟨σ, ?_, h, P, WritesIn.refl _ _, rfl, Or.inl ⟨rfl, rfl⟩⟩ + rw [hspec0] + simp only [Fl4Spec.NonDet.when] + rw [if_neg (by simp [hrd])]; rfl + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScWakeDonA.lean b/proofs/Fl4ProofsA64/P3/ScWakeDonA.lean new file mode 100644 index 000000000..61979160e --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScWakeDonA.lean @@ -0,0 +1,310 @@ +/- +P3 (aarch64, A64-P3-MCS gen 7): **`maybe_donate_sched_context` with a donation and the local wake tail with a +donation** (ruling 00:22Z gen-7 item 2; x86 `P3/ScDonateRef`'s `maybe_donate_refines` and `P3/IpcWake`'s +`wakeTail_top`, ported). + +* `SchedSRN.qBit0`: a thread of the current core in no spec ready queue has its `Q_QUEUED` bit clear. +* `SchedSRN.donAddr`: the two donation words are aligned and distinct. +* **`maybe_donate_localA`** (x86 `maybe_donate_refines`, donation case): the thread has no SC, the notification's + SC `sc ∈ Z.C` is bound to no TCB and lies on the current core (**`hcpu`, the ruled `donLocal`**, so `donate`'s + `migrate_tcb` is the no-op): FL4's `maybe_donate_sched_context` (`donate` then `resume`) refines l4v's + `maybe_donate_sc` (`sched_context_donate` then `sched_context_resume`) with a single outcome; `SchedSRA` holds + after with new release lists (`SchedSRA.donateB`, `resume_refinesA`); the facts the rest of the wake tail reads + (clock, TCB states and `t`'s SC, every TCB's state word, "`t` released ⇒ its SC's head not ready"). +* **`wake_tail_donateA`** (`SchedOpsA.wakeTail`, the donation case): `rustWakeTail` against `specWakeTail` (the IPC + line's texts, inline), with `SchedSRA {Z with Q := Q', R := R'}` after. With `wake_tail_nodonateA` (P3/ScTailA) + this covers every case of the local `wakeTail`: `hno` there is the negation of `hts0 ∧ hsc0 ∧ hC ∧ htw0` here. + +Gen 10 (ruling 01:39Z item 1): `maybe_donate_localA` also gives `SKeepA` / `SSzA` of the donation + resume +(`SKeepA.two`); **`SchedSRN.mwr_don`** / **`SchedSRN.mwr_two`** (moved here from `P3/ScFrameA`, `mwr_don` now through +`SchedSRN.qfoot_swr`) put its write set in `MWrA`, **`SchedSRN.don_mfr`** bundles it as `MFrA`, and +`wake_tail_donateA` carries the frame `MFrA` end to end. + +No named premise. Hypotheses: `ht` / `hon` (local thread), `hN` (the notification's `SC` word, a projection of IPC's +`NtfnStateRel.sc`), `hst` (`StEqA`), `hnqs` / `hnq` (IPC's `NotQueued`), `hrunt` (IPC's `x.tcbState = .running`), +the case selectors `hts0` / `hsc0` / `hC` / `htw0`, `hcpu` (`donLocal`), `hmb` (the platform's `MIN_BUDGET`). +-/ +import Fl4ProofsA64.P3.ScDonBA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +/-- A thread of the current core in no spec ready queue has its `Q_QUEUED` bit clear. -/ +theorem SchedSRN.qBit0 {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} {σ : Fl4SpecA64.State Unit} + (H : SchedSRN st Z m κ s σ) {t : U64} (ht : t ∈ Z.T) (hon : L.mem s (affA t) = L.cpu s) + (hnqs : ∀ d p, specPtr t ∉ σ.readyQueues d p) : (L.mem s (inqA t) &&& sched.Q_QUEUED) = 0#u64 := by + by_contra h + have hq := (H.core.queued t ht).1 h + rw [hon] at hq + exact hnqs 0 _ (by rw [H.rest.rq _ (H.core.prio t ht)]; exact List.mem_map_of_mem hq) + +/-- The donation's two words (the SC's `TCB` word, the TCB's `SC` word) are aligned and distinct. -/ +theorem SchedSRN.donAddr {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} {σ : Fl4SpecA64.State Unit} + (H : SchedSRN st Z m κ s σ) {sc t : U64} (hsc : sc ∈ Z.C) (ht : t ∈ Z.T) : + Aligned (scTcbA sc) ∧ Aligned (scA t) ∧ (scA t).val ≠ (scTcbA sc).val := by + obtain ⟨x, n, hk, hx⟩ := H.scr.sc sc hsc + have h24 := hx.hdr (K := 24#u64) (by simp) (by simp) + have hF : ScFootA Z.C m σ (scTcbA sc) := by rw [scTcbA_eqSc]; exact ⟨sc, hsc, x, n, hk, hx.in_obj h24.2⟩ + have hFt := H.scp.th_out hF ((H.thT t).2 ht) + have hT := H.core.tcbs t ht + have hTf := hT.fits + have hTa := hT.align + simp only [ObjKindA.size] at hFt + have hv2 : (scA t).val = t.val + 3464 := by + show (wa t 3464#u64).val = _ + rw [wa_valm]; have : (3464#u64 : U64).val = 3464 := by simp + omega + refine ⟨by rw [scTcbA_eqSc]; exact h24.1, by unfold Aligned; rw [hv2]; omega, by rw [hv2]; omega⟩ + +/-- **The donation + resume write set is in `MWrA`** (gen 9, moved from `P3/ScFrameA`). -/ +theorem SchedSRN.mwr_don {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} {σ : Fl4SpecA64.State Unit} + (H : SchedSRN st Z m κ s σ) {b sc t : U64} (hsc : sc ∈ Z.C) (ht : t ∈ Z.T) + (hb : b = scTcbA sc ∨ b = scA t ∨ L.QFootN s (t :: Z.R (L.cpu s)) (L.cpu s) RelOffs t b ∨ + b = L.nodeAt s (L.cpu s) layout.node.REPROGRAM) : MWrA (st := st) Z m σ s b := by + rcases hb with e | e | e | e + · obtain ⟨x, n, hk, hx⟩ := H.scr.sc sc hsc + have h24 := hx.hdr (K := 24#u64) (by simp) (by simp) + exact Or.inl (by rw [e, scTcbA_eqSc]; exact ⟨sc, hsc, x, n, hk, hx.in_obj h24.2⟩) + · exact Or.inr (Or.inr ⟨t, ht, Or.inl e⟩) + · refine Or.inr (H.qfoot_swr (fun w hw => ?_) H.sch.cpuCs ht e) + rcases List.mem_cons.1 hw with rfl | hw + · exact ht + · exact (H.relAct w hw).1 + · rw [e]; exact Or.inr H.repr_swr + +/-- The unbinding's two words are in `MWrA` (gen 9, moved from `P3/ScFrameA`). -/ +theorem SchedSRN.mwr_two {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} {σ : Fl4SpecA64.State Unit} + (H : SchedSRN st Z m κ s σ) {b sc t : U64} (hsc : sc ∈ Z.C) (ht : t ∈ Z.T) + (hb : b = scTcbA sc ∨ b = scA t) : MWrA (st := st) Z m σ s b := + H.mwr_don hsc ht (hb.elim Or.inl (fun e => Or.inr (Or.inl e))) + +/-- (gen 10) **The donation's frame as `MFrA`.** -/ +theorem SchedSRN.don_mfr {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s s' : S} + {σ σ' : Fl4SpecA64.State Unit} (H : SchedSRN st Z m κ s σ) {sc t : U64} (hsc : sc ∈ Z.C) (ht : t ∈ Z.T) + (hw : WritesIn st s s' (fun b => b = scTcbA sc ∨ b = scA t ∨ + L.QFootN s (t :: Z.R (L.cpu s)) (L.cpu s) RelOffs t b ∨ b = L.nodeAt s (L.cpu s) layout.node.REPROGRAM)) + (hK : SKeepA σ σ' ∧ SSzA σ σ') : MFrA (st := st) Z m s s' σ σ' := + ⟨hw.mono fun _ hb => H.mwr_don hsc ht hb, hK.1, hK.2⟩ + +/-- An SC word read as the spec's SC reference (`optScA`) names the SC. -/ +theorem scNameA_of_optSc {m : Fl4SpecA64.McState} {w : U64} {N : Fl4SpecA64.ObjRef} (h : optScA m w = some N) : + scNameA m w = N := by + unfold optScA at h + split_ifs at h + injection h + +/-- **`maybe_donate_sched_context` with a donation, local case** (module docstring; x86 `maybe_donate_refines`). -/ +theorem maybe_donate_localA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} + {σ : Fl4SpecA64.State Unit} (H : SchedSRA st Z m κ s σ) (a : A) + (hmb : sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) {t n sc : U64} (ht : t ∈ Z.T) + (hon : L.mem s (affA t) = L.cpu s) (hN : NtfnScEqA m n (L.mem s) σ) (hst : StEqA Z.T (L.mem s) σ) + (hnqs : ∀ d p, specPtr t ∉ σ.readyQueues d p) (hnq : specPtr t ∉ σ.releaseQueue) + (hrunt : ∀ x, σ.kheap (specPtr t) = some (.tcb x) → Fl4SpecA64.runnable x.tcbState = true) + (hts0 : L.mem s (scA t) = 0#u64) (hscn : L.mem s (nScSc n) = sc) (hsc0 : sc ≠ 0#u64) (hC : sc ∈ Z.C) + (htw0 : L.mem s (scTcbA sc) = 0#u64) (hcpu : L.mem s (scCoreA sc) = L.cpu s) : + notification.maybe_donate_sched_context st ar s a t n ⦃ r => r.2 = a ∧ ∃ σ' R', + Fl4SpecA64.maybeDonateSc (Z := Unit) (specPtr t) (specPtr n) σ = ({((), σ')}, False) ∧ + σ'.curTime = σ.curTime ∧ + (specPtr t ∈ σ'.releaseQueue → ∀ y k, σ'.kheap (scNameA m sc) = some (.schedContext y k) → + Fl4SpecA64.refillReady σ'.curTime (Fl4SpecA64.refillHd y) = false) ∧ + (∀ u x, σ'.kheap u = some (.tcb x) → ∃ x0, σ.kheap u = some (.tcb x0) ∧ x0.tcbState = x.tcbState) ∧ + (∀ x, σ'.kheap (specPtr t) = some (.tcb x) → x.tcbSchedContext = some (scNameA m sc)) ∧ + (∀ y ∈ Z.T, L.mem r.1 (stA y) = L.mem s (stA y)) ∧ + (∀ p, p ≠ specPtr t → p ≠ scNameA m sc → σ'.kheap p = σ.kheap p) ∧ + (∀ x, σ.kheap (specPtr t) = some (.tcb x) → + σ'.kheap (specPtr t) = some (.tcb { x with tcbSchedContext := some (scNameA m sc) })) ∧ + (SKeepA σ σ' ∧ SSzA σ σ') ∧ + WritesIn st s r.1 (fun b => b = scTcbA sc ∨ b = scA t ∨ L.QFootN s (t :: Z.R (L.cpu s)) (L.cpu s) RelOffs t b ∨ + b = L.nodeAt s (L.cpu s) layout.node.REPROGRAM) ∧ + SchedSRA st { Z with R := R' } m κ r.1 σ' ⦄ := by + obtain ⟨xt, hkt, hsct⟩ := H.sch.tcb t ht + have hxt0 : xt.tcbSchedContext = none := by rw [hsct, hts0]; simp [optScA] + obtain ⟨z, hkn, hzs⟩ := hN + obtain ⟨xs, ns, hks, hxs⟩ := H.scr.sc sc hC + have hzN : z.ntfnSc = some (scNameA m sc) := by + rw [hzs, hscn]; unfold optScA; rw [if_neg hsc0]; rfl + have hni : NotIdleScA m (scNameA m sc) := + ⟨fun e => H.inv.ntfnIdle _ z hkn (by rw [hzN, e]), fun c hc e => H.inv.ntfnIdleOf _ z hkn c hc (by rw [hzN, e])⟩ + have hx0 : xs.scTcb = none := by rw [hxs.tcb, ← scTcbA_eqSc, htw0, optRefS_zero] + have hNT : scNameA m sc ≠ specPtr t := fun e => by rw [e, hkt] at hks; cases hks + obtain ⟨hA1, hA2, hne⟩ := H.donAddr hC ht + have H2 := H.donateB hC ht hsc0 htw0 hts0 hni hks hkt + obtain ⟨-, -, tK, -⟩ := H.donW hC ht ((writesIn_wr hA1).trans (writesIn_wr hA2)) + have hcpu2 : L.cpu (L.wr (L.wr s (scTcbA sc) t) (scA t) sc) = L.cpu s := by + rw [L.cpu_wr, L.cpu_wr] + have hon2 : L.mem (L.wr (L.wr s (scTcbA sc) t) (scA t) sc) (affA t) = + L.cpu (L.wr (L.wr s (scTcbA sc) t) (scA t) sc) := by + rw [hcpu2, show affA t = wa t 3520#u64 from rfl, tK t ht 3520#u64 (by simp) (by simp) (fun _ => by simp)] + exact hon + have hcpuS : L.mem (L.wr (L.wr s (scTcbA sc) t) (scA t) sc) (scCoreA sc) = + L.cpu (L.wr (L.wr s (scTcbA sc) t) (scA t) sc) := by + rw [hcpu2, H2.scCore sc hC, ← H.scCore sc hC]; exact hcpu + have htw2 : L.mem (L.wr (L.wr s (scTcbA sc) t) (scA t) sc) (scTcbA sc) = t := by + rw [L.mem_wr_ne hA2 hA1 hne, L.mem_wr_same hA1] + have hts2 : L.mem (L.wr (L.wr s (scTcbA sc) t) (scA t) sc) (scA t) = sc := L.mem_wr_same hA2 + have hstK : ∀ u ∈ Z.T, L.mem (L.wr (L.wr s (scTcbA sc) t) (scA t) sc) (stA u) = L.mem s (stA u) := fun u hu => by + rw [stA_eq]; exact tK u hu 3328#u64 (by simp) (by simp) (fun _ => by simp) + have hrunt2 : ∀ x, (donStA σ (scNameA m sc) (specPtr t) xs ns xt).kheap (specPtr t) = some (.tcb x) → + Fl4SpecA64.runnable x.tcbState = true := by + intro x hx + rw [donStA_kheap hNT, if_pos rfl] at hx; cases hx; exact hrunt xt hkt + have hst2 : StEqA Z.T (L.mem (L.wr (L.wr s (scTcbA sc) t) (scA t) sc)) + (donStA σ (scNameA m sc) (specPtr t) xs ns xt) := by + intro u hu x hx + rw [hstK u hu] + rw [donStA_kheap hNT] at hx + split_ifs at hx with h1 h2 + · cases hx; exact hst u hu xt (by rw [h1]; exact hkt) + · cases hx + · exact hst u hu x hx + have hspec : ∀ σ', Fl4SpecA64.schedContextResume (Z := Unit) (scNameA m sc) + (donStA σ (scNameA m sc) (specPtr t) xs ns xt) = ({((), σ')}, False) → + Fl4SpecA64.maybeDonateSc (Z := Unit) (specPtr t) (specPtr n) σ = ({((), σ')}, False) := by + intro σ' hres + unfold Fl4SpecA64.maybeDonateSc Fl4SpecA64.getTcbObjRef + rw [nd_bind_oneSc (threadGet_eqSc _ hkt)] + simp only [Fl4Spec.NonDet.when] + rw [if_pos hxt0] + refine (nd_bind_oneSc (getNtfnSc_eqSc hkn) _).trans ?_ + rw [hzs, hscn, + show optScA m sc = some (scNameA m sc) by unfold optScA; rw [if_neg hsc0]; rfl] + show (do + let scTcb ← Fl4SpecA64.getScObjRef Fl4SpecA64.SchedContext.scTcb (scNameA m sc) + if scTcb = none then (do + Fl4SpecA64.schedContextDonate (scNameA m sc) (specPtr t) + Fl4SpecA64.schedContextResume (scNameA m sc)) else Fl4Spec.NonDet.pure ()) σ = _ + rw [nd_bind_oneSc (getScTcb_eqSc hks), hx0, if_pos (show (none : Option Fl4SpecA64.ObjRef) = none from rfl), + nd_bind_oneSc (donate_specA hks hkt hx0)] + exact hres + unfold notification.maybe_donate_sched_context + refine WP.spec_bind (obj.Tcb.sc.spec s t) ?_ + rintro i rfl + rw [if_pos hts0] + refine WP.spec_bind (ntfn_sc_specSc s n) ?_ + rintro i1 rfl + rw [hscn] + refine WP.spec_bind (obj.Sc.is_null.spec _) ?_ + rintro b rfl + rw [if_neg (by simpa using hsc0)] + refine WP.spec_bind (obj.Sc.tcb.spec s _) ?_ + rintro i2 rfl + rw [if_pos htw0] + refine WP.spec_bind (donate_localA s a sc t htw0 (by rw [hon2, hcpuS])) ?_ + rintro r rfl + refine WP.spec_mono (resume_refinesA H2 a hmb hC ht hon2 htw2 hts2 hsc0 hcpuS hni (H2.qBit0 ht hon2 hnqs) + ((H2.relOn ht hon2).2 hnq) hnqs hrunt2 hst2) ?_ + rintro r ⟨hr2, σ', R', hres, hk', -, hct', -, hrel', hst', hwR, H'⟩ + have hwD : WritesIn st s (L.wr (L.wr s (scTcbA sc) t) (scA t) sc) (fun b => b = scTcbA sc ∨ b = scA t) := + (writesIn_wr hA1).trans (writesIn_wr hA2) + refine ⟨hr2, σ', R', hspec σ' hres, hct', fun hm y k hy => ?_, fun u x hx => ?_, fun x hx => ?_, + fun y hy => (hst' y hy).trans (hstK y hy), fun p h1 h2 => ?_, fun x hx => ?_, + SKeepA.two (ys := { xs with scTcb := some (specPtr t) }) (o := some (scNameA m sc)) + (fun p => by rw [hk', donStA_kheap hNT]) hks hkt, (hwD.trans hwR).mono fun b hb => ?_, H'⟩ + · rw [hct']; exact hrel' hm y k (by rw [← hk']; exact hy) + · rw [hk', donStA_kheap hNT] at hx + split_ifs at hx with h1 h2 + · cases hx; exact ⟨xt, by rw [h1]; exact hkt, rfl⟩ + · cases hx + · exact ⟨x, hx, rfl⟩ + · rw [hk', donStA_kheap hNT, if_pos rfl] at hx; cases hx; rfl + · rw [hk', donStA_kheap hNT, if_neg h1, if_neg h2] + · rw [hkt] at hx; cases hx; rw [hk', donStA_kheap hNT, if_pos rfl] + · rcases hb with (e | e) | (e | e) + · exact Or.inl e + · exact Or.inr (Or.inl e) + · refine Or.inr (Or.inr (Or.inl ?_)) + rw [hcpu2] at e + unfold LawfulStore.QFootN LawfulStore.NodeOf at e ⊢ + simpa only [nodeAt_of_ksb (show L.ksb (L.wr (L.wr s (scTcbA sc) t) (scA t) sc) = L.ksb s by + rw [L.ksb_wr, L.ksb_wr]), hcpu2] using e + · rw [hcpu2, nodeAt_of_ksb (show L.ksb (L.wr (L.wr s (scTcbA sc) t) (scA t) sc) = L.ksb s by + rw [L.ksb_wr, L.ksb_wr])] at e + exact Or.inr (Or.inr (Or.inr e)) + +/-- **The local wake tail with a donation** (`SchedOpsA.wakeTail`, the donation case; module docstring). -/ +theorem wake_tail_donateA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} + {σ : Fl4SpecA64.State Unit} (H : SchedSRA st Z m κ s σ) (a : A) + (hmb : sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) {t n sc : U64} (ht : t ∈ Z.T) + (hon : L.mem s (affA t) = L.cpu s) (hN : NtfnScEqA m n (L.mem s) σ) (hst : StEqA Z.T (L.mem s) σ) + (hnqs : ∀ d p, specPtr t ∉ σ.readyQueues d p) (hnq : specPtr t ∉ σ.releaseQueue) + (hrunt : ∀ x, σ.kheap (specPtr t) = some (.tcb x) → Fl4SpecA64.runnable x.tcbState = true) + (hts0 : L.mem s (scA t) = 0#u64) (hscn : L.mem s (nScSc n) = sc) (hsc0 : sc ≠ 0#u64) (hC : sc ∈ Z.C) + (htw0 : L.mem s (scTcbA sc) = 0#u64) (hcpu : L.mem s (scCoreA sc) = L.cpu s) : + (do + let (s4, a2) ← notification.maybe_donate_sched_context st ar s a t n + let b1 ← sched.is_schedulable st s4 t + let s5 ← if b1 then sched.possible_switch_to st s4 t else ok s4 + let s6 ← notification.unblock_sporadic st s5 t + ok (s6, a2)) ⦃ r => r.2 = a ∧ ∃ σ' Q' R', + (do + Fl4SpecA64.maybeDonateSc (Z := Unit) (specPtr t) (specPtr n) + let b ← Fl4Spec.NonDet.gets (Fl4SpecA64.schedulable (Z := Unit) (specPtr t)) + Fl4Spec.NonDet.when b <| Fl4SpecA64.possibleSwitchTo (specPtr t) + let scopt ← Fl4SpecA64.getTcbObjRef Fl4SpecA64.Tcb.tcbSchedContext (specPtr t) + Fl4SpecA64.ifSporadicCurScTestRefillUnblockCheck scopt) σ = ({((), σ')}, False) ∧ + SchedSRA st { Z with Q := Q', R := R' } m κ r.1 σ' ∧ MFrA (st := st) Z m s r.1 σ σ' ⦄ := by + refine WP.spec_bind (maybe_donate_localA H a hmb ht hon hN hst hnqs hnq hrunt hts0 hscn hsc0 hC htw0 hcpu) ?_ + rintro ⟨s3, a3⟩ ⟨hra, σ3, R', hd, -, hrel3, hTk3, hTsc3, hst3, -, -, hK3, hw3, H3⟩ + dsimp only at hra hst3 H3 hw3 + have F3 := H.toSchedSRN.don_mfr hC ht hw3 hK3 + subst hra + refine let_pairSc s3 a3 (fun s4 a2 => do + let b1 ← sched.is_schedulable st s4 t + let s5 ← if b1 then sched.possible_switch_to st s4 t else ok s4 + let s6 ← notification.unblock_sporadic st s5 t + ok (s6, a2)) _ ?_ + beta_reduce + have hon3 : L.mem s3 (affA t) = L.cpu s3 := by + have e1 := H.sch.aff t ht + have e2 := H3.sch.aff t ht + rw [e1] at e2 + injection e2 with e2 + rw [UScalar.eq_of_val_eq e2.symm, show L.cpu s3 = L.cpu s from + UScalar.eq_of_val_eq (H3.sch.core.symm.trans H.sch.core)] + exact hon + have hst3' : StEqA Z.T (L.mem s3) σ3 := fun u hu x hx => by + obtain ⟨x0, h0, e0⟩ := hTk3 _ x hx + rw [hst3 u hu, ← e0]; exact hst u hu x0 h0 + have hsw := switch_if_refinesA H3 t ht hon3 hst3' + rw [is_schedulable_eqA] at hsw ⊢ + simp only [bind_ok] at hsw ⊢ + refine WP.spec_bind hsw ?_ + rintro s5 ⟨σ4, Q', hw2, hk4, hwr4, H4⟩ + obtain ⟨x4, hkt4, hsc4⟩ := H4.sch.tcb t ht + rw [nd_gets_bindSc] at hw2 + have hrq4 : specPtr t ∈ σ4.releaseQueue → ∀ y n, σ4.kheap (scNameA m (L.mem s5 (scA t))) = + some (.schedContext y n) → Fl4SpecA64.refillReady σ4.curTime (Fl4SpecA64.refillHd y) = false := by + intro hm y k hy + have hm3 : specPtr t ∈ σ3.releaseQueue := by rw [← H3.relQ_eq H4 rfl]; exact hm + have hsf : Fl4SpecA64.schedulable (Z := Unit) (specPtr t) σ3 = false := by + by_contra h; exact (schedulable_partsA (by simpa using h)).2 hm3 + have e43 : σ3 = σ4 := by + simp only [Fl4Spec.NonDet.when, hsf, Bool.false_eq_true, ↓reduceIte] at hw2 + exact (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst hw2))).2 + subst e43 + rw [scNameA_of_optSc (hsc4.symm.trans (hTsc3 x4 hkt4))] at hy + exact hrel3 hm y k hy + refine WP.spec_bind (unblock_sporadic_SRA H4 ht false hrq4) ?_ + rintro s6 ⟨σ', h3, H', F⟩ + rw [WP.spec_ok] + refine ⟨rfl, σ', Q', R', ?_, H', + (F3.trans (MFrA.of_sw hk4 hwr4) rfl rfl).trans (F.mfr H4.toSchedSRN) rfl rfl⟩ + have h3' : Fl4SpecA64.ifSporadicCurScTestRefillUnblockCheck (Z := Unit) (optScA m (L.mem s5 (scA t))) σ4 = + ({((), σ')}, False) := h3 + unfold Fl4SpecA64.getTcbObjRef + rw [nd_bind_oneSc hd, nd_gets_bindSc, nd_bind_oneSc hw2, nd_bind_oneSc (threadGet_eqSc _ hkt4), hsc4] + exact h3' + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScYieldBodyA.lean b/proofs/Fl4ProofsA64/P3/ScYieldBodyA.lean new file mode 100644 index 000000000..639e963b1 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScYieldBodyA.lean @@ -0,0 +1,213 @@ +/- +P3 (AArch64): local execution of YieldTo, including either previous-yielder +branch, the local switch, and the immediate or deferred return. +-/ +import Fl4ProofsA64.P3.ScYieldRunA +import Fl4ProofsA64.P3.ScYieldSwitchA +import Fl4ProofsA64.P3.ScYieldResumeA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4SpecA64 Fl4Spec + +local notation "wa" => core.num.U64.wrapping_add + +noncomputable section + +private theorem yieldSpecBodyA (p : ObjRef) : + schedContextYieldTo (Z := Unit) p = (do + completeScYielderA p + schedContextResume p + let b ← yieldSwitchA p + if b then returnConsumed p else pure []) := by + unfold schedContextYieldTo yieldSwitchA completeScYielderA + have assoc {α β γ : Type} (f : SMonad Unit α) (g : α → SMonad Unit β) (k : β → SMonad Unit γ) : + (f >>= g) >>= k = (f >>= fun x => g x >>= k) := funext (nd_bind_assocSc f g k) + simp only [assoc] + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +private theorem yieldFromReadA (s : S) (sc : U64) : + obj.Sc.yield_from st sc s = ok (L.mem s (scYieldFromA sc)) := by + unfold obj.Sc.yield_from + unfold layout.sc.YIELD_FROM + simp only [lift, bind_ok] + rw [show wa sc 56#u64 = scYieldFromA sc by unfold scYieldFromA layout.sc.YIELD_FROM; rfl] + exact L.read_eq _ _ + +/-- The common completion prefix. A zero previous yielder takes no +writes. A nonzero previous yielder receives the reply and loses its link. -/ +theorem yieldPriorA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : YieldQueueRelA (st := st) Z m κ s σ) (a : A) {sc t ct : U64} + (hc : sc ∈ Z.C) (ht : t ∈ Z.T) (hct : ct ∈ Z.T) (hC0 : ∀ c ∈ Z.C, c ≠ 0#u64) + (hb : L.mem s (scTcbA sc) = t) (hs : L.mem s (scA t) = sc) (hy : L.mem s (yieldA ct) = 0#u64) + {x : Tcb} (hx : σ.kheap (specPtr ct) = some (.tcb x)) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) : + let yf := L.mem s (scYieldFromA sc) + (if yf = 0#u64 then ok (s, a) else schedcontext.complete_yield_to st ar s a yf) ⦃ r => + r.2 = a ∧ ∃ σ', + completeScYielderA (scNameA m sc) σ = ({((), σ')}, False) ∧ YieldQueueRelA (st := st) Z m κ r.1 σ' ∧ + SSzA σ σ' ∧ σ'.curThread = σ.curThread ∧ σ'.releaseQueue = σ.releaseQueue ∧ σ'.kheap (specPtr ct) = some (.tcb x) ∧ + L.mem r.1 (scTcbA sc) = t ∧ L.mem r.1 (scA t) = sc ∧ L.mem r.1 (yieldA ct) = 0#u64 ∧ L.mem r.1 (scYieldFromA sc) = 0#u64 ∧ + WritesIn st s r.1 (fun b => MWrA (st := st) Z m σ s b ∨ (yf ≠ 0#u64 ∧ (ScConsumedRegsA yf b ∨ b = yieldA yf))) + ⦄ := by + let yf := L.mem s (scYieldFromA sc) + change (if yf = 0#u64 then _ else _) ⦃ _ => _ ⦄ + obtain ⟨y, n, hk, hyR⟩ := H.sched.scr.sc sc hc + have a56 : scYieldFromA sc = scWA sc 56#u64 := by unfold scYieldFromA scWA layout.sc.YIELD_FROM; rfl + have hfrom : y.scYieldFrom = optRefS yf := by rw [hyR.yieldFrom, ← a56] + by_cases e : yf = 0#u64 + · rw [if_pos e, WP.spec_ok] + refine ⟨rfl, σ, ?_, H, SSzA.refl σ, rfl, rfl, hx, hb, hs, hy, e, WritesIn.refl _ _⟩ + unfold completeScYielderA + rw [nd_bind_oneSc (getScYieldFrom_eqA hk), hfrom, e, optRefS_zero, NonDet.when, if_neg (by simp)] + rfl + · rw [if_neg e] + have hf : y.scYieldFrom = some (specPtr yf) := hfrom.trans (optRefS_ne e) + have ne : ct ≠ yf := by + intro eq + obtain ⟨u, _, hu, h⟩ := H.links.frm sc hc y n hk _ hf + have e' : u = ct := (specPtr_inj hu).trans eq.symm + rw [e', hy] at h + exact hC0 sc hc h.symm + refine WP.spec_mono (yield_complete_liveA H.sched H.states H.mcp H.links a hc hC0 e rfl hmax htu) ?_ + rintro ⟨s', a'⟩ ⟨ha, σ', he, H', S', M', Y', sz, hcur, hrel, tview, sview, w⟩ + obtain ⟨xc, hxc, -, ex⟩ := tview ct hct x hx + have hxc' : σ'.kheap (specPtr ct) = some (.tcb x) := by rwa [ex ne] at hxc + obtain ⟨y', hy', ht', hf'⟩ := sview y n hk + obtain ⟨y'', k, hnew, hz⟩ := H'.scr.sc sc hc + rw [hy'] at hnew + cases hnew + have ht0 : t ≠ 0#u64 := fun e => (H.sched.core.tcbs t ht).ne0 (by rw [e]; rfl) + have ht' : optRefS (L.mem s' (scTcbA sc)) = some (specPtr t) := by + rw [scTcbA_eqSc, ← hz.tcb, ht', hyR.tcb, ← scTcbA_eqSc, hb, optRefS_ne ht0] + have htw : L.mem s' (scTcbA sc) = t := by + by_cases e' : L.mem s' (scTcbA sc) = 0#u64 + · rw [e', optRefS_zero] at ht'; cases ht' + · rw [optRefS_ne e'] at ht'; exact specPtr_inj (Option.some.inj ht') + have hz0 : L.mem s' (scYieldFromA sc) = 0#u64 := by + by_contra h + have hn := hz.yieldFrom + rw [← a56, hf', optRefS_ne h] at hn + cases hn + obtain ⟨v, hv, e1⟩ := H.sched.sch.tcb t ht + obtain ⟨v', hv', e2, -⟩ := tview t ht v hv + obtain ⟨v'', hv'', e3⟩ := H'.sch.tcb t ht + rw [hv'] at hv'' + cases hv'' + have hsc' : optScA m (L.mem s' (scA t)) = some (scNameA m sc) := by rw [← e3, e2, e1, hs]; simp [optScA, hC0 _ hc, scNameA_eq] + have hsw : L.mem s' (scA t) = sc := by + by_cases e' : L.mem s' (scA t) = 0#u64 + · simp [e', optScA] at hsc' + · simp only [optScA, e', ↓reduceIte] at hsc' + exact H'.scp.inj _ (H'.rest.scMem t ht e') _ hc (Option.some.inj hsc') + have hyc : L.mem s' (yieldA ct) = 0#u64 := by + obtain ⟨x', hx', he⟩ := Y'.tcb ct hct + rw [hxc'] at hx' + cases hx' + obtain ⟨x0, hx0, h0⟩ := H.links.tcb ct hct + rw [hx] at hx0 + cases hx0 + rw [hy] at h0 + rw [h0] at he + by_contra hn + simp [optScA, hn] at he + exact ⟨ha, σ', he, ⟨H', S', M', Y'⟩, sz, hcur, hrel, hxc', htw, hsw, hyc, hz0, + w.mono (fun _ h => h.elim Or.inl (fun h => Or.inr ⟨e, h⟩))⟩ + +set_option maxHeartbeats 500000 in +/-- The local YieldTo body, from Restart through the final scheduling state. -/ +theorem yieldBodyA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : YieldQueueRelA (st := st) Z m κ s σ) (a : A) {sc t ct : U64} + (hc : sc ∈ Z.C) (ht : t ∈ Z.T) (hct : ct ∈ Z.T) (hC0 : ∀ c ∈ Z.C, c ≠ 0#u64) (hne : t ≠ ct) + (hc0 : L.mem s (scCoreA sc) = L.cpu s) (hon : L.mem s (affA t) = L.cpu s) (hcur : L.curW s = ct) + (hb : L.mem s (scTcbA sc) = t) (hs : L.mem s (scA t) = sc) (hy : L.mem s (yieldA ct) = 0#u64) + {x : Tcb} (hx : σ.kheap (specPtr ct) = some (.tcb x)) (hr : runnable x.tcbState = true) + (hnr : specPtr ct ∉ σ.releaseQueue) (call : Bool) + (hmb : sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) : + schedcontext.yield_to st ar s a sc call ⦃ r => r.2 = a ∧ ∃ data σ1 σ' Q', + schedContextYieldTo (Z := Unit) (scNameA m sc) σ = ({(data, σ1)}, False) ∧ + ((data = [] ∧ σ' = σ1) ∨ ∃ tm, data = wordsFromTime tm ∧ + (do NonDet.when (call = true) (replyFromKernel (specPtr ct) (0, data)) + setThreadState (specPtr ct) .running) σ1 = ({((), σ')}, False)) ∧ + YieldQueueRelA (st := st) {Z with Q := Q'} m κ r.1 σ' ∧ SSzA σ σ' ∧ + WritesIn st s r.1 (fun b => MWrA (st := st) Z m σ s b ∨ b = yieldA ct ∨ b = stA ct ∨ + (call = true ∧ ScConsumedRegsA ct b) ∨ + (let yf := L.mem s (scYieldFromA sc); yf ≠ 0#u64 ∧ (ScConsumedRegsA yf b ∨ b = yieldA yf))) + ⦄ := by + unfold schedcontext.yield_to + rw [yieldFromReadA, bind_ok, eq_of_spec_fd (obj.Tcb.is_null.spec _), bind_ok] + simp only [decide_eq_true_eq] + refine WP.spec_bind (yieldPriorA H a hc ht hct hC0 hb hs hy hx hmax htu) ?_ + rintro ⟨s1, a1⟩ ⟨ha, σ1, ep, H1, sz1, cur1, rel1, hx1, hb1, hs1, hy1, hf1, w1⟩ + change a1 = a at ha + subst a1 + have hc1 : L.mem s1 (scCoreA sc) = L.cpu s1 := by + rw [H1.sched.scCore sc hc, ← H.sched.scCore sc hc, hc0, H.sched.cpu_eq H1.sched] + have hon1 := yieldAffKeepA H.sched H1.sched ht ht hon + have hcur1 : L.curW s1 = ct := specPtr_inj (H1.sched.sch.curThread.symm.trans + (cur1.trans (H.sched.sch.curThread.trans (congrArg specPtr hcur)))) + refine WP.spec_bind (yield_resume_noopA H1.sched H1.states a hc (hC0 sc hc) ht hon1 hb1 hs1 hmb) ?_ + rintro r ⟨rfl, er⟩ + change (do + let current ← sched.cur_thread st s1 + let (waits, s2) ← schedcontext.yield_switches st s1 sc current + if waits then ok (s2, a) + else + let s3 ← schedcontext.set_consumed st ar s2 a sc current call + let s4 ← sched.set_thread_state st s3 current sched.TS_RUNNING + ok (s4, a)) ⦃ _ => _ ⦄ + have readCur : sched.cur_thread st s1 = ok ct := + (eq_of_spec_fd (sched.cur_thread.spec s1)).trans (congrArg ok hcur1) + rw [readCur, bind_ok] + refine WP.spec_bind (yield_switches_localA H1 hc ht hct hne hC0 hc1 hon1 hcur1 hb1 hy1 hf1 + (fun x' h => by rw [hx1] at h; cases h; exact hr) (rel1.symm ▸ hnr)) ?_ + rintro ⟨waits, s2⟩ ⟨σ2, Q', es, H2, sz2, hkeep, w2⟩ + have hpre : schedContextYieldTo (Z := Unit) (scNameA m sc) σ = + (if !waits then returnConsumed (Z := Unit) (scNameA m sc) else pure []) σ2 := by + rw [yieldSpecBodyA, nd_bind_oneSc ep, nd_bind_oneSc er, nd_bind_oneSc es] + change (if waits then ok (s2, a) else (do + let s3 ← schedcontext.set_consumed st ar s2 a sc ct call + let s4 ← sched.set_thread_state st s3 ct sched.TS_RUNNING + ok (s4, a))) ⦃ _ => _ ⦄ + cases waits + · simp only [Bool.false_eq_true, ↓reduceIte] + have hx2 : σ2.kheap (specPtr ct) = some (.tcb x) := (hkeep rfl).symm ▸ hx1 + have ht' := yieldReturnA H2 a hc hct hx2 hr call hmax htu + have assoc {α β γ : Type} (u : Result α) (f : α → Result β) (g : β → Result γ) : + Std.bind (Std.bind u f) g = Std.bind u (fun x => Std.bind (f x) g) := by + cases u with + | ret x => simp only [bind_ok] + | vis i k => simp only [bind_vis]; congr 1; funext y; cases i; exact PEmpty.elim y + | div => simp only [bind_div] + rw [← assoc] + refine WP.spec_bind ht' ?_ + rintro s3 ⟨tm, σ3, σ', ei, ef, H3, sz3, w3⟩ + refine (WP.spec_ok _).2 ⟨rfl, wordsFromTime tm, σ3, σ', Q', hpre.trans ei, Or.inr ⟨tm, rfl, ef⟩, H3, + (sz1.trans sz2).trans sz3, ?_⟩ + refine ((w1.trans w2).trans w3).mono fun b h => ?_ + rcases h with ((h | h) | h | h) | h | h | h + · exact Or.inl h + · exact Or.inr (Or.inr (Or.inr (Or.inr h))) + · exact Or.inl (mwr_back rfl rfl sz1 w1.1 h) + · exact Or.inr (Or.inl h) + · exact Or.inl (mwr_back rfl rfl (sz1.trans sz2) (w1.trans w2).1 h) + · exact Or.inr (Or.inr (Or.inl h)) + · exact Or.inr (Or.inr (Or.inr (Or.inl h))) + · refine (WP.spec_ok _).2 ⟨rfl, [], σ2, σ2, Q', hpre, Or.inl ⟨rfl, rfl⟩, H2, sz1.trans sz2, ?_⟩ + exact (w1.trans w2).mono fun b h => by + rcases h with (h | h) | h | h + · exact Or.inl h + · exact Or.inr (Or.inr (Or.inr (Or.inr h))) + · exact Or.inl (mwr_back rfl rfl sz1 w1.1 h) + · exact Or.inr (Or.inl h) + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScYieldCompleteA.lean b/proofs/Fl4ProofsA64/P3/ScYieldCompleteA.lean new file mode 100644 index 000000000..e8e03b6d8 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScYieldCompleteA.lean @@ -0,0 +1,319 @@ +/- +P3 (AArch64): finish a previous yield before transferring the SC again. +The live arm charges the SC, gives a one-word reply, and clears both links. +-/ +import Fl4ProofsA64.P3.ScYieldLinkA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4SpecA64 Fl4Spec + +noncomputable section +local notation "wa" => core.num.U64.wrapping_add + +theorem getScYieldFrom_eqA {σ : State Unit} {p : ObjRef} {y : SchedContext} {n : Nat} + (hk : σ.kheap p = some (.schedContext y n)) : + getScObjRef (Z := Unit) SchedContext.scYieldFrom p σ = ({(y.scYieldFrom, σ)}, False) := by + unfold getScObjRef + rw [nd_bind_oneSc (getSchedContext_eqA hk)] + rfl + +private theorem yieldCancel_eqA {σ : State Unit} {p q : ObjRef} {x : Tcb} {y : SchedContext} {n : Nat} + (hk : σ.kheap q = some (.tcb x)) (hs : σ.kheap p = some (.schedContext y n)) + (ht : x.tcbYieldTo = some p) : + schedContextCancelYieldTo (Z := Unit) q σ = ({((), yieldPairStA σ p q x y n false)}, False) := by + have hne : q ≠ p := fun e => by rw [e, hs] at hk; cases hk + unfold schedContextCancelYieldTo getTcbObjRef + rw [nd_bind_oneSc (threadGet_eqSc _ hk), ht] + change (updateSchedContext p _ >>= _) σ = _ + rw [nd_bind_oneSc (updateSchedContext_eqA hs _)] + exact threadSet_eqSc _ (by rwa [updKSc_kheap, Function.update_of_ne hne]) + +theorem yieldConsumed_viewWithA (cap : Ticks) (toUs : Ticks → Word64) {σ : State Unit} {p : ObjRef} {y : SchedContext} {n : Nat} + (hk : σ.kheap p = some (.schedContext y n)) : + ∃ tm y', (∀ (f : SchedContext → Option ObjRef), f = SchedContext.scYieldFrom ∨ f = SchedContext.scTcb → f y' = f y) ∧ + schedContextUpdateConsumedWith (Z := Unit) cap toUs p σ = + ({(tm, updKSc σ p (.schedContext y' n))}, False) := by + unfold schedContextUpdateConsumedWith + rw [nd_bind_oneSc (getSchedContext_eqA hk), nd_pure_bindSc] + split + · refine ⟨toUs cap, {y with scConsumed := y.scConsumed - cap}, (fun f h => by rcases h with rfl | rfl <;> rfl), ?_⟩ + change (updateSchedContext p _ >>= _) σ = _ + rw [nd_bind_oneSc (updateSchedContext_eqA hk _)] + rfl + · refine ⟨toUs y.scConsumed, {y with scConsumed := 0}, (fun f h => by rcases h with rfl | rfl <;> rfl), ?_⟩ + change (updateSchedContext p _ >>= _) σ = _ + rw [nd_bind_oneSc (updateSchedContext_eqA hk _)] + rfl + +theorem yieldConsumed_viewA {σ : State Unit} {p : ObjRef} {y : SchedContext} {n : Nat} + (hk : σ.kheap p = some (.schedContext y n)) : + ∃ tm y', (∀ (f : SchedContext → Option ObjRef), f = SchedContext.scYieldFrom ∨ f = SchedContext.scTcb → f y' = f y) ∧ + schedContextUpdateConsumed (Z := Unit) p σ = + ({(tm, updKSc σ p (.schedContext y' n))}, False) := by + simpa only [schedContextUpdateConsumedWith_eq_l4v] using yieldConsumed_viewWithA maxTicksToUs ticksToUs hk + +/-- Reply to a previously identified yielder, or do nothing when the +SC's YIELD_FROM word is zero. -/ +def completeScYielderWithA (cap : Ticks) (toUs : Ticks → Word64) (p : ObjRef) : SMonad Unit Unit := do + let yf ← getScObjRef SchedContext.scYieldFrom p + NonDet.when (yf ≠ none) <| do + completeYieldToWith cap toUs (the yf) + let remaining ← getScObjRef SchedContext.scYieldFrom p + NonDet.assert (remaining = none) + +/-- Reply to a previously identified yielder, or do nothing when the +SC's YIELD_FROM word is zero. -/ +def completeScYielderA (p : ObjRef) : SMonad Unit Unit := do + let yf ← getScObjRef SchedContext.scYieldFrom p + NonDet.when (yf ≠ none) <| do + completeYieldTo (the yf) + let remaining ← getScObjRef SchedContext.scYieldFrom p + NonDet.assert (remaining = none) + +theorem completeScYielderWith_eq_l4vA (p : ObjRef) : + completeScYielderWithA maxTicksToUs ticksToUs p = completeScYielderA p := by + simp only [completeScYielderWithA, completeScYielderA, completeYieldToWith_eq_l4v] + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +/-- The yielder after the reply: `X0` the zero badge, `X1` the one-word message info, `X2` the consumed time, and +no yield target. -/ +def yieldDoneA (x : Tcb) (d : Data) : Tcb := + { yieldRegA (yieldRegA (yieldRegA x badge_register 0) .x2 d) msg_info_register + (messageInfoToData ⟨natToLen 1, 0, 0, 0⟩) with tcbYieldTo := none } + +/-- At a live previous yielder, the reply only extends the scheduler's +write footprint by that TCB's X0, X1 and X2 and its own yield word. -/ +theorem yield_complete_liveWithA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRA st Z m κ s σ) (hst : StEqA Z.T (L.mem s) σ) + (M : McpRelA Z.T m (L.mem s) σ) (Y : YieldRelA Z.T Z.C m (L.mem s) σ) + (a : A) {sc yf : U64} (hc : sc ∈ Z.C) (hC0 : ∀ c ∈ Z.C, c ≠ 0#u64) + (hy0 : yf ≠ 0#u64) (hy : L.mem s (scYieldFromA sc) = yf) + (cap : Ticks) (toUs : Ticks → Word64) (hmax : cap = core.num.U64.MAX.bv) + (htu : ∀ v, ar.ticks_to_us a v ⦃ r => r.bv = toUs v.bv ⦄) : + schedcontext.complete_yield_to st ar s a yf ⦃ r => r.2 = a ∧ ∃ σ', + completeScYielderWithA cap toUs (scNameA m sc) σ = ({((), σ')}, False) ∧ + SchedSRA st Z m κ r.1 σ' ∧ StEqA Z.T (L.mem r.1) σ' ∧ + McpRelA Z.T m (L.mem r.1) σ' ∧ YieldRelA Z.T Z.C m (L.mem r.1) σ' ∧ + SSzA σ σ' ∧ σ'.curThread = σ.curThread ∧ σ'.releaseQueue = σ.releaseQueue ∧ + (∀ t ∈ Z.T, ∀ x, σ.kheap (specPtr t) = some (.tcb x) → ∃ x', σ'.kheap (specPtr t) = some (.tcb x') ∧ + x'.tcbSchedContext = x.tcbSchedContext ∧ x'.tcbState = x.tcbState ∧ + x'.tcbArch.tcbContext.userRegs .faultIP = x.tcbArch.tcbContext.userRegs .faultIP ∧ (t ≠ yf → x' = x)) ∧ + (∀ y n, σ.kheap (scNameA m sc) = some (.schedContext y n) → + ∃ y', σ'.kheap (scNameA m sc) = some (.schedContext y' n) ∧ y'.scTcb = y.scTcb ∧ y'.scYieldFrom = none) ∧ + WritesIn st s r.1 (fun b => MWrA (st := st) Z m σ s b ∨ ScConsumedRegsA yf b ∨ b = yieldA yf) ∧ + completeYieldToWith (Z := Unit) cap toUs (specPtr yf) σ = ({((), σ')}, False) ∧ + (∀ q, q ≠ scNameA m sc → q ≠ specPtr yf → σ'.kheap q = σ.kheap q) ∧ + (∃ d : Data, (∀ x, σ.kheap (specPtr yf) = some (.tcb x) → + σ'.kheap (specPtr yf) = some (.tcb (yieldDoneA x d))) ∧ + L.mem r.1 (wa yf 256#u64) = 0#u64 ∧ + (L.mem r.1 (wa yf 264#u64)).bv = messageInfoToData ⟨natToLen 1, 0, 0, 0⟩ ∧ + (L.mem r.1 (wa yf 272#u64)).bv = d) + ⦄ := by + let p := scNameA m sc + let q := specPtr yf + obtain ⟨y, n, hk, hr⟩ := H.scr.sc sc hc + change σ.kheap p = some (.schedContext y n) at hk + have hYF : y.scYieldFrom = some q := by + have ha : scYieldFromA sc = scWA sc 56#u64 := by unfold scYieldFromA scWA layout.sc.YIELD_FROM; rfl + rw [hr.yieldFrom, ← ha, hy, optRefS_ne hy0] + obtain ⟨t, ht, eq, hyt⟩ := Y.frm sc hc y n hk q hYF + have e : t = yf := specPtr_inj eq + subst t + obtain ⟨x, hx, hxt⟩ := Y.tcb yf ht + change σ.kheap q = some (.tcb x) at hx + have hyt' : x.tcbYieldTo = some p := by rw [hxt, hyt]; simp [optScA, hC0 _ hc, p, scNameA_eq] + have hcn := hC0 sc hc + have hne : q ≠ p := fun e => by rw [e, hk] at hx; cases hx + obtain ⟨tm, y', hfields, hup⟩ := yieldConsumed_viewWithA cap toUs hk + have hf := hfields _ (Or.inl rfl) + let σ1 := updKSc σ p (.schedContext y' n) + have hinv : returnConsumedWith (Z := Unit) cap toUs p σ = + ({(wordsFromTime tm, σ1)}, False) := by + unfold returnConsumedWith + rw [nd_bind_oneSc hup] + rfl + unfold schedcontext.complete_yield_to + rw [eq_of_spec_fd (obj.Tcb.is_null.spec yf), bind_ok, decide_eq_false hy0] + simp only [Bool.false_eq_true, ↓reduceIte] + rw [eq_of_spec_fd (obj.Tcb.yield_to.spec s yf), bind_ok, hyt, if_pos (bne_iff_ne.2 hcn)] + refine WP.spec_bind (sc_consumed_msg_frameWithA H hst a hc ht true cap toUs hmax htu) ?_ + rintro s1 ⟨time, σc, hret, H1, S1, hz1, ht1, W1, V1⟩ + change returnConsumedWith (Z := Unit) cap toUs p σ = _ at hret + rw [hinv] at hret + have eqpost := Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst hret)) + have eσ : σc = σ1 := eqpost.2.symm + subst σc + have w1 : WritesIn st s s1 (fun b => MWrA (st := st) Z m σ s b ∨ ScConsumedRegsA yf b) := + W1.mono (fun _ h => h.elim Or.inl (fun e => Or.inr e.2)) + have kept : ∀ t ∈ Z.T, L.mem s1 (yieldA t) = L.mem s (yieldA t) := + fun t h => yieldWordKeepA H.toSchedSRN ht w1 h (Or.inr (Or.inr rfl)) + have hz := ht1 yf x hx + change σ1.kheap q = some (.tcb x) at hz + let z := yieldRegA (yieldRegA (yieldRegA x badge_register 0) .x2 (ucast 64 tm)) msg_info_register + (messageInfoToData ⟨natToLen 1, 0, 0, 0⟩) + let σ2 := updKSc σ1 q (.tcb z) + have hreply : replyFromKernel (Z := Unit) q (0, wordsFromTime tm) σ1 = ({((), σ2)}, False) := + yield_reply_wordA hz _ + have step := yieldTcbStepA (z := z) hz rfl rfl rfl rfl rfl + have H2 : SchedSRA st Z m κ s1 σ2 := H1.stable (WritesIn.refl _ (fun _ => False)) + (fun _ h => h.elim) step.1 step.2 + have hT : ∀ t ∈ Z.T, ∀ r, σ.kheap (specPtr t) = some (.tcb r) → + ∃ r', σ2.kheap (specPtr t) = some (.tcb r') ∧ r'.tcbState = r.tcbState ∧ + r'.tcbMcpriority = r.tcbMcpriority ∧ r'.tcbYieldTo = r.tcbYieldTo ∧ r'.tcbFlags = r.tcbFlags := by + intro t h r hr + by_cases e : t = yf + · subst t + rw [hx] at hr + cases hr + exact ⟨z, updKSc_same _ _ _, rfl, rfl, rfl, rfl⟩ + · exact ⟨r, by simpa [σ2, Function.update_of_ne (fun h => e (specPtr_inj h) : specPtr t ≠ q)] using ht1 t r hr, + rfl, rfl, rfl, rfl⟩ + have hSc : ∀ c ∈ Z.C, ∃ v k v', σ.kheap (scNameA m c) = some (.schedContext v k) ∧ + σ2.kheap (scNameA m c) = some (.schedContext v' k) ∧ v'.scYieldFrom = v.scYieldFrom := by + intro c hc' + by_cases e : c = sc + · subst c + exact ⟨y, n, y', hk, by change (updKSc σ1 q (.tcb z)).kheap p = _; rw [updKSc_kheap, Function.update_of_ne hne.symm]; exact updKSc_same _ _ _, hf⟩ + · obtain ⟨v, k, hv, -⟩ := H.scr.sc c hc' + have hn : scNameA m c ≠ p := fun h => e (H.scp.inj c hc' sc hc h) + have hq : scNameA m c ≠ q := fun e => by rw [e, hx] at hv; cases hv + exact ⟨v, k, v, hv, by simpa [σ2, σ1, Function.update_of_ne hn, Function.update_of_ne hq] using hv, rfl⟩ + obtain ⟨M2, Y2⟩ := yieldFrameA M Y + (fun t h => ⟨yieldWordKeepA H.toSchedSRN ht w1 h (Or.inr (Or.inl rfl)), yieldFlagsKeepA H.toSchedSRN ht w1 h⟩) kept + (fun t h r hr => by obtain ⟨r', hk', _, hm', hy', hf'⟩ := hT t h r hr; exact ⟨r', hk', hm', hy', hf'⟩) hSc + have S2 : StEqA Z.T (L.mem s1) σ2 := by + intro t h r hr + obtain ⟨r0, hr0, -⟩ := H.sch.tcb t h + obtain ⟨r', hr', hstate, -⟩ := hT t h r0 hr0 + rw [hr] at hr' + cases hr' + rw [hstate] + exact S1 t h r0 (ht1 t r0 hr0) + have hy1 := (kept _ ht).trans hyt + have hz2 : σ2.kheap q = some (.tcb z) := updKSc_same _ _ _ + have hsc2 : σ2.kheap p = some (.schedContext y' n) := by change (updKSc σ1 q (.tcb z)).kheap p = _; rw [updKSc_kheap, Function.update_of_ne hne.symm]; exact updKSc_same _ _ _ + have hy2 : y'.scYieldFrom = some q := hf.trans hYF + obtain ⟨H3, S3, M3, Y3, hz3, W3⟩ := yield_pair_wordsA H2 S2 M2 Y2 hc ht hC0 hz2 hsc2 (Or.inl ⟨hy1, hy2⟩) false + unfold schedcontext.cancel_yield_to + rw [eq_of_spec_fd (obj.Tcb.is_null.spec yf), bind_ok, decide_eq_false hy0] + simp only [Bool.false_eq_true, ↓reduceIte] + rw [eq_of_spec_fd (obj.Tcb.yield_to.spec s1 yf), bind_ok, hy1, if_pos (bne_iff_ne.2 hcn)] + rw [eq_of_spec_fd (obj.Sc.set_yield_from.spec s1 sc _), bind_ok, + eq_of_spec_fd (obj.Tcb.set_yield_to.spec _ yf _), bind_ok, WP.spec_ok] + have sz2 : SSzA σ1 σ2 := by + intro r v k hv + have hn : r ≠ q := fun e => by rw [e, hz2] at hv; cases hv + exact ⟨v, by simpa [σ2, Function.update_of_ne hn] using hv⟩ + let σ3 := yieldPairStA σ2 p q z y' n false + have he3 : σ3.kheap p = some (.schedContext {y' with scYieldFrom := none} n) := by + change (updKSc (updKSc σ2 p (.schedContext {y' with scYieldFrom := none} n)) + q (.tcb {z with tcbYieldTo := none})).kheap p = _ + rw [updKSc_kheap, Function.update_of_ne hne.symm] + exact updKSc_same _ _ _ + have cancel := yieldCancel_eqA hz2 hsc2 (show z.tcbYieldTo = some p from hyt') + have he : completeYieldToWith (Z := Unit) cap toUs q σ = ({((), σ3)}, False) := by + unfold completeYieldToWith getTcbObjRef + rw [nd_bind_oneSc (threadGet_eqSc _ hx), hyt'] + change (((schedContextUpdateConsumedWith cap toUs p >>= fun c => replyFromKernel q (0, wordsFromTime c)) >>= _) σ) = _ + rw [nd_bind_assocSc, nd_bind_oneSc hup, nd_bind_oneSc hreply] + exact cancel + have far : ∀ r, r ≠ p → r ≠ q → σ3.kheap r = σ.kheap r := fun r hp' hq' => by + change (updKSc (updKSc σ2 p (.schedContext {y' with scYieldFrom := none} n)) + q (.tcb {z with tcbYieldTo := none})).kheap r = _ + simp only [σ2, σ1, updKSc_kheap, Function.update_of_ne hq', Function.update_of_ne hp'] + have done : ∀ x0, σ.kheap q = some (.tcb x0) → σ3.kheap q = some (.tcb (yieldDoneA x0 (ucast 64 tm))) := by + intro x0 h0 + rw [hx] at h0 + cases h0 + exact updKSc_same _ _ _ + have regs : ∀ K : U64, K = 256#u64 ∨ K = 264#u64 ∨ K = 272#u64 → + L.mem (L.wr (L.wr s1 (scYieldFromA sc) (if false then yf else 0#u64)) (yieldA yf) (if false then sc else 0#u64)) + (wa yf K) = L.mem s1 (wa yf K) := by + intro K hK + have htA := H.core.tcbs yf ht + have hb : K.val < 4096 := by rcases hK with rfl | rfl | rfl <;> decide + have h8 : K.val % 8 = 0 := by rcases hK with rfl | rfl | rfl <;> decide + refine W3.mem_eq (field_aligned htA hb h8) ?_ + rintro (h | h) + · obtain ⟨hks, hsc'⟩ := yieldTcbOffA H2.toSchedSRN ht hb + refine not_mwrA hsc' (fun u hu => ?_) hks h + have field {k : U64} (hk4 : k.val < 4096) (hne' : K.val ≠ k.val) : + wa yf K ≠ wa u k := field_ne htA (H.core.tcbs u hu) hb hk4 (fun _ => hne') + refine ⟨field (by decide) ?_, field (by decide) ?_, field (by decide) ?_, field (by decide) ?_⟩ <;> + rcases hK with rfl | rfl | rfl <;> decide + · exact field_ne htA htA hb (K2 := 3472#u64) (by decide) (fun _ => by rcases hK with rfl | rfl | rfl <;> decide) h + obtain ⟨v256, v264, v272⟩ := V1 rfl + refine ⟨rfl, σ3, ?_, H3, S3, M3, Y3, (hz1.trans sz2).trans hz3, rfl, rfl, ?_, ?_, ?_, he, far, ucast 64 tm, done, + (regs _ (Or.inl rfl)).trans v256, + (congrArg (fun w : U64 => w.bv) (regs _ (Or.inr (Or.inl rfl)))).trans v264, + (congrArg (fun w : U64 => w.bv) (regs _ (Or.inr (Or.inr rfl)))).trans (v272.trans (show ucast 64 time = ucast 64 tm by + simpa [wordsFromTime] using eqpost.1.symm))⟩ + · unfold completeScYielderWithA + rw [nd_bind_oneSc (getScYieldFrom_eqA hk), hYF, NonDet.when, if_pos (by simp)] + rw [show the (some q) = q from rfl, nd_bind_oneSc he, nd_bind_oneSc (getScYieldFrom_eqA he3)] + exact assert_trueSc rfl σ3 + · intro t ht' x' hk' + by_cases e : t = yf + · subst t + rw [hx] at hk' + cases hk' + refine ⟨_, updKSc_same _ _ _, rfl, rfl, ?_, fun h => (h rfl).elim⟩ + simp only [z, yieldRegA, archTcbSetRegisters, archTcbGetRegisters, archTcbContextGet, + badge_register, msg_info_register] + rw [Function.update_of_ne (show Register.faultIP ≠ msgInfoRegister by decide), + Function.update_of_ne (show Register.faultIP ≠ Register.x2 by decide), + Function.update_of_ne (show Register.faultIP ≠ badgeRegister by decide)] + · have et : specPtr t ≠ q := fun he => e (specPtr_inj he) + have ep : specPtr t ≠ p := fun he => by rw [he, hk] at hk'; cases hk' + refine ⟨x', ?_, rfl, rfl, rfl, fun _ => rfl⟩ + change (updKSc (updKSc σ2 p (.schedContext {y' with scYieldFrom := none} n)) + q (.tcb {z with tcbYieldTo := none})).kheap (specPtr t) = _ + simp only [updKSc_kheap, Function.update_of_ne et, Function.update_of_ne ep] + simpa [σ2, Function.update_of_ne et] using ht1 t x' hk' + · intro y0 n0 hk0 + change σ.kheap p = _ at hk0 + rw [hk] at hk0 + cases hk0 + exact ⟨_, he3, hfields _ (Or.inr rfl), rfl⟩ + · exact (w1.trans W3).mono fun b h => by + rcases h with (h | h) | h | h + · exact Or.inl h + · exact Or.inr (Or.inl h) + · exact Or.inl (mwr_back rfl rfl (hz1.trans sz2) w1.1 h) + · exact Or.inr (Or.inr h) + +/-- Legacy YieldTo callers use the port's consumed-time constants. -/ +theorem yield_complete_liveA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRA st Z m κ s σ) (hst : StEqA Z.T (L.mem s) σ) + (M : McpRelA Z.T m (L.mem s) σ) (Y : YieldRelA Z.T Z.C m (L.mem s) σ) + (a : A) {sc yf : U64} (hc : sc ∈ Z.C) (hC0 : ∀ c ∈ Z.C, c ≠ 0#u64) + (hy0 : yf ≠ 0#u64) (hy : L.mem s (scYieldFromA sc) = yf) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) : + schedcontext.complete_yield_to st ar s a yf ⦃ r => r.2 = a ∧ ∃ σ', + completeScYielderA (scNameA m sc) σ = ({((), σ')}, False) ∧ + SchedSRA st Z m κ r.1 σ' ∧ StEqA Z.T (L.mem r.1) σ' ∧ + McpRelA Z.T m (L.mem r.1) σ' ∧ YieldRelA Z.T Z.C m (L.mem r.1) σ' ∧ + SSzA σ σ' ∧ σ'.curThread = σ.curThread ∧ σ'.releaseQueue = σ.releaseQueue ∧ + (∀ t ∈ Z.T, ∀ x, σ.kheap (specPtr t) = some (.tcb x) → ∃ x', σ'.kheap (specPtr t) = some (.tcb x') ∧ + x'.tcbSchedContext = x.tcbSchedContext ∧ (t ≠ yf → x' = x)) ∧ + (∀ y n, σ.kheap (scNameA m sc) = some (.schedContext y n) → + ∃ y', σ'.kheap (scNameA m sc) = some (.schedContext y' n) ∧ y'.scTcb = y.scTcb ∧ y'.scYieldFrom = none) ∧ + WritesIn st s r.1 (fun b => MWrA (st := st) Z m σ s b ∨ ScConsumedRegsA yf b ∨ b = yieldA yf) + ⦄ := by + refine WP.spec_mono (yield_complete_liveWithA H hst M Y a hc hC0 hy0 hy _ _ hmax htu) ?_ + rintro r ⟨ha, τ, e, hS, hSt, hM, hY, sz, ht, hR, tcb, scs, w, _⟩ + rw [completeScYielderWith_eq_l4vA] at e + refine ⟨ha, τ, e, hS, hSt, hM, hY, sz, ht, hR, ?_, scs, w⟩ + intro u hu z hz + obtain ⟨z', hz', hsc, _, _, he⟩ := tcb u hu z hz + exact ⟨z', hz', hsc, he⟩ + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScYieldCoreA.lean b/proofs/Fl4ProofsA64/P3/ScYieldCoreA.lean new file mode 100644 index 000000000..40396c58f --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScYieldCoreA.lean @@ -0,0 +1,71 @@ +/- +P3 (AArch64): the core guard in FL4's YieldTo. +Where the SC is on another core, FL4 requeues its TCB and returns immediately. +The ported l4v `schedContextYieldTo` has a priority test at this point and no +core guard. The x86 proof `ScJointYield` uses the single-core relation. +-/ +import Fl4ProofsA64.P3.ScDonA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +/-- The schedulable remote-core case returns false before reading either priority. -/ +theorem yield_switches_remoteA {S : Type} {st : machine.Store S} [L : LawfulStore st] + (s : S) (sc ct : U64) (h : sched.is_schedulable st s (L.mem s (scTcbA sc)) = ok true) + (hcore : L.mem s (scCoreA sc) ≠ L.cpu s) : + schedcontext.yield_switches st s sc ct = (do + let s1 ← sched.dequeue st s (L.mem s (scTcbA sc)) + let s2 ← sched.enqueue st s1 (L.mem s (scTcbA sc)) + ok (false, s2)) := by + unfold schedcontext.yield_switches + rw [eq_of_spec_fd (obj.Sc.tcb.spec (L := L) s sc), bind_ok, h, bind_ok] + simp only [↓reduceIte] + rw [eq_of_spec_fd (obj.Sc.cpu.spec (L := L) s sc), bind_ok, L.cur_cpu_eq s, bind_ok] + rw [if_pos (bne_iff_ne.2 hcore)] + +noncomputable section +open Fl4SpecA64 Fl4Spec + +/-- With no previous yielder, a completed resume and a schedulable target +of at least the current priority, the l4v branch installs the yield pair. +No affinity is tested in this branch. -/ +theorem schedContextYieldTo_waitA {σ σ' : State Unit} {sc t : ObjRef} {x x' : SchedContext} {n n' : Nat} + (hk : σ.kheap sc = some (.schedContext x n)) (hy : x.scYieldFrom = none) + (hr : schedContextResume (Z := Unit) sc σ = ({((), σ')}, False)) + (hk' : σ'.kheap sc = some (.schedContext x' n')) (hb : x'.scTcb = some t) + (hready : schedulable t σ' = true) {xt xc : Tcb} + (hkt : σ'.kheap t = some (.tcb xt)) (hkc : σ'.kheap σ'.curThread = some (.tcb xc)) + (hprio : ¬ xt.tcbPriority < xc.tcbPriority) : + schedContextYieldTo (Z := Unit) sc σ = (do + setTcbObjRef tcbYieldToUpdate σ'.curThread (some sc) + setScObjRef scYieldFromUpdate sc (some σ'.curThread) + tcbSchedAction tcbSchedDequeue t + tcbSchedAction tcbSchedEnqueue σ'.curThread + tcbSchedAction tcbSchedEnqueue t + rescheduleRequired + pure ([] : List Data)) σ' := by + have hf : getScObjRef (Z := Unit) SchedContext.scYieldFrom sc σ = ({(none, σ)}, False) := by + unfold getScObjRef + rw [nd_bind_oneSc (getSchedContext_eqA hk), hy] + rfl + unfold schedContextYieldTo + rw [nd_bind_oneSc hf, NonDet.when, if_neg (by simp)] + rw [nd_bind_oneSc (show NonDet.pure (σ := State Unit) () σ = ({((), σ)}, False) from rfl), + nd_bind_oneSc hr] + rw [nd_bind_assocSc, nd_bind_oneSc (getScTcb_eqSc hk'), hb, nd_bind_assocSc] + have ha : NonDet.assertOpt (σ := State Unit) (some t) σ' = ({(t, σ')}, False) := rfl + rw [nd_bind_oneSc ha, nd_bind_assocSc, nd_gets_bindSc, if_pos hready] + repeat rw [nd_bind_assocSc] + rw [nd_gets_bindSc, nd_bind_assocSc, nd_bind_oneSc (threadGet_eqSc _ hkt), + nd_bind_assocSc, nd_bind_oneSc (threadGet_eqSc _ hkc), if_neg hprio] + have assoc {α β γ : Type} (f : NonDet (State Unit) α) (g : α → NonDet (State Unit) β) + (h : β → NonDet (State Unit) γ) : (f >>= g) >>= h = f >>= fun x => g x >>= h := + funext (nd_bind_assocSc f g h) + have pureBind {α β : Type} (x : α) (f : α → NonDet (State Unit) β) : (pure x >>= f) = f x := + funext (nd_pure_bindSc x f) + simp only [assoc, pureBind, Bool.false_eq_true, ↓reduceIte] + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScYieldFrameA.lean b/proofs/Fl4ProofsA64/P3/ScYieldFrameA.lean new file mode 100644 index 000000000..c8f78997a --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScYieldFrameA.lean @@ -0,0 +1,191 @@ +/- +P3 (AArch64): the write frames for the MCP and yield-link words. +-/ +import Fl4ProofsA64.P3.ScYieldRelA +import Fl4ProofsA64.P3.ScConsumeMsgA +import Fl4ProofsA64.P1.S1aEpoch +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4SpecA64 + +noncomputable section +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- The bound TCB is local by the SC/TCB affinity equality. -/ +theorem yield_local_target_of_scAffA {T : Set U64} {s : S} + (H : ScAff st T s) {t sc : U64} (ht : t ∈ T) + (h0 : sc ≠ 0#u64) (hsc : L.mem s (scA t) = sc) + (hc : L.mem s (scCoreA sc) = L.cpu s) : + L.mem s (affA t) = L.cpu s := by + rw [H.core_eq ht (fun h => h0 (hsc.symm.trans h)), hsc, hc] + +theorem yieldTcbOffA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRN st Z m κ s σ) {t K : U64} (ht : t ∈ Z.T) (hK : K.val < 4096) : + ((wa t K).val < (L.ksb s).val ∨ (L.ksb s).val + ksSizeA ≤ (wa t K).val) ∧ + ¬ ScFootA Z.C m σ (wa t K) := by + have hv := field_val (H.core.tcbs t ht) hK + constructor + · have h := H.tcb_ks ht + rw [hv] + omega + · intro h + have hn := H.scp.th_out h ((H.thT t).2 ht) + simp only [ObjKindA.size] at hn + rw [hv] at hn + omega + +/-- A TCB's state, MCP and yield target are outside the scheduler write footprint. -/ +theorem yieldTcbNotMWrA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRN st Z m κ s σ) {t K : U64} (ht : t ∈ Z.T) + (hK : K = 3328#u64 ∨ K = 3440#u64 ∨ K = 3472#u64) : + ¬ MWrA (st := st) Z m σ s (wa t K) := by + have hb : K.val < 4096 := by rcases hK with rfl | rfl | rfl <;> decide + obtain ⟨hks, hsc⟩ := yieldTcbOffA H ht hb + refine not_mwrA hsc (fun u hu => ?_) hks + have field {k : U64} (hk : k.val < 4096) (hne : K.val ≠ k.val) : + wa t K ≠ wa u k := field_ne (H.core.tcbs t ht) (H.core.tcbs u hu) hb hk (fun _ => hne) + refine ⟨field (by decide) ?_, field (by decide) ?_, field (by decide) ?_, field (by decide) ?_⟩ <;> + rcases hK with rfl | rfl | rfl <;> decide + +/-- (item 9) A TCB's flags word is outside the scheduler write footprint. -/ +theorem yieldFlagsNotMWrA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRN st Z m κ s σ) {t : U64} (ht : t ∈ Z.T) : + ¬ MWrA (st := st) Z m σ s (flagsA t) := by + have hb : (3456#u64).val < 4096 := by decide + obtain ⟨hks, hsc⟩ := yieldTcbOffA H ht hb + refine not_mwrA hsc (fun u hu => ?_) hks + have field {k : U64} (hk : k.val < 4096) (hne : (3456#u64).val ≠ k.val) : + wa t 3456#u64 ≠ wa u k := field_ne (H.core.tcbs t ht) (H.core.tcbs u hu) hb hk (fun _ => hne) + exact ⟨field (by decide) (by decide), field (by decide) (by decide), field (by decide) (by decide), + field (by decide) (by decide)⟩ + +theorem yieldWordOffReadA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRN st Z m κ s σ) {t : U64} (ht : t ∈ Z.T) : + ¬ SReadA st Z m s σ (yieldA t) := by + change ¬ SReadA st Z m s σ (wa t 3472#u64) + obtain ⟨hks, hsc⟩ := yieldTcbOffA H ht (K := 3472#u64) (by decide) + have glob {k : U64} (hk : k.val < ksSizeA) : wa t 3472#u64 ≠ wa (L.ksb s) k := by + intro e + have hf := H.ksFit + have hv : (wa (L.ksb s) k).val = (L.ksb s).val + k.val := by rw [wa_valm]; omega + rw [e, hv] at hks + omega + rintro (⟨c, hc, o, ho, e⟩ | e | e | ⟨u, hu, k, hk, e⟩ | h) + · have hv := H.node_in hc ho + rw [e] at hks + omega + · exact glob (by decide) e + · exact glob (by decide) e + · rcases hk with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> + exact field_ne (H.core.tcbs t ht) (H.core.tcbs u hu) (by decide) (by decide) (fun _ => by decide) e + · exact hsc h + +/-- Memory at the state, MCP and yield words is unchanged by the ordinary +scheduler footprint and a one-word reply. -/ +theorem yieldWordKeepA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s s' : S} {σ : State Unit} + (H : SchedSRN st Z m κ s σ) {v : U64} (hv : v ∈ Z.T) + (hw : WritesIn st s s' (fun b => MWrA (st := st) Z m σ s b ∨ ScConsumedRegsA v b)) + {t K : U64} (ht : t ∈ Z.T) (hK : K = 3328#u64 ∨ K = 3440#u64 ∨ K = 3472#u64) : + L.mem s' (wa t K) = L.mem s (wa t K) := by + refine hw.mem_eq (field_aligned (H.core.tcbs t ht) ?_ ?_) ?_ + · rcases hK with rfl | rfl | rfl <;> decide + · rcases hK with rfl | rfl | rfl <;> decide + · rintro (h | h) + · exact yieldTcbNotMWrA H ht hK h + · rcases h with h | h | h <;> rcases hK with rfl | rfl | rfl <;> + exact field_ne (H.core.tcbs t ht) (H.core.tcbs v hv) (by decide) (by decide) (fun _ => by decide) h + +/-- (item 9) The flags word is unchanged by the ordinary scheduler footprint and a one-word reply. -/ +theorem yieldFlagsKeepA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s s' : S} {σ : State Unit} + (H : SchedSRN st Z m κ s σ) {v : U64} (hv : v ∈ Z.T) + (hw : WritesIn st s s' (fun b => MWrA (st := st) Z m σ s b ∨ ScConsumedRegsA v b)) + {t : U64} (ht : t ∈ Z.T) : L.mem s' (flagsA t) = L.mem s (flagsA t) := by + refine hw.mem_eq (field_aligned (H.core.tcbs t ht) (K := 3456#u64) (by decide) (by decide)) ?_ + rintro (h | h) + · exact yieldFlagsNotMWrA H ht h + · rcases h with h | h | h <;> + exact field_ne (H.core.tcbs t ht) (H.core.tcbs v hv) (K1 := 3456#u64) (by decide) (by decide) + (fun _ => by decide) h + +end + +/-- Replacing the registers or yield target of an existing TCB leaves +its scheduling fields and IPC classification unchanged. -/ +theorem yieldTcbStepA {σ : State Unit} {p : ObjRef} {x z : Tcb} + (hk : σ.kheap p = some (.tcb x)) (hsc : z.tcbSchedContext = x.tcbSchedContext) + (hd : z.tcbDomain = x.tcbDomain) (hp : z.tcbPriority = x.tcbPriority) + (hs : z.tcbState = x.tcbState) (hf : z.tcbFault = x.tcbFault) : + SStepA σ (updKSc σ p (.tcb z)) ∧ NoBlockESc σ (updKSc σ p (.tcb z)) := by + let σ' := updKSc σ p (.tcb z) + have hh : σ'.kheap p = some (.tcb z) := updKSc_same _ _ _ + have far : ∀ q, q ≠ p → σ'.kheap q = σ.kheap q := fun q hq => by simp [σ', updKSc, hq] + refine ⟨⟨⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩, rfl, rfl, rfl, + fun q y n hq => far q (fun e => by rw [e, hk] at hq; cases hq), + ?_, fun q y n hq => (far q (fun e => by rw [e, hh] at hq; cases hq)).symm, ?_, ?_⟩, ?_⟩ + · intro q y hq + by_cases e : q = p + · subst q + rw [hk] at hq + cases hq + exact ⟨z, hh, hsc, hd, hp⟩ + · exact ⟨y, (far q e).trans hq, rfl, rfl, rfl⟩ + · intro q y hq + by_cases e : q = p + · exact ⟨x, e.symm ▸ hk⟩ + · exact ⟨y, (far q e).symm.trans hq⟩ + · intro q y hq + exact ⟨y, (far q (fun e => by rw [e, hh] at hq; cases hq)).symm.trans hq, rfl⟩ + · intro q y w h1 h2 + by_cases e : q = p + · subst q + rw [hk] at h1 + rw [hh] at h2 + cases h1 + cases h2 + exact Or.inl ⟨hs, by rw [hf, hs]; exact id⟩ + · rw [far q e, h1] at h2 + cases h2 + exact Or.inl ⟨rfl, id⟩ + +/-- Transport the independent relations across steps preserving their +word and heap projections. -/ +theorem yieldFrameA {T C : Set U64} {m : McState} {mem mem' : U64 → U64} {σ σ' : State Unit} + (M : McpRelA T m mem σ) (Y : YieldRelA T C m mem σ) + (hm : ∀ t ∈ T, mem' (mcpA t) = mem (mcpA t) ∧ mem' (flagsA t) = mem (flagsA t)) + (hy : ∀ t ∈ T, mem' (yieldA t) = mem (yieldA t)) + (htcb : ∀ t ∈ T, ∀ x, σ.kheap (specPtr t) = some (.tcb x) → + ∃ x', σ'.kheap (specPtr t) = some (.tcb x') ∧ + x'.tcbMcpriority = x.tcbMcpriority ∧ x'.tcbYieldTo = x.tcbYieldTo ∧ x'.tcbFlags = x.tcbFlags) + (hsc : ∀ c ∈ C, ∃ y n y', σ.kheap (scNameA m c) = some (.schedContext y n) ∧ + σ'.kheap (scNameA m c) = some (.schedContext y' n) ∧ y'.scYieldFrom = y.scYieldFrom) : + McpRelA T m mem' σ' ∧ YieldRelA T C m mem' σ' := by + refine ⟨?_, ⟨?_, ?_, ?_⟩⟩ + · intro t ht + obtain ⟨x, hx, h, hf⟩ := M t ht + obtain ⟨x', hx', h', _, hf'⟩ := htcb t ht x hx + exact ⟨x', hx', by rw [(hm t ht).1, h', h], fun g => (hf g).keep (hm t ht).2 hf'⟩ + · intro t ht + obtain ⟨x, hx, h⟩ := Y.tcb t ht + obtain ⟨x', hx', _, h', _⟩ := htcb t ht x hx + exact ⟨x', hx', by rw [hy t ht, h', h]⟩ + · intro t ht h + rw [hy t ht] at h ⊢ + obtain ⟨hc, y, n, h1, h2⟩ := Y.toSc t ht h + obtain ⟨z, k, z', hz, hz', he⟩ := hsc _ hc + rw [h1] at hz + cases hz + exact ⟨hc, z', n, hz', he.trans h2⟩ + · intro c hc y n h p e + obtain ⟨z, k, z', hz, hz', he⟩ := hsc c hc + rw [h] at hz' + cases hz' + obtain ⟨t, ht, hp, hc'⟩ := Y.frm c hc z n hz p (he.symm.trans e) + exact ⟨t, ht, hp, (hy t ht).trans hc'⟩ + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScYieldLinkA.lean b/proofs/Fl4ProofsA64/P3/ScYieldLinkA.lean new file mode 100644 index 000000000..a4f713720 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScYieldLinkA.lean @@ -0,0 +1,242 @@ +/- +P3 (AArch64): pairing and clearing reciprocal yield links. +-/ +import Fl4ProofsA64.P3.ScYieldFrameA +import Fl4ProofsA64.P3.ScYieldMsgA +import Fl4ProofsA64.P3.ScDonBA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4SpecA64 + +noncomputable section +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +private theorem scRel_set_yieldA {ii : Prop} {s : S} {sc : U64} {xs : SchedContext} {ns : Nat} + (h : ScRelWA st ii s sc xs ns) (v : U64) : + let s' := L.wr s (scWA sc 56#u64) v + ScRelWA st ii s' sc { xs with scYieldFrom := optRefS v } ns ∧ WritesIn st s s' (ScObjA sc ns) ∧ L.mem s' (scCoreA sc) = L.mem s (scCoreA sc) := by + intro s' + obtain ⟨hR, hp, hm, hw, hwr, hf⟩ := h.wr_hdr (K := 56#u64) (by simp [HdrKA]) v + have keep : ∀ k : U64, k.val < 96 → k.val % 8 = 0 → k.val ≠ 56 → + L.mem s' (scWA sc k) = L.mem s (scWA sc k) := + fun k h1 h2 h3 => hw k h1 h2 (fun e => h3 (by subst e; simp)) + refine ⟨⟨h.pl, by rw [hp]; exact h.period, by rw [keep 8#u64 (by simp) (by simp) (by simp)]; exact h.consumed, + by rw [keep 24#u64 (by simp) (by simp) (by simp)]; exact h.tcb, + by rw [keep 32#u64 (by simp) (by simp) (by simp)]; exact h.reply, by rw [keep 40#u64 (by simp) (by simp) (by simp)]; exact h.ntfn, + by rw [keep 48#u64 (by simp) (by simp) (by simp)]; exact h.badge, + by rw [hwr], + by rw [keep 64#u64 (by simp) (by simp) (by simp)]; exact h.sporadic, + fun h0 => by rw [hm]; exact h.spor (by rwa [keep 64#u64 (by simp) (by simp) (by simp)] at h0), + fun h0 => by rw [hm]; exact h.idle h0, by rw [hm]; exact h.max, fun ha => ?_⟩, hf, ?_⟩ + swap + · rw [show scCoreA sc = scWA sc 16#u64 by unfold scCoreA scWA layout.sc.CORE; rfl] + exact hw 16#u64 (by decide) (by decide) (by decide) + have ha' : (L.rmax s sc).val ≠ 0 := by rwa [hm] at ha + have h1 := h.act ha' + exact ⟨(hR ha').1, by rw [hp]; exact h1.period, + by rw [keep 8#u64 (by simp) (by simp) (by simp)]; exact h1.consumed, + by rw [keep 24#u64 (by simp) (by simp) (by simp)]; exact h1.tcb, + by rw [keep 32#u64 (by simp) (by simp) (by simp)]; exact h1.reply, by rw [keep 40#u64 (by simp) (by simp) (by simp)]; exact h1.ntfn, + by rw [keep 48#u64 (by simp) (by simp) (by simp)]; exact h1.badge, + by rw [hwr], + by rw [keep 64#u64 (by simp) (by simp) (by simp)]; exact h1.sporadic, + by rw [hm]; exact h1.max, by rw [(hR ha').2]; exact h1.refills⟩ + +/-- The abstract state after setting or clearing both yield links. -/ +def yieldPairStA (σ : State Unit) (p q : ObjRef) (x : Tcb) (y : SchedContext) (n : Nat) + (link : Bool) : State Unit := + updKSc (updKSc σ p (.schedContext {y with scYieldFrom := if link then some q else none} n)) + q (.tcb {x with tcbYieldTo := if link then some p else none}) + +private theorem yieldPairRelA {T C : Set U64} {m : McState} {s s' : S} {σ : State Unit} + (Y : YieldRelA T C m (L.mem s) σ) {sc t : U64} (hc : sc ∈ C) (ht : t ∈ T) + (nonzero : ∀ c ∈ C, c ≠ 0#u64) {x : Tcb} {y : SchedContext} {n : Nat} + (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hs : σ.kheap (scNameA m sc) = some (.schedContext y n)) + (hi : ∀ c ∈ C, scNameA m c = scNameA m sc → c = sc) + (old : (L.mem s (yieldA t) = sc ∧ y.scYieldFrom = some (specPtr t)) ∨ + (L.mem s (yieldA t) = 0#u64 ∧ y.scYieldFrom = none)) + (link : Bool) (hm : L.mem s' (yieldA t) = if link then sc else 0#u64) + (keep : ∀ u ∈ T, u ≠ t → L.mem s' (yieldA u) = L.mem s (yieldA u)) : + YieldRelA T C m (L.mem s') (yieldPairStA σ (scNameA m sc) (specPtr t) x y n link) := by + have hc0 := nonzero sc hc + let p := scNameA m sc + let q := specPtr t + let z := {x with tcbYieldTo := if link then some p else none} + let w := {y with scYieldFrom := if link then some q else none} + let σ' := yieldPairStA σ p q x y n link + change σ.kheap p = some (.schedContext y n) at hs + change σ.kheap q = some (.tcb x) at hk + have epq : p ≠ q := by intro e; rw [e, hk] at hs; cases hs + have hq : σ'.kheap q = some (.tcb z) := updKSc_same _ _ _ + have hp : σ'.kheap p = some (.schedContext w n) := by + simp [σ', yieldPairStA, Function.update_of_ne epq, w] + have far : ∀ a, a ≠ p → a ≠ q → σ'.kheap a = σ.kheap a := by + intro a ap aq + simp [σ', yieldPairStA, Function.update_of_ne ap, Function.update_of_ne aq] + have othTCB {u a : _} (h : σ.kheap (specPtr u) = some (.tcb a)) : + specPtr u ≠ p := by intro e; rw [e, hs] at h; cases h + have othSC {c v k : _} (h : σ.kheap (scNameA m c) = some (.schedContext v k)) : + scNameA m c ≠ q := by intro e; rw [e, hk] at h; cases h + have handsOff {u : U64} (hu : u ∈ T) (hne : u ≠ t) : L.mem s (yieldA u) ≠ sc := by + intro e + obtain ⟨_, v, k, h, hlink⟩ := Y.toSc u hu (by rw [e]; exact hc0) + rw [e, hs] at h + cases h + rcases old with ⟨_, h'⟩ | ⟨_, h'⟩ + · rw [h'] at hlink + exact hne (specPtr_inj (Option.some.inj hlink).symm) + · rw [h'] at hlink + cases hlink + refine ⟨?_, ?_, ?_⟩ + · intro u hu + by_cases e : u = t + · subst u + refine ⟨z, hq, ?_⟩ + rw [hm] + cases link <;> simp [z, p, optScA, hc0, scNameA_eq] + · obtain ⟨a, ha, he⟩ := Y.tcb u hu + rw [keep u hu e] + exact ⟨a, (far _ (othTCB ha) (fun h => e (specPtr_inj h))).trans ha, he⟩ + · intro u hu h0 + by_cases e : u = t + · subst u + rw [hm] at h0 ⊢ + cases link + · exact (h0 rfl).elim + · exact ⟨hc, w, n, hp, rfl⟩ + · rw [keep u hu e] at h0 ⊢ + obtain ⟨hd, v, k, hv, hy⟩ := Y.toSc u hu h0 + exact ⟨hd, v, k, (far _ (fun h => handsOff hu e (hi _ hd h)) (othSC hv)).trans hv, hy⟩ + · intro c cd v k hv a ha + by_cases e : c = sc + · subst c + rw [hp] at hv + cases hv + cases link + · cases ha + · exact ⟨t, ht, Option.some.inj ha, hm⟩ + · have qp : scNameA m c ≠ p := fun h => e (hi _ cd h) + have qq : scNameA m c ≠ q := by + intro h + rw [h, hq] at hv + cases hv + obtain ⟨u, hu, hu_a, hu_c⟩ := Y.frm c cd v k ((far _ qp qq).symm.trans hv) a ha + have hut : u ≠ t := by + intro eq + subst u + rcases old with ⟨h, _⟩ | ⟨h, _⟩ + · exact e (hu_c.symm.trans h) + · exact nonzero c cd (hu_c.symm.trans h) + exact ⟨u, hu, hu_a, (keep u hu hut).trans hu_c⟩ + +/-- The two link writes preserve the scheduler, MCP and reciprocal yield +relations. The SCs have nonnull bases, as in P1's ObjsOK.sc0. -/ +theorem yield_pair_wordsA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRA st Z m κ s σ) (hst : StEqA Z.T (L.mem s) σ) + (M : McpRelA Z.T m (L.mem s) σ) (Y : YieldRelA Z.T Z.C m (L.mem s) σ) + {sc t : U64} (hc : sc ∈ Z.C) (ht : t ∈ Z.T) (hC0 : ∀ c ∈ Z.C, c ≠ 0#u64) + {x : Tcb} {y : SchedContext} {n : Nat} + (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hs : σ.kheap (scNameA m sc) = some (.schedContext y n)) + (old : (L.mem s (yieldA t) = sc ∧ y.scYieldFrom = some (specPtr t)) ∨ + (L.mem s (yieldA t) = 0#u64 ∧ y.scYieldFrom = none)) (link : Bool) : + let s' := L.wr (L.wr s (scYieldFromA sc) (if link then t else 0#u64)) + (yieldA t) (if link then sc else 0#u64) + let σ' := yieldPairStA σ (scNameA m sc) (specPtr t) x y n link + SchedSRA st Z m κ s' σ' ∧ StEqA Z.T (L.mem s') σ' ∧ + McpRelA Z.T m (L.mem s') σ' ∧ YieldRelA Z.T Z.C m (L.mem s') σ' ∧ + SSzA σ σ' ∧ WritesIn st s s' (fun b => MWrA (st := st) Z m σ s b ∨ b = yieldA t) := by + intro s' σ' + let p := scNameA m sc + let q := specPtr t + let v := if link then t else 0#u64 + let u := if link then sc else 0#u64 + let y' := {y with scYieldFrom := if link then some q else none} + let x' := {x with tcbYieldTo := if link then some p else none} + let s1 := L.wr s (scYieldFromA sc) v + let σ1 := updKSc σ p (.schedContext y' n) + obtain ⟨y0, n0, hy0, hr⟩ := H.scr.sc sc hc + rw [hs] at hy0 + cases hy0 + change σ.kheap p = some (.schedContext y n) at hs + change σ.kheap q = some (.tcb x) at hk + have hne : q ≠ p := fun e => by rw [e, hs] at hk; cases hk + have ht0 : t ≠ 0#u64 := fun e => (H.core.tcbs t ht).ne0 (by rw [e]; rfl) + have hopt : optRefS v = y'.scYieldFrom := by + cases link + · exact optRefS_zero + · exact optRefS_ne ht0 + obtain ⟨hr', hw1, hco⟩ := scRel_set_yieldA hr v + have haddr : scYieldFromA sc = scWA sc 56#u64 := by unfold scYieldFromA scWA layout.sc.YIELD_FROM; rfl + rw [← haddr] at hr' hw1 hco + rw [hopt] at hr' + change ScRelWA st (IdleScA m sc) s1 sc y' n at hr' + have H1 : SchedSRA st Z m κ s1 σ1 := H.scObjA hc hs hw1 hr' rfl rfl rfl rfl hco + have hk1 : σ1.kheap q = some (.tcb x) := by + simpa [σ1, Function.update_of_ne hne] using hk + have step := yieldTcbStepA (z := x') hk1 rfl rfl rfl rfl rfl + have hw1' : WritesIn st s s1 (fun b => ScFootA Z.C m σ b ∨ b = L.nodeAt s (L.cpu s) 0#u64) := + hw1.mono fun b hb => Or.inl ⟨sc, hc, y, n, hs, hb⟩ + have keep1 := (H.keepW hw1' (by decide) (fun _ h => h)).2.2 + have htAddr := H1.core.tcbs t ht + have hal : Aligned (yieldA t) := field_aligned htAddr (by decide) (by decide) + have hw2 : WritesIn st s1 s' (· = yieldA t) := writesIn_wr hal + have H' : SchedSRA st Z m κ s' σ' := + H1.stable hw2 (fun _ e => e ▸ yieldWordOffReadA H1.toSchedSRN ht) step.1 step.2 + have keep2 {d K : U64} (hd : d ∈ Z.T) (hb : K.val < 4096) (ha : K.val % 8 = 0) + (he : d = t → K.val ≠ 3472) : L.mem s' (wa d K) = L.mem s (wa d K) := by + rw [hw2.mem_eq (field_aligned (H1.core.tcbs d hd) hb ha)] + · exact keep1 d hd K (by omega) ha + · exact field_ne (H1.core.tcbs d hd) htAddr hb (by decide) he + have tcbFrames : ∀ d ∈ Z.T, ∀ z, σ.kheap (specPtr d) = some (.tcb z) → + ∃ z', σ'.kheap (specPtr d) = some (.tcb z') ∧ z'.tcbState = z.tcbState ∧ z'.tcbMcpriority = z.tcbMcpriority ∧ + z'.tcbFlags = z.tcbFlags := by + intro d hd z hz + by_cases e : d = t + · subst d + rw [hk] at hz + cases hz + exact ⟨x', updKSc_same _ _ _, rfl, rfl, rfl⟩ + · have hn : specPtr d ≠ p := fun e' => by rw [e', hs] at hz; cases hz + have hq : specPtr d ≠ q := fun e' => e (specPtr_inj e') + refine ⟨z, ?_, rfl, rfl, rfl⟩ + change (updKSc σ1 q (.tcb x')).kheap (specPtr d) = _ + rw [updKSc_kheap, Function.update_of_ne hq] + change (updKSc σ p (.schedContext y' n)).kheap (specPtr d) = _ + rwa [updKSc_kheap, Function.update_of_ne hn] + refine ⟨H', ?_, ?_, ?_, ?_, (hw1.trans hw2).mono (fun b hb => hb.elim (fun hb => Or.inl (Or.inl ⟨sc, hc, y, n, hs, hb⟩)) Or.inr)⟩ + · intro d hd z hz + obtain ⟨z0, hk0, -⟩ := H.sch.tcb d hd + obtain ⟨z', hk', he, -⟩ := tcbFrames d hd z0 hk0 + rw [hz] at hk' + cases hk' + rw [he, stA_eq, keep2 hd (by decide) (by decide) (fun _ => by decide), ← stA_eq] + exact hst d hd z0 hk0 + · intro d hd + obtain ⟨z, hz, hm, hf⟩ := M d hd + obtain ⟨z', hz', _, he, hfl⟩ := tcbFrames d hd z hz + exact ⟨z', hz', by rw [he, mcpA, keep2 hd (by decide) (by decide) (fun _ => by decide), ← mcpA]; exact hm, + fun g => (hf g).keep (by unfold flagsA; exact keep2 hd (by decide) (by decide) (fun _ => by decide)) hfl⟩ + · exact yieldPairRelA Y hc ht hC0 hk hs (fun c h => H.scp.inj c h sc hc) old link + (L.mem_wr_same hal) (fun d hd hd' => keep2 hd (by decide) (by decide) (fun h => (hd' h).elim)) + · intro d z k h + have hq : d ≠ q := fun e => by rw [e, show σ'.kheap q = some (.tcb x') from updKSc_same _ _ _] at h; cases h + change (updKSc σ1 q (.tcb x')).kheap d = _ at h + rw [updKSc_kheap, Function.update_of_ne hq] at h + by_cases ep : d = p + · subst d + rw [updKSc_same] at h + cases h + exact ⟨y, hs⟩ + · exact ⟨z, by simpa [σ1, Function.update_of_ne ep] using h⟩ + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScYieldMsgA.lean b/proofs/Fl4ProofsA64/P3/ScYieldMsgA.lean new file mode 100644 index 000000000..e59001494 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScYieldMsgA.lean @@ -0,0 +1,123 @@ +/- +P3 (AArch64): abstract one-word kernel reply when completing a yield. +The IPC-frame lookup returns a value for every related TCB. The reply +fits in a message register and keeps memory for either lookup outcome. +-/ +import Fl4ProofsA64.P3.ScConsumeMsgA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4SpecA64 Fl4Spec + +noncomputable section + +private theorem yieldGetIpcCapA {σ : State Unit} {p : ObjRef} {x : Tcb} + (hk : σ.kheap p = some (.tcb x)) : + getCap (Z := Unit) (p, tcbCnodeIndex 2) σ = ({(x.tcbIpcframe, σ)}, False) := by + unfold getCap + dsimp only + rw [nd_bind_oneSc (getObject_eqSc hk)] + dsimp only + rw [nd_pure_bindSc] + have h : tcbCnodeMap x (tcbCnodeIndex 2) = some x.tcbIpcframe := by + have e3 : toBl (2#3 : BitVec 3) ≠ toBl (3#3 : BitVec 3) := by decide + have e4 : toBl (2#3 : BitVec 3) ≠ toBl (4#3 : BitVec 3) := by decide + have e5 : toBl (2#3 : BitVec 3) ≠ toBl (5#3 : BitVec 3) := by decide + have e6 : toBl (2#3 : BitVec 3) ≠ toBl (6#3 : BitVec 3) := by decide + simp [tcbCnodeMap, tcbStage2Slot, tcbHighVSpaceSlot, Function.update_apply, tcbCnodeIndex, e3, e4, e5, e6] + rw [h] + rfl + +private theorem yieldLookupIpcA {σ : State Unit} {p : ObjRef} {x : Tcb} + (hk : σ.kheap p = some (.tcb x)) : + ∃ buf, lookupIpcBuffer (Z := Unit) true p σ = ({(buf, σ)}, False) := by + unfold lookupIpcBuffer + rw [nd_bind_oneSc (threadGet_eqSc _ hk), nd_pure_bindSc, nd_bind_oneSc (yieldGetIpcCapA hk)] + cases h : x.tcbIpcframe + all_goals try exact ⟨none, rfl⟩ + next a => + cases a + all_goals try exact ⟨none, rfl⟩ + next p R v d asid => + cases d + · by_cases hR : vmReadWrite ⊆ R + · exact ⟨some (p + (x.tcbIpcBuffer &&& mask (pageBitsForSize v))), by simp [hR]⟩ + · exact ⟨none, by simp [hR]⟩ + · exact ⟨none, rfl⟩ + +/-- Replace the indicated register and keep the TCB's other fields. -/ +def yieldRegA (x : Tcb) (r : Register) (v : Word) : Tcb := + { x with tcbArch := archTcbSetRegisters (Function.update (archTcbGetRegisters x.tcbArch) r v) x.tcbArch } + +theorem yieldAsUserRegA {σ : State Unit} {p : ObjRef} {x : Tcb} + (hk : σ.kheap p = some (.tcb x)) (r : Register) (v : Word) : + asUser (Z := Unit) p (setRegister r v) σ = ({((), updKSc σ p (.tcb (yieldRegA x r v)))}, False) := by + have hg : NonDet.getsThe (getTcb (Z := Unit) p) σ = ({(x, σ)}, False) := by + simp [NonDet.getsThe, getTcb, readObject, hk, NonDet.assertOpt] + rfl + unfold asUser + rw [nd_bind_oneSc hg, nd_pure_bindSc] + have hsel : NonDet.selectF (setRegister r v (archTcbContextGet x.tcbArch)) σ = + ({(((), ⟨(archTcbContextGet x.tcbArch).userFpuState, + Function.update (archTcbContextGet x.tcbArch).userRegs r v⟩), σ)}, False) := by + simp [setRegister, NonDet.modify, NonDet.selectF, Set.ext_iff] + rw [nd_bind_oneSc hsel] + dsimp only + rw [nd_pure_bindSc] + have hs := setObject_eqSc hk (o' := .tcb (yieldRegA x r v)) rfl + unfold yieldRegA archTcbSetRegisters archTcbGetRegisters at hs ⊢ + simp only [archTcbContextSet, archTcbContextGet] + rw [nd_bind_oneSc hs] + rfl + +private theorem yieldSetMrA {σ : State Unit} {p : ObjRef} {x : Tcb} + (hk : σ.kheap p = some (.tcb x)) (buf : Option ObjRef) (d : Data) : + setMrs (Z := Unit) p buf [d] σ = ({(natToLen 1, updKSc σ p (.tcb (yieldRegA x .x2 d)))}, False) := by + have hg : NonDet.getsThe (getTcb (Z := Unit) p) σ = ({(x, σ)}, False) := by + simp [NonDet.getsThe, getTcb, readObject, hk, NonDet.assertOpt] + rfl + unfold setMrs + rw [nd_bind_oneSc hg, nd_pure_bindSc, nd_pure_bindSc] + have reg : (fun r : Register => + if r ∈ msg_registers.take [d].length then the [d][theIndex msg_registers r]? + else archTcbGetRegisters x.tcbArch r) = + Function.update (archTcbGetRegisters x.tcbArch) .x2 d := by + funext r + by_cases hr : r = .x2 + · subst r + simp [msg_registers, msgRegisters, Register.x2ToX5, theIndex, the] + · simp [msg_registers, msgRegisters, Register.x2ToX5, hr, Function.update_of_ne] + rw [reg] + have hs := setObject_eqSc hk (o' := .tcb (yieldRegA x .x2 d)) rfl + unfold yieldRegA at hs + rw [nd_bind_oneSc hs, nd_pure_bindSc] + cases buf with + | none => rfl + | some pptr => + change ((NonDet.pure (σ := State Unit) () >>= fun _ => pure (natToLen 1)) + (updKSc σ p (.tcb (yieldRegA x .x2 d)))) = _ + exact nd_pure_bindSc () (fun _ => (pure (natToLen 1) : SMonad Unit LengthType)) _ + +/-- The reply to a live yielder sets three registers. The machine memory +and the TCB's IPC frame are projections of the original state. -/ +theorem yield_reply_wordA {σ : State Unit} {p : ObjRef} {x : Tcb} + (hk : σ.kheap p = some (.tcb x)) (d : Data) : + let z := yieldRegA (yieldRegA (yieldRegA x badge_register 0) .x2 d) msg_info_register + (messageInfoToData ⟨natToLen 1, 0, 0, 0⟩) + replyFromKernel (Z := Unit) p (0, [d]) σ = ({((), updKSc σ p (.tcb z))}, False) := by + dsimp only + obtain ⟨buf, hbuf⟩ := yieldLookupIpcA hk + unfold replyFromKernel + rw [nd_pure_bindSc] + dsimp only + rw [nd_bind_oneSc hbuf, nd_bind_oneSc (yieldAsUserRegA hk _ _)] + rw [nd_bind_oneSc (yieldSetMrA (updKSc_same _ _ _) _ _)] + unfold setMessageInfo + rw [yieldAsUserRegA (updKSc_same _ _ _)] + congr 2 + unfold updKSc + simp [Function.update_idem] + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScYieldQueueA.lean b/proofs/Fl4ProofsA64/P3/ScYieldQueueA.lean new file mode 100644 index 000000000..8caf305cf --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScYieldQueueA.lean @@ -0,0 +1,153 @@ +/- +P3 (AArch64): the queue effects of YieldTo, with MCP and yield links +carried through dequeue, enqueue and reschedule. +-/ +import Fl4ProofsA64.P3.ScYieldFrameA +import Fl4ProofsA64.P3.ScDeqA +import Fl4ProofsA64.P3.ScSwitchA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4SpecA64 Fl4Spec + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +theorem yieldAffKeepA {Z Z' : XSetsA} {m : McState} {κ : U64 → U64} {s s' : S} {σ σ' : State Unit} + (H : SchedSRA st Z m κ s σ) (H' : SchedSRA st Z' m κ s' σ') + {t : U64} (ht : t ∈ Z.T) (ht' : t ∈ Z'.T) + (hon : L.mem s (affA t) = L.cpu s) : L.mem s' (affA t) = L.cpu s' := by + rw [H.cpu_eq H', ← hon] + exact UScalar.eq_of_val_eq (Option.some.inj ((H'.sch.aff t ht').symm.trans (H.sch.aff t ht))) + +theorem yieldQueueFrameNA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s s' : S} {σ σ' : State Unit} + (H : SchedSRN st Z m κ s σ) (S0 : StEqA Z.T (L.mem s) σ) + (M : McpRelA Z.T m (L.mem s) σ) (Y : YieldRelA Z.T Z.C m (L.mem s) σ) + (hk : σ'.kheap = σ.kheap) (W : WritesIn st s s' (MWrA (st := st) Z m σ s)) : + StEqA Z.T (L.mem s') σ' ∧ McpRelA Z.T m (L.mem s') σ' ∧ YieldRelA Z.T Z.C m (L.mem s') σ' := by + have W' : WritesIn st s s' (fun b => MWrA (st := st) Z m σ s b ∨ ScConsumedRegsA (L.curW s) b) := + W.mono fun _ h => Or.inl h + have keep {t k : U64} (ht : t ∈ Z.T) (h : k = 3328#u64 ∨ k = 3440#u64 ∨ k = 3472#u64) := + yieldWordKeepA H H.curT W' ht h + refine ⟨S0.keep (fun t h => by rw [stA_eq]; exact keep h (Or.inl rfl)) + (fun t x h => ⟨x, hk ▸ h, rfl⟩), ?_⟩ + exact yieldFrameA M Y (fun t h => ⟨keep h (Or.inr (Or.inl rfl)), yieldFlagsKeepA H H.curT W' h⟩) + (fun t h => keep h (Or.inr (Or.inr rfl))) + (fun _ _ x h => ⟨x, hk.symm ▸ h, rfl, rfl, rfl⟩) (fun c hc => by + obtain ⟨y, n, h, -⟩ := H.scr.sc c hc + exact ⟨y, n, y, h, hk.symm ▸ h, rfl⟩) + +theorem yieldQueueFrameA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s s' : S} {σ σ' : State Unit} + (H : SchedSRA st Z m κ s σ) (S0 : StEqA Z.T (L.mem s) σ) + (M : McpRelA Z.T m (L.mem s) σ) (Y : YieldRelA Z.T Z.C m (L.mem s) σ) + (hk : σ'.kheap = σ.kheap) (W : WritesIn st s s' (MWrA (st := st) Z m σ s)) : + StEqA Z.T (L.mem s') σ' ∧ McpRelA Z.T m (L.mem s') σ' ∧ YieldRelA Z.T Z.C m (L.mem s') σ' := + yieldQueueFrameNA H.toSchedSRN S0 M Y hk W + +/-- The three relations after a ready-queue change or reschedule. -/ +structure YieldQueueRelA (Z : XSetsA) (m : McState) (κ : U64 → U64) (s : S) (σ : State Unit) : Prop where + sched : SchedSRA st Z m κ s σ + states : StEqA Z.T (L.mem s) σ + mcp : McpRelA Z.T m (L.mem s) σ + links : YieldRelA Z.T Z.C m (L.mem s) σ + +variable {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + +theorem yieldDequeueA (H : YieldQueueRelA (st := st) Z m κ s σ) {t : U64} (ht : t ∈ Z.T) + (hon : L.mem s (affA t) = L.cpu s) : + sched.dequeue st s t ⦃ s' => ∃ σ' Q', + tcbSchedAction (Z := Unit) tcbSchedDequeue (specPtr t) σ = ({((), σ')}, False) ∧ + σ'.kheap = σ.kheap ∧ σ'.releaseQueue = σ.releaseQueue ∧ + YieldQueueRelA (st := st) {Z with Q := Q'} m κ s' σ' ∧ MFrA (st := st) Z m s s' σ σ' ⦄ := by + refine WP.spec_mono (dequeue_refines_localA H.sched ht hon) ?_ + rintro s' ⟨σ', he, hk, hr, -, H', f⟩ + obtain ⟨S', M', Y'⟩ := yieldQueueFrameA H.sched H.states H.mcp H.links hk f.wr + exact ⟨σ', _, he, hk, hr, ⟨H', S', M', Y'⟩, f⟩ + +theorem yieldEnqueueA (H : YieldQueueRelA (st := st) Z m κ s σ) {t : U64} (ht : t ∈ Z.T) + (hon : L.mem s (affA t) = L.cpu s) + (hrun : ∀ x, σ.kheap (specPtr t) = some (.tcb x) → runnable x.tcbState = true) + (hnr : specPtr t ∉ σ.releaseQueue) : + sched.enqueue st s t ⦃ s' => ∃ σ' Q', + tcbSchedAction (Z := Unit) tcbSchedEnqueue (specPtr t) σ = ({((), σ')}, False) ∧ + σ'.kheap = σ.kheap ∧ σ'.releaseQueue = σ.releaseQueue ∧ + YieldQueueRelA (st := st) {Z with Q := Q'} m κ s' σ' ∧ MFrA (st := st) Z m s s' σ σ' ⦄ := by + refine WP.spec_mono (enqueue_refines_localA H.sched t ht hon + (fun _ => (H.sched.relOn ht hon).2 hnr) hrun) ?_ + rintro s' ⟨σ', he, hk, hr, -, w, H'⟩ + have f := MFrA.of_sw (Z := Z) (m := m) hk (w.mono fun _ h => H.sched.toSchedSRN.enq_swr ht h) + obtain ⟨S', M', Y'⟩ := yieldQueueFrameA H.sched H.states H.mcp H.links hk f.wr + exact ⟨σ', _, he, hk, hr, ⟨H', S', M', Y'⟩, f⟩ + +theorem yieldRescheduleA (H : YieldQueueRelA (st := st) Z m κ s σ) : + sched.reschedule_required st s ⦃ s' => ∃ σ' Q', + rescheduleRequired (Z := Unit) σ = ({((), σ')}, False) ∧ + σ'.kheap = σ.kheap ∧ σ'.releaseQueue = σ.releaseQueue ∧ + YieldQueueRelA (st := st) {Z with Q := Q'} m κ s' σ' ∧ MFrA (st := st) Z m s s' σ σ' ⦄ := by + refine WP.spec_mono (reschedule_required_refinesA H.sched H.states) ?_ + rintro s' ⟨σ', Q', he, hk, hr, w, H'⟩ + have f := MFrA.of_sw (Z := Z) (m := m) hk w + obtain ⟨S', M', Y'⟩ := yieldQueueFrameA H.sched H.states H.mcp H.links hk f.wr + exact ⟨σ', Q', he, hk, hr, ⟨H', S', M', Y'⟩, f⟩ + +end + +section +local notation "wa" => core.num.U64.wrapping_add +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- (record edit #8b-B1) A TCB's state, MCP and yield words are off the domain-schedule words. -/ +theorem yieldTcbNotMWrDA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRN st Z m κ s σ) {t K : U64} (ht : t ∈ Z.T) + (hK : K = 3328#u64 ∨ K = 3440#u64 ∨ K = 3472#u64) : + ¬ MWrDA (st := st) Z m σ s (wa t K) := by + rintro (h | h) + · exact yieldTcbNotMWrA H ht hK h + · have hb : K.val < 4096 := by rcases hK with rfl | rfl | rfl <;> decide + obtain ⟨hks, -⟩ := yieldTcbOffA H ht hb + unfold DomW at h + unfold ksSizeA at hks + omega + +/-- `yieldQueueFrameNA` across a write that may include the domain-schedule words. -/ +theorem yieldQueueFrameNDA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s s' : S} {σ σ' : State Unit} + (H : SchedSRN st Z m κ s σ) (S0 : StEqA Z.T (L.mem s) σ) + (M : McpRelA Z.T m (L.mem s) σ) (Y : YieldRelA Z.T Z.C m (L.mem s) σ) + (hk : σ'.kheap = σ.kheap) (W : WritesIn st s s' (MWrDA (st := st) Z m σ s)) : + StEqA Z.T (L.mem s') σ' ∧ McpRelA Z.T m (L.mem s') σ' ∧ YieldRelA Z.T Z.C m (L.mem s') σ' := by + have keep {t K : U64} (ht : t ∈ Z.T) (h : K = 3328#u64 ∨ K = 3440#u64 ∨ K = 3472#u64) : + L.mem s' (wa t K) = L.mem s (wa t K) := by + refine W.mem_eq (field_aligned (H.core.tcbs t ht) ?_ ?_) (yieldTcbNotMWrDA H ht h) + · rcases h with rfl | rfl | rfl <;> decide + · rcases h with rfl | rfl | rfl <;> decide + refine ⟨S0.keep (fun t h => by rw [stA_eq]; exact keep h (Or.inl rfl)) + (fun t x h => ⟨x, hk ▸ h, rfl⟩), ?_⟩ + have keepF {t : U64} (ht : t ∈ Z.T) : L.mem s' (flagsA t) = L.mem s (flagsA t) := by + refine W.mem_eq (field_aligned (H.core.tcbs t ht) (K := 3456#u64) (by decide) (by decide)) ?_ + rintro (h | h) + · exact yieldFlagsNotMWrA H ht h + · obtain ⟨hks, -⟩ := yieldTcbOffA H ht (K := 3456#u64) (by decide) + change DomW _ (wa t 3456#u64) at h + unfold DomW at h + unfold ksSizeA at hks + omega + exact yieldFrameA M Y (fun t h => ⟨keep h (Or.inr (Or.inl rfl)), keepF h⟩) + (fun t h => keep h (Or.inr (Or.inr rfl))) + (fun _ _ x h => ⟨x, hk.symm ▸ h, rfl, rfl, rfl⟩) (fun c hc => by + obtain ⟨y, n, h, -⟩ := H.scr.sc c hc + exact ⟨y, n, y, h, hk.symm ▸ h, rfl⟩) + +theorem yieldQueueFrameDA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s s' : S} {σ σ' : State Unit} + (H : SchedSRA st Z m κ s σ) (S0 : StEqA Z.T (L.mem s) σ) + (M : McpRelA Z.T m (L.mem s) σ) (Y : YieldRelA Z.T Z.C m (L.mem s) σ) + (hk : σ'.kheap = σ.kheap) (W : WritesIn st s s' (MWrDA (st := st) Z m σ s)) : + StEqA Z.T (L.mem s') σ' ∧ McpRelA Z.T m (L.mem s') σ' ∧ YieldRelA Z.T Z.C m (L.mem s') σ' := + yieldQueueFrameNDA H.toSchedSRN S0 M Y hk W + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScYieldRelA.lean b/proofs/Fl4ProofsA64/P3/ScYieldRelA.lean new file mode 100644 index 000000000..b24607cc5 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScYieldRelA.lean @@ -0,0 +1,165 @@ +/- +P3 (AArch64): the caller MCP and reciprocal yield links. These relations +mirror the MCP field of x86 ScDecodeSc.McpRel and the yield links of +ScYield.YieldRel. The SC link uses the current core's SC naming convention. +The Stage-3 join carries both relations independently of SchedSRA and StEqA. +-/ +import Fl4ProofsA64.P3.ScBootA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-- The MCP word of TCB `t` (AArch64 `layout::tcb::MCP`). -/ +def mcpA (t : U64) : U64 := wa t 3440#u64 + +/-- The TCB flags word of `t` (`tcb::FLAGS`, `+3456`). -/ +def flagsA (t : U64) : U64 := wa t 3456#u64 + +/-- (record edit #8b-C2, item 9 `hfl`) The flags word represents the spec flags: its flag bits decode to `tcbFlags` (the +raw word may also carry FL4's TRAP_SYSCALLS bit, which the ported projection forgets). -/ +def FlagsOkA (mem : U64 → U64) (t : U64) (x : Tcb) : Prop := + wordToTcbFlags (mem (flagsA t)).bv = x.tcbFlags + +/-- `FlagsOkA` across a step keeping the flags word and the TCB's flags. -/ +theorem FlagsOkA.keep {mem mem' : U64 → U64} {t : U64} {x y : Tcb} (h : FlagsOkA mem t x) + (hw : mem' (flagsA t) = mem (flagsA t)) (hf : y.tcbFlags = x.tcbFlags) : FlagsOkA mem' t y := by + unfold FlagsOkA at h ⊢ + rw [hw, hf] + exact h + +/-- The concrete MCP word represents the abstract maximum controlled priority, and (item 9) the flags word of a TCB that +is no core's idle thread its flags. Guarded by idle identity (lead ruling): `boot.rs:691` sets `FPU_DISABLED` on the idle +threads. -/ +def McpRelA (T : Set U64) (m : McState) (mem : U64 → U64) (σ : State Unit) : Prop := + ∀ t ∈ T, ∃ x, σ.kheap (specPtr t) = some (.tcb x) ∧ (mem (mcpA t)).val = x.tcbMcpriority.toNat ∧ + ((∀ c < fl4A64Platform.numNodes, specPtr t ≠ m.idleThreadOf c) → FlagsOkA mem t x) + +/-- **`McpRelA` across a step** (Integration round 2): the MCP and flags words of each listed TCB kept, and each TCB +kept with the same MCP and flags. -/ +theorem McpRelA.congrA {T : Set U64} {m : McState} {mem mem' : U64 → U64} {σ σ' : State Unit} + (h : McpRelA T m mem σ) (hmem : ∀ t ∈ T, mem' (mcpA t) = mem (mcpA t) ∧ mem' (flagsA t) = mem (flagsA t)) + (hb : ∀ t ∈ T, ∀ x, σ.kheap (specPtr t) = some (.tcb x) → ∃ y, σ'.kheap (specPtr t) = some (.tcb y) ∧ + y.tcbMcpriority = x.tcbMcpriority ∧ y.tcbFlags = x.tcbFlags) : McpRelA T m mem' σ' := fun t ht => by + obtain ⟨x, hx, e1, e2⟩ := h t ht + obtain ⟨y, hy, hm, hf⟩ := hb t ht x hx + exact ⟨y, hy, by rw [(hmem t ht).1, e1, hm], fun hn => (e2 hn).keep (hmem t ht).2 hf⟩ + +/-- The TCB word represents its yield target, and the TCB and SC yield +links point back to each other. The SC-side concrete word is in `ScRelWA`. -/ +structure YieldRelA (T C : Set U64) (m : McState) (mem : U64 → U64) (σ : State Unit) : Prop where + tcb : ∀ t ∈ T, ∃ x, σ.kheap (specPtr t) = some (.tcb x) ∧ + x.tcbYieldTo = optScA m (mem (yieldA t)) + toSc : ∀ t ∈ T, mem (yieldA t) ≠ 0#u64 → mem (yieldA t) ∈ C ∧ + ∃ y n, σ.kheap (scNameA m (mem (yieldA t))) = some (.schedContext y n) ∧ y.scYieldFrom = some (specPtr t) + frm : ∀ c ∈ C, ∀ y n, σ.kheap (scNameA m c) = some (.schedContext y n) → ∀ p, y.scYieldFrom = some p → + ∃ t ∈ T, specPtr t = p ∧ mem (yieldA t) = c + +theorem obj.Tcb.mcp.specA {S : Type} {st : machine.Store S} [L : LawfulStore st] (s : S) (t : U64) : + obj.Tcb.mcp st t s = ok (L.mem s (mcpA t)) := by + unfold obj.Tcb.mcp + rw [eq_of_spec_fd Fl4ProofsA64.P0.Consts.layout.tcb.MCP.spec, bind_ok] + simp only [lift, bind_ok] + change st.read s (mcpA t) = _ + exact L.read_eq s (mcpA t) + +section Boot +open Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P1.W7.R.ST.BootWit + +variable {kb ib r now mt sl : U64} {nc : Nat} {uw : U64 → U64} + +private theorem bootM_mcpA (P : BootLay kb ib nc) {t : U64} (ht : t ∈ (bootH kb ib nc).tcbs) : + bL.mem (bootS kb ib nc r now mt) (mcpA t) = 0#u64 := + tz P ht (K := 3440#u64) (k := 3440) (by simp) (by omega) (by unfold TZ; decide) + +/-- Every idle thread has MCP zero at `w0`. -/ +theorem w0_mcpRelA (hL : BootLayA ib nc) (P : BootLay kb ib nc) + (hnc : nc ≤ Fl4SpecA64.fl4A64Platform.numNodes) : + McpRelA (bootZA kb ib nc).T (bootMA ib nc) (bL.mem (bootS kb ib nc r now mt)) (bootSA kb ib nc now mt) := by + intro t ht + change t ∈ bootTA ib nc at ht + have hm : t ∈ (bootH kb ib nc).tcbs := by rwa [bootTA_eq hL] at ht + obtain ⟨c, hc, rfl⟩ := ht + exact ⟨_, bootKA_tcb hL hc, by rw [bootM_mcpA P hm]; rfl, + fun hg => absurd rfl (hg c (lt_of_lt_of_le hc hnc))⟩ + +/-- The idle TCBs and SCs have no yield links at `w0`. -/ +theorem w0_yieldRelA (hL : BootLayA ib nc) (P : BootLay kb ib nc) : + YieldRelA (bootZA kb ib nc).T (bootZA kb ib nc).C (bootMA ib nc) + (bL.mem (bootS kb ib nc r now mt)) (bootSA kb ib nc now mt) where + tcb t ht := by + change t ∈ bootTA ib nc at ht + have hm : t ∈ (bootH kb ib nc).tcbs := by rwa [bootTA_eq hL] at ht + obtain ⟨c, hc, rfl⟩ := ht + exact ⟨_, bootKA_tcb hL hc, by rw [m_yield P hm]; rfl⟩ + toSc t ht h := by + change t ∈ bootTA ib nc at ht + rw [bootTA_eq hL] at ht + exact (h (m_yield P ht)).elim + frm _ _ _ _ hk _ hp := by + rcases bootSA_cases hL hk with ⟨_, _, _, _, e⟩ | ⟨_, e⟩ | e + · cases e + · cases e; cases hp + · cases e + +/-- The idle threads keep MCP zero, and the root thread has MCP 255 at `w1`. -/ +theorem w1_mcpRelA (hL : BootLayA ib nc) (P : BootLay1 kb ib nc) + (hnc : nc ≤ Fl4SpecA64.fl4A64Platform.numNodes) : + McpRelA (w1ZA kb ib nc).T (w1MA ib nc) (bL.mem (w1S kb ib nc r now mt sl uw)) (w1SA kb ib nc now mt sl) := by + intro t ht + change t ∈ (w1H kb ib nc).tcbs at ht + rcases w1T P ht with hi | rfl + · obtain ⟨c, hc, rfl⟩ := bootT_of hL hi + refine ⟨_, w1KA_idle hL P hc, ?_, fun hg => absurd rfl (hg c (lt_of_lt_of_le hc hnc))⟩ + have hm := w1_tI (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) + P hi (K := 3440#u64) (by simp) (fun _ => by simp) + change bL.mem (w1S kb ib nc r now mt sl uw) (mcpA (idleTA ib c)) = _ at hm + rw [hm] + exact congrArg UScalar.val (bootM_mcpA (r := r) (now := now) (mt := mt) P.w0 hi) + · refine ⟨_, w1KA_rt, ?_, fun _ => ?_⟩ + · have hm := w1_tR (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) + P (K := 3440#u64) (by simp) + change bL.mem (w1S kb ib nc r now mt sl uw) (mcpA (bwv RTb)) = _ at hm + rw [hm] + rfl + · have hm := w1_tR (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) + P (K := 3456#u64) (by simp) + change bL.mem (w1S kb ib nc r now mt sl uw) (flagsA (bwv RTb)) = _ at hm + unfold FlagsOkA + rw [hm] + have h0 : rtW ib (3456#u64).val = 0#u64 := by unfold rtW; simp (config := {decide := true}) + rw [h0] + change wordToTcbFlags 0#u64.bv = (Fl4SpecA64.defaultTcb 0).tcbFlags + ext f + simp [wordToTcbFlags, Fl4SpecA64.defaultTcb] + +/-- Neither the idle threads nor the root task have a yield link at `w1`. -/ +theorem w1_yieldRelA (hL : BootLayA ib nc) (P : BootLay1 kb ib nc) : + YieldRelA (w1ZA kb ib nc).T (w1ZA kb ib nc).C (w1MA ib nc) + (bL.mem (w1S kb ib nc r now mt sl uw)) (w1SA kb ib nc now mt sl) where + tcb t ht := by + change t ∈ (w1H kb ib nc).tcbs at ht + rcases w1T P ht with hi | rfl + · obtain ⟨c, hc, rfl⟩ := bootT_of hL hi + exact ⟨_, w1KA_idle hL P hc, by rw [m1_yield P ht]; rfl⟩ + · exact ⟨_, w1KA_rt, by rw [m1_yield P ht]; rfl⟩ + toSc _ ht h := (h (m1_yield P ht)).elim + frm _ _ _ _ hk _ hp := by + change w1KA kb ib nc now mt sl _ = _ at hk + rcases w1KA_cases hL hk with e | e | e | e | ⟨_, e⟩ | e | ⟨_, e⟩ + all_goals try (cases e; done) + · cases e; cases hp + · rcases e with ⟨_, _, _, _, e⟩ | ⟨_, e⟩ | e + · cases e + · cases e; cases hp + · cases e + +end Boot + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScYieldResumeA.lean b/proofs/Fl4ProofsA64/P3/ScYieldResumeA.lean new file mode 100644 index 000000000..109136962 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScYieldResumeA.lean @@ -0,0 +1,86 @@ +/- +P3 (AArch64): resume in YieldTo. The scheduler's released-SC invariant +makes resume a stutter for a local bound target. +-/ +import Fl4ProofsA64.P3.ScResumeA +import Fl4ProofsA64.P3.ScSwitchA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4SpecA64 Fl4Spec + +noncomputable section + +private theorem yieldResume_specA {σ : State Unit} {p t : ObjRef} {x : SchedContext} {n : Nat} {xt : Tcb} + (hk : σ.kheap p = some (.schedContext x n)) (htc : x.scTcb = some t) (hkt : σ.kheap t = some (.tcb xt)) + (hne : scActive x = true → x.scRefills ≠ []) + (hrel : schedulable t σ = true → scActive x = true ∧ releasedSc σ x) : + schedContextResume (Z := Unit) p σ = ({((), σ)}, False) := by + unfold schedContextResume + rw [nd_bind_oneSc (getSchedContext_eqA hk), htc, + nd_bind_oneSc (show NonDet.assertOpt (σ := State Unit) (some t) σ = ({(t, σ)}, False) from rfl), nd_gets_bindSc] + cases e : schedulable t σ + · rfl + · obtain ⟨ha, hr, hs⟩ := hrel e + simp only [NonDet.when, ↓reduceIte] + rw [nd_bind_oneSc (threadGet_eqSc _ hkt), nd_bind_oneSc (getScRefillReady_eqA hk (hne ha)), + nd_bind_oneSc (getScRefillSufficient_eqA hk (hne ha) 0)] + have hs' : refillSufficient (0#64) (refillHd x) = true := hs + simp [ha, hr, hs'] + rfl + +/-- The A64 YieldTo target's resume agrees with the abstract resume, and +both keep the input state. A non-schedulable target takes the early exit. -/ +theorem yield_resume_noopA {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRA st Z m κ s σ) (hst : StEqA Z.T (L.mem s) σ) (a : A) + {sc t : U64} (hc : sc ∈ Z.C) (hsc0 : sc ≠ 0#u64) (ht : t ∈ Z.T) + (hon : L.mem s (affA t) = L.cpu s) (htw : L.mem s (scTcbA sc) = t) + (hts : L.mem s (scA t) = sc) + (hmb : sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) : + schedcontext.resume st ar s a sc ⦃ r => r = (s, a) ∧ + schedContextResume (Z := Unit) (scNameA m sc) σ = ({((), σ)}, False) ⦄ := by + obtain ⟨x, n, hk, hx⟩ := H.scr.sc sc hc + obtain ⟨xt, hkt, hxt⟩ := H.sch.tcb t ht + have ht0 : t ≠ 0#u64 := fun e => (H.core.tcbs t ht).ne0 (by rw [e]; rfl) + have htc : x.scTcb = some (specPtr t) := by rw [hx.tcb, ← scTcbA_eqSc, htw, optRefS_ne ht0] + have hxt' : xt.tcbSchedContext = some (scNameA m sc) := by rw [hxt, hts]; simp [optScA, hsc0, scNameA_eq] + have he := schedulable_eqA H.toSchedSRN ht (H.relOn ht hon) hst + have rel : schedulable (specPtr t) σ = true → scActive x = true ∧ releasedSc σ x := by + intro h + obtain ⟨hrun, hnr⟩ := schedulable_partsA h + have hp := (of_decide_eq_true (he.trans h)).2.2.1 + rw [hts, gt_iff_valSc, hx.max] at hp + have ha : scActive x = true := by simpa [scActive, activeSc] using hp + exact ⟨ha, H.rd _ xt hkt (Or.inl (hrun xt hkt)) xt _ x n hkt hxt' hk ha hnr⟩ + have hspec := yieldResume_specA hk htc hkt (H.inv.ne hk) rel + unfold schedcontext.resume + rw [eq_of_spec_fd (obj.Sc.is_null.spec sc), bind_ok, decide_eq_false hsc0] + simp only [Bool.false_eq_true, ↓reduceIte] + rw [eq_of_spec_fd (obj.Sc.tcb.spec s sc), bind_ok, htw, + eq_of_spec_fd (obj.Tcb.is_null.spec t), bind_ok, decide_eq_false ht0] + simp only [Bool.false_eq_true, ↓reduceIte] + rw [is_schedulable_eqA, bind_ok, he] + cases e : schedulable (specPtr t) σ + · exact (WP.spec_ok _).2 ⟨rfl, hspec⟩ + · simp only [↓reduceIte] + obtain ⟨ha, hr, hs⟩ := rel e + have hn := H.inv.ne hk ha + refine WP.spec_bind (ready_refinesA H.scr H.sch.time hc hk ha) ?_ + intro b hb + rw [getScRefillReady_eqA hk hn, hr] at hb + have eb := (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst hb))).1 + rw [← eb] + simp only [↓reduceIte] + refine WP.spec_bind (sufficient_refinesA a H.scr hc hk ha 0#u64 hmb) ?_ + intro b' hb' + rw [getScRefillSufficient_eqA hk hn] at hb' + change ({(refillSufficient 0 (refillHd x), σ)}, False) = _ at hb' + rw [hs] at hb' + have eb' := (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst hb'))).1 + rw [← eb'] + exact (WP.spec_ok _).2 ⟨rfl, hspec⟩ + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScYieldRunA.lean b/proofs/Fl4ProofsA64/P3/ScYieldRunA.lean new file mode 100644 index 000000000..61052d876 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScYieldRunA.lean @@ -0,0 +1,196 @@ +/- +P3 (AArch64): runnable caller transitions and the immediate YieldTo return. +-/ +import Fl4ProofsA64.P3.ScYieldCompleteA +import Fl4ProofsA64.P3.ScYieldQueueA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4SpecA64 Fl4Spec + +noncomputable section +local notation "wa" => core.num.U64.wrapping_add + +theorem yieldSetStateSpecA {σ : State Unit} {p : ObjRef} {x : Tcb} + (hk : σ.kheap p = some (.tcb x)) (ts : ThreadState) : + setThreadState (Z := Unit) p ts σ = + ({((), stsActA (updKSc σ p (.tcb {x with tcbState := ts})) p)}, False) := by + unfold setThreadState + rw [nd_bind_oneSc (threadSet_eqSc _ hk)] + unfold setThreadStateAct + repeat rw [nd_gets_bindSc] + unfold stsActA NonDet.when + split <;> rfl + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +theorem yieldRunnableA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : YieldQueueRelA (st := st) Z m κ s σ) {ct : U64} (ht : ct ∈ Z.T) + {x : Tcb} (hk : σ.kheap (specPtr ct) = some (.tcb x)) (hr : runnable x.tcbState = true) + {ts : ThreadState} (hn : runnable ts = true) {v : U64} (hv : v.val = tsCodeA ts) : + sched.set_thread_state st s ct v ⦃ s' => ∃ σ', + setThreadState (Z := Unit) (specPtr ct) ts σ = ({((), σ')}, False) ∧ + YieldQueueRelA (st := st) Z m κ s' σ' ∧ + σ'.kheap = (updKSc σ (specPtr ct) (.tcb {x with tcbState := ts})).kheap ∧ + σ'.curThread = σ.curThread ∧ σ'.releaseQueue = σ.releaseQueue ∧ SSzA σ σ' ∧ + WritesIn st s s' (fun b => SWrA (st := st) Z s b ∨ b = stA ct) ⦄ := by + refine WP.spec_mono (sc_set_runnable_stateA H.sched H.states ht hk hr hn hv) ?_ + rintro s' ⟨σ', he, H', S', hheap, w⟩ + have eq : σ' = stsActA (updKSc σ (specPtr ct) (.tcb {x with tcbState := ts})) (specPtr ct) := + (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst (he.symm.trans (yieldSetStateSpecA hk ts))))).2 + have keep {t K : U64} (h : t ∈ Z.T) (hK : K = 3440#u64 ∨ K = 3472#u64) : + L.mem s' (wa t K) = L.mem s (wa t K) := by + have ha : Aligned (wa t K) := by + rcases hK with rfl | rfl <;> exact field_aligned (H.sched.core.tcbs t h) (by decide) (by decide) + refine w.mem_eq ha ?_ + rintro (hm | hm) + · exact yieldTcbNotMWrA H.sched.toSchedSRN h (Or.inr hK) (Or.inr hm) + · rw [stA_eq] at hm + rcases hK with rfl | rfl <;> + exact field_ne (H.sched.core.tcbs t h) (H.sched.core.tcbs ct ht) (by decide) (by decide) (fun _ => by decide) hm + have keepF {t : U64} (h : t ∈ Z.T) : L.mem s' (flagsA t) = L.mem s (flagsA t) := by + refine w.mem_eq (field_aligned (H.sched.core.tcbs t h) (K := 3456#u64) (by decide) (by decide)) ?_ + rintro (hm | hm) + · exact yieldFlagsNotMWrA H.sched.toSchedSRN h (Or.inr hm) + · rw [stA_eq] at hm + exact field_ne (H.sched.core.tcbs t h) (H.sched.core.tcbs ct ht) (K1 := 3456#u64) (by decide) (by decide) + (fun _ => by decide) hm + have tview : ∀ t ∈ Z.T, ∀ z, σ.kheap (specPtr t) = some (.tcb z) → + ∃ z', σ'.kheap (specPtr t) = some (.tcb z') ∧ z'.tcbMcpriority = z.tcbMcpriority ∧ z'.tcbYieldTo = z.tcbYieldTo ∧ + z'.tcbFlags = z.tcbFlags := by + intro t ht' z hz + by_cases e : t = ct + · subst t + rw [hk] at hz + cases hz + exact ⟨{x with tcbState := ts}, by rw [hheap, updKSc_same], rfl, rfl, rfl⟩ + · exact ⟨z, by rwa [hheap, updKSc_kheap, Function.update_of_ne (fun h => e (specPtr_inj h))], rfl, rfl, rfl⟩ + obtain ⟨M', Y'⟩ := yieldFrameA H.mcp H.links (fun t ht' => ⟨keep ht' (Or.inl rfl), keepF ht'⟩) + (fun t ht' => keep ht' (Or.inr rfl)) tview (fun c hc => by + obtain ⟨y, n, k, -⟩ := H.sched.scr.sc c hc + have hn : scNameA m c ≠ specPtr ct := fun e => by rw [e, hk] at k; cases k + exact ⟨y, n, y, k, by rwa [hheap, updKSc_kheap, Function.update_of_ne hn], rfl⟩) + refine ⟨σ', he, ⟨H', S', M', Y'⟩, hheap, ?_, ?_, ?_, w⟩ + · rw [eq] + unfold stsActA + split <;> rfl + · rw [eq] + unfold stsActA + split <;> rfl + · intro p y n hy + rw [hheap, updKSc_kheap] at hy + by_cases e : p = specPtr ct + · rw [e, Function.update_self] at hy + cases hy + · exact ⟨y, by rwa [Function.update_of_ne e] at hy⟩ + +/-- The SC update and the optional one-word reply agree with the abstract +invocation result and the caller's user context. -/ +theorem yieldReturnA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : YieldQueueRelA (st := st) Z m κ s σ) (a : A) {sc ct : U64} + (hc : sc ∈ Z.C) (hct : ct ∈ Z.T) {x : Tcb} (hx : σ.kheap (specPtr ct) = some (.tcb x)) + (hr : runnable x.tcbState = true) (call : Bool) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) : + (do + let s1 ← schedcontext.set_consumed st ar s a sc ct call + sched.set_thread_state st s1 ct sched.TS_RUNNING) ⦃ s' => + ∃ tm σ1 σ', returnConsumed (Z := Unit) (scNameA m sc) σ = ({(wordsFromTime tm, σ1)}, False) ∧ + (do NonDet.when (call = true) (replyFromKernel (specPtr ct) (0, wordsFromTime tm)) + setThreadState (specPtr ct) .running) σ1 = ({((), σ')}, False) ∧ + YieldQueueRelA (st := st) Z m κ s' σ' ∧ SSzA σ σ' ∧ + WritesIn st s s' (fun b => MWrA (st := st) Z m σ s b ∨ b = stA ct ∨ (call = true ∧ ScConsumedRegsA ct b)) + ⦄ := by + let p := scNameA m sc + let q := specPtr ct + obtain ⟨y, n, hk, -⟩ := H.sched.scr.sc sc hc + obtain ⟨time, y', hf, he⟩ := yieldConsumed_viewA hk + let σ1 := updKSc σ p (.schedContext y' n) + have e1 : returnConsumed (Z := Unit) p σ = ({(wordsFromTime time, σ1)}, False) := by + unfold returnConsumed + rw [nd_bind_oneSc he] + rfl + refine WP.spec_bind (sc_consumed_msg_frameA H.sched H.states a hc hct call hmax htu) ?_ + rintro s1 ⟨tm, σc, hc', H1, S1, sz1, k1, w1⟩ + change returnConsumed (Z := Unit) p σ = _ at hc' + rw [e1] at hc' + have eσ : σc = σ1 := + (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst hc'))).2.symm + subst σc + have w1' : WritesIn st s s1 (fun b => MWrA (st := st) Z m σ s b ∨ ScConsumedRegsA ct b) := + w1.mono fun _ h => h.elim Or.inl (fun h => Or.inr h.2) + obtain ⟨M1, Y1⟩ := yieldFrameA H.mcp H.links + (fun t ht => ⟨yieldWordKeepA H.sched.toSchedSRN hct w1' ht (Or.inr (Or.inl rfl)), + yieldFlagsKeepA H.sched.toSchedSRN hct w1' ht⟩) + (fun t ht => yieldWordKeepA H.sched.toSchedSRN hct w1' ht (Or.inr (Or.inr rfl))) + (fun t _ x hx => ⟨x, k1 t x hx, rfl, rfl, rfl⟩) (fun c hd => by + by_cases e : c = sc + · subst c + exact ⟨y, n, y', hk, updKSc_same _ _ _, hf _ (Or.inl rfl)⟩ + · obtain ⟨v, k, hv, -⟩ := H.sched.scr.sc c hd + have hn := fun h => e (H.sched.scp.inj c hd sc hc h) + refine ⟨v, k, v, hv, ?_, rfl⟩ + change (updKSc σ p (.schedContext y' n)).kheap (scNameA m c) = _ + rwa [updKSc_kheap, Function.update_of_ne hn]) + have hq1 := k1 ct x hx + let z := yieldRegA (yieldRegA (yieldRegA x badge_register 0) .x2 (ucast 64 time)) msg_info_register + (messageInfoToData ⟨natToLen 1, 0, 0, 0⟩) + have hreply : ∃ σ2 x', + NonDet.when (call = true) (replyFromKernel (Z := Unit) q (0, wordsFromTime time)) σ1 = ({((), σ2)}, False) ∧ + YieldQueueRelA (st := st) Z m κ s1 σ2 ∧ σ2.kheap q = some (.tcb x') ∧ runnable x'.tcbState = true ∧ SSzA σ1 σ2 := by + cases call + · exact ⟨σ1, x, rfl, ⟨H1, S1, M1, Y1⟩, hq1, hr, SSzA.refl σ1⟩ + · let σ2 := updKSc σ1 q (.tcb z) + have hz : σ2.kheap q = some (.tcb z) := updKSc_same _ _ _ + have hstep := yieldTcbStepA (z := z) hq1 rfl rfl rfl rfl rfl + have H2 : SchedSRA st Z m κ s1 σ2 := + H1.stable (WritesIn.refl _ (fun _ => False)) (fun _ h => h.elim) hstep.1 hstep.2 + have tview : ∀ t ∈ Z.T, ∀ v, σ1.kheap (specPtr t) = some (.tcb v) → + ∃ v', σ2.kheap (specPtr t) = some (.tcb v') ∧ v'.tcbState = v.tcbState ∧ + v'.tcbMcpriority = v.tcbMcpriority ∧ v'.tcbYieldTo = v.tcbYieldTo ∧ v'.tcbFlags = v.tcbFlags := by + intro t ht v hv + by_cases eq : t = ct + · subst t; rw [hq1] at hv; cases hv + exact ⟨z, hz, rfl, rfl, rfl, rfl⟩ + · have ne : specPtr t ≠ q := fun e => eq (specPtr_inj e) + refine ⟨v, ?_, rfl, rfl, rfl, rfl⟩ + change (updKSc σ1 q (.tcb z)).kheap (specPtr t) = _ + rwa [updKSc_kheap, Function.update_of_ne ne] + obtain ⟨M2, Y2⟩ := yieldFrameA M1 Y1 (fun _ _ => ⟨rfl, rfl⟩) (fun _ _ => rfl) + (fun t ht v hv => by + obtain ⟨v', hv', _, hmp, hyt, hfl⟩ := tview t ht v hv; exact ⟨v', hv', hmp, hyt, hfl⟩) + (fun c hd => by + obtain ⟨v, k, hv, -⟩ := H1.scr.sc c hd + exact ⟨v, k, v, hv, (hstep.1.sc _ _ _ hv).trans hv, rfl⟩) + have S2 : StEqA Z.T (L.mem s1) σ2 := by + intro t ht v hv + obtain ⟨u, hu, -⟩ := H1.sch.tcb t ht + obtain ⟨u', hu', hs, -⟩ := tview t ht u hu + rw [hv] at hu' + cases hu' + rw [hs] + exact S1 t ht u hu + refine ⟨σ2, z, ?_, ⟨H2, S2, M2, Y2⟩, hz, hr, ?_⟩ + · exact yield_reply_wordA hq1 _ + · intro p y n hp + exact ⟨y, (hstep.1.scBack p y n hp).trans hp⟩ + obtain ⟨σ2, x', hrp, H2, hx', hr', sz2⟩ := hreply + have hv : sched.TS_RUNNING.val = tsCodeA .running := by rw [ts_vals.1]; rfl + refine WP.spec_mono (yieldRunnableA H2 hct hx' hr' rfl hv) ?_ + rintro s2 ⟨σ', hrun, H', -, -, -, sz3, w2⟩ + refine ⟨time, σ1, σ', e1, ?_, H', (sz1.trans sz2).trans sz3, ?_⟩ + · rw [nd_bind_oneSc hrp] + exact hrun + · refine (w1.trans w2).mono fun b h => ?_ + rcases h with (h | h) | h | h + · exact Or.inl h + · exact Or.inr (Or.inr h) + · exact Or.inl (Or.inr (swr_ksb rfl w1.1 h)) + · exact Or.inr (Or.inl h) + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScYieldSwitchA.lean b/proofs/Fl4ProofsA64/P3/ScYieldSwitchA.lean new file mode 100644 index 000000000..3523d1872 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScYieldSwitchA.lean @@ -0,0 +1,222 @@ +/- +P3 (AArch64): the local YieldTo scheduling branches. +-/ +import Fl4ProofsA64.P3.ScYieldQueueA +import Fl4ProofsA64.P3.ScYieldLinkA +import Fl4ProofsA64.P3.ScBindLinkA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4SpecA64 Fl4Spec + +noncomputable section +local notation "wa" => core.num.U64.wrapping_add + +/-- The return-now computation of l4v YieldTo. -/ +def yieldSwitchA (sc : ObjRef) : SMonad Unit Bool := do + let bound ← getScObjRef SchedContext.scTcb sc + let t ← NonDet.assertOpt bound + let ready ← NonDet.gets (schedulable t) + if ready then + let ct ← NonDet.gets fun s : State Unit => s.curThread + let p ← threadGet Tcb.tcbPriority t + let q ← threadGet Tcb.tcbPriority ct + if p < q then + tcbSchedAction tcbSchedDequeue t + tcbSchedAction tcbSchedEnqueue t + pure true + else + setTcbObjRef tcbYieldToUpdate ct (some sc) + setScObjRef scYieldFromUpdate sc (some ct) + tcbSchedAction tcbSchedDequeue t + tcbSchedAction tcbSchedEnqueue ct + tcbSchedAction tcbSchedEnqueue t + rescheduleRequired + pure false + else pure true + +private theorem yieldInstall_specA {σ : State Unit} {p q : ObjRef} {x : Tcb} {y : SchedContext} {n : Nat} + (hk : σ.kheap q = some (.tcb x)) (hs : σ.kheap p = some (.schedContext y n)) : + (do + setTcbObjRef (Z := Unit) tcbYieldToUpdate q (some p) + setScObjRef scYieldFromUpdate p (some q)) σ = ({((), yieldPairStA σ p q x y n true)}, False) := by + have hpq : p ≠ q := fun e => by rw [e, hk] at hs; cases hs + let x' := {x with tcbYieldTo := some p} + let y' := {y with scYieldFrom := some q} + have hs' : (updKSc σ q (.tcb x')).kheap p = some (.schedContext y n) := by + rw [updKSc_kheap, Function.update_of_ne hpq] + exact hs + change (threadSet _ q >>= fun _ => updateSchedContext p _) σ = _ + rw [nd_bind_oneSc (threadSet_eqSc _ hk)] + change updateSchedContext p (fun z => {z with scYieldFrom := some q}) (updKSc σ q (.tcb x')) = _ + rw [updateSchedContext_eqA hs'] + have eq : updKSc (updKSc σ q (.tcb x')) p (.schedContext y' n) = + updKSc (updKSc σ p (.schedContext y' n)) q (.tcb x') := by + unfold updKSc + rw [Function.update_comm hpq.symm] + rw [eq] + rfl + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +private theorem yieldInstall_wordsA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : YieldQueueRelA (st := st) Z m κ s σ) {sc ct : U64} (hc : sc ∈ Z.C) + (hct : ct ∈ Z.T) (hC0 : ∀ c ∈ Z.C, c ≠ 0#u64) + {x : Tcb} {y : SchedContext} {n : Nat} + (hk : σ.kheap (specPtr ct) = some (.tcb x)) (hs : σ.kheap (scNameA m sc) = some (.schedContext y n)) + (hy : L.mem s (yieldA ct) = 0#u64) (hf : y.scYieldFrom = none) : + let s' := L.wr (L.wr s (yieldA ct) sc) (scYieldFromA sc) ct + let σ' := yieldPairStA σ (scNameA m sc) (specPtr ct) x y n true + YieldQueueRelA (st := st) Z m κ s' σ' ∧ SSzA σ σ' ∧ + WritesIn st s s' (fun b => MWrA (st := st) Z m σ s b ∨ b = yieldA ct) := by + intro s' σ' + let s0 := L.wr (L.wr s (scYieldFromA sc) ct) (yieldA ct) sc + obtain ⟨H0, S0, M0, Y0, sz, w⟩ := + yield_pair_wordsA H.sched H.states H.mcp H.links hc hct hC0 hk hs (Or.inr ⟨hy, hf⟩) true + have hT := H.sched.core.tcbs ct hct + have hya : Aligned (yieldA ct) := field_aligned hT (by decide) (by decide) + obtain ⟨z, k, h, hr⟩ := H.sched.scr.sc sc hc + rw [hs] at h + cases h + have ha : scYieldFromA sc = scWA sc 56#u64 := by unfold scYieldFromA scWA layout.sc.YIELD_FROM; rfl + have hs56 := hr.hdr (K := 56#u64) (by decide) (by decide) + rw [← ha] at hs56 + have hm : MWrA (st := st) Z m σ s (scYieldFromA sc) := Or.inl ⟨sc, hc, y, n, hs, hr.in_obj (ha ▸ hs56.2)⟩ + have hn : scYieldFromA sc ≠ yieldA ct := by + intro e + rw [e] at hm + exact yieldTcbNotMWrA H.sched.toSchedSRN hct (Or.inr (Or.inr rfl)) hm + have eq : WritesIn st s0 s' (fun _ => False) := by + refine ⟨by simp [s0, s', L.ksb_wr], by simp [s0, s', L.cpu_wr], ?_⟩ + intro b hab _ + dsimp only [s0, s'] + rw [L.mem_wr _ _ _ _ hs56.1 hab, L.mem_wr _ _ _ _ hya hab, + L.mem_wr _ _ _ _ hya hab, L.mem_wr _ _ _ _ hs56.1 hab] + by_cases h : scYieldFromA sc = b + · subst b + simp [Ne.symm hn] + · simp [h] + have H' : SchedSRA st Z m κ s' σ' := ⟨H0.toSchedSRN.sameWordsA eq, H0.rd⟩ + have hsF : ∀ t ∈ Z.T, L.mem s' (stA t) = L.mem s0 (stA t) := fun t ht => by + rw [stA_eq] + exact eq.mem_eq (field_aligned (H.sched.core.tcbs t ht) (by decide) (by decide)) id + obtain ⟨M', Y'⟩ := yieldFrameA M0 Y0 + (fun t ht => ⟨eq.mem_eq (field_aligned (H.sched.core.tcbs t ht) (by decide) (by decide)) id, + eq.mem_eq (field_aligned (H.sched.core.tcbs t ht) (K := 3456#u64) (by decide) (by decide)) id⟩) + (fun t ht => eq.mem_eq (field_aligned (H.sched.core.tcbs t ht) (by decide) (by decide)) id) + (fun _ _ x h => ⟨x, h, rfl, rfl, rfl⟩) (fun c hc => by + obtain ⟨y, n, h, -⟩ := H0.scr.sc c hc + exact ⟨y, n, y, h, h, rfl⟩) + exact ⟨⟨H', S0.keep hsF (fun _ x hx => ⟨x, hx, rfl⟩), M', Y'⟩, sz, + (w.trans eq).mono fun _ h => h.elim id False.elim⟩ + +theorem yield_switches_localA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : YieldQueueRelA (st := st) Z m κ s σ) + {sc t ct : U64} (hc : sc ∈ Z.C) (ht : t ∈ Z.T) (hct : ct ∈ Z.T) (hne : t ≠ ct) + (hC0 : ∀ c ∈ Z.C, c ≠ 0#u64) (hcore : L.mem s (scCoreA sc) = L.cpu s) + (hon : L.mem s (affA t) = L.cpu s) (hcur : L.curW s = ct) + (hb : L.mem s (scTcbA sc) = t) (hy : L.mem s (yieldA ct) = 0#u64) + (hf : L.mem s (scYieldFromA sc) = 0#u64) + (hrc : ∀ x, σ.kheap (specPtr ct) = some (.tcb x) → runnable x.tcbState = true) + (hnrc : specPtr ct ∉ σ.releaseQueue) : + schedcontext.yield_switches st s sc ct ⦃ r => ∃ σ' Q', + yieldSwitchA (scNameA m sc) σ = ({(!r.1, σ')}, False) ∧ + YieldQueueRelA (st := st) {Z with Q := Q'} m κ r.2 σ' ∧ SSzA σ σ' ∧ + (r.1 = false → σ'.kheap = σ.kheap) ∧ + WritesIn st s r.2 (fun b => MWrA (st := st) Z m σ s b ∨ b = yieldA ct) ⦄ := by + obtain ⟨y, n, hk, hR⟩ := H.sched.scr.sc sc hc + obtain ⟨xt, htK, -, -, htp⟩ := H.sched.rest.prio t ht + obtain ⟨xc, hcK, -, -, hcp⟩ := H.sched.rest.prio ct hct + have ccur : σ.curThread = specPtr ct := H.sched.sch.curThread.trans (congrArg specPtr hcur) + have con : L.mem s (affA ct) = L.cpu s := by rw [← hcur]; exact H.sched.curOn + have ht0 : t ≠ 0#u64 := fun e => (H.sched.core.tcbs t ht).ne0 (by rw [e]; rfl) + have htb : y.scTcb = some (specPtr t) := by rw [hR.tcb, ← scTcbA_eqSc, hb, optRefS_ne ht0] + have hYF : y.scYieldFrom = none := by + have ha : scYieldFromA sc = scWA sc 56#u64 := by unfold scYieldFromA scWA layout.sc.YIELD_FROM; rfl + rw [hR.yieldFrom, ← ha, hf, optRefS_zero] + have pri : L.mem s (prioA t) < L.mem s (prioA ct) ↔ xt.tcbPriority < xc.tcbPriority := by + rw [UScalar.lt_equiv, BitVec.lt_def, ← htp, ← hcp] + have pre : ∀ (k : ObjRef → Bool → SMonad Unit Bool), + (getScObjRef (Z := Unit) SchedContext.scTcb (scNameA m sc) >>= fun b => + NonDet.assertOpt b >>= fun u => NonDet.gets (schedulable u) >>= k u) σ = + k (specPtr t) (schedulable (specPtr t) σ) σ := by + intro k + rw [nd_bind_oneSc (getScTcb_eqSc hk), htb, + nd_bind_oneSc (show NonDet.assertOpt (σ := State Unit) (some (specPtr t)) σ = ({(specPtr t, σ)}, False) from rfl), + nd_gets_bindSc] + unfold schedcontext.yield_switches + rw [eq_of_spec_fd (obj.Sc.tcb.spec s sc), bind_ok, hb, is_schedulable_eqA, bind_ok, + schedulable_eqA H.sched.toSchedSRN ht (H.sched.relOn ht hon) H.states] + cases e : schedulable (specPtr t) σ + · apply (WP.spec_ok _).2 + refine ⟨σ, Z.Q, ?_, H, SSzA.refl _, fun _ => rfl, WritesIn.refl _ _⟩ + unfold yieldSwitchA + rw [pre, e] + rfl + · simp only [↓reduceIte] + rw [eq_of_spec_fd (obj.Sc.cpu.spec s sc), bind_ok, hcore, L.cur_cpu_eq s, bind_ok] + simp only [bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + rw [eq_of_spec_fd (obj.Tcb.prio.spec s t), bind_ok, eq_of_spec_fd (obj.Tcb.prio.spec s ct), bind_ok] + obtain ⟨hr, hnr⟩ := schedulable_partsA e + by_cases hlt : L.mem s (prioA t) < L.mem s (prioA ct) + · simp only [hlt, ↓reduceIte] + refine WP.spec_bind (yieldDequeueA H ht hon) ?_ + rintro s1 ⟨σ1, Q1, d, k, r, H1, F1⟩ + refine WP.spec_bind (yieldEnqueueA H1 ht (yieldAffKeepA H.sched H1.sched ht ht hon) + (fun x hx => hr x (k ▸ hx)) (r.symm ▸ hnr)) ?_ + rintro s2 ⟨σ2, Q2, en, k2, -, H2, F2⟩ + refine (WP.spec_ok _).2 ⟨σ2, Q2, ?_, H2, F1.sz.trans F2.sz, fun _ => k2.trans k, + (F1.trans F2 rfl rfl).wr.mono (fun _ h => Or.inl h)⟩ + unfold yieldSwitchA + rw [pre, e] + simp only [↓reduceIte] + rw [nd_gets_bindSc, ccur, nd_bind_oneSc (threadGet_eqSc _ htK), nd_bind_oneSc (threadGet_eqSc _ hcK), + if_pos (pri.1 hlt), nd_bind_oneSc d, nd_bind_oneSc en] + rfl + · simp only [hlt, ↓reduceIte] + rw [eq_of_spec_fd (obj.Tcb.set_yield_to.spec s ct sc), bind_ok, + eq_of_spec_fd (obj.Sc.set_yield_from.spec _ sc ct), bind_ok] + obtain ⟨H1, sz, w⟩ := yieldInstall_wordsA H hc hct hC0 hcK hk hy hYF + have ep : specPtr t ≠ scNameA m sc := fun e => by rw [e, hk] at htK; cases htK + have ec : specPtr t ≠ specPtr ct := fun h => hne (specPtr_inj h) + let σ1 := yieldPairStA σ (scNameA m sc) (specPtr ct) xc y n true + have kt1 : σ1.kheap (specPtr t) = some (.tcb xt) := by + simpa [σ1, yieldPairStA, Function.update_of_ne ep, Function.update_of_ne ec] using htK + have kc1 : σ1.kheap (specPtr ct) = some (.tcb {xc with tcbYieldTo := some (scNameA m sc)}) := + updKSc_same _ _ _ + have d0 : σ1.releaseQueue = σ.releaseQueue := rfl + refine WP.spec_bind (yieldDequeueA H1 ht (yieldAffKeepA H.sched H1.sched ht ht hon)) ?_ + rintro s2 ⟨σ2, Q2, d, k2, r2, H2, F2⟩ + refine WP.spec_bind (yieldEnqueueA H2 hct (yieldAffKeepA H.sched H2.sched hct hct con) ?_ + ((r2.trans d0).symm ▸ hnrc)) ?_ + · intro x hx + rw [k2, kc1] at hx + cases hx + exact hrc xc hcK + rintro s3 ⟨σ3, Q3, ec', k3, r3, H3, F3⟩ + refine WP.spec_bind (yieldEnqueueA H3 ht (yieldAffKeepA H.sched H3.sched ht ht hon) ?_ + ((r3.trans (r2.trans d0)).symm ▸ hnr)) ?_ + · intro x hx + rw [k3, k2, kt1] at hx + cases hx + exact hr xt htK + rintro s4 ⟨σ4, Q4, et, -, -, H4, F4⟩ + refine WP.spec_bind (yieldRescheduleA H4) ?_ + rintro s5 ⟨σ5, Q5, rs, -, -, H5, F5⟩ + have f := ((F2.trans F3 rfl rfl).trans F4 rfl rfl).trans F5 rfl rfl + refine (WP.spec_ok _).2 ⟨σ5, Q5, ?_, H5, sz.trans f.sz, (fun h => by cases h), ?_⟩ + · unfold yieldSwitchA + rw [pre, e] + simp only [↓reduceIte] + rw [nd_gets_bindSc, ccur, nd_bind_oneSc (threadGet_eqSc _ htK), nd_bind_oneSc (threadGet_eqSc _ hcK), + if_neg (fun h => hlt (pri.2 h)), ← nd_bind_assocSc, + nd_bind_oneSc (yieldInstall_specA hcK hk), nd_bind_oneSc d, nd_bind_oneSc ec', nd_bind_oneSc et, nd_bind_oneSc rs] + rfl + · exact (w.trans f.wr).mono fun _ h => h.elim id (fun h => Or.inl (mwr_back rfl rfl sz w.1 h)) + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/ScYieldTopA.lean b/proofs/Fl4ProofsA64/P3/ScYieldTopA.lean new file mode 100644 index 000000000..0b1fab499 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ScYieldTopA.lean @@ -0,0 +1,227 @@ +/- +P3 (AArch64): the successful local-target SC YieldTo decoder. +[S] The SC's CORE is the current CPU. [J] The bound TCB's affinity +agrees with that of its SC. Stage 3 projects the equality via P1 +ScAff.core_eq from JEntA.sys.ent.bxi.scAff. The nonnull SC placement +and the caller outside the release queue are the existing ObjsOK.sc0 +and ScYieldGap entry conditions. +-/ +import Fl4ProofsA64.P3.ScYieldBodyA +import Fl4ProofsA64.P3.ScInvokeTopA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4SpecA64 Fl4Spec + +noncomputable section + +theorem decodeYieldTo_specA {σ : State Unit} {p t ct : ObjRef} {x c : Tcb} {y : SchedContext} + {n : Nat} {label : Data} (hl : genInvocationType label = .schedContextYieldTo) (caps : List Cap) + (hk : σ.kheap p = some (.schedContext y n)) (hb : y.scTcb = some t) + (ht : σ.kheap t = some (.tcb x)) (hc : σ.kheap ct = some (.tcb c)) (hcur : σ.curThread = ct) + (hne : t ≠ ct) (hprio : ¬ x.tcbPriority > c.tcbMcpriority) (hy : c.tcbYieldTo = none) : + (decodeSchedContextInvocation (Z := Unit) label p caps).run σ = + ({(.ok (.invokeSchedContextYieldTo p), σ)}, False) := by + unfold decodeSchedContextInvocation + rw [hl] + unfold decodeSchedContextYieldTo + rw [run_bind_okA (run_liftEA (getScTcb_eqSc hk)), hb] + simp only [NonDetE.whenE, reduceCtorEq, not_false_eq_true, ↓reduceIte, ndE_pure_bindA] + have current : NonDet.gets (fun s : State Unit => s.curThread) σ = ({(ct, σ)}, False) := by + change ({(σ.curThread, σ)}, False) = _ + rw [hcur] + rw [run_bind_okA (run_liftEA current)] + simp only [NonDetE.whenE, Option.some.injEq, hne, ↓reduceIte, ndE_pure_bindA, the_someA] + rw [run_bind_okA (run_liftEA (threadGet_eqSc _ ht)), run_bind_okA (run_liftEA (threadGet_eqSc _ hc))] + simp only [NonDetE.whenE, hprio, ↓reduceIte, ndE_pure_bindA] + rw [run_bind_okA (run_liftEA (threadGet_eqSc _ hc)), hy] + simp only [NonDetE.whenE, ne_eq, not_true_eq_false, ↓reduceIte, ndE_pure_bindA] + rfl + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +private theorem decodeYieldTo_concA {s s1 : S} {a a1 : A} {sc t ct : U64} + (ht : obj.Sc.tcb st sc s = ok t) + (he : (if t != 0#u64 then smp.remote_tcb_stall st ar s a t else ok (s, a)) = ok (s1, a1)) + (hq : t != 0#u64 → smp.remote_tcb_stall st ar s a t = ok (s, a)) + (ht' : obj.Sc.tcb st sc s1 = ok t) (hcur : sched.cur_thread st s1 = ok ct) (ht0 : t ≠ 0#u64) (hne : t ≠ ct) + (hp : ¬ L.mem s1 (prioA t) > L.mem s1 (mcpA ct)) (hy : L.mem s1 (yieldA ct) = 0#u64) + (call : Bool) (extra : Aeneas.Std.Array obj.Slot 3#usize) : + schedcontext.decode st ar s a fl4_abi.label.SchedContextYieldTo sc call extra = (do + let s2 ← sched.set_thread_state st s1 ct sched.TS_RESTART + let (s3, a2) ← schedcontext.yield_to st ar s2 a1 sc call + ok (core.result.Result.Ok (), s3, a2)) := by + unfold schedcontext.decode + rw [ht, bind_ok, he, bind_ok] + simp only [uncurry_apply_pair, hcur, bind_ok] + have hConsumed : fl4_abi.label.SchedContextYieldTo ≠ fl4_abi.label.SchedContextConsumed := by + unfold fl4_abi.label.SchedContextYieldTo fl4_abi.label.SchedContextConsumed + decide + have hBind : fl4_abi.label.SchedContextYieldTo ≠ fl4_abi.label.SchedContextBind := by + unfold fl4_abi.label.SchedContextYieldTo fl4_abi.label.SchedContextBind + decide + have hObj : fl4_abi.label.SchedContextYieldTo ≠ fl4_abi.label.SchedContextUnbindObject := by + unfold fl4_abi.label.SchedContextYieldTo fl4_abi.label.SchedContextUnbindObject + decide + have hUnb : fl4_abi.label.SchedContextYieldTo ≠ fl4_abi.label.SchedContextUnbind := by + unfold fl4_abi.label.SchedContextYieldTo fl4_abi.label.SchedContextUnbind + decide + simp only [hConsumed, hBind, hObj, hUnb, ↓reduceIte] + rw [ht', bind_ok, eq_of_spec_fd (obj.Tcb.is_null.spec t), bind_ok, decide_eq_false ht0] + simp only [Bool.false_eq_true, ↓reduceIte] + have htne : obj.Tcb.Insts.CoreCmpPartialEqTcb.eq t ct = ok false := by + unfold obj.Tcb.Insts.CoreCmpPartialEqTcb.eq + simp [hne] + rw [htne, bind_ok] + simp only [Bool.false_eq_true, ↓reduceIte] + rw [eq_of_spec_fd (obj.Tcb.prio.spec s1 t), bind_ok, obj.Tcb.mcp.specA, bind_ok, if_neg hp, + eq_of_spec_fd (obj.Tcb.yield_to.spec s1 ct), bind_ok, hy] + rfl + +private theorem optRefYield_injA {u v : U64} (h : optRefS u = optRefS v) : u = v := by + by_cases hu : u = 0#u64 <;> by_cases hv : v = 0#u64 + · exact hu.trans hv.symm + · rw [hu, optRefS_zero, optRefS_ne hv] at h; cases h + · rw [hv, optRefS_zero, optRefS_ne hu] at h; cases h + · rw [optRefS_ne hu, optRefS_ne hv] at h + exact specPtr_inj (Option.some.inj h) + +set_option maxHeartbeats 550000 in +/-- Successful SC YieldTo. [S] `hcore` limits the case to a local SC. +[J] `hAff` is the target's SC/TCB affinity equality, projected from +`JEntA.sys.ent.bxi.scAff` at Stage 3. The result carries both new +relations, the two previous-yielder arms, and the aligned write frame. -/ +theorem sc_decode_yield_to_localA (hA : P0.ArchTotal ar) + {Z : XSetsA} {m : McState} {κ : U64 → U64} {s s1 : S} {σ : State Unit} + (H : YieldQueueRelA (st := st) Z m κ s σ) + {a a1 : A} {sc t ct : U64} (hc : sc ∈ Z.C) (ht : t ∈ Z.T) (hct : ct ∈ Z.T) (hne : t ≠ ct) + (hC0 : ∀ c ∈ Z.C, c ≠ 0#u64) + (hcore : L.mem s (scCoreA sc) = L.cpu s) (hAff : L.mem s (affA t) = L.mem s (scCoreA sc)) + (hbound : L.mem s (scA t) = sc) + (hcurR : specPtr ct ∉ σ.releaseQueue) {x : Tcb} + (hx : σ.kheap (specPtr ct) = some (.tcb x)) (hr : runnable x.tcbState = true) + (htSc : obj.Sc.tcb st sc s = ok t) + (he : (if t != 0#u64 then smp.remote_tcb_stall st ar s a t else ok (s, a)) = ok (s1, a1)) + (hq : t != 0#u64 → smp.remote_tcb_stall st ar s a t = ok (s, a)) + (hc' : sched.cur_thread st s1 = ok ct) (htSc' : obj.Sc.tcb st sc s1 = ok t) + (hprio : ¬ L.mem s1 (prioA t) > L.mem s1 (mcpA ct)) (hy : L.mem s1 (yieldA ct) = 0#u64) + (hmb : sporadic.min_budget ar a1 ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ v, ar.ticks_to_us a1 v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (call : Bool) (extra : Aeneas.Std.Array obj.Slot 3#usize) {label : Data} + (hl : genInvocationType label = .schedContextYieldTo) (caps : List Cap) : + schedcontext.decode st ar s a fl4_abi.label.SchedContextYieldTo sc call extra ⦃ r => + r.1 = .Ok () ∧ r.2.2 = a1 ∧ ∃ data σ0 σ1 σ' Q', + (decodeSchedContextInvocation (Z := Unit) label (scNameA m sc) caps).run σ = + ({(.ok (.invokeSchedContextYieldTo (scNameA m sc)), σ)}, False) ∧ + setThreadState (Z := Unit) (specPtr ct) .restart σ = ({((), σ0)}, False) ∧ + invokeSchedContext (Z := Unit) (.invokeSchedContextYieldTo (scNameA m sc)) σ0 = ({(data, σ1)}, False) ∧ + ((data = [] ∧ σ' = σ1) ∨ ∃ tm, data = wordsFromTime tm ∧ + (do NonDet.when (call = true) (replyFromKernel (specPtr ct) (0, data)) + setThreadState (specPtr ct) .running) σ1 = ({((), σ')}, False)) ∧ + YieldQueueRelA (st := st) {Z with Q := Q'} m κ r.2.1 σ' ∧ + WritesIn st s r.2.1 (fun b => MWrA (st := st) Z m σ s b ∨ b = yieldA ct ∨ b = stA ct ∨ + (call = true ∧ ScConsumedRegsA ct b) ∨ + (let yf := L.mem s (scYieldFromA sc); yf ≠ 0#u64 ∧ (ScConsumedRegsA yf b ∨ b = yieldA yf))) + ⦄ := by + obtain ⟨H1, hs1, F0⟩ := sc_stall_entryA hA H.sched H.states he hq + obtain ⟨-, M1, Y1⟩ := yieldQueueFrameA H.sched H.states H.mcp H.links rfl F0.wr + have H1' : YieldQueueRelA (st := st) Z m κ s1 σ := ⟨H1, hs1, M1, Y1⟩ + have ht0 := (tcb_ne01Sc (H.sched.core.tcbs t ht)).1 + rw [decodeYieldTo_concA htSc he hq htSc' hc' ht0 hne hprio hy] + have hcur := ok_inj_ff ((eq_of_spec_fd (sched.cur_thread.spec (L := L) s1)).symm.trans hc') + have hwT := ok_inj_ff ((eq_of_spec_fd (obj.Sc.tcb.spec (L := L) s1 sc)).symm.trans htSc') + obtain ⟨y, n, hk, hyR⟩ := H1.scr.sc sc hc + obtain ⟨xt, hxt, -, -, htP⟩ := H1.rest.prio t ht + obtain ⟨xm, hxm, hm, -⟩ := M1 ct hct + rw [hx] at hxm + cases hxm + obtain ⟨xy, hxy, hyC⟩ := Y1.tcb ct hct + rw [hx] at hxy + cases hxy + have htb : y.scTcb = some (specPtr t) := by rw [hyR.tcb, ← scTcbA_eqSc, hwT, optRefS_ne ht0] + have hd : (decodeSchedContextInvocation (Z := Unit) label (scNameA m sc) caps).run σ = + ({(.ok (.invokeSchedContextYieldTo (scNameA m sc)), σ)}, False) := + decodeYieldTo_specA hl caps hk htb hxt hx (H1.sch.curThread.trans (congrArg specPtr hcur)) + (fun e => hne (specPtr_inj e)) + (by rw [GT.gt, BitVec.lt_def, ← htP, ← hm, ← UScalar.lt_equiv]; exact hprio) + (by rw [hyC, hy]; rfl) + have hv : sched.TS_RESTART.val = tsCodeA .restart := by rw [ts_vals.2.1]; rfl + refine WP.spec_bind (yieldRunnableA H1' hct hx hr rfl hv) ?_ + rintro s2 ⟨σ2, hRestart, H2, hheap, cur2, rel2, sz, w⟩ + let x2 := {x with tcbState := ThreadState.restart} + have hxc : σ2.kheap (specPtr ct) = some (.tcb x2) := by rw [hheap, updKSc_same] + have ne : scNameA m sc ≠ specPtr ct := fun e => by rw [e, hx] at hk; cases hk + have hk2 : σ2.kheap (scNameA m sc) = some (.schedContext y n) := by + rwa [hheap, updKSc_kheap, Function.update_of_ne ne] + obtain ⟨y2, n2, hks2, hy2⟩ := H2.sched.scr.sc sc hc + rw [hk2] at hks2 + cases hks2 + have hT2 : L.mem s2 (scTcbA sc) = t := by + apply optRefYield_injA + rw [scTcbA_eqSc, ← hy2.tcb, htb, optRefS_ne ht0] + have hcur2 : L.curW s2 = ct := specPtr_inj (H2.sched.sch.curThread.symm.trans + (cur2.trans (H1.sch.curThread.trans (congrArg specPtr hcur)))) + have hcore2 : L.mem s2 (scCoreA sc) = L.cpu s2 := by + rw [H2.sched.scCore sc hc, ← H.sched.scCore sc hc, hcore, H.sched.cpu_eq H2.sched] + have hAffTarget : ScAff st ({t} : Set U64) s := by + intro u hu _ + have e : u = t := by simpa using hu + subst u + rw [hbound, hAff] + have ha := yield_local_target_of_scAffA hAffTarget (by simp : t ∈ ({t} : Set U64)) (hC0 sc hc) hbound hcore + have hon2 := yieldAffKeepA H.sched H2.sched ht ht ha + obtain ⟨x0, h0, link0⟩ := H.sched.sch.tcb t ht + rw [hxt] at h0 + cases h0 + have hkT2 : σ2.kheap (specPtr t) = some (.tcb xt) := by + rwa [hheap, updKSc_kheap, Function.update_of_ne (fun e => hne (specPtr_inj e))] + obtain ⟨xt2, hxt2, link2⟩ := H2.sched.sch.tcb t ht + rw [hkT2] at hxt2 + cases hxt2 + have heq : optScA m (L.mem s2 (scA t)) = some (scNameA m sc) := by + rw [← link2, link0, hbound] + simp [optScA, hC0 _ hc, scNameA_eq] + have hs2 : L.mem s2 (scA t) = sc := by + by_cases e : L.mem s2 (scA t) = 0#u64 + · simp [optScA, e] at heq + · simp only [optScA, e, ↓reduceIte] at heq + exact H2.sched.scp.inj _ (H2.sched.rest.scMem t ht e) _ hc (Option.some.inj heq) + obtain ⟨z, hz, link⟩ := H2.links.tcb ct hct + rw [hxc] at hz + cases hz + have hyc2 : L.mem s2 (yieldA ct) = 0#u64 := by + change x.tcbYieldTo = _ at link + rw [hyC, hy] at link + by_contra e + simp [optScA, e] at link + obtain ⟨y0, n0, hk0, hy0⟩ := H.sched.scr.sc sc hc + rw [hk] at hk0 + cases hk0 + have sameYF : L.mem s2 (scYieldFromA sc) = L.mem s (scYieldFromA sc) := by + apply optRefYield_injA + unfold scYieldFromA layout.sc.YIELD_FROM + exact hy2.yieldFrom.symm.trans hy0.yieldFrom + have hBody := yieldBodyA H2 a1 hc ht hct hC0 hne hcore2 hon2 hcur2 hT2 hs2 hyc2 + hxc rfl (rel2.symm ▸ hcurR) call hmb hmax htu + refine WP.spec_bind hBody ?_ + rintro ⟨s3, a2⟩ ⟨ea, data, σ3, σ', Q', heBody, hret, H', -, w'⟩ + refine (WP.spec_ok _).2 ⟨rfl, ea, data, σ2, σ3, σ', Q', hd, hRestart, heBody, hret, H', ?_⟩ + have w0 := F0.wr.trans w + exact (w0.trans w').mono fun b e => by + rcases e with (e | e | e) | e | e | e | e | e + · exact Or.inl e + · exact Or.inl (Or.inr (swr_ksb rfl F0.wr.1 e)) + · exact Or.inr (Or.inr (Or.inl e)) + · exact Or.inl (mwr_back rfl rfl sz w0.1 e) + · exact Or.inr (Or.inl e) + · exact Or.inr (Or.inr (Or.inl e)) + · exact Or.inr (Or.inr (Or.inr (Or.inl e))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (by rwa [sameYF] at e)))) + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/SchedFootA.lean b/proofs/Fl4ProofsA64/P3/SchedFootA.lean new file mode 100644 index 000000000..8e94895e8 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/SchedFootA.lean @@ -0,0 +1,108 @@ +/- +P3 (aarch64, A64-P3 Stage-3 integration, D8/T1 step 2): **the write footprints of the endpoint scheduler operations**. + +`SchedOpsEA` (`P3/SchedOpsA`, ratified) relates the scheduler operations on `EpStateRel ∧ SR` and drops what they write. +`SchedFootEA` is its footprint twin, kept apart so that no ratified statement changes: for a write-set function +`Wr σ s` (the scheduler words of the state `s` related to the spec state `σ`; `MWrA` at the instance) every operation +writes inside `Wr σ s`, and `Wr` is monotone backwards along steps that keep the SC sizes (`SSzA`) and the kernel-state +block (`back`), which is what lets the footprints of consecutive operations compose to the entry state. + +No named premise. +-/ +import Fl4ProofsA64.P3.SchedOpsA +import Fl4ProofsA64.P3.ScSWrA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Notif + +noncomputable section + +section Hyps +variable {S : Type} (st : machine.Store S) [L : LawfulStore st] + (T E R : Set U64) (Bf : Set (U64 × Fl4SpecA64.Word)) (SR : S → Fl4SpecA64.State Unit → Prop) + (Wr : Fl4SpecA64.State Unit → S → U64 → Prop) + +/-- **The write footprints of the scheduler operations of the endpoint paths** (module docstring). The hypotheses +are those of the matching `SchedOpsEA` field. -/ +structure SchedFootEA : Prop where + /-- The write set of a later state is inside the write set of an earlier one, if the SC sizes and the kernel-state + block are kept. -/ + back : ∀ {σ σ1 : Fl4SpecA64.State Unit} {s s1 : S} {b : U64}, SSzA σ σ1 → L.ksb s1 = L.ksb s → Wr σ1 s1 b → Wr σ s b + /-- The spec's sporadic unblock keeps the SC sizes (a pure spec fact, `SPresInst`). -/ + unblockSz : ∀ (σ σ' : Fl4SpecA64.State Unit) (o : Option Fl4SpecA64.ObjRef), + Fl4SpecA64.ifSporadicCurScAssertRefillUnblockCheck (Z := Unit) o σ = ({((), σ')}, False) → SSzA σ σ' + scheduleTcb : ∀ s σ (t : U64), EpStateRel T E R Bf (L.mem s) σ → SR s σ → t ∈ T → + sched.schedule_tcb st s t ⦃ s' => WritesIn st s s' (Wr σ s) ⦄ + restartTail : ∀ s σ (t : U64) (x : Fl4SpecA64.Tcb), EpStateRel T E R Bf (L.mem s) σ → SR s σ → t ∈ T → + σ.kheap (specPtr t) = some (.tcb x) → (x.tcbState = .running ∨ x.tcbState = .restart) → + (∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) → + L.mem s (affA t) = L.cpu s → NotQueued σ (specPtr t) → + rustRestartTail st s t ⦃ s' => WritesIn st s s' (Wr σ s) ⦄ + reschedule : ∀ s σ, EpStateRel T E R Bf (L.mem s) σ → SR s σ → + sched.reschedule_required st s ⦃ s' => WritesIn st s s' (Wr σ s) ⦄ + unblockSporadic : ∀ s σ (t : U64) (x : Fl4SpecA64.Tcb), EpStateRel T E R Bf (L.mem s) σ → SR s σ → t ∈ T → + σ.kheap (specPtr t) = some (.tcb x) → Fl4SpecA64.runnable x.tcbState = false → NotQueued σ (specPtr t) → + endpoint.unblock_sporadic st s t ⦃ s' => WritesIn st s s' (Wr σ s) ⦄ + switchTo : ∀ s σ (t : U64) (x : Fl4SpecA64.Tcb), EpStateRel T E R Bf (L.mem s) σ → SR s σ → t ∈ T → + σ.kheap (specPtr t) = some (.tcb x) → x.tcbState = .running → + (∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) → + L.mem s (affA t) = L.cpu s → NotQueued σ (specPtr t) → + sched.possible_switch_to st s t ⦃ s' => WritesIn st s s' (Wr σ s) ⦄ + +/-- (Lift 2b) `SchedFootEA`'s `back` and `scheduleTcb` (the fields `SchedOpsE0A`'s users need). -/ +structure SchedFootE0A : Prop where + back : ∀ {σ σ1 : Fl4SpecA64.State Unit} {s s1 : S} {b : U64}, SSzA σ σ1 → L.ksb s1 = L.ksb s → Wr σ1 s1 b → Wr σ s b + scheduleTcb : ∀ s σ (t : U64), EpStateRel T E R Bf (L.mem s) σ → SR s σ → t ∈ T → + sched.schedule_tcb st s t ⦃ s' => WritesIn st s s' (Wr σ s) ⦄ + +variable {st T E R Bf SR Wr} in +theorem SchedFootEA.to0 (h : SchedFootEA st T E R Bf SR Wr) : SchedFootE0A st T E R Bf SR Wr := + ⟨h.back, h.scheduleTcb⟩ + +end Hyps + +section Fp +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {T E R : Set U64} + {Bf : Set (U64 × Fl4SpecA64.Word)} {SR : S → Fl4SpecA64.State Unit → Prop} + {Wr : Fl4SpecA64.State Unit → S → U64 → Prop} + +variable (st T E R Bf Wr) in +/-- The footprint shape of the IPC posts: endpoint-code words or scheduler words of the entry state. -/ +def FpA (σ : Fl4SpecA64.State Unit) (s s' : S) : Prop := + WritesIn st s s' (fun b => EWrite T E R Bf b ∨ Wr σ s b) + +theorem FpA.refl (σ : Fl4SpecA64.State Unit) (s : S) : FpA st T E R Bf Wr σ s s := WritesIn.refl _ _ + +/-- A footprint of a later segment joins the earlier one. -/ +theorem FpA.trans (F : SchedFootEA st T E R Bf SR Wr) {σ σ1 : Fl4SpecA64.State Unit} {s s1 s2 : S} + (h1 : FpA st T E R Bf Wr σ s s1) (hz : SSzA σ σ1) (h2 : FpA st T E R Bf Wr σ1 s1 s2) : + FpA st T E R Bf Wr σ s s2 := + (WritesIn.trans (st := st) h1 h2).mono fun _ hb => by + rcases hb with (h | h) | (h | h) + · exact Or.inl h + · exact Or.inr h + · exact Or.inl h + · exact Or.inr (F.back hz h1.1 h) + +theorem FpA.trans0 (F : SchedFootE0A st T E R Bf SR Wr) {σ σ1 : Fl4SpecA64.State Unit} {s s1 s2 : S} + (h1 : FpA st T E R Bf Wr σ s s1) (hz : SSzA σ σ1) (h2 : FpA st T E R Bf Wr σ1 s1 s2) : + FpA st T E R Bf Wr σ s s2 := + (WritesIn.trans (st := st) h1 h2).mono fun _ hb => by + rcases hb with (h | h) | (h | h) + · exact Or.inl h + · exact Or.inr h + · exact Or.inl h + · exact Or.inr (F.back hz h1.1 h) + +theorem FpA.of_ew {σ : Fl4SpecA64.State Unit} {s s' : S} (h : WritesIn st s s' (EWrite T E R Bf)) : + FpA st T E R Bf Wr σ s s' := h.mono fun _ hb => Or.inl hb + +theorem FpA.of_wr {σ : Fl4SpecA64.State Unit} {s s' : S} (h : WritesIn st s s' (Wr σ s)) : + FpA st T E R Bf Wr σ s s' := h.mono fun _ hb => Or.inr hb + +end Fp + +end + +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/SchedFootNA.lean b/proofs/Fl4ProofsA64/P3/SchedFootNA.lean new file mode 100644 index 000000000..da6ef1415 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/SchedFootNA.lean @@ -0,0 +1,88 @@ +/- +P3 (aarch64, A64-P3 Stage-3 integration, D8/T1 step 2): **the write footprints of the notification scheduler +operations** (`SchedOpsA`'s twin, as `SchedFootEA` is `SchedOpsEA`'s; no ratified statement changed). + +`SchedFootNA` gives each notification-side scheduler operation a `WritesIn … (Wr σ s)` with the same hypotheses as its +`SchedOpsA` field; `back` is the same backward transport along `SSzA` as `SchedFootEA.back`. `FpNA` is the notification +footprint shape `WritesIn (NWrite N T ∨ Wr σ s)`. + +No named premise. +-/ +import Fl4ProofsA64.P3.SchedOpsA +import Fl4ProofsA64.P3.ScSWrA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Notif +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4ProofsA64.P3.Sched + +noncomputable section + +section Hyps +variable {S A : Type} (st : machine.Store S) (ar : machine.Arch A) [L : LawfulStore st] + (N T : Set U64) (SR : S → Fl4SpecA64.State Unit → Prop) (donLocal : S → U64 → Prop) + (Wr : Fl4SpecA64.State Unit → S → U64 → Prop) + +/-- **The write footprints of the notification scheduler operations** (module docstring). -/ +structure SchedFootNA : Prop where + back : ∀ {σ σ1 : Fl4SpecA64.State Unit} {s s1 : S} {b : U64}, SSzA σ σ1 → L.ksb s1 = L.ksb s → Wr σ1 s1 b → Wr σ s b + scheduleTcb : ∀ s σ (t : U64), NtfnStateRel N T (L.mem s) σ → SR s σ → t ∈ T → + sched.schedule_tcb st s t ⦃ s' => WritesIn st s s' (Wr σ s) ⦄ + wakeTail : ∀ s (a : A) σ (t n : U64) (x : Fl4SpecA64.Tcb), NtfnStateRel N T (L.mem s) σ → SR s σ → t ∈ T → + n ∈ N → σ.kheap (specPtr t) = some (.tcb x) → x.tcbState = .running → NotQueued σ (specPtr t) → + L.mem s (affA t) = L.cpu s → donLocal s n → + rustWakeTail st ar s a t n ⦃ r => WritesIn st s r.1 (Wr σ s) ⦄ + recvTail : ∀ s (a : A) σ (t n : U64), NtfnStateRel N T (L.mem s) σ → SR s σ → t ∈ T → n ∈ N → + NotQueued σ (specPtr t) → (∀ x, σ.kheap (specPtr t) = some (.tcb x) → Fl4SpecA64.runnable x.tcbState = true) → + L.mem s (affA t) = L.cpu s → donLocal s n → + rustRecvTail st ar s a t n ⦃ r => WritesIn st s r.1 (Wr σ s) ⦄ + completeTail : ∀ s (a : A) σ (t n : U64), NtfnStateRel N T (L.mem s) σ → SR s σ → t ∈ T → n ∈ N → + NotQueued σ (specPtr t) → (∀ x, σ.kheap (specPtr t) = some (.tcb x) → Fl4SpecA64.runnable x.tcbState = true) → + L.mem s (affA t) = L.cpu s → donLocal s n → + rustCompleteTail st ar s a t n ⦃ r => WritesIn st s r.1 (Wr σ s) ⦄ + returnSc : ∀ s σ0 σ (t n : U64), NtfnStateRel N T (L.mem s) σ → SR s σ → t ∈ T → n ∈ N → + ActReady σ0 → BlockStep σ0 σ (specPtr t) (specPtr n) → (specPtr t) ≠ σ.idleThread → + notification.maybe_return_sched_context st s n t ⦃ s' => WritesIn st s s' (Wr σ s) ⦄ + restartTail : ∀ s σ (t : U64) (x : Fl4SpecA64.Tcb), NtfnStateRel N T (L.mem s) σ → SR s σ → t ∈ T → + σ.kheap (specPtr t) = some (.tcb x) → x.tcbState = .restart → + (∀ n ∈ N, ∀ z, σ.kheap (specPtr n) = some (.notification z) → specPtr t ∉ z.ntfnObj.ntfnQueue) → + L.mem s (affA t) = L.cpu s → NotQueued σ (specPtr t) → + rustRestartTailN st s t ⦃ s' => WritesIn st s s' (Wr σ s) ⦄ + reschedule : ∀ s σ, NtfnStateRel N T (L.mem s) σ → SR s σ → + sched.reschedule_required st s ⦃ s' => WritesIn st s s' (Wr σ s) ⦄ + +end Hyps + +section Fp +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {N T : Set U64} + {Wr : Fl4SpecA64.State Unit → S → U64 → Prop} + +variable (st N T Wr) in +/-- The footprint shape of the notification posts. -/ +def FpNA (σ : Fl4SpecA64.State Unit) (s s' : S) : Prop := + WritesIn st s s' (fun b => NWrite N T b ∨ Wr σ s b) + +theorem FpNA.refl (σ : Fl4SpecA64.State Unit) (s : S) : FpNA st N T Wr σ s s := WritesIn.refl _ _ + +theorem FpNA.of_nw {σ : Fl4SpecA64.State Unit} {s s' : S} (h : WritesIn st s s' (NWrite N T)) : + FpNA st N T Wr σ s s' := h.mono fun _ hb => Or.inl hb + +theorem FpNA.of_wr {σ : Fl4SpecA64.State Unit} {s s' : S} (h : WritesIn st s s' (Wr σ s)) : + FpNA st N T Wr σ s s' := h.mono fun _ hb => Or.inr hb + +/-- A footprint of a later segment joins the earlier one (`back` of `SchedFootNA`). -/ +theorem FpNA.trans {σ σ1 : Fl4SpecA64.State Unit} {s s1 s2 : S} + (hb : ∀ {σ σ1 : Fl4SpecA64.State Unit} {s s1 : S} {b : U64}, SSzA σ σ1 → L.ksb s1 = L.ksb s → + Wr σ1 s1 b → Wr σ s b) + (h1 : FpNA st N T Wr σ s s1) (hz : SSzA σ σ1) (h2 : FpNA st N T Wr σ1 s1 s2) : FpNA st N T Wr σ s s2 := + (WritesIn.trans (st := st) h1 h2).mono fun _ h => by + rcases h with (h | h) | (h | h) + · exact Or.inl h + · exact Or.inr h + · exact Or.inl h + · exact Or.inr (hb hz h1.1 h) + +end Fp + +end + +end Fl4ProofsA64.P3.Notif diff --git a/proofs/Fl4ProofsA64/P3/SchedOpsA.lean b/proofs/Fl4ProofsA64/P3/SchedOpsA.lean new file mode 100644 index 000000000..0d7b97950 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/SchedOpsA.lean @@ -0,0 +1,726 @@ +/- +P3 (aarch64, A64-P3-IPC gen 3): **the scheduler interface of the notification (and later endpoint) wake / block +paths**, `SchedOpsA` / `SchedHypsA` (x86 `P3/NtfnSched`'s `SchedHyps` and `P3/NtfnRecv`'s `RecvHyps`, ported and +split; the FL4 tails are the x86 text, extraction diffed). + +This is a **subsystem-composition interface**, not a premise of the refinement and not a permanent `[J]`: the +notification proofs (`P3/NtfnWakeA`, `P3/NtfnBlockA`) are parametric in the relation `SR` of the other subsystems +(scheduler queues and action, scheduling contexts, endpoints, ...) and in the contracts below; the A64 scheduler +track proves them once (for `SR` := `JAtZA`'s `SchedRelA` / `SchedRestA` part) and discharges them for the +Notification, Endpoint, Reply and Timer leaves alike. + +* `SchedHypsA st N T SR`: the laws of `SR` the notification code relies on: + - `stable`: `SR` ignores what the notification code changes itself (FL4 words of `NWrite`; the spec's `ntfn_obj` + and TCB state / user context, `NtfnStep`), when no thread is blocked (`Endp.NoBlockE`); + - `notQueued`: a thread that is not runnable is in no ready queue and not in the release queue (l4v + `valid_ready_qs`, `valid_release_q`); + - `block`: blocking a thread in no queue, not the switch target and not idle keeps `SR` (only its state changes; + gen 11, Architect 01:09Z: every other TCB keeps its state and reflects `inClassR` back, MCS `SchedSRA.block`). +* `SchedOpsA st ar N T SR`: each scheduler operation the paths call, with its relational contract and frame (FL4 + keeps `NtfnStateRel`'s footprint, `FootEq`; the spec keeps `KView`; `SR` after): + - `scheduleTcb`: `sched::schedule_tcb t` against `set_thread_state_act t` (`stsAct`); + - `wakeTail`: `rustWakeTail` (`maybe_donate_sched_context`, `is_schedulable` / `possible_switch_to`, + `unblock_sporadic`) against `specWakeTail`; + - `recvTail` / `completeTail`: the SC tails of `receive_signal` / `complete_signal` on an active notification; + - `returnSc`: `maybe_return_sched_context` against `maybe_return_sc` after a block (`BlockStep`, `ActReady`); + - (gen 5) `restartTail`: `notification::unblock_sporadic t` then `possible_switch_to t` (`rustRestartTailN`) + against the spec's sporadic check then `possible_switch_to t` (`Endp.specRestartTail`), for a restarted TCB in no + notification queue (`cancel_all_signals`); + - (gen 5) `reschedule`: `reschedule_required` against `reschedule_required`. + +* `SchedOpsEA st T E R Bf SR` (namespace `Endp`): the same for the endpoint paths, over `EpStateRel` (x86 + `P3/EpSched`'s `EpHyps`, plus the scheduler steps of `restart_thread_if_no_fault` / `cancel_badged_sends`): + - `stable`: `SR` ignores the endpoint code's own writes (`EWrite`) and spec steps (`EpStep`) that block no thread + (gen 5, `NoBlockE`; x86 `EpHyps.stable` with `ScInv.Inv.stable`'s side condition); + - `scheduleTcb`: `schedule_tcb t` against `set_thread_state_act t`, keeping `EpStateRel`'s footprint; + - `restartTail`: `endpoint::unblock_sporadic t` then `possible_switch_to t` against the spec's sporadic check + then `possible_switch_to t`, for a restarted TCB in no endpoint queue (`rustRestartTail` / `specRestartTail`) + on the current core (gen 7); (gen 12, ruling Q1) also for a TCB set `Running` (`Running ∨ Restart`); + - `reschedule`: `reschedule_required` against `reschedule_required`; + - (gen 6, ruling (a)) `rd`: a thread whose state and fault are in `inClassR` has `RelIfBound` (l4v + `released_ipc_queues`; x86 `ScInv.Inv.rd`) — a law of `SR`, not a premise; the MCS line discharges it from + `SchedSRA`. Used for the `Inactive → Restart` of a receiver after `reply_unlink` (`P3/EpCancelAllA`). + - (gen 9) `notQueued`; (gen 12, ruling Q2) `block`: `SchedHypsA.block`'s shape on `EWrite` / `EpStep` (the sender + of a call or faulted send set `Inactive`). + - (gen 13, ruling Q1, Architect 02:21Z) `unblockSporadic`: `endpoint::unblock_sporadic t` of a TCB that is not + runnable and in no scheduler queue against `if_sporadic_cur_sc_assert_refill_unblock_check` of its SC (one + outcome; `receive_ipc`'s dequeued sender, still blocked on send); `switchTo`: `possible_switch_to t` of a TCB set + `Running`, in no queue, on the current core, against `possible_switch_to` (`receive_ipc`'s non-call sender). +(gen 12, Architect 01:39Z) `SchedOpsA.recvTail` / `.completeTail` take `wakeTail`'s local-core preconditions +(`L.mem s (affA t) = L.cpu s`, `donLocal s n`). + +Objects are named by `specPtr` (`P3/PtrA`). Also the spec-side lemmas of the wake / block paths +(`setThreadState_eq`, `tcbNtfnDequeue_eq`, `removeAll_map`). No named premise. + +(gen 7, Architect 23:24Z) The unused `NoBlock` (and its helpers) is deleted (every step goes through `NoBlockE`). +**Local-core alignment with A64-P3-MCS** (ruling item 3; `Fl4SpecA64/Ext/MultiCore.lean` `McState.onCore`): +`SchedOpsA.restartTail`, `Endp.SchedOpsEA.restartTail` and `SchedOpsA.wakeTail` take `L.mem s (affA t) = L.cpu s`. +(gen 8, Architect ruling Q1) **The donation clause is a law pair**: `SchedOpsA` takes the predicate `donLocal : S → +U64 → Prop` (a structure parameter: a `Prop` structure cannot have a data field; MCS instantiates it as `fun s n => ∀ +sc, L.mem s (ntfnScA n) = sc → sc ≠ 0#u64 → L.mem s (scCoreA sc) = L.cpu s`), `wakeTail` takes `donLocal s n`, and the +law `donLocalKeep` carries it between related states agreeing on the notification's SC word. `P3/NtfnWakeA` takes it +at entry and applies `donLocalKeep` once before `wakeTail` (the SC word kept: `NtfnStateRel.sc` at both ends and the +spec's `ntfnSc` kept on the way, `NScK`). No SC offset, SC placement or frame on `scheduleTcb` / `cancelIpc`. +(gen 7, superseded) **Stop-first (not added): `wakeTail`'s donation clause** (`t.sc = 0 → n.sc ≠ 0 → sc.cpu = L.cpu s`, so `donate`'s +`migrate_tcb` takes its no-op branch, smp.rs 14–15): the wake paths (`P3/NtfnWakeA`) reach `wakeTail` after +`scheduleTcb` (and, for a bound receiver, `cancelIpc`), whose contracts say nothing of the SC's `CORE` word or the +TCB's `SC` word, and no IPC placement keeps the SC's `CORE` word off the notification / TCB words written on the way; +`SR` does not determine the `CORE` word (no spec counterpart, `ScRelA`), so no law can carry it. Threading it needs +a frame on those contracts (for `scheduleTcb`, MCS's `schedule_tcb_refinesA` already gives `WritesIn` of the action +word) and an SC placement hypothesis: a ruling. The endpoint / notification writes never touch an `AFFINITY` word, but the scheduler steps between the +restarts of a queue (`scheduleTcb`, `restartTail`, `reschedule`) are contracts that say nothing of it; both interfaces +therefore gain the law **`coreKeep`** (`CoreKeep`: two states related by `SR` have the same current core and the same +affinity words; for the MCS line's `SR` = `∃ Q', SchedSRA st {Z with Q := Q'} m`, with `m` fixed, this is +`SchedRelNoStA.core` / `.aff`), which carries the queue-wide local-core facts of the cancel lemmas across them. +-/ +import Fl4ProofsA64.P3.NtfnRecvA +import Fl4ProofsA64.P3.EpQueueA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Notif +open fl4_core_a64 Fl4ProofsA64.P1 + +noncomputable section + +/-! ## The tails -/ + +section Defs +variable {S A : Type} (st : machine.Store S) (ar : machine.Arch A) + +/-- FL4's scheduler tail of a wake-up (notification.rs `send_signal`, after +the badge is written). -/ +def rustWakeTail (s : S) (a : A) (t : obj.Tcb) (n : U64) : Result (S × A) := do + let (s4, a2) ← notification.maybe_donate_sched_context st ar s a t n + let b1 ← sched.is_schedulable st s4 t + let s5 ← if b1 then sched.possible_switch_to st s4 t else ok s4 + let s6 ← notification.unblock_sporadic st s5 t + ok (s6, a2) + +/-- FL4's wake-up of `t` by `n` with `badge`, after the dequeue / cancel. -/ +def rustWake (s : S) (a : A) (t : obj.Tcb) (n badge : U64) : Result (S × A) := do + let s2 ← sched.set_thread_state st s t sched.TS_RUNNING + let s3 ← obj.Tcb.set_reg st t s2 layout.reg.BADGE badge + rustWakeTail st ar s3 a t n + +end Defs + +section SpecDefs +open Fl4SpecA64 Fl4Spec + +/-- The spec's scheduler tail of a wake-up (Ipc_A `send_signal`, +`update_waiting_ntfn`, after `as_user … setRegister`). -/ +def specWakeTail (t n : ObjRef) : SMonad Unit Unit := do + maybeDonateSc t n + let sched ← NonDet.gets (schedulable t) + NonDet.when sched <| possibleSwitchTo t + let scopt ← getTcbObjRef Tcb.tcbSchedContext t + ifSporadicCurScTestRefillUnblockCheck scopt + +/-- The spec's wake-up of `t` by `n` with `badge`. -/ +def specWake (t n : ObjRef) (badge : Badge) : SMonad Unit Unit := do + setThreadState t .running + asUser t <| setRegister badge_register badge + specWakeTail t n + +/-- A scheduling context whose head refill is ready and sufficient (l4v +`released_sc`). -/ +def released (σ : State Unit) (y : SchedContext) : Prop := + refillReady σ.curTime (refillHd y) = true ∧ refillSufficient 0 (refillHd y) = true + +/-- The thread states whose bound SC is kept released (l4v `valid_ready_qs`, +`valid_sched_action`, `released_ipc_queues`: the notification- and +endpoint-blocked states). -/ +def inClass (ts : ThreadState) : Prop := runnable ts = true ∨ (∃ n, ts = .blockedOnNotification n) ∨ + (∃ e d, ts = .blockedOnSend e d) ∨ (∃ e r d, ts = .blockedOnReceive e r d) + +/-- **The states and faults whose bound SC is kept released**: `inClass` +but for a sender with a timeout fault (l4v `released_ipc_queues`: a thread +blocked on send needs `valid_sender_sc_tcb_at`, a released SC if bound, or a +timeout fault and an active SC; the active SC of a blocked thread is +`BlockedScActive`). A thread that `handle_timeout` blocks on its timeout +handler's endpoint is such a sender: its SC was not released. -/ +def inClassR (ts : ThreadState) (f : Option Fault) : Prop := + runnable ts = true ∨ (∃ n, ts = .blockedOnNotification n) ∨ + (∃ e d, ts = .blockedOnSend e d ∧ ∀ b, f ≠ some (.timeout b)) ∨ (∃ e r d, ts = .blockedOnReceive e r d) + +theorem inClassR.cls {ts : ThreadState} {f : Option Fault} (h : inClassR ts f) : inClass ts := by + rcases h with h | h | ⟨e, d, h, -⟩ | h + · exact Or.inl h + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr (Or.inl ⟨e, d, h⟩)) + · exact Or.inr (Or.inr (Or.inr h)) + +/-- `inClassR` from `inClass` and no timeout fault when blocked on send. -/ +theorem inClassR_of {ts : ThreadState} {f : Option Fault} (h : inClass ts) + (hs : ∀ e d, ts = .blockedOnSend e d → ∀ b, f ≠ some (.timeout b)) : inClassR ts f := by + rcases h with h | h | ⟨e, d, h⟩ | h + · exact Or.inl h + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr (Or.inl ⟨e, d, h, hs e d h⟩)) + · exact Or.inr (Or.inr (Or.inr h)) + +/-- `inClassR` of a state of `inClass` not blocked on send. -/ +theorem inClassR_ns {ts : ThreadState} {f : Option Fault} (h : inClass ts) (hs : ∀ e d, ts ≠ .blockedOnSend e d) : + inClassR ts f := + inClassR_of h fun e d e' => absurd e' (hs e d) + +/-- `inClassR` of a state of `inClass` without a timeout fault. -/ +theorem inClassR_nf {ts : ThreadState} {f : Option Fault} (h : inClass ts) (hf : ∀ b, f ≠ some (.timeout b)) : + inClassR ts f := + inClassR_of h fun _ _ _ => hf + +/-- **Not a sender with a timeout fault** (the exemption of `inClassR`; +l4v `valid_sender_sc_tcb_at`'s `is_timeout_fault`). -/ +def NotTmoS (x : Tcb) : Prop := ∀ e d b, x.tcbState = .blockedOnSend e d → x.tcbFault ≠ some (.timeout b) + +theorem inClassR_of_not {x : Tcb} (h : inClass x.tcbState) (hn : NotTmoS x) : inClassR x.tcbState x.tcbFault := + inClassR_of h fun e d hs b => hn e d b hs + +theorem inClassR.notTmoS {x : Tcb} (h : inClassR x.tcbState x.tcbFault) : NotTmoS x := by + intro e d b hs hf + rcases h with h | ⟨n, h⟩ | ⟨e', d', h, h'⟩ | ⟨e', r, d', h⟩ <;> rw [hs] at h + · cases h + · cases h + · exact h' b hf + · cases h + +theorem NotTmoS.of_fault {x : Tcb} (h : x.tcbFault = none) : NotTmoS x := fun _ _ _ _ e => by rw [h] at e; cases e + +theorem NotTmoS.of_ns {x : Tcb} (h : ∀ e d, x.tcbState ≠ .blockedOnSend e d) : NotTmoS x := + fun e d _ hs => absurd hs (h e d) + +/-- `inClassR` only reads the state, and the fault of a sender. -/ +theorem inClassR.congr {ts ts' : ThreadState} {f f' : Option Fault} (h : inClassR ts f) (hs : ts' = ts) + (hf : (∃ e d, ts = .blockedOnSend e d) → f' = f) : inClassR ts' f' := by + subst hs + rcases h with h | h | ⟨e, d, h, h'⟩ | h + · exact Or.inl h + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr (Or.inl ⟨e, d, h, by rw [hf ⟨e, d, h⟩]; exact h'⟩)) + · exact Or.inr (Or.inr (Or.inr h)) + +/-- Blocked on no notification and no endpoint (the blocked states of +`inClass`). -/ +def NotIpcBlk (ts : ThreadState) : Prop := + (∀ q, ts ≠ .blockedOnNotification q) ∧ (∀ e d, ts ≠ .blockedOnSend e d) ∧ (∀ e r d, ts ≠ .blockedOnReceive e r d) + +/-- A state of `inClass` blocked on nothing is runnable. -/ +theorem inClass_runnable {ts : ThreadState} (h : inClass ts) (hn : NotIpcBlk ts) : runnable ts = true := by + rcases h with h | ⟨q, rfl⟩ | ⟨e, d, rfl⟩ | ⟨e, r, d, rfl⟩ + · exact h + · exact absurd rfl (hn.1 q) + · exact absurd rfl (hn.2.1 e d) + · exact absurd rfl (hn.2.2 e r d) + +/-- The idle state is blocked on nothing. -/ +theorem NotIpcBlk.idle : NotIpcBlk .idleThreadState := by + refine ⟨fun q e => ?_, fun e d h => ?_, fun e r d h => ?_⟩ + · cases e + · cases h + · cases h + +/-- A runnable state is blocked on nothing. -/ +theorem NotIpcBlk.of_runnable {ts : ThreadState} (h : runnable ts = true) : NotIpcBlk ts := + ⟨fun q e => (by rw [e] at h; cases h), fun e d h' => (by rw [h'] at h; cases h), + fun e r d h' => (by rw [h'] at h; cases h)⟩ + +/-- The TCB at `p` has a released SC if bound to an active one and not in +the release queue. -/ +def RelIfBound (σ : State Unit) (p : ObjRef) : Prop := + ∀ x scp y n, σ.kheap p = some (.tcb x) → x.tcbSchedContext = some scp → + σ.kheap scp = some (.schedContext y n) → scActive y = true → p ∉ σ.releaseQueue → released σ y + +/-- (gen 5, Architect ruling Q1) **An endpoint step that blocks no thread**: each TCB keeps its state (and stays out +of `inClassR` if it was out), or becomes runnable from a non-idle state of `inClassR` or with a released SC, or +becomes `Inactive` from a non-runnable non-idle state (`restart_thread_if_no_fault` of a faulted thread, +`reply_unlink_tcb`, `blocked_cancel_ipc`, `cancel_signal`, `reply_remove_tcb`); mirrors x86 `ScInv.Inv.stable`. (gen 6, ruling (b): defined here, before `SchedHypsA`, whose `stable` takes it too.) -/ +def _root_.Fl4ProofsA64.P3.Endp.NoBlockE (σ σ' : State Unit) : Prop := + ∀ p x y, σ.kheap p = some (.tcb x) → σ'.kheap p = some (.tcb y) → + (y.tcbState = x.tcbState ∧ (inClassR y.tcbState y.tcbFault → inClassR x.tcbState x.tcbFault)) ∨ + (runnable y.tcbState = true ∧ x.tcbState ≠ .idleThreadState ∧ + (inClassR x.tcbState x.tcbFault ∨ RelIfBound σ p)) ∨ + (y.tcbState = .inactive ∧ runnable x.tcbState = false ∧ x.tcbState ≠ .idleThreadState) + +/-- The thread is in no ready queue and not in the release queue (l4v +`not_queued`, `not_in_release_q`). -/ +def NotQueued (σ : State Unit) (t : ObjRef) : Prop := + (∀ d p, t ∉ σ.readyQueues d p) ∧ t ∉ σ.releaseQueue + +end SpecDefs + +section Defs +variable {S A : Type} (st : machine.Store S) (ar : machine.Arch A) + +/-- FL4's tail of a receive on an active notification (after the +notification is made idle). -/ +def rustRecvTail (s : S) (a : A) (t : obj.Tcb) (n : U64) : Result (S × A) := do + let (s3, a1) ← notification.maybe_donate_sched_context st ar s a t n + let i ← obj.Tcb.sc st t s3 + let s4 ← sched.cur_sc st s3 + let b1 ← core.cmp.PartialEq.ne.trait_default obj.Sc.Insts.CoreCmpPartialEqSc i s4 + if b1 + then + let b2 ← sporadic.sc_sporadic st s3 i + if b2 + then + let s5 ← sporadic.unblock_check st s3 i + ok (s5, a1) + else ok (s3, a1) + else ok (s3, a1) + +/-- (gen 5) FL4's restart tail of a notification waiter (notification.rs `cancel_all_signals`' loop, after the state +word): the sporadic unblock, then the switch. -/ +def rustRestartTailN (s : S) (t : obj.Tcb) : Result S := do + let s2 ← notification.unblock_sporadic st s t + sched.possible_switch_to st s2 t + +/-- FL4's tail of `complete_signal`. -/ +def rustCompleteTail (s : S) (a : A) (t : obj.Tcb) (n : U64) : Result (S × A) := do + let (s3, a1) ← notification.maybe_donate_sched_context st ar s a t n + let i1 ← obj.Tcb.sc st t s3 + let b2 ← sporadic.sc_sporadic st s3 i1 + if b2 + then + let i2 ← obj.Ntfn.sc st n s3 + if i1 = i2 + then + let s4 ← sched.cur_sc st s3 + let b3 ← core.cmp.PartialEq.ne.trait_default obj.Sc.Insts.CoreCmpPartialEqSc i1 s4 + if b3 + then + let s5 ← sporadic.unblock_check st s3 i1 + ok (s5, a1) + else ok (s3, a1) + else ok (s3, a1) + else ok (s3, a1) + +end Defs + +section SpecDefs +open Fl4SpecA64 Fl4Spec + +/-- The spec's restart after `set_thread_state t Restart` (IpcCancel_A `restart_thread_if_no_fault`, +`remove_and_restart_ntfn_queued_thread`; gen 5: defined here, used by `SchedOpsA.restartTail` too). -/ +def _root_.Fl4ProofsA64.P3.Endp.specRestartTail (t : ObjRef) : SMonad Unit Unit := do + let scOpt ← getTcbObjRef Tcb.tcbSchedContext t + ifSporadicCurScAssertRefillUnblockCheck scOpt + possibleSwitchTo t + +/-- The spec's tail of `complete_signal`. -/ +def specCompleteTail (tptr ntfnptr : ObjRef) : SMonad Unit Unit := do + maybeDonateSc tptr ntfnptr + let scOpt ← getTcbObjRef Tcb.tcbSchedContext tptr + NonDet.maybeM (fun scp => do + let sc ← getSchedContext scp + NonDet.when sc.scSporadic <| do + let ntfnScp ← getNtfnObjRef Notification.ntfnSc ntfnptr + let curScPtr ← NonDet.gets fun s : State Unit => s.curSc + NonDet.when (scOpt = ntfnScp ∧ scp ≠ curScPtr) <| refillUnblockCheck scp) + scOpt + +/-- Hypothesis (l4v `valid_sched_action`, refill part): a schedulable +switch-to candidate has an SC whose head refill is sufficient and ready. -/ +def ActReady (σ : State Unit) : Prop := + ∀ t, σ.schedulerAction = .switchThread t → schedulable t σ = true → + ∃ x scp, σ.kheap t = some (.tcb x) ∧ x.tcbSchedContext = some scp ∧ + getScRefillSufficient (Z := Unit) scp 0 σ = ({(true, σ)}, False) ∧ + getScRefillReady (Z := Unit) scp σ = ({(true, σ)}, False) + +/-- What blocking thread `t` on notification `n` changes (NtfnBlock.lean): the +TCB's state (to one that is not runnable), the notification, possibly the +scheduler action (to choose-new). -/ +def BlockStep (σ σ' : State Unit) (t n : ObjRef) : Prop := + (σ'.schedulerAction = σ.schedulerAction ∨ σ'.schedulerAction = .chooseNewThread) ∧ + σ'.curTime = σ.curTime ∧ σ'.releaseQueue = σ.releaseQueue ∧ + (∀ p, p ≠ t → p ≠ n → σ'.kheap p = σ.kheap p) ∧ + (∃ x y, σ.kheap n = some (.notification x) ∧ σ'.kheap n = some (.notification y)) ∧ + (∃ x y, σ.kheap t = some (.tcb x) ∧ σ'.kheap t = some (.tcb y) ∧ + y.tcbSchedContext = x.tcbSchedContext ∧ runnable y.tcbState = false) + +end SpecDefs + +/-! ## The interface -/ + +section Hyps +variable {S A : Type} (st : machine.Store S) (ar : machine.Arch A) [L : LawfulStore st] + +/-- (gen 7) **`SR`-related states agree on the current core and the threads' affinity words** (the law `coreKeep` +of `SchedOpsA` / `Endp.SchedOpsEA`; MCS: `SchedRelNoStA.core` / `.aff` at a fixed `McState`). -/ +def CoreKeep (T : Set U64) (s s' : S) : Prop := + L.cpu s' = L.cpu s ∧ ∀ u ∈ T, L.mem s' (affA u) = L.mem s (affA u) + +variable {st} in +/-- (gen 7) A thread on the current core stays there across `CoreKeep`. -/ +theorem CoreKeep.onCore {T : Set U64} {s s' : S} (h : CoreKeep st T s s') {t : U64} (ht : t ∈ T) + (hon : L.mem s (affA t) = L.cpu s) : L.mem s' (affA t) = L.cpu s' := by + rw [h.2 t ht, h.1, hon] + +/-- `c` (FL4, from `s`) refines `m` (spec, from `σ`): the spec does not fail, FL4's outcome is matched by one of +the spec's, FL4 keeps the footprint, the spec keeps `KView`, and `SR` holds after (x86 `TailRef`). -/ +def TailRef (N T : Set U64) (SR : S → Fl4SpecA64.State Unit → Prop) {α : Type} (fst : α → S) (s : S) + (σ : Fl4SpecA64.State Unit) (c : Result α) (m : Fl4SpecA64.SMonad Unit Unit) : Prop := + c ⦃ r => ¬ (m σ).2 ∧ ∃ σ', ((), σ') ∈ (m σ).1 ∧ FootEq N T (L.mem s) (L.mem (fst r)) ∧ KView σ σ' ∧ + SR (fst r) σ' ⦄ + +/-- **The laws of the other subsystems' relation `SR`** that the notification paths rely on (x86 `SchedHyps`' +`stable` / `notQueued` / `block`; subsystem-composition interface, see the module docstring). -/ +structure SchedHypsA (N T : Set U64) (SR : S → Fl4SpecA64.State Unit → Prop) : Prop where + /-- `SR` ignores the notification code's own writes and spec steps that block no thread (gen 6, ruling (b): the + three-branch `NoBlockE`, as `Endp.SchedOpsEA.stable`). -/ + stable : ∀ s s' σ σ', SR s σ → WritesIn st s s' (NWrite N T) → NtfnStep σ σ' → Endp.NoBlockE σ σ' → SR s' σ' + /-- A thread that is not runnable is in no queue (l4v `valid_ready_qs`, `valid_release_q`). -/ + notQueued : ∀ s σ (t : U64) (x : Fl4SpecA64.Tcb), SR s σ → σ.kheap (specPtr t) = some (.tcb x) → + Fl4SpecA64.runnable x.tcbState = false → NotQueued σ (specPtr t) + /-- Blocking a thread in no queue, not the switch target and not idle: only its TCB's state changes. (gen 11, + Architect 01:09Z, MCS `SchedSRA.block`) Every other TCB keeps its state and reflects `inClassR` back (its fault + word may change only without entering `inClassR`). -/ + block : ∀ s s' σ σ' (t : U64), SR s σ → NotQueued σ (specPtr t) → RelIfBound σ (specPtr t) → + (∀ u, σ.schedulerAction = .switchThread u → u ≠ (specPtr t)) → (specPtr t) ≠ σ.idleThread → + WritesIn st s s' (NWrite N T) → NtfnStep σ σ' → + (∀ p x y, σ.kheap p = some (.tcb x) → σ'.kheap p = some (.tcb y) → p ≠ (specPtr t) → + y.tcbState = x.tcbState ∧ (inClassR y.tcbState y.tcbFault → inClassR x.tcbState x.tcbFault)) → + SR s' σ' + +/-- **The scheduler operations of the notification wake / block paths, each with its relational contract and +frame** (x86 `SchedHyps`' `scheduleTcb` / `wakeTail` and `RecvHyps`; subsystem-composition +interface, see the module docstring). -/ +structure SchedOpsA (N T : Set U64) (SR : S → Fl4SpecA64.State Unit → Prop) (donLocal : S → U64 → Prop) : Prop where + /-- `sched::schedule_tcb t` refines `set_thread_state_act t`. -/ + scheduleTcb : ∀ s σ (t : U64), NtfnStateRel N T (L.mem s) σ → SR s σ → t ∈ T → + sched.schedule_tcb st s t ⦃ s' => FootEq N T (L.mem s) (L.mem s') ∧ SR s' (stsAct σ (specPtr t)) ⦄ + /-- The scheduler tail of a wake-up (`maybe_donate_sched_context`, `possible_switch_to`, the sporadic unblock). + (gen 7, ruling item 3) For a thread on the current core; (gen 8, ruling Q1) with the notification's donation + clause `donLocal s n` (MCS: its SC, if any, on the current core). -/ + wakeTail : ∀ s (a : A) σ (t n : U64) (x : Fl4SpecA64.Tcb), NtfnStateRel N T (L.mem s) σ → SR s σ → t ∈ T → + n ∈ N → σ.kheap (specPtr t) = some (.tcb x) → x.tcbState = .running → NotQueued σ (specPtr t) → + L.mem s (affA t) = L.cpu s → donLocal s n → + TailRef st N T SR Prod.fst s σ (rustWakeTail st ar s a t n) (specWakeTail (specPtr t) (specPtr n)) + /-- The SC tail of `receive_signal` on an active notification. (gen 12, Architect 01:39Z) With `wakeTail`'s + local-core preconditions: the thread on the current core and the donation clause `donLocal s n`. -/ + recvTail : ∀ s (a : A) σ (t n : U64), NtfnStateRel N T (L.mem s) σ → SR s σ → t ∈ T → n ∈ N → + NotQueued σ (specPtr t) → (∀ x, σ.kheap (specPtr t) = some (.tcb x) → Fl4SpecA64.runnable x.tcbState = true) → + L.mem s (affA t) = L.cpu s → donLocal s n → + TailRef st N T SR Prod.fst s σ (rustRecvTail st ar s a t n) (specRecvTail (specPtr t) (specPtr n)) + /-- The SC tail of `complete_signal` on an active notification. (gen 12, Architect 01:39Z) With `wakeTail`'s + local-core preconditions, as `recvTail`. -/ + completeTail : ∀ s (a : A) σ (t n : U64), NtfnStateRel N T (L.mem s) σ → SR s σ → t ∈ T → n ∈ N → + NotQueued σ (specPtr t) → (∀ x, σ.kheap (specPtr t) = some (.tcb x) → Fl4SpecA64.runnable x.tcbState = true) → + L.mem s (affA t) = L.cpu s → donLocal s n → + TailRef st N T SR Prod.fst s σ (rustCompleteTail st ar s a t n) (specCompleteTail (specPtr t) (specPtr n)) + /-- `maybe_return_sched_context` after blocking `t` on `n`. -/ + returnSc : ∀ s σ0 σ (t n : U64), NtfnStateRel N T (L.mem s) σ → SR s σ → t ∈ T → n ∈ N → + ActReady σ0 → BlockStep σ0 σ (specPtr t) (specPtr n) → (specPtr t) ≠ σ.idleThread → + TailRef st N T SR id s σ (notification.maybe_return_sched_context st s n t) + (Fl4SpecA64.maybeReturnSc (specPtr n) (specPtr t)) + /-- (gen 5, ruling Q4a) The sporadic unblock and the switch of a TCB restarted out of a notification queue + (`cancel_all_signals`' per-waiter tail; the twin of `Endp.SchedOpsEA.restartTail`). `KView` keeps every TCB's + state, so the other TCBs' states (`KeepO`) too. -/ + restartTail : ∀ s σ (t : U64) (x : Fl4SpecA64.Tcb), NtfnStateRel N T (L.mem s) σ → SR s σ → t ∈ T → + σ.kheap (specPtr t) = some (.tcb x) → x.tcbState = .restart → + (∀ n ∈ N, ∀ z, σ.kheap (specPtr n) = some (.notification z) → specPtr t ∉ z.ntfnObj.ntfnQueue) → + L.mem s (affA t) = L.cpu s → NotQueued σ (specPtr t) → + TailRef st N T SR id s σ (rustRestartTailN st s t) (Endp.specRestartTail (specPtr t)) + /-- (gen 5, ruling Q4a) `sched::reschedule_required` refines `reschedule_required`. -/ + reschedule : ∀ s σ, NtfnStateRel N T (L.mem s) σ → SR s σ → + TailRef st N T SR id s σ (sched.reschedule_required st s) Fl4SpecA64.rescheduleRequired + /-- (gen 7) `SR`-related states agree on the current core and the affinity words (`CoreKeep`). -/ + coreKeep : ∀ s s' σ σ', SR s σ → SR s' σ' → CoreKeep st T s s' + /-- (gen 8, ruling Q1) The donation clause is kept between related states that agree on the notification's SC + word (`nScA n`, the P1 `ntfnScA n` by `rfl`); MCS: `donLocal s n := ∀ sc, L.mem s (ntfnScA n) = sc → sc ≠ 0#u64 → + L.mem s (scCoreA sc) = L.cpu s`, kept by `SR` (the SC's `CORE` word and the current core). -/ + donLocalKeep : ∀ s s' σ σ' (n : U64), n ∈ N → NtfnStateRel N T (L.mem s) σ → NtfnStateRel N T (L.mem s') σ' → + SR s σ → SR s' σ' → L.mem s' (nScA n) = L.mem s (nScA n) → donLocal s n → donLocal s' n + +end Hyps + +/-! ## The spec side -/ + +/-! ## The spec side -/ + +section Spec +open Fl4SpecA64 Fl4Spec +variable {Z : Type} + +theorem assert_true {P : Prop} (h : P) (σ : State Z) : NonDet.assert P σ = ({((), σ)}, False) := by + simp [NonDet.assert, h] + +theorem stsAct_kheap (σ : State Z) (t : ObjRef) : (stsAct σ t).kheap = σ.kheap := by + unfold stsAct; split_ifs <;> rfl + +theorem stsAct_readyQueues (σ : State Z) (t : ObjRef) : (stsAct σ t).readyQueues = σ.readyQueues := by + unfold stsAct; split_ifs <;> rfl + +theorem stsAct_releaseQueue (σ : State Z) (t : ObjRef) : (stsAct σ t).releaseQueue = σ.releaseQueue := by + unfold stsAct; split_ifs <;> rfl + +theorem stsAct_curTime (σ : State Z) (t : ObjRef) : (stsAct σ t).curTime = σ.curTime := by + unfold stsAct; split_ifs <;> rfl + +/-- (gen 9) `NotQueued` across `set_thread_state` (`stsAct` of a TCB write keeps both queues). -/ +theorem NotQueued.sts {σ : State Unit} {t : ObjRef} (h : NotQueued σ t) (p : ObjRef) (o : KernelObject) (u : ObjRef) : + NotQueued (stsAct (updK σ p o) u) t := by + unfold NotQueued + rw [stsAct_readyQueues, stsAct_releaseQueue] + exact h + +theorem stsAct_idleThread (σ : State Z) (t : ObjRef) : (stsAct σ t).idleThread = σ.idleThread := by + unfold stsAct; split_ifs <;> rfl + +theorem stsAct_sa (σ : State Z) (t : ObjRef) : + (stsAct σ t).schedulerAction = σ.schedulerAction ∨ (stsAct σ t).schedulerAction = .chooseNewThread := by + unfold stsAct; split_ifs + · exact Or.inr rfl + · exact Or.inl rfl + +theorem KView.stsAct (σ : State Z) (t : ObjRef) : KView σ (stsAct σ t) := by + refine ⟨fun p x h => ?_, fun p x h => ⟨x, ?_, rfl, rfl, rfl⟩⟩ <;> rw [stsAct_kheap] <;> exact h + +theorem setThreadState_eq {σ : State Z} {p : ObjRef} {x : Tcb} (ts : ThreadState) + (hk : σ.kheap p = some (.tcb x)) : + setThreadState (Z := Z) p ts σ = ({((), stsAct (updK σ p (.tcb { x with tcbState := ts })) p)}, False) := by + unfold setThreadState + rw [nd_bind_one (threadSet_eq _ hk)] + exact setThreadStateAct_eq _ _ + +/-- `removeAll` through an injective naming (stated for any `f`, so the kernel never unfolds `specPtr`). -/ +theorem removeAll_map_inj {f : U64 → ObjRef} (hf : ∀ a b, f a = f b → a = b) {xs : List U64} (h : xs.Nodup) + (d : U64) : removeAll (f d) (xs.map f) = (xs.erase d).map f := by + unfold removeAll + rw [h.erase_eq_filter, List.filter_map] + congr 1 + apply List.filter_congr + intro y _ + simp only [Function.comp, ne_eq, decide_not] + by_cases e : y = d + · subst e; simp + · have hne : f d ≠ f y := fun h' => e (hf _ _ h').symm + simp [e, hne] + +theorem removeAll_map {xs : List U64} (h : xs.Nodup) (d : U64) : + removeAll (specPtr d) (xs.map specPtr) = (xs.erase d).map specPtr := + removeAll_map_inj (fun _ _ e => specPtr_inj e) h d + +/-- `tcb_ntfn_dequeue` of a member. -/ +theorem tcbNtfnDequeue_eq {σ : State Z} {p t : ObjRef} {x : Notification} + (hk : σ.kheap p = some (.notification x)) (ht : t ∈ ntfnQueueOf x) : + tcbNtfnDequeue (Z := Z) t p σ = ({((), updK σ p (.notification (ntfnObjUpdate (fun _ => + match removeAll t (ntfnQueueOf x) with + | [] => Ntfn.idleNtfn + | _ => Ntfn.waitingNtfn (removeAll t (ntfnQueueOf x))) x)))}, False) := by + unfold tcbNtfnDequeue + rw [nd_bind_one (getNotification_eq hk), nd_pure_bind', nd_bind_one (assert_true ht σ), nd_pure_bind', + nd_pure_bind'] + exact setNotification_eq _ hk + +/-! `updK` forms of the step / no-block facts: at a state `σ` that is itself an `updK` / `stsAct` term, these keep +the kernel's defeq check syntactic (the record-update forms make it unfold `stsAct`'s condition, which names +`specPtr`, and recurse). -/ + +theorem NtfnStep.tcbK {σ : State Unit} {p : ObjRef} {x : Tcb} (hk : σ.kheap p = some (.tcb x)) (y : Tcb) + (hy : y = { x with tcbState := y.tcbState, tcbArch := y.tcbArch }) : NtfnStep σ (updK σ p (.tcb y)) := by + have h := NtfnStep.tcb hk y.tcbState y.tcbArch + rw [← hy] at h + exact h + +theorem NtfnStep.ntfnK {σ : State Unit} {p : ObjRef} {x y : Notification} (hk : σ.kheap p = some (.notification x)) + (h1 : y.ntfnBoundTcb = x.ntfnBoundTcb) (h2 : y.ntfnSc = x.ntfnSc) : NtfnStep σ (updK σ p (.notification y)) := + NtfnStep.ntfn hk h1 h2 + +theorem tsCode_six' {ts : ThreadState} (h : tsCode ts = 6) : ∃ m, ts = .blockedOnNotification m := by + rcases ts with _ | _ | _ | _ | _ | _ | _ | _ | ⟨⟨⟩⟩ <;> simp [tsCode] at h ⊢ + +end Spec + + +end + +end Fl4ProofsA64.P3.Notif + +/-! ## The endpoint side -/ + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif + +noncomputable section + +section Defs +variable {S : Type} (st : machine.Store S) + +/-- FL4's restart after the state word (endpoint.rs `restart_thread_if_no_fault`, no fault): the sporadic unblock, +then the switch. -/ +def rustRestartTail (s : S) (t : obj.Tcb) : Result S := do + let s2 ← endpoint.unblock_sporadic st s t + sched.possible_switch_to st s2 t + +end Defs + +section SpecDefs +open Fl4SpecA64 Fl4Spec + +/-- `NoBlockE` of a step that keeps every TCB's state and fault. -/ +theorem NoBlockE.of {σ σ' : State Unit} + (h : ∀ p x y, σ.kheap p = some (.tcb x) → σ'.kheap p = some (.tcb y) → + y.tcbState = x.tcbState ∧ y.tcbFault = x.tcbFault) : NoBlockE σ σ' := fun p x y h1 h2 => + Or.inl ⟨(h p x y h1 h2).1, fun hc => hc.congr (h p x y h1 h2).1.symm fun _ => (h p x y h1 h2).2.symm⟩ + +/-- `NoBlockE` of a step that changes the TCB at `p` only (among TCBs), by one of the three branches. -/ +theorem NoBlockE.only {σ σ' : State Unit} {p : ObjRef} {x y : Tcb} (hk : σ.kheap p = some (.tcb x)) + (hk' : σ'.kheap p = some (.tcb y)) + (ho : ∀ q, q ≠ p → ∀ x' y', σ.kheap q = some (.tcb x') → σ'.kheap q = some (.tcb y') → y' = x') + (h : (y.tcbState = x.tcbState ∧ (inClassR y.tcbState y.tcbFault → inClassR x.tcbState x.tcbFault)) ∨ + (runnable y.tcbState = true ∧ x.tcbState ≠ .idleThreadState ∧ + (inClassR x.tcbState x.tcbFault ∨ RelIfBound σ p)) ∨ + (y.tcbState = .inactive ∧ runnable x.tcbState = false ∧ x.tcbState ≠ .idleThreadState)) : + NoBlockE σ σ' := by + intro q x' y' h1 h2 + by_cases e : q = p + · subst e + rw [hk] at h1; rw [hk'] at h2 + injection h1 with h1; injection h1 with h1 + injection h2 with h2; injection h2 with h2 + subst h1; subst h2; exact h + · have := ho q e x' y' h1 h2 + subst this + exact Or.inl ⟨rfl, id⟩ + +/-- `NoBlockE` of writing the TCB at `p`. -/ +theorem NoBlockE.updT {σ : State Unit} {p : ObjRef} {x : Tcb} (hk : σ.kheap p = some (.tcb x)) (y : Tcb) + (h : (y.tcbState = x.tcbState ∧ (inClassR y.tcbState y.tcbFault → inClassR x.tcbState x.tcbFault)) ∨ + (runnable y.tcbState = true ∧ x.tcbState ≠ .idleThreadState ∧ + (inClassR x.tcbState x.tcbFault ∨ RelIfBound σ p)) ∨ + (y.tcbState = .inactive ∧ runnable x.tcbState = false ∧ x.tcbState ≠ .idleThreadState)) : + NoBlockE σ (updK σ p (.tcb y)) := by + refine NoBlockE.only hk (by rw [updK_kheap, Function.update_self]) (fun q hq x' y' h1 h2 => ?_) h + rw [updK_kheap, Function.update_of_ne hq, h1] at h2 + injection h2 with h2; injection h2 with h2 + exact h2.symm + +/-- `NoBlockE` of writing a non-TCB object. -/ +theorem NoBlockE.updO (σ : State Unit) (p : ObjRef) {o' : KernelObject} (ho' : ∀ x, o' ≠ .tcb x) : + NoBlockE σ (updK σ p o') := by + refine NoBlockE.of fun q x y h1 h2 => ?_ + by_cases e : q = p + · subst e; rw [updK_kheap, Function.update_self] at h2 + injection h2 with h2; exact absurd h2 (ho' y) + · rw [updK_kheap, Function.update_of_ne e, h1] at h2 + injection h2 with h2; injection h2 with h2 + subst h2; exact ⟨rfl, rfl⟩ + +/-- (gen 6) `NoBlockE` of writing a notification. -/ +theorem NoBlockE.ntfnK (σ : State Unit) (p : ObjRef) (y : Notification) : NoBlockE σ (updK σ p (.notification y)) := + NoBlockE.updO σ p fun _ h => by cases h + +/-- (gen 6) `NoBlockE` of writing a TCB's user context only (state and fault kept). -/ +theorem NoBlockE.tcbK {σ : State Unit} {p : ObjRef} {x : Tcb} (hk : σ.kheap p = some (.tcb x)) (y : Tcb) + (hy : y = { x with tcbState := y.tcbState, tcbArch := y.tcbArch }) (h : y.tcbState = x.tcbState) : + NoBlockE σ (updK σ p (.tcb y)) := + NoBlockE.updT hk y (Or.inl ⟨h, fun hc => hc.congr h.symm fun _ => by rw [hy]⟩) + +/-- (gen 6) `NoBlockE` of `receive_signal`'s prefix (the badge register, then the notification). -/ +theorem NoBlockE.recvPrefix {σ : State Unit} {t n : ObjRef} {xt : Tcb} (hkt : σ.kheap t = some (.tcb xt)) + (hnt : n ≠ t) (r : Register) (v : Word) (y : Notification) : + NoBlockE σ (updK (updK σ t (.tcb (setReg xt r v))) n (.notification y)) := by + refine NoBlockE.of fun q x z h1 h2 => ?_ + simp only [updK_kheap, Function.update_apply] at h2 + by_cases e : q = n + · simp [e] at h2 + · by_cases e2 : q = t + · subst e2; rw [hkt] at h1; cases h1; simp only [e, ↓reduceIte] at h2; cases h2; exact ⟨rfl, rfl⟩ + · simp only [e, e2, ↓reduceIte] at h2; rw [h1] at h2; cases h2; exact ⟨rfl, rfl⟩ + +/-- A state of `inClass` that is not runnable is not idle. -/ +theorem _root_.Fl4ProofsA64.P3.Notif.inClass.ne_idle {ts : ThreadState} (h : inClass ts) (hr : runnable ts = false) : ts ≠ .idleThreadState := by + rintro rfl + rcases h with h | ⟨n, h⟩ | ⟨e, d, h⟩ | ⟨e, r, d, h⟩ + · simp [runnable] at h + all_goals cases h + +end SpecDefs + +section Hyps +variable {S : Type} (st : machine.Store S) [L : LawfulStore st] + +/-- `c` (FL4, from `s`) refines `m` (spec, from `σ`) on the endpoint side: the spec does not fail, FL4's outcome is +matched by one of the spec's, FL4 keeps `EpStateRel`'s footprint, the spec keeps its `KView`, and `SR` holds. -/ +def TailRefE (T E R : Set U64) (Bf : Set (U64 × Fl4SpecA64.Word)) (SR : S → Fl4SpecA64.State Unit → Prop) (s : S) + (σ : Fl4SpecA64.State Unit) (c : Result S) (m : Fl4SpecA64.SMonad Unit Unit) : Prop := + c ⦃ s' => ¬ (m σ).2 ∧ ∃ σ', ((), σ') ∈ (m σ).1 ∧ FootEq T E R Bf (L.mem s) (L.mem s') ∧ KView σ σ' ∧ SR s' σ' ⦄ + +/-- **The scheduler interface of the endpoint paths** (subsystem-composition interface, see the module docstring; +x86 `EpHyps` plus the restart / reschedule steps). -/ +structure SchedOpsEA (T E R : Set U64) (Bf : Set (U64 × Fl4SpecA64.Word)) (SR : S → Fl4SpecA64.State Unit → Prop) : + Prop where + /-- `SR` ignores the endpoint code's own writes and spec steps that block no thread (gen 5: `NoBlockE`). -/ + stable : ∀ s s' σ σ', SR s σ → WritesIn st s s' (EWrite T E R Bf) → EpStep σ σ' → NoBlockE σ σ' → SR s' σ' + /-- `sched::schedule_tcb t` refines `set_thread_state_act t`. -/ + scheduleTcb : ∀ s σ (t : U64), EpStateRel T E R Bf (L.mem s) σ → SR s σ → t ∈ T → + sched.schedule_tcb st s t ⦃ s' => FootEq T E R Bf (L.mem s) (L.mem s') ∧ SR s' (stsAct σ (specPtr t)) ⦄ + /-- The sporadic unblock and the switch of a restarted TCB in no endpoint queue. (gen 12, ruling Q1) Also of a + TCB set running (`send_ipc` / `receive_ipc`'s woken partner): neither tail reads the thread state. -/ + restartTail : ∀ s σ (t : U64) (x : Fl4SpecA64.Tcb), EpStateRel T E R Bf (L.mem s) σ → SR s σ → t ∈ T → + σ.kheap (specPtr t) = some (.tcb x) → (x.tcbState = .running ∨ x.tcbState = .restart) → + (∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) → + L.mem s (affA t) = L.cpu s → NotQueued σ (specPtr t) → + TailRefE st T E R Bf SR s σ (rustRestartTail st s t) (specRestartTail (specPtr t)) + /-- `sched::reschedule_required` refines `reschedule_required`. -/ + reschedule : ∀ s σ, EpStateRel T E R Bf (L.mem s) σ → SR s σ → + TailRefE st T E R Bf SR s σ (sched.reschedule_required st s) Fl4SpecA64.rescheduleRequired + /-- (gen 6, ruling (a)) A thread whose state and fault are in `inClassR` has a released SC if it is bound to an + active one outside the release queue (l4v `released_ipc_queues` / `valid_sched`; x86 `ScInv.Inv.rd`). -/ + rd : ∀ s σ u (x : Fl4SpecA64.Tcb), SR s σ → σ.kheap u = some (.tcb x) → inClassR x.tcbState x.tcbFault → + RelIfBound σ u + /-- (gen 7) `SR`-related states agree on the current core and the affinity words (`Notif.CoreKeep`). -/ + coreKeep : ∀ s s' σ σ', SR s σ → SR s' σ' → CoreKeep st T s s' + /-- (gen 9, Architect 00:22Z) A thread that is not runnable is in no queue (l4v `valid_ready_qs`, + `valid_release_q`; `SchedHypsA.notQueued`'s twin; MCS: `SchedSRA.notQueued`). -/ + notQueued : ∀ s σ (t : U64) (x : Fl4SpecA64.Tcb), SR s σ → σ.kheap (specPtr t) = some (.tcb x) → + Fl4SpecA64.runnable x.tcbState = false → NotQueued σ (specPtr t) + /-- (gen 12, ruling Q2) Blocking a thread in no queue, not the switch target and not idle keeps `SR` (the endpoint + twin of `Notif.SchedHypsA.block`, same shape: every other TCB keeps its state and reflects `inClassR` back; MCS + `SchedSRA.block`). Used for the sender of a call / faulted send set `Inactive`. -/ + block : ∀ s s' σ σ' (t : U64), SR s σ → NotQueued σ (specPtr t) → RelIfBound σ (specPtr t) → + (∀ u, σ.schedulerAction = .switchThread u → u ≠ (specPtr t)) → (specPtr t) ≠ σ.idleThread → + WritesIn st s s' (EWrite T E R Bf) → EpStep σ σ' → + (∀ p x y, σ.kheap p = some (.tcb x) → σ'.kheap p = some (.tcb y) → p ≠ (specPtr t) → + y.tcbState = x.tcbState ∧ (inClassR y.tcbState y.tcbFault → inClassR x.tcbState x.tcbFault)) → + SR s' σ' + /-- (gen 13, ruling Q1) The sporadic unblock of a TCB that is not runnable and in no scheduler queue (`receive_ipc`'s + dequeued sender, before its state changes): FL4's `unblock_sporadic t` against l4v's + `if_sporadic_cur_sc_assert_refill_unblock_check` of its SC, one outcome (x86 `EpTailHyps.sporadicAssert`; MCS: + `unblock_sporadic_SRA`, `UnbFrA`). -/ + unblockSporadic : ∀ s σ (t : U64) (x : Fl4SpecA64.Tcb), EpStateRel T E R Bf (L.mem s) σ → SR s σ → t ∈ T → + σ.kheap (specPtr t) = some (.tcb x) → Fl4SpecA64.runnable x.tcbState = false → NotQueued σ (specPtr t) → + endpoint.unblock_sporadic st s t ⦃ s' => ∃ σ', + Fl4SpecA64.ifSporadicCurScAssertRefillUnblockCheck (Z := Unit) x.tcbSchedContext σ = ({((), σ')}, False) ∧ + FootEq T E R Bf (L.mem s) (L.mem s') ∧ KView σ σ' ∧ SR s' σ' ⦄ + /-- (gen 13, ruling Q1) `possible_switch_to` of a TCB set `Running`, in no endpoint or scheduler queue, on the + current core (`receive_ipc`'s non-call sender; x86 `EpTailHyps.switchTo`; MCS: `possible_switch_to_refinesA`, + `MFrA`). -/ + switchTo : ∀ s σ (t : U64) (x : Fl4SpecA64.Tcb), EpStateRel T E R Bf (L.mem s) σ → SR s σ → t ∈ T → + σ.kheap (specPtr t) = some (.tcb x) → x.tcbState = .running → + (∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) → + L.mem s (affA t) = L.cpu s → NotQueued σ (specPtr t) → + TailRefE st T E R Bf SR s σ (sched.possible_switch_to st s t) (Fl4SpecA64.possibleSwitchTo (specPtr t)) + +/-- (Lift 2b) **The part of `SchedOpsEA` the message transfer and the receiver's reply unlink use**: `stable`, +`scheduleTcb` and `rd` at threads blocked on receive. It also holds for a relation excepting a running thread from +`rd` (the timeout fault sender of an exhausted SC, `SchedSRB`). -/ +structure SchedOpsE0A (T E R : Set U64) (Bf : Set (U64 × Fl4SpecA64.Word)) (SR : S → Fl4SpecA64.State Unit → Prop) : + Prop where + stable : ∀ s s' σ σ', SR s σ → WritesIn st s s' (EWrite T E R Bf) → EpStep σ σ' → NoBlockE σ σ' → SR s' σ' + scheduleTcb : ∀ s σ (t : U64), EpStateRel T E R Bf (L.mem s) σ → SR s σ → t ∈ T → + sched.schedule_tcb st s t ⦃ s' => FootEq T E R Bf (L.mem s) (L.mem s') ∧ SR s' (stsAct σ (specPtr t)) ⦄ + rdRecv : ∀ s σ u (x : Fl4SpecA64.Tcb) e r d, SR s σ → σ.kheap u = some (.tcb x) → + x.tcbState = .blockedOnReceive e r d → RelIfBound σ u + +variable {st} in +theorem SchedOpsEA.to0 {T E R : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {SR : S → Fl4SpecA64.State Unit → Prop} + (h : SchedOpsEA st T E R Bf SR) : SchedOpsE0A st T E R Bf SR := + ⟨h.stable, h.scheduleTcb, fun s σ u x e r d hS hk hst => + h.rd s σ u x hS hk (Or.inr (Or.inr (Or.inr ⟨e, r, d, hst⟩)))⟩ + +end Hyps + +end + +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/SendSignalDetA.lean b/proofs/Fl4ProofsA64/P3/SendSignalDetA.lean new file mode 100644 index 000000000..e609cd7f4 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/SendSignalDetA.lean @@ -0,0 +1,149 @@ +/- +P3 (aarch64, sigwake2): **determinism of `sendSignal`** (at most one outcome at every state), on top of `P3/SpecDetA`: +`asUser` of a register write, `cancelIpc`, the donate/resume path (`maybeDonateSc`, `schedContextDonate`, +`schedContextResume`, `postpone`, `tcbReleaseEnqueue`), `updateWaitingNtfn` and `sendSignal`. +`sendSignal_singletonA`: with no failure, one outcome is the whole outcome set. No named premise. +-/ +import Fl4ProofsA64.P3.SpecDetA + +namespace Fl4ProofsA64.P3.Det +open Fl4Spec Fl4SpecA64 + +section +variable {σ α β : Type} + +theorem det_mapM {f : α → NonDet σ β} (h : ∀ a, Det (f a)) : ∀ l, Det (NonDet.mapM f l) + | [] => det_pure _ + | a :: l => by + unfold NonDet.mapM + exact det_bind' (h a) fun _ => det_bind' (det_mapM h l) fun _ => det_pure _ + +/-- `selectF` of a singleton outcome set. -/ +theorem det_selectF_single (a : α) (p : Prop) : Det (NonDet.selectF ({a}, p) : NonDet σ α) := by + constructor; intro s x y hx hy + have hx' : x.1 = a := hx.1 + have hy' : y.1 = a := hy.1 + have e1 : x.2 = s := hx.2 + have e2 : y.2 = s := hy.2 + exact Prod.ext (hx'.trans hy'.symm) (e1.trans e2.symm) + +end + +section Spec + +theorem det_getScObjRef (f : SchedContext → Option ObjRef) (p : ObjRef) : + Det (getScObjRef (Z := Unit) f p) := by + unfold getScObjRef; det + all_goals exact det_getSchedContext _ + +theorem det_setScObjRef {α : Type} (f : (α → α) → SchedContext → SchedContext) (p : ObjRef) (new : α) : + Det (setScObjRef (Z := Unit) f p new) := by + unfold setScObjRef; exact det_updateSchedContext _ _ + +theorem det_getTcbQueue (d : Domain) (p : Priority) : Det (getTcbQueue (Z := Unit) d p) := by + unfold getTcbQueue; det + +theorem det_setTcbQueue (d : Domain) (p : Priority) (q : ReadyQueue) : Det (setTcbQueue (Z := Unit) d p q) := by + unfold setTcbQueue; det + +theorem det_tcbSchedAction (a : ObjRef → List ObjRef → List ObjRef) (t : ObjRef) : + Det (tcbSchedAction (Z := Unit) a t) := by + unfold tcbSchedAction; det + all_goals first | exact det_threadGet _ _ | exact det_getTcbQueue _ _ | exact det_setTcbQueue _ _ _ | exact det_mapM (fun _ => det_getsThe _) _ + +theorem det_setReleaseQueue (q : List ObjRef) : Det (setReleaseQueue (Z := Unit) q) := by + unfold setReleaseQueue; det + +theorem det_tcbReleaseRemove (t : ObjRef) : Det (tcbReleaseRemove (Z := Unit) t) := by + unfold tcbReleaseRemove; det + all_goals exact det_setReleaseQueue _ + +theorem det_testReschedule (t : ObjRef) : Det (testReschedule (Z := Unit) t) := by + unfold testReschedule; det + all_goals exact det_rescheduleRequired + +theorem det_schedContextDonate (sc t : ObjRef) : Det (schedContextDonate (Z := Unit) sc t) := by + unfold schedContextDonate; det + all_goals first | exact det_getScObjRef _ _ | exact det_tcbSchedAction _ _ | exact det_tcbReleaseRemove _ | exact det_threadSet _ _ | exact det_testReschedule _ | exact det_setScObjRef _ _ _ | exact det_mapM (fun _ => det_getsThe _) _ + +theorem det_readyTimesAppend (t : ObjRef) (qs : List ObjRef) : Det (readyTimesAppend (Z := Unit) t qs) := by + unfold readyTimesAppend orderedInsert; det + all_goals first | exact det_mapM (fun _ => det_getsThe _) _ + +theorem det_tcbReleaseEnqueue (t : ObjRef) : Det (tcbReleaseEnqueue (Z := Unit) t) := by + unfold tcbReleaseEnqueue; det + all_goals first | exact det_readyTimesAppend _ _ | exact det_setReleaseQueue _ | exact det_mapM (fun _ => det_getsThe _) _ + +theorem det_postpone (sc : ObjRef) : Det (postpone (Z := Unit) sc) := by + unfold postpone; det + all_goals first | exact det_getScObjRef _ _ | exact det_tcbSchedAction _ _ | exact det_tcbReleaseEnqueue _ | exact det_mapM (fun _ => det_getsThe _) _ + +theorem det_schedContextResume (sc : ObjRef) : Det (schedContextResume (Z := Unit) sc) := by + unfold schedContextResume getScRefillReady getScRefillSufficient; det + all_goals first | exact det_getSchedContext _ | exact det_threadGet _ _ | exact det_getTcbQueue _ _ | exact det_postpone _ | exact det_mapM (fun _ => det_getsThe _) _ + +theorem det_maybeDonateSc (t n : ObjRef) : Det (maybeDonateSc (Z := Unit) t n) := by + unfold maybeDonateSc getNtfnObjRef getSkObjRef; det + all_goals first | exact det_getTcbObjRef _ _ | exact det_getSimpleKo _ _ | exact det_getScObjRef _ _ | exact det_schedContextDonate _ _ | exact det_schedContextResume _ | exact det_mapM (fun _ => det_getsThe _) _ + +theorem det_getThreadState (t : ObjRef) : Det (getThreadState (Z := Unit) t) := by + unfold getThreadState; exact det_threadGet _ _ + +theorem det_getTcbObjRef (f : Tcb → Option ObjRef) (t : ObjRef) : Det (getTcbObjRef (Z := Unit) f t) := by + unfold getTcbObjRef; det + all_goals exact det_threadGet _ _ + +theorem det_getBlockingObject (st : ThreadState) : Det (getBlockingObject (Z := Unit) st) := by + unfold getBlockingObject; exact det_assertOpt _ + +theorem det_getNotification (p : ObjRef) : Det (getNotification (Z := Unit) p) := by + unfold getNotification; first | exact det_getSimpleKo _ _ | (det; all_goals exact det_getSimpleKo _ _) + +theorem det_setNotification (p : ObjRef) (n : Notification) : Det (setNotification (Z := Unit) p n) := by + unfold setNotification; first | exact det_setSimpleKo _ _ _ | (det; all_goals exact det_setSimpleKo _ _ _) + +theorem det_asUser_setRegister (t : ObjRef) (r : Register) (v : Word) : + Det (asUser (Z := Unit) t (setRegister r v)) := by + unfold asUser; det + all_goals first | exact det_getsThe _ | exact det_setObject _ _ | (unfold setRegister NonDet.modify; exact det_selectF_single _ _) + +theorem det_selectF_setRegister {σ : Type} (r : Register) (v : Word) (uc : UserContext) : + Det (NonDet.selectF (σ := σ) (setRegister r v uc)) := by + unfold setRegister NonDet.modify; exact det_selectF_single _ _ + +theorem det_blockedCancelIpc (st : ThreadState) (t : ObjRef) (ro : Option ObjRef) : + Det (blockedCancelIpc (Z := Unit) st t ro) := by + unfold blockedCancelIpc; det + all_goals first | exact det_getBlockingObject _ | exact det_tcbEpDequeue _ _ | exact det_replyUnlinkTcb _ _ | exact det_setThreadState _ _ + +theorem det_cancelSignal (t n : ObjRef) : Det (cancelSignal (Z := Unit) t n) := by + unfold cancelSignal; det + all_goals first | exact det_tcbNtfnDequeue _ _ | exact det_setThreadState _ _ + +theorem det_updateWaitingNtfn (n : ObjRef) (q : List ObjRef) (b : Badge) : + Det (updateWaitingNtfn (Z := Unit) n q b) := by + unfold updateWaitingNtfn ifSporadicCurScTestRefillUnblockCheck; det + all_goals first | exact det_tcbNtfnDequeue _ _ | exact det_setThreadState _ _ | exact det_asUser_setRegister _ _ _ | exact det_maybeDonateSc _ _ | exact det_possibleSwitchTo _ | exact det_getTcbObjRef _ _ | exact det_ifCondRefillUnblockCheck _ _ _ | exact det_selectF_setRegister _ _ _ | exact det_mapM (fun _ => det_getsThe _) _ + +theorem det_replyRemoveTcb (t r : ObjRef) : Det (replyRemoveTcb (Z := Unit) t r) := by + unfold replyRemoveTcb; det + all_goals first | exact det_pure _ | exact det_getsThe _ | exact det_threadGet _ _ | exact det_getThreadState _ | exact det_threadSet _ _ | exact det_setThreadState _ _ | exact det_replyUnlinkTcb _ _ | exact det_blockedCancelIpc _ _ _ | exact det_cancelSignal _ _ | exact det_getSchedContext _ | exact det_updateSchedContext _ _ | exact det_getSimpleKo _ _ | exact det_setSimpleKo _ _ _ | exact det_updateSkObjRef _ _ _ _ | exact det_assert _ | exact det_liftM (det_getSchedContext _) | exact det_getNotification _ | exact det_setNotification _ _ | exact det_asUser_setRegister _ _ _ | exact det_maybeDonateSc _ _ | exact det_possibleSwitchTo _ | exact det_getTcbObjRef _ _ | exact det_ifCondRefillUnblockCheck _ _ _ | exact det_updateWaitingNtfn _ _ _ | exact det_selectF_setRegister _ _ _ | exact det_mapM (fun _ => det_getsThe _) _ + +theorem det_cancelIpc (t : ObjRef) : Det (cancelIpc (Z := Unit) t) := by + unfold cancelIpc; det + all_goals first | exact det_pure _ | exact det_getsThe _ | exact det_threadGet _ _ | exact det_getThreadState _ | exact det_threadSet _ _ | exact det_setThreadState _ _ | exact det_replyUnlinkTcb _ _ | exact det_blockedCancelIpc _ _ _ | exact det_cancelSignal _ _ | exact det_getSchedContext _ | exact det_updateSchedContext _ _ | exact det_getSimpleKo _ _ | exact det_setSimpleKo _ _ _ | exact det_updateSkObjRef _ _ _ _ | exact det_assert _ | exact det_liftM (det_getSchedContext _) | exact det_getNotification _ | exact det_setNotification _ _ | exact det_asUser_setRegister _ _ _ | exact det_maybeDonateSc _ _ | exact det_possibleSwitchTo _ | exact det_getTcbObjRef _ _ | exact det_ifCondRefillUnblockCheck _ _ _ | exact det_updateWaitingNtfn _ _ _ | exact det_selectF_setRegister _ _ _ | exact det_mapM (fun _ => det_getsThe _) _ | exact det_replyRemoveTcb _ _ + +theorem det_sendSignal (n : ObjRef) (b : Badge) : Det (sendSignal (Z := Unit) n b) := by + unfold sendSignal ifSporadicCurScTestRefillUnblockCheck; det + all_goals first | exact det_pure _ | exact det_getsThe _ | exact det_threadGet _ _ | exact det_getThreadState _ | exact det_threadSet _ _ | exact det_setThreadState _ _ | exact det_replyUnlinkTcb _ _ | exact det_blockedCancelIpc _ _ _ | exact det_cancelSignal _ _ | exact det_getSchedContext _ | exact det_updateSchedContext _ _ | exact det_getSimpleKo _ _ | exact det_setSimpleKo _ _ _ | exact det_updateSkObjRef _ _ _ _ | exact det_assert _ | exact det_liftM (det_getSchedContext _) | exact det_getNotification _ | exact det_setNotification _ _ | exact det_asUser_setRegister _ _ _ | exact det_maybeDonateSc _ _ | exact det_possibleSwitchTo _ | exact det_getTcbObjRef _ _ | exact det_ifCondRefillUnblockCheck _ _ _ | exact det_updateWaitingNtfn _ _ _ | exact det_selectF_setRegister _ _ _ | exact det_mapM (fun _ => det_getsThe _) _ | exact det_replyRemoveTcb _ _ | exact det_cancelIpc _ + +/-- `sendSignal` does not fail and has an outcome: that outcome is the whole outcome set. -/ +theorem sendSignal_singletonA {n : ObjRef} {b : Badge} {s s' : State Unit} + (hf : ¬ (sendSignal (Z := Unit) n b s).2) (hm : ((), s') ∈ (sendSignal (Z := Unit) n b s).1) : + sendSignal (Z := Unit) n b s = ({((), s')}, False) := + Prod.ext (Set.ext fun y => ⟨fun hy => Set.mem_singleton_iff.2 ((det_sendSignal n b).det s y ((), s') hy hm), + fun hy => (Set.mem_singleton_iff.1 hy) ▸ hm⟩) (eq_false hf) + +end Spec + +end Fl4ProofsA64.P3.Det diff --git a/proofs/Fl4ProofsA64/P3/SetCapJA.lean b/proofs/Fl4ProofsA64/P3/SetCapJA.lean new file mode 100644 index 000000000..f91df605d --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/SetCapJA.lean @@ -0,0 +1,102 @@ +/- +P3 (aarch64, A64-P3 S3 gen 25, ruling 22:14Z Q3): **the post-unmap cap write as a `JAtZAX → JAtZAX` step** — FL4's +`set_cap` of the unmapped cap into the invoked slot (`vspace.rs` `decode_frame` / `decode_page_table` after the +unmap: `Frame … false false 0 0`, `PageTable ptr false 0 0`) against l4v's `setCap`, with `JAtZAX` before and after +and no `JEntA` / `SysInvA` at the written state: the slot-word step needs only A64 P1's slot-set facts (`OSOK`, the +relation's slots in `OS`), the outside-the-slots footprint off the slot words and aligned, and the call-site MDB tree. +The x86 counterpart is the cap write of `P3/VsFrameMapRel` / `EptCore.frameUnmapF`'s tail. + +* **`JAtZAX.of_setcapA`**: from `JAtZAX` at `(s, σ)`, the slot facts and the cap-tree facts (the tree at `OS`, the + null non-relation slots, the old cap non-null, the new one non-null, `UA`-alike to it, encodable, `CapRelA`-related to + the spec cap and `CapObjOkA`): `set_cap` succeeds, `setCap` has the matching outcome (a `SpecSlotA` step), FL4 writes + only cap words (`WritesIn … CapW`), and `JAtZAX` and the tree hold after. The same steps as gen 22's + `restart_setcapJA` after its restart, with `JAtZAX` and those facts in place of `JEntA`. +* `footNOff_ksA` / `footNAl_ksA`: the slot facts carried across steps that keep `ksb` / `cpu` (`P3/VsTabJA`'s + `FootNA.of_ksA`, gen 25: the outside-the-slots footprint reads the state only through them). + +No named premise; no decode-site hypothesis. No existing statement changed. +-/ +import Fl4ProofsA64.P3.VsTabJA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} + +set_option maxHeartbeats 2000000 in +/-- **`JAtZAX` across an in-place cap write** (module docstring). -/ +theorem JAtZAX.of_setcapA (J : JAtZAX (L := L) AR Z s a σ m) {T Ω Sg OS : Set U64} (hO : OSOK T Ω Sg OS) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hoff : ∀ b, FootNA (L := L) Z s b → ¬ W7.CapW T {b | InOS OS b} ∅ b) + (hal : ∀ b, FootNA (L := L) Z s b → Aligned b) + (cte : Fl4SpecA64.CslotPtr) (hab : AbsSlotA Z.th cte) {c : capability.Cap} {sc : Fl4SpecA64.Cap} + (hEnc : CapRt.EncWf c) (hca : CapRelA c sc) + (hT : TreeA st OS Z.G.chains Z.G.par s) (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) + (hcn : c ≠ .Null) (hc0 : capA st s (cslotAddrA cte) ≠ .Null) (hua : UA c (capA st s (cslotAddrA cte))) + (hok : CapObjOkA Z c) : + ∃ s2 σ2, obj.Slot.set_cap st (cslotAddrA cte) s c = ok s2 ∧ + ((), σ2) ∈ (Fl4SpecA64.setCap (Z := Unit) sc cte σ).1 ∧ SpecSlotA σ σ2 ∧ + WritesIn st s s2 (W7.CapW T {b | InOS OS b} ∅) ∧ + JAtZAX (L := L) AR Z s2 a σ2 m ∧ TreeA st OS Z.G.chains Z.G.par s2 := by + have hP1 := J.1.place + have hcs : IsSlotA Z.th (cslotAddrA cte) := hab.isSlot hP1 + have hos := hsub _ hcs + obtain ⟨w0, w1, hen, hd⟩ := CapRt.decode_encode _ hEnc + have a0x := hO.al0 hos + have a8x := hO.al hos (k := 8#u64) (by simp) (by simp) + obtain ⟨v8, -, -⟩ := vals hO hos + have hw2 : WritesIn st s (L.wr (L.wr s (cslotAddrA cte) w0) (wa (cslotAddrA cte) 8#u64) w1) + (W7.CapW T {b | InOS OS b} ∅) := by + have := W7.obj.Slot.set_cap.capw (st := st) (s := s) (c := c) (osw hO hos) + rw [set_cap_eq hen, WP.spec_ok] at this + exact this + generalize hs2 : L.wr (L.wr s (cslotAddrA cte) w0) (wa (cslotAddrA cte) 8#u64) w1 = s2 at hw2 + have hcx : capAtA (L.mem s2) (cslotAddrA cte) = c := by + have hd' := hd + unfold capAtA + rw [← hs2, L.mem_wr_ne a8x a0x (by omega), L.mem_wr_same a0x, L.mem_wr_same a8x] + rw [CapRt.decode_eq] at hd' + exact okinj hd' + have hcapo : ∀ y, IsSlotA Z.th y → y ≠ cslotAddrA cte → capAtA (L.mem s2) y = capAtA (L.mem s) y := by + intro y hy hne + rw [capAtA_eq, capAtA_eq, ← hs2] + exact capA_wr2_other hO hos (hsub y hy) hne + obtain ⟨o, hσ2, hSR, HS⟩ := StateRelA.setAbsA J.1.st hP1 hab (by rw [hcx]; exact hca) hcapo + have hT2 : TreeA st OS Z.G.chains Z.G.par s2 := by + rw [← hs2]; exact hT.set_cap hO hos hd hcn hc0 hua + have C1 := J.1.cdt + have hC : CdtRelA st Z.th s2 (setObjA σ cte.1 o) Z.G := + CdtRelA.mkA (cdtTree_of hT2 hsub fun y hy hn => by + rw [← hs2, capA_wr2_other hO hos hy (fun e => hn (e ▸ hcs))]; exact hnul y hy hn) + C1.cdt C1.cdt_slot + (fun sl hsl => by rw [← hs2, revA_wr2A hO hos (hsub _ (hsl.isSlot hP1))]; exact C1.orig sl hsl) + C1.par_slot C1.po + refine ⟨s2, _, by rw [← hs2]; exact set_cap_eq hen, hσ2, HS, hw2, + J.of_slotWrites hw2 (fun b hb hf => hoff b hf hb) hal Z.G HS hSR hC fun y hy => ?_, hT2⟩ + by_cases hne : y = cslotAddrA cte + · subst hne; rw [hcx]; exact hok + · rw [hcapo y hy hne]; exact J.capObj hy + +/-- The slot facts carried to a state with the same `ksb` / `cpu`. -/ +theorem footNOff_ksA {T OS : Set U64} {s' : S} (hks : L.ksb s' = L.ksb s) (hcpu : L.cpu s' = L.cpu s) + (h : ∀ b, FootNA (L := L) Z s b → ¬ W7.CapW T {b | InOS OS b} ∅ b) : + ∀ b, FootNA (L := L) Z s' b → ¬ W7.CapW T {b | InOS OS b} ∅ b := + fun b hb => h b (FootNA.of_ksA hks.symm hcpu.symm hb) + +theorem footNAl_ksA {s' : S} (hks : L.ksb s' = L.ksb s) (hcpu : L.cpu s' = L.cpu s) + (h : ∀ b, FootNA (L := L) Z s b → Aligned b) : ∀ b, FootNA (L := L) Z s' b → Aligned b := + fun b hb => h b (FootNA.of_ksA hks.symm hcpu.symm hb) + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/SigFootA.lean b/proofs/Fl4ProofsA64/P3/SigFootA.lean new file mode 100644 index 000000000..be6166d32 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/SigFootA.lean @@ -0,0 +1,435 @@ +/- +P3 (aarch64, delete hooks W3, contract §44, executor 17): **the footprint-and-keep sibling of `cancel_all_signals`' +loop** (`P3/NtfnCancelAllA`'s `sig_tail` / `sig_deq` / `sig_loop` / `cancel_all_signals_rel`, whose statements are +unchanged). `cancel_all_signals_rel` exports only `NtfnStateRel ∧ SR`; the sibling also exports + +* `NtfnSpecKeepA σ σ'` (every object kept, or replaced by the same kind with the MCP / yield fields kept: the + spec dequeue, `set_thread_state`, `Endp.specRestartTail`, `rescheduleRequired`, from `P3/SigKeepA`), +* `NtfnTcbKeepA σ σ'` (`P3/SigTcbKeepA`: endpoints, replies, user memory and the TCB fields `EpStateRel` reads), +* `SSzA σ σ'`, and +* `FpSA`: the exact write set `SigWrA` of the loop (the notification `n`'s 40 bytes and the `Ep` links / state word of + the TCBs) plus the scheduler words `Wr σ s` of `SchedFootNA`. The fault-type word `t + 3376` of `NWrite` is **not** + in it: `ntfn_dequeue` writes only the head / tail words of `n` and the `Ep` links of the queue (`QKeeps.agree`), + `set_thread_state` only the state word. The TCB words are those of a set `Q` holding the +queued threads (a parameter of the sibling), so the other TCBs' `Ep` links are kept. + +No named premise (the interfaces are `SchedOpsA` / `SchedFootNA`, as before); new module. +-/ +import Fl4ProofsA64.P3.NtfnCancelAllA +import Fl4ProofsA64.P3.NtfnWakeFootA +import Fl4ProofsA64.P3.SchedFootNA +import Fl4ProofsA64.P3.SigTcbKeepA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Notif +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Endp + +noncomputable section + +section Fp +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {N T : Set U64} + {Wr : Fl4SpecA64.State Unit → S → U64 → Prop} + +/-- The words the loop writes (without the scheduler words and the fault type). -/ +def SigWrA (T : Set U64) (n b : U64) : Prop := + (n.val ≤ b.val ∧ b.val < n.val + 40) ∨ ∃ t ∈ T, b = stA t ∨ b = nextA .Ep t ∨ b = prevA .Ep t + +variable (st T Wr) in +/-- The footprint shape of the loop's posts. -/ +def FpSA (n : U64) (σ : Fl4SpecA64.State Unit) (s s' : S) : Prop := + WritesIn st s s' (fun b => SigWrA T n b ∨ Wr σ s b) + +theorem FpSA.refl {n : U64} (σ : Fl4SpecA64.State Unit) (s : S) : FpSA st T Wr n σ s s := WritesIn.refl _ _ + +theorem FpSA.of_sw {n : U64} {σ : Fl4SpecA64.State Unit} {s s' : S} (h : WritesIn st s s' (SigWrA T n)) : + FpSA st T Wr n σ s s' := h.mono fun _ hb => Or.inl hb + +theorem FpSA.of_wr {n : U64} {σ : Fl4SpecA64.State Unit} {s s' : S} (h : WritesIn st s s' (Wr σ s)) : + FpSA st T Wr n σ s s' := h.mono fun _ hb => Or.inr hb + +theorem FpSA.trans {n : U64} {σ σ1 : Fl4SpecA64.State Unit} {s s1 s2 : S} + (hb : ∀ {σ σ1 : Fl4SpecA64.State Unit} {s s1 : S} {b : U64}, SSzA σ σ1 → L.ksb s1 = L.ksb s → + Wr σ1 s1 b → Wr σ s b) + (h1 : FpSA st T Wr n σ s s1) (hz : SSzA σ σ1) (h2 : FpSA st T Wr n σ1 s1 s2) : FpSA st T Wr n σ s s2 := + (WritesIn.trans (st := st) h1 h2).mono fun _ h => by + rcases h with (h | h) | (h | h) + · exact Or.inl h + · exact Or.inr h + · exact Or.inl h + · exact Or.inr (hb hz h1.1 h) + +/-- **`ntfn_dequeue` writes only `n`'s head / tail words and the `Ep` links of the queue.** -/ +theorem QKeeps.sigW {Q : Set U64} (hP : NPlaced T N) {n : U64} (hn : n ∈ N) {W : List U64} + (hW : ∀ w ∈ W, w ∈ Q) {s s' : S} (h : QKeeps st N T n W s s') : WritesIn st s s' (SigWrA Q n) := by + refine ⟨h.foot.1, h.foot.2.1, fun b hb hnot => h.agree b hb ?_ ?_ ?_⟩ + · intro w hw + exact ⟨fun e => hnot (Or.inr ⟨w, hW w hw, Or.inr (Or.inl e)⟩), fun e => hnot (Or.inr ⟨w, hW w hw, Or.inr (Or.inr e)⟩)⟩ + · intro e + apply hnot; left + rw [e, epHeadA_eq, nfield_val (hP.ntfn n hn) (by simp)] + simp + · intro e + apply hnot; left + rw [e, epTSA_eq, nfield_val (hP.ntfn n hn) (by simp)] + simp + +end Fp + +section Wk +open Fl4SpecA64 +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] {N T : Set U64} + {SR : S → Fl4SpecA64.State Unit → Prop} {DL : S → U64 → Prop} + {Wr : Fl4SpecA64.State Unit → S → U64 → Prop} + +theorem set_thread_state_sigF (hH : SchedHypsA st N T SR) (hO : SchedOpsA st ar N T SR DL) (hF : SchedFootNA st ar N T SR DL Wr) (hP : NPlaced T N) (n : U64) (s : S) + (σ : Fl4SpecA64.State Unit) (hR : NtfnStateRel N T (L.mem s) σ) (hSR : SR s σ) {t : U64} (ht : t ∈ T) + {x : Fl4SpecA64.Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hq : ∀ n ∈ N, ∀ z, σ.kheap (specPtr n) = some (.notification z) → (specPtr t) ∉ z.ntfnObj.ntfnQueue) + {Q : Set U64} (htQ : t ∈ Q) + (ts : Fl4SpecA64.ThreadState) (hts : ∀ n, ts ≠ .blockedOnNotification n) (code : U64) + (hcode : code.val = tsCode ts) (hNB : Endp.NoBlockE σ (updK σ (specPtr t) (.tcb { x with tcbState := ts }))) : + sched.set_thread_state st s t code ⦃ s' => + NtfnStateRel N T (L.mem s') (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := ts })) (specPtr t)) ∧ + SR s' (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := ts })) (specPtr t)) ∧ + FpSA st Q Wr n σ s s' ⦄ := by + unfold sched.set_thread_state + step with obj.Tcb.set_state.spec as ⟨s1, s1_post⟩ + subst s1_post + have hx := hR.at_tcb ht hk + obtain ⟨hw, hoth⟩ := twr_wr (st := st) hP ht (K := 3328#u64) (Or.inl rfl) s code + rw [← stA_eq] at hw hoth + have hR1 : NtfnStateRel N T (L.mem (L.wr s (stA t) code)) + (updK σ (specPtr t) (.tcb { x with tcbState := ts })) := by + refine hR.upd_tcb ht _ hw.ntfn hw.tcb hw.links (Or.inl hq) ⟨?_, ?_, ?_, ?_⟩ + · rw [rd_wr_eq (foot_st hP ht).1]; exact hcode + · intro n hn; exact absurd hn (hts n) + · show (L.mem _ (badgeRegA t)).bv = badgeOf x + rw [badgeRegA_eq, hoth _ (by simp) (by simp) (by simp), ← badgeRegA_eq]; exact hx.badge + · show (L.mem _ (prioA t)).val = x.tcbPriority.toNat + rw [prioA_eq', hoth _ (by simp) (by simp) (by simp), ← prioA_eq']; exact hx.prio + have hSR1 := hH.stable _ _ _ _ hSR hw.foot (NtfnStep.tcb hk ts x.tcbArch) hNB + refine WP.spec_mono (WP.spec_and (hO.scheduleTcb _ _ t hR1 hSR1 ht) (hF.scheduleTcb _ _ t hR1 hSR1 ht)) ?_ + rintro s2 ⟨⟨hf, hS⟩, hw2⟩ + exact ⟨hR1.frame hP hf (KView.stsAct _ _), hS, + FpSA.trans hF.back (FpSA.of_sw ((writesIn_wr (a := stA t) + (by rw [stA_eq]; exact tword hP ht (K := 3328#u64) (by simp) (by simp))).mono fun b hb => + Or.inr ⟨t, htQ, Or.inl hb⟩)) (Endp.ssz_updK_tcb σ _ _) (FpSA.of_wr hw2)⟩ +end Wk + +section Loop +open Fl4SpecA64 +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] {N T : Set U64} + {SR : S → Fl4SpecA64.State Unit → Prop} {DL : S → U64 → Prop} + {Wr : Fl4SpecA64.State Unit → S → U64 → Prop} + +variable (st N T SR Wr) in +/-- `NInv` with the keep, the SC sizes and the footprint from the loop's entry `(σ0, s0)`. -/ +def NInvF (Q : Set U64) (n : U64) (σ0 : Fl4SpecA64.State Unit) (s0 : S) (q0 : List Fl4SpecA64.ObjRef) (x : S × U64 × U64) : + Prop := + ∃ σ' y, ∃ rest : List U64, NtfnStateRel N T (L.mem x.1) σ' ∧ SR x.1 σ' ∧ + σ'.kheap (specPtr n) = some (.notification y) ∧ + (rest ≠ [] → y.ntfnObj = .waitingNtfn (rest.map specPtr)) ∧ x.2.1 = rest.headD 0#u64 ∧ + rest.length ≤ x.2.2.val ∧ (∀ t ∈ rest, t ∈ T → L.mem x.1 (affA t) = L.cpu x.1) ∧ + (∀ z, z ∈ (Fl4Spec.NonDet.mapMx (rrn n) (rest.map specPtr) σ').1 → + z ∈ (Fl4Spec.NonDet.mapMx (rrn n) q0 σ0).1) ∧ + ((Fl4Spec.NonDet.mapMx (rrn n) q0 σ0).2 → (Fl4Spec.NonDet.mapMx (rrn n) (rest.map specPtr) σ').2) ∧ + NtfnSpecKeepA σ0 σ' ∧ SSzA σ0 σ' ∧ FpSA st Q Wr n σ0 s0 x.1 ∧ (∀ t ∈ rest, t ∈ Q) ∧ NtfnTcbKeepA σ0 σ' + +/-- The restart tail of the dequeued head, re-establishing `NInvF` (`sig_tail` with the keep and footprint). -/ +theorem sigF_tail (hO : SchedOpsA st ar N T SR DL) (hF : SchedFootNA st ar N T SR DL Wr) (hP : NPlaced T N) + {Q : Set U64} {n : U64} (σ0 : Fl4SpecA64.State Unit) (s0 : S) (q0 : List Fl4SpecA64.ObjRef) {s2 : S} + {σ' : Fl4SpecA64.State Unit} + {y : Fl4SpecA64.Notification} {rest' : List U64} {t0 nx fuel1 : U64} + (hky : σ'.kheap (specPtr n) = some (.notification y)) + (hw : y.ntfnObj = .waitingNtfn ((t0 :: rest').map specPtr)) (hnd : (t0 :: rest').Nodup) (ht0T : t0 ∈ T) + {xt : Fl4SpecA64.Tcb} + (hktd : (updK σ' (specPtr n) (.notification (afterDequeue y t0))).kheap (specPtr t0) = some (.tcb xt)) + (hq1 : ∀ n' ∈ N, ∀ z, (updK σ' (specPtr n) (.notification (afterDequeue y t0))).kheap (specPtr n') = + some (.notification z) → specPtr t0 ∉ z.ntfnObj.ntfnQueue) + (hR2 : NtfnStateRel N T (L.mem s2) (stsAct (updK (updK σ' (specPtr n) (.notification (afterDequeue y t0))) + (specPtr t0) (.tcb { xt with tcbState := .restart })) (specPtr t0))) + (hS2 : SR s2 (stsAct (updK (updK σ' (specPtr n) (.notification (afterDequeue y t0))) + (specPtr t0) (.tcb { xt with tcbState := .restart })) (specPtr t0))) + (hdq : Fl4SpecA64.tcbNtfnDequeue (Z := Unit) (specPtr t0) (specPtr n) σ' = + ({((), updK σ' (specPtr n) (.notification (afterDequeue y t0)))}, False)) + (hnx : nx = rest'.headD 0#u64) (hlen : rest'.length ≤ fuel1.val) + (hon : ∀ t ∈ t0 :: rest', t ∈ T → L.mem s2 (affA t) = L.cpu s2) + (hNQ : NotQueued (stsAct (updK (updK σ' (specPtr n) (.notification (afterDequeue y t0))) + (specPtr t0) (.tcb { xt with tcbState := .restart })) (specPtr t0)) (specPtr t0)) + (hinc : ∀ z, z ∈ (Fl4Spec.NonDet.mapMx (rrn n) ((t0 :: rest').map specPtr) σ').1 → + z ∈ (Fl4Spec.NonDet.mapMx (rrn n) q0 σ0).1) + (hfail : (Fl4Spec.NonDet.mapMx (rrn n) q0 σ0).2 → + (Fl4Spec.NonDet.mapMx (rrn n) ((t0 :: rest').map specPtr) σ').2) + (hK2 : NtfnSpecKeepA σ0 (stsAct (updK (updK σ' (specPtr n) (.notification (afterDequeue y t0))) + (specPtr t0) (.tcb { xt with tcbState := .restart })) (specPtr t0))) + (hZ2 : SSzA σ0 (stsAct (updK (updK σ' (specPtr n) (.notification (afterDequeue y t0))) + (specPtr t0) (.tcb { xt with tcbState := .restart })) (specPtr t0))) + (hfp2 : FpSA st Q Wr n σ0 s0 s2) (hqs : ∀ t ∈ rest', t ∈ Q) + (hM2 : NtfnTcbKeepA σ0 (stsAct (updK (updK σ' (specPtr n) (.notification (afterDequeue y t0))) + (specPtr t0) (.tcb { xt with tcbState := .restart })) (specPtr t0))) : + (do + let s3 ← notification.unblock_sporadic st s2 t0 + let s4 ← sched.possible_switch_to st s3 t0 + ok (ControlFlow.cont (s4, nx, fuel1) : ControlFlow (S × U64 × U64) S)) ⦃ r => + ∃ x' : S × U64 × U64, r = (ControlFlow.cont x' : ControlFlow (S × U64 × U64) S) ∧ + NInvF st N T SR Wr Q n σ0 s0 q0 x' ∧ x'.2.2 = fuel1 ⦄ := by + have hte : specPtr t0 ≠ specPtr n := by + intro h; rw [h, updK_kheap, Function.update_self] at hktd; cases hktd + have hk2 : (stsAct (updK (updK σ' (specPtr n) (.notification (afterDequeue y t0))) (specPtr t0) + (.tcb { xt with tcbState := .restart })) (specPtr t0)).kheap (specPtr t0) = + some (.tcb { xt with tcbState := .restart }) := by + rw [stsAct_kheap, updK_kheap, Function.update_self] + have hkn2 : (stsAct (updK (updK σ' (specPtr n) (.notification (afterDequeue y t0))) (specPtr t0) + (.tcb { xt with tcbState := .restart })) (specPtr t0)).kheap (specPtr n) = + some (.notification (afterDequeue y t0)) := by + rw [stsAct_kheap, updK_kheap, Function.update_of_ne hte.symm, updK_kheap, Function.update_self] + have hq2 : ∀ n' ∈ N, ∀ z, (stsAct (updK (updK σ' (specPtr n) (.notification (afterDequeue y t0))) (specPtr t0) + (.tcb { xt with tcbState := .restart })) (specPtr t0)).kheap (specPtr n') = + some (.notification z) → specPtr t0 ∉ z.ntfnObj.ntfnQueue := by + intro n' hn' z hz + have hne : specPtr n' ≠ specPtr t0 := by intro h; rw [h, hk2] at hz; cases hz + rw [stsAct_kheap, updK_kheap, Function.update_of_ne hne] at hz + exact hq1 n' hn' z hz + have hrr : Fl4SpecA64.removeAndRestartNtfnQueuedThread (Z := Unit) (specPtr t0) (specPtr n) σ' = _ := + rrn_eq hdq hktd + have hprog : (do + let s3 ← notification.unblock_sporadic st s2 t0 + let s4 ← sched.possible_switch_to st s3 t0 + ok (ControlFlow.cont (s4, nx, fuel1) : ControlFlow (S × U64 × U64) S)) = + (rustRestartTailN st s2 t0 >>= fun s4 => ok (ControlFlow.cont (s4, nx, fuel1) : ControlFlow (S × U64 × U64) S)) := by + first + | (simp only [rustRestartTailN, bind_assoc]; done) + | (unfold rustRestartTailN; rw [bind_assoc_eq]) + | (unfold rustRestartTailN; simp only [Std.bind_tc_eq, Std.bind_assoc]) + | rfl + rw [hprog] + refine WP.spec_bind (WP.spec_and + (hO.restartTail s2 _ t0 _ hR2 hS2 ht0T hk2 rfl hq2 (hon t0 List.mem_cons_self ht0T) hNQ) + (hF.restartTail s2 _ t0 _ hR2 hS2 ht0T hk2 rfl hq2 (hon t0 List.mem_cons_self ht0T) hNQ)) ?_ + rintro s4 ⟨⟨hnf, σ'', hres, hf, hv, hS4⟩, hw4⟩ + rw [WP.spec_ok] + have hresx : ((), σ'') ∈ (Endp.specRestartTail (specPtr t0) (stsAct (updK (updK σ' (specPtr n) + (.notification (afterDequeue y t0))) (specPtr t0) (.tcb { xt with tcbState := .restart })) (specPtr t0))).1 := + hres + refine ⟨_, rfl, ⟨σ'', afterDequeue y t0, rest', hR2.frame hP hf hv, hS4, hv.1 _ _ hkn2, + afterDequeue_obj hw hnd, hnx, hlen, + fun t ht hT => (hO.coreKeep _ _ _ _ hS2 hS4).onCore hT (hon t (List.mem_cons_of_mem _ ht) hT), + fun z hz => hinc z (Endp.mapMx_cons_mem (g := rrn n) (l := rest'.map specPtr) (by (try simp only [rrn]); rw [hrr]; exact hres) z hz), + fun hw' => Det.detAt_bind_fail ((Det.det_removeAndRestartNtfn _ _).at σ') (by (try simp only [rrn]); rw [hrr]; exact hnf) + (by (try simp only [rrn]); rw [hrr]; exact hres) (hfail hw'), + hK2.trans (specRestartTail_nkA _ _ _ hresx), hZ2.trans (specRestartTail_ssz _ hresx), + FpSA.trans hF.back hfp2 hZ2 (FpSA.of_wr hw4), hqs, + hM2.trans (NtfnTcbKeepA.of_scOnly (specRestartTail_tkA _ _ _ hresx))⟩, rfl⟩ + +/-- **One dequeue-and-restart step of the loop** with the keep and footprint. -/ +theorem sigF_deq (hH : SchedHypsA st N T SR) (hO : SchedOpsA st ar N T SR DL) (hF : SchedFootNA st ar N T SR DL Wr) + (hP : NPlaced T N) {Q : Set U64} {n : U64} + (hn : n ∈ N) (σ0 : Fl4SpecA64.State Unit) (s0 : S) (q0 : List Fl4SpecA64.ObjRef) {s : S} + {σ' : Fl4SpecA64.State Unit} + {y : Fl4SpecA64.Notification} {rest' : List U64} {t0 nx fuel1 : U64} + (hR : NtfnStateRel N T (L.mem s) σ') (hSR : SR s σ') (hky : σ'.kheap (specPtr n) = some (.notification y)) + (hw : y.ntfnObj = .waitingNtfn ((t0 :: rest').map specPtr)) (hst : stOf (L.mem s) n = 1#u64) + (hQ : QueueRel (L.mem s) T .Ep (qOf (L.mem s) n) (t0 :: rest')) + (hB : ∀ z ∈ t0 :: rest', BlockedOn (L.mem s) n z) + (hnx : nx = rest'.headD 0#u64) (hlen : rest'.length ≤ fuel1.val) + (hon : ∀ t ∈ t0 :: rest', t ∈ T → L.mem s (affA t) = L.cpu s) + (hinc : ∀ z, z ∈ (Fl4Spec.NonDet.mapMx (rrn n) ((t0 :: rest').map specPtr) σ').1 → + z ∈ (Fl4Spec.NonDet.mapMx (rrn n) q0 σ0).1) + (hfail : (Fl4Spec.NonDet.mapMx (rrn n) q0 σ0).2 → + (Fl4Spec.NonDet.mapMx (rrn n) ((t0 :: rest').map specPtr) σ').2) + (hK : NtfnSpecKeepA σ0 σ') (hZ : SSzA σ0 σ') (hfp : FpSA st Q Wr n σ0 s0 s) + (hqs : ∀ t ∈ t0 :: rest', t ∈ Q) (hM : NtfnTcbKeepA σ0 σ') : + (do + let s1 ← notification.ntfn_dequeue st s n t0 + let s2 ← sched.set_thread_state st s1 t0 sched.TS_RESTART + let s3 ← notification.unblock_sporadic st s2 t0 + let s4 ← sched.possible_switch_to st s3 t0 + ok (ControlFlow.cont (s4, nx, fuel1) : ControlFlow (S × U64 × U64) S)) ⦃ r => + ∃ x' : S × U64 × U64, r = (ControlFlow.cont x' : ControlFlow (S × U64 × U64) S) ∧ + NInvF st N T SR Wr Q n σ0 s0 q0 x' ∧ x'.2.2 = fuel1 ⦄ := by + have hd : t0 ∈ t0 :: rest' := List.mem_cons_self + have ht0T : t0 ∈ T := hQ.mem t0 hd + obtain ⟨xt, hkt, hxt⟩ := hR.tcb t0 ht0T + obtain ⟨m6, hm6⟩ : ∃ m, xt.tcbState = .blockedOnNotification m := by + have := hxt.st + rw [(hB t0 hd).1] at this + exact tsCode_six' this.symm + have hmem : specPtr t0 ∈ Fl4SpecA64.ntfnQueueOf y := by + show specPtr t0 ∈ y.ntfnObj.ntfnQueue + rw [hw]; simp [Fl4SpecA64.Ntfn.ntfnQueue] + have hdq : Fl4SpecA64.tcbNtfnDequeue (Z := Unit) (specPtr t0) (specPtr n) σ' = + ({((), updK σ' (specPtr n) (.notification (afterDequeue y t0)))}, False) := tcbNtfnDequeue_eq hky hmem + have hte : specPtr t0 ≠ specPtr n := by intro h; rw [h, hky] at hkt; cases hkt + have hktd : (updK σ' (specPtr n) (.notification (afterDequeue y t0))).kheap (specPtr t0) = some (.tcb xt) := by + rw [updK_kheap, Function.update_of_ne hte, hkt] + refine WP.spec_bind (ntfn_dequeue_full hP s hn t0 _ hQ hd) ?_ + rintro s1 ⟨h1, h2, h3⟩ + obtain ⟨hR1, hq1⟩ := rel_after_dequeue hR hn hky hw hQ hB hd h1 h2 h3 hst + have hSR1 : SR s1 (updK σ' (specPtr n) (.notification (afterDequeue y t0))) := + hH.stable _ _ _ _ hSR h3.foot (NtfnStep.ntfnK (y := afterDequeue y t0) hky rfl rfl) (Endp.NoBlockE.ntfnK _ _ _) + have hKa : NtfnSpecKeepA σ' (updK σ' (specPtr n) (.notification (afterDequeue y t0))) := + NtfnSpecKeepA.of_upd hky rfl ⟨_, rfl⟩ + have hKb : NtfnSpecKeepA (updK σ' (specPtr n) (.notification (afterDequeue y t0))) + (updK (updK σ' (specPtr n) (.notification (afterDequeue y t0))) (specPtr t0) + (.tcb { xt with tcbState := .restart })) := + NtfnSpecKeepA.of_upd hktd rfl ⟨_, rfl, rfl, rfl, rfl⟩ + have hKc : NtfnSpecKeepA (updK (updK σ' (specPtr n) (.notification (afterDequeue y t0))) (specPtr t0) + (.tcb { xt with tcbState := .restart })) + (stsAct (updK (updK σ' (specPtr n) (.notification (afterDequeue y t0))) (specPtr t0) + (.tcb { xt with tcbState := .restart })) (specPtr t0)) := fun p => Or.inl (by rw [stsAct_kheap]) + have hZ1 : SSzA σ0 (updK σ' (specPtr n) (.notification (afterDequeue y t0))) := + hZ.trans (ssz_updK_ntfn _ _ _) + have hZ1' := hZ1.trans (ssz_updK_tcb _ (specPtr t0) { xt with tcbState := .restart }) + have hfp1 : FpSA st Q Wr n σ0 s0 s1 := + FpSA.trans hF.back hfp hZ (FpSA.of_sw (h3.sigW hP hn hqs)) + have hMa : NtfnTcbKeepA σ' (updK σ' (specPtr n) (.notification (afterDequeue y t0))) := + NtfnTcbKeepA.of_ntfn hky rfl rfl + have hMb : NtfnTcbKeepA (updK σ' (specPtr n) (.notification (afterDequeue y t0))) + (updK (updK σ' (specPtr n) (.notification (afterDequeue y t0))) (specPtr t0) + (.tcb { xt with tcbState := .restart })) := + NtfnTcbKeepA.of_tcb hktd rfl ⟨rfl, rfl, rfl, rfl, rfl, rfl, Or.inr ⟨⟨m6, hm6⟩, rfl⟩⟩ rfl + have hMc : NtfnTcbKeepA (updK (updK σ' (specPtr n) (.notification (afterDequeue y t0))) (specPtr t0) + (.tcb { xt with tcbState := .restart })) + (stsAct (updK (updK σ' (specPtr n) (.notification (afterDequeue y t0))) (specPtr t0) + (.tcb { xt with tcbState := .restart })) (specPtr t0)) := + NtfnTcbKeepA.of_scOnly ⟨fun p o h _ => by rw [stsAct_kheap]; exact h, by unfold stsAct; split <;> rfl⟩ + refine WP.spec_bind (set_thread_state_sigF hH hO hF hP n s1 _ hR1 hSR1 ht0T hktd hq1 (hqs t0 hd) .restart (fun _ h => by cases h) + sched.TS_RESTART (by rw [tsVals.2.1]; rfl) + (Endp.NoBlockE.updT hktd _ (Or.inr (Or.inl ⟨rfl, (by rw [hm6]; exact fun h => by cases h), + Or.inl (Or.inr (Or.inl ⟨m6, hm6⟩))⟩)))) ?_ + rintro s2 ⟨hR2, hS2, hfs2⟩ + exact sigF_tail hO hF hP σ0 s0 q0 hky hw hQ.nodup ht0T hktd hq1 hR2 hS2 hdq hnx hlen + (fun t ht hT => (hO.coreKeep _ _ _ _ hSR hS2).onCore hT (hon t ht hT)) + ((hH.notQueued s1 _ t0 xt hSR1 hktd (by rw [hm6]; rfl)).sts _ _ _) hinc hfail + (hK.trans (hKa.trans (hKb.trans hKc))) (hZ1'.trans (ssz_stsAct _ _)) + (FpSA.trans hF.back hfp1 hZ1 hfs2) (fun t ht => hqs t (List.mem_cons_of_mem _ ht)) + (hM.trans (hMa.trans (hMb.trans hMc))) + +set_option maxHeartbeats 4000000 in +/-- **The loop of `cancel_all_signals`** with the keep and footprint. -/ +theorem sigF_loop (hH : SchedHypsA st N T SR) (hO : SchedOpsA st ar N T SR DL) (hF : SchedFootNA st ar N T SR DL Wr) + (hP : NPlaced T N) + {Q : Set U64} {n : U64} (hn : n ∈ N) (σ0 : Fl4SpecA64.State Unit) (s0 : S) (q0 : List Fl4SpecA64.ObjRef) + (x0 : S × U64 × U64) (h0 : NInvF st N T SR Wr Q n σ0 s0 q0 x0) : + notification.cancel_all_signals_loop st x0.1 n x0.2.1 x0.2.2 ⦃ s' => + ¬ (Fl4Spec.NonDet.mapMx (rrn n) q0 σ0).2 ∧ + ∃ σf, ((), σf) ∈ (Fl4Spec.NonDet.mapMx (rrn n) q0 σ0).1 ∧ NtfnStateRel N T (L.mem s') σf ∧ SR s' σf ∧ + NtfnSpecKeepA σ0 σf ∧ SSzA σ0 σf ∧ FpSA st Q Wr n σ0 s0 s' ∧ NtfnTcbKeepA σ0 σf ⦄ := by + unfold notification.cancel_all_signals_loop + apply loop.spec_decr_nat (measure := fun x : S × U64 × U64 => x.2.2.val) + (inv := NInvF st N T SR Wr Q n σ0 s0 q0) _ _ x0 ?_ h0 + rintro ⟨s, t, fuel⟩ ⟨σ', y, rest, hR, hSR, hky, hw, ht, hlen, hon, hinc, hfail, hK, hZ, hfp, hqs, hM⟩ + simp only at hR hSR hky hw ht hlen hon hinc hfail hK hZ hfp hqs hM ⊢ + unfold notification.cancel_all_signals_loop.body + step with obj.Tcb.is_null.spec as ⟨bn, hbn⟩ + cases rest with + | nil => + simp only [List.headD_nil] at ht + subst ht + simp only [hbn, decide_true, ↓reduceIte, WP.spec_ok] + exact ⟨fun h => hfail h, σ', hinc _ rfl, hR, hSR, hK, hZ, hfp, hM⟩ + | cons t0 rest' => + simp only [List.headD_cons] at ht + subst t + have hw' := hw (List.cons_ne_nil _ _) + have hobj := (hR.at_ntfn hn hky).obj + rw [hw'] at hobj + obtain ⟨hst, xs, -, hxs, hQ, hB⟩ := hobj + have hxs' : xs = t0 :: rest' := + (List.map_injective_iff.2 (fun a b h => specPtr_inj h) hxs).symm + subst hxs' + have ht0T : t0 ∈ T := hQ.mem t0 (by simp) + have ht00 : t0 ≠ 0#u64 := hQ.ne0 hP.tcb t0 (by simp) + have hf0 : fuel > 0#u64 := by + simp only [List.length_cons] at hlen + show (0#u64 : U64).val < fuel.val; simp; omega + simp only [hbn, ht00, decide_false, Bool.false_eq_true, ↓reduceIte, hf0] + step as ⟨fuel1, hfuel1⟩ + have hseg := hQ.seg + rw [show t0 :: rest' = [] ++ t0 :: rest' from rfl, Seg_mid] at hseg + obtain ⟨-, -, hnext, -⟩ := hseg + unfold tcbqueue.next_of + step with tcbqueue.next.spec as ⟨nx, hnx⟩ + try simp only [bind_ok] + refine WP.spec_mono (sigF_deq hH hO hF hP hn σ0 s0 q0 hR hSR hky hw' hst hQ hB (by rw [hnx, hnext]) + (by rw [hfuel1]; simp only [List.length_cons] at hlen; scalar_tac) hon hinc hfail hK hZ hfp hqs hM) ?_ + rintro r ⟨x', rfl, hI, hf⟩ + exact ⟨hI, by rw [hf, hfuel1]; scalar_tac⟩ + +set_option maxHeartbeats 4000000 in +/-- **`cancel_all_signals` refines `cancel_all_signals`** on `NtfnStateRel ∧ SR`, also exporting the spec keep, the +SC sizes and the loop's write set. -/ +theorem cancel_all_signals_relF (hH : SchedHypsA st N T SR) (hO : SchedOpsA st ar N T SR DL) + (hF : SchedFootNA st ar N T SR DL Wr) (hP : NPlaced T N) + (s : S) (σ : Fl4SpecA64.State Unit) (hR : NtfnStateRel N T (L.mem s) σ) (hSR : SR s σ) {n : U64} (hn : n ∈ N) + (hon : ∀ z, σ.kheap (specPtr n) = some (.notification z) → ∀ t ∈ T, specPtr t ∈ z.ntfnObj.ntfnQueue → + L.mem s (affA t) = L.cpu s) + {Q : Set U64} (hQs : ∀ z, σ.kheap (specPtr n) = some (.notification z) → ∀ t ∈ T, + specPtr t ∈ z.ntfnObj.ntfnQueue → t ∈ Q) : + notification.cancel_all_signals st s n ⦃ s' => ¬ (Fl4SpecA64.cancelAllSignals (Z := Unit) (specPtr n) σ).2 ∧ + ∃ σ', ((), σ') ∈ (Fl4SpecA64.cancelAllSignals (Z := Unit) (specPtr n) σ).1 ∧ + NtfnStateRel N T (L.mem s') σ' ∧ SR s' σ' ∧ NtfnSpecKeepA σ σ' ∧ SSzA σ σ' ∧ FpSA st Q Wr n σ s s' ∧ + NtfnTcbKeepA σ σ' ⦄ := by + obtain ⟨y, hky, hy⟩ := hR.ntfn n hn + unfold notification.cancel_all_signals + step with obj.Ntfn.state.spec as ⟨i, hi⟩ + rw [epState_eq] at hi + cases hw : y.ntfnObj with + | waitingNtfn q => + have hsw : i = obj.NTFN_WAITING := by rw [hi]; exact stOf_waiting hy.obj hw + have hsp : Fl4SpecA64.cancelAllSignals (Z := Unit) (specPtr n) σ = + (Fl4Spec.NonDet.mapMx (rrn n) q >>= fun _ => Fl4SpecA64.rescheduleRequired) σ := by + unfold Fl4SpecA64.cancelAllSignals + rw [nd_bind_one (getNotification_eq hky), hw] + simp only [hsw, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + have hobj := hy.obj + rw [hw] at hobj + obtain ⟨-, xs, -, hqx, hQ, -⟩ := hobj + step with notification.get_queue.spec as ⟨qq, hqq⟩ + have hfuel : layout.WALK_FUEL.val = 2 ^ 64 - 1 := by simp [layout.WALK_FUEL, U64.rMax] + have hI0 : NInvF st N T SR Wr Q n σ s q (s, qq.head, layout.WALK_FUEL) := + ⟨σ, y, xs, hR, hSR, hky, fun _ => by rw [hw, hqx], by rw [hqq, epQ_eq, hQ.head], + by rw [hfuel]; exact Endp.len_le_max xs hQ.nodup (hQ.ne0 hP.tcb), + fun t ht hT => hon y hky t hT (by rw [hw]; show specPtr t ∈ q; rw [hqx]; exact List.mem_map.2 ⟨t, ht, rfl⟩), + fun z hz => by rw [hqx]; exact hz, fun h => by rw [← hqx]; exact h, + NtfnSpecKeepA.refl σ, SSzA.refl σ, FpSA.refl σ s, + fun t ht => hQs y hky t (hQ.mem t ht) (by rw [hw]; show specPtr t ∈ q; rw [hqx]; exact List.mem_map.2 ⟨t, ht, rfl⟩), + NtfnTcbKeepA.refl σ⟩ + refine WP.spec_bind (sigF_loop hH hO hF hP hn σ s q _ hI0) ?_ + rintro s1 ⟨hnf, σf, hmf, hRf, hSf, hKf, hZf, hFf, hMf⟩ + refine WP.spec_mono (WP.spec_and (hO.reschedule s1 σf hRf hSf) (hF.reschedule s1 σf hRf hSf)) ?_ + rintro s2 ⟨⟨hnr2, σr, hrs, hf2, hv2, hS2⟩, hw2⟩ + refine ⟨?_, σr, ?_, hRf.frame hP hf2 hv2, hS2, hKf.trans (rescheduleRequired_nkA _ _ hrs), + hZf.trans (rescheduleRequired_ssz hrs), FpSA.trans hF.back hFf hZf (FpSA.of_wr hw2), + hMf.trans (NtfnTcbKeepA.of_scOnly (rescheduleRequired_tkA _ _ hrs))⟩ + · rw [hsp] + exact fun h => hnr2 (Det.detAt_bind_fail + ((Det.det_mapMx (fun a => Det.det_removeAndRestartNtfn a _) q).at σ) hnf hmf h) + rw [hsp] + exact ⟨((), σf), hmf, hrs⟩ + | idleNtfn => + have hsw : i = obj.NTFN_IDLE := by rw [hi]; exact stOf_idle hy.obj hw + have hsp : Fl4SpecA64.cancelAllSignals (Z := Unit) (specPtr n) σ = ({((), σ)}, False) := by + unfold Fl4SpecA64.cancelAllSignals + rw [nd_bind_one (getNotification_eq hky), hw] + rfl + simp only [hsw, ntfn_consts.1, ntfn_consts.2.1, show (0#u64 : U64) ≠ 1#u64 by decide, bne_iff_ne, ne_eq, + not_false_eq_true, ↓reduceIte, WP.spec_ok] + exact ⟨by rw [hsp]; exact fun h => h, σ, by rw [hsp]; rfl, hR, hSR, NtfnSpecKeepA.refl σ, SSzA.refl σ, + FpSA.refl σ s, NtfnTcbKeepA.refl σ⟩ + | activeNtfn b => + have hsw : i = obj.NTFN_ACTIVE := by rw [hi]; exact stOf_active hy.obj hw + have hsp : Fl4SpecA64.cancelAllSignals (Z := Unit) (specPtr n) σ = ({((), σ)}, False) := by + unfold Fl4SpecA64.cancelAllSignals + rw [nd_bind_one (getNotification_eq hky), hw] + rfl + simp only [hsw, ntfn_consts.2.1, ntfn_consts.2.2, show (2#u64 : U64) ≠ 1#u64 by decide, bne_iff_ne, ne_eq, + not_false_eq_true, ↓reduceIte, WP.spec_ok] + exact ⟨by rw [hsp]; exact fun h => h, σ, by rw [hsp]; rfl, hR, hSR, NtfnSpecKeepA.refl σ, SSzA.refl σ, + FpSA.refl σ s, NtfnTcbKeepA.refl σ⟩ + +end Loop + +end + +end Fl4ProofsA64.P3.Notif diff --git a/proofs/Fl4ProofsA64/P3/SigKeepA.lean b/proofs/Fl4ProofsA64/P3/SigKeepA.lean new file mode 100644 index 000000000..db6c58887 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/SigKeepA.lean @@ -0,0 +1,232 @@ +/- +P3 (aarch64, delete hooks W3, contract §44): **the spec side of `cancel_all_signals`' loop tail**. `NtfnSpecKeepA σ σ'` +(`DelNtfnFrameA`: every object kept, or replaced by the same kind with the framed fields kept) is preserved by +the spec's restart tail (`Endp.specRestartTail`: the sporadic refill check, `possibleSwitchTo`) and by +`rescheduleRequired`. `SchedOpsA.restartTail` / `reschedule` export only `KView`, which says nothing of the MCP, +yield-to, reply-stack fields, so the keep is proved on the spec definitions by a copy of the `SPresInst` calculus +(`P3/SpecFrameCalcA`) at `NtfnSpecKeepA`. No named premise; new module. +-/ +import Fl4ProofsA64.P3.DelNtfnFrameA +import Fl4ProofsA64.P3.SpecFrameSchedA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P3 Fl4ProofsA64.P3.Endp + +noncomputable section + +instance : PreO NtfnSpecKeepA := ⟨fun σ => NtfnSpecKeepA.refl σ, fun h1 h2 => NtfnSpecKeepA.trans h1 h2⟩ + +/-- Preservation of `NtfnSpecKeepA` by a spec computation. -/ +abbrev NKPresA {α : Type} (m : SMonad Unit α) : Prop := PresR NtfnSpecKeepA m + +class NKInst {α : Type} (m : SMonad Unit α) : Prop where + pres : NKPresA m + +instance {α β : Type} {m : SMonad Unit α} {f : α → SMonad Unit β} [hm : NKInst m] + [hf : ∀ a, NKInst (f a)] : NKInst (m >>= f) := + ⟨PresR.bind hm.pres fun a => (hf a).pres⟩ + +instance {α : Type} (a : α) : NKInst (pure a : SMonad Unit α) := ⟨PresR.pure a⟩ + +instance {α : Type} (f : State Unit → α) : NKInst (NonDet.gets f : SMonad Unit α) := ⟨PresR.gets f⟩ + +instance : NKInst (NonDet.get : SMonad Unit (State Unit)) := ⟨PresR.get⟩ + +instance {α : Type} (f : State Unit → Option α) : NKInst (NonDet.getsThe f : SMonad Unit α) := + ⟨PresR.getsThe f⟩ + +instance {α : Type} (o : Option α) : NKInst (NonDet.assertOpt o : SMonad Unit α) := ⟨PresR.assertOpt o⟩ + +instance (P : Prop) : NKInst (NonDet.assert P : SMonad Unit Unit) := ⟨PresR.assert P⟩ + +instance {α : Type} : NKInst (NonDet.fail : SMonad Unit α) := ⟨PresR.fail⟩ + +instance {P : Prop} {d : Decidable P} {m : SMonad Unit Unit} [h : NKInst m] : + NKInst (@NonDet.when (State Unit) P d m) := ⟨PresR.when h.pres⟩ + +instance {P : Prop} {d : Decidable P} {m : SMonad Unit Unit} [h : NKInst m] : + NKInst (@NonDet.unless (State Unit) P d m) := ⟨PresR.unless' h.pres⟩ + +instance {α : Type} {P : Prop} {d : Decidable P} {a b : SMonad Unit α} [ha : NKInst a] [hb : NKInst b] : + NKInst (@ite _ P d a b) := ⟨PresR.ite' ha.pres hb.pres⟩ + +instance {β γ : Type} {f : γ → SMonad Unit β} [hf : ∀ a, NKInst (f a)] (l : List γ) : + NKInst (NonDet.mapM f l) := ⟨PresR.mapM (fun a => (hf a).pres) l⟩ + +instance {α : Type} {C : α → State Unit → Prop} {B : α → SMonad Unit α} [hB : ∀ a, NKInst (B a)] (r : α) : + NKInst (NonDet.whileLoop C B r) := ⟨PresR.whileLoop (fun a => (hB a).pres) r⟩ + +instance {α : Type} (S : Set α × Prop) : NKInst (NonDet.selectF S : SMonad Unit α) := ⟨PresR.selectF S⟩ + +instance {α : Type} {f : α → SMonad Unit Unit} [hf : ∀ a, NKInst (f a)] (o : Option α) : + NKInst (NonDet.maybeM f o) := by + cases o + · exact ⟨PresR.pure _⟩ + · exact hf _ + +/-- A TCB function keeping the framed fields. -/ +class TcbMyFn (f : Tcb → Tcb) : Prop where + keep : ∀ x, (f x).tcbMcpriority = x.tcbMcpriority ∧ (f x).tcbYieldTo = x.tcbYieldTo ∧ + (f x).tcbFlags = x.tcbFlags + +/-- An SC function keeping the reply stack and the yield-from link. -/ +class ScRyFn (f : SchedContext → SchedContext) : Prop where + keep : ∀ x, (f x).scReplies = x.scReplies ∧ (f x).scYieldFrom = x.scYieldFrom + +instance (ts : ThreadState) : TcbMyFn (fun tcb : Tcb => { tcb with tcbState := ts }) := ⟨fun _ => ⟨rfl, rfl, rfl⟩⟩ +instance (g : Option ObjRef → Option ObjRef) : TcbMyFn (tcbSchedContextUpdate g) := ⟨fun _ => ⟨rfl, rfl, rfl⟩⟩ + +instance (g) : ScRyFn (scRefillsUpdate g) := ⟨fun _ => ⟨rfl, rfl⟩⟩ +instance (g) : ScRyFn (scRefillMaxUpdate g) := ⟨fun _ => ⟨rfl, rfl⟩⟩ + +/-- A state modification that keeps the heap. -/ +theorem NKPresA.modify_fix (f : State Unit → State Unit) (hk : ∀ σ, (f σ).kheap = σ.kheap) : + NKPresA (NonDet.modify f : SMonad Unit Unit) := + PresR.modify f fun σ p => Or.inl (by rw [hk]) + +macro "nkpres" : tactic => `(tactic| + repeat' (first + | exact NKInst.pres + | refine PresR.bind_pure _ _ ?_ + | refine PresR.bind ?_ (fun _ => ?_) + | refine PresR.when ?_ + | refine PresR.unless' ?_ + | refine PresR.ite' ?_ ?_ + | (refine NKPresA.modify_fix _ (fun _ => rfl)) + | dsimp only + | split)) + +theorem threadSet_nkA (f : Tcb → Tcb) [TcbMyFn f] (t : ObjRef) : NKPresA (threadSet (Z := Unit) f t) := by + intro σ r hr + obtain ⟨y1, hy1, h⟩ := hr + have hy1' : y1 ∈ (NonDet.assertOpt (getTcb (Z := Unit) t σ) σ).1 := hy1 + cases hg : getTcb (Z := Unit) t σ with + | none => rw [hg] at hy1'; exact absurd hy1' (Set.notMem_empty _) + | some x => + rw [hg] at hy1' + have e1 : y1 = (x, σ) := hy1' + subst e1 + obtain ⟨ko, hko, hr2⟩ := setObject_memA h + have hx := getTcb_kheapA hg + rw [hr2] + exact NtfnSpecKeepA.of_upd hx rfl ⟨_, rfl, (TcbMyFn.keep x).1, (TcbMyFn.keep x).2.1, (TcbMyFn.keep x).2.2⟩ + +theorem updateSchedContext_nkA (f : SchedContext → SchedContext) [ScRyFn f] (p : ObjRef) : + NKPresA (updateSchedContext (Z := Unit) p f) := by + intro σ r hr + obtain ⟨y1, hy1, h⟩ := hr + have hy1' : y1 ∈ (NonDet.assertOpt (σ.kheap p) σ).1 := hy1 + cases hk : σ.kheap p with + | none => rw [hk] at hy1'; exact absurd hy1' (Set.notMem_empty _) + | some ko => + rw [hk] at hy1' + have e1 : y1 = (ko, σ) := hy1' + subst e1 + cases ko with + | schedContext sc n => + obtain ⟨ko', hko, hr2⟩ := setObject_memA h + rw [hr2] + exact NtfnSpecKeepA.of_upd hk rfl ⟨_, rfl, (ScRyFn.keep sc).1, (ScRyFn.keep sc).2⟩ + | _ => exact absurd h (Set.notMem_empty _) + +instance (f : Tcb → Tcb) [TcbMyFn f] (t : ObjRef) : NKInst (threadSet (Z := Unit) f t) := ⟨threadSet_nkA f t⟩ +instance (p : ObjRef) (f : SchedContext → SchedContext) [ScRyFn f] : NKInst (updateSchedContext (Z := Unit) p f) := + ⟨updateSchedContext_nkA f p⟩ + +/-! ## Readers and small writers -/ + +instance {α : Type} (f : Tcb → α) (t : ObjRef) : NKInst (threadGet (Z := Unit) f t) := + ⟨by unfold threadGet; nkpres⟩ +instance (f : Tcb → Option ObjRef) (r : ObjRef) : NKInst (getTcbObjRef (Z := Unit) f r) := + ⟨by unfold getTcbObjRef; nkpres⟩ +instance (r : ObjRef) : NKInst (getThreadState (Z := Unit) r) := ⟨by unfold getThreadState; nkpres⟩ +instance (d : Domain) (p : Priority) : NKInst (getTcbQueue (Z := Unit) d p) := ⟨by unfold getTcbQueue; nkpres⟩ +instance (d : Domain) (p : Priority) (q : ReadyQueue) : NKInst (setTcbQueue (Z := Unit) d p q) := + ⟨by unfold setTcbQueue; nkpres⟩ +instance (a : ObjRef → List ObjRef → List ObjRef) (t : ObjRef) : NKInst (tcbSchedAction (Z := Unit) a t) := + ⟨by unfold tcbSchedAction; nkpres⟩ +instance (a : SchedulerAction) : NKInst (setSchedulerAction (Z := Unit) a) := + ⟨by unfold setSchedulerAction; nkpres⟩ +instance (t : ObjRef) : NKInst (tcbReleaseRemove (Z := Unit) t) := ⟨by unfold tcbReleaseRemove; nkpres⟩ + +instance (p : ObjRef) : NKInst (getObject (Z := Unit) p) := ⟨by unfold getObject; nkpres⟩ +instance (p : ObjRef) : NKInst (getSchedContext (Z := Unit) p) := ⟨by unfold getSchedContext; nkpres⟩ +instance (f : SchedContext → Option ObjRef) (p : ObjRef) : NKInst (getScObjRef (Z := Unit) f p) := + ⟨by unfold getScObjRef; nkpres⟩ +instance (p : ObjRef) : NKInst (getRefills (Z := Unit) p) := ⟨by unfold getRefills; nkpres⟩ +instance (p : ObjRef) : NKInst (getRefillHead (Z := Unit) p) := ⟨by unfold getRefillHead; nkpres⟩ +instance (p : ObjRef) : NKInst (getRefillTail (Z := Unit) p) := ⟨by unfold getRefillTail; nkpres⟩ +instance (p : ObjRef) : NKInst (getScRefillReady (Z := Unit) p) := ⟨by unfold getScRefillReady; nkpres⟩ +instance (p : ObjRef) (u : Ticks) : NKInst (getScRefillSufficient (Z := Unit) p u) := + ⟨by unfold getScRefillSufficient; nkpres⟩ +instance (p : ObjRef) (u : Ticks) : NKInst (fl4GetScRefillSufficient (Z := Unit) p u) := + ⟨by unfold fl4GetScRefillSufficient; nkpres⟩ +instance (p : ObjRef) : NKInst (getScActive (Z := Unit) p) := ⟨by unfold getScActive; nkpres⟩ +instance (p : ObjRef) : NKInst (isRoundRobin (Z := Unit) p) := ⟨by unfold isRoundRobin; nkpres⟩ + +/-! ## Scheduler queues -/ + +instance : NKInst (rescheduleRequired (Z := Unit)) := ⟨by unfold rescheduleRequired; nkpres⟩ +instance (t : ObjRef) : NKInst (possibleSwitchTo (Z := Unit) t) := ⟨by unfold possibleSwitchTo; nkpres⟩ + + +/-! ### Refills -/ + +instance (p : ObjRef) (f : Refill → Refill) : NKInst (updateRefillHd (Z := Unit) p f) := + ⟨by unfold updateRefillHd; nkpres⟩ +instance (p : ObjRef) (f : Refill → Refill) : NKInst (updateRefillTl (Z := Unit) p f) := + ⟨by unfold updateRefillTl; nkpres⟩ +instance (p : ObjRef) (r : Refill) : NKInst (refillAddTail (Z := Unit) p r) := ⟨by unfold refillAddTail; nkpres⟩ +instance (p : ObjRef) : NKInst (refillPopHead (Z := Unit) p) := ⟨by unfold refillPopHead; nkpres⟩ +instance (p : ObjRef) : NKInst (refillSize (Z := Unit) p) := ⟨by unfold refillSize; nkpres⟩ +instance (p : ObjRef) : NKInst (refillSingle (Z := Unit) p) := ⟨by unfold refillSingle; nkpres⟩ +instance (p : ObjRef) : NKInst (refillFull (Z := Unit) p) := ⟨by unfold refillFull; nkpres⟩ +instance (p : ObjRef) (r : Refill) : NKInst (scheduleUsed (Z := Unit) p r) := ⟨by unfold scheduleUsed; nkpres⟩ +instance (p : ObjRef) : NKInst (mergeOverlappingRefills (Z := Unit) p) := ⟨by unfold mergeOverlappingRefills; nkpres⟩ +instance (p : ObjRef) : NKInst (refillHeadOverlappingLoop (Z := Unit) p) := + ⟨by unfold refillHeadOverlappingLoop; nkpres⟩ +instance (p : ObjRef) : NKInst (refillUnblockCheck (Z := Unit) p) := ⟨by unfold refillUnblockCheck; nkpres⟩ +instance (o : Option ObjRef) (a b : Option Bool) : NKInst (ifCondRefillUnblockCheck (Z := Unit) o a b) := + ⟨by unfold ifCondRefillUnblockCheck; nkpres⟩ +instance (p : ObjRef) : NKInst (mergeNonoverlappingHeadRefill (Z := Unit) p) := + ⟨by unfold mergeNonoverlappingHeadRefill; nkpres⟩ +instance (m : Ticks) (p : ObjRef) : NKInst (headInsufficientLoopWith (Z := Unit) m p) := + ⟨by unfold headInsufficientLoopWith; nkpres⟩ +instance (p : ObjRef) (u : Ticks) : NKInst (chargeEntireHeadRefill (Z := Unit) p u) := + ⟨by unfold chargeEntireHeadRefill; nkpres⟩ +instance (l : Ticks) (p : ObjRef) (u : Ticks) : NKInst (handleOverrunWith (Z := Unit) l p u) := + ⟨by unfold handleOverrunWith; nkpres⟩ +instance (l m u : Ticks) : NKInst (refillBudgetCheckWith (Z := Unit) l m u) := + ⟨by unfold refillBudgetCheckWith; nkpres⟩ +instance (u : Ticks) : NKInst (fl4RefillBudgetCheck (Z := Unit) u) := ⟨by unfold fl4RefillBudgetCheck; nkpres⟩ +instance (u : Ticks) : NKInst (refillBudgetCheckRoundRobin (Z := Unit) u) := + ⟨by unfold refillBudgetCheckRoundRobin; nkpres⟩ + + +instance (t : ObjRef) : NKInst (setThreadStateAct (Z := Unit) t) := ⟨by unfold setThreadStateAct; nkpres⟩ +instance : NKInst (rescheduleRequired (Z := Unit)) := ⟨by unfold rescheduleRequired; nkpres⟩ +instance (t : ObjRef) : NKInst (possibleSwitchTo (Z := Unit) t) := ⟨by unfold possibleSwitchTo; nkpres⟩ + +/-- **`rescheduleRequired` keeps the framed fields.** -/ +theorem rescheduleRequired_nkA : NKPresA (rescheduleRequired (Z := Unit)) := NKInst.pres + +/-- **The spec's restart tail keeps the framed fields.** -/ +theorem specRestartTail_nkA (t : ObjRef) : NKPresA (specRestartTail t) := by + unfold specRestartTail; nkpres + +instance (t : ObjRef) : SPresInst (specRestartTail t) := ⟨by unfold specRestartTail; spres⟩ + +/-- The spec's restart tail keeps the SC sizes. -/ +theorem specRestartTail_ssz (t : ObjRef) {σ : State Unit} {x : Unit × State Unit} + (hx : x ∈ (specRestartTail t σ).1) : SSzA σ x.2 := + ((inferInstance : SPresInst (specRestartTail t)).pres σ x hx).2.1 + +/-- `rescheduleRequired` keeps the SC sizes. -/ +theorem rescheduleRequired_ssz {σ : State Unit} {x : Unit × State Unit} + (hx : x ∈ (rescheduleRequired (Z := Unit) σ).1) : SSzA σ x.2 := + ((inferInstance : SPresInst (rescheduleRequired (Z := Unit))).pres σ x hx).2.1 + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/SigTcbKeepA.lean b/proofs/Fl4ProofsA64/P3/SigTcbKeepA.lean new file mode 100644 index 000000000..093a5986f --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/SigTcbKeepA.lean @@ -0,0 +1,274 @@ +/- +P3 (aarch64, delete hooks W3, contract §45 step 1, executor 18): **the TCB / endpoint / reply keep of +`cancel_all_signals`' steps**. `NtfnTcbKeepA σ σ'` is the second spec relation of the finaliser steps, beside +`NtfnSpecKeepA` (`P3/DelNtfnFrameA`, unchanged): endpoint and reply objects are equal, user memory is equal, and +every TCB keeps its arch, fault, bound notification, priority and IPC buffer fields, with its state equal or going +`blockedOnNotification _` → `restart`. It is what `EpStateRel` reads of the spec (`P3/DelNtfnEpA`). + +The spec's restart tail and `rescheduleRequired` change only sched-context objects (`ScOnlyA`), proved by a copy of +the `NKInst` calculus of `P3/SigKeepA`. No named premise; new module. +-/ +import Fl4ProofsA64.P3.SigKeepA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open Fl4SpecA64 + +/-- What a finaliser step keeps of one TCB. -/ +def TcbKeepNA (x y : Tcb) : Prop := + y.tcbArch = x.tcbArch ∧ y.tcbFault = x.tcbFault ∧ y.tcbBoundNotification = x.tcbBoundNotification ∧ + y.tcbPriority = x.tcbPriority ∧ y.tcbIpcframe = x.tcbIpcframe ∧ y.tcbIpcBuffer = x.tcbIpcBuffer ∧ + (y.tcbState = x.tcbState ∨ (∃ n, x.tcbState = .blockedOnNotification n) ∧ y.tcbState = .restart) + +theorem TcbKeepNA.refl (x : Tcb) : TcbKeepNA x x := ⟨rfl, rfl, rfl, rfl, rfl, rfl, Or.inl rfl⟩ + +theorem TcbKeepNA.trans {x y z : Tcb} (h1 : TcbKeepNA x y) (h2 : TcbKeepNA y z) : TcbKeepNA x z := by + obtain ⟨a1, b1, c1, d1, f1, g1, e1⟩ := h1 + obtain ⟨a2, b2, c2, d2, f2, g2, e2⟩ := h2 + refine ⟨a2.trans a1, b2.trans b1, c2.trans c1, d2.trans d1, f2.trans f1, g2.trans g1, ?_⟩ + rcases e1 with e1 | ⟨⟨n, hn⟩, e1⟩ <;> rcases e2 with e2 | ⟨⟨m, hm⟩, e2⟩ + · exact Or.inl (e2.trans e1) + · exact Or.inr ⟨⟨m, e1.symm.trans hm⟩, e2⟩ + · exact Or.inr ⟨⟨n, hn⟩, e2.trans e1⟩ + · rw [e1] at hm; cases hm + +/-- The second spec relation of the finaliser steps (module docstring). -/ +structure NtfnTcbKeepA (σ σ' : State Unit) : Prop where + ep : ∀ p x, σ.kheap p = some (.endpoint x) → σ'.kheap p = some (.endpoint x) + reply : ∀ p x, σ.kheap p = some (.reply x) → σ'.kheap p = some (.reply x) + tcb : ∀ p x, σ.kheap p = some (.tcb x) → ∃ y, σ'.kheap p = some (.tcb y) ∧ TcbKeepNA x y + um : σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory + +theorem NtfnTcbKeepA.refl (σ : State Unit) : NtfnTcbKeepA σ σ := + ⟨fun _ _ h => h, fun _ _ h => h, fun _ x h => ⟨x, h, TcbKeepNA.refl x⟩, rfl⟩ + +theorem NtfnTcbKeepA.trans {σ1 σ2 σ3 : State Unit} (h1 : NtfnTcbKeepA σ1 σ2) (h2 : NtfnTcbKeepA σ2 σ3) : + NtfnTcbKeepA σ1 σ3 := + ⟨fun p x h => h2.ep p x (h1.ep p x h), fun p x h => h2.reply p x (h1.reply p x h), + fun p x h => by + obtain ⟨y, hy, k1⟩ := h1.tcb p x h + obtain ⟨z, hz, k2⟩ := h2.tcb p y hy + exact ⟨z, hz, k1.trans k2⟩, + h2.um.trans h1.um⟩ + +/-- One notification replaced. -/ +theorem NtfnTcbKeepA.of_ntfn {σ σ' : State Unit} {q : ObjRef} {o : Notification} {o' : KernelObject} + (hq : σ.kheap q = some (.notification o)) (h : σ'.kheap = Function.update σ.kheap q (some o')) + (hum : σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory) : NtfnTcbKeepA σ σ' := by + have ne : ∀ p k, σ.kheap p = some k → (∀ z, k ≠ .notification z) → σ'.kheap p = some k := fun p k hp hk => by + have : p ≠ q := fun e => by rw [e, hq] at hp; injection hp with hp; exact hk o hp.symm + rw [h, Function.update_of_ne this, hp] + exact ⟨fun p x hp => ne p _ hp (fun _ e => by cases e), fun p x hp => ne p _ hp (fun _ e => by cases e), + fun p x hp => ⟨x, ne p _ hp (fun _ e => by cases e), TcbKeepNA.refl x⟩, hum⟩ + +/-- One TCB replaced. -/ +theorem NtfnTcbKeepA.of_tcb {σ σ' : State Unit} {q : ObjRef} {x y : Tcb} (hq : σ.kheap q = some (.tcb x)) + (h : σ'.kheap = Function.update σ.kheap q (some (.tcb y))) (k : TcbKeepNA x y) + (hum : σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory) : NtfnTcbKeepA σ σ' := by + have ne : ∀ p k, σ.kheap p = some k → (∀ z, k ≠ .tcb z) → σ'.kheap p = some k := fun p k hp hk => by + have : p ≠ q := fun e => by rw [e, hq] at hp; injection hp with hp; exact hk x hp.symm + rw [h, Function.update_of_ne this, hp] + refine ⟨fun p x hp => ne p _ hp (fun _ e => by cases e), fun p x hp => ne p _ hp (fun _ e => by cases e), + fun p z hp => ?_, hum⟩ + by_cases e : p = q + · subst e + rw [hq] at hp; cases hp + exact ⟨y, by rw [h, Function.update_self], k⟩ + · exact ⟨z, by rw [h, Function.update_of_ne e, hp], TcbKeepNA.refl z⟩ + +/-- Only sched-context objects change; user memory is kept. -/ +def ScOnlyA (σ σ' : State Unit) : Prop := + (∀ p o, σ.kheap p = some o → (∀ sc n, o ≠ .schedContext sc n) → σ'.kheap p = some o) ∧ + σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory + +theorem ScOnlyA.refl (σ : State Unit) : ScOnlyA σ σ := ⟨fun _ _ h _ => h, rfl⟩ + +theorem ScOnlyA.trans {σ1 σ2 σ3 : State Unit} (h1 : ScOnlyA σ1 σ2) (h2 : ScOnlyA σ2 σ3) : ScOnlyA σ1 σ3 := + ⟨fun p o h ho => h2.1 p o (h1.1 p o h ho) ho, h2.2.trans h1.2⟩ + +theorem NtfnTcbKeepA.of_scOnly {σ σ' : State Unit} (h : ScOnlyA σ σ') : NtfnTcbKeepA σ σ' := + ⟨fun p x hp => h.1 p _ hp (fun _ _ e => by cases e), fun p x hp => h.1 p _ hp (fun _ _ e => by cases e), + fun p x hp => ⟨x, h.1 p _ hp (fun _ _ e => by cases e), TcbKeepNA.refl x⟩, h.2⟩ + +end Fl4ProofsA64.P3 + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P3 Fl4ProofsA64.P3.Endp + +noncomputable section + +instance : PreO ScOnlyA := ⟨fun σ => ScOnlyA.refl σ, fun h1 h2 => ScOnlyA.trans h1 h2⟩ + +/-- Preservation of `ScOnlyA` by a spec computation. -/ +abbrev TcKPresA {α : Type} (m : SMonad Unit α) : Prop := PresR ScOnlyA m + +class TcKInst {α : Type} (m : SMonad Unit α) : Prop where + pres : TcKPresA m + +instance {α β : Type} {m : SMonad Unit α} {f : α → SMonad Unit β} [hm : TcKInst m] + [hf : ∀ a, TcKInst (f a)] : TcKInst (m >>= f) := + ⟨PresR.bind hm.pres fun a => (hf a).pres⟩ + +instance {α : Type} (a : α) : TcKInst (pure a : SMonad Unit α) := ⟨PresR.pure a⟩ + +instance {α : Type} (f : State Unit → α) : TcKInst (NonDet.gets f : SMonad Unit α) := ⟨PresR.gets f⟩ + +instance : TcKInst (NonDet.get : SMonad Unit (State Unit)) := ⟨PresR.get⟩ + +instance {α : Type} (f : State Unit → Option α) : TcKInst (NonDet.getsThe f : SMonad Unit α) := + ⟨PresR.getsThe f⟩ + +instance {α : Type} (o : Option α) : TcKInst (NonDet.assertOpt o : SMonad Unit α) := ⟨PresR.assertOpt o⟩ + +instance (P : Prop) : TcKInst (NonDet.assert P : SMonad Unit Unit) := ⟨PresR.assert P⟩ + +instance {α : Type} : TcKInst (NonDet.fail : SMonad Unit α) := ⟨PresR.fail⟩ + +instance {P : Prop} {d : Decidable P} {m : SMonad Unit Unit} [h : TcKInst m] : + TcKInst (@NonDet.when (State Unit) P d m) := ⟨PresR.when h.pres⟩ + +instance {P : Prop} {d : Decidable P} {m : SMonad Unit Unit} [h : TcKInst m] : + TcKInst (@NonDet.unless (State Unit) P d m) := ⟨PresR.unless' h.pres⟩ + +instance {α : Type} {P : Prop} {d : Decidable P} {a b : SMonad Unit α} [ha : TcKInst a] [hb : TcKInst b] : + TcKInst (@ite _ P d a b) := ⟨PresR.ite' ha.pres hb.pres⟩ + +instance {β γ : Type} {f : γ → SMonad Unit β} [hf : ∀ a, TcKInst (f a)] (l : List γ) : + TcKInst (NonDet.mapM f l) := ⟨PresR.mapM (fun a => (hf a).pres) l⟩ + +instance {α : Type} {C : α → State Unit → Prop} {B : α → SMonad Unit α} [hB : ∀ a, TcKInst (B a)] (r : α) : + TcKInst (NonDet.whileLoop C B r) := ⟨PresR.whileLoop (fun a => (hB a).pres) r⟩ + +instance {α : Type} (S : Set α × Prop) : TcKInst (NonDet.selectF S : SMonad Unit α) := ⟨PresR.selectF S⟩ + +instance {α : Type} {f : α → SMonad Unit Unit} [hf : ∀ a, TcKInst (f a)] (o : Option α) : + TcKInst (NonDet.maybeM f o) := by + cases o + · exact ⟨PresR.pure _⟩ + · exact hf _ + +/-- A state modification that keeps the heap and the machine state. -/ +theorem TcKPresA.modify_fix (f : State Unit → State Unit) (hk : ∀ σ, (f σ).kheap = σ.kheap) + (hm : ∀ σ, (f σ).machineState.underlyingMemory = σ.machineState.underlyingMemory) : + TcKPresA (NonDet.modify f : SMonad Unit Unit) := + PresR.modify f fun σ => ⟨fun p o h _ => by rw [hk]; exact h, hm σ⟩ + +macro "tckpres" : tactic => `(tactic| + repeat' (first + | exact TcKInst.pres + | refine PresR.bind_pure _ _ ?_ + | refine PresR.bind ?_ (fun _ => ?_) + | refine PresR.when ?_ + | refine PresR.unless' ?_ + | refine PresR.ite' ?_ ?_ + | (refine TcKPresA.modify_fix _ (fun _ => rfl) (fun _ => rfl)) + | dsimp only + | split)) + +theorem updateSchedContext_tkA (f : SchedContext → SchedContext) (p : ObjRef) : + TcKPresA (updateSchedContext (Z := Unit) p f) := by + intro σ r hr + obtain ⟨y1, hy1, h⟩ := hr + have hy1' : y1 ∈ (NonDet.assertOpt (σ.kheap p) σ).1 := hy1 + cases hk : σ.kheap p with + | none => rw [hk] at hy1'; exact absurd hy1' (Set.notMem_empty _) + | some ko => + rw [hk] at hy1' + have e1 : y1 = (ko, σ) := hy1' + subst e1 + cases ko with + | schedContext sc n => + obtain ⟨ko', hko, hr2⟩ := setObject_memA h + rw [hr2] + refine ⟨fun q o hq ho => ?_, rfl⟩ + have : q ≠ p := fun e => by rw [e, hk] at hq; injection hq with hq; exact ho sc n hq.symm + exact (Function.update_of_ne this _ _).trans hq + | _ => exact absurd h (Set.notMem_empty _) + +instance (p : ObjRef) (f : SchedContext → SchedContext) : TcKInst (updateSchedContext (Z := Unit) p f) := + ⟨updateSchedContext_tkA f p⟩ + +/-! ## Readers and small writers -/ + +instance {α : Type} (f : Tcb → α) (t : ObjRef) : TcKInst (threadGet (Z := Unit) f t) := + ⟨by unfold threadGet; tckpres⟩ +instance (f : Tcb → Option ObjRef) (r : ObjRef) : TcKInst (getTcbObjRef (Z := Unit) f r) := + ⟨by unfold getTcbObjRef; tckpres⟩ +instance (r : ObjRef) : TcKInst (getThreadState (Z := Unit) r) := ⟨by unfold getThreadState; tckpres⟩ +instance (d : Domain) (p : Priority) : TcKInst (getTcbQueue (Z := Unit) d p) := ⟨by unfold getTcbQueue; tckpres⟩ +instance (d : Domain) (p : Priority) (q : ReadyQueue) : TcKInst (setTcbQueue (Z := Unit) d p q) := + ⟨by unfold setTcbQueue; tckpres⟩ +instance (a : ObjRef → List ObjRef → List ObjRef) (t : ObjRef) : TcKInst (tcbSchedAction (Z := Unit) a t) := + ⟨by unfold tcbSchedAction; tckpres⟩ +instance (a : SchedulerAction) : TcKInst (setSchedulerAction (Z := Unit) a) := + ⟨by unfold setSchedulerAction; tckpres⟩ +instance (t : ObjRef) : TcKInst (tcbReleaseRemove (Z := Unit) t) := ⟨by unfold tcbReleaseRemove; tckpres⟩ + +instance (p : ObjRef) : TcKInst (getObject (Z := Unit) p) := ⟨by unfold getObject; tckpres⟩ +instance (p : ObjRef) : TcKInst (getSchedContext (Z := Unit) p) := ⟨by unfold getSchedContext; tckpres⟩ +instance (f : SchedContext → Option ObjRef) (p : ObjRef) : TcKInst (getScObjRef (Z := Unit) f p) := + ⟨by unfold getScObjRef; tckpres⟩ +instance (p : ObjRef) : TcKInst (getRefills (Z := Unit) p) := ⟨by unfold getRefills; tckpres⟩ +instance (p : ObjRef) : TcKInst (getRefillHead (Z := Unit) p) := ⟨by unfold getRefillHead; tckpres⟩ +instance (p : ObjRef) : TcKInst (getRefillTail (Z := Unit) p) := ⟨by unfold getRefillTail; tckpres⟩ +instance (p : ObjRef) : TcKInst (getScRefillReady (Z := Unit) p) := ⟨by unfold getScRefillReady; tckpres⟩ +instance (p : ObjRef) (u : Ticks) : TcKInst (getScRefillSufficient (Z := Unit) p u) := + ⟨by unfold getScRefillSufficient; tckpres⟩ +instance (p : ObjRef) (u : Ticks) : TcKInst (fl4GetScRefillSufficient (Z := Unit) p u) := + ⟨by unfold fl4GetScRefillSufficient; tckpres⟩ +instance (p : ObjRef) : TcKInst (getScActive (Z := Unit) p) := ⟨by unfold getScActive; tckpres⟩ +instance (p : ObjRef) : TcKInst (isRoundRobin (Z := Unit) p) := ⟨by unfold isRoundRobin; tckpres⟩ + +/-! ## Scheduler queues -/ + +instance : TcKInst (rescheduleRequired (Z := Unit)) := ⟨by unfold rescheduleRequired; tckpres⟩ +instance (t : ObjRef) : TcKInst (possibleSwitchTo (Z := Unit) t) := ⟨by unfold possibleSwitchTo; tckpres⟩ + + +/-! ### Refills -/ + +instance (p : ObjRef) (f : Refill → Refill) : TcKInst (updateRefillHd (Z := Unit) p f) := + ⟨by unfold updateRefillHd; tckpres⟩ +instance (p : ObjRef) (f : Refill → Refill) : TcKInst (updateRefillTl (Z := Unit) p f) := + ⟨by unfold updateRefillTl; tckpres⟩ +instance (p : ObjRef) (r : Refill) : TcKInst (refillAddTail (Z := Unit) p r) := ⟨by unfold refillAddTail; tckpres⟩ +instance (p : ObjRef) : TcKInst (refillPopHead (Z := Unit) p) := ⟨by unfold refillPopHead; tckpres⟩ +instance (p : ObjRef) : TcKInst (refillSize (Z := Unit) p) := ⟨by unfold refillSize; tckpres⟩ +instance (p : ObjRef) : TcKInst (refillSingle (Z := Unit) p) := ⟨by unfold refillSingle; tckpres⟩ +instance (p : ObjRef) : TcKInst (refillFull (Z := Unit) p) := ⟨by unfold refillFull; tckpres⟩ +instance (p : ObjRef) (r : Refill) : TcKInst (scheduleUsed (Z := Unit) p r) := ⟨by unfold scheduleUsed; tckpres⟩ +instance (p : ObjRef) : TcKInst (mergeOverlappingRefills (Z := Unit) p) := ⟨by unfold mergeOverlappingRefills; tckpres⟩ +instance (p : ObjRef) : TcKInst (refillHeadOverlappingLoop (Z := Unit) p) := + ⟨by unfold refillHeadOverlappingLoop; tckpres⟩ +instance (p : ObjRef) : TcKInst (refillUnblockCheck (Z := Unit) p) := ⟨by unfold refillUnblockCheck; tckpres⟩ +instance (o : Option ObjRef) (a b : Option Bool) : TcKInst (ifCondRefillUnblockCheck (Z := Unit) o a b) := + ⟨by unfold ifCondRefillUnblockCheck; tckpres⟩ +instance (p : ObjRef) : TcKInst (mergeNonoverlappingHeadRefill (Z := Unit) p) := + ⟨by unfold mergeNonoverlappingHeadRefill; tckpres⟩ +instance (m : Ticks) (p : ObjRef) : TcKInst (headInsufficientLoopWith (Z := Unit) m p) := + ⟨by unfold headInsufficientLoopWith; tckpres⟩ +instance (p : ObjRef) (u : Ticks) : TcKInst (chargeEntireHeadRefill (Z := Unit) p u) := + ⟨by unfold chargeEntireHeadRefill; tckpres⟩ +instance (l : Ticks) (p : ObjRef) (u : Ticks) : TcKInst (handleOverrunWith (Z := Unit) l p u) := + ⟨by unfold handleOverrunWith; tckpres⟩ +instance (l m u : Ticks) : TcKInst (refillBudgetCheckWith (Z := Unit) l m u) := + ⟨by unfold refillBudgetCheckWith; tckpres⟩ +instance (u : Ticks) : TcKInst (fl4RefillBudgetCheck (Z := Unit) u) := ⟨by unfold fl4RefillBudgetCheck; tckpres⟩ +instance (u : Ticks) : TcKInst (refillBudgetCheckRoundRobin (Z := Unit) u) := + ⟨by unfold refillBudgetCheckRoundRobin; tckpres⟩ + + +instance (t : ObjRef) : TcKInst (setThreadStateAct (Z := Unit) t) := ⟨by unfold setThreadStateAct; tckpres⟩ +instance : TcKInst (rescheduleRequired (Z := Unit)) := ⟨by unfold rescheduleRequired; tckpres⟩ +instance (t : ObjRef) : TcKInst (possibleSwitchTo (Z := Unit) t) := ⟨by unfold possibleSwitchTo; tckpres⟩ + +/-- **`rescheduleRequired` changes only sched contexts.** -/ +theorem rescheduleRequired_tkA : TcKPresA (rescheduleRequired (Z := Unit)) := TcKInst.pres + +/-- **The spec's restart tail changes only sched contexts.** -/ +theorem specRestartTail_tkA (t : ObjRef) : TcKPresA (specRestartTail t) := by + unfold specRestartTail; tckpres + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/SlotCoverageA.lean b/proofs/Fl4ProofsA64/P3/SlotCoverageA.lean new file mode 100644 index 000000000..624d330c5 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/SlotCoverageA.lean @@ -0,0 +1,104 @@ +/- +P3 (AArch64): converse coverage for successful abstract CSpace reads. + +[J] The frozen typed-heap correspondence represents every well-formed +abstract CNode and every abstract TCB. StateRelA supplies the forward +direction. CDT edges already name typed slots by CdtRelA.cdt_slot. +-/ +import Fl4ProofsA64.P3.SpecFrA +import Fl4ProofsA64.P3.LookupA +import Fl4ProofsA64.P3.EpBufA +import Fl4ProofsA64.P3.ScSpecA + +open Aeneas Aeneas.Std + +namespace Fl4ProofsA64.P3 +open Fl4Spec Fl4SpecA64 Fl4ProofsA64.P1 + +/-- The converse of the object coverage in StateRelA, for slot-bearing +objects. Corresponds to the x86 finality proof's KTyped. -/ +structure SlotHeapCovA (th : TypedHeapA) (σ : State Unit) : Prop where + cnode : ∀ q r cs, σ.kheap q = some (.cnode r cs) → wellFormedCnodeN r cs → + th (fl4Ptr q) = some (.cnode r) + tcb : ∀ q x, σ.kheap q = some (.tcb x) → th (fl4Ptr q) = some .tcb + +private theorem tcbCnodeMap_indexA {x : Tcb} {idx : CnodeIndex} {c : Cap} + (h : tcbCnodeMap x idx = some c) : ∃ i < tcbSlotsA, idx = tcbCnodeIndex i := by + simp only [tcbCnodeMap, Function.update_apply] at h + split_ifs at h <;> first + | contradiction + | exact ⟨tcbHighVSpaceSlot, by decide, by assumption⟩ + | exact ⟨tcbStage2Slot, by decide, by assumption⟩ + | exact ⟨4, by decide, by assumption⟩ + | exact ⟨3, by decide, by assumption⟩ + | exact ⟨2, by decide, by assumption⟩ + | exact ⟨1, by decide, by assumption⟩ + | exact ⟨0, by decide, by assumption⟩ + +/-- A successful abstract read names a concrete slot and returns its +related capability. The conclusion includes the exact abstract slot, so +callers can use the same slot for its address and its CDT entries. -/ +theorem SlotHeapCovA.getCap {th : TypedHeapA} {mem : U64 → U64} {σ : State Unit} + (cov : SlotHeapCovA th σ) (rel : StateRelA th mem σ) {q : CslotPtr} {c : Cap} + (hget : (c, σ) ∈ (Fl4SpecA64.getCap (Z := Unit) q σ).1) : + AbsSlotA th q ∧ CapRelA (capAtA mem (cslotAddrA q)) c := by + obtain ⟨-, o, ho, hc⟩ := getCap_memA hget + obtain ⟨p, idx⟩ := q + cases o with + | cnode radix cs => + have hwf : wellFormedCnodeN radix cs := by + by_contra hn + simp only [Fl4SpecA64.getCap] at hget + rw [Sched.nd_bind_oneSc (Sched.getObject_eqSc ho)] at hget + simp [Sched.nd_bind_applySc, NonDet.assert, hn] at hget + have hl : idx.length = radix := by + have hm : idx ∈ dom cs := by + change cs idx ≠ none + rw [show cs idx = some c from hc] + exact Option.some_ne_none _ + rwa [hwf] at hm + have ht := cov.cnode p radix cs ho hwf + have hp : p = specPtr (fl4Ptr p) := (specPtr_fl4Ptr p).symm + refine ⟨⟨fl4Ptr p, hp, Or.inl (hl.symm ▸ ht)⟩, ?_⟩ + obtain ⟨cs', hk, _, hs⟩ := rel.cnode _ _ ht + rw [← hp, ho] at hk + obtain rfl := (KernelObject.cnode.inj (Option.some.inj hk)).2 + obtain ⟨ac, he, hr⟩ := hs idx hl + rw [show cs idx = some c from hc] at he + change CapRelA (capAtA mem (slotAddrA p (ofBl idx))) c + rw [hp, Option.some.inj he] + exact hr + | tcb x => + obtain ⟨i, hi, hij⟩ := tcbCnodeMap_indexA (show tcbCnodeMap x idx = some c from hc) + have ht := cov.tcb p x ho + have hp : p = specPtr (fl4Ptr p) := (specPtr_fl4Ptr p).symm + refine ⟨⟨fl4Ptr p, hp, Or.inr ⟨ht, i, hi, hij⟩⟩, ?_⟩ + obtain ⟨y, hk, hs⟩ := rel.tcb _ ht + rw [← hp, ho] at hk + obtain rfl := KernelObject.tcb.inj (Option.some.inj hk) + obtain ⟨ac, he, hr⟩ := hs i hi + rw [← hij, show tcbCnodeMap x idx = some c from hc] at he + change CapRelA (capAtA mem (slotAddrA p (ofBl idx))) c + rw [hp, hij, ofBl_tcbIdx i (by unfold tcbSlotsA at hi; omega), Option.some.inj he] + exact hr + | _ => cases hc + +section +open fl4_core_a64 +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- Successful abstract reads also select the concrete address, CDT +parent and original-cap bit from the same ghost state. -/ +theorem SlotHeapCovA.getCap_cdt + {th : TypedHeapA} {s : S} {σ : State Unit} {G : MdbGhostA} + (cov : SlotHeapCovA th σ) (rel : StateRelA th (L.mem s) σ) + (cdt : CdtRelA st th s σ G) (place : PlaceA th (L.ksb s)) + {q : CslotPtr} {c : Cap} (hget : (c, σ) ∈ (Fl4SpecA64.getCap (Z := Unit) q σ).1) : + IsSlotA th (cslotAddrA q) ∧ CapRelA (capAtA (L.mem s) (cslotAddrA q)) c ∧ + (σ.cdt q).map cslotAddrA = G.par (cslotAddrA q) ∧ + σ.isOriginalCap q = revA (L.mem s) (cslotAddrA q) := by + obtain ⟨hq, hc⟩ := cov.getCap rel hget + exact ⟨hq.isSlot place, hc, cdt.cdt q hq, cdt.orig q hq⟩ + +end +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/SlotCoverageBootA.lean b/proofs/Fl4ProofsA64/P3/SlotCoverageBootA.lean new file mode 100644 index 000000000..c998856d7 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/SlotCoverageBootA.lean @@ -0,0 +1,77 @@ +/- +P3 (AArch64): boot producers for converse CSpace coverage. +The proof uses only the slot-bearing objects of the two boot heaps. +-/ +import Fl4ProofsA64.P3.SlotCoverageA +import Fl4ProofsA64.P3.JBootW1SA + +open Aeneas Aeneas.Std + +namespace Fl4ProofsA64.P3 +open Fl4SpecA64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST.BootWit + +private theorem bootSlotHeapCovA_at {kb ib now mt : U64} {nc : Nat} (hL : BootLayA ib nc) + (hK : BootKsA kb ib nc) {p : ObjRef} {o : KernelObject} + (h : bootKA kb ib nc now mt p = some o) : + (∀ r cs, o = .cnode r cs → bootThA kb ib nc (fl4Ptr p) = some (.cnode r)) ∧ + (∀ x, o = .tcb x → bootThA kb ib nc (fl4Ptr p) = some .tcb) := by + classical + unfold bootKA at h + split_ifs at h with h0 ht hsc hirq + · cases h + exact ⟨by intros; contradiction, by intros; contradiction⟩ + · obtain ⟨c, hc, rfl⟩ := ht + cases h + refine ⟨by intros; contradiction, fun _ _ => ?_⟩ + rw [fl4Ptr_specPtr, bootThA, if_pos ⟨c, hc, rfl⟩] + · cases h + exact ⟨by intros; contradiction, by intros; contradiction⟩ + · obtain ⟨q, hq, rfl⟩ := hirq + cases h + refine ⟨fun _ _ he => ?_, by intros; contradiction⟩ + cases he + rw [fl4Ptr_specPtr, bootThA, if_neg (irqN_notT hL hK hq), if_pos hq] + +theorem boot_slotHeapCovA {kb ib now mt : U64} {nc : Nat} (hL : BootLayA ib nc) (hK : BootKsA kb ib nc) : + SlotHeapCovA (bootThA kb ib nc) (bootSA kb ib nc now mt) where + cnode _ r cs hk _ := (bootSlotHeapCovA_at hL hK hk).1 r cs rfl + tcb _ x hk := (bootSlotHeapCovA_at hL hK hk).2 x rfl + +private theorem w1SlotHeapCovA_at {kb ib now mt sl : U64} {nc : Nat} (hL : BootLayA ib nc) + (hK : BootKsA kb ib nc) {p : ObjRef} {o : KernelObject} + (h : w1KA kb ib nc now mt sl p = some o) : + (∀ r cs, o = .cnode r cs → w1ThA kb ib nc (fl4Ptr p) = some (.cnode r)) ∧ + (∀ x, o = .tcb x → w1ThA kb ib nc (fl4Ptr p) = some .tcb) := by + classical + unfold w1KA at h + split_ifs at h with hrr hrt hsc hpool htable hframe + · cases h + refine ⟨fun _ _ he => ?_, by intros; contradiction⟩ + cases he + have hp : fl4Ptr p = bwv RRb := UScalar.eq_of_val_eq (hrr.trans vRR.symm) + rw [hp] + exact w1ThA_rr + · cases h + refine ⟨by intros; contradiction, fun _ _ => ?_⟩ + have hp : fl4Ptr p = bwv RTb := UScalar.eq_of_val_eq (hrt.trans vRT.symm) + rw [hp] + exact w1ThA_rt + · cases h + exact ⟨by intros; contradiction, by intros; contradiction⟩ + · cases h + exact ⟨by intros; contradiction, by intros; contradiction⟩ + · cases h + exact ⟨by intros; contradiction, by intros; contradiction⟩ + · cases h + exact ⟨by intros; contradiction, by intros; contradiction⟩ + · rw [w1ThA, if_neg (fun e => hrr (congrArg UScalar.val e |>.trans vRR)), + if_neg (fun e => hrt (congrArg UScalar.val e |>.trans vRT)), + if_neg hframe] + exact bootSlotHeapCovA_at hL hK h + +theorem w1_slotHeapCovA {kb ib now mt sl : U64} {nc : Nat} (hL : BootLayA ib nc) (hK : BootKsA kb ib nc) : + SlotHeapCovA (w1ThA kb ib nc) (w1SA kb ib nc now mt sl) where + cnode _ r cs hk _ := (w1SlotHeapCovA_at hL hK hk).1 r cs rfl + tcb _ x hk := (w1SlotHeapCovA_at hL hK hk).2 x rfl + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/SlotJA.lean b/proofs/Fl4ProofsA64/P3/SlotJA.lean new file mode 100644 index 000000000..4ffb6672e --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/SlotJA.lean @@ -0,0 +1,236 @@ +/- +P3 (aarch64, A64-P3 S3 gen 5, ruling 11:09Z step 1): **slot-word steps at entry level and the full `JAtZAX` across +`empty_slot`** — the A64 counterpart of x86's `EmptyFrame` / `DelEmpty` frame (the scheduler, IRQ and VM parts of the +relation carried over a CSpace leaf). + +* **`SlotApA Z H OS kb`** (gen 5 a [J] premise here; gen 7, ruling 12:26Z item 1: moved to `P3/EntryA` as the field + `JEntA.ap`; state-free placement facts of A64 P1's deletion tops, `MdbLiveK.DelS`): `OS` slots + are off every SC (`ScApart`, `DelS.sca`) and off the IRQ-state words (`IrqStApart`, `DelS.irqSt`); the VCPUs of + `Z.V` are aligned and their `TCB` / `RUN_CALLER` words lie in no `OS` slot and in no TCB's slot words (`VcOK.os` and + `VcTy.w` with `Ω` outside the TCBs, for A64 P1's VCPU set; `Z.V` is not tied to it yet, so it is stated for `Z.V`). +* **`footN_offA`**: a word of an `OS` slot or a TCB's slot area (A64 P1's `W7.CapW T {InOS OS} ∅`, the write set of + its slot primitives) is no `FootNA` word: node words and TCB fields by `capw_ne_node` / `capw_ne_hi` (via + `inOS_capw`: an aligned `OS` word is an `OSOK` cap-slot word), SC words by `Ω` (`HeapOK`) and `ScApart`, the IRQ + state words by `ksOut` and `IrqStApart`, the global `CUR_DOMAIN` / `DOMAIN_TIME` by `ksOut` and `A64Layout.kslo`, + the VCPU words by `SlotApA.vc`. +* **`emptySlot_osA`**: `empty_slot` with a `Null` cleanup writes only such words (A64 P1's `cdt.empty_slot.os` with + the links taken from the tree instead of `SlotTy`). +* **`empty_slot_jA`** (x86 `empty_slot_refines` + `JAtZB` frame): at `JEntA`, FL4's `empty_slot` of a non-null (gen 8: CNode or TCB) + slot with a `Null` cleanup keeps the arch state and ends related by the **whole** `JAtZAX` (at the ghost + `remGA Z.G x`) to an outcome of the spec's `empty_slot slot NullCap`, with A64 P1's tree facts at the new ghost. + +Named premise: none (gen 7: `SlotApA` is `JEntA.ap`; the leaves take no `hA`). +-/ +import Fl4ProofsA64.P3.EmptyA +import Fl4ProofsA64.P3.EntryA +import Fl4ProofsA64.P1.QueueVsWs +import Fl4ProofsA64.P1.QueueSlotTyEp +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- An aligned word of an `OS` slot is an `OSOK` cap-slot word. -/ +theorem inOS_capw {T Ω Sg OS : Set U64} (hO : OSOK T Ω Sg OS) {b : U64} (hb : Aligned b) (h : InOS OS b) : + W7.CapW T Ω Sg b := by + obtain ⟨y, hy, h1, h2⟩ := h + have hf := hO.fit y hy + have hya : y.val % 8 = 0 := hO.al0 hy + have hb8 : b.val % 8 = 0 := hb + have hbl : b.val < 2 ^ 64 := b.bv.isLt + let o : U64 := ⟨BitVec.ofNat 64 (b.val - y.val)⟩ + have ho : o.val = b.val - y.val := by + show (BitVec.ofNat 64 (b.val - y.val)).toNat = _ + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + have e : wa y o = b := UScalar.eq_of_val_eq (by rw [wv hf (by omega)]; omega) + rw [← e]; exact (hO.slot y hy).2 o (by omega) (by omega) + +/-- A TCB's slot word is no word of the kernel-state block below the nodes (`SchedQueuesInvX.ksOut`). -/ +theorem tlow_ksA {T Cs Ω X : Set U64} {H : Heap} {Q : U64 → U64 → List U64} {R : U64 → List U64} {s : S} + (q : SchedQueuesInvX st T Cs Ω H X s Q R) {b : U64} (hl : W7.TLow T b) (h1 : (L.ksb s).val ≤ b.val) + (h2 : b.val < (L.ksb s).val + 223488) : False := by + obtain ⟨t, ht, K, hK, rfl⟩ := hl + have e := field_val (q.tcbs t ht) (K := K) (by omega) + have := q.ksOut t ht + omega + +/-- An SC word below `REFILL` is no cap-slot word of `OS` (`Ω` lies outside the TCBs; `ScApart`). -/ +theorem sc_offA {T Cs X OS : Set U64} {H : Heap} {Q : U64 → U64 → List U64} {R : U64 → List U64} {s : S} + (q : SchedQueuesInvX st T Cs H.omega H X s Q R) (hH : HeapOK H) (hA : ScApart H.scs H.scBits OS) {c : U64} + {z : Nat} (hz : H c = some (.sc z)) {K : U64} (hK : K.val < 128) (hal : Aligned (wa c K)) : + ¬ W7.CapW T {b | InOS OS b} ∅ (wa c K) := by + have hf := hH.fits c _ hz + have h128 : 2 ^ 7 ≤ 2 ^ z := Nat.pow_le_pow_right (by norm_num) (hH.scmin c z hz) + simp only [HKind.bits] at hf + have hv : (wa c K).val = c.val + K.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + rintro (hl | ⟨⟨y, hy, h1, h2⟩, -⟩) + · refine W7.tlow_ne_out q.tcbs (q.omega _ ⟨hal, c, _, hz, Or.inr (Or.inl ⟨z, rfl⟩), ?_⟩) hl + unfold InObj; simp only [HKind.bits] + exact ⟨by omega, by omega⟩ + · have e : H.scBits c = z := by unfold Heap.scBits; rw [hz] + have := hA y hy c ⟨z, hz⟩ + rw [e] at this + omega + +/-- (gen 3's offsets) IRQ `i`'s state-word offset by value. -/ +theorem irqOffA_val {i : U64} (hi : i.val < 5376) : (irqOffA i).val = 224 + 8 * i.val := by + unfold irqOffA + simp [core.num.U64.wrapping_add_val_eq, core.num.U64.wrapping_mul_val_eq, U64.size_def, U64.numBits] + omega + +variable {A : Type} + +/-- **A slot primitive's word is no `FootNA` word** (module docstring). -/ +theorem footN_offA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {s : S} {a : A} {σ : State Unit} {m : McState} + (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) : + ∀ b, FootNA (L := L) Z s b → ¬ W7.CapW H.tcbs {b | InOS OS b} ∅ b := by + have hal := FootNA.aligned E.j.1 (bxi_nodes E.sys.ent.bxi) E.scAl E.ap.vcAl + obtain ⟨⟨Q, R, q⟩, -⟩ := E.sys.ent.bxi + have hO := E.sys.ent.os + have hH := E.sys.ent.heap + have hkf : (L.ksb s).val + 788736 ≤ 2 ^ 64 := E.j.1.ksFit + have tT : ∀ t, t ∈ Z.T → t ∈ H.tcbs := fun t ht => by rw [E.tcbs]; exact ht + have sH : ∀ c, c ∈ Z.C → ∃ z, H c = some (.sc z) := fun c hc => by rw [← E.scs] at hc; exact hc + have cw : ∀ b, Aligned b → W7.CapW H.tcbs {b | InOS OS b} ∅ b → W7.CapW H.tcbs H.omega H.frame b := by + rintro b hb (hl | ⟨hi, -⟩) + · exact Or.inl hl + · exact inOS_capw hO hb hi + intro b hb hw + have hc := cw b (hal b hb) hw + cases hb with + | node c w hc' hw' => + rcases hw' with rfl | rfl | rfl | rfl <;> + exact capw_ne_node q hc hc' (by simp [layout.node.CUR_THREAD, layout.node.IDLE_THREAD, layout.node.CUR_SC, + layout.node.IDLE_SC]) rfl + | cur w hw' => + rcases hw' with rfl | rfl <;> + exact capw_ne_node q hc q.cur (by simp [layout.node.SCHED_ACTION, layout.node.CUR_TIME]) rfl + | tcb t w ht hw' => + rcases hw' with rfl | rfl | rfl | rfl | rfl + · exact capw_ne_hi q hc (tT t ht) (K := 3328#u64) (by simp) (by simp) (stA_eq t) + · exact capw_ne_hi q hc (tT t ht) (K := 3464#u64) (by simp) (by simp) rfl + · exact capw_ne_hi q hc (tT t ht) (K := 3520#u64) (by simp) (by simp) rfl + · exact capw_ne_hi q hc (tT t ht) (K := 3664#u64) (by simp) (by simp) rfl + · exact capw_ne_hi q hc (tT t ht) (K := 3672#u64) (by simp) (by simp) rfl + | sc c hc' => + obtain ⟨z, hz⟩ := sH c hc' + exact sc_offA q hH E.ap.sca hz (by simp) (hal _ (.sc _ hc')) hw + | vcpu v w hv hw' => + obtain ⟨h1, h2⟩ := E.ap.vc v hv _ hw' + rcases hw with hl | ⟨hi, -⟩ + · exact h2 hl + · exact h1 hi + | irq i hi => + have hv := irqWordA_val hkf hi + rcases hw with hl | ⟨⟨y, hy, h1, h2⟩, -⟩ + · exact tlow_ksA q hl (by omega) (by omega) + · have ho := irqOffA_val hi + exact E.ap.irqSt (irqOffA i) (by omega) (by omega) y hy ⟨h1, h2⟩ + | cur2 w hw' => + rcases hw' with rfl | rfl <;> + exact capw_ne_node q hc q.cur (by simp [layout.node.CONSUMED, layout.node.REPROGRAM]) rfl + | glob w hw' => + have k8 : (wa (L.ksb s) 8#u64).val = (L.ksb s).val + 8 := by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by simp; omega) + have k16 : (wa (L.ksb s) 16#u64).val = (L.ksb s).val + 16 := by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by simp; omega) + rcases hw' with rfl | rfl <;> rcases hw with hl | ⟨⟨y, hy, h1, h2⟩, -⟩ + · exact tlow_ksA q hl (by omega) (by omega) + · exact E.sys.sysl.ly.kslo 8#u64 (by simp) y hy ⟨h1, h2⟩ + · exact tlow_ksA q hl (by omega) (by omega) + · exact E.sys.sysl.ly.kslo 16#u64 (by simp) y hy ⟨h1, h2⟩ + | tcb2 t w ht hw' => + rcases hw' with rfl | rfl + · exact capw_ne_hi q hc (tT t ht) (K := 3448#u64) (by simp) (by simp) rfl + · exact capw_ne_hi q hc (tT t ht) (K := 3432#u64) (by simp) (by simp) rfl + | sc2 c hc' => + obtain ⟨z, hz⟩ := sH c hc' + exact sc_offA q hH E.ap.sca hz (by simp) (hal _ (.sc2 _ hc')) hw + | vs w hw' => + obtain ⟨h1, h2⟩ := E.ap.vs _ hw' + rcases hw with hl | ⟨hi, -⟩ + · obtain ⟨t, ht, K, hK, rfl⟩ := hl + have e := field_val (q.tcbs t ht) (K := K) (by omega) + rcases h2 t ht with h | h <;> omega + · exact h1 hi + | ext w hw' => + obtain ⟨h1, h2⟩ := E.ap.ext _ hw' + rcases hw with hl | ⟨hi, -⟩ + · obtain ⟨t, ht, K, hK, rfl⟩ := hl + have e := field_val (q.tcbs t ht) (K := K) (by omega) + rcases h2 t ht with h | h <;> omega + · exact h1 hi + +set_option maxHeartbeats 1000000 in +/-- **`empty_slot`'s write set with a `Null` cleanup** (module docstring; A64 P1's `cdt.empty_slot.os`, the slot's +`PREV` / `NEXT` links in `OS` from the tree instead of `SlotTy`). -/ +theorem emptySlot_osA {ar : machine.Arch A} {T Ω Sg OS : Set U64} (hO : OSOK T Ω Sg OS) {s : S} (a : A) {x : U64} + (hx : x ∈ OS) (hpP : L.mem s (wa x 16#u64) ≠ 0#u64 → L.mem s (wa x 16#u64) ∈ OS) + (hnN : NxA st s x ≠ 0#u64 → NxA st s x ∈ OS) : + cdt.empty_slot st ar s a x .Null ⦃ r => WritesIn st s r.1 (W7.CapW T {b | InOS OS b} ∅) ⦄ := by + have hS : P0.StoreTotal st := L.total + unfold cdt.empty_slot + rw [capA_eq, bind_ok] + by_cases hn : capA st s x = .Null + · rw [hn, is_null_t, bind_ok, if_pos rfl]; exact (WP.spec_ok _).2 (WritesIn.refl _ _) + rw [is_null_f hn, bind_ok, if_neg (by simp), slot_prev_eq, bind_ok, next_eq, bind_ok] + refine WP.spec_bind (P0.obj.Slot.first_badged.p0 st x s hS) fun fb _ => ?_ + simp only [obj.Slot.is_null, bind_ok] + refine WP.spec_bind (Pₘ := fun s1 => WritesIn st s s1 (W7.CapW T {b | InOS OS b} ∅)) ?_ fun s1 h1 => ?_ + · split + · exact (WP.spec_ok _).2 (WritesIn.refl _ _) + · rename_i hp0 + exact W7.obj.Slot.set_next.capw (osw hO (hpP (by simpa using hp0))) + refine WP.spec_bind (Pₘ := fun s2 => WritesIn st s s2 (W7.CapW T {b | InOS OS b} ∅)) ?_ fun s2 h2 => ?_ + · split + · exact (WP.spec_ok _).2 h1 + · rename_i hn0 + have hnO := hnN (by simpa using hn0) + refine WP.spec_bind (W7.obj.Slot.set_prev.capw (osw hO hnO)) fun s3 w3 => ?_ + refine WP.spec_bind (P0.obj.Slot.first_badged.p0 st _ s3 hS) fun nfb _ => ?_ + split <;> exact WP.spec_mono (W7.obj.Slot.set_first_badged.capw (osw hO hnO)) fun s' w => + (h1.trans1 w3).trans1 w + refine WP.spec_bind (W7.obj.Slot.clear.capw (osw hO hx)) fun s3 w3 => ?_ + rw [pcd_null]; exact (WP.spec_ok _).2 (h2.trans1 w3) + +/-- **FL4's `cdt::empty_slot` refines l4v `empty_slot` with the whole relation** (module docstring; ruling 11:09Z +step 1). -/ +theorem empty_slot_jA {AR : A → HwRootA → Prop} {ar : machine.Arch A} {Z : XSetsA} {H : Heap} + {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} {ia ie : Nat} {s : S} {a : A} {σ : State Unit} {m : McState} + (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + (hT : TreeA st OS Z.G.chains Z.G.par s) (hRO : RngOneA64 st Z.G.chains s) (hK : KindA st OS s) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) + {xs : CslotPtr} (hxs : AbsSlotA Z.th xs) (hcn : capAtA (L.mem s) (cslotAddrA xs) ≠ .Null) : + cdt.empty_slot st ar s a (cslotAddrA xs) .Null ⦃ q => q.2 = a ∧ + ∃ σ', ((), σ') ∈ (emptySlot (Z := Unit) xs .nullCap σ).1 ∧ + JAtZAX (L := L) AR { Z with G := remGA Z.G (cslotAddrA xs) } q.1 a σ' m ∧ + TreeA st OS (remGA Z.G (cslotAddrA xs)).chains (remGA Z.G (cslotAddrA xs)).par q.1 ∧ + RngOneA64 st (remGA Z.G (cslotAddrA xs)).chains q.1 ∧ KindA st OS q.1 ⦄ := by + have hO := E.sys.ent.os + have hxS : IsSlotA Z.th (cslotAddrA xs) := AbsSlotA.isSlot E.j.1.place hxs + have hxG : cslotAddrA xs ∈ Z.G.chains.flatten := (hT.lk.members _).2 ⟨hsub _ hxS, hcn⟩ + have F := hT.lk.lkF hO.nz + have inOS : ∀ y ∈ Z.G.chains.flatten, y ∈ OS := fun y hy => ((hT.lk.members y).1 hy).1 + refine WP.spec_mono (WP.spec_and (empty_slot_refinesA s a σ Z.G E.j.1.st E.j.1.cdt E.j.1.place hO hT hRO hK + hsub hnul hxs hcn) (emptySlot_osA hO a (hsub _ hxS) (fun h => inOS _ (F.pvm _ hxG h)) + (fun h => inOS _ (F.nxm _ hxG h)))) ?_ + rintro q ⟨⟨har, σ', hm, hst, hcdt, hT', hRO', hK', hSS, hcs⟩, hw⟩ + refine ⟨har, σ', hm, E.j.of_slotWrites hw (fun b hw hb => footN_offA E b hb hw) + (FootNA.aligned E.j.1 (bxi_nodes E.sys.ent.bxi) E.scAl E.ap.vcAl) _ hSS hst hcdt fun y hy => ?_, hT', hRO', hK'⟩ + rcases hcs y hy with e | e <;> rw [e] + · exact CapObjOkA.null Z + · exact E.j.capObj hy + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/SpecDetA.lean b/proofs/Fl4ProofsA64/P3/SpecDetA.lean new file mode 100644 index 000000000..dbcfc1480 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/SpecDetA.lean @@ -0,0 +1,237 @@ +/- +P3 (aarch64, A64-P3-IPC gen 4): **determinism of spec computations** (`Det`; x86 `P3/SpecDet`, its generic part +verbatim on the shared `Fl4Spec.NonDet` monad): at every state, at most one outcome. The combinators (`det_bind`, +`det_when`, `det_maybeM`, `det_mapMx`, `det_whileLoop`, …) and the `det` tactic, then the `Fl4SpecA64` functions of the +endpoint and notification cancellation paths (`restart_thread_if_no_fault`, `remove_and_restart_badged_thread`, +`remove_and_restart_ep_queued_thread` (with `reply_unlink_tcb`), `remove_and_restart_ntfn_queued_thread`, +`reschedule_required`, `possible_switch_to`, the sporadic unblock check, …). + +Used to turn the non-failure of the one outcome FL4's run is related to into the non-failure of a whole `mapM_x` loop +(`detAt_bind_fail`: with one outcome, a bind fails only where that outcome's continuation fails). No hypothesis. +-/ +import Fl4SpecA64 + +namespace Fl4ProofsA64.P3.Det +open Fl4Spec + +/-- At every state, at most one outcome (a structure, so that `intro` does not unfold it). -/ +structure Det {σ α : Type} (m : NonDet σ α) : Prop where + det : ∀ s x y, x ∈ (m s).1 → y ∈ (m s).1 → x = y + +/-- At the state `s`, at most one outcome. -/ +def DetAt {σ α : Type} (m : NonDet σ α) (s : σ) : Prop := ∀ x y, x ∈ (m s).1 → y ∈ (m s).1 → x = y + +section +variable {σ α β : Type} + +theorem Det.at {m : NonDet σ α} (h : Det m) (s : σ) : DetAt m s := h.det s + +theorem det_pure (a : α) : Det (NonDet.pure a : NonDet σ α) := by + constructor; intro s x y hx hy; rw [Set.mem_singleton_iff.1 hx, Set.mem_singleton_iff.1 hy] + +theorem det_pure' (a : α) : Det (pure a : NonDet σ α) := det_pure a + +theorem det_get : Det (NonDet.get : NonDet σ σ) := by + constructor; intro s x y hx hy; rw [Set.mem_singleton_iff.1 hx, Set.mem_singleton_iff.1 hy] + +theorem det_put (t : σ) : Det (NonDet.put t : NonDet σ Unit) := by + constructor; intro s x y hx hy; rw [Set.mem_singleton_iff.1 hx, Set.mem_singleton_iff.1 hy] + +theorem det_gets (f : σ → α) : Det (NonDet.gets f : NonDet σ α) := by + constructor; intro s x y hx hy; rw [Set.mem_singleton_iff.1 hx, Set.mem_singleton_iff.1 hy] + +theorem det_modify (f : σ → σ) : Det (NonDet.modify f : NonDet σ Unit) := by + constructor; intro s x y hx hy; rw [Set.mem_singleton_iff.1 hx, Set.mem_singleton_iff.1 hy] + +theorem det_fail : Det (NonDet.fail : NonDet σ α) := by + constructor; intro s x y hx; cases hx + +theorem det_assert (P : Prop) : Det (NonDet.assert P : NonDet σ Unit) := by + constructor; intro s x y hx hy; rw [hx.2, hy.2] + +theorem det_assertOpt (o : Option α) : Det (NonDet.assertOpt o : NonDet σ α) := by + cases o + · exact det_fail + · exact det_pure _ + +theorem det_getsThe (f : σ → Option α) : Det (NonDet.getsThe f : NonDet σ α) := ⟨fun s => + (det_assertOpt (f s)).det s⟩ + +theorem det_stateAssert (P : σ → Prop) : Det (NonDet.stateAssert P) := ⟨fun s => (det_assert (P s)).det s⟩ + +theorem det_bind {m : NonDet σ α} {f : α → NonDet σ β} (hm : Det m) (hf : ∀ a, Det (f a)) : + Det (m >>= f) := by + constructor + intro s x y hx hy + obtain ⟨u, hu, hxu⟩ := hx + obtain ⟨v, hv, hyv⟩ := hy + have e := hm.det s u v hu hv + subst e + exact (hf u.1).det u.2 x y hxu hyv + +theorem det_bind' {m : NonDet σ α} {f : α → NonDet σ β} (hm : Det m) (hf : ∀ a, Det (f a)) : + Det (NonDet.bind m f) := det_bind hm hf + +theorem det_when (P : Prop) {_ : Decidable P} {m : NonDet σ Unit} (h : Det m) : Det (NonDet.when P m) := by + unfold NonDet.when; split + · exact h + · exact det_pure _ + +theorem det_maybeM {f : α → NonDet σ Unit} (h : ∀ a, Det (f a)) (o : Option α) : Det (NonDet.maybeM f o) := by + cases o + · exact det_pure _ + · exact h _ + +theorem det_mapMx {f : α → NonDet σ β} (h : ∀ a, Det (f a)) : ∀ l, Det (NonDet.mapMx f l) + | [] => det_pure _ + | a :: l => det_bind' (h a) fun _ => det_mapMx h l + +theorem det_liftM {f : α → β} {m : NonDet σ α} (h : Det m) : Det (NonDet.liftM f m) := + det_bind' h fun _ => det_pure _ + +theorem whileResults_det {C : α → σ → Prop} {B : α → NonDet σ α} (hB : ∀ r, Det (B r)) {r : α} {s : σ} + {x : α × σ} (hx : NonDet.WhileResults C B r s x) : + ∀ {y}, NonDet.WhileResults C B r s y → x = y := by + induction hx with + | done hc => + intro y hy + cases hy with + | done _ => rfl + | step hc' _ _ => exact absurd hc' hc + | step hc hm _ ih => + intro y hy + cases hy with + | done hc' => exact absurd hc hc' + | step _ hm' hy' => + have e := (hB _).det _ _ _ hm hm' + simp only [Prod.mk.injEq] at e + obtain ⟨rfl, rfl⟩ := e + exact ih hy' + +theorem det_whileLoop {C : α → σ → Prop} {B : α → NonDet σ α} (hB : ∀ r, Det (B r)) (r : α) : + Det (NonDet.whileLoop C B r) := by + constructor + intro s x y hx hy + exact whileResults_det hB hx hy + +/-- **A bind with one outcome fails only where that outcome's continuation fails** (new). -/ +theorem detAt_bind_fail {m : NonDet σ α} {f : α → NonDet σ β} {s : σ} (hm : DetAt m s) (hm0 : ¬ (m s).2) + {u : α × σ} (hu : u ∈ (m s).1) (h : (NonDet.bind m f s).2) : (f u.1 u.2).2 := by + rcases h with h | ⟨v, hv, h⟩ + · exact absurd h hm0 + · rw [hm v u hv hu] at h; exact h + +end + +/-- **The `det` tactic**: the combinators, and `split` for `if` and `match`. -/ +macro "det" : tactic => `(tactic| repeat' (first + | exact det_pure _ | exact det_pure' _ | exact det_get | exact det_put _ | exact det_gets _ + | exact det_modify _ | exact det_fail | exact det_assert _ | exact det_assertOpt _ | exact det_getsThe _ + | exact det_stateAssert _ + | apply det_bind | apply det_bind' | apply det_when | apply det_maybeM | apply det_mapMx + | apply det_liftM | apply det_whileLoop + | (intro) + | (split))) + +/-! ## The spec functions of the cancellation paths -/ + +section Spec +open Fl4SpecA64 + +theorem det_getObject (p : ObjRef) : Det (getObject (Z := Unit) p) := det_getsThe _ + +theorem det_setObject (p : ObjRef) (o : KernelObject) : Det (setObject (Z := Unit) p o) := by + unfold setObject; det + all_goals exact det_getObject _ + +theorem det_threadGet {α : Type} (f : Tcb → α) (t : ObjRef) : Det (threadGet (Z := Unit) f t) := by + unfold threadGet; det + +theorem det_threadSet (f : Tcb → Tcb) (t : ObjRef) : Det (threadSet (Z := Unit) f t) := by + unfold threadSet; det + all_goals exact det_setObject _ _ + +theorem det_getSimpleKo {α : Type} [Nonempty α] (f : α → KernelObject) (p : ObjRef) : + Det (getSimpleKo (Z := Unit) f p) := by + unfold getSimpleKo; det + all_goals exact det_getObject _ + +theorem det_setSimpleKo {α : Type} [Nonempty α] (f : α → KernelObject) (p : ObjRef) (e : α) : + Det (setSimpleKo (Z := Unit) f p e) := by + unfold setSimpleKo; det + all_goals first | exact det_getObject _ | exact det_setObject _ _ + +theorem det_getSchedContext (p : ObjRef) : Det (getSchedContext (Z := Unit) p) := by + unfold getSchedContext; det + all_goals exact det_getObject _ + +theorem det_updateSchedContext (p : ObjRef) (f : SchedContext → SchedContext) : + Det (updateSchedContext (Z := Unit) p f) := by + unfold updateSchedContext; det + all_goals first | exact det_getObject _ | exact det_setObject _ _ + +theorem det_tcbEpDequeue (t e : ObjRef) : Det (tcbEpDequeue (Z := Unit) t e) := by + unfold tcbEpDequeue; det + all_goals first | exact det_getSimpleKo _ _ | exact det_setSimpleKo _ _ _ + +theorem det_tcbNtfnDequeue (t n : ObjRef) : Det (tcbNtfnDequeue (Z := Unit) t n) := by + unfold tcbNtfnDequeue; det + all_goals first | exact det_getSimpleKo _ _ | exact det_setSimpleKo _ _ _ + +theorem det_rescheduleRequired : Det (rescheduleRequired (Z := Unit)) := by + unfold rescheduleRequired tcbSchedAction getTcbQueue setTcbQueue setSchedulerAction + getScRefillSufficient getScRefillReady; det + all_goals exact det_threadGet _ _ + +theorem det_setThreadState (t : ObjRef) (ts : ThreadState) : Det (setThreadState (Z := Unit) t ts) := by + unfold setThreadState setThreadStateAct setSchedulerAction; det + all_goals exact det_threadSet _ _ + +theorem det_possibleSwitchTo (t : ObjRef) : Det (possibleSwitchTo (Z := Unit) t) := by + unfold possibleSwitchTo getTcbObjRef tcbSchedAction getTcbQueue setTcbQueue setSchedulerAction; det + all_goals first | exact det_threadGet _ _ | exact det_rescheduleRequired + +theorem det_refillUnblockCheck (p : ObjRef) : Det (refillUnblockCheck (Z := Unit) p) := by + unfold refillUnblockCheck isRoundRobin getScRefillReady refillHeadOverlappingLoop mergeOverlappingRefills + refillPopHead getRefillHead updateRefillHd; det + all_goals first | exact det_getSchedContext _ | exact det_updateSchedContext _ _ + +theorem det_ifCondRefillUnblockCheck (o : Option ObjRef) (ac as' : Option Bool) : + Det (ifCondRefillUnblockCheck (Z := Unit) o ac as') := by + unfold ifCondRefillUnblockCheck; det + all_goals first | exact det_getSchedContext _ | exact det_refillUnblockCheck _ + +/-- `restart_thread_if_no_fault` has one outcome. -/ +theorem det_restartThreadIfNoFault (t : ObjRef) : Det (restartThreadIfNoFault (Z := Unit) t) := by + unfold restartThreadIfNoFault getTcbObjRef; det + all_goals first | exact det_threadGet _ _ | exact det_setThreadState _ _ | exact det_ifCondRefillUnblockCheck _ _ _ | exact det_possibleSwitchTo _ + +/-- `remove_and_restart_badged_thread` has one outcome. -/ +theorem det_removeAndRestartBadged (t e : ObjRef) (b : Badge) : + Det (removeAndRestartBadgedThread (Z := Unit) t e b) := by + unfold removeAndRestartBadgedThread getThreadState; det + all_goals first | exact det_threadGet _ _ | exact det_tcbEpDequeue _ _ | exact det_restartThreadIfNoFault _ + +/-- `remove_and_restart_ntfn_queued_thread` has one outcome. -/ +theorem det_removeAndRestartNtfn (t n : ObjRef) : Det (removeAndRestartNtfnQueuedThread (Z := Unit) t n) := by + unfold removeAndRestartNtfnQueuedThread getTcbObjRef; det + all_goals first | exact det_tcbNtfnDequeue _ _ | exact det_setThreadState _ _ | exact det_threadGet _ _ | exact det_ifCondRefillUnblockCheck _ _ _ | exact det_possibleSwitchTo _ + +theorem det_updateSkObjRef {α : Type} [Nonempty α] (C : α → KernelObject) + (f : (Option ObjRef → Option ObjRef) → α → α) (p : ObjRef) (n : Option ObjRef) : + Det (updateSkObjRef (Z := Unit) C f p n) := by + unfold updateSkObjRef; det + all_goals first | exact det_getSimpleKo _ _ | exact det_setSimpleKo _ _ _ + +theorem det_replyUnlinkTcb (t r : ObjRef) : Det (replyUnlinkTcb (Z := Unit) t r) := by + unfold replyUnlinkTcb getThreadState; det + all_goals first | exact det_getSimpleKo _ _ | exact det_threadGet _ _ | exact det_updateSkObjRef _ _ _ _ | exact det_setThreadState _ _ + +/-- `remove_and_restart_ep_queued_thread` has one outcome. -/ +theorem det_removeAndRestartEp (t e : ObjRef) : Det (removeAndRestartEpQueuedThread (Z := Unit) t e) := by + unfold removeAndRestartEpQueuedThread getThreadState; det + all_goals first | exact det_tcbEpDequeue _ _ | exact det_threadGet _ _ | exact det_replyUnlinkTcb _ _ | exact det_restartThreadIfNoFault _ + +end Spec + +end Fl4ProofsA64.P3.Det diff --git a/proofs/Fl4ProofsA64/P3/SpecFrA.lean b/proofs/Fl4ProofsA64/P3/SpecFrA.lean new file mode 100644 index 000000000..a27423bf8 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/SpecFrA.lean @@ -0,0 +1,805 @@ +/- +P3 (aarch64, A64-P3 S3 gen 3, S4 step 3): **`SInvXA` across the first CSpace leaves** of the AArch64 spec — the +spec-side half of x86's first CSpace-leaf refinement steps (`P3/InsertRefine` / `InsertSpec` for `cap_insert`, +`P3/MoveRefine` for `cap_move` / `cap_swap`, `P3/EmptyRefine` for `empty_slot`), each of which evaluates the l4v +operation on the spec and keeps the spec invariant beside the relation. + +* **`SchedFrA σ σ'`**: the scheduler's view of the spec is kept: every TCB stays a TCB with the same thread state and + SC (`TcbSchedEq`; its cap fields may change) and nothing becomes a TCB, every SC object is kept both ways, and the + current / idle thread, current SC, ready and release queues are kept. It is a preorder (`refl`, `trans`), weaker than + gen 2's `SchedSameA` (`SchedSameA.fr`), and **`SInvXA.of_fr`**: it keeps `SInvXA` (which reads only these). +* **`NdRel f`**: every outcome of the spec computation `f` (from any state) is `SchedFrA`-related to its start + (l4v `valid` with a two-state postcondition). Closed under `bind`; the read-only primitives (`get`, `gets`, + `assert`, `assertOpt`, `getsThe`, `selectF`, `pure`, `fail`), `modify`/`get`-`put` of non-scheduler fields. +* The CSpace primitives: **`setCap_ndr`** (`set_cap` replaces a CNode by a CNode or a TCB by the same TCB with one cap + field changed: `setObject_mem`, `getObject_mem`), `getCap_ndr`, `setCdt_ndr`, `updateCdt_ndr`, `setOriginal_ndr`, + `doExtendedOp_ndr`, `setIrqState_ndr`, `postCapDeletion_ndr`, `setUntypedCapAsFull_ndr`. +* **The leaves**: `capInsert_ndr` (`cap_insert`), `capMove_ndr` (`cap_move`, FL4's `cte_move`), `capSwap_ndr` + (`cap_swap`), `capSwapForDelete_ndr`, `emptySlot_ndr` (`empty_slot`), and **`SInvXA.capInsert`**, + `SInvXA.capMove`, `SInvXA.capSwap`, `SInvXA.emptySlot`: every outcome keeps `SInvXA` at the same `m`, `n`. + +* (gen 4, ruling 10:33Z item 5) **The IRQ node across the leaves**: `NdK f` (every outcome keeps the heap and the IRQ + node; the read-only and bookkeeping primitives), `NdIrqAt nd f σ` / `NdIrq nd f` (every outcome from a state at IRQ + node `nd` with `SpecIrqA` is at `nd` with `SpecIrqA`; closed under `bind`, `NdIrq.keep`), `getCap_memA` / + `setCap_memA` (the outcomes of `get_cap` / `set_cap`), **`setCap_irq`** for a cap that may go into the slot + (`CapOkA nd sl c`: null or a notification in an IRQ-node CNode, never a CNode cap naming one), `capInsert_irq` + (the source's own untyped cap: `setUntypedCapAsFull_irq`), `capMove_irq`, `capSwap_irq`, `capSwapForDelete_irq` + (both slots off the IRQ node, `OffNodeA`; x86 `SpecGlob.swap`), `emptySlot_irq`. + **Changed statements**: `SInvXA.of_fr` takes `SpecIrqA σ'`; `SInvXA.capInsert` / `.capMove` take `CapOkA` for + the destination, `SInvXA.capSwap` for both slots; new `SInvXA.capSwapForDelete` (`OffNodeA` for both slots). + +No named premise. +-/ +import Fl4ProofsA64.P3.FrameA + +namespace Fl4ProofsA64.P3 +open Fl4Spec Fl4SpecA64 + +noncomputable section + +/-! ## The scheduler frame -/ + +/-- The TCB fields `SInvXA` reads are equal. -/ +def TcbSchedEq (x x' : Tcb) : Prop := x'.tcbState = x.tcbState ∧ x'.tcbSchedContext = x.tcbSchedContext ∧ + x'.tcbFault = x.tcbFault + +/-- **The scheduler's view of the spec is kept** (module docstring). -/ +structure SchedFrA (σ σ' : State Unit) : Prop where + tcb : ∀ p x, σ.kheap p = some (.tcb x) → ∃ x', σ'.kheap p = some (.tcb x') ∧ TcbSchedEq x x' + tcb' : ∀ p x', σ'.kheap p = some (.tcb x') → ∃ x, σ.kheap p = some (.tcb x) ∧ TcbSchedEq x x' + sc : ∀ p y k, σ'.kheap p = some (.schedContext y k) ↔ σ.kheap p = some (.schedContext y k) + cur : σ'.curThread = σ.curThread + idle : σ'.idleThread = σ.idleThread + csc : σ'.curSc = σ.curSc + rq : σ'.readyQueues = σ.readyQueues + rls : σ'.releaseQueue = σ.releaseQueue + /-- (record edit #8) The scheduler action is kept (`SInvXA.ctQ` is guarded by it). -/ + act : σ'.schedulerAction = σ.schedulerAction + /-- (record edit #8b-C2, item 11) The time and the consumed time are kept. -/ + time : σ'.curTime = σ.curTime + cons : σ'.consumedTime = σ.consumedTime + +theorem SchedFrA.refl (σ : State Unit) : SchedFrA σ σ := + ⟨fun _ x h => ⟨x, h, rfl, rfl, rfl⟩, fun _ x h => ⟨x, h, rfl, rfl, rfl⟩, fun _ _ _ => Iff.rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ + +theorem SchedFrA.trans {σ1 σ2 σ3 : State Unit} (h1 : SchedFrA σ1 σ2) (h2 : SchedFrA σ2 σ3) : SchedFrA σ1 σ3 where + tcb p x h := by + obtain ⟨x2, e2, a2, b2, c2⟩ := h1.tcb p x h + obtain ⟨x3, e3, a3, b3, c3⟩ := h2.tcb p x2 e2 + exact ⟨x3, e3, a3.trans a2, b3.trans b2, c3.trans c2⟩ + tcb' p x3 h := by + obtain ⟨x2, e2, a3, b3, c3⟩ := h2.tcb' p x3 h + obtain ⟨x, e, a2, b2, c2⟩ := h1.tcb' p x2 e2 + exact ⟨x, e, a3.trans a2, b3.trans b2, c3.trans c2⟩ + sc p y k := (h2.sc p y k).trans (h1.sc p y k) + cur := h2.cur.trans h1.cur + idle := h2.idle.trans h1.idle + csc := h2.csc.trans h1.csc + rq := h2.rq.trans h1.rq + rls := h2.rls.trans h1.rls + act := h2.act.trans h1.act + time := h2.time.trans h1.time + cons := h2.cons.trans h1.cons + +/-- A state with the same heap and scheduler fields. -/ +theorem SchedFrA.of_eq {σ σ' : State Unit} (hk : σ'.kheap = σ.kheap) (hc : σ'.curThread = σ.curThread) + (hi : σ'.idleThread = σ.idleThread) (hs : σ'.curSc = σ.curSc) (hr : σ'.readyQueues = σ.readyQueues) + (hl : σ'.releaseQueue = σ.releaseQueue) (ha : σ'.schedulerAction = σ.schedulerAction) + (ht : σ'.curTime = σ.curTime) (hn : σ'.consumedTime = σ.consumedTime) : SchedFrA σ σ' where + tcb p x h := ⟨x, by rw [hk]; exact h, rfl, rfl, rfl⟩ + tcb' p x h := ⟨x, by rw [← hk]; exact h, rfl, rfl, rfl⟩ + sc p y k := by rw [hk] + cur := hc + idle := hi + csc := hs + rq := hr + rls := hl + act := ha + time := ht + cons := hn + +/-- Gen 2's `SchedSameA` is a `SchedFrA`. -/ +theorem SchedSameA.fr {σ σ' : State Unit} (H : SchedSameA σ σ') (ha : σ'.schedulerAction = σ.schedulerAction) : + SchedFrA σ σ' where + time := H.time + cons := H.cons + tcb p x h := ⟨x, H.tcb.2 h, rfl, rfl, rfl⟩ + tcb' p x h := ⟨x, H.tcb.1 h, rfl, rfl, rfl⟩ + sc p y k := H.sc + cur := H.cur + idle := H.idle + csc := H.csc + rq := H.rq + rls := H.rls + act := ha + +/-- **`SInvXA` kept by the scheduler frame** (module docstring; gen 4: given the IRQ node's `SpecIrqA` after). -/ +theorem SInvXA.of_fr {σ σ' : State Unit} {m : McState} {n : Nat} (h : SInvXA σ m n) (H : SchedFrA σ σ') + (hI : SpecIrqA σ') : SInvXA σ' m n where + mc := h.mc + view := by unfold McView; rw [H.idle]; exact h.view + curTcb := by + obtain ⟨x, e⟩ := h.curTcb + obtain ⟨x', e', -⟩ := H.tcb _ x e + exact ⟨x', by rw [H.cur]; exact e'⟩ + idleTcb := by + obtain ⟨x, e, e2⟩ := h.idleTcb + obtain ⟨x', e', s', -⟩ := H.tcb _ x e + exact ⟨x', by rw [H.idle]; exact e', s'.trans e2⟩ + onlyIdle t x' e e2 := by + obtain ⟨x, e1, s1, -⟩ := H.tcb' t x' e + exact h.onlyIdle t x e1 (s1.symm.trans e2) + idleSc := by + obtain ⟨x, y, k, e1, e2, e3, e4⟩ := h.idleSc + obtain ⟨x', e1', -, c', -⟩ := H.tcb _ x e1 + exact ⟨x', y, k, by rw [H.idle]; exact e1', c'.trans e2, (H.sc _ y k).2 e3, by rw [H.idle]; exact e4⟩ + curSc := by obtain ⟨y, k, e⟩ := h.curSc; exact ⟨y, k, by rw [H.csc]; exact (H.sc _ y k).2 e⟩ + ctQ ha d p := by rw [H.cur, H.rq]; exact h.ctQ (H.act.symm.trans ha) d p + idleQ d p := by rw [H.rq]; exact h.idleQ d p + rq d p t ht := by + rw [H.rq] at ht + obtain ⟨x, e, e2⟩ := h.rq d p t ht + obtain ⟨x', e', s', -⟩ := H.tcb _ x e + exact ⟨x', e', by rw [s']; exact e2⟩ + rls t ht := by + rw [H.rls] at ht + obtain ⟨x, e⟩ := h.rls t ht + obtain ⟨x', e', -⟩ := H.tcb _ x e + exact ⟨x', e'⟩ + rpos p y k e a := h.rpos p y k ((H.sc p y k).1 e) a + zc := (CurFactsA.back (σ := σ) (σ' := σ') ⟨h.zc, h.rlsRun, h.curScReady⟩ (fun t ht => by rw [H.rls] at ht; exact ht) H.cons H.csc H.time + (fun t x' e => by obtain ⟨x, e0, -, s, -⟩ := H.tcb' t x' e; exact ⟨x, e0, s.symm⟩) + (fun t x e _ => by obtain ⟨x', e', s, -⟩ := H.tcb t x e; exact ⟨x', e', fun r => by rw [s]; exact r⟩) + (fun y k e => ⟨y, (H.sc _ y k).1 e, fun a => ⟨a, id⟩⟩)).1 + rlsRun := (CurFactsA.back (σ := σ) (σ' := σ') ⟨h.zc, h.rlsRun, h.curScReady⟩ (fun t ht => by rw [H.rls] at ht; exact ht) H.cons H.csc H.time + (fun t x' e => by obtain ⟨x, e0, -, s, -⟩ := H.tcb' t x' e; exact ⟨x, e0, s.symm⟩) + (fun t x e _ => by obtain ⟨x', e', s, -⟩ := H.tcb t x e; exact ⟨x', e', fun r => by rw [s]; exact r⟩) + (fun y k e => ⟨y, (H.sc _ y k).1 e, fun a => ⟨a, id⟩⟩)).2.1 + curScReady := (CurFactsA.back (σ := σ) (σ' := σ') ⟨h.zc, h.rlsRun, h.curScReady⟩ (fun t ht => by rw [H.rls] at ht; exact ht) H.cons H.csc H.time + (fun t x' e => by obtain ⟨x, e0, -, s, -⟩ := H.tcb' t x' e; exact ⟨x, e0, s.symm⟩) + (fun t x e _ => by obtain ⟨x', e', s, -⟩ := H.tcb t x e; exact ⟨x', e', fun r => by rw [s]; exact r⟩) + (fun y k e => ⟨y, (H.sc _ y k).1 e, fun a => ⟨a, id⟩⟩)).2.2 + faultSt t x' e f := by + obtain ⟨x, e0, s, -, fa⟩ := H.tcb' t x' e + rw [s]; exact h.faultSt t x e0 (by rw [← fa]; exact f) + irqN := hI + +/-- A heap update at `p` that replaces a TCB by one with the same state and SC, or a non-scheduler object by a +non-scheduler object. -/ +def SchedLikeA (o o' : KernelObject) : Prop := + (∃ x x', o = .tcb x ∧ o' = .tcb x' ∧ TcbSchedEq x x') ∨ (¬ SchedObjA o ∧ ¬ SchedObjA o') + +theorem SchedFrA.upd {σ : State Unit} {p : ObjRef} {o o' : KernelObject} (h : σ.kheap p = some o) + (hl : SchedLikeA o o') : SchedFrA σ { σ with kheap := Function.update σ.kheap p (some o') } where + tcb q x hq := by + by_cases e : q = p + · subst e + rw [h] at hq + cases hq + rcases hl with ⟨x0, x', e0, rfl, hs⟩ | ⟨h1, -⟩ + · cases e0; exact ⟨x', by simp, hs⟩ + · exact absurd (Or.inl ⟨x, rfl⟩) h1 + · exact ⟨x, by simp [Function.update_of_ne e]; exact hq, rfl, rfl, rfl⟩ + tcb' q x' hq := by + by_cases e : q = p + · subst e + simp only [Function.update_self, Option.some.injEq] at hq + subst hq + rcases hl with ⟨x0, x1, rfl, e1, hs⟩ | ⟨-, h2⟩ + · cases e1; exact ⟨x0, h, hs⟩ + · exact absurd (Or.inl ⟨x', rfl⟩) h2 + · exact ⟨x', by simpa [Function.update_of_ne e] using hq, rfl, rfl, rfl⟩ + sc q y k := by + by_cases e : q = p + · subst e + simp only [Function.update_self, Option.some.injEq] + rw [h, Option.some.injEq] + rcases hl with ⟨x0, x1, rfl, rfl, -⟩ | ⟨h1, h2⟩ + · constructor <;> intro e' <;> cases e' + · constructor + · rintro rfl; exact absurd (Or.inr ⟨y, k, rfl⟩) h2 + · rintro rfl; exact absurd (Or.inr ⟨y, k, rfl⟩) h1 + · simp [Function.update_of_ne e] + cur := rfl + idle := rfl + csc := rfl + rq := rfl + rls := rfl + act := rfl + time := rfl + cons := rfl + +/-! ## Spec computations that keep the frame -/ + +/-- **Every outcome of `f` is `SchedFrA`-related to its start** (module docstring). -/ +def NdRel {α : Type} (f : NonDet (State Unit) α) : Prop := ∀ σ r, r ∈ (f σ).1 → SchedFrA σ r.2 + +theorem mem_bindA {α β : Type} {m : NonDet (State Unit) α} {f : α → NonDet (State Unit) β} {σ : State Unit} + {r : β × State Unit} : r ∈ ((m >>= f) σ).1 ↔ ∃ x ∈ (m σ).1, r ∈ (f x.1 x.2).1 := Iff.rfl + +theorem NdRel.bind {α β : Type} {m : NonDet (State Unit) α} {f : α → NonDet (State Unit) β} (hm : NdRel m) + (hf : ∀ a, NdRel (f a)) : NdRel (m >>= f) := fun σ r hr => by + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + exact (hm σ x hx).trans (hf x.1 x.2 r hr) + +/-- A computation that does not change the state. -/ +theorem NdRel.ro {α : Type} {f : NonDet (State Unit) α} (h : ∀ σ r, r ∈ (f σ).1 → r.2 = σ) : NdRel f := + fun σ r hr => by rw [h σ r hr]; exact SchedFrA.refl σ + +theorem NdRel.pure {α : Type} (a : α) : NdRel (Pure.pure a : NonDet (State Unit) α) := + NdRel.ro fun σ r h => by obtain rfl : r = (a, σ) := h; rfl + +theorem NdRel.pure' {α : Type} (a : α) : NdRel (NonDet.pure a : NonDet (State Unit) α) := + NdRel.ro fun σ r h => by obtain rfl : r = (a, σ) := h; rfl + +theorem NdRel.get : NdRel (NonDet.get : NonDet (State Unit) (State Unit)) := + NdRel.ro fun σ r h => by obtain rfl : r = (σ, σ) := h; rfl + +theorem NdRel.gets {α : Type} (g : State Unit → α) : NdRel (NonDet.gets g) := + NdRel.ro fun σ r h => by obtain rfl : r = (g σ, σ) := h; rfl + +theorem NdRel.fail {α : Type} : NdRel (NonDet.fail : NonDet (State Unit) α) := fun _ _ h => absurd h (Set.notMem_empty _) + +theorem NdRel.assert (P : Prop) : NdRel (NonDet.assert P : NonDet (State Unit) Unit) := + NdRel.ro fun _ _ h => by obtain ⟨-, rfl⟩ := h; rfl + +theorem NdRel.assertOpt {α : Type} (o : Option α) : NdRel (NonDet.assertOpt o : NonDet (State Unit) α) := by + cases o + · exact NdRel.fail + · exact NdRel.pure' _ + +theorem NdRel.getsThe {α : Type} (g : State Unit → Option α) : NdRel (NonDet.getsThe g) := fun σ r hr => + NdRel.assertOpt (g σ) σ r hr + +theorem NdRel.selectF {α : Type} (S : Set α × Prop) : NdRel (NonDet.selectF S : NonDet (State Unit) α) := + NdRel.ro fun _ _ h => h.2 + +theorem NdRel.modify {g : State Unit → State Unit} (h : ∀ σ, SchedFrA σ (g σ)) : NdRel (NonDet.modify g) := + fun σ r hr => by obtain rfl : r = ((), g σ) := hr; exact h σ + +theorem NdRel.getPut {g : State Unit → State Unit} (h : ∀ σ, SchedFrA σ (g σ)) : + NdRel (NonDet.get >>= fun s => NonDet.put (g s)) := fun σ r hr => by + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + obtain rfl : x = (σ, σ) := hx + obtain rfl : r = ((), g σ) := hr + exact h σ + +theorem NdRel.ite {α : Type} {c : Prop} {_ : Decidable c} {f g : NonDet (State Unit) α} (hf : NdRel f) (hg : NdRel g) : + NdRel (if c then f else g) := by + split + · exact hf + · exact hg + +/-! ## The CSpace primitives -/ + +/-- `get_object`'s outcome: the object at `p`, the state unchanged. -/ +theorem getObject_mem {p : ObjRef} {σ : State Unit} {x : KernelObject × State Unit} + (hx : x ∈ (getObject (Z := Unit) p σ).1) : x.2 = σ ∧ σ.kheap p = some x.1 := by + change x ∈ (NonDet.assertOpt (σ.kheap p) σ).1 at hx + cases h : σ.kheap p with + | none => rw [h] at hx; exact absurd hx (Set.notMem_empty _) + | some o => + rw [h] at hx + obtain rfl : x = (o, σ) := hx + exact ⟨rfl, rfl⟩ + +theorem getObject_ndr (p : ObjRef) : NdRel (getObject (Z := Unit) p) := NdRel.ro fun _ _ h => (getObject_mem h).1 + +/-- `set_object`'s outcome: the heap updated at `p`. -/ +theorem setObject_mem {p : ObjRef} {o : KernelObject} {σ : State Unit} {r : Unit × State Unit} + (hr : r ∈ (setObject (Z := Unit) p o σ).1) : + ∃ o0, σ.kheap p = some o0 ∧ r.2 = { σ with kheap := Function.update σ.kheap p (some o) } := by + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + obtain ⟨e1, e2⟩ := getObject_mem hx + obtain ⟨y, hy, hr⟩ := mem_bindA.1 hr + obtain ⟨-, hy⟩ := hy + rw [hy] at hr + obtain ⟨z, hz, hr⟩ := mem_bindA.1 hr + obtain rfl : z = (x.2, x.2) := hz + obtain rfl : r = ((), { x.2 with kheap := Function.update x.2.kheap p (some o) }) := hr + rw [e1] + exact ⟨x.1, e2, rfl⟩ + +theorem getCap_ndr (sl : CslotPtr) : NdRel (getCap (Z := Unit) sl) := by + obtain ⟨oref, cref⟩ := sl + refine NdRel.bind (getObject_ndr oref) fun obj => ?_ + cases obj <;> dsimp only <;> first + | exact NdRel.fail + | exact NdRel.bind NdRel.fail fun _ => NdRel.assertOpt _ + | exact NdRel.bind (NdRel.pure _) fun _ => NdRel.assertOpt _ + | exact NdRel.bind (NdRel.assert _) fun _ => NdRel.bind (NdRel.pure _) fun _ => NdRel.assertOpt _ + +theorem cnode_notSched {sz : Nat} {cn : CnodeContents} : ¬ SchedObjA (.cnode sz cn) := by + rintro (⟨_, h⟩ | ⟨_, _, h⟩) <;> cases h + +/-- **`set_cap` keeps the scheduler frame** (module docstring). -/ +theorem setCap_ndr (cap : Cap) (sl : CslotPtr) : NdRel (setCap (Z := Unit) cap sl) := by + obtain ⟨oref, cref⟩ := sl + intro σ r hr + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + obtain ⟨e1, e2⟩ := getObject_mem hx + obtain ⟨obj, σ1⟩ := x + simp only at e1 e2 hr + subst e1 + have key : ∀ o, SchedLikeA obj o → + r ∈ ((Pure.pure o >>= fun o' => setObject (Z := Unit) oref o') σ1).1 → SchedFrA σ1 r.2 := by + intro o hl h + obtain ⟨y, hy, h⟩ := mem_bindA.1 h + obtain rfl : y = (o, σ1) := hy + obtain ⟨o0, -, hr2⟩ := setObject_mem h + rw [hr2]; exact SchedFrA.upd e2 hl + have kfail : ∀ f : KernelObject → NonDet (State Unit) Unit, r ∈ ((NonDet.fail >>= f) σ1).1 → False := by + intro f h + obtain ⟨y, hy, -⟩ := mem_bindA.1 h + exact absurd hy (Set.notMem_empty _) + cases obj with + | cnode sz cn => + simp only at hr + split_ifs at hr + · exact key _ (Or.inr ⟨cnode_notSched, cnode_notSched⟩) hr + · exact (kfail _ hr).elim + | tcb t => + simp only at hr + split_ifs at hr <;> + first + | exact (kfail _ hr).elim + | (refine key _ ?_ hr; exact Or.inl ⟨_, _, rfl, rfl, rfl, rfl, rfl⟩) + | _ => + try simp only at hr + exact (kfail _ hr).elim + +theorem setCdt_ndr (t : Cdt) : NdRel (setCdt (Z := Unit) t) := + NdRel.getPut fun σ => SchedFrA.of_eq rfl rfl rfl rfl rfl rfl rfl rfl rfl + +theorem updateCdt_ndr (g : Cdt → Cdt) : NdRel (updateCdt (Z := Unit) g) := + NdRel.bind (NdRel.gets _) fun _ => setCdt_ndr _ + +theorem setOriginal_ndr (sl : CslotPtr) (v : Bool) : NdRel (setOriginal (Z := Unit) sl v) := + NdRel.bind (NdRel.gets _) fun _ => NdRel.modify fun σ => SchedFrA.of_eq rfl rfl rfl rfl rfl rfl rfl rfl rfl + +theorem doExtendedOp_ndr (eop : DetExtMonad Unit) : NdRel (doExtendedOp (Z := Unit) eop) := + NdRel.bind NdRel.get fun _ => NdRel.bind (NdRel.selectF _) fun x => + NdRel.modify fun σ => SchedFrA.of_eq rfl rfl rfl rfl rfl rfl rfl rfl rfl + +theorem setIrqState_ndr (sti : IrqState) (irq : Irq) : NdRel (setIrqState (Z := Unit) sti irq) := + NdRel.bind (NdRel.modify fun σ => SchedFrA.of_eq rfl rfl rfl rfl rfl rfl rfl rfl rfl) fun _ => + NdRel.bind (NdRel.gets _) fun _ => NdRel.bind (NdRel.selectF _) fun x => + NdRel.bind (NdRel.modify fun σ => SchedFrA.of_eq rfl rfl rfl rfl rfl rfl rfl rfl rfl) fun _ => NdRel.pure _ + +theorem postCapDeletion_ndr (cap : Cap) : NdRel (postCapDeletion (Z := Unit) cap) := by + cases cap <;> first | exact NdRel.pure _ | exact setIrqState_ndr _ _ + +theorem setUntypedCapAsFull_ndr (srcCap newCap : Cap) (sl : CslotPtr) : + NdRel (setUntypedCapAsFull (Z := Unit) srcCap newCap sl) := + NdRel.ite (setCap_ndr _ _) (NdRel.pure _) + +/-! ## The CSpace leaves -/ + +/-- One step of a leaf: a primitive above, or a `bind` of two. -/ +syntax "ndr_step" : tactic +macro_rules + | `(tactic| ndr_step) => `(tactic| first + | exact NdRel.pure _ | exact NdRel.pure' _ | exact NdRel.gets _ | exact NdRel.get | exact NdRel.assert _ + | exact setCap_ndr _ _ | exact getCap_ndr _ | exact setCdt_ndr _ | exact updateCdt_ndr _ + | exact setOriginal_ndr _ _ | exact doExtendedOp_ndr _ | exact setUntypedCapAsFull_ndr _ _ _ + | exact postCapDeletion_ndr _ + | (refine NdRel.bind ?_ (fun _ => ?_))) + +/-- `cap_insert` (CSpace_A; x86 `P3/InsertSpec`). -/ +theorem capInsert_ndr (c : Cap) (src dst : CslotPtr) : NdRel (capInsert (Z := Unit) c src dst) := by + unfold capInsert; repeat ndr_step + +/-- `cap_move` (CSpace_A; FL4's `cte_move`, x86 `P3/MoveRefine`). -/ +theorem capMove_ndr (c : Cap) (src dst : CslotPtr) : NdRel (capMove (Z := Unit) c src dst) := by + unfold capMove; repeat ndr_step + +/-- `cap_swap` (CSpace_A). -/ +theorem capSwap_ndr (c1 : Cap) (s1 : CslotPtr) (c2 : Cap) (s2 : CslotPtr) : + NdRel (capSwap (Z := Unit) c1 s1 c2 s2) := by + unfold capSwap; repeat ndr_step + +/-- `cap_swap_for_delete` (CSpace_A). -/ +theorem capSwapForDelete_ndr (s1 s2 : CslotPtr) : NdRel (capSwapForDelete (Z := Unit) s1 s2) := by + unfold capSwapForDelete NonDet.when + refine NdRel.ite ?_ (NdRel.pure' _) + refine NdRel.bind (getCap_ndr _) fun _ => NdRel.bind (getCap_ndr _) fun _ => capSwap_ndr _ _ _ _ + +/-- `empty_slot` (IpcCancel_A; x86 `P3/EmptyRefine`). -/ +theorem emptySlot_ndr (sl : CslotPtr) (info : Cap) : NdRel (emptySlot (Z := Unit) sl info) := by + unfold emptySlot + refine NdRel.bind (getCap_ndr _) fun cap => NdRel.ite (NdRel.pure _) ?_ + repeat ndr_step + +/-! ## The IRQ node (gen 4) -/ + +/-- `SpecIrqA` reads only the heap and the IRQ node. -/ +theorem SpecIrqA.of_eq {σ σ' : State Unit} (h : SpecIrqA σ) (hk : σ'.kheap = σ.kheap) + (hn : σ'.interruptIrqNode = σ.interruptIrqNode) : SpecIrqA σ' where + node := by + have := h.node + unfold Fl4SpecA64.IrqNodeFl4 Fl4SpecA64.validIrqNodeFl4 Fl4SpecA64.irqNodeDomFl4 Fl4SpecA64.capTableAt at this ⊢ + simp only [hk, hn]; exact this + slot irq hi o b a e := h.slot irq hi o b a (by rw [← hk, ← hn]; exact e) + ref p o b a e := by rw [hn]; exact h.ref p o b a (by rw [← hk]; exact e) + +/-- Every outcome of `f` keeps the heap and the IRQ node. -/ +def NdK {α : Type} (f : NonDet (State Unit) α) : Prop := + ∀ σ r, r ∈ (f σ).1 → r.2.kheap = σ.kheap ∧ r.2.interruptIrqNode = σ.interruptIrqNode + +theorem NdK.bind {α β : Type} {m : NonDet (State Unit) α} {f : α → NonDet (State Unit) β} (hm : NdK m) + (hf : ∀ a, NdK (f a)) : NdK (m >>= f) := fun σ r hr => by + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + obtain ⟨a1, a2⟩ := hm σ x hx + obtain ⟨b1, b2⟩ := hf x.1 x.2 r hr + exact ⟨b1.trans a1, b2.trans a2⟩ + +theorem NdK.ro {α : Type} {f : NonDet (State Unit) α} (h : ∀ σ r, r ∈ (f σ).1 → r.2 = σ) : NdK f := + fun σ r hr => by rw [h σ r hr]; exact ⟨rfl, rfl⟩ + +theorem NdK.modify {g : State Unit → State Unit} + (h : ∀ σ, (g σ).kheap = σ.kheap ∧ (g σ).interruptIrqNode = σ.interruptIrqNode) : NdK (NonDet.modify g) := + fun σ r hr => by obtain rfl : r = ((), g σ) := hr; exact h σ + +theorem ndk_pure {α : Type} (a : α) : NdK (Pure.pure a : NonDet (State Unit) α) := + NdK.ro fun σ r h => by obtain rfl : r = (a, σ) := h; rfl + +theorem ndk_pure' {α : Type} (a : α) : NdK (NonDet.pure a : NonDet (State Unit) α) := + NdK.ro fun σ r h => by obtain rfl : r = (a, σ) := h; rfl + +theorem ndk_gets {α : Type} (g : State Unit → α) : NdK (NonDet.gets g) := + NdK.ro fun σ r h => by obtain rfl : r = (g σ, σ) := h; rfl + +theorem ndk_get : NdK (NonDet.get : NonDet (State Unit) (State Unit)) := + NdK.ro fun σ r h => by obtain rfl : r = (σ, σ) := h; rfl + +theorem ndk_assert (P : Prop) : NdK (NonDet.assert P : NonDet (State Unit) Unit) := + NdK.ro fun _ _ h => by obtain ⟨-, rfl⟩ := h; rfl + +theorem ndk_selectF {α : Type} (S : Set α × Prop) : NdK (NonDet.selectF S : NonDet (State Unit) α) := + NdK.ro fun _ _ h => h.2 + +theorem pure_memA {α : Type} {a : α} {σ : State Unit} {r : α × State Unit} + (h : r ∈ ((Pure.pure a : NonDet (State Unit) α) σ).1) : r = (a, σ) := h + +/-- **`get_cap`'s outcome**: the state unchanged, the cap of the object at the slot (`cap_of`). -/ +theorem getCap_memA {sl : CslotPtr} {σ : State Unit} {r : Cap × State Unit} + (hr : r ∈ (Fl4SpecA64.getCap (Z := Unit) sl σ).1) : + r.2 = σ ∧ ∃ o, σ.kheap sl.1 = some o ∧ Fl4SpecA64.capOf o sl.2 = some r.1 := by + obtain ⟨oref, cref⟩ := sl + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + obtain ⟨e1, e2⟩ := getObject_mem hx + obtain ⟨obj, σ1⟩ := x + simp only at e1 e2 hr + subst e1 + have fin : ∀ cs : CnodeIndex → Option Cap, Fl4SpecA64.capOf obj = cs → + r ∈ (NonDet.assertOpt (cs cref) σ1).1 → + r.2 = σ1 ∧ ∃ o, σ1.kheap oref = some o ∧ Fl4SpecA64.capOf o cref = some r.1 := by + intro cs hc hr + cases h : cs cref with + | none => rw [h] at hr; exact absurd hr (Set.notMem_empty _) + | some c => + rw [h] at hr + obtain rfl : r = (c, σ1) := hr + exact ⟨rfl, obj, e2, by rw [hc]; exact h⟩ + cases obj with + | cnode sz cn => + simp only at hr + obtain ⟨y, hy, hr⟩ := mem_bindA.1 hr + first + | (obtain ⟨-, rfl⟩ := hy + obtain ⟨y', hy', hr⟩ := mem_bindA.1 hr + obtain rfl := pure_memA hy' + exact fin cn rfl hr) + | (obtain ⟨z, hz, hy⟩ := mem_bindA.1 hy + obtain ⟨-, rfl⟩ := hz + obtain rfl := pure_memA hy + exact fin cn rfl hr) + | tcb t => + simp only at hr + obtain ⟨y, hy, hr⟩ := mem_bindA.1 hr + obtain rfl := pure_memA hy + exact fin _ rfl hr + | _ => + try simp only at hr + obtain ⟨y, hy, -⟩ := mem_bindA.1 hr + exact absurd hy (Set.notMem_empty _) + +theorem ndk_getCap (sl : CslotPtr) : NdK (Fl4SpecA64.getCap (Z := Unit) sl) := + NdK.ro fun _ _ h => (getCap_memA h).1 + +theorem NdK.getPut {g : State Unit → State Unit} + (h : ∀ σ, (g σ).kheap = σ.kheap ∧ (g σ).interruptIrqNode = σ.interruptIrqNode) : + NdK (NonDet.get >>= fun s => NonDet.put (g s)) := fun σ r hr => by + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + obtain rfl : x = (σ, σ) := hx + obtain rfl : r = ((), g σ) := hr + exact h σ + +theorem ndk_setCdt (t : Cdt) : NdK (setCdt (Z := Unit) t) := NdK.getPut fun _ => ⟨rfl, rfl⟩ + +theorem ndk_updateCdt (g : Cdt → Cdt) : NdK (updateCdt (Z := Unit) g) := + NdK.bind (ndk_gets _) fun _ => ndk_setCdt _ + +theorem ndk_setOriginal (sl : CslotPtr) (v : Bool) : NdK (setOriginal (Z := Unit) sl v) := + NdK.bind (ndk_gets _) fun _ => NdK.modify fun _ => ⟨rfl, rfl⟩ + +theorem ndk_doExtendedOp (eop : DetExtMonad Unit) : NdK (doExtendedOp (Z := Unit) eop) := + NdK.bind ndk_get fun _ => NdK.bind (ndk_selectF _) fun _ => NdK.modify fun _ => ⟨rfl, rfl⟩ + +theorem ndk_setIrqState (sti : IrqState) (irq : Irq) : NdK (setIrqState (Z := Unit) sti irq) := + NdK.bind (NdK.modify fun _ => ⟨rfl, rfl⟩) fun _ => NdK.bind (ndk_gets _) fun _ => + NdK.bind (ndk_selectF _) fun _ => NdK.bind (NdK.modify fun _ => ⟨rfl, rfl⟩) fun _ => ndk_pure _ + +theorem ndk_postCapDeletion (cap : Cap) : NdK (postCapDeletion (Z := Unit) cap) := by + cases cap <;> first | exact ndk_pure _ | exact ndk_setIrqState _ _ + +/-- (gen 4) **Every outcome of `f` at `σ` keeps the IRQ node `nd` and `SpecIrqA`.** -/ +def NdIrqAt {α : Type} (nd : Irq → ObjRef) (f : NonDet (State Unit) α) (σ : State Unit) : Prop := + σ.interruptIrqNode = nd → SpecIrqA σ → ∀ r, r ∈ (f σ).1 → r.2.interruptIrqNode = nd ∧ SpecIrqA r.2 + +/-- `NdIrqAt` from every state. -/ +def NdIrq {α : Type} (nd : Irq → ObjRef) (f : NonDet (State Unit) α) : Prop := ∀ σ, NdIrqAt nd f σ + +theorem NdIrqAt.bind {α β : Type} {nd : Irq → ObjRef} {m : NonDet (State Unit) α} {f : α → NonDet (State Unit) β} + {σ : State Unit} (hm : NdIrqAt nd m σ) (hf : ∀ x ∈ (m σ).1, NdIrqAt nd (f x.1) x.2) : + NdIrqAt nd (m >>= f) σ := fun hn hI r hr => by + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + obtain ⟨hn', hI'⟩ := hm hn hI x hx + exact hf x hx hn' hI' r hr + +theorem NdIrq.bind {α β : Type} {nd : Irq → ObjRef} {m : NonDet (State Unit) α} {f : α → NonDet (State Unit) β} + (hm : NdIrq nd m) (hf : ∀ a, NdIrq nd (f a)) : NdIrq nd (m >>= f) := + fun σ => (hm σ).bind fun x _ => hf x.1 x.2 + +theorem NdIrq.keep {α : Type} {nd : Irq → ObjRef} {f : NonDet (State Unit) α} (h : NdK f) : NdIrq nd f := + fun σ hn hI r hr => by + obtain ⟨e1, e2⟩ := h σ r hr + exact ⟨e2.trans hn, hI.of_eq e1 e2⟩ + +/-- (gen 4) **A cap `c` that may be written into slot `sl`** at the IRQ node `nd`: null or a notification if `sl` +is an IRQ-node CNode of FL4's IRQs, and not a CNode cap naming one. -/ +def CapOkA (nd : Irq → ObjRef) (sl : CslotPtr) (c : Cap) : Prop := + (∀ irq : Irq, irq.toNat < fl4IrqCount → sl.1 = nd irq → IrqCapA c) ∧ ¬ NodeCapA nd c + +theorem CapOkA.null (nd : Irq → ObjRef) (sl : CslotPtr) : CapOkA nd sl .nullCap := + ⟨fun _ _ _ => Or.inl rfl, fun ⟨_, _, _, _, e⟩ => by cases e⟩ + +/-- (gen 4) A pointer off the IRQ-node CNodes of FL4's IRQs. -/ +def OffNodeA (nd : Irq → ObjRef) (p : ObjRef) : Prop := ∀ irq : Irq, irq.toNat < fl4IrqCount → p ≠ nd irq + +theorem dom_updA {cn : CnodeIndex → Option Cap} {cref : CnodeIndex} (h : cref ∈ Fl4SpecA64.dom cn) (c : Cap) : + Fl4SpecA64.dom (Function.update cn cref (some c)) = Fl4SpecA64.dom cn := by + ext x + by_cases e : x = cref + · subst e; simp only [Fl4SpecA64.dom, Set.mem_setOf_eq, Function.update_self] at h ⊢ + exact ⟨fun _ => h, fun _ => by simp⟩ + · simp [Fl4SpecA64.dom, Function.update_of_ne e] + +theorem upd_or {f : CnodeIndex → Option Cap} {k b : CnodeIndex} {c a : Cap} + (h : Function.update f k (some c) b = some a) : (b = k ∧ a = c) ∨ f b = some a := by + by_cases e : b = k + · subst e; simp only [Function.update_self, Option.some.injEq] at h; exact Or.inl ⟨rfl, h.symm⟩ + · rw [Function.update_of_ne e] at h; exact Or.inr h + +/-- A TCB whose cap map differs from `x`'s only at slot `k`, which holds `c`. -/ +theorem tcbMap_or {x x' : Tcb} {k : Nat} {c : Cap} + (ho : ∀ b, b ≠ tcbCnodeIndex k → tcbCnodeMap x' b = tcbCnodeMap x b) (hk : tcbCnodeMap x' (tcbCnodeIndex k) = some c) + {b : CnodeIndex} {a : Cap} (h : tcbCnodeMap x' b = some a) : (b = tcbCnodeIndex k ∧ a = c) ∨ tcbCnodeMap x b = some a := by + by_cases e : b = tcbCnodeIndex k + · subst e; rw [hk] at h; exact Or.inl ⟨rfl, (Option.some.inj h).symm⟩ + · rw [ho b e] at h; exact Or.inr h + +/-- **`set_cap`'s outcome** (gen 4): the object at the slot replaced by one whose caps are the old ones but the +slot's, which is `cap`, and which is a well-formed CNode of a radix iff the old one is. -/ +theorem setCap_memA {cap : Cap} {p : ObjRef} {cref : CnodeIndex} {σ : State Unit} {r : Unit × State Unit} + (hr : r ∈ (Fl4SpecA64.setCap (Z := Unit) cap (p, cref) σ).1) : + ∃ o o', σ.kheap p = some o ∧ r.2 = { σ with kheap := Function.update σ.kheap p (some o') } ∧ + (∀ b a, Fl4SpecA64.capOf o' b = some a → (b = cref ∧ a = cap) ∨ Fl4SpecA64.capOf o b = some a) ∧ + ∀ n, (∃ cs, o' = .cnode n cs ∧ wellFormedCnodeN n cs) ↔ (∃ cs, o = .cnode n cs ∧ wellFormedCnodeN n cs) := by + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + obtain ⟨e1, e2⟩ := getObject_mem hx + obtain ⟨obj, σ1⟩ := x + simp only at e1 e2 hr + subst e1 + have key : ∀ o, (∀ b a, Fl4SpecA64.capOf o b = some a → (b = cref ∧ a = cap) ∨ Fl4SpecA64.capOf obj b = some a) → + (∀ n, (∃ cs, o = .cnode n cs ∧ wellFormedCnodeN n cs) ↔ (∃ cs, obj = .cnode n cs ∧ wellFormedCnodeN n cs)) → + r ∈ ((Pure.pure o >>= fun o' => setObject (Z := Unit) p o') σ1).1 → + ∃ o o', σ1.kheap p = some o ∧ r.2 = { σ1 with kheap := Function.update σ1.kheap p (some o') } ∧ + (∀ b a, Fl4SpecA64.capOf o' b = some a → (b = cref ∧ a = cap) ∨ Fl4SpecA64.capOf o b = some a) ∧ + ∀ n, (∃ cs, o' = .cnode n cs ∧ wellFormedCnodeN n cs) ↔ (∃ cs, o = .cnode n cs ∧ wellFormedCnodeN n cs) := by + intro o hc hs h + obtain ⟨y, hy, h⟩ := mem_bindA.1 h + obtain rfl : y = (o, σ1) := hy + obtain ⟨o0, -, hr2⟩ := setObject_mem h + exact ⟨obj, o, e2, hr2, hc, hs⟩ + have kfail : ∀ f : KernelObject → NonDet (State Unit) Unit, r ∈ ((NonDet.fail >>= f) σ1).1 → False := by + intro f h + obtain ⟨y, hy, -⟩ := mem_bindA.1 h + exact absurd hy (Set.notMem_empty _) + have ntcb : ∀ t t' : Tcb, ∀ n, (∃ cs, KernelObject.tcb t' = .cnode n cs ∧ wellFormedCnodeN n cs) ↔ + (∃ cs, KernelObject.tcb t = .cnode n cs ∧ wellFormedCnodeN n cs) := + fun _ _ _ => ⟨fun ⟨_, e, _⟩ => (by cases e), fun ⟨_, e, _⟩ => (by cases e)⟩ + cases obj with + | cnode sz cn => + simp only at hr + split_ifs at hr with hd + · refine key (.cnode sz (Function.update cn cref (some cap))) (fun b a h => upd_or h) (fun n => ?_) hr + constructor + · rintro ⟨cs, e, hw⟩ + cases e + refine ⟨cn, rfl, ?_⟩ + unfold wellFormedCnodeN at hw ⊢ + rw [← hw, dom_updA hd.1] + · rintro ⟨cs, e, hw⟩ + cases e + refine ⟨_, rfl, ?_⟩ + unfold wellFormedCnodeN at hw ⊢ + rw [← hw, dom_updA hd.1] + · exact (kfail _ hr).elim + | tcb t => + simp only at hr + split_ifs at hr <;> + first + | exact (kfail _ hr).elim + | (refine key _ (fun b a h => ?_) (ntcb _ _) hr + subst_vars + exact tcbMap_or (fun b hb => by simp [Fl4SpecA64.tcbCnodeMap, Function.update_apply, hb]) + (by simp [Fl4SpecA64.tcbCnodeMap, tcbIdx_eq, Fl4SpecA64.tcbStage2Slot, + Fl4SpecA64.tcbHighVSpaceSlot]) h) + | _ => + try simp only at hr + exact (kfail _ hr).elim + +/-- **`set_cap` keeps `SpecIrqA`** (gen 4) for a cap that may be written into the slot (`CapOkA`). -/ +theorem setCap_irq {nd : Irq → ObjRef} {cap : Cap} {sl : CslotPtr} (hc : CapOkA nd sl cap) : + NdIrq nd (Fl4SpecA64.setCap (Z := Unit) cap sl) := by + obtain ⟨p, cref⟩ := sl + intro σ hn hI r hr + obtain ⟨o, o', ho, hr2, hcap, hsh⟩ := setCap_memA hr + have hN : r.2.interruptIrqNode = σ.interruptIrqNode := by rw [hr2] + have hcaps : ∀ q o2 b a, r.2.kheap q = some o2 → Fl4SpecA64.capOf o2 b = some a → + (q = p ∧ a = cap) ∨ ∃ o1, σ.kheap q = some o1 ∧ Fl4SpecA64.capOf o1 b = some a := by + intro q o2 b a hq hb + rw [hr2] at hq + by_cases e : q = p + · subst e + simp only [Function.update_self, Option.some.injEq] at hq + subst hq + rcases hcap b a hb with ⟨-, rfl⟩ | hb' + · exact Or.inl ⟨rfl, rfl⟩ + · exact Or.inr ⟨o, ho, hb'⟩ + · simp only [Function.update_of_ne e] at hq + exact Or.inr ⟨o2, hq, hb⟩ + have htab : ∀ n q, Fl4SpecA64.capTableAt n q r.2 ↔ Fl4SpecA64.capTableAt n q σ := by + intro n q + unfold Fl4SpecA64.capTableAt + rw [hr2] + by_cases e : q = p + · subst e; simp only [Function.update_self, Option.some.injEq, ho]; exact hsh n + · simp [Function.update_of_ne e] + refine ⟨hN.trans hn, ⟨⟨⟨fun i j hi hj e => hI.node.1.1 i j hi hj (by rwa [hN] at e), fun irq hi => ?_⟩, + fun irq hi n ht => ?_⟩, fun irq hi o2 b a hq hb => ?_, fun q o2 b a hq hb => ?_⟩⟩ + · rw [htab, hN]; exact hI.node.1.2 irq hi + · rw [htab, hN] at ht; exact hI.node.2 irq hi n ht + · rw [hN] at hq + rcases hcaps _ o2 b a hq hb with ⟨e, rfl⟩ | ⟨o1, h1, h2⟩ + · exact hc.1 irq hi (by rw [← hn]; exact e.symm) + · exact hI.slot irq hi o1 b a h1 h2 + · rw [hN] + rcases hcaps q o2 b a hq hb with ⟨-, rfl⟩ | ⟨o1, h1, h2⟩ + · rw [hn]; exact hc.2 + · exact hI.ref q o1 b a h1 h2 + +/-- `set_untyped_cap_as_full` of the source's own cap keeps `SpecIrqA`: an IRQ-node slot holds no untyped cap. -/ +theorem setUntypedCapAsFull_irq {nd : Irq → ObjRef} {srcCap newCap : Cap} {src : CslotPtr} {σ : State Unit} + {o : KernelObject} (ho : σ.kheap src.1 = some o) (hco : Fl4SpecA64.capOf o src.2 = some srcCap) : + NdIrqAt nd (setUntypedCapAsFull (Z := Unit) srcCap newCap src) σ := by + intro hn hI + unfold setUntypedCapAsFull + split_ifs with h + · refine setCap_irq ⟨fun irq hi e => ?_, ?_⟩ σ hn hI + · exfalso + rcases hI.slot irq hi o src.2 srcCap (by rw [hn, ← e]; exact ho) hco with rfl | ⟨_, _, _, rfl⟩ <;> + simp [isUntypedCap] at h + · obtain ⟨hu, -⟩ := h + cases srcCap <;> simp [isUntypedCap] at hu + rintro ⟨_, _, _, _, e⟩ + simp [freeIndexUpdate] at e + · exact NdIrq.keep (ndk_pure _) σ hn hI + +/-- One step of a leaf for `NdIrq`. -/ +syntax "irq_step" : tactic +macro_rules + | `(tactic| irq_step) => `(tactic| first + | exact NdIrq.keep (ndk_pure _) | exact NdIrq.keep (ndk_pure' _) | exact NdIrq.keep (ndk_gets _) + | exact NdIrq.keep ndk_get | exact NdIrq.keep (ndk_assert _) | exact NdIrq.keep (ndk_getCap _) + | exact NdIrq.keep (ndk_setCdt _) | exact NdIrq.keep (ndk_updateCdt _) | exact NdIrq.keep (ndk_setOriginal _ _) + | exact NdIrq.keep (ndk_doExtendedOp _) | exact NdIrq.keep (ndk_postCapDeletion _) + | exact setCap_irq (CapOkA.null _ _) | exact setCap_irq (by assumption) + | (refine NdIrq.bind ?_ (fun _ => ?_))) + +/-- `cap_insert` keeps `SpecIrqA` for a new cap that may be written into the destination. -/ +theorem capInsert_irq {nd : Irq → ObjRef} {c : Cap} {src dst : CslotPtr} (hc : CapOkA nd dst c) : + NdIrq nd (capInsert (Z := Unit) c src dst) := by + intro σ + unfold capInsert + refine NdIrqAt.bind (NdIrq.keep (ndk_getCap _) σ) fun x hx => ?_ + obtain ⟨e, o, ho, hco⟩ := getCap_memA hx + rw [e] + refine NdIrqAt.bind (NdIrq.keep (ndk_pure _) σ) fun y hy => ?_ + obtain rfl := pure_memA hy + refine NdIrqAt.bind (NdIrq.keep (ndk_getCap _) σ) fun z hz => ?_ + rw [(getCap_memA hz).1] + refine NdIrqAt.bind (NdIrq.keep (ndk_assert _) σ) fun w hw => ?_ + obtain ⟨-, rfl⟩ := hw + refine NdIrqAt.bind (setUntypedCapAsFull_irq ho hco) fun x' _ => (?_ : NdIrq nd _) x'.2 + repeat irq_step + +/-- `cap_move` keeps `SpecIrqA` for a new cap that may be written into the destination. -/ +theorem capMove_irq {nd : Irq → ObjRef} {c : Cap} {src dst : CslotPtr} (hc : CapOkA nd dst c) : + NdIrq nd (capMove (Z := Unit) c src dst) := by + unfold capMove; repeat irq_step + +/-- `cap_swap` keeps `SpecIrqA` for new caps that may be written into the slots they go to. -/ +theorem capSwap_irq {nd : Irq → ObjRef} {c1 c2 : Cap} {s1 s2 : CslotPtr} (h1 : CapOkA nd s1 c2) + (h2 : CapOkA nd s2 c1) : NdIrq nd (capSwap (Z := Unit) c1 s1 c2 s2) := by + unfold capSwap; repeat irq_step + +/-- `cap_swap_for_delete` of two slots off the IRQ node keeps `SpecIrqA` (x86 `SpecGlob.swap`). -/ +theorem capSwapForDelete_irq {nd : Irq → ObjRef} {s1 s2 : CslotPtr} (h1 : OffNodeA nd s1.1) + (h2 : OffNodeA nd s2.1) : NdIrq nd (capSwapForDelete (Z := Unit) s1 s2) := by + intro σ hn hI + unfold capSwapForDelete NonDet.when + split_ifs + · refine NdIrqAt.bind (NdIrq.keep (ndk_getCap _) σ) (fun x hx => ?_) hn hI + obtain ⟨e, o, ho, hco⟩ := getCap_memA hx + rw [e] + refine NdIrqAt.bind (NdIrq.keep (ndk_getCap _) σ) fun y hy => ?_ + obtain ⟨e', o', ho', hco'⟩ := getCap_memA hy + rw [e'] + refine capSwap_irq ⟨fun irq hi e => absurd e (h1 irq hi), ?_⟩ ⟨fun irq hi e => absurd e (h2 irq hi), ?_⟩ σ + · rw [← hn]; exact hI.ref _ _ _ _ ho' hco' + · rw [← hn]; exact hI.ref _ _ _ _ ho hco + · exact NdIrq.keep (ndk_pure' _) σ hn hI + +/-- `empty_slot` keeps `SpecIrqA` (only a null cap is written). -/ +theorem emptySlot_irq {nd : Irq → ObjRef} (sl : CslotPtr) (info : Cap) : + NdIrq nd (emptySlot (Z := Unit) sl info) := by + unfold emptySlot + refine NdIrq.bind (NdIrq.keep (ndk_getCap _)) fun cap => ?_ + split + · exact NdIrq.keep (ndk_pure _) + · repeat irq_step + +/-- **`SInvXA` across `cap_insert`.** -/ +theorem SInvXA.capInsert {σ : State Unit} {m : McState} {n : Nat} (h : SInvXA σ m n) {c : Cap} + {src dst : CslotPtr} (hc : CapOkA σ.interruptIrqNode dst c) {r : Unit × State Unit} + (hr : r ∈ (Fl4SpecA64.capInsert (Z := Unit) c src dst σ).1) : SInvXA r.2 m n := + h.of_fr (capInsert_ndr c src dst σ r hr) (capInsert_irq hc σ rfl h.irqN r hr).2 + +/-- **`SInvXA` across `cap_move`.** -/ +theorem SInvXA.capMove {σ : State Unit} {m : McState} {n : Nat} (h : SInvXA σ m n) {c : Cap} + {src dst : CslotPtr} (hc : CapOkA σ.interruptIrqNode dst c) {r : Unit × State Unit} + (hr : r ∈ (Fl4SpecA64.capMove (Z := Unit) c src dst σ).1) : SInvXA r.2 m n := + h.of_fr (capMove_ndr c src dst σ r hr) (capMove_irq hc σ rfl h.irqN r hr).2 + +/-- **`SInvXA` across `cap_swap`.** -/ +theorem SInvXA.capSwap {σ : State Unit} {m : McState} {n : Nat} (h : SInvXA σ m n) {c1 c2 : Cap} + {s1 s2 : CslotPtr} (h1 : CapOkA σ.interruptIrqNode s1 c2) (h2 : CapOkA σ.interruptIrqNode s2 c1) + {r : Unit × State Unit} (hr : r ∈ (Fl4SpecA64.capSwap (Z := Unit) c1 s1 c2 s2 σ).1) : SInvXA r.2 m n := + h.of_fr (capSwap_ndr c1 s1 c2 s2 σ r hr) (capSwap_irq h1 h2 σ rfl h.irqN r hr).2 + +/-- (gen 4) **`SInvXA` across `cap_swap_for_delete`** of two slots off the IRQ node. -/ +theorem SInvXA.capSwapForDelete {σ : State Unit} {m : McState} {n : Nat} (h : SInvXA σ m n) {s1 s2 : CslotPtr} + (h1 : OffNodeA σ.interruptIrqNode s1.1) (h2 : OffNodeA σ.interruptIrqNode s2.1) {r : Unit × State Unit} + (hr : r ∈ (Fl4SpecA64.capSwapForDelete (Z := Unit) s1 s2 σ).1) : SInvXA r.2 m n := + h.of_fr (capSwapForDelete_ndr s1 s2 σ r hr) (capSwapForDelete_irq h1 h2 σ rfl h.irqN r hr).2 + +/-- **`SInvXA` across `empty_slot`.** -/ +theorem SInvXA.emptySlot {σ : State Unit} {m : McState} {n : Nat} (h : SInvXA σ m n) {sl : CslotPtr} + {info : Cap} {r : Unit × State Unit} (hr : r ∈ (Fl4SpecA64.emptySlot (Z := Unit) sl info σ).1) : + SInvXA r.2 m n := h.of_fr (emptySlot_ndr sl info σ r hr) (emptySlot_irq sl info σ rfl h.irqN r hr).2 + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/SpecFrameCalcA.lean b/proofs/Fl4ProofsA64/P3/SpecFrameCalcA.lean new file mode 100644 index 000000000..15d456742 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/SpecFrameCalcA.lean @@ -0,0 +1,420 @@ +/- +P3 (aarch64, A64-P3 Stage-3 integration; Gate-2 ruling 00:33Z A–C, queue item 1): **a frame calculus for the spec +monad**, so that the *spec* effect of a scheduler step needs no per-step explicit state. + +* `PresR R m`: every result state of `m` is `R`-related to its start state (`PreO R`: `R` is a preorder). Closed under + `pure`, `bind`, the state readers, `modify`, `when`, `ite`, `mapM` and `whileLoop` (`PresR.*`). +* `SchedFrameA σ σ'` = `SpecSchedA σ σ'` (`P3/Stage3SchedPresA`) ∧ `SSzA σ σ'` ∧ the machine's user memory is kept. It is a + preorder (`SpecSchedA.trans`). +* `SPresA m` = `PresR SchedFrameA m`. The writing primitives: `updateSchedContext`, `threadSet` (TCB function + keeping the cap slots and VCPU links: `TcbSlotFn`), `asUser`, `setSchedulerAction`, ready / release queue updates, + `doExtendedOp`, and `doMachineOp` of a user-memory-preserving machine operation (`MPresA`). + +No premise; new module, no existing statement changed. +-/ +import Fl4ProofsA64.P3.Stage3SchedPresA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec + +noncomputable section + +/-- A preorder (the frame relations below). -/ +class PreO {σ : Type} (R : σ → σ → Prop) : Prop where + refl : ∀ s, R s s + trans : ∀ {a b c}, R a b → R b c → R a c + +/-- Every result state of `m` is `R`-related to the start state. -/ +def PresR {σ α : Type} (R : σ → σ → Prop) (m : NonDet σ α) : Prop := + ∀ s, ∀ x ∈ (m s).1, R s x.2 + +namespace PresR +variable {σ α β : Type} {R : σ → σ → Prop} [PreO R] + +theorem of_keep {m : NonDet σ α} (h : ∀ s, ∀ x ∈ (m s).1, x.2 = s) : PresR R m := fun s x hx => by + rw [h s x hx]; exact PreO.refl s + +theorem pure (a : α) : PresR R (NonDet.pure a : NonDet σ α) := + of_keep fun s x hx => by + have : x = (a, s) := hx + rw [this] + +theorem bind {m : NonDet σ α} {f : α → NonDet σ β} (hm : PresR R m) (hf : ∀ a, PresR R (f a)) : + PresR R (NonDet.bind m f) := fun s x hx => by + obtain ⟨y, hy, hx'⟩ := hx + exact PreO.trans (hm s y hy) (hf y.1 y.2 x hx') + +theorem bind_pure (a : α) (f : α → NonDet σ β) (h : PresR R (f a)) : + PresR R (NonDet.bind (NonDet.pure a) f) := fun s x hx => by + obtain ⟨y, hy, hx'⟩ := hx + have e : y = (a, s) := hy + subst e + exact h s x hx' + +theorem get : PresR R (NonDet.get : NonDet σ σ) := + of_keep fun s x hx => by + have : x = (s, s) := hx + rw [this] + +theorem gets (f : σ → α) : PresR R (NonDet.gets f) := + of_keep fun s x hx => by + have : x = (f s, s) := hx + rw [this] + +theorem fail : PresR R (NonDet.fail : NonDet σ α) := fun s x hx => absurd hx (Set.notMem_empty _) + +theorem assert (P : Prop) : PresR R (NonDet.assert P : NonDet σ Unit) := + of_keep fun s x hx => by + obtain ⟨-, h⟩ := hx + rw [h] + +theorem assertOpt (o : Option α) : PresR R (NonDet.assertOpt o : NonDet σ α) := by + cases o + · exact fail + · exact pure _ + +theorem getsThe (f : σ → Option α) : PresR R (NonDet.getsThe f) := + fun s x hx => by + change x ∈ (NonDet.assertOpt (f s) s).1 at hx + exact (assertOpt (R := R) (f s)) s x hx + +theorem selectF (S : Set α × Prop) : PresR R (NonDet.selectF S : NonDet σ α) := + of_keep fun s x hx => hx.2 + +theorem modify (f : σ → σ) (h : ∀ s, R s (f s)) : PresR R (NonDet.modify f) := fun s x hx => by + have : x = ((), f s) := hx + rw [this]; exact h s + +theorem when {P : Prop} {d : Decidable P} {m : NonDet σ Unit} (h : PresR R m) : + PresR R (@NonDet.when σ P d m) := by + unfold NonDet.when + by_cases hP : P + · simp only [hP, if_true]; exact h + · simp only [hP, if_false]; exact pure _ + +theorem unless' {P : Prop} {d : Decidable P} {m : NonDet σ Unit} (h : PresR R m) : + PresR R (@NonDet.unless σ P d m) := by + unfold NonDet.unless + by_cases hP : P + · simp only [hP, if_true]; exact pure _ + · simp only [hP, if_false]; exact h + +theorem ite' {P : Prop} {d : Decidable P} {a b : NonDet σ α} (ha : PresR R a) (hb : PresR R b) : + PresR R (@ite _ P d a b) := by + by_cases hP : P + · simp only [hP, if_true]; exact ha + · simp only [hP, if_false]; exact hb + +theorem mapM {γ : Type} {f : γ → NonDet σ β} (hf : ∀ a, PresR R (f a)) (l : List γ) : + PresR R (NonDet.mapM f l) := by + induction l with + | nil => exact pure _ + | cons a l ih => + show PresR R (NonDet.bind (f a) fun b => NonDet.bind (NonDet.mapM f l) fun bs => NonDet.pure (b :: bs)) + exact bind (hf a) fun b => bind ih fun bs => pure _ + +theorem whileLoop {C : α → σ → Prop} {B : α → NonDet σ α} (hB : ∀ a, PresR R (B a)) (r : α) : + PresR R (NonDet.whileLoop C B r) := by + intro s x hx + have key : ∀ r s x, NonDet.WhileResults C B r s x → R s x.2 := by + intro r s x h + induction h with + | done _ => exact PreO.refl _ + | step hC hb _ ih => exact PreO.trans (hB _ _ _ hb) ih + exact key r s x hx + +end PresR + +/-! ## The scheduler frame -/ + +theorem TcbSlotKeepA.refl (x : Fl4SpecA64.Tcb) : TcbSlotKeepA x x := ⟨rfl, rfl, rfl⟩ + +theorem TcbSlotKeepA.trans {x y z : Fl4SpecA64.Tcb} (h1 : TcbSlotKeepA x y) (h2 : TcbSlotKeepA y z) : + TcbSlotKeepA x z := ⟨h2.1.trans h1.1, h2.2.1.trans h1.2.1, h2.2.2.trans h1.2.2⟩ + +theorem SpecSchedA.refl (σ : Fl4SpecA64.State Unit) : SpecSchedA σ σ := + ⟨fun _ => Or.inl rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ + +theorem SpecSchedA.trans {σ1 σ2 σ3 : Fl4SpecA64.State Unit} (h1 : SpecSchedA σ1 σ2) (h2 : SpecSchedA σ2 σ3) : + SpecSchedA σ1 σ3 where + kh p := by + rcases h1.kh p with e1 | ⟨x, x', a, b, k⟩ | ⟨o, o', a, b, f⟩ + · rcases h2.kh p with e2 | ⟨x, x', a, b, k⟩ | ⟨o, o', a, b, f⟩ + · exact Or.inl (e2.trans e1) + · exact Or.inr (Or.inl ⟨x, x', by rw [← e1]; exact a, b, k⟩) + · exact Or.inr (Or.inr ⟨o, o', by rw [← e1]; exact a, b, f⟩) + · rcases h2.kh p with e2 | ⟨u, u', a2, b2, k2⟩ | ⟨o2, o3, a2, b2, f2⟩ + · exact Or.inr (Or.inl ⟨x, x', a, e2.trans b, k⟩) + · rw [b] at a2 + obtain rfl : x' = u := by injection a2 with a2; injection a2 + exact Or.inr (Or.inl ⟨x, u', a, b2, k.trans k2⟩) + · rw [b] at a2; cases a2; exact absurd f2.kind FrameKindA.tcb_not + · rcases h2.kh p with e2 | ⟨u, u', a2, b2, k2⟩ | ⟨o2, o3, a2, b2, f2⟩ + · exact Or.inr (Or.inr ⟨o, o', a, e2.trans b, f⟩) + · rw [b] at a2; cases a2; exact absurd f.kind' FrameKindA.tcb_not + · rw [b] at a2; cases a2 + exact Or.inr (Or.inr ⟨o, o3, a, b2, f.trans f2⟩) + irqS := h2.irqS.trans h1.irqS + irqN := h2.irqN.trans h1.irqN + asid := h2.asid.trans h1.asid + vcur := h2.vcur.trans h1.vcur + cdt := h2.cdt.trans h1.cdt + orig := h2.orig.trans h1.orig + +/-- **The scheduler spec frame**: `SpecSchedA`, SC sizes kept, user memory kept. -/ +def SchedFrameA (σ σ' : Fl4SpecA64.State Unit) : Prop := + SpecSchedA σ σ' ∧ SSzA σ σ' ∧ σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory + +instance : PreO SchedFrameA where + refl σ := ⟨SpecSchedA.refl σ, SSzA.refl σ, rfl⟩ + trans h1 h2 := ⟨h1.1.trans h2.1, SSzA.trans h1.2.1 h2.2.1, h2.2.2.trans h1.2.2⟩ + +/-- A step that changes no heap object, IRQ / ASID / CDT / original-cap state, or user memory. -/ +theorem SchedFrameA.of_eq {σ σ' : Fl4SpecA64.State Unit} (hk : σ'.kheap = σ.kheap) + (h1 : σ'.interruptStates = σ.interruptStates) (h2 : σ'.interruptIrqNode = σ.interruptIrqNode) + (h3 : σ'.archState.armAsidTable = σ.archState.armAsidTable) (h4 : σ'.cdt = σ.cdt) + (h5 : σ'.isOriginalCap = σ.isOriginalCap) (h6 : σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory) + (h7 : σ'.archState.armCurrentVcpu = σ.archState.armCurrentVcpu) : SchedFrameA σ σ' := + ⟨⟨fun p => Or.inl (by rw [hk]), h1, h2, h3, h7, h4, h5⟩, SSzA.of_kheap hk, h6⟩ + +/-- Replace a TCB by one with the same cap slots and VCPU links. -/ +theorem SchedFrameA.upd_tcb {σ : Fl4SpecA64.State Unit} {p : Fl4SpecA64.ObjRef} {x x' : Fl4SpecA64.Tcb} + (h : σ.kheap p = some (.tcb x)) (k : TcbSlotKeepA x x') : + SchedFrameA σ { σ with kheap := Function.update σ.kheap p (some (.tcb x')) } := by + refine ⟨⟨fun q => ?_, rfl, rfl, rfl, rfl, rfl, rfl⟩, fun q y n hq => ?_, rfl⟩ + · by_cases e : q = p + · subst e + exact Or.inr (Or.inl ⟨x, x', h, by simp, k⟩) + · exact Or.inl (by simp [Function.update_of_ne e]) + · by_cases e : q = p + · subst e + simp at hq + · exact ⟨y, by simpa [Function.update_of_ne e] using hq⟩ + +/-- Replace an SC by one of the same size. -/ +theorem SchedFrameA.upd_sc {σ : Fl4SpecA64.State Unit} {p : Fl4SpecA64.ObjRef} {y y' : Fl4SpecA64.SchedContext} + {n : Nat} (h : σ.kheap p = some (.schedContext y n)) : + SchedFrameA σ { σ with kheap := Function.update σ.kheap p (some (.schedContext y' n)) } := by + refine ⟨⟨fun q => ?_, rfl, rfl, rfl, rfl, rfl, rfl⟩, fun q z m hq => ?_, rfl⟩ + · by_cases e : q = p + · subst e + exact Or.inr (Or.inr ⟨.schedContext y n, .schedContext y' n, h, by simp, Or.inl ⟨y, n, y', n, rfl, rfl⟩⟩) + · exact Or.inl (by simp [Function.update_of_ne e]) + · by_cases e : q = p + · subst e + simp at hq + obtain ⟨-, rfl⟩ := hq + exact ⟨y, h⟩ + · exact ⟨z, by simpa [Function.update_of_ne e] using hq⟩ + +/-- Scheduler-frame preservation of a spec computation. -/ +abbrev SPresA {α : Type} (m : SMonad Unit α) : Prop := PresR SchedFrameA m + +/-- A TCB function keeping the cap slots and VCPU links. -/ +class TcbSlotFn (f : Fl4SpecA64.Tcb → Fl4SpecA64.Tcb) : Prop where + keep : ∀ x, TcbSlotKeepA x (f x) + +/-- Instance form of `SPresA` (so named spec functions are found by type-class search). -/ +class SPresInst {α : Type} (m : SMonad Unit α) : Prop where + pres : SPresA m + +instance {α β : Type} {m : SMonad Unit α} {f : α → SMonad Unit β} [hm : SPresInst m] + [hf : ∀ a, SPresInst (f a)] : SPresInst (m >>= f) := + ⟨PresR.bind hm.pres fun a => (hf a).pres⟩ + +instance {α : Type} (a : α) : SPresInst (pure a : SMonad Unit α) := ⟨PresR.pure a⟩ + +instance {α : Type} (f : State Unit → α) : SPresInst (NonDet.gets f : SMonad Unit α) := ⟨PresR.gets f⟩ + +instance : SPresInst (NonDet.get : SMonad Unit (State Unit)) := ⟨PresR.get⟩ + +instance {α : Type} (f : State Unit → Option α) : SPresInst (NonDet.getsThe f : SMonad Unit α) := + ⟨PresR.getsThe f⟩ + +instance {α : Type} (o : Option α) : SPresInst (NonDet.assertOpt o : SMonad Unit α) := ⟨PresR.assertOpt o⟩ + +instance (P : Prop) : SPresInst (NonDet.assert P : SMonad Unit Unit) := ⟨PresR.assert P⟩ + +instance {α : Type} : SPresInst (NonDet.fail : SMonad Unit α) := ⟨PresR.fail⟩ + +instance {P : Prop} {d : Decidable P} {m : SMonad Unit Unit} [h : SPresInst m] : + SPresInst (@NonDet.when (State Unit) P d m) := ⟨PresR.when h.pres⟩ + +instance {P : Prop} {d : Decidable P} {m : SMonad Unit Unit} [h : SPresInst m] : + SPresInst (@NonDet.unless (State Unit) P d m) := ⟨PresR.unless' h.pres⟩ + +instance {α : Type} {P : Prop} {d : Decidable P} {a b : SMonad Unit α} [ha : SPresInst a] [hb : SPresInst b] : + SPresInst (@ite _ P d a b) := ⟨PresR.ite' ha.pres hb.pres⟩ + +instance {β γ : Type} {f : γ → SMonad Unit β} [hf : ∀ a, SPresInst (f a)] (l : List γ) : + SPresInst (NonDet.mapM f l) := ⟨PresR.mapM (fun a => (hf a).pres) l⟩ + +instance {α : Type} {C : α → State Unit → Prop} {B : α → SMonad Unit α} [hB : ∀ a, SPresInst (B a)] (r : α) : + SPresInst (NonDet.whileLoop C B r) := ⟨PresR.whileLoop (fun a => (hB a).pres) r⟩ + +instance {α : Type} (S : Set α × Prop) : SPresInst (NonDet.selectF S : SMonad Unit α) := ⟨PresR.selectF S⟩ + +instance {α : Type} {f : α → SMonad Unit Unit} [hf : ∀ a, SPresInst (f a)] (o : Option α) : + SPresInst (NonDet.maybeM f o) := by + cases o + · exact ⟨PresR.pure _⟩ + · exact hf _ + +/-- A state modification that keeps the heap, IRQ / ASID / CDT / original-cap state and user memory. -/ +theorem SPresA.modify_fix (f : State Unit → State Unit) (hk : ∀ σ, (f σ).kheap = σ.kheap) + (h1 : ∀ σ, (f σ).interruptStates = σ.interruptStates) (h2 : ∀ σ, (f σ).interruptIrqNode = σ.interruptIrqNode) + (h3 : ∀ σ, (f σ).archState.armAsidTable = σ.archState.armAsidTable) (h4 : ∀ σ, (f σ).cdt = σ.cdt) + (h5 : ∀ σ, (f σ).isOriginalCap = σ.isOriginalCap) + (h6 : ∀ σ, (f σ).machineState.underlyingMemory = σ.machineState.underlyingMemory) + (h7 : ∀ σ, (f σ).archState.armCurrentVcpu = σ.archState.armCurrentVcpu) : + SPresA (NonDet.modify f : SMonad Unit Unit) := + PresR.modify f fun σ => SchedFrameA.of_eq (hk σ) (h1 σ) (h2 σ) (h3 σ) (h4 σ) (h5 σ) (h6 σ) (h7 σ) + +/-- **The structural tactic**: unfold the binds / conditionals of a spec computation and close the leaves by +type-class search (`SPresInst`) or by `SPresA.modify_fix` (a record update off the frame fields). -/ +macro "spres" : tactic => `(tactic| + repeat' (first + | exact SPresInst.pres + | refine PresR.bind_pure _ _ ?_ + | refine PresR.bind ?_ (fun _ => ?_) + | refine PresR.when ?_ + | refine PresR.unless' ?_ + | refine PresR.ite' ?_ ?_ + | (refine SPresA.modify_fix _ (fun _ => rfl) (fun _ => rfl) (fun _ => rfl) (fun _ => rfl) (fun _ => rfl) + (fun _ => rfl) (fun _ => rfl) (fun _ => rfl)) + | dsimp only + | split)) + +/-! ## The writing primitives -/ + +section prims +variable {σ : Fl4SpecA64.State Unit} + +theorem getTcb_kheapA {t : Fl4SpecA64.ObjRef} {x : Fl4SpecA64.Tcb} (h : getTcb (Z := Unit) t σ = some x) : + σ.kheap t = some (.tcb x) := by + unfold getTcb readObject at h + cases hk : σ.kheap t with + | none => rw [hk] at h; cases h + | some o => + rw [hk] at h + cases o <;> simp at h + rw [h] + +theorem setObject_memA {p : Fl4SpecA64.ObjRef} {o : Fl4SpecA64.KernelObject} {r : Unit × State Unit} + (h : r ∈ (setObject (Z := Unit) p o σ).1) : + ∃ ko, σ.kheap p = some ko ∧ r.2 = { σ with kheap := Function.update σ.kheap p (some o) } := by + obtain ⟨y1, hy1, h⟩ := h + have hy1' : y1 ∈ (NonDet.assertOpt (σ.kheap p) σ).1 := hy1 + cases hk : σ.kheap p with + | none => rw [hk] at hy1'; exact absurd hy1' (Set.notMem_empty _) + | some ko => + rw [hk] at hy1' + have e1 : y1 = (ko, σ) := hy1' + subst e1 + obtain ⟨y2, hy2, h⟩ := h + have e2 : y2 = ((), σ) := hy2.2 + subst e2 + obtain ⟨y3, hy3, h⟩ := h + have e3 : y3 = (σ, σ) := hy3 + subst e3 + have e4 : r = ((), { σ with kheap := Function.update σ.kheap p (some o) }) := h + exact ⟨ko, rfl, by rw [e4]⟩ + +theorem threadSet_presA (f : Fl4SpecA64.Tcb → Fl4SpecA64.Tcb) [TcbSlotFn f] (t : Fl4SpecA64.ObjRef) : + SPresA (threadSet (Z := Unit) f t) := by + intro σ r hr + obtain ⟨y1, hy1, h⟩ := hr + have hy1' : y1 ∈ (NonDet.assertOpt (getTcb (Z := Unit) t σ) σ).1 := hy1 + cases hg : getTcb (Z := Unit) t σ with + | none => rw [hg] at hy1'; exact absurd hy1' (Set.notMem_empty _) + | some x => + rw [hg] at hy1' + have e1 : y1 = (x, σ) := hy1' + subst e1 + obtain ⟨ko, hko, hr2⟩ := setObject_memA h + have hx := getTcb_kheapA hg + rw [hr2] + exact SchedFrameA.upd_tcb hx (TcbSlotFn.keep x) + +theorem updateSchedContext_presA (f : Fl4SpecA64.SchedContext → Fl4SpecA64.SchedContext) (p : Fl4SpecA64.ObjRef) : + SPresA (updateSchedContext (Z := Unit) p f) := by + intro σ r hr + obtain ⟨y1, hy1, h⟩ := hr + have hy1' : y1 ∈ (NonDet.assertOpt (σ.kheap p) σ).1 := hy1 + cases hk : σ.kheap p with + | none => rw [hk] at hy1'; exact absurd hy1' (Set.notMem_empty _) + | some ko => + rw [hk] at hy1' + have e1 : y1 = (ko, σ) := hy1' + subst e1 + cases ko with + | schedContext sc n => + obtain ⟨ko', hko, hr2⟩ := setObject_memA h + rw [hr2] + exact SchedFrameA.upd_sc hk + | _ => exact absurd h (Set.notMem_empty _) + +theorem asUser_presA {α : Type} (t : Fl4SpecA64.ObjRef) (f : UserMonad α) : SPresA (asUser (Z := Unit) t f) := by + intro σ r hr + obtain ⟨y1, hy1, hr⟩ := hr + have hy1' : y1 ∈ (NonDet.assertOpt (getTcb (Z := Unit) t σ) σ).1 := hy1 + cases hg : getTcb (Z := Unit) t σ with + | none => rw [hg] at hy1'; exact absurd hy1' (Set.notMem_empty _) + | some x => + rw [hg] at hy1' + have e1 : y1 = (x, σ) := hy1' + subst e1 + obtain ⟨y2, hy2, hr⟩ := hr + have e2 : y2 = (archTcbContextGet x.tcbArch, σ) := hy2 + subst e2 + obtain ⟨⟨⟨a, uc'⟩, s3⟩, hy3, hr⟩ := hr + have e3 : s3 = σ := hy3.2 + subst e3 + obtain ⟨y4, hy4, hr⟩ := hr + have e4 : y4 = ({ x with tcbArch := archTcbContextSet uc' x.tcbArch }, s3) := hy4 + subst e4 + obtain ⟨y5, hy5, hr⟩ := hr + obtain ⟨ko, hko, hr5⟩ := setObject_memA hy5 + have hx := getTcb_kheapA hg + have e6 : r = (a, y5.2) := hr + rw [e6, hr5] + exact SchedFrameA.upd_tcb hx ⟨rfl, rfl, rfl⟩ + +theorem machineRestLift_umA {α : Type} (f : MachineRestMonad α) (ms : MachineState) (x : α × MachineState) + (hx : x ∈ (machineRestLift f ms).1) : x.2.underlyingMemory = ms.underlyingMemory := by + obtain ⟨y1, hy1, hx⟩ := hx + have e1 : y1 = (ms.machineStateRest, ms) := hy1 + subst e1 + obtain ⟨⟨⟨a, mr'⟩, s2⟩, hy2, hx⟩ := hx + have e2 : s2 = ms := hy2.2 + subst e2 + obtain ⟨y3, hy3, hx⟩ := hx + have e3 : y3 = ((), { s2 with machineStateRest := mr' }) := hy3 + subst e3 + have e4 : x = (a, { s2 with machineStateRest := mr' }) := hx + rw [e4] + +theorem doMachineOp_presA {α : Type} (mop : NonDet MachineState α) + (h : ∀ ms, ∀ x ∈ (mop ms).1, x.2.underlyingMemory = ms.underlyingMemory) : + SPresA (doMachineOp (Z := Unit) mop) := by + intro σ r hr + obtain ⟨y1, hy1, hr⟩ := hr + have e1 : y1 = (σ.machineState, σ) := hy1 + subst e1 + obtain ⟨⟨⟨a, ms'⟩, s2⟩, hy2, hr⟩ := hr + have hm : (a, ms') ∈ (mop σ.machineState).1 := hy2.1 + have e2 : s2 = σ := hy2.2 + subst e2 + obtain ⟨y3, hy3, hr⟩ := hr + have e3 : y3 = ((), { s2 with machineState := ms' }) := hy3 + subst e3 + have e4 : r = (a, { s2 with machineState := ms' }) := hr + rw [e4] + exact SchedFrameA.of_eq rfl rfl rfl rfl rfl rfl (h _ _ hm) rfl + +end prims + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/SpecFrameIpcA.lean b/proofs/Fl4ProofsA64/P3/SpecFrameIpcA.lean new file mode 100644 index 000000000..4a820305d --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/SpecFrameIpcA.lean @@ -0,0 +1,488 @@ +/- +P3 (aarch64, A64-P3 Stage-3 integration; queue item R1-R3, contract §10 (a)): **the IPC / notification spec functions keep +`SchedFrameA`**: `sendSignal`, `cancelIpc` (with `blockedCancelIpc`, `cancelSignal`, `replyRemoveTcb`), the queue +operations `tcbEpDequeue` / `tcbNtfnAppend` / ..., `replyPush`, `replyUnlinkTcb`, `schedContextDonate`, `maybeDonateSc`, +`updateWaitingNtfn`, `sendIpcBlocked`. Pure spec statements (no concrete state, no premise). New module. + +The calculus of `P3/SpecFrameCalcA` already allows notification / endpoint / reply / SC replacement (`FrameStepA`) and +TCBs with the same cap slots; the new leaves are the simple-object accessors. +-/ +import Fl4ProofsA64.P3.Stage3PartialSpecA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec + +noncomputable section + +instance {α : Type} (a : α) : SPresInst (NonDet.pure a : SMonad Unit α) := ⟨PresR.pure a⟩ + +/-! ## Simple objects (endpoint / notification / reply) -/ + +/-- A simple-object constructor: replacing one by another of its kind is a `FrameStepA`. -/ +class SimpleCtorA {α : Type} (C : α → KernelObject) : Prop where + step : ∀ a b, FrameStepA (C a) (C b) + nosc : ∀ a y n, C a ≠ .schedContext y n + notcb : ∀ a x, C a ≠ .tcb x + +instance : SimpleCtorA KernelObject.endpoint := + ⟨fun a b => Or.inr (Or.inr (Or.inl ⟨a, b, rfl, rfl⟩)), (fun _ _ _ h => by cases h), (fun _ _ h => by cases h)⟩ +instance : SimpleCtorA KernelObject.notification := + ⟨fun a b => Or.inr (Or.inl ⟨a, b, rfl, rfl⟩), (fun _ _ _ h => by cases h), (fun _ _ h => by cases h)⟩ +instance : SimpleCtorA KernelObject.reply := + ⟨fun a b => Or.inr (Or.inr (Or.inr ⟨a, b, rfl, rfl⟩)), (fun _ _ _ h => by cases h), (fun _ _ h => by cases h)⟩ + +/-- Replace a non-SC object by one that is a `FrameStepA` successor and not an SC. -/ +theorem SchedFrameA.upd_simple {σ : Fl4SpecA64.State Unit} {p : Fl4SpecA64.ObjRef} + {o o' : Fl4SpecA64.KernelObject} (h : σ.kheap p = some o) (f : FrameStepA o o') + (hn : ∀ y n, o' ≠ .schedContext y n) : + SchedFrameA σ { σ with kheap := Function.update σ.kheap p (some o') } := by + refine ⟨⟨fun q => ?_, rfl, rfl, rfl, rfl, rfl, rfl⟩, fun q y n hq => ?_, rfl⟩ + · by_cases e : q = p + · subst e + exact Or.inr (Or.inr ⟨o, o', h, by simp, f⟩) + · exact Or.inl (by simp [Function.update_of_ne e]) + · by_cases e : q = p + · subst e + have hq' : o' = .schedContext y n := by simpa using hq + exact absurd hq' (hn y n) + · exact ⟨y, by simpa [Function.update_of_ne e] using hq⟩ + +theorem setSimpleKo_memA {α : Type} [Nonempty α] (C : α → KernelObject) (ptr : ObjRef) (a : α) + {σ : Fl4SpecA64.State Unit} {r : Unit × Fl4SpecA64.State Unit} + (h : r ∈ (setSimpleKo (Z := Unit) C ptr a σ).1) : + ∃ y, σ.kheap ptr = some (C y) ∧ + r.2 = { σ with kheap := Function.update σ.kheap ptr (some (C a)) } := by + obtain ⟨y1, hy1, h⟩ := h + have hy1' : y1 ∈ (NonDet.assertOpt (σ.kheap ptr) σ).1 := hy1 + cases hk : σ.kheap ptr with + | none => rw [hk] at hy1'; exact absurd hy1' (Set.notMem_empty _) + | some ko => + rw [hk] at hy1' + have e1 : y1 = (ko, σ) := hy1' + subst e1 + obtain ⟨y2, hy2, h⟩ := h + have e2 : y2 = ((), σ) := hy2.2 + subst e2 + obtain ⟨y3, hy3, h⟩ := h + have e3 : y3 = ((), σ) := hy3.2 + subst e3 + have hex : ∃ y, C y = ko := by + by_contra hne + apply hy3.1 + unfold partialInv + rw [if_neg] + rintro ⟨y, hy, -⟩ + exact hne ⟨y, hy⟩ + obtain ⟨y, rfl⟩ := hex + obtain ⟨ko', hko', hr2⟩ := setObject_memA h + exact ⟨y, rfl, hr2⟩ + +theorem setSimpleKo_presA {α : Type} [Nonempty α] (C : α → KernelObject) [SimpleCtorA C] (ptr : ObjRef) (a : α) : + SPresA (setSimpleKo (Z := Unit) C ptr a) := by + intro σ r hr + obtain ⟨y, hk, hr2⟩ := setSimpleKo_memA C ptr a hr + rw [hr2] + exact SchedFrameA.upd_simple hk (SimpleCtorA.step y a) (SimpleCtorA.nosc a) + +instance {α : Type} [Nonempty α] (C : α → KernelObject) [SimpleCtorA C] (ptr : ObjRef) (a : α) : + SPresInst (setSimpleKo (Z := Unit) C ptr a) := ⟨setSimpleKo_presA C ptr a⟩ + +instance {α : Type} [Nonempty α] (C : α → KernelObject) (ptr : ObjRef) : + SPresInst (getSimpleKo (Z := Unit) C ptr) := ⟨by unfold getSimpleKo; spres⟩ + +instance {α : Type} [Nonempty α] (C : α → KernelObject) (f : α → Option ObjRef) (ptr : ObjRef) : + SPresInst (getSkObjRef (Z := Unit) C f ptr) := ⟨by unfold getSkObjRef; spres⟩ + +instance {α : Type} [Nonempty α] (C : α → KernelObject) [SimpleCtorA C] + (f : (Option ObjRef → Option ObjRef) → α → α) (ref : ObjRef) (new : Option ObjRef) : + SPresInst (updateSkObjRef (Z := Unit) C f ref new) := ⟨by unfold updateSkObjRef; spres⟩ + +/-! ## TCB writers -/ + +instance (f : Option Fault) : TcbSlotFn (fun tcb : Tcb => { tcb with tcbFault := f }) := ⟨fun _ => ⟨rfl, rfl, rfl⟩⟩ + +/-! ## Queue operations -/ + +instance {α β : Type} [Nonempty β] (t : α) (ts : List α) (f : α → RMonad Unit β) (R : β → β → Prop) : + SPresInst (orderedInsert (Z := Unit) t ts f R) := ⟨by unfold orderedInsert; spres⟩ +instance (t : ObjRef) (qs : List ObjRef) : SPresInst (tcbAppend (Z := Unit) t qs) := ⟨by unfold tcbAppend; spres⟩ +instance (t e : ObjRef) : SPresInst (tcbEpDequeue (Z := Unit) t e) := ⟨by unfold tcbEpDequeue; spres⟩ +instance (t e : ObjRef) : SPresInst (tcbNtfnDequeue (Z := Unit) t e) := ⟨by unfold tcbNtfnDequeue; spres⟩ +instance (t e : ObjRef) (b : Bool) : SPresInst (tcbEpAppend (Z := Unit) t e b) := ⟨by unfold tcbEpAppend; spres⟩ +instance (t e : ObjRef) : SPresInst (tcbNtfnAppend (Z := Unit) t e) := ⟨by unfold tcbNtfnAppend; spres⟩ + +/-! ## Scheduling-context donation and replies -/ + +instance (t : ObjRef) : SPresInst (tcbReleaseEnqueue (Z := Unit) t) := ⟨by unfold tcbReleaseEnqueue; spres⟩ +instance (p : ObjRef) : SPresInst (postpone (Z := Unit) p) := ⟨by unfold postpone; spres⟩ +instance (p : ObjRef) : SPresInst (schedContextResume (Z := Unit) p) := ⟨by unfold schedContextResume; spres⟩ +instance (t : ObjRef) : SPresInst (testReschedule (Z := Unit) t) := ⟨by unfold testReschedule; spres⟩ +instance (sc t : ObjRef) : SPresInst (schedContextDonate (Z := Unit) sc t) := + ⟨by unfold schedContextDonate; spres⟩ +instance (t n : ObjRef) : SPresInst (maybeDonateSc (Z := Unit) t n) := ⟨by unfold maybeDonateSc; spres⟩ +instance (n t : ObjRef) : SPresInst (maybeReturnSc (Z := Unit) n t) := ⟨by unfold maybeReturnSc; spres⟩ +instance (sc r : ObjRef) : SPresInst (replyUnlinkSc (Z := Unit) sc r) := ⟨by unfold replyUnlinkSc; spres⟩ +instance (t r : ObjRef) : SPresInst (replyUnlinkTcb (Z := Unit) t r) := ⟨by unfold replyUnlinkTcb; spres⟩ +instance (c r : ObjRef) : SPresInst (replyRemove (Z := Unit) c r) := ⟨by unfold replyRemove; spres⟩ +instance (t r : ObjRef) : SPresInst (replyRemoveTcb (Z := Unit) t r) := ⟨by unfold replyRemoveTcb; spres⟩ +instance (sc r : ObjRef) : SPresInst (bindScReply (Z := Unit) sc r) := ⟨by unfold bindScReply; spres⟩ +instance (c e r : ObjRef) (d : Bool) : SPresInst (replyPush (Z := Unit) c e r d) := + ⟨by unfold replyPush; spres⟩ + +/-! ## Cancellation and signals -/ + +instance (s : ThreadState) : SPresInst (getBlockingObject (Z := Unit) s) := ⟨by unfold getBlockingObject; spres⟩ +instance (s : ThreadState) (t : ObjRef) (r : Option ObjRef) : SPresInst (blockedCancelIpc (Z := Unit) s t r) := + ⟨by unfold blockedCancelIpc; spres⟩ +instance (t n : ObjRef) : SPresInst (cancelSignal (Z := Unit) t n) := ⟨by unfold cancelSignal; spres⟩ +instance (t : ObjRef) : SPresInst (cancelIpc (Z := Unit) t) := ⟨by unfold cancelIpc; spres⟩ +instance (n : ObjRef) (q : List ObjRef) (b : Badge) : SPresInst (updateWaitingNtfn (Z := Unit) n q b) := + ⟨by unfold updateWaitingNtfn; spres⟩ +instance (t e : ObjRef) (b : Badge) (g gr c : Bool) : SPresInst (sendIpcBlocked (Z := Unit) t e b g gr c) := + ⟨by unfold sendIpcBlocked; spres⟩ + +/-- **R1 (spec, signals).** `send_signal` keeps the scheduler frame. -/ +instance (n : ObjRef) (b : Badge) : SPresInst (sendSignal (Z := Unit) n b) := ⟨by unfold sendSignal; spres⟩ + +theorem sendSignal_presA (n : ObjRef) (b : Badge) : SPresA (sendSignal (Z := Unit) n b) := + (inferInstance : SPresInst (sendSignal (Z := Unit) n b)).pres + +theorem cancelIpc_presA (t : ObjRef) : SPresA (cancelIpc (Z := Unit) t) := + (inferInstance : SPresInst (cancelIpc (Z := Unit) t)).pres + +instance (sc : ObjRef) : SPresInst (fl4SchedContextUpdateConsumed (Z := Unit) sc) := + ⟨by unfold fl4SchedContextUpdateConsumed; spres⟩ +instance (t : ObjRef) : SPresInst (isValidTimeoutHandler (Z := Unit) t) := + ⟨by unfold isValidTimeoutHandler; spres⟩ +instance (t : ObjRef) (i : MessageInfo) : SPresInst (setMessageInfo (Z := Unit) t i) := + ⟨by unfold setMessageInfo; spres⟩ + +/-! ## Conditional frames (the timeout send: the sender's fault is a timeout fault) -/ + +/-- Every result state of `m` is `R`-related to its start state, from start states satisfying `P`. -/ +def PresRC {σ α : Type} (R : σ → σ → Prop) (P : σ → Prop) (m : NonDet σ α) : Prop := + ∀ s, P s → ∀ x ∈ (m s).1, R s x.2 + +namespace PresRC +variable {σ α β : Type} {R : σ → σ → Prop} [PreO R] {P : σ → Prop} + +theorem of_pres {m : NonDet σ α} (h : PresR R m) : PresRC R P m := fun s _ x hx => h s x hx + +theorem bind_last {m : NonDet σ α} {f : α → NonDet σ β} (hm : PresRC R P m) (hf : ∀ a, PresR R (f a)) : + PresRC R P (NonDet.bind m f) := fun s hs x hx => by + obtain ⟨y, hy, hx'⟩ := hx + exact PreO.trans (hm s hs y hy) (hf y.1 y.2 x hx') + +theorem bind_inv {m : NonDet σ α} {f : α → NonDet σ β} + (hm : ∀ s, P s → ∀ x ∈ (m s).1, R s x.2 ∧ P x.2) (hf : ∀ a, PresRC R P (f a)) : + PresRC R P (NonDet.bind m f) := fun s hs x hx => by + obtain ⟨y, hy, hx'⟩ := hx + obtain ⟨h1, h2⟩ := hm s hs y hy + exact PreO.trans h1 (hf y.1 y.2 h2 x hx') + +theorem bind_fail {f : α → NonDet σ β} : PresRC R P (NonDet.bind NonDet.fail f) := fun s _ x hx => by + obtain ⟨y, hy, -⟩ := hx + exact absurd hy (Set.notMem_empty _) + +end PresRC + +theorem PresR.bind_est {σ α β : Type} {R : σ → σ → Prop} [PreO R] {P : σ → Prop} {m : NonDet σ α} + {f : α → NonDet σ β} (hm : PresR R m) (hP : ∀ s, ∀ x ∈ (m s).1, P x.2) (hf : ∀ a, PresRC R P (f a)) : + PresR R (NonDet.bind m f) := fun s x hx => by + obtain ⟨y, hy, hx'⟩ := hx + exact PreO.trans (hm s y hy) (hf y.1 y.2 (hP s y hy) x hx') + +theorem PresR.bind_val {σ α β : Type} {R : σ → σ → Prop} [PreO R] {Q : α → Prop} {m : NonDet σ α} + {f : α → NonDet σ β} (hm : ∀ s, ∀ x ∈ (m s).1, R s x.2 ∧ Q x.1) (hf : ∀ a, Q a → PresR R (f a)) : + PresR R (NonDet.bind m f) := fun s x hx => by + obtain ⟨y, hy, hx'⟩ := hx + obtain ⟨h1, h2⟩ := hm s y hy + exact PreO.trans h1 (hf y.1 h2 y.2 x hx') + +/-- TCBs are kept (up to nothing): every TCB of the start state is the same TCB afterwards. -/ +def TcbKeepA (σ σ' : Fl4SpecA64.State Unit) : Prop := + ∀ p y, σ.kheap p = some (.tcb y) → σ'.kheap p = some (.tcb y) + +instance : PreO TcbKeepA := ⟨fun _ _ _ h => h, fun h1 h2 p y hy => h2 p y (h1 p y hy)⟩ + +/-- Instance form of `PresR TcbKeepA`. -/ +class TKInst {α : Type} (m : SMonad Unit α) : Prop where + pres : PresR TcbKeepA m + +instance {α β : Type} {m : SMonad Unit α} {f : α → SMonad Unit β} [hm : TKInst m] [hf : ∀ a, TKInst (f a)] : + TKInst (m >>= f) := ⟨PresR.bind hm.pres fun a => (hf a).pres⟩ +instance {α : Type} (a : α) : TKInst (pure a : SMonad Unit α) := ⟨PresR.pure a⟩ +instance {α : Type} (a : α) : TKInst (NonDet.pure a : SMonad Unit α) := ⟨PresR.pure a⟩ +instance {α : Type} (f : State Unit → α) : TKInst (NonDet.gets f : SMonad Unit α) := ⟨PresR.gets f⟩ +instance : TKInst (NonDet.get : SMonad Unit (State Unit)) := ⟨PresR.get⟩ +instance {α : Type} (f : State Unit → Option α) : TKInst (NonDet.getsThe f : SMonad Unit α) := ⟨PresR.getsThe f⟩ +instance {α : Type} (o : Option α) : TKInst (NonDet.assertOpt o : SMonad Unit α) := ⟨PresR.assertOpt o⟩ +instance (P : Prop) : TKInst (NonDet.assert P : SMonad Unit Unit) := ⟨PresR.assert P⟩ +instance {α : Type} : TKInst (NonDet.fail : SMonad Unit α) := ⟨PresR.fail⟩ +instance {P : Prop} {d : Decidable P} {m : SMonad Unit Unit} [h : TKInst m] : + TKInst (@NonDet.when (State Unit) P d m) := ⟨PresR.when h.pres⟩ +instance {α : Type} {P : Prop} {d : Decidable P} {a b : SMonad Unit α} [ha : TKInst a] [hb : TKInst b] : + TKInst (@ite _ P d a b) := ⟨PresR.ite' ha.pres hb.pres⟩ + +macro "tkres" : tactic => `(tactic| + repeat' (first + | exact TKInst.pres + | refine PresR.bind_pure _ _ ?_ + | refine PresR.bind ?_ (fun _ => ?_) + | refine PresR.when ?_ + | refine PresR.ite' ?_ ?_ + | dsimp only + | split)) + +instance (p : ObjRef) : TKInst (getObject (Z := Unit) p) := ⟨by unfold getObject; tkres⟩ +instance {α : Type} [Nonempty α] (C : α → KernelObject) (ptr : ObjRef) : + TKInst (getSimpleKo (Z := Unit) C ptr) := ⟨by unfold getSimpleKo; tkres⟩ + +theorem setSimpleKo_tkA {α : Type} [Nonempty α] (C : α → KernelObject) [SimpleCtorA C] (ptr : ObjRef) (a : α) : + PresR TcbKeepA (setSimpleKo (Z := Unit) C ptr a) := by + intro σ r hr p x hx + obtain ⟨y, hk, hr2⟩ := setSimpleKo_memA C ptr a hr + rw [hr2] + have hne : p ≠ ptr := by + rintro rfl + rw [hk] at hx + exact SimpleCtorA.notcb y x (Option.some.inj hx) + simpa [Function.update_of_ne hne] using hx + +instance {α : Type} [Nonempty α] (C : α → KernelObject) [SimpleCtorA C] (ptr : ObjRef) (a : α) : + TKInst (setSimpleKo (Z := Unit) C ptr a) := ⟨setSimpleKo_tkA C ptr a⟩ + +instance {α : Type} (f : Tcb → α) (t : ObjRef) : TKInst (threadGet (Z := Unit) f t) := + ⟨by unfold threadGet; tkres⟩ +instance (r : ObjRef) : TKInst (getThreadState (Z := Unit) r) := ⟨by unfold getThreadState; tkres⟩ +instance (p : CslotPtr) : TKInst (getCap (Z := Unit) p) := ⟨by unfold getCap; tkres⟩ +instance (lookup : Bool) (t : ObjRef) : TKInst (lookupIpcBuffer (Z := Unit) lookup t) := + ⟨by unfold lookupIpcBuffer; tkres⟩ +instance (t e : ObjRef) : TKInst (tcbEpDequeue (Z := Unit) t e) := ⟨by unfold tcbEpDequeue; tkres⟩ + +/-- The sender's TCB carries a timeout fault. -/ +def Pf (t : ObjRef) (σ : Fl4SpecA64.State Unit) : Prop := + ∃ tcb, σ.kheap t = some (.tcb tcb) ∧ ∃ b, tcb.tcbFault = some (.timeout b) + +theorem Pf.of_keep {t : ObjRef} {σ σ' : Fl4SpecA64.State Unit} (h : TcbKeepA σ σ') (hp : Pf t σ) : Pf t σ' := by + obtain ⟨x, hx, b, hb⟩ := hp + exact ⟨x, h t x hx, b, hb⟩ + +theorem inv_of {α : Type} (t : ObjRef) (m : SMonad Unit α) [hs : SPresInst m] [hk : TKInst m] : + ∀ s, Pf t s → ∀ x ∈ (m s).1, SchedFrameA s x.2 ∧ Pf t x.2 := + fun s hp x hx => ⟨hs.pres s x hx, Pf.of_keep (hk.pres s x hx) hp⟩ + +theorem threadGet_valA {α : Type} (f : Tcb → α) {t : ObjRef} {σ : Fl4SpecA64.State Unit} {tcb : Tcb} + (h : σ.kheap t = some (.tcb tcb)) {x : α × Fl4SpecA64.State Unit} + (hx : x ∈ (threadGet (Z := Unit) f t σ).1) : x = (f tcb, σ) := by + obtain ⟨y1, hy1, hx1⟩ := hx + have hg : getTcb (Z := Unit) t σ = some tcb := by simp [getTcb, readObject, h] + have hy1' : y1 ∈ (NonDet.assertOpt (getTcb (Z := Unit) t σ) σ).1 := hy1 + rw [hg] at hy1' + have e1 : y1 = (tcb, σ) := hy1' + subst e1 + exact mem_pureA hx1 + +theorem PresRC.bind_threadGet {α β : Type} {P : Fl4SpecA64.State Unit → Prop} (f : Tcb → α) (t : ObjRef) + {g : α → SMonad Unit β} + (h : ∀ s, P s → ∃ tcb, s.kheap t = some (.tcb tcb) ∧ ∀ x ∈ ((g (f tcb)) s).1, SchedFrameA s x.2) : + PresRC SchedFrameA P (NonDet.bind (threadGet (Z := Unit) f t) g) := fun s hs x hx => by + obtain ⟨y1, hy1, hx1⟩ := hx + obtain ⟨tcb, hk, H⟩ := h s hs + have e := threadGet_valA f hk hy1 + subst e + exact H x hx1 + +theorem PresRC.bind_threadGet' {α β : Type} {P : Fl4SpecA64.State Unit → Prop} (f : Tcb → α) (t : ObjRef) + {g : α → SMonad Unit β} + (h : ∀ s, P s → ∃ tcb, s.kheap t = some (.tcb tcb) ∧ PresR SchedFrameA (g (f tcb))) : + PresRC SchedFrameA P (NonDet.bind (threadGet (Z := Unit) f t) g) := + PresRC.bind_threadGet f t fun s hs => by + obtain ⟨tcb, hk, H⟩ := h s hs + exact ⟨tcb, hk, fun x hx => H s x hx⟩ + +/-- The timeout fault message is short (at most 2 words) and keeps the frame. -/ +theorem fl4MakeFaultMsg_timeout_valA (b : Data) (t : ObjRef) : + ∀ σ, ∀ x ∈ (fl4MakeFaultMsg (Z := Unit) (.timeout b) t σ).1, + SchedFrameA σ x.2 ∧ x.1.2.length ≤ 2 := by + intro σ x hx + unfold fl4MakeFaultMsg at hx + dsimp only at hx + obtain ⟨y1, hy1, hx1⟩ := hx + have hy1' : y1 ∈ (NonDet.assertOpt (getTcb (Z := Unit) t σ) σ).1 := hy1 + cases hg : getTcb (Z := Unit) t σ with + | none => rw [hg] at hy1'; exact absurd hy1' (Set.notMem_empty _) + | some tcb => + rw [hg] at hy1' + have e1 : y1 = (tcb, σ) := hy1' + subst e1 + cases hsc : tcb.tcbSchedContext with + | none => + simp only [hsc] at hx1 + have := mem_pureA hx1 + subst this + exact ⟨PreO.refl σ, by simp⟩ + | some sc => + simp only [hsc] at hx1 + obtain ⟨y2, hy2, hx2⟩ := hx1 + have := mem_pureA hx2 + subst this + exact ⟨(inferInstance : SPresInst (fl4SchedContextUpdateConsumed (Z := Unit) sc)).pres σ y2 hy2, + by simp [wordsFromTime]⟩ + +theorem msg_registers_length_A : msg_registers.length = 4 := by + simp [msg_registers, msgRegisters, Register.x2ToX5] + +theorem fl4DoFaultTransfer_timeoutA (badge : Data) (sender receiver : ObjRef) (buf : Option ObjRef) : + PresRC SchedFrameA (Pf sender) (fl4DoFaultTransfer (Z := Unit) badge sender receiver buf) := by + unfold fl4DoFaultTransfer + refine PresRC.bind_threadGet' _ _ ?_ + intro s hs + obtain ⟨tcb, hk, b, hb⟩ := hs + refine ⟨tcb, hk, ?_⟩ + simp only [hb] + refine PresR.bind_pure _ _ ?_ + refine PresR.bind_val (Q := fun lm : Data × List Data => lm.2.length ≤ 2) (fl4MakeFaultMsg_timeout_valA b sender) + (fun lm hlm => ?_) + obtain ⟨label, msg⟩ := lm + dsimp only at hlm ⊢ + haveI : SPresInst (setMrs (Z := Unit) receiver buf msg) := + ⟨setMrs_presA _ _ _ (by rw [msg_registers_length_A]; omega)⟩ + spres + +theorem fl4DoIpcTransfer_timeoutC (sender : ObjRef) (ep : Option ObjRef) (badge : Badge) (grant : Bool) + (receiver : ObjRef) : + PresRC SchedFrameA (Pf sender) (fl4DoIpcTransfer (Z := Unit) sender ep badge grant receiver) := by + unfold fl4DoIpcTransfer + refine PresRC.bind_inv (inv_of sender _) (fun rb => ?_) + refine PresRC.bind_threadGet _ _ ?_ + intro s hs + obtain ⟨tcb, hk, b, hb⟩ := hs + refine ⟨tcb, hk, ?_⟩ + simp only [hb] + intro x hx + exact fl4DoFaultTransfer_timeoutA badge sender receiver rb s ⟨tcb, hk, b, hb⟩ x hx + +theorem threadSet_fault_PfA (t : ObjRef) (b : Data) : + ∀ σ, ∀ x ∈ (threadSet (Z := Unit) (fun tcb : Tcb => { tcb with tcbFault := some (.timeout b) }) t σ).1, + Pf t x.2 := by + intro σ r hr + obtain ⟨y1, hy1, h⟩ := hr + have hy1' : y1 ∈ (NonDet.assertOpt (getTcb (Z := Unit) t σ) σ).1 := hy1 + cases hg : getTcb (Z := Unit) t σ with + | none => rw [hg] at hy1'; exact absurd hy1' (Set.notMem_empty _) + | some x => + rw [hg] at hy1' + have e1 : y1 = (x, σ) := hy1' + subst e1 + obtain ⟨ko, hko, hr2⟩ := setObject_memA h + rw [hr2] + exact ⟨{ x with tcbFault := some (.timeout b) }, by simp, b, rfl⟩ + +theorem fl4SendIpc_timeoutC (block call : Bool) (badge : Badge) (cg cgr cd : Bool) (thread epptr : ObjRef) : + PresRC SchedFrameA (Pf thread) (fl4SendIpc (Z := Unit) block call badge cg cgr cd thread epptr) := by + unfold fl4SendIpc + refine PresRC.bind_inv (inv_of thread _) (fun ep => ?_) + cases ep with + | idleEP => cases block <;> exact PresRC.of_pres (by spres) + | sendEP q => cases block <;> exact PresRC.of_pres (by spres) + | recvEP q => + cases q with + | nil => exact PresRC.of_pres (by spres) + | cons dest q => + refine PresRC.bind_inv (inv_of thread _) (fun _ => ?_) + refine PresRC.bind_inv (inv_of thread _) (fun recvState => ?_) + cases recvState with + | blockedOnReceive a r d => + dsimp only + refine PresRC.bind_inv (inv_of thread _) (fun reply => ?_) + refine PresRC.bind_last (fl4DoIpcTransfer_timeoutC _ _ _ _ _) (fun _ => ?_) + spres + | _ => dsimp only; exact PresRC.bind_fail + +theorem fl4SendFaultIpc_timeoutA (tptr : ObjRef) (handlerCap : Cap) (b : Data) (canDonate : Bool) : + SPresA (fl4SendFaultIpc (Z := Unit) tptr handlerCap (.timeout b) canDonate) := by + unfold fl4SendFaultIpc + cases handlerCap with + | endpointCap r bd rights => + dsimp only + refine PresR.bind_est (threadSet_presA _ tptr) (threadSet_fault_PfA tptr b) (fun _ => ?_) + refine PresRC.bind_last (fl4SendIpc_timeoutC _ _ _ _ _ _ _ _) (fun _ => ?_) + spres + | nullCap => dsimp only; spres + | _ => dsimp only; spres + +/-- **R3 (spec, timeout send).** `fl4HandleTimeout t (.timeout b)` keeps the scheduler frame. -/ +theorem fl4HandleTimeout_presA (t : ObjRef) (b : Data) : SPresA (fl4HandleTimeout (Z := Unit) t (.timeout b)) := by + haveI : ∀ (c : Cap) (d : Bool), SPresInst (fl4SendFaultIpc (Z := Unit) t c (.timeout b) d) := + fun c d => ⟨fl4SendFaultIpc_timeoutA t c b d⟩ + unfold fl4HandleTimeout + spres + +/-! ## R2 (spec): the IRQ arm -/ + +/-- The machine's user memory is kept. -/ +def UmKeepA (ms ms' : MachineState) : Prop := ms'.underlyingMemory = ms.underlyingMemory + +instance : PreO UmKeepA := ⟨fun _ => rfl, fun h1 h2 => h2.trans h1⟩ + +theorem PresR.app {σ α : Type} {R : σ → σ → Prop} {m : NonDet σ α} (h : PresR R m) {s : σ} {x : α × σ} + (hx : x ∈ (m s).1) : R s x.2 := h s x hx + +theorem maskInterrupt_umA (m : Bool) (irq : Irq) : PresR UmKeepA (maskInterrupt m irq) := by + unfold maskInterrupt + exact PresR.modify _ (fun _ => rfl) + +theorem ackInterrupt_umA (irq : Irq) : PresR UmKeepA (ackInterrupt irq) := by + unfold ackInterrupt + exact PresR.pure _ + +theorem ackDeadlineIRQ_umA : PresR UmKeepA ackDeadlineIRQ := by + unfold ackDeadlineIRQ + exact ackInterrupt_umA _ + +theorem getActiveIRQ_umA (b : Bool) : PresR UmKeepA (getActiveIRQ b) := by + unfold getActiveIRQ + refine PresR.bind (PresR.gets _) (fun _ => ?_) + refine PresR.bind (PresR.modify _ (fun _ => rfl)) (fun _ => ?_) + refine PresR.bind (PresR.gets _) (fun _ => ?_) + exact PresR.ite' (PresR.pure _) (PresR.pure _) + +instance (m : Bool) (irq : Irq) : SPresInst (doMachineOp (Z := Unit) (maskInterrupt m irq)) := + ⟨doMachineOp_presA _ (maskInterrupt_umA m irq)⟩ +instance (irq : Irq) : SPresInst (doMachineOp (Z := Unit) (ackInterrupt irq)) := + ⟨doMachineOp_presA _ (ackInterrupt_umA irq)⟩ +instance : SPresInst (doMachineOp (Z := Unit) ackDeadlineIRQ) := ⟨doMachineOp_presA _ ackDeadlineIRQ_umA⟩ +instance (b : Bool) : SPresInst (doMachineOp (Z := Unit) (getActiveIRQ b)) := + ⟨doMachineOp_presA _ (getActiveIRQ_umA b)⟩ + +instance (irq : Irq) : SPresInst (getIrqSlot (Z := Unit) irq) := ⟨by unfold getIrqSlot; spres⟩ +instance (irq : Irq) : SPresInst (archMaskIrqSignal (Z := Unit) irq) := ⟨by unfold archMaskIrqSignal; spres⟩ + +theorem PresRC.bind_gets {α β : Type} {P : Fl4SpecA64.State Unit → Prop} (f : Fl4SpecA64.State Unit → α) + {g : α → SMonad Unit β} (h : ∀ s, P s → ∀ x ∈ ((g (f s)) s).1, SchedFrameA s x.2) : + PresRC SchedFrameA P (NonDet.bind (NonDet.gets f) g) := fun s hs x hx => by + obtain ⟨y1, hy1, hx1⟩ := hx + have e : y1 = (f s, s) := hy1 + subst e + exact h s hs x hx1 + +/-- **R2 (spec, IRQ arm).** `handle_interrupt irq` keeps the scheduler frame on a non-reserved line (the reserved +arm — `vgic_maintenance`, `vppi_event` — writes VCPU objects: S4's). -/ +theorem handleInterrupt_presC (irq : Irq) : + PresRC SchedFrameA (fun σ => σ.interruptStates irq ≠ .irqReserved) (handleInterrupt (Z := Unit) irq) := by + unfold handleInterrupt + split + · exact PresRC.of_pres (doMachineOp_presA _ (PresR.bind (maskInterrupt_umA _ _) (fun _ => ackInterrupt_umA _))) + · refine PresRC.bind_gets (fun s => s.interruptStates irq) (fun s hs x hx => ?_) + revert hx hs + cases hst : s.interruptStates irq with + | irqSignal => intro hs hx; exact PresR.app (by spres) hx + | irqTimer => intro hs hx; exact PresR.app (by spres) hx + | irqInactive => intro hs hx; exact PresR.app (by spres) hx + | irqReserved => intro hs; exact absurd rfl hs + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/SpecFrameSchedA.lean b/proofs/Fl4ProofsA64/P3/SpecFrameSchedA.lean new file mode 100644 index 000000000..351afc9a2 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/SpecFrameSchedA.lean @@ -0,0 +1,223 @@ +/- +P3 (aarch64, A64-P3 Stage-3 integration; queue item 1): **the scheduler spec functions keep `SchedFrameA`** +(`P3/SpecFrameCalcA`): every result state of `fl4SelectAction`, `fl4SwitchSchedContext`, `fl4SetNextInterrupt`, `awaken`, +`checkDomainTime`, `fl4ActivateState`, ... differs from the start state only as `SpecSchedA` allows (TCBs with the same cap +slots and VCPU links, SCs of the same size), keeps the IRQ / ASID / CDT / original-cap state and the user memory. + +Pure spec statements (independent of the concrete state). No premise; new module. +-/ +import Fl4ProofsA64.P3.SpecFrameCalcA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec + +noncomputable section + +/-! ## Writing leaves -/ + +instance (ts : ThreadState) : TcbSlotFn (fun tcb : Tcb => { tcb with tcbState := ts }) := ⟨fun _ => ⟨rfl, rfl, rfl⟩⟩ +instance (g : Option ObjRef → Option ObjRef) : TcbSlotFn (tcbSchedContextUpdate g) := ⟨fun _ => ⟨rfl, rfl, rfl⟩⟩ +instance (g : Option ObjRef → Option ObjRef) : TcbSlotFn (tcbYieldToUpdate g) := ⟨fun _ => ⟨rfl, rfl, rfl⟩⟩ + +instance (f : Tcb → Tcb) [TcbSlotFn f] (t : ObjRef) : SPresInst (threadSet (Z := Unit) f t) := + ⟨threadSet_presA f t⟩ + +instance (p : ObjRef) (f : SchedContext → SchedContext) : SPresInst (updateSchedContext (Z := Unit) p f) := + ⟨updateSchedContext_presA f p⟩ + +instance {α : Type} (t : ObjRef) (f : UserMonad α) : SPresInst (asUser (Z := Unit) t f) := ⟨asUser_presA t f⟩ + +/-! ## Readers and small writers -/ + +instance {α : Type} (f : Tcb → α) (t : ObjRef) : SPresInst (threadGet (Z := Unit) f t) := + ⟨by unfold threadGet; spres⟩ +instance (f : Tcb → Option ObjRef) (r : ObjRef) : SPresInst (getTcbObjRef (Z := Unit) f r) := + ⟨by unfold getTcbObjRef; spres⟩ +instance (r : ObjRef) : SPresInst (getThreadState (Z := Unit) r) := ⟨by unfold getThreadState; spres⟩ +instance (d : Domain) (p : Priority) : SPresInst (getTcbQueue (Z := Unit) d p) := ⟨by unfold getTcbQueue; spres⟩ +instance (d : Domain) (p : Priority) (q : ReadyQueue) : SPresInst (setTcbQueue (Z := Unit) d p q) := + ⟨by unfold setTcbQueue; spres⟩ +instance (a : ObjRef → List ObjRef → List ObjRef) (t : ObjRef) : SPresInst (tcbSchedAction (Z := Unit) a t) := + ⟨by unfold tcbSchedAction; spres⟩ +instance (a : SchedulerAction) : SPresInst (setSchedulerAction (Z := Unit) a) := + ⟨by unfold setSchedulerAction; spres⟩ +instance (t : ObjRef) : SPresInst (tcbReleaseRemove (Z := Unit) t) := ⟨by unfold tcbReleaseRemove; spres⟩ + +instance (p : ObjRef) : SPresInst (getObject (Z := Unit) p) := ⟨by unfold getObject; spres⟩ +instance (p : ObjRef) : SPresInst (getSchedContext (Z := Unit) p) := ⟨by unfold getSchedContext; spres⟩ +instance (f : SchedContext → Option ObjRef) (p : ObjRef) : SPresInst (getScObjRef (Z := Unit) f p) := + ⟨by unfold getScObjRef; spres⟩ +instance (p : ObjRef) : SPresInst (getRefills (Z := Unit) p) := ⟨by unfold getRefills; spres⟩ +instance (p : ObjRef) : SPresInst (getRefillHead (Z := Unit) p) := ⟨by unfold getRefillHead; spres⟩ +instance (p : ObjRef) : SPresInst (getRefillTail (Z := Unit) p) := ⟨by unfold getRefillTail; spres⟩ +instance (p : ObjRef) : SPresInst (getScRefillReady (Z := Unit) p) := ⟨by unfold getScRefillReady; spres⟩ +instance (p : ObjRef) (u : Ticks) : SPresInst (getScRefillSufficient (Z := Unit) p u) := + ⟨by unfold getScRefillSufficient; spres⟩ +instance (p : ObjRef) (u : Ticks) : SPresInst (fl4GetScRefillSufficient (Z := Unit) p u) := + ⟨by unfold fl4GetScRefillSufficient; spres⟩ +instance (p : ObjRef) : SPresInst (getScActive (Z := Unit) p) := ⟨by unfold getScActive; spres⟩ +instance (p : ObjRef) : SPresInst (isRoundRobin (Z := Unit) p) := ⟨by unfold isRoundRobin; spres⟩ + +/-! ## Scheduler queues -/ + +instance : SPresInst (rescheduleRequired (Z := Unit)) := ⟨by unfold rescheduleRequired; spres⟩ +instance (t : ObjRef) : SPresInst (possibleSwitchTo (Z := Unit) t) := ⟨by unfold possibleSwitchTo; spres⟩ + + +/-! ## Tops -/ + +instance : SPresInst (fl4ChooseThread (Z := Unit)) := ⟨by unfold fl4ChooseThread; spres⟩ +instance (t : ObjRef) : SPresInst (fl4SwitchToThread (Z := Unit) t) := ⟨by unfold fl4SwitchToThread; spres⟩ +instance (eop : DetExtMonad Unit) : SPresInst (doExtendedOp (Z := Unit) eop) := ⟨by unfold doExtendedOp; spres⟩ +instance (limit : Nat) : SPresInst (fl4NextDomain (Z := Unit) limit) := ⟨by unfold fl4NextDomain; spres⟩ +instance (limit : Nat) : SPresInst (fl4ChooseNewThread (Z := Unit) limit) := ⟨by unfold fl4ChooseNewThread; spres⟩ +instance (limit : Nat) (c cu i : ObjRef) (d : Domain) (w : Bool) : + SPresInst (fl4SelectCandidate (Z := Unit) limit c cu i d w) := ⟨by unfold fl4SelectCandidate; spres⟩ + +instance (p : CslotPtr) : SPresInst (getCap (Z := Unit) p) := ⟨by unfold getCap; spres⟩ + +/-! ### Refills -/ + +instance (p : ObjRef) (f : Refill → Refill) : SPresInst (updateRefillHd (Z := Unit) p f) := + ⟨by unfold updateRefillHd; spres⟩ +instance (p : ObjRef) (f : Refill → Refill) : SPresInst (updateRefillTl (Z := Unit) p f) := + ⟨by unfold updateRefillTl; spres⟩ +instance (p : ObjRef) (r : Refill) : SPresInst (refillAddTail (Z := Unit) p r) := ⟨by unfold refillAddTail; spres⟩ +instance (p : ObjRef) : SPresInst (refillPopHead (Z := Unit) p) := ⟨by unfold refillPopHead; spres⟩ +instance (p : ObjRef) : SPresInst (refillSize (Z := Unit) p) := ⟨by unfold refillSize; spres⟩ +instance (p : ObjRef) : SPresInst (refillSingle (Z := Unit) p) := ⟨by unfold refillSingle; spres⟩ +instance (p : ObjRef) : SPresInst (refillFull (Z := Unit) p) := ⟨by unfold refillFull; spres⟩ +instance (p : ObjRef) (r : Refill) : SPresInst (scheduleUsed (Z := Unit) p r) := ⟨by unfold scheduleUsed; spres⟩ +instance (p : ObjRef) : SPresInst (mergeOverlappingRefills (Z := Unit) p) := ⟨by unfold mergeOverlappingRefills; spres⟩ +instance (p : ObjRef) : SPresInst (refillHeadOverlappingLoop (Z := Unit) p) := + ⟨by unfold refillHeadOverlappingLoop; spres⟩ +instance (p : ObjRef) : SPresInst (refillUnblockCheck (Z := Unit) p) := ⟨by unfold refillUnblockCheck; spres⟩ +instance (o : Option ObjRef) (a b : Option Bool) : SPresInst (ifCondRefillUnblockCheck (Z := Unit) o a b) := + ⟨by unfold ifCondRefillUnblockCheck; spres⟩ +instance (p : ObjRef) : SPresInst (mergeNonoverlappingHeadRefill (Z := Unit) p) := + ⟨by unfold mergeNonoverlappingHeadRefill; spres⟩ +instance (m : Ticks) (p : ObjRef) : SPresInst (headInsufficientLoopWith (Z := Unit) m p) := + ⟨by unfold headInsufficientLoopWith; spres⟩ +instance (p : ObjRef) (u : Ticks) : SPresInst (chargeEntireHeadRefill (Z := Unit) p u) := + ⟨by unfold chargeEntireHeadRefill; spres⟩ +instance (l : Ticks) (p : ObjRef) (u : Ticks) : SPresInst (handleOverrunWith (Z := Unit) l p u) := + ⟨by unfold handleOverrunWith; spres⟩ +instance (l m u : Ticks) : SPresInst (refillBudgetCheckWith (Z := Unit) l m u) := + ⟨by unfold refillBudgetCheckWith; spres⟩ +instance (u : Ticks) : SPresInst (fl4RefillBudgetCheck (Z := Unit) u) := ⟨by unfold fl4RefillBudgetCheck; spres⟩ +instance (u : Ticks) : SPresInst (refillBudgetCheckRoundRobin (Z := Unit) u) := + ⟨by unfold refillBudgetCheckRoundRobin; spres⟩ +instance : SPresInst (fl4CommitTime (Z := Unit)) := ⟨by unfold fl4CommitTime; spres⟩ + +/-! ### Switch, timer, awaken, domain -/ + +instance : SPresInst (fl4SwitchSchedContext (Z := Unit)) := ⟨by unfold fl4SwitchSchedContext; spres⟩ + +theorem setDeadline_presA (d : Word64) : SPresA (doMachineOp (Z := Unit) (setDeadline d)) := + doMachineOp_presA _ fun ms x hx => by + unfold setDeadline machineOpLift at hx + exact machineRestLift_umA _ ms x hx +instance (d : Word64) : SPresInst (doMachineOp (Z := Unit) (setDeadline d)) := ⟨setDeadline_presA d⟩ + +instance (q : List ObjRef) : SPresInst (fl4ReleaseDeadline (Z := Unit) q) := ⟨by unfold fl4ReleaseDeadline; spres⟩ +instance (timer : State Unit → Fl4TimerView) : SPresInst (fl4SetNextInterrupt (Z := Unit) timer) := + ⟨by unfold fl4SetNextInterrupt; spres⟩ +instance (timer : State Unit → Fl4TimerView) : SPresInst (fl4ScAndTimer (Z := Unit) timer) := + ⟨by unfold fl4ScAndTimer; spres⟩ +instance : SPresInst (tcbReleaseDequeue (Z := Unit)) := ⟨by unfold tcbReleaseDequeue; spres⟩ +instance : SPresInst (awaken (Z := Unit)) := ⟨by unfold awaken; spres⟩ +instance : SPresInst (checkDomainTime (Z := Unit)) := ⟨by unfold checkDomainTime; spres⟩ + +theorem fl4SelectAction_presA (limit : Nat) : SPresA (fl4SelectAction (Z := Unit) limit) := by + unfold fl4SelectAction; spres + +theorem fl4SelectThread_presA (limit : Nat) : SPresA (fl4SelectThread (Z := Unit) limit) := by + unfold fl4SelectThread; spres + + +theorem awaken_presA : SPresA (awaken (Z := Unit)) := (inferInstance : SPresInst (awaken (Z := Unit))).pres +theorem checkDomainTime_presA : SPresA (checkDomainTime (Z := Unit)) := + (inferInstance : SPresInst (checkDomainTime (Z := Unit))).pres +theorem fl4SwitchSchedContext_presA : SPresA (fl4SwitchSchedContext (Z := Unit)) := + (inferInstance : SPresInst (fl4SwitchSchedContext (Z := Unit))).pres +theorem fl4SetNextInterrupt_presA (timer : State Unit → Fl4TimerView) : + SPresA (fl4SetNextInterrupt (Z := Unit) timer) := + (inferInstance : SPresInst (fl4SetNextInterrupt (Z := Unit) timer)).pres +theorem fl4ScAndTimer_presA (timer : State Unit → Fl4TimerView) : SPresA (fl4ScAndTimer (Z := Unit) timer) := + (inferInstance : SPresInst (fl4ScAndTimer (Z := Unit) timer)).pres + +/-! ## Activation -/ + +theorem zipWithMx_nil_rightA {α β γ : Type} (f : α → β → NonDet (State Unit) γ) (l : List α) : + NonDet.zipWithMx f l ([] : List β) = NonDet.pure () := by + cases l <;> rfl + +theorem mem_pureA {α σ : Type} {a : α} {s : σ} {y : α × σ} (h : y ∈ (NonDet.pure a s).1) : y = (a, s) := h + +theorem setMrs_presA (thread : ObjRef) (buf : Option ObjRef) (msgs : List Message) + (h : msgs.length ≤ msg_registers.length) : SPresA (setMrs (Z := Unit) thread buf msgs) := by + intro σ r hr + obtain ⟨y1, hy1, hr⟩ := hr + have hy1' : y1 ∈ (NonDet.assertOpt (getTcb (Z := Unit) thread σ) σ).1 := hy1 + cases hg : getTcb (Z := Unit) thread σ with + | none => rw [hg] at hy1'; exact absurd hy1' (Set.notMem_empty _) + | some x => + rw [hg] at hy1' + have e1 : y1 = (x, σ) := hy1' + subst e1 + obtain ⟨y2, hy2, hr⟩ := hr + obtain rfl := mem_pureA hy2 + obtain ⟨y3, hy3, hr⟩ := hr + obtain rfl := mem_pureA hy3 + obtain ⟨y4, hy4, hr⟩ := hr + obtain ⟨ko, hko, hr4⟩ := setObject_memA hy4 + have hx := getTcb_kheapA hg + have F : SchedFrameA σ y4.2 := by + rw [hr4]; exact SchedFrameA.upd_tcb hx ⟨rfl, rfl, rfl⟩ + obtain ⟨y5, hy5, hr⟩ := hr + obtain rfl := mem_pureA hy5 + have hd : msgs.drop msg_registers.length = [] := List.drop_eq_nil_of_le h + have hr2 : r.2 = y4.2 := by + cases buf with + | none => rw [mem_pureA hr] + | some pptr => + obtain ⟨y6, hy6, hr⟩ := hr + dsimp only at hy6 + rw [hd, zipWithMx_nil_rightA] at hy6 + obtain rfl := mem_pureA hy6 + rw [mem_pureA hr] + rw [hr2]; exact F + +instance (thread : ObjRef) (buf : Option ObjRef) (c : Time) : SPresInst (setMrs (Z := Unit) thread buf (wordsFromTime c)) := + ⟨setMrs_presA _ _ _ (by simp [wordsFromTime, msg_registers, msgRegisters, Register.x2ToX5])⟩ + +instance (thread : ObjRef) (l : Data) (c : Time) : SPresInst (replyFromKernel (Z := Unit) thread (l, wordsFromTime c)) := + ⟨by unfold replyFromKernel setMessageInfo; spres⟩ + +instance (lookup : Bool) (t : ObjRef) : SPresInst (lookupIpcBuffer (Z := Unit) lookup t) := + ⟨by unfold lookupIpcBuffer; spres⟩ + +instance (scPtr : ObjRef) (cap : Ticks) (toUs : Ticks → Word64) : + SPresInst (schedContextUpdateConsumedWith (Z := Unit) cap toUs scPtr) := + ⟨by unfold schedContextUpdateConsumedWith; spres⟩ +instance (t : ObjRef) : SPresInst (schedContextCancelYieldTo (Z := Unit) t) := + ⟨by unfold schedContextCancelYieldTo; spres⟩ +instance (cap : Ticks) (toUs : Ticks → Word64) (t : ObjRef) : + SPresInst (completeYieldToWith (Z := Unit) cap toUs t) := ⟨by unfold completeYieldToWith; spres⟩ +instance (t : ObjRef) : SPresInst (fl4CompleteYieldTo (Z := Unit) t) := ⟨by unfold fl4CompleteYieldTo; spres⟩ + +instance (t : ObjRef) : SPresInst (setThreadStateAct (Z := Unit) t) := ⟨by unfold setThreadStateAct; spres⟩ +instance (r : ObjRef) (ts : ThreadState) : SPresInst (setThreadState (Z := Unit) r ts) := + ⟨by unfold setThreadState; spres⟩ +instance (t : ObjRef) : SPresInst (archActivateIdleThread (Z := Unit) t) := + ⟨by unfold archActivateIdleThread; spres⟩ +instance (t : ObjRef) : SPresInst (fl4ActivateState (Z := Unit) t) := ⟨by unfold fl4ActivateState; spres⟩ +instance : SPresInst (fl4ActivateThread (Z := Unit)) := ⟨by unfold fl4ActivateThread; spres⟩ + +theorem fl4ActivateThread_presA : SPresA (fl4ActivateThread (Z := Unit)) := + (inferInstance : SPresInst (fl4ActivateThread (Z := Unit))).pres + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3A.lean b/proofs/Fl4ProofsA64/P3/Stage3A.lean new file mode 100644 index 000000000..ee9c2ef7d --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3A.lean @@ -0,0 +1,891 @@ +/- +P3 (aarch64, A64-P3 Stage-3 integration step 1; Architect 21:51Z D6, `a64p3untyped-contract.md` §5/§8): **the unified +Stage-3 record**: S3's entry relation `JEntA` (which carries J1, `ObjAllocA` / `IdleAnchorOffA`), the IPC / MCS +relations at the same `σ`, `m` and ghosts, and the Untyped line's J fields. + +* **`JStage3A`** extends `JEntA` with `SchedSRA` (at the core map `κ`), `ScCovA`, `EpStateRel` / `NtfnStateRel` / + `ReplyStackRelA` over the ghosts `N E Rp Bf stack`, `BufCover`, `BufNativeA`, `IPlacedA`, `IpcSchedPlacementA`, + `IdleNoFaultA`, `McpRelA`, `YieldRelA`, `SInvXA`, and: + * **J2** [J] **`SetsInHA`** (every page of `Pp S1 T2 V` and every 8 KiB root of `X Y` lies in an object of `H`) and + **`DeadExtA`** (an object that no `OS` cap names, `DeadA`, has no VCPU binding, is in no reply stack and is no + ASID-table entry). + * **J3** [J] **`SpecFreeA`**: no spec `kheap` object, `cdt` edge end or `is_original_cap` flag in the free part of + an untyped (`UtFreeA`). + * **J4** [J] **`UtBlk39A`**: every untyped cap of `OS` has `size_bits ≤ 39` ([P] boot bound 2^39). + * **J9** [J] **`UnmPtSpecA`** (an unmapped PageTable / VSpace cap's table has no valid descriptor, an all-invalid + spec table and `TabOffA`), `TabOffA` of the tables of `S1`, `ExtTabOffA`. + * **`UtIpcHeapA`** [J] (`P3/UtIpcHeapA`): `N`, `E`, `Rp` are heap notifications, endpoints and replies. + * **J10** [J] **`SpecZeroFreeA`**: spec user memory is zero in the free part of a RAM untyped. + * **J11** [J] **`origLive`** (Architect 03:22Z): every `is_original_cap` slot is a slot of `th` (x86 `JKern.orig`). + * **J12** [J] **`nullOrig`** (Architect 03:29Z; l4v `valid_nullcaps`): a spec slot holding the null cap is not original + (`specCapAtA`); **`thPos`** (closure D): every `other` object of `th` is nonempty; **`DeadExtA.tvcpu`** (closure E): a + dead TCB has no VCPU word. + * **D1** (`P3/Stage3FieldsA`): `frBase`, `devFr`, `finCov`, `idleAll`, `ipcHeapCov`, `zombieNoVcpu`, `zombieTcb`, + `bufOff`. + * **D1b**: `vsOk`, `bufAl`, `zombieBnd`, `devUtOff`. + * **F3–F6** (`SIdleA`): the idle objects of all cores (thread states, idle SC's replies and binding, no cap to them). + * **J5** (P1-MDB), **J7** (S1A close: `S1aR2` / `S1aKtUt`): the modular slots `J5`, `J7`. J8 `PPHypUtA` is a loop + hook and not a field. +* **`JStage3EntryA`**: with the entry-phase facts `CurInvA`, `OffOKA`, `OffReadyA`, `ActIdleA`, + `curScNotIdle`, `validTime` and **F1–F2** (`SActA`: pending `switchThread` target, current thread's state). + +Premises: the J fields above are [J]; boot witnesses are in `P3/Stage3BootA`. New module; no existing statement changed. +-/ +import Fl4ProofsA64.P3.JEntW1A +import Fl4ProofsA64.P3.UtIpcHeapA +import Fl4ProofsA64.P3.Stage3FieldsA +import Fl4ProofsA64.P3.Stage3SortA +import Fl4ProofsA64.P3.Stage3Rec5A +import Fl4ProofsA64.P3.Stage3SchedValidA +import Fl4ProofsA64.P3.IpcScGlueA +import Fl4ProofsA64.P3.IpcScBootA +import Fl4ProofsA64.P3.ScYieldRelA +import Fl4ProofsA64.P3.BufNativeFrA +import Fl4ProofsA64.P3.ScCfgEntryA +import Fl4ProofsA64.P3.IpcCancelA +import Fl4ProofsA64.P3.EpReplyStackA +import Fl4ProofsA64.P3.FinVsXA +import Fl4ProofsA64.P3.ScTimeA +import Fl4ProofsA64.P1.S1aR2Contract +import Fl4ProofsA64.P3.ScDomainA +import Fl4ProofsA64.P3.Stage3IpiKeepA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched + +noncomputable section + +section +variable {S : Type} (st : machine.Store S) [L : LawfulStore st] {A : Type} + +/-- **The free part of an `OS` untyped** holds the FL4 address `a`. -/ +def UtFreeA (OS : Set U64) (s : S) (dev : Bool → Prop) (a : Nat) : Prop := + ∃ u ∈ OS, ∃ q b fi d, capA st s u = .Untyped q b fi d ∧ dev d ∧ q.val + fi.val ≤ a ∧ a < q.val + 2 ^ b.val + +/-- The kind of object a `DeadExtA` clause speaks of. -/ +inductive DKindA + | vcpu + | reply + | pool + | tcb + +/-- **The typed namers** (Architect ruling 2026-12-03, contract §35): the caps that name the object at `p` as a +VCPU, a reply object, an ASID pool, or a TCB (a Thread cap or a TCB zombie). -/ +def NamesDA : DKindA → U64 → capability.Cap → Prop + | .vcpu, p, c => c = .Vcpu p + | .reply, p, c => ∃ g, c = .Reply p g + | .pool, p, c => ∃ b, c = .AsidPool p b + | .tcb, p, c => c = .Thread p ∨ ∃ n, c = .Zombie p capability.ZOMBIE_TCB n + +theorem not_namesD_nullA (k : DKindA) (p : U64) : ¬ NamesDA k p .Null := by + cases k + · intro h; cases (show capability.Cap.Null = .Vcpu p from h) + · rintro ⟨_, h⟩; cases h + · rintro ⟨_, h⟩; cases h + · rintro (h | ⟨_, h⟩) <;> cases h + +theorem not_namesD_untypedA (k : DKindA) (p q b i : U64) (d : Bool) : ¬ NamesDA k p (.Untyped q b i d) := by + cases k + · intro h; cases (show capability.Cap.Untyped q b i d = .Vcpu p from h) + · rintro ⟨_, h⟩; cases h + · rintro ⟨_, h⟩; cases h + · rintro (h | ⟨_, h⟩) <;> cases h + +/-- No `OS` cap names the object at `p` as an object of kind `k`. -/ +def DeadA (OS : Set U64) (k : DKindA) (s : S) (p : U64) : Prop := + ∀ u ∈ OS, ¬ NamesDA k p (capA st s u) + +/-- **J2, the covering half** (module docstring). -/ +structure SetsInHA (Z : XSetsA) (H : Heap) : Prop where + pages : PtHA H (Z.Pp ∪ Z.S1 ∪ Z.T2 ∪ Z.V) + roots : ∀ p ∈ Z.X ∪ Z.Y, ∃ q z, H q = some (.other z) ∧ q.val ≤ p.val ∧ p.val + 8192 ≤ q.val + 2 ^ z + +open Classical in +/-- **The cap a spec slot holds, as a pure function of the heap** (CNode slot of a well-formed CNode, or TCB slot; `none` +elsewhere): l4v `get_cap` without the monad. -/ +noncomputable def specCapAtA (σ : Fl4SpecA64.State Unit) (sl : Fl4SpecA64.CslotPtr) : Option Fl4SpecA64.Cap := + match σ.kheap sl.1 with + | some (.cnode sz cn) => if Fl4SpecA64.wellFormedCnodeN sz cn then cn sl.2 else none + | some (.tcb t) => Fl4SpecA64.tcbCnodeMap t sl.2 + | _ => none + +/-- **J2, the reference half** (module docstring). -/ +structure DeadExtA (Z : XSetsA) (OS Rp : Set U64) (stack : U64 → List U64) (s : S) + (σ : Fl4SpecA64.State Unit) : Prop where + vcpu : ∀ v ∈ Z.V, DeadA st OS .vcpu s v → ∀ x, σ.kheap (specPtr v) = some (.archObj (.vcpu x)) → x.vcpuTcb = none + reply : ∀ r ∈ Rp, DeadA st OS .reply s r → ∀ c ∈ Z.C, r ∉ stack c + asid : ∀ i p, σ.archState.armAsidTable i = some (specPtr p) → ¬ DeadA st OS .pool s p + /-- (Architect 00:06Z, closure E) a dead TCB has no VCPU (its VCPU word is 0). -/ + tvcpu : ∀ t ∈ Z.T, DeadA st OS .tcb s t → L.mem s (core.num.U64.wrapping_add t 3664#u64) = 0#u64 + /-- (record edit #6) A dead VCPU has no runner (its `RUN_CALLER` word is 0). -/ + vcDeadRun : ∀ v ∈ Z.V, DeadA st OS .vcpu s v → L.mem s (core.num.U64.wrapping_add v 24#u64) = 0#u64 + /-- (record edit #8) A dead reply object has no caller. -/ + replyTcb : ∀ r ∈ Rp, DeadA st OS .reply s r → ∀ x, σ.kheap (specPtr r) = some (.reply x) → x.replyTcb = none + +/-- (record edit #6) Every RAM untyped of `OS` lies in the kernel window `[PPTR_BASE, PPTR_BASE + 2^44)`. -/ +def UtWinCapA (OS : Set U64) (s : S) : Prop := + ∀ u ∈ OS, ∀ p sb fi, capA st s u = .Untyped p sb fi false → + layout.PPTR_BASE.val ≤ p.val ∧ p.val + 2 ^ sb.val ≤ layout.PPTR_BASE.val + 2 ^ 44 + +/-- (record edit #8b-C, item 14) Every device `Untyped` and device `Frame` cap of `OS` lies in the `2^44` device window: +the device twin of `UtWinCapA`. -/ +def DevWinCapA (OS : Set U64) (s : S) : Prop := + ∀ u ∈ OS, (∀ p sb fi, capA st s u = .Untyped p sb fi true → p.val + 2 ^ sb.val ≤ 2 ^ 44) ∧ + ∀ p sz r m e a v, capA st s u = .Frame p sz r true m e a v → + ∀ bits : U64, sz.bits = ok bits → p.val + 2 ^ bits.val ≤ 2 ^ 44 + +/-- (record edit #6) Narrow VCPU state snapshot read by `vcpu::complete_run`'s `read_state_to_frame` +(`out_mask ≠ 0`): the 64 VCPU register words, `VPPI_MASKED` (exact `0`/`1`), `RUN_OFFSET`, +and `RUN_OUT_MASK`. Vacuous at boot (`V = ∅`), zeroed by Untyped (`vcpu::init`), framed in +one line by non-VCPU ops, and preserved by S4 VCPU. -/ +structure VcpuStateSnapA (V : Set U64) (m : U64 → U64) (σ : Fl4SpecA64.State Unit) : Prop where + snap : ∀ v ∈ V, ∀ x, σ.kheap (specPtr v) = some (.archObj (.vcpu x)) → + (∀ i < 64, (m (core.num.U64.wrapping_add v ⟨BitVec.ofNat 64 (64 + 8 * i)⟩)).bv = + Fl4SpecA64.fl4VcpuRegGet x i) ∧ + m (core.num.U64.wrapping_add v 16#u64) = + (if x.vcpuVppiMasked .vppiEventIRQ_VTimer then 1#u64 else 0#u64) ∧ + (m (core.num.U64.wrapping_add v 32#u64)).bv = x.vcpuExt.runOffset ∧ + (m (core.num.U64.wrapping_add v 40#u64)).bv = x.vcpuExt.runOutMask ∧ + (m (core.num.U64.wrapping_add v 8#u64)).val ≤ 128 + /-- (record edit #8b-C, item 17) The run-out mask is within `vcpu_state_mask::ALL`. -/ + mask : ∀ v ∈ V, ∀ x, σ.kheap (specPtr v) = some (.archObj (.vcpu x)) → + x.vcpuExt.runOutMask &&& ~~~Fl4SpecA64.smAll = 0 + +/-- **J3** (module docstring). -/ +structure SpecFreeA (OS : Set U64) (s : S) (σ : Fl4SpecA64.State Unit) : Prop where + heap : ∀ p : U64, UtFreeA st OS s (fun _ => True) p.val → σ.kheap (specPtr p) = none + cdt : ∀ x y, σ.cdt x = some y → ∀ p : U64, UtFreeA st OS s (fun _ => True) p.val → + x.1 ≠ specPtr p ∧ y.1 ≠ specPtr p + orig : ∀ x, σ.isOriginalCap x = true → ∀ p : U64, UtFreeA st OS s (fun _ => True) p.val → x.1 ≠ specPtr p + +/-- **J4** (module docstring). -/ +def UtBlk39A (OS : Set U64) (s : S) : Prop := + ∀ u ∈ OS, ∀ q b fi d, capA st s u = .Untyped q b fi d → b.val ≤ 39 + +/-- A page table that is mapped nowhere: no valid descriptor, an all-invalid spec table of type `ty`, off the +relation's footprint. -/ +structure UnmTabA (Z : XSetsA) (m : Fl4SpecA64.McState) (s : S) (σ : Fl4SpecA64.State Unit) + (ty : Fl4SpecA64.PtType) (p : U64) : Prop where + inv : ∀ i : U64, i.val < 512 → dBitA (L.mem s (peAF p i)) 0 = false + spec : ∃ t, σ.kheap (specPtr p) = some (.archObj (.pageTable t)) ∧ Fl4SpecA64.ptType t = ty ∧ + ∀ i : U64, i.val < 512 → Fl4SpecA64.ptApply t i.bv = .invalidPTE + off : TabOffA (L := L) Z m s p + +/-- An unmapped Stage-2 VSpace root: no valid descriptor across its 1024 slots, and an all-invalid spec `stage2Root`. -/ +structure UnmS2RootA (s : S) (σ : Fl4SpecA64.State Unit) (r : U64) : Prop where + inv : ∀ i : U64, i.val < 1024 → dBitA (L.mem s (peAF r i)) 0 = false + spec : ∃ es, σ.kheap (specPtr r) = some (.archObj (.ext (.stage2Root es))) ∧ + ∀ i : Fin 1024, es i = .invalidPTE + +/-- An unmapped `ArmHighVSpace` window: no valid descriptor in its private L1 table `y + 4096`, and an all-invalid +spec `highVSpace` object. -/ +structure UnmHiRootA (s : S) (σ : Fl4SpecA64.State Unit) (y : U64) : Prop where + inv : ∀ i : U64, i.val < 512 → dBitA (L.mem s (peAF (core.num.U64.wrapping_add y 4096#u64) i)) 0 = false + spec : ∃ l1, σ.kheap (specPtr y) = some (.archObj (.ext (.highVSpace l1))) ∧ + ∀ i : Fin 512, l1 i = .invalidPTE + +/-- **J9, at the caps** (module docstring). -/ +structure UnmPtSpecA (Z : XSetsA) (m : Fl4SpecA64.McState) (OS : Set U64) (s : S) + (σ : Fl4SpecA64.State Unit) : Prop where + pt : ∀ u ∈ OS, ∀ p asid va, capA st s u = .PageTable p false asid va → UnmTabA st Z m s σ .normalPtT p + vs : ∀ u ∈ OS, ∀ p asid, capA st s u = .Pml4 p false asid → UnmTabA st Z m s σ .vsRootPtT p + s2 : ∀ u ∈ OS, ∀ p asid va, capA st s u = .Ept p 0#u64 false asid va → UnmS2RootA st s σ p + hv : ∀ u ∈ OS, ∀ p asid, capA st s u = .HighVSpace p false asid → UnmHiRootA st s σ p + +/-- **J10** (module docstring). -/ +def SpecZeroFreeA (OS : Set U64) (s : S) (σ : Fl4SpecA64.State Unit) : Prop := + ∀ a : Fl4SpecA64.Word, UtFreeA st OS s (fun d => d = false) (fl4Ptr a).val → + σ.machineState.underlyingMemory a = 0 + +/-- The typed-heap sizes of the IPC ghost objects (`ipcTh`). -/ +def IpcThA (th : TypedHeapA) (N E Rp : Set U64) : Prop := + (∀ n ∈ N, th n = some (.other 64)) ∧ (∀ e ∈ E, th e = some (.other 16)) ∧ (∀ r ∈ Rp, th r = some (.other 32)) + +/-- (record edit #8b-C2, item 13) A spec cap that is no mapped Stage-1 page-table cap whose address lies in the +Stage-2 window `[2^47, 2^48)` (the only values where `CapEqA`'s second disjunct, `unpack48A`, differs from the plain one). -/ +def NoHiPtV (a : Fl4SpecA64.Cap) : Prop := + ∀ p asid' va', a = .archObjectCap (.pageTableCap p .normalPtT (some (asid', va'))) → + ¬ (2 ^ 47 ≤ va'.toNat ∧ va'.toNat < 2 ^ 48) + +/-- **F6** (Architect 15:06Z; delete-hooks contract §47): no spec cap is a thread cap to one of the `n` cores' idle threads +or an SC cap to `idle_sc_ptr` (`SysL.ci` covers only the current core's idle word). -/ +def NoIdleCapA (σ : Fl4SpecA64.State Unit) (m : Fl4SpecA64.McState) (n : Nat) : Prop := + ∀ sl, (∀ c < n, specCapAtA σ sl ≠ some (.threadCap (m.idleThreadOf c))) ∧ + (∀ b, specCapAtA σ sl ≠ some (.schedContextCap Fl4SpecA64.idleScPtr b)) ∧ + ∀ a, specCapAtA σ sl = some a → NoHiPtV a + +/-- A spec cap that is no idle-thread cap and no idle-SC cap (what a cap-writing arm owes for the cap it installs), +and no mapped page-table cap in the window `[2^47, 2^48)` (`NoHiPtV`). -/ +def NoIdleCapV (m : Fl4SpecA64.McState) (n : Nat) (a : Fl4SpecA64.Cap) : Prop := + (∀ c < n, a ≠ .threadCap (m.idleThreadOf c)) ∧ (∀ b, a ≠ .schedContextCap Fl4SpecA64.idleScPtr b) ∧ NoHiPtV a + +/-- A spec step that keeps every `SIdleA` cap fact. -/ +theorem NoIdleCapA.of_eq {σ σ' : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} {n : Nat} + (h : NoIdleCapA σ m n) (e : ∀ sl, specCapAtA σ' sl = specCapAtA σ sl) : NoIdleCapA σ' m n := + fun sl => by rw [e sl]; exact h sl + +/-- A spec step whose every new cap is an old one or a non-idle cap. -/ +theorem NoIdleCapA.of_new {σ σ' : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} {n : Nat} + (h : NoIdleCapA σ m n) + (hc : ∀ sl a, specCapAtA σ' sl = some a → specCapAtA σ sl = some a ∨ NoIdleCapV m n a) : NoIdleCapA σ' m n := + fun sl => ⟨fun c hc' e => by + rcases hc sl _ e with e0 | ⟨hh, -⟩ + · exact (h sl).1 c hc' e0 + · exact hh c hc' rfl, + fun b e => by + rcases hc sl _ e with e0 | ⟨-, hh, -⟩ + · exact (h sl).2.1 b e0 + · exact hh b rfl, + fun a e => by + rcases hc sl _ e with e0 | ⟨-, -, hh⟩ + · exact (h sl).2.2 a e0 + · exact hh⟩ + +/-- **`PtCanonA`** (record edit #8b-C2, item 13; Architect ruling 13'): a mapped Stage-1 page-table cap whose abstract +cap is `NoHiPtV` has `CapEqA`'s plain address (the `unpack48A` disjunct needs the address in `[2^47, 2^48)`). -/ +theorem NoHiPtV.canonA {p asid va : U64} {p' : Fl4SpecA64.ObjRef} {asid' : Fl4SpecA64.Asid} + {va' : Fl4SpecA64.VSpaceRef} + (hn : NoHiPtV (.archObjectCap (.pageTableCap p' .normalPtT (some (asid', va'))))) + (h : CapEqA (.PageTable p true asid va) (.archObjectCap (.pageTableCap p' .normalPtT (some (asid', va'))))) : + va.bv = va' := by + by_cases h47 : va'.toNat < 2 ^ 47 + · exact CapEqA.pt_va_eq_of_lt47 h h47 + · rcases h.2.2 with hv | ⟨_, h48⟩ + · exact hv + · exact absurd ⟨by omega, h48⟩ (hn _ _ _ rfl) + +/-- **`PtCanonA`, in the joint relation's vocabulary**: a slot related by `SlotRelA` whose concrete cap is a mapped +`PageTable` and whose spec cap is a mapped Stage-1 page-table cap in a `NoIdleCapA` state has `va.bv = va'`. -/ +theorem NoIdleCapA.ptCanonA {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} {n : Nat} (h : NoIdleCapA σ m n) + {sl : Fl4SpecA64.CslotPtr} {mem : U64 → U64} {u ptr asid va : U64} {p' : Fl4SpecA64.ObjRef} + {asid' : Fl4SpecA64.Asid} {va' : Fl4SpecA64.VSpaceRef} + (hr : SlotRelA mem u (specCapAtA σ sl)) (hc : capAtA mem u = .PageTable ptr true asid va) + (hs : specCapAtA σ sl = some (.archObjectCap (.pageTableCap p' .normalPtT (some (asid', va'))))) : + va.bv = va' := by + obtain ⟨ac, e, hrel⟩ := hr + rw [hs] at e + cases e + rw [hc] at hrel + exact (h sl).2.2 _ hs |> fun hn => NoHiPtV.canonA hn hrel.2 + +/-- **F3–F6** [J] (Architect 15:06Z, delete-hooks contract §47; the idle objects of all `n` cores): every core's idle +thread is a TCB in `idleThreadState` (F3), `idle_sc_ptr`'s SC has no replies (F4), an SC bound to the current idle +thread is `idle_sc_ptr` (F5), and no cap names an idle thread or the idle SC (F6, `NoIdleCapA`). Not derivable from +`SInvXA` / `SysL` (current core only). -/ +structure SIdleA (σ : Fl4SpecA64.State Unit) (m : Fl4SpecA64.McState) (n : Nat) : Prop where + f3 : ∀ c < n, ∃ x, σ.kheap (m.idleThreadOf c) = some (.tcb x) ∧ x.tcbState = .idleThreadState + f4 : ∀ y k, σ.kheap Fl4SpecA64.idleScPtr = some (.schedContext y k) → y.scReplies = [] + f5 : ∀ p y k, σ.kheap p = some (.schedContext y k) → y.scTcb = some σ.idleThread → p = Fl4SpecA64.idleScPtr + f6 : NoIdleCapA σ m n + /-- (record edit #8b) [J] `idle_sc_ptr`'s SC is active and its head refill is ready. -/ + f7 : ∀ y k, σ.kheap Fl4SpecA64.idleScPtr = some (.schedContext y k) → + Fl4SpecA64.scActive y = true ∧ (Fl4SpecA64.refillHd y).rTime ≤ σ.curTime + /-- (record edit #8b) [J] Every core's idle thread has the null cap in slot 1 (`tcb_vtable`). -/ + idleSlot : ∀ c < n, vtCapA σ (m.idleThreadOf c) = some .nullCap + /-- (record edit #8b) [J] Every core's idle thread has no VCPU. -/ + idleBound : ∀ c < n, vcpuOfA σ (m.idleThreadOf c) = none + +/-- **F1–F2** [J] (Architect 15:06Z, widened; delete-hooks contract §47): a pending `switchThread u` names neither the +current nor the idle thread (F1), and the current thread is a TCB running, restarting, inactive or idle (F2). +A field of the entry record and the deletion loop's, not of `JStage3A` (a blocking IPC leaves the current thread +blocked until `schedule`). -/ +structure SActA (σ : Fl4SpecA64.State Unit) : Prop where + f1 : ∀ u, σ.schedulerAction = .switchThread u → u ≠ σ.curThread ∧ u ≠ σ.idleThread + f2 : ∃ x, σ.kheap σ.curThread = some (.tcb x) ∧ + (x.tcbState = .running ∨ x.tcbState = .restart ∨ x.tcbState = .inactive ∨ x.tcbState = .idleThreadState) + +theorem vtCapA_tcbI {σ : Fl4SpecA64.State Unit} {t : Fl4SpecA64.ObjRef} {x : Fl4SpecA64.Tcb} + (h : σ.kheap t = some (.tcb x)) : vtCapA σ t = some x.tcbVtable := by + unfold vtCapA; rw [h]; rfl + +theorem vcpuOfA_tcbI {σ : Fl4SpecA64.State Unit} {t : Fl4SpecA64.ObjRef} {x : Fl4SpecA64.Tcb} + (h : σ.kheap t = some (.tcb x)) : vcpuOfA σ t = x.tcbArch.tcbVcpu := by + unfold vcpuOfA; rw [h] + +/-- (record edit #8b) The idle slot-1 / VCPU facts of one thread across a step that keeps its vtable cap and VCPU. -/ +theorem idleVtKeepA {σ σ' : Fl4SpecA64.State Unit} {t : Fl4SpecA64.ObjRef} {x y : Fl4SpecA64.Tcb} + (hx : σ.kheap t = some (.tcb x)) (hy : σ'.kheap t = some (.tcb y)) (ev : y.tcbVtable = x.tcbVtable) + (ec : y.tcbArch.tcbVcpu = x.tcbArch.tcbVcpu) (h : vtCapA σ t = some .nullCap) (b : vcpuOfA σ t = none) : + vtCapA σ' t = some .nullCap ∧ vcpuOfA σ' t = none := by + rw [vtCapA_tcbI hx] at h; rw [vcpuOfA_tcbI hx] at b + rw [vtCapA_tcbI hy, vcpuOfA_tcbI hy, ev, ec] + exact ⟨h, b⟩ + +/-- (record edit #8b) The same for a thread whose heap object is unchanged. -/ +theorem idleVtSameA {σ σ' : Fl4SpecA64.State Unit} {t : Fl4SpecA64.ObjRef} (e : σ'.kheap t = σ.kheap t) + (h : vtCapA σ t = some .nullCap) (b : vcpuOfA σ t = none) : + vtCapA σ' t = some .nullCap ∧ vcpuOfA σ' t = none := by + unfold vtCapA vcpuOfA at *; rw [e]; exact ⟨h, b⟩ + +/-- `SIdleA` depends on the heap and the idle thread only, and on a current time that does not go back. -/ +theorem SIdleA.congr {σ σ' : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} {n : Nat} (h : SIdleA σ m n) + (hk : σ'.kheap = σ.kheap) (hi : σ'.idleThread = σ.idleThread) (ht : σ.curTime ≤ σ'.curTime) : + SIdleA σ' m n where + f3 := fun c hc => by rw [hk]; exact h.f3 c hc + f4 := fun y k hy => h.f4 y k (by rw [← hk]; exact hy) + f5 := fun p y k hy he => h.f5 p y k (by rw [← hk]; exact hy) (by rw [← hi]; exact he) + f6 := h.f6.of_eq fun sl => by unfold specCapAtA; rw [hk] + f7 := fun y k hy => by + obtain ⟨a, b⟩ := h.f7 y k (by rw [← hk]; exact hy) + exact ⟨a, BitVec.le_trans b ht⟩ + idleSlot := fun c hc => (idleVtSameA (by rw [hk]) (h.idleSlot c hc) (h.idleBound c hc)).1 + idleBound := fun c hc => (idleVtSameA (by rw [hk]) (h.idleSlot c hc) (h.idleBound c hc)).2 + +/-- `SActA` depends on the heap, the action and the two threads only. -/ +theorem SActA.congr {σ σ' : Fl4SpecA64.State Unit} (h : SActA σ) (hk : σ'.kheap = σ.kheap) + (ha : σ'.schedulerAction = σ.schedulerAction) (hc : σ'.curThread = σ.curThread) + (hi : σ'.idleThread = σ.idleThread) : SActA σ' where + f1 := fun u e => by rw [ha] at e; rw [hc, hi]; exact h.f1 u e + f2 := by rw [hk, hc]; exact h.f2 + +/-- A modular slot of the unified record (J5, J7). -/ +abbrev JSlotA (S : Type) : Type := + XSetsA → Heap → Set U64 → S → Fl4SpecA64.State Unit → Fl4SpecA64.McState → Prop + +variable {st} in +/-- (record edit #8) **`bufVs` across a step that shrinks the reachable set.** -/ +theorem bufVs_subA {Bf : Set (U64 × Fl4SpecA64.Word)} {Pp Tb Pp' Tb' : Set U64} {Lv Lv' : U64 → Nat} {s s' : S} + (h : ∀ b, BufW Bf b → ¬ VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b) (hk : L.ksb s' = L.ksb s) + (sub : utVsPtA st Pp' Tb' Lv' s' ⊆ utVsPtA st Pp Tb Lv s) : + ∀ b, BufW Bf b → ¬ VsR (L.ksb s') (utVsPtA st Pp' Tb' Lv' s') b := fun b hb hv => by + rw [hk] at hv + rcases hv with ⟨p, hp, o⟩ | hv + · exact h b hb (Or.inl ⟨p, sub hp, o⟩) + · exact h b hb (Or.inr hv) + +variable {st} in +/-- (record edit #8) **`bufVs` across a step whose new reachable pages are scheduler pages** (`BufOffA.page` keeps the +buffers off them): a pool assign's root, a page-table map's table. -/ +theorem bufVs_growA {Z : XSetsA} {OS : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {Pp Tb Pp' Tb' : Set U64} + {Lv Lv' : U64 → Nat} {s s' : S} (B : BufOffA st Z OS s Bf) + (h : ∀ b, BufW Bf b → ¬ VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b) (hk : L.ksb s' = L.ksb s) + (grow : ∀ p ∈ utVsPtA st Pp' Tb' Lv' s', p ∈ utVsPtA st Pp Tb Lv s ∨ SchedPageA st Z OS s p) : + ∀ b, BufW Bf b → ¬ VsR (L.ksb s') (utVsPtA st Pp' Tb' Lv' s') b := fun b hb hv => by + rw [hk] at hv + rcases hv with ⟨p, hp, o, h1, h2, e⟩ | hv + · rcases grow p hp with g | g + · exact h b hb (Or.inl ⟨p, g, o, h1, h2, e⟩) + · exact B.page b hb p g o h1 h2 e + · exact h b hb (Or.inr hv) + +/-- **The unified Stage-3 record** (module docstring). -/ +structure JStage3A (AR : A → HwRootA → Prop) (Z : XSetsA) (H : Heap) (OS U Ac Pp Tb : Set U64) (Lv : U64 → Nat) + (ia ie : Nat) (N E Rp : Set U64) (Bf : Set (U64 × Fl4SpecA64.Word)) (stack : U64 → List U64) (κ : U64 → U64) + (n : Nat) (J5 J7 : JSlotA S) (s : S) (a : A) (σ : Fl4SpecA64.State Unit) (m : Fl4SpecA64.McState) : Prop + extends JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m where + sr : SchedSRA st Z m κ s σ + scCov : ScCovA Z.C m σ + /-- (Architect ruling, 2026-12-03) [J] Every related spec SC object lies inside its heap object (kept by `SSzA`; + Retype preservation is the Untyped line's); (record edit #8b-D, Architect 11:22Z) a non-idle one fills it exactly + (idle SCs are 256 B with size exponent 0, so not unconditional). -/ + scSize : ∀ c ∈ Z.C, ∀ x n, σ.kheap (scNameA m c) = some (.schedContext x n) → + n + Fl4SpecA64.minSchedContextBits ≤ H.scBits c ∧ + (Sched.NotIdleScA m (scNameA m c) → n + Fl4SpecA64.minSchedContextBits = H.scBits c) + ep : EpStateRel Z.T E Rp Bf (L.mem s) σ + ntfn : NtfnStateRel N Z.T (L.mem s) σ + reply : ReplyStackRelA (L.mem s) Rp Z.C σ (scNameA m) stack + bufCover : BufCover st Z.T Bf s σ + bufNative : BufNativeA Z.T (L.mem s) + iplaced : IPlacedA N Z.T E Rp Bf + ipcPlace : IpcSchedPlacementA Z.th N E Rp Bf + idleNoFault : IdleNoFaultA m σ + mcp : McpRelA Z.T m (L.mem s) σ + yieldRel : YieldRelA Z.T Z.C m (L.mem s) σ + sinv : SInvXA σ m n + setsInH : SetsInHA Z H + /-- (Architect 00:54Z) [J] The IPC ghosts' notifications, endpoints and replies are heap objects of those kinds. -/ + utIpc : UtIpcHeapA H N E Rp + /-- (Architect 13:12Z) [J] The IPC ghosts' typed-heap sizes: notifications `.other 64`, endpoints `.other 16`, + replies `.other 32` (Untyped owes preservation). -/ + ipcTh : IpcThA Z.th N E Rp + deadExt : DeadExtA st Z OS Rp stack s σ + specFree : SpecFreeA st OS s σ + utBlk : UtBlk39A st OS s + unmPt : UnmPtSpecA st Z m OS s σ + tabS1 : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s q + tabExt : ExtTabOffA (L := L) Z m s + specZero : SpecZeroFreeA st OS s σ + /-- (Architect 03:22Z) [J] **`OrigLiveA`**: every original cap's slot is a slot of `th` (x86 `JKern.orig`). -/ + origLive : ∀ sl, σ.isOriginalCap sl = true → AbsSlotA Z.th sl + /-- (Architect 03:29Z) [J] **`nullOrig`** (l4v `valid_nullcaps`): a null slot is not original. -/ + nullOrig : ∀ sl, specCapAtA σ sl = some .nullCap → σ.isOriginalCap sl = false + /-- (Architect 00:06Z, closure D) [J] every `other` object of `th` is nonempty. -/ + thPos : ∀ p z, Z.th p = some (.other z) → 0 < z + /-- [J] (D1) No `OS` frame cap shares its base with a range key cap (`P3/CnodeFinalA`). -/ + frBase : FrBaseApartA st OS s + /-- [J] (D1) A device frame cap names no non-data-page object (`P3/DevFrDisjA`). -/ + devFr : DevFrDisjA st H OS s σ + /-- [J] (D1) Every spec CNode and TCB is in the typed heap (`P3/CnodeFinalA`). -/ + finCov : FinCovA Z.th σ + /-- [J] (D1, J13) Every core's idle objects lie in the idle area. -/ + idleAll : IdleAllA st ia ie Z.Cs m s + /-- [J] (D1, N1) The converse of `utIpc`. -/ + ipcHeapCov : IpcHeapCovA H N E Rp + /-- [J] (D1, N2) A TCB under a TCB zombie has no VCPU. -/ + zombieNoVcpu : ZombieTcbNoVcpuA st OS s + /-- [J] (D1) An `OS` TCB zombie's base is a heap TCB. -/ + zombieTcb : ZombieTcbTA st H OS s + /-- [J] (D1) The tracked buffer words are off the `OS` slots, VSpace words, table pages and VCPU words. -/ + bufOff : BufOffA st Z OS s Bf + /-- [J] (D1b) The installed pools' and reachable tables' words are placed (`P3/Stage3D1bA`). -/ + vsOk : UtVsOkA st H OS Z.V Pp Tb Lv s + /-- [J] (D1b) Every TCB's IPC buffer is aligned to 1024. -/ + bufAl : UtBufAlA Z.T σ + /-- [J] (D1b) A `Zombie`'s count fits its object. -/ + zombieBnd : UtZombieA st OS s + /-- [J] (D1b-4) Device untyped regions are apart from the record (`P3/Stage3FieldsA`). -/ + devUtOff : DevUtOffA st Z H OS ia ie s + /-- [J] (F3–F6, Architect 15:06Z) The idle objects of all cores (`SIdleA`). -/ + sidle : SIdleA σ m n + /-- [J] (Architect 17:31Z) The `OS` caps' heap kinds (`CapTyA`). -/ + capTy : CapTyA st H OS s + /-- [J] (record edit #3, Architect 19:30Z) Every related SC's core (`κ`, its `scCoreA` word) is a kernel core. -/ + scCpu : ∀ c ∈ Z.C, (κ c).val < Fl4SpecA64.fl4A64Platform.numNodes + /-- [J] (record edit #3) No related SC is at address `0`. -/ + scNZ : ∀ c ∈ Z.C, c ≠ 0#u64 + /-- [J] (record edit #3) The VCPU binding words (`VcRunA`). -/ + vcRun : VcRunA st Z.T Z.V s + /-- [J] (record edit #4, Architect 20:21Z) TCB → VCPU symmetry (`VcSym1A`). -/ + vcSym1 : VcSym1A st Z.T s + /-- [J] (record edit #4) P1's `RunVcpuInv`: a thread running a VCPU is in `TS_RUNNING_VM`. -/ + runVcpu : RunVcpuInv st Z.T s + /-- [J] (record edit #4) VCPU → TCB symmetry (`VcSym2A`). -/ + vcSym2 : VcSym2A st Z.V s + /-- [J] (record edit #4) Blocked ⇒ queued, endpoints and notifications (`BlkQueuedA`). -/ + blkQ : BlkQueuedA Z.T E N (L.mem s) σ + /-- [J] (record edit #5) Every heap object lies in `[PPTR_BASE, PPTR_BASE + 2^44)` (the [P] kernel-window bound of + `Fl4SpecA64/PORTING.md`). -/ + heapWin : ∀ p k, H p = some k → + layout.PPTR_BASE.val ≤ p.val ∧ p.val + 2 ^ k.bits ≤ layout.PPTR_BASE.val + 2 ^ 44 + /-- [J] (record edit #5) Every kernel core is a core of the record. -/ + coresAll : ∀ k < Fl4SpecA64.fl4A64Platform.numNodes, ∃ c ∈ Z.Cs, c.val = k + /-- [J] (record edit #5) Mapped page-table caps name tracked tables (`PtCapInA`). -/ + ptCapIn : PtCapInA Z (L.mem s) + /-- [J] (record edit #5) The debug-list and yield links are symmetric (A64 P1's `SymL`). -/ + symL : SymL st H Z.Cs s + /-- [J] (record edit #5) Every TCB's fault-handler slot holds `Null` or an endpoint cap. -/ + fhOK : ∀ t ∈ Z.T, NullOrEp H (capA st s (slotIdx t layout.tcb.SLOT_FAULT_HANDLER)) + /-- [J] (record edit #5) Every typed slot's zombie is well formed (`DelInvA`). -/ + zwf : DelInvA st Z.th s + /-- [J] (record edit #5, F8) The scheduler's validity facts (`SchedValidA`). -/ + schedValid : SchedValidA σ + /-- [J] (record edit #6) Every RAM untyped of `OS` lies in the kernel window. -/ + utWin : UtWinCapA st OS s + /-- [J] (record edit #8b-C, item 14) Every device untyped and device frame of `OS` lies in the `2^44` window. -/ + devWin : DevWinCapA st OS s + /-- [J] (record edit #6) A bound VCPU's TCB names it back (`vcpu.rs`). -/ + vcBack : ∀ v ∈ Z.V, L.mem s v ≠ 0#u64 → L.mem s (core.num.U64.wrapping_add (L.mem s v) 3664#u64) = v + /-- [J] (record edit #6) A running VCPU's `RUN_CALLER` names its runner back. -/ + runBack : ∀ t ∈ Z.T, L.mem s (runVcpuA t) ≠ 0#u64 → + L.mem s (core.num.U64.wrapping_add (L.mem s (runVcpuA t)) 24#u64) = t + /-- (record edit #6) The VCPU state snapshot (`VcpuStateSnapA`). -/ + vcSnap : VcpuStateSnapA Z.V (L.mem s) σ + /-- [J] (record edit #6, G3) No `OS` slot covers a VCPU word (A64 P1's `VcOK.os`). -/ + vcOS : ∀ b, VcW Z.V b → ¬ InOS OS b + /-- [J] (record edit #6, item 12) Non-device frame caps name spec data pages (`UserFrSpecA`). -/ + userFr : UserFrSpecA st OS s σ + /-- [J] (record edit #6, item 13) A tracked buffer's spec name is its FL4 address's. -/ + bfSpec : ∀ bp ∈ Bf, bp.2 = specPtr bp.1 + /-- [J] (record edit #7) No VCPU is loaded in the spec's `arm_current_vcpu` (FL4 switches VCPU registers lazily). -/ + curVcpuNone : σ.archState.armCurrentVcpu = none + /-- [J] (record edit #8) The tracked endpoints' queues and notifications' wait queues are priority sorted + (`IpcSortedA`). -/ + ipcSorted : IpcSortedA (specPtr '' E) (specPtr '' N) σ + /-- [J] (record edit #8) An SC with replies is active (l4v `active_reply_scs`). -/ + activeReplyScs : ActRepA σ + /-- [J] (record edit #8) No tracked IPC-buffer word is a reachable VSpace word. -/ + bufVs : ∀ b, BufW Bf b → ¬ VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b + /-- [J] (record edit #8b-B1, Architect 04:04Z item 5) The concrete domain-schedule words (position, start and the + two-entry table in the kernel-state block) match the spec's `domainIndex`, `domainStartIndex` and `domainList`: + `NdOKA` (`P3/ScDomainA`), entries in domain `0`. Kept by every step off the words `DomW` with the spec's schedule + kept (`NdOKA.keepS`; `QKeepA.dom`); the domain advance of `schedule` (`next_domain`, frame `MFrDA`) re-establishes it. + (Record edit F1) It also says the kernel-state block's `NUM_NODES` word (offset 0, in `DomW`) is the platform's + `numNodes`, which the IRQ control decode reads (`NdOKA.nodesGet`; no longer a premise of `IrqPlatA`). + (Record edit F4) And that the `KERNEL_ROOT` word (offset 48, in `DomW`) is the spec's global user VSpace (`NdOKA.kroot`, + `JStage3A.kroot`), which `set_kernel_vm_root` installs. -/ + domOK : Sched.NdOKA (st := st) s σ + /-- [J] (record edit B2, Architect 04:04Z item 6) This core's `IPI_PENDING` pair is empty (`SilentScheduleIpiA`, + `P3/ScScheduleTimerA`): the one-node invariant (`add_pending` writes only behind a thread of another core). Kept by + every step off the node words of the scheduler's read set (`SchedSRN.ipiKeepR`) and by every scheduler step + (`SchedSRN.ipiKeepW`: the narrowed `SKsA` leaves the pair out of `SWrA`). -/ + ipiZero : Sched.SilentScheduleIpiA (st := st) s + /-- [J] (frth, Architect 21:53Z) Every live non-device frame cap names an `other` object of `Z.th` of its size. -/ + frTh : FrThA st Z.th OS s + j5 : J5 Z H OS s σ m + j7 : J7 Z H OS s σ m + +/-- **The unified record at a kernel entry** (module docstring). -/ +structure JStage3EntryA (AR : A → HwRootA → Prop) (Z : XSetsA) (H : Heap) (OS U Ac Pp Tb : Set U64) + (Lv : U64 → Nat) (ia ie : Nat) (N E Rp : Set U64) (Bf : Set (U64 × Fl4SpecA64.Word)) (stack : U64 → List U64) + (κ : U64 → U64) (n : Nat) (J5 J7 : JSlotA S) (s : S) (a : A) (σ : Fl4SpecA64.State Unit) + (m : Fl4SpecA64.McState) : Prop + extends JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m where + curInv : CurInvA σ + offOK : OffOKA σ + offReady : OffReadyA σ + actIdle : ActIdleA σ + /-- [J] (Architect R1) A non-idle current SC is no core's idle SC. -/ + curScNotIdle : σ.curSc ≠ Fl4SpecA64.idleScPtr → Sched.NotIdleScA m σ.curSc + /-- [J] (Architect R1) The current time is bounded by the last counter observation. -/ + validTime : Sched.ValidMachineTimeA σ + /-- [J] (F1–F2, Architect 15:06Z) Scheduler action and current thread (`SActA`). -/ + sact : SActA σ + +/-! ## P1 `S1aPath` bridges to `VsReachA` / `S2ReachA` (Design 1A) -/ + +private theorem peAF_of_off8A (t o : U64) (ho : o.val < 8192) (h8 : o.val % 8 = 0) : + ∃ i : U64, i.val = o.val / 8 ∧ peAF t i = core.num.U64.wrapping_add t o := by + refine ⟨⟨BitVec.ofNat 64 (o.val / 8)⟩, ?_, ?_⟩ + · show (BitVec.ofNat 64 (o.val / 8)).toNat = o.val / 8 + rw [BitVec.toNat_ofNat] + exact Nat.mod_eq_of_lt (by simp only [Nat.reducePow]; omega) + · unfold peAF + congr 1 + apply UScalar.eq_of_val_eq + have hv : (⟨BitVec.ofNat 64 (o.val / 8)⟩ : U64).val = o.val / 8 := by + show (BitVec.ofNat 64 (o.val / 8)).toNat = o.val / 8 + rw [BitVec.toNat_ofNat] + exact Nat.mod_eq_of_lt (by simp only [Nat.reducePow]; omega) + simp [core.num.U64.wrapping_mul_val_eq, U64.size_def, U64.numBits, hv] + omega + +variable {st} in +/-- (record edit #8) **`bufVs` across one descriptor write that links a table with no valid descriptor** (a page-table +map's table, a Stage-1 pool assign's root): the written word `x` points only at `r`, `r` is a scheduler page, so +`BufOffA.page` keeps the buffers off it. -/ +theorem bufVs_leafA {Z : XSetsA} {OS : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {Pp Tb Tb' : Set U64} + {Lv Lv' : U64 → Nat} {s s' : S} {x r : U64} (B : BufOffA st Z OS s Bf) + (h : ∀ b, BufW Bf b → ¬ VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b) + (ty : VsTyR st (L.ksb s) (PoolSet st Pp s) ∅ Tb Lv s) + (ty' : VsTyR st (L.ksb s') (PoolSet st Pp s') ∅ Tb' Lv' s') (hk : L.ksb s' = L.ksb s) + (kA : ∀ b, AsW (L.ksb s) b → L.mem s' b = L.mem s b) + (same : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → b ≠ x → L.mem s' b = L.mem s b) + (hr : SchedPageA st Z OS s r) + (x0 : L.mem s' x ≠ 0#u64 → (L.mem s' x &&& 3#u64) = 0#u64 → L.mem s' x = r) + (x2 : (L.mem s' x &&& 3#u64) = 2#u64 → (L.mem s' x &&& ~~~3#u64) = r) + (x3 : (L.mem s' x &&& 3#u64) = 3#u64 → + core.num.U64.wrapping_add (L.mem s' x &&& vspace.ADDR_MASK) layout.PPTR_BASE = r) + (leaf : ∀ j : U64, j.val < 512 → dBitA (L.mem s' (peAF r j)) 0 = false) : + ∀ b, BufW Bf b → ¬ VsR (L.ksb s') (utVsPtA st Pp Tb' Lv' s') b := by + refine bufVs_growA B h hk fun q hq => ?_ + have g := utVsPtA_wrA (N := {r}) (x := x) ty ty' (poolSet_asidA hk kA) same x0 x2 x3 (fun p hp o h1 h8 h3 => by + rw [Set.mem_singleton_iff] at hp + subst hp + obtain ⟨j, hj, e⟩ := peAF_of_off8A p o (by omega) h8 + have b0 : dBitA (L.mem s' (core.num.U64.wrapping_add p o)) 0 = true := by + have c := congrArg (fun v : U64 => v.bv.getLsbD 0) h3 + simpa [dBitA] using c + rw [← e, leaf j (by omega)] at b0 + cases b0) hq + rcases g with g | g + · exact Or.inl g + · rw [Set.mem_singleton_iff] at g + exact Or.inr (g ▸ hr) + +/-- P1's `ActiveAsidRoot` implies `VsRelA`'s pure `ActiveAsidRootA`. -/ +theorem ActiveAsidRootA.of_p1 {Pp : Set U64} {s : S} {asid raw : U64} + (h : ActiveAsidRoot st Pp s asid raw) : ActiveAsidRootA (L.mem s) (L.ksb s) Pp asid raw := by + obtain ⟨hi, o, hhi, ho, h8, ha, pool, hp, htab, hw, hrne⟩ := h + obtain ⟨hpool, _, _, _, _, _, hpne⟩ := hp + obtain ⟨lo, hlov, hpe⟩ := peAF_of_off8A pool o (by omega) h8 + have hat : core.num.U64.wrapping_add (L.ksb s) (AtOff hi) = atAF (L.ksb s) hi := by + unfold AtOff atAF + have h127 : (hi &&& 127#u64) = hi := UScalar.eq_of_val_eq (by + rw [and_maskA hi 7 _ rfl]; exact Nat.mod_eq_of_lt (by omega)) + rw [h127] + rw [hat] at htab + rw [← hpe] at hw + exact ⟨hi, lo, hhi, by omega, by omega, pool, hpool, hpne, htab, hw, hrne⟩ + +private theorem addr_pptr_dTabA (w : U64) : + core.num.U64.wrapping_add (w &&& vspace.ADDR_MASK) layout.PPTR_BASE = dTabA w := by + unfold vspace.ADDR_MASK layout.PPTR_BASE dTabA dAddrA; rfl + +/-- Bridge from P1's `S1aPath` (`tag = 0` or `tag = 2`) to `VsRelA`'s `VsPathA`. -/ +theorem VsPathA.of_s1aPath {Pp Tb X Y T2 : Set U64} {Lv Sh2 : U64 → Nat} {n : U64} {s : S} + {σ : Fl4SpecA64.State Unit} (R : VsRelA Pp Tb Lv X Y (L.mem s) (L.ksb s) σ) + (E : ExtVsRelA Pp Tb X Y T2 Sh2 (L.mem s) σ) + {tag asid : U64} {sh va : Nat} {q : U64} (htag : tag = 0#u64 ∨ tag = 2#u64) + (hp : S1aPath st Pp n s tag asid sh va q) : + VsPathA Pp Tb Y (L.mem s) (L.ksb s) tag sh q := by + induction hp with + | @native asid raw hact hr0 => + have hactA := ActiveAsidRootA.of_p1 st hact + have htag0 : tagA raw = 0 := (tag_iffA raw).1 hr0 + obtain ⟨pool, hpool, hactP, lo, hlo, hw, hrne⟩ := hactA.activePool + have hk := R.kind pool hpool hactP lo hlo + rw [hw] at hk + rcases hk with rfl | ⟨-, htb, -⟩ | ⟨h1, -⟩ | ⟨h2, -⟩ + · exact absurd rfl hrne + · rw [untagA_of htag0] at htb + exact .native hactA htag0 htb + · omega + · omega + | @high asid raw hact hr2 _ => + have hactA := ActiveAsidRootA.of_p1 st hact + have htag2 : tagA raw = 2 := by + unfold tagA; have := congrArg UScalar.val hr2; rw [and3_valA] at this; exact this + obtain ⟨pool, hpool, hactP, lo, hlo, hw, hrne⟩ := hactA.activePool + have hk := R.kind pool hpool hactP lo hlo + rw [hw] at hk + rcases hk with rfl | ⟨h0, -⟩ | ⟨h1, -⟩ | ⟨-, hy⟩ + · exact absurd rfl hrne + · omega + · omega + · have hun : (raw &&& ~~~3#u64) = untagA raw := rfl + have h511 : core.num.U64.wrapping_add (untagA raw) 4088#u64 = peAF (untagA raw) 511#u64 := rfl + rw [hun, h511, addr_pptr_dTabA, (E.wl0 _ hy).2.2] + exact .high hactA htag2 hy + | stage2 => + rcases htag with h | h <;> cases h + | @child tag asid t o sh va hpar h12 ho h8 _ h3 ih => + have hvp := ih htag + have htY : t ∈ Tb ∨ ∃ y ∈ Y, t = core.num.U64.wrapping_add y 4096#u64 := by + rcases (R.path_props hvp).2 with ⟨ht, -⟩ | ⟨-, -, y, hy, -, hq⟩ + · exact Or.inl ht + · exact Or.inr ⟨y, hy, hq⟩ + have ho4k : o.val < 4096 := by + rcases htag with rfl | rfl + · have h01 : (0#u64 : U64) ≠ 1#u64 := by decide + simp only [s1aTableWidth, h01, ↓reduceIte] at ho + split_ifs at ho <;> omega + · have h21 : (2#u64 : U64) ≠ 1#u64 := by decide + have h20 : (2#u64 : U64) ≠ 0#u64 := by decide + simp only [s1aTableWidth, h21, h20, ↓reduceIte] at ho + split_ifs at ho <;> omega + obtain ⟨i, hiv, hpe⟩ := peAF_of_off8A t o (by omega) h8 + rw [← hpe] at h3 + have hd : core.num.U64.wrapping_add (L.mem s (core.num.U64.wrapping_add t o) &&& vspace.ADDR_MASK) layout.PPTR_BASE = + dTabA (L.mem s (peAF t i)) := by rw [← hpe]; exact addr_pptr_dTabA _ + rw [hd] + exact .child hvp htY h12 (by omega) h3 + +/-- One-line bridge from P1's `S1aPath` (`tag = 0` or `tag = 2`) to `VsReachA`. -/ +theorem VsReachA.of_s1aPath {Pp Tb X Y T2 : Set U64} {Lv Sh2 : U64 → Nat} {n : U64} {s : S} + {σ : Fl4SpecA64.State Unit} (R : VsRelA Pp Tb Lv X Y (L.mem s) (L.ksb s) σ) + (E : ExtVsRelA Pp Tb X Y T2 Sh2 (L.mem s) σ) + {tag asid : U64} {sh va : Nat} {q : U64} (htag : tag = 0#u64 ∨ tag = 2#u64) + (hp : S1aPath st Pp n s tag asid sh va q) : + VsReachA Pp Tb Y (L.mem s) (L.ksb s) q := + ⟨tag, sh, htag, VsPathA.of_s1aPath st R E htag hp⟩ + +/-- Bridge from P1's `S1aPath` (`tag = 1`) to `ExtVsRelA`'s `S2ReachA`. -/ +theorem S2ReachA.of_s1aPath {Pp Tb X Y T2 : Set U64} {Lv Sh2 : U64 → Nat} {n : U64} {s : S} + {σ : Fl4SpecA64.State Unit} (R : VsRelA Pp Tb Lv X Y (L.mem s) (L.ksb s) σ) + (hn : n.val = Fl4SpecA64.fl4A64Platform.s2IpaBits) + (hchild : ∀ asid sh va t, S1aPath st Pp n s 1#u64 asid sh va t → sh < s1aTopShift 1#u64 n → + t ∈ T2 ∧ sh = Sh2 t) + {asid : U64} {sh va : Nat} {q : U64} + (hp : S1aPath st Pp n s 1#u64 asid sh va q) : + S2ReachA Pp X T2 Sh2 (L.mem s) σ q := by + suffices H : ∀ tag, tag = 1#u64 → ∀ {asid sh va q}, S1aPath st Pp n s tag asid sh va q → + (q ∈ X ∧ ActiveS2RootA Pp X σ q ∧ sh = s1aTopShift 1#u64 n) ∨ + (q ∈ T2 ∧ S2ReachA Pp X T2 Sh2 (L.mem s) σ q ∧ sh < s1aTopShift 1#u64 n ∧ sh = Sh2 q) by + rcases H 1#u64 rfl hp with ⟨-, ha, -⟩ | ⟨-, hr, -⟩ + · exact .root ha + · exact hr + intro tag htag asid sh va q hp + induction hp with + | native => cases htag + | high => cases htag + | @stage2 asid raw hact hr1 => + obtain ⟨hi, lo, hhi, hlo, ha, pool, hpool, hpne, htab, hw, hrne⟩ := ActiveAsidRootA.of_p1 st hact + have hactP : ActivePoolA (L.mem s) (L.ksb s) pool := ⟨hpne, hi, hhi, htab⟩ + have htag1 : tagA raw = 1 := by + unfold tagA; have := congrArg UScalar.val hr1; rw [and3_valA] at this; exact this + have hk := R.kind pool hpool hactP lo hlo + rw [hw] at hk + have hrX : untagA raw ∈ X := by + rcases hk with rfl | ⟨h0, -⟩ | ⟨-, hx⟩ | ⟨h2, -⟩ + · exact absurd rfl hrne + · omega + · exact hx + · omega + have hav : asid.val < 65536 := by omega + have hpos : (0 : Fl4SpecA64.Asid) < BitVec.ofNat 16 asid.val := by + rw [BitVec.lt_def, BitVec.toNat_ofNat] + simp only [Fl4SpecA64.asidRepLen, Fl4SpecA64.asidLen, Nat.reducePow] + rw [Nat.mod_eq_of_lt hav] + show 0 < asid.val + by_cases haz : asid.val = 0 + · have hhi0 : hi = 0#u64 := UScalar.eq_of_val_eq (by change hi.val = 0; omega) + have hlo0 : lo = 0#u64 := UScalar.eq_of_val_eq (by change lo.val = 0; omega) + subst hhi0; subst hlo0 + have hz := R.asid0 (by rw [htab]; exact hpne) + rw [htab, hw] at hz + exact absurd hz hrne + · omega + obtain ⟨f, hkh, hfe⟩ := R.pool pool hpool + have htabS := R.table hi hhi + rw [htab, optPtrA_ne hpne] at htabS + have hhiEq : Fl4SpecA64.asidHighBitsOf (BitVec.ofNat 16 asid.val) = BitVec.ofNat 7 hi.val := by + rw [asidHi_eqA hav]; congr 1; omega + have hloEq : Fl4SpecA64.asidLowBitsOf (BitVec.ofNat 16 asid.val) = BitVec.ofNat 9 lo.val := by + rw [asidLo_eqA hav]; congr 1; omega + have hfS := hfe hactP lo hlo + rw [hw, poolEntA_ne hrne] at hfS + have hun : (raw &&& ~~~3#u64) = untagA raw := rfl + rw [hun] + exact Or.inl ⟨hrX, ⟨hrX, BitVec.ofNat 16 asid.val, pool, f, hpos, hpool, + by rw [hhiEq]; exact htabS, hkh, by rw [hloEq]; exact hfS⟩, rfl⟩ + | @child tag asid t o sh va hpar h12 ho h8 husr h3 ih => + subst htag + have ih' := ih rfl + have hltTop : sh - 9 < s1aTopShift 1#u64 n := by + rcases ih' with ⟨-, -, hsh⟩ | ⟨-, -, hsh, -⟩ <;> omega + obtain ⟨hdT2, hdSh⟩ := hchild asid (sh - 9) _ _ (.child hpar h12 ho h8 husr h3) hltTop + have h10 : (1#u64 : U64) ≠ 0#u64 := by decide + have h12' : (1#u64 : U64) ≠ 2#u64 := by decide + have ho8k : o.val < 8192 := by + by_cases htop : sh = s1aTopShift 1#u64 n + · rw [htop] at ho + simp only [s1aTableWidth, ↓reduceIte, s1aTopShift, s1aIpaWidth, h10, h12'] at ho + split_ifs at ho <;> (try simp only [Nat.reduceSub, Nat.reducePow, Nat.reduceMul] at ho) <;> omega + · simp only [s1aTableWidth, htop, ↓reduceIte] at ho; omega + obtain ⟨i, hiv, hpe⟩ := peAF_of_off8A t o ho8k h8 + have hd : core.num.U64.wrapping_add (L.mem s (core.num.U64.wrapping_add t o) &&& vspace.ADDR_MASK) layout.PPTR_BASE = + dTabA (L.mem s (peAF t i)) := by rw [← hpe]; exact addr_pptr_dTabA _ + rw [hd] at hdT2 hdSh ⊢ + rw [← hpe] at h3 + rcases ih' with ⟨_, ha, hsh⟩ | ⟨ht, hreach, hsh, hshT⟩ + · have hitop : i.val < Fl4SpecA64.s2Geom.topMask + 1 := by + rw [hsh] at ho + simp only [s1aTableWidth, ↓reduceIte, s1aTopShift, s1aIpaWidth, h10, h12'] at ho + unfold Fl4SpecA64.s2Geom + rw [← hn] + unfold Fl4SpecA64.s2GeomOf + split_ifs at ho ⊢ <;> dsimp only at ho ⊢ <;> + (try simp only [Nat.reduceSub, Nat.reducePow, Nat.reduceMul] at ho ⊢) <;> omega + exact Or.inr ⟨hdT2, .stepRoot ha hitop h3 hdT2, hltTop, hdSh⟩ + · have hneTop : sh ≠ s1aTopShift 1#u64 n := by omega + have ho4k : o.val < 4096 := by + simp only [s1aTableWidth, hneTop, ↓reduceIte] at ho + exact ho + exact Or.inr ⟨hdT2, .stepPt hreach ht (by omega) (by omega) h3 hdT2, hltTop, hdSh⟩ + +end + +/-! ## Record edit #6: keeping the new fields -/ + +section rec6 +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +theorem UtWinCapA.keepA {OS : Set U64} {s s' : S} (capE : ∀ u ∈ OS, capA st s' u = capA st s u) + (h : UtWinCapA st OS s) : UtWinCapA st OS s' := + fun u hu p sb fi e => h u hu p sb fi ((capE u hu).symm.trans e) + +theorem DevWinCapA.keepA {OS : Set U64} {s s' : S} (capE : ∀ u ∈ OS, capA st s' u = capA st s u) + (h : DevWinCapA st OS s) : DevWinCapA st OS s' := fun u hu => + ⟨fun p sb fi e => (h u hu).1 p sb fi ((capE u hu).symm.trans e), + fun p sz r m e a v c => (h u hu).2 p sz r m e a v ((capE u hu).symm.trans c)⟩ + +/-- `vcBack` across a step keeping the VCPUs' `TCB` words and the TCBs' VCPU words. -/ +theorem vcBack_keepA {V T : Set U64} {s s' : S} + (hR : ∀ v ∈ V, L.mem s v = 0#u64 ∨ L.mem s v ∈ T) (hv : ∀ v ∈ V, L.mem s' v = L.mem s v) + (ht : ∀ t ∈ T, L.mem s' (core.num.U64.wrapping_add t 3664#u64) = L.mem s (core.num.U64.wrapping_add t 3664#u64)) + (h : ∀ v ∈ V, L.mem s v ≠ 0#u64 → L.mem s (core.num.U64.wrapping_add (L.mem s v) 3664#u64) = v) : + ∀ v ∈ V, L.mem s' v ≠ 0#u64 → L.mem s' (core.num.U64.wrapping_add (L.mem s' v) 3664#u64) = v := by + intro v hv0 hne + rw [hv v hv0] at hne ⊢ + rcases hR v hv0 with e | e + · exact absurd e hne + · rw [ht _ e]; exact h v hv0 hne + +/-- `runBack` across a step keeping the TCBs' `RUN_VCPU` words and the VCPUs' `RUN_CALLER` words. -/ +theorem runBack_keepA {V T : Set U64} {s s' : S} + (hR : ∀ t ∈ T, L.mem s (runVcpuA t) = 0#u64 ∨ L.mem s (runVcpuA t) ∈ V) + (ht : ∀ t ∈ T, L.mem s' (runVcpuA t) = L.mem s (runVcpuA t)) + (hv : ∀ v ∈ V, L.mem s' (core.num.U64.wrapping_add v 24#u64) = L.mem s (core.num.U64.wrapping_add v 24#u64)) + (h : ∀ t ∈ T, L.mem s (runVcpuA t) ≠ 0#u64 → + L.mem s (core.num.U64.wrapping_add (L.mem s (runVcpuA t)) 24#u64) = t) : + ∀ t ∈ T, L.mem s' (runVcpuA t) ≠ 0#u64 → + L.mem s' (core.num.U64.wrapping_add (L.mem s' (runVcpuA t)) 24#u64) = t := by + intro t ht0 hne + rw [ht t ht0] at hne ⊢ + rcases hR t ht0 with e | e + · exact absurd e hne + · rw [hv _ e]; exact h t ht0 hne + +/-- The snapshot's word offsets are aligned VCPU-object offsets. -/ +theorem snapOffA (i : Nat) (hi : i < 64) : + (⟨BitVec.ofNat 64 (64 + 8 * i)⟩ : U64).val < 4096 ∧ (⟨BitVec.ofNat 64 (64 + 8 * i)⟩ : U64).val % 8 = 0 := by + have e : (⟨BitVec.ofNat 64 (64 + 8 * i)⟩ : U64).val = 64 + 8 * i := by + show (BitVec.ofNat 64 (64 + 8 * i)).toNat = _ + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + rw [e]; omega + +/-- **`VcpuStateSnapA` across a step keeping every VCPU word and every spec VCPU object.** -/ +theorem VcpuStateSnapA.keepA {V : Set U64} {m m' : U64 → U64} {σ σ' : Fl4SpecA64.State Unit} + (hw : ∀ v ∈ V, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → + m' (core.num.U64.wrapping_add v o) = m (core.num.U64.wrapping_add v o)) + (hk : ∀ v ∈ V, ∀ x, σ'.kheap (specPtr v) = some (.archObj (.vcpu x)) → + σ.kheap (specPtr v) = some (.archObj (.vcpu x))) + (h : VcpuStateSnapA V m σ) : VcpuStateSnapA V m' σ' := by + refine ⟨fun v hv x hx => ?_, fun v hv x hx => h.mask v hv x (hk v hv x hx)⟩ + obtain ⟨h1, h2, h3, h4, h5⟩ := h.snap v hv x (hk v hv x hx) + refine ⟨fun i hi => ?_, ?_, ?_, ?_, ?_⟩ + · obtain ⟨o1, o2⟩ := snapOffA i hi + rw [hw v hv _ o1 o2]; exact h1 i hi + · rw [hw v hv 16#u64 (by simp) (by simp)]; exact h2 + · rw [hw v hv 32#u64 (by simp) (by simp)]; exact h3 + · rw [hw v hv 40#u64 (by simp) (by simp)]; exact h4 + · rw [hw v hv 8#u64 (by simp) (by simp)]; exact h5 + +/-- **A VCPU word is no slot-write word**: aligned, in no `OS` slot (`vcOS`) and no TCB's low words (the VCPU is +an `other 12` heap object, apart from every TCB). -/ +theorem vcw_notCapWA {H : Heap} (hH : HeapOK H) {V OS : Set U64} (vh : VcH H V) + (vos : ∀ b, VcW V b → ¬ InOS OS b) {v o : U64} (hv : v ∈ V) (ho : o.val < 4096) (h8 : o.val % 8 = 0) : + Aligned (core.num.U64.wrapping_add v o) ∧ + ¬ W7.CapW H.tcbs {b | InOS OS b} ∅ (core.num.U64.wrapping_add v o) := by + have hH12 := vh v hv + have hf := hH.fits v _ hH12 + have hal := hH.align v _ hH12 + simp only [show (HKind.other 12).bits = 12 from rfl, Nat.reducePow] at hf hal + have ev : (core.num.U64.wrapping_add v o).val = v.val + o.val := by + rw [wa_val]; exact Nat.mod_eq_of_lt (by omega) + refine ⟨by unfold Aligned; rw [ev]; omega, ?_⟩ + rintro (⟨t, ht, K, hK, e⟩ | ⟨hin, -⟩) + · have ht' : H t = some .tcb := ht + have hft := hH.fits t _ ht' + simp only [show HKind.tcb.bits = 12 from rfl, Nat.reducePow] at hft + have et : (core.num.U64.wrapping_add t K).val = t.val + K.val := by + rw [wa_val]; exact Nat.mod_eq_of_lt (by omega) + have hne : t ≠ v := fun h => by rw [h, hH12] at ht'; cases ht' + have hd := hH.disj t _ v _ ht' hH12 hne + simp only [show HKind.tcb.bits = 12 from rfl, show (HKind.other 12).bits = 12 from rfl, Nat.reducePow] at hd + have := congrArg UScalar.val e + rw [ev, et] at this + omega + · exact vos _ ⟨v, hv, o, ho, h8, rfl⟩ hin + +/-- `VcpuStateSnapA` with no VCPU. -/ +theorem VcpuStateSnapA.empty (m : U64 → U64) (σ : Fl4SpecA64.State Unit) : VcpuStateSnapA ∅ m σ := + ⟨fun _ h => h.elim, fun _ h => h.elim⟩ + +end rec6 + +/-- (record edit #8b) The current idle thread has the null cap in slot 1. -/ +theorem JStage3A.idleSlot_curA {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J5 J7 : JSlotA S} {s : S} {a : A} + {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) (hc : m.curCore < n) : + vtCapA σ σ.idleThread = some .nullCap := by + rw [show σ.idleThread = m.idleThreadOf m.curCore from J.sinv.view] + exact J.sidle.idleSlot _ hc + +/-- (record edit #8b) The current idle thread has no VCPU. -/ +theorem JStage3A.idleBound_curA {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J5 J7 : JSlotA S} {s : S} {a : A} + {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) (hc : m.curCore < n) : + vcpuOfA σ σ.idleThread = none := by + rw [show σ.idleThread = m.idleThreadOf m.curCore from J.sinv.view] + exact J.sidle.idleBound _ hc + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3AfterPreemptA.lean b/proofs/Fl4ProofsA64/P3/Stage3AfterPreemptA.lean new file mode 100644 index 000000000..93e153e8e --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3AfterPreemptA.lean @@ -0,0 +1,282 @@ +/- +P3 (aarch64, A64-P3 Stage-3 integration, G3): **`after_invocation` of a preempted invocation** (`afterPreemptA`): the +body of the callers' plug-in `hPre` (`sendBodyScopeA`, `cnodeCopyBodyA`, `VsBodyA`, `sendBodyErrA`), from +`A64RelPS3` and `PreExitA`: `check_budget` for a schedulable current thread, else `charge_budget … false` for an active +current SC, else the clear of the consumed time, then the pending interrupt. + +* `SendPA`: the timeout-fault plug-in without a waiting receiver, free of the scheduler action. +* **`hSendO_of_hSendP`**: it gives `kentryChargeIrqSendA`'s `hSendO`, so the kernel-entry assembly discharges one term. +* **`afterPreemptA`**. + +The pending interrupt is **`take_ofA`** (`P3/Stage3PreemptIrqA`): `pending := pendingIrqA poll preemptServiceA`, +`JStage3A.handleInterruptA` at the polled state. The old [J] `take` is gone, and so is [J] `chargeable`: `CurScChargeableA` +is the last conjunct of `PreExitA`, carried by the deletion loop (`DelOwnA`). + +Named premises: none of the two [J]s of the preempted exit remains open. The interrupt entry's set, the same +definitions and tags (`P3/Stage3PreemptIrqA`): [P] `PollAgreeA` (`hpoll`), [P] `IrqArchA` (`K`), [P] +`hMax`, [P] `ArchPlat` (`hPl`, `nA = hPl.ipa`), `line` (`IrqLineBaseA`: [P] `num`, [J] `active`, [S] `noResched`; `reserved` is [P], in `PollAgreeA`, from fl4-kernel#154). [J] `T`, `hX`, `hSend` +(`SendPA`, guarded by the reached record `s a σ`, no `∀` over records); [S] `hnd` (one domain); [P] `hA`, `hCs`, `one`, `hag`, `hmb`, `hLimit`, `hmax`, `htu`, `hconv`, `clk`. +-/ +import Fl4ProofsA64.P3.Stage3PreemptChargeA +import Fl4ProofsA64.P3.Stage3BudgetFalseA +import Fl4ProofsA64.P3.Stage3PartialA +import Fl4ProofsA64.P3.Stage3PreemptIrqA +import Fl4ProofsA64.P3.Stage3EntryValidA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4SpecA64 Fl4Spec Classical + +noncomputable section + +/-- The body-end facts after a charge: no consumed time, `choose_new_thread` pending. -/ +theorem bodyOKA_chargedPreA {σ τ : State Unit} {m : McState} (P : BodyPPA σ m) (C : ChargedA σ τ) : BodyOKA τ m := by + refine ⟨⟨?_, fun y n _ _ h => ?_, fun h => ?_⟩, fun t e => ?_, fun h => ?_, fun y n _ _ _ h => absurd C.used h⟩ + · have := P.validTime + unfold ValidMachineTimeA at this ⊢ + rw [C.time, C.lmt]; exact this + · rcases h with h | h + · exact absurd C.used h + · rw [C.act] at h; cases h + · rw [C.act] at h; cases h + · rw [C.act] at e; cases e + · rw [C.csc] at h ⊢; exact P.scNotIdle h + +/-- A `check_budget` on an insufficient head from a schedulable current thread charges. -/ +theorem fl4CheckBudget_chargedA {σ τ : State Unit} {b : Bool} (h : schedulable σ.curThread σ = true) + (spec : fl4GetScRefillSufficient (Z := Unit) σ.curSc σ.consumedTime σ = ({(false, σ)}, False)) + (e : fl4CheckBudget (Z := Unit) σ = ({(b, τ)}, False)) : ChargedA σ τ := by + have h1 : (b, τ) ∈ (fl4CheckBudget (Z := Unit) σ).1 := by rw [e]; exact Set.mem_singleton _ + unfold fl4CheckBudget at h1 + rw [nd_gets_bindSc, nd_gets_bindSc, nd_bind_oneSc spec] at h1 + simp only [Bool.false_eq_true, ↓reduceIte] at h1 + rw [nd_gets_bindSc] at h1 + obtain ⟨⟨⟨⟩, σ2⟩, h2, h3⟩ := mem_bindA.1 h1 + have e3 : (b, τ) = (false, σ2) := h3 + obtain ⟨-, es⟩ := Prod.mk.inj e3 + subst es + exact fl4ChargeBudget_doneA h h2 + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + +/-- [J] **The timeout send without a waiting receiver, at the record `s1 a1 σ1`** (guarded by it: no `∀` over records): `kentryChargeIrqSendA`'s `hSendO` without the +timestamp equation, with `ChargeEntryA` replaced by the facts free of the scheduler action. -/ +def SendPA (st : machine.Store S) [LawfulStore st] (ar : machine.Arch A) (AR : A → HwRootA → Prop) (Z : XSetsA) + (H : Heap) (OS U Ac Pp Tb : Set U64) (Lv : U64 → Nat) (ia ie : Nat) (N E Rp : Set U64) + (Bf : Set (U64 × Fl4SpecA64.Word)) (stack : U64 → List U64) (κ : U64 → U64) (nA : U64) (m : McState) + (Clock : A → MachineState → Prop) (s1 : S) (a1 : A) (σ1 : State Unit) : Prop := + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar nA)) s1 a1 σ1 m → + ChargeFactsA st Ac s1 → (∀ u, σ1.schedulerAction ≠ .switchThread u) → + (∀ d p, σ1.curThread ∉ σ1.readyQueues d p) → CurInvA σ1 → ActIdleA σ1 → Clock a1 σ1.machineState → + fl4GetScRefillSufficient (Z := Unit) σ1.curSc σ1.consumedTime σ1 = ({(false, σ1)}, False) → + ¬ NoTimeoutA σ1.consumedTime σ1 → ¬ TimeoutRecvA st Z E s1 σ1 → + sched.check_budget st ar s1 a1 ⦃ r => ∃ τ, fl4CheckBudget (Z := Unit) σ1 = ({(r.1, τ)}, False) ∧ + JStage3QA st ar AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes nA m + r.2.1 r.2.2 τ ⦄ + +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s : S} {a : A} {σ : State Unit} {m : McState} + +/-- **`SendPA` gives `kentryChargeIrqSendA`'s `hSendO`.** -/ +theorem hSendO_of_hSendP {Clock : A → MachineState → Prop} + (noSwitch : ∀ u, σ.schedulerAction ≠ .switchThread u) + (h : ∀ s1 a1 σ1, fl4UpdateTimestamp (Z := Unit) σ = ({((), σ1)}, False) → + SendPA st ar AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ nA m Clock s1 a1 σ1) : + ∀ s1 a1 σ1, fl4UpdateTimestamp (Z := Unit) σ = ({((), σ1)}, False) → + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar nA)) s1 a1 σ1 m → + ChargeEntryA st Ac s1 → CurInvA σ1 → ActIdleA σ1 → Clock a1 σ1.machineState → + fl4GetScRefillSufficient (Z := Unit) σ1.curSc σ1.consumedTime σ1 = ({(false, σ1)}, False) → + ¬ NoTimeoutA σ1.consumedTime σ1 → ¬ TimeoutRecvA st Z E s1 σ1 → + sched.check_budget st ar s1 a1 ⦃ r => ∃ τ, fl4CheckBudget (Z := Unit) σ1 = ({(r.1, τ)}, False) ∧ + JStage3QA st ar AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes nA m + r.2.1 r.2.2 τ ⦄ := by + intro s1 a1 σ1 e1 J1 En1 CI1 Act1 Cl1 hex hns hR + have eσ1 : σ1 = fl4TimestampState (rawNextA σ.machineState) { σ with machineState := rawStateA σ.machineState } := + (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst ((fl4UpdateTimestamp_eqA σ).symm.trans + e1)))).2.symm + have sw1 : ∀ u, σ1.schedulerAction ≠ .switchThread u := by rw [eσ1]; exact noSwitch + exact h s1 a1 σ1 e1 J1 ⟨En1.cA, fun r => ⟨(En1.run r).1, (En1.run r).2.1⟩⟩ sw1 (J1.sinv.ctQ (J1.resumeA En1.act)) + CI1 Act1 Cl1 hex hns hR + +set_option maxHeartbeats 4000000 in +/-- **`after_invocation` of a preempted invocation** (module docstring): the conclusion is the callers' `hPre` body. +[J] `take` and [J] `hch` are discharged (`take_ofA`; `chargeable` from `PreExitA`). -/ +theorem afterPreemptA {Cs : Set U64} {Clock : A → MachineState → Prop} {poll : SMonad Unit (Option Word)} + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) (hnA : nA = hPl.ipa) (K : IrqArchA ar AR (fun _ _ => True)) + (hMax : kernelConfig.maxIRQ = 5375) (hpoll : PollAgreeA ar AR poll) (hCs : ∀ c ∈ Cs, c.val < fl4A64Platform.numNodes) (one : fl4A64Platform.numNodes = 1) + (hnd : numDomains = 1) (hn : n = fl4A64Platform.numNodes) + (hag : fl4MinBudget = MIN_BUDGET) (hmb : ∀ a', Fl4MinBudgetAgreeA ar a') + (hLimit : ∀ a', sporadic.max_release_time ar a' ⦃ v => v.bv = fl4MaxReleaseTime ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) (hconv : Fl4TicksToUsLegacyAgree) + (clk : Clock a σ.machineState) + (hX : ∀ (Z : XSetsA) (H : Heap) (OS U Ac Pp Tb : Set U64) (Lv : U64 → Nat) (ia ie : Nat) (N E Rp : Set U64) + (Bf : Set (U64 × Fl4SpecA64.Word)) (stack : U64 → List U64) (κ : U64 → U64), Z.Cs = Cs → + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar nA)) s a σ m → BufOffXA st Z H OS Pp Tb Lv Bf s) + (hSend : ∀ (Z : XSetsA) (H : Heap) (OS U Ac Pp Tb : Set U64) (Lv : U64 → Nat) (ia ie : Nat) (N E Rp : Set U64) + (Bf : Set (U64 × Fl4SpecA64.Word)) (stack : U64 → List U64) (κ : U64 → U64), Z.Cs = Cs → + SendPA st ar AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ nA m Clock s a σ) + (hRel : A64RelPS3 st AR ar nA n Cs s a σ m) (hPre : PreExitA σ) + (line : ∀ (r : S × A) (τ : State Unit) (irq : U64) (a' : A) (τ' : State Unit), + syscall.mcs_preemption_point st ar s a = ok r → A64RelS3 st AR ar nA n Cs r.1 r.2 τ m → + ar.take_pending_irq r.2 = ok (irq, a') → irq ≠ syscall.NO_IRQ → (some irq.bv, τ') ∈ (poll τ).1 → + IrqLineBaseA st r.1 irq) : + syscall.after_invocation st ar s a (.Err .Preempted) ⦃ r => + r.1 = false ∧ StepPostA (fun s' a' υ => A64RelS3 st AR ar nA n Cs s' a' υ m) + (fl4PreemptionPath (pendingIrqA poll preemptServiceA)) σ + r.2 ⦄ := by + subst hn + obtain ⟨Z, H, OS, U, Ac, Pp, Tb, Lv, ia, ie, N, E, Rp, Bf, stack, κ, eCs, J, P⟩ := hRel + obtain ⟨noSwitch, hnq, hsc, hrel, hrun, hown, hch⟩ := hPre + have rec_ : ∀ s' a' τ, JStage3QA st ar AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes nA m + s' a' τ → BodyOKA τ m → A64RelS3 st AR ar nA fl4A64Platform.numNodes Cs s' a' τ m := by + rintro s' a' τ ⟨Q, Rl, co, cs, J1⟩ B + exact ⟨{ Z with Q := Q, R := Rl, curOf := co, curScOf := cs }, H, OS, U, Ac, Pp, Tb, Lv, ia, ie, N, E, Rp, Bf, + stack, κ, eCs, J1, B⟩ + have htZ : L.curW s ∈ Z.T := J.sr.curT + have hon : L.mem s (affA (L.curW s)) = L.cpu s := J.sr.curOn + have ct : σ.curThread = specPtr (L.curW s) := J.sr.sch.curThread + have hCsZ : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes := by rw [eCs]; exact hCs + have hcore := J.curScOnCoreA hCsZ one + have hni := P.scNotIdle + have eligible : sched.is_schedulable st s (L.curW s) = ok (schedulable σ.curThread σ) := by + rw [is_schedulable_eqA, schedulable_eqA J.sr.toSchedSRN htZ (J.sr.toSchedSRN.relOn htZ hon) J.stEqA, ct] + obtain ⟨y, k, hk0, R⟩ := J.sr.scr.sc _ J.sr.cscC + have hk : σ.kheap σ.curSc = some (.schedContext y k) := by + rw [J.sr.toSchedSRN.currentScA]; exact hk0 + have idleT : ∃ x, σ.kheap σ.idleThread = some (.tcb x) ∧ runnable x.tcbState = false := by + obtain ⟨x, hx, hs⟩ := J.sinv.idleTcb + exact ⟨x, hx, by rw [hs]; rfl⟩ + -- an unschedulable current thread is not resumed + have chosen : schedulable σ.curThread σ = false → σ.schedulerAction = .chooseNewThread := by + intro hun + cases hact : σ.schedulerAction with + | chooseNewThread => rfl + | switchThread u => exact absurd hact (noSwitch u) + | resumeCurThread => + exfalso + obtain ⟨t, ht⟩ := J.sinv.curTcb + have hr := hrun hact t ht + have hni' : σ.curThread ≠ σ.idleThread := fun e => by + obtain ⟨x, hx, hn⟩ := idleT + rw [← e, ht] at hx; cases hx; rw [hr] at hn; cases hn + obtain ⟨⟨x, p, hx, hp, yp, kp, hyp, hya⟩, hnr⟩ := J.schedValid.act hact hni' + rw [ht] at hx; cases hx + have ep : p = σ.curSc := by + rcases hsc t ht with e | e + · rw [e] at hp; cases hp + · exact Option.some.inj (hp.symm.trans e) + have := cfgSchedulableA ht hp hyp hr hya hnr + rw [hun] at this; cases this + have take := take_ofA (poll := poll) (nA := nA) hA hPl hnA K hCs one hMax + (fun a' => WP.spec_mono (hmb a') fun mb h => h.trans hag) hmax htu hpoll line + refine afterInvocationPreempt_refinesA (pending := pendingIrqA poll preemptServiceA) + (Rel := fun s' a' υ => A64RelS3 st AR ar nA fl4A64Platform.numNodes Cs s' a' υ m) + (Final := fun s' a' υ => A64RelS3 st AR ar nA fl4A64Platform.numNodes Cs s' a' υ m) + (cur_thread_ok s) eligible ?check ?readSC ?consumed ?charge ?clear take + case readSC => + intro _ + obtain ⟨active, ea, e⟩ := spec_existsA (sc_active_refinesA J.sr.scr J.sr.cscC hk0) + refine ⟨L.cscW s, active, eq_of_spec_fd (sched.cur_sc.spec s), ea, ?_⟩ + rw [J.sr.toSchedSRN.currentScA]; exact e + case consumed => + rw [eq_of_spec_fd (sched.consumed.specA s)] + exact congrArg ok (by rw [J.sr.rest.consumed]; rfl) + case check => + intro hs + obtain ⟨t, ht⟩ := J.sinv.curTcb + obtain ⟨hr, -, p, yp, kp, hp, hyp, hya⟩ := schedulable_tcbExA hs ht + have ep : p = σ.curSc := by + rcases hsc t ht with e | e + · rw [e] at hp; cases hp + · exact Option.some.inj (hp.symm.trans e) + rw [ep] at hp hyp + have ey : yp = y := by rw [hk] at hyp; cases hyp; rfl + have hAx : scActive y = true := ey ▸ hya + have hipc : ipcQueuedThreadState t.tcbState = false := by + cases h : t.tcbState <;> rw [h] at hr <;> first | rfl | (cases hr) + have I : CurInvA σ := ⟨⟨t, ht, hp, hipc⟩, J.sinv.curSc, fun _ y' n' h' => by + rw [hk] at h'; cases h'; exact hAx, hrel⟩ + have Idle : ActIdleA σ := by + refine ⟨fun u h => absurd h (noSwitch u), ?_⟩ + obtain ⟨x, yi, ki, hx, hb, -, -⟩ := J.sinv.idleSc + obtain ⟨x', hx', hs'⟩ := J.sinv.idleTcb + have ex : x' = x := by rw [hx] at hx'; cases hx'; rfl + refine ⟨x, hx, hb, ex ▸ hs', fun h => ?_⟩ + obtain ⟨z, hz, hzr⟩ := J.sinv.rlsRun _ h + rw [hx'] at hz; cases hz + rw [hs'] at hzr; cases hzr + have Fc := J.chargeFactsA hk hAx hsc hrel hon + have H0 := J.yieldQueueA + by_cases hex : fl4GetScRefillSufficient (Z := Unit) σ.curSc σ.consumedTime σ = ({(false, σ)}, False) + swap + · -- a sufficient head: nothing changes + have suff : sched.check_budget st ar s a ⦃ r => r = (true, s, a) ∧ + fl4CheckBudget (Z := Unit) σ = ({(true, σ)}, False) ∧ (OffReadyA σ → OffOKA σ) ⦄ := by + unfold sched.check_budget + rw [eq_of_spec_fd (sched.cur_sc.spec s), bind_ok, eq_of_spec_fd (sched.consumed.specA s), bind_ok] + have dec := entryBudgetDecisionA H0.sched.toSchedSRN I J.sinv.mc a (hmb a) + rw [eq_of_spec_fd (sched.cur_sc.spec s), bind_ok, eq_of_spec_fd (sched.consumed.specA s), bind_ok] at dec + refine WP.spec_bind dec ?_ + rintro b ⟨e, off⟩ + have hb : b = true := by + cases b + · exact absurd e hex + · rfl + subst hb + simp only [↓reduceIte, is_cur_domain_expired_eqA, bind_ok, WP.spec_ok] + refine ⟨rfl, ?_, off rfl⟩ + unfold fl4CheckBudget + rw [nd_gets_bindSc, nd_gets_bindSc, nd_bind_oneSc e, if_pos rfl, nd_gets_bindSc] + simp [Fl4Spec.NonDet.gets, Fl4Spec.NonDet.pure, isCurDomainExpired, hnd] + refine WP.spec_mono suff ?_ + rintro r ⟨er, es, off⟩ + subst er + refine ⟨σ, by rw [es]; exact Set.mem_singleton _, rec_ _ _ _ J.toQA ?_⟩ + exact ⟨⟨P.validTime, fun y' n' h' hg hc => ⟨P.ready y' n' h' hg hc, off (P.ready y' n' h' hg hc)⟩, P.ctAct⟩, + P.sw, P.scNotIdle, P.owner⟩ + · have done : sched.check_budget st ar s a ⦃ r => ∃ τ, fl4CheckBudget (Z := Unit) σ = ({(r.1, τ)}, False) ∧ + JStage3QA st ar AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes nA m + r.2.1 r.2.2 τ ⦄ := by + have T' := J.entryValidA (Clock := Clock) + by_cases hns : NoTimeoutA σ.consumedTime σ + · exact J.checkBudgetNoSendPreA noSwitch I Idle hcore hni hnq clk T' hag (hmb a) (hLimit a) hex hns hA Fc + by_cases hR : TimeoutRecvA st Z E s σ + · obtain ⟨xt, slot, ep', hbg, sd, rv, g, gr, d, q, hkt, hslot, hsl, hcap, hgc, he, hke⟩ := hR + refine WP.spec_mono (J.checkBudgetOfChargeA I (hmb a) hex hns fun used hu => + J.chargeSendRecvCoreA noSwitch I Idle hcore hni hCsZ one hag (hmb a) (hLimit a) hmax htu hconv hnq used + (J.chargeP1PreA hA Fc noSwitch hnq used true) (hX Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ eCs J) + hu hkt hslot hsl hcap hgc he hke) ?_ + rintro r ⟨τ, h, Q, -⟩ + exact ⟨τ, h, Q⟩ + · exact hSend Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ eCs J Fc noSwitch hnq I Idle clk hex hns hR + refine WP.spec_mono done ?_ + rintro r ⟨τ, h, Q⟩ + exact ⟨τ, by rw [h]; exact Set.mem_singleton _, + rec_ _ _ _ Q (bodyOKA_chargedPreA P (fl4CheckBudget_chargedA hs hex h))⟩ + case charge => + intro hun hact + refine WP.spec_mono (J.chargeUnschedPreA noSwitch hni hnq hsc hrel hown hun hact hch hon hA hag (hmb a) + (hLimit a) ⟨σ.consumedTime⟩) ?_ + rintro r ⟨τ, h, Q, hc0, ea, ec, et, em⟩ + refine ⟨τ, by rw [h]; exact Set.mem_singleton _, rec_ _ _ _ Q (bodyOKA_chargedPreA P ?_)⟩ + exact ⟨hc0, ea.trans (chosen hun), ec, et, congrArg (·.lastMachineTime) em⟩ + case clear => + intro hun _ + refine WP.spec_mono (clearConsumedWriteA (st := st) s a) ?_ + rintro r ⟨e1, e2⟩ + rw [e1, e2] + exact rec_ _ _ _ J.clearConsumedRecA (bodyOKA_chargedPreA P ⟨rfl, chosen hun, rfl, rfl, rfl⟩) + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3ArmAssignA.lean b/proofs/Fl4ProofsA64/P3/Stage3ArmAssignA.lean new file mode 100644 index 000000000..c3f6a8d45 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ArmAssignA.lean @@ -0,0 +1,640 @@ +/- +P3 (AArch64, merge line, D8 step 5 (ii)): **closing the `ARMASIDPoolAssign` arm** for the three root kinds (a Stage-1 +root, a Stage-2 root, a high window). FL4's decode restarts the thread, rewrites the root cap as mapped at the free +ASID and writes `root ||| tag` into the pool; l4v's `perform_asid_pool_invocation` (or the Ext assign) does the same. +The relation's ghost sets are kept; the record's tables / depths become those of the final-state J7 joined with `Z.S1` +(`P3/Stage3VsTyPostA`). The shape is `of_capVsFrameGA` with the pool word as the only VSpace write. + +* `SchedPageA.backAssignA`: no scheduler page is added by the rewrite to a mapped root cap. +* `UnmPtSpecA.ofAssignA`: J9 across the cap rewrite and the pool write. +* **`JStage3A.asidAssignA`**: arm hypotheses are FL4's decode-success facts (the ASID table names the pool, the pool + search finds an offset, the extra cap is an unassigned root) and `fin`: `SysL`, `BXI` at some `Ac'`, `UserOK` and J7 + at the final state. The root's emptiness (`UnmRootAssignA`) is derived from the record's J9. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3BodyKeepA +import Fl4ProofsA64.P3.VsAssignA +import Fl4ProofsA64.P3.Stage3CapVsFrameGA +import Fl4ProofsA64.P3.Stage3ArmPtUnmXA +import Fl4ProofsA64.P3.Stage3VsTyPostA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +theorem poolSetA_getO {σ : Fl4SpecA64.State Unit} {p : U64} {f : Fl4SpecA64.AsidPool} {q : Fl4SpecA64.ObjRef} + (h : q ≠ specPtr p) : (poolSetA σ p f).kheap q = σ.kheap q := by + simp [poolSetA, setObjA, Function.update_of_ne h] + +/-- **No scheduler page is added** by rewriting an unassigned root cap as assigned. -/ +theorem SchedPageA.backAssignA {Z : XSetsA} {OS : Set U64} {s s' : S} {x root oldAsid oldVa asid q : U64} + {k : ExtRootKind} (hx : x ∈ OS) (hold : capA st s x = assignUnmappedA k root oldAsid oldVa) + (hnew : capA st s' x = assignMappedA k root asid) (hkeep : ∀ u ∈ OS, u ≠ x → capA st s' u = capA st s u) + (h : SchedPageA st Z OS s' q) : SchedPageA st Z OS s q := by + unfold SchedPageA at h ⊢ + rcases h with h | h | h | h | h | h | h | ⟨u, hu, h⟩ + · exact Or.inl h + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr (Or.inl h)) + · exact Or.inr (Or.inr (Or.inr (Or.inl h))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inl h)))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl h))))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl h)))))) + · refine Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ⟨u, hu, ?_⟩)))))) + by_cases e : u = x + · subst e + rw [hnew] at h + rw [hold] + cases k with + | stage1 => + rcases h with ⟨m, a', v', h⟩ | ⟨m, a', h⟩ | ⟨a', v', h⟩ | ⟨q', a', v', h, -⟩ | ⟨y, a', h, -⟩ + · cases h + · injection h with h1 + subst h1 + exact Or.inr (Or.inl ⟨false, oldAsid, rfl⟩) + · cases h + · cases h + · cases h + | stage2 => + rcases h with ⟨m, a', v', h⟩ | ⟨m, a', h⟩ | ⟨a', v', h⟩ | ⟨q', a', v', h, -⟩ | ⟨y, a', h, -⟩ + · cases h + · cases h + · simp [assignMappedA] at h + · simp [assignMappedA] at h + · cases h + | high => + rcases h with ⟨m, a', v', h⟩ | ⟨m, a', h⟩ | ⟨a', v', h⟩ | ⟨q', a', v', h, -⟩ | ⟨y, a', h, -⟩ + · cases h + · cases h + · cases h + · cases h + · simp [assignMappedA] at h + · rw [hkeep u hu e] at h + exact h + +/-- **J9 across ASIDPool Assign**: an unmapped cap after is an unchanged one, and its tables are not the pool. -/ +theorem UnmPtSpecA.ofAssignA {Z : XSetsA} {OS Pt : Set U64} {s s' : S} {σ σ' : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} {p i : U64} (hp : p.val % 4096 = 0) (hi : i.val < 512) + (hS1al : ∀ q ∈ Z.S1, q.val % 4096 = 0) (hxal : ∀ r ∈ Z.X, 8192 ∣ r.val) (hyal : ∀ y ∈ Z.Y, 8192 ∣ y.val) + (hxup : ∀ r ∈ Z.X, wa r 4096#u64 ≠ p) (hyup : ∀ y ∈ Z.Y, wa y 4096#u64 ≠ p) + (hobj : ∀ u ∈ OS, CapObjOkA Z (capA st s u)) + (hptal : ∀ u ∈ OS, ∀ q a v, capA st s u = .PageTable q false a v → q.val % 4096 = 0) + (hbk : ∀ u ∈ OS, ∀ c, capA st s' u = c → (∀ q a v, c = .PageTable q false a v ∨ c = .Pml4 q false a ∨ + c = .Ept q 0#u64 false a v ∨ c = .HighVSpace q false a → capA st s u = c)) + (hword : ∀ q ∈ Pt, ∀ j : U64, j.val < 512 → peAF q j ≠ peAF p i → L.mem s' (peAF q j) = L.mem s (peAF q j)) + (link : ∀ q, SchedPageA st Z OS s q → q ∈ Pt) (hk : L.ksb s' = L.ksb s) (hcpu : L.cpu s' = L.cpu s) + (hset : ∀ q ao, q ≠ specPtr p → σ.kheap q = some (.archObj ao) → σ'.kheap q = some (.archObj ao)) + {f : Fl4SpecA64.AsidPool} (hpk : σ.kheap (specPtr p) = some (.archObj (.asidPool f))) + (h : UnmPtSpecA st Z m OS s σ) : UnmPtSpecA st Z m OS s' σ' := by + have al8 : ∀ y : U64, 8192 ∣ y.val → y.val % 4096 = 0 ∧ (wa y 4096#u64).val % 4096 = 0 := fun y hy => by + have h1 := y.bv.isLt + have h2 : y.val = y.bv.toNat := rfl + have h3 : (4096#u64 : U64).val = 4096 := rfl + rw [wa_valm, h3] + simp only [Nat.reducePow] at h1 ⊢ + omega + have words : ∀ q ∈ Pt, q.val % 4096 = 0 → q ≠ p → ∀ j : U64, j.val < 512 → + L.mem s' (peAF q j) = L.mem s (peAF q j) := + fun q hq ha ne j hj => hword q hq j hj (peAF_ne_tabA hp ha hi hj ne) + have ne_sp : ∀ q, q ≠ p → specPtr q ≠ specPtr p := fun q hn e => hn (specPtr_inj e) + have neO : ∀ q ao, σ.kheap (specPtr q) = some (.archObj ao) → (∀ g, ao ≠ .asidPool g) → q ≠ p := by + intro q ao hq hn e + rw [e, hpk] at hq + injection hq with h1 + injection h1 with h2 + exact hn f h2.symm + have tab : ∀ ty q, q ∈ Pt → q.val % 4096 = 0 → UnmTabA st Z m s σ ty q → UnmTabA st Z m s' σ' ty q := + fun ty q hq ha h => by + obtain ⟨t, k1, k2, k3⟩ := h.spec + have ne : q ≠ p := neO q _ k1 (fun _ e => by cases e) + exact ⟨fun j hj => by rw [words q hq ha ne j hj]; exact h.inv j hj, + ⟨t, hset _ _ (ne_sp q ne) k1, k2, k3⟩, h.off.of_ksA hk hcpu⟩ + refine ⟨fun u hu q asid va hc => ?_, fun u hu q asid hc => ?_, fun u hu q asid va hc => ?_, + fun u hu q asid hc => ?_⟩ + · have o := hbk u hu _ hc q asid va (Or.inl rfl) + have hq : q.val % 4096 = 0 := hptal u hu q asid va o + exact tab _ _ (link q (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inl ⟨_, _, _, o⟩⟩)))))))) hq (h.pt u hu q asid va o) + · have o := hbk u hu _ hc q asid 0#u64 (Or.inr (Or.inl rfl)) + have hok := hobj u hu + rw [o] at hok + exact tab _ _ (link q (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inl ⟨_, _, o⟩)⟩)))))))) (hS1al q (hok.2.2.2.1 q false asid rfl).1) (h.vs u hu q asid o) + · have o := hbk u hu _ hc q asid va (Or.inr (Or.inr (Or.inl rfl))) + have hok := hobj u hu + rw [o] at hok + have hqX := hok.2.1 q 0#u64 false asid va rfl + obtain ⟨a0, a1⟩ := al8 q (hxal q hqX) + have l0 : q ∈ Pt := link q (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inr (Or.inl ⟨_, _, o⟩))⟩))))))) + have l1 : wa q 4096#u64 ∈ Pt := link _ (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inr (Or.inr (Or.inl ⟨q, _, _, o, rfl⟩)))⟩))))))) + have h0 := h.s2 u hu q asid va o + obtain ⟨es, k1, k2⟩ := h0.spec + have n0 : q ≠ p := neO q _ k1 (fun _ e => by cases e) + refine ⟨fun j hj => ?_, es, hset _ _ (ne_sp q n0) k1, k2⟩ + by_cases hlo : j.val < 512 + · rw [words q l0 a0 n0 j hlo]; exact h0.inv j hj + · obtain ⟨j', hj', ej⟩ := peAF_hiA q j (by omega) hj + have := h0.inv j hj + rw [ej] at this ⊢ + rw [words _ l1 a1 (hxup q hqX) j' hj']; exact this + · have o := hbk u hu _ hc q asid 0#u64 (Or.inr (Or.inr (Or.inr rfl))) + have hok := hobj u hu + rw [o] at hok + have hqY := hok.2.2.1 q false asid rfl + obtain ⟨-, a1⟩ := al8 q (hyal q hqY) + have l1 : wa q 4096#u64 ∈ Pt := link _ (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inr (Or.inr (Or.inr ⟨q, _, o, rfl⟩)))⟩))))))) + have h0 := h.hv u hu q asid o + obtain ⟨l, k1, k2⟩ := h0.spec + have n0 : q ≠ p := neO q _ k1 (fun _ e => by cases e) + refine ⟨fun j hj => ?_, l, hset _ _ (ne_sp q n0) k1, k2⟩ + rw [words _ l1 a1 (hyup q hqY) j hj]; exact h0.inv j hj + +end + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +set_option maxHeartbeats 8000000 in +/-- **`JStage3A` across the `ARMASIDPoolAssign` arm** (module docstring). -/ +theorem JStage3A.asidAssignA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {ar : machine.Arch A} {nA : U64} {s : S} {a : A} + {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) + (hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM) + (cte : Fl4SpecA64.CslotPtr) (hab : AbsSlotA Z.th cte) (p base : U64) + (hpool : capA st s (cslotAddrA cte) = .AsidPool p base) + (length buf : U64) (block call cd fp : Bool) (ex : Aeneas.Std.Array obj.Slot 3#usize) + (dst : Fl4SpecA64.CslotPtr) (hd : AbsSlotA Z.th dst) (hex : vspace.extra ex 0#usize = ok (some (cslotAddrA dst))) + (k : Fl4SpecA64.ExtRootKind) (root oldAsid oldVa : U64) + (hcap : capA st s (cslotAddrA dst) = assignUnmappedA k root oldAsid oldVa) + (htab : ∀ hi : U64, hi.val = base.val / 512 → L.mem s (atAF (L.ksb s) hi) = p) (hp0 : p ≠ 0#u64) + (i : U64) (hsearch : vspace.decode_loop1 st s base p 0#u64 = ok i) (hfull : i ≠ 512#u64) + (fin : ∀ (x : Fl4SpecA64.Tcb) (s3 : S) (σ3 : Fl4SpecA64.State Unit), + σ.kheap σ.curThread = some (.tcb x) → + syscall.decode_invocation st ar s a fl4_abi.label.ARMASIDPoolAssign length (cslotAddrA cte) + (.AsidPool p base) block call cd fp buf ex = ok (core.result.Result.Ok (), s3, a) → + (.ok ([] : List Fl4SpecA64.Data), σ3) ∈ ((fl4ArchPerformInvocation (Z := Unit) + (assignInvA k (specPtr p) dst (asidOfA (wa base i)))).run (stUpdA σ σ.curThread x .restart)).1 → + ∃ Ac' : Set U64, SysL st H Z.Cs OS ia ie s3 ∧ SymL st H Z.Cs s3 ∧ + BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac' s3 ∧ + Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s3 ∧ + J7A st (J7R2A st ar nA) Z H OS s3 σ3 m) : + ∃ (x : Fl4SpecA64.Tcb) (s3 : S) (σ3 : Fl4SpecA64.State Unit) (Ac' Tb' : Set U64) (Lv' : U64 → Nat), + σ.kheap σ.curThread = some (.tcb x) ∧ + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + syscall.decode_invocation st ar s a fl4_abi.label.ARMASIDPoolAssign length (cslotAddrA cte) + (.AsidPool p base) block call cd fp buf ex = ok (core.result.Result.Ok (), s3, a) ∧ + (∀ (calling : Bool) (args : List Fl4SpecA64.Data) (xSlot : Fl4SpecA64.CapRef) (c0 : Fl4SpecA64.CslotPtr) + (rest : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)), + (.ok (assignInvA k (specPtr p) dst (asidOfA (wa base i))), σ) ∈ ((fl4ArchDecodeInvocation (Z := Unit) + calling (archL .armASIDPoolAssign) args xSlot c0 (.asidPoolCap (specPtr p) (asidOfA base)) + ((.archObjectCap (assignRootCapA k (specPtr root) none), dst) :: rest)).run σ).1) ∧ + (.ok ([] : List Fl4SpecA64.Data), σ3) ∈ ((fl4ArchPerformInvocation (Z := Unit) + (assignInvA k (specPtr p) dst (asidOfA (wa base i)))).run (stUpdA σ σ.curThread x .restart)).1 ∧ + JStage3A st AR Z H OS U Ac' Pp Tb' Lv' ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s3 a σ3 m ∧ + Sched.BodyKeepA (stUpdA σ σ.curThread x .restart) σ3 := by + have E0 := J.toJEntA + have hA := hd.isSlot E0.j.1.place + have hx : cslotAddrA dst ∈ OS := RS.sub _ hA + have hO : OSOK H.tcbs H.omega H.frame OS := J.sys.ent.os + -- the pool cap and the ASID table + obtain ⟨_, _, hbase, _⟩ := StateRelA.getAbsA E0.j.1.st hab + change CapWfA (capA st s (cslotAddrA cte)) at hbase + rw [hpool] at hbase + have hb : base.val < 65536 := hbase.1 + obtain ⟨hi, hshr, hv⟩ := shrA base 9#u64 512 (by decide) rfl + have hhi : hi.val < 128 := by omega + have hq := htab hi hv + have hm : hi &&& 127#u64 = hi := + UScalar.eq_of_val_eq (by rw [and_maskA _ 7 _ rfl]; exact Nat.mod_eq_of_lt hhi) + have hread := asid_table_eq (st := st) s hi + rw [hm] at hread + have h0 : L.mem s (atAF (L.ksb s) hi) ≠ 0#u64 := by rw [hq]; exact hp0 + have ht := E0.j.1.vs.table hi hhi + rw [hv, ← asidHi_eqA hb] at ht + change Fl4SpecA64.asidTable σ (Fl4SpecA64.asidHighBitsOf (asidOfA base)) = _ at ht + rw [optPtrA_ne h0, hq] at ht + have hpP : p ∈ Z.Pp := hq ▸ E0.j.1.vs.tmem hi hhi h0 + have hactP : ActivePoolA (L.mem s) (L.ksb s) p := ⟨hp0, hi, hhi, hq⟩ + obtain ⟨f, hk, hpool0⟩ := E0.j.1.vs.pool p hpP + have hpoolE := hpool0 hactP + -- the search + have hS := assign_searchA (st := st) s base p + rw [hsearch, WP.spec_ok] at hS + obtain ⟨hiLe, -, hfree⟩ := hS + have hi512 : i.val < 512 := Nat.lt_of_le_of_ne hiLe (fun h => hfull (UScalar.eq_of_val_eq h)) + obtain ⟨hasid, hentry⟩ := hfree hi512 + obtain ⟨_, h16, hmask⟩ := assign_sumA hbase hi512 + have hPoolOff := E0.poolOff hi + rw [hread, hq, WP.spec_ok] at hPoolOff + have w : PoolW st H Z.Cs OS s (peAF p i) := by + simpa only [hmask, peAF] using hPoolOff hp0 (wa base i) + have hf := (assign_free_iffA hbase hi512 (hpoolE i hi512)).2 ⟨hasid, hentry⟩ + have hDec := (assignDec_memA (assignInvA k (specPtr p) dst) ht hk hf).1 + rw [assign_asidA hbase hi512] at hDec + -- the unassigned root is empty (J9) + have hunm : UnmRootAssignA k (L.mem s) σ root := by + cases k with + | stage1 => + have h0 := J.unmPt.vs _ hx root oldAsid hcap + obtain ⟨t, k1, -, k3⟩ := h0.spec + exact ⟨h0.inv, t, k1, k3⟩ + | stage2 => + have h0 := J.unmPt.s2 _ hx root oldAsid oldVa hcap + exact ⟨h0.inv, h0.spec⟩ + | high => + have h0 := J.unmPt.hv _ hx root oldAsid hcap + exact ⟨h0.inv, h0.spec⟩ + -- the new cap + have hOld : CapRt.EncWf (assignUnmappedA k root oldAsid oldVa) := + hcap ▸ (CapRt.decodeF_encWf _ _ : CapRt.EncWf (capA st s (cslotAddrA dst))) + obtain ⟨hEnc, hrel, habs, hshape, -⟩ := assignCapFactsA k hOld h16 + have hOldObj := E0.j.capObj hA + change CapObjOkA Z (capA st s (cslotAddrA dst)) at hOldObj + rw [hcap] at hOldObj + have hObj := CapObjOkA.of_shapeA hshape hOldObj (fun _ => by cases k <;> trivial) + have hn1 : assignMappedA k root (wa base i) ≠ .Null := by cases k <;> intro h <;> cases h + have hn0 : assignUnmappedA k root oldAsid oldVa ≠ .Null := by cases k <;> intro h <;> cases h + -- restart and the cap write + obtain ⟨x, s2, σ2, hkx, hrestart, hrestartS, hs, hsS, Hs, hks, R, hcpu2, hw2, -, -⟩ := + restart_setcapJA E0 RS dst hd hx hEnc hrel + obtain ⟨x', hkx', heq, hset, J1, -, hOS1, -⟩ := J.of_restartCurA + RS.pl.choose_spec.choose_spec.choose_spec.choose_spec RS.sym RS.run hnvm RS.cur RS.vs RS.vc + have exx : x' = x := by rw [hkx] at hkx'; cases hkx'; rfl + subst exx + have hks1 : L.ksb (L.wr s (stA (L.curW s)) sched.TS_RESTART) = L.ksb s := L.ksb_wr _ _ _ + have hcpu1 : L.cpu (L.wr s (stA (L.curW s)) sched.TS_RESTART) = L.cpu s := L.cpu_wr _ _ _ + have sA : Aligned (stA (L.curW s)) := by + rw [stA_eq]; exact field_aligned (E0.sq.tcbs _ E0.sq.curT) (by simp) (by simp) + generalize hs1 : L.wr s (stA (L.curW s)) sched.TS_RESTART = s1 at heq J1 hOS1 hks1 hcpu1 hs hw2 hrestart + have c1 : capA st s1 (cslotAddrA dst) = assignUnmappedA k root oldAsid oldVa := (hOS1.capA hx).trans hcap + have E1 := J1.toJEntA + -- the pool at the post-cap state + have htyR := TyKeepA.stUpdA (ts := .restart) hkx + have hk1 : (stUpdA σ σ.curThread x' .restart).kheap (specPtr p) = some (.archObj (.asidPool f)) := by + have hn : specPtr p ≠ σ.curThread := fun e => by rw [e, hkx] at hk; cases hk + simp [stUpdA, setObjA, Function.update_of_ne hn, hk] + have hk2 := Hs.keep hk1 (fun _ _ e => by cases e) (fun _ e => by cases e) + obtain ⟨hkind, hent⟩ := assignRootWordA E0 hd k hcap + -- the FL4 run + have hm8 : i * 8#u64 = ok (core.num.U64.wrapping_mul i 8#u64) := by + apply eq_of_spec_fd + step as ⟨y, hy⟩ + apply UScalar.eq_of_val_eq + rw [wm8_valA hi512] + simpa [Nat.mul_comm] using hy + have hdec : syscall.decode_invocation st ar s a fl4_abi.label.ARMASIDPoolAssign length (cslotAddrA cte) + (.AsidPool p base) block call cd fp buf ex = ok (core.result.Result.Ok (), + L.wr s2 (peAF p i) (root ||| assignTagWordA k), a) := by + rw [decodeAssignRunA ar s a k root p base oldAsid oldVa cte dst length buf block call cd fp ex hex hcap] + unfold assignFl4A + have hne : (p != p) = false := by simp + rw [asidLowA, hshr, bind_ok, hread, bind_ok, hq, if_neg hp0] + simp only [hne, Bool.false_eq_true, ↓reduceIte] + rw [hsearch, bind_ok, if_neg hfull] + simp only [lift, bind_ok, bind_tc_ok, hmask, hm8] + rw [hrestart, bind_ok, hs, bind_ok, L.write_eq, bind_ok] + rfl + -- the spec run + obtain ⟨ac, hg, heqc⟩ := getCap_eqA E1.j.1.st hd + change CapEqA (capA st s1 (cslotAddrA dst)) ac at heqc + rw [c1] at heqc + have hac : ac = .archObjectCap (assignRootCapA k (specPtr root) none) := by + rw [← habs]; exact (heqc.eq_abs (by cases k <;> simp [assignUnmappedA])).symm + subst hac + have hget : (.archObjectCap (assignRootCapA k (specPtr root) none), stUpdA σ σ.curThread x' .restart) ∈ + (Fl4SpecA64.getCap (Z := Unit) dst (stUpdA σ σ.curThread x' .restart)).1 := by rw [hg]; rfl + have hlo : Fl4SpecA64.asidLowBitsOf (asidOfA (wa base i)) = BitVec.ofNat 9 i.val := by + have hh := congrArg UScalar.val hmask + rw [and_maskA _ 9 _ rfl] at hh + exact (asidLo_eqA h16).trans (congrArg (BitVec.ofNat 9) hh) + have hperfAt : ∀ τ : Fl4SpecA64.State Unit, τ.kheap (specPtr p) = some (.archObj (.asidPool f)) → + ((), τ) ∈ (Fl4SpecA64.setCap (Z := Unit) (.archObjectCap (assignRootCapA k (specPtr root) + (some (asidOfA (wa base i))))) dst (stUpdA σ σ.curThread x' .restart)).1 → + (.ok ([] : List Fl4SpecA64.Data), poolSetA τ p (Function.update f (BitVec.ofNat 9 i.val) + (poolEntA (root ||| assignTagWordA k)))) ∈ ((fl4ArchPerformInvocation (Z := Unit) + (assignInvA k (specPtr p) dst (asidOfA (wa base i)))).run (stUpdA σ σ.curThread x' .restart)).1 := by + intro τ hτ hτS + have hstore := storePool_runA hτ (asidOfA (wa base i)) (some (.asidPoolVSpace (specPtr root))) + rw [hlo] at hstore + exact assignPerformA k (specPtr p) (specPtr root) dst (asidOfA (wa base i)) hget hτS + (by rw [hstore, hent]; rfl) + -- the final-state facts, and the record's new typing + obtain ⟨Ac', hsl, hsy, hbx, hus, hj7⟩ := fin x' _ _ hkx hdec (hperfAt σ2 hk2 hsS) + have al_x : Aligned (cslotAddrA dst) := hO.al0 hx + have al_x8 : Aligned (wa (cslotAddrA dst) 8#u64) := hO.al hx (k := 8#u64) (by simp) (by simp) + have vo : VrOK H.omega H.frame H.scs H.scBits OS Z.V ∅ (L.ksb s) (utVsPtA st Pp Tb Lv s) := J.vsOk + have hnA : ¬ AsW (L.ksb s) (peAF p i) := E0.vsLeafA.2.2 p hpP i hi512 + have hks3 : L.ksb (L.wr s2 (peAF p i) (root ||| assignTagWordA k)) = L.ksb s := (L.ksb_wr _ _ _).trans hks + have hcpu3 : L.cpu (L.wr s2 (peAF p i) (root ||| assignTagWordA k)) = L.cpu s := + (L.cpu_wr _ _ _).trans hcpu2 + have wS := writesIn_setCapA al_x al_x8 hs + have hAsK : ∀ b, AsW (L.ksb s) b → L.mem (L.wr s2 (peAF p i) (root ||| assignTagWordA k)) b = L.mem s b := by + intro b hb + have hb1 : Aligned b := vo.al b (Or.inr hb) + have hns : ¬ SlotWordA (cslotAddrA dst) b := fun h => vo.os b (Or.inr hb) (slotWord_inOS hO hx h) + have hvsR := RS.vs b (Or.inr hb) + rw [mem_wr_ne' w.al hb1 (fun e => hnA (e ▸ hb)), wS.mem_eq hb1 hns, ← hs1, + mem_wr_ne' sA hb1 (Ne.symm hvsR.2)] + have htbl : ∀ o : U64, 772352 ≤ o.val → o.val < 773376 → o.val % 8 = 0 → + L.mem (L.wr s2 (peAF p i) (root ||| assignTagWordA k)) + (wa (L.ksb (L.wr s2 (peAF p i) (root ||| assignTagWordA k))) o) ≠ 0#u64 → + L.mem (L.wr s2 (peAF p i) (root ||| assignTagWordA k)) + (wa (L.ksb (L.wr s2 (peAF p i) (root ||| assignTagWordA k))) o) ∈ Pp := by + intro o h1 h2 h8 hne + have e : L.mem (L.wr s2 (peAF p i) (root ||| assignTagWordA k)) + (wa (L.ksb (L.wr s2 (peAF p i) (root ||| assignTagWordA k))) o) = L.mem s (wa (L.ksb s) o) := by + rw [hks3]; exact hAsK _ ⟨o, h1, h2, h8, rfl⟩ + rw [e] at hne ⊢ + exact (J.sys.ent.vs.tbl o h1 h2 h8 hne).1 + obtain ⟨T, Lv', hty, hvok, hbufT⟩ := vsPostB_of_j7A (S1 := Z.S1) hj7 htbl + -- the wrapper + have hN := bxi_nodes E1.sys.ent.bxi + have hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s1 y = .Null := J1.j5.2.2 + have hTr : TreeA st OS Z.G.chains Z.G.par s1 := + J1.j.1.cdt.tree.grow J1.j5.2.1 (fun x hx hn => J1.j5.2.2 x hx hn) + have hptal : ∀ u ∈ OS, ∀ q a v, capA st s1 u = .PageTable q false a v → q.val % 4096 = 0 := by + intro u hu q a' v hc + rw [hOS1.capA hu] at hc + have hunm : UnmCap (capA st s u) q 4096 := Or.inl ⟨rfl, a', v, hc⟩ + obtain ⟨_, _, -, -, -, -, -, -, -, -, -, -, -, -, R7⟩ := J.j7 + obtain ⟨_, _, _, _, _, _, _, _, R2, -, -⟩ := R7 + obtain ⟨z, hz, hH⟩ := R2.core.lookup.vh.1 u hu q 4096 hunm + have hz12 : z = 12 := Nat.pow_right_injective (le_refl 2) (hz.symm.trans (by norm_num)) + have := J.sys.ent.heap.align q _ hH + rw [hz12] at this + exact this + have hirq : CapOkA (stUpdA σ σ.curThread x' .restart).interruptIrqNode dst + (.archObjectCap (assignRootCapA k (specPtr root) (some (asidOfA (wa base i))))) := by + refine ⟨fun irq hlt e => ?_, fun ⟨_, _, _, _, e⟩ => by cases k <;> cases e⟩ + have obj : ∃ o ac, (stUpdA σ σ.curThread x' .restart).kheap dst.1 = some o ∧ + Fl4SpecA64.capOf o dst.2 = some ac ∧ CapRelA (capAtA (L.mem s1) (cslotAddrA dst)) ac := by + obtain ⟨o, idx⟩ := dst + obtain ⟨p0, e1, hc | ⟨ht', i0, hi0, e2⟩⟩ := hd + · simp only at e1 hc; subst e1 + obtain ⟨cs, hk0, -, hr⟩ := J1.j.1.st.cnode p0 _ hc + obtain ⟨ac, hac, hrel0⟩ := hr idx rfl + exact ⟨_, ac, hk0, hac, hrel0⟩ + · simp only at e1 e2; subst e1 e2 + obtain ⟨y, hk0, hr⟩ := J1.j.1.st.tcb p0 ht' + obtain ⟨ac, hac, hrel0⟩ := hr i0 hi0 + refine ⟨_, ac, hk0, hac, ?_⟩ + unfold cslotAddrA; rw [ofBl_tcbIdx i0 (by unfold tcbSlotsA at hi0; omega)]; exact hrel0 + obtain ⟨o, ac, hko, hco, hrel0⟩ := obj + have c1' : capAtA (L.mem s1) (cslotAddrA dst) = assignUnmappedA k root oldAsid oldVa := c1 + rw [e] at hko + rw [c1'] at hrel0 + rcases J1.sinv.irqN.slot irq hlt o _ ac hko hco with rfl | ⟨_, _, _, rfl⟩ + · obtain ⟨-, h2⟩ := hrel0 + cases k <;> simp [assignUnmappedA, CapEqA, absCapA] at h2 + · obtain ⟨-, h2⟩ := hrel0 + cases k <;> simp [assignUnmappedA, CapEqA, absCapA] at h2 + have hsci : NoIdleCapV m n (.archObjectCap (assignRootCapA k (specPtr root) (some (asidOfA (wa base i))))) := + ⟨fun _ _ e => (by cases k <;> cases e), fun _ e => (by cases k <;> cases e), fun _ _ _ e => (by cases k <;> cases e)⟩ + have hM : MapAlikeA (assignMappedA k root (wa base i)) (capA st s1 (cslotAddrA dst)) := by + rw [c1] + cases k with + | stage1 => exact .pml4 .. + | stage2 => exact .ept .. + | high => exact .hv .. + have main := JStage3A.of_capVsFrameGA (hS := fun _ h => h) (hT2 := fun _ h => h) (hpt := fun _ _ _ h => by cases k <;> cases h) (J := J1) (S1' := Z.S1) (T2' := Z.T2) (Zl' := Z.Lv) (Sh2' := Z.Sh2) + (Tb' := Z.S1 ∪ T) (Lv' := Lv') + (Q := fun s' a' σ' hw => s' = L.wr s2 (peAF p i) (root ||| assignTagWordA k) ∧ a' = a ∧ + (.ok ([] : List Fl4SpecA64.Data), σ') ∈ ((fl4ArchPerformInvocation (Z := Unit) + (assignInvA k (specPtr p) dst (asidOfA (wa base i)))).run (stUpdA σ σ.curThread x' .restart)).1 ∧ hw = Z.hw) + RS.sub (footNOff_ksA rfl rfl (footN_offA E1)) (footNAl_ksA rfl rfl (FootNA.aligned E1.j.1 hN E1.scAl E1.ap.vcAl)) + dst hd hEnc hrel hTr hnul hn1 (by rw [c1]; exact hn0) hM hObj hirq hsci + (fun s2' σ2' hs2 hσ2 HSs wc jx hcx => by + have es : s2' = s2 := okinj (hs2.symm.trans hs) + subst es + have hk2' := HSs.keep hk1 (fun _ _ e => by cases e) (fun _ e => by cases e) + have hperf := hperfAt σ2' hk2' hσ2 + have hjks : L.ksb s2' = L.ksb s := hks + have halN := FootNA.aligned E0.j.1 (bxi_nodes E0.sys.ent.bxi) E0.scAl E0.ap.vcAl + have hvc := RS.vc + have hOSt := E0.stOffA hvc + have hcpw : ∀ b, FootNA (L := L) Z s1 b → L.mem s2' b = L.mem s1 b := fun b hb => + wc.mem_eq (footNAl_ksA rfl rfl (FootNA.aligned E1.j.1 hN E1.scAl E1.ap.vcAl) b hb) + (footNOff_ksA rfl rfl (footN_offA E1) b hb) + have hVsMem : ∀ b, VsFootA Z.Pp Z.S1 Z.Y (L.ksb s) b → L.mem s2' b = L.mem s b := fun b hb => by + rw [hcpw b (.vs b (by rw [hks1]; exact hb)), ← hs1, + mem_wr_ne' sA (halN _ (.vs b hb)) (Ne.symm (hOSt.vs b hb))] + have hExtMem : ∀ b, ExtFootE Z.X Z.Y Z.T2 b → L.mem s2' b = L.mem s b := fun b hb => by + rw [hcpw b (.ext b hb), ← hs1, mem_wr_ne' sA (halN _ (.ext b hb)) (Ne.symm (hOSt.ext b hb))] + have hzero := assignBankZeroA E0 hbase hhi hv hp0 hq hasid jx.1.vs HSs.asid + have hunmVs2 : (tagA (root ||| assignTagWordA k) = 0 → (root ||| assignTagWordA k) ≠ 0#u64 → + ((∀ j : U64, j.val < 512 → dBitA (L.mem s2' (peAF (untagA (root ||| assignTagWordA k)) j)) 0 = false) ∧ + ∀ t, σ2'.kheap (specPtr (untagA (root ||| assignTagWordA k))) = some (.archObj (.pageTable t)) → + ∀ j : U64, j.val < 512 → Fl4SpecA64.ptApply t j.bv = .invalidPTE)) ∧ + (tagA (root ||| assignTagWordA k) = 2 → + ((∀ j : U64, j.val < 512 → + dBitA (L.mem s2' (peAF (wa (untagA (root ||| assignTagWordA k)) 4096#u64) j)) 0 = false) ∧ + ∀ l1, σ2'.kheap (specPtr (untagA (root ||| assignTagWordA k))) = some (.archObj (.ext (.highVSpace l1))) → + ∀ j : Fin 512, l1 j = .invalidPTE)) := by + cases k with + | stage1 => + have hrootS1 : root ∈ Z.S1 := (hOldObj.2.2.2.1 root false oldAsid rfl).1 + obtain ⟨ht', hu⟩ := assignTagA (tag := assignTagWordA .stage1) (E0.j.1.vs.al_mod hrootS1) (by decide) + obtain ⟨hinv0, t0, hkt0, het0⟩ := hunm + have hkt2 := HSs.keep (stUpd_keep hkx hkt0 (fun _ h => by cases h)) (fun _ _ h => by cases h) + (fun _ h => by cases h) + refine ⟨fun _ _ => ?_, fun ht2 => by rw [ht'] at ht2; cases ht2⟩ + rw [hu] + refine ⟨fun j hj => by rw [hVsMem _ (.pt root j hrootS1 hj)]; exact hinv0 j hj, fun t hkt => ?_⟩ + rw [hkt2] at hkt; cases hkt; exact het0 + | stage2 => + have hrootX : root ∈ Z.X := hOldObj.2.1 root 0#u64 false oldAsid oldVa rfl + have hr : root.val % 4096 = 0 := + Nat.mod_eq_zero_of_dvd (dvd_trans (by decide : 4096 ∣ 8192) (E0.j.1.ext.xal root hrootX)) + obtain ⟨ht', -⟩ := assignTagA (tag := assignTagWordA .stage2) hr (by decide) + exact ⟨fun ht0 _ => (by rw [ht'] at ht0; cases ht0), fun ht2 => (by rw [ht'] at ht2; cases ht2)⟩ + | high => + have hrootY : root ∈ Z.Y := hOldObj.2.2.1 root false oldAsid rfl + have hr : root.val % 4096 = 0 := + Nat.mod_eq_zero_of_dvd (dvd_trans (by decide : 4096 ∣ 8192) (E0.j.1.vs.yal root hrootY)) + obtain ⟨ht', hu⟩ := assignTagA (tag := assignTagWordA .high) hr (by decide) + obtain ⟨hinv0, l1, hkl0, hel0⟩ := hunm + have hkl2 := HSs.keep (stUpd_keep hkx hkl0 (fun _ h => by cases h)) (fun _ _ h => by cases h) + (fun _ h => by cases h) + refine ⟨fun ht0 _ => (by rw [ht'] at ht0; cases ht0), fun _ => ?_⟩ + rw [hu] + refine ⟨fun j hj => by rw [hVsMem _ (.high root j hrootY hj)]; exact hinv0 j hj, fun l1' hkl => ?_⟩ + rw [hkl2] at hkl; cases hkl; exact hel0 + have hunmS2' : tagA (root ||| assignTagWordA k) = 1 → + ((∀ j : U64, j.val < 1024 → dBitA (L.mem s2' (peAF (untagA (root ||| assignTagWordA k)) j)) 0 = false) ∧ + ∀ es, σ2'.kheap (specPtr (untagA (root ||| assignTagWordA k))) = some (.archObj (.ext (.stage2Root es))) → + ∀ j : Fin 1024, es j = .invalidPTE) := by + cases k with + | stage1 => + have hrootS1 : root ∈ Z.S1 := (hOldObj.2.2.2.1 root false oldAsid rfl).1 + obtain ⟨ht', -⟩ := assignTagA (tag := assignTagWordA .stage1) (E0.j.1.vs.al_mod hrootS1) (by decide) + exact fun ht1 => by rw [ht'] at ht1; cases ht1 + | stage2 => + have hrootX : root ∈ Z.X := hOldObj.2.1 root 0#u64 false oldAsid oldVa rfl + have hr : root.val % 4096 = 0 := + Nat.mod_eq_zero_of_dvd (dvd_trans (by decide : 4096 ∣ 8192) (E0.j.1.ext.xal root hrootX)) + obtain ⟨-, hu⟩ := assignTagA (tag := assignTagWordA .stage2) hr (by decide) + obtain ⟨hinv0, es0, hke0, hee0⟩ := hunm + have hke2 := HSs.keep (stUpd_keep hkx hke0 (fun _ h => by cases h)) (fun _ _ h => by cases h) + (fun _ h => by cases h) + intro _ + rw [hu] + refine ⟨fun j hj => by + rw [hExtMem _ (rootFootE hrootX (E0.j.1.ext.xal root hrootX) hj)]; exact hinv0 j hj, fun es hke => ?_⟩ + rw [hke2] at hke; cases hke; exact hee0 + | high => + have hrootY : root ∈ Z.Y := hOldObj.2.2.1 root false oldAsid rfl + have hr : root.val % 4096 = 0 := + Nat.mod_eq_zero_of_dvd (dvd_trans (by decide : 4096 ∣ 8192) (E0.j.1.vs.yal root hrootY)) + obtain ⟨ht', -⟩ := assignTagA (tag := assignTagWordA .high) hr (by decide) + exact fun ht1 => by rw [ht'] at ht1; cases ht1 + obtain ⟨J3, -, -⟩ := assign_write_jointA E0 RS jx hjks hcpu2 hpP hi512 w hk2' hkind hzero hunmVs2 hunmS2' + -- the writes + have wP : WritesIn st s2' (L.wr s2' (peAF p i) (root ||| assignTagWordA k)) (fun b => b = peAF p i) := + (writesIn_wr w.al).mono fun b hb => hb + have wS' := writesIn_setCapA al_x al_x8 hs2 + obtain ⟨Pt, Ub, vr, -, -, -, -, -, -, link, -, -⟩ := J1.j7 + obtain ⟨Qb, Rb, q⟩ := J1.sys.ent.bxi.1 + have vP : VsR (L.ksb s1) Pt (peAF p i) := vsW_pageA link (Or.inl hpP) hi512 + have offT : ∀ t ∈ Z.T, (peAF p i).val < t.val ∨ t.val + 4096 ≤ (peAF p i).val := fun t ht' => by + have hth : t ∈ H.tcbs := by rw [J1.tcbs]; exact ht' + exact (q.omega _ (vr.om _ vP)).2.1 t hth + have osk3 : OSKeep st OS s2' (L.wr s2' (peAF p i) (root ||| assignTagWordA k)) := + osKeep_ofA hO fun w' hw' hin => wP.mem_eq hw' (fun e => vr.os w' (e ▸ vP) hin) + obtain ⟨hcx', hco⟩ := capA_setCapA hO hx hEnc hs2 + have hnew : capA st (L.wr s2' (peAF p i) (root ||| assignTagWordA k)) (cslotAddrA dst) = + assignMappedA k root (wa base i) := by rw [osk3.capA hx, hcx'] + have hkeep : ∀ u ∈ OS, u ≠ cslotAddrA dst → + capA st (L.wr s2' (peAF p i) (root ||| assignTagWordA k)) u = capA st s1 u := fun u hu e => by + rw [osk3.capA hu, hco u hu e] + have hMs2 : ∀ u ∈ OS, MapAlikeA (capA st s2' u) (capA st s1 u) := fun u hu => by + by_cases e : u = cslotAddrA dst + · subst e; rw [hcx']; exact hM + · rw [hco u hu e]; exact .refl _ + have sbS : ∀ t ∈ Z.T, sbWordA t ∈ OS := fun t ht' => + RS.sub _ ⟨t, Or.inr ⟨(J1.j.1.thT t).2 ht', 2, by simp [tcbSlotsA], (bufSlot_eqA t).symm⟩⟩ + have wA : WritesIn st s1 s2' (fun b => Aligned b ∧ W7.CapW H.tcbs {b | InOS OS b} ∅ b) := writesIn_alAG wc + have hB : ∀ b, (Aligned b ∧ W7.CapW H.tcbs {b | InOS OS b} ∅ b) → W7.CapW H.tcbs H.omega H.frame b := + fun b hb => os_capw hO hb.1 hb.2 + obtain ⟨-, -, -, -, -, fld, -, -⟩ := W7.blk_of_capw (BXI.blk J1.sys.ent.bxi) wA hB + have ib : ∀ t ∈ Z.T, L.mem s2' (ibWordA t) = L.mem s1 (ibWordA t) := fun t ht' => + fld t (by rw [J1.tcbs]; exact ht') 3512#u64 (by simp) (by simp) (by simp) + have HS2 : VsSpecA σ2' (poolSetA σ2' p (Function.update f (BitVec.ofNat 9 i.val) + (poolEntA (root ||| assignTagWordA k)))) := by + refine ⟨rfl, fun q0 => ?_, fun _ _ h => h⟩ + by_cases e : q0 = specPtr p + · subst e + exact Or.inr ⟨_, _, hk2', by simp [poolSetA, setObjA]; rfl, fun _ h => (by cases h), + fun _ h => (by cases h)⟩ + · exact Or.inl (poolSetA_getO e) + have tw : ∀ t ∈ Z.T, ∀ K : U64, K.val + 8 ≤ 4096 → K.val % 8 = 0 → + L.mem (L.wr s2' (peAF p i) (root ||| assignTagWordA k)) (wa t K) = L.mem s2' (wa t K) := by + intro t ht' K hK' h8 + have hta := J1.sr.core.tcbs t ht' + have hvv : (wa t K).val = t.val + K.val := field_val hta (K := K) (by omega) + refine wP.mem_eq (field_aligned hta (by omega) h8) (fun e => ?_) + have := offT t ht' + rw [← e] at this + omega + have hib : ∀ t ∈ Z.T, IbSame (L := L) s2' (L.wr s2' (peAF p i) (root ||| assignTagWordA k)) t := + fun t ht' => by + refine ⟨tw t ht' _ (by simp) (by simp), ?_, ?_⟩ + · rw [sbWordA_eq]; exact tw t ht' _ (by simp) (by simp) + · rw [sbWordA8_eq]; exact tw t ht' _ (by simp) (by simp) + have hj7' := hj7 + obtain ⟨Pt', Ub', vr', -, ac', -⟩ := hj7' + have hBw : ∀ b, (SlotWordA (cslotAddrA dst) b ∨ b = peAF p i) → + L.Free s1 H.omega Z.Cs b ∨ W7.TLow H.tcbs b := fun b hb => by + rcases hb with hsl' | rfl + · have hal : Aligned b := by rcases hsl' with rfl | rfl; exact al_x; exact al_x8 + exact (os_capw hO hal (Or.inr ⟨slotWord_inOS hO hx hsl', Set.notMem_empty _⟩)).elim Or.inr + fun h' => Or.inl (Or.inl h'.1) + · exact Or.inl (Or.inl (vr.om _ vP)) + have hsp : ∀ q', SchedPageA st Z OS (L.wr s2' (peAF p i) (root ||| assignTagWordA k)) q' → + SchedPageA st Z OS s1 q' := fun q' h => SchedPageA.backAssignA hx c1 hnew hkeep h + have hks31 : L.ksb (L.wr s2' (peAF p i) (root ||| assignTagWordA k)) = L.ksb s1 := hks3.trans hks1.symm + have hcpu31 : L.cpu (L.wr s2' (peAF p i) (root ||| assignTagWordA k)) = L.cpu s1 := hcpu3.trans hcpu1.symm + have G := JStage3A.keepGhostA (hw := Z.hw) (s' := L.wr s2' (peAF p i) (root ||| assignTagWordA k)) J1 hks31 hcpu31 + have hunm9 : UnmPtSpecA st Z m OS (L.wr s2' (peAF p i) (root ||| assignTagWordA k)) + (poolSetA σ2' p (Function.update f (BitVec.ofNat 9 i.val) (poolEntA (root ||| assignTagWordA k)))) := by + have hk1p : (stUpdA σ σ.curThread x' .restart).kheap (specPtr p) = some (.archObj (.asidPool f)) := hk1 + refine UnmPtSpecA.ofAssignA (Pt := Pt) (p := p) (i := i) (Nat.mod_eq_zero_of_dvd (E0.j.1.vs.pal p hpP)) hi512 + (fun _ h => E0.j.1.vs.al_mod h) E0.j.1.ext.xal E0.j.1.vs.yal + (fun r hr e => (E0.j.1.ext.pgd _ (Or.inr ⟨r, hr, Or.inr rfl⟩)).1 (Or.inl (e ▸ hpP))) + (fun y hy e => E0.j.1.ext.ypp y hy (e ▸ hpP)) + (fun u hu => by + by_cases hs0 : IsSlotA Z.th u + · exact J1.j.capObj hs0 + · rw [hnul u hu hs0] + exact ⟨fun _ h => (by cases h), fun _ _ _ _ _ h => (by cases h), fun _ _ _ h => (by cases h), + fun _ _ _ h => (by cases h), trivial⟩) hptal (fun u hu c hc qq asid va hk0 => ?_) + (fun qq hqq j hj hne => ?_) link hks31 hcpu31 (fun qq ao hne h => ?_) hk1p J1.unmPt + · by_cases e : u = cslotAddrA dst + · subst e + rw [hnew] at hc + rw [← hc] at hk0 + rcases hk0 with h | h | h | h <;> cases k <;> simp [assignMappedA] at h + · rw [← hkeep u hu e]; exact hc + · have hv' : VsR (L.ksb s1) Pt (peAF qq j) := Or.inl ⟨qq, hqq, core.num.U64.wrapping_mul j 8#u64, + by rw [wm8_valA hj]; omega, by rw [wm8_valA hj]; omega, rfl⟩ + rw [wP.mem_eq (vr.al _ hv') hne] + exact wS'.mem_eq (vr.al _ hv') (fun h => vr.os _ hv' (slotWord_inOS hO hx h)) + · rw [poolSetA_getO hne] + exact HSs.keep h (fun _ _ e => by cases e) (fun _ e => by cases e) + -- (12′) the tracked buffers are pinned in live frames, so they are off the final typed VSpace words + have hbv : ∀ b, BufW Bf b → ¬ VsR (L.ksb (L.wr s2' (peAF p i) (root ||| assignTagWordA k))) + (utVsPtA st Pp (Z.S1 ∪ T) Lv' (L.wr s2' (peAF p i) (root ||| assignTagWordA k))) b := + hbufT Z.T Bf fun bp hbp => (J1.bufOff.pin bp hbp).keepM fun u hu => by rw [osk3.capA hu]; exact hMs2 u hu + refine ⟨_, a, _, Z.hw, fun b => b = peAF p i, Ac', + ⟨wP, fun Pt'' Ub'' vr'' link'' b e => by rw [e]; exact vsW_pageA link'' (Or.inl hpP) hi512, + fun b e hbuf => J1.bufOff.page b hbuf p (Or.inl hpP) + (core.num.U64.wrapping_mul i 8#u64) (by rw [wm8_valA hi512]; omega) (by rw [wm8_valA hi512]; omega) + (by rw [e]; rfl), + HS2, rfl, ?_, J3, + ⟨⟨J1.sys.ent.heap, hbx, hO, hus, hty, by rw [hks3]; exact E0.sys.ent.ksw⟩, hsl⟩, hvok, hbv, + W7.inv_of_capw J1.sq (wS'.trans wP) hBw, ac'.case.po, ?_, ?_, hunm9, + ⟨J1.bufOff.os, fun b hb => by rw [hks31]; exact J1.bufOff.vs b hb, + fun b hb q' hq' => J1.bufOff.page b hb q' (hsp q' hq'), J1.bufOff.vcw, + fun bp hbp => (J1.bufOff.pin bp hbp).keepM fun u hu => by rw [osk3.capA hu]; exact hMs2 u hu⟩, + hj7, fun h => h, fun h => h, fun h => h, fun h => h, + G.1, Set.subset_union_left, G.2.1, G.2.2.1, G.2.2.2.1, G.2.2.2.2.1, G.2.2.2.2.2, hsy⟩, + rfl, rfl, hperf, rfl⟩ + · intro q0 dv pg h + have hne : q0 ≠ specPtr p := fun e => by rw [e, hk2'] at h; cases h + rw [poolSetA_getO hne]; exact h + · exact BufCover.vsA (BufCover.mapA J1.bufCover J1.bufNative J1.j.1.st jx.1.st + (fun t ht' => (J1.j.1.thT t).2 ht') HSs ib (fun t ht' => hMs2 _ (sbS t ht'))) HS2 hib + · exact BufNativeA.mapA J1.bufNative (fun t ht' q' sz' rr dv' m' e' a'' v' h => by + by_cases e1 : sbWordA t = cslotAddrA dst + · rw [e1, hnew] at h; cases k <;> cases h + · rw [hkeep _ (sbS t ht') e1] at h + have hn := J1.bufNative t ht' + rw [bufSlot_eqA, capAtA_eq (st := st), h] at hn + exact hn)) + obtain ⟨s2'', σ2'', s3, a3, σ3, hw, P, Ac'', hs2, hσk, HSsk, HSvk, HSc, wT, Jf, e3, ea, hperf, hhw⟩ := main + subst hhw e3 ea + refine ⟨x', _, σ3, Ac'', Z.S1 ∪ T, Lv', hkx, hset, hdec, fun calling args xSlot c0 rest => ?_, hperf, Jf, + (Sched.BodyKeepA.ofSlot HSsk (setCap_keepsA hσk).2.2).trans (Sched.BodyKeepA.ofVs HSvk)⟩ + rw [specDecodeAssignA] + exact hDec + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3ArmCnCopyA.lean b/proofs/Fl4ProofsA64/P3/Stage3ArmCnCopyA.lean new file mode 100644 index 000000000..af0caeff0 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ArmCnCopyA.lean @@ -0,0 +1,547 @@ +/- +P3 (AArch64, merge line, D8 step 5 (iii)): **the CNode Copy / Mint arm** — the caller's Restart, then `cdt::insert` +of the derived cap (cnode.rs `decode_copy`'s `insert_with_cap` with the source's cap), against l4v `cap_insert`, +keeps `JStage3A`. + +* `JStage3A.capInsertA` (Lift 1b): the arm after its Restart, with no Restart (the IRQ Handler Set arm, `Stage3LiftIrqSetA`, + inserts at the state after the deletion); `JStage3A.cnodeCopyA` is the Restart then `capInsertA`. +* `JStage3A.cnodeCopyA`: one `of_capDerivA` application: the refinement `cap_insert_refinesA` (its `hub` / `hsib` + from `JEntA`, as `cap_insert_dA`), J5 from `J5A.insA` at the ghost of the refinement (`sbp_eqA`), the slot caps + from `CapsFromA.of_insA`, the spec side from `capInsert_outA`. Final-state `SysL` and J7 are hypotheses + (Option A/X); the case hypotheses are the decode's facts about the derived cap and the two CNode slots. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3BodyKeepA +import Fl4ProofsA64.P3.Stage3CnInsA +import Fl4ProofsA64.P3.IpcXferLoopA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-- `HandlersActiveA` along `insert_with_cap`: the new cap's handler is the source's or of an active IRQ (Lift 1b; +`HandlersActiveA.ins_derived` with `hd` weakened). -/ +theorem HandlersActiveA.insGenA {G : List (List U64)} {s s' : S} (h : HandlersActiveA st G s) + {src dest : U64} (hsrc : src ∈ G.flatten) + (hk : ∀ y ∈ G.flatten, ∀ i, capA st s' y = .IrqHandler i → capA st s y = .IrqHandler i) + (hd : ∀ i, capA st s' dest = .IrqHandler i → capA st s src = .IrqHandler i ∨ + (i.val < 5376 ∧ L.mem s (irqStAddrA (L.ksb s) i) ≠ 0#u64)) + (hks : L.ksb s' = L.ksb s) + (hst : ∀ i : U64, i.val < 5376 → L.mem s' (irqStAddrA (L.ksb s) i) = L.mem s (irqStAddrA (L.ksb s) i)) : + HandlersActiveA st (insG src dest G) s' := fun y hy i k => by + rcases (mem_insG_flat hsrc).1 hy with rfl | hy0 + · rcases hd i k with hs | ⟨h1, h2⟩ + · have ⟨h1, h2⟩ := h src hsrc i hs + exact ⟨h1, by rw [hks, hst i h1]; exact h2⟩ + · exact ⟨h1, by rw [hks, hst i h1]; exact h2⟩ + · have ⟨h1, h2⟩ := h y hy0 i (hk y hy0 i k) + exact ⟨h1, by rw [hks, hst i h1]; exact h2⟩ + +/-- **`J5A.insA` with `hirq` weakened** (Lift 1b): the inserted cap `nc` is an `IrqHandler i` only when the source holds +one or no handler of `i` exists (a fresh issue) and the IRQ is active at `s`. -/ +theorem J5A.insGenA {Z : XSetsA} {H : Heap} {s : S} {σ σ' : Fl4SpecA64.State Unit} {m m' : Fl4SpecA64.McState} + (hO : OSOK H.tcbs H.omega H.frame OS) (J : J5A st Z H OS s σ m) + (hT : TreeA st OS Z.G.chains Z.G.par s) {src dest : U64} (hsrc : src ∈ OS) (hd : dest ∈ OS) + (hSs : IsSlotA Z.th src) (hSd : IsSlotA Z.th dest) + (hsn : capA st s src ≠ .Null) (hdn : capA st s dest = .Null) (hdc : obj.sext48 dest = ok dest) + {nc : capability.Cap} (hw : CapRt.EncWf nc) + (hsh : Fl4ProofsA64.P1.W7.R.ST.shapeA nc = Fl4ProofsA64.P1.W7.R.ST.shapeA (capA st s src)) + (hbd : badgeA nc = badgeA (capA st s src) ∨ badgeA (capA st s src) = 0#u64) + (hirqB : ∀ i, capA st s src = .IrqHandler i → nc = .IrqHandler i) + (hirqN : ∀ i, nc = .IrqHandler i → capA st s src = .IrqHandler i ∨ + ∀ y ∈ Z.G.chains.flatten, capA st s y ≠ .IrqHandler i) + (hirqA : ∀ i, nc = .IrqHandler i → capA st s src = .IrqHandler i ∨ + (i.val < 5376 ∧ L.mem s (irqStAddrA (L.ksb s) i) ≠ 0#u64)) + (hut : isUtA (capA st s src) = true → ∀ y, ¬ Fl4Proofs.P3.Mdb.Desc Z.G.par y src) + (hzb : isZbA (capA st s src) = false) + (hst : ∀ s', WritesIn st s s' (W7.CapW H.tcbs {b | InOS OS b} ∅) → + ∀ i : U64, i.val < 5376 → L.mem s' (irqStAddrA (L.ksb s) i) = L.mem s (irqStAddrA (L.ksb s) i)) : + cdt.insert_with_cap st s nc (capA st s src) src dest ⦃ s' => + WritesIn st s s' (W7.CapW H.tcbs {b | InOS OS b} ∅) ∧ capA st s' dest = nc ∧ + FullA (capA st s' src) (capA st s src) ∧ (∀ y ∈ OS, y ≠ src → y ≠ dest → capA st s' y = capA st s y) ∧ + J5A st { Z with G := ⟨insG src dest Z.G.chains, insParA Z.G.par src dest + (isPFA (Fl4ProofsA64.P1.W7.R.ST.revA st s src) (capA st s src) nc (revFA nc (capA st s src)))⟩ } H OS s' σ' m' ⦄ := by + have F := hT.lk.lkF hO.nz + have inOS : ∀ y ∈ Z.G.chains.flatten, y ∈ OS := fun y hy => ((hT.lk.members y).1 hy).1 + have hsG : src ∈ Z.G.chains.flatten := (hT.lk.members _).2 ⟨hsrc, hsn⟩ + have hdG : dest ∉ Z.G.chains.flatten := fun h => ((hT.lk.members _).1 h).2 hdn + have hsd : src ≠ dest := fun e => hdG (e ▸ hsG) + have hnN : NxA st s src ≠ 0#u64 → NxA st s src ∈ OS := fun h => inOS _ (F.nxm _ hsG h) + refine WP.spec_mono (WP.spec_and (WP.spec_and + (cdt.insert_with_cap.lrF (osokA hO) (LR.self s) hsrc hd hsd hdc rfl hsn hw hnN) + (cdt.insert_with_cap.srcF hO hsrc hd hsd rfl hw hnN)) + (insWC_bitsA hO hsrc hd hsd rfl hw hnN)) ?_ + rintro s' ⟨⟨⟨w, hL, ed, -, -, ek, -⟩, hfull⟩, hbk, hrd, hfd⟩ + have hc1 : Fl4ProofsA64.P1.W7.R.ST.shapeA (capA st s' src) = Fl4ProofsA64.P1.W7.R.ST.shapeA (capA st s src) ∧ + badgeA (capA st s' src) = badgeA (capA st s src) := by + rcases hfull with e | ⟨p, b, i, d, i1, h1, h2, -⟩ + · rw [e]; exact ⟨rfl, rfl⟩ + · rw [h1, h2]; exact ⟨rfl, rfl⟩ + have aOS : ∀ a, (a = dest ∨ a ∈ Z.G.chains.flatten) → a ∈ OS := fun a ha => ha.elim (fun e => e ▸ hd) (inOS a) + have C : InsCtxA st OS Z.G.chains Z.G.par s s' src dest nc (capA st s' src) := by + refine ⟨hT.lk, hT.tree, J.1.1, hsG, hdG, fun a ha => ?_, fun a ha => ?_, fun a ha => ?_, fun a ha => ?_, + hsh, hbd, hc1.1, hc1.2, hut, hzb⟩ + · have e := (hL a (aOS a ha)).2 + rw [e] + by_cases e1 : a = dest <;> by_cases e2 : a = src <;> simp [e1, e2, hsd, Ne.symm hsd] + · by_cases e1 : a = dest + · rw [if_pos e1, e1]; exact ed + · rw [if_neg e1] + by_cases e2 : a = src + · rw [if_pos e2, e2] + · rw [if_neg e2]; exact ek a (aOS a ha) e2 e1 + · by_cases e1 : a = dest + · rw [if_pos e1, e1]; exact hrd + · rw [if_neg e1]; exact (hbk a (aOS a ha) e1).1 + · by_cases e1 : a = dest + · rw [if_pos e1, e1]; exact hfd + · rw [if_neg e1]; exact (hbk a (aOS a ha) e1).2 + have hk : ∀ y ∈ Z.G.chains.flatten, ∀ i, capA st s' y = .IrqHandler i ↔ capA st s y = .IrqHandler i := + fun y hy i => by + by_cases e2 : y = src + · rw [e2] + rcases hfull with e | ⟨p, b, ii, d, i1, h1, h2, -⟩ + · rw [e] + · rw [h1, h2]; exact ⟨fun h => (by cases h), fun h => (by cases h)⟩ + · rw [ek y (inOS y hy) e2 (C.dne hy)] + refine ⟨w, ed, hfull, ek, ⟨C.inv, ?_, ?_⟩, J.2.1, fun y hy hns => ?_⟩ + · exact Fl4ProofsA64.P1.W7.R.ST.IrqRunA.ins J.1.2.1 hT.lk.nodup hsG hdG hk + (fun i h => hirqN i (ed.symm.trans h)) (fun i h => ed.trans (hirqB i h)) + · exact HandlersActiveA.insGenA J.1.2.2 hsG (fun y hy i h => (hk y hy i).1 h) + (fun i h => hirqA i (ed.symm.trans h)) w.1 (hst s' w) + · have f1 : y ≠ src := fun e => hns (e ▸ hSs) + have f2 : y ≠ dest := fun e => hns (e ▸ hSd) + rw [ek y hy f1 f2]; exact J.2.2 y hy hns + +set_option maxHeartbeats 4000000 in +/-- **`JStage3A` across a `cap_insert` of `nc` from `ss` into `sd`, without the Restart, the derivation facts as +hypotheses** (module docstring): `hno` / `hoffN` (the new cap's object and node facts), `hub` / `hsib` / `hbeq` (the +parent bit of the spec's `cap_insert`), `hJ5` (J5 across `insert_with_cap`). `SysL` / `SymL` at the final state and J7 +are hypotheses. -/ +theorem JStage3A.capInsertGenA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {ar : machine.Arch A} {nA : U64} {s : S} {a : A} + {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (ss sd : Fl4SpecA64.CslotPtr) {ps pd : U64} + (hCs : ss.1 = specPtr ps ∧ Z.th ps = some (.cnode ss.2.length)) + (hCd : sd.1 = specPtr pd ∧ Z.th pd = some (.cnode sd.2.length)) + (hsn : capA st s (cslotAddrA ss) ≠ .Null) (hdn : capA st s (cslotAddrA sd) = .Null) + (hdc : obj.sext48 (cslotAddrA sd) = ok (cslotAddrA sd)) + {nc : capability.Cap} {anc : Fl4SpecA64.Cap} (hwf : CapWfA nc) (hanc : CapEqA nc anc) + (hw : CapRt.EncWf nc) (hn : nc ≠ .Null) + (hD : Fl4ProofsA64.P3.DerivA nc (capA st s (cslotAddrA ss))) (hal : UA nc (capA st s (cslotAddrA ss))) + (hk : SameBadgeKindA nc (capA st s (cslotAddrA ss))) + (hut : isUtA (capA st s (cslotAddrA ss)) = true → ∀ y, ¬ Fl4Proofs.P3.Mdb.Desc Z.G.par y (cslotAddrA ss)) + (hdo : ¬ IrqNodeA (L.ksb s) (fl4Ptr sd.1) ∨ IrqCapA (absCapA nc)) + (hsci : NoIdleCapV m n anc) (hno : CapObjOkA Z nc) (hoffN : NodeOffA (L.ksb s) nc) + (hub : isUtA (capA st s (cslotAddrA ss)) = true → + insBA σ ss (absCapA nc) (absCapA (capA st s (cslotAddrA ss))) = true) + (hsib : insBA σ ss (absCapA nc) (absCapA (capA st s (cslotAddrA ss))) = false → + ∀ y, ¬ Fl4Proofs.P3.Mdb.Desc Z.G.par y (cslotAddrA ss)) + (hbeq : insBA σ ss (absCapA nc) (absCapA (capA st s (cslotAddrA ss))) = + isPFA (Fl4ProofsA64.P1.W7.R.ST.revA st s (cslotAddrA ss)) (capA st s (cslotAddrA ss)) nc + (revFA nc (capA st s (cslotAddrA ss)))) + (hJ5 : (∀ s', WritesIn st s s' (W7.CapW H.tcbs {b | InOS OS b} ∅) → + ∀ i : U64, i.val < 5376 → L.mem s' (irqStAddrA (L.ksb s) i) = L.mem s (irqStAddrA (L.ksb s) i)) → + ∀ σ' : Fl4SpecA64.State Unit, cdt.insert_with_cap st s nc (capA st s (cslotAddrA ss)) (cslotAddrA ss) + (cslotAddrA sd) ⦃ s' => + WritesIn st s s' (W7.CapW H.tcbs {b | InOS OS b} ∅) ∧ capA st s' (cslotAddrA sd) = nc ∧ + FullA (capA st s' (cslotAddrA ss)) (capA st s (cslotAddrA ss)) ∧ + (∀ y ∈ OS, y ≠ cslotAddrA ss → y ≠ cslotAddrA sd → capA st s' y = capA st s y) ∧ + J5A st { Z with G := ⟨insG (cslotAddrA ss) (cslotAddrA sd) Z.G.chains, insParA Z.G.par (cslotAddrA ss) + (cslotAddrA sd) (isPFA (Fl4ProofsA64.P1.W7.R.ST.revA st s (cslotAddrA ss)) (capA st s (cslotAddrA ss)) nc + (revFA nc (capA st s (cslotAddrA ss))))⟩ } H OS s' σ' m ⦄) + (symL : ∀ s2, cdt.insert st s nc (cslotAddrA ss) (cslotAddrA sd) = ok s2 → SymL st H Z.Cs s2) + (sysl : ∀ s2, cdt.insert st s nc (cslotAddrA ss) (cslotAddrA sd) = ok s2 → SysL st H Z.Cs OS ia ie s2) + (j7 : ∀ s2 σ2, cdt.insert st s nc (cslotAddrA ss) (cslotAddrA sd) = ok s2 → + ((), σ2) ∈ (Fl4SpecA64.capInsert (Z := Unit) anc ss sd σ).1 → + J7A st (J7R2A st ar nA) { Z with G := (insGA Z.G (cslotAddrA ss) (cslotAddrA sd) + (insBA σ ss (absCapA nc) (absCapA (capA st s (cslotAddrA ss))))) } H OS s2 σ2 m) : + ∃ s2 σ2, cdt.insert st s nc (cslotAddrA ss) (cslotAddrA sd) = ok s2 ∧ + ((), σ2) ∈ (Fl4SpecA64.capInsert (Z := Unit) anc ss sd σ).1 ∧ Sched.BodyKeepA σ σ2 ∧ + (∀ p y k, σ.kheap p = some (.schedContext y k) → σ2.kheap p = some (.schedContext y k)) ∧ + JStage3A st AR { Z with G := (insGA Z.G (cslotAddrA ss) (cslotAddrA sd) + (insBA σ ss (absCapA nc) (absCapA (capA st s (cslotAddrA ss))))) } + H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s2 a σ2 m := by + have hAs : AbsSlotA Z.th ss := ⟨ps, hCs.1, Or.inl hCs.2⟩ + have hAd : AbsSlotA Z.th sd := ⟨pd, hCd.1, Or.inl hCd.2⟩ + have J1 := J + have hO : OSOK H.tcbs H.omega H.frame OS := J1.sys.ent.os + have hP := J1.j.1.place + have hsS := hAs.isSlot hP + have hdS := hAd.isSlot hP + have xs : cslotAddrA ss ∈ OS := J1.j5.2.1 _ hsS + have xd : cslotAddrA sd ∈ OS := J1.j5.2.1 _ hdS + have cd0 : capA st s (cslotAddrA sd) = .Null := hdn + have E1 := J1.toJEntA + have hTr : TreeA st OS Z.G.chains Z.G.par s := + J1.j.1.cdt.tree.grow J1.j5.2.1 (fun x hx hn => J1.j5.2.2 x hx hn) + have hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null := J1.j5.2.2 + have hoff := footN_offA E1 + have hfal := FootNA.aligned J1.j.1 (bxi_nodes E1.sys.ent.bxi) E1.scAl E1.ap.vcAl + have hokAnc : CapOkA σ.interruptIrqNode sd anc := by + have h0 := capOk_insA E1.j.1.irq hdo hoffN + cases nc with + | PageTable p mp asid va => + cases mp + · rw [← hanc]; exact h0 + · obtain ⟨_, _, rfl, _, _⟩ := CapEqA.pt_mapped_iff.mp hanc + refine ⟨fun irq hirq' hsl => ?_, fun ⟨_, _, _, _, e⟩ => by cases e⟩ + have hi := h0.1 irq hirq' hsl + simp [IrqCapA, absCapA] at hi + | _ => rw [← hanc]; exact h0 + have hIns : cdt.insert st s nc (cslotAddrA ss) (cslotAddrA sd) = + cdt.insert_with_cap st s nc (capA st s (cslotAddrA ss)) (cslotAddrA ss) (cslotAddrA sd) := by + unfold cdt.insert; rw [capA_eq, bind_ok] + have hst0 : ∀ s', WritesIn st s s' (W7.CapW H.tcbs {b | InOS OS b} ∅) → + ∀ i : U64, i.val < 5376 → L.mem s' (irqStAddrA (L.ksb s) i) = L.mem s (irqStAddrA (L.ksb s) i) := + fun s' w i hi => w.mem_eq (hfal _ (.irq i hi)) (hoff _ (.irq i hi)) + have mJ := hJ5 hst0 σ + rw [← hIns] at mJ + have mv := WP.spec_and (cap_insert_refinesA s σ Z.G J1.j.1.st J1.j.1.cdt hP hO hTr J1.j5.2.1 hnul hAs hAd + hsn cd0 hdc hwf hanc hw hn hal hk hut hub hsib) mJ + obtain ⟨s2, hs2, ⟨σ2, hm, hst2, hcdt2, -, HS, hcs2⟩, w, ed, hfull, ek, -⟩ := spec_existsA mv + have j5' : J5A st { Z with G := (insGA Z.G (cslotAddrA ss) (cslotAddrA sd) + (insBA σ ss (absCapA nc) (absCapA (capA st s (cslotAddrA ss))))) } H OS s2 σ2 m := by + have mJ2 := hJ5 hst0 σ2 + rw [← hIns] at mJ2 + obtain ⟨s2', hs2', -, -, -, -, J5'⟩ := spec_existsA mJ2 + rw [hs2] at hs2'; have e := okinj hs2'; subst e + rw [hbeq]; exact J5' + have jx : JAtZAX (L := L) AR { Z with G := (insGA Z.G (cslotAddrA ss) (cslotAddrA sd) + (insBA σ ss (absCapA nc) (absCapA (capA st s (cslotAddrA ss))))) } s2 a σ2 m := by + refine J1.j.of_slotWrites w (fun b hb' hb => hoff b hb hb') hfal _ HS hst2 hcdt2 (fun y hy => ?_) + rcases hcs2 y hy with e | e | ⟨p, b, i, d, e⟩ <;> rw [e] + · exact J1.j.capObj hy + · exact hno + · exact ⟨fun _ h => (by cases h), fun _ _ _ _ _ h => (by cases h), fun _ _ _ h => (by cases h), + fun _ _ _ h => (by cases h), trivial⟩ + have F : CapsFromA st OS s s2 := + CapsFromA.of_insA xs xd hD cd0 ed hfull (fun p b i d h => hTr.ut.fib _ xs p b i d h) ek + -- the spec side + obtain ⟨asc, hgs, -⟩ := getCap_eqA J1.j.1.st hAs + obtain ⟨dc, hgd0, -⟩ := getCap_eqA J1.j.1.st hAd + have hgd : ∀ y ∈ (getCap (Z := Unit) sd σ).1, y.2 = σ := fun y hy => by + rw [hgd0] at hy; obtain rfl : y = (dc, σ) := hy; rfl + obtain ⟨σa, σb, v, h1, h2, ecdt, eorig, ekh⟩ := capInsert_outA hgs hgd hm + obtain ⟨c2, o2, -⟩ := setCap_keepsA h2 + have A1 : σa.cdt = σ.cdt ∧ σa.isOriginalCap = σ.isOriginalCap ∧ WfKeepA σ σa ∧ + ∀ p o', σa.kheap p = some o' → ∃ o, σ.kheap p = some o ∧ + (∀ b c, Fl4SpecA64.capOf o' b = some c → ((p, b) = ss ∧ c = maxFreeIndexUpdate asc) ∨ + Fl4SpecA64.capOf o b = some c) ∧ + ∀ n, (∃ cs, o' = .cnode n cs ∧ wellFormedCnodeN n cs) ↔ (∃ cs, o = .cnode n cs ∧ wellFormedCnodeN n cs) := by + rcases h1 with rfl | h1 + · exact ⟨rfl, rfl, WfKeepA.of_kheap rfl, fun p o h => ⟨o, h, fun _ _ h => Or.inr h, fun _ => Iff.rfl⟩⟩ + · obtain ⟨c1, o1, -⟩ := setCap_keepsA h1 + exact ⟨c1, o1, setCap_wfA _ _ _ _ h1, setCap_objsA h1⟩ + have W : WfKeepA σ σ2 := (A1.2.2.1.trans (setCap_wfA _ _ _ _ h2)).trans (WfKeepA.of_kheap ekh) + have hms : σ2.machineState = σ.machineState := capInsert_msA _ _ _ _ _ hm + have ec : σ2.cdt = Function.update σ.cdt sd (if v then some ss else σ.cdt ss) := by + rw [ecdt, c2, A1.1] + have eo : σ2.isOriginalCap = Function.update σ.isOriginalCap sd + (decide (Fl4SpecA64.isCapRevocable anc asc)) := by + rw [eorig, o2, A1.2.1] + have kk : ∀ r : Fl4SpecA64.CslotPtr, (r = ss ∨ r = sd) → σ.kheap r.1 ≠ none := by + intro r hr + rcases hr with e | e <;> rw [e] + · obtain ⟨cs, hk1, -⟩ := J1.j.1.st.cnode ps _ hCs.2 + rw [hCs.1, hk1]; simp + · obtain ⟨cs, hk1, -⟩ := J1.j.1.st.cnode pd _ hCd.2 + rw [hCd.1, hk1]; simp + have hcdtS : ∀ x y, σ2.cdt x = some y → ∀ sl, sl = x ∨ sl = y → + σ.kheap sl.1 ≠ none ∨ ∃ x0 y0, σ.cdt x0 = some y0 ∧ (sl = x0 ∨ sl = y0) := by + intro p q h sl hsl + rw [ec] at h + by_cases ep : p = sd + · rw [ep, Function.update_self] at h + rcases hsl with e | e + · exact Or.inl (kk _ (Or.inr (e.trans ep))) + · split at h + · cases h; exact Or.inl (kk _ (Or.inl e)) + · exact Or.inr ⟨ss, q, h, Or.inr e⟩ + · rw [Function.update_of_ne ep] at h + exact Or.inr ⟨p, q, h, hsl⟩ + have horig : ∀ p, σ2.isOriginalCap p = true → σ.kheap p.1 ≠ none ∨ σ.isOriginalCap p = true := fun p h => by + by_cases ep : p = sd + · exact Or.inl (kk _ (Or.inr ep)) + · rw [eo, Function.update_of_ne ep] at h; exact Or.inr h + have origLive : ∀ sl, σ2.isOriginalCap sl = true → AbsSlotA Z.th sl := fun p h => by + by_cases ep : p = sd + · rw [ep]; exact hAd + · rw [eo, Function.update_of_ne ep] at h; exact J1.origLive p h + have capE : ∀ sl c, specCapAtA σ2 sl = some c → + specCapAtA σ sl = some c ∨ (sl = sd ∧ c = anc) ∨ (sl = ss ∧ c = maxFreeIndexUpdate asc) := by + refine specCapAtA_orA (Q := fun sl c => (sl = sd ∧ c = anc) ∨ (sl = ss ∧ c = maxFreeIndexUpdate asc)) HS + (fun p o h => ?_) + rw [ekh] at h + obtain ⟨ob, kb, cb, wb⟩ := setCap_objsA h2 p o h + obtain ⟨oa, ka, ca, wa'⟩ := A1.2.2.2 p ob kb + refine ⟨oa, ka, fun b c hb => ?_, fun n => (wb n).trans (wa' n)⟩ + rcases cb b c hb with h | h + · exact Or.inl (Or.inl h) + · rcases ca b c h with h | h + · exact Or.inl (Or.inr h) + · exact Or.inr h + have hsAsc : specCapAtA σ ss = some asc := by + obtain ⟨cs, hk1, hwf1, hr1⟩ := J1.j.1.st.cnode ps _ hCs.2 + obtain ⟨c0, hc0, -⟩ := hr1 ss.2 rfl + have hk2 : σ.kheap ss.1 = some (.cnode ss.2.length cs) := by rw [hCs.1]; exact hk1 + have hm0 : (c0, σ) ∈ (getCap (Z := Unit) ss σ).1 := getCap_cnode_memA hk2 hwf1 hc0 + rw [hgs] at hm0 + have e0 : c0 = asc := (Prod.mk.inj hm0).1 + unfold specCapAtA; simp only [hk2]; rw [if_pos hwf1, ← e0]; exact hc0 + have hsc : ∀ sl c, specCapAtA σ2 sl = some c → (∃ sl0, specCapAtA σ sl0 = some c) ∨ NoIdleCapV m n c := + fun sl c h => by + rcases capE sl c h with h | ⟨-, e⟩ | ⟨-, e⟩ + · exact Or.inl ⟨sl, h⟩ + · exact Or.inr (e ▸ hsci) + · rw [e] + cases asc + all_goals first + | exact Or.inl ⟨ss, hsAsc⟩ + | exact Or.inr ⟨fun _ _ h => (by cases h), fun _ h => (by cases h), fun _ _ _ h => (by cases h)⟩ + have nullOrig : ∀ sl, specCapAtA σ2 sl = some .nullCap → σ2.isOriginalCap sl = false := fun sl h => by + by_cases f1 : sl = sd + · exfalso + rw [f1] at h + exact specCap_nnA jx.1.st hAd + (fun h' => hn ((show capAtA (L.mem s2) (cslotAddrA sd) = nc from ed).symm.trans h')) h + by_cases f2 : sl = ss + · exfalso + rw [f2] at h + refine specCap_nnA jx.1.st hAs (fun h' => ?_) h + have h'' : capA st s2 (cslotAddrA ss) = .Null := h' + rcases hfull with e | ⟨p, b, i, d, i1, -, e, -⟩ + · exact hsn (e.symm.trans h'') + · rw [e] at h''; cases h'' + rw [eo, Function.update_of_ne f1] + rcases capE sl _ h with h0 | ⟨e, -⟩ | ⟨e, -⟩ + · exact J1.nullOrig sl h0 + · exact absurd e f1 + · exact absurd e f2 + -- the buffer slots, the tables + have wA : WritesIn st s s2 (fun b => Aligned b ∧ W7.CapW H.tcbs {b | InOS OS b} ∅ b) := writesIn_alAG w + have hB : ∀ b, (Aligned b ∧ W7.CapW H.tcbs {b | InOS OS b} ∅ b) → W7.CapW H.tcbs H.omega H.frame b := + fun b hb => os_capw hO hb.1 hb.2 + obtain ⟨-, -, -, -, -, fld, -, -⟩ := W7.blk_of_capw (BXI.blk J1.sys.ent.bxi) wA hB + have sbS : ∀ t ∈ Z.T, sbWordA t ∈ OS := fun t ht => + J1.j5.2.1 _ ⟨t, Or.inr ⟨(J1.j.1.thT t).2 ht, 2, by simp [tcbSlotsA], (bufSlot_eqA t).symm⟩⟩ + have sbK : ∀ t ∈ Z.T, capA st s2 (sbWordA t) = capA st s (sbWordA t) := fun t ht => + ek _ (sbS t ht) (cnSlot_ne_sbA hP hCs.1 hCs.2 ((J1.j.1.thT t).2 ht)) + (cnSlot_ne_sbA hP hCd.1 hCd.2 ((J1.j.1.thT t).2 ht)) + have ib : ∀ t ∈ Z.T, L.mem s2 (ibWordA t) = L.mem s (ibWordA t) := + fun t ht => fld t (by rw [J1.tcbs]; exact ht) 3512#u64 (by simp) (by simp) (by simp) + have bufCover : BufCover st Z.T Bf s2 σ2 := BufCover.mapA J1.bufCover J1.bufNative J1.j.1.st jx.1.st + (fun t ht => (J1.j.1.thT t).2 ht) HS ib (fun t ht => by rw [sbK t ht]; exact .refl _) + have bufNative : BufNativeA Z.T (L.mem s2) := BufNativeA.mapA J1.bufNative (fun t ht q sz' rr dv m' e' a' v' h => by + rw [sbK t ht] at h + have hb := J1.bufNative t ht + rw [bufSlot_eqA, capAtA_eq (st := st), h] at hb + exact hb) + have vk : ∀ Pt Ub : Set U64, VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt → + VsKeep st (L.ksb s) Pt s s2 := fun Pt Ub vr => + VsKeep.of_w vr.al w (fun b hb => J1.vsr_offCapWA vr hb) + have unmPt : UnmPtSpecA st { Z with G := (insGA Z.G (cslotAddrA ss) (cslotAddrA sd) + (insBA σ ss (absCapA nc) (absCapA (capA st s (cslotAddrA ss))))) } m OS s2 σ2 := by + obtain ⟨Pt, Ub, vr, -, -, -, -, -, -, link, -, -⟩ := J1.j7 + exact UnmPtSpecA.derivA F HS w.1 w.2.1 (vk Pt Ub vr) link + (fun p h => ⟨fun i hi hf => h.mem i hi (FootA.monoA + (Z := { Z with G := (insGA Z.G (cslotAddrA ss) (cslotAddrA sd) + (insBA σ ss (absCapA nc) (absCapA (capA st s (cslotAddrA ss))))), S1 := ∅ }) rfl rfl + (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) + (fun _ h => h) (FootA.of_ksA w.1.symm w.2.1.symm hf)), h.obj⟩) J1.unmPt + have bufOff : BufOffA st { Z with G := (insGA Z.G (cslotAddrA ss) (cslotAddrA sd) + (insBA σ ss (absCapA nc) (absCapA (capA st s (cslotAddrA ss))))) } OS s2 Bf := + ⟨J1.bufOff.os, fun b hb => by rw [w.1]; exact J1.bufOff.vs b hb, + fun b hb q hq => J1.bufOff.page b hb q (SchedPageA.derivBackA F rfl rfl rfl rfl rfl hq), J1.bufOff.vcw, + fun bp hbp => (J1.bufOff.pin bp hbp).keepC rfl rfl fun t ht _ => sbK t ht⟩ + have sinv : SInvXA σ2 m n := J1.sinv.capInsert hokAnc hm + refine ⟨s2, σ2, hs2, hm, Sched.BodyKeepA.ofSlot HS hms, + fun p y k h => HS.keep h (fun r cs e => by cases e) (fun x e => by cases e), ?_⟩ + have hfh : ∀ t ∈ Z.T, capA st s2 (slotIdx t layout.tcb.SLOT_FAULT_HANDLER) = + capA st s (slotIdx t layout.tcb.SLOT_FAULT_HANDLER) := fun t ht => + ek _ (J1.j5.2.1 _ (isSlot_fhA ((J1.j.1.thT t).2 ht))) + (cnSlot_ne_fhA J1.j.1.place hCs.1 hCs.2 ((J1.j.1.thT t).2 ht)) + (cnSlot_ne_fhA J1.j.1.place hCd.1 hCd.2 ((J1.j.1.thT t).2 ht)) + have hvt : IdleSlotNewA σ σ2 m n := IdleSlotNewA.of_cnodeA J1.sidle fun sl c h => by + rcases capE sl c h with h | ⟨e, -⟩ | ⟨e, -⟩ + · exact Or.inl h + · obtain ⟨cs, hk1, -⟩ := J1.j.1.st.cnode pd _ hCd.2 + exact Or.inr ⟨_, cs, by rw [e, hCd.1]; exact hk1⟩ + · obtain ⟨cs, hk1, -⟩ := J1.j.1.st.cnode ps _ hCs.2 + exact Or.inr ⟨_, cs, by rw [e, hCs.1]; exact hk1⟩ + exact J1.of_capDerivA (hsub := J1.j5.2.1) (hnul := J1.j5.2.2) (hfh := hfh) (hvt := hvt) + (symL := symL s2 hs2) w F HS W hms hoff hfal hcdtS horig origLive nullOrig hsc (sysl s2 hs2) jx sinv bufCover + bufNative unmPt bufOff j5' (j7 s2 σ2 hs2 hm) + + +set_option maxHeartbeats 4000000 in +/-- **`JStage3A` across a `cap_insert` of a cap derived from the source's, without the Restart** (module docstring): the +body of `cnodeCopyA` after its Restart, `capInsertGenA` with the derivation facts from the shapes. -/ +theorem JStage3A.capInsertA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {ar : machine.Arch A} {nA : U64} {s : S} {a : A} + {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (ss sd : Fl4SpecA64.CslotPtr) {ps pd : U64} + (hCs : ss.1 = specPtr ps ∧ Z.th ps = some (.cnode ss.2.length)) + (hCd : sd.1 = specPtr pd ∧ Z.th pd = some (.cnode sd.2.length)) + (hsn : capA st s (cslotAddrA ss) ≠ .Null) (hdn : capA st s (cslotAddrA sd) = .Null) + (hdc : obj.sext48 (cslotAddrA sd) = ok (cslotAddrA sd)) + {nc : capability.Cap} {anc : Fl4SpecA64.Cap} (hwf : CapWfA nc) (hanc : CapEqA nc anc) + (hw : CapRt.EncWf nc) (hn : nc ≠ .Null) + (hD : Fl4ProofsA64.P3.DerivA nc (capA st s (cslotAddrA ss))) (hal : UA nc (capA st s (cslotAddrA ss))) + (hk : SameBadgeKindA nc (capA st s (cslotAddrA ss))) + (hsh : Fl4ProofsA64.P3.shapeA nc = Fl4ProofsA64.P3.shapeA (capA st s (cslotAddrA ss))) + (hsh1 : Fl4ProofsA64.P1.W7.R.ST.shapeA nc = Fl4ProofsA64.P1.W7.R.ST.shapeA (capA st s (cslotAddrA ss))) + (hbd1 : badgeA nc = badgeA (capA st s (cslotAddrA ss)) ∨ badgeA (capA st s (cslotAddrA ss)) = 0#u64) + (hbd : ∀ r b rs, (absCapA (capA st s (cslotAddrA ss)) = .endpointCap r b rs ∨ + absCapA (capA st s (cslotAddrA ss)) = .notificationCap r b rs) → b ≠ 0 → + Fl4SpecA64.capEpBadge (absCapA nc) = b) + (hirqB : ∀ i, capA st s (cslotAddrA ss) = .IrqHandler i → nc = .IrqHandler i) + (hirqN : ∀ i, nc = .IrqHandler i → capA st s (cslotAddrA ss) = .IrqHandler i ∨ + ∀ y ∈ Z.G.chains.flatten, capA st s y ≠ .IrqHandler i) + (hirqA : ∀ i, nc = .IrqHandler i → capA st s (cslotAddrA ss) = .IrqHandler i ∨ + (i.val < 5376 ∧ L.mem s (irqStAddrA (L.ksb s) i) ≠ 0#u64)) + (hz : ∀ p t k, capA st s (cslotAddrA ss) ≠ .Zombie p t k) + (hut : isUtA (capA st s (cslotAddrA ss)) = true → ∀ y, ¬ Fl4Proofs.P3.Mdb.Desc Z.G.par y (cslotAddrA ss)) + (hdo : ¬ IrqNodeA (L.ksb s) (fl4Ptr sd.1) ∨ IrqCapA (absCapA nc)) + (hcv : CapValidA Z.th (capA st s (cslotAddrA ss)) → CapValidA Z.th nc) (hsci : NoIdleCapV m n anc) + (symL : ∀ s2, cdt.insert st s nc (cslotAddrA ss) (cslotAddrA sd) = ok s2 → SymL st H Z.Cs s2) + (sysl : ∀ s2, cdt.insert st s nc (cslotAddrA ss) (cslotAddrA sd) = ok s2 → SysL st H Z.Cs OS ia ie s2) + (j7 : ∀ s2 σ2, cdt.insert st s nc (cslotAddrA ss) (cslotAddrA sd) = ok s2 → + ((), σ2) ∈ (Fl4SpecA64.capInsert (Z := Unit) anc ss sd σ).1 → + J7A st (J7R2A st ar nA) { Z with G := (insGA Z.G (cslotAddrA ss) (cslotAddrA sd) + (insBA σ ss (absCapA nc) (absCapA (capA st s (cslotAddrA ss))))) } H OS s2 σ2 m) : + ∃ s2 σ2, cdt.insert st s nc (cslotAddrA ss) (cslotAddrA sd) = ok s2 ∧ + ((), σ2) ∈ (Fl4SpecA64.capInsert (Z := Unit) anc ss sd σ).1 ∧ Sched.BodyKeepA σ σ2 ∧ + (∀ p y k, σ.kheap p = some (.schedContext y k) → σ2.kheap p = some (.schedContext y k)) ∧ + JStage3A st AR { Z with G := (insGA Z.G (cslotAddrA ss) (cslotAddrA sd) + (insBA σ ss (absCapA nc) (absCapA (capA st s (cslotAddrA ss))))) } + H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s2 a σ2 m := by + have hAs : AbsSlotA Z.th ss := ⟨ps, hCs.1, Or.inl hCs.2⟩ + have hAd : AbsSlotA Z.th sd := ⟨pd, hCd.1, Or.inl hCd.2⟩ + have J1 := J + have hO : OSOK H.tcbs H.omega H.frame OS := J1.sys.ent.os + have hP := J1.j.1.place + have hsS := hAs.isSlot hP + have hdS := hAd.isSlot hP + have xs : cslotAddrA ss ∈ OS := J1.j5.2.1 _ hsS + have xd : cslotAddrA sd ∈ OS := J1.j5.2.1 _ hdS + have cd0 : capA st s (cslotAddrA sd) = .Null := hdn + have E1 := J1.toJEntA + have hTr : TreeA st OS Z.G.chains Z.G.par s := + J1.j.1.cdt.tree.grow J1.j5.2.1 (fun x hx hn => J1.j5.2.2 x hx hn) + have hoff := footN_offA E1 + have hfal := FootNA.aligned J1.j.1 (bxi_nodes E1.sys.ent.bxi) E1.scAl E1.ap.vcAl + obtain ⟨_, -, hcr⟩ := J1.j.1.st.getAbsA hAs + have hov : NoOvA (capA st s (cslotAddrA ss)) := noOvA_of_wf hcr.1 + have hsr := srA_shape hsh hov hcr.1 hsn hz + have hd := derivA_of _ _ hsr hbd + have hno : CapObjOkA Z nc := (J1.j.capObj hsS).of_shapeA hsh hcv + have hoffN : NodeOffA (L.ksb s) nc := (NodeOffA.of_ctOff (E1.sys.sysl.ct _ xs)).of_shapeA hsh + have hzb : isZbA (capA st s (cslotAddrA ss)) = false := by + generalize capA st s (cslotAddrA ss) = c at hz + cases c <;> first | rfl | exact absurd rfl (hz _ _ _) + have hss : sraA (capA st s (cslotAddrA ss)) nc = true := by + have h1 := J1.j5.1.1.self _ ((hTr.lk.members _).2 ⟨xs, hsn⟩) hzb + unfold srMA at h1 + rw [sraA_congr rfl hsh1]; exact h1 + have hbeq : insBA σ ss (absCapA nc) (absCapA (capA st s (cslotAddrA ss))) = + isPFA (Fl4ProofsA64.P1.W7.R.ST.revA st s (cslotAddrA ss)) (capA st s (cslotAddrA ss)) nc + (revFA nc (capA st s (cslotAddrA ss))) := by + unfold insBA + rw [J1.j.1.cdt.orig ss hAs] + exact sbp_eqA _ _ _ hsh hsr hss hk hwf + exact JStage3A.capInsertGenA (J := J) ss sd hCs hCd hsn hdn hdc hwf hanc hw hn hD hal hk hut hdo hsci hno hoffN + (hub_ofA E1 hAs xs hd) (hsib_ofA J1.j.1.cdt hAs hd) hbeq + (fun hst σ' => J5A.insGenA (σ' := σ') (m := m) (m' := m) hO J1.j5 hTr xs xd hsS hdS hsn cd0 hdc hw hsh1 hbd1 hirqB + hirqN hirqA hut hzb hst) symL sysl j7 + +set_option maxHeartbeats 4000000 in +/-- **`JStage3A` across the CNode Copy / Mint arm** (module docstring). -/ +theorem JStage3A.cnodeCopyA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {ar : machine.Arch A} {nA : U64} {s : S} {a : A} + {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) (hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM) + (ss sd : Fl4SpecA64.CslotPtr) {ps pd : U64} + (hCs : ss.1 = specPtr ps ∧ Z.th ps = some (.cnode ss.2.length)) + (hCd : sd.1 = specPtr pd ∧ Z.th pd = some (.cnode sd.2.length)) + (hsn : capA st s (cslotAddrA ss) ≠ .Null) (hdn : capA st s (cslotAddrA sd) = .Null) + (hdc : obj.sext48 (cslotAddrA sd) = ok (cslotAddrA sd)) + {nc : capability.Cap} {anc : Fl4SpecA64.Cap} (hwf : CapWfA nc) (hanc : CapEqA nc anc) + (hw : CapRt.EncWf nc) (hn : nc ≠ .Null) + (hD : Fl4ProofsA64.P3.DerivA nc (capA st s (cslotAddrA ss))) (hal : UA nc (capA st s (cslotAddrA ss))) + (hk : SameBadgeKindA nc (capA st s (cslotAddrA ss))) + (hsh : Fl4ProofsA64.P3.shapeA nc = Fl4ProofsA64.P3.shapeA (capA st s (cslotAddrA ss))) + (hsh1 : Fl4ProofsA64.P1.W7.R.ST.shapeA nc = Fl4ProofsA64.P1.W7.R.ST.shapeA (capA st s (cslotAddrA ss))) + (hbd1 : badgeA nc = badgeA (capA st s (cslotAddrA ss)) ∨ badgeA (capA st s (cslotAddrA ss)) = 0#u64) + (hbd : ∀ r b rs, (absCapA (capA st s (cslotAddrA ss)) = .endpointCap r b rs ∨ + absCapA (capA st s (cslotAddrA ss)) = .notificationCap r b rs) → b ≠ 0 → + Fl4SpecA64.capEpBadge (absCapA nc) = b) + (hirq : ∀ i, nc = .IrqHandler i ↔ capA st s (cslotAddrA ss) = .IrqHandler i) + (hz : ∀ p t k, capA st s (cslotAddrA ss) ≠ .Zombie p t k) + (hut : isUtA (capA st s (cslotAddrA ss)) = true → ∀ y, ¬ Fl4Proofs.P3.Mdb.Desc Z.G.par y (cslotAddrA ss)) + (hdo : ¬ IrqNodeA (L.ksb s) (fl4Ptr sd.1) ∨ IrqCapA (absCapA nc)) + (hcv : CapValidA Z.th (capA st s (cslotAddrA ss)) → CapValidA Z.th nc) (hsci : NoIdleCapV m n anc) + (symL : ∀ s2, cdt.insert st (L.wr s (stA (L.curW s)) sched.TS_RESTART) nc (cslotAddrA ss) (cslotAddrA sd) = + ok s2 → SymL st H Z.Cs s2) + (sysl : ∀ s2, cdt.insert st (L.wr s (stA (L.curW s)) sched.TS_RESTART) nc (cslotAddrA ss) (cslotAddrA sd) = + ok s2 → SysL st H Z.Cs OS ia ie s2) + (j7 : ∀ x s2 σ2, σ.kheap σ.curThread = some (.tcb x) → + cdt.insert st (L.wr s (stA (L.curW s)) sched.TS_RESTART) nc (cslotAddrA ss) (cslotAddrA sd) = ok s2 → + ((), σ2) ∈ (Fl4SpecA64.capInsert (Z := Unit) anc ss sd (stUpdA σ σ.curThread x .restart)).1 → + J7A st (J7R2A st ar nA) { Z with G := (insGA Z.G (cslotAddrA ss) (cslotAddrA sd) + (insBA (stUpdA σ σ.curThread x .restart) ss (absCapA nc) (absCapA (capA st s (cslotAddrA ss))))) } + H OS s2 σ2 m) : + ∃ x s2 σ2, σ.kheap σ.curThread = some (.tcb x) ∧ + sched.set_thread_state st s (L.curW s) sched.TS_RESTART = ok (L.wr s (stA (L.curW s)) sched.TS_RESTART) ∧ + cdt.insert st (L.wr s (stA (L.curW s)) sched.TS_RESTART) nc (cslotAddrA ss) (cslotAddrA sd) = ok s2 ∧ + ((), σ2) ∈ (Fl4SpecA64.capInsert (Z := Unit) anc ss sd (stUpdA σ σ.curThread x .restart)).1 ∧ + JStage3A st AR { Z with G := (insGA Z.G (cslotAddrA ss) (cslotAddrA sd) + (insBA (stUpdA σ σ.curThread x .restart) ss (absCapA nc) (absCapA (capA st s (cslotAddrA ss))))) } + H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s2 a σ2 m ∧ + Sched.BodyKeepA (stUpdA σ σ.curThread x .restart) σ2 := by + have hAs : AbsSlotA Z.th ss := ⟨ps, hCs.1, Or.inl hCs.2⟩ + have hAd : AbsSlotA Z.th sd := ⟨pd, hCd.1, Or.inl hCd.2⟩ + obtain ⟨x, hk0, heq, -, J1, -, hOS1, -⟩ := J.of_restartCurA RS.pl.choose_spec.choose_spec.choose_spec.choose_spec + RS.sym RS.run hnvm RS.cur RS.vs RS.vc + have hO : OSOK H.tcbs H.omega H.frame OS := J1.sys.ent.os + have hP := J1.j.1.place + have hsS := hAs.isSlot hP + have hdS := hAd.isSlot hP + have xs : cslotAddrA ss ∈ OS := RS.sub _ hsS + have xd : cslotAddrA sd ∈ OS := RS.sub _ hdS + have cs0 : capA st (L.wr s (stA (L.curW s)) sched.TS_RESTART) (cslotAddrA ss) = capA st s (cslotAddrA ss) := + hOS1.capA xs + have cd0 : capA st (L.wr s (stA (L.curW s)) sched.TS_RESTART) (cslotAddrA sd) = .Null := (hOS1.capA xd).trans hdn + rw [← cs0] at hsn hD hal hk hsh hsh1 hbd1 hbd hirq hz hut hcv j7 ⊢ + generalize hs0 : L.wr s (stA (L.curW s)) sched.TS_RESTART = s0 at heq J1 hOS1 sysl symL j7 cs0 cd0 hsn hD hal hk hsh hsh1 hbd1 hbd hirq hz hut hcv ⊢ + generalize hσ0 : stUpdA σ σ.curThread x .restart = σ0 at J1 j7 ⊢ + have hdo0 : ¬ IrqNodeA (L.ksb s0) (fl4Ptr sd.1) ∨ IrqCapA (absCapA nc) := by + rw [← hs0, L.ksb_wr]; exact hdo + obtain ⟨s2, σ2, hs2, hm, hbk, -, J3⟩ := JStage3A.capInsertA (J := J1) ss sd hCs hCd hsn cd0 hdc hwf hanc hw hn hD hal hk hsh + hsh1 hbd1 hbd (fun i h => (hirq i).2 h) (fun i h => Or.inl ((hirq i).1 h)) (fun i h => Or.inl ((hirq i).1 h)) hz hut + hdo0 hcv hsci symL sysl (fun s2 σ2 h1 h2 => by + have h3 := j7 x s2 σ2 hk0 h1 (by rw [hσ0]; exact h2) + rw [hσ0] at h3; exact h3) + refine ⟨x, s2, σ2, hk0, heq, hs2, by rw [hσ0]; exact hm, ?_⟩ + rw [hσ0]; exact ⟨J3, hbk⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3ArmCnMoveA.lean b/proofs/Fl4ProofsA64/P3/Stage3ArmCnMoveA.lean new file mode 100644 index 000000000..761e59adb --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ArmCnMoveA.lean @@ -0,0 +1,516 @@ +/- +P3 (AArch64, merge line, D8 step 5 (iii)): **the CNode Move / Mutate arm** — the caller's Restart, then +`cdt::move_cap`, against l4v `cap_move`, keeps `JStage3A`. + +* `UnmPtSpecA.derivA`, `SchedPageA.derivBackA`: the table-reading fields across derived caps. +* `capMove_outA`: what every outcome of `cap_move` does to the CDT, the original flags, the heap and the machine. +* `JStage3A.cnodeMoveA`: the arm. Its case hypotheses are the decode's facts about the new cap and the two slots; + its final-state hypotheses are `SysL` and J7 (Option A/X). Both slots are CNode slots (`hCs`, `hCd`: what the + `cspace` lookups return), so neither is a TCB's IPC-buffer slot (`P3/CnSlotSbA`). +* `updCapData_trueA` / `updCapData_true_okA`: Mutate's new cap (`update_cap_data true`) is `Null` or derived with the + same shape, badge and IRQ, discharging `hD` / `hal` / `hsh` / `hbd` / `hirq` for Mutate (Move: `nc` is the cap). + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3BodyKeepA +import Fl4ProofsA64.P3.Stage3CnMoveA +import Fl4ProofsA64.P3.Stage3ArmFrUnmA +import Fl4ProofsA64.P3.InsDisA +import Fl4ProofsA64.P3.CnSlotSbA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-- A table page after derived-cap writes is one before: derivation keeps every table cap. -/ +theorem SchedPageA.derivBackA {Z Z' : XSetsA} {OS : Set U64} {s s' : S} (F : CapsFromA st OS s s') + (e1 : Z'.Pp = Z.Pp) (e2 : Z'.S1 = Z.S1) (e3 : Z'.T2 = Z.T2) (e4 : Z'.X = Z.X) (e5 : Z'.Y = Z.Y) + {q : U64} (h : SchedPageA st Z' OS s' q) : SchedPageA st Z OS s q := by + unfold SchedPageA at h ⊢ + rw [e1, e2, e3, e4, e5] at h + rcases h with h | h | h | h | h | h | h | ⟨u, hu, h⟩ + · exact Or.inl h + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr (Or.inl h)) + · exact Or.inr (Or.inr (Or.inr (Or.inl h))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inl h)))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl h))))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl h)))))) + · refine Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ?_)))))) + have fx : ∀ c : capability.Cap, FixedCapA c → c ≠ .Null → capA st s' u = c → ∃ v ∈ OS, capA st s v = c := + fun c hc hn e => F.fixed hu hc hn e + rcases h with ⟨m, a', v', h⟩ | ⟨m, a', h⟩ | ⟨a', v', h⟩ | ⟨q', a', v', h, e'⟩ | ⟨y, a', h, e'⟩ + · obtain ⟨v, hv, e⟩ := fx _ (by trivial) (by simp) h + exact ⟨v, hv, Or.inl ⟨_, _, _, e⟩⟩ + · obtain ⟨v, hv, e⟩ := fx _ (by trivial) (by simp) h + exact ⟨v, hv, Or.inr (Or.inl ⟨_, _, e⟩)⟩ + · obtain ⟨v, hv, e⟩ := fx _ (by trivial) (by simp) h + exact ⟨v, hv, Or.inr (Or.inr (Or.inl ⟨_, _, e⟩))⟩ + · obtain ⟨v, hv, e⟩ := fx _ (by trivial) (by simp) h + exact ⟨v, hv, Or.inr (Or.inr (Or.inr (Or.inl ⟨q', _, _, e, e'⟩)))⟩ + · obtain ⟨v, hv, e⟩ := fx _ (by trivial) (by simp) h + exact ⟨v, hv, Or.inr (Or.inr (Or.inr (Or.inr ⟨y, _, e, e'⟩)))⟩ + +/-- **J9 across derived-cap writes** that keep the table words and the arch objects. -/ +theorem UnmPtSpecA.derivA {Z Z' : XSetsA} {OS : Set U64} {s s' : S} {σ σ' : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} (F : CapsFromA st OS s s') (HS : SpecSlotA σ σ') (hks : L.ksb s' = L.ksb s) + (hcpu : L.cpu s' = L.cpu s) {Pt : Set U64} (mem : VsKeep st (L.ksb s) Pt s s') + (link : ∀ q, SchedPageA st Z OS s q → q ∈ Pt) + (off : ∀ p, TabOffA (L := L) Z m s p → TabOffA (L := L) Z' m s' p) + (h : UnmPtSpecA st Z m OS s σ) : UnmPtSpecA st Z' m OS s' σ' := by + have word : ∀ q ∈ Pt, ∀ i : U64, i.val < 512 → L.mem s' (peAF q i) = L.mem s (peAF q i) := fun q hq i hi => + mem _ (Or.inl ⟨q, hq, core.num.U64.wrapping_mul i 8#u64, by rw [wm8_valA hi]; omega, + by rw [wm8_valA hi]; omega, rfl⟩) + have kk : ∀ q ao, σ.kheap q = some (.archObj ao) → σ'.kheap q = some (.archObj ao) := fun q ao h => + HS.keep h (fun _ _ e => by cases e) (fun _ e => by cases e) + have tab : ∀ ty q, q ∈ Pt → UnmTabA st Z m s σ ty q → UnmTabA st Z' m s' σ' ty q := fun ty q hq h => by + obtain ⟨t, k1, k2, k3⟩ := h.spec + exact ⟨fun i hi => by rw [word q hq i hi]; exact h.inv i hi, ⟨t, kk _ _ k1, k2, k3⟩, off _ h.off⟩ + have bk : ∀ u ∈ OS, ∀ c : capability.Cap, FixedCapA c → c ≠ .Null → capA st s' u = c → + ∃ v ∈ OS, capA st s v = c := fun u hu c hc hn e => F.fixed hu hc hn e + refine ⟨fun u hu q asid va hc => ?_, fun u hu q asid hc => ?_, fun u hu q asid va hc => ?_, + fun u hu q asid hc => ?_⟩ + · obtain ⟨v, hv, o⟩ := bk u hu _ (by trivial) (by simp) hc + exact tab _ _ (link q (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨v, hv, Or.inl ⟨_, _, _, o⟩⟩)))))))) (h.pt v hv q asid va o) + · obtain ⟨v, hv, o⟩ := bk u hu _ (by trivial) (by simp) hc + exact tab _ _ (link q (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨v, hv, Or.inr (Or.inl ⟨_, _, o⟩)⟩)))))))) (h.vs v hv q asid o) + · obtain ⟨v, hv, o⟩ := bk u hu _ (by trivial) (by simp) hc + have l0 : q ∈ Pt := link q (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨v, hv, Or.inr (Or.inr (Or.inl ⟨_, _, o⟩))⟩))))))) + have l1 : wa q 4096#u64 ∈ Pt := link _ (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨v, hv, Or.inr (Or.inr (Or.inr (Or.inl ⟨q, _, _, o, rfl⟩)))⟩))))))) + have h0 := h.s2 v hv q asid va o + obtain ⟨es, k1, k2⟩ := h0.spec + refine ⟨fun i hi => ?_, es, kk _ _ k1, k2⟩ + by_cases hlo : i.val < 512 + · rw [word q l0 i hlo]; exact h0.inv i hi + · obtain ⟨j, hj, ej⟩ := peAF_hiA q i (by omega) hi + have := h0.inv i hi + rw [ej] at this ⊢ + rw [word _ l1 j hj]; exact this + · obtain ⟨v, hv, o⟩ := bk u hu _ (by trivial) (by simp) hc + have l1 : wa q 4096#u64 ∈ Pt := link _ (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨v, hv, Or.inr (Or.inr (Or.inr (Or.inr ⟨q, _, o, rfl⟩)))⟩))))))) + have h0 := h.hv v hv q asid o + obtain ⟨l, k1, k2⟩ := h0.spec + exact ⟨fun i hi => by rw [word _ l1 i hi]; exact h0.inv i hi, l, kk _ _ k1, k2⟩ + +/-- **Every outcome of `cap_move`** (the inversion of `capMove_memA`): two `set_cap`s, then the CDT and original-cap +updates; the extended state aside. -/ +theorem capMove_outA {σ σ' : State Unit} {a : Cap} {src dst : CslotPtr} + (h : ((), σ') ∈ (capMove (Z := Unit) a src dst σ).1) : + ∃ σ1 σ2, ((), σ1) ∈ (setCap (Z := Unit) a dst σ).1 ∧ ((), σ2) ∈ (setCap (Z := Unit) .nullCap src σ1).1 ∧ + σ'.cdt = (moveFinA σ2 src dst).cdt ∧ σ'.isOriginalCap = (moveFinA σ2 src dst).isOriginalCap ∧ + σ'.kheap = σ2.kheap ∧ σ'.machineState = σ2.machineState := by + unfold capMove at h + obtain ⟨⟨_, σ1⟩, h1, h⟩ := mem_bindA.1 h + obtain ⟨⟨_, σ2⟩, h2, h⟩ := mem_bindA.1 h + refine ⟨σ1, σ2, h1, h2, ?_⟩ + obtain ⟨x, hx, h⟩ := mem_bindA.1 h + obtain rfl : x = (σ2.cdt src, σ2) := hx + obtain ⟨x, hx, h⟩ := mem_bindA.1 h + obtain rfl : x = (σ2.cdt dst, σ2) := hx + obtain ⟨x, hx, h⟩ := mem_bindA.1 h + obtain rfl : x = (σ2.cdt, σ2) := hx + obtain ⟨x, hx, h⟩ := mem_bindA.1 h + obtain rfl : x = (σ2.cdt src, σ2) := hx + obtain ⟨x, hx, h⟩ := mem_bindA.1 h + obtain rfl : x = (Function.update (Function.update σ2.cdt dst (σ2.cdt src)) src none, σ2) := hx + obtain ⟨x, hx, h⟩ := mem_bindA.1 h + unfold setCdt at hx + obtain ⟨y, hy, hx⟩ := mem_bindA.1 hx + obtain rfl : y = (σ2, σ2) := hy + obtain rfl : x = _ := hx + obtain ⟨x, hx, h⟩ := mem_bindA.1 h + unfold doExtendedOp at hx + obtain ⟨y, hy, hx⟩ := mem_bindA.1 hx + obtain rfl : y = _ := hy + obtain ⟨z, hz, hx⟩ := mem_bindA.1 hx + have ez := hz.2 + obtain rfl : x = _ := hx + rw [ez] at h + obtain ⟨x, hx, h⟩ := mem_bindA.1 h + obtain rfl : x = _ := hx + unfold setOriginal at h + obtain ⟨x, hx, h⟩ := mem_bindA.1 h + obtain ⟨y, hy, hx⟩ := mem_bindA.1 hx + obtain rfl : y = _ := hy + obtain rfl : x = _ := hx + obtain ⟨y, hy, h⟩ := mem_bindA.1 h + obtain rfl : y = _ := hy + have e := (Prod.mk.inj h).2 + subst e + exact ⟨rfl, rfl, rfl, rfl⟩ + +/-- A cap after a slot-only step whose objects hold old caps or `Q` ones is an old one of that slot, or a `Q` one. -/ +theorem specCapAtA_orA {σ σ2 : Fl4SpecA64.State Unit} {Q : Fl4SpecA64.CslotPtr → Fl4SpecA64.Cap → Prop} + (HS : SpecSlotA σ σ2) + (H : ∀ p o', σ2.kheap p = some o' → ∃ o, σ.kheap p = some o ∧ + (∀ b c, Fl4SpecA64.capOf o' b = some c → Q (p, b) c ∨ Fl4SpecA64.capOf o b = some c) ∧ + ∀ n, (∃ cs, o' = .cnode n cs ∧ wellFormedCnodeN n cs) ↔ (∃ cs, o = .cnode n cs ∧ wellFormedCnodeN n cs)) : + ∀ sl a, specCapAtA σ2 sl = some a → specCapAtA σ sl = some a ∨ Q sl a := by + intro sl a hs + cases k1 : σ2.kheap sl.1 with + | none => unfold specCapAtA at hs; rw [k1] at hs; cases hs + | some o' => + obtain ⟨o, k0, hc, hw⟩ := H _ _ k1 + have key : Fl4SpecA64.capOf o' sl.2 = some a → Q sl a ∨ Fl4SpecA64.capOf o sl.2 = some a := hc _ _ + rcases HS.kh sl.1 with e' | ⟨r, cs, cs', e1, e2⟩ | ⟨y, y', e1, e2, -⟩ + · rw [k1, k0] at e'; cases e' + left; unfold specCapAtA at hs ⊢; rw [k1] at hs; rwa [k0] + · rw [k0] at e1; rw [k1] at e2; cases e1; cases e2 + unfold specCapAtA at hs ⊢ + rw [k1] at hs; rw [k0] + simp only at hs ⊢ + split at hs + case isFalse => cases hs + case isTrue hwf => + have w2 := (hw r).1 ⟨cs', rfl, hwf⟩ + obtain ⟨cs2, e3, w3⟩ := w2 + cases e3 + rw [if_pos w3] + rcases key hs with e | hh + · exact Or.inr e + · exact Or.inl hh + · rw [k0] at e1; rw [k1] at e2; cases e1; cases e2 + unfold specCapAtA at hs ⊢ + rw [k1] at hs; rw [k0] + rcases key hs with e | hh + · exact Or.inr e + · exact Or.inl hh + +/-- The objects after a `set_cap`: the old caps, or the written one at the written slot. -/ +theorem setCap_objsA {sc : Fl4SpecA64.Cap} {cte : Fl4SpecA64.CslotPtr} {σ σ2 : Fl4SpecA64.State Unit} + (h : ((), σ2) ∈ (setCap (Z := Unit) sc cte σ).1) : ∀ p o', σ2.kheap p = some o' → ∃ o, σ.kheap p = some o ∧ + (∀ b c, Fl4SpecA64.capOf o' b = some c → ((p, b) = cte ∧ c = sc) ∨ Fl4SpecA64.capOf o b = some c) ∧ + ∀ n, (∃ cs, o' = .cnode n cs ∧ wellFormedCnodeN n cs) ↔ (∃ cs, o = .cnode n cs ∧ wellFormedCnodeN n cs) := by + intro p o2 h2 + obtain ⟨o, o', hk, hσ, hc, hw⟩ := setCap_memA (p := cte.1) (cref := cte.2) h + have k2 : σ2.kheap = Function.update σ.kheap cte.1 (some o') := by + have := congrArg (fun x : Fl4SpecA64.State Unit => x.kheap) hσ + simpa using this + by_cases e : p = cte.1 + · subst e + rw [k2, Function.update_self] at h2 + cases h2 + refine ⟨o, hk, fun b c hb => ?_, hw⟩ + rcases hc b c hb with ⟨e1, e2⟩ | h + · exact Or.inl ⟨by rw [e1], e2⟩ + · exact Or.inr h + · rw [k2, Function.update_of_ne e] at h2 + exact ⟨o2, h2, fun _ _ h => Or.inr h, fun _ => Iff.rfl⟩ + +/-- **Mutate's (and Rotate's) new cap**, `update_cap_data true` (cnode.rs `copied_cap`, `decode_rotate`): `Null` +(an endpoint or notification cap: `preserve` refuses a badge), or a cap derived from the old one with the same +shape, badge and IRQ — a CNode cap's guard is the only field it changes. So `cnodeMoveA`'s `hD` / `hsh` / `hbd` / +`hirq` hold for Mutate too. -/ +theorem updCapData_trueA (d : U64) (c : capability.Cap) : + objects.update_cap_data true d c ⦃ m => m = .Null ∨ (Fl4ProofsA64.P3.DerivA m c ∧ + Fl4ProofsA64.P1.W7.R.ST.shapeA m = Fl4ProofsA64.P1.W7.R.ST.shapeA c ∧ badgeA m = badgeA c ∧ + ∀ i, m = .IrqHandler i ↔ c = .IrqHandler i) ⦄ := by + cases c + case Endpoint p b sd rc g gr => + simp only [objects.update_cap_data] + split + · exact (WP.spec_ok _).2 (Or.inl rfl) + · rename_i h; exact absurd trivial h + case Notification p b sd rc => + simp only [objects.update_cap_data] + split + · exact (WP.spec_ok _).2 (Or.inl rfl) + · rename_i h; exact absurd trivial h + case CNode p rd gs g => + simp only [objects.update_cap_data] + refine WP.spec_bind (Pₘ := fun _ => True) (by first | p0_auto | p0_split) fun _ _ => ?_ + refine WP.spec_bind (Pₘ := fun _ => True) (by first | p0_auto | p0_split) fun _ _ => ?_ + split + · exact (WP.spec_ok _).2 (Or.inl rfl) + · refine WP.spec_bind (Pₘ := fun _ => True) (by first | p0_auto | p0_split) fun _ _ => ?_ + refine WP.spec_bind (Pₘ := fun _ => True) (by first | p0_auto | p0_split) fun _ _ => ?_ + refine WP.spec_bind (Pₘ := fun _ => True) (by first | p0_auto | p0_split) fun _ _ => ?_ + refine (WP.spec_ok _).2 (Or.inr ⟨Fl4ProofsA64.P3.DerivA.cnode _ _ _ _ _ _, rfl, rfl, fun i => ?_⟩) + constructor <;> intro h <;> cases h + all_goals (simp only [objects.update_cap_data] + exact (WP.spec_ok _).2 (Or.inr ⟨.refl _, rfl, rfl, fun _ => Iff.rfl⟩)) + +/-- `updCapData_trueA` at an `ok` outcome, with `UA` (A64 P1's `update_cap_data.ua`). -/ +theorem updCapData_true_okA {d : U64} {c nc : capability.Cap} (h : objects.update_cap_data true d c = ok nc) + (hn : nc ≠ .Null) : Fl4ProofsA64.P3.DerivA nc c ∧ UA nc c ∧ + Fl4ProofsA64.P1.W7.R.ST.shapeA nc = Fl4ProofsA64.P1.W7.R.ST.shapeA c ∧ badgeA nc = badgeA c ∧ + ∀ i, nc = .IrqHandler i ↔ c = .IrqHandler i := by + have h1 := updCapData_trueA d c + have h2 := objects.update_cap_data.ua true d c + rw [h] at h1 h2 + rcases (WP.spec_ok _).1 h1 with e | ⟨a, b, c', e⟩ + · exact absurd e hn + rcases (WP.spec_ok _).1 h2 with e' | ⟨u, -⟩ + · exact absurd e' hn + exact ⟨a, u, b, c', e⟩ + +set_option maxHeartbeats 4000000 in +/-- **`JStage3A` across the CNode Move / Mutate arm** (module docstring). -/ +theorem JStage3A.cnodeMoveA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {ar : machine.Arch A} {nA : U64} {s : S} {a : A} + {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) (hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM) + (ss sd : Fl4SpecA64.CslotPtr) {ps pd : U64} (hCs : ss.1 = specPtr ps ∧ Z.th ps = some (.cnode ss.2.length)) + (hCd : sd.1 = specPtr pd ∧ Z.th pd = some (.cnode sd.2.length)) + (hsn : capA st s (cslotAddrA ss) ≠ .Null) (hdn : capA st s (cslotAddrA sd) = .Null) + (hdc : obj.sext48 (cslotAddrA sd) = ok (cslotAddrA sd)) + {nc : capability.Cap} {ac : Fl4SpecA64.Cap} (hca : CapRelA nc ac) (hw : CapRt.EncWf nc) (hn : nc ≠ .Null) + (hD : DerivA nc (capA st s (cslotAddrA ss))) (hal : UA nc (capA st s (cslotAddrA ss))) + (hsh : Fl4ProofsA64.P1.W7.R.ST.shapeA nc = Fl4ProofsA64.P1.W7.R.ST.shapeA (capA st s (cslotAddrA ss))) + (hbd : badgeA nc = badgeA (capA st s (cslotAddrA ss))) + (hirq : ∀ i, nc = .IrqHandler i ↔ capA st s (cslotAddrA ss) = .IrqHandler i) + (hno : CapObjOkA Z nc) (hok : CapOkA σ.interruptIrqNode sd ac) (hsci : NoIdleCapV m n ac) + (symL : ∀ s2, cdt.move_cap st (L.wr s (stA (L.curW s)) sched.TS_RESTART) nc (cslotAddrA ss) + (cslotAddrA sd) = ok s2 → SymL st H Z.Cs s2) + (sysl : ∀ s2, cdt.move_cap st (L.wr s (stA (L.curW s)) sched.TS_RESTART) nc (cslotAddrA ss) + (cslotAddrA sd) = ok s2 → SysL st H Z.Cs OS ia ie s2) + (j7 : ∀ x s2 σ2, σ.kheap σ.curThread = some (.tcb x) → + cdt.move_cap st (L.wr s (stA (L.curW s)) sched.TS_RESTART) nc (cslotAddrA ss) (cslotAddrA sd) = ok s2 → + ((), σ2) ∈ (capMove (Z := Unit) ac ss sd (stUpdA σ σ.curThread x .restart)).1 → + J7A st (J7R2A st ar nA) { Z with G := mvGA Z.G (cslotAddrA ss) (cslotAddrA sd) } H OS s2 σ2 m) : + ∃ x s2 σ2, σ.kheap σ.curThread = some (.tcb x) ∧ + sched.set_thread_state st s (L.curW s) sched.TS_RESTART = ok (L.wr s (stA (L.curW s)) sched.TS_RESTART) ∧ + cdt.move_cap st (L.wr s (stA (L.curW s)) sched.TS_RESTART) nc (cslotAddrA ss) (cslotAddrA sd) = ok s2 ∧ + ((), σ2) ∈ (capMove (Z := Unit) ac ss sd (stUpdA σ σ.curThread x .restart)).1 ∧ + JStage3A st AR { Z with G := mvGA Z.G (cslotAddrA ss) (cslotAddrA sd) } H OS U Ac Pp Tb Lv ia ie N E Rp Bf + stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s2 a σ2 m ∧ + Sched.BodyKeepA (stUpdA σ σ.curThread x .restart) σ2 := by + have hAs : AbsSlotA Z.th ss := ⟨ps, hCs.1, Or.inl hCs.2⟩ + have hAd : AbsSlotA Z.th sd := ⟨pd, hCd.1, Or.inl hCd.2⟩ + obtain ⟨x, hk, heq, -, J1, -, hOS1, -⟩ := J.of_restartCurA RS.pl.choose_spec.choose_spec.choose_spec.choose_spec + RS.sym RS.run hnvm RS.cur RS.vs RS.vc + generalize hs1 : L.wr s (stA (L.curW s)) sched.TS_RESTART = s1 at heq J1 hOS1 sysl symL j7 ⊢ + generalize hσ1 : stUpdA σ σ.curThread x .restart = σ1 at J1 j7 ⊢ + have hirqN : σ1.interruptIrqNode = σ.interruptIrqNode := by rw [← hσ1]; rfl + have hO : OSOK H.tcbs H.omega H.frame OS := J1.sys.ent.os + have hP := J1.j.1.place + have hsS := hAs.isSlot hP + have hdS := hAd.isSlot hP + have xs : cslotAddrA ss ∈ OS := RS.sub _ hsS + have xd : cslotAddrA sd ∈ OS := RS.sub _ hdS + have cs1 : capA st s1 (cslotAddrA ss) = capA st s (cslotAddrA ss) := hOS1.capA xs + have cd1 : capA st s1 (cslotAddrA sd) = .Null := (hOS1.capA xd).trans hdn + rw [← cs1] at hsn hD hal hsh hbd hirq + have hsd : cslotAddrA ss ≠ cslotAddrA sd := fun e => hsn (e ▸ cd1) + have E1 := J1.toJEntA + have hTr : TreeA st OS Z.G.chains Z.G.par s1 := + J1.j.1.cdt.tree.grow J1.j5.2.1 (fun x hx hn => J1.j5.2.2 x hx hn) + have hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s1 y = .Null := J1.j5.2.2 + have hoff := footN_offA E1 + have hfal := FootNA.aligned J1.j.1 (bxi_nodes E1.sys.ent.bxi) E1.scAl E1.ap.vcAl + have F0 := hTr.lk.lkF hO.nz + have inOS : ∀ y ∈ Z.G.chains.flatten, y ∈ OS := fun y hy => ((hTr.lk.members y).1 hy).1 + have hsG : cslotAddrA ss ∈ Z.G.chains.flatten := (hTr.lk.members _).2 ⟨xs, hsn⟩ + have mv := WP.spec_and (WP.spec_and + (cap_move_refinesA s1 σ1 Z.G J1.j.1.st J1.j.1.cdt hP hO hTr J1.j5.2.1 hnul hAs hAd hsn cd1 hdc hca hw hn hal) + (moveCap_osA hO xs xd (fun h => inOS _ (F0.pvm _ hsG h)) (fun h => inOS _ (F0.nxm _ hsG h)))) + (J5A.moveA (σ' := σ1) (m := m) (m' := m) hO J1.j5 hTr xs xd hsd hsS hdS hsn cd1 hdc hw hn hal hsh hbd hirq + (fun s' w i hi => w.mem_eq (hfal _ (.irq i hi)) (hoff _ (.irq i hi)))) + obtain ⟨s2, hs2, ⟨⟨σ2, hm, hst, hcdt, -, HS, hcs⟩, -⟩, w, ed, es, ek, -⟩ := spec_existsA mv + have jx : JAtZAX (L := L) AR { Z with G := mvGA Z.G (cslotAddrA ss) (cslotAddrA sd) } s2 a σ2 m := by + refine J1.j.of_slotWrites w (fun b hb' hb => hoff b hb hb') hfal _ HS hst hcdt (fun y hy => ?_) + rcases hcs y hy with e | e | e <;> rw [e] + · exact CapObjOkA.null Z + · exact J1.j.capObj hy + · exact hno + have j5' : J5A st { Z with G := mvGA Z.G (cslotAddrA ss) (cslotAddrA sd) } H OS s2 σ2 m := by + obtain ⟨s2', hs2', -, -, -, -, J5'⟩ := spec_existsA + (J5A.moveA (σ' := σ2) (m := m) (m' := m) hO J1.j5 hTr xs xd hsd hsS hdS hsn cd1 hdc hw hn hal hsh hbd hirq + (fun s' w i hi => w.mem_eq (hfal _ (.irq i hi)) (hoff _ (.irq i hi)))) + rw [hs2] at hs2'; have e := okinj hs2'; subst e; exact J5' + have F : CapsFromA st OS s1 s2 := CapsFromA.of_moveA xs xd hsd hD cd1 ed es ek + -- the spec side + obtain ⟨σa, σb, h1, h2, ecdt, eorig, ekh, ems⟩ := capMove_outA hm + obtain ⟨c1, o1, m1⟩ := setCap_keepsA h1 + obtain ⟨c2, o2, m2⟩ := setCap_keepsA h2 + have W1 := setCap_wfA _ _ _ _ h1 + have W2 := setCap_wfA _ _ _ _ h2 + have Wb : WfKeepA σ1 σb := W1.trans W2 + have W : WfKeepA σ1 σ2 := Wb.trans (WfKeepA.of_kheap ekh) + have hms : σ2.machineState = σ1.machineState := by rw [ems, m2, m1] + have kd : σ1.kheap sd.1 ≠ none := by + obtain ⟨o, -, hk0, -⟩ := setCap_memA (p := sd.1) (cref := sd.2) h1 + rw [hk0]; simp + have ks : σ1.kheap ss.1 ≠ none := by + obtain ⟨c, -, hr⟩ := J1.j.1.st.getAbsA hAs + obtain ⟨p, e1, hc | ⟨ht, i, hi, e2⟩⟩ := hAs + · obtain ⟨cs, hk0, -⟩ := J1.j.1.st.cnode p _ hc + rw [e1, hk0]; simp + · obtain ⟨y, hk0, -⟩ := J1.j.1.st.tcb p ht + rw [e1, hk0]; simp + have hne : ss ≠ sd := fun e => hsd (by rw [e]) + have cdtE : ∀ x y, σ2.cdt x = some y → (x = sd ∨ σ1.cdt x ≠ none) ∧ (y = sd ∨ ∃ x0, σ1.cdt x0 = some y) := by + intro p q h + rw [ecdt] at h + unfold moveFinA at h + simp only [c2, c1] at h + by_cases ep : p = ss + · subst ep + rw [Function.update_self] at h + simp at h + rw [Function.update_of_ne ep] at h + by_cases ed' : p = sd + · subst ed' + rw [Function.update_self] at h + split at h + · cases h; exact ⟨Or.inl rfl, Or.inl rfl⟩ + · exact ⟨Or.inl rfl, Or.inr ⟨ss, h⟩⟩ + rw [Function.update_of_ne ed'] at h + split at h + · rename_i h' + cases h; exact ⟨Or.inr (by rw [h']; simp), Or.inl rfl⟩ + · exact ⟨Or.inr (by rw [h]; simp), Or.inr ⟨p, h⟩⟩ + have hcdtS : ∀ x y, σ2.cdt x = some y → ∀ sl, sl = x ∨ sl = y → + σ1.kheap sl.1 ≠ none ∨ ∃ x0 y0, σ1.cdt x0 = some y0 ∧ (sl = x0 ∨ sl = y0) := by + intro p q h sl hsl + obtain ⟨hp, hq⟩ := cdtE p q h + rcases hsl with rfl | rfl + · rcases hp with e | e + · exact Or.inl (e ▸ kd) + · obtain ⟨y0, h0⟩ := Option.ne_none_iff_exists'.1 e + exact Or.inr ⟨_, y0, h0, Or.inl rfl⟩ + · rcases hq with e | ⟨x0, h0⟩ + · exact Or.inl (e ▸ kd) + · exact Or.inr ⟨x0, _, h0, Or.inr rfl⟩ + have orig2 : σ2.isOriginalCap = + Function.update (Function.update σ1.isOriginalCap sd (σ1.isOriginalCap ss)) ss false := by + rw [eorig]; unfold moveFinA; simp only [o2, o1] + have origE : ∀ p, σ2.isOriginalCap p = true → p ≠ ss ∧ (p = sd ∧ σ1.isOriginalCap ss = true ∨ + p ≠ sd ∧ σ1.isOriginalCap p = true) := by + intro p h + rw [orig2] at h + have ep : p ≠ ss := fun e => by rw [e, Function.update_self] at h; cases h + rw [Function.update_of_ne ep] at h + by_cases ed' : p = sd + · rw [ed', Function.update_self] at h; exact ⟨ep, Or.inl ⟨ed', h⟩⟩ + · rw [Function.update_of_ne ed'] at h; exact ⟨ep, Or.inr ⟨ed', h⟩⟩ + have horig : ∀ p, σ2.isOriginalCap p = true → σ1.kheap p.1 ≠ none ∨ σ1.isOriginalCap p = true := fun p h => by + rcases (origE p h).2 with ⟨e, -⟩ | ⟨-, e⟩ + · exact Or.inl (e ▸ kd) + · exact Or.inr e + have origLive : ∀ sl, σ2.isOriginalCap sl = true → AbsSlotA Z.th sl := fun p h => by + rcases (origE p h).2 with ⟨e, -⟩ | ⟨-, e⟩ + · exact e ▸ hAd + · exact J1.origLive p e + -- spec caps + have capb : ∀ sl, specCapAtA σ2 sl = specCapAtA σb sl := fun sl => by unfold specCapAtA; rw [ekh] + have capE : ∀ sl c, specCapAtA σ2 sl = some c → + specCapAtA σ1 sl = some c ∨ (sl = sd ∧ c = ac) ∨ (sl = ss ∧ c = .nullCap) := by + refine specCapAtA_orA (Q := fun sl c => (sl = sd ∧ c = ac) ∨ (sl = ss ∧ c = .nullCap)) HS (fun p o h => ?_) + rw [ekh] at h + obtain ⟨ob, kb, cb, wb⟩ := setCap_objsA h2 p o h + obtain ⟨oa, ka, ca, wa'⟩ := setCap_objsA h1 p ob kb + refine ⟨oa, ka, fun b c hb => ?_, fun n => (wb n).trans (wa' n)⟩ + rcases cb b c hb with h | h + · exact Or.inl (Or.inr h) + · rcases ca b c h with h | h + · exact Or.inl (Or.inl h) + · exact Or.inr h + have hsc : ∀ sl c, specCapAtA σ2 sl = some c → (∃ sl0, specCapAtA σ1 sl0 = some c) ∨ NoIdleCapV m n c := + fun sl c h => by + rcases capE sl c h with h | ⟨-, e⟩ | ⟨-, e⟩ + · exact Or.inl ⟨sl, h⟩ + · exact Or.inr (e ▸ hsci) + · exact Or.inr (by rw [e]; exact ⟨fun _ _ h => (by cases h), fun _ h => (by cases h), fun _ _ _ h => (by cases h)⟩) + have nullOrig : ∀ sl, specCapAtA σ2 sl = some .nullCap → σ2.isOriginalCap sl = false := fun sl h => by + by_cases es' : sl = ss + · rw [orig2, es', Function.update_self] + by_cases ed' : sl = sd + · exfalso + rw [ed'] at h + have cd2 : capAtA (L.mem s2) (cslotAddrA sd) = nc := ed + have key : ∀ c, specCapAtA σ2 sd = some c → CapRelA (capAtA (L.mem s2) (cslotAddrA sd)) c → False := by + intro c e hr + rw [h] at e; cases e + rw [cd2] at hr + exact CapRelA.ne_null hr hn rfl + obtain ⟨o, idx⟩ := sd + obtain ⟨p, e1, hc | ⟨ht, i, hi, e2⟩⟩ := hAd + · simp only at e1 hc; subst e1 + obtain ⟨cs, hk2, hwf, hr⟩ := jx.1.st.cnode p _ hc + obtain ⟨c, hcc, hrel⟩ := hr idx rfl + exact key c (by unfold specCapAtA; simp only [hk2]; rw [if_pos hwf]; exact hcc) hrel + · simp only at e1 e2; subst e1 e2 + obtain ⟨y, hk2, hr⟩ := jx.1.st.tcb p ht + obtain ⟨c, hcc, hrel⟩ := hr i hi + refine key c (by unfold specCapAtA; simp only [hk2]; exact hcc) ?_ + unfold cslotAddrA; rw [ofBl_tcbIdx i (by unfold tcbSlotsA at hi; omega)]; exact hrel + rcases capE sl _ h with h | ⟨e, -⟩ | ⟨e, -⟩ + · rw [orig2, Function.update_of_ne es', Function.update_of_ne ed'] + exact J1.nullOrig sl h + · exact absurd e ed' + · exact absurd e es' + -- the buffer slots, the tables + have wA : WritesIn st s1 s2 (fun b => Aligned b ∧ W7.CapW H.tcbs {b | InOS OS b} ∅ b) := writesIn_alAG w + have hB : ∀ b, (Aligned b ∧ W7.CapW H.tcbs {b | InOS OS b} ∅ b) → W7.CapW H.tcbs H.omega H.frame b := + fun b hb => os_capw hO hb.1 hb.2 + obtain ⟨-, -, -, -, -, fld, -, -⟩ := W7.blk_of_capw (BXI.blk J1.sys.ent.bxi) wA hB + have sbS : ∀ t ∈ Z.T, sbWordA t ∈ OS := fun t ht => + RS.sub _ ⟨t, Or.inr ⟨(J1.j.1.thT t).2 ht, 2, by simp [tcbSlotsA], (bufSlot_eqA t).symm⟩⟩ + have sbK : ∀ t ∈ Z.T, capA st s2 (sbWordA t) = capA st s1 (sbWordA t) := fun t ht => + ek _ (sbS t ht) (cnSlot_ne_sbA hP hCs.1 hCs.2 ((J1.j.1.thT t).2 ht)) + (cnSlot_ne_sbA hP hCd.1 hCd.2 ((J1.j.1.thT t).2 ht)) + have ib : ∀ t ∈ Z.T, L.mem s2 (ibWordA t) = L.mem s1 (ibWordA t) := + fun t ht => fld t (by rw [J1.tcbs]; exact ht) 3512#u64 (by simp) (by simp) (by simp) + have bufCover : BufCover st Z.T Bf s2 σ2 := BufCover.mapA J1.bufCover J1.bufNative J1.j.1.st jx.1.st + (fun t ht => (J1.j.1.thT t).2 ht) HS ib (fun t ht => by rw [sbK t ht]; exact .refl _) + have bufNative : BufNativeA Z.T (L.mem s2) := BufNativeA.mapA J1.bufNative (fun t ht q sz' rr dv m' e' a' v' h => by + rw [sbK t ht] at h + have hb := J1.bufNative t ht + rw [bufSlot_eqA, capAtA_eq (st := st), h] at hb + exact hb) + have vk : ∀ Pt Ub : Set U64, VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s1) Pt → + VsKeep st (L.ksb s1) Pt s1 s2 := fun Pt Ub vr => + VsKeep.of_w vr.al w (fun b hb => J1.vsr_offCapWA vr hb) + have unmPt : UnmPtSpecA st { Z with G := mvGA Z.G (cslotAddrA ss) (cslotAddrA sd) } m OS s2 σ2 := by + obtain ⟨Pt, Ub, vr, -, -, -, -, -, -, link, -, -⟩ := J1.j7 + exact UnmPtSpecA.derivA F HS w.1 w.2.1 (vk Pt Ub vr) link + (fun p h => ⟨fun i hi hf => h.mem i hi (FootA.monoA + (Z := { Z with G := mvGA Z.G (cslotAddrA ss) (cslotAddrA sd), S1 := ∅ }) rfl rfl (fun _ h => h) + (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) + (FootA.of_ksA w.1.symm w.2.1.symm hf)), h.obj⟩) J1.unmPt + have bufOff : BufOffA st { Z with G := mvGA Z.G (cslotAddrA ss) (cslotAddrA sd) } OS s2 Bf := + ⟨J1.bufOff.os, fun b hb => by rw [w.1]; exact J1.bufOff.vs b hb, + fun b hb q hq => J1.bufOff.page b hb q (SchedPageA.derivBackA F rfl rfl rfl rfl rfl hq), J1.bufOff.vcw, + fun bp hbp => (J1.bufOff.pin bp hbp).keepC rfl rfl fun t ht _ => sbK t ht⟩ + have sinv : SInvXA σ2 m n := J1.sinv.capMove (by rw [hirqN]; exact hok) hm + have hm0 : ((), σ2) ∈ (capMove (Z := Unit) ac ss sd (stUpdA σ σ.curThread x .restart)).1 := by rw [hσ1]; exact hm + have hm' := j7 x s2 σ2 hk hs2 hm0 + refine ⟨x, s2, σ2, hk, heq, hs2, hm0, ?_⟩ + have hfh : ∀ t ∈ Z.T, capA st s2 (slotIdx t layout.tcb.SLOT_FAULT_HANDLER) = + capA st s1 (slotIdx t layout.tcb.SLOT_FAULT_HANDLER) := fun t ht => + ek _ (J1.j5.2.1 _ (isSlot_fhA ((J1.j.1.thT t).2 ht))) + (cnSlot_ne_fhA J1.j.1.place hCs.1 hCs.2 ((J1.j.1.thT t).2 ht)) + (cnSlot_ne_fhA J1.j.1.place hCd.1 hCd.2 ((J1.j.1.thT t).2 ht)) + have hvt : IdleSlotNewA σ1 σ2 m n := IdleSlotNewA.of_cnodeA J1.sidle fun sl c h => by + rcases capE sl c h with h | ⟨e, -⟩ | ⟨e, -⟩ + · exact Or.inl h + · obtain ⟨cs, hk1, -⟩ := J1.j.1.st.cnode pd _ hCd.2 + exact Or.inr ⟨_, cs, by rw [e, hCd.1]; exact hk1⟩ + · obtain ⟨cs, hk1, -⟩ := J1.j.1.st.cnode ps _ hCs.2 + exact Or.inr ⟨_, cs, by rw [e, hCs.1]; exact hk1⟩ + refine ⟨?_, by rw [hσ1]; exact Sched.BodyKeepA.ofSlot HS hms⟩ + exact J1.of_capDerivA (hsub := J1.j5.2.1) (hnul := J1.j5.2.2) (hfh := hfh) (hvt := hvt) + (symL := symL s2 hs2) (G' := mvGA Z.G (cslotAddrA ss) (cslotAddrA sd)) w F HS W hms hoff hfal hcdtS horig + origLive nullOrig hsc (sysl s2 hs2) jx sinv bufCover bufNative unmPt bufOff j5' hm' + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3ArmCnRotA.lean b/proofs/Fl4ProofsA64/P3/Stage3ArmCnRotA.lean new file mode 100644 index 000000000..a7d534280 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ArmCnRotA.lean @@ -0,0 +1,415 @@ +/- +P3 (AArch64, merge line, D8 step 5 (iii)): **the CNode Rotate arm, source ≠ destination** — the caller's Restart, +then two `cdt::move_cap`s (pivot → destination, then source → pivot; cnode.rs `decode_rotate`), against l4v's two +`cap_move`s, keeps `JStage3A`. + +* `CapMoveSpecA` / `capMove_specA`: what one spec `cap_move` outcome does to the CDT, the original flags, the slot + caps, the well-formed CNodes and the machine (the spec-side block of `cnodeMoveA`, factored per step), with the + transfers `horig`, `live`, `noIdle` and `SpecSlotA.domBackA`, composable across two steps. +* `specCap_nnA`: a related slot holding a non-null FL4 cap does not hold the spec null cap. +* `CapsFromA.of_rotA`: the slot caps after both moves, derived both ways from those before. +* `JStage3A.cnodeRotateA`: the arm. One `of_capDerivA` application over the composite: the MDB slot (`J5A.moveA` + twice, the middle tree from `cap_move_refinesA`), the joint relation rebuilt once (`JAtZAX.of_slotWrites` over + both write sets), and **no `SysL` / J7 hypothesis at the middle state**: only at the final state (Option A/X). + Its case hypotheses are the decode's facts about the two new caps and the three CNode slots. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3BodyKeepA +import Fl4ProofsA64.P3.Stage3ArmCnMoveA +import Fl4ProofsA64.P3.CnodeRotA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## One spec `cap_move` -/ + +/-- A slot-only spec step keeps every object present: one after was there before. -/ +theorem SpecSlotA.domBackA {σ σ' : Fl4SpecA64.State Unit} (HS : SpecSlotA σ σ') {p : Fl4SpecA64.ObjRef} + (h : σ'.kheap p ≠ none) : σ.kheap p ≠ none := by + rcases HS.kh p with e | ⟨r, cs, cs', e1, -⟩ | ⟨x, x', e1, -, -⟩ + · rwa [← e] + · rw [e1]; simp + · rw [e1]; simp + +/-- **What one spec `cap_move a ss sd` outcome does** (module docstring). -/ +structure CapMoveSpecA (σ σ' : Fl4SpecA64.State Unit) (a : Fl4SpecA64.Cap) (ss sd : Fl4SpecA64.CslotPtr) : + Prop where + wf : WfKeepA σ σ' + ms : σ'.machineState = σ.machineState + kd : σ.kheap sd.1 ≠ none + cdt : ∀ x y, σ'.cdt x = some y → ∀ sl, sl = x ∨ sl = y → + σ.kheap sl.1 ≠ none ∨ ∃ x0 y0, σ.cdt x0 = some y0 ∧ (sl = x0 ∨ sl = y0) + origS : σ'.isOriginalCap ss = false + origD : σ'.isOriginalCap sd = σ.isOriginalCap ss + origO : ∀ p, p ≠ ss → p ≠ sd → σ'.isOriginalCap p = σ.isOriginalCap p + cap : ∀ sl c, specCapAtA σ' sl = some c → + specCapAtA σ sl = some c ∨ (sl = sd ∧ c = a) ∨ (sl = ss ∧ c = .nullCap) + +/-- **`CapMoveSpecA` of every `cap_move` outcome** of a slot-only step between distinct slots. -/ +theorem capMove_specA {σ σ' : Fl4SpecA64.State Unit} {a : Fl4SpecA64.Cap} {ss sd : Fl4SpecA64.CslotPtr} + (hm : ((), σ') ∈ (capMove (Z := Unit) a ss sd σ).1) (HS : SpecSlotA σ σ') (hne : ss ≠ sd) : + CapMoveSpecA σ σ' a ss sd := by + obtain ⟨σa, σb, h1, h2, ecdt, eorig, ekh, ems⟩ := capMove_outA hm + obtain ⟨c1, o1, m1⟩ := setCap_keepsA h1 + obtain ⟨c2, o2, m2⟩ := setCap_keepsA h2 + have W1 := setCap_wfA _ _ _ _ h1 + have W2 := setCap_wfA _ _ _ _ h2 + have kd : σ.kheap sd.1 ≠ none := by + obtain ⟨o, -, hk0, -⟩ := setCap_memA (p := sd.1) (cref := sd.2) h1 + rw [hk0]; simp + have orig2 : σ'.isOriginalCap = + Function.update (Function.update σ.isOriginalCap sd (σ.isOriginalCap ss)) ss false := by + rw [eorig]; unfold moveFinA; simp only [o2, o1] + have cdtE : ∀ x y, σ'.cdt x = some y → (x = sd ∨ σ.cdt x ≠ none) ∧ (y = sd ∨ ∃ x0, σ.cdt x0 = some y) := by + intro p q h + rw [ecdt] at h + unfold moveFinA at h + simp only [c2, c1] at h + by_cases ep : p = ss + · subst ep + rw [Function.update_self] at h + simp at h + rw [Function.update_of_ne ep] at h + by_cases ed' : p = sd + · subst ed' + rw [Function.update_self] at h + split at h + · cases h; exact ⟨Or.inl rfl, Or.inl rfl⟩ + · exact ⟨Or.inl rfl, Or.inr ⟨ss, h⟩⟩ + rw [Function.update_of_ne ed'] at h + split at h + · rename_i h' + cases h; exact ⟨Or.inr (by rw [h']; simp), Or.inl rfl⟩ + · exact ⟨Or.inr (by rw [h]; simp), Or.inr ⟨p, h⟩⟩ + refine ⟨(W1.trans W2).trans (WfKeepA.of_kheap ekh), by rw [ems, m2, m1], kd, ?_, ?_, ?_, ?_, ?_⟩ + · intro p q h sl hsl + obtain ⟨hp, hq⟩ := cdtE p q h + rcases hsl with rfl | rfl + · rcases hp with e | e + · exact Or.inl (e ▸ kd) + · obtain ⟨y0, h0⟩ := Option.ne_none_iff_exists'.1 e + exact Or.inr ⟨_, y0, h0, Or.inl rfl⟩ + · rcases hq with e | ⟨x0, h0⟩ + · exact Or.inl (e ▸ kd) + · exact Or.inr ⟨x0, _, h0, Or.inr rfl⟩ + · rw [orig2, Function.update_self] + · rw [orig2, Function.update_of_ne (Ne.symm hne), Function.update_self] + · intro p e1 e2 + rw [orig2, Function.update_of_ne e1, Function.update_of_ne e2] + · refine specCapAtA_orA (Q := fun sl c => (sl = sd ∧ c = a) ∨ (sl = ss ∧ c = .nullCap)) HS (fun p o h => ?_) + rw [ekh] at h + obtain ⟨ob, kb, cb, wb⟩ := setCap_objsA h2 p o h + obtain ⟨oa, ka, ca, wa'⟩ := setCap_objsA h1 p ob kb + refine ⟨oa, ka, fun b c hb => ?_, fun n => (wb n).trans (wa' n)⟩ + rcases cb b c hb with h | h + · exact Or.inl (Or.inr h) + · rcases ca b c h with h | h + · exact Or.inl (Or.inl h) + · exact Or.inr h + +namespace CapMoveSpecA +variable {σ σ' : Fl4SpecA64.State Unit} {a : Fl4SpecA64.Cap} {ss sd : Fl4SpecA64.CslotPtr} + (M : CapMoveSpecA σ σ' a ss sd) +include M + +theorem orig_true {p : Fl4SpecA64.CslotPtr} (h : σ'.isOriginalCap p = true) : + p = sd ∧ σ.isOriginalCap ss = true ∨ σ.isOriginalCap p = true := by + by_cases e1 : p = ss + · rw [e1, M.origS] at h; cases h + by_cases e2 : p = sd + · rw [e2, M.origD] at h; exact Or.inl ⟨e2, h⟩ + · rw [M.origO p e1 e2] at h; exact Or.inr h + +theorem horig {p : Fl4SpecA64.CslotPtr} (h : σ'.isOriginalCap p = true) : + σ.kheap p.1 ≠ none ∨ σ.isOriginalCap p = true := by + rcases M.orig_true h with ⟨e, -⟩ | e + · exact Or.inl (e ▸ M.kd) + · exact Or.inr e + +theorem live {P : Fl4SpecA64.CslotPtr → Prop} (h : ∀ sl, σ.isOriginalCap sl = true → P sl) (hd : P sd) : + ∀ sl, σ'.isOriginalCap sl = true → P sl := fun p hp => by + rcases M.orig_true hp with ⟨e, -⟩ | e + · exact e ▸ hd + · exact h p e + +theorem noIdle {m : Fl4SpecA64.McState} {n : Nat} (ha : NoIdleCapV m n a) : + ∀ sl c, specCapAtA σ' sl = some c → (∃ sl0, specCapAtA σ sl0 = some c) ∨ NoIdleCapV m n c := + fun sl c h => by + rcases M.cap sl c h with h | ⟨-, e⟩ | ⟨-, e⟩ + · exact Or.inl ⟨sl, h⟩ + · exact Or.inr (e ▸ ha) + · exact Or.inr (by rw [e]; exact ⟨fun _ _ h => (by cases h), fun _ h => (by cases h), fun _ _ _ h => (by cases h)⟩) + +end CapMoveSpecA + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-- **The slot caps after Rotate's two moves** (pivot's `np` to the destination, source's `ns` to the pivot). -/ +theorem CapsFromA.of_rotA {OS : Set U64} {s s' : S} {src piv dest : U64} (hs : src ∈ OS) (hp : piv ∈ OS) + (hd : dest ∈ OS) {np ns : capability.Cap} (hDp : Fl4ProofsA64.P3.DerivA np (capA st s piv)) + (hDs : Fl4ProofsA64.P3.DerivA ns (capA st s src)) (hdn : capA st s dest = .Null) + (ed : capA st s' dest = np) (ep : capA st s' piv = ns) (es : capA st s' src = .Null) + (ek : ∀ y ∈ OS, y ≠ src → y ≠ piv → y ≠ dest → capA st s' y = capA st s y) : CapsFromA st OS s s' := by + refine ⟨fun u hu => ?_, fun v hv => ?_⟩ + · by_cases e1 : u = src + · exact Or.inl (e1 ▸ es) + by_cases e2 : u = piv + · exact Or.inr ⟨src, hs, by rw [e2, ep]; exact hDs⟩ + by_cases e3 : u = dest + · exact Or.inr ⟨piv, hp, by rw [e3, ed]; exact hDp⟩ + exact Or.inr ⟨u, hu, by rw [ek u hu e1 e2 e3]; exact .refl _⟩ + · by_cases e1 : v = src + · exact Or.inr ⟨piv, hp, by rw [ep, e1]; exact hDs⟩ + by_cases e2 : v = piv + · exact Or.inr ⟨dest, hd, by rw [ed, e2]; exact hDp⟩ + by_cases e3 : v = dest + · exact Or.inl (e3 ▸ hdn) + exact Or.inr ⟨v, hv, by rw [ek v hv e1 e2 e3]; exact .refl _⟩ + +set_option maxHeartbeats 4000000 in +/-- **`JStage3A` across the CNode Rotate arm, source ≠ destination** (module docstring). -/ +theorem JStage3A.cnodeRotateA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {ar : machine.Arch A} {nA : U64} {s : S} {a : A} + {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) (hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM) + (ss sp sd : Fl4SpecA64.CslotPtr) {ps pp pd : U64} + (hCs : ss.1 = specPtr ps ∧ Z.th ps = some (.cnode ss.2.length)) + (hCp : sp.1 = specPtr pp ∧ Z.th pp = some (.cnode sp.2.length)) + (hCd : sd.1 = specPtr pd ∧ Z.th pd = some (.cnode sd.2.length)) + (hsp : cslotAddrA ss ≠ cslotAddrA sp) (hsd : cslotAddrA ss ≠ cslotAddrA sd) + (hsn : capA st s (cslotAddrA ss) ≠ .Null) (hpn : capA st s (cslotAddrA sp) ≠ .Null) + (hdn : capA st s (cslotAddrA sd) = .Null) + (hpc : obj.sext48 (cslotAddrA sp) = ok (cslotAddrA sp)) (hdc : obj.sext48 (cslotAddrA sd) = ok (cslotAddrA sd)) + {np : capability.Cap} {ap : Fl4SpecA64.Cap} (hcp : CapRelA np ap) (hwp : CapRt.EncWf np) (hnp : np ≠ .Null) + (hDp : Fl4ProofsA64.P3.DerivA np (capA st s (cslotAddrA sp))) (halp : UA np (capA st s (cslotAddrA sp))) + (hshp : Fl4ProofsA64.P1.W7.R.ST.shapeA np = Fl4ProofsA64.P1.W7.R.ST.shapeA (capA st s (cslotAddrA sp))) + (hbdp : badgeA np = badgeA (capA st s (cslotAddrA sp))) + (hirqp : ∀ i, np = .IrqHandler i ↔ capA st s (cslotAddrA sp) = .IrqHandler i) + (hop : CapObjOkA Z np) (hokp : CapOkA σ.interruptIrqNode sd ap) (hscp : NoIdleCapV m n ap) + {ns : capability.Cap} {an : Fl4SpecA64.Cap} (hcs : CapRelA ns an) (hws : CapRt.EncWf ns) (hns : ns ≠ .Null) + (hDs : Fl4ProofsA64.P3.DerivA ns (capA st s (cslotAddrA ss))) (hals : UA ns (capA st s (cslotAddrA ss))) + (hshs : Fl4ProofsA64.P1.W7.R.ST.shapeA ns = Fl4ProofsA64.P1.W7.R.ST.shapeA (capA st s (cslotAddrA ss))) + (hbds : badgeA ns = badgeA (capA st s (cslotAddrA ss))) + (hirqs : ∀ i, ns = .IrqHandler i ↔ capA st s (cslotAddrA ss) = .IrqHandler i) + (hos : CapObjOkA Z ns) (hoks : CapOkA σ.interruptIrqNode sp an) (hscs : NoIdleCapV m n an) + (symL : ∀ s1 s3, cdt.move_cap st (L.wr s (stA (L.curW s)) sched.TS_RESTART) np (cslotAddrA sp) + (cslotAddrA sd) = ok s1 → cdt.move_cap st s1 ns (cslotAddrA ss) (cslotAddrA sp) = ok s3 → + SymL st H Z.Cs s3) + (sysl : ∀ s1 s3, cdt.move_cap st (L.wr s (stA (L.curW s)) sched.TS_RESTART) np (cslotAddrA sp) + (cslotAddrA sd) = ok s1 → cdt.move_cap st s1 ns (cslotAddrA ss) (cslotAddrA sp) = ok s3 → + SysL st H Z.Cs OS ia ie s3) + (j7 : ∀ x s1 s3 σ1 σ3, σ.kheap σ.curThread = some (.tcb x) → + cdt.move_cap st (L.wr s (stA (L.curW s)) sched.TS_RESTART) np (cslotAddrA sp) (cslotAddrA sd) = ok s1 → + cdt.move_cap st s1 ns (cslotAddrA ss) (cslotAddrA sp) = ok s3 → + ((), σ1) ∈ (capMove (Z := Unit) ap sp sd (stUpdA σ σ.curThread x .restart)).1 → + ((), σ3) ∈ (capMove (Z := Unit) an ss sp σ1).1 → + J7A st (J7R2A st ar nA) { Z with G := rotGA Z.G (cslotAddrA ss) (cslotAddrA sp) (cslotAddrA sd) } + H OS s3 σ3 m) : + ∃ x s1 s3 σ1 σ3, σ.kheap σ.curThread = some (.tcb x) ∧ + sched.set_thread_state st s (L.curW s) sched.TS_RESTART = ok (L.wr s (stA (L.curW s)) sched.TS_RESTART) ∧ + cdt.move_cap st (L.wr s (stA (L.curW s)) sched.TS_RESTART) np (cslotAddrA sp) (cslotAddrA sd) = ok s1 ∧ + cdt.move_cap st s1 ns (cslotAddrA ss) (cslotAddrA sp) = ok s3 ∧ + ((), σ1) ∈ (capMove (Z := Unit) ap sp sd (stUpdA σ σ.curThread x .restart)).1 ∧ + ((), σ3) ∈ (capMove (Z := Unit) an ss sp σ1).1 ∧ + JStage3A st AR { Z with G := rotGA Z.G (cslotAddrA ss) (cslotAddrA sp) (cslotAddrA sd) } H OS U Ac Pp Tb + Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s3 a σ3 m ∧ + Sched.BodyKeepA (stUpdA σ σ.curThread x .restart) σ3 := by + have hAs : AbsSlotA Z.th ss := ⟨ps, hCs.1, Or.inl hCs.2⟩ + have hAp : AbsSlotA Z.th sp := ⟨pp, hCp.1, Or.inl hCp.2⟩ + have hAd : AbsSlotA Z.th sd := ⟨pd, hCd.1, Or.inl hCd.2⟩ + obtain ⟨x, hk, heq, -, J1, -, hOS1, -⟩ := J.of_restartCurA RS.pl.choose_spec.choose_spec.choose_spec.choose_spec + RS.sym RS.run hnvm RS.cur RS.vs RS.vc + generalize hs0 : L.wr s (stA (L.curW s)) sched.TS_RESTART = s0 at heq J1 hOS1 sysl symL j7 ⊢ + generalize hσ0 : stUpdA σ σ.curThread x .restart = σ0 at J1 j7 ⊢ + have hirqN : σ0.interruptIrqNode = σ.interruptIrqNode := by rw [← hσ0]; rfl + have hO : OSOK H.tcbs H.omega H.frame OS := J1.sys.ent.os + have hP := J1.j.1.place + have hsS := hAs.isSlot hP + have hpS := hAp.isSlot hP + have hdS := hAd.isSlot hP + have xs : cslotAddrA ss ∈ OS := RS.sub _ hsS + have xp : cslotAddrA sp ∈ OS := RS.sub _ hpS + have xd : cslotAddrA sd ∈ OS := RS.sub _ hdS + have cs0 : capA st s0 (cslotAddrA ss) = capA st s (cslotAddrA ss) := hOS1.capA xs + have cp0 : capA st s0 (cslotAddrA sp) = capA st s (cslotAddrA sp) := hOS1.capA xp + have cd0 : capA st s0 (cslotAddrA sd) = .Null := (hOS1.capA xd).trans hdn + rw [← cs0] at hsn hDs hals hshs hbds hirqs + rw [← cp0] at hpn hDp halp hshp hbdp hirqp + have hpd : cslotAddrA sp ≠ cslotAddrA sd := fun e => hpn (e ▸ cd0) + have E1 := J1.toJEntA + have hTr : TreeA st OS Z.G.chains Z.G.par s0 := + J1.j.1.cdt.tree.grow J1.j5.2.1 (fun x hx hn => J1.j5.2.2 x hx hn) + have hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s0 y = .Null := J1.j5.2.2 + have hoff := footN_offA E1 + have hfal := FootNA.aligned J1.j.1 (bxi_nodes E1.sys.ent.bxi) E1.scAl E1.ap.vcAl + have F0 := hTr.lk.lkF hO.nz + have inOS : ∀ y ∈ Z.G.chains.flatten, y ∈ OS := fun y hy => ((hTr.lk.members y).1 hy).1 + have hpG : cslotAddrA sp ∈ Z.G.chains.flatten := (hTr.lk.members _).2 ⟨xp, hpn⟩ + -- the first move: the pivot's new cap to the destination + have mv1 := WP.spec_and (WP.spec_and + (cap_move_refinesA s0 σ0 Z.G J1.j.1.st J1.j.1.cdt hP hO hTr J1.j5.2.1 hnul hAp hAd hpn cd0 hdc hcp hwp hnp halp) + (moveCap_osA hO xp xd (fun h => inOS _ (F0.pvm _ hpG h)) (fun h => inOS _ (F0.nxm _ hpG h)))) + (J5A.moveA (σ' := σ0) (m := m) (m' := m) hO J1.j5 hTr xp xd hpd hpS hdS hpn cd0 hdc hwp hnp halp hshp hbdp + hirqp (fun s' w i hi => w.mem_eq (hfal _ (.irq i hi)) (hoff _ (.irq i hi)))) + obtain ⟨s1, hs1, ⟨⟨σ1, hm1, hst1, hcdt1, hT1, HS1, hcs1⟩, -⟩, w1, ed1, es1, ek1, J51⟩ := spec_existsA mv1 + -- between the moves: no `SysL` / J7, only the relation, the tree and the MDB slot + have ks1 : L.ksb s1 = L.ksb s0 := w1.1 + have cs1 : capA st s1 (cslotAddrA ss) = capA st s0 (cslotAddrA ss) := ek1 _ xs hsp hsd + have hsn1 : capA st s1 (cslotAddrA ss) ≠ .Null := by rw [cs1]; exact hsn + have hnul1 : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s1 y = .Null := fun y hy hn => by + rw [ek1 y hy (fun e => hn (e ▸ hpS)) (fun e => hn (e ▸ hdS))]; exact hnul y hy hn + have hst1' : ∀ s', WritesIn st s1 s' (W7.CapW H.tcbs {b | InOS OS b} ∅) → + ∀ i : U64, i.val < 5376 → L.mem s' (irqStAddrA (L.ksb s1) i) = L.mem s1 (irqStAddrA (L.ksb s1) i) := + fun s' w i hi => by rw [ks1]; exact w.mem_eq (hfal _ (.irq i hi)) (hoff _ (.irq i hi)) + -- the second move: the source's new cap to the pivot + have mv2 := WP.spec_and + (cap_move_refinesA s1 σ1 _ hst1 hcdt1 hP hO hT1 J1.j5.2.1 hnul1 hAs hAp hsn1 es1 hpc hcs hws hns + (by rw [cs1]; exact hals)) + (J5A.moveA (σ' := σ1) (m := m) (m' := m) hO J51 hT1 xs xp hsp hsS hpS hsn1 es1 hpc hws hns + (by rw [cs1]; exact hals) (by rw [cs1]; exact hshs) (by rw [cs1]; exact hbds) (by rw [cs1]; exact hirqs) hst1') + obtain ⟨s3, hs3, ⟨σ3, hm2, hst3, hcdt3, -, HS2, hcs2⟩, w2, ed2, es2, ek2, -⟩ := spec_existsA mv2 + have j5' : J5A st { Z with G := rotGA Z.G (cslotAddrA ss) (cslotAddrA sp) (cslotAddrA sd) } H OS s3 σ3 m := by + obtain ⟨s3', hs3', -, -, -, -, J5'⟩ := spec_existsA + (J5A.moveA (σ' := σ3) (m := m) (m' := m) hO J51 hT1 xs xp hsp hsS hpS hsn1 es1 hpc hws hns + (by rw [cs1]; exact hals) (by rw [cs1]; exact hshs) (by rw [cs1]; exact hbds) (by rw [cs1]; exact hirqs) + hst1') + rw [hs3] at hs3'; have e := okinj hs3'; subst e; exact J5' + have w : WritesIn st s0 s3 (W7.CapW H.tcbs {b | InOS OS b} ∅) := w1.trans1 w2 + have HS : SpecSlotA σ0 σ3 := HS1.trans HS2 + have jx : JAtZAX (L := L) AR { Z with G := rotGA Z.G (cslotAddrA ss) (cslotAddrA sp) (cslotAddrA sd) } s3 a σ3 m := by + refine J1.j.of_slotWrites w (fun b hb' hb => hoff b hb hb') hfal _ HS hst3 hcdt3 (fun y hy => ?_) + rcases hcs2 y hy with e | e | e <;> rw [e] + · exact CapObjOkA.null Z + · rcases hcs1 y hy with e' | e' | e' <;> rw [e'] + · exact CapObjOkA.null Z + · exact J1.j.capObj hy + · exact hop + · exact hos + -- the caps after both moves + have cd3 : capA st s3 (cslotAddrA sd) = np := by rw [ek2 _ xd (Ne.symm hsd) (Ne.symm hpd)]; exact ed1 + have ek : ∀ y ∈ OS, y ≠ cslotAddrA ss → y ≠ cslotAddrA sp → y ≠ cslotAddrA sd → capA st s3 y = capA st s0 y := + fun y hy e1 e2 e3 => by rw [ek2 y hy e1 e2, ek1 y hy e2 e3] + have F : CapsFromA st OS s0 s3 := CapsFromA.of_rotA xs xp xd hDp hDs cd0 cd3 ed2 es2 ek + -- the spec side, per step + have hne1 : sp ≠ sd := fun e => hpd (by rw [e]) + have hne2 : ss ≠ sp := fun e => hsp (by rw [e]) + have M1 := capMove_specA hm1 HS1 hne1 + have M2 := capMove_specA hm2 HS2 hne2 + have W : WfKeepA σ0 σ3 := M1.wf.trans M2.wf + have hms : σ3.machineState = σ0.machineState := M2.ms.trans M1.ms + have hcdtS : ∀ x y, σ3.cdt x = some y → ∀ sl, sl = x ∨ sl = y → + σ0.kheap sl.1 ≠ none ∨ ∃ x0 y0, σ0.cdt x0 = some y0 ∧ (sl = x0 ∨ sl = y0) := fun p q h sl hsl => by + rcases M2.cdt p q h sl hsl with k | ⟨x0, y0, h0, hs0⟩ + · exact Or.inl (HS1.domBackA k) + · exact M1.cdt x0 y0 h0 sl hs0 + have horig : ∀ p, σ3.isOriginalCap p = true → σ0.kheap p.1 ≠ none ∨ σ0.isOriginalCap p = true := fun p h => by + rcases M2.horig h with k | k + · exact Or.inl (HS1.domBackA k) + · exact M1.horig k + have origLive : ∀ sl, σ3.isOriginalCap sl = true → AbsSlotA Z.th sl := + M2.live (M1.live J1.origLive hAd) hAp + have hsc : ∀ sl c, specCapAtA σ3 sl = some c → (∃ sl0, specCapAtA σ0 sl0 = some c) ∨ NoIdleCapV m n c := + fun sl c h => by + rcases M2.noIdle hscs sl c h with ⟨sl1, h1⟩ | h1 + · exact M1.noIdle hscp sl1 c h1 + · exact Or.inr h1 + have nullOrig : ∀ sl, specCapAtA σ3 sl = some .nullCap → σ3.isOriginalCap sl = false := fun sl h => by + by_cases e1 : sl = ss + · rw [e1]; exact M2.origS + by_cases e2 : sl = sp + · exfalso + rw [e2] at h + exact specCap_nnA jx.1.st hAp (fun h' => hns ((show capAtA (L.mem s3) (cslotAddrA sp) = ns from ed2).symm.trans h')) h + by_cases e3 : sl = sd + · exfalso + rw [e3] at h + exact specCap_nnA jx.1.st hAd (fun h' => hnp ((show capAtA (L.mem s3) (cslotAddrA sd) = np from cd3).symm.trans h')) h + rw [M2.origO sl e1 e2, M1.origO sl e2 e3] + rcases M2.cap sl _ h with h2 | ⟨e, -⟩ | ⟨e, -⟩ + · rcases M1.cap sl _ h2 with h1 | ⟨e, -⟩ | ⟨e, -⟩ + · exact J1.nullOrig sl h1 + · exact absurd e e3 + · exact absurd e e2 + · exact absurd e e2 + · exact absurd e e1 + -- the buffer slots, the tables + have wA : WritesIn st s0 s3 (fun b => Aligned b ∧ W7.CapW H.tcbs {b | InOS OS b} ∅ b) := writesIn_alAG w + have hB : ∀ b, (Aligned b ∧ W7.CapW H.tcbs {b | InOS OS b} ∅ b) → W7.CapW H.tcbs H.omega H.frame b := + fun b hb => os_capw hO hb.1 hb.2 + obtain ⟨-, -, -, -, -, fld, -, -⟩ := W7.blk_of_capw (BXI.blk J1.sys.ent.bxi) wA hB + have sbS : ∀ t ∈ Z.T, sbWordA t ∈ OS := fun t ht => + RS.sub _ ⟨t, Or.inr ⟨(J1.j.1.thT t).2 ht, 2, by simp [tcbSlotsA], (bufSlot_eqA t).symm⟩⟩ + have sbK : ∀ t ∈ Z.T, capA st s3 (sbWordA t) = capA st s0 (sbWordA t) := fun t ht => + ek _ (sbS t ht) (cnSlot_ne_sbA hP hCs.1 hCs.2 ((J1.j.1.thT t).2 ht)) + (cnSlot_ne_sbA hP hCp.1 hCp.2 ((J1.j.1.thT t).2 ht)) (cnSlot_ne_sbA hP hCd.1 hCd.2 ((J1.j.1.thT t).2 ht)) + have ib : ∀ t ∈ Z.T, L.mem s3 (ibWordA t) = L.mem s0 (ibWordA t) := + fun t ht => fld t (by rw [J1.tcbs]; exact ht) 3512#u64 (by simp) (by simp) (by simp) + have bufCover : BufCover st Z.T Bf s3 σ3 := BufCover.mapA J1.bufCover J1.bufNative J1.j.1.st jx.1.st + (fun t ht => (J1.j.1.thT t).2 ht) HS ib (fun t ht => by rw [sbK t ht]; exact .refl _) + have bufNative : BufNativeA Z.T (L.mem s3) := BufNativeA.mapA J1.bufNative (fun t ht q sz' rr dv m' e' a' v' h => by + rw [sbK t ht] at h + have hb := J1.bufNative t ht + rw [bufSlot_eqA, capAtA_eq (st := st), h] at hb + exact hb) + have vk : ∀ Pt Ub : Set U64, VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s0) Pt → + VsKeep st (L.ksb s0) Pt s0 s3 := fun Pt Ub vr => + VsKeep.of_w vr.al w (fun b hb => J1.vsr_offCapWA vr hb) + have unmPt : UnmPtSpecA st { Z with G := rotGA Z.G (cslotAddrA ss) (cslotAddrA sp) (cslotAddrA sd) } m OS s3 σ3 := by + obtain ⟨Pt, Ub, vr, -, -, -, -, -, -, link, -, -⟩ := J1.j7 + exact UnmPtSpecA.derivA F HS w.1 w.2.1 (vk Pt Ub vr) link + (fun p h => ⟨fun i hi hf => h.mem i hi (FootA.monoA + (Z := { Z with G := rotGA Z.G (cslotAddrA ss) (cslotAddrA sp) (cslotAddrA sd), S1 := ∅ }) rfl rfl + (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) + (fun _ h => h) (FootA.of_ksA w.1.symm w.2.1.symm hf)), h.obj⟩) J1.unmPt + have bufOff : BufOffA st { Z with G := rotGA Z.G (cslotAddrA ss) (cslotAddrA sp) (cslotAddrA sd) } OS s3 Bf := + ⟨J1.bufOff.os, fun b hb => by rw [w.1]; exact J1.bufOff.vs b hb, + fun b hb q hq => J1.bufOff.page b hb q (SchedPageA.derivBackA F rfl rfl rfl rfl rfl hq), J1.bufOff.vcw, + fun bp hbp => (J1.bufOff.pin bp hbp).keepC rfl rfl fun t ht _ => sbK t ht⟩ + have sinv1 : SInvXA σ1 m n := J1.sinv.capMove (by rw [hirqN]; exact hokp) hm1 + have sinv : SInvXA σ3 m n := sinv1.capMove (by rw [HS1.irqN, hirqN]; exact hoks) hm2 + have hm0 : ((), σ1) ∈ (capMove (Z := Unit) ap sp sd (stUpdA σ σ.curThread x .restart)).1 := by + rw [hσ0]; exact hm1 + have hm' := j7 x s1 s3 σ1 σ3 hk hs1 hs3 hm0 hm2 + refine ⟨x, s1, s3, σ1, σ3, hk, heq, hs1, hs3, hm0, hm2, ?_⟩ + have hfh : ∀ t ∈ Z.T, capA st s3 (slotIdx t layout.tcb.SLOT_FAULT_HANDLER) = + capA st s0 (slotIdx t layout.tcb.SLOT_FAULT_HANDLER) := fun t ht => + ek _ (J1.j5.2.1 _ (isSlot_fhA ((J1.j.1.thT t).2 ht))) + (cnSlot_ne_fhA J1.j.1.place hCs.1 hCs.2 ((J1.j.1.thT t).2 ht)) + (cnSlot_ne_fhA J1.j.1.place hCp.1 hCp.2 ((J1.j.1.thT t).2 ht)) + (cnSlot_ne_fhA J1.j.1.place hCd.1 hCd.2 ((J1.j.1.thT t).2 ht)) + have hvt : IdleSlotNewA σ0 σ3 m n := IdleSlotNewA.of_cnodeA J1.sidle fun sl c h => by + have kS : sl = ss ∨ sl = sp ∨ sl = sd → ∃ r cs, σ0.kheap sl.1 = some (.cnode r cs) := by + rintro (e | e | e) + · obtain ⟨cs, hk1, -⟩ := J1.j.1.st.cnode ps _ hCs.2 + exact ⟨_, cs, by rw [e, hCs.1]; exact hk1⟩ + · obtain ⟨cs, hk1, -⟩ := J1.j.1.st.cnode pp _ hCp.2 + exact ⟨_, cs, by rw [e, hCp.1]; exact hk1⟩ + · obtain ⟨cs, hk1, -⟩ := J1.j.1.st.cnode pd _ hCd.2 + exact ⟨_, cs, by rw [e, hCd.1]; exact hk1⟩ + rcases M2.cap sl c h with h2 | ⟨e, -⟩ | ⟨e, -⟩ + · rcases M1.cap sl c h2 with h1 | ⟨e, -⟩ | ⟨e, -⟩ + · exact Or.inl h1 + · exact Or.inr (kS (Or.inr (Or.inr e))) + · exact Or.inr (kS (Or.inr (Or.inl e))) + · exact Or.inr (kS (Or.inr (Or.inl e))) + · exact Or.inr (kS (Or.inl e)) + refine ⟨?_, by rw [hσ0]; exact Sched.BodyKeepA.ofSlot HS hms⟩ + exact J1.of_capDerivA (hsub := J1.j5.2.1) (hnul := J1.j5.2.2) (hfh := hfh) (hvt := hvt) + (symL := symL s1 s3 hs1 hs3) (G' := rotGA Z.G (cslotAddrA ss) (cslotAddrA sp) (cslotAddrA sd)) w F HS W hms hoff hfal + hcdtS horig origLive nullOrig hsc (sysl s1 s3 hs1 hs3) jx sinv bufCover bufNative unmPt bufOff j5' hm' + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3ArmCnSwapA.lean b/proofs/Fl4ProofsA64/P3/Stage3ArmCnSwapA.lean new file mode 100644 index 000000000..a7d8962ae --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ArmCnSwapA.lean @@ -0,0 +1,535 @@ +/- +P3 (AArch64, merge line, D8 step 5 (iii)): **the CNode Rotate arm, source = destination** — the caller's Restart, +then `cdt::swap` (cnode.rs `decode_rotate`: the source's new cap to the pivot, the pivot's to the source), against +l4v `cap_swap`, keeps `JStage3A`. + +* `swap_fbA`: `swap` and the first-badged bit, exactly (the twin of `P3/SwapA.swap_bitA`, bit 49; `linkFbA` and + `set_nf_fbE` of `P3/Stage3CnMoveA`). +* `J5A.swapA`: the concrete J5 slot across a swap, at the ghost `swGA` (`MdbInvA.ren_swp`, `IrqRunA.ren`, + `HandlersActiveA.ren`), for new caps of the shapes, badges and IRQs of the caps they replace. +* `capSwap_outA`: every outcome of `cap_swap` (two `set_cap`s, then `swapFinA`'s CDT and original flags). +* `CapsFromA.of_swapA`: the slot caps after a swap, derived both ways. +* `JStage3A.cnodeRotateSwA`: the arm, one `of_capDerivA` application; final-state `SysL` and J7 as hypotheses + (Option A/X). Case hypotheses: the decode's facts about the two new caps and the two CNode slots. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3BodyKeepA +import Fl4ProofsA64.P3.Stage3ArmCnRotA +import Fl4ProofsA64.P3.SwapA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add +local notation "FBB" => (562949953421312#u64 : U64) + +/-- **Every outcome of `cap_swap`** (the inversion of `capSwap_memA`): two `set_cap`s, then `swapFinA`'s CDT and +original flags; the extended state aside. -/ +theorem capSwap_outA {σ σ' : Fl4SpecA64.State Unit} {a1 a2 : Fl4SpecA64.Cap} {s1 s2 : Fl4SpecA64.CslotPtr} + (h : ((), σ') ∈ (Fl4SpecA64.capSwap (Z := Unit) a1 s1 a2 s2 σ).1) : + ∃ σa σb, ((), σa) ∈ (setCap (Z := Unit) a2 s1 σ).1 ∧ ((), σb) ∈ (setCap (Z := Unit) a1 s2 σa).1 ∧ + σ'.cdt = (swapFinA σb s1 s2).cdt ∧ σ'.isOriginalCap = (swapFinA σb s1 s2).isOriginalCap ∧ + σ'.kheap = σb.kheap ∧ σ'.machineState = σb.machineState := by + unfold Fl4SpecA64.capSwap at h + obtain ⟨⟨_, σ1⟩, h1, h⟩ := mem_bindA.1 h + obtain ⟨⟨_, σ2⟩, h2, h⟩ := mem_bindA.1 h + refine ⟨σ1, σ2, h1, h2, ?_⟩ + obtain ⟨x, hx, h⟩ := mem_bindA.1 h + obtain rfl : x = (σ2.cdt s1, σ2) := hx + obtain ⟨x, hx, h⟩ := mem_bindA.1 h + obtain rfl : x = (σ2.cdt s2, σ2) := hx + obtain ⟨x, hx, h⟩ := mem_bindA.1 h + obtain rfl : x = (σ2.cdt, σ2) := hx + obtain ⟨x, hx, h⟩ := mem_bindA.1 h + obtain rfl : x = ((fun n => if σ2.cdt n = some s1 then some s2 else if σ2.cdt n = some s2 then some s1 + else σ2.cdt n), σ2) := hx + obtain ⟨x, hx, h⟩ := mem_bindA.1 h + unfold setCdt at hx + obtain ⟨y, hy, hx⟩ := mem_bindA.1 hx + obtain rfl : y = (σ2, σ2) := hy + obtain rfl : x = _ := hx + obtain ⟨x, hx, h⟩ := mem_bindA.1 h + unfold doExtendedOp at hx + obtain ⟨y, hy, hx⟩ := mem_bindA.1 hx + obtain rfl : y = _ := hy + obtain ⟨z, hz, hx⟩ := mem_bindA.1 hx + have ez := hz.2 + obtain rfl : x = _ := hx + rw [ez] at h + obtain ⟨x, hx, h⟩ := mem_bindA.1 h + obtain rfl : x = _ := hx + unfold setOriginal at h + obtain ⟨x, hx, h⟩ := mem_bindA.1 h + obtain ⟨y, hy, hx⟩ := mem_bindA.1 hx + obtain rfl : y = _ := hy + obtain rfl : x = _ := hx + obtain ⟨y, hy, h⟩ := mem_bindA.1 h + obtain rfl : y = _ := hy + have e := (Prod.mk.inj h).2 + subst e + exact ⟨rfl, rfl, rfl, rfl⟩ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {T Ω Sg OS : Set U64} + +theorem fb_okA (s : S) (x : U64) : obj.Slot.first_badged st x s = ok (fbA st s x) := by + unfold obj.Slot.first_badged obj.Slot.next_word fbA + simp only [lift, bind_ok, cte_next, L.read_eq, fb_val] + +/-- `set_next` keeps bit 49 of every `OS` slot (the twin of `bit_next`). -/ +theorem fb_nextA (hO : OSOK T Ω Sg OS) (s : S) {p y : U64} (hp : p ∈ OS) (hy : y ∈ OS) (n : U64) : + L.mem (L.wr s (wa p 24#u64) ((L.mem s (wa p 24#u64) &&& ~~~ obj.MDB_PTR_MASK) ||| (n &&& obj.MDB_PTR_MASK))) + (wa y 24#u64) &&& FBB = L.mem s (wa y 24#u64) &&& FBB := by + by_cases e : y = p + · rw [e, w24_self hO s hp] + exact ptr_bitsFA _ _ _ _ (by unfold obj.MDB_PTR_MASK; rfl) (by unfold obj.MDB_PTR_MASK; rfl) + · rw [w24_wr24 hO s hp hy e] + +set_option maxHeartbeats 4000000 in +/-- **`swap` and the first-badged bit, exactly** (module docstring): the two slots exchange their bits, every other +`OS` slot keeps its own. Hypotheses as `cdt.swap.lrW`. -/ +theorem swap_fbA (hO : OSOK T Ω Sg OS) {s : S} {pv nx : U64 → U64} (hL : LR st OS s pv nx) {x1 x2 : U64} + {c1 c2 : capability.Cap} (hx1 : x1 ∈ OS) (hx2 : x2 ∈ OS) (hne : x1 ≠ x2) (hs1 : obj.sext48 x1 = ok x1) + (hs2 : obj.sext48 x2 = ok x2) (hw1 : CapRt.EncWf c1) (hw2 : CapRt.EncWf c2) + (hp1 : pv x1 ≠ 0#u64 → pv x1 ∈ OS) (hn1 : nx x1 ≠ 0#u64 → nx x1 ∈ OS) + (hp2 : pv x2 ≠ 0#u64 → pv x2 ∈ OS) (hn2 : nx x2 ≠ 0#u64 → nx x2 ∈ OS) : + cdt.swap st s c1 x1 c2 x2 ⦃ s' => + (∀ y ∈ OS, y ≠ x1 → y ≠ x2 → fbA st s' y = fbA st s y) ∧ fbA st s' x1 = fbA st s x2 ∧ + fbA st s' x2 = fbA st s x1 ⦄ := by + obtain ⟨a1, b1, he1, -⟩ := CapRt.decode_encode c1 hw1 + obtain ⟨a2, b2, he2, -⟩ := CapRt.decode_encode c2 hw2 + unfold cdt.swap + rw [set_cap_eq he2, bind_ok, set_cap_eq he1, bind_ok] + have L2 : LR st OS (L.wr (L.wr (L.wr (L.wr s x1 a2) (wa x1 8#u64) b2) x2 a1) (wa x2 8#u64) b1) pv nx := + (hL.wr2 hO hx1).wr2 hO hx2 + have K2 : ∀ y ∈ OS, L.mem (L.wr (L.wr (L.wr (L.wr s x1 a2) (wa x1 8#u64) b2) x2 a1) (wa x2 8#u64) b1) + (wa y 24#u64) = L.mem s (wa y 24#u64) := fun y hy => + ((set_cap_links hO hx2 y hy).2).trans ((set_cap_links hO hx1 y hy).2) + generalize L.wr (L.wr (L.wr (L.wr s x1 a2) (wa x1 8#u64) b2) x2 a1) (wa x2 8#u64) b1 = s2 at L2 K2 ⊢ + have ef1 : fbA st s2 x1 = fbA st s x1 := by unfold fbA; rw [K2 x1 hx1] + rw [slot_prev_eq s2 x1, bind_ok, (L2 x1 hx1).1, next_eq, bind_ok, (L2 x1 hx1).2, revocable_eqA, bind_ok, + fb_okA s2 x1, bind_ok] + simp only [obj.Slot.is_null, bind_ok] + -- stage 3: `P1.NEXT := x2` + refine WP.spec_bind (Pₘ := fun s3 => LR st OS s3 pv (LkAbs.s3 0#u64 pv nx x1 x2) ∧ + ∀ y ∈ OS, L.mem s3 (wa y 24#u64) &&& FBB = L.mem s (wa y 24#u64) &&& FBB) ?_ fun s3 h3 => ?_ + · split + · rename_i hp0 + have hP0 : pv x1 = 0#u64 := by simpa using hp0 + refine (WP.spec_ok _).2 ⟨L2.congr (fun _ _ => rfl) fun z _ => ?_, fun y hy => by rw [K2 y hy]⟩ + unfold LkAbs.s3; simp [hP0] + · rename_i hp0 + have hP0 : pv x1 ≠ 0#u64 := by simpa using hp0 + rw [set_next_eq, WP.spec_ok] + refine ⟨(L2.nw hO (hp1 hP0) (link_bits _ _) hs2).congr (fun _ _ => rfl) fun z _ => ?_, + fun y hy => by rw [fb_nextA hO s2 (hp1 hP0) hy x2, K2 y hy]⟩ + unfold LkAbs.s3; by_cases e : z = pv x1 <;> simp [e, hP0] + obtain ⟨L3, B3⟩ := h3 + -- stage 4: `N1.PREV := x2` + refine WP.spec_bind (Pₘ := fun s4 => LR st OS s4 (LkAbs.s4 0#u64 pv nx x1 x2) (LkAbs.s3 0#u64 pv nx x1 x2) ∧ + ∀ y ∈ OS, L.mem s4 (wa y 24#u64) &&& FBB = L.mem s (wa y 24#u64) &&& FBB) ?_ fun s4 h4 => ?_ + · split + · rename_i hn0 + have hN0 : nx x1 = 0#u64 := by simpa using hn0 + refine (WP.spec_ok _).2 ⟨L3.congr (fun z _ => ?_) (fun _ _ => rfl), B3⟩ + unfold LkAbs.s4; simp [hN0] + · rename_i hn0 + have hN0 : nx x1 ≠ 0#u64 := by simpa using hn0 + rw [set_prev_eq, WP.spec_ok] + refine ⟨(L3.prev hO (hn1 hN0) x2).congr (fun z _ => ?_) (fun _ _ => rfl), + fun y hy => by rw [w24_wr16 hO s3 (hn1 hN0) hy]; exact B3 y hy⟩ + unfold LkAbs.s4; by_cases e : z = nx x1 <;> simp [e, hN0] + obtain ⟨L4, B4⟩ := h4 + have ef2 : fbA st s4 x2 = fbA st s x2 := by unfold fbA; rw [B4 x2 hx2] + have hP2 : LkAbs.s4 0#u64 pv nx x1 x2 x2 ≠ 0#u64 → LkAbs.s4 0#u64 pv nx x1 x2 x2 ∈ OS := by + unfold LkAbs.s4; split + · exact fun _ => hx2 + · exact hp2 + have hN2 : LkAbs.s3 0#u64 pv nx x1 x2 x2 ≠ 0#u64 → LkAbs.s3 0#u64 pv nx x1 x2 x2 ∈ OS := by + unfold LkAbs.s3; split + · exact fun _ => hx2 + · exact hn2 + rw [slot_prev_eq s4 x2, bind_ok, (L4 x2 hx2).1, next_eq, bind_ok, (L4 x2 hx2).2, revocable_eqA, bind_ok, + fb_okA s4 x2, bind_ok] + -- stages 5–8: slot 1 and slot 2 get each other's flags, exactly + rw [set_prev_eq s4 x1 _, bind_ok] + refine WP.spec_bind (set_nf_fbE _ x1 _ _ (fbA st s4 x2)) ?_ + rintro s6 ⟨w6, rfl, hw6⟩ + rw [set_prev_eq _ x2 _, bind_ok] + refine WP.spec_bind (set_nf_fbE _ x2 _ _ (fbA st s2 x1)) ?_ + rintro s8 ⟨w8, rfl, hw8⟩ + rw [ef2] at hw6 + rw [ef1] at hw8 + have B8 : ∀ y ∈ OS, y ≠ x1 → y ≠ x2 → L.mem (L.wr (L.wr (L.wr (L.wr s4 (wa x1 16#u64) + (LkAbs.s4 0#u64 pv nx x1 x2 x2)) (wa x1 24#u64) w6) (wa x2 16#u64) (pv x1)) (wa x2 24#u64) w8) + (wa y 24#u64) &&& FBB = L.mem s (wa y 24#u64) &&& FBB := fun y hy e1 e2 => by + rw [w24_wr24 hO _ hx2 hy e2, w24_wr16 hO _ hx2 hy, w24_wr24 hO _ hx1 hy e1, w24_wr16 hO _ hx1 hy] + exact B4 y hy + have X1 : L.mem (L.wr (L.wr (L.wr (L.wr s4 (wa x1 16#u64) (LkAbs.s4 0#u64 pv nx x1 x2 x2)) (wa x1 24#u64) w6) + (wa x2 16#u64) (pv x1)) (wa x2 24#u64) w8) (wa x1 24#u64) &&& FBB = + (if fbA st s x2 then FBB else 0#u64) := by + rw [w24_wr24 hO _ hx2 hx1 hne, w24_wr16 hO _ hx2 hx1, w24_self hO _ hx1]; exact hw6 + have X2 : L.mem (L.wr (L.wr (L.wr (L.wr s4 (wa x1 16#u64) (LkAbs.s4 0#u64 pv nx x1 x2 x2)) (wa x1 24#u64) w6) + (wa x2 16#u64) (pv x1)) (wa x2 24#u64) w8) (wa x2 24#u64) &&& FBB = + (if fbA st s x1 then FBB else 0#u64) := by + rw [w24_self hO _ hx2]; exact hw8 + generalize L.wr (L.wr (L.wr (L.wr s4 (wa x1 16#u64) (LkAbs.s4 0#u64 pv nx x1 x2 x2)) (wa x1 24#u64) w6) + (wa x2 16#u64) (pv x1)) (wa x2 24#u64) w8 = s8 at B8 X1 X2 ⊢ + try simp only [obj.Slot.is_null, bind_ok] + -- stages 9–10: `P2.NEXT := x1`, `N2.PREV := x1` + refine WP.spec_bind (linkFbA hO s8 (p := LkAbs.s4 0#u64 pv nx x1 x2 x2) (x := x1) ?_ (Or.inl rfl)) + fun s9 h9 => ?_ + · exact fun h => hP2 (by simpa using h) + refine WP.spec_mono (linkFbA hO s9 (p := LkAbs.s3 0#u64 pv nx x1 x2 x2) (x := x1) ?_ (Or.inr rfl)) + fun s10 h10 => ?_ + · exact fun h => hN2 (by simpa using h) + have fin : ∀ y b, L.mem s10 (wa y 24#u64) &&& FBB = (if b then FBB else 0#u64) → fbA st s10 y = b := by + intro y b h + show (L.mem s10 (wa y 24#u64) &&& FBB != 0#u64) = b + rw [h] + cases b <;> rfl + refine ⟨fun y hy e1 e2 => ?_, fin _ _ (by rw [h10 x1 hx1, h9 x1 hx1]; exact X1), + fin _ _ (by rw [h10 x2 hx2, h9 x2 hx2]; exact X2)⟩ + unfold fbA; rw [h10 y hy, h9 y hy, B8 y hy e1 e2] + +/-- **The concrete J5 slot across `cdt::swap`** (module docstring). -/ +theorem J5A.swapA {Z : XSetsA} {H : Heap} {s : S} {σ σ' : Fl4SpecA64.State Unit} {m m' : Fl4SpecA64.McState} + (hO : OSOK H.tcbs H.omega H.frame OS) (J : J5A st Z H OS s σ m) + (hT : TreeA st OS Z.G.chains Z.G.par s) {x1 x2 : U64} (hx1 : x1 ∈ OS) (hx2 : x2 ∈ OS) (hne : x1 ≠ x2) + (hS1 : IsSlotA Z.th x1) (hS2 : IsSlotA Z.th x2) + (hn1 : capA st s x1 ≠ .Null) (hn2 : capA st s x2 ≠ .Null) + (hs1 : obj.sext48 x1 = ok x1) (hs2 : obj.sext48 x2 = ok x2) + {c1 c2 : capability.Cap} (hw1 : CapRt.EncWf c1) (hw2 : CapRt.EncWf c2) + (hsh1 : Fl4ProofsA64.P1.W7.R.ST.shapeA c1 = Fl4ProofsA64.P1.W7.R.ST.shapeA (capA st s x1)) + (hbd1 : badgeA c1 = badgeA (capA st s x1)) (hirq1 : ∀ i, c1 = .IrqHandler i ↔ capA st s x1 = .IrqHandler i) + (hsh2 : Fl4ProofsA64.P1.W7.R.ST.shapeA c2 = Fl4ProofsA64.P1.W7.R.ST.shapeA (capA st s x2)) + (hbd2 : badgeA c2 = badgeA (capA st s x2)) (hirq2 : ∀ i, c2 = .IrqHandler i ↔ capA st s x2 = .IrqHandler i) + (hst : ∀ s', WritesIn st s s' (W7.CapW H.tcbs {b | InOS OS b} ∅) → + ∀ i : U64, i.val < 5376 → L.mem s' (irqStAddrA (L.ksb s) i) = L.mem s (irqStAddrA (L.ksb s) i)) : + cdt.swap st s c1 x1 c2 x2 ⦃ s' => + WritesIn st s s' (W7.CapW H.tcbs {b | InOS OS b} ∅) ∧ capA st s' x1 = c2 ∧ capA st s' x2 = c1 ∧ + (∀ y ∈ OS, y ≠ x1 → y ≠ x2 → capA st s' y = capA st s y) ∧ + J5A st { Z with G := swGA Z.G x1 x2 } H OS s' σ' m' ⦄ := by + have F := hT.lk.lkF hO.nz + have inOS : ∀ y ∈ Z.G.chains.flatten, y ∈ OS := fun y hy => ((hT.lk.members y).1 hy).1 + have hG1 : x1 ∈ Z.G.chains.flatten := (hT.lk.members _).2 ⟨hx1, hn1⟩ + have hG2 : x2 ∈ Z.G.chains.flatten := (hT.lk.members _).2 ⟨hx2, hn2⟩ + have p1 : L.mem s (wa x1 16#u64) ≠ 0#u64 → L.mem s (wa x1 16#u64) ∈ OS := fun h => inOS _ (F.pvm _ hG1 h) + have q1 : NxA st s x1 ≠ 0#u64 → NxA st s x1 ∈ OS := fun h => inOS _ (F.nxm _ hG1 h) + have p2 : L.mem s (wa x2 16#u64) ≠ 0#u64 → L.mem s (wa x2 16#u64) ∈ OS := fun h => inOS _ (F.pvm _ hG2 h) + have q2 : NxA st s x2 ≠ 0#u64 → NxA st s x2 ∈ OS := fun h => inOS _ (F.nxm _ hG2 h) + refine WP.spec_mono (WP.spec_and (WP.spec_and + (cdt.swap.lrWF (osokA hO) (LR.self s) hx1 hx2 hne hs1 hs2 hw1 hw2 p1 q1 p2 q2) + (swap_bitA hO (LR.self s) hx1 hx2 hne hs1 hs2 hw1 hw2 p1 q1 p2 q2)) + (swap_fbA hO (LR.self s) hx1 hx2 hne hs1 hs2 hw1 hw2 p1 q1 p2 q2)) ?_ + rintro s' ⟨⟨⟨w, hLR, e1, e2, ek⟩, hbk, hb1, hb2⟩, hfk, hf1, hf2⟩ + have rv : ∀ y z, L.mem s' (wa y 24#u64) &&& RVB = (if ((L.mem s (wa z 24#u64) &&& RVB) != 0#u64) then RVB + else 0#u64) → Fl4ProofsA64.P1.W7.R.ST.revA st s' y = Fl4ProofsA64.P1.W7.R.ST.revA st s z := by + intro y z h + unfold Fl4ProofsA64.P1.W7.R.ST.revA + have h' : L.mem s' (wa y 24#u64) &&& (281474976710656#u64 : U64) = + (if ((L.mem s (wa z 24#u64) &&& (281474976710656#u64 : U64)) != 0#u64) then 281474976710656#u64 + else 0#u64) := h + rw [h'] + cases ((L.mem s (wa z 24#u64) &&& (281474976710656#u64 : U64)) != 0#u64) <;> rfl + have sw : ∀ a, (a = x1 ∧ swp x1 x2 a = x2) ∨ (a = x2 ∧ swp x1 x2 a = x1) ∨ + (a ≠ x1 ∧ a ≠ x2 ∧ swp x1 x2 a = a) := fun a => by + unfold swp + by_cases h1 : a = x1 + · exact Or.inl ⟨h1, by rw [if_pos h1]⟩ + · by_cases h2 : a = x2 + · exact Or.inr (Or.inl ⟨h2, by rw [if_neg h1, if_pos h2]⟩) + · exact Or.inr (Or.inr ⟨h1, h2, by rw [if_neg h1, if_neg h2]⟩) + have capR : ∀ a ∈ Z.G.chains.flatten, Fl4ProofsA64.P1.W7.R.ST.shapeA (capA st s' (swp x1 x2 a)) = + Fl4ProofsA64.P1.W7.R.ST.shapeA (capA st s a) ∧ badgeA (capA st s' (swp x1 x2 a)) = badgeA (capA st s a) ∧ + (∀ i, capA st s' (swp x1 x2 a) = .IrqHandler i ↔ capA st s a = .IrqHandler i) := fun a ha => by + rcases sw a with ⟨e, ew⟩ | ⟨e, ew⟩ | ⟨f1, f2, ew⟩ + · rw [ew, e2, e]; exact ⟨hsh1, hbd1, hirq1⟩ + · rw [ew, e1, e]; exact ⟨hsh2, hbd2, hirq2⟩ + · rw [ew, ek a (inOS a ha) f1 f2]; exact ⟨rfl, rfl, fun _ => Iff.rfl⟩ + have M : MdbInvA st (renG x1 x2 Z.G.chains) (Fl4Proofs.P3.Mdb.conjPar (Equiv.swap x1 x2) Z.G.par) s' := by + refine MdbInvA.ren_swp hT.lk hT.tree J.1.1 (hO.nz x1 hx1) (hO.nz x2 hx2) (fun a ha => ?_) + (fun a ha => ⟨(capR a ha).1, (capR a ha).2.1⟩) (fun a ha => ?_) (fun a ha => ?_) + · have hm : swp x1 x2 a ∈ OS := swp_os hx1 hx2 (inOS a ha) + rw [(hLR _ hm).2, swp_tr, swp_tr] + exact (LkAbs.sw_ok F hG1 hG2 hne ha).1 + · rcases sw a with ⟨e, ew⟩ | ⟨e, ew⟩ | ⟨f1, f2, ew⟩ + · rw [ew, e]; exact rv _ _ hb2 + · rw [ew, e]; exact rv _ _ hb1 + · rw [ew] + unfold Fl4ProofsA64.P1.W7.R.ST.revA + have := hbk a (inOS a ha) f1 f2 + exact congrArg (fun v => v != 0#u64) this + · rcases sw a with ⟨e, ew⟩ | ⟨e, ew⟩ | ⟨f1, f2, ew⟩ + · rw [ew, e]; exact hf2 + · rw [ew, e]; exact hf1 + · rw [ew]; exact hfk a (inOS a ha) f1 f2 + refine ⟨w, e1, e2, ek, ⟨M, ?_, ?_⟩, J.2.1, fun y hy hns => ?_⟩ + · exact Fl4ProofsA64.P1.W7.R.ST.IrqRunA.ren J.1.2.1 (fun y hy i => (capR y hy).2.2 i) + · exact Fl4ProofsA64.P1.W7.R.ST.HandlersActiveA.ren J.1.2.2 (fun y hy i h => ((capR y hy).2.2 i).1 h) w.1 + (hst s' w) + · have f1 : y ≠ x1 := fun e => hns (e ▸ hS1) + have f2 : y ≠ x2 := fun e => hns (e ▸ hS2) + rw [ek y hy f1 f2]; exact J.2.2 y hy hns + +/-- **The slot caps after a swap**: each slot holds a cap derived from the other's. -/ +theorem CapsFromA.of_swapA {s s' : S} {x1 x2 : U64} (h1 : x1 ∈ OS) (h2 : x2 ∈ OS) + {c1 c2 : capability.Cap} (hD1 : Fl4ProofsA64.P3.DerivA c1 (capA st s x1)) + (hD2 : Fl4ProofsA64.P3.DerivA c2 (capA st s x2)) (e1 : capA st s' x1 = c2) (e2 : capA st s' x2 = c1) + (ek : ∀ y ∈ OS, y ≠ x1 → y ≠ x2 → capA st s' y = capA st s y) : CapsFromA st OS s s' := by + refine ⟨fun u hu => ?_, fun v hv => ?_⟩ + · by_cases f1 : u = x1 + · exact Or.inr ⟨x2, h2, by rw [f1, e1]; exact hD2⟩ + by_cases f2 : u = x2 + · exact Or.inr ⟨x1, h1, by rw [f2, e2]; exact hD1⟩ + exact Or.inr ⟨u, hu, by rw [ek u hu f1 f2]; exact .refl _⟩ + · by_cases f1 : v = x1 + · exact Or.inr ⟨x2, h2, by rw [e2, f1]; exact hD1⟩ + by_cases f2 : v = x2 + · exact Or.inr ⟨x1, h1, by rw [e1, f2]; exact hD2⟩ + exact Or.inr ⟨v, hv, by rw [ek v hv f1 f2]; exact .refl _⟩ + +end + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +set_option maxHeartbeats 4000000 in +/-- **`JStage3A` across the CNode Rotate arm, source = destination** (module docstring). The source's new cap `ns` +goes to the pivot, the pivot's `np` to the source. -/ +theorem JStage3A.cnodeRotateSwA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {ar : machine.Arch A} {nA : U64} {s : S} {a : A} + {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) (hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM) + (ss sp : Fl4SpecA64.CslotPtr) {ps pp : U64} + (hCs : ss.1 = specPtr ps ∧ Z.th ps = some (.cnode ss.2.length)) + (hCp : sp.1 = specPtr pp ∧ Z.th pp = some (.cnode sp.2.length)) + (hsp : cslotAddrA ss ≠ cslotAddrA sp) + (hsn : capA st s (cslotAddrA ss) ≠ .Null) (hpn : capA st s (cslotAddrA sp) ≠ .Null) + (hsx : obj.sext48 (cslotAddrA ss) = ok (cslotAddrA ss)) (hpx : obj.sext48 (cslotAddrA sp) = ok (cslotAddrA sp)) + {np : capability.Cap} {ap : Fl4SpecA64.Cap} (hcp : CapRelA np ap) (hwp : CapRt.EncWf np) (hnp : np ≠ .Null) + (hDp : Fl4ProofsA64.P3.DerivA np (capA st s (cslotAddrA sp))) (halp : UA np (capA st s (cslotAddrA sp))) + (hshp : Fl4ProofsA64.P1.W7.R.ST.shapeA np = Fl4ProofsA64.P1.W7.R.ST.shapeA (capA st s (cslotAddrA sp))) + (hbdp : badgeA np = badgeA (capA st s (cslotAddrA sp))) + (hirqp : ∀ i, np = .IrqHandler i ↔ capA st s (cslotAddrA sp) = .IrqHandler i) + (hop : CapObjOkA Z np) (hokp : CapOkA σ.interruptIrqNode ss ap) (hscp : NoIdleCapV m n ap) + {ns : capability.Cap} {an : Fl4SpecA64.Cap} (hcs : CapRelA ns an) (hws : CapRt.EncWf ns) (hns : ns ≠ .Null) + (hDs : Fl4ProofsA64.P3.DerivA ns (capA st s (cslotAddrA ss))) (hals : UA ns (capA st s (cslotAddrA ss))) + (hshs : Fl4ProofsA64.P1.W7.R.ST.shapeA ns = Fl4ProofsA64.P1.W7.R.ST.shapeA (capA st s (cslotAddrA ss))) + (hbds : badgeA ns = badgeA (capA st s (cslotAddrA ss))) + (hirqs : ∀ i, ns = .IrqHandler i ↔ capA st s (cslotAddrA ss) = .IrqHandler i) + (hos : CapObjOkA Z ns) (hoks : CapOkA σ.interruptIrqNode sp an) (hscs : NoIdleCapV m n an) + (symL : ∀ s2, cdt.swap st (L.wr s (stA (L.curW s)) sched.TS_RESTART) ns (cslotAddrA ss) np (cslotAddrA sp) = + ok s2 → SymL st H Z.Cs s2) + (sysl : ∀ s2, cdt.swap st (L.wr s (stA (L.curW s)) sched.TS_RESTART) ns (cslotAddrA ss) np (cslotAddrA sp) = + ok s2 → SysL st H Z.Cs OS ia ie s2) + (j7 : ∀ x s2 σ2, σ.kheap σ.curThread = some (.tcb x) → + cdt.swap st (L.wr s (stA (L.curW s)) sched.TS_RESTART) ns (cslotAddrA ss) np (cslotAddrA sp) = ok s2 → + ((), σ2) ∈ (Fl4SpecA64.capSwap (Z := Unit) an ss ap sp (stUpdA σ σ.curThread x .restart)).1 → + J7A st (J7R2A st ar nA) { Z with G := swGA Z.G (cslotAddrA ss) (cslotAddrA sp) } H OS s2 σ2 m) : + ∃ x s2 σ2, σ.kheap σ.curThread = some (.tcb x) ∧ + sched.set_thread_state st s (L.curW s) sched.TS_RESTART = ok (L.wr s (stA (L.curW s)) sched.TS_RESTART) ∧ + cdt.swap st (L.wr s (stA (L.curW s)) sched.TS_RESTART) ns (cslotAddrA ss) np (cslotAddrA sp) = ok s2 ∧ + ((), σ2) ∈ (Fl4SpecA64.capSwap (Z := Unit) an ss ap sp (stUpdA σ σ.curThread x .restart)).1 ∧ + JStage3A st AR { Z with G := swGA Z.G (cslotAddrA ss) (cslotAddrA sp) } H OS U Ac Pp Tb Lv ia ie N E Rp Bf + stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s2 a σ2 m ∧ + Sched.BodyKeepA (stUpdA σ σ.curThread x .restart) σ2 := by + have hAs : AbsSlotA Z.th ss := ⟨ps, hCs.1, Or.inl hCs.2⟩ + have hAp : AbsSlotA Z.th sp := ⟨pp, hCp.1, Or.inl hCp.2⟩ + obtain ⟨x, hk, heq, -, J1, -, hOS1, -⟩ := J.of_restartCurA RS.pl.choose_spec.choose_spec.choose_spec.choose_spec + RS.sym RS.run hnvm RS.cur RS.vs RS.vc + generalize hs0 : L.wr s (stA (L.curW s)) sched.TS_RESTART = s0 at heq J1 hOS1 sysl symL j7 ⊢ + generalize hσ0 : stUpdA σ σ.curThread x .restart = σ0 at J1 j7 ⊢ + have hirqN : σ0.interruptIrqNode = σ.interruptIrqNode := by rw [← hσ0]; rfl + have hO : OSOK H.tcbs H.omega H.frame OS := J1.sys.ent.os + have hP := J1.j.1.place + have hsS := hAs.isSlot hP + have hpS := hAp.isSlot hP + have xs : cslotAddrA ss ∈ OS := RS.sub _ hsS + have xp : cslotAddrA sp ∈ OS := RS.sub _ hpS + have cs0 : capA st s0 (cslotAddrA ss) = capA st s (cslotAddrA ss) := hOS1.capA xs + have cp0 : capA st s0 (cslotAddrA sp) = capA st s (cslotAddrA sp) := hOS1.capA xp + rw [← cs0] at hsn hDs hals hshs hbds hirqs + rw [← cp0] at hpn hDp halp hshp hbdp hirqp + have hne : ss ≠ sp := fun e => hsp (by rw [e]) + have E1 := J1.toJEntA + have hTr : TreeA st OS Z.G.chains Z.G.par s0 := + J1.j.1.cdt.tree.grow J1.j5.2.1 (fun x hx hn => J1.j5.2.2 x hx hn) + have hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s0 y = .Null := J1.j5.2.2 + have hoff := footN_offA E1 + have hfal := FootNA.aligned J1.j.1 (bxi_nodes E1.sys.ent.bxi) E1.scAl E1.ap.vcAl + have mv := WP.spec_and + (cap_swap_refinesA s0 σ0 Z.G J1.j.1.st J1.j.1.cdt hP hO hTr J1.j5.2.1 hnul hAs hAp hne hsn hpn hsx hpx + hcs hcp hws hwp hns hnp hals halp) + (J5A.swapA (σ' := σ0) (m := m) (m' := m) hO J1.j5 hTr xs xp hsp hsS hpS hsn hpn hsx hpx hws hwp hshs hbds + hirqs hshp hbdp hirqp (fun s' w i hi => w.mem_eq (hfal _ (.irq i hi)) (hoff _ (.irq i hi)))) + obtain ⟨s2, hs2, ⟨σ2, hm, hst2, hcdt2, -, HS, hcs2⟩, w, e1, e2, ek, -⟩ := spec_existsA mv + have j5' : J5A st { Z with G := swGA Z.G (cslotAddrA ss) (cslotAddrA sp) } H OS s2 σ2 m := by + obtain ⟨s2', hs2', -, -, -, -, J5'⟩ := spec_existsA + (J5A.swapA (σ' := σ2) (m := m) (m' := m) hO J1.j5 hTr xs xp hsp hsS hpS hsn hpn hsx hpx hws hwp hshs hbds + hirqs hshp hbdp hirqp (fun s' w i hi => w.mem_eq (hfal _ (.irq i hi)) (hoff _ (.irq i hi)))) + rw [hs2] at hs2'; have e := okinj hs2'; subst e; exact J5' + have jx : JAtZAX (L := L) AR { Z with G := swGA Z.G (cslotAddrA ss) (cslotAddrA sp) } s2 a σ2 m := by + refine J1.j.of_slotWrites w (fun b hb' hb => hoff b hb hb') hfal _ HS hst2 hcdt2 (fun y hy => ?_) + rcases hcs2 y hy with e | e | e <;> rw [e] + · exact J1.j.capObj hy + · exact hos + · exact hop + have F : CapsFromA st OS s0 s2 := CapsFromA.of_swapA xs xp hDs hDp e1 e2 ek + -- the spec side + obtain ⟨σa, σb, h1, h2, ecdt, eorig, ekh, ems⟩ := capSwap_outA hm + obtain ⟨c1, o1, m1⟩ := setCap_keepsA h1 + obtain ⟨c2, o2, m2⟩ := setCap_keepsA h2 + have W : WfKeepA σ0 σ2 := + ((setCap_wfA _ _ _ _ h1).trans (setCap_wfA _ _ _ _ h2)).trans (WfKeepA.of_kheap ekh) + have hms : σ2.machineState = σ0.machineState := by rw [ems, m2, m1] + have kk : ∀ r : Fl4SpecA64.CslotPtr, (r = ss ∨ r = sp) → σ0.kheap r.1 ≠ none := by + intro r hr + rcases hr with e | e <;> rw [e] + · obtain ⟨cs, hk0, -⟩ := J1.j.1.st.cnode ps _ hCs.2 + rw [hCs.1, hk0]; simp + · obtain ⟨cs, hk0, -⟩ := J1.j.1.st.cnode pp _ hCp.2 + rw [hCp.1, hk0]; simp + have ecd : σ2.cdt = fun r => (σ0.cdt (Equiv.swap ss sp r)).map (Equiv.swap ss sp) := by + rw [ecdt, swap_cdt_eqA _ _ _ hne, c2, c1] + have eor : σ2.isOriginalCap = fun r => σ0.isOriginalCap (Equiv.swap ss sp r) := by + rw [eorig, swap_io_eqA _ _ _ hne, o2, o1] + have ec : ∀ r, σ2.cdt r = (σ0.cdt (Equiv.swap ss sp r)).map (Equiv.swap ss sp) := fun r => congrFun ecd r + have eo : ∀ r, σ2.isOriginalCap r = σ0.isOriginalCap (Equiv.swap ss sp r) := fun r => congrFun eor r + have fix : ∀ r, ¬ (r = ss ∨ r = sp) → Equiv.swap ss sp r = r := fun r hr => + Equiv.swap_apply_of_ne_of_ne (fun e => hr (Or.inl e)) (fun e => hr (Or.inr e)) + have hcdtS : ∀ x y, σ2.cdt x = some y → ∀ sl, sl = x ∨ sl = y → + σ0.kheap sl.1 ≠ none ∨ ∃ x0 y0, σ0.cdt x0 = some y0 ∧ (sl = x0 ∨ sl = y0) := by + intro p q h sl hsl + rw [ec] at h + cases h0 : σ0.cdt (Equiv.swap ss sp p) with + | none => rw [h0] at h; exact absurd h (by simp) + | some y0 => + rw [h0] at h + have ey : Equiv.swap ss sp y0 = q := Option.some.inj h + rcases hsl with e | e + · by_cases hx : p = ss ∨ p = sp + · exact Or.inl (by rw [e]; exact kk _ hx) + · rw [fix p hx] at h0 + exact Or.inr ⟨p, y0, h0, Or.inl e⟩ + · by_cases hy : y0 = ss ∨ y0 = sp + · have hq : q = ss ∨ q = sp := by + rw [← ey] + rcases hy with e' | e' <;> rw [e'] + · rw [Equiv.swap_apply_left]; exact Or.inr rfl + · rw [Equiv.swap_apply_right]; exact Or.inl rfl + exact Or.inl (by rw [e]; exact kk _ hq) + · exact Or.inr ⟨_, y0, h0, Or.inr (by rw [e, ← ey, fix y0 hy])⟩ + have horig : ∀ p, σ2.isOriginalCap p = true → σ0.kheap p.1 ≠ none ∨ σ0.isOriginalCap p = true := fun p h => by + by_cases hx : p = ss ∨ p = sp + · exact Or.inl (kk _ hx) + · rw [eo, fix p hx] at h; exact Or.inr h + have origLive : ∀ sl, σ2.isOriginalCap sl = true → AbsSlotA Z.th sl := fun p h => by + by_cases hx : p = ss ∨ p = sp + · rcases hx with e | e <;> rw [e] + · exact hAs + · exact hAp + · rw [eo, fix p hx] at h; exact J1.origLive p h + have capE : ∀ sl c, specCapAtA σ2 sl = some c → + specCapAtA σ0 sl = some c ∨ (sl = ss ∧ c = ap) ∨ (sl = sp ∧ c = an) := by + refine specCapAtA_orA (Q := fun sl c => (sl = ss ∧ c = ap) ∨ (sl = sp ∧ c = an)) HS (fun p o h => ?_) + rw [ekh] at h + obtain ⟨ob, kb, cb, wb⟩ := setCap_objsA h2 p o h + obtain ⟨oa, ka, ca, wa'⟩ := setCap_objsA h1 p ob kb + refine ⟨oa, ka, fun b c hb => ?_, fun n => (wb n).trans (wa' n)⟩ + rcases cb b c hb with h | h + · exact Or.inl (Or.inr h) + · rcases ca b c h with h | h + · exact Or.inl (Or.inl h) + · exact Or.inr h + have hsc : ∀ sl c, specCapAtA σ2 sl = some c → (∃ sl0, specCapAtA σ0 sl0 = some c) ∨ NoIdleCapV m n c := + fun sl c h => by + rcases capE sl c h with h | ⟨-, e⟩ | ⟨-, e⟩ + · exact Or.inl ⟨sl, h⟩ + · exact Or.inr (e ▸ hscp) + · exact Or.inr (e ▸ hscs) + have nullOrig : ∀ sl, specCapAtA σ2 sl = some .nullCap → σ2.isOriginalCap sl = false := fun sl h => by + by_cases f1 : sl = ss + · exfalso + rw [f1] at h + exact specCap_nnA jx.1.st hAs + (fun h' => hnp ((show capAtA (L.mem s2) (cslotAddrA ss) = np from e1).symm.trans h')) h + by_cases f2 : sl = sp + · exfalso + rw [f2] at h + exact specCap_nnA jx.1.st hAp + (fun h' => hns ((show capAtA (L.mem s2) (cslotAddrA sp) = ns from e2).symm.trans h')) h + have hx : ¬ (sl = ss ∨ sl = sp) := fun h' => h'.elim f1 f2 + rw [eo, fix sl hx] + rcases capE sl _ h with h0 | ⟨e, -⟩ | ⟨e, -⟩ + · exact J1.nullOrig sl h0 + · exact absurd e f1 + · exact absurd e f2 + -- the buffer slots, the tables + have wA : WritesIn st s0 s2 (fun b => Aligned b ∧ W7.CapW H.tcbs {b | InOS OS b} ∅ b) := writesIn_alAG w + have hB : ∀ b, (Aligned b ∧ W7.CapW H.tcbs {b | InOS OS b} ∅ b) → W7.CapW H.tcbs H.omega H.frame b := + fun b hb => os_capw hO hb.1 hb.2 + obtain ⟨-, -, -, -, -, fld, -, -⟩ := W7.blk_of_capw (BXI.blk J1.sys.ent.bxi) wA hB + have sbS : ∀ t ∈ Z.T, sbWordA t ∈ OS := fun t ht => + RS.sub _ ⟨t, Or.inr ⟨(J1.j.1.thT t).2 ht, 2, by simp [tcbSlotsA], (bufSlot_eqA t).symm⟩⟩ + have sbK : ∀ t ∈ Z.T, capA st s2 (sbWordA t) = capA st s0 (sbWordA t) := fun t ht => + ek _ (sbS t ht) (cnSlot_ne_sbA hP hCs.1 hCs.2 ((J1.j.1.thT t).2 ht)) + (cnSlot_ne_sbA hP hCp.1 hCp.2 ((J1.j.1.thT t).2 ht)) + have ib : ∀ t ∈ Z.T, L.mem s2 (ibWordA t) = L.mem s0 (ibWordA t) := + fun t ht => fld t (by rw [J1.tcbs]; exact ht) 3512#u64 (by simp) (by simp) (by simp) + have bufCover : BufCover st Z.T Bf s2 σ2 := BufCover.mapA J1.bufCover J1.bufNative J1.j.1.st jx.1.st + (fun t ht => (J1.j.1.thT t).2 ht) HS ib (fun t ht => by rw [sbK t ht]; exact .refl _) + have bufNative : BufNativeA Z.T (L.mem s2) := BufNativeA.mapA J1.bufNative (fun t ht q sz' rr dv m' e' a' v' h => by + rw [sbK t ht] at h + have hb := J1.bufNative t ht + rw [bufSlot_eqA, capAtA_eq (st := st), h] at hb + exact hb) + have vk : ∀ Pt Ub : Set U64, VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s0) Pt → + VsKeep st (L.ksb s0) Pt s0 s2 := fun Pt Ub vr => + VsKeep.of_w vr.al w (fun b hb => J1.vsr_offCapWA vr hb) + have unmPt : UnmPtSpecA st { Z with G := swGA Z.G (cslotAddrA ss) (cslotAddrA sp) } m OS s2 σ2 := by + obtain ⟨Pt, Ub, vr, -, -, -, -, -, -, link, -, -⟩ := J1.j7 + exact UnmPtSpecA.derivA F HS w.1 w.2.1 (vk Pt Ub vr) link + (fun p h => ⟨fun i hi hf => h.mem i hi (FootA.monoA + (Z := { Z with G := swGA Z.G (cslotAddrA ss) (cslotAddrA sp), S1 := ∅ }) rfl rfl + (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) + (fun _ h => h) (FootA.of_ksA w.1.symm w.2.1.symm hf)), h.obj⟩) J1.unmPt + have bufOff : BufOffA st { Z with G := swGA Z.G (cslotAddrA ss) (cslotAddrA sp) } OS s2 Bf := + ⟨J1.bufOff.os, fun b hb => by rw [w.1]; exact J1.bufOff.vs b hb, + fun b hb q hq => J1.bufOff.page b hb q (SchedPageA.derivBackA F rfl rfl rfl rfl rfl hq), J1.bufOff.vcw, + fun bp hbp => (J1.bufOff.pin bp hbp).keepC rfl rfl fun t ht _ => sbK t ht⟩ + have sinv : SInvXA σ2 m n := + J1.sinv.capSwap (by rw [hirqN]; exact hokp) (by rw [hirqN]; exact hoks) hm + have hm0 : ((), σ2) ∈ (Fl4SpecA64.capSwap (Z := Unit) an ss ap sp (stUpdA σ σ.curThread x .restart)).1 := by + rw [hσ0]; exact hm + have hm' := j7 x s2 σ2 hk hs2 hm0 + refine ⟨x, s2, σ2, hk, heq, hs2, hm0, ?_⟩ + have hfh : ∀ t ∈ Z.T, capA st s2 (slotIdx t layout.tcb.SLOT_FAULT_HANDLER) = + capA st s0 (slotIdx t layout.tcb.SLOT_FAULT_HANDLER) := fun t ht => + ek _ (J1.j5.2.1 _ (isSlot_fhA ((J1.j.1.thT t).2 ht))) + (cnSlot_ne_fhA J1.j.1.place hCs.1 hCs.2 ((J1.j.1.thT t).2 ht)) + (cnSlot_ne_fhA J1.j.1.place hCp.1 hCp.2 ((J1.j.1.thT t).2 ht)) + have hvt : IdleSlotNewA σ0 σ2 m n := IdleSlotNewA.of_cnodeA J1.sidle fun sl c h => by + rcases capE sl c h with h | ⟨e, -⟩ | ⟨e, -⟩ + · exact Or.inl h + · obtain ⟨cs, hk1, -⟩ := J1.j.1.st.cnode ps _ hCs.2 + exact Or.inr ⟨_, cs, by rw [e, hCs.1]; exact hk1⟩ + · obtain ⟨cs, hk1, -⟩ := J1.j.1.st.cnode pp _ hCp.2 + exact Or.inr ⟨_, cs, by rw [e, hCp.1]; exact hk1⟩ + refine ⟨?_, by rw [hσ0]; exact Sched.BodyKeepA.ofSlot HS hms⟩ + exact J1.of_capDerivA (hsub := J1.j5.2.1) (hnul := J1.j5.2.2) (hfh := hfh) (hvt := hvt) + (symL := symL s2 hs2) (G' := swGA Z.G (cslotAddrA ss) (cslotAddrA sp)) w F HS W hms hoff hfal hcdtS horig + origLive nullOrig hsc (sysl s2 hs2) jx sinv bufCover bufNative unmPt bufOff j5' hm' + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3ArmFrMapA.lean b/proofs/Fl4ProofsA64/P3/Stage3ArmFrMapA.lean new file mode 100644 index 000000000..5f454b218 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ArmFrMapA.lean @@ -0,0 +1,347 @@ +/- +P3 (AArch64, merge line, D8 step 5 (ii), the map arms): **`JStage3A` across a successful Stage-1 `ARMPageMap`** +(`JStage3A.frameMapS1A`): the shape lemma (`page_map_shapeA`), the slot-first wrapper `of_capVsFrameA` with the one +descriptor write as its VSpace write set. The final-state facts owed at T3 (`SysL`, `BXI` at some `Ac'`, `UserOK`, +J7) are the arm hypothesis `fin`. No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3BodyKeepA +import Fl4ProofsA64.P3.Stage3MapShapeA +import Fl4ProofsA64.P3.Stage3MapUnmA +import Fl4ProofsA64.P3.VsMapJA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-- `set_cap` has one outcome on the spec side. -/ +theorem setCap_detA {cap : Cap} {sl : CslotPtr} {σ a b : State Unit} + (h1 : ((), a) ∈ (Fl4SpecA64.setCap (Z := Unit) cap sl σ).1) + (h2 : ((), b) ∈ (Fl4SpecA64.setCap (Z := Unit) cap sl σ).1) : a = b := by + obtain ⟨p, cref⟩ := sl + obtain ⟨o, o', k1, c1, e1⟩ := setCap_objA h1 + obtain ⟨q, q', k2, c2, e2⟩ := setCap_objA h2 + rw [k1] at k2 + cases k2 + rw [c1] at c2 + cases c2 + exact e1.trans e2.symm + +set_option maxHeartbeats 8000000 in +/-- **`JStage3A` across a successful Stage-1 `ARMPageMap`** (module docstring). -/ +theorem JStage3A.frameMapS1A {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {ar : machine.Arch A} {nA : U64} {s s3 : S} {a a3 : A} + {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (K : ArchKeepsA ar AR) + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) (hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM) + (cte : Fl4SpecA64.CslotPtr) (hab : AbsSlotA Z.th cte) + {fp asid0 va0 rt : U64} {sz : capability.FrameSize} {dev mp : Bool} + (hcap : capAtA (L.mem s) (cslotAddrA cte) = .Frame fp sz rt dev mp false asid0 va0) + (FP : FrPlA sz fp mp asid0) + (slbl : Fl4SpecA64.Data) (hl : Fl4SpecA64.invocationType slbl = .archInvocationLabel .armPageMap) + (length buf : U64) (args : List U64) (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) (call : Bool) + (extra : Aeneas.Std.Array obj.Slot 3#usize) + (x0 : U64) (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) + (exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) (hex : ExtraRelA Z.th (L.mem s) x0 (exs.map Prod.fst)) + (hS1 : ∀ p l mp' asid va, capAtA (L.mem s) x0 ≠ .Ept p l mp' asid va) + (hHi : ∀ p mp' asid, capAtA (L.mem s) x0 ≠ .HighVSpace p mp' asid) + (hdec : vspace.decode_frame st ar s a fl4_abi.label.ARMPageMap length (cslotAddrA cte) + (.Frame fp sz rt dev mp false asid0 va0) call buf extra = ok (core.result.Result.Ok (), s3, a3)) + (fin : ∀ (x : Fl4SpecA64.Tcb) (σ3 : Fl4SpecA64.State Unit) (iv : Fl4SpecA64.ArchInvocation), + σ.kheap σ.curThread = some (.tcb x) → + (Fl4SpecA64.decodeFrameInvocation (Z := Unit) slbl (args.map UScalar.bv) cte + (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev (mapDataA mp asid0 va0)) exs).run σ = + ({(.ok iv, σ)}, False) → + ∀ ivp, iv = .invokePage ivp → + (([] : List Fl4SpecA64.Data), σ3) ∈ (Fl4SpecA64.performPageInvocation (Z := Unit) ivp + (stUpdA σ σ.curThread x .restart)).1 → + ∃ Ac' : Set U64, SysL st H Z.Cs OS ia ie s3 ∧ SymL st H Z.Cs s3 ∧ + BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac' s3 ∧ + Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s3 ∧ + J7A st (J7R2A st ar nA) Z H OS s3 σ3 m) : + ∃ (x : Fl4SpecA64.Tcb) (ivp : _) (σ3 : Fl4SpecA64.State Unit) (Ac' : Set U64), + σ.kheap σ.curThread = some (.tcb x) ∧ + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + (Fl4SpecA64.decodeFrameInvocation (Z := Unit) slbl (args.map UScalar.bv) cte + (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev (mapDataA mp asid0 va0)) exs).run σ = + ({(.ok (.invokePage ivp), σ)}, False) ∧ + (([] : List Fl4SpecA64.Data), σ3) ∈ (Fl4SpecA64.performPageInvocation (Z := Unit) ivp + (stUpdA σ σ.curThread x .restart)).1 ∧ + JStage3A st AR Z H OS U Ac' Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s3 a3 σ3 m ∧ + Sched.BodyKeepA (stUpdA σ σ.curThread x .restart) σ3 := by + have hx : cslotAddrA cte ∈ OS := RS.sub _ (hab.isSlot J.j.1.place) + have hos := hx + have hO : OSOK H.tcbs H.omega H.frame OS := J.sys.ent.os + have E0 := J.toJEntA + have hSh := page_map_shapeA (L := L) (extra := extra) ar E0 RS cte hab hos hcap FP slbl hl length buf args hlen hargs + call x0 hx0 exs hex hS1 hHi s3 a3 hdec + obtain ⟨hfl, ivp, hD, x', s2, σ2, asid1, va, p, i, e, pte, σp, σ', hk', -, hsc, hσ2, HSs, hEnc, hca, hks2, hcpu2, + R2, hJ2, hFr2, hva, hp, hreach, hi, hk1, hk3', hwr, ⟨pa, sm, att, rg, hpte⟩, hA, hks3, hcpu3, hm3, ⟨t', hσp⟩, hms, + hperf, VR, HS2⟩ := hSh + obtain ⟨x, hk, heq, hset, J1, -, hOS1, -⟩ := J.of_restartCurA RS.pl.choose_spec.choose_spec.choose_spec.choose_spec + RS.sym RS.run hnvm RS.cur RS.vs RS.vc + obtain rfl : x' = x := by + rw [hk] at hk' + injection hk' with h + injection h with h + exact h.symm + have hks : L.ksb (L.wr s (stA (L.curW s)) sched.TS_RESTART) = L.ksb s := L.ksb_wr _ _ _ + have hcpu : L.cpu (L.wr s (stA (L.curW s)) sched.TS_RESTART) = L.cpu s := L.cpu_wr _ _ _ + have hs1m : ∀ b, Aligned b → b ≠ stA (L.curW s) → L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART) b = L.mem s b := + fun b hb hn => by + have : WritesIn st s (L.wr s (stA (L.curW s)) sched.TS_RESTART) (fun w => w = stA (L.curW s)) := by + refine writesIn_wr ?_ + have hc := J.sr.core.tcbs _ J.sr.curT + exact field_aligned hc (by simp) (by simp) + exact this.mem_eq hb hn + generalize L.wr s (stA (L.curW s)) sched.TS_RESTART = s1 at heq J1 hOS1 hks hcpu hs1m hsc + have c0 : capA st s (cslotAddrA cte) = .Frame fp sz rt dev mp false asid0 va0 := hcap + have c1 : capA st s1 (cslotAddrA cte) = .Frame fp sz rt dev mp false asid0 va0 := (hOS1.capA hx).trans c0 + have c1' : capAtA (L.mem s1) (cslotAddrA cte) = .Frame fp sz rt dev mp false asid0 va0 := c1 + have hTr : TreeA st OS Z.G.chains Z.G.par s1 := + J1.j.1.cdt.tree.grow J1.j5.2.1 (fun x hx hn => J1.j5.2.2 x hx hn) + have hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s1 y = .Null := J1.j5.2.2 + have hTr0 : TreeA st OS Z.G.chains Z.G.par s := + J.j.1.cdt.tree.grow J.j5.2.1 (fun x hx hn => J.j5.2.2 x hx hn) + have hnul0 : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null := J.j5.2.2 + have E1 := J1.toJEntA + have hN := bxi_nodes E1.sys.ent.bxi + have hok : CapObjOkA Z (.Frame fp sz rt dev true false asid1 va) := + ⟨fun _ h => (by cases h), fun _ _ _ _ _ h => (by cases h), fun _ _ _ h => (by cases h), + fun _ _ _ h => (by cases h), fun _ _ => hva⟩ + have hM : MapAlikeA (.Frame fp sz rt dev true false asid1 va) (.Frame fp sz rt dev mp false asid0 va0) := .frame .. + have hirq : CapOkA (stUpdA σ σ.curThread x' .restart).interruptIrqNode cte + (.archObjectCap (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev (some (asidOfA asid1, va.bv)))) := by + refine ⟨fun irq hlt e => ?_, fun ⟨_, _, _, _, e⟩ => by cases e⟩ + have obj : ∃ o ac, (stUpdA σ σ.curThread x' .restart).kheap cte.1 = some o ∧ + Fl4SpecA64.capOf o cte.2 = some ac ∧ CapRelA (capAtA (L.mem s1) (cslotAddrA cte)) ac := by + obtain ⟨o, idx⟩ := cte + obtain ⟨p, e1, hc | ⟨ht, i, hi, e2⟩⟩ := hab + · simp only at e1 hc; subst e1 + obtain ⟨cs, hk, -, hr⟩ := J1.j.1.st.cnode p _ hc + obtain ⟨ac, hac, hrel⟩ := hr idx rfl + exact ⟨_, ac, hk, hac, hrel⟩ + · simp only at e1 e2; subst e1 e2 + obtain ⟨y, hk, hr⟩ := J1.j.1.st.tcb p ht + obtain ⟨ac, hac, hrel⟩ := hr i hi + refine ⟨_, ac, hk, hac, ?_⟩ + unfold cslotAddrA; rw [ofBl_tcbIdx i (by unfold tcbSlotsA at hi; omega)]; exact hrel + obtain ⟨o, ac, hko, hco, hrel⟩ := obj + rw [e] at hko + rw [c1'] at hrel + rcases J1.sinv.irqN.slot irq hlt o _ ac hko hco with rfl | ⟨_, _, _, rfl⟩ + · cases hrel.2 + · cases hrel.2 + have hsci : NoIdleCapV m n (.archObjectCap (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev + (some (asidOfA asid1, va.bv)))) := + ⟨fun _ _ e => (by cases e), fun _ e => (by cases e), fun _ _ _ e => (by cases e)⟩ + -- the record across the cap write, from the shape lemma + obtain ⟨J2, hT2⟩ := hJ2 hTr0 hnul0 (fun h => by cases h) (by rw [c0]; exact fun h => by cases h) + (by rw [c0]; exact hM.ua) hok + have al_x : Aligned (cslotAddrA cte) := hO.al0 hx + have al_x8 : Aligned (wa (cslotAddrA cte) 8#u64) := hO.al hx (k := 8#u64) (by simp) (by simp) + have wS := writesIn_setCapA al_x al_x8 hsc + have hal : Aligned (peAF p i) := peAF_alignedA (J.j.1.vs.al_mod hp) hi + have wP : WritesIn st s2 s3 (fun b => b = peAF p i) := by + refine ⟨hks3, hcpu3, fun b hb hn => ?_⟩ + rw [hm3 b hb, L.mem_wr_ne hal hb (fun h => hn (UScalar.eq_of_val_eq h).symm)] + have hmslot : L.mem s3 (peAF p i) = e := by rw [hm3 _ hal, L.mem_wr_same hal] + have hk31 : L.ksb s3 = L.ksb s1 := wP.1.trans wS.1 + have hcpu31 : L.cpu s3 = L.cpu s1 := wP.2.1.trans wS.2.1 + have hk30 : L.ksb s3 = L.ksb s := hk31.trans hks + -- the typing across the leaf write, from `s` + obtain ⟨Pt, Ub, vr, -, -, -, -, -, -, link, -, -, -, -, r2⟩ := J.j7 + obtain ⟨PpJ, TbJ, HrJ, TeJ, KtJ, LvJ, ReJ, bitsJ, R2J, -, -⟩ := r2 + have Psp : ∀ b, b = peAF p i → VsR (L.ksb s) Pt b := fun b hb => by + rw [hb]; exact vsW_pageA link (Or.inr (Or.inl hp)) hi + have vo : VrOK H.omega H.frame H.scs H.scBits OS Z.V ∅ (L.ksb s) (utVsPtA st Pp Tb Lv s) := J.vsOk + have hksF : (L.ksb s).val + 788736 ≤ 2 ^ 64 := by + have := J.sr.ksFit + unfold ksSizeA at this + omega + have hty : VsTyR st (L.ksb s) (PoolSet st Z.Pp s) ∅ Tb Lv s := by rw [J.pp]; exact J.sys.ent.vs + have R0 := J.j.1.vs + have hpa : p.val % 4096 = 0 := R0.al_mod hp + have keep : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → b ≠ peAF p i → L.mem s3 b = L.mem s b := by + intro b hb hne + have hbal := vo.al b hb + have hns : ¬ SlotWordA (cslotAddrA cte) b := fun h => vo.os b hb (slotWord_inOS hO hx h) + have hnst : b ≠ stA (L.curW s) := (RS.vs b hb).2 + exact ((wP.mem_eq hbal hne).trans (wS.mem_eq hbal hns)).trans (hs1m b hbal hnst) + have nPool : ∀ q ∈ PoolSet st Z.Pp s, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → wa q o ≠ peAF p i := by + intro q hq o ho h8 + obtain ⟨j, hj, ej⟩ := peAF_offA q o ho h8 + rw [← ej] + refine peAF_ne_tabA hpa (Nat.mod_eq_zero_of_dvd (R0.pal q hq.1)) hi (by omega) (fun h => ?_) + obtain ⟨f, kf, -⟩ := R0.pool q hq.1 + obtain ⟨tp, kp, -⟩ := R0.pt p hp + rw [h, kp] at kf + cases kf + have hb1 : Z.Lv p < 3 → dBitA e 1 = false := fun hlt => by + rw [hpte] at hwr + have h := hwr.2.2.2.1 + cases hd : dBitA e 1 + · rfl + · have := h.1 hd; omega + have nAs : ¬ AsW (L.ksb s) (peAF p i) := by rw [← hks2]; exact hA + have vsT := VsTyR.leafWrA (Pp := Z.Pp) (Tr := Tb) (Lv := Lv) (s := s) (s' := s3) (b := peAF p i) (w := e) + hty hk30 (by rw [J.pp]; exact keep) hmslot nAs nPool + (leafOkA R0 J.j.1.ext hty hksF hp hreach hi hb1) + rw [J.pp] at vsT + -- J7's reach of the written table + have hrec := recReach_pathA R0 J.j.1.ext hty hksF hreach.choose_spec.choose_spec.2 + have hpools : PoolSet st Z.Pp s ⊆ PoolSet st PpJ s := fun q hq => by + obtain ⟨-, o, h1, h2, h3, h4, h5⟩ := hq + rw [← h4] + exact R2J.core.stage1.tbl o h1 h2 h3 (by rw [h4]; exact h5) + have hJR := vsReach_bridgeA hty R2J.core.stage1 hpools hrec.1 + have hJ : ∀ u ∈ OS, ∀ q a' v, capA st s1 u = .PageTable q false a' v → q ≠ p ∧ q.val % 4096 = 0 := by + intro u hu q a' v hc + rw [hOS1.capA hu] at hc + have hunm : UnmCap (capA st s u) q 4096 := Or.inl ⟨rfl, a', v, hc⟩ + refine ⟨fun h => R2J.core.lookup.unmR1 hu hunm (h ▸ ⟨hJR.2.2.1, hJR.1⟩), ?_⟩ + obtain ⟨z, hz, hH⟩ := R2J.core.lookup.vh.1 u hu q 4096 hunm + have hz12 : z = 12 := Nat.pow_right_injective (le_refl 2) (hz.symm.trans (by norm_num)) + have := J.sys.ent.heap.align q _ hH + rw [hz12] at this + exact this + have main := J1.of_capVsFrameA (hpt := fun _ _ _ h => by cases h) + (Q := fun s' a' σ'' hw => s' = s3 ∧ a' = a3 ∧ σ'' = σ' ∧ hw = Z.hw) + RS.sub (footNOff_ksA rfl rfl (footN_offA E1)) (footNAl_ksA rfl rfl (FootNA.aligned E1.j.1 hN E1.scAl E1.ap.vcAl)) + cte hab hEnc hca hTr hnul (fun h => by cases h) (by rw [c1]; exact fun h => by cases h) + (c := .Frame fp sz rt dev true false asid1 va) (by rw [c1]; exact hM) hok hirq hsci + (fun s2' σ2' hs2 hσ2' HS' w jx hcx => by + obtain rfl : s2' = s2 := okinj (hs2.symm.trans hsc) + obtain rfl : σ2' = σ2 := setCap_detA hσ2' hσ2 + have hAR : AR a3 Z.hw := by + have h0 := FlA.arA K hfl J.j.1.ar + rw [hdec] at h0 + obtain ⟨a', e', h⟩ := (WP.spec_ok _).1 h0 + simp only [Prod.mk.injEq, true_and] at e' + rw [e']; exact h + have J3 : JAtZAX (L := L) AR Z s3 a3 σ' m := by + have := map_finJA (S1' := Z.S1) (Lv' := Z.Lv) E0 J2 hT2 hks2 hcpu2 hp (J.tabS1 p hp) hi hks3 hcpu3 + (fun b hb hne => wP.mem_eq hb hne) (hσp ▸ hms) (fun q hq => Or.inl hq) (fun _ hq hl => ⟨hq, hl⟩) VR hFr2 hos + exact this.1.of_arA hAR + have eσ : σ'.kheap = σp.kheap := by + obtain ⟨ms, h⟩ := hms + rw [h] + have hset' : ∀ q ao, q ≠ specPtr p → (stUpdA σ σ.curThread x' .restart).kheap q = some (.archObj ao) → + σ'.kheap q = some (.archObj ao) := fun q ao hq h => by + rw [eσ, hσp, ptSetA_kheap, if_neg hq] + exact HSs.keep h (fun _ _ e => by cases e) (fun _ e => by cases e) + obtain ⟨Ac', hsl, hsy, hbx, hus, hj7⟩ := fin x' σ' _ hk hD ivp rfl hperf + have osk3 : OSKeep st OS s2' s3 := osKeep_ofA hO fun b hb hin => + wP.mem_eq hb (fun h => vr.os b (Psp b h) hin) + obtain ⟨hcx', hco⟩ := capA_setCapA hO hx hEnc hs2 + have capE3 : ∀ u ∈ OS, capA st s3 u = capA st s2' u := fun u hu => osk3.capA hu + have hbk : ∀ u ∈ OS, ∀ c, capA st s3 u = c → (∀ p sz r d mp e a v, c ≠ .Frame p sz r d mp e a v) → + capA st s1 u = c := fun u hu c hc hn => by + by_cases e : u = cslotAddrA cte + · subst e + rw [capE3 _ hx, hcx] at hc + exact absurd hc.symm (hn _ _ _ _ _ _ _ _) + · rw [capE3 u hu, hco u hu e] at hc + exact hc + have hMs2 : ∀ u ∈ OS, MapAlikeA (capA st s2' u) (capA st s1 u) := fun u hu => by + by_cases e : u = cslotAddrA cte + · subst e; rw [hcx, c1]; exact hM + · rw [hco u hu e]; exact .refl _ + have sbS : ∀ t ∈ Z.T, sbWordA t ∈ OS := fun t ht => + RS.sub _ ⟨t, Or.inr ⟨(J1.j.1.thT t).2 ht, 2, by simp [tcbSlotsA], (bufSlot_eqA t).symm⟩⟩ + have wA : WritesIn st s1 s2' (fun b => Aligned b ∧ W7.CapW H.tcbs {b | InOS OS b} ∅ b) := writesIn_alAG w + have hB : ∀ b, (Aligned b ∧ W7.CapW H.tcbs {b | InOS OS b} ∅ b) → W7.CapW H.tcbs H.omega H.frame b := + fun b hb => os_capw hO hb.1 hb.2 + obtain ⟨-, -, -, -, -, fld, -, -⟩ := W7.blk_of_capw (BXI.blk J1.sys.ent.bxi) wA hB + have ib : ∀ t ∈ Z.T, L.mem s2' (ibWordA t) = L.mem s1 (ibWordA t) := fun t ht => + fld t (by rw [J1.tcbs]; exact ht) 3512#u64 (by simp) (by simp) (by simp) + obtain ⟨Qb, Rb, q⟩ := J.sys.ent.bxi.1 + have tw : ∀ t ∈ Z.T, ∀ K : U64, K.val + 8 ≤ 4096 → K.val % 8 = 0 → + L.mem s3 (wa t K) = L.mem s2' (wa t K) := by + intro t ht K hK' h8 + have hta := J1.sr.core.tcbs t ht + have hv : (wa t K).val = t.val + K.val := field_val hta (K := K) (by omega) + refine wP.mem_eq (field_aligned hta (by omega) h8) (fun h => ?_) + have hth : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + have := (q.omega _ (vr.om _ (Psp _ h))).2.1 t hth + omega + have hib : ∀ t ∈ Z.T, IbSame (L := L) s2' s3 t := fun t ht => by + refine ⟨tw t ht _ (by simp) (by simp), ?_, ?_⟩ + · rw [sbWordA_eq]; exact tw t ht _ (by simp) (by simp) + · rw [sbWordA8_eq]; exact tw t ht _ (by simp) (by simp) + have hj7' := hj7 + obtain ⟨Pt', Ub', vr', -, ac', -⟩ := hj7' + have hBw : ∀ b, (SlotWordA (cslotAddrA cte) b ∨ b = peAF p i) → + L.Free s1 H.omega Z.Cs b ∨ W7.TLow H.tcbs b := fun b hb => by + rcases hb with hsl' | hb + · have hal' : Aligned b := by rcases hsl' with rfl | rfl; exact al_x; exact al_x8 + exact (os_capw hO hal' (Or.inr ⟨slotWord_inOS hO hx hsl', Set.notMem_empty _⟩)).elim Or.inr + fun h' => Or.inl (Or.inl h'.1) + · exact Or.inl (Or.inl (vr.om b (Psp b hb))) + have hsp : ∀ q', SchedPageA st Z OS s3 q' → SchedPageA st Z OS s1 q' := fun q' h => SchedPageA.backA hbk h + have link1 : ∀ q', SchedPageA st Z OS s1 q' → q' ∈ Pt := fun q' h => + link q' (SchedPageA.backA (fun u hu c hc _ => (hOS1.capA hu).symm.trans hc) h) + have hobj : ∀ u ∈ OS, CapObjOkA Z (capA st s1 u) := fun u hu => by + by_cases h : IsSlotA Z.th u + · exact J1.j.capObj h + · rw [hnul u hu h]; exact CapObjOkA.null Z + have R1 := J1.j.1.vs + refine ⟨s3, a3, σ', Z.hw, fun b => b = peAF p i, Ac', + ⟨wP, fun Pt'' Ub'' vr'' link'' b h => by rw [h]; exact vsW_pageA link'' (Or.inr (Or.inl hp)) hi, + fun b h hbuf => J1.bufOff.page b hbuf p (Or.inr (Or.inl hp)) + (core.num.U64.wrapping_mul i 8#u64) (by rw [wm8_valA hi]; omega) (by rw [wm8_valA hi]; omega) + (by rw [h]; rfl), + HS2, ?_, ?_, J3, ⟨⟨J1.sys.ent.heap, hbx, hO, hus, vsT.1, by rw [hk31]; exact J1.sys.ent.ksw⟩, hsl⟩, + UtVsOkA.zeroStepA hk30 vsT.2 J.vsOk, bufVs_subA J.bufVs hk30 vsT.2, + W7.inv_of_capw J1.sq (wS.trans wP) hBw, ac'.case.po, + ?_, ?_, ?_, ?_, hj7, hsy⟩, rfl, rfl, rfl, rfl⟩ + · obtain ⟨ms, h⟩ := hms + rw [h, hσp]; rfl + · intro q' dv pg h + refine (by + rw [eσ, hσp, ptSetA_kheap, if_neg (fun e => ?_)] + · exact h + · obtain ⟨tp, kp, -⟩ := R2.pt p hp + rw [e, kp] at h + cases h) + · exact BufCover.vsA (BufCover.mapA J1.bufCover J1.bufNative J1.j.1.st J2.1.st (fun t ht => (J1.j.1.thT t).2 ht) + HSs ib (fun t ht => hMs2 _ (sbS t ht))) HS2 hib + · exact BufNativeA.mapA J1.bufNative (fun t ht q' sz' rr dv' m' e' a'' v' h => by + by_cases e1 : sbWordA t = cslotAddrA cte + · rw [e1, capE3 _ hx, hcx] at h; cases h; rfl + · rw [capE3 _ (sbS t ht), hco _ (sbS t ht) e1] at h + have hn := J1.bufNative t ht + rw [bufSlot_eqA, capAtA_eq (st := st), h] at hn + exact hn) + · refine UnmPtSpecA.ofLeafWrA (Pt := Pt) (p := p) (i := i) hpa hi hp hk1 + (fun h => (J.j.1.ext.pgd p h).1 (Or.inr (Or.inl hp))) R1.ytb R1.yal J1.j.1.ext.xal + (fun q' hq => R1.al_mod hq) hobj hJ hbk (fun q' hq j hj hne => ?_) link1 hk31 hcpu31 hset' + (by obtain ⟨tp, kp, -⟩ := R1.pt p hp; exact ⟨tp, kp⟩) J1.unmPt + have hv : VsR (L.ksb s) Pt (peAF q' j) := Or.inl ⟨q', hq, core.num.U64.wrapping_mul j 8#u64, + (wm8_offA hj).1, (wm8_offA hj).2, rfl⟩ + have hbal : Aligned (peAF q' j) := vr.al _ hv + have hns : ¬ SlotWordA (cslotAddrA cte) (peAF q' j) := fun h => vr.os _ hv (slotWord_inOS hO hx h) + exact (wP.mem_eq hbal hne).trans (wS.mem_eq hbal hns) + · exact ⟨J1.bufOff.os, fun b hb => by rw [hk31]; exact J1.bufOff.vs b hb, + fun b hb q' hq => J1.bufOff.page b hb q' (hsp q' hq), J1.bufOff.vcw, + fun bp hbp => (J1.bufOff.pin bp hbp).keepM fun u hu => by rw [capE3 u hu]; exact hMs2 u hu⟩) + obtain ⟨_, _, s3', a3', σ3', hw, P, Ac', -, hσk, HSsk, HSvk, -, -, Jf, e1, e2, e3, e4⟩ := main + subst e1 e2 e3 e4 + exact ⟨x', ivp, σ3', Ac', hk, hset, hD, hperf, Jf, (Sched.BodyKeepA.ofSlot HSsk (setCap_keepsA hσk).2.2).trans (Sched.BodyKeepA.ofVs HSvk)⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3ArmFrMapHiA.lean b/proofs/Fl4ProofsA64/P3/Stage3ArmFrMapHiA.lean new file mode 100644 index 000000000..5709eb085 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ArmFrMapHiA.lean @@ -0,0 +1,485 @@ +/- +P3 (AArch64, merge line, D8 step 5 (ii), the map arms): **`JStage3A` across a successful high-window `ARMPageMap`** +(`JStage3A.frameMapHiA`, at `vspace::decode_high_page_map`): `hi_map_invA`, the relation after the write (as in +`hi_map_frame_jA`'s finish), P1's `vspace.decode_high_page_map.exactEffect` and `frameMapExtCoreA`. Decode-site +hypotheses: the mapped `ArmHighVSpace` extra cap in an `OS` slot, `DecPre` (T3 adapter), [P] `P0.ArchTotal`. The +final-state facts owed at T3 are `fin`. No named [J]. New module. +-/ +import Fl4ProofsA64.P3.Stage3BodyKeepA +import Fl4ProofsA64.P3.Stage3MapExtCoreA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +private theorem hiTail_memA {B : Prop} [Decidable B] {e : SyscallError} {r : Fl4SpecA64.ObjRef} + {va : Fl4SpecA64.VSpaceRef} {sh : Nat} {cap : ArchCap} {cte : CslotPtr} {pte : PTE} {σ σ' : State Unit} + {x : ExtVSpaceInvocation} + (h : (Except.ok x, σ') ∈ (((do + NonDetE.whenE B (NonDetE.throwError e) + let pos ← NonDetE.liftE (NonDet.gets fun s : State Unit => extLookupSlot s highGeom r va) + NonDetE.unlessE (pos.shift = sh) + (NonDetE.throwError (.failedLookup false (.missingCapability pos.shift))) + NonDetE.returnOk (.map cap cte pte pos.slot)) : SEMonad Unit ExtVSpaceInvocation).run σ).1) : + ¬ B ∧ x = .map cap cte pte (extLookupSlot σ highGeom r va).slot := by + obtain ⟨_, s1, h1, g1⟩ := mem_bind_invD h + have hB : ¬ B := fun b => by + unfold NonDetE.whenE at h1 + rw [if_pos b] at h1 + have := Set.mem_singleton_iff.1 h1 + simp at this + obtain rfl := mem_when_invD h1 + obtain ⟨pos, s2, h2, g2⟩ := mem_bind_invD g1 + obtain ⟨e2', rfl⟩ := mem_gets_invD h2 + subst e2' + obtain ⟨_, s3, h3, g3⟩ := mem_bind_invD g2 + obtain rfl := mem_unless_invD h3 + exact ⟨hB, Except.ok.inj (Prod.mk.inj (Set.mem_singleton_iff.1 g3)).1⟩ + +/-- **The successful outcome of `decodeHighPageMap`**: a `map` of the looked-up slot. -/ +theorem hiMap_memA {cte : CslotPtr} {p : Fl4SpecA64.ObjRef} {R : CapRights} {sz : VMPageSize} {dev : Bool} + {mapped : Option (Asid × Fl4SpecA64.VSpaceRef)} {vcap : Fl4SpecA64.Cap} {r : Fl4SpecA64.ObjRef} {asid : Asid} + {va rm attr : Data} {σ σ' : State Unit} {iv : ExtVSpaceInvocation} + (hchk : (checkExtRoot (Z := Unit) .high vcap 1).run σ = ({(.ok (r, asid), σ)}, False)) + (h : (Except.ok iv, σ') ∈ + ((decodeHighPageMap (Z := Unit) cte (.frameCap p R sz dev mapped) vcap va rm attr).run σ).1) : + ¬ (va < highVSpaceBase ∨ va + mask (pageBitsForSize sz) ≥ highVSpaceTop ∨ va + mask (pageBitsForSize sz) < va) ∧ + iv = .map (.frameCap p R sz dev (some (asid, va))) cte + (makeUserPte (addrFromPPtr p) (attribsFromWord attr) (maskVmRights R (dataToRights rm)) R sz) + (extLookupSlot σ highGeom r va).slot := by + unfold decodeHighPageMap at h + dsimp only at h + rw [run_bind_okA hchk] at h + obtain ⟨u0, s0, h0, g⟩ := mem_bind_invD h + clear h hchk + obtain rfl := checkAlign_keepA h0 + cases mapped with + | some w => + obtain ⟨asid', va'⟩ := w + dsimp only at g + obtain ⟨u1, s1, h1, g1⟩ := mem_bind_invD g + obtain rfl := mem_when_invD h1 + obtain ⟨u2, s2, h2, g2⟩ := mem_bind_invD g1 + obtain rfl := mem_when_invD h2 + exact hiTail_memA g2 + | none => + dsimp only at g + first + | exact hiTail_memA g + | (obtain ⟨u1, s1, h1, g1⟩ := mem_bind_invD g + obtain rfl : s1 = _ := (Prod.mk.inj (Set.mem_singleton_iff.1 h1)).2 + exact hiTail_memA g1) + +private theorem hiSlotOkA {Pp Tb X Y : Set U64} {Lv : U64 → Nat} {mem : U64 → U64} {ksb : U64} + {σ : State Unit} (R : VsRelA Pp Tb Lv X Y mem ksb σ) {x : U64} {sl : ExtSlot} {sh : Nat} + (h : SlotRelE (G1E Tb Lv) (TopHiE Y) x sl sh) : ExtSlotOkX σ sl := by + rcases h with ⟨t, i, ⟨ht, h1, -⟩, hi, rfl, rfl⟩ | ⟨y, j, hy, hj, rfl, rfl, -⟩ + · obtain ⟨pt, hk, hty, -⟩ := R.pt t ht + have hty' : ptType pt = .normalPtT := by rw [hty]; unfold ptTyA; rw [if_neg (by omega)] + cases pt with + | vsRootPT _ => cases hty' + | normalPT f => + obtain ⟨hal, hb, -⟩ := slot_geomA .normalPtT (R.al_mod ht) hi + exact ⟨hal, f, by rw [hb]; exact hk⟩ + · obtain ⟨l1, hk, -⟩ := R.high y hy + exact ⟨.highVSpace l1, extObjAt_someX.2 hk⟩ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +set_option maxHeartbeats 8000000 in +/-- **`JStage3A` across a successful high-window `ARMPageMap`** (module docstring). -/ +theorem JStage3A.frameMapHiA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {ar : machine.Arch A} {nA : U64} {s : S} {a : A} + {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (K : ArchKeepsA ar AR) (hA : Fl4ProofsA64.P0.ArchTotal ar) + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) (hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM) + (hTO : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s q) (hEO : ExtTabOffA (L := L) Z m s) + (hK : Aligned (L.ksb s)) (hT : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s)) + (cte : Fl4SpecA64.CslotPtr) (hab : AbsSlotA Z.th cte) (fp rt asid va : U64) (sz : capability.FrameSize) + (dev mp : Bool) (hcap : capAtA (L.mem s) (cslotAddrA cte) = .Frame fp sz rt dev mp false asid va) + (FP : FrPlA sz fp mp asid) (p1 asid1 : U64) (mp1 : Bool) (h16 : asid1.val < 2 ^ 16) + {x0 : U64} (hx0 : x0 ∈ OS) (hx0c : capA st s x0 = .HighVSpace p1 mp1 asid1) + (vaddr rightsMask vm attr : U64) + (hvmEq : vmRightsOfA vm = maskVmRights (vmRightsOfA rt) (dataToRights rightsMask.bv)) + {D : Set (Except SyscallError Fl4ArchInvocation × State Unit) × Prop} + (hD : D = (NonDetE.liftME Fl4ArchInvocation.extVSpace + (decodeHighPageMap (Z := Unit) cte (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev + (mapDataA mp asid va)) (extRootCapA .high (specPtr p1) (asidDataA mp1 asid1)) + vaddr.bv rightsMask.bv attr.bv)).run σ) + {dc : capability.Cap} {ex : Aeneas.Std.Array obj.Slot 3#usize} + (d : DecPre st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits U Ac s dc ex) + {s3 : S} {a3 : A} + (hres : vspace.decode_high_page_map st ar s a (cslotAddrA cte) + { ptr := fp, size := sz, rights := rt, is_device := dev, mapped := mp, is_ept := false, asid := asid, vaddr := va } + (.HighVSpace p1 mp1 asid1) vaddr vm attr = ok (core.result.Result.Ok (), s3, a3)) + (fin : ∀ (x : Fl4SpecA64.Tcb) (iv : ExtVSpaceInvocation) (σ3 : Fl4SpecA64.State Unit), + σ.kheap σ.curThread = some (.tcb x) → (.ok (.extVSpace iv), σ) ∈ D.1 → + (([] : List Fl4SpecA64.Data), σ3) ∈ + (Fl4SpecA64.performExtVSpace (Z := Unit) iv (stUpdA σ σ.curThread x .restart)).1 → + ∃ Ac' : Set U64, SysL st H Z.Cs OS ia ie s3 ∧ SymL st H Z.Cs s3 ∧ + BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac' s3 ∧ + Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s3 ∧ + J7A st (J7R2A st ar nA) Z H OS s3 σ3 m) : + ∃ (x : Fl4SpecA64.Tcb) (iv : ExtVSpaceInvocation) (σ3 : Fl4SpecA64.State Unit) (Ac' : Set U64), + σ.kheap σ.curThread = some (.tcb x) ∧ + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + (.ok (.extVSpace iv), σ) ∈ D.1 ∧ + (([] : List Fl4SpecA64.Data), σ3) ∈ + (Fl4SpecA64.performExtVSpace (Z := Unit) iv (stUpdA σ σ.curThread x .restart)).1 ∧ + JStage3A st AR Z H OS U Ac' Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s3 a3 σ3 m ∧ + Sched.BodyKeepA (stUpdA σ σ.curThread x .restart) σ3 := by + have E0 := J.toJEntA + have hos : cslotAddrA cte ∈ OS := RS.sub _ (hab.isSlot J.j.1.place) + have hTr : TreeA st OS Z.G.chains Z.G.par s := J.j.1.cdt.tree.grow J.j5.2.1 (fun x hx hn => J.j5.2.2 x hx hn) + have hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null := J.j5.2.2 + have hcap0 : capA st s (cslotAddrA cte) = .Frame fp sz rt dev mp false asid va := by rw [← capAtA_eq]; exact hcap + obtain ⟨c0', -, hrel0⟩ := StateRelA.getAbsA E0.j.1.st hab + rw [hcap] at hrel0 + obtain ⟨⟨hr1, hr3, ⟨has16, -, hmz1, _⟩, hov⟩, -⟩ := hrel0 + have hcanP : CapRt.Canonical fp := by + have := CapRt.decodeF_encWf (L.mem s (cslotAddrA cte)).bv (L.mem s (wa (cslotAddrA cte) 8#u64)).bv + change CapRt.EncWf (capAtA (L.mem s) (cslotAddrA cte)) at this + rw [hcap] at this + exact this.1 + -- the code + obtain ⟨root, asid', shift, lu, cr, pa, ew, s1, s2', hchkv, hsh, hlu, hlsh, hcr, hpa, hew, hst1, hsc1, hinst⟩ := + hi_map_invA hres + rw [page_shift_eqA] at hsh + obtain rfl := ok_inj_ff hsh + rw [rights_readableA] at hcr + have ecr := ok_inj_ff hcr + have epa : core.num.U64.wrapping_sub fp layout.PPTR_BASE = pa := ok_inj_ff hpa + have hX : ∀ r ∈ Z.X, ∃ es, σ.kheap (specPtr r) = some (.archObj (.ext (.stage2Root es))) := + fun r hr => let ⟨es, hk, _⟩ := E0.j.1.ext.s2root r hr; ⟨es, hk⟩ + have hc0 := check_high_vspaceA E0.j.1.vs hX p1 asid1 1#u64 mp1 (lt_of_lt_of_eq h16 (by norm_num)) + rw [hchkv] at hc0 + obtain ⟨hv, hrY, has1nz, hchk⟩ := (WP.spec_ok _).1 hc0 + simp only [Prod.mk.injEq] at hv + obtain ⟨e1, e2⟩ := hv + subst root asid' + have hmp1 : mp1 = true := by + cases mp1 + · simp [vspace.check_high_vspace, error.invalid_cap] at hchkv + · rfl + -- the spec decode + have h3 := hi_map_frame_jA K E0 RS hTO hEO hK hT hTr hnul cte hab hos fp rt asid va sz dev mp hcap FP p1 asid1 mp1 + h16 vaddr rightsMask vm attr hvmEq hD + rw [hres] at h3 + obtain ⟨inv, ⟨hmem, -⟩, -⟩ := (WP.spec_ok _).1 h3 + have hmem0 := hmem + rw [hD] at hmem + unfold NonDetE.liftME at hmem + obtain ⟨iv, s4, hm1, hm2⟩ := mem_bind_invD hmem + have einv : inv = .extVSpace iv := Except.ok.inj (Prod.mk.inj (Set.mem_singleton_iff.1 hm2)).1 + have hiv := hiMap_memA hchk hm1 + -- the lookup + have hactY := activeHighRootA_of_vspaceForAsid E0.j.1.vs hX hrY (vspaceForAsid_of_checkExtRootA (by decide) hchk) + obtain ⟨hbnd, hiv⟩ := hiv + have hbase : 18446743936270598144 ≤ vaddr.val := by + have : ¬ vaddr.bv < highVSpaceBase := fun h => hbnd (Or.inl h) + have e : (highVSpaceBase : BitVec 64).toNat = 18446743936270598144 := by unfold highVSpaceBase; decide + have := BitVec.le_def.1 (BitVec.not_lt.1 this) + rw [e] at this + exact this + have htop : vaddr.val < 18446744056529682432 := by + have a1 := Nat.lt_of_not_le fun h => hbnd (Or.inr (Or.inl (BitVec.le_def.2 h))) + have a2 := Nat.le_of_not_lt fun h => hbnd (Or.inr (Or.inr (BitVec.lt_def.2 h))) + have h1 : vaddr.bv < highVSpaceTop := BitVec.lt_def.2 (Nat.lt_of_le_of_lt a2 a1) + have e : (highVSpaceTop : BitVec 64).toNat = 18446744056529682432 := by unfold highVSpaceTop; decide + have := BitVec.lt_def.1 h1 + rw [e] at this + exact this + have h39 : idxA vaddr 39#u64 = 511#u64 := by + apply UScalar.eq_of_val_eq + have h2 : vaddr.val < 2 ^ 64 := vaddr.bv.isLt + rw [idxA_val] + show (vaddr.val / 2 ^ 39) % 512 = 511 + omega + have hcanV : CapRt.Canonical vaddr := by + right + intro j hj hj47 + show vaddr.val.testBit j = true + have h2 : vaddr.val < 2 ^ 64 := vaddr.bv.isLt + have : vaddr.val / 2 ^ j = 2 ^ (64 - j) - 1 := by + have hle : (2 ^ (64 - j) - 1) * 2 ^ j = 2 ^ 64 - 2 ^ j := by + rw [Nat.sub_mul, Nat.one_mul, ← Nat.pow_add, Nat.sub_add_cancel (by omega)] + have h47 : 2 ^ 47 ≤ 2 ^ j := Nat.pow_le_pow_right (by norm_num) hj47 + have a1 : 2 ^ (64 - j) - 1 ≤ vaddr.val / 2 ^ j := + (Nat.le_div_iff_mul_le (Nat.two_pow_pos j)).2 (by omega) + have a2 : vaddr.val / 2 ^ j < 2 ^ (64 - j) := by + rw [Nat.div_lt_iff_lt_mul (Nat.two_pow_pos j), ← Nat.pow_add, Nat.sub_add_cancel (by omega)] + exact h2 + omega + rw [Nat.testBit_eq_decide_div_mod_eq, this] + obtain ⟨k, hk⟩ : ∃ k, 64 - j = k + 1 := ⟨63 - j, by omega⟩ + have : 1 ≤ 2 ^ k := Nat.one_le_two_pow + rw [hk, Nat.pow_succ] + simp only [decide_eq_true_eq] + omega + have hc1 := lookup_pt_slot_hiA E0.j.1.vs E0.j.1.ext hrY hactY h39 + rw [hlu] at hc1 + obtain ⟨-, hslR⟩ := (WP.spec_ok _).1 hc1 + -- the restart, the cap write and the install + have hEnc : CapRt.EncWf (.Frame fp sz rt dev true false asid1 vaddr) := by + simp only [CapRt.EncWf]; exact ⟨hcanP, by omega, by omega, hcanV⟩ + have hca : CapRelA (.Frame fp sz rt dev true false asid1 vaddr) + (.archObjectCap (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev + (some (asidOfA asid1, vaddr.bv)))) := + ⟨⟨hr1, hr3, ⟨h16, by simp, fun _ => has1nz, by simp⟩, hov⟩, rfl⟩ + have hok : CapObjOkA Z (.Frame fp sz rt dev true false asid1 vaddr) := + ⟨fun _ h => (by cases h), fun _ _ _ _ _ h => (by cases h), fun _ _ _ h => (by cases h), + fun _ _ _ h => (by cases h), fun _ _ => Or.inr ⟨hbase, htop⟩⟩ + obtain ⟨x, s2, σ2, hk, hst, hset, hsc, hσ2, -, hksb2, -, hcpu2, -, hJ2, -⟩ := + restart_setcapJA E0 RS cte hab hos hEnc hca + have es1 := ok_inj_ff (hst.symm.trans hst1) + subst es1 + have es2 := ok_inj_ff (hsc.symm.trans hsc1) + subst es2 + obtain ⟨J2, -⟩ := hJ2 hTr hnul (by intro h; cases h) (by rw [hcap0]; intro h; cases h) + (by rw [hcap0]; exact UA.of_shapeA rfl (by simp [isUtA])) hok + have hSlotAl : Aligned lu.slot := by + rcases hslR with ⟨t, i, ⟨⟨ht, -⟩, -⟩, hi, hluEq, -⟩ | ⟨y, j, hy, -, hj, hluEq, -, -⟩ + · rw [hluEq]; exact peAF_alignedA (E0.j.1.vs.al_mod ht) hi + · rw [hluEq]; exact peAF_alignedA (waWin_modA (E0.j.1.vs.yal y hy)) hj + have hI := install_page_hiA (st := st) (s := s2) K J2.1.ar lu ew 0#u64 asid1 vaddr attr + { ptr := fp, size := sz, rights := rt, is_device := dev, mapped := mp, is_ept := false, asid := asid, vaddr := va } + hSlotAl + rw [hinst] at hI + obtain ⟨hW, hAR'⟩ := (WP.spec_ok _).1 hI + have hfa2 : ∀ w, FootA (L := L) Z s2 w → Aligned w := + FootA.aligned J2.1 (fun c hc o h1 h2 => by rw [nodeAt_ksb hksb2]; exact bxi_nodes E0.sys.ent.bxi c hc o h1 h2) + E0.scAl E0.ap.vcAl + have hEO2 : ExtTabOffA (L := L) Z m s2 := hEO.of_ksA hksb2 hcpu2 + have hTO2 : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s2 q := fun q hq => (hTO q hq).of_ksA hksb2 hcpu2 + have hK2 : Aligned (L.ksb s2) := hksb2 ▸ hK + have hT2 : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s2) := hksb2 ▸ hT + obtain ⟨pte, epte⟩ : ∃ pte, pte = makeUserPte (addrFromPPtr (specPtr fp)) (attribsFromWord attr.bv) + (vmRightsOfA vm) (vmRightsOfA rt) (frameSizeOfA sz) := ⟨_, rfl⟩ + have einv' : iv = .map (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev + (some (asidOfA asid1, vaddr.bv))) cte pte (extLookupSlot σ highGeom (specPtr p1) vaddr.bv).slot := by + rw [hiv, epte, hvmEq] + have hnotPt : ∀ ppn, pte ≠ .pageTablePTE ppn := fun ppn h => by rw [epte] at h; unfold makeUserPte at h; cases h + have hpaSpec : (core.num.U64.wrapping_sub fp layout.PPTR_BASE).bv = addrFromPPtr (specPtr fp) := + (addr_specA fp).symm + have hm := mup_specA pa vm attr (psA sz) cr + rw [hew] at hm + have hewv := (WP.spec_ok _).1 hm + have hPteAt (k : Nat) (hk1 : 0 < k) (hk3 : k ≤ 3) (hshk : shiftA k = (psA sz).val) : + PteRelA Z.S1 Z.Lv k ew pte := by + have hkEq : decide (k = 3) = decide (psA sz = 12#u64) := by + rcases (show k = 1 ∨ k = 2 ∨ k = 3 by omega) with rfl | rfl | rfl <;> + cases sz <;> simp [shiftA, psA] at hshk ⊢ + have hsmEq : decide (frameSizeOfA sz = .armSmallPage) = decide (k = 3) := by + rcases (show k = 1 ∨ k = 2 ∨ k = 3 by omega) with rfl | rfl | rfl <;> + cases sz <;> simp [shiftA, psA, frameSizeOfA] at hshk ⊢ + have h1 := pteRel_mupA (Tb := Z.S1) (Lv := Z.Lv) hk1 hk3 (R := vmRightsOfA rt) ecr.symm + (by rw [hkEq, epa]; exact hewv) (by rw [hshk]; exact FP.al) FP.pa + rw [epte] + unfold makeUserPte + rw [hsmEq, ← hpaSpec] + exact h1 + have hsh1 : lu.shift.val = (psA sz).val := by rw [hlsh] + have hsh2 : (extLookupSlot σ highGeom (specPtr p1) vaddr.bv).shift = lu.shift.val := by + have hc2 := lookup_pt_slot_hiA E0.j.1.vs E0.j.1.ext hrY hactY h39 + rw [hlu] at hc2 + exact ((WP.spec_ok _).1 hc2).1.symm + -- J7's exact frame map and the unmapped-root uniqueness + have ev : ∀ (Pt Pp' Tb' Hr Te Kt : Set U64) (Lv' Re bits : U64 → Nat), + S1aR2 st H OS Pt Pp' Tb' Hr Te Kt Lv' Re bits nA s → + (Ub : Set U64) → VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt → + s3 = s ∨ ∃ tag f, S1aFrameMap st OS Pt Pp' nA tag s s3 (cslotAddrA cte) f := by + intro Pt Pp' Tb' Hr Te Kt Lv' Re bits R2 Ub vr + have h := vspace.decode_high_page_map.exactEffect (ar := ar) (a := a) hA R2 vr J.sys.ent.os hos d + { ptr := fp, size := sz, rights := rt, is_device := dev, mapped := mp, is_ept := false, asid := asid, vaddr := va } + hcanP (by show rt.val < 4; omega) (fun sh hsh => by + rw [show capability.FrameSize.bits sz = ok (psA sz) by cases sz <;> rfl] at hsh + obtain rfl := ok_inj_ff hsh + have hal := FP.al + have h12 : (psA sz).val = 12 ∨ (psA sz).val = 21 ∨ (psA sz).val = 30 := psA_valA sz + dsimp only + rcases h12 with h | h | h <;> rw [h, ws_valA] at hal <;> rw [h] <;> omega) (fun _ => rfl) hx0 (.HighVSpace p1 mp1 asid1) hx0c vaddr vm attr + rw [hres] at h + rcases (WP.spec_ok _).1 h with e | e + · exact Or.inl e + · exact Or.inr ⟨_, _, e⟩ + have fin' : ∃ Ac' : Set U64, SysL st H Z.Cs OS ia ie s3 ∧ SymL st H Z.Cs s3 ∧ + BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac' s3 ∧ + Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s3 ∧ + J7A st (J7R2A st ar nA) Z H OS s3 (extSlotWriteX σ2 (extLookupSlot σ highGeom (specPtr p1) vaddr.bv).slot pte) m := by + refine fin x iv _ hk (einv ▸ hmem0) ?_ + rw [einv'] + unfold Fl4SpecA64.performExtVSpace + have hExtOk : ExtSlotOkX σ2 (extLookupSlot σ highGeom (specPtr p1) vaddr.bv).slot := + hiSlotOkA J2.1.vs (hslR.mono (fun _ _ g => g.1) (fun _ _ _ ht => ht.toTopHiE)) + have hst' : (Fl4SpecA64.storeExtSlot (Z := Unit) (extLookupSlot σ highGeom (specPtr p1) vaddr.bv).slot pte >>= + fun _ => pure ([] : List Fl4SpecA64.Data)) σ2 = + ({([], extSlotWriteX σ2 (extLookupSlot σ highGeom (specPtr p1) vaddr.bv).slot pte)}, False) := by + rw [nd_bind_oneD (storeExtSlot_runX hExtOk pte)]; rfl + exact ⟨((), σ2), hσ2, by dsimp only; rw [hst']; rfl⟩ + have hExtOk : ExtSlotOkX σ2 (extLookupSlot σ highGeom (specPtr p1) vaddr.bv).slot := + hiSlotOkA J2.1.vs (hslR.mono (fun _ _ g => g.1) (fun _ _ _ ht => ht.toTopHiE)) + have uniq : ∀ y, ActiveHighRootA (L.mem s) (L.ksb s) Z.Pp y → ∀ u ∈ OS, ∀ a', capA st s u = .HighVSpace y false a' → + False := by + intro y ⟨asid0, raw, ⟨hi, lo, hhi, hlo, hasid, pool, hpool, hpne, htab, hw, hrne⟩, htag, hun⟩ u hu a' hc' + obtain ⟨_, _, _, -, -, -, -, -, -, -, -, -, -, -, r2⟩ := J.j7 + obtain ⟨PpJ, _, _, _, _, _, _, _, R2, -, -⟩ := r2 + have hksF : (L.ksb s).val + 788736 ≤ 2 ^ 64 := by + have := J.sr.ksFit + unfold ksSizeA at this + omega + have hP : pool ∈ PoolSet st Z.Pp s := PoolSet.ofActiveA hpool ⟨hpne, hi, hhi, htab⟩ hksF + have hP7 : pool ∈ PoolSet st PpJ s := by + obtain ⟨-, o, h1, h2, h3, h4, h5⟩ := hP + rw [← h4] + have hty : VsTyR st (L.ksb s) (PoolSet st Z.Pp s) ∅ Tb Lv s := by rw [J.pp]; exact J.sys.ent.vs + exact R2.core.stage1.tbl o h1 h2 h3 (by rw [h4]; exact h5) + have act : ActiveAsidRoot st PpJ s asid0 raw := by + have h127 : (hi &&& 127#u64) = hi := UScalar.eq_of_val_eq (by + rw [and_maskA hi 7 _ rfl]; exact Nat.mod_eq_of_lt (by omega)) + have hat : wa (L.ksb s) (AtOff hi) = atAF (L.ksb s) hi := by unfold AtOff atAF; rw [h127] + have ho : (core.num.U64.wrapping_mul lo 8#u64).val = 8 * lo.val := wm8_valA hlo + exact ⟨hi, core.num.U64.wrapping_mul lo 8#u64, hhi, by omega, by omega, by omega, pool, hP7, + by rw [hat]; exact htab, hw, hrne⟩ + obtain ⟨x', hx', cap⟩ := R2.core.root.backed asid0 raw act + have t2 : raw &&& 3#u64 = 2#u64 := UScalar.eq_of_val_eq (by rw [and3_valA]; exact htag) + rcases cap with ⟨bad, -⟩ | ⟨bad, -⟩ | ⟨-, cap⟩ + · rw [t2] at bad; exact absurd bad (by decide) + · rw [t2] at bad; exact absurd bad (by decide) + have er : raw &&& ~~~3#u64 = y := hun + rw [er] at cap + have e := R2.capRefs.unique u hu x' hx' (3, y) (by rw [hc']; rfl) (by rw [cap]; rfl) (by rw [hc']; rfl) + rw [e, hc'] at cap + cases cap + have core := fun (q j : U64) (hq : XqA Z q) (hj : j.val < 512) (hal : Aligned (peAF q j)) + (hW' : ExtWordWriteA (st := st) s2 s3 (peAF q j) ew) + (hJ3 : JAtZAX (L := L) AR Z s3 a3 (extSlotWriteX σ2 (extLookupSlot σ highGeom (specPtr p1) vaddr.bv).slot pte) m) + hunm => + J.frameMapExtCoreA (s3 := s3) (a3 := a3) RS hnvm hK hT cte hab hcap0 (c := .Frame fp sz rt dev true false asid1 vaddr) + ⟨_, _, _, _, rfl, Or.inl rfl⟩ hEnc hca hok hq hj hal (extLookupSlot σ highGeom (specPtr p1) vaddr.bv).slot pte + hk hsc hσ2 hW' hJ3 hExtOk ev hunm fin' + have done : (∃ Ac' : Set U64, JStage3A st AR Z H OS U Ac' Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s3 a3 (extSlotWriteX σ2 (extLookupSlot σ highGeom (specPtr p1) vaddr.bv).slot pte) m ∧ + Sched.BodyKeepA (stUpdA σ σ.curThread x .restart) (extSlotWriteX σ2 (extLookupSlot σ highGeom (specPtr p1) vaddr.bv).slot pte)) ∧ + (([] : List Fl4SpecA64.Data), extSlotWriteX σ2 (extLookupSlot σ highGeom (specPtr p1) vaddr.bv).slot pte) ∈ + (Fl4SpecA64.performExtVSpace (Z := Unit) iv (stUpdA σ σ.curThread x .restart)).1 := by + rw [einv'] + rcases hslR with ⟨t, i, ⟨⟨ht, h1t, hst'⟩, hreach⟩, hi, hluEq, hslEq⟩ | ⟨y, j, hy, hyr, hj, hluEq, hslEq, hshEq⟩ + · have h3t : Z.Lv t ≤ 3 := E0.j.1.vs.lv t ht + have hshk : shiftA (Z.Lv t) = (psA sz).val := by unfold shiftA; rw [← hst', hsh2, hsh1] + have hW' : ExtWordWriteA (st := st) s2 s3 (peAF t i) ew := by rw [← hluEq]; exact hW + have hJ3 : JAtZAX (L := L) AR Z s3 a3 (extSlotWriteX σ2 (extLookupSlot σ highGeom (specPtr p1) vaddr.bv).slot pte) m := by + rw [hslEq] + have hV3 := J2.1.vs.of_ptWriteMapE hK2 ht h1t hi (hT2 t i hi (Or.inr (Or.inr (Or.inl ht)))) + (hPteAt (Z.Lv t) (by omega) h3t hshk) hW'.2.2 (fun _ h => (hnotPt _ h).elim) + have hE3 := J2.1.ext.of_vsWriteE (mem' := L.mem s3) J2.1.vs.yal (Or.inr (Or.inl ht)) (J2.1.vs.al t ht) hi + (fun b hb hne => by rw [hW'.2.2 b hb, if_neg (Ne.symm hne)]) (Or.inl ht) + (fun hp => by obtain ⟨_, h1, -⟩ := J2.1.vs.pool t hp; obtain ⟨_, h2, -⟩ := J2.1.vs.pt t ht; rw [h1] at h2; cases h2) + (slotObjPtE (J2.1.vs.al_mod ht) hi) pte + have hJ' := J2.of_extPageA hfa2 hTO2 hEO2 (Or.inr (Or.inr ht)) hW'.1 hW'.2.1 + (fun w hw hne => by rw [hW'.2.2 w hw, if_neg (Ne.symm (hne i hi))]) + (Or.inr (Or.inr (Or.inr ht))) (fun p hp => + kheap_writeX (by rw [slotObjPtE (J2.1.vs.al_mod ht) hi]; exact hp)) + (by rw [hW'.1]; exact hV3) hE3 + exact hJ'.of_arA hAR' + have R1 := E0.j.1.vs + have main := core t i (Or.inr (Or.inr ht)) hi (peAF_alignedA (R1.al_mod ht) hi) hW' hJ3 + (fun s1 hOS1 hks hcpu J1 Pt link hbk hword hk31 hcpu31 hset => by + have hsl' : extSlotObjX (extLookupSlot σ highGeom (specPtr p1) vaddr.bv).slot = specPtr t := by + rw [hslEq]; exact slotObjPtE (R1.al_mod ht) hi + have hty : VsTyR st (L.ksb s) (PoolSet st Z.Pp s) ∅ Tb Lv s := by rw [J.pp]; exact J.sys.ent.vs + have hksF : (L.ksb s).val + 788736 ≤ 2 ^ 64 := by + have := J.sr.ksFit + unfold ksSizeA at this + omega + obtain ⟨_, _, _, -, -, -, -, -, -, -, -, -, -, -, r2⟩ := J.j7 + obtain ⟨PpJ, _, _, _, _, _, _, _, R2J, -, -⟩ := r2 + have hpools : PoolSet st Z.Pp s ⊆ PoolSet st PpJ s := fun q hq => by + obtain ⟨-, o, h1, h2, h3, h4, h5⟩ := hq + rw [← h4] + exact R2J.core.stage1.tbl o h1 h2 h3 (by rw [h4]; exact h5) + have hrec := recReach_pathA R1 E0.j.1.ext hty hksF hreach.choose_spec.choose_spec.2 + have hJR := vsReach_bridgeA hty R2J.core.stage1 hpools hrec.1 + have hobj : ∀ u ∈ OS, CapObjOkA Z (capA st s1 u) := fun u hu => by + by_cases h : IsSlotA Z.th u + · exact J1.j.capObj h + · rw [J1.j5.2.2 u hu h]; exact CapObjOkA.null Z + have hJ : ∀ u ∈ OS, ∀ q a' v, capA st s1 u = .PageTable q false a' v → q ≠ t ∧ q.val % 4096 = 0 := by + intro u hu q a' v hc + rw [hOS1.capA hu] at hc + have hunm : UnmCap (capA st s u) q 4096 := Or.inl ⟨rfl, a', v, hc⟩ + refine ⟨fun h => R2J.core.lookup.unmR1 hu hunm (h ▸ ⟨hJR.2.2.1, hJR.1⟩), ?_⟩ + obtain ⟨z, hz, hH⟩ := R2J.core.lookup.vh.1 u hu q 4096 hunm + have hz12 : z = 12 := Nat.pow_right_injective (le_refl 2) (hz.symm.trans (by norm_num)) + have := J.sys.ent.heap.align q _ hH + rw [hz12] at this + exact this + obtain ⟨tp, kp, -⟩ := J1.j.1.vs.pt t ht + exact UnmPtSpecA.ofLeafWrA (Pt := Pt) (p := t) (i := i) (R1.al_mod ht) hi ht (by omega) + (fun h => (J.j.1.ext.pgd t h).1 (Or.inr (Or.inl ht))) R1.ytb R1.yal J1.j.1.ext.xal + (fun q hq => R1.al_mod hq) hobj hJ hbk hword link hk31 hcpu31 + (fun q ao hq h => hset q ao (by rw [hsl']; exact hq) h) ⟨tp, kp⟩ J1.unmPt) + exact ⟨main.2.2, main.2.1⟩ + · have hshk : shiftA 1 = (psA sz).val := by + have : (extLookupSlot σ highGeom (specPtr p1) vaddr.bv).shift = 30 := hshEq + rw [hsh2, hsh1] at this; unfold shiftA; omega + have hW' : ExtWordWriteA (st := st) s2 s3 (peAF (wa y 4096#u64) j) ew := by rw [← hluEq]; exact hW + have hJ3 : JAtZAX (L := L) AR Z s3 a3 (extSlotWriteX σ2 (extLookupSlot σ highGeom (specPtr p1) vaddr.bv).slot pte) m := by + rw [hslEq] + have hV3 := J2.1.vs.of_hiRootWriteMapE hK2 hy hj (hT2 (wa y 4096#u64) j hj (Or.inr (Or.inr (Or.inr ⟨y, hy, rfl⟩)))) + (hPteAt 1 (by omega) (by omega) hshk) (J2.1.ext.ypp y hy) hW'.2.2 (fun _ h => (hnotPt _ h).elim) + have hE3 := J2.1.ext.of_vsWriteE (mem' := L.mem s3) J2.1.vs.yal (Or.inr (Or.inr ⟨y, hy, rfl⟩)) + (Nat.dvd_of_mod_eq_zero (waWin_modA (J2.1.vs.yal y hy))) hj + (fun b hb hne => by rw [hW'.2.2 b hb, if_neg (Ne.symm hne)]) (sl := .root (specPtr y) j.val) (Or.inr hy) + (fun hp => J2.1.ext.ypg y hy (Or.inl hp)) rfl pte + have hJ' := J2.of_extPageA hfa2 hTO2 hEO2 (Or.inr (Or.inl ⟨y, hy, rfl⟩)) hW'.1 hW'.2.1 + (fun w hw hne => by rw [hW'.2.2 w hw, if_neg (Ne.symm (hne j hj))]) + (Or.inr (Or.inl hy)) (fun p hp => kheap_writeX (sl := .root (specPtr y) j.val) (pte := pte) hp) + (by rw [hW'.1]; exact hV3) hE3 + exact hJ'.of_arA hAR' + have R1 := E0.j.1.vs + have hym : (wa y 4096#u64).val % 4096 = 0 := waWin_modA (R1.yal y hy) + have main := core (wa y 4096#u64) j (Or.inr (Or.inl ⟨y, hy, rfl⟩)) hj (peAF_alignedA hym hj) hW' hJ3 + (fun s1 hOS1 hks hcpu J1 Pt link hbk hword hk31 hcpu31 hset => by + have hsl' : extSlotObjX (extLookupSlot σ highGeom (specPtr p1) vaddr.bv).slot = specPtr y := by + rw [hslEq]; rfl + refine UnmPtSpecA.ofFootWrA (Pt := Pt) (b := peAF (wa y 4096#u64) j) (o := specPtr y) + (.nslot _ (.vs _ (.high y j hy hj))) + (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨y, hy, rfl⟩)))))) hbk hword link hk31 hcpu31 + (fun q ao hq h => hset q ao (by rw [hsl']; exact hq) h) (fun u hu p a' v hc => ?_) + (fun u hu p a' hc => ?_) J1.unmPt + · have hpX : p ∈ Z.X := (J1.j.capObj (by + by_contra h + rw [J1.j5.2.2 u hu h] at hc; cases hc)).2.1 p 0#u64 false a' v hc + have pa8 := J1.j.1.ext.xal p hpX + have g1 := J1.j.1.ext.pgd p (Or.inr ⟨p, hpX, Or.inl rfl⟩) + have g2 := J1.j.1.ext.pgd (wa p 4096#u64) (Or.inr ⟨p, hpX, Or.inr rfl⟩) + refine ⟨fun k hk' => ⟨peAF_ne_tabA hym (by have := al4kE pa8; omega) hj hk' + (fun e => g1.1 (Or.inr (Or.inr ⟨y, hy, e⟩))), peAF_ne_tabA hym (by have := al4kHiE pa8; omega) hj hk' + (fun e => g2.1 (Or.inr (Or.inr ⟨y, hy, e⟩)))⟩, fun e => g1.2 (specPtr_inj e ▸ hy)⟩ + · have hpY : p ∈ Z.Y := (J1.j.capObj (by + by_contra h + rw [J1.j5.2.2 u hu h] at hc; cases hc)).2.2.1 p false a' hc + have ne : p ≠ y := fun e => uniq y hyr u hu a' (by rw [← e, ← hOS1.capA hu]; exact hc) + refine ⟨fun k hk' => peAF_ne_tabA hym (waWin_modA (R1.yal p hpY)) hj hk' (fun e => ne ?_), + fun e => ne (specPtr_inj e)⟩ + refine UScalar.eq_of_val_eq ?_ + have := congrArg UScalar.val e + rw [waWin_valA (R1.yal p hpY), waWin_valA (R1.yal y hy)] at this + omega) + exact ⟨main.2.2, main.2.1⟩ + obtain ⟨⟨Ac', Jf, Kf⟩, hperf⟩ := done + exact ⟨x, iv, _, Ac', hk, hset, einv ▸ hmem0, hperf, Jf, Kf⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3ArmFrUnmA.lean b/proofs/Fl4ProofsA64/P3/Stage3ArmFrUnmA.lean new file mode 100644 index 000000000..d42b60fed --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ArmFrUnmA.lean @@ -0,0 +1,390 @@ +/- +P3 (AArch64, merge line, D8 step 5 (ii)): **closing the `ARMPageUnmap` arm of an unmapped frame cap**. The arm is +the caller's Restart followed by the in-place cap write that clears the map data (no VSpace word is written), so the +closure is `of_restartCurA` then `of_capFrame`. Both the old and the new cap are unmapped frames, which R2 does not +read: **J7 is derived** (`J7A.ofUnmFrA`, `S1aR2.ofImage`), as are the IPC-buffer fields, J9 (`unmPt`) and the buffer +placement. The only arm hypothesis is `SysL` at the final state (discharged once at T3). + +No named premise. New module; changed statement: `JStage3A.of_capFrame`'s `arm` argument now also receives the +spec slot step and the FL4 write set. +-/ +import Fl4ProofsA64.P3.Stage3BodyKeepA +import Fl4ProofsA64.P3.Stage3CapFrameA +import Fl4ProofsA64.P3.Stage3CapSlotA +import Fl4ProofsA64.P3.Stage3CapBufA +import Fl4ProofsA64.P3.Stage3RestartA +import Fl4ProofsA64.P3.UnmapL4vJA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-- **An unmapped frame cap rewritten as an unmapped frame cap** at slot `x`, the other `OS` slots, the link words, +the kernel base and the `VsR` words of `Pt` kept. -/ +structure UnmFrWrA (st : machine.Store S) [L : LawfulStore st] (OS : Set U64) (s s' : S) (x p : U64) + (sz : capability.FrameSize) (r : U64) (d e : Bool) (a v : U64) : Prop where + hx : x ∈ OS + old : capA st s x = .Frame p sz r d false e a v + new : capA st s' x = .Frame p sz r d false false 0#u64 0#u64 + keep : ∀ y ∈ OS, y ≠ x → capA st s' y = capA st s y + nx : ∀ y ∈ OS, NxA st s' y = NxA st s y + ksb : L.ksb s' = L.ksb s + +namespace UnmFrWrA +variable {OS : Set U64} {s s' : S} {x p : U64} {sz : capability.FrameSize} {r : U64} {d e : Bool} {a v : U64} + +theorem alike (W : UnmFrWrA st OS s s' x p sz r d e a v) : ∀ y ∈ OS, MapAlikeA (capA st s' y) (capA st s y) := + fun y hy => by + by_cases h : y = x + · subst h; rw [W.new, W.old]; exact .frame .. + · rw [W.keep y hy h]; exact .refl _ + +/-- A cap that is no unmapped frame is at the same slot before and after. -/ +theorem back (W : UnmFrWrA st OS s s' x p sz r d e a v) {y : U64} (hy : y ∈ OS) {c : capability.Cap} + (h : capA st s' y = c) (hc : ∀ p sz r d e a v, c ≠ .Frame p sz r d false e a v) : capA st s y = c := by + by_cases e' : y = x + · subst e'; exact absurd (h.symm.trans W.new) (hc _ _ _ _ _ _ _) + · rw [← W.keep y hy e']; exact h + +theorem forth (W : UnmFrWrA st OS s s' x p sz r d e a v) {y : U64} (hy : y ∈ OS) {c : capability.Cap} + (h : capA st s y = c) (hc : ∀ p sz r d e a v, c ≠ .Frame p sz r d false e a v) : capA st s' y = c := by + by_cases e' : y = x + · subst e'; exact absurd (h.symm.trans W.old) (hc _ _ _ _ _ _ _) + · rw [W.keep y hy e']; exact h + +theorem image (W : UnmFrWrA st OS s s' x p sz r d e a v) (R : S1aRefs st OS s) : S1aSlotImage st OS s s' := by + have nr : ∀ p sz r d e a v, ¬ S1aR2Reads (.Frame p sz r d false e a v) := fun _ _ _ _ _ _ _ h => by + exact Bool.false_ne_true h + refine ⟨fun y hy h => ?_, fun y hy h => ?_, fun y hy z hz k ky kz n => ?_⟩ + · exact ⟨y, hy, W.back hy rfl fun _ _ _ _ _ _ _ q => nr _ _ _ _ _ _ _ (q ▸ h)⟩ + · exact ⟨y, hy, W.forth hy rfl fun _ _ _ _ _ _ _ q => nr _ _ _ _ _ _ _ (q ▸ h)⟩ + · have key : ∀ w ∈ OS, s1aTableKey (capA st s' w) = some k → capA st s w = capA st s' w := fun w hw q => + W.back hw rfl fun _ _ _ _ _ _ _ f => by rw [f] at q; cases q + have ey := key y hy ky + have ez := key z hz kz + exact R.unique y hy z hz k (ey ▸ ky) (ez ▸ kz) (ey ▸ n) + +/-- **A frame fact that ignores the mapping data** is kept at some slot. -/ +theorem frameAt (W : UnmFrWrA st OS s s' x p sz r d e a v) {y : U64} (hy : y ∈ OS) {q : U64} + {sz' : capability.FrameSize} {r' : U64} {x1 x2 : Bool} {x3 x4 : U64} + (h : capA st s y = .Frame q sz' r' false x1 x2 x3 x4) : + ∃ y1 x2' x3' x4', ∃ x1' : Bool, y1 ∈ OS ∧ capA st s' y1 = .Frame q sz' r' false x1' x2' x3' x4' := by + by_cases e' : y = x + · subst e' + rw [W.old] at h + injection h with h1 h2 h3 h4 + subst h1 h2 h3 h4 + exact ⟨y, false, 0#u64, 0#u64, false, hy, W.new⟩ + · exact ⟨y, x2, x3, x4, x1, hy, by rw [W.keep y hy e']; exact h⟩ + +theorem frameBack (W : UnmFrWrA st OS s s' x p sz r d e a v) {y : U64} (hy : y ∈ OS) {q : U64} + {sz' : capability.FrameSize} {r' : U64} {x1 x2 : Bool} {x3 x4 : U64} + (h : capA st s' y = .Frame q sz' r' false x1 x2 x3 x4) : + ∃ y1 x2' x3' x4', ∃ x1' : Bool, y1 ∈ OS ∧ capA st s y1 = .Frame q sz' r' false x1' x2' x3' x4' := by + by_cases e' : y = x + · subst e' + rw [W.new] at h + injection h with h1 h2 h3 h4 + subst h1 h2 h3 h4 + exact ⟨y, e, a, v, false, hy, W.old⟩ + · exact ⟨y, x2, x3, x4, x1, hy, by rw [← W.keep y hy e']; exact h⟩ + +theorem inU (W : UnmFrWrA st OS s s' x p sz r d e a v) (q b i : U64) (dv : Bool) (z : U64) : + InU st OS s' (.Untyped q b i dv) z ↔ InU st OS s (.Untyped q b i dv) z := by + unfold InU + refine and_congr_right fun hz => ?_ + by_cases e' : z = x + · subst e'; rw [W.new, W.old]; exact Iff.of_eq rfl + · rw [W.keep z hz e'] + +theorem capCol (W : UnmFrWrA st OS s s' x p sz r d e a v) {H : Heap} {Ub : Set U64} (C : S1aCapCol st H OS Ub s) : + S1aCapCol st H OS Ub s' := by + have fw : FrameWords st OS s' = FrameWords st OS s := by + ext b + constructor + · rintro ⟨ha, y, hy, q, sz', r', x1, x2, x3, x4, bits, hc, hr⟩ + obtain ⟨y1, x2', x3', x4', x1', hy1, h1⟩ := W.frameBack hy hc + exact ⟨ha, y1, hy1, q, sz', r', x1', x2', x3', x4', bits, h1, hr⟩ + · rintro ⟨ha, y, hy, q, sz', r', x1, x2, x3, x4, bits, hc, hr⟩ + obtain ⟨y1, x2', x3', x4', x1', hy1, h1⟩ := W.frameAt hy hc + exact ⟨ha, y1, hy1, q, sz', r', x1', x2', x3', x4', bits, h1, hr⟩ + have ut : ∀ u ∈ OS, ∀ q b i, capA st s' u = .Untyped q b i false → capA st s u = .Untyped q b i false := + fun u hu q b i h => W.back hu h fun _ _ _ _ _ _ _ f => by cases f + refine ⟨fun b hb => ?_, ?_, fun y hy q sz' r' x1 x2 x3 x4 h => ?_⟩ + · obtain ⟨y, hy, q, sz', r', x1, x2, x3, x4, bits, hc, hr⟩ := C.uf b hb + obtain ⟨y1, x2', x3', x4', x1', hy1, h1⟩ := W.frameAt hy hc + exact ⟨y1, hy1, q, sz', r', x1', x2', x3', x4', bits, h1, hr⟩ + · show UserOffUt st OS (FrameWords st OS s') s' + rw [fw] + refine ⟨fun u hu q b i hc => C.uo.1 u hu q b i (ut u hu q b i hc), fun u hu q b i hc hn => ?_⟩ + refine C.uo.2 u hu q b i (ut u hu q b i hc) fun hI => hn ?_ + rw [W.nx u hu] + exact (W.inU q b i false _).2 hI + · obtain ⟨y1, x2', x3', x4', x1', hy1, h1⟩ := W.frameBack hy h + exact C.fo y1 hy1 q sz' r' x1' x2' x3' x4' h1 + +theorem schedPage (W : UnmFrWrA st OS s s' x p sz r d e a v) {Z : XSetsA} {q : U64} + (h : SchedPageA st Z OS s' q) : SchedPageA st Z OS s q := by + unfold SchedPageA at h ⊢ + rcases h with h | h | h | h | h | h | h | ⟨u, hu, h⟩ + · exact Or.inl h + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr (Or.inl h)) + · exact Or.inr (Or.inr (Or.inr (Or.inl h))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inl h)))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl h))))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl h)))))) + · refine Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ⟨u, hu, ?_⟩)))))) + rcases h with ⟨m, a', v', h⟩ | ⟨m, a', h⟩ | ⟨a', v', h⟩ | ⟨q', a', v', h, e'⟩ | ⟨y, a', h, e'⟩ + · exact Or.inl ⟨m, a', v', W.back hu h fun _ _ _ _ _ _ _ f => by cases f⟩ + · exact Or.inr (Or.inl ⟨m, a', W.back hu h fun _ _ _ _ _ _ _ f => by cases f⟩) + · exact Or.inr (Or.inr (Or.inl ⟨a', v', W.back hu h fun _ _ _ _ _ _ _ f => by cases f⟩)) + · exact Or.inr (Or.inr (Or.inr (Or.inl ⟨q', a', v', W.back hu h (fun _ _ _ _ _ _ _ f => by cases f), e'⟩))) + · exact Or.inr (Or.inr (Or.inr (Or.inr ⟨y, a', W.back hu h (fun _ _ _ _ _ _ _ f => by cases f), e'⟩))) + +end UnmFrWrA + +/-- **J7 across an unmapped-frame cap rewrite** (module docstring). -/ +theorem J7A.ofUnmFrA {ar : machine.Arch A} {nA : U64} {Z : XSetsA} {H : Heap} {OS : Set U64} {s s' : S} + {σ σ' : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} {x p : U64} {sz : capability.FrameSize} {r : U64} + {d e : Bool} {a v : U64} (W : UnmFrWrA st OS s s' x p sz r d e a v) + (vk : ∀ Pt Ub : Set U64, VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt → + VsKeep st (L.ksb s) Pt s s') + (h : J7A st (J7R2A st ar nA) Z H OS s σ m) : J7A st (J7R2A st ar nA) Z H OS s' σ' m := by + obtain ⟨Pt, Ub, vr, pn, col, cc, vc, da, -, link, vcw, fu, ual, vcu, Pp, Tb, Hr, Te, Kt, Lv, Re, bits, r2, kt, + KLv, ktK, hik⟩ := h + have mem := vk Pt Ub vr + have tw : ∀ j, L.mem s' (wa (L.ksb s') (AtOff j)) = L.mem s (wa (L.ksb s) (AtOff j)) := fun j => by + rw [W.ksb]; exact mem _ (Or.inr (asW_at _ j)) + have R2 := S1aR2.ofImage r2 W.ksb mem (W.image r2.capRefs.refs) + have ci : ∀ {P : capability.Cap → Prop}, P (.Frame p sz r d false false 0#u64 0#u64) → CapInv st P OS s → + CapInv st P OS s' := fun {P} hP h y hy => by + by_cases e' : y = x + · subst e'; rw [W.new]; exact hP + · rw [W.keep y hy e']; exact h y hy + refine ⟨Pt, Ub, by rw [W.ksb]; exact vr, ?_, col.ofTab ⟨W.ksb, tw⟩, W.capCol cc, + ci (fun _ h => by cases h) vc, ci ?_ da, R2.highHeaders, + fun q hq => link q (W.schedPage hq), by rw [W.ksb]; exact vcw, ci ?_ fu, ual, vcu, Pp, Tb, Hr, Te, Kt, Lv, Re, bits, + R2, + S1aKtUt.ofRanges kt (fun y hy q b i dv hc => ⟨y, hy, q, b, i, dv, + W.back hy hc (fun _ _ _ _ _ _ _ f => by cases f), le_refl _, le_refl _⟩), KLv, ktK, + S1aHiK.keep hik ⟨EdgeKeepOn.ofVsKeep r2 mem, fun y hy R m a c => by + have c' : capA st s' y = capA st s y := by + by_cases e' : y = x + · subst e'; rw [W.new] at c; cases c + · exact W.keep y hy e' + exact ⟨y, hy, m, a, c'.symm.trans c⟩⟩⟩ + · intro j + have h0 := pn j + rw [vspace.asid_table.eq] at h0 ⊢ + rw [tw j] + refine (WP.spec_ok _).2 fun hne => ?_ + obtain ⟨y, hy, b, hc⟩ := (WP.spec_ok _).1 h0 hne + exact ⟨y, hy, b, W.forth hy hc fun _ _ _ _ _ _ _ f => by cases f⟩ + · have old := da x W.hx + rw [W.old] at old + intro q sz' r' m' e' a' v' h + injection h with h1 h2 h3 h4 + subst h1 h2 h3 h4 + exact old _ _ _ _ _ _ _ rfl + · have old := fu x W.hx + rw [W.old] at old + intro q sz' r' x1 x2 x3 x4 h + injection h with h1 h2 h3 h4 + subst h1 h2 h4 + exact old _ _ _ _ _ _ _ rfl + +/-- **J9 across an unmapped-frame cap rewrite.** -/ +theorem UnmPtSpecA.ofUnmFrA {Z : XSetsA} {OS : Set U64} {s s' : S} {σ σ' : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} {x p : U64} {sz : capability.FrameSize} {r : U64} {d e : Bool} {a v : U64} + (W : UnmFrWrA st OS s s' x p sz r d e a v) (HS : SpecSlotA σ σ') (hcpu : L.cpu s' = L.cpu s) + {Pt : Set U64} (mem : VsKeep st (L.ksb s) Pt s s') (link : ∀ q, SchedPageA st Z OS s q → q ∈ Pt) + (h : UnmPtSpecA st Z m OS s σ) : UnmPtSpecA st Z m OS s' σ' := by + have word : ∀ q ∈ Pt, ∀ i : U64, i.val < 512 → L.mem s' (peAF q i) = L.mem s (peAF q i) := fun q hq i hi => + mem _ (Or.inl ⟨q, hq, core.num.U64.wrapping_mul i 8#u64, by rw [wm8_valA hi]; omega, + by rw [wm8_valA hi]; omega, rfl⟩) + have kk : ∀ q ao, σ.kheap q = some (.archObj ao) → σ'.kheap q = some (.archObj ao) := fun q ao h => + HS.keep h (fun _ _ e => by cases e) (fun _ e => by cases e) + have tab : ∀ ty q, q ∈ Pt → UnmTabA st Z m s σ ty q → UnmTabA st Z m s' σ' ty q := fun ty q hq h => by + obtain ⟨t, k1, k2, k3⟩ := h.spec + exact ⟨fun i hi => by rw [word q hq i hi]; exact h.inv i hi, ⟨t, kk _ _ k1, k2, k3⟩, + h.off.of_ksA W.ksb hcpu⟩ + have bk : ∀ u ∈ OS, ∀ c, capA st s' u = c → (∀ p sz r d e a v, c ≠ .Frame p sz r d false e a v) → + capA st s u = c := fun u hu c h n => W.back hu h n + refine ⟨fun u hu q asid va hc => ?_, fun u hu q asid hc => ?_, fun u hu q asid va hc => ?_, + fun u hu q asid hc => ?_⟩ + · have o := bk u hu _ hc (fun _ _ _ _ _ _ _ f => by cases f) + exact tab _ _ (link q (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inl ⟨_, _, _, o⟩⟩)))))))) (h.pt u hu q asid va o) + · have o := bk u hu _ hc (fun _ _ _ _ _ _ _ f => by cases f) + exact tab _ _ (link q (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inl ⟨_, _, o⟩)⟩)))))))) (h.vs u hu q asid o) + · have o := bk u hu _ hc (fun _ _ _ _ _ _ _ f => by cases f) + have l0 : q ∈ Pt := link q (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inr (Or.inl ⟨_, _, o⟩))⟩))))))) + have l1 : wa q 4096#u64 ∈ Pt := link _ (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inr (Or.inr (Or.inl ⟨q, _, _, o, rfl⟩)))⟩))))))) + have h0 := h.s2 u hu q asid va o + obtain ⟨es, k1, k2⟩ := h0.spec + refine ⟨fun i hi => ?_, es, kk _ _ k1, k2⟩ + by_cases hlo : i.val < 512 + · rw [word q l0 i hlo]; exact h0.inv i hi + · obtain ⟨j, hj, ej⟩ := peAF_hiA q i (by omega) hi + have := h0.inv i hi + rw [ej] at this ⊢ + rw [word _ l1 j hj]; exact this + · have o := bk u hu _ hc (fun _ _ _ _ _ _ _ f => by cases f) + have l1 : wa q 4096#u64 ∈ Pt := link _ (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inr (Or.inr (Or.inr ⟨q, _, o, rfl⟩)))⟩))))))) + have h0 := h.hv u hu q asid o + obtain ⟨l, k1, k2⟩ := h0.spec + exact ⟨fun i hi => by rw [word _ l1 i hi]; exact h0.inv i hi, l, kk _ _ k1, k2⟩ + +set_option maxHeartbeats 2000000 in +/-- **`JStage3A` across the `ARMPageUnmap` arm of an unmapped frame cap** (module docstring): FL4's decode succeeds, +the spec takes the Restart and the cap write, and the record holds at the final state. -/ +theorem JStage3A.frameUnmapUA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {ar : machine.Arch A} {nA : U64} {s : S} {a : A} + {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) (hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM) (length buf : U64) (call : Bool) + (ex : Aeneas.Std.Array obj.Slot 3#usize) (cte : Fl4SpecA64.CslotPtr) (hab : AbsSlotA Z.th cte) + (fp rt asid va : U64) (sz : capability.FrameSize) (dev isEpt : Bool) + (hcap : capA st s (cslotAddrA cte) = .Frame fp sz rt dev false isEpt asid va) + (symL : ∀ s2, obj.Slot.set_cap st (cslotAddrA cte) (L.wr s (stA (L.curW s)) sched.TS_RESTART) + (.Frame fp sz rt dev false false 0#u64 0#u64) = ok s2 → SymL st H Z.Cs s2) + (sysl : ∀ s2, obj.Slot.set_cap st (cslotAddrA cte) (L.wr s (stA (L.curW s)) sched.TS_RESTART) + (.Frame fp sz rt dev false false 0#u64 0#u64) = ok s2 → SysL st H Z.Cs OS ia ie s2) : + ∃ x s2 σ2, σ.kheap σ.curThread = some (.tcb x) ∧ + vspace.decode_frame st ar s a fl4_abi.label.ARMPageUnmap length (cslotAddrA cte) + (.Frame fp sz rt dev false isEpt asid va) call buf ex = ok (core.result.Result.Ok (), s2, a) ∧ + ((), σ2) ∈ (setCap (Z := Unit) (.archObjectCap (.frameCap (specPtr fp) (vmRightsOfA rt) + (frameSizeOfA sz) dev none)) cte (stUpdA σ σ.curThread x .restart)).1 ∧ + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s2 a σ2 m ∧ + Sched.BodyKeepA (stUpdA σ σ.curThread x .restart) σ2 := by + have Je := J.toJEntA + obtain ⟨x, hk, heq, -, J1, -, hOS1, -⟩ := J.of_restartCurA RS.pl.choose_spec.choose_spec.choose_spec.choose_spec + RS.sym RS.run hnvm RS.cur RS.vs RS.vc + have hx : cslotAddrA cte ∈ OS := RS.sub _ (hab.isSlot J.j.1.place) + have hO : OSOK H.tcbs H.omega H.frame OS := J1.sys.ent.os + have c1 : capA st (L.wr s (stA (L.curW s)) sched.TS_RESTART) (cslotAddrA cte) = + .Frame fp sz rt dev false isEpt asid va := (hOS1.capA hx).trans hcap + obtain ⟨c0, -, hrel0⟩ := StateRelA.getAbsA J1.j.1.st hab + have c1' : capAtA (L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART)) (cslotAddrA cte) = + .Frame fp sz rt dev false isEpt asid va := c1 + rw [c1'] at hrel0 + obtain ⟨⟨hr1, hr3, -, hov⟩, -⟩ := hrel0 + have hEnc0 : CapRt.EncWf (capAtA (L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART)) (cslotAddrA cte)) := + CapRt.decodeF_encWf _ _ + rw [c1'] at hEnc0 + have hcan : CapRt.Canonical fp ∧ rt.val < 4 := by + cases isEpt <;> (simp only [CapRt.EncWf] at hEnc0; exact ⟨hEnc0.1, hEnc0.2.1⟩) + have hEnc : CapRt.EncWf (.Frame fp sz rt dev false false 0#u64 0#u64) := by + simp only [CapRt.EncWf]; exact ⟨hcan.1, hcan.2, by simp, Or.inl fun j hj _ => by simp⟩ + have hca : CapRelA (.Frame fp sz rt dev false false 0#u64 0#u64) + (.archObjectCap (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev none)) := + ⟨⟨hr1, hr3, by simp, hov⟩, rfl⟩ + have hok : CapObjOkA Z (.Frame fp sz rt dev false false 0#u64 0#u64) := + ⟨fun _ h => (by cases h), fun _ _ _ _ _ h => (by cases h), fun _ _ _ h => (by cases h), + fun _ _ _ h => (by cases h), (by intro h; cases h)⟩ + have hTr : TreeA st OS Z.G.chains Z.G.par (L.wr s (stA (L.curW s)) sched.TS_RESTART) := + J1.j.1.cdt.tree.grow J1.j5.2.1 (fun x hx hn => J1.j5.2.2 x hx hn) + have hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st (L.wr s (stA (L.curW s)) sched.TS_RESTART) y = .Null := + J1.j5.2.2 + have E1 := J1.toJEntA + have hN := bxi_nodes E1.sys.ent.bxi + have hirq : CapOkA (stUpdA σ σ.curThread x .restart).interruptIrqNode cte + (.archObjectCap (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev none)) := by + refine ⟨fun irq hlt e => ?_, fun ⟨_, _, _, _, e⟩ => by cases e⟩ + have obj : ∃ o ac, (stUpdA σ σ.curThread x .restart).kheap cte.1 = some o ∧ + Fl4SpecA64.capOf o cte.2 = some ac ∧ + CapRelA (capAtA (L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART)) (cslotAddrA cte)) ac := by + obtain ⟨o, idx⟩ := cte + obtain ⟨p, e1, hc | ⟨ht, i, hi, e2⟩⟩ := hab + · simp only at e1 hc; subst e1 + obtain ⟨cs, hk, -, hr⟩ := J1.j.1.st.cnode p _ hc + obtain ⟨ac, hac, hrel⟩ := hr idx rfl + exact ⟨_, ac, hk, hac, hrel⟩ + · simp only at e1 e2; subst e1 e2 + obtain ⟨y, hk, hr⟩ := J1.j.1.st.tcb p ht + obtain ⟨ac, hac, hrel⟩ := hr i hi + refine ⟨_, ac, hk, hac, ?_⟩ + unfold cslotAddrA; rw [ofBl_tcbIdx i (by unfold tcbSlotsA at hi; omega)]; exact hrel + obtain ⟨o, ac, hko, hco, hrel⟩ := obj + rw [e] at hko + rw [c1'] at hrel + rcases J1.sinv.irqN.slot irq hlt o _ ac hko hco with rfl | ⟨_, _, _, rfl⟩ + · cases isEpt <;> cases hrel.2 + · cases isEpt <;> cases hrel.2 + have hsci : NoIdleCapV m n (.archObjectCap (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev none)) := + ⟨fun _ _ e => (by cases e), fun _ e => (by cases e), fun _ _ _ e => (by cases e)⟩ + obtain ⟨s2, σ2, hs2, hσ2, HSsk, -, J2⟩ := J1.of_capFrame (hpt := fun _ _ _ h => by cases h) RS.sub + (footNOff_ksA rfl rfl (footN_offA E1)) (footNAl_ksA rfl rfl (FootNA.aligned E1.j.1 hN E1.scAl E1.ap.vcAl)) + cte hab hEnc hca hTr hnul (fun h => by cases h) (by rw [c1]; exact fun h => by cases h) + (c := .Frame fp sz rt dev false false 0#u64 0#u64) (by rw [c1]; exact .frame ..) hok hirq hsci + (fun s2 σ2 hs2 hσ2 HS w jx hc2 => by + obtain ⟨hcx, hco⟩ := capA_setCapA hO hx hEnc hs2 + obtain ⟨w0, w1, hen, -⟩ := CapRt.decode_encode _ hEnc + have es2 : L.wr (L.wr (L.wr s (stA (L.curW s)) sched.TS_RESTART) (cslotAddrA cte) w0) + (wa (cslotAddrA cte) 8#u64) w1 = s2 := + okinj ((set_cap_eq (st := st) (s := L.wr s (stA (L.curW s)) sched.TS_RESTART) (x := cslotAddrA cte) + hen).symm.trans hs2) + have W : UnmFrWrA st OS (L.wr s (stA (L.curW s)) sched.TS_RESTART) s2 (cslotAddrA cte) fp sz rt dev isEpt + asid va := + ⟨hx, c1, hcx, hco, fun y hy => by unfold NxA; rw [← es2, (set_cap_links hO hx y hy).2], w.1⟩ + have vk : ∀ Pt Ub : Set U64, VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub + (L.ksb (L.wr s (stA (L.curW s)) sched.TS_RESTART)) Pt → + VsKeep st (L.ksb (L.wr s (stA (L.curW s)) sched.TS_RESTART)) Pt + (L.wr s (stA (L.curW s)) sched.TS_RESTART) s2 := fun Pt Ub vr => + VsKeep.of_w vr.al w (fun b hb => J1.vsr_offCapWA vr hb) + obtain ⟨Pt, Ub, vr, -, -, -, -, -, -, link, -, -⟩ := J1.j7 + have wA : WritesIn st (L.wr s (stA (L.curW s)) sched.TS_RESTART) s2 + (fun b => Aligned b ∧ W7.CapW H.tcbs {b | InOS OS b} ∅ b) := writesIn_alAG w + have hB : ∀ b, (Aligned b ∧ W7.CapW H.tcbs {b | InOS OS b} ∅ b) → W7.CapW H.tcbs H.omega H.frame b := + fun b hb => os_capw hO hb.1 hb.2 + obtain ⟨-, -, -, -, -, fld, -, -⟩ := W7.blk_of_capw (BXI.blk J1.sys.ent.bxi) wA hB + have sbS : ∀ t ∈ Z.T, sbWordA t ∈ OS := fun t ht => + RS.sub _ ⟨t, Or.inr ⟨(J1.j.1.thT t).2 ht, 2, by simp [tcbSlotsA], (bufSlot_eqA t).symm⟩⟩ + have ib : ∀ t ∈ Z.T, L.mem s2 (ibWordA t) = L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART) (ibWordA t) := + fun t ht => fld t (by rw [J1.tcbs]; exact ht) 3512#u64 (by simp) (by simp) (by simp) + have R1 := J1.j.1.st + have R2 := jx.1.st + refine + { sysl := sysl s2 hs2 + symL := symL s2 hs2 + unmPt := UnmPtSpecA.ofUnmFrA W HS w.2.1 (vk Pt Ub vr) link J1.unmPt + bufOff := ?_ + bufCover := BufCover.mapA J1.bufCover J1.bufNative R1 R2 (fun t ht => (J1.j.1.thT t).2 ht) HS ib + (fun t ht => W.alike _ (sbS t ht)) + bufNative := BufNativeA.mapA J1.bufNative (fun t ht q sz' rr dv m' e' a' v' h => by + by_cases e1 : sbWordA t = cslotAddrA cte + · rw [e1, hcx] at h; cases h; rfl + · rw [hco _ (sbS t ht) e1] at h + have hn := J1.bufNative t ht + rw [bufSlot_eqA, capAtA_eq (st := st), h] at hn + exact hn) + j7 := J7A.ofUnmFrA W vk J1.j7 } + exact ⟨J1.bufOff.os, fun b hb => by rw [W.ksb]; exact J1.bufOff.vs b hb, + fun b hb q hq => J1.bufOff.page b hb q (W.schedPage hq), J1.bufOff.vcw, + fun bp hbp => (J1.bufOff.pin bp hbp).keepF fun t ht hO _ _ r x1 x2 x3 x4 e => ⟨ht, hO, by + by_cases e1 : sbWordA t = cslotAddrA cte + · rw [e1, c1] at e; cases e; exact ⟨_, _, _, _, _, by rw [e1]; exact hcx⟩ + · exact ⟨r, x1, x2, x3, x4, by rw [hco _ hO e1]; exact e⟩⟩⟩) + refine ⟨x, s2, σ2, hk, ?_, hσ2, J2, Sched.BodyKeepA.ofSlot HSsk (setCap_keepsA hσ2).2.2⟩ + rw [page_unmapU_eqA ar s a length (cslotAddrA cte) fp rt asid va sz dev isEpt call buf ex, heq, bind_ok, hs2, + bind_ok] + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3ArmFrUnmMA.lean b/proofs/Fl4ProofsA64/P3/Stage3ArmFrUnmMA.lean new file mode 100644 index 000000000..33457af22 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ArmFrUnmMA.lean @@ -0,0 +1,510 @@ +/- +P3 (AArch64, merge line, D8 step 5 (ii)): **closing the `ARMPageUnmap` arm of a mapped frame cap on a Stage-1 root or +on none** (`page_unmapS1_decJA`'s arm: FL4's `unmap_page` against l4v's `unmap_page`, then the cap write). + +FL4 unmaps first (a zero write into one Stage-1 table, or nothing) and then rewrites the slot; the record's wrapper +`JStage3A.of_capVsFrameA` is slot-first. The two commute: the slot words and the written table word are disjoint +(`WritesIn.setCapCommA`), and on the spec side the unmap is a `VsSpecA` step (`P3/Stage3UnmapSpecA`) that +`VsSpecA.setCapCommA` moves across the `set_cap`. + +* `unmap_post2A`: `unmap_postS1JA` with the written word, the spec relation `UnQ` and the FL4 state's shape exported. +* `WritesIn.setCapCommA`, `BufCover.vsA`, `slotWord_inOS`, `setcap_core2A`. +* `UnmPtSpecA.ofFrUnmMA`: J9 across the cap rewrite and the table invalidation. +* **`JStage3A.frameUnmapS1A`**: restart, `of_capVsFrameA`. The arm hypotheses are the final-state facts owed by T3 + (`SysL`, `BXI` at some `Ac'`, `UserOK`, J7); everything else (`vs`, `vsOk`, `sq`, `unmPt`, the IPC-buffer fields, + `poolOff`) is derived here. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3BodyKeepA +import Fl4ProofsA64.P3.Stage3ArmFrUnmA +import Fl4ProofsA64.P3.Stage3CapVsFrameA +import Fl4ProofsA64.P3.Stage3UnmapExtSpecA +import Fl4ProofsA64.P3.Stage3VsZeroA +import Fl4ProofsA64.P3.UnmapL4vJA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} + +/-! ## Helpers -/ + +/-- **`unmap_postS1JA`, with the shape of the unmap exported** (module docstring). -/ +theorem unmap_post2A {fl : Result A} {u : Result (S × A)} {mm : Fl4SpecA64.SMonad Unit Unit} (hmm : OutKmA mm) + (hqv : OutQ UnQ mm) (J : JAtZAX (L := L) AR Z s a σ m) (hfa : ∀ w, FootA (L := L) Z s w → Aligned w) + (hTO : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s q) {T Ω Sg OS : Set U64} (hO : OSOK T Ω Sg OS) + (hpOS : VsPgOffOSA Z OS) (h : UnmapRelA Z.Pp Z.S1 Z.Lv Z.X Z.Y σ st s a fl u (mm σ)) : + ∃ (s2 : S) (fa : Result A) (σu : Fl4SpecA64.State Unit), (fa = ok a ∨ fa = fl) ∧ + u = (do let a1 ← fa; ok (s2, a1)) ∧ ((), σu) ∈ (mm σ).1 ∧ JAtZAX (L := L) AR Z s2 a σu m ∧ + OSKeep st OS s s2 ∧ L.ksb s2 = L.ksb s ∧ L.cpu s2 = L.cpu s ∧ UnQ σ σu ∧ + (s2 = s ∨ ∃ p ∈ Z.S1, ∃ i : U64, i.val < 512 ∧ Aligned (peAF p i) ∧ s2 = L.wr s (peAF p i) 0#u64) := by + rcases UnmapRelA.extE J.1.vs J.1.ext h with ⟨hu, hW⟩ | + ⟨slot, ⟨p, hp, i, hi, rfl⟩, hu, ⟨-, ⟨⟨⟨⟩, σu⟩, hx0⟩, h3⟩, h4⟩ + · exact ⟨s, ok a, σ, Or.inl rfl, by rw [hu, bind_ok], by rw [hW]; exact Set.mem_singleton _, J, + fun y _ => ⟨rfl, rfl, rfl, rfl⟩, rfl, rfl, UnQ.refl σ, Or.inl rfl⟩ + · have hal : Aligned (peAF p i) := peAF_alignedA (J.1.vs.al_mod hp) hi + exact ⟨_, fl, σu, Or.inr rfl, hu, hx0, + J.of_s1ClearA hfa hTO hp hi (hmm σ _ hx0) (h3 _ hx0).2 (h3 _ hx0).1 (h4 _ hx0), + osKeep_ofA hO fun w hw hin => by + rw [L.mem_wr _ _ _ _ hal hw, if_neg fun (e : peAF p i = w) => hpOS p i hi (Or.inr (Or.inr hp)) (e ▸ hin)], + L.ksb_wr _ _ _, L.cpu_wr _ _ _, hqv σ _ hx0, Or.inr ⟨p, hp, i, hi, hal, rfl⟩⟩ + +/-- **The two writes commute**: a write set `A` before a `set_cap` is the write set after it. -/ +theorem WritesIn.setCapCommA {s1 s2u s2 s3 : S} {x : U64} {c : capability.Cap} {w0 w1 : U64} {A : U64 → Prop} + (hx : Aligned x) (hx8 : Aligned (wa x 8#u64)) (hen : capability.Cap.encode c = ok (w0, w1)) + (hs2 : obj.Slot.set_cap st x s1 c = ok s2) (hs3 : obj.Slot.set_cap st x s2u c = ok s3) + (h : WritesIn st s1 s2u A) : WritesIn st s2 s3 A := by + have e2 : L.wr (L.wr s1 x w0) (wa x 8#u64) w1 = s2 := okinj ((set_cap_eq (st := st) (s := s1) hen).symm.trans hs2) + have e3 : L.wr (L.wr s2u x w0) (wa x 8#u64) w1 = s3 := okinj ((set_cap_eq (st := st) (s := s2u) hen).symm.trans hs3) + subst e2 e3 + refine ⟨?_, ?_, fun b hb hn => ?_⟩ + · simp only [L.ksb_wr]; exact h.1 + · simp only [L.cpu_wr]; exact h.2.1 + · simp only [L.mem_wr _ _ _ _ hx8 hb, L.mem_wr _ _ _ _ hx hb] + rw [h.2.2 b hb hn] + +/-- The words of a cap slot of `OS` lie in `OS`. -/ +theorem slotWord_inOS {T Ω Sg OS : Set U64} (hO : OSOK T Ω Sg OS) {x b : U64} (hx : x ∈ OS) (h : SlotWordA x b) : + InOS OS b := by + have hfit := hO.fit x hx + rcases h with rfl | rfl + · exact ⟨_, hx, Nat.le_refl _, by omega⟩ + · have h8 : (8#u64 : U64).val = 8 := by simp + have hv : (wa x 8#u64).val = x.val + 8 := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact ⟨x, hx, by omega, by omega⟩ + +/-- **The IPC-buffer cover across a VSpace step** that keeps the buffer words of every TCB. -/ +theorem BufCover.vsA {T : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {s s' : S} {σ σ' : Fl4SpecA64.State Unit} + (h : BufCover st T Bf s σ) (HS : VsSpecA σ σ') (hib : ∀ t ∈ T, IbSame (L := L) s s' t) : + BufCover st T Bf s' σ' := by + intro t ht x b sb v hx hb hl + rw [lookup_ipc_buffer_sameA (hib t ht)] at hl + exact h t ht x b sb v (HS.back hx (fun _ e => by cases e)) hb hl + +variable {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} {ia ie : Nat} + +set_option maxHeartbeats 1000000 in +/-- **`setcap_coreA`, with the spec slot step and the write set exported.** -/ +theorem setcap_core2A (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) {s2 : S} {σ2 : Fl4SpecA64.State Unit} + (J2 : JAtZAX (L := L) AR Z s2 a σ2 m) (hk : OSKeep st OS s s2) (hks : L.ksb s2 = L.ksb s) + (hcpu : L.cpu s2 = L.cpu s) (cte : Fl4SpecA64.CslotPtr) (hab : AbsSlotA Z.th cte) {c0 c : capability.Cap} + {sc : Fl4SpecA64.Cap} (hcap : capA st s (cslotAddrA cte) = c0) (hEnc : CapRt.EncWf c) (hca : CapRelA c sc) + (hua : UA c c0) (hok : CapObjOkA Z c) (hcn : c ≠ .Null) (hc0 : c0 ≠ .Null) + (hT : TreeA st OS Z.G.chains Z.G.par s) (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) : + ∃ s3 σ3, obj.Slot.set_cap st (cslotAddrA cte) s2 c = ok s3 ∧ + (∃ c', CapEqA c0 c' ∧ (c', σ2) ∈ (Fl4SpecA64.getCap (Z := Unit) cte σ2).1) ∧ + ((), σ3) ∈ (Fl4SpecA64.setCap (Z := Unit) sc cte σ2).1 ∧ SpecSlotA σ2 σ3 ∧ + JAtZAX (L := L) AR Z s3 a σ3 m ∧ TreeA st OS Z.G.chains Z.G.par s3 := by + have hos : cslotAddrA cte ∈ OS := RS.sub _ (hab.isSlot E.j.1.place) + have hN := bxi_nodes E.sys.ent.bxi + have e0 : capA st s2 (cslotAddrA cte) = c0 := (hk.capA hos).trans hcap + obtain ⟨c', hg, -, hc'⟩ := StateRelA.getAbsA J2.1.st hab + have e1 : capAtA (L.mem s2) (cslotAddrA cte) = c0 := e0 + rw [e1] at hc' + obtain ⟨s3, σ3, hsc, hσ3, HS, -, J3, T3⟩ := J2.of_setcapA E.sys.ent.os RS.sub + (footNOff_ksA hks hcpu (footN_offA E)) (footNAl_ksA hks hcpu (FootNA.aligned E.j.1 hN E.scAl E.ap.vcAl)) + cte hab hEnc hca (hT.keepO hk) (fun y hy hn => by rw [hk.capA hy]; exact hnul y hy hn) hcn + (by rw [e0]; exact hc0) (by rw [e0]; exact hua) hok + exact ⟨s3, σ3, hsc, ⟨c', hc', hg⟩, hσ3, HS, J3, T3⟩ + + +/-- **J9 across the unmap of a mapped frame** (the cap rewrite to an unmapped frame, entries of tables and Ext roots +invalidated). -/ +theorem UnmPtSpecA.ofFrUnmMA {Z : XSetsA} {OS : Set U64} {s s' : S} {σ σu σ' : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} {Pt : Set U64} + (hbk : ∀ u ∈ OS, ∀ c, capA st s' u = c → (∀ p sz r d mp e a v, c ≠ .Frame p sz r d mp e a v) → + capA st s u = c) + (hword : ∀ q ∈ Pt, ∀ i : U64, i.val < 512 → + L.mem s' (peAF q i) = L.mem s (peAF q i) ∨ L.mem s' (peAF q i) = 0#u64) + (link : ∀ q, SchedPageA st Z OS s q → q ∈ Pt) (hk : L.ksb s' = L.ksb s) (hcpu : L.cpu s' = L.cpu s) + (UI : InvXA σ σu) (hset : ∀ q ao, σu.kheap q = some (.archObj ao) → σ'.kheap q = some (.archObj ao)) + (h : UnmPtSpecA st Z m OS s σ) : UnmPtSpecA st Z m OS s' σ' := by + have hz0 : dBitA (0#u64) 0 = false := by unfold dBitA; rfl + have dz : ∀ (w w' : U64), dBitA w 0 = false → (w' = w ∨ w' = 0#u64) → dBitA w' 0 = false := fun w w' h0 e => by + rcases e with rfl | rfl + · exact h0 + · exact hz0 + have tab : ∀ ty q, q ∈ Pt → UnmTabA st Z m s σ ty q → UnmTabA st Z m s' σ' ty q := fun ty q hq h => by + obtain ⟨t, k1, k2, k3⟩ := h.spec + refine ⟨fun i hi => dz _ _ (h.inv i hi) (hword q hq i hi), ?_, h.off.of_ksA hk hcpu⟩ + rcases UI (specPtr q) with e | ⟨o, o', a0, b0, hn⟩ + · exact ⟨t, hset _ _ (e.trans k1), k2, k3⟩ + · rw [k1] at a0 + have h1 : o = .pageTable t := by + have := Option.some.inj a0 + injection this with h2 + exact h2.symm + subst h1 + obtain ⟨t0', e0, ty0, ap0⟩ := ArchInvA.pt hn + subst e0 + refine ⟨t0', hset _ _ b0, ty0.trans k2, fun i hi => ?_⟩ + rcases ap0 i.bv with h2 | h2 + · rw [h2]; exact k3 i hi + · exact h2 + have s2k : ∀ q es, σ.kheap (specPtr q) = some (.archObj (.ext (.stage2Root es))) → + (∀ i : Fin 1024, es i = .invalidPTE) → + ∃ es', σ'.kheap (specPtr q) = some (.archObj (.ext (.stage2Root es'))) ∧ ∀ i : Fin 1024, es' i = .invalidPTE := by + intro q es k1 k2 + rcases UI (specPtr q) with e | ⟨o, o', a0, b0, hn⟩ + · exact ⟨es, hset _ _ (e.trans k1), k2⟩ + · rw [k1] at a0 + have h1 : o = .ext (.stage2Root es) := by + have := Option.some.inj a0 + injection this with h2 + exact h2.symm + subst h1 + obtain ⟨e', e0, he⟩ := ArchInvA.s2 hn + subst e0 + exact ⟨e', hset _ _ b0, fun i => by rcases he i with h2 | h2; rw [h2]; exact k2 i; exact h2⟩ + have hik : ∀ q l, σ.kheap (specPtr q) = some (.archObj (.ext (.highVSpace l))) → + (∀ i : Fin 512, l i = .invalidPTE) → + ∃ l', σ'.kheap (specPtr q) = some (.archObj (.ext (.highVSpace l'))) ∧ ∀ i : Fin 512, l' i = .invalidPTE := by + intro q l k1 k2 + rcases UI (specPtr q) with e | ⟨o, o', a0, b0, hn⟩ + · exact ⟨l, hset _ _ (e.trans k1), k2⟩ + · rw [k1] at a0 + have h1 : o = .ext (.highVSpace l) := by + have := Option.some.inj a0 + injection this with h2 + exact h2.symm + subst h1 + obtain ⟨e', e0, he⟩ := ArchInvA.hi hn + subst e0 + exact ⟨e', hset _ _ b0, fun i => by rcases he i with h2 | h2; rw [h2]; exact k2 i; exact h2⟩ + refine ⟨fun u hu q asid va hc => ?_, fun u hu q asid hc => ?_, fun u hu q asid va hc => ?_, + fun u hu q asid hc => ?_⟩ + · have o := hbk u hu _ hc (fun _ _ _ _ _ _ _ _ f => by cases f) + exact tab _ _ (link q (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inl ⟨_, _, _, o⟩⟩)))))))) (h.pt u hu q asid va o) + · have o := hbk u hu _ hc (fun _ _ _ _ _ _ _ _ f => by cases f) + exact tab _ _ (link q (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inl ⟨_, _, o⟩)⟩)))))))) (h.vs u hu q asid o) + · have o := hbk u hu _ hc (fun _ _ _ _ _ _ _ _ f => by cases f) + have l0 : q ∈ Pt := link q (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inr (Or.inl ⟨_, _, o⟩))⟩))))))) + have l1 : wa q 4096#u64 ∈ Pt := link _ (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inr (Or.inr (Or.inl ⟨q, _, _, o, rfl⟩)))⟩))))))) + have h0 := h.s2 u hu q asid va o + obtain ⟨es, k1, k2⟩ := h0.spec + obtain ⟨es', k1', k2'⟩ := s2k q es k1 k2 + refine ⟨fun i hi => ?_, es', k1', k2'⟩ + by_cases hlo : i.val < 512 + · exact dz _ _ (h0.inv i hi) (hword q l0 i hlo) + · obtain ⟨j, hj, ej⟩ := peAF_hiA q i (by omega) hi + have := h0.inv i hi + rw [ej] at this ⊢ + exact dz _ _ this (hword _ l1 j hj) + · have o := hbk u hu _ hc (fun _ _ _ _ _ _ _ _ f => by cases f) + have l1 : wa q 4096#u64 ∈ Pt := link _ (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inr (Or.inr (Or.inr ⟨q, _, o, rfl⟩)))⟩))))))) + have h0 := h.hv u hu q asid o + obtain ⟨l, k1, k2⟩ := h0.spec + obtain ⟨l', k1', k2'⟩ := hik q l k1 k2 + exact ⟨fun i hi => dz _ _ (h0.inv i hi) (hword _ l1 i hi), l', k1', k2'⟩ + +/-- A `set_cap` keeps the arch state of the spec. -/ +theorem setCap_archA {cap : Fl4SpecA64.Cap} {sl : Fl4SpecA64.CslotPtr} {σ σ' : Fl4SpecA64.State Unit} + (h : ((), σ') ∈ (Fl4SpecA64.setCap (Z := Unit) cap sl σ).1) : σ'.archState = σ.archState := by + obtain ⟨p, cref⟩ := sl + obtain ⟨o, o', -, -, e⟩ := setCap_objA h + have h2 : σ'.archState = _ := congrArg (fun x : Fl4SpecA64.State Unit => x.archState) e + exact h2 + +/-- **J2 across a table-entry zeroing and `set_cap`**: the `SchedPageA` pages after a rewrite that adds no table cap. -/ +theorem SchedPageA.backA {Z : XSetsA} {OS : Set U64} {s s' : S} {q : U64} + (hbk : ∀ u ∈ OS, ∀ c, capA st s' u = c → (∀ p sz r d mp e a v, c ≠ .Frame p sz r d mp e a v) → + capA st s u = c) + (h : SchedPageA st Z OS s' q) : SchedPageA st Z OS s q := by + unfold SchedPageA at h ⊢ + rcases h with h | h | h | h | h | h | h | ⟨u, hu, h⟩ + · exact Or.inl h + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr (Or.inl h)) + · exact Or.inr (Or.inr (Or.inr (Or.inl h))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inl h)))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl h))))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl h)))))) + · refine Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ⟨u, hu, ?_⟩)))))) + rcases h with ⟨m, a', v', h⟩ | ⟨m, a', h⟩ | ⟨a', v', h⟩ | ⟨q', a', v', h, e'⟩ | ⟨y, a', h, e'⟩ + · exact Or.inl ⟨m, a', v', hbk u hu _ h fun _ _ _ _ _ _ _ _ f => by cases f⟩ + · exact Or.inr (Or.inl ⟨m, a', hbk u hu _ h fun _ _ _ _ _ _ _ _ f => by cases f⟩) + · exact Or.inr (Or.inr (Or.inl ⟨a', v', hbk u hu _ h fun _ _ _ _ _ _ _ _ f => by cases f⟩)) + · exact Or.inr (Or.inr (Or.inr (Or.inl ⟨q', a', v', hbk u hu _ h (fun _ _ _ _ _ _ _ _ f => by cases f), e'⟩))) + · exact Or.inr (Or.inr (Or.inr (Or.inr ⟨y, a', hbk u hu _ h (fun _ _ _ _ _ _ _ _ f => by cases f), e'⟩))) + +set_option maxHeartbeats 8000000 in +/-- **`JStage3A` across the `ARMPageUnmap` arm of a mapped frame cap on a Stage-1 root or on none** (module docstring): +FL4's decode succeeds, the spec performs l4v's `unmap_page` and the cap write, and the record holds at the final state. +The final-state facts owed at T3 (`SysL`, `BXI` at some `Ac'`, `UserOK`, J7) are the arm hypothesis `fin`. -/ +theorem JStage3A.frameUnmapS1A {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {ar : machine.Arch A} {nA : U64} {s : S} {a : A} + {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (K : ArchKeepsA ar AR) + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) (hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM) (hTO : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s q) + (hK : Aligned (L.ksb s)) (hT : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s)) + (length buf : U64) (call : Bool) (ex : Aeneas.Std.Array obj.Slot 3#usize) + (cte : Fl4SpecA64.CslotPtr) (hab : AbsSlotA Z.th cte) (fp rt asid va : U64) (sz : capability.FrameSize) + (dev : Bool) (h0 : asid ≠ 0#u64) + (hcap : capA st s (cslotAddrA cte) = .Frame fp sz rt dev true false asid va) + (hS1 : Fl4SpecA64.asidRootKind σ (BitVec.ofNat 16 asid.val) = none ∨ + Fl4SpecA64.asidRootKind σ (BitVec.ofNat 16 asid.val) = some .stage1) + (fin : ∀ (x : Fl4SpecA64.Tcb) (s3 : S) (σ3 : Fl4SpecA64.State Unit) (a' : A), + σ.kheap σ.curThread = some (.tcb x) → + vspace.decode_frame st ar s a fl4_abi.label.ARMPageUnmap length (cslotAddrA cte) + (.Frame fp sz rt dev true false asid va) call buf ex = ok (core.result.Result.Ok (), s3, a') → + (([] : List Fl4SpecA64.Data), σ3) ∈ (Fl4SpecA64.performPageInvocation (Z := Unit) + (.pageUnmap (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev + (some (BitVec.ofNat 16 asid.val, va.bv))) cte) (stUpdA σ σ.curThread x .restart)).1 → + ∃ Ac' : Set U64, SysL st H Z.Cs OS ia ie s3 ∧ SymL st H Z.Cs s3 ∧ + BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac' s3 ∧ + Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s3 ∧ + J7A st (J7R2A st ar nA) Z H OS s3 σ3 m) : + ∃ (x : Fl4SpecA64.Tcb) (s3 : S) (a' : A) (σ3 : Fl4SpecA64.State Unit) (Ac' : Set U64), + σ.kheap σ.curThread = some (.tcb x) ∧ + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + vspace.decode_frame st ar s a fl4_abi.label.ARMPageUnmap length (cslotAddrA cte) + (.Frame fp sz rt dev true false asid va) call buf ex = ok (core.result.Result.Ok (), s3, a') ∧ + (([] : List Fl4SpecA64.Data), σ3) ∈ (Fl4SpecA64.performPageInvocation (Z := Unit) + (.pageUnmap (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev + (some (BitVec.ofNat 16 asid.val, va.bv))) cte) (stUpdA σ σ.curThread x .restart)).1 ∧ + JStage3A st AR Z H OS U Ac' Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s3 a' σ3 m ∧ + Sched.BodyKeepA (stUpdA σ σ.curThread x .restart) σ3 := by + have hx : cslotAddrA cte ∈ OS := RS.sub _ (hab.isSlot J.j.1.place) + have hO : OSOK H.tcbs H.omega H.frame OS := J.sys.ent.os + have E0 := J.toJEntA + obtain ⟨x, hk, heq, hset, J1, -, hOS1, -⟩ := J.of_restartCurA RS.pl.choose_spec.choose_spec.choose_spec.choose_spec + RS.sym RS.run hnvm RS.cur RS.vs RS.vc + have hks : L.ksb (L.wr s (stA (L.curW s)) sched.TS_RESTART) = L.ksb s := L.ksb_wr _ _ _ + have hcpu : L.cpu (L.wr s (stA (L.curW s)) sched.TS_RESTART) = L.cpu s := L.cpu_wr _ _ _ + generalize L.wr s (stA (L.curW s)) sched.TS_RESTART = s1 at heq J1 hOS1 hks hcpu + have c1 : capA st s1 (cslotAddrA cte) = .Frame fp sz rt dev true false asid va := (hOS1.capA hx).trans hcap + obtain ⟨c0, -, hrel0⟩ := StateRelA.getAbsA J1.j.1.st hab + have c1' : capAtA (L.mem s1) (cslotAddrA cte) = .Frame fp sz rt dev true false asid va := c1 + rw [c1'] at hrel0 + obtain ⟨⟨hr1, hr3, ⟨h16a, -⟩, hov⟩, -⟩ := hrel0 + have h16 : asid.val < 65536 := lt_of_lt_of_eq h16a (by norm_num) + have hEnc0 : CapRt.EncWf (capAtA (L.mem s1) (cslotAddrA cte)) := CapRt.decodeF_encWf _ _ + rw [c1'] at hEnc0 + simp only [CapRt.EncWf] at hEnc0 + have hEnc : CapRt.EncWf (.Frame fp sz rt dev false false 0#u64 0#u64) := by + simp only [CapRt.EncWf]; exact ⟨hEnc0.1, hEnc0.2.1, by simp, Or.inl fun j hj _ => by simp⟩ + have hca : CapRelA (.Frame fp sz rt dev false false 0#u64 0#u64) + (.archObjectCap (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev none)) := + ⟨⟨hr1, hr3, by simp, hov⟩, rfl⟩ + have hok : CapObjOkA Z (.Frame fp sz rt dev false false 0#u64 0#u64) := + ⟨fun _ h => (by cases h), fun _ _ _ _ _ h => (by cases h), fun _ _ _ h => (by cases h), + fun _ _ _ h => (by cases h), (by intro h; cases h)⟩ + have hTr : TreeA st OS Z.G.chains Z.G.par s1 := + J1.j.1.cdt.tree.grow J1.j5.2.1 (fun x hx hn => J1.j5.2.2 x hx hn) + have hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s1 y = .Null := J1.j5.2.2 + have hTr0 : TreeA st OS Z.G.chains Z.G.par s := + J.j.1.cdt.tree.grow J.j5.2.1 (fun x hx hn => J.j5.2.2 x hx hn) + have hnul0 : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null := J.j5.2.2 + have E1 := J1.toJEntA + have hN := bxi_nodes E1.sys.ent.bxi + have hpOS := VsPgOffOSA.of_apA E1.ap + have hirq : CapOkA (stUpdA σ σ.curThread x .restart).interruptIrqNode cte + (.archObjectCap (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev none)) := by + refine ⟨fun irq hlt e => ?_, fun ⟨_, _, _, _, e⟩ => by cases e⟩ + have obj : ∃ o ac, (stUpdA σ σ.curThread x .restart).kheap cte.1 = some o ∧ + Fl4SpecA64.capOf o cte.2 = some ac ∧ CapRelA (capAtA (L.mem s1) (cslotAddrA cte)) ac := by + obtain ⟨o, idx⟩ := cte + obtain ⟨p, e1, hc | ⟨ht, i, hi, e2⟩⟩ := hab + · simp only at e1 hc; subst e1 + obtain ⟨cs, hk, -, hr⟩ := J1.j.1.st.cnode p _ hc + obtain ⟨ac, hac, hrel⟩ := hr idx rfl + exact ⟨_, ac, hk, hac, hrel⟩ + · simp only at e1 e2; subst e1 e2 + obtain ⟨y, hk, hr⟩ := J1.j.1.st.tcb p ht + obtain ⟨ac, hac, hrel⟩ := hr i hi + refine ⟨_, ac, hk, hac, ?_⟩ + unfold cslotAddrA; rw [ofBl_tcbIdx i (by unfold tcbSlotsA at hi; omega)]; exact hrel + obtain ⟨o, ac, hko, hco, hrel⟩ := obj + rw [e] at hko + rw [c1'] at hrel + rcases J1.sinv.irqN.slot irq hlt o _ ac hko hco with rfl | ⟨_, _, _, rfl⟩ + · cases hrel.2 + · cases hrel.2 + have hsci : NoIdleCapV m n (.archObjectCap (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev none)) := + ⟨fun _ _ e => (by cases e), fun _ e => (by cases e), fun _ _ _ e => (by cases e)⟩ + have main := J1.of_capVsFrameA (hpt := fun _ _ _ h => by cases h) + (Q := fun s' a' σ' hw => vspace.decode_frame st ar s a fl4_abi.label.ARMPageUnmap length (cslotAddrA cte) + (.Frame fp sz rt dev true false asid va) call buf ex = ok (core.result.Result.Ok (), s', a') ∧ + (([] : List Fl4SpecA64.Data), σ') ∈ (Fl4SpecA64.performPageInvocation (Z := Unit) + (.pageUnmap (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev + (some (BitVec.ofNat 16 asid.val, va.bv))) cte) (stUpdA σ σ.curThread x .restart)).1 ∧ hw = Z.hw) + RS.sub (footNOff_ksA rfl rfl (footN_offA E1)) (footNAl_ksA rfl rfl (FootNA.aligned E1.j.1 hN E1.scAl E1.ap.vcAl)) + cte hab hEnc hca hTr hnul (fun h => by cases h) (by rw [c1]; exact fun h => by cases h) + (c := .Frame fp sz rt dev false false 0#u64 0#u64) (by rw [c1]; exact .frame ..) hok hirq hsci + (fun s2 σ2 hs2 hσ2 HSs w jx hcx => by + have hS1' : Fl4SpecA64.asidRootKind (stUpdA σ σ.curThread x .restart) (BitVec.ofNat 16 asid.val) = none ∨ + Fl4SpecA64.asidRootKind (stUpdA σ σ.curThread x .restart) (BitVec.ofNat 16 asid.val) = some .stage1 := by + rw [asidRootKind_stUpdA hk]; exact hS1 + have hU := unmap_pageA (ar := ar) (a := a) E1.j.1.vs (by rw [hks]; exact hK) + (fun p hp i hi => by rw [hks]; exact hT p i hi (Or.inr (Or.inr (Or.inl hp)))) E1.j.2.2.1.obj sz h16 + (hs1_ofE hS1') va fp + obtain ⟨s2u, fa, σu, hfa, hu, hσu, J2, hk2, hks2, hcpu2, UQ, hcase⟩ := unmap_post2A + (unmapPage_kmA _ _ _ _) (unmapPage_unQ _ _ _ _) E1.j (FootA.aligned E1.j.1 hN E1.scAl E1.ap.vcAl) + (fun q hq => (hTO q hq).of_ksA hks hcpu) E1.sys.ent.os hpOS hU + obtain ⟨s3, σ3, hsc, ⟨_, heq', hg⟩, hσ3, HSu, J3, T3⟩ := setcap_core2A E0 RS J2 (hOS1.trans' hk2) + (hks2.trans hks) (hcpu2.trans hcpu) cte hab hcap hEnc hca (UA.of_shapeA rfl fun h => by cases h) hok + (fun h => by cases h) (fun h => by cases h) hTr0 hnul0 + rw [← heq'] at hg + obtain ⟨a', hfa', hAR⟩ : ∃ a', fa = ok a' ∧ AR a' Z.hw := by + rcases hfa with rfl | rfl + · exact ⟨a, rfl, J3.1.ar⟩ + · exact spec_ex_fd (K.invPage a Z.hw asid va J3.1.ar) + have J3' := J3.of_arA hAR + have hdec' : vspace.decode_frame st ar s a fl4_abi.label.ARMPageUnmap length (cslotAddrA cte) + (.Frame fp sz rt dev true false asid va) call buf ex = ok (core.result.Result.Ok (), s3, a') := by + rw [page_unmap_eqA ar s a length (cslotAddrA cte) fp rt asid va sz dev false call buf ex h0, heq, bind_ok, + if_neg Bool.false_ne_true] + refine (then_setcapA hu hsc).trans ?_ + rw [hfa', bind_ok] + have hperf' := perfPgUnmapM_memA (R := vmRightsOfA rt) (dev := dev) hσu hg hσ3 + obtain ⟨Ac', hsl, hsy, hbx, hus, hj7⟩ := fin x s3 σ3 a' hk hdec' hperf' + have al_x : Aligned (cslotAddrA cte) := hO.al0 hx + have al_x8 : Aligned (wa (cslotAddrA cte) 8#u64) := hO.al hx (k := 8#u64) (by simp) (by simp) + obtain ⟨w0, w1, hen, -⟩ := CapRt.decode_encode _ hEnc + have wU : WritesIn st s1 s2u (fun b => ∃ p ∈ Z.S1, ∃ i : U64, i.val < 512 ∧ b = peAF p i) := by + rcases hcase with rfl | ⟨p, hp, i, hi, hal, rfl⟩ + · exact WritesIn.refl _ _ + · exact (writesIn_wr hal).mono fun b hb => ⟨p, hp, i, hi, hb⟩ + have wS := writesIn_setCapA al_x al_x8 hsc + have w2 := WritesIn.setCapCommA al_x al_x8 hen hs2 hsc wU + have hk3 : L.ksb s3 = L.ksb s1 := wS.1.trans hks2 + have hcpu3 : L.cpu s3 = L.cpu s1 := wS.2.1.trans hcpu2 + obtain ⟨Pt, Ub, vr, -, -, -, -, -, -, link, -, -⟩ := J1.j7 + obtain ⟨Qb, Rb, q⟩ := J1.sys.ent.bxi.1 + have vo : VrOK H.omega H.frame H.scs H.scBits OS Z.V ∅ (L.ksb s1) (utVsPtA st Pp Tb Lv s1) := J1.vsOk + have Psp : ∀ b, (∃ p ∈ Z.S1, ∃ i : U64, i.val < 512 ∧ b = peAF p i) → VsR (L.ksb s1) Pt b := + fun b ⟨p, hp, i, hi, e⟩ => by rw [e]; exact vsW_pageA link (Or.inr (Or.inl hp)) hi + have hzG : ∀ Pt' : Set U64, (∀ b, VsR (L.ksb s1) Pt' b → Aligned b) → + (∀ b, VsR (L.ksb s1) Pt' b → ¬ InOS OS b) → VsZ st (L.ksb s1) Pt' s1 s3 := fun Pt' hal hos b hb => by + have hb1 := hal b hb + have hns : ¬ SlotWordA (cslotAddrA cte) b := fun h => hos b hb (slotWord_inOS hO hx h) + rw [wS.mem_eq hb1 hns] + rcases hcase with rfl | ⟨p, hp, i, hi, hal', rfl⟩ + · exact Or.inl rfl + · by_cases e : peAF p i = b + · subst e; exact Or.inr (L.mem_wr_same hal') + · exact Or.inl (L.mem_wr_ne hal' hb1 (fun h => e (UScalar.eq_of_val_eq h))) + have hAsK : ∀ b, AsW (L.ksb s1) b → L.mem s3 b = L.mem s1 b := fun b hb => by + have hnP : ¬ ∃ p ∈ Z.S1, ∃ i : U64, i.val < 512 ∧ b = peAF p i := fun ⟨p, hp, i, hi, e⟩ => + hT p i hi (Or.inr (Or.inr (Or.inl hp))) (by rw [← e, ← hks]; exact hb) + have hns : ¬ SlotWordA (cslotAddrA cte) b := fun h => vo.os b (Or.inr hb) (slotWord_inOS hO hx h) + exact (wS.mem_eq (vo.al b (Or.inr hb)) hns).trans (wU.mem_eq (vo.al b (Or.inr hb)) hnP) + have vsT := VsTyR.zeroStepA hk3 hAsK (hzG _ vo.al vo.os) J1.sys.ent.vs + have osk3 : OSKeep st OS s2 s3 := + osKeep_ofA hO fun w' hw' hin => w2.mem_eq hw' (fun hp => vr.os w' (Psp w' hp) hin) + obtain ⟨hcx', hco⟩ := capA_setCapA hO hx hEnc hs2 + have capE3 : ∀ u ∈ OS, capA st s3 u = capA st s2 u := fun u hu => osk3.capA hu + have hbk : ∀ u ∈ OS, ∀ c, capA st s3 u = c → (∀ p sz r d mp e a v, c ≠ .Frame p sz r d mp e a v) → + capA st s1 u = c := fun u hu c hc hn => by + by_cases e : u = cslotAddrA cte + · subst e + rw [capE3 _ hx, hcx] at hc + exact absurd hc.symm (hn _ _ _ _ _ _ _ _) + · rw [capE3 u hu, hco u hu e] at hc + exact hc + have hMs2 : ∀ u ∈ OS, MapAlikeA (capA st s2 u) (capA st s1 u) := fun u hu => by + by_cases e : u = cslotAddrA cte + · subst e; rw [hcx, c1]; exact .frame .. + · rw [hco u hu e]; exact .refl _ + have sbS : ∀ t ∈ Z.T, sbWordA t ∈ OS := fun t ht => + RS.sub _ ⟨t, Or.inr ⟨(J1.j.1.thT t).2 ht, 2, by simp [tcbSlotsA], (bufSlot_eqA t).symm⟩⟩ + have wA : WritesIn st s1 s2 (fun b => Aligned b ∧ W7.CapW H.tcbs {b | InOS OS b} ∅ b) := writesIn_alAG w + have hB : ∀ b, (Aligned b ∧ W7.CapW H.tcbs {b | InOS OS b} ∅ b) → W7.CapW H.tcbs H.omega H.frame b := + fun b hb => os_capw hO hb.1 hb.2 + obtain ⟨-, -, -, -, -, fld, -, -⟩ := W7.blk_of_capw (BXI.blk J1.sys.ent.bxi) wA hB + have ib : ∀ t ∈ Z.T, L.mem s2 (ibWordA t) = L.mem s1 (ibWordA t) := fun t ht => + fld t (by rw [J1.tcbs]; exact ht) 3512#u64 (by simp) (by simp) (by simp) + have HS2 : VsSpecA σ2 σ3 := (VsSpecA.of_unQ UQ).setCapCommA hσ2 hσ3 + have offT : ∀ b, (∃ p ∈ Z.S1, ∃ i : U64, i.val < 512 ∧ b = peAF p i) → ∀ t ∈ Z.T, + b.val < t.val ∨ t.val + 4096 ≤ b.val := fun b hb t ht => by + have hth : t ∈ H.tcbs := by rw [J1.tcbs]; exact ht + have := (q.omega b (vr.om b (Psp b hb))).2.1 t hth + omega + have tw : ∀ t ∈ Z.T, ∀ K : U64, K.val + 8 ≤ 4096 → K.val % 8 = 0 → + L.mem s3 (wa t K) = L.mem s2 (wa t K) := by + intro t ht K hK' h8 + have hta := J1.sr.core.tcbs t ht + have hv : (wa t K).val = t.val + K.val := field_val hta (K := K) (by omega) + refine w2.mem_eq (field_aligned hta (by omega) h8) (fun h => ?_) + have := offT _ h t ht + omega + have hib : ∀ t ∈ Z.T, IbSame (L := L) s2 s3 t := fun t ht => by + refine ⟨tw t ht _ (by simp) (by simp), ?_, ?_⟩ + · rw [sbWordA_eq]; exact tw t ht _ (by simp) (by simp) + · rw [sbWordA8_eq]; exact tw t ht _ (by simp) (by simp) + have hj7' := hj7 + obtain ⟨Pt', Ub', vr', -, ac', -⟩ := hj7' + have hBw : ∀ b, ((∃ p ∈ Z.S1, ∃ i : U64, i.val < 512 ∧ b = peAF p i) ∨ SlotWordA (cslotAddrA cte) b) → + L.Free s1 H.omega Z.Cs b ∨ W7.TLow H.tcbs b := fun b hb => by + rcases hb with hp | hsl + · exact Or.inl (Or.inl (vr.om b (Psp b hp))) + · have hal : Aligned b := by rcases hsl with rfl | rfl; exact al_x; exact al_x8 + exact (os_capw hO hal (Or.inr ⟨slotWord_inOS hO hx hsl, Set.notMem_empty _⟩)).elim Or.inr + fun h' => Or.inl (Or.inl h'.1) + have hsp : ∀ q', SchedPageA st Z OS s3 q' → SchedPageA st Z OS s1 q' := fun q' h => SchedPageA.backA hbk h + refine ⟨s3, a', σ3, Z.hw, fun b => ∃ p ∈ Z.S1, ∃ i : U64, i.val < 512 ∧ b = peAF p i, Ac', + ⟨w2, fun Pt'' Ub'' vr'' link'' b ⟨p, hp, i, hi, e⟩ => by rw [e]; exact vsW_pageA link'' (Or.inr (Or.inl hp)) hi, + fun b ⟨p, hp, i, hi, e⟩ hbuf => J1.bufOff.page b hbuf p (Or.inr (Or.inl hp)) + (core.num.U64.wrapping_mul i 8#u64) (by rw [wm8_valA hi]; omega) (by rw [wm8_valA hi]; omega) + (by rw [e]; rfl), + HS2, ?_, ?_, J3', ⟨⟨J1.sys.ent.heap, hbx, hO, hus, vsT.1, by rw [hk3]; exact J1.sys.ent.ksw⟩, hsl⟩, + UtVsOkA.zeroStepA hk3 vsT.2 J1.vsOk, bufVs_subA J1.bufVs hk3 vsT.2, + W7.inv_of_capw J1.sq (wU.trans wS) hBw, ac'.case.po, + ?_, ?_, ?_, ?_, hj7, hsy⟩, hdec', hperf', rfl⟩ + · rw [setCap_archA hσ3, setCap_archA hσ2]; exact UQ.1.asidTable + · intro p dv pg h + have h1 : (stUpdA σ σ.curThread x .restart).kheap p = some (.archObj (.dataPage dv pg)) := + HSs.back h (fun _ _ e => by cases e) (fun _ e => by cases e) + have h2 : σu.kheap p = some (.archObj (.dataPage dv pg)) := by + rcases UQ.2 p with e | ⟨t0, t0', a0, -⟩ + · exact e.trans h1 + · rw [h1] at a0 + have h3 := Option.some.inj a0 + injection h3 with h3 + cases h3 + exact HSu.keep h2 (fun _ _ e => by cases e) (fun _ e => by cases e) + · exact BufCover.vsA (BufCover.mapA J1.bufCover J1.bufNative J1.j.1.st jx.1.st (fun t ht => (J1.j.1.thT t).2 ht) + HSs ib (fun t ht => hMs2 _ (sbS t ht))) HS2 hib + · exact BufNativeA.mapA J1.bufNative (fun t ht q' sz' rr dv' m' e' a'' v' h => by + by_cases e1 : sbWordA t = cslotAddrA cte + · rw [e1, capE3 _ hx, hcx] at h; cases h; rfl + · rw [capE3 _ (sbS t ht), hco _ (sbS t ht) e1] at h + have hn := J1.bufNative t ht + rw [bufSlot_eqA, capAtA_eq (st := st), h] at hn + exact hn) + · exact UnmPtSpecA.ofFrUnmMA (Pt := Pt) hbk + (fun q' hq i hi => hzG Pt vr.al vr.os _ (Or.inl ⟨q', hq, core.num.U64.wrapping_mul i 8#u64, + by rw [wm8_valA hi]; omega, by rw [wm8_valA hi]; omega, rfl⟩)) + link hk3 hcpu3 UQ.2.toX (fun q' ao h => HSu.keep h (fun _ _ e => by cases e) (fun _ e => by cases e)) J1.unmPt + · exact ⟨J1.bufOff.os, fun b hb => by rw [hk3]; exact J1.bufOff.vs b hb, + fun b hb q' hq => J1.bufOff.page b hb q' (hsp q' hq), J1.bufOff.vcw, + fun bp hbp => (J1.bufOff.pin bp hbp).keepM fun u hu => by rw [capE3 u hu]; exact hMs2 u hu⟩) + obtain ⟨s2, σ2, s3, a3, σ3, hw, P, Ac', hs2, hσk, HSsk, HSvk, HSc, wT, Jf, hdec, hperf, hhw⟩ := main + subst hhw + exact ⟨x, s3, a3, σ3, Ac', hk, hset, hdec, hperf, Jf, + (Sched.BodyKeepA.ofSlot HSsk (setCap_keepsA hσk).2.2).trans (Sched.BodyKeepA.ofVs HSvk)⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3ArmFrUnmXA.lean b/proofs/Fl4ProofsA64/P3/Stage3ArmFrUnmXA.lean new file mode 100644 index 000000000..64509cd87 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ArmFrUnmXA.lean @@ -0,0 +1,426 @@ +/- +P3 (AArch64, merge line, D8 step 5 (ii)): **closing the `ARMPageUnmap` arm of a mapped frame cap on an FL4 root** +(`page_unmap_decJA`: a Stage-2 root, or a high window; FL4's `ept::unmap` / `unmap_page` against `performExtVSpace`'s +`extUnmapPage`, then the cap write). The shape is `P3/Stage3ArmFrUnmMA`'s: the Ext unmap writes a zero into one table +page word (a Stage-2 page, a window's level-1 page, or a Stage-1 table) or nothing, and the cap write commutes with it. + +* `perform_pgUnmapXB`: `perform_pgUnmapXA` with the spec step shown to be `QvA` and `InvXA` + (`P3/Stage3UnmapExtSpecA`: the Ext slot write invalidates one entry of an arch object). +* `unmap_post2JA`: `unmap_postJA` with the written word exported. +* **`JStage3A.frameUnmapXA`**: the arm; arm hypotheses as `frameUnmapS1A` (`SysL`, `BXI` at some `Ac'`, `UserOK`, J7 at + the final state). + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3BodyKeepA +import Fl4ProofsA64.P3.Stage3ArmFrUnmZA +import Fl4ProofsA64.P3.Stage3UnmapExtSpecA +import Fl4ProofsA64.P3.ExtVsPerfA +import Fl4ProofsA64.P3.UnmapDecJA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-- The tables an Ext unmap may write: a Stage-2 page, a window's level-1 page, a Stage-1 table. -/ +def XqA (Z : XSetsA) (q : U64) : Prop := + q ∈ S2PgE Z.X Z.T2 ∨ (∃ y ∈ Z.Y, q = wa y 4096#u64) ∨ q ∈ Z.S1 + +/-- Such a page is a scheduler-or-VSpace page. -/ +theorem XqA.sched {S : Type} {st : machine.Store S} [L : LawfulStore st] {Z : XSetsA} {OS : Set U64} {s : S} {q : U64} + (h : XqA Z q) : SchedPageA st Z OS s q := by + unfold SchedPageA + rcases h with (h | ⟨r, hr, rfl | rfl⟩) | ⟨y, hy, rfl⟩ | h + · exact Or.inr (Or.inr (Or.inl h)) + · exact Or.inr (Or.inr (Or.inr (Or.inl hr))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨r, hr, rfl⟩)))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨y, hy, rfl⟩)))))) + · exact Or.inr (Or.inl h) + +theorem XqA.offAs {Z : XSetsA} {kb : U64} (hT : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 kb) {q j : U64} (hj : j.val < 512) + (h : XqA Z q) : ¬ AsW kb (peAF q j) := by + refine hT q j hj ?_ + rcases h with h | ⟨y, hy, e⟩ | h + · exact Or.inl h + · exact Or.inr (Or.inr (Or.inr ⟨y, hy, e⟩)) + · exact Or.inr (Or.inr (Or.inl h)) + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} + +/-- **`unmap_postJA` with the written word exported** (module docstring). -/ +theorem unmap_post2JA {ar : machine.Arch A} {fk : S → Result (S × A)} (hfk : ArchFlE ar a fk) {u : Result (S × A)} + {σu : Fl4SpecA64.State Unit} (J : JAtZAX (L := L) AR Z s a σ m) + (hfa : ∀ w, FootA (L := L) Z s w → Aligned w) (hTO : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s q) + (hEO : ExtTabOffA (L := L) Z m s) {T Ω Sg OS : Set U64} (hO : OSOK T Ω Sg OS) (hpOS : VsPgOffOSA Z OS) + (h : ExtUnmapRelE Z.Pp Z.S1 Z.Lv Z.X Z.Y Z.T2 Z.Sh2 σ st s a fk u ({((), σu)}, False)) : + ∃ (s2 : S) (fa : Result A), FlushE ar a fa ∧ u = (do let a1 ← fa; ok (s2, a1)) ∧ + JAtZAX (L := L) AR Z s2 a σu m ∧ OSKeep st OS s s2 ∧ L.ksb s2 = L.ksb s ∧ L.cpu s2 = L.cpu s ∧ + (s2 = s ∨ ∃ q j : U64, XqA Z q ∧ j.val < 512 ∧ Aligned (peAF q j) ∧ s2 = L.wr s (peAF q j) 0#u64) := by + rcases h with ⟨hu, hW⟩ | ⟨w, σ', hu, hW, hR, hE, hw⟩ + · obtain rfl := sing_stA hW + exact ⟨s, ok a, Or.inl rfl, by rw [hu, bind_ok], J, fun y _ => ⟨rfl, rfl, rfl, rfl⟩, rfl, rfl, Or.inl rfl⟩ + · obtain rfl := sing_stA hW + have J1 := J.of_extClearA hfa hTO hEO hw hR hE + obtain ⟨⟨q, j, hj, rfl, hq4, hq⟩, -⟩ := hw + have hal' : Aligned (peAF q j) := peAF_alignedA (Nat.mod_eq_zero_of_dvd hq4) hj + obtain ⟨fa, hfl, hfa'⟩ := hfk (L.wr s (peAF q j) 0#u64) + exact ⟨_, fa, hfl, hu.trans hfa', J1, osKeep_ofA hO fun w' hw' hin => by + rw [L.mem_wr _ _ _ _ hal' hw', if_neg fun (e : peAF q j = w') => hpOS q j hj hq (e ▸ hin)], + L.ksb_wr _ _ _, L.cpu_wr _ _ _, Or.inr ⟨q, j, hq, hj, hal', rfl⟩⟩ + +end + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {Pp Tb : Set U64} {Lv : U64 → Nat} {X Y T2 : Set U64} {Sh2 : U64 → Nat} {σ : Fl4SpecA64.State Unit} + +set_option maxHeartbeats 2000000 in +/-- **`perform_pgUnmapXA` with the spec step shown to invalidate entries only** (module docstring). -/ +theorem perform_pgUnmapXB {A : Type} {ar : machine.Arch A} {s : S} {a : A} + (R : VsRelA Pp Tb Lv X Y (L.mem s) (L.ksb s) σ) (E : ExtVsRelA Pp Tb X Y T2 Sh2 (L.mem s) σ) + (hK : Aligned (L.ksb s)) (hT : OffAsE Pp Tb X Y T2 (L.ksb s)) + (hX : ∀ r ∈ X, ∃ es, σ.kheap (specPtr r) = some (.archObj (.ext (.stage2Root es)))) + (hIpa : S2IpaE ar a) {asid : U64} (h16 : asid.val < 65536) (sz : capability.FrameSize) (va pptr : U64) + (isEpt : Bool) (hVa : isEpt = false → VaOkE va) (Rt : Fl4SpecA64.CapRights) (dev : Bool) + (k : Fl4SpecA64.ExtRootKind) + (hk : isEpt = false → Fl4SpecA64.asidRootKind σ (BitVec.ofNat 16 asid.val) = some k ∧ k ≠ .stage1) + (slot : Fl4SpecA64.CslotPtr) : + ∃ σu fk, ArchFlE ar a fk ∧ + Fl4SpecA64.performExtVSpace (Z := Unit) (.pageUnmap k (absFrameE isEpt (specPtr pptr) Rt (frameSizeOfA sz) dev + (some (BitVec.ofNat 16 asid.val, va.bv))) slot) σ = + (do Fl4SpecA64.setCap (Z := Unit) (.archObjectCap (Fl4SpecA64.extUnmappedFrame (absFrameE isEpt (specPtr pptr) + Rt (frameSizeOfA sz) dev (some (BitVec.ofNat 16 asid.val, va.bv))))) slot + return ([] : List Fl4SpecA64.Data)) σu ∧ + ExtUnmapRelE Pp Tb Lv X Y T2 Sh2 σ st s a fk + (if isEpt then (do let i ← capability.FrameSize.bits sz; ept.unmap st ar s a asid va pptr i false) + else vspace.unmap_page st ar s a sz asid va pptr) ({((), σu)}, False) ∧ + QvA σ σu ∧ InvXA σ σu := by + rcases unmap_frameXGE R E hK hT hX hIpa h16 sz va pptr isEpt hVa Rt dev true with + ⟨he, hn, -, -⟩ | ⟨σ', fk, hfk, hF, hU⟩ + · obtain ⟨hs, hk1⟩ := hk he + rcases hn with h | h <;> rw [h] at hs + · cases hs + · exact (hk1 (Option.some.inj hs).symm).elim + refine ⟨σ', fk, hfk, ?_, hU, ?_⟩ + · cases isEpt with + | true => + rw [show absFrameE true (specPtr pptr) Rt (frameSizeOfA sz) dev (some (BitVec.ofNat 16 asid.val, va.bv)) = + .ext (.stage2FrameCap (specPtr pptr) Rt (frameSizeOfA sz) dev (some (BitVec.ofNat 16 asid.val, va.bv))) from rfl, + perform_pgUnmapS2A k] + rw [show absFrameE true (specPtr pptr) Rt (frameSizeOfA sz) dev (some (BitVec.ofNat 16 asid.val, va.bv)) = + .ext (.stage2FrameCap (specPtr pptr) Rt (frameSizeOfA sz) dev (some (BitVec.ofNat 16 asid.val, va.bv))) from rfl, + fl4FinaliseCap_s2frame_runX] at hF + rw [sing_stA hF] + | false => + obtain ⟨hs, hk1⟩ := hk rfl + rw [show absFrameE false (specPtr pptr) Rt (frameSizeOfA sz) dev (some (BitVec.ofNat 16 asid.val, va.bv)) = + .frameCap (specPtr pptr) Rt (frameSizeOfA sz) dev (some (BitVec.ofNat 16 asid.val, va.bv)) from rfl, + perform_pgUnmapFrA k] + rw [show absFrameE false (specPtr pptr) Rt (frameSizeOfA sz) dev (some (BitVec.ofNat 16 asid.val, va.bv)) = + .frameCap (specPtr pptr) Rt (frameSizeOfA sz) dev (some (BitVec.ofNat 16 asid.val, va.bv)) from rfl] at hF + cases k with + | stage1 => exact (hk1 rfl).elim + | stage2 => + rw [if_neg (by decide), sing_stA (hF.symm.trans (fl4FinaliseCap_frame_s2X hs))] + | high => + rw [if_pos rfl, sing_stA (hF.symm.trans (fl4FinaliseCap_frame_runX hs))] + · cases isEpt with + | true => + rw [show absFrameE true (specPtr pptr) Rt (frameSizeOfA sz) dev (some (BitVec.ofNat 16 asid.val, va.bv)) = + .ext (.stage2FrameCap (specPtr pptr) Rt (frameSizeOfA sz) dev (some (BitVec.ofNat 16 asid.val, va.bv))) from rfl, + fl4FinaliseCap_s2frame_runX] at hF + have h1 := sing_stA hF + first | (rw [← h1]; exact extUnmapPageStX_invX _ _ _ _ _ _) | (rw [h1]; exact extUnmapPageStX_invX _ _ _ _ _ _) + | false => + obtain ⟨hs, hk1⟩ := hk rfl + rw [show absFrameE false (specPtr pptr) Rt (frameSizeOfA sz) dev (some (BitVec.ofNat 16 asid.val, va.bv)) = + .frameCap (specPtr pptr) Rt (frameSizeOfA sz) dev (some (BitVec.ofNat 16 asid.val, va.bv)) from rfl] at hF + cases k with + | stage1 => exact (hk1 rfl).elim + | stage2 => + have h1 := sing_stA (hF.symm.trans (fl4FinaliseCap_frame_s2X hs)) + first | (rw [h1]; exact ⟨QvA.refl _, InvXA.refl _⟩) | (rw [← h1]; exact ⟨QvA.refl _, InvXA.refl _⟩) + | high => + have h1 := sing_stA (hF.symm.trans (fl4FinaliseCap_frame_runX hs)) + first | (rw [h1]; exact extUnmapPageStX_invX _ _ _ _ _ _) | (rw [← h1]; exact extUnmapPageStX_invX _ _ _ _ _ _) + +end + + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +set_option maxHeartbeats 8000000 in +/-- **`JStage3A` across the `ARMPageUnmap` arm of a mapped frame cap on an FL4 root** (module docstring). -/ +theorem JStage3A.frameUnmapXA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {ar : machine.Arch A} {nA : U64} {s : S} {a : A} + {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (K : ArchKeepsA ar AR) + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) (hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM) (hTO : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s q) + (hEO : ExtTabOffA (L := L) Z m s) + (hK : Aligned (L.ksb s)) (hT : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s)) (hIpa : S2IpaE ar a) + (length buf : U64) (call : Bool) (ex : Aeneas.Std.Array obj.Slot 3#usize) + (cte : Fl4SpecA64.CslotPtr) (hab : AbsSlotA Z.th cte) (fp rt asid va : U64) (sz : capability.FrameSize) + (dev isEpt : Bool) (h0 : asid ≠ 0#u64) + (hcap : capA st s (cslotAddrA cte) = .Frame fp sz rt dev true isEpt asid va) (k : Fl4SpecA64.ExtRootKind) + (hkA : isEpt = false → Fl4SpecA64.asidRootKind σ (BitVec.ofNat 16 asid.val) = some k ∧ k ≠ .stage1) + (fin : ∀ (x : Fl4SpecA64.Tcb) (s3 : S) (σ3 : Fl4SpecA64.State Unit) (a' : A), + σ.kheap σ.curThread = some (.tcb x) → + vspace.decode_frame st ar s a fl4_abi.label.ARMPageUnmap length (cslotAddrA cte) + (.Frame fp sz rt dev true isEpt asid va) call buf ex = ok (core.result.Result.Ok (), s3, a') → + (([] : List Fl4SpecA64.Data), σ3) ∈ (Fl4SpecA64.performExtVSpace (Z := Unit) (.pageUnmap k + (absFrameE isEpt (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev + (some (BitVec.ofNat 16 asid.val, va.bv))) cte) (stUpdA σ σ.curThread x .restart)).1 → + ∃ Ac' : Set U64, SysL st H Z.Cs OS ia ie s3 ∧ SymL st H Z.Cs s3 ∧ + BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac' s3 ∧ + Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s3 ∧ + J7A st (J7R2A st ar nA) Z H OS s3 σ3 m) : + ∃ (x : Fl4SpecA64.Tcb) (s3 : S) (a' : A) (σ3 : Fl4SpecA64.State Unit) (Ac' : Set U64), + σ.kheap σ.curThread = some (.tcb x) ∧ + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + vspace.decode_frame st ar s a fl4_abi.label.ARMPageUnmap length (cslotAddrA cte) + (.Frame fp sz rt dev true isEpt asid va) call buf ex = ok (core.result.Result.Ok (), s3, a') ∧ + (([] : List Fl4SpecA64.Data), σ3) ∈ (Fl4SpecA64.performExtVSpace (Z := Unit) (.pageUnmap k + (absFrameE isEpt (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev + (some (BitVec.ofNat 16 asid.val, va.bv))) cte) (stUpdA σ σ.curThread x .restart)).1 ∧ + JStage3A st AR Z H OS U Ac' Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s3 a' σ3 m ∧ + Sched.BodyKeepA (stUpdA σ σ.curThread x .restart) σ3 := by + have hx : cslotAddrA cte ∈ OS := RS.sub _ (hab.isSlot J.j.1.place) + have hO : OSOK H.tcbs H.omega H.frame OS := J.sys.ent.os + have E0 := J.toJEntA + have hcap0 : capAtA (L.mem s) (cslotAddrA cte) = .Frame fp sz rt dev true isEpt asid va := hcap + have hVa : isEpt = false → VaOkE va := fun he' => by + subst he'; exact J.j.1.vaOkE (hab.isSlot J.j.1.place) hcap0 + obtain ⟨x, hk, heq, hset, J1, -, hOS1, -⟩ := J.of_restartCurA RS.pl.choose_spec.choose_spec.choose_spec.choose_spec + RS.sym RS.run hnvm RS.cur RS.vs RS.vc + have hks : L.ksb (L.wr s (stA (L.curW s)) sched.TS_RESTART) = L.ksb s := L.ksb_wr _ _ _ + have hcpu : L.cpu (L.wr s (stA (L.curW s)) sched.TS_RESTART) = L.cpu s := L.cpu_wr _ _ _ + generalize L.wr s (stA (L.curW s)) sched.TS_RESTART = s1 at heq J1 hOS1 hks hcpu + have c1 : capA st s1 (cslotAddrA cte) = .Frame fp sz rt dev true isEpt asid va := (hOS1.capA hx).trans hcap + obtain ⟨c0, -, hrel0⟩ := StateRelA.getAbsA J1.j.1.st hab + have c1' : capAtA (L.mem s1) (cslotAddrA cte) = .Frame fp sz rt dev true isEpt asid va := c1 + rw [c1'] at hrel0 + obtain ⟨⟨hr1, hr3, ⟨h16a, -⟩, hov⟩, -⟩ := hrel0 + have h16 : asid.val < 65536 := lt_of_lt_of_eq h16a (by norm_num) + have hEnc0 : CapRt.EncWf (capAtA (L.mem s1) (cslotAddrA cte)) := CapRt.decodeF_encWf _ _ + rw [c1'] at hEnc0 + have hcan : CapRt.Canonical fp ∧ rt.val < 4 := by + cases isEpt <;> (simp only [CapRt.EncWf] at hEnc0; exact ⟨hEnc0.1, hEnc0.2.1⟩) + have hEnc : CapRt.EncWf (.Frame fp sz rt dev false false 0#u64 0#u64) := by + simp only [CapRt.EncWf]; exact ⟨hcan.1, hcan.2, by simp, Or.inl fun j hj _ => by simp⟩ + have hca : CapRelA (.Frame fp sz rt dev false false 0#u64 0#u64) (.archObjectCap (Fl4SpecA64.extUnmappedFrame (absFrameE isEpt (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev (some (BitVec.ofNat 16 asid.val, va.bv))))) := + ⟨⟨hr1, hr3, by simp, hov⟩, by cases isEpt <;> rfl⟩ + have hok : CapObjOkA Z (.Frame fp sz rt dev false false 0#u64 0#u64) := + ⟨fun _ h => (by cases h), fun _ _ _ _ _ h => (by cases h), fun _ _ _ h => (by cases h), + fun _ _ _ h => (by cases h), (by intro h; cases h)⟩ + have hTr : TreeA st OS Z.G.chains Z.G.par s1 := + J1.j.1.cdt.tree.grow J1.j5.2.1 (fun x hx hn => J1.j5.2.2 x hx hn) + have hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s1 y = .Null := J1.j5.2.2 + have hTr0 : TreeA st OS Z.G.chains Z.G.par s := + J.j.1.cdt.tree.grow J.j5.2.1 (fun x hx hn => J.j5.2.2 x hx hn) + have hnul0 : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null := J.j5.2.2 + have E1 := J1.toJEntA + have hN := bxi_nodes E1.sys.ent.bxi + have hpOS := VsPgOffOSA.of_apA E1.ap + have hirq : CapOkA (stUpdA σ σ.curThread x .restart).interruptIrqNode cte (.archObjectCap (Fl4SpecA64.extUnmappedFrame (absFrameE isEpt (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev (some (BitVec.ofNat 16 asid.val, va.bv))))) := by + refine ⟨fun irq hlt e => ?_, fun ⟨_, _, _, _, e⟩ => by cases e⟩ + have obj : ∃ o ac, (stUpdA σ σ.curThread x .restart).kheap cte.1 = some o ∧ + Fl4SpecA64.capOf o cte.2 = some ac ∧ CapRelA (capAtA (L.mem s1) (cslotAddrA cte)) ac := by + obtain ⟨o, idx⟩ := cte + obtain ⟨p, e1, hc | ⟨ht, i, hi, e2⟩⟩ := hab + · simp only at e1 hc; subst e1 + obtain ⟨cs, hk, -, hr⟩ := J1.j.1.st.cnode p _ hc + obtain ⟨ac, hac, hrel⟩ := hr idx rfl + exact ⟨_, ac, hk, hac, hrel⟩ + · simp only at e1 e2; subst e1 e2 + obtain ⟨y, hk, hr⟩ := J1.j.1.st.tcb p ht + obtain ⟨ac, hac, hrel⟩ := hr i hi + refine ⟨_, ac, hk, hac, ?_⟩ + unfold cslotAddrA; rw [ofBl_tcbIdx i (by unfold tcbSlotsA at hi; omega)]; exact hrel + obtain ⟨o, ac, hko, hco, hrel⟩ := obj + rw [e] at hko + rw [c1'] at hrel + rcases J1.sinv.irqN.slot irq hlt o _ ac hko hco with rfl | ⟨_, _, _, rfl⟩ + · cases isEpt <;> cases hrel.2 + · cases isEpt <;> cases hrel.2 + have hsci : NoIdleCapV m n (.archObjectCap (Fl4SpecA64.extUnmappedFrame (absFrameE isEpt (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev (some (BitVec.ofNat 16 asid.val, va.bv))))) := + ⟨fun _ _ e => (by cases e), fun _ e => (by cases e), fun _ _ _ e => by cases isEpt <;> simp [Fl4SpecA64.extUnmappedFrame, absFrameE, Fl4SpecA64.updateMapData] at e⟩ + have main := J1.of_capVsFrameA (hpt := fun _ _ _ h => by cases h) + (Q := fun s' a' σ' hw => vspace.decode_frame st ar s a fl4_abi.label.ARMPageUnmap length (cslotAddrA cte) + (.Frame fp sz rt dev true isEpt asid va) call buf ex = ok (core.result.Result.Ok (), s', a') ∧ + (([] : List Fl4SpecA64.Data), σ') ∈ (Fl4SpecA64.performExtVSpace (Z := Unit) (.pageUnmap k + (absFrameE isEpt (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev + (some (BitVec.ofNat 16 asid.val, va.bv))) cte) (stUpdA σ σ.curThread x .restart)).1 ∧ hw = Z.hw) + RS.sub (footNOff_ksA rfl rfl (footN_offA E1)) (footNAl_ksA rfl rfl (FootNA.aligned E1.j.1 hN E1.scAl E1.ap.vcAl)) + cte hab hEnc hca hTr hnul (fun h => by cases h) (by rw [c1]; exact fun h => by cases h) + (c := .Frame fp sz rt dev false false 0#u64 0#u64) (by rw [c1]; exact .frame ..) hok hirq hsci + (fun s2 σ2 hs2 hσ2 HSs w jx hcx => by + have hk' : isEpt = false → Fl4SpecA64.asidRootKind (stUpdA σ σ.curThread x .restart) + (BitVec.ofNat 16 asid.val) = some k ∧ k ≠ .stage1 := fun he' => by + rw [asidRootKind_stUpdA hk]; exact hkA he' + obtain ⟨σu, fk, hfk, hperf, hU, QU, IU⟩ := perform_pgUnmapXB (ar := ar) (a := a) E1.j.1.vs E1.j.1.ext + (by rw [hks]; exact hK) (by rw [hks]; exact hT) E1.j.2.2.1.obj hIpa h16 sz va fp isEpt hVa + (vmRightsOfA rt) dev k hk' cte + obtain ⟨s2u, fa, hfl, hu, J2, hk2, hks2, hcpu2, hcase⟩ := unmap_post2JA hfk E1.j + (FootA.aligned E1.j.1 hN E1.scAl E1.ap.vcAl) (fun q hq => (hTO q hq).of_ksA hks hcpu) + (hEO.of_ksA hks hcpu) E1.sys.ent.os hpOS hU + obtain ⟨s3, σ3, hsc, -, hσ3, HSu, J3, T3⟩ := setcap_core2A E0 RS J2 (hOS1.trans' hk2) + (hks2.trans hks) (hcpu2.trans hcpu) cte hab hcap hEnc hca (UA.of_shapeA rfl fun h => by cases h) hok + (fun h => by cases h) (fun h => by cases h) hTr0 hnul0 + obtain ⟨a', hfa', hAR⟩ := spec_ex_fd (hfl.arA K J3.1.ar) + have J3' := J3.of_arA hAR + have hdec' : vspace.decode_frame st ar s a fl4_abi.label.ARMPageUnmap length (cslotAddrA cte) + (.Frame fp sz rt dev true isEpt asid va) call buf ex = ok (core.result.Result.Ok (), s3, a') := by + rw [page_unmap_eqA ar s a length (cslotAddrA cte) fp rt asid va sz dev isEpt call buf ex h0, heq, bind_ok] + refine (then_setcapA hu hsc).trans ?_ + rw [hfa', bind_ok] + have hperf' : (([] : List Fl4SpecA64.Data), σ3) ∈ (Fl4SpecA64.performExtVSpace (Z := Unit) (.pageUnmap k + (absFrameE isEpt (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev + (some (BitVec.ofNat 16 asid.val, va.bv))) cte) (stUpdA σ σ.curThread x .restart)).1 := by + rw [hperf]; exact ⟨((), σ3), hσ3, rfl⟩ + obtain ⟨Ac', hsl, hsy, hbx, hus, hj7⟩ := fin x s3 σ3 a' hk hdec' hperf' + have al_x : Aligned (cslotAddrA cte) := hO.al0 hx + have al_x8 : Aligned (wa (cslotAddrA cte) 8#u64) := hO.al hx (k := 8#u64) (by simp) (by simp) + obtain ⟨w0, w1, hen, -⟩ := CapRt.decode_encode _ hEnc + have wU : WritesIn st s1 s2u (fun b => ∃ q j : U64, XqA Z q ∧ j.val < 512 ∧ b = peAF q j) := by + rcases hcase with rfl | ⟨q, j, hq, hj, hal, rfl⟩ + · exact WritesIn.refl _ _ + · exact (writesIn_wr hal).mono fun b hb => ⟨q, j, hq, hj, hb⟩ + have wS := writesIn_setCapA al_x al_x8 hsc + have w2 := WritesIn.setCapCommA al_x al_x8 hen hs2 hsc wU + have hk3 : L.ksb s3 = L.ksb s1 := wS.1.trans hks2 + have hcpu3 : L.cpu s3 = L.cpu s1 := wS.2.1.trans hcpu2 + obtain ⟨Pt, Ub, vr, -, -, -, -, -, -, link, -, -⟩ := J1.j7 + obtain ⟨Qb, Rb, q0⟩ := J1.sys.ent.bxi.1 + have vo : VrOK H.omega H.frame H.scs H.scBits OS Z.V ∅ (L.ksb s1) (utVsPtA st Pp Tb Lv s1) := J1.vsOk + have Psp : ∀ b, (∃ q j : U64, XqA Z q ∧ j.val < 512 ∧ b = peAF q j) → VsR (L.ksb s1) Pt b := + fun b ⟨q, j, hq, hj, e⟩ => by rw [e]; exact vsW_pageA link hq.sched hj + have hzG : ∀ Pt' : Set U64, (∀ b, VsR (L.ksb s1) Pt' b → Aligned b) → + (∀ b, VsR (L.ksb s1) Pt' b → ¬ InOS OS b) → VsZ st (L.ksb s1) Pt' s1 s3 := fun Pt' hal hos b hb => by + have hb1 := hal b hb + have hns : ¬ SlotWordA (cslotAddrA cte) b := fun h => hos b hb (slotWord_inOS hO hx h) + rw [wS.mem_eq hb1 hns] + rcases hcase with rfl | ⟨q, j, hq, hj, hal', rfl⟩ + · exact Or.inl rfl + · by_cases e : peAF q j = b + · subst e; exact Or.inr (L.mem_wr_same hal') + · exact Or.inl (L.mem_wr_ne hal' hb1 (fun h => e (UScalar.eq_of_val_eq h))) + have hAsK : ∀ b, AsW (L.ksb s1) b → L.mem s3 b = L.mem s1 b := fun b hb => by + have hnP : ¬ ∃ q j : U64, XqA Z q ∧ j.val < 512 ∧ b = peAF q j := fun ⟨q, j, hq, hj, e⟩ => + hq.offAs hT hj (by rw [← e, ← hks]; exact hb) + have hns : ¬ SlotWordA (cslotAddrA cte) b := fun h => vo.os b (Or.inr hb) (slotWord_inOS hO hx h) + exact (wS.mem_eq (vo.al b (Or.inr hb)) hns).trans (wU.mem_eq (vo.al b (Or.inr hb)) hnP) + have vsT := VsTyR.zeroStepA hk3 hAsK (hzG _ vo.al vo.os) J1.sys.ent.vs + have osk3 : OSKeep st OS s2 s3 := + osKeep_ofA hO fun w' hw' hin => w2.mem_eq hw' (fun hp => vr.os w' (Psp w' hp) hin) + obtain ⟨hcx', hco⟩ := capA_setCapA hO hx hEnc hs2 + have capE3 : ∀ u ∈ OS, capA st s3 u = capA st s2 u := fun u hu => osk3.capA hu + have hbk : ∀ u ∈ OS, ∀ c, capA st s3 u = c → (∀ p sz r d mp e a v, c ≠ .Frame p sz r d mp e a v) → + capA st s1 u = c := fun u hu c hc hn => by + by_cases e : u = cslotAddrA cte + · subst e + rw [capE3 _ hx, hcx] at hc + exact absurd hc.symm (hn _ _ _ _ _ _ _ _) + · rw [capE3 u hu, hco u hu e] at hc + exact hc + have hMs2 : ∀ u ∈ OS, MapAlikeA (capA st s2 u) (capA st s1 u) := fun u hu => by + by_cases e : u = cslotAddrA cte + · subst e; rw [hcx, c1]; exact .frame .. + · rw [hco u hu e]; exact .refl _ + have sbS : ∀ t ∈ Z.T, sbWordA t ∈ OS := fun t ht => + RS.sub _ ⟨t, Or.inr ⟨(J1.j.1.thT t).2 ht, 2, by simp [tcbSlotsA], (bufSlot_eqA t).symm⟩⟩ + have wA : WritesIn st s1 s2 (fun b => Aligned b ∧ W7.CapW H.tcbs {b | InOS OS b} ∅ b) := writesIn_alAG w + have hB : ∀ b, (Aligned b ∧ W7.CapW H.tcbs {b | InOS OS b} ∅ b) → W7.CapW H.tcbs H.omega H.frame b := + fun b hb => os_capw hO hb.1 hb.2 + obtain ⟨-, -, -, -, -, fld, -, -⟩ := W7.blk_of_capw (BXI.blk J1.sys.ent.bxi) wA hB + have ib : ∀ t ∈ Z.T, L.mem s2 (ibWordA t) = L.mem s1 (ibWordA t) := fun t ht => + fld t (by rw [J1.tcbs]; exact ht) 3512#u64 (by simp) (by simp) (by simp) + have HS2 : VsSpecA σ2 σ3 := (VsSpecA.of_invX QU IU).setCapCommA hσ2 hσ3 + have offT : ∀ b, (∃ q j : U64, XqA Z q ∧ j.val < 512 ∧ b = peAF q j) → ∀ t ∈ Z.T, + b.val < t.val ∨ t.val + 4096 ≤ b.val := fun b hb t ht => by + have hth : t ∈ H.tcbs := by rw [J1.tcbs]; exact ht + have := (q0.omega b (vr.om b (Psp b hb))).2.1 t hth + omega + have tw : ∀ t ∈ Z.T, ∀ K : U64, K.val + 8 ≤ 4096 → K.val % 8 = 0 → + L.mem s3 (wa t K) = L.mem s2 (wa t K) := by + intro t ht K hK' h8 + have hta := J1.sr.core.tcbs t ht + have hv : (wa t K).val = t.val + K.val := field_val hta (K := K) (by omega) + refine w2.mem_eq (field_aligned hta (by omega) h8) (fun h => ?_) + have := offT _ h t ht + omega + have hib : ∀ t ∈ Z.T, IbSame (L := L) s2 s3 t := fun t ht => by + refine ⟨tw t ht _ (by simp) (by simp), ?_, ?_⟩ + · rw [sbWordA_eq]; exact tw t ht _ (by simp) (by simp) + · rw [sbWordA8_eq]; exact tw t ht _ (by simp) (by simp) + have hj7' := hj7 + obtain ⟨Pt', Ub', vr', -, ac', -⟩ := hj7' + have hBw : ∀ b, ((∃ q j : U64, XqA Z q ∧ j.val < 512 ∧ b = peAF q j) ∨ SlotWordA (cslotAddrA cte) b) → + L.Free s1 H.omega Z.Cs b ∨ W7.TLow H.tcbs b := fun b hb => by + rcases hb with hp | hsl + · exact Or.inl (Or.inl (vr.om b (Psp b hp))) + · have hal : Aligned b := by rcases hsl with rfl | rfl; exact al_x; exact al_x8 + exact (os_capw hO hal (Or.inr ⟨slotWord_inOS hO hx hsl, Set.notMem_empty _⟩)).elim Or.inr + fun h' => Or.inl (Or.inl h'.1) + have hsp : ∀ q', SchedPageA st Z OS s3 q' → SchedPageA st Z OS s1 q' := fun q' h => SchedPageA.backA hbk h + refine ⟨s3, a', σ3, Z.hw, fun b => ∃ q j : U64, XqA Z q ∧ j.val < 512 ∧ b = peAF q j, Ac', + ⟨w2, fun Pt'' Ub'' vr'' link'' b ⟨q, j, hq, hj, e⟩ => by rw [e]; exact vsW_pageA link'' hq.sched hj, + fun b ⟨q, j, hq, hj, e⟩ hbuf => J1.bufOff.page b hbuf q hq.sched + (core.num.U64.wrapping_mul j 8#u64) (by rw [wm8_valA hj]; omega) (by rw [wm8_valA hj]; omega) + (by rw [e]; rfl), + HS2, ?_, ?_, J3', ⟨⟨J1.sys.ent.heap, hbx, hO, hus, vsT.1, by rw [hk3]; exact J1.sys.ent.ksw⟩, hsl⟩, + UtVsOkA.zeroStepA hk3 vsT.2 J1.vsOk, bufVs_subA J1.bufVs hk3 vsT.2, + W7.inv_of_capw J1.sq (wU.trans wS) hBw, ac'.case.po, + ?_, ?_, ?_, ?_, hj7, hsy⟩, hdec', hperf', rfl⟩ + · rw [setCap_archA hσ3, setCap_archA hσ2]; exact QU.asidTable + · intro p dv pg h + have h1 : (stUpdA σ σ.curThread x .restart).kheap p = some (.archObj (.dataPage dv pg)) := + HSs.back h (fun _ _ e => by cases e) (fun _ e => by cases e) + have h2 : σu.kheap p = some (.archObj (.dataPage dv pg)) := by + rcases IU p with e | ⟨o, o', a0, b0, hn⟩ + · exact e.trans h1 + · rw [h1] at a0 + have h3 : o = .dataPage dv pg := by + have := Option.some.inj a0 + injection this with h4 + exact h4.symm + subst h3 + exact absurd hn (by simp [ArchInvA]) + exact HSu.keep h2 (fun _ _ e => by cases e) (fun _ e => by cases e) + · exact BufCover.vsA (BufCover.mapA J1.bufCover J1.bufNative J1.j.1.st jx.1.st (fun t ht => (J1.j.1.thT t).2 ht) + HSs ib (fun t ht => hMs2 _ (sbS t ht))) HS2 hib + · exact BufNativeA.mapA J1.bufNative (fun t ht q' sz' rr dv' m' e' a'' v' h => by + by_cases e1 : sbWordA t = cslotAddrA cte + · rw [e1, capE3 _ hx, hcx] at h; cases h; rfl + · rw [capE3 _ (sbS t ht), hco _ (sbS t ht) e1] at h + have hn := J1.bufNative t ht + rw [bufSlot_eqA, capAtA_eq (st := st), h] at hn + exact hn) + · exact UnmPtSpecA.ofFrUnmMA (Pt := Pt) hbk + (fun q' hq i hi => hzG Pt vr.al vr.os _ (Or.inl ⟨q', hq, core.num.U64.wrapping_mul i 8#u64, + by rw [wm8_valA hi]; omega, by rw [wm8_valA hi]; omega, rfl⟩)) + link hk3 hcpu3 IU (fun q' ao h => HSu.keep h (fun _ _ e => by cases e) (fun _ e => by cases e)) J1.unmPt + · exact ⟨J1.bufOff.os, fun b hb => by rw [hk3]; exact J1.bufOff.vs b hb, + fun b hb q' hq => J1.bufOff.page b hb q' (hsp q' hq), J1.bufOff.vcw, + fun bp hbp => (J1.bufOff.pin bp hbp).keepM fun u hu => by rw [capE3 u hu]; exact hMs2 u hu⟩) + obtain ⟨s2, σ2, s3, a3, σ3, hw, P, Ac', hs2, hσk, HSsk, HSvk, HSc, wT, Jf, hdec, hperf, hhw⟩ := main + subst hhw + exact ⟨x, s3, a3, σ3, Ac', hk, hset, hdec, hperf, Jf, + (Sched.BodyKeepA.ofSlot HSsk (setCap_keepsA hσk).2.2).trans (Sched.BodyKeepA.ofVs HSvk)⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3ArmFrUnmZA.lean b/proofs/Fl4ProofsA64/P3/Stage3ArmFrUnmZA.lean new file mode 100644 index 000000000..8c1dbe05f --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ArmFrUnmZA.lean @@ -0,0 +1,277 @@ +/- +P3 (AArch64, merge line, D8 step 5 (ii)): **the ASID-0 `ARMPageUnmap` arms** (`page_unmapZ_decJA`: a plain frame mapped +with ASID 0; `page_unmapS2Z_decJA`: the same for a Stage-2 frame). FL4 skips the unmap and writes the unmapped cap, the +spec's unmap is a no-op, so the arm is the Restart and one in-place cap write: `of_restartCurA` then `of_capFrame`, as +`frameUnmapUA`, but the old cap is a *mapped* frame, so J7 is not derived: it and `SysL` at the final state are the arm +hypothesis `fin` (T3). `UnmPtSpecA.ofFrUnmMA` gives J9 and `SchedPageA.backA` the buffer placement. + +`JStage3A.frameClearZA` is the common core; the FL4 decode equation and the spec perform membership of each arm are +supplied by the caller's `hD` / `hPf` (the arm's `page_unmap0_eqA` / `page_unmapS20_eqA` and `perfPgUnmapM_memA` / +`perfS2Unmap0_memA`). + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3BodyKeepA +import Fl4ProofsA64.P3.Stage3ArmFrUnmMA +import Fl4ProofsA64.P3.UnmapPtS1JA +import Fl4ProofsA64.P3.VsTopFrA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +set_option maxHeartbeats 4000000 in +/-- **`JStage3A` across a cap-only unmap of a mapped frame** (module docstring). -/ +theorem JStage3A.frameClearZA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {ar : machine.Arch A} {nA : U64} {s : S} {a : A} + {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) (hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM) (cte : Fl4SpecA64.CslotPtr) (hab : AbsSlotA Z.th cte) + (fp rt va : U64) (sz : capability.FrameSize) (dev isEpt : Bool) + (hcap : capA st s (cslotAddrA cte) = .Frame fp sz rt dev true isEpt 0#u64 va) + {D : S → Prop} {Pf : Fl4SpecA64.Tcb → Fl4SpecA64.State Unit → Prop} + (hD : ∀ s1 s3, sched.set_thread_state st s (L.curW s) sched.TS_RESTART = ok s1 → + obj.Slot.set_cap st (cslotAddrA cte) s1 (.Frame fp sz rt dev false false 0#u64 0#u64) = ok s3 → D s3) + (hPf : ∀ (x : Fl4SpecA64.Tcb) (σ3 : Fl4SpecA64.State Unit) c', σ.kheap σ.curThread = some (.tcb x) → + CapEqA (.Frame fp sz rt dev true isEpt 0#u64 va) c' → + (c', stUpdA σ σ.curThread x .restart) ∈ (Fl4SpecA64.getCap (Z := Unit) cte (stUpdA σ σ.curThread x .restart)).1 → + ((), σ3) ∈ (Fl4SpecA64.setCap (Z := Unit) + (.archObjectCap (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev none)) cte + (stUpdA σ σ.curThread x .restart)).1 → Pf x σ3) + (fin : ∀ (x : Fl4SpecA64.Tcb) (s3 : S) (σ3 : Fl4SpecA64.State Unit), σ.kheap σ.curThread = some (.tcb x) → + obj.Slot.set_cap st (cslotAddrA cte) (L.wr s (stA (L.curW s)) sched.TS_RESTART) + (.Frame fp sz rt dev false false 0#u64 0#u64) = ok s3 → + ((), σ3) ∈ (Fl4SpecA64.setCap (Z := Unit) + (.archObjectCap (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev none)) cte + (stUpdA σ σ.curThread x .restart)).1 → + SysL st H Z.Cs OS ia ie s3 ∧ SymL st H Z.Cs s3 ∧ J7A st (J7R2A st ar nA) Z H OS s3 σ3 m) : + ∃ (x : Fl4SpecA64.Tcb) (s3 : S) (σ3 : Fl4SpecA64.State Unit), σ.kheap σ.curThread = some (.tcb x) ∧ + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ D s3 ∧ Pf x σ3 ∧ + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s3 a σ3 m ∧ + Sched.BodyKeepA (stUpdA σ σ.curThread x .restart) σ3 := by + have hx : cslotAddrA cte ∈ OS := RS.sub _ (hab.isSlot J.j.1.place) + have hO : OSOK H.tcbs H.omega H.frame OS := J.sys.ent.os + obtain ⟨x, hk, heq, hset, J1, -, hOS1, -⟩ := J.of_restartCurA RS.pl.choose_spec.choose_spec.choose_spec.choose_spec + RS.sym RS.run hnvm RS.cur RS.vs RS.vc + have c1 : capA st (L.wr s (stA (L.curW s)) sched.TS_RESTART) (cslotAddrA cte) = + .Frame fp sz rt dev true isEpt 0#u64 va := (hOS1.capA hx).trans hcap + obtain ⟨c', hg, hP0, hceq⟩ := StateRelA.getAbsA J1.j.1.st hab + have c1' : capAtA (L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART)) (cslotAddrA cte) = + .Frame fp sz rt dev true isEpt 0#u64 va := c1 + rw [c1'] at hP0 hceq + obtain ⟨hr1, hr3, -, hov⟩ := hP0 + have hEnc0 : CapRt.EncWf (capAtA (L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART)) (cslotAddrA cte)) := + CapRt.decodeF_encWf _ _ + rw [c1'] at hEnc0 + have hcan : CapRt.Canonical fp ∧ rt.val < 4 := by + cases isEpt <;> (simp only [CapRt.EncWf] at hEnc0; exact ⟨hEnc0.1, hEnc0.2.1⟩) + have hEnc : CapRt.EncWf (.Frame fp sz rt dev false false 0#u64 0#u64) := by + simp only [CapRt.EncWf]; exact ⟨hcan.1, hcan.2, by simp, Or.inl fun j hj _ => by simp⟩ + have hca : CapRelA (.Frame fp sz rt dev false false 0#u64 0#u64) + (.archObjectCap (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev none)) := + ⟨⟨hr1, hr3, by simp, hov⟩, rfl⟩ + have hok : CapObjOkA Z (.Frame fp sz rt dev false false 0#u64 0#u64) := + ⟨fun _ h => (by cases h), fun _ _ _ _ _ h => (by cases h), fun _ _ _ h => (by cases h), + fun _ _ _ h => (by cases h), (by intro h; cases h)⟩ + have hTr : TreeA st OS Z.G.chains Z.G.par (L.wr s (stA (L.curW s)) sched.TS_RESTART) := + J1.j.1.cdt.tree.grow J1.j5.2.1 (fun x hx hn => J1.j5.2.2 x hx hn) + have hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st (L.wr s (stA (L.curW s)) sched.TS_RESTART) y = .Null := + J1.j5.2.2 + have E1 := J1.toJEntA + have hN := bxi_nodes E1.sys.ent.bxi + have hirq : CapOkA (stUpdA σ σ.curThread x .restart).interruptIrqNode cte + (.archObjectCap (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev none)) := by + refine ⟨fun irq hlt e => ?_, fun ⟨_, _, _, _, e⟩ => by cases e⟩ + have obj : ∃ o ac, (stUpdA σ σ.curThread x .restart).kheap cte.1 = some o ∧ + Fl4SpecA64.capOf o cte.2 = some ac ∧ + CapRelA (capAtA (L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART)) (cslotAddrA cte)) ac := by + obtain ⟨o, idx⟩ := cte + obtain ⟨p, e1, hc | ⟨ht, i, hi, e2⟩⟩ := hab + · simp only at e1 hc; subst e1 + obtain ⟨cs, hk, -, hr⟩ := J1.j.1.st.cnode p _ hc + obtain ⟨ac, hac, hrel⟩ := hr idx rfl + exact ⟨_, ac, hk, hac, hrel⟩ + · simp only at e1 e2; subst e1 e2 + obtain ⟨y, hk, hr⟩ := J1.j.1.st.tcb p ht + obtain ⟨ac, hac, hrel⟩ := hr i hi + refine ⟨_, ac, hk, hac, ?_⟩ + unfold cslotAddrA; rw [ofBl_tcbIdx i (by unfold tcbSlotsA at hi; omega)]; exact hrel + obtain ⟨o, ac, hko, hco, hrel⟩ := obj + rw [e] at hko + rw [c1'] at hrel + rcases J1.sinv.irqN.slot irq hlt o _ ac hko hco with rfl | ⟨_, _, _, rfl⟩ + · cases isEpt <;> cases hrel.2 + · cases isEpt <;> cases hrel.2 + have hsci : NoIdleCapV m n (.archObjectCap (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev none)) := + ⟨fun _ _ e => (by cases e), fun _ e => (by cases e), fun _ _ _ e => (by cases e)⟩ + obtain ⟨s2, σ2, hs2, hσ2, HSsk, -, J2⟩ := J1.of_capFrame (hpt := fun _ _ _ h => by cases h) RS.sub + (footNOff_ksA rfl rfl (footN_offA E1)) (footNAl_ksA rfl rfl (FootNA.aligned E1.j.1 hN E1.scAl E1.ap.vcAl)) + cte hab hEnc hca hTr hnul (fun h => by cases h) (by rw [c1]; exact fun h => by cases h) + (c := .Frame fp sz rt dev false false 0#u64 0#u64) (by rw [c1]; exact .frame ..) hok hirq hsci + (fun s2 σ2 hs2 hσ2 HS w jx hc2 => by + obtain ⟨hsl, hsy, hj7⟩ := fin x s2 σ2 hk hs2 hσ2 + obtain ⟨hcx, hco⟩ := capA_setCapA hO hx hEnc hs2 + have hbk : ∀ u ∈ OS, ∀ c, capA st s2 u = c → (∀ p sz r d mp e a v, c ≠ .Frame p sz r d mp e a v) → + capA st (L.wr s (stA (L.curW s)) sched.TS_RESTART) u = c := fun u hu c hc hn => by + by_cases e : u = cslotAddrA cte + · subst e; rw [hcx] at hc; exact absurd hc.symm (hn _ _ _ _ _ _ _ _) + · rw [hco u hu e] at hc; exact hc + have hMs2 : ∀ u ∈ OS, MapAlikeA (capA st s2 u) + (capA st (L.wr s (stA (L.curW s)) sched.TS_RESTART) u) := fun u hu => by + by_cases e : u = cslotAddrA cte + · subst e; rw [hcx, c1]; exact .frame .. + · rw [hco u hu e]; exact .refl _ + have sbS : ∀ t ∈ Z.T, sbWordA t ∈ OS := fun t ht => + RS.sub _ ⟨t, Or.inr ⟨(J1.j.1.thT t).2 ht, 2, by simp [tcbSlotsA], (bufSlot_eqA t).symm⟩⟩ + have wA : WritesIn st (L.wr s (stA (L.curW s)) sched.TS_RESTART) s2 + (fun b => Aligned b ∧ W7.CapW H.tcbs {b | InOS OS b} ∅ b) := writesIn_alAG w + have hB : ∀ b, (Aligned b ∧ W7.CapW H.tcbs {b | InOS OS b} ∅ b) → W7.CapW H.tcbs H.omega H.frame b := + fun b hb => os_capw hO hb.1 hb.2 + obtain ⟨-, -, -, -, -, fld, -, -⟩ := W7.blk_of_capw (BXI.blk J1.sys.ent.bxi) wA hB + have ib : ∀ t ∈ Z.T, L.mem s2 (ibWordA t) = L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART) (ibWordA t) := + fun t ht => fld t (by rw [J1.tcbs]; exact ht) 3512#u64 (by simp) (by simp) (by simp) + obtain ⟨Pt, Ub, vr, -, -, -, -, -, -, link, -, -⟩ := J1.j7 + have hksw : L.ksb s2 = L.ksb (L.wr s (stA (L.curW s)) sched.TS_RESTART) := w.1 + have hsp : ∀ q', SchedPageA st Z OS s2 q' → + SchedPageA st Z OS (L.wr s (stA (L.curW s)) sched.TS_RESTART) q' := fun q' h => SchedPageA.backA hbk h + exact + { sysl := hsl + symL := hsy + unmPt := UnmPtSpecA.ofFrUnmMA (Pt := Pt) (σu := stUpdA σ σ.curThread x .restart) hbk + (fun q' hq i hi => Or.inl (w.mem_eq (vr.al _ (Or.inl ⟨q', hq, core.num.U64.wrapping_mul i 8#u64, + by rw [wm8_valA hi]; omega, by rw [wm8_valA hi]; omega, rfl⟩)) + (fun h => J1.vsr_offCapWA vr (Or.inl ⟨q', hq, core.num.U64.wrapping_mul i 8#u64, + by rw [wm8_valA hi]; omega, by rw [wm8_valA hi]; omega, rfl⟩) h))) + link hksw w.2.1 (InvXA.refl _) + (fun q' ao h => HS.keep (σ := stUpdA σ σ.curThread x .restart) h (fun _ _ e => by cases e) + (fun _ e => by cases e)) J1.unmPt + bufOff := ⟨J1.bufOff.os, fun b hb => by rw [hksw]; exact J1.bufOff.vs b hb, + fun b hb q' hq => J1.bufOff.page b hb q' (hsp q' hq), J1.bufOff.vcw, + fun bp hbp => (J1.bufOff.pin bp hbp).keepF fun t ht hO _ _ r x1 x2 x3 x4 e => ⟨ht, hO, by + by_cases e1 : sbWordA t = cslotAddrA cte + · rw [e1, c1] at e; cases e; exact ⟨_, _, _, _, _, by rw [e1]; exact hcx⟩ + · exact ⟨r, x1, x2, x3, x4, by rw [hco _ hO e1]; exact e⟩⟩⟩ + bufCover := BufCover.mapA J1.bufCover J1.bufNative J1.j.1.st jx.1.st + (fun t ht => (J1.j.1.thT t).2 ht) HS ib (fun t ht => hMs2 _ (sbS t ht)) + bufNative := BufNativeA.mapA J1.bufNative (fun t ht q' sz' rr dv' m' e' a'' v' h => by + by_cases e1 : sbWordA t = cslotAddrA cte + · rw [e1, hcx] at h; cases h; rfl + · rw [hco _ (sbS t ht) e1] at h + have hn := J1.bufNative t ht + rw [bufSlot_eqA, capAtA_eq (st := st), h] at hn + exact hn) + j7 := hj7 }) + have hc'' : (c', stUpdA σ σ.curThread x .restart) ∈ + (Fl4SpecA64.getCap (Z := Unit) cte (stUpdA σ σ.curThread x .restart)).1 := hg + exact ⟨x, s2, σ2, hk, hset, hD _ _ heq hs2, hPf x σ2 c' hk hceq hc'' hσ2, J2, + Sched.BodyKeepA.ofSlot HSsk (setCap_keepsA hσ2).2.2⟩ + + +/-- **`ARMPageUnmap` of a mapped frame with ASID 0, plain** (module docstring). -/ +theorem JStage3A.frameUnmapZA + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {ar : machine.Arch A} {nA : U64} {s : S} {a : A} + {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) (hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM) (length buf : U64) (call : Bool) + (ex : Aeneas.Std.Array obj.Slot 3#usize) (cte : Fl4SpecA64.CslotPtr) (hab : AbsSlotA Z.th cte) + (fp rt va : U64) (sz : capability.FrameSize) (dev : Bool) + (hcap : capA st s (cslotAddrA cte) = .Frame fp sz rt dev true false 0#u64 va) + (fin : ∀ (x : Fl4SpecA64.Tcb) (s3 : S) (σ3 : Fl4SpecA64.State Unit), σ.kheap σ.curThread = some (.tcb x) → + obj.Slot.set_cap st (cslotAddrA cte) (L.wr s (stA (L.curW s)) sched.TS_RESTART) + (.Frame fp sz rt dev false false 0#u64 0#u64) = ok s3 → + ((), σ3) ∈ (Fl4SpecA64.setCap (Z := Unit) + (.archObjectCap (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev none)) cte + (stUpdA σ σ.curThread x .restart)).1 → + SysL st H Z.Cs OS ia ie s3 ∧ SymL st H Z.Cs s3 ∧ J7A st (J7R2A st ar nA) Z H OS s3 σ3 m) : + ∃ (x : Fl4SpecA64.Tcb) (s3 : S) (σ3 : Fl4SpecA64.State Unit), σ.kheap σ.curThread = some (.tcb x) ∧ + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + vspace.decode_frame st ar s a fl4_abi.label.ARMPageUnmap length (cslotAddrA cte) + (.Frame fp sz rt dev true false 0#u64 va) call buf ex = ok (core.result.Result.Ok (), s3, a) ∧ + (([] : List Fl4SpecA64.Data), σ3) ∈ (Fl4SpecA64.performPageInvocation (Z := Unit) + (.pageUnmap (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev + (some (BitVec.ofNat 16 (0#u64 : U64).val, va.bv))) cte) (stUpdA σ σ.curThread x .restart)).1 ∧ + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s3 a σ3 m ∧ + Sched.BodyKeepA (stUpdA σ σ.curThread x .restart) σ3 := by + have e0 : (0#u64 : U64).val = 0 := rfl + obtain ⟨x, s3, σ3, hk, hset, hD, hP, J3, K3⟩ := JStage3A.frameClearZA (ar := ar) (nA := nA) J RS hnvm cte hab fp rt va sz dev + false hcap + (D := fun s3 => vspace.decode_frame st ar s a fl4_abi.label.ARMPageUnmap length (cslotAddrA cte) + (.Frame fp sz rt dev true false 0#u64 va) call buf ex = ok (core.result.Result.Ok (), s3, a)) + (Pf := fun x σ3 => (([] : List Fl4SpecA64.Data), σ3) ∈ (Fl4SpecA64.performPageInvocation (Z := Unit) + (.pageUnmap (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev + (some (BitVec.ofNat 16 (0#u64 : U64).val, va.bv))) cte) (stUpdA σ σ.curThread x .restart)).1) + (fun s1 s3 heq hsc => by + rw [page_unmap0_eqA ar s a length (cslotAddrA cte) fp rt va sz dev call buf ex, heq, bind_ok, hsc, bind_ok]) + (fun x σ3 c' hk hceq hg hσ3 => by + have hu0 : ((), stUpdA σ σ.curThread x .restart) ∈ (Fl4SpecA64.unmapPage (Z := Unit) (frameSizeOfA sz) + (BitVec.ofNat 16 (0#u64 : U64).val) va.bv (specPtr fp) (stUpdA σ σ.curThread x .restart)).1 := by + rw [e0]; exact unmapPage_zeroA _ _ _ _ + rw [← hceq] at hg + exact perfPgUnmapM_memA (R := vmRightsOfA rt) (dev := dev) hu0 hg hσ3) + fin + exact ⟨x, s3, σ3, hk, hset, hD, hP, J3, K3⟩ + +/-- **`ARMPageUnmap` of a mapped frame with ASID 0, Stage-2** (module docstring). -/ +theorem JStage3A.frameUnmapS2ZA + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {ar : machine.Arch A} {nA : U64} {s : S} {a : A} + {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) (hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM) (length buf : U64) (call : Bool) + (ex : Aeneas.Std.Array obj.Slot 3#usize) (cte : Fl4SpecA64.CslotPtr) (hab : AbsSlotA Z.th cte) + (fp rt va : U64) (sz : capability.FrameSize) (dev : Bool) + (hcap : capA st s (cslotAddrA cte) = .Frame fp sz rt dev true true 0#u64 va) + (fin : ∀ (x : Fl4SpecA64.Tcb) (s3 : S) (σ3 : Fl4SpecA64.State Unit), σ.kheap σ.curThread = some (.tcb x) → + obj.Slot.set_cap st (cslotAddrA cte) (L.wr s (stA (L.curW s)) sched.TS_RESTART) + (.Frame fp sz rt dev false false 0#u64 0#u64) = ok s3 → + ((), σ3) ∈ (Fl4SpecA64.setCap (Z := Unit) + (.archObjectCap (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev none)) cte + (stUpdA σ σ.curThread x .restart)).1 → + SysL st H Z.Cs OS ia ie s3 ∧ SymL st H Z.Cs s3 ∧ J7A st (J7R2A st ar nA) Z H OS s3 σ3 m) : + ∃ (x : Fl4SpecA64.Tcb) (s3 : S) (σ3 : Fl4SpecA64.State Unit), σ.kheap σ.curThread = some (.tcb x) ∧ + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + vspace.decode_frame st ar s a fl4_abi.label.ARMPageUnmap length (cslotAddrA cte) + (.Frame fp sz rt dev true true 0#u64 va) call buf ex = ok (core.result.Result.Ok (), s3, a) ∧ + (([] : List Fl4SpecA64.Data), σ3) ∈ (Fl4SpecA64.performExtVSpace (Z := Unit) + (.pageUnmap .stage2 (.ext (.stage2FrameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev + (some (BitVec.ofNat 16 (0#u64 : U64).val, va.bv)))) cte) (stUpdA σ σ.curThread x .restart)).1 ∧ + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s3 a σ3 m ∧ + Sched.BodyKeepA (stUpdA σ σ.curThread x .restart) σ3 := by + have e0 : (0#u64 : U64).val = 0 := rfl + obtain ⟨x, s3, σ3, hk, hset, hD, hP, J3, K3⟩ := JStage3A.frameClearZA (ar := ar) (nA := nA) J RS hnvm cte hab fp rt va sz dev + true hcap + (D := fun s3 => vspace.decode_frame st ar s a fl4_abi.label.ARMPageUnmap length (cslotAddrA cte) + (.Frame fp sz rt dev true true 0#u64 va) call buf ex = ok (core.result.Result.Ok (), s3, a)) + (Pf := fun x σ3 => (([] : List Fl4SpecA64.Data), σ3) ∈ (Fl4SpecA64.performExtVSpace (Z := Unit) + (.pageUnmap .stage2 (.ext (.stage2FrameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev + (some (BitVec.ofNat 16 (0#u64 : U64).val, va.bv)))) cte) (stUpdA σ σ.curThread x .restart)).1) + (fun s1 s3 heq hsc => by + rw [page_unmapS20_eqA ar s a length (cslotAddrA cte) fp rt va sz dev call buf ex, heq, bind_ok, hsc, bind_ok]) + (fun x σ3 c' hk hceq hg hσ3 => by + have hu0 : ((), stUpdA σ σ.curThread x .restart) ∈ (Fl4SpecA64.extUnmapPage (Z := Unit) .stage2 + (frameSizeOfA sz) (BitVec.ofNat 16 (0#u64 : U64).val) va.bv (specPtr fp) (stUpdA σ σ.curThread x .restart)).1 := by + rw [e0]; exact extUnmapPage_zeroA _ _ _ _ + exact perfS2Unmap0_memA (R := vmRightsOfA rt) (dev := dev) (cte := cte) hu0 hσ3) + fin + exact ⟨x, s3, σ3, hk, hset, hD, hP, J3, K3⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3ArmPtMapA.lean b/proofs/Fl4ProofsA64/P3/Stage3ArmPtMapA.lean new file mode 100644 index 000000000..f22b5ace2 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ArmPtMapA.lean @@ -0,0 +1,609 @@ +/- +P3 (AArch64, merge line, D8 step 5 (ii)): **closing the Stage-1 `ARMPageTableMap` arm** (`pt_map_refinesGA`'s branch: a +plain page-table cap, a mapped Stage-1 root in the extra slot). FL4 restarts the thread, rewrites the invoked cap as +mapped and writes one table descriptor (`P3/Stage3PtMapShapeA`). The relation's Stage-1 tables grow by the new table +(`insert ptr Z.S1`, its depth `n`); the record's tables / depths are those of the final-state J7 joined with them +(`P3/Stage3VsTyPostA`). Closed by `of_capVsFrameGA` with the descriptor word as the only VSpace write. + +* `JointObjectA.shrinkS1A`, `TabOffA.regrowA`, `SchedPageA.backPtMapA`, `SchedPageA.growPtMapA`: the ghost-growth + transfers. +* `UnmPtSpecA.ofOneTableA`: J9 across a cap rewrite to a mapped cap and one word of one table. +* **`JStage3A.ptMapS1A`**: arm hypotheses are `pt_map_refinesGA`'s decode-site facts (`UnmPtA`, `ptr ∉ S2PgE`, + `ptr ∉ Y`, the message and extra cap), FL4's decode succeeds, and `fin`: `SysL`, `BXI` at some `Ac'`, `UserOK` and + J7 at the final state. + +Note (`UnmPtA.nt`): a table unmapped by the `ARMPageTableUnmap` arms stays in `Z.S1`, so mapping it again is not +covered here. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3BodyKeepA +import Fl4ProofsA64.P3.Stage3PtMapShapeA +import Fl4ProofsA64.P3.Stage3ArmAssignA +import Fl4ProofsA64.P3.Stage3ArmFrMapA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- A new Stage-1 table that is a heap `other 12` object adds no joint object. -/ +theorem JointObjectA.shrinkS1A {Z : XSetsA} {H : Heap} {ptr p : U64} {Lv' : U64 → Nat} {bytes : Nat} + (hH : H ptr = some (.other 12)) (h : JointObjectA { Z with S1 := insert ptr Z.S1, Lv := Lv' } H p bytes) : + JointObjectA Z H p bytes := by + rcases h with ⟨hb, h⟩ | h | h | h + · rcases h with h | h | h | h | h + · exact Or.inl ⟨hb, Or.inl h⟩ + · rcases Set.mem_insert_iff.1 h with rfl | h + · exact Or.inr (Or.inr (Or.inr ⟨_, hH, by rw [hb]; rfl⟩)) + · exact Or.inl ⟨hb, Or.inr (Or.inl h)⟩ + · exact Or.inl ⟨hb, Or.inr (Or.inr (Or.inl h))⟩ + · exact Or.inl ⟨hb, Or.inr (Or.inr (Or.inr (Or.inl h)))⟩ + · exact Or.inl ⟨hb, Or.inr (Or.inr (Or.inr (Or.inr h)))⟩ + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr (Or.inl h)) + · exact Or.inr (Or.inr (Or.inr h)) + +/-- A table off the non-Stage-1 footprint stays off it when only the Stage-1 ghosts change. -/ +theorem TabOffA.regrowA {Z : XSetsA} {m : Fl4SpecA64.McState} {s s' : S} {q : U64} {S1' : Set U64} {Lv' : U64 → Nat} + (hk : L.ksb s' = L.ksb s) (hc : L.cpu s' = L.cpu s) (h : TabOffA (L := L) Z m s q) : + TabOffA (L := L) { Z with S1 := S1', Lv := Lv' } m s' q := + have h' := h.of_ksA hk hc + ⟨fun i hi hf => h'.mem i hi + (FootA.monoA (Z := { { Z with S1 := S1', Lv := Lv' } with S1 := ∅ }) rfl rfl (fun _ h => h) (fun _ h => h) + (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) hf), h'.obj⟩ + +/-- **No scheduler page is added** by mapping a page-table cap and listing its table. -/ +theorem SchedPageA.backPtMapA {Z : XSetsA} {OS : Set U64} {s s' : S} {x ptr a0 v0 asid va q : U64} + {Lv' : U64 → Nat} (hx : x ∈ OS) (hold : capA st s x = .PageTable ptr false a0 v0) + (hnew : capA st s' x = .PageTable ptr true asid va) (hkeep : ∀ u ∈ OS, u ≠ x → capA st s' u = capA st s u) + (h : SchedPageA st { Z with S1 := insert ptr Z.S1, Lv := Lv' } OS s' q) : SchedPageA st Z OS s q := by + have own : SchedPageA st Z OS s ptr := by + unfold SchedPageA + exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ⟨x, hx, Or.inl ⟨_, _, _, hold⟩⟩)))))) + unfold SchedPageA at h + rcases h with h | h | h | h | h | h | h | ⟨u, hu, h⟩ + · exact Or.inl h + · rcases Set.mem_insert_iff.1 h with rfl | h + · exact own + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr (Or.inl h)) + · exact Or.inr (Or.inr (Or.inr (Or.inl h))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inl h)))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl h))))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl h)))))) + · by_cases e : u = x + · subst e + rw [hnew] at h + rcases h with ⟨m, a', v', h⟩ | ⟨m, a', h⟩ | ⟨a', v', h⟩ | ⟨q', a', v', h, -⟩ | ⟨y, a', h, -⟩ + · injection h with h1 + subst h1 + exact own + · cases h + · cases h + · cases h + · cases h + · rw [hkeep u hu e] at h + unfold SchedPageA + exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ⟨u, hu, h⟩)))))) + +/-- Listing the table of a page-table cap among the Stage-1 tables keeps J7. -/ +theorem J7A.growPtMapA {R : Set U64 → JSlotA S} {Z : XSetsA} {H : Heap} {OS : Set U64} {s : S} + {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} {x ptr asid va : U64} {Lv' : U64 → Nat} (hx : x ∈ OS) + (hcap : capA st s x = .PageTable ptr true asid va) + (hR : ∀ Pt, R Pt Z H OS s σ m → R Pt { Z with S1 := insert ptr Z.S1, Lv := Lv' } H OS s σ m) + (h : J7A st R Z H OS s σ m) : J7A st R { Z with S1 := insert ptr Z.S1, Lv := Lv' } H OS s σ m := by + obtain ⟨Pt, Ub, vr, pn, ac, cc, vc, df, hi, link, sa, fu, ual, vcu, r⟩ := h + have own : ptr ∈ Pt := link ptr (by + unfold SchedPageA + exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ⟨x, hx, Or.inl ⟨_, _, _, hcap⟩⟩))))))) + refine ⟨Pt, Ub, vr, pn, ac, cc, vc, df, hi, fun p hp => ?_, sa, fu, ual, vcu, hR Pt r⟩ + unfold SchedPageA at hp + rcases hp with h | h | h + · exact link p (by unfold SchedPageA; exact Or.inl h) + · rcases Set.mem_insert_iff.1 h with rfl | h + · exact own + · exact link p (by unfold SchedPageA; exact Or.inr (Or.inl h)) + · exact link p (by unfold SchedPageA; exact Or.inr (Or.inr h)) + +/-- **J9 across a cap rewrite and one word of one table** (module docstring). -/ +theorem UnmPtSpecA.ofOneTableA {Z Z' : XSetsA} {OS Pt : Set U64} {s s' : S} {σ σ' : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} {p i : U64} (hp : p.val % 4096 = 0) (hi : i.val < 512) + (hxal : ∀ r ∈ Z.X, 8192 ∣ r.val) (hyal : ∀ y ∈ Z.Y, 8192 ∣ y.val) + (hxup : ∀ r ∈ Z.X, wa r 4096#u64 ≠ p) (hyup : ∀ y ∈ Z.Y, wa y 4096#u64 ≠ p) + (hobj : ∀ u ∈ OS, CapObjOkA Z (capA st s u)) + (hptal : ∀ u ∈ OS, ∀ q a v, capA st s u = .PageTable q false a v → q.val % 4096 = 0 ∧ q ≠ p) + (hvsal : ∀ u ∈ OS, ∀ q a, capA st s u = .Pml4 q false a → q.val % 4096 = 0) + (hbk : ∀ u ∈ OS, ∀ c, capA st s' u = c → (∀ q a v, c = .PageTable q false a v ∨ c = .Pml4 q false a ∨ + c = .Ept q 0#u64 false a v ∨ c = .HighVSpace q false a → capA st s u = c)) + (hword : ∀ q ∈ Pt, ∀ j : U64, j.val < 512 → peAF q j ≠ peAF p i → L.mem s' (peAF q j) = L.mem s (peAF q j)) + (link : ∀ q, SchedPageA st Z OS s q → q ∈ Pt) + (hoff : ∀ q, TabOffA (L := L) Z m s q → TabOffA (L := L) Z' m s' q) + (hset : ∀ q ao, q ≠ specPtr p → σ.kheap q = some (.archObj ao) → σ'.kheap q = some (.archObj ao)) + {tp : Fl4SpecA64.PT} (hpk : σ.kheap (specPtr p) = some (.archObj (.pageTable tp))) + (hne : ∀ u ∈ OS, ∀ q a, capA st s u = .Pml4 q false a → q ≠ p) + (hs2ne : ∀ r ∈ Z.X, r ≠ p) + (h : UnmPtSpecA st Z m OS s σ) : UnmPtSpecA st Z' m OS s' σ' := by + have al8 : ∀ y : U64, 8192 ∣ y.val → y.val % 4096 = 0 ∧ (wa y 4096#u64).val % 4096 = 0 := fun y hy => by + have h1 := y.bv.isLt + have h2 : y.val = y.bv.toNat := rfl + have h3 : (4096#u64 : U64).val = 4096 := rfl + rw [wa_valm, h3] + simp only [Nat.reducePow] at h1 ⊢ + omega + have words : ∀ q ∈ Pt, q.val % 4096 = 0 → q ≠ p → ∀ j : U64, j.val < 512 → + L.mem s' (peAF q j) = L.mem s (peAF q j) := + fun q hq ha ne j hj => hword q hq j hj (peAF_ne_tabA hp ha hi hj ne) + have ne_sp : ∀ q, q ≠ p → specPtr q ≠ specPtr p := fun q hn e => hn (specPtr_inj e) + have tab : ∀ ty q, q ∈ Pt → q.val % 4096 = 0 → q ≠ p → UnmTabA st Z m s σ ty q → UnmTabA st Z' m s' σ' ty q := + fun ty q hq ha ne h => by + obtain ⟨t, k1, k2, k3⟩ := h.spec + exact ⟨fun j hj => by rw [words q hq ha ne j hj]; exact h.inv j hj, + ⟨t, hset _ _ (ne_sp q ne) k1, k2, k3⟩, hoff q h.off⟩ + refine ⟨fun u hu q asid va hc => ?_, fun u hu q asid hc => ?_, fun u hu q asid va hc => ?_, + fun u hu q asid hc => ?_⟩ + · have o := hbk u hu _ hc q asid va (Or.inl rfl) + obtain ⟨hq, ne⟩ := hptal u hu q asid va o + exact tab _ _ (link q (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inl ⟨_, _, _, o⟩⟩)))))))) hq ne (h.pt u hu q asid va o) + · have o := hbk u hu _ hc q asid 0#u64 (Or.inr (Or.inl rfl)) + exact tab _ _ (link q (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inl ⟨_, _, o⟩)⟩)))))))) (hvsal u hu q asid o) (hne u hu q asid o) (h.vs u hu q asid o) + · have o := hbk u hu _ hc q asid va (Or.inr (Or.inr (Or.inl rfl))) + have hok := hobj u hu + rw [o] at hok + have hqX := hok.2.1 q 0#u64 false asid va rfl + obtain ⟨a0, a1⟩ := al8 q (hxal q hqX) + have l0 : q ∈ Pt := link q (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inr (Or.inl ⟨_, _, o⟩))⟩))))))) + have l1 : wa q 4096#u64 ∈ Pt := link _ (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inr (Or.inr (Or.inl ⟨q, _, _, o, rfl⟩)))⟩))))))) + have h0 := h.s2 u hu q asid va o + obtain ⟨es, k1, k2⟩ := h0.spec + have n0 : q ≠ p := hs2ne q hqX + refine ⟨fun j hj => ?_, es, hset _ _ (ne_sp q n0) k1, k2⟩ + by_cases hlo : j.val < 512 + · rw [words q l0 a0 n0 j hlo]; exact h0.inv j hj + · obtain ⟨j', hj', ej⟩ := peAF_hiA q j (by omega) hj + have := h0.inv j hj + rw [ej] at this ⊢ + rw [words _ l1 a1 (hxup q hqX) j' hj']; exact this + · have o := hbk u hu _ hc q asid 0#u64 (Or.inr (Or.inr (Or.inr rfl))) + have hok := hobj u hu + rw [o] at hok + have hqY := hok.2.2.1 q false asid rfl + obtain ⟨-, a1⟩ := al8 q (hyal q hqY) + have l1 : wa q 4096#u64 ∈ Pt := link _ (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inr (Or.inr (Or.inr ⟨q, _, o, rfl⟩)))⟩))))))) + have h0 := h.hv u hu q asid o + obtain ⟨l, k1, k2⟩ := h0.spec + have n0 : specPtr q ≠ specPtr p := fun e => by + rw [e, hpk] at k1 + cases k1 + refine ⟨fun j hj => ?_, l, hset _ _ n0 k1, k2⟩ + rw [words _ l1 a1 (hyup q hqY) j hj]; exact h0.inv j hj + +end + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +set_option maxHeartbeats 8000000 in +/-- **`JStage3A` across the Stage-1 `ARMPageTableMap` arm** (module docstring). -/ +theorem JStage3A.ptMapS1A {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {ar : machine.Arch A} {nA : U64} {s s3 : S} {a a3 : A} + {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) (hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM) + (cte : Fl4SpecA64.CslotPtr) (hab : AbsSlotA Z.th cte) {ptr asid0 va0 : U64} + (hcap : capAtA (L.mem s) (cslotAddrA cte) = .PageTable ptr false asid0 va0) + (UP : UnmPtA Z.S1 Z.Y OS H.tcbs (L.mem s) σ ptr) (hS2 : ptr ∉ S2PgE Z.X Z.T2) (hY : ptr ∉ Z.Y) + (slbl : Fl4SpecA64.Data) (hl : Fl4SpecA64.invocationType slbl = .archInvocationLabel .armPageTableMap) + (length buf : U64) (args : List U64) (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) + (extra : Aeneas.Std.Array obj.Slot 3#usize) + (x0 : U64) (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) + (exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) (hex : ExtraRelA Z.th (L.mem s) x0 (exs.map Prod.fst)) + (hS1 : ∀ p l mp' asid va, capAtA (L.mem s) x0 ≠ .Ept p l mp' asid va) + (hHi : ∀ p mp' asid, capAtA (L.mem s) x0 ≠ .HighVSpace p mp' asid) + (hdec : vspace.decode_page_table st ar s a fl4_abi.label.ARMPageTableMap length (cslotAddrA cte) + (.PageTable ptr false asid0 va0) buf extra = ok (core.result.Result.Ok (), s3, a3)) + (fin : ∀ (x : Fl4SpecA64.Tcb) (σ3 : Fl4SpecA64.State Unit) (ivp : Fl4SpecA64.PageTableInvocation), + σ.kheap σ.curThread = some (.tcb x) → + (Fl4SpecA64.decodePageTableInvocation (Z := Unit) slbl (args.map UScalar.bv) cte + (.pageTableCap (specPtr ptr) .normalPtT (mapDataA false asid0 va0)) exs).run σ = + ({(.ok (.invokePageTable ivp), σ)}, False) → + ((), σ3) ∈ (Fl4SpecA64.performPageTableInvocation (Z := Unit) ivp (stUpdA σ σ.curThread x .restart)).1 → + ∃ Ac' : Set U64, SysL st H Z.Cs OS ia ie s3 ∧ SymL st H Z.Cs s3 ∧ + BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac' s3 ∧ + Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s3 ∧ + J7A st (J7R2A st ar nA) Z H OS s3 σ3 m) : + ∃ (x : Fl4SpecA64.Tcb) (ivp : Fl4SpecA64.PageTableInvocation) (σ3 : Fl4SpecA64.State Unit) + (Ac' Tb' : Set U64) (Lv' : U64 → Nat) (nL : Nat), + σ.kheap σ.curThread = some (.tcb x) ∧ + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + (Fl4SpecA64.decodePageTableInvocation (Z := Unit) slbl (args.map UScalar.bv) cte + (.pageTableCap (specPtr ptr) .normalPtT (mapDataA false asid0 va0)) exs).run σ = + ({(.ok (.invokePageTable ivp), σ)}, False) ∧ + ((), σ3) ∈ (Fl4SpecA64.performPageTableInvocation (Z := Unit) ivp (stUpdA σ σ.curThread x .restart)).1 ∧ + JStage3A st AR { Z with S1 := insert ptr Z.S1, Lv := Function.update Z.Lv ptr nL } H OS U Ac' Pp Tb' Lv' ia ie N E + Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s3 a3 σ3 m ∧ + Sched.BodyKeepA (stUpdA σ σ.curThread x .restart) σ3 := by + have E0 := J.toJEntA + have hx : cslotAddrA cte ∈ OS := RS.sub _ (hab.isSlot J.j.1.place) + have hO : OSOK H.tcbs H.omega H.frame OS := J.sys.ent.os + have hSh := pt_map_shapeA (L := L) (extra := extra) ar E0 RS cte hab hx hcap UP slbl hl length buf args hlen hargs + x0 hx0 exs hex hS1 hHi + rw [hdec, WP.spec_ok] at hSh + obtain ⟨ha, iv, hD, x', s2, σ2, asid1, va, p, i, v, nL, σp, σ', wv, hk', hset', hsc, hσ2, HSs, hEnc, hca, hks2, hcpu2, + hp, hpn, hi, hA, hn0, hn3, hs3, hreach, ⟨t', hσp⟩, ⟨t0, hk2p⟩, hms, hunq, hv0, hperf, VR, hMJ, hv3, hdTab, hPtr, hwlt⟩ := hSh s3 a3 rfl + subst ha hs3 + obtain ⟨x, hk, heq, hset, J1, -, hOS1, -⟩ := J.of_restartCurA RS.pl.choose_spec.choose_spec.choose_spec.choose_spec + RS.sym RS.run hnvm RS.cur RS.vs RS.vc + obtain rfl : x' = x := by + rw [hk] at hk' + injection hk' with h + injection h with h + exact h.symm + have hks1 : L.ksb (L.wr s (stA (L.curW s)) sched.TS_RESTART) = L.ksb s := L.ksb_wr _ _ _ + have hcpu1 : L.cpu (L.wr s (stA (L.curW s)) sched.TS_RESTART) = L.cpu s := L.cpu_wr _ _ _ + have sA : Aligned (stA (L.curW s)) := by + rw [stA_eq]; exact field_aligned (E0.sq.tcbs _ E0.sq.curT) (by simp) (by simp) + generalize hs1 : L.wr s (stA (L.curW s)) sched.TS_RESTART = s1 at heq J1 hOS1 hks1 hcpu1 hsc + have c0 : capA st s (cslotAddrA cte) = .PageTable ptr false asid0 va0 := hcap + have c1 : capA st s1 (cslotAddrA cte) = .PageTable ptr false asid0 va0 := (hOS1.capA hx).trans c0 + have E1 := J1.toJEntA + have R0 := J.j.1.vs + have al_x : Aligned (cslotAddrA cte) := hO.al0 hx + have al_x8 : Aligned (wa (cslotAddrA cte) 8#u64) := hO.al hx (k := 8#u64) (by simp) (by simp) + have hpa : p.val % 4096 = 0 := R0.al_mod hp + have alw : Aligned (peAF p i) := peAF_alignedA hpa hi + have hks3 : L.ksb (L.wr s2 (peAF p i) v) = L.ksb s := (L.ksb_wr _ _ _).trans hks2 + have hcpu3 : L.cpu (L.wr s2 (peAF p i) v) = L.cpu s := (L.cpu_wr _ _ _).trans hcpu2 + have wS := writesIn_setCapA al_x al_x8 hsc + have wP : WritesIn st s2 (L.wr s2 (peAF p i) v) (fun b => b = peAF p i) := writesIn_wr alw + -- J7 at entry: the new table is an `other 12` heap object, the written one is reachable + obtain ⟨PtJ, UbJ, vrJ, -, -, -, -, -, -, linkJ, -, -, -, -, R7⟩ := J.j7 + obtain ⟨PpJ, TbJ, HrJ, TeJ, KtJ, LvJ, ReJ, bitsJ, R2J, -, -⟩ := R7 + have hH12 : H ptr = some (.other 12) := by + have hunm : UnmCap (capA st s (cslotAddrA cte)) ptr 4096 := Or.inl ⟨rfl, asid0, va0, c0⟩ + obtain ⟨z, hz, hh⟩ := R2J.core.lookup.vh.1 _ hx ptr 4096 hunm + have hz12 : z = 12 := Nat.pow_right_injective (le_refl 2) (hz.symm.trans (by norm_num)) + rw [hz12] at hh + exact hh + -- the final-state facts and the record's new typing + obtain ⟨Ac', hsl, hsy, hbx, hus, hj7⟩ := fin x' σ' iv hk hD hperf + have vo : VrOK H.omega H.frame H.scs H.scBits OS Z.V ∅ (L.ksb s) (utVsPtA st Pp Tb Lv s) := J.vsOk + have hAsK : ∀ b, AsW (L.ksb s) b → L.mem (L.wr s2 (peAF p i) v) b = L.mem s b := by + intro b hb + have hb1 : Aligned b := vo.al b (Or.inr hb) + have hns : ¬ SlotWordA (cslotAddrA cte) b := fun h => vo.os b (Or.inr hb) (slotWord_inOS hO hx h) + have hvsR := RS.vs b (Or.inr hb) + rw [mem_wr_ne' alw hb1 (fun e => hA (by rw [e, hks2]; exact hb)), wS.mem_eq hb1 hns, ← hs1, + mem_wr_ne' sA hb1 (Ne.symm hvsR.2)] + have htbl : ∀ o : U64, 772352 ≤ o.val → o.val < 773376 → o.val % 8 = 0 → + L.mem (L.wr s2 (peAF p i) v) (wa (L.ksb (L.wr s2 (peAF p i) v)) o) ≠ 0#u64 → + L.mem (L.wr s2 (peAF p i) v) (wa (L.ksb (L.wr s2 (peAF p i) v)) o) ∈ Pp := by + intro o h1 h2 h8 hne + have e : L.mem (L.wr s2 (peAF p i) v) (wa (L.ksb (L.wr s2 (peAF p i) v)) o) = L.mem s (wa (L.ksb s) o) := by + rw [hks3]; exact hAsK _ ⟨o, h1, h2, h8, rfl⟩ + rw [e] at hne ⊢ + exact (J.sys.ent.vs.tbl o h1 h2 h8 hne).1 + obtain ⟨T, LvR, hty, hvok⟩ := vsPost_of_j7A (S1 := insert ptr Z.S1) hj7 htbl + have hbv : ∀ b, BufW Bf b → ¬ VsR (L.ksb (L.wr s2 (peAF p i) v)) + (utVsPtA st Pp (insert ptr Z.S1 ∪ T) LvR (L.wr s2 (peAF p i) v)) b := by + have x : L.mem (L.wr s2 (peAF p i) v) (peAF p i) = v := L.mem_wr_same alw + refine bufVs_leafA (x := peAF p i) (r := ptr) J.bufOff J.bufVs J.sys.ent.vs hty hks3 hAsK + (fun b hb ne => ?_) ?_ (fun _ h => ?_) (fun h => ?_) (fun _ => ?_) (fun j hj => ?_) + · have hb1 : Aligned b := vo.al b hb + have hns : ¬ SlotWordA (cslotAddrA cte) b := fun h => vo.os b hb (slotWord_inOS hO hx h) + have hvsR := RS.vs b hb + rw [mem_wr_ne' alw hb1 (Ne.symm ne), wS.mem_eq hb1 hns, ← hs1, mem_wr_ne' sA hb1 (Ne.symm hvsR.2)] + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨_, hx, Or.inl ⟨_, _, _, c0⟩⟩)))))) + · rw [x, hv3] at h + exact absurd h (by decide) + · rw [x, hv3] at h + exact absurd h (by decide) + · rw [x, ← hdTab] + unfold vspace.ADDR_MASK dTabA dAddrA layout.PPTR_BASE + rfl + · rw [hPtr j hj] + exact UP.inv j hj + -- the wrapper's side facts + have hN := bxi_nodes E1.sys.ent.bxi + have hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s1 y = .Null := J1.j5.2.2 + have hTr : TreeA st OS Z.G.chains Z.G.par s1 := + J1.j.1.cdt.tree.grow J1.j5.2.1 (fun x hx hn => J1.j5.2.2 x hx hn) + have hTr0 : TreeA st OS Z.G.chains Z.G.par s := + J.j.1.cdt.tree.grow J.j5.2.1 (fun x hx hn => J.j5.2.2 x hx hn) + have hnul0 : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null := J.j5.2.2 + have hok : CapObjOkA Z (.PageTable ptr true asid1 va) := + ⟨fun _ h => (by cases h), fun _ _ _ _ _ h => (by cases h), fun _ _ _ h => (by cases h), + fun _ _ _ h => (by cases h), trivial⟩ + have hM : MapAlikeA (.PageTable ptr true asid1 va) (capA st s1 (cslotAddrA cte)) := by rw [c1]; exact .pt .. + have hirq : CapOkA (stUpdA σ σ.curThread x' .restart).interruptIrqNode cte + (.archObjectCap (.pageTableCap (specPtr ptr) .normalPtT (some (asidOfA asid1, wv)))) := by + refine ⟨fun irq hlt e => ?_, fun ⟨_, _, _, _, e⟩ => by cases e⟩ + have obj : ∃ o ac, (stUpdA σ σ.curThread x' .restart).kheap cte.1 = some o ∧ + Fl4SpecA64.capOf o cte.2 = some ac ∧ CapRelA (capAtA (L.mem s1) (cslotAddrA cte)) ac := by + obtain ⟨o, idx⟩ := cte + obtain ⟨p0, e1, hc | ⟨ht, i0, hi0, e2⟩⟩ := hab + · simp only at e1 hc; subst e1 + obtain ⟨cs, hk0, -, hr⟩ := J1.j.1.st.cnode p0 _ hc + obtain ⟨ac, hac, hrel⟩ := hr idx rfl + exact ⟨_, ac, hk0, hac, hrel⟩ + · simp only at e1 e2; subst e1 e2 + obtain ⟨y, hk0, hr⟩ := J1.j.1.st.tcb p0 ht + obtain ⟨ac, hac, hrel⟩ := hr i0 hi0 + refine ⟨_, ac, hk0, hac, ?_⟩ + unfold cslotAddrA; rw [ofBl_tcbIdx i0 (by unfold tcbSlotsA at hi0; omega)]; exact hrel + obtain ⟨o, ac, hko, hco, hrel⟩ := obj + have c1' : capAtA (L.mem s1) (cslotAddrA cte) = .PageTable ptr false asid0 va0 := c1 + rw [e] at hko + rw [c1'] at hrel + rcases J1.sinv.irqN.slot irq hlt o _ ac hko hco with rfl | ⟨_, _, _, rfl⟩ + · cases hrel.2 + · cases hrel.2 + have hsci : NoIdleCapV m n (.archObjectCap (.pageTableCap (specPtr ptr) .normalPtT + (some (asidOfA asid1, wv)))) := + ⟨fun _ _ e => (by cases e), fun _ e => (by cases e), + fun _ _ _ e => by cases e; exact fun h => absurd hwlt (not_lt.2 h.1)⟩ + have main := JStage3A.of_capVsFrameGA (J := J1) + (hS := fun q0 hq => by + refine ⟨Set.mem_insert_of_mem _ hq.1, ?_⟩ + by_cases e : q0 = ptr + · subst e; rw [Function.update_self]; omega + · rw [Function.update_of_ne e]; exact hq.2) + (hT2 := fun _ h => h) + (hpt := fun q0 _ _ h => by + injection h with h1 + subst h1 + exact Or.inl ⟨Set.mem_insert _ _, by rw [Function.update_self]; omega⟩) (S1' := insert ptr Z.S1) (T2' := Z.T2) + (Zl' := Function.update Z.Lv ptr nL) (Sh2' := Z.Sh2) (Tb' := insert ptr Z.S1 ∪ T) (Lv' := LvR) + (Q := fun s' a' σ'' hw => s' = L.wr s2 (peAF p i) v ∧ a' = a3 ∧ σ'' = σ' ∧ hw = Z.hw) + RS.sub (footNOff_ksA rfl rfl (footN_offA E1)) (footNAl_ksA rfl rfl (FootNA.aligned E1.j.1 hN E1.scAl E1.ap.vcAl)) + cte hab hEnc hca hTr hnul (fun h => by cases h) (by rw [c1]; exact fun h => by cases h) hM hok hirq hsci + (fun s2' σ2' hs2 hσ2' HSs' wc jx hcx => by + have es : s2' = s2 := okinj (hs2.symm.trans hsc) + subst es + have eσ : σ2' = σ2 := setCap_detA hσ2' hσ2 + subst eσ + obtain ⟨J3, -, -, -, -⟩ := hMJ hTr0 hnul0 J.tabS1 ⟨hS2, hY⟩ + obtain ⟨Pt, Ub, vr, -, -, -, -, -, -, link, -, -⟩ := J1.j7 + obtain ⟨Qb, Rb, q⟩ := J1.sys.ent.bxi.1 + have vP : VsR (L.ksb s1) Pt (peAF p i) := vsW_pageA link (Or.inr (Or.inl hp)) hi + have offT : ∀ t ∈ Z.T, (peAF p i).val < t.val ∨ t.val + 4096 ≤ (peAF p i).val := fun t ht => by + have hth : t ∈ H.tcbs := by rw [J1.tcbs]; exact ht + exact (q.omega _ (vr.om _ vP)).2.1 t hth + have osk3 : OSKeep st OS s2' (L.wr s2' (peAF p i) v) := + osKeep_ofA hO fun w' hw' hin => wP.mem_eq hw' (fun e => vr.os w' (e ▸ vP) hin) + obtain ⟨hcx', hco⟩ := capA_setCapA hO hx hEnc hs2 + have hnew : capA st (L.wr s2' (peAF p i) v) (cslotAddrA cte) = .PageTable ptr true asid1 va := by + rw [osk3.capA hx, hcx'] + have hkeep : ∀ u ∈ OS, u ≠ cslotAddrA cte → capA st (L.wr s2' (peAF p i) v) u = capA st s1 u := fun u hu e => by + rw [osk3.capA hu, hco u hu e] + have hMs2 : ∀ u ∈ OS, MapAlikeA (capA st s2' u) (capA st s1 u) := fun u hu => by + by_cases e : u = cslotAddrA cte + · subst e; rw [hcx']; exact hM + · rw [hco u hu e]; exact .refl _ + have sbS : ∀ t ∈ Z.T, sbWordA t ∈ OS := fun t ht => + RS.sub _ ⟨t, Or.inr ⟨(J1.j.1.thT t).2 ht, 2, by simp [tcbSlotsA], (bufSlot_eqA t).symm⟩⟩ + have wA : WritesIn st s1 s2' (fun b => Aligned b ∧ W7.CapW H.tcbs {b | InOS OS b} ∅ b) := writesIn_alAG wc + have hB : ∀ b, (Aligned b ∧ W7.CapW H.tcbs {b | InOS OS b} ∅ b) → W7.CapW H.tcbs H.omega H.frame b := + fun b hb => os_capw hO hb.1 hb.2 + obtain ⟨-, -, -, -, -, fld, -, -⟩ := W7.blk_of_capw (BXI.blk J1.sys.ent.bxi) wA hB + have ib : ∀ t ∈ Z.T, L.mem s2' (ibWordA t) = L.mem s1 (ibWordA t) := fun t ht => + fld t (by rw [J1.tcbs]; exact ht) 3512#u64 (by simp) (by simp) (by simp) + have hk2p' : σ2'.kheap (specPtr p) = some (.archObj (.pageTable t0)) := hk2p + have HS2 : VsSpecA σ2' σ' := VsSpecA.of_ptStoreA hk2p' (hσp ▸ hunq) + have tw : ∀ t ∈ Z.T, ∀ K : U64, K.val + 8 ≤ 4096 → K.val % 8 = 0 → + L.mem (L.wr s2' (peAF p i) v) (wa t K) = L.mem s2' (wa t K) := by + intro t ht K hK' h8 + have hta := J1.sr.core.tcbs t ht + have hvv : (wa t K).val = t.val + K.val := field_val hta (K := K) (by omega) + refine wP.mem_eq (field_aligned hta (by omega) h8) (fun e => ?_) + have := offT t ht + rw [← e] at this + omega + have hib : ∀ t ∈ Z.T, IbSame (L := L) s2' (L.wr s2' (peAF p i) v) t := fun t ht => by + refine ⟨tw t ht _ (by simp) (by simp), ?_, ?_⟩ + · rw [sbWordA_eq]; exact tw t ht _ (by simp) (by simp) + · rw [sbWordA8_eq]; exact tw t ht _ (by simp) (by simp) + have hj7' := hj7 + obtain ⟨Pt', Ub', vr', -, ac', -⟩ := hj7' + have hBw : ∀ b, (SlotWordA (cslotAddrA cte) b ∨ b = peAF p i) → + L.Free s1 H.omega Z.Cs b ∨ W7.TLow H.tcbs b := fun b hb => by + rcases hb with hsl' | rfl + · have hal : Aligned b := by rcases hsl' with rfl | rfl; exact al_x; exact al_x8 + exact (os_capw hO hal (Or.inr ⟨slotWord_inOS hO hx hsl', Set.notMem_empty _⟩)).elim Or.inr + fun h' => Or.inl (Or.inl h'.1) + · exact Or.inl (Or.inl (vr.om _ vP)) + have hsp : ∀ q', SchedPageA st { Z with S1 := insert ptr Z.S1, Lv := Function.update Z.Lv ptr nL } OS + (L.wr s2' (peAF p i) v) q' → SchedPageA st Z OS s1 q' := + fun q' h => SchedPageA.backPtMapA hx c1 hnew hkeep h + have hks31 : L.ksb (L.wr s2' (peAF p i) v) = L.ksb s1 := hks3.trans hks1.symm + have hcpu31 : L.cpu (L.wr s2' (peAF p i) v) = L.cpu s1 := hcpu3.trans hcpu1.symm + have G := JStage3A.keepGhostA (hw := Z.hw) (s' := L.wr s2' (peAF p i) v) J1 hks31 hcpu31 + have hptr : ptr ∈ Pt := link ptr (by + unfold SchedPageA + exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ⟨_, hx, Or.inl ⟨_, _, _, c1⟩⟩))))))) + have hptrV : ∀ j : U64, j.val < 512 → VsR (L.ksb s1) Pt (peAF ptr j) := fun j hj => + Or.inl ⟨ptr, hptr, core.num.U64.wrapping_mul j 8#u64, by rw [wm8_valA hj]; omega, + by rw [wm8_valA hj]; omega, rfl⟩ + have hvsF : ∀ b, VsFootA Z.Pp (insert ptr Z.S1) Z.Y (L.ksb s1) b → + VsFootA Z.Pp Z.S1 Z.Y (L.ksb s1) b ∨ ∃ j : U64, j.val < 512 ∧ b = peAF ptr j := by + intro b hb + cases hb with + | asid j hj => exact Or.inl (.asid j hj) + | pool q0 j hq hj => exact Or.inl (.pool q0 j hq hj) + | pt q0 j hq hj => + rcases Set.mem_insert_iff.1 hq with rfl | hq + · exact Or.inr ⟨j, hj, rfl⟩ + · exact Or.inl (.pt q0 j hq hj) + | high q0 j hq hj => exact Or.inl (.high q0 j hq hj) + have h12 : ∃ q0 z, H q0 = some (.other z) ∧ q0.val ≤ ptr.val ∧ ptr.val + 4096 ≤ q0.val + 2 ^ z := + ⟨ptr, 12, hH12, le_refl _, by norm_num⟩ + have hunm9 : UnmPtSpecA st ({ Z with S1 := insert ptr Z.S1, Lv := Function.update Z.Lv ptr nL } : XSetsA) + m OS (L.wr s2' (peAF p i) v) σ' := by + have hv' : L.mem (L.wr s2' (peAF p i) v) (peAF p i) = v := L.mem_wr_same alw + have hz : ∀ u ∈ OS, ∀ q, ((∃ a w, capA st (L.wr s2' (peAF p i) v) u = .PageTable q false a w) ∨ + ∃ a, capA st (L.wr s2' (peAF p i) v) u = .Pml4 q false a) → q ≠ p := by + intro u hu q hc e + subst e + apply hv0 + obtain ⟨_, _, -, -, -, -, -, -, -, -, -, -, -, -, R7f⟩ := hj7 + obtain ⟨_, _, _, _, _, _, _, _, R2f, -, -⟩ := R7f + have ho : (core.num.U64.wrapping_mul i 8#u64).val < 4096 ∧ + (core.num.U64.wrapping_mul i 8#u64).val % 8 = 0 := by rw [wm8_valA hi]; omega + rw [← hv'] + rcases hc with ⟨a', w', hc⟩ | ⟨a', hc⟩ + · exact R2f.core.lookup.zr u hu q 4096 (Or.inl ⟨rfl, a', w', hc⟩) _ ho.1 ho.2 + · exact R2f.capRefs.nativeEmpty u hu q a' hc _ ho.1 ho.2 + have keepC : ∀ u ∈ OS, ∀ c, capA st (L.wr s2' (peAF p i) v) u = c → (∀ q a w, c = .PageTable q false a w ∨ + c = .Pml4 q false a ∨ c = .Ept q 0#u64 false a w ∨ c = .HighVSpace q false a → capA st s1 u = c) := by + intro u hu c hc q a' w hk0 + by_cases e : u = cslotAddrA cte + · subst e + rw [hnew] at hc + rw [← hc] at hk0 + rcases hk0 with h | h | h | h <;> cases h + · rw [← hkeep u hu e]; exact hc + obtain ⟨tp1, kp1, -⟩ := R0.pt p hp + have hpk1 : (stUpdA σ σ.curThread x' .restart).kheap (specPtr p) = some (.archObj (.pageTable tp1)) := + stUpd_keep hk kp1 (fun _ h => by cases h) + refine UnmPtSpecA.ofOneTableA (Z := Z) (Pt := Pt) (p := p) (i := i) hpa hi J.j.1.ext.xal R0.yal + (fun r hr e => (J.j.1.ext.pgd _ (Or.inr ⟨r, hr, Or.inr rfl⟩)).1 (Or.inr (Or.inl (e ▸ hp)))) + (fun y hy e => R0.ytb y hy (e ▸ hp)) (fun u hu => ?_) (fun u hu q a' w hc => ?_) (fun u hu q a' hc => ?_) + keepC (fun q hq j hj hne => ?_) link (fun q h => TabOffA.regrowA hks31 hcpu31 h) (fun q ao hne h => ?_) + hpk1 (fun u hu q a' hc => ?_) + (fun r hr e => (J.j.1.ext.pgd _ (Or.inr ⟨r, hr, Or.inl rfl⟩)).1 (Or.inr (Or.inl (e ▸ hp)))) J1.unmPt + · by_cases hs0 : IsSlotA Z.th u + · exact J1.j.capObj hs0 + · rw [hnul u hu hs0] + exact CapObjOkA.null Z + · by_cases e : u = cslotAddrA cte + · subst e + rw [c1] at hc + injection hc with h1 + subst h1 + exact ⟨Nat.mod_eq_zero_of_dvd UP.al, hpn⟩ + · have hc3 : capA st (L.wr s2' (peAF p i) v) u = .PageTable q false a' w := by rw [hkeep u hu e]; exact hc + refine ⟨?_, hz u hu q (Or.inl ⟨a', w, hc3⟩)⟩ + rw [hOS1.capA hu] at hc + have hunm : UnmCap (capA st s u) q 4096 := Or.inl ⟨rfl, a', w, hc⟩ + obtain ⟨z, hzz, hh⟩ := R2J.core.lookup.vh.1 u hu q 4096 hunm + have hz12 : z = 12 := Nat.pow_right_injective (le_refl 2) (hzz.symm.trans (by norm_num)) + have := J.sys.ent.heap.align q _ hh + rw [hz12] at this + exact this + · have hs0 : IsSlotA Z.th u := Classical.byContradiction fun h => by rw [hnul u hu h] at hc; cases hc + have hok0 : CapObjOkA Z (capA st s1 u) := J1.j.capObj hs0 + rw [hc] at hok0 + exact R0.al_mod (hok0.2.2.2.1 q false a' rfl).1 + · have hv'' : VsR (L.ksb s1) Pt (peAF q j) := Or.inl ⟨q, hq, core.num.U64.wrapping_mul j 8#u64, + by rw [wm8_valA hj]; omega, by rw [wm8_valA hj]; omega, rfl⟩ + rw [wP.mem_eq (vr.al _ hv'') hne] + exact (writesIn_setCapA al_x al_x8 hs2).mem_eq (vr.al _ hv'') + (fun h => vr.os _ hv'' (slotWord_inOS hO hx h)) + · obtain ⟨msr, e⟩ := hms + rw [e, hσp] + show (ptSetA σ2' (specPtr p) t').kheap q = _ + rw [ptSetA_kheap, if_neg hne] + exact HSs'.keep h (fun _ _ e => by cases e) (fun _ e => by cases e) + · by_cases e : u = cslotAddrA cte + · subst e; rw [c1] at hc; cases hc + · exact hz u hu q (Or.inr ⟨a', by rw [hkeep u hu e]; exact hc⟩) + refine ⟨_, a3, σ', Z.hw, fun b => b = peAF p i, Ac', + ⟨wP, fun Pt'' Ub'' vr'' link'' b e => by rw [e]; exact vsW_pageA link'' (Or.inr (Or.inl hp)) hi, + fun b e hbuf => J1.bufOff.page b hbuf p (Or.inr (Or.inl hp)) + (core.num.U64.wrapping_mul i 8#u64) (by rw [wm8_valA hi]; omega) (by rw [wm8_valA hi]; omega) + (by rw [e]; rfl), + HS2, ?_, ?_, J3, + ⟨⟨J1.sys.ent.heap, hbx, hO, hus, hty, by rw [hks3]; exact E0.sys.ent.ksw⟩, hsl⟩, hvok, hbv, + W7.inv_of_capw J1.sq (wS.trans wP) hBw, ac'.case.po, ?_, ?_, hunm9, + ⟨J1.bufOff.os, fun b hb hf => ?_, fun b hb q' hq' => J1.bufOff.page b hb q' (hsp q' hq'), J1.bufOff.vcw, + fun bp hbp => (J1.bufOff.pin bp hbp).keepF fun t ht hO _ _ r x1 x2 x3 x4 e => + ⟨ht, hO, r, x1, x2, x3, x4, by rw [hkeep _ hO (fun e1 => by rw [e1, c1] at e; cases e)]; exact e⟩⟩, + J7A.growPtMapA hx hnew (fun _ h => h) hj7, + fun h p0 bytes hp0 => h p0 bytes (JointObjectA.shrinkS1A hH12 hp0), fun h => ⟨h.vcpu, h.reply, h.asid, h.tvcpu, h.vcDeadRun, h.replyTcb⟩, + fun h => ⟨fun u hu q0 b0 fi hc p0 bytes hp0 => h.obj u hu q0 b0 fi hc p0 bytes (JointObjectA.shrinkS1A hH12 hp0), + h.ks, h.idle, h.sep⟩, + fun h => h, + ⟨G.1.sca, G.1.irqSt, G.1.vcAl, G.1.vc, fun b hb => ?_, G.1.ext⟩, Set.subset_union_left, + fun p0 hp0 => ?_, ⟨fun p0 hp0 => ?_, G.2.2.1.roots⟩, fun b hb => ?_, fun q0 hq0 => ?_, + ⟨fun q0 hq0 j hj hf => G.2.2.2.2.2.mem q0 hq0 j hj (FootA.monoA + (Z := extZA { Z with hw := Z.hw, S1 := insert ptr Z.S1, Lv := Function.update Z.Lv ptr nL }) + rfl rfl (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) + (fun _ h => h) (fun _ h => h) hf), G.2.2.2.2.2.obj⟩, hsy⟩, + rfl, rfl, rfl, rfl⟩ + · obtain ⟨msr, e⟩ := hms + rw [e, hσp] + rfl + · intro q0 dv pg h + obtain ⟨msr, e⟩ := hms + have hne : q0 ≠ specPtr p := fun e' => by rw [e', hk2p'] at h; cases h + rw [e, hσp] + show (ptSetA σ2' (specPtr p) t').kheap q0 = _ + rw [ptSetA_kheap, if_neg hne] + exact h + · exact BufCover.vsA (BufCover.mapA J1.bufCover J1.bufNative J1.j.1.st jx.1.st + (fun t ht => (J1.j.1.thT t).2 ht) HSs' ib (fun t ht => hMs2 _ (sbS t ht))) HS2 hib + · exact BufNativeA.mapA J1.bufNative (fun t ht q' sz' rr dv' m' e' a'' v' h => by + by_cases e1 : sbWordA t = cslotAddrA cte + · rw [e1, hnew] at h; cases h + · rw [hkeep _ (sbS t ht) e1] at h + have hn := J1.bufNative t ht + rw [bufSlot_eqA, capAtA_eq (st := st), h] at hn + exact hn) + · rw [hks31] at hf + rcases hvsF b hf with hf | ⟨j, hj, e⟩ + · exact J1.bufOff.vs b hb hf + · exact J1.bufOff.page b hb ptr (by + unfold SchedPageA + exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ⟨_, hx, Or.inl ⟨_, _, _, c1⟩⟩))))))) + (core.num.U64.wrapping_mul j 8#u64) (by rw [wm8_valA hj]; omega) (by rw [wm8_valA hj]; omega) + (by rw [e]; rfl) + · rw [hks31] at hb + rcases hvsF b hb with hf | ⟨j, hj, e⟩ + · have := G.1.vs b (by rw [hks31]; exact hf) + exact this + · subst e + refine ⟨fun h => UP.os j hj h, fun t ht => UP.tcb j hj t ht⟩ + · rcases hp0 with hp0 | hp0 + · exact J1.ptH p0 (Or.inl hp0) + · rcases Set.mem_insert_iff.1 hp0 with rfl | hp0 + · exact h12 + · exact J1.ptH p0 (Or.inr hp0) + · rcases hp0 with ((hp0 | hp0) | hp0) | hp0 + · exact G.2.2.1.pages p0 (Or.inl (Or.inl (Or.inl hp0))) + · rcases Set.mem_insert_iff.1 hp0 with rfl | hp0 + · exact h12 + · exact G.2.2.1.pages p0 (Or.inl (Or.inl (Or.inr hp0))) + · exact G.2.2.1.pages p0 (Or.inl (Or.inr hp0)) + · exact G.2.2.1.pages p0 (Or.inr hp0) + · rw [hks31] at hb + rcases hvsF b hb with hf | ⟨j, hj, e⟩ + · exact J1.vsVc b hf + · rw [e]; exact vr.vc _ (hptrV j hj) + · rcases Set.mem_insert_iff.1 hq0 with e | hq0 + · rw [e]; exact TabOffA.regrowA hks3 hcpu3 ((J.unmPt.pt _ hx ptr asid0 va0 c0).off) + · exact TabOffA.regrowA hks31 hcpu31 (J1.tabS1 q0 hq0)) + obtain ⟨_, _, s3', a3', σ3', hw, P, Ac'', -, hσk, HSsk, HSvk, -, -, Jf, e1, e2, e3, e4⟩ := main + subst e1 e2 e3 e4 + exact ⟨x', iv, _, Ac'', insert ptr Z.S1 ∪ T, LvR, nL, hk, hset', hD, hperf, Jf, (Sched.BodyKeepA.ofSlot HSsk (setCap_keepsA hσk).2.2).trans (Sched.BodyKeepA.ofVs HSvk)⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3ArmPtUnmMA.lean b/proofs/Fl4ProofsA64/P3/Stage3ArmPtUnmMA.lean new file mode 100644 index 000000000..bffc91eb8 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ArmPtUnmMA.lean @@ -0,0 +1,579 @@ +/- +P3 (AArch64, merge line, D8 step 5 (ii)): **closing the `ARMPageTableUnmap` arm of a final page-table cap mapped on a +Stage-1 root or on none** (`pt_unmapS1_decJA`'s arm; ASID 0 included: `vspaceForAsid 0 = none`), for a table of the +relation's Stage-1 tables (`ptr ∈ Z.S1`, the first branch of `PtCapInA`). + +FL4 unmaps first (one zero write into a Stage-1 table, or nothing), zeroes the 512 words of the table and then rewrites +the slot; the spec runs l4v's `unmap_page_table`, the 512 `store_pte`s and the cache clean (each an `UnQ` step) and +the cap write. As for the mapped frame (`Stage3ArmFrUnmMA`) this is a zero step moved across the slot write +(`WritesIn.setCapCommA`, `VsSpecA.setCapCommA`), closed by `of_capVsFrameA`. + +* `unmapPageTable_unQ`, `OutQ.mapMx`: the spec side is `UnQ`. +* `zero_post2A`: `zero_postA` with the cleared table exported. +* `UnmPtSpecA.ofPtUnmMA`, `SchedPageA.backPtA`: J9 and the scheduler pages across the rewrite to an unmapped table cap + whose table was just cleared (`UnmTabA` of the new cap: the zeroed words, the spec table, `TabOffA` from `hTO`). +* **`JStage3A.ptUnmapS1A`**: the arm hypothesis `fin` is the final-state facts owed by T3 (`SysL`, `BXI` at some + `Ac'`, `UserOK`, J7); `vs`, `vsOk`, `sq`, `unmPt`, the IPC-buffer fields and `poolOff` are derived. + +Not covered: the `ptr ∈ Z.T2` branch of `PtCapInA` (J9's `UnmTabA.off` needs `TabOffA`, which a table of `Z.T2` +violates while `Z.T2` is kept; see the report). + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3BodyKeepA +import Fl4ProofsA64.P3.Stage3ArmFrUnmMA +import Fl4ProofsA64.P3.UnmapPtS1JA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## The spec side -/ + +section spec +open Fl4Spec + +theorem OutQ.mapMx {Q : Fl4SpecA64.State Unit → Fl4SpecA64.State Unit → Prop} {α β : Type} + (hr : ∀ σ, Q σ σ) (ht : ∀ a b c, Q a b → Q b c → Q a c) {f : α → NonDet (Fl4SpecA64.State Unit) β} + (hf : ∀ a, OutQ Q (f a)) : ∀ l : List α, OutQ Q (NonDet.mapMx f l) + | [] => OutQ.one hr fun _ => ⟨(), rfl⟩ + | x :: xs => OutQ.bind ht (hf x) fun _ => OutQ.mapMx hr ht hf xs + +theorem invTlb_unQ (asid : Fl4SpecA64.Asid) : OutQ UnQ (Fl4SpecA64.invalidateTlbByAsid (Z := Unit) asid) := by + unfold Fl4SpecA64.invalidateTlbByAsid Fl4SpecA64.loadVmid + refine OutQ.bind trQ (OutQ.bind trQ (OutQ.gets UnQ.refl _) fun _ => OutQ.pure UnQ.refl _) fun o => ?_ + cases o with + | none => exact OutQ.pure UnQ.refl _ + | some _ => exact doMachineOp_unQ _ + +theorem ptLookupN_unQ : ∀ (n : Nat) (level : Fl4SpecA64.VmLevel) (pt : Fl4SpecA64.ObjRef) + (v : Fl4SpecA64.VSpaceRef) (t : Fl4SpecA64.ObjRef), + OutQ UnQ (Fl4SpecA64.ptLookupFromLevelN (Z := Unit) n level pt v t).run + | 0, level, pt, v, t => by + rw [Fl4SpecA64.ptLookupFromLevelN] + exact OutQ.ebind trQ (OutQ.unlessE UnQ.refl (OutQ.throw UnQ.refl _)) fun _ => OutQ.throw UnQ.refl _ + | n + 1, level, pt, v, t => by + rw [Fl4SpecA64.ptLookupFromLevelN] + refine OutQ.ebind trQ (OutQ.unlessE UnQ.refl (OutQ.throw UnQ.refl _)) fun _ => ?_ + refine OutQ.ebind trQ (OutQ.retOk UnQ.refl _) fun _ => ?_ + refine OutQ.ebind trQ (OutQ.liftE (OutQ.getsThe UnQ.refl _)) fun _ => ?_ + refine OutQ.ebind trQ (OutQ.unlessE UnQ.refl (OutQ.throw UnQ.refl _)) fun _ => ?_ + refine OutQ.ebind trQ (OutQ.retOk UnQ.refl _) fun ptr => ?_ + by_cases hp : ptr = t + · rw [if_pos hp]; exact OutQ.retOk UnQ.refl _ + · rw [if_neg hp]; exact ptLookupN_unQ n _ _ _ _ + +/-- **l4v's `unmap_page_table` is a `UnQ` step.** -/ +theorem unmapPageTable_unQ (asid : Fl4SpecA64.Asid) (v : Fl4SpecA64.VSpaceRef) (pt : Fl4SpecA64.ObjRef) : + OutQ UnQ (Fl4SpecA64.unmapPageTable (Z := Unit) asid v pt) := by + unfold Fl4SpecA64.unmapPageTable Fl4SpecA64.ptLookupFromLevel + refine OutQ.catch trQ ?_ fun _ => OutQ.pure UnQ.refl _ + refine OutQ.ebind trQ (findVs_unQ _) fun _ => ?_ + refine OutQ.ebind trQ (ptLookupN_unQ _ _ _ _ _) fun x => ?_ + obtain ⟨slot, level⟩ := x + refine OutQ.ebind trQ (OutQ.liftE (storePte_unQ _ _)) fun _ => ?_ + refine OutQ.ebind trQ (OutQ.liftE (doMachineOp_unQ _)) fun _ => ?_ + exact OutQ.liftE (invTlb_unQ _) + +end spec + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} + +/-- **`zero_postA`, with the cleared table exported.** -/ +theorem zero_post2A {s2 : S} {σu : Fl4SpecA64.State Unit} (J2 : JAtZAX (L := L) AR Z s2 a σu m) + (hfa : ∀ w, FootA (L := L) Z s2 w → Aligned w) (hTO : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s2 q) + (hEO : ExtTabOffA (L := L) Z m s2) (hK : Aligned (L.ksb s2)) (hT : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s2)) + {T Ω Sg OS : Set U64} (hO : OSOK T Ω Sg OS) (hpOS : VsPgOffOSA Z OS) {ptr : U64} + (hp : (ptr ∈ Z.S1 ∧ 1 ≤ Z.Lv ptr) ∨ ptr ∈ Z.T2) : + ∃ t', Fl4SpecA64.ptType t' = .normalPtT ∧ + (∀ i : U64, i.val < 512 → Fl4SpecA64.ptApply t' i.bv = .invalidPTE) ∧ + Fl4Spec.NonDet.mapMx (fun sl => Fl4SpecA64.storePte (Z := Unit) .normalPtT sl .invalidPTE) + (Fl4SpecA64.uptoEnumStep (specPtr ptr) (specPtr ptr + ((1 : Fl4SpecA64.Word) <<< Fl4SpecA64.pteBits)) + (specPtr ptr + Fl4SpecA64.mask (Fl4SpecA64.ptBits .normalPtT))) σu = + ({((), ptSetA σu (specPtr ptr) t')}, False) ∧ + (∀ i : U64, i.val < 512 → L.mem (L.zr s2 ptr 4096#u64) (peAF ptr i) = 0#u64) ∧ + JAtZAX (L := L) AR Z (L.zr s2 ptr 4096#u64) a (ptSetA σu (specPtr ptr) t') m ∧ + OSKeep st OS s2 (L.zr s2 ptr 4096#u64) ∧ L.ksb (L.zr s2 ptr 4096#u64) = L.ksb s2 ∧ + L.cpu (L.zr s2 ptr 4096#u64) = L.cpu s2 := by + have hp4 : ptr.val % 4096 = 0 := by + rcases hp with ⟨h, -⟩ | h + · exact J2.1.vs.al_mod h + · exact Nat.mod_eq_zero_of_dvd (J2.1.ext.tal ptr h) + obtain ⟨t', hty, hinv, hrun0, hz, VR, ER⟩ := zero_relA J2.1.vs J2.1.ext hK hT hp + refine ⟨t', hty, hinv, hrun0, hz, + J2.of_zeroA hfa hTO hEO (by rcases hp with ⟨h, -⟩ | h; exact Or.inl h; exact Or.inr h) hp4 VR ER, + osKeep_ofA hO fun w hw hin => ?_, L.ksb_zr _ _ _, L.cpu_zr _ _ _⟩ + rw [zr_wrsA hp4 s2 hw, mem_wrsA hp4 hw _ (fun j h => List.mem_range.1 h), if_neg] + rintro ⟨j, hj, e⟩ + have hj' := List.mem_range.1 hj + exact hpOS ptr (ujA j) (by rw [ujA_val hj']; exact hj') + (by rcases hp with ⟨h, -⟩ | h; exact Or.inr (Or.inr h); exact Or.inl (Or.inl h)) (e ▸ hin) + +/-- An aligned word of a 4096-aligned page is one of its 512 entries. -/ +theorem peAF_ofRangeA {p b : U64} (hp : p.val % 4096 = 0) (hb : Aligned b) (h1 : p.val ≤ b.val) + (h2 : b.val < p.val + 4096) : ∃ i : U64, i.val < 512 ∧ b = peAF p i := by + have hpl : p.val < 2 ^ 64 := p.bv.isLt + have hfit : p.val + 4096 ≤ 2 ^ 64 := by simp only [Nat.reducePow] at hpl ⊢; omega + have hb8 : b.val % 8 = 0 := hb + refine ⟨ujA ((b.val - p.val) / 8), by rw [ujA_val (by omega)]; omega, UScalar.eq_of_val_eq ?_⟩ + rw [peAF_valA hfit (by rw [ujA_val (by omega)]; omega), ujA_val (by omega)] + omega + +/-- A word after `zero(p, 4096)`: `0` or kept. -/ +theorem zr_caseA {s : S} {p b : U64} (hp : p.val % 4096 = 0) (hb : Aligned b) : + L.mem (L.zr s p 4096#u64) b = 0#u64 ∨ L.mem (L.zr s p 4096#u64) b = L.mem s b := by + have hpl : p.val < 2 ^ 64 := p.bv.isLt + have h4 : (4096#u64 : U64).val = 4096 := rfl + have hfit : p.val + (4096#u64 : U64).val ≤ 2 ^ 64 := by rw [h4]; simp only [Nat.reducePow] at hpl ⊢; omega + rw [L.mem_zr s p 4096#u64 b (show p.val % 8 = 0 by omega) (by simp) hfit hb] + split_ifs + · exact Or.inl rfl + · exact Or.inr rfl + +/-- **J9 across the unmap of a mapped table**: the slot `x` rewritten to an unmapped table cap of `p` (whose table is +`UnmTabA` after), the other slots kept, page-table entries invalidated. -/ +theorem UnmPtSpecA.ofPtUnmMA {Z : XSetsA} {OS : Set U64} {s s' : S} {σ σu σ' : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} {Pt : Set U64} {x p : U64} + (hnew : capA st s' x = .PageTable p false 0#u64 0#u64) + (hkeep : ∀ u ∈ OS, u ≠ x → capA st s' u = capA st s u) + (hword : ∀ q ∈ Pt, ∀ i : U64, i.val < 512 → + L.mem s' (peAF q i) = L.mem s (peAF q i) ∨ L.mem s' (peAF q i) = 0#u64) + (link : ∀ q, SchedPageA st Z OS s q → q ∈ Pt) (hk : L.ksb s' = L.ksb s) (hcpu : L.cpu s' = L.cpu s) + (UI : InvXA σ σu) (hset : ∀ q ao, σu.kheap q = some (.archObj ao) → σ'.kheap q = some (.archObj ao)) + (htab : UnmTabA st Z m s' σ' .normalPtT p) + (h : UnmPtSpecA st Z m OS s σ) : UnmPtSpecA st Z m OS s' σ' := by + have hz0 : dBitA (0#u64) 0 = false := by unfold dBitA; rfl + have dz : ∀ (w w' : U64), dBitA w 0 = false → (w' = w ∨ w' = 0#u64) → dBitA w' 0 = false := fun w w' h0 e => by + rcases e with rfl | rfl + · exact h0 + · exact hz0 + have tab : ∀ ty q, q ∈ Pt → UnmTabA st Z m s σ ty q → UnmTabA st Z m s' σ' ty q := fun ty q hq h => by + obtain ⟨t, k1, k2, k3⟩ := h.spec + refine ⟨fun i hi => dz _ _ (h.inv i hi) (hword q hq i hi), ?_, h.off.of_ksA hk hcpu⟩ + rcases UI (specPtr q) with e | ⟨o, o', a0, b0, hn⟩ + · exact ⟨t, hset _ _ (e.trans k1), k2, k3⟩ + · rw [k1] at a0 + have h1 : o = .pageTable t := by + have := Option.some.inj a0 + injection this with h2 + exact h2.symm + subst h1 + obtain ⟨t0', e0, ty0, ap0⟩ := ArchInvA.pt hn + subst e0 + refine ⟨t0', hset _ _ b0, ty0.trans k2, fun i hi => ?_⟩ + rcases ap0 i.bv with h2 | h2 + · rw [h2]; exact k3 i hi + · exact h2 + have s2k : ∀ q es, σ.kheap (specPtr q) = some (.archObj (.ext (.stage2Root es))) → + (∀ i : Fin 1024, es i = .invalidPTE) → + ∃ es', σ'.kheap (specPtr q) = some (.archObj (.ext (.stage2Root es'))) ∧ ∀ i : Fin 1024, es' i = .invalidPTE := by + intro q es k1 k2 + rcases UI (specPtr q) with e | ⟨o, o', a0, b0, hn⟩ + · exact ⟨es, hset _ _ (e.trans k1), k2⟩ + · rw [k1] at a0 + have h1 : o = .ext (.stage2Root es) := by + have := Option.some.inj a0 + injection this with h2 + exact h2.symm + subst h1 + obtain ⟨e', e0, he⟩ := ArchInvA.s2 hn + subst e0 + exact ⟨e', hset _ _ b0, fun i => by rcases he i with h2 | h2; rw [h2]; exact k2 i; exact h2⟩ + have hik : ∀ q l, σ.kheap (specPtr q) = some (.archObj (.ext (.highVSpace l))) → + (∀ i : Fin 512, l i = .invalidPTE) → + ∃ l', σ'.kheap (specPtr q) = some (.archObj (.ext (.highVSpace l'))) ∧ ∀ i : Fin 512, l' i = .invalidPTE := by + intro q l k1 k2 + rcases UI (specPtr q) with e | ⟨o, o', a0, b0, hn⟩ + · exact ⟨l, hset _ _ (e.trans k1), k2⟩ + · rw [k1] at a0 + have h1 : o = .ext (.highVSpace l) := by + have := Option.some.inj a0 + injection this with h2 + exact h2.symm + subst h1 + obtain ⟨e', e0, he⟩ := ArchInvA.hi hn + subst e0 + exact ⟨e', hset _ _ b0, fun i => by rcases he i with h2 | h2; rw [h2]; exact k2 i; exact h2⟩ + have old : ∀ u ∈ OS, ∀ c, capA st s' u = c → (∀ q a v, c ≠ .PageTable q false a v) → capA st s u = c := + fun u hu c hc hn => by + by_cases e : u = x + · subst e; exact absurd (hc.symm.trans hnew) (hn _ _ _) + · rw [← hkeep u hu e]; exact hc + refine ⟨fun u hu q asid va hc => ?_, fun u hu q asid hc => ?_, fun u hu q asid va hc => ?_, + fun u hu q asid hc => ?_⟩ + · by_cases e : u = x + · subst e + rw [hnew] at hc + injection hc with h1 + subst h1 + exact htab + · rw [hkeep u hu e] at hc + exact tab _ _ (link q (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inl ⟨_, _, _, hc⟩⟩)))))))) (h.pt u hu q asid va hc) + · have o := old u hu _ hc (fun _ _ _ f => by cases f) + exact tab _ _ (link q (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inl ⟨_, _, o⟩)⟩)))))))) (h.vs u hu q asid o) + · have o := old u hu _ hc (fun _ _ _ f => by cases f) + have l0 : q ∈ Pt := link q (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inr (Or.inl ⟨_, _, o⟩))⟩))))))) + have l1 : wa q 4096#u64 ∈ Pt := link _ (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inr (Or.inr (Or.inl ⟨q, _, _, o, rfl⟩)))⟩))))))) + have h0 := h.s2 u hu q asid va o + obtain ⟨es, k1, k2⟩ := h0.spec + obtain ⟨es', k1', k2'⟩ := s2k q es k1 k2 + refine ⟨fun i hi => ?_, es', k1', k2'⟩ + by_cases hlo : i.val < 512 + · exact dz _ _ (h0.inv i hi) (hword q l0 i hlo) + · obtain ⟨j, hj, ej⟩ := peAF_hiA q i (by omega) hi + have := h0.inv i hi + rw [ej] at this ⊢ + exact dz _ _ this (hword _ l1 j hj) + · have o := old u hu _ hc (fun _ _ _ f => by cases f) + have l1 : wa q 4096#u64 ∈ Pt := link _ (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inr (Or.inr (Or.inr ⟨q, _, o, rfl⟩)))⟩))))))) + have h0 := h.hv u hu q asid o + obtain ⟨l, k1, k2⟩ := h0.spec + obtain ⟨l', k1', k2'⟩ := hik q l k1 k2 + exact ⟨fun i hi => dz _ _ (h0.inv i hi) (hword _ l1 i hi), l', k1', k2'⟩ + +/-- **The scheduler pages across the rewrite to an unmapped table cap of a Stage-1 table.** -/ +theorem SchedPageA.backPtA {Z : XSetsA} {OS : Set U64} {s s' : S} {q x p : U64} + (hnew : capA st s' x = .PageTable p false 0#u64 0#u64) + (hkeep : ∀ u ∈ OS, u ≠ x → capA st s' u = capA st s u) (hp : p ∈ Z.S1) + (h : SchedPageA st Z OS s' q) : SchedPageA st Z OS s q := by + unfold SchedPageA at h ⊢ + rcases h with h | h | h | h | h | h | h | ⟨u, hu, h⟩ + · exact Or.inl h + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr (Or.inl h)) + · exact Or.inr (Or.inr (Or.inr (Or.inl h))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inl h)))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl h))))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl h)))))) + · by_cases e : u = x + · subst e + rcases h with ⟨m, a', v', h⟩ | ⟨m, a', h⟩ | ⟨a', v', h⟩ | ⟨q', a', v', h, e'⟩ | ⟨y, a', h, e'⟩ <;> + rw [hnew] at h <;> cases h + exact Or.inr (Or.inl hp) + · rw [hkeep u hu e] at h + exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ⟨u, hu, h⟩)))))) + +set_option maxHeartbeats 8000000 in +/-- **`JStage3A` across the `ARMPageTableUnmap` arm of a final page-table cap of a Stage-1 table mapped on a Stage-1 +root or on none** (module docstring): FL4's decode succeeds, the spec performs l4v's `perform_pt_inv_unmap`, and the +record holds at the final state. The final-state facts owed at T3 (`SysL`, `BXI` at some `Ac'`, `UserOK`, J7) are the +arm hypothesis `fin`. -/ +theorem JStage3A.ptUnmapS1A {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {ar : machine.Arch A} {nA : U64} {s : S} {a : A} + {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (K : ArchKeepsA ar AR) + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) + (hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM) (hTO : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s q) + (hEO : ExtTabOffA (L := L) Z m s) + (hK : Aligned (L.ksb s)) (hT : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s)) + (length buf : U64) (ex : Aeneas.Std.Array obj.Slot 3#usize) + (cte : Fl4SpecA64.CslotPtr) (hab : AbsSlotA Z.th cte) (ptr asid va : U64) + (hpS : ptr ∈ Z.S1 ∧ 1 ≤ Z.Lv ptr) + (hcap : capA st s (cslotAddrA cte) = .PageTable ptr true asid va) + (hf : cdt.is_final_capability st s (cslotAddrA cte) = ok true) + (hS1 : Fl4SpecA64.asidRootKind σ (BitVec.ofNat 16 asid.val) = none ∨ + Fl4SpecA64.asidRootKind σ (BitVec.ofNat 16 asid.val) = some .stage1) + (fin : ∀ (x : Fl4SpecA64.Tcb) (s3 : S) (σ3 : Fl4SpecA64.State Unit) (a' : A), + σ.kheap σ.curThread = some (.tcb x) → + vspace.decode_page_table st ar s a fl4_abi.label.ARMPageTableUnmap length (cslotAddrA cte) + (.PageTable ptr true asid va) buf ex = ok (core.result.Result.Ok (), s3, a') → + ((), σ3) ∈ (Fl4SpecA64.performPageTableInvocation (Z := Unit) (.pageTableUnmap (.pageTableCap (specPtr ptr) + .normalPtT (some (BitVec.ofNat 16 asid.val, va.bv))) cte) (stUpdA σ σ.curThread x .restart)).1 → + ∃ Ac' : Set U64, SysL st H Z.Cs OS ia ie s3 ∧ SymL st H Z.Cs s3 ∧ + BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac' s3 ∧ + Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s3 ∧ + J7A st (J7R2A st ar nA) Z H OS s3 σ3 m) : + ∃ (x : Fl4SpecA64.Tcb) (s3 : S) (a' : A) (σ3 : Fl4SpecA64.State Unit) (Ac' : Set U64), + σ.kheap σ.curThread = some (.tcb x) ∧ + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + vspace.decode_page_table st ar s a fl4_abi.label.ARMPageTableUnmap length (cslotAddrA cte) + (.PageTable ptr true asid va) buf ex = ok (core.result.Result.Ok (), s3, a') ∧ + ((), σ3) ∈ (Fl4SpecA64.performPageTableInvocation (Z := Unit) (.pageTableUnmap (.pageTableCap (specPtr ptr) + .normalPtT (some (BitVec.ofNat 16 asid.val, va.bv))) cte) (stUpdA σ σ.curThread x .restart)).1 ∧ + JStage3A st AR Z H OS U Ac' Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s3 a' σ3 m ∧ + Sched.BodyKeepA (stUpdA σ σ.curThread x .restart) σ3 := by + have hx : cslotAddrA cte ∈ OS := RS.sub _ (hab.isSlot J.j.1.place) + have hO : OSOK H.tcbs H.omega H.frame OS := J.sys.ent.os + have E0 := J.toJEntA + have hp4 : ptr.val % 4096 = 0 := J.j.1.vs.al_mod hpS.1 + obtain ⟨x, hk, heq, hset, J1, -, hOS1, -⟩ := J.of_restartCurA RS.pl.choose_spec.choose_spec.choose_spec.choose_spec + RS.sym RS.run hnvm RS.cur RS.vs RS.vc + have hks : L.ksb (L.wr s (stA (L.curW s)) sched.TS_RESTART) = L.ksb s := L.ksb_wr _ _ _ + have hcpu : L.cpu (L.wr s (stA (L.curW s)) sched.TS_RESTART) = L.cpu s := L.cpu_wr _ _ _ + generalize L.wr s (stA (L.curW s)) sched.TS_RESTART = s1 at heq J1 hOS1 hks hcpu + have c1 : capA st s1 (cslotAddrA cte) = .PageTable ptr true asid va := (hOS1.capA hx).trans hcap + obtain ⟨c0, -, hrel0⟩ := StateRelA.getAbsA J1.j.1.st hab + have c1' : capAtA (L.mem s1) (cslotAddrA cte) = .PageTable ptr true asid va := c1 + rw [c1'] at hrel0 + obtain ⟨h16a, -⟩ := hrel0 + have h16 : asid.val < 65536 := lt_of_lt_of_eq h16a (by norm_num) + have hEnc0 : CapRt.EncWf (capAtA (L.mem s1) (cslotAddrA cte)) := CapRt.decodeF_encWf _ _ + rw [c1'] at hEnc0 + simp only [CapRt.EncWf] at hEnc0 + have hEnc : CapRt.EncWf (.PageTable ptr false 0#u64 0#u64) := by + simp only [CapRt.EncWf]; exact ⟨hEnc0.1, by simp, Or.inl fun j hj _ => by simp⟩ + have hca : CapRelA (.PageTable ptr false 0#u64 0#u64) + (.archObjectCap (.pageTableCap (specPtr ptr) .normalPtT none)) := + ⟨show (0#u64 : U64).val < 2 ^ 16 by simp, rfl⟩ + have hok : CapObjOkA Z (.PageTable ptr false 0#u64 0#u64) := + ⟨fun _ h => (by cases h), fun _ _ _ _ _ h => (by cases h), fun _ _ _ h => (by cases h), + fun _ _ _ h => (by cases h), trivial⟩ + have hTr : TreeA st OS Z.G.chains Z.G.par s1 := + J1.j.1.cdt.tree.grow J1.j5.2.1 (fun x hx hn => J1.j5.2.2 x hx hn) + have hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s1 y = .Null := J1.j5.2.2 + have hTr0 : TreeA st OS Z.G.chains Z.G.par s := + J.j.1.cdt.tree.grow J.j5.2.1 (fun x hx hn => J.j5.2.2 x hx hn) + have hnul0 : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null := J.j5.2.2 + have E1 := J1.toJEntA + have hN := bxi_nodes E1.sys.ent.bxi + have hpOS := VsPgOffOSA.of_apA E1.ap + have hirq : CapOkA (stUpdA σ σ.curThread x .restart).interruptIrqNode cte + (.archObjectCap (.pageTableCap (specPtr ptr) .normalPtT none)) := by + refine ⟨fun irq hlt e => ?_, fun ⟨_, _, _, _, e⟩ => by cases e⟩ + have obj : ∃ o ac, (stUpdA σ σ.curThread x .restart).kheap cte.1 = some o ∧ + Fl4SpecA64.capOf o cte.2 = some ac ∧ CapRelA (capAtA (L.mem s1) (cslotAddrA cte)) ac := by + obtain ⟨o, idx⟩ := cte + obtain ⟨p, e1, hc | ⟨ht, i, hi, e2⟩⟩ := hab + · simp only at e1 hc; subst e1 + obtain ⟨cs, hk, -, hr⟩ := J1.j.1.st.cnode p _ hc + obtain ⟨ac, hac, hrel⟩ := hr idx rfl + exact ⟨_, ac, hk, hac, hrel⟩ + · simp only at e1 e2; subst e1 e2 + obtain ⟨y, hk, hr⟩ := J1.j.1.st.tcb p ht + obtain ⟨ac, hac, hrel⟩ := hr i hi + refine ⟨_, ac, hk, hac, ?_⟩ + unfold cslotAddrA; rw [ofBl_tcbIdx i (by unfold tcbSlotsA at hi; omega)]; exact hrel + obtain ⟨o, ac, hko, hco, hrel⟩ := obj + rw [e] at hko + rw [c1'] at hrel + rcases J1.sinv.irqN.slot irq hlt o _ ac hko hco with rfl | ⟨_, _, _, rfl⟩ + · cases hrel.2 + · cases hrel.2 + have hsci : NoIdleCapV m n (.archObjectCap (.pageTableCap (specPtr ptr) .normalPtT none)) := + ⟨fun _ _ e => (by cases e), fun _ e => (by cases e), fun _ _ _ e => (by cases e)⟩ + have main := J1.of_capVsFrameA (hpt := fun _ _ _ h => by cases h) + (Q := fun s' a' σ' hw => vspace.decode_page_table st ar s a fl4_abi.label.ARMPageTableUnmap length + (cslotAddrA cte) (.PageTable ptr true asid va) buf ex = ok (core.result.Result.Ok (), s', a') ∧ + ((), σ') ∈ (Fl4SpecA64.performPageTableInvocation (Z := Unit) (.pageTableUnmap (.pageTableCap (specPtr ptr) + .normalPtT (some (BitVec.ofNat 16 asid.val, va.bv))) cte) (stUpdA σ σ.curThread x .restart)).1 ∧ hw = Z.hw) + RS.sub (footNOff_ksA rfl rfl (footN_offA E1)) (footNAl_ksA rfl rfl (FootNA.aligned E1.j.1 hN E1.scAl E1.ap.vcAl)) + cte hab hEnc hca hTr hnul (fun h => by cases h) (by rw [c1]; exact fun h => by cases h) + (c := .PageTable ptr false 0#u64 0#u64) (by rw [c1]; exact .pt ..) hok hirq hsci + (fun s2 σ2 hs2 hσ2 HSs w jx hcx => by + have hS1' : Fl4SpecA64.asidRootKind (stUpdA σ σ.curThread x .restart) (BitVec.ofNat 16 asid.val) = none ∨ + Fl4SpecA64.asidRootKind (stUpdA σ σ.curThread x .restart) (BitVec.ofNat 16 asid.val) = some .stage1 := by + rw [asidRootKind_stUpdA hk]; exact hS1 + have hU := unmap_page_tableA (ar := ar) (a := a) E1.j.1.vs (by rw [hks]; exact hK) + (fun p hp i hi => by rw [hks]; exact hT p i hi (Or.inr (Or.inr (Or.inl hp)))) E1.j.2.2.1.obj h16 + (hs1_ofE hS1') va ptr + obtain ⟨s2u, fa, σu, hfa, hu, hσu, J2, hk2, hks2, hcpu2, UQ, hcase⟩ := unmap_post2A + (unmapPageTable_kmA _ _ _) (unmapPageTable_unQ _ _ _) E1.j (FootA.aligned E1.j.1 hN E1.scAl E1.ap.vcAl) + (fun q hq => (hTO q hq).of_ksA hks hcpu) E1.sys.ent.os hpOS hU + have hks2' : L.ksb s2u = L.ksb s := hks2.trans hks + have hcpu2' : L.cpu s2u = L.cpu s := hcpu2.trans hcpu + have hN2 : ∀ c ∈ Z.Cs, ∀ o : U64, o.val < 4288 → o.val % 8 = 0 → Aligned (L.nodeAt s2u c o) := + fun c hc o h1 h2 => by rw [nodeAt_ksb hks2]; exact hN c hc o h1 h2 + obtain ⟨t', hty', hinv', hrun0, hz0, J4, hk3, hks3, hcpu3⟩ := zero_post2A J2 + (FootA.aligned J2.1 hN2 E1.scAl E1.ap.vcAl) (fun q hq => (hTO q hq).of_ksA hks2' hcpu2') + (hEO.of_ksA hks2' hcpu2') (by rw [hks2']; exact hK) (by rw [hks2']; exact hT) hO hpOS (Or.inl hpS) + obtain ⟨-, ⟨⟨⟨⟩, σ5⟩, h5⟩, h5m⟩ := doMachineOp_okA (mop := Fl4SpecA64.cleanCacheRange_PoU (specPtr ptr) + (specPtr ptr + Fl4SpecA64.mask (Fl4SpecA64.ptBits .normalPtT)) (Fl4SpecA64.addrFromPPtr (specPtr ptr))) + (machineOpLift_okA _) (ptSetA σu (specPtr ptr) t') + have hm5 := h5m _ h5 + have J5 := J4.of_msA (σ' := σ5) hm5 + have UQz : UnQ σu (ptSetA σu (specPtr ptr) t') := by + have hm0 := OutQ.mapMx UnQ.refl trQ (fun sl => storePte_unQ .normalPtT sl) + (Fl4SpecA64.uptoEnumStep (specPtr ptr) (specPtr ptr + ((1 : Fl4SpecA64.Word) <<< Fl4SpecA64.pteBits)) + (specPtr ptr + Fl4SpecA64.mask (Fl4SpecA64.ptBits .normalPtT))) σu ((), ptSetA σu (specPtr ptr) t') + rw [hrun0] at hm0 + exact hm0 (Set.mem_singleton _) + have UQc : UnQ (ptSetA σu (specPtr ptr) t') σ5 := doMachineOp_unQ _ _ ((), σ5) h5 + have UQ5 : UnQ (stUpdA σ σ.curThread x .restart) σ5 := UQ.trans (UQz.trans UQc) + have hk5 : (σ5.kheap : _) = (ptSetA σu (specPtr ptr) t').kheap := by + obtain ⟨ms, e⟩ := hm5 + have e' : σ5 = _ := e + rw [e'] + obtain ⟨s5, σ6, hsc, ⟨c', heq', hg⟩, hσ6, HSu, J6, T6⟩ := setcap_core2A E0 RS J5 + ((hOS1.trans' hk2).trans' hk3) (hks3.trans hks2') (hcpu3.trans hcpu2') cte hab hcap hEnc hca + (UA.of_shapeA rfl fun h => by cases h) hok (fun h => by cases h) (fun h => by cases h) hTr0 hnul0 + obtain ⟨asid', va', rfl, _, _⟩ := CapEqA.pt_mapped_iff.mp heq' + have hperf := perfPtUnmapN_memA (p := specPtr ptr) (asid := BitVec.ofNat 16 asid.val) (v := va.bv) (cte := cte) + (σz := ptSetA σu (specPtr ptr) t') + (ac := .pageTableCap (specPtr ptr) .normalPtT (some (asid', va'))) hσu hrun0 h5 hg rfl hσ6 + obtain ⟨a', hfa', hAR⟩ : ∃ a', fa = ok a' ∧ AR a' Z.hw := by + rcases hfa with rfl | rfl + · exact ⟨a, rfl, J6.1.ar⟩ + · exact spec_ex_fd (K.invAsid a Z.hw asid J6.1.ar) + have J6' := J6.of_arA hAR + have hdec' : vspace.decode_page_table st ar s a fl4_abi.label.ARMPageTableUnmap length (cslotAddrA cte) + (.PageTable ptr true asid va) buf ex = ok (core.result.Result.Ok (), s5, a') := by + rw [pt_unmap_eqA ar s a length (cslotAddrA cte) ptr asid va buf ex hf, heq, bind_ok] + refine (then_setcapA (then_zeroA hu ptr) hsc).trans ?_ + rw [hfa', bind_ok] + obtain ⟨Ac', hsl, hsy, hbx, hus, hj7⟩ := fin x s5 σ6 a' hk hdec' hperf + have al_x : Aligned (cslotAddrA cte) := hO.al0 hx + have al_x8 : Aligned (wa (cslotAddrA cte) 8#u64) := hO.al hx (k := 8#u64) (by simp) (by simp) + obtain ⟨w0, w1, hen, -⟩ := CapRt.decode_encode _ hEnc + have hpl : ptr.val < 2 ^ 64 := ptr.bv.isLt + have hfit : ptr.val + (4096#u64 : U64).val ≤ 2 ^ 64 := by + show ptr.val + 4096 ≤ 2 ^ 64; simp only [Nat.reducePow] at hpl ⊢; omega + have wU : WritesIn st s1 s2u (fun b => ∃ p ∈ Z.S1, ∃ i : U64, i.val < 512 ∧ b = peAF p i) := by + rcases hcase with rfl | ⟨p, hp, i, hi, hal, rfl⟩ + · exact WritesIn.refl _ _ + · exact (writesIn_wr hal).mono fun b hb => ⟨p, hp, i, hi, hb⟩ + have wZ : WritesIn st s2u (L.zr s2u ptr 4096#u64) (fun b => ∃ p ∈ Z.S1, ∃ i : U64, i.val < 512 ∧ b = peAF p i) := + WritesIn.monoA (writesIn_zrV (st := st) (s := s2u) (show ptr.val % 8 = 0 by omega) (by simp) hfit) fun b hb h => by + obtain ⟨i, hi, e⟩ := peAF_ofRangeA hp4 hb h.1 h.2 + exact ⟨ptr, hpS.1, i, hi, e⟩ + have wV := wU.trans1 wZ + have wS := writesIn_setCapA al_x al_x8 hsc + have w2 := WritesIn.setCapCommA al_x al_x8 hen hs2 hsc wV + have hk3' : L.ksb s5 = L.ksb s1 := wS.1.trans (hks3.trans hks2) + have hcpu5 : L.cpu s5 = L.cpu s1 := wS.2.1.trans (hcpu3.trans hcpu2) + obtain ⟨Pt, Ub, vr, -, -, -, -, -, -, link, -, -⟩ := J1.j7 + obtain ⟨Qb, Rb, q⟩ := J1.sys.ent.bxi.1 + have vo : VrOK H.omega H.frame H.scs H.scBits OS Z.V ∅ (L.ksb s1) (utVsPtA st Pp Tb Lv s1) := J1.vsOk + have Psp : ∀ b, (∃ p ∈ Z.S1, ∃ i : U64, i.val < 512 ∧ b = peAF p i) → VsR (L.ksb s1) Pt b := + fun b ⟨p, hp, i, hi, e⟩ => by rw [e]; exact vsW_pageA link (Or.inr (Or.inl hp)) hi + have hzG : ∀ Pt' : Set U64, (∀ b, VsR (L.ksb s1) Pt' b → Aligned b) → + (∀ b, VsR (L.ksb s1) Pt' b → ¬ InOS OS b) → VsZ st (L.ksb s1) Pt' s1 s5 := fun Pt' hal hos b hb => by + have hb1 := hal b hb + have hns : ¬ SlotWordA (cslotAddrA cte) b := fun h => hos b hb (slotWord_inOS hO hx h) + rw [wS.mem_eq hb1 hns] + rcases zr_caseA (st := st) (s := s2u) hp4 hb1 with e | e + · exact Or.inr e + · rw [e] + rcases hcase with rfl | ⟨p, hp, i, hi, hal', rfl⟩ + · exact Or.inl rfl + · by_cases e : peAF p i = b + · subst e; exact Or.inr (L.mem_wr_same hal') + · exact Or.inl (L.mem_wr_ne hal' hb1 (fun h => e (UScalar.eq_of_val_eq h))) + have hAsK : ∀ b, AsW (L.ksb s1) b → L.mem s5 b = L.mem s1 b := fun b hb => by + have hnP : ¬ ∃ p ∈ Z.S1, ∃ i : U64, i.val < 512 ∧ b = peAF p i := fun ⟨p, hp, i, hi, e⟩ => + hT p i hi (Or.inr (Or.inr (Or.inl hp))) (by rw [← e, ← hks]; exact hb) + have hns : ¬ SlotWordA (cslotAddrA cte) b := fun h => vo.os b (Or.inr hb) (slotWord_inOS hO hx h) + exact (wS.mem_eq (vo.al b (Or.inr hb)) hns).trans (wV.mem_eq (vo.al b (Or.inr hb)) hnP) + have vsT := VsTyR.zeroStepA hk3' hAsK (hzG _ vo.al vo.os) J1.sys.ent.vs + have osk3 : OSKeep st OS s2 s5 := + osKeep_ofA hO fun w' hw' hin => w2.mem_eq hw' (fun hp => vr.os w' (Psp w' hp) hin) + obtain ⟨hcx', hco⟩ := capA_setCapA hO hx hEnc hs2 + have capE3 : ∀ u ∈ OS, capA st s5 u = capA st s2 u := fun u hu => osk3.capA hu + have hnew : capA st s5 (cslotAddrA cte) = .PageTable ptr false 0#u64 0#u64 := by rw [capE3 _ hx, hcx] + have hkeep : ∀ u ∈ OS, u ≠ cslotAddrA cte → capA st s5 u = capA st s1 u := fun u hu e => by + rw [capE3 u hu, hco u hu e] + have hMs2 : ∀ u ∈ OS, MapAlikeA (capA st s2 u) (capA st s1 u) := fun u hu => by + by_cases e : u = cslotAddrA cte + · subst e; rw [hcx, c1]; exact .pt .. + · rw [hco u hu e]; exact .refl _ + have sbS : ∀ t ∈ Z.T, sbWordA t ∈ OS := fun t ht => + RS.sub _ ⟨t, Or.inr ⟨(J1.j.1.thT t).2 ht, 2, by simp [tcbSlotsA], (bufSlot_eqA t).symm⟩⟩ + have wA : WritesIn st s1 s2 (fun b => Aligned b ∧ W7.CapW H.tcbs {b | InOS OS b} ∅ b) := writesIn_alAG w + have hB : ∀ b, (Aligned b ∧ W7.CapW H.tcbs {b | InOS OS b} ∅ b) → W7.CapW H.tcbs H.omega H.frame b := + fun b hb => os_capw hO hb.1 hb.2 + obtain ⟨-, -, -, -, -, fld, -, -⟩ := W7.blk_of_capw (BXI.blk J1.sys.ent.bxi) wA hB + have ib : ∀ t ∈ Z.T, L.mem s2 (ibWordA t) = L.mem s1 (ibWordA t) := fun t ht => + fld t (by rw [J1.tcbs]; exact ht) 3512#u64 (by simp) (by simp) (by simp) + have HS2 : VsSpecA σ2 σ6 := (VsSpecA.of_unQ UQ5).setCapCommA hσ2 hσ6 + have offT : ∀ b, (∃ p ∈ Z.S1, ∃ i : U64, i.val < 512 ∧ b = peAF p i) → ∀ t ∈ Z.T, + b.val < t.val ∨ t.val + 4096 ≤ b.val := fun b hb t ht => by + have hth : t ∈ H.tcbs := by rw [J1.tcbs]; exact ht + have := (q.omega b (vr.om b (Psp b hb))).2.1 t hth + omega + have tw : ∀ t ∈ Z.T, ∀ K : U64, K.val + 8 ≤ 4096 → K.val % 8 = 0 → + L.mem s5 (wa t K) = L.mem s2 (wa t K) := by + intro t ht K hK' h8 + have hta := J1.sr.core.tcbs t ht + have hv : (wa t K).val = t.val + K.val := field_val hta (K := K) (by omega) + refine w2.mem_eq (field_aligned hta (by omega) h8) (fun h => ?_) + have := offT _ h t ht + omega + have hib : ∀ t ∈ Z.T, IbSame (L := L) s2 s5 t := fun t ht => by + refine ⟨tw t ht _ (by simp) (by simp), ?_, ?_⟩ + · rw [sbWordA_eq]; exact tw t ht _ (by simp) (by simp) + · rw [sbWordA8_eq]; exact tw t ht _ (by simp) (by simp) + have hj7' := hj7 + obtain ⟨Pt', Ub', vr', -, ac', -⟩ := hj7' + have hBw : ∀ b, ((∃ p ∈ Z.S1, ∃ i : U64, i.val < 512 ∧ b = peAF p i) ∨ SlotWordA (cslotAddrA cte) b) → + L.Free s1 H.omega Z.Cs b ∨ W7.TLow H.tcbs b := fun b hb => by + rcases hb with hp | hsl + · exact Or.inl (Or.inl (vr.om b (Psp b hp))) + · have hal : Aligned b := by rcases hsl with rfl | rfl; exact al_x; exact al_x8 + exact (os_capw hO hal (Or.inr ⟨slotWord_inOS hO hx hsl, Set.notMem_empty _⟩)).elim Or.inr + fun h' => Or.inl (Or.inl h'.1) + have hsp : ∀ q', SchedPageA st Z OS s5 q' → SchedPageA st Z OS s1 q' := fun q' h => + SchedPageA.backPtA hnew hkeep hpS.1 h + -- the new cap's table + have hz5 : ∀ i : U64, i.val < 512 → L.mem s5 (peAF ptr i) = 0#u64 := fun i hi => by + have hv : VsR (L.ksb s1) Pt (peAF ptr i) := vsW_pageA link (Or.inr (Or.inl hpS.1)) hi + rw [wS.mem_eq (vr.al _ hv) (fun h => vr.os _ hv (slotWord_inOS hO hx h))] + exact hz0 i hi + have htab : UnmTabA st Z m s5 σ6 .normalPtT ptr := by + refine ⟨fun i hi => by rw [hz5 i hi]; unfold dBitA; rfl, ⟨t', ?_, hty', hinv'⟩, + (hTO ptr hpS.1).of_ksA (hk3'.trans hks) (hcpu5.trans hcpu)⟩ + have h1 : σ5.kheap (specPtr ptr) = some (.archObj (.pageTable t')) := by + rw [hk5, ptSetA_kheap, if_pos rfl] + exact HSu.keep h1 (fun _ _ e => by cases e) (fun _ e => by cases e) + refine ⟨s5, a', σ6, Z.hw, fun b => ∃ p ∈ Z.S1, ∃ i : U64, i.val < 512 ∧ b = peAF p i, Ac', + ⟨w2, fun Pt'' Ub'' vr'' link'' b ⟨p, hp, i, hi, e⟩ => by rw [e]; exact vsW_pageA link'' (Or.inr (Or.inl hp)) hi, + fun b ⟨p, hp, i, hi, e⟩ hbuf => J1.bufOff.page b hbuf p (Or.inr (Or.inl hp)) + (core.num.U64.wrapping_mul i 8#u64) (by rw [wm8_valA hi]; omega) (by rw [wm8_valA hi]; omega) + (by rw [e]; rfl), + HS2, ?_, ?_, J6', ⟨⟨J1.sys.ent.heap, hbx, hO, hus, vsT.1, by rw [hk3']; exact J1.sys.ent.ksw⟩, hsl⟩, + UtVsOkA.zeroStepA hk3' vsT.2 J1.vsOk, bufVs_subA J1.bufVs hk3' vsT.2, + W7.inv_of_capw J1.sq (wV.trans wS) hBw, ac'.case.po, + ?_, ?_, ?_, ?_, hj7, hsy⟩, hdec', hperf, rfl⟩ + · rw [setCap_archA hσ6, setCap_archA hσ2]; exact UQ5.1.asidTable + · intro p dv pg h + have h1 : (stUpdA σ σ.curThread x .restart).kheap p = some (.archObj (.dataPage dv pg)) := + HSs.back h (fun _ _ e => by cases e) (fun _ e => by cases e) + have h2 : σ5.kheap p = some (.archObj (.dataPage dv pg)) := by + rcases UQ5.2 p with e | ⟨t0, t0', a0, -⟩ + · exact e.trans h1 + · rw [h1] at a0 + have h3 := Option.some.inj a0 + injection h3 with h3 + cases h3 + exact HSu.keep h2 (fun _ _ e => by cases e) (fun _ e => by cases e) + · exact BufCover.vsA (BufCover.mapA J1.bufCover J1.bufNative J1.j.1.st jx.1.st (fun t ht => (J1.j.1.thT t).2 ht) + HSs ib (fun t ht => hMs2 _ (sbS t ht))) HS2 hib + · exact BufNativeA.mapA J1.bufNative (fun t ht q' sz' rr dv' m' e' a'' v' h => by + by_cases e1 : sbWordA t = cslotAddrA cte + · rw [e1, hnew] at h; cases h + · rw [hkeep _ (sbS t ht) e1] at h + have hn := J1.bufNative t ht + rw [bufSlot_eqA, capAtA_eq (st := st), h] at hn + exact hn) + · exact UnmPtSpecA.ofPtUnmMA (Pt := Pt) hnew hkeep + (fun q' hq i hi => hzG Pt vr.al vr.os _ (Or.inl ⟨q', hq, core.num.U64.wrapping_mul i 8#u64, + by rw [wm8_valA hi]; omega, by rw [wm8_valA hi]; omega, rfl⟩)) + link hk3' hcpu5 UQ5.2.toX (fun q' ao h => HSu.keep h (fun _ _ e => by cases e) (fun _ e => by cases e)) + htab J1.unmPt + · exact ⟨J1.bufOff.os, fun b hb => by rw [hk3']; exact J1.bufOff.vs b hb, + fun b hb q' hq => J1.bufOff.page b hb q' (hsp q' hq), J1.bufOff.vcw, + fun bp hbp => (J1.bufOff.pin bp hbp).keepM fun u hu => by rw [capE3 u hu]; exact hMs2 u hu⟩) + obtain ⟨s2, σ2, s3, a3, σ3, hw, P, Ac', hs2, hσk, HSsk, HSvk, HSc, wT, Jf, hdec, hperf, hhw⟩ := main + subst hhw + exact ⟨x, s3, a3, σ3, Ac', hk, hset, hdec, hperf, Jf, + (Sched.BodyKeepA.ofSlot HSsk (setCap_keepsA hσk).2.2).trans (Sched.BodyKeepA.ofVs HSvk)⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3ArmPtUnmUA.lean b/proofs/Fl4ProofsA64/P3/Stage3ArmPtUnmUA.lean new file mode 100644 index 000000000..7cefce2a2 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ArmPtUnmUA.lean @@ -0,0 +1,271 @@ +/- +P3 (AArch64, merge line, D8 step 5 (ii)): **closing the `ARMPageTableUnmap` arm of a final unmapped page-table cap** +(`pt_unmapU_decJA`'s arm). The arm is the caller's Restart followed by the in-place cap write that clears the map data +(no VSpace word is written), so the closure is `of_restartCurA` then `of_capFrame`, as `frameUnmapUA`. + +The old and the new cap are unmapped tables of the same `ptr`: J9 (`unmPt`) for the new cap is the old cap's +(`UnmPtSpecA.ofUnmPtA`), the IPC-buffer fields and the buffer placement are derived. An unmapped `PageTable` cap is +read by R2 and its `asid` / `va` are not canonical (`CapRt.EncWf` bounds them only), so **J7 is not derived**: the arm +hypothesis `fin` is `SysL` and J7 at the final state (Option A / X, owed by T3). + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3BodyKeepA +import Fl4ProofsA64.P3.Stage3ArmFrUnmA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-- **An unmapped table cap rewritten as an unmapped table cap of the same table** at slot `x`, the other `OS` slots and +the kernel base kept. -/ +structure UnmPtWrA (st : machine.Store S) [L : LawfulStore st] (OS : Set U64) (s s' : S) (x p a v : U64) : Prop where + hx : x ∈ OS + old : capA st s x = .PageTable p false a v + new : capA st s' x = .PageTable p false 0#u64 0#u64 + keep : ∀ y ∈ OS, y ≠ x → capA st s' y = capA st s y + ksb : L.ksb s' = L.ksb s + +namespace UnmPtWrA +variable {OS : Set U64} {s s' : S} {x p a v : U64} + +theorem alike (W : UnmPtWrA st OS s s' x p a v) : ∀ y ∈ OS, MapAlikeA (capA st s' y) (capA st s y) := + fun y hy => by + by_cases h : y = x + · subst h; rw [W.new, W.old]; exact .pt .. + · rw [W.keep y hy h]; exact .refl _ + +/-- A cap that is no unmapped table is at the same slot before and after. -/ +theorem back (W : UnmPtWrA st OS s s' x p a v) {y : U64} (hy : y ∈ OS) {c : capability.Cap} + (h : capA st s' y = c) (hc : ∀ q a v, c ≠ .PageTable q false a v) : capA st s y = c := by + by_cases e' : y = x + · subst e'; exact absurd (h.symm.trans W.new) (hc _ _ _) + · rw [← W.keep y hy e']; exact h + +/-- An unmapped table cap after is one of the same table before. -/ +theorem backPt (W : UnmPtWrA st OS s s' x p a v) {y : U64} (hy : y ∈ OS) {q a' v' : U64} + (h : capA st s' y = .PageTable q false a' v') : ∃ a'' v'', capA st s y = .PageTable q false a'' v'' := by + by_cases e' : y = x + · subst e' + rw [W.new] at h + injection h with h1 + subst h1 + exact ⟨a, v, W.old⟩ + · exact ⟨a', v', by rw [← W.keep y hy e']; exact h⟩ + +theorem schedPage (W : UnmPtWrA st OS s s' x p a v) {Z : XSetsA} {q : U64} + (h : SchedPageA st Z OS s' q) : SchedPageA st Z OS s q := by + unfold SchedPageA at h ⊢ + rcases h with h | h | h | h | h | h | h | ⟨u, hu, h⟩ + · exact Or.inl h + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr (Or.inl h)) + · exact Or.inr (Or.inr (Or.inr (Or.inl h))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inl h)))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl h))))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl h)))))) + · refine Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ⟨u, hu, ?_⟩)))))) + rcases h with ⟨m, a', v', h⟩ | ⟨m, a', h⟩ | ⟨a', v', h⟩ | ⟨q', a', v', h, e'⟩ | ⟨y, a', h, e'⟩ + · cases m with + | false => + obtain ⟨a'', v'', h'⟩ := W.backPt hu h + exact Or.inl ⟨false, a'', v'', h'⟩ + | true => exact Or.inl ⟨true, a', v', W.back hu h fun _ _ _ f => by cases f⟩ + · exact Or.inr (Or.inl ⟨m, a', W.back hu h fun _ _ _ f => by cases f⟩) + · exact Or.inr (Or.inr (Or.inl ⟨a', v', W.back hu h fun _ _ _ f => by cases f⟩)) + · exact Or.inr (Or.inr (Or.inr (Or.inl ⟨q', a', v', W.back hu h (fun _ _ _ f => by cases f), e'⟩))) + · exact Or.inr (Or.inr (Or.inr (Or.inr ⟨y, a', W.back hu h (fun _ _ _ f => by cases f), e'⟩))) + +end UnmPtWrA + +/-- **J9 across an unmapped-table cap rewrite.** -/ +theorem UnmPtSpecA.ofUnmPtA {Z : XSetsA} {OS : Set U64} {s s' : S} {σ σ' : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} {x p a v : U64} + (W : UnmPtWrA st OS s s' x p a v) (HS : SpecSlotA σ σ') (hcpu : L.cpu s' = L.cpu s) + {Pt : Set U64} (mem : VsKeep st (L.ksb s) Pt s s') (link : ∀ q, SchedPageA st Z OS s q → q ∈ Pt) + (h : UnmPtSpecA st Z m OS s σ) : UnmPtSpecA st Z m OS s' σ' := by + have word : ∀ q ∈ Pt, ∀ i : U64, i.val < 512 → L.mem s' (peAF q i) = L.mem s (peAF q i) := fun q hq i hi => + mem _ (Or.inl ⟨q, hq, core.num.U64.wrapping_mul i 8#u64, by rw [wm8_valA hi]; omega, + by rw [wm8_valA hi]; omega, rfl⟩) + have kk : ∀ q ao, σ.kheap q = some (.archObj ao) → σ'.kheap q = some (.archObj ao) := fun q ao h => + HS.keep h (fun _ _ e => by cases e) (fun _ e => by cases e) + have tab : ∀ ty q, q ∈ Pt → UnmTabA st Z m s σ ty q → UnmTabA st Z m s' σ' ty q := fun ty q hq h => by + obtain ⟨t, k1, k2, k3⟩ := h.spec + exact ⟨fun i hi => by rw [word q hq i hi]; exact h.inv i hi, ⟨t, kk _ _ k1, k2, k3⟩, + h.off.of_ksA W.ksb hcpu⟩ + refine ⟨fun u hu q asid va hc => ?_, fun u hu q asid hc => ?_, fun u hu q asid va hc => ?_, + fun u hu q asid hc => ?_⟩ + · obtain ⟨a'', v'', o⟩ := W.backPt hu hc + exact tab _ _ (link q (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inl ⟨_, _, _, o⟩⟩)))))))) (h.pt u hu q a'' v'' o) + · have o := W.back hu hc (fun _ _ _ f => by cases f) + exact tab _ _ (link q (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inl ⟨_, _, o⟩)⟩)))))))) (h.vs u hu q asid o) + · have o := W.back hu hc (fun _ _ _ f => by cases f) + have l0 : q ∈ Pt := link q (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inr (Or.inl ⟨_, _, o⟩))⟩))))))) + have l1 : wa q 4096#u64 ∈ Pt := link _ (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inr (Or.inr (Or.inl ⟨q, _, _, o, rfl⟩)))⟩))))))) + have h0 := h.s2 u hu q asid va o + obtain ⟨es, k1, k2⟩ := h0.spec + refine ⟨fun i hi => ?_, es, kk _ _ k1, k2⟩ + by_cases hlo : i.val < 512 + · rw [word q l0 i hlo]; exact h0.inv i hi + · obtain ⟨j, hj, ej⟩ := peAF_hiA q i (by omega) hi + have := h0.inv i hi + rw [ej] at this ⊢ + rw [word _ l1 j hj]; exact this + · have o := W.back hu hc (fun _ _ _ f => by cases f) + have l1 : wa q 4096#u64 ∈ Pt := link _ (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inr (Or.inr (Or.inr ⟨q, _, o, rfl⟩)))⟩))))))) + have h0 := h.hv u hu q asid o + obtain ⟨l, k1, k2⟩ := h0.spec + exact ⟨fun i hi => by rw [word _ l1 i hi]; exact h0.inv i hi, l, kk _ _ k1, k2⟩ + +set_option maxHeartbeats 2000000 in +/-- **`JStage3A` across the `ARMPageTableUnmap` arm of a final unmapped page-table cap** (module docstring): FL4's +decode succeeds, the spec takes the Restart and l4v's `perform_pt_inv_unmap` (the `None` arm and the cap write), and +the record holds at the final state. The final-state facts owed at T3 (`SysL`, J7) are the arm hypothesis `fin`. -/ +theorem JStage3A.ptUnmapUA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {ar : machine.Arch A} {nA : U64} {s : S} {a : A} + {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) + (hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM) (length buf : U64) + (ex : Aeneas.Std.Array obj.Slot 3#usize) (cte : Fl4SpecA64.CslotPtr) (hab : AbsSlotA Z.th cte) + (ptr asid va : U64) (hcap : capA st s (cslotAddrA cte) = .PageTable ptr false asid va) + (hf : cdt.is_final_capability st s (cslotAddrA cte) = ok true) + (fin : ∀ s2 (σ2 : Fl4SpecA64.State Unit), + obj.Slot.set_cap st (cslotAddrA cte) (L.wr s (stA (L.curW s)) sched.TS_RESTART) + (.PageTable ptr false 0#u64 0#u64) = ok s2 → + SysL st H Z.Cs OS ia ie s2 ∧ SymL st H Z.Cs s2 ∧ J7A st (J7R2A st ar nA) Z H OS s2 σ2 m) : + ∃ x s2 σ2, σ.kheap σ.curThread = some (.tcb x) ∧ + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + vspace.decode_page_table st ar s a fl4_abi.label.ARMPageTableUnmap length (cslotAddrA cte) + (.PageTable ptr false asid va) buf ex = ok (core.result.Result.Ok (), s2, a) ∧ + ((), σ2) ∈ (Fl4SpecA64.performPageTableInvocation (Z := Unit) + (.pageTableUnmap (.pageTableCap (specPtr ptr) .normalPtT none) cte) (stUpdA σ σ.curThread x .restart)).1 ∧ + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s2 a σ2 m ∧ + Sched.BodyKeepA (stUpdA σ σ.curThread x .restart) σ2 := by + obtain ⟨x, hk, heq, hset, J1, -, hOS1, -⟩ := J.of_restartCurA RS.pl.choose_spec.choose_spec.choose_spec.choose_spec + RS.sym RS.run hnvm RS.cur RS.vs RS.vc + have hx : cslotAddrA cte ∈ OS := RS.sub _ (hab.isSlot J.j.1.place) + have hO : OSOK H.tcbs H.omega H.frame OS := J1.sys.ent.os + have c1 : capA st (L.wr s (stA (L.curW s)) sched.TS_RESTART) (cslotAddrA cte) = + .PageTable ptr false asid va := (hOS1.capA hx).trans hcap + have c1' : capAtA (L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART)) (cslotAddrA cte) = + .PageTable ptr false asid va := c1 + have hEnc0 : CapRt.EncWf (capAtA (L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART)) (cslotAddrA cte)) := + CapRt.decodeF_encWf _ _ + rw [c1'] at hEnc0 + simp only [CapRt.EncWf] at hEnc0 + have hEnc : CapRt.EncWf (.PageTable ptr false 0#u64 0#u64) := by + simp only [CapRt.EncWf]; exact ⟨hEnc0.1, by simp, Or.inl fun j hj _ => by simp⟩ + have hca : CapRelA (.PageTable ptr false 0#u64 0#u64) + (.archObjectCap (.pageTableCap (specPtr ptr) .normalPtT none)) := + ⟨show (0#u64 : U64).val < 2 ^ 16 by simp, rfl⟩ + have hok : CapObjOkA Z (.PageTable ptr false 0#u64 0#u64) := + ⟨fun _ h => (by cases h), fun _ _ _ _ _ h => (by cases h), fun _ _ _ h => (by cases h), + fun _ _ _ h => (by cases h), trivial⟩ + have hTr : TreeA st OS Z.G.chains Z.G.par (L.wr s (stA (L.curW s)) sched.TS_RESTART) := + J1.j.1.cdt.tree.grow J1.j5.2.1 (fun x hx hn => J1.j5.2.2 x hx hn) + have hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st (L.wr s (stA (L.curW s)) sched.TS_RESTART) y = .Null := + J1.j5.2.2 + have E1 := J1.toJEntA + have hN := bxi_nodes E1.sys.ent.bxi + have hirq : CapOkA (stUpdA σ σ.curThread x .restart).interruptIrqNode cte + (.archObjectCap (.pageTableCap (specPtr ptr) .normalPtT none)) := by + refine ⟨fun irq hlt e => ?_, fun ⟨_, _, _, _, e⟩ => by cases e⟩ + have obj : ∃ o ac, (stUpdA σ σ.curThread x .restart).kheap cte.1 = some o ∧ + Fl4SpecA64.capOf o cte.2 = some ac ∧ + CapRelA (capAtA (L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART)) (cslotAddrA cte)) ac := by + obtain ⟨o, idx⟩ := cte + obtain ⟨p, e1, hc | ⟨ht, i, hi, e2⟩⟩ := hab + · simp only at e1 hc; subst e1 + obtain ⟨cs, hk, -, hr⟩ := J1.j.1.st.cnode p _ hc + obtain ⟨ac, hac, hrel⟩ := hr idx rfl + exact ⟨_, ac, hk, hac, hrel⟩ + · simp only at e1 e2; subst e1 e2 + obtain ⟨y, hk, hr⟩ := J1.j.1.st.tcb p ht + obtain ⟨ac, hac, hrel⟩ := hr i hi + refine ⟨_, ac, hk, hac, ?_⟩ + unfold cslotAddrA; rw [ofBl_tcbIdx i (by unfold tcbSlotsA at hi; omega)]; exact hrel + obtain ⟨o, ac, hko, hco, hrel⟩ := obj + rw [e] at hko + rw [c1'] at hrel + rcases J1.sinv.irqN.slot irq hlt o _ ac hko hco with rfl | ⟨_, _, _, rfl⟩ + · cases hrel.2 + · cases hrel.2 + have hsci : NoIdleCapV m n (.archObjectCap (.pageTableCap (specPtr ptr) .normalPtT none)) := + ⟨fun _ _ e => (by cases e), fun _ e => (by cases e), fun _ _ _ e => (by cases e)⟩ + obtain ⟨s2, σ2, hs2, hσ2, HSsk, -, J2⟩ := J1.of_capFrame (hpt := fun _ _ _ h => by cases h) RS.sub + (footNOff_ksA rfl rfl (footN_offA E1)) (footNAl_ksA rfl rfl (FootNA.aligned E1.j.1 hN E1.scAl E1.ap.vcAl)) + cte hab hEnc hca hTr hnul (fun h => by cases h) (by rw [c1]; exact fun h => by cases h) + (c := .PageTable ptr false 0#u64 0#u64) (by rw [c1]; exact .pt ..) hok hirq hsci + (fun s2 σ2 hs2 hσ2 HS w jx hc2 => by + obtain ⟨hcx, hco⟩ := capA_setCapA hO hx hEnc hs2 + have W : UnmPtWrA st OS (L.wr s (stA (L.curW s)) sched.TS_RESTART) s2 (cslotAddrA cte) ptr asid va := + ⟨hx, c1, hcx, hco, w.1⟩ + have vk : ∀ Pt Ub : Set U64, VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub + (L.ksb (L.wr s (stA (L.curW s)) sched.TS_RESTART)) Pt → + VsKeep st (L.ksb (L.wr s (stA (L.curW s)) sched.TS_RESTART)) Pt + (L.wr s (stA (L.curW s)) sched.TS_RESTART) s2 := fun Pt Ub vr => + VsKeep.of_w vr.al w (fun b hb => J1.vsr_offCapWA vr hb) + obtain ⟨Pt, Ub, vr, -, -, -, -, -, -, link, -, -⟩ := J1.j7 + have wA : WritesIn st (L.wr s (stA (L.curW s)) sched.TS_RESTART) s2 + (fun b => Aligned b ∧ W7.CapW H.tcbs {b | InOS OS b} ∅ b) := writesIn_alAG w + have hB : ∀ b, (Aligned b ∧ W7.CapW H.tcbs {b | InOS OS b} ∅ b) → W7.CapW H.tcbs H.omega H.frame b := + fun b hb => os_capw hO hb.1 hb.2 + obtain ⟨-, -, -, -, -, fld, -, -⟩ := W7.blk_of_capw (BXI.blk J1.sys.ent.bxi) wA hB + have sbS : ∀ t ∈ Z.T, sbWordA t ∈ OS := fun t ht => + RS.sub _ ⟨t, Or.inr ⟨(J1.j.1.thT t).2 ht, 2, by simp [tcbSlotsA], (bufSlot_eqA t).symm⟩⟩ + have ib : ∀ t ∈ Z.T, L.mem s2 (ibWordA t) = L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART) (ibWordA t) := + fun t ht => fld t (by rw [J1.tcbs]; exact ht) 3512#u64 (by simp) (by simp) (by simp) + have R1 := J1.j.1.st + have R2 := jx.1.st + obtain ⟨hsl, hsy, hj7⟩ := fin s2 σ2 hs2 + refine + { sysl := hsl + symL := hsy + unmPt := UnmPtSpecA.ofUnmPtA W HS w.2.1 (vk Pt Ub vr) link J1.unmPt + bufOff := ?_ + bufCover := BufCover.mapA J1.bufCover J1.bufNative R1 R2 (fun t ht => (J1.j.1.thT t).2 ht) HS ib + (fun t ht => W.alike _ (sbS t ht)) + bufNative := BufNativeA.mapA J1.bufNative (fun t ht q sz' rr dv m' e' a' v' h => by + by_cases e1 : sbWordA t = cslotAddrA cte + · rw [e1, hcx] at h; cases h + · rw [hco _ (sbS t ht) e1] at h + have hn := J1.bufNative t ht + rw [bufSlot_eqA, capAtA_eq (st := st), h] at hn + exact hn) + j7 := hj7 } + exact ⟨J1.bufOff.os, fun b hb => by rw [W.ksb]; exact J1.bufOff.vs b hb, + fun b hb q hq => J1.bufOff.page b hb q (W.schedPage hq), J1.bufOff.vcw, + fun bp hbp => (J1.bufOff.pin bp hbp).keepF fun t ht hO _ _ r x1 x2 x3 x4 e => + ⟨ht, hO, r, x1, x2, x3, x4, by rw [hco _ hO (fun e1 => by rw [e1, c1] at e; cases e)]; exact e⟩⟩) + obtain ⟨cg, hcg, heqg⟩ := getCap_eqA E1.j.1.st hab + change CapEqA (capA st (L.wr s (stA (L.curW s)) sched.TS_RESTART) (cslotAddrA cte)) cg at heqg + rw [c1] at heqg + have hcg' : cg = .archObjectCap (.pageTableCap (specPtr ptr) .normalPtT none) := heqg.symm + subst hcg' + have hget : (.archObjectCap (.pageTableCap (specPtr ptr) .normalPtT none), stUpdA σ σ.curThread x .restart) ∈ + (getCap (Z := Unit) cte (stUpdA σ σ.curThread x .restart)).1 := by rw [hcg]; rfl + refine ⟨x, s2, σ2, hk, hset, ?_, perfPtUnmapU_memA hget hσ2, J2, + Sched.BodyKeepA.ofSlot HSsk (setCap_keepsA hσ2).2.2⟩ + rw [pt_unmapU_eqA ar s a length (cslotAddrA cte) ptr asid va buf ex hf, heq, bind_ok, hs2, bind_ok] + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3ArmPtUnmXA.lean b/proofs/Fl4ProofsA64/P3/Stage3ArmPtUnmXA.lean new file mode 100644 index 000000000..824146590 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ArmPtUnmXA.lean @@ -0,0 +1,409 @@ +/- +P3 (AArch64, merge line, D8 step 5 (ii)): **closing the `ARMPageTableUnmap` arm of a final page-table cap mapped on an +FL4 root** (`pt_unmap_decJA`'s arm: a high window, or a Stage-2 root), for a table of the relation's Stage-1 tables +(`ptr ∈ Z.S1`, the first branch of `PtCapInA`). The shape is `P3/Stage3ArmPtUnmMA`'s: FL4's Ext unmap writes a zero +into one table page word (`unmap_post2JA`) or nothing, the 512 words of the table are zeroed, then the slot is +rewritten; the spec's `extUnmapTable` invalidates one entry of an arch object (`InvXA`), the 512 `store_pte`s are +`UnQ` steps, then the cap write. The ghost sets are kept. + +* `ArchInvA.trans`, `InvXA.trans`; `perform_tabUnmapXB`: `perform_tabUnmapXA` with the spec step shown `QvA` and + `InvXA`. +* **`JStage3A.ptUnmapXA`**: arm hypotheses as `ptUnmapS1A` (`SysL`, `BXI` at some `Ac'`, `UserOK`, J7 at the final + state). + +Not covered: `ptr ∈ Z.T2` (Architect ruling 20:03Z: shrink `T2` through the generalised `of_capVsA`). + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3BodyKeepA +import Fl4ProofsA64.P3.Stage3ArmPtUnmMA +import Fl4ProofsA64.P3.Stage3ArmFrUnmXA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +theorem ArchInvA.trans {a b c : Fl4SpecA64.ArchKernelObj} (h1 : ArchInvA a b) (h2 : ArchInvA b c) : + ArchInvA a c := by + cases a with + | asidPool f => cases b <;> exact absurd h1 (by simp [ArchInvA]) + | pageTable t => + obtain ⟨t', rfl, ty1, ap1⟩ := ArchInvA.pt h1 + obtain ⟨t'', rfl, ty2, ap2⟩ := ArchInvA.pt h2 + exact ⟨ty2.trans ty1, fun i => (ap2 i).elim (fun h => (ap1 i).imp (h.trans ·) (h.trans ·)) Or.inr⟩ + | dataPage dv sz => cases b <;> exact absurd h1 (by simp [ArchInvA]) + | vcpu v => cases b <;> exact absurd h1 (by simp [ArchInvA]) + | ext x => + cases x with + | stage2Root e => + obtain ⟨e', rfl, ap1⟩ := ArchInvA.s2 h1 + obtain ⟨e'', rfl, ap2⟩ := ArchInvA.s2 h2 + exact fun j => (ap2 j).elim (fun h => (ap1 j).imp (h.trans ·) (h.trans ·)) Or.inr + | highVSpace e => + obtain ⟨e', rfl, ap1⟩ := ArchInvA.hi h1 + obtain ⟨e'', rfl, ap2⟩ := ArchInvA.hi h2 + exact fun j => (ap2 j).elim (fun h => (ap1 j).imp (h.trans ·) (h.trans ·)) Or.inr + +theorem InvXA.trans {a b c : Fl4SpecA64.State Unit} (h1 : InvXA a b) (h2 : InvXA b c) : InvXA a c := fun q => by + rcases h1 q with e1 | ⟨o, o', a1, b1, i1⟩ + · rcases h2 q with e2 | ⟨u, u', a2, b2, i2⟩ + · exact Or.inl (e2.trans e1) + · exact Or.inr ⟨u, u', e1.symm.trans a2, b2, i2⟩ + · rcases h2 q with e2 | ⟨u, u', a2, b2, i2⟩ + · exact Or.inr ⟨o, o', a1, e2.trans b1, i1⟩ + · rw [b1] at a2 + have e : o' = u := by + have h3 := Option.some.inj a2 + injection h3 + subst e + exact Or.inr ⟨o, u', a1, b2, ArchInvA.trans i1 i2⟩ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {Pp Tb : Set U64} {Lv : U64 → Nat} {X Y T2 : Set U64} {Sh2 : U64 → Nat} {σ : Fl4SpecA64.State Unit} + +set_option maxHeartbeats 2000000 in +/-- **`perform_tabUnmapXA` with the spec step shown to invalidate entries only** (module docstring). -/ +theorem perform_tabUnmapXB {A : Type} {ar : machine.Arch A} {s : S} {a : A} + (R : VsRelA Pp Tb Lv X Y (L.mem s) (L.ksb s) σ) (E : ExtVsRelA Pp Tb X Y T2 Sh2 (L.mem s) σ) + (hK : Aligned (L.ksb s)) (hT : OffAsE Pp Tb X Y T2 (L.ksb s)) + (hX : ∀ r ∈ X, ∃ es, σ.kheap (specPtr r) = some (.archObj (.ext (.stage2Root es)))) + (hIpa : S2IpaE ar a) {asid : U64} (h16 : asid.val < 65536) (va pt : U64) (k : Fl4SpecA64.ExtRootKind) + (hk : Fl4SpecA64.asidRootKind σ (BitVec.ofNat 16 asid.val) = some k) (hk1 : k ≠ .stage1) + (slot : Fl4SpecA64.CslotPtr) : + ∃ σu fk, ArchFlE ar a fk ∧ + Fl4SpecA64.performExtVSpace (Z := Unit) + (.tableUnmap k (.pageTableCap (specPtr pt) .normalPtT (some (BitVec.ofNat 16 asid.val, va.bv))) slot) σ = + (Fl4Spec.NonDet.mapMx (fun sl => Fl4SpecA64.storePte (Z := Unit) .normalPtT sl .invalidPTE) + (Fl4SpecA64.uptoEnumStep (specPtr pt) (specPtr pt + ((1 : Fl4SpecA64.Word) <<< Fl4SpecA64.pteBits)) + (specPtr pt + Fl4SpecA64.mask (Fl4SpecA64.ptBits .normalPtT))) >>= fun _ => do + Fl4SpecA64.setCap (Z := Unit) (.archObjectCap (Fl4SpecA64.updateMapData + (.pageTableCap (specPtr pt) .normalPtT (some (BitVec.ofNat 16 asid.val, va.bv))) none)) slot + return ([] : List Fl4SpecA64.Data)) σu ∧ + ExtUnmapRelE Pp Tb Lv X Y T2 Sh2 σ st s a fk (vspace.unmap_page_table st ar s a asid va pt) ({((), σu)}, False) ∧ + QvA σ σu ∧ InvXA σ σu := by + rcases unmap_page_tableXGE R E hK hT hX hIpa h16 va pt with ⟨hn, -, -⟩ | ⟨k', fk, -, hfk, hF, hU⟩ + · rcases hn with h | h <;> rw [h] at hk + · cases hk + · exact (hk1 (Option.some.inj hk).symm).elim + have hz := perform_tabUnmapZA (σ := σ) (asid := BitVec.ofNat 16 asid.val) (va := va.bv) (p := specPtr pt) + (slot := slot) k .normalPtT + have e : extUnmapTableStX σ k' (BitVec.ofNat 16 asid.val) va.bv (specPtr pt) = + extUnmapTableStX σ k (BitVec.ofNat 16 asid.val) va.bv (specPtr pt) := + sing_stA (hF.symm.trans (fl4FinaliseCap_pt_runX hk hk1)) + refine ⟨_, fk, hfk, hz, ?_, extUnmapTableStX_invX σ k _ _ _⟩ + rw [extUnmapTable_runX, e] at hU + exact hU + +end + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +set_option maxHeartbeats 8000000 in +/-- **`JStage3A` across the `ARMPageTableUnmap` arm of a final page-table cap of a Stage-1 table mapped on an FL4 root** +(module docstring): FL4's decode succeeds, the spec performs `performExtVSpace`'s `tableUnmap`, and the record holds at +the final state. The final-state facts owed at T3 (`SysL`, `BXI` at some `Ac'`, `UserOK`, J7) are the arm hypothesis +`fin`. -/ +theorem JStage3A.ptUnmapXA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {ar : machine.Arch A} {nA : U64} {s : S} {a : A} + {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (K : ArchKeepsA ar AR) + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) + (hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM) (hTO : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s q) + (hEO : ExtTabOffA (L := L) Z m s) + (hK : Aligned (L.ksb s)) (hT : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s)) (hIpa : S2IpaE ar a) + (length buf : U64) (ex : Aeneas.Std.Array obj.Slot 3#usize) + (cte : Fl4SpecA64.CslotPtr) (hab : AbsSlotA Z.th cte) (ptr asid va : U64) + (hpS : ptr ∈ Z.S1 ∧ 1 ≤ Z.Lv ptr) + (hcap : capA st s (cslotAddrA cte) = .PageTable ptr true asid va) + (hf : cdt.is_final_capability st s (cslotAddrA cte) = ok true) + (k : Fl4SpecA64.ExtRootKind) (hkR : Fl4SpecA64.asidRootKind σ (BitVec.ofNat 16 asid.val) = some k) + (hkR1 : k ≠ .stage1) + (fin : ∀ (x : Fl4SpecA64.Tcb) (s3 : S) (σ3 : Fl4SpecA64.State Unit) (a' : A), + σ.kheap σ.curThread = some (.tcb x) → + vspace.decode_page_table st ar s a fl4_abi.label.ARMPageTableUnmap length (cslotAddrA cte) + (.PageTable ptr true asid va) buf ex = ok (core.result.Result.Ok (), s3, a') → + (([] : List Fl4SpecA64.Data), σ3) ∈ (Fl4SpecA64.performExtVSpace (Z := Unit) (.tableUnmap k (.pageTableCap + (specPtr ptr) .normalPtT (some (BitVec.ofNat 16 asid.val, va.bv))) cte) (stUpdA σ σ.curThread x .restart)).1 → + ∃ Ac' : Set U64, SysL st H Z.Cs OS ia ie s3 ∧ SymL st H Z.Cs s3 ∧ + BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac' s3 ∧ + Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s3 ∧ + J7A st (J7R2A st ar nA) Z H OS s3 σ3 m) : + ∃ (x : Fl4SpecA64.Tcb) (s3 : S) (a' : A) (σ3 : Fl4SpecA64.State Unit) (Ac' : Set U64), + σ.kheap σ.curThread = some (.tcb x) ∧ + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + vspace.decode_page_table st ar s a fl4_abi.label.ARMPageTableUnmap length (cslotAddrA cte) + (.PageTable ptr true asid va) buf ex = ok (core.result.Result.Ok (), s3, a') ∧ + (([] : List Fl4SpecA64.Data), σ3) ∈ (Fl4SpecA64.performExtVSpace (Z := Unit) (.tableUnmap k (.pageTableCap + (specPtr ptr) .normalPtT (some (BitVec.ofNat 16 asid.val, va.bv))) cte) (stUpdA σ σ.curThread x .restart)).1 ∧ + JStage3A st AR Z H OS U Ac' Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s3 a' σ3 m ∧ + Sched.BodyKeepA (stUpdA σ σ.curThread x .restart) σ3 := by + have hx : cslotAddrA cte ∈ OS := RS.sub _ (hab.isSlot J.j.1.place) + have hO : OSOK H.tcbs H.omega H.frame OS := J.sys.ent.os + have E0 := J.toJEntA + have hp4 : ptr.val % 4096 = 0 := J.j.1.vs.al_mod hpS.1 + obtain ⟨x, hk, heq, hset, J1, -, hOS1, -⟩ := J.of_restartCurA RS.pl.choose_spec.choose_spec.choose_spec.choose_spec + RS.sym RS.run hnvm RS.cur RS.vs RS.vc + have hks : L.ksb (L.wr s (stA (L.curW s)) sched.TS_RESTART) = L.ksb s := L.ksb_wr _ _ _ + have hcpu : L.cpu (L.wr s (stA (L.curW s)) sched.TS_RESTART) = L.cpu s := L.cpu_wr _ _ _ + generalize L.wr s (stA (L.curW s)) sched.TS_RESTART = s1 at heq J1 hOS1 hks hcpu + have c1 : capA st s1 (cslotAddrA cte) = .PageTable ptr true asid va := (hOS1.capA hx).trans hcap + obtain ⟨c0, -, hrel0⟩ := StateRelA.getAbsA J1.j.1.st hab + have c1' : capAtA (L.mem s1) (cslotAddrA cte) = .PageTable ptr true asid va := c1 + rw [c1'] at hrel0 + obtain ⟨h16a, -⟩ := hrel0 + have h16 : asid.val < 65536 := lt_of_lt_of_eq h16a (by norm_num) + have hEnc0 : CapRt.EncWf (capAtA (L.mem s1) (cslotAddrA cte)) := CapRt.decodeF_encWf _ _ + rw [c1'] at hEnc0 + simp only [CapRt.EncWf] at hEnc0 + have hEnc : CapRt.EncWf (.PageTable ptr false 0#u64 0#u64) := by + simp only [CapRt.EncWf]; exact ⟨hEnc0.1, by simp, Or.inl fun j hj _ => by simp⟩ + have hca : CapRelA (.PageTable ptr false 0#u64 0#u64) + (.archObjectCap (.pageTableCap (specPtr ptr) .normalPtT none)) := + ⟨show (0#u64 : U64).val < 2 ^ 16 by simp, rfl⟩ + have hok : CapObjOkA Z (.PageTable ptr false 0#u64 0#u64) := + ⟨fun _ h => (by cases h), fun _ _ _ _ _ h => (by cases h), fun _ _ _ h => (by cases h), + fun _ _ _ h => (by cases h), trivial⟩ + have hTr : TreeA st OS Z.G.chains Z.G.par s1 := + J1.j.1.cdt.tree.grow J1.j5.2.1 (fun x hx hn => J1.j5.2.2 x hx hn) + have hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s1 y = .Null := J1.j5.2.2 + have hTr0 : TreeA st OS Z.G.chains Z.G.par s := + J.j.1.cdt.tree.grow J.j5.2.1 (fun x hx hn => J.j5.2.2 x hx hn) + have hnul0 : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null := J.j5.2.2 + have E1 := J1.toJEntA + have hN := bxi_nodes E1.sys.ent.bxi + have hpOS := VsPgOffOSA.of_apA E1.ap + have hirq : CapOkA (stUpdA σ σ.curThread x .restart).interruptIrqNode cte + (.archObjectCap (.pageTableCap (specPtr ptr) .normalPtT none)) := by + refine ⟨fun irq hlt e => ?_, fun ⟨_, _, _, _, e⟩ => by cases e⟩ + have obj : ∃ o ac, (stUpdA σ σ.curThread x .restart).kheap cte.1 = some o ∧ + Fl4SpecA64.capOf o cte.2 = some ac ∧ CapRelA (capAtA (L.mem s1) (cslotAddrA cte)) ac := by + obtain ⟨o, idx⟩ := cte + obtain ⟨p, e1, hc | ⟨ht, i, hi, e2⟩⟩ := hab + · simp only at e1 hc; subst e1 + obtain ⟨cs, hk, -, hr⟩ := J1.j.1.st.cnode p _ hc + obtain ⟨ac, hac, hrel⟩ := hr idx rfl + exact ⟨_, ac, hk, hac, hrel⟩ + · simp only at e1 e2; subst e1 e2 + obtain ⟨y, hk, hr⟩ := J1.j.1.st.tcb p ht + obtain ⟨ac, hac, hrel⟩ := hr i hi + refine ⟨_, ac, hk, hac, ?_⟩ + unfold cslotAddrA; rw [ofBl_tcbIdx i (by unfold tcbSlotsA at hi; omega)]; exact hrel + obtain ⟨o, ac, hko, hco, hrel⟩ := obj + rw [e] at hko + rw [c1'] at hrel + rcases J1.sinv.irqN.slot irq hlt o _ ac hko hco with rfl | ⟨_, _, _, rfl⟩ + · cases hrel.2 + · cases hrel.2 + have hsci : NoIdleCapV m n (.archObjectCap (.pageTableCap (specPtr ptr) .normalPtT none)) := + ⟨fun _ _ e => (by cases e), fun _ e => (by cases e), fun _ _ _ e => (by cases e)⟩ + have main := J1.of_capVsFrameA (hpt := fun _ _ _ h => by cases h) + (Q := fun s' a' σ' hw => vspace.decode_page_table st ar s a fl4_abi.label.ARMPageTableUnmap length + (cslotAddrA cte) (.PageTable ptr true asid va) buf ex = ok (core.result.Result.Ok (), s', a') ∧ + (([] : List Fl4SpecA64.Data), σ') ∈ (Fl4SpecA64.performExtVSpace (Z := Unit) (.tableUnmap k (.pageTableCap + (specPtr ptr) .normalPtT (some (BitVec.ofNat 16 asid.val, va.bv))) cte) (stUpdA σ σ.curThread x .restart)).1 ∧ hw = Z.hw) + RS.sub (footNOff_ksA rfl rfl (footN_offA E1)) (footNAl_ksA rfl rfl (FootNA.aligned E1.j.1 hN E1.scAl E1.ap.vcAl)) + cte hab hEnc hca hTr hnul (fun h => by cases h) (by rw [c1]; exact fun h => by cases h) + (c := .PageTable ptr false 0#u64 0#u64) (by rw [c1]; exact .pt ..) hok hirq hsci + (fun s2 σ2 hs2 hσ2 HSs w jx hcx => by + have hkR' : Fl4SpecA64.asidRootKind (stUpdA σ σ.curThread x .restart) (BitVec.ofNat 16 asid.val) = some k := by + rw [asidRootKind_stUpdA hk]; exact hkR + obtain ⟨σu, fk, hfk, hperfE, hU, QU, IU⟩ := perform_tabUnmapXB (ar := ar) (a := a) E1.j.1.vs E1.j.1.ext + (by rw [hks]; exact hK) (by rw [hks]; exact hT) E1.j.2.2.1.obj hIpa h16 va ptr k hkR' hkR1 cte + obtain ⟨s2u, fa, hfl, hu, J2, hk2, hks2, hcpu2, hcase⟩ := unmap_post2JA hfk E1.j + (FootA.aligned E1.j.1 hN E1.scAl E1.ap.vcAl) (fun q hq => (hTO q hq).of_ksA hks hcpu) + (hEO.of_ksA hks hcpu) E1.sys.ent.os hpOS hU + have hks2' : L.ksb s2u = L.ksb s := hks2.trans hks + have hcpu2' : L.cpu s2u = L.cpu s := hcpu2.trans hcpu + have hN2 : ∀ c ∈ Z.Cs, ∀ o : U64, o.val < 4288 → o.val % 8 = 0 → Aligned (L.nodeAt s2u c o) := + fun c hc o h1 h2 => by rw [nodeAt_ksb hks2]; exact hN c hc o h1 h2 + obtain ⟨t', hty', hinv', hrun0, hz0, J5, hk3, hks3, hcpu3⟩ := zero_post2A J2 + (FootA.aligned J2.1 hN2 E1.scAl E1.ap.vcAl) (fun q hq => (hTO q hq).of_ksA hks2' hcpu2') + (hEO.of_ksA hks2' hcpu2') (by rw [hks2']; exact hK) (by rw [hks2']; exact hT) hO hpOS (Or.inl hpS) + have UQz : UnQ σu (ptSetA σu (specPtr ptr) t') := by + have hm0 := OutQ.mapMx UnQ.refl trQ (fun sl => storePte_unQ .normalPtT sl) + (Fl4SpecA64.uptoEnumStep (specPtr ptr) (specPtr ptr + ((1 : Fl4SpecA64.Word) <<< Fl4SpecA64.pteBits)) + (specPtr ptr + Fl4SpecA64.mask (Fl4SpecA64.ptBits .normalPtT))) σu ((), ptSetA σu (specPtr ptr) t') + rw [hrun0] at hm0 + exact hm0 (Set.mem_singleton _) + have Q5 : QvA (stUpdA σ σ.curThread x .restart) (ptSetA σu (specPtr ptr) t') := QU.trans UQz.1 + have I5 : InvXA (stUpdA σ σ.curThread x .restart) (ptSetA σu (specPtr ptr) t') := IU.trans UQz.2.toX + obtain ⟨s5, σ6, hsc, -, hσ6, HSu, J6, T6⟩ := setcap_core2A E0 RS J5 + ((hOS1.trans' hk2).trans' hk3) (hks3.trans hks2') (hcpu3.trans hcpu2') cte hab hcap hEnc hca + (UA.of_shapeA rfl fun h => by cases h) hok (fun h => by cases h) (fun h => by cases h) hTr0 hnul0 + have hperf : (([] : List Fl4SpecA64.Data), σ6) ∈ (Fl4SpecA64.performExtVSpace (Z := Unit) (.tableUnmap k + (.pageTableCap (specPtr ptr) .normalPtT (some (BitVec.ofNat 16 asid.val, va.bv))) cte) + (stUpdA σ σ.curThread x .restart)).1 := by + rw [hperfE, nd_bind_oneD hrun0] + exact ⟨((), σ6), hσ6, rfl⟩ + obtain ⟨a', hfa', hAR⟩ := spec_ex_fd (hfl.arA K J6.1.ar) + have J6' := J6.of_arA hAR + have hdec' : vspace.decode_page_table st ar s a fl4_abi.label.ARMPageTableUnmap length (cslotAddrA cte) + (.PageTable ptr true asid va) buf ex = ok (core.result.Result.Ok (), s5, a') := by + rw [pt_unmap_eqA ar s a length (cslotAddrA cte) ptr asid va buf ex hf, heq, bind_ok] + refine (then_setcapA (then_zeroA hu ptr) hsc).trans ?_ + rw [hfa', bind_ok] + obtain ⟨Ac', hsl, hsy, hbx, hus, hj7⟩ := fin x s5 σ6 a' hk hdec' hperf + have al_x : Aligned (cslotAddrA cte) := hO.al0 hx + have al_x8 : Aligned (wa (cslotAddrA cte) 8#u64) := hO.al hx (k := 8#u64) (by simp) (by simp) + obtain ⟨w0, w1, hen, -⟩ := CapRt.decode_encode _ hEnc + have hpl : ptr.val < 2 ^ 64 := ptr.bv.isLt + have hfit : ptr.val + (4096#u64 : U64).val ≤ 2 ^ 64 := by + show ptr.val + 4096 ≤ 2 ^ 64; simp only [Nat.reducePow] at hpl ⊢; omega + have wU : WritesIn st s1 s2u (fun b => ∃ q j : U64, XqA Z q ∧ j.val < 512 ∧ b = peAF q j) := by + rcases hcase with rfl | ⟨q, j, hq, hj, hal, rfl⟩ + · exact WritesIn.refl _ _ + · exact (writesIn_wr hal).mono fun b hb => ⟨q, j, hq, hj, hb⟩ + have wZ : WritesIn st s2u (L.zr s2u ptr 4096#u64) (fun b => ∃ q j : U64, XqA Z q ∧ j.val < 512 ∧ b = peAF q j) := + WritesIn.monoA (writesIn_zrV (st := st) (s := s2u) (show ptr.val % 8 = 0 by omega) (by simp) hfit) fun b hb h => by + obtain ⟨i, hi, e⟩ := peAF_ofRangeA hp4 hb h.1 h.2 + exact ⟨ptr, i, Or.inr (Or.inr hpS.1), hi, e⟩ + have wV := wU.trans1 wZ + have wS := writesIn_setCapA al_x al_x8 hsc + have w2 := WritesIn.setCapCommA al_x al_x8 hen hs2 hsc wV + have hk3' : L.ksb s5 = L.ksb s1 := wS.1.trans (hks3.trans hks2) + have hcpu5 : L.cpu s5 = L.cpu s1 := wS.2.1.trans (hcpu3.trans hcpu2) + obtain ⟨Pt, Ub, vr, -, -, -, -, -, -, link, -, -⟩ := J1.j7 + obtain ⟨Qb, Rb, q⟩ := J1.sys.ent.bxi.1 + have vo : VrOK H.omega H.frame H.scs H.scBits OS Z.V ∅ (L.ksb s1) (utVsPtA st Pp Tb Lv s1) := J1.vsOk + have Psp : ∀ b, (∃ q j : U64, XqA Z q ∧ j.val < 512 ∧ b = peAF q j) → VsR (L.ksb s1) Pt b := + fun b ⟨q, j, hq, hj, e⟩ => by rw [e]; exact vsW_pageA link hq.sched hj + have hzG : ∀ Pt' : Set U64, (∀ b, VsR (L.ksb s1) Pt' b → Aligned b) → + (∀ b, VsR (L.ksb s1) Pt' b → ¬ InOS OS b) → VsZ st (L.ksb s1) Pt' s1 s5 := fun Pt' hal hos b hb => by + have hb1 := hal b hb + have hns : ¬ SlotWordA (cslotAddrA cte) b := fun h => hos b hb (slotWord_inOS hO hx h) + rw [wS.mem_eq hb1 hns] + rcases zr_caseA (st := st) (s := s2u) hp4 hb1 with e | e + · exact Or.inr e + · rw [e] + rcases hcase with rfl | ⟨q, j, hq, hj, hal', rfl⟩ + · exact Or.inl rfl + · by_cases e : peAF q j = b + · subst e; exact Or.inr (L.mem_wr_same hal') + · exact Or.inl (L.mem_wr_ne hal' hb1 (fun h => e (UScalar.eq_of_val_eq h))) + have hAsK : ∀ b, AsW (L.ksb s1) b → L.mem s5 b = L.mem s1 b := fun b hb => by + have hnP : ¬ ∃ q j : U64, XqA Z q ∧ j.val < 512 ∧ b = peAF q j := fun ⟨q, j, hq, hj, e⟩ => + hq.offAs hT hj (by rw [← e, ← hks]; exact hb) + have hns : ¬ SlotWordA (cslotAddrA cte) b := fun h => vo.os b (Or.inr hb) (slotWord_inOS hO hx h) + exact (wS.mem_eq (vo.al b (Or.inr hb)) hns).trans (wV.mem_eq (vo.al b (Or.inr hb)) hnP) + have vsT := VsTyR.zeroStepA hk3' hAsK (hzG _ vo.al vo.os) J1.sys.ent.vs + have osk3 : OSKeep st OS s2 s5 := + osKeep_ofA hO fun w' hw' hin => w2.mem_eq hw' (fun hp => vr.os w' (Psp w' hp) hin) + obtain ⟨hcx', hco⟩ := capA_setCapA hO hx hEnc hs2 + have capE3 : ∀ u ∈ OS, capA st s5 u = capA st s2 u := fun u hu => osk3.capA hu + have hnew : capA st s5 (cslotAddrA cte) = .PageTable ptr false 0#u64 0#u64 := by rw [capE3 _ hx, hcx] + have hkeep : ∀ u ∈ OS, u ≠ cslotAddrA cte → capA st s5 u = capA st s1 u := fun u hu e => by + rw [capE3 u hu, hco u hu e] + have hMs2 : ∀ u ∈ OS, MapAlikeA (capA st s2 u) (capA st s1 u) := fun u hu => by + by_cases e : u = cslotAddrA cte + · subst e; rw [hcx, c1]; exact .pt .. + · rw [hco u hu e]; exact .refl _ + have sbS : ∀ t ∈ Z.T, sbWordA t ∈ OS := fun t ht => + RS.sub _ ⟨t, Or.inr ⟨(J1.j.1.thT t).2 ht, 2, by simp [tcbSlotsA], (bufSlot_eqA t).symm⟩⟩ + have wA : WritesIn st s1 s2 (fun b => Aligned b ∧ W7.CapW H.tcbs {b | InOS OS b} ∅ b) := writesIn_alAG w + have hB : ∀ b, (Aligned b ∧ W7.CapW H.tcbs {b | InOS OS b} ∅ b) → W7.CapW H.tcbs H.omega H.frame b := + fun b hb => os_capw hO hb.1 hb.2 + obtain ⟨-, -, -, -, -, fld, -, -⟩ := W7.blk_of_capw (BXI.blk J1.sys.ent.bxi) wA hB + have ib : ∀ t ∈ Z.T, L.mem s2 (ibWordA t) = L.mem s1 (ibWordA t) := fun t ht => + fld t (by rw [J1.tcbs]; exact ht) 3512#u64 (by simp) (by simp) (by simp) + have HS2 : VsSpecA σ2 σ6 := (VsSpecA.of_invX Q5 I5).setCapCommA hσ2 hσ6 + have offT : ∀ b, (∃ q j : U64, XqA Z q ∧ j.val < 512 ∧ b = peAF q j) → ∀ t ∈ Z.T, + b.val < t.val ∨ t.val + 4096 ≤ b.val := fun b hb t ht => by + have hth : t ∈ H.tcbs := by rw [J1.tcbs]; exact ht + have := (q.omega b (vr.om b (Psp b hb))).2.1 t hth + omega + have tw : ∀ t ∈ Z.T, ∀ K : U64, K.val + 8 ≤ 4096 → K.val % 8 = 0 → + L.mem s5 (wa t K) = L.mem s2 (wa t K) := by + intro t ht K hK' h8 + have hta := J1.sr.core.tcbs t ht + have hv : (wa t K).val = t.val + K.val := field_val hta (K := K) (by omega) + refine w2.mem_eq (field_aligned hta (by omega) h8) (fun h => ?_) + have := offT _ h t ht + omega + have hib : ∀ t ∈ Z.T, IbSame (L := L) s2 s5 t := fun t ht => by + refine ⟨tw t ht _ (by simp) (by simp), ?_, ?_⟩ + · rw [sbWordA_eq]; exact tw t ht _ (by simp) (by simp) + · rw [sbWordA8_eq]; exact tw t ht _ (by simp) (by simp) + have hj7' := hj7 + obtain ⟨Pt', Ub', vr', -, ac', -⟩ := hj7' + have hBw : ∀ b, ((∃ q j : U64, XqA Z q ∧ j.val < 512 ∧ b = peAF q j) ∨ SlotWordA (cslotAddrA cte) b) → + L.Free s1 H.omega Z.Cs b ∨ W7.TLow H.tcbs b := fun b hb => by + rcases hb with hp | hsl + · exact Or.inl (Or.inl (vr.om b (Psp b hp))) + · have hal : Aligned b := by rcases hsl with rfl | rfl; exact al_x; exact al_x8 + exact (os_capw hO hal (Or.inr ⟨slotWord_inOS hO hx hsl, Set.notMem_empty _⟩)).elim Or.inr + fun h' => Or.inl (Or.inl h'.1) + have hsp : ∀ q', SchedPageA st Z OS s5 q' → SchedPageA st Z OS s1 q' := fun q' h => + SchedPageA.backPtA hnew hkeep hpS.1 h + -- the new cap's table + have hz5 : ∀ i : U64, i.val < 512 → L.mem s5 (peAF ptr i) = 0#u64 := fun i hi => by + have hv : VsR (L.ksb s1) Pt (peAF ptr i) := vsW_pageA link (Or.inr (Or.inl hpS.1)) hi + rw [wS.mem_eq (vr.al _ hv) (fun h => vr.os _ hv (slotWord_inOS hO hx h))] + exact hz0 i hi + have htab : UnmTabA st Z m s5 σ6 .normalPtT ptr := by + refine ⟨fun i hi => by rw [hz5 i hi]; unfold dBitA; rfl, ⟨t', ?_, hty', hinv'⟩, + (hTO ptr hpS.1).of_ksA (hk3'.trans hks) (hcpu5.trans hcpu)⟩ + have h1 : (ptSetA σu (specPtr ptr) t').kheap (specPtr ptr) = some (.archObj (.pageTable t')) := by + rw [ptSetA_kheap, if_pos rfl] + exact HSu.keep h1 (fun _ _ e => by cases e) (fun _ e => by cases e) + refine ⟨s5, a', σ6, Z.hw, fun b => ∃ q j : U64, XqA Z q ∧ j.val < 512 ∧ b = peAF q j, Ac', + ⟨w2, fun Pt'' Ub'' vr'' link'' b ⟨q, j, hq, hj, e⟩ => by rw [e]; exact vsW_pageA link'' hq.sched hj, + fun b ⟨q, j, hq, hj, e⟩ hbuf => J1.bufOff.page b hbuf q hq.sched + (core.num.U64.wrapping_mul j 8#u64) (by rw [wm8_valA hj]; omega) (by rw [wm8_valA hj]; omega) + (by rw [e]; rfl), + HS2, ?_, ?_, J6', ⟨⟨J1.sys.ent.heap, hbx, hO, hus, vsT.1, by rw [hk3']; exact J1.sys.ent.ksw⟩, hsl⟩, + UtVsOkA.zeroStepA hk3' vsT.2 J1.vsOk, bufVs_subA J1.bufVs hk3' vsT.2, + W7.inv_of_capw J1.sq (wV.trans wS) hBw, ac'.case.po, + ?_, ?_, ?_, ?_, hj7, hsy⟩, hdec', hperf, rfl⟩ + · rw [setCap_archA hσ6, setCap_archA hσ2]; exact Q5.asidTable + · intro p dv pg h + have h1 : (stUpdA σ σ.curThread x .restart).kheap p = some (.archObj (.dataPage dv pg)) := + HSs.back h (fun _ _ e => by cases e) (fun _ e => by cases e) + have h2 : (ptSetA σu (specPtr ptr) t').kheap p = some (.archObj (.dataPage dv pg)) := by + rcases I5 p with e | ⟨o, o', a0, b0, hn⟩ + · exact e.trans h1 + · rw [h1] at a0 + have h3 : o = .dataPage dv pg := by + have := Option.some.inj a0 + injection this with h4 + exact h4.symm + subst h3 + exact absurd hn (by simp [ArchInvA]) + exact HSu.keep h2 (fun _ _ e => by cases e) (fun _ e => by cases e) + · exact BufCover.vsA (BufCover.mapA J1.bufCover J1.bufNative J1.j.1.st jx.1.st (fun t ht => (J1.j.1.thT t).2 ht) + HSs ib (fun t ht => hMs2 _ (sbS t ht))) HS2 hib + · exact BufNativeA.mapA J1.bufNative (fun t ht q' sz' rr dv' m' e' a'' v' h => by + by_cases e1 : sbWordA t = cslotAddrA cte + · rw [e1, hnew] at h; cases h + · rw [hkeep _ (sbS t ht) e1] at h + have hn := J1.bufNative t ht + rw [bufSlot_eqA, capAtA_eq (st := st), h] at hn + exact hn) + · exact UnmPtSpecA.ofPtUnmMA (Pt := Pt) hnew hkeep + (fun q' hq i hi => hzG Pt vr.al vr.os _ (Or.inl ⟨q', hq, core.num.U64.wrapping_mul i 8#u64, + by rw [wm8_valA hi]; omega, by rw [wm8_valA hi]; omega, rfl⟩)) + link hk3' hcpu5 I5 (fun q' ao h => HSu.keep h (fun _ _ e => by cases e) (fun _ e => by cases e)) + htab J1.unmPt + · exact ⟨J1.bufOff.os, fun b hb => by rw [hk3']; exact J1.bufOff.vs b hb, + fun b hb q' hq => J1.bufOff.page b hb q' (hsp q' hq), J1.bufOff.vcw, + fun bp hbp => (J1.bufOff.pin bp hbp).keepM fun u hu => by rw [capE3 u hu]; exact hMs2 u hu⟩) + obtain ⟨s2, σ2, s3, a3, σ3, hw, P, Ac', hs2, hσk, HSsk, HSvk, HSc, wT, Jf, hdec, hperf, hhw⟩ := main + subst hhw + exact ⟨x, s3, a3, σ3, Ac', hk, hset, hdec, hperf, Jf, + (Sched.BodyKeepA.ofSlot HSsk (setCap_keepsA hσk).2.2).trans (Sched.BodyKeepA.ofVs HSvk)⟩ + + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3BA.lean b/proofs/Fl4ProofsA64/P3/Stage3BA.lean new file mode 100644 index 000000000..9517319c0 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3BA.lean @@ -0,0 +1,353 @@ +/- +P3 (AArch64, A64-P3 sigwake2, Lead's ruling): **`JStage3TA` without `noSwitch` / `sact`** (`JStage3BA`), the record of a +step that runs after a waking signal (a switch may be pending). `JStage3TA` is unchanged; `toBA` / `toTA` convert. +Generated by `handoff/tools/gen_stage3ba.py` from `P3/Stage3TA`. +No named premise beyond `JStage3TA`'s [J] fields (the same). New module. +-/ +import Fl4ProofsA64.P3.Stage3TA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched + +noncomputable section + +section +variable {S : Type} (st : machine.Store S) [L : LawfulStore st] {A : Type} + +/-- **The per-step record without a pending-switch clause** (module docstring). -/ +structure JStage3BA (AR : A → HwRootA → Prop) (Z : XSetsA) (H : Heap) (OS U Ac Pp Tb : Set U64) (Lv : U64 → Nat) + (ia ie : Nat) (N E Rp : Set U64) (Bf : Set (U64 × Fl4SpecA64.Word)) (stack : U64 → List U64) (κ : U64 → U64) + (n : Nat) (J5 J7 : JSlotA S) (s : S) (a : A) (σ : Fl4SpecA64.State Unit) (m : Fl4SpecA64.McState) : Prop + extends JEntTA st AR Z H OS U Ac Pp Tb Lv ia ie s a σ m where + sr : SchedSRA st Z m κ s σ + scCov : ScCovA Z.C m σ + /-- (Architect ruling, 2026-12-03) [J] Every related spec SC object lies inside its heap object (kept by `SSzA`; + Retype preservation is the Untyped line's); (record edit #8b-D, Architect 11:22Z) a non-idle one fills it exactly + (idle SCs are 256 B with size exponent 0, so not unconditional). -/ + scSize : ∀ c ∈ Z.C, ∀ x n, σ.kheap (scNameA m c) = some (.schedContext x n) → + n + Fl4SpecA64.minSchedContextBits ≤ H.scBits c ∧ + (Sched.NotIdleScA m (scNameA m c) → n + Fl4SpecA64.minSchedContextBits = H.scBits c) + ep : EpStateRel Z.T E Rp Bf (L.mem s) σ + ntfn : NtfnStateRel N Z.T (L.mem s) σ + reply : ReplyStackRelA (L.mem s) Rp Z.C σ (scNameA m) stack + bufCover : BufCover st Z.T Bf s σ + bufNative : BufNativeA Z.T (L.mem s) + iplaced : IPlacedA N Z.T E Rp Bf + ipcPlace : IpcSchedPlacementA Z.th N E Rp Bf + idleNoFault : IdleNoFaultA m σ + mcp : McpRelA Z.T m (L.mem s) σ + yieldRel : YieldRelA Z.T Z.C m (L.mem s) σ + sinv : SInvXA σ m n + setsInH : SetsInHA Z H + /-- (Architect 00:54Z) [J] The IPC ghosts' notifications, endpoints and replies are heap objects of those kinds. -/ + utIpc : UtIpcHeapA H N E Rp + ipcTh : IpcThA Z.th N E Rp + deadExt : DeadExtA st Z OS Rp stack s σ + specFree : SpecFreeA st OS s σ + utBlk : UtBlk39A st OS s + unmPt : UnmPtSpecA st Z m OS s σ + tabS1 : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s q + tabExt : ExtTabOffA (L := L) Z m s + specZero : SpecZeroFreeA st OS s σ + /-- (Architect 03:22Z) [J] **`OrigLiveA`**: every original cap's slot is a slot of `th` (x86 `JKern.orig`). -/ + origLive : ∀ sl, σ.isOriginalCap sl = true → AbsSlotA Z.th sl + /-- (Architect 03:29Z) [J] **`nullOrig`** (l4v `valid_nullcaps`): a null slot is not original. -/ + nullOrig : ∀ sl, specCapAtA σ sl = some .nullCap → σ.isOriginalCap sl = false + /-- (Architect 00:06Z, closure D) [J] every `other` object of `th` is nonempty. -/ + thPos : ∀ p z, Z.th p = some (.other z) → 0 < z + /-- [J] (D1) No `OS` frame cap shares its base with a range key cap (`P3/CnodeFinalA`). -/ + frBase : FrBaseApartA st OS s + /-- [J] (D1) A device frame cap names no non-data-page object (`P3/DevFrDisjA`). -/ + devFr : DevFrDisjA st H OS s σ + /-- [J] (D1) Every spec CNode and TCB is in the typed heap (`P3/CnodeFinalA`). -/ + finCov : FinCovA Z.th σ + /-- [J] (D1, J13) Every core's idle objects lie in the idle area. -/ + idleAll : IdleAllA st ia ie Z.Cs m s + /-- [J] (D1, N1) The converse of `utIpc`. -/ + ipcHeapCov : IpcHeapCovA H N E Rp + /-- [J] (D1, N2) A TCB under a TCB zombie has no VCPU. -/ + zombieNoVcpu : ZombieTcbNoVcpuA st OS s + /-- [J] (D1) An `OS` TCB zombie's base is a heap TCB. -/ + zombieTcb : ZombieTcbTA st H OS s + /-- [J] (D1) The tracked buffer words are off the `OS` slots, VSpace words, table pages and VCPU words. -/ + bufOff : BufOffA st Z OS s Bf + /-- [J] (D1b) The installed pools' and reachable tables' words are placed (`P3/Stage3D1bA`). -/ + vsOk : UtVsOkA st H OS Z.V Pp Tb Lv s + /-- [J] (D1b) Every TCB's IPC buffer is aligned to 1024. -/ + bufAl : UtBufAlA Z.T σ + /-- [J] (D1b) A `Zombie`'s count fits its object. -/ + zombieBnd : UtZombieA st OS s + /-- [J] (D1b-4) Device untyped regions are apart from the record (`P3/Stage3FieldsA`). -/ + devUtOff : DevUtOffA st Z H OS ia ie s + /-- [J] (F3–F6, Architect 15:06Z) The idle objects of all cores (`SIdleA`). -/ + sidle : SIdleA σ m n + /-- [J] (Architect 17:31Z) The `OS` caps' heap kinds (`CapTyA`). -/ + capTy : CapTyA st H OS s + /-- [J] (record edit #3, Architect 19:30Z) Every related SC's core (`κ`, its `scCoreA` word) is a kernel core. -/ + scCpu : ∀ c ∈ Z.C, (κ c).val < Fl4SpecA64.fl4A64Platform.numNodes + /-- [J] (record edit #3) No related SC is at address `0`. -/ + scNZ : ∀ c ∈ Z.C, c ≠ 0#u64 + /-- [J] (record edit #3) The VCPU binding words (`VcRunA`). -/ + vcRun : VcRunA st Z.T Z.V s + /-- [J] (record edit #4, Architect 20:21Z) P1's `RunVcpuInv`. -/ + runVcpu : RunVcpuInv st Z.T s + /-- [J] (record edit #4) Every reply on an SC's stack is a blocked reply (weak `ValidReplies`). -/ + vr : ∀ sc ∈ Z.C, ∀ r ∈ stack sc, RBlk st s r + /-- [J] (record edit #5) Blocked ⇒ queued (`BlkQueuedA`). -/ + blkQ : BlkQueuedA Z.T E N (L.mem s) σ + /-- [J] (record edit #4) No `OS` SC cap names the current core's idle SC (the SC half of `SysL.ci`). -/ + ciSc : ∀ u ∈ OS, ∀ p b, capA st s u = .SchedContext p b → specPtr p ≠ m.idleScOf m.curCore + /-- [J] (record edit #7) `arm_current_vcpu` is empty. -/ + curVcpuNone : σ.archState.armCurrentVcpu = none + /-- [J] (record edit #8) Endpoint queues and notification wait queues are sorted by priority (`IpcSortedA`). -/ + ipcSorted : IpcSortedA (specPtr '' E) (specPtr '' N) σ + /-- [J] (record edit #8) An SC with replies is active (l4v `active_reply_scs`). -/ + activeReplyScs : ActRepA σ + /-- [J] (record edit #8) No tracked IPC-buffer word is a reachable VSpace word. -/ + bufVs : ∀ b, BufW Bf b → ¬ VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b + /-- [J] (record edit #8b-B1) The concrete domain-schedule words match the spec's (`NdOKA`). -/ + domOK : Sched.NdOKA (st := st) s σ + /-- [J] (record edit B2) This core's IPI pair is empty (`SilentScheduleIpiA`). -/ + ipiZero : Sched.SilentScheduleIpiA (st := st) s + j5 : J5 Z H OS s σ m + j7 : J7 Z H OS s σ m + +variable {st} {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- `JStage3TA` without `noSwitch` / `sact`. -/ +theorem JStage3TA.toBA (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) : + JStage3BA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m where + ent := J.ent + j := J.j + tcbs := J.tcbs + scs := J.scs + sq := J.sq + ap := J.ap + pp := J.pp + s1 := J.s1 + ko := J.ko + ptH := J.ptH + tabInj := J.tabInj + poolOff := J.poolOff + objAlloc := J.objAlloc + idleAnchor := J.idleAnchor + vsVc := J.vsVc + sr := J.sr + scCov := J.scCov + scSize := J.scSize + ep := J.ep + ntfn := J.ntfn + reply := J.reply + bufCover := J.bufCover + bufNative := J.bufNative + iplaced := J.iplaced + ipcPlace := J.ipcPlace + idleNoFault := J.idleNoFault + mcp := J.mcp + yieldRel := J.yieldRel + sinv := J.sinv + setsInH := J.setsInH + utIpc := J.utIpc + ipcTh := J.ipcTh + deadExt := J.deadExt + specFree := J.specFree + utBlk := J.utBlk + unmPt := J.unmPt + tabS1 := J.tabS1 + tabExt := J.tabExt + specZero := J.specZero + origLive := J.origLive + nullOrig := J.nullOrig + thPos := J.thPos + frBase := J.frBase + devFr := J.devFr + finCov := J.finCov + idleAll := J.idleAll + ipcHeapCov := J.ipcHeapCov + zombieNoVcpu := J.zombieNoVcpu + zombieTcb := J.zombieTcb + bufOff := J.bufOff + vsOk := J.vsOk + bufAl := J.bufAl + zombieBnd := J.zombieBnd + devUtOff := J.devUtOff + sidle := J.sidle + capTy := J.capTy + scCpu := J.scCpu + scNZ := J.scNZ + vcRun := J.vcRun + runVcpu := J.runVcpu + vr := J.vr + blkQ := J.blkQ + ciSc := J.ciSc + curVcpuNone := J.curVcpuNone + ipcSorted := J.ipcSorted + activeReplyScs := J.activeReplyScs + bufVs := J.bufVs + domOK := J.domOK + ipiZero := J.ipiZero + j5 := J.j5 + j7 := J.j7 + +/-- `JStage3BA` with `noSwitch` / `sact` back. -/ +theorem JStage3BA.toTA (J : JStage3BA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (noSwitch : ∀ u, σ.schedulerAction ≠ .switchThread u) (sact : SActA σ) : + JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m where + ent := J.ent + j := J.j + tcbs := J.tcbs + scs := J.scs + sq := J.sq + ap := J.ap + pp := J.pp + s1 := J.s1 + ko := J.ko + ptH := J.ptH + tabInj := J.tabInj + poolOff := J.poolOff + objAlloc := J.objAlloc + idleAnchor := J.idleAnchor + vsVc := J.vsVc + sr := J.sr + scCov := J.scCov + scSize := J.scSize + ep := J.ep + ntfn := J.ntfn + reply := J.reply + bufCover := J.bufCover + bufNative := J.bufNative + iplaced := J.iplaced + ipcPlace := J.ipcPlace + idleNoFault := J.idleNoFault + mcp := J.mcp + yieldRel := J.yieldRel + sinv := J.sinv + setsInH := J.setsInH + utIpc := J.utIpc + ipcTh := J.ipcTh + deadExt := J.deadExt + specFree := J.specFree + utBlk := J.utBlk + unmPt := J.unmPt + tabS1 := J.tabS1 + tabExt := J.tabExt + specZero := J.specZero + origLive := J.origLive + nullOrig := J.nullOrig + thPos := J.thPos + frBase := J.frBase + devFr := J.devFr + finCov := J.finCov + idleAll := J.idleAll + ipcHeapCov := J.ipcHeapCov + zombieNoVcpu := J.zombieNoVcpu + zombieTcb := J.zombieTcb + bufOff := J.bufOff + vsOk := J.vsOk + bufAl := J.bufAl + zombieBnd := J.zombieBnd + devUtOff := J.devUtOff + sidle := J.sidle + capTy := J.capTy + scCpu := J.scCpu + scNZ := J.scNZ + vcRun := J.vcRun + runVcpu := J.runVcpu + vr := J.vr + blkQ := J.blkQ + ciSc := J.ciSc + curVcpuNone := J.curVcpuNone + ipcSorted := J.ipcSorted + activeReplyScs := J.activeReplyScs + bufVs := J.bufVs + domOK := J.domOK + ipiZero := J.ipiZero + j5 := J.j5 + j7 := J.j7 + noSwitch := noSwitch + sact := sact + +/-- `JStage3A` as `JStage3BA` (no premise). -/ +theorem JStage3A.toBA (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) : + JStage3BA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m where + ent := J.sys.ent + j := J.j + tcbs := J.tcbs + scs := J.scs + sq := J.sq + ap := J.ap + pp := J.pp + s1 := J.s1 + ko := J.ko + ptH := J.ptH + tabInj := J.tabInj + poolOff := J.poolOff + objAlloc := J.objAlloc + idleAnchor := J.idleAnchor + vsVc := J.vsVc + sr := J.sr + scCov := J.scCov + scSize := J.scSize + ep := J.ep + ntfn := J.ntfn + reply := J.reply + bufCover := J.bufCover + bufNative := J.bufNative + iplaced := J.iplaced + ipcPlace := J.ipcPlace + idleNoFault := J.idleNoFault + mcp := J.mcp + yieldRel := J.yieldRel + sinv := J.sinv + setsInH := J.setsInH + utIpc := J.utIpc + ipcTh := J.ipcTh + deadExt := J.deadExt + specFree := J.specFree + utBlk := J.utBlk + unmPt := J.unmPt + tabS1 := J.tabS1 + tabExt := J.tabExt + specZero := J.specZero + origLive := J.origLive + nullOrig := J.nullOrig + thPos := J.thPos + frBase := J.frBase + devFr := J.devFr + finCov := J.finCov + idleAll := J.idleAll + ipcHeapCov := J.ipcHeapCov + zombieNoVcpu := J.zombieNoVcpu + zombieTcb := J.zombieTcb + bufOff := J.bufOff + vsOk := J.vsOk + bufAl := J.bufAl + zombieBnd := J.zombieBnd + devUtOff := J.devUtOff + sidle := J.sidle + capTy := J.capTy + scCpu := J.scCpu + scNZ := J.scNZ + vcRun := J.vcRun + runVcpu := J.runVcpu + vr := stackBlkA J + blkQ := J.blkQ + ciSc := ciScA J + curVcpuNone := J.curVcpuNone + ipcSorted := J.ipcSorted + activeReplyScs := J.activeReplyScs + bufVs := J.bufVs + domOK := J.domOK + ipiZero := J.ipiZero + j5 := J.j5 + j7 := J.j7 + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3BodyCurA.lean b/proofs/Fl4ProofsA64/P3/Stage3BodyCurA.lean new file mode 100644 index 000000000..4a468793b --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3BodyCurA.lean @@ -0,0 +1,399 @@ +/- +P3 (aarch64, A64-P3 Stage 3, dispatch restore): **`BodyEndA` from the current thread's SC binding**. + +`BodyOKA.owner` asks that the thread bound to the current SC be outside the release queue while time is consumed. The +record at the body's end state gives it once the current thread is still bound to the current SC (`CurBA`): the +binding's back link (`ScInvA.back`) names the current thread as the SC's thread, and `ZeroConsA` keeps that thread out +of the release queue while time is consumed. So an arm owes the scalars, the current SC's refills, `SwA`, the current +thread's state on a resume and `CurBA`, and no release-queue fact. + +* `BodyEndA.ofCurBA`: `BodyEndA` from those facts, the record's `ScInvA` and `ZeroConsA` at the end state. +* `CurBN d`: `CurBA` with the current thread other than `d`. It survives `TPres` steps, steps keeping the heap and the + two current pointers, a TCB write to `d`, `set_thread_state d`, `postpone`, `sched_context_resume`, + `maybe_donate_sc d` (it donates only to a thread without an SC, from an SC without a thread), the bound receiver's + `cancel_ipc`, and the wake tail. +* **`sendSignal_curBA`**: `send_signal` keeps `CurBA` when the current thread is runnable (the woken thread is + blocked, so it is another one). +* **`BodyEndA.ofSignalA`** (the woken-receiver shape of the Notification send arms): the caller's `Restart` and one + `send_signal` end in `BodyEndA`, given the record's `ScInvA` / `ZeroConsA` at the end state (`BKeepA` from + `sendSignal_bkA`, `SwA` from `sendSignal_swa`, the caller's state from `sendSignal_ctStMA`). + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3TcbBodyA +import Fl4ProofsA64.P3.Stage3SigSwA +import Fl4ProofsA64.P3.Stage3SchedExitFrA +import Fl4ProofsA64.P3.Stage3SigBudgetA +import Fl4ProofsA64.P3.Stage3KEntryBudgetA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Classical + +noncomputable section + +/-- The current thread is bound to the current SC. -/ +def CurBA (σ : State Unit) : Prop := + ∃ x, σ.kheap σ.curThread = some (.tcb x) ∧ x.tcbSchedContext = some σ.curSc + +section +variable {S A : Type} {st : machine.Store S} [L : LawfulStore st] +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s : S} {a : A} {σ σ' : State Unit} {m : McState} + +/-- **`BodyEndA` from the current thread's binding** (module docstring). -/ +theorem BodyEndA.ofCurBA (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (hinv : ScInvA m σ') (hzc : ZeroConsA σ') (hsw : SwA σ') (htime : σ'.curTime = σ.curTime) + (hlast : σ'.machineState.lastMachineTime = σ.machineState.lastMachineTime) + (hcur : σ'.curThread = σ.curThread) (hcsc : σ'.curSc = σ.curSc) (hcon : σ'.consumedTime = σ.consumedTime) + (hobj : ∀ y' k, σ'.kheap σ.curSc = some (.schedContext y' k) → ∃ y, σ.kheap σ.curSc = some (.schedContext y k) ∧ + y'.scRefills = y.scRefills ∧ y'.scRefillMax = y.scRefillMax) + (hb : CurBA σ') + (hrun : σ'.schedulerAction = .resumeCurThread → ∀ x, σ'.kheap σ.curThread = some (.tcb x) → + x.tcbState = .running ∨ x.tcbState = .restart) : BodyEndA σ σ' m := by + have hd : ∀ y' y : SchedContext, y'.scRefills = y.scRefills → refillHd y' = refillHd y := fun y' y e => by + unfold refillHd; rw [e] + have ha : ∀ y' y : SchedContext, y'.scRefillMax = y.scRefillMax → scActive y' = scActive y := fun y' y e => by + unfold scActive; rw [e] + refine ⟨⟨⟨?_, fun y' k hk _ _ => ?_, fun h x hx => ?_⟩, hsw, fun h => ?_, fun y' k hk _ _ hc0 o ho => ?_⟩, hcur⟩ + · have := J.validTime + unfold ValidMachineTimeA at this ⊢ + rw [htime, hlast]; exact this + · rw [hcsc] at hk + obtain ⟨y, hy, e1, e2⟩ := hobj y' k hk + constructor + · intro z j hz + rw [hcsc, hk] at hz + obtain ⟨rfl, -⟩ := KernelObject.schedContext.inj (Option.some.inj hz) + rw [hd _ _ e1, hcon, htime] + exact J.offReady y k hy + · intro hpos z j hz hactive + rw [hcsc, hk] at hz + obtain ⟨rfl, -⟩ := KernelObject.schedContext.inj (Option.some.inj hz) + rw [hd _ _ e1, hcon, htime] + rw [hcon] at hpos + refine J.offOK hpos y k hy fun h => ?_ + rw [← ha _ _ e2]; exact hactive (hcsc ▸ h) + · rcases hrun h x (hcur ▸ hx) with h | h <;> rw [h] + · exact Or.inl rfl + · exact Or.inr (Or.inl rfl) + · rw [hcsc] at h ⊢; exact J.curScNotIdle h + · obtain ⟨xb, hxb, hbs⟩ := hb + obtain ⟨y, j, hy, hyt⟩ := hinv.back _ _ _ hxb hbs + rw [hk] at hy + obtain ⟨rfl, -⟩ := KernelObject.schedContext.inj (Option.some.inj hy) + rw [hyt] at ho + rw [← Option.some.inj ho] + exact fun hm => hc0 (hzc _ _ hxb hbs hm) + +end + +/-! ## `CurBA` across the signal -/ + +/-- `CurBA`, and the current thread is not `d`. -/ +def CurBN (d : ObjRef) (σ : State Unit) : Prop := CurBA σ ∧ σ.curThread ≠ d + +theorem CurBN.congr {d : ObjRef} {σ σ' : State Unit} (h : CurBN d σ) (hk : σ'.kheap = σ.kheap) + (hc : σ'.curThread = σ.curThread) (hs : σ'.curSc = σ.curSc) : CurBN d σ' := by + obtain ⟨⟨x, hx, hb⟩, ne⟩ := h + exact ⟨⟨x, by rw [hk, hc]; exact hx, by rw [hs]; exact hb⟩, by rw [hc]; exact ne⟩ + +theorem CurBN.tkeep {d : ObjRef} {σ σ' : State Unit} (k : TKeepA σ σ') (h : CurBN d σ) : CurBN d σ' := by + obtain ⟨⟨x, hx, hb⟩, ne⟩ := h + obtain ⟨ec, -, -, -, -, -, kf, -, es⟩ := k + obtain ⟨x', hx', e⟩ := kf _ x hx + exact ⟨⟨x', by rw [ec]; exact hx', by rw [e, es]; exact hb⟩, by rw [ec]; exact ne⟩ + +theorem HT.curbT {α : Type} {m : SMonad Unit α} (h : TPres m) (d : ObjRef) : HT (CurBN d) m (fun _ => CurBN d) := + fun σ hσ r hr => hσ.tkeep (h σ r hr) + +/-- A step keeping the heap and the current thread and SC keeps `CurBN d`. -/ +theorem HT.curbK {α : Type} {m : SMonad Unit α} (d : ObjRef) + (h : ∀ σ r, r ∈ (m σ).1 → r.2.kheap = σ.kheap ∧ r.2.curThread = σ.curThread ∧ r.2.curSc = σ.curSc) : + HT (CurBN d) m (fun _ => CurBN d) := + fun σ hσ r hr => let e := h σ r hr; hσ.congr e.1 e.2.1 e.2.2 + +/-- A scheduler step (`SfKeepA` and `BKeepA`) keeps `CurBN d`. -/ +theorem HT.curbS {α : Type} {m : SMonad Unit α} (d : ObjRef) (h1 : SfPresA m) (h2 : KPresB m) : + HT (CurBN d) m (fun _ => CurBN d) := + HT.curbK d fun σ r hr => ⟨(h1 σ r hr).kh, (h2 σ r hr).ct, (h2 σ r hr).csc⟩ + +theorem threadSet_curb (d : ObjRef) (f : Tcb → Tcb) : HT (CurBN d) (threadSet (Z := Unit) f d) (fun _ => CurBN d) := + fun σ hσ r hr => by + obtain ⟨x, hx, e⟩ := threadSet_spec hr + obtain ⟨⟨y, hy, hb⟩, ne⟩ := hσ + rw [e] + exact ⟨⟨y, by simpa [Function.update_of_ne ne] using hy, hb⟩, ne⟩ + +theorem setThreadState_curb (d : ObjRef) (ts : ThreadState) : + HT (CurBN d) (setThreadState (Z := Unit) d ts) (fun _ => CurBN d) := by + unfold setThreadState setThreadStateAct + refine HT.bind (Q := fun _ => CurBN d) (threadSet_curb d _) fun _ => ?_ + refine HT.bind (Q := fun _ => CurBN d) (HT.gets _ fun σ h => h) fun cur => ?_ + refine HT.bind (Q := fun _ => CurBN d) (HT.gets _ fun σ h => h) fun act => ?_ + refine HT.bind (Q := fun _ => CurBN d) (HT.gets _ fun σ h => h) fun sch => ?_ + refine HT.when (fun _ => ?_) (fun _ σ h => h) + unfold setSchedulerAction + exact HT.modify _ fun σ h => h.congr rfl rfl rfl + +theorem postpone_curb (d p : ObjRef) : HT (CurBN d) (postpone (Z := Unit) p) (fun _ => CurBN d) := by + unfold postpone + refine HT.bind (Q := fun _ => CurBN d) (HT.curbT (by unfold getScObjRef; tpres) d) fun o => ?_ + refine HT.bind (Q := fun _ => CurBN d) (HT.assertOpt _ fun a _ σ h => h) fun t => ?_ + refine HT.bind (Q := fun _ => CurBN d) (tcbSchedAction_keep _ t (CurBN d) fun σ g h => h.congr rfl rfl rfl) + fun _ => ?_ + refine HT.bind (Q := fun _ => CurBN d) ?_ fun _ => HT.modify _ fun σ h => h.congr rfl rfl rfl + unfold tcbReleaseEnqueue + refine HT.bind (Q := fun _ => CurBN d) (HT.gets _ fun σ h => h) fun q => ?_ + refine HT.bind (Q := fun _ => CurBN d) ?_ fun q' => ?_ + · intro σ hσ r hr + unfold readyTimesAppend at hr + obtain ⟨hs, -⟩ := orderedInsert_spec _ _ _ _ hr + rw [hs]; exact hσ + refine HT.bind (Q := fun _ => CurBN d) ?_ fun _ => ?_ + · unfold setReleaseQueue + exact HT.modify _ fun σ h => h.congr rfl rfl rfl + · exact HT.when (fun _ => HT.modify _ fun σ h => h.congr rfl rfl rfl) (fun _ σ h => h) + +theorem schedContextResume_curb (d p : ObjRef) : + HT (CurBN d) (schedContextResume (Z := Unit) p) (fun _ => CurBN d) := by + unfold schedContextResume + refine HT.bind (Q := fun _ => CurBN d) (HT.curbT (by unfold getSchedContext; tpres) d) fun sc => ?_ + refine HT.bind (Q := fun _ => CurBN d) (HT.assertOpt _ fun a _ σ h => h) fun t => ?_ + refine HT.bind (Q := fun _ => CurBN d) (HT.gets _ fun σ h => h) fun sched => ?_ + refine HT.when (fun _ => ?_) (fun _ σ h => h) + refine HT.bind (Q := fun _ => CurBN d) (HT.threadGet _ t fun σ h x hx => h) fun ts => ?_ + refine HT.bind (Q := fun _ => CurBN d) (HT.curbT (by unfold getScRefillReady; tpres) d) fun ready => ?_ + refine HT.bind (Q := fun _ => CurBN d) (HT.curbT (by unfold getScRefillSufficient; tpres) d) fun suf => ?_ + refine HT.when (fun _ => ?_) (fun _ σ h => h) + refine HT.bind (Q := fun _ => CurBN d) (HT.threadGet _ t fun σ h x hx => h) fun dom => ?_ + refine HT.bind (Q := fun _ => CurBN d) (HT.threadGet _ t fun σ h x hx => h) fun prio => ?_ + refine HT.bind (Q := fun _ => CurBN d) ?_ fun q => ?_ + · unfold getTcbQueue + exact HT.bind (Q := fun _ => CurBN d) (HT.gets _ fun σ h => h) fun qs => HT.pure _ fun σ h => h + refine HT.bind (Q := fun _ => CurBN d) (HT.assert fun _ σ h => h) fun _ => ?_ + exact postpone_curb d p + +/-- `maybe_donate_sc d` keeps `CurBN d`: it donates an SC without a thread, to `d`. -/ +theorem maybeDonateSc_curb (d n : ObjRef) : HT (CurBN d) (maybeDonateSc (Z := Unit) d n) (fun _ => CurBN d) := by + unfold maybeDonateSc + refine HT.bind (Q := fun _ => CurBN d) (HT.curbT (by unfold getTcbObjRef; tpres) d) fun scOpt => ?_ + refine HT.when (fun _ => ?_) (fun _ σ h => h) + refine HT.bind (Q := fun _ => CurBN d) (HT.curbT (by unfold getNtfnObjRef getSkObjRef; tpres) d) fun nsc => ?_ + cases nsc with + | none => exact HT.pure () (fun σ h => h) + | some p => + show HT _ ((do let scTcb ← getScObjRef SchedContext.scTcb p + NonDet.when (scTcb = none) (do schedContextDonate p d; schedContextResume p))) _ + refine HT.bind (Q := fun o σ => CurBN d σ ∧ (o = none → ∀ y k, σ.kheap p = some (.schedContext y k) → y.scTcb = none)) + ?_ fun o => ?_ + · intro σ hσ r hr + obtain ⟨e, y, k, hk, hv⟩ := getScObjRef_mem hr + obtain ⟨a, s'⟩ := r + simp only at e hv ⊢ + subst e + refine ⟨hσ, fun hn y' k' hk' => ?_⟩ + rw [hk] at hk' + have := Option.some.inj hk' + injection this with e1 e2 + subst e1 + rw [← hv]; exact hn + refine HT.when (fun hn => ?_) (fun _ σ h => h.1) + refine HT.bind (Q := fun _ => CurBN d) ?_ fun _ => schedContextResume_curb d p + intro σ hσ r hr + unfold schedContextDonate at hr + obtain ⟨⟨fromOpt, σ1⟩, h1, h2⟩ := mem_bindA.1 hr + obtain ⟨e1, y, k, hk, hv⟩ := getScObjRef_mem h1 + have e1' : σ1 = σ := e1 + have hnone : fromOpt = none := by + have : fromOpt = y.scTcb := hv + rw [this]; exact hσ.2 hn y k hk + rw [e1'] at h2 + obtain ⟨⟨u, σ2⟩, h3, h4⟩ := mem_bindA.1 h2 + have e3 : ((u, σ2) : Unit × State Unit) = ((), σ) := by + unfold NonDet.when at h3 + rw [if_neg (by simp [hnone])] at h3 + exact h3 + have hσ2 : σ2 = σ := (Prod.mk.inj e3).2 + rw [hσ2] at h4 + obtain ⟨⟨u2, σ3⟩, h5, h6⟩ := mem_bindA.1 h4 + have h5' : (u2, σ3) ∈ (setScObjRef (Z := Unit) scTcbUpdate p (some d) σ).1 := h5 + have k3 : TKeepA σ σ3 := (by tpres : TPres (setScObjRef (Z := Unit) scTcbUpdate p (some d))) σ _ h5' + exact threadSet_curb d _ σ3 (hσ.1.tkeep k3) r h6 + +theorem replyUnlinkTcb_curb (d r : ObjRef) : HT (CurBN d) (replyUnlinkTcb (Z := Unit) d r) (fun _ => CurBN d) := by + unfold replyUnlinkTcb + refine HT.bind (Q := fun _ => CurBN d) (HT.curbT (by tpres) d) fun _ => ?_ + refine HT.bind (Q := fun _ => CurBN d) (HT.curbT (by tpres) d) fun _ => ?_ + refine HT.bind (Q := fun _ => CurBN d) (HT.curbT (by tpres) d) fun _ => ?_ + refine HT.bind (Q := fun _ => CurBN d) (HT.curbT (by tpres) d) fun _ => ?_ + refine HT.bind (Q := fun _ => CurBN d) (HT.curbT (by tpres) d) fun _ => ?_ + exact setThreadState_curb d _ + +theorem cancelIpcRecv_curb (d : ObjRef) : + HT (fun σ => CurBN d σ ∧ RecvSt d σ) (cancelIpc (Z := Unit) d) (fun _ => CurBN d) := by + unfold cancelIpc + refine HT.bind (Q := fun st σ => CurBN d σ ∧ ∃ ep rp q, st = .blockedOnReceive ep rp q) ?_ fun st => ?_ + · show HT _ (threadGet (Z := Unit) Tcb.tcbState d) _ + exact HT.threadGet _ d fun σ hσ x hx => ⟨hσ.1, hσ.2 x hx⟩ + refine HT.bind (Q := fun _ σ => CurBN d σ ∧ ∃ ep rp q, st = .blockedOnReceive ep rp q) + (fun σ hσ r hr => ⟨threadSet_curb d _ σ hσ.1 r hr, hσ.2⟩) fun _ => ?_ + cases st with + | blockedOnReceive ep rp q => + dsimp only + refine HT.mono (P := CurBN d) (Q := fun _ => CurBN d) ?_ (fun σ h => h.1) (fun _ _ h => h) + unfold blockedCancelIpc getBlockingObject + refine HT.bind (Q := fun _ => CurBN d) (HT.assertOpt _ fun a _ σ h => h) fun p => ?_ + refine HT.bind (Q := fun _ => CurBN d) (HT.curbT (by unfold tcbEpDequeue; tpres) d) fun _ => ?_ + cases rp with + | none => exact setThreadState_curb d _ + | some r => + dsimp only + exact HT.bind (Q := fun _ => CurBN d) (replyUnlinkTcb_curb d r) fun _ => setThreadState_curb d _ + | _ => exact fun σ hσ _ _ => absurd hσ.2 (by rintro ⟨ep, rp, q, h⟩; cases h) + +/-- The wake-up tail of `update_waiting_ntfn` and the bound-thread arm keeps `CurBN d`. -/ +theorem wakeTail_curb (d n : ObjRef) (badge : Badge) : + HT (CurBN d) + (do setThreadState (Z := Unit) d .running + asUser d (setRegister badge_register badge) + maybeDonateSc d n + let sched ← NonDet.gets (schedulable d) + NonDet.when sched <| possibleSwitchTo d + let scopt ← getTcbObjRef Tcb.tcbSchedContext d + ifSporadicCurScTestRefillUnblockCheck scopt) + (fun _ => CurBN d) := by + refine HT.bind (Q := fun _ => CurBN d) (setThreadState_curb d _) fun _ => ?_ + refine HT.bind (Q := fun _ => CurBN d) (HT.curbT (by tpres) d) fun _ => ?_ + refine HT.bind (Q := fun _ => CurBN d) (maybeDonateSc_curb d n) fun _ => ?_ + refine HT.bind (Q := fun _ => CurBN d) (HT.gets _ fun σ h => h) fun sched => ?_ + refine HT.bind (Q := fun _ => CurBN d) ?_ fun _ => ?_ + · exact HT.when (fun _ => HT.curbS d (inferInstance : SfKInst (possibleSwitchTo (Z := Unit) d)).pres + (inferInstance : KPresBInst (possibleSwitchTo (Z := Unit) d)).pres) (fun _ σ h => h) + refine HT.bind (Q := fun _ => CurBN d) (HT.curbT (by unfold getTcbObjRef; tpres) d) fun _ => ?_ + exact HT.curbT (by unfold ifSporadicCurScTestRefillUnblockCheck ifCondRefillUnblockCheck; tpres) d + +/-- A runnable thread is not a blocked one. -/ +theorem curBN_of_blocked {d : ObjRef} {σ : State Unit} (h : CurBA σ) + (hr : ∀ x, σ.kheap σ.curThread = some (.tcb x) → runnable x.tcbState = true) + (hb : ∀ x, σ.kheap d = some (.tcb x) → runnable x.tcbState ≠ true) : CurBN d σ := by + refine ⟨h, fun e => ?_⟩ + obtain ⟨x, hx, -⟩ := h + exact hb x (e ▸ hx) (hr x hx) + +/-- **`send_signal` keeps `CurBA`** of a runnable current thread (module docstring). -/ +theorem sendSignal_curBA (n : ObjRef) (badge : Badge) : + HT (fun σ => CurBA σ ∧ (∀ x, σ.kheap σ.curThread = some (.tcb x) → runnable x.tcbState = true) ∧ SigQ n σ) + (sendSignal (Z := Unit) n badge) (fun _ => CurBA) := by + unfold sendSignal + let P : State Unit → Prop := fun σ => + CurBA σ ∧ (∀ x, σ.kheap σ.curThread = some (.tcb x) → runnable x.tcbState = true) ∧ SigQ n σ + refine HT.bind (Q := fun ntfn σ => P σ ∧ σ.kheap n = some (.notification ntfn)) ?_ fun ntfn => ?_ + · intro σ hσ r hr + obtain ⟨hk, hs⟩ := getNotification_mem hr + obtain ⟨a, s'⟩ := r + simp only at hk hs ⊢ + subst hs + exact ⟨hσ, hk⟩ + have ns : ∀ (o : Notification), HT (fun σ => P σ ∧ σ.kheap n = some (.notification ntfn)) + (setNotification (Z := Unit) n o) (fun _ => CurBA) := fun o σ hσ r hr => by + have k := (by tpres : TPres (setNotification (Z := Unit) n o)) σ r hr + obtain ⟨⟨⟨x, hx, hb⟩, -⟩, -⟩ := hσ + obtain ⟨ec, -, -, -, -, -, kf, -, es⟩ := k + obtain ⟨x', hx', e⟩ := kf _ x hx + exact ⟨x', by rw [ec]; exact hx', by rw [e, es]; exact hb⟩ + rcases ntfn with ⟨o, bt, nsc⟩ + cases o with + | idleNtfn => + cases bt with + | none => exact ns _ + | some tcb => + dsimp only + refine HT.bind (Q := fun st σ => (P σ ∧ + σ.kheap n = some (.notification { ntfnObj := .idleNtfn, ntfnBoundTcb := some tcb, ntfnSc := nsc })) ∧ + ∃ x, σ.kheap tcb = some (.tcb x) ∧ x.tcbState = st) ?_ fun st => ?_ + · show HT _ (threadGet (Z := Unit) Tcb.tcbState tcb) _ + exact HT.threadGet _ tcb fun σ hσ x hx => ⟨hσ, x, hx, rfl⟩ + refine HT.ite (fun hc => ?_) (fun hc => HT.mono (ns _) (fun σ h => h.1) (fun _ _ h => h)) + cases st with + | blockedOnReceive ep rp q => + refine HT.mono (Q := fun _ => CurBN tcb) ?_ (fun σ h => h) (fun _ _ h => h.1) + refine HT.bind (Q := fun _ => CurBN tcb) (HT.mono (cancelIpcRecv_curb tcb) (fun σ h => ?_) (fun _ _ h => h)) + fun _ => wakeTail_curb tcb n badge + obtain ⟨⟨⟨hB, hR, -⟩, -⟩, x, hx, hst⟩ := h + refine ⟨curBN_of_blocked hB hR fun y hy => ?_, fun y hy => ?_⟩ + all_goals + rw [hx] at hy + have := KernelObject.tcb.inj (Option.some.inj hy) + subst this + · rw [hst]; simp [runnable] + · exact ⟨ep, rp, q, hst⟩ + | _ => simp [receiveBlocked] at hc + | waitingNtfn q => + dsimp only + unfold updateWaitingNtfn + refine HT.bind (Q := fun _ σ => (P σ ∧ + σ.kheap n = some (.notification { ntfnObj := .waitingNtfn q, ntfnBoundTcb := bt, ntfnSc := nsc })) ∧ q ≠ []) + (HT.assert fun h σ hσ => ⟨hσ, h⟩) fun _ => ?_ + refine HT.pure_bind ?_ + refine HT.mono (Q := fun _ => CurBN (the q.head?)) ?_ (fun σ h => h) (fun _ _ h => h.1) + refine HT.bind (Q := fun _ => CurBN (the q.head?)) + (HT.mono (HT.curbT (by unfold tcbNtfnDequeue; tpres) (the q.head?)) (fun σ h => ?_) (fun _ _ h => h)) + fun _ => wakeTail_curb _ n badge + obtain ⟨⟨⟨hB, hR, hq⟩, hk⟩, hne⟩ := h + have hm : the q.head? ∈ q := by + cases q with + | nil => exact absurd rfl hne + | cons a l => exact List.mem_cons_self + obtain ⟨y, hy, hst⟩ := hq _ q _ hk rfl hm + refine curBN_of_blocked hB hR fun x hx => ?_ + rw [hy] at hx + have := KernelObject.tcb.inj (Option.some.inj hx) + subst this + rw [hst]; simp [runnable] + | activeNtfn b => exact ns _ + +end + +section +variable {S A : Type} {st : machine.Store S} [L : LawfulStore st] +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s : S} {a : A} {σ σ2 : State Unit} {m : McState} + +/-- **The Notification send arms end in `BodyEndA`** (module docstring). -/ +theorem BodyEndA.ofSignalA (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {xc : Tcb} (hkc : σ.kheap σ.curThread = some (.tcb xc)) {p : ObjRef} {b : Badge} + (hm : ((), σ2) ∈ (sendSignal (Z := Unit) p b (stUpdA σ σ.curThread xc .restart)).1) + (hI : FIA (stUpdA σ σ.curThread xc .restart)) (hD : RqDomPrio (stUpdA σ σ.curThread xc .restart)) + (hid : IdleSt (stUpdA σ σ.curThread xc .restart)) (hQ : SigQ p (stUpdA σ σ.curThread xc .restart)) + (hinv : ScInvA m σ2) (hzc : ZeroConsA σ2) : BodyEndA σ σ2 m := by + have BK : BKeepA (stUpdA σ σ.curThread xc .restart) σ2 := sendSignal_bkA p b _ _ hm + have hk1 : (stUpdA σ σ.curThread xc .restart).kheap σ.curThread = + some (.tcb { xc with tcbState := .restart }) := by simp [stUpdA, setObjA] + have ne : σ.curSc ≠ σ.curThread := fun e => by + obtain ⟨y0, j, h0⟩ := J.curInv.sc + rw [e, hkc] at h0; cases h0 + have e1 : (stUpdA σ σ.curThread xc .restart).kheap σ.curSc = σ.kheap σ.curSc := by + simp [stUpdA, setObjA, Function.update_of_ne ne] + have SW : SwA σ2 := sendSignal_swa p b (stUpdA σ σ.curThread xc .restart) ⟨fun t e => absurd e (J.noSwitch t), hQ⟩ _ hm + have CB : CurBA σ2 := by + refine sendSignal_curBA p b (stUpdA σ σ.curThread xc .restart) ⟨?_, fun x hx => ?_, hQ⟩ _ hm + · obtain ⟨t, ht, hts, -⟩ := J.curInv.tcb + rw [hkc] at ht + obtain rfl := KernelObject.tcb.inj (Option.some.inj ht) + exact ⟨_, hk1, hts⟩ + · have hx' : (stUpdA σ σ.curThread xc .restart).kheap σ.curThread = some (.tcb x) := hx + rw [hk1] at hx' + obtain rfl := KernelObject.tcb.inj (Option.some.inj hx') + rfl + have ST := sendSignal_ctStMA p b _ hk1 (Or.inr (Or.inl rfl)) hI hD hid hQ hm + refine BodyEndA.ofCurBA J hinv hzc SW BK.time (by rw [BK.ms]; rfl) BK.ct BK.csc BK.used (fun y' k h => ?_) CB + (fun _ x hx => Or.inr (ST x hx)) + obtain ⟨y, hy, h1, h2⟩ := BK.sc y' k h + exact ⟨y, by rw [← e1]; exact hy, h1, h2⟩ + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3BodyKeepA.lean b/proofs/Fl4ProofsA64/P3/Stage3BodyKeepA.lean new file mode 100644 index 000000000..8854ec6d4 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3BodyKeepA.lean @@ -0,0 +1,107 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Plan B step 2): **a spec step that keeps what the body-end facts read** +(`P3/Stage3BodyOKA`). + +`BodyKeepA σ σ'`: the scheduler action, the clock and counter observation, the current thread and SC, the consumed +time and the release queue are kept; every SC and every TCB state of `σ'` was in `σ`. A preorder. It follows from +`SpecSlotA` with the machine state kept (cap writes) and from `VsSpecA` (VSpace writes). + +Named premises: none. + +New module. +-/ +import Fl4ProofsA64.P3.Stage3VsWriteA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +/-- **The step relation** (module docstring). -/ +structure BodyKeepA (σ σ' : State Unit) : Prop where + act : σ'.schedulerAction = σ.schedulerAction + time : σ'.curTime = σ.curTime + last : σ'.machineState.lastMachineTime = σ.machineState.lastMachineTime + cur : σ'.curThread = σ.curThread + csc : σ'.curSc = σ.curSc + cons : σ'.consumedTime = σ.consumedTime + rel : σ'.releaseQueue = σ.releaseQueue + sc : ∀ p y k, σ'.kheap p = some (.schedContext y k) → σ.kheap p = some (.schedContext y k) + tcb : ∀ p x', σ'.kheap p = some (.tcb x') → ∃ x, σ.kheap p = some (.tcb x) ∧ x'.tcbState = x.tcbState + +namespace BodyKeepA + +theorem refl (σ : State Unit) : BodyKeepA σ σ := + ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, fun _ _ _ h => h, fun _ x h => ⟨x, h, rfl⟩⟩ + +theorem trans {a b c : State Unit} (h1 : BodyKeepA a b) (h2 : BodyKeepA b c) : BodyKeepA a c where + act := h2.act.trans h1.act + time := h2.time.trans h1.time + last := h2.last.trans h1.last + cur := h2.cur.trans h1.cur + csc := h2.csc.trans h1.csc + cons := h2.cons.trans h1.cons + rel := h2.rel.trans h1.rel + sc := fun p y k h => h1.sc p y k (h2.sc p y k h) + tcb := fun p x h => by + obtain ⟨y, hy, e⟩ := h2.tcb p x h + obtain ⟨z, hz, f⟩ := h1.tcb p y hy + exact ⟨z, hz, e.trans f⟩ + +/-- A cap-slot step with the machine state kept. -/ +theorem ofSlot {σ σ' : State Unit} (H : SpecSlotA σ σ') (hm : σ'.machineState = σ.machineState) : + BodyKeepA σ σ' where + act := H.act + time := H.time + last := by rw [hm] + cur := H.cur + csc := H.csc + cons := H.cons + rel := H.rls + sc := fun p y k h => H.back h (fun _ _ e => by cases e) (fun _ e => by cases e) + tcb := fun p x' h => by + rcases H.kh p with e | ⟨r, cs, cs', -, e⟩ | ⟨x, y', e1, e2, f⟩ + · exact ⟨x', e ▸ h, rfl⟩ + · rw [h] at e; cases e + · rw [h] at e2; cases e2 + exact ⟨x, e1, (tcbNcA_eq f).1⟩ + +/-- A VSpace-write step. -/ +theorem ofVs {σ σ' : State Unit} (H : VsSpecA σ σ') : BodyKeepA σ σ' := by + have e := H.eq + refine ⟨?_, ?_, ?_, ?_, ?_, ?_, ?_, fun p y k h => ?_, fun p x' h => ?_⟩ + · rw [e] + · rw [e] + · rw [e] + · rw [e] + · rw [e] + · rw [e] + · rw [e] + · rcases H.kh p with e1 | ⟨a, a', -, e2, -⟩ + · rwa [e1] at h + · rw [h] at e2; cases e2 + · rcases H.kh p with e1 | ⟨a, a', -, e2, -⟩ + · exact ⟨x', e1 ▸ h, rfl⟩ + · rw [h] at e2; cases e2 + +/-- One object replaced by one that is neither a TCB nor an SC. -/ +theorem ofOther (σ : State Unit) (p : ObjRef) {o : KernelObject} (hs : ∀ y k, o ≠ .schedContext y k) + (ht : ∀ x, o ≠ .tcb x) : BodyKeepA σ { σ with kheap := Function.update σ.kheap p (some o) } := by + refine ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, fun q y k h => ?_, fun q x' h => ?_⟩ + · by_cases e : q = p + · subst e + simp only [Function.update_self, Option.some.injEq] at h + exact absurd h (hs y k) + · simpa only [Function.update_of_ne e] using h + · by_cases e : q = p + · subst e + simp only [Function.update_self, Option.some.injEq] at h + exact absurd h (ht x') + · exact ⟨x', by simpa only [Function.update_of_ne e] using h, rfl⟩ + +end BodyKeepA + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3BodyOKA.lean b/proofs/Fl4ProofsA64/P3/Stage3BodyOKA.lean new file mode 100644 index 000000000..53739702b --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3BodyOKA.lean @@ -0,0 +1,233 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Plan B step 1): **what a kernel-entry body leaves for `syscall.finish`**, beside the +Stage-3 record `JStage3A` (frozen once, Architect 04:21Z). + +`BodyOKA σ m` is a spec-side bundle at the body's end state. The exit side (`syscall.finish`: timer pass, schedule, +activation, next entry record) needs these facts and `JStage3A` does not hold them. `BodyExitA` is the part carried +across `timer::fire` (kernel-entry Plan A's structure, same name and fields; `BodyOKA` extends it): + +| field | needed by | +|---|---| +| `validTime` | the next entry's `validTime` | +| `budget` | `OutgoingChargeA` (`tail.charge`), the next entry's `offOK` / `offReady` on the no-switch path | +| `ctAct` | activation without a switch (`ScheduleFinishInputsA.activation`), the next entry's `sact` | +| `sw` | `fire_recA` (`SwA` at the body state), `FireRecA` | +| `scNotIdle` | `OutgoingChargeA`'s `NotIdleScA` of the outgoing SC | +| `owner` | `OutgoingChargeA`'s owner outside the release queue | + +`SwA` is not derivable from `JStage3A`: its only fact on a pending switch target is `SchedSRN.actOn` (a TCB of this +core); `actIdle` is a field of the entry record `JStage3EntryA` only. + +* `BodyOKA.ofEntry`: from the syscall-entry record, across a spec step that keeps the clock, the current thread and SC, + the consumed time, the release queue and the current SC's refills / bound / thread, keeps the scheduler action or + leaves `chooseNewThread`, and when resuming leaves the current thread running or restarting. +* `BodyOKA.ofKeep`: `ofEntry` across `Restart` of the current thread and a `BodyKeepA` step. +* `curW_keptA`: two records whose spec states have the same current thread have the same `CUR_THREAD` word. +* `BodyOKA.charge`: with the record, the conclusion of `OutgoingChargeA` at the body state. + +Named premises: none (the fields are [J] facts each body proves). +-/ +import Fl4ProofsA64.P3.Stage3SyscallA +import Fl4ProofsA64.P3.Stage3BodyKeepA +import Fl4ProofsA64.P3.Stage3SchedSelectA +import Fl4ProofsA64.P3.ScActivateA +import Fl4ProofsA64.P3.ScTimeA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +/-- [J] **What `timer::fire` and the exit read** at a body's end (module docstring). -/ +structure BodyExitA (σ : State Unit) : Prop where + /-- (G-A) The current time is bounded by the last counter observation. -/ + validTime : ValidMachineTimeA σ + /-- (G-A) A current SC that is the idle SC or active has its head ready before and after the consumed time, + while there is consumed time or no switch is pending (a budget charge resets the time and leaves a switch pending, + with the charged SC's head possibly in the future). -/ + budget : ∀ y n, σ.kheap σ.curSc = some (.schedContext y n) → (σ.curSc = idleScPtr ∨ scActive y = true) → + σ.consumedTime ≠ 0 ∨ σ.schedulerAction = .resumeCurThread → OffReadyA σ ∧ OffOKA σ + /-- (G-C) With no switch pending, the current thread's state is one activation accepts. -/ + ctAct : σ.schedulerAction = .resumeCurThread → ∀ x, σ.kheap σ.curThread = some (.tcb x) → + ActivateStateOKA x.tcbState + +/-- [J] **The body-end facts** (module docstring). -/ +structure BodyOKA (σ : State Unit) (m : McState) : Prop extends BodyExitA σ where + /-- A pending switch target is `schedulable`. -/ + sw : SwA σ + /-- A non-idle current SC is no core's idle SC. -/ + scNotIdle : σ.curSc ≠ idleScPtr → NotIdleScA m σ.curSc + /-- When the current SC is to be charged, the thread bound to it is outside the release queue. -/ + owner : ∀ y n, σ.kheap σ.curSc = some (.schedContext y n) → scActive y = true → σ.curSc ≠ idleScPtr → + σ.consumedTime ≠ 0 → ∀ o, y.scTcb = some o → o ∉ σ.releaseQueue + +section +variable {S A : Type} {st : machine.Store S} [L : LawfulStore st] +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s : S} {a : A} {σ σ' : State Unit} {m : McState} + +/-- **The body-end facts from the syscall-entry record**, across a spec step that keeps what they read, the +scheduler action kept or left at `chooseNewThread` (module docstring). -/ +theorem BodyOKA.ofEntry (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (hact : σ'.schedulerAction = σ.schedulerAction ∨ σ'.schedulerAction = .chooseNewThread) (htime : σ'.curTime = σ.curTime) + (hlast : σ'.machineState.lastMachineTime = σ.machineState.lastMachineTime) + (hthr : σ'.curThread = σ.curThread) (hsc : σ'.curSc = σ.curSc) (hcon : σ'.consumedTime = σ.consumedTime) + (hrel : σ'.releaseQueue = σ.releaseQueue) + (hobj : ∀ y' k, σ'.kheap σ.curSc = some (.schedContext y' k) → ∃ y, σ.kheap σ.curSc = some (.schedContext y k) ∧ + y'.scRefills = y.scRefills ∧ y'.scRefillMax = y.scRefillMax ∧ y'.scTcb = y.scTcb) + (hrun : σ'.schedulerAction = .resumeCurThread → ∀ x, σ'.kheap σ.curThread = some (.tcb x) → + x.tcbState = .running ∨ x.tcbState = .restart) : + BodyOKA σ' m := by + have hd : ∀ y' y : SchedContext, y'.scRefills = y.scRefills → refillHd y' = refillHd y := fun y' y e => by + unfold refillHd; rw [e] + have ha : ∀ y' y : SchedContext, y'.scRefillMax = y.scRefillMax → scActive y' = scActive y := fun y' y e => by + unfold scActive; rw [e] + refine ⟨⟨?_, fun y' k hk _ _ => ?_, fun h x hx => ?_⟩, fun t e => ?_, fun h => ?_, fun y' k hk _ _ _ o ho => ?_⟩ + · have := J.validTime + unfold ValidMachineTimeA at this ⊢ + rw [htime, hlast]; exact this + · rw [hsc] at hk + obtain ⟨y, hy, e1, e2, -⟩ := hobj y' k hk + constructor + · intro z j hz + rw [hsc, hk] at hz + obtain ⟨rfl, -⟩ := KernelObject.schedContext.inj (Option.some.inj hz) + rw [hd _ _ e1, hcon, htime] + exact J.offReady y k hy + · intro hpos z j hz hactive + rw [hsc, hk] at hz + obtain ⟨rfl, -⟩ := KernelObject.schedContext.inj (Option.some.inj hz) + rw [hd _ _ e1, hcon, htime] + rw [hcon] at hpos + refine J.offOK hpos y k hy fun h => ?_ + rw [← ha _ _ e2]; exact hactive (hsc ▸ h) + · rcases hrun h x (hthr ▸ hx) with h | h <;> rw [h] + · exact Or.inl rfl + · exact Or.inr (Or.inl rfl) + · rcases hact with h | h + · rw [h, J.act] at e; cases e + · rw [h] at e; cases e + · rw [hsc] at h ⊢; exact J.curScNotIdle h + · rw [hsc] at hk + obtain ⟨y, hy, -, -, e3⟩ := hobj y' k hk + obtain ⟨x, hx, hxs, -⟩ := J.curInv.tcb + obtain ⟨z, j, hz, hzt⟩ := J.sr.inv.back _ x _ hx hxs + rw [hy] at hz + obtain ⟨rfl, -⟩ := KernelObject.schedContext.inj (Option.some.inj hz) + rw [e3, hzt] at ho + rw [hrel, ← Option.some.inj ho] + exact J.curInv.notRel + + +/-- **The body-end facts with `SwA` given and a release queue that may grow elsewhere** (Integration round 2): +`ofEntry`'s proof with the pending-switch field from `SwA σ'` and the owner field from the current thread being outside +the release queue (a `sched_context_resume` of another thread may enqueue that one). -/ +theorem BodyOKA.ofEntryHT (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (hsw : SwA σ') (htime : σ'.curTime = σ.curTime) + (hlast : σ'.machineState.lastMachineTime = σ.machineState.lastMachineTime) + (hthr : σ'.curThread = σ.curThread) (hsc : σ'.curSc = σ.curSc) (hcon : σ'.consumedTime = σ.consumedTime) + (hrel : σ.curThread ∉ σ'.releaseQueue) + (hobj : ∀ y' k, σ'.kheap σ.curSc = some (.schedContext y' k) → ∃ y, σ.kheap σ.curSc = some (.schedContext y k) ∧ + y'.scRefills = y.scRefills ∧ y'.scRefillMax = y.scRefillMax ∧ y'.scTcb = y.scTcb) + (hrun : σ'.schedulerAction = .resumeCurThread → ∀ x, σ'.kheap σ.curThread = some (.tcb x) → + x.tcbState = .running ∨ x.tcbState = .restart) : + BodyOKA σ' m := by + have hd : ∀ y' y : SchedContext, y'.scRefills = y.scRefills → refillHd y' = refillHd y := fun y' y e => by + unfold refillHd; rw [e] + have ha : ∀ y' y : SchedContext, y'.scRefillMax = y.scRefillMax → scActive y' = scActive y := fun y' y e => by + unfold scActive; rw [e] + refine ⟨⟨?_, fun y' k hk _ _ => ?_, fun h x hx => ?_⟩, hsw, fun h => ?_, fun y' k hk _ _ _ o ho => ?_⟩ + · have := J.validTime + unfold ValidMachineTimeA at this ⊢ + rw [htime, hlast]; exact this + · rw [hsc] at hk + obtain ⟨y, hy, e1, e2, -⟩ := hobj y' k hk + constructor + · intro z j hz + rw [hsc, hk] at hz + obtain ⟨rfl, -⟩ := KernelObject.schedContext.inj (Option.some.inj hz) + rw [hd _ _ e1, hcon, htime] + exact J.offReady y k hy + · intro hpos z j hz hactive + rw [hsc, hk] at hz + obtain ⟨rfl, -⟩ := KernelObject.schedContext.inj (Option.some.inj hz) + rw [hd _ _ e1, hcon, htime] + rw [hcon] at hpos + refine J.offOK hpos y k hy fun h => ?_ + rw [← ha _ _ e2]; exact hactive (hsc ▸ h) + · rcases hrun h x (hthr ▸ hx) with h | h <;> rw [h] + · exact Or.inl rfl + · exact Or.inr (Or.inl rfl) + · rw [hsc] at h ⊢; exact J.curScNotIdle h + · rw [hsc] at hk + obtain ⟨y, hy, -, -, e3⟩ := hobj y' k hk + obtain ⟨x, hx, hxs, -⟩ := J.curInv.tcb + obtain ⟨z, j, hz, hzt⟩ := J.sr.inv.back _ x _ hx hxs + rw [hy] at hz + obtain ⟨rfl, -⟩ := KernelObject.schedContext.inj (Option.some.inj hz) + rw [e3, hzt] at ho + rw [← Option.some.inj ho] + exact hrel + + +/-- **The body-end facts across `Restart` and a keeping step** (module docstring). -/ +theorem BodyOKA.ofKeep (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {x : Tcb} (hk : σ.kheap σ.curThread = some (.tcb x)) + (K : BodyKeepA (stUpdA σ σ.curThread x .restart) σ') : BodyOKA σ' m := by + refine BodyOKA.ofEntry J (Or.inl K.act) K.time K.last K.cur K.csc K.cons K.rel (fun y' k h => ?_) + (fun _ y h => ?_) + · have h1 := K.sc _ _ _ h + have ne : σ.curSc ≠ σ.curThread := fun e => by + rw [e] at h1; simp [stUpdA, setObjA] at h1 + simp only [stUpdA, setObjA, Function.update_of_ne ne] at h1 + exact ⟨y', h1, rfl, rfl, rfl⟩ + · obtain ⟨z, hz, e⟩ := K.tcb _ _ h + simp only [stUpdA, setObjA, Function.update_self] at hz + obtain rfl := KernelObject.tcb.inj (Option.some.inj hz) + exact Or.inr e + +/-- **The body-end facts after `Restart` alone.** -/ +theorem BodyOKA.ofRestart (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {x : Tcb} (hk : σ.kheap σ.curThread = some (.tcb x)) {τ : State Unit} + (h : τ = stUpdA σ σ.curThread x .restart) : BodyOKA τ m ∧ τ.curThread = σ.curThread := by + subst h + exact ⟨BodyOKA.ofKeep J hk (BodyKeepA.refl _), rfl⟩ + +/-- **The `CUR_THREAD` word from the spec current thread** (module docstring). -/ +theorem curW_keptA {Z' : XSetsA} {H' : Heap} {OS' U' Ac' Pp' Tb' : Set U64} {Lv' : U64 → Nat} {ia' ie' : Nat} + {N' E' Rp' : Set U64} {Bf' : Set (U64 × Fl4SpecA64.Word)} {stack' : U64 → List U64} {κ' : U64 → U64} {n' : Nat} + {AR' : A → HwRootA → Prop} {J5' J7' : JSlotA S} {s' : S} {a' : A} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (J' : JStage3A st AR' Z' H' OS' U' Ac' Pp' Tb' Lv' ia' ie' N' E' Rp' Bf' stack' κ' n' J5' J7' s' a' σ' m) + (h : σ'.curThread = σ.curThread) : L.curW s' = L.curW s := by + have e := J.sr.sch.curThread + have e' := J'.sr.sch.curThread + rw [h, e] at e' + exact (specPtr_inj e').symm + +/-- **`OutgoingChargeA`'s conclusion at the body state**, from the record and the body-end facts. -/ +theorem BodyOKA.charge (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (B : BodyOKA σ m) {y : SchedContext} {k : Nat} (hk : σ.kheap σ.curSc = some (.schedContext y k)) + (hact : scActive y = true) (hni : σ.curSc ≠ idleScPtr) (hcon : σ.consumedTime ≠ 0) : + OffOKA σ ∧ OffReadyA σ ∧ NotIdleScA m σ.curSc ∧ + ∃ owner, (y.scTcb = none ∨ y.scTcb = some owner) ∧ owner ∉ σ.releaseQueue := by + have o := B.budget y k hk (Or.inr hact) (Or.inl hcon) + refine ⟨o.2, o.1, B.scNotIdle hni, ?_⟩ + cases h : y.scTcb with + | some t => exact ⟨t, Or.inr rfl, B.owner y k hk hact hni hcon t h⟩ + | none => + refine ⟨σ.idleThread, Or.inl rfl, fun hm => ?_⟩ + obtain ⟨x, hx, hr⟩ := J.sr.run.2 _ hm + obtain ⟨z, hz, hs⟩ := J.sinv.idleTcb + rw [hz] at hx + obtain rfl := KernelObject.tcb.inj (Option.some.inj hx) + rw [hs] at hr + cases hr + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3BootA.lean b/proofs/Fl4ProofsA64/P3/Stage3BootA.lean new file mode 100644 index 000000000..0fba06752 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3BootA.lean @@ -0,0 +1,1186 @@ +/- +P3 (aarch64, A64-P3 Stage-3 integration step 1): **boot witnesses of the unified Stage-3 record** (`P3/Stage3A`) at +the concrete `w0` and `w1` (`kb = PPTR_BASE + 2^30`, `ib = PPTR_BASE + 2^31`, 128 cores): **`boot_stage3_ex`** (`w0`, empty IPC ghosts) +and **`w1_stage3_ex`** (`w1` at `w1SUA`, the root IPC buffer tracked), with the entry-phase facts +(`JStage3EntryA`). The J5 / J7 slots are arbitrary, given a proof at the boot state. + +The D1 clauses (`P3/Stage3FieldsA`) hold with no further hypothesis. + +Hypotheses: the platform's node count is 128 (`hnc` / `hnn`), and `sl ≥ MIN_BUDGET` at `w1` (as +`w1_schedWitnessA`). New module; no existing statement changed. +-/ +import Fl4ProofsA64.P3.Stage3A +import Fl4ProofsA64.P3.Stage3DomIpiA +import Fl4ProofsA64.P3.BootAddrA +import Fl4ProofsA64.P3.PtCapBootA +import Fl4ProofsA64.P1.S1aCBootSym +import Fl4ProofsA64.P1.MdbBootInvA +import Fl4ProofsA64.P3.Stage3FBootA +import Fl4ProofsA64.P3.Stage3BootNullA +import Fl4ProofsA64.P3.JEntW0A +import Fl4ProofsA64.P3.W1UserBytesA +import Fl4ProofsA64.P3.TabOffW1A +import Fl4ProofsA64.P3.SlotCoverageBootA +import Fl4ProofsA64.P1.BootWit +import Fl4ProofsA64.P1.BootW1Items +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P1.W7.R.ST.BootWit Fl4ProofsA64.P3.Endp + Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched + +noncomputable section + +/-! ## The scheduler relation across the byte memory -/ + +section Um +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +theorem Sched.SchedSRA.withUmA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} + {σ : Fl4SpecA64.State Unit} (H : SchedSRA st Z m κ s σ) (um : Fl4SpecA64.Word → Fl4SpecA64.Word8) : + SchedSRA st Z m κ s (Fl4ProofsA64.P3.withUmA σ um) := + { H with + sch := { H.sch with } + rest := { H.rest with } + scr := { H.scr with } + scp := { H.scp with } + inv := H.inv.of_kheap rfl rfl + run := H.run + rd := H.rd } + +end Um + +/-- (record edit #8b-D) No `w1` heap object reached through the boot heap is a non-idle SC (`bootKA_scA` at `w1MA`, +which has `bootMA`'s idle SCs). -/ +private theorem w1_bootKA_idleA {kb ib now mt : U64} {nc : Nat} (hnc : nc ≤ Fl4SpecA64.fl4A64Platform.numNodes) + {p : Fl4SpecA64.ObjRef} {y : Fl4SpecA64.SchedContext} {n : Nat} + (h : bootKA kb ib nc now mt p = some (.schedContext y n)) : ¬ Sched.NotIdleScA (w1MA ib nc) p := + fun hn => (Sched.bootKA_scA hnc h).2.2 ⟨hn.1, fun c hc => hn.2 c hc⟩ + +/-- Boot SC objects of size exponent 0 fit their heap objects (`HeapOK.scmin`); (record edit #8b-D) a non-idle one +(`NI`) fills a 128-byte object (`h7`). -/ +private theorem scSize_of_zeroA {H : Heap} (hH : HeapOK H) {C : Set U64} (hs : H.scs = C) + {k : Fl4SpecA64.ObjRef → Option Fl4SpecA64.KernelObject} {nm : U64 → Fl4SpecA64.ObjRef} {NI : U64 → Prop} + (h0 : ∀ c ∈ C, ∀ x n, k (nm c) = some (.schedContext x n) → n = 0) + (h7 : ∀ c ∈ C, ∀ x n, k (nm c) = some (.schedContext x n) → NI c → H.scBits c = 7) : + ∀ c ∈ C, ∀ x n, k (nm c) = some (.schedContext x n) → + n + Fl4SpecA64.minSchedContextBits ≤ H.scBits c ∧ (NI c → n + Fl4SpecA64.minSchedContextBits = H.scBits c) := by + intro c hc x n hk + refine ⟨?_, fun hn => by rw [h0 c hc x n hk, h7 c hc x n hk hn]; rfl⟩ + rw [h0 c hc x n hk] + rw [← hs] at hc + obtain ⟨z, hz⟩ := hc + have e : H.scBits c = z := by unfold Heap.scBits; rw [hz] + have := hH.scmin c z hz + rw [e] + unfold Fl4SpecA64.minSchedContextBits + omega + +/-! ## The TCB relations of a boot TCB, from its words -/ + +/-- A TCB's word at offset `o`, read through any address of that value. -/ +def TcbWordsA (mem : U64 → U64) (t : U64) (f : Nat → U64) : Prop := + ∀ (x : U64) (o : Nat), o < 4096 → o ≠ 3576 → x.val = t.val + o → mem x = f o + +theorem tcbRels_of_wordsA {mem : U64 → U64} {t : U64} {f : Nat → U64} {x : Fl4SpecA64.Tcb} (ht : TcbAddr t) + (W : TcbWordsA mem t f) + (hst : (f 3328).val = tsCode x.tcbState) + (hs : x.tcbState = .running ∨ x.tcbState = .idleThreadState) + (hregs : ∀ r, (f (256 + 8 * (regIdxA r).val)).bv = x.tcbArch.tcbContext.userRegs r) + (hprio : (f 3448).val = x.tcbPriority.toNat) + (hfault : f 3376 = 0#u64) (hf : x.tcbFault = none) + (hbnd : f 3368 = 0#u64) (hb : x.tcbBoundNotification = none) : + TcbRelE ∅ mem t x ∧ TcbRel1 mem t x := by + have wd : ∀ (K : U64) (o : Nat), K.val = o → o < 4096 → o ≠ 3576 → + mem (core.num.U64.wrapping_add t K) = f o := fun K o hK h1 h2 => + W _ o h1 h2 (by rw [tfield_val ht (by omega), hK]) + have wreg : ∀ r, mem (Endp.regA t (regIdxA r)) = f (256 + 8 * (regIdxA r).val) := fun r => by + have := regIdxA_lt r + exact W _ _ (by omega) (by omega) (by rw [Endp.regA_val ht (regIdxA_lt r)]; omega) + have est : mem (stA t) = f 3328 := by rw [stA_eq]; exact wd 3328#u64 3328 rfl (by omega) (by omega) + have epr : mem (prioA t) = f 3448 := wd 3448#u64 3448 rfl (by omega) (by omega) + have efa : mem (tFaultA t) = 0#u64 := (wd 3376#u64 3376 rfl (by omega) (by omega)).trans hfault + have ebd : mem (tBoundA t) = 0#u64 := (wd 3368#u64 3368 rfl (by omega) (by omega)).trans hbnd + have ebadge : (mem (badgeRegA t)).bv = badgeOf x := by + rw [Endp.badgeRegA_eq_regA, wreg]; exact hregs _ + refine ⟨⟨by rw [est]; exact hst, ?_, ?_, ?_, ?_, fun r => by rw [wreg]; exact hregs r, + by rw [epr]; exact hprio, ?_, ?_, ?_, ?_⟩, ⟨by rw [est]; exact hst, ?_, ebadge, by rw [epr]; exact hprio⟩⟩ + · intro e h; rcases hs with hs | hs <;> rw [hs] at h <;> cases h + · intro e d h; rcases hs with hs | hs <;> rw [hs] at h <;> cases h + · intro e r d h; rcases hs with hs | hs <;> rw [hs] at h <;> cases h + · intro r h; rcases hs with hs | hs <;> rw [hs] at h <;> cases h + · rw [efa, hf]; simp + · rw [efa, hf]; simp + · rw [ebd, hb]; simp [optRef] + · unfold FaultRel; rw [hf, efa]; rfl + · intro n h; rcases hs with hs | hs <;> rw [hs] at h <;> cases h + +theorem idle_regsA (ib : U64) (c : Nat) (sc : Fl4SpecA64.ObjRef) (r : Fl4SpecA64.Register) : + (tcbW ib c (256 + 8 * (regIdxA r).val)).bv = (idleTcbA sc).tcbArch.tcbContext.userRegs r := by + cases r <;> rfl + +theorem root_regsA (ib : U64) (r : Fl4SpecA64.Register) : + (rtW ib (256 + 8 * (regIdxA r).val)).bv = rootTcbA.tcbArch.tcbContext.userRegs r := by + cases r <;> rfl + +theorem idle_relsA {mem : U64 → U64} {t ib : U64} {c : Nat} (sc : Fl4SpecA64.ObjRef) (ht : TcbAddr t) + (W : TcbWordsA mem t (tcbW ib c)) : TcbRelE ∅ mem t (idleTcbA sc) ∧ TcbRel1 mem t (idleTcbA sc) := + tcbRels_of_wordsA ht W rfl (Or.inr rfl) (idle_regsA ib c sc) rfl rfl rfl rfl rfl + +theorem root_relsA {mem : U64 → U64} {t ib : U64} (ht : TcbAddr t) (W : TcbWordsA mem t (rtW ib)) : + TcbRelE ∅ mem t rootTcbA ∧ TcbRel1 mem t rootTcbA := + tcbRels_of_wordsA ht W rfl (Or.inl rfl) (root_regsA ib) rfl rfl rfl rfl rfl + +section Words +variable {kb ib r now mt sl : U64} {nc : Nat} {uw : U64 → U64} + +theorem w0_wordsA (P : BootLay kb ib nc) {t : U64} (ht : t ∈ (bootH kb ib nc).tcbs) : + ∃ c < nc, TcbWordsA (bL.mem (bootS kb ib nc r now mt)) t (tcbW ib c) := by + obtain ⟨c, hc, e, -⟩ := tw (r := r) (now := now) (mt := mt) P ht (K := 0#u64) (k := 0) rfl (by omega) + refine ⟨c, hc, fun x o ho _ hx => ?_⟩ + exact tcbV P hc ho (by omega) + +theorem w1_wordsA (P : BootLay1 kb ib nc) {t : U64} (ht : t ∈ (bootH kb ib nc).tcbs) : + ∃ c < nc, TcbWordsA (w1M kb ib nc r now mt sl uw) t (tcbW ib c) := by + obtain ⟨c, hc, e, -⟩ := tw (r := r) (now := now) (mt := mt) P.w0 ht (K := 0#u64) (k := 0) rfl (by omega) + refine ⟨c, hc, fun x o ho h' hx => ?_⟩ + have := P.w0.ibw + rw [w1M_idle P (by omega) (by omega) (by omega)] + exact tcbV P.w0 hc ho (by omega) + +theorem w1_rootWordsA (P : BootLay1 kb ib nc) : TcbWordsA (w1M kb ib nc r now mt sl uw) (bwv RTb) (rtW ib) := + fun x o ho _ hx => w1M_rt P ho (by rw [hx, vRT]) + +theorem w1_tvcpuA {t : U64} (P : BootLay1 kb ib nc) (ht : t ∈ (w1H kb ib nc).tcbs) (hp : TcbAddr t) : + bL.mem (w1S kb ib nc r now mt sl uw) (core.num.U64.wrapping_add t 3664#u64) = 0#u64 := by + rcases w1T P ht with h | rfl + · obtain ⟨c, hc, W⟩ := w1_wordsA (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) P h + exact (W _ 3664 (by norm_num) (by norm_num) (field_val hp (by decide))).trans (by simp [tcbW]) + · exact (w1_rootWordsA (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) P _ 3664 (by norm_num) + (by norm_num) (field_val hp (by decide))).trans (by simp [rtW]) + +theorem w1_trunVcpuA {t : U64} (P : BootLay1 kb ib nc) (ht : t ∈ (w1H kb ib nc).tcbs) (hp : TcbAddr t) : + bL.mem (w1S kb ib nc r now mt sl uw) (core.num.U64.wrapping_add t 3672#u64) = 0#u64 := by + rcases w1T P ht with h | rfl + · obtain ⟨c, hc, W⟩ := w1_wordsA (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) P h + exact (W _ 3672 (by norm_num) (by norm_num) (field_val hp (by decide))).trans (by simp [tcbW]) + · exact (w1_rootWordsA (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) P _ 3672 (by norm_num) + (by norm_num) (field_val hp (by decide))).trans (by simp [rtW]) + +end Words + +/-! ## Boot caps -/ + +/-- No root CNode cap is an unmapped table or VSpace root cap. -/ +def NoUnmCapA (c : capability.Cap) : Prop := + (∀ p asid va, c ≠ .PageTable p false asid va) ∧ + (∀ p asid, c ≠ .Pml4 p false asid) ∧ + (∀ p asid va, c ≠ .Ept p 0#u64 false asid va) ∧ + (∀ p asid, c ≠ .HighVSpace p false asid) + +macro "nounm" : tactic => `(tactic| (unfold NoUnmCapA; refine ⟨fun _ _ _ h => ?_, fun _ _ h => ?_, fun _ _ _ h => ?_, fun _ _ h => ?_⟩ <;> cases h)) + +theorem capOf_noUnmA (nc i : Nat) : NoUnmCapA (capOf nc i) := by + unfold capOf + by_cases h1 : i < 16 + · rw [slotC_lo h1]; interval_cases i <;> nounm + by_cases h2 : i < 16 + nc + · rw [slotC_sch (by omega) h2]; nounm + rw [slotC_hi (by omega)] + generalize i - (16 + nc) = k + by_cases h3 : k < 37 + · interval_cases k <;> nounm + · rw [tailC_big (by omega)]; nounm + +/-! ## The D1 clauses at boot -/ + +/-- The idle clause from the boot view's idle objects. -/ +private theorem idleAll_bootA {S : Type} {st : machine.Store S} [L : LawfulStore st] {Z : XSetsA} + {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} {σ : Fl4SpecA64.State Unit} {ib : U64} {nc : Nat} + (SR : SchedSRA st Z m κ s σ) (hm : ∀ c, m.idleThreadOf c = specPtr (idleTA ib c)) + (hs : ∀ c, m.idleScOf c = specPtr (idleSA ib c)) (hCs : ∀ c ∈ Z.Cs, c.val < nc) (hL : BootLayA ib nc) + (hn : Fl4SpecA64.fl4A64Platform.numNodes ≤ nc) : IdleAllA st ib.val (ib.val + nc * 8192) Z.Cs m s := by + refine ⟨fun c hc => ?_, fun c hc => ?_, fun c hc => ?_⟩ + · have e := specPtr_inj ((SR.sch.idleOf c hc).symm.trans (hm c.val)) + have := hCs c hc + rw [e, idleTA_val' hL this]; omega + · have e := specPtr_inj ((SR.sch.idleScOf c hc).symm.trans (hs c.val)) + have := hCs c hc + rw [e, idleSA_val' hL this]; omega + · have : c < nc := by omega + exact ⟨idleTA ib c, hm c, by rw [idleTA_val' hL this]; omega, by rw [idleTA_val' hL this]; omega⟩ + +/-- A frame cap's base at `w1`: the extra BootInfo, the IPC buffer, the BootInfo frame or the image frame. -/ +private def W1FrPtrA (v : Nat) : Prop := (EFb ≤ v ∧ v < EFb + 131072) ∨ v = IPb ∨ v = BIb ∨ v = IMb + +/-- The shape of every `w1` cap: frames are normal frames at `W1FrPtrA`, the tables are `w1Tb`, no zombie, Stage-2 or +high-window cap, and no other range-key cap is based at a frame. -/ +private def W1CapA : capability.Cap → Prop + | .Frame p _ _ dev _ _ _ _ => dev = false ∧ W1FrPtrA p.val + | .Thread p => ¬ W1FrPtrA p.val + | .CNode p _ _ _ => ¬ W1FrPtrA p.val + | .AsidPool p _ => ¬ W1FrPtrA p.val + | .SchedContext p _ => ¬ W1FrPtrA p.val + | .PageTable p _ _ _ => p ∈ w1Tb ∧ ¬ W1FrPtrA p.val + | .Pml4 p _ _ => p ∈ w1Tb ∧ ¬ W1FrPtrA p.val + | .Zombie .. => False + | .Ept .. => False + | .HighVSpace .. => False + | c => ¬ RngKeyA c + +private theorem notRk_physA {c : capability.Cap} (h : objects.cap_is_physical c = ok false) : ¬ RngKeyA c := by + intro r + rw [physA_eq] at h + have := r.1 + rw [okinj h] at this + exact Bool.false_ne_true this + +private theorem w1CapA_nullA : W1CapA .Null := notRk_physA (by simp [objects.cap_is_physical]) + +private theorem w1CapA_frA {p v : Nat} (h : p < 2 ^ 64) (hp : W1FrPtrA p) : W1CapA (frC p v) := + ⟨rfl, by rw [bwv_val h]; exact hp⟩ + +private theorem w1CapA_capOfA (nc i : Nat) : W1CapA (capOf nc i) := by + unfold capOf + by_cases h1 : i < 16 + · rw [slotC_lo h1] + interval_cases i + all_goals dsimp only [fixC] + all_goals norm_num only [if_true, if_false] + all_goals first + | exact w1CapA_nullA + | exact w1CapA_frA (by norm_num) (by unfold W1FrPtrA; norm_num) + | exact notRk_physA (by simp [objects.cap_is_physical]) + | (show ¬ W1FrPtrA _; unfold W1FrPtrA; simp only [vRT, vRR, vPL, vSC]; norm_num) + | (refine ⟨?_, ?_⟩ + · show (bwv VSb).val = VSb ∨ _; exact Or.inl vVS + · unfold W1FrPtrA; simp only [vVS]; norm_num) + by_cases h2 : i < 16 + nc + · rw [slotC_sch (by omega) h2] + exact notRk_physA (by simp [objects.cap_is_physical]) + rw [slotC_hi (by omega)] + generalize i - (16 + nc) = k + unfold tailC + split_ifs + · exact ⟨Or.inr (Or.inl vL1), by unfold W1FrPtrA; simp only [vL1]; norm_num⟩ + · exact ⟨Or.inr (Or.inr (Or.inl vL2)), by unfold W1FrPtrA; simp only [vL2]; norm_num⟩ + · exact ⟨Or.inr (Or.inr (Or.inr vL3)), by unfold W1FrPtrA; simp only [vL3]; norm_num⟩ + · exact w1CapA_frA (by omega) (Or.inl (by omega)) + · exact w1CapA_frA (by norm_num) (by unfold W1FrPtrA; norm_num) + · exact fun r => by have := r.2.1; simp [isUtA] at this + · exact w1CapA_nullA + +/-- A cap of none of the four pointing kinds satisfies `CapKindA`. -/ +private theorem capKind_otherA {H : Heap} {c : capability.Cap} (h1 : ∀ p b x y z w, c ≠ .Endpoint p b x y z w) + (h2 : ∀ p b x y, c ≠ .Notification p b x y) (h3 : ∀ p g, c ≠ .Reply p g) + (h4 : ∀ p sz, c ≠ .SchedContext p sz) (h5 : ∀ cpu, c ≠ .SchedControl cpu) : CapKindA H c := + ⟨fun p b x y z w e => absurd e (h1 p b x y z w), fun p b x y e => absurd e (h2 p b x y), + fun p g e => absurd e (h3 p g), fun p sz e => absurd e (h4 p sz), fun cpu e => absurd e (h5 cpu)⟩ + +/-- Every root-CNode cap of `w1` has its heap object (`CapKindA`; the one pointing cap is the root SC). -/ +private theorem w1_capKindA_capOfA (kb ib : U64) (nc i : Nat) (hnc : nc ≤ Fl4SpecA64.fl4A64Platform.numNodes) : + CapKindA (w1H kb ib nc) (capOf nc i) := by + have o : ∀ c : capability.Cap, (∀ p b x y z w, c ≠ .Endpoint p b x y z w) → (∀ p b x y, c ≠ .Notification p b x y) → + (∀ p g, c ≠ .Reply p g) → (∀ p sz, c ≠ .SchedContext p sz) → (∀ cpu, c ≠ .SchedControl cpu) → + CapKindA (w1H kb ib nc) c := + fun _ => capKind_otherA + have sc : CapKindA (w1H kb ib nc) (.SchedContext (bwv SCb) 7#u64) := + ⟨fun _ _ _ _ _ _ e => (by cases e), fun _ _ _ _ e => (by cases e), fun _ _ e => (by cases e), + fun p sz e => by injection e with e1 e2; subst e1 e2; exact w1H_sc, fun _ e => (by cases e)⟩ + unfold capOf + by_cases h1 : i < 16 + · rw [slotC_lo h1] + interval_cases i + all_goals dsimp only [fixC] + all_goals norm_num only [if_true, if_false] + all_goals first + | exact sc + | (refine o _ ?_ ?_ ?_ ?_ ?_ <;> intros <;> simp [frC]) + by_cases h2 : i < 16 + nc + · rw [slotC_sch (by omega) h2] + refine ⟨fun _ _ _ _ _ _ e => (by cases e), fun _ _ _ _ e => (by cases e), fun _ _ e => (by cases e), + fun _ _ e => (by cases e), fun cpu e => ?_⟩ + cases e + rw [bwv_val (by have : i - 16 < 2 ^ 64 := lt_of_lt_of_le (by have := Fl4SpecA64.fl4A64Platform.numNodes_le; omega : i - 16 < 2 ^ 32) (by norm_num); exact this)] + omega + rw [slotC_hi (by omega)] + generalize i - (16 + nc) = k + unfold tailC + split_ifs <;> refine o _ ?_ ?_ ?_ ?_ ?_ <;> intros <;> simp [frC] + +private theorem w1CapA_frPtrA {c : capability.Cap} (W : W1CapA c) (f : isFrA c = true) : W1FrPtrA (ptrA c).val := by + cases c <;> try (simp [isFrA] at f; done) + rename_i q sz r dev m ept a v + rw [show ptrA (.Frame q sz r dev m ept a v) = q from ptrA_of rfl] + exact W.2 + +private theorem w1CapA_rkPtrA {c : capability.Cap} (W : W1CapA c) (k : RngKeyA c) : ¬ W1FrPtrA (ptrA c).val := by + cases c with + | Frame => exact absurd k.2.2 (by simp [isFrA]) + | Thread p => rw [show ptrA (.Thread p) = p from ptrA_of rfl]; exact W + | CNode p r g gs => rw [show ptrA (.CNode p r g gs) = p from ptrA_of rfl]; exact W + | AsidPool p b => rw [show ptrA (.AsidPool p b) = p from ptrA_of rfl]; exact W + | SchedContext p z => rw [show ptrA (.SchedContext p z) = p from ptrA_of rfl]; exact W + | PageTable p mp a v => rw [show ptrA (.PageTable p mp a v) = p from ptrA_of rfl]; exact W.2 + | Pml4 p mp a => rw [show ptrA (.Pml4 p mp a) = p from ptrA_of rfl]; exact W.2 + | Zombie => exact W.elim + | Ept => exact W.elim + | HighVSpace => exact W.elim + | _ => exact absurd k W + +section w1caps +variable {kb ib r now mt sl : U64} {nc : Nat} {uw : U64 → U64} + +private theorem w1CapA_osA (P : BootLay1 kb ib nc) {u : U64} (hu : u ∈ w1OS kb ib nc) : + W1CapA (capA bst (w1S kb ib nc r now mt sl uw) u) := by + rcases w1_capS P hu with h | ⟨i, -, -, h⟩ | ⟨-, h⟩ | ⟨-, h⟩ | ⟨-, h⟩ | ⟨i, -, -, -, h⟩ <;> rw [h] + · exact w1CapA_nullA + · exact w1CapA_capOfA _ _ + · exact w1CapA_capOfA _ _ + · exact w1CapA_capOfA _ _ + · exact ⟨rfl, by rw [vIP]; unfold W1FrPtrA; norm_num⟩ + · exact w1CapA_nullA + +/-- **`w1` holds no device frame cap.** -/ +theorem w1_noDevFrA (P : BootLay1 kb ib nc) {y p : U64} (hy : y ∈ w1OS kb ib nc) {size rights mapped ept asid va} + (h : capA bst (w1S kb ib nc r now mt sl uw) y = .Frame p size rights true mapped ept asid va) : False := by + have W := w1CapA_osA (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) P hy + rw [h] at W + exact Bool.noConfusion W.1 + +/-- `CapTyA` at `w1`. -/ +private theorem w1_capTyA (P : BootLay1 kb ib nc) (hnc : nc ≤ Fl4SpecA64.fl4A64Platform.numNodes) : + CapTyA bst (w1H kb ib nc) (w1OS kb ib nc) (w1S kb ib nc r now mt sl uw) := fun u hu => by + have n : CapKindA (w1H kb ib nc) .Null := + capKind_otherA (fun _ _ _ _ _ _ h => (by cases h)) (fun _ _ _ _ h => (by cases h)) (fun _ _ h => (by cases h)) + (fun _ _ h => (by cases h)) (fun _ h => (by cases h)) + rcases w1_capS P hu with h | ⟨i, -, -, h⟩ | ⟨-, h⟩ | ⟨-, h⟩ | ⟨-, h⟩ | ⟨i, -, -, -, h⟩ <;> rw [h] + · exact n + · exact w1_capKindA_capOfA _ _ _ _ hnc + · exact w1_capKindA_capOfA _ _ _ _ hnc + · exact w1_capKindA_capOfA _ _ _ _ hnc + · exact capKind_otherA (fun _ _ _ _ _ _ h => (by cases h)) (fun _ _ _ _ h => (by cases h)) (fun _ _ h => (by cases h)) + (fun _ _ h => (by cases h)) (fun _ h => (by cases h)) + · exact n + +private theorem w1_bufOffA (P : BootLay1 kb ib nc) : + BufOffA bst (w1ZA kb ib nc) (w1OS kb ib nc) (w1S kb ib nc r now mt sl uw) w1BfA := by + have bv : ∀ b, BufW w1BfA b → IPb ≤ b.val ∧ b.val < IPb + 1024 := by + rintro b ⟨bp, hbp, k, hk, rfl⟩ + obtain rfl : bp = (bwv IPb, specPtr (bwv IPb)) := hbp + dsimp only + rw [bufW_ipb hk]; omega + have kbR := P.kbR + have ibR := P.ibR + refine ⟨fun b hb => ?_, fun b hb h => ?_, fun b hb p hp o o1 _ e => ?_, fun _ _ v hv => absurd hv (Set.notMem_empty v), + fun bp hbp => ?_⟩ + · rintro ⟨y, hy, h1, h2⟩ + obtain ⟨b1, b2⟩ := bv b hb + rcases hy with (hy | ⟨i, hi, rfl⟩) | ⟨i, hi, e⟩ + · rcases bootOS_val P.w0 hy with ⟨t, c, i, -, hc, et, hi, -, ex⟩ | ⟨j, hj, -, ex⟩ <;> omega + · rw [slotRT hi] at h1; omega + · omega + · obtain ⟨b1, b2⟩ := bv b hb + change VsFootA w1Pp w1Tb ∅ kb b at h + cases h with + | asid hi hh => + rw [atAF_valA (by omega) hh] at b1 b2; omega + | pool p lo hp hl => + have e : p.val = PLb := hp + rw [peAF_valA (by omega) hl] at b1 b2; omega + | pt p i hp hi => + rcases w1Tb_val hp with e | e | e | e <;> rw [peAF_valA (by omega) hi] at b1 b2 <;> omega + | high y i hy => exact absurd hy (Set.notMem_empty y) + · obtain ⟨b1, b2⟩ := bv b hb + have pg : p ∈ w1Pp ∨ p ∈ w1Tb := by + rcases hp with hp | hp | hp | hp | ⟨_, hp, -⟩ | hp | ⟨_, hp, -⟩ | ⟨u, hu, hp⟩ + · exact Or.inl hp + · exact Or.inr hp + · exact absurd hp (Set.notMem_empty _) + · exact absurd hp (Set.notMem_empty _) + · exact absurd hp (Set.notMem_empty _) + · exact absurd hp (Set.notMem_empty _) + · exact absurd hp (Set.notMem_empty _) + · have W := w1CapA_osA (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) P hu + rcases hp with ⟨_, _, _, h⟩ | ⟨_, _, h⟩ | ⟨_, _, h⟩ | ⟨_, _, _, h, -⟩ | ⟨_, _, h, -⟩ <;> rw [h] at W + · exact Or.inr W.1 + · exact Or.inr W.1 + · exact W.elim + · exact W.elim + · exact W.elim + have pv : p.val = PLb ∨ p.val = VSb ∨ p.val = L1b ∨ p.val = L2b ∨ p.val = L3b := by + rcases pg with h | h + · exact Or.inl h + · exact Or.inr (w1Tb_val h) + have ev : (core.num.U64.wrapping_add p o).val = p.val + o.val := waV (by omega) + rw [e, ev] at b1 b2 + omega + · obtain rfl : bp = (bwv IPb, specPtr (bwv IPb)) := hbp + have hsO : sbWordA (bwv RTb) ∈ w1OS kb ib nc := by + refine Or.inl (Or.inr ⟨2#u64, by decide, ?_⟩) + unfold sbWordA slotIdx layout.tcb.SLOT_BUFFER layout.cte.SIZE + rfl + have hc : capA bst (w1S kb ib nc r now mt sl uw) (sbWordA (bwv RTb)) = rtCapA 2 := by + rw [← bufSlot_eqA, ← capAtA_eq (st := bst)] + exact rt_capA P (by decide) + have e2 : rtCapA 2 = .Frame (bwv IPb) .Small 3#u64 false false false 0#u64 (bwv 0) := by simp [rtCapA] + refine ⟨bwv RTb, w1H_rt, hsO, bwv IPb, .Small, 3#u64, false, false, 0#u64, bwv 0, 12#u64, hc.trans e2, + by simp [capability.FrameSize.bits], fun k hk => ?_⟩ + have v := bufW_ipb hk + have e12 : (12#u64 : U64).val = 12 := rfl + refine ⟨?_, by rw [v, vIP]; omega, by rw [v, vIP, e12]; omega⟩ + unfold Aligned + rw [v] + have : IPb % 8 = 0 := by decide + omega + + +/-- (record edit #8) **`bufVs` at `w1`**: the root IPC buffer is off the installed pool, its tables and the ASID +table. -/ +private theorem w1_bufVsA (P : BootLay1 kb ib nc) : + ∀ b, BufW w1BfA b → ¬ VsR (bL.ksb (w1S kb ib nc r now mt sl uw)) + (utVsPtA bst (Fl4ProofsA64.P2.BootEpB.w1Pp kb ib nc r now mt sl uw) w1Tb w1Lv (w1S kb ib nc r now mt sl uw)) + b := by + intro b hb h + have bv : IPb ≤ b.val ∧ b.val < IPb + 1024 := by + obtain ⟨bp, hbp, k, hk, rfl⟩ := hb + obtain rfl : bp = (bwv IPb, specPtr (bwv IPb)) := hbp + dsimp only + rw [bufW_ipb hk]; omega + obtain ⟨b1, b2⟩ := bv + have kbR := P.kbR + have ibR := P.ibR + change VsR kb _ b at h + rcases h with ⟨p, hp, o, h1, h2, e⟩ | ⟨o, o1, o2, -, e⟩ + · have pv : p.val = PLb ∨ p.val = VSb ∨ p.val = L1b ∨ p.val = L2b ∨ p.val = L3b := by + rcases utVsPtA_sub hp with h | h + · exact Or.inl (by rw [Fl4ProofsA64.P2.BootEpB.w1_pp P h]; exact vPL) + · exact Or.inr (w1Tb_val h) + have ev : (core.num.U64.wrapping_add p o).val = p.val + o.val := waV (by omega) + rw [e, ev] at b1 b2 + omega + · have ev : (core.num.U64.wrapping_add kb o).val = kb.val + o.val := waV (by omega) + rw [e, ev] at b1 b2 + omega + +end w1caps + +/-! ## `w0` -/ + +/-- **`w0`'s heap is in the kernel window** (record edit #5), from the [P] layout facts `BootCanon` and the `2^44` +bound. -/ +theorem boot_heapWinA {kb ib : U64} {nc : Nat} (P : BootLay kb ib nc) (Q : BootCanon kb ib nc) + (hk : kb.val + 788736 ≤ layout.PPTR_BASE.val + 2 ^ 44) (hi : ib.val + nc * 8192 ≤ layout.PPTR_BASE.val + 2 ^ 44) : + ∀ p k, bootH kb ib nc p = some k → + layout.PPTR_BASE.val ≤ p.val ∧ p.val + 2 ^ k.bits ≤ layout.PPTR_BASE.val + 2 ^ 44 := by + intro p k h + have := Q.kbB; have := Q.ibB + rcases bootH_pos P h with ⟨-, rfl, -, h1, h2⟩ | ⟨c, hc, ⟨e, rfl⟩ | ⟨e, rfl⟩⟩ + · rw [show (2 : Nat) ^ (HKind.other 6).bits = 64 by simp [HKind.bits]]; omega + · rw [show (2 : Nat) ^ (HKind.tcb).bits = 4096 by simp [HKind.bits]]; omega + · rw [show (2 : Nat) ^ (HKind.sc 8).bits = 256 by simp [HKind.bits]]; omega + +/-- **`w1`'s heap is in the kernel window** (record edit #5). -/ +theorem w1_heapWinA {kb ib : U64} {nc : Nat} (P : BootLay1 kb ib nc) (Q : BootCanon kb ib nc) + (hk : kb.val + 788736 ≤ layout.PPTR_BASE.val + 2 ^ 44) (hi : ib.val + nc * 8192 ≤ layout.PPTR_BASE.val + 2 ^ 44) : + ∀ p k, w1H kb ib nc p = some k → + layout.PPTR_BASE.val ≤ p.val ∧ p.val + 2 ^ k.bits ≤ layout.PPTR_BASE.val + 2 ^ 44 := by + intro p k h + have eB := Fl4ProofsA64.P0.Consts.layout.PPTR_BASE.val_eq + rcases w1H_pos P h with ⟨g, -⟩ | ⟨e, rfl⟩ | ⟨e, rfl⟩ | ⟨j, -, hj, -, e, rfl⟩ | ⟨e, rfl⟩ | ⟨e, rfl⟩ + · exact boot_heapWinA P.w0 Q hk hi p k g + · rw [show (2 : Nat) ^ (HKind.other 18).bits = 262144 by simp [HKind.bits]]; omega + · rw [show (2 : Nat) ^ (HKind.tcb).bits = 4096 by simp [HKind.bits]]; omega + · rw [show (2 : Nat) ^ (HKind.other 12).bits = 4096 by simp [HKind.bits]]; omega + · rw [show (2 : Nat) ^ (HKind.sc 7).bits = 128 by simp [HKind.bits]]; omega + · rw [show (2 : Nat) ^ (HKind.other 12).bits = 4096 by simp [HKind.bits]]; omega + +/-- (record edit #6, item 12) A root frame page of `w1` is a spec non-device small data page. -/ +theorem w1KA_frA {kb ib now mt sl : U64} {nc : Nat} {p : U64} (hp : FrPg p.val) : + w1KA kb ib nc now mt sl (specPtr p) = some (.archObj (.dataPage false .armSmallPage)) := by + have hT : p ∉ w1Tb := fun h => by have := w1Tb_val h; unfold FrPg at hp; omega + unfold w1KA + rw [fl4Ptr_specPtr, if_neg (by unfold FrPg at hp; omega), if_neg (by unfold FrPg at hp; omega), + if_neg (by unfold FrPg at hp; omega), if_neg (by unfold FrPg at hp; omega), if_neg hT, if_pos hp] + +/-- **F8 at `w0`** (vacuous: empty queues, the idle thread current, no blocked thread). -/ +theorem boot_schedValidA (kb ib : U64) (nc : Nat) (now mt : U64) : SchedValidA (bootSA kb ib nc now mt) := by + refine ⟨fun d p t h => ?_, fun _ h => absurd rfl h, fun t x c e1 e2 _ => ?_, fun t h => ?_⟩ + rotate_right + · change t ∈ ([] : List _) at h; cases h + · change t ∈ ([] : List _) at h; cases h + · change bootKA kb ib nc now mt t = _ at e1 + unfold bootKA at e1 + split_ifs at e1 <;> cases e1 + simp [BlockedTsA, idleTcbA, Fl4SpecA64.defaultTcb] at e2 + +/-- **F8 at `w1`** (vacuous: empty queues, a switch pending, no blocked thread). -/ +theorem w1_schedValidA (kb ib : U64) (nc : Nat) (now mt sl : U64) (uw : U64 → U64) : + SchedValidA (w1SUA kb ib nc now mt sl uw) := by + refine ⟨fun d p t h => ?_, fun e _ => ?_, fun t x c e1 e2 _ => ?_, fun t h => ?_⟩ + rotate_right + · change t ∈ ([] : List _) at h; cases h + · change t ∈ ([] : List _) at h; cases h + · change Fl4SpecA64.SchedulerAction.switchThread _ = .resumeCurThread at e + cases e + · change w1KA kb ib nc now mt sl t = _ at e1 + unfold w1KA at e1 + split_ifs at e1 + all_goals first + | (cases e1; simp [BlockedTsA, rootTcbA, Fl4SpecA64.defaultTcb] at e2; done) + | (cases e1; done) + | (unfold bootKA at e1 + split_ifs at e1 <;> cases e1 + simp [BlockedTsA, idleTcbA, Fl4SpecA64.defaultTcb] at e2) + +theorem boot_stage3_ex {A : Type} (AR : A → HwRootA → Prop) {a : A} (r now mt : U64) (Lv : U64 → Nat) + (hAR : AR a bootHwA) (hnc : 128 ≤ Fl4SpecA64.fl4A64Platform.numNodes) + (hnn : Fl4SpecA64.fl4A64Platform.numNodes ≤ 128) (hr : specPtr r = Fl4SpecA64.armGlobalPtPtr) (J5 J7 : JSlotA _) + (h5 : J5 (bootZA 18446603337294938112#u64 18446603338368679936#u64 128) (bootH 18446603337294938112#u64 18446603338368679936#u64 128) + (bootOS 18446603337294938112#u64 18446603338368679936#u64 128) (bootS 18446603337294938112#u64 18446603338368679936#u64 128 r now mt) + (bootSA 18446603337294938112#u64 18446603338368679936#u64 128 now mt) (bootMA 18446603338368679936#u64 128)) + (h7 : J7 (bootZA 18446603337294938112#u64 18446603338368679936#u64 128) (bootH 18446603337294938112#u64 18446603338368679936#u64 128) + (bootOS 18446603337294938112#u64 18446603338368679936#u64 128) (bootS 18446603337294938112#u64 18446603338368679936#u64 128 r now mt) + (bootSA 18446603337294938112#u64 18446603338368679936#u64 128 now mt) (bootMA 18446603338368679936#u64 128)) : + JStage3EntryA bst AR (bootZA 18446603337294938112#u64 18446603338368679936#u64 128) (bootH 18446603337294938112#u64 18446603338368679936#u64 128) + (bootOS 18446603337294938112#u64 18446603338368679936#u64 128) ∅ (bootH 18446603337294938112#u64 18446603338368679936#u64 128).scs ∅ ∅ Lv + (18446603338368679936#u64 : U64).val ((18446603338368679936#u64 : U64).val + 128 * 8192) ∅ ∅ ∅ ∅ (fun _ => []) + (bootSchedScCoreA 18446603338368679936#u64) 128 J5 J7 (bootS 18446603337294938112#u64 18446603338368679936#u64 128 r now mt) a + (bootSA 18446603337294938112#u64 18446603338368679936#u64 128 now mt) (bootMA 18446603338368679936#u64 128) := by + have hi : (18446603338368679936#u64 : U64).val = 18446603338368679936 := by simp + have hk : (18446603337294938112#u64 : U64).val = 18446603337294938112 := by simp + have P : BootLay 18446603337294938112#u64 18446603338368679936#u64 128 := bootLayC_ex + have hsc : 18446603336221208576 < (18446603338368679936#u64 : U64).val ∨ + (18446603338368679936#u64 : U64).val + 128 * 8192 ≤ 18446603336221208576 := Or.inl (by rw [hi]; omega) + have hsk : 18446603336221208576 < (18446603337294938112#u64 : U64).val ∨ + (18446603337294938112#u64 : U64).val + 788736 ≤ 18446603336221208576 := Or.inl (by rw [hk]; omega) + obtain ⟨J, SI⟩ := boot_jEntA_ex (r := r) (now := now) (mt := mt) AR (a := a) Lv hAR + have hL := bootLayA_of P hsc + obtain ⟨SR, SC⟩ := w0_schedWitnessA (r := r) (now := now) (mt := mt) P hsc hsk hnc + have hT : ∀ t ∈ (bootZA 18446603337294938112#u64 18446603338368679936#u64 128).T, + t ∈ (bootH 18446603337294938112#u64 18446603338368679936#u64 128).tcbs := fun t ht => by rw [J.tcbs]; exact ht + have TP : TcbPlaced (bootZA 18446603337294938112#u64 18446603338368679936#u64 128).T := fun t ht => + J.sys.ent.bxi.1.choose_spec.choose_spec.tcbs t (hT t ht) + have idle : ∀ t ∈ (bootZA 18446603337294938112#u64 18446603338368679936#u64 128).T, ∃ sc, + (bootSA 18446603337294938112#u64 18446603338368679936#u64 128 now mt).kheap (specPtr t) = some (.tcb (idleTcbA sc)) ∧ + TcbRelE ∅ (bL.mem (bootS 18446603337294938112#u64 18446603338368679936#u64 128 r now mt)) t (idleTcbA sc) ∧ + TcbRel1 (bL.mem (bootS 18446603337294938112#u64 18446603338368679936#u64 128 r now mt)) t (idleTcbA sc) := by + intro t ht + obtain ⟨c, hc, W⟩ := w0_wordsA (r := r) (now := now) (mt := mt) P (hT t ht) + obtain ⟨c', hc', rfl⟩ := bootT_of hL (hT t ht) + exact ⟨_, bootKA_tcb hL hc', idle_relsA _ (TP _ ht) W⟩ + have nul : ∀ u ∈ bootOS 18446603337294938112#u64 18446603338368679936#u64 128, + capA bst (bootS 18446603337294938112#u64 18446603338368679936#u64 128 r now mt) u = .Null := Fl4ProofsA64.P1.W7.R.ST.BootWit.boot_null P + have noFree : ∀ dev x, ¬ UtFreeA bst (bootOS 18446603337294938112#u64 18446603338368679936#u64 128) + (bootS 18446603337294938112#u64 18446603338368679936#u64 128 r now mt) dev x := by + rintro dev x ⟨u, hu, q, b, fi, d, hc, -⟩ + rw [nul u hu] at hc; cases hc + have cur := w0_curTimingA (kb := 18446603337294938112#u64) (now := now) (mt := mt) hL + have hK := bootKsA_of P hsk + refine + { toJEntA := J + sr := SR + scCov := SC + scSize := scSize_of_zeroA J.sys.ent.heap J.scs (fun c hc x n hk => by + change bootKA 18446603337294938112#u64 18446603338368679936#u64 128 now mt _ = _ at hk + unfold bootKA at hk + split_ifs at hk <;> cases hk <;> rfl) + fun c hc x n hk hni => by + change bootKA 18446603337294938112#u64 18446603338368679936#u64 128 now mt _ = _ at hk + exact absurd hni (Sched.bootKA_scA hnc hk).2.2 + ep := ⟨fun _ h => absurd h (Set.notMem_empty _), fun _ h => absurd h (Set.notMem_empty _), + fun t ht => let ⟨_, hk, h, _⟩ := idle t ht; ⟨_, hk, h⟩, fun _ h => absurd h (Set.notMem_empty _)⟩ + ntfn := ⟨fun _ h => absurd h (Set.notMem_empty _), fun t ht => let ⟨_, hk, _, h⟩ := idle t ht; ⟨_, hk, h⟩⟩ + reply := ?_ + bufCover := ?_ + bufNative := J.tcbs ▸ w0_bufNativeA (r := r) (now := now) (mt := mt) P + iplaced := ?_ + ipcPlace := ⟨fun _ h => absurd h (Set.notMem_empty _), fun _ h => absurd h (Set.notMem_empty _), + fun _ h => absurd h (Set.notMem_empty _), fun b ⟨_, h, _⟩ => absurd h (Set.notMem_empty _)⟩ + idleNoFault := w0_idleNoFaultA hL hnn + mcp := w0_mcpRelA hL P hnc + yieldRel := w0_yieldRelA hL P + sinv := SI + setsInH := ?_ + utIpc := ⟨fun _ h => absurd h (Set.notMem_empty _), fun _ h => absurd h (Set.notMem_empty _), + fun _ h => absurd h (Set.notMem_empty _)⟩ + ipcTh := ⟨fun _ h => absurd h (Set.notMem_empty _), fun _ h => absurd h (Set.notMem_empty _), + fun _ h => absurd h (Set.notMem_empty _)⟩ + deadExt := ?_ + specFree := ⟨fun p h => (noFree _ _ h).elim, fun _ _ _ p h => (noFree _ _ h).elim, + fun _ _ p h => (noFree _ _ h).elim⟩ + utBlk := fun u hu q b fi d hc => by rw [nul u hu] at hc; cases hc + unmPt := ⟨fun u hu p asid va hc => (by rw [nul u hu] at hc; cases hc), + fun u hu p asid hc => (by rw [nul u hu] at hc; cases hc), + fun u hu p asid va hc => (by rw [nul u hu] at hc; cases hc), + fun u hu p asid hc => (by rw [nul u hu] at hc; cases hc)⟩ + tabS1 := fun q h => absurd h (Set.notMem_empty _) + tabExt := ?_ + specZero := fun _ h => (noFree _ _ h).elim + origLive := fun _ h => Bool.noConfusion h + nullOrig := fun _ _ => rfl + thPos := fun p z h => thPos_bootA h + frBase := fun u hu _ _ f => by rw [nul u hu] at f; simp [isFrA] at f + devFr := fun u hu _ _ _ _ _ _ _ h => by rw [nul u hu] at h; cases h + finCov := ⟨(boot_slotHeapCovA hL hK).cnode, (boot_slotHeapCovA hL hK).tcb⟩ + idleAll := idleAll_bootA SR (fun _ => rfl) (fun _ => rfl) (fun _ h => h) hL hnn + ipcHeapCov := by + refine ⟨fun p h => ?_, fun p h => ?_, fun p h => ?_⟩ <;> + (unfold bootH ks64H at h; split_ifs at h <;> cases h) + zombieNoVcpu := fun u hu _ _ h => by rw [nul u hu] at h; cases h + zombieTcb := fun u hu _ _ h => by rw [nul u hu] at h; cases h + bufOff := by + have bw : ∀ a, ¬ BufW (∅ : Set (U64 × Fl4SpecA64.Word)) a := fun a ⟨_, h, _⟩ => h + exact ⟨fun a h => (bw a h).elim, fun a h => (bw a h).elim, fun a h => (bw a h).elim, + fun a h => (bw a h).elim, fun _ h => h.elim⟩ + vsOk := vrOK_monoPtA (boot_vrOK P) (utVsPtA_sub.trans (by rw [Set.union_empty])) + bufAl := fun t ht y hy => by + obtain ⟨sc, hk, -⟩ := idle t ht + rw [hk] at hy + cases hy + simp [idleTcbA, Fl4SpecA64.defaultTcb] + zombieBnd := fun u hu _ _ _ h => by rw [nul u hu] at h; cases h + devUtOff := ⟨fun u hu _ _ _ h => (by rw [nul u hu] at h; cases h), + fun u hu _ _ _ h => (by rw [nul u hu] at h; cases h), + fun u hu _ _ _ h => (by rw [nul u hu] at h; cases h), + fun u hu _ _ _ h => (by rw [nul u hu] at h; cases h)⟩ + sidle := boot_sidleA hL hK + capTy := fun u hu => by + rw [nul u hu] + exact ⟨fun _ _ _ _ _ _ h => (by cases h), fun _ _ _ _ h => (by cases h), fun _ _ h => (by cases h), + fun _ _ h => (by cases h), fun _ h => (by cases h)⟩ + scCpu := by + rintro _ ⟨j, hj, rfl⟩ + have e : bootSchedScCoreA 18446603338368679936#u64 (idleSA 18446603338368679936#u64 j) = bwv j := by + unfold bootSchedScCoreA + rw [idleSA_val hL hj] + exact congrArg bwv (by omega) + rw [e, bwv_val (lt_of_lt_of_le hj (by norm_num))] + omega + scNZ := by + rintro _ ⟨j, hj, rfl⟩ e + have h0 := idleSA_val hL hj + rw [e] at h0 + have : (0#u64 : U64).val = 0 := rfl + omega + vcRun := ⟨fun t ht => by + obtain ⟨c, hc, W⟩ := w0_wordsA (r := r) (now := now) (mt := mt) P (hT t ht) + rw [W (core.num.U64.wrapping_add t 3664#u64) 3664 (by norm_num) (by norm_num) + (field_val (TP t ht) (by decide))] + exact Or.inl (by simp [tcbW]), + fun _ h => absurd h (Set.notMem_empty _), fun _ h => absurd h (Set.notMem_empty _), fun t ht => by + obtain ⟨c, hc, W⟩ := w0_wordsA (r := r) (now := now) (mt := mt) P (hT t ht) + rw [show runVcpuA t = core.num.U64.wrapping_add t 3672#u64 from rfl, + W (core.num.U64.wrapping_add t 3672#u64) 3672 (by norm_num) (by norm_num) + (field_val (TP t ht) (by decide))] + exact Or.inl (by simp [tcbW]), + fun t ht h0 => by + obtain ⟨c, hc, W⟩ := w0_wordsA (r := r) (now := now) (mt := mt) P (hT t ht) + rw [W (core.num.U64.wrapping_add t 3664#u64) 3664 (by norm_num) (by norm_num) + (field_val (TP t ht) (by decide))] at h0 + exact absurd (by simp [tcbW]) h0⟩ + heapWin := boot_heapWinA P bootWinC_ex.1 bootWinC_ex.2.1 bootWinC_ex.2.2 + coresAll := fun k hk => ⟨bwv k, cs_mem P (lt_of_lt_of_le hk hnn), + bwv_val (lt_of_lt_of_le (lt_of_lt_of_le hk hnn) (by norm_num))⟩ + ptCapIn := ptCapIn_w0A hL (bootWordsA_w0 (r := r) (now := now) (mt := mt) P hsc hsk) + symL := boot_symL P + fhOK := fun t ht => by + obtain ⟨c, hc, e, -⟩ := tw (r := r) (now := now) (mt := mt) P (hT t ht) (K := 0#u64) (k := 0) rfl (by omega) + exact Or.inl (nul _ (Or.inl ⟨t, ⟨c, hc, e⟩, layout.tcb.SLOT_FAULT_HANDLER, + by simp [layout.tcb.SLOT_FAULT_HANDLER], rfl⟩)) + zwf := fun y hy p zt n h => by + rw [nul y ((Set.ext_iff.1 (bootOS_eq hL hK) y).1 hy)] at h; cases h + schedValid := boot_schedValidA _ _ _ _ _ + utWin := fun u hu q b fi hc => by rw [nul u hu] at hc; cases hc + devWin := fun u hu => ⟨fun q b fi hc => by (rw [nul u hu] at hc; cases hc), + fun p sz r m e a v hc => by (rw [nul u hu] at hc; cases hc)⟩ + vcBack := fun v h => absurd h (Set.notMem_empty _) + runBack := fun t ht h0 => by + obtain ⟨c, hc, W⟩ := w0_wordsA (r := r) (now := now) (mt := mt) P (hT t ht) + exact absurd ((W (runVcpuA t) 3672 (by norm_num) (by norm_num) + (field_val (TP t ht) (K := 3672#u64) (by decide))).trans (by simp [tcbW])) h0 + vcSnap := VcpuStateSnapA.empty _ _ + vcOS := fun b ⟨v, hv, _⟩ => absurd hv (Set.notMem_empty _) + userFr := fun u hu _ _ _ _ _ _ _ h => by rw [nul u hu] at h; cases h + frTh := fun u hu _ _ _ _ _ _ _ _ h _ => by rw [nul u hu] at h; cases h + bfSpec := fun _ h => absurd h (Set.notMem_empty _) + vcSym1 := fun t ht h0 => by + obtain ⟨c, hc, W⟩ := w0_wordsA (r := r) (now := now) (mt := mt) P (hT t ht) + exact absurd ((W (core.num.U64.wrapping_add t 3664#u64) 3664 (by norm_num) (by norm_num) + (field_val (TP t ht) (by decide))).trans (by simp [tcbW])) h0 + runVcpu := fun t ht h0 => by + obtain ⟨c, hc, W⟩ := w0_wordsA (r := r) (now := now) (mt := mt) P (hT t ht) + exact absurd ((W (runVcpuA t) 3672 (by norm_num) (by norm_num) + (field_val (TP t ht) (K := 3672#u64) (by decide))).trans (by simp [tcbW])) h0 + vcSym2 := fun _ h => absurd h (Set.notMem_empty _) + blkQ := ⟨fun t ht x hx e hb => by + obtain ⟨sc, hk', -⟩ := idle t ht + rw [hk'] at hx + cases hx + simp [idleTcbA, Fl4SpecA64.epBlocked] at hb, + fun t ht x hx p hb => by + obtain ⟨sc, hk', -⟩ := idle t ht + rw [hk'] at hx + cases hx + simp [idleTcbA] at hb⟩ + curVcpuNone := rfl + ipcSorted := ⟨fun _ ⟨_, h, _⟩ => absurd h (Set.notMem_empty _), fun _ ⟨_, h, _⟩ => absurd h (Set.notMem_empty _)⟩ + activeReplyScs := boot_actRepA hL hK + domOK := Sched.boot_ndOKA (r := r) (now := now) (mt := mt) hr (by rw [hk]; norm_num) (by rw [hk]) + (le_antisymm hnc hnn) (le_refl _) + ipiZero := Sched.boot_ipiZeroA (r := r) (now := now) (mt := mt) (by rw [hk]; norm_num) (by norm_num) + bufVs := fun _ ⟨_, h, _⟩ => absurd h (Set.notMem_empty _) + j5 := h5 + j7 := h7 + sact := boot_sactA hL hK + curInv := cur.1 + offOK := cur.2.1 + offReady := cur.2.2.1 + actIdle := cur.2.2.2 + curScNotIdle := fun h => absurd rfl h + validTime := BitVec.le_refl _ } + · refine ⟨fun c hc => ?_, fun _ _ _ _ _ h => absurd h List.not_mem_nil, + fun _ h => absurd h (Set.notMem_empty _), fun _ h => absurd h (Set.notMem_empty _)⟩ + obtain ⟨x, n, hk, -⟩ := SR.scr.sc c hc + refine ⟨⟨x, n, hk, ?_⟩, fun _ h => absurd h List.not_mem_nil, List.nodup_nil, + fun i hi => absurd hi (Nat.not_lt_zero _)⟩ + change bootKA 18446603337294938112#u64 18446603338368679936#u64 128 now mt _ = _ at hk + unfold bootKA at hk + split_ifs at hk <;> cases hk <;> rfl + · intro t ht x b sb w hk hb _ + obtain ⟨sc, hk', -⟩ := idle t ht + rw [hk'] at hk; cases hk + cases hb + · have e : ∀ x : U64, x ∈ (∅ : Set U64) → False := fun _ h => h + have bw : ∀ a, ¬ BufW (∅ : Set (U64 × Fl4SpecA64.Word)) a := fun a ⟨_, h, _⟩ => h + exact ⟨⟨TP, fun _ h => (e _ h).elim, fun _ h => (e _ h).elim⟩, + ⟨TP, fun _ h => (e _ h).elim, fun _ h => (e _ h).elim, fun _ h => (e _ h).elim, fun _ h => (e _ h).elim, + fun _ h => (e _ h).elim, fun a h => (bw a h).elim, fun a h => (bw a h).elim, fun a h => (bw a h).elim, + fun a h => (bw a h).elim, fun _ h => absurd h (Set.notMem_empty _)⟩, + fun _ h => (e _ h).elim, fun _ h => (e _ h).elim, fun _ h => (e _ h).elim⟩ + · refine ⟨fun p h => ?_, fun p h => ?_⟩ + · rcases h with ((h | h) | h) | h <;> exact absurd h (Set.notMem_empty _) + · rcases h with h | h <;> exact absurd h (Set.notMem_empty _) + · refine ⟨fun v h => absurd h (Set.notMem_empty _), fun _ h => absurd h (Set.notMem_empty _), fun i p h => ?_, + fun t ht _ => ?_, fun v h => absurd h (Set.notMem_empty _), fun _ h => absurd h (Set.notMem_empty _)⟩ + · simp [bootSA, Fl4SpecA64.initASt] at h + cases h + · obtain ⟨c, hc, W⟩ := w0_wordsA (r := r) (now := now) (mt := mt) P (hT t ht) + rw [W (core.num.U64.wrapping_add t 3664#u64) 3664 (by norm_num) (by norm_num) + (field_val (TP t ht) (by decide))] + simp [tcbW] + · refine ⟨fun q h => ?_, fun p h => ?_⟩ + · rcases h with (h | ⟨_, h, -⟩) | ⟨_, h, -⟩ <;> exact absurd h (Set.notMem_empty _) + · rcases h with h | h | h <;> exact absurd h (Set.notMem_empty _) + +/-! ## `w1` -/ + +/-- **`w1_stage3_ex` at any core count `nc`** equal to the platform's (`hnc` / `hnn`), over any layout that is +`BootLay1`, canonical (`BootCanon`), inside the `2^44` kernel window and off the global user VSpace page. -/ +theorem w1_stage3 {kb ib : U64} {nc : Nat} (P : BootLay1 kb ib nc) (Q : BootCanon kb ib nc) + (hkw : kb.val + 788736 ≤ layout.PPTR_BASE.val + 2 ^ 44) (hiw : ib.val + nc * 8192 ≤ layout.PPTR_BASE.val + 2 ^ 44) + (hsc : 18446603336221208576 < ib.val ∨ ib.val + nc * 8192 ≤ 18446603336221208576) + (hsk : 18446603336221208576 < kb.val ∨ kb.val + 788736 ≤ 18446603336221208576) + {A : Type} (AR : A → HwRootA → Prop) {a : A} (r now mt sl : U64) (uw : U64 → U64) + (hAR : AR a bootHwA) (hnc : nc ≤ Fl4SpecA64.fl4A64Platform.numNodes) + (hnn : Fl4SpecA64.fl4A64Platform.numNodes ≤ nc) (hsl : Fl4SpecA64.MIN_BUDGET ≤ sl.bv) + (hr : specPtr r = Fl4SpecA64.armGlobalPtPtr) (J5 J7 : JSlotA _) + (h5 : J5 (w1ZA kb ib nc) (w1H kb ib nc) + (w1OS kb ib nc) (w1S kb ib nc r now mt sl uw) + (w1SUA kb ib nc now mt sl uw) (w1MA ib nc)) + (h7 : J7 (w1ZA kb ib nc) (w1H kb ib nc) + (w1OS kb ib nc) (w1S kb ib nc r now mt sl uw) + (w1SUA kb ib nc now mt sl uw) (w1MA ib nc)) : + JStage3EntryA bst AR (w1ZA kb ib nc) (w1H kb ib nc) + (w1OS kb ib nc) w1U (w1H kb ib nc).scs + (Fl4ProofsA64.P2.BootEpB.w1Pp kb ib nc r now mt sl uw) w1Tb w1Lv + (ib : U64).val ((ib : U64).val + nc * 8192) ∅ ∅ ∅ w1BfA (fun _ => []) + (w1SchedScCoreA ib) nc J5 J7 (w1S kb ib nc r now mt sl uw) a + (w1SUA kb ib nc now mt sl uw) (w1MA ib nc) := by + have J := JEntA_withUmA (w1_jEntA (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) (bootLayA_of P.w0 hsc) + (bootKsA_of P.w0 hsk) P (bootWordsA_w0 (r := r) (now := now) (mt := mt) P.w0 hsc hsk) AR (a := a) hAR) (userBytesA uw) + have SI := SInvXA_withUmA (w1_sinvXA (now := now) (mt := mt) (sl := sl) (bootLayA_of P.w0 hsc) + (bootKsA_of P.w0 hsk) P) (userBytesA uw) + have hL := bootLayA_of P.w0 hsc + have hK := bootKsA_of P.w0 hsk + obtain ⟨SR, SC⟩ := w1_schedWitnessA (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) P hsc hsk hnc hnn hsl + have TP : TcbPlaced (w1ZA kb ib nc).T := fun t ht => + J.sys.ent.bxi.1.choose_spec.choose_spec.tcbs t (by rw [J.tcbs]; exact ht) + have tcbs : ∀ t ∈ (w1ZA kb ib nc).T, ∃ x, + (w1SUA kb ib nc now mt sl uw).kheap (specPtr t) = some (.tcb x) ∧ + TcbRelE ∅ (bL.mem (w1S kb ib nc r now mt sl uw)) t x ∧ + TcbRel1 (bL.mem (w1S kb ib nc r now mt sl uw)) t x := by + intro t ht + have htp := TP t ht + change t ∈ (w1H _ _ _).tcbs at ht + rcases w1T P ht with h | rfl + · obtain ⟨c, hc, W⟩ := w1_wordsA (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) P h + obtain ⟨c', hc', rfl⟩ := bootT_of hL h + exact ⟨_, w1KA_idle hL P hc', idle_relsA _ htp W⟩ + · exact ⟨_, w1KA_rt, root_relsA htp (w1_rootWordsA P)⟩ + have C := w1SA_cs (now := now) (mt := mt) (sl := sl) hL hK P + have free : ∀ dev x, UtFreeA bst (w1OS kb ib nc) + (w1S kb ib nc r now mt sl uw) dev x → UTb ≤ x ∧ x < UTb + 1048576 := by + rintro dev x ⟨u, hu, q, b, fi, d, hc, -, h1, h2⟩ + obtain ⟨-, rfl, rfl, rfl, -⟩ := w1_ut P hu hc + rw [vUT] at h1 h2 + have e0 : (0#u64 : U64).val = 0 := rfl + have e20 : (20#u64 : U64).val = 20 := rfl + rw [e0] at h1; rw [e20] at h2 + exact ⟨by omega, by simpa using h2⟩ + have cur := w1_curTimingA (kb := kb) (now := now) (mt := mt) (sl := sl) hL P + have yl := w1_yieldRelA (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) hL P + have e : ∀ x : U64, x ∈ (∅ : Set U64) → False := fun _ h => h + refine + { toJEntA := J + sr := SR.withUmA (userBytesA uw) + scCov := SC + scSize := scSize_of_zeroA J.sys.ent.heap J.scs (fun c hc x n hk => by + change w1KA kb ib nc now mt sl _ = _ at hk + unfold w1KA at hk + split_ifs at hk <;> first + | (cases hk; rfl) + | (cases hk) + | (unfold bootKA at hk; split_ifs at hk <;> cases hk <;> rfl)) + fun c hc x n hk hni => by + change w1KA kb ib nc now mt sl _ = _ at hk + unfold w1KA at hk + split_ifs at hk with g1 g2 g3 + · cases hk + · cases hk + · -- the root SC: `c` is `SCb`, a 128-byte object + have hn : scNameA (w1MA ib nc) c = specPtr c := by + unfold scNameA + split_ifs with e + · exact absurd (show scNameA (w1MA ib nc) c = Fl4SpecA64.idleScPtr by + unfold scNameA; rw [if_pos e]) hni.1 + · rfl + rw [hn, fl4Ptr_specPtr] at g3 + have ec : c = bwv SCb := UScalar.eq_of_val_eq (by rw [g3, vSC]) + rw [ec] + exact Heap.scBits_eq w1H_sc + all_goals first + | (cases hk) + | exact absurd hni (w1_bootKA_idleA hnc hk) + ep := ⟨fun _ h => (e _ h).elim, fun _ h => (e _ h).elim, + fun t ht => let ⟨_, hk, h, _⟩ := tcbs t ht; ⟨_, hk, h⟩, w1_bufRelA P⟩ + ntfn := ⟨fun _ h => (e _ h).elim, fun t ht => let ⟨_, hk, _, h⟩ := tcbs t ht; ⟨_, hk, h⟩⟩ + reply := ?_ + bufCover := w1_bufCoverA hL hK P + bufNative := w1_bufNativeA P + iplaced := ?_ + ipcPlace := ⟨fun _ h => (e _ h).elim, fun _ h => (e _ h).elim, fun _ h => (e _ h).elim, ?_⟩ + idleNoFault := w1_idleNoFaultA hL P hnn + mcp := w1_mcpRelA hL P hnc + yieldRel := { tcb := yl.tcb, toSc := yl.toSc, frm := yl.frm } + sinv := SI + setsInH := ?_ + utIpc := ⟨fun _ h => absurd h (Set.notMem_empty _), fun _ h => absurd h (Set.notMem_empty _), + fun _ h => absurd h (Set.notMem_empty _)⟩ + ipcTh := ⟨fun _ h => absurd h (Set.notMem_empty _), fun _ h => absurd h (Set.notMem_empty _), + fun _ h => absurd h (Set.notMem_empty _)⟩ + deadExt := ?_ + specFree := ?_ + utBlk := ?_ + unmPt := ?_ + tabS1 := fun q hq => tabOff_w1A hL hK P hq + tabExt := ?_ + specZero := ?_ + origLive := fun x h => by + change (w1SA kb ib nc now mt sl).isOriginalCap x = true at h + rw [C.orig] at h + unfold w1OrigA at h + split_ifs at h with hx + refine ⟨bwv RRb, hx.1, Or.inl ?_⟩ + rw [hx.2] + exact w1ThA_rr (kb := kb) (ib := ib) (nc := nc) + nullOrig := fun x h => w1_nullOrigA C x h + thPos := fun p z h => thPos_w1A h + frBase := fun y hy z hz f k e => by + have Wy := w1CapA_osA (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) P hy + have Wz := w1CapA_osA (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) P hz + exact w1CapA_rkPtrA Wz k (e ▸ w1CapA_frPtrA Wy f) + devFr := fun u hu p _ _ _ _ _ _ h => by + have W := w1CapA_osA (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) P hu + rw [h] at W + exact Bool.noConfusion W.1 + finCov := ⟨(w1_slotHeapCovA hL hK).cnode, (w1_slotHeapCovA hL hK).tcb⟩ + idleAll := idleAll_bootA (SR.withUmA (userBytesA uw)) (fun _ => rfl) (fun _ => rfl) (fun _ h => h) hL hnn + ipcHeapCov := by + refine ⟨fun p h => ?_, fun p h => ?_, fun p h => ?_⟩ <;> + (unfold w1H bootH ks64H at h; split_ifs at h <;> cases h) + zombieNoVcpu := fun u hu _ _ h => by + have W := w1CapA_osA (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) P hu + rw [h] at W + exact W.elim + zombieTcb := fun u hu _ _ h => by + have W := w1CapA_osA (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) P hu + rw [h] at W + exact W.elim + bufOff := w1_bufOffA P + vsOk := vrOK_monoPtA (w1_vrOK P) (utVsPtA_sub.trans (Set.union_subset (fun p hp => Or.inr (Or.inl (by rw [Fl4ProofsA64.P2.BootEpB.w1_pp P hp]; exact vPL))) + (fun p hp => by rcases hp with h | h | h | h + exacts [Or.inl h, Or.inr (Or.inr (Or.inl h)), Or.inr (Or.inr (Or.inr (Or.inl h))), + Or.inr (Or.inr (Or.inr (Or.inr h)))]))) + bufAl := fun t ht y hy => by + change t ∈ (w1H _ _ _).tcbs at ht + rcases w1T P ht with h | rfl + · obtain ⟨c, hc, -⟩ := w1_wordsA (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) P h + obtain ⟨c', hc', rfl⟩ := bootT_of hL h + change (w1KA _ _ _ _ _ _) _ = _ at hy + rw [w1KA_idle hL P hc'] at hy + cases hy + simp [idleTcbA, Fl4SpecA64.defaultTcb] + · change (w1KA _ _ _ _ _ _) _ = _ at hy + rw [w1KA_rt] at hy + cases hy + rfl + zombieBnd := fun u hu _ _ _ h => by + have W := w1CapA_osA (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) P hu + rw [h] at W + exact W.elim + devUtOff := by + have nd : ∀ u ∈ w1OS kb ib nc, ∀ q b fi, + capA bst (w1S kb ib nc r now mt sl uw) u ≠ .Untyped q b fi true := + fun u hu _ _ _ h => Bool.noConfusion (w1_ut P hu h).2.2.2.2 + exact ⟨fun u hu q b fi h => (nd u hu q b fi h).elim, fun u hu q b fi h => (nd u hu q b fi h).elim, + fun u hu q b fi h => (nd u hu q b fi h).elim, fun u hu q b fi h => (nd u hu q b fi h).elim⟩ + sidle := (w1_sidleA (now := now) (mt := mt) (sl := sl) hL hK P).congr rfl rfl (BitVec.le_refl _) + capTy := w1_capTyA P hnc + scCpu := fun c hc => by + unfold w1SchedScCoreA + split_ifs with h + · exact Fl4SpecA64.fl4A64Platform.numNodes_pos + · rcases w1H_scs P hc with ⟨hb, -⟩ | ⟨hv, -⟩ + · obtain ⟨j, hj, hval, -⟩ := memS P.w0 hb + have e : bootSchedScCoreA ib c = bwv j := by + unfold bootSchedScCoreA + rw [hval] + exact congrArg bwv (by omega) + rw [e, bwv_val (lt_of_lt_of_le hj (le_trans P.w0.nc128 (by norm_num)))] + omega + · exact absurd (UScalar.eq_of_val_eq (hv.trans vSC.symm)) h + scNZ := fun c hc e => by + have : (0#u64 : U64).val = 0 := rfl + subst e + rcases w1H_scs P hc with ⟨hb, -⟩ | ⟨hv, -⟩ + · obtain ⟨j, -, hval, -⟩ := memS P.w0 hb + omega + · omega + vcRun := ⟨fun t ht => Or.inl (w1_tvcpuA P (by rw [J.tcbs]; exact ht) (TP t ht)), + fun _ h => (e _ h).elim, fun _ h => (e _ h).elim, + fun t ht => Or.inl (w1_trunVcpuA P (by rw [J.tcbs]; exact ht) (TP t ht)), + fun t ht h0 => absurd (w1_tvcpuA P (by rw [J.tcbs]; exact ht) (TP t ht)) h0⟩ + heapWin := w1_heapWinA P Q hkw hiw + coresAll := fun k hk => ⟨bwv k, cs_mem P.w0 (lt_of_lt_of_le hk hnn), + bwv_val (lt_of_lt_of_le (lt_of_lt_of_le hk hnn) (le_trans P.w0.nc128 (by norm_num)))⟩ + ptCapIn := ptCapIn_w1A hL hK P + symL := w1_symL P + fhOK := fun t ht => by + have ht' : t ∈ (w1H kb ib nc).tcbs := by rw [J.tcbs]; exact ht + rcases w1T P ht' with h | rfl + · obtain ⟨c, hc, e, -⟩ := tw (r := r) (now := now) (mt := mt) P.w0 h (K := 0#u64) (k := 0) rfl (by omega) + exact Or.inl (cap_idle P (Or.inl ⟨t, ⟨c, hc, e⟩, layout.tcb.SLOT_FAULT_HANDLER, + by simp [layout.tcb.SLOT_FAULT_HANDLER], rfl⟩)) + · have e3 : layout.tcb.SLOT_FAULT_HANDLER.val = 3 := by simp [layout.tcb.SLOT_FAULT_HANDLER] + have h := cap_rt (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) P + (i := layout.tcb.SLOT_FAULT_HANDLER) (by omega) + rw [e3, if_neg (by omega), if_neg (by omega), if_neg (by omega)] at h + exact Or.inl h + zwf := fun y hy p zt n h => + absurd h (w1_no_zombie P ((Set.ext_iff.1 (w1OS_eqA hL hK P) y).1 hy) p zt n) + schedValid := w1_schedValidA _ _ _ _ _ _ _ + utWin := fun u hu q b fi hc => by + obtain ⟨-, rfl, rfl, -, -⟩ := w1_ut P hu hc + have e1 := Fl4ProofsA64.P0.Consts.layout.PPTR_BASE.val_eq + have e2 : (bwv 18446605535244451840).val = 18446605535244451840 := bwv_val (by norm_num) + have e3 : (20#u64 : U64).val = 20 := rfl + rw [e1, e2, e3]; norm_num + devWin := fun u hu => ⟨fun q b fi h => Bool.noConfusion (w1_ut P hu h).2.2.2.2, + fun p sz r m e a v h _ _ => (w1_noDevFrA P hu h).elim⟩ + vcBack := fun _ h => (e _ h).elim + runBack := fun t ht h0 => absurd (w1_trunVcpuA P (by rw [J.tcbs]; exact ht) (TP t ht)) h0 + vcSnap := ⟨fun _ h => (e _ h).elim, fun _ h => (e _ h).elim⟩ + vcOS := fun b ⟨v, hv, _⟩ => (e _ hv).elim + userFr := fun y hy p size rights mapped ept asid va hc => by + obtain ⟨rfl, hF⟩ := (w1_rootCap P hy).fr _ _ _ _ _ _ _ _ hc + exact w1KA_frA hF + frTh := fun y hy p size rights x1 x2 x3 x4 bits hc hb => by + obtain ⟨rfl, hF⟩ := (w1_rootCap P hy).fr _ _ _ _ _ _ _ _ hc + rw [show 2 ^ bits.val = 4096 by simp [small_bits hb]] + exact w1ThA_fr hF + bfSpec := fun bp h => by + change bp ∈ ({(bwv 18446604435733225472, specPtr (bwv 18446604435733225472))} : + Set (U64 × Fl4SpecA64.Word)) at h + rw [Set.mem_singleton_iff] at h + subst h; rfl + vcSym1 := fun t ht h0 => absurd (w1_tvcpuA P (by rw [J.tcbs]; exact ht) (TP t ht)) h0 + runVcpu := fun t ht h0 => absurd (w1_trunVcpuA P (by rw [J.tcbs]; exact ht) (TP t ht)) h0 + vcSym2 := fun _ h => (e _ h).elim + blkQ := by + have nb : ∀ t ∈ (w1ZA kb ib nc).T, ∀ y, + (w1SUA kb ib nc now mt sl uw).kheap (specPtr t) = some (.tcb y) → + y.tcbState = .idleThreadState ∨ y.tcbState = .running := by + intro t ht y hy + change t ∈ (w1H _ _ _).tcbs at ht + rcases w1T P ht with h | rfl + · obtain ⟨c, hc, -⟩ := w1_wordsA (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) P h + obtain ⟨c', hc', rfl⟩ := bootT_of hL h + change (w1KA _ _ _ _ _ _) _ = _ at hy + rw [w1KA_idle hL P hc'] at hy + cases hy + exact Or.inl rfl + · change (w1KA _ _ _ _ _ _) _ = _ at hy + rw [w1KA_rt] at hy + cases hy + exact Or.inr rfl + refine ⟨fun t ht x hx p hb => ?_, fun t ht x hx p hb => ?_⟩ + · rcases nb t ht x hx with h | h <;> rw [h] at hb <;> simp [Fl4SpecA64.epBlocked] at hb + · rcases nb t ht x hx with h | h <;> rw [h] at hb <;> cases hb + curVcpuNone := rfl + ipcSorted := ⟨fun _ ⟨_, h, _⟩ => absurd h (Set.notMem_empty _), fun _ ⟨_, h, _⟩ => absurd h (Set.notMem_empty _)⟩ + activeReplyScs := (w1_actRepA (now := now) (mt := mt) (sl := sl) hL hK P) + domOK := { Sched.w1_ndOKA (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) hr P (by have := P.w0.kb64; omega) (le_antisymm hnc hnn) with } + ipiZero := Sched.w1_ipiZeroA (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) P P.w0.nc0 + bufVs := w1_bufVsA P + j5 := h5 + j7 := h7 + sact := (w1_sactA (now := now) (mt := mt) (sl := sl) hL hK P).congr rfl rfl rfl rfl + curInv := { tcb := cur.1.tcb, sc := cur.1.sc, active := cur.1.active, notRel := cur.1.notRel } + offOK := cur.2.1 + offReady := cur.2.2.1 + actIdle := cur.2.2.2 + curScNotIdle := fun h => absurd rfl h + validTime := BitVec.le_refl _ } + · refine ⟨fun c hc => ?_, fun _ _ _ _ _ h => absurd h List.not_mem_nil, fun _ h => (e _ h).elim, + fun _ h => (e _ h).elim⟩ + obtain ⟨x, n, hk, -⟩ := SR.scr.sc c hc + refine ⟨⟨x, n, hk, ?_⟩, fun _ h => absurd h List.not_mem_nil, List.nodup_nil, + fun i hi => absurd hi (Nat.not_lt_zero _)⟩ + change w1KA kb ib nc now mt sl _ = _ at hk + rcases w1KA_cases hL hk with h | h | h | h | ⟨-, h⟩ | h | ⟨-, ⟨_, _, _, _, h⟩ | ⟨_, h⟩ | h⟩ <;> cases h <;> rfl + · exact ⟨⟨TP, fun _ h => (e _ h).elim, fun _ h => (e _ h).elim⟩, + ⟨TP, fun _ h => (e _ h).elim, fun _ h => (e _ h).elim, fun _ h => (e _ h).elim, fun _ h => (e _ h).elim, + fun _ h => (e _ h).elim, w1Bf_align, w1Bf_tcb hL P, fun _ _ _ h => (e _ h).elim, + fun _ _ _ h => (e _ h).elim, w1Bf_inj⟩, + fun _ h => (e _ h).elim, fun _ h => (e _ h).elim, fun _ h => (e _ h).elim⟩ + · rintro b ⟨bp, hbp, k, hk', rfl⟩ + obtain rfl : bp = (bwv IPb, specPtr (bwv IPb)) := hbp + refine ⟨bwv IPb, 4096, w1ThA_ip (kb := kb) (ib := ib) (nc := nc), ?_, ?_⟩ + all_goals dsimp only; rw [bufW_ipb hk', vIP]; omega + · refine ⟨fun p h => ?_, fun p h => ?_⟩ + · rcases h with ((h | h) | h) | h + · exact w1_ptHA p (Or.inl h) + · exact w1_ptHA p (Or.inr h) + · exact (e _ h).elim + · exact (e _ h).elim + · rcases h with h | h <;> exact (e _ h).elim + · refine ⟨fun v h => (e _ h).elim, fun _ h => (e _ h).elim, fun i p h hd => ?_, fun t ht _ => ?_, + fun v h => (e _ h).elim, fun _ h => (e _ h).elim⟩ + swap + · have ht' : t ∈ (w1H kb ib nc).tcbs := by rw [J.tcbs]; exact ht + exact w1_tvcpuA P ht' (TP t ht) + change w1AsidA i = some (specPtr p) at h + unfold w1AsidA at h + split_ifs at h + have hp : p = bwv PLb := specPtr_inj (Option.some.inj h).symm + have h6 : rs 6 ∈ w1OS kb ib nc := Or.inr ⟨6, by norm_num, rfl⟩ + have hc : capA bst (w1S kb ib nc r now mt sl uw) (rs 6) = + .AsidPool (bwv PLb) 0#u64 := by + rw [cap_root P (i := 6) (by norm_num) (rs_val (by norm_num))] + rfl + exact hd (rs 6) h6 ⟨0#u64, by rw [hc, hp]⟩ + · refine ⟨fun p h => ?_, fun x y h p hp => ?_, fun x h p hp => ?_⟩ + · obtain ⟨h1, h2⟩ := free _ _ h + have ibR := P.ibR; have kbR := P.kbR + change w1KA kb ib nc now mt sl _ = none + unfold w1KA + rw [fl4Ptr_specPtr, if_neg (by omega), if_neg (by omega), if_neg (by omega), if_neg (by omega), + if_neg (by rintro (h | h | h | h) <;> omega), if_neg (by unfold FrPg; omega)] + unfold bootKA + rw [if_neg (fun h => by rw [specPtr_eq_idleSc] at h; omega), if_neg, if_neg, if_neg] + · rintro ⟨q, ⟨j, hj, rfl⟩, h⟩ + have := congrArg UScalar.val (specPtr_inj h) + rw [irqSlotA_val hK.fit hj] at this; omega + · rintro ⟨c, -, hc, h⟩ + have := congrArg UScalar.val (specPtr_inj h) + rw [idleSA_val hL hc] at this; omega + · rintro ⟨c, hc, h⟩ + have := congrArg UScalar.val (specPtr_inj h) + rw [idleTA_val' hL hc] at this; omega + · have := C.cdt x + change (w1SA kb ib nc now mt sl).cdt x = _ at h + rw [this] at h + obtain ⟨h1, -⟩ := free _ _ hp + have nrr : specPtr (bwv RRb) ≠ specPtr p := fun heq => by + have := congrArg UScalar.val (specPtr_inj heq); rw [vRR] at this; omega + have nrt : specPtr (bwv RTb) ≠ specPtr p := fun heq => by + have := congrArg UScalar.val (specPtr_inj heq); rw [vRT] at this; omega + unfold w1CdtA at h + by_cases h0 : x = w1RtSlotA 0 + · rw [if_pos h0] at h; obtain rfl := Option.some.inj h + exact ⟨by rw [h0]; exact nrt, nrr⟩ + · by_cases h1' : x = w1RtSlotA 1 + · rw [if_neg h0, if_pos h1'] at h; obtain rfl := Option.some.inj h + exact ⟨by rw [h1']; exact nrt, nrr⟩ + · by_cases h2 : x = w1RtSlotA 2 + · rw [if_neg h0, if_neg h1', if_pos h2] at h; obtain rfl := Option.some.inj h + exact ⟨by rw [h2]; exact nrt, nrr⟩ + · rw [if_neg h0, if_neg h1', if_neg h2] at h; cases h + · obtain ⟨h1, -⟩ := free _ _ hp + change (w1SA kb ib nc now mt sl).isOriginalCap x = true at h + rw [C.orig] at h + unfold w1OrigA at h + split_ifs at h with hx + rw [hx.1] + intro heq + have := congrArg UScalar.val (specPtr_inj heq) + rw [vRR] at this; omega + · intro u hu q b fi d hc + obtain ⟨-, -, rfl, -⟩ := w1_ut P hu hc + decide + · have no : ∀ u ∈ w1OS kb ib nc, + NoUnmCapA (capA bst (w1S kb ib nc r now mt sl uw) u) := by + intro u hu + rcases w1_capS P hu with h | ⟨i, -, -, h⟩ | ⟨-, h⟩ | ⟨-, h⟩ | ⟨-, h⟩ | ⟨i, -, -, -, h⟩ <;> rw [h] + · nounm + · exact capOf_noUnmA _ _ + · exact capOf_noUnmA _ _ + · exact capOf_noUnmA _ _ + · nounm + · nounm + exact ⟨fun u hu p asid va hc => ((no u hu).1 p asid va hc).elim, + fun u hu p asid hc => ((no u hu).2.1 p asid hc).elim, + fun u hu p asid va hc => ((no u hu).2.2.1 p asid va hc).elim, + fun u hu p asid hc => ((no u hu).2.2.2 p asid hc).elim⟩ + · refine ⟨fun q h => ?_, fun p h => ?_⟩ + · rcases h with (h | ⟨_, h, -⟩) | ⟨_, h, -⟩ <;> exact (e _ h).elim + · rcases h with h | h | h <;> exact (e _ h).elim + · intro a h + obtain ⟨h1, h2⟩ := free _ _ h + exact userBytesA_ut uw h1 h2 + +theorem w1_stage3_ex {A : Type} (AR : A → HwRootA → Prop) {a : A} (r now mt sl : U64) (uw : U64 → U64) + (hAR : AR a bootHwA) (hnc : 128 ≤ Fl4SpecA64.fl4A64Platform.numNodes) + (hnn : Fl4SpecA64.fl4A64Platform.numNodes ≤ 128) (hsl : Fl4SpecA64.MIN_BUDGET ≤ sl.bv) (hr : specPtr r = Fl4SpecA64.armGlobalPtPtr) (J5 J7 : JSlotA _) + (h5 : J5 (w1ZA 18446603337294938112#u64 18446603338368679936#u64 128) (w1H 18446603337294938112#u64 18446603338368679936#u64 128) + (w1OS 18446603337294938112#u64 18446603338368679936#u64 128) (w1S 18446603337294938112#u64 18446603338368679936#u64 128 r now mt sl uw) + (w1SUA 18446603337294938112#u64 18446603338368679936#u64 128 now mt sl uw) (w1MA 18446603338368679936#u64 128)) + (h7 : J7 (w1ZA 18446603337294938112#u64 18446603338368679936#u64 128) (w1H 18446603337294938112#u64 18446603338368679936#u64 128) + (w1OS 18446603337294938112#u64 18446603338368679936#u64 128) (w1S 18446603337294938112#u64 18446603338368679936#u64 128 r now mt sl uw) + (w1SUA 18446603337294938112#u64 18446603338368679936#u64 128 now mt sl uw) (w1MA 18446603338368679936#u64 128)) : + JStage3EntryA bst AR (w1ZA 18446603337294938112#u64 18446603338368679936#u64 128) (w1H 18446603337294938112#u64 18446603338368679936#u64 128) + (w1OS 18446603337294938112#u64 18446603338368679936#u64 128) w1U (w1H 18446603337294938112#u64 18446603338368679936#u64 128).scs + (Fl4ProofsA64.P2.BootEpB.w1Pp 18446603337294938112#u64 18446603338368679936#u64 128 r now mt sl uw) w1Tb w1Lv + (18446603338368679936#u64 : U64).val ((18446603338368679936#u64 : U64).val + 128 * 8192) ∅ ∅ ∅ w1BfA (fun _ => []) + (w1SchedScCoreA 18446603338368679936#u64) 128 J5 J7 (w1S 18446603337294938112#u64 18446603338368679936#u64 128 r now mt sl uw) a + (w1SUA 18446603337294938112#u64 18446603338368679936#u64 128 now mt sl uw) (w1MA 18446603338368679936#u64 128) := by + have hi : (18446603338368679936#u64 : U64).val = 18446603338368679936 := by simp + have hk : (18446603337294938112#u64 : U64).val = 18446603337294938112 := by simp + exact w1_stage3 (kb := 18446603337294938112#u64) (ib := 18446603338368679936#u64) (nc := 128) + ⟨bootLayC_ex, by rw [hk]; norm_num, by rw [hi]; norm_num⟩ bootWinC_ex.1 bootWinC_ex.2.1 bootWinC_ex.2.2 + (Or.inl (by rw [hi]; omega)) (Or.inl (by rw [hk]; omega)) AR r now mt sl uw hAR hnc hnn hsl hr J5 J7 h5 h7 + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3BootNullA.lean b/proofs/Fl4ProofsA64/P3/Stage3BootNullA.lean new file mode 100644 index 000000000..642cdde25 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3BootNullA.lean @@ -0,0 +1,71 @@ +/- +P3 (aarch64, A64-P3 Stage-3 integration; queue item 2): **the boot witnesses of the J12 fields** `nullOrig` (a null spec +slot is not original), `thPos` (every `other` object is nonempty) at `w0` / `w1`. + +* `w1_nullOrigA`: at `w1` the original caps are the non-null root CNode slots (`w1OrigA`: `cw0 ≠ 0`), and a root slot + with `cw0 ≠ 0` holds a non-null cap (`cw0_zero_of_nullA`). +* `thPos_bootA` / `thPos_w1A`: the boot typed heap has no `other` object; `w1` adds the 4 KiB IPC buffer. + +No premise; new module. +-/ +import Fl4ProofsA64.P3.Stage3A +import Fl4ProofsA64.P3.JBootW1A +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P1.W7.R.ST.BootWit + +noncomputable section + +theorem thPos_bootA {kb ib : U64} {nc : Nat} {p : U64} {z : Nat} (h : bootThA kb ib nc p = some (.other z)) : 0 < z := by + unfold bootThA at h + split_ifs at h <;> cases h + +theorem thPos_w1A {kb ib : U64} {nc : Nat} {p : U64} {z : Nat} (h : w1ThA kb ib nc p = some (.other z)) : 0 < z := by + rcases w1ThA_cases h with ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | e + · cases e + · cases e + · cases e; decide + · exact thPos_bootA e + +set_option maxHeartbeats 2000000 in +theorem fixC_nullA {i : Nat} (h : absCapA (fixC i).1 = .nullCap) : (fixC i).2.1 = 0 := by + unfold fixC at h ⊢ + split_ifs at h ⊢ <;> first | rfl | exact absurd h (by simp [absCapA, frC]) + +set_option maxHeartbeats 2000000 in +theorem tailC_nullA {k : Nat} (h : absCapA (tailC k).1 = .nullCap) : (tailC k).2.1 = 0 := by + unfold tailC at h ⊢ + split_ifs at h ⊢ <;> first | rfl | exact absurd h (by simp [absCapA, frC]) + +theorem cw0_zero_of_nullA {nc j : Nat} (h : absCapA (capOf nc j) = .nullCap) : cw0 nc j = 0 := by + unfold capOf slotC at h + unfold cw0 slotC + by_cases h1 : j < 16 + · rw [if_pos h1] at h ⊢; exact fixC_nullA h + · rw [if_neg h1] at h ⊢ + by_cases h2 : j < 16 + nc + · rw [if_pos h2] at h ⊢; exact absurd h (by simp [absCapA, frC]) + · rw [if_neg h2] at h ⊢; exact tailC_nullA h + +theorem w1_nullOrigA {kb ib : U64} {nc : Nat} {σ : Fl4SpecA64.State Unit} (C : W1CsA kb ib nc σ) : + ∀ sl, specCapAtA σ sl = some .nullCap → σ.isOriginalCap sl = false := by + rintro ⟨q, idx⟩ h + rw [C.orig] + unfold w1OrigA + by_cases hx : q = specPtr (bwv RRb) ∧ idx.length = 13 + · rw [if_pos hx] + obtain ⟨hq, hl⟩ := hx + subst hq + unfold specCapAtA at h + rw [C.cn] at h + simp only [rootCsA_wf nc, if_true] at h + unfold rootCsA at h + rw [if_pos hl] at h + have := cw0_zero_of_nullA (Option.some.inj h) + simp [this] + · rw [if_neg hx] + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3BudgetFalseA.lean b/proofs/Fl4ProofsA64/P3/Stage3BudgetFalseA.lean new file mode 100644 index 000000000..e83ec4bcf --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3BudgetFalseA.lean @@ -0,0 +1,425 @@ +/- +P3 (aarch64, A64-P3 dispatch restore step 6): **the body-end facts after an exhausted budget prefix**. + +* `fl4EndTimeslice_blA`: `end_timeslice` (either `canTimeout`) keeps the budget frame `BLA` (current thread and SC, + time, consumed time, last observation), including the timeout send to a handler. +* `fl4ChargeBudget_doneA`: from a state whose current thread is `schedulable`, `charge_budget … canTimeout` leaves no + consumed time and a pending `chooseNewThread`, with the current SC, the time and the last observation kept. +* `JSyscallA.bodyOKA_budgetFalse`: so at a syscall-entry record, `Sched.BodyOKA` holds at the state of a `false` + `budget_ok`: the guard of `BodyExitA.budget` is vacuous there. + +Named premises: those of `JStage3A.timestampA`. New module. +-/ +import Fl4ProofsA64.P3.Stage3KEntryBudgetSysA +import Fl4ProofsA64.P3.Stage3SendBudgetA +import Fl4ProofsA64.P3.Stage3RecvSendCscA +import Fl4ProofsA64.P3.SpecFrameIpcA +import Fl4ProofsA64.P3.Stage3CancelBodyA +import Fl4ProofsA64.P3.Stage3SchedActResA +import Fl4ProofsA64.P3.Stage3KEntryRestartA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +/-! ## The timeout send keeps the budget frame -/ + +instance (g : Ticks → Ticks) : ScBudFn (scConsumedUpdate g) := ⟨fun _ => ⟨rfl, rfl⟩⟩ + +instance (p : CslotPtr) : KPresBInst (getCap (Z := Unit) p) := ⟨by unfold getCap; kbres⟩ + +instance (b : Bool) (t : ObjRef) : KPresBInst (lookupIpcBuffer (Z := Unit) b t) := + ⟨by unfold lookupIpcBuffer; kbres⟩ + +instance (t : ObjRef) : KPresBInst (isValidTimeoutHandler (Z := Unit) t) := + ⟨by unfold isValidTimeoutHandler; kbres⟩ + +instance (sc : ObjRef) : KPresBInst (fl4SchedContextUpdateConsumed (Z := Unit) sc) := + ⟨by unfold fl4SchedContextUpdateConsumed schedContextUpdateConsumedWith setScObjRef; kbres⟩ + +theorem setMrs_short_blA (thread : ObjRef) (buf : Option ObjRef) (msgs : List Message) + (h : msgs.length ≤ msg_registers.length) : PresR BLA (setMrs (Z := Unit) thread buf msgs) := by + intro σ r hr + obtain ⟨y1, hy1, hr⟩ := hr + have hy1' : y1 ∈ (NonDet.assertOpt (getTcb (Z := Unit) thread σ) σ).1 := hy1 + cases hg : getTcb (Z := Unit) thread σ with + | none => rw [hg] at hy1'; exact absurd hy1' (Set.notMem_empty _) + | some x => + rw [hg] at hy1' + have e1 : y1 = (x, σ) := hy1' + subst e1 + obtain ⟨y2, hy2, hr⟩ := hr + obtain rfl := mem_pureA hy2 + obtain ⟨y3, hy3, hr⟩ := hr + obtain rfl := mem_pureA hy3 + obtain ⟨y4, hy4, hr⟩ := hr + obtain ⟨ko, hko, hr4⟩ := setObject_memA hy4 + have F : BLA σ y4.2 := by + rw [hr4]; exact (BKeepA.upd (fun _ _ e => by cases e)).toBLA + obtain ⟨y5, hy5, hr⟩ := hr + obtain rfl := mem_pureA hy5 + have hd : msgs.drop msg_registers.length = [] := List.drop_eq_nil_of_le h + have hr2 : r.2 = y4.2 := by + cases buf with + | none => rw [mem_pureA hr] + | some pptr => + obtain ⟨y6, hy6, hr⟩ := hr + dsimp only at hy6 + rw [hd, zipWithMx_nil_rightA] at hy6 + obtain rfl := mem_pureA hy6 + rw [mem_pureA hr] + rw [hr2]; exact F + +theorem bl_invA {α : Type} (t : ObjRef) (m : SMonad Unit α) [hb : KPresBInst m] [hk : TKInst m] : + ∀ s, Pf t s → ∀ x ∈ (m s).1, BLA s x.2 ∧ Pf t x.2 := + fun s hp x hx => ⟨(hb.pres s x hx).toBLA, Pf.of_keep (hk.pres s x hx) hp⟩ + +theorem fl4MakeFaultMsg_timeout_blA (b : Data) (t : ObjRef) : + ∀ σ, ∀ x ∈ (fl4MakeFaultMsg (Z := Unit) (.timeout b) t σ).1, BLA σ x.2 ∧ x.1.2.length ≤ 2 := by + intro σ x hx + refine ⟨?_, (fl4MakeFaultMsg_timeout_valA b t σ x hx).2⟩ + have P : KPresB (fl4MakeFaultMsg (Z := Unit) (.timeout b) t) := by + unfold fl4MakeFaultMsg + kbres + exact (P σ x hx).toBLA + +theorem fl4DoFaultTransfer_timeout_blA (badge : Data) (sender receiver : ObjRef) (buf : Option ObjRef) : + PresRC BLA (Pf sender) (fl4DoFaultTransfer (Z := Unit) badge sender receiver buf) := by + intro s hs x hx + obtain ⟨tcb, hk, b, hb⟩ := hs + unfold fl4DoFaultTransfer at hx + obtain ⟨y1, hy1, hx1⟩ := hx + have e := threadGet_valA Tcb.tcbFault hk hy1 + subst e + simp only [hb] at hx1 + have P : PresR BLA (do + let f ← (pure (Fault.timeout b) : SMonad Unit Fault) + let (label, msg) ← fl4MakeFaultMsg f sender + let sent ← setMrs receiver buf msg + setMessageInfo receiver ⟨sent, 0, 0, label⟩ + asUser receiver <| setRegister badge_register badge) := by + refine PresR.bind_pure _ _ ?_ + refine PresR.bind_val (Q := fun lm : Data × List Data => lm.2.length ≤ 2) (fl4MakeFaultMsg_timeout_blA b sender) + (fun lm hlm => ?_) + obtain ⟨label, msg⟩ := lm + dsimp only at hlm ⊢ + refine PresR.bind (setMrs_short_blA _ _ _ (by rw [msg_registers_length_A]; omega)) fun _ => ?_ + exact PresR.blA (by unfold setMessageInfo; kbres) + exact P s x hx1 + +theorem fl4DoIpcTransfer_timeout_blA (sender : ObjRef) (ep : Option ObjRef) (badge : Badge) (grant : Bool) + (receiver : ObjRef) : + PresRC BLA (Pf sender) (fl4DoIpcTransfer (Z := Unit) sender ep badge grant receiver) := by + unfold fl4DoIpcTransfer + refine PresRC.bind_inv (bl_invA sender _) (fun rb => ?_) + intro s hs x hx + obtain ⟨tcb, hk, b, hb⟩ := hs + obtain ⟨y1, hy1, hx1⟩ := hx + have e := threadGet_valA Tcb.tcbFault hk hy1 + subst e + simp only [hb] at hx1 + exact fl4DoFaultTransfer_timeout_blA badge sender receiver rb s ⟨tcb, hk, b, hb⟩ x hx1 + +theorem fl4SendIpc_timeout_blA (block call : Bool) (badge : Badge) (cg cgr cd : Bool) (thread epptr : ObjRef) : + PresRC BLA (Pf thread) (fl4SendIpc (Z := Unit) block call badge cg cgr cd thread epptr) := by + unfold fl4SendIpc + refine PresRC.bind_inv (bl_invA thread _) (fun ep => ?_) + cases ep with + | idleEP => cases block <;> exact PresRC.of_pres (PresR.blA (by unfold sendIpcBlocked; kbres)) + | sendEP q => cases block <;> exact PresRC.of_pres (PresR.blA (by unfold sendIpcBlocked; kbres)) + | recvEP q => + cases q with + | nil => exact PresRC.of_pres (PresR.blA (by kbres)) + | cons dest q => + refine PresRC.bind_inv (bl_invA thread _) (fun _ => ?_) + refine PresRC.bind_inv (bl_invA thread _) (fun recvState => ?_) + cases recvState with + | blockedOnReceive a r d => + dsimp only + refine PresRC.bind_inv (bl_invA thread _) (fun reply => ?_) + refine PresRC.bind_last (fl4DoIpcTransfer_timeout_blA _ _ _ _ _) (fun _ => ?_) + exact PresR.blA (by kbres) + | _ => dsimp only; exact PresRC.bind_fail + +theorem fl4HandleTimeout_blA (t : ObjRef) (b : Data) : PresR BLA (fl4HandleTimeout (Z := Unit) t (.timeout b)) := by + have send : ∀ (c : Cap) (d : Bool), PresR BLA (fl4SendFaultIpc (Z := Unit) t c (.timeout b) d) := fun c d => by + unfold fl4SendFaultIpc + cases c with + | endpointCap r bd rights => + dsimp only + refine PresR.bind_est (PresR.blA (threadSet_bkA _ t)) (threadSet_fault_PfA t b) (fun _ => ?_) + refine PresRC.bind_last (fl4SendIpc_timeout_blA _ _ _ _ _ _ _ _) (fun _ => ?_) + exact PresR.blA (by kbres) + | _ => dsimp only; exact PresR.blA (by kbres) + unfold fl4HandleTimeout isValidTimeoutHandler getCap + refine PresR.bind (PresR.blA (by kbres)) fun _ => ?_ + refine PresR.bind (PresR.blA (by kbres)) fun _ => ?_ + refine PresR.bind (PresR.blA (by kbres)) fun _ => ?_ + refine PresR.bind (PresR.blA (by kbres)) fun _ => ?_ + exact PresR.bind (send _ _) fun _ => PresR.blA (by kbres) + +theorem fl4EndTimeslice_blA {canTimeout : Bool} : PresR BLA (fl4EndTimeslice (Z := Unit) canTimeout) := by + unfold fl4EndTimeslice + refine PresR.bind (PresR.gets _) fun p => ?_ + refine PresR.bind (PresR.blA (by kbres)) fun _ => ?_ + refine PresR.bind (PresR.gets _) fun ct => ?_ + refine PresR.bind (PresR.blA (by kbres)) fun ts => ?_ + dsimp only + split + all_goals first + | refine PresR.bind_pure _ _ ?_ + | refine PresR.bind (PresR.blA KPresBInst.pres) fun valid => ?_ + all_goals refine PresR.ite' ?_ (PresR.blA (by kbres)) + all_goals exact PresR.bind (PresR.blA (by kbres)) fun _ => fl4HandleTimeout_blA _ _ + +/-! ## The charge -/ + +theorem updateSchedContext_bsA (p : ObjRef) (f : SchedContext → SchedContext) : + PresR BScalA (updateSchedContext (Z := Unit) p f) := fun σ r hr => + (updateSchedContext_onlyA (p := p) f σ r hr).toBScalA + +macro "bsres" : tactic => `(tactic| + repeat' (first + | exact updateSchedContext_bsA _ _ + | exact PresR.gets _ + | exact PresR.get + | exact PresR.pure _ + | exact PresR.assert _ + | exact PresR.assertOpt _ + | exact PresR.getsThe _ + | exact PresR.fail + | refine PresR.bind_pure _ _ ?_ + | refine PresR.bind ?_ (fun _ => ?_) + | refine PresR.when ?_ + | refine PresR.unless' ?_ + | refine PresR.ite' ?_ ?_ + | refine PresR.whileLoop (fun _ => ?_) _ + | dsimp only + | split + | unfold refillResetRr + | unfold fl4RefillBudgetCheck + | unfold refillBudgetCheckWith + | unfold handleOverrunWith + | unfold chargeEntireHeadRefill + | unfold headInsufficientLoopWith + | unfold mergeNonoverlappingHeadRefill + | unfold scheduleUsed + | unfold refillAddTail + | unfold refillPopHead + | unfold mergeOverlappingRefills + | unfold refillHeadOverlappingLoop + | unfold refillFull + | unfold refillSingle + | unfold refillSize + | unfold updateRefillHd + | unfold updateRefillTl + | unfold getRefillHead + | unfold getRefillTail + | unfold getRefills + | unfold isRoundRobin + | unfold getSchedContext + | unfold getObject)) + +instance : PreO BScalA := ⟨BScalA.refl, BScalA.trans⟩ + +theorem fl4ChargeHead_bsA (p : ObjRef) (c : Ticks) : PresR BScalA (fl4ChargeHead (Z := Unit) p c) := by + unfold fl4ChargeHead + bsres + +theorem fl4ChargeHead_actKeepA (p : ObjRef) (c : Ticks) : APres (fl4ChargeHead (Z := Unit) p c) := by + unfold fl4ChargeHead refillResetRr + apres + +/-- `ActKeepA` keeps `schedulable`. -/ +theorem Sched.ActKeepA.schedulableA {σ σ' : State Unit} (K : ActKeepA σ σ') {t : ObjRef} (h : schedulable t σ = true) : + schedulable t σ' = true := by + obtain ⟨⟨x, sc, hx, hs, y, k, hy, ha⟩, nr, x', hx', hr⟩ := schedulable_actA h + obtain ⟨y', hy', ha'⟩ := K.2.2.2.2.2.2 sc y k hy + refine schedulable_of_act ⟨x, sc, (K.2.2.2.1 _ _).2 hx, hs, y', k, hy', ha'.trans ha⟩ ?_ + ⟨x', (K.2.2.2.1 _ _).2 hx', hr⟩ + rw [K.2.2.1]; exact nr + +/-- What the exit reads of a charged state. -/ +structure ChargedA (σ σ' : State Unit) : Prop where + used : σ'.consumedTime = 0 + act : σ'.schedulerAction = .chooseNewThread + csc : σ'.curSc = σ.curSc + time : σ'.curTime = σ.curTime + lmt : σ'.machineState.lastMachineTime = σ.machineState.lastMachineTime + +theorem fl4ChargeBudget_doneA {σ τ : State Unit} {c : Ticks} {canTimeout : Bool} (h : schedulable σ.curThread σ = true) + (m : ((), τ) ∈ (fl4ChargeBudget (Z := Unit) c canTimeout σ).1) : ChargedA σ τ := by + unfold fl4ChargeBudget at m + rw [nd_gets_bindSc] at m + obtain ⟨⟨⟨⟩, σ1⟩, h1, m⟩ := mem_bindA.1 m + have B1 : BScalA σ σ1 := fl4ChargeHead_bsA _ _ σ _ h1 + have K1 : ActKeepA σ σ1 := fl4ChargeHead_actKeepA _ _ σ _ h1 + obtain ⟨⟨⟨⟩, σ2⟩, h2, m⟩ := mem_bindA.1 m + have e2 : σ2 = { σ1 with consumedTime := 0 } := (Prod.mk.inj (show ((), σ2) = ((), { σ1 with consumedTime := 0 }) + from h2)).2 + rw [nd_gets_bindSc, nd_gets_bindSc] at m + have K2 : ActKeepA σ σ2 := by + refine PreO.trans K1 ?_ + rw [e2] + exact ⟨rfl, rfl, rfl, fun _ _ => Iff.rfl, rfl, rfl, fun _ y _ h => ⟨y, h, rfl⟩⟩ + have sch : schedulable σ2.curThread σ2 = true := by + rw [K2.2.1]; exact K2.schedulableA h + unfold NonDet.when at m + rw [if_pos sch] at m + obtain ⟨⟨⟨⟩, σ3⟩, h3, m⟩ := mem_bindA.1 m + obtain ⟨⟨⟨⟩, σ4⟩, h4, h5⟩ := mem_bindA.1 m + have B3 : BLA σ2 σ3 := fl4EndTimeslice_blA σ2 _ h3 + have P4 : KPresB (rescheduleRequired (Z := Unit)) := KPresBInst.pres + have B4 : BLA σ3 σ4 := (P4 σ3 _ h4).toBLA + have a4 : σ4.schedulerAction = .chooseNewThread := rescheduleRequired_chooseA h4 + have B := PreO.trans B3 B4 + have e5 : τ = { σ4 with reprogramTimer := true } := (Prod.mk.inj (show ((), τ) = ((), { σ4 with reprogramTimer := true }) + from h5)).2 + rw [e5] + refine ⟨?_, a4, ?_, ?_, ?_⟩ + · show σ4.consumedTime = 0 + rw [B.used, e2] + · show σ4.curSc = σ.curSc + rw [B.csc, e2]; exact B1.csc + · show σ4.curTime = σ.curTime + rw [B.time, e2]; exact B1.time + · show σ4.machineState.lastMachineTime = _ + rw [B.lmt, e2]; exact congrArg (·.lastMachineTime) B1.ms + +/-- **A `false` `check_budget_restart` charged**: from a state with a `schedulable` current thread and an +insufficient head. -/ +theorem fl4CheckBudgetRestart_chargedA {σ τ : State Unit} {b : Bool} + (h : schedulable σ.curThread σ = true) + (spec : fl4GetScRefillSufficient (Z := Unit) σ.curSc σ.consumedTime σ = ({(false, σ)}, False)) + (m : (b, τ) ∈ (fl4CheckBudgetRestart (Z := Unit) σ).1) : ChargedA σ τ := by + unfold fl4CheckBudgetRestart at m + obtain ⟨⟨r, σ1⟩, h1, m⟩ := mem_bindA.1 m + have c1 : r = false ∧ ChargedA σ σ1 := by + unfold fl4CheckBudget at h1 + rw [nd_gets_bindSc, nd_gets_bindSc, nd_bind_oneSc spec] at h1 + simp only [Bool.false_eq_true, ↓reduceIte] at h1 + rw [nd_gets_bindSc] at h1 + obtain ⟨⟨⟨⟩, σ2⟩, h2, h3⟩ := mem_bindA.1 h1 + have e : (r, σ1) = (false, σ2) := h3 + obtain ⟨er, es⟩ := Prod.mk.inj e + subst es + exact ⟨er, fl4ChargeBudget_doneA h h2⟩ + obtain ⟨er, C⟩ := c1 + subst er + rw [nd_gets_bindSc] at m + obtain ⟨⟨ts, σ2⟩, h2, m⟩ := mem_bindA.1 m + have P2 : KPresB (getThreadState (Z := Unit) σ1.curThread) := KPresBInst.pres + have B2 : BLA σ1 σ2 := (P2 σ1 _ h2).toBLA + have a2 : σ2.schedulerAction = .chooseNewThread := by + have P : APres (getThreadState (Z := Unit) σ1.curThread) := APresInst.pres + exact (P σ1 _ h2).1.trans C.act + obtain ⟨⟨⟨⟩, σ3⟩, h3, h4⟩ := mem_bindA.1 m + have e4 : (b, τ) = (false, σ3) := h4 + obtain ⟨-, es⟩ := Prod.mk.inj e4 + subst es + have w : BLA σ2 τ ∧ τ.schedulerAction = .chooseNewThread := by + unfold NonDet.when at h3 + split at h3 + · have P : KPresB (setThreadState (Z := Unit) σ1.curThread .restart) := KPresBInst.pres + refine ⟨(P σ2 _ h3).toBLA, ?_⟩ + unfold setThreadState setThreadStateAct at h3 + obtain ⟨⟨⟨⟩, σt⟩, ht, h3⟩ := mem_bindA.1 h3 + have at_ : σt.schedulerAction = .chooseNewThread := by + obtain ⟨y1, hy1, hs⟩ := ht + have hy1' : y1 ∈ (NonDet.assertOpt (getTcb (Z := Unit) σ1.curThread σ2) σ2).1 := hy1 + cases hg : getTcb (Z := Unit) σ1.curThread σ2 with + | none => rw [hg] at hy1'; exact absurd hy1' (Set.notMem_empty _) + | some x => + rw [hg] at hy1' + have e1 : y1 = (x, σ2) := hy1' + subst e1 + obtain ⟨ko, -, hr2⟩ := setObject_memA hs + change σt = _ at hr2 + rw [hr2]; exact a2 + rw [nd_gets_bindSc, nd_gets_bindSc, nd_gets_bindSc] at h3 + unfold NonDet.when at h3 + split at h3 + · have e : ((), τ) = ((), { σt with schedulerAction := .chooseNewThread }) := h3 + rw [(Prod.mk.inj e).2] + · have e : ((), τ) = ((), σt) := h3 + rw [(Prod.mk.inj e).2]; exact at_ + · have e : ((), τ) = ((), σ2) := h3 + rw [(Prod.mk.inj e).2] + exact ⟨PreO.refl _, a2⟩ + have B := PreO.trans B2 w.1 + exact ⟨B.used.trans C.used, w.2, B.csc.trans C.csc, B.time.trans C.time, B.lmt.trans C.lmt⟩ + +/-! ## The body end -/ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {ar : machine.Arch A} +variable {AR AR' : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {nA : U64} {s : S} {a : A} {σ τ : State Unit} {m : McState} + +/-- **The body-end facts at a `false` `budget_ok`** from the syscall-entry record (module docstring). -/ +theorem JSyscallA.bodyOKA_budgetFalse + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar nA)) s a σ m) + (hvp : ∀ b, VsR (L.ksb s) (Pp ∪ Tb) b → Aligned b ∧ ¬ PPW st s b) + {Clock : A → MachineState → Prop} (P : NowAgreeA ar Clock) (hClock : Clock a σ.machineState) + (hnd : numDomains = 1) (C : CounterStepA σ) + (hNow : fl4ReadRawCounter σ.machineState = getCurrentTime σ.machineState) + (hAr : sched.update_timestamp st ar s a ⦃ r => AR' r.2 Z.hw ⦄) + (hmb : ∀ a', Fl4MinBudgetAgreeA ar a') + (h : fl4BudgetOk (Z := Unit) σ = ({(false, τ)}, False)) : Sched.BodyOKA τ m := by + obtain ⟨r, -, σ1, e1, J1, -, -, V1, CI1, -, -, -, -⟩ := spec_existsA (J.toJStage3A.timestampA (AR' := AR') J.sact + J.noSwitch hvp P hClock hnd J.validTime C J.curInv J.offReady J.actIdle hNow hAr) + have m1 : ((), σ1) ∈ (fl4UpdateTimestamp (Z := Unit) σ).1 := by rw [e1]; exact Set.mem_singleton _ + have T : APres (fl4UpdateTimestamp (Z := Unit)) := by + unfold fl4UpdateTimestamp + exact PresR.bind (doMachineOp_apres _) fun _ => APres.modify_fix _ (fun _ => + ⟨rfl, rfl, rfl, fun _ _ => Iff.rfl, rfl, rfl, fun _ y _ h => ⟨y, h, rfl⟩⟩) + have K : ActKeepA σ σ1 := T σ _ m1 + have csc : σ1.curSc = σ.curSc := by + have e := fl4UpdateTimestamp_eqA σ + rw [e1] at e + rw [(Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst e))).2] + rfl + have sch : schedulable σ1.curThread σ1 = true := by + rw [K.2.1] + exact K.schedulableA (J.toJStage3A.curSchedulableA J.chargeEntryA J.running) + have m2 : (false, τ) ∈ (fl4CheckBudgetRestart (Z := Unit) σ1).1 := by + have h' := h + unfold fl4BudgetOk at h' + rw [nd_bind_oneSc e1] at h' + rw [h']; exact Set.mem_singleton _ + -- the head is insufficient: a sufficient one answers `true` + obtain ⟨b, -, spec, -⟩ := spec_existsA (entryBudgetDecisionA J1.sr.toSchedSRN CI1 J1.sinv.mc r.2 (hmb r.2)) + have hb : b = false := by + cases b + · rfl + have hc : fl4CheckBudget (Z := Unit) σ1 = ({(true, σ1)}, False) := by + unfold fl4CheckBudget + rw [nd_gets_bindSc, nd_gets_bindSc, nd_bind_oneSc spec, if_pos rfl, nd_gets_bindSc] + simp [Fl4Spec.NonDet.gets, Fl4Spec.NonDet.pure, isCurDomainExpired, hnd] + unfold fl4CheckBudgetRestart at m2 + rw [nd_bind_oneSc hc, nd_gets_bindSc] at m2 + obtain ⟨y, -, hy⟩ := mem_bindA.1 m2 + exact absurd (mem_bind_pureA hy) (by simp) + subst hb + have D := fl4CheckBudgetRestart_chargedA sch spec m2 + have na : ¬ τ.schedulerAction = .resumeCurThread := by rw [D.act]; intro e; cases e + refine ⟨⟨?_, fun y k _ _ g => ?_, fun e => absurd e na⟩, fun t e => ?_, fun hn => ?_, + fun y k _ _ _ hc => absurd D.used hc⟩ + · unfold ValidMachineTimeA at V1 ⊢ + rw [D.time, D.lmt]; exact V1 + · exact (g.elim (fun g => g D.used) na).elim + · rw [D.act] at e; cases e + · rw [D.csc, csc] at hn ⊢ + exact J.curScNotIdle hn + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3BufLookupA.lean b/proofs/Fl4ProofsA64/P3/Stage3BufLookupA.lean new file mode 100644 index 000000000..8651d7fb6 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3BufLookupA.lean @@ -0,0 +1,225 @@ +/- +P3 (aarch64, A64-P3 frth, SetIPCBuffer ≠ 0): **the lookup of a freshly installed IPC buffer** (the labels line's `hlk`). + +`lookup_newBufA`: at a state whose slot 2 of the TCB `t` holds a native non-device frame cap `Frame ptr size rights` +(`size.bits = ok bits`), whose IPC-buffer word `[t + 3512]` is `w`, with the spec TCB's `tcbIpcBuffer = w.bv`, and `w` +inside the frame (`ptr ≤ w < ptr + 2^bits`, `ptr` aligned to `2^bits`), FL4's `lookup_ipc_buffer` and the spec's +`ipcBufOfA` agree on the new pair: `w' = w ∧ sb = specPtr w`. The FL4 side is the three frame-size cases of the +masked-offset computation (`ptr + (w & (2^bits - 1)) = w`); the spec side is `StateRelA`'s slot-2 cap and `ipcBufOfA`. + +Plain hypotheses only (no named premise). New module. +-/ +import Fl4ProofsA64.P3.Stage3CapBufA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +local notation "wa" => + core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **The masked offset of a frame cap**, by size: the value FL4's `lookup_ipc_buffer` returns is +`ptr + (w & (2^bits - 1))`, and its `Nat` value is `(ptr + w mod 2^bits) mod 2^64`. -/ +theorem frame_offValA (sz : capability.FrameSize) (p w bits : U64) (hb : sz.bits = ok bits) : + (do + let bits ← capability.FrameSize.bits sz + let i ← lift (bits &&& 63#u64) + let i1 ← 1#u64 <<< i + let i2 ← lift (core.num.U64.wrapping_sub i1 1#u64) + let i3 ← lift (w &&& i2) + ok (core.num.U64.wrapping_add p i3)) = + ok (core.num.U64.wrapping_add p ⟨w.bv &&& BitVec.ofNat 64 (2 ^ bits.val - 1)⟩) := by + cases sz <;> simp only [capability.FrameSize.bits] at hb <;> rw [← okinj hb] <;> + simp only [capability.FrameSize.bits, lift, bind_ok] <;> + rw [Fl4ProofsA64.P1.CapRt.shl_u64_eq _ _ (by decide)] <;> simp only [bind_ok] <;> rfl + +theorem frame_bitsLtA {sz : capability.FrameSize} {bits : U64} (hb : sz.bits = ok bits) : + 12 ≤ bits.val ∧ bits.val < 64 := by + cases sz <;> simp only [capability.FrameSize.bits] at hb <;> rw [← okinj hb] <;> simp + +/-- The masked offset, as a bit-vector equation: `ptr + (w & (2^k - 1)) = w` for `w` in the aligned frame. -/ +theorem mask_addA {ptr w bits : U64} (hk : bits.val < 64) + (hr : ptr.val ≤ w.val ∧ w.val < ptr.val + 2 ^ bits.val) (hal : ptr.val % 2 ^ bits.val = 0) : + ptr.bv + (w.bv &&& BitVec.ofNat 64 (2 ^ bits.val - 1)) = w.bv := by + have hlt : 2 ^ bits.val < 2 ^ 64 := Nat.pow_lt_pow_right (by norm_num) hk + have hm : (w.bv &&& BitVec.ofNat 64 (2 ^ bits.val - 1)).toNat = w.val % 2 ^ bits.val := by + rw [BitVec.toNat_and, BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)] + exact Nat.and_two_pow_sub_one_eq_mod w.val bits.val + have hwm : w.val % 2 ^ bits.val = w.val - ptr.val := by + have h1 : w.val = ptr.val + (w.val - ptr.val) := by omega + conv_lhs => rw [h1] + rw [Nat.add_mod, hal, zero_add, Nat.mod_mod, Nat.mod_eq_of_lt (by omega)] + have hb64 : w.val < 2 ^ 64 := w.bv.isLt + apply BitVec.eq_of_toNat_eq + rw [BitVec.toNat_add, hm, hwm] + have e : ptr.val + (w.val - ptr.val) = w.val := by omega + have e2 : ptr.bv.toNat = ptr.val := rfl + rw [e2, e] + exact Nat.mod_eq_of_lt hb64 + +theorem wa_maskA {ptr w bits : U64} (hk : bits.val < 64) + (hr : ptr.val ≤ w.val ∧ w.val < ptr.val + 2 ^ bits.val) (hal : ptr.val % 2 ^ bits.val = 0) : + wa ptr ⟨w.bv &&& BitVec.ofNat 64 (2 ^ bits.val - 1)⟩ = w := by + apply UScalar.eq_of_val_eq + rw [core.num.U64.wrapping_add_val_eq] + have h := congrArg BitVec.toNat (mask_addA hk hr hal) + rw [BitVec.toNat_add] at h + have e : (⟨w.bv &&& BitVec.ofNat 64 (2 ^ bits.val - 1)⟩ : U64).val = + (w.bv &&& BitVec.ofNat 64 (2 ^ bits.val - 1)).toNat := rfl + have e2 : ptr.val = ptr.bv.toNat := rfl + have e3 : w.val = w.bv.toNat := rfl + rw [e, e2, e3] + simpa [U64.size_def, U64.numBits] using h + +theorem pageBits_frameA {sz : capability.FrameSize} {bits : U64} (hb : sz.bits = ok bits) (w : U64) : + (w.bv &&& Fl4SpecA64.mask (Fl4SpecA64.pageBitsForSize (frameSizeOfA sz)) : BitVec 64) = + w.bv &&& BitVec.ofNat 64 (2 ^ bits.val - 1) := by + cases sz <;> simp only [capability.FrameSize.bits] at hb <;> rw [← okinj hb] <;> rfl + +theorem vmCondA (r : U64) (b : Bool) + (h : Fl4SpecA64.vmReadWrite ⊆ vmRightsOfA r ∨ Fl4SpecA64.vmReadOnly ⊆ vmRightsOfA r ∧ ¬ b = true) : + r.val = 3 ∨ (r.val = 2 ∧ b = false) := by + unfold vmRightsOfA at h + by_cases h3 : r.val = 3 + · exact Or.inl h3 + by_cases h2 : r.val = 2 + · right + refine ⟨h2, ?_⟩ + rcases h with h | ⟨-, hb⟩ + · simp only [h3, h2, ↓reduceIte, Fl4SpecA64.vmReadWrite, Fl4SpecA64.vmReadOnly, Set.subset_def, + Set.mem_insert_iff, Set.mem_singleton_iff] at h + have := h Fl4SpecA64.Rights.allowWrite (Or.inr rfl) + cases this + · simpa using hb + · exfalso + simp only [h3, h2, ↓reduceIte, Fl4SpecA64.vmKernelOnly] at h + rcases h with h | ⟨h, -⟩ + · have := h (a := Fl4SpecA64.Rights.allowRead) (by simp [Fl4SpecA64.vmReadWrite]) + exact this + · have := h (a := Fl4SpecA64.Rights.allowRead) (by simp [Fl4SpecA64.vmReadOnly]) + exact this + +/-- **The kernel address of an IPC buffer** in a frame cap `ptr`/`bits` for the user VA `baddr`: FL4's +`lookup_ipc_buffer` (`msg.rs`) and the spec's `lookupIPCBuffer` both return `ptr + (baddr & (2^bits - 1))`. -/ +def bufAddrOfA (ptr baddr bits : U64) : U64 := wa ptr ⟨baddr.bv &&& BitVec.ofNat 64 (2 ^ bits.val - 1)⟩ + +/-- **The kernel address of a 1024-aligned buffer VA lies inside its frame**: for `ptr` aligned to the frame size +(12 ≤ bits < 64) and a 1024-aligned `baddr`, the address is `ptr + baddr mod 2^bits`, 1024-aligned, and the whole 1 KiB +buffer lies in `[ptr, ptr + 2^bits)`. -/ +theorem bufAddr_rangeA {ptr baddr bits : U64} (hk : 12 ≤ bits.val ∧ bits.val < 64) + (hal : ptr.val % 2 ^ bits.val = 0) (hb : baddr.val % 1024 = 0) : + (bufAddrOfA ptr baddr bits).val = ptr.val + baddr.val % 2 ^ bits.val ∧ + (bufAddrOfA ptr baddr bits).val % 1024 = 0 ∧ ptr.val ≤ (bufAddrOfA ptr baddr bits).val ∧ + (bufAddrOfA ptr baddr bits).val + 1024 ≤ ptr.val + 2 ^ bits.val := by + have hp : ptr.val < 2 ^ 64 := ptr.bv.isLt + have hd : 1024 ∣ 2 ^ bits.val := by + have e : (1024 : Nat) = 2 ^ 10 := by norm_num + rw [e]; exact Nat.pow_dvd_pow 2 (by omega) + have hlt : 2 ^ bits.val < 2 ^ 64 := Nat.pow_lt_pow_right (by norm_num) hk.2 + have hno : ptr.val + 2 ^ bits.val ≤ 2 ^ 64 := by + have hdiv := Nat.div_add_mod ptr.val (2 ^ bits.val) + rw [hal] at hdiv + have h64 : 2 ^ 64 = 2 ^ bits.val * 2 ^ (64 - bits.val) := by + rw [← pow_add]; congr 1; omega + have hq : ptr.val / 2 ^ bits.val < 2 ^ (64 - bits.val) := by + have : 2 ^ bits.val * (ptr.val / 2 ^ bits.val) < 2 ^ bits.val * 2 ^ (64 - bits.val) := by + rw [← h64]; omega + exact Nat.lt_of_mul_lt_mul_left this + have this : 2 ^ bits.val * (ptr.val / 2 ^ bits.val + 1) ≤ 2 ^ 64 := by + rw [h64]; exact Nat.mul_le_mul_left _ hq + have h2 : 2 ^ bits.val * (ptr.val / 2 ^ bits.val + 1) = 2 ^ bits.val * (ptr.val / 2 ^ bits.val) + 2 ^ bits.val := by + rw [Nat.mul_succ] + omega + have hm : (baddr.bv &&& BitVec.ofNat 64 (2 ^ bits.val - 1)).toNat = baddr.val % 2 ^ bits.val := by + rw [BitVec.toNat_and, BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)] + exact Nat.and_two_pow_sub_one_eq_mod baddr.val bits.val + have hpos : 0 < 2 ^ bits.val := Nat.two_pow_pos _ + have hoff : baddr.val % 2 ^ bits.val < 2 ^ bits.val := Nat.mod_lt _ hpos + have hval : (bufAddrOfA ptr baddr bits).val = ptr.val + baddr.val % 2 ^ bits.val := by + show (wa ptr ⟨baddr.bv &&& BitVec.ofNat 64 (2 ^ bits.val - 1)⟩).bv.toNat = _ + rw [core.num.U64.wrapping_add_bv_eq, BitVec.toNat_add] + show (ptr.val + (baddr.bv &&& BitVec.ofNat 64 (2 ^ bits.val - 1)).toNat) % 2 ^ 64 = _ + rw [hm] + exact Nat.mod_eq_of_lt (by omega) + have hbd : 1024 ∣ baddr.val := Nat.dvd_of_mod_eq_zero hb + have hpd : 1024 ∣ ptr.val := Nat.dvd_trans hd (Nat.dvd_of_mod_eq_zero hal) + have hod : 1024 ∣ baddr.val % 2 ^ bits.val := (Nat.dvd_mod_iff hd).2 hbd + obtain ⟨a, ha⟩ := hod + obtain ⟨c, hc⟩ := hd + obtain ⟨q, hq⟩ := hpd + refine ⟨hval, ?_, by omega, ?_⟩ + · rw [hval]; omega + · rw [hval]; omega + +/-- **The lookup of a freshly installed IPC buffer** (module docstring): FL4's `lookup_ipc_buffer` and the spec's +`ipcBufOfA` agree on the kernel address `bufAddrOfA ptr baddr bits` of the user VA `baddr`. -/ +theorem lookup_newBufA {th : TypedHeapA} {s : S} {σ : Fl4SpecA64.State Unit} + (hR : StateRelA th (L.mem s) σ) {t : U64} (ht : th t = some .tcb) + {x : Fl4SpecA64.Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + {ptr rights x3 x4 bits baddr : U64} {x1 : Bool} {size : capability.FrameSize} + (hcap : capA st s (sbWordA t) = .Frame ptr size rights false x1 false x3 x4) + (hbits : size.bits = ok bits) (hibw : L.mem s (ibWordA t) = baddr) (hspec : x.tcbIpcBuffer = baddr.bv) + (b : Bool) {sb : Fl4SpecA64.ObjRef} {w' : U64} (hb : ipcBufOfA b x = some sb) + (hl : msg.lookup_ipc_buffer st s b t = ok w') : w' = bufAddrOfA ptr baddr bits ∧ sb = specPtr (bufAddrOfA ptr baddr bits) := by + classical + have hk64 := (frame_bitsLtA hbits).2 + obtain ⟨x', hk', hs⟩ := hR.tcb t ht + rw [hk] at hk'; cases hk' + obtain ⟨ac, hac, hca⟩ := hs 2 (by decide) + rw [tcbCnodeMap_twoA] at hac + cases hac + obtain ⟨-, hab⟩ := hca + rw [capAtA_eq, bufSlot_eqA, hcap] at hab + replace hab : absCapA (.Frame ptr size rights false x1 false x3 x4) = x.tcbIpcframe := hab + simp only [absCapA, absFrameE, Bool.false_eq_true, ↓reduceIte] at hab + have hsp : ipcBufOfA b x = if Fl4SpecA64.vmReadWrite ⊆ vmRightsOfA rights ∨ + Fl4SpecA64.vmReadOnly ⊆ vmRightsOfA rights ∧ ¬ b = true then some (specPtr ptr + (x.tcbIpcBuffer &&& + Fl4SpecA64.mask (Fl4SpecA64.pageBitsForSize (frameSizeOfA size)))) else none := by + unfold ipcBufOfA; rw [← hab] + have hsl : obj.Tcb.slot t layout.tcb.SLOT_BUFFER = ok (sbWordA t) := by simp [obj.Tcb.slot, lift, sbWordA] + unfold msg.lookup_ipc_buffer at hl + rw [ipc_buffer_word, hsl] at hl + simp only [bind_ok] at hl + rw [capA_eq (st := st) s (sbWordA t)] at hl + simp only [bind_ok, hcap, Bool.false_eq_true, ↓reduceIte] at hl + rw [hsp] at hb + have hcond : (Fl4SpecA64.vmReadWrite ⊆ vmRightsOfA rights ∨ + Fl4SpecA64.vmReadOnly ⊆ vmRightsOfA rights ∧ ¬ b = true) := by + by_contra hn; rw [if_neg hn] at hb; cases hb + rw [if_pos hcond] at hb + have hsb : sb = specPtr ptr + (baddr.bv &&& BitVec.ofNat 64 (2 ^ bits.val - 1)) := by + have := Option.some.inj hb + rw [← this, hspec, pageBits_frameA hbits baddr] + refine ⟨?_, ?_⟩ + · rcases vmCondA rights b hcond with h3 | ⟨h2, hbf⟩ + · have h3' : rights = capability.VM_READ_WRITE := by + unfold capability.VM_READ_WRITE; exact UScalar.eq_of_val_eq (by rw [h3]; rfl) + rw [if_pos h3', hibw, frame_offValA size ptr baddr bits hbits] at hl + rw [← okinj hl]; rfl + · subst hbf + have h2' : rights = capability.VM_READ_ONLY := by + unfold capability.VM_READ_ONLY; exact UScalar.eq_of_val_eq (by rw [h2]; rfl) + have h3' : ¬ rights = capability.VM_READ_WRITE := by + intro e; rw [e] at h2; unfold capability.VM_READ_WRITE at h2; revert h2; decide + rw [if_neg h3'] at hl + simp only [Bool.false_eq_true, ↓reduceIte] at hl + rw [if_pos h2', hibw, frame_offValA size ptr baddr bits hbits] at hl + rw [← okinj hl]; rfl + · rw [hsb, specPtr_def, specPtr_def] + unfold bufAddrOfA + rw [core.num.U64.wrapping_add_bv_eq] + show ptr.bv + pptrShiftA + _ = ptr.bv + (baddr.bv &&& BitVec.ofNat 64 (2 ^ bits.val - 1)) + pptrShiftA + rw [BitVec.add_assoc, BitVec.add_comm pptrShiftA, ← BitVec.add_assoc] + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3BufNewA.lean b/proofs/Fl4ProofsA64/P3/Stage3BufNewA.lean new file mode 100644 index 000000000..e4e2029f9 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3BufNewA.lean @@ -0,0 +1,222 @@ +/- +P3 (aarch64, A64-P3 Lift 3b labels, SetIPCBuffer ≠ 0, step 2): **placement of a newly tracked IPC buffer** at the state +after the slot-2 delete (before the `cdt::insert`). + +The buffer's 128 words `bufW w k` are inserted into `Bf` (`Bf' = insert (w, specPtr w) Bf`). Their placement is not a +record field at the old `Bf`, so it is derived here from **plain hypotheses of these lemmas** (no named premise): + +* **`BufLiveA st OS s w`** (post-delete liveness): `w ≠ 0`, `w` is 1024-aligned and each word lies in a live non-device + `Frame` cap of an `OS` slot (the source cap of the insert, still there after the slot-2 delete). +* **`hag`** (post-delete agreement; delframe's `FrAgA` at the 128 words): FL4's memory and the spec's user memory agree + on each word. +* **`hth`** (typed-heap cover): the words lie in a typed `other` object of `Z.th`. No record field gives it for a live + frame (`IpcSchedPlacementA.buffer` is the tracked-buffer instance of it). + +From them: `EPlaced` / `IPlacedA` at `Bf'` (`bufNewPlaceA`, heap disjointness from `HeapOK`, `utIpc`, `FrameOK`), the +buffer clause of `IpcSchedPlacementA` (`bufNewPlaceThA`), the endpoint relation's `buf` clause (`EpStateRel.bufGrowA`), +`bufVs` at `Bf'` and the `os` / `vs` / `page` / `vcw` clauses of `BufOffA` for the new words (`bufNewOffA`), and the +assembled `BufOffA` at `Bf'` (`BufOffA.insertA`). + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3CapInsertTA +import Fl4ProofsA64.P3.Stage3VsZeroA +import Fl4ProofsA64.P3.VcpuDelFrameA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-- **Post-delete liveness of a new buffer** (module docstring): `w ≠ 0`, 1024-aligned, and every word of its 1 KiB +buffer lies in a non-device `Frame` cap of an `OS` slot. -/ +def BufLiveA (st : machine.Store S) [L : LawfulStore st] (OS : Set U64) (s : S) (w : U64) : Prop := + w ≠ 0#u64 ∧ w.val % 1024 = 0 ∧ ∀ k : U64, k.val < 128 → ∃ u ∈ OS, ∃ ptr size rights x1 x2 x3 x4 bits, + capA st s u = .Frame ptr size rights false x1 x2 x3 x4 ∧ size.bits = ok bits ∧ + ptr.val ≤ (bufW w k).val ∧ (bufW w k).val < ptr.val + 2 ^ bits.val + +theorem bufW_valLiveA {w k : U64} (h : w.val % 1024 = 0) (hk : k.val < 128) : (bufW w k).val = w.val + 8 * k.val := by + have hb : w.val < 2 ^ 64 := w.bv.isLt + exact bufW_valA (by omega) + +theorem bufW_alignedLiveA {w k : U64} (h : w.val % 1024 = 0) (hk : k.val < 128) : Aligned (bufW w k) := by + unfold Aligned + rw [bufW_valLiveA h hk] + omega + +/-- The words of a grown tracked set. -/ +theorem BufW_insertA {w : U64} {x : Fl4SpecA64.Word} {Bf : Set (U64 × Fl4SpecA64.Word)} {a : U64} : + BufW (insert (w, x) Bf) a ↔ (∃ k : U64, k.val < 128 ∧ a = bufW w k) ∨ BufW Bf a := by + constructor + · rintro ⟨bp, hbp, k, hk, e⟩ + rcases hbp with rfl | hbp + · exact Or.inl ⟨k, hk, e⟩ + · exact Or.inr ⟨bp, hbp, k, hk, e⟩ + · rintro (⟨k, hk, e⟩ | ⟨bp, hbp, k, hk, e⟩) + · exact ⟨(w, x), Or.inl rfl, k, hk, e⟩ + · exact ⟨bp, Or.inr hbp, k, hk, e⟩ + +/-- **A word of a frame object is off every other heap object.** -/ +theorem frameWord_offA {H : Heap} (hH : HeapOK H) {ptr q : U64} {bits : Nat} (hp : H ptr = some (.other bits)) + {kd : HKind} (hq : H q = some kd) (hne : ∀ z, kd ≠ .other z) {b : U64} (h1 : ptr.val ≤ b.val) + (h2 : b.val < ptr.val + 2 ^ bits) : b.val < q.val ∨ q.val + 2 ^ kd.bits ≤ b.val := by + by_cases e : q = ptr + · rw [e, hp] at hq + exact absurd (Option.some.inj hq).symm (hne _) + · have := hH.disj ptr (.other bits) q kd hp hq (Ne.symm e) + have hb : (HKind.other bits).bits = bits := rfl + rw [hb] at this + omega + +/-- The new words lie in `J7`'s user-frame set. -/ +theorem bufNewUbA {OS Ub : Set U64} {s : S} (fu : Fl4ProofsA64.P1.W7.R.ST.FrUb st Ub OS s) {w : U64} + (h : BufLiveA st OS s w) {k : U64} (hk : k.val < 128) : bufW w k ∈ Ub := by + obtain ⟨-, h1024, hl⟩ := h + obtain ⟨u, hu, ptr, size, rights, x1, x2, x3, x4, bits, hc, hb, l1, l2⟩ := hl k hk + exact fu u hu ptr size rights x1 x2 x3 x4 hc bits hb _ (bufW_alignedLiveA h1024 hk) l1 l2 + +section rec +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 : JSlotA S} {ar : machine.Arch A} {nA : U64} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} + +set_option maxHeartbeats 2000000 in +/-- **`IPlacedA` (hence `EPlaced`) at the grown set** (module docstring). -/ +theorem JStage3A.bufNewPlaceA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7A st (J7R2A st ar nA)) s a σ m) + {w : U64} (hl : BufLiveA st OS s w) : + IPlacedA N Z.T E Rp (insert (w, specPtr w) Bf) := by + obtain ⟨Pt, Ub, vr, arm, uf, fo, link, frub, ubal, vcub, hut⟩ := Vcpu.J7FrA.fr (st := st) _ _ _ _ _ _ J.j7 + have hH := J.sys.ent.heap + have hnew : ∀ k : U64, k.val < 128 → ∃ ptr bits, H ptr = some (.other bits) ∧ ptr.val ≤ (bufW w k).val ∧ + (bufW w k).val < ptr.val + 2 ^ bits := fun k hk => by + obtain ⟨u, hu, ptr, size, rights, x1, x2, x3, x4, bits, hc, hb, l1, l2⟩ := hl.2.2 k hk + exact ⟨ptr, bits.val, fo u hu ptr size rights x1 x2 x3 x4 hc bits hb, l1, l2⟩ + have P := J.iplaced + have off : ∀ {q : U64} {kd : HKind}, H q = some kd → (∀ z, kd ≠ .other z) → ∀ k : U64, k.val < 128 → + (bufW w k).val < q.val ∨ q.val + 2 ^ kd.bits ≤ (bufW w k).val := fun hq hne k hk => by + obtain ⟨ptr, bits, hp, l1, l2⟩ := hnew k hk + exact frameWord_offA hH hp hq hne l1 l2 + have bf := bfSpec_insertA J.bfSpec w + refine ⟨P.n, ⟨P.e.tcb, P.e.ep, P.e.rep, P.e.et, P.e.rt, P.e.er, fun b hb => ?_, fun b hb t ht => ?_, + fun b hb e he => ?_, fun b hb r hr => ?_, binj_of_bfSpecA bf⟩, P.ne, P.nr, fun nn hn b hb => ?_⟩ + · rcases BufW_insertA.1 hb with ⟨k, hk, rfl⟩ | hb + · exact bufW_alignedLiveA hl.2.1 hk + · exact P.e.balign b hb + · rcases BufW_insertA.1 hb with ⟨k, hk, rfl⟩ | hb + · have htH : H t = some .tcb := show t ∈ H.tcbs by rw [J.tcbs]; exact ht + simpa only [HKind.bits, Nat.reducePow] using off htH (fun _ e => by cases e) k hk + · exact P.e.bt b hb t ht + · rcases BufW_insertA.1 hb with ⟨k, hk, rfl⟩ | hb + · simpa only [HKind.bits, Nat.reducePow] using off (J.utIpc.ep e he) (fun _ e => by cases e) k hk + · exact P.e.be b hb e he + · rcases BufW_insertA.1 hb with ⟨k, hk, rfl⟩ | hb + · simpa only [HKind.bits, Nat.reducePow] using off (J.utIpc.reply r hr) (fun _ e => by cases e) k hk + · exact P.e.br b hb r hr + · rcases BufW_insertA.1 hb with ⟨k, hk, rfl⟩ | hb + · simpa only [HKind.bits, Nat.reducePow] using off (J.utIpc.ntfn nn hn) (fun _ e => by cases e) k hk + · exact P.nb nn hn b hb + +/-- **The buffer clause of `IpcSchedPlacementA` at the grown set**, from the typed-heap cover `hth` (module docstring). -/ +theorem Endp.IpcSchedPlacementA.growA {th : TypedHeapA} {N E R : Set U64} {Bf : Set (U64 × Word)} {x : Word} {w : U64} + (P : IpcSchedPlacementA th N E R Bf) + (hth : ∀ k : U64, k.val < 128 → ∃ p len, th p = some (.other len) ∧ p.val ≤ (bufW w k).val ∧ + (bufW w k).val < p.val + len) : IpcSchedPlacementA th N E R (insert (w, x) Bf) := + ⟨P.notification, P.endpoint, P.reply, fun b hb => by + rcases BufW_insertA.1 hb with ⟨k, hk, rfl⟩ | hb + · exact hth k hk + · exact P.buffer b hb⟩ + +/-- **The endpoint relation at the grown set**, from the post-delete agreement `hag` of the new words. -/ +theorem Endp.EpStateRel.bufGrowA {T E R : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {m : U64 → U64} + {σ : Fl4SpecA64.State Unit} (h : EpStateRel T E R Bf m σ) {w : U64} (hw : w.val % 1024 = 0) + (hag : ∀ k : U64, k.val < 128 → + (m (bufW w k)).bv = loadWordAt σ.machineState.underlyingMemory (specPtr (bufW w k))) : + EpStateRel T E R (insert (w, specPtr w) Bf) m σ := + ⟨h.ep, h.reply, h.tcb, fun bp hbp k hk => by + rcases hbp with rfl | hbp + · have e : bufAddr (specPtr w) k.val = specPtr (bufW w k) := bufAddr_specPtrWrapA w k hk + show (m (bufW w k)).bv = loadWordAt _ (bufAddr (specPtr w) k.val) ∧ (bufAddr (specPtr w) k.val).toNat % 8 = 0 + refine ⟨by rw [e]; exact hag k hk, ?_⟩ + have := bufW_alignedLiveA hw hk + unfold Aligned at this + rw [e, specPtr_toNat] + omega + · exact h.buf bp hbp k hk⟩ + +set_option maxHeartbeats 2000000 in +/-- **The new words are off `OS`, the VSpace words, the scheduler pages and the VCPU words** (clauses of `BufOffA`), +and off the reachable VSpace words (`bufVs`), at the state after the delete (module docstring). -/ +theorem JStage3A.bufNewOffA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7A st (J7R2A st ar nA)) s a σ m) + {w : U64} (hl : BufLiveA st OS s w) : + (∀ k : U64, k.val < 128 → ¬ InOS OS (bufW w k)) ∧ + (∀ k : U64, k.val < 128 → ¬ VsFootA Z.Pp Z.S1 Z.Y (L.ksb s) (bufW w k)) ∧ + (∀ k : U64, k.val < 128 → ∀ p, SchedPageA st Z OS s p → ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → + bufW w k ≠ wa p o) ∧ + (∀ k : U64, k.val < 128 → ∀ v ∈ Z.V, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → bufW w k ≠ wa v o) ∧ + (∀ k : U64, k.val < 128 → ¬ VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) (bufW w k)) := by + obtain ⟨Pt, Ub, vr, arm, uf, fo, link, frub, ubal, vcub, hut⟩ := Vcpu.J7FrA.fr (st := st) _ _ _ _ _ _ J.j7 + have hU : ∀ k : U64, k.val < 128 → bufW w k ∈ Ub := fun k hk => bufNewUbA frub hl hk + have sub : VsPtA Z.Pp Z.S1 Z.Y ⊆ Pt := by + rintro p (h | h | ⟨y, hy, rfl⟩) + · exact link p (by unfold SchedPageA; exact Or.inl h) + · exact link p (by unfold SchedPageA; exact Or.inr (Or.inl h)) + · exact link _ (by unfold SchedPageA; exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨y, hy, rfl⟩))))))) + have sub' := hut Pp Tb Lv J.sys.ent.vs + refine ⟨fun k hk => UbFr.offOS uf J.sys.sysl.fo _ (hU k hk), + fun k hk hv => (VrOK.monoPtA vr sub).ub _ hv.vsR (hU k hk), + fun k hk p hp o ho1 ho2 e => vr.ub _ (Or.inl ⟨p, link p hp, o, ho1, ho2, e⟩) (hU k hk), + fun k hk v hv o ho1 ho2 e => vcub _ ⟨v, hv, o, ho1, ho2, e⟩ (hU k hk), fun k hk hv => ?_⟩ + have hbPt : VsR (L.ksb s) Pt (bufW w k) := by + rcases hv with ⟨p, hp, o, ho1, ho2, e⟩ | has + · exact Or.inl ⟨p, sub' hp, o, ho1, ho2, e⟩ + · exact Or.inr has + exact vr.ub _ hbPt (hU k hk) + +/-- **`BufOffA` at the grown set** from `BufOffA` at `Bf`, the new words' clauses and the new pair's pin. -/ +theorem BufOffA.insertA {Z' : XSetsA} {OS : Set U64} {s : S} {Bf : Set (U64 × Fl4SpecA64.Word)} {w : U64} + (B : BufOffA st Z' OS s Bf) + (os : ∀ k : U64, k.val < 128 → ¬ InOS OS (bufW w k)) + (vs : ∀ k : U64, k.val < 128 → ¬ VsFootA Z'.Pp Z'.S1 Z'.Y (L.ksb s) (bufW w k)) + (page : ∀ k : U64, k.val < 128 → ∀ p, SchedPageA st Z' OS s p → ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → + bufW w k ≠ wa p o) + (vcw : ∀ k : U64, k.val < 128 → ∀ v ∈ Z'.V, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → bufW w k ≠ wa v o) + (pin : BufPinA st Z'.T OS s w) : BufOffA st Z' OS s (insert (w, specPtr w) Bf) := + ⟨fun b hb => by + rcases BufW_insertA.1 hb with ⟨k, hk, rfl⟩ | hb + · exact os k hk + · exact B.os b hb, + fun b hb => by + rcases BufW_insertA.1 hb with ⟨k, hk, rfl⟩ | hb + · exact vs k hk + · exact B.vs b hb, + fun b hb p hp o ho1 ho2 => by + rcases BufW_insertA.1 hb with ⟨k, hk, rfl⟩ | hb + · exact page k hk p hp o ho1 ho2 + · exact B.page b hb p hp o ho1 ho2, + fun b hb v hv o ho1 ho2 => by + rcases BufW_insertA.1 hb with ⟨k, hk, rfl⟩ | hb + · exact vcw k hk v hv o ho1 ho2 + · exact B.vcw b hb v hv o ho1 ho2, + fun bp hbp => by + rcases hbp with rfl | hbp + · exact pin + · exact B.pin bp hbp⟩ + +end rec + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3BufWrA.lean b/proofs/Fl4ProofsA64/P3/Stage3BufWrA.lean new file mode 100644 index 000000000..d62a23777 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3BufWrA.lean @@ -0,0 +1,165 @@ +/- +P3 (aarch64, A64-P3 Stage-3 D8/T1, contract §13.4 step 1): **`JStage3A` across a step that also writes IPC buffer +words and user memory** (frame class F0u). + +* The tracked buffer words are off the `OS` slots, the VSpace relation's words, the table pages and the VCPU words + (the record's `bufOff`, `P3/Stage3FieldsA`). Their typed slots, TCB links and kernel-state words are derived here + from `IpcSchedPlacementA.buffer` and `PlaceA`. +* **`JStage3A.specZero_of_bufA`** (D12): user memory changed only inside typed `other` objects (the buffer frames) + is kept on the free untyped (J10), by `ObjAllocA`. +* **`JStage3A.of_schedWrU`**: `of_schedWrN` with `BufW Bf` added to the write set and user memory changed inside + those objects only. +-/ +import Fl4ProofsA64.P3.Stage3SchedWrNA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- A buffer word is not a word of a typed slot. -/ +theorem JStage3A.buf_slot_offA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) {y b : U64} + (hy : IsSlotA Z.th y) (y1 : y.val ≤ b.val) (y2 : b.val < y.val + 32) : ¬ BufW Bf b := by + intro hw + have P := J.sr.place + obtain ⟨e, len, he, e1, e2⟩ := J.ipcPlace.buffer b hw + obtain ⟨p, k, hk, p1, p2, hkind⟩ : ∃ p k, Z.th p = some k ∧ p.val ≤ y.val ∧ y.val + 32 ≤ p.val + k.size ∧ + k ≠ .other len := by + obtain ⟨p, ⟨r, hr, i, hi, rfl⟩ | ⟨ht, i, hi, rfl⟩⟩ := hy + · obtain ⟨k, hk, hs, hv⟩ := slotVA_of P hr (j := i) (by rw [cnode_size]; omega) + exact ⟨p, k, hk, by omega, by omega, fun e' => by rw [e', hr] at hk; cases hk⟩ + · obtain ⟨k, hk, hs, hv⟩ := tcb_slotV P ht hi + have : i < 7 := hi + exact ⟨p, k, hk, by omega, by omega, fun e' => by rw [e', ht] at hk; cases hk⟩ + have hne : p ≠ e := fun h => hkind (by subst h; exact Option.some.inj (hk.symm.trans he)) + have := P.apart p e k (.other len) hk he hne + have hsz : ObjKindA.size (.other len) = len := rfl + omega + +/-- A buffer word is no VCPU-link word of a TCB. -/ +theorem JStage3A.buf_link_offA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) {t K : U64} (ht : t ∈ Z.T) + (hK : K = 3664#u64 ∨ K = 3672#u64) : ¬ BufW Bf (wa t K) := by + intro hw + have P := J.sr.place + obtain ⟨e, len, he, e1, e2⟩ := J.ipcPlace.buffer _ hw + have hth : Z.th t = some .tcb := (J.sr.thT t).2 ht + have hfit := (J.sr.core.tcbs t ht).fits + have hKv : K.val = 3664 ∨ K.val = 3672 := by rcases hK with rfl | rfl <;> simp + have hv : (wa t K).val = t.val + K.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have hne : t ≠ e := fun h => by + subst h + rw [hth] at he + cases he + have := P.apart t e .tcb (.other len) hth he hne + have hsz : ObjKindA.size (.other len) = len := rfl + have hsz' : ObjKindA.size .tcb = 4096 := rfl + omega + +/-- A buffer word is off the kernel-state block. -/ +theorem JStage3A.buf_ksA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) {b : U64} (h : BufW Bf b) : + b.val < (L.ksb s).val ∨ (L.ksb s).val + ksSizeA ≤ b.val := by + obtain ⟨e, len, he, e1, e2⟩ := J.ipcPlace.buffer b h + have P := J.sr.place + have := P.ks e (.other len) he (fun hn => by have := P.irqK he hn; cases this) + have hsz : ObjKindA.size (.other len) = len := rfl + omega + +/-- The buffer words are extra words off the footprint. -/ +theorem JStage3A.bufXOffA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + : XOffA (st := st) Z H OS s (BufW Bf) where + os := J.bufOff.os + slot := fun _ _ hy h1 h2 => J.buf_slot_offA hy h1 h2 + link := fun _ ht _ hK => J.buf_link_offA ht hK + irq := fun i hi h => by + have hv := irqWordA_val J.sr.ksFit hi + have hksz : ksSizeA = 788736 := rfl + have := J.buf_ksA h + omega + vs := fun b h hw => J.bufOff.vs b hw h + page := fun p hp o o1 o2 hw => J.bufOff.page _ hw p hp o o1 o2 rfl + vcw := fun v hv o ho hw => J.bufOff.vcw _ hw v hv o (by rcases ho with h | h <;> omega) (by rcases ho with h | h <;> omega) rfl + dom := fun b hd hw => by + have hksz : ksSizeA = 788736 := rfl + have := J.buf_ksA hw + unfold DomW at hd + omega + ipi := fun o ho hw => by + have hksz : ksSizeA = 788736 := rfl + obtain ⟨hv, hcl⟩ := J.sr.toSchedSRN.ipi_val ho + have := J.buf_ksA hw + omega + +/-- **(D12) J10 across a step changing user memory only inside typed `other` objects**: such an object is off every +free untyped part (`ObjAllocA`). -/ +theorem JStage3A.specZero_of_bufA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) {σ' : Fl4SpecA64.State Unit} + (hum : ∀ x, σ'.machineState.underlyingMemory x ≠ σ.machineState.underlyingMemory x → + ∃ p len, Z.th p = some (.other len) ∧ p.val ≤ (fl4Ptr x).val ∧ (fl4Ptr x).val < p.val + len) : + ∀ x : Fl4SpecA64.Word, UtFreeA st OS s (fun d => d = false) (fl4Ptr x).val → + σ'.machineState.underlyingMemory x = σ.machineState.underlyingMemory x := by + intro x hf + by_contra hne + obtain ⟨p, len, hp, h1, h2⟩ := hum x hne + obtain ⟨u, hu, q, b, fi, d, hc, -, f1, f2⟩ := hf + have hsz : ObjKindA.size (.other len) = len := rfl + have hO := J.objAlloc p len (Or.inr (Or.inr (Or.inl ⟨.other len, hp, hsz.symm⟩))) u hu q b fi d hc + omega + +set_option maxHeartbeats 1000000 in +/-- **`JStage3A` across a scheduler or IPC step that writes buffer words and user memory** (module docstring). -/ +theorem JStage3A.of_schedWrU {Rj : Set U64 → JSlotA S} {s' : S} {a' : A} {σ' : Fl4SpecA64.State Unit} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7A st Rj) s a σ m) + {Q : U64 → U64 → List U64} {Rl : U64 → List U64} {curOf curScOf : Fl4SpecA64.CoreId → Fl4SpecA64.ObjRef} + (w : WritesIn st s s' (fun b => SchedStepNWA (st := st) N E Rp Z m σ s b ∨ BufW Bf b)) + (HS : SpecSchedA σ σ') (SSz : SSzA σ σ') (Qk : QKeepA σ σ') + (hum : ∀ x, σ'.machineState.underlyingMemory x ≠ σ.machineState.underlyingMemory x → + ∃ p len, Z.th p = some (.other len) ∧ p.val ≤ (fl4Ptr x).val ∧ (fl4Ptr x).val < p.val + len) + (har : AR a' Z.hw) (hcurOf : ∀ k, k ≠ m.curCore → curOf k = Z.curOf k) + (hXvs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → ¬ BufW Bf b) (bufAl : UtBufAlA Z.T σ') + (hXvc : ∀ v ∈ Z.V, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → + ¬ BufW Bf (core.num.U64.wrapping_add v o)) + (sys : SysInvA st H Z.Cs OS U Ac Pp Tb Lv ia ie s') + (sq : SchedQueuesInv st H.tcbs Z.Cs H.omega H s' Q Rl) + (poolOff : PoolOff st H Z.Cs OS s') + (sr : SchedSRA st { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } m κ s' σ') + (scCov : ScCovA Z.C m σ') + (ep : EpStateRel Z.T E Rp Bf (L.mem s') σ') + (ntfn : NtfnStateRel N Z.T (L.mem s') σ') + (reply : ReplyStackRelA (L.mem s') Rp Z.C σ' (scNameA m) stack) + (bufCover : BufCover st Z.T Bf s' σ') + (idleNoFault : IdleNoFaultA m σ') + (mcp : McpRelA Z.T m (L.mem s') σ') + (yieldRel : YieldRelA Z.T Z.C m (L.mem s') σ') + (sinv : SInvXA σ' m n) + (sidle : SIdleA σ' m n) + (runVcpu : RunVcpuInv st Z.T s') (blkQ : BlkQueuedA Z.T E N (L.mem s') σ') + (symL : SymL st H Z.Cs s') + (schedValid : SchedValidA σ') + (j5 : J5 { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } H OS s' σ' m) + (j7 : J7A st Rj { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } H OS s' σ' m) : + JStage3A st AR { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } H OS U Ac Pp Tb Lv ia ie N E Rp + Bf stack κ n J5 (J7A st Rj) s' a' σ' m := + JStage3A.of_schedWrX (symL := symL) (schedValid := schedValid) (vcX := hXvc) J J.bufXOffA hXvs bufAl w HS SSz Qk (J.specZero_of_bufA hum) har hcurOf sys sq poolOff sr scCov ep ntfn + reply bufCover idleNoFault mcp yieldRel sinv sidle runVcpu blkQ j5 j7 + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3CancelBadgedA.lean b/proofs/Fl4ProofsA64/P3/Stage3CancelBadgedA.lean new file mode 100644 index 000000000..9ec062c86 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3CancelBadgedA.lean @@ -0,0 +1,316 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2): **CNode CancelBadgedSends at the record**. + +* **`JStage3A.cancelBadged_stepA`**: `cancel_badged_sends` at a record state refines the spec's `cancel_badged_sends`; + given the final `SysL` / `SymL` / `UserOK` / `J7A` (the decode's), `JStage3A` holds again at the new queues + (`Stage3IpcGlueA`'s `of_ipcStepA`, the write facts of `P3/DelEpRecA` at the threads queued on the endpoint). +* **`JSyscallA.cancelBadgedA`**: `decode_invocation` on a CNode cap with label CancelBadgedSends naming a slot with a + full-rights endpoint cap, at a syscall entry. + +Named premises: [P] `ArchTotal`, `MaxSpi`, `ArchPlat`, `A64FrameMasks`, `hCs`, `hmb`, `hmax`, `htu`; [S] `h1` +(`numNodes = 1`: the queued threads are local, `JStage3TA.onCore_single`). +New module. +-/ +import Fl4ProofsA64.P3.Stage3CancelBodyA +import Fl4ProofsA64.P3.Stage3CancelLoopA +import Fl4ProofsA64.P3.DelEpRecA +import Fl4ProofsA64.P3.Stage3SignalIdleA +import Fl4ProofsA64.P3.Stage3DelXSchedValidA +import Fl4ProofsA64.P3.CnodeCancelA +import Fl4ProofsA64.P3.CnodeTopA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 Fl4ProofsA64.P3.Endp +open Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Notif + +local notation "wa" => core.num.U64.wrapping_add + +noncomputable section + +private theorem mapMx_presCA {α β : Type} (f : α → SMonad Unit β) (hf : ∀ x, SPresA (f x)) : + ∀ l : List α, SPresA (NonDet.mapMx f l) + | [] => by unfold NonDet.mapMx; exact PresR.pure _ + | x :: l => by + unfold NonDet.mapMx + exact PresR.bind (hf x) fun _ => mapMx_presCA f hf l + +theorem removeAndRestartBadged_presA (t e : ObjRef) (b : Badge) : + SPresA (removeAndRestartBadgedThread (Z := Unit) t e b) := by + unfold removeAndRestartBadgedThread tcbEpDequeue restartThreadIfNoFault + spres + +/-- **The spec's badged cancel is a scheduler step.** -/ +theorem cancelBadgedSends_presA (p : ObjRef) (b : Badge) : SPresA (cancelBadgedSends (Z := Unit) p b) := by + unfold cancelBadgedSends + refine PresR.bind SPresInst.pres fun ep => ?_ + split + · exact PresR.bind (mapMx_presCA _ (fun t => removeAndRestartBadged_presA t p b) _) fun _ => SPresInst.pres + · exact PresR.pure _ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {ar : machine.Arch A} {nA : U64} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +set_option maxHeartbeats 2000000 in +/-- **`cancel_badged_sends` at the record** (module docstring). -/ +theorem JStage3A.cancelBadged_stepA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (sact : SActA σ) (noSwitch : ∀ u, σ.schedulerAction ≠ .switchThread u) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p : U64} (hp : p ∈ E) (b : U64) : + endpoint.cancel_badged_sends st s p b ⦃ s' => + ∃ σ', ((), σ') ∈ (cancelBadgedSends (Z := Unit) (specPtr p) b.bv σ).1 ∧ + BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s' ∧ RunVcpuInv st Z.T s' ∧ + (SysL st H Z.Cs OS ia ie s' → SymL st H Z.Cs s' → Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s' → + J7A st (J7R2A st ar nA) Z H OS s' σ' m → + ∃ Q Rl, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s' a σ' m) ⦄ := by + have T := J.toTA sact noSwitch + have hP := J.iplaced + have hc := T.curCore_lt hCs + have hH : H p = some .ep := J.utIpc.ep p hp + let Q : Set U64 := {q | q ∈ Z.T ∧ ∃ z, σ.kheap (specPtr p) = some (.endpoint z) ∧ specPtr q ∈ z.epQueue} + have hQ : Q ⊆ Z.T := fun _ h => h.1 + have hQb : ∀ q ∈ Q, q ∈ Z.T ∧ L.mem s (Notif.tBlockA q) = p := fun q ⟨ht, z, hz, hm⟩ => + ⟨ht, ((J.ep.at_ep hp hz).blocked hm).2⟩ + have hon : ∀ z, σ.kheap (specPtr p) = some (.endpoint z) → ∀ t ∈ Z.T, specPtr t ∈ z.epQueue → + L.mem s (affA t) = L.cpu s := fun _ _ t ht _ => T.onCore_single hCs h1 ht + have hQs : ∀ z, σ.kheap (specPtr p) = some (.endpoint z) → ∀ t ∈ Z.T, specPtr t ∈ z.epQueue → t ∈ Q := + fun z hz t ht hm => ⟨ht, z, hz, hm⟩ + have blk := endpoint.cancel_badged_sends.blkF s p b (BXI.blk J.sys.ent.bxi) hH + refine WP.spec_mono (WP.spec_and blk (cancel_badged_relF (T.schedOpsEA (ar := ar) hc hmb hmax htu) T.schedFootEA + hP.e s σ J.ep T.ipcSchedA hp b hon hQs)) ?_ + rintro s' ⟨⟨blk', -⟩, -, σ', h', E', SR', K, M, de, -⟩ + have wE : WritesIn st s s' (fun x => EpSigWA Rp Q p x ∨ MWrA (st := st) Z m σ s x) := K.fp + have w : WritesIn st s s' (SchedStepNWA (st := st) N E Rp Z m σ s) := wE.mono fun _ h => T.epW_stepNA hp hQ h + have tw : ∀ t ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → + (K.val ≠ 3328 ∧ K.val ≠ 3496 ∧ K.val ≠ 3504 ∧ K.val ≠ 3464 ∧ K.val ≠ 3480 ∧ K.val ≠ 3488 ∧ + K.val ≠ 3592) → L.mem s' (wa t K) = L.mem s (wa t K) := fun t ht K h1 h2 h3 => + wE.mem_eq (field_aligned (J.sr.core.tcbs t ht) h1 h2) (T.tcbWord_offEpA hp hQ ht h1 h3) + -- `RunVcpuInv`: a thread running a VCPU is not queued + have rv : RunVcpuInv st Z.T s' := fun t ht h0 => by + have er : L.mem s' (runVcpuA t) = L.mem s (runVcpuA t) := by + unfold runVcpuA + exact tw t ht 3672#u64 (by simp) (by simp) (by simp) + have hvm := J.runVcpu t ht (er ▸ h0) + have tQ : t ∉ Q := fun ⟨_, z, hz, hm⟩ => by + obtain ⟨xs, -, hxs, hB⟩ := (J.ep.at_ep hp hz).queue + rw [hxs, List.mem_map] at hm + obtain ⟨y, hy, ey⟩ := hm + obtain rfl := specPtr_inj ey + have h8 : (sched.TS_RUNNING_VM : U64).val = 8 := by unfold sched.TS_RUNNING_VM; rfl + rcases (hB y hy).2 with h | h <;> + · rw [hvm, h8] at h + omega + have hta := hP.e.tcb t ht + have es : L.mem s' (Fl4ProofsA64.P1.stA t) = L.mem s (Fl4ProofsA64.P1.stA t) := by + refine wE.mem_eq (a_st hta).1 ?_ + have htH : H t = some .tcb := by + have : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + exact this + have hfit := (J.sr.core.tcbs t ht).fits + have offI : ¬ IpcObjWA N E Rp (Fl4ProofsA64.P1.stA t) := + T.ipc_off_hobjA (v := t) (kv := .tcb) htH (by simp) + ⟨by rw [Endp.val_st hta]; omega, by rw [Endp.val_st hta]; simp only [HKind.bits]; omega⟩ + rintro (h | h) + · rcases h with h | ⟨r, hr, e⟩ | ⟨u, hu, h⟩ + · refine offI (Or.inr (Or.inl ⟨p, hp, ?_⟩)); unfold EW at h; omega + · refine offI (Or.inr (Or.inr ⟨r, hr, ?_⟩)); rw [e, Endp.rTcbA_val]; omega + · have ne : u ≠ t := fun e => tQ (e ▸ hu) + have := Endp.tcb_disj hP.e ht (hQ hu) ne + have hua := hP.e.tcb u (hQ hu) + rcases h with h | h + · have : (Fl4ProofsA64.P1.stA t).val = u.val + 3328 := by rw [h, Endp.val_st hua] + rw [Endp.val_st hta] at this + omega + · unfold TL at h + rw [Endp.val_st hta] at h + omega + · rw [Fl4ProofsA64.P1.stA_eq] at h + exact T.tcbWord_offMA ht (K := 3328#u64) (by simp) (by simp) h + rw [es]; exact hvm + refine ⟨σ', h', blk', rv, fun sysl symL userOK j7 => ?_⟩ + have F : SchedFrameA σ σ' := cancelBadgedSends_presA (specPtr p) b.bv σ ((), σ') h' + obtain ⟨Q', R', H3⟩ := SR' + have ntfn : NtfnStateRel N Z.T (L.mem s') σ' := T.ntfn_epFr hp hQb wE K.keep E' + have reply : ReplyStackRelA (L.mem s') Rp Z.C σ' (scNameA m) stack := by + refine J.reply.epFr K.keep fun r hr => ?_ + obtain ⟨r32, -, rfit⟩ := hP.e.rep r hr + have one : ∀ o : U64, 8 ≤ o.val → o.val < 32 → o.val % 8 = 0 → L.mem s' (wa r o) = L.mem s (wa r o) := by + intro o o0 o1 o2 + have v : (wa r o).val = r.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + refine wE.mem_eq ?_ (T.reply_offEpA hp hQ hr (by omega) (by omega)) + show (wa r o).val % 8 = 0 + omega + have n16 : (layout.reply.NEXT).val = 16 := by unfold layout.reply.NEXT; rfl + have p8 : (layout.reply.PREV).val = 8 := by unfold layout.reply.PREV; rfl + exact ⟨one _ (by omega) (by omega) (by omega), one _ (by omega) (by omega) (by omega)⟩ + have kc : IpcKeepA σ σ' := by + refine ⟨fun q x h => ?_, fun q y k h => ?_, fun q y' k' h => ?_⟩ + · obtain ⟨y, hy, c⟩ := K.keep.tcb h + exact ⟨y, hy, c.fault, c.frame, c.buf, c.mcp, c.yld, c.flg⟩ + · rcases K.keep q with e | ⟨o, o', e1, e2, c⟩ + · exact ⟨y, k, e.trans h, rfl⟩ + · rw [h] at e1; cases e1 + obtain ⟨y', rfl, -, c2⟩ := c + exact ⟨y', k, e2, c2⟩ + · rcases K.keep q with e | ⟨o, o', e1, e2, c⟩ + · exact ⟨y', k', e.symm.trans h, rfl⟩ + · rw [h] at e2; cases e2 + cases o <;> simp only [EpObjKeepA] at c + case endpoint => obtain ⟨_, c⟩ := c; cases c + case reply => obtain ⟨_, c, -⟩ := c; cases c + case tcb => obtain ⟨_, c, -⟩ := c; cases c + case schedContext y k => + obtain ⟨z, c, -, c2⟩ := c + cases c + exact ⟨y, _, e1, c2⟩ + have inv := K.inv m n ⟨J.sinv, J.sidle, sact⟩ + have bq : BlkQueuedA Z.T E N (L.mem s') σ' := by + obtain ⟨y, hy, -⟩ := J.ep.ep p hp + refine J.blkQ.finA M (fun t ht => ?_) (fun t ht => ?_) (fun y' hy' r hr => de y' hy' r hr) (fun y' q hn => ?_) + · obtain ⟨x, hx, -⟩ := J.ntfn.tcb t ht + exact ⟨x, hx⟩ + · rw [Notif.tBlockA_eq]; exact tw t ht 3336#u64 (by simp) (by simp) (by simp) + · rw [hy] at hn; cases hn + have sv : SchedValidA σ' := by + refine Sched.cancelBadgedSends_schedValid (specPtr p) b.bv σ ⟨J.schedValid, fun x hx => ?_⟩ ((), σ') h' + refine Sched.BAs.ofBlockedV J.schedValid fun t ht y hy => ?_ + obtain ⟨xs, hQ', hxs, hB⟩ := (J.ep.at_ep hp hx).queue + rw [hxs, List.mem_map] at ht + obtain ⟨u, hu, rfl⟩ := ht + have code := (J.ep.at_tcb (hQ'.mem u hu) hy).st + rcases (hB u hu).2 with h | h + · obtain ⟨_, _, _, e⟩ := tsCode_three (code.symm.trans h) + rw [e]; rfl + · obtain ⟨_, _, e⟩ := tsCode_four (code.symm.trans h) + rw [e]; rfl + refine ⟨Q', R', ?_⟩ + exact J.of_ipcStepA (Q := Q') (Rl := R') (curOf := Z.curOf) (curScOf := Z.curScOf) w F.1 F.2.1 (Sched.qk_of h') F.2.2 (fun _ _ => rfl) + (BlkOK.bxi blk') sysl symL userOK j7 H3 E' ntfn reply kc + (fun t ht O h h8 => tw t ht O (by omega) h8 (by omega)) + inv.sinv inv.sidle rv bq sv + +end + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {ar : machine.Arch A} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +set_option maxHeartbeats 2000000 in +/-- **CNode CancelBadgedSends at a syscall entry** (module docstring). -/ +theorem JSyscallA.cancelBadgedA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {slot lbl length buf index depth dest p b c0 cr cgs cg : U64} {blk call don : Bool} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .CNode c0 cr cgs cg) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hlbl : lbl = fl4_abi.label.CNodeCancelBadgedSends) (hl2 : ¬ length < 2#u64) + (hi : msg.syscall_arg st s (L.curW s) buf 0#u64 = ok index) + (hd : msg.syscall_arg st s (L.curW s) buf 1#u64 = ok depth) + (hres : cspace.lookup_target_slot st s (.CNode c0 cr cgs cg) index depth = ok (.Ok dest)) + (hdc : capA st s dest = .Endpoint p b true true true true) (hdOS : dest ∈ OS) : + ∃ xc σ1 σ2 s2, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + (.ok (), σ2) ∈ ((invokeCnode (Z := Unit) + (.cancelBadgedSendsCall (.endpointCap (specPtr p) b.bv Fl4SpecA64.allRights))).run σ1).1 ∧ + syscall.decode_invocation st ar s a lbl length slot (capA st s slot) blk call don false buf extra = + ok (.Ok (), s2, a) ∧ + (Sched.BodyOKA σ2 m ∧ σ2.curThread = σ.curThread) ∧ + ∃ Q Rl, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s2 a σ2 m := by + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hslot hcov J.actW J.running J.cur + have hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM := J.toJStage3EntryA.curNotVmA + obtain ⟨xc, hkc, hst, hsp, J1, -, -, -⟩ := J.toJStage3A.of_restartCurA RS.pl.choose_spec.choose_spec.choose_spec.choose_spec + RS.sym RS.run hnvm RS.cur RS.vs RS.vc + have hp : p ∈ E := by + have hH := (J.toJStage3A.slotCapTy hdOS).1.1.1 + rw [hdc] at hH + exact J.ipcHeapCov.ep p (hH p b true true true true rfl) + set s1 := L.wr s (stA (L.curW s)) sched.TS_RESTART with hs1 + set σ1 := stUpdA σ σ.curThread xc .restart with hσ1 + have sact : SActA σ1 := + { f1 := fun u h => by + have : σ1.schedulerAction = σ.schedulerAction := rfl + rw [this, J.act] at h; cases h + f2 := ⟨{ xc with tcbState := .restart }, by rw [hσ1]; simp [stUpdA, setObjA], Or.inr (Or.inl rfl)⟩ } + have noSw : ∀ u, σ1.schedulerAction ≠ .switchThread u := fun u => J.noSwitch u + have hdec : ∀ s2, (b = 0#u64 ∧ s2 = s1) ∨ (b ≠ 0#u64 ∧ endpoint.cancel_badged_sends st s1 p b = ok s2) → + syscall.decode_invocation st ar s a lbl length slot (capA st s slot) blk call don false buf extra = + ok (.Ok (), s2, a) := fun s2 h2 => by + have n1 : ¬ lbl < fl4_abi.label.CNodeRevoke := by + subst hlbl; have := lblA_revoke; have := lblA_cancel; scalar_tac + have n2 : ¬ lbl > fl4_abi.label.CNodeRotate := by + subst hlbl; have := lblA_cancel; have := lblA_rotate; scalar_tac + have g1 : ¬ lbl ≥ fl4_abi.label.CNodeCopy := by + subst hlbl; have := lblA_copy; have := lblA_cancel; scalar_tac + have k1 : ¬ lbl = fl4_abi.label.CNodeRevoke := by + subst hlbl; rw [u64_eq_iffA]; simp + have k2 : ¬ lbl = fl4_abi.label.CNodeDelete := by + subst hlbl; rw [u64_eq_iffA]; simp + obtain ⟨o, ho, hoe, -⟩ := full_endpoint_relA (.Endpoint p b true true true true) + obtain rfl : o = some (p, b) := (hoe p b).2 rfl + rw [hcap, decodeCnode_programA, if_neg Bool.false_ne_true] + unfold cnode.decode + rw [if_neg n1, if_neg n2, if_neg hl2] + simp only [cur_thread_ok, bind_ok, hi, hd, hres, core.result.Result.Insts.CoreOpsTry.branch] + simp only [g1, k1, k2, ↓reduceIte, bind_ok] + rw [if_pos hlbl] + unfold cnode.decode_cancel_badged_sends + simp only [cur_thread_ok, capA_eq, hdc, bind_ok, ho, uncurry_apply_pair, hst] + rcases h2 with ⟨hb, e⟩ | ⟨hb, h2⟩ + · have bf : (b != 0#u64) = false := by simp [hb] + simp only [bf, Bool.false_eq_true, ↓reduceIte, bind_ok, uncurry_apply_pair, ← e] + · have bt : (b != 0#u64) = true := by simpa using hb + simp only [bt, h2, ↓reduceIte, bind_ok, uncurry_apply_pair] + have fin : ∀ s2, (b = 0#u64 ∧ s2 = s1) ∨ (b ≠ 0#u64 ∧ endpoint.cancel_badged_sends st s1 p b = ok s2) → + ∀ σX : Fl4SpecA64.State Unit, SysL st H Z.Cs OS ia ie s2 ∧ SymL st H Z.Cs s2 ∧ + Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s2 ∧ J7A st (J7R2A st ar hPl.ipa) Z H OS s2 σX m := + fun s2 h2 σX => by + obtain ⟨-, -, sysl, symL, -, userOK, j7⟩ := J.decodeFinA hA hM hPl hfm hslot hcov + (rfl : capA st s slot = capA st s slot) hext hSC (lbl := lbl) + (by rw [hcap, hlbl]; exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨rfl, rfl⟩)))))))) + (fun _ h => by rw [hcap] at h; cases h) (fun _ h => by rw [hcap] at h; cases h) (hdec s2 h2) σX m + exact ⟨sysl, symL, userOK, j7⟩ + by_cases hb : b = 0#u64 + · have h2 : (b = 0#u64 ∧ s1 = s1) ∨ (b ≠ 0#u64 ∧ endpoint.cancel_badged_sends st s1 p b = ok s1) := + Or.inl ⟨hb, rfl⟩ + refine ⟨xc, σ1, σ1, s1, hkc, rfl, hsp, ?_, hdec s1 h2, Sched.BodyOKA.ofRestart J hkc hσ1, Z.Q, Z.R, J1⟩ + subst hb + show _ ∈ ((invokeCnode (Z := Unit) (.cancelBadgedSendsCall + (.endpointCap (specPtr p) 0 Fl4SpecA64.allRights))).run σ1).1 + rw [invokeCnode_cancel_zeroA]; rfl + · have hbv : b.bv ≠ 0 := by + intro h; apply hb; apply UScalar.eq_of_val_eq + have := congrArg BitVec.toNat h; simpa using this + have body := J1.cancelBadged_stepA (ar := ar) sact noSw hCs h1 hmb hmax htu hp b + obtain ⟨s2, eb, σ2, hm, -, -, K⟩ := exists_of_specA body + have h2 : (b = 0#u64 ∧ s2 = s1) ∨ (b ≠ 0#u64 ∧ endpoint.cancel_badged_sends st s1 p b = ok s2) := + Or.inr ⟨hb, eb⟩ + obtain ⟨sysl, symL, userOK, j7⟩ := fin s2 h2 σ2 + obtain ⟨Q, Rl, J2⟩ := K sysl symL userOK j7 + have hm' : ((), σ2) ∈ (cancelBadgedSends (Z := Unit) (specPtr p) b.bv (stUpdA σ σ.curThread xc .restart)).1 := by + rw [← hσ1]; exact hm + refine ⟨xc, σ1, σ2, s2, hkc, rfl, hsp, ?_, hdec s2 h2, Sched.BodyOKA.ofCancel J hkc hm', Q, Rl, J2⟩ + rw [invokeCnode_cancel_nzA _ _ hbv] + exact liftE_memA hm + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3CancelBodyA.lean b/proofs/Fl4ProofsA64/P3/Stage3CancelBodyA.lean new file mode 100644 index 000000000..3a071bcb0 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3CancelBodyA.lean @@ -0,0 +1,565 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Plan B step 2): **the body-end facts after a badged cancel**. + +`CKeepA σ σ'`: the clock, the counter observation, the current thread and SC, the consumed time and the release queue +are kept, and the current SC object is kept. The loop of `cancel_badged_sends` is such a step: its only refill write is +`refill_unblock_check` guarded to an SC that is not the current one. The closing `reschedule_required` leaves +`chooseNewThread`, so the switch-target and activation facts are vacuous. + +* `cancelBadgedSends_bodyA`: on a send endpoint the cancel is a `CKeepA` step ending in `chooseNewThread`; on any + other endpoint it changes nothing. +* `BodyOKA.ofCancel`: the body-end facts after `Restart` and the cancel, from the syscall-entry record. + +Same method as the budget frame of `send_signal` (kernel-entry Plan A, `P3/Stage3SigBudgetA`), with the release queue +kept and without the signal's operations. + +Named premises: none. + +New module. +-/ +import Fl4ProofsA64.P3.SpecFrameIpcA +import Fl4ProofsA64.P3.Stage3SigSchedValidA +import Fl4ProofsA64.P3.Stage3DelSchedValidA +import Fl4ProofsA64.P3.Stage3BodyOKA +import Fl4ProofsA64.P3.Stage3DelFrameA +import Fl4ProofsA64.P3.DelSinvLoopA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Classical + +noncomputable section + +theorem PresR.monoR {σ α : Type} {R R' : σ → σ → Prop} {m : NonDet σ α} (h : PresR R m) + (hRR : ∀ a b, R a b → R' a b) : PresR R' m := fun s x hx => hRR _ _ (h s x hx) + +theorem PresR.ofEq' {σ α : Type} {R : σ → σ → Prop} [PreO R] {m : NonDet σ α} (h : PresR (@Eq σ) m) : + PresR R m := fun s x hx => by + have e := h s x hx + rw [← e] + exact PreO.refl s + +/-- The scalars of the cancel frame. -/ +structure CScalA (σ σ' : State Unit) : Prop where + time : σ'.curTime = σ.curTime + ms : σ'.machineState = σ.machineState + cur : σ'.curThread = σ.curThread + csc : σ'.curSc = σ.curSc + cons : σ'.consumedTime = σ.consumedTime + rel : σ'.releaseQueue = σ.releaseQueue + +theorem CScalA.refl (σ : State Unit) : CScalA σ σ := ⟨rfl, rfl, rfl, rfl, rfl, rfl⟩ + +theorem CScalA.trans {a b c : State Unit} (h1 : CScalA a b) (h2 : CScalA b c) : CScalA a c := + ⟨h2.time.trans h1.time, h2.ms.trans h1.ms, h2.cur.trans h1.cur, h2.csc.trans h1.csc, h2.cons.trans h1.cons, + h2.rel.trans h1.rel⟩ + +/-- The cancel frame without the current thread's queue and scheduling-context clauses. -/ +structure CBaseA (σ σ' : State Unit) : Prop extends CScalA σ σ' where + sc : ∀ y k, σ.kheap σ.curSc = some (.schedContext y k) → σ'.kheap σ.curSc = some (.schedContext y k) + +instance : PreO CBaseA where + refl σ := ⟨CScalA.refl σ, fun _ _ h => h⟩ + trans h1 h2 := ⟨h1.toCScalA.trans h2.toCScalA, fun y k h => by + have := h2.sc y k (by rw [h1.csc]; exact h1.sc y k h) + rwa [h1.csc] at this⟩ + +/-- **The cancel frame** (module docstring). -/ +structure CKeepA (σ σ' : State Unit) : Prop extends CBaseA σ σ' where + off : CtOffA σ.curThread σ → CtOffA σ.curThread σ' + ctSc : CtScKA σ.curThread σ σ' + +instance : PreO CKeepA where + refl σ := ⟨PreO.refl (R := CBaseA) σ, id, CtScKA.refl _ _⟩ + trans h1 h2 := ⟨PreO.trans (R := CBaseA) h1.toCBaseA h2.toCBaseA, CtOffA.trans h1.cur h1.off h2.off, + h1.ctSc.trans (h1.cur ▸ h2.ctSc)⟩ + +/-- One object replaced that was not an SC; a new TCB keeps its scheduling context. -/ +theorem CKeepA.upd {σ : State Unit} {p : ObjRef} {o : KernelObject} + (h : ∀ y k, σ.kheap p ≠ some (.schedContext y k)) + (ht : ∀ x', o = .tcb x' → ∃ x, σ.kheap p = some (.tcb x) ∧ + (x'.tcbSchedContext = x.tcbSchedContext ∨ x'.tcbSchedContext = none)) : + CKeepA σ { σ with kheap := Function.update σ.kheap p (some o) } := by + refine ⟨⟨⟨rfl, rfl, rfl, rfl, rfl, rfl⟩, fun y k hy => ?_⟩, id, + CtScKA.ofUpd (o' := o) (p := p) rfl (fun x' e _ => ht x' e)⟩ + have ne : σ.curSc ≠ p := fun e => h y k (e ▸ hy) + simpa only [Function.update_of_ne ne] using hy + +abbrev CPresA {α : Type} (m : SMonad Unit α) : Prop := PresR CKeepA m + +theorem CPresA.modify_fix (f : State Unit → State Unit) (s : ∀ σ, CScalA σ (f σ)) + (hk : ∀ σ, (f σ).kheap = σ.kheap) (hr : ∀ σ, (f σ).readyQueues = σ.readyQueues) + (ha : ∀ σ, (f σ).schedulerAction = σ.schedulerAction) : CPresA (NonDet.modify f : SMonad Unit Unit) := + PresR.modify f fun σ => ⟨⟨s σ, fun y k h => by rw [hk]; exact h⟩, CtOffA.keep (hr σ) (Or.inl (ha σ)), + CtScKA.ofEq (hk σ)⟩ + +theorem threadSet_ckA (f : Tcb → Tcb) (hf : ∀ x, (f x).tcbSchedContext = x.tcbSchedContext) (t : ObjRef) : CPresA (threadSet (Z := Unit) f t) := by + intro σ r hr + obtain ⟨y1, hy1, h⟩ := hr + have hy1' : y1 ∈ (NonDet.assertOpt (getTcb (Z := Unit) t σ) σ).1 := hy1 + cases hg : getTcb (Z := Unit) t σ with + | none => rw [hg] at hy1'; exact absurd hy1' (Set.notMem_empty _) + | some x => + rw [hg] at hy1' + have e1 : y1 = (x, σ) := hy1' + subst e1 + obtain ⟨ko, hko, hr2⟩ := setObject_memA h + rw [hr2] + refine CKeepA.upd (fun y k e => ?_) (fun x' e => ⟨x, Sched.getTcb_kheapA hg, Or.inl ?_⟩) + · simp [getTcb, readObject, e] at hg + · rw [← KernelObject.tcb.inj e]; exact hf x + +theorem setEndpoint_ckA (p : ObjRef) (a : Endpoint) : CPresA (setEndpoint (Z := Unit) p a) := by + intro σ r hr + obtain ⟨y, hk, hr2⟩ := setSimpleKo_memA KernelObject.endpoint p a hr + rw [hr2] + refine CKeepA.upd (fun z k e => ?_) (fun x' e => by cases e) + rw [hk] at e + cases e + +macro "ckres" : tactic => `(tactic| + repeat' (first + | (apply threadSet_ckA; intro _; rfl) + | exact setEndpoint_ckA _ _ + | exact PresR.pure _ + | exact PresR.gets _ + | exact PresR.get + | exact PresR.getsThe _ + | exact PresR.assertOpt _ + | exact PresR.assert _ + | exact PresR.fail + | refine PresR.bind_pure _ _ ?_ + | refine PresR.bind ?_ (fun _ => ?_) + | refine PresR.when ?_ + | refine PresR.unless' ?_ + | refine PresR.ite' ?_ ?_ + | (refine CPresA.modify_fix _ (fun _ => ⟨rfl, rfl, rfl, rfl, rfl, rfl⟩) (fun _ => rfl) (fun _ => rfl) (fun _ => rfl)) + | dsimp only + | split)) + +theorem getObject_ckA (p : ObjRef) : CPresA (getObject (Z := Unit) p) := by unfold getObject; ckres + +theorem threadGet_ckA {α : Type} (f : Tcb → α) (t : ObjRef) : CPresA (threadGet (Z := Unit) f t) := by + unfold threadGet; ckres + +theorem getThreadState_ckA (t : ObjRef) : CPresA (getThreadState (Z := Unit) t) := by + unfold getThreadState; exact threadGet_ckA _ _ + +theorem getTcbObjRef_ckA (f : Tcb → Option ObjRef) (t : ObjRef) : CPresA (getTcbObjRef (Z := Unit) f t) := by + unfold getTcbObjRef; exact threadGet_ckA _ _ + +theorem getSchedContext_ckA (p : ObjRef) : CPresA (getSchedContext (Z := Unit) p) := by + unfold getSchedContext + refine PresR.bind (getObject_ckA _) (fun _ => ?_) + ckres + +theorem getEndpoint_ckA (p : ObjRef) : CPresA (getEndpoint (Z := Unit) p) := by + unfold getEndpoint getSimpleKo + refine PresR.bind (getObject_ckA _) (fun _ => ?_) + ckres + +theorem setSchedulerAction_ckA (a : SchedulerAction) (ha : ∀ t, a ≠ .switchThread t) : + CPresA (setSchedulerAction (Z := Unit) a) := by + unfold setSchedulerAction + exact PresR.modify _ fun σ => ⟨⟨⟨rfl, rfl, rfl, rfl, rfl, rfl⟩, fun _ _ h => h⟩, + CtOffA.keep rfl (Or.inr ha), CtScKA.ofEq rfl⟩ + +theorem getScRefillReady_ckA (p : ObjRef) : CPresA (getScRefillReady (Z := Unit) p) := by + unfold getScRefillReady; ckres + +theorem getScRefillSufficient_ckA (p : ObjRef) (u : Ticks) : CPresA (getScRefillSufficient (Z := Unit) p u) := by + unfold getScRefillSufficient; ckres + +/-- Only ready queues change, each by at most gaining `t`. -/ +def EnqA (t : ObjRef) (σ σ' : State Unit) : Prop := + ∃ Q, σ' = { σ with readyQueues := Q } ∧ ∀ d p x, x ∈ Q d p → x ∈ σ.readyQueues d p ∨ x = t + +instance (t : ObjRef) : PreO (EnqA t) where + refl σ := ⟨σ.readyQueues, rfl, fun _ _ _ h => Or.inl h⟩ + trans := by + rintro a b c ⟨Q, rfl, h1⟩ ⟨Q', rfl, h2⟩ + refine ⟨Q', rfl, fun d p x h => ?_⟩ + rcases h2 d p x h with h | h + · exact h1 d p x h + · exact Or.inr h + +theorem tcbSchedEnqueue_enqA (t : ObjRef) : PresR (EnqA t) (tcbSchedAction (Z := Unit) tcbSchedEnqueue t) := by + intro σ r hr + unfold tcbSchedAction getTcbQueue setTcbQueue at hr + obtain ⟨d, h1, hr⟩ := mem_bindA.1 hr + have e1 : σ = d.2 := threadGet_eqPres _ _ σ d h1 + obtain ⟨pr, h2, hr⟩ := mem_bindA.1 hr + have e2 : d.2 = pr.2 := threadGet_eqPres _ _ d.2 pr h2 + rw [← e2, ← e1] at hr + obtain ⟨q, hq, hr⟩ := mem_bindA.1 hr + obtain ⟨q0, hq0, hq⟩ := mem_bindA.1 hq + obtain rfl : q0 = (σ.readyQueues, σ) := hq0 + obtain rfl : q = (σ.readyQueues d.1 pr.1, σ) := hq + obtain ⟨u, hu, hr⟩ := mem_bindA.1 hr + obtain rfl : u = (tcbSchedEnqueue t (σ.readyQueues d.1 pr.1), σ) := hu + obtain rfl : r = ((), _) := hr + refine ⟨_, rfl, fun d' p' x hx => ?_⟩ + dsimp only at hx + split at hx + · rename_i c + obtain ⟨rfl, rfl⟩ := c + unfold tcbSchedEnqueue at hx + split at hx + · rcases List.mem_cons.1 hx with h | h + · exact Or.inr h + · exact Or.inl h + · exact Or.inl hx + · exact Or.inl hx + +/-- `reschedule_required` leaves `chooseNewThread` and at most enqueues the pending switch target. -/ +theorem rescheduleRequired_shapeA {σ : State Unit} {r : Unit × State Unit} + (h : r ∈ (rescheduleRequired (Z := Unit) σ).1) : + ∃ Q, r.2 = { σ with readyQueues := Q, schedulerAction := .chooseNewThread } ∧ + ∀ d p x, x ∈ Q d p → x ∈ σ.readyQueues d p ∨ σ.schedulerAction = .switchThread x := by + have set : ∀ (s : State Unit) (r : Unit × State Unit), r ∈ (setSchedulerAction (Z := Unit) .chooseNewThread s).1 → + r.2 = { s with schedulerAction := .chooseNewThread } := fun s r h => by + obtain rfl : r = ((), _) := h + rfl + unfold rescheduleRequired at h + obtain ⟨a, ha, h⟩ := mem_bindA.1 h + obtain rfl : a = (σ.schedulerAction, σ) := ha + cases hact : σ.schedulerAction with + | switchThread t => + simp only [hact] at h + obtain ⟨b, hb, h⟩ := mem_bindA.1 h + obtain rfl : b = (schedulable t σ, σ) := hb + obtain ⟨c, hc, h⟩ := mem_bindA.1 h + have k : PresR (EnqA t) (NonDet.when (schedulable t σ = true) (do + let scOpt ← threadGet (Z := Unit) Tcb.tcbSchedContext t + let scp ← NonDet.assertOpt scOpt + let sufficient ← getScRefillSufficient scp 0 + let ready ← getScRefillReady scp + NonDet.assert (sufficient && ready) + tcbSchedAction tcbSchedEnqueue t)) := by + unfold getScRefillSufficient getScRefillReady + repeat' (first + | exact tcbSchedEnqueue_enqA _ + | exact PresR.ofEq' (threadGet_eqPres _ _) + | exact PresR.ofEq' (getSchedContext_eqPres _) + | exact PresR.pure _ + | exact PresR.gets _ + | exact PresR.getsThe _ + | exact PresR.assertOpt _ + | exact PresR.assert _ + | refine PresR.bind ?_ (fun _ => ?_) + | refine PresR.when ?_ + | dsimp only + | split) + obtain ⟨Q, e, m⟩ := k σ c hc + refine ⟨Q, by rw [set _ _ h, e], fun d p x hx => ?_⟩ + rcases m d p x hx with h' | h' + · exact Or.inl h' + · exact Or.inr (by rw [h']) + | resumeCurThread => + simp only [hact, pure_bind] at h + exact ⟨σ.readyQueues, set _ _ h, fun _ _ _ h => Or.inl h⟩ + | chooseNewThread => + simp only [hact, pure_bind] at h + exact ⟨σ.readyQueues, set _ _ h, fun _ _ _ h => Or.inl h⟩ + +/-- The enqueue of the pending switch target keeps the current thread off (the switch half of `CtOffA`). -/ +theorem CtOffA.resched {σ : State Unit} {Q : Domain → Priority → ReadyQueue} + (m : ∀ d p x, x ∈ Q d p → x ∈ σ.readyQueues d p ∨ σ.schedulerAction = .switchThread x) + (h : CtOffA σ.curThread σ) : + CtOffA σ.curThread { σ with readyQueues := Q, schedulerAction := .chooseNewThread } := by + refine ⟨fun d p hm => ?_, fun t e => by cases e⟩ + rcases m d p _ hm with c | c + · exact h.1 d p c + · exact h.2 _ c rfl + +theorem rescheduleRequired_ckA : CPresA (rescheduleRequired (Z := Unit)) := by + intro σ r hr + obtain ⟨Q, e, m⟩ := rescheduleRequired_shapeA hr + rw [e] + exact ⟨⟨⟨rfl, rfl, rfl, rfl, rfl, rfl⟩, fun _ _ h => h⟩, CtOffA.resched m, CtScKA.ofEq rfl⟩ + +/-- The cancel frame for an operation on thread `t`: the current thread's clauses only when `t` is not it. -/ +def CNeA (t : ObjRef) (σ σ' : State Unit) : Prop := CBaseA σ σ' ∧ (t ≠ σ.curThread → CKeepA σ σ') + +instance (t : ObjRef) : PreO (CNeA t) where + refl σ := ⟨PreO.refl σ, fun _ => PreO.refl σ⟩ + trans h1 h2 := ⟨PreO.trans (R := CBaseA) h1.1 h2.1, fun n => + PreO.trans (R := CKeepA) (h1.2 n) (h2.2 (by rw [h1.1.cur]; exact n))⟩ + +theorem cne_of {α : Type} {t : ObjRef} {m : SMonad Unit α} (h : CPresA m) : PresR (CNeA t) m := + PresR.monoR h fun _ _ k => ⟨k.toCBaseA, fun _ => k⟩ + +theorem tcbSchedEnqueue_neA (t : ObjRef) : PresR (CNeA t) (tcbSchedAction (Z := Unit) tcbSchedEnqueue t) := by + intro σ r hr + obtain ⟨Q, e, m⟩ := tcbSchedEnqueue_enqA t σ r hr + rw [e] + have b : CBaseA σ { σ with readyQueues := Q } := ⟨⟨rfl, rfl, rfl, rfl, rfl, rfl⟩, fun _ _ h => h⟩ + refine ⟨b, fun n => ⟨b, fun h => ⟨fun d p hm => ?_, h.2⟩, CtScKA.ofEq rfl⟩⟩ + rcases m d p _ hm with c | c + · exact h.1 d p c + · exact n c.symm + +theorem setSwitch_neA (t : ObjRef) : PresR (CNeA t) (setSchedulerAction (Z := Unit) (.switchThread t)) := by + unfold setSchedulerAction + refine PresR.modify _ fun σ => ?_ + have b : CBaseA σ { σ with schedulerAction := .switchThread t } := + ⟨⟨rfl, rfl, rfl, rfl, rfl, rfl⟩, fun _ _ h => h⟩ + refine ⟨b, fun n => ⟨b, fun h => ⟨h.1, fun u e => ?_⟩, CtScKA.ofEq rfl⟩⟩ + cases e; exact n + +theorem possibleSwitchTo_ckA (t : ObjRef) : PresR (CNeA t) (possibleSwitchTo (Z := Unit) t) := by + unfold possibleSwitchTo + repeat' (first + | exact setSwitch_neA _ + | exact tcbSchedEnqueue_neA _ + | exact cne_of (getTcbObjRef_ckA _ _) + | exact cne_of (threadGet_ckA _ _) + | exact cne_of rescheduleRequired_ckA + | exact PresR.gets _ + | exact PresR.pure _ + | refine PresR.bind ?_ (fun _ => ?_) + | refine PresR.when ?_ + | refine PresR.ite' ?_ ?_ + | dsimp only + | split) + +/-! ## The guarded refill check -/ + +/-- Only the SC at `p`, the reprogram flag and nothing the cancel frame reads change. -/ +structure OnlyCkA (p : ObjRef) (σ σ' : State Unit) : Prop extends CScalA σ σ' where + other : ∀ q, q ≠ p → σ'.kheap q = σ.kheap q + rq : σ'.readyQueues = σ.readyQueues + act : σ'.schedulerAction = σ.schedulerAction + tcb : ∀ q x', σ'.kheap q = some (.tcb x') → σ.kheap q = some (.tcb x') + +instance (p : ObjRef) : PreO (OnlyCkA p) where + refl σ := ⟨CScalA.refl σ, fun _ _ => rfl, rfl, rfl, fun _ _ h => h⟩ + trans h1 h2 := ⟨h1.toCScalA.trans h2.toCScalA, fun q h => (h2.other q h).trans (h1.other q h), + h2.rq.trans h1.rq, h2.act.trans h1.act, fun q x h => h1.tcb q x (h2.tcb q x h)⟩ + +theorem updateSchedContext_onlyCkA (p : ObjRef) (f : SchedContext → SchedContext) : + PresR (OnlyCkA p) (updateSchedContext (Z := Unit) p f) := by + intro σ r hr + obtain ⟨y1, hy1, h⟩ := hr + have hy1' : y1 ∈ (NonDet.assertOpt (σ.kheap p) σ).1 := hy1 + cases hk : σ.kheap p with + | none => rw [hk] at hy1'; exact absurd hy1' (Set.notMem_empty _) + | some ko => + rw [hk] at hy1' + have e1 : y1 = (ko, σ) := hy1' + subst e1 + cases ko with + | schedContext sc n => + obtain ⟨ko', hko, hr2⟩ := setObject_memA h + rw [hr2] + refine ⟨⟨rfl, rfl, rfl, rfl, rfl, rfl⟩, fun q e => Function.update_of_ne e _ _, rfl, rfl, fun q x' h => ?_⟩ + change Function.update σ.kheap p _ q = _ at h + by_cases e : q = p + · subst e; rw [Function.update_self] at h + cases h + · rwa [Function.update_of_ne e] at h + | _ => exact absurd h (Set.notMem_empty _) + +macro "ocres" : tactic => `(tactic| + repeat' (first + | exact updateSchedContext_onlyCkA _ _ + | exact PresR.pure _ + | exact PresR.gets _ + | exact PresR.get + | exact PresR.getsThe _ + | exact PresR.assertOpt _ + | exact PresR.assert _ + | exact PresR.fail + | refine PresR.bind_pure _ _ ?_ + | refine PresR.bind ?_ (fun _ => ?_) + | refine PresR.when ?_ + | refine PresR.unless' ?_ + | refine PresR.ite' ?_ ?_ + | (refine PresR.modify _ (fun _ => ⟨⟨rfl, rfl, rfl, rfl, rfl, rfl⟩, fun _ _ => rfl, rfl, rfl, fun _ _ h => h⟩)) + | dsimp only + | split)) + +theorem refillUnblockCheck_onlyCkA (p : ObjRef) : PresR (OnlyCkA p) (refillUnblockCheck (Z := Unit) p) := by + have go : PresR (OnlyCkA p) (getObject (Z := Unit) p) := by unfold getObject; ocres + have gs : PresR (OnlyCkA p) (getSchedContext (Z := Unit) p) := by + unfold getSchedContext; refine PresR.bind go (fun _ => ?_); ocres + have gr : PresR (OnlyCkA p) (getRefills (Z := Unit) p) := by + unfold getRefills; repeat' (first | exact gs | refine PresR.bind ?_ (fun _ => ?_) | exact PresR.pure _ | dsimp only) + have gh : PresR (OnlyCkA p) (getRefillHead (Z := Unit) p) := by + unfold getRefillHead; exact PresR.getsThe _ + have rr : PresR (OnlyCkA p) (getScRefillReady (Z := Unit) p) := by unfold getScRefillReady; ocres + have rb : PresR (OnlyCkA p) (isRoundRobin (Z := Unit) p) := by + unfold isRoundRobin; repeat' (first | exact gs | refine PresR.bind ?_ (fun _ => ?_) | exact PresR.pure _ | dsimp only) + have up : ∀ f, PresR (OnlyCkA p) (updateRefillHd (Z := Unit) p f) := fun f => by + unfold updateRefillHd; ocres + have pop : PresR (OnlyCkA p) (refillPopHead (Z := Unit) p) := by + unfold refillPopHead + repeat' (first | exact gh | exact gr | exact gs | exact updateSchedContext_onlyCkA _ _ | refine PresR.bind ?_ (fun _ => ?_) | exact PresR.pure _ | exact PresR.assert _ | dsimp only) + have mo : PresR (OnlyCkA p) (mergeOverlappingRefills (Z := Unit) p) := by + unfold mergeOverlappingRefills + exact PresR.bind pop fun _ => up _ + have lp : PresR (OnlyCkA p) (refillHeadOverlappingLoop (Z := Unit) p) := by + unfold refillHeadOverlappingLoop; exact PresR.whileLoop (fun _ => mo) _ + unfold refillUnblockCheck + repeat' (first + | exact rb + | exact rr + | exact up _ + | exact lp + | exact PresR.gets _ + | exact PresR.pure _ + | refine PresR.bind ?_ (fun _ => ?_) + | refine PresR.when ?_ + | (refine PresR.modify _ (fun _ => ⟨⟨rfl, rfl, rfl, rfl, rfl, rfl⟩, fun _ _ => rfl, rfl, rfl, fun _ _ h => h⟩)) + | dsimp only) + +/-- The sporadic check of a restart keeps the cancel frame: it is guarded to an SC that is not the current one. -/ +theorem ifSporadicAssert_ckA (o : Option ObjRef) : + CPresA (ifSporadicCurScAssertRefillUnblockCheck (Z := Unit) o) := by + unfold ifSporadicCurScAssertRefillUnblockCheck ifCondRefillUnblockCheck + cases o with + | none => exact PresR.pure _ + | some p => + intro σ r hr + simp only [NonDet.maybeM] at hr + obtain ⟨⟨sc, σ1⟩, h1, hr⟩ := mem_bindA.1 hr + have e1 : σ1 = σ := (getSchedContext_mem h1).1 + rw [e1] at hr + dsimp only at hr + rw [nd_gets_bindSc, nd_pure_bindSc] at hr + try dsimp only at hr + unfold NonDet.when at hr + split at hr + · split at hr + · rename_i c + have ne : p ≠ σ.curSc := c rfl + have h := refillUnblockCheck_onlyCkA p σ r hr + exact ⟨⟨h.toCScalA, fun y k hy => by rw [h.other _ (Ne.symm ne)]; exact hy⟩, + CtOffA.keep h.rq (Or.inl h.act), fun x' hx => ⟨x', h.tcb _ _ hx, Or.inl rfl⟩⟩ + · have : r = ((), σ) := hr + rw [this]; exact PreO.refl _ + · have : r = ((), σ) := hr + rw [this]; exact PreO.refl _ + +theorem setThreadState_ckA (t : ObjRef) (s : ThreadState) : CPresA (setThreadState (Z := Unit) t s) := by + unfold setThreadState setThreadStateAct + repeat' (first + | (apply threadSet_ckA; intro _; rfl) + | exact getThreadState_ckA _ + | exact rescheduleRequired_ckA + | exact setSchedulerAction_ckA _ (fun _ h => by cases h) + | exact PresR.gets _ + | exact PresR.pure _ + | refine PresR.bind ?_ (fun _ => ?_) + | refine PresR.when ?_ + | refine PresR.ite' ?_ ?_ + | dsimp only + | split) + +theorem removeAndRestartBadged_ckA (t e : ObjRef) (b : Badge) : + PresR (CNeA t) (removeAndRestartBadgedThread (Z := Unit) t e b) := by + have dq : CPresA (tcbEpDequeue (Z := Unit) t e) := by + unfold tcbEpDequeue + refine PresR.bind (getEndpoint_ckA _) (fun _ => ?_) + ckres + have rs : PresR (CNeA t) (restartThreadIfNoFault (Z := Unit) t) := by + unfold restartThreadIfNoFault + refine PresR.bind (cne_of (threadGet_ckA _ _)) (fun _ => PresR.ite' ?_ (cne_of (setThreadState_ckA _ _))) + exact PresR.bind (cne_of (setThreadState_ckA _ _)) fun _ => PresR.bind (cne_of (getTcbObjRef_ckA _ _)) fun _ => + PresR.bind (cne_of (ifSporadicAssert_ckA _)) fun _ => possibleSwitchTo_ckA _ + unfold removeAndRestartBadgedThread + exact PresR.bind (cne_of (getThreadState_ckA _)) fun _ => PresR.when (PresR.bind (cne_of dq) fun _ => rs) + +/-- `reschedule_required` leaves `chooseNewThread`. -/ +theorem rescheduleRequired_chooseA {σ σ' : State Unit} (h : ((), σ') ∈ (rescheduleRequired (Z := Unit) σ).1) : + σ'.schedulerAction = .chooseNewThread := by + have set : ∀ s s' : State Unit, ((), s') ∈ (setSchedulerAction (Z := Unit) .chooseNewThread s).1 → + s'.schedulerAction = .chooseNewThread := fun s s' h => by + have e : ((), s') = ((), { s with schedulerAction := .chooseNewThread }) := h + rw [(Prod.mk.inj e).2] + unfold rescheduleRequired at h + obtain ⟨⟨action, s1⟩, -, h⟩ := mem_bindA.1 h + cases action with + | switchThread t => + dsimp only at h + obtain ⟨_, -, h⟩ := mem_bindA.1 h + obtain ⟨_, -, h⟩ := mem_bindA.1 h + exact set _ _ h + | resumeCurThread => + dsimp only at h + exact set _ _ h + | chooseNewThread => + dsimp only at h + exact set _ _ h + +theorem mapMx_presCK {α β : Type} (f : α → SMonad Unit β) (hf : ∀ x, CPresA (f x)) : + ∀ l : List α, CPresA (NonDet.mapMx f l) + | [] => by unfold NonDet.mapMx; exact PresR.pure _ + | x :: l => by + unfold NonDet.mapMx + exact PresR.bind (hf x) fun _ => mapMx_presCK f hf l + +/-- The cancel frame for the loop over a queue `L`. -/ +def CNlA (L : List ObjRef) (σ σ' : State Unit) : Prop := CBaseA σ σ' ∧ (σ.curThread ∉ L → CKeepA σ σ') + +instance (L : List ObjRef) : PreO (CNlA L) where + refl σ := ⟨PreO.refl σ, fun _ => PreO.refl σ⟩ + trans h1 h2 := ⟨PreO.trans (R := CBaseA) h1.1 h2.1, fun n => + PreO.trans (R := CKeepA) (h1.2 n) (h2.2 (by rw [h1.1.cur]; exact n))⟩ + +theorem mapMx_neA {β : Type} (f : ObjRef → SMonad Unit β) (hf : ∀ x, PresR (CNeA x) (f x)) (L : List ObjRef) : + ∀ l : List ObjRef, (∀ x ∈ l, x ∈ L) → PresR (CNlA L) (NonDet.mapMx f l) + | [], _ => by unfold NonDet.mapMx; exact PresR.pure _ + | x :: l, h => by + unfold NonDet.mapMx + refine PresR.bind (PresR.monoR (hf x) fun σ _ k => ⟨k.1, fun n => k.2 fun e => n (e ▸ h x (List.mem_cons_self ..))⟩) + fun _ => mapMx_neA f hf L l fun y hy => h y (List.mem_cons_of_mem _ hy) + +/-- **The badged cancel is a kept step or ends rescheduling** (module docstring). -/ +theorem cancelBadgedSends_bodyA {p : ObjRef} {b : Badge} {σ σ' : State Unit} + (h : ((), σ') ∈ (cancelBadgedSends (Z := Unit) p b σ).1) : + σ' = σ ∨ CBaseA σ σ' ∧ σ'.schedulerAction = .chooseNewThread := by + unfold cancelBadgedSends at h + obtain ⟨⟨ep, σ1⟩, h1, h⟩ := mem_bindA.1 h + have e1 : σ1 = σ := (getEndpoint_mem h1).2 + rw [e1] at h + cases ep with + | sendEP q => + refine Or.inr ?_ + obtain ⟨⟨u, σ2⟩, h2, h3⟩ := mem_bindA.1 h + have loop : PresR (CNlA q) (NonDet.mapMx (fun t => removeAndRestartBadgedThread (Z := Unit) t p b) q) := + mapMx_neA _ (fun t => removeAndRestartBadged_ckA t p b) q q fun _ h => h + have k1 : CBaseA σ σ2 := (loop σ _ h2).1 + have k2 : CBaseA σ2 σ' := (rescheduleRequired_ckA σ2 _ h3).toCBaseA + exact ⟨PreO.trans k1 k2, rescheduleRequired_chooseA h3⟩ + | idleEP => exact Or.inl (Prod.mk.inj h).2 + | recvEP q => exact Or.inl (Prod.mk.inj h).2 + +section +variable {S A : Type} {st : machine.Store S} [L : LawfulStore st] +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s : S} {a : A} {σ σ' : State Unit} {m : McState} + +/-- **The body-end facts after `Restart` and a badged cancel**, at a syscall entry. -/ +theorem BodyOKA.ofCancel (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {x : Tcb} (hk : σ.kheap σ.curThread = some (.tcb x)) {p : ObjRef} {b : Badge} + (h : ((), σ') ∈ (cancelBadgedSends (Z := Unit) p b (stUpdA σ σ.curThread x .restart)).1) : BodyOKA σ' m ∧ σ'.curThread = σ.curThread := by + rcases cancelBadgedSends_bodyA h with rfl | ⟨K, act⟩ + · exact BodyOKA.ofRestart J hk rfl + · obtain ⟨y, j, hy⟩ := J.curInv.sc + have ne : σ.curSc ≠ σ.curThread := fun e => by rw [e, hk] at hy; cases hy + have h1 : (stUpdA σ σ.curThread x .restart).kheap σ.curSc = some (.schedContext y j) := by + simp only [stUpdA, setObjA, Function.update_of_ne ne]; exact hy + have h2 : σ'.kheap σ.curSc = some (.schedContext y j) := K.sc y j h1 + refine ⟨BodyOKA.ofEntry J (Or.inr act) K.time (by rw [K.ms]; rfl) K.cur K.csc K.cons K.rel (fun y' k e => ?_) + (fun e => by rw [act] at e; cases e), K.cur⟩ + rw [h2] at e + obtain ⟨rfl, rfl⟩ := KernelObject.schedContext.inj (Option.some.inj e) + exact ⟨_, hy, rfl, rfl, rfl⟩ + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3CancelFootA.lean b/proofs/Fl4ProofsA64/P3/Stage3CancelFootA.lean new file mode 100644 index 000000000..dcc3d8d64 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3CancelFootA.lean @@ -0,0 +1,50 @@ +/- +P3 (aarch64, A64-P3 Stage 3, e5b): **the frozen footprint of `cancel_ipc` of a receive-blocked thread**. + +* `CancelFootA Rp T s t`: the words the cancel of the receive-blocked `t` writes (Rust `ipc.rs` `cancel_ipc`, + RecvBlocked arm = `clear_fault` + `ep_dequeue` + `reply_unlink` + `set_thread_state Inactive`): the fault word of + `t`, the endpoint finaliser's words `EpSigWA` (endpoint words, `Ep` links, state words and reply words of the + threads queued on `t`'s endpoint) and the narrow scheduler block `SKsA` (the action word). No `scA`, no `nScA`, + no reply-stack, no notification word: `set_thread_state` of a non-current thread only writes the action word + (`schedule_tcb_refinesA`). +* `schedFootE0A_ks`: the scheduler footprint at `SKsA` only (`back` and `scheduleTcb` of `SchedFootEA`), which + `cancel_ipc_recvF` / `cancel_ipc_recvNRF` take for `Wr`. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.IpcScFootA +import Fl4ProofsA64.P3.ReplyCancelFootA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +variable (st) in +/-- **The footprint of the cancel of the receive-blocked `t`** (module docstring). -/ +def CancelFootA (Rp T : Set U64) (s : S) (t : U64) (b : U64) : Prop := + b = tFaultA t ∨ + EpSigWA Rp {q | q ∈ T ∧ L.mem s (tBlockA q) = L.mem s (tBlockA t)} (L.mem s (tBlockA t)) b ∨ + SKsA (L.ksb s) b + +/-- The scheduler footprint at `SKsA` only: `set_thread_state` writes the action word (`schedule_tcb_refinesA`). -/ +theorem schedFootE0A_ks {Z : XSetsA} {m : McState} {κ : U64 → U64} {E R : Set U64} {Bf : Set (U64 × Word)} : + SchedFootE0A st Z.T E R Bf (IpcSchedRelA st Z m κ) (fun _ s b => SKsA (L.ksb s) b) := by + refine ⟨?_, ?_⟩ + · intro σ σ1 s s1 b _ hk h + rw [← hk] + exact h + · rintro s σ t hRel ⟨Q, R', H⟩ _ + refine WP.spec_mono (schedule_tcb_refinesA H t (fun _ ht _ hx => (hRel.at_tcb ht hx).st)) ?_ + rintro s' ⟨hw, -⟩ + exact hw.mono fun b hb => hb ▸ H.node_sks H.sch.cpuCs (by simp [layout.node.SCHED_ACTION]) + +end + +end + +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/Stage3CancelFootOffA.lean b/proofs/Fl4ProofsA64/P3/Stage3CancelFootOffA.lean new file mode 100644 index 000000000..9c46be891 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3CancelFootOffA.lean @@ -0,0 +1,107 @@ +/- +P3 (aarch64, A64-P3 Stage 3, e5b): **what `CancelFootA` does not write** (the frame of the frozen cancel footprint). + +* `JStage3BA.cancelFoot_tcbOffA`: a TCB word of any `u ∈ T` outside the state word, the `Ep` links and the fault word + (so `scA u`, `tTflA u`, the `Sched` links, ...) is not in `CancelFootA`. +* `JStage3BA.cancelFoot_ntfnOffA`: no notification word (`nScA n`, ...) is in `CancelFootA`. + +Both are consumers' lemmas at the footprint level; the rest of the stage uses them through `WritesIn.mem_eq`. +No named premise. New module. +-/ +import Fl4ProofsA64.P3.ReplyHookRecvA +import Fl4ProofsA64.P3.DelEpRecA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 Fl4ProofsA64.P3.Endp +open Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Notif + +local notation "wa" => core.num.U64.wrapping_add + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} [J7PpA st J7] {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- A TCB word outside the state word, the `Ep` links and the fault word is not written by the cancel. -/ +theorem JStage3BA.cancelFoot_tcbOffA + (J : JStage3BA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {t u K : U64} (he : L.mem s (Notif.tBlockA t) ∈ E) (ht : t ∈ Z.T) (hu : u ∈ Z.T) (hK : K.val < 4096) + (h7 : K.val ≠ 3328 ∧ K.val ≠ 3496 ∧ K.val ≠ 3504) (hf : K.val ≠ 3376) : + ¬ CancelFootA st Rp Z.T s t (wa u K) := by + have hfit := (J.sr.core.tcbs u hu).fits + have hv : (wa u K).val = u.val + K.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have huH : H u = some .tcb := by + have : u ∈ H.tcbs := by rw [J.tcbs]; exact hu + exact this + have hQ : {q | q ∈ Z.T ∧ L.mem s (Notif.tBlockA q) = L.mem s (Notif.tBlockA t)} ⊆ Z.T := fun _ h => h.1 + rintro (h | h | h) + · have h' := congrArg (fun w : U64 => w.val) h + simp only [hv, val_fault (J.iplaced.e.tcb t ht)] at h' + by_cases ett : u = t + · subst ett; omega + · have := tcb_disj J.iplaced.e hu ht (Ne.symm ett) + omega + · rcases J.epSig_casesA he hQ h with h | ⟨v, hv', e⟩ + · exact J.ipc_off_hobjA (v := u) (kv := .tcb) huH (by simp) ⟨by omega, by simp only [HKind.bits]; omega⟩ h + have hfv := (J.sr.core.tcbs v hv').fits + have other : ∀ K' : U64, K'.val < 4096 → K.val ≠ K'.val → wa u K ≠ wa v K' := by + intro K' k1 k2 h + have hv'' : (wa v K').val = v.val + K'.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have h' := congrArg (fun w : U64 => w.val) h + simp only [hv, hv''] at h' + by_cases e : v = u + · subst e; omega + · have := J.sr.place.apart v u .tcb .tcb ((J.sr.thT v).2 hv') ((J.sr.thT u).2 hu) e + have hsz : ObjKindA.size .tcb = 4096 := rfl + omega + rcases e with e | e | e + · exact other _ (by simp) (by simpa using h7.1) e + · exact other _ (by simp) (by simpa using h7.2.1) e + · exact other _ (by simp) (by simpa using h7.2.2) e + · have := J.sr.tcb_ks hu + unfold SKsA at h + unfold ksSizeA at this + omega + +/-- No notification word is written by the cancel. -/ +theorem JStage3BA.cancelFoot_ntfnOffA + (J : JStage3BA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {t q b : U64} (he : L.mem s (Notif.tBlockA t) ∈ E) (ht : t ∈ Z.T) (hq : q ∈ N) + (lo : q.val ≤ b.val) (hi : b.val < q.val + 40) : + ¬ CancelFootA st Rp Z.T s t b := by + have hP := J.iplaced + have hw : IpcObjWA N E Rp b := Or.inl ⟨q, hq, lo, by omega⟩ + have hQ : {q | q ∈ Z.T ∧ L.mem s (Notif.tBlockA q) = L.mem s (Notif.tBlockA t)} ⊆ Z.T := fun _ h => h.1 + rintro (h | h | h) + · have hta := hP.e.tcb t ht + have hbv : b.val = t.val + 3376 := by rw [h]; exact val_fault hta + have := hP.n.disj q hq t ht + omega + · rcases h with h | ⟨r, hr, e⟩ | ⟨u, hu, h⟩ + · unfold EW at h + have := hP.ne q hq _ he + omega + · have : b.val = r.val := by rw [e, Endp.rTcbA_val] + have := hP.nr q hq r hr + omega + · have huT := hQ hu + have hta := hP.e.tcb u huT + have := hP.n.disj q hq u huT + rcases h with h | h + · have : b.val = u.val + 3328 := by rw [h, Endp.val_st hta] + omega + · unfold TL at h; omega + · have := J.ipc_ksA hw + unfold SKsA at h + unfold ksSizeA at this + omega + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3CancelLoopA.lean b/proofs/Fl4ProofsA64/P3/Stage3CancelLoopA.lean new file mode 100644 index 000000000..76e88a5f9 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3CancelLoopA.lean @@ -0,0 +1,285 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2): **the footprint-and-keep sibling of `cancel_badged_sends`' loop** (`P3/EpCancelA`'s +`badged_deq` / `badged_loop` / `cancel_badged_rel`, whose statements are unchanged), as `P3/DelEpLoopA` for +`cancel_all_ipc`. `cancel_badged_relF` adds `LKeepA` (spec keep, SC sizes, the write set, `LoopInvA`), `BlkMonoA` on the +endpoint, and "each thread queued before is unqueued or still queued". + +No named premise (the interfaces are `SchedOpsEA` / `SchedFootEA`); new module. +-/ +import Fl4ProofsA64.P3.DelEpLoopA +import Fl4ProofsA64.P3.DelBlkKeepA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {T E R : Set U64} + {Bf : Set (U64 × Fl4SpecA64.Word)} {SR : S → Fl4SpecA64.State Unit → Prop} + {Wr : Fl4SpecA64.State Unit → S → U64 → Prop} + +/-- The thread at `r` is unqueued, or still in the queue of `y`. -/ +def BQueuedA (σ : Fl4SpecA64.State Unit) (y : Fl4SpecA64.Endpoint) (r : Fl4SpecA64.ObjRef) : Prop := + NoBA σ r ∨ r ∈ y.epQueue + +variable (st T E R Bf SR Wr) in +/-- `BInv` with the keep from the loop's entry, the whole queue inside `Q`, and the blocking facts. -/ +def BInvF (Q : Set U64) (e b : U64) (σ0 : Fl4SpecA64.State Unit) (s0 : S) (q0 : List Fl4SpecA64.ObjRef) + (x : S × U64 × U64) : Prop := + ∃ σ' y, ∃ kept rest : List U64, EpStateRel T E R Bf (L.mem x.1) σ' ∧ SR x.1 σ' ∧ + σ'.kheap (specPtr e) = some (.endpoint y) ∧ + y.epQueue = (kept ++ rest).map specPtr ∧ (∀ q, y ≠ .recvEP q) ∧ x.2.1 = rest.headD 0#u64 ∧ + rest.length ≤ x.2.2.val ∧ (∀ t ∈ rest, t ∈ T → L.mem x.1 (affA t) = L.cpu x.1) ∧ + (∀ z, z ∈ (Fl4Spec.NonDet.mapMx (rrb e b) (rest.map specPtr) σ').1 → + z ∈ (Fl4Spec.NonDet.mapMx (rrb e b) q0 σ0).1) ∧ + ((Fl4Spec.NonDet.mapMx (rrb e b) q0 σ0).2 → (Fl4Spec.NonDet.mapMx (rrb e b) (rest.map specPtr) σ').2) ∧ + LKeepA st R Wr Q e σ0 s0 σ' x.1 ∧ (∀ t ∈ kept ++ rest, t ∈ Q) ∧ BlkMonoA (specPtr e) σ0 σ' ∧ + (∀ r ∈ q0, BQueuedA σ' y r) ∧ y.epQueue.Sublist q0 + +/-- **One dequeue-and-restart step of the loop**, with the footprint and keep. -/ +theorem badged_deqF (hO : SchedOpsEA st T E R Bf SR) (hF : SchedFootEA st T E R Bf SR Wr) (hP : EPlaced T E R Bf) + {Q : Set U64} {e b : U64} (he : e ∈ E) (σ0 : Fl4SpecA64.State Unit) (s0 : S) (q0 : List Fl4SpecA64.ObjRef) + {s : S} {σ' : Fl4SpecA64.State Unit} {y : Fl4SpecA64.Endpoint} {kept rest' : List U64} {t0 nx fuel1 : U64} + (hR : EpStateRel T E R Bf (L.mem s) σ') (hSR : SR s σ') (hky : σ'.kheap (specPtr e) = some (.endpoint y)) + (hq : y.epQueue = (kept ++ t0 :: rest').map specPtr) (hnr : ∀ q, y ≠ .recvEP q) + (hQ : QueueRel (L.mem s) T .Ep (qOf (L.mem s) e) (kept ++ t0 :: rest')) (ht0T : t0 ∈ T) {xt : Fl4SpecA64.Tcb} + (hkt : σ'.kheap (specPtr t0) = some (.tcb xt)) {d : Fl4SpecA64.SenderPayload} + (hst : xt.tcbState = .blockedOnSend (specPtr e) d) (hbe : L.mem s (tBlockA t0) = e) (hb : d.senderBadge = b.bv) + (hnx : nx = rest'.headD 0#u64) (hlen : rest'.length ≤ fuel1.val) + (hon : ∀ t ∈ t0 :: rest', t ∈ T → L.mem s (affA t) = L.cpu s) + (hinc : ∀ z, z ∈ (Fl4Spec.NonDet.mapMx (rrb e b) ((t0 :: rest').map specPtr) σ').1 → + z ∈ (Fl4Spec.NonDet.mapMx (rrb e b) q0 σ0).1) + (hfail : (Fl4Spec.NonDet.mapMx (rrb e b) q0 σ0).2 → + (Fl4Spec.NonDet.mapMx (rrb e b) ((t0 :: rest').map specPtr) σ').2) + (K : LKeepA st R Wr Q e σ0 s0 σ' s) (hqs : ∀ t ∈ kept ++ t0 :: rest', t ∈ Q) + (M : BlkMonoA (specPtr e) σ0 σ') (hbq : ∀ r ∈ q0, BQueuedA σ' y r) (hsub : y.epQueue.Sublist q0) : + (do + let s1 ← endpoint.ep_dequeue st s e t0 + let s2 ← endpoint.restart_thread_if_no_fault st s1 t0 + ok (ControlFlow.cont (s2, nx, fuel1) : ControlFlow (S × U64 × U64) S)) ⦃ r => + ∃ x' : S × U64 × U64, r = (ControlFlow.cont x' : ControlFlow (S × U64 × U64) S) ∧ + BInvF st T E R Bf SR Wr Q e b σ0 s0 q0 x' ∧ x'.2.2 = fuel1 ⦄ := by + have hd : specPtr t0 ∈ y.epQueue := by rw [hq]; simp + have ht0Q : t0 ∈ Q := hqs t0 (by simp) + refine WP.spec_bind (WP.spec_and (ep_dequeue_rel hO hP s σ' hR hSR he hky hd) + (ep_dequeue_sigE hP s he t0 (kept ++ t0 :: rest') hQ (by simp) (Q := Q) hqs)) ?_ + rintro s1 ⟨⟨hdq, hR1, hS1, hA1⟩, hw1⟩ + have hte : specPtr t0 ≠ specPtr e := by intro h; rw [h, hky] at hkt; cases hkt + have hktd : (updK σ' (specPtr e) (.endpoint (epDeq y (specPtr t0)))).kheap (specPtr t0) = some (.tcb xt) := by + rw [updK_kheap, Function.update_of_ne hte, hkt] + have hkyd : (updK σ' (specPtr e) (.endpoint (epDeq y (specPtr t0)))).kheap (specPtr e) = + some (.endpoint (epDeq y (specPtr t0))) := by rw [updK_kheap, Function.update_self] + have hta := hP.tcb t0 ht0T + have hb1 : L.mem s1 (tBlockA t0) = e := (hA1 _ (a_blk hta).1 ⟨t0, ht0T, (a_blk hta).2⟩).trans hbe + obtain ⟨hrq, hnq, hdnr⟩ := epDeq_send_rm hnr hq hQ.nodup + have hq1 := dequeued_not_queued hR1 he hb1 hkyd hnq + have K1 : LKeepA st R Wr Q e σ0 s0 (updK σ' (specPtr e) (.endpoint (epDeq y (specPtr t0)))) s1 := + ⟨K.keep.trans (EpSpecKeepA.of_upd hky ⟨_, rfl⟩), K.sz.trans (ssz_of_epStep (EpStep.ep hky)), + FpEA.trans hF.to0 K.fp K.sz (FpEA.of_sw hw1), fun m n h => (K.inv m n h).updEp hky⟩ + have hc : tsCode xt.tcbState = 3 ∨ tsCode xt.tcbState = 4 := Or.inr (by rw [hst]; rfl) + have hnn : (∀ n, xt.tcbState ≠ .blockedOnNotification n) ∧ ∀ r, xt.tcbState ≠ .blockedOnReply r := + ⟨(fun n h => by rw [hst] at h; cases h), (fun r h => by rw [hst] at h; cases h)⟩ + refine WP.spec_bind (restart_sigE hO hF hP s1 _ hR1 hS1 ht0T hktd hq1 + (RestartOK.of_blk ⟨Or.inr (Or.inr (Or.inl ⟨_, _, hst⟩)), by rw [hst]; rfl⟩) + ((hO.coreKeep _ _ _ _ hSR hS1).onCore ht0T (hon t0 List.mem_cons_self ht0T)) hnn (Q := Q) (e := e) ht0Q) ?_ + rintro s2 ⟨hnf, σ'', hres, hR2, hS2, hkeep, -, hK, hZ, hW, hI⟩ + rw [WP.spec_ok] + have heq : rrb e b (specPtr t0) σ' = _ := rrb_deq_eq hkt hst hb hdq + have Md : BlkMonoA (specPtr e) σ' (updK σ' (specPtr e) (.endpoint (epDeq y (specPtr t0)))) := + tcbEpDequeue_blkA (specPtr t0) (specPtr e) σ' ((), _) (by rw [hdq]; exact rfl) + have Mr : BlkMonoA (specPtr e) (updK σ' (specPtr e) (.endpoint (epDeq y (specPtr t0)))) σ'' := + restartThreadIfNoFault_blkA (specPtr e) (specPtr t0) _ ((), σ'') hres + have n0 : NoBA σ'' (specPtr t0) := restartThreadIfNoFault_noBA (r := ((), σ'')) hres + have K2 : LKeepA st R Wr Q e σ0 s0 σ'' s2 := + ⟨K1.keep.trans hK, K1.sz.trans hZ, FpEA.trans hF.to0 K1.fp K1.sz hW, + fun m n h => hI m n (K1.inv m n h) ((K1.inv m n h).offCur hktd hc)⟩ + have hbq' : ∀ r ∈ q0, BQueuedA σ'' (epDeq y (specPtr t0)) r := fun r hr => by + rcases hbq r hr with h | h + · exact Or.inl ((h.mono Md).mono Mr) + · by_cases c : r = specPtr t0 + · exact Or.inl (c ▸ n0) + · refine Or.inr ?_ + rw [epDeq_queue] + unfold Fl4SpecA64.removeAll + exact List.mem_filter.2 ⟨h, decide_eq_true (Ne.symm c)⟩ + refine ⟨_, rfl, ⟨σ'', epDeq y (specPtr t0), kept, rest', hR2, hS2, hkeep _ _ hkyd, hrq, hdnr, hnx, hlen, + fun t ht hT => (hO.coreKeep _ _ _ _ hSR hS2).onCore hT (hon t (List.mem_cons_of_mem _ ht) hT), + fun z hz => hinc z (mapMx_cons_mem (g := rrb e b) (l := rest'.map specPtr) (rrb_deq_mem hkt hst hb hdq hres) z hz), + fun hw => mapMx_cons_fail (l := rest'.map specPtr) (by rw [heq]; exact hnf) (by rw [heq]; exact hres) + (hfail hw), K2, fun t ht => hqs t ?_, M.trans (Md.trans Mr), hbq', by rw [epDeq_queue]; unfold Fl4SpecA64.removeAll; exact List.filter_sublist.trans hsub⟩, rfl⟩ + rcases List.mem_append.1 ht with h | h + · exact List.mem_append_left _ h + · exact List.mem_append_right _ (List.mem_cons_of_mem _ h) + +set_option maxHeartbeats 4000000 in +/-- **The loop of `cancel_badged_sends`** on `EpStateRel ∧ SR`, with the footprint and keep. -/ +theorem badged_loopF (hO : SchedOpsEA st T E R Bf SR) (hF : SchedFootEA st T E R Bf SR Wr) (hP : EPlaced T E R Bf) + {Q : Set U64} {e b : U64} (he : e ∈ E) (σ0 : Fl4SpecA64.State Unit) (s0 : S) (q0 : List Fl4SpecA64.ObjRef) + (x0 : S × U64 × U64) (h0 : BInvF st T E R Bf SR Wr Q e b σ0 s0 q0 x0) : + endpoint.cancel_badged_sends_loop st x0.1 e b x0.2.1 x0.2.2 ⦃ s' => ¬ (Fl4Spec.NonDet.mapMx (rrb e b) q0 σ0).2 ∧ + ∃ σf, ((), σf) ∈ (Fl4Spec.NonDet.mapMx (rrb e b) q0 σ0).1 ∧ EpStateRel T E R Bf (L.mem s') σf ∧ SR s' σf ∧ + LKeepA st R Wr Q e σ0 s0 σf s' ∧ BlkMonoA (specPtr e) σ0 σf ∧ + ∃ y, σf.kheap (specPtr e) = some (.endpoint y) ∧ (∀ r ∈ q0, BQueuedA σf y r) ∧ y.epQueue.Sublist q0 ⦄ := by + unfold endpoint.cancel_badged_sends_loop + apply loop.spec_decr_nat (measure := fun x : S × U64 × U64 => x.2.2.val) + (inv := BInvF st T E R Bf SR Wr Q e b σ0 s0 q0) _ _ x0 ?_ h0 + rintro ⟨s, t, fuel⟩ ⟨σ', y, kept, rest, hR, hSR, hky, hq, hnr, ht, hlen, hon, hinc, hfail, K, hqs, M, hbq, hsub⟩ + simp only at hR hSR hky hq hnr ht hlen hon hinc hfail K hqs M hbq hsub ⊢ + have hEP := hR.at_ep he hky + obtain ⟨xs, hQ, hxs, hB⟩ := hEP.queue + have hxs' : xs = kept ++ rest := by + rw [hq] at hxs + exact (List.map_injective_iff.2 (fun a b h => specPtr_inj h) hxs).symm + subst hxs' + unfold endpoint.cancel_badged_sends_loop.body + step with obj.Tcb.is_null.spec as ⟨bn, hbn⟩ + cases rest with + | nil => + simp only [List.headD_nil] at ht + subst ht + simp only [hbn, decide_true, ↓reduceIte, WP.spec_ok] + exact ⟨fun h => hfail h, σ', hinc _ rfl, hR, hSR, K, M, y, hky, hbq, hsub⟩ + | cons t0 rest' => + simp only [List.headD_cons] at ht + subst t + have ht0T : t0 ∈ T := hQ.mem t0 (by simp) + have ht00 : t0 ≠ 0#u64 := hQ.ne0 hP.tcb t0 (by simp) + simp only [hbn, ht00, decide_false, Bool.false_eq_true, ↓reduceIte] + have hf0 : fuel > 0#u64 := by + simp only [List.length_cons] at hlen + show (0#u64 : U64).val < fuel.val; simp; omega + simp only [hf0, ↓reduceIte] + step as ⟨fuel1, hfuel1⟩ + have hseg := hQ.seg + rw [Seg_mid] at hseg + obtain ⟨-, -, hnext, -⟩ := hseg + unfold tcbqueue.next_of + step with tcbqueue.next.spec as ⟨nx, hnx⟩ + try simp only [bind_ok] + obtain ⟨hbe, hcode⟩ := hB t0 (by simp) + obtain ⟨xt, hkt, hTRE⟩ := hR.tcb t0 ht0T + have h4 : (L.mem s (stA t0)).val = 4 := by + cases y with + | idleEP => simp [Fl4SpecA64.Endpoint.epQueue] at hq + | recvEP q => exact absurd rfl (hnr q) + | sendEP q => + obtain ⟨-, ys, -, hys, hQ', hqo⟩ := hEP + have := hQ'.unique hP.tcb hQ + rw [this] at hqo + exact (hqo t0 (by simp)).1 + obtain ⟨e', d, hst⟩ : ∃ e' d, xt.tcbState = .blockedOnSend e' d := by + have := hTRE.st; rw [h4] at this + exact tsCode_four this.symm + have he' : e' = specPtr e := by + rw [← hTRE.blk e' (by simp [hst, Fl4SpecA64.epBlocked]), hbe] + subst he' + have hbdg : (L.mem s (tBadgeA t0)).bv = d.senderBadge := (hTRE.send _ d hst).1 + step with obj.Tcb.block_badge.spec as ⟨i, hi⟩ + subst hi + by_cases hm : L.mem s (tBadgeA t0) = b + · simp only [hm, ↓reduceIte] + have hb' : d.senderBadge = b.bv := by rw [← hbdg, hm] + refine WP.spec_mono (badged_deqF hO hF hP he σ0 s0 q0 hR hSR hky hq hnr hQ ht0T hkt hst hbe hb' + (by rw [hnx, hnext]) (by rw [hfuel1]; simp only [List.length_cons] at hlen; scalar_tac) hon hinc hfail K hqs + M hbq hsub) ?_ + rintro r ⟨x', rfl, hI, hf⟩ + exact ⟨hI, by rw [hf, hfuel1]; scalar_tac⟩ + · simp only [hm, ↓reduceIte, WP.spec_ok] + have hkq : rrb e b (specPtr t0) σ' = ({((), σ')}, False) := + rrb_keep_eq hkt hst (fun h => hm (eq_of_bv (hbdg.trans h))) + refine ⟨⟨σ', y, kept ++ [t0], rest', hR, hSR, hky, by rw [hq]; simp, hnr, by rw [hnx, hnext], ?_, + fun t ht hT => hon t (List.mem_cons_of_mem _ ht) hT, ?_, ?_, K, fun t ht => hqs t (by simpa using ht), M, + hbq, hsub⟩, ?_⟩ + · rw [hfuel1]; simp only [List.length_cons] at hlen; scalar_tac + · exact fun z hz => hinc z (mapMx_cons_mem (g := rrb e b) (l := rest'.map specPtr) + (rrb_keep_mem hkt hst (fun h => hm (eq_of_bv (hbdg.trans h)))) z hz) + · exact fun hw => mapMx_cons_fail (l := rest'.map specPtr) (by rw [hkq]; exact fun h => h) + (by rw [hkq]; rfl) (hfail hw) + · rw [hfuel1]; scalar_tac + +set_option maxHeartbeats 4000000 in +/-- **`cancel_badged_sends` refines `cancel_badged_sends`** on `EpStateRel ∧ SR` (`cancel_badged_rel`), with the write +set inside `Q`, the keep, `BlkMonoA`, and the queue facts. -/ +theorem cancel_badged_relF (hO : SchedOpsEA st T E R Bf SR) (hF : SchedFootEA st T E R Bf SR Wr) + (hP : EPlaced T E R Bf) (s : S) (σ : Fl4SpecA64.State Unit) (hR : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) + {e : U64} (he : e ∈ E) (b : U64) + (hon : ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → ∀ t ∈ T, specPtr t ∈ z.epQueue → + L.mem s (affA t) = L.cpu s) + {Q : Set U64} (hQs : ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → ∀ t ∈ T, specPtr t ∈ z.epQueue → t ∈ Q) : + endpoint.cancel_badged_sends st s e b ⦃ s' => ¬ (Fl4SpecA64.cancelBadgedSends (Z := Unit) (specPtr e) b.bv σ).2 ∧ + ∃ σ', ((), σ') ∈ (Fl4SpecA64.cancelBadgedSends (Z := Unit) (specPtr e) b.bv σ).1 ∧ + EpStateRel T E R Bf (L.mem s') σ' ∧ SR s' σ' ∧ LKeepA st R Wr Q e σ s σ' s' ∧ BlkMonoA (specPtr e) σ σ' ∧ + (∀ y, σ.kheap (specPtr e) = some (.endpoint y) → ∀ r ∈ y.epQueue, + NoBA σ' r ∨ ∃ y', σ'.kheap (specPtr e) = some (.endpoint y') ∧ r ∈ y'.epQueue) ∧ + (∀ y, σ.kheap (specPtr e) = some (.endpoint y) → + ∃ y', σ'.kheap (specPtr e) = some (.endpoint y') ∧ y'.epQueue.Sublist y.epQueue) ⦄ := by + obtain ⟨y, hky, hEP⟩ := hR.ep e he + have same : ∀ y0, σ.kheap (specPtr e) = some (.endpoint y0) → ∀ r ∈ y0.epQueue, + NoBA σ r ∨ ∃ y', σ.kheap (specPtr e) = some (.endpoint y') ∧ r ∈ y'.epQueue := + fun y0 h r hr => Or.inr ⟨y0, h, hr⟩ + have sameS : ∀ y0, σ.kheap (specPtr e) = some (.endpoint y0) → + ∃ y', σ.kheap (specPtr e) = some (.endpoint y') ∧ y'.epQueue.Sublist y0.epQueue := + fun y0 h => ⟨y0, h, List.Sublist.refl _⟩ + unfold endpoint.cancel_badged_sends + step with obj.Ep.state.spec as ⟨i, hi⟩ + rw [epState_eq] at hi + have hsp : Fl4SpecA64.cancelBadgedSends (Z := Unit) (specPtr e) b.bv σ = (match y with + | .sendEP queue => (do + Fl4Spec.NonDet.mapMx (fun t => Fl4SpecA64.removeAndRestartBadgedThread t (specPtr e) b.bv) queue + Fl4SpecA64.rescheduleRequired) + | _ => return ()) σ := by + unfold Fl4SpecA64.cancelBadgedSends + rw [nd_bind_one (getEndpoint_eq hky)] + rfl + cases y with + | idleEP => + have h0 : i ≠ obj.EP_SEND := by rw [hi, hEP.1, ep_state_vals.2.1]; decide + simp only [h0, bne_iff_ne, ne_eq, not_false_eq_true, ↓reduceIte, WP.spec_ok] + exact ⟨by rw [hsp]; exact fun h => h, σ, by rw [hsp]; rfl, hR, hSR, LKeepA.refl σ s, BlkMonoA.refl _ σ, same, sameS⟩ + | recvEP q => + have h0 : i ≠ obj.EP_SEND := by rw [hi, hEP.1, ep_state_vals.2.1]; decide + simp only [h0, bne_iff_ne, ne_eq, not_false_eq_true, ↓reduceIte, WP.spec_ok] + exact ⟨by rw [hsp]; exact fun h => h, σ, by rw [hsp]; rfl, hR, hSR, LKeepA.refl σ s, BlkMonoA.refl _ σ, same, sameS⟩ + | sendEP q => + have h0 : i = obj.EP_SEND := by rw [hi, hEP.1, ep_state_vals.2.1] + simp only [h0, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + obtain ⟨-, xs, -, hqx, hQ, -⟩ := hEP + step with endpoint.get_queue.spec as ⟨qq, hqq⟩ + have hfuel : layout.WALK_FUEL.val = 2 ^ 64 - 1 := by simp [layout.WALK_FUEL, U64.rMax] + have hI0 : BInvF st T E R Bf SR Wr Q e b σ s q (s, qq.head, layout.WALK_FUEL) := + ⟨σ, .sendEP q, [], xs, hR, hSR, hky, by rw [hqx]; rfl, (fun q' e' => by cases e'), + by rw [hqq, epQ_eq, hQ.head], by rw [hfuel]; exact len_le_max xs hQ.nodup (hQ.ne0 hP.tcb), + fun t ht hT => hon _ hky t hT (by show specPtr t ∈ q; rw [hqx]; exact List.mem_map.2 ⟨t, ht, rfl⟩), + fun z hz => by rw [hqx]; exact hz, fun h => by rw [← hqx]; exact h, LKeepA.refl σ s, + fun t ht => hQs _ hky t (hQ.mem t ht) (by show specPtr t ∈ q; rw [hqx]; exact List.mem_map.2 ⟨t, ht, rfl⟩), + BlkMonoA.refl _ σ, fun r hr => Or.inr hr, List.Sublist.refl _⟩ + refine WP.spec_bind (badged_loopF hO hF hP he σ s q _ hI0) ?_ + rintro s1 ⟨hnf, σf, hmf, hRf, hSf, Kf, Mf, yf, hkf, hbf, hsf⟩ + refine WP.spec_mono (WP.spec_and (hO.reschedule s1 σf hRf hSf) (hF.reschedule s1 σf hRf hSf)) ?_ + rintro s2 ⟨⟨hnr2, σr, hrs, hf2, hv2, hS2⟩, hw2⟩ + have Ms : BlkMonoA (specPtr e) σf σr := BlkMonoA.of_scOnly rescheduleRequired_tkA σf ((), σr) hrs + have hkr : σr.kheap (specPtr e) = some (.endpoint yf) := + (rescheduleRequired_tkA σf ((), σr) hrs).1 _ _ hkf (fun _ _ e => by cases e) + refine ⟨?_, σr, ?_, hRf.frame hP hf2 hv2, hS2, + ⟨Kf.keep.trans (rescheduleRequired_epkA hrs), Kf.sz.trans (rescheduleRequired_ssz hrs), + FpEA.trans hF.to0 Kf.fp Kf.sz (FpEA.of_wr hw2), fun m n h => (Kf.inv m n h).reschedule hrs⟩, + Mf.trans Ms, fun y0 hy0 r hr => ?_, fun y0 hy0 => ?_⟩ + · rw [hsp] + exact fun h => hnr2 (Det.detAt_bind_fail ((Det.det_mapMx (fun a => Det.det_removeAndRestartBadged a _ _) q).at σ) + hnf hmf h) + · rw [hsp] + exact ⟨((), σf), hmf, hrs⟩ + · rw [hky] at hy0; cases hy0 + rcases hbf r hr with h | h + · exact Or.inl (h.mono Ms) + · exact Or.inr ⟨yf, hkr, h⟩ + · rw [hky] at hy0; cases hy0 + exact ⟨yf, hkr, hsf⟩ + +end + +end + +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/Stage3CancelRecvPInvA.lean b/proofs/Fl4ProofsA64/P3/Stage3CancelRecvPInvA.lean new file mode 100644 index 000000000..4fb7c9c78 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3CancelRecvPInvA.lean @@ -0,0 +1,144 @@ +/- +P3 (aarch64, A64-P3 sigwake2): **`SInvXA ∧ SIdleA` across `cancel_ipc` of a receiver, without `SActA`** +(`SPairA.cancelRecv`). The cancel of a receiver commutes with `hatA` (no switch pending there, so `set_thread_state`'s +action step does nothing), so the object steps are `cancelIpc_invA`'s own at `hatA σ`. No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3WakePInvA +import Fl4ProofsA64.P3.DelSuspendSpecA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open Fl4Spec Fl4SpecA64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Endp + +noncomputable section + +/-- At `hatA` the action step does nothing, and it only changes the action. -/ +theorem stsAct_hatA (σ : State Unit) (t : ObjRef) : stsAct (hatA σ) t = hatA (stsAct σ t) := by + have e1 : stsAct (hatA σ) t = hatA σ := by + unfold stsAct; exact if_neg (fun c => by cases c.2.1) + rw [e1]; unfold stsAct; split_ifs <;> rfl + +/-- The cancel of a receiver at `σ` and at `hatA σ`. -/ +theorem cancelIpc_recv_hatA {σ : State Unit} {t e : ObjRef} {x : Tcb} (hk : σ.kheap t = some (.tcb x)) + {ro : Option ObjRef} {d : ReceiverPayload} (hs : x.tcbState = .blockedOnReceive e ro d) {z : Endpoint} + (hkz : σ.kheap e = some (.endpoint z)) (hq : t ∈ z.epQueue) (hrep : RepOKA σ t) : + ∃ τ, cancelIpc (Z := Unit) t σ = ({((), τ)}, False) ∧ + cancelIpc (Z := Unit) t (hatA σ) = ({((), hatA τ)}, False) ∧ + (τ.schedulerAction = .resumeCurThread → σ.schedulerAction = .resumeCurThread) ∧ + τ.readyQueues = σ.readyQueues ∧ τ.curThread = σ.curThread ∧ + (∀ q y, σ.kheap q = some (.tcb y) → ∃ y', τ.kheap q = some (.tcb y')) ∧ τ.machineState = σ.machineState := by + have het : e ≠ t := by intro c; rw [c, hk] at hkz; cases hkz + let σ1 := updK σ t (.tcb { x with tcbFault := none }) + have hkz1 : σ1.kheap e = some (.endpoint z) := by + simp only [σ1, updK_kheap, Function.update_of_ne het]; exact hkz + have hdq := tcbEpDequeue_eq (Z := Unit) hkz1 hq + have hdqh := tcbEpDequeue_eq (Z := Unit) (σ := hatA σ1) (p := e) (x := z) (t := t) hkz1 hq + let σ2 := updK σ1 e (.endpoint (epDeq z t)) + have hk2 : σ2.kheap t = some (.tcb { x with tcbFault := none }) := by + simp only [σ2, σ1, updK_kheap, Function.update_of_ne het.symm, Function.update_self] + have sa : ∀ (τ : State Unit) (u : ObjRef), (stsAct τ u).kheap = τ.kheap ∧ + (stsAct τ u).readyQueues = τ.readyQueues ∧ (stsAct τ u).curThread = τ.curThread ∧ + ((stsAct τ u).schedulerAction = .resumeCurThread → τ.schedulerAction = .resumeCurThread) ∧ + (stsAct τ u).machineState = τ.machineState := by + intro τ u + unfold stsAct; split_ifs + · exact ⟨rfl, rfl, rfl, fun c => (by cases c), rfl⟩ + · exact ⟨rfl, rfl, rfl, id, rfl⟩ + have tcbU : ∀ (f : ObjRef → Option KernelObject) (q p : ObjRef) (v : Tcb), (∃ y, f q = some (.tcb y)) → + ∃ y, Function.update f p (some (.tcb v)) q = some (.tcb y) := fun f q p v hf => by + by_cases c : q = p + · subst c; exact ⟨v, Function.update_self _ _ _⟩ + · rw [Function.update_of_ne c]; exact hf + have tcbO : ∀ (f : ObjRef → Option KernelObject) (q p : ObjRef) (o : KernelObject), q ≠ p → + (∃ y, f q = some (.tcb y)) → ∃ y, Function.update f p (some o) q = some (.tcb y) := fun f q p o c hf => by + rw [Function.update_of_ne c]; exact hf + cases ro with + | none => + have hb : epBlocked x.tcbState = some e := by rw [hs]; rfl + refine ⟨_, cancelIpc_blocked hk none (Or.inr ⟨e, d, hs⟩) (bci_none hb hdq hk2), ?_, ?_, ?_, ?_, ?_, ?_⟩ + · rw [← stsAct_hatA] + exact cancelIpc_blocked (σ := hatA σ) hk none (Or.inr ⟨e, d, hs⟩) (bci_none hb hdqh hk2) + · intro c + have c1 := (sa _ _).2.2.2.1 c + exact c1 + · rw [(sa _ _).2.1]; rfl + · rw [(sa _ _).2.2.1]; rfl + · intro q y hy + rw [(sa _ _).1] + refine tcbU _ _ _ _ (tcbO _ _ _ _ (fun c => ?_) (tcbU _ _ _ _ ⟨y, hy⟩)) + rw [c, hkz] at hy; cases hy + · rw [(sa _ _).2.2.2.2]; rfl + | some rp => + obtain ⟨xr, hkr, hback⟩ := hrep x hk e rp d hs + have hrt : rp ≠ t := by intro c; rw [c, hk] at hkr; cases hkr + have hre : rp ≠ e := by intro c; rw [c, hkz] at hkr; cases hkr + have hkr2 : σ2.kheap rp = some (.reply xr) := by + simp only [σ2, σ1, updK_kheap, Function.update_of_ne hre, Function.update_of_ne hrt]; exact hkr + have hun := replyUnlinkTcb_eq hkr2 hback hk2 (ep := e) (d := d) (by exact hs) + have hunh := replyUnlinkTcb_eq (σ := hatA σ2) hkr2 hback hk2 (ep := e) (d := d) (by exact hs) + let σ3 := updK σ2 rp (.reply { xr with replyTcb := none }) + have hk3 : σ3.kheap t = some (.tcb { x with tcbFault := none }) := by + simp only [σ3, updK_kheap, Function.update_of_ne hrt.symm]; exact hk2 + let x1 : Tcb := { { x with tcbFault := none } with tcbState := .inactive } + let σ4 := stsAct (updK σ3 t (.tcb x1)) t + have hk4 : σ4.kheap t = some (.tcb x1) := by simp only [σ4, stsAct_kheap, updK_kheap, Function.update_self] + have e4 : stsAct (updK (updK (hatA σ2) rp (.reply { xr with replyTcb := none })) t (.tcb x1)) t = hatA σ4 := + stsAct_hatA (updK σ3 t (.tcb x1)) t + refine ⟨_, cancelIpc_blocked hk (some rp) (Or.inr ⟨e, d, hs⟩) (bci_reply (by rw [hs]; rfl) hdq hun hk4), + ?_, ?_, ?_, ?_, ?_, ?_⟩ + · rw [← stsAct_hatA] + refine cancelIpc_blocked (σ := hatA σ) hk (some rp) (Or.inr ⟨e, d, hs⟩) + (bci_reply (ts := x.tcbState) (x4 := x1) (σ4 := hatA σ4) (show epBlocked x.tcbState = some e by rw [hs]; rfl) hdqh + (hunh.trans (by rw [e4])) hk4) + · intro c + have c1 := (sa _ _).2.2.2.1 c + have c2 := (sa _ _).2.2.2.1 c1 + exact c2 + · rw [(sa _ _).2.1]; exact (sa (updK σ3 t (.tcb x1)) t).2.1 + · rw [(sa _ _).2.2.1]; exact (sa (updK σ3 t (.tcb x1)) t).2.2.1 + · intro q y hy + rw [(sa _ _).1] + refine tcbU _ _ _ _ ?_ + show ∃ y, σ4.kheap q = some (.tcb y) + rw [show σ4.kheap = (updK σ3 t (.tcb x1)).kheap from (sa _ _).1] + refine tcbU _ _ _ _ (tcbO _ _ _ _ (fun c => ?_) (tcbO _ _ _ _ (fun c => ?_) (tcbU _ _ _ _ ⟨y, hy⟩))) + · rw [c, hkr] at hy; cases hy + · rw [c, hkz] at hy; cases hy + · rw [(sa _ _).2.2.2.2]; exact (sa (updK σ3 t (.tcb x1)) t).2.2.2.2 + +/-- **The cancel of a receiver keeps the machine state.** -/ +theorem cancelIpc_recv_msA {σ σ' : State Unit} {t e : ObjRef} {x : Tcb} (hk : σ.kheap t = some (.tcb x)) + {ro : Option ObjRef} {d : ReceiverPayload} (hs : x.tcbState = .blockedOnReceive e ro d) {z : Endpoint} + (hkz : σ.kheap e = some (.endpoint z)) (hq : t ∈ z.epQueue) (hrep : RepOKA σ t) + (hr : ((), σ') ∈ (cancelIpc (Z := Unit) t σ).1) : σ'.machineState = σ.machineState := by + obtain ⟨τ, h1, -, -, -, -, -, hm⟩ := cancelIpc_recv_hatA hk hs hkz hq hrep + rw [h1] at hr + have er : σ' = τ := congrArg Prod.snd (show ((), σ') = ((), τ) from hr) + rw [er]; exact hm + +theorem SPairA.cancelRecv {σ : State Unit} {m : McState} {n : Nat} (h : SPairA σ m n) {t e : ObjRef} {x : Tcb} + (hk : σ.kheap t = some (.tcb x)) {ro : Option ObjRef} {d : ReceiverPayload} + (hs : x.tcbState = .blockedOnReceive e ro d) {z : Endpoint} (hkz : σ.kheap e = some (.endpoint z)) + (hq : t ∈ z.epQueue) (hrep : RepOKA σ t) + {σ' : State Unit} (hr : ((), σ') ∈ (cancelIpc (Z := Unit) t σ).1) : SPairA σ' m n := by + obtain ⟨τ, h1, h2, ha, hrq, hct, htcb, -⟩ := cancelIpc_recv_hatA hk hs hkz hq hrep + rw [h1] at hr + have er : σ' = τ := congrArg Prod.snd (show ((), σ') = ((), τ) from hr) + rw [er] + have H := (cancelIpc_invA (σ := hatA σ) (m := m) (n := n) (t := t) (x := x) h.hat (fun u c => by cases c) hk + (fun e1 c => by + rw [hs] at c + cases c + exact ⟨z, hkz, hq⟩) + (fun n' c => by rw [hs] at c; cases c) hrep (r := ((), hatA τ)) + (by rw [h2]; exact Set.mem_singleton _)).1 + refine SPairA.ofHat H ?_ (fun c dm p => ?_) + · obtain ⟨y, hy⟩ := h.sinv.curTcb + obtain ⟨y', hy'⟩ := htcb _ _ hy + exact ⟨y', by rw [hct]; exact hy'⟩ + · rw [hct, hrq]; exact h.sinv.ctQ (ha c) dm p + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3CancelReplyPA.lean b/proofs/Fl4ProofsA64/P3/Stage3CancelReplyPA.lean new file mode 100644 index 000000000..68e4c87a6 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3CancelReplyPA.lean @@ -0,0 +1,302 @@ +/- +P3 (aarch64, A64-P3 Stage 3, line e5c): **the spec facts of `cancel_ipc` of a `BlockedOnReply` thread with a switch +possibly pending** (case (B) of `take_reply`'s cancel). + +* `cancelIpcReply_fia` / `_swn` / `_offq`: `FIA ∧ RqDomPrio` (with the `KI`/`BAs` frame), `SwN` and `OffQA` across + `cancel_ipc` of a reply-blocked thread (twins of `cancelIpcRecv_fia` / `_swn` / `_offq`; `reply_remove_tcb`'s prefix + only writes SCs and reply objects, then `reply_unlink_tcb`). +* `SPairA.cancelReply`: `SInvXA ∧ SIdleA` across it from a thread that is not the current one (`cancelIpc_invA` assumes no + switch is pending): the `PStepA` twin of `replyRemoveTcb_invA`. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3SigSchedValidA +import Fl4ProofsA64.P3.Stage3SigSwA +import Fl4ProofsA64.P3.Stage3NBSignalWakeA +import Fl4ProofsA64.P3.Stage3CancelRecvPInvA +import Fl4ProofsA64.P3.ReplyRemoveInvA +import Fl4ProofsA64.P3.Stage3DelReplyRemoveA +import Fl4ProofsA64.P3.CancelIpcReplyProgA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open Fl4Spec Fl4SpecA64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Endp + +noncomputable section + +theorem stsAct_neA {σ : State Unit} {t : ObjRef} (h : t ≠ σ.curThread) : stsAct σ t = σ := by + unfold stsAct + rw [if_neg (fun h' => h h'.1)] + +/-- The thread's TCB (if any) is blocked on a reply. -/ +def ReplySt (c : ObjRef) (σ : State Unit) : Prop := + ∀ x, σ.kheap c = some (.tcb x) → ∃ r, x.tcbState = .blockedOnReply r + +/-- `reply_remove_tcb` is its prefix (state reads, the SC's reply cut and the reply's SC link: TCB- and cell-preserving) +and then `reply_unlink_tcb`. -/ +theorem replyRemoveTcb_ht {t r : ObjRef} {P : State Unit → Prop} {Q : Unit → State Unit → Prop} + (hm : ∀ {α : Type} (m : SMonad Unit α), TPres m → CPres m → HT P m (fun _ => P)) + (hU : HT P (replyUnlinkTcb (Z := Unit) t r) Q) : HT P (replyRemoveTcb (Z := Unit) t r) Q := by + unfold replyRemoveTcb + refine HT.bind (Q := fun _ => P) (hm _ (by tpres) (by cpres)) fun _ => ?_ + refine HT.bind (Q := fun _ => P) (hm _ (by tpres) (by cpres)) fun _ => ?_ + refine HT.bind (Q := fun _ => P) (hm _ (by tpres) (by cpres)) fun o => ?_ + cases o with + | none => exact hU + | some p => + dsimp only + refine HT.bind (Q := fun _ => P) (hm _ (by tpres) (by cpres)) fun _ => ?_ + refine HT.bind (Q := fun _ => P) (hm _ (by tpres) (by cpres)) fun _ => ?_ + refine HT.bind (Q := fun _ => P) (hm _ (by tpres) (by cpres)) fun _ => ?_ + refine HT.bind (Q := fun _ => P) (hm _ (by tpres) (by cpres)) fun _ => ?_ + exact HT.bind (Q := fun _ => P) (hm _ (by tpres) (by cpres)) fun _ => hU + +/-- **`cancel_ipc` of a reply-blocked thread keeps `FIA`.** -/ +theorem cancelIpcReply_fia (t : ObjRef) (σ0 : State Unit) : + HT (fun σ => FIA σ ∧ RqDomPrio σ ∧ (KI t σ0 σ ∧ BAs [t] σ) ∧ ReplySt t σ) (cancelIpc (Z := Unit) t) + (fun _ σ => FIA σ ∧ RqDomPrio σ ∧ (KI t σ0 σ ∧ BAs [t] σ)) := by + unfold cancelIpc + have FK : TFrame (KI t σ0) := KI.frame t σ0 + have keep : ∀ σ σ', TKeepA σ σ' → (KI t σ0 σ ∧ BAs [t] σ) → (KI t σ0 σ' ∧ BAs [t] σ') := + fun σ σ' k h => ⟨FK.keep σ σ' k h.1, h.2.ofTKeep k⟩ + refine HT.bind (Q := fun st σ => (FIA σ ∧ RqDomPrio σ ∧ (KI t σ0 σ ∧ BAs [t] σ)) ∧ + ∃ r, st = .blockedOnReply r) ?_ fun st => ?_ + · show HT _ (threadGet (Z := Unit) Tcb.tcbState t) _ + exact HT.threadGet _ t fun σ hσ x hx => ⟨⟨hσ.1, hσ.2.1, hσ.2.2.1⟩, hσ.2.2.2 x hx⟩ + refine HT.bind (Q := fun _ σ => (FIA σ ∧ RqDomPrio σ ∧ (KI t σ0 σ ∧ BAs [t] σ)) ∧ + ∃ r, st = .blockedOnReply r) ?_ fun _ => ?_ + · intro σ hσ r hr + obtain ⟨x, hx, hr2⟩ := threadSet_spec hr + rw [hr2] + exact ⟨⟨hσ.1.1.ofTcbUpd hx (x' := { x with tcbFault := none }) rfl (fun _ => rfl), + hσ.1.2.1.ofTcbRepl hx rfl rfl, + ⟨FK.repl σ t x { x with tcbFault := none } hx rfl hσ.1.2.2.1, hσ.1.2.2.2.tcbUpd hx rfl⟩⟩, hσ.2⟩ + cases st with + | blockedOnReply r => + dsimp only + refine HT.mono (P := fun σ => FIA σ ∧ RqDomPrio σ ∧ (KI t σ0 σ ∧ BAs [t] σ)) + (Q := fun _ σ => FIA σ ∧ RqDomPrio σ ∧ (KI t σ0 σ ∧ BAs [t] σ)) ?_ (fun σ h => h.1) (fun _ _ h => h) + exact replyRemoveTcb_ht (fun {α} m h1 h2 => HT.ofTC' h1 h2 _ keep) (replyUnlinkTcbD t r σ0) + | _ => exact fun σ hσ _ _ => absurd hσ.2 (by rintro ⟨r, h⟩; cases h) + +/-- **`cancel_ipc` of a reply-blocked thread keeps `SwN d`.** -/ +theorem cancelIpcReply_swn (d : ObjRef) : + HT (fun σ => SwN d σ ∧ ReplySt d σ) (cancelIpc (Z := Unit) d) (fun _ => SwN d) := by + unfold cancelIpc + refine HT.bind (Q := fun st σ => SwN d σ ∧ ∃ r, st = .blockedOnReply r) ?_ fun st => ?_ + · show HT _ (threadGet (Z := Unit) Tcb.tcbState d) _ + exact HT.threadGet _ d fun σ hσ x hx => ⟨hσ.1, hσ.2 x hx⟩ + refine HT.bind (Q := fun _ σ => SwN d σ ∧ ∃ r, st = .blockedOnReply r) + (fun σ hσ r hr => ⟨threadSet_swn d _ σ hσ.1 r hr, hσ.2⟩) fun _ => ?_ + cases st with + | blockedOnReply r => + dsimp only + refine HT.mono (P := SwN d) (Q := fun _ => SwN d) ?_ (fun σ h => h.1) (fun _ _ h => h) + exact replyRemoveTcb_ht (fun {α} m h1 _ => HT.swn h1 d) (replyUnlinkTcb_swn d r) + | _ => exact fun σ hσ _ _ => absurd hσ.2 (by rintro ⟨r, h⟩; cases h) + +/-- **`cancel_ipc` of a reply-blocked thread keeps `OffQA c`.** -/ +theorem cancelIpcReply_offq (c d : ObjRef) : + HT (fun σ => OffQA c σ ∧ ReplySt d σ) (cancelIpc (Z := Unit) d) (fun _ => OffQA c) := by + unfold cancelIpc + refine HT.bind (Q := fun st σ => OffQA c σ ∧ ∃ r, st = .blockedOnReply r) ?_ fun st => ?_ + · show HT _ (threadGet (Z := Unit) Tcb.tcbState d) _ + exact HT.threadGet _ d fun σ hσ x hx => ⟨hσ.1, hσ.2 x hx⟩ + refine HT.bind (Q := fun _ σ => OffQA c σ ∧ ∃ r, st = .blockedOnReply r) + (fun σ hσ r hr => ⟨threadSet_offq c d _ σ hσ.1 r hr, hσ.2⟩) fun _ => ?_ + cases st with + | blockedOnReply r => + dsimp only + refine HT.mono (P := OffQA c) (Q := fun _ => OffQA c) ?_ (fun σ h => h.1) (fun _ _ h => h) + exact replyRemoveTcb_ht (fun {α} m h1 _ => HT.offqT h1 c) (replyUnlinkTcb_offq c d r) + | _ => exact fun σ hσ _ _ => absurd hσ.2 (by rintro ⟨r, h⟩; cases h) + +/-! ## `SInvXA ∧ SIdleA` without `SActA` -/ + +/-- Replacing a reply object keeps the pair. -/ +theorem SPairA.updReply {σ : State Unit} {m : McState} {n : Nat} (h : SPairA σ m n) {p : ObjRef} {y y' : Reply} + (hk : σ.kheap p = some (.reply y)) : SPairA (updK σ p (.reply y')) m n := by + refine h.refK ⟨rfl, fun q => ?_, fun g => g.updNs p _ fun _ _ e => by cases e⟩ + show RefObjA (σ.kheap q) (Function.update σ.kheap p (some (.reply y')) q) + by_cases e : q = p + · subst e; rw [Function.update_self, hk]; exact .reply _ _ + · rw [Function.update_of_ne e]; exact .same _ + +/-- Deactivating a blocked thread that is neither current nor idle keeps the pair. -/ +theorem SPairA.setInactive {σ : State Unit} {m : McState} {n : Nat} (h : SPairA σ m n) {t : ObjRef} {x : Tcb} + (hk : σ.kheap t = some (.tcb x)) (hb : runnable x.tcbState = false) (o : OffCurA σ m n t) : + SPairA (updK σ t (.tcb { x with tcbState := .inactive })) m n := by + have o' : OffCurA (hatA σ) m n t := ⟨o.idle, o.idle, o.idles⟩ + have H := h.hat.setBlk (x := x) hk hb o' (ts := .inactive) (Or.inr rfl) (fun e => by cases e) + have e1 : stsAct (updK (hatA σ) t (.tcb { x with tcbState := .inactive })) t = + hatA (updK σ t (.tcb { x with tcbState := .inactive })) := by + unfold stsAct; exact if_neg (fun c => by cases c.2.1) + rw [e1] at H + obtain ⟨c, hc⟩ := h.sinv.curTcb + refine SPairA.ofHat H ⟨c, ?_⟩ (fun hr d p => h.sinv.ctQ hr d p) + show Function.update σ.kheap t _ σ.curThread = _ + rw [Function.update_of_ne o.cur.symm] + exact hc + +/-- The heap form of `set_simple_ko` of a reply object. -/ +theorem setReply_heapA {σ : State Unit} {p : ObjRef} {y' : Reply} {r : Unit × State Unit} + (hr : r ∈ (setSimpleKo (Z := Unit) KernelObject.reply p y' σ).1) : + (∃ a, σ.kheap p = some (.reply a)) ∧ r.2 = updK σ p (.reply y') := by + unfold setSimpleKo at hr + obtain ⟨o, ho, hr⟩ := eqPres_mem (getObject_eqPres p) hr + obtain ⟨_, -, hr⟩ := eqPres_mem (PresR.assert _) hr + obtain ⟨_, hpi, hr⟩ := eqPres_mem (PresR.assert _) hr + obtain ⟨ko, hko, e⟩ := setObject_memA hr + have hgo : σ.kheap p = some o := by + have ho' : (o, σ) ∈ (NonDet.assertOpt (σ.kheap p) σ).1 := ho + cases hk : σ.kheap p with + | none => rw [hk] at ho'; exact absurd ho' (Set.notMem_empty _) + | some o' => + rw [hk] at ho' + have := congrArg Prod.fst (show (o, σ) = (o', σ) from ho') + exact congrArg some this.symm + have hn : ∃ a, o = .reply a := by + have h1 := hpi.1 + unfold partialInv at h1 + split_ifs at h1 with hu + · obtain ⟨a, ha, -⟩ := hu + exact ⟨a, ha.symm⟩ + · exact absurd rfl h1 + obtain ⟨a, ea⟩ := hn + exact ⟨⟨a, by rw [← ea]; exact hgo⟩, e⟩ + +/-- **`reply_unlink_tcb`** of a thread that is neither current nor idle keeps the pair, and every other object but +replies. -/ +theorem replyUnlinkTcb_pairA {m : McState} {n : Nat} {t rp : ObjRef} {σ : State Unit} (h : SPairA σ m n) + {x : Tcb} (hk : σ.kheap t = some (.tcb x)) (hb : runnable x.tcbState = false) (o : OffCurA σ m n t) + {r : Unit × State Unit} (hr : r ∈ (replyUnlinkTcb (Z := Unit) t rp σ).1) : + SPairA r.2 m n ∧ (∀ q, q ≠ t → (∀ y, σ.kheap q ≠ some (.reply y)) → r.2.kheap q = σ.kheap q) ∧ + r.2 = { σ with kheap := r.2.kheap } ∧ ∃ z, r.2.kheap (rp) = some (.reply z) ∧ z.replyTcb = none := by + unfold replyUnlinkTcb at hr + obtain ⟨rep, -, hr⟩ := eqPres_mem (getReply_eqPres rp) hr + obtain ⟨_, -, hr⟩ := eqPres_mem (PresR.assert _) hr + have hg : PresR (@Eq (State Unit)) (getThreadState (Z := Unit) t) := by + unfold getThreadState + exact threadGet_eqPres _ t + obtain ⟨ts, -, hr⟩ := eqPres_mem hg hr + obtain ⟨_, -, hr⟩ := eqPres_mem (PresR.assert _) hr + obtain ⟨a, ha, hr⟩ := mem_bindA.1 hr + unfold setReplyObjRef updateSkObjRef at ha + obtain ⟨xr, -, ha⟩ := eqPres_mem (getReply_eqPres rp) ha + obtain ⟨⟨z, hz⟩, ea⟩ := setReply_heapA ha + have hrt : t ≠ rp := fun e => by rw [e, hz] at hk; cases hk + have hA : SPairA a.2 m n := by rw [ea]; exact h.updReply hz + have hka : a.2.kheap t = some (.tcb x) := by + rw [ea, updK_kheap, Function.update_of_ne hrt]; exact hk + have oa : OffCurA a.2 m n t := by rw [ea]; exact ⟨o.cur, o.idle, o.idles⟩ + rw [setThreadState_eq .inactive hka] at hr + have er : r = ((), stsAct (updK a.2 t (.tcb { x with tcbState := .inactive })) t) := hr + rw [er, stsAct_neA (by exact oa.cur)] + refine ⟨hA.setInactive hka hb oa, fun q hqt hqr => ?_, by rw [ea]; rfl, ⟨replyTcbUpdate (fun _ => none) xr, ?_, rfl⟩⟩ + · rw [updK_kheap, Function.update_of_ne hqt, ea, updK_kheap, + Function.update_of_ne (fun e => hqr z (by rw [e]; exact hz))] + · rw [updK_kheap, Function.update_of_ne hrt.symm, ea, updK_kheap, Function.update_self] + +/-- **`reply_remove_tcb`** of a reply-blocked thread that is not the current one keeps the pair. -/ +theorem replyRemoveTcb_pairA {σ : State Unit} {m : McState} {n : Nat} {t rp : ObjRef} {x : Tcb} + (h : SPairA σ m n) (hk : σ.kheap t = some (.tcb x)) (hst : x.tcbState = .blockedOnReply rp) + (hne : t ≠ σ.curThread) {r : Unit × State Unit} (hr : r ∈ (replyRemoveTcb (Z := Unit) t rp σ).1) : + SPairA r.2 m n ∧ (∀ q, q ≠ t → (∀ y n, σ.kheap q ≠ some (.schedContext y n)) → + (∀ y, σ.kheap q ≠ some (.reply y)) → r.2.kheap q = σ.kheap q) ∧ r.2 = { σ with kheap := r.2.kheap } ∧ + ∃ z, r.2.kheap rp = some (.reply z) ∧ z.replyTcb = none := by + have oh := h.hat.offBlockedA hk (by rw [hst]; refine ⟨?_, ?_, ?_, ?_⟩ <;> (intro e; cases e)) + have off : OffCurA σ m n t := ⟨hne, oh.idle, oh.idles⟩ + have hb : runnable x.tcbState = false := by rw [hst]; rfl + unfold replyRemoveTcb at hr + obtain ⟨ts, -, hr⟩ := eqPres_mem (by unfold getThreadState; exact threadGet_eqPres _ t) hr + obtain ⟨_, -, hr⟩ := eqPres_mem (PresR.assert _) hr + obtain ⟨so, hso, hr⟩ := eqPres_mem (PresR.gets _) hr + have eso : so = scWithReply rp σ := congrArg Prod.fst (show (so, σ) = (scWithReply rp σ, σ) from hso) + cases so with + | none => + obtain ⟨H, F, G, Z⟩ := replyUnlinkTcb_pairA h hk hb off hr + exact ⟨H, fun q hqt _ hqr => F q hqt hqr, G, Z⟩ + | some p => + dsimp only at hr + have hp : p ≠ idleScPtr := by + intro ep + have hw := eso.symm + unfold scWithReply partialInv at hw + split_ifs at hw with hun + have hsp := (Classical.choose_spec hun).1 + have eThe := Option.some.inj hw + unfold The at eThe + rw [dif_pos hun] at eThe + have hsat : ∃ sc k, σ.kheap p = some (.schedContext sc k) ∧ rp ∈ sc.scReplies := by + rw [← eThe] + simpa only [decide_eq_true_eq] using hsp + obtain ⟨sc, k, hsc, hm⟩ := hsat + rw [ep] at hsc + rw [h.sidle.f4 sc k hsc] at hm + cases hm + have hg : PresR (@Eq (State Unit)) + (NonDet.liftM SchedContext.scReplies (getSchedContext (Z := Unit) p)) := by + unfold NonDet.liftM + refine PresR.bind (getSchedContext_eqPres p) (fun _ => ?_) + eqpres + obtain ⟨l, -, hr⟩ := eqPres_mem hg hr + have hg2 : PresR (@Eq (State Unit)) (getReplyObjRef (Z := Unit) Reply.replySc rp) := by + unfold getReplyObjRef getSkObjRef getSimpleKo + refine PresR.bind (PresR.bind (getObject_eqPres _) (fun _ => by eqpres)) (fun _ => ?_) + eqpres + obtain ⟨rs, -, hr⟩ := eqPres_mem hg2 hr + obtain ⟨_, -, hr⟩ := eqPres_mem (PresR.assert _) hr + obtain ⟨a, ha, hr⟩ := mem_bindA.1 hr + obtain ⟨y, k, hy, ea⟩ := updateSchedContext_memA _ _ ha + have A := h.updSc (y := scRepliesUpdate (List.takeWhile fun d => decide (d ≠ rp)) y) hy hp rfl rfl rfl (Or.inl rfl) + have hA : SPairA a.2 m n := by rw [ea]; exact A.inv + obtain ⟨b, hb', hr⟩ := mem_bindA.1 hr + unfold setReplyObjRef updateSkObjRef at hb' + obtain ⟨xr, -, hb'⟩ := eqPres_mem (getReply_eqPres rp) hb' + obtain ⟨⟨z, hz⟩, eb⟩ := setReply_heapA hb' + have hB : SPairA b.2 m n := by rw [eb]; exact hA.updReply hz + have htp : t ≠ p := fun e => by rw [e, hy] at hk; cases hk + have hka : a.2.kheap t = some (.tcb x) := by + rw [ea] + show Function.update σ.kheap p _ t = _ + rw [Function.update_of_ne htp]; exact hk + have hrt : t ≠ rp := fun e => by rw [e, hz] at hka; cases hka + have hkb : b.2.kheap t = some (.tcb x) := by + rw [eb, updK_kheap, Function.update_of_ne hrt]; exact hka + have cb : b.2.curThread = σ.curThread := by rw [eb, ea]; rfl + have ib : b.2.idleThread = σ.idleThread := by rw [eb, ea]; rfl + obtain ⟨H, F, G, Z⟩ := replyUnlinkTcb_pairA hB hkb hb ⟨by rw [cb]; exact hne, by rw [ib]; exact off.idle, off.idles⟩ hr + refine ⟨H, fun q hqt hnsc hqr => ?_, G.trans (by rw [eb, ea]; rfl), Z⟩ + have hqp : q ≠ p := fun e => hnsc y k (by rw [e]; exact hy) + have hσq : a.2.kheap q = σ.kheap q := by + rw [ea]; show Function.update σ.kheap p _ q = _; rw [Function.update_of_ne hqp] + have hqrp : q ≠ rp := fun e => hqr z (by rw [← hσq, e]; exact hz) + have hbq : b.2.kheap q = σ.kheap q := by rw [eb, updK_kheap, Function.update_of_ne hqrp, hσq] + rw [F q hqt (fun y' h' => hqr y' (by rw [← hbq]; exact h')), hbq] + +/-- **`SInvXA ∧ SIdleA` across `cancel_ipc` of a reply-blocked thread** that is not the current one (a switch may be +pending). -/ +theorem SPairA.cancelReply {σ : State Unit} {m : McState} {n : Nat} (h : SPairA σ m n) {t rp : ObjRef} {x : Tcb} + (hk : σ.kheap t = some (.tcb x)) (hs : x.tcbState = .blockedOnReply rp) (hne : t ≠ σ.curThread) + {σ' : State Unit} (hr : ((), σ') ∈ (cancelIpc (Z := Unit) t σ).1) : + SPairA σ' m n ∧ (∀ q, q ≠ t → (∀ y n, σ.kheap q ≠ some (.schedContext y n)) → + (∀ y, σ.kheap q ≠ some (.reply y)) → σ'.kheap q = σ.kheap q) ∧ σ' = { σ with kheap := σ'.kheap } ∧ + ∃ z, σ'.kheap rp = some (.reply z) ∧ z.replyTcb = none := by + rw [cancelIpc_replyEqA hk hs] at hr + have R1 : RefK σ (updK σ t (.tcb { x with tcbFault := none })) := by + refine ⟨rfl, fun q => ?_, fun g => g.updNs t _ fun _ _ e => by cases e⟩ + show RefObjA (σ.kheap q) (Function.update σ.kheap t (some (.tcb { x with tcbFault := none })) q) + by_cases e : q = t + · subst e; rw [Function.update_self, hk]; exact .tcb _ _ ⟨rfl, rfl, rfl, rfl, Or.inr rfl⟩ + · rw [Function.update_of_ne e]; exact .same _ + obtain ⟨H, F, G, Z⟩ := replyRemoveTcb_pairA (h.refK R1) (x := { x with tcbFault := none }) + (by simp only [updK_kheap, Function.update_self]) hs hne (r := ((), σ')) hr + refine ⟨H, fun q hqt hnsc hqr => ?_, G, Z⟩ + have e1 : (updK σ t (.tcb { x with tcbFault := none })).kheap q = σ.kheap q := by + rw [updK_kheap, Function.update_of_ne hqt] + rw [F q hqt (fun y n h' => hnsc y n (by rw [← e1]; exact h')) (fun y h' => hqr y (by rw [← e1]; exact h')), e1] + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3CancelReplyRecA.lean b/proofs/Fl4ProofsA64/P3/Stage3CancelReplyRecA.lean new file mode 100644 index 000000000..ff6d186e9 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3CancelReplyRecA.lean @@ -0,0 +1,233 @@ +/- +P3 (aarch64, A64-P3 Stage 3, line e5c): **the record across `cancel_ipc` of a reply-blocked thread** (case (B) of +`take_reply`'s cancel), with a switch possibly pending (`JStage3BA`). + +* `JStage3BA.cancel_p1A`: A64 P1's facts across `cancel_ipc` of any thread: `BlkOK`, `KeepT` (the word frame: every TCB word + but the state / fault of the thread and the `Ep` links, the notification `SC` words, the SCs' reply words kept or + cleared) and the footprint `FootD`. +* `JStage3BA.cancelReply_recA`: the record at the successor (the reply stacks shrink: `stack'`), the spec outcome + (`SPairA.cancelReply`), the write set `SchedStepNWA` (`cancel_ipc_replyA` at an empty buffer set, as `suspend_stepA`), + and the frames the arms need. + +[S] the thread is reply-blocked, is not the current thread, and its reply is tracked and points back. [P] `ArchTotal` +premises of `JStage3BA.schedOpsA` (`hCs`, `hmb`, `hmax`, `htu`). No new named premise. New module. +-/ +import Fl4ProofsA64.P3.DelSuspendA +import Fl4ProofsA64.P3.Stage3CancelReplyPA +import Fl4ProofsA64.P3.Stage3SigBoundRecA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 Fl4ProofsA64.P3.Endp +open Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Notif + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- **A64 P1's facts across `cancel_ipc`** (module docstring). -/ +theorem JStage3BA.cancel_p1A {ar : machine.Arch A} {J5 J7 : JSlotA S} [J7PpA st J7] + (J : JStage3BA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {i : U64} (hiT : i ∈ H.tcbs) : + ipc.cancel_ipc st ar s a i ⦃ r => + BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac r.1 ∧ + KeepT st H.tcbs Z.Cs H.replies H.scs H.ntfns i s r.1 ∧ + Fl4ProofsA64.P1.K8.FootD st H.tcbs Z.Cs H.frame H.scs H.scBits (EH H) s r.1 ⦄ := by + obtain ⟨⟨Q, R, h0⟩, hB, -, hS, hSg⟩ := J.blkA + have hX := J.blkA.2.2.1 + refine WP.spec_mono (WP.spec_and (J.cancelRecv_blkA (ar := ar) hiT) + (WP.spec_and (ipc.cancel_ipc.all s a i Q R h0 hiT hB hX hSg.sub) + (Fl4ProofsA64.P1.K8.ipc.cancel_ipc.kw8 s a i h0 hiT hX.1 hSg.sub (fun hs => by + obtain ⟨h1, h2⟩ := hX.2.1.reply i hiT hs + exact ⟨h1, h2, hX.2.1.next _ h1, hX.2.1.prev _ h1⟩) (fun hs h0' => (hX.2.1.recv i hiT hs h0').1)))) ?_ + rintro ⟨s1, a1⟩ ⟨b, ⟨-, -, -, -, -, -, k1, -⟩, f⟩ + exact ⟨b, k1, Fl4ProofsA64.P1.K8.Foot.d f⟩ + +set_option maxHeartbeats 4000000 in +/-- **The record across `cancel_ipc` of a reply-blocked thread** (module docstring). -/ +theorem JStage3BA.cancelReply_recA {ar : machine.Arch A} + (J : JStage3BA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {t r : U64} (ht : t ∈ Z.T) (hr : r ∈ Rp) {x : Fl4SpecA64.Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hs : x.tcbState = .blockedOnReply (specPtr r)) {xr : Fl4SpecA64.Reply} + (hkr : σ.kheap (specPtr r) = some (.reply xr)) (hback : xr.replyTcb = some (specPtr t)) + (hne : specPtr t ≠ σ.curThread) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) : + ipc.cancel_ipc st ar s a t ⦃ out => + out.2 = a ∧ ∃ σ' stack' x' Q Rl, + cancelIpc (Z := Unit) (specPtr t) σ = ({((), σ')}, False) ∧ + JStage3BA st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack' κ n (J5A st) + (J7A st (J7R2A st ar nA)) out.1 a σ' m ∧ + WritesIn st s out.1 (SchedStepNWA (st := st) N E Rp Z m σ s) ∧ + SchedFrameA σ σ' ∧ QKeepA σ σ' ∧ σ'.kheap (specPtr t) = some (.tcb x') ∧ x'.tcbState = .inactive ∧ + (∀ q, q ≠ specPtr t → (∀ y n, σ.kheap q ≠ some (.schedContext y n)) → (∀ y, σ.kheap q ≠ some (.reply y)) → + σ'.kheap q = σ.kheap q) ∧ σ' = { σ with kheap := σ'.kheap } ∧ + (∃ z, σ'.kheap (specPtr r) = some (.reply z) ∧ z.replyTcb = none) ∧ (∀ c, ∀ q ∈ stack' c, q ∈ stack c) ∧ + CancelKeepA σ σ' ∧ KeepT st H.tcbs Z.Cs H.replies H.scs H.ntfns t s out.1 ⦄ := by + have htH : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + have hc := J.curCore_lt hCs + have hP := J.iplaced + have hx := J.ep.at_tcb ht hk + have hst5 : L.mem s (Fl4ProofsA64.P1.stA t) = sched.TS_BLOCKED_ON_REPLY := by + refine UScalar.eq_of_val_eq ?_ + have := hx.st + rw [hs] at this + exact this.trans (by unfold sched.TS_BLOCKED_ON_REPLY; rfl) + have RV : RunVcpuInv st H.tcbs s := J.tcbs ▸ J.runVcpu + have hrv0 : L.mem s (runVcpuA t) = 0#u64 := by + by_contra hne0 + have hR := RV t htH hne0 + rw [hst5] at hR + exact absurd hR (by unfold sched.TS_BLOCKED_ON_REPLY sched.TS_RUNNING_VM; decide) + have hAct : Aligned (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) := + nodeAt_aligned (J.sr.core.nodes _ J.sr.sch.cpuCs) (by simp [layout.node.SCHED_ACTION]) + (by simp [layout.node.SCHED_ACTION]) + -- the placement at an empty buffer set (the cancel writes no buffer word) + have PE0 : EPlaced Z.T E Rp (∅ : Set (U64 × Fl4SpecA64.Word)) := + { hP.e with + balign := fun _ ⟨_, h, _⟩ => h.elim + bt := fun _ ⟨_, h, _⟩ => h.elim + be := fun _ ⟨_, h, _⟩ => h.elim + br := fun _ ⟨_, h, _⟩ => h.elim + binj := fun _ h => h.elim } + have hP0 : IPlacedA N Z.T E Rp (∅ : Set (U64 × Fl4SpecA64.Word)) := + ⟨hP.n, PE0, hP.ne, hP.nr, fun _ _ _ ⟨_, h, _⟩ => h.elim⟩ + have P0' : IpcSchedPlacementA Z.th N E Rp (∅ : Set (U64 × Fl4SpecA64.Word)) := + ⟨J.ipcPlace.notification, J.ipcPlace.endpoint, J.ipcPlace.reply, fun _ ⟨_, h, _⟩ => h.elim⟩ + have HE0 : EpStateRel Z.T E Rp (∅ : Set (U64 × Fl4SpecA64.Word)) (L.mem s) σ := + ⟨J.ep.ep, J.ep.reply, J.ep.tcb, fun _ h => h.elim⟩ + have hSched : CancelScheduleA st N Z.T (IpcSchedRelA st Z m κ) := cancelScheduleA_of_placement J.ipcPlace + have ops := J.schedOpsA (ar := ar) hc hmb hmax htu + have ops0 := ipcSchedOpsA (st := st) (ar := ar) (κ := κ) hP0 P0' hc hmb hmax htu + have cov := J.scCov.reply (specPtr r) + refine WP.spec_mono (WP.spec_and + (cancel_ipc_replyA hSched ops.2.2.1 hP J.ipcPlace s a σ J.ipcSchedA J.ntfn J.ep J.reply ht hr hk hs hkr hback cov + J.scNZ hAct) + (WP.spec_and + (cancel_ipc_replyA hSched ops0.2.2.1 hP0 P0' s a σ J.ipcSchedA J.ntfn HE0 J.reply ht hr hk hs hkr hback cov + J.scNZ hAct) + (J.cancel_p1A (ar := ar) htH))) ?_ + rintro ⟨s', a'⟩ ⟨⟨ha, σ1, stk', hc1, RN1, H1, HE1, HS1, hiso, w1, K1, y, hy, hin⟩, + ⟨-, σ0, -, hc0, -, -, -, -, -, w0, -, -, -, -⟩, blk, k1, f1⟩ + dsimp only at ha RN1 H1 HE1 HS1 hiso w1 K1 w0 blk k1 f1 ⊢ + have e0 : σ0 = σ1 := by + have := hc0.symm.trans hc1 + simpa using congrArg Prod.fst this + subst e0 + have hmem : ((), σ0) ∈ (cancelIpc (Z := Unit) (specPtr t) σ).1 := by rw [hc1]; exact Set.mem_singleton _ + have w : WritesIn st s s' (SchedStepNWA (st := st) N E Rp Z m σ s) := + w0.mono fun b hb => (cancelReplyW_stepNA J.sr.toSchedSRN hP0 hb).resolve_right fun ⟨_, h, _⟩ => h.elim + have kb : L.ksb s' = L.ksb s := w.1 + have F : SchedFrameA σ σ0 := cancelIpc_presA (specPtr t) σ ((), σ0) hmem + have Qk : QKeepA σ σ0 := Sched.qk_of (m := cancelIpc (Z := Unit) (specPtr t)) hmem + have SK : SStepA σ σ0 := cancelIpc_sstepA (specPtr t) σ ((), σ0) hmem + have inv := SPairA.cancelReply ⟨J.sinv, J.sidle⟩ hk hs hne hmem + obtain ⟨Q', R', H3⟩ := H1 + have ent := J.ent_stepNWA w blk + have sq : SchedQueuesInv st H.tcbs Z.Cs H.omega H s' Q' R' := by + obtain ⟨Q0, R0, h0'⟩ := blk.1 + exact SchedQueuesInv.reQA h0' (by rw [J.tcbs]; exact H3.core) + have poolOff : PoolOff st H Z.Cs OS s' := fun i => by + have h0' := J.poolOff i + rw [vspace.asid_table.eq] at h0' ⊢ + have v := AtOff.v i + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.ent.ksw + have ev : (wa (L.ksb s) (AtOff i)).val = (L.ksb s).val + (AtOff i).val := by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have e0 : L.mem s' (wa (L.ksb s') (AtOff i)) = L.mem s (wa (L.ksb s) (AtOff i)) := by + rw [kb] + refine w.mem_eq ?_ (J.asid_offStepNA (by omega) (by omega)) + have h32 := J.j.1.ksbAl + show (wa (L.ksb s) (AtOff i)).val % 8 = 0 + omega + rw [e0] + refine (WP.spec_ok _).2 fun hne asid => ?_ + have q := (WP.spec_ok _).1 h0' hne asid + exact ⟨q.al, q.om, q.sg, q.os, RegionOffNodes.ksb kb q.ks⟩ + have tw : ∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → K.val ≠ 3496 → K.val ≠ 3504 → + K.val ≠ 3328 → K.val ≠ 3376 → L.mem s' (wa u K) = L.mem s (wa u K) := fun u hu K h1 h2 h3 h4 h5 h6 => + k1.fld u (by rw [J.tcbs]; exact hu) K h1 h2 h3 h4 (fun _ => ⟨h5, h6⟩) + -- the reply stacks, total + classical + obtain ⟨stk, estk⟩ : ∃ f : U64 → List U64, f = fun c => if c ∈ Z.C then stk' c else [] := ⟨_, rfl⟩ + have eC : ∀ c ∈ Z.C, stk c = stk' c := fun c hc => by rw [estk]; exact if_pos hc + have rs : ReplyStackRelA (L.mem s') Rp Z.C σ0 (scNameA m) stk := + { stackRel := fun c hc => by rw [eC c hc]; exact HS1.stackRel c hc + unique := fun c hc c' hc' r h h' => by + rw [eC c hc] at h; rw [eC c' hc'] at h'; exact HS1.unique c hc c' hc' r h h' + owner := fun r hr x hx p => (HS1.owner r hr x hx p).trans + (exists_congr fun c => and_congr_right fun hc => by rw [eC c hc]) + detached := fun r hr h => by + have h' : ∀ c ∈ Z.C, r ∉ stk' c := fun c hc => by rw [← eC c hc]; exact h c hc + obtain ⟨a, b, d⟩ := HS1.detached r hr h' + refine ⟨a, fun ne => ?_, fun ne => ?_⟩ + · obtain ⟨m1, m2, m3⟩ := b ne + exact ⟨m1, fun c hc => by rw [eC c hc]; exact m2 c hc, m3⟩ + · obtain ⟨m1, m2, m3⟩ := d ne + exact ⟨m1, fun c hc => by rw [eC c hc]; exact m2 c hc, m3⟩ } + have hsub : ∀ c, ∀ r ∈ stk c, r ∈ stack c := fun c r hr' => by + by_cases hc : c ∈ Z.C + · exact SK.stackSubA J.reply rs c hc r hr' + · rw [estk] at hr' + simp only [if_neg hc] at hr' + cases hr' + have bufAl : UtBufAlA Z.T σ0 := SK.utBufAlA J.bufAl fun u hu => by + obtain ⟨x0, hx0, -⟩ := J.ntfn.tcb u hu + exact ⟨x0, hx0⟩ + have idle : IdleNoFaultA m σ0 := SK.idleNoFaultA J.idleNoFault + have scCov : ScCovA Z.C m σ0 := fun q y k h => by + obtain ⟨y', hy'⟩ := F.2.1 q y k h + exact J.scCov q y' k hy' + have mcp : McpRelA Z.T m (L.mem s') σ0 := + J.mcp.congrA (fun u hu => ⟨tw u hu 3440#u64 (by simp) (by simp) (by simp) (by simp) (by simp) (by simp), + tw u hu 3456#u64 (by simp) (by simp) (by simp) (by simp) (by simp) (by simp)⟩) fun u hu x0 hx0 => by + obtain ⟨y2, h2, e⟩ := K1.tcb _ x0 hx0 + exact ⟨y2, h2, by rw [e], by rw [e]⟩ + have ye : ∀ u ∈ Z.T, L.mem s' (yieldA u) = L.mem s (yieldA u) := fun u hu => by + unfold yieldA; exact tw u hu 3472#u64 (by simp) (by simp) (by simp) (by simp) (by simp) (by simp) + have yr : YieldRelA Z.T Z.C m (L.mem s') σ0 := by + refine ⟨fun u hu => ?_, fun u hu h => ?_, fun c hcc y2 k h2 p hp => ?_⟩ + · obtain ⟨y1, hy1, e0⟩ := J.yieldRel.tcb u hu + obtain ⟨y3, h3, e⟩ := K1.tcb _ y1 hy1 + exact ⟨y3, h3, by rw [ye u hu, e]; exact e0⟩ + · rw [ye u hu] at h ⊢ + obtain ⟨hC, y1, k, hy1, e0⟩ := J.yieldRel.toSc u hu h + obtain ⟨y3, h3, e⟩ := K1.sc _ y1 k hy1 + exact ⟨hC, y3, k, h3, by rw [e]; exact e0⟩ + · obtain ⟨x0, h0⟩ := F.2.1 _ y2 k h2 + obtain ⟨y3, h3, e⟩ := K1.sc _ x0 k h0 + rw [h2] at h3 + obtain ⟨rfl, -⟩ := KernelObject.schedContext.inj (Option.some.inj h3) + obtain ⟨u, hu, e1, e2⟩ := J.yieldRel.frm c hcc x0 k h0 p (by rw [e] at hp; exact hp) + exact ⟨u, hu, e1, by rw [ye u hu]; exact e2⟩ + have rv : RunVcpuInv st H.tcbs s' := RV.keepT k1 (Or.inr hrv0) + have buf : BufCover st Z.T Bf s' σ0 := J.bufCover_susA w f1 SK + have KB : BlkOthA (specPtr t) σ σ0 := + cancelIpc_blkOthA hk (fun e' h => by rw [hs] at h; cases h) (fun n' h => by rw [hs] at h; cases h) hmem + have bq : BlkQueuedA Z.T E N (L.mem s') σ0 := J.blkQ_susA ⟨y, hy, hin⟩ KB HE1 RN1 + have oth : Fl4ProofsA64.P1.OthF st H.tcbs t s s' := fun u hu hut K h1 h2 h3 h4 h5 h6 => + k1.fld u hu K h1 h2 h5 h6 (fun e => absurd e hut) + have hvr : ∀ sc ∈ Z.C, ∀ q ∈ stk sc, RBlk st s' q := + J.vr_susA blk hk KB oth hsub (fun sc hc q h hq => by + have e : q = r := specPtr_inj (by injection hs.symm.trans hq with e; exact e.symm) + have h' : q ∈ stk' sc := by rw [← eC sc hc]; exact h + exact hiso.1 sc hc (e ▸ h')) rs HE1 + have j := J.j57_stepNWA w σ0 + have J' := JStage3BA.of_schedWrN (blkQ := bq) J (hsub := hsub) (stack' := stk) (Q := Q') (Rl := R') + (curOf := Z.curOf) (curScOf := Z.curScOf) (s' := s') (a' := a) (σ' := σ0) w F.1 F.2.1 Qk F.2.2 J.j.1.ar + (fun _ _ => rfl) bufAl ent sq poolOff H3 scCov HE1 RN1 rs buf idle mcp yr inv.1.sinv inv.1.sidle (J.tcbs ▸ rv) hvr j.1 j.2 + exact ⟨ha, σ0, stk, y, Q', R', hc1, J', w, F, Qk, hy, hin, inv.2.1, inv.2.2.1, inv.2.2.2, hsub, K1, k1⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3CapBufA.lean b/proofs/Fl4ProofsA64/P3/Stage3CapBufA.lean new file mode 100644 index 000000000..4853ceee7 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3CapBufA.lean @@ -0,0 +1,133 @@ +/- +P3 (AArch64, merge line, D8 step 5 `of_capFrame`, part (i)): **the IPC-buffer fields across an in-place cap write**. + +`msg.lookup_ipc_buffer` reads only the pointer, size, rights and device flag of a `Frame` cap in the buffer slot (not +`mapped` / `ept` / `asid` / `va`), so `lookup_ipc_buffer_mapA`: the lookup is unchanged by a `MapAlikeA` update of the +slot. Hence **`BufNativeA.mapA`** (the new frame stays native: the arm's `ept' = false` fact) and **`BufCover.mapA`** +(with the old `BufNativeA`, which rules out a Stage-2 buffer cap whose spec cap `ipcBufOfA` ignores). +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3CapFrameA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **The IPC-buffer lookup across an in-place update of the buffer slot** (module docstring). -/ +theorem lookup_ipc_buffer_mapA {s r : S} {t : U64} (b : Bool) (ib : L.mem r (ibWordA t) = L.mem s (ibWordA t)) + (k : MapAlikeA (capA st r (sbWordA t)) (capA st s (sbWordA t))) : + msg.lookup_ipc_buffer st r b t = msg.lookup_ipc_buffer st s b t := by + unfold msg.lookup_ipc_buffer + rw [ipc_buffer_word, ipc_buffer_word, ib] + have hsl : obj.Tcb.slot t layout.tcb.SLOT_BUFFER = ok (sbWordA t) := by simp [obj.Tcb.slot, lift, sbWordA] + rw [hsl] + simp only [bind_ok] + rw [capA_eq (st := st) r (sbWordA t), capA_eq (st := st) s (sbWordA t)] + simp only [bind_ok] + generalize capA st r (sbWordA t) = d at k ⊢ + generalize capA st s (sbWordA t) = c at k ⊢ + cases k <;> rfl + +/-- `BufNativeA` across an in-place update of the buffer slots, the new buffer frame being native. -/ +theorem BufNativeA.mapA {T : Set U64} {s r : S} (h : BufNativeA T (L.mem s)) + (hn : ∀ t ∈ T, ∀ p sz rr dv m e a v, capA st r (sbWordA t) = .Frame p sz rr dv m e a v → e = false) : + BufNativeA T (L.mem r) := by + intro t ht + rw [bufSlot_eqA, capAtA_eq (st := st)] + generalize hc : capA st r (sbWordA t) = cc + cases cc with + | Frame p sz rr dv m e a v => exact hn t ht p sz rr dv m e a v hc + | _ => trivial + +/-- A frame cap's `CapEqA` is its `absCapA`. -/ +theorem capEq_of_frameA {c : capability.Cap} {a : Fl4SpecA64.Cap} (h : CapEqA c a) + (hf : ∃ p R vms md, absCapA c = .archObjectCap (.frameCap p R vms false md)) : a = absCapA c := by + obtain ⟨p, R, vms, md, hf⟩ := hf + cases c <;> first | exact h.symm | (simp [absCapA] at hf; done) + +/-- **The IPC buffer a TCB's frame cap names, across an in-place update of that cap.** -/ +theorem ipcBuf_mapA {x x' : Fl4SpecA64.Tcb} {d c1 : capability.Cap} (km : MapAlikeA d c1) + (hn : ∀ p sz rr dv m e a v, c1 = .Frame p sz rr dv m e a v → e = false) (c : CapEqA c1 x.tcbIpcframe) + (abs' : absCapA d = x'.tcbIpcframe) + (hf : ∃ p R vms md, x'.tcbIpcframe = .archObjectCap (.frameCap p R vms false md)) + (hbuf : x'.tcbIpcBuffer = x.tcbIpcBuffer) (b : Bool) : ipcBufOfA b x' = ipcBufOfA b x := by + have hf2 : ∃ p R vms md, absCapA d = .archObjectCap (.frameCap p R vms false md) := by rw [abs']; exact hf + have hx : x.tcbIpcframe = absCapA c1 := by + have key : ∃ p R vms md, absCapA c1 = .archObjectCap (.frameCap p R vms false md) := by + cases km with + | refl => exact hf2 + | frame p sz rr dv m e a v m' e' a' v' => + have hn' := hn p sz rr dv m e a v rfl + subst hn' + cases e' <;> simp [absCapA, absFrameE] at hf2 ⊢ + subst hf2 + simp + | _ => simp [absCapA] at hf2 + exact capEq_of_frameA c key + unfold ipcBufOfA + rw [hx, ← abs', hbuf] + rw [← abs'] at hf + cases km with + | refl => rfl + | frame p sz rr dv m e a v m' e' a' v' => + have hn' := hn p sz rr dv m e a v rfl + subst hn' + cases e' <;> cases dv <;> simp [absCapA, absFrameE] at hf ⊢ + | _ => simp [absCapA] at hf + +/-- **`BufCover` across an in-place update of the buffer slots** (module docstring). -/ +theorem BufCover.mapA {th : TypedHeapA} {T : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {s r : S} + {σ σ' : Fl4SpecA64.State Unit} (h : BufCover st T Bf s σ) (hN : BufNativeA T (L.mem s)) + (R : StateRelA th (L.mem s) σ) (R' : StateRelA th (L.mem r) σ') (hT : ∀ t ∈ T, th t = some .tcb) + (HS : SpecSlotA σ σ') (ib : ∀ t ∈ T, L.mem r (ibWordA t) = L.mem s (ibWordA t)) + (k : ∀ t ∈ T, MapAlikeA (capA st r (sbWordA t)) (capA st s (sbWordA t))) : BufCover st T Bf r σ' := by + intro t ht x' b sb w hx' hb hl + obtain ⟨x, hx, hs⟩ := R.tcb t (hT t ht) + obtain ⟨y', hy', hs'⟩ := R'.tcb t (hT t ht) + rw [hx'] at hy'; cases hy' + obtain ⟨y, hy, ey⟩ := HS.tcb hx + rw [hx'] at hy; cases hy + have hbuf : x'.tcbIpcBuffer = x.tcbIpcBuffer := by + have h0 := congrArg Fl4SpecA64.Tcb.tcbIpcBuffer ey; exact h0 + have slot : ∀ {m : U64 → U64} {z : Fl4SpecA64.Tcb} {a : U64}, + SlotRelA m a (tcbCnodeMap z (tcbCnodeIndex 2)) → CapEqA (capAtA m a) z.tcbIpcframe := by + rintro m z a ⟨ac, e, hc⟩ + rw [tcbCnodeMap_twoA] at e + cases e + exact hc.2 + have c := slot (hs 2 (by decide)) + have c' := slot (hs' 2 (by decide)) + rw [bufSlot_eqA, capAtA_eq (st := st)] at c c' + have hfr : ∃ p R vms md, x'.tcbIpcframe = .archObjectCap (.frameCap p R vms false md) := by + unfold ipcBufOfA at hb + split at hb + · next e => exact ⟨_, _, _, _, e⟩ + · cases hb + have abs' : absCapA (capA st r (sbWordA t)) = x'.tcbIpcframe := by + obtain ⟨fp, fR, fv, fm, hfr'⟩ := hfr + rcases c'.eq_or_pt with e | ⟨_, _, _, e⟩ + · exact e.symm + · rw [hfr'] at e; cases e + have hn := hN t ht + rw [bufSlot_eqA, capAtA_eq (st := st)] at hn + have hb0 : ipcBufOfA b x = some sb := by + rw [← ipcBuf_mapA (k t ht) (fun p sz rr dv m e a v h => by rw [h] at hn; exact hn) c abs' hfr hbuf b] + exact hb + have hlk : msg.lookup_ipc_buffer st r b t = msg.lookup_ipc_buffer st s b t := + lookup_ipc_buffer_mapA b (ib t ht) (k t ht) + rw [hlk] at hl + exact h t ht x b sb w hx hb0 hl + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3CapDerivA.lean b/proofs/Fl4ProofsA64/P3/Stage3CapDerivA.lean new file mode 100644 index 000000000..443788afe --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3CapDerivA.lean @@ -0,0 +1,523 @@ +/- +P3 (AArch64, merge line, D8 step 5 (iii)): **`JStage3A` across slot writes whose caps are derived from old caps** +(CNode copy / mint / move / mutate / rotate). + +Unlike the in-place class `MapAlikeA` (`P3/Stage3MapAlikeA`), these writes move caps between slots and change the +ghost `G`. `DerivA d c` says `d` is `c` up to badge, rights, CNode guard, frame rights / mapping data, or a grown +untyped free index. `CapsFromA` relates the two states' slot caps in both directions: every new cap is null or +derived from an old one, and every old non-null cap survives (derived) in some slot. From it one keep lemma per +cap-reading field of `JStage3A`, assembled in `JStage3A.of_capDerivA`. The MDB slot, J7, `sysl`, the joint relation +and the fields that read the mapped flag or the TCB buffer slots stay arm hypotheses here. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3CapFrameA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-- **A cap derived from an old cap** (module docstring): `DerivA d c` is `d` from `c`. -/ +inductive DerivA : capability.Cap → capability.Cap → Prop + | refl (c : capability.Cap) : DerivA c c + | ep (p b : U64) (x y z w : Bool) (b' : U64) (x' y' z' w' : Bool) : + DerivA (.Endpoint p b' x' y' z' w') (.Endpoint p b x y z w) + | ntfn (p b : U64) (x y : Bool) (b' : U64) (x' y' : Bool) : + DerivA (.Notification p b' x' y') (.Notification p b x y) + | reply (p : U64) (g g' : Bool) : DerivA (.Reply p g') (.Reply p g) + | cnode (p r g gs g' gs' : U64) : DerivA (.CNode p r g' gs') (.CNode p r g gs) + | frame (p : U64) (sz : capability.FrameSize) (r : U64) (dv m e : Bool) (a v r' : U64) (m' e' : Bool) + (a' v' : U64) : DerivA (.Frame p sz r' dv m' e' a' v') (.Frame p sz r dv m e a v) + | ut (p b i i' : U64) (dv : Bool) (h : i.val ≤ i'.val) : DerivA (.Untyped p b i' dv) (.Untyped p b i dv) + /-- (Lift 1b) `IRQControl` issue's handler cap is derived from the control cap. -/ + | irqH (i : U64) : DerivA (.IrqHandler i) .IrqControl + +/-- The cap kinds a derivation may change. -/ +def FixedCapA : capability.Cap → Prop + | .Endpoint .. => False + | .Notification .. => False + | .Reply .. => False + | .CNode .. => False + | .IrqHandler _ => False + | .IrqControl => False + | .Frame .. => False + | .Untyped .. => False + | _ => True + +section deriv +variable {d c : capability.Cap} + +theorem DerivA.back_eq (h : DerivA d c) {e : capability.Cap} (he : FixedCapA e) (hd : d = e) : c = e := by + cases h with + | refl => exact hd + | _ => subst hd; exact he.elim + +theorem DerivA.fwd_eq (h : DerivA d c) {e : capability.Cap} (he : FixedCapA e) (hc : c = e) : d = e := by + cases h with + | refl => exact hc + | _ => subst hc; exact he.elim + +theorem DerivA.ut_back (h : DerivA d c) {q b fi : U64} {dv : Bool} (hd : d = .Untyped q b fi dv) : + ∃ fi0 : U64, c = .Untyped q b fi0 dv ∧ fi0.val ≤ fi.val := by + cases h with + | refl => exact ⟨fi, hd, le_refl _⟩ + | ut p b0 i i' dv0 h => + injection hd with h1 h2 h3 h4 + subst h1 h2 h3 h4 + exact ⟨i, rfl, h⟩ + | _ => cases hd + +theorem DerivA.frame_back (h : DerivA d c) {p : U64} {sz : capability.FrameSize} {r : U64} {dv m e : Bool} + {a v : U64} (hd : d = .Frame p sz r dv m e a v) : ∃ r0 m0 e0 a0 v0, c = .Frame p sz r0 dv m0 e0 a0 v0 := by + cases h with + | refl => exact ⟨r, m, e, a, v, hd⟩ + | frame p1 sz1 r1 dv1 m1 e1 a1 v1 r2 m2 e2 a2 v2 => + injection hd with h1 h2 h3 h4 h5 h6 h7 h8 + subst h1 h2 h4 + exact ⟨r1, m1, e1, a1, v1, rfl⟩ + | _ => cases hd + +theorem DerivA.isFr (h : DerivA d c) : isFrA d = isFrA c := by cases h <;> rfl + +theorem DerivA.isUt (h : DerivA d c) : isUtA d = isUtA c := by cases h <;> rfl + +theorem DerivA.ptr (h : DerivA d c) : ptrA d = ptrA c := by + have e : capability.Cap.ptr d = capability.Cap.ptr c := by cases h <;> rfl + rw [ptrA_eq, ptrA_eq] at e + exact okinj e + +theorem DerivA.phys (h : DerivA d c) : physA d = physA c := by + have e : objects.cap_is_physical d = objects.cap_is_physical c := by cases h <;> rfl + rw [physA_eq, physA_eq] at e + exact okinj e + +theorem DerivA.rngKey (h : DerivA d c) : RngKeyA d ↔ RngKeyA c := by + unfold RngKeyA + rw [h.phys, h.isUt, h.isFr] + +theorem DerivA.names (h : DerivA d c) {k : DKindA} {p : U64} (hn : NamesDA k p c) : NamesDA k p d := by + cases h with + | refl => exact hn + | reply q g g' => + cases k + · cases (show capability.Cap.Reply q g = .Vcpu p from hn) + · obtain ⟨g0, e⟩ := hn; injection e with e1 e2; subst e1; exact ⟨g', rfl⟩ + · obtain ⟨_, e⟩ := hn; cases e + · rcases hn with e | ⟨_, e⟩ <;> cases e + | _ => + exfalso + cases k + · exact capability.Cap.noConfusion (show _ = capability.Cap.Vcpu p from hn) + · obtain ⟨_, e⟩ := hn; cases e + · obtain ⟨_, e⟩ := hn; cases e + · rcases hn with e | ⟨_, e⟩ <;> cases e + +/-- `CapKindA` across a derivation: the pointed object is kept. -/ +theorem CapKindA.derivA {H : Heap} (h : DerivA d c) (k : CapKindA H c) : CapKindA H d := by + refine ⟨fun p b x y z w e => ?_, fun p b x y e => ?_, fun p g e => ?_, + fun p sz e => k.2.2.2.1 p sz (h.back_eq (e := .SchedContext p sz) trivial e), + fun cpu e => k.2.2.2.2 cpu (h.back_eq (e := .SchedControl cpu) trivial e)⟩ + · cases h with + | refl => exact k.1 p b x y z w e + | ep q b0 x0 y0 z0 w0 => injection e with e1; subst e1; exact k.1 _ _ _ _ _ _ rfl + | _ => cases e + · cases h with + | refl => exact k.2.1 p b x y e + | ntfn q b0 x0 y0 => injection e with e1; subst e1; exact k.2.1 _ _ _ _ rfl + | _ => cases e + · cases h with + | refl => exact k.2.2.1 p g e + | reply q g0 g' => injection e with e1; subst e1; exact k.2.2.1 _ _ rfl + | _ => cases e + +end deriv + +section keep +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {OS : Set U64} {s s' : S} + +variable (st) in +/-- **The slot caps of `s'` are derived from those of `s`**, both ways (module docstring). -/ +structure CapsFromA (OS : Set U64) (s s' : S) : Prop where + back : ∀ u ∈ OS, capA st s' u = .Null ∨ ∃ v ∈ OS, DerivA (capA st s' u) (capA st s v) + fwd : ∀ v ∈ OS, capA st s v = .Null ∨ ∃ u ∈ OS, DerivA (capA st s' u) (capA st s v) + +variable (F : CapsFromA st OS s s') +include F + +/-- A non-null new cap has an old source. -/ +theorem CapsFromA.src {u : U64} (hu : u ∈ OS) {e : capability.Cap} (hc : capA st s' u = e) (hn : e ≠ .Null) : + ∃ v ∈ OS, DerivA e (capA st s v) := by + rcases F.back u hu with h | ⟨v, hv, h⟩ + · exact absurd (hc.symm.trans h) hn + · exact ⟨v, hv, hc ▸ h⟩ + +/-- A new cap of a kind derivation does not change is an old cap. -/ +theorem CapsFromA.fixed {u : U64} (hu : u ∈ OS) {e : capability.Cap} (he : FixedCapA e) (hn : e ≠ .Null) + (hc : capA st s' u = e) : ∃ v ∈ OS, capA st s v = e := by + obtain ⟨v, hv, h⟩ := F.src hu hc hn + exact ⟨v, hv, h.back_eq he rfl⟩ + +/-- A new untyped cap is an old one with a smaller or equal free index. -/ +theorem CapsFromA.untyped {u : U64} (hu : u ∈ OS) {q b fi : U64} {d : Bool} (hc : capA st s' u = .Untyped q b fi d) : + ∃ v ∈ OS, ∃ fi0 : U64, capA st s v = .Untyped q b fi0 d ∧ fi0.val ≤ fi.val := by + obtain ⟨v, hv, h⟩ := F.src hu hc (by simp) + obtain ⟨fi0, e, le⟩ := h.ut_back rfl + exact ⟨v, hv, fi0, e, le⟩ + +theorem UtFreeA.derivBack {dev : Bool → Prop} {a : Nat} (hf : UtFreeA st OS s' dev a) : UtFreeA st OS s dev a := by + obtain ⟨u, hu, q, b, fi, d, hc, hd, h1, h2⟩ := hf + obtain ⟨v, hv, fi0, e, le⟩ := F.untyped hu hc + exact ⟨v, hv, q, b, fi0, d, e, hd, by omega, h2⟩ + +theorem ObjAllocA.derivA {Z Z' : XSetsA} {H : Heap} + (hJ : ∀ p bytes, JointObjectA Z' H p bytes → JointObjectA Z H p bytes) (o : ObjAllocA (st := st) Z H OS s) : + ObjAllocA (st := st) Z' H OS s' := fun p bytes hp u hu q b fi d hc => by + obtain ⟨v, hv, fi0, e, le⟩ := F.untyped hu hc + rcases o p bytes (hJ p bytes hp) v hv q b fi0 d e with h | h | ⟨h1, h2⟩ + · exact Or.inl h + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr ⟨h1, by omega⟩) + +theorem IdleAnchorOffA.derivA (o : IdleAnchorOffA (st := st) OS s) : IdleAnchorOffA (st := st) OS s' := + fun u hu q b fi d hc => by + obtain ⟨v, hv, fi0, e, -⟩ := F.untyped hu hc + exact o v hv q b fi0 d e + +theorem UtBlk39A.derivA (o : UtBlk39A st OS s) : UtBlk39A st OS s' := fun u hu q b fi d hc => by + obtain ⟨v, hv, fi0, e, -⟩ := F.untyped hu hc + exact o v hv q b fi0 d e + +theorem UtWinCapA.derivA (o : UtWinCapA st OS s) : UtWinCapA st OS s' := fun u hu q b fi hc => by + obtain ⟨v, hv, fi0, e, -⟩ := F.untyped hu hc + exact o v hv q b fi0 e + +theorem DevWinCapA.derivA (o : DevWinCapA st OS s) : DevWinCapA st OS s' := fun u hu => + ⟨fun q b fi hc => by + obtain ⟨v, hv, fi0, e, -⟩ := F.untyped hu hc + exact (o v hv).1 q b fi0 e, + fun p sz r m e a v hc bits hb => by + obtain ⟨w, hw, d⟩ := F.src hu hc (by simp) + obtain ⟨r0, m0, e0, a0, v0, e1⟩ := d.frame_back rfl + exact (o w hw).2 p sz r0 m0 e0 a0 v0 e1 bits hb⟩ + +theorem UtZombieA.derivA (o : UtZombieA st OS s) : UtZombieA st OS s' := fun u hu p t n hc => by + obtain ⟨v, hv, e⟩ := F.fixed hu (e := .Zombie p t n) trivial (by simp) hc + exact o v hv p t n e + +theorem ZombieTcbTA.derivA {H : Heap} (o : ZombieTcbTA st H OS s) : ZombieTcbTA st H OS s' := fun u hu p n hc => by + obtain ⟨v, hv, e⟩ := F.fixed hu (e := .Zombie p capability.ZOMBIE_TCB n) trivial (by simp) hc + exact o v hv p n e + +theorem ZombieTcbNoVcpuA.derivA {H : Heap} (hT : ZombieTcbTA st H OS s) + (w : ∀ t ∈ H.tcbs, L.mem s' (wa t 3664#u64) = L.mem s (wa t 3664#u64)) + (o : ZombieTcbNoVcpuA st OS s) : ZombieTcbNoVcpuA st OS s' := fun u hu p n hc => by + obtain ⟨v, hv, e⟩ := F.fixed hu (e := .Zombie p capability.ZOMBIE_TCB n) trivial (by simp) hc + rw [w p (hT v hv p n e)] + exact o v hv p n e + +theorem CapTyA.derivA {H : Heap} (h : CapTyA st H OS s) : CapTyA st H OS s' := fun u hu => by + rcases F.back u hu with e | ⟨v, hv, d⟩ + · rw [e] + exact ⟨fun _ _ _ _ _ _ h => (by cases h), fun _ _ _ _ h => (by cases h), fun _ _ h => (by cases h), + fun _ _ h => (by cases h), fun _ h => (by cases h)⟩ + · exact CapKindA.derivA d (h v hv) + +theorem DevUtOffA.derivA {Z Z' : XSetsA} {H : Heap} {ia ie : Nat} (hk : L.ksb s' = L.ksb s) + (eJ : ∀ p bytes, JointObjectA Z' H p bytes → JointObjectA Z H p bytes) (eC : Z'.Cs = Z.Cs) + (h : DevUtOffA st Z H OS ia ie s) : DevUtOffA st Z' H OS ia ie s' := by + refine ⟨fun u hu q b fi hc p bytes hj => ?_, fun u hu q b fi hc => ?_, fun u hu q b fi hc => ?_, + fun u hu q b fi hc v hv q' b' fi' hc' => ?_⟩ + · obtain ⟨u0, hu0, fi0, e, -⟩ := F.untyped hu hc + exact h.obj u0 hu0 q b fi0 e p bytes (eJ p bytes hj) + · obtain ⟨u0, hu0, fi0, e, -⟩ := F.untyped hu hc + have hnb : ∀ c, L.nodeBase s' c = L.nodeBase s c := fun c => by simp [LawfulStore.nodeBase, hk] + have r := h.ks u0 hu0 q b fi0 e + unfold RegionOffNodes at r ⊢ + rw [eC, hk] + exact ⟨fun c hcs => by rw [hnb c]; exact r.1 c hcs, r.2⟩ + · obtain ⟨u0, hu0, fi0, e, -⟩ := F.untyped hu hc + exact h.idle u0 hu0 q b fi0 e + · obtain ⟨u0, hu0, fi0, e, -⟩ := F.untyped hu hc + obtain ⟨v0, hv0, fi1, e', -⟩ := F.untyped hv hc' + exact h.sep u0 hu0 q b fi0 e v0 hv0 q' b' fi1 e' + +theorem FrBaseApartA.derivA (h : FrBaseApartA st OS s) : FrBaseApartA st OS s' := fun y hy z hz fy kz => by + have ny : capA st s' y ≠ .Null := fun e => by rw [e] at fy; cases fy + have nz : capA st s' z ≠ .Null := fun e => by + have h0 := physA_eq capability.Cap.Null + simp only [objects.cap_is_physical] at h0 + have := kz.1 + rw [e, ← okinj h0] at this + cases this + obtain ⟨y0, hy0, dy⟩ := F.src hy rfl ny + obtain ⟨z0, hz0, dz⟩ := F.src hz rfl nz + rw [dy.isFr] at fy + rw [dz.ptr, dy.ptr] + exact h y0 hy0 z0 hz0 fy (dz.rngKey.1 kz) + +theorem UserFrSpecA.derivA {σ σ' : Fl4SpecA64.State Unit} + (dp : ∀ p dev pg, σ.kheap p = some (.archObj (.dataPage dev pg)) → σ'.kheap p = some (.archObj (.dataPage dev pg))) + (h : UserFrSpecA st OS s σ) : UserFrSpecA st OS s' σ' := by + intro y hy p size rights mapped ept asid va hc + obtain ⟨v, hv, d⟩ := F.src hy hc (by simp) + obtain ⟨r0, m0, e0, a0, v0, e⟩ := d.frame_back rfl + exact dp _ _ _ (h v hv p size r0 m0 e0 a0 v0 e) + +theorem DevFrDisjA.derivA {H : Heap} {σ σ' : Fl4SpecA64.State Unit} + (dp : ∀ p dev pg, σ.kheap p = some (.archObj (.dataPage dev pg)) → σ'.kheap p = some (.archObj (.dataPage dev pg))) + (h : DevFrDisjA st H OS s σ) : DevFrDisjA st H OS s' σ' := by + intro y hy p size rights mapped ept asid va hc k hk + obtain ⟨v, hv, d⟩ := F.src hy hc (by simp) + obtain ⟨r0, m0, e0, a0, v0, e⟩ := d.frame_back rfl + obtain ⟨h1, dev, pg, h2⟩ := h v hv p size r0 m0 e0 a0 v0 e k hk + exact ⟨h1, dev, pg, dp _ _ _ h2⟩ + +theorem FrThA.derivA {th th' : TypedHeapA} (thE : ∀ p z, th p = some (.other z) → th' p = some (.other z)) + (h : FrThA st th OS s) : FrThA st th' OS s' := + FrThA.ofSrc (fun y hy p size rights x1 x2 x3 x4 hc => by + obtain ⟨v, hv, d⟩ := F.src hy hc (by simp) + obtain ⟨r0, m0, e0, a0, v0, e⟩ := d.frame_back rfl + exact ⟨v, hv, r0, m0, e0, a0, v0, e⟩) thE h + +theorem SpecZeroFreeA.derivA {σ σ' : Fl4SpecA64.State Unit} + (um : σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory) (h : SpecZeroFreeA st OS s σ) : + SpecZeroFreeA st OS s' σ' := fun a hf => by rw [um]; exact h a (UtFreeA.derivBack F hf) + +theorem DeadA.derivBack {k : DKindA} {p : U64} (hd : DeadA st OS k s' p) : DeadA st OS k s p := fun v hv hn => by + rcases F.fwd v hv with e | ⟨u, hu, d⟩ + · exact not_namesD_nullA k p (e ▸ hn) + · exact hd u hu (d.names hn) + +theorem DeadExtA.derivA {Z Z' : XSetsA} {Rp : Set U64} {stack : U64 → List U64} {σ σ' : Fl4SpecA64.State Unit} + (HS : SpecSlotA σ σ') (eV : Z'.V = Z.V) (eC : Z'.C = Z.C) (eT : Z'.T = Z.T) + (tw : ∀ t ∈ Z.T, L.mem s' (wa t 3664#u64) = L.mem s (wa t 3664#u64)) + (vw : ∀ v ∈ Z.V, L.mem s' (wa v 24#u64) = L.mem s (wa v 24#u64)) (h : DeadExtA st Z OS Rp stack s σ) : + DeadExtA st Z' OS Rp stack s' σ' where + vcpu := fun v hv hd x hx => by + rw [eV] at hv + refine h.vcpu v hv (DeadA.derivBack F hd) x ?_ + exact HS.back hx (fun _ _ e => by cases e) (fun _ e => by cases e) + reply := fun r hr hd c hc => by + rw [eC] at hc + exact h.reply r hr (DeadA.derivBack F hd) c hc + asid := fun i p hi hd => h.asid i p (by rw [← HS.asid]; exact hi) (DeadA.derivBack F hd) + tvcpu := fun t ht hd => by + rw [eT] at ht + rw [tw t ht] + exact h.tvcpu t ht (DeadA.derivBack F hd) + vcDeadRun := fun v hv hd => by + rw [eV] at hv + rw [vw v hv] + exact h.vcDeadRun v hv (DeadA.derivBack F hd) + replyTcb := fun r hr hd x hx => + h.replyTcb r hr (DeadA.derivBack F hd) x (HS.back hx (fun _ _ e => by cases e) (fun _ e => by cases e)) + +/-- **J3 across a derivation.** The new CDT edges and original flags are old ones or sit at slots of existing +objects. -/ +theorem SpecFreeA.derivA {σ σ' : Fl4SpecA64.State Unit} (HS : SpecSlotA σ σ') + (hcdt : ∀ x y, σ'.cdt x = some y → ∀ sl, sl = x ∨ sl = y → + σ.kheap sl.1 ≠ none ∨ ∃ x0 y0, σ.cdt x0 = some y0 ∧ (sl = x0 ∨ sl = y0)) + (horig : ∀ x, σ'.isOriginalCap x = true → σ.kheap x.1 ≠ none ∨ σ.isOriginalCap x = true) + (h : SpecFreeA st OS s σ) : SpecFreeA st OS s' σ' := by + have heap : ∀ p : U64, UtFreeA st OS s (fun _ => True) p.val → ∀ q, σ.kheap q ≠ none → q ≠ specPtr p := + fun p hf q hq e => hq (e ▸ h.heap p hf) + refine ⟨fun p hf => ?_, fun x y hx p hf => ?_, fun x hx p hf => ?_⟩ + · have e := h.heap p (UtFreeA.derivBack F hf) + rcases HS.kh (specPtr p) with e' | ⟨r, cs, cs', e1, -⟩ | ⟨x, x', e1, -⟩ + · rw [e', e] + · rw [e] at e1; cases e1 + · rw [e] at e1; cases e1 + · have hf0 := UtFreeA.derivBack F hf + have key : ∀ sl, sl = x ∨ sl = y → sl.1 ≠ specPtr p := fun sl hsl => by + rcases hcdt x y hx sl hsl with hk | ⟨x0, y0, e, rfl | rfl⟩ + · exact heap p hf0 _ hk + · exact (h.cdt _ _ e p hf0).1 + · exact (h.cdt _ _ e p hf0).2 + exact ⟨key x (Or.inl rfl), key y (Or.inr rfl)⟩ + · have hf0 := UtFreeA.derivBack F hf + rcases horig x hx with hk | e + · exact heap p hf0 _ hk + · exact h.orig x e p hf0 + +end keep + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +set_option maxHeartbeats 2000000 in +/-- **`of_capDerivFhA` with the tracked-buffer set changing from `Bf` to `Bf'`** (`of_slotWBfA`). -/ +theorem JStage3A.of_capDerivBfA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {Bf' : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J5 : JSlotA S} + {Rj : Set U64 → JSlotA S} {s s' : S} + {a : A} {σ σ' : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7A st Rj) s a σ m) + {G' : MdbGhostA} (w : WritesIn st s s' (W7.CapW H.tcbs {b | InOS OS b} ∅)) + (F : CapsFromA st OS s s') + (HS : SpecSlotA σ σ') (W : WfKeepA σ σ') (hms : σ'.machineState = σ.machineState) + (hoff : ∀ b, FootNA (L := L) Z s b → ¬ W7.CapW H.tcbs {b | InOS OS b} ∅ b) + (hal : ∀ b, FootNA (L := L) Z s b → Aligned b) + (hcdt : ∀ x y, σ'.cdt x = some y → ∀ sl, sl = x ∨ sl = y → + σ.kheap sl.1 ≠ none ∨ ∃ x0 y0, σ.cdt x0 = some y0 ∧ (sl = x0 ∨ sl = y0)) + (horig : ∀ x, σ'.isOriginalCap x = true → σ.kheap x.1 ≠ none ∨ σ.isOriginalCap x = true) + (origLive : ∀ sl, σ'.isOriginalCap sl = true → AbsSlotA Z.th sl) + (nullOrig : ∀ sl, specCapAtA σ' sl = some .nullCap → σ'.isOriginalCap sl = false) + (hsc : ∀ sl a, specCapAtA σ' sl = some a → (∃ sl0, specCapAtA σ sl0 = some a) ∨ NoIdleCapV m n a) + (hvt : IdleSlotNewA σ σ' m n) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) + (fhOK' : ∀ t ∈ Z.T, NullOrEp H (capA st s' (slotIdx t layout.tcb.SLOT_FAULT_HANDLER))) + (symL : SymL st H Z.Cs s') + (sysl : SysL st H Z.Cs OS ia ie s') + (jx : JAtZAX (L := L) AR { Z with G := G' } s' a σ' m) + (sinv : SInvXA σ' m n) + (ep' : EpStateRel Z.T E Rp Bf' (L.mem s) σ) (iplaced' : IPlacedA N Z.T E Rp Bf') + (ipcPlace' : IpcSchedPlacementA Z.th N E Rp Bf') + (bufVs' : ∀ b, BufW Bf' b → ¬ VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b) (bfSpec' : ∀ bp ∈ Bf', bp.2 = specPtr bp.1) + (bufCover : BufCover st Z.T Bf' s' σ') + (bufNative : BufNativeA Z.T (L.mem s')) + (unmPt : UnmPtSpecA st { Z with G := G' } m OS s' σ') + (bufOff : BufOffA st { Z with G := G' } OS s' Bf') + (j5 : J5 { Z with G := G' } H OS s' σ' m) + (j7 : J7A st Rj { Z with G := G' } H OS s' σ' m) : + JStage3A st AR { Z with G := G' } H OS U Ac Pp Tb Lv ia ie N E Rp Bf' stack κ n J5 (J7A st Rj) s' a σ' m := by + have hO : OSOK H.tcbs H.omega H.frame OS := J.sys.ent.os + have hH := J.sys.ent.heap + have wA : WritesIn st s s' (fun b => Aligned b ∧ W7.CapW H.tcbs {b | InOS OS b} ∅ b) := + ⟨w.1, w.2.1, fun b hb hn => w.2.2 b hb fun h => hn ⟨hb, h⟩⟩ + have hB : ∀ b, (Aligned b ∧ W7.CapW H.tcbs {b | InOS OS b} ∅ b) → W7.CapW H.tcbs H.omega H.frame b := + fun b hb => os_capw hO hb.1 hb.2 + obtain ⟨-, -, -, -, -, fld, -, -⟩ := W7.blk_of_capw (BXI.blk J.sys.ent.bxi) wA hB + have twH : ∀ t ∈ H.tcbs, L.mem s' (wa t 3664#u64) = L.mem s (wa t 3664#u64) := fun t ht => + fld t ht 3664#u64 (by simp) (by simp) (by simp) + have tw : ∀ t ∈ Z.T, L.mem s' (wa t 3664#u64) = L.mem s (wa t 3664#u64) := fun t ht => + twH t (by rw [J.tcbs]; exact ht) + have um : σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory := by rw [hms] + have dp : ∀ p dev pg, σ.kheap p = some (.archObj (.dataPage dev pg)) → + σ'.kheap p = some (.archObj (.dataPage dev pg)) := + fun p dev pg h => HS.keep h (fun _ _ e => by cases e) (fun _ e => by cases e) + have sidle : SIdleA σ' m n := by + refine J.sidle.slotA HS (fun sl a h => ?_) hvt + rcases hsc sl a h with ⟨sl0, h0⟩ | h1 + · refine Or.inr ⟨fun c hc e => ?_, fun b e => ?_, (J.sidle.f6 sl0).2.2 a h0⟩ + · exact (J.sidle.f6 sl0).1 c hc (e ▸ h0) + · exact (J.sidle.f6 sl0).2.1 b (e ▸ h0) + · exact Or.inr h1 + -- a new cap of a kind derivation does not change is the cap of an old typed slot + have old : ∀ u, IsSlotA Z.th u → ∀ e : capability.Cap, FixedCapA e → e ≠ .Null → capA st s' u = e → + ∃ v, IsSlotA Z.th v ∧ capA st s v = e := fun u hu e he hn hc => by + obtain ⟨v, hv, h⟩ := F.fixed (hsub u hu) he hn hc + refine ⟨v, ?_, h⟩ + by_contra hv' + exact hn (h.symm.trans (hnul v hv hv')) + have zwf2 : DelInvA st Z.th s' := fun y hy p zt k h => by + obtain ⟨v, hv, e⟩ := old y hy (.Zombie p zt k) trivial (by simp) h + exact J.zwf v hv p zt k e + have fh2 := fhOK' + have pt2 : PtCapInA { Z with G := G' } (L.mem s') := fun y hy p a v h => by + obtain ⟨u, hu, e⟩ := old y hy (.PageTable p true a v) trivial (by simp) h + exact J.ptCapIn u hu p a v e + exact JStage3A.of_slotWBfA (ep' := ep') (iplaced' := iplaced') (ipcPlace' := ipcPlace') (bufVs' := bufVs') + (bfSpec' := bfSpec') (symL := symL) (zwf := zwf2) (fhOK := fh2) (ptCapIn := pt2) (G' := G') J w HS hms hoff sysl jx sinv + (ObjAllocA.derivA F (fun _ _ h => h) J.objAlloc) (IdleAnchorOffA.derivA F J.idleAnchor) + bufCover bufNative + (DeadExtA.derivA F (Z := Z) (Z' := { Z with G := G' }) HS rfl rfl rfl tw + (fun v hv => w.mem_eq (hal _ (.vcpu v _ hv (Or.inr rfl))) (hoff _ (.vcpu v _ hv (Or.inr rfl)))) J.deadExt) + (SpecFreeA.derivA F HS hcdt horig J.specFree) (UtBlk39A.derivA F J.utBlk) unmPt + (SpecZeroFreeA.derivA F um J.specZero) origLive nullOrig + (FrBaseApartA.derivA F J.frBase) (DevFrDisjA.derivA F dp J.devFr) (FinCovA.ofWfA J.finCov HS W) + (ZombieTcbNoVcpuA.derivA F J.zombieTcb twH J.zombieNoVcpu) (ZombieTcbTA.derivA F J.zombieTcb) bufOff + (UtZombieA.derivA F J.zombieBnd) + (DevUtOffA.derivA F (Z := Z) (Z' := { Z with G := G' }) w.1 (fun _ _ h => h) rfl J.devUtOff) + sidle (CapTyA.derivA F J.capTy) + (J.vcRun.keepA tw (fun v hv => w.mem_eq (hal _ (.vcpu v _ hv (Or.inr rfl))) (hoff _ (.vcpu v _ hv (Or.inr rfl)))) + (fun v hv => w.mem_eq (hal _ (.vcpu v _ hv (Or.inl rfl))) (hoff _ (.vcpu v _ hv (Or.inl rfl)))) + (fun t ht => fld t (by rw [J.tcbs]; exact ht) 3672#u64 (by simp) (by simp) (by simp))) + (J.vcSym1.keepA J.vcRun tw fun v hv => w.mem_eq (hal _ (.vcpu v _ hv (Or.inl rfl))) (hoff _ (.vcpu v _ hv (Or.inl rfl)))) + (J.vcSym2.keepA J.vcRun (fun v hv => w.mem_eq (hal _ (.vcpu v _ hv (Or.inr rfl))) (hoff _ (.vcpu v _ hv (Or.inr rfl)))) + (fun t ht => fld t (by rw [J.tcbs]; exact ht) 3672#u64 (by simp) (by simp) (by simp))) + (UtWinCapA.derivA F J.utWin) (DevWinCapA.derivA F J.devWin) (UserFrSpecA.derivA F dp J.userFr) (FrThA.derivA F (fun _ _ e => e) J.frTh) + j5 j7 + +/-- **`JStage3A` across slot writes whose caps are derived from old caps** (module docstring), with the fault-handler +words' validity as a hypothesis instead of their being kept (`of_capDerivA` is the wrapper that keeps them). -/ +theorem JStage3A.of_capDerivFhA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J5 : JSlotA S} {Rj : Set U64 → JSlotA S} {s s' : S} + {a : A} {σ σ' : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7A st Rj) s a σ m) + {G' : MdbGhostA} (w : WritesIn st s s' (W7.CapW H.tcbs {b | InOS OS b} ∅)) + (F : CapsFromA st OS s s') + (HS : SpecSlotA σ σ') (W : WfKeepA σ σ') (hms : σ'.machineState = σ.machineState) + (hoff : ∀ b, FootNA (L := L) Z s b → ¬ W7.CapW H.tcbs {b | InOS OS b} ∅ b) + (hal : ∀ b, FootNA (L := L) Z s b → Aligned b) + (hcdt : ∀ x y, σ'.cdt x = some y → ∀ sl, sl = x ∨ sl = y → + σ.kheap sl.1 ≠ none ∨ ∃ x0 y0, σ.cdt x0 = some y0 ∧ (sl = x0 ∨ sl = y0)) + (horig : ∀ x, σ'.isOriginalCap x = true → σ.kheap x.1 ≠ none ∨ σ.isOriginalCap x = true) + (origLive : ∀ sl, σ'.isOriginalCap sl = true → AbsSlotA Z.th sl) + (nullOrig : ∀ sl, specCapAtA σ' sl = some .nullCap → σ'.isOriginalCap sl = false) + (hsc : ∀ sl a, specCapAtA σ' sl = some a → (∃ sl0, specCapAtA σ sl0 = some a) ∨ NoIdleCapV m n a) + (hvt : IdleSlotNewA σ σ' m n) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) + (fhOK' : ∀ t ∈ Z.T, NullOrEp H (capA st s' (slotIdx t layout.tcb.SLOT_FAULT_HANDLER))) + (symL : SymL st H Z.Cs s') + (sysl : SysL st H Z.Cs OS ia ie s') + (jx : JAtZAX (L := L) AR { Z with G := G' } s' a σ' m) + (sinv : SInvXA σ' m n) + (bufCover : BufCover st Z.T Bf s' σ') + (bufNative : BufNativeA Z.T (L.mem s')) + (unmPt : UnmPtSpecA st { Z with G := G' } m OS s' σ') + (bufOff : BufOffA st { Z with G := G' } OS s' Bf) + (j5 : J5 { Z with G := G' } H OS s' σ' m) + (j7 : J7A st Rj { Z with G := G' } H OS s' σ' m) : + JStage3A st AR { Z with G := G' } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7A st Rj) s' a σ' m := + JStage3A.of_capDerivBfA J (w := w) (F := F) (HS := HS) (W := W) (hms := hms) (hoff := hoff) (hal := hal) + (hcdt := hcdt) (horig := horig) (origLive := origLive) (nullOrig := nullOrig) (hsc := hsc) (hvt := hvt) + (hsub := hsub) (hnul := hnul) (fhOK' := fhOK') (symL := symL) (sysl := sysl) (jx := jx) (sinv := sinv) + (ep' := J.ep) (iplaced' := J.iplaced) (ipcPlace' := J.ipcPlace) (bufVs' := J.bufVs) (bfSpec' := J.bfSpec) + (bufCover := bufCover) (bufNative := bufNative) (unmPt := unmPt) (bufOff := bufOff) (j5 := j5) (j7 := j7) + +/-- **`JStage3A` across slot writes whose caps are derived from old caps** (module docstring). -/ +theorem JStage3A.of_capDerivA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J5 : JSlotA S} {Rj : Set U64 → JSlotA S} {s s' : S} + {a : A} {σ σ' : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7A st Rj) s a σ m) + {G' : MdbGhostA} (w : WritesIn st s s' (W7.CapW H.tcbs {b | InOS OS b} ∅)) + (F : CapsFromA st OS s s') + (HS : SpecSlotA σ σ') (W : WfKeepA σ σ') (hms : σ'.machineState = σ.machineState) + (hoff : ∀ b, FootNA (L := L) Z s b → ¬ W7.CapW H.tcbs {b | InOS OS b} ∅ b) + (hal : ∀ b, FootNA (L := L) Z s b → Aligned b) + (hcdt : ∀ x y, σ'.cdt x = some y → ∀ sl, sl = x ∨ sl = y → + σ.kheap sl.1 ≠ none ∨ ∃ x0 y0, σ.cdt x0 = some y0 ∧ (sl = x0 ∨ sl = y0)) + (horig : ∀ x, σ'.isOriginalCap x = true → σ.kheap x.1 ≠ none ∨ σ.isOriginalCap x = true) + (origLive : ∀ sl, σ'.isOriginalCap sl = true → AbsSlotA Z.th sl) + (nullOrig : ∀ sl, specCapAtA σ' sl = some .nullCap → σ'.isOriginalCap sl = false) + (hsc : ∀ sl a, specCapAtA σ' sl = some a → (∃ sl0, specCapAtA σ sl0 = some a) ∨ NoIdleCapV m n a) + (hvt : IdleSlotNewA σ σ' m n) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) + (hfh : ∀ t ∈ Z.T, capA st s' (slotIdx t layout.tcb.SLOT_FAULT_HANDLER) = + capA st s (slotIdx t layout.tcb.SLOT_FAULT_HANDLER)) + (symL : SymL st H Z.Cs s') + (sysl : SysL st H Z.Cs OS ia ie s') + (jx : JAtZAX (L := L) AR { Z with G := G' } s' a σ' m) + (sinv : SInvXA σ' m n) + (bufCover : BufCover st Z.T Bf s' σ') + (bufNative : BufNativeA Z.T (L.mem s')) + (unmPt : UnmPtSpecA st { Z with G := G' } m OS s' σ') + (bufOff : BufOffA st { Z with G := G' } OS s' Bf) + (j5 : J5 { Z with G := G' } H OS s' σ' m) + (j7 : J7A st Rj { Z with G := G' } H OS s' σ' m) : + JStage3A st AR { Z with G := G' } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7A st Rj) s' a σ' m := + JStage3A.of_capDerivFhA J w F HS W hms hoff hal hcdt horig origLive nullOrig hsc hvt hsub hnul + (fun t ht => by rw [hfh t ht]; exact J.fhOK t ht) symL sysl jx sinv bufCover bufNative unmPt bufOff j5 j7 + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3CapFrameA.lean b/proofs/Fl4ProofsA64/P3/Stage3CapFrameA.lean new file mode 100644 index 000000000..f90607b43 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3CapFrameA.lean @@ -0,0 +1,417 @@ +/- +P3 (AArch64, merge line, D8 step 5 `of_capFrame`, part (i)): **`JStage3A` across an in-place single-slot cap write** +(frame map / unmap, page-table map / unmap, ASID assign, frame-cap update). + +`JStage3A.of_capFrame` composes `JAtZAX.of_setcapA` (`P3/SetCapJA`: the cap word write at one slot, with the spec's +`setCap`) with `JStage3A.of_slotWA` (`P3/Stage3CapSlotA`) and the generic keep lemmas of `P3/Stage3MapAlikeA` +(`MapAlikeA`: the new cap is the old one up to its mapping data). From the call-site facts of `of_setcapA` and the +cap-kind fact `MapAlikeA c (capA s x)` it proves, for the post state, the record's `sinv`, `objAlloc`, `idleAnchor`, +`deadExt`, `specFree`, `utBlk`, `specZero`, `origLive`, `frBase`, `devFr`, `finCov`, `zombieNoVcpu`, `zombieTcb`, +`zombieBnd`, `devUtOff` and `jx` (the joint relation). What the arm still supplies, as the one bundle +`CapFrameArmA` at the post state (each field is the arm's own fact; P1's `ArmQIAt` / `CapSrc` producers give `j7` +and `sysl`, the page-table words give `unmPt` / `bufOff`): + +* `sysl`: A64 P1's `SysL`; `j7`: the modular VSpace slot (reads the mapped flags); +* `unmPt`, `bufOff`: the fields that read the mapped flag (`SchedPageA` grows when a table is unmapped); +* `bufCover`, `bufNative`: the TCB buffer slot may be the written slot. + +The MDB slot `J5A` (`J5A.mapA`: `MdbInvA.mapA` by shape and badge, the IRQ facts, the typed slot sets) and +`nullOrig` (`nullOrig_setCapA`) are proved here, not asked of the arm. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3MapAlikeA +import Fl4ProofsA64.P3.Stage3J5A +import Fl4ProofsA64.P3.SetCapJA +import Fl4ProofsA64.P3.CnodeDelStepA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-- **The caps after a one-slot `set_cap`**: the written slot holds the new cap, every other `OS` slot is kept. -/ +theorem capA_setCapA {T Ω Sg OS : Set U64} (hO : OSOK T Ω Sg OS) {x : U64} (hos : x ∈ OS) {c : capability.Cap} + (hEnc : CapRt.EncWf c) {s s2 : S} (h : obj.Slot.set_cap st x s c = ok s2) : + capA st s2 x = c ∧ ∀ y ∈ OS, y ≠ x → capA st s2 y = capA st s y := by + obtain ⟨w0, w1, hen, hd⟩ := CapRt.decode_encode _ hEnc + have e := set_cap_eq (st := st) (s := s) (x := x) hen + rw [e] at h + have hs2 : L.wr (L.wr s x w0) (wa x 8#u64) w1 = s2 := (okinj h) + have a0x := hO.al0 hos + have a8x := hO.al hos (k := 8#u64) (by simp) (by simp) + obtain ⟨v8, -, -⟩ := vals hO hos + refine ⟨?_, fun y hy hne => ?_⟩ + · have hcx : capAtA (L.mem s2) x = c := by + have hd' := hd + unfold capAtA + rw [← hs2, L.mem_wr_ne a8x a0x (by omega), L.mem_wr_same a0x, L.mem_wr_same a8x] + rw [CapRt.decode_eq] at hd' + exact okinj hd' + rw [← capAtA_eq]; exact hcx + · rw [← hs2]; exact capA_wr2_other hO hos hy hne + +/-- `FinCovA` across a slot-only spec step that keeps the well-formed CNodes. -/ +theorem FinCovA.ofWfA {th : TypedHeapA} {σ σ' : Fl4SpecA64.State Unit} (h : FinCovA th σ) (HS : SpecSlotA σ σ') + (W : WfKeepA σ σ') : FinCovA th σ' := + ⟨fun q r cs hk hw => by + obtain ⟨cs0, hk0, hw0⟩ := (W q r).1 ⟨cs, hk, hw⟩ + exact h.cnode q r cs0 hk0 hw0, + fun q x hk => by + rcases HS.kh q with e | ⟨r, cs, cs', e1, e2⟩ | ⟨y, y', e1, e2, -⟩ + · rw [e] at hk; exact h.tcb q x hk + · rw [hk] at e2; cases e2 + · exact h.tcb q y e1⟩ + +/-- **`nullOrig` across a `set_cap` of a non-null cap** (l4v `valid_nullcaps`). -/ +theorem nullOrig_setCapA {sc : Fl4SpecA64.Cap} {cte : Fl4SpecA64.CslotPtr} {σ σ2 : Fl4SpecA64.State Unit} + (h : ((), σ2) ∈ (setCap (Z := Unit) sc cte σ).1) (hsc : sc ≠ .nullCap) (HS : SpecSlotA σ σ2) (W : WfKeepA σ σ2) + (horig : σ2.isOriginalCap = σ.isOriginalCap) + (hn : ∀ sl, specCapAtA σ sl = some .nullCap → σ.isOriginalCap sl = false) : + ∀ sl, specCapAtA σ2 sl = some .nullCap → σ2.isOriginalCap sl = false := by + intro sl hs + rw [horig] + refine hn sl ?_ + obtain ⟨o, o', hk, hσ, hc, hw⟩ := setCap_memA (p := cte.1) (cref := cte.2) h + have k2 : σ2.kheap = Function.update σ.kheap cte.1 (some o') := by + have := congrArg (fun x : Fl4SpecA64.State Unit => x.kheap) hσ + simpa using this + by_cases e : sl.1 = cte.1 + swap + · unfold specCapAtA at hs ⊢; rwa [k2, Function.update_of_ne e] at hs + have k1 : σ2.kheap sl.1 = some o' := by rw [k2, e, Function.update_self] + have k0 : σ.kheap sl.1 = some o := by rw [e]; exact hk + have key : Fl4SpecA64.capOf o' sl.2 = some .nullCap → Fl4SpecA64.capOf o sl.2 = some .nullCap := fun h => by + rcases hc _ _ h with ⟨-, e⟩ | h + · exact absurd e.symm hsc + · exact h + rcases HS.kh sl.1 with e' | ⟨r, cs, cs', e1, e2⟩ | ⟨y, y', e1, e2, -⟩ + · rw [k1, k0] at e'; cases e' + unfold specCapAtA at hs ⊢; rw [k1] at hs; rwa [k0] + · rw [k0] at e1; rw [k1] at e2; cases e1; cases e2 + unfold specCapAtA at hs ⊢ + rw [k1] at hs; rw [k0] + simp only at hs ⊢ + split at hs + case isFalse => cases hs + case isTrue hwf => + have w2 := (hw r).1 ⟨cs', rfl, hwf⟩ + obtain ⟨cs2, e3, w3⟩ := w2 + cases e3 + rw [if_pos w3] + exact key hs + · rw [k0] at e1; rw [k1] at e2; cases e1; cases e2 + unfold specCapAtA at hs ⊢ + rw [k1] at hs; rw [k0] + exact key hs + +/-- **`MdbInvA` across an in-place update of the chain members' caps** with the link words kept. -/ +theorem MdbInvA.mapA {OS : Set U64} {G : List (List U64)} {par : U64 → Option U64} {s s' : S} + (hM : MdbLk st OS G s) (hOk : Fl4Proofs.P3.Mdb.MdbOK par G) (hI : MdbInvA st G par s) + (hMs : ∀ u ∈ OS, MapAlikeA (capA st s' u) (capA st s u)) + (hl : ∀ u ∈ OS, L.mem s' (wa u 16#u64) = L.mem s (wa u 16#u64) ∧ L.mem s' (wa u 24#u64) = L.mem s (wa u 24#u64)) : + MdbInvA st G par s' := by + have mem : ∀ x ∈ G.flatten, x ∈ OS := fun x hx => ((hM.members x).1 hx).1 + exact MdbInvA.recap_alike hM hOk hI (fun x hx => (hMs x (mem x hx)).shape) (fun x hx => (hMs x (mem x hx)).badge) + (fun x hx => by unfold NxA; rw [(hl x (mem x hx)).2]) + (fun x hx => revA_congr (hl x (mem x hx)).2) (fun x hx => fbA_congr (hl x (mem x hx)).2) + +/-- **The caps after a `set_cap`**, located: a cap of the new state is the old one of its slot, or it is the written +one at the written slot. -/ +theorem specCapAtA_setCapLocA {sc : Fl4SpecA64.Cap} {cte : Fl4SpecA64.CslotPtr} {σ σ2 : Fl4SpecA64.State Unit} + (h : ((), σ2) ∈ (setCap (Z := Unit) sc cte σ).1) (HS : SpecSlotA σ σ2) (W : WfKeepA σ σ2) : + ∀ sl a, specCapAtA σ2 sl = some a → specCapAtA σ sl = some a ∨ (sl = cte ∧ a = sc) := by + intro sl a hs + obtain ⟨o, o', hk, hσ, hc, hw⟩ := setCap_memA (p := cte.1) (cref := cte.2) h + have k2 : σ2.kheap = Function.update σ.kheap cte.1 (some o') := by + have := congrArg (fun x : Fl4SpecA64.State Unit => x.kheap) hσ + simpa using this + by_cases e : sl.1 = cte.1 + swap + · left; unfold specCapAtA at hs ⊢; rwa [k2, Function.update_of_ne e] at hs + have k1 : σ2.kheap sl.1 = some o' := by rw [k2, e, Function.update_self] + have k0 : σ.kheap sl.1 = some o := by rw [e]; exact hk + have key : ∀ b, Fl4SpecA64.capOf o' b = some a → (b = cte.2 ∧ a = sc) ∨ Fl4SpecA64.capOf o b = some a := fun b h => by + rcases hc _ _ h with g | h + · exact Or.inl g + · exact Or.inr h + rcases HS.kh sl.1 with e' | ⟨r, cs, cs', e1, e2⟩ | ⟨y, y', e1, e2, -⟩ + · rw [k1, k0] at e'; cases e' + unfold specCapAtA at hs ⊢; rw [k1] at hs; left; rwa [k0] + · rw [k0] at e1; rw [k1] at e2; cases e1; cases e2 + unfold specCapAtA at hs ⊢ + rw [k1] at hs; rw [k0] + simp only at hs ⊢ + split at hs + case isFalse => cases hs + case isTrue hwf => + have w2 := (hw r).1 ⟨cs', rfl, hwf⟩ + obtain ⟨cs2, e3, w3⟩ := w2 + cases e3 + rw [if_pos w3] + rcases key _ hs with ⟨e2, ea⟩ | hh + · exact Or.inr ⟨Prod.ext e e2, ea⟩ + · exact Or.inl hh + · rw [k0] at e1; rw [k1] at e2; cases e1; cases e2 + unfold specCapAtA at hs ⊢ + rw [k1] at hs; rw [k0] + rcases key _ hs with ⟨e2, ea⟩ | hh + · exact Or.inr ⟨Prod.ext e e2, ea⟩ + · exact Or.inl hh + +/-- **The caps after a `set_cap`**: a cap of the new state is an old one or the written one (generalising +`nullOrig_setCapA`). -/ +theorem specCapAtA_setCapA {sc : Fl4SpecA64.Cap} {cte : Fl4SpecA64.CslotPtr} {σ σ2 : Fl4SpecA64.State Unit} + (h : ((), σ2) ∈ (setCap (Z := Unit) sc cte σ).1) (HS : SpecSlotA σ σ2) (W : WfKeepA σ σ2) : + ∀ sl a, specCapAtA σ2 sl = some a → specCapAtA σ sl = some a ∨ a = sc := + fun sl a hs => (specCapAtA_setCapLocA h HS W sl a hs).imp_right And.right + +/-- **A related slot with a non-null FL4 cap does not hold the spec null cap.** -/ +theorem specCap_nnA {th : TypedHeapA} {mem : U64 → U64} {σ : Fl4SpecA64.State Unit} (hR : StateRelA th mem σ) + {sl : Fl4SpecA64.CslotPtr} (hA : AbsSlotA th sl) (hc : capAtA mem (cslotAddrA sl) ≠ .Null) : + specCapAtA σ sl ≠ some .nullCap := by + intro h + have key : ∀ c, specCapAtA σ sl = some c → CapRelA (capAtA mem (cslotAddrA sl)) c → False := by + intro c e hr + rw [h] at e; cases e + exact CapRelA.ne_null hr hc rfl + obtain ⟨o, idx⟩ := sl + obtain ⟨p, e1, hc' | ⟨ht, i, hi, e2⟩⟩ := hA + · simp only at e1 hc'; subst e1 + obtain ⟨cs, hk2, hwf, hr⟩ := hR.cnode p _ hc' + obtain ⟨c, hcc, hrel⟩ := hr idx rfl + exact key c (by unfold specCapAtA; simp only [hk2]; rw [if_pos hwf]; exact hcc) hrel + · simp only at e1 e2; subst e1 e2 + obtain ⟨y, hk2, hr⟩ := hR.tcb p ht + obtain ⟨c, hcc, hrel⟩ := hr i hi + refine key c (by unfold specCapAtA; simp only [hk2]; exact hcc) ?_ + unfold cslotAddrA; rw [ofBl_tcbIdx i (by unfold tcbSlotsA at hi; omega)]; exact hrel + +/-- (record edit #8b) **A step that puts no new non-null cap in slot 1 of an idle thread.** -/ +def IdleSlotNewA (σ σ' : Fl4SpecA64.State Unit) (m : Fl4SpecA64.McState) (n : Nat) : Prop := + ∀ c < n, ∀ a, specCapAtA σ' (m.idleThreadOf c, Fl4SpecA64.tcbCnodeIndex 1) = some a → + specCapAtA σ (m.idleThreadOf c, Fl4SpecA64.tcbCnodeIndex 1) = some a ∨ a = .nullCap + +/-- An idle thread's slot 1 holds the null cap. -/ +theorem SIdleA.idleSlotCapA {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} {n : Nat} (h : SIdleA σ m n) + {c : Nat} (hc : c < n) : specCapAtA σ (m.idleThreadOf c, Fl4SpecA64.tcbCnodeIndex 1) = some .nullCap := by + obtain ⟨x, hx, -⟩ := h.f3 c hc + have e := h.idleSlot c hc + rw [vtCapA_tcbI hx] at e + unfold specCapAtA + simp only [hx] + exact e + +/-- (record edit #8b) **A step whose changed slots are CNode slots** puts nothing in an idle thread's slots. -/ +theorem IdleSlotNewA.of_cnodeA {σ σ' : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} {n : Nat} (h : SIdleA σ m n) + (hl : ∀ sl a, specCapAtA σ' sl = some a → specCapAtA σ sl = some a ∨ ∃ r cs, σ.kheap sl.1 = some (.cnode r cs)) : + IdleSlotNewA σ σ' m n := fun c hc a ha => by + rcases hl _ _ ha with g | ⟨r, cs, g⟩ + · exact Or.inl g + · obtain ⟨x, hx, -⟩ := h.f3 c hc + simp only at g + rw [hx] at g; cases g + +/-- (record edit #8b) **A `set_cap` over a slot with a non-null FL4 cap** is not at an idle thread's slot 1. -/ +theorem IdleSlotNewA.of_inplaceA {th : TypedHeapA} {mem : U64 → U64} {σ σ2 : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} {n : Nat} (h : SIdleA σ m n) (hR : StateRelA th mem σ) {cte : Fl4SpecA64.CslotPtr} + (hA : AbsSlotA th cte) (hc0 : capAtA mem (cslotAddrA cte) ≠ .Null) {sc : Fl4SpecA64.Cap} + (hs : ((), σ2) ∈ (setCap (Z := Unit) sc cte σ).1) (HS : SpecSlotA σ σ2) (W : WfKeepA σ σ2) : + IdleSlotNewA σ σ2 m n := fun c hc a ha => by + rcases specCapAtA_setCapLocA hs HS W _ _ ha with g | ⟨e, -⟩ + · exact Or.inl g + · exact absurd (e ▸ h.idleSlotCapA hc) (specCap_nnA hR hA hc0) + +/-- **F3–F6 across a slot-only spec step** whose new caps are old or non-idle. -/ +theorem SIdleA.slotA {σ σ' : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} {n : Nat} (h : SIdleA σ m n) + (HS : SpecSlotA σ σ') + (hc : ∀ sl a, specCapAtA σ' sl = some a → specCapAtA σ sl = some a ∨ NoIdleCapV m n a) + (hv : IdleSlotNewA σ σ' m n) : SIdleA σ' m n where + f3 := fun c hc' => by + obtain ⟨x, hx, hs⟩ := h.f3 c hc' + rcases HS.kh (m.idleThreadOf c) with e | ⟨r, cs, cs', e1, -⟩ | ⟨y, y', e1, e2, g⟩ + · exact ⟨x, by rw [e]; exact hx, hs⟩ + · rw [hx] at e1; cases e1 + · rw [hx] at e1 + have hxy := KernelObject.tcb.inj (Option.some.inj e1) + subst hxy + exact ⟨y', e2, (tcbNcA_eq g).1.trans hs⟩ + f4 := fun y k hy => by + refine h.f4 y k ?_ + rcases HS.kh Fl4SpecA64.idleScPtr with e | ⟨r, cs, cs', -, e2⟩ | ⟨x, x', -, e2, -⟩ + · rw [← e]; exact hy + · rw [hy] at e2; cases e2 + · rw [hy] at e2; cases e2 + f5 := fun p y k hy he => by + have hb : σ.kheap p = some (.schedContext y k) := by + rcases HS.kh p with e | ⟨r, cs, cs', -, e2⟩ | ⟨x, x', -, e2, -⟩ + · rw [← e]; exact hy + · rw [hy] at e2; cases e2 + · rw [hy] at e2; cases e2 + exact h.f5 p y k hb (by rw [← HS.idle]; exact he) + f6 := h.f6.of_new hc + f7 := fun y k hy => by + rw [HS.time] + refine h.f7 y k ?_ + rcases HS.kh Fl4SpecA64.idleScPtr with e | ⟨r, cs, cs', -, e2⟩ | ⟨x, x', -, e2, -⟩ + · rw [← e]; exact hy + · rw [hy] at e2; cases e2 + · rw [hy] at e2; cases e2 + idleSlot := fun c hc' => by + obtain ⟨x, hx, -⟩ := h.f3 c hc' + obtain ⟨y, hy, -⟩ : ∃ y, σ'.kheap (m.idleThreadOf c) = some (.tcb y) ∧ True := by + rcases HS.kh (m.idleThreadOf c) with e | ⟨r, cs, cs', e1, -⟩ | ⟨z, z', -, e2, -⟩ + · exact ⟨x, by rw [e]; exact hx, trivial⟩ + · rw [hx] at e1; cases e1 + · exact ⟨z', e2, trivial⟩ + have e1 : specCapAtA σ' (m.idleThreadOf c, Fl4SpecA64.tcbCnodeIndex 1) = some y.tcbVtable := by + unfold specCapAtA; simp only [hy]; rfl + rw [vtCapA_tcbI hy] + rcases hv c hc' _ e1 with g | g + · rw [h.idleSlotCapA hc'] at g; exact g.symm + · rw [g] + idleBound := fun c hc' => by + obtain ⟨x, hx, -⟩ := h.f3 c hc' + have b := h.idleBound c hc' + rcases HS.kh (m.idleThreadOf c) with e | ⟨r, cs, cs', e1, -⟩ | ⟨z, z', e1, e2, g⟩ + · exact (idleVtSameA e (h.idleSlot c hc') b).2 + · rw [hx] at e1; cases e1 + · rw [vcpuOfA_tcbI e2, (tcbNcA_eq g).2.2.2.2.1, ← vcpuOfA_tcbI e1]; exact b + +variable (st) in +/-- **What an in-place arm supplies at the post state** (module docstring). -/ +structure CapFrameArmA (AR : A → HwRootA → Prop) (Z : XSetsA) (H : Heap) (OS U Ac Pp Tb : Set U64) (Lv : U64 → Nat) + (ia ie : Nat) (Bf : Set (U64 × Fl4SpecA64.Word)) (T : Set U64) (J7 : JSlotA S) (s2 : S) + (σ2 : Fl4SpecA64.State Unit) (m : Fl4SpecA64.McState) : Prop where + sysl : SysL st H Z.Cs OS ia ie s2 + symL : SymL st H Z.Cs s2 + unmPt : UnmPtSpecA st Z m OS s2 σ2 + bufOff : BufOffA st Z OS s2 Bf + bufCover : BufCover st T Bf s2 σ2 + bufNative : BufNativeA T (L.mem s2) + j7 : J7 Z H OS s2 σ2 m + +set_option maxHeartbeats 2000000 in +/-- **`JStage3A` across an in-place single-slot cap write** (module docstring). -/ +theorem JStage3A.of_capFrame {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {Rj : Set U64 → JSlotA S} {s : S} {a : A} + {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st Rj) s a σ m) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hoff : ∀ b, FootNA (L := L) Z s b → ¬ W7.CapW H.tcbs {b | InOS OS b} ∅ b) + (hal : ∀ b, FootNA (L := L) Z s b → Aligned b) + (cte : Fl4SpecA64.CslotPtr) (hab : AbsSlotA Z.th cte) {c : capability.Cap} {sc : Fl4SpecA64.Cap} + (hEnc : CapRt.EncWf c) (hca : CapRelA c sc) (hT : TreeA st OS Z.G.chains Z.G.par s) + (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) (hcn : c ≠ .Null) + (hc0 : capA st s (cslotAddrA cte) ≠ .Null) (hM : MapAlikeA c (capA st s (cslotAddrA cte))) + (hok : CapObjOkA Z c) (hirq : CapOkA σ.interruptIrqNode cte sc) (hsci : NoIdleCapV m n sc) + (hpt : ∀ p a v, c ≠ .PageTable p true a v) + (arm : ∀ s2 σ2, obj.Slot.set_cap st (cslotAddrA cte) s c = ok s2 → ((), σ2) ∈ (setCap (Z := Unit) sc cte σ).1 → + SpecSlotA σ σ2 → WritesIn st s s2 (W7.CapW H.tcbs {b | InOS OS b} ∅) → + JAtZAX (L := L) AR Z s2 a σ2 m → capA st s2 (cslotAddrA cte) = c → + CapFrameArmA st AR Z H OS U Ac Pp Tb Lv ia ie Bf Z.T (J7A st Rj) s2 σ2 m) : + ∃ s2 σ2, obj.Slot.set_cap st (cslotAddrA cte) s c = ok s2 ∧ ((), σ2) ∈ (setCap (Z := Unit) sc cte σ).1 ∧ + SpecSlotA σ σ2 ∧ WritesIn st s s2 (W7.CapW H.tcbs {b | InOS OS b} ∅) ∧ + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st Rj) s2 a σ2 m := by + have hO : OSOK H.tcbs H.omega H.frame OS := J.sys.ent.os + obtain ⟨s2, σ2, hs2, hσ2, HS, w, jx, T2⟩ := + J.j.of_setcapA hO hsub hoff hal cte hab hEnc hca hT hnul hcn hc0 hM.ua hok + have hx : cslotAddrA cte ∈ OS := hsub _ (hab.isSlot J.j.1.place) + obtain ⟨hcx, hco⟩ := capA_setCapA hO hx hEnc hs2 + have hMs : ∀ u ∈ OS, MapAlikeA (capA st s2 u) (capA st s u) := fun u hu => by + by_cases e : u = cslotAddrA cte + · subst e; rw [hcx]; exact hM + · rw [hco u hu e]; exact .refl _ + obtain ⟨hcdt, horig, hms⟩ := setCap_keepsA hσ2 + have ar := arm s2 σ2 hs2 hσ2 HS w jx hcx + have hH := J.sys.ent.heap + have wA : WritesIn st s s2 (fun b => Aligned b ∧ W7.CapW H.tcbs {b | InOS OS b} ∅ b) := + ⟨w.1, w.2.1, fun b hb hn => w.2.2 b hb fun h => hn ⟨hb, h⟩⟩ + have hB : ∀ b, (Aligned b ∧ W7.CapW H.tcbs {b | InOS OS b} ∅ b) → W7.CapW H.tcbs H.omega H.frame b := + fun b hb => os_capw hO hb.1 hb.2 + obtain ⟨blk, -, -, -, -, fld, nod, -⟩ := W7.blk_of_capw (BXI.blk J.sys.ent.bxi) wA hB + have twH : ∀ t ∈ H.tcbs, L.mem s2 (wa t 3664#u64) = L.mem s (wa t 3664#u64) := fun t ht => + fld t ht 3664#u64 (by simp) (by simp) (by simp) + have tw : ∀ t ∈ Z.T, L.mem s2 (wa t 3664#u64) = L.mem s (wa t 3664#u64) := fun t ht => + twH t (by rw [J.tcbs]; exact ht) + have sinv : SInvXA σ2 m n := + J.sinv.of_fr (setCap_ndr sc cte σ ((), σ2) hσ2) (setCap_irq hirq σ rfl J.sinv.irqN ((), σ2) hσ2).2 + have um : σ2.machineState.underlyingMemory = σ.machineState.underlyingMemory := by rw [hms] + have W := setCap_wfA sc cte σ ((), σ2) hσ2 + have hTr : TreeA st OS Z.G.chains Z.G.par s := + J.j.1.cdt.tree.grow J.j5.2.1 (fun x hx hn => J.j5.2.2 x hx hn) + obtain ⟨w0, w1, hen, -⟩ := CapRt.decode_encode _ hEnc + have es2 : L.wr (L.wr s (cslotAddrA cte) w0) (wa (cslotAddrA cte) 8#u64) w1 = s2 := + okinj ((set_cap_eq (st := st) (s := s) (x := cslotAddrA cte) hen).symm.trans hs2) + have hl : ∀ u ∈ OS, L.mem s2 (wa u 16#u64) = L.mem s (wa u 16#u64) ∧ L.mem s2 (wa u 24#u64) = L.mem s (wa u 24#u64) := + fun u hu => by rw [← es2]; exact set_cap_links hO hx u hu + have mem : ∀ y ∈ Z.G.chains.flatten, y ∈ OS := fun y hy => ((hTr.lk.members y).1 hy).1 + have irqC : ∀ y ∈ Z.G.chains.flatten, ∀ i, capA st s2 y = .IrqHandler i ↔ capA st s y = .IrqHandler i := + fun y hy i => ⟨(hMs y (mem y hy)).back_eq (e := .IrqHandler i) trivial, + (hMs y (mem y hy)).fwd_eq (e := .IrqHandler i) trivial⟩ + have j5 : J5A st { Z with G := Z.G } H OS s2 σ2 m := + ⟨⟨MdbInvA.mapA hTr.lk hTr.tree J.j5.1.1 hMs hl, IrqRunA.keepIrq J.j5.1.2.1 irqC, + HandlersActiveA.keepIrq J.j5.1.2.2 (fun y hy i h => (irqC y hy i).1 h) w.1 (fun i hi => by + have eI : irqStAddrA (L.ksb s) i = irqWordA (L.ksb s) i := rfl + rw [eI] + exact w.mem_eq (hal _ (.irq i hi)) (hoff _ (.irq i hi)))⟩, J.j5.2.1, + fun y hy hn => by + have ne : y ≠ cslotAddrA cte := fun e => hn (e ▸ hab.isSlot J.j.1.place) + rw [hco y hy ne]; exact J.j5.2.2 y hy hn⟩ + have nul : ∀ sl, specCapAtA σ2 sl = some .nullCap → σ2.isOriginalCap sl = false := + nullOrig_setCapA hσ2 (CapRelA.ne_null hca hcn) HS W horig J.nullOrig + refine ⟨s2, σ2, hs2, hσ2, HS, w, ?_⟩ + have kfh : ∀ t ∈ Z.T, slotIdx t layout.tcb.SLOT_FAULT_HANDLER ∈ OS := fun t ht => + hsub _ (isSlot_fhA ((J.sr.thT t).2 ht)) + have zwf2 : DelInvA st Z.th s2 := fun y hy p zt k h => + J.zwf y hy p zt k ((hMs y (hsub y hy)).back_eq (e := .Zombie p zt k) trivial h) + have fh2 : ∀ t ∈ Z.T, NullOrEp H (capA st s2 (slotIdx t layout.tcb.SLOT_FAULT_HANDLER)) := fun t ht => by + rcases J.fhOK t ht with e | ⟨ep, b0, b1, b2, b3, b4, e, he⟩ + · exact Or.inl ((hMs _ (kfh t ht)).fwd_eq (e := .Null) trivial e) + · exact Or.inr ⟨ep, b0, b1, b2, b3, b4, + (hMs _ (kfh t ht)).fwd_eq (e := .Endpoint ep b0 b1 b2 b3 b4) trivial e, he⟩ + have pt2 : PtCapInA Z (L.mem s2) := fun y hy p a v h => by + change capA st s2 y = _ at h + by_cases e : y = cslotAddrA cte + · rw [e, hcx] at h; exact absurd h (hpt p a v) + · rw [hco y (hsub y hy) e] at h; exact J.ptCapIn y hy p a v h + exact JStage3A.of_slotWA (symL := ar.symL) (zwf := zwf2) (fhOK := fh2) (ptCapIn := pt2) (G' := Z.G) J w HS hms hoff ar.sysl jx sinv + (ObjAllocA.mapA hMs (fun _ _ h => h) J.objAlloc) (IdleAnchorOffA.mapA hMs J.idleAnchor) + ar.bufCover ar.bufNative + (DeadExtA.mapA (Z := Z) (Z' := { Z with G := Z.G }) hMs HS rfl rfl rfl tw + (fun v hv => w.mem_eq (hal _ (.vcpu v _ hv (Or.inr rfl))) (hoff _ (.vcpu v _ hv (Or.inr rfl)))) J.deadExt) + (SpecFreeA.mapA hMs HS hcdt horig J.specFree) (UtBlk39A.mapA hMs J.utBlk) ar.unmPt + (SpecZeroFreeA.mapA hMs um J.specZero) (fun sl h => J.origLive sl (by rw [← horig]; exact h)) nul + (FrBaseApartA.mapA hMs J.frBase) + (DevFrDisjA.mapA hMs (fun p dev pg h => HS.keep h (fun _ _ e => by cases e) (fun _ e => by cases e)) J.devFr) + (FinCovA.ofWfA J.finCov HS W) (ZombieTcbNoVcpuA.mapA hMs J.zombieTcb twH J.zombieNoVcpu) + (ZombieTcbTA.mapA hMs J.zombieTcb) ar.bufOff (UtZombieA.mapA hMs J.zombieBnd) + (DevUtOffA.mapA (Z := Z) (Z' := { Z with G := Z.G }) hMs w.1 (fun _ _ h => h) + rfl J.devUtOff) + (J.sidle.slotA HS (fun sl a h => (specCapAtA_setCapA hσ2 HS W sl a h).imp_right fun (e : a = sc) => by rw [e]; exact hsci) + (IdleSlotNewA.of_inplaceA J.sidle J.j.1.st hab hc0 hσ2 HS W)) + (CapTyA.mapA hMs J.capTy) + (J.vcRun.keepA tw (fun v hv => w.mem_eq (hal _ (.vcpu v _ hv (Or.inr rfl))) (hoff _ (.vcpu v _ hv (Or.inr rfl)))) + (fun v hv => w.mem_eq (hal _ (.vcpu v _ hv (Or.inl rfl))) (hoff _ (.vcpu v _ hv (Or.inl rfl)))) + (fun t ht => fld t (by rw [J.tcbs]; exact ht) 3672#u64 (by simp) (by simp) (by simp))) + (J.vcSym1.keepA J.vcRun tw fun v hv => w.mem_eq (hal _ (.vcpu v _ hv (Or.inl rfl))) (hoff _ (.vcpu v _ hv (Or.inl rfl)))) + (J.vcSym2.keepA J.vcRun (fun v hv => w.mem_eq (hal _ (.vcpu v _ hv (Or.inr rfl))) (hoff _ (.vcpu v _ hv (Or.inr rfl)))) + (fun t ht => fld t (by rw [J.tcbs]; exact ht) 3672#u64 (by simp) (by simp) (by simp))) + (UtWinCapA.mapA hMs J.utWin) (DevWinCapA.mapA hMs J.devWin) + (UserFrSpecA.mapA hMs (fun p dev pg h => HS.keep h (fun _ _ e => by cases e) (fun _ e => by cases e)) J.userFr) + (FrThA.mapA hMs (fun _ _ e => e) J.frTh) + j5 ar.j7 + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3CapInsertBufA.lean b/proofs/Fl4ProofsA64/P3/Stage3CapInsertBufA.lean new file mode 100644 index 000000000..dfb98c3b9 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3CapInsertBufA.lean @@ -0,0 +1,241 @@ +/- +P3 (aarch64, A64-P3 Lift 3b labels, SetIPCBuffer ≠ 0, step 3): **`JStage3A` across the slot-2 `cdt::insert`, growing `Bf`**. + +`JStage3A.capInsertTcbBufA` is `capInsertTcbSlotA` for the buffer slot 2 of a TCB: the tracked set grows to +`insert (w, specPtr w) Bf` (`of_capDerivBfA`, `bfSpec_insertA`), with the placement of the new words from step 2 +(`Stage3BufNewA`). Plain hypotheses (no named premise): `hl` / `hag` (step 2's post-delete hypotheses at the state +before the insert), `hnc` (the inserted cap is a native non-device frame, aligned to its size, whose range holds the 128 +words), and `hibw` / `hspec` (the buffer word `[t0 + 3512]` and the spec TCB's `tcbIpcBuffer` already hold `w`: what the +`setIpcBuf` step before the insert establishes). `hth` (typed-heap cover) is derived from `FrThA` (`JStage3A.bufThA`) and +`hlk` (the lookup returns the new pair) from `lookup_newBufA` (`Stage3BufLookupA`): neither is a hypothesis any more. + +New module. +-/ +import Fl4ProofsA64.P3.Stage3BufNewA +import Fl4ProofsA64.P3.Stage3BufLookupA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-- **The typed-heap cover of a live buffer** from `FrThA`: each word lies in the `other` object of its frame cap. -/ +theorem JStage3A.bufThA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J5 J7 : JSlotA S} {s : S} {a : A} + {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {w : U64} (hl : BufLiveA st OS s w) : + ∀ k : U64, k.val < 128 → ∃ p len, Z.th p = some (.other len) ∧ p.val ≤ (bufW w k).val ∧ + (bufW w k).val < p.val + len := fun k hk => by + obtain ⟨u, hu, ptr, size, rights, x1, x2, x3, x4, bits, hc, hb, h1, h2⟩ := hl.2.2 k hk + exact ⟨ptr, 2 ^ bits.val, J.frTh u hu ptr size rights x1 x2 x3 x4 bits hc hb, h1, h2⟩ + +set_option maxHeartbeats 4000000 in +/-- **`JStage3A` across a `cap_insert` of a derived frame cap into slot 2 of a TCB, growing the tracked buffers** +(module docstring). -/ +theorem JStage3A.capInsertTcbBufA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {ar : machine.Arch A} {nA : U64} {s : S} {a : A} + {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (ss sd : Fl4SpecA64.CslotPtr) {ps : U64} + (hCs : ss.1 = specPtr ps ∧ Z.th ps = some (.cnode ss.2.length)) + {t0 : U64} (ht0 : Z.th t0 = some .tcb) (hsd : sd = (specPtr t0, tcbCnodeIndex 2)) + (hsn : capA st s (cslotAddrA ss) ≠ .Null) (hdn : capA st s (cslotAddrA sd) = .Null) + (hdc : obj.sext48 (cslotAddrA sd) = ok (cslotAddrA sd)) + {nc : capability.Cap} {anc : Fl4SpecA64.Cap} (hwf : CapWfA nc) (hanc : CapEqA nc anc) + (hw : CapRt.EncWf nc) (hn : nc ≠ .Null) + (hD : Fl4ProofsA64.P3.DerivA nc (capA st s (cslotAddrA ss))) (hal : UA nc (capA st s (cslotAddrA ss))) + (hk : SameBadgeKindA nc (capA st s (cslotAddrA ss))) + (hsh : Fl4ProofsA64.P3.shapeA nc = Fl4ProofsA64.P3.shapeA (capA st s (cslotAddrA ss))) + (hsh1 : Fl4ProofsA64.P1.W7.R.ST.shapeA nc = Fl4ProofsA64.P1.W7.R.ST.shapeA (capA st s (cslotAddrA ss))) + (hbd1 : badgeA nc = badgeA (capA st s (cslotAddrA ss)) ∨ badgeA (capA st s (cslotAddrA ss)) = 0#u64) + (hbd : ∀ r b rs, (absCapA (capA st s (cslotAddrA ss)) = .endpointCap r b rs ∨ + absCapA (capA st s (cslotAddrA ss)) = .notificationCap r b rs) → b ≠ 0 → + Fl4SpecA64.capEpBadge (absCapA nc) = b) + (hirqB : ∀ i, capA st s (cslotAddrA ss) = .IrqHandler i → nc = .IrqHandler i) + (hirqN : ∀ i, nc = .IrqHandler i → capA st s (cslotAddrA ss) = .IrqHandler i ∨ + ∀ y ∈ Z.G.chains.flatten, capA st s y ≠ .IrqHandler i) + (hirqA : ∀ i, nc = .IrqHandler i → capA st s (cslotAddrA ss) = .IrqHandler i ∨ + (i.val < 5376 ∧ L.mem s (irqStAddrA (L.ksb s) i) ≠ 0#u64)) + (hz : ∀ p t k, capA st s (cslotAddrA ss) ≠ .Zombie p t k) + (hut : isUtA (capA st s (cslotAddrA ss)) = true → ∀ y, ¬ Fl4Proofs.P3.Mdb.Desc Z.G.par y (cslotAddrA ss)) + (hdo : ¬ IrqNodeA (L.ksb s) (fl4Ptr sd.1) ∨ IrqCapA (absCapA nc)) + (hcv : CapValidA Z.th (capA st s (cslotAddrA ss)) → CapValidA Z.th nc) (hsci : NoIdleCapV m n anc) + {w : U64} (hl : BufLiveA st OS s w) + (hag : ∀ k : U64, k.val < 128 → + (L.mem s (bufW w k)).bv = loadWordAt σ.machineState.underlyingMemory (specPtr (bufW w k))) + {baddr : U64} + (hnc : ∃ ptr size rights x1 x3 x4 bits, nc = .Frame ptr size rights false x1 false x3 x4 ∧ size.bits = ok bits ∧ + ptr.val % 2 ^ bits.val = 0 ∧ w = bufAddrOfA ptr baddr bits ∧ baddr.val % 1024 = 0) + (hibw : L.mem s (ibWordA t0) = baddr) + (hspec : ∀ x, σ.kheap (specPtr t0) = some (.tcb x) → x.tcbIpcBuffer = baddr.bv) + (symL : ∀ s2, cdt.insert st s nc (cslotAddrA ss) (cslotAddrA sd) = ok s2 → SymL st H Z.Cs s2) + (sysl : ∀ s2, cdt.insert st s nc (cslotAddrA ss) (cslotAddrA sd) = ok s2 → SysL st H Z.Cs OS ia ie s2) + (j7 : ∀ s2 σ2, cdt.insert st s nc (cslotAddrA ss) (cslotAddrA sd) = ok s2 → + ((), σ2) ∈ (Fl4SpecA64.capInsert (Z := Unit) anc ss sd σ).1 → + J7A st (J7R2A st ar nA) { Z with G := (insGA Z.G (cslotAddrA ss) (cslotAddrA sd) + (insBA σ ss (absCapA nc) (absCapA (capA st s (cslotAddrA ss))))) } H OS s2 σ2 m) : + ∃ s2 σ2, cdt.insert st s nc (cslotAddrA ss) (cslotAddrA sd) = ok s2 ∧ + ((), σ2) ∈ (Fl4SpecA64.capInsert (Z := Unit) anc ss sd σ).1 ∧ Sched.BodyKeepA σ σ2 ∧ + (∀ p y k, σ.kheap p = some (.schedContext y k) → σ2.kheap p = some (.schedContext y k)) ∧ + JStage3A st AR { Z with G := (insGA Z.G (cslotAddrA ss) (cslotAddrA sd) + (insBA σ ss (absCapA nc) (absCapA (capA st s (cslotAddrA ss))))) } + H OS U Ac Pp Tb Lv ia ie N E Rp (insert (w, specPtr w) Bf) stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s2 a σ2 m := by + have hi : 2 < tcbSlotsA := by simp [tcbSlotsA] + have hAd : AbsSlotA Z.th sd := by subst hsd; exact ⟨t0, rfl, Or.inr ⟨ht0, 2, hi, rfl⟩⟩ + have hkd : σ.kheap sd.1 ≠ none := by + obtain ⟨x', hk', -⟩ := J.j.1.st.tcb t0 ht0 + rw [hsd]; simp [hk'] + have hsd1 : ∀ c < n, sd ≠ (m.idleThreadOf c, tcbCnodeIndex 1) := fun c hc e => by + have h21 : tcbCnodeIndex 2 = tcbCnodeIndex 1 := by rw [hsd] at e; exact congrArg Prod.snd e + exact absurd ((tcbIdx_eq (by simp [tcbSlotsA]) (by simp [tcbSlotsA])).1 h21) (by decide) + obtain ⟨s2, σ2, hs2, hm, hbk, scF, P⟩ := JStage3A.capInsertPartsA (J := J) ss sd hCs hAd hkd hsd1 hsn hdn hdc hwf hanc + hw hn hD hal hk hsh hsh1 hbd1 hbd hirqB hirqN hirqA hz hut hdo hcv hsci symL sysl j7 + obtain ⟨w0, F, HS, W, hms, hcdtS, horig, origLive, nullOrig, hsc, jx, sinv, j5', hvt, ek, ed, hfull⟩ := P + have hO : OSOK H.tcbs H.omega H.frame OS := J.sys.ent.os + have hP := J.j.1.place + have E1 := J.toJEntA + have hoff := footN_offA E1 + have hfal := FootNA.aligned J.j.1 (bxi_nodes E1.sys.ent.bxi) E1.scAl E1.ap.vcAl + have eaB : ∀ t, cslotAddrA (specPtr t, tcbCnodeIndex 2) = sbWordA t := fun t => by + unfold cslotAddrA + rw [ofBl_tcbIdx 2 (by omega)] + exact Endp.bufSlot_eqA t + have esd : cslotAddrA sd = sbWordA t0 := by rw [hsd]; exact eaB t0 + have ht0T : t0 ∈ Z.T := (J.j.1.thT t0).1 ht0 + have wA : WritesIn st s s2 (fun b => Aligned b ∧ W7.CapW H.tcbs {b | InOS OS b} ∅ b) := writesIn_alAG w0 + have hB : ∀ b, (Aligned b ∧ W7.CapW H.tcbs {b | InOS OS b} ∅ b) → W7.CapW H.tcbs H.omega H.frame b := + fun b hb => os_capw hO hb.1 hb.2 + obtain ⟨-, -, -, -, -, fld, -, -⟩ := W7.blk_of_capw (BXI.blk J.sys.ent.bxi) wA hB + have sbS : ∀ t ∈ Z.T, sbWordA t ∈ OS := fun t ht => + J.j5.2.1 _ ⟨t, Or.inr ⟨(J.j.1.thT t).2 ht, 2, hi, (bufSlot_eqA t).symm⟩⟩ + have sbK : ∀ t ∈ Z.T, t ≠ t0 → capA st s2 (sbWordA t) = capA st s (sbWordA t) := fun t ht hne => + ek _ (sbS t ht) (cnSlot_ne_sbA hP hCs.1 hCs.2 ((J.j.1.thT t).2 ht)) (by + rw [← eaB t] + intro e + have hA : AbsSlotA Z.th (specPtr t, tcbCnodeIndex 2) := ⟨t, rfl, Or.inr ⟨(J.j.1.thT t).2 ht, 2, hi, rfl⟩⟩ + have e2 := cslotAddrA_inj hP hA hAd e + rw [hsd] at e2 + exact hne (specPtr_inj (congrArg Prod.fst e2))) + have ib : ∀ t ∈ Z.T, L.mem s2 (ibWordA t) = L.mem s (ibWordA t) := + fun t ht => fld t (by rw [J.tcbs]; exact ht) 3512#u64 (by simp) (by simp) (by simp) + obtain ⟨ptr, size, rights, x1, x3, x4, bits, hncE, hbits, hal, hwE, hbd⟩ := hnc + have hrk := bufAddr_rangeA (frame_bitsLtA hbits) hal hbd + rw [← hwE] at hrk + have hrange : ∀ k : U64, k.val < 128 → ptr.val ≤ (bufW w k).val ∧ (bufW w k).val < ptr.val + 2 ^ bits.val := + fun k hk => by + rw [bufW_valLiveA hl.2.1 hk] + omega + have capNew : capA st s2 (sbWordA t0) = nc := by rw [← esd]; exact ed + -- the buffer lookups: other TCBs by `mapA`, the target by `hlk` + have bufCoverO : BufCover st {t | t ∈ Z.T ∧ t ≠ t0} Bf s2 σ2 := by + refine BufCover.mapA (th := Z.th) (T := {t | t ∈ Z.T ∧ t ≠ t0}) ?_ ?_ J.j.1.st jx.1.st + (fun t ht => (J.j.1.thT t).2 ht.1) HS (fun t ht => ib t ht.1) + (fun t ht => by rw [sbK t ht.1 ht.2]; exact .refl _) + · intro t ht x b sb w' hx hb hl' + exact J.bufCover t ht.1 x b sb w' hx hb hl' + · exact fun t ht => J.bufNative t ht.1 + have bufCover : BufCover st Z.T (insert (w, specPtr w) Bf) s2 σ2 := by + intro t ht x b sb w' hx hb hl' + by_cases e : t = t0 + · rw [e] at hx hl' + have hxb : x.tcbIpcBuffer = baddr.bv := by + obtain ⟨x0, hx0, -⟩ := J.j.1.st.tcb t0 ht0 + have hbuf0 := hspec x0 hx0 + rcases HS.kh (specPtr t0) with e1 | ⟨r, cs, cs', h1, -⟩ | ⟨y, y', h1, h2, hnc'⟩ + · rw [e1, hx0] at hx + cases hx + exact hbuf0 + · rw [hx0] at h1; cases h1 + · rw [hx0] at h1 + cases h1 + rw [h2] at hx + cases hx + have := congrArg Fl4SpecA64.Tcb.tcbIpcBuffer hnc' + simpa [tcbNcA] using this.trans hbuf0 + obtain ⟨h1, h2⟩ := lookup_newBufA (st := st) jx.1.st ((J.j.1.thT t0).2 ht0T) hx + (by rw [capNew, hncE]) hbits (by rw [ib t0 ht0T]; exact hibw) hxb b hb hl' + rw [h1, h2, ← hwE] + exact ⟨Or.inl rfl, hl.1⟩ + · have := bufCoverO t ⟨ht, e⟩ x b sb w' hx hb hl' + exact ⟨Or.inr this.1, this.2⟩ + have bufNative : BufNativeA Z.T (L.mem s2) := + BufNativeA.mapA J.bufNative (fun t ht q sz' rr dv m' e' a' v' h => by + by_cases e : t = t0 + · subst e + rw [capNew, hncE] at h + cases h + rfl + · rw [sbK t ht e] at h + have hb := J.bufNative t ht + rw [bufSlot_eqA, capAtA_eq (st := st), h] at hb + exact hb) + have vk : ∀ Pt Ub : Set U64, VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt → + VsKeep st (L.ksb s) Pt s s2 := fun Pt Ub vr => + VsKeep.of_w vr.al w0 (fun b hb => J.vsr_offCapWA vr hb) + have unmPt : UnmPtSpecA st { Z with G := (insGA Z.G (cslotAddrA ss) (cslotAddrA sd) + (insBA σ ss (absCapA nc) (absCapA (capA st s (cslotAddrA ss))))) } m OS s2 σ2 := by + obtain ⟨Pt, Ub, vr, -, -, -, -, -, -, link, -, -⟩ := J.j7 + exact UnmPtSpecA.derivA F HS w0.1 w0.2.1 (vk Pt Ub vr) link + (fun p h => ⟨fun i hi hf => h.mem i hi (FootA.monoA + (Z := { Z with G := (insGA Z.G (cslotAddrA ss) (cslotAddrA sd) + (insBA σ ss (absCapA nc) (absCapA (capA st s (cslotAddrA ss))))), S1 := ∅ }) rfl rfl + (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) + (fun _ h => h) (FootA.of_ksA w0.1.symm w0.2.1.symm hf)), h.obj⟩) J.unmPt + -- BufOffA at the grown set + obtain ⟨hos, hvs, hpg, hvc, hvr⟩ := J.bufNewOffA hl + have pinOld : ∀ bp ∈ Bf, BufPinA st Z.T OS s2 bp.1 := fun bp hbp => + (J.bufOff.pin bp hbp).keepF fun t ht hO' ptr' size' rights' y1 y2 y3 y4 hc => by + by_cases e : t = t0 + · subst e + rw [← esd, hdn] at hc + cases hc + · exact ⟨ht, hO', rights', y1, y2, y3, y4, (sbK t ht e).trans hc⟩ + have B0 : BufOffA st { Z with G := (insGA Z.G (cslotAddrA ss) (cslotAddrA sd) + (insBA σ ss (absCapA nc) (absCapA (capA st s (cslotAddrA ss))))) } OS s2 Bf := + ⟨J.bufOff.os, fun b hb => by rw [w0.1]; exact J.bufOff.vs b hb, + fun b hb q hq => J.bufOff.page b hb q (SchedPageA.derivBackA F rfl rfl rfl rfl rfl hq), J.bufOff.vcw, pinOld⟩ + have bufOff := BufOffA.insertA B0 hos (fun k hk => by rw [w0.1]; exact hvs k hk) + (fun k hk p hq o o1 o2 => hpg k hk p (SchedPageA.derivBackA F rfl rfl rfl rfl rfl hq) o o1 o2) hvc + ⟨t0, ht0T, sbS t0 ht0T, ptr, size, rights, x1, false, x3, x4, bits, by rw [capNew, hncE], hbits, + fun k hk => ⟨bufW_alignedLiveA hl.2.1 hk, (hrange k hk).1, (hrange k hk).2⟩⟩ + have hfh : ∀ t ∈ Z.T, NullOrEp H (capA st s2 (slotIdx t layout.tcb.SLOT_FAULT_HANDLER)) := fun t ht => by + have eaF : cslotAddrA (specPtr t, tcbCnodeIndex 3) = slotIdx t layout.tcb.SLOT_FAULT_HANDLER := by + unfold cslotAddrA + rw [ofBl_tcbIdx 3 (by omega)] + exact (slotIdx_fhA t).symm + have ne : slotIdx t layout.tcb.SLOT_FAULT_HANDLER ≠ cslotAddrA sd := fun e => by + rw [← eaF] at e + have hA : AbsSlotA Z.th (specPtr t, tcbCnodeIndex 3) := + ⟨t, rfl, Or.inr ⟨(J.j.1.thT t).2 ht, 3, by simp [tcbSlotsA], rfl⟩⟩ + have e2 := cslotAddrA_inj hP hA hAd e + rw [hsd] at e2 + have e3 : tcbCnodeIndex 3 = tcbCnodeIndex 2 := congrArg Prod.snd e2 + exact absurd ((tcbIdx_eq (by simp [tcbSlotsA]) (by simp [tcbSlotsA])).1 e3) (by decide) + rw [ek _ (J.j5.2.1 _ (isSlot_fhA ((J.j.1.thT t).2 ht))) + (cnSlot_ne_fhA J.j.1.place hCs.1 hCs.2 ((J.j.1.thT t).2 ht)) ne] + exact J.fhOK t ht + have bfS := bfSpec_insertA J.bfSpec w + exact ⟨s2, σ2, hs2, hm, hbk, scF, J.of_capDerivBfA (hsub := J.j5.2.1) (hnul := J.j5.2.2) (fhOK' := hfh) (hvt := hvt) + (symL := symL s2 hs2) w0 F HS W hms hoff hfal hcdtS horig origLive nullOrig hsc (sysl s2 hs2) jx sinv + (J.ep.bufGrowA hl.2.1 hag) (J.bufNewPlaceA hl) (J.ipcPlace.growA (J.bufThA hl)) + (fun b hb => by + rcases BufW_insertA.1 hb with ⟨k, hk, rfl⟩ | hb + · exact hvr k hk + · exact J.bufVs b hb) bfS bufCover bufNative unmPt bufOff j5' (j7 s2 σ2 hs2 hm)⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3CapInsertTA.lean b/proofs/Fl4ProofsA64/P3/Stage3CapInsertTA.lean new file mode 100644 index 000000000..812e3b242 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3CapInsertTA.lean @@ -0,0 +1,515 @@ +/- +P3 (aarch64, A64-P3 Lift 3b labels): **the shared `cdt::insert` record step for a TCB destination slot** (parts). + +`Stage3ArmCnCopyA.capInsertGenA` finishes `JStage3A` across an insert into a CNode slot. A TCB label inserts into a TCB +slot (the IPC buffer slot 2, the fault handler slot 3, the timeout handler slot 4), where the buffer, fault-handler +and idle-slot relations differ per slot. So this module proves the common part and stops before `of_capDerivA`: + +* **`CapInsPartsA`**: the slot-write facts `of_capDerivA` takes (writes, caps from, spec slot frame, CDT / original-flag + liveness, the null-original and no-idle-cap facts, `JAtZAX`, `SInvXA`, J5), the other slots kept and the new cap. +* **`JStage3A.capInsertTcbSlotA`**: the whole `JStage3A` for slot `i ∉ {1, 2}` of a TCB (the CSpace root, the fault + handler, the timeout handler), whose buffer and idle-slot relations are kept; for the fault-handler slot 3 the new cap + is `Null` or an endpoint cap (`hfh3`, `of_capDerivFhA`). +* **`JStage3A.capInsertPartsTA`**: the parts across `cdt::insert nc ss sd` against `cap_insert`, `sd` any typed slot of + the heap (`AbsSlotA`) whose spec object exists (`hkd`), `ss` a CNode slot; the derivation facts as `capInsertGenA`. + +* **`nullOrEp_validFhA`**: a decode-site valid fault handler is `NullOrEp`. + +No named premise (those of `capInsertGenA`). New module. +-/ +import Fl4ProofsA64.P3.Stage3ArmCnCopyA +import Fl4ProofsA64.P3.CnSlotSbA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + + +/-- **What a `cap_insert` leaves, for `of_capDerivA`** (module docstring). -/ +structure CapInsPartsA {S : Type} (st : machine.Store S) [L : LawfulStore st] {A : Type} (AR : A → HwRootA → Prop) + (Z' : XSetsA) (H : Heap) (OS : Set U64) (s s2 : S) (a : A) (σ σ2 : Fl4SpecA64.State Unit) + (m : Fl4SpecA64.McState) (n : Nat) (src dest : U64) (nc : capability.Cap) (anc : Fl4SpecA64.Cap) : Prop where + w : WritesIn st s s2 (W7.CapW H.tcbs {b | InOS OS b} ∅) + F : CapsFromA st OS s s2 + HS : SpecSlotA σ σ2 + W : WfKeepA σ σ2 + hms : σ2.machineState = σ.machineState + hcdt : ∀ x y, σ2.cdt x = some y → ∀ sl, sl = x ∨ sl = y → + σ.kheap sl.1 ≠ none ∨ ∃ x0 y0, σ.cdt x0 = some y0 ∧ (sl = x0 ∨ sl = y0) + horig : ∀ x, σ2.isOriginalCap x = true → σ.kheap x.1 ≠ none ∨ σ.isOriginalCap x = true + origLive : ∀ sl, σ2.isOriginalCap sl = true → AbsSlotA Z'.th sl + nullOrig : ∀ sl, specCapAtA σ2 sl = some .nullCap → σ2.isOriginalCap sl = false + hsc : ∀ sl a, specCapAtA σ2 sl = some a → (∃ sl0, specCapAtA σ sl0 = some a) ∨ NoIdleCapV m n a + jx : JAtZAX (L := L) AR Z' s2 a σ2 m + sinv : SInvXA σ2 m n + j5 : J5A st Z' H OS s2 σ2 m + hvt : IdleSlotNewA σ σ2 m n + ek : ∀ y ∈ OS, y ≠ src → y ≠ dest → capA st s2 y = capA st s y + ed : capA st s2 dest = nc + hfull : FullA (capA st s2 src) (capA st s src) + +/-- **A decode-site valid fault handler is `Null` or an endpoint of the heap** (the `NullOrEp` the fault-handler slot +needs), from the cap typing `CapTyA` of its source slot. -/ +theorem nullOrEp_validFhA {S : Type} {st : machine.Store S} [L : LawfulStore st] {H : Heap} {OS : Set U64} {s : S} + {src : U64} (hT : CapTyA st H OS s) (hsrc : src ∈ OS) + (hv : faults.valid_fault_handler (capA st s src) = ok true) : NullOrEp H (capA st s src) := by + have k := hT src hsrc + generalize capA st s src = c at hv k ⊢ + cases c with + | Null => exact Or.inl rfl + | Endpoint p b x y z w => exact Or.inr ⟨p, b, x, y, z, w, rfl, k.1 _ _ _ _ _ _ rfl⟩ + | _ => exact absurd (okinj hv) Bool.false_ne_true + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +set_option maxHeartbeats 4000000 in +/-- **The parts of `JStage3A` across a `cap_insert` of `nc` from the CNode slot `ss` into any typed slot `sd`** (a CNode +slot or a TCB slot; `Stage3ArmCnCopyA.capInsertGenA` without the buffer / fault-handler / idle-slot relations): the +slot-write facts `of_capDerivA` takes, for the caller to finish with its own. -/ +theorem JStage3A.capInsertPartsTA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {ar : machine.Arch A} {nA : U64} {s : S} {a : A} + {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (ss sd : Fl4SpecA64.CslotPtr) {ps : U64} + (hCs : ss.1 = specPtr ps ∧ Z.th ps = some (.cnode ss.2.length)) + (hAd : AbsSlotA Z.th sd) (hkd : σ.kheap sd.1 ≠ none) (hsd1 : ∀ c < n, sd ≠ (m.idleThreadOf c, tcbCnodeIndex 1)) + (hsn : capA st s (cslotAddrA ss) ≠ .Null) (hdn : capA st s (cslotAddrA sd) = .Null) + (hdc : obj.sext48 (cslotAddrA sd) = ok (cslotAddrA sd)) + {nc : capability.Cap} {anc : Fl4SpecA64.Cap} (hwf : CapWfA nc) (hanc : CapEqA nc anc) + (hw : CapRt.EncWf nc) (hn : nc ≠ .Null) + (hD : Fl4ProofsA64.P3.DerivA nc (capA st s (cslotAddrA ss))) (hal : UA nc (capA st s (cslotAddrA ss))) + (hk : SameBadgeKindA nc (capA st s (cslotAddrA ss))) + (hut : isUtA (capA st s (cslotAddrA ss)) = true → ∀ y, ¬ Fl4Proofs.P3.Mdb.Desc Z.G.par y (cslotAddrA ss)) + (hdo : ¬ IrqNodeA (L.ksb s) (fl4Ptr sd.1) ∨ IrqCapA (absCapA nc)) + (hsci : NoIdleCapV m n anc) (hno : CapObjOkA Z nc) (hoffN : NodeOffA (L.ksb s) nc) + (hub : isUtA (capA st s (cslotAddrA ss)) = true → + insBA σ ss (absCapA nc) (absCapA (capA st s (cslotAddrA ss))) = true) + (hsib : insBA σ ss (absCapA nc) (absCapA (capA st s (cslotAddrA ss))) = false → + ∀ y, ¬ Fl4Proofs.P3.Mdb.Desc Z.G.par y (cslotAddrA ss)) + (hbeq : insBA σ ss (absCapA nc) (absCapA (capA st s (cslotAddrA ss))) = + isPFA (Fl4ProofsA64.P1.W7.R.ST.revA st s (cslotAddrA ss)) (capA st s (cslotAddrA ss)) nc + (revFA nc (capA st s (cslotAddrA ss)))) + (hJ5 : (∀ s', WritesIn st s s' (W7.CapW H.tcbs {b | InOS OS b} ∅) → + ∀ i : U64, i.val < 5376 → L.mem s' (irqStAddrA (L.ksb s) i) = L.mem s (irqStAddrA (L.ksb s) i)) → + ∀ σ' : Fl4SpecA64.State Unit, cdt.insert_with_cap st s nc (capA st s (cslotAddrA ss)) (cslotAddrA ss) + (cslotAddrA sd) ⦃ s' => + WritesIn st s s' (W7.CapW H.tcbs {b | InOS OS b} ∅) ∧ capA st s' (cslotAddrA sd) = nc ∧ + FullA (capA st s' (cslotAddrA ss)) (capA st s (cslotAddrA ss)) ∧ + (∀ y ∈ OS, y ≠ cslotAddrA ss → y ≠ cslotAddrA sd → capA st s' y = capA st s y) ∧ + J5A st { Z with G := ⟨insG (cslotAddrA ss) (cslotAddrA sd) Z.G.chains, insParA Z.G.par (cslotAddrA ss) + (cslotAddrA sd) (isPFA (Fl4ProofsA64.P1.W7.R.ST.revA st s (cslotAddrA ss)) (capA st s (cslotAddrA ss)) nc + (revFA nc (capA st s (cslotAddrA ss))))⟩ } H OS s' σ' m ⦄) + (symL : ∀ s2, cdt.insert st s nc (cslotAddrA ss) (cslotAddrA sd) = ok s2 → SymL st H Z.Cs s2) + (sysl : ∀ s2, cdt.insert st s nc (cslotAddrA ss) (cslotAddrA sd) = ok s2 → SysL st H Z.Cs OS ia ie s2) + (j7 : ∀ s2 σ2, cdt.insert st s nc (cslotAddrA ss) (cslotAddrA sd) = ok s2 → + ((), σ2) ∈ (Fl4SpecA64.capInsert (Z := Unit) anc ss sd σ).1 → + J7A st (J7R2A st ar nA) { Z with G := (insGA Z.G (cslotAddrA ss) (cslotAddrA sd) + (insBA σ ss (absCapA nc) (absCapA (capA st s (cslotAddrA ss))))) } H OS s2 σ2 m) : + ∃ s2 σ2, cdt.insert st s nc (cslotAddrA ss) (cslotAddrA sd) = ok s2 ∧ + ((), σ2) ∈ (Fl4SpecA64.capInsert (Z := Unit) anc ss sd σ).1 ∧ Sched.BodyKeepA σ σ2 ∧ + (∀ p y k, σ.kheap p = some (.schedContext y k) → σ2.kheap p = some (.schedContext y k)) ∧ + CapInsPartsA st AR { Z with G := (insGA Z.G (cslotAddrA ss) (cslotAddrA sd) + (insBA σ ss (absCapA nc) (absCapA (capA st s (cslotAddrA ss))))) } H OS s s2 a σ σ2 m n + (cslotAddrA ss) (cslotAddrA sd) nc anc := by + have hAs : AbsSlotA Z.th ss := ⟨ps, hCs.1, Or.inl hCs.2⟩ + have J1 := J + have hO : OSOK H.tcbs H.omega H.frame OS := J1.sys.ent.os + have hP := J1.j.1.place + have hsS := hAs.isSlot hP + have hdS := hAd.isSlot hP + have xs : cslotAddrA ss ∈ OS := J1.j5.2.1 _ hsS + have xd : cslotAddrA sd ∈ OS := J1.j5.2.1 _ hdS + have cd0 : capA st s (cslotAddrA sd) = .Null := hdn + have E1 := J1.toJEntA + have hTr : TreeA st OS Z.G.chains Z.G.par s := + J1.j.1.cdt.tree.grow J1.j5.2.1 (fun x hx hn => J1.j5.2.2 x hx hn) + have hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null := J1.j5.2.2 + have hoff := footN_offA E1 + have hfal := FootNA.aligned J1.j.1 (bxi_nodes E1.sys.ent.bxi) E1.scAl E1.ap.vcAl + have hokAnc : CapOkA σ.interruptIrqNode sd anc := by + have h0 := capOk_insA E1.j.1.irq hdo hoffN + cases nc with + | PageTable p mp asid va => + cases mp + · rw [← hanc]; exact h0 + · obtain ⟨_, _, rfl, _, _⟩ := CapEqA.pt_mapped_iff.mp hanc + refine ⟨fun irq hirq' hsl => ?_, fun ⟨_, _, _, _, e⟩ => by cases e⟩ + have hi := h0.1 irq hirq' hsl + simp [IrqCapA, absCapA] at hi + | _ => rw [← hanc]; exact h0 + have hIns : cdt.insert st s nc (cslotAddrA ss) (cslotAddrA sd) = + cdt.insert_with_cap st s nc (capA st s (cslotAddrA ss)) (cslotAddrA ss) (cslotAddrA sd) := by + unfold cdt.insert; rw [capA_eq, bind_ok] + have hst0 : ∀ s', WritesIn st s s' (W7.CapW H.tcbs {b | InOS OS b} ∅) → + ∀ i : U64, i.val < 5376 → L.mem s' (irqStAddrA (L.ksb s) i) = L.mem s (irqStAddrA (L.ksb s) i) := + fun s' w i hi => w.mem_eq (hfal _ (.irq i hi)) (hoff _ (.irq i hi)) + have mJ := hJ5 hst0 σ + rw [← hIns] at mJ + have mv := WP.spec_and (cap_insert_refinesA s σ Z.G J1.j.1.st J1.j.1.cdt hP hO hTr J1.j5.2.1 hnul hAs hAd + hsn cd0 hdc hwf hanc hw hn hal hk hut hub hsib) mJ + obtain ⟨s2, hs2, ⟨σ2, hm, hst2, hcdt2, -, HS, hcs2⟩, w, ed, hfull, ek, -⟩ := spec_existsA mv + have j5' : J5A st { Z with G := (insGA Z.G (cslotAddrA ss) (cslotAddrA sd) + (insBA σ ss (absCapA nc) (absCapA (capA st s (cslotAddrA ss))))) } H OS s2 σ2 m := by + have mJ2 := hJ5 hst0 σ2 + rw [← hIns] at mJ2 + obtain ⟨s2', hs2', -, -, -, -, J5'⟩ := spec_existsA mJ2 + rw [hs2] at hs2'; have e := okinj hs2'; subst e + rw [hbeq]; exact J5' + have jx : JAtZAX (L := L) AR { Z with G := (insGA Z.G (cslotAddrA ss) (cslotAddrA sd) + (insBA σ ss (absCapA nc) (absCapA (capA st s (cslotAddrA ss))))) } s2 a σ2 m := by + refine J1.j.of_slotWrites w (fun b hb' hb => hoff b hb hb') hfal _ HS hst2 hcdt2 (fun y hy => ?_) + rcases hcs2 y hy with e | e | ⟨p, b, i, d, e⟩ <;> rw [e] + · exact J1.j.capObj hy + · exact hno + · exact ⟨fun _ h => (by cases h), fun _ _ _ _ _ h => (by cases h), fun _ _ _ h => (by cases h), + fun _ _ _ h => (by cases h), trivial⟩ + have F : CapsFromA st OS s s2 := + CapsFromA.of_insA xs xd hD cd0 ed hfull (fun p b i d h => hTr.ut.fib _ xs p b i d h) ek + -- the spec side + obtain ⟨asc, hgs, -⟩ := getCap_eqA J1.j.1.st hAs + obtain ⟨dc, hgd0, -⟩ := getCap_eqA J1.j.1.st hAd + have hgd : ∀ y ∈ (getCap (Z := Unit) sd σ).1, y.2 = σ := fun y hy => by + rw [hgd0] at hy; obtain rfl : y = (dc, σ) := hy; rfl + obtain ⟨σa, σb, v, h1, h2, ecdt, eorig, ekh⟩ := capInsert_outA hgs hgd hm + obtain ⟨c2, o2, -⟩ := setCap_keepsA h2 + have A1 : σa.cdt = σ.cdt ∧ σa.isOriginalCap = σ.isOriginalCap ∧ WfKeepA σ σa ∧ + ∀ p o', σa.kheap p = some o' → ∃ o, σ.kheap p = some o ∧ + (∀ b c, Fl4SpecA64.capOf o' b = some c → ((p, b) = ss ∧ c = maxFreeIndexUpdate asc) ∨ + Fl4SpecA64.capOf o b = some c) ∧ + ∀ n, (∃ cs, o' = .cnode n cs ∧ wellFormedCnodeN n cs) ↔ (∃ cs, o = .cnode n cs ∧ wellFormedCnodeN n cs) := by + rcases h1 with rfl | h1 + · exact ⟨rfl, rfl, WfKeepA.of_kheap rfl, fun p o h => ⟨o, h, fun _ _ h => Or.inr h, fun _ => Iff.rfl⟩⟩ + · obtain ⟨c1, o1, -⟩ := setCap_keepsA h1 + exact ⟨c1, o1, setCap_wfA _ _ _ _ h1, setCap_objsA h1⟩ + have W : WfKeepA σ σ2 := (A1.2.2.1.trans (setCap_wfA _ _ _ _ h2)).trans (WfKeepA.of_kheap ekh) + have hms : σ2.machineState = σ.machineState := capInsert_msA _ _ _ _ _ hm + have ec : σ2.cdt = Function.update σ.cdt sd (if v then some ss else σ.cdt ss) := by + rw [ecdt, c2, A1.1] + have eo : σ2.isOriginalCap = Function.update σ.isOriginalCap sd + (decide (Fl4SpecA64.isCapRevocable anc asc)) := by + rw [eorig, o2, A1.2.1] + have kk : ∀ r : Fl4SpecA64.CslotPtr, (r = ss ∨ r = sd) → σ.kheap r.1 ≠ none := by + intro r hr + rcases hr with e | e <;> rw [e] + · obtain ⟨cs, hk1, -⟩ := J1.j.1.st.cnode ps _ hCs.2 + rw [hCs.1, hk1]; simp + · exact hkd + have hcdtS : ∀ x y, σ2.cdt x = some y → ∀ sl, sl = x ∨ sl = y → + σ.kheap sl.1 ≠ none ∨ ∃ x0 y0, σ.cdt x0 = some y0 ∧ (sl = x0 ∨ sl = y0) := by + intro p q h sl hsl + rw [ec] at h + by_cases ep : p = sd + · rw [ep, Function.update_self] at h + rcases hsl with e | e + · exact Or.inl (kk _ (Or.inr (e.trans ep))) + · split at h + · cases h; exact Or.inl (kk _ (Or.inl e)) + · exact Or.inr ⟨ss, q, h, Or.inr e⟩ + · rw [Function.update_of_ne ep] at h + exact Or.inr ⟨p, q, h, hsl⟩ + have horig : ∀ p, σ2.isOriginalCap p = true → σ.kheap p.1 ≠ none ∨ σ.isOriginalCap p = true := fun p h => by + by_cases ep : p = sd + · exact Or.inl (kk _ (Or.inr ep)) + · rw [eo, Function.update_of_ne ep] at h; exact Or.inr h + have origLive : ∀ sl, σ2.isOriginalCap sl = true → AbsSlotA Z.th sl := fun p h => by + by_cases ep : p = sd + · rw [ep]; exact hAd + · rw [eo, Function.update_of_ne ep] at h; exact J1.origLive p h + have capE : ∀ sl c, specCapAtA σ2 sl = some c → + specCapAtA σ sl = some c ∨ (sl = sd ∧ c = anc) ∨ (sl = ss ∧ c = maxFreeIndexUpdate asc) := by + refine specCapAtA_orA (Q := fun sl c => (sl = sd ∧ c = anc) ∨ (sl = ss ∧ c = maxFreeIndexUpdate asc)) HS + (fun p o h => ?_) + rw [ekh] at h + obtain ⟨ob, kb, cb, wb⟩ := setCap_objsA h2 p o h + obtain ⟨oa, ka, ca, wa'⟩ := A1.2.2.2 p ob kb + refine ⟨oa, ka, fun b c hb => ?_, fun n => (wb n).trans (wa' n)⟩ + rcases cb b c hb with h | h + · exact Or.inl (Or.inl h) + · rcases ca b c h with h | h + · exact Or.inl (Or.inr h) + · exact Or.inr h + have hsAsc : specCapAtA σ ss = some asc := by + obtain ⟨cs, hk1, hwf1, hr1⟩ := J1.j.1.st.cnode ps _ hCs.2 + obtain ⟨c0, hc0, -⟩ := hr1 ss.2 rfl + have hk2 : σ.kheap ss.1 = some (.cnode ss.2.length cs) := by rw [hCs.1]; exact hk1 + have hm0 : (c0, σ) ∈ (getCap (Z := Unit) ss σ).1 := getCap_cnode_memA hk2 hwf1 hc0 + rw [hgs] at hm0 + have e0 : c0 = asc := (Prod.mk.inj hm0).1 + unfold specCapAtA; simp only [hk2]; rw [if_pos hwf1, ← e0]; exact hc0 + have hsc : ∀ sl c, specCapAtA σ2 sl = some c → (∃ sl0, specCapAtA σ sl0 = some c) ∨ NoIdleCapV m n c := + fun sl c h => by + rcases capE sl c h with h | ⟨-, e⟩ | ⟨-, e⟩ + · exact Or.inl ⟨sl, h⟩ + · exact Or.inr (e ▸ hsci) + · rw [e] + cases asc + all_goals first + | exact Or.inl ⟨ss, hsAsc⟩ + | exact Or.inr ⟨fun _ _ h => (by cases h), fun _ h => (by cases h), fun _ _ _ h => (by cases h)⟩ + have nullOrig : ∀ sl, specCapAtA σ2 sl = some .nullCap → σ2.isOriginalCap sl = false := fun sl h => by + by_cases f1 : sl = sd + · exfalso + rw [f1] at h + exact specCap_nnA jx.1.st hAd + (fun h' => hn ((show capAtA (L.mem s2) (cslotAddrA sd) = nc from ed).symm.trans h')) h + by_cases f2 : sl = ss + · exfalso + rw [f2] at h + refine specCap_nnA jx.1.st hAs (fun h' => ?_) h + have h'' : capA st s2 (cslotAddrA ss) = .Null := h' + rcases hfull with e | ⟨p, b, i, d, i1, -, e, -⟩ + · exact hsn (e.symm.trans h'') + · rw [e] at h''; cases h'' + rw [eo, Function.update_of_ne f1] + rcases capE sl _ h with h0 | ⟨e, -⟩ | ⟨e, -⟩ + · exact J1.nullOrig sl h0 + · exact absurd e f1 + · exact absurd e f2 + have sinv : SInvXA σ2 m n := J1.sinv.capInsert hokAnc hm + refine ⟨s2, σ2, hs2, hm, Sched.BodyKeepA.ofSlot HS hms, + fun p y k h => HS.keep h (fun r cs e => by cases e) (fun x e => by cases e), ?_⟩ + have hvt : IdleSlotNewA σ σ2 m n := fun c hc a ha => by + rcases capE _ _ ha with h | ⟨e, -⟩ | ⟨e, -⟩ + · exact Or.inl h + · exact absurd e.symm (hsd1 c hc) + · exfalso + obtain ⟨x, hx, -⟩ := J1.sidle.f3 c hc + obtain ⟨cs, hk1, -⟩ := J1.j.1.st.cnode ps _ hCs.2 + have e1 : m.idleThreadOf c = ss.1 := congrArg Prod.fst e + rw [e1, hCs.1, hk1] at hx + cases hx + exact ⟨w, F, HS, W, hms, hcdtS, horig, origLive, nullOrig, hsc, jx, sinv, j5', hvt, ek, ed, hfull⟩ + +set_option maxHeartbeats 4000000 in +/-- **The parts across a `cap_insert` of a cap derived from the source's into any typed slot** (`capInsertA` with +`capInsertPartsTA`). -/ +theorem JStage3A.capInsertPartsA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {ar : machine.Arch A} {nA : U64} {s : S} {a : A} + {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (ss sd : Fl4SpecA64.CslotPtr) {ps : U64} + (hCs : ss.1 = specPtr ps ∧ Z.th ps = some (.cnode ss.2.length)) + (hAd : AbsSlotA Z.th sd) (hkd : σ.kheap sd.1 ≠ none) + (hsd1 : ∀ c < n, sd ≠ (m.idleThreadOf c, tcbCnodeIndex 1)) + (hsn : capA st s (cslotAddrA ss) ≠ .Null) (hdn : capA st s (cslotAddrA sd) = .Null) + (hdc : obj.sext48 (cslotAddrA sd) = ok (cslotAddrA sd)) + {nc : capability.Cap} {anc : Fl4SpecA64.Cap} (hwf : CapWfA nc) (hanc : CapEqA nc anc) + (hw : CapRt.EncWf nc) (hn : nc ≠ .Null) + (hD : Fl4ProofsA64.P3.DerivA nc (capA st s (cslotAddrA ss))) (hal : UA nc (capA st s (cslotAddrA ss))) + (hk : SameBadgeKindA nc (capA st s (cslotAddrA ss))) + (hsh : Fl4ProofsA64.P3.shapeA nc = Fl4ProofsA64.P3.shapeA (capA st s (cslotAddrA ss))) + (hsh1 : Fl4ProofsA64.P1.W7.R.ST.shapeA nc = Fl4ProofsA64.P1.W7.R.ST.shapeA (capA st s (cslotAddrA ss))) + (hbd1 : badgeA nc = badgeA (capA st s (cslotAddrA ss)) ∨ badgeA (capA st s (cslotAddrA ss)) = 0#u64) + (hbd : ∀ r b rs, (absCapA (capA st s (cslotAddrA ss)) = .endpointCap r b rs ∨ + absCapA (capA st s (cslotAddrA ss)) = .notificationCap r b rs) → b ≠ 0 → + Fl4SpecA64.capEpBadge (absCapA nc) = b) + (hirqB : ∀ i, capA st s (cslotAddrA ss) = .IrqHandler i → nc = .IrqHandler i) + (hirqN : ∀ i, nc = .IrqHandler i → capA st s (cslotAddrA ss) = .IrqHandler i ∨ + ∀ y ∈ Z.G.chains.flatten, capA st s y ≠ .IrqHandler i) + (hirqA : ∀ i, nc = .IrqHandler i → capA st s (cslotAddrA ss) = .IrqHandler i ∨ + (i.val < 5376 ∧ L.mem s (irqStAddrA (L.ksb s) i) ≠ 0#u64)) + (hz : ∀ p t k, capA st s (cslotAddrA ss) ≠ .Zombie p t k) + (hut : isUtA (capA st s (cslotAddrA ss)) = true → ∀ y, ¬ Fl4Proofs.P3.Mdb.Desc Z.G.par y (cslotAddrA ss)) + (hdo : ¬ IrqNodeA (L.ksb s) (fl4Ptr sd.1) ∨ IrqCapA (absCapA nc)) + (hcv : CapValidA Z.th (capA st s (cslotAddrA ss)) → CapValidA Z.th nc) (hsci : NoIdleCapV m n anc) + (symL : ∀ s2, cdt.insert st s nc (cslotAddrA ss) (cslotAddrA sd) = ok s2 → SymL st H Z.Cs s2) + (sysl : ∀ s2, cdt.insert st s nc (cslotAddrA ss) (cslotAddrA sd) = ok s2 → SysL st H Z.Cs OS ia ie s2) + (j7 : ∀ s2 σ2, cdt.insert st s nc (cslotAddrA ss) (cslotAddrA sd) = ok s2 → + ((), σ2) ∈ (Fl4SpecA64.capInsert (Z := Unit) anc ss sd σ).1 → + J7A st (J7R2A st ar nA) { Z with G := (insGA Z.G (cslotAddrA ss) (cslotAddrA sd) + (insBA σ ss (absCapA nc) (absCapA (capA st s (cslotAddrA ss))))) } H OS s2 σ2 m) : + ∃ s2 σ2, cdt.insert st s nc (cslotAddrA ss) (cslotAddrA sd) = ok s2 ∧ + ((), σ2) ∈ (Fl4SpecA64.capInsert (Z := Unit) anc ss sd σ).1 ∧ Sched.BodyKeepA σ σ2 ∧ + (∀ p y k, σ.kheap p = some (.schedContext y k) → σ2.kheap p = some (.schedContext y k)) ∧ + CapInsPartsA st AR { Z with G := (insGA Z.G (cslotAddrA ss) (cslotAddrA sd) + (insBA σ ss (absCapA nc) (absCapA (capA st s (cslotAddrA ss))))) } H OS s s2 a σ σ2 m n + (cslotAddrA ss) (cslotAddrA sd) nc anc := by + have hAs : AbsSlotA Z.th ss := ⟨ps, hCs.1, Or.inl hCs.2⟩ + have J1 := J + have hO : OSOK H.tcbs H.omega H.frame OS := J1.sys.ent.os + have hP := J1.j.1.place + have hsS := hAs.isSlot hP + have hdS := hAd.isSlot hP + have xs : cslotAddrA ss ∈ OS := J1.j5.2.1 _ hsS + have xd : cslotAddrA sd ∈ OS := J1.j5.2.1 _ hdS + have cd0 : capA st s (cslotAddrA sd) = .Null := hdn + have E1 := J1.toJEntA + have hTr : TreeA st OS Z.G.chains Z.G.par s := + J1.j.1.cdt.tree.grow J1.j5.2.1 (fun x hx hn => J1.j5.2.2 x hx hn) + have hoff := footN_offA E1 + have hfal := FootNA.aligned J1.j.1 (bxi_nodes E1.sys.ent.bxi) E1.scAl E1.ap.vcAl + obtain ⟨_, -, hcr⟩ := J1.j.1.st.getAbsA hAs + have hov : NoOvA (capA st s (cslotAddrA ss)) := noOvA_of_wf hcr.1 + have hsr := srA_shape hsh hov hcr.1 hsn hz + have hd := derivA_of _ _ hsr hbd + have hno : CapObjOkA Z nc := (J1.j.capObj hsS).of_shapeA hsh hcv + have hoffN : NodeOffA (L.ksb s) nc := (NodeOffA.of_ctOff (E1.sys.sysl.ct _ xs)).of_shapeA hsh + have hzb : isZbA (capA st s (cslotAddrA ss)) = false := by + generalize capA st s (cslotAddrA ss) = c at hz + cases c <;> first | rfl | exact absurd rfl (hz _ _ _) + have hss : sraA (capA st s (cslotAddrA ss)) nc = true := by + have h1 := J1.j5.1.1.self _ ((hTr.lk.members _).2 ⟨xs, hsn⟩) hzb + unfold srMA at h1 + rw [sraA_congr rfl hsh1]; exact h1 + have hbeq : insBA σ ss (absCapA nc) (absCapA (capA st s (cslotAddrA ss))) = + isPFA (Fl4ProofsA64.P1.W7.R.ST.revA st s (cslotAddrA ss)) (capA st s (cslotAddrA ss)) nc + (revFA nc (capA st s (cslotAddrA ss))) := by + unfold insBA + rw [J1.j.1.cdt.orig ss hAs] + exact sbp_eqA _ _ _ hsh hsr hss hk hwf + exact JStage3A.capInsertPartsTA (J := J) ss sd hCs hAd hkd hsd1 hsn hdn hdc hwf hanc hw hn hD hal hk hut hdo hsci hno hoffN + (hub_ofA E1 hAs xs hd) (hsib_ofA J1.j.1.cdt hAs hd) hbeq + (fun hst σ' => J5A.insGenA (σ' := σ') (m := m) (m' := m) hO J1.j5 hTr xs xd hsS hdS hsn cd0 hdc hw hsh1 hbd1 hirqB + hirqN hirqA hut hzb hst) symL sysl j7 + + +set_option maxHeartbeats 4000000 in +/-- **`JStage3A` across a `cap_insert` of a derived cap into slot `i ∉ {1, 2}` of a TCB** (the CSpace root, the fault +handler, the timeout handler): the buffer and idle-slot relations are kept and the fault-handler slots stay `Null` or +endpoint (`hfh3`), so `of_capDerivFhA` finishes (module docstring). -/ +theorem JStage3A.capInsertTcbSlotA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {ar : machine.Arch A} {nA : U64} {s : S} {a : A} + {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (ss sd : Fl4SpecA64.CslotPtr) {ps : U64} + (hCs : ss.1 = specPtr ps ∧ Z.th ps = some (.cnode ss.2.length)) + {t0 : U64} (ht0 : Z.th t0 = some .tcb) {i : Nat} (hi : i < tcbSlotsA) (hi1 : i ≠ 1) (hi2 : i ≠ 2) + (hsd : sd = (specPtr t0, tcbCnodeIndex i)) + (hsn : capA st s (cslotAddrA ss) ≠ .Null) (hdn : capA st s (cslotAddrA sd) = .Null) + (hdc : obj.sext48 (cslotAddrA sd) = ok (cslotAddrA sd)) + {nc : capability.Cap} {anc : Fl4SpecA64.Cap} (hfh3 : i = 3 → NullOrEp H nc) (hwf : CapWfA nc) (hanc : CapEqA nc anc) + (hw : CapRt.EncWf nc) (hn : nc ≠ .Null) + (hD : Fl4ProofsA64.P3.DerivA nc (capA st s (cslotAddrA ss))) (hal : UA nc (capA st s (cslotAddrA ss))) + (hk : SameBadgeKindA nc (capA st s (cslotAddrA ss))) + (hsh : Fl4ProofsA64.P3.shapeA nc = Fl4ProofsA64.P3.shapeA (capA st s (cslotAddrA ss))) + (hsh1 : Fl4ProofsA64.P1.W7.R.ST.shapeA nc = Fl4ProofsA64.P1.W7.R.ST.shapeA (capA st s (cslotAddrA ss))) + (hbd1 : badgeA nc = badgeA (capA st s (cslotAddrA ss)) ∨ badgeA (capA st s (cslotAddrA ss)) = 0#u64) + (hbd : ∀ r b rs, (absCapA (capA st s (cslotAddrA ss)) = .endpointCap r b rs ∨ + absCapA (capA st s (cslotAddrA ss)) = .notificationCap r b rs) → b ≠ 0 → + Fl4SpecA64.capEpBadge (absCapA nc) = b) + (hirqB : ∀ i, capA st s (cslotAddrA ss) = .IrqHandler i → nc = .IrqHandler i) + (hirqN : ∀ i, nc = .IrqHandler i → capA st s (cslotAddrA ss) = .IrqHandler i ∨ + ∀ y ∈ Z.G.chains.flatten, capA st s y ≠ .IrqHandler i) + (hirqA : ∀ i, nc = .IrqHandler i → capA st s (cslotAddrA ss) = .IrqHandler i ∨ + (i.val < 5376 ∧ L.mem s (irqStAddrA (L.ksb s) i) ≠ 0#u64)) + (hz : ∀ p t k, capA st s (cslotAddrA ss) ≠ .Zombie p t k) + (hut : isUtA (capA st s (cslotAddrA ss)) = true → ∀ y, ¬ Fl4Proofs.P3.Mdb.Desc Z.G.par y (cslotAddrA ss)) + (hdo : ¬ IrqNodeA (L.ksb s) (fl4Ptr sd.1) ∨ IrqCapA (absCapA nc)) + (hcv : CapValidA Z.th (capA st s (cslotAddrA ss)) → CapValidA Z.th nc) (hsci : NoIdleCapV m n anc) + (symL : ∀ s2, cdt.insert st s nc (cslotAddrA ss) (cslotAddrA sd) = ok s2 → SymL st H Z.Cs s2) + (sysl : ∀ s2, cdt.insert st s nc (cslotAddrA ss) (cslotAddrA sd) = ok s2 → SysL st H Z.Cs OS ia ie s2) + (j7 : ∀ s2 σ2, cdt.insert st s nc (cslotAddrA ss) (cslotAddrA sd) = ok s2 → + ((), σ2) ∈ (Fl4SpecA64.capInsert (Z := Unit) anc ss sd σ).1 → + J7A st (J7R2A st ar nA) { Z with G := (insGA Z.G (cslotAddrA ss) (cslotAddrA sd) + (insBA σ ss (absCapA nc) (absCapA (capA st s (cslotAddrA ss))))) } H OS s2 σ2 m) : + ∃ s2 σ2, cdt.insert st s nc (cslotAddrA ss) (cslotAddrA sd) = ok s2 ∧ + ((), σ2) ∈ (Fl4SpecA64.capInsert (Z := Unit) anc ss sd σ).1 ∧ Sched.BodyKeepA σ σ2 ∧ + (∀ p y k, σ.kheap p = some (.schedContext y k) → σ2.kheap p = some (.schedContext y k)) ∧ + JStage3A st AR { Z with G := (insGA Z.G (cslotAddrA ss) (cslotAddrA sd) + (insBA σ ss (absCapA nc) (absCapA (capA st s (cslotAddrA ss))))) } + H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s2 a σ2 m := by + have hAd : AbsSlotA Z.th sd := by subst hsd; exact ⟨t0, rfl, Or.inr ⟨ht0, i, hi, rfl⟩⟩ + have hkd : σ.kheap sd.1 ≠ none := by + obtain ⟨x', hk', -⟩ := J.j.1.st.tcb t0 ht0 + rw [hsd]; simp [hk'] + have hsd1 : ∀ c < n, sd ≠ (m.idleThreadOf c, tcbCnodeIndex 1) := fun c hc e => + hi1 ((tcbIdx_eq (by unfold tcbSlotsA at hi; omega) (by omega)).1 (by rw [hsd] at e; exact congrArg Prod.snd e)) + obtain ⟨s2, σ2, hs2, hm, hbk, scF, P⟩ := JStage3A.capInsertPartsA (J := J) ss sd hCs hAd hkd hsd1 hsn hdn hdc hwf hanc + hw hn hD hal hk hsh hsh1 hbd1 hbd hirqB hirqN hirqA hz hut hdo hcv hsci symL sysl j7 + obtain ⟨w, F, HS, W, hms, hcdtS, horig, origLive, nullOrig, hsc, jx, sinv, j5', hvt, ek, ed, hfull⟩ := P + have hO : OSOK H.tcbs H.omega H.frame OS := J.sys.ent.os + have hP := J.j.1.place + have E1 := J.toJEntA + have hoff := footN_offA E1 + have hfal := FootNA.aligned J.j.1 (bxi_nodes E1.sys.ent.bxi) E1.scAl E1.ap.vcAl + -- the destination slot is no TCB's slot 2 or 3 + have hneT : ∀ u, Z.th u = some .tcb → ∀ j : Nat, j < tcbSlotsA → j ≠ i → + cslotAddrA (specPtr u, tcbCnodeIndex j) ≠ cslotAddrA sd := fun u hu j hj hji e => by + have hA : AbsSlotA Z.th (specPtr u, tcbCnodeIndex j) := ⟨u, rfl, Or.inr ⟨hu, j, hj, rfl⟩⟩ + have e2 := cslotAddrA_inj hP hA hAd e + rw [hsd] at e2 + have e3 : tcbCnodeIndex j = tcbCnodeIndex i := congrArg Prod.snd e2 + exact hji ((tcbIdx_eq (by unfold tcbSlotsA at hj; omega) (by unfold tcbSlotsA at hi; omega)).1 e3) + have eaB : ∀ t, cslotAddrA (specPtr t, tcbCnodeIndex 2) = sbWordA t := fun t => by + unfold cslotAddrA + rw [ofBl_tcbIdx 2 (by omega)] + exact Endp.bufSlot_eqA t + have eaF : ∀ t, cslotAddrA (specPtr t, tcbCnodeIndex 3) = slotIdx t layout.tcb.SLOT_FAULT_HANDLER := fun t => by + unfold cslotAddrA + rw [ofBl_tcbIdx 3 (by omega)] + exact (slotIdx_fhA t).symm + -- the buffer slots, the tables + have wA : WritesIn st s s2 (fun b => Aligned b ∧ W7.CapW H.tcbs {b | InOS OS b} ∅ b) := writesIn_alAG w + have hB : ∀ b, (Aligned b ∧ W7.CapW H.tcbs {b | InOS OS b} ∅ b) → W7.CapW H.tcbs H.omega H.frame b := + fun b hb => os_capw hO hb.1 hb.2 + obtain ⟨-, -, -, -, -, fld, -, -⟩ := W7.blk_of_capw (BXI.blk J.sys.ent.bxi) wA hB + have sbS : ∀ t ∈ Z.T, sbWordA t ∈ OS := fun t ht => + J.j5.2.1 _ ⟨t, Or.inr ⟨(J.j.1.thT t).2 ht, 2, by simp [tcbSlotsA], (bufSlot_eqA t).symm⟩⟩ + have sbK : ∀ t ∈ Z.T, capA st s2 (sbWordA t) = capA st s (sbWordA t) := fun t ht => + ek _ (sbS t ht) (cnSlot_ne_sbA hP hCs.1 hCs.2 ((J.j.1.thT t).2 ht)) + (by rw [← eaB t]; exact hneT t ((J.j.1.thT t).2 ht) 2 (by unfold tcbSlotsA; omega) (fun e => hi2 e.symm)) + have ib : ∀ t ∈ Z.T, L.mem s2 (ibWordA t) = L.mem s (ibWordA t) := + fun t ht => fld t (by rw [J.tcbs]; exact ht) 3512#u64 (by simp) (by simp) (by simp) + have bufCover : BufCover st Z.T Bf s2 σ2 := BufCover.mapA J.bufCover J.bufNative J.j.1.st jx.1.st + (fun t ht => (J.j.1.thT t).2 ht) HS ib (fun t ht => by rw [sbK t ht]; exact .refl _) + have bufNative : BufNativeA Z.T (L.mem s2) := BufNativeA.mapA J.bufNative (fun t ht q sz' rr dv m' e' a' v' h => by + rw [sbK t ht] at h + have hb := J.bufNative t ht + rw [bufSlot_eqA, capAtA_eq (st := st), h] at hb + exact hb) + have vk : ∀ Pt Ub : Set U64, VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt → + VsKeep st (L.ksb s) Pt s s2 := fun Pt Ub vr => + VsKeep.of_w vr.al w (fun b hb => J.vsr_offCapWA vr hb) + have unmPt : UnmPtSpecA st { Z with G := (insGA Z.G (cslotAddrA ss) (cslotAddrA sd) + (insBA σ ss (absCapA nc) (absCapA (capA st s (cslotAddrA ss))))) } m OS s2 σ2 := by + obtain ⟨Pt, Ub, vr, -, -, -, -, -, -, link, -, -⟩ := J.j7 + exact UnmPtSpecA.derivA F HS w.1 w.2.1 (vk Pt Ub vr) link + (fun p h => ⟨fun i hi hf => h.mem i hi (FootA.monoA + (Z := { Z with G := (insGA Z.G (cslotAddrA ss) (cslotAddrA sd) + (insBA σ ss (absCapA nc) (absCapA (capA st s (cslotAddrA ss))))), S1 := ∅ }) rfl rfl + (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) + (fun _ h => h) (FootA.of_ksA w.1.symm w.2.1.symm hf)), h.obj⟩) J.unmPt + have bufOff : BufOffA st { Z with G := (insGA Z.G (cslotAddrA ss) (cslotAddrA sd) + (insBA σ ss (absCapA nc) (absCapA (capA st s (cslotAddrA ss))))) } OS s2 Bf := + ⟨J.bufOff.os, fun b hb => by rw [w.1]; exact J.bufOff.vs b hb, + fun b hb q hq => J.bufOff.page b hb q (SchedPageA.derivBackA F rfl rfl rfl rfl rfl hq), J.bufOff.vcw, + fun bp hbp => (J.bufOff.pin bp hbp).keepC rfl rfl fun t ht _ => sbK t ht⟩ + have hfh : ∀ t ∈ Z.T, NullOrEp H (capA st s2 (slotIdx t layout.tcb.SLOT_FAULT_HANDLER)) := fun t ht => by + by_cases h3 : i = 3 ∧ t = t0 + · have e : slotIdx t layout.tcb.SLOT_FAULT_HANDLER = cslotAddrA sd := by + rw [← eaF t, hsd, h3.1, h3.2] + rw [e, ed] + exact hfh3 h3.1 + · have ne : slotIdx t layout.tcb.SLOT_FAULT_HANDLER ≠ cslotAddrA sd := fun e => by + rw [← eaF t] at e + have hA : AbsSlotA Z.th (specPtr t, tcbCnodeIndex 3) := + ⟨t, rfl, Or.inr ⟨(J.j.1.thT t).2 ht, 3, by unfold tcbSlotsA; omega, rfl⟩⟩ + have e2 := cslotAddrA_inj hP hA hAd e + rw [hsd] at e2 + have e1 : specPtr t = specPtr t0 := congrArg Prod.fst e2 + have e3 : tcbCnodeIndex 3 = tcbCnodeIndex i := congrArg Prod.snd e2 + have : (3 : Nat) = i := + (tcbIdx_eq (by omega) (by unfold tcbSlotsA at hi; omega)).1 e3 + exact h3 ⟨this.symm, specPtr_inj e1⟩ + rw [ek _ (J.j5.2.1 _ (isSlot_fhA ((J.j.1.thT t).2 ht))) + (cnSlot_ne_fhA J.j.1.place hCs.1 hCs.2 ((J.j.1.thT t).2 ht)) ne] + exact J.fhOK t ht + exact ⟨s2, σ2, hs2, hm, hbk, scF, J.of_capDerivFhA (hsub := J.j5.2.1) (hnul := J.j5.2.2) (fhOK' := hfh) (hvt := hvt) + (symL := symL s2 hs2) w F HS W hms hoff hfal hcdtS horig origLive nullOrig hsc (sysl s2 hs2) jx sinv bufCover + bufNative unmPt bufOff j5' (j7 s2 σ2 hs2 hm)⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3CapSlotA.lean b/proofs/Fl4ProofsA64/P3/Stage3CapSlotA.lean new file mode 100644 index 000000000..5ba429f44 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3CapSlotA.lean @@ -0,0 +1,513 @@ +/- +P3 (AArch64, merge line, D8 step 5 `of_capFrame`, part (i)): **`JStage3A` across a write of slot words**. + +`JStage3A.of_slotWA` is the delete-hooks line's `JStage3TA.of_slotW` (`P3/DelSlotWA`, branch `par/a64-delhooks`) +re-targeted at the unified record `JStage3A` at `J7A st Rj`. Generated by +`handoff/tools/capslot/gen_capslota.py`; the proof body is theirs verbatim. The write is any +`W7.CapW H.tcbs {InOS OS} ∅` write with a slot-only spec step (`SpecSlotA`); the record fields that read caps are +hypotheses (`P3/Stage3MapAlikeA` proves them for in-place map / unmap / assign writes). `sysl` is A64 P1's `SysL` at +the new state. The helper lemmas are `private` (names as in `P3/DelSlotWA`, suffix `G`). +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3SchedWrNA +import Fl4ProofsA64.P3.Stage3VsWriteA +import Fl4ProofsA64.P3.IpcXferOrthA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +theorem wa_val_tcbAG {H : Heap} (hH : HeapOK H) {t K : U64} (ht : H t = some .tcb) (hK : K.val < 4096) : + (wa t K).val = t.val + K.val := by + have hf := hH.fits t _ ht + simp only [HKind.bits] at hf + simp [core.num.U64.wrapping_add_val_eq, U64.size_def, U64.numBits]; omega + +/-- **An aligned slot word lies in a heap TCB below offset 224 or in an `other` heap object.** -/ +theorem capw_inObjAG {H : Heap} {OS : Set U64} (hH : HeapOK H) (hO : OSOK H.tcbs H.omega H.frame OS) {b : U64} + (hb : Aligned b) (h : W7.CapW H.tcbs {b | InOS OS b} ∅ b) : + ∃ p k, H p = some k ∧ InObj p k b ∧ ((k = .tcb ∧ b.val < p.val + 224) ∨ ∃ z, k = .other z) := by + rcases os_capw hO hb h with ⟨t, ht, K, hK, e⟩ | ⟨⟨-, p, k, hp, hk, hin⟩, hF⟩ + · have ht' : H t = some .tcb := ht + have hv := wa_val_tcbAG hH ht' (K := K) (by omega) + subst e + exact ⟨t, .tcb, ht', ⟨by omega, by simp only [HKind.bits]; omega⟩, Or.inl ⟨rfl, by omega⟩⟩ + · refine ⟨p, k, hp, hin, Or.inr ?_⟩ + rcases hk with rfl | ⟨z, rfl⟩ | ⟨z, rfl⟩ | ⟨rfl, hw⟩ + · exact absurd ⟨hb, p, .reply, hp, Or.inl rfl, hin⟩ hF + · exact absurd ⟨hb, p, .sc z, hp, Or.inr (Or.inl ⟨z, rfl⟩), hin⟩ hF + · exact ⟨z, rfl⟩ + · exact absurd ⟨hb, p, .ntfn, hp, Or.inr (Or.inr ⟨rfl, hw⟩), hin⟩ hF + +/-- **A slot word is apart from every heap object but a TCB (below 224) or an `other` object.** -/ +theorem capw_apartAG {H : Heap} {OS : Set U64} (hH : HeapOK H) (hO : OSOK H.tcbs H.omega H.frame OS) {b : U64} + (hb : Aligned b) (h : W7.CapW H.tcbs {b | InOS OS b} ∅ b) {q : U64} {k' : HKind} (hq : H q = some k') + (hin : InObj q k' b) (hk : (k' ≠ .tcb ∧ ∀ z, k' ≠ .other z) ∨ (k' = .tcb ∧ q.val + 224 ≤ b.val)) : False := by + obtain ⟨p, k, hp, hinp, hkp⟩ := capw_inObjAG hH hO hb h + by_cases e : p = q + · subst e + rw [hp] at hq + cases hq + rcases hkp with ⟨rfl, h2⟩ | ⟨z, rfl⟩ + · rcases hk with ⟨h1, -⟩ | ⟨-, h3⟩ + · exact h1 rfl + · omega + · rcases hk with ⟨-, h1⟩ | ⟨h1, -⟩ + · exact h1 z rfl + · cases h1 + · have := hH.disj p k q k' hp hq e + unfold InObj at hinp hin + omega + +/-- The TCB fields from offset 224 are off the slot words. -/ +theorem capw_tcbHiAG {H : Heap} {OS : Set U64} (hH : HeapOK H) (hO : OSOK H.tcbs H.omega H.frame OS) {b : U64} + (hb : Aligned b) (h : W7.CapW H.tcbs {b | InOS OS b} ∅ b) {t : U64} (ht : H t = some .tcb) + (h1 : t.val + 224 ≤ b.val) (h2 : b.val < t.val + 4096) : False := + capw_apartAG hH hO hb h ht ⟨by omega, by simp only [HKind.bits]; omega⟩ (Or.inr ⟨rfl, h1⟩) + +/-- A word of a heap object of kind SC / endpoint / notification / reply is off the slot words. -/ +theorem capw_objOffAG {H : Heap} {OS : Set U64} (hH : HeapOK H) (hO : OSOK H.tcbs H.omega H.frame OS) {b : U64} + (hb : Aligned b) (h : W7.CapW H.tcbs {b | InOS OS b} ∅ b) {q : U64} {k' : HKind} (hq : H q = some k') + (hin : InObj q k' b) (hk : k' = .ep ∨ k' = .ntfn ∨ k' = .reply ∨ ∃ z, k' = .sc z) : False := by + refine capw_apartAG hH hO hb h hq hin (Or.inl ⟨?_, fun z e => ?_⟩) + · rintro rfl; rcases hk with h | h | h | ⟨z, h⟩ <;> cases h + · subst e; rcases hk with h | h | h | ⟨z', h⟩ <;> cases h + +/-- An aligned write of slot words, as a write set of aligned slot words (for P1's `W7.blk_of_capw`). -/ +theorem writesIn_alAG {s s' : S} {P : U64 → Prop} (w : WritesIn st s s' P) : + WritesIn st s s' (fun b => Aligned b ∧ P b) := + ⟨w.1, w.2.1, fun b hb hn => w.2.2 b hb fun h => hn ⟨hb, h⟩⟩ + +/-- **A VSpace word of the reachable set is no slot word** (`VrOK`: placed in an `other` object, off the `OS` +slots). -/ +theorem JStage3A.vsr_offCapWA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J5 J7 : JSlotA S} {s : S} {a : A} + {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) {Pt Ub : Set U64} + (vr : VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt) {b : U64} (h : VsR (L.ksb s) Pt b) : + ¬ W7.CapW H.tcbs {b | InOS OS b} ∅ b := by + rintro (⟨t, ht, K, hK, e⟩ | ⟨hb, -⟩) + · obtain ⟨Q, R', q⟩ := J.sys.ent.bxi.1 + have hΩ := vr.om b h + have := (q.omega b hΩ).2.1 t ht + have hv := wa_val_tcbAG J.sys.ent.heap (show H t = some .tcb from ht) (K := K) (by omega) + rw [e, hv] at this + omega + · exact vr.os b h hb + +/-! ## `binj` for buffers named by their spec pointers -/ + +/-- The spec name of a tracked buffer's word. -/ +theorem bufAddr_specPtrWrapA (w k : U64) (hk : k.val < 128) : bufAddr (specPtr w) k.val = specPtr (bufW w k) := by + have e : (core.num.U64.wrapping_mul k 8#u64).bv = BitVec.ofNat 64 (k.val * Fl4SpecA64.wordSize) := by + apply BitVec.eq_of_toNat_eq + change (core.num.U64.wrapping_mul k 8#u64).val = _ + rw [show Fl4SpecA64.wordSize = 8 from rfl, BitVec.toNat_ofNat] + simp [core.num.U64.wrapping_mul_val_eq, U64.size_def, U64.numBits] + unfold bufW bufAddr + rw [specPtr_wa, e] + +/-- **`EPlaced.binj` from `bfSpec`** (old × new pairs alike): both sides differ by the constant pointer shift. -/ +theorem binj_of_bfSpecA {Bf : Set (U64 × Fl4SpecA64.Word)} (h : ∀ bp ∈ Bf, bp.2 = specPtr bp.1) : + ∀ bp ∈ Bf, ∀ bq ∈ Bf, ∀ k k' : U64, k.val < 128 → k'.val < 128 → + (bufW bp.1 k = bufW bq.1 k' ↔ bufAddr bp.2 k.val = bufAddr bq.2 k'.val) := by + intro bp hbp bq hbq k k' hk hk' + rw [h bp hbp, h bq hbq, bufAddr_specPtrWrapA _ _ hk, bufAddr_specPtrWrapA _ _ hk'] + exact ⟨fun e => by rw [e], specPtr_inj⟩ + +/-- `bfSpec` for the grown set. -/ +theorem bfSpec_insertA {Bf : Set (U64 × Fl4SpecA64.Word)} (h : ∀ bp ∈ Bf, bp.2 = specPtr bp.1) (w : U64) : + ∀ bp ∈ insert (w, specPtr w) Bf, bp.2 = specPtr bp.1 := by + intro bp hbp + rcases hbp with rfl | hbp + · rfl + · exact h bp hbp + +/-! ## The record across slot writes -/ + +set_option maxHeartbeats 1000000 in +/-- **`of_slotWA` with the tracked-buffer set changing from `Bf` to `Bf'`**: the record's `Bf`-indexed facts are taken at +`Bf'` (`ep'` … `bfSpec'` at the pre-state, `bufCover` / `bufOff` at the post-state). -/ +theorem JStage3A.of_slotWBfA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {Bf' : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} + {J5 : JSlotA S} {Rj : Set U64 → JSlotA S} {s s' : S} + {a : A} {σ σ' : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7A st Rj) s a σ m) + {G' : MdbGhostA} (w : WritesIn st s s' (W7.CapW H.tcbs {b | InOS OS b} ∅)) + (HS : SpecSlotA σ σ') (hm : σ'.machineState = σ.machineState) + (hN : ∀ b, FootNA (L := L) Z s b → ¬ W7.CapW H.tcbs {b | InOS OS b} ∅ b) + (sysl : SysL st H Z.Cs OS ia ie s') + (jx : JAtZAX (L := L) AR { Z with G := G' } s' a σ' m) + (sinv : SInvXA σ' m n) + (objAlloc : ObjAllocA (st := st) { Z with G := G' } H OS s') + (idleAnchor : IdleAnchorOffA (st := st) OS s') + (ep' : EpStateRel Z.T E Rp Bf' (L.mem s) σ) (iplaced' : IPlacedA N Z.T E Rp Bf') + (ipcPlace' : IpcSchedPlacementA Z.th N E Rp Bf') + (bufVs' : ∀ b, BufW Bf' b → ¬ VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b) (bfSpec' : ∀ bp ∈ Bf', bp.2 = specPtr bp.1) + (bufCover : BufCover st Z.T Bf' s' σ') + (bufNative : BufNativeA Z.T (L.mem s')) + (deadExt : DeadExtA st { Z with G := G' } OS Rp stack s' σ') + (specFree : SpecFreeA st OS s' σ') + (utBlk : UtBlk39A st OS s') + (unmPt : UnmPtSpecA st { Z with G := G' } m OS s' σ') + (specZero : SpecZeroFreeA st OS s' σ') + (origLive : ∀ sl, σ'.isOriginalCap sl = true → AbsSlotA Z.th sl) + (nullOrig : ∀ sl, specCapAtA σ' sl = some .nullCap → σ'.isOriginalCap sl = false) + (frBase : FrBaseApartA st OS s') + (devFr : DevFrDisjA st H OS s' σ') + (finCov : FinCovA Z.th σ') + (zombieNoVcpu : ZombieTcbNoVcpuA st OS s') + (zombieTcb : ZombieTcbTA st H OS s') + (bufOff : BufOffA st { Z with G := G' } OS s' Bf') + (zombieBnd : UtZombieA st OS s') + (devUtOff : DevUtOffA st { Z with G := G' } H OS ia ie s') + (sidle : SIdleA σ' m n) + (capTy : CapTyA st H OS s') + (vcRun : VcRunA st Z.T Z.V s') + (vcSym1 : VcSym1A st Z.T s') (vcSym2 : VcSym2A st Z.V s') + (symL : SymL st H Z.Cs s') + (zwf : DelInvA st Z.th s') + (fhOK : ∀ t ∈ Z.T, NullOrEp H (capA st s' (slotIdx t layout.tcb.SLOT_FAULT_HANDLER))) + (ptCapIn : PtCapInA { Z with G := G' } (L.mem s')) + (utWin : UtWinCapA st OS s') (devWin : DevWinCapA st OS s') (userFr : UserFrSpecA st OS s' σ') (frTh : FrThA st Z.th OS s') + (j5 : J5 { Z with G := G' } H OS s' σ' m) + (j7 : J7A st Rj { Z with G := G' } H OS s' σ' m) : + JStage3A st AR { Z with G := G' } H OS U Ac Pp Tb Lv ia ie N E Rp Bf' stack κ n J5 (J7A st Rj) s' a σ' m := by + have hk := w.1 + have hc := w.2.1 + have hH := J.sys.ent.heap + have hO := J.sys.ent.os + have wA := writesIn_alAG w + have hB : ∀ b, (Aligned b ∧ W7.CapW H.tcbs {b | InOS OS b} ∅ b) → W7.CapW H.tcbs H.omega H.frame b := + fun b hb => os_capw hO hb.1 hb.2 + obtain ⟨blk, -, -, -, -, fld, nod, -⟩ := W7.blk_of_capw (BXI.blk J.sys.ent.bxi) wA hB + obtain ⟨Qb, Rb, q⟩ := J.sys.ent.bxi.1 + have keep : ∀ b, Aligned b → ¬ W7.CapW H.tcbs {b | InOS OS b} ∅ b → L.mem s' b = L.mem s b := + fun b hb hn => w.mem_eq hb hn + have hT : ∀ t ∈ Z.T, H t = some .tcb := fun t ht => by + have : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + exact this + -- the TCB fields from 224 + have tw : ∀ t ∈ Z.T, ∀ K : U64, 224 ≤ K.val → K.val + 8 ≤ 4096 → K.val % 8 = 0 → + L.mem s' (wa t K) = L.mem s (wa t K) := fun t ht K h0 hK h8 => + fld t (by rw [J.tcbs]; exact ht) K h0 (by omega) h8 + have vk : ∀ v ∈ Z.V, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → L.mem s' (wa v o) = L.mem s (wa v o) := by + obtain ⟨_, _, _, _, arm7, -⟩ := J.j7 + intro v hv o h1 h2 + obtain ⟨al, nw⟩ := vcw_notCapWA hH arm7.vh J.vcOS hv h1 h2 + exact w.mem_eq al nw + -- the spec side + obtain ⟨SS, NB⟩ := Endp.SpecSlotA.sstepA HS + have kS : ∀ p x k, σ.kheap p = some (.schedContext x k) → σ'.kheap p = some (.schedContext x k) := + fun p x k h => HS.keep h (fun _ _ e => by cases e) (fun _ e => by cases e) + have bS : ∀ p x k, σ'.kheap p = some (.schedContext x k) → σ.kheap p = some (.schedContext x k) := + fun p x k h => HS.back h (fun _ _ e => by cases e) (fun _ e => by cases e) + have bT : ∀ p y, σ'.kheap p = some (.tcb y) → ∃ x, σ.kheap p = some (.tcb x) ∧ tcbNcA y = tcbNcA x := by + intro p y h + rcases HS.kh p with e | ⟨r, cs, cs', -, e2⟩ | ⟨x, x', e1, e2, e3⟩ + · exact ⟨y, e ▸ h, rfl⟩ + · rw [h] at e2; cases e2 + · rw [h] at e2; cases e2; exact ⟨x, e1, e3⟩ + -- the scheduler relation + have sr0 : SchedSRA st Z m κ s' σ' := by + refine J.sr.stable wA (fun b hb hr => ?_) SS NB + have hcw := hB b hb + rcases hr with ⟨c, hcs, o, ho, e⟩ | e | e | ⟨t, ht, K, hK, e⟩ | h + · exact capw_ne_node q hcw hcs ho e + · exact hN b (.glob b (Or.inl e)) hb.2 + · exact hN b (.glob b (Or.inr e)) hb.2 + · have hKv : 224 ≤ K.val ∧ K.val < 4096 := by + rcases hK with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> simp + exact capw_ne_hi q hcw (by rw [J.tcbs]; exact ht) hKv.1 hKv.2 e + · obtain ⟨c, -, z, hz, hin⟩ := J.scFoot_heapA h + have e : H.scBits c = z := by unfold Heap.scBits; rw [hz] + rw [e] at hin + exact capw_objOffAG hH hO hb.1 hb.2 hz ⟨hin.1, by simp only [HKind.bits]; exact hin.2⟩ + (Or.inr (Or.inr (Or.inr ⟨z, rfl⟩))) + have sr : SchedSRA st { Z with G := G' } m κ s' σ' := + ⟨{ sr0.toSchedSRN with rest := { sr0.rest with } }, sr0.rd⟩ + -- the IPC relations + have ipcOff : ∀ b, Aligned b → W7.CapW H.tcbs {b | InOS OS b} ∅ b → ¬ IpcObjWA N E Rp b := fun b hb h hi => by + obtain ⟨p, k, hp, hk, hin⟩ := J.ipc_hobjA hi + exact capw_objOffAG hH hO hb h hp hin (by rcases hk with rfl | rfl | rfl <;> simp) + have tcbOff : ∀ b, Aligned b → W7.CapW H.tcbs {b | InOS OS b} ∅ b → ∀ t ∈ Z.T, + t.val + 224 ≤ b.val → b.val < t.val + 4096 → False := fun b hb h t ht h1 h2 => + capw_tcbHiAG hH hO hb h (hT t ht) h1 h2 + have ef : Endp.FootEq Z.T E Rp Bf' (L.mem s) (L.mem s') := by + intro b hb h + refine keep b hb (fun hp => ?_) + rcases h with ⟨e, he, h⟩ | ⟨r, hr, h⟩ | ⟨t, ht, h⟩ | h + · exact ipcOff b hb hp (Or.inr (Or.inl ⟨e, he, by omega, by omega⟩)) + · exact ipcOff b hb hp (Or.inr (Or.inr ⟨r, hr, by omega, by omega⟩)) + · rcases h with ⟨h1, h2⟩ | h + · unfold TOffN at h2; exact tcbOff b hb hp t ht (by omega) (by omega) + · unfold TL at h; exact tcbOff b hb hp t ht (by omega) (by omega) + · rcases hp with ⟨t, ht, K, hK, e⟩ | ⟨hos, -⟩ + · have ht' : t ∈ Z.T := by rw [← J.tcbs]; exact ht + have := iplaced'.e.bt b h t ht' + have hv := wa_val_tcbAG hH (hT t ht') (K := K) (by omega) + rw [e, hv] at this + omega + · exact bufOff.os b h hos + have ep : EpStateRel Z.T E Rp Bf' (L.mem s') σ' := ep'.frame iplaced'.e ef (Endp.SpecSlotA.kviewA HS hm) + have ntfn : NtfnStateRel N Z.T (L.mem s') σ' := by + refine J.ntfn.frame J.iplaced.n (fun b hb h => keep b hb (fun hp => ?_)) + ⟨fun p x h => HS.keep h (fun _ _ e => by cases e) (fun _ e => by cases e), fun p x h => ?_⟩ + · rcases h with ⟨nn, hn, h1, h2⟩ | ⟨t, ht, h⟩ + · exact ipcOff b hb hp (Or.inl ⟨nn, hn, h1, by omega⟩) + · have hta := J.sr.core.tcbs t ht + have fv : ∀ K : U64, K.val < 4096 → (wa t K).val = t.val + K.val := fun K hK => tfield_val hta hK + have inT : t.val + 224 ≤ b.val ∧ b.val < t.val + 4096 := by + rcases h with rfl | rfl | rfl | rfl | rfl | rfl + · rw [stA_eq, fv _ (by simp)]; simp + · unfold tBlockA; rw [fv _ (by simp)]; simp + · rw [badgeRegA_eq, fv _ (by simp)]; simp + · unfold prioA; rw [fv _ (by simp)]; simp + · rw [nextA_val hta]; simp [nextOff] + · rw [prevA_val hta]; simp [prevOff] + exact tcbOff b hb hp t ht inT.1 inT.2 + · obtain ⟨y, hy, e⟩ := HS.tcb h + obtain ⟨h1, -, -, h4, h5, -⟩ := tcbNcA_eq e + exact ⟨y, hy, h1, by unfold badgeOf; rw [h5], h4⟩ + have reply : ReplyStackRelA (L.mem s') Rp Z.C σ' (scNameA m) stack := by + refine J.reply.frame (fun r hr => ?_) (fun c _ x k h => ⟨x, k, kS _ _ _ h, rfl⟩) + (fun r _ x' h => ⟨x', HS.back h (fun _ _ e => by cases e) (fun _ e => by cases e), rfl⟩) + have hr3 := J.iplaced.e.rep r hr + have key : ∀ o : U64, o.val = 8 ∨ o.val = 16 → L.mem s' (wa r o) = L.mem s (wa r o) := by + intro o ho + have hv : (wa r o).val = r.val + o.val := by + simp [core.num.U64.wrapping_add_val_eq, U64.size_def, U64.numBits]; omega + have hal : Aligned (wa r o) := by unfold Aligned; rw [hv]; omega + exact keep _ hal (fun hp => ipcOff _ hal hp (Or.inr (Or.inr ⟨r, hr, by omega, by omega⟩))) + exact ⟨key _ (Or.inr (by simp [layout.reply.NEXT])), key _ (Or.inl (by simp [layout.reply.PREV]))⟩ + have mcp : McpRelA Z.T m (L.mem s') σ' := fun t ht => by + obtain ⟨x, hx, e, hf⟩ := J.mcp t ht + obtain ⟨y, hy, ey⟩ := HS.tcb hx + have hm' : y.tcbMcpriority = x.tcbMcpriority := by have h0 := congrArg Fl4SpecA64.Tcb.tcbMcpriority ey; exact h0 + have hfl : y.tcbFlags = x.tcbFlags := by have h0 := congrArg Fl4SpecA64.Tcb.tcbFlags ey; exact h0 + refine ⟨y, hy, ?_, fun g => (hf g).keep ?_ hfl⟩ + · unfold mcpA at e ⊢ + rw [tw t ht _ (by simp) (by simp) (by simp), hm'] + exact e + · unfold flagsA + exact tw t ht _ (by simp) (by simp) (by simp) + have ye : ∀ t ∈ Z.T, L.mem s' (yieldA t) = L.mem s (yieldA t) := fun t ht => by + unfold yieldA; exact tw t ht _ (by simp) (by simp) (by simp) + have yieldRel : YieldRelA Z.T Z.C m (L.mem s') σ' := by + refine ⟨fun t ht => ?_, fun t ht h => ?_, fun c hcc y k h p hp => ?_⟩ + · obtain ⟨x, hx, e⟩ := J.yieldRel.tcb t ht + obtain ⟨y, hy, ey⟩ := HS.tcb hx + have hy' : y.tcbYieldTo = x.tcbYieldTo := by have h0 := congrArg Fl4SpecA64.Tcb.tcbYieldTo ey; exact h0 + exact ⟨y, hy, by rw [ye t ht, hy']; exact e⟩ + · rw [ye t ht] at h ⊢ + obtain ⟨hC, y, k, hy, e⟩ := J.yieldRel.toSc t ht h + exact ⟨hC, y, k, kS _ _ _ hy, e⟩ + · obtain ⟨t, ht, e1, e2⟩ := J.yieldRel.frm c hcc y k (bS _ _ _ h) p hp + exact ⟨t, ht, e1, by rw [ye t ht]; exact e2⟩ + have idleNoFault : IdleNoFaultA m σ' := fun c hcn => by + obtain ⟨x, hx, e⟩ := J.idleNoFault c hcn + obtain ⟨y, hy, ey⟩ := HS.tcb hx + exact ⟨y, hy, by rw [← e]; have h0 := congrArg Fl4SpecA64.Tcb.tcbFault ey; exact h0⟩ + -- the entry invariants + have vsK : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → L.mem s' b = L.mem s b := fun b hb => + keep b (J.vsOk.al b hb) (J.vsr_offCapWA J.vsOk hb) + have vsT := utVsPt_keepA J.sys.ent.vs hk vsK + have ent : EntryInvA3 st H Z.Cs OS U Ac Pp Tb Lv s' := + { heap := hH + bxi := BlkOK.bxi blk + os := hO + user := J.sys.ent.user.ksb hk + vs := vsT.1 + ksw := by rw [hk]; exact J.sys.ent.ksw } + have sq : SchedQueuesInv st H.tcbs Z.Cs H.omega H s' Z.Q Z.R := + W7.inv_of_capw J.sq wA fun b hb => (hB b hb).elim Or.inr fun h' => Or.inl (Or.inl h'.1) + have poolOff : PoolOff st H Z.Cs OS s' := by + refine poolOff_keepTableA (Z := Z) J.poolOff hk fun i => vsK _ (Or.inr ?_) + obtain ⟨v1, v2, v3⟩ := AtOff.v i + exact ⟨AtOff i, v1, v2, v3, rfl⟩ + exact + { sys := ⟨ent, sysl⟩ + j := jx + tcbs := J.tcbs + scs := J.scs + sq := sq + ap := by rw [show L.ksb s' = L.ksb s from hk]; exact { J.ap with } + pp := J.pp + s1 := J.s1 + ko := by rw [show L.ksb s' = L.ksb s from hk]; exact J.ko + ptH := J.ptH + tabInj := fun i j p h1 h2 => J.tabInj i j p (by rw [← HS.asid]; exact h1) (by rw [← HS.asid]; exact h2) + poolOff := poolOff + objAlloc := objAlloc + idleAnchor := idleAnchor + vsVc := by rw [show L.ksb s' = L.ksb s from hk]; exact J.vsVc + sr := sr + scCov := fun p x k h => J.scCov p x k (bS _ _ _ h) + scSize := fun c hcm x k h => J.scSize c hcm x k (bS _ _ _ h) + ep := ep + ntfn := ntfn + reply := reply + bufCover := bufCover + bufNative := bufNative + iplaced := iplaced' + ipcPlace := ipcPlace' + idleNoFault := idleNoFault + mcp := mcp + yieldRel := yieldRel + sinv := sinv + setsInH := { J.setsInH with } + utIpc := J.utIpc + ipcTh := J.ipcTh + deadExt := deadExt + specFree := specFree + utBlk := utBlk + unmPt := unmPt + tabS1 := fun qq hq => + ⟨fun i hi h => (J.tabS1 qq hq).mem i hi + (footA_schedA (Z := Z) (Q := Z.Q) (Rl := Z.R) (curOf := Z.curOf) (curScOf := Z.curScOf) hk hc + (FootA.monoA (Z := { { Z with G := G' } with S1 := ∅ }) rfl rfl (fun _ h => h) (fun _ h => h) + (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) h)), + (J.tabS1 qq hq).obj⟩ + tabExt := + ⟨fun qq hq i hi h => J.tabExt.mem qq hq i hi + (footA_schedA (Z := extZA Z) (Q := Z.Q) (Rl := Z.R) (curOf := Z.curOf) (curScOf := Z.curScOf) hk hc + (FootA.monoA (Z := extZA { Z with G := G' }) rfl rfl (fun _ h => h) (fun _ h => h) + (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) h)), + J.tabExt.obj⟩ + specZero := specZero + origLive := origLive + nullOrig := nullOrig + thPos := fun p z h => J.thPos p z h + frBase := frBase + devFr := devFr + finCov := finCov + idleAll := J.idleAll.keepA + (fun c hcs => specPtr_inj ((sr.sch.idleOf c hcs).symm.trans (J.sr.sch.idleOf c hcs))) + (fun c hcs => specPtr_inj ((sr.sch.idleScOf c hcs).symm.trans (J.sr.sch.idleScOf c hcs))) + ipcHeapCov := J.ipcHeapCov + zombieNoVcpu := zombieNoVcpu + zombieTcb := zombieTcb + bufOff := bufOff + vsOk := by + unfold UtVsOkA + rw [show L.ksb s' = L.ksb s from hk, vsT.2] + exact J.vsOk + domOK := J.domOK.keepS J.sr.toSchedSRN HS.domk hk hc (fun b hb hd => w.mem_eq hb (fun h => J.capw_domA h hd)) + ipiZero := J.ipiKeepOf w fun o ho h => J.ipi_capwA ho h + bufVs := by + rw [show L.ksb s' = L.ksb s from hk, vsT.2] + exact bufVs' + bufAl := fun t ht y h => by + obtain ⟨x, hx, e⟩ := bT _ y h + have h0 : y.tcbIpcBuffer = x.tcbIpcBuffer := by + have h1 := congrArg Fl4SpecA64.Tcb.tcbIpcBuffer e + exact h1 + rw [h0]; exact J.bufAl t ht x hx + zombieBnd := zombieBnd + devUtOff := devUtOff + sidle := sidle + capTy := capTy + scCpu := J.scCpu + scNZ := J.scNZ + vcRun := vcRun + vcSym1 := vcSym1 + runVcpu := J.runVcpu.keep (fun y hy => by rw [stA_eq]; exact tw y hy 3328#u64 (by simp) (by simp) (by simp)) + (fun y hy => tw y hy 3672#u64 (by simp) (by simp) (by simp)) + vcSym2 := vcSym2 + blkQ := J.blkQ.keepA (fun t ht => tw t ht 3336#u64 (by simp) (by simp) (by simp)) + (fun t _ x' h => Or.inr (let ⟨x, hx, e⟩ := bT _ x' h; ⟨x, hx, (tcbNcA_eq e).1.symm⟩)) + (fun p y h => HS.keep h (fun _ _ e => by cases e) (fun _ e => by cases e)) (fun p y h => HS.keep h (fun _ _ e => by cases e) (fun _ e => by cases e)) + heapWin := J.heapWin + coresAll := J.coresAll + ptCapIn := ptCapIn + symL := symL + fhOK := fhOK + zwf := zwf + schedValid := J.schedValid.ofSlot HS + utWin := utWin + devWin := devWin + vcBack := vcBack_keepA J.vcRun.vtcb (fun v hv => by + have h0 := vk v hv 0#u64 (by simp) (by simp) + rwa [wa_zeroVcA] at h0) (fun t ht => tw t ht 3664#u64 (by simp) (by simp) (by simp)) J.vcBack + runBack := runBack_keepA J.vcRun.run (fun t ht => tw t ht 3672#u64 (by simp) (by simp) (by simp)) + (fun v hv => vk v hv 24#u64 (by simp) (by simp)) J.runBack + vcSnap := J.vcSnap.keepA vk (fun v _ x hx => HS.back hx (fun _ _ e => by cases e) (fun _ e => by cases e)) + vcOS := J.vcOS + userFr := userFr + frTh := frTh + bfSpec := bfSpec' + curVcpuNone := HS.vcur.trans J.curVcpuNone + ipcSorted := J.ipcSorted.of_q (QKeepA.of_slotA HS) + activeReplyScs := J.activeReplyScs.of_q (QKeepA.of_slotA HS) + j5 := j5 + j7 := j7 } + + +set_option maxHeartbeats 1000000 in +/-- **`JStage3A` across a write of slot words** (module docstring). -/ +theorem JStage3A.of_slotWA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J5 : JSlotA S} {Rj : Set U64 → JSlotA S} {s s' : S} + {a : A} {σ σ' : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7A st Rj) s a σ m) + {G' : MdbGhostA} (w : WritesIn st s s' (W7.CapW H.tcbs {b | InOS OS b} ∅)) + (HS : SpecSlotA σ σ') (hm : σ'.machineState = σ.machineState) + (hN : ∀ b, FootNA (L := L) Z s b → ¬ W7.CapW H.tcbs {b | InOS OS b} ∅ b) + (sysl : SysL st H Z.Cs OS ia ie s') + (jx : JAtZAX (L := L) AR { Z with G := G' } s' a σ' m) + (sinv : SInvXA σ' m n) + (objAlloc : ObjAllocA (st := st) { Z with G := G' } H OS s') + (idleAnchor : IdleAnchorOffA (st := st) OS s') + (bufCover : BufCover st Z.T Bf s' σ') + (bufNative : BufNativeA Z.T (L.mem s')) + (deadExt : DeadExtA st { Z with G := G' } OS Rp stack s' σ') + (specFree : SpecFreeA st OS s' σ') + (utBlk : UtBlk39A st OS s') + (unmPt : UnmPtSpecA st { Z with G := G' } m OS s' σ') + (specZero : SpecZeroFreeA st OS s' σ') + (origLive : ∀ sl, σ'.isOriginalCap sl = true → AbsSlotA Z.th sl) + (nullOrig : ∀ sl, specCapAtA σ' sl = some .nullCap → σ'.isOriginalCap sl = false) + (frBase : FrBaseApartA st OS s') + (devFr : DevFrDisjA st H OS s' σ') + (finCov : FinCovA Z.th σ') + (zombieNoVcpu : ZombieTcbNoVcpuA st OS s') + (zombieTcb : ZombieTcbTA st H OS s') + (bufOff : BufOffA st { Z with G := G' } OS s' Bf) + (zombieBnd : UtZombieA st OS s') + (devUtOff : DevUtOffA st { Z with G := G' } H OS ia ie s') + (sidle : SIdleA σ' m n) + (capTy : CapTyA st H OS s') + (vcRun : VcRunA st Z.T Z.V s') + (vcSym1 : VcSym1A st Z.T s') (vcSym2 : VcSym2A st Z.V s') + (symL : SymL st H Z.Cs s') + (zwf : DelInvA st Z.th s') + (fhOK : ∀ t ∈ Z.T, NullOrEp H (capA st s' (slotIdx t layout.tcb.SLOT_FAULT_HANDLER))) + (ptCapIn : PtCapInA { Z with G := G' } (L.mem s')) + (utWin : UtWinCapA st OS s') (devWin : DevWinCapA st OS s') (userFr : UserFrSpecA st OS s' σ') (frTh : FrThA st Z.th OS s') + (j5 : J5 { Z with G := G' } H OS s' σ' m) + (j7 : J7A st Rj { Z with G := G' } H OS s' σ' m) : + JStage3A st AR { Z with G := G' } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7A st Rj) s' a σ' m := + JStage3A.of_slotWBfA J (w := w) (HS := HS) (hm := hm) (hN := hN) (sysl := sysl) (jx := jx) (sinv := sinv) + (objAlloc := objAlloc) (idleAnchor := idleAnchor) (ep' := J.ep) (iplaced' := J.iplaced) (ipcPlace' := J.ipcPlace) + (bufVs' := J.bufVs) (bfSpec' := J.bfSpec) (bufCover := bufCover) (bufNative := bufNative) (deadExt := deadExt) + (specFree := specFree) (utBlk := utBlk) (unmPt := unmPt) (specZero := specZero) (origLive := origLive) + (nullOrig := nullOrig) (frBase := frBase) (devFr := devFr) (finCov := finCov) (zombieNoVcpu := zombieNoVcpu) + (zombieTcb := zombieTcb) (bufOff := bufOff) (zombieBnd := zombieBnd) (devUtOff := devUtOff) (sidle := sidle) + (capTy := capTy) (vcRun := vcRun) (vcSym1 := vcSym1) (vcSym2 := vcSym2) (symL := symL) (zwf := zwf) (fhOK := fhOK) + (ptCapIn := ptCapIn) (utWin := utWin) (devWin := devWin) (userFr := userFr) (frTh := frTh) (j5 := j5) (j7 := j7) + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3CapVsA.lean b/proofs/Fl4ProofsA64/P3/Stage3CapVsA.lean new file mode 100644 index 000000000..e6a6220a3 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3CapVsA.lean @@ -0,0 +1,607 @@ +/- +P3 (AArch64, merge line, D8 step 5, Architect option A): **the spec side of a whole cap-writing arm** — one +slot-only step (`SpecSlotA`: the `setCap`) followed by VSpace changes (`VsSpecA`: arch objects other than VCPUs, +the opaque machine part), with the ASID table kept — as one relation `CapVsSpecA`, and its consequences for the +record (`SStepA`, `NoBlockESc`, `Endp.KView`, object keeping). No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3CapFrameA +import Fl4ProofsA64.P3.Stage3CapSlotA +import Fl4ProofsA64.P3.Stage3VsCloseA +import Fl4ProofsA64.P3.IpcXferOrthA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +/-- **A slot step followed by a VSpace step** (module docstring). -/ +structure CapVsSpecA (σ σ' : Fl4SpecA64.State Unit) : Prop extends SpecGlobA σ σ' where + kh : ∀ p, σ'.kheap p = σ.kheap p ∨ + (∃ r cs cs', σ.kheap p = some (.cnode r cs) ∧ σ'.kheap p = some (.cnode r cs')) ∨ + (∃ x x', σ.kheap p = some (.tcb x) ∧ σ'.kheap p = some (.tcb x') ∧ tcbNcA x' = tcbNcA x) ∨ + ∃ a a', σ.kheap p = some (.archObj a) ∧ σ'.kheap p = some (.archObj a') ∧ (∀ v, a ≠ .vcpu v) ∧ (∀ v, a' ≠ .vcpu v) + um : σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory + +/-- Compose the two steps (the ASID table kept). -/ +theorem CapVsSpecA.of_steps {σ σc σ' : Fl4SpecA64.State Unit} (h1 : SpecSlotA σ σc) (h2 : VsSpecA σc σ') + (ha : σ'.archState.armAsidTable = σc.archState.armAsidTable) (hm : σc.machineState = σ.machineState) : + CapVsSpecA σ σ' := by + have e := h2.eq + refine ⟨⟨?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, h1.domk.trans h2.domk⟩, fun p => ?_, ?_⟩ + · rw [e]; exact h1.cur + · rw [e]; exact h1.idle + · rw [e]; exact h1.csc + · rw [e]; exact h1.act + · rw [e]; exact h1.time + · rw [e]; exact h1.irqS + · rw [e]; exact h1.irqN + · rw [e]; exact h1.rq + · rw [e]; exact h1.rls + · rw [e]; exact h1.cons + · rw [e]; exact h1.repr + · rw [e]; exact h1.dom + · rw [e]; exact h1.domT + · exact ha.trans h1.asid + · exact h2.vcur.trans h1.vcur + · rcases h2.kh p with e2 | ⟨a, a', e1, e2, n1, n2⟩ + · rcases h1.kh p with e1 | ⟨r, cs, cs', f1, f2⟩ | ⟨x, x', f1, f2, g1⟩ + · exact Or.inl (e2.trans e1) + · exact Or.inr (Or.inl ⟨r, cs, cs', f1, e2.trans f2⟩) + · exact Or.inr (Or.inr (Or.inl ⟨x, x', f1, e2.trans f2, g1⟩)) + · rcases h1.kh p with e1' | ⟨r, cs, cs', f1, f2⟩ | ⟨x, x', f1, f2, g1⟩ + · exact Or.inr (Or.inr (Or.inr ⟨a, a', e1'.symm.trans e1, e2, n1, n2⟩)) + · rw [f2] at e1; cases e1 + · rw [f2] at e1; cases e1 + · rw [e]; show σc.machineState.underlyingMemory = _; rw [hm] + +section api +variable {σ σ' : Fl4SpecA64.State Unit} + +theorem CapVsSpecA.keep (H : CapVsSpecA σ σ') {p : Fl4SpecA64.ObjRef} {ko : Fl4SpecA64.KernelObject} + (h : σ.kheap p = some ko) (hk : ∀ r cs, ko ≠ .cnode r cs) (ht : ∀ x, ko ≠ .tcb x) + (ha : ∀ a, ko ≠ .archObj a) : σ'.kheap p = some ko := by + rcases H.kh p with e | ⟨r, cs, cs', e1, -⟩ | ⟨x, x', e1, -⟩ | ⟨a, a', e1, -⟩ + · rw [e]; exact h + · rw [h] at e1; exact absurd (Option.some.inj e1) (hk r cs) + · rw [h] at e1; exact absurd (Option.some.inj e1) (ht x) + · rw [h] at e1; exact absurd (Option.some.inj e1) (ha a) + +theorem CapVsSpecA.back (H : CapVsSpecA σ σ') {p : Fl4SpecA64.ObjRef} {ko : Fl4SpecA64.KernelObject} + (h : σ'.kheap p = some ko) (hk : ∀ r cs, ko ≠ .cnode r cs) (ht : ∀ x, ko ≠ .tcb x) + (ha : ∀ a, ko ≠ .archObj a) : σ.kheap p = some ko := by + rcases H.kh p with e | ⟨r, cs, cs', -, e2⟩ | ⟨x, x', -, e2, -⟩ | ⟨a, a', -, e2, -⟩ + · rw [← e]; exact h + · rw [h] at e2; exact absurd (Option.some.inj e2) (hk r cs') + · rw [h] at e2; exact absurd (Option.some.inj e2) (ht x') + · rw [h] at e2; exact absurd (Option.some.inj e2) (ha a') + +theorem CapVsSpecA.tcb (H : CapVsSpecA σ σ') {p : Fl4SpecA64.ObjRef} {x : Fl4SpecA64.Tcb} + (h : σ.kheap p = some (.tcb x)) : ∃ x', σ'.kheap p = some (.tcb x') ∧ tcbNcA x' = tcbNcA x := by + rcases H.kh p with e | ⟨r, cs, cs', e1, -⟩ | ⟨y, y', e1, e2, hy⟩ | ⟨a, a', e1, -⟩ + · exact ⟨x, by rw [e]; exact h, rfl⟩ + · rw [h] at e1; cases e1 + · rw [h] at e1 + obtain rfl : x = y := by injection e1 with e1; injection e1 + exact ⟨y', e2, hy⟩ + · rw [h] at e1; cases e1 + +theorem CapVsSpecA.tcbBack (H : CapVsSpecA σ σ') {p : Fl4SpecA64.ObjRef} {y : Fl4SpecA64.Tcb} + (h : σ'.kheap p = some (.tcb y)) : ∃ x, σ.kheap p = some (.tcb x) ∧ tcbNcA y = tcbNcA x := by + rcases H.kh p with e | ⟨r, cs, cs', -, e2⟩ | ⟨x, x', e1, e2, e3⟩ | ⟨a, a', -, e2, -⟩ + · exact ⟨y, e ▸ h, rfl⟩ + · rw [h] at e2; cases e2 + · rw [h] at e2; cases e2; exact ⟨x, e1, e3⟩ + · rw [h] at e2; cases e2 + +theorem CapVsSpecA.sstepA (h : CapVsSpecA σ σ') : SStepA σ σ' ∧ NoBlockESc σ σ' := by + have nc := fun p x (hx : σ.kheap p = some (.tcb x)) => h.tcb hx + refine ⟨⟨⟨h.cur, h.csc, h.idle, h.time, h.cons, h.repr, h.dom, h.domT⟩, h.act, h.rq, h.rls, + fun p x n hx => (h.keep hx (fun _ _ e => by cases e) (fun _ e => by cases e) (fun _ e => by cases e)).trans hx.symm, + fun p x hx => ?_, + fun p x n hx => (h.back hx (fun _ _ e => by cases e) (fun _ e => by cases e) (fun _ e => by cases e)).trans hx.symm, + fun p y hy => ?_, + fun p y hy => ⟨y, h.back hy (fun _ _ e => by cases e) (fun _ e => by cases e) (fun _ e => by cases e), rfl⟩⟩, ?_⟩ + · obtain ⟨y, hy, e⟩ := nc p x hx + obtain ⟨-, h2, h3, h4, -, -⟩ := tcbNcA_eq e + exact ⟨y, hy, h2, h3, h4⟩ + · obtain ⟨x, hx, -⟩ := h.tcbBack hy + exact ⟨x, hx⟩ + · intro p x y hx hy + obtain ⟨y', hy', e⟩ := nc p x hx + rw [hy] at hy' + cases hy' + have hs := (tcbNcA_eq e).1 + have hf : y.tcbFault = x.tcbFault := by have := congrArg Tcb.tcbFault e; exact this + exact Or.inl ⟨hs, fun hc => by rwa [hs, hf] at hc⟩ + +theorem CapVsSpecA.kviewA (h : CapVsSpecA σ σ') : Endp.KView σ σ' := by + refine ⟨fun p x hx => h.keep hx (fun _ _ e => by cases e) (fun _ e => by cases e) (fun _ e => by cases e), + fun p x hx => ⟨x, h.keep hx (fun _ _ e => by cases e) (fun _ e => by cases e) (fun _ e => by cases e), rfl⟩, + fun p x hx => ?_, h.um⟩ + obtain ⟨z', e2, e3⟩ := h.tcb hx + obtain ⟨h1, -, -, h4, h5, -⟩ := tcbNcA_eq e3 + have hf : z'.tcbFault = x.tcbFault := by have := congrArg Tcb.tcbFault e3; exact this + have hn : z'.tcbBoundNotification = x.tcbBoundNotification := by + have := congrArg Tcb.tcbBoundNotification e3; exact this + exact ⟨z', e2, h1, by rw [h5], h4, hf, hn⟩ + +end api + +local notation "wa" => core.num.U64.wrapping_add + +section main +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +set_option maxHeartbeats 4000000 in +/-- **`JStage3A` across a whole cap-writing arm with a two-way ghost update**: as `of_capVsA`, but the final record +holds at new relation ghosts `S1'`, `Zl'` (`XSetsA.Lv`), `T2'`, `Sh2'` and new record tables `Tb'` / depths `Lv'` +(arbitrary: a map grows them, an unmap shrinks them, ASID assign inserts roots). `Pp`, `X`, `Y`, `th`, `G` and the +scheduler / IPC ghosts are kept. The fields that read these ghosts are hypotheses at the final state. -/ +theorem JStage3A.of_capVsGA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Ac' Pp Tb : Set U64} + {Lv Lv' Zl' Sh2' : U64 → Nat} {Tb' S1' T2' : Set U64} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J5 : JSlotA S} {Rj : Set U64 → JSlotA S} {s s' : S} + {a a' : A} {σ σc σ' : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7A st Rj) s a σ m) + {hw : HwRootA} {P : U64 → Prop} + (w : WritesIn st s s' (fun b => W7.CapW H.tcbs {b | InOS OS b} ∅ b ∨ P b)) + (hP : ∀ Pt Ub : Set U64, VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt → + (∀ p, SchedPageA st Z OS s p → p ∈ Pt) → ∀ b, P b → VsR (L.ksb s) Pt b) + (hbf : ∀ b, P b → ¬ BufW Bf b) + (HS1 : SpecSlotA σ σc) (hm1 : σc.machineState = σ.machineState) (HS2 : VsSpecA σc σ') + (hasid : σ'.archState.armAsidTable = σc.archState.armAsidTable) + (hdp : ∀ p dev pg, σ.kheap p = some (.archObj (.dataPage dev pg)) → + σ'.kheap p = some (.archObj (.dataPage dev pg))) + (hN : ∀ b, FootNA (L := L) Z s b → ¬ W7.CapW H.tcbs {b | InOS OS b} ∅ b) + (jx : JAtZAX (L := L) AR { Z with hw := hw, S1 := S1', Lv := Zl', T2 := T2', Sh2 := Sh2' } s' a' σ' m) + (sys : SysInvA st H Z.Cs OS U Ac' Pp Tb' Lv' ia ie s') + (vsOk' : UtVsOkA st H OS Z.V Pp Tb' Lv' s') + (bufVs' : ∀ b, BufW Bf b → ¬ VsR (L.ksb s') (utVsPtA st Pp Tb' Lv' s') b) + (sq : SchedQueuesInv st H.tcbs Z.Cs H.omega H s' Z.Q Z.R) + (poolOff : PoolOff st H Z.Cs OS s') + (sinv : SInvXA σ' m n) + (objAlloc : ObjAllocA (st := st) { Z with hw := hw, S1 := S1', Lv := Zl', T2 := T2', Sh2 := Sh2' } H OS s') + (idleAnchor : IdleAnchorOffA (st := st) OS s') + (bufCover : BufCover st Z.T Bf s' σ') + (bufNative : BufNativeA Z.T (L.mem s')) + (deadExt : DeadExtA st { Z with hw := hw, S1 := S1', Lv := Zl', T2 := T2', Sh2 := Sh2' } OS Rp stack s' σ') + (specFree : SpecFreeA st OS s' σ') + (utBlk : UtBlk39A st OS s') + (unmPt : UnmPtSpecA st { Z with hw := hw, S1 := S1', Lv := Zl', T2 := T2', Sh2 := Sh2' } m OS s' σ') + (specZero : SpecZeroFreeA st OS s' σ') + (origLive : ∀ sl, σ'.isOriginalCap sl = true → AbsSlotA Z.th sl) + (nullOrig : ∀ sl, specCapAtA σ' sl = some .nullCap → σ'.isOriginalCap sl = false) + (frBase : FrBaseApartA st OS s') + (devFr : DevFrDisjA st H OS s' σ') + (finCov : FinCovA Z.th σ') + (zombieNoVcpu : ZombieTcbNoVcpuA st OS s') + (zombieTcb : ZombieTcbTA st H OS s') + (bufOff : BufOffA st { Z with hw := hw, S1 := S1', Lv := Zl', T2 := T2', Sh2 := Sh2' } OS s' Bf) + (zombieBnd : UtZombieA st OS s') + (devUtOff : DevUtOffA st { Z with hw := hw, S1 := S1', Lv := Zl', T2 := T2', Sh2 := Sh2' } H OS ia ie s') + (sidle : SIdleA σ' m n) + (capTy : CapTyA st H OS s') + (vcRun : VcRunA st Z.T Z.V s') + (vcSym1 : VcSym1A st Z.T s') (vcSym2 : VcSym2A st Z.V s') + (symL : SymL st H Z.Cs s') + (zwf : DelInvA st Z.th s') + (fhOK : ∀ t ∈ Z.T, NullOrEp H (capA st s' (slotIdx t layout.tcb.SLOT_FAULT_HANDLER))) + (ptCapIn : PtCapInA { Z with hw := hw, S1 := S1', Lv := Zl', T2 := T2', Sh2 := Sh2' } (L.mem s')) + (utWin : UtWinCapA st OS s') (devWin : DevWinCapA st OS s') (userFr : UserFrSpecA st OS s' σ') (frTh : FrThA st Z.th OS s') + (j5 : J5 { Z with hw := hw, S1 := S1', Lv := Zl', T2 := T2', Sh2 := Sh2' } H OS s' σ' m) + (j7 : J7A st Rj { Z with hw := hw, S1 := S1', Lv := Zl', T2 := T2', Sh2 := Sh2' } H OS s' σ' m) + (ap' : SlotApA { Z with hw := hw, S1 := S1', Lv := Zl', T2 := T2', Sh2 := Sh2' } H OS (L.ksb s')) + (s1' : S1' ⊆ Tb') + (ptH' : PtHA H (Z.Pp ∪ S1')) + (setsInH' : SetsInHA { Z with hw := hw, S1 := S1', Lv := Zl', T2 := T2', Sh2 := Sh2' } H) + (vsVc' : ∀ b, VsFootA Z.Pp S1' Z.Y (L.ksb s') b → ¬ VcW Z.V b) + (tabS1' : ∀ q ∈ S1', TabOffA (L := L) { Z with hw := hw, S1 := S1', Lv := Zl', T2 := T2', Sh2 := Sh2' } m s' q) + (tabExt' : ExtTabOffA (L := L) { Z with hw := hw, S1 := S1', Lv := Zl', T2 := T2', Sh2 := Sh2' } m s') : + JStage3A st AR { Z with hw := hw, S1 := S1', Lv := Zl', T2 := T2', Sh2 := Sh2' } H OS U Ac' Pp Tb' Lv' ia ie N E Rp Bf stack κ n J5 (J7A st Rj) s' a' σ' m := by + have HS : CapVsSpecA σ σ' := CapVsSpecA.of_steps HS1 HS2 hasid hm1 + have hk := w.1 + have vk : ∀ v ∈ Z.V, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → + L.mem s' (core.num.U64.wrapping_add v o) = L.mem s (core.num.U64.wrapping_add v o) := by + obtain ⟨Pt7, Ub7, vr7, -, arm7, -, -, -, -, link7, -⟩ := J.j7 + intro v hv o h1 h2 + obtain ⟨al, nw⟩ := vcw_notCapWA J.sys.ent.heap arm7.vh J.vcOS hv h1 h2 + exact w.mem_eq al (fun hb => hb.elim nw (fun hp => vr7.vc _ (hP Pt7 Ub7 vr7 link7 _ hp) ⟨v, hv, o, h1, h2, rfl⟩)) + have hc := w.2.1 + have hH := J.sys.ent.heap + have hO := J.sys.ent.os + obtain ⟨Qb, Rb, q⟩ := J.sys.ent.bxi.1 + obtain ⟨Pt, Ub, vr, -, -, -, -, -, -, link, -, -⟩ := J.j7 + have V : ∀ b, P b → VsR (L.ksb s) Pt b := hP Pt Ub vr link + have keep : ∀ b, Aligned b → ¬ (W7.CapW H.tcbs {b | InOS OS b} ∅ b ∨ P b) → L.mem s' b = L.mem s b := + fun b hb hn => w.mem_eq hb hn + have hT : ∀ t ∈ Z.T, H t = some .tcb := fun t ht => by + have : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + exact this + have hB : ∀ b, Aligned b → W7.CapW H.tcbs {b | InOS OS b} ∅ b → W7.CapW H.tcbs H.omega H.frame b := + fun b hb h => os_capw hO hb h + -- P is off the record's words + have offT : ∀ b, P b → ∀ t ∈ Z.T, b.val < t.val ∨ t.val + 4096 ≤ b.val := fun b hb t ht => by + have := (q.omega b (vr.om b (V b hb))).2.1 t (J.tcbs ▸ ht) + omega + have offI : ∀ b, P b → ¬ IpcObjWA N E Rp b := fun b hb => J.ipc_omegaA (vr.om b (V b hb)) (vr.sg b (V b hb)) + have offSC : ∀ b, P b → ¬ ScFootA Z.C m σ b := fun b hb h => by + obtain ⟨c, hcC, z, hz, hin⟩ := J.scFoot_heapA h + exact vr.sc b (V b hb) c ⟨z, hz⟩ hin + have offN : ∀ b, P b → ¬ ((L.ksb s).val + 223488 ≤ b.val ∧ b.val < (L.ksb s).val + 772352) := fun b hb => by + obtain ⟨-, p, k, hp, -, hin⟩ := vr.om b (V b hb) + exact J.heap_offNodesA hp hin.1 hin.2 + have ipcOff : ∀ b, Aligned b → (W7.CapW H.tcbs {b | InOS OS b} ∅ b ∨ P b) → ¬ IpcObjWA N E Rp b := by + intro b hb hp hi + rcases hp with h | h + · obtain ⟨p, k, hp, hk, hin⟩ := J.ipc_hobjA hi + exact capw_objOffAG hH hO hb h hp hin (by rcases hk with rfl | rfl | rfl <;> simp) + · exact offI b h hi + have tcbOff : ∀ b, Aligned b → (W7.CapW H.tcbs {b | InOS OS b} ∅ b ∨ P b) → ∀ t ∈ Z.T, + t.val + 224 ≤ b.val → b.val < t.val + 4096 → False := by + intro b hb hp t ht h1 h2 + rcases hp with h | h + · exact capw_tcbHiAG hH hO hb h (hT t ht) h1 h2 + · have := offT b h t ht + omega + have tw : ∀ t ∈ Z.T, ∀ K : U64, 224 ≤ K.val → K.val + 8 ≤ 4096 → K.val % 8 = 0 → + L.mem s' (wa t K) = L.mem s (wa t K) := by + intro t ht K h0 hK h8 + have hta := J.sr.core.tcbs t ht + have hv : (wa t K).val = t.val + K.val := field_val hta (K := K) (by omega) + refine keep _ (field_aligned hta (by omega) h8) (fun hp => tcbOff _ (field_aligned hta (by omega) h8) hp t ht + (by omega) (by omega)) + -- the spec side + obtain ⟨SS, NB⟩ := HS.sstepA + have kS : ∀ p x k, σ.kheap p = some (.schedContext x k) → σ'.kheap p = some (.schedContext x k) := + fun p x k h => HS.keep h (fun _ _ e => by cases e) (fun _ e => by cases e) (fun _ e => by cases e) + have bS : ∀ p x k, σ'.kheap p = some (.schedContext x k) → σ.kheap p = some (.schedContext x k) := + fun p x k h => HS.back h (fun _ _ e => by cases e) (fun _ e => by cases e) (fun _ e => by cases e) + have bT : ∀ p y, σ'.kheap p = some (.tcb y) → ∃ x, σ.kheap p = some (.tcb x) ∧ tcbNcA y = tcbNcA x := + fun p y h => HS.tcbBack h + have wA : WritesIn st s s' (fun b => Aligned b ∧ (W7.CapW H.tcbs {b | InOS OS b} ∅ b ∨ P b)) := + ⟨w.1, w.2.1, fun b hb hn => w.2.2 b hb fun h => hn ⟨hb, h⟩⟩ + -- the scheduler relation + have sr0 : SchedSRA st Z m κ s' σ' := by + refine J.sr.stable wA (fun b hb hr => ?_) SS NB + rcases hb.2 with hcw0 | hpP + · have hcw := hB b hb.1 hcw0 + rcases hr with ⟨c, hcs, o, ho, e⟩ | e | e | ⟨t, ht, K, hK, e⟩ | h + · exact capw_ne_node q hcw hcs ho e + · exact hN b (.glob b (Or.inl e)) hcw0 + · exact hN b (.glob b (Or.inr e)) hcw0 + · have hKv : 224 ≤ K.val ∧ K.val < 4096 := by + rcases hK with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> simp + exact capw_ne_hi q hcw (by rw [J.tcbs]; exact ht) hKv.1 hKv.2 e + · obtain ⟨c, -, z, hz, hin⟩ := J.scFoot_heapA h + have e : H.scBits c = z := by unfold Heap.scBits; rw [hz] + rw [e] at hin + exact capw_objOffAG hH hO hb.1 hcw0 hz ⟨hin.1, by simp only [HKind.bits]; exact hin.2⟩ + (Or.inr (Or.inr (Or.inr ⟨z, rfl⟩))) + · rcases hr with ⟨c, hcs, o, ho, e⟩ | e | e | ⟨t, ht, K, hK, e⟩ | h + · have hin := J.sr.toSchedSRN.node_in hcs ho + obtain ⟨nn, -, hn, hcs'⟩ := J.sr.csN + have hcl : c.val < 128 := by have := (hcs' c).1 hcs; omega + have hv := nodeAt_valSc (s := s) J.sr.ksFit hcl ho + exact offN b hpP (by rw [e, hv]; omega) + · exact vr.ks b (V b hpP) 8#u64 (by simp) e.symm + · exact vr.ks b (V b hpP) 16#u64 (by simp) e.symm + · have hta := J.sr.core.tcbs t ht + have hKv : K.val + 8 ≤ 4096 := by rcases hK with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> simp + have hv : (wa t K).val = t.val + K.val := field_val hta (K := K) (by omega) + have := offT b hpP t ht + rw [e, hv] at this + omega + · exact offSC b hpP h + have sr : SchedSRA st { Z with hw := hw, S1 := S1', Lv := Zl', T2 := T2', Sh2 := Sh2' } m κ s' σ' := + ⟨{ sr0.toSchedSRN with rest := { sr0.rest with } }, sr0.rd⟩ + -- the IPC relations + have ef : Endp.FootEq Z.T E Rp Bf (L.mem s) (L.mem s') := by + intro b hb h + refine keep b hb (fun hp => ?_) + rcases h with ⟨e, he, h⟩ | ⟨r, hr, h⟩ | ⟨t, ht, h⟩ | h + · exact ipcOff b hb hp (Or.inr (Or.inl ⟨e, he, by omega, by omega⟩)) + · exact ipcOff b hb hp (Or.inr (Or.inr ⟨r, hr, by omega, by omega⟩)) + · rcases h with ⟨h1, h2⟩ | h + · unfold TOffN at h2; exact tcbOff b hb hp t ht (by omega) (by omega) + · unfold TL at h; exact tcbOff b hb hp t ht (by omega) (by omega) + · rcases hp with hp | hp + · rcases hp with ⟨t, ht, K, hK, e⟩ | ⟨hos, -⟩ + · have ht' : t ∈ Z.T := by rw [← J.tcbs]; exact ht + have := J.iplaced.e.bt b h t ht' + have hv := wa_val_tcbAG hH (hT t ht') (K := K) (by omega) + rw [e, hv] at this + omega + · exact J.bufOff.os b h hos + · exact hbf b hp h + have ep : EpStateRel Z.T E Rp Bf (L.mem s') σ' := J.ep.frame J.iplaced.e ef HS.kviewA + have ntfn : NtfnStateRel N Z.T (L.mem s') σ' := by + refine J.ntfn.frame J.iplaced.n (fun b hb h => keep b hb (fun hp => ?_)) + ⟨fun p x h => HS.keep h (fun _ _ e => by cases e) (fun _ e => by cases e) (fun _ e => by cases e), + fun p x h => ?_⟩ + · rcases h with ⟨nn, hn, h1, h2⟩ | ⟨t, ht, h⟩ + · exact ipcOff b hb hp (Or.inl ⟨nn, hn, h1, by omega⟩) + · have hta := J.sr.core.tcbs t ht + have fv : ∀ K : U64, K.val < 4096 → (wa t K).val = t.val + K.val := fun K hK => tfield_val hta hK + have inT : t.val + 224 ≤ b.val ∧ b.val < t.val + 4096 := by + rcases h with rfl | rfl | rfl | rfl | rfl | rfl + · rw [stA_eq, fv _ (by simp)]; simp + · unfold tBlockA; rw [fv _ (by simp)]; simp + · rw [badgeRegA_eq, fv _ (by simp)]; simp + · unfold prioA; rw [fv _ (by simp)]; simp + · rw [nextA_val hta]; simp [nextOff] + · rw [prevA_val hta]; simp [prevOff] + exact tcbOff b hb hp t ht inT.1 inT.2 + · obtain ⟨y, hy, e⟩ := HS.tcb h + obtain ⟨h1, -, -, h4, h5, -⟩ := tcbNcA_eq e + exact ⟨y, hy, h1, by unfold badgeOf; rw [h5], h4⟩ + have reply : ReplyStackRelA (L.mem s') Rp Z.C σ' (scNameA m) stack := by + refine J.reply.frame (fun r hr => ?_) (fun c _ x k h => ⟨x, k, kS _ _ _ h, rfl⟩) + (fun r _ x' h => ⟨x', HS.back h (fun _ _ e => by cases e) (fun _ e => by cases e) (fun _ e => by cases e), rfl⟩) + have hr3 := J.iplaced.e.rep r hr + have key : ∀ o : U64, o.val = 8 ∨ o.val = 16 → L.mem s' (wa r o) = L.mem s (wa r o) := by + intro o ho + have hv : (wa r o).val = r.val + o.val := by + simp [core.num.U64.wrapping_add_val_eq, U64.size_def, U64.numBits]; omega + have hal : Aligned (wa r o) := by unfold Aligned; rw [hv]; omega + exact keep _ hal (fun hp => ipcOff _ hal hp (Or.inr (Or.inr ⟨r, hr, by omega, by omega⟩))) + exact ⟨key _ (Or.inr (by simp [layout.reply.NEXT])), key _ (Or.inl (by simp [layout.reply.PREV]))⟩ + have mcp : McpRelA Z.T m (L.mem s') σ' := fun t ht => by + obtain ⟨x, hx, e, hf⟩ := J.mcp t ht + obtain ⟨y, hy, ey⟩ := HS.tcb hx + have hm' : y.tcbMcpriority = x.tcbMcpriority := by have h0 := congrArg Fl4SpecA64.Tcb.tcbMcpriority ey; exact h0 + have hfl : y.tcbFlags = x.tcbFlags := by have h0 := congrArg Fl4SpecA64.Tcb.tcbFlags ey; exact h0 + refine ⟨y, hy, ?_, fun g => (hf g).keep ?_ hfl⟩ + · unfold mcpA at e ⊢ + rw [tw t ht _ (by simp) (by simp) (by simp), hm'] + exact e + · unfold flagsA + exact tw t ht _ (by simp) (by simp) (by simp) + have ye : ∀ t ∈ Z.T, L.mem s' (yieldA t) = L.mem s (yieldA t) := fun t ht => by + unfold yieldA; exact tw t ht _ (by simp) (by simp) (by simp) + have yieldRel : YieldRelA Z.T Z.C m (L.mem s') σ' := by + refine ⟨fun t ht => ?_, fun t ht h => ?_, fun c hcc y k h p hp => ?_⟩ + · obtain ⟨x, hx, e⟩ := J.yieldRel.tcb t ht + obtain ⟨y, hy, ey⟩ := HS.tcb hx + have hy' : y.tcbYieldTo = x.tcbYieldTo := by have h0 := congrArg Fl4SpecA64.Tcb.tcbYieldTo ey; exact h0 + exact ⟨y, hy, by rw [ye t ht, hy']; exact e⟩ + · rw [ye t ht] at h ⊢ + obtain ⟨hC, y, k, hy, e⟩ := J.yieldRel.toSc t ht h + exact ⟨hC, y, k, kS _ _ _ hy, e⟩ + · obtain ⟨t, ht, e1, e2⟩ := J.yieldRel.frm c hcc y k (bS _ _ _ h) p hp + exact ⟨t, ht, e1, by rw [ye t ht]; exact e2⟩ + have idleNoFault : IdleNoFaultA m σ' := fun c hcn => by + obtain ⟨x, hx, e⟩ := J.idleNoFault c hcn + obtain ⟨y, hy, ey⟩ := HS.tcb hx + exact ⟨y, hy, by rw [← e]; have h0 := congrArg Fl4SpecA64.Tcb.tcbFault ey; exact h0⟩ + exact + { sys := sys + j := jx + tcbs := J.tcbs + scs := J.scs + sq := sq + ap := ap' + pp := J.pp + s1 := s1' + ko := by rw [show L.ksb s' = L.ksb s from hk]; exact J.ko + ptH := ptH' + tabInj := fun i j p h1 h2 => J.tabInj i j p (by rw [← HS.asid]; exact h1) (by rw [← HS.asid]; exact h2) + poolOff := poolOff + objAlloc := objAlloc + idleAnchor := idleAnchor + vsVc := vsVc' + sr := sr + scCov := fun p x k h => J.scCov p x k (bS _ _ _ h) + scSize := fun c hcm x k h => J.scSize c hcm x k (bS _ _ _ h) + ep := ep + ntfn := ntfn + reply := reply + bufCover := bufCover + bufNative := bufNative + iplaced := J.iplaced + ipcPlace := J.ipcPlace + idleNoFault := idleNoFault + mcp := mcp + yieldRel := yieldRel + sinv := sinv + setsInH := setsInH' + utIpc := J.utIpc + ipcTh := J.ipcTh + deadExt := deadExt + specFree := specFree + utBlk := utBlk + unmPt := unmPt + tabS1 := tabS1' + tabExt := tabExt' + specZero := specZero + origLive := origLive + nullOrig := nullOrig + thPos := fun p z h => J.thPos p z h + frBase := frBase + devFr := devFr + finCov := finCov + idleAll := J.idleAll.keepA + (fun c hcs => specPtr_inj ((sr.sch.idleOf c hcs).symm.trans (J.sr.sch.idleOf c hcs))) + (fun c hcs => specPtr_inj ((sr.sch.idleScOf c hcs).symm.trans (J.sr.sch.idleScOf c hcs))) + ipcHeapCov := J.ipcHeapCov + zombieNoVcpu := zombieNoVcpu + zombieTcb := zombieTcb + bufOff := bufOff + vsOk := vsOk' + bufAl := fun t ht y h => by + obtain ⟨x, hx, e⟩ := bT _ y h + have h0 : y.tcbIpcBuffer = x.tcbIpcBuffer := by + have h1 := congrArg Fl4SpecA64.Tcb.tcbIpcBuffer e + exact h1 + rw [h0]; exact J.bufAl t ht x hx + zombieBnd := zombieBnd + devUtOff := devUtOff + sidle := sidle + capTy := capTy + scCpu := J.scCpu + scNZ := J.scNZ + vcRun := vcRun + vcSym1 := vcSym1 + runVcpu := J.runVcpu.keep (fun y hy => by rw [stA_eq]; exact tw y hy 3328#u64 (by simp) (by simp) (by simp)) + (fun y hy => tw y hy 3672#u64 (by simp) (by simp) (by simp)) + vcSym2 := vcSym2 + blkQ := J.blkQ.keepA (fun t ht => tw t ht 3336#u64 (by simp) (by simp) (by simp)) + (fun t _ x' h => Or.inr (let ⟨x, hx, e⟩ := bT _ x' h; ⟨x, hx, (tcbNcA_eq e).1.symm⟩)) + (fun p y h => HS.keep h (fun _ _ e => by cases e) (fun _ e => by cases e) (fun _ e => by cases e)) (fun p y h => HS.keep h (fun _ _ e => by cases e) (fun _ e => by cases e) (fun _ e => by cases e)) + heapWin := J.heapWin + coresAll := J.coresAll + ptCapIn := ptCapIn + symL := symL + fhOK := fhOK + zwf := zwf + schedValid := (J.schedValid.ofSlot HS1).ofVs HS2 + utWin := utWin + devWin := devWin + vcBack := vcBack_keepA J.vcRun.vtcb (fun v hv => by + have h0 := vk v hv 0#u64 (by simp) (by simp) + rwa [wa_zeroVcA] at h0) (fun t ht => tw t ht 3664#u64 (by simp) (by simp) (by simp)) J.vcBack + runBack := runBack_keepA J.vcRun.run (fun t ht => tw t ht 3672#u64 (by simp) (by simp) (by simp)) + (fun v hv => vk v hv 24#u64 (by simp) (by simp)) J.runBack + vcSnap := J.vcSnap.keepA vk (fun v _ x hx => by + rcases HS.kh (specPtr v) with e | ⟨_, _, _, -, e2⟩ | ⟨_, _, -, e2, -⟩ | ⟨_, a2, -, e2, -, nv⟩ + · rw [← e]; exact hx + · rw [hx] at e2; cases e2 + · rw [hx] at e2; cases e2 + · rw [hx] at e2; cases e2; exact absurd rfl (nv x)) + vcOS := J.vcOS + userFr := userFr + frTh := frTh + bfSpec := J.bfSpec + curVcpuNone := HS.vcur.trans J.curVcpuNone + ipcSorted := J.ipcSorted.of_q ((QKeepA.of_slotA HS1).trans (QKeepA.of_vsA HS2)) + activeReplyScs := J.activeReplyScs.of_q ((QKeepA.of_slotA HS1).trans (QKeepA.of_vsA HS2)) + domOK := J.domOK.keepS J.sr.toSchedSRN HS.domk hk hc (fun b hb hd => w.mem_eq hb (fun h => h.elim (fun h => J.capw_domA h hd) (fun hp => by obtain ⟨o, ho, eo⟩ := hd.wa_exists J.sr.ksFit; exact vr.ks b (V b hp) o (by omega) eo))) + ipiZero := J.ipiKeepOf w fun o ho h => h.elim (fun h => J.ipi_capwA ho h) (fun hp => J.ipi_omegaA ho (vr.om _ (V _ hp))) + bufVs := bufVs' + j5 := j5 + j7 := j7 } + +set_option maxHeartbeats 1000000 in +/-- **`JStage3A` across a whole cap-writing arm**: the invoked cap slot's two words and `P` (VSpace table / pool / ASID +words), entry to final, with no intermediate record. The spec side is the slot step `σ → σc` and the VSpace step +`σc → σ'`. The fields that read caps or tables are the arm's hypotheses at the final state. -/ +theorem JStage3A.of_capVsA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Ac' Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J5 : JSlotA S} {Rj : Set U64 → JSlotA S} {s s' : S} + {a a' : A} {σ σc σ' : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7A st Rj) s a σ m) + {hw : HwRootA} {P : U64 → Prop} + (w : WritesIn st s s' (fun b => W7.CapW H.tcbs {b | InOS OS b} ∅ b ∨ P b)) + (hP : ∀ Pt Ub : Set U64, VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt → + (∀ p, SchedPageA st Z OS s p → p ∈ Pt) → ∀ b, P b → VsR (L.ksb s) Pt b) + (hbf : ∀ b, P b → ¬ BufW Bf b) + (HS1 : SpecSlotA σ σc) (hm1 : σc.machineState = σ.machineState) (HS2 : VsSpecA σc σ') + (hasid : σ'.archState.armAsidTable = σc.archState.armAsidTable) + (hdp : ∀ p dev pg, σ.kheap p = some (.archObj (.dataPage dev pg)) → + σ'.kheap p = some (.archObj (.dataPage dev pg))) + (hN : ∀ b, FootNA (L := L) Z s b → ¬ W7.CapW H.tcbs {b | InOS OS b} ∅ b) + (jx : JAtZAX (L := L) AR { Z with hw := hw } s' a' σ' m) + (sys : SysInvA st H Z.Cs OS U Ac' Pp Tb Lv ia ie s') + (vsOk' : UtVsOkA st H OS Z.V Pp Tb Lv s') + (bufVs' : ∀ b, BufW Bf b → ¬ VsR (L.ksb s') (utVsPtA st Pp Tb Lv s') b) + (sq : SchedQueuesInv st H.tcbs Z.Cs H.omega H s' Z.Q Z.R) + (poolOff : PoolOff st H Z.Cs OS s') + (sinv : SInvXA σ' m n) + (objAlloc : ObjAllocA (st := st) { Z with hw := hw } H OS s') + (idleAnchor : IdleAnchorOffA (st := st) OS s') + (bufCover : BufCover st Z.T Bf s' σ') + (bufNative : BufNativeA Z.T (L.mem s')) + (deadExt : DeadExtA st { Z with hw := hw } OS Rp stack s' σ') + (specFree : SpecFreeA st OS s' σ') + (utBlk : UtBlk39A st OS s') + (unmPt : UnmPtSpecA st { Z with hw := hw } m OS s' σ') + (specZero : SpecZeroFreeA st OS s' σ') + (origLive : ∀ sl, σ'.isOriginalCap sl = true → AbsSlotA Z.th sl) + (nullOrig : ∀ sl, specCapAtA σ' sl = some .nullCap → σ'.isOriginalCap sl = false) + (frBase : FrBaseApartA st OS s') + (devFr : DevFrDisjA st H OS s' σ') + (finCov : FinCovA Z.th σ') + (zombieNoVcpu : ZombieTcbNoVcpuA st OS s') + (zombieTcb : ZombieTcbTA st H OS s') + (bufOff : BufOffA st { Z with hw := hw } OS s' Bf) + (zombieBnd : UtZombieA st OS s') + (devUtOff : DevUtOffA st { Z with hw := hw } H OS ia ie s') + (sidle : SIdleA σ' m n) + (capTy : CapTyA st H OS s') + (vcRun : VcRunA st Z.T Z.V s') + (vcSym1 : VcSym1A st Z.T s') (vcSym2 : VcSym2A st Z.V s') + (symL : SymL st H Z.Cs s') + (zwf : DelInvA st Z.th s') + (fhOK : ∀ t ∈ Z.T, NullOrEp H (capA st s' (slotIdx t layout.tcb.SLOT_FAULT_HANDLER))) + (ptCapIn : PtCapInA { Z with hw := hw } (L.mem s')) + (utWin : UtWinCapA st OS s') (devWin : DevWinCapA st OS s') (userFr : UserFrSpecA st OS s' σ') (frTh : FrThA st Z.th OS s') + (j5 : J5 { Z with hw := hw } H OS s' σ' m) + (j7 : J7A st Rj { Z with hw := hw } H OS s' σ' m) : + JStage3A st AR { Z with hw := hw } H OS U Ac' Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7A st Rj) s' a' σ' m := by + have hk := w.1 + have hc := w.2.1 + exact JStage3A.of_capVsGA (J := J) (hw := hw) (P := P) (S1' := Z.S1) (Zl' := Z.Lv) (T2' := Z.T2) (Sh2' := Z.Sh2) + (Tb' := Tb) (Lv' := Lv) (Ac' := Ac') (a' := a') (σc := σc) (σ' := σ') (s' := s') + (w := w) + (hP := hP) + (hbf := hbf) + (HS1 := HS1) + (hm1 := hm1) + (HS2 := HS2) + (hasid := hasid) + (hdp := hdp) + (hN := hN) + (jx := jx) + (sys := sys) + (vsOk' := vsOk') (bufVs' := bufVs') + (sq := sq) + (poolOff := poolOff) + (sinv := sinv) + (objAlloc := objAlloc) + (idleAnchor := idleAnchor) + (bufCover := bufCover) + (bufNative := bufNative) + (deadExt := deadExt) + (specFree := specFree) + (utBlk := utBlk) + (unmPt := unmPt) + (specZero := specZero) + (origLive := origLive) + (nullOrig := nullOrig) + (frBase := frBase) + (devFr := devFr) + (finCov := finCov) + (zombieNoVcpu := zombieNoVcpu) + (zombieTcb := zombieTcb) + (bufOff := bufOff) + (zombieBnd := zombieBnd) + (devUtOff := devUtOff) + (sidle := sidle) + (capTy := capTy) + (vcRun := vcRun) + (vcSym1 := vcSym1) + (vcSym2 := vcSym2) + (symL := symL) (zwf := zwf) (fhOK := fhOK) (ptCapIn := ptCapIn) (utWin := utWin) (devWin := devWin) (userFr := userFr) (frTh := frTh) + (j5 := j5) + (j7 := j7) + (ap' := by rw [show L.ksb s' = L.ksb s from hk]; exact { J.ap with }) + (s1' := J.s1) (ptH' := J.ptH) (setsInH' := { J.setsInH with }) + (vsVc' := by rw [show L.ksb s' = L.ksb s from hk]; exact J.vsVc) + (tabS1' := fun qq hq => + ⟨fun i hi h => (J.tabS1 qq hq).mem i hi + (footA_schedA (Z := Z) (Q := Z.Q) (Rl := Z.R) (curOf := Z.curOf) (curScOf := Z.curScOf) hk hc + (FootA.monoA (Z := { { Z with hw := hw } with S1 := ∅ }) rfl rfl (fun _ h => h) (fun _ h => h) + (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) h)), + (J.tabS1 qq hq).obj⟩) + (tabExt' := + ⟨fun qq hq i hi h => J.tabExt.mem qq hq i hi + (footA_schedA (Z := extZA Z) (Q := Z.Q) (Rl := Z.R) (curOf := Z.curOf) (curScOf := Z.curScOf) hk hc + (FootA.monoA (Z := extZA { Z with hw := hw }) rfl rfl (fun _ h => h) (fun _ h => h) + (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) h)), + J.tabExt.obj⟩) + +end main + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3CapVsFrameA.lean b/proofs/Fl4ProofsA64/P3/Stage3CapVsFrameA.lean new file mode 100644 index 000000000..840c8b1d9 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3CapVsFrameA.lean @@ -0,0 +1,360 @@ +/- +P3 (AArch64, merge line, D8 step 5 (ii), 1b): **`JStage3A.of_capVsFrameA`** — the in-place cap-write wrapper over the +whole-arm transport `JStage3A.of_capVsA` (`P3/Stage3CapVsA`). + +An arm that rewrites one cap slot in place (`MapAlikeA c c0`: a frame / page-table / Pml4 / Ept / high-VSpace cap whose +mapping data change) and then writes VSpace words (`VsSpecA`: leaf entries, a table zeroing, pool words) supplies only the +**post-state facts of the VSpace part** (`CapVsFinA`: the write set `P` with its `VsR` fact, `hbf`, the spec step, the +joint relation `JAtZAX` at the final state, `sys` / `vsOk` / `sq` / `poolOff`, the IPC-buffer facts, `unmPt`, `bufOff`, +`j7`). Everything that only *reads caps* (`objAlloc`, `idleAnchor`, `utBlk`, `deadExt`, `specFree`, `specZero`, +`frBase`, `devFr`, `zombie*`, `devUtOff`, `j5`, `nullOrig`, `origLive`, `finCov`, `sinv`) is derived here from +`MapAlikeA` at every `OS` slot, as `of_capFrame` does for the in-place class, plus small VSpace lifts of the spec facts +(`SInvXA.vsA`, `DeadExtA.vsA`, `SpecFreeA.vsA`, `FinCovA.vsA`, `specCapAtA_vsA`). + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3CapVsA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## VSpace lifts of the spec facts -/ + +section lifts +variable {σ σ' : Fl4SpecA64.State Unit} + +theorem FinCovA.vsA {th : TypedHeapA} (HS : VsSpecA σ σ') (h : FinCovA th σ) : FinCovA th σ' := + h.keepA (fun q r cs h => HS.back h (fun _ e => by cases e)) (fun q x h => ⟨x, HS.back h (fun _ e => by cases e)⟩) + +theorem SInvXA.vsA {m : Fl4SpecA64.McState} {n : Nat} (HS : VsSpecA σ σ') (S : SInvXA σ m n) : SInvXA σ' m n := by + have kT : ∀ p x, σ.kheap p = some (.tcb x) → σ'.kheap p = some (.tcb x) := fun p x h => + HS.keep h (fun _ e => by cases e) + have bT : ∀ p x, σ'.kheap p = some (.tcb x) → σ.kheap p = some (.tcb x) := fun p x h => + HS.back h (fun _ e => by cases e) + have kS : ∀ p x k, σ.kheap p = some (.schedContext x k) → σ'.kheap p = some (.schedContext x k) := + fun p x k h => HS.keep h (fun _ e => by cases e) + have bS : ∀ p x k, σ'.kheap p = some (.schedContext x k) → σ.kheap p = some (.schedContext x k) := + fun p x k h => HS.back h (fun _ e => by cases e) + have gl : SFixA σ σ' := by rw [HS.eq]; exact ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ + have rq : σ'.readyQueues = σ.readyQueues := by rw [HS.eq] + have act : σ'.schedulerAction = σ.schedulerAction := by rw [HS.eq] + have rl : σ'.releaseQueue = σ.releaseQueue := by rw [HS.eq] + have irqN : σ'.interruptIrqNode = σ.interruptIrqNode := by rw [HS.eq] + have capNone : ∀ p o b c, σ'.kheap p = some o → Fl4SpecA64.capOf o b = some c → σ.kheap p = some o := by + intro p o b c h e + refine HS.back h (fun x ex => ?_) + subst ex + cases e + have table : ∀ k p, Fl4SpecA64.capTableAt k p σ' ↔ Fl4SpecA64.capTableAt k p σ := fun k p => + ⟨fun ⟨cs, h, w⟩ => ⟨cs, HS.back h (fun _ e => by cases e), w⟩, + fun ⟨cs, h, w⟩ => ⟨cs, HS.keep h (fun _ e => by cases e), w⟩⟩ + have irq : SpecIrqA σ' := + { node := by + have h := S.irqN.node + unfold Fl4SpecA64.IrqNodeFl4 Fl4SpecA64.validIrqNodeFl4 Fl4SpecA64.irqNodeDomFl4 at h ⊢ + simp only [irqN, table]; exact h + slot := fun i hi o b c h e => + S.irqN.slot i hi o b c (by rw [irqN] at h; exact capNone _ _ _ _ h e) e + ref := fun p o b c h e => by + rw [irqN]; exact S.irqN.ref p o b c (capNone _ _ _ _ h e) e } + have ct : σ'.curThread = σ.curThread := gl.curThread + have it : σ'.idleThread = σ.idleThread := gl.idleThread + have cs : σ'.curSc = σ.curSc := gl.curSc + have CF := CurFactsA.back (σ := σ) (σ' := σ') ⟨S.zc, S.rlsRun, S.curScReady⟩ (fun t h => by rw [rl] at h; exact h) + (by rw [HS.eq]) cs (by rw [HS.eq]) (fun t x h => ⟨x, bT _ _ h, rfl⟩) (fun t x h _ => ⟨x, kT _ _ h, id⟩) + (fun y k h => ⟨y, bS _ _ _ h, fun a => ⟨a, id⟩⟩) + exact + { mc := S.mc + view := by show σ'.idleThread = _; rw [it]; exact S.view + curTcb := by + obtain ⟨x, hx⟩ := S.curTcb + exact ⟨x, by rw [ct]; exact kT _ _ hx⟩ + idleTcb := by + obtain ⟨x, hx, e⟩ := S.idleTcb + exact ⟨x, by rw [it]; exact kT _ _ hx, e⟩ + onlyIdle := fun t x h e => S.onlyIdle t x (bT _ _ h) e + idleSc := by + obtain ⟨x, y, k, h1, h2, h3, h4⟩ := S.idleSc + exact ⟨x, y, k, by rw [it]; exact kT _ _ h1, h2, kS _ _ _ h3, by rw [it]; exact h4⟩ + curSc := by + obtain ⟨y, k, h⟩ := S.curSc + exact ⟨y, k, by rw [cs]; exact kS _ _ _ h⟩ + ctQ := fun ha d p => by rw [ct, rq]; exact S.ctQ (act.symm.trans ha) d p + idleQ := fun d p c hcn => by rw [rq]; exact S.idleQ d p c hcn + rq := fun d p t h => by + rw [rq] at h + obtain ⟨x, hx, e⟩ := S.rq d p t h + exact ⟨x, kT _ _ hx, e⟩ + rls := fun t h => by + rw [rl] at h + obtain ⟨x, hx⟩ := S.rls t h + exact ⟨x, kT _ _ hx⟩ + rpos := fun p y k h e => S.rpos p y k (bS _ _ _ h) e + zc := CF.1 + rlsRun := CF.2.1 + curScReady := CF.2.2 + faultSt := fun p x h f => S.faultSt p x (bT _ _ h) f + irqN := irq } + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +theorem DeadExtA.vsA {Z : XSetsA} {OS Rp : Set U64} {stack : U64 → List U64} {s : S} (HS : VsSpecA σ σ') + (h : DeadExtA st Z OS Rp stack s σ) : DeadExtA st Z OS Rp stack s σ' where + vcpu := fun v hv hd x hx => by + refine h.vcpu v hv hd x ?_ + rcases HS.kh (specPtr v) with e | ⟨_, a2, -, e2, -, nv⟩ + · rw [← e]; exact hx + · rw [hx] at e2; cases e2; exact absurd rfl (nv x) + reply := h.reply + asid := fun i p hh hd => h.asid i p (HS.asid _ _ hh) hd + tvcpu := h.tvcpu + vcDeadRun := h.vcDeadRun + replyTcb := fun r hr hd x hx => by + obtain ⟨y, hy, k⟩ := (QKeepA.of_vsA HS).rt _ x hx + exact k (h.replyTcb r hr hd y hy) + +theorem SpecFreeA.vsA {OS : Set U64} {s : S} (HS : VsSpecA σ σ') (h : SpecFreeA st OS s σ) : + SpecFreeA st OS s σ' where + heap := fun p hf => HS.none (h.heap p hf) + cdt := fun x y hx p hf => h.cdt x y (by have e : σ'.cdt = σ.cdt := by rw [HS.eq] + rw [e] at hx; exact hx) p hf + orig := fun x hx p hf => h.orig x (by have e : σ'.isOriginalCap = σ.isOriginalCap := by rw [HS.eq] + rw [e] at hx; exact hx) p hf + +end +end lifts + +/-! ## The wrapper -/ + +section main +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +variable (st) in +/-- **What an in-place cap-and-VSpace arm supplies at the final state** (module docstring): the VSpace part after the +cap write `s2`. -/ +structure CapVsFinA (AR : A → HwRootA → Prop) (Z : XSetsA) (H : Heap) (OS U Ac Pp Tb : Set U64) (Lv : U64 → Nat) + (ia ie : Nat) (Bf : Set (U64 × Fl4SpecA64.Word)) (Rj : Set U64 → JSlotA S) (s s2 s' : S) (a' : A) + (σ2 σ' : Fl4SpecA64.State Unit) (m : Fl4SpecA64.McState) (hw : HwRootA) (P : U64 → Prop) : Prop where + w : WritesIn st s2 s' P + hP : ∀ Pt Ub : Set U64, VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt → + (∀ p, SchedPageA st Z OS s p → p ∈ Pt) → ∀ b, P b → VsR (L.ksb s) Pt b + hbf : ∀ b, P b → ¬ BufW Bf b + HS : VsSpecA σ2 σ' + hasid : σ'.archState.armAsidTable = σ2.archState.armAsidTable + hdp : ∀ p dev pg, σ2.kheap p = some (.archObj (.dataPage dev pg)) → σ'.kheap p = some (.archObj (.dataPage dev pg)) + jx : JAtZAX (L := L) AR { Z with hw := hw } s' a' σ' m + sys : SysInvA st H Z.Cs OS U Ac Pp Tb Lv ia ie s' + vsOk : UtVsOkA st H OS Z.V Pp Tb Lv s' + bufVs : ∀ b, BufW Bf b → ¬ VsR (L.ksb s') (utVsPtA st Pp Tb Lv s') b + sq : SchedQueuesInv st H.tcbs Z.Cs H.omega H s' Z.Q Z.R + poolOff : PoolOff st H Z.Cs OS s' + bufCover : BufCover st Z.T Bf s' σ' + bufNative : BufNativeA Z.T (L.mem s') + unmPt : UnmPtSpecA st { Z with hw := hw } m OS s' σ' + bufOff : BufOffA st { Z with hw := hw } OS s' Bf + j7 : J7A st Rj { Z with hw := hw } H OS s' σ' m + symL : SymL st H Z.Cs s' + +set_option maxHeartbeats 4000000 in +/-- **`JStage3A` across an in-place cap write followed by VSpace word writes** (module docstring). -/ +theorem JStage3A.of_capVsFrameA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {Q : S → A → Fl4SpecA64.State Unit → HwRootA → Prop} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {Rj : Set U64 → JSlotA S} {s : S} {a : A} + {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st Rj) s a σ m) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hoff : ∀ b, FootNA (L := L) Z s b → ¬ W7.CapW H.tcbs {b | InOS OS b} ∅ b) + (hal : ∀ b, FootNA (L := L) Z s b → Aligned b) + (cte : Fl4SpecA64.CslotPtr) (hab : AbsSlotA Z.th cte) {c : capability.Cap} {sc : Fl4SpecA64.Cap} + (hEnc : CapRt.EncWf c) (hca : CapRelA c sc) (hT : TreeA st OS Z.G.chains Z.G.par s) + (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) (hcn : c ≠ .Null) + (hc0 : capA st s (cslotAddrA cte) ≠ .Null) (hM : MapAlikeA c (capA st s (cslotAddrA cte))) + (hok : CapObjOkA Z c) (hirq : CapOkA σ.interruptIrqNode cte sc) (hsci : NoIdleCapV m n sc) + (hpt : ∀ p0 a0' v0', c ≠ .PageTable p0 true a0' v0') + (arm : ∀ s2 σ2, obj.Slot.set_cap st (cslotAddrA cte) s c = ok s2 → ((), σ2) ∈ (setCap (Z := Unit) sc cte σ).1 → + SpecSlotA σ σ2 → WritesIn st s s2 (fun b => W7.CapW H.tcbs {b | InOS OS b} ∅ b) → + JAtZAX (L := L) AR Z s2 a σ2 m → capA st s2 (cslotAddrA cte) = c → + ∃ (s' : S) (a' : A) (σ' : Fl4SpecA64.State Unit) (hw : HwRootA) (P : U64 → Prop) (Ac' : Set U64), + CapVsFinA st AR Z H OS U Ac' Pp Tb Lv ia ie Bf Rj s s2 s' a' σ2 σ' m hw P ∧ Q s' a' σ' hw) : + ∃ (s2 : S) (σ2 : Fl4SpecA64.State Unit) (s' : S) (a' : A) (σ' : Fl4SpecA64.State Unit) (hw : HwRootA) + (P : U64 → Prop) (Ac' : Set U64), + obj.Slot.set_cap st (cslotAddrA cte) s c = ok s2 ∧ ((), σ2) ∈ (setCap (Z := Unit) sc cte σ).1 ∧ + SpecSlotA σ σ2 ∧ VsSpecA σ2 σ' ∧ CapVsSpecA σ σ' ∧ + WritesIn st s s' (fun b => W7.CapW H.tcbs {b | InOS OS b} ∅ b ∨ P b) ∧ + JStage3A st AR { Z with hw := hw } H OS U Ac' Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st Rj) s' a' σ' m ∧ + Q s' a' σ' hw := by + have hO : OSOK H.tcbs H.omega H.frame OS := J.sys.ent.os + have hH := J.sys.ent.heap + obtain ⟨s2, σ2, hs2, hσ2, HS, w, jx, -⟩ := + J.j.of_setcapA hO hsub hoff hal cte hab hEnc hca hT hnul hcn hc0 hM.ua hok + have hx : cslotAddrA cte ∈ OS := hsub _ (hab.isSlot J.j.1.place) + obtain ⟨hcx, hco⟩ := capA_setCapA hO hx hEnc hs2 + have hMs : ∀ u ∈ OS, MapAlikeA (capA st s2 u) (capA st s u) := fun u hu => by + by_cases e : u = cslotAddrA cte + · subst e; rw [hcx]; exact hM + · rw [hco u hu e]; exact .refl _ + obtain ⟨hcdt, horig, hms⟩ := setCap_keepsA hσ2 + obtain ⟨s', a', σ', hw, P, Ac', F, hQ⟩ := arm s2 σ2 hs2 hσ2 HS w jx hcx + obtain ⟨Pt, Ub, vr, -, -, -, -, -, -, link, -, -⟩ := J.j7 + obtain ⟨Qb, Rb, q⟩ := J.sys.ent.bxi.1 + have V : ∀ b, P b → VsR (L.ksb s) Pt b := F.hP Pt Ub vr link + have wT : WritesIn st s s' (fun b => W7.CapW H.tcbs {b | InOS OS b} ∅ b ∨ P b) := w.trans F.w + have offOS : ∀ b, P b → ¬ InOS OS b := fun b hb => vr.os b (V b hb) + -- the OS slot words after the VSpace writes + have osk2 : OSKeep st OS s2 s' := by + intro y hy + have al0 := hO.al0 hy + have hfit := hO.fit y hy + have key : ∀ o : U64, o.val < 32 → o.val % 8 = 0 → L.mem s' (wa y o) = L.mem s2 (wa y o) := by + intro o o1 o2 + have hv : (wa y o).val = y.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact F.w.mem_eq (hO.al hy o1 o2) (fun h => offOS _ h ⟨y, hy, by omega, by omega⟩) + have e0 : wa y 0#u64 = y := by apply UScalar.eq_of_val_eq; rw [wa_valm]; simp + exact ⟨by have := key 0#u64 (by simp) (by simp); rwa [e0] at this, key 8#u64 (by simp) (by simp), + key 16#u64 (by simp) (by simp), key 24#u64 (by simp) (by simp)⟩ + have capE2 : ∀ u ∈ OS, capA st s' u = capA st s2 u := fun u hu => by + unfold capA; rw [(osk2 u hu).1, (osk2 u hu).2.1] + have hMs' : ∀ u ∈ OS, MapAlikeA (capA st s' u) (capA st s u) := fun u hu => by + rw [capE2 u hu]; exact hMs u hu + -- the link words + have wA : WritesIn st s s2 (fun b => Aligned b ∧ W7.CapW H.tcbs {b | InOS OS b} ∅ b) := writesIn_alAG w + have hB : ∀ b, (Aligned b ∧ W7.CapW H.tcbs {b | InOS OS b} ∅ b) → W7.CapW H.tcbs H.omega H.frame b := + fun b hb => os_capw hO hb.1 hb.2 + obtain ⟨blk, -, -, -, -, fld, nod, -⟩ := W7.blk_of_capw (BXI.blk J.sys.ent.bxi) wA hB + have twH : ∀ t ∈ H.tcbs, L.mem s2 (wa t 3664#u64) = L.mem s (wa t 3664#u64) := fun t ht => + fld t ht 3664#u64 (by simp) (by simp) (by simp) + obtain ⟨w0, w1, hen, -⟩ := CapRt.decode_encode _ hEnc + have es2 : L.wr (L.wr s (cslotAddrA cte) w0) (wa (cslotAddrA cte) 8#u64) w1 = s2 := + okinj ((set_cap_eq (st := st) (s := s) (x := cslotAddrA cte) hen).symm.trans hs2) + have hl2 : ∀ u ∈ OS, L.mem s2 (wa u 16#u64) = L.mem s (wa u 16#u64) ∧ L.mem s2 (wa u 24#u64) = L.mem s (wa u 24#u64) := + fun u hu => by rw [← es2]; exact set_cap_links hO hx u hu + have hl : ∀ u ∈ OS, L.mem s' (wa u 16#u64) = L.mem s (wa u 16#u64) ∧ L.mem s' (wa u 24#u64) = L.mem s (wa u 24#u64) := + fun u hu => ⟨((osk2 u hu).2.2.1).trans (hl2 u hu).1, ((osk2 u hu).2.2.2).trans (hl2 u hu).2⟩ + have hTr : TreeA st OS Z.G.chains Z.G.par s := + J.j.1.cdt.tree.grow J.j5.2.1 (fun x hx hn => J.j5.2.2 x hx hn) + have mem : ∀ y ∈ Z.G.chains.flatten, y ∈ OS := fun y hy => ((hTr.lk.members y).1 hy).1 + have irqC : ∀ y ∈ Z.G.chains.flatten, ∀ i, capA st s' y = .IrqHandler i ↔ capA st s y = .IrqHandler i := + fun y hy i => ⟨(hMs' y (mem y hy)).back_eq (e := .IrqHandler i) trivial, + (hMs' y (mem y hy)).fwd_eq (e := .IrqHandler i) trivial⟩ + have j5 : J5A st { Z with hw := hw } H OS s' σ' m := + ⟨⟨MdbInvA.mapA hTr.lk hTr.tree J.j5.1.1 hMs' hl, IrqRunA.keepIrq J.j5.1.2.1 irqC, + HandlersActiveA.keepIrq J.j5.1.2.2 (fun y hy i h => (irqC y hy i).1 h) wT.1 (fun i hi => by + have eI : irqStAddrA (L.ksb s) i = irqWordA (L.ksb s) i := rfl + rw [eI] + exact wT.mem_eq (hal _ (.irq i hi)) (fun hp => hp.elim (hoff _ (.irq i hi)) (fun hp => + vr.ks _ (V _ hp) (irqOffA i) (by rw [irqOffA_val hi]; omega) rfl)))⟩, J.j5.2.1, + fun y hy hn => by + have ne : y ≠ cslotAddrA cte := fun e => hn (e ▸ hab.isSlot J.j.1.place) + rw [capE2 y hy, hco y hy ne]; exact J.j5.2.2 y hy hn⟩ + -- the TCB VCPU words + have twH' : ∀ t ∈ H.tcbs, L.mem s' (wa t 3664#u64) = L.mem s (wa t 3664#u64) := fun t ht => by + have ht' : H t = some .tcb := ht + have hv := wa_val_tcbAG hH ht' (K := 3664#u64) (by simp) + have h1 := hH.align t _ ht' + simp only [HKind.bits] at h1 + have hal3 : Aligned (wa t 3664#u64) := by + unfold Aligned; rw [hv]; simp; omega + rw [F.w.mem_eq hal3 (fun hp => by + have := (q.omega _ (vr.om _ (V _ hp))).2.1 t ht + have hv2 : (wa t 3664#u64).val = t.val + 3664 := by rw [hv]; simp + omega)] + exact twH t ht + have tw : ∀ t ∈ Z.T, L.mem s' (wa t 3664#u64) = L.mem s (wa t 3664#u64) := fun t ht => + twH' t (by rw [J.tcbs]; exact ht) + have twR : ∀ t ∈ Z.T, L.mem s' (runVcpuA t) = L.mem s (runVcpuA t) := fun t htZ => by + have ht : t ∈ H.tcbs := by rw [J.tcbs]; exact htZ + have ht' : H t = some .tcb := ht + have hv := wa_val_tcbAG hH ht' (K := 3672#u64) (by simp) + have h1 := hH.align t _ ht' + simp only [HKind.bits] at h1 + have hal3 : Aligned (wa t 3672#u64) := by + unfold Aligned; rw [hv]; simp; omega + unfold runVcpuA + rw [F.w.mem_eq hal3 (fun hp => by + have := (q.omega _ (vr.om _ (V _ hp))).2.1 t ht + have hv2 : (wa t 3672#u64).val = t.val + 3672 := by rw [hv]; simp + omega)] + exact fld t ht 3672#u64 (by simp) (by simp) (by simp) + -- the spec side + have W := setCap_wfA sc cte σ ((), σ2) hσ2 + have um2 : σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory := by + have e : σ'.machineState.underlyingMemory = σ2.machineState.underlyingMemory := by rw [F.HS.eq] + rw [e, hms] + have horig' : σ'.isOriginalCap = σ.isOriginalCap := by + have e : σ'.isOriginalCap = σ2.isOriginalCap := by rw [F.HS.eq] + rw [e, horig] + have hcdt' : σ'.cdt = σ.cdt := by + have e : σ'.cdt = σ2.cdt := by rw [F.HS.eq] + rw [e, hcdt] + have hdp' : ∀ p dev pg, σ.kheap p = some (.archObj (.dataPage dev pg)) → + σ'.kheap p = some (.archObj (.dataPage dev pg)) := + fun p dev pg h => F.hdp p dev pg (HS.keep h (fun _ _ e => by cases e) (fun _ e => by cases e)) + have sinv : SInvXA σ' m n := + (J.sinv.of_fr (setCap_ndr sc cte σ ((), σ2) hσ2) + (setCap_irq hirq σ rfl J.sinv.irqN ((), σ2) hσ2).2).vsA F.HS + have nul : ∀ sl, specCapAtA σ' sl = some .nullCap → σ'.isOriginalCap sl = false := by + have n2 := nullOrig_setCapA hσ2 (CapRelA.ne_null hca hcn) HS W horig J.nullOrig + intro sl h + rw [horig'] + have h2 := n2 sl (by rw [← specCapAtA_vsA F.HS sl]; exact h) + rw [← horig]; exact h2 + have hms' : σ2.machineState = σ.machineState := hms + have zwf2 : DelInvA st Z.th s' := fun y hy p0 zt k h => + J.zwf y hy p0 zt k ((hMs' y (hsub y hy)).back_eq (e := .Zombie p0 zt k) trivial h) + have kfh : ∀ t ∈ Z.T, slotIdx t layout.tcb.SLOT_FAULT_HANDLER ∈ OS := fun t ht => + hsub _ (isSlot_fhA ((J.sr.thT t).2 ht)) + have fh2 : ∀ t ∈ Z.T, NullOrEp H (capA st s' (slotIdx t layout.tcb.SLOT_FAULT_HANDLER)) := fun t ht => by + rcases J.fhOK t ht with e | ⟨ep, b0, b1, b2, b3, b4, e, he⟩ + · exact Or.inl ((hMs' _ (kfh t ht)).fwd_eq (e := .Null) trivial e) + · exact Or.inr ⟨ep, b0, b1, b2, b3, b4, + (hMs' _ (kfh t ht)).fwd_eq (e := .Endpoint ep b0 b1 b2 b3 b4) trivial e, he⟩ + have pt2 : PtCapInA { Z with hw := hw } (L.mem s') := fun y hy p0 a0' v0' h => by + change capA st s' y = _ at h + rw [capE2 y (hsub y hy)] at h + by_cases e : y = cslotAddrA cte + · rw [e, hcx] at h + exact absurd h (hpt p0 a0' v0') + · rw [hco y (hsub y hy) e] at h + exact J.ptCapIn y hy p0 a0' v0' h + have fin := JStage3A.of_capVsA (symL := F.symL) (zwf := zwf2) (fhOK := fh2) (ptCapIn := pt2) (J := J) (hw := hw) (P := P) (w := wT) F.hP F.hbf HS hms' F.HS F.hasid hdp' hoff + F.jx F.sys F.vsOk F.bufVs F.sq F.poolOff sinv + (ObjAllocA.mapA hMs' (fun _ _ h => h) J.objAlloc) (IdleAnchorOffA.mapA hMs' J.idleAnchor) + F.bufCover F.bufNative + (DeadExtA.mapA (Z := Z) (Z' := { Z with hw := hw }) hMs' HS rfl rfl rfl tw + (fun v hv => wT.mem_eq (hal _ (.vcpu v _ hv (Or.inr rfl))) (fun hp => hp.elim + (hoff _ (.vcpu v _ hv (Or.inr rfl))) (fun hp => vr.vc _ (V _ hp) ⟨v, hv, 24#u64, by simp, by simp, rfl⟩))) J.deadExt |>.vsA F.HS) + ((SpecFreeA.mapA hMs' HS hcdt horig J.specFree).vsA F.HS) (UtBlk39A.mapA hMs' J.utBlk) F.unmPt + (SpecZeroFreeA.mapA hMs' um2 J.specZero) (fun sl h => J.origLive sl (by rw [← horig']; exact h)) nul + (FrBaseApartA.mapA hMs' J.frBase) (DevFrDisjA.mapA hMs' hdp' J.devFr) + ((FinCovA.ofWfA J.finCov HS W).vsA F.HS) + (ZombieTcbNoVcpuA.mapA hMs' J.zombieTcb twH' J.zombieNoVcpu) (ZombieTcbTA.mapA hMs' J.zombieTcb) F.bufOff + (UtZombieA.mapA hMs' J.zombieBnd) + (DevUtOffA.mapA (Z := Z) (Z' := { Z with hw := hw }) hMs' wT.1 (fun _ _ h => h) rfl J.devUtOff) + ((J.sidle.slotA HS (fun sl a h => (specCapAtA_setCapA hσ2 HS W sl a h).imp_right fun (e : a = sc) => by rw [e]; exact hsci) + (IdleSlotNewA.of_inplaceA J.sidle J.j.1.st hab hc0 hσ2 HS W)).vsA F.HS) + (CapTyA.mapA hMs' J.capTy) + (J.vcRun.keepA tw (fun v hv => wT.mem_eq (hal _ (.vcpu v _ hv (Or.inr rfl))) (fun hp => hp.elim + (hoff _ (.vcpu v _ hv (Or.inr rfl))) (fun hp => vr.vc _ (V _ hp) ⟨v, hv, 24#u64, by simp, by simp, rfl⟩))) + (fun v hv => wT.mem_eq (hal _ (.vcpu v _ hv (Or.inl rfl))) (fun hp => hp.elim + (hoff _ (.vcpu v _ hv (Or.inl rfl))) (fun hp => vr.vc _ (V _ hp) ⟨v, hv, 0#u64, by simp, by simp, (wa_zeroVcA v).symm⟩))) + twR) + (J.vcSym1.keepA J.vcRun tw fun v hv => wT.mem_eq (hal _ (.vcpu v _ hv (Or.inl rfl))) (fun hp => hp.elim + (hoff _ (.vcpu v _ hv (Or.inl rfl))) (fun hp => vr.vc _ (V _ hp) ⟨v, hv, 0#u64, by simp, by simp, (wa_zeroVcA v).symm⟩))) + (J.vcSym2.keepA J.vcRun (fun v hv => wT.mem_eq (hal _ (.vcpu v _ hv (Or.inr rfl))) (fun hp => hp.elim + (hoff _ (.vcpu v _ hv (Or.inr rfl))) (fun hp => vr.vc _ (V _ hp) ⟨v, hv, 24#u64, by simp, by simp, rfl⟩))) twR) + (UtWinCapA.mapA hMs' J.utWin) (DevWinCapA.mapA hMs' J.devWin) (UserFrSpecA.mapA hMs' hdp' J.userFr) (FrThA.mapA hMs' (fun _ _ e => e) J.frTh) + j5 F.j7 + exact ⟨s2, σ2, s', a', σ', hw, P, Ac', hs2, hσ2, HS, F.HS, CapVsSpecA.of_steps HS F.HS F.hasid hms', wT, fin, hQ⟩ + +end main + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3CapVsFrameGA.lean b/proofs/Fl4ProofsA64/P3/Stage3CapVsFrameGA.lean new file mode 100644 index 000000000..b85ba3216 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3CapVsFrameGA.lean @@ -0,0 +1,286 @@ +/- +P3 (AArch64, merge line, D8 step 5 (ii)): **`JStage3A.of_capVsFrameGA`** — `of_capVsFrameA` (`P3/Stage3CapVsFrameA`) +over the two-way ghost update `JStage3A.of_capVsGA` (`P3/Stage3CapVsA`): the arm fixes new relation ghosts `S1'`, +`Zl'`, `T2'`, `Sh2'` and record tables `Tb'` / depths `Lv'`, and supplies (`CapVsFinGA`) what `CapVsFinA` does at the +new ghosts, the ghost-reading fields `ap'`, `s1'`, `ptH'`, `setsInH'`, `vsVc'`, `tabS1'`, `tabExt'`, and the transfers +of the cap-derived fields `objAlloc`, `deadExt`, `devUtOff`, `j5` from `{ Z with hw }` to the new ghosts. +`CapVsFinGA.keepGA` gives the ghost fields and the identity transfers when the relation ghosts are kept (only `Tb` / `Lv` +change, e.g. ASID assign). + +Generated by `handoff/tools/ptu/gen_ga.py` from `of_capVsFrameA`. No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3CapVsFrameA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section main +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +variable (st) in +/-- **What an in-place cap-and-VSpace arm supplies at the final state** (module docstring): the VSpace part after the +cap write `s2`. -/ +structure CapVsFinGA (AR : A → HwRootA → Prop) (Z : XSetsA) (H : Heap) (OS U Ac Pp Tb : Set U64) (Lv : U64 → Nat) + (ia ie : Nat) (Bf : Set (U64 × Fl4SpecA64.Word)) (Rj : Set U64 → JSlotA S) (s s2 s' : S) (a' : A) + (σ2 σ' : Fl4SpecA64.State Unit) (m : Fl4SpecA64.McState) (hw : HwRootA) (P : U64 → Prop) (S1' T2' : Set U64) (Zl' Sh2' : U64 → Nat) + (Rp : Set U64) (stack : U64 → List U64) : Prop where + w : WritesIn st s2 s' P + hP : ∀ Pt Ub : Set U64, VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt → + (∀ p, SchedPageA st Z OS s p → p ∈ Pt) → ∀ b, P b → VsR (L.ksb s) Pt b + hbf : ∀ b, P b → ¬ BufW Bf b + HS : VsSpecA σ2 σ' + hasid : σ'.archState.armAsidTable = σ2.archState.armAsidTable + hdp : ∀ p dev pg, σ2.kheap p = some (.archObj (.dataPage dev pg)) → σ'.kheap p = some (.archObj (.dataPage dev pg)) + jx : JAtZAX (L := L) AR { Z with hw := hw, S1 := S1', Lv := Zl', T2 := T2', Sh2 := Sh2' } s' a' σ' m + sys : SysInvA st H Z.Cs OS U Ac Pp Tb Lv ia ie s' + vsOk : UtVsOkA st H OS Z.V Pp Tb Lv s' + bufVs : ∀ b, BufW Bf b → ¬ VsR (L.ksb s') (utVsPtA st Pp Tb Lv s') b + sq : SchedQueuesInv st H.tcbs Z.Cs H.omega H s' Z.Q Z.R + poolOff : PoolOff st H Z.Cs OS s' + bufCover : BufCover st Z.T Bf s' σ' + bufNative : BufNativeA Z.T (L.mem s') + unmPt : UnmPtSpecA st { Z with hw := hw, S1 := S1', Lv := Zl', T2 := T2', Sh2 := Sh2' } m OS s' σ' + bufOff : BufOffA st { Z with hw := hw, S1 := S1', Lv := Zl', T2 := T2', Sh2 := Sh2' } OS s' Bf + j7 : J7A st Rj { Z with hw := hw, S1 := S1', Lv := Zl', T2 := T2', Sh2 := Sh2' } H OS s' σ' m + objT : ObjAllocA (st := st) { Z with hw := hw } H OS s' → ObjAllocA (st := st) { Z with hw := hw, S1 := S1', Lv := Zl', T2 := T2', Sh2 := Sh2' } H OS s' + deadT : DeadExtA st { Z with hw := hw } OS Rp stack s' σ' → DeadExtA st { Z with hw := hw, S1 := S1', Lv := Zl', T2 := T2', Sh2 := Sh2' } OS Rp stack s' σ' + devT : DevUtOffA st { Z with hw := hw } H OS ia ie s' → DevUtOffA st { Z with hw := hw, S1 := S1', Lv := Zl', T2 := T2', Sh2 := Sh2' } H OS ia ie s' + j5T : J5A st { Z with hw := hw } H OS s' σ' m → J5A st { Z with hw := hw, S1 := S1', Lv := Zl', T2 := T2', Sh2 := Sh2' } H OS s' σ' m + ap' : SlotApA { Z with hw := hw, S1 := S1', Lv := Zl', T2 := T2', Sh2 := Sh2' } H OS (L.ksb s') + s1' : S1' ⊆ Tb + ptH' : PtHA H (Z.Pp ∪ S1') + setsInH' : SetsInHA { Z with hw := hw, S1 := S1', Lv := Zl', T2 := T2', Sh2 := Sh2' } H + vsVc' : ∀ b, VsFootA Z.Pp S1' Z.Y (L.ksb s') b → ¬ VcW Z.V b + tabS1' : ∀ q ∈ S1', TabOffA (L := L) { Z with hw := hw, S1 := S1', Lv := Zl', T2 := T2', Sh2 := Sh2' } m s' q + tabExt' : ExtTabOffA (L := L) { Z with hw := hw, S1 := S1', Lv := Zl', T2 := T2', Sh2 := Sh2' } m s' + symL : SymL st H Z.Cs s' + +set_option maxHeartbeats 4000000 in +/-- **`JStage3A` across an in-place cap write followed by VSpace word writes** (module docstring). -/ +theorem JStage3A.of_capVsFrameGA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {S1' T2' Tb' : Set U64} {Zl' Sh2' Lv' : U64 → Nat} + {Lv : U64 → Nat} {ia ie : Nat} {Q : S → A → Fl4SpecA64.State Unit → HwRootA → Prop} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {Rj : Set U64 → JSlotA S} {s : S} {a : A} + {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st Rj) s a σ m) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hoff : ∀ b, FootNA (L := L) Z s b → ¬ W7.CapW H.tcbs {b | InOS OS b} ∅ b) + (hal : ∀ b, FootNA (L := L) Z s b → Aligned b) + (cte : Fl4SpecA64.CslotPtr) (hab : AbsSlotA Z.th cte) {c : capability.Cap} {sc : Fl4SpecA64.Cap} + (hEnc : CapRt.EncWf c) (hca : CapRelA c sc) (hT : TreeA st OS Z.G.chains Z.G.par s) + (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) (hcn : c ≠ .Null) + (hc0 : capA st s (cslotAddrA cte) ≠ .Null) (hM : MapAlikeA c (capA st s (cslotAddrA cte))) + (hok : CapObjOkA Z c) (hirq : CapOkA σ.interruptIrqNode cte sc) (hsci : NoIdleCapV m n sc) + (hS : ∀ p0, p0 ∈ Z.S1 ∧ 1 ≤ Z.Lv p0 → p0 ∈ S1' ∧ 1 ≤ Zl' p0) (hT2 : ∀ p0 ∈ Z.T2, p0 ∈ T2') + (hpt : ∀ p0 a0' v0', c = .PageTable p0 true a0' v0' → (p0 ∈ S1' ∧ 1 ≤ Zl' p0) ∨ p0 ∈ T2') + (arm : ∀ s2 σ2, obj.Slot.set_cap st (cslotAddrA cte) s c = ok s2 → ((), σ2) ∈ (setCap (Z := Unit) sc cte σ).1 → + SpecSlotA σ σ2 → WritesIn st s s2 (fun b => W7.CapW H.tcbs {b | InOS OS b} ∅ b) → + JAtZAX (L := L) AR Z s2 a σ2 m → capA st s2 (cslotAddrA cte) = c → + ∃ (s' : S) (a' : A) (σ' : Fl4SpecA64.State Unit) (hw : HwRootA) (P : U64 → Prop) (Ac' : Set U64), + CapVsFinGA st AR Z H OS U Ac' Pp Tb' Lv' ia ie Bf Rj s s2 s' a' σ2 σ' m hw P S1' T2' Zl' Sh2' Rp stack ∧ + Q s' a' σ' hw) : + ∃ (s2 : S) (σ2 : Fl4SpecA64.State Unit) (s' : S) (a' : A) (σ' : Fl4SpecA64.State Unit) (hw : HwRootA) + (P : U64 → Prop) (Ac' : Set U64), + obj.Slot.set_cap st (cslotAddrA cte) s c = ok s2 ∧ ((), σ2) ∈ (setCap (Z := Unit) sc cte σ).1 ∧ + SpecSlotA σ σ2 ∧ VsSpecA σ2 σ' ∧ CapVsSpecA σ σ' ∧ + WritesIn st s s' (fun b => W7.CapW H.tcbs {b | InOS OS b} ∅ b ∨ P b) ∧ + JStage3A st AR { Z with hw := hw, S1 := S1', Lv := Zl', T2 := T2', Sh2 := Sh2' } H OS U Ac' Pp Tb' Lv' ia ie N E Rp Bf stack κ n (J5A st) (J7A st Rj) s' + a' σ' m ∧ Q s' a' σ' hw := by + have hO : OSOK H.tcbs H.omega H.frame OS := J.sys.ent.os + have hH := J.sys.ent.heap + obtain ⟨s2, σ2, hs2, hσ2, HS, w, jx, -⟩ := + J.j.of_setcapA hO hsub hoff hal cte hab hEnc hca hT hnul hcn hc0 hM.ua hok + have hx : cslotAddrA cte ∈ OS := hsub _ (hab.isSlot J.j.1.place) + obtain ⟨hcx, hco⟩ := capA_setCapA hO hx hEnc hs2 + have hMs : ∀ u ∈ OS, MapAlikeA (capA st s2 u) (capA st s u) := fun u hu => by + by_cases e : u = cslotAddrA cte + · subst e; rw [hcx]; exact hM + · rw [hco u hu e]; exact .refl _ + obtain ⟨hcdt, horig, hms⟩ := setCap_keepsA hσ2 + obtain ⟨s', a', σ', hw, P, Ac', F, hQ⟩ := arm s2 σ2 hs2 hσ2 HS w jx hcx + obtain ⟨Pt, Ub, vr, -, -, -, -, -, -, link, -, -⟩ := J.j7 + obtain ⟨Qb, Rb, q⟩ := J.sys.ent.bxi.1 + have V : ∀ b, P b → VsR (L.ksb s) Pt b := F.hP Pt Ub vr link + have wT : WritesIn st s s' (fun b => W7.CapW H.tcbs {b | InOS OS b} ∅ b ∨ P b) := w.trans F.w + have offOS : ∀ b, P b → ¬ InOS OS b := fun b hb => vr.os b (V b hb) + -- the OS slot words after the VSpace writes + have osk2 : OSKeep st OS s2 s' := by + intro y hy + have al0 := hO.al0 hy + have hfit := hO.fit y hy + have key : ∀ o : U64, o.val < 32 → o.val % 8 = 0 → L.mem s' (wa y o) = L.mem s2 (wa y o) := by + intro o o1 o2 + have hv : (wa y o).val = y.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact F.w.mem_eq (hO.al hy o1 o2) (fun h => offOS _ h ⟨y, hy, by omega, by omega⟩) + have e0 : wa y 0#u64 = y := by apply UScalar.eq_of_val_eq; rw [wa_valm]; simp + exact ⟨by have := key 0#u64 (by simp) (by simp); rwa [e0] at this, key 8#u64 (by simp) (by simp), + key 16#u64 (by simp) (by simp), key 24#u64 (by simp) (by simp)⟩ + have capE2 : ∀ u ∈ OS, capA st s' u = capA st s2 u := fun u hu => by + unfold capA; rw [(osk2 u hu).1, (osk2 u hu).2.1] + have hMs' : ∀ u ∈ OS, MapAlikeA (capA st s' u) (capA st s u) := fun u hu => by + rw [capE2 u hu]; exact hMs u hu + -- the link words + have wA : WritesIn st s s2 (fun b => Aligned b ∧ W7.CapW H.tcbs {b | InOS OS b} ∅ b) := writesIn_alAG w + have hB : ∀ b, (Aligned b ∧ W7.CapW H.tcbs {b | InOS OS b} ∅ b) → W7.CapW H.tcbs H.omega H.frame b := + fun b hb => os_capw hO hb.1 hb.2 + obtain ⟨blk, -, -, -, -, fld, nod, -⟩ := W7.blk_of_capw (BXI.blk J.sys.ent.bxi) wA hB + have twH : ∀ t ∈ H.tcbs, L.mem s2 (wa t 3664#u64) = L.mem s (wa t 3664#u64) := fun t ht => + fld t ht 3664#u64 (by simp) (by simp) (by simp) + obtain ⟨w0, w1, hen, -⟩ := CapRt.decode_encode _ hEnc + have es2 : L.wr (L.wr s (cslotAddrA cte) w0) (wa (cslotAddrA cte) 8#u64) w1 = s2 := + okinj ((set_cap_eq (st := st) (s := s) (x := cslotAddrA cte) hen).symm.trans hs2) + have hl2 : ∀ u ∈ OS, L.mem s2 (wa u 16#u64) = L.mem s (wa u 16#u64) ∧ L.mem s2 (wa u 24#u64) = L.mem s (wa u 24#u64) := + fun u hu => by rw [← es2]; exact set_cap_links hO hx u hu + have hl : ∀ u ∈ OS, L.mem s' (wa u 16#u64) = L.mem s (wa u 16#u64) ∧ L.mem s' (wa u 24#u64) = L.mem s (wa u 24#u64) := + fun u hu => ⟨((osk2 u hu).2.2.1).trans (hl2 u hu).1, ((osk2 u hu).2.2.2).trans (hl2 u hu).2⟩ + have hTr : TreeA st OS Z.G.chains Z.G.par s := + J.j.1.cdt.tree.grow J.j5.2.1 (fun x hx hn => J.j5.2.2 x hx hn) + have mem : ∀ y ∈ Z.G.chains.flatten, y ∈ OS := fun y hy => ((hTr.lk.members y).1 hy).1 + have irqC : ∀ y ∈ Z.G.chains.flatten, ∀ i, capA st s' y = .IrqHandler i ↔ capA st s y = .IrqHandler i := + fun y hy i => ⟨(hMs' y (mem y hy)).back_eq (e := .IrqHandler i) trivial, + (hMs' y (mem y hy)).fwd_eq (e := .IrqHandler i) trivial⟩ + have j5 : J5A st { Z with hw := hw } H OS s' σ' m := + ⟨⟨MdbInvA.mapA hTr.lk hTr.tree J.j5.1.1 hMs' hl, IrqRunA.keepIrq J.j5.1.2.1 irqC, + HandlersActiveA.keepIrq J.j5.1.2.2 (fun y hy i h => (irqC y hy i).1 h) wT.1 (fun i hi => by + have eI : irqStAddrA (L.ksb s) i = irqWordA (L.ksb s) i := rfl + rw [eI] + exact wT.mem_eq (hal _ (.irq i hi)) (fun hp => hp.elim (hoff _ (.irq i hi)) (fun hp => + vr.ks _ (V _ hp) (irqOffA i) (by rw [irqOffA_val hi]; omega) rfl)))⟩, J.j5.2.1, + fun y hy hn => by + have ne : y ≠ cslotAddrA cte := fun e => hn (e ▸ hab.isSlot J.j.1.place) + rw [capE2 y hy, hco y hy ne]; exact J.j5.2.2 y hy hn⟩ + -- the TCB VCPU words + have twH' : ∀ t ∈ H.tcbs, L.mem s' (wa t 3664#u64) = L.mem s (wa t 3664#u64) := fun t ht => by + have ht' : H t = some .tcb := ht + have hv := wa_val_tcbAG hH ht' (K := 3664#u64) (by simp) + have h1 := hH.align t _ ht' + simp only [HKind.bits] at h1 + have hal3 : Aligned (wa t 3664#u64) := by + unfold Aligned; rw [hv]; simp; omega + rw [F.w.mem_eq hal3 (fun hp => by + have := (q.omega _ (vr.om _ (V _ hp))).2.1 t ht + have hv2 : (wa t 3664#u64).val = t.val + 3664 := by rw [hv]; simp + omega)] + exact twH t ht + have tw : ∀ t ∈ Z.T, L.mem s' (wa t 3664#u64) = L.mem s (wa t 3664#u64) := fun t ht => + twH' t (by rw [J.tcbs]; exact ht) + have twR : ∀ t ∈ Z.T, L.mem s' (runVcpuA t) = L.mem s (runVcpuA t) := fun t htZ => by + have ht : t ∈ H.tcbs := by rw [J.tcbs]; exact htZ + have ht' : H t = some .tcb := ht + have hv := wa_val_tcbAG hH ht' (K := 3672#u64) (by simp) + have h1 := hH.align t _ ht' + simp only [HKind.bits] at h1 + have hal3 : Aligned (wa t 3672#u64) := by + unfold Aligned; rw [hv]; simp; omega + unfold runVcpuA + rw [F.w.mem_eq hal3 (fun hp => by + have := (q.omega _ (vr.om _ (V _ hp))).2.1 t ht + have hv2 : (wa t 3672#u64).val = t.val + 3672 := by rw [hv]; simp + omega)] + exact fld t ht 3672#u64 (by simp) (by simp) (by simp) + -- the spec side + have W := setCap_wfA sc cte σ ((), σ2) hσ2 + have um2 : σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory := by + have e : σ'.machineState.underlyingMemory = σ2.machineState.underlyingMemory := by rw [F.HS.eq] + rw [e, hms] + have horig' : σ'.isOriginalCap = σ.isOriginalCap := by + have e : σ'.isOriginalCap = σ2.isOriginalCap := by rw [F.HS.eq] + rw [e, horig] + have hcdt' : σ'.cdt = σ.cdt := by + have e : σ'.cdt = σ2.cdt := by rw [F.HS.eq] + rw [e, hcdt] + have hdp' : ∀ p dev pg, σ.kheap p = some (.archObj (.dataPage dev pg)) → + σ'.kheap p = some (.archObj (.dataPage dev pg)) := + fun p dev pg h => F.hdp p dev pg (HS.keep h (fun _ _ e => by cases e) (fun _ e => by cases e)) + have sinv : SInvXA σ' m n := + (J.sinv.of_fr (setCap_ndr sc cte σ ((), σ2) hσ2) + (setCap_irq hirq σ rfl J.sinv.irqN ((), σ2) hσ2).2).vsA F.HS + have nul : ∀ sl, specCapAtA σ' sl = some .nullCap → σ'.isOriginalCap sl = false := by + have n2 := nullOrig_setCapA hσ2 (CapRelA.ne_null hca hcn) HS W horig J.nullOrig + intro sl h + rw [horig'] + have h2 := n2 sl (by rw [← specCapAtA_vsA F.HS sl]; exact h) + rw [← horig]; exact h2 + have hms' : σ2.machineState = σ.machineState := hms + have zwf2 : DelInvA st Z.th s' := fun y hy p0 zt k h => + J.zwf y hy p0 zt k ((hMs' y (hsub y hy)).back_eq (e := .Zombie p0 zt k) trivial h) + have kfh : ∀ t ∈ Z.T, slotIdx t layout.tcb.SLOT_FAULT_HANDLER ∈ OS := fun t ht => + hsub _ (isSlot_fhA ((J.sr.thT t).2 ht)) + have fh2 : ∀ t ∈ Z.T, NullOrEp H (capA st s' (slotIdx t layout.tcb.SLOT_FAULT_HANDLER)) := fun t ht => by + rcases J.fhOK t ht with e | ⟨ep, b0, b1, b2, b3, b4, e, he⟩ + · exact Or.inl ((hMs' _ (kfh t ht)).fwd_eq (e := .Null) trivial e) + · exact Or.inr ⟨ep, b0, b1, b2, b3, b4, + (hMs' _ (kfh t ht)).fwd_eq (e := .Endpoint ep b0 b1 b2 b3 b4) trivial e, he⟩ + have pt2 : PtCapInA { Z with hw := hw, S1 := S1', Lv := Zl', T2 := T2', Sh2 := Sh2' } (L.mem s') := fun y hy p0 a0' v0' h => by + change capA st s' y = _ at h + rw [capE2 y (hsub y hy)] at h + by_cases e : y = cslotAddrA cte + · rw [e, hcx] at h + exact hpt p0 a0' v0' h + · rw [hco y (hsub y hy) e] at h + exact (J.ptCapIn y hy p0 a0' v0' h).imp (hS p0) (hT2 p0) + have fin := JStage3A.of_capVsGA (symL := F.symL) (zwf := zwf2) (fhOK := fh2) (ptCapIn := pt2) (J := J) (hw := hw) (P := P) (S1' := S1') (Zl' := Zl') (T2' := T2') (Sh2' := Sh2') + (Tb' := Tb') (Lv' := Lv') (w := wT) F.hP F.hbf HS hms' F.HS F.hasid hdp' hoff + F.jx F.sys F.vsOk F.bufVs F.sq F.poolOff sinv + (F.objT (ObjAllocA.mapA hMs' (fun _ _ h => h) J.objAlloc)) (IdleAnchorOffA.mapA hMs' J.idleAnchor) + F.bufCover F.bufNative + (F.deadT (DeadExtA.mapA (Z := Z) (Z' := { Z with hw := hw }) hMs' HS rfl rfl rfl tw + (fun v hv => wT.mem_eq (hal _ (.vcpu v _ hv (Or.inr rfl))) (fun hp => hp.elim + (hoff _ (.vcpu v _ hv (Or.inr rfl))) (fun hp => vr.vc _ (V _ hp) ⟨v, hv, 24#u64, by simp, by simp, rfl⟩))) J.deadExt |>.vsA F.HS)) + ((SpecFreeA.mapA hMs' HS hcdt horig J.specFree).vsA F.HS) (UtBlk39A.mapA hMs' J.utBlk) F.unmPt + (SpecZeroFreeA.mapA hMs' um2 J.specZero) (fun sl h => J.origLive sl (by rw [← horig']; exact h)) nul + (FrBaseApartA.mapA hMs' J.frBase) (DevFrDisjA.mapA hMs' hdp' J.devFr) + ((FinCovA.ofWfA J.finCov HS W).vsA F.HS) + (ZombieTcbNoVcpuA.mapA hMs' J.zombieTcb twH' J.zombieNoVcpu) (ZombieTcbTA.mapA hMs' J.zombieTcb) F.bufOff + (UtZombieA.mapA hMs' J.zombieBnd) + (F.devT (DevUtOffA.mapA (Z := Z) (Z' := { Z with hw := hw }) hMs' wT.1 (fun _ _ h => h) rfl J.devUtOff)) + ((J.sidle.slotA HS (fun sl a h => (specCapAtA_setCapA hσ2 HS W sl a h).imp_right fun (e : a = sc) => by rw [e]; exact hsci) + (IdleSlotNewA.of_inplaceA J.sidle J.j.1.st hab hc0 hσ2 HS W)).vsA F.HS) + (CapTyA.mapA hMs' J.capTy) + (J.vcRun.keepA tw (fun v hv => wT.mem_eq (hal _ (.vcpu v _ hv (Or.inr rfl))) (fun hp => hp.elim + (hoff _ (.vcpu v _ hv (Or.inr rfl))) (fun hp => vr.vc _ (V _ hp) ⟨v, hv, 24#u64, by simp, by simp, rfl⟩))) + (fun v hv => wT.mem_eq (hal _ (.vcpu v _ hv (Or.inl rfl))) (fun hp => hp.elim + (hoff _ (.vcpu v _ hv (Or.inl rfl))) (fun hp => vr.vc _ (V _ hp) ⟨v, hv, 0#u64, by simp, by simp, (wa_zeroVcA v).symm⟩))) + twR) + (J.vcSym1.keepA J.vcRun tw fun v hv => wT.mem_eq (hal _ (.vcpu v _ hv (Or.inl rfl))) (fun hp => hp.elim + (hoff _ (.vcpu v _ hv (Or.inl rfl))) (fun hp => vr.vc _ (V _ hp) ⟨v, hv, 0#u64, by simp, by simp, (wa_zeroVcA v).symm⟩))) + (J.vcSym2.keepA J.vcRun (fun v hv => wT.mem_eq (hal _ (.vcpu v _ hv (Or.inr rfl))) (fun hp => hp.elim + (hoff _ (.vcpu v _ hv (Or.inr rfl))) (fun hp => vr.vc _ (V _ hp) ⟨v, hv, 24#u64, by simp, by simp, rfl⟩))) twR) + (UtWinCapA.mapA hMs' J.utWin) (DevWinCapA.mapA hMs' J.devWin) (UserFrSpecA.mapA hMs' hdp' J.userFr) (FrThA.mapA hMs' (fun _ _ e => e) J.frTh) + (F.j5T j5) F.j7 F.ap' F.s1' F.ptH' F.setsInH' F.vsVc' F.tabS1' F.tabExt' + exact ⟨s2, σ2, s', a', σ', hw, P, Ac', hs2, hσ2, HS, F.HS, CapVsSpecA.of_steps HS F.HS F.hasid hms', wT, fin, hQ⟩ + +/-- **The ghost fields when the relation ghosts are kept** (only the record's `Tb` / `Lv` may change). -/ +theorem JStage3A.keepGhostA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {Rj : Set U64 → JSlotA S} {s s' : S} {a : A} + {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} {hw : HwRootA} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st Rj) s a σ m) + (hk : L.ksb s' = L.ksb s) (hc : L.cpu s' = L.cpu s) : + SlotApA { Z with hw := hw } H OS (L.ksb s') ∧ PtHA H (Z.Pp ∪ Z.S1) ∧ SetsInHA { Z with hw := hw } H ∧ + (∀ b, VsFootA Z.Pp Z.S1 Z.Y (L.ksb s') b → ¬ VcW Z.V b) ∧ + (∀ q ∈ Z.S1, TabOffA (L := L) { Z with hw := hw } m s' q) ∧ ExtTabOffA (L := L) { Z with hw := hw } m s' := + ⟨by rw [hk]; exact { J.ap with }, J.ptH, { J.setsInH with }, by rw [hk]; exact J.vsVc, + fun qq hq => + ⟨fun i hi h => (J.tabS1 qq hq).mem i hi + (footA_schedA (Z := Z) (Q := Z.Q) (Rl := Z.R) (curOf := Z.curOf) (curScOf := Z.curScOf) hk hc + (FootA.monoA (Z := { { Z with hw := hw } with S1 := ∅ }) rfl rfl (fun _ h => h) (fun _ h => h) + (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) h)), + (J.tabS1 qq hq).obj⟩, + ⟨fun qq hq i hi h => J.tabExt.mem qq hq i hi + (footA_schedA (Z := extZA Z) (Q := Z.Q) (Rl := Z.R) (curOf := Z.curOf) (curScOf := Z.curScOf) hk hc + (FootA.monoA (Z := extZA { Z with hw := hw }) rfl rfl (fun _ h => h) (fun _ h => h) + (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) h)), + J.tabExt.obj⟩⟩ + +end main + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3ChCancelA.lean b/proofs/Fl4ProofsA64/P3/Stage3ChCancelA.lean new file mode 100644 index 000000000..b8eef0978 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ChCancelA.lean @@ -0,0 +1,351 @@ +/- +P3 (aarch64, A64-P3 Stage-3 integration, line chg): **`ChKA` of the whole `cancel_all_ipc` / `cancel_all_signals`**. + +The queue members' blocked states come from the state relation (`EpStateRel`, `NtfnStateRel`); they are taken at the +initial state, so no `Nodup` of the queue is needed: `RmA` allows a member to be written again. + +No named premise. +-/ +import Fl4ProofsA64.P3.Stage3ChKeepA +import Fl4ProofsA64.P3.Stage3DelFinIpcA +import Fl4ProofsA64.P3.Stage3DelFinReplyA + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4ProofsA64.P3.Endp Classical + +noncomputable section + +/-- **The member frame**: each TCB keeps its fault, and its state unless it is a member of `M`, which may become +`Inactive`, or `Restart` when its fault satisfies `P`. -/ +def RmA (M : List ObjRef) (P : Option Fault → Prop) (σ σ' : State Unit) : Prop := + ∀ p x', σ'.kheap p = some (.tcb x') → ∃ x, σ.kheap p = some (.tcb x) ∧ x'.tcbFault = x.tcbFault ∧ + (x'.tcbState = x.tcbState ∨ p ∈ M ∧ (x'.tcbState = .inactive ∨ x'.tcbState = .restart ∧ P x.tcbFault)) + +instance (M : List ObjRef) (P : Option Fault → Prop) : PreO (RmA M P) where + refl _ := fun _ x h => ⟨x, h, rfl, Or.inl rfl⟩ + trans h1 h2 := fun p x'' h => by + obtain ⟨x', hx', f2, s2⟩ := h2 p x'' h + obtain ⟨x, hx, f1, s1⟩ := h1 p x' hx' + refine ⟨x, hx, f2.trans f1, ?_⟩ + rcases s2 with s2 | ⟨m, s2⟩ + · rcases s1 with s1 | ⟨m, s1⟩ + · exact Or.inl (s2.trans s1) + · exact Or.inr ⟨m, by rw [s2]; exact s1⟩ + · exact Or.inr ⟨m, s2.imp id fun ⟨a, b⟩ => ⟨a, f1 ▸ b⟩⟩ + +variable {M : List ObjRef} {P : Option Fault → Prop} + +theorem rm_ofTk {α : Type} {m : SMonad Unit α} + (h : ∀ σ r, r ∈ (m σ).1 → ∀ p x, r.2.kheap p = some (.tcb x) → σ.kheap p = some (.tcb x)) : PresR (RmA M P) m := + fun σ r hr p x hx => ⟨x, h σ r hr p x hx, rfl, Or.inl rfl⟩ + +theorem rm_ofEq {α : Type} {m : SMonad Unit α} (h : PresR (@Eq (State Unit)) m) : PresR (RmA M P) m := + PresR.ofEq' h + +theorem setSimple_rmA {α : Type} [Nonempty α] (C : α → KernelObject) (hC : ∀ a x, C a ≠ .tcb x) (p : ObjRef) (a : α) + (h : ∀ σ r, r ∈ (setSimpleKo (Z := Unit) C p a σ).1 → + ∃ y : α, σ.kheap p = some (C y) ∧ r.2 = { σ with kheap := Function.update σ.kheap p (some (C a)) }) : + PresR (RmA M P) (setSimpleKo (Z := Unit) C p a) := by + refine rm_ofTk fun σ r hr q x hx => ?_ + obtain ⟨y, -, e⟩ := h σ r hr + rw [e] at hx + change Function.update σ.kheap p _ q = _ at hx + by_cases eq : q = p + · rw [eq, Function.update_self] at hx + exact absurd (Option.some.inj hx) (hC _ _) + · rwa [Function.update_of_ne eq] at hx + +theorem setEndpoint_rmA (p : ObjRef) (a : Endpoint) : PresR (RmA M P) (setEndpoint (Z := Unit) p a) := + setSimple_rmA KernelObject.endpoint (fun _ _ h => by cases h) p a + fun _ _ hr => setSimpleKo_memA KernelObject.endpoint p a hr + +theorem setReply_rmA (p : ObjRef) (a : Reply) : PresR (RmA M P) (setReply (Z := Unit) p a) := + setSimple_rmA KernelObject.reply (fun _ _ h => by cases h) p a + fun _ _ hr => setSimpleKo_memA KernelObject.reply p a hr + +theorem setNotification_rmA (p : ObjRef) (a : Notification) : PresR (RmA M P) (setNotification (Z := Unit) p a) := + setSimple_rmA KernelObject.notification (fun _ _ h => by cases h) p a + fun _ _ hr => setSimpleKo_memA KernelObject.notification p a hr + +theorem resched_rmA : PresR (RmA M P) (rescheduleRequired (Z := Unit)) := by + refine rm_ofTk fun σ r hr p x hx => ?_ + obtain ⟨Q, e, -⟩ := rescheduleRequired_shapeA hr + rw [e] at hx; exact hx + +theorem enqueue_rmA (t : ObjRef) : PresR (RmA M P) (tcbSchedAction (Z := Unit) tcbSchedEnqueue t) := by + refine rm_ofTk fun σ r hr p x hx => ?_ + obtain ⟨Q, e, -⟩ := tcbSchedEnqueue_enqA t σ r hr + rw [e] at hx; exact hx + +theorem setAction_rmA (a : SchedulerAction) : PresR (RmA M P) (setSchedulerAction (Z := Unit) a) := by + unfold setSchedulerAction + exact PresR.modify _ fun σ p x hx => ⟨x, hx, rfl, Or.inl rfl⟩ + +theorem possibleSwitchTo_rmA (t : ObjRef) : PresR (RmA M P) (possibleSwitchTo (Z := Unit) t) := by + unfold possibleSwitchTo + repeat' (first + | exact setAction_rmA _ + | exact enqueue_rmA _ + | exact resched_rmA + | exact rm_ofEq (PresR.ofEq' (threadGet_eqPres _ _)) + | exact rm_ofEq (PresR.ofEq' (threadGet_eqPres _ _)) + | exact PresR.gets _ + | exact PresR.pure _ + | refine PresR.bind ?_ (fun _ => ?_) + | refine PresR.when ?_ + | refine PresR.ite' ?_ ?_ + | unfold getTcbObjRef + | dsimp only + | split) + +theorem ifSporadic_rmA (o : Option ObjRef) : + PresR (RmA M P) (ifSporadicCurScAssertRefillUnblockCheck (Z := Unit) o) := by + unfold ifSporadicCurScAssertRefillUnblockCheck ifCondRefillUnblockCheck + cases o with + | none => exact PresR.pure _ + | some p => + intro σ r hr + simp only [NonDet.maybeM] at hr + obtain ⟨⟨sc, σ1⟩, h1, hr⟩ := mem_bindA.1 hr + have e1 : σ1 = σ := (getSchedContext_mem h1).1 + rw [e1] at hr + dsimp only at hr + rw [nd_gets_bindSc, nd_pure_bindSc] at hr + try dsimp only at hr + unfold NonDet.when at hr + split at hr + · split at hr + · have h := refillUnblockCheck_onlyCkA p σ r hr + exact fun q x hx => ⟨x, h.tcb _ _ hx, rfl, Or.inl rfl⟩ + · have : r = ((), σ) := hr + rw [this]; exact PreO.refl (R := RmA M P) _ + · have : r = ((), σ) := hr + rw [this]; exact PreO.refl (R := RmA M P) _ + +/-- A member written `Inactive`, or `Restart` under `P` of its fault. -/ +theorem setState_rmA {t : ObjRef} {s : ThreadState} (ht : t ∈ M) {σ : State Unit} + (hs : s = .inactive ∨ s = .restart ∧ ∀ x, σ.kheap t = some (.tcb x) → P x.tcbFault) {r : Unit × State Unit} + (hr : r ∈ (setThreadState (Z := Unit) t s σ).1) : RmA M P σ r.2 := by + unfold setThreadState at hr + obtain ⟨a, ha, h2⟩ := mem_bindA.1 hr + obtain ⟨x, hx, ea⟩ := threadSet_memA _ t ha + have k : PresR (RmA M P) (setThreadStateAct (Z := Unit) t) := by + unfold setThreadStateAct + repeat' (first + | exact setAction_rmA _ + | exact PresR.gets _ + | exact PresR.pure _ + | refine PresR.bind ?_ (fun _ => ?_) + | refine PresR.when ?_ + | dsimp only) + refine PreO.trans (R := RmA M P) (fun p z hz => ?_) (k a.2 r h2) + rw [ea] at hz + change Function.update σ.kheap t _ p = _ at hz + by_cases e : p = t + · subst e + rw [Function.update_self] at hz + obtain rfl := KernelObject.tcb.inj (Option.some.inj hz) + exact ⟨x, hx, rfl, Or.inr ⟨ht, hs.imp id fun h => ⟨h.1, h.2 x hx⟩⟩⟩ + · rw [Function.update_of_ne e] at hz + exact ⟨z, hz, rfl, Or.inl rfl⟩ + +theorem restartNoFault_rmA {t : ObjRef} (ht : t ∈ M) : + PresR (RmA M (· = none)) (restartThreadIfNoFault (Z := Unit) t) := by + intro σ r hr + unfold restartThreadIfNoFault at hr + obtain ⟨⟨f, σ1⟩, h1, hr⟩ := mem_bindA.1 hr + have e1 : σ = σ1 := threadGet_eqPres _ _ σ _ h1 + subst e1 + dsimp only at hr + split at hr + · rename_i hf + obtain ⟨a, ha, hr⟩ := mem_bindA.1 hr + have k1 : RmA M (· = none) σ a.2 := by + refine setState_rmA ht (Or.inr ⟨rfl, fun x hx => ?_⟩) ha + unfold threadGet at h1 + obtain ⟨⟨y, σ2⟩, h3, h4⟩ := mem_bindA.1 h1 + have hg : getTcb (Z := Unit) t σ = some x := by simp [getTcb, readObject, hx] + change (y, σ2) ∈ (NonDet.assertOpt (getTcb (Z := Unit) t σ) σ).1 at h3 + rw [hg] at h3 + have e2 : (y, σ2) = (x, σ) := h3 + have e3 : (f, σ) = (y.tcbFault, σ2) := h4 + rw [← (Prod.mk.inj e2).1, ← (Prod.mk.inj e3).1] + exact hf + have k2 : PresR (RmA M (· = none)) (do + let scOpt ← getTcbObjRef (Z := Unit) Tcb.tcbSchedContext t + ifSporadicCurScAssertRefillUnblockCheck scOpt + possibleSwitchTo t) := by + refine PresR.bind (rm_ofEq (PresR.ofEq' ?_)) fun _ => PresR.bind (ifSporadic_rmA _) fun _ => + possibleSwitchTo_rmA _ + unfold getTcbObjRef; exact threadGet_eqPres _ _ + exact PreO.trans (R := RmA M (· = none)) k1 (k2 a.2 r hr) + · exact setState_rmA ht (Or.inl rfl) hr + +theorem unlink_rmA {t rp : ObjRef} (ht : t ∈ M) : PresR (RmA M P) (replyUnlinkTcb (Z := Unit) t rp) := by + have su : PresR (RmA M P) (setThreadState (Z := Unit) t .inactive) := fun σ r hr => + setState_rmA ht (Or.inl rfl) hr + unfold replyUnlinkTcb setReplyObjRef updateSkObjRef + repeat' (first + | exact su + | exact rm_ofEq (getReply_eqPresR _) + | exact setReply_rmA _ _ + | exact rm_ofEq (PresR.ofEq' (threadGet_eqPres _ _)) + | exact PresR.pure _ + | exact PresR.assert _ + | refine PresR.bind ?_ (fun _ => ?_) + | unfold getThreadState + | dsimp only) + +theorem removeRestartEp_rmA {t e : ObjRef} (ht : t ∈ M) : + PresR (RmA M (· = none)) (removeAndRestartEpQueuedThread (Z := Unit) t e) := by + have dq : PresR (RmA M (· = none)) (tcbEpDequeue (Z := Unit) t e) := by + unfold tcbEpDequeue + refine PresR.bind (rm_ofEq (getEndpoint_eqPresR _)) (fun _ => ?_) + repeat' (first + | exact setEndpoint_rmA _ _ + | exact PresR.pure _ + | exact PresR.assert _ + | refine PresR.bind ?_ (fun _ => ?_) + | dsimp only + | split) + unfold removeAndRestartEpQueuedThread + have gs : PresR (RmA M (· = none)) (getThreadState (Z := Unit) t) := by + unfold getThreadState; exact rm_ofEq (PresR.ofEq' (threadGet_eqPres _ _)) + refine PresR.bind dq fun _ => PresR.bind gs fun _ => + PresR.bind (PresR.pure _) fun o => ?_ + refine PresR.bind ?_ fun _ => restartNoFault_rmA ht + cases o with + | none => exact PresR.pure _ + | some r => exact unlink_rmA ht + +theorem removeRestartNtfn_rmA {t n : ObjRef} (ht : t ∈ M) : + PresR (RmA M (fun _ => True)) (removeAndRestartNtfnQueuedThread (Z := Unit) t n) := by + have dq : PresR (RmA M (fun _ => True)) (tcbNtfnDequeue (Z := Unit) t n) := by + unfold tcbNtfnDequeue getNotification getSimpleKo + repeat' (first + | exact setNotification_rmA _ _ + | exact rm_ofEq (getObject_eqPres _) + | exact PresR.pure _ + | exact PresR.assert _ + | exact PresR.assertOpt _ + | exact PresR.fail + | refine PresR.bind ?_ (fun _ => ?_) + | dsimp only + | split) + have su : PresR (RmA M (fun _ => True)) (setThreadState (Z := Unit) t .restart) := fun σ r hr => + setState_rmA ht (Or.inr ⟨rfl, fun _ _ => trivial⟩) hr + unfold removeAndRestartNtfnQueuedThread + refine PresR.bind dq fun _ => PresR.bind su fun _ => PresR.bind (rm_ofEq (PresR.ofEq' ?_)) fun _ => + PresR.bind (ifSporadic_rmA _) fun _ => possibleSwitchTo_rmA _ + unfold getTcbObjRef; exact threadGet_eqPres _ _ + +theorem mapMx_rmA {β : Type} (f : ObjRef → SMonad Unit β) (hf : ∀ x ∈ M, PresR (RmA M P) (f x)) : + ∀ l : List ObjRef, (∀ x ∈ l, x ∈ M) → PresR (RmA M P) (NonDet.mapMx f l) + | [], _ => by unfold NonDet.mapMx; exact PresR.pure _ + | x :: l, h => by + unfold NonDet.mapMx + exact PresR.bind (hf x (h x (List.mem_cons_self ..))) + fun _ => mapMx_rmA f hf l fun y hy => h y (List.mem_cons_of_mem _ hy) + +/-- The member frame, with the current thread and SC kept, gives `ChKA` when every member was blocked in a class +state whenever `P` lets it restart, i.e. `Restart` does not bring it into the classes. -/ +theorem ChKA.ofRm {σ σ' : State Unit} (hs : CurScA σ) (K : CBaseA σ σ') (R : RmA M P σ σ') + (hM : ∀ p ∈ M, ∀ x, σ.kheap p = some (.tcb x) → P x.tcbFault → inClassRSc x.tcbState x.tcbFault) : + ChKA σ σ' := by + refine ⟨K.cur, K.csc, fun p x' hx' => ?_, fun y' k o hy ho => ?_⟩ + · obtain ⟨x, hx, ef, e | ⟨hm, e | ⟨e, hp⟩⟩⟩ := R p x' hx' + · exact ⟨x, hx, by rw [e, ef]; exact id, by rw [e]; exact id⟩ + · refine ⟨x, hx, fun h => ?_, fun h => by rw [e] at h; cases h⟩ + rw [e] at h + simp [inClassRSc, runnable] at h + · exact ⟨x, hx, fun _ => hM p hm x hx hp, fun h => by rw [e] at h; cases h⟩ + · obtain ⟨y, j, hyj⟩ := hs + rw [K.sc y j hyj] at hy + obtain ⟨rfl, rfl⟩ := KernelObject.schedContext.inj (Option.some.inj hy) + exact Or.inl ⟨y, j, hyj, ho⟩ + +/-- The endpoint's queue members are blocked sending or receiving. -/ +def EpQBlkA (p : ObjRef) (σ : State Unit) : Prop := + ∀ z, σ.kheap p = some (.endpoint z) → ∀ t ∈ z.epQueue, ∀ x, σ.kheap t = some (.tcb x) → + (∃ e d, x.tcbState = .blockedOnSend e d) ∨ ∃ e r d, x.tcbState = .blockedOnReceive e r d + +theorem epQBlk_ofRecA {T E R : Set Aeneas.Std.U64} {Bf : Set (Aeneas.Std.U64 × Fl4SpecA64.Word)} + {mem : Aeneas.Std.U64 → Aeneas.Std.U64} {σ : State Unit} + (hR : EpStateRel T E R Bf mem σ) {e : Aeneas.Std.U64} (he : e ∈ E) : EpQBlkA (specPtr e) σ := by + intro z hz t hm x hx + obtain ⟨xs, hQ, hq, hb⟩ := (hR.at_ep he hz).queue + rw [hq, List.mem_map] at hm + obtain ⟨y, hy, ey⟩ := hm + rw [← ey] at hx + have hst := (hR.at_tcb (hQ.mem y hy) hx).st + have h34 := (hb y hy).2 + cases h : x.tcbState with + | blockedOnSend e d => exact Or.inl ⟨e, d, rfl⟩ + | blockedOnReceive e r d => exact Or.inr ⟨e, r, d, rfl⟩ + | ext v => cases v; rw [h] at hst; simp [Notif.tsCode] at hst; omega + | _ => rw [h] at hst; simp [Notif.tsCode] at hst; omega + +/-- **`cancel_all_ipc` keeps `ChKA`.** -/ +theorem cancelAllIpc_chA {p : ObjRef} {σ σ' : State Unit} (h : ((), σ') ∈ (cancelAllIpc (Z := Unit) p σ).1) + (hs : CurScA σ) (hne : EpQNeA p σ) (hb : EpQBlkA p σ) : ChKA σ σ' := by + rcases cancelAllIpc_bodyA h hne with rfl | ⟨K, -⟩ + · exact ChKA.refl _ + unfold cancelAllIpc at h + obtain ⟨⟨ep, σ1⟩, h1, h⟩ := mem_bindA.1 h + have hk : σ.kheap p = some (.endpoint ep) := (getEndpoint_mem h1).1 + have e1 : σ1 = σ := (getEndpoint_mem h1).2 + rw [e1] at h + have R : RmA ep.epQueue (· = none) σ σ' := by + cases ep with + | idleEP => + obtain rfl : σ' = σ := (Prod.mk.inj h).2 + exact PreO.refl (R := RmA _ _) _ + | _ => + obtain ⟨⟨q, σ0⟩, hq, h⟩ := mem_bindA.1 h + have eqq : q = _ := (Prod.mk.inj hq).1 + have eq0 : σ0 = σ := (Prod.mk.inj hq).2 + rw [eq0] at h + obtain ⟨⟨u, σ2⟩, h2, h3⟩ := mem_bindA.1 h + subst eqq + exact PreO.trans (R := RmA _ _) + (mapMx_rmA _ (fun t ht => removeRestartEp_rmA ht) _ (fun _ h => h) σ _ h2) (resched_rmA σ2 _ h3) + refine ChKA.ofRm hs K.toCBaseA R fun t ht x hx hf => ?_ + rcases hb ep hk t ht x hx with ⟨e, d, es⟩ | ⟨e, r, d, es⟩ + · exact Or.inr (Or.inr (Or.inl ⟨e, d, es, fun b c => by rw [hf] at c; cases c⟩)) + · exact Or.inr (Or.inr (Or.inr ⟨e, r, d, es⟩)) + +/-- **`cancel_all_signals` keeps `ChKA`.** -/ +theorem cancelAllSignals_chA {p : ObjRef} {σ σ' : State Unit} (h : ((), σ') ∈ (cancelAllSignals (Z := Unit) p σ).1) + (hs : CurScA σ) (hne : NtfnQNeA p σ) + (hb : ∀ y q, σ.kheap p = some (.notification y) → y.ntfnObj = .waitingNtfn q → ∀ t ∈ q, NtfnBlockedA σ t) : + ChKA σ σ' := by + rcases cancelAllSignals_bodyA h hne with rfl | ⟨K, -⟩ + · exact ChKA.refl _ + unfold cancelAllSignals at h + obtain ⟨⟨y, σ1⟩, h1, h⟩ := mem_bindA.1 h + have hk : σ.kheap p = some (.notification y) := (getNotification_mem h1).1 + have e1 : σ1 = σ := (getNotification_mem h1).2 + rw [e1] at h + cases ho : y.ntfnObj with + | waitingNtfn q => + simp only [ho] at h + obtain ⟨⟨u, σ2⟩, h2, h3⟩ := mem_bindA.1 h + have R : RmA q (fun _ => True) σ σ' := PreO.trans (R := RmA _ _) + (mapMx_rmA _ (fun t ht => removeRestartNtfn_rmA ht) _ (fun _ h => h) σ _ h2) (resched_rmA σ2 _ h3) + refine ChKA.ofRm hs K.toCBaseA R fun t ht x hx _ => ?_ + obtain ⟨x', o, hx', es⟩ := hb y q hk ho t ht + rw [hx] at hx' + obtain rfl := KernelObject.tcb.inj (Option.some.inj hx') + exact Or.inr (Or.inl ⟨o, es⟩) + | idleNtfn => + simp only [ho] at h + obtain rfl : σ' = σ := (Prod.mk.inj h).2 + exact ChKA.refl _ + | activeNtfn b => + simp only [ho] at h + obtain rfl : σ' = σ := (Prod.mk.inj h).2 + exact ChKA.refl _ + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3ChKeepA.lean b/proofs/Fl4ProofsA64/P3/Stage3ChKeepA.lean new file mode 100644 index 000000000..083d42712 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ChKeepA.lean @@ -0,0 +1,73 @@ +/- +P3 (aarch64, A64-P3 Stage-3 integration, line chg): **the frame that keeps the current SC chargeable**. + +* `CurScChargeableA` (moved here from `Stage3PreemptChargeA`, statement unchanged). +* `ChKA σ σ'`: no TCB enters the `released_ipc_queues` classes or an IPC queue, and the current SC gains a bound + thread only if that thread ends outside the classes. It is transitive and keeps `CurScChargeableA`. + +It holds of whole operations, not of every primitive: `reply_remove` donates before it unlinks the caller, +`cancel_ipc` clears the fault before it blocks the thread, and `cancel_all_ipc` unlinks a receiver's reply before it +restarts it. Those must be proved as units. + +No named premise. +-/ +import Fl4ProofsA64.P3.ScSchedA +import Fl4ProofsA64.P3.ScCfgEntryA + +namespace Fl4ProofsA64.P3 +open Fl4ProofsA64.P3.Sched Fl4SpecA64 Fl4Spec + +/-- [J] **The current SC is chargeable** (the analogue of l4v's `cur_sc_chargeable`): the thread bound to the current +SC is the current thread and is queued on no IPC object, or is in none of the `released_ipc_queues` classes. Must be +discharged at the integration merge (carried through the deletion loop). -/ +def CurScChargeableA (σ : State Unit) : Prop := + ∀ y n o x, σ.kheap σ.curSc = some (.schedContext y n) → y.scTcb = some o → σ.kheap o = some (.tcb x) → + (o = σ.curThread ∧ ipcQueuedThreadState x.tcbState = false) ∨ ¬ inClassRSc x.tcbState x.tcbFault + +/-- **The chargeability frame** (module docstring). -/ +structure ChKA (σ σ' : State Unit) : Prop where + cur : σ'.curThread = σ.curThread + csc : σ'.curSc = σ.curSc + tcb : ∀ p x', σ'.kheap p = some (.tcb x') → ∃ x, σ.kheap p = some (.tcb x) ∧ + (inClassRSc x'.tcbState x'.tcbFault → inClassRSc x.tcbState x.tcbFault) ∧ + (ipcQueuedThreadState x'.tcbState = true → ipcQueuedThreadState x.tcbState = true) + bind : ∀ y' n o, σ'.kheap σ.curSc = some (.schedContext y' n) → y'.scTcb = some o → + (∃ y k, σ.kheap σ.curSc = some (.schedContext y k) ∧ y.scTcb = some o) ∨ + ∀ x', σ'.kheap o = some (.tcb x') → ¬ inClassRSc x'.tcbState x'.tcbFault + +theorem ChKA.ofKheap {σ σ' : State Unit} (hc : σ'.curThread = σ.curThread) (hs : σ'.curSc = σ.curSc) + (hk : σ'.kheap = σ.kheap) : ChKA σ σ' := + ⟨hc, hs, fun _ x h => ⟨x, hk ▸ h, id, id⟩, fun y n _ h ho => Or.inl ⟨y, n, hk ▸ h, ho⟩⟩ + +theorem ChKA.refl (σ : State Unit) : ChKA σ σ := ChKA.ofKheap rfl rfl rfl + +theorem ChKA.trans {a b c : State Unit} (h1 : ChKA a b) (h2 : ChKA b c) : ChKA a c := by + refine ⟨h2.cur.trans h1.cur, h2.csc.trans h1.csc, fun p x' hx' => ?_, fun y' n o hy ho => ?_⟩ + · obtain ⟨x1, hx1, c2, q2⟩ := h2.tcb p x' hx' + obtain ⟨x0, hx0, c1, q1⟩ := h1.tcb p x1 hx1 + exact ⟨x0, hx0, fun h => c1 (c2 h), fun h => q1 (q2 h)⟩ + · rcases h2.bind y' n o (by rw [h1.csc]; exact hy) ho with ⟨y1, k1, hy1, ho1⟩ | hout + · rw [h1.csc] at hy1 + rcases h1.bind y1 k1 o hy1 ho1 with h0 | hout + · exact Or.inl h0 + · refine Or.inr fun x' hx' hcl => ?_ + obtain ⟨x1, hx1, c2, -⟩ := h2.tcb o x' hx' + exact hout x1 hx1 (c2 hcl) + · exact Or.inr hout + +/-- **Chargeability across the frame.** -/ +theorem CurScChargeableA.keep {σ σ' : State Unit} (h : CurScChargeableA σ) (k : ChKA σ σ') : + CurScChargeableA σ' := by + intro y n o x hy ho hx + rw [k.csc] at hy + rcases k.bind y n o hy ho with ⟨y0, n0, hy0, ho0⟩ | hout + · obtain ⟨x0, hx0, c, q⟩ := k.tcb o x hx + rcases h y0 n0 o x0 hy0 ho0 hx0 with ⟨e, hq⟩ | hn + · refine Or.inl ⟨e.trans k.cur.symm, ?_⟩ + cases hb : ipcQueuedThreadState x.tcbState with + | false => rfl + | true => rw [q hb] at hq; cases hq + · exact Or.inr fun hc => hn (c hc) + · exact Or.inr (hout x hx) + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3ChPrimA.lean b/proofs/Fl4ProofsA64/P3/Stage3ChPrimA.lean new file mode 100644 index 000000000..6a69f2c75 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ChPrimA.lean @@ -0,0 +1,111 @@ +/- +P3 (aarch64, A64-P3 Stage-3 integration, line chg): **`ChKA` of the scheduler primitives** the SchedContext / Thread +finalisers use (`setSchedulerAction`, `tcbSchedAction tcbSchedDequeue`, `tcbReleaseRemove`, `reschedule_required`, +`setThreadState .inactive`), and `PreO ChKA`, so that the `PresR` combinators compose them. + +No named premise. +-/ +import Fl4ProofsA64.P3.Stage3DelFinReplyA +import Fl4ProofsA64.P3.Stage3ChReplyA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Classical + +noncomputable section + +instance : PreO ChKA where + refl σ := ChKA.refl σ + trans h1 h2 := ChKA.trans h1 h2 + +theorem chA_ofEq {α : Type} {m : SMonad Unit α} (h : PresR (@Eq (State Unit)) m) : PresR ChKA m := + PresR.ofEq' h + +theorem cancelIpc_chP (t : ObjRef) : PresR ChKA (cancelIpc (Z := Unit) t) := + fun _ r hr => cancelIpc_chA (show ((), r.2) ∈ _ from hr) + +theorem setSchedulerAction_chA (a : SchedulerAction) : PresR ChKA (setSchedulerAction (Z := Unit) a) := by + unfold setSchedulerAction + exact PresR.modify _ fun σ => ChKA.ofKheap rfl rfl rfl + +theorem tcbSchedDequeue_chA (t : ObjRef) : PresR ChKA (tcbSchedAction (Z := Unit) tcbSchedDequeue t) := by + intro σ r hr + unfold tcbSchedAction getTcbQueue setTcbQueue at hr + obtain ⟨d, h1, hr⟩ := mem_bindA.1 hr + have e1 : σ = d.2 := threadGet_eqPres _ _ σ d h1 + obtain ⟨pr, h2, hr⟩ := mem_bindA.1 hr + have e2 : d.2 = pr.2 := threadGet_eqPres _ _ d.2 pr h2 + rw [← e2, ← e1] at hr + obtain ⟨q, hq, hr⟩ := mem_bindA.1 hr + obtain ⟨q0, hq0, hq⟩ := mem_bindA.1 hq + obtain rfl : q0 = (σ.readyQueues, σ) := hq0 + obtain rfl : q = (σ.readyQueues d.1 pr.1, σ) := hq + obtain ⟨u, hu, hr⟩ := mem_bindA.1 hr + obtain rfl : u = (tcbSchedDequeue t (σ.readyQueues d.1 pr.1), σ) := hu + obtain rfl : r = ((), _) := hr + exact ChKA.ofKheap rfl rfl rfl + +theorem tcbReleaseRemove_chA (t : ObjRef) : PresR ChKA (tcbReleaseRemove (Z := Unit) t) := by + unfold tcbReleaseRemove setReleaseQueue + intro σ r hr + obtain ⟨q, hq, hr⟩ := mem_bindA.1 hr + obtain rfl : q = (σ.releaseQueue, σ) := hq + obtain ⟨u, hu, hr⟩ := mem_bindA.1 hr + have k : ChKA σ u.2 := by + unfold NonDet.when at hu + split at hu + · obtain rfl : u = ((), _) := hu + exact ChKA.ofKheap rfl rfl rfl + · obtain rfl : u = ((), σ) := hu + exact ChKA.refl σ + obtain ⟨w, hw, hr⟩ := mem_bindA.1 hr + obtain rfl : w = (tcbSchedDequeue t σ.releaseQueue, u.2) := hw + obtain rfl : r = ((), _) := hr + exact ChKA.trans k (ChKA.ofKheap rfl rfl rfl) + +theorem rescheduleRequired_chA : PresR ChKA (rescheduleRequired (Z := Unit)) := by + intro σ r hr + obtain ⟨Q, e, -⟩ := rescheduleRequired_shapeA hr + rw [e] + exact ChKA.ofKheap rfl rfl rfl + +theorem setInactive_chA (t : ObjRef) : PresR ChKA (setThreadState (Z := Unit) t .inactive) := by + intro σ r hr + unfold setThreadState at hr + obtain ⟨a, ha, h2⟩ := mem_bindA.1 hr + obtain ⟨x, hx, ea⟩ := threadSet_memA _ t ha + unfold setThreadStateAct at h2 + obtain ⟨c, hc, h2⟩ := mem_bindA.1 h2 + obtain rfl : c = (a.2.curThread, a.2) := hc + obtain ⟨d, hd, h2⟩ := mem_bindA.1 h2 + obtain rfl : d = (a.2.schedulerAction, a.2) := hd + obtain ⟨e, he, h2⟩ := mem_bindA.1 h2 + obtain rfl : e = (schedulable t a.2, a.2) := he + have hk : a.2.kheap = Function.update σ.kheap t (some (.tcb { x with tcbState := .inactive })) := by rw [ea] + have T : ChKA σ a.2 := by + refine ⟨by rw [ea], by rw [ea], fun p x' hx' => ?_, fun y' n o hy ho => ?_⟩ + · rw [hk] at hx' + by_cases e : p = t + · subst e + rw [Function.update_self] at hx' + obtain rfl := KernelObject.tcb.inj (Option.some.inj hx') + exact ⟨x, hx, fun c => by simp [inClassRSc, runnable] at c, fun q => by simp [ipcQueuedThreadState] at q⟩ + · rw [Function.update_of_ne e] at hx' + exact ⟨x', hx', id, id⟩ + · rw [hk] at hy + by_cases e : σ.curSc = t + · rw [e, Function.update_self] at hy; cases hy + · rw [Function.update_of_ne e] at hy + exact Or.inl ⟨y', n, hy, ho⟩ + unfold NonDet.when at h2 + split at h2 + · unfold setSchedulerAction at h2 + obtain rfl : r = ((), _) := h2 + exact ChKA.trans T (ChKA.ofKheap rfl rfl rfl) + · obtain rfl : r = ((), a.2) := h2 + exact T + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3ChReplyA.lean b/proofs/Fl4ProofsA64/P3/Stage3ChReplyA.lean new file mode 100644 index 000000000..21ea7950a --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ChReplyA.lean @@ -0,0 +1,420 @@ +/- +P3 (aarch64, A64-P3 Stage-3 integration, line chg): **`ChKA` of the whole `reply_remove` and `cancel_ipc`**. + +Both break `ChKA` at intermediate steps (`reply_remove` donates the SC to the caller before it unlinks it, +`cancel_ipc` clears the fault before it blocks the thread). `TtA t` is the frame of everything but the one thread `t`: +every other TCB keeps its state and fault, the current thread and SC are kept, and the current SC gains a bound +thread only if it is `t`. `t` itself is made `Inactive` by the last step; `FinTt` carries that. + +No named premise. +-/ +import Fl4ProofsA64.P3.Stage3ChCancelA +import Fl4ProofsA64.P3.Stage3KEntryChargeA + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4ProofsA64.P3.Endp Classical + +noncomputable section + +/-- **Everything but the thread `t`** (module docstring). -/ +structure TtA (t : ObjRef) (σ σ' : State Unit) : Prop where + cur : σ'.curThread = σ.curThread + csc : σ'.curSc = σ.curSc + tcb : ∀ p x', σ'.kheap p = some (.tcb x') → ∃ x, σ.kheap p = some (.tcb x) ∧ + (p ≠ t → x'.tcbState = x.tcbState ∧ x'.tcbFault = x.tcbFault) + bind : ∀ y' n o, σ'.kheap σ.curSc = some (.schedContext y' n) → y'.scTcb = some o → + (∃ y k, σ.kheap σ.curSc = some (.schedContext y k) ∧ y.scTcb = some o) ∨ o = t + +theorem TtA.ofKheap {t : ObjRef} {σ σ' : State Unit} (hc : σ'.curThread = σ.curThread) (hs : σ'.curSc = σ.curSc) + (hk : σ'.kheap = σ.kheap) : TtA t σ σ' := + ⟨hc, hs, fun _ x h => ⟨x, hk ▸ h, fun _ => ⟨rfl, rfl⟩⟩, fun y n _ h ho => Or.inl ⟨y, n, hk ▸ h, ho⟩⟩ + +instance (t : ObjRef) : PreO (TtA t) where + refl _ := TtA.ofKheap rfl rfl rfl + trans {a b c} h1 h2 := by + refine ⟨h2.cur.trans h1.cur, h2.csc.trans h1.csc, fun p x' hx' => ?_, fun y' n o hy ho => ?_⟩ + · obtain ⟨x1, hx1, c2⟩ := h2.tcb p x' hx' + obtain ⟨x0, hx0, c1⟩ := h1.tcb p x1 hx1 + exact ⟨x0, hx0, fun ne => ⟨(c2 ne).1.trans (c1 ne).1, (c2 ne).2.trans (c1 ne).2⟩⟩ + · rcases h2.bind y' n o (by rw [h1.csc]; exact hy) ho with ⟨y1, k1, hy1, ho1⟩ | e + · rw [h1.csc] at hy1 + rcases h1.bind y1 k1 o hy1 ho1 with h0 | e + · exact Or.inl h0 + · exact Or.inr e + · exact Or.inr e + +/-- One object replaced. -/ +theorem TtA.upd {t p : ObjRef} {σ : State Unit} {o' : KernelObject} + (ht : ∀ x', o' = .tcb x' → ∃ x, σ.kheap p = some (.tcb x) ∧ + (p ≠ t → x'.tcbState = x.tcbState ∧ x'.tcbFault = x.tcbFault)) + (hs : p = σ.curSc → ∀ y' n o, o' = .schedContext y' n → y'.scTcb = some o → + (∃ y k, σ.kheap p = some (.schedContext y k) ∧ y.scTcb = some o) ∨ o = t) : + TtA t σ { σ with kheap := Function.update σ.kheap p (some o') } := by + refine ⟨rfl, rfl, fun q x' hq => ?_, fun y' n o hy ho => ?_⟩ + · change Function.update σ.kheap p _ q = _ at hq + by_cases e : q = p + · subst e + rw [Function.update_self] at hq + exact ht x' (Option.some.inj hq) + · rw [Function.update_of_ne e] at hq + exact ⟨x', hq, fun _ => ⟨rfl, rfl⟩⟩ + · change Function.update σ.kheap p _ σ.curSc = _ at hy + by_cases e : σ.curSc = p + · rw [e, Function.update_self] at hy + rw [e] + exact hs e.symm y' n o (Option.some.inj hy) ho + · rw [Function.update_of_ne e] at hy + exact Or.inl ⟨y', n, hy, ho⟩ + +/-- Only the heap-independent fields change. -/ +def KhA (σ σ' : State Unit) : Prop := + σ'.kheap = σ.kheap ∧ σ'.curThread = σ.curThread ∧ σ'.curSc = σ.curSc + +instance : PreO KhA where + refl _ := ⟨rfl, rfl, rfl⟩ + trans h1 h2 := ⟨h2.1.trans h1.1, h2.2.1.trans h1.2.1, h2.2.2.trans h1.2.2⟩ + +theorem KhA.tt {t : ObjRef} {σ σ' : State Unit} (h : KhA σ σ') : TtA t σ σ' := TtA.ofKheap h.2.1 h.2.2 h.1 + +theorem tt_ofEq {t : ObjRef} {α : Type} {m : SMonad Unit α} (h : PresR (@Eq (State Unit)) m) : PresR (TtA t) m := + PresR.ofEq' h + +theorem tt_ofKh {t : ObjRef} {α : Type} {m : SMonad Unit α} (h : PresR KhA m) : PresR (TtA t) m := + fun σ r hr => (h σ r hr).tt + +theorem setAction_khA (a : SchedulerAction) : PresR KhA (setSchedulerAction (Z := Unit) a) := by + unfold setSchedulerAction + exact PresR.modify _ fun σ => ⟨rfl, rfl, rfl⟩ + +theorem setSimple_ttA {t : ObjRef} {α : Type} [Nonempty α] (C : α → KernelObject) (hC : ∀ a x, C a ≠ .tcb x) + (hS : ∀ a y n, C a ≠ .schedContext y n) (p : ObjRef) (a : α) + (h : ∀ σ r, r ∈ (setSimpleKo (Z := Unit) C p a σ).1 → + ∃ y : α, σ.kheap p = some (C y) ∧ r.2 = { σ with kheap := Function.update σ.kheap p (some (C a)) }) : + PresR (TtA t) (setSimpleKo (Z := Unit) C p a) := by + intro σ r hr + obtain ⟨y, -, e⟩ := h σ r hr + rw [e] + exact TtA.upd (fun x' h => absurd h (hC a x')) (fun _ y' n _ h => absurd h (hS a y' n)) + +theorem setEndpoint_ttA {t : ObjRef} (p : ObjRef) (a : Endpoint) : PresR (TtA t) (setEndpoint (Z := Unit) p a) := + setSimple_ttA KernelObject.endpoint (fun _ _ h => by cases h) (fun _ _ _ h => by cases h) p a + fun _ _ hr => setSimpleKo_memA KernelObject.endpoint p a hr + +theorem setReply_ttA {t : ObjRef} (p : ObjRef) (a : Reply) : PresR (TtA t) (setReply (Z := Unit) p a) := + setSimple_ttA KernelObject.reply (fun _ _ h => by cases h) (fun _ _ _ h => by cases h) p a + fun _ _ hr => setSimpleKo_memA KernelObject.reply p a hr + +theorem setNotification_ttA {t : ObjRef} (p : ObjRef) (a : Notification) : + PresR (TtA t) (setNotification (Z := Unit) p a) := + setSimple_ttA KernelObject.notification (fun _ _ h => by cases h) (fun _ _ _ h => by cases h) p a + fun _ _ hr => setSimpleKo_memA KernelObject.notification p a hr + +/-- A `threadSet` that keeps the state and the fault. -/ +theorem threadSet_ttA {t : ObjRef} (f : Tcb → Tcb) (hf : ∀ x, (f x).tcbState = x.tcbState ∧ (f x).tcbFault = x.tcbFault) + (p : ObjRef) : PresR (TtA t) (threadSet (Z := Unit) f p) := by + intro σ r hr + obtain ⟨x, hx, e⟩ := threadSet_memA f p hr + rw [e] + exact TtA.upd (fun x' h => ⟨x, hx, fun _ => by rw [← KernelObject.tcb.inj h]; exact hf x⟩) + (fun _ y' n _ h => by cases h) + +/-- Any `threadSet` of `t` itself. -/ +theorem threadSetT_ttA (f : Tcb → Tcb) (t : ObjRef) : PresR (TtA t) (threadSet (Z := Unit) f t) := by + intro σ r hr + obtain ⟨x, hx, e⟩ := threadSet_memA f t hr + rw [e] + exact TtA.upd (fun x' _ => ⟨x, hx, fun ne => absurd rfl ne⟩) (fun _ y' n _ h => by cases h) + +theorem updateSc_ttA {t : ObjRef} (p : ObjRef) (f : SchedContext → SchedContext) + (hf : ∀ sc o, (f sc).scTcb = some o → sc.scTcb = some o ∨ o = t) : + PresR (TtA t) (updateSchedContext (Z := Unit) p f) := by + intro σ r hr + obtain ⟨y1, hy1, h⟩ := hr + have hy1' : y1 ∈ (NonDet.assertOpt (σ.kheap p) σ).1 := hy1 + cases hk : σ.kheap p with + | none => rw [hk] at hy1'; exact absurd hy1' (Set.notMem_empty _) + | some ko => + rw [hk] at hy1' + have e1 : y1 = (ko, σ) := hy1' + subst e1 + cases ko with + | schedContext sc n => + obtain ⟨ko', hko, hr2⟩ := setObject_memA h + rw [hr2] + refine TtA.upd (fun x' e => by cases e) (fun _ y' m o e ho => ?_) + obtain ⟨rfl, rfl⟩ := KernelObject.schedContext.inj e + rcases hf sc o ho with h0 | h0 + · exact Or.inl ⟨sc, n, hk, h0⟩ + · exact Or.inr h0 + | _ => exact absurd h (Set.notMem_empty _) + +theorem tcbSchedAction_ttA {t : ObjRef} (f : ObjRef → List ObjRef → List ObjRef) (q : ObjRef) : + PresR (TtA t) (tcbSchedAction (Z := Unit) f q) := by + intro σ r hr + obtain ⟨d, pr, e⟩ := tcbSchedAction_anyMemA f q hr + rw [e] + exact TtA.ofKheap rfl rfl rfl + +theorem resched_ttA {t : ObjRef} : PresR (TtA t) (rescheduleRequired (Z := Unit)) := by + intro σ r hr + obtain ⟨Q, e, -⟩ := rescheduleRequired_shapeA hr + rw [e] + exact TtA.ofKheap rfl rfl rfl + +theorem testReschedule_ttA {t : ObjRef} (q : ObjRef) : PresR (TtA t) (testReschedule (Z := Unit) q) := by + unfold testReschedule + repeat' (first + | exact resched_ttA + | exact PresR.gets _ + | exact PresR.pure _ + | refine PresR.bind ?_ (fun _ => ?_) + | refine PresR.when ?_ + | dsimp only) + +theorem tcbReleaseRemove_ttA {t : ObjRef} (q : ObjRef) : PresR (TtA t) (tcbReleaseRemove (Z := Unit) q) := by + unfold tcbReleaseRemove setReleaseQueue + repeat' (first + | exact PresR.modify _ fun σ => TtA.ofKheap rfl rfl rfl + | exact PresR.gets _ + | exact PresR.pure _ + | refine PresR.bind ?_ (fun _ => ?_) + | refine PresR.when ?_ + | dsimp only) + +macro "ttres" : tactic => `(tactic| + repeat' (first + | exact tt_ofKh (setAction_khA _) + | exact tcbSchedAction_ttA _ _ + | exact tcbReleaseRemove_ttA _ + | exact resched_ttA + | exact testReschedule_ttA _ + | exact setReply_ttA _ _ + | exact setEndpoint_ttA _ _ + | exact setNotification_ttA _ _ + | (refine threadSet_ttA _ ?_ _; intro _; exact ⟨rfl, rfl⟩) + | (refine updateSc_ttA _ _ ?_; intro _ _ h; first | exact Or.inl h | exact Or.inr (Option.some.inj h).symm | cases h) + | exact tt_ofEq (getReply_eqPresR _) + | exact tt_ofEq (getEndpoint_eqPresR _) + | exact tt_ofEq (getNotification_eqPres _) + | exact tt_ofEq (getSchedContext_eqPres _) + | exact tt_ofEq (PresR.ofEq' (threadGet_eqPres _ _)) + | exact tt_ofEq (threadGet_eqPres _ _) + | exact PresR.pure _ + | exact PresR.gets _ + | exact PresR.getsThe _ + | exact PresR.assertOpt _ + | exact PresR.assert _ + | exact PresR.fail + | refine PresR.bind_pure _ _ ?_ + | refine PresR.bind ?_ (fun _ => ?_) + | refine PresR.when ?_ + | refine PresR.unless' ?_ + | refine PresR.ite' ?_ ?_ + | dsimp only + | split)) + +/-- `t` is `Inactive` (if it is a TCB). -/ +def InaA (t : ObjRef) (σ : State Unit) : Prop := ∀ x', σ.kheap t = some (.tcb x') → x'.tcbState = .inactive + +/-- The whole operation is a `TtA t` step ending with `t` `Inactive`. -/ +def FinTt (t : ObjRef) {α : Type} (m : SMonad Unit α) : Prop := + ∀ σ r, r ∈ (m σ).1 → TtA t σ r.2 ∧ InaA t r.2 + +theorem FinTt.bind {t : ObjRef} {α β : Type} {m : SMonad Unit α} {f : α → SMonad Unit β} (hm : PresR (TtA t) m) + (hf : ∀ a, FinTt t (f a)) : FinTt t (NonDet.bind m f) := by + intro σ r hr + obtain ⟨y, hy, h⟩ := hr + obtain ⟨k, i⟩ := hf y.1 y.2 r h + exact ⟨PreO.trans (hm σ y hy) k, i⟩ + +theorem setInactive_finTt (t : ObjRef) : FinTt t (setThreadState (Z := Unit) t .inactive) := by + intro σ r hr + unfold setThreadState at hr + obtain ⟨a, ha, h2⟩ := mem_bindA.1 hr + obtain ⟨x, hx, ea⟩ := threadSet_memA _ t ha + have k : PresR KhA (setThreadStateAct (Z := Unit) t) := by + unfold setThreadStateAct + repeat' (first + | exact setAction_khA _ + | exact PresR.gets _ + | exact PresR.pure _ + | refine PresR.bind ?_ (fun _ => ?_) + | refine PresR.when ?_ + | dsimp only) + have hk := k a.2 r h2 + refine ⟨PreO.trans (threadSetT_ttA _ t σ a ha) hk.tt, fun x' hx' => ?_⟩ + rw [hk.1, ea] at hx' + change Function.update σ.kheap t _ t = _ at hx' + rw [Function.update_self] at hx' + exact (congrArg Tcb.tcbState (KernelObject.tcb.inj (Option.some.inj hx'))).symm + +theorem replyUnlinkTcb_finTt (t r : ObjRef) : FinTt t (replyUnlinkTcb (Z := Unit) t r) := by + unfold replyUnlinkTcb setReplyObjRef updateSkObjRef getThreadState + refine FinTt.bind (tt_ofEq (getReply_eqPresR _)) fun _ => FinTt.bind (PresR.assert _) fun _ => + FinTt.bind (tt_ofEq (threadGet_eqPres _ _)) fun _ => FinTt.bind (PresR.assert _) fun _ => + FinTt.bind ?_ fun _ => setInactive_finTt _ + exact PresR.bind (tt_ofEq (getReply_eqPresR _)) fun _ => setReply_ttA _ _ + +theorem schedContextDonate_ttA (p t : ObjRef) : PresR (TtA t) (schedContextDonate (Z := Unit) p t) := by + unfold schedContextDonate getScObjRef + refine PresR.bind ?_ fun o => PresR.bind ?_ fun _ => PresR.bind ?_ fun _ => ?_ + · ttres + · refine PresR.when ?_ + refine PresR.bind (PresR.assertOpt _) fun f => ?_ + ttres + · refine updateSc_ttA _ _ ?_ + intro _ o h + exact Or.inr (Option.some.inj h).symm + · ttres + +theorem replyRemove_finTt (t r : ObjRef) : FinTt t (replyRemove (Z := Unit) t r) := by + unfold replyRemove getTcbObjRef + refine FinTt.bind (tt_ofEq (getReply_eqPresR _)) fun _ => FinTt.bind (PresR.assert _) fun _ => + FinTt.bind (PresR.gets _) fun o => ?_ + cases o with + | none => dsimp only; exact replyUnlinkTcb_finTt _ _ + | some p => + dsimp only + have un : PresR (TtA t) (replyUnlinkSc (Z := Unit) p r) := by + unfold replyUnlinkSc setReplyObjRef updateSkObjRef + dsimp only [NonDet.liftM] + ttres + refine FinTt.bind ?_ fun _ => FinTt.bind ?_ fun _ => FinTt.bind un fun _ => + FinTt.bind (PresR.when (schedContextDonate_ttA _ _)) fun _ => replyUnlinkTcb_finTt _ _ + · dsimp only [NonDet.liftM]; ttres + · ttres + +theorem tcbEpDequeue_ttA {t : ObjRef} (q e : ObjRef) : PresR (TtA t) (tcbEpDequeue (Z := Unit) q e) := by + unfold tcbEpDequeue + ttres + +theorem tcbNtfnDequeue_ttA {t : ObjRef} (q e : ObjRef) : PresR (TtA t) (tcbNtfnDequeue (Z := Unit) q e) := by + unfold tcbNtfnDequeue + ttres + +theorem replyRemoveTcb_finTt (t r : ObjRef) : FinTt t (replyRemoveTcb (Z := Unit) t r) := by + unfold replyRemoveTcb getThreadState setReplyObjRef updateSkObjRef getReplyObjRef + try unfold getSkObjRef + refine FinTt.bind (tt_ofEq (threadGet_eqPres _ _)) fun _ => FinTt.bind (PresR.assert _) fun _ => + FinTt.bind (PresR.gets _) fun o => ?_ + cases o with + | none => dsimp only; exact replyUnlinkTcb_finTt _ _ + | some p => + dsimp only + refine FinTt.bind ?_ fun _ => FinTt.bind ?_ fun _ => FinTt.bind ?_ fun _ => FinTt.bind ?_ fun _ => + FinTt.bind ?_ fun _ => replyUnlinkTcb_finTt _ _ + all_goals (try dsimp only [NonDet.liftM]) + all_goals ttres + +theorem blockedCancelIpc_finTt (s : ThreadState) (t : ObjRef) (o : Option ObjRef) : + FinTt t (blockedCancelIpc (Z := Unit) s t o) := by + unfold blockedCancelIpc getBlockingObject + refine FinTt.bind (PresR.assertOpt _) fun e => FinTt.bind (tcbEpDequeue_ttA _ _) fun _ => ?_ + cases o with + | none => dsimp only; exact setInactive_finTt _ + | some r => + dsimp only + exact FinTt.bind (fun σ x hx => (replyUnlinkTcb_finTt t r σ x hx).1) fun _ => setInactive_finTt _ + +theorem cancelSignal_finTt (t n : ObjRef) : FinTt t (cancelSignal (Z := Unit) t n) := by + unfold cancelSignal + exact FinTt.bind (tcbNtfnDequeue_ttA _ _) fun _ => setInactive_finTt _ + +/-- `TtA` with `t` made `Inactive` is a `ChKA` step. -/ +theorem ChKA.ofTt {t : ObjRef} {σ σ' : State Unit} (K : TtA t σ σ') (hin : InaA t σ') : ChKA σ σ' := by + refine ⟨K.cur, K.csc, fun p x' hx' => ?_, fun y' n o hy ho => ?_⟩ + · obtain ⟨x, hx, c⟩ := K.tcb p x' hx' + by_cases e : p = t + · subst e + have hi := hin x' hx' + exact ⟨x, hx, fun h => by rw [hi] at h; simp [inClassRSc, runnable] at h, + fun h => by rw [hi] at h; cases h⟩ + · obtain ⟨c1, c2⟩ := c e + exact ⟨x, hx, by rw [c1, c2]; exact id, by rw [c1]; exact id⟩ + · rcases K.bind y' n o hy ho with h | e + · exact Or.inl h + · subst e + exact Or.inr fun x' hx' h => by rw [hin x' hx'] at h; simp [inClassRSc, runnable] at h + +theorem replyRemove_chA {c r : ObjRef} {σ σ' : State Unit} (h : ((), σ') ∈ (replyRemove (Z := Unit) c r σ).1) : + ChKA σ σ' := + let k := replyRemove_finTt c r σ _ h + ChKA.ofTt k.1 k.2 + +/-- The state read by `getThreadState`. -/ +theorem getThreadState_ttmA {σ σ1 : State Unit} {t : ObjRef} {s0 : ThreadState} + (h : (s0, σ1) ∈ (getThreadState (Z := Unit) t σ).1) : + σ1 = σ ∧ ∃ y, σ.kheap t = some (.tcb y) ∧ y.tcbState = s0 := by + have e : σ = σ1 := threadGet_eqPres _ _ σ _ (show (s0, σ1) ∈ (threadGet (Z := Unit) Tcb.tcbState t σ).1 from h) + subst e + refine ⟨rfl, ?_⟩ + unfold getThreadState threadGet at h + obtain ⟨x, hx, h2⟩ := mem_bindA.1 h + change x ∈ (NonDet.assertOpt (getTcb t σ) σ).1 at hx + cases hg : getTcb t σ with + | none => rw [hg] at hx; exact absurd hx (Set.notMem_empty _) + | some tcb => + rw [hg] at hx + obtain rfl : x = (tcb, σ) := hx + have e : (s0, σ) = (tcb.tcbState, σ) := h2 + refine ⟨tcb, ?_, (Prod.mk.inj e).1.symm⟩ + unfold getTcb readObject at hg + cases hkh : σ.kheap t with + | none => rw [hkh] at hg; cases hg + | some ko => rw [hkh] at hg; cases ko <;> simp at hg; rw [hg] + +/-- A state change of one TCB that keeps its state and does not bring it into the classes. -/ +theorem ChKA.ofTcbUpd {σ : State Unit} {t : ObjRef} {x x' : Tcb} (hx : σ.kheap t = some (.tcb x)) + (hs : x'.tcbState = x.tcbState) (hc : inClassRSc x'.tcbState x'.tcbFault → inClassRSc x.tcbState x.tcbFault) : + ChKA σ { σ with kheap := Function.update σ.kheap t (some (.tcb x')) } := by + refine ⟨rfl, rfl, fun p y hy => ?_, fun y' n o hy ho => ?_⟩ + · change Function.update σ.kheap t _ p = _ at hy + by_cases e : p = t + · subst e + rw [Function.update_self] at hy + obtain rfl := KernelObject.tcb.inj (Option.some.inj hy) + exact ⟨x, hx, hc, fun h => by rw [hs] at h; exact h⟩ + · rw [Function.update_of_ne e] at hy + exact ⟨y, hy, id, id⟩ + · change Function.update σ.kheap t _ σ.curSc = _ at hy + have ne : σ.curSc ≠ t := fun e => by rw [e, Function.update_self] at hy; cases hy + rw [Function.update_of_ne ne] at hy + exact Or.inl ⟨y', n, hy, ho⟩ + +theorem inClassRSc_fault {ts : ThreadState} {f f' : Option Fault} (hn : ∀ e d, ts ≠ .blockedOnSend e d) + (h : inClassRSc ts f') : inClassRSc ts f := by + rcases h with h | h | ⟨e, d, h, -⟩ | h + · exact Or.inl h + · exact Or.inr (Or.inl h) + · exact absurd h (hn e d) + · exact Or.inr (Or.inr (Or.inr h)) + +/-- **`cancel_ipc` keeps `ChKA`.** -/ +theorem cancelIpc_chA {t : ObjRef} {σ σ' : State Unit} (h : ((), σ') ∈ (cancelIpc (Z := Unit) t σ).1) : + ChKA σ σ' := by + unfold cancelIpc at h + obtain ⟨⟨s, σ1⟩, h1, h'⟩ := mem_bindA.1 h + obtain ⟨e1, x, hx, es⟩ := getThreadState_ttmA h1 + rw [e1] at h' + obtain ⟨⟨u, σ2⟩, h2, h3⟩ := mem_bindA.1 h' + obtain ⟨x2, hx2, e2⟩ := threadSet_memA _ t h2 + have T : TtA t σ σ2 := threadSetT_ttA _ t σ _ h2 + have fin : ∀ {m : SMonad Unit Unit}, FinTt t m → ((), σ') ∈ (m σ2).1 → ChKA σ σ' := fun hm hr => + let k := hm σ2 _ hr + ChKA.ofTt (PreO.trans T k.1) k.2 + cases s with + | blockedOnSend a b => exact fin (blockedCancelIpc_finTt _ _ _) h3 + | blockedOnReceive a b c => exact fin (blockedCancelIpc_finTt _ _ _) h3 + | blockedOnNotification n => + exact fin (cancelSignal_finTt _ _) h3 + | blockedOnReply r => exact fin (replyRemoveTcb_finTt _ _) h3 + | _ => + obtain rfl : σ' = σ2 := (Prod.mk.inj h3).2 + obtain rfl := KernelObject.tcb.inj (Option.some.inj (hx2.symm.trans hx)) + rw [show σ' = _ from e2] + refine ChKA.ofTcbUpd hx rfl fun hc => inClassRSc_fault (fun e d hd => ?_) hc + rw [es] at hd; cases hd + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3ClearAfA.lean b/proofs/Fl4ProofsA64/P3/Stage3ClearAfA.lean new file mode 100644 index 000000000..e0d651d84 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ClearAfA.lean @@ -0,0 +1,294 @@ +/- +P3 (aarch64, A64-P3 Lift 1b): **the record across the access-flag clear** (`ARMPageTestAndClearAccessed`'s +`fetch_and` of one leaf word with `~~~D_AF`), with the spec state unchanged: **`JSyscallA.clearAFA`**. + +The joint relation is `jat_clearAF` (`P3/FrReplyA`); the A64 P1 facts are the ones its S1a step uses +(`PtW.sysK`, `S1aR2.ofAccessWrite`, the cap-only classes along `OSKeep`). + +Named premises: none beyond the record. +-/ +import Fl4ProofsA64.P3.Stage3ReplyOneA +import Fl4ProofsA64.P3.Stage3SyscallA +import Fl4ProofsA64.P3.Stage3FinA +import Fl4ProofsA64.P3.Stage3RsSiteA +import Fl4ProofsA64.P3.Stage3VsFootA +import Fl4ProofsA64.P3.Stage3VsZeroA +import Fl4ProofsA64.P1.S1aR2AccessPost +import Fl4ProofsA64.P1.S1aHiTca +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P0.Consts Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp + Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-- Clearing `D_AF` (bit 10) keeps every bit-mask that misses it. -/ +theorem andNot1024A (w c : U64) (hc : (~~~1024#u64) &&& c = c) : (w &&& ~~~1024#u64) &&& c = w &&& c := by + have h : (~~~1024#u64 : U64).bv &&& c.bv = c.bv := congrArg UScalar.bv hc + apply UScalar.eq_of_val_eq + refine congrArg BitVec.toNat (show (w.bv &&& (~~~1024#u64 : U64).bv) &&& c.bv = w.bv &&& c.bv from ?_) + rw [BitVec.and_assoc, h] + +theorem andNot1024_3A (w : U64) : (w &&& ~~~1024#u64) &&& 3#u64 = w &&& 3#u64 := by + refine andNot1024A w _ (UScalar.eq_of_val_eq ?_) + refine congrArg BitVec.toNat (show (~~~(1024#64 : BitVec 64)) &&& 3#64 = 3#64 by decide) + +theorem andNot1024_bit0A (w : U64) : dBitA (w &&& ~~~1024#u64) 0 = dBitA w 0 := by + unfold dBitA + change (w.bv &&& ~~~(1024#64)).getLsbD 0 = w.bv.getLsbD 0 + rw [BitVec.getLsbD_and, show (~~~(1024#64 : BitVec 64)).getLsbD 0 = true by decide, Bool.and_true] + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-- **The narrowed Stage-1 typing across the access-flag clear** of a word that is no installed pool's word and no +ASID-table word. -/ +theorem utVsTy_clearAFA {H : Heap} {OS V Pp Tb : Set U64} {Lv : U64 → Nat} {s : S} {sl : U64} (hsl : Aligned sl) + (T : VsTyR st (L.ksb s) (PoolSet st Pp s) ∅ Tb Lv s) (V0 : UtVsOkA st H OS V Pp Tb Lv s) + (hA : ¬ AsW (L.ksb s) sl) + (hP : ∀ p ∈ PoolSet st Pp s, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → wa p o ≠ sl) : + let q := L.wr s sl (L.mem s sl &&& ~~~1024#u64) + VsTyR st (L.ksb q) (PoolSet st Pp q) ∅ Tb Lv q ∧ UtVsOkA st H OS V Pp Tb Lv q ∧ + utVsPtA st Pp Tb Lv q ⊆ utVsPtA st Pp Tb Lv s := by + intro q + have hks : L.ksb q = L.ksb s := L.ksb_wr _ _ _ + have ne : ∀ b, Aligned b → b ≠ sl → L.mem q b = L.mem s b := fun b hb h => mem_wr_ne' hsl hb h.symm + have bits : ∀ b, Aligned b → (L.mem q b &&& 3#u64) = (L.mem s b &&& 3#u64) ∧ + (L.mem q b &&& vspace.ADDR_MASK) = (L.mem s b &&& vspace.ADDR_MASK) := fun b hb => by + by_cases h : b = sl + · rw [h, show L.mem q sl = L.mem s sl &&& ~~~1024#u64 from L.mem_wr_same hsl] + exact ⟨andNot1024_3A _, andNot1024A _ _ (UScalar.eq_of_val_eq + (congrArg BitVec.toNat (congrArg UScalar.bv (show (~~~1024#u64) &&& vspace.ADDR_MASK = vspace.ADDR_MASK from + s1aAccessMaskStage1))))⟩ + · rw [ne b hb h]; exact ⟨rfl, rfl⟩ + have asid : ∀ o : U64, 772352 ≤ o.val → o.val < 773376 → o.val % 8 = 0 → + L.mem q (wa (L.ksb s) o) = L.mem s (wa (L.ksb s) o) := fun o h1 h2 h8 => + ne _ (V0.al _ (Or.inr ⟨o, h1, h2, h8, rfl⟩)) (fun e => hA ⟨o, h1, h2, h8, e.symm⟩) + have hps : PoolSet st Pp q = PoolSet st Pp s := by + ext p + unfold PoolSet + rw [hks] + constructor <;> rintro ⟨hp, o, h1, h2, h8, e, hn⟩ + · exact ⟨hp, o, h1, h2, h8, by rw [← asid o h1 h2 h8]; exact e, hn⟩ + · exact ⟨hp, o, h1, h2, h8, by rw [asid o h1 h2 h8]; exact e, hn⟩ + have poolAl : ∀ p ∈ PoolSet st Pp s, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → Aligned (wa p o) := + fun p hp o h1 h2 => V0.al _ (Or.inl ⟨p, Or.inl hp, o, h1, h2, rfl⟩) + have poolW : ∀ p ∈ PoolSet st Pp s, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → + L.mem q (wa p o) = L.mem s (wa p o) := fun p hp o h1 h2 => ne _ (poolAl p hp o h1 h2) (hP p hp o h1 h2) + have tabAl : ∀ p ∈ Tb, VsReach st (PoolSet st Pp s) ∅ Lv s p → ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → + Aligned (wa p o) := fun p hp r o h1 h2 => V0.al _ (Or.inl ⟨p, Or.inr ⟨hp, r⟩, o, h1, h2, rfl⟩) + have back : ∀ p, VsReach st (PoolSet st Pp s) ∅ Lv q p → VsReach st (PoolSet st Pp s) ∅ Lv s p ∧ p ∈ Tb := by + intro p r + induction r with + | hr h => exact absurd h (Set.notMem_empty _) + | @root0 p o hp h1 h2 h3 h4 => + rw [poolW p hp o h1 h2] at h3 h4 ⊢ + exact ⟨.root0 hp h1 h2 h3 h4, ((T.pool p hp o h1 h2 h3).1 h4).1⟩ + | @root2 p o hp h1 h2 h3 => + rw [poolW p hp o h1 h2] at h3 ⊢ + have hn : L.mem s (wa p o) ≠ 0#u64 := fun e => by + rw [e] at h3 + exact absurd (congrArg UScalar.val h3) (by simp) + exact ⟨.root2 hp h1 h2 h3, ((T.pool p hp o h1 h2 hn).2 h3).1⟩ + | @step p o _ hl h1 h2 h3 ih => + have al := tabAl p ih.2 ih.1 o h1 h2 + rw [(bits _ al).1] at h3 + rw [(bits _ al).2] + exact ⟨.step ih.1 hl h1 h2 h3, (T.desc p ih.2 ih.1 hl o h1 h2 h3).1⟩ + have sub : utVsPtA st Pp Tb Lv q ⊆ utVsPtA st Pp Tb Lv s := by + unfold utVsPtA + rw [hps] + rintro p (h | ⟨h, r⟩) + · exact Or.inl h + · exact Or.inr ⟨h, (back p r).1⟩ + refine ⟨?_, ?_, sub⟩ + · rw [hks, hps] + refine ⟨fun o h1 h2 h8 h => ?_, fun p hp o h1 h2 h => ?_, fun p hp r hl o h1 h2 h => ?_⟩ + · rw [asid o h1 h2 h8] at h ⊢; exact T.tbl o h1 h2 h8 h + · rw [poolW p hp o h1 h2] at h ⊢; exact T.pool p hp o h1 h2 h + · have rs := (back p r).1 + have al := tabAl p hp rs o h1 h2 + rw [(bits _ al).1] at h + rw [(bits _ al).2] + exact T.desc p hp rs hl o h1 h2 h + · unfold UtVsOkA + rw [hks] + exact VrOK.monoPtA V0 sub + +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {ar : machine.Arch A} {nA : U64} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +set_option maxHeartbeats 2000000 in +/-- **`JStage3A` across the access-flag clear** of the leaf word that FL4's walk reaches (module docstring). -/ +theorem JSyscallA.clearAFA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) + {f : vspace.FrameCap} {root k : U64} {lu : vspace.PtSlot} + (hfr : vspace.frame_root st s f.asid = ok (.Ok (root, k))) + (hme : vspace.mapped_entry st s f root = ok (some lu)) + {P j : U64} (hP : P ∈ Z.S1 ∨ ∃ y ∈ Z.Y, P = wa y 4096#u64) (hj : j.val < 512) (hlu : lu.slot = peAF P j) : + let q := L.wr s (peAF P j) (L.mem s (peAF P j) &&& ~~~1024#u64) + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) q a σ m ∧ + L.curW q = L.curW s ∧ L.mem q (scA (L.curW s)) = L.mem s (scA (L.curW s)) ∧ + L.mem q (inqA (L.curW s)) = L.mem s (inqA (L.curW s)) := by + intro q + have J3 := J.toJStage3A + obtain ⟨Pt, Ub, Pp7, Tb7, Hr, Te, Kt, Lv7, Re, bits, KLv, W⟩ := J7A.unp J3.j7 + have h := W.r2 + have E0 := J3.toJEntA + have hTr : TreeA st OS Z.G.chains Z.G.par s := + J3.j.1.cdt.tree.grow J3.j5.2.1 (fun x hx hn => J3.j5.2.2 x hx hn) + obtain ⟨jx, -, -, -, -, hcur, hsc, hiq, -⟩ := jat_clearAF E0 RS J3.tabS1 J3.tabExt hTr hP hj + -- the leaf in A64 P1's form + have loc : PtW (TbO Tb7 Hr) lu.slot := by + have res := post_of_okA (vspace.frame_root.ty (st := st) h.core.stage1.sub rfl f.asid) hfr + obtain ⟨reach, lev⟩ := res _ rfl + obtain ⟨p, hp, off, bound, al, address, below⟩ := + post_of_okA (vspace.mapped_entry.leafSlot h.core.stage1.sub reach lev f) hme lu rfl + exact ⟨p, ⟨hp.1, fun r => (Nat.ne_of_gt below) (h.place.high.lv p r)⟩, off, bound, al, address⟩ + have foot : PtW Pt lu.slot := ptw_mono (tbo_sub.trans h.place.tables) loc + have al : Aligned lu.slot := W.vr.al _ (Or.inl foot) + rw [hlu] at loc foot al + have one : WritesIn st s q (· = peAF P j) := writesIn_wr al + have writes : WritesIn st s q (PtW Pt) := one.mono (fun _ e => e.symm ▸ foot) + have ptKs : ∀ b, PtW Pt b → ¬ AsW (L.ksb s) b := + J3.ko.ptAsWG h.footprint J3.sys.sysl.ly.ksw (h.footprint.fit J3.sys.ent.heap) + have tbl := TabKeep.ofW W.vr writes ptKs + have hB := J3.sys.ent.bxi.blk + obtain ⟨⟨sysl, symL⟩, kept⟩ := PtW.sysK J3.sys.ent.heap J3.sys.ent.os W.vr J3.sys.sysl J3.symL hB writes + have ne : ∀ b, Aligned b → b ≠ peAF P j → L.mem q b = L.mem s b := fun b hb e => one.mem_eq hb e + have edit : S1aAccessWrite st OS Pt Hr s q := by + refine ⟨tbl, fun root hr => ?_, fun x hx => kept.capA hx, fun b hb => ?_⟩ + · have header : PtW Pt (wa root 4088#u64) := + ⟨root, h.place.tables (h.place.high.sub hr), 4088#u64, by decide, by decide, rfl⟩ + exact ne _ (W.vr.al _ (Or.inl header)) (fun eq => h.place.high.tbo loc root hr eq.symm) + · by_cases e : b = peAF P j + · rw [e]; exact Or.inr (L.mem_wr_same al) + · exact Or.inl (ne b (W.vr.al _ (Or.inl hb)) e) + obtain ⟨_, _, _, kinds, _, _⟩ := J3.sys.sysl.sys.stz.1.1.mdbS + have R' := h.ofAccessWrite J3.sys.ent.heap kinds edit + have edge : EdgeKeepOn st (KtHdrW st OS Kt s) s q := fun b hF h3 => by + have bal : Aligned b := by + rcases hF with ⟨t, ht, o, o1, o2, rfl⟩ | ⟨R, hR, o, o1, o2, rfl⟩ + · exact W.vr.al _ (Or.inl ⟨t, h.place.tables (h.kernelTables.1 ht), o, o1, o2, rfl⟩) + · exact W.vr.al _ (Or.inl ⟨R, h.place.tables (h.place.high.sub (h.highRoots ▸ hR)), o, by omega, o2, rfl⟩) + by_cases e : b = peAF P j + · have m3 := edit.mask (e ▸ foot) 3#u64 (by decide) + have ma := edit.mask (e ▸ foot) vspace.ADDR_MASK s1aAccessMaskStage1 + refine ⟨by rw [← m3]; exact h3, ?_⟩ + unfold kChild; rw [ma] + · rw [ne b bal e] at h3 ⊢; exact ⟨h3, rfl⟩ + have Fx : S1aStepFix st H Z.Cs OS Z.V Ub Pt Kt KLv ia ie nA q := + S1aStepFix.mk sysl symL (S1aArmCol.ofTZ tbl.toDel W.col) (CapInv.keepO W.fu kept) (CapInv.keepO W.vcc kept) + (CapInv.keepO h.pageTables kept) (CapInv.keepO h.hvPt kept) h.footprint (CapInv.keepO W.df kept) + R'.highHeaders (CapInv.keepO h.eptRoots kept) (CapInv.keepO h.pageObjects kept) + (CapInv.keepO h.highObjects kept) (W.sep.keep kept) R'.ex + (W.hiK.keep ⟨edge, fun y hy R m a c => ⟨y, hy, m, a, (kept.capA hy).symm.trans c⟩⟩) + have j7 : J7A st (J7R2A st ar nA) Z H OS q σ m := + J7A.of_stepFixedA Fx W.vr tbl.1 W.al W.vu (PoolNamed.ofTab tbl.1 tbl.2 kept W.pn) (schedPage_ptA R' W.sp) + (fun b hb o ho => by rw [tbl.1]; exact W.ks b hb o ho) (J7R2K.of_stepFixedA (σ' := σ) (m := m) Fx W.ktK) + -- the VSpace typing of the record's own sets + have R := J3.j.1.vs + have hPa : P.val % 4096 = 0 := by + rcases hP with hP | ⟨y, hy, rfl⟩ + · exact Nat.mod_eq_zero_of_dvd (R.al P hP) + · exact waWin_modA (R.yal y hy) + have hAs : ¬ AsW (L.ksb s) (peAF P j) := ptKs _ foot + have hPool : ∀ p ∈ PoolSet st Pp s, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → wa p o ≠ peAF P j := by + rintro p ⟨-, o', h1, h2, h8, e, hn⟩ o ho ho8 eq + obtain ⟨hi, hhi, ea⟩ := asW_iff_atAF.1 (⟨o', h1, h2, h8, rfl⟩ : AsW (L.ksb s) (wa (L.ksb s) o')) + rw [ea] at e + have hpZ : p ∈ Z.Pp := e ▸ R.tmem hi hhi (by rw [e]; exact hn) + have pa : p.val % 4096 = 0 := Nat.mod_eq_zero_of_dvd (R.pal p hpZ) + have pb : p.val < 2 ^ 64 := p.bv.isLt + have Pb : P.val < 2 ^ 64 := P.bv.isLt + have ep : p = P := by + have ev := congrArg UScalar.val eq + rw [wa_valm, Nat.mod_eq_of_lt (by omega), peAF_valA (by omega) hj] at ev + exact UScalar.eq_of_val_eq (by omega) + obtain ⟨pf, hpf, -⟩ := R.pool p hpZ + rcases hP with hP | ⟨y, hy, rfl⟩ + · obtain ⟨pt, hpt, -⟩ := R.pt P hP + rw [ep, hpt] at hpf + cases hpf + · exact J3.j.1.ext.ypp y hy (ep ▸ hpZ) + obtain ⟨vsT, vsOk', vsSub⟩ := utVsTy_clearAFA al J3.sys.ent.vs J3.vsOk hAs hPool + have bxi : BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac q := + BlkOK.bxi (blk_capw_wr hB al (Or.inr ⟨W.vr.om _ (Or.inl foot), W.vr.sg _ (Or.inl foot)⟩)) + have sys : SysInvA st H Z.Cs OS U Ac Pp Tb Lv ia ie q := + ⟨⟨J3.sys.ent.heap, bxi, J3.sys.ent.os, J3.sys.ent.user.ksb tbl.1, vsT, by rw [tbl.1]; exact J3.sys.ent.ksw⟩, sysl⟩ + have sq : SchedQueuesInv st H.tcbs Z.Cs H.omega H q Z.Q Z.R := + inv_of_capw J3.sq writes (fun b hb => Or.inl (Or.inl (W.vr.om b (Or.inl hb)))) + have poolOff : PoolOff st H Z.Cs OS q := PoolOff.ofTab tbl.1 tbl.2 J3.poolOff + have bit : ∀ p ∈ Pt, ∀ i : U64, i.val < 512 → dBitA (L.mem q (peAF p i)) 0 = dBitA (L.mem s (peAF p i)) 0 := by + intro p hp i hi + have w : PtW Pt (peAF p i) := ⟨p, hp, core.num.U64.wrapping_mul i 8#u64, + by rw [wm8_valA hi]; omega, by rw [wm8_valA hi]; omega, rfl⟩ + by_cases e : peAF p i = peAF P j + · rw [e, show L.mem q (peAF P j) = L.mem s (peAF P j) &&& ~~~1024#u64 from L.mem_wr_same al] + exact andNot1024_bit0A _ + · rw [ne _ (W.vr.al _ (Or.inl w)) e] + have hcpu : L.cpu q = L.cpu s := L.cpu_wr _ _ _ + have unmPt : UnmPtSpecA st Z m OS q σ := by + have tab : ∀ ty p, p ∈ Pt → UnmTabA st Z m s σ ty p → UnmTabA st Z m q σ ty p := fun ty p hp u => + ⟨fun i hi => by rw [bit p hp i hi]; exact u.inv i hi, u.spec, u.off.of_ksA tbl.1 hcpu⟩ + refine ⟨fun u hu p asid va c => ?_, fun u hu p asid c => ?_, fun u hu p asid va c => ?_, + fun u hu p asid c => ?_⟩ + · have o := (kept.capA hu).symm.trans c + exact tab _ _ (W.sp p (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inl ⟨_, _, _, o⟩⟩)))))))) (J3.unmPt.pt u hu p asid va o) + · have o := (kept.capA hu).symm.trans c + exact tab _ _ (W.sp p (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inl ⟨_, _, o⟩)⟩)))))))) (J3.unmPt.vs u hu p asid o) + · have o := (kept.capA hu).symm.trans c + have l0 : p ∈ Pt := W.sp p (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inr (Or.inl ⟨_, _, o⟩))⟩))))))) + have l1 : wa p 4096#u64 ∈ Pt := W.sp _ (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inr (Or.inr (Or.inl ⟨p, _, _, o, rfl⟩)))⟩))))))) + have u0 := J3.unmPt.s2 u hu p asid va o + refine ⟨fun i hi => ?_, u0.spec⟩ + by_cases hlo : i.val < 512 + · rw [bit p l0 i hlo]; exact u0.inv i hi + · obtain ⟨i', hi', ei⟩ := peAF_hiA p i (by omega) hi + have := u0.inv i hi + rw [ei] at this ⊢ + rw [bit _ l1 i' hi']; exact this + · have o := (kept.capA hu).symm.trans c + have l1 : wa p 4096#u64 ∈ Pt := W.sp _ (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inr (Or.inr (Or.inr ⟨p, _, o, rfl⟩)))⟩))))))) + have u0 := J3.unmPt.hv u hu p asid o + exact ⟨fun i hi => by rw [bit _ l1 i hi]; exact u0.inv i hi, u0.spec⟩ + have j5 : J5A st Z H OS q σ m := by + refine ⟨⟨MdbInvA.keepO hTr.lk hTr.tree J3.j5.1.1 kept, IrqRunA.keepO hTr.lk J3.j5.1.2.1 kept, + HandlersActiveA.keepO hTr.lk J3.j5.1.2.2 kept tbl.1 (fun i hi => ?_)⟩, J3.j5.2.1, + fun y hy hn => by rw [kept.capA hy]; exact J3.j5.2.2 y hy hn⟩ + have hal : Aligned (irqWordA (L.ksb s) i) := by + have := J3.sr.toSchedSRN.ksb_al + have := irqWordA_val J3.sr.ksFit hi + show _ % 8 = 0 + omega + refine ne _ hal (fun e => ?_) + have o : (wa 224#u64 (core.num.U64.wrapping_mul i 8#u64)).val < 223488 := by + simp [core.num.U64.wrapping_add_val_eq, core.num.U64.wrapping_mul_val_eq, U64.size_def, U64.numBits] + omega + exact W.vr.ks _ (Or.inl foot) _ o e + refine ⟨?_, hcur, hsc, hiq⟩ + exact J3.of_vsWordsA (hw := Z.hw) (P := (· = peAF P j)) one + (fun b e => by + subst e + refine vsWord_entryA ?_ hj + rcases hP with hP | hP + · exact Or.inr (Or.inl hP) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl hP))))))) + ⟨by cases σ; rfl, fun _ => Or.inl rfl, fun _ _ h => h⟩ (fun _ _ _ h => h) jx sys vsOk' (bufVs_subA J3.bufVs tbl.1 vsSub) sq poolOff unmPt j5 j7 + symL J3.j5.2.1 + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3CnArmFactsA.lean b/proofs/Fl4ProofsA64/P3/Stage3CnArmFactsA.lean new file mode 100644 index 000000000..aba2f271e --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3CnArmFactsA.lean @@ -0,0 +1,829 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Plan B): **CNode Copy / Mint's arm inputs from the run and the record.** + +* `decCnodeCopy_invA`: what a successful `decode_invocation` of CNode Copy / Mint ran: the argument words, the two + slot lookups, the empty destination and the non-null `copied_cap`. +* `cnode.copied_cap.derivA3`: Copy / Mint's new cap is `DerivA` the source. +* `JSyscallA.cnCopy_armA`: `CnCopyArmA` from `JSyscallA`, the site and the successful decode; every derived-cap fact + comes from `copied_relA` and the record (`SIdleA`, the CDT tree, the untyped revocability). + +Named premises: [J] `JSyscallA`. New module. +-/ +import Fl4ProofsA64.P3.Stage3ScopeArmsA +import Fl4ProofsA64.P3.CnodeFrontA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +/-! ## `copied_cap`'s new cap is derived -/ + +theorem DerivA.trans3 {a b c : capability.Cap} (h1 : DerivA a b) (h2 : DerivA b c) : DerivA a c := by + cases h1 with + | refl => exact h2 + | ep => cases h2 <;> exact .ep .. + | ntfn => cases h2 <;> exact .ntfn .. + | reply => cases h2 <;> exact .reply .. + | cnode => cases h2 <;> exact .cnode .. + | frame => cases h2 <;> exact .frame .. + | irqH i => cases h2; exact .irqH i + | ut p b i i' dv h => + cases h2 with + | refl => exact .ut _ _ _ _ _ h + | ut _ _ i0 _ _ h' => exact .ut _ _ _ _ _ (le_trans h' h) + +theorem DerivA.ne_null3 {a b : capability.Cap} (h : DerivA a b) (ha : a ≠ .Null) : b ≠ .Null := by + cases h <;> first | exact ha | (intro e; cases e) + +/-- `d` is `Null` or derived from `c`. -/ +def DerNA (d c : capability.Cap) : Prop := d ≠ .Null → DerivA d c + +theorem DerNA.trans {a b c : capability.Cap} (h1 : DerNA a b) (h2 : DerNA b c) : DerNA a c := fun ha => + (h1 ha).trans3 (h2 ((h1 ha).ne_null3 ha)) + +set_option hygiene false in +/-- Closes `DerNA` of a concrete result against a concrete argument. -/ +local macro "drn" : tactic => `(tactic| ( + first + | exact fun h => absurd rfl h + | exact fun _ => .refl _ + | exact fun _ => .ep .. + | exact fun _ => .ntfn .. + | exact fun _ => .reply .. + | exact fun _ => .cnode .. + | exact fun _ => .frame ..)) + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +theorem capability.mask_cap_rights.dr (r : capability.Rights) (c : capability.Cap) : + capability.mask_cap_rights r c ⦃ m => DerNA m c ⦄ := by + cases c + case Endpoint p b sd rc g gr => + cases sd <;> cases rc <;> cases g <;> cases gr <;> + simp only [capability.mask_cap_rights, Bool.false_eq_true, ↓reduceIte, bind_ok] <;> + exact (WP.spec_ok _).2 (by drn) + case Notification p b sd rc => + cases sd <;> cases rc <;> + simp only [capability.mask_cap_rights, Bool.false_eq_true, ↓reduceIte, bind_ok] <;> + exact (WP.spec_ok _).2 (by drn) + case Reply p g => + cases g <;> simp only [capability.mask_cap_rights, Bool.false_eq_true, ↓reduceIte] <;> + exact (WP.spec_ok _).2 (by drn) + case Frame p sz rt dv mp ep asd va => + simp only [capability.mask_cap_rights] + exact WP.spec_bind (P0.capability.mask_vm_rights.p0 rt r) fun _ _ => (WP.spec_ok _).2 (by drn) + all_goals (simp only [capability.mask_cap_rights]; exact (WP.spec_ok _).2 (by drn)) + +theorem objects.update_cap_data.dr (pr : Bool) (data : U64) (c : capability.Cap) : + objects.update_cap_data pr data c ⦃ m => DerNA m c ⦄ := by + cases c + case Endpoint p b sd rc g gr => + simp only [objects.update_cap_data] + split + · exact (WP.spec_ok _).2 (by drn) + · split <;> exact (WP.spec_ok _).2 (by drn) + case Notification p b sd rc => + simp only [objects.update_cap_data] + split + · exact (WP.spec_ok _).2 (by drn) + · split <;> exact (WP.spec_ok _).2 (by drn) + case CNode p rd gs g => + simp only [objects.update_cap_data] + refine WP.spec_bind (Pₘ := fun _ => True) (by first | p0_auto | p0_split) fun _ _ => ?_ + refine WP.spec_bind (Pₘ := fun _ => True) (by first | p0_auto | p0_split) fun _ _ => ?_ + split + · exact (WP.spec_ok _).2 (by drn) + · refine WP.spec_bind (Pₘ := fun _ => True) (by first | p0_auto | p0_split) fun _ _ => ?_ + refine WP.spec_bind (Pₘ := fun _ => True) (by first | p0_auto | p0_split) fun _ _ => ?_ + refine WP.spec_bind (Pₘ := fun _ => True) (by first | p0_auto | p0_split) fun _ _ => ?_ + exact (WP.spec_ok _).2 (by drn) + all_goals (simp only [objects.update_cap_data]; exact (WP.spec_ok _).2 (by drn)) + +set_option hygiene false in +local macro "dr_ok" : tactic => `(tactic| ( + refine (WP.spec_ok _).2 fun nc h => ?_ + cases h + drn)) + +set_option maxHeartbeats 1000000 in +theorem objects.derive_cap.dr (s : S) (slot : U64) (c : capability.Cap) : + objects.derive_cap st s slot c ⦃ r => ∀ nc, r = .Ok nc → DerNA nc c ⦄ := by + have hS : P0.StoreTotal st := L.total + cases c + case Untyped p b i d => + simp only [objects.derive_cap] + refine WP.spec_bind (P0.cdt.ensure_no_children.p0 st s slot hS) fun r _ => ?_ + rcases r with _ | e + · simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok] + dr_ok + · simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame.from, + bind_tc_ok] + first + | exact (WP.spec_ok _).2 fun nc h => (by cases h) + | (simp only [WP.spec_ok]; intro nc h; cases h) + case PageTable p m a v => + cases m + · simp only [objects.derive_cap, Bool.false_eq_true, ↓reduceIte] + exact (WP.spec_ok _).2 fun nc h => (by cases h) + · simp only [objects.derive_cap, ↓reduceIte] + dr_ok + case PageDirectory p m a v => + cases m + · simp only [objects.derive_cap, Bool.false_eq_true, ↓reduceIte] + exact (WP.spec_ok _).2 fun nc h => (by cases h) + · simp only [objects.derive_cap, ↓reduceIte] + dr_ok + case Pdpt p m a v => + cases m + · simp only [objects.derive_cap, Bool.false_eq_true, ↓reduceIte] + exact (WP.spec_ok _).2 fun nc h => (by cases h) + · simp only [objects.derive_cap, ↓reduceIte] + dr_ok + case Pml4 p m a => + cases m + · simp only [objects.derive_cap, Bool.false_eq_true, ↓reduceIte] + exact (WP.spec_ok _).2 fun nc h => (by cases h) + · simp only [objects.derive_cap, ↓reduceIte] + dr_ok + case Ept p l m a v => + cases m + · simp only [objects.derive_cap, Bool.false_eq_true, ↓reduceIte] + exact (WP.spec_ok _).2 fun nc h => (by cases h) + · simp only [objects.derive_cap, ↓reduceIte] + dr_ok + case HighVSpace p m a => + cases m + · simp only [objects.derive_cap, Bool.false_eq_true, ↓reduceIte] + exact (WP.spec_ok _).2 fun nc h => (by cases h) + · simp only [objects.derive_cap, ↓reduceIte] + dr_ok + all_goals (simp only [objects.derive_cap]; dr_ok) + +/-- **Copy / Mint's new cap is derived from the source** (module docstring). -/ +theorem cnode.copied_cap.derivA3 {s : S} {lbl len buf src : U64} {c nc : capability.Cap} + (h : cnode.copied_cap st s lbl len buf src c = ok (.Ok (nc, false))) (hn : nc ≠ .Null) : DerivA nc c := by + have w := cnode.copied_cap.uaG (st := st) (R := DerNA) (fun h1 h2 => DerNA.trans h1 h2) + (fun r c => capability.mask_cap_rights.dr r c) (fun pr d c => objects.update_cap_data.dr pr d c) + (fun s sl c => objects.derive_cap.dr s sl c) s lbl len buf src c + rw [h, WP.spec_ok] at w + exact w nc rfl hn + +/-! ## The decode's successful path -/ + +variable {A : Type} {ar : machine.Arch A} +variable {Clock : A → Fl4SpecA64.MachineState → Prop} + +theorem errPairA {α : Type} {e : error.Exc} {x : α} {y : α} + (h : (ok (core.result.Result.Err e, x) : Result (core.result.Result Unit error.Exc × α)) = + ok (core.result.Result.Ok (), y)) : False := by + have := okinj h + simp at this + +theorem errTripleA {α β : Type} {e : error.Exc} {x x' : α} {y y' : β} + (h : (ok (core.result.Result.Err e, x, y) : Result (core.result.Result Unit error.Exc × α × β)) = + ok (core.result.Result.Ok (), x', y')) : False := by + have := okinj h + simp at this + +set_option maxHeartbeats 4000000 in +/-- **What a successful CNode Copy / Mint decode ran** (module docstring). -/ +theorem decCnodeCopy_invA {s s' : S} {a a' : A} {lbl length slot buf p r gs g : U64} {block call donate : Bool} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hlb : 24 ≤ lbl.val ∧ lbl.val ≤ 27) + (h : syscall.decode_invocation st ar s a lbl length slot (.CNode p r gs g) block call donate false buf extra = + ok (core.result.Result.Ok (), s', a')) : + ¬ length < 4#u64 ∧ ∃ (index depth dest x0 si sdp src : U64) (root nc : capability.Cap) (mv : Bool), + msg.syscall_arg st s (L.curW s) buf 0#u64 = ok index ∧ + msg.syscall_arg st s (L.curW s) buf 1#u64 = ok depth ∧ + cspace.lookup_target_slot st s (.CNode p r gs g) index depth = ok (.Ok dest) ∧ + Aeneas.Std.Array.index_usize extra 0#usize = ok x0 ∧ x0 ≠ 0#u64 ∧ + msg.syscall_arg st s (L.curW s) buf 2#u64 = ok si ∧ + msg.syscall_arg st s (L.curW s) buf 3#u64 = ok sdp ∧ + obj.Slot.cap st x0 s = ok root ∧ + cdt.ensure_empty_slot st s dest = ok (.Ok ()) ∧ + cspace.lookup_source_slot st s root si sdp = ok (.Ok src) ∧ + capA st s src ≠ .Null ∧ + cnode.copied_cap st s lbl length buf src (capA st s src) = ok (.Ok (nc, mv)) ∧ + nc ≠ .Null := by + have h21 := lblA_revoke; have h28 := lblA_rotate; have h24 := lblA_copy; have h27 := lblA_mutate + have hlo : ¬ lbl < fl4_abi.label.CNodeRevoke := by scalar_tac + have hhi : ¬ lbl > fl4_abi.label.CNodeRotate := by scalar_tac + have g1 : lbl ≥ fl4_abi.label.CNodeCopy := by scalar_tac + have g2 : lbl ≤ fl4_abi.label.CNodeMutate := by scalar_tac + rw [decodeCnode_programA, if_neg Bool.false_ne_true] at h + unfold cnode.decode at h + rw [if_neg hlo, if_neg hhi] at h + by_cases hl2 : length < 2#u64 + · rw [if_pos hl2] at h + simp only [error.truncated, bind_ok, bind_tc_ok] at h + exact (errTripleA h).elim + rw [if_neg hl2, cur_thread_ok, bind_ok] at h + obtain ⟨index, hi, h⟩ := bind_eq_ok h + obtain ⟨depth, hd, h⟩ := bind_eq_ok h + obtain ⟨rr, hres, h⟩ := bind_eq_ok h + rcases rr with dest | e + swap + · simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame.from, + bind_tc_ok] at h + exact (errTripleA h).elim + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok] at h + rw [if_pos g1, if_pos g2] at h + obtain ⟨⟨r1, s1⟩, hcp, h⟩ := bind_eq_ok h + have e1 : r1 = .Ok () := by + simp only [uncurry_apply_pair] at h + have := okinj h + simp only [Prod.mk.injEq] at this + exact this.1 + subst e1 + clear h + unfold cnode.decode_copy at hcp + by_cases hl4 : length < 4#u64 + · rw [if_pos hl4] at hcp + simp only [error.truncated, bind_ok, bind_tc_ok] at hcp + exact (errPairA hcp).elim + rw [if_neg hl4] at hcp + obtain ⟨x0, hx0, hcp⟩ := bind_eq_ok hcp + obtain ⟨b, hb, hcp⟩ := bind_eq_ok hcp + cases b + swap + · rw [if_pos rfl] at hcp + simp only [error.truncated, bind_ok, bind_tc_ok] at hcp + exact (errPairA hcp).elim + have h0 : x0 ≠ 0#u64 := by + intro e + unfold obj.Slot.is_null at hb + have := okinj hb + simp [e] at this + rw [if_neg Bool.false_ne_true, cur_thread_ok, bind_ok] at hcp + obtain ⟨si, h2, hcp⟩ := bind_eq_ok hcp + obtain ⟨sdp, h3, hcp⟩ := bind_eq_ok hcp + obtain ⟨root, hroot, hcp⟩ := bind_eq_ok hcp + obtain ⟨re, hemp, hcp⟩ := bind_eq_ok hcp + rcases re with u | e + swap + · simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame.from, + bind_tc_ok] at hcp + exact (errPairA hcp).elim + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok] at hcp + obtain ⟨rs, hsrc, hcp⟩ := bind_eq_ok hcp + rcases rs with src | e + swap + · simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame.from, + bind_tc_ok] at hcp + exact (errPairA hcp).elim + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok] at hcp + obtain ⟨c, hc, hcp⟩ := bind_eq_ok hcp + have ec : c = capA st s src := okinj (hc.symm.trans (capA_eq (st := st) s src)) + subst ec + obtain ⟨b1, hb1, hcp⟩ := bind_eq_ok hcp + cases b1 + swap + · rw [if_pos rfl] at hcp + simp only [cnode.missing_source, bind_ok, bind_tc_ok] at hcp + exact (errPairA hcp).elim + have hsn : capA st s src ≠ .Null := by + intro e + rw [e] at hb1 + simp only [capability.Cap.is_null] at hb1 + have := okinj hb1 + cases this + rw [if_neg Bool.false_ne_true] at hcp + obtain ⟨rc, hcop, hcp⟩ := bind_eq_ok hcp + rcases rc with ⟨nc, mv⟩ | e + swap + · simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame.from, + bind_tc_ok] at hcp + exact (errPairA hcp).elim + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok, uncurry_apply_pair] at hcp + obtain ⟨b2, hb2, hcp⟩ := bind_eq_ok hcp + cases b2 + swap + · rw [if_pos rfl] at hcp + simp only [error.illegal, bind_ok, bind_tc_ok] at hcp + exact (errPairA hcp).elim + have hnc : nc ≠ .Null := by + intro e + rw [e] at hb2 + simp only [capability.Cap.is_null] at hb2 + have := okinj hb2 + cases this + exact ⟨hl4, index, depth, dest, x0, si, sdp, src, root, nc, mv, hi, hd, hres, hx0, h0, h2, h3, hroot, + by cases u; exact hemp, hsrc, hsn, hcop, hnc⟩ + +end + +/-! ## Facts of caps of one shape -/ + +theorem shape1_of3A (c : capability.Cap) : + Fl4ProofsA64.P1.W7.R.ST.shapeA (Fl4ProofsA64.P3.shapeA c) = Fl4ProofsA64.P1.W7.R.ST.shapeA c := by + cases c <;> rfl + +theorem capEqA_not_ptA {c : capability.Cap} {a : Fl4SpecA64.Cap} (h : CapEqA c a) + (hne : ∀ x, a ≠ .archObjectCap x) : absCapA c = a := by + unfold CapEqA at h + split at h + · exact absurd rfl (hne _) + · exact h + +theorem irqH_of_shapeA {d c : capability.Cap} (h : Fl4ProofsA64.P3.shapeA d = Fl4ProofsA64.P3.shapeA c) (i : U64) : + d = .IrqHandler i ↔ c = .IrqHandler i := by + constructor + · rintro rfl + cases c <;> simp [Fl4ProofsA64.P3.shapeA] at h ⊢ + exact h.symm + · rintro rfl + cases d <;> simp [Fl4ProofsA64.P3.shapeA] at h ⊢ + exact h + +/-! ## Move / Mutate's new cap, and an empty destination -/ + +/-- `update_cap_data true`'s non-null result is the cap, or a `CNode` cap with another guard. -/ +def GuardEqA (d c : capability.Cap) : Prop := + d = c ∨ ∃ p r a b a' b', c = .CNode p r a b ∧ d = .CNode p r a' b' + +theorem updCapData_guardA (data : U64) (c : capability.Cap) : + objects.update_cap_data true data c ⦃ m => m = .Null ∨ GuardEqA m c ⦄ := by + cases c + case Endpoint p b sd rc g gr => + simp only [objects.update_cap_data] + split + · exact (WP.spec_ok _).2 (Or.inl rfl) + · rename_i h; exact absurd trivial h + case Notification p b sd rc => + simp only [objects.update_cap_data] + split + · exact (WP.spec_ok _).2 (Or.inl rfl) + · rename_i h; exact absurd trivial h + case CNode p rd gs g => + simp only [objects.update_cap_data] + refine WP.spec_bind (Pₘ := fun _ => True) (by first | p0_auto | p0_split) fun _ _ => ?_ + refine WP.spec_bind (Pₘ := fun _ => True) (by first | p0_auto | p0_split) fun _ _ => ?_ + split + · exact (WP.spec_ok _).2 (Or.inl rfl) + · refine WP.spec_bind (Pₘ := fun _ => True) (by first | p0_auto | p0_split) fun _ _ => ?_ + refine WP.spec_bind (Pₘ := fun _ => True) (by first | p0_auto | p0_split) fun _ _ => ?_ + refine WP.spec_bind (Pₘ := fun _ => True) (by first | p0_auto | p0_split) fun _ _ => ?_ + exact (WP.spec_ok _).2 (Or.inr (Or.inr ⟨_, _, _, _, _, _, rfl, rfl⟩)) + all_goals (simp only [objects.update_cap_data]; exact (WP.spec_ok _).2 (Or.inr (Or.inl rfl))) + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **Move / Mutate's new cap**: the source, or Mutate's `update_cap_data` of it. -/ +theorem copiedMove_formA {s : S} {lbl len buf src : U64} {c nc : capability.Cap} {mv : Bool} + (hl : lbl = fl4_abi.label.CNodeMove ∨ lbl = fl4_abi.label.CNodeMutate) + (h : cnode.copied_cap st s lbl len buf src c = ok (.Ok (nc, mv))) : + nc = c ∨ ∃ data, objects.update_cap_data true data c = ok nc := by + have h1 : ¬ lbl = fl4_abi.label.CNodeCopy := by + rcases hl with e | e <;> subst e <;> (rw [u64_eq_iffA]; simp) + have h2 : ¬ lbl = fl4_abi.label.CNodeMint := by + rcases hl with e | e <;> subst e <;> (rw [u64_eq_iffA]; simp) + unfold cnode.copied_cap at h + rw [cur_thread_ok, bind_ok, if_neg h1, if_neg h2] at h + by_cases h3 : lbl = fl4_abi.label.CNodeMove + · rw [if_pos h3] at h + have := okinj h + simp only [core.result.Result.Ok.injEq, Prod.mk.injEq] at this + exact Or.inl this.1.symm + rw [if_neg h3] at h + by_cases h5 : len < 5#u64 + · rw [if_pos h5] at h + simp only [error.truncated] at h + have := okinj h + cases this + rw [if_neg h5] at h + obtain ⟨data, -, h⟩ := bind_eq_ok h + obtain ⟨c', hu, h⟩ := bind_eq_ok h + have := okinj h + simp only [core.result.Result.Ok.injEq, Prod.mk.injEq] at this + exact Or.inr ⟨data, this.1 ▸ hu⟩ + +/-- An empty-slot check that succeeded read `Null`. -/ +theorem emptySlot_nullA {s : S} {x : U64} (h : cdt.ensure_empty_slot st s x = ok (.Ok ())) : + capA st s x = .Null := by + unfold cdt.ensure_empty_slot at h + rw [capA_eq, bind_ok] at h + obtain ⟨b, hb, h⟩ := bind_eq_ok h + cases b + · rw [if_neg Bool.false_ne_true] at h + have := okinj h + cases this + · revert hb + cases capA st s x <;> intro hb <;> simp only [capability.Cap.is_null] at hb <;> + first | rfl | (have := okinj hb; cases this) + +end + +/-! ## The arm inputs -/ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {Rj : Set U64 → JSlotA S} {ar : machine.Arch A} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} + +/-- **The two slots a CNode copy or move names**, typed, with the source's spec cap. -/ +structure CnSlotsA (st : machine.Store S) [L : LawfulStore st] (Z : XSetsA) (OS : Set U64) (s : S) + (σ : Fl4SpecA64.State Unit) (dest src : U64) where + ps : U64 + pd : U64 + ss : Fl4SpecA64.CslotPtr + sd : Fl4SpecA64.CslotPtr + srcA : Fl4SpecA64.Cap + esrc : src = cslotAddrA ss + edest : dest = cslotAddrA sd + hCs : ss.1 = specPtr ps ∧ Z.th ps = some (.cnode ss.2.length) + hCd : sd.1 = specPtr pd ∧ Z.th pd = some (.cnode sd.2.length) + hpdn : ¬ IrqNodeA (L.ksb s) pd + hsO : cslotAddrA ss ∈ OS + hdO : cslotAddrA sd ∈ OS + hsca : CapRelA (capA st s (cslotAddrA ss)) srcA + hspec : specCapAtA σ ss = some srcA + +set_option maxHeartbeats 4000000 in +/-- The two slots from the decode's lookups and the record. -/ +theorem JSyscallA.cnSlotsA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st Rj) s a σ m) + {info : msginfo.MessageInfo} {buf slot : U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} + {mi : Fl4SpecA64.MessageInfo} {sl : Fl4SpecA64.CslotPtr} {c : Fl4SpecA64.Cap} + {excaps : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)} {buffer : Option Fl4SpecA64.ObjRef} {argsU : List U64} + (Q : SiteA st Z OS s σ info buf slot extra mi sl c excaps buffer argsU) + {p r gs g index depth dest x0 si sdp src : U64} {root : capability.Cap} + (hslc : capA st s slot = .CNode p r gs g) + (hres : cspace.lookup_target_slot st s (.CNode p r gs g) index depth = ok (.Ok dest)) + (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) (h0 : x0 ≠ 0#u64) + (hroot : obj.Slot.cap st x0 s = ok root) + (hsrc : cspace.lookup_source_slot st s root si sdp = ok (.Ok src)) : + Nonempty (CnSlotsA st Z OS s σ dest src) := by + have F := Q.F + have hR := J.j.1.st + have hV : SlotsValidA Z.th (L.mem s) := J.j.1.cv + have hP := J.j.1.place + -- the destination + have hc : CapRelA (.CNode p r gs g) c := hslc ▸ F.cap + have hcv : CapValidA Z.th (.CNode p r gs g) := by + have h := hV _ (F.pin.habs.isSlot hP) + rw [capAtA_eq (st := st), ← F.pin.hslot, hslc] at h + exact h + have hresA := hres + unfold cspace.lookup_target_slot at hresA + obtain ⟨r1, hr1, f1, -, hrel1⟩ := spec_existsA + (lookup_slot_for_cnode_op_refinesA Z.th s σ hR hV false _ c hc hcv index depth) + have er1 : r1 = .Ok dest := okinj (hr1.symm.trans hresA) + subst er1 + cases f1 with + | error _ => exact hrel1.elim + | ok sd => + obtain ⟨hd1, pd, hpd1, hpd, hCn⟩ := hrel1 + have hAd : AbsSlotA Z.th sd := ⟨pd, hpd1, Or.inl hpd⟩ + have hpdn : ¬ IrqNodeA (L.ksb s) pd := noIrq_of_cnRefA hP hV hcv hCn + have edest : dest = cslotAddrA sd := hd1 + subst edest + -- the first extra cap + have ex0 : x0 = exAtA extra 0 := okinj (hx0.symm.trans (exAtA_idx0 extra)) + obtain ⟨ac0, exSl, erest, hexc, hac0⟩ : ∃ ac0 exSl erest, excaps = (ac0, exSl) :: erest ∧ CapRelA root ac0 := by + cases hcs : excaps with + | nil => + exact absurd (ex0.trans (F.extra.2.2 0 (by simp [hcs]) (by omega))) h0 + | cons y rest' => + obtain ⟨ps, -, -, -, hrel⟩ := F.extra.2.1 0 (by simp [hcs]) + refine ⟨y.1, y.2, rest', rfl, ?_⟩ + simp only [hcs, List.getElem_cons_zero] at hrel + rw [← ex0, capAtA_eq (st := st)] at hrel + rwa [show root = capA st s x0 from okinj (hroot.symm.trans (capA_eq (st := st) s x0))] + have hx0v : CapValidA Z.th root := by + obtain ⟨ps, hth, hsp, hxa, -⟩ := F.extra.2.1 0 (by rw [hexc]; simp) + rw [show root = capA st s x0 from okinj (hroot.symm.trans (capA_eq (st := st) s x0))] + exact hV x0 (by + rw [ex0, hxa] + exact AbsSlotA.isSlot hP ⟨ps, hsp, Or.inl hth⟩) + -- the source + have hsrcA := hsrc + rw [show cspace.lookup_source_slot st s root si sdp = cspace.lookup_slot_for_cnode_op st s true root si sdp from + rfl] at hsrcA + obtain ⟨r2, hr2, f2, -, hrel2⟩ := spec_existsA + (lookup_slot_for_cnode_op_refinesA Z.th s σ hR hV true root ac0 hac0 hx0v si sdp) + have er2 : r2 = .Ok src := okinj (hr2.symm.trans hsrcA) + subst er2 + cases f2 with + | error _ => exact hrel2.elim + | ok ss => + obtain ⟨hs1, ps, hps1, hps, -⟩ := hrel2 + have hAs : AbsSlotA Z.th ss := ⟨ps, hps1, Or.inl hps⟩ + have esrc : src = cslotAddrA ss := hs1 + subst esrc + have hsO : cslotAddrA ss ∈ OS := J.j5.2.1 _ (hAs.isSlot hP) + have hdO : cslotAddrA sd ∈ OS := J.j5.2.1 _ (hAd.isSlot hP) + -- the source in the spec + obtain ⟨cs, hk, hwfc, hsl⟩ := hR.cnode ps _ hps + obtain ⟨srcA, hcsA, hsrel⟩ := hsl ss.2 rfl + have hsca : CapRelA (capA st s (cslotAddrA ss)) srcA := by + obtain ⟨a0, h, hg⟩ := hR.slot_capA hps1 hps + have hg' : Fl4SpecA64.getCap (Z := Unit) ss σ = ({(srcA, σ)}, False) := by + have : ss = (specPtr ps, ss.2) := by rw [← hps1] + rw [this]; exact getCap_cnodeA hk hwfc hcsA + have e := congrArg Prod.fst (hg.symm.trans hg') + simp only [Set.singleton_eq_singleton_iff, Prod.mk.injEq, and_true] at e + rw [capAtA_eq (st := st), e] at h + exact h + have hspec : specCapAtA σ ss = some srcA := by + unfold specCapAtA + rw [hps1, hk] + simp [hwfc, hcsA] + exact ⟨{ ps := ps, pd := pd, ss := ss, sd := sd, srcA := srcA, esrc := rfl, edest := rfl, + hCs := ⟨hps1, hps⟩, hCd := ⟨hpd1, hpd⟩, hpdn := hpdn, hsO := hsO, hdO := hdO, hsca := hsca, + hspec := hspec }⟩ + +/-- **A cap of the source's shape installs no idle cap**: the source's spec cap is none (`SIdleA`). -/ +theorem noIdle_of_shapeA {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} {n : Nat} + (hi : SIdleA σ m n) {ss : Fl4SpecA64.CslotPtr} {src nc : capability.Cap} {srcA : Fl4SpecA64.Cap} + (hsca : CapRelA src srcA) (hspec : specCapAtA σ ss = some srcA) + (hsh : Fl4ProofsA64.P1.W7.R.ST.shapeA nc = Fl4ProofsA64.P1.W7.R.ST.shapeA src) : + ∀ anc, CapRelA nc anc → NoHiPtV anc → NoIdleCapV m n anc := by + intro anc hanc hnh + have key : ∀ x : Fl4SpecA64.Cap, anc = x → (∀ y, x ≠ .archObjectCap y) → + ((∃ t, x = .threadCap t) ∨ ∃ q b, x = .schedContextCap q b) → specCapAtA σ ss = some x := by + intro x ex hne hx + subst ex + have e1 : absCapA nc = anc := capEqA_not_ptA hanc.2 hne + have e2 : src = nc := by + rcases hx with ⟨t, e⟩ | ⟨q, b, e⟩ <;> rw [e] at e1 <;> + cases nc <;> simp [absCapA, absFrameE] at e1 <;> (try split at e1) <;> (try cases e1) <;> + cases src <;> simp [Fl4ProofsA64.P1.W7.R.ST.shapeA] at hsh ⊢ <;> simp_all + have e3 : absCapA src = srcA := + CapEqA.eq_abs hsca.2 (fun q asid va h => by + rw [e2] at h; rw [h] at e1 + exact hne _ (e1.symm.trans rfl)) + rw [hspec, ← e3, e2, e1] + refine ⟨fun c0 hc0 e => ?_, fun b e => ?_, hnh⟩ + · exact (hi.f6 ss).1 c0 hc0 (key _ e (fun _ h => by cases h) (Or.inl ⟨_, rfl⟩)) + · exact (hi.f6 ss).2.1 b (key _ e (fun _ h => by cases h) (Or.inr ⟨_, _, rfl⟩)) + +set_option maxHeartbeats 4000000 in +/-- **CNode Copy / Mint's arm inputs from the run and the record** (module docstring). -/ +theorem JSyscallA.cnCopy_armA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st Rj) s a σ m) + {info : msginfo.MessageInfo} {buf slot : U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} + {mi : Fl4SpecA64.MessageInfo} {sl : Fl4SpecA64.CslotPtr} {c : Fl4SpecA64.Cap} + {excaps : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)} {buffer : Option Fl4SpecA64.ObjRef} {argsU : List U64} + (Q : SiteA st Z OS s σ info buf slot extra mi sl c excaps buffer argsU) + {p r gs g : U64} (hslc : capA st s slot = .CNode p r gs g) + (hlb : info.label = fl4_abi.label.CNodeCopy ∨ info.label = fl4_abi.label.CNodeMint) + {bB bC bD : Bool} {s' : S} {a' : A} + (hdec : syscall.decode_invocation st ar s a info.label (msgLenA info buf) slot (capA st s slot) bB bC bD false + buf extra = ok (core.result.Result.Ok (), s', a')) : + Nonempty (CnCopyArmA st Z OS n s σ m buf slot info extra) := by + rw [hslc] at hdec + have h24 : 24 ≤ info.label.val ∧ info.label.val ≤ 27 := by + have := lblA_copy; have := lblA_mint + rcases hlb with e | e <;> rw [e] <;> omega + obtain ⟨hl4, index, depth, dest, x0, si, sdp, src, root, nc, mv, hi, hd0, hres, hx0, h0, h2, h3, hroot, hemp, + hsrc, hsn, hcop, hn⟩ := decCnodeCopy_invA h24 hdec + have hmv : mv = false := by + have w := cnode.copied_cap.wf (st := st) s info.label (msgLenA info buf) buf src (capA st s src) hlb + (by rw [← capAtA_eq (st := st)]; exact CapRt.decodeF_encWf _ _) + rw [hcop, WP.spec_ok] at w + exact (w nc mv rfl).1 + subst hmv + have F := Q.F + have hP := J.j.1.place + have hO : OSOK H.tcbs H.omega H.frame OS := J.sys.ent.os + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA Q.hext Q.hOS Q.cov J.actW J.running J.cur + have hwin := RS.pl.choose_spec.choose_spec.choose_spec.choose_spec.win + obtain ⟨X⟩ := J.cnSlotsA Q hslc hres hx0 h0 hroot hsrc + obtain ⟨ps, pd, ss, sd, srcA, esrc, edest, ⟨hps1, hps⟩, ⟨hpd1, hpd⟩, hpdn, hsO, hdO, hsca, hspec⟩ := X + subst esrc edest + have hAs : AbsSlotA Z.th ss := ⟨ps, hps1, Or.inl hps⟩ + -- the new cap + have h4 : 4 ≤ argsU.length := by + have := F.len + have : ¬ (msgLenA info buf).val < (4#u64 : U64).val := fun h => hl4 (by scalar_tac) + have : (4#u64 : U64).val = 4 := rfl + omega + obtain ⟨y0, y1, y2, y3, rest, rfl⟩ : ∃ y0 y1 y2 y3 rest, argsU = y0 :: y1 :: y2 :: y3 :: rest := by + match argsU, h4 with + | y0 :: y1 :: y2 :: y3 :: rest, _ => exact ⟨y0, y1, y2, y3, rest, rfl⟩ + have h24 : 24 ≤ info.label.val ∧ info.label.val ≤ 27 := by + have := lblA_copy; have := lblA_mint + rcases hlb with e | e <;> rw [e] <;> omega + set slbl := specLabelWord (fl4LabelOf mi.miLabel) with hslbl + have hg : genInvocationType slbl = cnodeLabelOfA info.label := by + have key : ∀ (v : Nat) (g : GenInvocationLabels), info.label.val = v → labNA v = .spec (.genInvocationLabel g) → + genInvocationType slbl = g := fun v g hv e => by + rw [hslbl, F.lab]; exact cnLabel_specA hv e + have := lblA_copy; have := lblA_mint + rcases hlb with e | e + · have hv : info.label.val = 24 := by rw [e]; assumption + rw [key 24 .cnodeCopy hv (by decide)]; simp [cnodeLabelOfA, hv] + · have hv : info.label.val = 25 := by rw [e]; assumption + rw [key 25 .cnodeMint hv (by decide)]; simp [cnodeLabelOfA, hv] + have hlen' : (msgLenA info buf).val = rest.length + 4 := by simpa using F.len + have hargs' : ∀ (i : U64) v, rest[i.val - 4]? = some v → 4 ≤ i.val → + msg.syscall_arg st s (L.curW s) buf i = ok v := by + intro i v hv hi4 + apply F.args i v + have : i.val = (i.val - 4) + 4 := by omega + rw [this]; simpa using hv + have hnc0 : isUtA (capA st s (cslotAddrA ss)) = true → NoChildrenRelA (st := st) s σ (cslotAddrA ss) ss := by + intro hu + obtain ⟨q, b, i, ee, hcu⟩ := isUtA_ut hu + exact noChildren_of_adjA hP J.sys.ent.os (J.j.1.cdt.tree.grow J.j5.2.1 (fun x hx hn => J.j5.2.2 x hx hn)) + J.j.1.cdt hAs hsO hcu (J.sys.sysl.sys.stz.1.2.1 _ hsO hu) + obtain ⟨r3, hr3, R, -, hrel3⟩ := copied_relA s σ info.label slbl hg h24 (msgLenA info buf) buf (L.curW s) + (cur_thread_ok s) rest hlen' hargs' ss sd _ hsca.1 (CapRt.decodeF_encWf _ _) hnc0 + have er3 : r3 = .Ok (nc, false) := okinj (hr3.symm.trans hcop) + subst er3 + obtain ⟨-, hwf, hew, hval, hsh', hut', hok'⟩ := hrel3 + have hsh := hsh' hn + have hok := hok' rfl hn + have hsh1 : Fl4ProofsA64.P1.W7.R.ST.shapeA nc = + Fl4ProofsA64.P1.W7.R.ST.shapeA (capA st s (cslotAddrA ss)) := by + rw [← shape1_of3A nc, hsh, shape1_of3A] + have hbd1 : badgeA nc = badgeA (capA st s (cslotAddrA ss)) ∨ badgeA (capA st s (cslotAddrA ss)) = 0#u64 := by + by_cases hz : badgeA (capA st s (cslotAddrA ss)) = 0#u64 + · exact Or.inr hz + left + have key : ∀ src : capability.Cap, Fl4ProofsA64.P3.shapeA nc = Fl4ProofsA64.P3.shapeA src → + (∀ r b rs, (absCapA src = .endpointCap r b rs ∨ absCapA src = .notificationCap r b rs) → b ≠ 0 → + capEpBadge (absCapA nc) = b) → ¬ badgeA src = 0#u64 → badgeA nc = badgeA src := by + intro src hsh hbd hz + cases src <;> (try exact absurd rfl hz) + case Endpoint p0 b0 x y z w => + cases nc <;> simp [Fl4ProofsA64.P3.shapeA] at hsh + case Endpoint p1 b1 x1 y1 z1 w1 => + have hb : b0.bv ≠ 0 := fun e => hz (UScalar.eq_equiv_bv_eq _ _ |>.2 e) + have h := hbd _ _ _ (Or.inl rfl) hb + simp only [absCapA, capEpBadge] at h + exact (UScalar.eq_equiv_bv_eq _ _).2 h + case Notification p0 b0 x y => + cases nc <;> simp [Fl4ProofsA64.P3.shapeA] at hsh + case Notification p1 b1 x1 y1 => + have hb : b0.bv ≠ 0 := fun e => hz (UScalar.eq_equiv_bv_eq _ _ |>.2 e) + have h := hbd _ _ _ (Or.inr rfl) hb + simp only [absCapA, capEpBadge] at h + exact (UScalar.eq_equiv_bv_eq _ _).2 h + exact key _ hsh hok.2.1 hz + have hsci := noIdle_of_shapeA J.sidle hsca hspec hsh1 + have hut : isUtA (capA st s (cslotAddrA ss)) = true → ∀ y, ¬ Fl4Proofs.P3.Mdb.Desc Z.G.par y (cslotAddrA ss) := by + have w := cnode.copied_cap.noDesc hO (J.j.1.cdt.tree.grow J.j5.2.1 (fun x hx hn => J.j5.2.2 x hx hn)) + J.sys.sysl.sys.stz.1.2.1 hsO info.label (msgLenA info buf) buf + rw [hcop, WP.spec_ok] at w + exact w nc rfl + exact ⟨{ + ps := ps, pd := pd, nc := nc, p := p, r := r, gs := gs, g := g, index := index, depth := depth, x0 := x0, + si := si, sdp := sdp, root := root, ss := ss, sd := sd, + hCs := ⟨hps1, hps⟩, hCd := ⟨hpd1, hpd⟩, hsn := hsn, + hdn := emptySlot_nullA hemp, + hdc := OSOK.canon hO hwin hdO, hw := hew, hn := hn, + hD := cnode.copied_cap.derivA3 hcop hn, + hal := UA.of_shapeA hsh (fun hu => ut_of_absA hsh hu (hut' hu hn)), + hk := sbk_of_shapeA hsh, hsh := hsh, hsh1 := hsh1, hbd1 := hbd1, hbd := hok.2.1, + hirq := irqH_of_shapeA hsh, + hz := fun q t k e => hok.1 (specPtr q) (zombieBitsA t) k.val (by rw [e]; rfl), + hut := hut, + hdo := Or.inl (by rw [hpd1, fl4Ptr_specPtr]; exact hpdn), + hcv := hval Z.th, hslc := hslc, hlb := hlb, hl4 := hl4, + hl2 := fun h => hl4 (by scalar_tac), + hi := hi, hd0 := hd0, hres := hres, hx0 := hx0, h0 := h0, h2 := h2, h3 := h3, hroot := hroot, + hemp := hemp, hsrc := hsrc, hcop := hcop, hsci := hsci }⟩ + +set_option maxHeartbeats 2000000 in +/-- **CNode Copy / Mint / Move / Mutate / Rotate's decode is never preempted**: every error is a system error (`cnode_decode_refinesA`). -/ +theorem JSyscallA.cnCopy_notPreemptedA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st Rj) s a σ m) + {info : msginfo.MessageInfo} {buf slot : U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} + {mi : Fl4SpecA64.MessageInfo} {sl : Fl4SpecA64.CslotPtr} {c : Fl4SpecA64.Cap} + {excaps : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)} {buffer : Option Fl4SpecA64.ObjRef} {argsU : List U64} + (Q : SiteA st Z OS s σ info buf slot extra mi sl c excaps buffer argsU) + {p r gs g : U64} (hslc : capA st s slot = .CNode p r gs g) + (hlb : 24 ≤ info.label.val ∧ info.label.val ≤ 28) + {bB bC bD : Bool} {x : error.Exc} {s' : S} {a' : A} + (hdec : syscall.decode_invocation st ar s a info.label (msgLenA info buf) slot (capA st s slot) bB bC bD false + buf extra = ok (core.result.Result.Err x, s', a')) : x ≠ error.Exc.Preempted := by + have F := Q.F + have hP := J.j.1.place + have hV : SlotsValidA Z.th (L.mem s) := J.j.1.cv + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA Q.hext Q.hOS Q.cov J.actW J.running J.cur + have hc : CapRelA (.CNode p r gs g) c := hslc ▸ F.cap + have hcv : CapValidA Z.th (.CNode p r gs g) := by + have h := hV _ (F.pin.habs.isSlot hP) + rw [capAtA_eq (st := st), ← F.pin.hslot, hslc] at h + exact h + rw [hslc, decodeCnode_programA, if_neg Bool.false_ne_true] at hdec + obtain ⟨slbl, hsl, -⟩ := cnode_canonA info.label + have hex := ExtraOKA.rel2 hP F.extra + have hl' : info.label.val < 21 ∨ 28 < info.label.val ∨ (24 ≤ info.label.val ∧ info.label.val ≤ 27) ∨ + info.label.val = 28 := by + omega + have w := cnode_decode_refinesA ar J.toJStage3A.toJEntA (J.toJStage3A.sysPlace Q.hext Q.hOS Q.cov) + RS.sym RS.run RS.cur RS.vs RS.vc (J.j.1.cdt.tree.grow J.j5.2.1 (fun x hx hn => J.j5.2.2 x hx hn)) J.j5.2.1 + J.j5.2.2 info.label slbl hsl hl' (msgLenA info buf) buf argsU F.len F.args _ c hc hcv (exAtA extra 0) + (exAtA extra 1) (exAtA_idx0 extra) (exAtA_idx1 extra) _ hex + rw [hdec, WP.spec_ok] at w + obtain ⟨h, -⟩ := w + obtain ⟨-, e', -, he⟩ := h + intro hx + subst hx + cases e' <;> exact he + +set_option maxHeartbeats 4000000 in +/-- **CNode Move / Mutate's scope member from the run and the record** (module docstring). -/ +theorem JSyscallA.cnodeMove_runA (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + {info : msginfo.MessageInfo} {buf slot : U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} + {mi : Fl4SpecA64.MessageInfo} {sl : Fl4SpecA64.CslotPtr} {c : Fl4SpecA64.Cap} + {excaps : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)} {buffer : Option Fl4SpecA64.ObjRef} {argsU : List U64} + (Q : SiteA st Z OS s σ info buf slot extra mi sl c excaps buffer argsU) + {p r gs g : U64} (hslc : capA st s slot = .CNode p r gs g) + (hlb : info.label = fl4_abi.label.CNodeMove ∨ info.label = fl4_abi.label.CNodeMutate) + {bB bC bD : Bool} {s' : S} {a' : A} + (hdec : syscall.decode_invocation st ar s a info.label (msgLenA info buf) slot (capA st s slot) bB bC bD false + buf extra = ok (core.result.Result.Ok (), s', a')) : + ∃ Out, DispScope4A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ m Clock hPl.ipa info.label + (msgLenA info buf) slot bB bC bD false buf extra Out := by + have hdec0 := hdec + rw [hslc] at hdec + have h24 : 24 ≤ info.label.val ∧ info.label.val ≤ 27 := by + have := lblA_move; have := lblA_mutate + rcases hlb with e | e <;> rw [e] <;> omega + obtain ⟨hl4, index, depth, dest, x0, si, sdp, src, root, nc, mv, hi, hd0, hres, hx0, h0, h2, h3, hroot, hemp, + hsrc, hsn, hcop, hn⟩ := decCnodeCopy_invA h24 hdec + have hwsrc : CapRt.EncWf (capA st s src) := by + rw [← capAtA_eq (st := st)]; exact CapRt.decodeF_encWf _ _ + have w := cnode.copied_cap.mv (st := st) s info.label (msgLenA info buf) buf src (capA st s src) hlb hwsrc + rw [hcop, WP.spec_ok] at w + obtain ⟨hmv, -, hw⟩ := w nc mv rfl + subst hmv + have hP := J.j.1.place + have hO : OSOK H.tcbs H.omega H.frame OS := J.sys.ent.os + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA Q.hext Q.hOS Q.cov J.actW J.running J.cur + have hwin := RS.pl.choose_spec.choose_spec.choose_spec.choose_spec.win + obtain ⟨X⟩ := J.cnSlotsA Q hslc hres hx0 h0 hroot hsrc + obtain ⟨ps, pd, ss, sd, srcA, esrc, edest, ⟨hps1, hps⟩, ⟨hpd1, hpd⟩, hpdn, hsO, hdO, hsca, hspec⟩ := X + subst esrc edest + have hAs : AbsSlotA Z.th ss := ⟨ps, hps1, Or.inl hps⟩ + have hform : GuardEqA nc (capA st s (cslotAddrA ss)) ∧ DerivA nc (capA st s (cslotAddrA ss)) ∧ + UA nc (capA st s (cslotAddrA ss)) ∧ + Fl4ProofsA64.P1.W7.R.ST.shapeA nc = Fl4ProofsA64.P1.W7.R.ST.shapeA (capA st s (cslotAddrA ss)) ∧ + badgeA nc = badgeA (capA st s (cslotAddrA ss)) ∧ + ∀ i, nc = .IrqHandler i ↔ capA st s (cslotAddrA ss) = .IrqHandler i := by + rcases copiedMove_formA hlb hcop with e | ⟨data, hu⟩ + · rw [e] + exact ⟨Or.inl rfl, .refl _, UA.refl _, rfl, rfl, fun _ => Iff.rfl⟩ + · have g := updCapData_guardA data (capA st s (cslotAddrA ss)) + rw [hu, WP.spec_ok] at g + rcases g with e | g + · exact absurd e hn + · exact ⟨g, updCapData_true_okA hu hn⟩ + obtain ⟨hg, hD, hal, hsh1, hbd, hirq⟩ := hform + have hsh3 : Fl4ProofsA64.P3.shapeA nc = Fl4ProofsA64.P3.shapeA (capA st s (cslotAddrA ss)) := by + rcases hg with e | ⟨_, _, _, _, _, _, e1, e2⟩ + · rw [e] + · rw [e1, e2]; rfl + have hcv : CapValidA Z.th (capA st s (cslotAddrA ss)) → CapValidA Z.th nc := by + intro h + rcases hg with e | ⟨_, _, _, _, _, _, e1, e2⟩ + · rw [e]; exact h + · rw [e1] at h; rw [e2]; exact h + have hE := J.toJStage3A.toJEntA + have hno : CapObjOkA Z nc := by + have h := hE.j.capObj (hAs.isSlot hP) + rw [capAtA_eq (st := st)] at h + exact h.of_shapeA hsh3 hcv + have hok : ∀ ac, CapRelA nc ac → CapOkA σ.interruptIrqNode sd ac := by + intro ac hac + have hdo : ¬ IrqNodeA (L.ksb s) (fl4Ptr sd.1) ∨ IrqCapA (absCapA nc) := + Or.inl (by rw [hpd1, fl4Ptr_specPtr]; exact hpdn) + have hoff : NodeOffA (L.ksb s) nc := (NodeOffA.of_ctOff (hE.sys.sysl.ct _ hsO)).of_shapeA hsh3 + have h00 := capOk_insA hE.j.1.irq hdo hoff + cases nc with + | PageTable p0 m0 asid va => + cases m0 + · rw [← CapEqA.eq_abs hac.2 (fun _ _ _ h => by cases h)]; exact h00 + · obtain ⟨_, _, rfl, _, _⟩ := CapEqA.pt_mapped_iff.mp hac.2 + refine ⟨fun irq hirq0 hsl => ?_, fun ⟨_, _, _, _, e⟩ => by cases e⟩ + have hi0 := h00.1 irq hirq0 hsl + simp [IrqCapA, absCapA] at hi0 + | _ => rw [← CapEqA.eq_abs hac.2 (fun _ _ _ h => by cases h)]; exact h00 + refine J.cnodeMove_scopeA hA hM hPl hfm Q (lbl := info.label) (bB := bB) (bC := bC) (bD := bD) ss sd + ⟨hps1, hps⟩ ⟨hpd1, hpd⟩ hsn ?_ (OSOK.canon hO hwin hdO) hw hn hD hal hsh1 hbd hirq hno hslc hlb hl4 + (fun h => hl4 (by scalar_tac)) hi hd0 hres hx0 h0 h2 h3 hroot hemp hsrc hcop hok + (noIdle_of_shapeA J.sidle hsca hspec hsh1) rfl + exact emptySlot_nullA hemp + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3CnInsA.lean b/proofs/Fl4ProofsA64/P3/Stage3CnInsA.lean new file mode 100644 index 000000000..465143ba4 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3CnInsA.lean @@ -0,0 +1,290 @@ +/- +P3 (AArch64, merge line, D8 step 5 (iii)): **the MDB slot and the cap relation across `cdt::insert_with_cap`** +(CNode Copy / Mint). + +* `insWC_bitsA`: `insert_with_cap` and the revocable / first-badged bits, exactly: every slot but `dest` keeps both, + `dest` gets `is_cap_revocable` in both (`set_nf_rvE`, `set_nf_fbE`). +* `J5A.insA`: the concrete J5 slot across the insertion — the first **`InsCtxA`** instance (A64 P1's + `InsCtxA.inv`), `IrqRunA.ins`, `HandlersActiveA.ins_derived` — at the ghost with `dest` after `src`, FL4's parent + bit `isPFA`. +* `sbp_eqA` (x86 `MdbSpec.sbp_eq`): FL4's parent bit is l4v's `should_be_parent_of` for a new cap of the source's + shape and badge kind, so the ghost of `cap_insert_refinesA` (`insBA`) is J5's. +* `capInsert_outA`: every outcome of `cap_insert` (`set_untyped_cap_as_full`, `set_cap`, the CDT and the flag). +* `CapsFromA.of_insA`: the slot caps after an insertion, derived both ways (the source's untyped index grows to + full, A64 P1's `UtInvA.fib`). + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3ArmCnSwapA +import Fl4ProofsA64.P3.InsDisA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add +local notation "FBB" => (562949953421312#u64 : U64) +local notation "RV" => (281474976710656#u64 : U64) + +theorem u64_beqA (x y : U64) : (x == y) = decide (x.bv = y.bv) := by + rw [Bool.eq_iff_iff]; simp [UScalar.eq_equiv_bv_eq] + +/-- **FL4's parent bit is l4v's `should_be_parent_of`** (module docstring; x86 `sbp_eq`). -/ +theorem sbp_eqA (sc nc : capability.Cap) (io : Bool) (hsh : Fl4ProofsA64.P3.shapeA nc = Fl4ProofsA64.P3.shapeA sc) + (hsr : Fl4SpecA64.sameRegionAs (absCapA sc) (absCapA nc)) (hss : sraA sc nc = true) + (hk : SameBadgeKindA nc sc) (hw : CapWfA nc) : + decide (Fl4SpecA64.shouldBeParentOf (absCapA sc) io (absCapA nc) + (decide (Fl4SpecA64.isCapRevocable (absCapA nc) (absCapA sc)))) = isPFA io sc nc (revFA nc sc) := by + have hrv : decide (Fl4SpecA64.isCapRevocable (absCapA nc) (absCapA sc)) = revFA nc sc := by + have := revFA_spec nc sc hk hw + cases h : revFA nc sc <;> simp_all + rw [hrv] + unfold Fl4SpecA64.shouldBeParentOf isPFA + rw [hss] + simp only [eq_true hsr, true_and, Bool.and_true] + cases sc <;> cases nc <;> simp [Fl4ProofsA64.P3.shapeA] at hsh + all_goals (try casesm* _ ∧ _) + all_goals (try subst_vars) + all_goals cases io <;> (try simp only [absCapA, absFrameE, isBadgedA, ↓reduceIte, Bool.false_eq_true]) <;> + (try split_ifs) <;> simp [isBadgedA, badgeA, + Fl4SpecA64.capEpBadge, UScalar.eq_equiv_bv_eq, u64_beqA, Fl4SpecA64.shouldBeArchParentOf, + Fl4SpecA64.ArchCap.isSGISignalCap, Fl4SpecA64.ArchCap.isSMCCap] + +/-- **Every outcome of `cap_insert`** at a source whose `get_cap` is `asc` (module docstring). -/ +theorem capInsert_outA {σ σ' : Fl4SpecA64.State Unit} {a asc : Fl4SpecA64.Cap} {ss sd : Fl4SpecA64.CslotPtr} + (hgs : (getCap (Z := Unit) ss σ).1 = {(asc, σ)}) (hgd : ∀ x ∈ (getCap (Z := Unit) sd σ).1, x.2 = σ) + (h : ((), σ') ∈ (Fl4SpecA64.capInsert (Z := Unit) a ss sd σ).1) : + ∃ σa σb, ∃ v : Bool, (σa = σ ∨ ((), σa) ∈ (setCap (Z := Unit) (maxFreeIndexUpdate asc) ss σ).1) ∧ + ((), σb) ∈ (setCap (Z := Unit) a sd σa).1 ∧ + σ'.cdt = Function.update σb.cdt sd (if v then some ss else σb.cdt ss) ∧ + σ'.isOriginalCap = Function.update σb.isOriginalCap sd (decide (Fl4SpecA64.isCapRevocable a asc)) ∧ + σ'.kheap = σb.kheap := by + unfold Fl4SpecA64.capInsert at h + obtain ⟨x, hx, h⟩ := mem_bindA.1 h + rw [hgs] at hx + obtain rfl : x = (asc, σ) := hx + obtain ⟨x, hx, h⟩ := mem_bindA.1 h + obtain rfl : x = (decide (Fl4SpecA64.isCapRevocable a asc), σ) := hx + obtain ⟨⟨c, σx⟩, hx, h⟩ := mem_bindA.1 h + have ex : σx = σ := hgd _ hx + subst ex + obtain ⟨x, hx, h⟩ := mem_bindA.1 h + obtain ⟨-, rfl⟩ := hx + obtain ⟨⟨_, σa⟩, h1, h⟩ := mem_bindA.1 h + obtain ⟨⟨_, σb⟩, h2, h⟩ := mem_bindA.1 h + have h1' : σa = σx ∨ ((), σa) ∈ (setCap (Z := Unit) (maxFreeIndexUpdate asc) ss σx).1 := by + unfold Fl4SpecA64.setUntypedCapAsFull at h1 + split at h1 + · exact Or.inr h1 + · have e : ((), σa) = ((), σx) := h1 + exact Or.inl (Prod.mk.inj e).2 + obtain ⟨x, hx, h⟩ := mem_bindA.1 h + obtain rfl : x = (σb.isOriginalCap, σb) := hx + obtain ⟨x, hx, h⟩ := mem_bindA.1 h + obtain rfl : x = (decide (Fl4SpecA64.shouldBeParentOf asc (σb.isOriginalCap ss) a + (decide (Fl4SpecA64.isCapRevocable a asc))), σb) := hx + obtain ⟨x, hx, h⟩ := mem_bindA.1 h + obtain rfl : x = (σb.cdt ss, σb) := hx + obtain ⟨x, hx, h⟩ := mem_bindA.1 h + obtain rfl : x = (σb.cdt sd, σb) := hx + obtain ⟨x, hx, h⟩ := mem_bindA.1 h + unfold updateCdt at hx + obtain ⟨y, hy, hx⟩ := mem_bindA.1 hx + obtain rfl : y = (σb.cdt, σb) := hy + unfold setCdt at hx + obtain ⟨z, hz, hx⟩ := mem_bindA.1 hx + obtain rfl : z = (σb, σb) := hz + obtain rfl : x = _ := hx + obtain ⟨x, hx, h⟩ := mem_bindA.1 h + unfold doExtendedOp at hx + obtain ⟨y, hy, hx⟩ := mem_bindA.1 hx + obtain rfl : y = _ := hy + obtain ⟨z, hz, hx⟩ := mem_bindA.1 hx + have ez := hz.2 + obtain rfl : x = _ := hx + rw [ez] at h + unfold setOriginal at h + obtain ⟨y, hy, h⟩ := mem_bindA.1 h + obtain rfl : y = _ := hy + have e := (Prod.mk.inj h).2 + subst e + exact ⟨σa, σb, _, h1', h2, rfl, rfl, rfl⟩ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {T Ω Sg OS : Set U64} + +set_option maxHeartbeats 2000000 in +/-- **`insert_with_cap` and the two flag bits, exactly** (module docstring). -/ +theorem insWC_bitsA (hO : OSOK T Ω Sg OS) {s : S} {src dest : U64} {nc sc : capability.Cap} + (hsrc : src ∈ OS) (hd : dest ∈ OS) (hsd : src ≠ dest) (hsc : capA st s src = sc) (hw : CapRt.EncWf nc) + (hnN : NxA st s src ≠ 0#u64 → NxA st s src ∈ OS) : + cdt.insert_with_cap st s nc sc src dest ⦃ s' => + (∀ y ∈ OS, y ≠ dest → Fl4ProofsA64.P1.W7.R.ST.revA st s' y = Fl4ProofsA64.P1.W7.R.ST.revA st s y ∧ fbA st s' y = fbA st s y) ∧ + Fl4ProofsA64.P1.W7.R.ST.revA st s' dest = revFA nc sc ∧ fbA st s' dest = revFA nc sc ⦄ := by + unfold cdt.insert_with_cap + rw [revFA_eq, bind_ok, next_eq, bind_ok] + refine WP.spec_bind (cdt.set_untyped_cap_as_full.w2 (nc := nc) hsc) fun s1 h1 => ?_ + have K1 : ∀ y ∈ OS, L.mem s1 (wa y 24#u64) = L.mem s (wa y 24#u64) := by + rcases h1 with rfl | ⟨w0, w1, rfl⟩ + · exact fun _ _ => rfl + · exact fun y hy => (set_cap_links hO hsrc y hy).2 + obtain ⟨w0, w1, he, -⟩ := CapRt.decode_encode nc hw + rw [set_cap_eq he, bind_ok] + have K2 : ∀ y ∈ OS, L.mem (L.wr (L.wr s1 dest w0) (wa dest 8#u64) w1) (wa y 24#u64) = L.mem s (wa y 24#u64) := + fun y hy => (set_cap_links hO hd y hy).2.trans (K1 y hy) + generalize L.wr (L.wr s1 dest w0) (wa dest 8#u64) w1 = s2 at K2 ⊢ + rw [set_prev_eq, bind_ok] + have K3 : ∀ y ∈ OS, L.mem (L.wr s2 (wa dest 16#u64) src) (wa y 24#u64) = L.mem s (wa y 24#u64) := + fun y hy => (w24_wr16 hO s2 hd hy src).trans (K2 y hy) + generalize L.wr s2 (wa dest 16#u64) src = s3 at K3 ⊢ + refine WP.spec_bind (WP.spec_and (set_nf_rvE s3 dest _ (revFA nc sc) (revFA nc sc)) + (set_nf_fbE s3 dest _ (revFA nc sc) (revFA nc sc))) ?_ + rintro s4 ⟨⟨w4, rfl, hr4⟩, ⟨w4', e4, hf4⟩⟩ + have ew : w4' = w4 := by + have := congrArg (fun t => L.mem t (wa dest 24#u64)) e4 + simp only [w24_self hO s3 hd] at this + exact this.symm + rw [ew] at hf4 + have hr4' : w4 &&& RV = (if revFA nc sc then RV else 0#u64) := hr4 + rw [set_next_eq, bind_ok] + have K5 : ∀ y ∈ OS, y ≠ dest → (L.mem (L.wr (L.wr s3 (wa dest 24#u64) w4) (wa src 24#u64) + ((L.mem (L.wr s3 (wa dest 24#u64) w4) (wa src 24#u64) &&& ~~~ obj.MDB_PTR_MASK) ||| + (dest &&& obj.MDB_PTR_MASK))) (wa y 24#u64) &&& RV = L.mem s (wa y 24#u64) &&& RV) ∧ + (L.mem (L.wr (L.wr s3 (wa dest 24#u64) w4) (wa src 24#u64) + ((L.mem (L.wr s3 (wa dest 24#u64) w4) (wa src 24#u64) &&& ~~~ obj.MDB_PTR_MASK) ||| + (dest &&& obj.MDB_PTR_MASK))) (wa y 24#u64) &&& FBB = L.mem s (wa y 24#u64) &&& FBB) := by + intro y hy e + by_cases ys : y = src + · rw [ys, w24_self hO _ hsrc, w24_wr24 hO s3 hd hsrc hsd, K3 src hsrc] + exact ⟨ptr_rvb _ _, ptr_bitsFA _ _ _ _ (by unfold obj.MDB_PTR_MASK; rfl) (by unfold obj.MDB_PTR_MASK; rfl)⟩ + · rw [w24_wr24 hO _ hsrc hy ys, w24_wr24 hO s3 hd hy e, K3 y hy] + exact ⟨rfl, rfl⟩ + have D5 : L.mem (L.wr (L.wr s3 (wa dest 24#u64) w4) (wa src 24#u64) + ((L.mem (L.wr s3 (wa dest 24#u64) w4) (wa src 24#u64) &&& ~~~ obj.MDB_PTR_MASK) ||| + (dest &&& obj.MDB_PTR_MASK))) (wa dest 24#u64) = w4 := by + rw [w24_wr24 hO _ hsrc hd hsd.symm, w24_self hO s3 hd] + generalize L.wr (L.wr s3 (wa dest 24#u64) w4) (wa src 24#u64) _ = s5 at K5 D5 ⊢ + simp only [obj.Slot.is_null, bind_ok] + have fin : ∀ s6 : S, (∀ y ∈ OS, L.mem s6 (wa y 24#u64) = L.mem s5 (wa y 24#u64)) → + (∀ y ∈ OS, y ≠ dest → Fl4ProofsA64.P1.W7.R.ST.revA st s6 y = Fl4ProofsA64.P1.W7.R.ST.revA st s y ∧ fbA st s6 y = fbA st s y) ∧ + Fl4ProofsA64.P1.W7.R.ST.revA st s6 dest = revFA nc sc ∧ fbA st s6 dest = revFA nc sc := by + intro s6 h6 + refine ⟨fun y hy e => ⟨?_, ?_⟩, ?_, ?_⟩ + · show (L.mem s6 (wa y 24#u64) &&& RV != 0#u64) = (L.mem s (wa y 24#u64) &&& RV != 0#u64) + rw [h6 y hy, (K5 y hy e).1] + · show (L.mem s6 (wa y 24#u64) &&& FBB != 0#u64) = (L.mem s (wa y 24#u64) &&& FBB != 0#u64) + rw [h6 y hy, (K5 y hy e).2] + · show (L.mem s6 (wa dest 24#u64) &&& RV != 0#u64) = revFA nc sc + rw [h6 dest hd, D5, hr4'] + cases revFA nc sc <;> rfl + · show (L.mem s6 (wa dest 24#u64) &&& FBB != 0#u64) = revFA nc sc + rw [h6 dest hd, D5, hf4] + cases revFA nc sc <;> rfl + split + · exact (WP.spec_ok _).2 (fin s5 fun _ _ => rfl) + · rename_i hn0 + have hN0 : NxA st s src ≠ 0#u64 := by simpa using hn0 + rw [set_prev_eq, WP.spec_ok] + exact fin _ fun y hy => w24_wr16 hO s5 (hnN hN0) hy _ + +/-- **The concrete J5 slot across `cdt::insert_with_cap`** (module docstring): the first `InsCtxA` instance. -/ +theorem J5A.insA {Z : XSetsA} {H : Heap} {s : S} {σ σ' : Fl4SpecA64.State Unit} {m m' : Fl4SpecA64.McState} + (hO : OSOK H.tcbs H.omega H.frame OS) (J : J5A st Z H OS s σ m) + (hT : TreeA st OS Z.G.chains Z.G.par s) {src dest : U64} (hsrc : src ∈ OS) (hd : dest ∈ OS) + (hSs : IsSlotA Z.th src) (hSd : IsSlotA Z.th dest) + (hsn : capA st s src ≠ .Null) (hdn : capA st s dest = .Null) (hdc : obj.sext48 dest = ok dest) + {nc : capability.Cap} (hw : CapRt.EncWf nc) + (hsh : Fl4ProofsA64.P1.W7.R.ST.shapeA nc = Fl4ProofsA64.P1.W7.R.ST.shapeA (capA st s src)) + (hbd : badgeA nc = badgeA (capA st s src) ∨ badgeA (capA st s src) = 0#u64) + (hirq : ∀ i, nc = .IrqHandler i ↔ capA st s src = .IrqHandler i) + (hut : isUtA (capA st s src) = true → ∀ y, ¬ Fl4Proofs.P3.Mdb.Desc Z.G.par y src) + (hzb : isZbA (capA st s src) = false) + (hst : ∀ s', WritesIn st s s' (W7.CapW H.tcbs {b | InOS OS b} ∅) → + ∀ i : U64, i.val < 5376 → L.mem s' (irqStAddrA (L.ksb s) i) = L.mem s (irqStAddrA (L.ksb s) i)) : + cdt.insert_with_cap st s nc (capA st s src) src dest ⦃ s' => + WritesIn st s s' (W7.CapW H.tcbs {b | InOS OS b} ∅) ∧ capA st s' dest = nc ∧ + FullA (capA st s' src) (capA st s src) ∧ (∀ y ∈ OS, y ≠ src → y ≠ dest → capA st s' y = capA st s y) ∧ + J5A st { Z with G := ⟨insG src dest Z.G.chains, insParA Z.G.par src dest + (isPFA (Fl4ProofsA64.P1.W7.R.ST.revA st s src) (capA st s src) nc (revFA nc (capA st s src)))⟩ } H OS s' σ' m' ⦄ := by + have F := hT.lk.lkF hO.nz + have inOS : ∀ y ∈ Z.G.chains.flatten, y ∈ OS := fun y hy => ((hT.lk.members y).1 hy).1 + have hsG : src ∈ Z.G.chains.flatten := (hT.lk.members _).2 ⟨hsrc, hsn⟩ + have hdG : dest ∉ Z.G.chains.flatten := fun h => ((hT.lk.members _).1 h).2 hdn + have hsd : src ≠ dest := fun e => hdG (e ▸ hsG) + have hnN : NxA st s src ≠ 0#u64 → NxA st s src ∈ OS := fun h => inOS _ (F.nxm _ hsG h) + refine WP.spec_mono (WP.spec_and (WP.spec_and + (cdt.insert_with_cap.lrF (osokA hO) (LR.self s) hsrc hd hsd hdc rfl hsn hw hnN) + (cdt.insert_with_cap.srcF hO hsrc hd hsd rfl hw hnN)) + (insWC_bitsA hO hsrc hd hsd rfl hw hnN)) ?_ + rintro s' ⟨⟨⟨w, hL, ed, -, -, ek, -⟩, hfull⟩, hbk, hrd, hfd⟩ + have hc1 : Fl4ProofsA64.P1.W7.R.ST.shapeA (capA st s' src) = Fl4ProofsA64.P1.W7.R.ST.shapeA (capA st s src) ∧ + badgeA (capA st s' src) = badgeA (capA st s src) := by + rcases hfull with e | ⟨p, b, i, d, i1, h1, h2, -⟩ + · rw [e]; exact ⟨rfl, rfl⟩ + · rw [h1, h2]; exact ⟨rfl, rfl⟩ + have aOS : ∀ a, (a = dest ∨ a ∈ Z.G.chains.flatten) → a ∈ OS := fun a ha => ha.elim (fun e => e ▸ hd) (inOS a) + have C : InsCtxA st OS Z.G.chains Z.G.par s s' src dest nc (capA st s' src) := by + refine ⟨hT.lk, hT.tree, J.1.1, hsG, hdG, fun a ha => ?_, fun a ha => ?_, fun a ha => ?_, fun a ha => ?_, + hsh, hbd, hc1.1, hc1.2, hut, hzb⟩ + · have e := (hL a (aOS a ha)).2 + rw [e] + by_cases e1 : a = dest <;> by_cases e2 : a = src <;> simp [e1, e2, hsd, Ne.symm hsd] + · by_cases e1 : a = dest + · rw [if_pos e1, e1]; exact ed + · rw [if_neg e1] + by_cases e2 : a = src + · rw [if_pos e2, e2] + · rw [if_neg e2]; exact ek a (aOS a ha) e2 e1 + · by_cases e1 : a = dest + · rw [if_pos e1, e1]; exact hrd + · rw [if_neg e1]; exact (hbk a (aOS a ha) e1).1 + · by_cases e1 : a = dest + · rw [if_pos e1, e1]; exact hfd + · rw [if_neg e1]; exact (hbk a (aOS a ha) e1).2 + have hk : ∀ y ∈ Z.G.chains.flatten, ∀ i, capA st s' y = .IrqHandler i ↔ capA st s y = .IrqHandler i := + fun y hy i => by + by_cases e2 : y = src + · rw [e2] + rcases hfull with e | ⟨p, b, ii, d, i1, h1, h2, -⟩ + · rw [e] + · rw [h1, h2]; exact ⟨fun h => (by cases h), fun h => (by cases h)⟩ + · rw [ek y (inOS y hy) e2 (C.dne hy)] + refine ⟨w, ed, hfull, ek, ⟨C.inv, ?_, ?_⟩, J.2.1, fun y hy hns => ?_⟩ + · exact Fl4ProofsA64.P1.W7.R.ST.IrqRunA.ins J.1.2.1 hT.lk.nodup hsG hdG hk + (fun i h => Or.inl ((hirq i).1 (ed.symm.trans h))) (fun i h => ed.trans ((hirq i).2 h)) + · exact Fl4ProofsA64.P1.W7.R.ST.HandlersActiveA.ins_derived J.1.2.2 hsG (fun y hy i h => (hk y hy i).1 h) + (fun i h => (hirq i).1 (ed.symm.trans h)) w.1 (hst s' w) + · have f1 : y ≠ src := fun e => hns (e ▸ hSs) + have f2 : y ≠ dest := fun e => hns (e ▸ hSd) + rw [ek y hy f1 f2]; exact J.2.2 y hy hns + +/-- **The slot caps after an insertion**: `dest` holds a cap derived from the source's, the source its cap or that +untyped at full index. -/ +theorem CapsFromA.of_insA {s s' : S} {src dest : U64} (hs : src ∈ OS) (hd : dest ∈ OS) + {nc : capability.Cap} (hD : Fl4ProofsA64.P3.DerivA nc (capA st s src)) (hdn : capA st s dest = .Null) + (ed : capA st s' dest = nc) (hf : FullA (capA st s' src) (capA st s src)) + (hfi : ∀ p b i d, capA st s src = .Untyped p b i d → i.val ≤ 2 ^ b.val) + (ek : ∀ y ∈ OS, y ≠ src → y ≠ dest → capA st s' y = capA st s y) : CapsFromA st OS s s' := by + have hDs : Fl4ProofsA64.P3.DerivA (capA st s' src) (capA st s src) := by + rcases hf with e | ⟨p, b, i, d, i1, h1, h2, hi⟩ + · rw [e]; exact .refl _ + · rw [h1, h2]; exact .ut p b i i1 d (by have := hfi p b i d h1; omega) + refine ⟨fun u hu => ?_, fun v hv => ?_⟩ + · by_cases e1 : u = src + · exact Or.inr ⟨src, hs, by rw [e1]; exact hDs⟩ + by_cases e2 : u = dest + · exact Or.inr ⟨src, hs, by rw [e2, ed]; exact hD⟩ + exact Or.inr ⟨u, hu, by rw [ek u hu e1 e2]; exact .refl _⟩ + · by_cases e1 : v = src + · exact Or.inr ⟨src, hs, by rw [e1]; exact hDs⟩ + by_cases e2 : v = dest + · exact Or.inl (e2 ▸ hdn) + exact Or.inr ⟨v, hv, by rw [ek v hv e1 e2]; exact .refl _⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3CnMoveA.lean b/proofs/Fl4ProofsA64/P3/Stage3CnMoveA.lean new file mode 100644 index 000000000..5f61a67c6 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3CnMoveA.lean @@ -0,0 +1,236 @@ +/- +P3 (AArch64, merge line, D8 step 5 (iii)): **the MDB slot and the cap relation across `cdt::move_cap`**. + +* `moveCap_fbA`: `move_cap` and the first-badged bit, exactly (the twin of `P3/MoveA.moveCap_bitA`, bit 48). +* `CapsFromA.of_moveA`: the slot caps after a move are derived from those before (`P3/Stage3CapDerivA`). +* `J5A.moveA`: the concrete J5 slot across a move, at the ghost `mvGA` (`MdbInvA.ren_swp`, `IrqRunA.ren`, + `HandlersActiveA.ren`), for a new cap of the source's shape, badge and IRQ. + +Both are for the CNode Move arm and, twice, the Rotate arm. No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3CapDerivA +import Fl4ProofsA64.P3.MoveA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add +local notation "FBB" => (562949953421312#u64 : U64) + +theorem nf_bitsFA (w n r f M P R : U64) (hP : P &&& R = 0#u64) (hr : r &&& R = 0#u64) (hM : ~~~M &&& R = 0#u64) : + ((((w &&& ~~~M) ||| (n &&& P)) ||| r) ||| f) &&& R = f &&& R := by + rw [UScalar.eq_equiv_bv_eq] at hP hr hM ⊢ + apply BitVec.eq_of_getLsbD_eq; intro i + have h1 := congrArg (fun x => BitVec.getLsbD x i) hP + have h2 := congrArg (fun x => BitVec.getLsbD x i) hr + have h3 := congrArg (fun x => BitVec.getLsbD x i) hM + simp only [UScalar.bv_and, UScalar.bv_or, UScalar.bv_not, BitVec.getLsbD_and, BitVec.getLsbD_or, + BitVec.getLsbD_not] at h1 h2 h3 ⊢ + revert h1 h2 h3 + cases w.bv.getLsbD i <;> cases n.bv.getLsbD i <;> cases r.bv.getLsbD i <;> cases f.bv.getLsbD i <;> + cases M.bv.getLsbD i <;> cases P.bv.getLsbD i <;> cases R.bv.getLsbD i <;> (try simp) <;> omega + +theorem ptr_bitsFA (a n M R : U64) (h : M &&& R = 0#u64) (h' : ~~~(~~~M) &&& R = 0#u64) : + ((a &&& ~~~M) ||| (n &&& M)) &&& R = a &&& R := by + rw [UScalar.eq_equiv_bv_eq] at h ⊢ + apply BitVec.eq_of_getLsbD_eq; intro i + have h1 := congrArg (fun x => BitVec.getLsbD x i) h + simp only [UScalar.bv_and, UScalar.bv_or, UScalar.bv_not, BitVec.getLsbD_and, BitVec.getLsbD_or, + BitVec.getLsbD_not] at h1 ⊢ + revert h1 + have : i < 64 → True := fun _ => trivial + by_cases hi : i < 64 + · cases a.bv.getLsbD i <;> cases n.bv.getLsbD i <;> cases R.bv.getLsbD i <;> cases M.bv.getLsbD i <;> + simp [hi] + · simp [BitVec.getLsbD_of_ge _ i (by simpa using hi)] + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **`set_next_flags` writes the first-badged bit = `first_badged`.** -/ +theorem set_nf_fbE (s : S) (x n : U64) (r f : Bool) : + obj.Slot.set_next_flags st x s n r f ⦃ s' => ∃ w, s' = L.wr s (wa x 24#u64) w ∧ + w &&& FBB = (if f then FBB else 0#u64) ⦄ := by + unfold obj.Slot.set_next_flags obj.Slot.next_word + simp only [lift, bind_ok, cte_next, L.read_eq, fb_val, rvb_val] + cases r <;> cases f <;> + simp only [Bool.false_eq_true, ↓reduceIte, bind_ok, L.write_eq, WP.spec_ok] <;> + (refine ⟨_, rfl, ?_⟩; rw [nf_bitsFA] <;> first | rfl | (unfold obj.MDB_PTR_MASK; rfl)) + +variable {T Ω Sg OS : Set U64} + +variable (st) in +/-- The first-badged bit of every `OS` slot's `NEXT` word agrees between `s` and `s'`. -/ +def BitFA (OS : Set U64) (s s' : S) : Prop := + ∀ y ∈ OS, L.mem s' (wa y 24#u64) &&& FBB = L.mem s (wa y 24#u64) &&& FBB + +theorem linkFbA (hO : OSOK T Ω Sg OS) (s : S) {q : Prop} [Decidable q] {p x : U64} (hp : ¬ q → p ∈ OS) + {f : S → Result S} + (hf : f = (fun s => obj.Slot.set_next st p s x) ∨ f = (fun s => obj.Slot.set_prev st p s x)) : + (if q then ok s else f s) ⦃ s' => BitFA st OS s s' ⦄ := by + by_cases h : q + · rw [if_pos h]; exact (WP.spec_ok _).2 fun _ _ => rfl + · rw [if_neg h] + rcases hf with rfl | rfl + · dsimp only; rw [set_next_eq, WP.spec_ok] + intro y hy + by_cases e : y = p + · rw [e, w24_self hO s (hp h)] + exact ptr_bitsFA _ _ _ _ (by unfold obj.MDB_PTR_MASK; rfl) (by unfold obj.MDB_PTR_MASK; rfl) + · rw [w24_wr24 hO s (hp h) hy e] + · dsimp only; rw [set_prev_eq, WP.spec_ok]; exact fun y hy => by rw [w24_wr16 hO s (hp h) hy] + +set_option maxHeartbeats 2000000 in +/-- **`move_cap` and the first-badged bit**: every slot but the two keeps it, `dest` gets the source's. -/ +theorem moveCap_fbA (hO : OSOK T Ω Sg OS) {s : S} {src dest : U64} {nc : capability.Cap} + (hsrc : src ∈ OS) (hd : dest ∈ OS) (hsd : src ≠ dest) (hw : CapRt.EncWf nc) + (hpP : L.mem s (wa src 16#u64) ≠ 0#u64 → L.mem s (wa src 16#u64) ∈ OS) + (hnN : NxA st s src ≠ 0#u64 → NxA st s src ∈ OS) : + cdt.move_cap st s nc src dest ⦃ s' => + (∀ y ∈ OS, y ≠ src → y ≠ dest → fbA st s' y = fbA st s y) ∧ fbA st s' dest = fbA st s src ⦄ := by + have hS : P0.StoreTotal st := L.total + unfold cdt.move_cap + have hf1 : obj.Slot.first_badged st src s = ok (fbA st s src) := by + unfold obj.Slot.first_badged obj.Slot.next_word fbA + simp only [lift, bind_ok, cte_next, L.read_eq, fb_val] + rw [slot_prev_eq s src, bind_ok, next_eq, bind_ok, revocable_eqA, bind_ok, hf1, bind_ok] + obtain ⟨w0, w1, he, -⟩ := CapRt.decode_encode nc hw + rw [set_cap_eq he, bind_ok] + have K1 : ∀ y ∈ OS, L.mem (L.wr (L.wr s dest w0) (wa dest 8#u64) w1) (wa y 24#u64) = L.mem s (wa y 24#u64) := + fun y hy => (set_cap_links hO hd y hy).2 + generalize L.wr (L.wr s dest w0) (wa dest 8#u64) w1 = s1 at K1 ⊢ + rw [set_prev_eq, bind_ok] + have K2 : ∀ y ∈ OS, L.mem (L.wr s1 (wa dest 16#u64) (L.mem s (wa src 16#u64))) (wa y 24#u64) = + L.mem s (wa y 24#u64) := fun y hy => (w24_wr16 hO s1 hd hy _).trans (K1 y hy) + generalize L.wr s1 (wa dest 16#u64) (L.mem s (wa src 16#u64)) = s2 at K2 ⊢ + refine WP.spec_bind (set_nf_fbE s2 dest _ _ (fbA st s src)) ?_ + rintro s3 ⟨w3, rfl, hw3⟩ + have K3 : ∀ y ∈ OS, y ≠ dest → L.mem (L.wr s2 (wa dest 24#u64) w3) (wa y 24#u64) = L.mem s (wa y 24#u64) := + fun y hy e => (w24_wr24 hO s2 hd hy e w3).trans (K2 y hy) + have D3 : L.mem (L.wr s2 (wa dest 24#u64) w3) (wa dest 24#u64) &&& FBB = + (if fbA st s src then FBB else 0#u64) := by + rw [w24_self hO s2 hd]; exact hw3 + generalize L.wr s2 (wa dest 24#u64) w3 = s3 at K3 D3 ⊢ + rw [clear_eq, bind_ok] + have K4 : ∀ y ∈ OS, y ≠ src → L.mem (L.wr (L.wr (L.wr (L.wr s3 src 0#u64) (wa src 8#u64) 0#u64) + (wa src 16#u64) 0#u64) (wa src 24#u64) 0#u64) (wa y 24#u64) = L.mem s3 (wa y 24#u64) := fun y hy e => by + rw [w24_wr24 hO _ hsrc hy e, w24_wr16 hO _ hsrc hy, (set_cap_links hO hsrc y hy).2] + generalize L.wr (L.wr (L.wr (L.wr s3 src 0#u64) (wa src 8#u64) 0#u64) (wa src 16#u64) 0#u64) + (wa src 24#u64) 0#u64 = s4 at K4 ⊢ + simp only [obj.Slot.is_null, bind_ok] + refine WP.spec_bind (linkFbA hO s4 (p := L.mem s (wa src 16#u64)) (x := dest) ?_ (Or.inl rfl)) + fun s5 h5 => ?_ + · exact fun h => hpP (by simpa using h) + refine WP.spec_mono (linkFbA hO s5 (p := NxA st s src) (x := dest) ?_ (Or.inr rfl)) fun s6 h6 => ?_ + · exact fun h => hnN (by simpa using h) + refine ⟨fun y hy e1 e2 => ?_, ?_⟩ + · unfold fbA; rw [h6 y hy, h5 y hy, K4 y hy e1, K3 y hy e2] + · have e : L.mem s6 (wa dest 24#u64) &&& FBB = (if fbA st s src then FBB else 0#u64) := by + rw [h6 dest hd, h5 dest hd, K4 dest hd hsd.symm]; exact D3 + show (L.mem s6 (wa dest 24#u64) &&& FBB != 0#u64) = fbA st s src + rw [e] + cases fbA st s src <;> rfl + +/-- The slot caps after a move: `dest` holds the new cap (derived from the source's), the source is null. -/ +theorem CapsFromA.of_moveA {s s' : S} {src dest : U64} (hsrc : src ∈ OS) (hd : dest ∈ OS) (hsd : src ≠ dest) + {nc : capability.Cap} (hn : DerivA nc (capA st s src)) (hdn : capA st s dest = .Null) + (ed : capA st s' dest = nc) (es : capA st s' src = .Null) + (ek : ∀ y ∈ OS, y ≠ src → y ≠ dest → capA st s' y = capA st s y) : CapsFromA st OS s s' := by + refine ⟨fun u hu => ?_, fun v hv => ?_⟩ + · by_cases e1 : u = src + · exact Or.inl (e1 ▸ es) + by_cases e2 : u = dest + · exact Or.inr ⟨src, hsrc, by rw [e2, ed]; exact hn⟩ + exact Or.inr ⟨u, hu, by rw [ek u hu e1 e2]; exact .refl _⟩ + · by_cases e1 : v = src + · exact Or.inr ⟨dest, hd, by rw [ed, e1]; exact hn⟩ + by_cases e2 : v = dest + · exact Or.inl (e2 ▸ hdn) + exact Or.inr ⟨v, hv, by rw [ek v hv e1 e2]; exact .refl _⟩ + +/-- **The concrete J5 slot across `cdt::move_cap`** (module docstring). -/ +theorem J5A.moveA {Z : XSetsA} {H : Heap} {s : S} {σ σ' : Fl4SpecA64.State Unit} {m m' : Fl4SpecA64.McState} + (hO : OSOK H.tcbs H.omega H.frame OS) (J : J5A st Z H OS s σ m) + (hT : TreeA st OS Z.G.chains Z.G.par s) {src dest : U64} (hsrc : src ∈ OS) (hd : dest ∈ OS) (hsd : src ≠ dest) + (hSs : IsSlotA Z.th src) (hSd : IsSlotA Z.th dest) + (hsn : capA st s src ≠ .Null) (hdn : capA st s dest = .Null) (hdc : obj.sext48 dest = ok dest) + {nc : capability.Cap} (hw : CapRt.EncWf nc) (hn : nc ≠ .Null) (hal : UA nc (capA st s src)) + (hsh : Fl4ProofsA64.P1.W7.R.ST.shapeA nc = Fl4ProofsA64.P1.W7.R.ST.shapeA (capA st s src)) + (hbd : badgeA nc = badgeA (capA st s src)) + (hirq : ∀ i, nc = .IrqHandler i ↔ capA st s src = .IrqHandler i) + (hst : ∀ s', WritesIn st s s' (W7.CapW H.tcbs {b | InOS OS b} ∅) → + ∀ i : U64, i.val < 5376 → L.mem s' (irqStAddrA (L.ksb s) i) = L.mem s (irqStAddrA (L.ksb s) i)) : + cdt.move_cap st s nc src dest ⦃ s' => + WritesIn st s s' (W7.CapW H.tcbs {b | InOS OS b} ∅) ∧ capA st s' dest = nc ∧ capA st s' src = .Null ∧ + (∀ y ∈ OS, y ≠ src → y ≠ dest → capA st s' y = capA st s y) ∧ + J5A st { Z with G := mvGA Z.G src dest } H OS s' σ' m' ⦄ := by + have F := hT.lk.lkF hO.nz + have inOS : ∀ y ∈ Z.G.chains.flatten, y ∈ OS := fun y hy => ((hT.lk.members y).1 hy).1 + have hsG : src ∈ Z.G.chains.flatten := (hT.lk.members _).2 ⟨hsrc, hsn⟩ + have hdG : dest ∉ Z.G.chains.flatten := fun h => ((hT.lk.members _).1 h).2 hdn + have hpP : L.mem s (wa src 16#u64) ≠ 0#u64 → L.mem s (wa src 16#u64) ∈ OS := + fun h => inOS _ (F.pvm _ hsG h) + have hnN : NxA st s src ≠ 0#u64 → NxA st s src ∈ OS := fun h => inOS _ (F.nxm _ hsG h) + have r1 : ∀ y, y ∈ Z.G.chains.flatten → NxA st s y ≠ dest ∧ L.mem s (wa y 16#u64) ≠ dest := + fun y hy => ⟨fun e => hdG (by rw [← e]; exact F.nxm y hy (by rw [e]; exact hO.nz dest hd)), + fun e => hdG (by rw [← e]; exact F.pvm y hy (by rw [e]; exact hO.nz dest hd))⟩ + refine WP.spec_mono (WP.spec_and (WP.spec_and (WP.spec_and + (cdt.move_cap.lrF (osokA hO) (LR.self s) hsrc hd hsd hdc hw hpP hnN) + (cdt.move_cap.tree hO hT hsG hd hdn hdc hw hn hal)) + (moveCap_bitA hO hsrc hd hsd hw hpP hnN)) + (moveCap_fbA hO hsrc hd hsd hw hpP hnN)) ?_ + rintro s' ⟨⟨⟨⟨w, hLR, ed, es, ek⟩, -⟩, hb, hdb, -⟩, hfk, hfd⟩ + have hrevk : ∀ y ∈ OS, y ≠ src → y ≠ dest → Fl4ProofsA64.P1.W7.R.ST.revA st s' y = Fl4ProofsA64.P1.W7.R.ST.revA st s y := fun y hy e1 e2 => by + unfold Fl4ProofsA64.P1.W7.R.ST.revA + have : L.mem s' (wa y 24#u64) &&& (281474976710656#u64 : U64) = L.mem s (wa y 24#u64) &&& 281474976710656#u64 := + hb y hy e1 e2 + rw [this] + have hrevd : Fl4ProofsA64.P1.W7.R.ST.revA st s' dest = Fl4ProofsA64.P1.W7.R.ST.revA st s src := by + unfold Fl4ProofsA64.P1.W7.R.ST.revA + have : L.mem s' (wa dest 24#u64) &&& (281474976710656#u64 : U64) = + (if ((L.mem s (wa src 24#u64) &&& (281474976710656#u64 : U64)) != 0#u64) then 281474976710656#u64 else 0#u64) := + hdb + rw [this] + cases h : ((L.mem s (wa src 24#u64) &&& (281474976710656#u64 : U64)) != 0#u64) <;> rfl + have sw : ∀ a ∈ Z.G.chains.flatten, (a = src ∧ swp dest src a = dest) ∨ + (a ≠ src ∧ a ≠ dest ∧ swp dest src a = a) := fun a ha => by + have had : a ≠ dest := fun e => hdG (e ▸ ha) + by_cases e : a = src + · exact Or.inl ⟨e, by unfold swp; rw [if_neg had, if_pos e]⟩ + · exact Or.inr ⟨e, had, by unfold swp; rw [if_neg had, if_neg e]⟩ + have capR : ∀ a ∈ Z.G.chains.flatten, Fl4ProofsA64.P1.W7.R.ST.shapeA (capA st s' (swp dest src a)) = + Fl4ProofsA64.P1.W7.R.ST.shapeA (capA st s a) ∧ badgeA (capA st s' (swp dest src a)) = badgeA (capA st s a) ∧ + (∀ i, capA st s' (swp dest src a) = .IrqHandler i ↔ capA st s a = .IrqHandler i) := fun a ha => by + rcases sw a ha with ⟨e, ew⟩ | ⟨e1, e2, ew⟩ + · rw [ew, ed, e]; exact ⟨hsh, hbd, hirq⟩ + · rw [ew, ek a (inOS a ha) e1 e2]; exact ⟨rfl, rfl, fun _ => Iff.rfl⟩ + have M : MdbInvA st (renG dest src Z.G.chains) (Fl4Proofs.P3.Mdb.conjPar (Equiv.swap dest src) Z.G.par) s' := by + refine MdbInvA.ren_swp hT.lk hT.tree J.1.1 (hO.nz dest hd) (hO.nz src hsrc) (fun a ha => ?_) + (fun a ha => ⟨(capR a ha).1, (capR a ha).2.1⟩) (fun a ha => ?_) (fun a ha => ?_) + · have hm : swp dest src a ∈ OS := by + rcases sw a ha with ⟨-, ew⟩ | ⟨-, -, ew⟩ + · rw [ew]; exact hd + · rw [ew]; exact inOS a ha + rw [(hLR _ hm).2] + exact (LkAbs.mv_ok F (x1 := dest) (x2 := src) hdG r1 hsG ha).1 + · rcases sw a ha with ⟨e, ew⟩ | ⟨e1, e2, ew⟩ + · rw [ew, e]; exact hrevd + · rw [ew]; exact hrevk a (inOS a ha) e1 e2 + · rcases sw a ha with ⟨e, ew⟩ | ⟨e1, e2, ew⟩ + · rw [ew, e]; exact hfd + · rw [ew]; exact hfk a (inOS a ha) e1 e2 + refine ⟨w, ed, es, ek, ⟨M, ?_, ?_⟩, J.2.1, fun y hy hns => ?_⟩ + · exact Fl4ProofsA64.P1.W7.R.ST.IrqRunA.ren J.1.2.1 (fun y hy i => (capR y hy).2.2 i) + · exact Fl4ProofsA64.P1.W7.R.ST.HandlersActiveA.ren J.1.2.2 (fun y hy i h => ((capR y hy).2.2 i).1 h) w.1 (hst s' w) + · have e1 : y ≠ src := fun e => hns (e ▸ hSs) + have e2 : y ≠ dest := fun e => hns (e ▸ hSd) + rw [ek y hy e1 e2]; exact J.2.2 y hy hns + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3CnRotFactsA.lean b/proofs/Fl4ProofsA64/P3/Stage3CnRotFactsA.lean new file mode 100644 index 000000000..681c0df97 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3CnRotFactsA.lean @@ -0,0 +1,362 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Plan B): **CNode Rotate's arm inputs from the run and the record** (both the +three-slot form and the swap). + +* `decCnodeRotate_invA`: what a successful `decode_invocation` of CNode Rotate ran. +* `JSyscallA.updCap_factsA`: the facts of a rotated cap, `update_cap_data true` of a slot's cap. +* `JSyscallA.cnodeRotate_runA`: the scope member from `JSyscallA`, the site and the successful decode. + +Named premises: [J] `JSyscallA`. New module. +-/ +import Fl4ProofsA64.P3.Stage3CnArmFactsA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {Clock : A → Fl4SpecA64.MachineState → Prop} + +set_option maxHeartbeats 4000000 in +/-- **What a successful CNode Rotate decode ran** (module docstring). -/ +theorem decCnodeRotate_invA {s s' : S} {a a' : A} {lbl length slot buf p r gs g : U64} {block call donate : Bool} + {extra : Aeneas.Std.Array obj.Slot 3#usize} (hlb : lbl = fl4_abi.label.CNodeRotate) + (h : syscall.decode_invocation st ar s a lbl length slot (.CNode p r gs g) block call donate false buf extra = + ok (core.result.Result.Ok (), s', a')) : + ¬ length < 8#u64 ∧ ∃ (index depth dest x0 x1 pdata sdata src pivot : U64) (ns np : capability.Cap), + msg.syscall_arg st s (L.curW s) buf 0#u64 = ok index ∧ + msg.syscall_arg st s (L.curW s) buf 1#u64 = ok depth ∧ + cspace.lookup_target_slot st s (.CNode p r gs g) index depth = ok (.Ok dest) ∧ + Aeneas.Std.Array.index_usize extra 0#usize = ok x0 ∧ x0 ≠ 0#u64 ∧ + Aeneas.Std.Array.index_usize extra 1#usize = ok x1 ∧ x1 ≠ 0#u64 ∧ + msg.syscall_arg st s (L.curW s) buf 2#u64 = ok pdata ∧ + msg.syscall_arg st s (L.curW s) buf 5#u64 = ok sdata ∧ + cnode.rotate_slots st s buf extra dest = ok (.Ok (src, pivot)) ∧ + objects.update_cap_data true sdata (capA st s src) = ok ns ∧ + objects.update_cap_data true pdata (capA st s pivot) = ok np ∧ + ns ≠ .Null ∧ np ≠ .Null := by + have h21 := lblA_revoke; have h28 := lblA_rotate; have h24 := lblA_copy; have h27 := lblA_mutate + have h22 : fl4_abi.label.CNodeDelete.val = 22 := by simp [fl4_abi.label.CNodeDelete] + have h23 : fl4_abi.label.CNodeCancelBadgedSends.val = 23 := by simp [fl4_abi.label.CNodeCancelBadgedSends] + have hv : lbl.val = 28 := by rw [hlb]; exact h28 + have hlo : ¬ lbl < fl4_abi.label.CNodeRevoke := by scalar_tac + have hhi : ¬ lbl > fl4_abi.label.CNodeRotate := by scalar_tac + have g1 : lbl ≥ fl4_abi.label.CNodeCopy := by scalar_tac + have g2 : ¬ lbl ≤ fl4_abi.label.CNodeMutate := by scalar_tac + have n1 : ¬ lbl = fl4_abi.label.CNodeRevoke := by rw [u64_eq_iffA]; omega + have n2 : ¬ lbl = fl4_abi.label.CNodeDelete := by rw [u64_eq_iffA]; omega + have n3 : ¬ lbl = fl4_abi.label.CNodeCancelBadgedSends := by rw [u64_eq_iffA]; omega + rw [decodeCnode_programA, if_neg Bool.false_ne_true] at h + unfold cnode.decode at h + rw [if_neg hlo, if_neg hhi] at h + by_cases hl2 : length < 2#u64 + · rw [if_pos hl2] at h + simp only [error.truncated, bind_ok, bind_tc_ok] at h + exact (errTripleA h).elim + rw [if_neg hl2, cur_thread_ok, bind_ok] at h + obtain ⟨index, hi, h⟩ := bind_eq_ok h + obtain ⟨depth, hd, h⟩ := bind_eq_ok h + obtain ⟨rr, hres, h⟩ := bind_eq_ok h + rcases rr with dest | e + swap + · simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame.from, + bind_tc_ok] at h + exact (errTripleA h).elim + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok] at h + rw [if_pos g1, if_neg g2, if_neg n1, if_neg n2, if_neg n3, if_pos hlb] at h + obtain ⟨⟨r1, s1⟩, hrt, h⟩ := bind_eq_ok h + have e1 : r1 = .Ok () := by + simp only [uncurry_apply_pair] at h + have := okinj h + simp only [Prod.mk.injEq] at this + exact this.1 + subst e1 + clear h + unfold cnode.decode_rotate at hrt + by_cases hl8 : length < 8#u64 + · rw [if_pos hl8] at hrt + simp only [error.truncated, bind_ok, bind_tc_ok] at hrt + exact (errPairA hrt).elim + rw [if_neg hl8] at hrt + obtain ⟨x0, hx0, hrt⟩ := bind_eq_ok hrt + obtain ⟨b, hb, hrt⟩ := bind_eq_ok hrt + cases b + swap + · rw [if_pos rfl] at hrt + simp only [error.truncated, bind_ok, bind_tc_ok] at hrt + exact (errPairA hrt).elim + have h0 : x0 ≠ 0#u64 := by + intro e + unfold obj.Slot.is_null at hb + have := okinj hb + simp [e] at this + rw [if_neg Bool.false_ne_true] at hrt + obtain ⟨x1, hx1, hrt⟩ := bind_eq_ok hrt + obtain ⟨b1, hb1, hrt⟩ := bind_eq_ok hrt + cases b1 + swap + · rw [if_pos rfl] at hrt + simp only [error.truncated, bind_ok, bind_tc_ok] at hrt + exact (errPairA hrt).elim + have h1 : x1 ≠ 0#u64 := by + intro e + unfold obj.Slot.is_null at hb1 + have := okinj hb1 + simp [e] at this + rw [if_neg Bool.false_ne_true, cur_thread_ok, bind_ok] at hrt + obtain ⟨pdata, h2, hrt⟩ := bind_eq_ok hrt + obtain ⟨sdata, h5, hrt⟩ := bind_eq_ok hrt + obtain ⟨rs, hrot, hrt⟩ := bind_eq_ok hrt + rcases rs with ⟨src, pivot⟩ | e + swap + · simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame.from, + bind_tc_ok] at hrt + exact (errPairA hrt).elim + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok, uncurry_apply_pair] at hrt + obtain ⟨c0, hc0, hrt⟩ := bind_eq_ok hrt + have ec0 : c0 = capA st s src := okinj (hc0.symm.trans (capA_eq (st := st) s src)) + subst ec0 + obtain ⟨ns, hns, hrt⟩ := bind_eq_ok hrt + obtain ⟨c1, hc1, hrt⟩ := bind_eq_ok hrt + have ec1 : c1 = capA st s pivot := okinj (hc1.symm.trans (capA_eq (st := st) s pivot)) + subst ec1 + obtain ⟨np, hnp, hrt⟩ := bind_eq_ok hrt + obtain ⟨b2, hb2, hrt⟩ := bind_eq_ok hrt + cases b2 + swap + · rw [if_pos rfl] at hrt + simp only [error.illegal, bind_ok, bind_tc_ok] at hrt + exact (errPairA hrt).elim + rw [if_neg Bool.false_ne_true] at hrt + obtain ⟨b3, hb3, hrt⟩ := bind_eq_ok hrt + cases b3 + swap + · rw [if_pos rfl] at hrt + simp only [error.illegal, bind_ok, bind_tc_ok] at hrt + exact (errPairA hrt).elim + have nn : ∀ c : capability.Cap, capability.Cap.is_null c = ok false → c ≠ .Null := by + intro c hc e + rw [e] at hc + simp only [capability.Cap.is_null] at hc + have := okinj hc + cases this + exact ⟨hl8, index, depth, dest, x0, x1, pdata, sdata, src, pivot, ns, np, hi, hd, hres, hx0, h0, hx1, h1, h2, h5, + hrot, hns, hnp, nn _ hb2, nn _ hb3⟩ + +end + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {Rj : Set U64 → JSlotA S} {ar : machine.Arch A} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} + +/-- The spec cap of a typed CNode slot. -/ +theorem JSyscallA.slotSpecA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st Rj) s a σ m) + {ss : Fl4SpecA64.CslotPtr} {ps : U64} (hps1 : ss.1 = specPtr ps) (hps : Z.th ps = some (.cnode ss.2.length)) : + ∃ srcA, CapRelA (capA st s (cslotAddrA ss)) srcA ∧ specCapAtA σ ss = some srcA := by + have hR := J.j.1.st + obtain ⟨cs, hk, hwfc, hsl⟩ := hR.cnode ps _ hps + obtain ⟨srcA, hcsA, -⟩ := hsl ss.2 rfl + refine ⟨srcA, ?_, ?_⟩ + · obtain ⟨a0, h, hg⟩ := hR.slot_capA hps1 hps + have hg' : Fl4SpecA64.getCap (Z := Unit) ss σ = ({(srcA, σ)}, False) := by + have : ss = (specPtr ps, ss.2) := by rw [← hps1] + rw [this]; exact getCap_cnodeA hk hwfc hcsA + have e := congrArg Prod.fst (hg.symm.trans hg') + simp only [Set.singleton_eq_singleton_iff, Prod.mk.injEq, and_true] at e + rw [capAtA_eq (st := st), e] at h + exact h + · unfold specCapAtA + rw [hps1, hk] + simp [hwfc, hcsA] + +set_option maxHeartbeats 2000000 in +/-- **A rotated cap**: `update_cap_data true` of a typed slot's cap, non-null. -/ +theorem JSyscallA.updCap_factsA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st Rj) s a σ m) + {ss : Fl4SpecA64.CslotPtr} {ps : U64} (hps1 : ss.1 = specPtr ps) (hps : Z.th ps = some (.cnode ss.2.length)) + (hsO : cslotAddrA ss ∈ OS) {data : U64} {nc : capability.Cap} + (hu : objects.update_cap_data true data (capA st s (cslotAddrA ss)) = ok nc) (hn : nc ≠ .Null) : + CapRt.EncWf nc ∧ DerivA nc (capA st s (cslotAddrA ss)) ∧ UA nc (capA st s (cslotAddrA ss)) ∧ + Fl4ProofsA64.P1.W7.R.ST.shapeA nc = Fl4ProofsA64.P1.W7.R.ST.shapeA (capA st s (cslotAddrA ss)) ∧ + badgeA nc = badgeA (capA st s (cslotAddrA ss)) ∧ + (∀ i, nc = .IrqHandler i ↔ capA st s (cslotAddrA ss) = .IrqHandler i) ∧ + CapObjOkA Z nc ∧ NodeOffA (L.ksb s) nc ∧ ∀ anc, CapRelA nc anc → NoHiPtV anc → NoIdleCapV m n anc := by + have hP := J.j.1.place + have hAs : AbsSlotA Z.th ss := ⟨ps, hps1, Or.inl hps⟩ + obtain ⟨srcA, hsca, hspec⟩ := J.slotSpecA hps1 hps + have hwsrc : CapRt.EncWf (capA st s (cslotAddrA ss)) := by + rw [← capAtA_eq (st := st)]; exact CapRt.decodeF_encWf _ _ + obtain ⟨c', hc', -, -, hew, hv, -, -⟩ := update_relA true data _ hsca.1 hwsrc + have ec : c' = nc := okinj (hc'.symm.trans hu) + subst ec + have g := updCapData_guardA data (capA st s (cslotAddrA ss)) + rw [hu, WP.spec_ok] at g + have g' : GuardEqA c' (capA st s (cslotAddrA ss)) := g.resolve_left hn + obtain ⟨hD, hal, hsh1, hbd, hirq⟩ := updCapData_true_okA hu hn + have hsh3 : Fl4ProofsA64.P3.shapeA c' = Fl4ProofsA64.P3.shapeA (capA st s (cslotAddrA ss)) := by + rcases g' with e | ⟨_, _, _, _, _, _, e1, e2⟩ + · rw [e] + · rw [e1, e2]; rfl + have hE := J.toJStage3A.toJEntA + have hno : CapObjOkA Z c' := by + have h := hE.j.capObj (hAs.isSlot hP) + rw [capAtA_eq (st := st)] at h + exact h.of_shapeA hsh3 (hv Z.th) + exact ⟨hew, hD, hal, hsh1, hbd, hirq, hno, (NodeOffA.of_ctOff (hE.sys.sysl.ct _ hsO)).of_shapeA hsh3, + noIdle_of_shapeA J.sidle hsca hspec hsh1⟩ + +/-- A cap off the IRQ-node slots may be written into a slot of a CNode that is none. -/ +theorem JSyscallA.capOk_relA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st Rj) s a σ m) + {sd : Fl4SpecA64.CslotPtr} {pd : U64} (hpd1 : sd.1 = specPtr pd) (hpdn : ¬ IrqNodeA (L.ksb s) pd) + {nc : capability.Cap} (hoff : NodeOffA (L.ksb s) nc) : + ∀ ac, CapRelA nc ac → CapOkA σ.interruptIrqNode sd ac := by + intro ac hac + have hdo : ¬ IrqNodeA (L.ksb s) (fl4Ptr sd.1) ∨ IrqCapA (absCapA nc) := + Or.inl (by rw [hpd1, fl4Ptr_specPtr]; exact hpdn) + have h00 := capOk_insA J.toJStage3A.toJEntA.j.1.irq hdo hoff + cases nc with + | PageTable p0 m0 asid va => + cases m0 + · rw [← CapEqA.eq_abs hac.2 (fun _ _ _ h => by cases h)]; exact h00 + · obtain ⟨_, _, rfl, _, _⟩ := CapEqA.pt_mapped_iff.mp hac.2 + refine ⟨fun irq hirq0 hsl => ?_, fun ⟨_, _, _, _, e⟩ => by cases e⟩ + have hi0 := h00.1 irq hirq0 hsl + simp [IrqCapA, absCapA] at hi0 + | _ => rw [← CapEqA.eq_abs hac.2 (fun _ _ _ h => by cases h)]; exact h00 + +set_option maxHeartbeats 8000000 in +/-- **CNode Rotate's scope member from the run and the record** (module docstring). -/ +theorem JSyscallA.cnodeRotate_runA (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + {info : msginfo.MessageInfo} {buf slot : U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} + {mi : Fl4SpecA64.MessageInfo} {sl : Fl4SpecA64.CslotPtr} {c : Fl4SpecA64.Cap} + {excaps : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)} {buffer : Option Fl4SpecA64.ObjRef} {argsU : List U64} + (Q : SiteA st Z OS s σ info buf slot extra mi sl c excaps buffer argsU) + {p r gs g : U64} (hslc : capA st s slot = .CNode p r gs g) (hlb : info.label = fl4_abi.label.CNodeRotate) + {bB bC bD : Bool} {s' : S} {a' : A} + (hdec : syscall.decode_invocation st ar s a info.label (msgLenA info buf) slot (capA st s slot) bB bC bD false + buf extra = ok (core.result.Result.Ok (), s', a')) : + ∃ Out, DispScope4A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ m Clock hPl.ipa info.label + (msgLenA info buf) slot bB bC bD false buf extra Out := by + rw [hslc] at hdec + obtain ⟨hl8, index, depth, dest, x0, x1, pdata, sdata, src, pivot, ns, np, hi, hd0, hres, hx0, h0, hx1, h1, h2, h5, + hrot, hus, hup, hns, hnp⟩ := decCnodeRotate_invA hlb hdec + have F := Q.F + have hR := J.j.1.st + have hV : SlotsValidA Z.th (L.mem s) := J.j.1.cv + have hP := J.j.1.place + have hO : OSOK H.tcbs H.omega H.frame OS := J.sys.ent.os + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA Q.hext Q.hOS Q.cov J.actW J.running J.cur + have hwin := RS.pl.choose_spec.choose_spec.choose_spec.choose_spec.win + -- the destination + have hc : CapRelA (.CNode p r gs g) c := hslc ▸ F.cap + have hcv : CapValidA Z.th (.CNode p r gs g) := by + have h := hV _ (F.pin.habs.isSlot hP) + rw [capAtA_eq (st := st), ← F.pin.hslot, hslc] at h + exact h + have hresA := hres + unfold cspace.lookup_target_slot at hresA + obtain ⟨r1, hr1, f1, -, hrel1⟩ := spec_existsA + (lookup_slot_for_cnode_op_refinesA Z.th s σ hR hV false _ c hc hcv index depth) + have er1 : r1 = .Ok dest := okinj (hr1.symm.trans hresA) + subst er1 + cases f1 with + | error _ => exact hrel1.elim + | ok sd => + obtain ⟨hd1, pd, hpd1, hpd, hCn⟩ := hrel1 + have hAd : AbsSlotA Z.th sd := ⟨pd, hpd1, Or.inl hpd⟩ + have hpdn : ¬ IrqNodeA (L.ksb s) pd := noIrq_of_cnRefA hP hV hcv hCn + have edest : dest = cslotAddrA sd := hd1 + subst edest + -- the two extra caps + have ex0 : x0 = exAtA extra 0 := okinj (hx0.symm.trans (exAtA_idx0 extra)) + have ex1 : x1 = exAtA extra 1 := okinj (hx1.symm.trans (index_exAtA extra 1#usize (by simp))) + have hlen2 : 1 < excaps.length := by + by_contra h + exact h1 (ex1.trans (F.extra.2.2 1 (by omega) (by omega))) + obtain ⟨y0, y1, erest, hexc⟩ : ∃ y0 y1 erest, excaps = y0 :: y1 :: erest := by + match excaps, hlen2 with + | y0 :: y1 :: erest, _ => exact ⟨y0, y1, erest, rfl⟩ + have exk : ∀ (j : Nat) (x : U64), x = exAtA extra j → ∀ (hj : j < excaps.length), + CapRelA (capA st s x) excaps[j].1 ∧ CapValidA Z.th (capA st s x) := by + intro j x ex hj + obtain ⟨ps, hth, hsp, hxa, hrel⟩ := F.extra.2.1 j hj + rw [← ex, capAtA_eq (st := st)] at hrel + exact ⟨hrel, hV x (by rw [ex, hxa]; exact AbsSlotA.isSlot hP ⟨ps, hsp, Or.inl hth⟩)⟩ + have k0 := exk 0 x0 ex0 (by omega) + have k1 := exk 1 x1 ex1 hlen2 + simp only [hexc, List.getElem_cons_zero, List.getElem_cons_succ] at k0 k1 + -- the source and pivot slots + have arg : ∀ i : U64, ∃ v, msg.syscall_arg st s (L.curW s) buf i = ok v := fun i => + let ⟨v, hv, _⟩ := spec_existsA (P0.msg.syscall_arg.p0 st s (L.curW s) buf i L.total) + ⟨v, hv⟩ + obtain ⟨a3, h3⟩ := arg 3#u64 + obtain ⟨a4, h4⟩ := arg 4#u64 + obtain ⟨a6, h6⟩ := arg 6#u64 + obtain ⟨a7, h7⟩ := arg 7#u64 + obtain ⟨res, hres', hrel⟩ := spec_existsA (rotate_slots_relA (st := st) s σ hR hV hP buf extra x0 x1 hx0 hx1 + y0.1 y1.1 k0.1 k0.2 k1.1 k1.2 a3 a4 a6 a7 h3 h4 h6 h7 sd pd hpd1 hpd (rotFinA pdata.bv sdata.bv sd)) + have er : res = .Ok (src, pivot) := okinj (hres'.symm.trans hrot) + subst er + obtain ⟨ss, sp, ⟨hs, ps, hps1, hps, hcr1⟩, ⟨hp', pp, hpl1, hpl, hcr2⟩, hne1, hne2, hdd, hsn, hpn, -⟩ := hrel + have esrc : src = cslotAddrA ss := hs + have epiv : pivot = cslotAddrA sp := hp' + subst esrc epiv + rw [capAtA_eq (st := st)] at hsn hpn + have hAs : AbsSlotA Z.th ss := ⟨ps, hps1, Or.inl hps⟩ + have hAp : AbsSlotA Z.th sp := ⟨pp, hpl1, Or.inl hpl⟩ + have hsO : cslotAddrA ss ∈ OS := J.j5.2.1 _ (hAs.isSlot hP) + have hpO : cslotAddrA sp ∈ OS := J.j5.2.1 _ (hAp.isSlot hP) + have hdO : cslotAddrA sd ∈ OS := J.j5.2.1 _ (hAd.isSlot hP) + have hpsn : ¬ IrqNodeA (L.ksb s) ps := noIrq_of_cnRefA hP hV k1.2 hcr1 + have hppn : ¬ IrqNodeA (L.ksb s) pp := noIrq_of_cnRefA hP hV k0.2 hcr2 + have hsp : cslotAddrA ss ≠ cslotAddrA sp := fun h => hne1 (cslotAddrA_inj hP hAp hAs h.symm) + obtain ⟨hews, hDs, hals, hshs, hbds, hirqs, hos, hofs, hscs⟩ := J.updCap_factsA hps1 hps hsO hus hns + obtain ⟨hewp, hDp, halp, hshp, hbdp, hirqp, hop, hofp, hscp⟩ := J.updCap_factsA hpl1 hpl hpO hup hnp + have hsx := OSOK.canon hO hwin hsO + have hpx := OSOK.canon hO hwin hpO + by_cases e : cslotAddrA ss = cslotAddrA sd + · -- the swap: the source is the destination + have hrot' := hrot + have hres'' := hres + rw [← e] at hrot' hres'' + exact J.cnodeRotateSw_scopeA hA hM hPl hfm Q (lbl := info.label) (bB := bB) (bC := bC) (bD := bD) ss sp + (hCs := ⟨hps1, hps⟩) (hCp := ⟨hpl1, hpl⟩) (hsp := hsp) (hsn := hsn) (hpn := hpn) (hsx := hsx) (hpx := hpx) + (hwp := hewp) (hnp := hnp) (hDp := hDp) (halp := halp) (hshp := hshp) (hbdp := hbdp) (hirqp := hirqp) + (hop := hop) (hws := hews) (hns := hns) (hDs := hDs) (hals := hals) (hshs := hshs) (hbds := hbds) + (hirqs := hirqs) (hos := hos) (hslc := hslc) (hlb := hlb) (hl8 := hl8) (hl2 := fun h => hl8 (by scalar_tac)) + (hi := hi) (hd0 := hd0) (hres := hres'') (hx0 := hx0) (h0 := h0) (hx1 := hx1) (h1 := h1) (h2 := h2) + (h5 := h5) (hrot := hrot') (hus := hus) (hup := hup) + (hokp := J.capOk_relA hps1 hpsn hofp) (hscp := hscp) (hoks := J.capOk_relA hpl1 hppn hofs) (hscs := hscs) + (hlab := rfl) + · have hsd' : ss ≠ sd := fun h => e (by rw [h]) + have hdn : capA st s (cslotAddrA sd) = .Null := by + rw [← capAtA_eq (st := st)]; exact hdd hsd' + exact J.cnodeRotate_scopeA hA hM hPl hfm Q (lbl := info.label) (bB := bB) (bC := bC) (bD := bD) ss sp sd + (hCs := ⟨hps1, hps⟩) (hCp := ⟨hpl1, hpl⟩) (hCd := ⟨hpd1, hpd⟩) (hsp := hsp) (hsd := e) (hsn := hsn) + (hpn := hpn) (hdn := hdn) (hpc := hpx) (hdc := OSOK.canon hO hwin hdO) + (hwp := hewp) (hnp := hnp) (hDp := hDp) (halp := halp) (hshp := hshp) (hbdp := hbdp) (hirqp := hirqp) + (hop := hop) (hws := hews) (hns := hns) (hDs := hDs) (hals := hals) (hshs := hshs) (hbds := hbds) + (hirqs := hirqs) (hos := hos) (hslc := hslc) (hlb := hlb) (hl8 := hl8) (hl2 := fun h => hl8 (by scalar_tac)) + (hi := hi) (hd0 := hd0) (hres := hres) (hx0 := hx0) (h0 := h0) (hx1 := hx1) (h1 := h1) (h2 := h2) + (h5 := h5) (hrot := hrot) (hus := hus) (hup := hup) + (hokp := J.capOk_relA hpd1 hpdn hofp) (hscp := hscp) (hoks := J.capOk_relA hpl1 hppn hofs) (hscs := hscs) + (hlab := rfl) + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3CombinedFrontA.lean b/proofs/Fl4ProofsA64/P3/Stage3CombinedFrontA.lean new file mode 100644 index 000000000..f848445c6 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3CombinedFrontA.lean @@ -0,0 +1,419 @@ +/- +P3 (aarch64, A64-P3 line RecvPreA): **the front end of the combined calls** (`ReplyRecv`, `NBSendRecv`, +`NBSendWait`), from the syscall entry to `handle_recv`. + +`handle_invocation` runs with `first_phase`; `fl4HandleInvocationR` runs with `firstPhase`. The prefix (message info, +capability lookup, IPC buffer, extra caps, message words) is that of `JSyscallA.invocationA`, read from `SiteA`. + +* **`JSyscallA.frontOkA`**: FL4's `handle_invocation` and the spec's, from a first half: a spec decode, perform and + epilogue, and FL4's decode and epilogue. +* **`JSyscallA.replyRecvEntryA`**, **`replyPassiveRecvEntryA`**, **`nbSendRecvEntryA`**, + **`nbSignalRecvEntryA`** (quiet signal): the first halves from + the entry, ending in `RecvPreA`; the slot facts come from the site. +* **`JSyscallA.combinedDispatchA`**: `dispatch_syscall` and `fl4DispatchSyscall` from a first half and the receive's + outcome. + +[S] those of the first half used. [J] `JSyscallA`. No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3NBSendRecvA +import Fl4ProofsA64.P3.Stage3SiteA +import Fl4ProofsA64.P3.IpcInvokeDispatchA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +theorem fl4HandleInvocationR_firstA (rep calling blocking donate first : Bool) (cptr : CapRef) : + fl4HandleInvocationR (Z := Unit) rep calling blocking donate first cptr = (do + let thread ← NonDetE.liftE <| NonDet.gets fun s : Fl4SpecA64.State Unit => s.curThread + let info ← withoutPreemption <| getMessageInfo thread + syscall (frontSpecA thread cptr info) (fun fault => NonDet.when blocking <| handleFault thread fault) + (fun (slot, cap, extracaps, buffer) => do + let args ← NonDetE.liftE <| getMrs thread buffer info + fl4DecodeInvocation calling first info.miLabel args cptr slot cap extracaps buffer) + (fun err => NonDet.when calling <| fl4ReplyFromKernelError rep thread <| msgFromSyscallError err) + (fun oper => do + withoutPreemption <| setThreadState thread .restart + let reply ← fl4PerformInvocation blocking calling donate oper + withoutPreemption <| epiSpecA calling thread reply)) := rfl + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s : S} {a : A} {σ : State Unit} {m : McState} + +set_option maxHeartbeats 2000000 in +/-- **`handle_invocation` in the first phase, both sides** (module docstring). -/ +theorem JSyscallA.frontOkA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + (rep : Bool) {cptr : U64} {info : msginfo.MessageInfo} {buf : U64} {r1 : cspace.Resolved} + {e : Aeneas.Std.Array obj.Slot 3#usize} + (einfo : msg.msg_info st s (L.curW s) = ok info) + (ebuf : msg.lookup_ipc_buffer st s false (L.curW s) = ok buf) + (elr : cspace.lookup_slot st s (L.curW s) cptr = ok (.Ok r1)) + (eex : ipc.lookup_extra_caps st s (L.curW s) buf info = ok (.Ok e)) + {oper : Fl4Invocation} {σ1 σ2 σ3 : State Unit} {s2 s3 : S} + (hdecS : ∀ c : Cap, CapRelA (capA st s r1.slot) c → ∀ (lbl : Data) (args : List Data) (ci : CapRef) + (sl : CslotPtr) (excaps : List (Cap × CslotPtr)) (buffer : Option ObjRef), + (.ok oper, σ) ∈ ((fl4DecodeInvocation (Z := Unit) false true lbl args ci sl c excaps buffer).run σ).1) + (hset : setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False)) + (hperf : (.ok [], σ2) ∈ ((fl4PerformInvocation (Z := Unit) false false true oper).run σ1).1) + (hget : getThreadState (Z := Unit) σ.curThread σ2 = ({(.restart, σ2)}, False)) + (hrun : ((), σ3) ∈ (setThreadState (Z := Unit) σ.curThread .running σ2).1) + (hdecF : syscall.decode_invocation st ar s a info.label (msgLenA info buf) r1.slot (capA st s r1.slot) false + false true true buf e = ok (.Ok (), s2, a)) + (hstate : obj.Tcb.state st (L.curW s) s2 = ok sched.TS_RESTART) + (hsr : sched.set_thread_state st s2 (L.curW s) sched.TS_RUNNING = ok s3) : + syscall.handle_invocation st ar s a false false true true cptr = ok (.Ok (), s3, a) ∧ + (.ok (), σ3) ∈ ((fl4HandleInvocationR (Z := Unit) rep false false true true + (Fl4SpecA64.toBl cptr.bv)).run σ).1 := by + constructor + · unfold syscall.handle_invocation + rw [cur_thread_ok s, bind_ok, einfo, bind_ok, elr, bind_ok] + dsimp only + rw [capA_eq, bind_ok, ebuf, bind_ok, eex, bind_ok] + dsimp only + have elen : (if info.length > msg.N_MSG_REGISTERS then + if buf = 0#u64 then ok msg.N_MSG_REGISTERS else ok info.length + else ok info.length) = ok (msgLenA info buf) := by + unfold msgLenA; split <;> (try split) <;> rfl + rw [elen, bind_ok, hdecF, bind_ok] + simp only [uncurry_apply_pair] + rw [hstate, bind_ok, if_pos rfl] + simp only [Bool.false_eq_true, ↓reduceIte, bind_ok] + rw [hsr, bind_ok] + · have ct : σ.curThread = specPtr (L.curW s) := J.j.1.sch.curThread + have htZ : L.curW s ∈ Z.T := J.sr.curT + have hth : Z.th (L.curW s) = some .tcb := (J.j.1.thT _).2 htZ + obtain ⟨mi, sl, c, excaps, buffer, argsU, F⟩ := J.frontJA einfo ebuf elr eex + have gmi := F.pin.hmi + -- the prefix of the spec + have front : (frontSpecA σ.curThread (Fl4SpecA64.toBl cptr.bv) mi).run σ = + ({(.ok (sl, c, excaps, buffer), σ)}, False) := by + obtain ⟨lr, elr', se, hse, hrel⟩ := spec_existsA (lookup_slot_refinesA Z.th s σ J.j.1.st J.j.1.cv _ hth cptr) + have el : lr = .Ok r1 := okinj (elr'.symm.trans elr) + subst el + cases se with + | error _ => exact hrel.1.elim + | ok v => + obtain ⟨sl', rest⟩ := v + obtain ⟨⟨hsl, -⟩, p, hp1, hp, -⟩ := hrel + obtain ⟨c', -, hgc⟩ := J.j.1.st.slot_capA hp1 hp + have e1 : sl' = sl := cslotAddrA_inj J.j.1.place ⟨p, hp1, Or.inl hp⟩ F.pin.habs + (hsl.symm.trans F.pin.hslot) + subst e1 + have e2 : c' = c := single_fstA (hgc.symm.trans F.pin.hcap) + subst e2 + rw [← ct] at hse + unfold frontSpecA + rw [run_bind_okA (lcs_okA (ofBl (Fl4SpecA64.toBl cptr.bv)) hse hgc)] + dsimp only + rw [run_bind_okA (run_liftEA F.pin.hbuf), run_bind_okA F.pin.hex] + rfl + have gc : (NonDet.gets fun s : Fl4SpecA64.State Unit => s.curThread) σ = ({(σ.curThread, σ)}, False) := rfl + rw [fl4HandleInvocationR_firstA, run_bind_okA (run_liftEA gc)] + unfold withoutPreemption + rw [run_bind_okA (run_liftEA gmi)] + refine syscall_memA (y := oper) front ?_ ?_ + · dsimp only + rw [run_bind_okA (run_liftEA F.pin.hargs)] + exact hdecS c F.cap _ _ _ _ _ _ + · rw [ExceptT.run_bind] + have m1 : ((), σ1) ∈ (setThreadState (Z := Unit) σ.curThread .restart σ).1 := by + rw [hset]; exact Set.mem_singleton _ + refine mem_bindA.2 ⟨(.ok (), σ1), liftE_memA m1, ?_⟩ + dsimp only [ExceptT.bindCont] + rw [ExceptT.run_bind] + refine mem_bindA.2 ⟨(.ok [], σ2), hperf, ?_⟩ + dsimp only [ExceptT.bindCont] + refine liftE_memA ?_ + unfold epiSpecA + rw [nd_bind_oneA hget] + dsimp only + unfold Fl4Spec.NonDet.when + rw [if_neg Bool.false_ne_true, nd_bind_oneA (show (Fl4Spec.NonDet.pure () : SMonad Unit Unit) σ2 = _ from rfl)] + exact hrun + +/-- **`dispatch_syscall` for a combined call** (module docstring): from a first half and the receive. `cptr` is the +register the call names (`REPLY`, or the destination register for `NBSendRecv`). -/ +theorem JSyscallA.combinedDispatchA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (rep : Bool) (pending : SMonad Unit Unit) {call : Syscall} {receiveReply : Bool} + (hc : DispatchCombinedAtA call receiveReply) {cptr : U64} + (hreg : cptr = L.mem s (regWA (L.curW s) + (if call = .sysNBSendRecv then layout.reg.NBSEND_RECV_DEST else layout.reg.REPLY))) + {s3 s4 : S} {a4 : A} {σ3 σ4 : State Unit} + (hinv : syscall.handle_invocation st ar s a false false true true cptr = ok (.Ok (), s3, a)) + (hspec : (.ok (), σ3) ∈ ((fl4HandleInvocationR (Z := Unit) rep false false true true + (Fl4SpecA64.toBl cptr.bv)).run σ).1) + (hrecv : syscall.handle_recv st ar s3 a true receiveReply = ok (s4, a4)) + (hrs : ((), σ4) ∈ (handleRecv (Z := Unit) true receiveReply σ3).1) : + syscall.dispatch_syscall st ar s a (syscallNumberA call) ⦃ r => + StepPostA (fun s' a' τ => s' = s4 ∧ a' = a4 ∧ τ = σ4) + (fl4SettleDispatch pending (fl4DispatchSyscall (fl4HandleInvocationR (Z := Unit) rep) call)) σ r ⦄ := by + have htZ : L.curW s ∈ Z.T := J.sr.curT + obtain ⟨x, hk, -⟩ := J.ep.tcb (L.curW s) htZ + have regs := (J.ep.at_tcb htZ hk).regs + have reg : ∀ (r : Fl4SpecA64.Register) (o : U64), Endp.regA (L.curW s) (regIdxA r) = regWA (L.curW s) o → + x.tcbArch.tcbContext.userRegs r = (L.mem s (regWA (L.curW s) o)).bv := fun r o e => by + rw [← e]; exact (regs r).symm + have off : ∀ (r : Fl4SpecA64.Register) (o : U64), (regIdxA r).val = o.val → + Endp.regA (L.curW s) (regIdxA r) = regWA (L.curW s) o := fun r o e => by + unfold Endp.regA regWA + rw [UScalar.eq_of_val_eq e] + have pins : ∀ calling blocking donate first (cp : U64), + DispatchInvokeAtA (L.mem s (regWA (L.curW s) layout.reg.CAP)) (L.mem s (regWA (L.curW s) layout.reg.REPLY)) + (L.mem s (regWA (L.curW s) layout.reg.NBSEND_RECV_DEST)) call calling blocking donate first cp → + calling = false ∧ blocking = false ∧ donate = true ∧ first = true ∧ cp = cptr := by + intro calling blocking donate first cp h + cases hc <;> cases h <;> refine ⟨rfl, rfl, rfl, rfl, ?_⟩ <;> rw [hreg] <;> simp + refine dispatchSyscall_refinesA (st := st) (ar := ar) (a := a) (InvRel := fun _ s' a' τ => s' = s3 ∧ a' = a ∧ τ = σ3) + call (cur_thread_ok s) ?_ J.j.1.sch.curThread hk + (reg cap_register layout.reg.CAP (off _ _ (by simp [cap_register, capRegister, regIdxA, layout.reg.CAP, layout.reg.X0]))) + (reg replyRegister layout.reg.REPLY (off _ _ (by simp [replyRegister, regIdxA, layout.reg.REPLY, layout.reg.X6]))) + (reg nbsendRecvDest layout.reg.NBSEND_RECV_DEST + (off _ _ (by simp [nbsendRecvDest, regIdxA, layout.reg.NBSEND_RECV_DEST, layout.reg.X8]))) + refine { inv := ?_, done := ?_, preempt := ?_, combined := ?_, recv := ?_, yield := ?_ } + · intro calling blocking donate first cp h + obtain ⟨rfl, rfl, rfl, rfl, rfl⟩ := pins _ _ _ _ _ h + rw [hinv, WP.spec_ok] + exact ⟨.ok (), σ3, hspec, rfl, rfl, rfl, rfl⟩ + · intro calling blocking donate first cp s' a' τ h hf + exact absurd ((pins _ _ _ _ _ h).2.2.2.1.symm.trans hf) (by simp) + · intro calling blocking donate first cp s' a' τ h he + obtain ⟨rfl, rfl, rfl, rfl, rfl⟩ := pins _ _ _ _ _ h + rw [hinv] at he + cases (Prod.mk.inj (okinj he)).1 + · intro cp rr s' a' τ h hr _ _ R + obtain ⟨rfl, rfl, rfl⟩ := R + have er : rr = receiveReply := by cases hc <;> cases hr <;> rfl + subst er + rw [hrecv, WP.spec_ok] + exact ⟨σ4, hrs, rfl, rfl, rfl⟩ + · intro b rr h + cases hc <;> cases h + · intro h + subst h + cases hc + +end + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {s : S} {a : A} {σ : State Unit} {m : McState} + +set_option maxHeartbeats 1000000 in +/-- **`NBSendRecv` / `NBSendWait` with no receiver, from the syscall entry to `handle_recv`** (module docstring). -/ +theorem JSyscallA.nbSendRecvEntryA + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (rep : Bool) {cptr : U64} {info : msginfo.MessageInfo} {buf : U64} {r1 : cspace.Resolved} + {e : Aeneas.Std.Array obj.Slot 3#usize} + (einfo : msg.msg_info st s (L.curW s) = ok info) + (ebuf : msg.lookup_ipc_buffer st s false (L.curW s) = ok buf) + (elr : cspace.lookup_slot st s (L.curW s) cptr = ok (.Ok r1)) + (eex : ipc.lookup_extra_caps st s (L.curW s) buf info = ok (.Ok e)) + {p badge : U64} {recv grant grantReply : Bool} + (hcap : capA st s r1.slot = .Endpoint p badge true recv grant grantReply) + (hp : p ∈ E) {x : Endpoint} (hk : σ.kheap (specPtr p) = some (.endpoint x)) (hc : CanBlockA false x) : + ∃ s3 σ3, syscall.handle_invocation st ar s a false false true true cptr = ok (.Ok (), s3, a) ∧ + (.ok (), σ3) ∈ ((fl4HandleInvocationR (Z := Unit) rep false false true true + (Fl4SpecA64.toBl cptr.bv)).run σ).1 ∧ + syscall.after_invocation st ar s3 a (.Ok ()) = ok (true, s3, a) ∧ L.curW s3 = L.curW s ∧ + (∀ x, σ3.kheap σ3.curThread = some (.tcb x) → x.tcbSchedContext = some σ3.curSc) ∧ + ∃ Q Rl, RecvPreA st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n + (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s3 a σ3 m := by + obtain ⟨mi, sl, c, excaps, buffer, argsU, T⟩ := J.siteA einfo ebuf elr eex + obtain ⟨xc, σ1, σ3, s1, s3, hkc, hσ1, hsp, hsend, mem3, hdec, hstate, eRun, haft, hcur3, -, own3, Q, Rl, P⟩ := + J.nbSendFirstA (lbl := info.label) (len := msgLenA info buf) (buf := buf) (don := true) (fp := true) hA hPl + T.hOS T.cov hcap T.hext hp hk hc + have f1 : σ1.curThread = σ.curThread := by rw [hσ1]; rfl + have hk1 : σ1.kheap σ.curThread = some (.tcb { xc with tcbState := .restart }) := by + rw [hσ1]; simp [stUpdA, setObjA] + have get1 : getThreadState (Z := Unit) σ.curThread σ1 = ({(.restart, σ1)}, False) := + threadGet_eqA Tcb.tcbState hk1 + have perf : (.ok [], σ1) ∈ ((fl4PerformInvocation (Z := Unit) false false true + (.invokeEndpoint (specPtr p) badge.bv grant grantReply)).run σ1).1 := by + refine liftE_memA (mem_bindA.2 ⟨(σ.curThread, σ1), ?_, ?_⟩) + · rw [← f1]; exact Set.mem_singleton _ + · refine mem_bindA.2 ⟨((), σ1), ?_, Set.mem_singleton _⟩ + rw [hsend]; exact Set.mem_singleton _ + have F := J.frontOkA rep einfo ebuf elr eex (oper := .invokeEndpoint (specPtr p) badge.bv grant grantReply) + (σ1 := σ1) (σ2 := σ1) (σ3 := σ3) (s2 := s1) (s3 := s3) (fun c hcr lbl args ci sl excaps buffer => by + have h2 : CapEqA (.Endpoint p badge true recv grant grantReply) c := hcap ▸ hcr.2 + have ec : absCapA (.Endpoint p badge true recv grant grantReply) = c := h2 + rw [← ec, decodeEndpoint_specA, if_pos rfl] + exact Set.mem_singleton _) + hsp perf get1 mem3 hdec hstate eRun + exact ⟨s3, σ3, F.1, F.2, haft, hcur3, own3, Q, Rl, P⟩ + +set_option maxHeartbeats 1000000 in +/-- **`NBSendRecv` / `NBSendWait` as a quiet signal, from the syscall entry to `handle_recv`** (module docstring). -/ +theorem JSyscallA.nbSignalRecvEntryA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hcore : m.curCore < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (rep : Bool) {cptr : U64} {info : msginfo.MessageInfo} {buf : U64} {r1 : cspace.Resolved} + {e : Aeneas.Std.Array obj.Slot 3#usize} + (einfo : msg.msg_info st s (L.curW s) = ok info) + (ebuf : msg.lookup_ipc_buffer st s false (L.curW s) = ok buf) + (elr : cspace.lookup_slot st s (L.curW s) cptr = ok (.Ok r1)) + (eex : ipc.lookup_extra_caps st s (L.curW s) buf info = ok (.Ok e)) + {p badge : U64} {recv : Bool} (hcap : capA st s r1.slot = .Notification p badge true recv) + (hp : p ∈ N) {x : Notification} (hk : σ.kheap (specPtr p) = some (.notification x)) + (hq : QuietSigA σ x) : + ∃ s3 σ3, syscall.handle_invocation st ar s a false false true true cptr = ok (.Ok (), s3, a) ∧ + (.ok (), σ3) ∈ ((fl4HandleInvocationR (Z := Unit) rep false false true true + (Fl4SpecA64.toBl cptr.bv)).run σ).1 ∧ + syscall.after_invocation st ar s3 a (.Ok ()) = ok (true, s3, a) ∧ L.curW s3 = L.curW s ∧ + (∀ x, σ3.kheap σ3.curThread = some (.tcb x) → x.tcbSchedContext = some σ3.curSc) ∧ + ∃ Q Rl, RecvPreA st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n + (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s3 a σ3 m := by + obtain ⟨mi, sl, c, excaps, buffer, argsU, T⟩ := J.siteA einfo ebuf elr eex + obtain ⟨xc, σ1, σ2, σ3, s2, s3, hkc, hσ1, hsp, hsig, get2, mem3, hdec, hstate, eRun, haft, hcur3, own3, Q, Rl, P⟩ := + J.nbSignalQuietFirstA (lbl := info.label) (len := msgLenA info buf) (buf := buf) (blk := false) (call := false) + (don := true) (fp := true) hA hM hPl hfm hcore hmb hmax htu T.hOS T.cov hcap T.hext T.hSC hp hk hq + have perf : (.ok [], σ2) ∈ ((fl4PerformInvocation (Z := Unit) false false true + (.invokeNotification (specPtr p) badge.bv)).run σ1).1 := by + have run : fl4PerformInvocation (Z := Unit) false false true (.invokeNotification (specPtr p) badge.bv) = + (do withoutPreemption <| sendSignal (specPtr p) badge.bv + Fl4Spec.NonDetE.returnOk []) := rfl + rw [run, ExceptT.run_bind] + refine mem_bindA.2 ⟨(.ok (), σ2), liftE_memA ?_, Set.mem_singleton _⟩ + rw [hsig] + exact Set.mem_singleton _ + have F := J.frontOkA rep einfo ebuf elr eex (oper := .invokeNotification (specPtr p) badge.bv) + (σ1 := σ1) (σ2 := σ2) (σ3 := σ3) (s2 := s2) (s3 := s3) (fun c hcr lbl args ci sl excaps buffer => by + have h2 : CapEqA (.Notification p badge true recv) c := hcap ▸ hcr.2 + have ec : absCapA (.Notification p badge true recv) = c := h2 + rw [← ec, decodeNotification_specA, if_pos rfl] + exact Set.mem_singleton _) + hsp perf get2 mem3 hdec hstate eRun + exact ⟨s3, σ3, F.1, F.2, haft, hcur3, own3, Q, Rl, P⟩ + +set_option maxHeartbeats 1000000 in +/-- **`ReplyRecv` through a live Reply, the replier keeping its SC, from the syscall entry to `handle_recv`** +(module docstring). -/ +theorem JSyscallA.replyRecvEntryA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hag : fl4MinBudget = MIN_BUDGET) (hmb : ∀ a, Fl4MinBudgetAgreeA ar a) + (rep : Bool) {cptr : U64} {info : msginfo.MessageInfo} {buf : U64} {r1 : cspace.Resolved} + {e : Aeneas.Std.Array obj.Slot 3#usize} + (einfo : msg.msg_info st s (L.curW s) = ok info) + (ebuf : msg.lookup_ipc_buffer st s false (L.curW s) = ok buf) + (elr : cspace.lookup_slot st s (L.curW s) cptr = ok (.Ok r1)) + (eex : ipc.lookup_extra_caps st s (L.curW s) buf info = ok (.Ok e)) + {r : U64} {grant : Bool} (hcap : capA st s r1.slot = .Reply r grant) + (hr : r ∈ Rp) {xr : Reply} (hk : σ.kheap (specPtr r) = some (.reply xr)) {d : U64} + (hcl : xr.replyTcb = some (specPtr d)) + (hsnd : ∀ x, σ.kheap σ.curThread = some (.tcb x) → x.tcbFault = none ∧ (grant = false ∨ + ipcBufOfA false x = none ∨ + unat (dataToMessageInfo (x.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0)) + (hrcv : ∀ y, σ.kheap (specPtr d) = some (.tcb y) → + y.tcbState = .blockedOnReply (specPtr r) ∧ y.tcbFault = none) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) (hconv : Fl4TicksToUsLegacyAgree) + (hdsc : ∀ y, σ.kheap (specPtr d) = some (.tcb y) → y.tcbSchedContext ≠ none) + (hnt : ∀ y, σ.kheap (specPtr d) = some (.tcb y) → isEpCap y.tcbTimeoutHandler = false) : + ∃ s3 σ3, syscall.handle_invocation st ar s a false false true true cptr = ok (.Ok (), s3, a) ∧ + (.ok (), σ3) ∈ ((fl4HandleInvocationR (Z := Unit) rep false false true true + (Fl4SpecA64.toBl cptr.bv)).run σ).1 ∧ + syscall.after_invocation st ar s3 a (.Ok ()) = ok (true, s3, a) ∧ L.curW s3 = L.curW s ∧ + (∀ x, σ3.kheap σ3.curThread = some (.tcb x) → x.tcbSchedContext = some σ3.curSc) ∧ + ∃ Q Rl stack', RecvPreA st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack' κ n + (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s3 a σ3 m := by + obtain ⟨mi, sl, c, excaps, buffer, argsU, T⟩ := J.siteA einfo ebuf elr eex + obtain ⟨xc, σ1, σ2, σ3, s2, s3, hkc, hσ1, hsp, hmem, mem3, hdec, hstate, eRun, haft, hcur3, own3, + get2, Q, Rl, stack', P⟩ := + J.replyKeepRecvPreA (lbl := info.label) (len := msgLenA info buf) (buf := buf) (blk := false) (call := false) + (don := true) (fp := true) hA hM hPl hfm hCs h1 hag hmb T.hOS T.cov hcap T.hext T.hSC hr hk hcl hsnd hrcv hmax + htu hconv hnt hdsc + have f1 : σ1.curThread = σ.curThread := by rw [hσ1]; rfl + have perf : (.ok [], σ2) ∈ ((fl4PerformInvocation (Z := Unit) false false true + (.invokeReply (specPtr r) grant)).run σ1).1 := by + refine liftE_memA (mem_bindA.2 ⟨(σ.curThread, σ1), ?_, ?_⟩) + · rw [← f1]; exact Set.mem_singleton _ + · exact mem_bindA.2 ⟨((), σ2), hmem, Set.mem_singleton _⟩ + have F := J.frontOkA rep einfo ebuf elr eex (oper := .invokeReply (specPtr r) grant) + (σ1 := σ1) (σ2 := σ2) (σ3 := σ3) (s2 := s2) (s3 := s3) (fun c hcr lbl args ci sl excaps buffer => by + have h2 : CapEqA (.Reply r grant) c := hcap ▸ hcr.2 + have ec : absCapA (.Reply r grant) = c := h2 + rw [← ec, decodeReply_specA] + exact Set.mem_singleton _) + hsp perf get2 mem3 hdec hstate eRun + exact ⟨s3, σ3, F.1, F.2, haft, hcur3, own3, Q, Rl, stack', P⟩ + +set_option maxHeartbeats 1000000 in +/-- **A passive server's `ReplyRecv` from the syscall entry to `handle_recv`** (module docstring). -/ +theorem JSyscallA.replyPassiveRecvEntryA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hag : fl4MinBudget = MIN_BUDGET) (hmb : ∀ a, Fl4MinBudgetAgreeA ar a) + (rep : Bool) {cptr : U64} {info : msginfo.MessageInfo} {buf : U64} {r1 : cspace.Resolved} + {e : Aeneas.Std.Array obj.Slot 3#usize} + (einfo : msg.msg_info st s (L.curW s) = ok info) + (ebuf : msg.lookup_ipc_buffer st s false (L.curW s) = ok buf) + (elr : cspace.lookup_slot st s (L.curW s) cptr = ok (.Ok r1)) + (eex : ipc.lookup_extra_caps st s (L.curW s) buf info = ok (.Ok e)) + {r : U64} {grant : Bool} (hcap : capA st s r1.slot = .Reply r grant) + (hr : r ∈ Rp) {xr : Reply} (hk : σ.kheap (specPtr r) = some (.reply xr)) {d : U64} + (hcl : xr.replyTcb = some (specPtr d)) + (hsnd : ∀ x, σ.kheap σ.curThread = some (.tcb x) → x.tcbFault = none ∧ (grant = false ∨ + ipcBufOfA false x = none ∨ + unat (dataToMessageInfo (x.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0)) + (hrcv : ∀ y, σ.kheap (specPtr d) = some (.tcb y) → + y.tcbState = .blockedOnReply (specPtr r) ∧ y.tcbFault = none) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) (hconv : Fl4TicksToUsLegacyAgree) + (hdsc : ∀ y, σ.kheap (specPtr d) = some (.tcb y) → y.tcbSchedContext = none) + (hsw : scWithReply (specPtr r) σ = some σ.curSc) + (hhd : ∀ y k, σ.kheap σ.curSc = some (.schedContext y k) → y.scReplies.head? = some (specPtr r)) + (hnt : ∀ y, σ.kheap (specPtr d) = some (.tcb y) → isEpCap y.tcbTimeoutHandler = false) : + ∃ s3 σ3, syscall.handle_invocation st ar s a false false true true cptr = ok (.Ok (), s3, a) ∧ + (.ok (), σ3) ∈ ((fl4HandleInvocationR (Z := Unit) rep false false true true + (Fl4SpecA64.toBl cptr.bv)).run σ).1 ∧ + syscall.after_invocation st ar s3 a (.Ok ()) = ok (true, s3, a) ∧ L.curW s3 = L.curW s ∧ + (∀ x, σ3.kheap σ3.curThread = some (.tcb x) → x.tcbSchedContext = none) ∧ + ∃ Q Rl stack', RecvPreA st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack' κ n + (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s3 a σ3 m := by + obtain ⟨mi, sl, c, excaps, buffer, argsU, T⟩ := J.siteA einfo ebuf elr eex + obtain ⟨xc, σ1, σ2, σ3, s2, s3, hkc, hσ1, hsp, hmem, mem3, hdec, hstate, eRun, haft, hcur3, own3, + get2, Q, Rl, stack', P⟩ := + J.replyPassiveRecvPreA (lbl := info.label) (len := msgLenA info buf) (buf := buf) (blk := false) (call := false) + (don := true) (fp := true) hA hM hPl hfm hCs h1 hag hmb T.hOS T.cov hcap T.hext T.hSC hr hk hcl hsnd hrcv hmax + htu hconv hnt hdsc hsw hhd + have f1 : σ1.curThread = σ.curThread := by rw [hσ1]; rfl + have perf : (.ok [], σ2) ∈ ((fl4PerformInvocation (Z := Unit) false false true + (.invokeReply (specPtr r) grant)).run σ1).1 := by + refine liftE_memA (mem_bindA.2 ⟨(σ.curThread, σ1), ?_, ?_⟩) + · rw [← f1]; exact Set.mem_singleton _ + · exact mem_bindA.2 ⟨((), σ2), hmem, Set.mem_singleton _⟩ + have F := J.frontOkA rep einfo ebuf elr eex (oper := .invokeReply (specPtr r) grant) + (σ1 := σ1) (σ2 := σ2) (σ3 := σ3) (s2 := s2) (s3 := s3) (fun c hcr lbl args ci sl excaps buffer => by + have h2 : CapEqA (.Reply r grant) c := hcap ▸ hcr.2 + have ec : absCapA (.Reply r grant) = c := h2 + rw [← ec, decodeReply_specA] + exact Set.mem_singleton _) + hsp perf get2 mem3 hdec hstate eRun + exact ⟨s3, σ3, F.1, F.2, haft, hcur3, own3, Q, Rl, stack', P⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3D1bA.lean b/proofs/Fl4ProofsA64/P3/Stage3D1bA.lean new file mode 100644 index 000000000..213d7d948 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3D1bA.lean @@ -0,0 +1,237 @@ +/- +P3 (AArch64, merge line, record edit D1b, Architect-approved): **the clause shapes of the three record additions** and +of the narrowed VSpace typing. Definitions byte-identical to the Untyped line's `P3/UtD1bA` / `UtZombieA` (that line +removes its copies at its merge), upstream of the record so that `EntryA` can use them. + +* `EntryInvA3`: `P2.S1b.EntryInvA` with `vs` narrowed to `VsTyR` over the installed pools (`PoolSet`). +* `UtVsOkA`: the installed pools' and reachable tables' words, and the ASID table's, are placed (A64 P1's `VrOK`). +* `UtBufAlA`: every TCB of `T` has an IPC buffer aligned to 1024. `UtZombieA`: a `Zombie`'s count fits its object. +* `utVsTyR_keepA`, `utVsPt_keepA`: writes missing the reachable words keep the typing and the reachable set. + +No named premise. +-/ +import Fl4ProofsA64.P3.FrameA +import Fl4ProofsA64.P1.MdbA64 +import Fl4ProofsA64.P1.QueueReach +import Fl4ProofsA64.P1.QueueVsWs +import Fl4ProofsA64.P1.S1aNonUser +import Fl4ProofsA64.P2.S1bLay + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4SpecA64 Classical + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} (st : machine.Store S) [L : LawfulStore st] + +/-- **`EntryInvA` with the narrowed VSpace typing** (module docstring). -/ +structure EntryInvA3 (H : Heap) (Cs OS U Ac Pp Tb : Set U64) (Lv : U64 → Nat) (s : S) : Prop where + heap : HeapOK H + bxi : BXI st H.tcbs Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s + os : OSOK H.tcbs H.omega H.frame OS + user : Fl4ProofsA64.P1.UserOK st H.tcbs Cs H.omega H U s + vs : VsTyR st (L.ksb s) (PoolSet st Pp s) ∅ Tb Lv s + ksw : (L.ksb s).val + 788736 ≤ 2 ^ 64 + +/-- The installed pools and the tables reachable from them. -/ +def utVsPtA (Pp Tb : Set U64) (Lv : U64 → Nat) (s : S) : Set U64 := + PoolSet st Pp s ∪ VsRt st (PoolSet st Pp s) ∅ Tb Lv s + +/-- **The reachable VSpace words are placed** (module docstring). -/ +def UtVsOkA (H : Heap) (OS V Pp Tb : Set U64) (Lv : U64 → Nat) (s : S) : Prop := + VrOK H.omega H.frame H.scs H.scBits OS V ∅ (L.ksb s) (utVsPtA st Pp Tb Lv s) + +/-- **`hsal`** (module docstring). -/ +def UtBufAlA (T : Set U64) (σ : Fl4SpecA64.State Unit) : Prop := + ∀ t ∈ T, ∀ y, σ.kheap (specPtr t) = some (.tcb y) → y.tcbIpcBuffer.toNat % 1024 = 0 + +variable {st} +variable {H : Heap} {OS V Pp Tb : Set U64} {Lv : U64 → Nat} {s s' : S} + +theorem EntryInvA3.of_full {Cs U Ac : Set U64} (h : Fl4ProofsA64.P2.S1b.EntryInvA st H Cs OS U Ac Pp Tb Lv s) : + EntryInvA3 st H Cs OS U Ac Pp Tb Lv s := by + refine ⟨h.heap, h.bxi, h.os, h.user, ⟨fun o h1 h2 h8 hn => ?_, fun p hp => h.vs.pool p hp.1, + fun p hp _ => h.vs.desc p hp⟩, h.ksw⟩ + exact ⟨h.vs.tbl o h1 h2 h8 hn, o, h1, h2, h8, rfl, hn⟩ + +theorem utVsPtA_sub : utVsPtA st Pp Tb Lv s ⊆ Pp ∪ Tb := fun _ h => + h.imp (fun h => h.1) (fun h => h.1) + +/-- **The narrowed typing is kept** by a step that keeps or zeroes every reachable VSpace word and keeps the ASID +table. -/ +theorem utVsTyR_keepA (h : VsTyR st (L.ksb s) (PoolSet st Pp s) ∅ Tb Lv s) (hk : L.ksb s' = L.ksb s) + (k : VsZ st (L.ksb s) (utVsPtA st Pp Tb Lv s) s s') (hps : PoolSet st Pp s' = PoolSet st Pp s) : + VsTyR st (L.ksb s') (PoolSet st Pp s') ∅ Tb Lv s' := by + rw [hk, hps] + have hP : PoolSet st Pp s ⊆ utVsPtA st Pp Tb Lv s := Set.subset_union_left + have hT : VsRt st (PoolSet st Pp s) ∅ Tb Lv s ⊆ utVsPtA st Pp Tb Lv s := Set.subset_union_right + exact (h.sub.keepZ hP hT k).toRsub VsRt_sub (hr_rt (Set.empty_subset _)) + +/-- Every reachable table of a typing is one of its tables. -/ +theorem vsReach_memA {kb : U64} {Pt : Set U64} (h : VsTyR st kb Pt ∅ Tb Lv s) {p : U64} + (r : VsReach st Pt ∅ Lv s p) : p ∈ Tb := by + induction r with + | hr h => exact absurd h (Set.notMem_empty _) + | root0 hp h1 h2 h0 h3 => exact ((h.pool _ hp _ h1 h2 h0).1 h3).1 + | root2 hp h1 h2 h3 => + refine ((h.pool _ hp _ h1 h2 fun e => ?_).2 h3).1 + rw [e] at h3 + exact absurd (congrArg (fun w : U64 => w.val) h3) (by simp) + | step r hl h1 h2 h3 ih => exact (h.desc _ ih r hl _ h1 h2 h3).1 + +/-- (record edit #8) **Tables beyond the typed ones are not reached**: the typing closes the reachable set inside +`Tb`, so growing `Tb` (Retype's new tables) keeps it. -/ +theorem utVsPtA_growA {Tb' : Set U64} (h : VsTyR st (L.ksb s) (PoolSet st Pp s) ∅ Tb Lv s) (sub : Tb ⊆ Tb') : + utVsPtA st Pp Tb' Lv s = utVsPtA st Pp Tb Lv s := by + unfold utVsPtA + congr 1 + ext p + exact ⟨fun g => ⟨vsReach_memA h g.2, g.2⟩, fun g => ⟨sub g.1, g.2⟩⟩ + +/-- (record edit #8) **One descriptor written into the reachable tables grows the reachable set only by the new +tables** `N` (a pool assign's root, a mapped page table): the written word `x` points into `N`, `N` is closed under its +own table descriptors, every other reachable word is kept, and the installed pools are the same. The depths of the old +tables agree, because both typings fix them along every path. -/ +theorem utVsPtA_wrA {Tb' : Set U64} {Lv' : U64 → Nat} {N : Set U64} {x : U64} {s' : S} + (h : VsTyR st (L.ksb s) (PoolSet st Pp s) ∅ Tb Lv s) (h' : VsTyR st (L.ksb s') (PoolSet st Pp s') ∅ Tb' Lv' s') + (hP : PoolSet st Pp s' = PoolSet st Pp s) + (same : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → b ≠ x → L.mem s' b = L.mem s b) + (x0 : L.mem s' x ≠ 0#u64 → (L.mem s' x &&& 3#u64) = 0#u64 → L.mem s' x ∈ N) + (x2 : (L.mem s' x &&& 3#u64) = 2#u64 → (L.mem s' x &&& ~~~3#u64) ∈ N) + (x3 : (L.mem s' x &&& 3#u64) = 3#u64 → wa (L.mem s' x &&& vspace.ADDR_MASK) layout.PPTR_BASE ∈ N) + (cl : ∀ q ∈ N, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → (L.mem s' (wa q o) &&& 3#u64) = 3#u64 → + wa (L.mem s' (wa q o) &&& vspace.ADDR_MASK) layout.PPTR_BASE ∈ N) : + utVsPtA st Pp Tb' Lv' s' ⊆ utVsPtA st Pp Tb Lv s ∪ N := by + rw [hP] at h' + have key : ∀ q, VsReach st (PoolSet st Pp s) ∅ Lv' s' q → + (VsReach st (PoolSet st Pp s) ∅ Lv s q ∧ Lv' q = Lv q) ∨ q ∈ N := fun q r => by + induction r with + | hr g => exact absurd g (Set.notMem_empty _) + | @root0 p o hp h1 h2 h0 h3 => + by_cases e : wa p o = x + · rw [e] at h0 h3 ⊢; exact Or.inr (x0 h0 h3) + have m := same _ (Or.inl ⟨p, Or.inl hp, o, h1, h2, rfl⟩) e + have a := (h'.pool p hp o h1 h2 h0).1 h3 + rw [m] at h0 h3 a ⊢ + exact Or.inl ⟨.root0 hp h1 h2 h0 h3, a.2.trans ((h.pool p hp o h1 h2 h0).1 h3).2.symm⟩ + | @root2 p o hp h1 h2 h3 => + by_cases e : wa p o = x + · rw [e] at h3 ⊢; exact Or.inr (x2 h3) + have m := same _ (Or.inl ⟨p, Or.inl hp, o, h1, h2, rfl⟩) e + have nz : L.mem s' (wa p o) ≠ 0#u64 := fun z => by + rw [z] at h3 + exact absurd (congrArg (fun w : U64 => w.val) h3) (by simp) + have a := (h'.pool p hp o h1 h2 nz).2 h3 + rw [m] at h3 nz a ⊢ + exact Or.inl ⟨.root2 hp h1 h2 h3, a.2.trans ((h.pool p hp o h1 h2 nz).2 h3).2.symm⟩ + | @step p o r hl h1 h2 h3 ih => + rcases ih with ⟨r0, el⟩ | n + swap + · exact Or.inr (cl p n o h1 h2 h3) + by_cases e : wa p o = x + · rw [e] at h3 ⊢; exact Or.inr (x3 h3) + have pT := vsReach_memA h r0 + have m := same _ (Or.inl ⟨p, Or.inr ⟨pT, r0⟩, o, h1, h2, rfl⟩) e + have pT' : p ∈ Tb' := vsReach_memA h' r + have a := (h'.desc p pT' r hl o h1 h2 h3).2 + rw [m] at h3 a ⊢ + have l0 : Lv p < 3 := el ▸ hl + exact Or.inl ⟨.step r0 l0 h1 h2 h3, by rw [a, el, (h.desc p pT r0 l0 o h1 h2 h3).2]⟩ + rintro q (hq | ⟨-, hr⟩) + · rw [hP] at hq + exact Or.inl (Or.inl hq) + · rw [hP] at hr + rcases key q hr with ⟨r0, -⟩ | n + · exact Or.inl (Or.inr ⟨vsReach_memA h r0, r0⟩) + · exact Or.inr n + +/-- (record edit #8) The installed pools stay across a step keeping the ASID table. -/ +theorem poolSet_asidA (hk : L.ksb s' = L.ksb s) (k : ∀ b, AsW (L.ksb s) b → L.mem s' b = L.mem s b) : + PoolSet st Pp s' = PoolSet st Pp s := by + have e : ∀ o : U64, 772352 ≤ o.val → o.val < 773376 → o.val % 8 = 0 → + L.mem s' (wa (L.ksb s') o) = L.mem s (wa (L.ksb s) o) := fun o h1 h2 h8 => by + rw [hk]; exact k _ ⟨o, h1, h2, h8, rfl⟩ + ext p + constructor <;> rintro ⟨hp, o, h1, h2, h8, hm, hn⟩ + · exact ⟨hp, o, h1, h2, h8, (e o h1 h2 h8).symm.trans hm, hn⟩ + · exact ⟨hp, o, h1, h2, h8, (e o h1 h2 h8).trans hm, hn⟩ + +/-- The installed pools only read the ASID table. -/ +theorem poolSet_keepA (hk : L.ksb s' = L.ksb s) + (k : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → L.mem s' b = L.mem s b) : + PoolSet st Pp s' = PoolSet st Pp s := by + have e : ∀ o : U64, 772352 ≤ o.val → o.val < 773376 → o.val % 8 = 0 → + L.mem s' (wa (L.ksb s') o) = L.mem s (wa (L.ksb s) o) := fun o h1 h2 h8 => by + rw [hk]; exact k _ (Or.inr ⟨o, h1, h2, h8, rfl⟩) + ext p + constructor <;> rintro ⟨hp, o, h1, h2, h8, hm, hn⟩ + · exact ⟨hp, o, h1, h2, h8, (e o h1 h2 h8).symm.trans hm, hn⟩ + · exact ⟨hp, o, h1, h2, h8, (e o h1 h2 h8).trans hm, hn⟩ + +/-- **A step missing every reachable VSpace word keeps the typing and the reachable set.** -/ +theorem utVsPt_keepA (h : VsTyR st (L.ksb s) (PoolSet st Pp s) ∅ Tb Lv s) (hk : L.ksb s' = L.ksb s) + (k : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → L.mem s' b = L.mem s b) : + VsTyR st (L.ksb s') (PoolSet st Pp s') ∅ Tb Lv s' ∧ utVsPtA st Pp Tb Lv s' = utVsPtA st Pp Tb Lv s := by + have hps := poolSet_keepA hk k + have ty := utVsTyR_keepA h hk (VsZ.of_keep k) hps + refine ⟨ty, ?_⟩ + have hP : PoolSet st Pp s ⊆ utVsPtA st Pp Tb Lv s := Set.subset_union_left + have hT : VsRt st (PoolSet st Pp s) ∅ Tb Lv s ⊆ utVsPtA st Pp Tb Lv s := Set.subset_union_right + have old' : VsTy st (L.ksb s) (PoolSet st Pp s) (VsRt st (PoolSet st Pp s) ∅ Tb Lv s) Lv s' := + h.sub.keepZ hP hT (VsZ.of_keep k) + have f1 : VsRt st (PoolSet st Pp s') ∅ Tb Lv s' ⊆ VsRt st (PoolSet st Pp s) ∅ Tb Lv s := by + rintro p ⟨-, hr⟩ + rw [hps] at hr + exact old'.reach (Set.empty_subset _) hr + have f2 : VsRt st (PoolSet st Pp s) ∅ Tb Lv s ⊆ VsRt st (PoolSet st Pp s') ∅ Tb Lv s' := by + have key : ∀ p, VsReach st (PoolSet st Pp s) ∅ Lv s p → VsReach st (PoolSet st Pp s') ∅ Lv s' p := by + intro p hr + rw [hps] + induction hr with + | hr hR => exact .hr hR + | @root0 q o hq ho h8 hn h0 => + have e : L.mem s' (wa q o) = L.mem s (wa q o) := k _ (Or.inl ⟨q, hP hq, o, ho, h8, rfl⟩) + rw [← e] + exact .root0 hq ho h8 (e ▸ hn) (e ▸ h0) + | @root2 q o hq ho h8 h2 => + have e : L.mem s' (wa q o) = L.mem s (wa q o) := k _ (Or.inl ⟨q, hP hq, o, ho, h8, rfl⟩) + rw [← e] + exact .root2 hq ho h8 (e ▸ h2) + | @step q o hq hl ho h8 h3 ih => + have hqR : q ∈ VsRt st (PoolSet st Pp s) ∅ Tb Lv s := ⟨(h.sub.reach (Set.empty_subset _) hq |>.1), hq⟩ + have e : L.mem s' (wa q o) = L.mem s (wa q o) := k _ (Or.inl ⟨q, hT hqR, o, ho, h8, rfl⟩) + rw [← e] + exact .step ih hl ho h8 (e ▸ h3) + exact fun p hp => ⟨hp.1, key p hp.2⟩ + unfold utVsPtA + rw [Set.Subset.antisymm f1 f2, hps] + +/-- `VrOK` is monotone down in the table pages. -/ +theorem vrOK_monoPtA {Ω Sg Scs Vc Ub Pt Pt' : Set U64} {Z : U64 → Nat} {kb : U64} + (h : VrOK Ω Sg Scs Z OS Vc Ub kb Pt) (hs : Pt' ⊆ Pt) : VrOK Ω Sg Scs Z OS Vc ∅ kb Pt' := by + have m : ∀ b, VsR kb Pt' b → VsR kb Pt b := fun b hb => hb.imp (ptw_mono hs) id + exact ⟨fun b hb => h.al b (m b hb), fun b hb => h.om b (m b hb), fun b hb => h.sg b (m b hb), + fun b hb => h.sc b (m b hb), fun b hb => h.os b (m b hb), fun b hb => h.vc b (m b hb), + fun _ _ hn => hn, fun b hb => h.ks b (m b hb)⟩ + +end + +section +variable {S : Type} (st : machine.Store S) [L : LawfulStore st] + +/-- **[J]** A `Zombie`'s count fits its object (l4v `valid_cap`: `zombie_cte_num`). -/ +def UtZombieA (OS : Set U64) (s : S) : Prop := + ∀ y ∈ OS, ∀ p t n, capA st s y = .Zombie p t n → + (t = capability.ZOMBIE_TCB → n.val ≤ 7) ∧ (t ≠ capability.ZOMBIE_TCB → t.val < 64 ∧ n.val ≤ 2 ^ t.val) + +variable {st} + +theorem UtZombieA.keepA {OS : Set U64} {s s' : S} (capE : ∀ u ∈ OS, capA st s' u = capA st s u) + (h : UtZombieA st OS s) : UtZombieA st OS s' := fun y hy p t n e => h y hy p t n ((capE y hy).symm.trans e) + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3DecErrAllA.lean b/proofs/Fl4ProofsA64/P3/Stage3DecErrAllA.lean new file mode 100644 index 000000000..71b62adc2 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3DecErrAllA.lean @@ -0,0 +1,133 @@ +/- +P3 (aarch64, A64-P3 Stage 3 assembly): **`DecErrSpecA` for every cap**, from the per-group pieces +(`DecErrSpecA.of_groups`, `rest_of`): `JSyscallA.decErrSpecA`. + +Named premises: the union of the pieces', none new. [P] `ArchTotal`, `ArchPlat`, `ArchKeepsA`, `MaxSpi`, `IrqPlatA`, +`ScCfgTimeAgreeA`, `VcpuPlatAgreeA`, `VcpuArchKeepsA`, `A64FrameMasks`, `hfan`, `hv`, `S2IpaE`, `numDomains = 1`; +[S] `numNodes = 1`, `hCs`, `hCsN`, the VCPU `hNoS2` / `hAgreeWS` / `hAgreeRun`; [J] `hsb`, `hYAs`, `hFI`, `hcO`, `hD`, +`hPtMap`, `hFrMap`, `hFrSub`, `hAsidAssign`. Taken from the record here: `PtCapInA`, `hvcR`, the Domain piece's +schedule facts. + +New module. +-/ +import Fl4ProofsA64.P3.Stage3DecErrCnA +import Fl4ProofsA64.P3.Stage3DecErrPtA +import Fl4ProofsA64.P3.Stage3DecErrDomA +import Fl4ProofsA64.P3.Stage3DecErrIrqA +import Fl4ProofsA64.P3.Stage3DecErrScA +import Fl4ProofsA64.P3.Stage3DecErrScCapA +import Fl4ProofsA64.P3.Stage3DecErrUtA +import Fl4ProofsA64.P3.Stage3DecErrVsA +import Fl4ProofsA64.P3.Stage3DecErrTcbA +import Fl4ProofsA64.P3.Stage3DecErrVcpuA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.ReplyErr +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3 Fl4ProofsA64.P3.Endp + Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4ProofsA64.P3.Vcpu + Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {ar : machine.Arch A} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- **`DecErrSpecA` for every cap kind** (module docstring). -/ +theorem _root_.Fl4ProofsA64.P3.JSyscallA.decErrSpecA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) (K : ArchKeepsA ar AR) (hIP : IrqPlatA st ar) + (hT : ScCfgTimeAgreeA ar a) (hP : VcpuPlatAgreeA ar) (hAK : VcpuArchKeepsA ar AR) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) (hfan : Fl4SpecA64.retypeFanOutLimit = 256#64) + (hv : ∃ vi, ar.virtualization a = ok vi ∧ (vi = 0#u64 ↔ fl4A64Virt = false)) + (hIpa : S2IpaE ar a) (hnum : numDomains = 1) + (hCs : ∀ c ∈ Z.Cs, c.val < Fl4SpecA64.fl4A64Platform.numNodes) (hCsN : ∀ c ∈ Z.Cs, c.val < n) + (h1 : Fl4SpecA64.fl4A64Platform.numNodes = 1) + (hsb : ∀ x0 : U64, x0 ≠ 0#u64 → ∀ p sb, capAtA (L.mem s) x0 = .SchedContext p sb → sb.val < 64) + (hYAs : ∀ y ∈ Z.Y, ∀ j : U64, j.val < 512 → ¬ AsW (L.ksb s) (peAF y j)) + (hFI : FinInvA Z.th (L.mem s) σ Z.G) (hcO : ∀ y ∈ OS, obj.sext48 y = ok y) + (hD : ∀ u frame fi, capA st s u = .Untyped frame 12#u64 fi false → + cdt.ensure_no_children st s u = ok (core.result.Result.Ok ()) → DeadDrop4KA Z H frame fi) + (hPtMap : ∀ (cap : capability.Cap) (lbl ptr : U64) (mp : Bool) (asid va : U64), cap = .PageTable ptr mp asid va → + lbl = fl4_abi.label.ARMPageTableMap → + UnmPtA Z.S1 Z.Y OS H.tcbs (L.mem s) σ ptr ∧ ptr ∉ S2PgE Z.X Z.T2 ∧ ptr ∉ Z.Y) + (hFrMap : ∀ (cap : capability.Cap) (lbl fp : U64) (sz : capability.FrameSize) (rt : U64) (dev mp e : Bool) + (asid va : U64), cap = .Frame fp sz rt dev mp e asid va → lbl = fl4_abi.label.ARMPageMap → FrPlA sz fp mp asid) + (hFrSub : ∀ (cap : capability.Cap) (lbl : U64) (cte : CslotPtr) (fp : U64) (sz : capability.FrameSize) (rt : U64) + (dev mp e : Bool) (asid va : U64), cap = .Frame fp sz rt dev mp e asid va → + lbl = fl4_abi.label.ARMPageDeriveSubframe → sz = capability.FrameSize.Large → dev = false → + (fp.val % 2 ^ 21 = 0 ∧ CapRt.Canonical fp) ∧ + ∀ w ∈ Z.G.chains.flatten, ∀ (q vm' : U64), fp.val ≤ q.val → q.val < fp.val + 2 ^ 21 → + OvlA (absCapA (.Frame q .Small vm' false false false 0#u64 0#u64)) (absCapA (capAtA (L.mem s) w)) → + isFrameA (capAtA (L.mem s) w) = true ∧ ∃ ch ∈ Z.G.chains, cslotAddrA cte ∈ ch ∧ w ∈ ch) + (hAsidAssign : ∀ (cap : capability.Cap) (lbl p base : U64), cap = .AsidPool p base → + lbl = fl4_abi.label.ARMASIDPoolAssign → + ∀ u ∈ OS, ∀ k root oldAsid oldVa, capA st s u = assignUnmappedA k root oldAsid oldVa → + UnmRootAssignA k (L.mem s) σ root) + (hNoS2 : ∀ info buf (e : Array obj.Slot 3#usize), msg.msg_info st s (L.curW s) = ok info → + msg.lookup_ipc_buffer st s false (L.curW s) = ok buf → + ipc.lookup_extra_caps st s (L.curW s) buf info = ok (.Ok e) → + ∀ p sz r d mp ept asid va, capA st s (exAtA e 0) = .Frame p sz r d mp ept asid va → ept = false) + (hAgreeWS : ∀ info buf (e : Array obj.Slot 3#usize), msg.msg_info st s (L.curW s) = ok info → + msg.lookup_ipc_buffer st s false (L.curW s) = ok buf → + ipc.lookup_extra_caps st s (L.curW s) buf info = ok (.Ok e) → + ∀ p sz r mp ept asid va off mask, capA st s (exAtA e 0) = .Frame p sz r false mp ept asid va → + msg.syscall_arg st s (L.curW s) buf 0#u64 = ok off → msg.syscall_arg st s (L.curW s) buf 1#u64 = ok mask → + mask ≠ 0#u64 → FrameInputAgreeA st s σ (wa p off)) + (hAgreeRun : ∀ buf, msg.lookup_ipc_buffer st s false (L.curW s) = ok buf → + ∀ p sz r mp ept asid va off mask, capA st s (sbWordA (L.curW s)) = .Frame p sz r false mp ept asid va → + msg.syscall_arg st s (L.curW s) buf 0#u64 = ok off → msg.syscall_arg st s (L.curW s) buf 1#u64 = ok mask → + mask ≠ 0#u64 → FrameInputAgreeA st s σ (wa p off)) + {calling blocking donate : Bool} {cptr : U64} : + DecErrSpecA st ar Z.th s a σ calling blocking donate cptr := by + have hcurT : L.curW s ∈ Z.T := J.sr.curT + have J3 := J.toJStage3A + obtain ⟨Pt, Ub, Pp7, Tb7, Hr, Te, Kt, Lv7, Re, bits7, KLv, W⟩ := J7A.unp J3.j7 + have hvcR : ∀ v ∈ Z.V, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → + wa (L.curW s) K ≠ v ∧ wa (L.curW s) K ≠ wa v 24#u64 := fun v hv K _ hK => by + have hpl := J3.vcPlacedA W + have h0 : wa v 0#u64 = v := by apply UScalar.eq_of_val_eq; rw [wa_valP]; simp [UScalar.hBounds] + refine ⟨?_, (hpl.disj_tcb v hv _ hcurT 24#u64 K (by decide) (by omega)).symm⟩ + have := (hpl.disj_tcb v hv _ hcurT 0#u64 K (by decide) (by omega)).symm + rwa [h0] at this + refine DecErrSpecA.of_groups (DecErrSpecPA.untyped hA J hfm hfan hv) (DecErrSpecPA.cnode hA J) + (DecErrSpecPA.schedCtl hA hT J hsb) + (DecErrSpecPA.archVs K J hvcR hYAs hIpa J.ptCapIn hFI hcO hD hPtMap hFrMap hFrSub hAsidAssign) + (DecErrSpecPA.rest_of J.j.1.place ?_) + intro info buf r1 e x s' a' mi sl c excaps buffer args h1' h2 h3 h4 hd hne hR + -- the open Rest caps: page tables, Domain, IRQControl, Thread, SchedContext, VCPU + intro hlab hsl hcr hok hab hbv gmi ggc glb gex gmrs + have go' : ∀ {P : capability.Cap → Prop}, DecErrSpecPA st ar Z.th s a σ calling blocking donate cptr P → + P (capA st s r1.slot) → ∃ (e2 : error.SysError) (e' : Fl4SpecA64.SyscallError), x = .Sys e2 ∧ + VsErrRelA x e' ∧ (.error e', σ) ∈ ((fl4DecodeInvocation (Z := Unit) calling false mi.miLabel args + (Fl4SpecA64.toBl cptr.bv) sl c excaps buffer).run σ).1 := fun Q h => + Q info buf r1 e x s' a' mi sl c excaps buffer args h1' h2 h3 h4 hd hne h hlab hsl hcr hok hab hbv gmi ggc glb gex gmrs + obtain ⟨hrest, hs, hi⟩ := hR + have hwf := hcr.1 + cases hc : capA st s r1.slot with + | PageTable => exact go' (DecErrSpecPA.pageTable K J hvcR hYAs hIpa J.ptCapIn hFI hcO hD hPtMap) (by rw [hc]; trivial) + | Domain => exact go' (DecErrSpecPA.domain J hnum) (by rw [hc]; trivial) + | IrqControl => exact go' (DecErrSpecPA.irqControl hM hIP J) (by rw [hc]; trivial) + | Thread => exact go' (DecErrSpecPA.thread hA J hCs h1 hPl) (by rw [hc]; trivial) + | SchedContext => exact go' (DecErrSpecPA.schedContext J hCs h1) (by rw [hc]; trivial) + | Vcpu => exact go' (DecErrSpecPA.vcpu hP hAK J hCs hCsN h1 hNoS2 hAgreeWS hAgreeRun) (by rw [hc]; trivial) + | Untyped => exact absurd (Or.inl (by rw [hc]; trivial)) hrest + | CNode => exact absurd (Or.inr (Or.inl (by rw [hc]; trivial))) hrest + | SchedControl => exact absurd (Or.inr (Or.inr (Or.inl (by rw [hc]; trivial)))) hrest + | IrqHandler => exact absurd (show IrqHandlerCapA (capA st s r1.slot) by rw [hc]; trivial) hi + | _ => + first + | exact absurd (show SimpleCapA (capA st s r1.slot) by rw [hc]; trivial) hs + | exact absurd (Or.inr (Or.inr (Or.inr (show VsCapA (capA st s r1.slot) by + rw [hc]; exact ⟨trivial, fun h => h⟩)))) hrest + +end + +end + +end Fl4ProofsA64.P3.ReplyErr diff --git a/proofs/Fl4ProofsA64/P3/Stage3DecErrBaseA.lean b/proofs/Fl4ProofsA64/P3/Stage3DecErrBaseA.lean new file mode 100644 index 000000000..d8abb2209 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3DecErrBaseA.lean @@ -0,0 +1,119 @@ +/- +P3 (aarch64, A64-P3 Lift 1b, batch a): **`DecErrSpecA` split per cap group** and the helpers shared by the per-group +pieces. + +* Cap groups: `UtCapA` (Untyped), `CnCapA` (CNode), `SchedCtlCapA` (SchedControl), `VsCapA` (the VSpace / ASID / Frame / + Stage-2 root / High VSpace kinds of `ArchTopA`, no page tables), `PtCapA` (page tables, in `RestCapA`), `RestCapA` + (everything else). +* `DecErrSpecA.of_groups`: `DecErrSpecA` from one `DecErrSpecPA` per group (each discharged in its own module). +* `sys_of_vsErrRelA`, `genIff_labelA`: helpers of the pieces. + +No new named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3ReplyErrTopA +import Fl4ProofsA64.P3.ArchTopA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.ReplyErr +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3 Fl4ProofsA64.P3.Endp + Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +/-- The Untyped caps. -/ +def UtCapA : capability.Cap → Prop + | .Untyped .. => True + | _ => False + +/-- The CNode caps. -/ +def CnCapA : capability.Cap → Prop + | .CNode .. => True + | _ => False + +/-- The SchedControl caps. -/ +def SchedCtlCapA : capability.Cap → Prop + | .SchedControl _ => True + | _ => False + +/-- The page-table caps (left open: a mapped Stage-2 page table's spec cap carries the 47-bit-wrapped address, which +`CapEqA` allows and `decInv_arch_jA`'s `absCapA cap` does not match). -/ +def PtCapA : capability.Cap → Prop + | .PageTable .. => True + | _ => False + +/-- The VSpace / ASID / Frame / Stage-2 root / High VSpace caps other than page tables. -/ +def VsCapA (c : capability.Cap) : Prop := IsArchVsCapA c ∧ ¬ PtCapA c + +/-- The caps no batch-a group covers. -/ +def RestCapA (c : capability.Cap) : Prop := ¬ (UtCapA c ∨ CnCapA c ∨ SchedCtlCapA c ∨ VsCapA c) + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {ar : machine.Arch A} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {th : TypedHeapA} + {calling blocking donate : Bool} {cptr : U64} + +/-- **`DecErrSpecA` from one piece per cap group** (module docstring). -/ +theorem DecErrSpecA.of_groups + (hU : DecErrSpecPA st ar th s a σ calling blocking donate cptr UtCapA) + (hC : DecErrSpecPA st ar th s a σ calling blocking donate cptr CnCapA) + (hS : DecErrSpecPA st ar th s a σ calling blocking donate cptr SchedCtlCapA) + (hV : DecErrSpecPA st ar th s a σ calling blocking donate cptr VsCapA) + (hR : DecErrSpecPA st ar th s a σ calling blocking donate cptr RestCapA) : + DecErrSpecA st ar th s a σ calling blocking donate cptr := by + intro info buf r1 e x s' a' mi sl c excaps buffer args + by_cases hu : UtCapA (capA st s r1.slot) + · exact fun h1 h2 h3 h4 hd hne _ => hU info buf r1 e x s' a' mi sl c excaps buffer args h1 h2 h3 h4 hd hne hu + by_cases hc : CnCapA (capA st s r1.slot) + · exact fun h1 h2 h3 h4 hd hne _ => hC info buf r1 e x s' a' mi sl c excaps buffer args h1 h2 h3 h4 hd hne hc + by_cases hs : SchedCtlCapA (capA st s r1.slot) + · exact fun h1 h2 h3 h4 hd hne _ => hS info buf r1 e x s' a' mi sl c excaps buffer args h1 h2 h3 h4 hd hne hs + by_cases hv : VsCapA (capA st s r1.slot) + · exact fun h1 h2 h3 h4 hd hne _ => hV info buf r1 e x s' a' mi sl c excaps buffer args h1 h2 h3 h4 hd hne hv + exact fun h1 h2 h3 h4 hd hne _ => hR info buf r1 e x s' a' mi sl c excaps buffer args h1 h2 h3 h4 hd hne + (fun h => h.elim hu fun h => h.elim hc fun h => h.elim hs hv) + +end + +/-! ## Shared helpers of the per-group pieces -/ + +/-- A related error is a syscall error (`Sys`). -/ +theorem sys_of_vsErrRelA {x : error.Exc} {e' : Fl4SpecA64.SyscallError} (h : VsErrRelA x e') : + ∃ e2, x = .Sys e2 := by + rcases h with h | ⟨n, rfl, _⟩ + · cases x <;> simp_all [SysErrRelA] + · exact ⟨_, rfl⟩ + +/-- `genInvocationType` of the spec label word of FL4 label number `k` (`labNA`) is `g` only there. -/ +theorem genIff_labelA (g : Fl4SpecA64.GenInvocationLabels) (hg : g ≠ .invalidInvocation) (k : Nat) (hk : k < 74) + (h1 : labNA k = .spec (.genInvocationLabel g)) + (h2 : ∀ n, n < 74 → labNA n = .spec (.genInvocationLabel g) → n = k) (lbl : U64) : + Fl4SpecA64.genInvocationType (Fl4SpecA64.specLabelWord (Fl4SpecA64.fl4LabelOf lbl.bv)) = g ↔ lbl.val = k := by + have key : ∀ l : Fl4SpecA64.Fl4Label, + Fl4SpecA64.genInvocationType (Fl4SpecA64.specLabelWord l) = g ↔ l = .spec (.genInvocationLabel g) := by + intro l + cases l with + | spec y => + have e : Fl4SpecA64.invocationType (Fl4SpecA64.specLabelWord (.spec y)) = y := invocationType_fromEnumA y + unfold Fl4SpecA64.genInvocationType + rw [e] + cases y with + | genInvocationLabel g' => simp + | archInvocationLabel x => simp [Ne.symm hg] + | ext x => + have h0 : Fl4SpecA64.specLabelWord (.ext x) = + BitVec.ofNat 64 (Fl4SpecA64.InvocationLabel.genInvocationLabel .invalidInvocation).fromEnum := by + show (0 : Fl4SpecA64.Data) = _; decide + unfold Fl4SpecA64.genInvocationType + rw [h0, invocationType_fromEnumA] + simp [Ne.symm hg] + rw [key, fl4LabelOf_bvA] + by_cases hn : lbl.val < 74 + · exact ⟨fun h => h2 _ hn h, fun h => h ▸ h1⟩ + · rw [labNA_geA (by omega)] + constructor + · intro e; cases e; exact absurd rfl hg + · intro e; omega + +end + +end Fl4ProofsA64.P3.ReplyErr diff --git a/proofs/Fl4ProofsA64/P3/Stage3DecErrCnA.lean b/proofs/Fl4ProofsA64/P3/Stage3DecErrCnA.lean new file mode 100644 index 000000000..8c90870c5 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3DecErrCnA.lean @@ -0,0 +1,223 @@ +/- +P3 (aarch64, A64-P3 Lift 1b, batch a): **`DecErrSpecPA` for CNode caps**. + +* **`cnode.decode.errSpecDRCA`**: labels 21..23 (Revoke / Delete / CancelBadgedSends). A decode `Err` other than + `Preempted` comes only from the prefix (length, target lookup, `full_endpoint`): the deletion loop errs only by + preemption (`cdt.revoke.preA` / `cdt.delete.preA`) and CancelBadgedSends returns `Ok` after `full_endpoint`. So it + needs neither the deletion hooks nor any scheduler relation, only `StateRelA` / `SlotsValidA`. +* **`DecErrSpecPA.cnode`**: the group; the other labels are S3's `cnode_decode_refinesA`, whose premises are the + record's (as in `JSyscallA.cnCopy_notPreemptedA`). + +No named premise beyond P0's `ArchTotal`. New module. +-/ +import Fl4ProofsA64.P3.Stage3DecErrBaseA +import Fl4ProofsA64.P3.CnodeFrontA +import Fl4ProofsA64.P3.CnodeTopA +import Fl4ProofsA64.P3.Stage3ErrPreA +import Fl4ProofsA64.P3.Stage3CnArmFactsA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.ReplyErr +open fl4_core_a64 Fl4ProofsA64.P0 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3 Fl4ProofsA64.P3.Endp + Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +set_option maxHeartbeats 4000000 in +/-- **Revoke / Delete / CancelBadgedSends: a decode `Err` other than `Preempted` is the spec decoder's error** +(module docstring). -/ +theorem cnode.decode.errSpecDRCA (arI : machine.Arch A) (hA : P0.ArchTotal arI) {th : TypedHeapA} (s : S) (a : A) + (σ : Fl4SpecA64.State Unit) (hR : StateRelA th (L.mem s) σ) (hV : SlotsValidA th (L.mem s)) + (lbl : U64) (slbl : Fl4SpecA64.Data) (hlbl : Fl4SpecA64.genInvocationType slbl = cnodeLabelOfA lbl) + (hl : 21 ≤ lbl.val ∧ lbl.val ≤ 23) (length buf : U64) (args : List U64) (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) + (cap : capability.Cap) (ac : Fl4SpecA64.Cap) (hca : CapRelA cap ac) (hcv : CapValidA th cap) + (extra : Aeneas.Std.Array obj.Slot 3#usize) (excaps : List Fl4SpecA64.Cap) : + cnode.decode st arI s a lbl length cap buf extra ⦃ res => + ∀ x, res.1 = .Err x → x ≠ error.Exc.Preempted → ∃ e', + (Fl4SpecA64.decodeCnodeInvocation (Z := Unit) slbl (args.map UScalar.bv) ac excaps).run σ = + ({(.error e', σ)}, False) ∧ SysErrRelA x e' ⦄ := by + have hS : P0.StoreTotal st := L.total + have hg : Fl4SpecA64.genInvocationType slbl ∈ + Fl4SpecA64.GenInvocationLabels.uptoEnum .cnodeRevoke .cnodeRotate := by + rw [hlbl] + have : lbl.val = 21 ∨ lbl.val = 22 ∨ lbl.val = 23 := by omega + rcases this with h | h | h <;> simp [cnodeLabelOfA, h] <;> decide + unfold cnode.decode + have n1 : ¬ lbl < fl4_abi.label.CNodeRevoke := by have := lblA_revoke; scalar_tac + have n2 : ¬ lbl > fl4_abi.label.CNodeRotate := by have := lblA_rotate; scalar_tac + rw [if_neg n1, if_neg n2] + by_cases h2 : length < 2#u64 + · rw [if_pos h2] + simp only [error.truncated, bind_ok] + rw [WP.spec_ok] + intro x hx _ + cases hx + exact ⟨.truncatedMessage, decode_shortA _ _ _ σ hg _ (by simp; scalar_tac), trivial⟩ + rw [if_neg h2] + obtain ⟨y0, y1, rest, rfl⟩ : ∃ y0 y1 rest, args = y0 :: y1 :: rest := by + match args with + | [] => exfalso; simp at hlen; scalar_tac + | [_] => exfalso; simp at hlen; scalar_tac + | y0 :: y1 :: rest => exact ⟨y0, y1, rest, rfl⟩ + simp only [cur_thread_ok, hargs 0#u64 y0 (by simp), hargs 1#u64 y1 (by simp), bind_ok] + simp only [List.map_cons] + unfold cspace.lookup_target_slot + obtain ⟨r1, hr1, e1, he1, hrel1⟩ := spec_existsA + (lookup_slot_for_cnode_op_refinesA th s σ hR hV false cap ac hca hcv y0 y1) + have hlt : (Fl4SpecA64.lookupTargetSlot (Z := Unit) ac (Fl4SpecA64.dataToCptr y0.bv) + (Fl4SpecA64.dataToNat y1.bv)).run σ = ({(e1, σ)}, False) := he1 + rw [hr1]; simp only [bind_ok, core.result.Result.Insts.CoreOpsTry.branch] + have hc : lbl = fl4_abi.label.CNodeRevoke ∨ lbl = fl4_abi.label.CNodeDelete ∨ + lbl = fl4_abi.label.CNodeCancelBadgedSends := by + simp only [u64_eq_iffA, lblA_revoke, lblA_delete, lblA_cancel]; omega + cases r1 with + | Err e => + cases e1 with + | ok _ => exact hrel1.elim + | error e' => + simp only [core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, bind_ok, bind_tc_ok] + rw [WP.spec_ok] + intro x hx _ + cases hx + refine ⟨e', ?_, hrel1⟩ + rcases hc with h | h | h + · exact dec_revoke_errA (by rw [hlbl, h]; simp [cnodeLabelOfA]) hlt + · exact dec_delete_errA (by rw [hlbl, h]; simp [cnodeLabelOfA, fl4_abi.label.CNodeDelete]) hlt + · exact dec_cancel_errA (by rw [hlbl, h]; simp [cnodeLabelOfA, fl4_abi.label.CNodeCancelBadgedSends]) hlt + | Ok dest => + cases e1 with + | error _ => exact hrel1.elim + | ok dsl => + obtain ⟨rfl, pd, hpd1, hpd, hcrd⟩ := hrel1 + have g1 : ¬ lbl ≥ fl4_abi.label.CNodeCopy := by have := lblA_copy; scalar_tac + simp only [g1, ↓reduceIte, bind_ok] + rcases hc with h | h | h + · rw [if_pos h] + step + refine WP.spec_mono (cdt.revoke.preA hA _ _ _) ?_ + intro r hr x hx hne + exact absurd (hr x hx) hne + · have k1 : ¬ lbl = fl4_abi.label.CNodeRevoke := by + rw [h, u64_eq_iffA]; simp [fl4_abi.label.CNodeDelete] + rw [if_neg k1, if_pos h] + step + refine WP.spec_mono (cdt.delete.preA hA _ _ _ _) ?_ + intro r hr x hx hne + exact absurd (hr x hx) hne + · have k1 : ¬ lbl = fl4_abi.label.CNodeRevoke := by + rw [h, u64_eq_iffA]; simp [fl4_abi.label.CNodeCancelBadgedSends] + have k2 : ¬ lbl = fl4_abi.label.CNodeDelete := by + rw [h, u64_eq_iffA]; simp [fl4_abi.label.CNodeCancelBadgedSends, fl4_abi.label.CNodeDelete] + rw [if_neg k1, if_neg k2, if_pos h] + have hAd : AbsSlotA th dsl := ⟨pd, hpd1, Or.inl hpd⟩ + obtain ⟨ad, hcad, hgd⟩ := hR.slot_capA hpd1 hpd + replace hcad : CapRelA (capA st s (cslotAddrA dsl)) ad := hcad + unfold cnode.decode_cancel_badged_sends + obtain ⟨o, ho, hoe, hos⟩ := full_endpoint_relA (capA st s (cslotAddrA dsl)) + simp only [cur_thread_ok, capA_eq, bind_ok, ho] + have hsp := (dec_cancel_okA (excaps := excaps) (rest := rest.map UScalar.bv) + (by rw [hlbl, h]; simp [cnodeLabelOfA, fl4_abi.label.CNodeCancelBadgedSends]) hlt).trans + (cancelArm_runA (σ := σ) dsl hgd) + cases o with + | none => + have hn : ¬ Fl4SpecA64.hasCancelSendRights ad := by + rcases hcad.2.eq_or_pt with e | ⟨_, _, _, e⟩ + · rw [e]; intro h; have := hos.1 h; simp at this + · rw [e]; intro h; simp [Fl4SpecA64.hasCancelSendRights] at h + simp only [hn, ↓reduceIte] at hsp + simp only [error.illegal, bind_ok, uncurry_apply_pair] + rw [WP.spec_ok] + intro x hx _ + cases hx + exact ⟨.illegalOperation, hsp, trivial⟩ + | some pb => + obtain ⟨p, b⟩ := pb + dsimp only + step + split + · step + intro x hx + cases hx + · simp only [bind_ok, uncurry_apply_pair] + rw [WP.spec_ok] + intro x hx + cases hx + +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {Rj : Set U64 → JSlotA S} {ar : machine.Arch A} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} + +set_option maxHeartbeats 4000000 in +/-- **`DecErrSpecPA` for CNode caps** (module docstring). -/ +theorem DecErrSpecPA.cnode (hA : P0.ArchTotal ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st Rj) s a σ m) + {calling blocking donate : Bool} {cptr : U64} : + DecErrSpecPA st ar Z.th s a σ calling blocking donate cptr CnCapA := by + intro info buf r1 e x s' a' mi sl c excaps buffer args einfo ebuf elr eex hd hne hP hlab hsl hcr hok hab hbv + gmi ggc glb gex gmrs + obtain ⟨p, r, gs, g, hc⟩ : ∃ p r gs g, capA st s r1.slot = .CNode p r gs g := by + revert hP + generalize capA st s r1.slot = c0 + intro h + cases c0 <;> first | exact ⟨_, _, _, _, rfl⟩ | exact h.elim + obtain ⟨-, argsU, hargsE, hlen, hargsU⟩ := J.argsLabA einfo hbv gmi gmrs + subst hargsE + obtain ⟨_, _, _, _, _, _, Q⟩ := J.siteA einfo ebuf elr eex + have hPl := J.j.1.place + have hV : SlotsValidA Z.th (L.mem s) := J.j.1.cv + have hR := J.j.1.st + have hcr' : CapRelA (.CNode p r gs g) c := hc ▸ hcr + have hcv : CapValidA Z.th (.CNode p r gs g) := by + have h := hV _ (hab.isSlot hPl) + rw [capAtA_eq (st := st), ← hsl, hc] at h + exact h + have hd0 : cnode.decode st ar s a info.label (msgLenA info buf) (.CNode p r gs g) buf e = ok (.Err x, s', a') := by + have hd2 := hd + rw [hc, decodeCnode_programA, if_neg Bool.false_ne_true] at hd2 + exact hd2 + obtain ⟨slbl, hsl', hcan⟩ := cnode_canonA info.label + have key : ∃ e', (Fl4SpecA64.decodeCnodeInvocation (Z := Unit) slbl (argsU.map UScalar.bv) c + (excaps.map Prod.fst)).run σ = ({(.error e', σ)}, False) ∧ SysErrRelA x e' := by + by_cases h : 21 ≤ info.label.val ∧ info.label.val ≤ 23 + · have K := cnode.decode.errSpecDRCA ar hA s a σ hR hV info.label slbl hsl' h (msgLenA info buf) buf argsU hlen + hargsU _ c hcr' hcv e (excaps.map Prod.fst) + rw [hd0, WP.spec_ok] at K + exact K x rfl hne + · have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA Q.hext Q.hOS Q.cov J.actW J.running J.cur + have hex := ExtraOKA.rel2 hPl hok + have hl' : info.label.val < 21 ∨ 28 < info.label.val ∨ (24 ≤ info.label.val ∧ info.label.val ≤ 27) ∨ + info.label.val = 28 := by omega + have w := cnode_decode_refinesA ar J.toJStage3A.toJEntA (J.toJStage3A.sysPlace Q.hext Q.hOS Q.cov) + RS.sym RS.run RS.cur RS.vs RS.vc (J.j.1.cdt.tree.grow J.j5.2.1 (fun x hx hn => J.j5.2.2 x hx hn)) J.j5.2.1 + J.j5.2.2 info.label slbl hsl' hl' (msgLenA info buf) buf argsU hlen hargsU _ c hcr' hcv (exAtA e 0) + (exAtA e 1) (exAtA_idx0 e) (exAtA_idx1 e) _ hex + rw [hd0, WP.spec_ok] at w + exact w.1.2 + obtain ⟨e', hrun, hrel⟩ := key + have hrel' : VsErrRelA x e' := Or.inl hrel + obtain ⟨e2, hxe⟩ := sys_of_vsErrRelA hrel' + refine ⟨e2, e', hxe, hrel', ?_⟩ + have hc2 : c = absCapA (.CNode p r gs g) := hcr'.2.symm + have hrun' : (Fl4SpecA64.decodeCnodeInvocation (Z := Unit) (Fl4SpecA64.fl4SpecLabel mi.miLabel) + (argsU.map UScalar.bv) c (excaps.map Prod.fst)).run σ = ({(.error e', σ)}, False) := by + rw [hlab, hcan]; exact hrun + have hd' : fl4DecodeInvocation (Z := Unit) calling false mi.miLabel (argsU.map UScalar.bv) + (Fl4SpecA64.toBl cptr.bv) sl c excaps buffer = + Fl4Spec.NonDetE.liftME Fl4Invocation.invokeCNode + (Fl4SpecA64.decodeCnodeInvocation (Z := Unit) (Fl4SpecA64.fl4SpecLabel mi.miLabel) + (argsU.map UScalar.bv) c (excaps.map Prod.fst)) := by + rw [hc2]; rfl + rw [hd', run_liftME_errA hrun'] + exact Set.mem_singleton _ + +end + +end + +end Fl4ProofsA64.P3.ReplyErr diff --git a/proofs/Fl4ProofsA64/P3/Stage3DecErrDomA.lean b/proofs/Fl4ProofsA64/P3/Stage3DecErrDomA.lean new file mode 100644 index 000000000..e4a9e25c2 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3DecErrDomA.lean @@ -0,0 +1,463 @@ +/- +P3 (aarch64, A64-P3 Lift 1b, rest part A): **`DecErrSpecPA` for the Domain cap** (`DomainSetSet`, +`DomainScheduleConfigure`, `DomainScheduleSetStart` and foreign labels). + +Named premises (**`DecErrSpecPA.domain`**): +* [P] `hnum : numDomains = 1` (FL4's one-domain build). +The concrete schedule words and the spec table's length are the record's `domOK` (`NdOKA`, field `len`); no premise. + +New module. +-/ +import Fl4ProofsA64.P3.Stage3DecErrMiscA +import Fl4ProofsA64.P3.DomainSetDispatchA +import Fl4ProofsA64.P3.ScDomainA +import Fl4ProofsA64.P3.Stage3InvSpecL1CtlA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.ReplyErr +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3 Fl4ProofsA64.P3.Endp + Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +/-- The Domain cap. -/ +def DomCapA : capability.Cap → Prop + | .Domain => True + | _ => False + +set_option maxRecDepth 8000 in +theorem labNA_domA : + (∀ n, n < 74 → (labNA n = .spec (.genInvocationLabel .domainSetSet) → n = 33)) ∧ + (∀ n, n < 74 → (labNA n = .spec (.genInvocationLabel .domainScheduleConfigure) → n = 34)) ∧ + (∀ n, n < 74 → (labNA n = .spec (.genInvocationLabel .domainScheduleSetStart) → n = 35)) := by + decide + +theorem labNA_domAtA : + labNA 33 = .spec (.genInvocationLabel .domainSetSet) ∧ + labNA 34 = .spec (.genInvocationLabel .domainScheduleConfigure) ∧ + labNA 35 = .spec (.genInvocationLabel .domainScheduleSetStart) := by + decide + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +set_option maxHeartbeats 4000000 in +/-- **`DecErrSpecPA` for the Domain cap** (module docstring). -/ +theorem DecErrSpecPA.domain {ar : machine.Arch A} + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + (hnum : numDomains = 1) + {calling blocking donate : Bool} {cptr : U64} : + DecErrSpecPA st ar Z.th s a σ calling blocking donate cptr DomCapA := by + have hnd : NdOKA (st := st) s σ := J.toJStage3A.domOK + have hdl : σ.domainList.length = layout.NUM_DOMAIN_SCHEDULES.val := hnd.len + intro info buf r1 e x s' a' mi sl c excaps buffer args einfo _ _ _ hd _ hPc hlab _ hcr hok _ hbv gmi _ _ _ gmrs + obtain ⟨-, argsU, hargsE, hlen, hargs⟩ := J.argsLabA einfo hbv gmi gmrs + subst hargsE + have hcap : capA st s r1.slot = .Domain := by + revert hPc + cases capA st s r1.slot <;> intro hPc <;> first | rfl | exact hPc.elim + rw [hcap] at hd hcr + have hc2 : c = .domainCap := hcr.2.symm + have run_dec : fl4DecodeInvocation (Z := Unit) calling false mi.miLabel (argsU.map UScalar.bv) + (Fl4SpecA64.toBl cptr.bv) sl c excaps buffer = + Fl4Spec.NonDetE.liftME Fl4Invocation.invokeDomain + (decodeDomainInvocation (specLabelWord (fl4LabelOf info.label.bv)) (argsU.map UScalar.bv) excaps) := by + rw [hc2, hlab]; rfl + have wrap : ∀ (e' : SyscallError), SysErrRelA x e' → + (decodeDomainInvocation (Z := Unit) (specLabelWord (fl4LabelOf info.label.bv)) (argsU.map UScalar.bv) + excaps).run σ = ({(.error e', σ)}, False) → + ∃ (e2 : error.SysError) (e'' : SyscallError), x = .Sys e2 ∧ VsErrRelA x e'' ∧ + (.error e'', σ) ∈ ((fl4DecodeInvocation (Z := Unit) calling false mi.miLabel (argsU.map UScalar.bv) + (Fl4SpecA64.toBl cptr.bv) sl c excaps buffer).run σ).1 := by + intro e' hrel hm + rw [run_dec] + exact decErr_ofA (Or.inl hrel) (run_liftME_errA hm) + have errOf : ∀ {y : error.Exc} {s1 : S} {a1 : A}, + (ok (core.result.Result.Err y, s1, a1) : Result (core.result.Result Unit error.Exc × S × A)) = + ok (.Err x, s', a') → x = y := by + intro y s1 a1 h + have := (Prod.mk.inj (okinj h)).1 + injection this with h' + exact h'.symm + have noOk : ∀ {s1 : S} {a1 : A}, + (ok (core.result.Result.Ok (), s1, a1) : Result (core.result.Result Unit error.Exc × S × A)) ≠ + ok (.Err x, s', a') := by + intro s1 a1 h + cases (Prod.mk.inj (okinj h)).1 + have gS : genInvocationType (specLabelWord (fl4LabelOf info.label.bv)) = .domainSetSet ↔ + info.label = fl4_abi.label.DomainSetSet := by + rw [genIff_labelA _ (by decide) 33 (by decide) labNA_domAtA.1 labNA_domA.1, u64_eq_iffA, + Fl4ProofsA64.P0.Consts.fl4_abi.label.DomainSetSet.val_eq] + have gC : genInvocationType (specLabelWord (fl4LabelOf info.label.bv)) = .domainScheduleConfigure ↔ + info.label = fl4_abi.label.DomainScheduleConfigure := by + rw [genIff_labelA _ (by decide) 34 (by decide) labNA_domAtA.2.1 labNA_domA.2.1, u64_eq_iffA, + Fl4ProofsA64.P0.Consts.fl4_abi.label.DomainScheduleConfigure.val_eq] + have gT : genInvocationType (specLabelWord (fl4LabelOf info.label.bv)) = .domainScheduleSetStart ↔ + info.label = fl4_abi.label.DomainScheduleSetStart := by + rw [genIff_labelA _ (by decide) 35 (by decide) labNA_domAtA.2.2 labNA_domA.2.2, u64_eq_iffA, + Fl4ProofsA64.P0.Consts.fl4_abi.label.DomainScheduleSetStart.val_eq] + have len0 : ∀ k : U64, msgLenA info buf < k → argsU.length < k.val := by + intro k h; rw [← hlen]; scalar_tac + have lenGe : ∀ k : U64, ¬ msgLenA info buf < k → k.val ≤ argsU.length := by + intro k h; rw [← hlen]; scalar_tac + have nDom : layout.NUM_DOMAINS.val = 1 := by + simp [layout.NUM_DOMAINS, fl4_abi.config.NUM_DOMAINS] + have nSch : layout.NUM_DOMAIN_SCHEDULES.val = 2 := by + simp [layout.NUM_DOMAIN_SCHEDULES, fl4_abi.config.NUM_DOMAIN_SCHEDULES] + have hdl2 : σ.domainList.length = 2 := hdl.trans nSch + rw [decodeDomain_programA] at hd + simp only [Bool.false_eq_true, ↓reduceIte] at hd + unfold domain.decode at hd + simp only [cur_thread_ok, bind_ok] at hd + by_cases hS : info.label = fl4_abi.label.DomainSetSet + · rw [if_pos hS] at hd + have dm : decodeDomainInvocation (Z := Unit) (specLabelWord (fl4LabelOf info.label.bv)) (argsU.map UScalar.bv) + excaps = decodeDomainSet (argsU.map UScalar.bv) excaps := by + unfold decodeDomainInvocation; rw [gS.2 hS] + rw [dm] at wrap + by_cases h0 : msgLenA info buf = 0#u64 + · rw [if_pos h0] at hd + simp only [error.truncated, bind_ok] at hd + have : argsU = [] := List.length_eq_zero_iff.1 (by rw [← hlen, h0]; rfl) + subst this + refine wrap .truncatedMessage (by rw [errOf hd]; trivial) ?_ + rw [domainSet_specA]; rfl + rw [if_neg h0] at hd + obtain ⟨y0, rest, rfl⟩ : ∃ y0 rest, argsU = y0 :: rest := by + cases h : argsU with + | nil => exact absurd (UScalar.eq_of_val_eq (by rw [hlen, h]; rfl)) h0 + | cons y rest => exact ⟨y, rest, rfl⟩ + rw [show msg.syscall_arg st s (L.curW s) buf 0#u64 = ok y0 from hargs 0#u64 y0 rfl, bind_ok] at hd + by_cases hdom : y0 ≥ layout.NUM_DOMAINS + · rw [if_pos hdom] at hd + simp only [error.invalid_arg, bind_ok] at hd + have hv : y0.bv.toNat ≥ numDomains := by + rw [hnum]; show 1 ≤ y0.val; scalar_tac + refine wrap (.invalidArgument 0) (by rw [errOf hd]; rfl) ?_ + rw [domainSet_specA] + simp only [List.map_cons, if_pos hv] + rw [if_neg hdom] at hd + have hv : ¬ y0.bv.toNat ≥ numDomains := by + rw [hnum]; show ¬ 1 ≤ y0.val; scalar_tac + rw [exAtA_idx0, bind_ok] at hd + obtain ⟨hl3, hs, hz⟩ := hok + by_cases hx0 : exAtA e 0 = 0#u64 + · simp only [obj.Slot.is_null, hx0, decide_true, ↓reduceIte, bind_ok, error.truncated] at hd + have hex : excaps = [] := by + cases excaps with + | nil => rfl + | cons y rest' => exact absurd hx0 (srcOK_factsA J.j.1.place (hs 0 (by simp))).1 + refine wrap .truncatedMessage (by rw [errOf hd]; trivial) ?_ + rw [domainSet_specA] + subst hex + simp only [List.map_cons, if_neg hv] + obtain ⟨cp, sl0, erest, hexc, -, -, hrel0⟩ := exCap0A ⟨hl3, hs, hz⟩ (exAtA_idx0 e) hx0 + rw [capAtA_eq (st := st)] at hrel0 + replace hrel0 : CapRelA (capA st s (exAtA e 0)) cp := hrel0 + simp only [obj.Slot.is_null, hx0, decide_false, Bool.false_eq_true, ↓reduceIte, bind_ok, capA_eq] at hd + have bad : x = .Sys (.InvalidArgument 1#u64) → (∀ t, cp ≠ .threadCap t) → + ∃ (e2 : error.SysError) (e'' : SyscallError), x = .Sys e2 ∧ VsErrRelA x e'' ∧ + (.error e'', σ) ∈ ((fl4DecodeInvocation (Z := Unit) calling false mi.miLabel + ((y0 :: rest).map UScalar.bv) (Fl4SpecA64.toBl cptr.bv) sl c excaps buffer).run σ).1 := by + intro hx hn + refine wrap (.invalidArgument 1) (by rw [hx]; rfl) ?_ + rw [domainSet_specA] + subst hexc + simp only [List.map_cons, if_neg hv] + cases cp <;> first | rfl | exact absurd rfl (hn _) + generalize capA st s (exAtA e 0) = k at hd hrel0 + have other : (∀ t, k ≠ .Thread t) → ∀ t, cp ≠ .threadCap t := by + intro hk t h + rcases hrel0.2.eq_or_pt with h' | ⟨_, _, _, h'⟩ + · rw [h'] at h + cases k <;> first | exact hk _ rfl | simp [absCapA] at h + · rw [h'] at h; cases h + cases k + case Thread t => + obtain ⟨⟨s2, a2⟩, -, hd⟩ := bind_eq_ok hd + simp only [uncurry_apply_pair] at hd + obtain ⟨s3, -, hd⟩ := bind_eq_ok hd + obtain ⟨s4, -, hd⟩ := bind_eq_ok hd + exact absurd hd noOk + all_goals + simp only [error.invalid_arg, bind_ok] at hd + exact bad (errOf hd) (other (fun _ h => by cases h)) + rw [if_neg hS] at hd + by_cases hC : info.label = fl4_abi.label.DomainScheduleConfigure + · rw [if_pos hC] at hd + have dm : decodeDomainInvocation (Z := Unit) (specLabelWord (fl4LabelOf info.label.bv)) (argsU.map UScalar.bv) + excaps = decodeDomainScheduleConfigure (argsU.map UScalar.bv) := by + unfold decodeDomainInvocation; rw [gC.2 hC] + rw [dm] at wrap + by_cases h3 : msgLenA info buf < 3#u64 + · rw [if_pos h3] at hd + simp only [error.truncated, bind_ok] at hd + have : (argsU.map UScalar.bv).length < 2 + timeArgLen := by + simpa [timeArgLen] using len0 _ h3 + refine wrap .truncatedMessage (by rw [errOf hd]; trivial) ?_ + unfold decodeDomainScheduleConfigure + simp only [Fl4Spec.NonDetE.whenE] + rw [if_pos this] + simp only [Fl4Spec.NonDetE.throwError, ndE_throw_bindD]; rfl + rw [if_neg h3] at hd + have h3' : 3 ≤ argsU.length := lenGe _ h3 + obtain ⟨y0, y1, y2, rest, rfl⟩ : ∃ y0 y1 y2 rest, argsU = y0 :: y1 :: y2 :: rest := by + match argsU, h3' with + | y0 :: y1 :: y2 :: rest, _ => exact ⟨y0, y1, y2, rest, rfl⟩ + simp only [show msg.syscall_arg st s (L.curW s) buf 0#u64 = ok y0 from hargs 0#u64 y0 rfl, + show msg.syscall_arg st s (L.curW s) buf 1#u64 = ok y1 from hargs 1#u64 y1 rfl, + show msg.syscall_arg st s (L.curW s) buf 2#u64 = ok y2 from hargs 2#u64 y2 rfl, bind_ok] at hd + have hlen3 : ¬ (((y0 :: y1 :: y2 :: rest).map UScalar.bv).length < 2 + timeArgLen) := by + simp [timeArgLen] + -- the spec's checks, in order + have spec : ∀ (e' : SyscallError), + ((y0.val ≥ 1 ∧ e' = .rangeError 0 0) ∨ (¬ y0.val ≥ 1 ∧ + ((y1.val ≥ 1 ∧ e' = .rangeError 0 0) ∨ (¬ y1.val ≥ 1 ∧ + ((y2.bv > maxDomainDuration ∧ e' = .invalidArgument 2) ∨ (¬ y2.bv > maxDomainDuration ∧ + y0.val = σ.domainStartIndex ∧ y2.bv = 0 ∧ e' = .invalidArgument 2)))))) → + (decodeDomainScheduleConfigure (Z := Unit) ((y0 :: y1 :: y2 :: rest).map UScalar.bv)).run σ = + ({(.error e', σ)}, False) := by + intro e' h + unfold decodeDomainScheduleConfigure + simp only [Fl4Spec.NonDetE.whenE] + rw [if_neg hlen3] + have a0 : unat (Fl4SpecA64.the ((y0 :: y1 :: y2 :: rest).map UScalar.bv)[0]?) = y0.val := rfl + have a1 : Fl4SpecA64.the ((y0 :: y1 :: y2 :: rest).map UScalar.bv)[1]? = y1.bv := rfl + have a2 : parseTimeArg 2 ((y0 :: y1 :: y2 :: rest).map UScalar.bv) = y2.bv := rfl + have u1 : unat y1.bv = y1.val := rfl + have e1 : (BitVec.ofNat 64 (σ.domainList.length - 2)) = 0 := by rw [hdl2]; rfl + have e2 : (BitVec.ofNat 64 (numDomains - 1)) = 0 := by rw [hnum]; rfl + have g : ∀ {β : Type} (f : List (Domain × DomainDuration) → SEMonad Unit β), + ((Fl4Spec.NonDetE.liftE <| Fl4Spec.NonDet.gets fun s : Fl4SpecA64.State Unit => s.domainList) >>= f).run σ = + (f σ.domainList).run σ := fun f => run_bind_okD (run_liftEA + (show Fl4Spec.NonDet.gets (fun s : Fl4SpecA64.State Unit => s.domainList) σ = ({(σ.domainList, σ)}, False) + from rfl)) f + have g' : ∀ {β : Type} (f : Nat → SEMonad Unit β), + ((Fl4Spec.NonDetE.liftE <| Fl4Spec.NonDet.gets fun s : Fl4SpecA64.State Unit => s.domainStartIndex) >>= + f).run σ = (f σ.domainStartIndex).run σ := fun f => run_bind_okD (run_liftEA + (show Fl4Spec.NonDet.gets (fun s : Fl4SpecA64.State Unit => s.domainStartIndex) σ = + ({(σ.domainStartIndex, σ)}, False) from rfl)) f + simp only [Fl4Spec.NonDetE.returnOk, ndE_pure_bindD, a0, a1, a2, u1] + rw [g] + simp only [e1, e2, hdl2, hnum] + rcases h with ⟨h1, rfl⟩ | ⟨h1, ⟨h2, rfl⟩ | ⟨h2, ⟨h4, rfl⟩ | ⟨h4, h5, h6, rfl⟩⟩⟩ + · rw [if_pos (show y0.val ≥ 2 - 1 from h1)] + simp only [Fl4Spec.NonDetE.throwError, ndE_throw_bindD]; rfl + · rw [if_neg (show ¬ y0.val ≥ 2 - 1 from h1), ndE_pure_bindD, if_pos h2] + simp only [Fl4Spec.NonDetE.throwError, ndE_throw_bindD]; rfl + · rw [if_neg (show ¬ y0.val ≥ 2 - 1 from h1), ndE_pure_bindD, if_neg h2, ndE_pure_bindD, if_pos h4] + simp only [Fl4Spec.NonDetE.throwError, ndE_throw_bindD]; rfl + · have hz : ¬ (y2.bv = 0 ∧ y1.bv ≠ 0) := by + intro h + apply h.2 + apply BitVec.eq_of_toNat_eq + show y1.val = 0 + omega + rw [if_neg (show ¬ y0.val ≥ 2 - 1 from h1), ndE_pure_bindD, if_neg h2, ndE_pure_bindD, if_neg h4, + ndE_pure_bindD, if_neg hz, ndE_pure_bindD, g', if_pos ⟨h5, h6⟩] + simp only [Fl4Spec.NonDetE.throwError, ndE_throw_bindD]; rfl + have fin : ∀ (e' : SyscallError), SysErrRelA x e' → _ := fun e' hrel h => wrap e' hrel (spec e' h) + obtain ⟨i, hi, hd⟩ := bind_eq_ok hd + have iv : i.val = 1 := by + have := post_of_okA (P := fun v : U64 => v.val = layout.NUM_DOMAIN_SCHEDULES.val - 1) + (WP.spec_mono (U64.sub_spec (x := layout.NUM_DOMAIN_SCHEDULES) (y := 1#u64) (by scalar_tac)) + (fun v h => by scalar_tac)) hi + omega + by_cases hix : y0 ≥ i + · rw [if_pos hix] at hd + obtain ⟨i1, hi1, hd⟩ := bind_eq_ok hd + simp only [error.range, bind_ok] at hd + have i1v : i1.val = 0 := by + have := post_of_okA (P := fun v : U64 => v.val = layout.NUM_DOMAIN_SCHEDULES.val - 2) + (WP.spec_mono (U64.sub_spec (x := layout.NUM_DOMAIN_SCHEDULES) (y := 2#u64) (by scalar_tac)) + (fun v h => by scalar_tac)) hi1 + omega + have i1b : i1.bv = 0 := BitVec.eq_of_toNat_eq (by show i1.val = 0; exact i1v) + exact fin (.rangeError 0 0) (by rw [errOf hd]; exact ⟨rfl, i1b.symm⟩) (Or.inl ⟨by scalar_tac, rfl⟩) + rw [if_neg hix] at hd + have n0 : ¬ y0.val ≥ 1 := by scalar_tac + by_cases hdom : y1 ≥ layout.NUM_DOMAINS + · rw [if_pos hdom] at hd + obtain ⟨i1, hi1, hd⟩ := bind_eq_ok hd + simp only [error.range, bind_ok] at hd + have i1v : i1.val = 0 := by + have := post_of_okA (P := fun v : U64 => v.val = layout.NUM_DOMAINS.val - 1) + (WP.spec_mono (U64.sub_spec (x := layout.NUM_DOMAINS) (y := 1#u64) (by scalar_tac)) + (fun v h => by scalar_tac)) hi1 + omega + have i1b : i1.bv = 0 := BitVec.eq_of_toNat_eq (by show i1.val = 0; exact i1v) + exact fin (.rangeError 0 0) (by rw [errOf hd]; exact ⟨rfl, i1b.symm⟩) + (Or.inr ⟨n0, Or.inl ⟨by scalar_tac, rfl⟩⟩) + rw [if_neg hdom] at hd + have n1 : ¬ y1.val ≥ 1 := by scalar_tac + have y10 : (y1 != 0#u64) = false := by + have : y1 = 0#u64 := UScalar.eq_of_val_eq (by show y1.val = 0; omega) + simp [this] + obtain ⟨mx, hmx, hd⟩ := bind_eq_ok hd + have mxv : mx.val = 2 ^ 56 - 1 := + post_of_okA (P := fun v : U64 => v.val = 2 ^ 56 - 1) + (WP.spec_mono Fl4ProofsA64.P0.Consts.domain.MAX_DURATION.spec (fun v h => by subst h; rfl)) hmx + have mxs : maxDomainDuration.toNat = 2 ^ 56 - 1 := by decide + by_cases hdur : y2 > mx + · rw [if_pos hdur] at hd + simp only [error.invalid_arg, bind_ok] at hd + have : y2.bv > maxDomainDuration := by + apply BitVec.lt_def.2 + rw [mxs] + show 2 ^ 56 - 1 < y2.val + scalar_tac + exact fin (.invalidArgument 2) (by rw [errOf hd]; rfl) (Or.inr ⟨n0, Or.inr ⟨n1, Or.inl ⟨this, rfl⟩⟩⟩) + rw [if_neg hdur] at hd + have ndur : ¬ y2.bv > maxDomainDuration := by + intro h + have := BitVec.lt_def.1 h + rw [mxs] at this + change 2 ^ 56 - 1 < y2.val at this + apply hdur + show mx < y2 + rw [UScalar.lt_equiv]; omega + have start : ∀ i2, obj.ks_get st s layout.ks.DOM_SCHED_START = ok i2 → i2.val = σ.domainStartIndex := by + intro i2 h + rw [eq_of_spec_fd (obj.ks_get.spec (L := L) s _)] at h + rw [← okinj h]; exact hnd.start + have last : ∀ i2, obj.ks_get st s layout.ks.DOM_SCHED_START = ok i2 → y0 = i2 → y2 = 0#u64 → + x = .Sys (.InvalidArgument 2#u64) → + ∃ (e2 : error.SysError) (e'' : SyscallError), x = .Sys e2 ∧ VsErrRelA x e'' ∧ + (.error e'', σ) ∈ ((fl4DecodeInvocation (Z := Unit) calling false mi.miLabel + ((y0 :: y1 :: y2 :: rest).map UScalar.bv) (Fl4SpecA64.toBl cptr.bv) sl c excaps buffer).run σ).1 := by + intro i2 h2 heq h0 hx + exact fin (.invalidArgument 2) (by rw [hx]; rfl) (Or.inr ⟨n0, Or.inr ⟨n1, Or.inr + ⟨ndur, by rw [heq]; exact start i2 h2, by rw [h0]; rfl, rfl⟩⟩⟩) + have done : ∀ {X : Result S} {Y : S → Result S}, (do + let s1 ← X + let s2 ← Y s1 + ok (core.result.Result.Ok (), s2, a)) = ok (.Err x, s', a') → False := by + intro X Y h + obtain ⟨s1, -, h⟩ := bind_eq_ok h + obtain ⟨s2, -, h⟩ := bind_eq_ok h + exact noOk h + by_cases hz : y2 = 0#u64 + · rw [if_pos hz, y10] at hd + simp only [Bool.false_eq_true, ↓reduceIte] at hd + obtain ⟨i2, h2, hd⟩ := bind_eq_ok hd + by_cases heq : y0 = i2 + · rw [if_pos heq, if_pos hz] at hd + simp only [error.invalid_arg, bind_ok] at hd + exact last i2 h2 heq hz (errOf hd) + · rw [if_neg heq] at hd + exact (done hd).elim + · rw [if_neg hz] at hd + obtain ⟨i2, -, hd⟩ := bind_eq_ok hd + by_cases heq : y0 = i2 + · rw [if_pos heq, if_neg hz] at hd + exact (done hd).elim + · rw [if_neg heq] at hd + exact (done hd).elim + rw [if_neg hC] at hd + by_cases hT : info.label = fl4_abi.label.DomainScheduleSetStart + · rw [if_pos hT] at hd + have dm : decodeDomainInvocation (Z := Unit) (specLabelWord (fl4LabelOf info.label.bv)) (argsU.map UScalar.bv) + excaps = decodeDomainScheduleSetStart (argsU.map UScalar.bv) := by + unfold decodeDomainInvocation; rw [gT.2 hT] + rw [dm] at wrap + by_cases h1 : msgLenA info buf < 1#u64 + · rw [if_pos h1] at hd + simp only [error.truncated, bind_ok] at hd + have : (argsU.map UScalar.bv).length = 0 := by + have := len0 _ h1 + simp at this + simp [this] + refine wrap .truncatedMessage (by rw [errOf hd]; trivial) ?_ + unfold decodeDomainScheduleSetStart + simp only [Fl4Spec.NonDetE.whenE] + rw [if_pos this] + simp only [Fl4Spec.NonDetE.throwError, ndE_throw_bindD]; rfl + rw [if_neg h1] at hd + have h1' : 1 ≤ argsU.length := lenGe _ h1 + obtain ⟨y0, rest, rfl⟩ : ∃ y0 rest, argsU = y0 :: rest := by + match argsU, h1' with + | y0 :: rest, _ => exact ⟨y0, rest, rfl⟩ + rw [show msg.syscall_arg st s (L.curW s) buf 0#u64 = ok y0 from hargs 0#u64 y0 rfl, bind_ok] at hd + have spec : ∀ (e' : SyscallError), + ((y0.val ≥ 2 ∧ e' = .rangeError 0 1) ∨ (¬ y0.val ≥ 2 ∧ + Fl4SpecA64.the σ.domainList[y0.val]? = domainEndMarker ∧ e' = .invalidArgument 0)) → + (decodeDomainScheduleSetStart (Z := Unit) ((y0 :: rest).map UScalar.bv)).run σ = + ({(.error e', σ)}, False) := by + intro e' h + unfold decodeDomainScheduleSetStart + simp only [Fl4Spec.NonDetE.whenE] + rw [if_neg (show ¬ ((y0 :: rest).map UScalar.bv).length = 0 by simp), ndE_pure_bindD] + have a0 : unat (Fl4SpecA64.the ((y0 :: rest).map UScalar.bv)[0]?) = y0.val := rfl + have g : ∀ {β : Type} (f : List (Domain × DomainDuration) → SEMonad Unit β), + ((Fl4Spec.NonDetE.liftE <| Fl4Spec.NonDet.gets fun s : Fl4SpecA64.State Unit => s.domainList) >>= f).run σ = + (f σ.domainList).run σ := fun f => run_bind_okD (run_liftEA + (show Fl4Spec.NonDet.gets (fun s : Fl4SpecA64.State Unit => s.domainList) σ = ({(σ.domainList, σ)}, False) + from rfl)) f + have e1 : (BitVec.ofNat 64 (σ.domainList.length - 1)) = 1 := by rw [hdl2]; rfl + simp only [Fl4Spec.NonDetE.returnOk, ndE_pure_bindD, a0] + rw [g] + simp only [e1, hdl2] + rcases h with ⟨h2, rfl⟩ | ⟨h2, h3, rfl⟩ + · rw [if_pos h2] + simp only [Fl4Spec.NonDetE.throwError, ndE_throw_bindD]; rfl + · rw [if_neg h2, ndE_pure_bindD, if_pos h3] + simp only [Fl4Spec.NonDetE.throwError, ndE_throw_bindD]; rfl + by_cases hix : y0 ≥ layout.NUM_DOMAIN_SCHEDULES + · rw [if_pos hix] at hd + obtain ⟨i1, hi1, hd⟩ := bind_eq_ok hd + simp only [error.range, bind_ok] at hd + have i1v : i1.val = 1 := by + have := post_of_okA (P := fun v : U64 => v.val = layout.NUM_DOMAIN_SCHEDULES.val - 1) + (WP.spec_mono (U64.sub_spec (x := layout.NUM_DOMAIN_SCHEDULES) (y := 1#u64) (by scalar_tac)) + (fun v h => by scalar_tac)) hi1 + omega + have i1b : i1.bv = 1 := BitVec.eq_of_toNat_eq (by show i1.val = 1; exact i1v) + exact wrap (.rangeError 0 1) (by rw [errOf hd]; exact ⟨rfl, i1b.symm⟩) + (spec _ (Or.inl ⟨by scalar_tac, rfl⟩)) + rw [if_neg hix] at hd + obtain ⟨dur, hdur, hd⟩ := bind_eq_ok hd + have hlt : y0 < layout.NUM_DOMAIN_SCHEDULES := by scalar_tac + obtain ⟨p, d, w, hp, -, hw, hdurw, -, -⟩ := hnd.table y0 hlt + by_cases hz : dur = 0#u64 + · rw [if_pos hz] at hd + simp only [error.invalid_arg, bind_ok] at hd + have hw0 : w = 0#u64 := (okinj (hdurw.symm.trans hdur)).trans hz + have p2 : p.2 = 0 := by + rw [hw0] at hw + have hlt2 : p.2.toNat < 2 ^ 64 := + Nat.lt_of_lt_of_le p.2.isLt (Nat.pow_le_pow_right (by decide) (Nat.sub_le _ _)) + have h0 : (0#u64 : U64).bv.toNat = (BitVec.setWidth 64 p.2).toNat := congrArg BitVec.toNat hw + rw [BitVec.toNat_setWidth, Nat.mod_eq_of_lt hlt2] at h0 + apply BitVec.eq_of_toNat_eq + exact h0.symm + have p1 : p.1 = 0 := hnd.entry_domain_zero hp + have pm : p = domainEndMarker := Prod.ext p1 p2 + refine wrap (.invalidArgument 0) (by rw [errOf hd]; rfl) (spec _ (Or.inr ⟨by scalar_tac, ?_, rfl⟩)) + rw [hp, pm]; rfl + rw [if_neg hz] at hd + obtain ⟨s1, -, hd⟩ := bind_eq_ok hd + obtain ⟨s2, -, hd⟩ := bind_eq_ok hd + obtain ⟨s3, -, hd⟩ := bind_eq_ok hd + obtain ⟨i1, -, hd⟩ := bind_eq_ok hd + obtain ⟨s4, -, hd⟩ := bind_eq_ok hd + obtain ⟨s5, -, hd⟩ := bind_eq_ok hd + exact absurd hd noOk + rw [if_neg hT] at hd + simp only [error.illegal, bind_ok] at hd + refine wrap .illegalOperation (by rw [errOf hd]; trivial) ?_ + have n1 := mt gS.1 hS + have n2 := mt gC.1 hC + have n3 := mt gT.1 hT + unfold decodeDomainInvocation + revert n1 n2 n3 + cases genInvocationType (specLabelWord (fl4LabelOf info.label.bv)) <;> intro n1 n2 n3 <;> + first | rfl | exact absurd rfl n1 | exact absurd rfl n2 | exact absurd rfl n3 + +end + +end + +end Fl4ProofsA64.P3.ReplyErr diff --git a/proofs/Fl4ProofsA64/P3/Stage3DecErrIrqA.lean b/proofs/Fl4ProofsA64/P3/Stage3DecErrIrqA.lean new file mode 100644 index 000000000..b0ac6e3b6 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3DecErrIrqA.lean @@ -0,0 +1,823 @@ +/- +P3 (aarch64, A64-P3 Lift 1b, rest part A): **`DecErrSpecPA` for the IRQControl cap** (the three SPI / banked issue +labels, MSI and foreign labels). + +* `irqTailA`: the target part FL4's four issue paths share (`is_irq_active`, `lookup_target_slot`, + `ensure_empty_slot`, then the continuation); **`irqTail_errA`**: an `Err` of it is either the spec's + `fl4IrqTarget` error or the spec target succeeds and the error is the continuation's. +* `issueHead_errA`, `issueRange_errA`, `issueEnd_errA`, `issueSpec_errA`: the parts the three issue labels share. +* **`DecErrSpecPA.irqControl`**. + +Named premises: [P] `MaxSpi`, [P] `IrqPlatA`. New module. +-/ +import Fl4ProofsA64.P3.Stage3DecErrBaseA +import Fl4ProofsA64.P3.Stage3DecErrMiscA +import Fl4ProofsA64.P3.Stage3InvSpecL1CtlA +import Fl4ProofsA64.P3.VsTopPtA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.ReplyErr +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3 Fl4ProofsA64.P3.Endp + Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +/-- The conclusion of `DecErrSpecPA` for a spec decode `msp`. -/ +def DecErrConclA {α : Type} (σ : Fl4SpecA64.State Unit) (x : error.Exc) (msp : Fl4SpecA64.SEMonad Unit α) : Prop := + ∃ (e2 : error.SysError) (e' : Fl4SpecA64.SyscallError), x = .Sys e2 ∧ VsErrRelA x e' ∧ (.error e', σ) ∈ (msp.run σ).1 + +/-- The IRQControl caps. -/ +def IrqCtlCapA : capability.Cap → Prop + | .IrqControl => True + | _ => False + +set_option maxRecDepth 8000 in +theorem labNA_irqCtlA : + (∀ n, n < 74 → (labNA n = .spec (.genInvocationLabel .irqIssueIRQHandler) → n = 29)) ∧ + (∀ n, n < 74 → (labNA n = .spec (.archInvocationLabel .armIRQIssueIRQHandlerTrigger) → n = 65)) ∧ + (∀ n, n < 74 → (labNA n = .ext .armIRQIssueIRQHandlerTriggerCore → n = 66)) ∧ + (∀ n, n < 74 → (labNA n = .ext .armIRQIssueIRQHandlerMSI → n = 71)) := by + decide + +theorem labNA_irqCtlAtA : + labNA 29 = .spec (.genInvocationLabel .irqIssueIRQHandler) ∧ + labNA 65 = .spec (.archInvocationLabel .armIRQIssueIRQHandlerTrigger) ∧ + labNA 66 = .ext .armIRQIssueIRQHandlerTriggerCore ∧ + labNA 71 = .ext .armIRQIssueIRQHandlerMSI := by + decide + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +variable (st) in +/-- The target part of FL4's IRQ issue paths. -/ +def irqTailA (s : S) (a : A) (irq : U64) (root : capability.Cap) (index depth : U64) + (K : obj.Slot → Result (core.result.Result Unit error.Exc × S × A)) : + Result (core.result.Result Unit error.Exc × S × A) := do + let b ← interrupt.is_irq_active st s irq + if b then ok (core.result.Result.Err (error.Exc.Sys error.SysError.RevokeFirst), s, a) + else + let r ← cspace.lookup_target_slot st s root index depth + let cf ← core.result.Result.Insts.CoreOpsTry.branch r + match cf with + | core.ops.control_flow.ControlFlow.Continue val => + let r1 ← cdt.ensure_empty_slot st s val + let cf1 ← core.result.Result.Insts.CoreOpsTry.branch r1 + match cf1 with + | core.ops.control_flow.ControlFlow.Continue _ => K val + | core.ops.control_flow.ControlFlow.Break residual => + let r2 ← core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual + Unit (core.convert.FromSame error.Exc) residual + ok (r2, s, a) + | core.ops.control_flow.ControlFlow.Break residual => + let r1 ← core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual + Unit (core.convert.FromSame error.Exc) residual + ok (r1, s, a) + +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s : S} {a a' : A} {s' : S} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +set_option maxHeartbeats 4000000 in +/-- **An `Err` of the shared target part** (module docstring). -/ +theorem irqTail_errA (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + {extra : Aeneas.Std.Array obj.Slot 3#usize} {excaps : List (Cap × CslotPtr)} + (hx : ExtraOKA Z.th (L.mem s) extra excaps) (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) + {root : capability.Cap} (he : interrupt.extra0 st s extra = ok (some root)) + {irq index depth : U64} (hirq : irq.val < 5376) + {K : obj.Slot → Result (core.result.Result Unit error.Exc × S × A)} {x : error.Exc} + (hd : irqTailA st s a irq root index depth K = ok (.Err x, s', a')) : + ∃ cp sl0 erest, excaps = (cp, sl0) :: erest ∧ + ((∃ e', (fl4IrqTarget (Z := Unit) (BitVec.ofNat 13 irq.val) cp index.bv depth.bv).run σ = + ({(.error e', σ)}, False) ∧ SysErrRelA x e') ∨ + (∃ sd dest, (fl4IrqTarget (Z := Unit) (BitVec.ofNat 13 irq.val) cp index.bv depth.bv).run σ = + ({(.ok sd, σ)}, False) ∧ K dest = ok (.Err x, s', a'))) := by + obtain ⟨hn0, hroot⟩ := extra0_exA he + obtain ⟨cp, sl0, erest, hexc, hsl0, hab0, hrel0⟩ := exCap0A hx (exAtA_idx0 extra) hn0 + refine ⟨cp, sl0, erest, hexc, ?_⟩ + have hrelR : CapRelA root cp := by rw [hroot]; rw [capAtA_eq] at hrel0; exact hrel0 + have hv : CapValidA Z.th root := by + have h := J.j.1.cv (exAtA extra 0) (by rw [← hsl0]; exact hab0.isSlot J.j.1.place) + rw [capAtA_eq] at h + rw [hroot]; exact h + have errOf : ∀ {y : error.Exc}, + (ok (core.result.Result.Err y, s, a) : Result (core.result.Result Unit error.Exc × S × A)) = + ok (.Err x, s', a') → x = y := by + intro y h + have := (Prod.mk.inj (okinj h)).1 + injection this with h' + exact h'.symm + unfold irqTailA at hd + obtain ⟨b, hb, hd⟩ := bind_eq_ok hd + have hact : isIrqActive (Z := Unit) (BitVec.ofNat 13 irq.val) σ = ({(b, σ)}, False) := + (post_of_okA (irqActive_refinesA (st := st) (s := s) (irq := irq) J.j.1.irq hirq) hb :) + cases b + case true => + simp only [↓reduceIte] at hd + refine Or.inl ⟨.revokeFirst, ?_, by rw [errOf hd]; trivial⟩ + unfold fl4IrqTarget + rw [run_bind_okD (run_liftEA hact)] + simp only [Fl4Spec.NonDetE.whenE, ↓reduceIte, Fl4Spec.NonDetE.throwError, ndE_throw_bindD] + rfl + simp only [Bool.false_eq_true, ↓reduceIte] at hd + obtain ⟨r, hr, hd⟩ := bind_eq_ok hd + have hr' := hr + unfold cspace.lookup_target_slot at hr' + have hl := lookup_slot_for_cnode_op_refinesA (L := L) Z.th s σ J.j.1.st J.j.1.cv false root cp hrelR hv index depth + obtain ⟨e1, he1, hrel⟩ := post_of_okA hl hr' + have hlt : (lookupTargetSlot (Z := Unit) cp (dataToCptr index.bv) (unat depth.bv)).run σ = ({(e1, σ)}, False) := he1 + have pre : ∀ {β : Type} (f : CslotPtr → SEMonad Unit β), + (do + let irqActive ← Fl4Spec.NonDetE.liftE <| isIrqActive (Z := Unit) (BitVec.ofNat 13 irq.val) + Fl4Spec.NonDetE.whenE irqActive <| Fl4Spec.NonDetE.throwError SyscallError.revokeFirst + let destSlot ← lookupTargetSlot cp (dataToCptr index.bv) (unat depth.bv) + f destSlot).run σ = ((lookupTargetSlot (Z := Unit) cp (dataToCptr index.bv) (unat depth.bv)) >>= f).run σ := by + intro β f + rw [run_bind_okD (run_liftEA hact)] + simp only [Fl4Spec.NonDetE.whenE, Bool.false_eq_true, ↓reduceIte, ndE_pure_bindD] + cases r with + | Err y => + cases e1 with + | ok _ => exact hrel.elim + | error e' => + simp only [core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + bind_ok, Aeneas.Std.bind_tc_ok] at hd + try simp only [bind_ok] at hd + refine Or.inl ⟨e', ?_, by rw [errOf hd]; exact hrel⟩ + unfold fl4IrqTarget + rw [pre] + exact run_bind_errorD hlt _ + | Ok dest => + cases e1 with + | error _ => exact hrel.elim + | ok dsl => + obtain ⟨hdst, pd, hpd1, hpd, -⟩ := hrel + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok] at hd + obtain ⟨r1, hr1, hd⟩ := bind_eq_ok hd + obtain ⟨ad, hcad, hgd⟩ := J.j.1.st.slot_capA hpd1 hpd + replace hcad : CapRelA (capA st s (cslotAddrA dsl)) ad := hcad + have hee := ensureEmpty_runA dsl σ hgd + have hr1' := hr1 + unfold cdt.ensure_empty_slot at hr1' + rw [capA_eq, bind_ok] at hr1' + rw [← hdst] at hcad + obtain ⟨b, hbn, h⟩ := bind_eq_ok hr1' + have hnull : ad = .nullCap ↔ b = true := by + obtain ⟨wf, hc⟩ := hcad + generalize capA st s dest = c at hbn wf hc + unfold capability.Cap.is_null at hbn + cases c + case Null => + have : ad = .nullCap := hc.symm + have e : true = b := okinj (show ok true = ok b from hbn) + exact ⟨fun _ => e.symm, fun _ => this⟩ + all_goals + have e : false = b := okinj (show ok false = ok b from hbn) + rw [← e] + refine ⟨fun hn => ?_, fun hf => (Bool.false_ne_true hf).elim⟩ + subst hn + first | exact wf.elim | (simp [CapEqA, absCapA] at hc) + cases b + · simp only [Bool.false_eq_true, ↓reduceIte] at h + have er1 := (okinj h).symm + subst er1 + simp only [core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, bind_ok, Aeneas.Std.bind_tc_ok] at hd + try simp only [bind_ok] at hd + rw [if_neg (fun hn => Bool.false_ne_true (hnull.1 hn))] at hee + refine Or.inl ⟨.deleteFirst, ?_, by rw [errOf hd]; trivial⟩ + unfold fl4IrqTarget + rw [pre, run_bind_okD hlt] + exact run_bind_errorD hee _ + · simp only [↓reduceIte] at h + have er1 := (okinj h).symm + subst er1 + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok] at hd + rw [if_pos (hnull.2 rfl)] at hee + refine Or.inr ⟨dsl, dest, ?_, hd⟩ + unfold fl4IrqTarget + rw [pre, run_bind_okD hlt, run_bind_okD hee] + rfl + +/-- The checks of the spec issue decode that fail, in order. -/ +def IssueFailA (σ : State Unit) (kind : Nat) (irqW cpu index depth : Data) (cp : Cap) (e' : SyscallError) : Prop := + (irqW > fl4A64Platform.maxSpi ∧ e' = .rangeError 0 fl4A64Platform.maxSpi) ∨ + (¬ irqW > fl4A64Platform.maxSpi ∧ + ((kind = 2 ∧ cpu.toNat ≥ fl4A64Platform.numNodes ∧ e' = .invalidArgument 4) ∨ + (¬ (kind = 2 ∧ cpu.toNat ≥ fl4A64Platform.numNodes) ∧ + ((kind = 1 ∧ irqW < 32 ∧ e' = .illegalOperation) ∨ + (¬ (kind = 1 ∧ irqW < 32) ∧ + (fl4IrqTarget (Z := Unit) (ucast irqLen (fl4IrqOf irqW cpu)) cp index depth).run σ = + ({(.error e', σ)}, False)))))) + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s : S} {a a' : A} {s' : S} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + {x : error.Exc} + +/-- **The shared head of an issue path**: the extra cap and the length check. -/ +theorem issueHead_errA {extra : Aeneas.Std.Array obj.Slot 3#usize} {excaps : List (Cap × CslotPtr)} + (hok : ExtraOKA Z.th (L.mem s) extra excaps) (hPl : PlaceA Z.th (L.ksb s)) {len minLen : U64} + {Q : capability.Cap → Result (core.result.Result Unit error.Exc × S × A)} + (hd : (do + let o ← interrupt.extra0 st s extra + match o with + | none => do + let r ← error.truncated Unit + ok (r, s, a) + | some c => + if len ≥ minLen then Q c + else do + let r ← error.truncated Unit + ok (r, s, a)) = ok (.Err x, s', a')) : + (x = .Sys .TruncatedMessage ∧ ¬ (minLen.val ≤ len.val ∧ 1 ≤ excaps.length)) ∨ + ∃ root cp sl0 erest, interrupt.extra0 st s extra = ok (some root) ∧ minLen.val ≤ len.val ∧ + excaps = (cp, sl0) :: erest ∧ Q root = ok (.Err x, s', a') := by + have tr : ∀ {s1 : S} {a1 : A}, (do + let r ← error.truncated Unit + ok (r, s1, a1)) = (ok (.Err x, s', a') : Result _) → x = .Sys .TruncatedMessage := by + intro s1 a1 h + simp only [error.truncated, bind_ok] at h + have := (Prod.mk.inj (okinj h)).1 + injection this with h' + exact h'.symm + obtain ⟨o, ho, hd⟩ := bind_eq_ok hd + cases o with + | none => + refine Or.inl ⟨tr hd, fun h => ?_⟩ + obtain ⟨-, hs, -⟩ := hok + cases excaps with + | nil => exact absurd h.2 (by simp) + | cons y rest => + obtain ⟨hne0, -⟩ := srcOK_factsA hPl (hs 0 (by simp)) + unfold interrupt.extra0 at ho + rw [exAtA_idx0, bind_ok] at ho + simp only [obj.Slot.is_null, hne0, decide_false, Bool.false_eq_true, ↓reduceIte, bind_ok, capA_eq] at ho + cases okinj ho + | some root => + dsimp only at hd + by_cases hl : len ≥ minLen + · rw [if_pos hl] at hd + obtain ⟨cp, sl0, erest, hexc, -⟩ := exCap0A hok (exAtA_idx0 extra) (extra0_exA ho).1 + exact Or.inr ⟨root, cp, sl0, erest, ho, by scalar_tac, hexc, hd⟩ + · rw [if_neg hl] at hd + exact Or.inl ⟨tr hd, fun h => hl (by have := h.1; scalar_tac)⟩ + +/-- **The shared range check** of an issue path. -/ +theorem issueRange_errA {ar : machine.Arch A} (hM : MaxSpi ar) (hP : IrqPlatA st ar) {irqW : U64} + {R : Result (core.result.Result Unit error.Exc × S × A)} + (hd : (do + let max ← ar.max_spi a + if irqW > max then do + let r ← error.range Unit 0#u64 max + ok (r, s, a) + else R) = ok (.Err x, s', a')) : + (irqW.bv > fl4A64Platform.maxSpi ∧ SysErrRelA x (.rangeError 0 fl4A64Platform.maxSpi)) ∨ + (¬ irqW.bv > fl4A64Platform.maxSpi ∧ irqW.val ≤ 1019 ∧ + (∀ cpu irq, layout.irq.of irqW cpu = ok irq → irq.val < 5376) ∧ R = ok (.Err x, s', a')) := by + obtain ⟨max, hm, hd⟩ := bind_eq_ok hd + have hmx : max.bv = fl4A64Platform.maxSpi := + post_of_okA (P := fun m : U64 => m.bv = fl4A64Platform.maxSpi) (hP.maxSpi a) hm + by_cases hi : irqW > max + · rw [if_pos hi] at hd + simp only [error.range, bind_ok] at hd + have hx := (Prod.mk.inj (okinj hd)).1 + injection hx with hx + refine Or.inl ⟨?_, by rw [← hx]; exact ⟨rfl, hmx.symm⟩⟩ + rw [← hmx] + show max.bv < irqW.bv + have : max.val < irqW.val := by scalar_tac + exact BitVec.lt_def.2 this + · rw [if_neg hi] at hd + exact Or.inr ⟨not_gt_bvA hmx hi, + le1019_of hi (post_of_okA (P := fun m : U64 => m.val ≤ 1019) (hM a) hm), + fun _ _ h => irqOf_ltA hM hm hi h, hd⟩ + +/-- **The shared end of an issue path**: the IRQ number and the target part, whose continuation never errs. -/ +theorem issueEnd_errA (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + {extra : Aeneas.Std.Array obj.Slot 3#usize} {cp : Cap} {sl0 : CslotPtr} {erest : List (Cap × CslotPtr)} + (hok : ExtraOKA Z.th (L.mem s) extra ((cp, sl0) :: erest)) (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) + {root : capability.Cap} (ho : interrupt.extra0 st s extra = ok (some root)) + {irqW cpu index depth : U64} (hle : irqW.val ≤ 1019) + (hlt : ∀ cpu irq, layout.irq.of irqW cpu = ok irq → irq.val < 5376) + {K : U64 → obj.Slot → Result (core.result.Result Unit error.Exc × S × A)} + (hd : (do + let irq ← layout.irq.of irqW cpu + irqTailA st s a irq root index depth (K irq)) = ok (.Err x, s', a')) + (hK : ∀ irq dest, K irq dest ≠ ok (.Err x, s', a')) : + ∃ e', SysErrRelA x e' ∧ + (fl4IrqTarget (Z := Unit) (ucast irqLen (fl4IrqOf irqW.bv cpu.bv)) cp index.bv depth.bv).run σ = + ({(.error e', σ)}, False) := by + obtain ⟨irq, hnum, hd⟩ := bind_eq_ok hd + have hu := ucast_irqA (post_of_okA (P := fun i : U64 => i.bv = fl4IrqOf irqW.bv cpu.bv) + (layout.irq.of.bvA cpu hle) hnum) + obtain ⟨cp', sl1, er1, hexc, h⟩ := irqTail_errA J hok hext ho (hlt _ _ hnum) hd + have ecp : cp' = cp := by injection hexc with h1 _; injection h1 with h1 _; exact h1.symm + subst ecp + rcases h with ⟨e', hrun, hrel⟩ | ⟨sd, dest, -, h⟩ + · exact ⟨e', hrel, by rw [hu]; exact hrun⟩ + · exact absurd h (hK _ _) + +end + +/-- The spec issue decode's `Err` cases (the argument values named). -/ +theorem issueSpec_errA {σ : State Unit} (kind : Nat) (args : List Data) (src : CslotPtr) (cps : List Cap) + {cnode : Cap} {irqW index depth cpu : Data} {e' : SyscallError} + (hlen : args.length ≥ (if kind = 0 then 3 else if kind = 2 then 5 else 4) ∧ cps.length ≥ 1) + (hcn : cps[0]? = some cnode) (hw : args[0]? = some irqW) + (hindex : (if kind = 0 then args[1]? else args[2]?) = some index) + (hdepth : the (if kind = 0 then args[2]? else if kind = 2 then some (Fl4SpecA64.the args[3]? &&& 0xff) else args[3]?) = depth) + (hcpu : (if kind = 2 then Fl4SpecA64.the args[4]? else 0) = cpu) + (h : IssueFailA σ kind irqW cpu index depth cnode e') : + (decodeFl4IrqIssue (Z := Unit) kind args src cps).run σ = ({(.error e', σ)}, False) := by + have hdepth' : (if kind = 0 then Fl4SpecA64.the args[2]? else if kind = 2 then Fl4SpecA64.the args[3]? &&& 0xff else Fl4SpecA64.the args[3]?) = + depth := by + rw [← hdepth] + split + · rfl + split <;> rfl + have hindex' : (if kind = 0 then Fl4SpecA64.the args[1]? else Fl4SpecA64.the args[2]?) = index := by + split at hindex <;> rename_i hk <;> simp [hk, hindex, the] + have e0 : Fl4SpecA64.the args[0]? = irqW := by rw [hw]; rfl + have ec : Fl4SpecA64.the cps[0]? = cnode := by rw [hcn]; rfl + unfold decodeFl4IrqIssue + dsimp only + rw [if_pos hlen] + simp only [Fl4Spec.NonDetE.whenE] + simp only [e0, ec, hdepth', hindex', hcpu] + unfold IssueFailA at h + rcases h with ⟨h1, rfl⟩ | ⟨h1, ⟨h2, h3, rfl⟩ | ⟨h2, ⟨h3, h4, rfl⟩ | ⟨h3, h4⟩⟩⟩ + · simp only [if_pos h1, Fl4Spec.NonDetE.throwError, ndE_throw_bindD]; rfl + · simp only [if_neg h1, if_pos (And.intro h2 h3), ndE_pure_bindD, + Fl4Spec.NonDetE.throwError, ndE_throw_bindD]; rfl + · simp only [if_neg h1, if_neg h2, if_pos (And.intro h3 h4), ndE_pure_bindD, + Fl4Spec.NonDetE.throwError, ndE_throw_bindD]; rfl + · simp only [if_neg h1, if_neg h2, if_neg h3, ndE_pure_bindD] + exact run_bind_errorD h4 _ + +set_option maxHeartbeats 8000000 in +/-- **`DecErrSpecPA` for the IRQControl cap** (module docstring). -/ +theorem DecErrSpecPA.irqControl {ar : machine.Arch A} (hM : MaxSpi ar) (hP : IrqPlatA st ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + {calling blocking donate : Bool} {cptr : U64} : + DecErrSpecPA st ar Z.th s a σ calling blocking donate cptr IrqCtlCapA := by + intro info buf r1 e x s' a' mi sl c excaps buffer args einfo ebuf elr eex hd hne hPc hlab hsl hcr hok hab hbv + gmi ggc glb gex gmrs + obtain ⟨-, argsU, hargsE, hlen, hargs⟩ := J.argsLabA einfo hbv gmi gmrs + subst hargsE + obtain ⟨_, _, _, _, _, _, Q⟩ := J.siteA einfo ebuf elr eex + have hext : ∀ x ∈ e.val, x ≠ 0#u64 → x ∈ OS := Q.hext + have hcap : capA st s r1.slot = .IrqControl := by + revert hPc + cases capA st s r1.slot <;> intro hPc <;> first | rfl | exact hPc.elim + rw [hcap] at hd hcr + have hc2 : c = .irqControlCap := hcr.2.symm + have run_dec : fl4DecodeInvocation (Z := Unit) calling false mi.miLabel (argsU.map UScalar.bv) + (Fl4SpecA64.toBl cptr.bv) sl c excaps buffer = + Fl4Spec.NonDetE.liftME Fl4Invocation.invokeIRQControl + (decodeFl4IrqControl (fl4LabelOf info.label.bv) (argsU.map UScalar.bv) sl (excaps.map Prod.fst)) := by + rw [hc2, hlab]; rfl + have wrap : ∀ (e' : SyscallError), SysErrRelA x e' → + (decodeFl4IrqControl (Z := Unit) (fl4LabelOf info.label.bv) (argsU.map UScalar.bv) sl + (excaps.map Prod.fst)).run σ = ({(.error e', σ)}, False) → + ∃ (e2 : error.SysError) (e'' : SyscallError), x = .Sys e2 ∧ VsErrRelA x e'' ∧ + (.error e'', σ) ∈ ((fl4DecodeInvocation (Z := Unit) calling false mi.miLabel (argsU.map UScalar.bv) + (Fl4SpecA64.toBl cptr.bv) sl c excaps buffer).run σ).1 := by + intro e' hrel hm + rw [run_dec] + exact decErr_ofA (Or.inl hrel) (run_liftME_errA hm) + have errOf : ∀ {y : error.Exc} {s1 : S} {a1 : A}, + (ok (core.result.Result.Err y, s1, a1) : Result (core.result.Result Unit error.Exc × S × A)) = + ok (.Err x, s', a') → x = y := by + intro y s1 a1 h + have := (Prod.mk.inj (okinj h)).1 + injection this with h' + exact h'.symm + have noOk : ∀ {s1 : S} {a1 : A}, + (ok (core.result.Result.Ok (), s1, a1) : Result (core.result.Result Unit error.Exc × S × A)) ≠ + ok (.Err x, s', a') := by + intro s1 a1 h + cases (Prod.mk.inj (okinj h)).1 + have lenN : (argsU.map UScalar.bv).length = (msgLenA info buf).val := by simp [hlen] + have argAt : ∀ (i : U64) (v : U64), i.val < argsU.length → msg.syscall_arg st s (L.curW s) buf i = ok v → + (argsU.map UScalar.bv)[i.val]? = some v.bv := by + intro i v hi h + have hg : argsU[i.val]? = some argsU[i.val] := List.getElem?_eq_getElem hi + have := okinj ((hargs i _ hg).symm.trans h) + rw [List.getElem?_map, hg, this]; rfl + have noEx : interrupt.extra0 st s e = ok none → excaps.map Prod.fst = [] := by + intro h + obtain ⟨-, hs, hz⟩ := hok + cases excaps with + | nil => rfl + | cons y rest => + obtain ⟨hne0, -⟩ := srcOK_factsA J.j.1.place (hs 0 (by simp)) + unfold interrupt.extra0 at h + rw [exAtA_idx0, bind_ok] at h + simp only [obj.Slot.is_null, hne0, decide_false, Bool.false_eq_true, ↓reduceIte, bind_ok, capA_eq] at h + cases okinj h + unfold syscall.decode_invocation at hd + simp only [cur_thread_ok, bind_ok] at hd + unfold interrupt.decode_irq_control at hd + by_cases hMsi : info.label = fl4_abi.label.ARMIRQIssueIRQHandlerMSI + · rw [if_pos hMsi] at hd + have hl : fl4LabelOf info.label.bv = .ext .armIRQIssueIRQHandlerMSI := by + rw [fl4LabelOf_bvA, hMsi, Fl4ProofsA64.P0.Consts.fl4_abi.label.ARMIRQIssueIRQHandlerMSI.val_eq]; exact labNA_irqCtlAtA.2.2.2 + have dm : decodeFl4IrqControl (Z := Unit) (fl4LabelOf info.label.bv) (argsU.map UScalar.bv) sl + (excaps.map Prod.fst) = decodeFl4IrqMsi (argsU.map UScalar.bv) sl (excaps.map Prod.fst) := by + rw [hl]; rfl + rw [dm] at wrap + unfold interrupt.decode_irq_control_msi at hd + simp only [cur_thread_ok, bind_ok] at hd + obtain ⟨o, ho, hd⟩ := bind_eq_ok hd + have trunc : x = .Sys .TruncatedMessage → + ¬ ((argsU.map UScalar.bv).length ≥ 6 ∧ (excaps.map Prod.fst).length ≥ 1) → + ∃ (e2 : error.SysError) (e'' : SyscallError), x = .Sys e2 ∧ VsErrRelA x e'' ∧ + (.error e'', σ) ∈ ((fl4DecodeInvocation (Z := Unit) calling false mi.miLabel (argsU.map UScalar.bv) + (Fl4SpecA64.toBl cptr.bv) sl c excaps buffer).run σ).1 := by + intro hx hn + refine wrap .truncatedMessage (by rw [hx]; trivial) ?_ + unfold decodeFl4IrqMsi + rw [if_neg hn]; rfl + cases o with + | none => + simp only [error.truncated, bind_ok] at hd + exact trunc (errOf hd) (by rw [noEx ho]; simp) + | some root => + dsimp only at hd + by_cases hlen6 : msgLenA info buf ≥ 6#u64 + swap + · rw [if_neg hlen6] at hd + simp only [error.truncated, bind_ok] at hd + refine trunc (errOf hd) (fun h => hlen6 ?_) + have := h.1 + rw [lenN] at this + show (6#u64 : U64) ≤ _ + rw [UScalar.le_equiv]; simpa using this + rw [if_pos hlen6] at hd + have h6 : 6 ≤ argsU.length := by + have : (6#u64 : U64).val ≤ (msgLenA info buf).val := by scalar_tac + rw [← hlen]; simpa using this + obtain ⟨y0, y1, y2, y3, y4, y5, rest, rfl⟩ : ∃ y0 y1 y2 y3 y4 y5 rest, + argsU = y0 :: y1 :: y2 :: y3 :: y4 :: y5 :: rest := by + match argsU, h6 with + | y0 :: y1 :: y2 :: y3 :: y4 :: y5 :: rest, _ => exact ⟨y0, y1, y2, y3, y4, y5, rest, rfl⟩ + rw [show msg.syscall_arg st s (L.curW s) buf 0#u64 = ok y0 from hargs 0#u64 y0 rfl, + show msg.syscall_arg st s (L.curW s) buf 1#u64 = ok y1 from hargs 1#u64 y1 rfl, + show msg.syscall_arg st s (L.curW s) buf 2#u64 = ok y2 from hargs 2#u64 y2 rfl, + show msg.syscall_arg st s (L.curW s) buf 3#u64 = ok y3 from hargs 3#u64 y3 rfl, + show msg.syscall_arg st s (L.curW s) buf 4#u64 = ok y4 from hargs 4#u64 y4 rfl, + show msg.syscall_arg st s (L.curW s) buf 5#u64 = ok y5 from hargs 5#u64 y5 rfl] at hd + simp only [bind_ok, lift] at hd + obtain ⟨cp0, sl0, erest, hexc, -⟩ := exCap0A hok (exAtA_idx0 e) (extra0_exA ho).1 + have hcond : ((y0 :: y1 :: y2 :: y3 :: y4 :: y5 :: rest).map UScalar.bv).length ≥ 6 ∧ + (excaps.map Prod.fst).length ≥ 1 := ⟨by simp, by rw [hexc]; simp⟩ + have body : ∀ (e' : SyscallError), SysErrRelA x e' → + (do + Fl4Spec.NonDetE.whenE (y0.bv ≥ fl4NumLpis) <| + Fl4Spec.NonDetE.throwError (.rangeError 0 (fl4NumLpis - 1)) + Fl4Spec.NonDetE.whenE (y5.bv.toNat ≥ fl4A64Platform.numNodes) <| + Fl4Spec.NonDetE.throwError (.invalidArgument 5) + let dest ← fl4IrqTarget (Z := Unit) (ucast irqLen (fl4LpiBase + y0.bv)) cp0 y3.bv (y4.bv &&& 0xff) + let answer ← Fl4Spec.NonDetE.liftE <| Fl4Spec.NonDet.select (Set.univ : Set Fl4MsiAnswer) + match answer with + | .ok => Fl4Spec.NonDetE.returnOk (Fl4IrqControlInvocation.msi (ucast irqLen (fl4LpiBase + y0.bv)) dest sl + y1.bv y2.bv y5.bv) + | .badDevice => Fl4Spec.NonDetE.throwError (.invalidArgument 1) + | .badEvent => Fl4Spec.NonDetE.throwError (.invalidArgument 2) + | .refused => Fl4Spec.NonDetE.throwError .illegalOperation : SEMonad Unit _).run σ = + ({(.error e', σ)}, False) → + ∃ (e2 : error.SysError) (e'' : SyscallError), x = .Sys e2 ∧ VsErrRelA x e'' ∧ + (.error e'', σ) ∈ ((fl4DecodeInvocation (Z := Unit) calling false mi.miLabel + ((y0 :: y1 :: y2 :: y3 :: y4 :: y5 :: rest).map UScalar.bv) + (Fl4SpecA64.toBl cptr.bv) sl c excaps buffer).run σ).1 := by + intro e' hrel hm + refine wrap e' hrel ?_ + unfold decodeFl4IrqMsi + rw [if_pos hcond] + simp only [hexc, List.map_cons, the, List.getElem?_cons_zero, List.getElem?_cons_succ, Option.getD_some] + exact hm + by_cases hn : y0 ≥ layout.irq.NUM_LPIS + · rw [if_pos hn] at hd + obtain ⟨i1, hi1, hd⟩ := bind_eq_ok hd + simp only [error.range, bind_ok] at hd + have e1 : fl4NumLpis - 1 = i1.bv := by + have : layout.irq.NUM_LPIS - 1#u64 = ok 255#u64 := by + simp only [layout.irq.NUM_LPIS]; rfl + rw [okinj (hi1.symm.trans this)]; decide + have hn' : y0.bv ≥ fl4NumLpis := by + have : (256#u64 : U64).val ≤ y0.val := by + simp only [layout.irq.NUM_LPIS] at hn; scalar_tac + show fl4NumLpis ≤ y0.bv + exact BitVec.le_def.2 this + refine body (.rangeError 0 (fl4NumLpis - 1)) (by rw [errOf hd]; exact ⟨rfl, e1⟩) ?_ + simp only [Fl4Spec.NonDetE.whenE, if_pos hn', Fl4Spec.NonDetE.throwError, ndE_throw_bindD] + rfl + rw [if_neg hn] at hd + have hn' : ¬ y0.bv ≥ fl4NumLpis := by + intro h + apply hn + have := BitVec.le_def.1 h + show layout.irq.NUM_LPIS ≤ y0 + rw [UScalar.le_equiv]; simp only [layout.irq.NUM_LPIS]; exact this + obtain ⟨nodes, hks, hd⟩ := bind_eq_ok hd + have hnodes := J.toJStage3A.domOK.nodesGet hks + by_cases hcpu : y5 ≥ nodes + · rw [if_pos hcpu] at hd + have hc' : y5.bv.toNat ≥ fl4A64Platform.numNodes := by + rw [← hnodes]; show nodes.val ≤ y5.val; scalar_tac + refine body (.invalidArgument 5) (by rw [errOf hd]; rfl) ?_ + simp only [Fl4Spec.NonDetE.whenE, if_neg hn', if_pos hc', ndE_pure_bindD, Fl4Spec.NonDetE.throwError, + ndE_throw_bindD] + rfl + rw [if_neg hcpu] at hd + have hc' : ¬ y5.bv.toNat ≥ fl4A64Platform.numNodes := by + rw [← hnodes]; intro h; apply hcpu; show nodes ≤ y5 + rw [UScalar.le_equiv]; exact h + obtain ⟨base, hbase, hd⟩ := bind_eq_ok hd + obtain ⟨irq, hirq, hd⟩ := bind_eq_ok hd + have hlt := irqOfLpi_ltA hn hbase hirq + have hbv : irq.bv = fl4LpiBase + y0.bv := by + have eb : base = 5120#u64 := + post_of_okA (P := fun v : U64 => v = 5120#u64) Fl4ProofsA64.P0.Consts.layout.irq.LPI_BASE.spec hbase + have hn256 : y0.val < 256 := by + simp only [layout.irq.NUM_LPIS, ge_iff_le, not_le] at hn; scalar_tac + subst eb + have hv := post_of_okA (P := fun v : U64 => v.val = (5120#u64 : U64).val + y0.val) + (U64.add_spec (x := 5120#u64) (y := y0) (by scalar_tac)) hirq + apply BitVec.eq_of_toNat_eq + have h5 : fl4LpiBase.toNat = 5120 := by decide + rw [BitVec.toNat_add, h5] + change irq.val = (5120 + y0.val) % 2 ^ 64 + rw [hv, Nat.mod_eq_of_lt (by omega)] + rfl + have hu := ucast_irqA hbv + have hdep : y4.bv &&& 0xff = (y4 &&& 255#u64).bv := rfl + rcases irqTail_errA (K := fun val => do + let (i3, a1) ← ar.msi_map a irq y1 y2 y5 + match i3 with + | 0#uscalar => + let s1 ← sched.set_thread_state st s (L.curW s) sched.TS_RESTART + let (s2, a2) ← interrupt.invoke_irq_control st ar s1 a1 irq val _ + ok (core.result.Result.Ok (), s2, a2) + | 2#uscalar => + ok (core.result.Result.Err (error.Exc.Sys (error.SysError.InvalidArgument 1#u64)), s, a1) + | 3#uscalar => + ok (core.result.Result.Err (error.Exc.Sys (error.SysError.InvalidArgument 2#u64)), s, a1) + | _ => + let r2 ← error.illegal Unit + ok (r2, s, a1)) J hok hext ho hlt hd with ⟨cp, sl1, er1, hexc', h⟩ + have ecp : cp = cp0 := by + rw [hexc] at hexc'; injection hexc' with h1 _; injection h1 with h1 _; exact h1.symm + subst ecp + rcases h with ⟨e', hrun, hrel⟩ | ⟨sd, dest, hrun, hK⟩ + · refine body e' hrel ?_ + simp only [Fl4Spec.NonDetE.whenE, if_neg hn', if_neg hc', ndE_pure_bindD] + rw [hu, hdep] + exact run_bind_errorD hrun _ + obtain ⟨⟨code, a1⟩, -, hK⟩ := bind_eq_ok hK + simp only [uncurry_apply_pair] at hK + have pick : ∀ (ans : Fl4MsiAnswer) (e' : SyscallError), SysErrRelA x e' → + (match ans with + | .ok => False + | .badDevice => e' = .invalidArgument 1 + | .badEvent => e' = .invalidArgument 2 + | .refused => e' = .illegalOperation) → + ∃ (e2 : error.SysError) (e'' : SyscallError), x = .Sys e2 ∧ VsErrRelA x e'' ∧ + (.error e'', σ) ∈ ((fl4DecodeInvocation (Z := Unit) calling false mi.miLabel + ((y0 :: y1 :: y2 :: y3 :: y4 :: y5 :: rest).map UScalar.bv) + (Fl4SpecA64.toBl cptr.bv) sl c excaps buffer).run σ).1 := by + intro ans e' hrel hans + obtain ⟨e2, hx2⟩ := sys_of_vsErrRelA (Or.inl hrel) + refine ⟨e2, e', hx2, Or.inl hrel, ?_⟩ + rw [run_dec, dm] + unfold Fl4Spec.NonDetE.liftME + rw [ExceptT.run_bind] + refine mem_bindA.2 ⟨(.error e', σ), ?_, rfl⟩ + unfold decodeFl4IrqMsi + rw [if_pos hcond] + simp only [hexc, List.map_cons, the, List.getElem?_cons_zero, List.getElem?_cons_succ, Option.getD_some, + Fl4Spec.NonDetE.whenE, if_neg hn', if_neg hc', ndE_pure_bindD] + rw [hu, hdep, run_bind_okD hrun, ExceptT.run_bind] + refine mem_bindA.2 ⟨(.ok ans, σ), (by + unfold Fl4Spec.NonDetE.liftE + rw [ExceptT.run_lift] + exact ⟨(ans, σ), ⟨Set.mem_univ _, rfl⟩, rfl⟩), ?_⟩ + cases ans <;> first | exact hans.elim | (subst hans; exact Set.mem_singleton _) + split at hK + · obtain ⟨s1, -, hK⟩ := bind_eq_ok hK + obtain ⟨⟨s2, a2⟩, -, hK⟩ := bind_eq_ok hK + exact absurd hK noOk + · exact pick .badDevice (.invalidArgument 1) (by rw [errOf hK]; rfl) rfl + · exact pick .badEvent (.invalidArgument 2) (by rw [errOf hK]; rfl) rfl + · simp only [error.illegal, bind_ok] at hK + exact pick .refused .illegalOperation (by rw [errOf hK]; trivial) rfl + rw [if_neg hMsi] at hd + simp only [cur_thread_ok, bind_ok] at hd + -- the three SPI / banked labels, uniformly in `kind` + have kinds : ∀ (kind : Nat), kind < 3 → + fl4LabelOf info.label.bv = labNA (if kind = 0 then 29 else if kind = 1 then 65 else 66) → + decodeFl4IrqControl (Z := Unit) (fl4LabelOf info.label.bv) (argsU.map UScalar.bv) sl (excaps.map Prod.fst) = + decodeFl4IrqIssue kind (argsU.map UScalar.bv) sl (excaps.map Prod.fst) := by + intro kind hk h + rw [h] + match kind, hk with + | 0, _ => simp only [↓reduceIte]; rw [labNA_irqCtlAtA.1]; rfl + | 1, _ => simp only [Nat.one_ne_zero, ↓reduceIte]; rw [labNA_irqCtlAtA.2.1]; rfl + | 2, _ => simp only [Nat.reduceEqDiff, ↓reduceIte]; rw [labNA_irqCtlAtA.2.2.1]; rfl + have n1 : fl4_abi.label.IRQIssueIRQHandler ≠ fl4_abi.label.ARMIRQIssueIRQHandlerTriggerCore := by + intro h; have := congrArg UScalar.val h; simp at this + have n2 : fl4_abi.label.ARMIRQIssueIRQHandlerTrigger ≠ fl4_abi.label.IRQIssueIRQHandler := by + intro h; have := congrArg UScalar.val h; simp at this + have n3 : fl4_abi.label.ARMIRQIssueIRQHandlerTrigger ≠ fl4_abi.label.ARMIRQIssueIRQHandlerTriggerCore := by + intro h; have := congrArg UScalar.val h; simp at this + have n4 : fl4_abi.label.ARMIRQIssueIRQHandlerTriggerCore ≠ fl4_abi.label.IRQIssueIRQHandler := n1.symm + have trunc : ∀ (kind : Nat) {args : List Data} {goal : Prop}, x = .Sys .TruncatedMessage → + (∀ e', SysErrRelA x e' → (decodeFl4IrqIssue (Z := Unit) kind args sl (excaps.map Prod.fst)).run σ = + ({(.error e', σ)}, False) → goal) → + ¬ (args.length ≥ (if kind = 0 then 3 else if kind = 2 then 5 else 4) ∧ (excaps.map Prod.fst).length ≥ 1) → + goal := by + intro kind args goal hx w hn + refine w .truncatedMessage (by rw [hx]; trivial) ?_ + unfold decodeFl4IrqIssue + dsimp only + rw [if_neg hn]; rfl + by_cases hP0 : info.label = fl4_abi.label.IRQIssueIRQHandler + · simp only [hP0, n1, ↓reduceIte] at hd + have dm := kinds 0 (by omega) (by rw [fl4LabelOf_bvA, hP0, Fl4ProofsA64.P0.Consts.fl4_abi.label.IRQIssueIRQHandler.val_eq]; rfl) + rw [dm] at wrap + simp only [bind_ok] at hd + rcases issueHead_errA hok J.j.1.place hd with ⟨hx, hn⟩ | ⟨root, cp, sl0, erest, ho, hlenN, hexc, hd⟩ + · exact trunc 0 hx wrap (fun h => hn ⟨by rw [hlen]; simpa using h.1, by simpa using h.2⟩) + have hk : 3 ≤ argsU.length := by rw [← hlen]; simpa using hlenN + obtain ⟨y0, y1, y2, rest, rfl⟩ : ∃ y0 y1 y2 rest, argsU = y0 :: y1 :: y2 :: rest := by + match argsU, hk with + | y0 :: y1 :: y2 :: rest, _ => exact ⟨y0, y1, y2, rest, rfl⟩ + simp only [show msg.syscall_arg st s (L.curW s) buf 0#u64 = ok y0 from hargs 0#u64 y0 rfl, + show msg.syscall_arg st s (L.curW s) buf 1#u64 = ok y1 from hargs 1#u64 y1 rfl, + show msg.syscall_arg st s (L.curW s) buf 2#u64 = ok y2 from hargs 2#u64 y2 rfl, + bind_ok] at hd + have go : ∀ (e' : SyscallError), SysErrRelA x e' → + IssueFailA σ 0 y0.bv (0#u64).bv (y1).bv (y2).bv cp e' → DecErrConclA σ x + (fl4DecodeInvocation (Z := Unit) calling false mi.miLabel ((y0 :: y1 :: y2 :: rest).map UScalar.bv) + (Fl4SpecA64.toBl cptr.bv) sl c excaps buffer) := fun e' hrel h => + wrap e' hrel (issueSpec_errA 0 _ _ _ (cnode := cp) ⟨by simp, by rw [hexc]; simp⟩ (by rw [hexc]; rfl) rfl rfl rfl + rfl h) + rcases issueRange_errA hM hP hd with ⟨hgt, hrel⟩ | ⟨hmax, hle, hlt, hd⟩ + · exact go _ hrel (Or.inl ⟨hgt, rfl⟩) + have hnc : ¬ (0 = 2 ∧ (0#u64 : U64).bv.toNat ≥ fl4A64Platform.numNodes) := fun h => by omega + have hns : ¬ (0 = 1 ∧ y0.bv < 32) := fun h => by omega + rw [hexc] at hok + obtain ⟨e', hrel, hrun⟩ := issueEnd_errA J hok hext ho hle hlt hd (fun irq dest hK => by + first + | exact absurd hK noOk + | (obtain ⟨p1, -, hK⟩ := bind_eq_ok hK + try cases p1 + first + | exact absurd hK noOk + | (obtain ⟨p2, -, hK⟩ := bind_eq_ok hK + try cases p2 + first + | exact absurd hK noOk + | (obtain ⟨p3, -, hK⟩ := bind_eq_ok hK + try cases p3 + exact absurd hK noOk)))) + exact go e' hrel (Or.inr ⟨hmax, Or.inr ⟨hnc, Or.inr ⟨hns, hrun⟩⟩⟩) + rw [if_neg hP0] at hd + by_cases hC : info.label = fl4_abi.label.ARMIRQIssueIRQHandlerTriggerCore + · simp only [hC, n4, ↓reduceIte] at hd + have dm := kinds 2 (by omega) (by rw [fl4LabelOf_bvA, hC, Fl4ProofsA64.P0.Consts.fl4_abi.label.ARMIRQIssueIRQHandlerTriggerCore.val_eq]; rfl) + rw [dm] at wrap + simp only [bind_ok] at hd + rcases issueHead_errA hok J.j.1.place hd with ⟨hx, hn⟩ | ⟨root, cp, sl0, erest, ho, hlenN, hexc, hd⟩ + · exact trunc 2 hx wrap (fun h => hn ⟨by rw [hlen]; simpa using h.1, by simpa using h.2⟩) + have hk : 5 ≤ argsU.length := by rw [← hlen]; simpa using hlenN + obtain ⟨y0, y1, y2, y3, y4, rest, rfl⟩ : ∃ y0 y1 y2 y3 y4 rest, argsU = y0 :: y1 :: y2 :: y3 :: y4 :: rest := by + match argsU, hk with + | y0 :: y1 :: y2 :: y3 :: y4 :: rest, _ => exact ⟨y0, y1, y2, y3, y4, rest, rfl⟩ + simp only [show msg.syscall_arg st s (L.curW s) buf 0#u64 = ok y0 from hargs 0#u64 y0 rfl, + show msg.syscall_arg st s (L.curW s) buf 1#u64 = ok y1 from hargs 1#u64 y1 rfl, + show msg.syscall_arg st s (L.curW s) buf 2#u64 = ok y2 from hargs 2#u64 y2 rfl, + show msg.syscall_arg st s (L.curW s) buf 3#u64 = ok y3 from hargs 3#u64 y3 rfl, + show msg.syscall_arg st s (L.curW s) buf 4#u64 = ok y4 from hargs 4#u64 y4 rfl, + bind_ok] at hd + have go : ∀ (e' : SyscallError), SysErrRelA x e' → + IssueFailA σ 2 y0.bv (y4).bv (y2).bv ((y3 &&& 255#u64)).bv cp e' → DecErrConclA σ x + (fl4DecodeInvocation (Z := Unit) calling false mi.miLabel ((y0 :: y1 :: y2 :: y3 :: y4 :: rest).map UScalar.bv) + (Fl4SpecA64.toBl cptr.bv) sl c excaps buffer) := fun e' hrel h => + wrap e' hrel (issueSpec_errA 2 _ _ _ (cnode := cp) ⟨by simp, by rw [hexc]; simp⟩ (by rw [hexc]; rfl) rfl rfl rfl + rfl h) + rcases issueRange_errA hM hP hd with ⟨hgt, hrel⟩ | ⟨hmax, hle, hlt, hd⟩ + · exact go _ hrel (Or.inl ⟨hgt, rfl⟩) + obtain ⟨nodes, hks, hd⟩ := bind_eq_ok hd + have hnodes := J.toJStage3A.domOK.nodesGet hks + by_cases hcpu : y4 ≥ nodes + · rw [if_pos hcpu] at hd + have hc' : y4.bv.toNat ≥ fl4A64Platform.numNodes := by + rw [← hnodes]; show nodes.val ≤ y4.val; scalar_tac + exact go (.invalidArgument 4) (by rw [errOf hd]; rfl) (Or.inr ⟨hmax, Or.inl ⟨rfl, hc', rfl⟩⟩) + rw [if_neg hcpu] at hd + have hnc : ¬ (2 = 2 ∧ y4.bv.toNat ≥ fl4A64Platform.numNodes) := by + intro h; apply hcpu; show nodes ≤ y4 + rw [UScalar.le_equiv, hnodes]; exact h.2 + have hns : ¬ (2 = 1 ∧ y0.bv < 32) := fun h => by omega + rw [hexc] at hok + obtain ⟨e', hrel, hrun⟩ := issueEnd_errA J hok hext ho hle hlt hd (fun irq dest hK => by + first + | exact absurd hK noOk + | (obtain ⟨p1, -, hK⟩ := bind_eq_ok hK + try cases p1 + first + | exact absurd hK noOk + | (obtain ⟨p2, -, hK⟩ := bind_eq_ok hK + try cases p2 + first + | exact absurd hK noOk + | (obtain ⟨p3, -, hK⟩ := bind_eq_ok hK + try cases p3 + exact absurd hK noOk)))) + exact go e' hrel (Or.inr ⟨hmax, Or.inr ⟨hnc, Or.inr ⟨hns, hrun⟩⟩⟩) + rw [if_neg hC] at hd + by_cases hT : info.label = fl4_abi.label.ARMIRQIssueIRQHandlerTrigger + · simp only [hT, n2, n3, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] at hd + have dm := kinds 1 (by omega) (by rw [fl4LabelOf_bvA, hT, Fl4ProofsA64.P0.Consts.fl4_abi.label.ARMIRQIssueIRQHandlerTrigger.val_eq]; rfl) + rw [dm] at wrap + simp only [bind_ok] at hd + rcases issueHead_errA hok J.j.1.place hd with ⟨hx, hn⟩ | ⟨root, cp, sl0, erest, ho, hlenN, hexc, hd⟩ + · exact trunc 1 hx wrap (fun h => hn ⟨by rw [hlen]; simpa using h.1, by simpa using h.2⟩) + have hk : 4 ≤ argsU.length := by rw [← hlen]; simpa using hlenN + obtain ⟨y0, y1, y2, y3, rest, rfl⟩ : ∃ y0 y1 y2 y3 rest, argsU = y0 :: y1 :: y2 :: y3 :: rest := by + match argsU, hk with + | y0 :: y1 :: y2 :: y3 :: rest, _ => exact ⟨y0, y1, y2, y3, rest, rfl⟩ + simp only [show msg.syscall_arg st s (L.curW s) buf 0#u64 = ok y0 from hargs 0#u64 y0 rfl, + show msg.syscall_arg st s (L.curW s) buf 1#u64 = ok y1 from hargs 1#u64 y1 rfl, + show msg.syscall_arg st s (L.curW s) buf 2#u64 = ok y2 from hargs 2#u64 y2 rfl, + show msg.syscall_arg st s (L.curW s) buf 3#u64 = ok y3 from hargs 3#u64 y3 rfl, + bind_ok] at hd + have go : ∀ (e' : SyscallError), SysErrRelA x e' → + IssueFailA σ 1 y0.bv (0#u64).bv (y2).bv (y3).bv cp e' → DecErrConclA σ x + (fl4DecodeInvocation (Z := Unit) calling false mi.miLabel ((y0 :: y1 :: y2 :: y3 :: rest).map UScalar.bv) + (Fl4SpecA64.toBl cptr.bv) sl c excaps buffer) := fun e' hrel h => + wrap e' hrel (issueSpec_errA 1 _ _ _ (cnode := cp) ⟨by simp, by rw [hexc]; simp⟩ (by rw [hexc]; rfl) rfl rfl rfl + rfl h) + rcases issueRange_errA hM hP hd with ⟨hgt, hrel⟩ | ⟨hmax, hle, hlt, hd⟩ + · exact go _ hrel (Or.inl ⟨hgt, rfl⟩) + have hnc : ¬ (1 = 2 ∧ (0#u64 : U64).bv.toNat ≥ fl4A64Platform.numNodes) := fun h => by omega + by_cases hspi : y0 < layout.irq.SPI_MIN + · rw [if_pos hspi] at hd + simp only [fl4_abi.config.ENABLE_SMP_SUPPORT, ↓reduceIte, error.illegal, bind_ok] at hd + have h32 : y0.bv < 32 := by + have : y0.val < 32 := by simp only [layout.irq.SPI_MIN] at hspi; scalar_tac + exact BitVec.lt_def.2 this + exact go .illegalOperation (by rw [errOf hd]; trivial) (Or.inr ⟨hmax, Or.inr ⟨hnc, Or.inl ⟨rfl, h32, rfl⟩⟩⟩) + rw [if_neg hspi] at hd + have hns : ¬ (1 = 1 ∧ y0.bv < 32) := by + intro h; apply hspi; show y0 < layout.irq.SPI_MIN + rw [UScalar.lt_equiv]; simp only [layout.irq.SPI_MIN]; exact BitVec.lt_def.1 h.2 + rw [hexc] at hok + obtain ⟨e', hrel, hrun⟩ := issueEnd_errA J hok hext ho hle hlt hd (fun irq dest hK => by + first + | exact absurd hK noOk + | (obtain ⟨p1, -, hK⟩ := bind_eq_ok hK + try cases p1 + first + | exact absurd hK noOk + | (obtain ⟨p2, -, hK⟩ := bind_eq_ok hK + try cases p2 + first + | exact absurd hK noOk + | (obtain ⟨p3, -, hK⟩ := bind_eq_ok hK + try cases p3 + exact absurd hK noOk)))) + exact go e' hrel (Or.inr ⟨hmax, Or.inr ⟨hnc, Or.inr ⟨hns, hrun⟩⟩⟩) + rw [if_pos (bne_iff_ne.2 hT)] at hd + simp only [error.illegal, bind_ok] at hd + refine wrap .illegalOperation (by rw [errOf hd]; trivial) ?_ + have m1 : fl4LabelOf info.label.bv ≠ .spec (.genInvocationLabel .irqIssueIRQHandler) := by + rw [fl4LabelOf_bvA] + exact labNA_neA labNA_irqCtlA.1 (by decide) (fun h => hP0 (UScalar.eq_of_val_eq (by simpa using h))) + have m2 : fl4LabelOf info.label.bv ≠ .spec (.archInvocationLabel .armIRQIssueIRQHandlerTrigger) := by + rw [fl4LabelOf_bvA] + exact labNA_neA labNA_irqCtlA.2.1 (by decide) (fun h => hT (UScalar.eq_of_val_eq (by simpa using h))) + have m3 : fl4LabelOf info.label.bv ≠ .ext .armIRQIssueIRQHandlerTriggerCore := by + rw [fl4LabelOf_bvA] + exact labNA_neA labNA_irqCtlA.2.2.1 (by decide) (fun h => hC (UScalar.eq_of_val_eq (by simpa using h))) + have m4 : fl4LabelOf info.label.bv ≠ .ext .armIRQIssueIRQHandlerMSI := by + rw [fl4LabelOf_bvA] + exact labNA_neA labNA_irqCtlA.2.2.2 (by decide) (fun h => hMsi (UScalar.eq_of_val_eq (by simpa using h))) + unfold decodeFl4IrqControl + split + · exact absurd ‹_› m1 + · exact absurd ‹_› m2 + · exact absurd ‹_› m3 + · exact absurd ‹_› m4 + · rfl + +end + +end + +end Fl4ProofsA64.P3.ReplyErr diff --git a/proofs/Fl4ProofsA64/P3/Stage3DecErrKeepA.lean b/proofs/Fl4ProofsA64/P3/Stage3DecErrKeepA.lean new file mode 100644 index 000000000..28350254e --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3DecErrKeepA.lean @@ -0,0 +1,97 @@ +/- +P3 (aarch64, A64-P3 Lift 1b, batch a): **the arch part of `DecErrKeepA`**, from the generated arch `errKA` modules. + +* `ArchKCapA`, `syscall.decode_invocation.errKArchA`: an arch cap's decode `Err` other than `Preempted` keeps the store and + the arch relation (`VKeepA`; `Stage3ErrKeepVsA` / `VsDecA` / `VcpuA`). +* `VcpuStallA` [J]: the VCPU stall keeps the store and the arch relation (the VCPU decoders stall before `DeleteFirst`). +* `DecErrKeepA.arch`: the arch part of `DecErrKeepA`, from the record (the x86-only caps are excluded by `CapWfA`). + +Named premises: [J] `VcpuStallA`; P0's `ArchTotal`. New module. +-/ +import Fl4ProofsA64.P3.Stage3ReplyErrTopA +import Fl4ProofsA64.P3.Stage3ErrKeepVsDecA +import Fl4ProofsA64.P3.Stage3ErrKeepVcpuA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.ReplyErr +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3 Fl4ProofsA64.P3.Endp + Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +/-! ## The arch part of `DecErrKeepA` -/ + +/-- [J] **The VCPU stall is a no-op on the store and keeps the arch relation**: `vcpu::stall_vcpu` stalls the VCPU's +bound thread and its run caller (no-ops on one node, by `JSyscallA.stallTA`) and flushes the VCPU's hardware state +(`vcpu_flush`, keeps `AR`). The VCPU decoders stall before they can return `DeleteFirst`, so an `Err` of those carries +the stalled state. Open: its discharge needs the VCPU's bound thread in `Z.T` and `VcpuArchKeepsA`'s flush. -/ +def VcpuStallA {S A Hw : Type} (st : machine.Store S) (ar : machine.Arch A) (AR : A → Hw → Prop) (hw : Hw) + (s : S) (a : A) : Prop := + ∀ vc : U64, vcpu.stall_vcpu st ar s a vc ⦃ r => r.1 = s ∧ AR r.2 hw ⦄ + +/-- The arch caps whose decoders are `vspace.decode`, `vcpu.decode` and `ept.decode` (every other arch kind is +excluded by `CapWfA`). -/ +def ArchKCapA : capability.Cap → Prop + | .Frame .. | .PageTable .. | .Pml4 .. | .HighVSpace .. | .AsidControl | .AsidPool .. | .Vcpu _ | .Ept .. => True + | _ => False + +section +variable {S A Hw : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {AR : A → Hw → Prop} {hw : Hw} + +set_option maxHeartbeats 8000000 in +/-- **An arch decoder's `Err` other than `Preempted` keeps the store and the arch relation** (`VKeepA`). -/ +theorem syscall.decode_invocation.errKArchA (hA : P0.ArchTotal ar) (s : S) (a : A) (hAR0 : AR a hw) + (hst : ∀ vc', vcpu.stall_vcpu st ar s a vc' ⦃ r => r.1 = s ∧ AR r.2 hw ⦄) + (lbl len : U64) (slot : obj.Slot) + (cap : capability.Cap) (blk call don fp : Bool) (buf : U64) (extra : Aeneas.Std.Array obj.Slot 3#usize) + (hcap : ArchKCapA cap) : + syscall.decode_invocation st ar s a lbl len slot cap blk call don fp buf extra ⦃ VKeepA AR hw s ⦄ := by + have hS : P0.StoreTotal st := L.total + have h_vs := fun lbl len slot cap call buf ex => WP.spec_mono + (@vspace.decode.errKA S A st ar L hA s a lbl len slot cap call buf ex) (fun _ h => VKeepA.of_keep hAR0 h) + have h_vc := @vcpu.decode.vkA S A st ar L Hw AR hw hA s a hAR0 hst + have h_ep := fun lbl len cap slot buf ex => WP.spec_mono + (@ept.decode.errKA S A st ar L hA s a lbl len cap slot buf ex) (fun _ h => VKeepA.of_keep hAR0 h) + unfold syscall.decode_invocation + cases cap + all_goals (simp only [ArchKCapA] at hcap <;> (try exact hcap.elim) <;> errk_steps <;> vk_close) + +end + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {ar : machine.Arch A} {nA : U64} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- **The arch part of `DecErrKeepA`** (module docstring). -/ +theorem DecErrKeepA.arch (hA : P0.ArchTotal ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (hSt : VcpuStallA st ar AR Z.hw s a) {blocking calling donate : Bool} {cptr : U64} : + DecErrKeepA st ar AR Z s a blocking calling donate cptr (fun c => ¬ NonArchA c ∧ c ≠ .IrqControl) := by + intro info buf r1 e x s' a' einfo ebuf elr eex hd hne ⟨hn, hi⟩ + obtain ⟨_, _, _, _, _, _, Q⟩ := J.siteA einfo ebuf elr eex + have wf : CapWfA (capA st s r1.slot) := Q.F.cap.1 + have hk : ArchKCapA (capA st s r1.slot) := by + generalize capA st s r1.slot = c at hn hi wf + cases c <;> simp_all [NonArchA, CapWfA, ArchKCapA] + have k := syscall.decode_invocation.errKArchA hA s a J.j.1.ar hSt info.label (msgLenA info buf) r1.slot + (capA st s r1.slot) blocking calling donate false buf e hk + rw [hd, WP.spec_ok] at k + exact k x rfl hne + +/-- **`DecErrKeepA` for every cap**: the non-arch caps and IRQ control (`DecErrKeepA.of_arch`) and the arch caps +(`DecErrKeepA.arch`). Named premises: [J] `VcpuStallA`, [P] `MsiKeepA`, [S] `hCs`, `numNodes = 1`. -/ +theorem DecErrKeepA.allA (hA : P0.ArchTotal ar) (hM : MsiKeepA ar AR) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < Fl4SpecA64.fl4A64Platform.numNodes) (h1 : Fl4SpecA64.fl4A64Platform.numNodes = 1) + (hSt : VcpuStallA st ar AR Z.hw s a) {blocking calling donate : Bool} {cptr : U64} : + DecErrKeepA st ar AR Z s a blocking calling donate cptr (fun _ => True) := + DecErrKeepA.of_arch hA hM J hCs h1 (DecErrKeepA.arch hA J hSt) + +end + +end + +end Fl4ProofsA64.P3.ReplyErr diff --git a/proofs/Fl4ProofsA64/P3/Stage3DecErrMiscA.lean b/proofs/Fl4ProofsA64/P3/Stage3DecErrMiscA.lean new file mode 100644 index 000000000..90f80e8b1 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3DecErrMiscA.lean @@ -0,0 +1,271 @@ +/- +P3 (aarch64, A64-P3 Lift 1b, batch a): **`DecErrSpecPA` for the simple caps of the Rest group**. + +* `SimpleCapA`: Null, Zombie, Endpoint, Notification, Reply and the x86-only caps. A decode `Err` is FL4's + `InvalidCapability 0` (Null, Zombie, Endpoint / Notification without send), the spec's `invalid_capability 0`; the + other arms return `Ok`, and `CapWfA` excludes the x86-only caps (**`DecErrSpecPA.simple`**). +* `IrqHandlerCapA`: `decode_irq_handler` errs only on `IRQSetIRQHandler` (no extra cap, or not a Notification with + send) and on a foreign label; the spec's `decodeIrqHandlerInvocation` returns the same error + (**`DecErrSpecPA.irqHandler`**). No deletion premise: the arms that delete return `Ok`. + +* **`DecErrSpecPA.rest_of`**: the Rest group from the caps still open. + +No named premise (the record's `PlaceA` only, `irqHandler` / `rest_of`). New module. +-/ +import Fl4ProofsA64.P3.Stage3DecErrBaseA +import Fl4ProofsA64.P3.IrqInvokeDispatchA +import Fl4ProofsA64.P3.VsTopPtA +import Fl4ProofsA64.P3.FrontEndExtraA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.ReplyErr +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3 Fl4ProofsA64.P3.Endp + Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +/-- Null, Zombie, Endpoint, Notification, Reply and the x86-only caps. -/ +def SimpleCapA : capability.Cap → Prop + | .Null | .Zombie .. | .Endpoint .. | .Notification .. | .Reply .. | .PageDirectory .. | .Pdpt .. | .IoPortControl + | .IoPort .. | .TdxControl => True + | _ => False + +/-- The IRQ handler caps. -/ +def IrqHandlerCapA : capability.Cap → Prop + | .IrqHandler _ => True + | _ => False + +set_option maxRecDepth 8000 in +theorem labNA_irqA : + (∀ n, n < 74 → (labNA n = .spec (.genInvocationLabel .irqAckIRQ) → n = 30)) ∧ + (∀ n, n < 74 → (labNA n = .spec (.genInvocationLabel .irqSetIRQHandler) → n = 31)) ∧ + (∀ n, n < 74 → (labNA n = .spec (.genInvocationLabel .irqClearIRQHandler) → n = 32)) := by + decide + +theorem labNA_irqAtA : + labNA 30 = .spec (.genInvocationLabel .irqAckIRQ) ∧ + labNA 31 = .spec (.genInvocationLabel .irqSetIRQHandler) ∧ + labNA 32 = .spec (.genInvocationLabel .irqClearIRQHandler) := by + decide + +/-- An error related by `VsErrRelA`, as the conclusion of `DecErrSpecPA`. -/ +theorem decErr_ofA {x : error.Exc} {e' : Fl4SpecA64.SyscallError} (h : VsErrRelA x e') {α : Type} + {msp : Fl4SpecA64.SEMonad Unit α} {σ : Fl4SpecA64.State Unit} (hr : msp.run σ = ({(.error e', σ)}, False)) : + ∃ (e2 : error.SysError) (e'' : SyscallError), x = .Sys e2 ∧ VsErrRelA x e'' ∧ (.error e'', σ) ∈ (msp.run σ).1 := by + obtain ⟨e2, hx⟩ := sys_of_vsErrRelA h + exact ⟨e2, e', hx, h, by rw [hr]; exact Set.mem_singleton _⟩ + +/-- FL4's `InvalidCapability 0` against the spec's. -/ +theorem invCap0_relA {x : error.Exc} (h : x = .Sys (.InvalidCapability 0#u64)) : VsErrRelA x (.invalidCapability 0) := + Or.inr ⟨0#u64, h, by simp⟩ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {ar : machine.Arch A} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {th : TypedHeapA} + {calling blocking donate : Bool} {cptr : U64} + +set_option maxHeartbeats 4000000 in +/-- **`DecErrSpecPA` for the simple caps** (module docstring). -/ +theorem DecErrSpecPA.simple : DecErrSpecPA st ar th s a σ calling blocking donate cptr SimpleCapA := by + intro info buf r1 e x s' a' mi sl c excaps buffer args _ _ _ _ hd _ hP _ _ hcr _ _ _ _ _ _ _ _ + have inv0 : x = .Sys (.InvalidCapability 0#u64) → fl4DecodeInvocation (Z := Unit) calling false mi.miLabel args + (Fl4SpecA64.toBl cptr.bv) sl c excaps buffer = Fl4Spec.NonDetE.throwError (.invalidCapability 0) → + ∃ (e2 : error.SysError) (e' : SyscallError), x = .Sys e2 ∧ VsErrRelA x e' ∧ + (.error e', σ) ∈ ((fl4DecodeInvocation (Z := Unit) calling false mi.miLabel args + (Fl4SpecA64.toBl cptr.bv) sl c excaps buffer).run σ).1 := by + intro hx hs + exact decErr_ofA (invCap0_relA hx) (by rw [hs]; rfl) + have errOf : ∀ {y : error.Exc} {s1 : S} {a1 : A}, + (ok (core.result.Result.Err y, s1, a1) : Result (core.result.Result Unit error.Exc × S × A)) = + ok (.Err x, s', a') → x = y := by + intro y s1 a1 h + have := (Prod.mk.inj (okinj h)).1 + injection this with h' + exact h'.symm + have noOk : ∀ {s1 : S} {a1 : A}, + (ok (core.result.Result.Ok (), s1, a1) : Result (core.result.Result Unit error.Exc × S × A)) ≠ + ok (.Err x, s', a') := by + intro s1 a1 h + cases (Prod.mk.inj (okinj h)).1 + generalize capA st s r1.slot = cp at hd hP hcr + obtain ⟨wf, hc⟩ := hcr + cases cp <;> try exact hP.elim + case Null => + simp only [syscall.decode_invocation, cur_thread_ok, bind_ok] at hd + have hc2 : c = .nullCap := hc.symm + exact inv0 (errOf hd) (by rw [hc2]; rfl) + case Zombie z zt n => + simp only [syscall.decode_invocation, cur_thread_ok, bind_ok] at hd + have hc2 : c = absCapA (.Zombie z zt n) := hc.symm + exact inv0 (errOf hd) (by rw [hc2]; rfl) + case Endpoint p badge send recv grant gr => + simp only [syscall.decode_invocation, cur_thread_ok, bind_ok] at hd + have hc2 : c = absCapA (.Endpoint p badge send recv grant gr) := hc.symm + cases send + · rw [if_neg Bool.false_ne_true] at hd + refine inv0 (errOf hd) ?_ + rw [hc2] + show (if AllowSend ∈ rightsOfA false recv grant gr then _ else _) = _ + rw [if_neg (by cases recv <;> cases grant <;> cases gr <;> simp [rightsOfA, AllowSend])] + · rw [if_pos rfl] at hd + obtain ⟨s1, -, hd⟩ := bind_eq_ok hd + obtain ⟨⟨s2, a2⟩, -, hd⟩ := bind_eq_ok hd + exact absurd hd noOk + case Notification p badge send recv => + simp only [syscall.decode_invocation, cur_thread_ok, bind_ok] at hd + have hc2 : c = absCapA (.Notification p badge send recv) := hc.symm + cases send + · rw [if_neg Bool.false_ne_true] at hd + refine inv0 (errOf hd) ?_ + rw [hc2] + show (if AllowSend ∈ rightsOfA false recv false false then _ else _) = _ + rw [if_neg (by cases recv <;> simp [rightsOfA, AllowSend])] + · rw [if_pos rfl] at hd + obtain ⟨s1, -, hd⟩ := bind_eq_ok hd + obtain ⟨⟨s2, a2⟩, -, hd⟩ := bind_eq_ok hd + exact absurd hd noOk + case Reply p grant => + simp only [syscall.decode_invocation, cur_thread_ok, bind_ok] at hd + obtain ⟨s1, -, hd⟩ := bind_eq_ok hd + obtain ⟨⟨s2, a2⟩, -, hd⟩ := bind_eq_ok hd + exact absurd hd noOk + all_goals exact wf.elim + +set_option maxHeartbeats 4000000 in +/-- **`DecErrSpecPA` for IRQ handler caps** (module docstring). -/ +theorem DecErrSpecPA.irqHandler {kb : U64} (hPl : PlaceA th kb) : + DecErrSpecPA st ar th s a σ calling blocking donate cptr IrqHandlerCapA := by + intro info buf r1 e x s' a' mi sl c excaps buffer args _ _ _ _ hd _ hP hlab _ hcr hok _ _ _ _ _ _ _ + have errOf : ∀ {y : error.Exc} {s1 : S} {a1 : A}, + (ok (core.result.Result.Err y, s1, a1) : Result (core.result.Result Unit error.Exc × S × A)) = + ok (.Err x, s', a') → x = y := by + intro y s1 a1 h + have := (Prod.mk.inj (okinj h)).1 + injection this with h' + exact h'.symm + have noOk : ∀ {s1 : S} {a1 : A}, + (ok (core.result.Result.Ok (), s1, a1) : Result (core.result.Result Unit error.Exc × S × A)) ≠ + ok (.Err x, s', a') := by + intro s1 a1 h + cases (Prod.mk.inj (okinj h)).1 + generalize capA st s r1.slot = cp at hd hP hcr + obtain ⟨irq, rfl⟩ : ∃ irq, cp = .IrqHandler irq := by + cases cp <;> first | exact ⟨_, rfl⟩ | exact hP.elim + have hc2 : c = absCapA (.IrqHandler irq) := hcr.2.symm + have gA : genInvocationType (specLabelWord (fl4LabelOf mi.miLabel)) = .irqAckIRQ ↔ + info.label = fl4_abi.label.IRQAckIRQ := by + rw [hlab, genIff_labelA _ (by decide) 30 (by decide) labNA_irqAtA.1 labNA_irqA.1, u64_eq_iffA, + Fl4ProofsA64.P0.Consts.fl4_abi.label.IRQAckIRQ.val_eq] + have gS : genInvocationType (specLabelWord (fl4LabelOf mi.miLabel)) = .irqSetIRQHandler ↔ + info.label = fl4_abi.label.IRQSetIRQHandler := by + rw [hlab, genIff_labelA _ (by decide) 31 (by decide) labNA_irqAtA.2.1 labNA_irqA.2.1, u64_eq_iffA, + Fl4ProofsA64.P0.Consts.fl4_abi.label.IRQSetIRQHandler.val_eq] + have gC : genInvocationType (specLabelWord (fl4LabelOf mi.miLabel)) = .irqClearIRQHandler ↔ + info.label = fl4_abi.label.IRQClearIRQHandler := by + rw [hlab, genIff_labelA _ (by decide) 32 (by decide) labNA_irqAtA.2.2 labNA_irqA.2.2, u64_eq_iffA, + Fl4ProofsA64.P0.Consts.fl4_abi.label.IRQClearIRQHandler.val_eq] + obtain ⟨iq, hiq⟩ : ∃ iq, absCapA (.IrqHandler irq) = .irqHandlerCap iq := ⟨_, rfl⟩ + have wrap : ∀ (e' : SyscallError), VsErrRelA x e' → + (decodeIrqHandlerInvocation (Z := Unit) (specLabelWord (fl4LabelOf mi.miLabel)) iq excaps).run σ = + ({(.error e', σ)}, False) → + ∃ (e2 : error.SysError) (e'' : SyscallError), x = .Sys e2 ∧ VsErrRelA x e'' ∧ + (.error e'', σ) ∈ ((fl4DecodeInvocation (Z := Unit) calling false mi.miLabel args + (Fl4SpecA64.toBl cptr.bv) sl c excaps buffer).run σ).1 := by + intro e' hrel hm + have hd' : fl4DecodeInvocation (Z := Unit) calling false mi.miLabel args (Fl4SpecA64.toBl cptr.bv) sl + (.irqHandlerCap iq) excaps buffer = + Fl4Spec.NonDetE.liftME Fl4Invocation.invokeIRQHandler + (decodeIrqHandlerInvocation (Z := Unit) (specLabelWord (fl4LabelOf mi.miLabel)) iq excaps) := rfl + rw [hc2, hiq, hd'] + exact decErr_ofA hrel (run_liftME_errA hm) + unfold syscall.decode_invocation interrupt.decode_irq_handler at hd + simp only [cur_thread_ok, bind_ok] at hd + by_cases hA : info.label = fl4_abi.label.IRQAckIRQ + · rw [if_pos hA] at hd + obtain ⟨s1, -, hd⟩ := bind_eq_ok hd + obtain ⟨a1, -, hd⟩ := bind_eq_ok hd + exact absurd hd noOk + rw [if_neg hA] at hd + by_cases hS : info.label = fl4_abi.label.IRQSetIRQHandler + · rw [if_pos hS, exAtA_idx0, bind_ok] at hd + have hg := gS.2 hS + obtain ⟨-, hs, hz⟩ := hok + cases excaps with + | nil => + rw [hz 0 (by simp) (by omega)] at hd + simp only [obj.Slot.is_null, decide_true, ↓reduceIte, bind_ok, error.truncated] at hd + refine wrap .truncatedMessage (Or.inl ?_) (by rw [irqHandler_specA, hg]) + rw [errOf hd]; trivial + | cons y rest => + obtain ⟨hne, -, -, hcy, -⟩ := srcOK_factsA hPl (hs 0 (by simp)) + simp only [List.getElem_cons_zero] at hcy + simp only [obj.Slot.is_null, hne, decide_false, Bool.false_eq_true, ↓reduceIte, bind_ok, capA_eq] at hd + rw [capAtA_eq (st := st)] at hcy + obtain ⟨cap, slot⟩ := y + replace hcy : CapRelA (capA st s (exAtA e 0)) cap := hcy + have bad : x = .Sys (.InvalidCapability 0#u64) → ¬ (isNtfnCap cap = true ∧ AllowSend ∈ capRights cap) → + ∃ (e2 : error.SysError) (e'' : SyscallError), x = .Sys e2 ∧ VsErrRelA x e'' ∧ + (.error e'', σ) ∈ ((fl4DecodeInvocation (Z := Unit) calling false mi.miLabel args + (Fl4SpecA64.toBl cptr.bv) sl c ((cap, slot) :: rest) buffer).run σ).1 := by + intro hx hn + refine wrap (.invalidCapability 0) (invCap0_relA hx) ?_ + rw [irqHandler_specA, hg]; simp only [hn, ↓reduceIte] + generalize capA st s (exAtA e 0) = k at hd hcy + have other : (∀ p b snd rcv, k ≠ .Notification p b snd rcv) → + ¬ (isNtfnCap cap = true ∧ AllowSend ∈ capRights cap) := by + intro hk hc' + rcases hcy.2.eq_or_pt with h | ⟨_, _, _, h⟩ + · rw [h] at hc' + cases k <;> first | exact hk _ _ _ _ rfl | simp [absCapA, isNtfnCap] at hc' + · rw [h] at hc'; simp [isNtfnCap] at hc' + cases k + case Notification p badge snd rcv => + cases snd + · simp only [Bool.false_eq_true, ↓reduceIte, error.invalid_cap, bind_ok] at hd + refine bad (errOf hd) ?_ + rcases hcy.2.eq_or_pt with h | ⟨_, _, _, h⟩ + · rw [h]; cases rcv <;> simp [absCapA, capRights, rightsOfA, AllowSend] + · rw [h]; simp [isNtfnCap] + · simp only [↓reduceIte] at hd + obtain ⟨s1, -, hd⟩ := bind_eq_ok hd + obtain ⟨isl, -, hd⟩ := bind_eq_ok hd + obtain ⟨⟨s2, a2⟩, -, hd⟩ := bind_eq_ok hd + obtain ⟨s3, -, hd⟩ := bind_eq_ok hd + exact absurd hd noOk + all_goals + simp only [error.invalid_cap, bind_ok] at hd + exact bad (errOf hd) (other (fun _ _ _ _ h => by cases h)) + rw [if_neg hS] at hd + by_cases hC : info.label = fl4_abi.label.IRQClearIRQHandler + · rw [if_pos hC] at hd + obtain ⟨s1, -, hd⟩ := bind_eq_ok hd + obtain ⟨isl, -, hd⟩ := bind_eq_ok hd + obtain ⟨⟨s2, a2⟩, -, hd⟩ := bind_eq_ok hd + exact absurd hd noOk + rw [if_neg hC] at hd + simp only [error.illegal, bind_ok] at hd + refine wrap .illegalOperation (Or.inl (by rw [errOf hd]; trivial)) ?_ + rw [irqHandler_specA] + have n1 := mt gA.1 hA + have n2 := mt gS.1 hS + have n3 := mt gC.1 hC + revert n1 n2 n3 + cases genInvocationType (specLabelWord (fl4LabelOf mi.miLabel)) <;> simp + +/-- **The Rest group from its open part**: the simple caps and the IRQ handler caps are discharged here. -/ +theorem DecErrSpecPA.rest_of {kb : U64} (hPl : PlaceA th kb) + (hO : DecErrSpecPA st ar th s a σ calling blocking donate cptr + (fun c => RestCapA c ∧ ¬ SimpleCapA c ∧ ¬ IrqHandlerCapA c)) : + DecErrSpecPA st ar th s a σ calling blocking donate cptr RestCapA := by + intro info buf r1 e x s' a' mi sl c excaps buffer args h1 h2 h3 h4 hd hne hP + by_cases hs : SimpleCapA (capA st s r1.slot) + · exact DecErrSpecPA.simple info buf r1 e x s' a' mi sl c excaps buffer args h1 h2 h3 h4 hd hne hs + by_cases hi : IrqHandlerCapA (capA st s r1.slot) + · exact DecErrSpecPA.irqHandler hPl info buf r1 e x s' a' mi sl c excaps buffer args h1 h2 h3 h4 hd hne hi + exact hO info buf r1 e x s' a' mi sl c excaps buffer args h1 h2 h3 h4 hd hne ⟨hP, hs, hi⟩ + +end + +end + +end Fl4ProofsA64.P3.ReplyErr diff --git a/proofs/Fl4ProofsA64/P3/Stage3DecErrPtA.lean b/proofs/Fl4ProofsA64/P3/Stage3DecErrPtA.lean new file mode 100644 index 000000000..4bcf9ff95 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3DecErrPtA.lean @@ -0,0 +1,187 @@ +/- +P3 (aarch64, A64-P3 Lift 1b, rest part A): **`DecErrSpecPA` for page-table caps** (`PtCapA`). + +A mapped Stage-2 page table's spec cap may carry a mapped address differing from `absCapA cap`'s (`CapEqA`). No error +branch of the spec decode of a mapped normal page table depends on that address (**`ptDec_errLeA`**), so the error of +`decInv_arch_jA` at `absCapA cap` transfers to the record's cap (**`DecErrSpecPA.pageTable`**). + +Premises as `DecErrSpecPA.archVs`: [P] `K` (`ArchKeepsA`), [P] `hIpa` (`S2IpaE`), [J] `hvcR`, `hYAs`, `hPC`, `hFI`, +`hcO`, `hD` (`DeadDrop4KA`), `hPtMap`. No new named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3DecErrBaseA +import Fl4ProofsA64.P3.VsTopPtA +import Fl4ProofsA64.P3.SpecFrA +import Fl4ProofsA64.P3.CapRelA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.ReplyErr +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3 Fl4ProofsA64.P3.Endp + Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-- Every error `m1` can return, `m2` returns too (from the same state, with the same final state). -/ +def ErrLeA {β γ : Type} (m1 : SEMonad Unit β) (m2 : SEMonad Unit γ) : Prop := + ∀ (σ τ : Fl4SpecA64.State Unit) (e : SyscallError), (.error e, τ) ∈ (m1.run σ).1 → (.error e, τ) ∈ (m2.run σ).1 + +theorem ErrLeA.refl {β : Type} (m : SEMonad Unit β) : ErrLeA m m := fun _ _ _ h => h + +theorem ErrLeA.bind {α β γ : Type} (m : SEMonad Unit α) {f : α → SEMonad Unit β} {g : α → SEMonad Unit γ} + (h : ∀ x, ErrLeA (f x) (g x)) : ErrLeA (m >>= f) (m >>= g) := by + intro σ τ e hm + rw [ExceptT.run_bind] at hm ⊢ + obtain ⟨⟨r, σ1⟩, h1, h2⟩ := mem_bindA.1 hm + refine mem_bindA.2 ⟨(r, σ1), h1, ?_⟩ + cases r with + | error x => + have h3 : ((.error e : Except SyscallError β), τ) = (.error x, σ1) := h2 + obtain ⟨hx, rfl⟩ := Prod.mk.inj h3 + cases hx + exact rfl + | ok x => exact h x _ _ _ h2 + +theorem ErrLeA.ret {β γ : Type} (x : β) (m : SEMonad Unit γ) : + ErrLeA (Fl4Spec.NonDetE.returnOk x : SEMonad Unit β) m := by + intro σ τ e h + have h' := (Prod.mk.inj (show ((.error e : Except SyscallError β), τ) = (.ok x, σ) from h)).1 + cases h' + +theorem ErrLeA.liftME {β γ : Type} (f : β → γ) {m1 m2 : SEMonad Unit β} (h : ErrLeA m1 m2) : + ErrLeA (Fl4Spec.NonDetE.liftME f m1) (Fl4Spec.NonDetE.liftME f m2) := by + intro σ τ e hm + unfold Fl4Spec.NonDetE.liftME at hm ⊢ + rw [ExceptT.run_bind] at hm ⊢ + obtain ⟨⟨r, σ1⟩, h1, h2⟩ := mem_bindA.1 hm + cases r with + | error x => exact mem_bindA.2 ⟨(.error x, σ1), h _ _ _ h1, h2⟩ + | ok x => + have h3 : ((.error e : Except SyscallError γ), τ) = (.ok (f x), σ1) := h2 + cases (Prod.mk.inj h3).1 + +/-- The spec's decode of a mapped normal page table (`decode_page_table_invocation`): errors do not depend on the +mapped address. -/ +theorem ptSpecDec_errLeA (label : Data) (args : List Data) (cte : Fl4SpecA64.CslotPtr) (p : Fl4SpecA64.ObjRef) + (asid : Fl4SpecA64.Asid) (v1 v2 : Fl4SpecA64.VSpaceRef) (exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) : + ErrLeA (decodePageTableInvocation (Z := Unit) label args cte (.pageTableCap p .normalPtT (some (asid, v1))) exs) + (decodePageTableInvocation (Z := Unit) label args cte (.pageTableCap p .normalPtT (some (asid, v2))) exs) := by + unfold decodePageTableInvocation + split + · exact ErrLeA.refl _ + split + · refine ErrLeA.bind _ fun final => ?_ + refine ErrLeA.bind _ fun _ => ?_ + exact ErrLeA.ret _ _ + · exact ErrLeA.refl _ + +/-- FL4's arch decode of a mapped normal page table: errors do not depend on the mapped address. -/ +theorem ptDec_errLeA (calling : Bool) (l : Fl4Label) (args : List Data) (xSlot : Fl4SpecA64.CapRef) + (cte : Fl4SpecA64.CslotPtr) (p : Fl4SpecA64.ObjRef) (asid : Fl4SpecA64.Asid) (v1 v2 : Fl4SpecA64.VSpaceRef) + (exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) : + ErrLeA (fl4ArchDecodeInvocation (Z := Unit) calling l args xSlot cte + (.pageTableCap p .normalPtT (some (asid, v1))) exs) + (fl4ArchDecodeInvocation (Z := Unit) calling l args xSlot cte + (.pageTableCap p .normalPtT (some (asid, v2))) exs) := by + have spec : ErrLeA (Fl4Spec.NonDetE.liftME Fl4ArchInvocation.spec <| archDecodeInvocation (Z := Unit) + (specLabelWord l) args xSlot cte (.pageTableCap p .normalPtT (some (asid, v1))) exs) + (Fl4Spec.NonDetE.liftME Fl4ArchInvocation.spec <| archDecodeInvocation (Z := Unit) + (specLabelWord l) args xSlot cte (.pageTableCap p .normalPtT (some (asid, v2))) exs) := + ErrLeA.liftME _ (ptSpecDec_errLeA _ _ _ _ _ _ _ _) + unfold fl4ArchDecodeInvocation + dsimp only + split + · split + · simp only [Fl4Spec.NonDetE.whenE, ne_eq, reduceCtorEq, not_false_eq_true, ↓reduceIte, + Fl4Spec.NonDetE.throwError, ndE_throw_bindD] + exact ErrLeA.refl _ + · exact spec + split + · refine ErrLeA.bind _ fun k => ?_ + cases k with + | none => exact spec + | some k => + refine ErrLeA.bind _ fun final => ?_ + refine ErrLeA.bind _ fun _ => ?_ + exact ErrLeA.ret _ _ + · exact spec + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {ar : machine.Arch A} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} + +set_option maxHeartbeats 8000000 in +/-- **`DecErrSpecPA` for page-table caps** (module docstring). -/ +theorem DecErrSpecPA.pageTable (K : ArchKeepsA ar AR) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (hvcR : ∀ v ∈ Z.V, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → + wa (L.curW s) K ≠ v ∧ wa (L.curW s) K ≠ wa v 24#u64) + (hYAs : ∀ y ∈ Z.Y, ∀ j : U64, j.val < 512 → ¬ AsW (L.ksb s) (peAF y j)) + (hIpa : S2IpaE ar a) (hPC : PtCapInA Z (L.mem s)) (hFI : FinInvA Z.th (L.mem s) σ Z.G) + (hcO : ∀ y ∈ OS, obj.sext48 y = ok y) + (hD : ∀ u frame fi, capA st s u = .Untyped frame 12#u64 fi false → + cdt.ensure_no_children st s u = ok (core.result.Result.Ok ()) → DeadDrop4KA Z H frame fi) + (hPtMap : ∀ (cap : capability.Cap) (lbl ptr : U64) (mp : Bool) (asid va : U64), cap = .PageTable ptr mp asid va → lbl = fl4_abi.label.ARMPageTableMap → + UnmPtA Z.S1 Z.Y OS H.tcbs (L.mem s) σ ptr ∧ ptr ∉ S2PgE Z.X Z.T2 ∧ ptr ∉ Z.Y) + {calling blocking donate : Bool} {cptr : U64} : + DecErrSpecPA st ar Z.th s a σ calling blocking donate cptr PtCapA := by + intro info buf r1 e x s' a' mi sl c excaps buffer args einfo ebuf elr eex hd hne hP hlab hsl hcr hok hab hbv + gmi ggc glb gex gmrs + obtain ⟨-, argsU, hargsE, hlen, hargsU⟩ := J.argsLabA einfo hbv gmi gmrs + subst hargsE + obtain ⟨_, _, _, _, _, _, Q⟩ := J.siteA einfo ebuf elr eex + have J3 := J.toJStage3A + have Je := J3.toJEntA + have hslO : cslotAddrA sl ∈ OS := J3.j5.2.1 _ (hab.isSlot Je.j.1.place) + have hcov : SlotCov st OS s (cslotAddrA sl) := by rw [← hsl]; exact Q.cov + have hext : ∀ x ∈ e.val, x ≠ 0#u64 → x ∈ OS := Q.hext + have RS := J3.rsSiteA hext hslO hcov J.actW J.running J.cur + have hTr : TreeA st OS Z.G.chains Z.G.par s := + Je.j.1.cdt.tree.grow J3.j5.2.1 (fun x hx hn => J3.j5.2.2 x hx hn) + have hcap : capAtA (L.mem s) (cslotAddrA sl) = capA st s r1.slot := by rw [hsl]; rfl + obtain ⟨ptr, mp, asid, va, hcp⟩ : ∃ ptr mp asid va, capA st s r1.slot = .PageTable ptr mp asid va := by + revert hP + cases capA st s r1.slot <;> intro hP <;> first | exact ⟨_, _, _, _, rfl⟩ | exact hP.elim + rw [hcp] at hcap hcr hd + have K1 := decInv_arch_jA K Je RS hvcR J.tabS1 J.tabExt J3.ksbAligned J3.offAsE hYAs hIpa hPC hFI hTr + (fun y hy hn => J3.j5.2.2 y hy hn) hcO hD sl hab hslO (.PageTable ptr mp asid va) trivial hcap info.label + (msgLenA info buf) buf blocking calling donate false e argsU hlen hargsU (Fl4SpecA64.toBl cptr.bv) excaps hok + false buffer (hPtMap _ _) (fun _ _ _ _ _ _ _ _ h => by cases h) (fun _ _ _ _ _ _ _ _ h => by cases h) + (fun _ _ h => by cases h) + have hd' : syscall.decode_invocation st ar s a info.label (msgLenA info buf) (cslotAddrA sl) + (.PageTable ptr mp asid va) blocking calling donate false buf e = ok (.Err x, s', a') := by + rw [← hsl]; exact hd + rw [hd', WP.spec_ok] at K1 + obtain ⟨-, -, e', hD', hrel⟩ := K1 + obtain ⟨e2, hxe⟩ := sys_of_vsErrRelA hrel + refine ⟨e2, e', hxe, hrel, ?_⟩ + have hm : (.error e', σ) ∈ ((fl4DecodeInvocation (Z := Unit) calling false info.label.bv (argsU.map UScalar.bv) + (Fl4SpecA64.toBl cptr.bv) sl (absCapA (.PageTable ptr mp asid va)) excaps buffer).run σ).1 := by + rw [hD']; exact Set.mem_singleton _ + rw [hlab] + rcases hcr.2.eq_or_pt with hc | ⟨p', asid', va', hc⟩ + · rw [hc]; exact hm + cases mp + · have : c = absCapA (.PageTable ptr false asid va) := hcr.2.symm + rw [this]; exact hm + obtain ⟨asid2, va2, hc2, has, -⟩ := CapEqA.pt_mapped_iff.mp hcr.2 + have le : ErrLeA + (fl4DecodeInvocation (Z := Unit) calling false info.label.bv (argsU.map UScalar.bv) + (Fl4SpecA64.toBl cptr.bv) sl (.archObjectCap (.pageTableCap (specPtr ptr) .normalPtT + (some (asidOfA asid, va.bv)))) excaps buffer) + (fl4DecodeInvocation (Z := Unit) calling false info.label.bv (argsU.map UScalar.bv) + (Fl4SpecA64.toBl cptr.bv) sl (.archObjectCap (.pageTableCap (specPtr ptr) .normalPtT + (some (asid2, va2)))) excaps buffer) := by + subst has + exact ErrLeA.liftME _ (ptDec_errLeA _ _ _ _ _ _ _ _ _ _) + rw [hc2] + exact le _ _ _ hm + +end + +end + +end Fl4ProofsA64.P3.ReplyErr diff --git a/proofs/Fl4ProofsA64/P3/Stage3DecErrScA.lean b/proofs/Fl4ProofsA64/P3/Stage3DecErrScA.lean new file mode 100644 index 000000000..bc124d4a0 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3DecErrScA.lean @@ -0,0 +1,97 @@ +/- +P3 (aarch64, A64-P3 Lift 1b, batch a): **`DecErrSpecPA` for SchedControl caps**, from `cfg_decode_sched_controlA`. + +The decoder's `Err` is the spec's `decodeFl4SchedControlInvocation` error (`ScCfgErrA` is `VsErrRelA`); the `Q` of the +refinement is `res.1 = .Ok ()` (FL4 returns `Ok` after the configure step), so no `Err` comes from there. + +Named premises: [P] `ScCfgTimeAgreeA` (timer agreement), [J] `hsb` (a SchedContext cap's size is below 64); P0's +`ArchTotal`. New module. +-/ +import Fl4ProofsA64.P3.Stage3DecErrBaseA +import Fl4ProofsA64.P3.ScCfgDecodeA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.ReplyErr +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3 Fl4ProofsA64.P3.Endp + Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +set_option maxRecDepth 8000 in +/-- FL4's label number 36 is `SchedControlConfigureFlags`, and no other number is. -/ +theorem labNA_scCfgA : ∀ n, n < 74 → + (labNA n = .spec (.genInvocationLabel .schedControlConfigureFlags) → n = 36) := by + decide + +theorem labNA_scCfg36A : labNA 36 = .spec (.genInvocationLabel .schedControlConfigureFlags) := by + decide + +/-- **The label hypothesis of `cfg_decode_sched_controlA`** at the word `fl4DecodeInvocation` passes on. -/ +theorem scCfg_labelA (lbl : U64) : + genInvocationType (specLabelWord (fl4LabelOf lbl.bv)) = .schedControlConfigureFlags ↔ + lbl = fl4_abi.label.SchedControlConfigureFlags := by + rw [genIff_labelA _ (by decide) 36 (by decide) labNA_scCfg36A labNA_scCfgA, u64_eq_iffA, + Fl4ProofsA64.P0.Consts.fl4_abi.label.SchedControlConfigureFlags.val_eq] + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J7 : JSlotA S} {ar : machine.Arch A} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} + +set_option maxHeartbeats 4000000 in +/-- **`DecErrSpecPA` for SchedControl caps** (module docstring). -/ +theorem DecErrSpecPA.schedCtl (hA : P0.ArchTotal ar) (hT : ScCfgTimeAgreeA ar a) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + (hsb : ∀ x0 : U64, x0 ≠ 0#u64 → ∀ p sb, capAtA (L.mem s) x0 = .SchedContext p sb → sb.val < 64) + {calling blocking donate : Bool} {cptr : U64} : + DecErrSpecPA st ar Z.th s a σ calling blocking donate cptr SchedCtlCapA := by + intro info buf r1 e x s' a' mi sl c excaps buffer args einfo ebuf elr eex hd hne hP hlab hsl hcr hok hab hbv + gmi ggc glb gex gmrs + obtain ⟨cpu, hc⟩ : ∃ cpu, capA st s r1.slot = .SchedControl cpu := by + revert hP + generalize capA st s r1.slot = c0 + intro h + cases c0 <;> first | exact ⟨_, rfl⟩ | exact h.elim + rw [hc] at hcr + have hc2 : c = .schedControlCap := hcr.2.symm + subst hc2 + obtain ⟨-, argsU, hargsE, hlen, hargsU⟩ := J.argsLabA einfo hbv gmi gmrs + subst hargsE + have hdc : schedcontext.decode_sched_control st ar s a info.label cpu (msgLenA info buf) buf e = + ok (.Err x, s', a') := by + rw [hc] at hd + simpa only [syscall.decode_invocation, cur_thread_ok, bind_ok, Bool.false_eq_true, ↓reduceIte] using hd + have hex := hok.rel1 J.j.1.place + have hl : genInvocationType (specLabelWord (fl4LabelOf mi.miLabel)) = .schedControlConfigureFlags ↔ + info.label = fl4_abi.label.SchedControlConfigureFlags := by + rw [hlab]; exact scCfg_labelA info.label + have K := cfg_decode_sched_controlA (st := st) (ar := ar) Z.th s a hT σ info.label + (specLabelWord (fl4LabelOf mi.miLabel)) hl cpu (msgLenA info buf) buf (L.curW s) (cur_thread_ok s) argsU hlen + hargsU e (exAtA e 0) (exAtA_idx0 e) (excaps.map Prod.fst) hex (hsb _) (fun _ res => res.1 = .Ok ()) ?_ + · rw [hdc, WP.spec_ok] at K + rcases K with ⟨e0, e', hres, hrun, hrel⟩ | ⟨inv, -, hq⟩ + · obtain ⟨h1, -, -⟩ := Prod.mk.inj hres + have hx : x = e0 := by injection h1 + have hrel' : VsErrRelA x e' := by rw [hx]; exact hrel + obtain ⟨e2, hxe⟩ := sys_of_vsErrRelA hrel' + refine ⟨e2, e', hxe, hrel', ?_⟩ + have hd' : fl4DecodeInvocation (Z := Unit) calling false mi.miLabel (argsU.map UScalar.bv) + (Fl4SpecA64.toBl cptr.bv) sl .schedControlCap excaps buffer = + Fl4Spec.NonDetE.liftME Fl4Invocation.invokeSchedControl + (decodeFl4SchedControlInvocation (Z := Unit) (specLabelWord (fl4LabelOf mi.miLabel)) + (argsU.map UScalar.bv) (excaps.map Prod.fst)) := rfl + rw [hd', run_liftME_errA hrun] + exact Set.mem_singleton _ + · cases hq + · intro p sb bu pu er bd fl budget period _ _ _ _ _ _ _ _ _ _ _ _ + have hS : P0.StoreTotal st := L.total + step* + all_goals first | rfl | (simp only [WP.spec_ok]; done) | (trace_state; done) + +end + +end + +end Fl4ProofsA64.P3.ReplyErr diff --git a/proofs/Fl4ProofsA64/P3/Stage3DecErrScCapA.lean b/proofs/Fl4ProofsA64/P3/Stage3DecErrScCapA.lean new file mode 100644 index 000000000..c498496bb --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3DecErrScCapA.lean @@ -0,0 +1,667 @@ +/- +P3 (aarch64, A64-P3 Lift 1b, batch a, Rest part B): **`DecErrSpecPA` for SchedContext caps**. + +`schedcontext.decode` first stalls the SC's bound thread (a no-op, `JSyscallA.stallSitesA`), then errs only by +`truncated`, `invalid_cap 1` and `illegal`; the spec's `decodeSchedContextInvocation` returns the same error from the +related words (SC `tcb` / `ntfn`, TCB `sc` / state / priority / MCP / yield target, notification `sc`, `released`). + +Premises: the syscall record `J`; [S] `hCs`, `numNodes = 1` (as `DecErrKeepA.of_arch`). No new named premise. New +module. +-/ +import Fl4ProofsA64.P3.Stage3DecErrBaseA +import Fl4ProofsA64.P3.Stage3DecErrMiscA +import Fl4ProofsA64.P3.Stage3StallA +import Fl4ProofsA64.P3.Stage3SchedOpsTA +import Fl4ProofsA64.P3.IpcInvokeDispatchA +import Fl4ProofsA64.P3.IpcScGlueA +import Fl4ProofsA64.P3.VsTopPtA +import Fl4ProofsA64.P3.FrontEndExtraA +import Fl4ProofsA64.P3.ScBindTopA +import Fl4ProofsA64.P3.ScUnbTopA +import Fl4ProofsA64.P3.ScYieldTopA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.ReplyErr +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3 Fl4ProofsA64.P3.Endp + Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +/-- The SchedContext caps. -/ +def ScObjCapA : capability.Cap → Prop + | .SchedContext .. => True + | _ => False + +theorem labNA_scObjA : + (∀ n, n < 74 → (labNA n = .spec (.genInvocationLabel .schedContextBind) → n = 37)) ∧ + (∀ n, n < 74 → (labNA n = .spec (.genInvocationLabel .schedContextUnbind) → n = 38)) ∧ + (∀ n, n < 74 → (labNA n = .spec (.genInvocationLabel .schedContextUnbindObject) → n = 39)) ∧ + (∀ n, n < 74 → (labNA n = .spec (.genInvocationLabel .schedContextConsumed) → n = 40)) ∧ + (∀ n, n < 74 → (labNA n = .spec (.genInvocationLabel .schedContextYieldTo) → n = 41)) := by + set_option maxRecDepth 8000 in decide + +theorem labNA_scObjAtA : + labNA 37 = .spec (.genInvocationLabel .schedContextBind) ∧ + labNA 38 = .spec (.genInvocationLabel .schedContextUnbind) ∧ + labNA 39 = .spec (.genInvocationLabel .schedContextUnbindObject) ∧ + labNA 40 = .spec (.genInvocationLabel .schedContextConsumed) ∧ + labNA 41 = .spec (.genInvocationLabel .schedContextYieldTo) := by + decide + +section spec +variable {σ : Fl4SpecA64.State Unit} {α : Type} + +private theorem run_whenE_throwA {P : Prop} [Decidable P] (h : P) (e : SyscallError) + (f : Unit → Fl4Spec.NonDetE (Fl4SpecA64.State Unit) SyscallError α) : + (NonDetE.whenE P (NonDetE.throwError e) >>= f).run σ = ({(.error e, σ)}, False) := by + unfold NonDetE.whenE + rw [if_pos h] + exact run_bind_errorD (s' := σ) (e := e) rfl f + +private theorem whenE_skipA {P : Prop} [Decidable P] (h : ¬ P) (e : SyscallError) + (f : Unit → Fl4Spec.NonDetE (Fl4SpecA64.State Unit) SyscallError α) : + (NonDetE.whenE P (NonDetE.throwError e) >>= f) = f () := by + simp [NonDetE.whenE, h, NonDetE.returnOk] + +private theorem scReleased_getA {p : ObjRef} {x : SchedContext} {n : Nat} + (hk : σ.kheap p = some (.schedContext x n)) (hne : scActive x = true → x.scRefills ≠ []) : + getScReleased (Z := Unit) p σ = ({(scReleased σ.curTime x, σ)}, False) := by + have hr : scActive x = true → readScRefillReady (Z := Unit) p σ = some (refillReady σ.curTime (refillHd x)) := by + intro ha + show Option.bind (readRefillHead p σ) (fun h => Option.bind (Lookup.asks (fun s : State Unit => s.curTime) σ) + (fun ct => Lookup.oreturn (refillReady ct h) σ)) = _ + rw [readRefillHead_eqA hk (hne ha)]; rfl + have ha : readScActive (Z := Unit) p σ = some (scActive x) := by + show Option.bind (readSchedContext p σ) (fun sc => Lookup.oreturn (scActive sc) σ) = _ + rw [readSchedContext_eqA hk]; rfl + have e : readScReleased (Z := Unit) p σ = some (scReleased σ.curTime x) := by + show Option.bind (readScActive p σ) (fun a => (if a then readScRefillReady p else Lookup.oreturn false) σ) = _ + rw [ha] + cases h : scActive x + · simp [scReleased, h, Lookup.oreturn] + · simp [scReleased, h, hr h] + simp [getScReleased, NonDet.getsThe, e, NonDet.assertOpt]; rfl + +end spec + +section conc +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +private theorem sc_extra_capA (s : S) (e : Aeneas.Std.Array obj.Slot 3#usize) : + schedcontext.extra_cap st s e 0#usize = + ok (if exAtA e 0 = 0#u64 then none else some (capA st s (exAtA e 0))) := by + unfold schedcontext.extra_cap + rw [if_neg (by decide), exAtA_idx0, bind_ok] + by_cases h : exAtA e 0 = 0#u64 + · simp [obj.Slot.is_null, h] + · simp [obj.Slot.is_null, h, capA_eq] + +private theorem sc_is_blockedA {s : S} {t : U64} {ts : ThreadState} + (hst : (L.mem s (stA t)).val = tsCodeA ts) : + sched.is_blocked st s t = ok (ipcQueuedThreadState ts) := by + unfold sched.is_blocked + rw [eq_of_spec_fd (obj.Tcb.state.spec s t), bind_ok] + have e : ∀ k : U64, L.mem s (stA t) = k ↔ tsCodeA ts = k.val := + fun k => ⟨fun h => h ▸ hst.symm, fun h => UScalar.eq_of_val_eq (hst.trans h)⟩ + have e5 : sched.TS_BLOCKED_ON_REPLY = 5#u64 := by unfold sched.TS_BLOCKED_ON_REPLY; rfl + obtain ⟨-, -, h3, h4, h6⟩ := ts_vals + simp only [e, h3, h4, h6, e5] + cases ts <;> simp [tsCodeA, ipcQueuedThreadState] + +private theorem sc_releasedA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRN st Z m κ s σ) {sc : U64} (hc : sc ∈ Z.C) {x : SchedContext} {n : Nat} + (hk : σ.kheap (scNameA m sc) = some (.schedContext x n)) : + sporadic.sc_released st s sc = ok (scReleased σ.curTime x) := by + obtain ⟨y, k, hy, hx⟩ := H.scr.sc sc hc + rw [hk] at hy; cases hy + have ha : sporadic.sc_active st s sc = ok (scActive x) := by + unfold sporadic.sc_active + rw [eq_of_spec_fd (obj.Sc.refill_max.spec (L := L) s sc), bind_ok] + congr 1 + apply decide_eq_decide.2 + rw [gt_iff_valSc, hx.max] + rfl + unfold sporadic.sc_released + rw [ha, bind_ok] + by_cases h : scActive x = true + · rw [if_pos h, scReleased, h, Bool.true_and] + have hReady := getScRefillReady_eqA hk (H.inv.ne hk h) + exact eq_of_spec_fd (WP.spec_mono (ready_refinesA H.scr H.sch.time hc hk h) fun b hb => + (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst (hReady.symm.trans hb)))).1.symm) + · have h0 : scActive x = false := (Bool.not_eq_true _) ▸ h + rw [if_neg h, scReleased, h0, Bool.false_and] + +end conc + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {ar : machine.Arch A} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} + +set_option maxHeartbeats 8000000 in +/-- **`DecErrSpecPA` for SchedContext caps** (module docstring). -/ +theorem DecErrSpecPA.schedContext + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < Fl4SpecA64.fl4A64Platform.numNodes) (h1 : Fl4SpecA64.fl4A64Platform.numNodes = 1) + {calling blocking donate : Bool} {cptr : U64} : + DecErrSpecPA st ar Z.th s a σ calling blocking donate cptr ScObjCapA := by + intro info buf r1 e x s' a' mi sl c excaps buffer args einfo ebuf elr eex hd _ hP hlab hsl hcr hok hab _ _ _ _ _ _ + obtain ⟨_, _, _, _, _, _, Q⟩ := J.siteA einfo ebuf elr eex + have hstall := (J.stallSitesA hCs h1 Q.hOS).2 + have hOS := Q.hOS + have errOf : ∀ {y : error.Exc} {s1 : S} {a1 : A}, + (ok (core.result.Result.Err y, s1, a1) : Result (core.result.Result Unit error.Exc × S × A)) = + ok (.Err x, s', a') → x = y := by + intro y s1 a1 h + have := (Prod.mk.inj (okinj h)).1 + injection this with h' + exact h'.symm + have noOk : ∀ {s1 : S} {a1 : A}, + (ok (core.result.Result.Ok (), s1, a1) : Result (core.result.Result Unit error.Exc × S × A)) ≠ + ok (.Err x, s', a') := by + intro s1 a1 h + cases (Prod.mk.inj (okinj h)).1 + generalize hcp : capA st s r1.slot = cp at hd hP hcr + obtain ⟨sc, b, rfl⟩ : ∃ sc b, cp = .SchedContext sc b := by + cases cp <;> first | exact ⟨_, _, rfl⟩ | exact hP.elim + obtain ⟨i, hi, hs⟩ := hstall sc b hcp + have hi' : obj.Sc.tcb st sc s = ok (L.mem s (scTcbA sc)) := eq_of_spec_fd (Fl4ProofsA64.P1.obj.Sc.tcb.spec s sc) + obtain rfl : i = L.mem s (scTcbA sc) := okinj (hi.symm.trans hi') + -- the SC and its spec object + have hH : H sc = some (.sc b.val) := (J.capTy _ hOS).2.2.2.1 sc b hcp + have hC : sc ∈ Z.C := by + rw [← J.scs]; exact ⟨_, hH⟩ + have T := J.toJStage3A.toTA J.sact J.noSwitch + have hnm : scNameA m sc = specPtr sc := if_neg (T.ciSc _ hOS sc b hcp) + obtain ⟨y, k, hk, hy⟩ := J.sr.scr.sc sc hC + rw [hnm] at hk + have hct : L.curW s ∈ Z.T := J.sr.curT + have hcur : σ.curThread = specPtr (L.curW s) := J.j.1.sch.curThread + have gcur : NonDet.gets (fun s : Fl4SpecA64.State Unit => s.curThread) σ = ({(specPtr (L.curW s), σ)}, False) := by + rw [← hcur]; rfl + have hscT : obj.Sc.tcb st sc s = ok (L.mem s (scTcbA sc)) := hi' + have hyT : y.scTcb = optRefS (L.mem s (scTcbA sc)) := by rw [hy.tcb, scTcbA_eqSc] + have hscN : obj.Sc.ntfn st sc s = ok (L.mem s (scWA sc 40#u64)) := by + refine eq_of_spec_fd (show obj.Sc.ntfn st sc s ⦃ v => v = L.mem s (scWA sc 40#u64) ⦄ from ?_) + unfold obj.Sc.ntfn scWA layout.sc.NTFN + step* + simp_all + have hc2 : c = absCapA (.SchedContext sc b) := hcr.2.symm + have wrap : ∀ (e' : SyscallError), VsErrRelA x e' → + (decodeSchedContextInvocation (Z := Unit) (specLabelWord (fl4LabelOf mi.miLabel)) (specPtr sc) + (excaps.map Prod.fst)).run σ = ({(.error e', σ)}, False) → + ∃ (e2 : error.SysError) (e'' : SyscallError), x = .Sys e2 ∧ VsErrRelA x e'' ∧ + (.error e'', σ) ∈ ((fl4DecodeInvocation (Z := Unit) calling false mi.miLabel args + (Fl4SpecA64.toBl cptr.bv) sl c excaps buffer).run σ).1 := by + intro e' hrel hm + rw [hc2] + exact decErr_ofA hrel (decodeSc_errorA hm) + have ill : ∀ {s1 : S} {a1 : A}, + (error.illegal Unit >>= fun r => ok (r, s1, a1)) = ok (.Err x, s', a') → + (decodeSchedContextInvocation (Z := Unit) (specLabelWord (fl4LabelOf mi.miLabel)) (specPtr sc) + (excaps.map Prod.fst)).run σ = ({(.error .illegalOperation, σ)}, False) → + ∃ (e2 : error.SysError) (e'' : SyscallError), x = .Sys e2 ∧ VsErrRelA x e'' ∧ + (.error e'', σ) ∈ ((fl4DecodeInvocation (Z := Unit) calling false mi.miLabel args + (Fl4SpecA64.toBl cptr.bv) sl c excaps buffer).run σ).1 := by + intro s1 a1 h hm + obtain ⟨r, hr, h⟩ := bind_eq_ok h + unfold error.illegal at hr + cases okinj hr + exact wrap .illegalOperation (Or.inl (by rw [errOf h]; trivial)) hm + have inv1 : ∀ {s1 : S} {a1 : A}, + (error.invalid_cap Unit 1#u64 >>= fun r => ok (r, s1, a1)) = ok (.Err x, s', a') → + (decodeSchedContextInvocation (Z := Unit) (specLabelWord (fl4LabelOf mi.miLabel)) (specPtr sc) + (excaps.map Prod.fst)).run σ = ({(.error (.invalidCapability 1), σ)}, False) → + ∃ (e2 : error.SysError) (e'' : SyscallError), x = .Sys e2 ∧ VsErrRelA x e'' ∧ + (.error e'', σ) ∈ ((fl4DecodeInvocation (Z := Unit) calling false mi.miLabel args + (Fl4SpecA64.toBl cptr.bv) sl c excaps buffer).run σ).1 := by + intro s1 a1 h hm + obtain ⟨r, hr, h⟩ := bind_eq_ok h + unfold error.invalid_cap at hr + cases okinj hr + exact wrap (.invalidCapability 1) (Or.inr ⟨1#u64, errOf h, by simp⟩) hm + have trunc : ∀ {s1 : S} {a1 : A}, + (error.truncated Unit >>= fun r => ok (r, s1, a1)) = ok (.Err x, s', a') → + (decodeSchedContextInvocation (Z := Unit) (specLabelWord (fl4LabelOf mi.miLabel)) (specPtr sc) + (excaps.map Prod.fst)).run σ = ({(.error .truncatedMessage, σ)}, False) → + ∃ (e2 : error.SysError) (e'' : SyscallError), x = .Sys e2 ∧ VsErrRelA x e'' ∧ + (.error e'', σ) ∈ ((fl4DecodeInvocation (Z := Unit) calling false mi.miLabel args + (Fl4SpecA64.toBl cptr.bv) sl c excaps buffer).run σ).1 := by + intro s1 a1 h hm + obtain ⟨r, hr, h⟩ := bind_eq_ok h + unfold error.truncated at hr + cases okinj hr + exact wrap .truncatedMessage (Or.inl (by rw [errOf h]; trivial)) hm + have gate : ∀ (g : Fl4SpecA64.GenInvocationLabels), g ≠ .invalidInvocation → ∀ (k : Nat), k < 74 → + labNA k = .spec (.genInvocationLabel g) → + (∀ n, n < 74 → labNA n = .spec (.genInvocationLabel g) → n = k) → ∀ (w : U64), w.val = k → + (genInvocationType (specLabelWord (fl4LabelOf mi.miLabel)) = g ↔ info.label = w) := by + intro g hg k hk h1' h2 w hw + rw [hlab, genIff_labelA g hg k hk h1' h2, u64_eq_iffA, hw] + have gB := gate .schedContextBind (by decide) 37 (by decide) labNA_scObjAtA.1 labNA_scObjA.1 + _ Fl4ProofsA64.P0.Consts.fl4_abi.label.SchedContextBind.val_eq + have gU := gate .schedContextUnbind (by decide) 38 (by decide) labNA_scObjAtA.2.1 labNA_scObjA.2.1 + _ Fl4ProofsA64.P0.Consts.fl4_abi.label.SchedContextUnbind.val_eq + have gO := gate .schedContextUnbindObject (by decide) 39 (by decide) labNA_scObjAtA.2.2.1 labNA_scObjA.2.2.1 + _ Fl4ProofsA64.P0.Consts.fl4_abi.label.SchedContextUnbindObject.val_eq + have gC := gate .schedContextConsumed (by decide) 40 (by decide) labNA_scObjAtA.2.2.2.1 labNA_scObjA.2.2.2.1 + _ Fl4ProofsA64.P0.Consts.fl4_abi.label.SchedContextConsumed.val_eq + have gY := gate .schedContextYieldTo (by decide) 41 (by decide) labNA_scObjAtA.2.2.2.2 labNA_scObjA.2.2.2.2 + _ Fl4ProofsA64.P0.Consts.fl4_abi.label.SchedContextYieldTo.val_eq + -- the first extra cap, on both sides + have hex : (excaps = [] ∧ exAtA e 0 = 0#u64) ∨ ∃ cap slot rest, excaps = (cap, slot) :: rest ∧ + exAtA e 0 ≠ 0#u64 ∧ IsSlotA Z.th (exAtA e 0) ∧ CapRelA (capA st s (exAtA e 0)) cap := by + obtain ⟨-, hsrc, hz⟩ := hok + cases excaps with + | nil => exact Or.inl ⟨rfl, hz 0 (by simp) (by omega)⟩ + | cons p rest => + obtain ⟨hne, hS, -, hcy, -⟩ := srcOK_factsA J.j.1.place (hsrc 0 (by simp)) + rw [capAtA_eq (st := st)] at hcy + exact Or.inr ⟨p.1, p.2, rest, rfl, hne, hS, hcy⟩ + unfold syscall.decode_invocation at hd + simp only [cur_thread_ok, bind_ok, Bool.false_eq_true, ↓reduceIte] at hd + unfold schedcontext.decode at hd + rw [hscT, bind_ok, hs, bind_ok] at hd + simp only [uncurry_apply_pair, cur_thread_ok, bind_ok, sc_extra_capA] at hd + by_cases lC : info.label = fl4_abi.label.SchedContextConsumed + · rw [if_pos lC] at hd + obtain ⟨s2, -, hd⟩ := bind_eq_ok hd + obtain ⟨s3, -, hd⟩ := bind_eq_ok hd + obtain ⟨s4, -, hd⟩ := bind_eq_ok hd + exact absurd hd noOk + rw [if_neg lC] at hd + have other : ∀ (k0 : capability.Cap) (cap : Fl4SpecA64.Cap), CapRelA k0 cap → + (∀ p, k0 ≠ .Thread p) → (∀ p bd sn rc, k0 ≠ .Notification p bd sn rc) → + (∀ p, cap ≠ .threadCap p) ∧ (∀ p bd rs, cap ≠ .notificationCap p bd rs) := by + intro k0 cap h hT hN + rcases h.2.eq_or_pt with h' | ⟨_, _, _, h'⟩ + · rw [h'] + cases k0 + case Thread p => exact absurd rfl (hT p) + case Notification p bd sn rc => exact absurd rfl (hN p bd sn rc) + all_goals exact ⟨fun _ h => by simp [absCapA] at h, fun _ _ _ h => by simp [absCapA] at h⟩ + · rw [h'] + exact ⟨fun _ h => (by cases h), fun _ _ _ h => (by cases h)⟩ + by_cases lB : info.label = fl4_abi.label.SchedContextBind + · rw [if_pos lB] at hd + have hg := gB.2 lB + have spec : ∀ e', (decodeSchedContextBind (Z := Unit) (specPtr sc) (excaps.map Prod.fst)).run σ = + ({(.error e', σ)}, False) → + (decodeSchedContextInvocation (Z := Unit) (specLabelWord (fl4LabelOf mi.miLabel)) (specPtr sc) + (excaps.map Prod.fst)).run σ = ({(.error e', σ)}, False) := by + intro e' h + unfold decodeSchedContextInvocation + rw [hg]; exact h + rcases hex with ⟨hnil, h0⟩ | ⟨cap, slot, rest, hcons, hne, hS, hcy⟩ + · rw [if_pos h0] at hd + refine trunc hd (spec _ ?_) + rw [hnil] + unfold decodeSchedContextBind + exact run_whenE_throwA rfl _ _ + rw [if_neg hne] at hd + have cv := J.toJStage3A.capValidA hS + have hxOS := (J.toJStage3A.slotW_of hS).1 + have kind := J.capTy _ hxOS + have enter : (decodeSchedContextBind (Z := Unit) (specPtr sc) (excaps.map Prod.fst)).run σ = + (decodeSchedContextBind (Z := Unit) (specPtr sc) (cap :: rest.map Prod.fst)).run σ := by rw [hcons]; rfl + generalize hk0 : capA st s (exAtA e 0) = k0 at hd hcy cv kind + have gsc0 := Sched.getSchedContext_eqA hk + by_cases hT : ∃ t, k0 = .Thread t + · obtain ⟨t, rfl⟩ := hT + have hcap : cap = .threadCap (specPtr t) := by + rcases hcy.2.eq_or_pt with h | ⟨_, _, _, h⟩ + · exact h + · have := hcy.2; rw [h] at this; simp [CapEqA, absCapA] at this + have ht : t ∈ Z.T := (J.sr.thT t).1 cv + obtain ⟨xt, hxt, hxsc⟩ := J.sr.sch.tcb t ht + have hst := Sched.SchedRelA.stEq J.j.1.sch t ht xt hxt + have htsc : obj.Tcb.sc st t s = ok (L.mem s (scA t)) := eq_of_spec_fd (obj.Tcb.sc.spec (L := L) s t) + have gsc : getTcbObjRef (Z := Unit) Tcb.tcbSchedContext (specPtr t) σ = ({(xt.tcbSchedContext, σ)}, False) := + threadGet_eqSc _ hxt + have gst : getThreadState (Z := Unit) (specPtr t) σ = ({(xt.tcbState, σ)}, False) := threadGet_eqSc _ hxt + dsimp only at hd + rw [hscT, bind_ok] at hd + by_cases c1 : L.mem s (scTcbA sc) = 0#u64 + swap + · rw [if_pos (by first | simpa using c1 | simpa [UScalar.eq_equiv] using c1 | simp [c1])] at hd + refine ill hd ?_ + refine spec _ ?_ + rw [enter] + unfold decodeSchedContextBind + rw [whenE_skipA (by simp)] + simp only [NonDetE.returnOk, ndE_pure_bindA, List.head?_cons, Sched.the_someA] + rw [run_bind_okA (run_liftEA gsc0), hcap] + dsimp only + exact run_whenE_throwA (by rw [hyT, optRefS_ne c1]; simp) _ _ + rw [if_neg (by first | simpa using c1 | simpa [UScalar.eq_equiv] using c1 | simp [c1]), htsc, bind_ok] at hd + have e1 : ¬ y.scTcb ≠ none := by rw [hyT, c1, optRefS_zero]; simp + by_cases c2 : L.mem s (scA t) = 0#u64 + swap + · rw [if_pos (by first | simpa using c2 | simpa [UScalar.eq_equiv] using c2 | simp [c2])] at hd + refine ill hd ?_ + refine spec _ ?_ + rw [enter] + unfold decodeSchedContextBind + rw [whenE_skipA (by simp)] + simp only [NonDetE.returnOk, ndE_pure_bindA, List.head?_cons, Sched.the_someA] + rw [run_bind_okA (run_liftEA gsc0), hcap] + dsimp only + rw [whenE_skipA e1, run_bind_okA (run_liftEA gsc)] + exact run_whenE_throwA (by rw [hxsc]; simp [optScA, c2]) _ _ + rw [if_neg (by first | simpa using c2 | simpa [UScalar.eq_equiv] using c2 | simp [c2]), sc_is_blockedA hst, bind_ok] at hd + have e2 : ¬ xt.tcbSchedContext ≠ none := by rw [hxsc]; simp [optScA, c2] + have hk' : σ.kheap (scNameA m sc) = some (.schedContext y k) := by rw [hnm]; exact hk + rw [sc_releasedA J.sr.toSchedSRN hC hk'] at hd + have grl := scReleased_getA hk (J.sr.inv.ne hk) + by_cases c3 : ipcQueuedThreadState xt.tcbState = true + · rw [if_pos c3, bind_ok] at hd + by_cases c4 : scReleased σ.curTime y = true + · rw [if_pos c4] at hd + obtain ⟨⟨s2, a2⟩, -, hd⟩ := bind_eq_ok hd + obtain ⟨s3, -, hd⟩ := bind_eq_ok hd + obtain ⟨⟨s4, a3⟩, -, hd⟩ := bind_eq_ok hd + exact absurd hd noOk + rw [if_neg c4] at hd + refine ill hd ?_ + refine spec _ ?_ + rw [enter] + unfold decodeSchedContextBind + rw [whenE_skipA (by simp)] + simp only [NonDetE.returnOk, ndE_pure_bindA, List.head?_cons, Sched.the_someA] + rw [run_bind_okA (run_liftEA gsc0), hcap] + dsimp only + rw [whenE_skipA e1, run_bind_okA (run_liftEA gsc), whenE_skipA e2, run_bind_okA (run_liftEA gst), + run_bind_okA (run_liftEA grl)] + exact run_whenE_throwA ⟨c3, c4⟩ _ _ + · rw [if_neg c3] at hd + obtain ⟨⟨s2, a2⟩, -, hd⟩ := bind_eq_ok hd + obtain ⟨s3, -, hd⟩ := bind_eq_ok hd + obtain ⟨⟨s4, a3⟩, -, hd⟩ := bind_eq_ok hd + exact absurd hd noOk + by_cases hN : ∃ p bd sn rc, k0 = .Notification p bd sn rc + · obtain ⟨p, bd, sn, rc, rfl⟩ := hN + obtain ⟨rs, hcap⟩ : ∃ rs, cap = .notificationCap (specPtr p) bd.bv rs := by + rcases hcy.2.eq_or_pt with h | ⟨_, _, _, h⟩ + · exact ⟨_, h⟩ + · have := hcy.2; rw [h] at this; simp [CapEqA, absCapA] at this + have hpN : p ∈ N := J.ipcHeapCov.ntfn p (kind.2.1 p bd sn rc rfl) + obtain ⟨z, hz, hzs⟩ := notifScWord hpN J.ntfn J.sr.inv (T.curCore_lt hCs) + have hnsc : obj.Ntfn.sc st p s = ok (L.mem s (nScSc p)) := eq_of_spec_fd (obj.Ntfn.sc.spec (L := L) s p) + dsimp only at hd + rw [hscN, bind_ok] at hd + by_cases c1 : L.mem s (scWA sc 40#u64) = 0#u64 + swap + · rw [if_pos (by first | simpa using c1 | simpa [UScalar.eq_equiv] using c1 | simp [c1])] at hd + refine ill hd ?_ + refine spec _ ?_ + rw [enter] + unfold decodeSchedContextBind + rw [whenE_skipA (by simp)] + simp only [NonDetE.returnOk, ndE_pure_bindA, List.head?_cons, Sched.the_someA] + rw [run_bind_okA (run_liftEA gsc0), hcap] + dsimp only + exact run_whenE_throwA (by rw [hy.ntfn, optRefS_ne c1]; simp) _ _ + rw [if_neg (by first | simpa using c1 | simpa [UScalar.eq_equiv] using c1 | simp [c1]), hnsc, bind_ok] at hd + have e1 : ¬ y.scNtfn ≠ none := by rw [hy.ntfn, c1, optRefS_zero]; simp + by_cases c2 : L.mem s (nScSc p) = 0#u64 + swap + · rw [if_pos (by first | simpa using c2 | simpa [UScalar.eq_equiv] using c2 | simp [c2])] at hd + refine ill hd ?_ + refine spec _ ?_ + rw [enter] + unfold decodeSchedContextBind + rw [whenE_skipA (by simp)] + simp only [NonDetE.returnOk, ndE_pure_bindA, List.head?_cons, Sched.the_someA] + rw [run_bind_okA (run_liftEA gsc0), hcap] + dsimp only + rw [whenE_skipA e1, run_bind_okA (run_liftEA (getNtfnSc_eqSc hz))] + exact run_whenE_throwA (by rw [hzs]; simp [optScA, c2]) _ _ + rw [if_neg (by first | simpa using c2 | simpa [UScalar.eq_equiv] using c2 | simp [c2])] at hd + obtain ⟨s2, -, hd⟩ := bind_eq_ok hd + obtain ⟨s3, -, hd⟩ := bind_eq_ok hd + exact absurd hd noOk + have o := other k0 cap hcy (fun p h => hT ⟨p, h⟩) (fun p bd sn rc h => hN ⟨p, bd, sn, rc, h⟩) + have bad : (decodeSchedContextInvocation (Z := Unit) (specLabelWord (fl4LabelOf mi.miLabel)) (specPtr sc) + (excaps.map Prod.fst)).run σ = ({(.error (.invalidCapability 1), σ)}, False) := by + refine spec _ ?_ + rw [enter] + unfold decodeSchedContextBind + rw [whenE_skipA (by simp)] + simp only [NonDetE.returnOk, ndE_pure_bindA, List.head?_cons, Sched.the_someA] + rw [run_bind_okA (run_liftEA gsc0)] + cases cap + case threadCap p => exact absurd rfl (o.1 p) + case notificationCap p bd rs => exact absurd rfl (o.2 p bd rs) + all_goals exact run_bind_errorD (s' := σ) (e := Fl4SpecA64.SyscallError.invalidCapability 1) rfl _ + cases k0 + case Thread p => exact absurd ⟨p, rfl⟩ hT + case Notification p bd sn rc => exact absurd ⟨p, bd, sn, rc, rfl⟩ hN + all_goals exact inv1 hd bad + rw [if_neg lB] at hd + by_cases lO : info.label = fl4_abi.label.SchedContextUnbindObject + · rw [if_pos lO] at hd + have hg := gO.2 lO + have spec : ∀ e', (decodeSchedContextUnbindObject (Z := Unit) (specPtr sc) (excaps.map Prod.fst)).run σ = + ({(.error e', σ)}, False) → + (decodeSchedContextInvocation (Z := Unit) (specLabelWord (fl4LabelOf mi.miLabel)) (specPtr sc) + (excaps.map Prod.fst)).run σ = ({(.error e', σ)}, False) := by + intro e' h + unfold decodeSchedContextInvocation + rw [hg]; exact h + rcases hex with ⟨hnil, h0⟩ | ⟨cap, slot, rest, hcons, hne, hS, hcy⟩ + · rw [if_pos h0] at hd + refine trunc hd (spec _ ?_) + rw [hnil] + unfold decodeSchedContextUnbindObject + exact run_whenE_throwA rfl _ _ + rw [if_neg hne] at hd + have enter : (decodeSchedContextUnbindObject (Z := Unit) (specPtr sc) (excaps.map Prod.fst)).run σ = + (decodeSchedContextUnbindObject (Z := Unit) (specPtr sc) (cap :: rest.map Prod.fst)).run σ := by rw [hcons]; rfl + generalize hk0 : capA st s (exAtA e 0) = k0 at hd hcy + by_cases hT : ∃ t, k0 = .Thread t + · obtain ⟨t, rfl⟩ := hT + have hcap : cap = .threadCap (specPtr t) := by + rcases hcy.2.eq_or_pt with h | ⟨_, _, _, h⟩ + · exact h + · have := hcy.2; rw [h] at this; simp [CapEqA, absCapA] at this + have gt := getScTcb_eqSc hk + dsimp only at hd + rw [hscT, bind_ok] at hd + by_cases c1 : L.mem s (scTcbA sc) = t + swap + · rw [if_pos (by first | simpa using c1 | simpa [UScalar.eq_equiv] using c1 | simp [c1])] at hd + refine ill hd ?_ + refine spec _ ?_ + rw [enter] + unfold decodeSchedContextUnbindObject + rw [whenE_skipA (by simp)] + simp only [NonDetE.returnOk, ndE_pure_bindA, List.head?_cons, Sched.the_someA] + rw [hcap] + dsimp only + rw [run_bind_okA (run_liftEA gt)] + exact run_whenE_throwA (by + rw [hyT] + intro h + by_cases z : L.mem s (scTcbA sc) = 0#u64 + · rw [z, optRefS_zero] at h; cases h + · rw [optRefS_ne z] at h; exact c1 (specPtr_inj (Option.some.inj h))) _ _ + rw [if_neg (by first | simpa using c1 | simpa [UScalar.eq_equiv] using c1 | simp [c1])] at hd + by_cases c2 : t = L.curW s + swap + · rw [if_neg c2] at hd + obtain ⟨s2, -, hd⟩ := bind_eq_ok hd + obtain ⟨s3, -, hd⟩ := bind_eq_ok hd + exact absurd hd noOk + rw [if_pos c2] at hd + have tne : t ≠ 0#u64 := by + intro h + exact (J.sr.core.tcbs _ hct).ne0 (by rw [← c2, h]; rfl) + refine ill hd ?_ + refine spec _ ?_ + rw [enter] + unfold decodeSchedContextUnbindObject + rw [whenE_skipA (by simp)] + simp only [NonDetE.returnOk, ndE_pure_bindA, List.head?_cons, Sched.the_someA] + rw [hcap] + dsimp only + rw [run_bind_okA (run_liftEA gt), whenE_skipA (by rw [hyT, c1, optRefS_ne tne]; simp), + run_bind_okA (run_liftEA gcur)] + exact run_whenE_throwA (by rw [c2]) _ _ + by_cases hN : ∃ p bd sn rc, k0 = .Notification p bd sn rc + · obtain ⟨p, bd, sn, rc, rfl⟩ := hN + obtain ⟨rs, hcap⟩ : ∃ rs, cap = .notificationCap (specPtr p) bd.bv rs := by + rcases hcy.2.eq_or_pt with h | ⟨_, _, _, h⟩ + · exact ⟨_, h⟩ + · have := hcy.2; rw [h] at this; simp [CapEqA, absCapA] at this + dsimp only at hd + rw [hscN, bind_ok] at hd + by_cases c1 : L.mem s (scWA sc 40#u64) = p + swap + · rw [if_pos (by first | simpa using c1 | simpa [UScalar.eq_equiv] using c1 | simp [c1])] at hd + refine ill hd ?_ + refine spec _ ?_ + rw [enter] + unfold decodeSchedContextUnbindObject + rw [whenE_skipA (by simp)] + simp only [NonDetE.returnOk, ndE_pure_bindA, List.head?_cons, Sched.the_someA] + rw [hcap] + dsimp only + rw [run_bind_okA (run_liftEA (getScNtfn_eqA hk))] + exact run_whenE_throwA (by + rw [hy.ntfn] + intro h + by_cases z : L.mem s (scWA sc 40#u64) = 0#u64 + · rw [z, optRefS_zero] at h; cases h + · rw [optRefS_ne z] at h; exact c1 (specPtr_inj (Option.some.inj h))) _ _ + rw [if_neg (by first | simpa using c1 | simpa [UScalar.eq_equiv] using c1 | simp [c1])] at hd + obtain ⟨s2, -, hd⟩ := bind_eq_ok hd + obtain ⟨s3, -, hd⟩ := bind_eq_ok hd + exact absurd hd noOk + have o := other k0 cap hcy (fun p h => hT ⟨p, h⟩) (fun p bd sn rc h => hN ⟨p, bd, sn, rc, h⟩) + have bad : (decodeSchedContextInvocation (Z := Unit) (specLabelWord (fl4LabelOf mi.miLabel)) (specPtr sc) + (excaps.map Prod.fst)).run σ = ({(.error (.invalidCapability 1), σ)}, False) := by + refine spec _ ?_ + rw [enter] + unfold decodeSchedContextUnbindObject + rw [whenE_skipA (by simp)] + simp only [NonDetE.returnOk, ndE_pure_bindA, List.head?_cons, Sched.the_someA] + cases cap + case threadCap p => exact absurd rfl (o.1 p) + case notificationCap p bd rs => exact absurd rfl (o.2 p bd rs) + all_goals exact run_bind_errorD (s' := σ) (e := Fl4SpecA64.SyscallError.invalidCapability 1) rfl _ + cases k0 + case Thread p => exact absurd ⟨p, rfl⟩ hT + case Notification p bd sn rc => exact absurd ⟨p, bd, sn, rc, rfl⟩ hN + all_goals exact inv1 hd bad + rw [if_neg lO] at hd + by_cases lU : info.label = fl4_abi.label.SchedContextUnbind + · rw [if_pos lU, hscT, bind_ok] at hd + by_cases c1 : L.mem s (scTcbA sc) = L.curW s + swap + · rw [if_neg c1] at hd + obtain ⟨s2, -, hd⟩ := bind_eq_ok hd + obtain ⟨⟨s3, a2⟩, -, hd⟩ := bind_eq_ok hd + obtain ⟨s4, -, hd⟩ := bind_eq_ok hd + obtain ⟨s5, -, hd⟩ := bind_eq_ok hd + exact absurd hd noOk + rw [if_pos c1] at hd + refine ill hd ?_ + unfold decodeSchedContextInvocation + rw [gU.2 lU] + dsimp only + rw [run_bind_okA (run_liftEA (Sched.getSchedContext_eqA hk)), run_bind_okA (run_liftEA gcur)] + refine run_whenE_throwA ?_ _ _ + rw [hyT, c1, optRefS_ne] + intro h + exact (J.sr.core.tcbs _ hct).ne0 (by rw [h]; rfl) + rw [if_neg lU] at hd + by_cases lY : info.label = fl4_abi.label.SchedContextYieldTo + · have spec : ∀ e', (decodeSchedContextYieldTo (Z := Unit) (specPtr sc)).run σ = ({(.error e', σ)}, False) → + (decodeSchedContextInvocation (Z := Unit) (specLabelWord (fl4LabelOf mi.miLabel)) (specPtr sc) + (excaps.map Prod.fst)).run σ = ({(.error e', σ)}, False) := by + intro e' h + unfold decodeSchedContextInvocation + rw [gY.2 lY]; exact h + have gt := getScTcb_eqSc hk + rw [if_pos lY, hscT, bind_ok, eq_of_spec_fd (obj.Tcb.is_null.spec (L.mem s (scTcbA sc))), bind_ok] at hd + by_cases c1 : L.mem s (scTcbA sc) = 0#u64 + · rw [if_pos (by first | simpa using c1 | simpa [UScalar.eq_equiv] using c1 | simp [c1])] at hd + refine ill hd (spec _ ?_) + unfold decodeSchedContextYieldTo + rw [run_bind_okA (run_liftEA gt)] + exact run_whenE_throwA (by rw [hyT, c1, optRefS_zero]) _ _ + rw [if_neg (by first | simpa using c1 | simpa [UScalar.eq_equiv] using c1 | simp [c1])] at hd + have htb : y.scTcb = some (specPtr (L.mem s (scTcbA sc))) := by rw [hyT, optRefS_ne c1] + have skip1 : (decodeSchedContextYieldTo (Z := Unit) (specPtr sc)).run σ = + (NonDetE.whenE (some (specPtr (L.mem s (scTcbA sc))) = some (specPtr (L.curW s))) + (NonDetE.throwError .illegalOperation) >>= fun _ => + NonDetE.liftE (threadGet (Z := Unit) Tcb.tcbPriority (specPtr (L.mem s (scTcbA sc)))) >>= fun prios => + NonDetE.liftE (threadGet (Z := Unit) Tcb.tcbMcpriority (specPtr (L.curW s))) >>= fun ctMcp => + NonDetE.whenE (prios > ctMcp) (NonDetE.throwError .illegalOperation) >>= fun _ => + NonDetE.liftE (threadGet (Z := Unit) Tcb.tcbYieldTo (specPtr (L.curW s))) >>= fun ytPtr => + NonDetE.whenE (ytPtr ≠ none) (NonDetE.throwError .illegalOperation) >>= fun _ => + NonDetE.returnOk (SchedContextInvocation.invokeSchedContextYieldTo (specPtr sc))).run σ := by + unfold decodeSchedContextYieldTo + rw [run_bind_okA (run_liftEA gt), htb, whenE_skipA (by simp), run_bind_okA (run_liftEA gcur)] + simp only [Sched.the_someA] + have htne : obj.Tcb.Insts.CoreCmpPartialEqTcb.eq (L.mem s (scTcbA sc)) (L.curW s) = + ok (decide (L.mem s (scTcbA sc) = L.curW s)) := by + unfold obj.Tcb.Insts.CoreCmpPartialEqTcb.eq + by_cases h : L.mem s (scTcbA sc) = L.curW s <;> simp [h] + rw [htne, bind_ok] at hd + by_cases c2 : L.mem s (scTcbA sc) = L.curW s + · rw [if_pos (by first | simpa using c2 | simpa [UScalar.eq_equiv] using c2 | simp [c2])] at hd + refine ill hd (spec _ ?_) + rw [skip1] + exact run_whenE_throwA (by rw [c2]) _ _ + rw [if_neg (by first | simpa using c2 | simpa [UScalar.eq_equiv] using c2 | simp [c2])] at hd + have e2 : ¬ some (specPtr (L.mem s (scTcbA sc))) = some (specPtr (L.curW s)) := + fun h => c2 (specPtr_inj (Option.some.inj h)) + have hH' : sc ∈ H.scs := by rw [J.scs]; exact hC + have ht : L.mem s (scTcbA sc) ∈ Z.T := J.tcbs ▸ T.blkA.2.2.1.2.2.yt.1 sc hH' c1 + obtain ⟨xt, hxt, -, -, htP⟩ := J.sr.rest.prio _ ht + obtain ⟨xc, hxc, hmc, -⟩ := J.mcp _ hct + obtain ⟨xy, hxy, hyl⟩ := J.yieldRel.tcb _ hct + rw [hxc] at hxy + cases hxy + rw [eq_of_spec_fd (obj.Tcb.prio.spec s (L.mem s (scTcbA sc))), bind_ok, obj.Tcb.mcp.specA, bind_ok] at hd + have gp := threadGet_eqSc (σ := σ) Tcb.tcbPriority hxt + have gm := threadGet_eqSc (σ := σ) Tcb.tcbMcpriority hxc + have gy := threadGet_eqSc (σ := σ) Tcb.tcbYieldTo hxc + have hlt : L.mem s (prioA (L.mem s (scTcbA sc))) > L.mem s (mcpA (L.curW s)) ↔ + xt.tcbPriority > xc.tcbMcpriority := by + rw [GT.gt, GT.gt, BitVec.lt_def, ← htP, ← hmc, ← UScalar.lt_equiv] + by_cases c3 : L.mem s (prioA (L.mem s (scTcbA sc))) > L.mem s (mcpA (L.curW s)) + · rw [if_pos c3] at hd + refine ill hd (spec _ ?_) + rw [skip1, whenE_skipA e2, run_bind_okA (run_liftEA gp), run_bind_okA (run_liftEA gm)] + exact run_whenE_throwA (hlt.1 c3) _ _ + rw [if_neg c3, eq_of_spec_fd (obj.Tcb.yield_to.spec s (L.curW s)), bind_ok] at hd + by_cases c4 : L.mem s (yieldA (L.curW s)) = 0#u64 + · rw [if_neg (by first | simpa using c4 | simpa [UScalar.eq_equiv] using c4 | simp [c4])] at hd + obtain ⟨s2, -, hd⟩ := bind_eq_ok hd + obtain ⟨⟨s3, a2⟩, -, hd⟩ := bind_eq_ok hd + exact absurd hd noOk + rw [if_pos (by first | simpa using c4 | simpa [UScalar.eq_equiv] using c4 | simp [c4])] at hd + refine ill hd (spec _ ?_) + rw [skip1, whenE_skipA e2, run_bind_okA (run_liftEA gp), run_bind_okA (run_liftEA gm), + whenE_skipA (mt hlt.2 c3), run_bind_okA (run_liftEA gy)] + exact run_whenE_throwA (by rw [hyl]; simp [optScA, c4]) _ _ + rw [if_neg lY] at hd + refine ill hd ?_ + unfold decodeSchedContextInvocation + have n1 := mt gC.1 lC + have n2 := mt gB.1 lB + have n3 := mt gO.1 lO + have n4 := mt gU.1 lU + have n5 := mt gY.1 lY + revert n1 n2 n3 n4 n5 + cases genInvocationType (specLabelWord (fl4LabelOf mi.miLabel)) <;> intros <;> first | rfl | contradiction + +end + +end + +end Fl4ProofsA64.P3.ReplyErr diff --git a/proofs/Fl4ProofsA64/P3/Stage3DecErrTcbA.lean b/proofs/Fl4ProofsA64/P3/Stage3DecErrTcbA.lean new file mode 100644 index 000000000..1e8484d08 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3DecErrTcbA.lean @@ -0,0 +1,360 @@ +/- +P3 (aarch64, A64-P3 Lift 1b, part C): **`DecErrSpecPA` for Thread caps**. + +* `DecErrSpecPA.thread`: `DecErrSpecPA`'s statement for Thread caps at every label, unknown ones included. + +Premises: P0 `ArchTotal`; [S] `h1` (`numNodes = 1`); [P] `hCs` (the cores are below `numNodes`), [P] `ArchPlat` +(`pauth`). New module. +-/ +import Fl4ProofsA64.P3.Stage3DecErrTcbSpaceA +import Fl4ProofsA64.P3.Stage3PinVsA +import Fl4ProofsA64.P3.NoChildA +import Fl4ProofsA64.P3.Stage3StallA +import Fl4ProofsA64.P3.Stage3OffAsEA +import Fl4ProofsA64.P3.Stage3DecErrMiscA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.ReplyErr +open fl4_core_a64 Fl4ProofsA64.P0 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P2.PathObl Fl4ProofsA64.P3 + Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4Spec Fl4SpecA64 + Classical + +noncomputable section + +/-- The Thread caps. -/ +def ThreadCapA : capability.Cap → Prop + | .Thread _ => True + | _ => False + +/-- A message label has 52 bits. -/ +theorem msg_info_label_ltA {S : Type} {st : machine.Store S} {s : S} {t : U64} {info : msginfo.MessageInfo} + (h : msg.msg_info st s t = ok info) : info.label.val < 2 ^ 52 := by + unfold msg.msg_info at h + obtain ⟨w, -, h⟩ := bind_eq_ok_std h + unfold msginfo.MessageInfo.from_word_clamped at h + obtain ⟨m, hm, h⟩ := bind_eq_ok_std h + have el : info.label = m.label := by + split at h <;> (cases ok_inj_ff h; rfl) + unfold msginfo.MessageInfo.from_word at hm + obtain ⟨i, hi, hm⟩ := bind_eq_ok_std hm + obtain ⟨i1, -, hm⟩ := bind_eq_ok_std hm + obtain ⟨i2, -, hm⟩ := bind_eq_ok_std hm + obtain ⟨i3, -, hm⟩ := bind_eq_ok_std hm + obtain ⟨i4, -, hm⟩ := bind_eq_ok_std hm + obtain ⟨i5, -, hm⟩ := bind_eq_ok_std hm + cases ok_inj_ff hm + rw [el] + show i.val < 2 ^ 52 + have hs : (w >>> 12#i32 : Result U64) ⦃ r => r.val < 2 ^ 52 ⦄ := by + step as ⟨r, hr⟩ + have hw : w.val < 2 ^ 64 := by scalar_tac + simp_all [Nat.shiftRight_eq_div_pow] + omega + rw [hi, WP.spec_ok] at hs + exact hs + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {ar : machine.Arch A} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} {calling blocking donate : Bool} {cptr : U64} + +set_option maxHeartbeats 4000000 in +/-- **`DecErrSpecPA` for Thread caps** (module docstring). -/ +theorem DecErrSpecPA.thread (hA : P0.ArchTotal ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < Fl4SpecA64.fl4A64Platform.numNodes) + (h1 : Fl4SpecA64.fl4A64Platform.numNodes = 1) (hPlat : ArchPlat ar) : + DecErrSpecPA st ar Z.th s a σ calling blocking donate cptr + ThreadCapA := by + intro info buf r1 e x s' a' mi sl c excaps buffer args einfo ebuf elr eex hd hne hP hlab hsl hcr hok hab hbv + gmi ggc glb gex gmrs + have hT := hP + obtain ⟨t, hc⟩ : ∃ t, capA st s r1.slot = .Thread t := by + revert hT + generalize capA st s r1.slot = c0 + intro h + cases c0 <;> first | exact ⟨_, rfl⟩ | exact h.elim + obtain ⟨-, argsU, hargsE, hlen, hargsU⟩ := J.argsLabA einfo hbv gmi gmrs + subst hargsE + obtain ⟨_, _, _, _, _, _, Q⟩ := J.siteA einfo ebuf elr eex + have hPl := J.j.1.place + have hst : smp.remote_tcb_stall st ar s a t = ok (s, a) := (J.stallSitesA hCs h1 Q.hOS).1 t hc + have hcur : σ.curThread = specPtr (L.curW s) := J.j.1.sch.curThread + have hc2 : c = .threadCap (specPtr t) := by + have e := hcr.2 + rw [hc] at e + exact e.symm + subst hc2 + have hd0 : tcb.decode st ar s a info.label t ⟨msgLenA info buf, buf, calling, r1.slot, e⟩ = ok (.Err x, s', a') := by + have hd2 := hd + rw [hc, decodeTcb_programA, if_neg Bool.false_ne_true] at hd2 + exact hd2 + set ctx : tcb.Ctx := ⟨msgLenA info buf, buf, calling, r1.slot, e⟩ with hctx + have hAr : TcbArgsA st s ctx argsU := by + refine ⟨hlen, fun i v hv => ?_⟩ + unfold tcb.arg + simp only [cur_thread_ok, bind_ok] + exact hargsU i v hv + have hX : ExtraOKA Z.th (L.mem s) ctx.extra excaps := hok + have hth : ∀ p, exAtA ctx.extra 0 ≠ 0#u64 → capA st s (exAtA ctx.extra 0) = .Thread p → p ∈ Z.T := by + intro p h0 hp + have hm : exAtA e 0 ∈ e.val := by + have hl : e.val.length = 3 := by simpa using e.property + have : exAtA e 0 = e.val[0]'(by omega) := by + simp [exAtA, List.getD_eq_getElem?_getD, List.getElem?_eq_getElem (show 0 < e.val.length by omega)] + rw [this]; exact List.getElem_mem _ + have hx := Q.hext _ hm h0 + exact J.tcbs ▸ (J.toJStage3A.slotCapTy hx).1.2.2.2.2.2 p hp + have hM : McpRelA Z.T m (L.mem s) σ := J.mcp + have done : ∀ (l : Fl4SpecA64.Fl4Label), fl4LabelOf mi.miLabel = l → + DecEA σ x (fl4DecodeTcbInvocation (Z := Unit) l (argsU.map UScalar.bv) (.threadCap (specPtr t)) sl excaps + buffer) → + ∃ (e2 : error.SysError) (e' : SyscallError), x = .Sys e2 ∧ VsErrRelA x e' ∧ + (.error e', σ) ∈ ((fl4DecodeInvocation (Z := Unit) calling false mi.miLabel (argsU.map UScalar.bv) + (Fl4SpecA64.toBl cptr.bv) sl (.threadCap (specPtr t)) excaps buffer).run σ).1 := by + rintro l hl ⟨e', hrun, hrel⟩ + have hrel' : VsErrRelA x e' := hrel + obtain ⟨e2, hxe⟩ := sys_of_vsErrRelA hrel' + refine ⟨e2, e', hxe, hrel', ?_⟩ + have hd' : fl4DecodeInvocation (Z := Unit) calling false mi.miLabel (argsU.map UScalar.bv) + (Fl4SpecA64.toBl cptr.bv) sl (.threadCap (specPtr t)) excaps buffer = + fl4DecodeTcbInvocation (fl4LabelOf mi.miLabel) (argsU.map UScalar.bv) (.threadCap (specPtr t)) sl excaps + buffer := rfl + rw [hd', hl, hrun] + exact Set.mem_singleton _ + have lift : ∀ {β : Type} {f : β → Fl4Invocation} {d : SEMonad Unit β}, DecEA σ x d → + DecEA σ x (Fl4Spec.NonDetE.liftME f d) := by + rintro β f d ⟨e', hr, hrel⟩ + exact ⟨e', run_liftME_errA hr, hrel⟩ + have lab : ∀ k : U64, info.label = k → fl4LabelOf mi.miLabel = labNA k.val := by + intro k hk + rw [hlab, hk, fl4LabelOf_bvA] + have hnc : ∀ (i : Nat) (hi : i < excaps.length), isUtA (capA st s (exAtA ctx.extra i)) = true → + NoChildrenRelA (st := st) s σ (exAtA ctx.extra i) (excaps[i]).2 := by + intro i hi hu + have hf := srcOK_factsA hPl (hX.2.1 i hi) + obtain ⟨-, hS, hx, -, ps, hth', hy1⟩ := hf + have hAs : AbsSlotA Z.th (excaps[i]).2 := ⟨ps, hy1, Or.inl hth'⟩ + have hsO : cslotAddrA (excaps[i]).2 ∈ OS := J.j5.2.1 _ (hx ▸ hS) + change isUtA (capA st s (exAtA e i)) = true at hu + change NoChildrenRelA (st := st) s σ (exAtA e i) _ + rw [show exAtA e i = cslotAddrA (excaps[i]).2 from hx] at hu ⊢ + obtain ⟨q, b, ix, ee, hcu⟩ := isUtA_ut hu + exact noChildren_of_adjA hPl J.sys.ent.os (J.j.1.cdt.tree.grow J.j5.2.1 (fun x hx hn => J.j5.2.2 x hx hn)) + J.j.1.cdt hAs hsO hcu (J.sys.sysl.sys.stz.1.2.1 _ hsO hu) + by_cases hknown : tcbLabelNumberA info.label.val + swap + · rw [decodeTcb_unknown_programA (msg_info_label_ltA einfo) hknown hst] at hd0 + cases tcbErr_injA hd0 + have hu : ¬ tcbLabelNumberA mi.miLabel.toNat := by rw [hlab]; exact hknown + refine ⟨.IllegalOperation, .illegalOperation, rfl, Or.inl trivial, ?_⟩ + rw [decodeTcb_unknown_specA hu] + exact (run_throwErrorD (α := Fl4Invocation) SyscallError.illegalOperation σ).symm ▸ Set.mem_singleton _ + have is : ∀ k : U64, info.label.val = k.val → info.label = k := fun k h => UScalar.eq_of_val_eq h + have cases11 : info.label.val = 2 ∨ info.label.val = 3 ∨ info.label.val = 4 ∨ info.label.val = 6 ∨ + info.label.val = 7 ∨ info.label.val = 9 ∨ info.label.val = 12 ∨ info.label.val = 13 ∨ + info.label.val = 16 ∨ info.label.val = 17 ∨ info.label.val = 20 ∨ info.label.val = 8 ∨ + info.label.val = 14 ∨ info.label.val = 15 ∨ info.label.val = 18 ∨ info.label.val = 19 ∨ + info.label.val = 73 ∨ info.label.val = 72 ∨ + info.label.val = 10 ∨ info.label.val = 11 ∨ info.label.val = 5 := by + unfold tcbLabelNumberA at hknown + omega + rcases cases11 with h | h | h | h | h | h | h | h | h | h | h | h | h | h | h | h | h | h | h | h | h + · have e0 := is fl4_abi.label.TCBReadRegisters (by rw [fl4_abi.label.TCBReadRegisters]; exact h) + rw [e0] at hd0 + refine done _ (lab _ e0) ?_ + rw [fl4_abi.label.TCBReadRegisters] + refine lift (d := decodeTcbInvocation _ _ _ _ _) ?_ + rw [show decodeTcbInvocation (Z := Unit) (specLabelWord (labNA (2#u64 : U64).val)) (argsU.map UScalar.bv) + (.threadCap (specPtr t)) sl excaps = decodeReadRegisters (argsU.map UScalar.bv) (.threadCap (specPtr t)) by + unfold decodeTcbInvocation; rw [gen_specWordA]; rfl] + exact tcbReadRegisters.decErrA hst hAr hcur hd0 + · have e0 := is fl4_abi.label.TCBWriteRegisters (by rw [fl4_abi.label.TCBWriteRegisters]; exact h) + rw [e0] at hd0 + refine done _ (lab _ e0) ?_ + rw [fl4_abi.label.TCBWriteRegisters] + refine lift (d := decodeTcbInvocation _ _ _ _ _) ?_ + rw [show decodeTcbInvocation (Z := Unit) (specLabelWord (labNA (3#u64 : U64).val)) (argsU.map UScalar.bv) + (.threadCap (specPtr t)) sl excaps = decodeWriteRegisters (argsU.map UScalar.bv) (.threadCap (specPtr t)) by + unfold decodeTcbInvocation; rw [gen_specWordA]; rfl] + exact tcbWriteRegisters.decErrA hst hAr hcur hd0 + · have e0 := is fl4_abi.label.TCBCopyRegisters (by rw [fl4_abi.label.TCBCopyRegisters]; exact h) + rw [e0] at hd0 + refine done _ (lab _ e0) ?_ + rw [fl4_abi.label.TCBCopyRegisters] + refine lift (d := decodeTcbInvocation _ _ _ _ _) ?_ + rw [show decodeTcbInvocation (Z := Unit) (specLabelWord (labNA (4#u64 : U64).val)) (argsU.map UScalar.bv) + (.threadCap (specPtr t)) sl excaps = decodeCopyRegisters (argsU.map UScalar.bv) (.threadCap (specPtr t)) + (excaps.map Prod.fst) by + unfold decodeTcbInvocation; rw [gen_specWordA]; rfl] + exact tcbCopyRegisters.decErrA hA hne hst hAr hPl hX hd0 + · have e0 := is fl4_abi.label.TCBSetPriority (by rw [fl4_abi.label.TCBSetPriority]; exact h) + rw [e0, tcbSetPriority_programA hst] at hd0 + refine done _ (lab _ e0) ?_ + rw [fl4_abi.label.TCBSetPriority] + refine lift (d := decodeTcbInvocation _ _ _ _ _) ?_ + rw [show decodeTcbInvocation (Z := Unit) (specLabelWord (labNA (6#u64 : U64).val)) (argsU.map UScalar.bv) + (.threadCap (specPtr t)) sl excaps = decodeSetPriority (argsU.map UScalar.bv) (.threadCap (specPtr t)) sl + excaps by + unfold decodeTcbInvocation; rw [gen_specWordA]; rfl] + exact (tcbPrio.decErrA hA hAr hPl hX hth hM true sl _ hd0 hne).1 + · have e0 := is fl4_abi.label.TCBSetMCPriority (by rw [fl4_abi.label.TCBSetMCPriority]; exact h) + rw [e0, tcbSetMCPriority_programA hst] at hd0 + refine done _ (lab _ e0) ?_ + rw [fl4_abi.label.TCBSetMCPriority] + refine lift (d := decodeTcbInvocation _ _ _ _ _) ?_ + rw [show decodeTcbInvocation (Z := Unit) (specLabelWord (labNA (7#u64 : U64).val)) (argsU.map UScalar.bv) + (.threadCap (specPtr t)) sl excaps = decodeSetMcpriority (argsU.map UScalar.bv) (.threadCap (specPtr t)) sl + excaps by + unfold decodeTcbInvocation; rw [gen_specWordA]; rfl] + exact (tcbPrio.decErrA hA hAr hPl hX hth hM false sl _ hd0 hne).2 + · have e0 := is fl4_abi.label.TCBSetTimeoutEndpoint (by rw [fl4_abi.label.TCBSetTimeoutEndpoint]; exact h) + rw [e0, tcbSetTimeoutEndpoint_programA hst] at hd0 + refine done _ (lab _ e0) ?_ + rw [fl4_abi.label.TCBSetTimeoutEndpoint] + refine lift (d := decodeTcbInvocation _ _ _ _ _) ?_ + rw [show decodeTcbInvocation (Z := Unit) (specLabelWord (labNA (9#u64 : U64).val)) (argsU.map UScalar.bv) + (.threadCap (specPtr t)) sl excaps = decodeSetTimeoutEp (.threadCap (specPtr t)) sl excaps by + unfold decodeTcbInvocation; rw [gen_specWordA]; rfl] + exact tcbTimeout.decErrA hA hPl hX sl _ hd0 hne + · have e0 := is fl4_abi.label.TCBSuspend (by rw [fl4_abi.label.TCBSuspend]; exact h) + rw [e0] at hd0 + exact (tcbSuspend.decErrA hst hd0).elim + · have e0 := is fl4_abi.label.TCBResume (by rw [fl4_abi.label.TCBResume]; exact h) + rw [e0] at hd0 + exact (tcbResume.decErrA hst hd0).elim + · have e0 := is fl4_abi.label.TCBSetTLSBase (by rw [fl4_abi.label.TCBSetTLSBase]; exact h) + rw [e0] at hd0 + refine done _ (lab _ e0) ?_ + rw [fl4_abi.label.TCBSetTLSBase] + refine lift (d := decodeTcbInvocation _ _ _ _ _) ?_ + rw [show decodeTcbInvocation (Z := Unit) (specLabelWord (labNA (16#u64 : U64).val)) (argsU.map UScalar.bv) + (.threadCap (specPtr t)) sl excaps = decodeSetTlsBase (argsU.map UScalar.bv) (.threadCap (specPtr t)) by + unfold decodeTcbInvocation; rw [gen_specWordA]; rfl] + exact tcbSetTLSBase.decErrA hst hne hAr hd0 + · have e0 := is fl4_abi.label.TCBSetFlags (by rw [fl4_abi.label.TCBSetFlags]; exact h) + rw [e0] at hd0 + refine done _ (lab _ e0) ?_ + rw [fl4_abi.label.TCBSetFlags] + exact lift (tcbSetFlags.decErrA hA hne hst hAr hd0) + · have e0 := is fl4_abi.label.TCBSetTimer (by rw [fl4_abi.label.TCBSetTimer]; exact h) + rw [e0] at hd0 + refine done _ (lab _ e0) ?_ + rw [fl4_abi.label.TCBSetTimer] + exact lift (tcbSetTimer.decErrA hst hAr hd0) + · have htT : t ∈ Z.T := J.tcbs ▸ (J.toJStage3A.slotCapTy Q.hOS).1.2.2.2.2.2 t hc + have e0 := is fl4_abi.label.TCBSetSchedParams (by rw [fl4_abi.label.TCBSetSchedParams]; exact h) + rw [e0] at hd0 + refine done _ (lab _ e0) ?_ + rw [fl4_abi.label.TCBSetSchedParams] + refine lift (d := decodeTcbInvocation _ _ _ _ _) ?_ + rw [show decodeTcbInvocation (Z := Unit) (specLabelWord (labNA (8#u64 : U64).val)) (argsU.map UScalar.bv) + (.threadCap (specPtr t)) sl excaps = decodeSetSchedParams (argsU.map UScalar.bv) (.threadCap (specPtr t)) sl + excaps by + unfold decodeTcbInvocation; rw [gen_specWordA]; rfl] + obtain ⟨u, hk, hks⟩ := J.sr.sch.tcb t htT + obtain ⟨u', hk', hr⟩ := J.ep.tcb t htT + obtain rfl : u = u' := Fl4SpecA64.KernelObject.tcb.inj (Option.some.inj (hk.symm.trans hk')) + have hbl := isBlockedWordA (st := st) (s := s) (t := t) (ts := u.tcbState) (by rw [← Notif.tsCode_eqA]; exact hr.st) + refine tcbSchedParams.decErrA hA hne hst hAr hPl hX hth hM hk hks hbl hcur ?_ sl hd0 + intro p b h0 hcap + have hm : exAtA e 1 ∈ e.val := by + have hl : e.val.length = 3 := by simpa using e.property + have : exAtA e 1 = e.val[1]'(by omega) := by + simp [exAtA, List.getD_eq_getElem?_getD, List.getElem?_eq_getElem (show 1 < e.val.length by omega)] + rw [this]; exact List.getElem_mem _ + have hx := Q.hext _ hm h0 + have hH : H p = some (.sc b.val) := (J.capTy _ hx).2.2.2.1 p b hcap + have hp : p ∈ Z.C := by + rw [← J.scs]; exact ⟨_, hH⟩ + exact tcbScViewA_of J.sr.toSchedSRN hp (J.scNZ p hp) + ((J.toJStage3A.toTA J.sact J.noSwitch).ciSc _ hx p b hcap) + · have htT : t ∈ Z.T := J.tcbs ▸ (J.toJStage3A.slotCapTy Q.hOS).1.2.2.2.2.2 t hc + have e0 := is fl4_abi.label.TCBBindNotification (by rw [fl4_abi.label.TCBBindNotification]; exact h) + rw [e0] at hd0 + refine done _ (lab _ e0) ?_ + rw [fl4_abi.label.TCBBindNotification] + refine lift (d := decodeTcbInvocation _ _ _ _ _) ?_ + rw [show decodeTcbInvocation (Z := Unit) (specLabelWord (labNA (14#u64 : U64).val)) (argsU.map UScalar.bv) + (.threadCap (specPtr t)) sl excaps = decodeBindNotification (.threadCap (specPtr t)) excaps by + unfold decodeTcbInvocation; rw [gen_specWordA]; rfl] + obtain ⟨y, hk, hr⟩ := J.ep.tcb t htT + refine tcbBind.decErrA hst hPl hX hk hr.bound J.ntfn ?_ hd0 + intro p b sd rc h0 hcap + have hm : exAtA e 0 ∈ e.val := by + have hl : e.val.length = 3 := by simpa using e.property + have : exAtA e 0 = e.val[0]'(by omega) := by + simp [exAtA, List.getD_eq_getElem?_getD, List.getElem?_eq_getElem (show 0 < e.val.length by omega)] + rw [this]; exact List.getElem_mem _ + exact J.ipcHeapCov.ntfn p ((J.capTy _ (Q.hext _ hm h0)).2.1 p b sd rc hcap) + · have htT : t ∈ Z.T := J.tcbs ▸ (J.toJStage3A.slotCapTy Q.hOS).1.2.2.2.2.2 t hc + have e0 := is fl4_abi.label.TCBUnbindNotification (by rw [fl4_abi.label.TCBUnbindNotification]; exact h) + rw [e0] at hd0 + refine done _ (lab _ e0) ?_ + rw [fl4_abi.label.TCBUnbindNotification] + refine lift (d := decodeTcbInvocation _ _ _ _ _) ?_ + rw [show decodeTcbInvocation (Z := Unit) (specLabelWord (labNA (15#u64 : U64).val)) (argsU.map UScalar.bv) + (.threadCap (specPtr t)) sl excaps = decodeUnbindNotification (.threadCap (specPtr t)) by + unfold decodeTcbInvocation; rw [gen_specWordA]; rfl] + obtain ⟨y, hk, hr⟩ := J.ep.tcb t htT + exact tcbUnbind.decErrA hst hk hr.bound hd0 + · have e0 := is fl4_abi.label.TCBReadFPU (by rw [fl4_abi.label.TCBReadFPU]; exact h) + rw [e0, tcbReadFPU_programA hst] at hd0 + refine done _ (lab _ e0) ?_ + rw [fl4_abi.label.TCBReadFPU] + exact lift (tcbFpu.decErrA true hAr hPl hX _ hd0) + · have e0 := is fl4_abi.label.TCBWriteFPU (by rw [fl4_abi.label.TCBWriteFPU]; exact h) + rw [e0, tcbWriteFPU_programA hst] at hd0 + refine done _ (lab _ e0) ?_ + rw [fl4_abi.label.TCBWriteFPU] + exact lift (tcbFpu.decErrA false hAr hPl hX _ hd0) + · have e0 := is fl4_abi.label.TCBSetHighVSpace (by rw [fl4_abi.label.TCBSetHighVSpace]; exact h) + rw [e0] at hd0 + refine done _ (lab _ e0) ?_ + rw [fl4_abi.label.TCBSetHighVSpace] + exact lift (tcbHigh.decErrA hA hne hst hPl hX J.j.1.vs + (fun r hr => let ⟨es, hk, _⟩ := J.j.1.ext.s2root r hr; ⟨es, hk⟩) _ _ hd0) + · have e0 := is fl4_abi.label.TCBSetPACKeys (by rw [fl4_abi.label.TCBSetPACKeys]; exact h) + rw [e0] at hd0 + refine done _ (lab _ e0) ?_ + rw [fl4_abi.label.TCBSetPACKeys] + have hT : L.curW s ∈ Z.T := J.sr.curT + have hRV : RegViewA (L.mem s) σ (L.curW s) := fun xs h => (J.ep.at_tcb hT h).regView + obtain ⟨xs, hks, -⟩ := J.j.1.curViewA hRV + refine lift (tcbPac.decErrA hst hAr hPlat buffer (fun v hv => ?_) hd0) + exact fl4Arg0_runA hcur hks buffer hbv.al mi (by rw [← hcur]; exact gmrs) (by rw [List.getElem?_map, hv]; rfl) + · have e0 := is fl4_abi.label.TCBSetIPCBuffer (by rw [fl4_abi.label.TCBSetIPCBuffer]; exact h) + rw [e0] at hd0 + refine done _ (lab _ e0) ?_ + rw [fl4_abi.label.TCBSetIPCBuffer] + refine lift (d := decodeTcbInvocation _ _ _ _ _) ?_ + rw [show decodeTcbInvocation (Z := Unit) (specLabelWord (labNA (10#u64 : U64).val)) (argsU.map UScalar.bv) + (.threadCap (specPtr t)) sl excaps = decodeSetIpcBuffer (argsU.map UScalar.bv) (.threadCap (specPtr t)) sl + excaps by + unfold decodeTcbInvocation; rw [gen_specWordA]; rfl] + refine tcbBuf.decErrA hA hne hst hAr hPl hX (fun y ys hy hu => ?_) _ sl hd0 + have hy0 : 0 < excaps.length := by rw [hy]; simp + have e0' : excaps[0] = y := by simp [hy] + exact e0' ▸ hnc 0 hy0 hu + · have htT : t ∈ Z.T := J.tcbs ▸ (J.toJStage3A.slotCapTy Q.hOS).1.2.2.2.2.2 t hc + have e0 := is fl4_abi.label.TCBSetSpace (by rw [fl4_abi.label.TCBSetSpace]; exact h) + rw [e0] at hd0 + refine done _ (lab _ e0) ?_ + rw [fl4_abi.label.TCBSetSpace] + refine lift (f := Fl4Invocation.invokeTCB) (d := fl4DecodeSetSpace _ _ _ _) ?_ + exact tcbSpace.decErrA hA hne hst hAr hPl hX J.finStA ((J.j.1.thT _).2 htT) hnc sl hd0 + · have htT : t ∈ Z.T := J.tcbs ▸ (J.toJStage3A.slotCapTy Q.hOS).1.2.2.2.2.2 t hc + have e0 := is fl4_abi.label.TCBConfigure (by rw [fl4_abi.label.TCBConfigure]; exact h) + rw [e0] at hd0 + refine done _ (lab _ e0) ?_ + rw [fl4_abi.label.TCBConfigure] + refine lift (f := Fl4Invocation.invokeTCB) (d := fl4DecodeTcbConfigure _ _ _ _) ?_ + exact tcbConfig.decErrA hA hne hst hAr hPl hX J.finStA ((J.j.1.thT _).2 htT) hnc sl hd0 + +end + +end + +end Fl4ProofsA64.P3.ReplyErr diff --git a/proofs/Fl4ProofsA64/P3/Stage3DecErrTcbAuthA.lean b/proofs/Fl4ProofsA64/P3/Stage3DecErrTcbAuthA.lean new file mode 100644 index 000000000..52cbce8d1 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3DecErrTcbAuthA.lean @@ -0,0 +1,226 @@ +/- +P3 (aarch64, A64-P3 Lift 1b, part C): **decoder errors of the Thread-cap labels, authority family** (SetPriority, +SetMCPriority, SetTimeoutEndpoint). Every FL4 `Err` precedes the caller's Restart; after it, +`thread_control_sched` / `thread_control_caps` err only by `Preempted`. + +[S] `hst` the target's stall is quiet. [J] `hth` a Thread cap of an extra slot names a TCB of `T`, `hM` the MCP relation +(both the record's). No other named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3DecErrTcbRegsA +import Fl4ProofsA64.P3.TcbInvokeTimeoutA +import Fl4ProofsA64.P3.TcbInvokeMcpA +import Fl4ProofsA64.P3.Stage3ErrKeep2A +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.ReplyErr +open fl4_core_a64 Fl4ProofsA64.P0 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P2.PathObl Fl4ProofsA64.P3 + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {mc : Fl4SpecA64.McState} {s s' : S} {a a' : A} {t : U64} {ctx : tcb.Ctx} {x : error.Exc} {σ : Fl4SpecA64.State Unit} + {argsU : List U64} {th : TypedHeapA} {kb : U64} {excaps : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)} {T : Set U64} + +theorem tcbPrio.decErrA (hA : ArchTotal ar) (hAr : TcbArgsA st s ctx argsU) (hP : PlaceA th kb) + (hX : ExtraOKA th (L.mem s) ctx.extra excaps) + (hth : ∀ p, exAtA ctx.extra 0 ≠ 0#u64 → capA st s (exAtA ctx.extra 0) = .Thread p → p ∈ T) (hM : McpRelA T mc (L.mem s) σ) + (prio : Bool) (slot : Fl4SpecA64.CslotPtr) (c : Fl4SpecA64.Cap) + (h : tcb.decode_set_priority st ar s a t ctx prio = ok (.Err x, s', a')) (hne : x ≠ .Preempted) : + DecEA σ x (decodeSetPriority (Z := Unit) (argsU.map UScalar.bv) c slot excaps) ∧ + DecEA σ x (decodeSetMcpriority (Z := Unit) (argsU.map UScalar.bv) c slot excaps) := by + obtain ⟨hlen, hargs⟩ := hAr + have he := tcbExtraWL_okA (L := L) (ctx := ctx) hP hX 0#usize (by simp) 1#u64 + by_cases hc : (0#usize).val < excaps.length ∧ 1#u64 ≤ ctx.length + swap + · rw [if_neg hc] at he + unfold tcb.decode_set_priority at h + simp only [he, bind_ok, core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, Std.bind_tc_ok] at h + cases tcbErr_injA h + have hs : (argsU.map UScalar.bv).length = 0 ∨ excaps.length = 0 := by + by_cases h0 : excaps.length = 0 + · exact Or.inr h0 + · refine Or.inl ?_ + rw [List.length_map, ← hlen] + have h1 : ¬ 1#u64 ≤ ctx.length := fun c => hc ⟨by simp; omega, c⟩ + scalar_tac + constructor <;> refine ⟨.truncatedMessage, ?_, Or.inl trivial⟩ <;> + [unfold decodeSetPriority; unfold decodeSetMcpriority] <;> + rw [show Fl4Spec.NonDetE.whenE (σ := Fl4SpecA64.State Unit) + ((argsU.map UScalar.bv).length = 0 ∨ excaps.length = 0) + (Fl4Spec.NonDetE.throwError Fl4SpecA64.SyscallError.truncatedMessage) = + Fl4Spec.NonDetE.throwError .truncatedMessage from if_pos hs] <;> + exact run_bind_errorD (run_throwErrorD _ σ) _ + rw [if_pos hc] at he + obtain ⟨h0, h1⟩ := hc + simp only [UScalar.ofNatCore_val_eq] at he h0 + match excaps, hX, h0, argsU, hargs, (show 1 ≤ argsU.length by rw [← hlen]; scalar_tac) with + | y :: ys, hX, _, p :: rest, hargs, _ => + have hp := hargs 0#u64 p (by simp) + have hcr := (srcOK_factsA hP (hX.2.1 0 (by simp))).2.2.2.1 + rw [capAtA_eq (st := st)] at hcr + simp only [List.getElem_cons_zero] at hcr + have hcap := capA_eq (st := st) s (exAtA ctx.extra 0) + have hn : ¬ (((p :: rest).map UScalar.bv).length = 0 ∨ (y :: ys).length = 0) := by simp + by_cases hk : ∃ q, capA st s (exAtA ctx.extra 0) = .Thread q + swap + · have hk' : ∀ q, capA st s (exAtA ctx.extra 0) ≠ .Thread q := fun q e => hk ⟨q, e⟩ + have hta : tcb.thread_arg st s (exAtA ctx.extra 0) 1#u64 = ok (.Err (.Sys (.InvalidCapability 1#u64))) := by + unfold tcb.thread_arg + rw [hcap] + simp only [bind_ok] + split <;> first | rfl | exact absurd ‹_› (hk' _) + unfold tcb.decode_set_priority at h + simp only [he, hp, hta, bind_ok, core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, Std.bind_tc_ok] at h + cases tcbErr_injA h + have hy := notThreadCapA hcr.2 hk' + obtain ⟨yc, ysl⟩ := y + constructor <;> refine ⟨.invalidCapability 1, ?_, Or.inr ⟨1#u64, rfl, rfl⟩⟩ <;> + [unfold decodeSetPriority; unfold decodeSetMcpriority] <;> + rw [show Fl4Spec.NonDetE.whenE (σ := Fl4SpecA64.State Unit) + (((p :: rest).map UScalar.bv).length = 0 ∨ ((yc, ysl) :: ys).length = 0) + (Fl4Spec.NonDetE.throwError Fl4SpecA64.SyscallError.truncatedMessage) = pure () from if_neg hn, + ndE_pure_bindD] <;> + simp only [Fl4Spec.NonDetE.returnOk, ndE_pure_bindD, List.getElem?_cons_zero, the, Option.getD_some] <;> + (cases yc <;> first + | exact absurd rfl (hy _) + | exact run_bind_errorD (run_throwErrorD _ σ) _) + · obtain ⟨q, hq⟩ := hk + have hta : tcb.thread_arg st s (exAtA ctx.extra 0) 1#u64 = ok (.Ok q) := by + unfold tcb.thread_arg + simp only [hcap, hq, bind_ok] + by_cases hlt : L.mem s (mcpA q) < p + swap + · unfold tcb.decode_set_priority at h + simp only [he, hp, hta, bind_ok, core.result.Result.Insts.CoreOpsTry.branch, tcb.check_prio, + Fl4ProofsA64.P3.Sched.obj.Tcb.mcp.specA, if_neg (show ¬ p > L.mem s (mcpA q) from hlt)] at h + obtain ⟨s1, -, h⟩ := bind_eq_ok_std h + split at h + all_goals + first + | exact absurd (by + have K' := tcb.thread_control_sched.preA (st := st) (ar := ar) hA s1 a t 0#u64 + (capability.Cap.Null, 0#u64) 0#u64 p 0#u64 tcb.SCHED_UPDATE_PRIO + rw [h, WP.spec_ok] at K' + exact K' x rfl) hne + | exact absurd (by + have K' := tcb.thread_control_sched.preA (st := st) (ar := ar) hA s1 a t 0#u64 + (capability.Cap.Null, 0#u64) p 0#u64 0#u64 tcb.SCHED_UPDATE_MCP + rw [h, WP.spec_ok] at K' + exact K' x rfl) hne + rw [tcbPriority_rangeA prio (by exact he) hp hta hlt] at h + cases tcbErr_injA h + have hy : y.1 = .threadCap (specPtr q) := by + have e := hcr.2 + rw [hq] at e + exact e.symm + obtain ⟨yc, ysl⟩ := y + simp only at hy + subst hy + obtain ⟨z, hz, hmcp, -⟩ := hM q (hth q (srcOK_factsA hP (hX.2.1 0 (by simp))).1 hq) + have hgt : p.bv > BitVec.setWidth 64 z.tcbMcpriority := by + show BitVec.setWidth 64 z.tcbMcpriority < p.bv + rw [BitVec.lt_def, BitVec.toNat_setWidth_of_le (by decide), ← hmcp] + exact hlt + have hchk : (checkPrio (Z := Unit) p.bv (specPtr q)).run σ = + ({(.error (.rangeError 0 (BitVec.setWidth 64 z.tcbMcpriority)), σ)}, False) := by + unfold checkPrio + rw [run_bind_okD (run_liftE_stD (threadGet_eqSc Tcb.tcbMcpriority hz))] + rw [show Fl4Spec.NonDetE.whenE (σ := Fl4SpecA64.State Unit) (p.bv > ucast 64 z.tcbMcpriority) + (Fl4Spec.NonDetE.throwError (Fl4SpecA64.SyscallError.rangeError 0 (ucast 64 z.tcbMcpriority))) = + Fl4Spec.NonDetE.throwError (.rangeError 0 (ucast 64 z.tcbMcpriority)) from if_pos hgt] + exact run_throwErrorD _ σ + have hrel : SysErrRelA (.Sys (.RangeError 0#u64 (L.mem s (mcpA q)))) + (.rangeError 0 (BitVec.setWidth 64 z.tcbMcpriority)) := by + refine ⟨rfl, ?_⟩ + apply BitVec.eq_of_toNat_eq + rw [BitVec.toNat_setWidth_of_le (by decide), ← hmcp] + rfl + constructor <;> refine ⟨_, ?_, Or.inl hrel⟩ <;> + [unfold decodeSetPriority; unfold decodeSetMcpriority] <;> + rw [show Fl4Spec.NonDetE.whenE (σ := Fl4SpecA64.State Unit) + (((p :: rest).map UScalar.bv).length = 0 ∨ ((Fl4SpecA64.Cap.threadCap (specPtr q), ysl) :: ys).length = 0) + (Fl4Spec.NonDetE.throwError Fl4SpecA64.SyscallError.truncatedMessage) = pure () from if_neg hn, + ndE_pure_bindD] <;> + simp only [Fl4Spec.NonDetE.returnOk, ndE_pure_bindD, List.getElem?_cons_zero, the, Option.getD_some, + List.map_cons] <;> + exact run_bind_errorD hchk _ + +/-- `CapEqA` keeps `validFaultHandler`. -/ +theorem validFH_eqA {c : capability.Cap} {y : Fl4SpecA64.Cap} (h : CapEqA c y) : + validFaultHandler y ↔ validFaultHandler (absCapA c) := by + revert h + unfold CapEqA + split + · intro _; simp [validFaultHandler, absCapA, isEpCap] + · intro e; rw [e] + +theorem tcbTimeout.decErrA (hA : ArchTotal ar) (hP : PlaceA th kb) + (hX : ExtraOKA th (L.mem s) ctx.extra excaps) (slot : Fl4SpecA64.CslotPtr) (c : Fl4SpecA64.Cap) + (h : tcb.decode_set_timeout_endpoint st ar s a t ctx = ok (.Err x, s', a')) (hne : x ≠ .Preempted) : + DecEA σ x (decodeSetTimeoutEp (Z := Unit) c slot excaps) := by + have he := tcbExtraWL_okA (L := L) (ctx := ctx) hP hX 0#usize (by simp) 0#u64 + by_cases h0 : (0#usize).val < excaps.length + swap + · rw [if_neg (fun c => h0 c.1)] at he + unfold tcb.decode_set_timeout_endpoint at h + simp only [he, bind_ok, core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, Std.bind_tc_ok] at h + cases tcbErr_injA h + refine ⟨.truncatedMessage, ?_, Or.inl trivial⟩ + unfold decodeSetTimeoutEp + rw [show Fl4Spec.NonDetE.whenE (σ := Fl4SpecA64.State Unit) (excaps.length < 1) + (Fl4Spec.NonDetE.throwError Fl4SpecA64.SyscallError.truncatedMessage) = + Fl4Spec.NonDetE.throwError .truncatedMessage from if_pos (by simp at h0; simp [h0])] + exact run_bind_errorD (run_throwErrorD _ σ) _ + rw [if_pos ⟨h0, by scalar_tac⟩] at he + simp only [UScalar.ofNatCore_val_eq] at he h0 + match excaps, hX, h0 with + | y :: ys, hX, _ => + have hcr := (srcOK_factsA hP (hX.2.1 0 (by simp))).2.2.2.1 + rw [capAtA_eq (st := st)] at hcr + simp only [List.getElem_cons_zero] at hcr + have hcap := capA_eq (st := st) s (exAtA ctx.extra 0) + unfold tcb.decode_set_timeout_endpoint at h + simp only [he, hcap, bind_ok, core.result.Result.Insts.CoreOpsTry.branch] at h + obtain ⟨b, hb, h⟩ := bind_eq_ok_std h + cases b with + | true => + simp only [ite_true] at h + obtain ⟨s1, -, h⟩ := bind_eq_ok_std h + have K := tcb.thread_control_caps.preA (st := st) (ar := ar) hA s1 a t ctx.slot + (capability.Cap.Null, 0#u64) (capA st s (exAtA ctx.extra 0), exAtA ctx.extra 0) (capability.Cap.Null, 0#u64) + (capability.Cap.Null, 0#u64) 0#u64 (capability.Cap.Null, 0#u64) tcb.UPDATE_TIMEOUT + rw [h, WP.spec_ok] at K + exact absurd (K x rfl) hne + | false => + simp only [Bool.false_eq_true, ite_false, error.invalid_cap, bind_ok] at h + cases tcbErr_injA h + have hv : ¬ validFaultHandler y.1 := by + rw [validFH_eqA hcr.2, ← tcbValidFaultHandlerA _ hcr.1, hb] + intro e + exact Bool.noConfusion (ok_inj_ff e) + refine ⟨.invalidCapability 1, ?_, Or.inr ⟨1#u64, rfl, rfl⟩⟩ + unfold decodeSetTimeoutEp + rw [show Fl4Spec.NonDetE.whenE (σ := Fl4SpecA64.State Unit) ((y :: ys).length < 1) + (Fl4Spec.NonDetE.throwError Fl4SpecA64.SyscallError.truncatedMessage) = pure () from if_neg (by simp), + ndE_pure_bindD] + simp only [Fl4Spec.NonDetE.returnOk, ndE_pure_bindD, List.getElem?_cons_zero, the, Option.getD_some] + refine run_bind_errorD ?_ _ + unfold checkHandlerEp + rw [show Fl4Spec.NonDetE.unlessE (σ := Fl4SpecA64.State Unit) (validFaultHandler y.1) + (Fl4Spec.NonDetE.throwError (Fl4SpecA64.SyscallError.invalidCapability 1)) = + Fl4Spec.NonDetE.throwError (.invalidCapability 1) from if_neg hv] + exact run_bind_errorD (run_throwErrorD _ σ) _ + +end + +end + +end Fl4ProofsA64.P3.ReplyErr diff --git a/proofs/Fl4ProofsA64/P3/Stage3DecErrTcbBufA.lean b/proofs/Fl4ProofsA64/P3/Stage3DecErrTcbBufA.lean new file mode 100644 index 000000000..0cc01d77e --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3DecErrTcbBufA.lean @@ -0,0 +1,252 @@ +/- +P3 (aarch64, A64-P3 Lift 1b, part C3): **decoder errors of SetIPCBuffer**. Every FL4 `Err` other than `Preempted` +precedes the caller's Restart. + +* `bufDerive.decErrA`: `derive_for_buffer`'s errors against `deriveCap` and `checkValidIpcBuffer` (shared with + Configure). + +P0 `ArchTotal`. [S] `hst`. `hnc`: `ensure_no_children` at an untyped buffer source. New module. +-/ +import Fl4ProofsA64.P3.Stage3DecErrTcbPacA +import Fl4ProofsA64.P3.IpcXferDeriveA +import Fl4ProofsA64.P3.TcbInvokeBufferCheckA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.ReplyErr +open fl4_core_a64 Fl4ProofsA64.P0 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P2.PathObl Fl4ProofsA64.P3 + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Endp Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {s s' : S} {a a' : A} {t : U64} {ctx : tcb.Ctx} {x : error.Exc} {σ : Fl4SpecA64.State Unit} + {th : TypedHeapA} {kb : U64} {excaps : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)} {argsU : List U64} + +/-- A derived frame cap is native and unmapped. -/ +theorem derivedFrameA {slot : obj.Slot} {c : capability.Cap} {p rights asid va : U64} {sz : capability.FrameSize} + {dev m e : Bool} (h : objects.derive_cap st s slot c = ok (.Ok (.Frame p sz rights dev m e asid va))) : + m = false ∧ e = false ∧ asid = 0#u64 ∧ va = 0#u64 := by + unfold objects.derive_cap at h + cases c + case Frame => + have := ok_inj_ff h + simp only [core.result.Result.Ok.injEq, capability.Cap.Frame.injEq] at this + obtain ⟨-, -, -, -, h1, h2, h3, h4⟩ := this + exact ⟨h1.symm, h2.symm, h3.symm, h4.symm⟩ + case Untyped => + dsimp only at h + obtain ⟨r, -, h⟩ := bind_eq_ok_std h + cases r with + | Ok v => + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok] at h + exact absurd (ok_inj_ff h) (by simp) + | Err v => + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, Std.bind_tc_ok] at h + exact absurd (ok_inj_ff h) (by simp) + all_goals + first + | exact absurd (ok_inj_ff h) (by simp) + | (dsimp only at h + split at h <;> exact absurd (ok_inj_ff h) (by simp)) + +/-- `check_valid_ipc_buffer`'s errors of a derived cap against the spec's. -/ +theorem bufCheck_errA {c : capability.Cap} {y : Fl4SpecA64.Cap} {v : U64} (hw : CapWfA c) (he : CapEqA c y) + (hn : ∀ p sz rights dev m e asid va, c = .Frame p sz rights dev m e asid va → + m = false ∧ e = false ∧ asid = 0#u64 ∧ va = 0#u64) + (h : vspace.check_valid_ipc_buffer v c = ok (.Err x)) : + ∃ e', (checkValidIpcBuffer (Z := Unit) v.bv y).run σ = ({(.error e', σ)}, False) ∧ VsErrRelA x e' := by + by_cases hF : ∃ p sz rights dev m e asid va, c = .Frame p sz rights dev m e asid va + · obtain ⟨p, sz, rights, dev, m, e, asid, va, rfl⟩ := hF + obtain ⟨rfl, rfl, rfl, rfl⟩ := hn _ _ _ _ _ _ _ _ rfl + have hy : y = absCapA (.Frame p sz rights dev false false 0#u64 0#u64) := by + unfold CapEqA at he + exact he.symm + subst hy + rw [tcbBufferCheck_nativeA] at h + have h := ok_inj_ff h + rw [tcbBufferCheck_specA] + cases dev + · simp only [Bool.false_eq_true, ite_false] at h ⊢ + by_cases ha : isAligned v.bv msgAlignBits + · rw [if_pos ha] at h + exact absurd h (by simp) + · rw [if_neg ha] at h ⊢ + cases h + exact ⟨_, rfl, Or.inl (by exact trivial)⟩ + · simp only [ite_true] at h ⊢ + cases h + exact ⟨_, rfl, Or.inl (by exact trivial)⟩ + · have hx : x = .Sys .IllegalOperation := by + unfold vspace.check_valid_ipc_buffer at h + cases c + case Frame => exact absurd ⟨_, _, _, _, _, _, _, _, rfl⟩ hF + all_goals + simp only [error.illegal] at h + exact (core.result.Result.Err.inj (ok_inj_ff h)).symm + have hy : ∀ r R z m, y ≠ .archObjectCap (.frameCap r R z false m) := by + intro r R z m hc + subst hc + have h' : absCapA c = .archObjectCap (.frameCap r R z false m) := by + unfold CapEqA at he + split at he <;> first | exact he | contradiction | (rename_i heq; cases heq; done) | cases he | simp_all + cases c + case Frame => exact hF ⟨_, _, _, _, _, _, _, _, rfl⟩ + case PageDirectory => exact hw.elim + case Pdpt => exact hw.elim + case IoPortControl => exact hw.elim + case IoPort => exact hw.elim + case TdxControl => exact hw.elim + all_goals (revert h'; simp [absCapA]; try (split <;> simp)) + refine ⟨.illegalOperation, ?_, Or.inl (by rw [hx]; exact trivial)⟩ + unfold checkValidIpcBuffer + split + · exact absurd rfl (hy _ _ _ _) + · exact run_throwErrorD _ σ + +/-- **`derive_for_buffer`'s errors against the spec's buffer derivation** (module docstring). -/ +theorem bufDerive.decErrA {v : U64} {y : Fl4SpecA64.Cap} {sl : Fl4SpecA64.CslotPtr} {slot : obj.Slot} + (hcr : CapRelA (capA st s slot) y) + (hnc : isUtA (capA st s slot) = true → NoChildrenRelA (st := st) s σ slot sl) + (h : tcb.derive_for_buffer st s v slot = ok (.Err x)) : + v.bv ≠ 0 ∧ ∃ e', (do + let bufferCap ← deriveCap (Z := Unit) sl y + checkValidIpcBuffer v.bv bufferCap + Fl4Spec.NonDetE.returnOk (some (bufferCap, sl))).run σ = ({(.error e', σ)}, False) ∧ VsErrRelA x e' := by + unfold tcb.derive_for_buffer at h + have hcap := capA_eq (st := st) s slot + by_cases h0 : v = 0#u64 + · simp only [if_pos h0, hcap, bind_ok] at h + exact absurd (ok_inj_ff h) (by simp) + have h0' : v.bv ≠ 0 := fun c => h0 (UScalar.eq_equiv_bv_eq _ _ |>.2 c) + refine ⟨h0', ?_⟩ + simp only [if_neg h0, hcap, bind_ok] at h + obtain ⟨r, hr, r', hr', hrel⟩ := derive_relEqA (st := st) s σ slot sl _ y hcr (CapRt.decodeF_encWf _ _) hnc + rw [hr] at h + simp only [bind_ok] at h + cases r with + | Err e => + cases r' with + | ok _ => exact hrel.elim + | error e' => + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, Std.bind_tc_ok] at h + have ex := core.result.Result.Err.inj (ok_inj_ff h) + subst ex + exact ⟨e', run_bind_errorD hr' _, Or.inl hrel⟩ + | Ok c' => + cases r' with + | error _ => exact hrel.elim + | ok an => + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok] at h + obtain ⟨r1, hr1, h⟩ := bind_eq_ok_std h + cases r1 with + | Ok _ => + simp only [bind_ok] at h + exact absurd (ok_inj_ff h) (by simp) + | Err e => + simp only [bind_ok, core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, Std.bind_tc_ok] at h + have ex := core.result.Result.Err.inj (ok_inj_ff h) + subst ex + obtain ⟨e', hs, hrel'⟩ := bufCheck_errA (σ := σ) (x := e) hrel.1 hrel.2.1 + (fun p sz rights dev m e asid va hc => derivedFrameA (st := st) (s := s) (by rw [← hc]; exact hr)) hr1 + refine ⟨e', ?_, hrel'⟩ + rw [run_bind_okD hr'] + exact run_bind_errorD hs _ + +theorem tcbBuf.decErrA (hA : ArchTotal ar) (hne : x ≠ .Preempted) (hst : smp.remote_tcb_stall st ar s a t = ok (s, a)) + (hAr : TcbArgsA st s ctx argsU) (hP : PlaceA th kb) (hX : ExtraOKA th (L.mem s) ctx.extra excaps) + (hnc : ∀ y ys, excaps = y :: ys → isUtA (capA st s (exAtA ctx.extra 0)) = true → + NoChildrenRelA (st := st) s σ (exAtA ctx.extra 0) y.2) + (target : Fl4SpecA64.Cap) (slot : Fl4SpecA64.CslotPtr) + (h : tcb.decode st ar s a fl4_abi.label.TCBSetIPCBuffer t ctx = ok (.Err x, s', a')) : + DecEA σ x (decodeSetIpcBuffer (Z := Unit) (argsU.map UScalar.bv) target slot excaps) := by + obtain ⟨hlen, hargs⟩ := hAr + rw [tcbSetIPCBuffer_programA hst] at h + unfold tcb.decode_set_ipc_buffer tcb.extra_with_length at h + have he := tcbExtra_okA (L := L) (ctx := ctx) hP hX 0#usize (by simp) + simp only [UScalar.ofNatCore_val_eq] at he + simp only [he, bind_ok] at h + have trunc : x = .Sys .TruncatedMessage → argsU = [] ∨ excaps = [] → + DecEA σ x (decodeSetIpcBuffer (Z := Unit) (argsU.map UScalar.bv) target slot excaps) := by + intro hx hc + refine ⟨.truncatedMessage, ?_, Or.inl (by rw [hx]; exact trivial)⟩ + unfold decodeSetIpcBuffer + by_cases hz : argsU = [] + · subst hz + exact run_bind_errorD (run_throwErrorD _ σ) _ + have hz' : (argsU.map UScalar.bv).length ≠ 0 := by simpa using hz + rw [show Fl4Spec.NonDetE.whenE (σ := Fl4SpecA64.State Unit) ((argsU.map UScalar.bv).length = 0) + (Fl4Spec.NonDetE.throwError Fl4SpecA64.SyscallError.truncatedMessage) = pure () from if_neg hz'] + rw [run_bind_okD (a := ()) (s' := σ) rfl] + rcases hc with hc | hc + · exact absurd hc hz + subst hc + exact run_bind_errorD (run_throwErrorD _ σ) _ + match excaps, hX, he, hnc, trunc with + | [], _, _, _, trunc => + simp only [List.length_nil, Nat.lt_irrefl, UScalar.ofNatCore_val_eq, ite_false, error.truncated, bind_ok, + core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, Std.bind_tc_ok] at h + exact trunc (tcbErr_injA h) (Or.inr rfl) + | (c, src) :: ys, hX, _, hnc, trunc => + simp only [List.length_cons, UScalar.ofNatCore_val_eq, Nat.zero_lt_succ, ite_true] at h + by_cases h1 : ctx.length >= 1#u64 + swap + · simp only [if_neg h1, error.truncated, bind_ok, core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, Std.bind_tc_ok] at h + refine trunc (tcbErr_injA h) (Or.inl (List.eq_nil_of_length_eq_zero ?_)) + rw [← hlen]; scalar_tac + simp only [if_pos h1, bind_ok, core.result.Result.Insts.CoreOpsTry.branch] at h + have hl : 0 < argsU.length := by rw [← hlen]; scalar_tac + obtain ⟨v, rest, hv⟩ : ∃ v rest, argsU = v :: rest := by + match argsU, hl with + | v :: rest, _ => exact ⟨v, rest, rfl⟩ + have hf := hargs 0#u64 v (by simp [hv]) + simp only [hf, bind_ok] at h + have hcr := (srcOK_factsA hP (hX.2.1 0 (by simp))).2.2.2.1 + rw [capAtA_eq (st := st)] at hcr + simp only [List.getElem_cons_zero] at hcr + obtain ⟨r, hr, h⟩ := bind_eq_ok_std h + cases r with + | Ok sel => + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok] at h + obtain ⟨s1, -, h⟩ := bind_eq_ok_std h + have K := tcb.thread_control_caps.preA (st := st) (ar := ar) hA s1 a t ctx.slot (capability.Cap.Null, 0#u64) + (capability.Cap.Null, 0#u64) (capability.Cap.Null, 0#u64) (capability.Cap.Null, 0#u64) v sel + tcb.UPDATE_IPC_BUFFER + rw [h, WP.spec_ok] at K + exact (hne (K x rfl)).elim + | Err e => + simp only [core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, Std.bind_tc_ok, bind_ok] at h + have ex := tcbErr_injA h + subst ex + obtain ⟨h0, e', hrun, hrel⟩ := bufDerive.decErrA (σ := σ) (x := x) (y := c) (sl := src) hcr + (hnc _ _ rfl) hr + refine ⟨e', ?_, hrel⟩ + subst hv + unfold decodeSetIpcBuffer + rw [show Fl4Spec.NonDetE.whenE (σ := Fl4SpecA64.State Unit) (((v :: rest).map UScalar.bv).length = 0) + (Fl4Spec.NonDetE.throwError Fl4SpecA64.SyscallError.truncatedMessage) = pure () from if_neg (by simp)] + rw [run_bind_okD (a := ()) (s' := σ) rfl] + rw [show Fl4Spec.NonDetE.whenE (σ := Fl4SpecA64.State Unit) (((c, src) :: ys).length = 0) + (Fl4Spec.NonDetE.throwError Fl4SpecA64.SyscallError.truncatedMessage) = pure () from if_neg (by simp)] + rw [run_bind_okD (a := ()) (s' := σ) rfl] + rw [run_bind_okD (a := v.bv) (s' := σ) rfl] + rw [run_bind_okD (a := (c, src)) (s' := σ) rfl] + dsimp only + rw [if_neg h0] + exact run_bind_errorD hrun _ + +end + +end + +end Fl4ProofsA64.P3.ReplyErr diff --git a/proofs/Fl4ProofsA64/P3/Stage3DecErrTcbHighA.lean b/proofs/Fl4ProofsA64/P3/Stage3DecErrTcbHighA.lean new file mode 100644 index 000000000..4f0e7a977 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3DecErrTcbHighA.lean @@ -0,0 +1,187 @@ +/- +P3 (aarch64, A64-P3 Lift 1b, part C2): **decoder errors of SetHighVSpace**. Every FL4 `Err` other than `Preempted` +precedes the caller's Restart. + +[J] `R`, `hXs` the record's VSpace relation and Stage-2 roots. P0 `ArchTotal`. [S] `hst`. New module. +-/ +import Fl4ProofsA64.P3.Stage3DecErrTcbAuthA +import Fl4ProofsA64.P3.TcbInvokeHighSelectA +import Fl4ProofsA64.P3.Stage3DecErrTcbObjA +import Fl4ProofsA64.P3.ExtMapFrA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.ReplyErr +open fl4_core_a64 Fl4ProofsA64.P0 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P2.PathObl Fl4ProofsA64.P3 + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +/-- A cap that is no FL4 high root, nor null, is neither in the spec. -/ +theorem notHighA {c : capability.Cap} {y : Fl4SpecA64.Cap} (h : CapEqA c y) (h0 : c ≠ .Null) + (hc : ∀ p m asid, c ≠ .HighVSpace p m asid) (hw : CapWfA c) : + y ≠ .nullCap ∧ ∀ p o, y ≠ .archObjectCap (.ext (.highVSpaceCap p o)) := by + unfold CapEqA at h + split at h + · exact ⟨fun e => (by cases e), fun _ _ e => (by cases e)⟩ + · subst h + cases c + case Null => exact absurd rfl h0 + case HighVSpace => exact absurd rfl (hc _ _ _) + case PageDirectory => exact hw.elim + case Pdpt => exact hw.elim + case IoPortControl => exact hw.elim + case IoPort => exact hw.elim + case TdxControl => exact hw.elim + case Frame => + simp only [absCapA, absFrameE] + split <;> exact ⟨fun h => (by cases h), fun _ _ h => (by cases h)⟩ + all_goals exact ⟨fun h => (by cases h), fun _ _ h => (by cases h)⟩ + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {s s' : S} {a a' : A} {t : U64} {ctx : tcb.Ctx} {x : error.Exc} {σ : Fl4SpecA64.State Unit} + {th : TypedHeapA} {kb : U64} {excaps : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)} + {Pp Tb X Y : Set U64} {Lv : U64 → Nat} + +set_option maxHeartbeats 4000000 in +theorem tcbHigh.decErrA (hA : ArchTotal ar) (hne : x ≠ .Preempted) (hst : smp.remote_tcb_stall st ar s a t = ok (s, a)) + (hP : PlaceA th kb) (hX : ExtraOKA th (L.mem s) ctx.extra excaps) + (R : VsRelA Pp Tb Lv X Y (L.mem s) (L.ksb s) σ) + (hXs : ∀ r ∈ X, ∃ es, σ.kheap (specPtr r) = some (.archObj (.ext (.stage2Root es)))) + (target : Fl4SpecA64.ObjRef) (slot : Fl4SpecA64.CslotPtr) + (h : tcb.decode st ar s a fl4_abi.label.TCBSetHighVSpace t ctx = ok (.Err x, s', a')) : + DecEA σ x (decodeSetHighVSpace (Z := Unit) target slot excaps) := by + rw [tcbSetHighVSpace_programA hst] at h + have hl : fl4_abi.label.TCBSetHighVSpace ≠ fl4_abi.label.TCBSetPACKeys := by + rw [fl4_abi.label.TCBSetHighVSpace, fl4_abi.label.TCBSetPACKeys] + decide + simp only [tcb.decode_arch, if_neg hl] at h + have he := tcbExtra_okA (L := L) (ctx := ctx) hP hX 0#usize (by simp) + simp only [UScalar.ofNatCore_val_eq] at he + unfold tcb.decode_set_high_vspace at h + match excaps, hX, he with + | [], _, he => + simp only [List.length_nil, Nat.lt_irrefl, ite_false] at he + simp only [he, bind_ok, error.truncated] at h + cases tcbErr_injA h + exact ⟨.truncatedMessage, run_throwErrorD _ σ, Or.inl trivial⟩ + | (c, src) :: ys, hX, he => + simp only [List.length_cons, Nat.zero_lt_succ, ite_true] at he + have hcr := (srcOK_factsA hP (hX.2.1 0 (by simp))).2.2.2.1 + rw [capAtA_eq (st := st)] at hcr + simp only [List.getElem_cons_zero] at hcr + have hcap := capA_eq (st := st) s (exAtA ctx.extra 0) + simp only [he, hcap, bind_ok] at h + have post : ∀ (s1 : S) (v : capability.Cap), + tcb.install_tcb_cap st ar s1 a t ctx.slot layout.tcb.SLOT_HIGH_VSPACE v (exAtA ctx.extra 0) = + ok (.Err x, s', a') → False := by + intro s1 v h + have K := tcb.install_tcb_cap.preA (st := st) (ar := ar) hA s1 a t ctx.slot layout.tcb.SLOT_HIGH_VSPACE v + (exAtA ctx.extra 0) + rw [h, WP.spec_ok] at K + exact hne (K x rfl) + have bad : x = .Sys (.InvalidCapability 1#u64) → + (decodeSetHighVSpace (Z := Unit) target slot ((c, src) :: ys)).run σ = + ({(.error (.invalidCapability 1), σ)}, False) → + DecEA σ x (decodeSetHighVSpace (Z := Unit) target slot ((c, src) :: ys)) := + fun hx hr => ⟨_, hr, Or.inr ⟨1#u64, hx, rfl⟩⟩ + by_cases h0 : capA st s (exAtA ctx.extra 0) = .Null + · rw [h0] at h + dsimp only at h + obtain ⟨s1, -, h⟩ := bind_eq_ok_std h + exact (post _ _ h).elim + by_cases hH : ∃ p m asid, capA st s (exAtA ctx.extra 0) = .HighVSpace p m asid + swap + · have hk : ∀ p m asid, capA st s (exAtA ctx.extra 0) ≠ .HighVSpace p m asid := + fun p m asid c => hH ⟨p, m, asid, c⟩ + have hx : x = .Sys (.InvalidCapability 1#u64) := by + cases hc : capA st s (exAtA ctx.extra 0) <;> rw [hc] at h <;> dsimp only at h <;> first + | exact absurd hc h0 + | exact absurd hc (hk _ _ _) + | (simp only [error.invalid_cap, bind_ok] at h; exact tcbErr_injA h) + have hn := notHighA hcr.2 h0 hk hcr.1 + refine bad hx ?_ + simp only [decodeSetHighVSpace] + cases c with + | nullCap => exact absurd rfl hn.1 + | archObjectCap c => + cases c with + | ext e => + cases e with + | highVSpaceCap p o => exact absurd rfl (hn.2 p o) + | _ => exact run_throwErrorD _ σ + | _ => exact run_throwErrorD _ σ + | _ => exact run_throwErrorD _ σ + obtain ⟨p, m, asid, hq⟩ := hH + rw [hq] at hcr h + have hc : c = .archObjectCap (.ext (.highVSpaceCap (specPtr p) (asidDataA m asid))) := by + have e := hcr.2 + exact e.symm + subst hc + have h16 : asid.val < 65536 := lt_of_lt_of_eq hcr.1 (by norm_num) + dsimp only at h + cases m with + | false => + simp only [Bool.false_eq_true, ite_false, error.invalid_cap, bind_ok] at h + refine bad (tcbErr_injA h) ?_ + simp only [decodeSetHighVSpace, asidDataA, Bool.false_eq_true, ite_false] + exact run_throwErrorD _ σ + | true => + simp only [ite_true] at h + have hrun := findExtRoot_runX .high (asidOfA asid) σ + have spec : extRootResX σ .high (asidOfA asid) ≠ .ok (specPtr p) → x = .Sys (.InvalidCapability 1#u64) → + DecEA σ x (decodeSetHighVSpace (Z := Unit) target slot + ((.archObjectCap (.ext (.highVSpaceCap (specPtr p) (asidDataA true asid))), src) :: ys)) := by + intro hn hx + refine bad hx ?_ + simp only [decodeSetHighVSpace, asidDataA, ite_true] + have hc : ∀ (o : Option Fl4SpecA64.ObjRef), o ≠ some (specPtr p) → + (NonDetE.catch (do + let r ← findExtRoot (Z := Unit) .high (asidOfA asid) + NonDetE.returnOk (some r)) (fun _ => pure none)) σ = ({(o, σ)}, False) → + (do + let r ← NonDetE.liftE <| NonDetE.catch (do + let r ← findExtRoot (Z := Unit) .high (asidOfA asid) + NonDetE.returnOk (some r)) (fun _ => pure none) + NonDetE.unlessE (r = some (specPtr p)) <| NonDetE.throwError (SyscallError.invalidCapability 1) + let derived ← deriveCap src (.archObjectCap (.ext (.highVSpaceCap (specPtr p) (some (asidOfA asid))))) + NonDetE.returnOk (⟨target, slot, derived, src⟩ : SetHighVSpaceInvocation)).run σ = + ({(.error (.invalidCapability 1), σ)}, False) := by + intro o ho hr + rw [run_bind_okD (run_liftE_stD hr)] + exact unlessE_neg_runX ho _ _ σ + cases hv : extRootResX σ .high (asidOfA asid) with + | error e => + refine hc none (by simp) ?_ + rw [catch_errX (e := e) (s' := σ) (by rw [run_bind_errorD (by rw [hrun, hv])])] + rfl + | ok r => + refine hc (some r) (fun c => hn (by rw [hv, Option.some.inj c])) ?_ + refine catch_okX (s' := σ) ?_ + rw [run_bind_okD (a := r) (s' := σ) (by rw [hrun, hv])] + rfl + have hf := find_high_asidA R hXs h16 + obtain ⟨v, hv, h⟩ := bind_eq_ok_std h + rw [hv, WP.spec_ok] at hf + cases v with + | Err e => + simp only [error.invalid_cap, bind_ok] at h + exact spec (by rw [hf.2]; exact fun c => by cases c) (tcbErr_injA h) + | Ok w => + dsimp only at h + by_cases hw : w = p + swap + · simp only [if_neg hw, error.invalid_cap, bind_ok] at h + refine spec (fun c => ?_) (tcbErr_injA h) + rw [hf.2] at c + exact hw (specPtr_inj (Except.ok.inj c)) + rw [if_pos hw] at h + simp only [objects.derive_cap, ite_true, bind_ok, core.result.Result.Insts.CoreOpsTry.branch] at h + obtain ⟨s1, -, h⟩ := bind_eq_ok_std h + exact (post _ _ h).elim + +end + +end + +end Fl4ProofsA64.P3.ReplyErr diff --git a/proofs/Fl4ProofsA64/P3/Stage3DecErrTcbObjA.lean b/proofs/Fl4ProofsA64/P3/Stage3DecErrTcbObjA.lean new file mode 100644 index 000000000..17bb54de4 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3DecErrTcbObjA.lean @@ -0,0 +1,491 @@ +/- +P3 (aarch64, A64-P3 Lift 1b, part C2): **decoder errors of ReadFPU, WriteFPU, BindNotification, UnbindNotification**. +Every FL4 `Err` precedes the caller's Restart. + +[J] (record facts) `hk` the target's `TcbRelE`, `hN` the notification relation, `hmem` a Notification cap of an extra +slot names a notification of `N`. [S] `hst`. New module. +-/ +import Fl4ProofsA64.P3.Stage3DecErrTcbAuthA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.ReplyErr +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P2.PathObl Fl4ProofsA64.P3 + Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +/-- A cap that is no FL4 frame represents no spec TCB frame. -/ +theorem notFrameA {c : capability.Cap} {y : Fl4SpecA64.Cap} (h : CapEqA c y) + (hc : ∀ p sz r d m e asid va, c ≠ .Frame p sz r d m e asid va) : fl4TcbFrame y = none := by + unfold CapEqA at h + split at h + · simp [fl4TcbFrame, ArchCap.isFrameCap] + · subst h + cases c + case Frame => exact absurd rfl (hc _ _ _ _ _ _ _ _) + all_goals + simp only [absCapA] + (repeat' split) <;> simp [fl4TcbFrame, ArchCap.isFrameCap] + +theorem frameCapViewA {p r asid va : U64} {sz : capability.FrameSize} {dev m ept : Bool} {y : Fl4SpecA64.Cap} + (h : CapEqA (.Frame p sz r dev m ept asid va) y) : + fl4TcbFrame y = some (specPtr p, vmRightsOfA r, frameSizeOfA sz, dev) := by + have e : absCapA (.Frame p sz r dev m ept asid va) = y := h + subst e + cases ept <;> cases sz <;> + simp [fl4TcbFrame, absCapA, absFrameE, frameSizeOfA, ArchCap.isFrameCap, ArchCap.acapObj, ArchCap.acapRights, + ArchCap.acapFsize, archCapIsDevice, ExtArchCap.objRef] + +theorem frameSizeWordA (sz : capability.FrameSize) : + ∃ b size : U64, capability.FrameSize.bits sz = ok b ∧ (1#u64 <<< (b &&& 63#u64)) = ok size ∧ + size.val = 2 ^ pageBitsForSize (frameSizeOfA sz) := by + cases sz + · exact ⟨12#u64, 4096#u64, rfl, by with_unfolding_all rfl, by decide⟩ + · exact ⟨21#u64, 2097152#u64, rfl, by with_unfolding_all rfl, by decide⟩ + · exact ⟨30#u64, 1073741824#u64, rfl, by with_unfolding_all rfl, by decide⟩ + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {s s' : S} {a a' : A} {t : U64} {ctx : tcb.Ctx} {x : error.Exc} {σ : Fl4SpecA64.State Unit} + {argsU : List U64} {th : TypedHeapA} {kb : U64} {excaps : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)} + +theorem tcbExtra_okA (hP : PlaceA th kb) (hX : ExtraOKA th (L.mem s) ctx.extra excaps) (i : Usize) (hi : i.val < 3) : + tcb.extra ctx i = ok (if i.val < excaps.length then some (exAtA ctx.extra i.val) else none) := by + have hidx : Aeneas.Std.Array.index_usize ctx.extra i = ok (exAtA ctx.extra i.val) := by + have hl : ctx.extra.val.length = 3 := by simpa using ctx.extra.property + have : ctx.extra.val[i.val]? = some (ctx.extra.val[i.val]'(by omega)) := List.getElem?_eq_getElem _ + simp [Aeneas.Std.Array.index_usize, exAtA, List.getD_eq_getElem?_getD, this] + have h3 : ¬ i ≥ 3#usize := by scalar_tac + unfold tcb.extra + simp only [if_neg h3, hidx, bind_ok, obj.Slot.is_null] + by_cases c : i.val < excaps.length + · have c0 : ¬ exAtA ctx.extra i.val = 0#u64 := fun e => (srcOK_factsA hP (hX.2.1 _ c)).1 e + simp only [c0, decide_false, Bool.false_eq_true, ite_false, if_pos c] + · have c0 : exAtA ctx.extra i.val = 0#u64 := hX.2.2 _ (by omega) hi + simp only [c0, decide_true, ite_true, if_neg c] + +private theorem fpu_truncA {toF : Bool} {args : List Fl4SpecA64.Data} {u : Fl4SpecA64.ObjRef} + (h : excaps = [] ∨ args.length < 1) : + DecEA σ (.Sys .TruncatedMessage) (decodeFl4FpuTransfer (Z := Unit) toF args u excaps) := by + refine ⟨.truncatedMessage, ?_, Or.inl trivial⟩ + unfold decodeFl4FpuTransfer + rw [show Fl4Spec.NonDetE.whenE (σ := Fl4SpecA64.State Unit) (excaps = [] ∨ args.length < 1) + (Fl4Spec.NonDetE.throwError Fl4SpecA64.SyscallError.truncatedMessage) = + Fl4Spec.NonDetE.throwError .truncatedMessage from if_pos h] + exact run_bind_errorD (run_throwErrorD _ σ) _ + +set_option maxHeartbeats 4000000 in +theorem tcbFpu.decErrA (toF : Bool) (hA : TcbArgsA st s ctx argsU) (hP : PlaceA th kb) + (hX : ExtraOKA th (L.mem s) ctx.extra excaps) (u : Fl4SpecA64.ObjRef) + (h : tcb.fpu_transfer st ar s a t ctx toF = ok (.Err x, s', a')) : + DecEA σ x (decodeFl4FpuTransfer (Z := Unit) toF (argsU.map UScalar.bv) u excaps) := by + obtain ⟨hlen, hargs⟩ := hA + have he := tcbExtra_okA (L := L) (ctx := ctx) hP hX 0#usize (by simp) + simp only [UScalar.ofNatCore_val_eq] at he + unfold tcb.fpu_transfer at h + by_cases h0 : 0 < excaps.length + swap + · rw [if_neg h0] at he + simp only [he, bind_ok, error.truncated] at h + cases tcbErr_injA h + exact fpu_truncA (Or.inl (List.length_eq_zero_iff.1 (by omega))) + rw [if_pos h0] at he + simp only [he, bind_ok] at h + by_cases h1 : ctx.length ≥ 1#u64 + swap + · rw [if_neg h1] at h + simp only [error.truncated, bind_ok] at h + cases tcbErr_injA h + refine fpu_truncA (Or.inr ?_) + rw [List.length_map, ← hlen]; scalar_tac + rw [if_pos h1] at h + match excaps, hX, h0, argsU, hargs, (show 1 ≤ argsU.length by rw [← hlen]; scalar_tac) with + | y :: ys, hX, _, f :: rest, hargs, _ => + have hf := hargs 0#u64 f (by simp) + have hcr := (srcOK_factsA hP (hX.2.1 0 (by simp))).2.2.2.1 + rw [capAtA_eq (st := st)] at hcr + simp only [List.getElem_cons_zero] at hcr + have hcap := capA_eq (st := st) s (exAtA ctx.extra 0) + simp only [hf, hcap, bind_ok] at h + have spec : ∀ (e : Fl4SpecA64.SyscallError), VsErrRelA x e → + (match fl4TcbFrame y.1 with + | some (_, rights, sz, dev) => + (dev = true ∨ rights = vmKernelOnly ∨ (toF = true ∧ rights ≠ vmReadWrite)) ∧ e = .invalidCapability 1 ∨ + (¬ (dev = true ∨ rights = vmKernelOnly ∨ (toF = true ∧ rights ≠ vmReadWrite)) ∧ e = .invalidArgument 0 ∧ + (f.val % 64 ≠ 0 ∨ f.val ≥ 2 ^ pageBitsForSize sz ∨ 2 ^ pageBitsForSize sz - f.val < fl4FpuStateSize)) + | none => e = .invalidCapability 1) → + DecEA σ x (decodeFl4FpuTransfer (Z := Unit) toF ((f :: rest).map UScalar.bv) u (y :: ys)) := by + intro e hrel hc + refine ⟨e, ?_, hrel⟩ + unfold decodeFl4FpuTransfer + rw [show Fl4Spec.NonDetE.whenE (σ := Fl4SpecA64.State Unit) + ((y :: ys) = [] ∨ ((f :: rest).map UScalar.bv).length < 1) + (Fl4Spec.NonDetE.throwError Fl4SpecA64.SyscallError.truncatedMessage) = pure () from if_neg (by simp), + ndE_pure_bindD] + simp only [Fl4Spec.NonDetE.returnOk, ndE_pure_bindD, List.getElem?_cons_zero, the, Option.getD_some, + List.map_cons] + cases hv : fl4TcbFrame y.1 with + | none => + rw [hv] at hc + subst hc + exact run_throwErrorD _ σ + | some q => + obtain ⟨base, rights, sz, dev⟩ := q + rw [hv] at hc + dsimp only at hc ⊢ + rcases hc with ⟨hb, rfl⟩ | ⟨hb, rfl, hbad⟩ + · rw [if_pos hb] + exact run_throwErrorD _ σ + · rw [if_neg hb] + rw [show Fl4Spec.NonDetE.whenE (σ := Fl4SpecA64.State Unit) + (f.bv.toNat % 64 ≠ 0 ∨ f.bv.toNat ≥ 2 ^ pageBitsForSize sz ∨ + 2 ^ pageBitsForSize sz - f.bv.toNat < fl4FpuStateSize) + (Fl4Spec.NonDetE.throwError (Fl4SpecA64.SyscallError.invalidArgument 0)) = + Fl4Spec.NonDetE.throwError (.invalidArgument 0) from if_pos hbad] + exact run_bind_errorD (run_throwErrorD _ σ) _ + by_cases hF : ∃ p sz r d m e asid va, capA st s (exAtA ctx.extra 0) = .Frame p sz r d m e asid va + swap + · have hk : ∀ p sz r d m e asid va, capA st s (exAtA ctx.extra 0) ≠ .Frame p sz r d m e asid va := + fun p sz r d m e asid va c => hF ⟨p, sz, r, d, m, e, asid, va, c⟩ + have hx : x = .Sys (.InvalidCapability 1#u64) := by + cases hc : capA st s (exAtA ctx.extra 0) <;> rw [hc] at h <;> dsimp only at h <;> first + | exact absurd hc (hk _ _ _ _ _ _ _ _) + | (simp only [error.invalid_cap, bind_ok] at h; exact tcbErr_injA h) + refine spec (.invalidCapability 1) (Or.inr ⟨1#u64, hx, rfl⟩) ?_ + rw [notFrameA hcr.2 hk] + obtain ⟨p, sz, r, d, m, e, asid, va, hq⟩ := hF + rw [hq] at hcr h + have hv := frameCapViewA hcr.2 + have hr : 1 ≤ r.val ∧ r.val ≤ 3 := ⟨hcr.1.1, hcr.1.2.1⟩ + have mw : Rights.allowWrite ∈ vmReadWrite := by simp [vmReadWrite] + have nrw : vmReadWrite ≠ vmKernelOnly := fun c => by rw [c] at mw; simp [vmKernelOnly] at mw + have nko : vmKernelOnly ≠ vmReadWrite := fun c => nrw c.symm + have nro' : vmReadOnly ≠ vmReadWrite := fun c => by rw [← c] at mw; simp [vmReadOnly] at mw + have nro : vmReadOnly ≠ vmKernelOnly := fun c => by + have mr : Rights.allowRead ∈ vmReadOnly := by simp [vmReadOnly] + rw [c] at mr; simp [vmKernelOnly] at mr + have hko : r = capability.VM_KERNEL_ONLY ↔ vmRightsOfA r = vmKernelOnly := by + have e1 : capability.VM_KERNEL_ONLY = 1#u64 := by with_unfolding_all rfl + have hrv : r = capability.VM_KERNEL_ONLY ↔ r.val = 1 := by + rw [e1] + exact ⟨fun h => by rw [h]; rfl, fun h => UScalar.eq_of_val_eq h⟩ + rw [hrv] + have : r.val = 1 ∨ r.val = 2 ∨ r.val = 3 := by omega + rcases this with h | h | h <;> unfold vmRightsOfA <;> rw [h] + · simp + · exact iff_of_false (by omega) nro + · exact iff_of_false (by omega) nrw + have hrw : r = capability.VM_READ_WRITE ↔ vmRightsOfA r = vmReadWrite := by + have e3 : capability.VM_READ_WRITE = 3#u64 := by with_unfolding_all rfl + have hrv : r = capability.VM_READ_WRITE ↔ r.val = 3 := by + rw [e3] + exact ⟨fun h => by rw [h]; rfl, fun h => UScalar.eq_of_val_eq h⟩ + rw [hrv] + have : r.val = 1 ∨ r.val = 2 ∨ r.val = 3 := by omega + rcases this with h | h | h <;> unfold vmRightsOfA <;> rw [h] + · exact iff_of_false (by omega) (fun c => nko c) + · exact iff_of_false (by omega) (fun c => nro' c) + · simp + have capErr : x = .Sys (.InvalidCapability 1#u64) → + (d = true ∨ vmRightsOfA r = vmKernelOnly ∨ (toF = true ∧ vmRightsOfA r ≠ vmReadWrite)) → + DecEA σ x (decodeFl4FpuTransfer (Z := Unit) toF ((f :: rest).map UScalar.bv) u (y :: ys)) := by + intro hx hb + refine spec (.invalidCapability 1) (Or.inr ⟨1#u64, hx, rfl⟩) ?_ + rw [hv] + exact Or.inl ⟨hb, rfl⟩ + dsimp only at h + cases d with + | true => + simp only [ite_true, error.invalid_cap, bind_ok] at h + exact capErr (tcbErr_injA h) (Or.inl rfl) + | false => + simp only [Bool.false_eq_true, ite_false] at h + by_cases hk : r = capability.VM_KERNEL_ONLY + · simp only [if_pos hk, error.invalid_cap, bind_ok] at h + exact capErr (tcbErr_injA h) (Or.inr (Or.inl (hko.1 hk))) + rw [if_neg hk] at h + by_cases hw : toF = true ∧ r ≠ capability.VM_READ_WRITE + · obtain ⟨rfl, hw⟩ := hw + have hw' : (r != capability.VM_READ_WRITE) = true := by simpa [bne_iff_ne] using hw + simp only [ite_true, hw', error.invalid_cap, bind_ok] at h + exact capErr (tcbErr_injA h) (Or.inr (Or.inr ⟨rfl, fun c => hw (hrw.2 c)⟩)) + have hnb : ¬ (false = true ∨ vmRightsOfA r = vmKernelOnly ∨ (toF = true ∧ vmRightsOfA r ≠ vmReadWrite)) := by + rintro (c | c | ⟨c1, c2⟩) + · cases c + · exact hk (hko.2 c) + · exact hw ⟨c1, fun e => c2 (hrw.1 e)⟩ + have argErr : x = .Sys (.InvalidArgument 0#u64) → + (f.val % 64 ≠ 0 ∨ f.val ≥ 2 ^ pageBitsForSize (frameSizeOfA sz) ∨ + 2 ^ pageBitsForSize (frameSizeOfA sz) - f.val < fl4FpuStateSize) → + DecEA σ x (decodeFl4FpuTransfer (Z := Unit) toF ((f :: rest).map UScalar.bv) u (y :: ys)) := by + intro hx hb + refine spec (.invalidArgument 0) (Or.inl (by rw [hx]; exact rfl)) ?_ + rw [hv] + exact Or.inr ⟨hnb, rfl, hb⟩ + obtain ⟨b, size, hb, hsz, hsv⟩ := frameSizeWordA sz + have hn := eq_of_spec_fd Fl4ProofsA64.P0.Consts.fl4_abi.consts.FPU_STATE_SIZE.spec + obtain ⟨md, hmd, hmdv⟩ := spec_existsA (show (f % 64#u64) ⦃ q => q.val = f.val % 64 ⦄ by + step as ⟨q, hq⟩ + simpa using hq) + have hw' : toF = true → (r != capability.VM_READ_WRITE) = false := by + intro c + have : r = capability.VM_READ_WRITE := Classical.byContradiction fun d => hw ⟨c, d⟩ + rw [this]; exact bne_self_eq_false _ + cases toF + all_goals simp only [Bool.false_eq_true, ite_false, ite_true] at h + all_goals (try simp only [hw' rfl, Bool.false_eq_true, ite_false] at h) + all_goals + obtain ⟨b', hb', h⟩ := bind_eq_ok_std h + rw [hb] at hb' + cases ok_inj_ff hb' + simp only [lift, bind_ok, Std.bind_tc_ok] at h + rw [hsz, bind_ok, hn, bind_ok, hmd, bind_ok] at h + by_cases hm : md = 0#u64 + swap + · have hm' : (md != 0#u64) = true := by simpa [bne_iff_ne] using hm + simp only [hm', ite_true, error.invalid_arg, bind_ok] at h + refine argErr (tcbErr_injA h) (Or.inl fun c => hm (UScalar.eq_of_val_eq ?_)) + rw [hmdv, c]; rfl + subst hm + simp only [bne_self_eq_false, Bool.false_eq_true, ite_false] at h + by_cases hge : f >= size + · simp only [if_pos hge, error.invalid_arg, bind_ok] at h + refine argErr (tcbErr_injA h) (Or.inr (Or.inl ?_)) + rw [← hsv]; scalar_tac + rw [if_neg hge] at h + obtain ⟨rm, hrm, hrmv⟩ := spec_existsA (show (size - f) ⦃ q => q.val = size.val - f.val ⦄ by + step as ⟨q, hq⟩ + scalar_tac) + rw [hrm, bind_ok] at h + by_cases hlt : rm < 520#u64 + · simp only [if_pos hlt, error.invalid_arg, bind_ok] at h + refine argErr (tcbErr_injA h) (Or.inr (Or.inr ?_)) + have e : fl4FpuStateSize = 520 := by decide + rw [← hsv, e]; scalar_tac + rw [if_neg hlt] at h + exfalso + tcb_noerr h +/-! ## Notification binding -/ + +theorem tcbBoundNtfn_eqA (s : S) (t : U64) : obj.Tcb.bound_ntfn st t s = ok (L.mem s (tBoundA t)) := by + unfold obj.Tcb.bound_ntfn + rw [show layout.tcb.BOUND_NTFN = ok 3368#u64 by with_unfolding_all rfl] + simp only [bind_ok, lift, L.read_eq] + rfl + +theorem tcbBoundRun_eqA {y : Fl4SpecA64.Tcb} (hk : σ.kheap (specPtr t) = some (.tcb y)) + (hb : y.tcbBoundNotification = optRef (L.mem s (tBoundA t))) : + (Fl4Spec.NonDetE.liftE (getTcbObjRef Fl4SpecA64.Tcb.tcbBoundNotification (specPtr t)) : + Fl4SpecA64.SEMonad Unit _).run σ = ({(.ok (optRef (L.mem s (tBoundA t))), σ)}, False) := by + rw [← hb] + exact run_liftE_stD (threadGet_eqSc Fl4SpecA64.Tcb.tcbBoundNotification hk) + +theorem tcbUnbind.decErrA (hst : smp.remote_tcb_stall st ar s a t = ok (s, a)) {y : Fl4SpecA64.Tcb} + (hk : σ.kheap (specPtr t) = some (.tcb y)) (hb : y.tcbBoundNotification = optRef (L.mem s (tBoundA t))) + (h : tcb.decode st ar s a fl4_abi.label.TCBUnbindNotification t ctx = ok (.Err x, s', a')) : + DecEA σ x (decodeUnbindNotification (Z := Unit) (.threadCap (specPtr t))) := by + rw [tcbUnbindNotification_programA hst, tcbBoundNtfn_eqA] at h + simp only [bind_ok] at h + by_cases h0 : L.mem s (tBoundA t) = 0#u64 + swap + · rw [if_neg h0] at h + exfalso + tcb_noerr h + rw [if_pos h0] at h + simp only [error.illegal, bind_ok] at h + cases tcbErr_injA h + refine ⟨.illegalOperation, ?_, Or.inl trivial⟩ + simp only [decodeUnbindNotification] + rw [run_bind_okD (tcbBoundRun_eqA hk hb), show optRef (L.mem s (tBoundA t)) = none from if_pos h0] + exact run_bind_errorD (run_throwErrorD _ σ) _ + +set_option maxHeartbeats 4000000 in +theorem tcbBind.decErrA (hst : smp.remote_tcb_stall st ar s a t = ok (s, a)) (hP : PlaceA th kb) + (hX : ExtraOKA th (L.mem s) ctx.extra excaps) {y : Fl4SpecA64.Tcb} + (hk : σ.kheap (specPtr t) = some (.tcb y)) (hb : y.tcbBoundNotification = optRef (L.mem s (tBoundA t))) + {N T : Set U64} (hN : NtfnStateRel N T (L.mem s) σ) + (hmem : ∀ p b sd rc, exAtA ctx.extra 0 ≠ 0#u64 → capA st s (exAtA ctx.extra 0) = .Notification p b sd rc → p ∈ N) + (h : tcb.decode st ar s a fl4_abi.label.TCBBindNotification t ctx = ok (.Err x, s', a')) : + DecEA σ x (decodeBindNotification (Z := Unit) (.threadCap (specPtr t)) excaps) := by + rw [tcbBindNotification_programA hst] at h + obtain ⟨⟨r, s1⟩, hd, h⟩ := bind_eq_ok_std h + have : r = .Err x := congrArg Prod.fst (ok_inj_ff h) + subst this + clear h + have he := tcbExtraWL_okA (L := L) (ctx := ctx) hP hX 0#usize (by simp) 0#u64 + simp only [UScalar.ofNatCore_val_eq] at he + unfold tcb.decode_bind_notification at hd + by_cases h0 : 0 < excaps.length + swap + · rw [if_neg (fun c => h0 c.1)] at he + simp only [he, bind_ok, core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, Std.bind_tc_ok] at hd + cases (congrArg Prod.fst (ok_inj_ff hd)) + refine ⟨.truncatedMessage, ?_, Or.inl trivial⟩ + simp only [decodeBindNotification] + rw [show Fl4Spec.NonDetE.whenE (σ := Fl4SpecA64.State Unit) (excaps.length = 0) + (Fl4Spec.NonDetE.throwError Fl4SpecA64.SyscallError.truncatedMessage) = + Fl4Spec.NonDetE.throwError .truncatedMessage from if_pos (by omega)] + exact run_bind_errorD (run_throwErrorD _ σ) _ + rw [if_pos ⟨h0, by scalar_tac⟩] at he + match excaps, hX, h0 with + | z :: zs, hX, _ => + have hsrc := srcOK_factsA hP (hX.2.1 0 (by simp)) + have hcr := hsrc.2.2.2.1 + rw [capAtA_eq (st := st)] at hcr + simp only [List.getElem_cons_zero] at hcr + have hcap := capA_eq (st := st) s (exAtA ctx.extra 0) + simp only [he, bind_ok, core.result.Result.Insts.CoreOpsTry.branch, tcbBoundNtfn_eqA] at hd + have pre : (Fl4Spec.NonDetE.whenE (σ := Fl4SpecA64.State Unit) ((z :: zs).length = 0) + (Fl4Spec.NonDetE.throwError Fl4SpecA64.SyscallError.truncatedMessage)) = pure () := if_neg (by simp) + have done : ∀ e, x = .Sys .IllegalOperation → e = Fl4SpecA64.SyscallError.illegalOperation → + (decodeBindNotification (Z := Unit) (.threadCap (specPtr t)) (z :: zs)).run σ = ({(.error e, σ)}, False) → + DecEA σ x (decodeBindNotification (Z := Unit) (.threadCap (specPtr t)) (z :: zs)) := by + rintro e rfl rfl hr + exact ⟨_, hr, Or.inl trivial⟩ + by_cases hbn : L.mem s (tBoundA t) = 0#u64 + swap + · have hbn' : (L.mem s (tBoundA t) != 0#u64) = true := by simpa [bne_iff_ne] using hbn + simp only [hbn', ite_true, error.illegal, bind_ok] at hd + refine done _ (core.result.Result.Err.inj (congrArg Prod.fst (ok_inj_ff hd))).symm rfl ?_ + simp only [decodeBindNotification] + rw [pre, ndE_pure_bindD, run_bind_okD (tcbBoundRun_eqA hk hb), + show optRef (L.mem s (tBoundA t)) = some (specPtr (L.mem s (tBoundA t))) from if_neg hbn] + exact run_bind_errorD (run_throwErrorD _ σ) _ + simp only [hbn, bne_self_eq_false, Bool.false_eq_true, ite_false] at hd + have hnone : optRef (L.mem s (tBoundA t)) = none := if_pos hbn + obtain ⟨v, hv, hd⟩ := bind_eq_ok_std hd + have hx : v = .Err x := by + cases v with + | Ok w => + exfalso + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok] at hd + obtain ⟨_, _, hd⟩ := bind_eq_ok_std hd + obtain ⟨_, _, hd⟩ := bind_eq_ok_std hd + exact absurd (congrArg Prod.fst (ok_inj_ff hd)) (by simp) + | Err e => + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, Std.bind_tc_ok] at hd + have := core.result.Result.Err.inj (congrArg Prod.fst (ok_inj_ff hd)) + rw [this] + subst hx + clear hd + unfold tcb.bindable_ntfn at hv + simp only [hcap, bind_ok] at hv + by_cases hNt : ∃ p b sd rc, capA st s (exAtA ctx.extra 0) = .Notification p b sd rc + swap + · have hk' : ∀ p b sd rc, capA st s (exAtA ctx.extra 0) ≠ .Notification p b sd rc := + fun p b sd rc c => hNt ⟨p, b, sd, rc, c⟩ + have hx : x = .Sys .IllegalOperation := by + cases hc : capA st s (exAtA ctx.extra 0) <;> rw [hc] at hv <;> dsimp only at hv <;> first + | exact absurd hc (hk' _ _ _ _) + | (simp only [error.illegal] at hv; exact core.result.Result.Err.inj (ok_inj_ff hv).symm) + refine done _ hx rfl ?_ + have hny : ∀ p b R, z.1 ≠ .notificationCap p b R := by + intro p b R c + have e := hcr.2 + unfold CapEqA at e + split at e + · rw [c] at * + rename_i heq + cases heq + · rw [c] at e + revert e + generalize hq : capA st s (exAtA ctx.extra 0) = q + intro e + cases q + case Notification p' b' sd rc => exact hk' _ _ _ _ hq + all_goals + revert e + simp only [absCapA] + (repeat' split) <;> intro e <;> cases e + simp only [decodeBindNotification] + rw [pre, ndE_pure_bindD, run_bind_okD (tcbBoundRun_eqA hk hb), hnone] + simp only [Fl4Spec.NonDetE.returnOk, ndE_pure_bindD, List.head?_cons, the, Option.getD_some] + obtain ⟨zc, zsl⟩ := z + cases zc <;> first + | exact absurd rfl (hny _ _ _) + | exact run_bind_errorD (run_throwErrorD _ σ) _ + obtain ⟨p, b, sd, rc, hq⟩ := hNt + have hz : z.1 = .notificationCap (specPtr p) b.bv (rightsOfA sd rc false false) := by + have e := hcr.2 + rw [hq] at e + exact e.symm + obtain ⟨zc, zsl⟩ := z + simp only at hz + subst hz + have hpN : p ∈ N := hmem p b sd rc hsrc.1 hq + obtain ⟨nt, hnt, hrel⟩ := hN.ntfn p hpN + have hrcv : (AllowRecv ∈ rightsOfA sd rc false false) ↔ rc = true := by + cases sd <;> cases rc <;> simp [rightsOfA, AllowRecv] + simp only [hq] at hv + have run : (rc = true → ∀ (_ : True), (match nt.ntfnObj, nt.ntfnBoundTcb with + | .idleNtfn, none => False + | .activeNtfn _, none => False + | _, _ => True)) → x = .Sys .IllegalOperation → + DecEA σ x (decodeBindNotification (Z := Unit) (.threadCap (specPtr t)) + ((.notificationCap (specPtr p) b.bv (rightsOfA sd rc false false), zsl) :: zs)) := by + intro hc hx + refine done _ hx rfl ?_ + simp only [decodeBindNotification] + rw [pre, ndE_pure_bindD, run_bind_okD (tcbBoundRun_eqA hk hb), hnone] + simp only [Fl4Spec.NonDetE.returnOk, ndE_pure_bindD, List.head?_cons, the, Option.getD_some] + by_cases hr : rc = true + swap + · rw [show Fl4Spec.NonDetE.whenE (σ := Fl4SpecA64.State Unit) (AllowRecv ∉ rightsOfA sd rc false false) + (Fl4Spec.NonDetE.throwError Fl4SpecA64.SyscallError.illegalOperation) = + Fl4Spec.NonDetE.throwError .illegalOperation from if_pos (fun c => hr (hrcv.1 c))] + exact run_bind_errorD (run_throwErrorD _ σ) _ + rw [show Fl4Spec.NonDetE.whenE (σ := Fl4SpecA64.State Unit) (AllowRecv ∉ rightsOfA sd rc false false) + (Fl4Spec.NonDetE.throwError Fl4SpecA64.SyscallError.illegalOperation) = pure () from + if_neg (not_not.2 (hrcv.2 hr)), ndE_pure_bindD] + rw [run_bind_okD (run_liftE_stD (getNotification_eq hnt))] + have hc := hc hr trivial + revert hc + cases nt.ntfnObj <;> cases nt.ntfnBoundTcb <;> intro hc <;> first + | exact hc.elim + | exact run_bind_errorD (run_throwErrorD _ σ) _ + cases rc with + | false => + simp only [Bool.false_eq_true, ite_false, error.illegal] at hv + exact run (fun c => Bool.noConfusion c) (core.result.Result.Err.inj (ok_inj_ff hv).symm) + | true => + have hh : obj.Ntfn.head st p s = ok (L.mem s (epHeadA p)) := by + unfold obj.Ntfn.head + rw [show epHeadA p = core.num.U64.wrapping_add p layout.ntfn.HEAD by + unfold epHeadA; congr 1; with_unfolding_all rfl] + simp only [bind_ok, lift, L.read_eq] + have hbt : obj.Ntfn.bound_tcb st p s = ok (L.mem s (nBoundA p)) := by + unfold obj.Ntfn.bound_tcb nBoundA + simp only [bind_ok, lift, L.read_eq] + simp only [ite_true, hh, bind_ok] at hv + by_cases h1 : L.mem s (epHeadA p) = 0#u64 + swap + · have h1' : (L.mem s (epHeadA p) != 0#u64) = true := by simpa [bne_iff_ne] using h1 + simp only [h1', ite_true, error.illegal] at hv + refine run (fun _ _ => ?_) (core.result.Result.Err.inj (ok_inj_ff hv).symm) + have ho := hrel.obj + revert ho + cases nt.ntfnObj <;> cases nt.ntfnBoundTcb <;> intro ho <;> first + | exact trivial + | exact absurd ho.2.head h1 + | exact absurd ho.2.1.head h1 + simp only [h1, bne_self_eq_false, Bool.false_eq_true, ite_false, hbt, bind_ok] at hv + by_cases h2 : L.mem s (nBoundA p) = 0#u64 + · simp only [h2, bne_self_eq_false, Bool.false_eq_true, ite_false] at hv + exact absurd (ok_inj_ff hv) (by simp) + have h2' : (L.mem s (nBoundA p) != 0#u64) = true := by simpa [bne_iff_ne] using h2 + simp only [h2', ite_true, error.illegal] at hv + refine run (fun _ _ => ?_) (core.result.Result.Err.inj (ok_inj_ff hv).symm) + have hbd : nt.ntfnBoundTcb = some (specPtr (L.mem s (nBoundA p))) := by + rw [hrel.bound]; exact if_neg h2 + rw [hbd] + cases nt.ntfnObj <;> trivial + +end + +end + +end Fl4ProofsA64.P3.ReplyErr diff --git a/proofs/Fl4ProofsA64/P3/Stage3DecErrTcbPacA.lean b/proofs/Fl4ProofsA64/P3/Stage3DecErrTcbPacA.lean new file mode 100644 index 000000000..8c48d023e --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3DecErrTcbPacA.lean @@ -0,0 +1,115 @@ +/- +P3 (aarch64, A64-P3 Lift 1b, part C3): **decoder errors of SetPACKeys**. Every FL4 `Err` precedes the caller's +Restart. + +[P] `hPl` (`ArchPlat.pauth`). [S] `hst`. `hArg0` the spec's read of the flag word. New module. +-/ +import Fl4ProofsA64.P3.Stage3DecErrTcbSchedA +import Fl4ProofsA64.P3.Stage3DecErrTcbRegsA +import Fl4ProofsA64.P3.TcbInvokePacSelectA +import Fl4ProofsA64.P1.QueueS2Ty +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.ReplyErr +open fl4_core_a64 Fl4ProofsA64.P0 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P2.PathObl Fl4ProofsA64.P3 + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {s s' : S} {a a' : A} {t : U64} {ctx : tcb.Ctx} {x : error.Exc} {σ : Fl4SpecA64.State Unit} + {argsU : List U64} + +theorem tcbPac.decErrA (hst : smp.remote_tcb_stall st ar s a t = ok (s, a)) (hAr : TcbArgsA st s ctx argsU) + (hPl : ArchPlat ar) (buf : Option Fl4SpecA64.ObjRef) + (hArg0 : ∀ v, argsU[0]? = some v → (fl4Arg (Z := Unit) buf 0).run σ = ({(.ok v.bv, σ)}, False)) + (h : tcb.decode st ar s a fl4_abi.label.TCBSetPACKeys t ctx = ok (.Err x, s', a')) : + DecEA σ x (decodeFl4SetPacKeys (Z := Unit) (argsU.map UScalar.bv) buf (specPtr t)) := by + obtain ⟨hlen, hargs⟩ := hAr + rw [tcbSetPACKeys_programA hst] at h + simp only [tcb.decode_arch, ite_true] at h + unfold tcb.decode_set_pac_keys at h + by_cases h11 : ctx.length < 11#u64 + · simp only [if_pos h11, error.truncated, bind_ok] at h + cases tcbErr_injA h + refine ⟨.truncatedMessage, ?_, Or.inl trivial⟩ + have hl : (argsU.map UScalar.bv).length < 11 := by rw [List.length_map, ← hlen]; scalar_tac + simp only [decodeFl4SetPacKeys] + rw [show Fl4Spec.NonDetE.whenE (σ := Fl4SpecA64.State Unit) ((argsU.map UScalar.bv).length < 11) + (Fl4Spec.NonDetE.throwError Fl4SpecA64.SyscallError.truncatedMessage) = + Fl4Spec.NonDetE.throwError .truncatedMessage from if_pos hl] + exact run_bind_errorD (run_throwErrorD _ σ) _ + have hl : ¬ (argsU.map UScalar.bv).length < 11 := by rw [List.length_map, ← hlen]; scalar_tac + have h0 : 0 < argsU.length := by rw [List.length_map] at hl; omega + obtain ⟨v, hv⟩ : ∃ v, argsU[0]? = some v := ⟨_, List.getElem?_eq_getElem h0⟩ + have hf := hargs 0#u64 v (by simpa using hv) + have hS := hArg0 v hv + simp only [if_neg h11, hf, bind_ok, lift] at h + have hskip : Fl4Spec.NonDetE.whenE (σ := Fl4SpecA64.State Unit) ((argsU.map UScalar.bv).length < 11) + (Fl4Spec.NonDetE.throwError Fl4SpecA64.SyscallError.truncatedMessage) = pure () := if_neg hl + have hp : (pure () : SEMonad Unit Unit).run σ = ({(.ok (), σ)}, False) := rfl + have hm := tcbPacMask_eqA v + by_cases hM : v &&& ~~~fl4_abi.consts.pac_flag.MASK = 0#u64 + swap + · simp only [bne_iff_ne.mpr hM, ite_true, error.invalid_arg, bind_ok] at h + cases tcbErr_injA h + refine ⟨.invalidArgument 0, ?_, Or.inl (by exact rfl)⟩ + have hM' : v.bv &&& ~~~fl4PacFlagMask ≠ 0 := fun c => hM (hm.2 c) + simp only [decodeFl4SetPacKeys, hskip] + rw [run_bind_okD hp, run_bind_okD hS] + rw [show Fl4Spec.NonDetE.whenE (σ := Fl4SpecA64.State Unit) (v.bv &&& ~~~fl4PacFlagMask ≠ 0) + (Fl4Spec.NonDetE.throwError (Fl4SpecA64.SyscallError.invalidArgument 0)) = + Fl4Spec.NonDetE.throwError (.invalidArgument 0) from if_pos hM'] + exact run_bind_errorD (run_throwErrorD _ σ) _ + simp only [hM, bne_self_eq_false, Bool.false_eq_true, ite_false] at h + by_cases hz : v = 0#u64 + · simp only [hz, bne_self_eq_false, Bool.false_eq_true, ite_false] at h + tcb_noerr h + simp only [bne_iff_ne.mpr hz, ite_true, hPl.pauth, bind_ok] at h + cases hP : fl4A64Platform.hasPauth + swap + · simp only [hP, ite_true] at h + tcb_noerr h + simp only [hP, Bool.false_eq_true, ite_false, error.illegal, bind_ok] at h + cases tcbErr_injA h + refine ⟨.illegalOperation, ?_, Or.inl trivial⟩ + have hM' : v.bv &&& ~~~fl4PacFlagMask = 0 := hm.1 hM + have hz' : v.bv ≠ 0 := fun c => hz (UScalar.eq_equiv_bv_eq _ _ |>.2 c) + simp only [decodeFl4SetPacKeys, hskip] + rw [run_bind_okD hp, run_bind_okD hS] + rw [show Fl4Spec.NonDetE.whenE (σ := Fl4SpecA64.State Unit) (v.bv &&& ~~~fl4PacFlagMask ≠ 0) + (Fl4Spec.NonDetE.throwError (Fl4SpecA64.SyscallError.invalidArgument 0)) = pure () from if_neg (fun c => c hM')] + rw [run_bind_okD hp] + rw [show Fl4Spec.NonDetE.whenE (σ := Fl4SpecA64.State Unit) (v.bv ≠ 0 ∧ fl4A64Platform.hasPauth = false) + (Fl4Spec.NonDetE.throwError Fl4SpecA64.SyscallError.illegalOperation) = + Fl4Spec.NonDetE.throwError .illegalOperation from if_pos ⟨hz', hP⟩] + exact run_bind_errorD (run_throwErrorD _ σ) _ + +/-- The spec's `fl4Arg buf 0` of the caller is the first message word. -/ +theorem fl4Arg0_runA {σ : Fl4SpecA64.State Unit} {t : Fl4SpecA64.ObjRef} {y : Fl4SpecA64.Tcb} + (hc : σ.curThread = t) (hk : σ.kheap t = some (.tcb y)) (buf : Option Fl4SpecA64.ObjRef) + (hal : ∀ sb, buf = some sb → ∀ k, k < 128 → (bufAddrA sb k).toNat % 8 = 0) + (mi : Fl4SpecA64.MessageInfo) {args : List Fl4SpecA64.Data} + (hmrs : getMrs (Z := Unit) t buf mi σ = ({(args, σ)}, False)) {w : Fl4SpecA64.Data} (hw : args[0]? = some w) : + (fl4Arg (Z := Unit) buf 0).run σ = ({(.ok w, σ)}, False) := by + have e := single_fstA (hmrs.symm.trans (getMrs_eqA hk buf hal mi)) + have e0 : w = specArgA y.tcbArch.tcbContext.userRegs buf σ.machineState.underlyingMemory 0 := by + rw [e] at hw + unfold specMrsA at hw + rw [List.getElem?_take] at hw + split at hw + · rw [List.getElem?_append_left (by first | (simp [msg_registers, msgRegisters, Register.x2ToX5]; done) | (simp only [List.length_map, msg_registers]; decide) | decide)] at hw + simpa [specArgA, msg_registers, msgRegisters, Register.x2ToX5] using hw.symm + · cases hw + rw [e0] + unfold fl4Arg + have hGet : (Fl4Spec.NonDet.gets fun s : Fl4SpecA64.State Unit => s.curThread) σ = ({(σ.curThread, σ)}, False) := rfl + rw [run_bind_okD (run_liftE_stD hGet), hc] + exact run_liftE_stD (fl4GetMr_eqA hk buf hal 0 (by decide)) + +end + +end + +end Fl4ProofsA64.P3.ReplyErr diff --git a/proofs/Fl4ProofsA64/P3/Stage3DecErrTcbRegsA.lean b/proofs/Fl4ProofsA64/P3/Stage3DecErrTcbRegsA.lean new file mode 100644 index 000000000..03af13571 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3DecErrTcbRegsA.lean @@ -0,0 +1,376 @@ +/- +P3 (aarch64, A64-P3 Lift 1b, part C): **decoder errors of the Thread-cap labels, register family** (ReadRegisters, +WriteRegisters, CopyRegisters, Suspend, Resume, SetTLSBase, SetFlags, SetTimer). + +* `DecEA σ x d`: the spec decoder `d` returns at `σ` a syscall error related to FL4's `x`. +* `tcb*.decErrA`: per label, from a decode `Err` other than `Preempted`. Every such `Err` precedes the caller's Restart. +* `tcbArg_okA`, `tcbExtraWL_okA`, `notThreadCapA`: helpers shared with the other families. + +[S] `hst`: the target's stall is quiet (`JSyscallA.stallTA`). No other named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3DecErrBaseA +import Fl4ProofsA64.P3.TcbInvokeRegisterSelectA +import Fl4ProofsA64.P3.TcbInvokeCopySelectA +import Fl4ProofsA64.P3.TcbInvokeReadPostA +import Fl4ProofsA64.P3.TcbInvokeThreadFieldsA +import Fl4ProofsA64.P3.FrontEndExtraA +import Fl4ProofsA64.P1.LocalTimerSet +import Fl4ProofsA64.P2.ProgGenBase +import Fl4ProofsA64.P3.Stage3ErrKeep2A +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.ReplyErr +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P2.PathObl Fl4ProofsA64.P3 Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +/-- The spec decoder's error at `σ`, related to FL4's. -/ +def DecEA {β : Type} (σ : Fl4SpecA64.State Unit) (x : error.Exc) (d : Fl4SpecA64.SEMonad Unit β) : Prop := + ∃ e', d.run σ = ({(.error e', σ)}, False) ∧ VsErrRelA x e' + +/-- An `Err` leaf: the FL4 error is the one returned. -/ +theorem tcbErr_injA {S A : Type} {e x : error.Exc} {s s' : S} {a a' : A} + (h : (ok (.Err e, s, a) : Result (core.result.Result Unit error.Exc × S × A)) = ok (.Err x, s', a')) : x = e := by + have h2 := congrArg Prod.fst (ok_inj_ff h) + exact (core.result.Result.Err.inj h2).symm + +/-- Close a path that can only return `Ok`. -/ +macro "tcb_noerr" h:ident : tactic => `(tactic| + repeat' (first + | exact absurd (congrArg Prod.fst (ok_inj_ff $h)) (by simp) + | (obtain ⟨_, _, $h:ident⟩ := bind_eq_ok_std $h) + | (split at $h:ident) + | (dsimp only at $h:ident))) + +section +variable {σ : Fl4SpecA64.State Unit} + +theorem decE_throwA {β : Type} {x : error.Exc} {e : Fl4SpecA64.SyscallError} (h : VsErrRelA x e) : + DecEA σ x (Fl4Spec.NonDetE.throwError e : Fl4SpecA64.SEMonad Unit β) := + ⟨e, run_throwErrorD _ _, h⟩ + +/-- A non-Thread FL4 cap represents no spec thread cap. -/ +theorem notThreadCapA {c : capability.Cap} {y : Fl4SpecA64.Cap} (h : CapEqA c y) (hc : ∀ t, c ≠ .Thread t) : + ∀ p, y ≠ .threadCap p := by + intro p e + subst e + have h' : absCapA c = .threadCap p := by + unfold CapEqA at h + split at h <;> first | exact h | contradiction + cases c + case Thread t => exact hc t rfl + all_goals + revert h' + simp only [absCapA] + (repeat' split) <;> intro h' <;> cases h' + +end + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {s s' : S} {a a' : A} {t : U64} {ctx : tcb.Ctx} {x : error.Exc} {σ : Fl4SpecA64.State Unit} + {argsU : List U64} + +/-- The argument words of the context (`ArgsLabA`). -/ +abbrev TcbArgsA (st : machine.Store S) (s : S) (ctx : tcb.Ctx) (argsU : List U64) : Prop := + ctx.length.val = argsU.length ∧ ∀ (i : U64) v, argsU[i.val]? = some v → tcb.arg st s ctx i = ok v + +set_option maxHeartbeats 4000000 in +/-- After its length check, SetTLSBase errs only by `Preempted`. -/ +theorem tcbSetTLSBase_preA (c : tcb.Ctx) (h1 : ¬ c.length < 1#u64) : + tcb.decode_set_tls_base st s t c ⦃ x => PreOnlyA x.1 ⦄ := by + have hS : P0.StoreTotal st := L.total + unfold tcb.decode_set_tls_base + rw [if_neg h1] + pre_steps2 + all_goals (try pre_close) + all_goals pre_res + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 16000000 in +/-- After its length check, SetFlags errs only by `Preempted`. -/ +theorem tcbSetFlags_preA (hAr : P0.ArchTotal ar) (c : tcb.Ctx) (h2 : ¬ c.length < 2#u64) : + tcb.decode_set_flags st ar s a t c ⦃ x => PreOnlyA x.1 ⦄ := by + have hS : P0.StoreTotal st := L.total + unfold tcb.decode_set_flags + rw [if_neg h2] + pre_steps2 + all_goals (try pre_close) + all_goals pre_res + +theorem tcbReadRegisters.decErrA (hst : smp.remote_tcb_stall st ar s a t = ok (s, a)) + (hA : TcbArgsA st s ctx argsU) (hcur : σ.curThread = specPtr (L.curW s)) + (h : tcb.decode st ar s a fl4_abi.label.TCBReadRegisters t ctx = ok (.Err x, s', a')) : + DecEA σ x (decodeReadRegisters (Z := Unit) (argsU.map UScalar.bv) (.threadCap (specPtr t))) := by + obtain ⟨hlen, hargs⟩ := hA + by_cases h2 : ctx.length < 2#u64 + · rw [tcbReadRegisters_truncatedA hst h2] at h + cases tcbErr_injA h + have : argsU.length < 2 := by rw [← hlen]; scalar_tac + match argsU, this with + | [], _ => exact ⟨.truncatedMessage, run_throwErrorD _ _, Or.inl trivial⟩ + | [_], _ => exact ⟨.truncatedMessage, run_throwErrorD _ _, Or.inl trivial⟩ + have : 2 ≤ argsU.length := by rw [← hlen]; scalar_tac + match argsU, this, hargs with + | f :: n :: rest, _, hargs => + have hf := hargs 0#u64 f (by simp) + have hn := hargs 1#u64 n (by simp) + simp only [List.map_cons] + by_cases hr : n < 1#u64 ∨ 36#u64 < n + · rw [tcbReadRegisters_countErrorA hst h2 hf hn hr] at h + cases tcbErr_injA h + refine ⟨_, tcbReadRegisters_rangeSpecA σ _ _ _ _ ?_, Or.inl ⟨rfl, rfl⟩⟩ + intro ⟨h1, h36⟩ + have e1 : 1 ≤ n.val := by simpa [BitVec.le_def] using h1 + have e2 : n.val ≤ 36 := by simpa [BitVec.le_def] using h36 + rcases hr with hr | hr <;> scalar_tac + have hlo : ¬ n < 1#u64 := fun c => hr (Or.inl c) + have hhi : ¬ 36#u64 < n := fun c => hr (Or.inr c) + by_cases hself : t = L.curW s + · rw [tcbReadRegisters_selfA hst h2 hf hn hlo hhi hself] at h + cases tcbErr_injA h + have hR : n.bv ≥ 1 ∧ n.bv ≤ 36 := by + constructor + · show (1 : BitVec 64) ≤ n.bv + rw [BitVec.le_def]; simp; scalar_tac + · rw [BitVec.le_def]; simp; scalar_tac + refine ⟨.illegalOperation, ?_, Or.inl trivial⟩ + simp only [decodeReadRegisters, rangeCheck] + rw [show Fl4Spec.NonDetE.unlessE (σ := Fl4SpecA64.State Unit) (ε := Fl4SpecA64.SyscallError) + (n.bv ≥ 1 ∧ n.bv ≤ BitVec.ofNat 64 (frameRegisters.length + gpRegisters.length)) + (Fl4Spec.NonDetE.throwError (.rangeError 1 (BitVec.ofNat 64 (frameRegisters.length + gpRegisters.length)))) + = pure () from if_pos hR, ndE_pure_bindD] + rw [run_bind_okD (run_returnOkD (specPtr t) σ)] + rw [run_bind_okD (run_liftE_stD (show (Fl4Spec.NonDet.gets fun s : Fl4SpecA64.State Unit => s.curThread) σ = + ({(σ.curThread, σ)}, False) from rfl))] + rw [show Fl4Spec.NonDetE.whenE (σ := Fl4SpecA64.State Unit) (specPtr t = σ.curThread) + (Fl4Spec.NonDetE.throwError Fl4SpecA64.SyscallError.illegalOperation) = + Fl4Spec.NonDetE.throwError .illegalOperation from if_pos (by rw [hcur, hself])] + exact run_bind_errorD (run_throwErrorD _ σ) _ + · rw [tcbReadRegisters_programA hst] at h + unfold tcb.decode_read_registers at h + simp only [if_neg h2, hf, hn, bind_ok, show tcb.N_FRAME + tcb.N_GP = ok 36#u64 by + rw [tcb.N_FRAME, tcb.N_GP]; rfl, if_neg hlo, if_neg (show ¬ n > 36#u64 from hhi), cur_thread_ok, + obj.Tcb.Insts.CoreCmpPartialEqTcb.eq, decide_eq_false hself, Bool.false_eq_true, ite_false] at h + tcb_noerr h + +theorem tcbWriteRegisters.decErrA (hst : smp.remote_tcb_stall st ar s a t = ok (s, a)) + (hA : TcbArgsA st s ctx argsU) (hcur : σ.curThread = specPtr (L.curW s)) + (h : tcb.decode st ar s a fl4_abi.label.TCBWriteRegisters t ctx = ok (.Err x, s', a')) : + DecEA σ x (decodeWriteRegisters (Z := Unit) (argsU.map UScalar.bv) (.threadCap (specPtr t))) := by + obtain ⟨hlen, hargs⟩ := hA + by_cases h2 : ctx.length < 2#u64 + · rw [tcbWriteRegisters_truncatedA hst h2] at h + cases tcbErr_injA h + have : argsU.length < 2 := by rw [← hlen]; scalar_tac + match argsU, this with + | [], _ => exact ⟨.truncatedMessage, run_throwErrorD _ _, Or.inl trivial⟩ + | [_], _ => exact ⟨.truncatedMessage, run_throwErrorD _ _, Or.inl trivial⟩ + have : 2 ≤ argsU.length := by rw [← hlen]; scalar_tac + match argsU, this, hargs, hlen with + | f :: n :: rest, _, hargs, hlen => + have hf := hargs 0#u64 f (by simp) + have hn := hargs 1#u64 n (by simp) + obtain ⟨tl, htl, htlv⟩ := spec_existsA (U64.sub_spec (x := ctx.length) (y := 2#u64) (by scalar_tac)) + have htl' : tl.val = rest.length := by simp at hlen; scalar_tac + simp only [List.map_cons] + have hun : unat n.bv = n.val := rfl + by_cases hs : tl < n + · rw [tcbWriteRegisters_countErrorA hst h2 hf hn htl hs] at h + cases tcbErr_injA h + refine ⟨.truncatedMessage, ?_, Or.inl trivial⟩ + simp only [decodeWriteRegisters] + rw [show Fl4Spec.NonDetE.whenE (σ := Fl4SpecA64.State Unit) ((rest.map UScalar.bv).length < unat n.bv) + (Fl4Spec.NonDetE.throwError Fl4SpecA64.SyscallError.truncatedMessage) = + Fl4Spec.NonDetE.throwError .truncatedMessage from if_pos (by rw [hun, List.length_map]; scalar_tac)] + exact run_bind_errorD (run_throwErrorD _ σ) _ + by_cases hself : t = L.curW s + · rw [tcbWriteRegisters_selfA hst h2 hf hn htl hs hself] at h + cases tcbErr_injA h + refine ⟨.illegalOperation, ?_, Or.inl trivial⟩ + simp only [decodeWriteRegisters] + rw [show Fl4Spec.NonDetE.whenE (σ := Fl4SpecA64.State Unit) ((rest.map UScalar.bv).length < unat n.bv) + (Fl4Spec.NonDetE.throwError Fl4SpecA64.SyscallError.truncatedMessage) = pure () from + if_neg (by rw [hun, List.length_map]; scalar_tac), ndE_pure_bindD] + rw [run_bind_okD (run_returnOkD (specPtr t) σ)] + rw [run_bind_okD (run_liftE_stD (show (Fl4Spec.NonDet.gets fun s : Fl4SpecA64.State Unit => s.curThread) σ = + ({(σ.curThread, σ)}, False) from rfl))] + rw [show Fl4Spec.NonDetE.whenE (σ := Fl4SpecA64.State Unit) (specPtr t = σ.curThread) + (Fl4Spec.NonDetE.throwError Fl4SpecA64.SyscallError.illegalOperation) = + Fl4Spec.NonDetE.throwError .illegalOperation from if_pos (by rw [hcur, hself])] + exact run_bind_errorD (run_throwErrorD _ σ) _ + · rw [tcbWriteRegisters_programA hst] at h + unfold tcb.decode_write_registers at h + simp only [if_neg h2, hf, hn, htl, bind_ok, if_neg hs, cur_thread_ok, + obj.Tcb.Insts.CoreCmpPartialEqTcb.eq, decide_eq_false hself, Bool.false_eq_true, ite_false] at h + tcb_noerr h + +theorem tcbSuspend.decErrA (hst : smp.remote_tcb_stall st ar s a t = ok (s, a)) + (h : tcb.decode st ar s a fl4_abi.label.TCBSuspend t ctx = ok (.Err x, s', a')) : False := by + rw [tcbSuspend_programA hst] at h + tcb_noerr h + +theorem tcbResume.decErrA (hst : smp.remote_tcb_stall st ar s a t = ok (s, a)) + (h : tcb.decode st ar s a fl4_abi.label.TCBResume t ctx = ok (.Err x, s', a')) : False := by + rw [tcbResume_programA hst] at h + tcb_noerr h + +theorem tcbSetTLSBase.decErrA (hst : smp.remote_tcb_stall st ar s a t = ok (s, a)) (hne : x ≠ .Preempted) + (hA : TcbArgsA st s ctx argsU) + (h : tcb.decode st ar s a fl4_abi.label.TCBSetTLSBase t ctx = ok (.Err x, s', a')) : + DecEA σ x (decodeSetTlsBase (Z := Unit) (argsU.map UScalar.bv) (.threadCap (specPtr t))) := by + obtain ⟨hlen, -⟩ := hA + by_cases h1 : ctx.length < 1#u64 + · rw [tcbSetTLSBase_truncatedA hst h1] at h + cases tcbErr_injA h + have : argsU = [] := List.length_eq_zero_iff.1 (by rw [← hlen]; scalar_tac) + subst this + refine ⟨.truncatedMessage, ?_, Or.inl trivial⟩ + simp only [decodeSetTlsBase] + rw [show Fl4Spec.NonDetE.whenE (σ := Fl4SpecA64.State Unit) ((([] : List U64).map UScalar.bv).length = 0) + (Fl4Spec.NonDetE.throwError Fl4SpecA64.SyscallError.truncatedMessage) = + Fl4Spec.NonDetE.throwError .truncatedMessage from if_pos rfl] + exact run_bind_errorD (run_throwErrorD _ σ) _ + · rw [tcbSetTLSBase_programA hst] at h + obtain ⟨⟨r, s1⟩, h2, h⟩ := bind_eq_ok_std h + have : r = .Err x := congrArg Prod.fst (ok_inj_ff h) + subst this + have K := tcbSetTLSBase_preA (st := st) (s := s) (t := t) ctx h1 + rw [h2, WP.spec_ok] at K + exact absurd (K x rfl) hne + +theorem tcbSetFlags.decErrA (hAr : P0.ArchTotal ar) (hne : x ≠ .Preempted) (hst : smp.remote_tcb_stall st ar s a t = ok (s, a)) + (hA : TcbArgsA st s ctx argsU) + (h : tcb.decode st ar s a fl4_abi.label.TCBSetFlags t ctx = ok (.Err x, s', a')) : + DecEA σ x (decodeFl4SetFlags (Z := Unit) (argsU.map UScalar.bv) (specPtr t)) := by + obtain ⟨hlen, -⟩ := hA + rw [tcbSetFlags_programA hst] at h + by_cases h2 : ctx.length < 2#u64 + · unfold tcb.decode_set_flags at h + simp only [if_pos h2, error.truncated, bind_ok] at h + cases tcbErr_injA h + refine ⟨.truncatedMessage, ?_, Or.inl trivial⟩ + simp only [decodeFl4SetFlags] + rw [show Fl4Spec.NonDetE.whenE (σ := Fl4SpecA64.State Unit) ((argsU.map UScalar.bv).length < 2) + (Fl4Spec.NonDetE.throwError Fl4SpecA64.SyscallError.truncatedMessage) = + Fl4Spec.NonDetE.throwError .truncatedMessage from if_pos (by rw [List.length_map, ← hlen]; scalar_tac)] + exact run_bind_errorD (run_throwErrorD _ σ) _ + · have K := tcbSetFlags_preA (st := st) (ar := ar) (s := s) (a := a) (t := t) hAr ctx h2 + rw [h, WP.spec_ok] at K + exact absurd (K x rfl) hne + +theorem tcbSetTimer.decErrA (hst : smp.remote_tcb_stall st ar s a t = ok (s, a)) + (hA : TcbArgsA st s ctx argsU) + (h : tcb.decode st ar s a fl4_abi.label.TCBSetTimer t ctx = ok (.Err x, s', a')) : + DecEA σ x (decodeFl4SetTimer (Z := Unit) (argsU.map UScalar.bv) (specPtr t)) := by + obtain ⟨hlen, -⟩ := hA + by_cases h2 : ctx.length < 2#u64 + · refine ⟨.truncatedMessage, ?_, ?_⟩ + · simp only [decodeFl4SetTimer] + rw [show Fl4Spec.NonDetE.whenE (σ := Fl4SpecA64.State Unit) ((argsU.map UScalar.bv).length < 2) + (Fl4Spec.NonDetE.throwError Fl4SpecA64.SyscallError.truncatedMessage) = + Fl4Spec.NonDetE.throwError .truncatedMessage from if_pos (by rw [List.length_map, ← hlen]; scalar_tac)] + exact run_bind_errorD (run_throwErrorD _ σ) _ + · unfold tcb.decode at h + rw [hst] at h + simp (config := { decide := true }) only [bind_ok, uncurry_apply_pair, fl4_abi.label.TCBSetTimer, + fl4_abi.label.TCBSetPACKeys, fl4_abi.label.TCBSetHighVSpace, fl4_abi.label.TCBSetEPTRoot, + fl4_abi.label.TCBReadRegisters, fl4_abi.label.TCBWriteRegisters, fl4_abi.label.TCBCopyRegisters, + fl4_abi.label.TCBSuspend, fl4_abi.label.TCBResume, fl4_abi.label.TCBConfigure, + fl4_abi.label.TCBSetPriority, fl4_abi.label.TCBSetMCPriority, fl4_abi.label.TCBSetSchedParams, + fl4_abi.label.TCBSetIPCBuffer, fl4_abi.label.TCBSetSpace, fl4_abi.label.TCBSetTimeoutEndpoint, + fl4_abi.label.TCBBindNotification, fl4_abi.label.TCBUnbindNotification, fl4_abi.label.TCBSetTLSBase, + fl4_abi.label.TCBSetFlags, fl4_abi.label.TCBReadFPU, fl4_abi.label.TCBWriteFPU, + fl4_abi.label.TCBSetBreakpoint, fl4_abi.label.TCBGetBreakpoint, fl4_abi.label.TCBUnsetBreakpoint, + fl4_abi.label.TCBConfigureSingleStepping, core.num.U64.MAX, if_pos h2, error.truncated, ↓reduceIte, if_true, + if_false] at h + cases tcbErr_injA h + exact Or.inl trivial + · rw [Fl4ProofsA64.P1.decode_set_timer ar hst h2] at h + tcb_noerr h + +/-- The extra slot `i` with the length check (`ExtraOKA`). -/ +theorem tcbExtraWL_okA {th : TypedHeapA} {kb : U64} (hP : PlaceA th kb) + {excaps : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)} (hX : ExtraOKA th (L.mem s) ctx.extra excaps) + (i : Usize) (hi : i.val < 3) (n : U64) : + tcb.extra_with_length ctx i n = ok (if i.val < excaps.length ∧ n ≤ ctx.length then + .Ok (exAtA ctx.extra i.val) else .Err (.Sys .TruncatedMessage)) := by + have hidx : Aeneas.Std.Array.index_usize ctx.extra i = ok (exAtA ctx.extra i.val) := by + have hl : ctx.extra.val.length = 3 := by simpa using ctx.extra.property + have : ctx.extra.val[i.val]? = some (ctx.extra.val[i.val]'(by omega)) := List.getElem?_eq_getElem _ + simp [Aeneas.Std.Array.index_usize, exAtA, List.getD_eq_getElem?_getD, this] + have h3 : ¬ i ≥ 3#usize := by scalar_tac + have hz : exAtA ctx.extra i.val = 0#u64 ↔ ¬ i.val < excaps.length := by + constructor + · intro e c + exact (srcOK_factsA hP (hX.2.1 _ c)).1 e + · intro c; exact hX.2.2 _ (by omega) hi + unfold tcb.extra_with_length tcb.extra + simp only [if_neg h3, hidx, bind_ok, obj.Slot.is_null] + by_cases c : i.val < excaps.length + · have c0 : ¬ exAtA ctx.extra i.val = 0#u64 := fun e => hz.1 e c + simp only [c0, decide_false, Bool.false_eq_true, ite_false, bind_ok, c, true_and] + by_cases hn : n ≤ ctx.length + · rw [if_pos (show ctx.length ≥ n from hn), if_pos hn] + · rw [if_neg (show ¬ ctx.length ≥ n from hn), if_neg hn]; rfl + · have c0 : exAtA ctx.extra i.val = 0#u64 := hz.2 c + simp only [c0, decide_true, ite_true, bind_ok, c, false_and, ite_false] + rfl + +set_option maxHeartbeats 1000000 in +theorem tcbCopyRegisters.decErrA (hAr : P0.ArchTotal ar) (hne : x ≠ .Preempted) (hst : smp.remote_tcb_stall st ar s a t = ok (s, a)) + (hA : TcbArgsA st s ctx argsU) {th : TypedHeapA} {kb : U64} (hP : PlaceA th kb) + {excaps : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)} (hX : ExtraOKA th (L.mem s) ctx.extra excaps) + (h : tcb.decode st ar s a fl4_abi.label.TCBCopyRegisters t ctx = ok (.Err x, s', a')) : + DecEA σ x (decodeCopyRegisters (Z := Unit) (argsU.map UScalar.bv) (.threadCap (specPtr t)) + (excaps.map Prod.fst)) := by + obtain ⟨hlen, hargs⟩ := hA + have he := tcbExtraWL_okA (L := L) (ctx := ctx) hP hX 0#usize (by simp) 1#u64 + by_cases hc : (0#usize).val < excaps.length ∧ 1#u64 ≤ ctx.length + swap + · rw [if_neg hc] at he + rw [tcbCopyRegisters_truncatedA hst he] at h + cases tcbErr_injA h + refine ⟨.truncatedMessage, ?_, Or.inl trivial⟩ + cases argsU with + | nil => exact (tcbCopyRegisters_truncatedSpecA σ _ 0 [] _).1 + | cons f rest => + have : excaps = [] := by + apply List.length_eq_zero_iff.1 + simp at hlen hc + by_contra hh + have := hc (by omega) + scalar_tac + subst this + exact (tcbCopyRegisters_truncatedSpecA σ _ f.bv (rest.map UScalar.bv) []).2 + rw [if_pos hc] at he + obtain ⟨h0, h1⟩ := hc + simp only [UScalar.ofNatCore_val_eq] at he h0 + match excaps, hX, h0, argsU, hargs, (show 1 ≤ argsU.length by rw [← hlen]; scalar_tac) with + | y :: ys, hX, _, f :: rest, hargs, _ => + have hf := hargs 0#u64 f (by simp) + have hcr := (srcOK_factsA hP (hX.2.1 0 (by simp))).2.2.2.1 + rw [capAtA_eq (st := st)] at hcr + simp only [List.getElem_cons_zero] at hcr + have hcap := capA_eq (st := st) s (exAtA ctx.extra 0) + simp only [List.map_cons] + by_cases hth : ∃ p, capA st s (exAtA ctx.extra 0) = .Thread p + · obtain ⟨p, hp⟩ := hth + rw [tcbCopyRegisters_programA hst] at h + unfold tcb.decode_copy_registers at h + simp only [he, hf, tcb.thread_arg, hcap, hp, bind_ok, core.result.Result.Insts.CoreOpsTry.branch] at h + obtain ⟨⟨s1, a1⟩, -, h⟩ := bind_eq_ok_std h + obtain ⟨s2, -, h⟩ := bind_eq_ok_std h + have K := tcb.copy_registers.preA (st := st) (ar := ar) hAr s2 a1 t p f + rw [h, WP.spec_ok] at K + exact absurd (K x rfl) hne + · have hk : ∀ p, capA st s (exAtA ctx.extra 0) ≠ .Thread p := fun p e => hth ⟨p, e⟩ + rw [tcbCopyRegisters_badSourceA hst he hf hcap hk] at h + cases tcbErr_injA h + exact ⟨_, tcbCopyRegisters_badSourceSpecA σ _ _ _ _ _ (notThreadCapA hcr.2 hk), Or.inr ⟨1#u64, rfl, rfl⟩⟩ + +end + +end + +end Fl4ProofsA64.P3.ReplyErr diff --git a/proofs/Fl4ProofsA64/P3/Stage3DecErrTcbRootsA.lean b/proofs/Fl4ProofsA64/P3/Stage3DecErrTcbRootsA.lean new file mode 100644 index 000000000..663ec96b5 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3DecErrTcbRootsA.lean @@ -0,0 +1,377 @@ +/- +P3 (aarch64, A64-P3 Lift 1b, part C3): **`derive_roots` against the spec's CSpace / VSpace root decode** (shared by +SetSpace and Configure). + +* `slotLongRun_relA`: `slot_cap_long_running_delete` is `slotCapLongRunningDelete`. +* `deriveData_relA`: `derive_with_data` is `deriveCap` of the updated cap. +* `cvProg P`: the spec's root decode with VTable root predicate `P` (`decodeCvSpace`, `fl4DecodeCvSpace`). +* `deriveRoots_relA`: `derive_roots`' outcome against `cvProg`. + +`F` the finality state facts; `hnc`: `ensure_no_children` at an untyped source. New module. +-/ +import Fl4ProofsA64.P3.Stage3DecErrTcbBufA +import Fl4ProofsA64.P3.TcbInvokeRootsA +import Fl4ProofsA64.P3.CnodeDelDefA +import Fl4ProofsA64.P3.FrontEndExtraA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.ReplyErr +open fl4_core_a64 Fl4ProofsA64.P0 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P2.PathObl Fl4ProofsA64.P3 + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Endp Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {s : S} {σ : Fl4SpecA64.State Unit} {th : TypedHeapA} + +theorem tcb_slot1A (t : U64) : + obj.Tcb.slot t layout.tcb.SLOT_VTABLE = ok (cslotAddrA (specPtr t, tcbCnodeIndex 1)) := by + have e : cslotAddrA (specPtr t, tcbCnodeIndex 1) = ⟨t.bv + 1#64 * 32#64⟩ := by + unfold cslotAddrA + rw [ofBl_tcbIdx 1 (by omega), slotAddrA_sp] + rw [e] + unfold obj.Tcb.slot + simp only [lift, bind_ok] + congr 1 + apply UScalar.eq_of_val_eq + have hv : (⟨t.bv + 1#64 * 32#64⟩ : U64).val = (t.val + 32) % 2 ^ 64 := by + show (t.bv + 1#64 * 32#64).toNat = _ + rw [BitVec.toNat_add]; rfl + rw [hv] + simp [core.num.U64.wrapping_add_val_eq, core.num.U64.wrapping_mul_val_eq, layout.tcb.SLOT_VTABLE, U64.size_def, + U64.numBits] + +theorem cslot0A (t : U64) : cslotAddrA (specPtr t, tcbCnodeIndex 0) = t := by + unfold cslotAddrA + rw [ofBl_tcbIdx 0 (by omega), slotAddrA_sp] + apply UScalar.eq_of_val_eq; simp + +theorem slotLongRun_relA {OS : Set U64} {G : List (List U64)} (F : FinStA st th OS G s σ) {sl : Fl4SpecA64.CslotPtr} + (hsl : AbsSlotA th sl) : + ∃ b, cdt.slot_cap_long_running_delete st s (cslotAddrA sl) = ok b ∧ + slotCapLongRunningDelete (Z := Unit) sl σ = ({(b, σ)}, False) := by + obtain ⟨ac, hg, hace⟩ := getCapOne_eqA F.rel hsl + obtain ⟨c, hc⟩ : ∃ c, capA st s (cslotAddrA sl) = c := ⟨_, rfl⟩ + rw [hc] at hace + have hwf : CapWfA c := hc ▸ F.wf (F.sub _ (hsl.isSlot F.place)) + unfold cdt.slot_cap_long_running_delete slotCapLongRunningDelete + simp only [capA_eq, hc, bind_ok] + rw [nd_bind_singleA hg] + by_cases hcn : c = .Null + · subst hcn + have hacn : ac = Fl4SpecA64.Cap.nullCap := (hace.eq_abs fun _ _ _ e => by cases e).symm + subst hacn + exact ⟨false, by simp only [cap_is_null_nullA, bind_ok, ↓reduceIte], rfl⟩ + have hcan : ac ≠ .nullCap := hace.ne_null hcn hwf + rw [cap_is_null_neA hcn] + simp only [bind_ok, Bool.false_eq_true, ↓reduceIte] + have hspec : ∀ (b : Bool), (decide (isFinalCap' ac σ) && longRunningDelete ac) = b → + (isFinalCap ac >>= fun final => (pure (final && longRunningDelete ac) : SMonad Unit Bool)) σ = + ({(b, σ)}, False) := by + intro b hb + subst hb + unfold isFinalCap + rw [nd_bind_singleA (a := decide (isFinalCap' ac σ)) (by rfl)] + rfl + by_cases hk : (∃ p r gs g, c = .CNode p r gs g) ∨ (∃ p, c = .Thread p) ∨ (∃ p t n, c = .Zombie p t n) + · have hN : NeedsFinCA c := by + rcases hk with ⟨_, _, _, _, rfl⟩ | ⟨_, rfl⟩ | ⟨_, _, _, rfl⟩ <;> trivial + obtain ⟨⟨b, hb, e⟩, -⟩ := finalHypA th OS G s σ sl c F hsl hc hN + have hld : longRunningDelete ac = true := by + have ha : ac = absCapA c := (hace.eq_abs (by + intro p asid va e; subst e + rcases hk with ⟨_, _, _, _, h⟩ | ⟨_, h⟩ | ⟨_, _, _, h⟩ <;> cases h)).symm + rw [ha] + rcases hk with ⟨_, _, _, _, rfl⟩ | ⟨_, rfl⟩ | ⟨_, _, _, rfl⟩ <;> rfl + refine ⟨b, ?_, hspec b ?_⟩ + · rw [hb] + simp only [bind_ok] + cases b + · rfl + · rcases hk with ⟨_, _, _, _, rfl⟩ | ⟨_, rfl⟩ | ⟨_, _, _, rfl⟩ <;> rfl + · rw [hld, Bool.and_true] + have := e.trans (isFinalCap'_eqA hace σ).symm + cases b <;> simp_all + · obtain ⟨b, hb, -⟩ := spec_existsA (P0.cdt.is_final_capability_with_cap.p0 st s (cslotAddrA sl) c L.total) + have hld : longRunningDelete ac = false := by + rcases hace.eq_or_pt with e | ⟨_, _, _, e⟩ + · rw [e] + cases c + case CNode => exact absurd (Or.inl ⟨_, _, _, _, rfl⟩) hk + case Thread => exact absurd (Or.inr (Or.inl ⟨_, rfl⟩)) hk + case Zombie => exact absurd (Or.inr (Or.inr ⟨_, _, _, rfl⟩)) hk + case Frame => simp only [absCapA, absFrameE]; split <;> rfl + all_goals rfl + · rw [e]; rfl + refine ⟨false, ?_, hspec false (by rw [hld, Bool.and_false])⟩ + rw [hb] + simp only [bind_ok] + cases b + · rfl + · cases c + case CNode => exact absurd (Or.inl ⟨_, _, _, _, rfl⟩) hk + case Thread => exact absurd (Or.inr (Or.inl ⟨_, rfl⟩)) hk + case Zombie => exact absurd (Or.inr (Or.inr ⟨_, _, _, rfl⟩)) hk + all_goals rfl + +theorem deriveData_relA {slot : obj.Slot} {sl : Fl4SpecA64.CslotPtr} {y : Fl4SpecA64.Cap} (d : U64) + (hcr : CapRelA (capA st s slot) y) + (hnc : isUtA (capA st s slot) = true → NoChildrenRelA (st := st) s σ slot sl) : + ∃ r r', tcb.derive_with_data st s slot d = ok r ∧ + (deriveCap (Z := Unit) sl ((if d.bv = 0 then id else updateCapData false d.bv) y)).run σ = ({(r', σ)}, False) ∧ + (match r, r' with + | .Ok c', .ok a => CapWfA c' ∧ CapEqA c' a + | .Err e, .error e' => SysErrRelA e e' + | _, _ => False) := by + have fin : ∀ (c : capability.Cap) (z : Fl4SpecA64.Cap), CapRelA c z → CapRt.EncWf c → + (isUtA c = true → NoChildrenRelA (st := st) s σ slot sl) → + ∃ r r', objects.derive_cap st s slot c = ok r ∧ (deriveCap (Z := Unit) sl z).run σ = ({(r', σ)}, False) ∧ + (match r, r' with + | .Ok c', .ok a => CapWfA c' ∧ CapEqA c' a + | .Err e, .error e' => SysErrRelA e e' + | _, _ => False) := by + intro c z hz he hn + obtain ⟨r, hr, r', hr', hrel⟩ := derive_relEqA (st := st) s σ slot sl c z hz he hn + refine ⟨r, r', hr, hr', ?_⟩ + cases r <;> cases r' <;> first | exact ⟨hrel.1, hrel.2.1⟩ | exact hrel + have hcap := capA_eq (st := st) s slot + have hew : CapRt.EncWf (capA st s slot) := CapRt.decodeF_encWf _ _ + unfold tcb.derive_with_data + simp only [hcap, bind_ok] + by_cases hd : d = 0#u64 + · have hd' : d.bv = 0 := by rw [hd]; rfl + have : (d != 0#u64) = false := by rw [hd]; rfl + simp only [this, Bool.false_eq_true, ite_false, bind_ok, if_pos hd', id_eq] + exact fin _ y hcr hew hnc + have hd' : d.bv ≠ 0 := fun c => hd (UScalar.eq_equiv_bv_eq _ _ |>.2 c) + have : (d != 0#u64) = true := bne_iff_ne.2 hd + simp only [this, ite_true, if_neg hd'] + obtain ⟨c2, hu, hw2, ha2, he2, -, -, hs2⟩ := update_relA false d _ hcr.1 hew + rw [hu] + simp only [bind_ok] + have hut : isUtA c2 = true → isUtA (capA st s slot) = true := by + intro h + obtain ⟨p, b, i, e, hc2⟩ := isUtA_ut h + have hs := hs2 (by rw [hc2]; exact fun c => by cases c) + rw [hc2] at hs + revert hs + cases capA st s slot <;> simp [shapeA, isUtA] + have h2 : CapRelA c2 (updateCapData false d.bv y) := by + refine ⟨hw2, ?_⟩ + by_cases hpt : ∃ p asid va, capA st s slot = .PageTable p true asid va + · obtain ⟨p, asid, va, e⟩ := hpt + have hq := hcr.2 + rw [e] at hq hu + have hc2 : c2 = .PageTable p true asid va := (ok_inj_ff hu).symm + obtain ⟨asid', va', hy, -, -⟩ := CapEqA.pt_mapped_iff.mp hq + have : updateCapData false d.bv y = y := by rw [hy]; rfl + rw [this, hc2] + exact hq + · have hab : absCapA (capA st s slot) = y := by + have hq := hcr.2 + unfold CapEqA at hq + split at hq + · exact absurd ⟨_, _, _, by assumption⟩ hpt + · exact hq + exact CapEqA.of_abs (by rw [ha2, hab]) + exact fin c2 _ h2 he2 (fun h => hnc (hut h)) + +/-- The spec's CSpace / VSpace root decode, with VTable root predicate `P`. -/ +def cvProg (P : Fl4SpecA64.Cap → Bool) (args : List Fl4SpecA64.Data) (cap : Fl4SpecA64.Cap) + (slot : Fl4SpecA64.CslotPtr) (excaps : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) : + SEMonad Unit TcbInvocation := do + NonDetE.whenE (args.length < 2 ∨ excaps.length < 2) <| NonDetE.throwError .truncatedMessage + let canChgCr ← NonDetE.liftE <| NonDet.liftM not <| slotCapLongRunningDelete (getTcbCtablePtr (objRefOf cap)) + let canChgVr ← NonDetE.liftE <| NonDet.liftM not <| slotCapLongRunningDelete (getTcbVtablePtr (objRefOf cap)) + NonDetE.unlessE (canChgCr ∧ canChgVr) <| NonDetE.throwError .illegalOperation + let croot ← deriveCap (the excaps[0]?).2 + ((if the args[0]? = 0 then id else updateCapData false (the args[0]?)) (the excaps[0]?).1) + NonDetE.unlessE (isCnodeCap croot) <| NonDetE.throwError .illegalOperation + let vroot ← deriveCap (the excaps[1]?).2 + ((if the args[1]? = 0 then id else updateCapData false (the args[1]?)) (the excaps[1]?).1) + NonDetE.unlessE (P vroot) <| NonDetE.throwError .illegalOperation + NonDetE.returnOk <| .threadControlCaps (objRefOf cap) slot none none + (some (croot, (the excaps[0]?).2)) (some (vroot, (the excaps[1]?).2)) none + +theorem isCnode_relA {c : capability.Cap} {y : Fl4SpecA64.Cap} (hw : CapWfA c) (h : CapEqA c y) : + tcb.is_cnode c = ok (isCnodeCap y) := by + rcases h.eq_or_pt with e | ⟨_, _, _, e⟩ + · subst e + cases c <;> first | rfl | exact hw.elim | skip + · have : ∃ p a v, c = .PageTable p true a v := by + unfold CapEqA at h + split at h + · exact ⟨_, _, _, rfl⟩ + · rw [e] at h + cases c with + | PageTable p m asid va => + cases m with + | false => simp [absCapA, mapDataA] at h + | true => exact ⟨_, _, _, rfl⟩ + | Frame => + simp only [absCapA, absFrameE] at h + split at h <;> cases h + | _ => simp [absCapA] at h + obtain ⟨p, a, v, rfl⟩ := this + rw [e]; rfl + +theorem deriveRoots_relA {OS : Set U64} {G : List (List U64)} (F : FinStA st th OS G s σ) {t : U64} + (ht : th t = some .tcb) {cdata vdata : U64} {cs vs : obj.Slot} {sl slot : Fl4SpecA64.CslotPtr} + {sl1 : Fl4SpecA64.CslotPtr} {y y1 : Fl4SpecA64.Cap} {rest : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)} + {args : List Fl4SpecA64.Data} (hcr : CapRelA (capA st s cs) y) (hcr1 : CapRelA (capA st s vs) y1) + (hnc : isUtA (capA st s cs) = true → NoChildrenRelA (st := st) s σ cs sl) + (hnc1 : isUtA (capA st s vs) = true → NoChildrenRelA (st := st) s σ vs sl1) + (P : Fl4SpecA64.Cap → Bool) + (hP : ∀ c z, tcb.derive_with_data st s vs vdata = ok (.Ok c) → CapWfA c → CapEqA c z → + (deriveCap (Z := Unit) sl1 ((if vdata.bv = 0 then id else updateCapData false vdata.bv) y1)).run σ = + ({(.ok z, σ)}, False) → vspace.is_valid_vtable_root c = ok (P z)) + {r : core.result.Result (capability.Cap × capability.Cap) error.Exc} + (h : tcb.derive_roots st s t cdata vdata cs vs = ok r) : + match r with + | .Ok _ => ∃ v, (cvProg P (cdata.bv :: vdata.bv :: args) (.threadCap (specPtr t)) slot + ((y, sl) :: (y1, sl1) :: rest)).run σ = ({(.ok v, σ)}, False) + | .Err x => ∃ e', (cvProg P (cdata.bv :: vdata.bv :: args) (.threadCap (specPtr t)) slot + ((y, sl) :: (y1, sl1) :: rest)).run σ = ({(.error e', σ)}, False) ∧ VsErrRelA x e' := by + have hA0 : AbsSlotA th (specPtr t, tcbCnodeIndex 0) := ⟨t, rfl, Or.inr ⟨ht, 0, by decide, rfl⟩⟩ + have hA1 : AbsSlotA th (specPtr t, tcbCnodeIndex 1) := ⟨t, rfl, Or.inr ⟨ht, 1, by decide, rfl⟩⟩ + obtain ⟨b0, hb0, hs0⟩ := slotLongRun_relA F hA0 + obtain ⟨b1, hb1, hs1⟩ := slotLongRun_relA F hA1 + rw [cslot0A] at hb0 + have hr0 : (NonDetE.liftE (ε := SyscallError) <| NonDet.liftM not <| + slotCapLongRunningDelete (Z := Unit) (getTcbCtablePtr (specPtr t))).run σ = ({(.ok (!b0), σ)}, False) := + run_liftE_stD (by + show (slotCapLongRunningDelete (Z := Unit) (specPtr t, tcbCnodeIndex 0) >>= fun x => pure (!x)) σ = _ + rw [nd_bind_singleA hs0]; rfl) + have hr1 : (NonDetE.liftE (ε := SyscallError) <| NonDet.liftM not <| + slotCapLongRunningDelete (Z := Unit) (getTcbVtablePtr (specPtr t))).run σ = ({(.ok (!b1), σ)}, False) := + run_liftE_stD (by + show (slotCapLongRunningDelete (Z := Unit) (specPtr t, tcbCnodeIndex 1) >>= fun x => pure (!x)) σ = _ + rw [nd_bind_singleA hs1]; rfl) + have hskip : NonDetE.whenE (σ := Fl4SpecA64.State Unit) + ((cdata.bv :: vdata.bv :: args).length < 2 ∨ ((y, sl) :: (y1, sl1) :: rest).length < 2) + (NonDetE.throwError SyscallError.truncatedMessage) = pure () := if_neg (by simp) + have head : ∀ (g : Bool → Bool → SEMonad Unit TcbInvocation) (R), + (g (!b0) (!b1)).run σ = R → + (NonDetE.whenE ((cdata.bv :: vdata.bv :: args).length < 2 ∨ ((y, sl) :: (y1, sl1) :: rest).length < 2) + (NonDetE.throwError SyscallError.truncatedMessage) >>= fun _ => + NonDetE.liftE (NonDet.liftM not (slotCapLongRunningDelete (getTcbCtablePtr (specPtr t)))) >>= fun a => + NonDetE.liftE (NonDet.liftM not (slotCapLongRunningDelete (getTcbVtablePtr (specPtr t)))) >>= fun b => + g a b).run σ = R := by + intro g R hg + rw [hskip, run_bind_okD (a := ()) (s' := σ) rfl, run_bind_okD hr0, run_bind_okD hr1] + exact hg + have ill : ∀ x : error.Exc, x = .Sys .IllegalOperation → VsErrRelA x .illegalOperation := + fun x hx => Or.inl (by rw [hx]; exact trivial) + unfold tcb.derive_roots at h + rw [tcb_slot0A, bind_ok, hb0, bind_ok] at h + unfold cvProg + simp only [show objRefOf (.threadCap (specPtr t)) = specPtr t from rfl] + cases b0 with + | true => + simp only [ite_true, error.illegal] at h + have := ok_inj_ff h + subst this + refine ⟨_, head _ _ ?_, ill _ rfl⟩ + exact unlessE_neg_runX (by simp) _ _ σ + | false => + simp only [Bool.false_eq_true, ite_false] at h + rw [tcb_slot1A, bind_ok, hb1, bind_ok] at h + cases b1 with + | true => + simp only [ite_true, error.illegal] at h + have := ok_inj_ff h + subst this + refine ⟨_, head _ _ ?_, ill _ rfl⟩ + exact unlessE_neg_runX (by simp) _ _ σ + | false => + simp only [Bool.false_eq_true, ite_false] at h + have hgo : NonDetE.unlessE (σ := Fl4SpecA64.State Unit) ((!false) = true ∧ (!false) = true) + (NonDetE.throwError SyscallError.illegalOperation) = pure () := if_pos (by simp) + obtain ⟨rc, rc', hrc, hsc, hrelc⟩ := deriveData_relA (σ := σ) (sl := sl) cdata hcr hnc + obtain ⟨rv, rv', hrv, hsv, hrelv⟩ := deriveData_relA (σ := σ) (sl := sl1) vdata hcr1 hnc1 + rw [hrc, bind_ok] at h + simp only [List.getElem?_cons_zero, List.getElem?_cons_succ] + cases rc with + | Err e => + cases rc' with + | ok _ => exact hrelc.elim + | error e' => + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, Std.bind_tc_ok] at h + have := ok_inj_ff h + subst this + refine ⟨e', head _ _ ?_, Or.inl hrelc⟩ + rw [hgo, run_bind_okD (a := ()) (s' := σ) rfl] + exact run_bind_errorD hsc _ + | Ok cc => + cases rc' with + | error _ => exact hrelc.elim + | ok zc => + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok] at h + rw [isCnode_relA hrelc.1 hrelc.2, bind_ok] at h + cases hcn : isCnodeCap zc with + | false => + simp only [hcn, Bool.false_eq_true, ite_false, error.illegal] at h + have := ok_inj_ff h + subst this + refine ⟨_, head _ _ ?_, ill _ rfl⟩ + rw [hgo, run_bind_okD (a := ()) (s' := σ) rfl] + refine (run_bind_okD (a := zc) (s' := σ) hsc _).trans ?_ + exact unlessE_neg_runX (by simp [hcn]) _ _ σ + | true => + simp only [hcn, ite_true] at h + have hgc : NonDetE.unlessE (σ := Fl4SpecA64.State Unit) (isCnodeCap zc = true) + (NonDetE.throwError SyscallError.illegalOperation) = pure () := if_pos hcn + rw [hrv, bind_ok] at h + cases rv with + | Err e => + cases rv' with + | ok _ => exact hrelv.elim + | error e' => + simp only [bind_ok, core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, Std.bind_tc_ok] at h + have := ok_inj_ff h + subst this + refine ⟨e', head _ _ ?_, Or.inl hrelv⟩ + rw [hgo, run_bind_okD (a := ()) (s' := σ) rfl] + refine (run_bind_okD (a := zc) (s' := σ) hsc _).trans ?_ + rw [hgc, run_bind_okD (a := ()) (s' := σ) rfl] + exact run_bind_errorD hsv _ + | Ok vc => + cases rv' with + | error _ => exact hrelv.elim + | ok zv => + simp only [bind_ok] at h + rw [hP vc zv hrv hrelv.1 hrelv.2 hsv, bind_ok] at h + cases hv : P zv with + | false => + simp only [hv, Bool.false_eq_true, ite_false, error.illegal] at h + have := ok_inj_ff h + subst this + refine ⟨_, head _ _ ?_, ill _ rfl⟩ + rw [hgo, run_bind_okD (a := ()) (s' := σ) rfl] + refine (run_bind_okD (a := zc) (s' := σ) hsc _).trans ?_ + rw [hgc, run_bind_okD (a := ()) (s' := σ) rfl] + refine (run_bind_okD (a := zv) (s' := σ) hsv _).trans ?_ + exact unlessE_neg_runX (by simp [hv]) _ _ σ + | true => + simp only [hv, ite_true] at h + have := ok_inj_ff h + subst this + refine ⟨.threadControlCaps (specPtr t) slot none none (some (zc, sl)) (some (zv, sl1)) none, head _ _ ?_⟩ + rw [hgo, run_bind_okD (a := ()) (s' := σ) rfl] + refine (run_bind_okD (a := zc) (s' := σ) hsc _).trans ?_ + rw [hgc, run_bind_okD (a := ()) (s' := σ) rfl] + refine (run_bind_okD (a := zv) (s' := σ) hsv _).trans ?_ + rw [show NonDetE.unlessE (σ := Fl4SpecA64.State Unit) (P zv = true) + (NonDetE.throwError SyscallError.illegalOperation) = pure () from if_pos hv, + run_bind_okD (a := ()) (s' := σ) rfl] + rfl + +end + +end + +end Fl4ProofsA64.P3.ReplyErr diff --git a/proofs/Fl4ProofsA64/P3/Stage3DecErrTcbSchedA.lean b/proofs/Fl4ProofsA64/P3/Stage3DecErrTcbSchedA.lean new file mode 100644 index 000000000..4cf9b47c4 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3DecErrTcbSchedA.lean @@ -0,0 +1,593 @@ +/- +P3 (aarch64, A64-P3 Lift 1b, part C2): **decoder errors of SetSchedParams**. Every FL4 `Err` other than `Preempted` +precedes the caller's Restart. + +[J] (record facts, discharged in the group top) `hk`/`hks`/`hbl` the target's spec TCB, `hSc` the SC of an extra SC +cap, `hth`/`hM` the MCP relation, `hcur`. P0 `ArchTotal`. [S] `hst`. New module. +-/ +import Fl4ProofsA64.P3.Stage3DecErrTcbHighA +import Fl4ProofsA64.P3.TcbInvokeSchedSelectA +import Fl4ProofsA64.P3.ScBindTopA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.ReplyErr +open fl4_core_a64 Fl4ProofsA64.P0 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P2.PathObl Fl4ProofsA64.P3 + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +theorem run_ret_bindA {α β σ ε : Type} (a : α) (f : α → Fl4Spec.NonDetE σ ε β) : + ((pure a : Fl4Spec.NonDetE σ ε α) >>= f) = f a := ndE_pure_bindD a f + +theorem getScObjRef_eqTcbA {σ : Fl4SpecA64.State Unit} {p : Fl4SpecA64.ObjRef} {z : Fl4SpecA64.SchedContext} {n : Nat} + (hk : σ.kheap p = some (.schedContext z n)) : + getScObjRef (Z := Unit) Fl4SpecA64.SchedContext.scTcb p σ = ({(z.scTcb, σ)}, False) := by + unfold getScObjRef + rw [nd_bind_oneSc (getSchedContext_eqA hk)] + rfl + +theorem isBlocked_eqTcbA {σ : Fl4SpecA64.State Unit} {p : Fl4SpecA64.ObjRef} {u : Fl4SpecA64.Tcb} + (hk : σ.kheap p = some (.tcb u)) : + isBlocked (Z := Unit) p σ = ({(ipcQueuedThreadState u.tcbState, σ)}, False) := by + have h : getThreadState (Z := Unit) p σ = ({(u.tcbState, σ)}, False) := threadGet_eqSc _ hk + unfold isBlocked Fl4Spec.NonDet.liftM + show (getThreadState (Z := Unit) p >>= fun x => pure (ipcQueuedThreadState x)) σ = _ + rw [nd_bind_oneSc h] + rfl + +theorem isBlockedWordA {S : Type} {st : machine.Store S} [L : LawfulStore st] {s : S} {t : U64} + {ts : Fl4SpecA64.ThreadState} (hst : (L.mem s (stA t)).val = tsCodeA ts) : + sched.is_blocked st s t = ok (ipcQueuedThreadState ts) := by + unfold sched.is_blocked + rw [eq_of_spec_fd (obj.Tcb.state.spec s t), bind_ok] + have e : ∀ k : U64, L.mem s (stA t) = k ↔ tsCodeA ts = k.val := + fun k => ⟨fun h => h ▸ hst.symm, fun h => UScalar.eq_of_val_eq (hst.trans h)⟩ + have e5 : sched.TS_BLOCKED_ON_REPLY = 5#u64 := by unfold sched.TS_BLOCKED_ON_REPLY; rfl + obtain ⟨-, -, h3, h4, h6⟩ := ts_vals + simp only [e, h3, h4, h6, e5] + cases ts <;> simp [tsCodeA, ipcQueuedThreadState] + +/-- The SC facts SetSchedParams reads of the extra SC cap `p`. -/ +structure TcbScViewA {S : Type} (st : machine.Store S) [L : LawfulStore st] (s : S) (σ : Fl4SpecA64.State Unit) + (m : Fl4SpecA64.McState) (p : U64) : Prop where + ne0 : p ≠ 0#u64 + nm : scRenA m p = specPtr p + ren : ∀ q : U64, q ≠ 0#u64 → scRenA m q = specPtr p → q = p + obj : ∃ z n, σ.kheap (specPtr p) = some (.schedContext z n) ∧ z.scTcb = optRefS (L.mem s (scTcbA p)) ∧ + sporadic.sc_released st s p = ok (scReleased σ.curTime z) ∧ + getScReleased (Z := Unit) (specPtr p) σ = ({(scReleased σ.curTime z, σ)}, False) + +section +open Fl4SpecA64 + +private theorem tcbGetReleasedA {σ : State Unit} {p : ObjRef} {x : SchedContext} {n : Nat} + (hk : σ.kheap p = some (.schedContext x n)) (hne : scActive x = true → x.scRefills ≠ []) : + getScReleased (Z := Unit) p σ = ({(scReleased σ.curTime x, σ)}, False) := by + have hr : scActive x = true → readScRefillReady (Z := Unit) p σ = some (refillReady σ.curTime (refillHd x)) := by + intro ha + show Option.bind (readRefillHead p σ) (fun h => Option.bind (Lookup.asks (fun s : State Unit => s.curTime) σ) + (fun ct => Lookup.oreturn (refillReady ct h) σ)) = _ + rw [readRefillHead_eqA hk (hne ha)]; rfl + have ha : readScActive (Z := Unit) p σ = some (scActive x) := by + show Option.bind (readSchedContext p σ) (fun sc => Lookup.oreturn (scActive sc) σ) = _ + rw [readSchedContext_eqA hk]; rfl + have e : readScReleased (Z := Unit) p σ = some (scReleased σ.curTime x) := by + show Option.bind (readScActive p σ) (fun a => (if a then readScRefillReady p else Lookup.oreturn false) σ) = _ + rw [ha] + cases h : scActive x + · simp [scReleased, h, Lookup.oreturn] + · simp [scReleased, h, hr h] + simp [getScReleased, NonDet.getsThe, e, NonDet.assertOpt]; rfl + +private theorem tcbScReleasedA {S : Type} {st : machine.Store S} [L : LawfulStore st] {Z : XSetsA} {m : Fl4SpecA64.McState} + {κ : U64 → U64} {s : S} {σ : Fl4SpecA64.State Unit} + (H : SchedSRN st Z m κ s σ) {sc : U64} (hc : sc ∈ Z.C) {x : SchedContext} {n : Nat} + (hk : σ.kheap (scNameA m sc) = some (.schedContext x n)) : + sporadic.sc_released st s sc = ok (scReleased σ.curTime x) := by + obtain ⟨y, k, hy, hx⟩ := H.scr.sc sc hc + rw [hk] at hy; cases hy + have ha : sporadic.sc_active st s sc = ok (scActive x) := by + unfold sporadic.sc_active + rw [eq_of_spec_fd (obj.Sc.refill_max.spec (L := L) s sc), bind_ok] + congr 1 + apply decide_eq_decide.2 + rw [gt_iff_valSc, hx.max] + rfl + unfold sporadic.sc_released + rw [ha, bind_ok] + by_cases h : scActive x = true + · rw [if_pos h, scReleased, h, Bool.true_and] + have hReady := getScRefillReady_eqA hk (H.inv.ne hk h) + exact eq_of_spec_fd (WP.spec_mono (ready_refinesA H.scr H.sch.time hc hk h) fun b hb => + (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst (hReady.symm.trans hb)))).1.symm) + · have h0 : scActive x = false := (Bool.not_eq_true _) ▸ h + rw [if_neg h, scReleased, h0, Bool.false_and] + + +/-- `TcbScViewA` of a tracked SC that is not the view's idle SC. -/ +theorem tcbScViewA_of {S : Type} {st : machine.Store S} [L : LawfulStore st] {Z : XSetsA} {m : Fl4SpecA64.McState} + {κ : U64 → U64} {s : S} {σ : Fl4SpecA64.State Unit} (H : SchedSRN st Z m κ s σ) {p : U64} (hp : p ∈ Z.C) + (h0 : p ≠ 0#u64) (hni : specPtr p ≠ m.idleScOf m.curCore) : TcbScViewA st s σ m p := by + have hidle : specPtr p ≠ Fl4SpecA64.idleScPtr := H.sch.scOff p hp hni + have hren : scRenA m p = specPtr p := by unfold scRenA; exact if_neg hni + have hname : scNameA m p = specPtr p := by unfold scNameA; exact if_neg hni + obtain ⟨z, n, hk, hz⟩ := H.scr.sc p hp + have hrel := tcbScReleasedA H hp hk + rw [hname] at hk + refine ⟨h0, hren, fun q _ hq => ?_, z, n, hk, ?_, hrel, tcbGetReleasedA hk (H.inv.ne hk)⟩ + · unfold scRenA at hq + split at hq + · exact absurd hq.symm hidle + · exact specPtr_inj hq + · rw [hz.tcb, ← scTcbA_eqSc] + +end + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {mc : Fl4SpecA64.McState} {s s' : S} {a a' : A} {t : U64} {ctx : tcb.Ctx} {x : error.Exc} {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} {argsU : List U64} {th : TypedHeapA} {kb : U64} + {excaps : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)} {T : Set U64} + +/-- `check_prio` against l4v `check_prio`. -/ +theorem checkPrio_viewA {q p : U64} (hq : q ∈ T) (hM : McpRelA T mc (L.mem s) σ) : + ∃ r, tcb.check_prio st s p q = ok r ∧ match r with + | .Ok _ => (checkPrio (Z := Unit) p.bv (specPtr q)).run σ = ({(.ok (), σ)}, False) + | .Err e => ∃ e', (checkPrio (Z := Unit) p.bv (specPtr q)).run σ = ({(.error e', σ)}, False) ∧ + VsErrRelA e e' := by + obtain ⟨z, hz, hmcp, -⟩ := hM q hq + have hgt : p.bv > BitVec.setWidth 64 z.tcbMcpriority ↔ p > L.mem s (mcpA q) := by + show BitVec.setWidth 64 z.tcbMcpriority < p.bv ↔ L.mem s (mcpA q) < p + rw [BitVec.lt_def, BitVec.toNat_setWidth_of_le (by decide), ← hmcp] + rfl + have hrun : (checkPrio (Z := Unit) p.bv (specPtr q)).run σ = + (Fl4Spec.NonDetE.whenE (σ := Fl4SpecA64.State Unit) (p.bv > ucast 64 z.tcbMcpriority) + (Fl4Spec.NonDetE.throwError (Fl4SpecA64.SyscallError.rangeError 0 (ucast 64 z.tcbMcpriority)))).run σ := by + unfold checkPrio + rw [run_bind_okD (run_liftE_stD (threadGet_eqSc Fl4SpecA64.Tcb.tcbMcpriority hz))] + unfold tcb.check_prio + rw [Fl4ProofsA64.P3.Sched.obj.Tcb.mcp.specA] + simp only [bind_ok] + by_cases h : p > L.mem s (mcpA q) + · refine ⟨.Err (.Sys (.RangeError 0#u64 (L.mem s (mcpA q)))), by rw [if_pos h]; rfl, ?_⟩ + refine ⟨.rangeError 0 (BitVec.setWidth 64 z.tcbMcpriority), ?_, Or.inl ⟨rfl, ?_⟩⟩ + · rw [hrun, show Fl4Spec.NonDetE.whenE (σ := Fl4SpecA64.State Unit) (p.bv > ucast 64 z.tcbMcpriority) + (Fl4Spec.NonDetE.throwError (Fl4SpecA64.SyscallError.rangeError 0 (ucast 64 z.tcbMcpriority))) = + Fl4Spec.NonDetE.throwError (.rangeError 0 (ucast 64 z.tcbMcpriority)) from if_pos (hgt.2 h)] + exact run_throwErrorD _ σ + · apply BitVec.eq_of_toNat_eq + rw [BitVec.toNat_setWidth_of_le (by decide), ← hmcp] + rfl + · refine ⟨.Ok (), by rw [if_neg h], ?_⟩ + show _ = _ + rw [hrun, show Fl4Spec.NonDetE.whenE (σ := Fl4SpecA64.State Unit) (p.bv > ucast 64 z.tcbMcpriority) + (Fl4Spec.NonDetE.throwError (Fl4SpecA64.SyscallError.rangeError 0 (ucast 64 z.tcbMcpriority))) = pure () from + if_neg (fun c => h (hgt.1 c))] + rfl + +/-- `sched_params_sc`'s result against the spec run of `d` at `σ`. -/ +def ScResA {β : Type} (σ : Fl4SpecA64.State Unit) (d : Fl4SpecA64.SEMonad Unit β) : + core.result.Result obj.Sc error.Exc → Prop + | .Ok _ => ∃ v, d.run σ = ({(.ok v, σ)}, False) + | .Err e => ∃ e', d.run σ = ({(.error e', σ)}, False) ∧ VsErrRelA e e' + +set_option maxHeartbeats 1000000 in +/-- `sched_params_sc` against l4v `decode_update_sc`. -/ +theorem updateSc_viewA {slot : U64} {y : Fl4SpecA64.Cap} (hy : CapRelA (capA st s slot) y) + {u : Fl4SpecA64.Tcb} (hk : σ.kheap (specPtr t) = some (.tcb u)) + (hks : u.tcbSchedContext = optScA m (L.mem s (scA t))) + (hbl : sched.is_blocked st s t = ok (ipcQueuedThreadState u.tcbState)) + (hcur : σ.curThread = specPtr (L.curW s)) + (hSc : ∀ p b, capA st s slot = .SchedContext p b → TcbScViewA st s σ m p) + (cap : Fl4SpecA64.Cap) (hcap : objRefOf cap = specPtr t) (sl : Fl4SpecA64.CslotPtr) : + ∀ r, tcb.sched_params_sc st s t slot = ok r → ScResA σ (decodeUpdateSc (Z := Unit) cap sl y) r := by + intro r hr + have htcb : (Fl4Spec.NonDetE.liftE (getTcbObjRef Fl4SpecA64.Tcb.tcbSchedContext (specPtr t)) : + Fl4SpecA64.SEMonad Unit _).run σ = ({(.ok u.tcbSchedContext, σ)}, False) := + run_liftE_stD (threadGet_eqSc Fl4SpecA64.Tcb.tcbSchedContext hk) + have hcapA : obj.Slot.cap st slot s = ok (capA st s slot) := capA_eq (st := st) s slot + have ill : ∀ r : core.result.Result obj.Sc error.Exc, r = .Err (.Sys .IllegalOperation) → + (decodeUpdateSc (Z := Unit) cap sl y).run σ = ({(.error .illegalOperation, σ)}, False) → + ScResA σ (decodeUpdateSc (Z := Unit) cap sl y) r := by + rintro r rfl h + exact ⟨_, h, Or.inl trivial⟩ + by_cases h0 : capA st s slot = .Null + · have ey : y = .nullCap := by + have e := hy.2 + rw [h0] at e + exact e.symm + subst ey + rw [tcbSchedParamsSc_nullA (h0 ▸ hcapA)] at hr + have hct : (Fl4Spec.NonDetE.liftE (Fl4Spec.NonDet.gets fun s : Fl4SpecA64.State Unit => s.curThread) : + Fl4SpecA64.SEMonad Unit _).run σ = ({(.ok σ.curThread, σ)}, False) := run_liftE_stD rfl + by_cases hc : t = L.curW s + · rw [if_pos hc] at hr + refine ill r (ok_inj_ff hr).symm ?_ + unfold decodeUpdateSc + simp only [Fl4Spec.NonDetE.returnOk, ndE_pure_bindD, hcap] + rw [run_bind_okD htcb] + try dsimp only + rw [run_bind_okD hct] + rw [show Fl4Spec.NonDetE.whenE (σ := Fl4SpecA64.State Unit) (specPtr t = σ.curThread) + (Fl4Spec.NonDetE.throwError Fl4SpecA64.SyscallError.illegalOperation) = + Fl4Spec.NonDetE.throwError .illegalOperation from if_pos (by rw [hcur, hc])] + exact run_bind_errorD (run_throwErrorD _ σ) _ + · rw [if_neg hc] at hr + cases ok_inj_ff hr + refine ⟨if u.tcbSchedContext ≠ none then some none else none, ?_⟩ + unfold decodeUpdateSc + simp only [Fl4Spec.NonDetE.returnOk, ndE_pure_bindD, hcap] + rw [run_bind_okD htcb] + try dsimp only + rw [run_bind_okD hct] + rw [show Fl4Spec.NonDetE.whenE (σ := Fl4SpecA64.State Unit) (specPtr t = σ.curThread) + (Fl4Spec.NonDetE.throwError Fl4SpecA64.SyscallError.illegalOperation) = pure () from + if_neg (by rw [hcur]; exact fun c => hc (specPtr_inj c)), ndE_pure_bindD] + rfl + by_cases hS : ∃ p b, capA st s slot = .SchedContext p b + swap + · have hk' : ∀ p b, capA st s slot ≠ .SchedContext p b := fun p b c => hS ⟨p, b, c⟩ + rw [tcbSchedParamsSc_badCapA hcapA h0 hk'] at hr + cases ok_inj_ff hr + show ∃ e', _ ∧ _ + have hn : y ≠ .nullCap ∧ ∀ p n, y ≠ .schedContextCap p n := by + have h := hy.2 + have hw := hy.1 + revert h h0 hk' hw + generalize capA st s slot = c + intro h0 hk' h hw + unfold CapEqA at h + split at h + · exact ⟨fun e => (by cases e), fun _ _ e => (by cases e)⟩ + · subst h + cases c + case Null => exact absurd rfl h0 + case SchedContext => exact absurd rfl (hk' _ _) + case PageDirectory => exact hw.elim + case Pdpt => exact hw.elim + case IoPortControl => exact hw.elim + case IoPort => exact hw.elim + case TdxControl => exact hw.elim + case Frame => + simp only [absCapA, absFrameE] + split <;> exact ⟨fun h => (by cases h), fun _ _ h => (by cases h)⟩ + all_goals exact ⟨fun h => (by cases h), fun _ _ h => (by cases h)⟩ + refine ⟨.invalidCapability 2, ?_, Or.inr ⟨2#u64, rfl, rfl⟩⟩ + unfold decodeUpdateSc + simp only [Fl4Spec.NonDetE.returnOk, ndE_pure_bindD, hcap] + rw [run_bind_okD htcb] + cases y with + | nullCap => exact absurd rfl hn.1 + | schedContextCap p n => exact absurd rfl (hn.2 p n) + | _ => dsimp only; exact run_bind_errorD (run_throwErrorD _ σ) _ + obtain ⟨p, b, hq⟩ := hS + have V := hSc p b hq + obtain ⟨z, n, hz, hzt, hrel, hgr⟩ := V.obj + have ey : y = .schedContextCap (specPtr p) (b.val - Fl4SpecA64.minSchedContextBits) := by + have e := hy.2 + rw [hq] at e + exact e.symm + subst ey + have hold : obj.Tcb.sc st t s = ok (L.mem s (scA t)) := eq_of_spec_fd (obj.Tcb.sc.spec (L := L) s t) + have hsct : obj.Sc.tcb st p s = ok (L.mem s (scTcbA p)) := by + exact eq_of_spec_fd (obj.Sc.tcb.spec (L := L) s p) + rw [tcbSchedParamsSc_contextA (show obj.Slot.cap st slot s = ok (.SchedContext p b) by rw [hcapA, hq]) hold] at hr + have hst : (Fl4Spec.NonDetE.liftE (getScObjRef Fl4SpecA64.SchedContext.scTcb (specPtr p)) : + Fl4SpecA64.SEMonad Unit _).run σ = ({(.ok z.scTcb, σ)}, False) := + run_liftE_stD (getScObjRef_eqTcbA hz) + have hib : (Fl4Spec.NonDetE.liftE (isBlocked (specPtr t)) : Fl4SpecA64.SEMonad Unit _).run σ = + ({(.ok (ipcQueuedThreadState u.tcbState), σ)}, False) := run_liftE_stD (isBlocked_eqTcbA hk) + have hgr' : (Fl4Spec.NonDetE.liftE (getScReleased (specPtr p)) : Fl4SpecA64.SEMonad Unit _).run σ = + ({(.ok (scReleased σ.curTime z), σ)}, False) := run_liftE_stD hgr + set C := u.tcbSchedContext ≠ + some (objRefOf (.schedContextCap (specPtr p) (b.val - Fl4SpecA64.minSchedContextBits))) ∧ + (u.tcbSchedContext ≠ none ∨ z.scTcb ≠ none) with hC + have specIll : C ∨ (ipcQueuedThreadState u.tcbState = true ∧ scReleased σ.curTime z = false) → + (decodeUpdateSc (Z := Unit) cap sl (.schedContextCap (specPtr p) (b.val - Fl4SpecA64.minSchedContextBits))).run σ = + ({(.error .illegalOperation, σ)}, False) := by + intro h + unfold decodeUpdateSc + simp only [Fl4Spec.NonDetE.returnOk, ndE_pure_bindD, hcap] + rw [run_bind_okD htcb] + try dsimp only + simp only [show objRefOf (.schedContextCap (specPtr p) (b.val - Fl4SpecA64.minSchedContextBits)) = specPtr p + from rfl, ndE_pure_bindD] + rw [run_bind_okD hst] + by_cases hc : C + · rw [show Fl4Spec.NonDetE.whenE (σ := Fl4SpecA64.State Unit) C + (Fl4Spec.NonDetE.throwError Fl4SpecA64.SyscallError.illegalOperation) = + Fl4Spec.NonDetE.throwError .illegalOperation from if_pos hc] + exact run_bind_errorD (run_throwErrorD _ σ) _ + rw [show Fl4Spec.NonDetE.whenE (σ := Fl4SpecA64.State Unit) C + (Fl4Spec.NonDetE.throwError Fl4SpecA64.SyscallError.illegalOperation) = pure () from if_neg hc, ndE_pure_bindD] + obtain ⟨h1, h2⟩ := h.resolve_left hc + rw [run_bind_okD hib, run_bind_okD hgr'] + rw [show Fl4Spec.NonDetE.whenE (σ := Fl4SpecA64.State Unit) + (ipcQueuedThreadState u.tcbState = true ∧ ¬ scReleased σ.curTime z = true) + (Fl4Spec.NonDetE.throwError Fl4SpecA64.SyscallError.illegalOperation) = + Fl4Spec.NonDetE.throwError .illegalOperation from if_pos ⟨h1, by rw [h2]; exact Bool.false_ne_true⟩] + exact run_bind_errorD (run_throwErrorD _ σ) _ + have specOk : ¬ C → ¬ (ipcQueuedThreadState u.tcbState = true ∧ ¬ scReleased σ.curTime z = true) → + ∃ v, (decodeUpdateSc (Z := Unit) cap sl + (.schedContextCap (specPtr p) (b.val - Fl4SpecA64.minSchedContextBits))).run σ = ({(.ok v, σ)}, False) := by + intro h1 h2 + refine ⟨if u.tcbSchedContext ≠ some (specPtr p) then some (some (specPtr p)) else none, ?_⟩ + unfold decodeUpdateSc + simp only [Fl4Spec.NonDetE.returnOk, ndE_pure_bindD, hcap] + rw [run_bind_okD htcb] + try dsimp only + simp only [show objRefOf (.schedContextCap (specPtr p) (b.val - Fl4SpecA64.minSchedContextBits)) = specPtr p + from rfl, ndE_pure_bindD] + rw [run_bind_okD hst] + rw [show Fl4Spec.NonDetE.whenE (σ := Fl4SpecA64.State Unit) C + (Fl4Spec.NonDetE.throwError Fl4SpecA64.SyscallError.illegalOperation) = pure () from if_neg h1, + ndE_pure_bindD, run_bind_okD hib, run_bind_okD hgr'] + rw [show Fl4Spec.NonDetE.whenE (σ := Fl4SpecA64.State Unit) + (ipcQueuedThreadState u.tcbState = true ∧ ¬ scReleased σ.curTime z = true) + (Fl4Spec.NonDetE.throwError Fl4SpecA64.SyscallError.illegalOperation) = pure () from if_neg h2, + ndE_pure_bindD] + rfl + have release : ¬ C → (do + let blocked ← sched.is_blocked st s t + if blocked then + let released ← sporadic.sc_released st s p + if released then ok (core.result.Result.Ok p) else error.illegal obj.Sc + else ok (core.result.Result.Ok p)) = ok r → + ScResA σ (decodeUpdateSc (Z := Unit) cap sl + (.schedContextCap (specPtr p) (b.val - Fl4SpecA64.minSchedContextBits))) r := by + intro hc h + rw [hbl, bind_ok] at h + cases hb : ipcQueuedThreadState u.tcbState with + | false => + rw [hb] at h + simp only [Bool.false_eq_true, ite_false] at h + cases ok_inj_ff h + exact specOk hc (fun c => by rw [hb] at c; exact Bool.noConfusion c.1) + | true => + rw [hb] at h + simp only [ite_true, hrel, bind_ok] at h + cases hrl : scReleased σ.curTime z with + | true => + rw [hrl] at h + simp only [ite_true] at h + cases ok_inj_ff h + exact specOk hc (fun c => c.2 hrl) + | false => + rw [hrl] at h + simp only [Bool.false_eq_true, ite_false, error.illegal] at h + exact ill r (ok_inj_ff h).symm (specIll (Or.inr ⟨hb, hrl⟩)) + by_cases h1 : L.mem s (scA t) = p + · rw [if_pos h1] at hr + refine release (fun c => c.1 ?_) hr + rw [hks, optScA, h1] + rw [if_neg V.ne0, V.nm] + try rfl + rw [if_neg h1] at hr + have hne : u.tcbSchedContext ≠ some (specPtr p) := by + rw [hks, optScA] + by_cases h2 : L.mem s (scA t) = 0#u64 + · rw [if_pos h2]; exact fun c => by cases c + · rw [if_neg h2] + exact fun c => h1 (V.ren _ h2 (Option.some.inj c)) + by_cases h2 : L.mem s (scA t) = 0#u64 + swap + · rw [if_neg h2] at hr + refine ill r (ok_inj_ff hr).symm (specIll (Or.inl ⟨hne, Or.inl ?_⟩)) + rw [hks, optScA, if_neg h2] + exact fun c => by cases c + rw [if_pos h2, hsct, bind_ok] at hr + by_cases h3 : L.mem s (scTcbA p) = 0#u64 + · rw [if_pos h3] at hr + refine release (fun c => ?_) hr + rcases c.2 with d | d + · exact d (by rw [hks, optScA, if_pos h2]) + · exact d (by rw [hzt, h3, optRefS_zero]) + · rw [if_neg h3] at hr + refine ill r (ok_inj_ff hr).symm (specIll (Or.inl ⟨hne, Or.inr ?_⟩)) + rw [hzt, optRefS_ne h3] + exact fun c => by cases c + +set_option maxHeartbeats 2000000 in +theorem tcbSchedParams.decErrA (hA : ArchTotal ar) (hne : x ≠ .Preempted) (hst : smp.remote_tcb_stall st ar s a t = ok (s, a)) + (hAr : TcbArgsA st s ctx argsU) (hP : PlaceA th kb) (hX : ExtraOKA th (L.mem s) ctx.extra excaps) + (hth : ∀ p, exAtA ctx.extra 0 ≠ 0#u64 → capA st s (exAtA ctx.extra 0) = .Thread p → p ∈ T) + (hM : McpRelA T mc (L.mem s) σ) + {u : Fl4SpecA64.Tcb} (hk : σ.kheap (specPtr t) = some (.tcb u)) + (hks : u.tcbSchedContext = optScA m (L.mem s (scA t))) + (hbl : sched.is_blocked st s t = ok (ipcQueuedThreadState u.tcbState)) + (hcur : σ.curThread = specPtr (L.curW s)) + (hSc : ∀ p b, exAtA ctx.extra 1 ≠ 0#u64 → capA st s (exAtA ctx.extra 1) = .SchedContext p b → TcbScViewA st s σ m p) + (sl : Fl4SpecA64.CslotPtr) + (h : tcb.decode st ar s a fl4_abi.label.TCBSetSchedParams t ctx = ok (.Err x, s', a')) : + DecEA σ x (decodeSetSchedParams (Z := Unit) (argsU.map UScalar.bv) (.threadCap (specPtr t)) sl excaps) := by + obtain ⟨hlen, hargs⟩ := hAr + rw [tcbSetSchedParams_programA hst] at h + unfold tcb.decode_set_sched_params at h + have he0 := tcbExtraWL_okA (L := L) (ctx := ctx) hP hX 0#usize (by simp) 2#u64 + have he1 := tcbExtraWL_okA (L := L) (ctx := ctx) hP hX 1#usize (by simp) 2#u64 + have he2 := tcbExtraWL_okA (L := L) (ctx := ctx) hP hX 2#usize (by simp) 2#u64 + simp only [UScalar.ofNatCore_val_eq] at he0 he1 he2 + by_cases hc : 3 ≤ excaps.length ∧ 2#u64 ≤ ctx.length + swap + · have hx : x = .Sys .TruncatedMessage := by + by_cases c0 : 0 < excaps.length ∧ 2#u64 ≤ ctx.length + swap + · rw [if_neg c0] at he0 + simp only [he0, bind_ok, core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, Std.bind_tc_ok] at h + exact tcbErr_injA h + rw [if_pos c0] at he0 + by_cases c1 : 1 < excaps.length ∧ 2#u64 ≤ ctx.length + swap + · rw [if_neg c1] at he1 + simp only [he0, he1, bind_ok, core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, Std.bind_tc_ok] at h + exact tcbErr_injA h + rw [if_pos c1] at he1 + have c2 : ¬ (2 < excaps.length ∧ 2#u64 ≤ ctx.length) := fun c => hc ⟨c.1, c.2⟩ + rw [if_neg c2] at he2 + simp only [he0, he1, he2, bind_ok, core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, Std.bind_tc_ok] at h + exact tcbErr_injA h + refine ⟨.truncatedMessage, ?_, Or.inl (by rw [hx]; exact trivial)⟩ + unfold decodeSetSchedParams + by_cases ha : (argsU.map UScalar.bv).length < 2 + · rw [show Fl4Spec.NonDetE.whenE (σ := Fl4SpecA64.State Unit) ((argsU.map UScalar.bv).length < 2) + (Fl4Spec.NonDetE.throwError Fl4SpecA64.SyscallError.truncatedMessage) = + Fl4Spec.NonDetE.throwError .truncatedMessage from if_pos ha] + exact run_bind_errorD (run_throwErrorD _ σ) _ + rw [show Fl4Spec.NonDetE.whenE (σ := Fl4SpecA64.State Unit) ((argsU.map UScalar.bv).length < 2) + (Fl4Spec.NonDetE.throwError Fl4SpecA64.SyscallError.truncatedMessage) = pure () from if_neg ha, ndE_pure_bindD] + have h3 : excaps.length < 3 := by + refine Nat.lt_of_not_le fun c => hc ⟨c, ?_⟩ + rw [List.length_map, ← hlen] at ha + scalar_tac + rw [show Fl4Spec.NonDetE.whenE (σ := Fl4SpecA64.State Unit) (excaps.length < 3) + (Fl4Spec.NonDetE.throwError Fl4SpecA64.SyscallError.truncatedMessage) = + Fl4Spec.NonDetE.throwError .truncatedMessage from if_pos h3] + exact run_bind_errorD (run_throwErrorD _ σ) _ + obtain ⟨h3, h2⟩ := hc + rw [if_pos ⟨by omega, h2⟩] at he0 he1 he2 + have ha : 2 ≤ argsU.length := by rw [← hlen]; scalar_tac + match excaps, hX, h3, argsU, hargs, ha with + | y0 :: y1 :: y2 :: ys, hX, _, p0 :: p1 :: rest, hargs, _ => + have hm := hargs 0#u64 p0 (by simp) + have hp := hargs 1#u64 p1 (by simp) + have s0 := srcOK_factsA hP (hX.2.1 0 (by simp)) + have s1 := srcOK_factsA hP (hX.2.1 1 (by simp)) + have s2 := srcOK_factsA hP (hX.2.1 2 (by simp)) + have c0 := s0.2.2.2.1 + have c1 := s1.2.2.2.1 + have c2 := s2.2.2.2.1 + rw [capAtA_eq (st := st)] at c0 c1 c2 + simp only [List.getElem_cons_zero, List.getElem_cons_succ] at c0 c1 c2 + simp only [he0, he1, he2, hm, hp, bind_ok, core.result.Result.Insts.CoreOpsTry.branch] at h + have hn2 : ¬ ((p0 :: p1 :: rest).map UScalar.bv).length < 2 := by simp + have hn3 : ¬ (y0 :: y1 :: y2 :: ys).length < 3 := by simp + unfold decodeSetSchedParams + rw [show Fl4Spec.NonDetE.whenE (σ := Fl4SpecA64.State Unit) (((p0 :: p1 :: rest).map UScalar.bv).length < 2) + (Fl4Spec.NonDetE.throwError Fl4SpecA64.SyscallError.truncatedMessage) = pure () from if_neg hn2, + ndE_pure_bindD, + show Fl4Spec.NonDetE.whenE (σ := Fl4SpecA64.State Unit) ((y0 :: y1 :: y2 :: ys).length < 3) + (Fl4Spec.NonDetE.throwError Fl4SpecA64.SyscallError.truncatedMessage) = pure () from if_neg hn3, + ndE_pure_bindD] + simp only [Fl4Spec.NonDetE.returnOk, ndE_pure_bindD, List.getElem?_cons_zero, List.getElem?_cons_succ, the, + Option.getD_some, List.map_cons] + have hcap0 := capA_eq (st := st) s (exAtA ctx.extra 0) + by_cases hq : ∃ q, capA st s (exAtA ctx.extra 0) = .Thread q + swap + · have hk' : ∀ q, capA st s (exAtA ctx.extra 0) ≠ .Thread q := fun q e => hq ⟨q, e⟩ + have hta : tcb.thread_arg st s (exAtA ctx.extra 0) 1#u64 = ok (.Err (.Sys (.InvalidCapability 1#u64))) := by + unfold tcb.thread_arg + rw [hcap0] + simp only [bind_ok] + cases hc : capA st s (exAtA ctx.extra 0) <;> first | rfl | exact absurd hc (hk' _) + simp only [hta, bind_ok, core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, Std.bind_tc_ok] at h + have hx := tcbErr_injA h + refine ⟨.invalidCapability 1, ?_, Or.inr ⟨1#u64, hx, rfl⟩⟩ + have hy := notThreadCapA c0.2 hk' + obtain ⟨yc, ysl⟩ := y0 + cases yc <;> first + | exact absurd rfl (hy _) + | exact run_bind_errorD (run_throwErrorD _ σ) _ + obtain ⟨q, hq⟩ := hq + have hta : tcb.thread_arg st s (exAtA ctx.extra 0) 1#u64 = ok (.Ok q) := by + unfold tcb.thread_arg + simp only [hcap0, hq, bind_ok] + have hy : y0.1 = .threadCap (specPtr q) := by + have e := c0.2 + rw [hq] at e + exact e.symm + have hqT := hth q s0.1 hq + simp only [hta, hy, bind_ok] at h ⊢ + try rw [run_ret_bindA] + obtain ⟨r1, e1, k1⟩ := checkPrio_viewA (st := st) (s := s) (p := p0) hqT hM + simp only [e1, bind_ok] at h + cases r1 with + | Err e => + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, Std.bind_tc_ok] at h + rw [tcbErr_injA h] + obtain ⟨e', hr, hrel⟩ := k1 + exact ⟨e', run_bind_errorD hr _, hrel⟩ + | Ok _ => + obtain ⟨r2, e2, k2⟩ := checkPrio_viewA (st := st) (s := s) (p := p1) hqT hM + simp only [e2, bind_ok] at h + unfold DecEA + rw [run_bind_okD k1] + cases r2 with + | Err e => + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, Std.bind_tc_ok] at h + rw [tcbErr_injA h] + obtain ⟨e', hr, hrel⟩ := k2 + exact ⟨e', run_bind_errorD hr _, hrel⟩ + | Ok _ => + rw [run_bind_okD k2] + simp only [bind_ok, Std.bind_tc_ok] at h + obtain ⟨r3, e3, h⟩ : ∃ r3, tcb.sched_params_sc st s t (exAtA ctx.extra 1) = ok r3 ∧ _ := bind_eq_ok_std h + have K := updateSc_viewA (st := st) (s := s) (t := t) (slot := exAtA ctx.extra 1) (y := y1.1) c1 hk hks hbl hcur + (fun p b e => hSc p b s1.1 e) (.threadCap (specPtr t)) rfl sl r3 e3 + cases r3 with + | Err e => + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, Std.bind_tc_ok] at h + rw [tcbErr_injA h] + obtain ⟨e', hr, hrel⟩ := K + exact ⟨e', run_bind_errorD hr _, hrel⟩ + | Ok sc => + obtain ⟨v, hv⟩ := K + rw [run_bind_okD hv] + simp only [lift, bind_ok] at h + obtain ⟨old, -, h⟩ := bind_eq_ok_std h + have fin : ∀ flags : U64, (do + let fh ← obj.Slot.cap st (exAtA ctx.extra 2) s + let b ← faults.valid_fault_handler fh + if b then + let s1 ← tcb.restart_caller st s + tcb.thread_control_sched st ar s1 a t ctx.slot (fh, exAtA ctx.extra 2) p0 p1 sc flags + else + let r7 ← error.invalid_cap Unit 3#u64 + ok (r7, s, a)) = ok (.Err x, s', a') → + ∃ e', ((checkHandlerEp (Z := Unit) 3 y2 >>= fun fh => pure (Fl4SpecA64.TcbInvocation.threadControlSched + (specPtr t) sl (some fh) (some (ucast 8 p0.bv, specPtr q)) (some (ucast 8 p1.bv, specPtr q)) v) : + Fl4SpecA64.SEMonad Unit _)).run σ = ({(.error e', σ)}, False) ∧ VsErrRelA x e' := by + intro flags h + rw [capA_eq (st := st) s (exAtA ctx.extra 2), bind_ok] at h + obtain ⟨b, hb, h⟩ := bind_eq_ok_std h + cases b with + | true => + simp only [ite_true] at h + obtain ⟨s1', -, h⟩ := bind_eq_ok_std h + have K := tcb.thread_control_sched.preA (st := st) (ar := ar) hA s1' a t ctx.slot + (capA st s (exAtA ctx.extra 2), exAtA ctx.extra 2) p0 p1 sc flags + rw [h, WP.spec_ok] at K + exact absurd (K x rfl) hne + | false => + simp only [Bool.false_eq_true, ite_false, error.invalid_cap, bind_ok] at h + have hx := tcbErr_injA h + have hv : ¬ validFaultHandler y2.1 := by + rw [validFH_eqA c2.2, ← tcbValidFaultHandlerA _ c2.1, hb] + intro e + exact Bool.noConfusion (ok_inj_ff e) + refine ⟨.invalidCapability 3, ?_, Or.inr ⟨3#u64, hx, rfl⟩⟩ + refine run_bind_errorD ?_ _ + unfold checkHandlerEp + rw [show Fl4Spec.NonDetE.unlessE (σ := Fl4SpecA64.State Unit) (validFaultHandler y2.1) + (Fl4Spec.NonDetE.throwError (Fl4SpecA64.SyscallError.invalidCapability 3)) = + Fl4Spec.NonDetE.throwError (.invalidCapability 3) from if_neg hv] + exact run_bind_errorD (run_throwErrorD _ σ) _ + split at h <;> exact fin _ h + +end + +end + +end Fl4ProofsA64.P3.ReplyErr diff --git a/proofs/Fl4ProofsA64/P3/Stage3DecErrTcbSpaceA.lean b/proofs/Fl4ProofsA64/P3/Stage3DecErrTcbSpaceA.lean new file mode 100644 index 000000000..95ec04061 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3DecErrTcbSpaceA.lean @@ -0,0 +1,611 @@ +/- +P3 (aarch64, A64-P3 Lift 1b, part C3): **decoder errors of SetSpace and Configure**. Every FL4 `Err` other than +`Preempted` precedes the caller's Restart. + +* `ipcDec_errA`, `ipcDec_okA`: `derive_for_buffer` against `decodeSetIpcBuffer` of the buffer argument. +* `tcbSpace.decErrA`, `tcbConfig.decErrA`: the l4v decode, or the Stage-2 root branch of the Ext decode. + +P0 `ArchTotal`. [S] `hst`. `F` the finality state facts; `hnc`: `ensure_no_children` at an untyped extra cap. +New module. +-/ +import Fl4ProofsA64.P3.Stage3DecErrTcbRootsA +import Fl4ProofsA64.P3.Stage3DecErrTcbAuthA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.ReplyErr +open fl4_core_a64 Fl4ProofsA64.P0 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P2.PathObl Fl4ProofsA64.P3 + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Endp Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +theorem decodeCvSpace_progA (args : List Fl4SpecA64.Data) (cap : Fl4SpecA64.Cap) (slot : Fl4SpecA64.CslotPtr) + (excaps : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) : + decodeCvSpace (Z := Unit) args cap slot excaps = cvProg isValidVtableRoot args cap slot excaps := by + unfold decodeCvSpace cvProg + simp only [Fl4Spec.NonDetE.returnOk, ndE_pure_bindD] + +theorem fl4CvSpace_progA (args : List Fl4SpecA64.Data) (cap : Fl4SpecA64.Cap) (slot : Fl4SpecA64.CslotPtr) + (excaps : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) : + fl4DecodeCvSpace (Z := Unit) args cap slot excaps = + cvProg (fun v => isValidVtableRoot v || fl4MappedStage2Root v) args cap slot excaps := by + have hor : ∀ (a b : Bool) (m : Fl4Spec.NonDetE (Fl4SpecA64.State Unit) Fl4SpecA64.SyscallError Unit), + Fl4Spec.NonDetE.unlessE (a = true ∨ b = true) m = Fl4Spec.NonDetE.unlessE ((a || b) = true) m := by + intro a b m + cases a <;> cases b <;> rfl + unfold fl4DecodeCvSpace cvProg + simp only [Fl4Spec.NonDetE.returnOk, ndE_pure_bindD, hor] + try rfl + +/-- A derived updated cap is a mapped Stage-2 root only if the source cap is. -/ +theorem stage2_deriveA {σ : Fl4SpecA64.State Unit} {sl : Fl4SpecA64.CslotPtr} {d : Fl4SpecA64.Data} + {y z : Fl4SpecA64.Cap} + (h : (deriveCap (Z := Unit) sl ((if d = 0 then id else updateCapData false d) y)).run σ = ({(.ok z, σ)}, False)) + (hz : fl4MappedStage2Root z = true) : fl4MappedStage2Root y = true := by + obtain ⟨p, a, rfl⟩ : ∃ p a, z = .archObjectCap (.ext (.stage2VSpaceCap p (some a))) := by + cases z with + | archObjectCap c => + cases c with + | ext e => + cases e with + | stage2VSpaceCap p m => + cases m with + | none => cases hz + | some a => exact ⟨p, a, rfl⟩ + | _ => cases hz + | _ => cases hz + | _ => cases hz + have key : ∀ w : Fl4SpecA64.Cap, + (deriveCap (Z := Unit) sl w).run σ = + ({(.ok (.archObjectCap (.ext (.stage2VSpaceCap p (some a)))), σ)}, False) → + w = .archObjectCap (.ext (.stage2VSpaceCap p (some a))) := by + intro w hw + cases w with + | untypedCap => + simp only [deriveCap] at hw + rw [ExceptT.run_bind, nd_bind_singleA (ensureNoChildren_runA σ sl)] at hw + by_cases hc : ∃ x, σ.cdt x = some sl + · simp only [hc, ↓reduceIte] at hw + exact (run_single_errA hw).elim + · simp only [hc, ↓reduceIte] at hw + exact absurd (run_single_okA hw) (by simp) + | zombie => exact absurd (run_single_okA hw) (by simp) + | irqControlCap => exact absurd (run_single_okA hw) (by simp) + | archObjectCap c => + cases c with + | pageTableCap r t m => + cases m + · exact (run_single_errA hw).elim + · exact absurd (run_single_okA hw) (by simp) + | frameCap => exact absurd (run_single_okA hw) (by simp) + | ext e => + cases e with + | stage2VSpaceCap q m => + cases m + · exact (run_single_errA hw).elim + · exact (run_single_okA hw) + | highVSpaceCap q m => + cases m + · exact (run_single_errA hw).elim + · exact absurd (run_single_okA hw) (by simp) + | _ => exact absurd (run_single_okA hw) (by simp) + | _ => exact absurd (run_single_okA hw) (by simp) + | _ => exact absurd (run_single_okA hw) (by simp) + have hw := key _ h + by_cases hd : d = 0 + · rw [if_pos hd] at hw + simp only [id_eq] at hw + rw [hw]; rfl + rw [if_neg hd] at hw + revert hw + cases y with + | endpointCap => + simp only [updateCapData, isEpCap, ite_true] + split <;> simp [badgeUpdate] + | notificationCap => + simp only [updateCapData, isEpCap, isNtfnCap, Bool.false_eq_true, ite_false, ite_true] + split <;> simp [badgeUpdate] + | cnodeCap => + simp only [updateCapData, isEpCap, isNtfnCap, isCnodeCap, Bool.false_eq_true, ite_false, ite_true] + split <;> simp + | archObjectCap c => + simp only [updateCapData, isEpCap, isNtfnCap, isCnodeCap, isArchCap, Bool.false_eq_true, ite_false, ite_true, + archUpdateCapData, Fl4SpecA64.Cap.theArchCap] + split + · split <;> simp + · intro hw; rw [hw]; rfl + | _ => simp [updateCapData, isEpCap, isNtfnCap, isCnodeCap, isArchCap] + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {s s' : S} {a a' : A} {t : U64} {ctx : tcb.Ctx} {x : error.Exc} {σ : Fl4SpecA64.State Unit} + {th : TypedHeapA} {kb : U64} {excaps : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)} {argsU : List U64} + +theorem ipcDec_errA {v : U64} {y cap : Fl4SpecA64.Cap} {sl slot : Fl4SpecA64.CslotPtr} {bs : obj.Slot} + (hcr : CapRelA (capA st s bs) y) + (hnc : isUtA (capA st s bs) = true → NoChildrenRelA (st := st) s σ bs sl) + (h : tcb.derive_for_buffer st s v bs = ok (.Err x)) : + ∃ e', (decodeSetIpcBuffer (Z := Unit) [v.bv] cap slot [(y, sl)]).run σ = ({(.error e', σ)}, False) ∧ + VsErrRelA x e' := by + obtain ⟨h0, e', hrun, hrel⟩ := bufDerive.decErrA (σ := σ) (x := x) (y := y) (sl := sl) hcr hnc h + refine ⟨e', ?_, hrel⟩ + unfold decodeSetIpcBuffer + rw [show Fl4Spec.NonDetE.whenE (σ := Fl4SpecA64.State Unit) ([v.bv].length = 0) + (Fl4Spec.NonDetE.throwError Fl4SpecA64.SyscallError.truncatedMessage) = pure () from if_neg (by simp)] + rw [run_bind_okD (a := ()) (s' := σ) rfl] + rw [show Fl4Spec.NonDetE.whenE (σ := Fl4SpecA64.State Unit) ([(y, sl)].length = 0) + (Fl4Spec.NonDetE.throwError Fl4SpecA64.SyscallError.truncatedMessage) = pure () from if_neg (by simp)] + rw [run_bind_okD (a := ()) (s' := σ) rfl] + rw [run_bind_okD (a := v.bv) (s' := σ) rfl] + rw [run_bind_okD (a := (y, sl)) (s' := σ) rfl] + dsimp only + rw [if_neg h0] + exact run_bind_errorD hrun _ + +theorem ipcDec_okA {v : U64} {y cap : Fl4SpecA64.Cap} {sl slot : Fl4SpecA64.CslotPtr} {bs : obj.Slot} + {sel : capability.Cap × obj.Slot} (hcr : CapRelA (capA st s bs) y) + (hnc : isUtA (capA st s bs) = true → NoChildrenRelA (st := st) s σ bs sl) + (h : tcb.derive_for_buffer st s v bs = ok (.Ok sel)) : + ∃ i, (decodeSetIpcBuffer (Z := Unit) [v.bv] cap slot [(y, sl)]).run σ = ({(.ok i, σ)}, False) := by + have run : ∀ nb, ((if v.bv = 0 then Fl4Spec.NonDetE.returnOk none + else do + let bufferCap ← deriveCap (Z := Unit) sl y + checkValidIpcBuffer v.bv bufferCap + Fl4Spec.NonDetE.returnOk (some (bufferCap, sl))) : + SEMonad Unit (Option (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr))).run σ = ({(.ok nb, σ)}, False) → + ∃ i, (decodeSetIpcBuffer (Z := Unit) [v.bv] cap slot [(y, sl)]).run σ = ({(.ok i, σ)}, False) := by + intro nb hnb + refine ⟨.threadControlCaps (objRefOf cap) slot none none none none (some (v.bv, nb)), ?_⟩ + unfold decodeSetIpcBuffer + rw [show Fl4Spec.NonDetE.whenE (σ := Fl4SpecA64.State Unit) ([v.bv].length = 0) + (Fl4Spec.NonDetE.throwError Fl4SpecA64.SyscallError.truncatedMessage) = pure () from if_neg (by simp)] + rw [run_bind_okD (a := ()) (s' := σ) rfl] + rw [show Fl4Spec.NonDetE.whenE (σ := Fl4SpecA64.State Unit) ([(y, sl)].length = 0) + (Fl4Spec.NonDetE.throwError Fl4SpecA64.SyscallError.truncatedMessage) = pure () from if_neg (by simp)] + rw [run_bind_okD (a := ()) (s' := σ) rfl] + rw [run_bind_okD (a := v.bv) (s' := σ) rfl] + rw [run_bind_okD (a := (y, sl)) (s' := σ) rfl] + dsimp only + rw [run_bind_okD hnb] + rfl + by_cases h0 : v = 0#u64 + · have h0' : v.bv = 0 := by rw [h0]; rfl + exact run none (by rw [if_pos h0']; rfl) + have h0' : v.bv ≠ 0 := fun c => h0 (UScalar.eq_equiv_bv_eq _ _ |>.2 c) + unfold tcb.derive_for_buffer at h + simp only [if_neg h0, capA_eq, bind_ok] at h + obtain ⟨r, hr, r', hr', hrel⟩ := derive_relEqA (st := st) s σ bs sl _ y hcr (CapRt.decodeF_encWf _ _) hnc + rw [hr, bind_ok] at h + cases r with + | Err e => + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, Std.bind_tc_ok] at h + exact absurd (ok_inj_ff h) (by simp) + | Ok c' => + cases r' with + | error _ => exact hrel.elim + | ok an => + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok] at h + obtain ⟨r1, hr1, h⟩ := bind_eq_ok_std h + cases r1 with + | Err e => + simp only [bind_ok, core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, Std.bind_tc_ok] at h + exact absurd (ok_inj_ff h) (by simp) + | Ok u => + obtain ⟨p, sz, rights, dev, m, e, asid, va, rfl⟩ : + ∃ p sz rights dev m e asid va, c' = .Frame p sz rights dev m e asid va := by + cases c' + case Frame => exact ⟨_, _, _, _, _, _, _, _, rfl⟩ + all_goals + unfold vspace.check_valid_ipc_buffer at hr1 + simp only [error.illegal] at hr1 + exact absurd (ok_inj_ff hr1) (by simp) + obtain ⟨rfl, rfl, rfl, rfl⟩ := derivedFrameA (st := st) hr + have hy : an = absCapA (.Frame p sz rights dev false false 0#u64 0#u64) := by + have he := hrel.2.1 + unfold CapEqA at he + exact he.symm + rw [tcbBufferCheck_nativeA] at hr1 + have hr1 := ok_inj_ff hr1 + have hck : (checkValidIpcBuffer (Z := Unit) v.bv an).run σ = ({(.ok (), σ)}, False) := by + rw [hy, tcbBufferCheck_specA] + cases dev + · simp only [Bool.false_eq_true, ite_false] at hr1 ⊢ + by_cases ha : isAligned v.bv msgAlignBits + · rw [if_pos ha] + · rw [if_neg ha] at hr1; exact absurd hr1 (by simp) + · simp only [ite_true] at hr1; exact absurd hr1 (by simp) + refine run (some (an, sl)) ?_ + rw [if_neg h0', run_bind_okD hr', run_bind_okD hck] + rfl + +/-- The VTable predicate of the Ext decode is FL4's. -/ +theorem vtP_extA {c : capability.Cap} {z : Fl4SpecA64.Cap} (hw : CapWfA c) (he : CapEqA c z) : + vspace.is_valid_vtable_root c = ok (isValidVtableRoot z || fl4MappedStage2Root z) := by + rw [tcbVtableRoot_eqA c hw] + rcases he.eq_or_pt with e | ⟨_, _, _, e⟩ + · rw [e] + · have : ∃ p a v, c = .PageTable p true a v := by + unfold CapEqA at he + split at he + · exact ⟨_, _, _, rfl⟩ + · rw [e] at he + cases c with + | PageTable p m asid va => + cases m with + | false => simp [absCapA, mapDataA] at he + | true => exact ⟨_, _, _, rfl⟩ + | Frame => + simp only [absCapA, absFrameE] at he + split at he <;> cases he + | _ => simp [absCapA] at he + obtain ⟨p, a, v, rfl⟩ := this + obtain ⟨_, _, hz, -, -⟩ := CapEqA.pt_mapped_iff.mp he + rw [hz]; rfl + +/-- `derive_roots` against the root decode the spec runs at the VTable source `y1`. -/ +theorem roots_corrA {OS : Set U64} {G : List (List U64)} (F : FinStA st th OS G s σ) (ht : th t = some .tcb) + {cdata vdata : U64} {cs vs : obj.Slot} {sl sl1 slot : Fl4SpecA64.CslotPtr} {y y1 : Fl4SpecA64.Cap} + {rest : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)} (hcr : CapRelA (capA st s cs) y) + (hcr1 : CapRelA (capA st s vs) y1) + (hnc : isUtA (capA st s cs) = true → NoChildrenRelA (st := st) s σ cs sl) + (hnc1 : isUtA (capA st s vs) = true → NoChildrenRelA (st := st) s σ vs sl1) + {r : core.result.Result (capability.Cap × capability.Cap) error.Exc} + (h : tcb.derive_roots st s t cdata vdata cs vs = ok r) (args : List Fl4SpecA64.Data) (P : Fl4SpecA64.Cap → Bool) + (hP : (P = fun v => isValidVtableRoot v || fl4MappedStage2Root v) ∨ + (P = isValidVtableRoot ∧ fl4MappedStage2Root y1 = false)) : + match r with + | .Ok _ => ∃ v, (cvProg P (cdata.bv :: vdata.bv :: args) (.threadCap (specPtr t)) slot + ((y, sl) :: (y1, sl1) :: rest)).run σ = ({(.ok v, σ)}, False) + | .Err x => ∃ e', (cvProg P (cdata.bv :: vdata.bv :: args) (.threadCap (specPtr t)) slot + ((y, sl) :: (y1, sl1) :: rest)).run σ = ({(.error e', σ)}, False) ∧ VsErrRelA x e' := by + refine deriveRoots_relA F ht hcr hcr1 hnc hnc1 P (fun c z _ hw he hd => ?_) h + rw [vtP_extA hw he] + rcases hP with rfl | ⟨rfl, hs⟩ + · rfl + have : fl4MappedStage2Root z = false := by + cases hz : fl4MappedStage2Root z with + | false => rfl + | true => rw [stage2_deriveA hd hz] at hs; exact absurd hs (by simp) + rw [this, Bool.or_false] + +theorem tcbSpace.decErrA (hA : ArchTotal ar) (hne : x ≠ .Preempted) (hst : smp.remote_tcb_stall st ar s a t = ok (s, a)) + (hAr : TcbArgsA st s ctx argsU) (hP : PlaceA th kb) (hX : ExtraOKA th (L.mem s) ctx.extra excaps) + {OS : Set U64} {G : List (List U64)} (F : FinStA st th OS G s σ) (ht : th t = some .tcb) + (hnc : ∀ (i : Nat) (hi : i < excaps.length), isUtA (capA st s (exAtA ctx.extra i)) = true → + NoChildrenRelA (st := st) s σ (exAtA ctx.extra i) (excaps[i]).2) + (slot : Fl4SpecA64.CslotPtr) + (h : tcb.decode st ar s a fl4_abi.label.TCBSetSpace t ctx = ok (.Err x, s', a')) : + DecEA σ x (fl4DecodeSetSpace (Z := Unit) (argsU.map UScalar.bv) (.threadCap (specPtr t)) slot excaps) := by + obtain ⟨hlen, hargs⟩ := hAr + rw [tcbSetSpace_programA hst] at h + unfold tcb.decode_set_space at h + have he0 := tcbExtraWL_okA (L := L) (ctx := ctx) hP hX 0#usize (by simp) 2#u64 + have he1 := tcbExtraWL_okA (L := L) (ctx := ctx) hP hX 1#usize (by simp) 2#u64 + have he2 := tcbExtraWL_okA (L := L) (ctx := ctx) hP hX 2#usize (by simp) 2#u64 + simp only [UScalar.ofNatCore_val_eq] at he0 he1 he2 + by_cases hall : 2 < excaps.length ∧ 2#u64 ≤ ctx.length + swap + · have hx : x = .Sys .TruncatedMessage := by + rw [he0] at h + by_cases c0 : 0 < excaps.length ∧ 2#u64 ≤ ctx.length + swap + · rw [if_neg c0] at h + simp only [bind_ok, core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, Std.bind_tc_ok] at h + exact tcbErr_injA h + rw [if_pos c0] at h + simp only [bind_ok, core.result.Result.Insts.CoreOpsTry.branch] at h + rw [he1] at h + by_cases c1 : 1 < excaps.length ∧ 2#u64 ≤ ctx.length + swap + · rw [if_neg c1] at h + simp only [bind_ok, core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, Std.bind_tc_ok] at h + exact tcbErr_injA h + rw [if_pos c1] at h + simp only [bind_ok, core.result.Result.Insts.CoreOpsTry.branch] at h + rw [he2, if_neg hall] at h + simp only [bind_ok, core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, Std.bind_tc_ok] at h + exact tcbErr_injA h + have hc : (argsU.map UScalar.bv).length < 2 ∨ excaps.length < 3 := by + rw [List.length_map, ← hlen] + by_cases c : 2#u64 ≤ ctx.length + · exact Or.inr (by + by_contra hh + exact hall ⟨by omega, c⟩) + · exact Or.inl (by scalar_tac) + refine ⟨.truncatedMessage, ?_, Or.inl (by rw [hx]; exact trivial)⟩ + have hw : Fl4Spec.NonDetE.whenE (σ := Fl4SpecA64.State Unit) + ((argsU.map UScalar.bv).length < 2 ∨ excaps.length < 3) + (Fl4Spec.NonDetE.throwError Fl4SpecA64.SyscallError.truncatedMessage) = + Fl4Spec.NonDetE.throwError .truncatedMessage := if_pos hc + unfold fl4DecodeSetSpace decodeSetSpace + split <;> (rw [hw]; exact run_bind_errorD (run_throwErrorD _ σ) _) + obtain ⟨h3, hl⟩ := hall + have hl' : 2 ≤ argsU.length := by rw [← hlen]; scalar_tac + obtain ⟨c, v, rest, rfl⟩ : ∃ c v rest, argsU = c :: v :: rest := by + match argsU, hl' with + | c :: v :: rest, _ => exact ⟨c, v, rest, rfl⟩ + obtain ⟨y0, y1, y2, ys, rfl⟩ : ∃ y0 y1 y2 ys, excaps = y0 :: y1 :: y2 :: ys := by + match excaps, h3 with + | y0 :: y1 :: y2 :: ys, _ => exact ⟨y0, y1, y2, ys, rfl⟩ + rw [he0, if_pos ⟨by simp, hl⟩] at h + simp only [bind_ok, core.result.Result.Insts.CoreOpsTry.branch] at h + rw [he1, if_pos ⟨by simp, hl⟩] at h + simp only [bind_ok, core.result.Result.Insts.CoreOpsTry.branch] at h + rw [he2, if_pos ⟨by simp, hl⟩] at h + simp only [bind_ok, core.result.Result.Insts.CoreOpsTry.branch] at h + have hc0 := hargs 0#u64 c (by simp) + have hc1 := hargs 1#u64 v (by simp) + have cr : ∀ (i : Nat) (hi : i < (y0 :: y1 :: y2 :: ys).length), + CapRelA (capA st s (exAtA ctx.extra i)) ((y0 :: y1 :: y2 :: ys)[i]).1 := by + intro i hi + have := (srcOK_factsA hP (hX.2.1 i hi)).2.2.2.1 + rwa [capAtA_eq (st := st)] at this + have cr0 : CapRelA (capA st s (exAtA ctx.extra 0)) y0.1 := cr 0 (by simp) + have cr1 : CapRelA (capA st s (exAtA ctx.extra 1)) y1.1 := cr 1 (by simp) + have cr2 : CapRelA (capA st s (exAtA ctx.extra 2)) y2.1 := cr 2 (by simp) + simp only [hc0, hc1, bind_ok] at h + have hnc0 : isUtA (capA st s (exAtA ctx.extra 0)) = true → + NoChildrenRelA (st := st) s σ (exAtA ctx.extra 0) y0.2 := hnc 0 (by simp) + have hnc1 : isUtA (capA st s (exAtA ctx.extra 1)) = true → + NoChildrenRelA (st := st) s σ (exAtA ctx.extra 1) y1.2 := hnc 1 (by simp) + have hnc2 : isUtA (capA st s (exAtA ctx.extra 2)) = true → + NoChildrenRelA (st := st) s σ (exAtA ctx.extra 2) y2.2 := hnc 2 (by simp) + obtain ⟨r, hr, h⟩ := bind_eq_ok_std h + have corr := fun P hPr => roots_corrA (σ := σ) (slot := slot) (rest := []) F ht cr1 cr2 + hnc1 hnc2 hr [] P hPr + have hw : Fl4Spec.NonDetE.whenE (σ := Fl4SpecA64.State Unit) + (((c :: v :: rest).map UScalar.bv).length < 2 ∨ (y0 :: y1 :: y2 :: ys).length < 3) + (Fl4Spec.NonDetE.throwError Fl4SpecA64.SyscallError.truncatedMessage) = pure () := if_neg (by simp) + have e1 : ((c :: v :: rest).map UScalar.bv).take 2 = [c.bv, v.bv] := by simp + have e2 : ((y0 :: y1 :: y2 :: ys).drop 1).take 2 = [(y1.1, y1.2), (y2.1, y2.2)] := by simp + have e3 : (the (y0 :: y1 :: y2 :: ys)[2]?) = y2 := by simp [the] + have e4 : (the (y0 :: y1 :: y2 :: ys)[0]?) = y0 := by simp [the] + cases r with + | Err e => + simp only [bind_ok, core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, Std.bind_tc_ok] at h + have ex := tcbErr_injA h + subst ex + cases hq : fl4MappedStage2Root y2.1 with + | true => + obtain ⟨e', hrun, hrel⟩ := corr _ (Or.inl rfl) + refine ⟨e', ?_, hrel⟩ + unfold fl4DecodeSetSpace + rw [if_pos (by rw [e3]; exact hq), hw, run_bind_okD (a := ()) (s' := σ) rfl, e1, e2, fl4CvSpace_progA] + exact run_bind_errorD hrun _ + | false => + obtain ⟨e', hrun, hrel⟩ := corr _ (Or.inr ⟨rfl, hq⟩) + refine ⟨e', ?_, hrel⟩ + unfold fl4DecodeSetSpace + rw [if_neg (by rw [e3, hq]; simp)] + unfold decodeSetSpace + rw [hw, run_bind_okD (a := ()) (s' := σ) rfl, e1, e2, decodeCvSpace_progA] + exact run_bind_errorD hrun _ + | Ok val => + obtain ⟨ccap, vcap⟩ := val + simp only [bind_ok, core.result.Result.Insts.CoreOpsTry.branch, capA_eq] at h + obtain ⟨b, hb, h⟩ := bind_eq_ok_std h + cases b with + | true => + simp only [ite_true] at h + obtain ⟨s1, -, h⟩ := bind_eq_ok_std h + obtain ⟨i, -, h⟩ := bind_eq_ok_std h + have K := tcb.thread_control_caps.preA (st := st) (ar := ar) hA s1 a t ctx.slot + (capA st s (exAtA ctx.extra 0), exAtA ctx.extra 0) (capability.Cap.Null, 0#u64) + (ccap, exAtA ctx.extra 1) (vcap, exAtA ctx.extra 2) 0#u64 (capability.Cap.Null, 0#u64) i + rw [h, WP.spec_ok] at K + exact absurd (K x rfl) hne + | false => + simp only [Bool.false_eq_true, ite_false, error.invalid_cap, bind_ok] at h + have ex := tcbErr_injA h + have hv : ¬ validFaultHandler y0.1 := by + rw [validFH_eqA cr0.2, ← tcbValidFaultHandlerA _ cr0.1, hb] + intro e + exact Bool.noConfusion (ok_inj_ff e) + have hfh : (checkHandlerEp (Z := Unit) 1 y0).run σ = ({(.error (.invalidCapability 1), σ)}, False) := by + unfold checkHandlerEp + rw [show Fl4Spec.NonDetE.unlessE (σ := Fl4SpecA64.State Unit) (validFaultHandler y0.1) + (Fl4Spec.NonDetE.throwError (Fl4SpecA64.SyscallError.invalidCapability 1)) = + Fl4Spec.NonDetE.throwError (.invalidCapability 1) from if_neg hv] + exact run_bind_errorD (run_throwErrorD _ σ) _ + refine ⟨.invalidCapability 1, ?_, Or.inr ⟨1#u64, ex, rfl⟩⟩ + cases hq : fl4MappedStage2Root y2.1 with + | true => + obtain ⟨sp, hrun⟩ := corr _ (Or.inl rfl) + unfold fl4DecodeSetSpace + rw [if_pos (by rw [e3]; exact hq), hw, run_bind_okD (a := ()) (s' := σ) rfl, e1, e2, fl4CvSpace_progA, + run_bind_okD hrun, e4] + exact run_bind_errorD hfh _ + | false => + obtain ⟨sp, hrun⟩ := corr _ (Or.inr ⟨rfl, hq⟩) + unfold fl4DecodeSetSpace + rw [if_neg (by rw [e3, hq]; simp)] + unfold decodeSetSpace + rw [hw, run_bind_okD (a := ()) (s' := σ) rfl, e1, e2, decodeCvSpace_progA, run_bind_okD hrun, e4, + run_bind_okD (a := y0) (s' := σ) rfl] + exact run_bind_errorD hfh _ + +theorem tcbConfig.decErrA (hA : ArchTotal ar) (hne : x ≠ .Preempted) + (hst : smp.remote_tcb_stall st ar s a t = ok (s, a)) + (hAr : TcbArgsA st s ctx argsU) (hP : PlaceA th kb) (hX : ExtraOKA th (L.mem s) ctx.extra excaps) + {OS : Set U64} {G : List (List U64)} (F : FinStA st th OS G s σ) (ht : th t = some .tcb) + (hnc : ∀ (i : Nat) (hi : i < excaps.length), isUtA (capA st s (exAtA ctx.extra i)) = true → + NoChildrenRelA (st := st) s σ (exAtA ctx.extra i) (excaps[i]).2) + (slot : Fl4SpecA64.CslotPtr) + (h : tcb.decode st ar s a fl4_abi.label.TCBConfigure t ctx = ok (.Err x, s', a')) : + DecEA σ x (fl4DecodeTcbConfigure (Z := Unit) (argsU.map UScalar.bv) (.threadCap (specPtr t)) slot excaps) := by + obtain ⟨hlen, hargs⟩ := hAr + rw [tcbConfigure_programA hst] at h + unfold tcb.decode_configure at h + have he0 := tcbExtraWL_okA (L := L) (ctx := ctx) hP hX 0#usize (by simp) 3#u64 + have he1 := tcbExtraWL_okA (L := L) (ctx := ctx) hP hX 1#usize (by simp) 3#u64 + have he2 := tcbExtraWL_okA (L := L) (ctx := ctx) hP hX 2#usize (by simp) 3#u64 + simp only [UScalar.ofNatCore_val_eq] at he0 he1 he2 + by_cases hall : 2 < excaps.length ∧ 3#u64 ≤ ctx.length + swap + · have hx : x = .Sys .TruncatedMessage := by + rw [he0] at h + by_cases c0 : 0 < excaps.length ∧ 3#u64 ≤ ctx.length + swap + · rw [if_neg c0] at h + simp only [bind_ok, core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, Std.bind_tc_ok] at h + exact tcbErr_injA h + rw [if_pos c0] at h + simp only [bind_ok, core.result.Result.Insts.CoreOpsTry.branch] at h + rw [he1] at h + by_cases c1 : 1 < excaps.length ∧ 3#u64 ≤ ctx.length + swap + · rw [if_neg c1] at h + simp only [bind_ok, core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, Std.bind_tc_ok] at h + exact tcbErr_injA h + rw [if_pos c1] at h + simp only [bind_ok, core.result.Result.Insts.CoreOpsTry.branch] at h + rw [he2, if_neg hall] at h + simp only [bind_ok, core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, Std.bind_tc_ok] at h + exact tcbErr_injA h + refine ⟨.truncatedMessage, ?_, Or.inl (by rw [hx]; exact trivial)⟩ + unfold fl4DecodeTcbConfigure decodeTcbConfigure + by_cases ha : (argsU.map UScalar.bv).length < 3 + · have hw : Fl4Spec.NonDetE.whenE (σ := Fl4SpecA64.State Unit) ((argsU.map UScalar.bv).length < 3) + (Fl4Spec.NonDetE.throwError Fl4SpecA64.SyscallError.truncatedMessage) = + Fl4Spec.NonDetE.throwError .truncatedMessage := if_pos ha + split <;> (rw [hw]; exact run_bind_errorD (run_throwErrorD _ σ) _) + have hc : excaps.length < 3 := by + rw [List.length_map, ← hlen] at ha + by_contra hh + exact hall ⟨by omega, by scalar_tac⟩ + have hw : Fl4Spec.NonDetE.whenE (σ := Fl4SpecA64.State Unit) ((argsU.map UScalar.bv).length < 3) + (Fl4Spec.NonDetE.throwError Fl4SpecA64.SyscallError.truncatedMessage) = pure () := if_neg ha + have hw' : Fl4Spec.NonDetE.whenE (σ := Fl4SpecA64.State Unit) (excaps.length < 3) + (Fl4Spec.NonDetE.throwError Fl4SpecA64.SyscallError.truncatedMessage) = + Fl4Spec.NonDetE.throwError .truncatedMessage := if_pos hc + split <;> (rw [hw, run_bind_okD (a := ()) (s' := σ) rfl, hw']; exact run_bind_errorD (run_throwErrorD _ σ) _) + obtain ⟨h3, hl⟩ := hall + have hl' : 3 ≤ argsU.length := by rw [← hlen]; scalar_tac + obtain ⟨c, v, b, rest, rfl⟩ : ∃ c v b rest, argsU = c :: v :: b :: rest := by + match argsU, hl' with + | c :: v :: b :: rest, _ => exact ⟨c, v, b, rest, rfl⟩ + obtain ⟨y0, y1, y2, ys, rfl⟩ : ∃ y0 y1 y2 ys, excaps = y0 :: y1 :: y2 :: ys := by + match excaps, h3 with + | y0 :: y1 :: y2 :: ys, _ => exact ⟨y0, y1, y2, ys, rfl⟩ + rw [he0, if_pos ⟨by simp, hl⟩] at h + simp only [bind_ok, core.result.Result.Insts.CoreOpsTry.branch] at h + rw [he1, if_pos ⟨by simp, hl⟩] at h + simp only [bind_ok, core.result.Result.Insts.CoreOpsTry.branch] at h + rw [he2, if_pos ⟨by simp, hl⟩] at h + simp only [bind_ok, core.result.Result.Insts.CoreOpsTry.branch] at h + have hc0 := hargs 0#u64 c (by simp) + have hc1 := hargs 1#u64 v (by simp) + have hc2 := hargs 2#u64 b (by simp) + have cr : ∀ (i : Nat) (hi : i < (y0 :: y1 :: y2 :: ys).length), + CapRelA (capA st s (exAtA ctx.extra i)) ((y0 :: y1 :: y2 :: ys)[i]).1 := by + intro i hi + have := (srcOK_factsA hP (hX.2.1 i hi)).2.2.2.1 + rwa [capAtA_eq (st := st)] at this + have cr0 : CapRelA (capA st s (exAtA ctx.extra 0)) y0.1 := cr 0 (by simp) + have cr1 : CapRelA (capA st s (exAtA ctx.extra 1)) y1.1 := cr 1 (by simp) + have cr2 : CapRelA (capA st s (exAtA ctx.extra 2)) y2.1 := cr 2 (by simp) + simp only [hc0, hc1, hc2, bind_ok] at h + have hnc0 : isUtA (capA st s (exAtA ctx.extra 0)) = true → + NoChildrenRelA (st := st) s σ (exAtA ctx.extra 0) y0.2 := hnc 0 (by simp) + have hnc1 : isUtA (capA st s (exAtA ctx.extra 1)) = true → + NoChildrenRelA (st := st) s σ (exAtA ctx.extra 1) y1.2 := hnc 1 (by simp) + have hnc2 : isUtA (capA st s (exAtA ctx.extra 2)) = true → + NoChildrenRelA (st := st) s σ (exAtA ctx.extra 2) y2.2 := hnc 2 (by simp) + have hw : Fl4Spec.NonDetE.whenE (σ := Fl4SpecA64.State Unit) + (((c :: v :: b :: rest).map UScalar.bv).length < 3) + (Fl4Spec.NonDetE.throwError Fl4SpecA64.SyscallError.truncatedMessage) = pure () := if_neg (by simp) + have hw' : Fl4Spec.NonDetE.whenE (σ := Fl4SpecA64.State Unit) ((y0 :: y1 :: y2 :: ys).length < 3) + (Fl4Spec.NonDetE.throwError Fl4SpecA64.SyscallError.truncatedMessage) = pure () := if_neg (by simp) + have e1 : ((c :: v :: b :: rest).map UScalar.bv).take 2 = [c.bv, v.bv] := by simp + have e2 : (y0 :: y1 :: y2 :: ys).take 2 = [(y0.1, y0.2), (y1.1, y1.2)] := by simp + have e3 : (the (y0 :: y1 :: y2 :: ys)[1]?) = y1 := by simp [the] + have e4 : (the (y0 :: y1 :: y2 :: ys)[2]?) = (y2.1, y2.2) := by simp [the] + have e5 : (the ((c :: v :: b :: rest).map UScalar.bv)[2]?) = b.bv := by simp [the] + obtain ⟨rb, hrb, h⟩ := bind_eq_ok_std h + cases rb with + | Err e => + simp only [bind_ok, core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, Std.bind_tc_ok] at h + have ex := tcbErr_injA h + subst ex + obtain ⟨e', hrun, hrel⟩ := ipcDec_errA (σ := σ) (cap := .threadCap (specPtr t)) (slot := slot) cr2 + hnc2 hrb + refine ⟨e', ?_, hrel⟩ + cases hq : fl4MappedStage2Root y1.1 with + | true => + unfold fl4DecodeTcbConfigure + rw [if_pos (by rw [e3]; exact hq), hw, run_bind_okD (a := ()) (s' := σ) rfl, hw', + run_bind_okD (a := ()) (s' := σ) rfl, e4, e5] + exact run_bind_errorD hrun _ + | false => + unfold fl4DecodeTcbConfigure + rw [if_neg (by rw [e3, hq]; simp)] + unfold decodeTcbConfigure + rw [hw, run_bind_okD (a := ()) (s' := σ) rfl, hw', run_bind_okD (a := ()) (s' := σ) rfl, + run_bind_okD (a := b.bv) (s' := σ) (by rw [e5]; rfl), + run_bind_okD (a := (y2.1, y2.2)) (s' := σ) (by rw [e4]; rfl)] + exact run_bind_errorD hrun _ + | Ok sel => + simp only [bind_ok, core.result.Result.Insts.CoreOpsTry.branch] at h + obtain ⟨i, hrun⟩ := ipcDec_okA (σ := σ) (cap := .threadCap (specPtr t)) (slot := slot) cr2 hnc2 hrb + obtain ⟨r, hr, h⟩ := bind_eq_ok_std h + have corr := fun P hPr => roots_corrA (σ := σ) (slot := slot) (rest := []) F ht cr0 cr1 + hnc0 hnc1 hr [] P hPr + cases r with + | Err e => + simp only [bind_ok, core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, Std.bind_tc_ok] at h + have ex := tcbErr_injA h + subst ex + cases hq : fl4MappedStage2Root y1.1 with + | true => + obtain ⟨e', hcv, hrel⟩ := corr _ (Or.inl rfl) + refine ⟨e', ?_, hrel⟩ + unfold fl4DecodeTcbConfigure + rw [if_pos (by rw [e3]; exact hq), hw, run_bind_okD (a := ()) (s' := σ) rfl, hw', + run_bind_okD (a := ()) (s' := σ) rfl, e4, e5, run_bind_okD hrun, e1, e2, fl4CvSpace_progA] + exact run_bind_errorD hcv _ + | false => + obtain ⟨e', hcv, hrel⟩ := corr _ (Or.inr ⟨rfl, hq⟩) + refine ⟨e', ?_, hrel⟩ + unfold fl4DecodeTcbConfigure + rw [if_neg (by rw [e3, hq]; simp)] + unfold decodeTcbConfigure + rw [hw, run_bind_okD (a := ()) (s' := σ) rfl, hw', run_bind_okD (a := ()) (s' := σ) rfl, + run_bind_okD (a := b.bv) (s' := σ) (by rw [e5]; rfl), + run_bind_okD (a := (y2.1, y2.2)) (s' := σ) (by rw [e4]; rfl), run_bind_okD hrun, e1, e2, + decodeCvSpace_progA] + exact run_bind_errorD hcv _ + | Ok val => + obtain ⟨ccap, vcap⟩ := val + simp only [bind_ok, core.result.Result.Insts.CoreOpsTry.branch] at h + obtain ⟨s1, -, h⟩ := bind_eq_ok_std h + obtain ⟨j, -, h⟩ := bind_eq_ok_std h + have K := tcb.thread_control_caps.preA (st := st) (ar := ar) hA s1 a t ctx.slot (capability.Cap.Null, 0#u64) + (capability.Cap.Null, 0#u64) (ccap, exAtA ctx.extra 0) (vcap, exAtA ctx.extra 1) b sel j + rw [h, WP.spec_ok] at K + exact absurd (K x rfl) hne + +end + +end + +end Fl4ProofsA64.P3.ReplyErr diff --git a/proofs/Fl4ProofsA64/P3/Stage3DecErrUtA.lean b/proofs/Fl4ProofsA64/P3/Stage3DecErrUtA.lean new file mode 100644 index 000000000..55f3c491f --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3DecErrUtA.lean @@ -0,0 +1,109 @@ +/- +P3 (aarch64, A64-P3 Lift 1b, batch a): **`DecErrSpecPA` for Untyped caps**, from `untyped_decode_refinesA`. + +`untyped_decode_refinesA` with `Q := (res.1 is `Preempted` if it is an `Err`)`: its error disjunct is the spec's error +with nothing changed, and the retype after a successful spec decode fails only by preemption (`untyped.retype.preA`). +The premises are the record's (`JSyscallA.decPreA`, `sysPlace`, `utBlk`, `UtDecP.of_live`) exactly as `utDecode_noResetA`. + +Named premises: [P] `hv` (the platform's virtualization word), `hfan` (`retypeFanOutLimit = 256`), `A64FrameMasks`; P0's `ArchTotal`. New module. +-/ +import Fl4ProofsA64.P3.Stage3DecErrBaseA +import Fl4ProofsA64.P3.UtRtDecSiteA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.ReplyErr +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3 Fl4ProofsA64.P3.Endp + Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J7 : JSlotA S} {ar : machine.Arch A} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} + +set_option maxHeartbeats 8000000 in +/-- **`DecErrSpecPA` for Untyped caps** (module docstring). -/ +theorem DecErrSpecPA.untyped (hA : P0.ArchTotal ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + (hfm : A64FrameMasks st s) (hfan : Fl4SpecA64.retypeFanOutLimit = 256#64) + (hv : ∃ vi, ar.virtualization a = ok vi ∧ (vi = 0#u64 ↔ fl4A64Virt = false)) + {calling blocking donate : Bool} {cptr : U64} : + DecErrSpecPA st ar Z.th s a σ calling blocking donate cptr UtCapA := by + intro info buf r1 e x s' a' mi sl c excaps buffer args einfo ebuf elr eex hd hne hP hlab hsl hcr hok hab hbv + gmi ggc glb gex gmrs + obtain ⟨p, sb, fi, dev, hc⟩ : ∃ p sb fi dev, capA st s r1.slot = .Untyped p sb fi dev := by + revert hP + generalize capA st s r1.slot = c0 + intro h + cases c0 <;> first | exact ⟨_, _, _, _, rfl⟩ | exact h.elim + obtain ⟨vi, hvi, hvirt⟩ := hv + have hcr' := hcr + rw [hc] at hcr' + have hc2 : c = absCapA (.Untyped p sb fi dev) := hcr'.2.symm + obtain ⟨-, argsU, hargsE, hlen, hargsU⟩ := J.argsLabA einfo hbv gmi gmrs + subst hargsE + obtain ⟨_, _, _, _, _, _, Q⟩ := J.siteA einfo ebuf elr eex + have J3 := J.toJStage3A + have Je := J3.toJEntA + have hV : SlotsValidA Z.th (L.mem s) := Je.j.1.cv + have hsub : ∀ y, IsSlotA Z.th y → y ∈ OS := J3.j5.2.1 + have hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null := J3.j5.2.2 + have hR := Je.j.1.st + have hC := Je.j.1.cdt + have hPl := Je.j.1.place + have hO := Je.sys.ent.os + have hTr : TreeA st OS Z.G.chains Z.G.par s := hC.tree.grow hsub fun x hx hn => hnul x hx hn + have hsO : cslotAddrA sl ∈ OS := hsub _ (hab.isSlot hPl) + have hslot : r1.slot = cslotAddrA sl := hsl + have hcap : capA st s (cslotAddrA sl) = .Untyped p sb fi dev := by rw [← hslot]; exact hc + have hcapF : obj.Slot.cap st (cslotAddrA sl) s = ok (.Untyped p sb fi dev) := by + rw [capA_eq]; exact congrArg ok hcap + have D := J.decPreA Q.hOS Q.hext + have P := J3.sysPlace Q.hext Q.hOS Q.cov + rw [hslot, hcap] at D + rw [hslot] at P + have R := UtDecP.of_live D Je.sys.sysl.sys P hcapF Je.sys.sysl.ln Je.sys.sysl.vr Je.sys.sysl.ly + have hrv : obj.Slot.revocable st (cslotAddrA sl) s = ok true := + Je.sys.sysl.sys.stz.1.2.1 _ hsO (by rw [hcap]; rfl) + have hsb : sb.val ≤ 39 := J3.utBlk _ hsO p sb fi dev hcap + have hdc : untyped.decode st ar s a info.label (msgLenA info buf) r1.slot (.Untyped p sb fi dev) buf e = + ok (.Err x, s', a') := by + have hd2 := hd + rw [hc] at hd2 + simpa only [syscall.decode_invocation, cur_thread_ok, bind_ok] using hd2 + rw [hslot] at hdc + have hl : genInvocationType (specLabelWord (fl4LabelOf mi.miLabel)) = .untypedRetype ↔ + info.label = fl4_abi.label.UntypedRetype := by + rw [hlab]; exact ut_labelA info.label + have K := untyped_decode_refinesA ar Z.th s a σ Z.G hR hV hPl hO hTr hC hsub hfm vi hvi hvirt + hfan info.label (specLabelWord (fl4LabelOf mi.miLabel)) hl (msgLenA info buf) buf argsU hlen + hargsU sl hab hsO p sb fi dev hcap hrv hsb R.utv.2.2 e excaps hok (fun _ res => PreOnlyA res.1) ?_ + · rw [hdc, WP.spec_ok] at K + rcases K with ⟨e0, e', hres, hrun, hrel⟩ | ⟨inv, -, hq⟩ + · obtain ⟨h1, -, -⟩ := Prod.mk.inj hres + have hx : x = e0 := by injection h1 + have hrel' : VsErrRelA x e' := by rw [hx]; exact Or.inl hrel + obtain ⟨e2, hxe⟩ := sys_of_vsErrRelA hrel' + refine ⟨e2, e', hxe, hrel', ?_⟩ + rw [hc2] + have hd' : fl4DecodeInvocation (Z := Unit) calling false mi.miLabel (argsU.map UScalar.bv) + (Fl4SpecA64.toBl cptr.bv) sl (absCapA (.Untyped p sb fi dev)) excaps buffer = + Fl4Spec.NonDetE.liftME Fl4Invocation.invokeUntyped + (fl4DecodeUntypedInvocationVirt (Z := Unit) (specLabelWord (fl4LabelOf mi.miLabel)) + (argsU.map UScalar.bv) sl (absCapA (.Untyped p sb fi dev)) (excaps.map Prod.fst)) := rfl + rw [hd', run_liftME_errA hrun] + exact Set.mem_singleton _ + · exact absurd (hq x rfl) hne + · intro nt us dest off win t rd reset fiv _ _ _ + have hS : P0.StoreTotal st := L.total + step + exact untyped.retype.preA hA _ a _ _ _ _ _ _ _ _ _ + +end + +end + +end Fl4ProofsA64.P3.ReplyErr diff --git a/proofs/Fl4ProofsA64/P3/Stage3DecErrVcpuA.lean b/proofs/Fl4ProofsA64/P3/Stage3DecErrVcpuA.lean new file mode 100644 index 000000000..79b467bfd --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3DecErrVcpuA.lean @@ -0,0 +1,1054 @@ +/- +P3 (aarch64, A64-P3 Stage 3 assembly): **`DecErrSpecPA` for VCPU caps** (all labels, and the unknown label). + +A decode `Err` of `vcpu.decode` is the spec's error. `ARMVCPUInjectIRQ` / `ReadReg` / `WriteReg` / `AckVPPI` come from +S4's `Err` arms (`vcpu_*_jA`); `SetTCB`, `ReadState`, `WriteState` and `Run` are walked on FL4's checks (their S4 +theorems ask the global `FrameMemRelA`). `WriteState` / `Run` read the VGIC part of the user frame before any stall, +so the entry-state window premise is enough (`read_vgic_frame_localA`). + +Named premises (**`DecErrSpecPA.vcpu`**), all S4's, none new: +* [P] `VcpuPlatAgreeA`, `VcpuArchKeepsA`; [S] `numNodes = 1`, `hCs`, `hCsN`. +* [S] `hNoS2`: the first extra cap is no Stage-2-mapped frame (`JSyscallA.vcpuDecodeA`'s). +* [S] `hAgreeWS` / `hAgreeRun`: `FrameInputAgreeA` on the 1 KiB input window when the mask is non-zero. + +New module. +-/ +import Fl4ProofsA64.P3.Stage3DecErrBaseA +import Fl4ProofsA64.P3.Stage3T3ArmsVcpu2A +import Fl4ProofsA64.P3.Stage3VcpuBridgeA +import Fl4ProofsA64.P3.VcpuTopA +import Fl4ProofsA64.P3.Stage3DecErrMiscA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.ReplyErr +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3 Fl4ProofsA64.P3.Endp + Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4ProofsA64.P3.Vcpu + Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-- The VCPU caps. -/ +def VcpuCapA : capability.Cap → Prop + | .Vcpu _ => True + | _ => False + +/-- The spec decoder `d` errs with an error related to FL4's `x`. -/ +def VcErrA {α : Type} (σ : State Unit) (x : error.Exc) (d : SEMonad Unit α) : Prop := + ∃ e', d.run σ = ({(.error e', σ)}, False) ∧ VsErrRelA x e' + +theorem vcErr_ofA {α : Type} {σ : State Unit} {x y : error.Exc} {d : SEMonad Unit α} {e' : SyscallError} + (hx : x = y) (hr : VsErrRelA y e') (hd : d.run σ = ({(.error e', σ)}, False)) : VcErrA σ x d := + ⟨e', hd, hx ▸ hr⟩ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {s s' : S} {a a' a1 : A} {σ : State Unit} {x y : error.Exc} + +/-- `smSize` at the width of `U64.bv`. -/ +abbrev smSizeU : BitVec UScalarTy.U64.numBits := smSize + +private theorem errInj0 {s1 : S} + (h : (ok (core.result.Result.Err y, s1, a1) : Result (core.result.Result Unit error.Exc × S × A)) = + ok (.Err x, s', a')) : x = y := by + have := (Prod.mk.inj (okinj h)).1 + injection this with h' + exact h'.symm + +/-- `errInjA h`: the `Err` payload of `h`, also when the error is built by `error.truncated` etc. -/ +local macro "errInjA " h:term:max : term => `(by + first + | exact errInj0 $h + | (have h' := $h; simp only [error.truncated, error.invalid_arg, error.invalid_cap, error.illegal, error.range, bind_ok] at h'; exact errInj0 h')) + +private theorem noOkA {s1 : S} + (h : (ok (core.result.Result.Ok (), s1, a1) : Result (core.result.Result Unit error.Exc × S × A)) = + ok (.Err x, s', a')) : False := by + cases (Prod.mk.inj (okinj h)).1 + +private theorem truncA {α : Type} {d : SEMonad Unit α} (hx : x = .Sys .TruncatedMessage) + (hd : d.run σ = ({(.error .truncatedMessage, σ)}, False)) : VcErrA σ x d := + by refine vcErr_ofA hx ?_ hd; exact Or.inl trivial + +private theorem illA {α : Type} {d : SEMonad Unit α} (hx : x = .Sys .IllegalOperation) + (hd : d.run σ = ({(.error .illegalOperation, σ)}, False)) : VcErrA σ x d := + by refine vcErr_ofA hx ?_ hd; exact Or.inl trivial + +private theorem invArgA {α : Type} {d : SEMonad Unit α} (k : U64) (n : Nat) (hn : k.val = n) + (hx : x = .Sys (.InvalidArgument k)) + (hd : d.run σ = ({(.error (.invalidArgument n), σ)}, False)) : VcErrA σ x d := by + subst hn; refine vcErr_ofA hx ?_ (by exact hd); exact Or.inl rfl + +private theorem invCapA {α : Type} {d : SEMonad Unit α} (hx : x = .Sys (.InvalidCapability 1#u64)) + (hd : d.run σ = ({(.error (.invalidCapability 1), σ)}, False)) : VcErrA σ x d := + vcErr_ofA hx (Or.inr ⟨1#u64, rfl, rfl⟩) hd + +/-! ## `SetTCB` -/ + +set_option maxHeartbeats 2000000 in +theorem vcpuSetTcb.decErrA {vc : U64} {ex : Array obj.Slot 3#usize} {th : TypedHeapA} {kb : U64} + (hPl : PlaceA th kb) {extras : List (Cap × CslotPtr)} (hEx : ExtraOKA th (L.mem s) ex extras) + (h : vcpu.decode_set_tcb st ar s a vc ex = ok (.Err x, s', a')) : + VcErrA σ x (decodeVcpuSetTcb (Z := Unit) (.vcpuCap (specPtr vc)) extras) := by + unfold vcpu.decode_set_tcb at h + rw [exAtA_idx0, bind_ok, show obj.Slot.is_null (exAtA ex 0) = ok (decide (exAtA ex 0 = 0#u64)) by + unfold obj.Slot.is_null; rfl, bind_ok] at h + cases extras with + | nil => + have h0 : exAtA ex 0 = 0#u64 := hEx.2.2 0 (by simp) (by decide) + rw [if_pos (by simp [h0])] at h + exact truncA (errInjA h) rfl + | cons ep rest => + obtain ⟨hne, -, -, hcr, -⟩ := srcOK_factsA hPl (hEx.2.1 0 (by simp)) + rw [if_neg (by simp [hne]), show obj.Slot.cap st (exAtA ex 0) s = ok (capAtA (L.mem s) (exAtA ex 0)) by + rw [Fl4ProofsA64.P1.W7.R.ST.capA_eq, ← capAtA_eq], bind_ok] at h + simp only [List.getElem_cons_zero] at hcr + obtain ⟨hwf, habs⟩ := hcr + have ill : x = .Sys .IllegalOperation → (∀ t, ep.1 ≠ .threadCap t) → + VcErrA σ x (decodeVcpuSetTcb (Z := Unit) (.vcpuCap (specPtr vc)) (ep :: rest)) := by + intro hx hnt + refine illA hx ?_ + rcases ep with ⟨ep1, ep2⟩ + unfold decodeVcpuSetTcb + cases hep : ep1 with + | threadCap t => exact absurd hep (hnt t) + | _ => rfl + cases hc : capAtA (L.mem s) (exAtA ex 0) with + | Thread ptr => + rw [hc] at h; dsimp only at h + exfalso + repeat (first | (with_reducible exact (noOkA h).elim) | (rw [if_pos rfl] at h) | (rw [if_neg Bool.false_ne_true] at h) | (dsimp only [Bool.false_eq_true, eq_self, ↓reduceIte] at h) | (rw [eq_of_spec_fd Fl4ProofsA64.P0.Consts.layout.tcb.RUN_VCPU.spec, bind_ok] at h) | (refine (bind_eq_ok h).elim fun p hp => ?_; obtain ⟨-, h⟩ := hp; (try obtain ⟨_, _⟩ := p))) + exact noOkA h + | PageTable p m asid va => + rw [hc] at h habs; dsimp only at h + refine ill (errInjA h) fun t e => ?_ + cases m + · rw [e] at habs; cases habs + · rw [e] at habs; cases habs + | _ => + rw [hc] at h habs hwf; dsimp only at h + first + | exact False.elim hwf + | exact ill (errInjA h) fun t e => by rw [e] at habs; cases habs + +/-! ## The user window -/ + +private theorem frameSzA (sz : capability.FrameSize) : + ∃ bits : U64, capability.FrameSize.bits sz = ok bits ∧ 12 ≤ bits.val ∧ bits.val < 64 ∧ + bits &&& 63#u64 = bits ∧ (1 : Word) <<< pageBitsForSize (frameSizeOfA sz) = (1#u64 : U64).bv <<< bits.val ∧ + ((1#u64 : U64).bv <<< bits.val).toNat = 2 ^ bits.val := by + cases sz <;> exact ⟨_, rfl, by decide, by decide, u64_eq_of_bv_eq (by decide), by decide, by decide⟩ + +/-- FL4's window checks on a frame of size `2 ^ bits`: an `Err` is `InvalidArgument 0` exactly where the spec's +`stateWindow` is empty, and otherwise comes from the rest `K`, with the window inside the frame. -/ +private theorem windowErrA {off bits : U64} (hb : bits.val < 64) + (hs : ((1#u64 : U64).bv <<< bits.val).toNat = 2 ^ bits.val) + {K : Result (core.result.Result Unit error.Exc × S × A)} + (h : (do + let i4 ← lift (bits &&& 63#u64) + let size1 ← 1#u64 <<< i4 + let i5 ← lift (off &&& 63#u64) + if i5 != 0#u64 then do + let r ← error.invalid_arg Unit 0#u64 + ok (r, s, a) + else if off >= size1 then do + let r ← error.invalid_arg Unit 0#u64 + ok (r, s, a) + else do + let i6 ← size1 - off + if i6 < fl4_abi.consts.vcpu_state_mask.SIZE then do + let r ← error.invalid_arg Unit 0#u64 + ok (r, s, a) + else K) = ok (.Err x, s', a')) + (hand : bits &&& 63#u64 = bits) : + (x = .Sys (.InvalidArgument 0#u64) ∧ + (off.bv &&& 63 ≠ 0 ∨ off.bv ≥ (1#u64 : U64).bv <<< bits.val ∨ (1 : Word) <<< bits.val - off.bv < smSize)) ∨ + (¬ (off.bv &&& 63 ≠ 0 ∨ off.bv ≥ (1#u64 : U64).bv <<< bits.val ∨ (1 : Word) <<< bits.val - off.bv < smSize) ∧ + off.val % 8 = 0 ∧ off.val + 1024 ≤ 2 ^ bits.val ∧ K = ok (.Err x, s', a')) := by + rw [lift_ok, bind_ok, hand, u64_shl_u64_ok hb, bind_ok, lift_ok, bind_ok] at h + set size1 : U64 := ⟨(1#u64 : U64).bv <<< bits.val⟩ with hsz + have hsv : size1.val = 2 ^ bits.val := hs + change _ ∨ (¬ (off.bv &&& 63 ≠ 0 ∨ off.bv ≥ size1.bv ∨ size1.bv - off.bv < smSizeU) ∧ _) + change (x = _ ∧ (off.bv &&& 63 ≠ 0 ∨ off.bv ≥ size1.bv ∨ size1.bv - off.bv < smSizeU)) ∨ _ + by_cases h63 : (off &&& 63#u64 != 0#u64) = true + · rw [if_pos h63] at h + exact Or.inl ⟨errInjA h, Or.inl fun e => by + have : off &&& 63#u64 = 0#u64 := u64_eq_of_bv_eq e + simp [this] at h63⟩ + rw [if_neg h63] at h + have h630 : off &&& 63#u64 = 0#u64 := by by_contra hne; exact h63 (bne_iff_ne.2 hne) + by_cases hge : off >= size1 + · rw [if_pos hge] at h; exact Or.inl ⟨errInjA h, Or.inr (Or.inl hge)⟩ + rw [if_neg hge] at h + have hlt : off.val < size1.val := by have : ¬ size1.val ≤ off.val := hge; omega + rw [show size1 - off = ok ⟨BitVec.ofNat 64 (size1.val - off.val)⟩ by + change UScalar.sub size1 off = _; unfold UScalar.sub + rw [if_neg (Nat.not_lt.mpr (Nat.le_of_lt hlt))] + congr 1, bind_ok] at h + have hsub : (⟨BitVec.ofNat 64 (size1.val - off.val)⟩ : U64).val = size1.val - off.val := by + change (BitVec.ofNat 64 _).toNat = _ + have : size1.val < 2 ^ 64 := size1.bv.isLt + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + have hsbv : (size1.bv - off.bv).toNat = size1.val - off.val := by + rw [BitVec.toNat_sub_of_le (by exact Nat.le_of_lt hlt)]; rfl + have hc : fl4_abi.consts.vcpu_state_mask.SIZE.val = 1024 := by + unfold fl4_abi.consts.vcpu_state_mask.SIZE; rfl + have hsm : smSize.toNat = 1024 := rfl + have hsmU : smSizeU.toNat = 1024 := rfl + by_cases hsmall : (⟨BitVec.ofNat 64 (size1.val - off.val)⟩ : U64) < fl4_abi.consts.vcpu_state_mask.SIZE + · rw [if_pos hsmall] at h + refine Or.inl ⟨errInjA h, Or.inr (Or.inr ?_)⟩ + change (size1.bv - off.bv).toNat < smSizeU.toNat + have : (⟨BitVec.ofNat 64 (size1.val - off.val)⟩ : U64).val < fl4_abi.consts.vcpu_state_mask.SIZE.val := hsmall + omega + rw [if_neg hsmall] at h + have hbig : 1024 ≤ size1.val - off.val := by + have : ¬ (⟨BitVec.ofNat 64 (size1.val - off.val)⟩ : U64).val < fl4_abi.consts.vcpu_state_mask.SIZE.val := hsmall + omega + have h64 : off.val % 64 = 0 := by + have hv : (off &&& 63#u64).val = 0 := by rw [h630]; rfl + change off.val &&& 63 = 0 at hv + have h6 := Nat.and_two_pow_sub_one_eq_mod off.val 6 + simp only [Nat.reducePow, Nat.reduceSub] at h6; omega + refine Or.inr ⟨?_, by omega, by omega, h⟩ + rintro (e | e | e) + · exact e (by have := congrArg UScalar.bv h630; exact this) + · exact hge e + · have : (size1.bv - off.bv).toNat < smSizeU.toNat := e + omega + +/-- The VGIC read of a RAM frame's window refines `readVgicFrame` (S4's `read_vgic_frame_localA`, its window facts from +the record). -/ +private theorem vgicNoneA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} + {κ : U64 → U64} {n : Nat} {nA : U64} {m : McState} + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {y ptr off bits : U64} {sz : capability.FrameSize} {rt : U64} {mp ept : Bool} {asid va : U64} + (hy : y ∈ OS) (hc : capA st s y = .Frame ptr sz rt false mp ept asid va) + (hbits : capability.FrameSize.bits sz = ok bits) (h12 : 12 ≤ bits.val) + (hoff8 : off.val % 8 = 0) (hoff : off.val + 1024 ≤ 2 ^ bits.val) + (hAg : FrameInputAgreeA st s σ (wa ptr off)) {lrs : U64} (hlrs : lrs = ⟨BitVec.ofNat 64 fl4NumLrs⟩) + {b : Bool} (hbv : b = fl4GicV3) {res} (hr : vcpu.read_vgic_frame st s (wa ptr off) lrs b = ok res) + (hn : res = none) : + specPtr (wa ptr off) = specPtr ptr + off.bv ∧ readVgicFrame (specPtr (wa ptr off)) σ = ({(none, σ)}, False) := by + have J3 := J.toJStage3A + obtain ⟨Pt, Ub, Pp7, Tb7, Hr, Te, Kt, Lv7, Re, bits7, KLv, W⟩ := J7A.unp J3.j7 + have hH : H ptr = some (.other bits.val) := W.cc.fo y hy ptr sz rt mp ept asid va hc bits hbits + have hfit : ptr.val + 2 ^ bits.val ≤ 2 ^ 64 := by + have := J3.sys.ent.heap.fits ptr _ hH; simpa [HKind.bits] using this + have hal : ptr.val % 2 ^ bits.val = 0 := by + have := J3.sys.ent.heap.align ptr _ hH; simpa [HKind.bits] using this + have hal8 : ptr.val % 8 = 0 := by + have h8 : 8 ∣ 2 ^ bits.val := ⟨2 ^ (bits.val - 3), by + rw [show (8 : Nat) = 2 ^ 3 from rfl, ← Nat.pow_add]; congr 1; omega⟩ + exact Nat.dvd_iff_mod_eq_zero.mp (Nat.dvd_trans h8 (Nat.dvd_iff_mod_eq_zero.mpr hal)) + have hfr : (wa ptr off).val = ptr.val + off.val := by + rw [wa_valP]; exact Nat.mod_eq_of_lt (by omega) + have hffit : (wa ptr off).val + 1024 ≤ 2 ^ 64 := by omega + have hfal : Aligned (wa ptr off) := by show (wa ptr off).val % 8 = 0; rw [hfr]; omega + have hks : ∀ k < 128, ¬ ((L.ksb s).val ≤ (wa (wa ptr off) ⟨BitVec.ofNat 64 (8 * k)⟩).val ∧ + (wa (wa ptr off) ⟨BitVec.ofNat 64 (8 * k)⟩).val < (L.ksb s).val + ksSizeA) := by + intro k hk + have hw : (wa (wa ptr off) ⟨BitVec.ofNat 64 (8 * k)⟩).val = (wa ptr off).val + 8 * k := by + rw [wa_valP] + have hk8 : (⟨BitVec.ofNat 64 (8 * k)⟩ : U64).val = 8 * k := by + change (BitVec.ofNat 64 (8 * k)).toNat = _; rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)] + rw [hk8]; exact Nat.mod_eq_of_lt (by omega) + have hwal : Aligned (wa (wa ptr off) ⟨BitVec.ofNat 64 (8 * k)⟩) := by + show (wa (wa ptr off) ⟨BitVec.ofNat 64 (8 * k)⟩).val % 8 = 0; rw [hw, hfr]; omega + have hU := W.fu y hy ptr sz rt mp ept asid va hc bits hbits _ hwal (by omega) (by omega) + rcases W.col.kb.ksU _ hU with h | h <;> (unfold ksSizeA; omega) + have K := read_vgic_frame_localA (st := st) (Z := Z) (σ := σ) hffit hfal hAg hks + rw [← hlrs, ← hbv, hr, WP.spec_ok, hn] at K + have hsp : specPtr (wa ptr off) = specPtr ptr + off.bv := by + have hb : (wa ptr off).bv = ptr.bv + off.bv := by + apply BitVec.eq_of_toNat_eq + change (wa ptr off).val = (ptr.bv + off.bv).toNat + rw [hfr, BitVec.toNat_add]; exact (Nat.mod_eq_of_lt (by omega : ptr.val + off.val < 2 ^ 64)).symm + unfold specPtr; rw [hb, BitVec.add_assoc, BitVec.add_comm off.bv pptrShiftA, ← BitVec.add_assoc] + exact ⟨hsp, K⟩ + +/-! ## `ReadState` / `WriteState` -/ + +section record +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} + {κ : U64 → U64} {n : Nat} {nA : U64} {m : McState} + +set_option maxHeartbeats 2000000 in +theorem vcpuXfer.decErrA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (hP : VcpuPlatAgreeA ar) {vc length buf : U64} {ex : Array obj.Slot 3#usize} {tf : Bool} + {args : List U64} (hlen : length.val = args.length) + (hargs : ∀ (i : U64) w, args[i.val]? = some w → msg.syscall_arg st s (L.curW s) buf i = ok w) + {extras : List (Cap × CslotPtr)} (hEx : ExtraOKA Z.th (L.mem s) ex extras) + (hext : ∀ x ∈ ex.val, x ≠ 0#u64 → x ∈ OS) + (hNoS2 : ∀ p sz r d mp ept asid va, capA st s (exAtA ex 0) = .Frame p sz r d mp ept asid va → ept = false) + (hAg : tf = false → ∀ p sz r mp ept asid va off mask, + capA st s (exAtA ex 0) = .Frame p sz r false mp ept asid va → + args[0]? = some off → args[1]? = some mask → mask ≠ 0#u64 → FrameInputAgreeA st s σ (wa p off)) + (h : vcpu.decode_state_transfer st ar s a vc length buf ex tf = ok (.Err x, s', a')) : + VcErrA σ x (decodeVcpuStateTransfer (Z := Unit) (specPtr vc) (args.map UScalar.bv) extras tf) := by + unfold vcpu.decode_state_transfer at h + by_cases hlen2 : length < 2#u64 + · rw [if_pos hlen2] at h + refine truncA (errInjA h) ?_ + have hlt : (args.map UScalar.bv).length < 2 := by rw [List.length_map, ← hlen]; exact hlen2 + unfold decodeVcpuStateTransfer; exact ndE_whenE_true (Or.inl hlt) _ _ _ + rw [if_neg hlen2] at h + have hge2 : 2 ≤ args.length := by have : ¬ length.val < 2 := hlen2; omega + rw [exAtA_idx0, bind_ok, show obj.Slot.is_null (exAtA ex 0) = ok (decide (exAtA ex 0 = 0#u64)) by + unfold obj.Slot.is_null; rfl, bind_ok] at h + cases extras with + | nil => + have h0 : exAtA ex 0 = 0#u64 := hEx.2.2 0 (by simp) (by decide) + rw [if_pos (by simp [h0])] at h + refine truncA (errInjA h) ?_ + unfold decodeVcpuStateTransfer; exact ndE_whenE_true (Or.inr rfl) _ _ _ + | cons ep rest => + obtain ⟨hne, -, -, hcr, -⟩ := srcOK_factsA J.j.1.place (hEx.2.1 0 (by simp)) + simp only [List.getElem_cons_zero] at hcr + have hmem : exAtA ex 0 ∈ ex.val := by + have hl : ex.val.length = 3 := by simpa using ex.property + unfold exAtA; rw [List.getD_eq_getElem _ _ (by omega)]; exact List.getElem_mem _ + have hos := hext _ hmem hne + rw [if_neg (by simp [hne])] at h + obtain ⟨off, mask, tl, rfl⟩ : ∃ off mask tl, args = off :: mask :: tl := by + match args, hge2 with + | off :: mask :: tl, _ => exact ⟨off, mask, tl, rfl⟩ + rw [show sched.cur_thread st s = ok (L.curW s) from eq_of_spec_fd (sched.cur_thread.spec s), bind_ok, + hargs 0#u64 off (by simp), bind_ok, hargs 1#u64 mask (by simp), bind_ok, + sm_all_ok, bind_ok, lift_ok, bind_ok, lift_ok, bind_ok] at h + have hnt : ¬ (((off :: mask :: tl).map UScalar.bv).length < 2 ∨ ep :: rest = []) := by simp + by_cases hbad : (mask &&& ~~~63#u64 != 0#u64) = true + · rw [if_pos hbad] at h + refine invArgA 1#u64 1 rfl (errInjA h) ?_ + have : mask.bv &&& ~~~smAll ≠ 0 := fun e => by + have h0 : mask &&& ~~~63#u64 = 0#u64 := u64_eq_of_bv_eq (by unfold smAll at e; exact e) + rw [h0] at hbad; exact absurd hbad (by decide) + unfold decodeVcpuStateTransfer + rw [ndE_whenE_false hnt] + exact ndE_whenE_true (by simpa [the] using this) _ _ _ + rw [if_neg hbad] at h + have hokm : ¬ (mask.bv &&& ~~~smAll ≠ 0) := + fun hne => hbad (bne_iff_ne.2 (fun e => hne (congrArg UScalar.bv e))) + rw [show obj.Slot.cap st (exAtA ex 0) s = ok (capAtA (L.mem s) (exAtA ex 0)) by + rw [Fl4ProofsA64.P1.W7.R.ST.capA_eq, ← capAtA_eq], bind_ok] at h + have noCap : x = .Sys (.InvalidCapability 1#u64) → + (frameRightsDev ep.1 = none ∨ stateWindow ep.1 off.bv = none) → + VcErrA σ x (decodeVcpuStateTransfer (Z := Unit) (specPtr vc) ((off :: mask :: tl).map UScalar.bv) + (ep :: rest) tf) := by + intro hx hnone + refine invCapA hx ?_ + unfold decodeVcpuStateTransfer + rw [ndE_whenE_false hnt, ndE_whenE_false (by simpa [the] using hokm)] + simp only [List.map_cons, List.getElem?_cons_zero, the] + rcases hnone with h | h <;> simp [h] <;> rfl + obtain ⟨hwf, habs⟩ := hcr + cases hc : capAtA (L.mem s) (exAtA ex 0) with + | Frame ptr sz rt dev mp ept asid va => + have hc' : capA st s (exAtA ex 0) = .Frame ptr sz rt dev mp ept asid va := hc + obtain rfl : ept = false := hNoS2 _ _ _ _ _ _ _ _ hc' + rw [hc] at h habs; dsimp only at h + have hep : ep.1 = .archObjectCap (.frameCap (specPtr ptr) (vmRightsOfA rt) (frameSizeOfA sz) dev + (mapDataA mp asid va)) := by rw [← habs]; rfl + have hfrd : frameRightsDev ep.1 = some (vmRightsOfA rt, dev) := by rw [hep]; rfl + set sw : Option ObjRef := + if off.bv &&& 63 ≠ 0 ∨ off.bv ≥ (1 : Word) <<< pageBitsForSize (frameSizeOfA sz) ∨ + (1 : Word) <<< pageBitsForSize (frameSizeOfA sz) - off.bv < smSize + then none else some (specPtr ptr + off.bv) with hswd + have hsw : stateWindow ep.1 off.bv = some sw := by + rw [hep]; unfold stateWindow; dsimp only [sw]; split_ifs <;> rfl + have capErr : x = .Sys (.InvalidCapability 1#u64) → + (dev ∨ vmRightsOfA rt = vmKernelOnly ∨ (tf = true ∧ vmRightsOfA rt ≠ vmReadWrite)) → + VcErrA σ x (decodeVcpuStateTransfer (Z := Unit) (specPtr vc) ((off :: mask :: tl).map UScalar.bv) + (ep :: rest) tf) := by + intro hx hb + refine invCapA hx ?_ + unfold decodeVcpuStateTransfer + rw [ndE_whenE_false hnt, ndE_whenE_false (by simpa [the] using hokm)] + simp only [List.map_cons, List.getElem?_cons_zero, List.getElem?_cons_succ, the, hfrd, hsw] + unfold NonDetE.whenE; rw [if_pos hb]; exact ndE_throw_bind _ _ _ + cases dev with + | true => rw [if_pos rfl] at h; exact capErr (errInjA h) (Or.inl rfl) + | false => + rw [if_neg Bool.false_ne_true] at h + have hwf' : 1 ≤ rt.val ∧ rt.val ≤ 3 := by rw [hc] at hwf; exact ⟨hwf.1, hwf.2.1⟩ + have hr : rt.val = 1 ∨ rt.val = 2 ∨ rt.val = 3 := by omega + have hko : vmRightsOfA rt = vmKernelOnly ↔ rt = capability.VM_KERNEL_ONLY := by + constructor + · intro e + rcases hr with h1 | h2 | h3 + · exact UScalar.eq_of_val_eq (by simp [h1, capability.VM_KERNEL_ONLY]) + · have := (Set.ext_iff.1 e .allowRead).1 (by simp [vmRightsOfA, vmReadOnly, h2]) + simp [vmKernelOnly] at this + · have := (Set.ext_iff.1 e .allowRead).1 (by simp [vmRightsOfA, vmReadWrite, h3]) + simp [vmKernelOnly] at this + · intro e; subst e; simp [vmRightsOfA, capability.VM_KERNEL_ONLY] + have hrw : vmRightsOfA rt = vmReadWrite ↔ rt = capability.VM_READ_WRITE := by + constructor + · intro e + rcases hr with h1 | h2 | h3 + · have := (Set.ext_iff.1 e .allowWrite).2 (by simp [vmReadWrite]) + simp [vmRightsOfA, vmKernelOnly, h1] at this + · have := (Set.ext_iff.1 e .allowWrite).2 (by simp [vmReadWrite]) + simp [vmRightsOfA, vmReadOnly, h2] at this + · exact UScalar.eq_of_val_eq (by simp [h3, capability.VM_READ_WRITE]) + · intro e; subst e; simp [vmRightsOfA, capability.VM_READ_WRITE] + by_cases hk : rt = capability.VM_KERNEL_ONLY + · rw [if_pos hk] at h; exact capErr (errInjA h) (Or.inr (Or.inl (hko.2 hk))) + rw [if_neg hk] at h + obtain ⟨bits, hbits, h12, h64, hand, hpb, hnat⟩ := frameSzA sz + -- the part after the rights checks + have body : (tf = true → vmRightsOfA rt = vmReadWrite) → (do + let i3 ← capability.FrameSize.bits sz + let i4 ← lift (i3 &&& 63#u64) + let size1 ← 1#u64 <<< i4 + let i5 ← lift (off &&& 63#u64) + if i5 != 0#u64 then do + let r ← error.invalid_arg Unit 0#u64 + ok (r, s, a) + else if off >= size1 then do + let r ← error.invalid_arg Unit 0#u64 + ok (r, s, a) + else do + let i6 ← size1 - off + if i6 < fl4_abi.consts.vcpu_state_mask.SIZE then do + let r ← error.invalid_arg Unit 0#u64 + ok (r, s, a) + else do + let frame ← lift (wa ptr off) + let lrs ← vcpu.num_lrs ar a + let i7 ← ar.vcpu_gic_version a + let a1 := Array.repeat 16#usize 0#u64 + if tf then do + let (s2, a2) ← vcpu.stall_vcpu st ar s a vc + let s3 ← sched.set_thread_state st s2 (L.curW s) sched.TS_RESTART + if tf then do + let s4 ← vcpu.read_state_to_frame st ar s3 a2 vc frame mask + ok (core.result.Result.Ok (), s4, a2) + else do + let s4 ← vcpu.write_state_from_frame st ar s3 a2 vc frame mask (0#u64, a1) + ok (core.result.Result.Ok (), s4, a2) + else do + let i8 ← fl4_abi.consts.vcpu_state_mask.VGIC + let i9 ← fl4_abi.consts.vcpu_state_mask.VGIC_LR + let i10 ← lift (i8 ||| i9) + let i11 ← lift (mask &&& i10) + if i11 != 0#u64 then do + let o ← vcpu.read_vgic_frame st s frame lrs (i7 = 3#u64) + match o with + | none => do + let r ← error.invalid_arg Unit 1#u64 + ok (r, s, a) + | some v => do + let (s2, a2) ← vcpu.stall_vcpu st ar s a vc + let s3 ← sched.set_thread_state st s2 (L.curW s) sched.TS_RESTART + if tf then do + let s4 ← vcpu.read_state_to_frame st ar s3 a2 vc frame mask + ok (core.result.Result.Ok (), s4, a2) + else do + let s4 ← vcpu.write_state_from_frame st ar s3 a2 vc frame mask v + ok (core.result.Result.Ok (), s4, a2) + else do + let (s2, a2) ← vcpu.stall_vcpu st ar s a vc + let s3 ← sched.set_thread_state st s2 (L.curW s) sched.TS_RESTART + if tf then do + let s4 ← vcpu.read_state_to_frame st ar s3 a2 vc frame mask + ok (core.result.Result.Ok (), s4, a2) + else do + let s4 ← vcpu.write_state_from_frame st ar s3 a2 vc frame mask (0#u64, a1) + ok (core.result.Result.Ok (), s4, a2)) = ok (.Err x, s', a') → + VcErrA σ x (decodeVcpuStateTransfer (Z := Unit) (specPtr vc) ((off :: mask :: tl).map UScalar.bv) + (ep :: rest) tf) := by + intro hrok h + have hncap : ¬ (false = true ∨ vmRightsOfA rt = vmKernelOnly ∨ (tf = true ∧ vmRightsOfA rt ≠ vmReadWrite)) := by + rintro (e | e | e) + · exact Bool.false_ne_true e + · exact hk (hko.1 e) + · exact e.2 (hrok e.1) + rw [hbits, bind_ok] at h + have spec : VcErrA σ x ((match (generalizing := false) sw with + | none => NonDetE.throwError (.invalidArgument 0) + | some frame => + if ¬ tf = true ∧ mask.bv &&& (smVgic ||| smVgicLr) ≠ 0 then do + let vg ← NonDetE.liftE <| readVgicFrame (Z := Unit) frame + match vg with + | some vg => NonDetE.returnOk (.stateTransfer (specPtr vc) frame mask.bv tf vg) + | none => NonDetE.throwError (.invalidArgument 1) + else NonDetE.returnOk (.stateTransfer (specPtr vc) frame mask.bv tf (0, [])) : + SEMonad Unit Fl4VcpuInvocation)) → + VcErrA σ x (decodeVcpuStateTransfer (Z := Unit) (specPtr vc) ((off :: mask :: tl).map UScalar.bv) + (ep :: rest) tf) := by + rintro ⟨e', hr, hv⟩ + refine ⟨e', ?_, hv⟩ + unfold decodeVcpuStateTransfer + rw [ndE_whenE_false hnt, ndE_whenE_false (by simpa [the] using hokm)] + simp only [List.map_cons, List.getElem?_cons_zero, List.getElem?_cons_succ, the, hfrd, hsw] + unfold NonDetE.whenE; rw [if_neg hncap, ndE_pure_bindA] + exact hr + apply spec + rcases windowErrA h64 hnat h hand with ⟨hx, hb⟩ | ⟨hok, h8, hfit, h⟩ + · have : sw = none := by rw [hswd, hpb]; erw [if_pos hb] + rw [this]; exact invArgA 0#u64 0 rfl hx rfl + have : sw = some (specPtr ptr + off.bv) := by rw [hswd, hpb]; erw [if_neg hok] + rw [this]; dsimp only + rw [lift_ok, bind_ok, vcpu_num_lrs_eqA hP a, bind_ok] at h + obtain ⟨g, hg, hgv⟩ := hP.gic a + rw [hg, bind_ok] at h + cases tf with + | true => + exfalso + rw [if_pos rfl] at h + repeat (first | (with_reducible exact (noOkA h).elim) | (rw [if_pos rfl] at h) | (rw [if_neg Bool.false_ne_true] at h) | (dsimp only [Bool.false_eq_true, eq_self, ↓reduceIte] at h) | (rw [eq_of_spec_fd Fl4ProofsA64.P0.Consts.layout.tcb.RUN_VCPU.spec, bind_ok] at h) | (refine (bind_eq_ok h).elim fun p hp => ?_; obtain ⟨-, h⟩ := hp; (try obtain ⟨_, _⟩ := p))) + try simp only [↓reduceIte] at h + repeat (first | (with_reducible exact (noOkA h).elim) | (rw [if_pos rfl] at h) | (rw [if_neg Bool.false_ne_true] at h) | (dsimp only [Bool.false_eq_true, eq_self, ↓reduceIte] at h) | (rw [eq_of_spec_fd Fl4ProofsA64.P0.Consts.layout.tcb.RUN_VCPU.spec, bind_ok] at h) | (refine (bind_eq_ok h).elim fun p hp => ?_; obtain ⟨-, h⟩ := hp; (try obtain ⟨_, _⟩ := p))) + all_goals (first | (dsimp only [Bool.false_eq_true, eq_self, ↓reduceIte] at h; exact noOkA h) | exact noOkA h) + | false => + rw [if_neg Bool.false_ne_true, sm_vgic_ok, bind_ok, sm_vgic_lr_ok, bind_ok, lift_ok, bind_ok, lift_ok, + bind_ok] at h + by_cases hvg : (mask &&& (8#u64 ||| 32#u64) != 0#u64) = true + swap + · exfalso + rw [if_neg hvg] at h + repeat (first | (with_reducible exact (noOkA h).elim) | (rw [if_pos rfl] at h) | (rw [if_neg Bool.false_ne_true] at h) | (dsimp only [Bool.false_eq_true, eq_self, ↓reduceIte] at h) | (rw [eq_of_spec_fd Fl4ProofsA64.P0.Consts.layout.tcb.RUN_VCPU.spec, bind_ok] at h) | (refine (bind_eq_ok h).elim fun p hp => ?_; obtain ⟨-, h⟩ := hp; (try obtain ⟨_, _⟩ := p))) + try simp only [Bool.false_eq_true, ↓reduceIte] at h + repeat (first | (with_reducible exact (noOkA h).elim) | (rw [if_pos rfl] at h) | (rw [if_neg Bool.false_ne_true] at h) | (dsimp only [Bool.false_eq_true, eq_self, ↓reduceIte] at h) | (rw [eq_of_spec_fd Fl4ProofsA64.P0.Consts.layout.tcb.RUN_VCPU.spec, bind_ok] at h) | (refine (bind_eq_ok h).elim fun p hp => ?_; obtain ⟨-, h⟩ := hp; (try obtain ⟨_, _⟩ := p))) + all_goals (first | (dsimp only [Bool.false_eq_true, eq_self, ↓reduceIte] at h; exact noOkA h) | exact noOkA h) + rw [if_pos hvg] at h + have hvgb : mask.bv &&& (smVgic ||| smVgicLr) ≠ 0 := fun e => by + have : mask &&& (8#u64 ||| 32#u64) = 0#u64 := u64_eq_of_bv_eq e + simp [this] at hvg + have hmnz : mask ≠ 0#u64 := fun e => by subst e; exact hvgb (by decide) + obtain ⟨res, hres, h⟩ := bind_eq_ok h + cases res with + | some v => + exfalso + dsimp only at h + repeat (first | (with_reducible exact (noOkA h).elim) | (rw [if_pos rfl] at h) | (rw [if_neg Bool.false_ne_true] at h) | (dsimp only [Bool.false_eq_true, eq_self, ↓reduceIte] at h) | (rw [eq_of_spec_fd Fl4ProofsA64.P0.Consts.layout.tcb.RUN_VCPU.spec, bind_ok] at h) | (refine (bind_eq_ok h).elim fun p hp => ?_; obtain ⟨-, h⟩ := hp; (try obtain ⟨_, _⟩ := p))) + try simp only [Bool.false_eq_true, ↓reduceIte] at h + repeat (first | (with_reducible exact (noOkA h).elim) | (rw [if_pos rfl] at h) | (rw [if_neg Bool.false_ne_true] at h) | (dsimp only [Bool.false_eq_true, eq_self, ↓reduceIte] at h) | (rw [eq_of_spec_fd Fl4ProofsA64.P0.Consts.layout.tcb.RUN_VCPU.spec, bind_ok] at h) | (refine (bind_eq_ok h).elim fun p hp => ?_; obtain ⟨-, h⟩ := hp; (try obtain ⟨_, _⟩ := p))) + all_goals (first | (dsimp only [Bool.false_eq_true, eq_self, ↓reduceIte] at h; exact noOkA h) | exact noOkA h) + | none => + obtain ⟨hsp, hread⟩ := vgicNoneA J hos hc' hbits h12 h8 hfit + (hAg rfl _ _ _ _ _ _ _ off mask hc' rfl rfl hmnz) rfl hgv hres rfl + rw [hsp] at hread + refine invArgA 1#u64 1 rfl (errInjA h) ?_ + rw [if_pos ⟨by decide, hvgb⟩, ndE_liftE_bind _ _ _ _ none hread]; rfl + cases tf with + | false => + rw [if_neg Bool.false_ne_true] at h + exact body (fun e => (Bool.false_ne_true e).elim) h + | true => + rw [if_pos rfl] at h + by_cases hnrw : (rt != capability.VM_READ_WRITE) = true + · rw [if_pos hnrw] at h + exact capErr (errInjA h) (Or.inr (Or.inr ⟨rfl, fun e => (bne_iff_ne.1 hnrw) (hrw.1 e)⟩)) + · rw [if_neg hnrw] at h + exact body (fun _ => hrw.2 (by by_contra e; exact hnrw (bne_iff_ne.2 e))) h + | PageTable p mp asid va => + rw [hc] at h habs; dsimp only at h + cases mp + · exact noCap (errInjA h) (Or.inl (by rw [← habs]; rfl)) + · cases hep : ep.1 with + | archObjectCap ac => + cases ac with + | pageTableCap _ _ _ => exact noCap (errInjA h) (Or.inl (by rw [hep]; rfl)) + | _ => rw [hep] at habs; cases habs + | _ => rw [hep] at habs; cases habs + | _ => + rw [hc] at h habs hwf; dsimp only at h + first + | exact False.elim hwf + | exact noCap (errInjA h) (Or.inl (by rw [← habs]; rfl)) + +/-! ## `Run` -/ + +set_option maxHeartbeats 2000000 in +theorem vcpuRun.decErrA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (hP : VcpuPlatAgreeA ar) {Ub : Set U64} {vc length buf : U64} {call : Bool} + (C : VcpuDecCtxA (L := L) (ar := ar) AR Z H OS U Ac Pp Tb Ub Lv ia ie s a σ m vc) + {args : List U64} (hlen : length.val = args.length) + (hargs : ∀ (i : U64) w, args[i.val]? = some w → msg.syscall_arg st s (L.curW s) buf i = ok w) + (hAg : ∀ p sz r mp ept asid va off mask, + capA st s (sbWordA (L.curW s)) = .Frame p sz r false mp ept asid va → + args[0]? = some off → args[1]? = some mask → mask ≠ 0#u64 → FrameInputAgreeA st s σ (wa p off)) + (h : vcpu.decode_run st ar s a vc length call buf = ok (.Err x, s', a')) : + VcErrA σ x (decodeVcpuRun (Z := Unit) (specPtr vc) (args.map UScalar.bv) call) := by + unfold vcpu.decode_run at h + cases call with + | false => + rw [if_neg Bool.false_ne_true] at h + refine illA (errInjA h) ?_ + unfold decodeVcpuRun NonDetE.unlessE; rw [if_neg Bool.false_ne_true]; exact ndE_throw_bind _ _ _ + | true => + rw [if_pos rfl] at h + by_cases hlt3 : length < 3#u64 + · rw [if_pos hlt3] at h + refine truncA (errInjA h) ?_ + have hl : (args.map UScalar.bv).length < 3 := by rw [List.length_map, ← hlen]; exact hlt3 + unfold decodeVcpuRun NonDetE.unlessE; rw [if_pos rfl, ndE_pure_bindA] + exact ndE_whenE_true hl _ _ _ + rw [if_neg hlt3] at h + have hge3 : 3 ≤ args.length := by change ¬ length.val < 3 at hlt3; omega + obtain ⟨a0, a1, a2, tl, rfl⟩ : ∃ a0 a1 a2 tl, args = a0 :: a1 :: a2 :: tl := by + match args, hge3 with + | a0 :: a1 :: a2 :: tl, _ => exact ⟨a0, a1, a2, tl, rfl⟩ + have ha0 := hargs 0#u64 a0 (by simp) + have ha1 := hargs 1#u64 a1 (by simp) + have ha2 := hargs 2#u64 a2 (by simp) + rw [show sched.cur_thread st s = ok (L.curW s) from eq_of_spec_fd (sched.cur_thread.spec s), bind_ok, + ha0, bind_ok, ha1, bind_ok, ha2, bind_ok, sm_all_ok, bind_ok, lift_ok, bind_ok, lift_ok, bind_ok] at h + set ct := L.curW s with hctd + have hcurT : ct ∈ Z.T := C.E.j.1.curT (L.cpu s) C.E.j.1.sch.cpuCs + have hct : σ.curThread = specPtr ct := C.E.j.1.sch.curThread + have hsmAll : smAll = (63#u64).bv := rfl + have pre : (decodeVcpuRun (Z := Unit) (specPtr vc) ((a0 :: a1 :: a2 :: tl).map UScalar.bv) true).run σ = + ((do NonDetE.whenE (a1.bv &&& ~~~smAll ≠ 0) <| NonDetE.throwError (.invalidArgument 1) + NonDetE.whenE (a2.bv &&& ~~~smAll ≠ 0) <| NonDetE.throwError (.invalidArgument 2) + let buf ← NonDetE.liftE <| getCap (specPtr ct, tcbCnodeIndex 2) + match frameRightsDev buf, stateWindow buf a0.bv with + | some (R, dev), some w => do + NonDetE.whenE (dev ∨ R ≠ vmReadWrite) <| NonDetE.throwError .illegalOperation + match w with + | none => NonDetE.throwError (.invalidArgument 0) + | some frame => do + let v ← NonDetE.liftE <| getVcpu (specPtr vc) + let ctVcpu ← NonDetE.liftE <| archThreadGet ArchTcb.tcbVcpu (specPtr ct) + NonDetE.whenE (v.vcpuTcb = none ∨ v.vcpuTcb = some (specPtr ct) ∨ ctVcpu ≠ none) <| + NonDetE.throwError .illegalOperation + NonDetE.whenE (v.vcpuExt.runCaller ≠ none) <| NonDetE.throwError .deleteFirst + if a1.bv &&& (smVgic ||| smVgicLr) ≠ 0 then do + let vg ← NonDetE.liftE <| readVgicFrame frame + match vg with + | some vg => NonDetE.returnOk (.run (specPtr vc) (specPtr ct) frame a0.bv a1.bv a2.bv vg) + | none => NonDetE.throwError (.invalidArgument 1) + else NonDetE.returnOk (.run (specPtr vc) (specPtr ct) frame a0.bv a1.bv a2.bv (0, [])) + | _, _ => NonDetE.throwError .illegalOperation : SEMonad Unit Fl4VcpuInvocation)).run σ := by + unfold decodeVcpuRun + rw [NonDetE.unlessE, if_pos rfl, ndE_pure_bindA, + NonDetE.whenE, if_neg (by rw [List.length_map, ← hlen]; exact hlt3), ndE_pure_bindA] + unfold NonDetE.liftE + change ((NonDet.gets (fun s : State Unit => s.curThread) >>= fun t => NonDet.pure (Except.ok t)) >>= _) σ = _ + rw [nd_bind_assocSc, nd_bind_oneSc (m := NonDet.gets _) rfl, nd_bind_oneSc (m := NonDet.pure _) rfl] + dsimp only [ExceptT.bindCont] + rw [hct] + simp only [List.map_cons, List.getElem?_cons_zero, List.getElem?_cons_succ, the]; rfl + by_cases hm1 : (a1 &&& ~~~63#u64 != 0#u64) = true + · rw [if_pos hm1] at h + refine invArgA 1#u64 1 rfl (errInjA h) ?_ + have : a1.bv &&& ~~~smAll ≠ 0 := fun e => by + rw [hsmAll] at e + have h0 : a1 &&& ~~~63#u64 = 0#u64 := u64_eq_of_bv_eq e + rw [h0] at hm1; revert hm1; decide + rw [pre]; exact ndE_whenE_true this _ _ _ + rw [if_neg hm1, lift_ok, bind_ok, lift_ok, bind_ok] at h + have hm10 : ¬ (a1.bv &&& ~~~smAll ≠ 0) := fun e => hm1 (bne_iff_ne.2 fun e' => e (by + rw [hsmAll]; exact congrArg UScalar.bv e')) + by_cases hm2 : (a2 &&& ~~~63#u64 != 0#u64) = true + · rw [if_pos hm2] at h + refine invArgA 2#u64 2 rfl (errInjA h) ?_ + have : a2.bv &&& ~~~smAll ≠ 0 := fun e => by + rw [hsmAll] at e + have h0 : a2 &&& ~~~63#u64 = 0#u64 := u64_eq_of_bv_eq e + rw [h0] at hm2; revert hm2; decide + rw [pre, NonDetE.whenE, if_neg hm10, ndE_pure_bindA] + exact ndE_whenE_true this _ _ _ + rw [if_neg hm2] at h + have hm20 : ¬ (a2.bv &&& ~~~smAll ≠ 0) := fun e => hm2 (bne_iff_ne.2 fun e' => e (by + rw [hsmAll]; exact congrArg UScalar.bv e')) + rw [show obj.Tcb.slot ct layout.tcb.SLOT_BUFFER = ok (sbWordA ct) by + simp [obj.Tcb.slot, lift, sbWordA, layout.tcb.SLOT_BUFFER, layout.cte.SIZE], bind_ok, + show obj.Slot.cap st (sbWordA ct) s = ok (capAtA (L.mem s) (sbWordA ct)) by + rw [Fl4ProofsA64.P1.W7.R.ST.capA_eq, ← capAtA_eq], bind_ok] at h + obtain ⟨xct, hkct, hslots⟩ := C.E.j.1.st.tcb ct ((C.E.j.1.thT ct).2 hcurT) + have hsr := hslots 2 (by unfold tcbSlotsA; decide) + have hsbeq : slotAddrA (specPtr ct) (BitVec.ofNat 64 2) = sbWordA ct := bufSlot_eqA ct + have hmap : tcbCnodeMap xct (tcbCnodeIndex 2) = some xct.tcbIpcframe := by + unfold tcbCnodeMap tcbCnodeIndex; rfl + rw [hsbeq] at hsr + have hrel : CapRelA (capAtA (L.mem s) (sbWordA ct)) xct.tcbIpcframe := by simpa [SlotRelA, hmap] using hsr + have hsbOS : sbWordA ct ∈ OS := + C.hsub (sbWordA ct) ⟨ct, Or.inr ⟨(C.E.j.1.thT ct).2 hcurT, 2, by unfold tcbSlotsA; decide, hsbeq.symm⟩⟩ + have preB : (decodeVcpuRun (Z := Unit) (specPtr vc) ((a0 :: a1 :: a2 :: tl).map UScalar.bv) true).run σ = + ((match frameRightsDev xct.tcbIpcframe, stateWindow xct.tcbIpcframe a0.bv with + | some (R, dev), some w => do + NonDetE.whenE (dev ∨ R ≠ vmReadWrite) <| NonDetE.throwError .illegalOperation + match w with + | none => NonDetE.throwError (.invalidArgument 0) + | some frame => do + let v ← NonDetE.liftE <| getVcpu (specPtr vc) + let ctVcpu ← NonDetE.liftE <| archThreadGet ArchTcb.tcbVcpu (specPtr ct) + NonDetE.whenE (v.vcpuTcb = none ∨ v.vcpuTcb = some (specPtr ct) ∨ ctVcpu ≠ none) <| + NonDetE.throwError .illegalOperation + NonDetE.whenE (v.vcpuExt.runCaller ≠ none) <| NonDetE.throwError .deleteFirst + if a1.bv &&& (smVgic ||| smVgicLr) ≠ 0 then do + let vg ← NonDetE.liftE <| readVgicFrame frame + match vg with + | some vg => NonDetE.returnOk (.run (specPtr vc) (specPtr ct) frame a0.bv a1.bv a2.bv vg) + | none => NonDetE.throwError (.invalidArgument 1) + else NonDetE.returnOk (.run (specPtr vc) (specPtr ct) frame a0.bv a1.bv a2.bv (0, [])) + | _, _ => NonDetE.throwError .illegalOperation : SEMonad Unit Fl4VcpuInvocation)).run σ := by + rw [pre, NonDetE.whenE, if_neg hm10, ndE_pure_bindA, NonDetE.whenE, if_neg hm20, ndE_pure_bindA] + unfold NonDetE.liftE + change ((getCap (specPtr ct, tcbCnodeIndex 2) >>= fun c => NonDet.pure (Except.ok c)) >>= _) σ = _ + rw [nd_bind_assocSc, nd_bind_oneSc (getCap_tcbIpcframe_runA hkct), nd_bind_oneSc (m := NonDet.pure _) rfl]; rfl + have illBuf : x = .Sys .IllegalOperation → (∀ p r sz d mp, xct.tcbIpcframe ≠ .archObjectCap (.frameCap p r sz d mp)) → + VcErrA σ x (decodeVcpuRun (Z := Unit) (specPtr vc) ((a0 :: a1 :: a2 :: tl).map UScalar.bv) true) := by + intro hx hnf + refine illA hx ?_ + rw [preB] + cases hf : xct.tcbIpcframe with + | archObjectCap ac => + cases ac with + | frameCap p r sz d mp => exact absurd hf (hnf p r sz d mp) + | _ => rfl + | _ => rfl + obtain ⟨hwf, habs⟩ := hrel + cases hc : capAtA (L.mem s) (sbWordA ct) with + | Frame ptr sz rt dev mp ept asid va => + have hc' : capA st s (sbWordA ct) = .Frame ptr sz rt dev mp ept asid va := hc + obtain rfl : ept = false := by + have hbn := J.bufNative ct hcurT + rw [bufSlot_eqA ct, hc] at hbn; exact hbn + rw [hc] at h habs; dsimp only at h + have hbuf : xct.tcbIpcframe = .archObjectCap (.frameCap (specPtr ptr) (vmRightsOfA rt) (frameSizeOfA sz) dev + (mapDataA mp asid va)) := by rw [← habs]; rfl + cases dev with + | true => + rw [if_pos rfl] at h + refine illA (errInjA h) ?_ + rw [preB, hbuf]; unfold frameRightsDev stateWindow; dsimp only + split_ifs <;> exact ndE_whenE_true (Or.inl rfl) _ _ _ + | false => + rw [if_neg Bool.false_ne_true] at h + by_cases hrw : (rt != capability.VM_READ_WRITE) = true + · rw [if_pos hrw] at h + refine illA (errInjA h) ?_ + have hne : vmRightsOfA rt ≠ vmReadWrite := by + have hne : rt ≠ capability.VM_READ_WRITE := bne_iff_ne.1 hrw + rw [hc] at hwf; unfold CapWfA at hwf + unfold capability.VM_READ_WRITE at hne + intro e + have h0 : rt.val = 3 := by + rcases hwf with ⟨h1, h2, -⟩ + have : rt.val = 1 ∨ rt.val = 2 ∨ rt.val = 3 := by omega + rcases this with h | h | h + · have haw := (Set.ext_iff.1 e .allowWrite).2 (by simp [vmReadWrite]); simp [vmRightsOfA, vmKernelOnly, h] at haw + · have haw := (Set.ext_iff.1 e .allowWrite).2 (by simp [vmReadWrite]); simp [vmRightsOfA, vmReadOnly, h] at haw + · exact h + exact hne (UScalar.eq_of_val_eq h0) + rw [preB, hbuf]; unfold frameRightsDev stateWindow; dsimp only + split_ifs <;> exact ndE_whenE_true (Or.inr hne) _ _ _ + rw [if_neg hrw] at h + have hr : vmRightsOfA rt = vmReadWrite := by + have : rt = capability.VM_READ_WRITE := by by_contra e; exact hrw (bne_iff_ne.2 e) + rw [this]; unfold vmRightsOfA capability.VM_READ_WRITE vmReadWrite; rfl + obtain ⟨bits, hbits, h12, h64, hand, hpb, hnat⟩ := frameSzA sz + rw [hbits, bind_ok] at h + rcases windowErrA h64 hnat h hand with ⟨hx, hb⟩ | ⟨hok, h8, hfit, h⟩ + · refine invArgA 0#u64 0 rfl hx ?_ + rw [preB, hbuf]; unfold frameRightsDev stateWindow; dsimp only + rw [hpb]; erw [if_pos hb]; dsimp only + unfold NonDetE.whenE; rw [if_neg (by simp [hr])] + change ((NonDet.pure (Except.ok ())) >>= _) σ = _ + rw [nd_bind_oneSc (m := NonDet.pure (Except.ok ())) rfl]; rfl + rw [show vcpu.TCB = 0#u64 by unfold vcpu.TCB; rfl, lift_ok, bind_ok, + show wa vc 0#u64 = vc by apply UScalar.eq_of_val_eq; rw [wa_valP]; simp [UScalar.hBounds], + L.read_eq, bind_ok] at h + set vt := L.mem s vc with hvtd + obtain ⟨v0, hkv, hvt, hrc⟩ := C.hV.core.obj vc C.hvc + obtain ⟨xct', hkct', htv, -⟩ := C.hV.core.tcb ct hcurT + rw [hkct] at hkct'; cases hkct' + have preV : (decodeVcpuRun (Z := Unit) (specPtr vc) ((a0 :: a1 :: a2 :: tl).map UScalar.bv) true).run σ = + ((do NonDetE.whenE (v0.vcpuTcb = none ∨ v0.vcpuTcb = some (specPtr ct) ∨ xct.tcbArch.tcbVcpu ≠ none) <| + NonDetE.throwError .illegalOperation + NonDetE.whenE (v0.vcpuExt.runCaller ≠ none) <| NonDetE.throwError .deleteFirst + if a1.bv &&& (smVgic ||| smVgicLr) ≠ 0 then do + let vg ← NonDetE.liftE <| readVgicFrame (specPtr ptr + a0.bv) + match vg with + | some vg => NonDetE.returnOk (.run (specPtr vc) (specPtr ct) (specPtr ptr + a0.bv) a0.bv a1.bv a2.bv vg) + | none => NonDetE.throwError (.invalidArgument 1) + else NonDetE.returnOk (.run (specPtr vc) (specPtr ct) (specPtr ptr + a0.bv) a0.bv a1.bv a2.bv (0, [])) : + SEMonad Unit Fl4VcpuInvocation)).run σ := by + rw [preB, hbuf]; unfold frameRightsDev stateWindow; dsimp only + rw [hpb]; erw [if_neg hok]; dsimp only + rw [NonDetE.whenE, if_neg (by simp [hr]), ndE_pure_bindA] + unfold NonDetE.liftE + change ((getVcpu (specPtr vc) >>= fun v => NonDet.pure (Except.ok v)) >>= _) σ = _ + rw [nd_bind_assocSc, nd_bind_oneSc (getVcpu_eqA hkv), nd_bind_oneSc (m := NonDet.pure _) rfl] + change ((archThreadGet ArchTcb.tcbVcpu (specPtr ct) >>= fun tv => NonDet.pure (Except.ok tv)) >>= _) σ = _ + rw [nd_bind_assocSc, nd_bind_oneSc (archThreadGet_eqA _ hkct), nd_bind_oneSc (m := NonDet.pure _) rfl]; rfl + have ill : x = .Sys .IllegalOperation → + (v0.vcpuTcb = none ∨ v0.vcpuTcb = some (specPtr ct) ∨ xct.tcbArch.tcbVcpu ≠ none) → + VcErrA σ x (decodeVcpuRun (Z := Unit) (specPtr vc) ((a0 :: a1 :: a2 :: tl).map UScalar.bv) true) := by + intro hx hb + refine illA hx ?_ + rw [preV]; exact ndE_whenE_true hb _ _ _ + by_cases hvt0 : vt = 0#u64 + · rw [if_pos hvt0] at h + refine ill (errInjA h) (Or.inl ?_) + rw [hvt]; change optRefA vt = _; rw [hvt0]; rfl + rw [if_neg hvt0] at h + by_cases hvtc : vt = ct + · rw [if_pos hvtc] at h + refine ill (errInjA h) (Or.inr (Or.inl ?_)) + rw [hvt]; change optRefA vt = _; rw [hvtc]; unfold optRefA; rw [if_neg (by rw [← hvtc]; exact hvt0)] + rw [if_neg hvtc, show vcpu.bound st s ct = ok (L.mem s (wa ct 3664#u64)) by + unfold vcpu.bound + rw [eq_of_spec_fd Fl4ProofsA64.P0.Consts.layout.tcb.VCPU.spec, bind_ok, lift_ok, bind_ok, L.read_eq], + bind_ok] at h + have hsvt : v0.vcpuTcb = some (specPtr vt) := by + rw [hvt]; change optRefA vt = _; unfold optRefA; rw [if_neg hvt0] + have hne1 : v0.vcpuTcb ≠ none := by rw [hsvt]; simp + have hne2 : v0.vcpuTcb ≠ some (specPtr ct) := by + rw [hsvt]; intro e + exact hvtc (specPtr_inj (Option.some.inj e)) + by_cases hb : (L.mem s (wa ct 3664#u64) != 0#u64) = true + · rw [if_pos hb] at h + refine ill (errInjA h) (Or.inr (Or.inr ?_)) + rw [htv]; unfold optRefA; rw [if_neg (bne_iff_ne.1 hb)]; simp + rw [if_neg hb, show vcpu.RUN_CALLER = 24#u64 by unfold vcpu.RUN_CALLER; rfl, lift_ok, bind_ok, L.read_eq, + bind_ok] at h + have hb0 : L.mem s (wa ct 3664#u64) = 0#u64 := by by_contra e; exact hb (bne_iff_ne.2 e) + have hn3 : ¬ (v0.vcpuTcb = none ∨ v0.vcpuTcb = some (specPtr ct) ∨ xct.tcbArch.tcbVcpu ≠ none) := by + rintro (e | e | e) + · exact hne1 e + · exact hne2 e + · exact e (by rw [htv, hb0]; rfl) + by_cases hrcn : (L.mem s (wa vc 24#u64) != 0#u64) = true + · rw [if_pos hrcn] at h + refine vcErr_ofA (e' := .deleteFirst) (errInjA h) ?_ ?_ + · exact Or.inl trivial + have : v0.vcpuExt.runCaller ≠ none := by + rw [hrc]; unfold optRefA; rw [if_neg (bne_iff_ne.1 hrcn)]; simp + rw [preV, NonDetE.whenE, if_neg hn3, ndE_pure_bindA] + exact ndE_whenE_true this _ _ _ + rw [if_neg hrcn] at h + have hrc0 : v0.vcpuExt.runCaller = none := by + rw [hrc, show L.mem s (wa vc 24#u64) = 0#u64 by by_contra e; exact hrcn (bne_iff_ne.2 e)]; rfl + rw [lift_ok, bind_ok, vcpu_num_lrs_eqA hP a, bind_ok] at h + obtain ⟨g, hg, hgv⟩ := hP.gic a + rw [hg, bind_ok, sm_vgic_ok, bind_ok, sm_vgic_lr_ok, bind_ok, lift_ok, bind_ok, lift_ok, bind_ok] at h + by_cases hvg : (a1 &&& (8#u64 ||| 32#u64) != 0#u64) = true + swap + · exfalso + rw [if_neg hvg] at h + repeat (first | (with_reducible exact (noOkA h).elim) | (rw [if_pos rfl] at h) | (rw [if_neg Bool.false_ne_true] at h) | (dsimp only [Bool.false_eq_true, eq_self, ↓reduceIte] at h) | (rw [eq_of_spec_fd Fl4ProofsA64.P0.Consts.layout.tcb.RUN_VCPU.spec, bind_ok] at h) | (refine (bind_eq_ok h).elim fun p hp => ?_; obtain ⟨-, h⟩ := hp; (try obtain ⟨_, _⟩ := p))) + all_goals (first | (dsimp only [Bool.false_eq_true, eq_self, ↓reduceIte] at h; exact noOkA h) | exact noOkA h) + rw [if_pos hvg] at h + have hvgb : a1.bv &&& (smVgic ||| smVgicLr) ≠ 0 := fun e => by + have : a1 &&& (8#u64 ||| 32#u64) = 0#u64 := u64_eq_of_bv_eq e + simp [this] at hvg + have hmnz : a1 ≠ 0#u64 := fun e => by subst e; exact hvgb (by decide) + obtain ⟨res, hres, h⟩ := bind_eq_ok h + cases res with + | some v => + exfalso + dsimp only at h + repeat (first | (with_reducible exact (noOkA h).elim) | (rw [if_pos rfl] at h) | (rw [if_neg Bool.false_ne_true] at h) | (dsimp only [Bool.false_eq_true, eq_self, ↓reduceIte] at h) | (rw [eq_of_spec_fd Fl4ProofsA64.P0.Consts.layout.tcb.RUN_VCPU.spec, bind_ok] at h) | (refine (bind_eq_ok h).elim fun p hp => ?_; obtain ⟨-, h⟩ := hp; (try obtain ⟨_, _⟩ := p))) + all_goals (first | (dsimp only [Bool.false_eq_true, eq_self, ↓reduceIte] at h; exact noOkA h) | exact noOkA h) + | none => + obtain ⟨hsp, hread⟩ := vgicNoneA J hsbOS hc' hbits h12 h8 hfit + (hAg _ _ _ _ _ _ _ a0 a1 hc' rfl rfl hmnz) rfl hgv hres rfl + rw [hsp] at hread + refine invArgA 1#u64 1 rfl (errInjA h) ?_ + rw [preV, NonDetE.whenE, if_neg hn3, ndE_pure_bindA, NonDetE.whenE, + if_neg (show ¬ v0.vcpuExt.runCaller ≠ none from fun e => e hrc0), ndE_pure_bindA, + if_pos hvgb, ndE_liftE_bind _ _ _ _ none hread]; rfl + | _ => + rw [hc] at h habs hwf; dsimp only at h + first + | exact False.elim hwf + | (refine illBuf (errInjA h) fun p r sz d mp e => ?_ + rw [e] at habs; cases habs) + | (rename_i p mp asid va + refine illBuf (errInjA h) fun p r sz d mp' e => ?_ + rw [e] at habs + cases mp <;> cases habs) + +/-! ## The piece -/ + +set_option maxHeartbeats 16000000 in +/-- **`DecErrSpecPA` for VCPU caps** (module docstring). -/ +theorem DecErrSpecPA.vcpu (hP : VcpuPlatAgreeA ar) (hAK : VcpuArchKeepsA ar AR) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < Fl4SpecA64.fl4A64Platform.numNodes) (hCsN : ∀ c ∈ Z.Cs, c.val < n) + (h1 : Fl4SpecA64.fl4A64Platform.numNodes = 1) + -- [S] no Stage-2-mapped frame as the first extra cap + (hNoS2 : ∀ info buf (e : Array obj.Slot 3#usize), msg.msg_info st s (L.curW s) = ok info → + msg.lookup_ipc_buffer st s false (L.curW s) = ok buf → + ipc.lookup_extra_caps st s (L.curW s) buf info = ok (.Ok e) → + ∀ p sz r d mp ept asid va, capA st s (exAtA e 0) = .Frame p sz r d mp ept asid va → ept = false) + -- [S] `WriteState`'s input window + (hAgreeWS : ∀ info buf (e : Array obj.Slot 3#usize), msg.msg_info st s (L.curW s) = ok info → + msg.lookup_ipc_buffer st s false (L.curW s) = ok buf → + ipc.lookup_extra_caps st s (L.curW s) buf info = ok (.Ok e) → + ∀ p sz r mp ept asid va off mask, capA st s (exAtA e 0) = .Frame p sz r false mp ept asid va → + msg.syscall_arg st s (L.curW s) buf 0#u64 = ok off → msg.syscall_arg st s (L.curW s) buf 1#u64 = ok mask → + mask ≠ 0#u64 → FrameInputAgreeA st s σ (wa p off)) + -- [S] `Run`'s input window + (hAgreeRun : ∀ buf, msg.lookup_ipc_buffer st s false (L.curW s) = ok buf → + ∀ p sz r mp ept asid va off mask, capA st s (sbWordA (L.curW s)) = .Frame p sz r false mp ept asid va → + msg.syscall_arg st s (L.curW s) buf 0#u64 = ok off → msg.syscall_arg st s (L.curW s) buf 1#u64 = ok mask → + mask ≠ 0#u64 → FrameInputAgreeA st s σ (wa p off)) + {calling blocking donate : Bool} {cptr : U64} : + DecErrSpecPA st ar Z.th s a σ calling blocking donate cptr VcpuCapA := by + intro info buf r1 e x s' a' mi sl c excaps buffer args einfo ebuf elr eex hd hne hPc hlab hsl hcr hok hab hbv + gmi _ _ _ gmrs + obtain ⟨-, argsU, hargsE, hlen, hargs⟩ := J.argsLabA einfo hbv gmi gmrs + subst hargsE + obtain ⟨_, _, _, _, _, _, Q⟩ := J.siteA einfo ebuf elr eex + obtain ⟨vc, hcap⟩ : ∃ vc, capA st s r1.slot = .Vcpu vc := by + revert hPc + generalize capA st s r1.slot = c0 + intro h + cases c0 <;> first | exact ⟨_, rfl⟩ | exact h.elim + rw [hcap] at hd hcr + have hc2 : c = .archObjectCap (.vcpuCap (specPtr vc)) := by + obtain ⟨-, habs⟩ := hcr + rw [← habs]; rfl + have J3 := J.toJStage3A + have hslO : cslotAddrA sl ∈ OS := J3.j5.2.1 _ (hab.isSlot J3.j.1.place) + have hcov : SlotCov st OS s (cslotAddrA sl) := by rw [← hsl]; exact Q.cov + have hvc : vc ∈ Z.V := J3.j.2.1.cap _ (hab.isSlot J3.j.1.place) vc (by rw [← hsl]; exact hcap) + have RS := J3.rsSiteA Q.hext hslO hcov J.actW J.running J.cur + obtain ⟨Pt, Ub, Pp7, Tb7, Hr, Te, Kt, Lv7, Re, bits7, KLv, W⟩ := J7A.unp J3.j7 + have C := J.vcpuDecCtxA W hCs hCsN h1 hvc + have hcurT : L.curW s ∈ Z.T := J.sr.curT + have hvcR : ∀ v ∈ Z.V, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → + wa (L.curW s) K ≠ v ∧ wa (L.curW s) K ≠ wa v 24#u64 := fun v hv K _ hK => by + have h0 : wa v 0#u64 = v := by apply UScalar.eq_of_val_eq; rw [wa_valP]; simp [UScalar.hBounds] + refine ⟨?_, (C.hV.place.disj_tcb v hv _ hcurT 24#u64 K (by decide) (by omega)).symm⟩ + have := (C.hV.place.disj_tcb v hv _ hcurT 0#u64 K (by decide) (by omega)).symm + rwa [h0] at this + -- it is enough to give the arch decoder's error + suffices K : VcErrA σ x (fl4ArchDecodeInvocation (Z := Unit) calling (fl4LabelOf info.label.bv) + (argsU.map UScalar.bv) (Fl4SpecA64.toBl cptr.bv) sl (.vcpuCap (specPtr vc)) excaps) by + obtain ⟨e', hr, hv⟩ := K + have run_dec : fl4DecodeInvocation (Z := Unit) calling false mi.miLabel (argsU.map UScalar.bv) + (Fl4SpecA64.toBl cptr.bv) sl c excaps buffer = + Fl4Spec.NonDetE.liftME Fl4Invocation.invokeArchObject (fl4ArchDecodeInvocation (Z := Unit) calling + (fl4LabelOf info.label.bv) (argsU.map UScalar.bv) (Fl4SpecA64.toBl cptr.bv) sl (.vcpuCap (specPtr vc)) + excaps) := by + rw [hc2, hlab]; rfl + rw [run_dec] + exact decErr_ofA hv (run_liftME_errA hr) + have lift : ∀ {d : SEMonad Unit Fl4VcpuInvocation}, VcErrA σ x d → + VcErrA σ x (NonDetE.liftME Fl4ArchInvocation.vcpu d) := + fun ⟨e', hr, hv⟩ => ⟨e', liftME_vcpu_errA hr, hv⟩ + have top : ∀ {D : SEMonad Unit Fl4ArchInvocation} {p : Result (core.result.Result Unit error.Exc × S × A)}, + p ⦃ res => VcpuTopPostA (L := L) AR Z OS m calling σ (D.run σ) s res ⦄ → p = ok (.Err x, s', a') → + VcErrA σ x D := fun K h => by + rw [h, WP.spec_ok] at K + exact K.2.2 + obtain ⟨hL60, hL61, hL62, hL63, hL64, hL67, hL68, hL69⟩ := fl4LabelOf_vcpu_eqA + unfold syscall.decode_invocation at hd + rw [show sched.cur_thread st s = ok (L.curW s) from eq_of_spec_fd (sched.cur_thread.spec s), bind_ok] at hd + dsimp only at hd + unfold vcpu.decode at hd + obtain ⟨Rp', Nt', extra', sp', Pl⟩ := RS.pl + by_cases h60 : info.label = fl4_abi.label.ARMVCPUSetTCB + · rw [h60] at hd ⊢; rw [if_pos rfl] at hd + have hdec : fl4ArchDecodeInvocation (Z := Unit) calling (fl4LabelOf fl4_abi.label.ARMVCPUSetTCB.bv) + (argsU.map UScalar.bv) (Fl4SpecA64.toBl cptr.bv) sl (.vcpuCap (specPtr vc)) excaps = + decodeFl4VcpuSetTcb (specPtr vc) excaps := by + unfold fl4ArchDecodeInvocation decodeFl4VcpuSetTcb; rw [hL60]; dsimp only [archL] + rw [if_neg (by decide), if_neg (by decide), if_neg (by decide), if_neg (by decide), + if_neg (by decide), if_neg (by decide), if_neg (by decide), if_pos rfl] + rfl + obtain ⟨e', hr, hv⟩ := vcpuSetTcb.decErrA (σ := σ) J.j.1.place hok hd + refine ⟨e', ?_, hv⟩ + rw [hdec] + unfold decodeFl4VcpuSetTcb ExceptT.run + change ((decodeVcpuSetTcb (Z := Unit) (.vcpuCap (specPtr vc)) excaps).run >>= _) σ = _ + rw [nd_bind_oneSc hr]; rfl + rw [if_neg h60] at hd + by_cases h61 : info.label = fl4_abi.label.ARMVCPUInjectIRQ + · rw [h61] at hd ⊢; rw [if_pos rfl] at hd + have hdec : fl4ArchDecodeInvocation (Z := Unit) calling (fl4LabelOf fl4_abi.label.ARMVCPUInjectIRQ.bv) + (argsU.map UScalar.bv) (Fl4SpecA64.toBl cptr.bv) sl (.vcpuCap (specPtr vc)) excaps = + NonDetE.liftME Fl4ArchInvocation.vcpu (decodeFl4VcpuInjectIrq (Z := Unit) (specPtr vc) + (argsU.map UScalar.bv)) := by + unfold fl4ArchDecodeInvocation; rw [hL61]; simp [archL] + rw [hdec] + exact top (vcpu_inject_irq_jA hP hAK C Pl RS.sym RS.run RS.cur RS.vs _ _ calling argsU hlen hargs) hd + rw [if_neg h61] at hd + by_cases h62 : info.label = fl4_abi.label.ARMVCPUReadReg + · rw [h62] at hd ⊢; rw [if_pos rfl] at hd + have hdec : fl4ArchDecodeInvocation (Z := Unit) calling (fl4LabelOf fl4_abi.label.ARMVCPUReadReg.bv) + (argsU.map UScalar.bv) (Fl4SpecA64.toBl cptr.bv) sl (.vcpuCap (specPtr vc)) excaps = + NonDetE.liftME Fl4ArchInvocation.vcpu (decodeFl4VcpuReadReg (Z := Unit) (specPtr vc) + (argsU.map UScalar.bv)) := by + unfold fl4ArchDecodeInvocation; rw [hL62]; simp [archL] + rw [hdec] + exact top (vcpu_read_reg_jA hP hAK C RS hvcR Pl RS.sym RS.run RS.cur RS.vs _ _ calling argsU hlen hargs) hd + rw [if_neg h62] at hd + by_cases h63 : info.label = fl4_abi.label.ARMVCPUWriteReg + · rw [h63] at hd ⊢; rw [if_pos rfl] at hd + have hdec : fl4ArchDecodeInvocation (Z := Unit) calling (fl4LabelOf fl4_abi.label.ARMVCPUWriteReg.bv) + (argsU.map UScalar.bv) (Fl4SpecA64.toBl cptr.bv) sl (.vcpuCap (specPtr vc)) excaps = + NonDetE.liftME Fl4ArchInvocation.vcpu (decodeFl4VcpuWriteReg (Z := Unit) (specPtr vc) + (argsU.map UScalar.bv)) := by + unfold fl4ArchDecodeInvocation; rw [hL63]; simp [archL] + rw [hdec] + exact top (vcpu_write_reg_jA hP hAK C Pl RS.sym RS.run RS.cur RS.vs _ _ calling argsU hlen hargs) hd + rw [if_neg h63] at hd + by_cases h64 : info.label = fl4_abi.label.ARMVCPUAckVPPI + · rw [h64] at hd ⊢; rw [if_pos rfl] at hd + have hdec : fl4ArchDecodeInvocation (Z := Unit) calling (fl4LabelOf fl4_abi.label.ARMVCPUAckVPPI.bv) + (argsU.map UScalar.bv) (Fl4SpecA64.toBl cptr.bv) sl (.vcpuCap (specPtr vc)) excaps = + NonDetE.liftME Fl4ArchInvocation.vcpu (decodeFl4VcpuAckVppi (Z := Unit) (specPtr vc) + (argsU.map UScalar.bv)) := by + unfold fl4ArchDecodeInvocation; rw [hL64]; simp [archL] + rw [hdec] + exact top (vcpu_ack_vppi_jA hAK C Pl RS.sym RS.run RS.cur RS.vs _ _ calling argsU hlen hargs) hd + rw [if_neg h64] at hd + by_cases h67 : info.label = fl4_abi.label.ARMVCPUReadState + · rw [h67] at hd ⊢; rw [if_pos rfl] at hd + have hdec : fl4ArchDecodeInvocation (Z := Unit) calling (fl4LabelOf fl4_abi.label.ARMVCPUReadState.bv) + (argsU.map UScalar.bv) (Fl4SpecA64.toBl cptr.bv) sl (.vcpuCap (specPtr vc)) excaps = + NonDetE.liftME Fl4ArchInvocation.vcpu (decodeVcpuStateTransfer (Z := Unit) (specPtr vc) + (argsU.map UScalar.bv) excaps true) := by + unfold fl4ArchDecodeInvocation; rw [hL67]; simp [archL] + rw [hdec] + exact lift (vcpuXfer.decErrA J hP hlen hargs hok Q.hext (hNoS2 _ _ _ einfo ebuf eex) + (fun e => (Bool.noConfusion e)) hd) + rw [if_neg h67] at hd + by_cases h68 : info.label = fl4_abi.label.ARMVCPUWriteState + · rw [h68] at hd ⊢; rw [if_pos rfl] at hd + have hdec : fl4ArchDecodeInvocation (Z := Unit) calling (fl4LabelOf fl4_abi.label.ARMVCPUWriteState.bv) + (argsU.map UScalar.bv) (Fl4SpecA64.toBl cptr.bv) sl (.vcpuCap (specPtr vc)) excaps = + NonDetE.liftME Fl4ArchInvocation.vcpu (decodeVcpuStateTransfer (Z := Unit) (specPtr vc) + (argsU.map UScalar.bv) excaps false) := by + unfold fl4ArchDecodeInvocation; rw [hL68]; simp [archL] + rw [hdec] + exact lift (vcpuXfer.decErrA J hP hlen hargs hok Q.hext (hNoS2 _ _ _ einfo ebuf eex) + (fun _ p sz r mp ept asid va off mask hc h0 h1' hm => + hAgreeWS _ _ _ einfo ebuf eex p sz r mp ept asid va off mask hc (hargs 0#u64 off h0) (hargs 1#u64 mask h1') hm) + hd) + rw [if_neg h68] at hd + by_cases h69 : info.label = fl4_abi.label.ARMVCPURun + · rw [h69] at hd ⊢; rw [if_pos rfl] at hd + have hdec : fl4ArchDecodeInvocation (Z := Unit) calling (fl4LabelOf fl4_abi.label.ARMVCPURun.bv) + (argsU.map UScalar.bv) (Fl4SpecA64.toBl cptr.bv) sl (.vcpuCap (specPtr vc)) excaps = + NonDetE.liftME Fl4ArchInvocation.vcpu (decodeVcpuRun (Z := Unit) (specPtr vc) (argsU.map UScalar.bv) + calling) := by + unfold fl4ArchDecodeInvocation; rw [hL69]; simp [archL] + rw [hdec] + exact lift (vcpuRun.decErrA J hP C hlen hargs + (fun p sz r mp ept asid va off mask hc h0 h1' hm => + hAgreeRun _ ebuf p sz r mp ept asid va off mask hc (hargs 0#u64 off h0) (hargs 1#u64 mask h1') hm) hd) + rw [if_neg h69] at hd + exact illA (errInjA hd) (fl4LabelOf_other_vcpu_illegalA info.label h60 h61 h62 h63 h64 h67 h68 h69 + calling (argsU.map UScalar.bv) (Fl4SpecA64.toBl cptr.bv) sl (specPtr vc) excaps σ) + +end record + +end + +end + +end Fl4ProofsA64.P3.ReplyErr diff --git a/proofs/Fl4ProofsA64/P3/Stage3DecErrVsA.lean b/proofs/Fl4ProofsA64/P3/Stage3DecErrVsA.lean new file mode 100644 index 000000000..c41302847 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3DecErrVsA.lean @@ -0,0 +1,93 @@ +/- +P3 (aarch64, A64-P3 Lift 1b, batch a): **`DecErrSpecPA` for the VSpace / ASID / Frame caps**, from `decInv_arch_jA`. + +`decInv_arch_jA`'s `Err` branch (`ArchDecTopPostA`) is the spec's error with the state kept: FL4's `Err x` is `VsErrRelA`-related +to an `e'` the spec decode of the same cap and message returns. The record supplies `JEntA`, `RsSiteA`, `TreeA`, `hnul`, the +slot facts and `ksbAligned`/`offAsE`/`tabS1`/`tabExt` as in `Stage3LiftFlushA`; everything the theorem asks beyond that is a +premise of this one (the same names as `decInv_arch_jA`'s): [P] `K` (`ArchKeepsA`), [P] `hIpa` (`S2IpaE`), [J] `hvcR`, +`hYAs`, `hPC`, `hFI`, `hcO`, `hD` (`DeadDrop4KA`), `hPtMap`, `hFrMap`, `hFrSub`, `hAsidAssign`. + +New module. +-/ +import Fl4ProofsA64.P3.Stage3DecErrBaseA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.ReplyErr +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3 Fl4ProofsA64.P3.Endp + Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {ar : machine.Arch A} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} + +set_option maxHeartbeats 8000000 in +/-- **`DecErrSpecPA` for the VSpace / ASID / Frame caps** (module docstring). -/ +theorem DecErrSpecPA.archVs (K : ArchKeepsA ar AR) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (hvcR : ∀ v ∈ Z.V, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → + wa (L.curW s) K ≠ v ∧ wa (L.curW s) K ≠ wa v 24#u64) + (hYAs : ∀ y ∈ Z.Y, ∀ j : U64, j.val < 512 → ¬ AsW (L.ksb s) (peAF y j)) + (hIpa : S2IpaE ar a) (hPC : PtCapInA Z (L.mem s)) (hFI : FinInvA Z.th (L.mem s) σ Z.G) + (hcO : ∀ y ∈ OS, obj.sext48 y = ok y) + (hD : ∀ u frame fi, capA st s u = .Untyped frame 12#u64 fi false → + cdt.ensure_no_children st s u = ok (core.result.Result.Ok ()) → DeadDrop4KA Z H frame fi) + (hPtMap : ∀ (cap : capability.Cap) (lbl ptr : U64) (mp : Bool) (asid va : U64), cap = .PageTable ptr mp asid va → lbl = fl4_abi.label.ARMPageTableMap → + UnmPtA Z.S1 Z.Y OS H.tcbs (L.mem s) σ ptr ∧ ptr ∉ S2PgE Z.X Z.T2 ∧ ptr ∉ Z.Y) + (hFrMap : ∀ (cap : capability.Cap) (lbl fp : U64) (sz : capability.FrameSize) (rt : U64) (dev mp e : Bool) (asid va : U64), cap = .Frame fp sz rt dev mp e asid va → + lbl = fl4_abi.label.ARMPageMap → FrPlA sz fp mp asid) + (hFrSub : ∀ (cap : capability.Cap) (lbl : U64) (cte : CslotPtr) (fp : U64) (sz : capability.FrameSize) (rt : U64) + (dev mp e : Bool) (asid va : U64), cap = .Frame fp sz rt dev mp e asid va → + lbl = fl4_abi.label.ARMPageDeriveSubframe → sz = capability.FrameSize.Large → dev = false → + (fp.val % 2 ^ 21 = 0 ∧ CapRt.Canonical fp) ∧ + ∀ w ∈ Z.G.chains.flatten, ∀ (q vm' : U64), fp.val ≤ q.val → q.val < fp.val + 2 ^ 21 → + OvlA (absCapA (.Frame q .Small vm' false false false 0#u64 0#u64)) (absCapA (capAtA (L.mem s) w)) → + isFrameA (capAtA (L.mem s) w) = true ∧ ∃ ch ∈ Z.G.chains, cslotAddrA cte ∈ ch ∧ w ∈ ch) + (hAsidAssign : ∀ (cap : capability.Cap) (lbl p base : U64), cap = .AsidPool p base → lbl = fl4_abi.label.ARMASIDPoolAssign → + ∀ u ∈ OS, ∀ k root oldAsid oldVa, capA st s u = assignUnmappedA k root oldAsid oldVa → + UnmRootAssignA k (L.mem s) σ root) + {calling blocking donate : Bool} {cptr : U64} : + DecErrSpecPA st ar Z.th s a σ calling blocking donate cptr VsCapA := by + intro info buf r1 e x s' a' mi sl c excaps buffer args einfo ebuf elr eex hd hne hP hlab hsl hcr hok hab hbv + gmi ggc glb gex gmrs + obtain ⟨-, argsU, hargsE, hlen, hargsU⟩ := J.argsLabA einfo hbv gmi gmrs + subst hargsE + obtain ⟨_, _, _, _, _, _, Q⟩ := J.siteA einfo ebuf elr eex + have J3 := J.toJStage3A + have Je := J3.toJEntA + have hslO : cslotAddrA sl ∈ OS := J3.j5.2.1 _ (hab.isSlot Je.j.1.place) + have hcov : SlotCov st OS s (cslotAddrA sl) := by rw [← hsl]; exact Q.cov + have hext : ∀ x ∈ e.val, x ≠ 0#u64 → x ∈ OS := Q.hext + have RS := J3.rsSiteA hext hslO hcov J.actW J.running J.cur + have hTr : TreeA st OS Z.G.chains Z.G.par s := + Je.j.1.cdt.tree.grow J3.j5.2.1 (fun x hx hn => J3.j5.2.2 x hx hn) + have hcap : capAtA (L.mem s) (cslotAddrA sl) = capA st s r1.slot := by rw [hsl]; rfl + have K1 := decInv_arch_jA K Je RS hvcR J.tabS1 J.tabExt J3.ksbAligned J3.offAsE hYAs hIpa hPC hFI hTr + (fun y hy hn => J3.j5.2.2 y hy hn) hcO hD sl hab hslO (capA st s r1.slot) hP.1 hcap info.label (msgLenA info buf) buf + blocking calling donate false e argsU hlen hargsU (Fl4SpecA64.toBl cptr.bv) excaps hok false buffer + (hPtMap _ _) (hFrMap _ _) (hFrSub _ _ sl) (hAsidAssign _ _) + have hd' : syscall.decode_invocation st ar s a info.label (msgLenA info buf) (cslotAddrA sl) (capA st s r1.slot) + blocking calling donate false buf e = ok (.Err x, s', a') := by rw [← hsl]; exact hd + rw [hd', WP.spec_ok] at K1 + obtain ⟨-, -, e', hD', hrel⟩ := K1 + obtain ⟨e2, hxe⟩ := sys_of_vsErrRelA hrel + refine ⟨e2, e', hxe, hrel, ?_⟩ + have hc2 : c = absCapA (capA st s r1.slot) := by + have hce := hcr.2 + have hnp := hP.2 + generalize capA st s r1.slot = cap at hce hnp ⊢ + cases cap <;> first | exact hce.symm | exact absurd trivial hnp + rw [hc2, hlab, hD'] + exact Set.mem_singleton _ + +end + +end + +end Fl4ProofsA64.P3.ReplyErr diff --git a/proofs/Fl4ProofsA64/P3/Stage3DecExtraA.lean b/proofs/Fl4ProofsA64/P3/Stage3DecExtraA.lean new file mode 100644 index 000000000..373f4fa56 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3DecExtraA.lean @@ -0,0 +1,127 @@ +/- +P3 (aarch64, A64-P3 Stage-3 T3 item G3, part 4): **`DecPre.extra` under the narrowed `NodeW`** (record edit #6, +item 10: `NodeW` excludes `IDLE_THREAD`). + +* `JStage3A.offNode`: an `Ω` word or a TCB low word is no word of the current core's node (`out_ne_node`, + `W7.tlow_ne_node`); +* `JStage3A.slot_keepNode`: a `NodeW` step keeps every `OS` slot's cap (its words are `CapW` words, `OSOK.slot`); +* `JStage3A.idle_keepNode`: it keeps the idle-thread word (`IDLE_THREAD`, offset 8, is no `NodeW` word); +* `JStage3A.scTcb_keepNode`: it keeps every SC's `TCB` word (an `Ω` word of the SC object); +* **`JStage3A.decExtra`**: `DecPre.extra` for extra caps in `OS` slots (the decode site's lookup results), from + `JStage3A.capOK` at the decode state; +* **`JStage3EntryA.decPreA`**: `DecPre` at the decode site with `extra` discharged (the G4 facts remain hypotheses + until `JSyscallA` lands on this branch). + +Named premises: none. New module; no existing statement changed. +-/ +import Fl4ProofsA64.P3.Stage3SysPlaceA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section rows +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- An `Ω` word or a TCB low word is no current-node word. -/ +theorem JStage3A.offNode (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {b : U64} (hb : b ∈ H.omega ∨ W7.TLow H.tcbs b) : ¬ NodeW st s b := by + have q := J.sq + rintro ⟨o, ho, -, -, e⟩ + rcases hb with hb | hb + · exact out_ne_node (q.omega _ hb) q.cur (q.nodes _ q.cur) ho (by rw [e]) + · exact W7.tlow_ne_node q.tcbs (q.nodes _ q.cur) ho hb e.symm + +/-- **A `NodeW` step keeps every `OS` slot's cap.** -/ +theorem JStage3A.slot_keepNode (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {s' : S} (w : WritesIn st s s' (NodeW st s)) {x : U64} (hx : x ∈ OS) : capA st s' x = capA st s x := by + have hsw := J.sys.ent.os.slot x hx + have hfit := J.sys.ent.os.fit x hx + have keepw : ∀ o : U64, o.val < 32 → o.val % 8 = 0 → L.mem s' (wa x o) = L.mem s (wa x o) := by + intro o h1 h2 + have cw := hsw.2 o h1 h2 + have al : Aligned (wa x o) := by + have := hsw.1 + unfold Aligned at this ⊢ + rw [wa_val, Nat.mod_mod_of_dvd _ (by norm_num : 8 ∣ 2 ^ 64)]; omega + exact w.2.2 _ al (J.offNode (cw.elim (fun t => Or.inr t) (fun h => Or.inl h.1))) + have k0 := keepw 0#u64 (by simp) (by simp) + rw [wa_zeroVcA] at k0 + unfold capA + rw [k0, keepw 8#u64 (by simp) (by simp)] + +/-- **A `NodeW` step keeps the idle-thread word.** -/ +theorem JStage3A.idle_keepNode (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {s' : S} (w : WritesIn st s s' (NodeW st s)) : L.idleW s' = L.idleW s := by + have q := J.sq + have hN := q.nodes _ q.cur + have h8 : layout.node.IDLE_THREAD.val = 8 := idle8 + unfold LawfulStore.idleW + rw [w.2.1, nodeAt_of_ksb w.1] + refine w.2.2 _ (nodeAt_aligned hN (by rw [h8]; omega) (by rw [h8])) ?_ + rintro ⟨o, ho, -, h8', e⟩ + have := congrArg UScalar.val e + rw [nodeAt_val hN (by rw [h8]; omega), nodeAt_val hN ho, h8] at this + omega + +/-- **A `NodeW` step keeps every SC's `TCB` word** (an `Ω` word of the SC object). -/ +theorem JStage3A.scTcb_keepNode (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {s' : S} (w : WritesIn st s s' (NodeW st s)) {p : U64} (hp : p ∈ H.scs) : + L.mem s' (scTcbA p) = L.mem s (scTcbA p) := by + obtain ⟨z, hz⟩ := hp + have hH := J.sys.ent.heap + have hf := hH.fits p _ hz + have hal := hH.align p _ hz + have hmin := hH.scmin p z hz + simp only [show (HKind.sc z).bits = z from rfl] at hf hal + have h128 : 128 ≤ 2 ^ z := by + calc 128 = 2 ^ 7 := by norm_num + _ ≤ 2 ^ z := Nat.pow_le_pow_right (by norm_num) hmin + have h8 : p.val % 8 = 0 := by + have : (8 : Nat) ∣ 2 ^ z := by + have : 2 ^ z = 2 ^ 3 * 2 ^ (z - 3) := by rw [← pow_add]; congr 1; omega + rw [this]; exact Dvd.intro _ rfl + exact Nat.mod_eq_zero_of_dvd (Nat.dvd_trans this (Nat.dvd_of_mod_eq_zero hal)) + have ev : (scTcbA p).val = p.val + 24 := by + unfold scTcbA + rw [wa_val, Fl4ProofsA64.P0.Consts.layout.sc.TCB.val_eq]; exact Nat.mod_eq_of_lt (by omega) + have al : Aligned (scTcbA p) := by unfold Aligned; rw [ev]; omega + refine w.2.2 _ al (J.offNode (Or.inl ⟨al, p, _, hz, Or.inr (Or.inl ⟨z, rfl⟩), ?_⟩)) + unfold InObj; simp only [show (HKind.sc z).bits = z from rfl]; omega + +/-- **`DecPre.extra`** for extra caps in `OS` slots (module docstring). -/ +theorem JStage3A.decExtra (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + {extra : Aeneas.Std.Array obj.Slot 3#usize} (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) : + ∀ s', WritesIn st s s' (NodeW st s) → ∀ sl ∈ extra.val, sl ≠ 0#u64 → + obj.Slot.cap st sl s' ⦃ c => CapOK st H.tcbs H.replies H.scs H.ntfns H s' c ⦄ := by + intro s' w sl hsl h0 + have hx := hext sl hsl h0 + have C := J.capOK hx + rw [capA_eq, WP.spec_ok, J.slot_keepNode w hx] + refine ⟨C.1, C.2.1, fun p b e => ?_⟩ + obtain ⟨h1, h2, h3⟩ := C.2.2 p b e + exact ⟨h1, h2, by rw [J.scTcb_keepNode w h1, J.idle_keepNode w]; exact h3⟩ + +/-- **`DecPre` at the decode site**, `extra` discharged (module docstring). -/ +theorem JStage3EntryA.decPreA + (J : JStage3EntryA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + (hact : L.actionW s = 0#u64) (hrun : Runnable (L.mem s (stA (L.curW s)))) + (hcsc : L.mem s (scA (L.curW s)) ≠ 0#u64 → L.cscW s = L.mem s (scA (L.curW s))) + (hcur : (L.mem s (inqA (L.curW s)) &&& sched.Q_RELEASE) = 0#u64 ∧ L.mem s (scA (L.curW s)) ≠ 0#u64) + {x : U64} (hx : x ∈ OS) {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hext : ∀ y ∈ extra.val, y ≠ 0#u64 → y ∈ OS) : + DecPre st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits U Ac s (capA st s x) extra := + J.decPre hact hrun hcsc hcur hx (J.toJStage3A.decExtra hext) + +end rows + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3DelAsmA.lean b/proofs/Fl4ProofsA64/P3/Stage3DelAsmA.lean new file mode 100644 index 000000000..c3d889c72 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3DelAsmA.lean @@ -0,0 +1,379 @@ +/- +P3 (AArch64, delete hooks, contract §90): **the assembly** of the CNode Delete / Revoke deletion hypotheses at +`DelWTopA`, and the twin tops without `Hk` / `hXr` / `hPA`. + +* **`DelPlatA`**: the platform premises of the hooks, the preemption point and the post-cleanup, in one record. +* **`DelS4A`**: the S4 plug-ins: the VCPU hook (`FinHookKA .vcpu`) and the Thread hook's detach premises + (`finHookThreadA`'s `hDetS` / `hComm` / `hKeepB`, copied verbatim). +* **`j7FrA_j7LA`**, **`DelVcpuHookA`**, **`DelS4A.ofHookA`** (§93): `DelS4A` from the VCPU hook alone (`detach` / + `comm` / `keepB` are S4's `vcpu_detach_stage3TA` / `fl4Dissociate_comm_tcbFinA` / `fl4VcpuEndRun_keeps_bindA`). +* **`delHypsA_topA`**: `DelHypsA` at `DelWTopA` / `DelWxTopA` / `DelWTopA`, from the S4 plug-ins `DelS4A`. +* **`JSyscallA.delRestartWA`**: `DelWTopA` at the restarted state (the tops' `hXr`). + +The twin tops (`JSyscallA.delHypsTopA` / `cnodeDeleteAsmA` / `cnodeRevokeAsmA`) are in `P3/Stage3DelAsmTopA`, which has +the VCPU hook (`Vcpu.vcpuHookA`). + +Named premises: `DelPlatA` ([P]; `numNodes1` / `numDomains1` [S]); `hCs` [S]; `DelS4A` (discharged downstream); +`PlainStore` (the Frame hook). The MDB parent adjacency comes from the record (`J.j5.1.1.parentAdj`) and the caller not +running a VCPU from `JStage3EntryA.curNotVmA` (§91). New module. +-/ +import Fl4ProofsA64.P3.Stage3DelSvScA +import Fl4ProofsA64.P3.Stage3DelSvThrA +import Fl4ProofsA64.P3.DelThrHookA +import Fl4ProofsA64.P3.DelPpA +import Fl4ProofsA64.P3.DelStepAsmA +import Fl4ProofsA64.P3.DelNtfnHookA +import Fl4ProofsA64.P3.Stage3RestartA +import Fl4ProofsA64.P3.Stage3OffAsEA +import Fl4ProofsA64.P3.Stage3Rec5A +import Fl4ProofsA64.P3.VcpuDelHookA +import Fl4ProofsA64.P3.VcpuDelCommA +import Fl4ProofsA64.P3.VcpuDelSpecA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Vcpu Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {m : Fl4SpecA64.McState} +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {Clock : A → Fl4SpecA64.MachineState → Prop} + +variable (st) in +/-- **[P]** The platform premises of the deletion. -/ +structure DelPlatA (ar : machine.Arch A) (AR : A → HwRootA → Prop) + (Clock : A → Fl4SpecA64.MachineState → Prop) : Prop where + total : P0.ArchTotal ar + keeps : ArchKeepsA ar AR + del : DelArchA st ar AR Clock + s2Ipa : ∀ a, S2IpaE ar a + vcpuAgree : VcpuPlatAgreeA ar + vcpuKeeps : VcpuArchKeepsA ar AR + vcpuVmRoot : VcpuVmRootA st ar AR + minBudget : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄ + maxTicks : maxTicksToUs = core.num.U64.MAX.bv + ticksUs : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄ + /-- [S] -/ + numNodes1 : fl4A64Platform.numNodes = 1 + /-- [S] -/ + numDomains1 : numDomains = 1 + now : NowAgreeA ar Clock + /-- [P] (Task 0) the raw counter oracle is monotone. -/ + counter : ∀ i, fl4RawCounterOracle i ≤ fl4RawCounterOracle (i + 1) + rawNow : ∀ σ : Fl4SpecA64.State Unit, fl4ReadRawCounter σ.machineState = getCurrentTime σ.machineState + arStamp : ∀ (s : S) (a : A) (hw : HwRootA), AR a hw → sched.update_timestamp st ar s a ⦃ r => AR r.2 hw ⦄ + irqPending : ∀ (a : A) (σ : Fl4SpecA64.State Unit) (hw : HwRootA), AR a hw → Clock a σ.machineState → + ar.irq_pending a ⦃ r => ∃ o ms, (o, { σ with machineState := ms }) ∈ + (doMachineOp (getActiveIRQ true) σ).1 ∧ (r.1 = true ↔ ¬ o = none) ∧ + ms.underlyingMemory = σ.machineState.underlyingMemory ∧ + ms.lastMachineTime = σ.machineState.lastMachineTime ∧ ms.timeState = σ.machineState.timeState ∧ + Clock r.2 ms ∧ AR r.2 hw ⦄ + /-- [P] FL4's configuration minimum budget is the spec's `MIN_BUDGET` (the preemption point's `OffOKA`). -/ + minAgree : Fl4SpecA64.fl4MinBudget = Fl4SpecA64.MIN_BUDGET + +variable (st) in +/-- **The S4 plug-ins** (VCPU line): the VCPU finaliser hook and the Thread hook's detach premises. -/ +structure DelS4A (ar : machine.Arch A) (AR : A → HwRootA → Prop) (Z : XSetsA) (H : Heap) (OS U Pp Tb : Set U64) + (Lv : U64 → Nat) (ia ie : Nat) (N E Rp : Set U64) (κ : U64 → U64) (n : Nat) (m : McState) (ipa : U64) + (Clock : A → Fl4SpecA64.MachineState → Prop) (s0 : S) : Prop where + vcpuHook : FinHookKA st .vcpu ar Z.th OS fl4FinaliseCap + (DelWTopA st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar ipa Clock s0) + (DelWxTopA st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m Clock s0) + (DelRdXA st ar Z H OS ipa (RvVcA st OS fun _ _ _ _ _ => True)) (DelRzA st) + detach : ∀ {Z' : XSetsA} {Ac : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} + {s : S} {a : A} {σ : State Unit} {m : McState} + (_ : JStage3TA st AR Z' H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7LA st (J7R2A st ar ipa)) s a σ m) + (_ : SchedValidA σ) (_ : VcpuStateSnapA Z'.V (L.mem s) σ) (_ : VcSym1A st Z'.T s) (_ : VcSym2A st Z'.V s) + (_ : ∀ v ∈ Z'.V, L.mem s v ≠ 0#u64 → L.mem s (wa (L.mem s v) 3664#u64) = v) + (_ : ∀ u ∈ Z'.T, L.mem s (runVcpuA u) ≠ 0#u64 → L.mem s (wa (L.mem s (runVcpuA u)) 24#u64) = u) + (_ : ∀ b, VcW Z'.V b → ¬ InOS OS b) (_ : FrOff st Z'.Cs OS (L.ksb s) s) (_ : UserFrSpecA st OS s σ) (_ : FrThA st Z'.th OS s) (_ : ∀ bp ∈ Bf, bp.2 = specPtr bp.1) + (_ : Clock a σ.machineState) + (_ : ∀ s' a' ms (u : U64), Clock a' ms → vspace.set_vm_root st ar s' a' u ⦃ r => Clock r.2 ms ⦄) + (_ : ∀ a' ms (vc : U64), Clock a' ms → ar.vcpu_flush a' vc ⦃ a'' => Clock a'' ms ⦄) + (_ : ∀ a' ms (vc om : U64), Clock a' ms → ar.vcpu_flush_state a' vc om ⦃ a'' => Clock a'' ms ⦄) + (_ : ∀ a' ms ms', Clock a' ms → ms'.timeState = ms.timeState → + ms'.lastMachineTime = ms.lastMachineTime → Clock a' ms') + (_ : VcpuPlatAgreeA ar) (_ : VcpuArchKeepsA ar AR) (_ : VcpuVmRootA st ar AR) + (_ : ∀ c ∈ Z'.Cs, c.val < fl4A64Platform.numNodes) (_ : fl4A64Platform.numNodes = 1) + {t : U64} (_ : t ∈ Z'.T), + vcpu.detach st ar s a t ⦃ res => + ∃ (Q' : U64 → U64 → List U64) (R' : U64 → List U64) (hw' : HwRootA) (σ' : State Unit), + ((), σ') ∈ (fl4VcpuDetach (specPtr t) σ).1 ∧ + JStage3TA st AR { Z' with Q := Q', R := R', hw := hw' } H OS U Ac Pp Tb Lv ia ie + N E Rp Bf stack κ n (J5A st) (J7LA st (J7R2A st ar ipa)) res.1 res.2 σ' m ∧ + SchedValidA σ' ∧ + VcpuStateSnapA Z'.V (L.mem res.1) σ' ∧ + VcSym1A st Z'.T res.1 ∧ VcSym2A st Z'.V res.1 ∧ + (∀ v ∈ Z'.V, L.mem res.1 v ≠ 0#u64 → L.mem res.1 (wa (L.mem res.1 v) 3664#u64) = v) ∧ + (∀ u ∈ Z'.T, L.mem res.1 (runVcpuA u) ≠ 0#u64 → + L.mem res.1 (wa (L.mem res.1 (runVcpuA u)) 24#u64) = u) ∧ + UserFrSpecA st OS res.1 σ' ∧ + Clock res.2 σ'.machineState ∧ + σ'.machineState.timeState = σ.machineState.timeState ∧ + σ'.machineState.lastMachineTime = σ.machineState.lastMachineTime ∧ + L.mem res.1 (wa t 3664#u64) = 0#u64 ∧ + L.mem res.1 (runVcpuA t) = 0#u64 ∧ + (L.mem s (wa t 3664#u64) ≠ 0#u64 → + L.mem res.1 (L.mem s (wa t 3664#u64)) = 0#u64 ∧ + L.mem res.1 (wa (L.mem s (wa t 3664#u64)) 24#u64) = 0#u64) ∧ + L.ksb res.1 = L.ksb s ∧ L.cpu res.1 = L.cpu s ∧ OSKeep st OS s res.1 ∧ + (∀ y ∈ OS, capA st res.1 y = capA st s y) ∧ + L.mem res.1 (boundA t) = L.mem s (boundA t) ∧ + L.mem res.1 (scA t) = L.mem s (scA t) ∧ + L.mem res.1 (stA t) = L.mem s (stA t) ∧ + (∀ x, σ.kheap (specPtr t) = some (.tcb x) → + ∃ x', σ'.kheap (specPtr t) = some (.tcb x') ∧ + x'.tcbArch.tcbVcpu = none ∧ x'.tcbExt.tcbRunVcpu = none ∧ + x'.tcbBoundNotification = x.tcbBoundNotification ∧ + x'.tcbSchedContext = x.tcbSchedContext ∧ + x'.tcbState = x.tcbState) ⦄ + comm : ∀ (τ τ1 τ2 : State Unit) (t v : ObjRef), + (∃ xt, τ.kheap t = some (.tcb xt) ∧ xt.tcbArch.tcbVcpu = some v) → + (∃ xv, τ.kheap v = some (.archObj (.vcpu xv)) ∧ xv.vcpuTcb = some t) → + ((), τ1) ∈ (dissociateVcpuTcb (Z := Unit) v t τ).1 → + ((), τ2) ∈ (((do unbindNotification t; unbindFromSc t; suspend t) : SMonad Unit Unit) τ1).1 → + ((), τ2) ∈ (((do unbindNotification t; unbindFromSc t; suspend t; dissociateVcpuTcb v t) : + SMonad Unit Unit) τ).1 + keepB : ∀ (σ σc σe : State Unit) (t v : ObjRef) (xc : Fl4SpecA64.Tcb), + (∃ xv, σ.kheap v = some (.archObj (.vcpu xv)) ∧ xv.vcpuTcb = some t) → + ((), σc) ∈ (fl4CancelRun (Z := Unit) t σ).1 → σc.kheap t = some (.tcb xc) → xc.tcbArch.tcbVcpu = some v → + ((), σe) ∈ (fl4VcpuEndRun (Z := Unit) v σc).1 → + (∃ xt, σe.kheap t = some (.tcb xt) ∧ xt.tcbArch.tcbVcpu = some v) ∧ + (∃ xv, σe.kheap v = some (.archObj (.vcpu xv)) ∧ xv.vcpuTcb = some t) + +/-- **`J7FrA` at the loop's `J7`** (`J7LA (J7R2A ..)`; as `j7FrA_j7A`, with the loop's `UbFr ∧ FrameOK` pair in +place of `S1aCapCol`), for S4's `vcpu_detach_stage3TA` at the loop record. -/ +instance j7FrA_j7LA (ar : machine.Arch A) (nA : U64) : J7FrA st (J7LA st (J7R2A st ar nA)) where + fr := fun _ _ _ s _ _ J => by + obtain ⟨Pt, Ub, vr, -, arm, capc, -, -, -, link, -, frub, ubal, vcub, + Pp', Tb', Hr, Te, Kt, Lv', Re, bits, R2, -⟩ := J + refine ⟨Pt, Ub, vr, arm, capc.1, capc.2, link, frub, ubal, vcub, fun Pp Tb Lv hty p hp => ?_⟩ + have hpools : PoolSet st Pp s ⊆ PoolSet st Pp' s := fun q ⟨_, o, h1, h2, h3, h4, h5⟩ => by + rw [← h4]; exact R2.core.stage1.tbl o h1 h2 h3 (by rw [h4]; exact h5) + rcases hp with hP | ⟨-, hr⟩ + · exact R2.place.pools (hpools hP) + · exact R2.place.tables (vsReach_bridgeA hty R2.core.stage1 hpools hr).2.2.1 + keepFr := fun Z _ H OS s s' _ _ _ J kb osk vsk => by + obtain ⟨Pt, Ub, vr, pn, arm, cc, vcc, dfa, hi, link, vcw, fu, ual, vcu, Pp', Tb', Hr, Te, Kt, Lv', Re, bits, R2, Kut, + KLv, ktK, hik⟩ := J + have vsr := vsk Pt Ub vr fu + have tab : TabKeep st s s' := ⟨kb, fun i => by rw [kb]; exact vsr _ (Or.inr (asW_at _ i))⟩ + have cc' : UbFr st OS Ub s' ∧ FrameOK st H OS s' := by + refine ⟨fun b hb => ?_, fun y hy ptr size rights x1 x2 x3 x4 hc => ?_⟩ + · obtain ⟨y, hy, ptr, size, rights, x1, x2, x3, x4, bits, hc, h⟩ := cc.1 b hb + exact ⟨y, hy, ptr, size, rights, x1, x2, x3, x4, bits, (osk.capA hy).trans hc, h⟩ + · exact cc.2 y hy ptr size rights x1 x2 x3 x4 ((osk.capA hy).symm.trans hc) + refine ⟨Pt, Ub, by rw [kb]; exact vr, PoolNamed.ofTab kb (fun j => tab.2 j) osk pn, S1aArmCol.ofTab tab arm, + cc', CapInv.keepO vcc osk, CapInv.keepO dfa osk, + HiCap511.keep hi (fun y hy => osk.capA hy) (fun y hy p m a e => ?_), fun p hp => link p (pp_pageA osk hp), + by rw [kb]; exact vcw, FrUb.keepO fu osk, ual, vcu, Pp', Tb', Hr, Te, Kt, Lv', Re, bits, + R2.ofImage kb vsr (S1aSlotImage.ofSame R2.capRefs.refs fun _ hy => Or.inl (osk.capA hy)), Kut.keep osk, KLv, ktK, + S1aHiK.keep hik ⟨EdgeKeepOn.ofVsKeep R2 vsr, fun y hy R m a c => ⟨y, hy, m, a, by rwa [osk.capA hy] at c⟩⟩⟩ + · have live : p ∈ Hr := by rw [R2.highRoots]; exact ⟨y, hy, m, a, e⟩ + exact vsr _ (Or.inl ⟨p, R2.place.tables (R2.place.high.sub live), 4088#u64, by decide, by decide, rfl⟩) + +variable (st) in +/-- **[S4] The VCPU finaliser hook** at `DelWTopA` / `DelWxTopA` (`DelS4A.vcpuHook`; S4 deliverable 2). -/ +def DelVcpuHookA (ar : machine.Arch A) (AR : A → HwRootA → Prop) (Z : XSetsA) (H : Heap) (OS U Pp Tb : Set U64) + (Lv : U64 → Nat) (ia ie : Nat) (N E Rp : Set U64) (κ : U64 → U64) (n : Nat) (m : McState) (ipa : U64) + (Clock : A → Fl4SpecA64.MachineState → Prop) (s0 : S) : Prop := + FinHookKA st .vcpu ar Z.th OS fl4FinaliseCap + (DelWTopA st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar ipa Clock s0) + (DelWxTopA st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m Clock s0) + (DelRdXA st ar Z H OS ipa (RvVcA st OS fun _ _ _ _ _ => True)) (DelRzA st) + +variable (st) in +/-- `DelVcpuHookA` with a frame-word guard set `Ag`, at `DelWTopA` / `DelWxTopA` (`DelS4A.vcpuHook`; S4 deliverable 2). -/ +def DelVcpuHookGA (Ag : Set U64) (ar : machine.Arch A) (AR : A → HwRootA → Prop) (Z : XSetsA) (H : Heap) (OS U Pp Tb : Set U64) + (Lv : U64 → Nat) (ia ie : Nat) (N E Rp : Set U64) (κ : U64 → U64) (n : Nat) (m : McState) (ipa : U64) + (Clock : A → Fl4SpecA64.MachineState → Prop) (s0 : S) : Prop := + FinHookKA st .vcpu ar Z.th OS fl4FinaliseCap + (DelWTopGA st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar ipa Clock s0) + (DelWxTopGA st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m Clock s0) + (DelRdXA st ar Z H OS ipa (RvVcA st OS fun _ _ _ _ _ => True)) (DelRzA st) + +/-- **`DelS4A` from the VCPU hook alone** (§93): `detach` is S4's `vcpu_detach_stage3TA` at the loop's `J7`, `comm` is +`fl4Dissociate_comm_tcbFinA`, `keepB` is `fl4VcpuEndRun_keeps_bindA`. -/ +theorem DelS4A.ofHookA {ipa : U64} {s0 : S} + (hV : DelVcpuHookA st ar AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ipa Clock s0) : + DelS4A st ar AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ipa Clock s0 where + vcpuHook := hV + detach := fun J h2 h3 h4 h5 h6 h7 h8 h9 h10 h11 h12 h13 h14 h15 h16 h17 h18 h19 h20 h21 h22 _t ht => + WP.spec_mono (vcpu_detach_stage3GA J h2 h3 h4 h5 h6 h7 h8 h9 h10 h11 h12 h13 h14 h15 h16 h17 h18 h19 h20 h21 h22 ht) (by + rintro res ⟨Q', R', hw', σ', c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, c14, c15, c16, c17, c18, + c19, c20, c21, c22, c23, -⟩ + exact ⟨Q', R', hw', σ', c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, c14, c15, c16, c17, c18, + c19, c20, c21, c22, c23⟩) + comm := fun _ _ _ _ _ htv hvt h1 h2 => fl4Dissociate_comm_tcbFinA htv hvt h1 h2 + keepB := fun _ _ _ _ _ _ hv hc hkc htv he => fl4VcpuEndRun_keeps_bindA hv hc hkc htv he + +section frag +variable {Ag : Set U64} + +/-- **`Rd` at the caps the loop finalises itself.** -/ +theorem rdTrivHyp_topA {ipa : U64} {s0 : S} {Rv : S → A → Fl4SpecA64.State Unit → MdbGhostA → U64 → Prop} : + RdTrivHypA st Z.th OS (DelWTopGA st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar ipa Clock s0) + (DelRdXA st ar Z H OS ipa Rv) := by + intro s a σ G sl c D hW hsl hc hn hk h1 h2 + have rd := rdTrivHyp_delRdA (st := st) (W := DelWTopGA st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar ipa Clock s0) + (Rv := Rv) s a σ G sl c D hW hsl hc hn hk h1 h2 + obtain ⟨⟨-, ⟨⟨Q, R, hw, Ac', Bf', stack', J, -⟩, -⟩, -⟩, -⟩ := hW + have unread : ¬ S1aR2Reads (capA st s (cslotAddrA sl)) := by + rw [hc] + intro h + exact hk (by cases c <;> simp [S1aR2Reads] at h <;> trivial) + have pin := J7LA.pinUnreadA J.j7 unread + exact ⟨⟨rd, rdThA_of_ne fun t e => hk (by rw [← hc, e]; trivial)⟩, m, pin⟩ + +/-- **`DelHypsA` at `DelWTopGA`** (any frame-word guard set): the delete engine keeps frame-word agreement. -/ +theorem delHypsA_topGA [PlainStore st] (hPl : ArchPlat ar) (P : DelPlatA st ar AR Clock) (s0 : S) + (F : DelFootA st Z H.tcbs H.omega H.frame OS s0) + (hT : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s0)) + (hYAs : ∀ y ∈ Z.Y, ∀ j : U64, j.val < 512 → ¬ AsW (L.ksb s0) (peAF y j)) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (hV : DelVcpuHookGA st Ag ar AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m hPl.ipa Clock s0) + (S4 : DelS4A st ar AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m hPl.ipa Clock s0) : + DelHypsA st ar H.tcbs Z.th OS Fl4SpecA64.fl4FinaliseCap + (DelWTopGA st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar hPl.ipa Clock s0) + (DelWxTopGA st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m Clock s0) + (DelWTopGA st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar hPl.ipa Clock s0) + (DelRdXA st ar Z H OS hPl.ipa (RvVcA st OS fun _ _ _ _ _ => True)) (DelRzA st) := by + have hbf : DelBufOffA st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n (J5A st) (J7LA st (J7R2A st ar hPl.ipa)) := by + intro Q R hw G Ac' Bf' stack' m' s a σ J b hb p hp i hi + have e := wm8_valA hi + exact J.bufOff.page b hb p hp (core.num.U64.wrapping_mul i 8#u64) (by omega) (by omega) + have fin : ∀ k, FinHookKA st k ar Z.th OS fl4FinaliseCap + (DelWTopGA st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar hPl.ipa Clock s0) + (DelWxTopGA st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m Clock s0) + (DelRdXA st ar Z H OS hPl.ipa (RvVcA st OS fun _ _ _ _ _ => True)) (DelRzA st) := by + intro k + cases k + case frame => exact finHookFrameSvA P.total hPl P.keeps P.del P.s2Ipa s0 hT hbf _ + case pageTable => exact finHookPtSvA P.total hPl P.keeps P.del P.s2Ipa s0 hT hbf _ + case pml4 => exact finHookPml4SvA P.total hPl P.keeps P.del s0 hT hbf _ + case highVSpace => exact finHookHighSvA P.total hPl P.keeps P.del s0 hT hbf _ + case ept => exact finHookEptSvA P.total hPl P.keeps P.del s0 hT hbf _ + case asidPool => exact finHookPoolSvA P.total hPl P.keeps P.del s0 hT hYAs _ + case endpoint => exact finHookEpSvA P.del s0 _ hCs P.minBudget P.maxTicks P.ticksUs P.numNodes1 + case notification => exact finHookNtfnSvA P.del s0 _ hCs P.minBudget P.maxTicks P.ticksUs P.numNodes1 + case reply => exact finHookReplySvA P.del s0 P.total P.numNodes1 _ hCs P.minBudget P.maxTicks P.ticksUs + case irqHandler => exact finHookIrqSvA P.del s0 _ F hCs P.minBudget P.maxTicks P.ticksUs P.numNodes1 + case thread => + exact finHookThreadSvA (finHookThreadA s0 _ P.total hCs P.numNodes1 P.minBudget P.maxTicks P.ticksUs P.del + P.vcpuAgree P.vcpuKeeps P.vcpuVmRoot (fun J => vcpu_detach_stage3GA J) S4.comm S4.keepB) + case schedContext => exact finHookScSvA P.del s0 _ P.total hCs P.maxTicks P.ticksUs P.numNodes1 + case vcpu => exact hV + exact + { agree := finAgreeA_fl4 + fin := finHookA_of_kinds fin + triv := rdTrivHyp_topA + pp := delPp_topA (delPp_hookA ar P.numDomains1 P.now s0 P.counter P.rawNow P.arStamp P.irqPending P.minBudget) + post := postDel_topA (postDelHookA P.del s0) + zomb := zombieHyp_topA F + swap := swapHyp_topA F + empty := emptyHyp_topA F + wx := delWx_topA } + +/-! ### The deletion's entry-state facts, from the record (§97) -/ + +/-- The CDT tree over the `OS` slots: the record's tree over the typed slots, grown by null words. -/ +theorem JSyscallA.delTreeA {J7 : JSlotA S} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) : + TreeA st OS Z.G.chains Z.G.par s := + J.j.1.cdt.tree.grow J.j5.2.1 fun y hy hn => J.j5.2.2 y hy hn + +/-- The deletion's decode-site record (`DelSiteA`). -/ +theorem JSyscallA.delSiteA {J7 : JSlotA S} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) : + DelSiteA st Z.th OS s σ Z.G.chains := + ⟨J.finCov, J.frBase, ⟨J.j5.1.2.1.one, J.j5.1.2.1.chunk⟩, J.zwf⟩ + +/-- Every `OS` slot is a canonical address. -/ +theorem JSyscallA.delCanA {J7 : JSlotA S} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) : + ∀ x ∈ OS, obj.sext48 x = ok x := + fun _ hx => OSOK.canon J.sys.ent.os J.toJStage3A.winOK hx + +/-- **The 128 `bufW w k` words** (corollary): if the guard set `Ag` holds them aligned, every live one of them agrees +between the store and the spec's user memory at any state of the delete loop's record (`Std6GA.frAg`). -/ +theorem FrAgA.bufW_agreeA {OS' Ag' : Set U64} {s' : S} {σ' : Fl4SpecA64.State Unit} {w : U64} + (h : FrAgA st OS' Ag' s' σ') + (hb : ∀ k : U64, k.val < 128 → bufW w k ∈ Ag' ∧ Aligned (bufW w k)) {k : U64} (hk : k.val < 128) + (lv : FrameWordLive st OS' s' (bufW w k)) : + (L.mem s' (bufW w k)).bv = loadWordAt σ'.machineState.underlyingMemory (specPtr (bufW w k)) := + h _ (hb k hk).1 (hb k hk).2 lv + +end frag + +/-- **`DelHypsA` at `DelWTopA`** (module docstring). -/ +theorem delHypsA_topA [PlainStore st] (hPl : ArchPlat ar) (P : DelPlatA st ar AR Clock) (s0 : S) + (F : DelFootA st Z H.tcbs H.omega H.frame OS s0) + (hT : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s0)) + (hYAs : ∀ y ∈ Z.Y, ∀ j : U64, j.val < 512 → ¬ AsW (L.ksb s0) (peAF y j)) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (S4 : DelS4A st ar AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m hPl.ipa Clock s0) : + DelHypsA st ar H.tcbs Z.th OS Fl4SpecA64.fl4FinaliseCap + (DelWTopA st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar hPl.ipa Clock s0) + (DelWxTopA st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m Clock s0) + (DelWTopA st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar hPl.ipa Clock s0) + (DelRdXA st ar Z H OS hPl.ipa (RvVcA st OS fun _ _ _ _ _ => True)) (DelRzA st) := + delHypsA_topGA hPl P s0 F hT hYAs hCs S4.vcpuHook S4 + +/-- **`DelWTopA` at the caller's restarted state** (the twin tops' `hXr`). -/ +theorem JSyscallA.delRestartWA {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + {slot : U64} (hsl : slot ∈ OS) (hcov : SlotCov st OS s slot) + {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hCl : Clock a σ.machineState) (hCnt : CounterStepA σ) + (x : Fl4SpecA64.Tcb) (hkx : σ.kheap σ.curThread = some (.tcb x)) : + DelWTopA st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar hPl.ipa Clock s + (L.wr s (stA (L.curW s)) sched.TS_RESTART) a (stUpdA σ σ.curThread x .restart) Z.G := by + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hsl hcov J.actW J.running J.cur + have hT : TreeA st OS Z.G.chains Z.G.par s := J.delTreeA + have hPar : ParentAdjA st Z.G.chains Z.G.par s := J.j5.1.1.parentAdj + have hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM := J.toJStage3EntryA.curNotVmA + obtain ⟨x', hkx', -, -, J', -, hOK, -⟩ := J.toJStage3A.of_restartCurA (J.toJStage3A.sysPlace hext hsl hcov) + RS.sym RS.run hnvm RS.cur RS.vs RS.vc + obtain rfl : x' = x := by + rw [hkx] at hkx' + cases hkx' + rfl + have noSw : ∀ u, (stUpdA σ σ.curThread x' .restart).schedulerAction ≠ .switchThread u := fun u h => by + have h' : σ.schedulerAction = .switchThread u := h + rw [J.act] at h' + cases h' + have sact : SActA (stUpdA σ σ.curThread x' .restart) := + { f1 := fun u h => absurd h (noSw u) + f2 := ⟨{ x' with tcbState := .restart }, by simp [stUpdA, setObjA], Or.inr (Or.inl rfl)⟩ } + have JT := J'.toTA sact noSw + have JL : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7LA st (J7R2A st ar hPl.ipa)) + (L.wr s (stA (L.curW s)) sched.TS_RESTART) a (stUpdA σ σ.curThread x' .restart) m := + { JT with j7 := J7A.toLA JT.j7 } + have fx : DelCapFxA st Z H (L.wr s (stA (L.curW s)) sched.TS_RESTART) := + { fhSlot := fun t ht => isSlot_fhA ((J'.sr.thT t).2 ht) + fhOK := J'.fhOK + ptCapIn := J'.ptCapIn } + have X : DelXA6 st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st (J7R2A st ar hPl.ipa)) + (L.wr s (stA (L.curW s)) sched.TS_RESTART) a (stUpdA σ σ.curThread x' .restart) Z.G := + ⟨⟨Z.Q, Z.R, Z.hw, Ac, Bf, stack, JL, Std6A.ofJA J'⟩, ParentAdjA.keepOS hT.lk hOK hPar, J'.zombieNoVcpu, + J'.sys.sysl.bh, J'.sys.sysl.ct, fx⟩ + have V : ValidMachineTimeA (stUpdA σ σ.curThread x' .restart) := J.validTime + have CE : CurEntryGA (stUpdA σ σ.curThread x' .restart) := + (CurEntryGA.of_cur J.toJStage3EntryA.curInv J.toJStage3EntryA.offReady J.sr.inv).keepA + (tcbW_tmA (x' := { x' with tcbState := .restart }) hkx' (fun _ h => h)).2.2.2 (BitVec.le_refl _) + exact ⟨⟨⟨L.ksb_wr _ _ _, by rw [LawfulStore.cpu_wr]⟩, X, J'.schedValid⟩, V, hCl, hCnt, CE⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3DelAsmTopA.lean b/proofs/Fl4ProofsA64/P3/Stage3DelAsmTopA.lean new file mode 100644 index 000000000..03b062637 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3DelAsmTopA.lean @@ -0,0 +1,382 @@ +/- +P3 (AArch64, delete hooks, contract §96; A64-P3 DelBodyA slice 1): **the twin tops** of CNode Delete / Revoke at a +syscall entry, with the S4 VCPU finaliser hook discharged (`Vcpu.vcpuHookA`, through `DelS4A.ofHookA`), and the body +facts carried through the loop. + +* **`JSyscallA.delHypsBaseA`**: `DelHypsA` at a syscall entry's own state (no body facts). +* **`JSyscallA.delHypsTopA`**: the same hooks at `DelWBodyA (DelWTopA ..)` / `DelWBodyA (DelWxTopA ..)` / + `DelWBodyPA (DelWTopA ..)`. +* **`JSyscallA.cnodeDeleteAsmA`** / **`JSyscallA.cnodeRevokeAsmA`**: the twin tops from the syscall record, the + platform premises and the run's slot / decode facts, concluding `CnodeDelBodyOutA` (the body-end facts by result). + +Named premises: `DelPlatA` ([P]; `numNodes1` / `numDomains1` [S]), `ArchPlat` [P], `hCl` [P] (the clock relation at +entry), `hCs` [S]; no [W] remains (slice 2 proved the SchedContext and Thread finaliser frames, +`Stage3DelFinScA` / `Stage3DelFinThrA`); run facts: the invoked slot and extra slots (`hsl`, +`hcov`, `hext`, `hSC`) and the decode data. The entry-state facts (tree, slot set, `DelSiteA`, canonical slots, VSpace +/ VCPU words apart from the caller's state) come from the record. `PlainStore` (the Frame hook). New module. +-/ +import Fl4ProofsA64.P3.VcpuDelFinHookA +import Fl4ProofsA64.P3.Stage3DelBodyLoopA +import Fl4ProofsA64.P3.Stage3DelFinReplyA +import Fl4ProofsA64.P3.Stage3DelFinChA +import Fl4ProofsA64.P3.Stage3DelFinIrqA +import Fl4ProofsA64.P3.Stage3DelFinThrA +import Fl4ProofsA64.P3.Stage3LiftIrqClearA +import Fl4ProofsA64.P3.Stage3LiftBodyA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Vcpu Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {m : Fl4SpecA64.McState} +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {Clock : A → Fl4SpecA64.MachineState → Prop} {Ag : Set U64} + +/-- **`DelHypsA` at a syscall entry's own state** (no body facts). -/ +theorem JSyscallA.delHypsBaseA [PlainStore st] {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (P : DelPlatA st ar AR Clock) (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) : + DelHypsA st ar H.tcbs Z.th OS Fl4SpecA64.fl4FinaliseCap + (DelWTopGA st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar hPl.ipa Clock s) + (DelWxTopGA st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m Clock s) + (DelWTopGA st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar hPl.ipa Clock s) + (DelRdXA st ar Z H OS hPl.ipa (RvVcA st OS fun _ _ _ _ _ => True)) (DelRzA st) := by + have hY : PtHA H Z.Y := by + intro y hy + obtain ⟨q', z, hq', h1, h2⟩ := J.setsInH.roots y (Or.inr hy) + obtain ⟨g1, g2, -, -⟩ := root_pageA J.sys.ent.heap hq' h1 h2 (Or.inl rfl) + exact ⟨q', z, hq', g1, g2⟩ + exact delHypsA_topGA hPl P s (DelFootA.of_entA J.toJEntA) J.toJStage3A.offAsE + (fun y hy j hj => ptHA_not_asW J.sys.ent.heap J.ko J.sys.ent.ksw hY hy hj) hCs + (Vcpu.vcpuHookA s P hCs) (DelS4A.ofHookA (Vcpu.vcpuHookA s P hCs)) + +/-! ## The body facts -/ + +/-- The record facts a deletion's hooks read of the spec state. -/ +def DelPrA (m : Fl4SpecA64.McState) (n : Nat) (σ : Fl4SpecA64.State Unit) : Prop := + SInvXA σ m n ∧ SActA σ ∧ SchedValidA σ + +theorem DelPrA.curSc {m : Fl4SpecA64.McState} {n : Nat} {σ : Fl4SpecA64.State Unit} (h : DelPrA m n σ) : + CurScA σ := h.1.curSc + +theorem DelPrA.notRunVm {m : Fl4SpecA64.McState} {n : Nat} {σ : Fl4SpecA64.State Unit} (h : DelPrA m n σ) : + NotRunVmA σ := fun x hx hs => by + obtain ⟨y, hy, hc⟩ := h.2.1.f2 + rw [hx] at hy + obtain rfl := Fl4SpecA64.KernelObject.tcb.inj (Option.some.inj hy) + rw [hs] at hc + rcases hc with h | h | h | h <;> cases h + +theorem delW_prA {s0 : S} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {G : MdbGhostA} {ipa : U64} + (h : DelWTopGA st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar ipa Clock s0 s a σ G) : + DelPrA m n σ ∧ DelPpTimeA Clock a σ := by + obtain ⟨⟨-, ⟨⟨Q, R, hw, Ac', Bf', stack', J, -⟩, -⟩, sv⟩, T⟩ := h + exact ⟨⟨J.sinv, J.sact, sv⟩, T⟩ + +theorem delWx_prA {s0 : S} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {G : MdbGhostA} + (h : DelWxTopGA st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m Clock s0 s a σ G) : DelPrA m n σ := by + obtain ⟨⟨-, ⟨⟨Q, R, hw, Ac', Bf', stack', J, -⟩, -⟩, sv⟩, -⟩ := h + exact ⟨J.sinv, J.sact, sv⟩ + +/-- The endpoint finaliser's side condition, from the loop's record. -/ +theorem delW_epPrA {s0 : S} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {G : MdbGhostA} {ipa : U64} + {sl : Fl4SpecA64.CslotPtr} (D : DStateA st Z.th OS s σ G) + (h : DelWTopGA st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar ipa Clock s0 s a σ G) (hsl : AbsSlotA Z.th sl) : + EpPrA σ (capA st s (cslotAddrA sl)) := by + refine ⟨(delW_prA h).1.curSc, fun r b rs hc => ?_⟩ + obtain ⟨⟨-, ⟨⟨Q, R, hw, Ac', Bf', stack', J, -⟩, -⟩, -⟩, -⟩ := h + have hxO : cslotAddrA sl ∈ OS := D.mem_slot hsl + obtain ⟨p, b0, b1, b2, b3, b4, hcp, er⟩ : ∃ p b0 b1 b2 b3 b4, + capA st s (cslotAddrA sl) = .Endpoint p b0 b1 b2 b3 b4 ∧ r = specPtr p := by + generalize capA st s (cslotAddrA sl) = c at hc ⊢ + cases c <;> simp [absCapA] at hc + exact ⟨_, _, _, _, _, _, rfl, hc.1.symm⟩ + subst er + have he := J.ipcHeapCov.ep p ((J.capTy _ hxO).1 p b0 b1 b2 b3 b4 hcp) + exact ⟨epQNe_ofRecA J.ep J.sact he, epQBlk_ofRecA J.ep he⟩ + +/-- The notification finaliser's side condition, from the loop's record. -/ +theorem delW_ntfnPrA {s0 : S} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {G : MdbGhostA} {ipa : U64} + {sl : Fl4SpecA64.CslotPtr} (D : DStateA st Z.th OS s σ G) + (h : DelWTopGA st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar ipa Clock s0 s a σ G) (hsl : AbsSlotA Z.th sl) : + NtfnPrA σ (capA st s (cslotAddrA sl)) := by + refine ⟨(delW_prA h).1.curSc, fun r b rs hc => ?_⟩ + obtain ⟨⟨-, ⟨⟨Q, R, hw, Ac', Bf', stack', J, -⟩, -⟩, -⟩, -⟩ := h + have hxO : cslotAddrA sl ∈ OS := D.mem_slot hsl + obtain ⟨p, b0, b1, b2, hcp, er⟩ : ∃ p b0 b1 b2, + capA st s (cslotAddrA sl) = .Notification p b0 b1 b2 ∧ r = specPtr p := by + generalize capA st s (cslotAddrA sl) = c at hc ⊢ + cases c <;> simp [absCapA] at hc + exact ⟨_, _, _, _, rfl, hc.1.symm⟩ + subst er + have hn := J.ipcHeapCov.ntfn p ((J.capTy _ hxO).2.1 p b0 b1 b2 hcp) + exact ⟨ntfnQNe_ofRecA J.ntfn J.sact hn, fun y q hk hq t hm => (ntfnQueue_blockedA J.ntfn hn hk q hq).2 t hm⟩ + +/-- The IRQ handler hook's side condition, from the loop's record. -/ +theorem delW_irqPrA {s0 : S} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {G : MdbGhostA} {ipa : U64} + {sl : Fl4SpecA64.CslotPtr} (D : DStateA st Z.th OS s σ G) + (h : DelWTopGA st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar ipa Clock s0 s a σ G) (hsl : AbsSlotA Z.th sl) : + IrqPrA σ (capA st s (cslotAddrA sl)) := by + refine ⟨(delW_prA h).1.curSc, fun irq hirq => ?_⟩ + obtain ⟨⟨-, ⟨⟨Q, R, hw, Ac', Bf', stack', J, -⟩, -⟩, -⟩, -⟩ := h + have wf := D.wf (D.mem_slot hsl) + rw [hirq] at wf + have hi : irq.val < 5376 := by + simpa [CapWfA, Fl4SpecA64.fl4IrqCount] using wf + obtain ⟨ac0, hg, hac⟩ := irqSlot_ntfnOrNullA J D hi + exact ⟨ac0, hg, hac, irqSlot_qneBA J D hi hg⟩ + +/-- The SchedContext finaliser's side condition, from the loop's record: the cap names no idle SC. -/ +theorem delW_scPrA {s0 : S} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {G : MdbGhostA} {ipa : U64} + {sl : Fl4SpecA64.CslotPtr} (D : DStateA st Z.th OS s σ G) + (h : DelWTopGA st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar ipa Clock s0 s a σ G) (hsl : AbsSlotA Z.th sl) : + ScPrA σ (capA st s (cslotAddrA sl)) := by + refine ⟨(delW_prA h).1.curSc, fun p b hc => ?_⟩ + obtain ⟨⟨-, ⟨⟨Q, R, hw, Ac', Bf', stack', J, -⟩, -⟩, -⟩, -⟩ := h + have hOSx : cslotAddrA sl ∈ OS := D.mem_slot hsl + have hH : H p = some (.sc b.val) := (J.capTy _ hOSx).2.2.2.1 p b hc + have hni := J.ciSc _ hOSx p b hc + have hp : p ∈ Z.C := by + rw [← J.scs] + first + | exact ⟨_, hH⟩ + | (change H p ∈ _; rw [hH]; simp) + | simp [Heap.scs, hH] + exact J.sr.sch.scOff p hp hni + +/-- **`DelHypsA` with the body facts** (module docstring). -/ +theorem JSyscallA.delHypsTopA [PlainStore st] {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (P : DelPlatA st ar AR Clock) (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + : + DelHypsA st ar H.tcbs Z.th OS Fl4SpecA64.fl4FinaliseCap + (DelWBodyA (DelWTopGA st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar hPl.ipa Clock s) σ.curThread m) + (DelWBodyA (DelWxTopGA st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m Clock s) σ.curThread m) + (DelWBodyPA (DelWTopGA st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar hPl.ipa Clock s) σ.curThread m) + (DelRdXA st ar Z H OS hPl.ipa (RvVcA st OS fun _ _ _ _ _ => True)) (DelRzA st) := by + have H0 := J.delHypsBaseA (Ag := Ag) hPl P hCs + have step : ∀ {σ0 σ1 : Fl4SpecA64.State Unit}, DelBodyInvA σ.curThread m σ0 ∧ DelOwnA σ0 → DelStepCA σ0 σ1 → + DelBodyInvA σ.curThread m σ1 ∧ DelOwnA σ1 := fun h k => ⟨BodyOKA.stepA h.1 k, h.2.stepA k⟩ + have sk : ∀ {σ0 σ1 : Fl4SpecA64.State Unit}, CurScA σ0 → DSkA σ0 σ1 → DelStepCA σ0 σ1 := + fun h k => DelStepA.ofWk h k.toDWkA (Or.inl k) k.ch + refine + { agree := H0.agree + fin := finHookA_of_kinds fun k => ?_ + triv := fun s1 a1 σ1 G sl c D hW => H0.triv s1 a1 σ1 G sl c D hW.1 + pp := ?_ + post := ?_ + zomb := ?_ + swap := ?_ + empty := ?_ + wx := fun s1 a1 σ1 G hW => ⟨H0.wx s1 a1 σ1 G hW.1, hW.2⟩ } + · have H1 := H0.fin.kind k + cases k + case endpoint => + exact H1.bodyCapA finBodySpec_epA fun _ _ _ _ _ D h hsl => delW_epPrA D h hsl + case notification => + exact H1.bodyCapA finBodySpec_ntfnA fun _ _ _ _ _ D h hsl => delW_ntfnPrA D h hsl + case reply => + exact H1.bodyA finBodySpec_replyA fun _ _ _ _ h => + ⟨(delW_prA h).1.curSc, (delW_prA h).1.1.rlsRun, (delW_prA h).1.2.1⟩ + case irqHandler => + exact H1.bodyCapA finBodySpec_irqA fun _ _ _ _ _ D h hsl => delW_irqPrA D h hsl + case vcpu => + exact H1.bodyA finBodySpec_vcpuA fun _ _ _ _ h => (delW_prA h).1.notRunVm + case thread => + exact H1.bodyA finBodySpec_threadA fun _ _ _ _ h => ⟨(delW_prA h).1.curSc, (delW_prA h).1.notRunVm⟩ + case schedContext => exact H1.bodyCapA finBodySpec_scA fun _ _ _ _ _ D h hsl => delW_scPrA D h hsl + all_goals + exact H1.bodyA (Pr := fun _ => True) (finBodySpec_vsA (by simp)) fun _ _ _ _ _ => trivial + · intro s1 a1 σ1 G D hW + refine WP.spec_mono (H0.pp s1 a1 σ1 G D hW.1) ?_ + rintro r ⟨hc, σ2, D', hr⟩ + refine ⟨hc, σ2, D', ?_⟩ + have T := (delW_prA hW.1).2 + have mono : σ1.curTime ≤ rawNextA σ1.machineState := BitVec.le_trans T.1 T.2.2.1 + rcases hr with ⟨e, hm, hW'⟩ | ⟨e, hm, hW'⟩ + · refine Or.inl ⟨e, hm, hW', ?_⟩ + rcases preemptionPoint_casesA P.numDomains1 (P.rawNow σ1) hm with ⟨-, rfl⟩ | ⟨t, F⟩ + · exact hW.2 + · have own := hW.2.2.tickA t + exact ⟨ppBody_okA hW.2.1 t mono P.minAgree (F rfl) (delW_prA hW').2.1 own, own⟩ + · refine Or.inr ⟨e, hm, hW', ?_⟩ + rcases preemptionPoint_casesA P.numDomains1 (P.rawNow σ1) hm with ⟨c, -⟩ | ⟨t, -⟩ + · cases c + · exact ppBody_preA hW.2.1 t mono (delW_prA hW').2.1 (hW.2.2.tickA t) + · intro s1 a1 σ1 G cl D hW hcl hne hwf hno + refine WP.spec_mono (H0.post s1 a1 σ1 G cl D hW.1 hcl hne hwf hno) ?_ + rintro r ⟨hc, σ2, hm, D', hW'⟩ + obtain ⟨i, rfl⟩ : ∃ i, cl = .IrqHandler i := by + cases cl <;> simp [CleanCapA] at hcl hne + exact ⟨_, rfl⟩ + exact ⟨hc, σ2, hm, D', hW', step hW.2 (postCapIrq_dlA _ σ1 _ hm).step⟩ + · intro s1 s2 a1 σ1 σ2 G sl p zt k hW hRz hZ hNZ D D' w HS hset hA hne hz hcap M + refine ⟨H0.zomb s1 s2 a1 σ1 σ2 G sl p zt k hW.1 hRz hZ hNZ D D' w HS hset hA hne hz hcap M, step hW.2 ?_⟩ + refine sk (delW_prA hW.1).1.curSc (setCap_skA (absCapA (.Zombie p zt k)) sl σ1 ((), σ2) ?_) + rw [hset]; exact rfl + · intro s1 s2 a1 σ1 σ2 G sl p zt k hW h1 D D' w HS hm hA hz hne hcap M + exact ⟨H0.swap s1 s2 a1 σ1 σ2 G sl p zt k hW.1 h1 D D' w HS hm hA hz hne hcap M, + step hW.2 (sk (delW_prA hW.1).1.curSc (capSwapForDelete_skA _ _ σ1 _ hm))⟩ + · intro s1 s2 a1 σ1 σ2 G sl hW hRd D D' w HS hm hA hne h0 hcap M + exact ⟨H0.empty s1 s2 a1 σ1 σ2 G sl hW.1 hRd D D' w HS hm hA hne h0 hcap M, + step hW.2 (sk (delWx_prA hW.1).curSc (emptySlot_skA _ σ1 _ hm))⟩ + +/-- **The body invariant at the caller's restarted state** (the tops' `hXr`). -/ +theorem JSyscallA.delRestartBodyA {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + {J5 J7 : JSlotA S} + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (x : Fl4SpecA64.Tcb) (hkx : σ.kheap σ.curThread = some (.tcb x)) : + DelBodyInvA σ.curThread m (stUpdA σ σ.curThread x .restart) ∧ DelOwnA (stUpdA σ σ.curThread x .restart) := by + have B := (BodyOKA.ofRestart J hkx rfl).1 + have BB : DelBudA (stUpdA σ σ.curThread x .restart) := by + obtain ⟨z, hz, hzs, -⟩ := J.curInv.tcb + obtain ⟨w, j, hw, -⟩ := J.sr.inv.back _ z _ hz hzs + have ne : σ.curSc ≠ σ.curThread := fun e => by rw [e, hz] at hw; cases hw + have eK : (stUpdA σ σ.curThread x .restart).kheap σ.curSc = σ.kheap σ.curSc := by + change Function.update σ.kheap σ.curThread _ σ.curSc = _ + rw [Function.update_of_ne ne] + intro _ _ _ _ + refine ⟨fun y k hy => J.offReady y k ?_, fun hpos y k hy ha => J.offOK hpos y k ?_ ha⟩ + · change (stUpdA σ σ.curThread x .restart).kheap σ.curSc = _ at hy + rw [eK] at hy; exact hy + · change (stUpdA σ σ.curThread x .restart).kheap σ.curSc = _ at hy + rw [eK] at hy; exact hy + have PS : DelPreSchedA σ.curThread (stUpdA σ σ.curThread x .restart) := by + refine ⟨fun d p => J.toJStage3A.sinv.ctQ J.act d p, fun x' hx' => ?_, J.curInv.notRel, fun _ x' hx' => ?_⟩ + swap + · change Function.update σ.kheap σ.curThread _ σ.curThread = _ at hx' + rw [Function.update_self] at hx' + obtain rfl := Fl4SpecA64.KernelObject.tcb.inj (Option.some.inj hx') + rfl + obtain ⟨t, hkt, hsc, -⟩ := J.curInv.tcb + change Function.update σ.kheap σ.curThread _ σ.curThread = _ at hx' + rw [Function.update_self] at hx' + obtain rfl := Fl4SpecA64.KernelObject.tcb.inj (Option.some.inj hx') + rw [hkx] at hkt + obtain rfl := Fl4SpecA64.KernelObject.tcb.inj (Option.some.inj hkt) + exact Or.inr hsc + refine ⟨⟨B, rfl, fun u h => ?_, BB, PS⟩, fun y k hk _ _ o ho => ?_, ?_⟩ + · have h' : σ.schedulerAction = .switchThread u := h + exact J.noSwitch u h' + · obtain ⟨z, hz, hzs, -⟩ := J.curInv.tcb + obtain ⟨w, j, hw, hwt⟩ := J.sr.inv.back _ z _ hz hzs + have ne : σ.curSc ≠ σ.curThread := fun e => by rw [e, hz] at hw; cases hw + change Function.update σ.kheap σ.curThread _ σ.curSc = _ at hk + rw [Function.update_of_ne ne, hw] at hk + obtain ⟨rfl, -⟩ := Fl4SpecA64.KernelObject.schedContext.inj (Option.some.inj hk) + rw [hwt] at ho + rw [← Option.some.inj ho] + exact J.curInv.notRel + · intro y n o x2 hy ho hx + obtain ⟨z, hz, hzs, -⟩ := J.curInv.tcb + obtain ⟨w, j, hw, hwt⟩ := J.sr.inv.back _ z _ hz hzs + have ne : σ.curSc ≠ σ.curThread := fun e => by rw [e, hz] at hw; cases hw + change Function.update σ.kheap σ.curThread _ σ.curSc = _ at hy + rw [Function.update_of_ne ne, hw] at hy + obtain ⟨rfl, -⟩ := Fl4SpecA64.KernelObject.schedContext.inj (Option.some.inj hy) + rw [hwt] at ho + have eo : σ.curThread = o := Option.some.inj ho + rw [← eo] at hx + change Function.update σ.kheap σ.curThread _ σ.curThread = _ at hx + rw [Function.update_self] at hx + obtain rfl := Fl4SpecA64.KernelObject.tcb.inj (Option.some.inj hx) + exact Or.inl ⟨eo.symm, rfl⟩ + +/-- **From the deletion's post at the body invariants and `SysL` / `SymL` at the final state, the outcome**. -/ +theorem cnodeDelBodyOut_ofA {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {ipa : U64} {s0 : S} + {msp : Fl4SpecA64.SEMonad Unit Fl4SpecA64.CnodeInvocation} {r : core.result.Result Unit error.Exc} {s3 : S} + {a3 : A} (B : BodyOKA σ m) + (h : InvPostDA st AR Z OS σ msp s a + (DelWBodyA (DelWTopA st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar ipa Clock s0) σ.curThread m) + (DelWBodyPA (DelWTopA st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar ipa Clock s0) σ.curThread m) + (r, s3, a3)) + (sysl : SysL st H Z.Cs OS ia ie s3) (symL : SymL st H Z.Cs s3) : + CnodeDelBodyOutA st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar Clock ipa s a σ msp (r, s3, a3) := by + rcases h with ⟨e, hr, hs, ha, e', hrun, herr⟩ | ⟨inv, hinv, σ1, hset, σ', G', hres, -, hW⟩ + · exact Or.inl ⟨e, e', hr, hs, ha, hrun, herr, B, rfl⟩ + · have hW' : DelWTopA st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar ipa Clock s0 s3 a3 σ' G' ∧ + DelBodyEndA σ σ' m r := by + rcases hres with ⟨rfl, -⟩ | ⟨rfl, -⟩ + · exact ⟨hW.1, hW.2.1.1, hW.2.1.2.1⟩ + · exact ⟨hW.1, hW.2.1, hW.2.2⟩ + obtain ⟨⟨⟨-, X, sv⟩, tm⟩, hend⟩ := hW' + obtain ⟨Q, R, hw, Ac', Bf', stack', J'⟩ := X.toStage3A sysl symL sv + exact Or.inr ⟨inv, σ1, σ', G', hinv, hset, hres, Q, R, hw, Ac', Bf', stack', J', hend, tm.2.1⟩ + +set_option maxHeartbeats 4000000 in +/-- **CNode Delete at a syscall entry**, from the platform premises. -/ +theorem JSyscallA.cnodeDeleteAsmA [PlainStore st] (s : S) (a : A) (σ : Fl4SpecA64.State Unit) (m : Fl4SpecA64.McState) + (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hCnt : CounterStepA σ) + {slot : U64} (hsl : slot ∈ OS) (hcov : SlotCov st OS s slot) + {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (P : DelPlatA st ar AR Clock) (hCl : Clock a σ.machineState) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (lbl : U64) (slbl : Fl4SpecA64.Data) (hlbl : Fl4SpecA64.genInvocationType slbl = cnodeLabelOfA lbl) + (hl : lbl.val = 22) (length buf : U64) (args : List U64) (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) + (cap : capability.Cap) (ac : Fl4SpecA64.Cap) (hca : CapRelA cap ac) (hcv : CapValidA Z.th cap) + (hcap : capA st s slot = cap) (excaps : List Fl4SpecA64.Cap) + {r : core.result.Result Unit error.Exc} {s3 : S} {a3 : A} + (hdec : cnode.decode st ar s a lbl length cap buf extra = ok (r, s3, a3)) : + CnodeDelBodyOutA st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar Clock hPl.ipa s a σ + (Fl4SpecA64.decodeCnodeInvocation slbl (args.map UScalar.bv) ac excaps) (r, s3, a3) := by + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hsl hcov J.actW J.running J.cur + have hl' : lbl = fl4_abi.label.CNodeDelete := by rw [u64_eq_iffA]; simp [hl] + have sy := J.cnodeRvDelSysA P.total hPl hsl hcov hext hcap (Or.inr hl') hdec + have post := post_of_okA (cnode_delete_refinesWA ar J.toJEntA (J.toJStage3A.sysPlace hext hsl hcov) J.symL + J.running RS.cur RS.vs RS.vc J.delTreeA J.j5.2.1 J.j5.2.2 J.delSiteA J.delCanA + (J.delHypsTopA hPl P hCs) + (fun x hkx _ => ⟨J.delRestartWA hPl hsl hcov hext hCl hCnt x hkx, J.delRestartBodyA x hkx⟩) + lbl slbl hlbl hl length buf args hlen hargs cap ac hca hcv excaps) hdec + exact cnodeDelBodyOut_ofA J.bodyOKA post sy.1 sy.2 + +set_option maxHeartbeats 4000000 in +/-- **CNode Revoke at a syscall entry**, from the platform premises. -/ +theorem JSyscallA.cnodeRevokeAsmA [PlainStore st] (s : S) (a : A) (σ : Fl4SpecA64.State Unit) (m : Fl4SpecA64.McState) + (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hCnt : CounterStepA σ) + {slot : U64} (hsl : slot ∈ OS) (hcov : SlotCov st OS s slot) + {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (P : DelPlatA st ar AR Clock) (hCl : Clock a σ.machineState) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (lbl : U64) (slbl : Fl4SpecA64.Data) (hlbl : Fl4SpecA64.genInvocationType slbl = cnodeLabelOfA lbl) + (hl : lbl.val = 21) (length buf : U64) (args : List U64) (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) + (cap : capability.Cap) (ac : Fl4SpecA64.Cap) (hca : CapRelA cap ac) (hcv : CapValidA Z.th cap) + (hcap : capA st s slot = cap) (excaps : List Fl4SpecA64.Cap) + {r : core.result.Result Unit error.Exc} {s3 : S} {a3 : A} + (hdec : cnode.decode st ar s a lbl length cap buf extra = ok (r, s3, a3)) : + CnodeDelBodyOutA st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar Clock hPl.ipa s a σ + (Fl4SpecA64.decodeCnodeInvocation slbl (args.map UScalar.bv) ac excaps) (r, s3, a3) := by + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hsl hcov J.actW J.running J.cur + have hl' : lbl = fl4_abi.label.CNodeRevoke := by rw [u64_eq_iffA]; simp [hl] + have sy := J.cnodeRvDelSysA P.total hPl hsl hcov hext hcap (Or.inl hl') hdec + have post := post_of_okA (cnode_revoke_refinesWA ar J.toJEntA (J.toJStage3A.sysPlace hext hsl hcov) J.symL + J.running RS.cur RS.vs RS.vc J.delTreeA J.j5.2.1 J.j5.2.2 J.delSiteA J.delCanA + (J.delHypsTopA hPl P hCs) + (fun _ _ _ _ _ hW => hW.1.1.2.1.2.1) + (fun x hkx _ => ⟨J.delRestartWA hPl hsl hcov hext hCl hCnt x hkx, J.delRestartBodyA x hkx⟩) + lbl slbl hlbl hl length buf args hlen hargs cap ac hca hcv excaps) hdec + exact cnodeDelBodyOut_ofA J.bodyOKA post sy.1 sy.2 + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3DelBodyA.lean b/proofs/Fl4ProofsA64/P3/Stage3DelBodyA.lean new file mode 100644 index 000000000..fd47c535d --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3DelBodyA.lean @@ -0,0 +1,519 @@ +/- +P3 (aarch64, A64-P3 Lift 1b, Plan B merge): **the body-end facts across `deleting_irq_handler`** (the IRQ Handler +Clear and Set arms; the CNode Delete / Revoke arms need the same walk with their own finalisers). + +The IRQ slot holds a notification cap or nothing. Deleting it is `cap_delete_one`: for a final notification cap +`sched_context_maybe_unbind_ntfn`, `unbind_maybe_notification`, `cancel_all_signals`, then `empty_slot`. + +* `DWkA σ σ'`: the clock, the counter observation, the current thread and SC, the consumed time and the release queue + are kept, and the current SC keeps its refills, refill maximum and TCB (its notification link may go). +* `DSkA σ σ'`: `DWkA`, the scheduler action kept, every TCB state kept. +* `deletingIrqHandler_bodyA`: the deletion is an `DSkA` step, or a `DWkA` step ending in `choose_new_thread` + (`cancel_all_signals` with a waiter: its loop is `CKeepA`, `Stage3CancelBodyA`, and ends in `reschedule_required`). +* `BodyOKA.ofDel`: the body-end facts after `Restart` and either outcome, from the syscall-entry record. + +Named premises: none. + +New module. +-/ +import Fl4ProofsA64.P3.Stage3CancelBodyA +import Fl4ProofsA64.P3.Stage3LiftBodyA +import Fl4ProofsA64.P3.DelSinvLoopA +import Fl4ProofsA64.P3.ReplyRemoveInvA +import Fl4ProofsA64.P3.Stage3DelSchedValidA +import Fl4ProofsA64.P3.Stage3VsZeroA +import Fl4ProofsA64.P3.Stage3DelSvIpcA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Classical + +noncomputable section + +/-- The current SC's refill data are kept (its notification link may change). -/ +def DScWkA (σ σ' : State Unit) : Prop := + ∀ y k, σ.kheap σ.curSc = some (.schedContext y k) → ∃ y', σ'.kheap σ.curSc = some (.schedContext y' k) ∧ + y'.scRefills = y.scRefills ∧ y'.scRefillMax = y.scRefillMax ∧ y'.scTcb = y.scTcb + +/-- **The weak delete frame** (module docstring). -/ +structure DWkA (σ σ' : State Unit) : Prop where + time : σ'.curTime = σ.curTime + last : σ'.machineState.lastMachineTime = σ.machineState.lastMachineTime + cur : σ'.curThread = σ.curThread + csc : σ'.curSc = σ.curSc + cons : σ'.consumedTime = σ.consumedTime + rel : σ'.releaseQueue = σ.releaseQueue + sc : DScWkA σ σ' + off : CtOffA σ.curThread σ → CtOffA σ.curThread σ' + ctSc : CtScKA σ.curThread σ σ' + +instance : PreO DWkA where + refl σ := ⟨rfl, rfl, rfl, rfl, rfl, rfl, fun y k h => ⟨y, h, rfl, rfl, rfl⟩, id, CtScKA.refl _ _⟩ + trans {a b c} h1 h2 := ⟨h2.time.trans h1.time, h2.last.trans h1.last, h2.cur.trans h1.cur, h2.csc.trans h1.csc, + h2.cons.trans h1.cons, h2.rel.trans h1.rel, fun y k hy => by + obtain ⟨y1, h1', e1, e2, e3⟩ := h1.sc y k hy + obtain ⟨y2, h2', f1, f2, f3⟩ := h2.sc y1 k (by rw [h1.csc]; exact h1') + rw [h1.csc] at h2' + exact ⟨y2, h2', f1.trans e1, f2.trans e2, f3.trans e3⟩, CtOffA.trans h1.cur h1.off h2.off, + h1.ctSc.trans (h1.cur ▸ h2.ctSc)⟩ + +theorem CKeepA.toWk {σ σ' : State Unit} (h : CKeepA σ σ') : DWkA σ σ' := + ⟨h.time, by rw [h.ms], h.cur, h.csc, h.cons, h.rel, fun y k hy => ⟨y, h.sc y k hy, rfl, rfl, rfl⟩, h.off, h.ctSc⟩ + +/-- **The strong delete frame** (module docstring). -/ +structure DSkB (σ σ' : State Unit) : Prop extends DWkA σ σ' where + act : σ'.schedulerAction = σ.schedulerAction + tcb : ∀ p x', σ'.kheap p = some (.tcb x') → ∃ x, σ.kheap p = some (.tcb x) ∧ x'.tcbState = x.tcbState + +instance : PreO DSkB where + refl σ := ⟨PreO.refl (R := DWkA) σ, rfl, fun _ x h => ⟨x, h, rfl⟩⟩ + trans {a b c} h1 h2 := ⟨PreO.trans (R := DWkA) h1.toDWkA h2.toDWkA, h2.act.trans h1.act, fun p x h => by + obtain ⟨y, hy, e⟩ := h2.tcb p x h + obtain ⟨z, hz, f⟩ := h1.tcb p y hy + exact ⟨z, hz, e.trans f⟩⟩ + +/-- (chg) **The strong delete frame**: `DSkB`, every TCB keeps its fault, and an SC at the current SC pointer after +the step was one before with the same bound thread; so `ChKA` holds of it. -/ +structure DSkA (σ σ' : State Unit) : Prop extends DSkB σ σ' where + flt : ∀ p x', σ'.kheap p = some (.tcb x') → ∃ x, σ.kheap p = some (.tcb x) ∧ x'.tcbFault = x.tcbFault + scb : ∀ y' k, σ'.kheap σ.curSc = some (.schedContext y' k) → ∃ y, σ.kheap σ.curSc = some (.schedContext y k) ∧ + y'.scTcb = y.scTcb + +instance : PreO DSkA where + refl σ := ⟨PreO.refl (R := DSkB) σ, fun _ x h => ⟨x, h, rfl⟩, fun y k h => ⟨y, h, rfl⟩⟩ + trans {a b c} h1 h2 := ⟨PreO.trans (R := DSkB) h1.toDSkB h2.toDSkB, fun p x h => by + obtain ⟨y, hy, e⟩ := h2.flt p x h + obtain ⟨z, hz, f⟩ := h1.flt p y hy + exact ⟨z, hz, e.trans f⟩, fun y' k h => by + obtain ⟨y1, h1', e1⟩ := h2.scb y' k (by rw [h1.csc]; exact h) + rw [h1.csc] at h1' + obtain ⟨y0, h0, e0⟩ := h1.scb y1 k h1' + exact ⟨y0, h0, e1.trans e0⟩⟩ + +/-- (chg) A strong delete step keeps the current SC chargeable. -/ +theorem DSkA.ch {σ σ' : State Unit} (h : DSkA σ σ') : ChKA σ σ' := by + refine ⟨h.cur, h.csc, fun p x' hx' => ?_, fun y' n o hy ho => ?_⟩ + · obtain ⟨x, hx, es⟩ := h.tcb p x' hx' + obtain ⟨x2, hx2, ef⟩ := h.flt p x' hx' + rw [hx] at hx2 + obtain rfl := KernelObject.tcb.inj (Option.some.inj hx2) + refine ⟨x, hx, fun c => ?_, fun q => ?_⟩ + · rw [es, ef] at c; exact c + · rw [es] at q; exact q + · obtain ⟨y, hy0, e⟩ := h.scb y' n hy + exact Or.inl ⟨y, n, hy0, by rw [← e]; exact ho⟩ + +theorem DSkA.of_kheap {σ σ' : State Unit} + (hc : σ'.curTime = σ.curTime ∧ σ'.machineState.lastMachineTime = σ.machineState.lastMachineTime ∧ + σ'.curThread = σ.curThread ∧ σ'.curSc = σ.curSc ∧ σ'.consumedTime = σ.consumedTime ∧ + σ'.releaseQueue = σ.releaseQueue) + (hk : σ'.kheap = σ.kheap) (ha : σ'.schedulerAction = σ.schedulerAction) + (hr : σ'.readyQueues = σ.readyQueues) : DSkA σ σ' := + ⟨⟨⟨hc.1, hc.2.1, hc.2.2.1, hc.2.2.2.1, hc.2.2.2.2.1, hc.2.2.2.2.2, fun y k hy => ⟨y, by rw [hk]; exact hy, rfl, rfl, rfl⟩, + CtOffA.keep hr (Or.inl ha), CtScKA.ofEq hk⟩, + ha, fun p x h => ⟨x, by rw [← hk]; exact h, rfl⟩⟩, + fun p x h => ⟨x, by rw [← hk]; exact h, rfl⟩, + fun y k hy => ⟨y, by rw [← hk]; exact hy, rfl⟩⟩ + +/-- One object replaced: neither the old nor the new one is an SC, and a new TCB has an old TCB's state and fault. -/ +theorem DSkA.upd {σ : State Unit} {p : ObjRef} {o o' : KernelObject} (h1 : σ.kheap p = some o) + (hn : ∀ y k, o' ≠ .schedContext y k) (ho : ∀ y k, o ≠ .schedContext y k) + (ht : ∀ x', o' = .tcb x' → ∃ x, o = .tcb x ∧ x'.tcbState = x.tcbState ∧ + (x'.tcbSchedContext = x.tcbSchedContext ∨ x'.tcbSchedContext = none) ∧ x'.tcbFault = x.tcbFault) : + DSkA σ { σ with kheap := Function.update σ.kheap p (some o') } := by + refine ⟨⟨⟨rfl, rfl, rfl, rfl, rfl, rfl, fun y k hy => ?_, id, CtScKA.ofUpd (o' := o') (p := p) rfl (fun x' e _ => by + obtain ⟨x, hx, -, hs, -⟩ := ht x' e + exact ⟨x, by rw [h1, hx], hs⟩)⟩, rfl, fun q x' hq => ?_⟩, fun q x' hq => ?_, fun y' k hy' => ?_⟩ + · have ne : σ.curSc ≠ p := fun e => by rw [e, h1] at hy; exact ho y k (Option.some.inj hy) + exact ⟨y, by show Function.update σ.kheap p _ σ.curSc = _; rw [Function.update_of_ne ne]; exact hy, rfl, rfl, rfl⟩ + · by_cases e : q = p + · rw [e] at hq + change Function.update σ.kheap p _ p = _ at hq + rw [Function.update_self] at hq + obtain ⟨x, hx, hs, -⟩ := ht x' (Option.some.inj hq) + exact ⟨x, by rw [e, h1, hx], hs⟩ + · exact ⟨x', by change Function.update σ.kheap p _ q = _ at hq; rwa [Function.update_of_ne e] at hq, rfl⟩ + · by_cases e : q = p + · rw [e] at hq + change Function.update σ.kheap p _ p = _ at hq + rw [Function.update_self] at hq + obtain ⟨x, hx, -, -, hf⟩ := ht x' (Option.some.inj hq) + exact ⟨x, by rw [e, h1, hx], hf⟩ + · exact ⟨x', by change Function.update σ.kheap p _ q = _ at hq; rwa [Function.update_of_ne e] at hq, rfl⟩ + · change Function.update σ.kheap p _ σ.curSc = _ at hy' + have ne : σ.curSc ≠ p := fun e => by rw [e, Function.update_self] at hy'; exact hn y' k (Option.some.inj hy') + rw [Function.update_of_ne ne] at hy' + exact ⟨y', hy', rfl⟩ + +theorem threadSet_skA (f : Tcb → Tcb) (hf : ∀ x, (f x).tcbState = x.tcbState) + (hsc : ∀ x, (f x).tcbSchedContext = x.tcbSchedContext ∨ (f x).tcbSchedContext = none) + (hfl : ∀ x, (f x).tcbFault = x.tcbFault) (t : ObjRef) : + PresR DSkA (threadSet (Z := Unit) f t) := by + intro σ r hr + obtain ⟨x, hx, hr2⟩ := threadSet_memA f t hr + rw [hr2] + exact DSkA.upd hx (fun y k h => by cases h) (fun y k h => by cases h) + (fun x' h => ⟨x, rfl, by rw [← KernelObject.tcb.inj h]; exact hf x, by rw [← KernelObject.tcb.inj h]; exact hsc x, + by rw [← KernelObject.tcb.inj h]; exact hfl x⟩) + +/-- (chg) `threadSet` without the fault clause (the `DSkB` frame): the fault clear of `cancel_ipc`. -/ +theorem threadSet_skB (f : Tcb → Tcb) (hf : ∀ x, (f x).tcbState = x.tcbState) + (hsc : ∀ x, (f x).tcbSchedContext = x.tcbSchedContext ∨ (f x).tcbSchedContext = none) (t : ObjRef) : + PresR DSkB (threadSet (Z := Unit) f t) := by + intro σ r hr + obtain ⟨x, hx, hr2⟩ := threadSet_memA f t hr + rw [hr2] + refine ⟨⟨rfl, rfl, rfl, rfl, rfl, rfl, fun y k hy => ?_, id, CtScKA.ofUpd (o' := .tcb (f x)) (p := t) rfl (fun x' e _ => + ⟨x, hx, by rw [← KernelObject.tcb.inj e]; exact hsc x⟩)⟩, rfl, fun q x' hq => ?_⟩ + · have ne : σ.curSc ≠ t := fun e => by rw [e, hx] at hy; cases hy + exact ⟨y, by show Function.update σ.kheap t _ σ.curSc = _; rw [Function.update_of_ne ne]; exact hy, rfl, rfl, rfl⟩ + · by_cases e : q = t + · rw [e] at hq + change Function.update σ.kheap t _ t = _ at hq + rw [Function.update_self] at hq + exact ⟨x, by rw [e, hx], by rw [← KernelObject.tcb.inj (Option.some.inj hq)]; exact hf x⟩ + · exact ⟨x', by change Function.update σ.kheap t _ q = _ at hq; rwa [Function.update_of_ne e] at hq, rfl⟩ + +theorem setNotification_skA (p : ObjRef) (a : Notification) : PresR DSkA (setNotification (Z := Unit) p a) := by + intro σ r hr + obtain ⟨y, hk, hr2⟩ := setSimpleKo_memA KernelObject.notification p a hr + rw [hr2] + exact DSkA.upd hk (fun y k h => by cases h) (fun y k h => by cases h) (fun x' h => by cases h) + +theorem updateSchedContext_skA (p : ObjRef) (f : SchedContext → SchedContext) + (hf : ∀ sc, (f sc).scRefills = sc.scRefills ∧ (f sc).scRefillMax = sc.scRefillMax ∧ (f sc).scTcb = sc.scTcb) : + PresR DSkA (updateSchedContext (Z := Unit) p f) := by + intro σ r hr + obtain ⟨y1, hy1, h⟩ := hr + have hy1' : y1 ∈ (NonDet.assertOpt (σ.kheap p) σ).1 := hy1 + cases hk : σ.kheap p with + | none => rw [hk] at hy1'; exact absurd hy1' (Set.notMem_empty _) + | some ko => + rw [hk] at hy1' + have e1 : y1 = (ko, σ) := hy1' + subst e1 + cases ko with + | schedContext sc n => + obtain ⟨ko', hko, hr2⟩ := setObject_memA h + rw [hr2] + refine ⟨⟨⟨rfl, rfl, rfl, rfl, rfl, rfl, fun z k hz => ?_, id, CtScKA.ofUpd (p := p) rfl (fun x' e _ => by cases e)⟩, rfl, fun q x' hq => ?_⟩, fun q x' hq => ?_, fun y' k hy' => ?_⟩ + · by_cases e : σ.curSc = p + · rw [e, hk] at hz + obtain ⟨rfl, rfl⟩ := KernelObject.schedContext.inj (Option.some.inj hz) + exact ⟨f sc, by show Function.update σ.kheap p _ σ.curSc = _; rw [e, Function.update_self], + (hf sc).1, (hf sc).2.1, (hf sc).2.2⟩ + · exact ⟨z, by show Function.update σ.kheap p _ σ.curSc = _; rw [Function.update_of_ne e]; exact hz, + rfl, rfl, rfl⟩ + · by_cases e : q = p + · rw [e] at hq + change Function.update σ.kheap p _ p = _ at hq + rw [Function.update_self] at hq + exact absurd (Option.some.inj hq) (by intro h; cases h) + · exact ⟨x', by change Function.update σ.kheap p _ q = _ at hq; rwa [Function.update_of_ne e] at hq, rfl⟩ + · by_cases e : q = p + · rw [e] at hq + change Function.update σ.kheap p _ p = _ at hq + rw [Function.update_self] at hq + exact absurd (Option.some.inj hq) (by intro h; cases h) + · exact ⟨x', by change Function.update σ.kheap p _ q = _ at hq; rwa [Function.update_of_ne e] at hq, rfl⟩ + · by_cases e : σ.curSc = p + · change Function.update σ.kheap p _ σ.curSc = _ at hy' + rw [e, Function.update_self] at hy' + obtain ⟨rfl, rfl⟩ := KernelObject.schedContext.inj (Option.some.inj hy') + exact ⟨sc, by rw [e]; exact hk, (hf sc).2.2⟩ + · change Function.update σ.kheap p _ σ.curSc = _ at hy' + rw [Function.update_of_ne e] at hy' + exact ⟨y', hy', rfl⟩ + | _ => exact absurd h (Set.notMem_empty _) + +theorem getNotification_eqPres (p : ObjRef) : PresR (@Eq (State Unit)) (getNotification (Z := Unit) p) := by + show PresR (@Eq (State Unit)) (getSimpleKo KernelObject.notification p) + unfold getSimpleKo + refine PresR.bind (getObject_eqPres p) (fun _ => ?_) + eqpres + +theorem getCap_eqPres (sl : CslotPtr) : PresR (@Eq (State Unit)) (getCap (Z := Unit) sl) := by + obtain ⟨oref, cref⟩ := sl + unfold getCap + dsimp only + refine PresR.bind (getObject_eqPres oref) (fun obj => ?_) + eqpres + +theorem setNtfnObjRef_skA (f : (Option ObjRef → Option ObjRef) → Notification → Notification) (ref : ObjRef) + (new : Option ObjRef) : PresR DSkA (setNtfnObjRef (Z := Unit) f ref new) := by + unfold setNtfnObjRef updateSkObjRef + exact PresR.bind (PresR.ofEq' (getNotification_eqPres ref)) (fun _ => setNotification_skA _ _) + +theorem doUnbindNtfnSc_skA (ntfn sc : ObjRef) : PresR DSkA (doUnbindNtfnSc (Z := Unit) ntfn sc) := + PresR.bind (setNtfnObjRef_skA _ _ _) (fun _ => updateSchedContext_skA _ _ (fun _ => ⟨rfl, rfl, rfl⟩)) + +theorem doUnbindNotification_skA (ntfn t : ObjRef) : PresR DSkA (doUnbindNotification (Z := Unit) ntfn t) := + PresR.bind (setNtfnObjRef_skA _ _ _) (fun _ => threadSet_skA (tcbBoundNotificationUpdate fun _ => none) (fun _ => rfl) (fun _ => Or.inl rfl) (fun _ => rfl) _) + +theorem schedContextMaybeUnbindNtfn_skA (r : ObjRef) : PresR DSkA (schedContextMaybeUnbindNtfn (Z := Unit) r) := by + unfold schedContextMaybeUnbindNtfn + refine PresR.bind (PresR.ofEq' (getNtfnObjRef_eqPres _ _)) (fun o => ?_) + cases o with + | none => exact PresR.pure _ + | some sc => exact doUnbindNtfnSc_skA _ _ + +theorem unbindMaybeNotification_skA (r : ObjRef) : PresR DSkA (unbindMaybeNotification (Z := Unit) r) := by + unfold unbindMaybeNotification + refine PresR.bind (PresR.ofEq' (getNtfnObjRef_eqPres _ _)) (fun o => ?_) + cases o with + | none => exact PresR.pure _ + | some t => exact doUnbindNotification_skA _ _ + +theorem setCap_skA (cap : Cap) (sl : CslotPtr) : PresR DSkA (setCap (Z := Unit) cap sl) := by + intro σ r hr + obtain ⟨p, cref⟩ := sl + obtain ⟨o, o', h1, h2, e⟩ := setCap_objA hr + rw [e] + cases o with + | cnode sz cn => + simp only [setCapObjA] at h2 + split_ifs at h2 + cases h2 + exact DSkA.upd h1 (fun y k h => by cases h) (fun y k h => by cases h) (fun x' h => by cases h) + | tcb t => + simp only [setCapObjA] at h2 + (repeat' split at h2) <;> cases h2 <;> + exact DSkA.upd h1 (fun y k h => by cases h) (fun y k h => by cases h) + (fun x' h => ⟨t, rfl, by cases h; rfl, Or.inl (by cases h; rfl), by cases h; rfl⟩) + | _ => cases h2 + +theorem emptySlot_skA (sl : CslotPtr) : PresR DSkA (emptySlot (Z := Unit) sl .nullCap) := by + intro σ r hr + unfold emptySlot at hr + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + have ex : x.2 = σ := (getCap_eqPres sl σ x hx).symm + rw [ex] at hr + have rest : PresR DSkA (if x.1 = .nullCap then (pure () : SMonad Unit Unit) else do + let slotP ← NonDet.gets fun s : State Unit => s.cdt sl + let cdt ← NonDet.gets fun s : State Unit => s.cdt + let parent ← pure (cdt sl) + setCdt (Function.update (fun p => if cdt p = some sl then parent else cdt p) sl none) + doExtendedOp (emptySlotExt sl slotP) + setOriginal sl false + setCap .nullCap sl + postCapDeletion .nullCap) := by + split + · exact PresR.pure _ + refine PresR.bind (PresR.gets _) fun _ => PresR.bind (PresR.gets _) fun _ => PresR.bind (PresR.pure _) fun _ => ?_ + refine PresR.bind (fun σ r hr => ?_) fun _ => PresR.bind (fun σ r hr => ?_) fun _ => + PresR.bind (fun σ r hr => ?_) fun _ => PresR.bind (setCap_skA _ _) fun _ => ?_ + · unfold setCdt at hr + obtain ⟨y, hy, hr⟩ := mem_bindA.1 hr + obtain rfl : y = (σ, σ) := hy + obtain rfl : r = ((), _) := hr + exact DSkA.of_kheap ⟨rfl, rfl, rfl, rfl, rfl, rfl⟩ rfl rfl rfl + · unfold doExtendedOp at hr + refine (?_ : PresR DSkA (_ : SMonad Unit Unit)) σ r hr + refine PresR.bind (fun σ r hr => ?_) fun _ => PresR.bind (fun σ r hr => ?_) fun x => ?_ + · obtain rfl : r = (σ, σ) := hr + exact PreO.refl (R := DSkA) σ + · have e : r.2 = σ := (PresR.selectF (R := @Eq (State Unit)) _ σ r hr).symm + rw [e] + exact PreO.refl (R := DSkA) σ + · obtain ⟨u, es⟩ := x + intro σ r hr + obtain rfl : r = ((), _) := hr + exact DSkA.of_kheap ⟨rfl, rfl, rfl, rfl, rfl, rfl⟩ rfl rfl rfl + · unfold setOriginal at hr + obtain ⟨y, hy, hr⟩ := mem_bindA.1 hr + obtain rfl : y = (σ.isOriginalCap, σ) := hy + obtain rfl : r = ((), _) := hr + exact DSkA.of_kheap ⟨rfl, rfl, rfl, rfl, rfl, rfl⟩ rfl rfl rfl + · exact PresR.pure _ + exact rest σ r hr + +/-! ## `cancel_all_signals` -/ + +theorem getNotification_ckA (p : ObjRef) : CPresA (getNotification (Z := Unit) p) := + PresR.ofEq' (getNotification_eqPres p) + +theorem setNotification_ckA (p : ObjRef) (a : Notification) : CPresA (setNotification (Z := Unit) p a) := by + intro σ r hr + obtain ⟨y, hk, hr2⟩ := setSimpleKo_memA KernelObject.notification p a hr + rw [hr2] + refine CKeepA.upd (fun z k e => ?_) (fun x' e => by cases e) + rw [hk] at e + cases e + +theorem tcbNtfnDequeue_ckA (t n : ObjRef) : CPresA (tcbNtfnDequeue (Z := Unit) t n) := by + unfold tcbNtfnDequeue + refine PresR.bind (getNotification_ckA _) (fun _ => ?_) + repeat' (first + | exact setNotification_ckA _ _ + | exact PresR.pure _ + | exact PresR.assert _ + | refine PresR.bind_pure _ _ ?_ + | refine PresR.bind ?_ (fun _ => ?_) + | dsimp only) + +theorem removeAndRestartNtfn_ckA (t n : ObjRef) : PresR (CNeA t) (removeAndRestartNtfnQueuedThread (Z := Unit) t n) := by + unfold removeAndRestartNtfnQueuedThread + exact PresR.bind (cne_of (tcbNtfnDequeue_ckA _ _)) fun _ => PresR.bind (cne_of (setThreadState_ckA _ _)) fun _ => + PresR.bind (cne_of (getTcbObjRef_ckA _ _)) fun _ => PresR.bind (cne_of (ifSporadicAssert_ckA _)) fun _ => + possibleSwitchTo_ckA _ + +/-- **`cancel_all_signals` changes nothing, or is a `CKeepA` step ending in `choose_new_thread`.** -/ +theorem cancelAllSignals_bodyA {p : ObjRef} {σ σ' : State Unit} (h : ((), σ') ∈ (cancelAllSignals (Z := Unit) p σ).1) + (hq : NtfnQNeA p σ) : + σ' = σ ∨ CKeepA σ σ' ∧ σ'.schedulerAction = .chooseNewThread := by + unfold cancelAllSignals at h + obtain ⟨⟨ntfn, σ1⟩, h1, h⟩ := mem_bindA.1 h + have hk : σ.kheap p = some (.notification ntfn) := (getNotification_mem h1).1 + have e1 : σ1 = σ := (getNotification_mem h1).2 + rw [e1] at h + cases hno : ntfn.ntfnObj with + | waitingNtfn q => + refine Or.inr ?_ + simp only [hno] at h + obtain ⟨⟨u, σ2⟩, h2, h3⟩ := mem_bindA.1 h + have loop : PresR (CNlA q) (NonDet.mapMx (fun t => removeAndRestartNtfnQueuedThread (Z := Unit) t p) q) := + mapMx_neA _ (fun t => removeAndRestartNtfn_ckA t p) q q fun _ h => h + have k1 : CKeepA σ σ2 := (loop σ _ h2).2 (hq ntfn q hk hno) + have k2 : CKeepA σ2 σ' := rescheduleRequired_ckA σ2 _ h3 + exact ⟨PreO.trans k1 k2, rescheduleRequired_chooseA h3⟩ + | _ => + simp only [hno] at h + exact Or.inl (Prod.mk.inj h).2 + +/-- The two unbinds keep the notification's wait queue. -/ +theorem _root_.Fl4ProofsA64.P3.NtfnQNeA.unbinds {p : ObjRef} {σ : State Unit} (h : NtfnQNeA p σ) + {y : Unit × State Unit} + (hy : y ∈ ((schedContextMaybeUnbindNtfn (Z := Unit) p >>= fun _ => unbindMaybeNotification p) σ).1) + (hc : y.2.curThread = σ.curThread) : NtfnQNeA p y.2 := by + intro z q hz hq + obtain ⟨-, ko⟩ := unbinds_nkA p σ y hy + obtain ⟨z0, hz0, hobj⟩ := ko z hz + rw [hc] + exact h z0 q hz0 (hobj ▸ hq) + +/-! ## `deleting_irq_handler` -/ + +/-- (chg) **`deleting_irq_handler`, with the pieces of the `cancel_all_signals` case**: a strong prefix, the +cancel, a strong suffix. -/ +theorem deletingIrqHandler_decompA {σ : State Unit} {irq : Irq} + {ac0 : Cap} (hg : getCap (Z := Unit) (σ.interruptIrqNode irq, []) σ = ({(ac0, σ)}, False)) + (hac : ac0 = .nullCap ∨ ∃ r b rs, ac0 = .notificationCap r b rs) + (hq : ∀ r b rs, ac0 = .notificationCap r b rs → NtfnQNeA r σ) + {y : Unit × State Unit} (hy : y ∈ (deletingIrqHandler (Z := Unit) irq σ).1) : + DSkA σ y.2 ∨ (DWkA σ y.2 ∧ y.2.schedulerAction = .chooseNewThread ∧ + ∃ (r : ObjRef) (b : Badge) (rs : CapRights) (σa σb : State Unit), + ac0 = .notificationCap r b rs ∧ DSkA σ σa ∧ NKeepA r σ σa ∧ NtfnQNeA r σa ∧ + ((), σb) ∈ (cancelAllSignals (Z := Unit) r σa).1 ∧ DSkA σb y.2) := by + unfold deletingIrqHandler at hy + obtain ⟨q, hq, hy⟩ := mem_bindA.1 hy + obtain rfl : q = (((σ.interruptIrqNode irq, []) : CslotPtr), σ) := hq + dsimp only at hy + unfold capDeleteOne at hy + obtain ⟨c0, hc0, hy⟩ := mem_bindA.1 hy + rw [hg] at hc0 + obtain rfl : c0 = (ac0, σ) := hc0 + dsimp only at hy + by_cases hnull : ac0 = .nullCap + · have ey : y = ((), σ) := by + simp only [NonDet.unless, hnull, not_true_eq_false, ↓reduceIte] at hy + exact hy + rw [ey] + exact Or.inl (PreO.refl σ) + have hy2 : y ∈ ((isFinalCap (Z := Unit) ac0 >>= fun final => fastFinalise ac0 final >>= + fun _ => emptySlot (σ.interruptIrqNode irq, []) .nullCap) σ).1 := by + simpa only [NonDet.unless, hnull, not_false_eq_true, ↓reduceIte] using hy + obtain ⟨fin, hfin, hy3⟩ := mem_bindA.1 hy2 + have efin : fin.2 = σ := (PresR.gets (R := @Eq (State Unit)) _ σ fin hfin).symm + rw [efin] at hy3 + obtain ⟨w, hw', hy4⟩ := mem_bindA.1 hy3 + have es : DSkA w.2 y.2 := emptySlot_skA _ w.2 y hy4 + rcases hac with h | ⟨r, b, rs, rfl⟩ + · exact absurd h hnull + unfold fastFinalise at hw' + cases hf : fin.1 + · rw [hf] at hw' + have ew : w = ((), σ) := hw' + rw [ew] at es + exact Or.inl es + · rw [hf] at hw' + obtain ⟨a1, ha1, hw2⟩ := mem_bindA.1 hw' + obtain ⟨a2, ha2, hw3⟩ := mem_bindA.1 hw2 + have k1 : DSkA σ a1.2 := schedContextMaybeUnbindNtfn_skA _ σ a1 ha1 + have k2 : DSkA a1.2 a2.2 := unbindMaybeNotification_skA _ a1.2 a2 ha2 + obtain ⟨u, σ3⟩ := w + have hw3' : ((), σ3) ∈ (cancelAllSignals (Z := Unit) r a2.2).1 := hw3 + have hq2 : NtfnQNeA r a2.2 := + (hq r b rs rfl).unbinds (mem_bindA.2 ⟨a1, ha1, ha2⟩) (k1.cur.symm ▸ k2.cur) + rcases cancelAllSignals_bodyA hw3' hq2 with e | ⟨K, act⟩ + · left + rw [e] at es + exact PreO.trans (R := DSkA) k1 (PreO.trans (R := DSkA) k2 es) + · right + refine ⟨PreO.trans (R := DWkA) k1.toDWkA (PreO.trans (R := DWkA) k2.toDWkA + (PreO.trans (R := DWkA) K.toWk es.toDWkA)), ?_, r, b, rs, a2.2, σ3, rfl, PreO.trans (R := DSkA) k1 k2, + (unbinds_nkA r σ _ (mem_bindA.2 ⟨a1, ha1, ha2⟩)), hq2, hw3', es⟩ + rw [es.act] + exact act + +/-- **`deleting_irq_handler` of an IRQ slot holding a notification cap or nothing** (module docstring). -/ +theorem deletingIrqHandler_bodyA {σ : State Unit} {irq : Irq} + {ac0 : Cap} (hg : getCap (Z := Unit) (σ.interruptIrqNode irq, []) σ = ({(ac0, σ)}, False)) + (hac : ac0 = .nullCap ∨ ∃ r b rs, ac0 = .notificationCap r b rs) + (hq : ∀ r b rs, ac0 = .notificationCap r b rs → NtfnQNeA r σ) + {y : Unit × State Unit} (hy : y ∈ (deletingIrqHandler (Z := Unit) irq σ).1) : + DSkA σ y.2 ∨ (DWkA σ y.2 ∧ y.2.schedulerAction = .chooseNewThread) := + (deletingIrqHandler_decompA hg hac hq hy).imp id fun h => ⟨h.1, h.2.1⟩ + +/-- (chg) The blocked-waiter premise of `cancel_all_signals` crosses the two unbinds. -/ +theorem ntfnBlk_nkeepA {r : ObjRef} {σ σ' : State Unit} (k : NKeepA r σ σ') + (hb : ∀ y q, σ.kheap r = some (.notification y) → y.ntfnObj = .waitingNtfn q → ∀ t ∈ q, NtfnBlockedA σ t) : + ∀ y q, σ'.kheap r = some (.notification y) → y.ntfnObj = .waitingNtfn q → ∀ t ∈ q, NtfnBlockedA σ' t := by + intro y q hy hq t hm + obtain ⟨x0, hx0, ho⟩ := k.2 y hy + obtain ⟨x, o, hx, hs⟩ := hb x0 q hx0 (ho ▸ hq) t hm + obtain ⟨x', hx', -⟩ := k.1.2.2.2.2.2.2.1 t x hx + obtain ⟨x2, hx2, -, hs2⟩ := k.1.2.2.2.2.2.1 t x' hx' + rw [hx] at hx2 + obtain rfl := KernelObject.tcb.inj (Option.some.inj hx2) + exact ⟨x', o, hx', hs2.trans hs⟩ + +section +variable {S A : Type} {st : machine.Store S} [L : LawfulStore st] +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s : S} {a : A} {σ σ' : State Unit} {m : McState} + +/-- **The body-end facts after `Restart` and a deletion step** (module docstring). -/ +theorem BodyOKA.ofDel (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {x : Tcb} (hk : σ.kheap σ.curThread = some (.tcb x)) + (h : DSkA (stUpdA σ σ.curThread x .restart) σ' ∨ + (DWkA (stUpdA σ σ.curThread x .restart) σ' ∧ σ'.schedulerAction = .chooseNewThread)) : + BodyOKA σ' m ∧ σ'.curThread = σ.curThread := by + obtain ⟨y, j, hy⟩ := J.curInv.sc + have ne : σ.curSc ≠ σ.curThread := fun e => by rw [e, hk] at hy; cases hy + have h1 : (stUpdA σ σ.curThread x .restart).kheap σ.curSc = some (.schedContext y j) := by + simp only [stUpdA, setObjA, Function.update_of_ne ne]; exact hy + have hobj : ∀ (K : DWkA (stUpdA σ σ.curThread x .restart) σ') y' k, σ'.kheap σ.curSc = some (.schedContext y' k) → + ∃ y0, σ.kheap σ.curSc = some (.schedContext y0 k) ∧ y'.scRefills = y0.scRefills ∧ + y'.scRefillMax = y0.scRefillMax ∧ y'.scTcb = y0.scTcb := by + intro K y' k hy' + obtain ⟨y2, h2, e1, e2, e3⟩ := K.sc y j h1 + have h2' : σ'.kheap σ.curSc = some (.schedContext y2 j) := h2 + rw [h2'] at hy' + obtain ⟨rfl, rfl⟩ := KernelObject.schedContext.inj (Option.some.inj hy') + exact ⟨y, hy, e1, e2, e3⟩ + rcases h with K | ⟨K, act⟩ + · refine ⟨BodyOKA.ofEntry J (Or.inl K.act) K.time K.last K.cur K.csc K.cons K.rel (hobj K.toDWkA) + (fun _ x' hx' => ?_), K.cur⟩ + obtain ⟨x0, hx0, e⟩ := K.tcb _ _ hx' + simp only [stUpdA, setObjA, Function.update_self, Option.some.injEq, KernelObject.tcb.injEq] at hx0 + subst hx0 + exact Or.inr e + · exact ⟨BodyOKA.ofEntry J (Or.inr act) K.time K.last K.cur K.csc K.cons K.rel (hobj K) + (fun e => by rw [act] at e; cases e), K.cur⟩ + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3DelBodyHookA.lean b/proofs/Fl4ProofsA64/P3/Stage3DelBodyHookA.lean new file mode 100644 index 000000000..1c090327f --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3DelBodyHookA.lean @@ -0,0 +1,66 @@ +/- +P3 (AArch64, A64-P3 DelBodyA): **a finaliser hook carries the body invariant** (`FinHookKA.bodyA`, twin of +`FinHookKA.timeA`): a hook at `W` / `Wx` is one at `DelWBodyA W ct m` / `DelWBodyA Wx ct m` given the spec finaliser's +per-kind step frame `DelStepA` (`FinBodySpecA`, from states satisfying the spec predicate `Pr`, which the hook's `W` +implies: `hP`). + +No named premise (`FinBodySpecA` is a hypothesis of the lemma; each kind's instance is its own lemma). New module. +-/ +import Fl4ProofsA64.P3.Stage3DelFrameA +import Fl4ProofsA64.P3.DelHookKindA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +/-- **The spec finaliser's body frame** on the caps of kind `k`: from a state with the body invariant, every outcome is a +`DelStepA` step. -/ +def FinBodySpecA (Fn : FinFnA) (k : FinKindA) (ct : ObjRef) (m : McState) (Pr : State Unit → Prop) : Prop := + ∀ (c : capability.Cap) (ac : Cap) (d : Bool) (σ : State Unit) (x : Cap × Cap) (σ' : State Unit), + DelBodyInvA ct m σ → Pr σ → finKindA c = some k → CapEqA c ac → (x, σ') ∈ (Fn ac d σ).1 → DelStepCA σ σ' + +/-- `FinBodySpecA` with a side condition that may read the cap. -/ +def FinBodySpecCA (Fn : FinFnA) (k : FinKindA) (ct : ObjRef) (m : McState) + (Pr : State Unit → capability.Cap → Prop) : Prop := + ∀ (c : capability.Cap) (ac : Cap) (d : Bool) (σ : State Unit) (x : Cap × Cap) (σ' : State Unit), + DelBodyInvA ct m σ → Pr σ c → finKindA c = some k → CapEqA c ac → (x, σ') ∈ (Fn ac d σ).1 → DelStepCA σ σ' + +/-- `DelOwnA` across one hook step. -/ +theorem DelOwn0A.stepA {σ σ' : State Unit} (h : DelOwn0A σ) (k : DelStepA σ σ') : DelOwn0A σ' := by + intro y' n hy hac hni o ho + rw [k.csc] at hy hni + obtain ⟨y, hy0, hc⟩ := k.sc y' n hy + rcases hc with ⟨-, e2, e3⟩ | ⟨-, hf⟩ | ⟨-, -, hn⟩ + · have hac' : scActive y = true := by + unfold scActive at hac ⊢; rwa [← e2] + exact fun hm => h y n hy0 hac' hni o (e3 ▸ ho) (k.rel _ hm) + · rw [hf] at hac; cases hac + · exact hn o ho + +theorem DelOwnA.stepA {σ σ' : State Unit} (h : DelOwnA σ) (k : DelStepCA σ σ') : DelOwnA σ' := + ⟨h.1.stepA k, h.2.keep k.ch⟩ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {th : TypedHeapA} {OS : Set U64} {Fn : FinFnA} {W Wx : DelWA S A} {k : FinKindA} + {Rd Rz : S → A → State Unit → MdbGhostA → U64 → Prop} + +/-- **A finaliser hook keeps the body invariant** (module docstring). -/ +theorem FinHookKA.bodyA (H : FinHookKA st k ar th OS Fn W Wx Rd Rz) {ct : ObjRef} {m : McState} + {Pr : State Unit → Prop} (hS : FinBodySpecA Fn k ct m Pr) (hP : ∀ s a σ G, W s a σ G → Pr σ) : + FinHookKA st k ar th OS Fn (DelWBodyA W ct m) (DelWBodyA Wx ct m) Rd Rz := by + intro s a σ G sl c ac f D hW hsl hc hk hg hac hf + refine WP.spec_mono (H s a σ G sl c ac f D hW.1 hsl hc hk hg hac hf) ?_ + rintro r ⟨h1, h2, h3, σ', G', ⟨d, hd, hmem⟩, D', hc', hoth, ⟨hWn, hWx⟩, hRd, hRz⟩ + have k := hS c ac d σ _ σ' hW.2.1 (hP _ _ _ _ hW.1) hk hac hmem + have st' : DelBodyInvA ct m σ' ∧ DelOwnA σ' := ⟨Fl4ProofsA64.P3.BodyOKA.stepA hW.2.1 k, hW.2.2.stepA k⟩ + exact ⟨h1, h2, h3, σ', G', ⟨d, hd, hmem⟩, D', hc', hoth, ⟨fun h => ⟨hWn h, st'⟩, fun h => ⟨hWx h, st'⟩⟩, hRd, hRz⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3DelBodyLoopA.lean b/proofs/Fl4ProofsA64/P3/Stage3DelBodyLoopA.lean new file mode 100644 index 000000000..d4cf5ae6d --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3DelBodyLoopA.lean @@ -0,0 +1,290 @@ +/- +P3 (AArch64, A64-P3 DelBodyA slice 1): **the body invariant across the deletion loop's own steps** (spec level): +`cap_swap_for_delete` (`capSwapForDelete_skA`), `post_cap_deletion` of an IRQ handler cap (`postCapIrq_dlA`) and the +preemption point (`ppBody_okA` / `ppBody_preA`). The zombie and emptying steps are `setCap_skA` / `emptySlot_skA`. + +The preemption point moves the clock, so it is not a `DelStepA` step: `OffReadyA` survives because the head's +`rTime + consumed` stays below the new time; on `Ok` the active SC passed the sufficiency check, which gives `OffOKA` +([P] `fl4MinBudget = MIN_BUDGET`); on `Preempted` only `BodyPreA` remains. + +Named premises: [P] `hag : fl4MinBudget = MIN_BUDGET`; [S] `numDomains = 1`; [P] the raw counter is the clock +(`hNow`). New module. +-/ +import Fl4ProofsA64.P3.Stage3DelFinVcpuA +import Fl4ProofsA64.P3.Stage3DelBodyHookA +import Fl4ProofsA64.P3.ScTimeA +import Fl4ProofsA64.P3.ScSpecA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +/-! ## The swap and post-deletion steps -/ + +theorem setCdt_skA (t : Cdt) : PresR DSkA (setCdt (Z := Unit) t) := by + intro σ r hr + unfold setCdt at hr + obtain ⟨y, hy, hr⟩ := mem_bindA.1 hr + obtain rfl : y = (σ, σ) := hy + obtain rfl : r = ((), _) := hr + exact DSkA.of_kheap ⟨rfl, rfl, rfl, rfl, rfl, rfl⟩ rfl rfl rfl + +theorem setOriginal_skA (sl : CslotPtr) (v : Bool) : PresR DSkA (setOriginal (Z := Unit) sl v) := by + unfold setOriginal + refine PresR.bind (PresR.gets _) fun _ => PresR.modify _ fun σ => ?_ + exact DSkA.of_kheap ⟨rfl, rfl, rfl, rfl, rfl, rfl⟩ rfl rfl rfl + +theorem doExtendedOp_eqU (eop : DetExtMonad Unit) : PresR (@Eq (State Unit)) (doExtendedOp (Z := Unit) eop) := by + unfold doExtendedOp + refine PresR.bind PresR.get fun _ => PresR.bind (PresR.selectF _) fun _ => PresR.modify _ fun σ => ?_ + cases σ; rfl + +theorem capSwapForDelete_skA (sl1 sl2 : CslotPtr) : PresR DSkA (capSwapForDelete (Z := Unit) sl1 sl2) := by + unfold capSwapForDelete capSwap + refine PresR.when (PresR.bind (PresR.ofEq' (getCap_eqPres _)) fun _ => + PresR.bind (PresR.ofEq' (getCap_eqPres _)) fun _ => ?_) + refine PresR.bind (setCap_skA _ _) fun _ => PresR.bind (setCap_skA _ _) fun _ => PresR.bind (PresR.gets _) fun _ => + PresR.bind (PresR.gets _) fun _ => PresR.bind (PresR.gets _) fun _ => PresR.bind (PresR.pure _) fun _ => + PresR.bind (setCdt_skA _) fun _ => PresR.bind (PresR.ofEq' (doExtendedOp_eqU _)) fun _ => + PresR.bind (PresR.gets _) fun _ => PresR.bind (setOriginal_skA _ _) fun _ => setOriginal_skA _ _ + +theorem capInsert_skA (c : Cap) (s d : CslotPtr) : PresR DSkA (capInsert (Z := Unit) c s d) := by + unfold capInsert setUntypedCapAsFull updateCdt + repeat' (first + | exact PresR.ofEq' (getCap_eqPres _) + | exact PresR.ofEq' (doExtendedOp_eqU _) + | exact setCap_skA _ _ + | exact setCdt_skA _ + | exact setOriginal_skA _ _ + | exact PresR.gets _ + | exact PresR.get + | exact PresR.pure _ + | exact PresR.assert _ + | refine PresR.bind ?_ (fun _ => ?_) + | refine PresR.ite' ?_ ?_ + | dsimp only) + +theorem postCapIrq_dlA (irq : Irq) : NdDlA (postCapDeletion (Z := Unit) (.irqHandlerCap irq)) := by + unfold postCapDeletion deletedIrqHandler setIrqState + exact NdDlA.bind (NdDlA.modify fun _ => dlA_eqA rfl rfl rfl rfl rfl rfl rfl rfl rfl) fun _ => + NdDlA.machine (maskInterrupt_mlkA _ _) + +/-! ## The preemption point -/ + +/-- What a clock-moving exit of the preemption point keeps, and its new time words. -/ +structure PpTickA (σ σ' : State Unit) : Prop where + kheap : σ'.kheap = σ.kheap + cur : σ'.curThread = σ.curThread + csc : σ'.curSc = σ.curSc + rel : σ'.releaseQueue = σ.releaseQueue + act : σ'.schedulerAction = σ.schedulerAction + rq : σ'.readyQueues = σ.readyQueues + time : σ'.curTime = rawNextA σ.machineState + cons : σ'.consumedTime = σ.consumedTime + (rawNextA σ.machineState - σ.curTime) + +/-- The sufficiency check an `Ok` exit passed. -/ +def PpFitA (σ : State Unit) : Prop := + readScActive (Z := Unit) σ.curSc σ = some true ∧ + readScRefillSufficient (Z := Unit) σ.curSc σ.consumedTime σ = some true + +private theorem mem_liftE_bindA {α β ε : Type} {m : NonDet (State Unit) α} {f : α → NonDetE (State Unit) ε β} + {σ : State Unit} {r : Except ε β × State Unit} + (h : r ∈ (((NonDetE.liftE m : NonDetE (State Unit) ε α) >>= f).run σ).1) : + ∃ x ∈ (m σ).1, r ∈ ((f x.1).run x.2).1 := by + rw [ExceptT.run_bind] at h + unfold NonDetE.liftE at h + rw [ExceptT.run_lift] at h + obtain ⟨y, hy, h⟩ := mem_bindA.1 h + obtain ⟨x, hx, hy⟩ := mem_bindA.1 hy + obtain rfl : y = (Except.ok x.1, x.2) := hy + exact ⟨x, hx, h⟩ + +/-- **The outcomes of the spec preemption point** (module docstring). -/ +theorem preemptionPoint_casesA {σ σ' : State Unit} {e : Except Unit Unit} (hnd : numDomains = 1) + (hNow : fl4ReadRawCounter σ.machineState = getCurrentTime σ.machineState) + (h : (e, σ') ∈ ((preemptionPoint (Z := Unit)).run σ).1) : + (e = .ok () ∧ σ' = σ) ∨ (PpTickA σ σ' ∧ (e = .ok () → PpFitA σ')) := by + unfold preemptionPoint at h + obtain ⟨x, hx, h⟩ := mem_liftE_bindA h + rw [← doExtendedOp_eqU _ σ x hx] at h + unfold orChoiceE at h + obtain ⟨x, hx, h⟩ := mem_liftE_bindA h + obtain rfl : x = (σ, σ) := hx + obtain ⟨x, hx, h⟩ := mem_liftE_bindA h + rw [show x.2 = σ from hx.2] at h + obtain ⟨⟨rv, w⟩, s1⟩ := x + dsimp only at h + split at h + swap + · have e1 : (e, σ') = (Except.ok (), σ) := h + exact Or.inl ⟨(Prod.mk.inj e1).1, (Prod.mk.inj e1).2⟩ + right + obtain ⟨x, hx, h⟩ := mem_liftE_bindA h + rw [← doExtendedOp_eqU _ σ x hx] at h + obtain ⟨x, hx, h⟩ := mem_liftE_bindA h + rw [← fl4UpdateTimestamp_eq_l4vA σ hnd hNow, fl4UpdateTimestamp_eqA σ] at hx + rw [show x = ((), _) from hx] at h + dsimp only at h + generalize eτ : fl4TimestampState (rawNextA σ.machineState) + { σ with machineState := rawStateA σ.machineState } = τ at h + have tick : ∀ ms : MachineState, PpTickA σ { τ with machineState := ms } := fun ms => by + subst eτ; exact ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ + obtain ⟨x, hx, h⟩ := mem_liftE_bindA h + have eirq : ∃ ms, x.2 = { τ with machineState := ms } := by + unfold doMachineOp at hx + obtain ⟨p, hp, hx⟩ := mem_bindA.1 hx + obtain rfl : p = (τ.machineState, τ) := hp + obtain ⟨q, hq, hx⟩ := mem_bindA.1 hx + have e2 : q.2 = τ := hq.2 + obtain ⟨⟨y, ms⟩, s2⟩ := q + dsimp only at e2 hx + rw [e2] at hx + obtain ⟨c, hc, hx⟩ := mem_bindA.1 hx + obtain rfl : c = ((), { τ with machineState := ms }) := hc + have e3 : x = (y, { τ with machineState := ms }) := hx + exact ⟨ms, by rw [e3]⟩ + obtain ⟨ms, ems⟩ := eirq + rw [ems] at h + obtain ⟨y1, hy1, h⟩ := mem_liftE_bindA h + rw [show y1.2 = { τ with machineState := ms } from (Set.mem_singleton_iff.1 hy1 ▸ rfl)] at h + obtain ⟨y2, hy2, h⟩ := mem_liftE_bindA h + obtain rfl : y2 = (({ τ with machineState := ms } : State Unit).curSc, { τ with machineState := ms }) := hy2 + obtain ⟨y3, hy3, h⟩ := mem_liftE_bindA h + obtain rfl : y3 = (({ τ with machineState := ms } : State Unit).consumedTime, { τ with machineState := ms }) := hy3 + obtain ⟨y4, hy4, h⟩ := mem_liftE_bindA h + dsimp only at hy4 h + have e4 : y4.2 = { τ with machineState := ms } ∧ + readScActive (Z := Unit) τ.curSc { τ with machineState := ms } = some y4.1 := by + unfold getScActive NonDet.getsThe at hy4 + change y4 ∈ (NonDet.assertOpt (readScActive (Z := Unit) τ.curSc { τ with machineState := ms }) + { τ with machineState := ms }).1 at hy4 + cases hr : readScActive (Z := Unit) τ.curSc { τ with machineState := ms } with + | none => rw [hr] at hy4; exact absurd hy4 (Set.notMem_empty _) + | some b => + rw [hr] at hy4 + obtain rfl : y4 = (b, _) := hy4 + exact ⟨rfl, rfl⟩ + rw [e4.1] at h + obtain ⟨y5, hy5, h⟩ := mem_liftE_bindA h + obtain rfl : y5 = (readScRefillSufficient (Z := Unit) τ.curSc τ.consumedTime { τ with machineState := ms }, + { τ with machineState := ms }) := hy5 + dsimp only at h + unfold NonDetE.whenE at h + split at h + · have e6 : (e, σ') = (Except.error (), { τ with machineState := ms }) := h + obtain ⟨rfl, rfl⟩ := Prod.mk.inj e6 + exact ⟨tick ms, fun c => by cases c⟩ + · rename_i hn + have e6 : (e, σ') = (Except.ok (), { τ with machineState := ms }) := h + obtain ⟨rfl, rfl⟩ := Prod.mk.inj e6 + refine ⟨tick ms, fun _ => ?_⟩ + have hg : y4.1 = true ∧ readScRefillSufficient (Z := Unit) τ.curSc τ.consumedTime + { τ with machineState := ms } = some true := by + by_contra c + exact hn (Or.inr (Or.inr c)) + exact ⟨by rw [← hg.1]; exact e4.2, hg.2⟩ + +/-- `DelOwnA` across a clock tick. -/ +theorem DelOwnA.tickA {σ σ' : State Unit} (h : DelOwnA σ) (t : PpTickA σ σ') : DelOwnA σ' := by + refine ⟨?_, h.2.keep (ChKA.ofKheap t.cur t.csc t.kheap)⟩ + have h1 := h.1 + unfold DelOwn0A at h1 ⊢ + rw [t.kheap, t.csc, t.rel] + exact h1 + +/-- The check's SC: active, with a sufficient head. -/ +theorem PpFitA.sc {σ : State Unit} (h : PpFitA σ) : ∃ y k, σ.kheap σ.curSc = some (.schedContext y k) ∧ + scActive y = true ∧ MIN_BUDGET ≤ refillCapacity σ.consumedTime (refillHd y) := by + obtain ⟨h1, h2⟩ := h + have h1' : Option.bind (readSchedContext (Z := Unit) σ.curSc σ) (fun sc => some (scActive sc)) = some true := h1 + have h2' : Option.bind (Option.bind (readSchedContext (Z := Unit) σ.curSc σ) (fun sc => + Option.bind (Lookup.oassert (sc.scRefills ≠ []) σ) (fun _ => some (refillHd sc)))) + (fun hd => some (refillSufficient σ.consumedTime hd)) = some true := h2 + have ex : ∃ y k, σ.kheap σ.curSc = some (.schedContext y k) := by + have e0 : readSchedContext (Z := Unit) σ.curSc σ = Option.bind (σ.kheap σ.curSc) (fun ko => (match ko with + | .schedContext sc _ => Lookup.oreturn sc + | _ => Lookup.ofail) σ) := rfl + cases hk : σ.kheap σ.curSc with + | none => rw [e0, hk] at h1'; cases h1' + | some ko => + cases ko with + | schedContext y k => exact ⟨y, k, rfl⟩ + | _ => rw [e0, hk] at h1'; cases h1' + obtain ⟨y, k, hk⟩ := ex + rw [readSchedContext_eqA hk] at h1' h2' + simp only [Option.bind_some, Option.some.injEq] at h1' h2' + refine ⟨y, k, hk, h1', ?_⟩ + cases ha : Lookup.oassert (y.scRefills ≠ []) σ with + | none => rw [ha] at h2'; cases h2' + | some u => + rw [ha] at h2' + simp only [Option.bind_some, Option.some.injEq] at h2' + exact of_decide_eq_true h2' + +section +variable {σ σ' : State Unit} {m : McState} {ct : ObjRef} + +/-- `OffReadyA` across a tick that does not move the time back. -/ +theorem PpTickA.offReady (t : PpTickA σ σ') (hmono : σ.curTime ≤ rawNextA σ.machineState) + (R : ∀ y k, σ.kheap σ.curSc = some (.schedContext y k) → + (refillHd y).rTime.toNat + σ.consumedTime.toNat ≤ σ.curTime.toNat) + {y : SchedContext} {k : Nat} (hy : σ.kheap σ.curSc = some (.schedContext y k)) : + (refillHd y).rTime.toNat + σ'.consumedTime.toNat ≤ σ'.curTime.toNat := by + have hS := R y k hy + rw [t.cons, t.time, BitVec.toNat_add, BitVec.toNat_sub_of_le hmono] + have hnat := BitVec.le_def.1 hmono + have hSize := (rawNextA σ.machineState).isLt + rw [Nat.mod_eq_of_lt (by omega)] + omega + +/-- The preempted exit's body facts after a tick. -/ +theorem ppBody_preA (B : DelBodyInvA ct m σ) (t : PpTickA σ σ') (hmono : σ.curTime ≤ rawNextA σ.machineState) + (V : ValidMachineTimeA σ') (own : DelOwnA σ') : DelBodyPreA ct m σ' := by + obtain ⟨B, hct, hns, BB, PS⟩ := B + have PS' : DelPreSchedA ct σ' := by + refine ⟨fun d p => by rw [t.rq]; exact PS.1 d p, fun x hx => ?_, by rw [t.rel]; exact PS.2.2.1, + fun hr x hx => PS.2.2.2 (t.act ▸ hr) x (t.kheap ▸ hx)⟩ + rw [t.kheap] at hx + rw [t.csc]; exact PS.2.1 x hx + refine ⟨⟨V, fun y n hk hg z j hz => ?_, fun h x hx => ?_, fun u e => ?_, fun h => ?_, + fun y n hk ha hni _ => own.1 y n hk ha hni⟩, t.cur.trans hct, fun u e => hns u (t.act ▸ e), PS', own⟩ + · rw [t.kheap, t.csc] at hk hz + rw [t.csc] at hg + exact t.offReady hmono (fun w i hw => (BB y n hk hg).1 w i hw) hz + · rw [t.act] at h + rw [t.kheap, t.cur] at hx + exact B.ctAct h x hx + · rw [t.act] at e; exact absurd e (hns u) + · rw [t.csc] at h ⊢; exact B.scNotIdle h + +/-- The `Ok` exit's body invariant after a tick. -/ +theorem ppBody_okA (B : DelBodyInvA ct m σ) (t : PpTickA σ σ') (hmono : σ.curTime ≤ rawNextA σ.machineState) + (hag : fl4MinBudget = MIN_BUDGET) (F : PpFitA σ') (V : ValidMachineTimeA σ') + (own : DelOwnA σ') : DelBodyInvA ct m σ' := by + have P := ppBody_preA B t hmono V own + have hns : ∀ u, σ'.schedulerAction ≠ .switchThread u := fun u e => B.2.2.1 u (t.act ▸ e) + suffices BB' : DelBudA σ' from + ⟨⟨⟨V, fun y n hk hg _ => BB' y n hk hg, P.1.ctAct⟩, P.1.sw, P.1.scNotIdle, P.1.owner⟩, P.2.1, hns, BB', P.2.2.2.1⟩ + refine fun y n hk hg => ⟨P.1.ready y n hk hg, fun _ z j hz _ => ?_⟩ + obtain ⟨w, i, hw, ha, hsuf⟩ := F.sc + rw [hw] at hz + obtain ⟨rfl, -⟩ := KernelObject.schedContext.inj (Option.some.inj hz) + have hr := P.1.ready w i hw (Or.inr ha) w i hw + have hlt := σ'.curTime.isLt + have e : ((refillHd w).rTime + σ'.consumedTime).toNat = (refillHd w).rTime.toNat + σ'.consumedTime.toNat := by + rw [BitVec.toNat_add]; exact Nat.mod_eq_of_lt (by omega) + refine ⟨by rw [hag]; exact hsuf, ?_, ?_⟩ + · unfold refillReady' + rw [decide_eq_true_eq, BitVec.le_def, e]; exact hr + · unfold refillReady refillReady' + have e0 : ((refillHd w).rTime + 0).toNat = (refillHd w).rTime.toNat := by simp + rw [decide_eq_true_eq, BitVec.le_def, e0]; omega + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3DelFinChA.lean b/proofs/Fl4ProofsA64/P3/Stage3DelFinChA.lean new file mode 100644 index 000000000..7a91a0e42 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3DelFinChA.lean @@ -0,0 +1,165 @@ +/- +P3 (AArch64, A64-P3 Stage 3, line chg): **the spec finaliser's body frame for endpoint / notification / reply caps, with +`ChKA`**. The `DelStepA` of these three kinds needs the whole-operation `ChKA` of `cancel_all_ipc`, +`cancel_all_signals`, `reply_remove` and `cancel_ipc` (`Stage3ChCancelA`, `Stage3ChReplyA`), which import the frames of +`Stage3DelFinIpcA` / `Stage3DelFinReplyA`; so the three `finBodySpec_*` theorems live here, after them. The side +conditions of the endpoint and notification kinds gain the blocked-waiter facts (`EpQBlkA`, `NtfnBlockedA`) that the +`ChKA` proofs read from the state relation. + +No named premise. +-/ +import Fl4ProofsA64.P3.Stage3ChCancelA +import Fl4ProofsA64.P3.Stage3ChReplyA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4ProofsA64.P3.Endp Classical + +noncomputable section + +/-- The endpoint finaliser's side condition: no member of the cap's endpoint queue is the current thread. -/ +def EpPrA (σ : State Unit) (c : capability.Cap) : Prop := + CurScA σ ∧ ∀ r b rs, absCapA c = .endpointCap r b rs → EpQNeA r σ ∧ EpQBlkA r σ + +/-- The notification finaliser's side condition. -/ +def NtfnPrA (σ : State Unit) (c : capability.Cap) : Prop := + CurScA σ ∧ ∀ r b rs, absCapA c = .notificationCap r b rs → NtfnQNeA r σ ∧ + ∀ y q, σ.kheap r = some (.notification y) → y.ntfnObj = .waitingNtfn q → ∀ t ∈ q, NtfnBlockedA σ t + +theorem finBodySpec_epA {ct : ObjRef} {m : McState} : FinBodySpecCA fl4FinaliseCap .endpoint ct m EpPrA := by + intro c ac d σ x σ' _ hP hk hac hmem + have ea : ∃ r b rs, ac = .endpointCap r b rs := by + cases c <;> simp [finKindA] at hk + exact ⟨_, _, _, (show absCapA _ = ac from hac).symm⟩ + obtain ⟨r, b, rs, rfl⟩ := ea + obtain ⟨hne, hblk⟩ := hP.2 r b rs (by + rcases hac.eq_or_pt with e | ⟨_, _, _, e⟩ + · exact e.symm + · cases e) + change (x, σ') ∈ (NonDet.liftM (fun _ => (Cap.nullCap, Cap.nullCap)) + (NonDet.when (d = true) (cancelAllIpc (Z := Unit) r)) σ).1 at hmem + obtain ⟨⟨u, σ2⟩, h2, h3⟩ := mem_bindA.1 hmem + have e3 : σ' = σ2 := (Prod.mk.inj h3).2 + subst e3 + unfold NonDet.when at h2 + split at h2 + · exact DelStepA.ofCancel hP.1 (PreO.refl σ) (cancelAllIpc_bodyA h2 hne) (cancelAllIpc_chA h2 hP.1 hne hblk) + · have e : σ' = σ := (Prod.mk.inj h2).2 + rw [e]; exact (dlA_refl σ).step + +theorem finBodySpec_ntfnA {ct : ObjRef} {m : McState} : + FinBodySpecCA fl4FinaliseCap .notification ct m NtfnPrA := by + intro c ac d σ x σ' _ hP hk hac hmem + have ea : ∃ r b rs, ac = .notificationCap r b rs := by + cases c <;> simp [finKindA] at hk + exact ⟨_, _, _, (show absCapA _ = ac from hac).symm⟩ + obtain ⟨r, b, rs, rfl⟩ := ea + obtain ⟨hne, hblk⟩ := hP.2 r b rs (by + rcases hac.eq_or_pt with e | ⟨_, _, _, e⟩ + · exact e.symm + · cases e) + change (x, σ') ∈ (NonDet.liftM (fun _ => (Cap.nullCap, Cap.nullCap)) + (NonDet.when (d = true) (do + schedContextMaybeUnbindNtfn (Z := Unit) r + unbindMaybeNotification r + cancelAllSignals r)) σ).1 at hmem + obtain ⟨⟨u, σ2⟩, h2, h3⟩ := mem_bindA.1 hmem + have e3 : σ' = σ2 := (Prod.mk.inj h3).2 + subst e3 + unfold NonDet.when at h2 + split at h2 + · obtain ⟨a1, ha1, h2⟩ := mem_bindA.1 h2 + obtain ⟨a2, ha2, h2⟩ := mem_bindA.1 h2 + have k1 : DSkA σ a1.2 := schedContextMaybeUnbindNtfn_skA _ σ a1 ha1 + have k2 : DSkA a1.2 a2.2 := unbindMaybeNotification_skA _ a1.2 a2 ha2 + have hne2 : NtfnQNeA r a2.2 := + hne.unbinds (mem_bindA.2 ⟨a1, ha1, ha2⟩) (k2.cur.trans k1.cur) + have kk : DSkA σ a2.2 := PreO.trans (R := DSkA) k1 k2 + have hcs : CurScA a2.2 := by + obtain ⟨y, j, hy⟩ := hP.1 + obtain ⟨y2, h2', -⟩ := kk.sc y j hy + exact ⟨y2, j, by rw [kk.csc]; exact h2'⟩ + have nk : NKeepA r σ a2.2 := unbinds_nkA r σ _ (mem_bindA.2 ⟨a1, ha1, ha2⟩) + exact DelStepA.ofCancel hP.1 kk (cancelAllSignals_bodyA h2 hne2) + (ChKA.trans kk.ch (cancelAllSignals_chA h2 hcs hne2 (ntfnBlk_nkeepA nk hblk))) + · have e : σ' = σ := (Prod.mk.inj h2).2 + rw [e]; exact (dlA_refl σ).step + +theorem finBodySpec_replyA {ct : ObjRef} {m : McState} : + FinBodySpecA fl4FinaliseCap .reply ct m (fun σ => CurScA σ ∧ RlsRunA σ ∧ SActA σ) := by + intro c ac d σ x σ' _ hP hk hac hmem + have ea : ∃ r b, ac = .replyCap r b := by + cases c <;> simp [finKindA] at hk + exact ⟨_, _, (show absCapA _ = ac from hac).symm⟩ + obtain ⟨r, b, rfl⟩ := ea + change (x, σ') ∈ (NonDet.liftM (fun _ => (Cap.nullCap, Cap.nullCap)) + (NonDet.when (d = true) (do + let tptr ← getReplyTcb (Z := Unit) r + match tptr with + | none => return () + | some tp => do + let state ← getThreadState tp + match state with + | .blockedOnReply r => replyRemove tp r + | _ => cancelIpc tp)) σ).1 at hmem + obtain ⟨⟨u, σ2⟩, h2, h3⟩ := mem_bindA.1 hmem + have e3 : σ' = σ2 := (Prod.mk.inj h3).2 + subst e3 + suffices key : RvA σ σ' ∧ ChKA σ σ' from RvA.step hP.1 key.1 key.2 + unfold NonDet.when at h2 + split at h2 + · obtain ⟨a1, ha1, h2⟩ := mem_bindA.1 h2 + have g : PresR (@Eq (State Unit)) (getReplyTcb (Z := Unit) r) := by + unfold getReplyTcb + dsimp only [NonDet.liftM] + refine PresR.bind (getReply_eqPresR _) (fun _ => ?_) + eqpres + have e1 : σ = a1.2 := g σ a1 ha1 + rw [← e1] at h2 + obtain ⟨o, s1⟩ := a1 + cases o with + | none => + have e : σ' = σ := (Prod.mk.inj h2).2 + rw [e]; exact ⟨PreO.refl σ, ChKA.refl σ⟩ + | some tp => + obtain ⟨a2, ha2, h2⟩ := mem_bindA.1 h2 + have e2 : σ = a2.2 := threadGet_eqPres _ _ σ a2 ha2 + rw [← e2] at h2 + have hst : ∃ y, σ.kheap tp = some (.tcb y) ∧ a2.1 = y.tcbState := by + unfold getThreadState threadGet at ha2 + obtain ⟨z, hz, ha3⟩ := mem_bindA.1 ha2 + change z ∈ (NonDet.assertOpt (getTcb (Z := Unit) tp σ) σ).1 at hz + cases hg : getTcb (Z := Unit) tp σ with + | none => rw [hg] at hz; exact absurd hz (Set.notMem_empty _) + | some y => + rw [hg] at hz + obtain rfl : z = (y, σ) := hz + refine ⟨y, ?_, (Prod.mk.inj ha3).1⟩ + unfold getTcb readObject at hg + cases hkh : σ.kheap tp with + | none => rw [hkh] at hg; cases hg + | some ko => rw [hkh] at hg; cases ko <;> simp at hg; rw [hg] + obtain ⟨y, hy, es⟩ := hst + obtain ⟨st, s2⟩ := a2 + dsimp only at es h2 + cases st with + | blockedOnReply r' => + refine ⟨replyRemove_rvA tp r' σ _ h2 (fun hm => ?_) (fun ec => ?_), replyRemove_chA h2⟩ + rotate_left + · obtain ⟨z, hz, hc⟩ := hP.2.2.f2 + rw [← ec, hy] at hz + obtain rfl := KernelObject.tcb.inj (Option.some.inj hz) + rw [← es] at hc + rcases hc with h | h | h | h <;> cases h + obtain ⟨z, hz, hr⟩ := hP.2.1 tp hm + rw [hy] at hz + obtain rfl := KernelObject.tcb.inj (Option.some.inj hz) + rw [← es] at hr; cases hr + | _ => exact ⟨cancelIpc_rvA tp σ _ h2, cancelIpc_chA h2⟩ + · have e : σ' = σ := (Prod.mk.inj h2).2 + rw [e]; exact ⟨PreO.refl σ, ChKA.refl σ⟩ + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3DelFinIpcA.lean b/proofs/Fl4ProofsA64/P3/Stage3DelFinIpcA.lean new file mode 100644 index 000000000..fbe908710 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3DelFinIpcA.lean @@ -0,0 +1,135 @@ +/- +P3 (AArch64, A64-P3 DelBodyA slice 1): **the spec finaliser's body frame, endpoint / notification** (`FinBodySpecA`). + +The current SC must be an SC (`CurScA`, a fact of the hooks' record): the cancel frames speak forward. +`cancel_all_ipc` / `cancel_all_signals` change nothing, or are a `CKeepA` step ending in `choose_new_thread`. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3DelBodyA +import Fl4ProofsA64.P3.Stage3DelFinVsA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4ProofsA64.P3.Endp Classical + +noncomputable section + +/-- The current SC pointer names an SC. -/ +def CurScA (σ : State Unit) : Prop := ∃ y k, σ.kheap σ.curSc = some (.schedContext y k) + +/-- A weak delete frame whose action is kept (with TCB states) or `choose_new_thread` is a `DelStepA` step. -/ +theorem DelStepA.ofWk {σ σ' : State Unit} (h : CurScA σ) (K : DWkA σ σ') + (act : DSkA σ σ' ∨ σ'.schedulerAction = .chooseNewThread) (ch : ChKA σ σ') : DelStepCA σ σ' := by + refine ⟨⟨K.cur, K.time, K.last, K.csc, K.cons, fun t ht => K.rel ▸ ht, fun y' k hy => ?_, ?_, fun e x hx => ?_, + K.off, K.ctSc⟩, ch⟩ + · obtain ⟨y, j, hyj⟩ := h + obtain ⟨y2, h2, e1, e2, e3⟩ := K.sc y j hyj + rw [h2] at hy + obtain ⟨rfl, rfl⟩ := KernelObject.schedContext.inj (Option.some.inj hy) + exact ⟨y, hyj, Or.inl ⟨e1, e2, e3⟩⟩ + · exact act.imp (·.act) id + · rcases act with S | c + · exact S.tcb _ _ hx + · rw [c] at e; cases e + +/-- A `DSkA` prefix, then nothing or a cancel ending in `choose_new_thread`. -/ +theorem DelStepA.ofCancel {σ σ1 σ' : State Unit} (h : CurScA σ) (k : DSkA σ σ1) + (c : σ' = σ1 ∨ CKeepA σ1 σ' ∧ σ'.schedulerAction = .chooseNewThread) (ch : ChKA σ σ') : DelStepCA σ σ' := by + rcases c with rfl | ⟨K, act⟩ + · exact DelStepA.ofWk h k.toDWkA (Or.inl k) ch + · exact DelStepA.ofWk h (PreO.trans (R := DWkA) k.toDWkA K.toWk) (Or.inr act) ch + +theorem setReply_ckA (p : ObjRef) (a : Reply) : CPresA (setReply (Z := Unit) p a) := by + intro σ r hr + obtain ⟨y, hk, hr2⟩ := setSimpleKo_memA KernelObject.reply p a hr + rw [hr2] + refine CKeepA.upd (fun z k e => ?_) (fun x' e => by cases e) + rw [hk] at e + cases e + +theorem getReply_ckA (p : ObjRef) : CPresA (getReply (Z := Unit) p) := by + unfold getReply getSimpleKo + refine PresR.bind (getObject_ckA _) (fun _ => ?_) + ckres + +theorem replyUnlinkTcb_ckA (t r : ObjRef) : CPresA (replyUnlinkTcb (Z := Unit) t r) := by + unfold replyUnlinkTcb setReplyObjRef updateSkObjRef + repeat' (first + | exact getReply_ckA _ + | exact setReply_ckA _ _ + | exact getThreadState_ckA _ + | exact setThreadState_ckA _ _ + | exact PresR.pure _ + | exact PresR.assert _ + | refine PresR.bind ?_ (fun _ => ?_) + | dsimp only) + +theorem removeAndRestartEp_ckA (t e : ObjRef) : PresR (CNeA t) (removeAndRestartEpQueuedThread (Z := Unit) t e) := by + have dq : CPresA (tcbEpDequeue (Z := Unit) t e) := by + unfold tcbEpDequeue + refine PresR.bind (getEndpoint_ckA _) (fun _ => ?_) + ckres + have rs : PresR (CNeA t) (restartThreadIfNoFault (Z := Unit) t) := by + unfold restartThreadIfNoFault + refine PresR.bind (cne_of (threadGet_ckA _ _)) (fun _ => PresR.ite' ?_ (cne_of (setThreadState_ckA _ _))) + exact PresR.bind (cne_of (setThreadState_ckA _ _)) fun _ => PresR.bind (cne_of (getTcbObjRef_ckA _ _)) fun _ => + PresR.bind (cne_of (ifSporadicAssert_ckA _)) fun _ => possibleSwitchTo_ckA _ + unfold removeAndRestartEpQueuedThread + refine PresR.bind (cne_of dq) fun _ => PresR.bind (cne_of (getThreadState_ckA _)) fun _ => + PresR.bind (PresR.pure _) fun o => ?_ + refine PresR.bind ?_ fun _ => rs + cases o with + | none => exact PresR.pure _ + | some r => exact cne_of (replyUnlinkTcb_ckA _ _) + +/-- **`cancel_all_ipc` changes nothing, or is a `CKeepA` step ending in `choose_new_thread`.** -/ +theorem cancelAllIpc_bodyA {p : ObjRef} {σ σ' : State Unit} (h : ((), σ') ∈ (cancelAllIpc (Z := Unit) p σ).1) + (hne : EpQNeA p σ) : + σ' = σ ∨ CKeepA σ σ' ∧ σ'.schedulerAction = .chooseNewThread := by + unfold cancelAllIpc at h + obtain ⟨⟨ep, σ1⟩, h1, h⟩ := mem_bindA.1 h + have hk : σ.kheap p = some (.endpoint ep) := (getEndpoint_mem h1).1 + have e1 : σ1 = σ := (getEndpoint_mem h1).2 + rw [e1] at h + cases ep with + | idleEP => exact Or.inl (Prod.mk.inj h).2 + | _ => + refine Or.inr ?_ + obtain ⟨⟨q, σ0⟩, hq, h⟩ := mem_bindA.1 h + have eqq : q = _ := (Prod.mk.inj hq).1 + have eq0 : σ0 = σ := (Prod.mk.inj hq).2 + rw [eq0] at h + obtain ⟨⟨u, σ2⟩, h2, h3⟩ := mem_bindA.1 h + have k1 : CKeepA σ σ2 := (mapMx_neA _ (fun t => removeAndRestartEp_ckA t p) q q (fun _ h => h) σ _ h2).2 + (by rw [eqq]; exact hne _ hk) + have k2 : CKeepA σ2 σ' := rescheduleRequired_ckA σ2 _ h3 + exact ⟨PreO.trans k1 k2, rescheduleRequired_chooseA h3⟩ + +/-- A related endpoint queue holds no thread in the current thread's state (`SActA.f2`). -/ +theorem epQNe_ofRecA {T E R : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {mem : U64 → U64} {σ : State Unit} + (hR : EpStateRel T E R Bf mem σ) (sa : SActA σ) {e : U64} (he : e ∈ E) : EpQNeA (specPtr e) σ := by + intro z hz hm + obtain ⟨xs, hQ, hq, hb⟩ := (hR.at_ep he hz).queue + rw [hq, List.mem_map] at hm + obtain ⟨y, hy, ey⟩ := hm + obtain ⟨x, hx, hc⟩ := sa.f2 + rw [← ey] at hx + have hst := (hR.at_tcb (hQ.mem y hy) hx).st + have h34 := (hb y hy).2 + rcases hc with h | h | h | h <;> rw [h] at hst <;> simp [Notif.tsCode] at hst <;> omega + +theorem ntfnQNe_ofRecA {N T : Set U64} {mem : U64 → U64} {σ : State Unit} + (hR : Notif.NtfnStateRel N T mem σ) (sa : SActA σ) {n : U64} (hn : n ∈ N) : NtfnQNeA (specPtr n) σ := by + intro y q hk hq hm + obtain ⟨x, o, hx, ho⟩ := (ntfnQueue_blockedA hR hn hk q hq).2 _ hm + obtain ⟨x', hx', hc⟩ := sa.f2 + rw [hx] at hx' + obtain rfl := KernelObject.tcb.inj (Option.some.inj hx') + rw [ho] at hc + rcases hc with h | h | h | h <;> cases h + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3DelFinIrqA.lean b/proofs/Fl4ProofsA64/P3/Stage3DelFinIrqA.lean new file mode 100644 index 000000000..e7a9ac116 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3DelFinIrqA.lean @@ -0,0 +1,83 @@ +/- +P3 (AArch64, A64-P3 DelBodyA slice 1): **the spec finaliser's body frame, IRQ handler** (`finBodySpec_irqA`). + +A final handler cap deletes its IRQ-node slot, which holds `Null` or a notification cap (`deletingIrqHandler_bodyA`). +The side condition depends on the cap's IRQ number, so the hook lemma here (`FinHookKA.bodyCapA`) gives `Pr` the cap +of the slot being finalised. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3DelFinIpcA +import Fl4ProofsA64.P3.Stage3ChCancelA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {th : TypedHeapA} {OS : Set U64} {Fn : FinFnA} {W Wx : DelWA S A} {k : FinKindA} + {Rd Rz : S → A → State Unit → MdbGhostA → U64 → Prop} + +/-- **A finaliser hook keeps the body invariant**, the side condition read at the finalised cap. -/ +theorem FinHookKA.bodyCapA (H : FinHookKA st k ar th OS Fn W Wx Rd Rz) {ct : ObjRef} {m : McState} + {Pr : State Unit → capability.Cap → Prop} (hS : FinBodySpecCA Fn k ct m Pr) + (hP : ∀ s a σ G (sl : CslotPtr), DStateA st th OS s σ G → W s a σ G → AbsSlotA th sl → + Pr σ (capA st s (cslotAddrA sl))) : + FinHookKA st k ar th OS Fn (DelWBodyA W ct m) (DelWBodyA Wx ct m) Rd Rz := by + intro s a σ G sl c ac f D hW hsl hc hk hg hac hf + refine WP.spec_mono (H s a σ G sl c ac f D hW.1 hsl hc hk hg hac hf) ?_ + rintro r ⟨h1, h2, h3, σ', G', ⟨d, hd, hmem⟩, D', hc', hoth, ⟨hWn, hWx⟩, hRd, hRz⟩ + have k := hS c ac d σ _ σ' hW.2.1 (hc ▸ hP _ _ _ _ _ D hW.1 hsl) hk hac hmem + have st' : DelBodyInvA ct m σ' ∧ DelOwnA σ' := ⟨BodyOKA.stepA hW.2.1 k, hW.2.2.stepA k⟩ + exact ⟨h1, h2, h3, σ', G', ⟨d, hd, hmem⟩, D', hc', hoth, ⟨fun h => ⟨hWn h, st'⟩, fun h => ⟨hWx h, st'⟩⟩, hRd, hRz⟩ + +end + +/-- The IRQ-node slot of a handler cap holds `Null` or a notification cap; the current SC is an SC. -/ +def IrqPrA (σ : State Unit) (c : capability.Cap) : Prop := + CurScA σ ∧ ∀ irq, c = .IrqHandler irq → + ∃ ac0, getCap (Z := Unit) (σ.interruptIrqNode (BitVec.ofNat 13 irq.val), []) σ = ({(ac0, σ)}, False) ∧ + (ac0 = .nullCap ∨ ∃ r b rs, ac0 = .notificationCap r b rs) ∧ + ∀ r b rs, ac0 = .notificationCap r b rs → NtfnQNeA r σ ∧ + ∀ y q, σ.kheap r = some (.notification y) → y.ntfnObj = .waitingNtfn q → ∀ t ∈ q, NtfnBlockedA σ t + +/-- **`FinBodySpecCA` for an IRQ handler cap** (module docstring). -/ +theorem finBodySpec_irqA {ct : ObjRef} {m : McState} : FinBodySpecCA fl4FinaliseCap .irqHandler ct m IrqPrA := by + intro c ac d σ x σ' _ hP hk hac hmem + obtain ⟨irq, rfl⟩ : ∃ irq, c = .IrqHandler irq := by + cases c <;> simp [finKindA] at hk + exact ⟨_, rfl⟩ + have ea : ac = .irqHandlerCap (BitVec.ofNat 13 irq.val) := (show absCapA (.IrqHandler irq) = ac from hac).symm + subst ea + have e : fl4FinaliseCap (Z := Unit) (.irqHandlerCap (BitVec.ofNat 13 irq.val)) d = + finaliseCap (.irqHandlerCap (BitVec.ofNat 13 irq.val)) d := by unfold fl4FinaliseCap; rfl + rw [e] at hmem + unfold finaliseCap at hmem + dsimp only at hmem + split at hmem + · obtain ⟨⟨u, σ1⟩, h1, h2⟩ := mem_bindA.1 hmem + have e1 : σ' = σ1 := (Prod.mk.inj h2).2 + subst e1 + obtain ⟨ac0, hg, hac0, hq⟩ := hP.2 irq rfl + rcases deletingIrqHandler_decompA hg hac0 (fun r b rs h => (hq r b rs h).1) h1 with + K | ⟨K, act, r, b, rs, σa, σb, ea, k1, nk, hqa, hc, k2⟩ + · exact DelStepA.ofWk hP.1 K.toDWkA (Or.inl K) K.ch + · have hcs : CurScA σa := by + obtain ⟨y, j, hy⟩ := hP.1 + obtain ⟨y2, h2', -⟩ := k1.sc y j hy + exact ⟨y2, j, by rw [k1.csc]; exact h2'⟩ + exact DelStepA.ofWk hP.1 K (Or.inr act) + (ChKA.trans k1.ch (ChKA.trans (cancelAllSignals_chA hc hcs hqa + (ntfnBlk_nkeepA nk (hq r b rs ea).2)) k2.ch)) + · have e1 : σ' = σ := (Prod.mk.inj hmem).2 + subst e1 + exact ⟨⟨rfl, rfl, rfl, rfl, rfl, fun _ h => h, fun y' _ hy => ⟨y', hy, Or.inl ⟨rfl, rfl, rfl⟩⟩, Or.inl rfl, + fun _ x hx => ⟨x, hx, rfl⟩, id, CtScKA.refl _ _⟩, ChKA.refl _⟩ + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3DelFinReplyA.lean b/proofs/Fl4ProofsA64/P3/Stage3DelFinReplyA.lean new file mode 100644 index 000000000..8c0e974e2 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3DelFinReplyA.lean @@ -0,0 +1,406 @@ +/- +P3 (AArch64, A64-P3 DelBodyA slice 1): **the spec finaliser's body frame, reply** (`finBodySpec_replyA`). + +`RvA`: time fields, current thread / SC and consumed time kept; the release queue only shrinks; the current SC keeps +refills / max and its thread, or names only threads outside the release queue; the action is kept or +`choose_new_thread`; a resuming current thread keeps its state. It is a preorder. `reply_remove`'s donation rewrites the +SC's thread to the caller, which is `BlockedOnReply`, hence outside the release queue (`RlsRunA`); `Rv'A tp` carries +that. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3DelFinIpcA +import Fl4ProofsA64.P3.SInvXA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Classical + +noncomputable section + +/-- **The reply frame** (module docstring). -/ +structure RvA (σ σ' : State Unit) : Prop where + time : σ'.curTime = σ.curTime + last : σ'.machineState.lastMachineTime = σ.machineState.lastMachineTime + cur : σ'.curThread = σ.curThread + csc : σ'.curSc = σ.curSc + cons : σ'.consumedTime = σ.consumedTime + rel : ∀ t ∈ σ'.releaseQueue, t ∈ σ.releaseQueue + sc : ∀ y k, σ.kheap σ.curSc = some (.schedContext y k) → ∃ y', σ'.kheap σ.curSc = some (.schedContext y' k) ∧ + y'.scRefills = y.scRefills ∧ y'.scRefillMax = y.scRefillMax ∧ + (y'.scTcb = y.scTcb ∨ ∀ o, y'.scTcb = some o → o ∉ σ'.releaseQueue) + act : σ'.schedulerAction = σ.schedulerAction ∨ σ'.schedulerAction = .chooseNewThread + st : σ'.schedulerAction = .resumeCurThread → ∀ x', σ'.kheap σ.curThread = some (.tcb x') → + ∃ x, σ.kheap σ.curThread = some (.tcb x) ∧ x'.tcbState = x.tcbState + off : CtOffA σ.curThread σ → CtOffA σ.curThread σ' + ctSc : CtScKA σ.curThread σ σ' + +instance : PreO RvA where + refl σ := ⟨rfl, rfl, rfl, rfl, rfl, fun _ h => h, fun y k h => ⟨y, h, rfl, rfl, Or.inl rfl⟩, Or.inl rfl, + fun _ x h => ⟨x, h, rfl⟩, id, CtScKA.refl _ _⟩ + trans {a b c} h1 h2 := by + refine ⟨h2.time.trans h1.time, h2.last.trans h1.last, h2.cur.trans h1.cur, h2.csc.trans h1.csc, + h2.cons.trans h1.cons, fun t h => h1.rel _ (h2.rel _ h), fun y k hy => ?_, ?_, fun e x hx => ?_, + CtOffA.trans h1.cur h1.off h2.off, h1.ctSc.trans (h1.cur ▸ h2.ctSc)⟩ + · obtain ⟨y1, k1, e1, e2, e3⟩ := h1.sc y k hy + obtain ⟨y2, k2, f1, f2, f3⟩ := h2.sc y1 k (by rw [h1.csc]; exact k1) + rw [h1.csc] at k2 + refine ⟨y2, k2, f1.trans e1, f2.trans e2, ?_⟩ + rcases f3 with f3 | f3 + · rcases e3 with e3 | e3 + · exact Or.inl (f3.trans e3) + · exact Or.inr fun o ho hm => e3 o (f3 ▸ ho) (h2.rel _ hm) + · exact Or.inr f3 + · rcases h2.act with f | f + · rw [f]; exact h1.act + · exact Or.inr f + · have eb : b.schedulerAction = .resumeCurThread := by + rcases h2.act with f | f + · exact f.symm.trans e + · rw [f] at e; cases e + obtain ⟨x1, hx1, s1⟩ := h2.st e x (by rw [h1.cur]; exact hx) + rw [h1.cur] at hx1 + obtain ⟨x0, hx0, s0⟩ := h1.st eb x1 hx1 + exact ⟨x0, hx0, s1.trans s0⟩ + +theorem DSkB.rv {σ σ' : State Unit} (h : DSkB σ σ') : RvA σ σ' := + ⟨h.time, h.last, h.cur, h.csc, h.cons, fun _ m => h.rel ▸ m, fun y k hy => by + obtain ⟨y', a, b, c, d⟩ := h.sc y k hy + exact ⟨y', a, b, c, Or.inl d⟩, Or.inl h.act, fun _ x hx => h.tcb _ _ hx, h.off, h.ctSc⟩ + +/-- A reply-frame step is a `DelStepA` step. -/ +theorem RvA.step {σ σ' : State Unit} (h : CurScA σ) (K : RvA σ σ') (ch : ChKA σ σ') : DelStepCA σ σ' := by + refine ⟨⟨K.cur, K.time, K.last, K.csc, K.cons, K.rel, fun y' k hy => ?_, K.act, K.st, K.off, K.ctSc⟩, ch⟩ + obtain ⟨y, j, hyj⟩ := h + obtain ⟨y2, h2, e1, e2, e3⟩ := K.sc y j hyj + rw [h2] at hy + obtain ⟨rfl, rfl⟩ := KernelObject.schedContext.inj (Option.some.inj hy) + refine ⟨y, hyj, ?_⟩ + rcases e3 with e3 | e3 + · exact Or.inl ⟨e1, e2, e3⟩ + · exact Or.inr (Or.inr ⟨e1, e2, e3⟩) + +abbrev RPresA {α : Type} (m : SMonad Unit α) : Prop := PresR RvA m + +theorem DSkA.rv {σ σ' : State Unit} (h : DSkA σ σ') : RvA σ σ' := DSkB.rv h.toDSkB + +theorem rv_ofSkB {α : Type} {m : SMonad Unit α} (h : PresR DSkB m) : RPresA m := PresR.monoR h fun _ _ => DSkB.rv + +theorem rv_ofSk {α : Type} {m : SMonad Unit α} (h : PresR DSkA m) : RPresA m := + PresR.monoR h fun _ _ k => DSkB.rv k.toDSkB +theorem rv_ofEq {α : Type} {m : SMonad Unit α} (h : PresR (@Eq (State Unit)) m) : RPresA m := PresR.ofEq' h + +theorem setReply_skA (p : ObjRef) (a : Reply) : PresR DSkA (setReply (Z := Unit) p a) := by + intro σ r hr + obtain ⟨y, hk, hr2⟩ := setSimpleKo_memA KernelObject.reply p a hr + rw [hr2] + exact DSkA.upd hk (fun y k h => by cases h) (fun y k h => by cases h) (fun x' h => by cases h) + +theorem setEndpoint_skA (p : ObjRef) (a : Endpoint) : PresR DSkA (setEndpoint (Z := Unit) p a) := by + intro σ r hr + obtain ⟨y, hk, hr2⟩ := setSimpleKo_memA KernelObject.endpoint p a hr + rw [hr2] + exact DSkA.upd hk (fun y k h => by cases h) (fun y k h => by cases h) (fun x' h => by cases h) + +theorem getReply_eqPresR (p : ObjRef) : PresR (@Eq (State Unit)) (getReply (Z := Unit) p) := by + unfold getReply getSimpleKo + refine PresR.bind (getObject_eqPres p) (fun _ => ?_) + eqpres + +theorem getEndpoint_eqPresR (p : ObjRef) : PresR (@Eq (State Unit)) (getEndpoint (Z := Unit) p) := by + unfold getEndpoint getSimpleKo + refine PresR.bind (getObject_eqPres p) (fun _ => ?_) + eqpres + +theorem setSchedulerAction_rvA : RPresA (setSchedulerAction (Z := Unit) .chooseNewThread) := by + unfold setSchedulerAction + refine PresR.modify _ fun σ => ?_ + exact ⟨rfl, rfl, rfl, rfl, rfl, fun _ h => h, fun y k h => ⟨y, h, rfl, rfl, Or.inl rfl⟩, Or.inr rfl, + fun e => (by cases e), CtOffA.keep rfl (Or.inr fun _ h => by cases h), CtScKA.ofEq rfl⟩ + +theorem tcbSchedDequeue_rvA (t : ObjRef) : RPresA (tcbSchedAction (Z := Unit) tcbSchedDequeue t) := by + intro σ r hr + unfold tcbSchedAction getTcbQueue setTcbQueue at hr + obtain ⟨d, h1, hr⟩ := mem_bindA.1 hr + have e1 : σ = d.2 := threadGet_eqPres _ _ σ d h1 + obtain ⟨pr, h2, hr⟩ := mem_bindA.1 hr + have e2 : d.2 = pr.2 := threadGet_eqPres _ _ d.2 pr h2 + rw [← e2, ← e1] at hr + obtain ⟨q, hq, hr⟩ := mem_bindA.1 hr + obtain ⟨q0, hq0, hq⟩ := mem_bindA.1 hq + obtain rfl : q0 = (σ.readyQueues, σ) := hq0 + obtain rfl : q = (σ.readyQueues d.1 pr.1, σ) := hq + obtain ⟨u, hu, hr⟩ := mem_bindA.1 hr + obtain rfl : u = (tcbSchedDequeue t (σ.readyQueues d.1 pr.1), σ) := hu + obtain rfl : r = ((), _) := hr + refine ⟨rfl, rfl, rfl, rfl, rfl, fun _ h => h, fun y k h => ⟨y, h, rfl, rfl, Or.inl rfl⟩, Or.inl rfl, + fun _ x h => ⟨x, h, rfl⟩, fun h => ⟨fun d' p' hm => ?_, h.2⟩, CtScKA.ofEq rfl⟩ + dsimp only at hm + split at hm + · rename_i c + obtain ⟨rfl, rfl⟩ := c + exact h.1 _ _ (List.mem_of_mem_filter hm) + · exact h.1 _ _ hm + +theorem tcbReleaseRemove_rvA (t : ObjRef) : RPresA (tcbReleaseRemove (Z := Unit) t) := by + unfold tcbReleaseRemove setReleaseQueue + intro σ r hr + obtain ⟨q, hq, hr⟩ := mem_bindA.1 hr + obtain rfl : q = (σ.releaseQueue, σ) := hq + obtain ⟨u, hu, hr⟩ := mem_bindA.1 hr + have k : RvA σ u.2 ∧ u.2.releaseQueue = σ.releaseQueue := by + unfold NonDet.when at hu + split at hu + · obtain rfl : u = ((), _) := hu + exact ⟨DSkA.rv (DSkA.of_kheap ⟨rfl, rfl, rfl, rfl, rfl, rfl⟩ rfl rfl rfl), rfl⟩ + · obtain rfl : u = ((), σ) := hu + exact ⟨PreO.refl σ, rfl⟩ + obtain ⟨w, hw, hr⟩ := mem_bindA.1 hr + obtain rfl : w = (tcbSchedDequeue t σ.releaseQueue, u.2) := hw + obtain rfl : r = ((), _) := hr + refine PreO.trans k.1 ⟨rfl, rfl, rfl, rfl, rfl, fun x hx => ?_, fun y j h => ⟨y, h, rfl, rfl, Or.inl rfl⟩, + Or.inl rfl, fun _ x h => ⟨x, h, rfl⟩, CtOffA.keep rfl (Or.inl rfl), CtScKA.ofEq rfl⟩ + change x ∈ tcbSchedDequeue t σ.releaseQueue at hx + rw [k.2] + unfold tcbSchedDequeue at hx + exact List.mem_of_mem_filter hx + +macro "rvres" : tactic => `(tactic| + repeat' (first + | exact setSchedulerAction_rvA + | exact tcbSchedDequeue_rvA _ + | exact tcbReleaseRemove_rvA _ + | exact rv_ofSk (setReply_skA _ _) + | exact rv_ofSk (setEndpoint_skA _ _) + | exact rv_ofSk (setNotification_skA _ _) + | (refine rv_ofSk ?_; apply threadSet_skA <;> intro _ <;> first | rfl | exact Or.inl rfl) + | exact rv_ofSk (updateSchedContext_skA _ _ (fun _ => ⟨rfl, rfl, rfl⟩)) + | exact rv_ofEq (getReply_eqPresR _) + | exact rv_ofEq (getEndpoint_eqPresR _) + | exact rv_ofEq (getNotification_eqPres _) + | exact rv_ofEq (getSchedContext_eqPres _) + | exact rv_ofEq (threadGet_eqPres _ _) + | exact PresR.pure _ + | exact PresR.gets _ + | exact PresR.getsThe _ + | exact PresR.assertOpt _ + | exact PresR.assert _ + | exact PresR.fail + | refine PresR.bind_pure _ _ ?_ + | refine PresR.bind ?_ (fun _ => ?_) + | refine PresR.when ?_ + | refine PresR.unless' ?_ + | refine PresR.ite' ?_ ?_ + | dsimp only + | split)) + +theorem rescheduleRequired_rvA : RPresA (rescheduleRequired (Z := Unit)) := by + intro σ r hr + obtain ⟨Q, e, m⟩ := rescheduleRequired_shapeA hr + rw [e] + exact ⟨rfl, rfl, rfl, rfl, rfl, fun _ h => h, fun y k h => ⟨y, h, rfl, rfl, Or.inl rfl⟩, Or.inr rfl, + fun h => (by cases h), CtOffA.resched m, CtScKA.ofEq rfl⟩ + +/-- Setting a thread `Inactive`. -/ +theorem setInactive_rvA (t : ObjRef) : RPresA (setThreadState (Z := Unit) t .inactive) := by + intro σ r hr + unfold setThreadState at hr + obtain ⟨a, ha, h2⟩ := mem_bindA.1 hr + obtain ⟨x, hx, ea⟩ := threadSet_memA _ t ha + unfold setThreadStateAct at h2 + obtain ⟨c, hc, h2⟩ := mem_bindA.1 h2 + obtain rfl : c = (a.2.curThread, a.2) := hc + obtain ⟨d, hd, h2⟩ := mem_bindA.1 h2 + obtain rfl : d = (a.2.schedulerAction, a.2) := hd + obtain ⟨e, he, h2⟩ := mem_bindA.1 h2 + obtain rfl : e = (schedulable t a.2, a.2) := he + have hk : a.2.kheap = Function.update σ.kheap t (some (.tcb { x with tcbState := .inactive })) := by rw [ea] + have hs : schedulable t a.2 = false := by + unfold schedulable + rw [decide_eq_false_iff_not] + rintro ⟨h, -⟩ + have et : tcbsOf a.2 t = some { x with tcbState := .inactive } := by + simp [tcbsOfKh, optMap, tcbOf, hk] + simp [optPred, optMapSome, optMap, et, runnable] at h + have base : ∀ τ : State Unit, τ.kheap = a.2.kheap → τ.curTime = σ.curTime → + τ.machineState.lastMachineTime = σ.machineState.lastMachineTime → τ.curThread = σ.curThread → + τ.curSc = σ.curSc → τ.consumedTime = σ.consumedTime → τ.releaseQueue = σ.releaseQueue → + (τ.schedulerAction = σ.schedulerAction ∨ τ.schedulerAction = .chooseNewThread) → + (τ.schedulerAction = .resumeCurThread → t ≠ σ.curThread) → τ.readyQueues = σ.readyQueues → RvA σ τ := by + intro τ e0 e1 e2 e3 e4 e5 e6 e7 e8 e9 + refine ⟨e1, e2, e3, e4, e5, fun _ h => e6 ▸ h, fun y k hy => ?_, e7, fun er x' hx' => ?_, + CtOffA.keep e9 (e7.imp id fun c u h => by rw [c] at h; cases h), + CtScKA.ofUpd (o' := .tcb { x with tcbState := .inactive }) (p := t) (e0.trans hk) + (fun z e _ => ⟨x, hx, Or.inl (by rw [← KernelObject.tcb.inj e])⟩)⟩ + · have ne : σ.curSc ≠ t := fun h => by rw [h, hx] at hy; cases hy + exact ⟨y, by rw [e0, hk, Function.update_of_ne ne]; exact hy, rfl, rfl, Or.inl rfl⟩ + · rw [e0, hk, Function.update_of_ne (e8 er).symm] at hx' + exact ⟨x', hx', rfl⟩ + unfold NonDet.when at h2 + split at h2 + · unfold setSchedulerAction at h2 + obtain rfl : r = ((), _) := h2 + exact base _ rfl (by rw [ea]) (by rw [ea]) (by rw [ea]) (by rw [ea]) (by rw [ea]) (by rw [ea]) (Or.inr rfl) + (fun h => by cases h) (by rw [ea]) + · rename_i hn + obtain rfl : r = ((), a.2) := h2 + refine base _ rfl (by rw [ea]) (by rw [ea]) (by rw [ea]) (by rw [ea]) (by rw [ea]) (by rw [ea]) + (Or.inl (by rw [ea])) (fun h e => hn ?_) (by rw [ea]) + exact ⟨e.trans (by rw [ea]), h, by simp [hs]⟩ + +theorem testReschedule_rvA (t : ObjRef) : RPresA (testReschedule (Z := Unit) t) := by + unfold testReschedule + repeat' (first + | exact rescheduleRequired_rvA + | exact PresR.gets _ + | exact PresR.pure _ + | refine PresR.bind ?_ (fun _ => ?_) + | refine PresR.when ?_ + | dsimp only) + +theorem replyUnlinkTcb_rvA (t r : ObjRef) : RPresA (replyUnlinkTcb (Z := Unit) t r) := by + unfold replyUnlinkTcb setReplyObjRef updateSkObjRef getThreadState + refine PresR.bind (rv_ofEq (getReply_eqPresR _)) fun _ => PresR.bind (PresR.assert _) fun _ => + PresR.bind (rv_ofEq (threadGet_eqPres _ _)) fun _ => PresR.bind (PresR.assert _) fun _ => + PresR.bind ?_ fun _ => setInactive_rvA _ + exact PresR.bind (rv_ofEq (getReply_eqPresR _)) fun _ => rv_ofSk (setReply_skA _ _) + +theorem tcbEpDequeue_rvA (t e : ObjRef) : RPresA (tcbEpDequeue (Z := Unit) t e) := by + unfold tcbEpDequeue + rvres + +theorem tcbNtfnDequeue_rvA (t n : ObjRef) : RPresA (tcbNtfnDequeue (Z := Unit) t n) := by + unfold tcbNtfnDequeue + rvres + +theorem replyRemoveTcb_rvA (t r : ObjRef) : RPresA (replyRemoveTcb (Z := Unit) t r) := by + unfold replyRemoveTcb getThreadState setReplyObjRef updateSkObjRef getReplyObjRef + repeat' (first + | exact replyUnlinkTcb_rvA _ _ + | exact rv_ofSk (setReply_skA _ _) + | exact rv_ofSk (updateSchedContext_skA _ _ (fun _ => ⟨rfl, rfl, rfl⟩)) + | exact rv_ofEq (getReply_eqPresR _) + | exact rv_ofEq (getSchedContext_eqPres _) + | exact rv_ofEq (threadGet_eqPres _ _) + | exact PresR.pure _ + | exact PresR.gets _ + | exact PresR.assert _ + | refine PresR.bind ?_ (fun _ => ?_) + | dsimp only [NonDet.liftM] + | unfold getSkObjRef + | split) + +theorem cancelIpc_rvA (t : ObjRef) : RPresA (cancelIpc (Z := Unit) t) := by + unfold cancelIpc getThreadState + refine PresR.bind (rv_ofEq (threadGet_eqPres _ _)) fun s => + PresR.bind (rv_ofSkB (threadSet_skB (fun x => { x with tcbFault := none }) (fun _ => rfl) (fun _ => Or.inl rfl) _)) fun _ => ?_ + have bc : ∀ s o, RPresA (blockedCancelIpc (Z := Unit) s t o) := fun s o => by + unfold blockedCancelIpc getBlockingObject + refine PresR.bind (PresR.assertOpt _) fun _ => PresR.bind (tcbEpDequeue_rvA _ _) fun _ => ?_ + cases o with + | none => + dsimp only + first | exact setInactive_rvA _ | exact PresR.bind (PresR.pure _) fun _ => setInactive_rvA _ + | some r => + dsimp only + exact PresR.bind (replyUnlinkTcb_rvA _ _) fun _ => setInactive_rvA _ + cases s with + | blockedOnSend a b => exact bc _ _ + | blockedOnReceive a b c => exact bc _ _ + | blockedOnNotification n => + unfold cancelSignal + exact PresR.bind (tcbNtfnDequeue_rvA _ _) fun _ => setInactive_rvA _ + | blockedOnReply r => exact replyRemoveTcb_rvA _ _ + | _ => exact PresR.pure _ + +/-- The reply frame from states with `tp` outside the release queue and not the current thread. -/ +def Rv'A (tp : ObjRef) (σ σ' : State Unit) : Prop := tp ∉ σ.releaseQueue → tp ≠ σ.curThread → RvA σ σ' + +instance (tp : ObjRef) : PreO (Rv'A tp) where + refl σ := fun _ _ => PreO.refl σ + trans h1 h2 := fun n c => PreO.trans (h1 n c) (h2 (fun m => n ((h1 n c).rel _ m)) (by rw [(h1 n c).cur]; exact c)) + +theorem rv'_of {α : Type} {tp : ObjRef} {m : SMonad Unit α} (h : RPresA m) : PresR (Rv'A tp) m := + PresR.monoR h fun _ _ k _ _ => k + +/-- The donation's write of the receiver's SC pointer. -/ +theorem setTcbSc_rvA (tp p : ObjRef) : + PresR (Rv'A tp) (threadSet (Z := Unit) (tcbSchedContextUpdate fun _ => some p) tp) := by + intro σ r hr _ hc + obtain ⟨x, hx, hr2⟩ := threadSet_memA _ tp hr + rw [hr2] + refine ⟨rfl, rfl, rfl, rfl, rfl, fun _ h => h, fun y k hy => ?_, Or.inl rfl, fun _ x' hx' => ?_, id, + CtScKA.ofUpd (p := tp) rfl (fun _ _ e => absurd e.symm hc)⟩ + · have ne : σ.curSc ≠ tp := fun e => by rw [e, hx] at hy; cases hy + exact ⟨y, by show Function.update σ.kheap tp _ σ.curSc = _; rw [Function.update_of_ne ne]; exact hy, + rfl, rfl, Or.inl rfl⟩ + · change Function.update σ.kheap tp _ σ.curThread = _ at hx' + rw [Function.update_of_ne hc.symm] at hx' + exact ⟨x', hx', rfl⟩ + +theorem setScTcb_rvA (tp p : ObjRef) : PresR (Rv'A tp) (setScObjRef (Z := Unit) scTcbUpdate p (some tp)) := by + intro σ r hr hn _ + unfold setScObjRef updateSchedContext at hr + obtain ⟨y1, hy1, h⟩ := hr + have hy1' : y1 ∈ (NonDet.assertOpt (σ.kheap p) σ).1 := hy1 + cases hk : σ.kheap p with + | none => rw [hk] at hy1'; exact absurd hy1' (Set.notMem_empty _) + | some ko => + rw [hk] at hy1' + have e1 : y1 = (ko, σ) := hy1' + subst e1 + cases ko with + | schedContext sc n => + obtain ⟨ko', hko, hr2⟩ := setObject_memA h + rw [hr2] + refine ⟨rfl, rfl, rfl, rfl, rfl, fun _ h => h, fun z k hz => ?_, Or.inl rfl, fun _ x' hx' => ?_, id, + CtScKA.ofUpd (p := p) rfl (fun x' e _ => by cases e)⟩ + · by_cases e : σ.curSc = p + · rw [e, hk] at hz + obtain ⟨rfl, rfl⟩ := KernelObject.schedContext.inj (Option.some.inj hz) + refine ⟨scTcbUpdate (fun _ => some tp) sc, by + show Function.update σ.kheap p _ σ.curSc = _; rw [e, Function.update_self], rfl, rfl, Or.inr fun o ho => ?_⟩ + obtain rfl : tp = o := Option.some.inj ho + exact hn + · exact ⟨z, by show Function.update σ.kheap p _ σ.curSc = _; rw [Function.update_of_ne e]; exact hz, + rfl, rfl, Or.inl rfl⟩ + · change Function.update σ.kheap p _ σ.curThread = _ at hx' + by_cases e : σ.curThread = p + · rw [e, Function.update_self] at hx'; cases hx' + · rw [Function.update_of_ne e] at hx'; exact ⟨x', hx', rfl⟩ + | _ => exact absurd h (Set.notMem_empty _) + +theorem schedContextDonate_rvA (p tp : ObjRef) : PresR (Rv'A tp) (schedContextDonate (Z := Unit) p tp) := by + unfold schedContextDonate getScObjRef + refine PresR.bind (rv'_of ?_) fun o => PresR.bind (rv'_of ?_) fun _ => + PresR.bind (setScTcb_rvA _ _) fun _ => setTcbSc_rvA _ _ + · rvres + · refine PresR.when (PresR.bind (PresR.assertOpt _) fun f => PresR.bind (tcbSchedDequeue_rvA _) fun _ => + PresR.bind (tcbReleaseRemove_rvA _) fun _ => PresR.bind (rv_ofSk (threadSet_skA (tcbSchedContextUpdate fun _ => none) (fun _ => rfl) (fun _ => Or.inr rfl) (fun _ => rfl) _)) fun _ => + testReschedule_rvA _) + +theorem replyRemove_rvA (c r : ObjRef) : PresR (Rv'A c) (replyRemove (Z := Unit) c r) := by + have un : ∀ p, RPresA (replyUnlinkSc (Z := Unit) p r) := fun p => by + unfold replyUnlinkSc setReplyObjRef updateSkObjRef + dsimp only [NonDet.liftM] + rvres + unfold replyRemove getTcbObjRef + refine PresR.bind (rv'_of (rv_ofEq (getReply_eqPresR _))) fun _ => PresR.bind (rv'_of (PresR.assert _)) fun _ => + PresR.bind (rv'_of (PresR.gets _)) fun o => ?_ + cases o with + | none => + dsimp only + exact rv'_of (replyUnlinkTcb_rvA _ _) + | some p => + dsimp only + have g : RPresA (NonDet.liftM SchedContext.scReplies (getSchedContext (Z := Unit) p)) := by + dsimp only [NonDet.liftM] + rvres + exact PresR.bind (rv'_of g) fun _ => PresR.bind (rv'_of (rv_ofEq (threadGet_eqPres _ _))) fun _ => + PresR.bind (rv'_of (un _)) fun _ => PresR.bind (PresR.when (schedContextDonate_rvA _ _)) fun _ => + rv'_of (replyUnlinkTcb_rvA _ _) + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3DelFinScA.lean b/proofs/Fl4ProofsA64/P3/Stage3DelFinScA.lean new file mode 100644 index 000000000..d35ec79d1 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3DelFinScA.lean @@ -0,0 +1,375 @@ +/- +P3 (AArch64, A64-P3 DelBodyA slice 2): **the spec finaliser's body frame, SchedContext** (`finBodySpec_scA`), and the +frame `RsA` the SchedContext and Thread finalisers share. + +`RsA` is the reply frame `RvA` with a second way for the current SC: it may be deactivated (`sched_context_set_inactive` +zeroes `scRefills` and `scRefillMax`; a non-idle current SC only). It is a preorder, so the walks of +`Stage3DelFinReplyA` (`PresR`) lift to it: a `RvA` / `DSkA` / `DlA` step is an `RsA` step. + +The side condition of the SchedContext kind (`ScPrA`) reads the cap: it names no idle SC (`sched_context_set_inactive` +is not guarded; the idle SC has no cap, `JSyscallA`'s `ciSc` / `scOff`). + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3DelFinReplyA +import Fl4ProofsA64.P3.Stage3DelFinIrqA +import Fl4ProofsA64.P3.Stage3DelFinVcpuA +import Fl4ProofsA64.P3.Stage3ChPrimA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Classical + +noncomputable section + +/-- **The SchedContext / Thread frame** (module docstring). -/ +structure RsA (σ σ' : State Unit) : Prop where + time : σ'.curTime = σ.curTime + last : σ'.machineState.lastMachineTime = σ.machineState.lastMachineTime + cur : σ'.curThread = σ.curThread + csc : σ'.curSc = σ.curSc + cons : σ'.consumedTime = σ.consumedTime + rel : ∀ t ∈ σ'.releaseQueue, t ∈ σ.releaseQueue + sc : ∀ y k, σ.kheap σ.curSc = some (.schedContext y k) → ∃ y', σ'.kheap σ.curSc = some (.schedContext y' k) ∧ + ((y'.scRefills = y.scRefills ∧ y'.scRefillMax = y.scRefillMax ∧ + (y'.scTcb = y.scTcb ∨ ∀ o, y'.scTcb = some o → o ∉ σ'.releaseQueue)) ∨ + (σ.curSc ≠ idleScPtr ∧ scActive y' = false)) + act : σ'.schedulerAction = σ.schedulerAction ∨ σ'.schedulerAction = .chooseNewThread + st : σ'.schedulerAction = .resumeCurThread → ∀ x', σ'.kheap σ.curThread = some (.tcb x') → + ∃ x, σ.kheap σ.curThread = some (.tcb x) ∧ x'.tcbState = x.tcbState + off : CtOffA σ.curThread σ → CtOffA σ.curThread σ' + ctSc : CtScKA σ.curThread σ σ' + /-- (chg) the frame that keeps the current SC chargeable. -/ + ch : ChKA σ σ' + +theorem scActive_ofMax {u v : SchedContext} (e : u.scRefillMax = v.scRefillMax) : scActive u = scActive v := by + unfold scActive; rw [e] + +instance : PreO RsA where + refl σ := ⟨rfl, rfl, rfl, rfl, rfl, fun _ h => h, + fun y k h => ⟨y, h, Or.inl ⟨rfl, rfl, Or.inl rfl⟩⟩, Or.inl rfl, fun _ x h => ⟨x, h, rfl⟩, id, CtScKA.refl _ _, ChKA.refl _⟩ + trans {a b c} h1 h2 := by + refine ⟨h2.time.trans h1.time, h2.last.trans h1.last, h2.cur.trans h1.cur, h2.csc.trans h1.csc, + h2.cons.trans h1.cons, fun t h => h1.rel _ (h2.rel _ h), fun y k hy => ?_, ?_, fun e x hx => ?_, + CtOffA.trans h1.cur h1.off h2.off, h1.ctSc.trans (h1.cur ▸ h2.ctSc), ChKA.trans h1.ch h2.ch⟩ + · obtain ⟨y1, k1, c1⟩ := h1.sc y k hy + obtain ⟨y2, k2, c2⟩ := h2.sc y1 k (by rw [h1.csc]; exact k1) + rw [h1.csc] at k2 c2 + refine ⟨y2, k2, ?_⟩ + rcases c1 with ⟨e1, e2, e3⟩ | ⟨hn, hf⟩ + · rcases c2 with ⟨f1, f2, f3⟩ | ⟨hn2, hf2⟩ + · refine Or.inl ⟨f1.trans e1, f2.trans e2, ?_⟩ + rcases f3 with f3 | f3 + · rcases e3 with e3 | e3 + · exact Or.inl (f3.trans e3) + · exact Or.inr fun o ho hm => e3 o (f3 ▸ ho) (h2.rel _ hm) + · exact Or.inr f3 + · exact Or.inr ⟨hn2, hf2⟩ + · rcases c2 with ⟨f1, f2, f3⟩ | ⟨hn2, hf2⟩ + · exact Or.inr ⟨hn, by rw [scActive_ofMax f2]; exact hf⟩ + · exact Or.inr ⟨hn, hf2⟩ + · rcases h2.act with f | f + · rw [f]; exact h1.act + · exact Or.inr f + · have eb : b.schedulerAction = .resumeCurThread := by + rcases h2.act with f | f + · exact f.symm.trans e + · rw [f] at e; cases e + obtain ⟨x1, hx1, s1⟩ := h2.st e x (by rw [h1.cur]; exact hx) + rw [h1.cur] at hx1 + obtain ⟨x0, hx0, s0⟩ := h1.st eb x1 hx1 + exact ⟨x0, hx0, s1.trans s0⟩ + +/-- A reply-frame step is an `RsA` step. -/ +theorem RvA.rs {σ σ' : State Unit} (h : RvA σ σ') (ch : ChKA σ σ') : RsA σ σ' := + ⟨h.time, h.last, h.cur, h.csc, h.cons, h.rel, fun y k hy => by + obtain ⟨y', a, b, c, d⟩ := h.sc y k hy + exact ⟨y', a, Or.inl ⟨b, c, d⟩⟩, h.act, h.st, h.off, h.ctSc, ch⟩ + +theorem _root_.Fl4ProofsA64.P3.DlA.rs {σ σ' : State Unit} (h : DlA σ σ') : RsA σ σ' := + ⟨h.time, h.last, h.cur, h.csc, h.cons, fun t ht => h.rel ▸ ht, fun y k hy => + ⟨y, h.scf _ _ _ hy, Or.inl ⟨rfl, rfl, Or.inl rfl⟩⟩, Or.inl h.act, fun _ x hx => h.tcb _ _ hx, h.off, h.ctSc, h.ch⟩ + +theorem _root_.Fl4ProofsA64.P3.DcA.rs {σ σ' : State Unit} (h : DcA σ σ') : RsA σ σ' := + ⟨h.time, h.last, h.cur, h.csc, h.cons, fun t ht => h.rel ▸ ht, fun y k hy => + ⟨y, h.scf _ _ _ hy, Or.inl ⟨rfl, rfl, Or.inl rfl⟩⟩, Or.inl h.act, fun _ x hx => h.ct x hx, h.off, h.ctSc, h.ch⟩ + +/-- An `RsA` step is a `DelStepA` step (the current SC is an SC). -/ +theorem RsA.step {σ σ' : State Unit} (h : CurScA σ) (K : RsA σ σ') : DelStepCA σ σ' := by + refine ⟨⟨K.cur, K.time, K.last, K.csc, K.cons, K.rel, fun y' k hy => ?_, K.act, K.st, K.off, K.ctSc⟩, K.ch⟩ + obtain ⟨y, j, hyj⟩ := h + obtain ⟨y2, h2, c⟩ := K.sc y j hyj + rw [h2] at hy + obtain ⟨rfl, rfl⟩ := KernelObject.schedContext.inj (Option.some.inj hy) + refine ⟨y, hyj, ?_⟩ + rcases c with ⟨e1, e2, e3⟩ | hd + · rcases e3 with e3 | e3 + · exact Or.inl ⟨e1, e2, e3⟩ + · exact Or.inr (Or.inr ⟨e1, e2, e3⟩) + · exact Or.inr (Or.inl hd) + +theorem rs_of {α : Type} {m : SMonad Unit α} (h : RPresA m) (c : PresR ChKA m) : PresR RsA m := + fun σ r hr => RvA.rs (h σ r hr) (c σ r hr) + +theorem rs_ofSk {α : Type} {m : SMonad Unit α} (h : PresR DSkA m) : PresR RsA m := + rs_of (rv_ofSk h) fun σ r hr => (h σ r hr).ch + +theorem rs_ofEq {α : Type} {m : SMonad Unit α} (h : PresR (@Eq (State Unit)) m) : PresR RsA m := PresR.ofEq' h + +theorem rs_ofDl {α : Type} {m : SMonad Unit α} (h : NdDlA m) : PresR RsA m := fun σ r hr => (h σ r hr).rs + +/-- `update_sched_context` of the current SC keeps refills / max, and the thread unless it becomes `none`. -/ +theorem updateSchedContext_memA {p : ObjRef} {f : SchedContext → SchedContext} {σ : State Unit} {r : Unit × State Unit} + (hr : r ∈ (updateSchedContext (Z := Unit) p f σ).1) : + ∃ y n, σ.kheap p = some (.schedContext y n) ∧ + r.2 = { σ with kheap := Function.update σ.kheap p (some (.schedContext (f y) n)) } := by + obtain ⟨y1, hy1, h⟩ := hr + have hy1' : y1 ∈ (NonDet.assertOpt (σ.kheap p) σ).1 := hy1 + cases hk : σ.kheap p with + | none => rw [hk] at hy1'; exact absurd hy1' (Set.notMem_empty _) + | some ko => + rw [hk] at hy1' + have e1 : y1 = (ko, σ) := hy1' + subst e1 + cases ko with + | schedContext sc n => + obtain ⟨ko', hko, hr2⟩ := setObject_memA h + exact ⟨sc, n, rfl, hr2⟩ + | _ => exact absurd h (Set.notMem_empty _) + +/-- (chg) One SC object replaced, keeping its thread or dropping it, keeps `ChKA`. -/ +theorem _root_.Fl4ProofsA64.P3.ChKA.ofScUpd {σ : State Unit} {p : ObjRef} {y Y : SchedContext} {n : Nat} + (hk : σ.kheap p = some (.schedContext y n)) (hT : Y.scTcb = y.scTcb ∨ Y.scTcb = none) : + ChKA σ { σ with kheap := Function.update σ.kheap p (some (.schedContext Y n)) } := by + refine ⟨rfl, rfl, fun q x' hq => ?_, fun y' n' o hy ho => ?_⟩ + · change Function.update σ.kheap p _ q = _ at hq + by_cases e : q = p + · subst e; rw [Function.update_self] at hq; exact absurd (Option.some.inj hq) (by intro h; cases h) + · rw [Function.update_of_ne e] at hq; exact ⟨x', hq, id, id⟩ + · change Function.update σ.kheap p _ σ.curSc = _ at hy + by_cases e : σ.curSc = p + · rw [e, Function.update_self] at hy + obtain ⟨rfl, rfl⟩ := KernelObject.schedContext.inj (Option.some.inj hy) + rcases hT with h | h + · exact Or.inl ⟨y, n, by rw [e]; exact hk, by rw [← h]; exact ho⟩ + · rw [h] at ho; cases ho + · rw [Function.update_of_ne e] at hy + exact Or.inl ⟨y', n', hy, ho⟩ + +/-- One SC object replaced at `p` (the heap otherwise kept) is an `RsA` step when the new object keeps refills / max and +drops or keeps the thread, or (a non-idle SC) is deactivated. -/ +theorem RsA.ofUpd {σ : State Unit} {p : ObjRef} {y Y : SchedContext} {n : Nat} + (hk : σ.kheap p = some (.schedContext y n)) + (hR : (Y.scRefills = y.scRefills ∧ Y.scRefillMax = y.scRefillMax ∧ (Y.scTcb = y.scTcb ∨ Y.scTcb = none)) ∨ + (p ≠ idleScPtr ∧ scActive Y = false)) + (hT : Y.scTcb = y.scTcb ∨ Y.scTcb = none) : + RsA σ { σ with kheap := Function.update σ.kheap p (some (.schedContext Y n)) } := by + refine ⟨rfl, rfl, rfl, rfl, rfl, fun _ h => h, fun z k hz => ?_, Or.inl rfl, fun _ x' hx' => ?_, id, + CtScKA.ofUpd (p := p) rfl (fun x' e _ => by cases e), ChKA.ofScUpd hk hT⟩ + · by_cases e : σ.curSc = p + · rw [e, hk] at hz + obtain ⟨rfl, rfl⟩ := KernelObject.schedContext.inj (Option.some.inj hz) + refine ⟨Y, by show Function.update σ.kheap p _ σ.curSc = _; rw [e, Function.update_self], ?_⟩ + rcases hR with ⟨h1, h2, h3⟩ | ⟨h1, h2⟩ + · refine Or.inl ⟨h1, h2, ?_⟩ + rcases h3 with h | h + · exact Or.inl h + · exact Or.inr fun o ho => by rw [h] at ho; cases ho + · exact Or.inr ⟨e ▸ h1, h2⟩ + · exact ⟨z, by show Function.update σ.kheap p _ σ.curSc = _; rw [Function.update_of_ne e]; exact hz, + Or.inl ⟨rfl, rfl, Or.inl rfl⟩⟩ + · change Function.update σ.kheap p _ σ.curThread = _ at hx' + by_cases e : σ.curThread = p + · rw [e, Function.update_self] at hx'; cases hx' + · rw [Function.update_of_ne e] at hx'; exact ⟨x', hx', rfl⟩ + +theorem updateSchedContext_rsA (p : ObjRef) (f : SchedContext → SchedContext) + (hf : ∀ sc, (f sc).scRefills = sc.scRefills ∧ (f sc).scRefillMax = sc.scRefillMax ∧ + ((f sc).scTcb = sc.scTcb ∨ (f sc).scTcb = none)) : + PresR RsA (updateSchedContext (Z := Unit) p f) := by + intro σ r hr + obtain ⟨sc, n, hk, hr2⟩ := updateSchedContext_memA hr + rw [hr2] + exact RsA.ofUpd hk (Or.inl (hf sc)) (hf sc).2.2 + +/-- `sched_context_set_inactive` of a non-idle SC (a single unit: the intermediate states keep refills but not their +maximum). -/ +theorem schedContextSetInactive_rsA (p : ObjRef) (hni : p ≠ idleScPtr) : + PresR RsA (schedContextSetInactive (Z := Unit) p) := by + intro σ r hr + unfold schedContextSetInactive at hr + obtain ⟨a1, h1, hr⟩ := mem_bindA.1 hr + obtain ⟨a2, h2, hr⟩ := mem_bindA.1 hr + obtain ⟨a3, h3, hr⟩ := mem_bindA.1 hr + obtain ⟨y1, n1, k1, e1⟩ := updateSchedContext_memA h1 + obtain ⟨y2, n2, k2, e2⟩ := updateSchedContext_memA h2 + obtain ⟨y3, n3, k3, e3⟩ := updateSchedContext_memA h3 + obtain ⟨y4, n4, k4, e4⟩ := updateSchedContext_memA hr + rw [e1] at k2 + change Function.update σ.kheap p _ p = _ at k2 + rw [Function.update_self] at k2 + obtain ⟨rfl, rfl⟩ := KernelObject.schedContext.inj (Option.some.inj k2) + rw [e2] at k3 + change Function.update _ p _ p = _ at k3 + rw [Function.update_self] at k3 + obtain ⟨rfl, rfl⟩ := KernelObject.schedContext.inj (Option.some.inj k3) + rw [e3] at k4 + change Function.update _ p _ p = _ at k4 + rw [Function.update_self] at k4 + obtain ⟨rfl, rfl⟩ := KernelObject.schedContext.inj (Option.some.inj k4) + have hr2 : r.2 = { σ with kheap := Function.update σ.kheap p (some (.schedContext + (scSporadicUpdate (fun _ => false) (scBudgetUpdate (fun _ => 0) + (scRefillMaxUpdate (fun _ => 0) (scRefillsUpdate (fun _ => []) y1)))) n1)) } := by + rw [e4, e3, e2, e1] + simp only [Function.update_idem] + rw [hr2] + exact RsA.ofUpd k1 (Or.inr ⟨hni, rfl⟩) (Or.inl rfl) + +/-! ## `reply_from_kernel` and the thread ops in the `DlA` calculus -/ + +theorem NdDlA.zipWithMx' {α β γ : Type} {f : α → β → NonDet (State Unit) γ} (h : ∀ a b, NdDlA (f a b)) : + ∀ (l : List α) (l' : List β), NdDlA (NonDet.zipWithMx f l l') + | a :: l, b :: l' => NdDlA.nbind (h a b) fun _ => NdDlA.zipWithMx' h l l' + | [], _ => by unfold NonDet.zipWithMx; exact NdDlA.npure _ + | _ :: _, [] => by unfold NonDet.zipWithMx; exact NdDlA.npure _ + +theorem setMrs_dlA (t : ObjRef) (buf : Option ObjRef) (msgs : List Message) : NdDlA (setMrs (Z := Unit) t buf msgs) := by + unfold setMrs + intro σ r hr + obtain ⟨y1, hy1, hr⟩ := mem_bindA.1 hr + change y1 ∈ (NonDet.assertOpt (getTcb (Z := Unit) t σ) σ).1 at hy1 + cases hg : getTcb (Z := Unit) t σ with + | none => rw [hg] at hy1; exact absurd hy1 (Set.notMem_empty _) + | some x => + rw [hg] at hy1 + obtain rfl : y1 = (x, σ) := hy1 + obtain ⟨y2, hy2, hr⟩ := mem_bindA.1 hr + obtain rfl : y2 = (_, σ) := hy2 + obtain ⟨y3, hy3, hr⟩ := mem_bindA.1 hr + obtain rfl : y3 = (_, σ) := hy3 + obtain ⟨y4, hy4, hr⟩ := mem_bindA.1 hr + obtain ⟨ko, hko, e4⟩ := Sched.setObject_memA hy4 + have k4 : DlA σ y4.2 := by rw [e4]; exact dlA_updTcbA (Sched.getTcb_kheapA hg) rfl rfl rfl + refine dlA_trans k4 ?_ + refine (NdDlA.bind (NdDlA.pure _) (fun _ => ?_) : NdDlA _) y4.2 r hr + cases buf with + | none => exact NdDlA.npure _ + | some pptr => + exact NdDlA.nbind (NdDlA.zipWithMx' (fun _ _ => storeWordOffs_dlA _ _ _) _ _) fun _ => NdDlA.npure _ + +theorem lookupIpcBuffer_dlA (b : Bool) (t : ObjRef) : NdDlA (lookupIpcBuffer (Z := Unit) b t) := by + unfold lookupIpcBuffer + refine NdDlA.bind (threadGet_dlA _ _) fun _ => NdDlA.bind (NdDlA.pure _) fun _ => NdDlA.bind (getCap_dlA _) fun c => ?_ + split + · split <;> exact NdDlA.pure _ + · exact NdDlA.pure _ + +theorem replyFromKernel_dlA (t : ObjRef) (x : Data × List Data) : NdDlA (replyFromKernel (Z := Unit) t x) := by + unfold replyFromKernel + refine NdDlA.bind (NdDlA.pure _) fun p => ?_ + obtain ⟨l, mm⟩ := p + exact NdDlA.bind (lookupIpcBuffer_dlA _ _) fun _ => NdDlA.bind (asUser_dlA _ _) fun _ => + NdDlA.bind (setMrs_dlA _ _ _) fun _ => setMessageInfo_dlA _ _ + +macro "rsres" : tactic => `(tactic| + repeat' (first + | exact rs_of setSchedulerAction_rvA (setSchedulerAction_chA _) + | exact rs_of (tcbSchedDequeue_rvA _) (tcbSchedDequeue_chA _) + | exact rs_of (tcbReleaseRemove_rvA _) (tcbReleaseRemove_chA _) + | exact rs_of rescheduleRequired_rvA rescheduleRequired_chA + | exact rs_of (setInactive_rvA _) (setInactive_chA _) + | exact rs_of (cancelIpc_rvA _) (cancelIpc_chP _) + | exact rs_ofSk (setReply_skA _ _) + | exact rs_ofSk (setEndpoint_skA _ _) + | exact rs_ofSk (setNotification_skA _ _) + | (refine rs_ofSk ?_; apply threadSet_skA <;> intro _ <;> first | rfl | exact Or.inl rfl | exact Or.inr rfl) + | exact updateSchedContext_rsA _ _ (fun _ => ⟨rfl, rfl, Or.inl rfl⟩) + | exact updateSchedContext_rsA _ _ (fun _ => ⟨rfl, rfl, Or.inr rfl⟩) + | exact rs_ofEq (getReply_eqPresR _) + | exact rs_ofEq (getEndpoint_eqPresR _) + | exact rs_ofEq (getNotification_eqPres _) + | exact rs_ofEq (getSchedContext_eqPres _) + | exact rs_ofEq (threadGet_eqPres _ _) + | exact rs_ofDl (asUser_dlA _ _) + | exact rs_ofDl (replyFromKernel_dlA _ _) + | exact rs_ofSk (setNtfnObjRef_skA _ _ _) + | exact rs_ofSk (doUnbindNtfnSc_skA _ _) + | exact rs_ofSk (doUnbindNotification_skA _ _) + | exact PresR.pure _ + | exact PresR.gets _ + | exact PresR.getsThe _ + | exact PresR.assertOpt _ + | exact PresR.assert _ + | exact PresR.fail + | refine PresR.bind_pure _ _ ?_ + | refine PresR.bind ?_ (fun _ => ?_) + | refine PresR.when ?_ + | refine PresR.unless' ?_ + | refine PresR.ite' ?_ ?_ + | unfold setReplyObjRef + | unfold updateSkObjRef + | unfold getScObjRef + | unfold getTcbObjRef + | unfold setTcbObjRef + | unfold NonDet.maybeM + | unfold NonDet.liftM + | dsimp only + | split)) + +/-! ## The SchedContext finaliser -/ + +theorem completeYieldTo_rsA (t : ObjRef) : PresR RsA (completeYieldTo (Z := Unit) t) := by + unfold completeYieldTo setConsumed schedContextUpdateConsumed schedContextCancelYieldTo + rsres + +theorem unbindTcb_rsA (p : ObjRef) : PresR RsA (schedContextUnbindTcb (Z := Unit) p) := by + unfold schedContextUnbindTcb + rsres + +theorem unbindAllTcbs_rsA (p : ObjRef) : PresR RsA (schedContextUnbindAllTcbs (Z := Unit) p) := by + unfold schedContextUnbindAllTcbs + refine PresR.bind (rs_ofEq (getSchedContext_eqPres _)) fun _ => PresR.when (unbindTcb_rsA _) + +theorem scUnbindNtfn_rsA (p : ObjRef) : PresR RsA (schedContextUnbindNtfn (Z := Unit) p) := by + unfold schedContextUnbindNtfn + rsres + +theorem scUnbindReply_rsA (p : ObjRef) : PresR RsA (schedContextUnbindReply (Z := Unit) p) := by + unfold schedContextUnbindReply + rsres + +theorem scUnbindYieldFrom_rsA (p : ObjRef) : PresR RsA (schedContextUnbindYieldFrom (Z := Unit) p) := by + unfold schedContextUnbindYieldFrom + refine PresR.bind (rs_ofEq (getSchedContext_eqPres _)) fun sc => ?_ + cases sc.scYieldFrom with + | none => exact PresR.pure _ + | some t => exact completeYieldTo_rsA _ + +theorem finaliseCap_scRs (sc : ObjRef) (n' : Nat) (d : Bool) (hni : sc ≠ idleScPtr) : + PresR RsA (finaliseCap (Z := Unit) (.schedContextCap sc n') d) := by + unfold finaliseCap + refine PresR.bind (PresR.when (unbindAllTcbs_rsA _)) fun _ => PresR.bind (PresR.when (scUnbindNtfn_rsA _)) fun _ => + PresR.bind (PresR.when (scUnbindReply_rsA _)) fun _ => PresR.bind (PresR.when (scUnbindYieldFrom_rsA _)) fun _ => + PresR.bind (PresR.when (schedContextSetInactive_rsA _ hni)) fun _ => PresR.pure _ + +/-- The SchedContext kind's side condition: the current SC is an SC, and the cap names no idle SC. -/ +def ScPrA (σ : State Unit) (c : capability.Cap) : Prop := + CurScA σ ∧ ∀ p b, c = .SchedContext p b → specPtr p ≠ idleScPtr + +/-- **`FinBodySpecCA` for a SchedContext cap** (module docstring). -/ +theorem finBodySpec_scA {ct : ObjRef} {m : McState} : FinBodySpecCA fl4FinaliseCap .schedContext ct m ScPrA := by + intro c ac d σ x σ' _ hP hk hac hmem + obtain ⟨p, b, rfl⟩ : ∃ p b, c = .SchedContext p b := by + cases c <;> simp [finKindA] at hk + exact ⟨_, _, rfl⟩ + have hni := hP.2 p b rfl + obtain rfl : ac = absCapA (.SchedContext p b) := hac.symm + simp only [absCapA] at hmem + rw [fl4FinaliseCap_plainA (by intro h; exact h)] at hmem + have K := finaliseCap_scRs _ _ d hni σ (x, σ') hmem + exact K.step hP.1 + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3DelFinThrA.lean b/proofs/Fl4ProofsA64/P3/Stage3DelFinThrA.lean new file mode 100644 index 000000000..401f45166 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3DelFinThrA.lean @@ -0,0 +1,191 @@ +/- +P3 (AArch64, A64-P3 DelBodyA slice 2): **the spec finaliser's body frame, Thread** (`finBodySpec_threadA`). + +A final thread cap runs `fl4CancelRun t` and the end of the thread's VCPU run (a `DcA` step, the caller not `runningVM`: +`NotRunVmA`), then `unbind_notification`, `unbind_from_sc` (an SC unbind that can make the current thread unscheduled), `suspend` +(`cancel_ipc`, the restart-PC write, the dequeues, `Inactive`; it can end in `choose_new_thread`) and +`prepare_thread_delete` (VCPU dissociate and `fpu_release`, a `DlA` step). Every step is an `RsA` step +(`Stage3DelFinScA`), so the whole is. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3DelFinScA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Classical + +noncomputable section + +/-! ## `fpu_release` and the VCPU / TCB hooks in the `DlA` calculus -/ + +theorem MlkA.stateAssert' (P : MachineState → Prop) : MlkA (NonDet.stateAssert P) := + MlkA.ro fun _ _ h => by rw [h.2] + +theorem readFpuState_mlkA : MlkA readFpuState := by + unfold readFpuState; exact MlkA.bind (MlkA.stateAssert' _) fun _ => MlkA.gets _ + +theorem writeFpuState_mlkA (fpu : FpuState) : MlkA (writeFpuState fpu) := by + unfold writeFpuState + exact MlkA.bind (MlkA.stateAssert' _) fun _ => MlkA.bind (MlkA.modify _ fun _ => rfl) fun _ => MlkA.lift _ + +theorem enableFpu_mlkA : MlkA enableFpu := by + unfold enableFpu; exact MlkA.bind (MlkA.lift _) fun _ => MlkA.modify _ fun _ => rfl + +theorem disableFpu_mlkA : MlkA disableFpu := by + unfold disableFpu; exact MlkA.bind (MlkA.lift _) fun _ => MlkA.modify _ fun _ => rfl + +theorem archThreadSet_dlA (f : ArchTcb → ArchTcb) (t : ObjRef) : NdDlA (archThreadSet (Z := Unit) f t) := by + unfold archThreadSet + intro σ r hr + obtain ⟨y, hy, hr⟩ := mem_bindA.1 hr + change y ∈ (NonDet.assertOpt (getTcb (Z := Unit) t σ) σ).1 at hy + cases hg : getTcb (Z := Unit) t σ with + | none => rw [hg] at hy; exact absurd hy (Set.notMem_empty _) + | some x => + rw [hg] at hy + obtain rfl : y = (x, σ) := hy + obtain ⟨ko, -, e⟩ := Sched.setObject_memA hr + rw [e]; exact dlA_updTcbA (Sched.getTcb_kheapA hg) rfl rfl rfl + +theorem saveFpuState_dlA (t : ObjRef) : NdDlA (saveFpuState (Z := Unit) t) := by + unfold saveFpuState + exact NdDlA.bind (NdDlA.machine readFpuState_mlkA) fun _ => asUser_dlA _ _ + +theorem loadFpuState_dlA (t : ObjRef) : NdDlA (loadFpuState (Z := Unit) t) := by + unfold loadFpuState + exact NdDlA.bind (asUser_dlA _ _) fun _ => NdDlA.machine (writeFpuState_mlkA _) + +theorem setArmCurrentFpuOwner_dlA (o : Option ObjRef) : NdDlA (setArmCurrentFpuOwner (Z := Unit) o) := by + unfold setArmCurrentFpuOwner + refine NdDlA.bind (NdDlA.gets _) fun c => NdDlA.bind ?_ fun _ => + NdDlA.bind (NdDlA.modify fun _ => dlA_eqA rfl rfl rfl rfl rfl rfl rfl rfl rfl) fun _ => ?_ + · cases c with + | none => exact NdDlA.npure _ + | some x => exact archThreadSet_dlA (fun a => { a with tcbCurFpu := false }) x + · cases o with + | none => exact NdDlA.npure _ + | some x => exact archThreadSet_dlA (fun a => { a with tcbCurFpu := true }) x + +theorem switchLocalFpuOwner_dlA (o : Option ObjRef) : NdDlA (switchLocalFpuOwner (Z := Unit) o) := by + unfold switchLocalFpuOwner + refine NdDlA.bind (NdDlA.gets _) fun c => NdDlA.bind (NdDlA.machine enableFpu_mlkA) fun _ => + NdDlA.bind ?_ fun _ => ?_ + · cases c with + | none => exact NdDlA.npure _ + | some x => exact saveFpuState_dlA _ + · cases o with + | none => + dsimp only + exact NdDlA.bind (NdDlA.machine disableFpu_mlkA) fun _ => setArmCurrentFpuOwner_dlA _ + | some x => + dsimp only + exact NdDlA.bind (loadFpuState_dlA _) fun _ => setArmCurrentFpuOwner_dlA _ + +theorem fpuRelease_dlA (t : ObjRef) : NdDlA (fpuRelease (Z := Unit) t) := by + unfold fpuRelease + exact NdDlA.bind (NdDlA.gets _) fun _ => NdDlA.when (switchLocalFpuOwner_dlA _) + +theorem prepareThreadDelete_dlA (t : ObjRef) : NdDlA (prepareThreadDelete (Z := Unit) t) := by + unfold prepareThreadDelete + refine NdDlA.bind (archThreadGet_dlA _ _) fun o => ?_ + cases o with + | none => + dsimp only + first + | exact NdDlA.bind (NdDlA.pure _) fun _ => fpuRelease_dlA _ + | exact fpuRelease_dlA _ + | some v => + dsimp only + exact NdDlA.bind (dissociateVcpuTcb_dlA _ _) fun _ => fpuRelease_dlA _ + +theorem fl4CancelRun_dlA (t : ObjRef) : NdDlA (fl4CancelRun (Z := Unit) t) := by + unfold fl4CancelRun + refine NdDlA.bind (getRunVcpu_dlA _) fun o => ?_ + cases o with + | none => exact NdDlA.pure _ + | some vc => exact NdDlA.bind (fl4VcpuExtUpdate_dlA _ _) fun _ => setRunVcpu_dlA _ _ + +theorem NotRunVmA.dl {σ σ' : State Unit} (h : NotRunVmA σ) (k : DlA σ σ') : NotRunVmA σ' := by + intro x hx hs + obtain ⟨x0, h0, e⟩ := k.tcb _ x hx + rw [k.cur] at h0 + exact h x0 h0 (e.symm.trans hs) + +/-! ## The Thread finaliser -/ + +theorem unbindNotification_rsA (t : ObjRef) : PresR RsA (unbindNotification (Z := Unit) t) := by + unfold unbindNotification + rsres + +theorem unbindFromSc_rsA (t : ObjRef) : PresR RsA (unbindFromSc (Z := Unit) t) := by + unfold unbindFromSc + refine PresR.bind (rs_ofEq (threadGet_eqPres _ _)) fun o => ?_ + cases o with + | none => exact PresR.bind (PresR.pure _) fun _ => PresR.pure _ + | some p => exact PresR.bind (unbindTcb_rsA _) fun _ => scUnbindYieldFrom_rsA _ + +theorem suspend_rsA (t : ObjRef) : PresR RsA (Fl4SpecA64.suspend (Z := Unit) t) := by + unfold Fl4SpecA64.suspend getThreadState updateRestartPc schedContextCancelYieldTo + rsres + +theorem finaliseCap_threadRs (t : ObjRef) (d : Bool) : PresR RsA (finaliseCap (Z := Unit) (.threadCap t) d) := by + unfold finaliseCap + refine PresR.bind (PresR.when (unbindNotification_rsA _)) fun _ => PresR.bind (PresR.when (unbindFromSc_rsA _)) fun _ => + PresR.bind (PresR.when (suspend_rsA _)) fun _ => + PresR.bind (PresR.when (rs_ofDl (prepareThreadDelete_dlA _))) fun _ => PresR.pure _ + +/-- The thread cap's own hook before `finalise_cap`: the thread's VCPU run ends, and its VCPU's. -/ +def fl4ThreadPre (t : ObjRef) : SMonad Unit Unit := do + fl4CancelRun t + let vOpt ← archThreadGet ArchTcb.tcbVcpu t + match vOpt with + | some v => fl4VcpuEndRun v + | none => pure () + +theorem threadPre_rsA (t : ObjRef) (d : Bool) {σ : State Unit} (hn : NotRunVmA σ) {y : Unit × State Unit} + (hy : y ∈ (NonDet.when (d = true) (fl4ThreadPre t) σ).1) : RsA σ y.2 := by + unfold NonDet.when at hy + split at hy + · unfold fl4ThreadPre at hy + obtain ⟨a, ha, hy⟩ := mem_bindA.1 hy + have k1 : DlA σ a.2 := fl4CancelRun_dlA t σ a ha + obtain ⟨b, hb, hy⟩ := mem_bindA.1 hy + have k2 : DlA a.2 b.2 := archThreadGet_dlA _ _ a.2 b hb + have hn' : NotRunVmA b.2 := NotRunVmA.dl (NotRunVmA.dl hn k1) k2 + have R12 : RsA σ b.2 := PreO.trans k1.rs k2.rs + obtain ⟨vOpt, s2⟩ := b + cases vOpt with + | none => + obtain rfl : y = ((), s2) := hy + exact R12 + | some v => exact PreO.trans R12 (fl4VcpuEndRun_dcA v s2 hn' y hy).rs + · obtain rfl : y = ((), σ) := hy + exact PreO.refl σ + +/-- **`FinBodySpecA` for a thread cap** (module docstring). -/ +theorem finBodySpec_threadA {ct : ObjRef} {m : McState} : + FinBodySpecA fl4FinaliseCap .thread ct m (fun σ => CurScA σ ∧ NotRunVmA σ) := by + intro c ac d σ x σ' _ hP hk hac hmem + have hA : ∃ t, ac = .threadCap t := by + rcases hac.eq_or_pt with e | ⟨p', a', v', e⟩ + · subst e + cases c <;> simp [finKindA] at hk + exact ⟨_, rfl⟩ + · subst e + cases c <;> simp [finKindA] at hk + simp [CapEqA, absCapA] at hac + obtain ⟨t, rfl⟩ := hA + have e : fl4FinaliseCap (Z := Unit) (.threadCap t) d = + (NonDet.when (d = true) (fl4ThreadPre t) >>= fun _ => finaliseCap (.threadCap t) d) := by + unfold fl4FinaliseCap fl4ThreadPre; rfl + rw [e] at hmem + obtain ⟨y, hy, hmem⟩ := mem_bindA.1 hmem + have R1 : RsA σ y.2 := threadPre_rsA t d hP.2 hy + have R2 : RsA y.2 σ' := finaliseCap_threadRs t d y.2 (x, σ') hmem + exact (PreO.trans R1 R2).step hP.1 + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3DelFinVcpuA.lean b/proofs/Fl4ProofsA64/P3/Stage3DelFinVcpuA.lean new file mode 100644 index 000000000..862388439 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3DelFinVcpuA.lean @@ -0,0 +1,530 @@ +/- +P3 (AArch64, A64-P3 DelBodyA slice 1): **the spec finaliser's body frame, VCPU** (`finBodySpec_vcpuA`). + +`fl4FinaliseCap (vcpuCap)` is `fl4VcpuEndRun` (when final) then `vcpuFinalise`. Every step but one is a `DlA` step +(current thread / SC, time fields, consumed time, release queue, scheduler action, TCB states and SC objects kept). The +exception is `fl4CompleteRun c`'s `setThreadState c Running`, where `c` was read as `runningVM`: `DcA` keeps only the +current thread's state, and `c` is not the current thread when that one is not in `runningVM` (the hook's `Pr`). + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3DelFinIpcA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4Spec Fl4SpecA64 + +noncomputable section + +/-! ## Machine operations keeping `lastMachineTime` -/ + +/-- Every outcome of a machine operation keeps `lastMachineTime`. -/ +def MlkA {α : Type} (mop : NonDet MachineState α) : Prop := + ∀ ms r, r ∈ (mop ms).1 → r.2.lastMachineTime = ms.lastMachineTime + +theorem MlkA.ro {α : Type} {m : NonDet MachineState α} (h : ∀ ms r, r ∈ (m ms).1 → r.2 = ms) : MlkA m := + fun ms r hr => by rw [h ms r hr] + +theorem MlkA.npure {α : Type} (a : α) : MlkA (NonDet.pure a : NonDet MachineState α) := + MlkA.ro fun _ _ h => by rw [Set.mem_singleton_iff.1 h] + +theorem MlkA.pure {α : Type} (a : α) : MlkA (Pure.pure a : NonDet MachineState α) := MlkA.npure a + +theorem MlkA.gets {α : Type} (f : MachineState → α) : MlkA (NonDet.gets f) := + MlkA.ro fun _ _ h => by rw [Set.mem_singleton_iff.1 h] + +theorem MlkA.assert (P : Prop) : MlkA (NonDet.assert P : NonDet MachineState Unit) := + MlkA.ro fun _ _ h => by rw [h.2] + +theorem MlkA.bind {α β : Type} {m : NonDet MachineState α} {f : α → NonDet MachineState β} (hm : MlkA m) + (hf : ∀ a, MlkA (f a)) : MlkA (m >>= f) := fun ms r hr => by + obtain ⟨x, hx, hr⟩ := hr + exact (hf x.1 x.2 r hr).trans (hm ms x hx) + +theorem MlkA.modify (f : MachineState → MachineState) (h : ∀ ms, (f ms).lastMachineTime = ms.lastMachineTime) : + MlkA (NonDet.modify f) := fun ms r hr => by rw [Set.mem_singleton_iff.1 hr]; exact h ms + +theorem MlkA.restLift {α : Type} (f : MachineRestMonad α) : MlkA (machineRestLift f) := by + unfold machineRestLift + refine MlkA.bind (MlkA.gets _) fun mr => MlkA.bind (MlkA.ro fun _ _ h => h.2) fun x => ?_ + obtain ⟨r, mr'⟩ := x + exact MlkA.bind (MlkA.modify _ fun _ => rfl) fun _ => MlkA.pure _ + +theorem MlkA.lift (f : MachineRestMonad Unit) : MlkA (machineOpLift f) := MlkA.restLift _ + +macro "mlk" : tactic => `(tactic| repeat' first + | exact MlkA.pure _ | exact MlkA.npure _ | exact MlkA.gets _ | exact MlkA.assert _ | exact MlkA.lift _ + | exact MlkA.modify _ (fun _ => rfl) + | refine MlkA.bind ?_ (fun _ => ?_)) + +theorem NdDlA.machine {α : Type} {mop : NonDet MachineState α} (h : MlkA mop) : + NdDlA (doMachineOp (Z := Unit) mop) := by + intro σ r hr + unfold doMachineOp at hr + obtain ⟨a, ha, hr⟩ := mem_bindA.1 hr + obtain rfl : a = (σ.machineState, σ) := ha + obtain ⟨b, hb, hr⟩ := mem_bindA.1 hr + obtain ⟨hm, e2⟩ := hb + obtain ⟨⟨x, ms'⟩, s2⟩ := b + dsimp only at hm e2 hr + rw [e2] at hr + obtain ⟨c, hc, hr⟩ := mem_bindA.1 hr + obtain rfl : c = ((), { σ with machineState := ms' }) := hc + have e : r = (x, { σ with machineState := ms' }) := hr + rw [e] + exact dlA_eqA rfl rfl (h _ _ hm) rfl rfl rfl rfl rfl rfl + +theorem dsb_mlkA : MlkA dsb := MlkA.lift _ +theorem isb_mlkA : MlkA isb := MlkA.lift _ +theorem readVCPUHardwareReg_mlkA (r : VCPUReg) : MlkA (readVCPUHardwareReg r) := MlkA.gets _ +theorem writeVCPUHardwareReg_mlkA (r : VCPUReg) (v : Fl4SpecA64.Word) : MlkA (writeVCPUHardwareReg r v) := by + unfold writeVCPUHardwareReg; mlk +theorem setGicVcpuCtrlHcr_mlkA (w : Word32) : MlkA (setGicVcpuCtrlHcr w) := by unfold setGicVcpuCtrlHcr; mlk +theorem setSCTLR_mlkA (w : Fl4SpecA64.Word) : MlkA (setSCTLR w) := by unfold setSCTLR; mlk +theorem writeTCR_mlkA (w : Fl4SpecA64.Word) : MlkA (writeTCR w) := by unfold writeTCR; mlk +theorem setHCR_mlkA (w : Fl4SpecA64.Word) : MlkA (setHCR w) := MlkA.lift _ +theorem enableFpuEL01_mlkA : MlkA enableFpuEL01 := by unfold enableFpuEL01; mlk +theorem checkExportArchTimer_mlkA : MlkA checkExportArchTimer := by unfold checkExportArchTimer; mlk +theorem maskInterrupt_mlkA (b : Bool) (i : Irq) : MlkA (Fl4SpecA64.maskInterrupt b i) := by + unfold Fl4SpecA64.maskInterrupt; mlk +theorem getGicHcr_mlkA : MlkA getGicVcpuCtrlHcr := MlkA.gets _ +theorem getGicVmcr_mlkA : MlkA getGicVcpuCtrlVmcr := MlkA.gets _ +theorem getGicApr_mlkA : MlkA getGicVcpuCtrlApr := MlkA.gets _ +theorem getGicLr_mlkA (n : Fl4SpecA64.Word) : MlkA (getGicVcpuCtrlLr n) := by unfold getGicVcpuCtrlLr; mlk +theorem storeWord_mlkA (p w : Fl4SpecA64.Word) : MlkA (storeWord p w) := by unfold storeWord; mlk + +/-! ## State operations -/ + +theorem NdDlA.mapMx {α β : Type} {f : α → NonDet (State Unit) β} (h : ∀ a, NdDlA (f a)) : + ∀ l : List α, NdDlA (NonDet.mapMx f l) + | [] => NdDlA.npure _ + | a :: l => NdDlA.nbind (h a) fun _ => NdDlA.mapMx h l + +theorem threadGet_dlA {α : Type} (f : Tcb → α) (t : ObjRef) : NdDlA (threadGet (Z := Unit) f t) := by + unfold threadGet; dl_auto + +theorem archThreadGet_dlA {α : Type} (f : ArchTcb → α) (t : ObjRef) : NdDlA (archThreadGet (Z := Unit) f t) := by + unfold archThreadGet; dl_auto + +/-- A TCB replaced by one with the same state. -/ +theorem dlA_updTcbA {σ : State Unit} {p : ObjRef} {x x' : Tcb} (hx : σ.kheap p = some (.tcb x)) + (hs : x'.tcbState = x.tcbState) (hsc : x'.tcbSchedContext = x.tcbSchedContext) + (hfl : x'.tcbFault = x.tcbFault) : + DlA σ { σ with kheap := Function.update σ.kheap p (some (.tcb x')) } := by + refine ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, fun q z hq => ?_, fun q z hq => ?_, fun q y k hq => ?_, fun q y k hq => ?_, id, + CtScKA.ofUpd (o' := .tcb x') (p := p) rfl (fun z e _ => ⟨x, hx, Or.inl (KernelObject.tcb.inj e ▸ hsc)⟩)⟩ + · dsimp only at hq + by_cases e : q = p + · subst e; rw [Function.update_self] at hq + exact ⟨x, hx, by rw [← KernelObject.tcb.inj (Option.some.inj hq)]; exact hs⟩ + · rw [Function.update_of_ne e] at hq; exact ⟨z, hq, rfl⟩ + · dsimp only at hq + by_cases e : q = p + · subst e; rw [Function.update_self] at hq + exact ⟨x, hx, by rw [← KernelObject.tcb.inj (Option.some.inj hq)]; exact hfl⟩ + · rw [Function.update_of_ne e] at hq; exact ⟨z, hq, rfl⟩ + · dsimp only at hq + by_cases e : q = p + · subst e; rw [Function.update_self] at hq; cases hq + · rw [Function.update_of_ne e] at hq; exact hq + · dsimp only + by_cases e : q = p + · subst e; rw [hx] at hq; cases hq + · rw [Function.update_of_ne e]; exact hq + +theorem threadSet_dlA (f : Tcb → Tcb) (hf : ∀ x, (f x).tcbState = x.tcbState) + (hsc : ∀ x, (f x).tcbSchedContext = x.tcbSchedContext) (hfl : ∀ x, (f x).tcbFault = x.tcbFault) (t : ObjRef) : + NdDlA (threadSet (Z := Unit) f t) := fun σ r hr => by + obtain ⟨x, hx, e⟩ := threadSet_memA f t hr + rw [e]; exact dlA_updTcbA hx (hf x) (hsc x) (hfl x) + +theorem asUser_dlA {α : Type} (t : ObjRef) (f : UserMonad α) : NdDlA (asUser (Z := Unit) t f) := by + intro σ r hr + obtain ⟨y1, hy1, hr⟩ := hr + have hy1' : y1 ∈ (NonDet.assertOpt (getTcb (Z := Unit) t σ) σ).1 := hy1 + cases hg : getTcb (Z := Unit) t σ with + | none => rw [hg] at hy1'; exact absurd hy1' (Set.notMem_empty _) + | some x => + rw [hg] at hy1' + have e1 : y1 = (x, σ) := hy1' + subst e1 + obtain ⟨y2, hy2, hr⟩ := hr + have e2 : y2 = (archTcbContextGet x.tcbArch, σ) := hy2 + subst e2 + obtain ⟨⟨⟨a, uc'⟩, s3⟩, hy3, hr⟩ := hr + have e3 : s3 = σ := hy3.2 + subst e3 + obtain ⟨y4, hy4, hr⟩ := hr + have e4 : y4 = ({ x with tcbArch := archTcbContextSet uc' x.tcbArch }, s3) := hy4 + subst e4 + obtain ⟨y5, hy5, hr⟩ := hr + obtain ⟨ko, hko, hr5⟩ := Sched.setObject_memA hy5 + have hx := Sched.getTcb_kheapA hg + have e6 : r = (a, y5.2) := hr + rw [e6, hr5] + exact dlA_updTcbA hx rfl rfl rfl + +theorem setMessageInfo_dlA (t : ObjRef) (i : MessageInfo) : NdDlA (setMessageInfo (Z := Unit) t i) := by + unfold setMessageInfo; exact asUser_dlA _ _ + +theorem getVcpu_dlA (p : ObjRef) : NdDlA (getVcpu (Z := Unit) p) := by + unfold getVcpu; exact NdDlA.getsMap _ _ + +theorem setVcpu_dlA (p : ObjRef) (v : Vcpu) : NdDlA (setVcpu (Z := Unit) p v) := by + unfold setVcpu; exact setObject_dlA _ _ (fun _ h => nomatch h) (fun _ _ h => nomatch h) (fun _ h => nomatch h) + +macro "vc_auto" : tactic => `(tactic| repeat' first + | exact NdDlA.pure _ | exact NdDlA.npure _ | exact NdDlA.gets _ | exact NdDlA.get | exact NdDlA.assert _ + | exact NdDlA.assertOpt _ | exact NdDlA.getsThe _ | exact NdDlA.getsMap _ _ | exact NdDlA.fail + | exact getVcpu_dlA _ | exact setVcpu_dlA _ _ | exact threadGet_dlA _ _ | exact archThreadGet_dlA _ _ + | exact asUser_dlA _ _ | exact getCap_dlA _ + | exact NdDlA.machine dsb_mlkA | exact NdDlA.machine isb_mlkA + | exact NdDlA.machine (readVCPUHardwareReg_mlkA _) | exact NdDlA.machine (writeVCPUHardwareReg_mlkA _ _) + | exact NdDlA.machine getGicHcr_mlkA | exact NdDlA.machine getGicVmcr_mlkA | exact NdDlA.machine getGicApr_mlkA + | exact NdDlA.machine (getGicLr_mlkA _) | exact NdDlA.machine checkExportArchTimer_mlkA + | exact NdDlA.machine (maskInterrupt_mlkA _ _) | exact NdDlA.machine (storeWord_mlkA _ _) + | exact NdDlA.modify (fun _ => dlA_eqA rfl rfl rfl rfl rfl rfl rfl rfl rfl) + | refine NdDlA.when ?_ | refine NdDlA.mapMx (fun _ => ?_) _ | refine NdDlA.mapM (fun _ => ?_) _ + | refine NdDlA.bind ?_ (fun _ => ?_) | refine NdDlA.nbind ?_ (fun _ => ?_) + | dsimp only + | split) + +theorem vcpuUpdate_dlA (p : ObjRef) (f : Vcpu → Vcpu) : NdDlA (vcpuUpdate (Z := Unit) p f) := by + unfold vcpuUpdate; vc_auto + +theorem vgicUpdate_dlA (p : ObjRef) (f : GicVcpuInterface → GicVcpuInterface) : + NdDlA (vgicUpdate (Z := Unit) p f) := by unfold vgicUpdate; exact vcpuUpdate_dlA _ _ + +theorem vgicUpdateLr_dlA (p : ObjRef) (i : Nat) (v : Virq) : NdDlA (vgicUpdateLr (Z := Unit) p i v) := by + unfold vgicUpdateLr; exact vgicUpdate_dlA _ _ + +theorem vcpuSaveReg_dlA (p : ObjRef) (r : VCPUReg) : NdDlA (vcpuSaveReg (Z := Unit) p r) := by + unfold vcpuSaveReg + exact NdDlA.bind (NdDlA.machine (readVCPUHardwareReg_mlkA _)) fun _ => vcpuUpdate_dlA _ _ + +theorem vcpuSaveRegRange_dlA (p : ObjRef) (a b : VCPUReg) : NdDlA (vcpuSaveRegRange (Z := Unit) p a b) := by + unfold vcpuSaveRegRange; exact NdDlA.mapMx (fun _ => vcpuSaveReg_dlA _ _) _ + +theorem disableVirtTimer_dlA (o : Option ObjRef) : NdDlA (disableVirtTimer (Z := Unit) o) := by + unfold disableVirtTimer + cases o with + | none => + dsimp only + vc_auto + | some p => + dsimp only + repeat' first + | exact vcpuSaveReg_dlA _ _ + | exact NdDlA.machine (writeVCPUHardwareReg_mlkA _ _) + | exact NdDlA.machine checkExportArchTimer_mlkA + | exact NdDlA.pure _ + | refine NdDlA.bind ?_ (fun _ => ?_) + +theorem vcpuDisable_dlA (o : Option ObjRef) : NdDlA (vcpuDisable (Z := Unit) o) := by + have g : MlkA (do + setGicVcpuCtrlHcr 0 + dsb + setSCTLR sctlrDefault + writeTCR tcrEL1Native + setHCR hcrNative + isb + enableFpuEL01) := + MlkA.bind (setGicVcpuCtrlHcr_mlkA _) fun _ => MlkA.bind dsb_mlkA fun _ => + MlkA.bind (setSCTLR_mlkA _) fun _ => MlkA.bind (writeTCR_mlkA _) fun _ => + MlkA.bind (setHCR_mlkA _) fun _ => MlkA.bind isb_mlkA fun _ => enableFpuEL01_mlkA + unfold vcpuDisable + repeat' first + | exact NdDlA.machine g | exact vcpuSaveReg_dlA _ _ | exact vgicUpdate_dlA _ _ + | exact disableVirtTimer_dlA _ | exact NdDlA.machine dsb_mlkA | exact NdDlA.machine getGicHcr_mlkA + | exact NdDlA.machine (maskInterrupt_mlkA _ _) | exact NdDlA.pure _ + | refine NdDlA.bind ?_ (fun _ => ?_) | dsimp only | split + +theorem vcpuInvalidateActive_dlA : NdDlA (vcpuInvalidateActive (Z := Unit)) := by + unfold vcpuInvalidateActive + repeat' first + | exact vcpuDisable_dlA _ | exact NdDlA.gets _ | exact NdDlA.pure _ + | exact NdDlA.modify (fun _ => dlA_eqA rfl rfl rfl rfl rfl rfl rfl rfl rfl) + | refine NdDlA.bind ?_ (fun _ => ?_) | dsimp only | split + +theorem vcpuSave_dlA (vb : Option (ObjRef × Bool)) : NdDlA (vcpuSave (Z := Unit) vb) := by + unfold vcpuSave + split + · repeat' first + | exact vcpuSaveReg_dlA _ _ | exact vgicUpdate_dlA _ _ | exact vgicUpdateLr_dlA _ _ _ + | exact vcpuSaveRegRange_dlA _ _ _ | exact disableVirtTimer_dlA _ + | exact NdDlA.machine dsb_mlkA | exact NdDlA.machine getGicHcr_mlkA | exact NdDlA.machine getGicVmcr_mlkA + | exact NdDlA.machine getGicApr_mlkA | exact NdDlA.machine (getGicLr_mlkA _) + | exact NdDlA.gets _ | exact NdDlA.pure _ + | refine NdDlA.mapM (fun _ => ?_) _ | refine NdDlA.when ?_ + | refine NdDlA.bind ?_ (fun _ => ?_) | dsimp only + · exact NdDlA.fail + +theorem vcpuFlush_dlA : NdDlA (vcpuFlush (Z := Unit)) := by + unfold vcpuFlush + exact NdDlA.bind (NdDlA.gets _) fun _ => NdDlA.when (NdDlA.bind (vcpuSave_dlA _) fun _ => vcpuInvalidateActive_dlA) + +theorem fl4VcpuFlush_dlA (vc : ObjRef) : NdDlA (fl4VcpuFlush (Z := Unit) vc) := by + unfold fl4VcpuFlush + exact NdDlA.bind (NdDlA.gets _) fun _ => NdDlA.when vcpuFlush_dlA + +theorem fl4VcpuExtUpdate_dlA (vc : ObjRef) (f : ExtVcpu → ExtVcpu) : NdDlA (fl4VcpuExtUpdate (Z := Unit) vc f) := by + unfold fl4VcpuExtUpdate; vc_auto + +theorem setRunVcpu_dlA (t : ObjRef) (v : Option ObjRef) : NdDlA (setRunVcpu (Z := Unit) t v) := by + unfold setRunVcpu; exact threadSet_dlA (fun tcb => { tcb with tcbExt := { tcb.tcbExt with tcbRunVcpu := v } }) (fun _ => rfl) (fun _ => rfl) (fun _ => rfl) t + +theorem getRunVcpu_dlA (t : ObjRef) : NdDlA (getRunVcpu (Z := Unit) t) := by + unfold getRunVcpu; exact threadGet_dlA _ _ + +theorem fl4VcpuGet_dlA (vc : ObjRef) (i : Nat) : NdDlA (fl4VcpuGet (Z := Unit) vc i) := by + unfold fl4VcpuGet; vc_auto + +theorem storeWordOffs_dlA (p : ObjRef) (k : Nat) (w : Fl4SpecA64.Word) : NdDlA (storeWordOffs (Z := Unit) p k w) := by + unfold storeWordOffs; vc_auto + +theorem copyRegsToFrame_dlA (vc fr : ObjRef) (rs : List Nat) : NdDlA (copyRegsToFrame (Z := Unit) vc fr rs) := by + unfold copyRegsToFrame + exact NdDlA.mapMx (fun _ => NdDlA.bind (fl4VcpuGet_dlA _ _) fun _ => storeWordOffs_dlA _ _ _) _ + +theorem fl4ReadStateToFrame_dlA (vc fr : ObjRef) (m : Fl4SpecA64.Word) : + NdDlA (fl4ReadStateToFrame (Z := Unit) vc fr m) := by + unfold fl4ReadStateToFrame + repeat' first + | exact copyRegsToFrame_dlA _ _ _ | exact storeWordOffs_dlA _ _ _ | exact fl4VcpuGet_dlA _ _ + | exact getVcpu_dlA _ | exact asUser_dlA _ _ | exact NdDlA.pure _ | exact NdDlA.npure _ + | refine NdDlA.mapMx (fun _ => ?_) _ | refine NdDlA.when ?_ + | refine NdDlA.bind ?_ (fun _ => ?_) | dsimp only | split + +/-! ## The end of a run -/ + +/-- **The VCPU body frame**: `DlA`, but only the current thread's state is kept. -/ +structure DcA (σ σ' : State Unit) : Prop where + cur : σ'.curThread = σ.curThread + time : σ'.curTime = σ.curTime + last : σ'.machineState.lastMachineTime = σ.machineState.lastMachineTime + csc : σ'.curSc = σ.curSc + cons : σ'.consumedTime = σ.consumedTime + rel : σ'.releaseQueue = σ.releaseQueue + act : σ'.schedulerAction = σ.schedulerAction + ct : ∀ x', σ'.kheap σ.curThread = some (.tcb x') → ∃ x, σ.kheap σ.curThread = some (.tcb x) ∧ x'.tcbState = x.tcbState + sc : ∀ p y k, σ'.kheap p = some (.schedContext y k) → σ.kheap p = some (.schedContext y k) + scf : ∀ p y k, σ.kheap p = some (.schedContext y k) → σ'.kheap p = some (.schedContext y k) + off : CtOffA σ.curThread σ → CtOffA σ.curThread σ' + ctSc : CtScKA σ.curThread σ σ' + /-- (chg) no TCB enters the `released_ipc_queues` classes or an IPC queue. -/ + chT : ∀ p x', σ'.kheap p = some (.tcb x') → ∃ x, σ.kheap p = some (.tcb x) ∧ + (Sched.inClassRSc x'.tcbState x'.tcbFault → Sched.inClassRSc x.tcbState x.tcbFault) ∧ + (ipcQueuedThreadState x'.tcbState = true → ipcQueuedThreadState x.tcbState = true) + +theorem DlA.toDc {σ σ' : State Unit} (h : DlA σ σ') : DcA σ σ' := + ⟨h.cur, h.time, h.last, h.csc, h.cons, h.rel, h.act, fun x hx => h.tcb _ x hx, h.sc, h.scf, h.off, h.ctSc, + fun p x' hx' => (h.ch.tcb p x' hx')⟩ + +theorem DcA.trans {a b c : State Unit} (h1 : DcA a b) (h2 : DcA b c) : DcA a c := + ⟨h2.cur.trans h1.cur, h2.time.trans h1.time, h2.last.trans h1.last, h2.csc.trans h1.csc, h2.cons.trans h1.cons, + h2.rel.trans h1.rel, h2.act.trans h1.act, fun x hx => by + obtain ⟨y, hy, e⟩ := h2.ct x (by rw [h1.cur]; exact hx) + rw [h1.cur] at hy + obtain ⟨z, hz, f⟩ := h1.ct y hy + exact ⟨z, hz, e.trans f⟩, fun p y k h => h1.sc p y k (h2.sc p y k h), + fun p y k h => h2.scf p y k (h1.scf p y k h), CtOffA.trans h1.cur h1.off h2.off, + h1.ctSc.trans (h1.cur ▸ h2.ctSc), fun p x hx => by + obtain ⟨y, hy, c2, q2⟩ := h2.chT p x hx + obtain ⟨z, hz, c1, q1⟩ := h1.chT p y hy + exact ⟨z, hz, fun h => c1 (c2 h), fun h => q1 (q2 h)⟩⟩ + +/-- (chg) A `DcA` step keeps the current SC chargeable. -/ +theorem DcA.ch {σ σ' : State Unit} (h : DcA σ σ') : ChKA σ σ' := + ⟨h.cur, h.csc, h.chT, fun y' n o hy ho => Or.inl ⟨y', n, h.sc _ _ _ hy, ho⟩⟩ + +/-- A `DcA` step is a `DelStepA` step. -/ +theorem DcA.step {σ σ' : State Unit} (h : DcA σ σ') : DelStepCA σ σ' := + ⟨⟨h.cur, h.time, h.last, h.csc, h.cons, fun _ ht => h.rel ▸ ht, + fun y' k hy => ⟨y', h.sc _ _ _ hy, Or.inl ⟨rfl, rfl, rfl⟩⟩, Or.inl h.act, fun _ x hx => h.ct x hx, h.off, h.ctSc⟩, + h.ch⟩ + +/-- A thread not the current one is made `Running`. -/ +theorem setRunning_dcA (c : ObjRef) {σ : State Unit} (hc : c ≠ σ.curThread) + (hrv : ∀ x, σ.kheap c = some (.tcb x) → runnable x.tcbState = true) {r : Unit × State Unit} + (hr : r ∈ (setThreadState (Z := Unit) c .running σ).1) : DcA σ r.2 := by + unfold setThreadState at hr + obtain ⟨a, ha, h2⟩ := mem_bindA.1 hr + obtain ⟨x, hx, ea⟩ := threadSet_memA _ c ha + unfold setThreadStateAct at h2 + obtain ⟨d, hd, h2⟩ := mem_bindA.1 h2 + obtain rfl : d = (a.2.curThread, a.2) := hd + obtain ⟨e, he, h2⟩ := mem_bindA.1 h2 + obtain rfl : e = (a.2.schedulerAction, a.2) := he + obtain ⟨f, hf, h2⟩ := mem_bindA.1 h2 + obtain rfl : f = (schedulable c a.2, a.2) := hf + unfold NonDet.when at h2 + split at h2 + · rename_i h + exact absurd (h.1.trans (by rw [ea])) hc + · obtain rfl : r = ((), a.2) := h2 + show DcA σ a.2 + rw [ea] + refine ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, fun z hz => ?_, fun p y k hp => ?_, fun p y k hp => ?_, id, + CtScKA.ofUpd (p := c) rfl (fun _ _ e => absurd e.symm hc), fun p x' hx' => ?_⟩ + · change Function.update σ.kheap c _ σ.curThread = _ at hz + rw [Function.update_of_ne hc.symm] at hz + exact ⟨z, hz, rfl⟩ + · change Function.update σ.kheap c _ p = _ at hp + by_cases e : p = c + · subst e; rw [Function.update_self] at hp; cases hp + · rwa [Function.update_of_ne e] at hp + · change Function.update σ.kheap c _ p = _ + by_cases e : p = c + · subst e; rw [hx] at hp; cases hp + · rwa [Function.update_of_ne e] + · change Function.update σ.kheap c _ p = _ at hx' + by_cases e : p = c + · subst e; rw [Function.update_self] at hx' + obtain rfl := KernelObject.tcb.inj (Option.some.inj hx') + exact ⟨x, hx, fun _ => Or.inl (hrv x hx), fun q => absurd q (by simp [ipcQueuedThreadState])⟩ + · rw [Function.update_of_ne e] at hx' + exact ⟨x', hx', id, id⟩ + +/-- `c` is not the current thread and is runnable. -/ +def HcA (c : ObjRef) (σ : State Unit) : Prop := + c ≠ σ.curThread ∧ ∀ x, σ.kheap c = some (.tcb x) → runnable x.tcbState = true + +theorem HcA.dl {c : ObjRef} {σ σ' : State Unit} (h : HcA c σ) (k : DlA σ σ') : HcA c σ' := + ⟨by rw [k.cur]; exact h.1, fun x hx => by + obtain ⟨x0, hx0, es⟩ := k.tcb c x hx + rw [es]; exact h.2 x0 hx0⟩ + +/-- A `DlA` computation before a `DcA` one that needs `c` not to be the current thread. -/ +theorem dc_bindA {α β : Type} {c : ObjRef} {m : NonDet (State Unit) α} {f : α → NonDet (State Unit) β} (hm : NdDlA m) + (hf : ∀ a (σ : State Unit), HcA c σ → ∀ r, r ∈ (f a σ).1 → DcA σ r.2) + (σ : State Unit) (hc : HcA c σ) (r : β × State Unit) (hr : r ∈ ((m >>= f) σ).1) : DcA σ r.2 := by + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + have k := hm σ x hx + exact k.toDc.trans (hf x.1 x.2 (hc.dl k) r hr) + +theorem fl4CompleteRun_dcA (c : ObjRef) (stop : RunStop) (w1 w2 w3 : Fl4SpecA64.Word) (σ : State Unit) + (hc : HcA c σ) (r : Unit × State Unit) (hr : r ∈ (fl4CompleteRun (Z := Unit) c stop w1 w2 w3 σ).1) : + DcA σ r.2 := by + unfold fl4CompleteRun at hr + refine dc_bindA (getRunVcpu_dlA _) (fun o σ hc r hr => ?_) σ hc r hr + cases o with + | none => exact (NdDlA.pure () σ r hr).toDc + | some vc => + dsimp only at hr + refine dc_bindA (fl4VcpuFlush_dlA _) (fun _ σ hc r hr => ?_) σ hc r hr + refine dc_bindA (getVcpu_dlA _) (fun v σ hc r hr => ?_) σ hc r hr + refine dc_bindA ?_ (fun _ σ hc r hr => ?_) σ hc r hr + · refine NdDlA.when (NdDlA.bind (getCap_dlA _) fun buf => ?_) + split + · exact NdDlA.when (fl4ReadStateToFrame_dlA _ _ _) + · exact NdDlA.pure _ + refine dc_bindA (fl4VcpuExtUpdate_dlA _ _) (fun _ σ hc r hr => ?_) σ hc r hr + refine dc_bindA (setRunVcpu_dlA _ _) (fun _ σ hc r hr => ?_) σ hc r hr + refine dc_bindA (asUser_dlA _ _) (fun _ σ hc r hr => ?_) σ hc r hr + refine dc_bindA (asUser_dlA _ _) (fun _ σ hc r hr => ?_) σ hc r hr + refine dc_bindA (setMessageInfo_dlA _ _) (fun _ σ hc r hr => ?_) σ hc r hr + refine dc_bindA (asUser_dlA _ _) (fun _ σ hc r hr => ?_) σ hc r hr + exact setRunning_dcA c hc.1 hc.2 hr + +/-- The current thread is not in `runningVM` (a deletion's caller is `Restart`). -/ +def NotRunVmA (σ : State Unit) : Prop := + ∀ x, σ.kheap σ.curThread = some (.tcb x) → x.tcbState ≠ .ext .runningVM + +theorem fl4VcpuEndRun_dcA (vc : ObjRef) (σ : State Unit) (hn : NotRunVmA σ) (r : Unit × State Unit) + (hr : r ∈ (fl4VcpuEndRun (Z := Unit) vc σ).1) : DcA σ r.2 := by + unfold fl4VcpuEndRun at hr + obtain ⟨a, ha, hr⟩ := mem_bindA.1 hr + have k := getVcpu_dlA vc σ a ha + have e1 : a.2 = σ := by + unfold getVcpu NonDet.getsMap at ha + obtain ⟨y, hy, ha⟩ := mem_bindA.1 ha + obtain rfl : y = (vcpusOf σ, σ) := hy + change a ∈ (NonDet.assertOpt (vcpusOf σ vc) σ).1 at ha + cases hv : vcpusOf σ vc with + | none => rw [hv] at ha; exact absurd ha (Set.notMem_empty _) + | some v => rw [hv] at ha; obtain rfl : a = (v, σ) := ha; rfl + rw [e1] at hr + cases hrc : a.1.vcpuExt.runCaller with + | none => rw [hrc] at hr; exact (NdDlA.pure () σ r hr).toDc + | some c => + rw [hrc] at hr + dsimp only at hr + obtain ⟨b, hb, hr⟩ := mem_bindA.1 hr + unfold getThreadState threadGet at hb + obtain ⟨y, hy, hb⟩ := mem_bindA.1 hb + change y ∈ (NonDet.assertOpt (getTcb (Z := Unit) c σ) σ).1 at hy + cases hg : getTcb (Z := Unit) c σ with + | none => rw [hg] at hy; exact absurd hy (Set.notMem_empty _) + | some x => + rw [hg] at hy + obtain rfl : y = (x, σ) := hy + obtain rfl : b = (x.tcbState, σ) := hb + dsimp only at hr + split at hr + · rename_i hrun + refine fl4CompleteRun_dcA c _ _ _ _ σ ⟨fun e => ?_, fun x' hx' => ?_⟩ r hr + · exact hn x (e ▸ Sched.getTcb_kheapA hg) hrun + · rw [Sched.getTcb_kheapA hg] at hx' + obtain rfl := KernelObject.tcb.inj (Option.some.inj hx') + rw [hrun]; rfl + · exact ((NdDlA.bind (fl4VcpuExtUpdate_dlA _ _) fun _ => setRunVcpu_dlA _ _) σ r hr).toDc + +theorem dissociateVcpuTcb_dlA (v t : ObjRef) : NdDlA (dissociateVcpuTcb (Z := Unit) v t) := by + unfold dissociateVcpuTcb + refine NdDlA.bind (archThreadGet_dlA _ _) fun _ => NdDlA.bind (getVcpu_dlA _) fun _ => + NdDlA.bind (NdDlA.assert _) fun _ => NdDlA.bind (NdDlA.gets _) fun _ => + NdDlA.bind (NdDlA.when vcpuInvalidateActive_dlA) fun _ => NdDlA.bind ?_ fun _ => + NdDlA.bind (setVcpu_dlA _ _) fun _ => asUser_dlA _ _ + intro σ r hr + unfold archThreadSet at hr + obtain ⟨y, hy, hr⟩ := mem_bindA.1 hr + change y ∈ (NonDet.assertOpt (getTcb (Z := Unit) t σ) σ).1 at hy + cases hg : getTcb (Z := Unit) t σ with + | none => rw [hg] at hy; exact absurd hy (Set.notMem_empty _) + | some x => + rw [hg] at hy + obtain rfl : y = (x, σ) := hy + obtain ⟨ko, -, e⟩ := Sched.setObject_memA hr + rw [e]; exact dlA_updTcbA (Sched.getTcb_kheapA hg) rfl rfl rfl + +theorem vcpuFinalise_dlA (v : ObjRef) : NdDlA (vcpuFinalise (Z := Unit) v) := by + unfold vcpuFinalise + refine NdDlA.bind (getVcpu_dlA _) fun x => ?_ + split + · exact dissociateVcpuTcb_dlA _ _ + · exact NdDlA.pure _ + +theorem finaliseCap_vcpu_dlA (v : ObjRef) (d : Bool) : + NdDlA (finaliseCap (Z := Unit) (.archObjectCap (.vcpuCap v)) d) := by + unfold finaliseCap + cases d + · unfold archFinaliseCap; exact NdDlA.pure _ + · unfold archFinaliseCap; exact NdDlA.bind (vcpuFinalise_dlA _) fun _ => NdDlA.pure _ + +/-- **`FinBodySpecA` for a VCPU cap** (module docstring). -/ +theorem finBodySpec_vcpuA {ct : ObjRef} {m : McState} : FinBodySpecA fl4FinaliseCap .vcpu ct m NotRunVmA := by + intro c ac d σ x σ' _ hP hk hac hmem + have ea : ∃ v, ac = .archObjectCap (.vcpuCap v) := by + rcases hac.eq_or_pt with e | ⟨p', a', v', e⟩ + · cases c <;> simp [finKindA] at hk + exact ⟨_, e⟩ + · subst e; cases c <;> simp [finKindA] at hk + simp [CapEqA, absCapA] at hac + obtain ⟨v, rfl⟩ := ea + have e : fl4FinaliseCap (Z := Unit) (.archObjectCap (.vcpuCap v)) d = + (NonDet.when (d = true) (fl4VcpuEndRun v) >>= fun _ => finaliseCap (.archObjectCap (.vcpuCap v)) d) := by + unfold fl4FinaliseCap; rfl + rw [e] at hmem + obtain ⟨y, hy, hmem⟩ := mem_bindA.1 hmem + have k1 : DcA σ y.2 := by + unfold NonDet.when at hy + split at hy + · exact fl4VcpuEndRun_dcA v σ hP y hy + · obtain rfl : y = ((), σ) := hy + exact (dlA_refl σ).toDc + exact (k1.trans (finaliseCap_vcpu_dlA v d y.2 _ hmem).toDc).step + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3DelFinVsA.lean b/proofs/Fl4ProofsA64/P3/Stage3DelFinVsA.lean new file mode 100644 index 000000000..f88f72a8e --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3DelFinVsA.lean @@ -0,0 +1,434 @@ +/- +P3 (AArch64, A64-P3 DelBodyA slice 1): **the spec finaliser's body frame, arch-VSpace kinds** (`FinBodySpecA` for +the six VSpace kinds). + +`DlA σ σ'`: current thread / SC, time fields, consumed time, release queue and scheduler action kept; every TCB of `σ'` +was a TCB of `σ` with the same state, every SC of `σ'` is the same object of `σ`. The spec's VSpace operations are `DlA` +steps (the closure calculus mirrors `DelTimeSpecA`'s). + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.DelTimeSpecA +import Fl4ProofsA64.P3.Stage3DelBodyHookA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4Spec Fl4SpecA64 + +noncomputable section + +/-- **The strong body frame** (module docstring). -/ +structure DlA (σ σ' : State Unit) : Prop where + cur : σ'.curThread = σ.curThread + time : σ'.curTime = σ.curTime + last : σ'.machineState.lastMachineTime = σ.machineState.lastMachineTime + csc : σ'.curSc = σ.curSc + cons : σ'.consumedTime = σ.consumedTime + rel : σ'.releaseQueue = σ.releaseQueue + act : σ'.schedulerAction = σ.schedulerAction + tcb : ∀ p x', σ'.kheap p = some (.tcb x') → ∃ x, σ.kheap p = some (.tcb x) ∧ x'.tcbState = x.tcbState + /-- (chg) every TCB keeps its fault. -/ + flt : ∀ p x', σ'.kheap p = some (.tcb x') → ∃ x, σ.kheap p = some (.tcb x) ∧ x'.tcbFault = x.tcbFault + sc : ∀ p y k, σ'.kheap p = some (.schedContext y k) → σ.kheap p = some (.schedContext y k) + /-- (slice 2) forward: every SC of `σ` is still the same object in `σ'` (no step of the closure replaces or erases an SC). -/ + scf : ∀ p y k, σ.kheap p = some (.schedContext y k) → σ'.kheap p = some (.schedContext y k) + off : CtOffA σ.curThread σ → CtOffA σ.curThread σ' + ctSc : CtScKA σ.curThread σ σ' + +theorem dlA_refl (σ : State Unit) : DlA σ σ := + ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, fun _ x h => ⟨x, h, rfl⟩, fun _ x h => ⟨x, h, rfl⟩, fun _ _ _ h => h, + fun _ _ _ h => h, id, CtScKA.refl _ _⟩ + +theorem dlA_trans {a b c : State Unit} (h1 : DlA a b) (h2 : DlA b c) : DlA a c := + ⟨h2.cur.trans h1.cur, h2.time.trans h1.time, h2.last.trans h1.last, h2.csc.trans h1.csc, h2.cons.trans h1.cons, + h2.rel.trans h1.rel, h2.act.trans h1.act, fun p x h => by + obtain ⟨y, hy, e⟩ := h2.tcb p x h + obtain ⟨z, hz, f⟩ := h1.tcb p y hy + exact ⟨z, hz, e.trans f⟩, fun p x h => by + obtain ⟨y, hy, e⟩ := h2.flt p x h + obtain ⟨z, hz, f⟩ := h1.flt p y hy + exact ⟨z, hz, e.trans f⟩, fun p y k h => h1.sc p y k (h2.sc p y k h), + fun p y k h => h2.scf p y k (h1.scf p y k h), CtOffA.trans h1.cur h1.off h2.off, + h1.ctSc.trans (h1.cur ▸ h2.ctSc)⟩ + +theorem dlA_eqA {σ σ' : State Unit} (h1 : σ'.curThread = σ.curThread) (h2 : σ'.curTime = σ.curTime) + (h3 : σ'.machineState.lastMachineTime = σ.machineState.lastMachineTime) (h4 : σ'.curSc = σ.curSc) + (h5 : σ'.consumedTime = σ.consumedTime) (h6 : σ'.releaseQueue = σ.releaseQueue) + (h7 : σ'.schedulerAction = σ.schedulerAction) (hk : σ'.kheap = σ.kheap) + (hr : σ'.readyQueues = σ.readyQueues) : DlA σ σ' := + ⟨h1, h2, h3, h4, h5, h6, h7, fun p x h => ⟨x, hk ▸ h, rfl⟩, fun p x h => ⟨x, hk ▸ h, rfl⟩, fun p y k h => hk ▸ h, + fun p y k h => by rw [hk]; exact h, CtOffA.keep hr (Or.inl h7), CtScKA.ofEq hk⟩ + +/-- (chg) A `DlA` step keeps the current SC chargeable. -/ +theorem DlA.ch {σ σ' : State Unit} (h : DlA σ σ') : ChKA σ σ' := by + refine ⟨h.cur, h.csc, fun p x' hx' => ?_, fun y' n o hy ho => Or.inl ⟨y', n, h.sc _ _ _ hy, ho⟩⟩ + obtain ⟨x, hx, es⟩ := h.tcb p x' hx' + obtain ⟨x2, hx2, ef⟩ := h.flt p x' hx' + rw [hx] at hx2 + obtain rfl := KernelObject.tcb.inj (Option.some.inj hx2) + exact ⟨x, hx, fun c => by rw [es, ef] at c; exact c, fun q => by rw [es] at q; exact q⟩ + +/-- A `DlA` step is a `DelStepA` step. -/ +theorem DlA.step {σ σ' : State Unit} (h : DlA σ σ') : DelStepCA σ σ' := + ⟨⟨h.cur, h.time, h.last, h.csc, h.cons, fun t ht => h.rel ▸ ht, + fun y' k hy => ⟨y', h.sc _ _ _ hy, Or.inl ⟨rfl, rfl, rfl⟩⟩, Or.inl h.act, fun _ x hx => h.tcb _ _ hx, h.off, h.ctSc⟩, + h.ch⟩ + +/-- **Every outcome** (module docstring). -/ +def NdDlA {α : Type} (m : NonDet (State Unit) α) : Prop := ∀ σ r, r ∈ (m σ).1 → DlA σ r.2 + +section +variable {α β ε : Type} + +theorem NdDlA.ro {m : NonDet (State Unit) α} (h : ∀ σ r, r ∈ (m σ).1 → r.2 = σ) : NdDlA m := fun σ r hr => by + rw [h σ r hr]; exact dlA_refl σ + +theorem NdDlA.pure (a : α) : NdDlA (Pure.pure a : NonDet (State Unit) α) := + NdDlA.ro fun _ _ h => by rw [Set.mem_singleton_iff.1 h] + +theorem NdDlA.npure (a : α) : NdDlA (NonDet.pure a : NonDet (State Unit) α) := NdDlA.pure a + +theorem NdDlA.bind {m : NonDet (State Unit) α} {f : α → NonDet (State Unit) β} (hm : NdDlA m) + (hf : ∀ a, NdDlA (f a)) : NdDlA (m >>= f) := fun σ r hr => by + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + exact dlA_trans (hm σ x hx) (hf x.1 x.2 r hr) + +theorem NdDlA.nbind {m : NonDet (State Unit) α} {f : α → NonDet (State Unit) β} (hm : NdDlA m) + (hf : ∀ a, NdDlA (f a)) : NdDlA (NonDet.bind m f) := NdDlA.bind hm hf + +theorem NdDlA.fail : NdDlA (NonDet.fail : NonDet (State Unit) α) := fun _ _ h => h.elim + +theorem NdDlA.gets (g : State Unit → α) : NdDlA (NonDet.gets g) := + NdDlA.ro fun _ _ h => by rw [Set.mem_singleton_iff.1 h] + +theorem NdDlA.get : NdDlA (NonDet.get : NonDet (State Unit) (State Unit)) := + NdDlA.ro fun _ _ h => by rw [Set.mem_singleton_iff.1 h] + +theorem NdDlA.assert (P : Prop) : NdDlA (NonDet.assert P : NonDet (State Unit) Unit) := + NdDlA.ro fun _ _ h => by rw [h.2] + +theorem NdDlA.assertOpt (o : Option α) : NdDlA (NonDet.assertOpt o : NonDet (State Unit) α) := by + cases o + · exact NdDlA.fail + · exact NdDlA.npure _ + +theorem NdDlA.getsThe (g : State Unit → Option α) : NdDlA (NonDet.getsThe g) := fun σ r hr => + NdDlA.assertOpt (g σ) σ r hr + +theorem NdDlA.getsMap {γ : Type} (g : State Unit → γ → Option α) (p : γ) : NdDlA (NonDet.getsMap g p) := + NdDlA.nbind (NdDlA.gets _) fun _ => NdDlA.assertOpt _ + +theorem NdDlA.selectF (S : Set α × Prop) : NdDlA (NonDet.selectF S : NonDet (State Unit) α) := + NdDlA.ro fun _ _ h => h.2 + +theorem NdDlA.modify {g : State Unit → State Unit} (h : ∀ σ, DlA σ (g σ)) : NdDlA (NonDet.modify g) := + fun σ r hr => by rw [Set.mem_singleton_iff.1 hr]; exact h σ + +theorem NdDlA.when {P : Prop} {inst : Decidable P} {m : NonDet (State Unit) Unit} (h : NdDlA m) : NdDlA (NonDet.when P m) := by + unfold NonDet.when; split + · exact h + · exact NdDlA.npure _ + +theorem NdDlA.mapM {f : α → NonDet (State Unit) β} (h : ∀ a, NdDlA (f a)) : + ∀ l : List α, NdDlA (NonDet.mapM f l) + | [] => NdDlA.npure _ + | a :: l => NdDlA.nbind (h a) fun _ => NdDlA.nbind (NdDlA.mapM h l) fun _ => NdDlA.npure _ + +theorem NdDlA.ofOk {Q : State Unit → State Unit → Prop} (hQ : ∀ a b, Q a b → DlA a b) + {m : NonDet (State Unit) α} (h : NdOkA Q m) : NdDlA m := fun σ r hr => hQ _ _ ((h σ).2.2 r hr) + +theorem VmA.dlA {σ σ' : State Unit} (h : VmA σ σ') : DlA σ σ' := by + obtain ⟨mr, am, vt, nv, rfl⟩ := h; exact dlA_eqA rfl rfl rfl rfl rfl rfl rfl rfl rfl + +/-! ### The `NonDetE` forms (on `run`) -/ + +theorem NdDlA.liftE {m : NonDet (State Unit) α} (h : NdDlA m) : NdDlA (NonDetE.liftE m : NonDetE (State Unit) ε α).run := by + unfold NonDetE.liftE + rw [ExceptT.run_lift] + exact NdDlA.bind h fun _ => NdDlA.pure _ + +theorem NdDlA.runBind {x : NonDetE (State Unit) ε α} {f : α → NonDetE (State Unit) ε β} (hx : NdDlA x.run) + (hf : ∀ a, NdDlA (f a).run) : NdDlA (x >>= f).run := by + rw [ExceptT.run_bind] + refine NdDlA.bind hx fun r => ?_ + cases r with + | error e => exact NdDlA.pure _ + | ok a => exact hf a + +theorem NdDlA.runPure (a : α) : NdDlA (Pure.pure a : NonDetE (State Unit) ε α).run := by + rw [ExceptT.run_pure]; exact NdDlA.pure _ + +theorem NdDlA.returnOk (a : α) : NdDlA (NonDetE.returnOk a : NonDetE (State Unit) ε α).run := NdDlA.runPure a + +theorem NdDlA.throwError (e : ε) : NdDlA (NonDetE.throwError e : NonDetE (State Unit) ε α).run := by + unfold NonDetE.throwError + show NdDlA (ExceptT.run (throw e : ExceptT ε (NonDet (State Unit)) α)) + exact NdDlA.pure _ + +theorem NdDlA.throwOpt (e : ε) (o : Option α) : NdDlA (NonDetE.throwOpt e o : NonDetE (State Unit) ε α).run := by + cases o + · exact NdDlA.throwError e + · exact NdDlA.runPure _ + +theorem NdDlA.unlessE {P : Prop} {inst : Decidable P} {m : NonDetE (State Unit) ε Unit} (h : NdDlA m.run) : + NdDlA (NonDetE.unlessE P m).run := by + unfold NonDetE.unlessE; split + · exact NdDlA.runPure _ + · exact h + +theorem NdDlA.whenE {P : Prop} {inst : Decidable P} {m : NonDetE (State Unit) ε Unit} (h : NdDlA m.run) : + NdDlA (NonDetE.whenE P m).run := by + unfold NonDetE.whenE; split + · exact h + · exact NdDlA.runPure _ + +theorem NdDlA.catch {m : NonDetE (State Unit) ε α} {h : ε → NonDet (State Unit) α} (hm : NdDlA m.run) + (hh : ∀ e, NdDlA (h e)) : NdDlA (NonDetE.catch m h) := by + unfold NonDetE.catch + refine NdDlA.nbind hm fun r => ?_ + cases r with + | error e => exact hh e + | ok a => exact NdDlA.npure a + +end + +/-! ## The spec's VSpace operations -/ + +theorem getObject_dlA (p : ObjRef) : NdDlA (getObject (Z := Unit) p) := NdDlA.ro fun _ _ h => (getObject_mem h).1 + +/-- `set_object`'s outcome with its type check: the old object at `p` has the new one's `a_type`. -/ +theorem setObject_memT {p : ObjRef} {o : KernelObject} {σ : State Unit} {r : Unit × State Unit} + (hr : r ∈ (setObject (Z := Unit) p o σ).1) : + ∃ o0, σ.kheap p = some o0 ∧ aType o0 = aType o ∧ r.2 = { σ with kheap := Function.update σ.kheap p (some o) } := by + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + obtain ⟨e1, e2⟩ := getObject_mem hx + obtain ⟨y, hy, hr⟩ := mem_bindA.1 hr + obtain ⟨hT, hy⟩ := hy + rw [hy] at hr + obtain ⟨z, hz, hr⟩ := mem_bindA.1 hr + obtain rfl : z = (x.2, x.2) := hz + obtain rfl : r = ((), { x.2 with kheap := Function.update x.2.kheap p (some o) }) := hr + rw [e1] + exact ⟨x.1, e2, hT, rfl⟩ + +theorem aType_scA {o : KernelObject} {k : Nat} (h : aType o = .aSchedContext k) : ∃ y n, o = .schedContext y n := by + cases o <;> simp only [aType] at h <;> first | exact ⟨_, _, rfl⟩ | (split at h <;> cases h) | cases h + +/-- A write of no TCB and no SC (and no garbage type, so the replaced object was no SC either). -/ +theorem setObject_dlA (p : ObjRef) (o : KernelObject) (ht : ∀ x, o ≠ .tcb x) (hs : ∀ y n, o ≠ .schedContext y n) + (hg : ∀ g, aType o ≠ .aGarbage g) : + NdDlA (setObject (Z := Unit) p o) := fun σ r hr => by + obtain ⟨o0, h0, hT, h⟩ := setObject_memT hr; rw [h] + refine ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, fun q x hq => ?_, fun q x hq => ?_, fun q y k hq => ?_, fun q y k hq => ?_, id, + CtScKA.ofUpd (o' := o) (p := p) rfl (fun x' e _ => absurd e (ht x'))⟩ + · dsimp only at hq + by_cases e : q = p + · subst e; rw [Function.update_self] at hq; exact absurd (Option.some.inj hq) (ht x) + · rw [Function.update_of_ne e] at hq; exact ⟨x, hq, rfl⟩ + · dsimp only at hq + by_cases e : q = p + · subst e; rw [Function.update_self] at hq; exact absurd (Option.some.inj hq) (ht x) + · rw [Function.update_of_ne e] at hq; exact ⟨x, hq, rfl⟩ + · dsimp only at hq + by_cases e : q = p + · subst e; rw [Function.update_self] at hq; exact absurd (Option.some.inj hq) (hs y k) + · rw [Function.update_of_ne e] at hq; exact hq + · dsimp only + by_cases e : q = p + · exfalso + subst e + rw [hq] at h0 + obtain rfl := Option.some.inj h0 + simp only [aType] at hT + split at hT + · obtain ⟨y', n', rfl⟩ := aType_scA hT.symm + exact hs y' n' rfl + · exact hg _ hT.symm + · rw [Function.update_of_ne e]; exact hq + +macro "dl_auto" : tactic => `(tactic| repeat' first + | exact NdDlA.pure _ | exact NdDlA.npure _ | exact NdDlA.gets _ | exact NdDlA.get | exact NdDlA.assert _ + | exact NdDlA.assertOpt _ | exact NdDlA.getsThe _ | exact NdDlA.getsMap _ _ | exact NdDlA.selectF _ + | exact NdDlA.fail | exact getObject_dlA _ | exact setObject_dlA _ _ (fun _ h => nomatch h) (fun _ _ h => nomatch h) (fun _ h => nomatch h) + | exact NdDlA.runPure _ | exact NdDlA.returnOk _ | exact NdDlA.throwError _ | exact NdDlA.throwOpt _ _ + | exact NdDlA.modify (fun _ => dlA_eqA rfl rfl rfl rfl rfl rfl rfl rfl rfl) + | refine NdDlA.liftE ?_ | refine NdDlA.unlessE ?_ | refine NdDlA.whenE ?_ | refine NdDlA.when ?_ + | refine NdDlA.catch ?_ (fun _ => ?_) + | refine NdDlA.runBind ?_ (fun _ => ?_) + | refine NdDlA.bind ?_ (fun _ => ?_) | refine NdDlA.nbind ?_ (fun _ => ?_) + | split) + +theorem doMachineOp_dlA (g : MachineRestMonad Unit) : NdDlA (doMachineOp (Z := Unit) (machineOpLift g)) := + NdDlA.ofOk (fun _ _ h => VmA.dlA h) (doMachineOp_vmA g) + +theorem invalidateTlbByAsid_dlA (asid : Asid) : NdDlA (invalidateTlbByAsid (Z := Unit) asid) := + NdDlA.ofOk (fun _ _ h => VmA.dlA h) (invalidateTlbByAsid_vmA asid) + +theorem invalidateAsidEntry_dlA (asid : Asid) : NdDlA (invalidateAsidEntry (Z := Unit) asid) := + NdDlA.ofOk (fun _ _ h => VmA.dlA h) (invalidateAsidEntry_vmA asid) + +theorem invalidateTlbByAsidVa_dlA (asid : Asid) (v : VSpaceRef) : NdDlA (invalidateTlbByAsidVa (Z := Unit) asid v) := by + unfold invalidateTlbByAsidVa + refine NdDlA.bind (NdDlA.ofOk (fun _ _ h => VmA.dlA h) (loadVmid_vmA asid)) fun mv => ?_ + cases mv with + | none => exact NdDlA.pure _ + | some vmid => exact doMachineOp_dlA _ + +theorem findVspaceForAsid_dlA (asid : Asid) : NdDlA (findVspaceForAsid (Z := Unit) asid).run := + NdDlA.ofOk (fun _ _ h => VmA.dlA h) (findVspaceForAsid_vmA asid) + +theorem getCap_dlA (sl : CslotPtr) : NdDlA (getCap (Z := Unit) sl) := by + obtain ⟨oref, cref⟩ := sl + unfold getCap + dl_auto + +theorem setVmRoot_dlA (t : ObjRef) : NdDlA (setVmRoot (Z := Unit) t) := by + unfold setVmRoot + refine NdDlA.bind (NdDlA.pure _) fun _ => NdDlA.bind (getCap_dlA _) fun c => ?_ + refine NdDlA.catch ?_ fun _ => NdDlA.ofOk (fun _ _ h => VmA.dlA h) setGlobalUserVspace_vmA + split + · refine NdDlA.runBind (findVspaceForAsid_dlA _) fun _ => NdDlA.runBind ?_ fun _ => + NdDlA.liftE (NdDlA.ofOk (fun _ _ h => VmA.dlA h) (armContextSwitch_vmA _ _)) + exact NdDlA.whenE (NdDlA.throwError _) + · exact NdDlA.throwError _ + +theorem setAsidPool_dlA (p : ObjRef) (pool : AsidPool) : NdDlA (setAsidPool (Z := Unit) p pool) := by + unfold setAsidPool; dl_auto + +theorem storePte_dlA (t : PtType) (p : ObjRef) (pte : PTE) : NdDlA (storePte (Z := Unit) t p pte) := by + unfold storePte setPt; dl_auto + +theorem ptLookupFromLevelN_dlA : ∀ (n : Nat) (lv : VmLevel) (p : ObjRef) (v : VSpaceRef) (t : ObjRef), + NdDlA (ptLookupFromLevelN (Z := Unit) n lv p v t).run + | 0, lv, p, v, t => by unfold ptLookupFromLevelN; dl_auto + | n + 1, lv, p, v, t => by + unfold ptLookupFromLevelN + dl_auto + all_goals exact ptLookupFromLevelN_dlA n _ _ _ _ + +theorem deleteAsid_dlA (asid : Asid) (pt : ObjRef) : NdDlA (deleteAsid (Z := Unit) asid pt) := by + unfold deleteAsid + refine NdDlA.bind (NdDlA.gets _) fun o => ?_ + split + · exact NdDlA.pure _ + · refine NdDlA.bind (NdDlA.getsMap _ _) fun _ => NdDlA.when ?_ + exact NdDlA.bind (invalidateTlbByAsid_dlA _) fun _ => NdDlA.bind (invalidateAsidEntry_dlA _) fun _ => + NdDlA.bind (NdDlA.getsMap _ _) fun _ => NdDlA.bind (NdDlA.pure _) fun _ => + NdDlA.bind (setAsidPool_dlA _ _) fun _ => NdDlA.bind (NdDlA.gets _) fun _ => setVmRoot_dlA _ + +theorem deleteAsidPool_dlA (base : Asid) (ptr : ObjRef) : NdDlA (deleteAsidPool (Z := Unit) base ptr) := by + unfold deleteAsidPool + refine NdDlA.bind (NdDlA.assert _) fun _ => NdDlA.bind (NdDlA.gets _) fun _ => NdDlA.when ?_ + refine NdDlA.bind (NdDlA.getsMap _ _) fun _ => NdDlA.bind (NdDlA.mapM (fun _ => NdDlA.when ?_) _) fun _ => + NdDlA.bind (NdDlA.pure _) fun _ => NdDlA.bind (NdDlA.modify fun _ => dlA_eqA rfl rfl rfl rfl rfl rfl rfl rfl rfl) fun _ => + NdDlA.bind (NdDlA.gets _) fun _ => setVmRoot_dlA _ + exact NdDlA.bind (invalidateTlbByAsid_dlA _) fun _ => invalidateAsidEntry_dlA _ + +theorem unmapPageTable_dlA (asid : Asid) (v : VSpaceRef) (pt : ObjRef) : + NdDlA (unmapPageTable (Z := Unit) asid v pt) := by + unfold unmapPageTable + refine NdDlA.catch ?_ fun _ => NdDlA.pure _ + refine NdDlA.runBind (findVspaceForAsid_dlA _) fun _ => NdDlA.runBind ?_ fun x => ?_ + · unfold ptLookupFromLevel; exact ptLookupFromLevelN_dlA _ _ _ _ _ + · obtain ⟨sl, lv⟩ := x + exact NdDlA.runBind (NdDlA.liftE (storePte_dlA _ _ _)) fun _ => + NdDlA.runBind (NdDlA.liftE (doMachineOp_dlA _)) fun _ => NdDlA.liftE (invalidateTlbByAsid_dlA _) + +theorem unmapPage_dlA (sz : VMPageSize) (asid : Asid) (v : VSpaceRef) (p : ObjRef) : + NdDlA (unmapPage (Z := Unit) sz asid v p) := by + unfold unmapPage + refine NdDlA.catch ?_ fun _ => NdDlA.pure _ + refine NdDlA.runBind (findVspaceForAsid_dlA _) fun _ => NdDlA.runBind (NdDlA.liftE (NdDlA.getsThe _)) fun x => ?_ + obtain ⟨lv, sl⟩ := x + exact NdDlA.runBind (NdDlA.unlessE (NdDlA.throwError _)) fun _ => + NdDlA.runBind (NdDlA.liftE (NdDlA.getsMap _ _)) fun _ => + NdDlA.runBind (NdDlA.unlessE (NdDlA.throwError _)) fun _ => + NdDlA.runBind (NdDlA.liftE (storePte_dlA _ _ _)) fun _ => + NdDlA.runBind (NdDlA.liftE (doMachineOp_dlA _)) fun _ => NdDlA.liftE (invalidateTlbByAsidVa_dlA _ _) + +theorem storeExtSlot_dlA (sl : ExtSlot) (pte : PTE) : NdDlA (storeExtSlot (Z := Unit) sl pte) := by + cases sl with + | root r i => unfold storeExtSlot; dl_auto + | pt p => exact storePte_dlA _ _ _ + +theorem findExtRoot_dlA (k : ExtRootKind) (asid : Asid) : NdDlA (findExtRoot (Z := Unit) k asid).run := by + unfold findExtRoot findAnyRootForAsid; dl_auto + +theorem extUnmapPage_dlA (k : ExtRootKind) (sz : VMPageSize) (asid : Asid) (v : VSpaceRef) (p : ObjRef) : + NdDlA (extUnmapPage (Z := Unit) k sz asid v p) := by + unfold extUnmapPage + refine NdDlA.catch ?_ fun _ => NdDlA.pure _ + refine NdDlA.runBind (NdDlA.unlessE (NdDlA.throwError _)) fun _ => NdDlA.runBind (findExtRoot_dlA _ _) fun _ => ?_ + exact NdDlA.runBind (NdDlA.liftE (NdDlA.gets _)) fun _ => NdDlA.runBind (NdDlA.unlessE (NdDlA.throwError _)) fun _ => + NdDlA.runBind (NdDlA.liftE (NdDlA.gets _)) fun _ => NdDlA.runBind (NdDlA.unlessE (NdDlA.throwError _)) fun _ => + NdDlA.liftE (storeExtSlot_dlA _ _) + +theorem extUnmapTable_dlA (k : ExtRootKind) (asid : Asid) (v : VSpaceRef) (pt : ObjRef) : + NdDlA (extUnmapTable (Z := Unit) k asid v pt) := by + unfold extUnmapTable + refine NdDlA.catch ?_ fun _ => NdDlA.pure _ + refine NdDlA.runBind (findExtRoot_dlA _ _) fun _ => NdDlA.runBind (NdDlA.liftE (NdDlA.gets _)) fun o => ?_ + cases o with + | some sl => exact NdDlA.liftE (storeExtSlot_dlA _ _) + | none => exact NdDlA.returnOk _ + +theorem extKindOfAsid_dlA (asid : Asid) : NdDlA (extKindOfAsid (Z := Unit) asid) := by + unfold extKindOfAsid; dl_auto + +/-- **`archFinaliseCap` is a `DlA` step** on every arch cap but a VCPU's. -/ +theorem archFinaliseCap_dlA (c : ArchCap) (hc : ∀ v, c ≠ .vcpuCap v) (d : Bool) : + NdDlA (archFinaliseCap (Z := Unit) c d) := by + unfold archFinaliseCap + split + · exact NdDlA.bind (deleteAsidPool_dlA _ _) fun _ => NdDlA.pure _ + · exact NdDlA.bind (deleteAsid_dlA _ _) fun _ => NdDlA.pure _ + · exact NdDlA.bind (unmapPageTable_dlA _ _ _) fun _ => NdDlA.pure _ + · exact NdDlA.bind (unmapPage_dlA _ _ _ _) fun _ => NdDlA.pure _ + all_goals first | exact NdDlA.pure _ | exact absurd rfl (hc _) | (exfalso; simp_all) + +/-- **`fl4FinaliseCap` is a `DlA` step** on every arch cap but a VCPU's (module docstring). -/ +theorem fl4FinaliseCap_dlA (c : ArchCap) (hc : ∀ v, c ≠ .vcpuCap v) (d : Bool) : + NdDlA (fl4FinaliseCap (Z := Unit) (.archObjectCap c) d) := by + have hF : NdDlA (finaliseCap (Z := Unit) (.archObjectCap c) d) := by + unfold finaliseCap; exact archFinaliseCap_dlA c hc d + unfold fl4FinaliseCap + split + all_goals first + | exact hF + | (refine NdDlA.bind (extKindOfAsid_dlA _) fun k => ?_; split + all_goals first + | exact hF + | exact NdDlA.pure _ + | exact NdDlA.bind (extUnmapPage_dlA _ _ _ _ _) fun _ => NdDlA.pure _ + | (split + · exact NdDlA.bind (extUnmapTable_dlA _ _ _ _) fun _ => NdDlA.pure _ + · exact NdDlA.pure _)) + | exact NdDlA.bind (extUnmapPage_dlA _ _ _ _ _) fun _ => NdDlA.pure _ + | (split + · split + · exact NdDlA.bind (deleteAsid_dlA _ _) fun _ => NdDlA.pure _ + · exact NdDlA.pure _ + · exact NdDlA.pure _) + | (exfalso; simp_all) + + +/-- **`FinBodySpecA` for the six VSpace kinds** (module docstring). -/ +theorem finBodySpec_vsA {k : FinKindA} {ct : ObjRef} {m : McState} {Pr : State Unit → Prop} + (hk : k = .frame ∨ k = .pageTable ∨ k = .pml4 ∨ k = .highVSpace ∨ k = .ept ∨ k = .asidPool) : + FinBodySpecA fl4FinaliseCap k ct m Pr := by + intro c ac d σ x σ' _ _ hck hac hmem + have hA : ∃ y, ac = .archObjectCap y ∧ ∀ v, y ≠ .vcpuCap v := by + rcases hac.eq_or_pt with e | ⟨p', a', v', e⟩ + · subst e + rcases hk with rfl | rfl | rfl | rfl | rfl | rfl <;> cases c <;> simp [finKindA] at hck <;> + first + | exact ⟨_, rfl, fun v h => by cases h⟩ + | exact ⟨_, rfl, fun v h => by unfold absFrameE at h; split at h <;> cases h⟩ + · exact ⟨_, e, fun v h => by cases h⟩ + obtain ⟨y, rfl, hy⟩ := hA + exact (fl4FinaliseCap_dlA y hy d σ (x, σ') hmem).step + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3DelFrameA.lean b/proofs/Fl4ProofsA64/P3/Stage3DelFrameA.lean new file mode 100644 index 000000000..bebfb1e23 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3DelFrameA.lean @@ -0,0 +1,313 @@ +/- +P3 (AArch64, A64-P3 DelBodyA, Step 0 freeze): **the body-end frame of the deletion loop** (statements only). + +Every dispatch arm must end in `BodyEndA`; after a generic `cdt::delete` nothing provided it. The loop carries a +state invariant beside its record (as `DelXSV` carries `SchedValidA`): + +* `DelBodyInvA ct m σ`: `BodyOKA σ m`, the current thread is still `ct`, no switch is pending (the loop's + steps end in `reschedule_required` or leave the action; `SwA` is then trivial), and `DelBudA σ` (integration: + the unguarded head facts, `BodyExitA.budget` before the restore line's guard, which the preemption tick needs). +* `BodyPreA σ m`: `BodyOKA` less `OffOKA`. The preemption point's `Preempted` exit may leave the head insufficient + (the spec throws exactly then), so only `OffReadyA` can be promised there; the exit's `check_budget` / + `charge_budget` (`check_budget_entry_refinesA`) need `CurInvA` and `OffReadyA`, not `OffOKA`. +* `DelOwnA σ`: the unguarded owner clause, carried by `DelWBodyA` (kept by `DelStepA` and by the clock tick). +* `DelWBodyA W ct m` / `DelWBodyPA W ct m`: a loop invariant `W` with the body facts (the hooks' `W` / `Wx` / + `Wp` become `DelWBodyA (DelWTopA ..)` / `DelWBodyA (DelWxTopA ..)` / `DelWBodyPA (DelWTopA ..)`). +* `DelBodyEndA σ σ' m r`: the exit clause by result (`BodyEndA` on `Ok`, `BodyPreA` on `Preempted`). +* `CnodeDelBodyOutA`: `CnodeDelOutA` with that clause at the final state (and `BodyEndA σ σ m` on a decode error). + +No named premise. New module (definitions only). +-/ +import Fl4ProofsA64.P3.Stage3DelTopA +import Fl4ProofsA64.P3.Stage3TcbBodyA +import Fl4ProofsA64.P3.Stage3ChKeepA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +/-- **`BodyOKA` without `OffOKA`**: what a `Preempted` exit still gives. -/ +structure BodyPreA (σ : State Unit) (m : McState) : Prop where + validTime : ValidMachineTimeA σ + ready : ∀ y n, σ.kheap σ.curSc = some (.schedContext y n) → (σ.curSc = idleScPtr ∨ scActive y = true) → + OffReadyA σ + ctAct : σ.schedulerAction = .resumeCurThread → ∀ x, σ.kheap σ.curThread = some (.tcb x) → + ActivateStateOKA x.tcbState + sw : SwA σ + scNotIdle : σ.curSc ≠ idleScPtr → NotIdleScA m σ.curSc + owner : ∀ y n, σ.kheap σ.curSc = some (.schedContext y n) → scActive y = true → σ.curSc ≠ idleScPtr → + σ.consumedTime ≠ 0 → ∀ o, y.scTcb = some o → o ∉ σ.releaseQueue + +/-- (integration) The current SC's head facts without `BodyExitA.budget`'s guard (the restore line's +`consumedTime ≠ 0 ∨ resumeCurThread`): the deletion loop keeps them from the entry record. -/ +def DelBudA (σ : State Unit) : Prop := + ∀ y n, σ.kheap σ.curSc = some (.schedContext y n) → (σ.curSc = idleScPtr ∨ scActive y = true) → + OffReadyA σ ∧ OffOKA σ + +theorem BodyOKA.toPre {σ : State Unit} {m : McState} (h : BodyOKA σ m) (hb : DelBudA σ) : BodyPreA σ m := + ⟨h.validTime, fun y n hk ha => (hb y n hk ha).1, h.ctAct, h.sw, h.scNotIdle, h.owner⟩ + +/-- The thread `ct` is in no ready queue and is not the target of a pending switch. -/ +def CtOffA (ct : ObjRef) (σ : State Unit) : Prop := + (∀ d p, ct ∉ σ.readyQueues d p) ∧ ∀ t, σ.schedulerAction = .switchThread t → t ≠ ct + +/-- `ct`'s TCB, if still one, was one before and keeps its scheduling context or has none. -/ +def CtScKA (ct : ObjRef) (σ σ' : State Unit) : Prop := + ∀ x', σ'.kheap ct = some (.tcb x') → ∃ x, σ.kheap ct = some (.tcb x) ∧ + (x'.tcbSchedContext = x.tcbSchedContext ∨ x'.tcbSchedContext = none) + +theorem CtScKA.refl (ct : ObjRef) (σ : State Unit) : CtScKA ct σ σ := fun x h => ⟨x, h, Or.inl rfl⟩ + +theorem CtScKA.trans {ct : ObjRef} {a b c : State Unit} (h1 : CtScKA ct a b) (h2 : CtScKA ct b c) : CtScKA ct a c := by + intro x hx + obtain ⟨y, hy, e⟩ := h2 x hx + obtain ⟨z, hz, f⟩ := h1 y hy + refine ⟨z, hz, ?_⟩ + rcases e with e | e + · rcases f with f | f + · exact Or.inl (e.trans f) + · exact Or.inr (e.trans f) + · exact Or.inr e + +theorem CtScKA.ofEq {ct : ObjRef} {σ σ' : State Unit} (h : σ'.kheap = σ.kheap) : CtScKA ct σ σ' := + fun x hx => ⟨x, h ▸ hx, Or.inl rfl⟩ + +/-- One object replaced: a new TCB at `ct` comes from a TCB with the same scheduling context, or has none. -/ +theorem CtScKA.ofUpd {ct p : ObjRef} {σ σ' : State Unit} {o' : KernelObject} + (hk : σ'.kheap = Function.update σ.kheap p (some o')) + (h : ∀ x', o' = .tcb x' → ct = p → ∃ x, σ.kheap p = some (.tcb x) ∧ + (x'.tcbSchedContext = x.tcbSchedContext ∨ x'.tcbSchedContext = none)) : CtScKA ct σ σ' := by + intro x' hx' + rw [hk] at hx' + by_cases e : ct = p + · rw [e, Function.update_self] at hx' + obtain ⟨x, hx, hs⟩ := h x' (Option.some.inj hx') e + exact ⟨x, e ▸ hx, hs⟩ + · rw [Function.update_of_ne e] at hx' + exact ⟨x', hx', Or.inl rfl⟩ + +theorem CtOffA.keep {ct : ObjRef} {σ σ' : State Unit} (hr : σ'.readyQueues = σ.readyQueues) + (ha : σ'.schedulerAction = σ.schedulerAction ∨ ∀ t, σ'.schedulerAction ≠ .switchThread t) + (h : CtOffA ct σ) : CtOffA ct σ' := by + refine ⟨by rw [hr]; exact h.1, fun t e => ?_⟩ + rcases ha with ha | ha + · exact h.2 t (ha ▸ e) + · exact absurd e (ha t) + +theorem CtOffA.trans {a b c : State Unit} (e : b.curThread = a.curThread) + (h1 : CtOffA a.curThread a → CtOffA a.curThread b) (h2 : CtOffA b.curThread b → CtOffA b.curThread c) : + CtOffA a.curThread a → CtOffA a.curThread c := fun h => by + rw [e] at h2; exact h2 (h1 h) + +/-- No member of the endpoint queue at `p` is the current thread. -/ +def EpQNeA (p : ObjRef) (σ : State Unit) : Prop := + ∀ z, σ.kheap p = some (.endpoint z) → σ.curThread ∉ z.epQueue + +/-- No thread waiting on the notification at `p` is the current thread. -/ +def NtfnQNeA (p : ObjRef) (σ : State Unit) : Prop := + ∀ y q, σ.kheap p = some (.notification y) → y.ntfnObj = .waitingNtfn q → σ.curThread ∉ q + +/-- **The per-step frame the hooks prove** (internals not frozen; see `BodyOKA.stepA`): current thread, time fields, +consumed time and current SC kept; the release queue only shrinks; the current SC object keeps refills / max / thread +or (for a non-idle current SC) is deactivated, or keeps refills / max and names only a thread outside the release +queue (a reply's SC donation); the action is kept or `chooseNewThread` (never a switch); a resuming +current thread keeps its state. Time moves only at the preemption point, which has its own step. -/ +structure DelStepA (σ σ' : State Unit) : Prop where + cur : σ'.curThread = σ.curThread + time : σ'.curTime = σ.curTime + last : σ'.machineState.lastMachineTime = σ.machineState.lastMachineTime + csc : σ'.curSc = σ.curSc + cons : σ'.consumedTime = σ.consumedTime + rel : ∀ t ∈ σ'.releaseQueue, t ∈ σ.releaseQueue + sc : ∀ y' k, σ'.kheap σ.curSc = some (.schedContext y' k) → ∃ y, σ.kheap σ.curSc = some (.schedContext y k) ∧ + ((y'.scRefills = y.scRefills ∧ y'.scRefillMax = y.scRefillMax ∧ y'.scTcb = y.scTcb) ∨ + (σ.curSc ≠ idleScPtr ∧ scActive y' = false) ∨ + (y'.scRefills = y.scRefills ∧ y'.scRefillMax = y.scRefillMax ∧ ∀ o, y'.scTcb = some o → o ∉ σ'.releaseQueue)) + act : σ'.schedulerAction = σ.schedulerAction ∨ σ'.schedulerAction = .chooseNewThread + ctSt : σ'.schedulerAction = .resumeCurThread → ∀ x, σ'.kheap σ.curThread = some (.tcb x) → + ∃ x0, σ.kheap σ.curThread = some (.tcb x0) ∧ x.tcbState = x0.tcbState + off : CtOffA σ.curThread σ → CtOffA σ.curThread σ' + ctSc : CtScKA σ.curThread σ σ' + +/-- (chg) **`DelStepA` that also keeps the current SC chargeable** (`ChKA`): what the deletion loop's hooks carry to the +preempted exit. The TCB invocations' body steps (priority, MCP, timeout) end in `Ok`, so they need only `DelStepA`. -/ +structure DelStepCA (σ σ' : State Unit) : Prop extends DelStepA σ σ' where + ch : ChKA σ σ' + +instance {σ σ' : State Unit} : CoeOut (DelStepCA σ σ') (DelStepA σ σ') := ⟨DelStepCA.toDelStepA⟩ + +/-- **What the preemption point's charge needs of the current thread** (G2): `ct` is in no ready queue, its +TCB (if any) has no scheduling context or the current one, it is outside the release queue, and while the action is +still `resume_cur_thread` it is runnable. With `CurInvA`'s bound split off, this gives +`CurInvA ∨ ¬schedulable` at the preempted exit; the idle thread's facts (`ActIdleA`'s second half) come from the +record (`sr.run`, `sinv`), the switch half from `noSwitch`. -/ +def DelPreSchedA (ct : ObjRef) (σ : State Unit) : Prop := + (∀ d p, ct ∉ σ.readyQueues d p) ∧ + (∀ x, σ.kheap ct = some (.tcb x) → x.tcbSchedContext = none ∨ x.tcbSchedContext = some σ.curSc) ∧ + ct ∉ σ.releaseQueue ∧ + (σ.schedulerAction = .resumeCurThread → ∀ x, σ.kheap ct = some (.tcb x) → runnable x.tcbState = true) + +/-- `DelPreSchedA` across a hook step, given the body's no-switch fact. -/ +theorem DelPreSchedA.stepA {ct : ObjRef} {σ σ' : State Unit} (h : DelPreSchedA ct σ) (hct : σ.curThread = ct) + (hns : ∀ t, σ.schedulerAction ≠ .switchThread t) (k : DelStepA σ σ') : DelPreSchedA ct σ' := by + subst hct + refine ⟨(k.off ⟨h.1, fun t e => absurd e (hns t)⟩).1, fun x' hx' => ?_, fun hm => h.2.2.1 (k.rel _ hm), + fun hr x' hx' => ?_⟩ + swap + · have hr0 : σ.schedulerAction = .resumeCurThread := by + rcases k.act with e | e + · exact e ▸ hr + · rw [e] at hr; cases hr + obtain ⟨x0, hx0, hs⟩ := k.ctSt hr x' hx' + rw [hs]; exact h.2.2.2 hr0 x0 hx0 + obtain ⟨x, hx, e⟩ := k.ctSc x' hx' + rw [k.csc] + rcases h.2.1 x hx with e0 | e0 + · rcases e with e | e + · exact Or.inl (e.trans e0) + · exact Or.inl e + · rcases e with e | e + · exact Or.inr (e.trans e0) + · exact Or.inl e + +/-- **The loop's body invariant** (module docstring). -/ +def DelBodyInvA (ct : ObjRef) (m : McState) (σ : State Unit) : Prop := + BodyOKA σ m ∧ σ.curThread = ct ∧ (∀ t, σ.schedulerAction ≠ .switchThread t) ∧ DelBudA σ ∧ DelPreSchedA ct σ + +/-- The thread bound to an active non-idle current SC is outside the release queue (`BodyOKA.owner` without its +`consumedTime` guard: the preemption point's clock tick can make the guard true, and no recorded invariant links +`scTcb` back to the thread's SC pointer). -/ +def DelOwn0A (σ : State Unit) : Prop := + ∀ y n, σ.kheap σ.curSc = some (.schedContext y n) → scActive y = true → σ.curSc ≠ idleScPtr → + ∀ o, y.scTcb = some o → o ∉ σ.releaseQueue + +/-- (chg) `DelOwn0A` with the current SC chargeable (`CurScChargeableA`), carried by the deletion loop to the +preempted exit, where `afterPreemptA`'s `chargeable` premise comes from it. -/ +def DelOwnA (σ : State Unit) : Prop := DelOwn0A σ ∧ CurScChargeableA σ + +/-- **The preempted exit's body facts** (no switch is pending, as in the loop). -/ +def DelBodyPreA (ct : ObjRef) (m : McState) (σ : State Unit) : Prop := + BodyPreA σ m ∧ σ.curThread = ct ∧ (∀ t, σ.schedulerAction ≠ .switchThread t) ∧ DelPreSchedA ct σ ∧ DelOwnA σ + +/-- `W` with the body invariant. -/ +def DelWBodyA {S A : Type} (W : DelWA S A) (ct : ObjRef) (m : McState) : DelWA S A := + fun s a σ G => W s a σ G ∧ DelBodyInvA ct m σ ∧ DelOwnA σ + +/-- `Wp` (the preempted exit) with the body facts. -/ +def DelWBodyPA {S A : Type} (W : DelWA S A) (ct : ObjRef) (m : McState) : DelWA S A := + fun s a σ G => W s a σ G ∧ DelBodyPreA ct m σ + +/-- **The exit clause of a deletion**, by result. -/ +def DelBodyEndA (σ σ' : State Unit) (m : McState) (r : core.result.Result Unit error.Exc) : Prop := + match r with + | .Err .Preempted => BodyPreA σ' m ∧ σ'.curThread = σ.curThread ∧ (∀ t, σ'.schedulerAction ≠ .switchThread t) ∧ + DelPreSchedA σ.curThread σ' ∧ DelOwnA σ' + | _ => BodyEndA σ σ' m + +/-- **`BodyOKA` across one hook step** (labels: writes after the delete). -/ +theorem BodyOKA.stepA {σ σ' : State Unit} {m : McState} {ct : ObjRef} + (h : DelBodyInvA ct m σ) (k : DelStepA σ σ') : DelBodyInvA ct m σ' := by + obtain ⟨B, hct, hns, BB, PS⟩ := h + have PS' := PS.stepA hct hns k + have hd : ∀ y' y : SchedContext, y'.scRefills = y.scRefills → refillHd y' = refillHd y := fun y' y e => by + unfold refillHd; rw [e] + have ha : ∀ y' y : SchedContext, y'.scRefillMax = y.scRefillMax → scActive y' = scActive y := fun y' y e => by + unfold scActive; rw [e] + -- the old object behind a new one at the current SC, with the frame case + have key : ∀ y' n, σ'.kheap σ'.curSc = some (.schedContext y' n) → + ∃ y, σ.kheap σ.curSc = some (.schedContext y n) ∧ + ((y'.scRefills = y.scRefills ∧ y'.scRefillMax = y.scRefillMax ∧ y'.scTcb = y.scTcb) ∨ + (σ.curSc ≠ idleScPtr ∧ scActive y' = false) ∨ + (y'.scRefills = y.scRefills ∧ y'.scRefillMax = y.scRefillMax ∧ ∀ o, y'.scTcb = some o → o ∉ σ'.releaseQueue)) := by + intro y' n hy' + rw [k.csc] at hy' + obtain ⟨y, hy, hc⟩ := k.sc y' n hy' + exact ⟨y, hy, hc⟩ + have nsw : ∀ t, σ'.schedulerAction ≠ .switchThread t := fun t e => by + rcases k.act with e2 | e2 + · exact hns t (e2.symm.trans e) + · rw [e2] at e; cases e + have BB' : DelBudA σ' := by + intro y' n hk hg + obtain ⟨y, hy, hc⟩ := key y' n hk + rw [k.csc] at hg + have hc' : (y'.scRefills = y.scRefills ∧ y'.scRefillMax = y.scRefillMax) ∨ + (σ.curSc ≠ idleScPtr ∧ scActive y' = false) := by + rcases hc with ⟨e1, e2, -⟩ | h | ⟨e1, e2, -⟩ + exacts [Or.inl ⟨e1, e2⟩, Or.inr h, Or.inl ⟨e1, e2⟩] + rcases hc' with ⟨e1, e2⟩ | ⟨hni, hf⟩ + · have hg' : σ.curSc = idleScPtr ∨ scActive y = true := by + rcases hg with h | h + · exact Or.inl h + · exact Or.inr (by rw [← ha _ _ e2]; exact h) + obtain ⟨r1, r2⟩ := BB y n hy hg' + constructor + · intro z j hz + rw [hk] at hz + obtain ⟨rfl, -⟩ := KernelObject.schedContext.inj (Option.some.inj hz) + rw [hd _ _ e1, k.cons, k.time] + exact r1 y n hy + · intro hpos z j hz hactive + rw [hk] at hz + obtain ⟨rfl, -⟩ := KernelObject.schedContext.inj (Option.some.inj hz) + rw [k.cons] at hpos + have := r2 hpos y n hy (fun h => by + rw [← ha _ _ e2]; exact hactive (by rw [k.csc]; exact h)) + rw [hd _ _ e1, k.cons, k.time] + exact this + · rcases hg with h | h + · exact absurd (k.csc ▸ h) hni + · rw [hf] at h; cases h + refine ⟨⟨⟨?_, fun y' n hk hg _ => BB' y' n hk hg, fun h x hx => ?_⟩, fun t e => absurd e (nsw t), fun h => ?_, + fun y' n hk hac hni hcn o ho => ?_⟩, k.cur.trans hct, nsw, BB', PS'⟩ + · have := B.validTime + unfold ValidMachineTimeA at this ⊢ + rw [k.time, k.last]; exact this + · have hr := h + rcases k.act with e | e + · rw [e] at h + obtain ⟨x0, hx0, hs⟩ := k.ctSt hr x (by rw [k.cur] at hx; exact hx) + have hx0' : σ.kheap σ.curThread = some (.tcb x0) := hx0 + rw [hs]; exact B.ctAct h x0 hx0' + · rw [e] at h; cases h + · rw [k.csc]; exact B.scNotIdle (fun e => h (k.csc.trans e)) + · obtain ⟨y, hy, hc⟩ := key y' n hk + rw [k.csc] at hni + rcases hc with ⟨e1, e2, e3⟩ | ⟨-, hf⟩ | ⟨-, -, hn⟩ + rotate_left + · rw [hf] at hac; cases hac + · exact hn o ho + · rw [e3] at ho + have hac' : scActive y = true := by rw [← ha _ _ e2]; exact hac + exact fun hm => B.owner y n hy hac' hni (by rw [← k.cons]; exact hcn) o ho (k.rel _ hm) + +section +variable {S : Type} (st : machine.Store S) [L : LawfulStore st] {A : Type} + +/-- **`CnodeDelOutA` with the body-end clause** (the frozen output of CNode Delete / Revoke). -/ +def CnodeDelBodyOutA (AR : A → HwRootA → Prop) (Z : XSetsA) (H : Heap) (OS U Pp Tb : Set U64) (Lv : U64 → Nat) + (ia ie : Nat) (N E Rp : Set U64) (κ : U64 → U64) + (n : Nat) (m : Fl4SpecA64.McState) (ar : machine.Arch A) (Clock : A → Fl4SpecA64.MachineState → Prop) (ipa : U64) + (s : S) (a : A) (σ : Fl4SpecA64.State Unit) + (msp : Fl4SpecA64.SEMonad Unit Fl4SpecA64.CnodeInvocation) : + core.result.Result Unit error.Exc × S × A → Prop + | (r, s3, a3) => + (∃ e e', r = .Err e ∧ s3 = s ∧ a3 = a ∧ msp.run σ = ({(.error e', σ)}, False) ∧ SysErrRelA e e' ∧ + BodyEndA σ σ m) ∨ + ∃ inv σ1 σ3 G', msp.run σ = ({(.ok inv, σ)}, False) ∧ + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ((r = .Ok () ∧ (.ok (), σ3) ∈ ((Fl4SpecA64.fl4InvokeCnode (Z := Unit) inv).run σ1).1) ∨ + (r = .Err .Preempted ∧ (.error (), σ3) ∈ ((Fl4SpecA64.fl4InvokeCnode (Z := Unit) inv).run σ1).1)) ∧ + ∃ (Q : U64 → U64 → List U64) (R : U64 → List U64) (hw : HwRootA) (Ac' : Set U64) + (Bf' : Set (U64 × Fl4SpecA64.Word)) (stack' : U64 → List U64), + JStage3A st AR { Z with G := G', Q := Q, R := R, hw := hw } H OS U Ac' Pp Tb Lv ia ie N E Rp Bf' stack' κ n + (J5A st) (J7A st (J7R2A st ar ipa)) s3 a3 σ3 m ∧ DelBodyEndA σ σ3 m r ∧ Clock a3 σ3.machineState + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3DelInA.lean b/proofs/Fl4ProofsA64/P3/Stage3DelInA.lean new file mode 100644 index 000000000..b514da965 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3DelInA.lean @@ -0,0 +1,296 @@ +/- +P3 (aarch64, A64-P3 Stage-3 T3 item G3, part 1): **the `JStage3A → DelIn` adapter** (lead 23:15Z, G3 freeze table +of `handoff/a64-t3-gate1.md`, D rows; the F rows are record fields since record edit #5 and are projected here). + +* **`J7UnpA`** / **`J7A.unp`**: the concrete J7 slot `J7A (J7R2A ar n)` unpacked into one named record (`Pt`, `Ub`, + the R2 sets and level functions, `KTab` at the kernel-table level function, `S1aHiK`; record edit #6's `J7R2K`). +* The state-free rows of `DelS`: `heapWin_p1A` (F1 → A64 P1's `HeapWin`, hence `WinOK`), `JStage3A.winOK`, + `JStage3A.dcan` (`DCan (OSN OS ksb)` by `OSOK.canon`), `JStage3A.wuApart` (`SysL.ly.kslo` at `WORK_UNITS = 40`), + `JStage3A.irqOK` (`PlaceA.irq` → `IsSlotA` → J5's `IsSlotA → OS`). +* `VcOK`: `JStage3A.vcOK` — every field but `os` is derived (`sc`, `fit`, `disj` from `VcH` and `HeapOK`; `ubos` by + `UbFr.offOS`; `ubv` from F2; `bcov` from `S1aCase.ts`). `VcOK.os` (no `OS` slot covers a VCPU word) was not + derivable from the record (G3 stop-first: `SlotApA.vc` places only the VCPU's `TCB` and `RUN_CALLER` words, and an + `OS` slot that is no `IsSlotA` slot may lie in any `other` object); it is record edit #6's field `vcOS`. +* `VcTy`: `JStage3A.vcTy` (`obj` from `VcH` + `VcRunA.vtcb` / `.vcpu`, `bnd` / `run` from `VcRunA`, `usr` by + `UbFr.usrW`, `buf` from F2's `FrUb` (`runBuf_ofFrUbA`), `ks` from J7's `schedApart`). +* `IrqN`: `JStage3A.irqN` (`IrqRelA.node`, the IRQ slot's one-slot CNode (`PlaceA.irq`, `StateRelA.cnode`) and + `SInvXA.irqN.slot`: the spec slot is null or a notification cap, so the related FL4 cap is). +* **`JStage3A.delIn`**: A64 P1's `DelIn` at the record's heap, `OS`, `Z.V`, J7's `Ub` / `Pt`, `K := S2Q` and the + R2 typing `VsTyR … ∧ S2TyR …` at the active pool set (as `w0_delIn` / `w1_delIn`), from the record, J5A / J7A, + [P] `P0.ArchTotal ar` and [P] `ArchPlat ar` (`n = hPl.ipa`). + +Named premises: [P] `P0.ArchTotal`, [P] `ArchPlat` (as the boot witnesses). New module; no existing statement +changed. +-/ +import Fl4ProofsA64.P3.Stage3J5A +import Fl4ProofsA64.P3.Stage3Rec5A +import Fl4ProofsA64.P3.IrqInvokeSlotsA +import Fl4ProofsA64.P3.CnodeDelDefA +import Fl4ProofsA64.P1.S1aHiDelR +import Fl4ProofsA64.P1.MdbDelIn +import Fl4ProofsA64.P1.MdbDelVsZ +import Fl4ProofsA64.P1.S1aBootV +import Fl4ProofsA64.P1.MdbWin +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-! ## J7 unpacked -/ + +variable (st) in +/-- **The concrete J7 slot, unpacked** (module docstring). -/ +structure J7UnpA {A : Type} (ar : machine.Arch A) (n : U64) (Z : XSetsA) (H : Heap) (OS : Set U64) (s : S) + (Pt Ub Pp Tb Hr Te Kt : Set U64) (Lv Re bits KLv : U64 → Nat) : Prop where + vr : VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt + pn : PoolNamed st OS s + col : S1aArmCol st H Z.Cs OS Z.V Ub s + cc : S1aCapCol st H OS Ub s + vcc : VcCap st Z.V OS s + df : DevFrAl st OS s + h511 : HiCap511 st OS s + sp : ∀ p, SchedPageA st Z OS s p → p ∈ Pt + ks : ∀ b, VcW Z.V b → ∀ o : U64, o.val < 223488 → wa (L.ksb s) o ≠ b + fu : FrUb st Ub OS s + al : ∀ b ∈ Ub, Aligned b + vu : ∀ b, VcW Z.V b → b ∉ Ub + r2 : S1aR2 st H OS Pt Pp Tb Hr Te Kt Lv Re bits n s + sep : S1aKtUt st OS Kt s + ktK : KTab ar Kt KLv + hiK : S1aHiK st OS Kt KLv s + +/-- **`J7A (J7R2A ar n)` unpacked.** -/ +theorem J7A.unp {A : Type} {ar : machine.Arch A} {n : U64} {Z : XSetsA} {H : Heap} {OS : Set U64} {s : S} + {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} (h : J7A st (J7R2A st ar n) Z H OS s σ m) : + ∃ Pt Ub Pp Tb Hr Te Kt : Set U64, ∃ Lv Re bits KLv : U64 → Nat, + J7UnpA st ar n Z H OS s Pt Ub Pp Tb Hr Te Kt Lv Re bits KLv := by + obtain ⟨Pt, Ub, vr, pn, col, cc, vcc, df, h511, sp, ks, fu, al, vu, Pp, Tb, Hr, Te, Kt, Lv, Re, bits, r2, sep, + KLv, ktK, hiK⟩ := h + exact ⟨Pt, Ub, Pp, Tb, Hr, Te, Kt, Lv, Re, bits, KLv, + ⟨vr, pn, col, cc, vcc, df, h511, sp, ks, fu, al, vu, r2, sep, ktK, hiK⟩⟩ + +/-! ## Generic lemmas -/ + +/-- **F1 gives A64 P1's `HeapWin`** (`PPTR_BASE + 2^44 ≤ PPTR_TOP`). -/ +theorem heapWin_p1A {H : Heap} + (h : ∀ p k, H p = some k → + layout.PPTR_BASE.val ≤ p.val ∧ p.val + 2 ^ k.bits ≤ layout.PPTR_BASE.val + 2 ^ 44) : HeapWin H := + fun p k hp => by + obtain ⟨h1, h2⟩ := h p k hp + have e1 := Fl4ProofsA64.P0.Consts.layout.PPTR_BASE.val_eq + have e2 := Fl4ProofsA64.P0.Consts.layout.PPTR_TOP.val_eq + exact ⟨h1, le_trans h2 (by rw [e1, e2]; norm_num)⟩ + +/-- An IRQ-node slot is a slot of the typed heap (its one-slot CNode, `PlaceA.irq`). -/ +theorem isSlot_irqA {th : TypedHeapA} {kb : U64} (hP : PlaceA th kb) {i : U64} (hi : i.val < 5376) : + IsSlotA th (irqSlotA kb i) := + ⟨irqSlotA kb i, Or.inl ⟨0, hP.irq i hi, 0, by norm_num, by + rw [slotAddrA_sp] + apply UScalar.eq_of_val_eq + show _ = ((irqSlotA kb i).bv + BitVec.ofNat 64 0 * 32#64).toNat + simp⟩⟩ + +/-- A spec cap related to an FL4 cap: `nullCap` only for `Null`. -/ +theorem capRelA_null {c : capability.Cap} (h : CapRelA c .nullCap) : c = .Null := by + by_contra hcn + exact CapEqA.ne_null h.2 hcn h.1 rfl + +/-- A spec cap related to an FL4 cap: a notification cap only for a `Notification`. -/ +theorem capRelA_ntfn {c : capability.Cap} {r b rs} (h : CapRelA c (.notificationCap r b rs)) : + ∃ n b0 b1 b2, c = .Notification n b0 b1 b2 := by + rcases h.2.eq_or_pt with e | ⟨_, _, _, e⟩ + · cases c <;> first | exact ⟨_, _, _, _, rfl⟩ | (simp [absCapA] at e) | (cases e) + · cases e + +/-- An `S2Q` cap is no VCPU cap (any `Vc`). -/ +theorem s2q_vcCapPA {Vc : Set U64} {c : capability.Cap} (hc : S2Q c) : VcCapP Vc c := fun v e => by + subst e; simp [S2Q, PtQ, VsQ, VsCap] at hc + +/-- **The run frame of a buffer slot from `FrUb`** (as `RunBuf.frUb`, without an old `RunBuf`). -/ +theorem runBuf_ofFrUbA {H : Heap} {OS Ub : Set U64} {s : S} {y : U64} (hH : HeapOK H) (hF : FrameOK st H OS s) + (hf : FrUb st Ub OS s) (hx : sbWordA y ∈ OS) : RunBuf st Ub s y := by + intro ptr size rights dev x1 x2 x3 x4 bits sz hcap hdev hrw hbits hsz h63 hlt hge + subst hdev + have hcap0 : capA st s (sbWordA y) = .Frame ptr size rights false x1 x2 x3 x4 := + okinj ((capA_eq s _).symm.trans hcap) + have hp : H ptr = some (.other bits.val) := hF _ hx ptr size rights x1 x2 x3 x4 hcap0 bits hbits + intro i hi + obtain ⟨hal, hin⟩ := frame_off_words hH hp hbits hsz h63 hlt hge hi + exact hf _ hx ptr size rights x1 x2 x3 x4 hcap0 bits hbits _ hal hin.1 hin.2 + +/-- A word of a 4 KiB heap object by value. -/ +theorem vcw_valA {H : Heap} (hH : HeapOK H) {v o : U64} (hv : H v = some (.other 12)) (ho : o.val < 4096) : + (wa v o).val = v.val + o.val := by + have hf := hH.fits v _ hv + simp only [HKind.bits, Nat.reducePow] at hf + rw [wa_val] + exact Nat.mod_eq_of_lt (by omega) + +/-- **A VCPU object's words** (`VcWords`): aligned `other`-object words off the frame words. -/ +theorem vcWords_ofHA {H : Heap} (hH : HeapOK H) {v : U64} (hv : H v = some (.other 12)) : + VcWords H.omega H.frame v := by + intro o ho h8 + have hal := hH.align v _ hv + simp only [HKind.bits, Nat.reducePow] at hal + have e := vcw_valA hH hv ho + have hA : Aligned (wa v o) := by unfold Aligned; rw [e]; omega + refine ⟨hA, ⟨hA, v, _, hv, Or.inr (Or.inr (Or.inl ⟨12, rfl⟩)), by + simp only [InObj, HKind.bits, Nat.reducePow, e]; omega⟩, ?_⟩ + rintro ⟨-, q, k, hq, hk, q1, q2⟩ + rw [e] at q1 q2 + by_cases eq : q = v + · subst eq + rw [hv] at hq + obtain rfl := Option.some.inj hq + rcases hk with h | ⟨z, h⟩ | ⟨h, -⟩ <;> cases h + · have := hH.disj q k v _ hq hv eq + simp only [show (HKind.other 12).bits = 12 from rfl, Nat.reducePow] at this + omega + +end + +/-! ## The rows from the record -/ + +section rows +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- `WinOK` from F1. -/ +theorem JStage3A.winOK (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) : + WinOK H.tcbs H.omega := + WinOK.ofHeap (heapWin_p1A J.heapWin) + +/-- `DCan` of the `OS` slots off the IRQ node (`OSOK.canon` under F1). -/ +theorem JStage3A.dcan (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) : + DCan (OSN OS (L.ksb s)) := + fun _ hy => OSOK.canon J.sys.ent.os J.winOK hy.1 + +/-- `WuApart`: `WORK_UNITS` (offset 40) is a low state-block word (`SysL.ly.kslo`). -/ +theorem JStage3A.wuApart (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) : + WuApart OS (L.ksb s) := + fun y hy => J.sys.sysl.ly.kslo layout.ks.WORK_UNITS + (by rw [Fl4ProofsA64.P0.Consts.layout.ks.WORK_UNITS.val_eq]; omega) y hy + +/-- `IrqOK`: the IRQ-node slots are typed slots, hence `OS` slots (J5). -/ +theorem JStage3A.irqOK (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) : + IrqOK OS (OSN OS (L.ksb s)) (L.ksb s) := + ⟨fun _ hi => J.j5.2.1 _ (isSlot_irqA J.j.1.place hi), fun _ hy => hy.2⟩ + +/-- **`VcOK`** (module docstring): every field derived but `os`, which is the open hypothesis `hvos`. -/ +theorem JStage3A.vcOK {A' : Type} {ar : machine.Arch A'} {nA : U64} + {Pt Ub Pp' Tb' Hr Te Kt : Set U64} {Lv' Re bits KLv : U64 → Nat} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (W : J7UnpA st ar nA Z H OS s Pt Ub Pp' Tb' Hr Te Kt Lv' Re bits KLv) + (hvos : ∀ b, VcW Z.V b → ¬ InOS OS b) : + VcOK H.tcbs H.scs H.scBits OS Z.V Ub := by + have hH := J.sys.ent.heap + have vh := W.col.vh + refine ⟨hvos, ?_, UbFr.offOS W.cc.uf J.sys.sysl.fo, fun b hb hv => W.vu b hv hb, ?_, ?_, ?_⟩ + · rintro b ⟨v, hv, o, ho, h8, rfl⟩ c ⟨z, hc⟩ hin + have hne : v ≠ c := by + rintro rfl + rw [vh v hv] at hc + cases hc + have hz : H.scBits c = z := by simp [Heap.scBits, hc] + rw [hz] at hin + have e := vcw_valA hH (vh v hv) ho + have hd := hH.disj v _ c _ (vh v hv) hc hne + simp only [HKind.bits, Nat.reducePow] at hd + unfold InSC at hin + rw [e] at hin + omega + · intro t ht + have e : sbWordA t = slotIdx t 2#u64 := by + rw [sbWordA_eq]; apply UScalar.eq_of_val_eq + unfold slotIdx + simp [core.num.U64.wrapping_add_val_eq, core.num.U64.wrapping_mul_val_eq, U64.size_def, U64.numBits] + rw [e] + exact W.col.case.ts t ht 2#u64 (by simp) + · intro v hv + have := hH.fits v _ (vh v hv) + simpa [HKind.bits] using this + · intro v hv v' hv' hne + have := hH.disj v _ v' _ (vh v hv) (vh v' hv') hne + simpa [HKind.bits] using this + +/-- **`VcTy`** (module docstring). -/ +theorem JStage3A.vcTy {A' : Type} {ar : machine.Arch A'} {nA : U64} + {Pt Ub Pp' Tb' Hr Te Kt : Set U64} {Lv' Re bits KLv : U64 → Nat} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (W : J7UnpA st ar nA Z H OS s Pt Ub Pp' Tb' Hr Te Kt Lv' Re bits KLv) + (hbc : ∀ t ∈ H.tcbs, sbWordA t ∈ OS) : + VcTy st H.tcbs Z.Cs H.omega H H.frame H.scs H.scBits Z.V Ub s := by + have hH := J.sys.ent.heap + have R := J.vcRun + have hT := J.tcbs + refine ⟨fun v hv => ⟨vcWords_ofHA hH (W.col.vh v hv), ?_, ?_⟩, fun t ht => ?_, fun t ht => ?_, + UbFr.usrW hH W.cc.fo W.cc.uf J.sys.sysl.fo W.al J.sys.ent.ksw, + fun t ht _ => runBuf_ofFrUbA hH W.cc.fo W.fu (hbc t ht), W.ks⟩ + · have e : wa v vcpu.TCB = v := by + apply UScalar.eq_of_val_eq + rw [wa_val, show vcpu.TCB.val = 0 by simp [vcpu.TCB], Nat.add_zero] + exact Nat.mod_eq_of_lt v.bv.isLt + rw [e, hT] + exact R.vtcb v hv + · have e : vcpu.RUN_CALLER = 24#u64 := by simp [vcpu.RUN_CALLER] + rw [e, hT] + exact R.vcpu v hv + · rw [hT] at ht; exact R.tcb t ht + · rw [hT] at ht; exact R.run t ht + +/-- **`IrqN`** (module docstring). -/ +theorem JStage3A.irqN (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) : + IrqN st (L.ksb s) s := by + intro i hi c hc + have hcA : capA st s (irqSlotA (L.ksb s) i) = c := okinj ((capA_eq s _).symm.trans hc) + obtain ⟨cs, hk, -, hr⟩ := J.j.1.st.cnode _ 0 (J.j.1.place.irq i hi) + obtain ⟨ac, hac, hrel⟩ := hr [] rfl + rw [slotAddrA_nil] at hrel + have hnode := J.j.1.irq.node i hi + have hirq : (BitVec.ofNat 13 i.val).toNat < Fl4SpecA64.fl4IrqCount := by + rw [BitVec.toNat_ofNat]; unfold Fl4SpecA64.fl4IrqCount; omega + have hI := J.sinv.irqN.slot (BitVec.ofNat 13 i.val) hirq (.cnode 0 cs) [] ac (by rw [hnode]; exact hk) hac + rw [capAtA_eq, hcA] at hrel + rcases hI with rfl | ⟨r, b, rs, rfl⟩ + · exact Or.inr (capRelA_null hrel) + · exact Or.inl (capRelA_ntfn hrel) + +/-- **`DelIn` at the record** (module docstring). -/ +theorem JStage3A.delIn {ar : machine.Arch A} {Pt Ub Pp' Tb' Hr Te Kt : Set U64} {Lv' Re bits KLv : U64 → Nat} + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + (W : J7UnpA st ar hPl.ipa Z H OS s Pt Ub Pp' Tb' Hr Te Kt Lv' Re bits KLv) : + DelIn st ar H Z.Cs OS Z.V Ub Pt S2Q + (fun q => VsTyR st (L.ksb s) (PoolSet st Pp' s) ∅ Tb' Lv' q ∧ + S2TyR st hPl.ipa (PoolSet st Pp' s) Hr Te Re bits q) s := + have R := HiDelTyR.ofR2 W.r2 + have hH := J.sys.ent.heap + have V := J.vcOK W J.vcOS + { del := ⟨hA, hH, J.sys.ent.os, J.ap.sca, V, fun _ hy => hy.1, J.dcan, + fun b hb => (UbFr.omega hH W.cc.fo W.cc.uf W.al b hb).1, W.vr, J.wuApart, J.irqOK, J.ap.irqSt⟩ + fin := finKT_s2R hA hPl W.vr R.pools R.tables R.granules R.high R.stage2 + tyk := tyk_s2R R.pools R.tables R.granules R.high R.stage2 + kl := fun _ hc => s2q_noLive hc + kv := fun _ hc => s2q_vcCapPA hc + s2 := fun _ h => h + ts := W.col.case.ts + vty := J.vcTy W V.bcov + ty := R.ty + irq := J.irqN + vcc := W.vcc + fu := W.fu } + +end rows + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3DelReplyRemoveA.lean b/proofs/Fl4ProofsA64/P3/Stage3DelReplyRemoveA.lean new file mode 100644 index 000000000..5967be25b --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3DelReplyRemoveA.lean @@ -0,0 +1,809 @@ +/- +P3 (aarch64, A64-P3 Stage 3, T3 G4b / F8, delete hooks): **F8 across `reply_remove`** (second module of item 4, after +`P3/Stage3DelSchedValidA`). + +`reply_remove caller r` unlinks `r` from its SC (`replyUnlinkSc`), donates that SC to `caller` when `caller` has none +(`schedContextDonate`, which unbinds the SC from its old owner), and unlinks `caller` (`replyUnlinkTcb`: `Inactive`). + +Side hypothesis `RqDomPrio σ` (Architect-cleared form, ruling 00:56Z: a named in-module hypothesis, no record edit): +every ready-queue member sits in the queue of its own domain and priority. `tcbSchedDequeue` filters only that queue; +without the fact the SC's old owner could stay queued after its SC is removed. **Record source** (to be wired at the +entry glue; it is derivable, not a new premise): `SchedRestA` (`P3/JointA`) has `rqDom` (spec queues of other domains +are empty), `rq` (the domain-0 queue of priority `p` is `Z.Q (L.cpu s) p` mapped by `specPtr`) and `prio` (each TCB of +`Z.T` is a spec TCB with domain 0 and `(prioA t).val = x.tcbPriority.toNat`); `SchedCoreA.inQ` (`P3/ScCoreA`) gives +`prioA t = p` for every member `t` of `Q c p`. So a spec queue member `specPtr t` at `(d, p')` has `d = 0 = +x.tcbDomain` and `p' = x.tcbPriority`. + +Nothing else is assumed: that `caller` has no SC (the donate guard) puts it out of the ready and release queues and +makes it not the current thread by F8 itself. + +Pieces: `CellsEq` (the ready queues and every TCB cell kept) with a `PresR` calculus for the reads and the SC / reply +writes of the prefix; then the donate block as a Hoare triple. +-/ +import Fl4ProofsA64.P3.Stage3DelSchedValidA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P3 Classical + +noncomputable section + +/-- Every ready-queue member sits in the queue of its own domain and priority. -/ +def RqDomPrio (σ : State Unit) : Prop := + ∀ f d p x, σ.kheap f = some (.tcb x) → f ∈ σ.readyQueues d p → d = x.tcbDomain ∧ p = x.tcbPriority + +/-- The ready queues and every TCB cell are kept. -/ +def CellsEq (σ σ' : State Unit) : Prop := + σ'.readyQueues = σ.readyQueues ∧ ∀ p x, σ.kheap p = some (.tcb x) ↔ σ'.kheap p = some (.tcb x) + +instance : PreO CellsEq where + refl _ := ⟨rfl, fun _ _ => Iff.rfl⟩ + trans h1 h2 := ⟨h2.1.trans h1.1, fun p x => (h1.2 p x).trans (h2.2 p x)⟩ + +theorem RqDomPrio.ofCells {σ σ' : State Unit} (h : RqDomPrio σ) (c : CellsEq σ σ') : RqDomPrio σ' := + fun f d p x hx hm => h f d p x ((c.2 f x).2 hx) (by rw [c.1] at hm; exact hm) + +abbrev CPres {α : Type} (m : SMonad Unit α) : Prop := PresR CellsEq m + +class CPresInst {α : Type} (m : SMonad Unit α) : Prop where + pres : CPres m + +instance {α β : Type} {m : SMonad Unit α} {f : α → SMonad Unit β} [hm : CPresInst m] + [hf : ∀ a, CPresInst (f a)] : CPresInst (m >>= f) := + ⟨PresR.bind hm.pres fun a => (hf a).pres⟩ +instance {α : Type} (a : α) : CPresInst (pure a : SMonad Unit α) := ⟨PresR.pure a⟩ +instance {α : Type} (f : State Unit → α) : CPresInst (NonDet.gets f : SMonad Unit α) := ⟨PresR.gets f⟩ +instance {α : Type} (f : State Unit → Option α) : CPresInst (NonDet.getsThe f : SMonad Unit α) := + ⟨PresR.getsThe f⟩ +instance {α : Type} (o : Option α) : CPresInst (NonDet.assertOpt o : SMonad Unit α) := ⟨PresR.assertOpt o⟩ +instance (P : Prop) : CPresInst (NonDet.assert P : SMonad Unit Unit) := ⟨PresR.assert P⟩ +instance {α : Type} : CPresInst (NonDet.fail : SMonad Unit α) := ⟨PresR.fail⟩ +instance {P : Prop} {d : Decidable P} {m : SMonad Unit Unit} [h : CPresInst m] : + CPresInst (@NonDet.when (State Unit) P d m) := ⟨PresR.when h.pres⟩ +instance {α : Type} {P : Prop} {d : Decidable P} {a b : SMonad Unit α} [ha : CPresInst a] [hb : CPresInst b] : + CPresInst (@ite _ P d a b) := ⟨PresR.ite' ha.pres hb.pres⟩ +instance {α β : Type} {f : α → β} {m : SMonad Unit α} [h : CPresInst m] : CPresInst (NonDet.liftM f m) := + ⟨PresR.bind h.pres fun a => PresR.pure (f a)⟩ + +macro "cpres" : tactic => `(tactic| + repeat' (first + | exact CPresInst.pres + | refine PresR.bind_pure _ _ ?_ + | refine PresR.bind ?_ (fun _ => ?_) + | refine PresR.when ?_ + | refine PresR.ite' ?_ ?_ + | dsimp only + | split)) + +/-- Replacing a non-TCB cell by a non-TCB object keeps the cells. -/ +theorem cells_upd {σ : State Unit} {p : ObjRef} {ko o : KernelObject} (hp : σ.kheap p = some ko) + (h1 : ∀ x, ko ≠ .tcb x) (h2 : ∀ x, o ≠ .tcb x) : + CellsEq σ { σ with kheap := Function.update σ.kheap p (some o) } := by + refine ⟨rfl, fun q x => ?_⟩ + by_cases e : q = p + · subst e + constructor + · intro h; rw [hp] at h; exact absurd (Option.some.inj h) (h1 x) + · intro h + have : o = .tcb x := by simpa using h + exact absurd this (h2 x) + · simp [Function.update_of_ne e] + +theorem setSimpleKo_cells {α : Type} [Nonempty α] (f : α → KernelObject) (hf : ∀ e, NSKo (f e)) (p : ObjRef) + (e : α) : CPres (setSimpleKo (Z := Unit) f p e) := by + intro σ r hr + unfold setSimpleKo at hr + obtain ⟨y1, hy1, h1⟩ := mem_bindA.1 hr + have hy1' : y1 ∈ (NonDet.assertOpt (σ.kheap p) σ).1 := hy1 + cases hk : σ.kheap p with + | none => rw [hk] at hy1'; exact absurd hy1' (Set.notMem_empty _) + | some ko => + rw [hk] at hy1' + have e1 : y1 = (ko, σ) := hy1' + subst e1 + obtain ⟨y2, hy2, h2⟩ := mem_bindA.1 h1 + obtain ⟨hP2, rfl⟩ := hy2 + obtain ⟨y3, hy3, h3⟩ := mem_bindA.1 h2 + obtain ⟨hP3, rfl⟩ := hy3 + obtain ⟨ko', hko, hr2⟩ := setObject_memA h3 + have hns : NSKo ko := by + unfold partialInv at hP3 + by_cases hu : ∃! y, f y = ko + · obtain ⟨y, hy, _⟩ := hu + rw [← hy]; exact hf y + · rw [if_neg hu] at hP3; exact absurd rfl hP3 + rw [hr2] + exact cells_upd hk hns.1 (hf e).1 + +theorem getSimpleKo_cells {α : Type} [Nonempty α] (f : α → KernelObject) (p : ObjRef) : + CPres (getSimpleKo (Z := Unit) f p) := by + unfold getSimpleKo + refine PresR.bind (PresR.getsThe _) (fun kobj => ?_) + refine PresR.bind (PresR.assert _) (fun _ => ?_) + cases partialInv f kobj with + | some e => exact PresR.pure e + | none => exact PresR.fail + +theorem updateSc_cells (p : ObjRef) (f : SchedContext → SchedContext) : + CPres (updateSchedContext (Z := Unit) p f) := by + intro σ r hr + obtain ⟨y1, hy1, h⟩ := hr + have hy1' : y1 ∈ (NonDet.assertOpt (σ.kheap p) σ).1 := hy1 + cases hk : σ.kheap p with + | none => rw [hk] at hy1'; exact absurd hy1' (Set.notMem_empty _) + | some ko => + rw [hk] at hy1' + have e1 : y1 = (ko, σ) := hy1' + subst e1 + cases ko with + | schedContext sc n => + obtain ⟨ko', hko, hr2⟩ := setObject_memA h + rw [hr2] + exact cells_upd hk (fun x h => by cases h) (fun x h => by cases h) + | _ => exact absurd h (Set.notMem_empty _) + +instance (p : ObjRef) : CPresInst (getObject (Z := Unit) p) := ⟨by unfold getObject; exact PresR.getsThe _⟩ +instance (p : ObjRef) : CPresInst (getSchedContext (Z := Unit) p) := ⟨by unfold getSchedContext; cpres⟩ +instance (p : ObjRef) : CPresInst (getReply (Z := Unit) p) := ⟨getSimpleKo_cells _ p⟩ +instance {α : Type} (f : Tcb → α) (t : ObjRef) : CPresInst (threadGet (Z := Unit) f t) := + ⟨by unfold threadGet; cpres⟩ +instance (f : Tcb → Option ObjRef) (r : ObjRef) : CPresInst (getTcbObjRef (Z := Unit) f r) := + ⟨by unfold getTcbObjRef; cpres⟩ +instance (p : ObjRef) (f : SchedContext → SchedContext) : CPresInst (updateSchedContext (Z := Unit) p f) := + ⟨updateSc_cells p f⟩ +instance (r : ObjRef) (u : (Option ObjRef → Option ObjRef) → Reply → Reply) (v : Option ObjRef) : + CPresInst (setReplyObjRef (Z := Unit) u r v) := + ⟨by + unfold setReplyObjRef updateSkObjRef + exact PresR.bind (getSimpleKo_cells _ _) fun _ => setSimpleKo_cells _ nsko_reply _ _⟩ + +instance {α β : Type} {f : α → β} {m : SMonad Unit α} [h : TPresInst m] : TPresInst (NonDet.liftM f m) := + ⟨PresR.bind h.pres fun a => PresR.pure (f a)⟩ + +theorem replyUnlinkSc_tpres (sc rp : ObjRef) : TPres (replyUnlinkSc (Z := Unit) sc rp) := by + unfold replyUnlinkSc; tpres + +theorem replyUnlinkSc_cpres (sc rp : ObjRef) : CPres (replyUnlinkSc (Z := Unit) sc rp) := by + unfold replyUnlinkSc; cpres + + +/-! ## `FIX`: `FIA` with exempt threads + +`FIX ea eb`: as `FIA`, but the act clause is waived when the current thread satisfies `ea`, and the blocked-SC clause is +waived for the threads satisfying `eb`. Both are transient states of `schedContextDonate` / `replyRemove`. -/ + +structure SVx (ea eb : ObjRef → Prop) (σ : State Unit) : Prop where + rq : ∀ d p t, t ∈ σ.readyQueues d p → ActTcbA σ t ∧ t ∉ σ.releaseQueue + act : σ.schedulerAction = .resumeCurThread → σ.curThread ≠ σ.idleThread → ¬ea σ.curThread → + ActTcbA σ σ.curThread ∧ σ.curThread ∉ σ.releaseQueue + blk : ∀ t x sc, σ.kheap t = some (.tcb x) → BlockedTsA x.tcbState = true → x.tcbSchedContext = some sc → + ¬eb t → ScActiveAtA σ sc + +def FIX (ea eb : ObjRef → Prop) (σ : State Unit) : Prop := SVx ea eb σ ∧ ∀ t ∈ σ.releaseQueue, ActTcbA σ t + +theorem FIA.toX {σ : State Unit} (h : FIA σ) (ea eb : ObjRef → Prop) : FIX ea eb σ := + ⟨⟨h.1.rq, fun e1 e2 _ => h.1.act e1 e2, fun t x sc e1 e2 e3 _ => h.1.blk t x sc e1 e2 e3⟩, h.2⟩ + +theorem FIX.toA {σ : State Unit} (h : FIX (fun _ => False) (fun _ => False) σ) : FIA σ := + ⟨⟨h.1.rq, fun e1 e2 => h.1.act e1 e2 id, fun t x sc e1 e2 e3 => h.1.blk t x sc e1 e2 e3 id⟩, h.2⟩ + +/-- With the action not `resumeCurThread` (or the current thread idle), the act exemption is moot. -/ +theorem FIX.actVac {σ : State Unit} {ea : ObjRef → Prop} {eb : ObjRef → Prop} (h : FIX ea eb σ) + (hv : σ.schedulerAction ≠ .resumeCurThread) : FIX (fun _ => False) eb σ := + ⟨⟨h.1.rq, fun e1 => absurd e1 hv, h.1.blk⟩, h.2⟩ + +theorem FIX.updRq {σ : State Unit} {ea eb : ObjRef → Prop} (h : FIX ea eb σ) (f : Domain → Priority → List ObjRef) + (hmem : ∀ d p t, t ∈ f d p → t ∈ σ.readyQueues d p ∨ (ActTcbA σ t ∧ t ∉ σ.releaseQueue)) : + FIX ea eb { σ with readyQueues := f } := by + refine ⟨⟨fun d p t hm => ?_, h.1.act, h.1.blk⟩, h.2⟩ + rcases hmem d p t hm with m | ⟨⟨x, sc, e, es, a⟩, n⟩ + · exact h.1.rq d p t m + · exact ⟨⟨x, sc, e, es, a⟩, n⟩ + +theorem FIX.act' {σ : State Unit} {ea eb : ObjRef → Prop} (h : FIX ea eb σ) (a : SchedulerAction) + (hv : a ≠ .resumeCurThread) : FIX (fun _ => False) eb { σ with schedulerAction := a } := + ⟨⟨h.1.rq, fun e1 => absurd e1 hv, h.1.blk⟩, h.2⟩ + +namespace HT + +theorem setTcbQueueX {P : State Unit → Prop} {ea eb : ObjRef → Prop} (R : State Unit → Prop) + (hR : ∀ σ f, R σ → R { σ with readyQueues := f }) (d : Domain) (p : Priority) (q : ReadyQueue) + (h : ∀ σ, P σ → FIX ea eb σ ∧ R σ ∧ ∀ t ∈ q, t ∈ σ.readyQueues d p ∨ (ActTcbA σ t ∧ t ∉ σ.releaseQueue)) : + HT P (Fl4SpecA64.setTcbQueue (Z := Unit) d p q) (fun _ σ => FIX ea eb σ ∧ R σ) := by + unfold Fl4SpecA64.setTcbQueue + refine HT.modify _ fun σ hσ => ?_ + obtain ⟨hI, hr, hq⟩ := h σ hσ + refine ⟨hI.updRq _ fun d' p' t hm => ?_, hR σ _ hr⟩ + by_cases e : d' = d ∧ p' = p + · rw [if_pos e] at hm + rw [e.1, e.2]; exact hq t hm + · rw [if_neg e] at hm; exact Or.inl hm + +theorem enqX {ea eb : ObjRef → Prop} (t : ObjRef) (R : State Unit → Prop) + (hR : ∀ σ f, R σ → R { σ with readyQueues := f }) : + HT (fun σ => FIX ea eb σ ∧ R σ ∧ ActTcbA σ t ∧ t ∉ σ.releaseQueue) + (tcbSchedAction (Z := Unit) tcbSchedEnqueue t) (fun _ σ => FIX ea eb σ ∧ R σ) := by + unfold Fl4SpecA64.tcbSchedAction + refine HT.bind (Q := fun _ σ => FIX ea eb σ ∧ R σ ∧ ActTcbA σ t ∧ t ∉ σ.releaseQueue) + (HT.threadGet _ t fun σ hσ x hx => hσ) fun d => ?_ + refine HT.bind (Q := fun _ σ => FIX ea eb σ ∧ R σ ∧ ActTcbA σ t ∧ t ∉ σ.releaseQueue) + (HT.threadGet _ t fun σ hσ x hx => hσ) fun p => ?_ + unfold Fl4SpecA64.getTcbQueue + refine HT.bind (Q := fun q σ => (FIX ea eb σ ∧ R σ ∧ ActTcbA σ t ∧ t ∉ σ.releaseQueue) ∧ q = σ.readyQueues d p) + (HT.bind (Q := fun qs σ => (FIX ea eb σ ∧ R σ ∧ ActTcbA σ t ∧ t ∉ σ.releaseQueue) ∧ qs = σ.readyQueues) + (HT.gets _ fun σ hσ => ⟨hσ, rfl⟩) fun qs => + HT.pure _ fun σ hσ => ⟨hσ.1, by rw [hσ.2]⟩) fun queue => ?_ + refine HT.bind (Q := fun q' σ => (FIX ea eb σ ∧ R σ ∧ ActTcbA σ t ∧ t ∉ σ.releaseQueue) ∧ + queue = σ.readyQueues d p ∧ q' = tcbSchedEnqueue t queue) + (HT.pure _ fun σ hσ => ⟨hσ.1, hσ.2, rfl⟩) fun queue' => ?_ + refine HT.setTcbQueueX R hR d p queue' fun σ hσ => ?_ + obtain ⟨hP, hq, hq'⟩ := hσ + refine ⟨hP.1, hP.2.1, fun t' ht' => ?_⟩ + rw [hq'] at ht' + unfold tcbSchedEnqueue at ht' + by_cases hm : t ∉ queue + · rw [if_pos hm] at ht' + rcases List.mem_cons.1 ht' with e | m + · subst e; exact Or.inr hP.2.2 + · rw [hq] at m; exact Or.inl m + · rw [if_neg hm] at ht'; rw [hq] at ht'; exact Or.inl ht' + +end HT + + +/-! ## The detach block of `schedContextDonate` -/ + +def NoQ (t : ObjRef) (σ : State Unit) : Prop := ∀ d p, t ∉ σ.readyQueues d p + +theorem actTcb_ne {σ : State Unit} {p : ObjRef} {x x' : Tcb} {u : ObjRef} (hx : σ.kheap p = some (.tcb x)) + (hne : u ≠ p) (h : ActTcbA σ u) : + ActTcbA { σ with kheap := Function.update σ.kheap p (some (.tcb x')) } u := by + obtain ⟨z, sc, e, es, y, k, ey, ea⟩ := h + have hne' : sc ≠ p := fun h => by subst h; rw [hx] at ey; cases ey + exact ⟨z, sc, by simp [Function.update_of_ne hne, e], es, y, k, by simp [Function.update_of_ne hne', ey], ea⟩ + +/-- Dequeue: the thread leaves every ready queue (it sat only in its own one). -/ +theorem deqD (t : ObjRef) (R : State Unit → Prop) (hR : ∀ σ f, R σ → R { σ with readyQueues := f }) : + HT (fun σ => FIA σ ∧ RqDomPrio σ ∧ R σ) (tcbSchedAction (Z := Unit) tcbSchedDequeue t) + (fun _ σ => FIA σ ∧ R σ ∧ NoQ t σ) := by + unfold Fl4SpecA64.tcbSchedAction + refine HT.bind (Q := fun d σ => (FIA σ ∧ RqDomPrio σ ∧ R σ) ∧ ∃ x, σ.kheap t = some (.tcb x) ∧ d = x.tcbDomain) + (HT.threadGet _ t fun σ hσ x hx => ⟨hσ, x, hx, rfl⟩) fun d => ?_ + refine HT.bind (Q := fun p σ => (FIA σ ∧ RqDomPrio σ ∧ R σ) ∧ + ∃ x, σ.kheap t = some (.tcb x) ∧ d = x.tcbDomain ∧ p = x.tcbPriority) + (HT.threadGet _ t fun σ hσ x hx => ?_) fun p => ?_ + · obtain ⟨hP, x0, e0, hd⟩ := hσ + rw [hx] at e0 + have : x = x0 := KernelObject.tcb.inj (Option.some.inj e0) + subst this + exact ⟨hP, x, hx, hd, rfl⟩ + unfold Fl4SpecA64.getTcbQueue + refine HT.bind (Q := fun q σ => ((FIA σ ∧ RqDomPrio σ ∧ R σ) ∧ + ∃ x, σ.kheap t = some (.tcb x) ∧ d = x.tcbDomain ∧ p = x.tcbPriority) ∧ q = σ.readyQueues d p) + (HT.bind (Q := fun qs σ => ((FIA σ ∧ RqDomPrio σ ∧ R σ) ∧ + ∃ x, σ.kheap t = some (.tcb x) ∧ d = x.tcbDomain ∧ p = x.tcbPriority) ∧ qs = σ.readyQueues) + (HT.gets _ fun σ hσ => ⟨hσ, rfl⟩) fun qs => + HT.pure _ fun σ hσ => ⟨hσ.1, by rw [hσ.2]⟩) fun queue => ?_ + refine HT.bind (Q := fun q' σ => (((FIA σ ∧ RqDomPrio σ ∧ R σ) ∧ + ∃ x, σ.kheap t = some (.tcb x) ∧ d = x.tcbDomain ∧ p = x.tcbPriority) ∧ queue = σ.readyQueues d p) ∧ + q' = tcbSchedDequeue t queue) + (HT.pure _ fun σ hσ => ⟨hσ, rfl⟩) fun queue' => ?_ + unfold Fl4SpecA64.setTcbQueue + refine HT.modify _ fun σ hσ => ?_ + obtain ⟨⟨⟨⟨hI, hD, hRR⟩, x, ex, hd, hp⟩, hq⟩, hq'⟩ := hσ + have hmem : ∀ d' p' t', t' ∈ (fun d' p' => if d' = d ∧ p' = p then queue' else σ.readyQueues d' p') d' p' → + t' ∈ σ.readyQueues d' p' ∨ (ActTcbA σ t' ∧ t' ∉ σ.releaseQueue) := by + intro d' p' t' hm + dsimp only at hm + by_cases e : d' = d ∧ p' = p + · rw [if_pos e] at hm + rw [hq'] at hm + left; rw [e.1, e.2, ← hq]; exact (List.mem_filter.1 hm).1 + · rw [if_neg e] at hm; exact Or.inl hm + refine ⟨hI.updRq _ hmem, hR σ _ hRR, fun d' p' hm => ?_⟩ + dsimp only at hm + by_cases e : d' = d ∧ p' = p + · rw [if_pos e] at hm + rw [hq'] at hm + exact (by simpa using (List.mem_filter.1 hm).2 : False) + · rw [if_neg e] at hm + obtain ⟨a, b⟩ := hD t d' p' x ex hm + exact e ⟨a.trans hd.symm, b.trans hp.symm⟩ + +/-- Release-queue removal: the thread leaves the release queue. -/ +theorem relRemoveD (t : ObjRef) (R : State Unit → Prop) (hr : ∀ σ f, R σ → R { σ with releaseQueue := f }) + (hp : ∀ σ b, R σ → R { σ with reprogramTimer := b }) : + HT (fun σ => FIA σ ∧ R σ) (tcbReleaseRemove (Z := Unit) t) (fun _ σ => FIA σ ∧ R σ ∧ t ∉ σ.releaseQueue) := by + unfold Fl4SpecA64.tcbReleaseRemove + refine HT.bind (Q := fun qs σ => (FIA σ ∧ R σ) ∧ qs = σ.releaseQueue) (HT.gets _ fun σ hσ => ⟨hσ, rfl⟩) fun qs => ?_ + refine HT.bind (Q := fun _ σ => (FIA σ ∧ R σ) ∧ qs = σ.releaseQueue) + (HT.when (fun _ => HT.modify _ fun σ hσ => + ⟨⟨hσ.1.1.ofSame rfl rfl rfl rfl rfl rfl rfl, hp σ true hσ.1.2⟩, hσ.2⟩) (fun _ σ hσ => hσ)) fun _ => ?_ + refine HT.bind (Q := fun qs' σ => (FIA σ ∧ R σ) ∧ qs = σ.releaseQueue ∧ qs' = tcbSchedDequeue t qs) + (HT.pure _ fun σ hσ => ⟨hσ.1, hσ.2, rfl⟩) fun qs' => ?_ + refine HT.modify _ fun σ hσ => ?_ + obtain ⟨⟨hI, hR⟩, hq, hq'⟩ := hσ + refine ⟨hI.ofRelSub rfl rfl (fun t' ht' => ?_) rfl rfl rfl rfl, hr σ _ hR, fun hm => ?_⟩ + · have ht'' : t' ∈ qs' := ht' + rw [hq'] at ht'' + rw [← hq] + exact (List.mem_filter.1 ht'').1 + · have hm' : t ∈ qs' := hm + rw [hq'] at hm' + exact (by simpa using (List.mem_filter.1 hm').2 : False) + + +/-! ## `FIX`-states of the donate block -/ + +theorem FIX.ofScNone {σ : State Unit} {p : ObjRef} {x x' : Tcb} (h : FIA σ) (hx : σ.kheap p = some (.tcb x)) + (hq : NoQ p σ) (hr : p ∉ σ.releaseQueue) (h1 : x'.tcbSchedContext = none) : + FIX (fun u => u = p) (fun _ => False) { σ with kheap := Function.update σ.kheap p (some (.tcb x')) } := by + have scK : ∀ q y k, σ.kheap q = some (.schedContext y k) → + Function.update σ.kheap p (some (.tcb x')) q = some (.schedContext y k) := fun q y k e => by + by_cases hq : q = p + · subst hq; rw [hx] at e; cases e + · simpa [Function.update_of_ne hq] using e + have actT : ∀ u, u ≠ p → ActTcbA σ u → ActTcbA { σ with kheap := Function.update σ.kheap p (some (.tcb x')) } u := + fun u hu a => actTcb_ne hx hu a + refine ⟨⟨fun d p' u hm => ?_, fun e1 e2 hn => ?_, fun u z sc e1 e2 e3 _ => ?_⟩, fun u hu => ?_⟩ + · obtain ⟨a, n⟩ := h.1.rq d p' u hm + have hup : u ≠ p := fun e => hq d p' (e ▸ hm) + exact ⟨actT u hup a, n⟩ + · obtain ⟨a, n⟩ := h.1.act e1 e2 + exact ⟨actT _ hn a, n⟩ + · by_cases hq' : u = p + · subst hq' + have hz : x' = z := by simpa using e1 + subst hz + rw [h1] at e3; cases e3 + · obtain ⟨y, k, ey, ea⟩ := h.1.blk u z sc (by simpa [Function.update_of_ne hq'] using e1) e2 e3 + exact ⟨y, k, scK sc y k ey, ea⟩ + · have hup : u ≠ p := fun e => hr (e ▸ hu) + exact actT u hup (h.2 u hu) + +theorem FIX.toA' {σ : State Unit} {t : ObjRef} (h : FIX (fun u => u = t) (fun _ => False) σ) + (hne : σ.curThread ≠ t) : FIA σ := + ⟨⟨h.1.rq, fun e1 e2 => h.1.act e1 e2 hne, fun t x sc e1 e2 e3 => h.1.blk t x sc e1 e2 e3 id⟩, h.2⟩ + +theorem setScNoneD (t : ObjRef) (R : State Unit → Prop) + (hS : ∀ σ x x', σ.kheap t = some (.tcb x) → x'.tcbSchedContext = none → x'.tcbState = x.tcbState → R σ → + R { σ with kheap := Function.update σ.kheap t (some (.tcb x')) }) : + HT (fun σ => FIA σ ∧ R σ ∧ NoQ t σ ∧ t ∉ σ.releaseQueue) + (setTcbObjRef tcbSchedContextUpdate t none) + (fun _ σ => FIX (fun u => u = t) (fun _ => False) σ ∧ R σ) := by + show HT _ (threadSet (Z := Unit) (tcbSchedContextUpdate fun _ => none) t) _ + unfold Fl4SpecA64.threadSet + intro σ hσ r hr + obtain ⟨y1, hy1, h⟩ := mem_bindA.1 hr + change y1 ∈ (NonDet.assertOpt (getTcb (Z := Unit) t σ) σ).1 at hy1 + cases hg : getTcb (Z := Unit) t σ with + | none => rw [hg] at hy1; exact absurd hy1 (Set.notMem_empty _) + | some x => + rw [hg] at hy1 + have e1 : y1 = (x, σ) := hy1 + subst e1 + obtain ⟨ko, hko, hr2⟩ := setObject_memA h + rw [hr2] + have hx := getTcb_kheapA hg + exact ⟨FIX.ofScNone hσ.1 hx hσ.2.2.1 hσ.2.2.2 (x' := tcbSchedContextUpdate (fun _ => none) x) rfl, + hS σ x _ hx rfl rfl hσ.2.1⟩ + +theorem reschedX (ea eb : ObjRef → Prop) (R : State Unit → Prop) (hq : ∀ σ f, R σ → R { σ with readyQueues := f }) + (ha : ∀ σ a, R σ → R { σ with schedulerAction := a }) : + HT (fun σ => FIX ea eb σ ∧ R σ) (rescheduleRequired (Z := Unit)) + (fun _ σ => FIX (fun _ => False) eb σ ∧ R σ) := by + unfold Fl4SpecA64.rescheduleRequired + refine HT.bind (Q := fun a σ => (FIX ea eb σ ∧ R σ) ∧ a = σ.schedulerAction) (HT.gets _ fun σ hσ => ⟨hσ, rfl⟩) + fun action => ?_ + have fin : HT (fun σ => FIX ea eb σ ∧ R σ) (setSchedulerAction (Z := Unit) .chooseNewThread) + (fun _ σ => FIX (fun _ => False) eb σ ∧ R σ) := by + unfold Fl4SpecA64.setSchedulerAction + exact HT.modify _ fun σ hσ => ⟨hσ.1.act' _ (by intro e; cases e), ha σ _ hσ.2⟩ + cases action with + | switchThread t => + dsimp only + refine HT.bind (Q := fun b σ => (FIX ea eb σ ∧ R σ) ∧ b = schedulable t σ) + (HT.gets _ fun σ hσ => ⟨hσ.1, rfl⟩) fun sched => ?_ + refine HT.bind (Q := fun _ σ => FIX ea eb σ ∧ R σ) ?_ fun _ => fin + refine HT.when (fun hs => ?_) (fun _ σ hσ => hσ.1) + refine HT.mono (P := fun σ => (FIX ea eb σ ∧ R σ) ∧ schedulable t σ = true) ?_ + (fun σ hσ => ⟨hσ.1, by rw [← hσ.2]; exact hs⟩) (fun _ _ h => h) + refine HT.bind (Q := fun _ σ => (FIX ea eb σ ∧ R σ) ∧ schedulable t σ = true) + (HT.threadGet _ t fun σ hσ x hx => hσ) fun scOpt => ?_ + refine HT.bind (Q := fun _ σ => (FIX ea eb σ ∧ R σ) ∧ schedulable t σ = true) + (HT.assertOpt _ fun a _ σ hσ => hσ) fun scp => ?_ + refine HT.bind (Q := fun _ σ => (FIX ea eb σ ∧ R σ) ∧ schedulable t σ = true) + (HT.getsThe _ fun σ hσ a _ => hσ) fun suf => ?_ + refine HT.bind (Q := fun _ σ => (FIX ea eb σ ∧ R σ) ∧ schedulable t σ = true) + (HT.getsThe _ fun σ hσ a _ => hσ) fun rdy => ?_ + refine HT.bind (Q := fun _ σ => (FIX ea eb σ ∧ R σ) ∧ schedulable t σ = true) + (HT.assert fun _ σ hσ => hσ) fun _ => ?_ + refine HT.mono (HT.enqX (ea := ea) (eb := eb) t R hq) (fun σ hσ => ?_) (fun _ _ h => h) + obtain ⟨a, n, -⟩ := schedulable_actA hσ.2 + exact ⟨hσ.1.1, hσ.1.2, a, n⟩ + | resumeCurThread => + dsimp only + exact HT.mono fin (fun σ hσ => hσ.1) (fun _ _ h => h) + | chooseNewThread => + dsimp only + exact HT.mono fin (fun σ hσ => hσ.1) (fun _ _ h => h) + +theorem testRescheduleD (t : ObjRef) (R : State Unit → Prop) (hq : ∀ σ f, R σ → R { σ with readyQueues := f }) + (ha : ∀ σ a, R σ → R { σ with schedulerAction := a }) : + HT (fun σ => FIX (fun u => u = t) (fun _ => False) σ ∧ R σ) (testReschedule (Z := Unit) t) + (fun _ σ => FIA σ ∧ R σ) := by + unfold Fl4SpecA64.testReschedule + refine HT.bind (Q := fun cur σ => (FIX (fun u => u = t) (fun _ => False) σ ∧ R σ) ∧ cur = σ.curThread) + (HT.gets _ fun σ hσ => ⟨hσ, rfl⟩) fun cur => ?_ + refine HT.bind (Q := fun a σ => ((FIX (fun u => u = t) (fun _ => False) σ ∧ R σ) ∧ cur = σ.curThread) ∧ + a = σ.schedulerAction) (HT.gets _ fun σ hσ => ⟨hσ, rfl⟩) fun action => ?_ + refine HT.bind (Q := fun _ σ => (FIX (fun u => u = t) (fun _ => False) σ ∧ R σ) ∧ cur = σ.curThread) + (HT.pure _ fun σ hσ => hσ.1) fun flag => ?_ + refine HT.when (fun hc => ?_) (fun hn σ hσ => ?_) + · exact HT.mono (reschedX (fun u => u = t) (fun _ => False) R hq ha) (fun σ hσ => hσ.1) + (fun _ σ h => ⟨h.1.toA, h.2⟩) + · have hne : σ.curThread ≠ t := fun e => hn (Or.inl (by rw [hσ.2, e])) + exact ⟨hσ.1.1.toA' hne, hσ.1.2⟩ + +/-- **The detach block of `schedContextDonate`** keeps `FIA`: dequeue, release-remove, SC := none, `testReschedule`. -/ +theorem detach_fia (t : ObjRef) (R : State Unit → Prop) + (hK : ∀ σ σ', σ'.kheap = σ.kheap → σ'.idleThread = σ.idleThread → R σ → R σ') + (hS : ∀ σ x x', σ.kheap t = some (.tcb x) → x'.tcbSchedContext = none → x'.tcbState = x.tcbState → R σ → + R { σ with kheap := Function.update σ.kheap t (some (.tcb x')) }) : + HT (fun σ => FIA σ ∧ RqDomPrio σ ∧ R σ) + (do tcbSchedAction (Z := Unit) tcbSchedDequeue t + tcbReleaseRemove t + setTcbObjRef tcbSchedContextUpdate t none + testReschedule t) + (fun _ σ => FIA σ ∧ R σ) := by + refine HT.bind (Q := fun _ σ => FIA σ ∧ R σ ∧ NoQ t σ) (deqD t R fun σ f h => hK σ _ rfl rfl h) fun _ => ?_ + refine HT.bind (Q := fun _ σ => FIA σ ∧ (R σ ∧ NoQ t σ) ∧ t ∉ σ.releaseQueue) + (HT.mono (relRemoveD t (fun σ => R σ ∧ NoQ t σ) (fun σ f h => ⟨hK σ _ rfl rfl h.1, h.2⟩) + (fun σ b h => ⟨hK σ _ rfl rfl h.1, h.2⟩)) (fun σ h => ⟨h.1, h.2.1, h.2.2⟩) + (fun _ σ h => ⟨h.1, h.2.1, h.2.2⟩)) fun _ => ?_ + refine HT.bind (Q := fun _ σ => FIX (fun u => u = t) (fun _ => False) σ ∧ R σ) + (HT.mono (setScNoneD t R hS) (fun σ h => ⟨h.1, h.2.1.1, h.2.1.2, h.2.2⟩) (fun _ _ h => h)) fun _ => ?_ + exact testRescheduleD t R (fun σ f h => hK σ _ rfl rfl h) (fun σ a h => hK σ _ rfl rfl h) + + +/-! ## The rebind to the caller and `replyUnlinkTcb` from the `FIX` state -/ + +theorem FIX.ofTKeep {σ σ' : State Unit} {ea eb : ObjRef → Prop} (h : FIX ea eb σ) (k : TKeepA σ σ') : + FIX ea eb σ' := by + obtain ⟨kc, kr, kq, ki, ka, kb, kf, ks, kcs⟩ := k + have act : ∀ p, ScActiveAtA σ p → ScActiveAtA σ' p := fun p ⟨y, k, e, a⟩ => by + obtain ⟨y', e', a'⟩ := ks p y k e + exact ⟨y', k, e', a'.trans a⟩ + have actT : ∀ t, ActTcbA σ t → ActTcbA σ' t := fun t ⟨x, sc, e, es, a⟩ => by + obtain ⟨x', e', sc'⟩ := kf t x e + exact ⟨x', sc, e', sc'.trans es, act sc a⟩ + refine ⟨⟨fun d p t hm => ?_, fun e1 e2 hn => ?_, fun t x sc e1 e2 e3 hne => ?_⟩, fun t ht => ?_⟩ + · rw [kq] at hm + obtain ⟨a, n⟩ := h.1.rq d p t hm + exact ⟨actT t a, by rw [kr]; exact n⟩ + · rw [ka] at e1 + rw [kc, ki] at e2 + rw [kc] at hn + obtain ⟨a, n⟩ := h.1.act e1 e2 hn + rw [kc] + exact ⟨actT _ a, by rw [kr]; exact n⟩ + · obtain ⟨x0, e0, sc0, st0⟩ := kb t x e1 + exact act sc (h.1.blk t x0 sc e0 (by rw [← st0]; exact e2) (by rw [← sc0]; exact e3) hne) + · rw [kr] at ht + exact actT t (h.2 t ht) + +theorem HT.ofTPresX {α : Type} {m : SMonad Unit α} (h : TPres m) (ea eb : ObjRef → Prop) : + HT (fun σ => FIX ea eb σ) m (fun _ σ => FIX ea eb σ) := + fun σ hσ r hr => hσ.ofTKeep (h σ r hr) + +theorem HT.ofTC {α : Type} {m : SMonad Unit α} (h1 : TPres m) (h2 : CPres m) (R : State Unit → Prop) + (hR : ∀ σ σ', TKeepA σ σ' → R σ → R σ') : + HT (fun σ => FIA σ ∧ R σ ∧ RqDomPrio σ) m (fun _ σ => FIA σ ∧ R σ ∧ RqDomPrio σ) := + fun σ hσ r hr => ⟨hσ.1.ofTKeep (h1 σ r hr), hR _ _ (h1 σ r hr) hσ.2.1, hσ.2.2.ofCells (h2 σ r hr)⟩ + +/-- The thread's TCB (if any) has the SC `v`. -/ +def ScIs (c : ObjRef) (v : Option ObjRef) (σ : State Unit) : Prop := + ∀ x, σ.kheap c = some (.tcb x) → x.tcbSchedContext = v + +theorem ScIs.ofTKeep {c : ObjRef} {v : Option ObjRef} {σ σ' : State Unit} (h : ScIs c v σ) (k : TKeepA σ σ') : + ScIs c v σ' := by + intro x hx + obtain ⟨x0, e0, sc0, -⟩ := k.2.2.2.2.2.1 c x hx + exact sc0.trans (h x0 e0) + +theorem ne_of_act {σ : State Unit} {p u : ObjRef} {x : Tcb} (hx : σ.kheap p = some (.tcb x)) + (hno : x.tcbSchedContext = none) (h : ActTcbA σ u) : u ≠ p := by + obtain ⟨z, sc, e, es, -⟩ := h + intro hu; subst hu + rw [hx] at e + have : x = z := KernelObject.tcb.inj (Option.some.inj e) + subst this + rw [hno] at es; cases es + +theorem FIX.ofScSome {σ : State Unit} {p : ObjRef} {x x' : Tcb} {sc0 : ObjRef} (h : FIA σ) + (hx : σ.kheap p = some (.tcb x)) (hno : x.tcbSchedContext = none) (h1 : x'.tcbSchedContext = some sc0) : + FIX (fun _ => False) (fun u => u = p) { σ with kheap := Function.update σ.kheap p (some (.tcb x')) } := by + have scK : ∀ q y k, σ.kheap q = some (.schedContext y k) → + Function.update σ.kheap p (some (.tcb x')) q = some (.schedContext y k) := fun q y k e => by + by_cases hq : q = p + · subst hq; rw [hx] at e; cases e + · simpa [Function.update_of_ne hq] using e + refine ⟨⟨fun d p' u hm => ?_, fun e1 e2 _ => ?_, fun u z sc e1 e2 e3 hne => ?_⟩, fun u hu => ?_⟩ + · obtain ⟨a, n⟩ := h.1.rq d p' u hm + exact ⟨actTcb_ne hx (ne_of_act hx hno a) a, n⟩ + · obtain ⟨a, n⟩ := h.1.act e1 e2 + exact ⟨actTcb_ne hx (ne_of_act hx hno a) a, n⟩ + · obtain ⟨y, k, ey, ea⟩ := h.1.blk u z sc (by simpa [Function.update_of_ne hne] using e1) e2 e3 + exact ⟨y, k, scK sc y k ey, ea⟩ + · exact actTcb_ne hx (ne_of_act hx hno (h.2 u hu)) (h.2 u hu) + +theorem rebindD (c scPtr : ObjRef) : + HT (fun σ => FIA σ ∧ ScIs c none σ) (setTcbObjRef tcbSchedContextUpdate c (some scPtr)) + (fun _ σ => FIX (fun _ => False) (fun u => u = c) σ) := by + show HT _ (threadSet (Z := Unit) (tcbSchedContextUpdate fun _ => some scPtr) c) _ + unfold Fl4SpecA64.threadSet + intro σ hσ r hr + obtain ⟨y1, hy1, h⟩ := mem_bindA.1 hr + change y1 ∈ (NonDet.assertOpt (getTcb (Z := Unit) c σ) σ).1 at hy1 + cases hg : getTcb (Z := Unit) c σ with + | none => rw [hg] at hy1; exact absurd hy1 (Set.notMem_empty _) + | some x => + rw [hg] at hy1 + have e1 : y1 = (x, σ) := hy1 + subst e1 + obtain ⟨ko, hko, hr2⟩ := setObject_memA h + rw [hr2] + have hx := getTcb_kheapA hg + exact FIX.ofScSome hσ.1 hx (hσ.2 x hx) (x' := tcbSchedContextUpdate (fun _ => some scPtr) x) rfl + +theorem FIX.ofTcbUpd' {σ : State Unit} {p : ObjRef} {x x' : Tcb} + (h : FIX (fun _ => False) (fun u => u = p) σ) (hx : σ.kheap p = some (.tcb x)) + (h1 : x'.tcbSchedContext = x.tcbSchedContext) (h2 : BlockedTsA x'.tcbState = false) : + FIA { σ with kheap := Function.update σ.kheap p (some (.tcb x')) } := by + have scK : ∀ q y k, σ.kheap q = some (.schedContext y k) → + Function.update σ.kheap p (some (.tcb x')) q = some (.schedContext y k) := fun q y k e => by + by_cases hq : q = p + · subst hq; rw [hx] at e; cases e + · simpa [Function.update_of_ne hq] using e + have act : ∀ q, ScActiveAtA σ q → ScActiveAtA { σ with kheap := Function.update σ.kheap p (some (.tcb x')) } q := + fun q ⟨y, k, e, a⟩ => ⟨y, k, scK q y k e, a⟩ + have actT : ∀ t, ActTcbA σ t → ActTcbA { σ with kheap := Function.update σ.kheap p (some (.tcb x')) } t := + fun t a => by + obtain ⟨z, sc, e, es, a⟩ := a + by_cases hq : t = p + · subst hq + rw [hx] at e + have hz : x = z := KernelObject.tcb.inj (Option.some.inj e) + subst hz + exact ⟨x', sc, by simp, h1.trans es, act sc a⟩ + · exact ⟨z, sc, by simpa [Function.update_of_ne hq] using e, es, act sc a⟩ + refine ⟨⟨fun d p' t hm => ?_, fun e1 e2 => ?_, fun t z sc e1 e2 e3 => ?_⟩, fun t ht => ?_⟩ + · obtain ⟨a, n⟩ := h.1.rq d p' t hm + exact ⟨actT t a, n⟩ + · obtain ⟨a, n⟩ := h.1.act e1 e2 id + exact ⟨actT _ a, n⟩ + · by_cases hq : t = p + · subst hq + have hz : x' = z := by simpa using e1 + subst hz + rw [h2] at e2; cases e2 + · exact act sc (h.1.blk t z sc (by simpa [Function.update_of_ne hq] using e1) e2 e3 hq) + · exact actT t (h.2 t ht) + +theorem setThreadStateX (r : ObjRef) (ts : ThreadState) (hts : BlockedTsA ts = false) : + HT (fun σ => FIX (fun _ => False) (fun u => u = r) σ) (setThreadState (Z := Unit) r ts) (fun _ σ => FIA σ) := by + unfold Fl4SpecA64.setThreadState Fl4SpecA64.setThreadStateAct + refine HT.bind (Q := fun _ σ => FIA σ) ?_ fun _ => ?_ + · intro σ hσ r' hr + unfold Fl4SpecA64.threadSet at hr + obtain ⟨y1, hy1, h⟩ := mem_bindA.1 hr + change y1 ∈ (NonDet.assertOpt (getTcb (Z := Unit) r σ) σ).1 at hy1 + cases hg : getTcb (Z := Unit) r σ with + | none => rw [hg] at hy1; exact absurd hy1 (Set.notMem_empty _) + | some x => + rw [hg] at hy1 + have e1 : y1 = (x, σ) := hy1 + subst e1 + obtain ⟨ko, hko, hr2⟩ := setObject_memA h + rw [hr2] + exact FIX.ofTcbUpd' hσ (getTcb_kheapA hg) (x' := { x with tcbState := ts }) rfl hts + · refine HT.bind (Q := fun _ σ => FIA σ) (HT.gets _ fun σ hσ => hσ) fun cur => ?_ + refine HT.bind (Q := fun _ σ => FIA σ) (HT.gets _ fun σ hσ => hσ) fun act => ?_ + refine HT.bind (Q := fun _ σ => FIA σ) (HT.gets _ fun σ hσ => hσ) fun sched => ?_ + exact HT.when (fun _ => HT.mono (HT.setAct .chooseNewThread (by intro e; cases e) (fun _ => True) + (fun _ _ _ => trivial)) (fun σ h => ⟨h, trivial⟩) (fun _ _ h => h.1)) (fun _ σ hσ => hσ) + +/-- `replyUnlinkTcb` from the `FIX` state (the caller's blocked-SC clause waived) ends in `FIA`. -/ +theorem replyUnlinkTcbX (t r : ObjRef) : + HT (fun σ => FIX (fun _ => False) (fun u => u = t) σ) (replyUnlinkTcb (Z := Unit) t r) (fun _ σ => FIA σ) := by + unfold replyUnlinkTcb + have tp : ∀ {β : Type} {m : SMonad Unit β}, TPres m → + HT (fun σ => FIX (fun _ => False) (fun u => u = t) σ) m + (fun _ σ => FIX (fun _ => False) (fun u => u = t) σ) := fun h => HT.ofTPresX h _ _ + refine HT.bind (Q := fun _ σ => FIX (fun _ => False) (fun u => u = t) σ) (tp (by tpres)) fun _ => ?_ + refine HT.bind (Q := fun _ σ => FIX (fun _ => False) (fun u => u = t) σ) (tp (by tpres)) fun _ => ?_ + refine HT.bind (Q := fun _ σ => FIX (fun _ => False) (fun u => u = t) σ) (tp (by tpres)) fun _ => ?_ + refine HT.bind (Q := fun _ σ => FIX (fun _ => False) (fun u => u = t) σ) (tp (by tpres)) fun _ => ?_ + refine HT.bind (Q := fun _ σ => FIX (fun _ => False) (fun u => u = t) σ) (tp (by tpres)) fun _ => ?_ + exact setThreadStateX t .inactive rfl + + +/-! ## `schedContextDonate` and `replyRemove` -/ + +/-! ## The general donate (frame `R`, owner-less SC) -/ + +/-- A frame predicate for the transient SC moves: closed under `TKeepA`, heap-equal updates and state-keeping TCB +replacement. -/ +structure TFrame (R : State Unit → Prop) : Prop where + keep : ∀ σ σ', TKeepA σ σ' → R σ → R σ' + kh : ∀ σ σ', σ'.kheap = σ.kheap → σ'.idleThread = σ.idleThread → R σ → R σ' + repl : ∀ σ p x x', σ.kheap p = some (.tcb x) → x'.tcbState = x.tcbState → R σ → + R { σ with kheap := Function.update σ.kheap p (some (.tcb x')) } + +/-- The thread's TCB, if any, is in the state `ts`. -/ +def StIs (c : ObjRef) (ts : ThreadState) (σ : State Unit) : Prop := + ∀ x, σ.kheap c = some (.tcb x) → x.tcbState = ts + +theorem StIs.frame (c : ObjRef) (ts : ThreadState) : TFrame (StIs c ts) := + ⟨fun σ σ' k h x hx => by + obtain ⟨x0, e0, -, st0⟩ := k.2.2.2.2.2.1 c x hx + exact st0.trans (h x0 e0), + fun σ σ' hk _ h x hx => h x (by rw [hk] at hx; exact hx), + fun σ p x x' hx hst h y hy => by + by_cases e : c = p + · subst e + have : x' = y := by simpa using hy + subst this + exact hst.trans (h x hx) + · exact h y (by simpa [Function.update_of_ne e] using hy)⟩ + +def DonOK (scPtr : ObjRef) (σ : State Unit) : Prop := + RqDomPrio σ ∨ ∀ y k, σ.kheap scPtr = some (.schedContext y k) → y.scTcb = none + +theorem getScObjRef_mem {σ : State Unit} {f : SchedContext → Option ObjRef} {p : ObjRef} + {r : Option ObjRef × State Unit} (h : r ∈ (getScObjRef (Z := Unit) f p σ).1) : + r.2 = σ ∧ ∃ y k, σ.kheap p = some (.schedContext y k) ∧ r.1 = f y := by + unfold getScObjRef at h + obtain ⟨⟨sc, σ2⟩, hx, hr⟩ := mem_bindA.1 h + have hr' : r = (f sc, σ2) := hr + unfold getSchedContext at hx + obtain ⟨y1, hy1, hx2⟩ := mem_bindA.1 hx + have hy1' : y1 ∈ (NonDet.assertOpt (σ.kheap p) σ).1 := hy1 + cases hk : σ.kheap p with + | none => rw [hk] at hy1'; exact absurd hy1' (Set.notMem_empty _) + | some ko => + rw [hk] at hy1' + have e1 : y1 = (ko, σ) := hy1' + subst e1 + cases ko with + | schedContext sc' n => + have e3 : (sc, σ2) = (sc', σ) := hx2 + have e4 := Prod.mk.inj e3 + subst hr' + obtain ⟨e5, e6⟩ := e4 + subst e5 + exact ⟨e6, sc, n, rfl, rfl⟩ + | _ => exact absurd hx2 (Set.notMem_empty _) + +theorem rebindR (c scPtr : ObjRef) (R : State Unit → Prop) + (hS : ∀ σ x x', σ.kheap c = some (.tcb x) → x'.tcbState = x.tcbState → R σ → + R { σ with kheap := Function.update σ.kheap c (some (.tcb x')) }) : + HT (fun σ => FIA σ ∧ ScIs c none σ ∧ R σ) (setTcbObjRef tcbSchedContextUpdate c (some scPtr)) + (fun _ σ => FIX (fun _ => False) (fun u => u = c) σ ∧ R σ) := by + show HT _ (threadSet (Z := Unit) (tcbSchedContextUpdate fun _ => some scPtr) c) _ + unfold Fl4SpecA64.threadSet + intro σ hσ r hr + obtain ⟨y1, hy1, h⟩ := mem_bindA.1 hr + change y1 ∈ (NonDet.assertOpt (getTcb (Z := Unit) c σ) σ).1 at hy1 + cases hg : getTcb (Z := Unit) c σ with + | none => rw [hg] at hy1; exact absurd hy1 (Set.notMem_empty _) + | some x => + rw [hg] at hy1 + have e1 : y1 = (x, σ) := hy1 + subst e1 + obtain ⟨ko, hko, hr2⟩ := setObject_memA h + rw [hr2] + have hx := getTcb_kheapA hg + exact ⟨FIX.ofScSome hσ.1 hx (hσ.2.1 x hx) (x' := tcbSchedContextUpdate (fun _ => some scPtr) x) rfl, + hS σ x _ hx rfl hσ.2.2⟩ + +/-- **The general donate**: frame `R`, and the SC's old owner either absent or covered by `RqDomPrio`. -/ +theorem donateR_fia (scPtr caller : ObjRef) (R : State Unit → Prop) (F : TFrame R) : + HT (fun σ => FIA σ ∧ ScIs caller none σ ∧ DonOK scPtr σ ∧ R σ) (schedContextDonate (Z := Unit) scPtr caller) + (fun _ σ => FIX (fun _ => False) (fun u => u = caller) σ ∧ R σ) := by + unfold schedContextDonate + have keep : ∀ σ σ', TKeepA σ σ' → ScIs caller none σ → ScIs caller none σ' := fun _ _ k h => h.ofTKeep k + refine HT.bind (Q := fun fromOpt σ => FIA σ ∧ ScIs caller none σ ∧ R σ ∧ (RqDomPrio σ ∨ fromOpt = none)) ?_ + fun fromOpt => ?_ + · intro σ hσ r hr + obtain ⟨hs, y, k, hk, hv⟩ := getScObjRef_mem hr + obtain ⟨a, s'⟩ := r + simp only at hs hv ⊢ + subst hs + refine ⟨hσ.1, hσ.2.1, hσ.2.2.2, ?_⟩ + rcases hσ.2.2.1 with h | h + · exact Or.inl h + · exact Or.inr (by rw [hv]; exact h y k hk) + refine HT.bind (Q := fun _ σ => FIA σ ∧ ScIs caller none σ ∧ R σ) ?_ fun _ => ?_ + · refine HT.when (fun hne => ?_) (fun _ σ h => ⟨h.1, h.2.1, h.2.2.1⟩) + refine HT.bind (Q := fun fromT σ => FIA σ ∧ RqDomPrio σ ∧ ScIs caller none σ ∧ R σ) + (HT.assertOpt _ fun a ha σ hσ => ⟨hσ.1, ?_, hσ.2.1, hσ.2.2.1⟩) fun fromT => ?_ + · rcases hσ.2.2.2 with h | h + · exact h + · exact absurd h hne + · exact HT.mono (detach_fia fromT (fun σ => ScIs caller none σ ∧ R σ) + (fun σ σ' hk hid h => ⟨fun x hx => h.1 x (by rw [hk] at hx; exact hx), F.kh σ σ' hk hid h.2⟩) + (fun σ x x' hx h1 hst h => ⟨fun y hy => by + by_cases e : caller = fromT + · subst e + have : x' = y := by simpa using hy + subst this; exact h1 + · exact h.1 y (by simpa [Function.update_of_ne e] using hy), F.repl σ fromT x x' hx hst h.2⟩)) + (fun σ h => ⟨h.1, h.2.1, h.2.2.1, h.2.2.2⟩) (fun _ σ h => ⟨h.1, h.2.1, h.2.2⟩) + · refine HT.bind (Q := fun _ σ => FIA σ ∧ ScIs caller none σ ∧ R σ) + (HT.mono (HT.ofTPres (R := fun σ => ScIs caller none σ ∧ R σ) (by tpres) + (fun σ σ' k h => ⟨h.1.ofTKeep k, F.keep σ σ' k h.2⟩)) (fun σ h => ⟨h.1, h.2.1, h.2.2⟩) + (fun _ σ h => ⟨h.1, h.2.1, h.2.2⟩)) fun _ => ?_ + exact rebindR caller scPtr R (fun σ x x' hx hst h => F.repl σ caller x x' hx hst h) + + +theorem donate_fia (scPtr caller : ObjRef) : + HT (fun σ => FIA σ ∧ ScIs caller none σ ∧ RqDomPrio σ) (schedContextDonate (Z := Unit) scPtr caller) + (fun _ σ => FIX (fun _ => False) (fun u => u = caller) σ) := by + unfold schedContextDonate + have keep : ∀ σ σ', TKeepA σ σ' → ScIs caller none σ → ScIs caller none σ' := fun _ _ k h => h.ofTKeep k + refine HT.bind (Q := fun _ σ => FIA σ ∧ ScIs caller none σ ∧ RqDomPrio σ) + (HT.ofTC (by unfold getScObjRef; tpres) (by unfold getScObjRef; cpres) _ keep) fun fromOpt => ?_ + refine HT.bind (Q := fun _ σ => FIA σ ∧ ScIs caller none σ) ?_ fun _ => ?_ + · refine HT.when (fun _ => ?_) (fun _ σ h => ⟨h.1, h.2.1⟩) + refine HT.bind (Q := fun fromT σ => FIA σ ∧ ScIs caller none σ ∧ RqDomPrio σ) + (HT.assertOpt _ fun a _ σ hσ => hσ) fun fromT => ?_ + exact HT.mono (detach_fia fromT (ScIs caller none) + (fun σ σ' hk _ h x hx => h x (by rw [hk] at hx; exact hx)) + (fun σ x x' hx h1 _ h y hy => by + by_cases e : caller = fromT + · subst e + have : x' = y := by simpa using hy + subst this; exact h1 + · exact h y (by simpa [Function.update_of_ne e] using hy))) + (fun σ h => ⟨h.1, h.2.2, h.2.1⟩) (fun _ σ h => ⟨h.1, h.2⟩) + · refine HT.bind (Q := fun _ σ => FIA σ ∧ ScIs caller none σ) + (HT.mono (HT.ofTPres (by tpres) (ScIs caller none) keep) (fun σ h => h) (fun _ _ h => h)) fun _ => ?_ + exact rebindD caller scPtr + +theorem replyRemove_fia (caller r : ObjRef) : + HT (fun σ => FIA σ ∧ RqDomPrio σ) (replyRemove (Z := Unit) caller r) (fun _ σ => FIA σ) := by + unfold replyRemove + have R0 : ∀ σ σ', TKeepA σ σ' → True → True := fun _ _ _ h => h + have T0 : ∀ {β : Type} {m : SMonad Unit β}, TPres m → CPres m → + HT (fun σ => FIA σ ∧ True ∧ RqDomPrio σ) m (fun _ σ => FIA σ ∧ True ∧ RqDomPrio σ) := + fun h1 h2 => HT.ofTC h1 h2 (fun _ => True) R0 + refine HT.bind (Q := fun _ σ => FIA σ ∧ True ∧ RqDomPrio σ) + (HT.mono (T0 (by tpres) (by cpres)) (fun σ h => ⟨h.1, trivial, h.2⟩) (fun _ _ h => h)) fun reply => ?_ + refine HT.bind (Q := fun _ σ => FIA σ ∧ True ∧ RqDomPrio σ) (T0 (by tpres) (by cpres)) fun _ => ?_ + refine HT.bind (Q := fun _ σ => FIA σ ∧ True ∧ RqDomPrio σ) (T0 (by tpres) (by cpres)) fun rScOpt => ?_ + cases rScOpt with + | none => + dsimp only + exact HT.mono (replyUnlinkTcbX caller r) (fun σ h => h.1.toX _ _) (fun _ _ h => h) + | some scPtr => + dsimp only + refine HT.bind (Q := fun _ σ => FIA σ ∧ True ∧ RqDomPrio σ) (T0 (by tpres) (by cpres)) fun replies => ?_ + refine HT.bind (Q := fun callerSc σ => FIA σ ∧ ScIs caller callerSc σ ∧ RqDomPrio σ) ?_ fun callerSc => ?_ + · unfold getTcbObjRef + exact HT.threadGet _ caller fun σ hσ x hx => ⟨hσ.1, fun y hy => by + rw [hx] at hy + have : x = y := KernelObject.tcb.inj (Option.some.inj hy) + subst this; rfl, hσ.2.2⟩ + refine HT.bind (Q := fun _ σ => FIA σ ∧ ScIs caller callerSc σ ∧ RqDomPrio σ) + (HT.ofTC (replyUnlinkSc_tpres scPtr r) (replyUnlinkSc_cpres scPtr r) (ScIs caller callerSc) + (fun _ _ k h => h.ofTKeep k)) fun _ => ?_ + refine HT.bind (Q := fun _ σ => FIX (fun _ => False) (fun u => u = caller) σ) ?_ + fun _ => replyUnlinkTcbX caller r + refine HT.when (fun hc => ?_) (fun _ σ h => h.1.toX _ _) + exact HT.mono (donate_fia scPtr caller) (fun σ h => ⟨h.1, by rw [hc.2] at h; exact h.2.1, h.2.2⟩) + (fun _ _ h => h) + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3DelSchedValidA.lean b/proofs/Fl4ProofsA64/P3/Stage3DelSchedValidA.lean new file mode 100644 index 000000000..6921e2494 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3DelSchedValidA.lean @@ -0,0 +1,507 @@ +/- +P3 (aarch64, A64-P3 Stage 3, T3 G4b / F8, delete hooks): **F8 across the leaves of the cancel hooks** +(`cancel_all_ipc`, `cancel_all_signals`, `cancel_badged_sends`, `reply_remove`), shared pieces. + +On `FIA` (`P3/Stage3SchedValidStepA`: F8 on the current thread's own SC, with the temporary F8(d)): + +* `BoundActive t σ`: if `t`'s TCB has a bound SC, the SC is active (`ActTcbA`). For a woken waiter it is F8(c)'s + `blk` captured before the state write. +* `possibleSwitchTo_fia'`: `possibleSwitchTo` keeps `FIA` from `BoundActive` of its target (a target without SC does + nothing: the guard). +* `FIA.setNS`: a heap write that replaces a non-TCB, non-SC object by one keeps `FIA` (endpoint, notification, reply). +* `setThreadState_fia`: `setThreadState r ts` for a non-blocked `ts` keeps `FIA` and any predicate `R` preserved by + a TCB replacement with the same SC and by action writes (`BoundActive t0`, ...). + +No premise; new module. +-/ +import Fl4ProofsA64.P3.Stage3SchedActivateA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P3 Classical + +noncomputable section + +/-- A bound SC of `t`'s TCB is active. -/ +def BoundActive (t : ObjRef) (σ : State Unit) : Prop := + ∀ x sc, σ.kheap t = some (.tcb x) → x.tcbSchedContext = some sc → ActTcbA σ t + +theorem BoundActive.ofAct {t : ObjRef} {σ : State Unit} (h : ActTcbA σ t) : BoundActive t σ := fun _ _ _ _ => h + +/-- `R` together with the target's `TgtA`. -/ +abbrev RT (R : State Unit → Prop) (target : ObjRef) (σ : State Unit) : Prop := R σ ∧ TgtA target σ + +/-- `possibleSwitchTo` keeps `FIA` and a frame `R` (preserved by queue and action writes) when its target's bound SC +(if any) is active. -/ +theorem possibleSwitchTo_fia' (target : ObjRef) (R : State Unit → Prop) + (hq : ∀ σ f, R σ → R { σ with readyQueues := f }) (ha : ∀ σ a, R σ → R { σ with schedulerAction := a }) : + HT (fun σ => FIA σ ∧ BoundActive target σ ∧ R σ) (possibleSwitchTo (Z := Unit) target) + (fun _ σ => FIA σ ∧ R σ) := by + unfold Fl4SpecA64.possibleSwitchTo Fl4SpecA64.getTcbObjRef + refine HT.bind (Q := fun scOpt σ => FIA σ ∧ R σ ∧ (scOpt ≠ none → ActTcbA σ target)) + (HT.threadGet _ target fun σ hσ x hx => ⟨hσ.1, hσ.2.2, fun hne => ?_⟩) fun scOpt => ?_ + · cases hs : x.tcbSchedContext with + | none => exact absurd hs hne + | some sc => exact hσ.2.1 x sc hx hs + refine HT.bind (Q := fun inq σ => (FIA σ ∧ R σ ∧ (scOpt ≠ none → ActTcbA σ target)) ∧ inq = inReleaseQueue target σ) + (HT.gets _ fun σ hσ => ⟨hσ, rfl⟩) fun inq => ?_ + refine HT.when (fun hc => ?_) (fun _ σ hσ => ⟨hσ.1.1, hσ.1.2.1⟩) + refine HT.mono (P := fun σ => FIA σ ∧ RT R target σ) ?_ + (fun σ hσ => ⟨hσ.1.1, hσ.1.2.1, hσ.1.2.2 hc.1, + fun m => hc.2 (by rw [hσ.2]; simp [inReleaseQueue, m])⟩) (fun _ _ h => h) + have hq' : ∀ σ f, RT R target σ → RT R target { σ with readyQueues := f } := fun σ f h => ⟨hq σ f h.1, h.2⟩ + have ha' : ∀ σ a, RT R target σ → RT R target { σ with schedulerAction := a } := fun σ a h => ⟨ha σ a h.1, h.2⟩ + refine HT.bind (Q := fun _ σ => FIA σ ∧ RT R target σ) (HT.gets _ fun σ hσ => hσ) fun curDom => ?_ + refine HT.bind (Q := fun _ σ => FIA σ ∧ RT R target σ) (HT.threadGet _ target fun σ hσ x hx => hσ) fun targetDom => ?_ + refine HT.bind (Q := fun _ σ => FIA σ ∧ RT R target σ) (HT.gets _ fun σ hσ => hσ) fun action => ?_ + have enqT : HT (fun σ => FIA σ ∧ RT R target σ) (tcbSchedAction (Z := Unit) tcbSchedEnqueue target) + (fun _ σ => FIA σ ∧ RT R target σ) := + HT.mono (HT.enq target (RT R target) hq') (fun σ hσ => ⟨hσ.1, hσ.2, hσ.2.2.1, hσ.2.2.2⟩) (fun _ _ h => h) + refine HT.mono (Q := fun _ σ => FIA σ ∧ RT R target σ) ?_ (fun σ hσ => hσ) (fun _ σ h => ⟨h.1, h.2.1⟩) + refine HT.ite (fun _ => enqT) (fun _ => ?_) + refine HT.ite (fun _ => ?_) (fun _ => ?_) + · exact HT.bind (Q := fun _ σ => FIA σ ∧ RT R target σ) (resched_fia (RT R target) hq' ha') fun _ => enqT + · exact HT.setAct (.switchThread target) (by intro e; cases e) (RT R target) ha' + +/-- `BoundActive` of a family, and its preservation along `TKeepA`. -/ +def BAs (S : List ObjRef) (σ : State Unit) : Prop := ∀ u ∈ S, BoundActive u σ + +theorem BAs.ofTKeep {S : List ObjRef} {σ σ' : State Unit} (h : BAs S σ) (k : TKeepA σ σ') : BAs S σ' := by + intro u hu x sc e hs + obtain ⟨kc, kr, kq, ki, ka, kb, kf, ks, kcs⟩ := k + obtain ⟨x0, e0, sc0, _⟩ := kb u x e + exact (h u hu x0 sc e0 (by rw [← sc0]; exact hs)).ofTKeep ⟨kc, kr, kq, ki, ka, kb, kf, ks, kcs⟩ + +theorem BAs.queue {S : List ObjRef} {σ : State Unit} (h : BAs S σ) (f) : BAs S { σ with readyQueues := f } := h +theorem BAs.act {S : List ObjRef} {σ : State Unit} (h : BAs S σ) (a) : BAs S { σ with schedulerAction := a } := h +theorem BAs.tail {a : ObjRef} {S : List ObjRef} {σ : State Unit} (h : BAs (a :: S) σ) : BAs S σ := + fun u hu => h u (List.mem_cons_of_mem _ hu) + +/-- Induction for `mapMx` with a list-indexed invariant. -/ +theorem HT.mapMx {γ : Type} {f : γ → SMonad Unit Unit} (I : List γ → State Unit → Prop) + (hs : ∀ a l, HT (I (a :: l)) (f a) (fun _ => I l)) : + ∀ l, HT (I l) (NonDet.mapMx f l) (fun _ => I []) + | [] => fun σ h r hr => by + have : r = ((), σ) := hr + subst this; exact h + | a :: l => by + have : NonDet.mapMx f (a :: l) = (f a >>= fun _ => NonDet.mapMx f l) := rfl + rw [this] + exact HT.bind (hs a l) fun _ => HT.mapMx I hs l + +/-- A heap object that is neither a TCB nor an SC. -/ +def NSKo (ko : KernelObject) : Prop := (∀ x, ko ≠ .tcb x) ∧ ∀ y n, ko ≠ .schedContext y n + +theorem setNS_tkeep {σ : State Unit} {p : ObjRef} {ko o : KernelObject} (hp : σ.kheap p = some ko) + (hko : NSKo ko) (ho : NSKo o) : + TKeepA σ { σ with kheap := Function.update σ.kheap p (some o) } := by + refine ⟨rfl, rfl, rfl, rfl, rfl, fun q y' hq => ?_, fun q y hq => ?_, ⟨fun q y k hq => ?_, rfl⟩⟩ + · by_cases e : q = p + · subst e + have hq' : o = .tcb y' := by simpa using hq + exact absurd hq' (ho.1 y') + · exact ⟨y', by simpa [Function.update_of_ne e] using hq, rfl, rfl⟩ + · by_cases e : q = p + · subst e; rw [hp] at hq + have hq' : ko = .tcb y := Option.some.inj hq + exact absurd hq' (hko.1 y) + · exact ⟨y, by simpa [Function.update_of_ne e] using hq, rfl⟩ + · by_cases e : q = p + · subst e; rw [hp] at hq + have hq' : ko = .schedContext y k := Option.some.inj hq + exact absurd hq' (hko.2 y k) + · exact ⟨y, by simpa [Function.update_of_ne e] using hq, rfl⟩ + +theorem FIA.setNS {σ : State Unit} {p : ObjRef} {ko o : KernelObject} (h : FIA σ) (hp : σ.kheap p = some ko) + (hko : NSKo ko) (ho : NSKo o) : FIA { σ with kheap := Function.update σ.kheap p (some o) } := + h.ofTKeep (setNS_tkeep hp hko ho) + +/-- **`setThreadState r ts` (a non-blocked `ts`) keeps `FIA` and any frame predicate `R`** preserved by a TCB +replacement with the same SC and by action writes. -/ +theorem setThreadState_fia (r : ObjRef) (ts : ThreadState) (hts : BlockedTsA ts = false) (R : State Unit → Prop) + (hT : ∀ σ p x x', σ.kheap p = some (.tcb x) → x'.tcbSchedContext = x.tcbSchedContext → R σ → + R { σ with kheap := Function.update σ.kheap p (some (.tcb x')) }) + (hA : ∀ σ a, R σ → R { σ with schedulerAction := a }) : + HT (fun σ => FIA σ ∧ R σ) (setThreadState (Z := Unit) r ts) (fun _ σ => FIA σ ∧ R σ) := by + unfold Fl4SpecA64.setThreadState Fl4SpecA64.setThreadStateAct + refine HT.bind (Q := fun _ σ => FIA σ ∧ R σ) ?_ fun _ => ?_ + · intro σ hσ r' hr + unfold Fl4SpecA64.threadSet at hr + obtain ⟨y1, hy1, h⟩ := mem_bindA.1 hr + change y1 ∈ (NonDet.assertOpt (getTcb (Z := Unit) r σ) σ).1 at hy1 + cases hg : getTcb (Z := Unit) r σ with + | none => rw [hg] at hy1; exact absurd hy1 (Set.notMem_empty _) + | some x => + rw [hg] at hy1 + have e1 : y1 = (x, σ) := hy1 + subst e1 + obtain ⟨ko, hko, hr2⟩ := setObject_memA h + rw [hr2] + have hx := getTcb_kheapA hg + exact ⟨hσ.1.ofTcbUpd hx (x' := { x with tcbState := ts }) rfl (fun hb => by rw [hts] at hb; cases hb), + hT σ r x { x with tcbState := ts } hx rfl hσ.2⟩ + · refine HT.bind (Q := fun _ σ => FIA σ ∧ R σ) (HT.gets _ fun σ hσ => hσ) fun cur => ?_ + refine HT.bind (Q := fun _ σ => FIA σ ∧ R σ) (HT.gets _ fun σ hσ => hσ) fun act => ?_ + refine HT.bind (Q := fun _ σ => FIA σ ∧ R σ) (HT.gets _ fun σ hσ => hσ) fun sched => ?_ + exact HT.when (fun _ => HT.setAct .chooseNewThread (by intro e; cases e) R hA) (fun _ σ hσ => hσ) + + +/-! ## Endpoint/notification/reply writes and reads keep `TKeepA` -/ + +theorem nsko_ntfn (e : Notification) : NSKo (.notification e) := by + unfold NSKo; exact ⟨fun x h => (by cases h), fun y n h => (by cases h)⟩ +theorem nsko_ep (e : Endpoint) : NSKo (.endpoint e) := by + unfold NSKo; exact ⟨fun x h => (by cases h), fun y n h => (by cases h)⟩ +theorem nsko_reply (e : Reply) : NSKo (.reply e) := by + unfold NSKo; exact ⟨fun x h => (by cases h), fun y n h => (by cases h)⟩ + +theorem setSimpleKo_tpres {α : Type} [Nonempty α] (f : α → KernelObject) (hf : ∀ e, NSKo (f e)) (p : ObjRef) + (e : α) : TPres (setSimpleKo (Z := Unit) f p e) := by + intro σ r hr + unfold setSimpleKo at hr + obtain ⟨y1, hy1, h1⟩ := mem_bindA.1 hr + have hy1' : y1 ∈ (NonDet.assertOpt (σ.kheap p) σ).1 := hy1 + cases hk : σ.kheap p with + | none => rw [hk] at hy1'; exact absurd hy1' (Set.notMem_empty _) + | some ko => + rw [hk] at hy1' + have e1 : y1 = (ko, σ) := hy1' + subst e1 + obtain ⟨y2, hy2, h2⟩ := mem_bindA.1 h1 + obtain ⟨hP2, rfl⟩ := hy2 + obtain ⟨y3, hy3, h3⟩ := mem_bindA.1 h2 + obtain ⟨hP3, rfl⟩ := hy3 + obtain ⟨ko', hko, hr2⟩ := setObject_memA h3 + have hns : NSKo ko := by + unfold partialInv at hP3 + by_cases hu : ∃! y, f y = ko + · obtain ⟨y, hy, _⟩ := hu + rw [← hy]; exact hf y + · rw [if_neg hu] at hP3; exact absurd rfl hP3 + rw [hr2] + exact setNS_tkeep hk hns (hf e) + +theorem getSimpleKo_tpres {α : Type} [Nonempty α] (f : α → KernelObject) (p : ObjRef) : + TPres (getSimpleKo (Z := Unit) f p) := by + unfold getSimpleKo + refine PresR.bind TPresInst.pres (fun kobj => ?_) + refine PresR.bind TPresInst.pres (fun _ => ?_) + cases partialInv f kobj with + | some e => exact PresR.pure e + | none => exact PresR.fail + +instance (p : ObjRef) : TPresInst (getNotification (Z := Unit) p) := ⟨getSimpleKo_tpres _ p⟩ +instance (p : ObjRef) : TPresInst (getEndpoint (Z := Unit) p) := ⟨getSimpleKo_tpres _ p⟩ +instance (p : ObjRef) : TPresInst (getReply (Z := Unit) p) := ⟨getSimpleKo_tpres _ p⟩ +instance (p : ObjRef) (e : Notification) : TPresInst (setNotification (Z := Unit) p e) := + ⟨setSimpleKo_tpres _ nsko_ntfn p e⟩ +instance (p : ObjRef) (e : Endpoint) : TPresInst (setEndpoint (Z := Unit) p e) := + ⟨setSimpleKo_tpres _ nsko_ep p e⟩ +instance (p : ObjRef) (e : Reply) : TPresInst (setReply (Z := Unit) p e) := + ⟨setSimpleKo_tpres _ nsko_reply p e⟩ + +theorem HT.ofTPres {α : Type} {m : SMonad Unit α} (h : TPres m) (R : State Unit → Prop) + (hR : ∀ σ σ', TKeepA σ σ' → R σ → R σ') : + HT (fun σ => FIA σ ∧ R σ) m (fun _ σ => FIA σ ∧ R σ) := + fun σ hσ r hr => ⟨hσ.1.ofTKeep (h σ r hr), hR _ _ (h σ r hr) hσ.2⟩ + +theorem tcbNtfnDequeue_fia (t n : ObjRef) (R : State Unit → Prop) (hR : ∀ σ σ', TKeepA σ σ' → R σ → R σ') : + HT (fun σ => FIA σ ∧ R σ) (tcbNtfnDequeue (Z := Unit) t n) (fun _ σ => FIA σ ∧ R σ) := + HT.ofTPres (by unfold tcbNtfnDequeue; tpres) R hR + +theorem tcbEpDequeue_fia (t n : ObjRef) (R : State Unit → Prop) (hR : ∀ σ σ', TKeepA σ σ' → R σ → R σ') : + HT (fun σ => FIA σ ∧ R σ) (tcbEpDequeue (Z := Unit) t n) (fun _ σ => FIA σ ∧ R σ) := + HT.ofTPres (by unfold tcbEpDequeue; tpres) R hR + +instance (p : ObjRef) : TPresInst (refillPopHead (Z := Unit) p) := ⟨by unfold refillPopHead; tpres⟩ +instance (p : ObjRef) (f : Refill → Refill) : TPresInst (updateRefillHd (Z := Unit) p f) := + ⟨by unfold updateRefillHd; tpres⟩ +instance (p : ObjRef) : TPresInst (mergeOverlappingRefills (Z := Unit) p) := + ⟨by unfold mergeOverlappingRefills; tpres⟩ +instance (p : ObjRef) : TPresInst (refillHeadOverlappingLoop (Z := Unit) p) := + ⟨by unfold refillHeadOverlappingLoop; tpres⟩ +instance (p : ObjRef) : TPresInst (refillUnblockCheck (Z := Unit) p) := + ⟨by unfold refillUnblockCheck; tpres⟩ + +theorem refillCheck_fia (scOpt : Option ObjRef) (R : State Unit → Prop) (hR : ∀ σ σ', TKeepA σ σ' → R σ → R σ') : + HT (fun σ => FIA σ ∧ R σ) (ifSporadicCurScAssertRefillUnblockCheck (Z := Unit) scOpt) + (fun _ σ => FIA σ ∧ R σ) := + HT.ofTPres (by unfold ifSporadicCurScAssertRefillUnblockCheck ifCondRefillUnblockCheck; tpres) R hR + +/-! ## `BAs` across a TCB replacement with the same SC -/ + +theorem actTcb_upd {σ : State Unit} {p : ObjRef} {x x' : Tcb} (hx : σ.kheap p = some (.tcb x)) + (h1 : x'.tcbSchedContext = x.tcbSchedContext) {u : ObjRef} (h : ActTcbA σ u) : + ActTcbA { σ with kheap := Function.update σ.kheap p (some (.tcb x')) } u := by + obtain ⟨z, sc, e, es, y, k, ey, ea⟩ := h + have hne : sc ≠ p := fun h => by subst h; rw [hx] at ey; cases ey + by_cases hu : u = p + · subst hu + rw [hx] at e + have hz := Option.some.inj e + injection hz with hz + subst hz + exact ⟨x', sc, by simp, h1.trans es, y, k, by simp [Function.update_of_ne hne, ey], ea⟩ + · exact ⟨z, sc, by simp [Function.update_of_ne hu, e], es, y, k, by simp [Function.update_of_ne hne, ey], ea⟩ + +theorem BAs.tcbUpd {S : List ObjRef} {σ : State Unit} {p : ObjRef} {x x' : Tcb} (h : BAs S σ) + (hx : σ.kheap p = some (.tcb x)) (h1 : x'.tcbSchedContext = x.tcbSchedContext) : + BAs S { σ with kheap := Function.update σ.kheap p (some (.tcb x')) } := by + intro u hu z sc e hs + by_cases hup : u = p + · subst hup + have hz : x' = z := by simpa using e + subst hz + exact actTcb_upd hx h1 (h u hu x sc hx (by rw [← h1]; exact hs)) + · have e' : σ.kheap u = some (.tcb z) := by simpa [Function.update_of_ne hup] using e + exact actTcb_upd hx h1 (h u hu z sc e' hs) + +/-- `removeAndRestartNtfnQueuedThread`'s per-thread step keeps `FIA` and the bound-SC activity of the rest. -/ +theorem removeNtfn_fia (t n : ObjRef) (S : List ObjRef) : + HT (fun σ => FIA σ ∧ BAs (t :: S) σ) (removeAndRestartNtfnQueuedThread (Z := Unit) t n) + (fun _ σ => FIA σ ∧ BAs S σ) := by + unfold removeAndRestartNtfnQueuedThread + have hR : ∀ σ σ', TKeepA σ σ' → BAs (t :: S) σ → BAs (t :: S) σ' := fun _ _ k h => h.ofTKeep k + refine HT.bind (Q := fun _ σ => FIA σ ∧ BAs (t :: S) σ) (tcbNtfnDequeue_fia t n _ hR) fun _ => ?_ + refine HT.bind (Q := fun _ σ => FIA σ ∧ BAs (t :: S) σ) + (setThreadState_fia t .restart rfl (BAs (t :: S)) (fun σ p x x' hx h1 h => h.tcbUpd hx h1) + (fun σ a h => h.act a)) fun _ => ?_ + refine HT.bind (Q := fun _ σ => FIA σ ∧ BAs (t :: S) σ) + (HT.mono (P := fun σ => FIA σ ∧ BAs (t :: S) σ) (m := getTcbObjRef (Z := Unit) Tcb.tcbSchedContext t) + (HT.ofTPres (by unfold getTcbObjRef; tpres) _ hR) (fun σ h => h) (fun _ _ h => h)) fun scOpt => ?_ + refine HT.bind (Q := fun _ σ => FIA σ ∧ BAs (t :: S) σ) (refillCheck_fia scOpt _ hR) fun _ => ?_ + exact HT.mono (possibleSwitchTo_fia' t (BAs S) (fun σ f h => h.queue f) (fun σ a h => h.act a)) + (fun σ h => ⟨h.1, fun x sc hx hs => h.2 t (List.mem_cons_self ..) x sc hx hs, h.2.tail⟩) (fun _ _ h => h) + + + +theorem partialInv_ntfn {kobj : KernelObject} {e : Notification} + (he : partialInv KernelObject.notification kobj = some e) : KernelObject.notification e = kobj := by + unfold partialInv at he + by_cases hu : ∃! y, KernelObject.notification y = kobj + · rw [if_pos hu] at he + have := Option.some.inj he + unfold The at this + rw [dif_pos hu] at this + rw [← this]; exact (Classical.choose_spec hu).1 + · rw [if_neg hu] at he; cases he + +/-- `getNotification` returns the stored notification and leaves the state alone. -/ +theorem getNotification_mem {σ : State Unit} {n : ObjRef} {r : Notification × State Unit} + (h : r ∈ (getNotification (Z := Unit) n σ).1) : σ.kheap n = some (.notification r.1) ∧ r.2 = σ := by + unfold getNotification getSimpleKo at h + obtain ⟨y1, hy1, h1⟩ := mem_bindA.1 h + have hy1' : y1 ∈ (NonDet.assertOpt (σ.kheap n) σ).1 := hy1 + cases hk : σ.kheap n with + | none => rw [hk] at hy1'; exact absurd hy1' (Set.notMem_empty _) + | some ko => + rw [hk] at hy1' + have e1 : y1 = (ko, σ) := hy1' + subst e1 + obtain ⟨y2, hy2, h2⟩ := mem_bindA.1 h1 + obtain ⟨hP2, rfl⟩ := hy2 + cases hp : partialInv KernelObject.notification ko with + | none => rw [hp] at h2; exact absurd h2 (Set.notMem_empty _) + | some e => + rw [hp] at h2 + have e2 : r = (e, σ) := h2 + subst e2 + exact ⟨by rw [partialInv_ntfn hp], rfl⟩ + +/-- **`cancelAllSignals` keeps `FIA`**, given that the bound SCs of the waiting queue are active. -/ +theorem cancelAllSignals_fia (n : ObjRef) : + HT (fun σ => FIA σ ∧ ∀ x q, σ.kheap n = some (.notification x) → x.ntfnObj = .waitingNtfn q → BAs q σ) + (cancelAllSignals (Z := Unit) n) (fun _ σ => FIA σ) := by + unfold cancelAllSignals + refine HT.bind (Q := fun nt σ => FIA σ ∧ ∀ q, nt.ntfnObj = .waitingNtfn q → BAs q σ) ?_ fun nt => ?_ + · intro σ hσ r hr + obtain ⟨hk, hs⟩ := getNotification_mem hr + obtain ⟨a, s'⟩ := r + simp only at hk hs ⊢ + subst hs + exact ⟨hσ.1, fun q hq => hσ.2 a q hk hq⟩ + · rcases nt with ⟨o, b, s⟩ + cases o with + | waitingNtfn q => + dsimp only + refine HT.bind (Q := fun _ σ => FIA σ ∧ BAs [] σ) + (HT.mono (HT.mapMx (fun l σ => FIA σ ∧ BAs l σ) (fun a l => removeNtfn_fia a n l) q) + (fun σ h => ⟨h.1, h.2 q rfl⟩) (fun _ _ h => h)) fun _ => ?_ + exact HT.mono (resched_fia (fun _ => True) (fun _ _ _ => trivial) (fun _ _ _ => trivial)) + (fun σ h => ⟨h.1, trivial⟩) (fun _ _ h => h.1) + | idleNtfn => exact HT.pure () (fun σ h => h.1) + | activeNtfn _ => exact HT.pure () (fun σ h => h.1) + + +/-! ## The IPC restarts and the endpoint hooks -/ + +structure FrameOK (R : State Unit → Prop) : Prop where + keep : ∀ σ σ', TKeepA σ σ' → R σ → R σ' + tcb : ∀ σ p x x', σ.kheap p = some (.tcb x) → x'.tcbSchedContext = x.tcbSchedContext → R σ → + R { σ with kheap := Function.update σ.kheap p (some (.tcb x')) } + act : ∀ σ a, R σ → R { σ with schedulerAction := a } + +theorem FrameOK.bas (S : List ObjRef) : FrameOK (BAs S) := + ⟨fun _ _ k h => h.ofTKeep k, fun _ _ _ _ hx h1 h => h.tcbUpd hx h1, fun _ a h => h.act a⟩ + +instance (r : ObjRef) (u : (Option ObjRef → Option ObjRef) → Reply → Reply) (v : Option ObjRef) : + TPresInst (setReplyObjRef (Z := Unit) u r v) := + ⟨by + unfold setReplyObjRef updateSkObjRef + exact PresR.bind (getSimpleKo_tpres _ _) fun _ => setSimpleKo_tpres _ nsko_reply _ _⟩ + +theorem replyUnlinkTcb_fia (t r : ObjRef) (R : State Unit → Prop) (F : FrameOK R) : + HT (fun σ => FIA σ ∧ R σ) (replyUnlinkTcb (Z := Unit) t r) (fun _ σ => FIA σ ∧ R σ) := by + unfold replyUnlinkTcb + have tp : ∀ {β : Type} {m : SMonad Unit β}, TPres m → + HT (fun σ => FIA σ ∧ R σ) m (fun _ σ => FIA σ ∧ R σ) := fun h => HT.ofTPres h R F.keep + refine HT.bind (Q := fun _ σ => FIA σ ∧ R σ) (tp (by tpres)) fun _ => ?_ + refine HT.bind (Q := fun _ σ => FIA σ ∧ R σ) (tp (by tpres)) fun _ => ?_ + refine HT.bind (Q := fun _ σ => FIA σ ∧ R σ) (tp (by tpres)) fun _ => ?_ + refine HT.bind (Q := fun _ σ => FIA σ ∧ R σ) (tp (by tpres)) fun _ => ?_ + refine HT.bind (Q := fun _ σ => FIA σ ∧ R σ) (tp (by tpres)) fun _ => ?_ + exact setThreadState_fia t .inactive rfl R F.tcb F.act + +/-- The restart sequence of `restart_thread_if_no_fault`'s fault-free arm. -/ +theorem restartSeq_fia (t : ObjRef) (S : List ObjRef) : + HT (fun σ => FIA σ ∧ BAs (t :: S) σ) + (do setThreadState (Z := Unit) t .restart + let scOpt ← getTcbObjRef Tcb.tcbSchedContext t + ifSporadicCurScAssertRefillUnblockCheck scOpt + possibleSwitchTo t) + (fun _ σ => FIA σ ∧ BAs S σ) := by + have hR : ∀ σ σ', TKeepA σ σ' → BAs (t :: S) σ → BAs (t :: S) σ' := fun _ _ k h => h.ofTKeep k + refine HT.bind (Q := fun _ σ => FIA σ ∧ BAs (t :: S) σ) + (setThreadState_fia t .restart rfl (BAs (t :: S)) (fun σ p x x' hx h1 h => h.tcbUpd hx h1) + (fun σ a h => h.act a)) fun _ => ?_ + refine HT.bind (Q := fun _ σ => FIA σ ∧ BAs (t :: S) σ) + (HT.ofTPres (by unfold getTcbObjRef; tpres) _ hR) fun scOpt => ?_ + refine HT.bind (Q := fun _ σ => FIA σ ∧ BAs (t :: S) σ) (refillCheck_fia scOpt _ hR) fun _ => ?_ + exact HT.mono (possibleSwitchTo_fia' t (BAs S) (fun σ f h => h.queue f) (fun σ a h => h.act a)) + (fun σ h => ⟨h.1, fun x sc hx hs => h.2 t (List.mem_cons_self ..) x sc hx hs, h.2.tail⟩) (fun _ _ h => h) + +theorem restartThreadIfNoFault_fia (t : ObjRef) (S : List ObjRef) : + HT (fun σ => FIA σ ∧ BAs (t :: S) σ) (restartThreadIfNoFault (Z := Unit) t) + (fun _ σ => FIA σ ∧ BAs S σ) := by + unfold restartThreadIfNoFault + refine HT.bind (Q := fun _ σ => FIA σ ∧ BAs (t :: S) σ) + (HT.ofTPres (by tpres) _ (FrameOK.bas _).keep) fun fault => ?_ + refine HT.ite (fun _ => restartSeq_fia t S) (fun _ => ?_) + exact HT.mono (setThreadState_fia t .inactive rfl (BAs (t :: S)) (FrameOK.bas _).tcb (FrameOK.bas _).act) + (fun σ h => h) (fun _ σ h => ⟨h.1, h.2.tail⟩) + +theorem removeEp_fia (t ep : ObjRef) (S : List ObjRef) : + HT (fun σ => FIA σ ∧ BAs (t :: S) σ) (removeAndRestartEpQueuedThread (Z := Unit) t ep) + (fun _ σ => FIA σ ∧ BAs S σ) := by + unfold removeAndRestartEpQueuedThread + have F := FrameOK.bas (t :: S) + refine HT.bind (Q := fun _ σ => FIA σ ∧ BAs (t :: S) σ) (tcbEpDequeue_fia t ep _ F.keep) fun _ => ?_ + refine HT.bind (Q := fun _ σ => FIA σ ∧ BAs (t :: S) σ) (HT.ofTPres (by tpres) _ F.keep) fun st => ?_ + refine HT.bind (Q := fun _ σ => FIA σ ∧ BAs (t :: S) σ) (HT.ofTPres (by tpres) _ F.keep) fun replyOpt => ?_ + refine HT.bind (Q := fun _ σ => FIA σ ∧ BAs (t :: S) σ) ?_ fun _ => restartThreadIfNoFault_fia t S + cases replyOpt with + | none => exact HT.ofTPres (PresR.pure _) _ F.keep + | some r => exact replyUnlinkTcb_fia t r _ F + + + +theorem partialInv_ep {kobj : KernelObject} {e : Endpoint} + (he : partialInv KernelObject.endpoint kobj = some e) : KernelObject.endpoint e = kobj := by + unfold partialInv at he + by_cases hu : ∃! y, KernelObject.endpoint y = kobj + · rw [if_pos hu] at he + have := Option.some.inj he + unfold The at this + rw [dif_pos hu] at this + rw [← this]; exact (Classical.choose_spec hu).1 + · rw [if_neg hu] at he; cases he + +theorem getEndpoint_mem {σ : State Unit} {n : ObjRef} {r : Endpoint × State Unit} + (h : r ∈ (getEndpoint (Z := Unit) n σ).1) : σ.kheap n = some (.endpoint r.1) ∧ r.2 = σ := by + unfold getEndpoint getSimpleKo at h + obtain ⟨y1, hy1, h1⟩ := mem_bindA.1 h + have hy1' : y1 ∈ (NonDet.assertOpt (σ.kheap n) σ).1 := hy1 + cases hk : σ.kheap n with + | none => rw [hk] at hy1'; exact absurd hy1' (Set.notMem_empty _) + | some ko => + rw [hk] at hy1' + have e1 : y1 = (ko, σ) := hy1' + subst e1 + obtain ⟨y2, hy2, h2⟩ := mem_bindA.1 h1 + obtain ⟨hP2, rfl⟩ := hy2 + cases hp : partialInv KernelObject.endpoint ko with + | none => rw [hp] at h2; exact absurd h2 (Set.notMem_empty _) + | some e => + rw [hp] at h2 + have e2 : r = (e, σ) := h2 + subst e2 + exact ⟨by rw [partialInv_ep hp], rfl⟩ + +/-- **`cancelAllIpc` keeps `FIA`**, given that the bound SCs of the endpoint's queue are active. -/ +theorem cancelAllIpc_fia (ep : ObjRef) : + HT (fun σ => FIA σ ∧ ∀ x, σ.kheap ep = some (.endpoint x) → BAs x.epQueue σ) + (cancelAllIpc (Z := Unit) ep) (fun _ σ => FIA σ) := by + unfold cancelAllIpc + refine HT.bind (Q := fun e σ => FIA σ ∧ BAs e.epQueue σ) ?_ fun e => ?_ + · intro σ hσ r hr + obtain ⟨hk, hs⟩ := getEndpoint_mem hr + obtain ⟨a, s'⟩ := r + simp only at hk hs ⊢ + subst hs + exact ⟨hσ.1, hσ.2 a hk⟩ + · have main : ∀ q : List ObjRef, HT (fun σ => FIA σ ∧ BAs q σ) + (do let queue ← (pure q : SMonad Unit (List ObjRef)) + NonDet.mapMx (fun t => removeAndRestartEpQueuedThread (Z := Unit) t ep) queue + rescheduleRequired) (fun _ σ => FIA σ) := fun q => + HT.bind (Q := fun r σ => FIA σ ∧ BAs r σ) (HT.pure q fun σ h => h) fun queue => + HT.bind (Q := fun _ σ => FIA σ ∧ BAs [] σ) + (HT.mapMx (fun l σ => FIA σ ∧ BAs l σ) (fun a l => removeEp_fia a ep l) queue) fun _ => + HT.mono (resched_fia (fun _ => True) (fun _ _ _ => trivial) (fun _ _ _ => trivial)) + (fun σ h => ⟨h.1, trivial⟩) (fun _ _ h => h.1) + cases e with + | idleEP => exact HT.pure () (fun σ h => h.1) + | sendEP q => exact HT.mono (main q) (fun σ h => h) (fun _ _ h => h) + | recvEP q => exact HT.mono (main q) (fun σ h => h) (fun _ _ h => h) + +theorem removeBadged_fia (t ep : ObjRef) (badge : Badge) (S : List ObjRef) : + HT (fun σ => FIA σ ∧ BAs (t :: S) σ) (removeAndRestartBadgedThread (Z := Unit) t ep badge) + (fun _ σ => FIA σ ∧ BAs S σ) := by + unfold removeAndRestartBadgedThread + have F := FrameOK.bas (t :: S) + refine HT.bind (Q := fun _ σ => FIA σ ∧ BAs (t :: S) σ) (HT.ofTPres (by tpres) _ F.keep) fun st => ?_ + refine HT.when (fun _ => ?_) (fun _ σ h => ⟨h.1, h.2.tail⟩) + exact HT.bind (Q := fun _ σ => FIA σ ∧ BAs (t :: S) σ) (tcbEpDequeue_fia t ep _ F.keep) fun _ => + restartThreadIfNoFault_fia t S + +theorem cancelBadgedSends_fia (ep : ObjRef) (badge : Badge) : + HT (fun σ => FIA σ ∧ ∀ x, σ.kheap ep = some (.endpoint x) → BAs x.epQueue σ) + (cancelBadgedSends (Z := Unit) ep badge) (fun _ σ => FIA σ) := by + unfold cancelBadgedSends + refine HT.bind (Q := fun e σ => FIA σ ∧ BAs e.epQueue σ) ?_ fun e => ?_ + · intro σ hσ r hr + obtain ⟨hk, hs⟩ := getEndpoint_mem hr + obtain ⟨a, s'⟩ := r + simp only at hk hs ⊢ + subst hs + exact ⟨hσ.1, hσ.2 a hk⟩ + · cases e with + | sendEP q => + exact HT.bind (Q := fun _ σ => FIA σ ∧ BAs [] σ) + (HT.mapMx (fun l σ => FIA σ ∧ BAs l σ) (fun a l => removeBadged_fia a ep badge l) q) fun _ => + HT.mono (resched_fia (fun _ => True) (fun _ _ _ => trivial) (fun _ _ _ => trivial)) + (fun σ h => ⟨h.1, trivial⟩) (fun _ _ h => h.1) + | idleEP => exact HT.pure () (fun σ h => h.1) + | recvEP q => exact HT.pure () (fun σ h => h.1) + + +/-- **The side premise of the cancel hooks from F8(c)**: the queue's threads are blocked, so their bound SCs are +active. -/ +theorem BAs.ofBlocked {σ : State Unit} (h : FIA σ) {q : List ObjRef} + (hb : ∀ t ∈ q, ∀ x, σ.kheap t = some (.tcb x) → BlockedTsA x.tcbState = true) : BAs q σ := + fun u hu x sc e hs => + let ⟨y, k, ey, ea⟩ := h.1.blk u x sc e (hb u hu x e) hs + ⟨x, sc, e, hs, y, k, ey, ea⟩ + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3DelSvAkA.lean b/proofs/Fl4ProofsA64/P3/Stage3DelSvAkA.lean new file mode 100644 index 000000000..45095d4e3 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3DelSvAkA.lean @@ -0,0 +1,409 @@ +/- +P3 (AArch64, delete hooks, contract §87): **spec steps that keep the scheduler's data** (`AkA`: queues, action, +current/idle thread, current SC, and every heap object except `ArchObj`s), and their consequences: + +* **`SchedValidA.ofAk`**; `NdAkA m` (every outcome of `m` is an `AkA` step) with its closure lemmas (the `NdTmA` + framework of `P3/DelTimeSpecA` at another relation); +* **`fl4FinaliseCap_akA`** on every arch cap but a VCPU's, **`preemptionPoint_akA`**, **`postCapDeletionIrq_akA`**. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.DelTimeSpecA +import Fl4ProofsA64.P3.Stage3SchedValidA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4Spec Fl4SpecA64 + +noncomputable section + +/-- **A step keeping the scheduler's data** (module docstring). -/ +structure AkA (σ σ' : State Unit) : Prop where + rq : σ'.readyQueues = σ.readyQueues + rls : σ'.releaseQueue = σ.releaseQueue + act : σ'.schedulerAction = σ.schedulerAction + cur : σ'.curThread = σ.curThread + idle : σ'.idleThread = σ.idleThread + csc : σ'.curSc = σ.curSc + kh : ∀ p, σ'.kheap p = σ.kheap p ∨ ∃ a a', σ.kheap p = some (.archObj a) ∧ σ'.kheap p = some (.archObj a') + +theorem akA_refl (σ : State Unit) : AkA σ σ := ⟨rfl, rfl, rfl, rfl, rfl, rfl, fun _ => Or.inl rfl⟩ + +theorem akA_trans {a b c : State Unit} (h1 : AkA a b) (h2 : AkA b c) : AkA a c := by + refine ⟨h2.rq.trans h1.rq, h2.rls.trans h1.rls, h2.act.trans h1.act, h2.cur.trans h1.cur, h2.idle.trans h1.idle, + h2.csc.trans h1.csc, fun p => ?_⟩ + rcases h2.kh p with e2 | ⟨x, y, e2, e2'⟩ + · rcases h1.kh p with e1 | ⟨x, y, e1, e1'⟩ + · exact Or.inl (e2.trans e1) + · exact Or.inr ⟨x, y, e1, by rw [e2]; exact e1'⟩ + · rcases h1.kh p with e1 | ⟨x', y', e1, -⟩ + · exact Or.inr ⟨x, y, by rw [← e1]; exact e2, e2'⟩ + · exact Or.inr ⟨x', y, e1, e2'⟩ + +theorem SchedValidA.ofAk {σ σ' : State Unit} (h : SchedValidA σ) (H : AkA σ σ') : SchedValidA σ' := by + have keep : ∀ p o, (∀ a, o ≠ .archObj a) → σ.kheap p = some o → σ'.kheap p = some o := by + intro p o ho e + rcases H.kh p with e1 | ⟨a, a', e1, -⟩ + · rw [e1]; exact e + · rw [e] at e1; exact absurd (Option.some.inj e1) (ho a) + have back : ∀ p o, (∀ a, o ≠ .archObj a) → σ'.kheap p = some o → σ.kheap p = some o := by + intro p o ho e + rcases H.kh p with e1 | ⟨a, a', -, e1⟩ + · rw [← e1]; exact e + · rw [e] at e1; exact absurd (Option.some.inj e1) (ho a') + exact h.congr H.rq H.rls H.act H.cur H.idle H.csc (fun p y k e => keep p _ (fun a h => by cases h) e) + (fun t x e => ⟨x, keep t _ (fun a h => by cases h) e, rfl⟩) + (fun t x e => ⟨x, back t _ (fun a h => by cases h) e, rfl, rfl⟩) + +theorem VmA.akA {σ σ' : State Unit} (h : VmA σ σ') : AkA σ σ' := by + obtain ⟨mr, am, vt, nv, rfl⟩ := h + exact ⟨rfl, rfl, rfl, rfl, rfl, rfl, fun _ => Or.inl rfl⟩ + +/-- **Every outcome keeps `AkA`** (module docstring). -/ +def NdAkA {α : Type} (m : NonDet (State Unit) α) : Prop := ∀ σ r, r ∈ (m σ).1 → AkA σ r.2 + +section +variable {α β ε : Type} + +theorem NdAkA.ro {m : NonDet (State Unit) α} (h : ∀ σ r, r ∈ (m σ).1 → r.2 = σ) : NdAkA m := fun σ r hr => by + rw [h σ r hr]; exact akA_refl σ + +theorem NdAkA.pure (a : α) : NdAkA (Pure.pure a : NonDet (State Unit) α) := + NdAkA.ro fun _ _ h => by rw [Set.mem_singleton_iff.1 h] + +theorem NdAkA.npure (a : α) : NdAkA (NonDet.pure a : NonDet (State Unit) α) := NdAkA.pure a + +theorem NdAkA.bind {m : NonDet (State Unit) α} {f : α → NonDet (State Unit) β} (hm : NdAkA m) + (hf : ∀ a, NdAkA (f a)) : NdAkA (m >>= f) := fun σ r hr => by + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + exact akA_trans (hm σ x hx) (hf x.1 x.2 r hr) + +theorem NdAkA.nbind {m : NonDet (State Unit) α} {f : α → NonDet (State Unit) β} (hm : NdAkA m) + (hf : ∀ a, NdAkA (f a)) : NdAkA (NonDet.bind m f) := NdAkA.bind hm hf + +theorem NdAkA.fail : NdAkA (NonDet.fail : NonDet (State Unit) α) := fun _ _ h => h.elim + +theorem NdAkA.gets (g : State Unit → α) : NdAkA (NonDet.gets g) := + NdAkA.ro fun _ _ h => by rw [Set.mem_singleton_iff.1 h] + +theorem NdAkA.get : NdAkA (NonDet.get : NonDet (State Unit) (State Unit)) := + NdAkA.ro fun _ _ h => by rw [Set.mem_singleton_iff.1 h] + +theorem NdAkA.assert (P : Prop) : NdAkA (NonDet.assert P : NonDet (State Unit) Unit) := + NdAkA.ro fun _ _ h => by rw [h.2] + +theorem NdAkA.assertOpt (o : Option α) : NdAkA (NonDet.assertOpt o : NonDet (State Unit) α) := by + cases o + · exact NdAkA.fail + · exact NdAkA.npure _ + +theorem NdAkA.getsThe (g : State Unit → Option α) : NdAkA (NonDet.getsThe g) := fun σ r hr => + NdAkA.assertOpt (g σ) σ r hr + +theorem NdAkA.getsMap {γ : Type} (g : State Unit → γ → Option α) (p : γ) : NdAkA (NonDet.getsMap g p) := + NdAkA.nbind (NdAkA.gets _) fun _ => NdAkA.assertOpt _ + +theorem NdAkA.selectF (S : Set α × Prop) : NdAkA (NonDet.selectF S : NonDet (State Unit) α) := + NdAkA.ro fun _ _ h => h.2 + +theorem NdAkA.modify {g : State Unit → State Unit} (h : ∀ σ, AkA σ (g σ)) : NdAkA (NonDet.modify g) := + fun σ r hr => by rw [Set.mem_singleton_iff.1 hr]; exact h σ + +theorem NdAkA.when {P : Prop} {inst : Decidable P} {m : NonDet (State Unit) Unit} (h : NdAkA m) : NdAkA (NonDet.when P m) := by + unfold NonDet.when; split + · exact h + · exact NdAkA.npure _ + +theorem NdAkA.mapM {f : α → NonDet (State Unit) β} (h : ∀ a, NdAkA (f a)) : + ∀ l : List α, NdAkA (NonDet.mapM f l) + | [] => NdAkA.npure _ + | a :: l => NdAkA.nbind (h a) fun _ => NdAkA.nbind (NdAkA.mapM h l) fun _ => NdAkA.npure _ + +theorem NdAkA.ofOk {Q : State Unit → State Unit → Prop} (hQ : ∀ a b, Q a b → AkA a b) + {m : NonDet (State Unit) α} (h : NdOkA Q m) : NdAkA m := fun σ r hr => hQ _ _ ((h σ).2.2 r hr) + +/-! ### The `NonDetE` forms (on `run`) -/ + +theorem NdAkA.liftE {m : NonDet (State Unit) α} (h : NdAkA m) : NdAkA (NonDetE.liftE m : NonDetE (State Unit) ε α).run := by + unfold NonDetE.liftE + rw [ExceptT.run_lift] + exact NdAkA.bind h fun _ => NdAkA.pure _ + +theorem NdAkA.runBind {x : NonDetE (State Unit) ε α} {f : α → NonDetE (State Unit) ε β} (hx : NdAkA x.run) + (hf : ∀ a, NdAkA (f a).run) : NdAkA (x >>= f).run := by + rw [ExceptT.run_bind] + refine NdAkA.bind hx fun r => ?_ + cases r with + | error e => exact NdAkA.pure _ + | ok a => exact hf a + +theorem NdAkA.runPure (a : α) : NdAkA (Pure.pure a : NonDetE (State Unit) ε α).run := by + rw [ExceptT.run_pure]; exact NdAkA.pure _ + +theorem NdAkA.returnOk (a : α) : NdAkA (NonDetE.returnOk a : NonDetE (State Unit) ε α).run := NdAkA.runPure a + +theorem NdAkA.throwError (e : ε) : NdAkA (NonDetE.throwError e : NonDetE (State Unit) ε α).run := by + unfold NonDetE.throwError + show NdAkA (ExceptT.run (throw e : ExceptT ε (NonDet (State Unit)) α)) + exact NdAkA.pure _ + +theorem NdAkA.throwOpt (e : ε) (o : Option α) : NdAkA (NonDetE.throwOpt e o : NonDetE (State Unit) ε α).run := by + cases o + · exact NdAkA.throwError e + · exact NdAkA.runPure _ + +theorem NdAkA.unlessE {P : Prop} {inst : Decidable P} {m : NonDetE (State Unit) ε Unit} (h : NdAkA m.run) : + NdAkA (NonDetE.unlessE P m).run := by + unfold NonDetE.unlessE; split + · exact NdAkA.runPure _ + · exact h + +theorem NdAkA.whenE {P : Prop} {inst : Decidable P} {m : NonDetE (State Unit) ε Unit} (h : NdAkA m.run) : + NdAkA (NonDetE.whenE P m).run := by + unfold NonDetE.whenE; split + · exact h + · exact NdAkA.runPure _ + +theorem NdAkA.catch {m : NonDetE (State Unit) ε α} {h : ε → NonDet (State Unit) α} (hm : NdAkA m.run) + (hh : ∀ e, NdAkA (h e)) : NdAkA (NonDetE.catch m h) := by + unfold NonDetE.catch + refine NdAkA.nbind hm fun r => ?_ + cases r with + | error e => exact hh e + | ok a => exact NdAkA.npure a + +end + +/-! ## The spec's VSpace operations -/ + +theorem getObject_akA (p : ObjRef) : NdAkA (getObject (Z := Unit) p) := NdAkA.ro fun _ _ h => (getObject_mem h).1 + +/-- Writing an arch object keeps `AkA` (`set_object` asserts the object's type). -/ +theorem setObject_akA (p : ObjRef) (a : ArchKernelObj) : NdAkA (setObject (Z := Unit) p (.archObj a)) := fun σ r hr => by + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + obtain ⟨e1, e2⟩ := getObject_mem hx + obtain ⟨y, hy, hr⟩ := mem_bindA.1 hr + obtain ⟨ht, hy⟩ := hy + rw [hy] at hr + obtain ⟨z, hz, hr⟩ := mem_bindA.1 hr + obtain rfl : z = (x.2, x.2) := hz + obtain rfl : r = ((), { x.2 with kheap := Function.update x.2.kheap p (some (.archObj a)) }) := hr + rw [e1] + obtain ⟨a0, h0⟩ : ∃ a0, x.1 = .archObj a0 := by + generalize x.1 = o at ht + cases o with + | archObj a0 => exact ⟨a0, rfl⟩ + | _ => simp only [aType] at ht; (try split at ht) <;> cases ht + refine ⟨rfl, rfl, rfl, rfl, rfl, rfl, fun q => ?_⟩ + by_cases e : q = p + · subst e + exact Or.inr ⟨a0, a, by rw [e2, h0], Function.update_self ..⟩ + · exact Or.inl (Function.update_of_ne e ..) + +theorem doMachineOp_akA {α : Type} (g : NonDet MachineState α) : NdAkA (doMachineOp (Z := Unit) g) := by + unfold doMachineOp + refine NdAkA.bind (NdAkA.gets _) fun _ => NdAkA.bind (NdAkA.selectF _) fun x => ?_ + exact NdAkA.bind (NdAkA.modify fun σ => ⟨rfl, rfl, rfl, rfl, rfl, rfl, fun _ => Or.inl rfl⟩) fun _ => NdAkA.pure _ + +macro "ak_auto" : tactic => `(tactic| repeat' first + | exact NdAkA.pure _ | exact NdAkA.npure _ | exact NdAkA.gets _ | exact NdAkA.get | exact NdAkA.assert _ + | exact NdAkA.assertOpt _ | exact NdAkA.getsThe _ | exact NdAkA.getsMap _ _ | exact NdAkA.selectF _ + | exact NdAkA.fail | exact getObject_akA _ | exact setObject_akA _ _ | exact doMachineOp_akA _ + | exact NdAkA.runPure _ | exact NdAkA.returnOk _ | exact NdAkA.throwError _ | exact NdAkA.throwOpt _ _ + | exact NdAkA.modify (fun _ => ⟨rfl, rfl, rfl, rfl, rfl, rfl, fun _ => Or.inl rfl⟩) + | refine NdAkA.liftE ?_ | refine NdAkA.unlessE ?_ | refine NdAkA.whenE ?_ | refine NdAkA.when ?_ + | refine NdAkA.catch ?_ (fun _ => ?_) + | refine NdAkA.runBind ?_ (fun _ => ?_) + | refine NdAkA.bind ?_ (fun _ => ?_) | refine NdAkA.nbind ?_ (fun _ => ?_) + | split) + +theorem invalidateTlbByAsid_akA (asid : Asid) : NdAkA (invalidateTlbByAsid (Z := Unit) asid) := + NdAkA.ofOk (fun _ _ h => VmA.akA h) (invalidateTlbByAsid_vmA asid) + +theorem invalidateAsidEntry_akA (asid : Asid) : NdAkA (invalidateAsidEntry (Z := Unit) asid) := + NdAkA.ofOk (fun _ _ h => VmA.akA h) (invalidateAsidEntry_vmA asid) + +theorem invalidateTlbByAsidVa_akA (asid : Asid) (v : VSpaceRef) : NdAkA (invalidateTlbByAsidVa (Z := Unit) asid v) := by + unfold invalidateTlbByAsidVa + refine NdAkA.bind (NdAkA.ofOk (fun _ _ h => VmA.akA h) (loadVmid_vmA asid)) fun mv => ?_ + cases mv with + | none => exact NdAkA.pure _ + | some vmid => exact doMachineOp_akA _ + +theorem findVspaceForAsid_akA (asid : Asid) : NdAkA (findVspaceForAsid (Z := Unit) asid).run := + NdAkA.ofOk (fun _ _ h => VmA.akA h) (findVspaceForAsid_vmA asid) + +theorem getCap_akA (sl : CslotPtr) : NdAkA (getCap (Z := Unit) sl) := by + obtain ⟨oref, cref⟩ := sl + unfold getCap + ak_auto + +theorem setVmRoot_akA (t : ObjRef) : NdAkA (setVmRoot (Z := Unit) t) := by + unfold setVmRoot + refine NdAkA.bind (NdAkA.pure _) fun _ => NdAkA.bind (getCap_akA _) fun c => ?_ + refine NdAkA.catch ?_ fun _ => NdAkA.ofOk (fun _ _ h => VmA.akA h) setGlobalUserVspace_vmA + split + · refine NdAkA.runBind (findVspaceForAsid_akA _) fun _ => NdAkA.runBind ?_ fun _ => + NdAkA.liftE (NdAkA.ofOk (fun _ _ h => VmA.akA h) (armContextSwitch_vmA _ _)) + exact NdAkA.whenE (NdAkA.throwError _) + · exact NdAkA.throwError _ + +theorem setAsidPool_akA (p : ObjRef) (pool : AsidPool) : NdAkA (setAsidPool (Z := Unit) p pool) := by + unfold setAsidPool; ak_auto + +theorem storePte_akA (t : PtType) (p : ObjRef) (pte : PTE) : NdAkA (storePte (Z := Unit) t p pte) := by + unfold storePte setPt; ak_auto + +theorem ptLookupFromLevelN_akA : ∀ (n : Nat) (lv : VmLevel) (p : ObjRef) (v : VSpaceRef) (t : ObjRef), + NdAkA (ptLookupFromLevelN (Z := Unit) n lv p v t).run + | 0, lv, p, v, t => by unfold ptLookupFromLevelN; ak_auto + | n + 1, lv, p, v, t => by + unfold ptLookupFromLevelN + ak_auto + all_goals exact ptLookupFromLevelN_akA n _ _ _ _ + +theorem deleteAsid_akA (asid : Asid) (pt : ObjRef) : NdAkA (deleteAsid (Z := Unit) asid pt) := by + unfold deleteAsid + refine NdAkA.bind (NdAkA.gets _) fun o => ?_ + split + · exact NdAkA.pure _ + · refine NdAkA.bind (NdAkA.getsMap _ _) fun _ => NdAkA.when ?_ + exact NdAkA.bind (invalidateTlbByAsid_akA _) fun _ => NdAkA.bind (invalidateAsidEntry_akA _) fun _ => + NdAkA.bind (NdAkA.getsMap _ _) fun _ => NdAkA.bind (NdAkA.pure _) fun _ => + NdAkA.bind (setAsidPool_akA _ _) fun _ => NdAkA.bind (NdAkA.gets _) fun _ => setVmRoot_akA _ + +theorem deleteAsidPool_akA (base : Asid) (ptr : ObjRef) : NdAkA (deleteAsidPool (Z := Unit) base ptr) := by + unfold deleteAsidPool + refine NdAkA.bind (NdAkA.assert _) fun _ => NdAkA.bind (NdAkA.gets _) fun _ => NdAkA.when ?_ + refine NdAkA.bind (NdAkA.getsMap _ _) fun _ => NdAkA.bind (NdAkA.mapM (fun _ => NdAkA.when ?_) _) fun _ => + NdAkA.bind (NdAkA.pure _) fun _ => NdAkA.bind (NdAkA.modify fun _ => ⟨rfl, rfl, rfl, rfl, rfl, rfl, fun _ => Or.inl rfl⟩) fun _ => + NdAkA.bind (NdAkA.gets _) fun _ => setVmRoot_akA _ + exact NdAkA.bind (invalidateTlbByAsid_akA _) fun _ => invalidateAsidEntry_akA _ + +theorem unmapPageTable_akA (asid : Asid) (v : VSpaceRef) (pt : ObjRef) : + NdAkA (unmapPageTable (Z := Unit) asid v pt) := by + unfold unmapPageTable + refine NdAkA.catch ?_ fun _ => NdAkA.pure _ + refine NdAkA.runBind (findVspaceForAsid_akA _) fun _ => NdAkA.runBind ?_ fun x => ?_ + · unfold ptLookupFromLevel; exact ptLookupFromLevelN_akA _ _ _ _ _ + · obtain ⟨sl, lv⟩ := x + exact NdAkA.runBind (NdAkA.liftE (storePte_akA _ _ _)) fun _ => + NdAkA.runBind (NdAkA.liftE (doMachineOp_akA _)) fun _ => NdAkA.liftE (invalidateTlbByAsid_akA _) + +theorem unmapPage_akA (sz : VMPageSize) (asid : Asid) (v : VSpaceRef) (p : ObjRef) : + NdAkA (unmapPage (Z := Unit) sz asid v p) := by + unfold unmapPage + refine NdAkA.catch ?_ fun _ => NdAkA.pure _ + refine NdAkA.runBind (findVspaceForAsid_akA _) fun _ => NdAkA.runBind (NdAkA.liftE (NdAkA.getsThe _)) fun x => ?_ + obtain ⟨lv, sl⟩ := x + exact NdAkA.runBind (NdAkA.unlessE (NdAkA.throwError _)) fun _ => + NdAkA.runBind (NdAkA.liftE (NdAkA.getsMap _ _)) fun _ => + NdAkA.runBind (NdAkA.unlessE (NdAkA.throwError _)) fun _ => + NdAkA.runBind (NdAkA.liftE (storePte_akA _ _ _)) fun _ => + NdAkA.runBind (NdAkA.liftE (doMachineOp_akA _)) fun _ => NdAkA.liftE (invalidateTlbByAsidVa_akA _ _) + +theorem storeExtSlot_akA (sl : ExtSlot) (pte : PTE) : NdAkA (storeExtSlot (Z := Unit) sl pte) := by + cases sl with + | root r i => unfold storeExtSlot; ak_auto + | pt p => exact storePte_akA _ _ _ + +theorem findExtRoot_akA (k : ExtRootKind) (asid : Asid) : NdAkA (findExtRoot (Z := Unit) k asid).run := by + unfold findExtRoot findAnyRootForAsid; ak_auto + +theorem extUnmapPage_akA (k : ExtRootKind) (sz : VMPageSize) (asid : Asid) (v : VSpaceRef) (p : ObjRef) : + NdAkA (extUnmapPage (Z := Unit) k sz asid v p) := by + unfold extUnmapPage + refine NdAkA.catch ?_ fun _ => NdAkA.pure _ + refine NdAkA.runBind (NdAkA.unlessE (NdAkA.throwError _)) fun _ => NdAkA.runBind (findExtRoot_akA _ _) fun _ => ?_ + exact NdAkA.runBind (NdAkA.liftE (NdAkA.gets _)) fun _ => NdAkA.runBind (NdAkA.unlessE (NdAkA.throwError _)) fun _ => + NdAkA.runBind (NdAkA.liftE (NdAkA.gets _)) fun _ => NdAkA.runBind (NdAkA.unlessE (NdAkA.throwError _)) fun _ => + NdAkA.liftE (storeExtSlot_akA _ _) + +theorem extUnmapTable_akA (k : ExtRootKind) (asid : Asid) (v : VSpaceRef) (pt : ObjRef) : + NdAkA (extUnmapTable (Z := Unit) k asid v pt) := by + unfold extUnmapTable + refine NdAkA.catch ?_ fun _ => NdAkA.pure _ + refine NdAkA.runBind (findExtRoot_akA _ _) fun _ => NdAkA.runBind (NdAkA.liftE (NdAkA.gets _)) fun o => ?_ + cases o with + | some sl => exact NdAkA.liftE (storeExtSlot_akA _ _) + | none => exact NdAkA.returnOk _ + +theorem extKindOfAsid_akA (asid : Asid) : NdAkA (extKindOfAsid (Z := Unit) asid) := by + unfold extKindOfAsid; ak_auto + +/-- **`archFinaliseCap` keeps `AkA`** on every arch cap but a VCPU's. -/ +theorem archFinaliseCap_akA (c : ArchCap) (hc : ∀ v, c ≠ .vcpuCap v) (d : Bool) : + NdAkA (archFinaliseCap (Z := Unit) c d) := by + unfold archFinaliseCap + split + · exact NdAkA.bind (deleteAsidPool_akA _ _) fun _ => NdAkA.pure _ + · exact NdAkA.bind (deleteAsid_akA _ _) fun _ => NdAkA.pure _ + · exact NdAkA.bind (unmapPageTable_akA _ _ _) fun _ => NdAkA.pure _ + · exact NdAkA.bind (unmapPage_akA _ _ _ _) fun _ => NdAkA.pure _ + all_goals first | exact NdAkA.pure _ | exact absurd rfl (hc _) | (exfalso; simp_all) + +/-- **`fl4FinaliseCap` keeps `AkA`** on every arch cap but a VCPU's (module docstring). -/ +theorem fl4FinaliseCap_akA (c : ArchCap) (hc : ∀ v, c ≠ .vcpuCap v) (d : Bool) : + NdAkA (fl4FinaliseCap (Z := Unit) (.archObjectCap c) d) := by + have hF : NdAkA (finaliseCap (Z := Unit) (.archObjectCap c) d) := by + unfold finaliseCap; exact archFinaliseCap_akA c hc d + unfold fl4FinaliseCap + split + all_goals first + | exact hF + | (refine NdAkA.bind (extKindOfAsid_akA _) fun k => ?_; split + all_goals first + | exact hF + | exact NdAkA.pure _ + | exact NdAkA.bind (extUnmapPage_akA _ _ _ _ _) fun _ => NdAkA.pure _ + | (split + · exact NdAkA.bind (extUnmapTable_akA _ _ _ _) fun _ => NdAkA.pure _ + · exact NdAkA.pure _)) + | exact NdAkA.bind (extUnmapPage_akA _ _ _ _ _) fun _ => NdAkA.pure _ + | (split + · split + · exact NdAkA.bind (deleteAsid_akA _ _) fun _ => NdAkA.pure _ + · exact NdAkA.pure _ + · exact NdAkA.pure _) + | (exfalso; simp_all) + + +theorem getScActive_akA (p : ObjRef) : NdAkA (getScActive (Z := Unit) p) := by + unfold getScActive + ak_auto + +theorem preemptionPoint_akA : NdAkA (preemptionPoint (Z := Unit)).run := by + have m : ∀ g : State Unit → State Unit, (∀ σ, AkA σ (g σ)) → NdAkA (NonDet.modify g) := fun g h => NdAkA.modify h + have k : ∀ σ : State Unit, ∀ τ : State Unit, τ.readyQueues = σ.readyQueues → τ.releaseQueue = σ.releaseQueue → + τ.schedulerAction = σ.schedulerAction → τ.curThread = σ.curThread → τ.idleThread = σ.idleThread → + τ.curSc = σ.curSc → τ.kheap = σ.kheap → AkA σ τ := + fun _ _ a b c d e f g => ⟨a, b, c, d, e, f, fun _ => Or.inl (by rw [g])⟩ + have u : NdAkA (updateTimeStamp (Z := Unit)) := by + unfold updateTimeStamp + refine NdAkA.bind (NdAkA.gets _) fun _ => NdAkA.bind (doMachineOp_akA _) fun _ => + NdAkA.bind (NdAkA.pure _) fun _ => ?_ + refine NdAkA.bind (m _ fun σ => k _ _ rfl rfl rfl rfl rfl rfl rfl) fun _ => ?_ + refine NdAkA.bind (m _ fun σ => k _ _ rfl rfl rfl rfl rfl rfl rfl) fun _ => ?_ + refine NdAkA.bind (NdAkA.gets _) fun _ => NdAkA.when ?_ + split <;> exact m _ fun σ => k _ _ rfl rfl rfl rfl rfl rfl rfl + have x : ∀ e, NdAkA (doExtendedOp (Z := Unit) e) := fun e => by + unfold doExtendedOp + exact NdAkA.bind NdAkA.get fun _ => NdAkA.bind (NdAkA.selectF _) fun _ => m _ fun σ => k _ _ rfl rfl rfl rfl rfl rfl rfl + unfold preemptionPoint orChoiceE + repeat' first + | exact x _ | exact u | exact doMachineOp_akA _ | exact NdAkA.get | exact NdAkA.gets _ | exact NdAkA.selectF _ + | exact NdAkA.returnOk _ | exact NdAkA.throwError _ + | exact getScActive_akA _ + | refine NdAkA.liftE ?_ | refine NdAkA.whenE ?_ + | refine NdAkA.runBind ?_ (fun _ => ?_) + | split + +theorem postCapDeletionIrq_akA (irq : Irq) : NdAkA (postCapDeletion (Z := Unit) (.irqHandlerCap irq)) := by + show NdAkA (setIrqState (Z := Unit) .irqInactive irq) + unfold setIrqState + exact NdAkA.bind (NdAkA.modify fun σ => ⟨rfl, rfl, rfl, rfl, rfl, rfl, fun _ => Or.inl rfl⟩) fun _ => doMachineOp_akA _ + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3DelSvIpcA.lean b/proofs/Fl4ProofsA64/P3/Stage3DelSvIpcA.lean new file mode 100644 index 000000000..145c2fb9c --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3DelSvIpcA.lean @@ -0,0 +1,443 @@ +/- +P3 (AArch64, delete hooks, contract §87): **the Endpoint and Notification hooks at `DelWTopA` / `DelWxTopA`**. + +The waiting queue's threads are blocked in the spec by the record (`EpStateRel` / `NtfnStateRel`: queued TCBs carry +the blocked state words), so G1's `cancelAll{Ipc,Signals}_schedValid` apply through `BAs.ofBlockedV`. The +notification finaliser's two unbinds keep `TKeepA` and the notification's queue (`NKeepA`). + +* **`bas_of_codeA`**, **`finSv_epA`**, **`finSv_ntfnA`**. +* **`fl4FinaliseCap_ep_tmA`**, **`fl4FinaliseCap_ntfn_tmA`** (the time facts). +* **`finHookEpSvA`**, **`finHookNtfnSvA`**. + +Named premises: the hooks' own, and [P] `DelArchA`'s `clockFr`. New module. +-/ +import Fl4ProofsA64.P3.Stage3DelSvLiftA +import Fl4ProofsA64.P3.DelThrTimeA +import Fl4ProofsA64.P3.DelEpHookA +import Fl4ProofsA64.P3.DelNtfnHookA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +/-- Queued TCBs whose state word is a blocked code have active bound SCs. -/ +theorem bas_of_codeA {σ : State Unit} (sv : SchedValidA σ) {xs : List U64} + (h : ∀ t ∈ xs, ∀ x, σ.kheap (specPtr t) = some (.tcb x) → BlockedTsA x.tcbState = true) : + BAs (xs.map specPtr) σ := by + refine BAs.ofBlockedV sv fun u hu x hx => ?_ + obtain ⟨t, ht, rfl⟩ := List.mem_map.1 hu + exact h t ht x hx + +theorem blocked_of_codeA {ts : ThreadState} {k : Nat} (h : k = tsCode ts) (hk : k = 3 ∨ k = 4 ∨ k = 6) : + BlockedTsA ts = true := by + subst h + cases ts <;> simp [tsCode, BlockedTsA] at hk ⊢ + +/-- `TKeepA` with the notification at `n` keeping its queue. -/ +def NKeepA (n : ObjRef) (σ σ' : State Unit) : Prop := + TKeepA σ σ' ∧ ∀ x', σ'.kheap n = some (.notification x') → + ∃ x, σ.kheap n = some (.notification x) ∧ x'.ntfnObj = x.ntfnObj + +instance (n : ObjRef) : PreO (NKeepA n) where + refl σ := ⟨PreO.refl σ, fun x h => ⟨x, h, rfl⟩⟩ + trans h1 h2 := ⟨PreO.trans h1.1 h2.1, fun x'' h => by + obtain ⟨x', e', a⟩ := h2.2 x'' h + obtain ⟨x, e, b⟩ := h1.2 x' e' + exact ⟨x, e, a.trans b⟩⟩ + +theorem setNtfnRef_nkA (n p : ObjRef) (f : Notification → Notification) (hf : ∀ x, (f x).ntfnObj = x.ntfnObj) + {σ : State Unit} {r : Unit × State Unit} + (hr : r ∈ ((getSimpleKo (Z := Unit) KernelObject.notification p >>= fun x => + setSimpleKo KernelObject.notification p (f x)) σ).1) : NKeepA n σ r.2 := by + obtain ⟨y, hy, hr⟩ := mem_bindA.1 hr + have t1 := getSimpleKo_tpres KernelObject.notification p σ y hy + have t2 := setSimpleKo_tpres KernelObject.notification nsko_ntfn p (f y.1) y.2 r hr + have g : y.2 = σ ∧ σ.kheap p = some (.notification y.1) := (getNotification_mem hy).symm + refine ⟨PreO.trans t1 t2, fun x' h' => ?_⟩ + unfold setSimpleKo at hr + obtain ⟨o, ho, hr⟩ := mem_bindA.1 hr + obtain ⟨e1, -⟩ := getObject_mem ho + obtain ⟨u, hu, hr⟩ := mem_bindA.1 hr + obtain ⟨-, hu⟩ := hu + rw [hu] at hr + obtain ⟨v, hv, hr⟩ := mem_bindA.1 hr + obtain ⟨-, hv⟩ := hv + rw [hv] at hr + obtain ⟨ko, -, e⟩ := setObject_memA hr + rw [e, e1, g.1] at h' + by_cases hn : n = p + · subst hn + dsimp only at h' + rw [Function.update_self] at h' + cases h' + exact ⟨y.1, g.2, hf _⟩ + · dsimp only at h' + rw [Function.update_of_ne hn] at h' + exact ⟨x', h', rfl⟩ + +theorem setBound_nkA (n t : ObjRef) : PresR (NKeepA n) (setTcbObjRef (Z := Unit) tcbBoundNotificationUpdate t none) := by + intro σ r hr + obtain ⟨x, hx, e⟩ := threadSet_memA _ t hr + refine ⟨?_, fun x' h' => ?_⟩ + · rw [e] + refine ⟨rfl, rfl, rfl, rfl, rfl, fun q y' hq => ?_, fun q y hq => ?_, ⟨fun q y k hq => ?_, rfl⟩⟩ + · by_cases h : q = t + · subst h + dsimp only at hq + rw [Function.update_self] at hq + cases hq + exact ⟨x, hx, rfl, rfl⟩ + · dsimp only at hq + rw [Function.update_of_ne h] at hq + exact ⟨y', hq, rfl, rfl⟩ + · by_cases h : q = t + · subst h + rw [hx] at hq + cases hq + exact ⟨_, Function.update_self .., rfl⟩ + · exact ⟨y, by dsimp only; rw [Function.update_of_ne h]; exact hq, rfl⟩ + · have h : q ≠ t := fun h => by rw [h, hx] at hq; cases hq + exact ⟨y, by dsimp only; rw [Function.update_of_ne h]; exact hq, rfl⟩ + · rw [e] at h' + have h : n ≠ t := fun h => by + dsimp only at h' + rw [h, Function.update_self] at h' + cases h' + dsimp only at h' + rw [Function.update_of_ne h] at h' + exact ⟨x', h', rfl⟩ + +theorem setScNtfn_nkA (n p : ObjRef) : PresR (NKeepA n) (setScObjRef (Z := Unit) scNtfnUpdate p none) := by + intro σ r hr + obtain ⟨o, ho, hr⟩ := mem_bindA.1 hr + obtain ⟨e1, e2⟩ := getObject_mem ho + obtain ⟨ob, os⟩ := o + cases ob with + | schedContext sc k => + obtain ⟨ko, -, e⟩ := setObject_memA hr + dsimp only at e1 e2 + rw [e1] at e + refine ⟨?_, fun x' h' => ?_⟩ + · rw [e] + refine ⟨rfl, rfl, rfl, rfl, rfl, fun q y' hq => ?_, fun q y hq => ?_, ⟨fun q y j hq => ?_, rfl⟩⟩ + · have h : q ≠ p := fun h => by + dsimp only at hq + rw [h, Function.update_self] at hq + cases hq + dsimp only at hq + rw [Function.update_of_ne h] at hq + exact ⟨y', hq, rfl, rfl⟩ + · have h : q ≠ p := fun h => by rw [h, e2] at hq; cases hq + exact ⟨y, by dsimp only; rw [Function.update_of_ne h]; exact hq, rfl⟩ + · by_cases h : q = p + · subst h + rw [e2] at hq + cases hq + exact ⟨_, Function.update_self .., rfl⟩ + · exact ⟨y, by dsimp only; rw [Function.update_of_ne h]; exact hq, rfl⟩ + · rw [e] at h' + have h : n ≠ p := fun h => by + dsimp only at h' + rw [h, Function.update_self] at h' + cases h' + dsimp only at h' + rw [Function.update_of_ne h] at h' + exact ⟨x', h', rfl⟩ + | _ => exact absurd hr (Set.notMem_empty _) + +theorem unbinds_nkA (n : ObjRef) : PresR (NKeepA n) (schedContextMaybeUnbindNtfn (Z := Unit) n >>= fun _ => + unbindMaybeNotification n) := by + have ro : ∀ (g : Notification → Option ObjRef), + PresR (NKeepA n) (getNtfnObjRef (Z := Unit) g n) := fun g σ r hr => by + obtain ⟨y, hy, hr⟩ := mem_bindA.1 hr + have e : y.2 = σ := (getNotification_mem hy).2 + have er : r = (g y.1, y.2) := hr + rw [er] + dsimp only + rw [e] + exact PreO.refl σ + refine PresR.bind ?_ fun _ => ?_ + · unfold schedContextMaybeUnbindNtfn + refine PresR.bind (ro _) fun o => ?_ + cases o with + | none => exact PresR.pure _ + | some sc => + exact PresR.bind (fun σ r hr => setNtfnRef_nkA n n (ntfnScUpdate fun _ => none) (fun _ => rfl) hr) fun _ => setScNtfn_nkA n sc + · unfold unbindMaybeNotification + refine PresR.bind (ro _) fun o => ?_ + cases o with + | none => exact PresR.pure _ + | some t => + exact PresR.bind (fun σ r hr => setNtfnRef_nkA n n (ntfnBoundTcbUpdate fun _ => none) (fun _ => rfl) hr) fun _ => setBound_nkA n t + +/-! ## The time facts -/ + +instance : PreO TmA := ⟨tmA_refl, tmA_trans⟩ + +/-- (Task 0 merge) **A step writing only the SC `p`** and keeping the time, the current SC, the consumed time and the +release queue; a TCB at `p` afterwards was one before. -/ +def WpK (p : ObjRef) (σ σ' : State Unit) : Prop := + σ'.curTime = σ.curTime ∧ σ'.machineState.lastMachineTime = σ.machineState.lastMachineTime ∧ + σ'.machineState.timeState = σ.machineState.timeState ∧ σ'.curSc = σ.curSc ∧ + σ'.consumedTime = σ.consumedTime ∧ σ'.releaseQueue = σ.releaseQueue ∧ + (∀ q, q ≠ p → σ'.kheap q = σ.kheap q) ∧ (∀ x, σ'.kheap p = some (.tcb x) → σ.kheap p = some (.tcb x)) + +instance (p : ObjRef) : PreO (WpK p) := + ⟨fun _ => ⟨rfl, rfl, rfl, rfl, rfl, rfl, fun _ _ => rfl, fun _ h => h⟩, fun a b => + ⟨b.1.trans a.1, b.2.1.trans a.2.1, b.2.2.1.trans a.2.2.1, b.2.2.2.1.trans a.2.2.2.1, + b.2.2.2.2.1.trans a.2.2.2.2.1, b.2.2.2.2.2.1.trans a.2.2.2.2.2.1, + fun q hq => (b.2.2.2.2.2.2.1 q hq).trans (a.2.2.2.2.2.2.1 q hq), + fun x h => a.2.2.2.2.2.2.2 x (b.2.2.2.2.2.2.2 x h)⟩⟩ + +theorem updateSchedContext_wpA (p : ObjRef) (f : SchedContext → SchedContext) : + PresR (WpK p) (updateSchedContext (Z := Unit) p f) := by + intro σ r hr + obtain ⟨y, n, -, e⟩ := updateSchedContext_memA p f hr + rw [e] + refine ⟨rfl, rfl, rfl, rfl, rfl, rfl, fun q hq => Function.update_of_ne hq _ _, fun x h => ?_⟩ + change Function.update σ.kheap p _ p = _ at h + rw [Function.update_self] at h + cases h + +macro "wpk" : tactic => `(tactic| + repeat' (first + | exact PresR.pure _ + | exact PresR.gets _ + | exact PresR.getsThe _ + | exact PresR.assertOpt _ + | exact PresR.assert _ + | exact PresR.fail + | exact updateSchedContext_wpA _ _ + | exact PresR.modify _ (fun _ => ⟨rfl, rfl, rfl, rfl, rfl, rfl, fun _ _ => rfl, fun _ h => h⟩) + | refine PresR.bind_pure _ _ ?_ + | refine PresR.bind ?_ (fun _ => ?_) + | refine PresR.when ?_ + | refine PresR.whileLoop (fun _ => ?_) _ + | unfold refillUnblockCheck + | unfold refillHeadOverlappingLoop + | unfold mergeOverlappingRefills + | unfold getScRefillReady + | unfold updateRefillHd + | unfold updateRefillTl + | unfold refillAddTail + | unfold refillPopHead + | unfold getRefills + | unfold getRefillHead + | unfold getRefillTail + | unfold refillSingle + | unfold refillFull + | unfold isRoundRobin + | unfold getSchedContext + | unfold getObject + | dsimp only + | split)) + +theorem refillUnblockCheck_wpA (p : ObjRef) : PresR (WpK p) (refillUnblockCheck (Z := Unit) p) := by + wpk + +/-- A `WpK p` step from a state whose current SC is not `p` keeps the time facts. -/ +theorem WpK.tmA {p : ObjRef} {σ σ' : State Unit} (h : WpK p σ σ') (hp : p ≠ σ.curSc) : TmA σ σ' := by + obtain ⟨h1, h2, h3, h4, h5, h6, h7, h8⟩ := h + refine ⟨h1, h2, h3, ⟨h4, h5, fun t ht => h6 ▸ ht, fun y k hy a => ⟨y, k, ?_, a, rfl⟩, fun t x ht e _ => ?_⟩⟩ + · rw [← h7 σ.curSc (Ne.symm hp)]; exact hy + · by_cases e' : t = p + · subst e'; exact ⟨x, h8 x ht, e⟩ + · exact ⟨x, by rw [← h7 t e']; exact ht, e⟩ + +/-- (Task 0 merge) **The conditional refill-unblock check keeps the time facts when it is guarded by a current-SC +test** (`asst` is `some _`): it only writes an SC other than the current one. -/ +theorem ifCondRefillUnblockCheck_tmA (o : Option ObjRef) (a b : Option Bool) (hb : b ≠ none) : + NdTmA (ifCondRefillUnblockCheck (Z := Unit) o a b) := by + cases o with + | none => exact NdTmA.pure _ + | some scp => + intro σ r hr + obtain ⟨sc, -, hr⟩ := eqPres_mem (getSchedContext_eqPres scp) hr + obtain ⟨cp, hcp, hr⟩ := eqPres_mem (PresR.gets (fun s : State Unit => s.curSc)) hr + have ecp : cp = σ.curSc := congrArg Prod.fst (show (cp, σ) = (σ.curSc, σ) from hcp) + obtain ⟨g, -, hr⟩ := eqPres_mem (PresR.pure _ : PresR (@Eq (State Unit)) (pure _)) hr + unfold NonDet.when at hr + split_ifs at hr with h1 h2 + · have hne : scp ≠ cp := by + cases b with + | none => exact absurd rfl hb + | some bb => + cases bb + · exact h1.2 rfl + · exact h2 rfl + exact (refillUnblockCheck_wpA scp σ r hr).tmA (ecp ▸ hne) + all_goals + have e : r = ((), σ) := hr + rw [e]; exact tmA_refl σ + +theorem fl4FinaliseCap_ep_tmA (r : ObjRef) (b : Badge) (rt : CapRights) (d : Bool) (cap : Cap) + (h : cap = .endpointCap r b rt) : NdTmA (fl4FinaliseCap (Z := Unit) cap d) := by + subst h + rw [fl4FinaliseCap_plainA (by intro h; exact h)] + unfold finaliseCap + refine NdTmA.bind (NdTmA.when ?_) fun _ => NdTmA.npure _ + unfold cancelAllIpc removeAndRestartEpQueuedThread restartThreadIfNoFault possibleSwitchTo + repeat' first | exact ifCondRefillUnblockCheck_tmA _ _ _ (by simp) | tmstep + +theorem fl4FinaliseCap_ntfn_tmA (r : ObjRef) (b : Badge) (rt : CapRights) (d : Bool) (cap : Cap) + (h : cap = .notificationCap r b rt) : NdTmA (fl4FinaliseCap (Z := Unit) cap d) := by + subst h + rw [fl4FinaliseCap_plainA (by intro h; exact h)] + unfold finaliseCap + refine NdTmA.bind (NdTmA.when ?_) fun _ => NdTmA.npure _ + unfold schedContextMaybeUnbindNtfn unbindMaybeNotification doUnbindNtfnSc cancelAllSignals + removeAndRestartNtfnQueuedThread possibleSwitchTo getNtfnObjRef + repeat' first | exact ifCondRefillUnblockCheck_tmA _ _ _ (by simp) | tmstep + +section +variable {m : Fl4SpecA64.McState} +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Pp Tb : Set U64} {Lv : U64 → Nat} {ia ie : Nat} + {N E Rp : Set U64} {κ : U64 → U64} {n : Nat} {ipa : U64} {Clock : A → MachineState → Prop} {s0 : S} + +/-- **The Endpoint hook at `DelWTopA`**. -/ +theorem finHookEpSvA (Da : DelArchA st ar AR Clock) (s0 : S) + (Rv : S → A → State Unit → MdbGhostA → U64 → Prop) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (h1 : fl4A64Platform.numNodes = 1) : + FinHookKA st .endpoint ar Z.th OS fl4FinaliseCap + (DelWTopGA st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar ipa Clock s0) + (DelWxTopGA st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m Clock s0) (DelRdXA st ar Z H OS ipa Rv) (DelRzA st) := by + have cap : ∀ (c : capability.Cap) (ac : Cap), finKindA c = some .endpoint → CapEqA c ac → + ∃ p b0 b1 b2 b3 b4, c = .Endpoint p b0 b1 b2 b3 b4 ∧ ac = absCapA c := by + intro c ac hk hac + cases c <;> simp [finKindA] at hk + exact ⟨_, _, _, _, _, _, rfl, hac.symm⟩ + refine FinHookKA.topA (FinHookKA.timeA (finHookEpA s0 Rv hCs hmb hmax htu h1) ?_ Da.clockFr ?_) ?_ + · intro c ac d σ x σ' hck hac hmem + obtain ⟨p, b0, b1, b2, b3, b4, rfl, rfl⟩ := cap c ac hck hac + exact fl4FinaliseCap_ep_tmA _ _ _ d _ rfl σ (x, σ') hmem + · intro s a σ G sl c ac f D hW hsl hc hk hg hac hf hCl + obtain ⟨p, b0, b1, b2, b3, b4, rfl, rfl⟩ := cap c ac hk hac + obtain ⟨-, ⟨Q, R, hw, Ac, Bf, stack, J, -⟩, -⟩ := hW + have hH : H p = some .ep := (J.capTy _ (D.mem_slot hsl)).1 p b0 b1 b2 b3 b4 hc + rw [fin_cap_eq_obj (by simp)] + cases f + · have e : objects.finalise_cap_object st ar s a (.Endpoint p b0 b1 b2 b3 b4) false = + ok ((capability.Cap.Null, capability.Cap.Null), s, a) := rfl + rw [e, WP.spec_ok] + exact hCl + · refine WP.spec_mono (J.epFin_recA (b0 := b0) (b1 := b1) (b2 := b2) (b3 := b3) (b4 := b4) hH hCs hmb hmax htu + (fun _ _ t ht _ => J.onCore_single hCs h1 ht)) ?_ + rintro r ⟨-, ha, -⟩ + rw [ha] + exact hCl + · intro s a σ G sl c ac D hW hsl hc hk hac d x σ' hd hmem + obtain ⟨p, b0, b1, b2, b3, b4, rfl, rfl⟩ := cap c ac hk hac + obtain ⟨⟨-, ⟨⟨Q, R, hw, Ac, Bf, stack, J, -⟩, -⟩, sv⟩, -⟩ := hW + have hp : p ∈ E := J.ipcHeapCov.ep p ((J.capTy _ (D.mem_slot hsl)).1 p b0 b1 b2 b3 b4 hc) + simp only [absCapA] at hmem + rw [fl4FinaliseCap_plainA (by intro h; exact h)] at hmem + obtain ⟨y, hy, hr⟩ := mem_bindA.1 hmem + have e : σ' = y.2 := congrArg Prod.snd (show (x, σ') = (_, y.2) from hr) + rw [e] + cases d + · have ey : y = ((), σ) := hy + rw [ey]; exact sv + · refine cancelAllIpc_schedValid (specPtr p) σ ⟨sv, fun z hz => ?_⟩ y hy + obtain ⟨z', hz', rel⟩ := J.ep.ep p hp + have ez : z' = z := KernelObject.endpoint.inj (Option.some.inj (hz'.symm.trans hz)) + rw [ez] at rel + have blk : ∀ (xs : List U64) (k : Nat), (k = 3 ∨ k = 4) → (∀ t ∈ xs, t ∈ Z.T) → + (∀ t ∈ xs, QueuedOn (L.mem s) p k t) → BAs (xs.map specPtr) σ := by + intro xs k hk' hT hq + refine bas_of_codeA sv fun t ht x hx => ?_ + obtain ⟨x', hx', R'⟩ := J.ep.tcb t (hT t ht) + obtain rfl : x' = x := KernelObject.tcb.inj (Option.some.inj (hx'.symm.trans hx)) + refine blocked_of_codeA R'.st ?_ + rw [(hq t ht).1] + omega + cases z with + | idleEP => exact fun u hu => by cases hu + | sendEP q => + obtain ⟨-, xs, -, rfl, hq, hall⟩ := rel + exact blk xs 4 (Or.inr rfl) hq.mem hall + | recvEP q => + obtain ⟨-, xs, -, rfl, hq, hall⟩ := rel + exact blk xs 3 (Or.inl rfl) hq.mem hall + +/-- **The Notification hook at `DelWTopA`**. -/ +theorem finHookNtfnSvA (Da : DelArchA st ar AR Clock) (s0 : S) + (Rv : S → A → State Unit → MdbGhostA → U64 → Prop) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (h1 : fl4A64Platform.numNodes = 1) : + FinHookKA st .notification ar Z.th OS fl4FinaliseCap + (DelWTopGA st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar ipa Clock s0) + (DelWxTopGA st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m Clock s0) (DelRdXA st ar Z H OS ipa Rv) (DelRzA st) := by + have cap : ∀ (c : capability.Cap) (ac : Cap), finKindA c = some .notification → CapEqA c ac → + ∃ p b0 b1 b2, c = .Notification p b0 b1 b2 ∧ ac = absCapA c := by + intro c ac hk hac + cases c <;> simp [finKindA] at hk + exact ⟨_, _, _, _, rfl, hac.symm⟩ + refine FinHookKA.topA (FinHookKA.timeA (finHookNtfnA s0 Rv hCs hmb hmax htu h1) ?_ Da.clockFr ?_) ?_ + · intro c ac d σ x σ' hck hac hmem + obtain ⟨p, b0, b1, b2, rfl, rfl⟩ := cap c ac hck hac + exact fl4FinaliseCap_ntfn_tmA _ _ _ d _ rfl σ (x, σ') hmem + · intro s a σ G sl c ac f D hW hsl hc hk hg hac hf hCl + obtain ⟨p, b0, b1, b2, rfl, rfl⟩ := cap c ac hk hac + obtain ⟨-, ⟨Q, R, hw, Ac, Bf, stack, J, -⟩, -⟩ := hW + have hH : H p = some .ntfn := (J.capTy _ (D.mem_slot hsl)).2.1 p b0 b1 b2 hc + rw [fin_cap_eq_obj (by simp)] + cases f + · have e : objects.finalise_cap_object st ar s a (.Notification p b0 b1 b2) false = + ok ((capability.Cap.Null, capability.Cap.Null), s, a) := rfl + rw [e, WP.spec_ok] + exact hCl + · refine WP.spec_mono (J.ntfnFin_recA (b0 := b0) (b1 := b1) (b2 := b2) hH hCs hmb hmax htu + (fun _ _ t ht _ => J.onCore_single hCs h1 ht)) ?_ + rintro r ⟨-, ha, -⟩ + rw [ha] + exact hCl + · intro s a σ G sl c ac D hW hsl hc hk hac d x σ' hd hmem + obtain ⟨p, b0, b1, b2, rfl, rfl⟩ := cap c ac hk hac + obtain ⟨⟨-, ⟨⟨Q, R, hw, Ac, Bf, stack, J, -⟩, -⟩, sv⟩, -⟩ := hW + have hp : p ∈ N := J.ipcHeapCov.ntfn p ((J.capTy _ (D.mem_slot hsl)).2.1 p b0 b1 b2 hc) + simp only [absCapA] at hmem + rw [fl4FinaliseCap_plainA (by intro h; exact h)] at hmem + obtain ⟨y, hy, hr⟩ := mem_bindA.1 hmem + have e : σ' = y.2 := congrArg Prod.snd (show (x, σ') = (_, y.2) from hr) + rw [e] + cases d + · have ey : y = ((), σ) := hy + rw [ey]; exact sv + · have hy' : y ∈ (((schedContextMaybeUnbindNtfn (Z := Unit) (specPtr p) >>= fun _ => + unbindMaybeNotification (specPtr p)) >>= fun _ => cancelAllSignals (specPtr p)) σ).1 := by + obtain ⟨u, hu, hy⟩ := mem_bindA.1 hy + obtain ⟨v, hv, hy⟩ := mem_bindA.1 hy + exact mem_bindA.2 ⟨v, mem_bindA.2 ⟨u, hu, hv⟩, hy⟩ + obtain ⟨u, hu, hc3⟩ := mem_bindA.1 hy' + obtain ⟨tk, ko⟩ := unbinds_nkA (specPtr p) σ u hu + have sv2 : SchedValidA u.2 := ((FIA.ofValid sv).ofTKeep tk).toValid + refine cancelAllSignals_schedValid (specPtr p) u.2 ⟨sv2, fun z q hz hq => ?_⟩ y hc3 + obtain ⟨z0, hz0, hobj⟩ := ko z hz + obtain ⟨z', hz', rel⟩ := J.ntfn.ntfn p hp + have ez : z' = z0 := KernelObject.notification.inj (Option.some.inj (hz'.symm.trans hz0)) + rw [ez] at rel + have ro := rel.obj + rw [← hobj, hq] at ro + obtain ⟨-, xs, -, rfl, hq', hall⟩ := ro + refine BAs.ofTKeep (bas_of_codeA sv fun t ht x hx => ?_) tk + obtain ⟨x', hx', R'⟩ := J.ntfn.tcb t (hq'.mem t ht) + obtain rfl : x' = x := KernelObject.tcb.inj (Option.some.inj (hx'.symm.trans hx)) + exact blocked_of_codeA R'.st (Or.inr (Or.inr (hall t ht).1)) + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3DelSvLiftA.lean b/proofs/Fl4ProofsA64/P3/Stage3DelSvLiftA.lean new file mode 100644 index 000000000..bb3ef6845 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3DelSvLiftA.lean @@ -0,0 +1,224 @@ +/- +P3 (AArch64, delete hooks, contract §87): **the lift to `DelWTopA` / `DelWxTopA`** (`DelXA6` loop hooks carrying +`SchedValidA`). + +* **`FinHookKA.topA`**: a finaliser hook at `DelJWQA (DelXA6 …) ∧ DelPpTimeA` is one at `DelWTopA` / `DelWxTopA`, + given that the spec finaliser keeps `SchedValidA` on that kind. +* **`delWx_topA`** (`wx`), **`swapHyp_topA`**, **`zombieHyp_topA`**, **`emptyHyp_topA`**, **`delPp_topA`**, **`postDel_topA`**: the other fields of `DelHypsA` (`triv` is `rdTrivHyp_delRdA` at any `W`). +* **`finHookFrameSvA`** … **`finHookPoolSvA`**: the six VSpace hooks at `DelWTopA` (`SchedValidA` by + `fl4FinaliseCap_akA`). + +No named premise of its own. New module. +-/ +import Fl4ProofsA64.P3.Stage3DelTopA +import Fl4ProofsA64.P3.Stage3DelSvAkA +import Fl4ProofsA64.P3.DelClockA +import Fl4ProofsA64.P3.DelIrqPostA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +section +variable {m : Fl4SpecA64.McState} +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {κ : U64 → U64} {n : Nat} {ar : machine.Arch A} {ipa : U64} + {Clock : A → MachineState → Prop} {s0 : S} {Rd Rz : S → A → State Unit → MdbGhostA → U64 → Prop} + +/-- **A finaliser hook lifted to `DelWTopA`** (module docstring). -/ +theorem FinHookKA.topA {k : FinKindA} + (H' : FinHookKA st k ar Z.th OS fl4FinaliseCap + (fun s a σ G => DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) + (J7LA st (J7R2A st ar ipa))) s a σ G ∧ DelPpTimeA Clock a σ) + (fun s a σ G => DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) + (J7LA st J7R2tA)) s a σ G ∧ DelPpTimeA Clock a σ) Rd Rz) + (hS : ∀ (s : S) (a : A) (σ : State Unit) (G : MdbGhostA) (sl : CslotPtr) (c : capability.Cap) (ac : Cap), + DStateA st Z.th OS s σ G → DelWTopGA st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar ipa Clock s0 s a σ G → + AbsSlotA Z.th sl → capA st s (cslotAddrA sl) = c → finKindA c = some k → CapEqA c ac → + ∀ (d : Bool) (x : Cap × Cap) (σ' : State Unit), (d = true ↔ isFinalCap' ac σ) → + (x, σ') ∈ (fl4FinaliseCap (Z := Unit) ac d σ).1 → SchedValidA σ') : + FinHookKA st k ar Z.th OS fl4FinaliseCap + (DelWTopGA st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar ipa Clock s0) + (DelWxTopGA st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m Clock s0) Rd Rz := by + intro s a σ G sl c ac f D hW hsl hc hk hg hac hf + have hW0 := hW + obtain ⟨⟨sc, X, sv⟩, T⟩ := hW0 + refine WP.spec_mono (H' s a σ G sl c ac f D ⟨⟨sc, X⟩, T⟩ hsl hc hk hg hac hf) ?_ + rintro r ⟨h1, h2, h3, σ', G', ⟨d, hd, hmem⟩, D', hc', hoth, ⟨hWn, hWx⟩, hRd, hRz⟩ + have sv' := hS s a σ G sl c ac D hW hsl hc hk hac d _ σ' hd hmem + refine ⟨h1, h2, h3, σ', G', ⟨d, hd, hmem⟩, D', hc', hoth, ⟨fun h => ?_, fun h => ?_⟩, hRd, hRz⟩ + · obtain ⟨⟨sc', X'⟩, T'⟩ := hWn h + exact ⟨⟨sc', X', sv'⟩, T'⟩ + · obtain ⟨⟨sc', X'⟩, T'⟩ := hWx h + exact ⟨⟨sc', X', sv'⟩, T'⟩ + +/-- **`wx`**: the loop's invariant gives the window's. -/ +theorem delWx_topA (s : S) (a : A) (σ : State Unit) (G : MdbGhostA) + (h : DelWTopGA st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar ipa Clock s0 s a σ G) : + DelWxTopGA st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m Clock s0 s a σ G := by + obtain ⟨⟨sc, ⟨⟨Q, R, hw, Ac, Bf, stack, J, X⟩, rest⟩, sv⟩, T⟩ := h + exact ⟨⟨sc, ⟨⟨Q, R, hw, Ac, Bf, stack, J.mapJ7A fun _ _ => trivial, X⟩, rest⟩, sv⟩, T⟩ + +/-- **`SwapHypA` at `DelWTopA`**. -/ +theorem swapHyp_topA (F : DelFootA st Z H.tcbs H.omega H.frame OS s0) : + SwapHypA st H.tcbs Z.th OS (DelWTopGA st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar ipa Clock s0) := by + intro s s' a σ σ' G sl p zt k hW h1 D D' w HS hm hA hz hne hcap M + obtain ⟨⟨sc, X, sv⟩, T⟩ := hW + have r := swapHyp_timeA (Clock := Clock) (swapHyp_delA (ipa := ipa) F) s s' a σ σ' G sl p zt k ⟨⟨sc, X⟩, T⟩ h1 D + D' w HS hm hA hz hne hcap M + exact ⟨⟨r.1.1, r.1.2, sv.ofSlot HS⟩, r.2⟩ + +/-- **`ZombieHypA` at `DelWTopA`**. -/ +theorem zombieHyp_topA (F : DelFootA st Z H.tcbs H.omega H.frame OS s0) : + ZombieHypA st H.tcbs Z.th OS (DelWTopGA st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar ipa Clock s0) + (DelRzA st) := by + intro s s' a σ σ' G sl p zt k hW hRz hZ hNZ D D' w HS hset hA hne hz hcap M + obtain ⟨⟨sc, X, sv⟩, T⟩ := hW + have r := zombieHyp_timeA (Clock := Clock) (zombieHyp_delA (ipa := ipa) F) s s' a σ σ' G sl p zt k ⟨⟨sc, X⟩, T⟩ + hRz hZ hNZ D D' w HS hset hA hne hz hcap M + exact ⟨⟨r.1.1, r.1.2, sv.ofSlot HS⟩, r.2⟩ + +/-- **`EmptyHypA` at `DelWTopA` / `DelWxTopA`**. -/ +theorem emptyHyp_topA {Rv : S → A → State Unit → MdbGhostA → U64 → Prop} + (F : DelFootA st Z H.tcbs H.omega H.frame OS s0) : + EmptyHypA st H.tcbs Z.th OS (DelWTopGA st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar ipa Clock s0) + (DelWxTopGA st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m Clock s0) (DelRdXA st ar Z H OS ipa (RvVcA st OS Rv)) := by + intro s s' a σ σ' G sl hW hRd D D' w HS hm hA hne h0 hcap M + obtain ⟨⟨sc, X, sv⟩, T⟩ := hW + have r := emptyHyp_timeA (Clock := Clock) (emptyHyp_delA (ipa := ipa) (Rv := Rv) F) s s' a σ σ' G sl ⟨⟨sc, X⟩, T⟩ + hRd D D' w HS hm hA hne h0 hcap M + exact ⟨⟨r.1.1, r.1.2, sv.ofSlot HS⟩, r.2⟩ + +/-- **`PPHypDA` at `DelWTopA`**, from the one at `DelXA6` (`delPp_hookA`). -/ +theorem delPp_topA + (h : PPHypDA st ar Z.th OS + (fun s a σ G => DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) + (J7LA st (J7R2A st ar ipa))) s a σ G ∧ DelPpTimeA Clock a σ) + (fun s a σ G => DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) + (J7LA st (J7R2A st ar ipa))) s a σ G ∧ DelPpTimeA Clock a σ)) : + PPHypDA st ar Z.th OS (DelWTopGA st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar ipa Clock s0) + (DelWTopGA st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar ipa Clock s0) := by + intro s a σ G D hW + obtain ⟨⟨sc, X, sv⟩, T⟩ := hW + refine WP.spec_mono (h s a σ G D ⟨⟨sc, X⟩, T⟩) ?_ + rintro r ⟨hc, σ', D', hr⟩ + refine ⟨hc, σ', D', ?_⟩ + rcases hr with ⟨e, hm, ⟨sc', X'⟩, T'⟩ | ⟨e, hm, ⟨sc', X'⟩, T'⟩ + · exact Or.inl ⟨e, hm, ⟨sc', X', sv.ofAk (preemptionPoint_akA σ _ hm)⟩, T'⟩ + · exact Or.inr ⟨e, hm, ⟨sc', X', sv.ofAk (preemptionPoint_akA σ _ hm)⟩, T'⟩ + +/-- **`PostDelHypA` at `DelWTopA`**, from the one at `DelXA6` (`postDelHookA`). -/ +theorem postDel_topA + (h : PostDelHypA st ar Z.th OS + (fun s a σ G => DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) + (J7LA st (J7R2A st ar ipa))) s a σ G ∧ DelPpTimeA Clock a σ)) : + PostDelHypA st ar Z.th OS (DelWTopGA st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar ipa Clock s0) := by + intro s a σ G cl D hW hcl hne hwf hno + obtain ⟨⟨sc, X, sv⟩, T⟩ := hW + refine WP.spec_mono (h s a σ G cl D ⟨⟨sc, X⟩, T⟩ hcl hne hwf hno) ?_ + rintro r ⟨hc, σ', hm, D', ⟨sc', X'⟩, T'⟩ + obtain ⟨i, rfl⟩ : ∃ i, cl = .IrqHandler i := by + cases cl <;> simp [CleanCapA] at hcl hne + exact ⟨_, rfl⟩ + exact ⟨hc, σ', hm, D', ⟨sc', X', sv.ofAk (postCapDeletionIrq_akA _ σ _ hm)⟩, T'⟩ + +/-! ## The VSpace hooks -/ + +/-- The spec finaliser keeps `SchedValidA` on the six VSpace kinds. -/ +theorem finSv_vsA {k : FinKindA} + (hk : k = .frame ∨ k = .pageTable ∨ k = .pml4 ∨ k = .highVSpace ∨ k = .ept ∨ k = .asidPool) + {c : capability.Cap} {ac : Cap} {d : Bool} {σ σ' : State Unit} {x : Cap × Cap} (hck : finKindA c = some k) + (hac : CapEqA c ac) (sv : SchedValidA σ) (hmem : (x, σ') ∈ (fl4FinaliseCap (Z := Unit) ac d σ).1) : + SchedValidA σ' := by + have hA : ∃ y, ac = .archObjectCap y ∧ ∀ v, y ≠ .vcpuCap v := by + rcases hac.eq_or_pt with e | ⟨p', a', v', e⟩ + · subst e + rcases hk with rfl | rfl | rfl | rfl | rfl | rfl <;> cases c <;> simp [finKindA] at hck <;> + first + | exact ⟨_, rfl, fun v h => by cases h⟩ + | exact ⟨_, rfl, fun v h => by unfold absFrameE at h; split at h <;> cases h⟩ + · exact ⟨_, e, fun v h => by cases h⟩ + obtain ⟨y, rfl, hy⟩ := hA + exact sv.ofAk (fl4FinaliseCap_akA y hy d σ (x, σ') hmem) + +/-- **The frame hook at `DelWTopA`**. -/ +theorem finHookFrameSvA [PlainStore st] (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) (K : ArchKeepsA ar AR) + (Da : DelArchA st ar AR Clock) (hIpa : ∀ a, S2IpaE ar a) (s0 : S) + (hT : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s0)) + (hbf : DelBufOffA st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n (J5A st) (J7LA st (J7R2A st ar hPl.ipa))) + (Rv : S → A → State Unit → MdbGhostA → U64 → Prop) : + FinHookKA st .frame ar Z.th OS fl4FinaliseCap + (DelWTopGA st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar hPl.ipa Clock s0) + (DelWxTopGA st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m Clock s0) (DelRdXA st ar Z H OS hPl.ipa Rv) (DelRzA st) := + FinHookKA.topA (finHookFrameTA hA hPl K Da hIpa s0 hT hbf Rv) + fun _ _ _ _ _ _ _ _ W _ _ hck hac _ _ _ _ hm => finSv_vsA (by simp) hck hac W.1.2.2 hm + +/-- **The pageTable hook at `DelWTopA`**. -/ +theorem finHookPtSvA (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) (K : ArchKeepsA ar AR) + (Da : DelArchA st ar AR Clock) (hIpa : ∀ a, S2IpaE ar a) (s0 : S) + (hT : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s0)) + (hbf : DelBufOffA st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n (J5A st) (J7LA st (J7R2A st ar hPl.ipa))) + (Rv : S → A → State Unit → MdbGhostA → U64 → Prop) : + FinHookKA st .pageTable ar Z.th OS fl4FinaliseCap + (DelWTopGA st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar hPl.ipa Clock s0) + (DelWxTopGA st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m Clock s0) (DelRdXA st ar Z H OS hPl.ipa Rv) (DelRzA st) := + FinHookKA.topA (finHookPtTA hA hPl K Da hIpa s0 hT hbf Rv) + fun _ _ _ _ _ _ _ _ W _ _ hck hac _ _ _ _ hm => finSv_vsA (by simp) hck hac W.1.2.2 hm + +/-- **The pml4 hook at `DelWTopA`**. -/ +theorem finHookPml4SvA (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) (K : ArchKeepsA ar AR) + (Da : DelArchA st ar AR Clock) (s0 : S) + (hT : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s0)) + (hbf : DelBufOffA st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n (J5A st) (J7LA st (J7R2A st ar hPl.ipa))) + (Rv : S → A → State Unit → MdbGhostA → U64 → Prop) : + FinHookKA st .pml4 ar Z.th OS fl4FinaliseCap + (DelWTopGA st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar hPl.ipa Clock s0) + (DelWxTopGA st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m Clock s0) (DelRdXA st ar Z H OS hPl.ipa Rv) (DelRzA st) := + FinHookKA.topA (finHookPml4TA hA hPl K Da s0 hT hbf Rv) + fun _ _ _ _ _ _ _ _ W _ _ hck hac _ _ _ _ hm => finSv_vsA (by simp) hck hac W.1.2.2 hm + +/-- **The highVSpace hook at `DelWTopA`**. -/ +theorem finHookHighSvA (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) (K : ArchKeepsA ar AR) + (Da : DelArchA st ar AR Clock) (s0 : S) + (hT : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s0)) + (hbf : DelBufOffA st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n (J5A st) (J7LA st (J7R2A st ar hPl.ipa))) + (Rv : S → A → State Unit → MdbGhostA → U64 → Prop) : + FinHookKA st .highVSpace ar Z.th OS fl4FinaliseCap + (DelWTopGA st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar hPl.ipa Clock s0) + (DelWxTopGA st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m Clock s0) (DelRdXA st ar Z H OS hPl.ipa Rv) (DelRzA st) := + FinHookKA.topA (finHookHighTA hA hPl K Da s0 hT hbf Rv) + fun _ _ _ _ _ _ _ _ W _ _ hck hac _ _ _ _ hm => finSv_vsA (by simp) hck hac W.1.2.2 hm + +/-- **The ept hook at `DelWTopA`**. -/ +theorem finHookEptSvA (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) (K : ArchKeepsA ar AR) + (Da : DelArchA st ar AR Clock) (s0 : S) + (hT : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s0)) + (hbf : DelBufOffA st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n (J5A st) (J7LA st (J7R2A st ar hPl.ipa))) + (Rv : S → A → State Unit → MdbGhostA → U64 → Prop) : + FinHookKA st .ept ar Z.th OS fl4FinaliseCap + (DelWTopGA st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar hPl.ipa Clock s0) + (DelWxTopGA st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m Clock s0) (DelRdXA st ar Z H OS hPl.ipa Rv) (DelRzA st) := + FinHookKA.topA (finHookEptTA hA hPl K Da s0 hT hbf Rv) + fun _ _ _ _ _ _ _ _ W _ _ hck hac _ _ _ _ hm => finSv_vsA (by simp) hck hac W.1.2.2 hm + +/-- **The asidPool hook at `DelWTopA`**. -/ +theorem finHookPoolSvA (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) (K : ArchKeepsA ar AR) + (Da : DelArchA st ar AR Clock) (s0 : S) (hT : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s0)) + (hYAs : ∀ y ∈ Z.Y, ∀ j : U64, j.val < 512 → ¬ AsW (L.ksb s0) (peAF y j)) + (Rv : S → A → State Unit → MdbGhostA → U64 → Prop) : + FinHookKA st .asidPool ar Z.th OS fl4FinaliseCap + (DelWTopGA st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar hPl.ipa Clock s0) + (DelWxTopGA st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m Clock s0) (DelRdXA st ar Z H OS hPl.ipa Rv) (DelRzA st) := + FinHookKA.topA (finHookPoolTA hA hPl K Da s0 hT hYAs Rv) + fun _ _ _ _ _ _ _ _ W _ _ hck hac _ _ _ _ hm => finSv_vsA (by simp) hck hac W.1.2.2 hm + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3DelSvReplyA.lean b/proofs/Fl4ProofsA64/P3/Stage3DelSvReplyA.lean new file mode 100644 index 000000000..90261931f --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3DelSvReplyA.lean @@ -0,0 +1,562 @@ +/- +P3 (AArch64, delete hooks, contract §88): **the Reply and IrqHandler hooks at `DelWTopA` / `DelWxTopA`**. + +Reply: the record leaves the caller blocked on reply (`replyRemove_schedValid`, with `RqDomPrio` from the scheduler +relation) or on receive (`cancelIpcRecv_fia`). IrqHandler: `capDeleteOne` of the IRQ-node slot, whose cap is `Null` or +a notification cap (the notification argument of `finHookNtfnSvA`, then `empty_slot` keeps `TKeepA`). + +* **`rqDomPrio_of_schedA`** (copy of `par/a64-p3-lift2b` `Stage3RqDomPrioA`), **`finHookReplyKA`**, + **`finHookReplySvA`**, **`ntfnFin_schedValidA`**, **`emptySlot_tkeepA`**, **`finHookIrqSvA`**. + +Named premises: the hooks' own, and [P] `DelArchA`'s `clockFr` / `clockK`. New module. +-/ +import Fl4ProofsA64.P3.Stage3DelSvIpcA +import Fl4ProofsA64.P3.Stage3RqDomPrioA +import Fl4ProofsA64.P3.Stage3SigSchedValidA +import Fl4ProofsA64.P3.ReplyHookA +import Fl4ProofsA64.P3.DelIrqHookA +import Fl4ProofsA64.P3.Stage3VsZeroA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 Classical + +noncomputable section + + +/-! ## Time facts of the Reply arm (integration round 2) + +`TmA` carries entry's `CurKeepA`, whose `tcb` field (a release-queue TCB bound to the current SC was bound to it) is +not stateless for `reply_remove`: its donation binds the caller anew. The caller is `BlockedOnReply`, hence not in the +release queue (`RlsRunA`), and the release queue only shrinks (`CurKeepA.rls`), so the arm keeps `TmA` from any state +with `RlsRunA` (`NdTmRA`). -/ + +/-- `NdTmA` from the states where `t` is not in the release queue. -/ +def NdTmXA {α : Type} (t : ObjRef) (m : NonDet (State Unit) α) : Prop := + ∀ σ r, t ∉ σ.releaseQueue → r ∈ (m σ).1 → TmA σ r.2 + +/-- `NdTmA` from the states with `RlsRunA`. -/ +def NdTmRA {α : Type} (m : NonDet (State Unit) α) : Prop := ∀ σ r, RlsRunA σ → r ∈ (m σ).1 → TmA σ r.2 + +section +variable {α β : Type} {t : ObjRef} {m : NonDet (State Unit) α} {f : α → NonDet (State Unit) β} + +theorem NdTmA.toX (h : NdTmA m) : NdTmXA t m := fun σ r _ hr => h σ r hr + +theorem NdTmA.toR (h : NdTmA m) : NdTmRA m := fun σ r _ hr => h σ r hr + +theorem NdTmXA.bind (hm : NdTmXA t m) (hf : ∀ a, NdTmXA t (f a)) : NdTmXA t (m >>= f) := fun σ r ht hr => by + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + have T1 := hm σ x ht hx + exact tmA_trans T1 (hf x.1 x.2 r (fun h => ht (T1.2.2.2.rls t h)) hr) + +theorem NdTmXA.when {P : Prop} {inst : Decidable P} {m : NonDet (State Unit) Unit} (h : NdTmXA t m) : + NdTmXA t (NonDet.when P m) := by + unfold NonDet.when; split + · exact h + · exact (NdTmA.npure _).toX + +theorem NdTmRA.bindEq (hm : PresR (@Eq (State Unit)) m) (hf : ∀ a, NdTmRA (f a)) : NdTmRA (m >>= f) := + fun σ r hR hr => by + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + have e : σ = x.2 := hm σ x hx + have T := hf x.1 x.2 r (e ▸ hR) hr + rw [← e] at T + exact T + +theorem NdTmRA.bindT (hm : NdTmRA m) (hf : ∀ a, NdTmA (f a)) : NdTmRA (m >>= f) := fun σ r hR hr => by + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + exact tmA_trans (hm σ x hR hx) (hf x.1 x.2 r hr) + +theorem NdTmRA.when {P : Prop} {inst : Decidable P} {m : NonDet (State Unit) Unit} (h : NdTmRA m) : + NdTmRA (NonDet.when P m) := by + unfold NonDet.when; split + · exact h + · exact (NdTmA.npure _).toR + +theorem NdTmRA.unless {P : Prop} {inst : Decidable P} {m : NonDet (State Unit) Unit} (h : NdTmRA m) : + NdTmRA (NonDet.unless P m) := by + unfold NonDet.unless; split + · exact (NdTmA.npure _).toR + · exact h + +end + +/-- The SC binding write of `t` itself, from a state where `t` is not in the release queue. -/ +theorem threadSetSc_tmXA (t : ObjRef) (v : Option ObjRef) : + NdTmXA t (setTcbObjRef (Z := Unit) tcbSchedContextUpdate t v) := fun σ r ht hr => by + obtain ⟨x, hx, e⟩ := threadSet_memA _ t hr + rw [e] + refine ⟨rfl, rfl, rfl, ⟨rfl, rfl, fun _ h => h, fun y k h a => ?_, fun p x1 h e' hp => ?_⟩⟩ + · dsimp only at h + by_cases e : σ.curSc = t + · rw [e, Function.update_self] at h; cases h + · rw [Function.update_of_ne e] at h; exact ⟨y, k, h, a, rfl⟩ + · dsimp only at h hp + by_cases ep : p = t + · subst ep; exact absurd hp ht + · rw [Function.update_of_ne ep] at h; exact ⟨x1, h, e'⟩ + +set_option maxHeartbeats 1600000 in +theorem replyRemove_tmXA (t r : ObjRef) : NdTmXA t (replyRemove (Z := Unit) t r) := by + unfold replyRemove schedContextDonate replyUnlinkSc getScObjRef testReschedule + repeat' first + | exact threadSetSc_tmXA _ _ + | with_reducible refine NdTmXA.bind ?_ (fun _ => ?_) + | with_reducible refine NdTmXA.when ?_ + | split + | exact NdTmA.toX (by repeat' tmstep) + +set_option maxHeartbeats 1600000 in +theorem cancelIpc_tmA (t : ObjRef) : NdTmA (cancelIpc (Z := Unit) t) := by + repeat' first | exact ifCondRefillUnblockCheck_tmA _ _ _ (by simp) | tmstep + +private theorem getThreadState_memLA {σ σ1 : State Unit} {t : ObjRef} {s0 : ThreadState} + (h : (s0, σ1) ∈ (getThreadState (Z := Unit) t σ).1) : + σ1 = σ ∧ ∃ y, σ.kheap t = some (.tcb y) ∧ y.tcbState = s0 := by + unfold getThreadState threadGet at h + obtain ⟨x, hx, h2⟩ := mem_bindA.1 h + change x ∈ (NonDet.assertOpt (getTcb t σ) σ).1 at hx + cases hg : getTcb t σ with + | none => rw [hg] at hx; exact absurd hx (Set.notMem_empty _) + | some tcb => + rw [hg] at hx + obtain rfl : x = (tcb, σ) := hx + have e : (s0, σ1) = (tcb.tcbState, σ) := h2 + obtain ⟨e1, e2⟩ := Prod.mk.inj e + exact ⟨e2, tcb, getTcb_kheapA hg, e1.symm⟩ + +theorem replyArm_tmRA (r : ObjRef) (final : Bool) : NdTmRA (NonDet.when final (do + let tptr ← getReplyTcb (Z := Unit) r + match tptr with + | none => return () + | some tp => do + let state ← getThreadState tp + match state with + | .blockedOnReply r => replyRemove tp r + | _ => cancelIpc tp)) := by + refine NdTmRA.when (NdTmRA.bindEq (by unfold getReplyTcb NonDet.liftM; exact PresR.bind (getReply_eqPres r) fun _ => PresR.pure _) + fun tptr => ?_) + cases tptr with + | none => exact (NdTmA.pure _).toR + | some tp => + intro σ x hR hx + obtain ⟨⟨st0, σ2⟩, h2, hx⟩ := mem_bindA.1 hx + obtain ⟨e2, y, hy, hys⟩ := getThreadState_memLA h2 + subst e2 + cases st0 with + | blockedOnReply r' => + exact replyRemove_tmXA tp r' _ x (fun hm => by + obtain ⟨z, hz, hrun⟩ := hR tp hm + rw [hy] at hz; cases hz; rw [hys] at hrun; cases hrun) hx + | _ => exact cancelIpc_tmA tp _ x hx + +theorem fl4FinaliseCap_reply_tmRA (r : ObjRef) (rt : CapRights) (d : Bool) (cap : Cap) + (h : cap = .replyCap r rt) : NdTmRA (fl4FinaliseCap (Z := Unit) cap d) := by + subst h + rw [fl4FinaliseCap_plainA (by intro h; exact h)] + unfold finaliseCap + exact NdTmRA.bindT (replyArm_tmRA r d) fun _ => NdTmA.npure _ + +set_option maxHeartbeats 1600000 in +theorem fastFinalise_tmA (c : Cap) (d : Bool) (hc : ∀ r rt, c ≠ .replyCap r rt) : + NdTmA (fastFinalise (Z := Unit) c d) := by + cases c <;> unfold fastFinalise <;> try exact NdTmA.fail + case nullCap => exact NdTmA.npure _ + case replyCap r rt => exact absurd rfl (hc r rt) + case endpointCap r b rt => + refine NdTmA.when ?_ + unfold cancelAllIpc removeAndRestartEpQueuedThread restartThreadIfNoFault possibleSwitchTo + repeat' first | exact ifCondRefillUnblockCheck_tmA _ _ _ (by simp) | tmstep + case notificationCap r b rt => + refine NdTmA.when ?_ + unfold schedContextMaybeUnbindNtfn unbindMaybeNotification doUnbindNtfnSc cancelAllSignals + removeAndRestartNtfnQueuedThread possibleSwitchTo getNtfnObjRef + repeat' first | exact ifCondRefillUnblockCheck_tmA _ _ _ (by simp) | tmstep + +/-- (Task 0 merge) A heap write of a non-TCB, non-SC object keeps the time facts. -/ +theorem nsW_tmA {σ : State Unit} {p : ObjRef} {o : KernelObject} (ht : ∀ x, o ≠ .tcb x) + (hs : ∀ y n, o ≠ .schedContext y n) : + TmA σ { σ with kheap := Function.update σ.kheap p (some o) } := by + refine ⟨rfl, rfl, rfl, ⟨rfl, rfl, fun _ h => h, fun y k h a => ?_, fun t x1 h e _ => ?_⟩⟩ + · dsimp only at h + by_cases e : σ.curSc = p + · rw [e, Function.update_self] at h; exact absurd (Option.some.inj h) (hs y k) + · rw [Function.update_of_ne e] at h; exact ⟨y, k, h, a, rfl⟩ + · dsimp only at h + by_cases e' : t = p + · subst e'; rw [Function.update_self] at h; exact absurd (Option.some.inj h) (ht x1) + · rw [Function.update_of_ne e'] at h; exact ⟨x1, h, e⟩ + +theorem setCap_tmA (c : Cap) (sl : CslotPtr) : NdTmA (setCap (Z := Unit) c sl) := fun σ r hr => by + obtain ⟨p, cref⟩ := sl + obtain ⟨o, o', h1, h2, e⟩ := setCap_objA hr + rw [e] + cases o with + | cnode sz cn => + simp only [setCapObjA] at h2 + split_ifs at h2 + cases h2 + exact nsW_tmA (fun x h => by cases h) (fun y n h => by cases h) + | tcb t => + simp only [setCapObjA] at h2 + (repeat' split at h2) <;> cases h2 <;> exact tcbW_tmA h1 (fun _ h => h) + | _ => cases h2 + +theorem setCdt_tmA (c : Cdt) : NdTmA (setCdt (Z := Unit) c) := fun σ r hr => by + unfold setCdt at hr + obtain ⟨y, hy, hr⟩ := mem_bindA.1 hr + obtain rfl : y = (σ, σ) := hy + obtain rfl : r = ((), _) := hr + exact tmA_eqA rfl rfl rfl rfl rfl rfl rfl + +theorem emptySlot_tmA (sl : CslotPtr) : NdTmA (emptySlot (Z := Unit) sl .nullCap) := by + unfold emptySlot doExtendedOp setOriginal postCapDeletion + tm + all_goals first | exact setCap_tmA _ _ | exact setCdt_tmA _ + +private theorem getCap_eqPresLA (sl : CslotPtr) : PresR (@Eq (State Unit)) (getCap (Z := Unit) sl) := by + obtain ⟨oref, cref⟩ := sl + unfold getCap + dsimp only + refine PresR.bind (getObject_eqPres oref) (fun obj => ?_) + eqpres + +/-- (integration round 2) `deleting_irq_handler` from a state with `RlsRunA` (the IRQ slot's cap may, as far as the +spec knows, be a Reply cap). -/ +theorem deletingIrqHandler_tmRA (irq : Irq) : NdTmRA (deletingIrqHandler (Z := Unit) irq) := by + unfold deletingIrqHandler capDeleteOne + refine NdTmRA.bindEq (by unfold getIrqSlot; exact PresR.gets _) fun slot => ?_ + refine NdTmRA.bindEq (getCap_eqPresLA slot) fun cap => NdTmRA.unless ?_ + refine NdTmRA.bindEq (by unfold isFinalCap; exact PresR.gets _) fun final => ?_ + by_cases hc : ∃ r rt, cap = .replyCap r rt + · obtain ⟨r, rt, rfl⟩ := hc + exact NdTmRA.bindT (by unfold fastFinalise; exact replyArm_tmRA r final) fun _ => emptySlot_tmA slot + · exact NdTmA.toR (NdTmA.bind (fastFinalise_tmA cap final fun r rt h => hc ⟨r, rt, h⟩) + fun _ => emptySlot_tmA _) + +/-! ## `empty_slot` keeps `TKeepA` -/ + +theorem tcbRepl_tkeepA {σ : State Unit} {p : ObjRef} {x x' : Tcb} (hx : σ.kheap p = some (.tcb x)) + (hs : x'.tcbSchedContext = x.tcbSchedContext) (ht : x'.tcbState = x.tcbState) : + TKeepA σ { σ with kheap := Function.update σ.kheap p (some (.tcb x')) } := by + refine ⟨rfl, rfl, rfl, rfl, rfl, fun q y' hq => ?_, fun q y hq => ?_, ⟨fun q y k hq => ?_, rfl⟩⟩ + · by_cases h : q = p + · subst h + dsimp only at hq + rw [Function.update_self] at hq + cases hq + exact ⟨x, hx, hs, ht⟩ + · dsimp only at hq + rw [Function.update_of_ne h] at hq + exact ⟨y', hq, rfl, rfl⟩ + · by_cases h : q = p + · subst h + rw [hx] at hq + cases hq + exact ⟨x', Function.update_self .., hs⟩ + · exact ⟨y, by dsimp only; rw [Function.update_of_ne h]; exact hq, rfl⟩ + · have h : q ≠ p := fun h => by rw [h, hx] at hq; cases hq + exact ⟨y, by dsimp only; rw [Function.update_of_ne h]; exact hq, rfl⟩ + +theorem setCap_tkeepA (cap : Cap) (sl : CslotPtr) : PresR TKeepA (setCap (Z := Unit) cap sl) := by + intro σ r hr + obtain ⟨p, cref⟩ := sl + obtain ⟨o, o', h1, h2, e⟩ := setCap_objA hr + rw [e] + cases o with + | cnode sz cn => + simp only [setCapObjA] at h2 + split_ifs at h2 + cases h2 + exact setNS_tkeep h1 ⟨(by intro x h; cases h), (by intro y n h; cases h)⟩ + ⟨(by intro x h; cases h), (by intro y n h; cases h)⟩ + | tcb t => + simp only [setCapObjA] at h2 + (repeat' split at h2) <;> cases h2 <;> exact tcbRepl_tkeepA h1 rfl rfl + | _ => cases h2 + +theorem emptySlot_tkeepA (sl : CslotPtr) : PresR TKeepA (emptySlot (Z := Unit) sl .nullCap) := by + intro σ r hr + unfold emptySlot at hr + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + have ex : x.2 = σ := getCap_stateA sl σ x hx + rw [ex] at hr + have rest : PresR TKeepA (if x.1 = .nullCap then (pure () : SMonad Unit Unit) else do + let slotP ← NonDet.gets fun s : State Unit => s.cdt sl + let cdt ← NonDet.gets fun s : State Unit => s.cdt + let parent ← pure (cdt sl) + setCdt (Function.update (fun p => if cdt p = some sl then parent else cdt p) sl none) + doExtendedOp (emptySlotExt sl slotP) + setOriginal sl false + setCap .nullCap sl + postCapDeletion .nullCap) := by + split + · exact PresR.pure _ + refine PresR.bind (PresR.gets _) fun _ => PresR.bind (PresR.gets _) fun _ => PresR.bind (PresR.pure _) fun _ => ?_ + refine PresR.bind (fun σ r hr => ?_) fun _ => PresR.bind (fun σ r hr => ?_) fun _ => + PresR.bind (fun σ r hr => ?_) fun _ => PresR.bind (setCap_tkeepA _ _) fun _ => ?_ + · unfold setCdt at hr + obtain ⟨y, hy, hr⟩ := mem_bindA.1 hr + obtain rfl : y = (σ, σ) := hy + obtain rfl : r = ((), _) := hr + exact PreO.refl (R := TKeepA) σ + · unfold doExtendedOp at hr + refine (?_ : PresR TKeepA (_ : SMonad Unit Unit)) σ r hr + refine PresR.bind (fun σ r hr => ?_) fun _ => PresR.bind (fun σ r hr => ?_) fun x => ?_ + · obtain rfl : r = (σ, σ) := hr + exact PreO.refl (R := TKeepA) σ + · have e : r.2 = σ := (PresR.selectF (R := @Eq (State Unit)) _ σ r hr).symm + rw [e] + exact PreO.refl (R := TKeepA) σ + · obtain ⟨u, es⟩ := x + intro σ r hr + obtain rfl : r = ((), _) := hr + exact PreO.refl (R := TKeepA) σ + · unfold setOriginal at hr + obtain ⟨y, hy, hr⟩ := mem_bindA.1 hr + obtain rfl : y = (σ.isOriginalCap, σ) := hy + obtain rfl : r = ((), _) := hr + exact PreO.refl (R := TKeepA) σ + · exact PresR.pure _ + exact rest σ r hr + +/-! ## The notification argument -/ + +theorem ntfnFin_schedValidA {S : Type} {st : machine.Store S} [L : LawfulStore st] {N T : Set U64} {s : S} + {σ : State Unit} {p : U64} (Rn : NtfnStateRel N T (L.mem s) σ) (hp : p ∈ N) (sv : SchedValidA σ) : + ∀ y ∈ ((do schedContextMaybeUnbindNtfn (Z := Unit) (specPtr p) + unbindMaybeNotification (specPtr p) + cancelAllSignals (specPtr p) : SMonad Unit Unit) σ).1, SchedValidA y.2 := by + intro y hy + have hy' : y ∈ (((schedContextMaybeUnbindNtfn (Z := Unit) (specPtr p) >>= fun _ => + unbindMaybeNotification (specPtr p)) >>= fun _ => cancelAllSignals (specPtr p)) σ).1 := by + obtain ⟨u, hu, hy⟩ := mem_bindA.1 hy + obtain ⟨v, hv, hy⟩ := mem_bindA.1 hy + exact mem_bindA.2 ⟨v, mem_bindA.2 ⟨u, hu, hv⟩, hy⟩ + obtain ⟨u, hu, hc3⟩ := mem_bindA.1 hy' + obtain ⟨tk, ko⟩ := unbinds_nkA (specPtr p) σ u hu + have sv2 : SchedValidA u.2 := ((FIA.ofValid sv).ofTKeep tk).toValid + refine cancelAllSignals_schedValid (specPtr p) u.2 ⟨sv2, fun z q hz hq => ?_⟩ y hc3 + obtain ⟨z0, hz0, hobj⟩ := ko z hz + obtain ⟨z', hz', rel⟩ := Rn.ntfn p hp + have ez : z' = z0 := KernelObject.notification.inj (Option.some.inj (hz'.symm.trans hz0)) + rw [ez] at rel + have ro := rel.obj + rw [← hobj, hq] at ro + obtain ⟨-, xs, -, rfl, hq', hall⟩ := ro + refine BAs.ofTKeep (bas_of_codeA sv fun t ht x hx => ?_) tk + obtain ⟨x', hx', R'⟩ := Rn.tcb t (hq'.mem t ht) + obtain rfl : x' = x := KernelObject.tcb.inj (Option.some.inj (hx'.symm.trans hx)) + exact blocked_of_codeA R'.st (Or.inr (Or.inr (hall t ht).1)) + +section +variable {m : Fl4SpecA64.McState} +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Pp Tb : Set U64} {Lv : U64 → Nat} {ia ie : Nat} + {N E Rp : Set U64} {κ : U64 → U64} {n : Nat} {ipa : U64} {Clock : A → MachineState → Prop} {s0 : S} + +/-- **The Reply finaliser hook** in `FinHookKA` form. -/ +theorem finHookReplyKA (s0 : S) (hA : P0.ArchTotal ar) (h1 : fl4A64Platform.numNodes = 1) + (Rv : S → A → State Unit → MdbGhostA → U64 → Prop) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) : + FinHookKA st .reply ar Z.th OS fl4FinaliseCap + (DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st (J7R2A st ar ipa)))) + (DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st J7R2tA))) + (DelRdXA st ar Z H OS ipa Rv) (DelRzA st) := by + intro s a σ G sl c ac f D W hsl hc hk hg hac hf + obtain ⟨p, g, rfl⟩ : ∃ p g, c = .Reply p g := by + cases c <;> simp [finKindA] at hk + exact ⟨_, _, rfl⟩ + exact finHookReplyA s0 Rv hA h1 hCs hmb hmax htu D W hsl hc hg hac (hf trivial) + +/-- **The Reply hook at `DelWTopA`**. -/ +theorem finHookReplySvA (Da : DelArchA st ar AR Clock) (s0 : S) (hA : P0.ArchTotal ar) + (h1 : fl4A64Platform.numNodes = 1) + (Rv : S → A → State Unit → MdbGhostA → U64 → Prop) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) : + FinHookKA st .reply ar Z.th OS fl4FinaliseCap + (DelWTopGA st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar ipa Clock s0) + (DelWxTopGA st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m Clock s0) (DelRdXA st ar Z H OS ipa Rv) (DelRzA st) := by + have cap : ∀ (c : capability.Cap) (ac : Cap), finKindA c = some .reply → CapEqA c ac → + ∃ p g, c = .Reply p g ∧ ac = absCapA c := by + intro c ac hk hac + cases c <;> simp [finKindA] at hk + exact ⟨_, _, rfl, hac.symm⟩ + refine FinHookKA.topA (FinHookKA.timePA (finHookReplyKA s0 hA h1 Rv hCs hmb hmax htu) RlsRunA + (fun s a σ G hW => by obtain ⟨-, ⟨Q, R, hw, Ac, Bf, stack, J, -⟩, -⟩ := hW; exact J.sinv.rlsRun) ?_ Da.clockFr ?_) ?_ + · intro c ac d σ x σ' hR hck hac hmem + obtain ⟨p, g, rfl, rfl⟩ := cap c ac hck hac + exact fl4FinaliseCap_reply_tmRA _ _ d _ rfl σ (x, σ') hR hmem + · intro s a σ G sl c ac f D hW hsl hc hk hg hac hf hCl + exact FinHookKA.clockOfA (C := fun a => Clock a σ.machineState) + (finHookReplyKA s0 hA h1 Rv hCs hmb hmax htu) s a σ G sl c ac f D hW hsl hc hk hg hac hf hCl + · intro s a σ G sl c ac D hW hsl hc hk hac d x σ' hd hmem + obtain ⟨p, g, rfl, rfl⟩ := cap c ac hk hac + obtain ⟨⟨-, ⟨⟨Q, R, hw, Ac, Bf, stack, J, -⟩, -⟩, sv⟩, -⟩ := hW + have hH : H p = some .reply := (J.capTy _ (D.mem_slot hsl)).2.2.1 p g hc + have hp : p ∈ Rp := J.ipcHeapCov.reply p hH + simp only [absCapA] at hmem + rw [fl4FinaliseCap_plainA (by intro h; exact h)] at hmem + obtain ⟨y, hy, hr⟩ := mem_bindA.1 hmem + have e : σ' = y.2 := congrArg Prod.snd (show (x, σ') = (_, y.2) from hr) + rw [e] + cases d + · have ey : y = ((), σ) := hy + rw [ey]; exact sv + obtain ⟨t, ht, hy⟩ := mem_bindA.1 hy + obtain ⟨xr, hkr, hrb⟩ := J.ep.reply p hp + rw [getReplyTcb_eq hkr] at ht + obtain rfl : t = (xr.replyTcb, σ) := ht + dsimp only at hy + by_cases h0 : L.mem s (Endp.rTcbA p) = 0#u64 + · rw [hrb, h0, optRef_zero] at hy + have ey : y = ((), σ) := hy + rw [ey]; exact sv + rw [hrb, optRef_ne h0] at hy + dsimp only at hy + obtain ⟨ht', two⟩ := J.replyCallerA hH h0 + obtain ⟨xt, hkt, hxt⟩ := J.ep.tcb _ ht' + obtain ⟨u, hu, hy⟩ := mem_bindA.1 hy + rw [getThreadState_eq hkt] at hu + obtain rfl : u = (xt.tcbState, σ) := hu + dsimp only at hy + have rq : RqDomPrio σ := rqDomPrio_of_schedA J.sr + rcases two with hs | hs + · obtain ⟨ro, es⟩ := tsCode_five (ts := xt.tcbState) (by + rw [← hxt.st, hs]; unfold sched.TS_BLOCKED_ON_REPLY; rfl) + rw [es] at hy + exact replyRemove_schedValid _ _ σ ⟨sv, rq⟩ y hy + · obtain ⟨e', ro, dd, es⟩ := tsCode_three (ts := xt.tcbState) (by + rw [← hxt.st, hs]; unfold sched.TS_BLOCKED_ON_RECV; rfl) + rw [es] at hy + dsimp only at hy + have uniq : ∀ z, σ.kheap (specPtr (L.mem s (Endp.rTcbA p))) = some (.tcb z) → z = xt := fun z hz => + KernelObject.tcb.inj (Option.some.inj (hz.symm.trans hkt)) + have blk : BAs ([L.mem s (Endp.rTcbA p)].map specPtr) σ := by + refine bas_of_codeA sv fun t ht z hz => ?_ + obtain rfl := List.mem_singleton.1 ht + rw [uniq z hz, es] + rfl + refine (cancelIpcRecv_fia (specPtr (L.mem s (Endp.rTcbA p))) σ σ + ⟨FIA.ofValid sv, rq, ⟨KI.refl _ σ, blk⟩, fun z hz => ?_⟩ y hy).1.toValid + exact ⟨e', ro, dd, by rw [uniq z hz, es]⟩ + +/-- **The IrqHandler hook at `DelWTopA`**. -/ +theorem finHookIrqSvA (Da : DelArchA st ar AR Clock) (s0 : S) + (Rv : S → A → State Unit → MdbGhostA → U64 → Prop) + (F : DelFootA st Z H.tcbs H.omega H.frame OS s0) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (h1 : fl4A64Platform.numNodes = 1) : + FinHookKA st .irqHandler ar Z.th OS fl4FinaliseCap + (DelWTopGA st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar ipa Clock s0) + (DelWxTopGA st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m Clock s0) + (DelRdXA st ar Z H OS ipa (RvVcA st OS Rv)) (DelRzA st) := by + have cap : ∀ (c : capability.Cap) (ac : Cap), finKindA c = some .irqHandler → CapEqA c ac → + ∃ irq, c = .IrqHandler irq ∧ ac = absCapA c := by + intro c ac hk hac + cases c <;> simp [finKindA] at hk + exact ⟨_, rfl, hac.symm⟩ + refine FinHookKA.topA (FinHookKA.timePA (finHookIrqA s0 Rv F hCs hmb hmax htu h1) RlsRunA + (fun s a σ G hW => by obtain ⟨-, ⟨Q, R, hw, Ac, Bf, stack, J, -⟩, -⟩ := hW; exact J.sinv.rlsRun) ?_ Da.clockFr ?_) ?_ + · intro c ac d σ x σ' hR hck hac hmem + obtain ⟨irq, rfl, rfl⟩ := cap c ac hck hac + simp only [absCapA] at hmem + rw [fl4FinaliseCap_plainA (by intro h; exact h)] at hmem + have t : NdTmRA (finaliseCap (Z := Unit) (absCapA (.IrqHandler irq)) d) := by + simp only [absCapA] + unfold finaliseCap + cases d <;> simp only [Bool.false_eq_true, ↓reduceIte] + · exact (NdTmA.pure _).toR + · exact NdTmRA.bindT (deletingIrqHandler_tmRA _) fun _ => NdTmA.pure _ + simp only [absCapA] at t + exact t σ (x, σ') hR hmem + · intro s a σ G sl c ac f D hW hsl hc hk hg hac hf hCl + exact FinHookKA.clockOfA (C := fun a => Clock a σ.machineState) + (finHookIrqA s0 Rv F hCs hmb hmax htu h1) s a σ G sl c ac f D hW hsl hc hk hg hac hf hCl + · intro s a σ G sl c ac D hW hsl hc hk hac d x σ' hd hmem + obtain ⟨irq, rfl, rfl⟩ := cap c ac hk hac + obtain ⟨⟨-, ⟨⟨Q, R, hw, Ac, Bf, stack, J, -⟩, -⟩, sv⟩, -⟩ := hW + simp only [absCapA] at hmem + rw [fl4FinaliseCap_plainA (by intro h; exact h)] at hmem + unfold finaliseCap at hmem + dsimp only at hmem + cases d + · simp only [Bool.false_eq_true, ↓reduceIte] at hmem + have ey : (x, σ') = ((Cap.nullCap, Cap.nullCap), σ) := hmem + have e2 : σ' = σ := congrArg Prod.snd ey + rw [e2]; exact sv + simp only [↓reduceIte] at hmem + obtain ⟨y, hy, hr⟩ := mem_bindA.1 hmem + have e : σ' = y.2 := congrArg Prod.snd (show (x, σ') = (_, y.2) from hr) + rw [e] + have hOSx : cslotAddrA sl ∈ OS := D.mem_slot hsl + have hwf : CapWfA (.IrqHandler irq) := hc ▸ D.wf hOSx + have hi : irq.val < 5376 := hwf + have irqRel : IrqRelA (L.mem s) (L.ksb s) σ := J.j.1.irq + have place : PlaceA Z.th (L.ksb s) := J.j.1.place + obtain ⟨hA, he, -⟩ := irqSlot_relatedA (st := st) irqRel place hi + unfold deletingIrqHandler at hy + obtain ⟨q, hq, hy⟩ := mem_bindA.1 hy + obtain rfl : q = ((σ.interruptIrqNode (BitVec.ofNat 13 irq.val), []), σ) := hq + dsimp only at hy + generalize esl : ((σ.interruptIrqNode (BitVec.ofNat 13 irq.val), []) : CslotPtr) = isl at hA he hy + obtain ⟨ac0, hg, hace⟩ := getCapOne_eqA (st := st) D.rel hA + have hxO := D.mem_slot hA + unfold capDeleteOne at hy + obtain ⟨c0, hc0, hy⟩ := mem_bindA.1 hy + rw [hg] at hc0 + obtain rfl : c0 = (ac0, σ) := hc0 + dsimp only at hy + by_cases hnull : ac0 = .nullCap + · have ey : y = ((), σ) := by + simp only [NonDet.unless, hnull, not_true_eq_false, ↓reduceIte] at hy + exact hy + rw [ey]; exact sv + have hy2 : y ∈ ((isFinalCap (Z := Unit) ac0 >>= fun final => fastFinalise ac0 final >>= fun _ => + emptySlot isl .nullCap) σ).1 := by + simpa only [NonDet.unless, hnull, not_false_eq_true, ↓reduceIte] using hy + obtain ⟨fin, hfin, hy3⟩ := mem_bindA.1 hy2 + have efin : fin.2 = σ := (PresR.gets (R := @Eq (State Unit)) _ σ fin hfin).symm + rw [efin] at hy3 + obtain ⟨w, hw', hy4⟩ := mem_bindA.1 hy3 + have svw : SchedValidA w.2 := by + have hirqc : IrqCapA ac0 := irqSlot_capA D.rel irqRel place J.sinv.irqN hi (by rw [esl]; exact hg) + rcases hirqc with h | ⟨r, bd, rs, h⟩ + · exact absurd h hnull + have eabs : absCapA (capA st s (cslotAddrA isl)) = .notificationCap r bd rs := by + rcases hace.eq_or_pt with e | ⟨_, _, _, e⟩ + · rw [← e]; exact h + · rw [h] at e; cases e + obtain ⟨p, b0, b1, b2, hcp, er⟩ : ∃ p b0 b1 b2, capA st s (cslotAddrA isl) = .Notification p b0 b1 b2 ∧ + r = specPtr p := by + generalize capA st s (cslotAddrA isl) = c at eabs ⊢ + cases c <;> simp [absCapA] at eabs + exact ⟨_, _, _, _, rfl, eabs.1.symm⟩ + have hp : p ∈ N := J.ipcHeapCov.ntfn p ((J.capTy _ hxO).2.1 p b0 b1 b2 hcp) + rw [h, er] at hw' + unfold fastFinalise at hw' + cases hf : fin.1 + · rw [hf] at hw' + have ew : w = ((), σ) := hw' + rw [ew]; exact sv + · rw [hf] at hw' + exact ntfnFin_schedValidA J.ntfn hp sv w hw' + exact ((FIA.ofValid svw).ofTKeep (emptySlot_tkeepA _ _ _ hy4)).toValid + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3DelSvScA.lean b/proofs/Fl4ProofsA64/P3/Stage3DelSvScA.lean new file mode 100644 index 000000000..354a21715 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3DelSvScA.lean @@ -0,0 +1,297 @@ +/- +P3 (AArch64, delete hooks, contract §89): **`SchedValidA` across the SchedContext finaliser** and its hook at +`DelWTopA` / `DelWxTopA`. + +* **`scFin_schedValid`**: `unbind_all_tcbs` leaves no TCB bound to the SC (`ScInvA.back`), the three unlinks keep that, + and making an SC no TCB is bound to inactive keeps `SchedValidA`. +* **`finHookScSvA`**: `finHookScA` through `FinHookKA.timeA` and `FinHookKA.topA`. + +No named premise of its own (`finHookScA`'s: [P] `hA`, `hCs`, `hmax`, `htu`; [S] `h1`). New module. +-/ +import Fl4ProofsA64.P3.Stage3DelSvReplyA +import Fl4ProofsA64.P3.Stage3DelSvSuspendA +import Fl4ProofsA64.P3.DelScHookA +import Fl4ProofsA64.P3.DelThrTimeA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +/-- No TCB is bound to the SC at `p`. -/ +def NoBoundA (p : ObjRef) (σ : State Unit) : Prop := + ∀ t x, σ.kheap t = some (.tcb x) → x.tcbSchedContext ≠ some p + +theorem NoBoundA.ofTKeep {p : ObjRef} {σ σ' : State Unit} (h : NoBoundA p σ) (k : TKeepA σ σ') : NoBoundA p σ' := by + obtain ⟨-, -, -, -, -, kb, -, -, -⟩ := k + intro t x' hx' + obtain ⟨x, hx, e, -⟩ := kb t x' hx' + rw [e] + exact h t x hx + +/-- An SC no TCB is bound to may be replaced by any SC. -/ +theorem SchedValidA.updUnbound {σ : State Unit} {p : ObjRef} {y y' : SchedContext} {k : Nat} (h : SchedValidA σ) + (hnb : NoBoundA p σ) (hk : σ.kheap p = some (.schedContext y k)) : + SchedValidA { σ with kheap := Function.update σ.kheap p (some (.schedContext y' k)) } ∧ + NoBoundA p { σ with kheap := Function.update σ.kheap p (some (.schedContext y' k)) } := by + have fwd : ∀ t x, σ.kheap t = some (.tcb x) → + Function.update σ.kheap p (some (.schedContext y' k)) t = some (.tcb x) := by + intro t x e + have ne : t ≠ p := fun c => by rw [c, hk] at e; cases e + rw [Function.update_of_ne ne]; exact e + have back : ∀ t x, Function.update σ.kheap p (some (.schedContext y' k)) t = some (.tcb x) → + σ.kheap t = some (.tcb x) := by + intro t x e + by_cases c : t = p + · rw [c, Function.update_self] at e; cases e + · rwa [Function.update_of_ne c] at e + have act : ∀ t x q, σ.kheap t = some (.tcb x) → x.tcbSchedContext = some q → ScActiveAtA σ q → + ScActiveAtA { σ with kheap := Function.update σ.kheap p (some (.schedContext y' k)) } q := by + rintro t x q e es ⟨z, j, hz, ha⟩ + have ne : q ≠ p := fun c => hnb t x e (c ▸ es) + exact ⟨z, j, by show Function.update σ.kheap p _ q = _; rw [Function.update_of_ne ne]; exact hz, ha⟩ + refine ⟨⟨fun d pr t hm => ?_, fun e1 e2 => ?_, fun t x q e1 e2 e3 => ?_, fun t ht => ?_⟩, + fun t x e => hnb t x (back t x e)⟩ + · obtain ⟨⟨x, q, e, es, a⟩, nr⟩ := h.rq d pr t hm + exact ⟨⟨x, q, fwd _ _ e, es, act _ _ _ e es a⟩, nr⟩ + · obtain ⟨⟨x, q, e, es, a⟩, nr⟩ := h.act e1 e2 + exact ⟨⟨x, q, fwd _ _ e, es, act _ _ _ e es a⟩, nr⟩ + · have e := back _ _ e1 + exact act _ _ _ e e3 (h.blk t x q e e2 e3) + · obtain ⟨x, q, e, es, a⟩ := h.rls t ht + exact ⟨x, q, fwd _ _ e, es, act _ _ _ e es a⟩ + +theorem setInactive_unbound (p : ObjRef) : + HT (fun σ => SchedValidA σ ∧ NoBoundA p σ) (schedContextSetInactive (Z := Unit) p) + (fun _ σ => SchedValidA σ) := by + have step : ∀ f : SchedContext → SchedContext, + HT (fun σ => SchedValidA σ ∧ NoBoundA p σ) (updateSchedContext (Z := Unit) p f) + (fun _ σ => SchedValidA σ ∧ NoBoundA p σ) := by + intro f σ h r hr + obtain ⟨y, k, hk, e⟩ := updateSchedContext_memA p f hr + rw [e] + exact h.1.updUnbound h.2 hk + unfold schedContextSetInactive + refine HT.bind (Q := fun _ σ => SchedValidA σ ∧ NoBoundA p σ) (step _) fun _ => ?_ + refine HT.bind (Q := fun _ σ => SchedValidA σ ∧ NoBoundA p σ) (step _) fun _ => ?_ + refine HT.bind (Q := fun _ σ => SchedValidA σ ∧ NoBoundA p σ) (step _) fun _ => ?_ + exact HT.mono (step _) (fun _ h => h) (fun _ _ h => h.1) + +/-- `sched_context_unbind_tcb` leaves no TCB bound to the SC, given the SC / TCB back-pointers. -/ +theorem unbindTcb_noBoundA {σ : State Unit} {p t : ObjRef} {x : SchedContext} {k : Nat} + (hk : σ.kheap p = some (.schedContext x k)) (ht : x.scTcb = some t) + (back : ∀ u xu, σ.kheap u = some (.tcb xu) → xu.tcbSchedContext = some p → x.scTcb = some u) + {r : Unit × State Unit} (hr : r ∈ (schedContextUnbindTcb (Z := Unit) p σ).1) : NoBoundA p r.2 := by + unfold schedContextUnbindTcb at hr + rw [nd_bind_oneSc (Sched.getSchedContext_eqA hk), ht, + nd_bind_oneSc (show Fl4Spec.NonDet.assertOpt (σ := State Unit) (some t) σ = ({(t, σ)}, False) from rfl), + nd_gets_bindSc] at hr + obtain ⟨a, ha, hr⟩ := mem_bindA.1 hr + have A : a.2.kheap = σ.kheap := by + unfold Fl4Spec.NonDet.when at ha + split_ifs at ha + · have e := (rescheduleRequired_memA ha).1.1 + rw [e] + · have : a = ((), σ) := ha + rw [this] + obtain ⟨b, hb, hr⟩ := mem_bindA.1 hr + have B : b.2.kheap = σ.kheap := (tcbSchedDeq_kheapA t hb).trans A + obtain ⟨c, hc', hr⟩ := mem_bindA.1 hr + have C : c.2.kheap = σ.kheap := (tcbReleaseRemove_kheapA t hc').trans B + obtain ⟨d, hd, hr⟩ := mem_bindA.1 hr + unfold setTcbObjRef at hd + obtain ⟨zt, hzt, ez⟩ := threadSet_memA _ _ hd + unfold setScObjRef at hr + obtain ⟨y, j, hy, ey⟩ := updateSchedContext_memA _ _ hr + intro u xu hu + rw [ey] at hu + change Function.update d.2.kheap p _ u = _ at hu + have ne : u ≠ p := fun e => by rw [e, Function.update_self] at hu; cases hu + rw [Function.update_of_ne ne, ez] at hu + change Function.update c.2.kheap t _ u = _ at hu + by_cases e : u = t + · rw [e, Function.update_self] at hu + obtain rfl := KernelObject.tcb.inj (Option.some.inj hu) + intro h + cases h + · rw [Function.update_of_ne e, C] at hu + intro h + have := back u xu hu h + rw [ht] at this + exact e (Option.some.inj this).symm + +theorem unbindAll_unbound (p : ObjRef) (hni : p ≠ idleScPtr) : + HT (fun σ => SchedValidA σ ∧ RqDomPrio σ ∧ + ∀ u xu, σ.kheap u = some (.tcb xu) → xu.tcbSchedContext = some p → + ∃ y k, σ.kheap p = some (.schedContext y k) ∧ y.scTcb = some u) + (schedContextUnbindAllTcbs (Z := Unit) p) (fun _ σ => SchedValidA σ ∧ NoBoundA p σ) := by + rintro σ ⟨sv, rq, back⟩ r hr + unfold schedContextUnbindAllTcbs at hr + obtain ⟨g, hg, hr⟩ := mem_bindA.1 hr + have hg0 := hg + unfold getSchedContext at hg0 + obtain ⟨o, ho, hg1⟩ := mem_bindA.1 hg0 + obtain ⟨e2, hk0⟩ := getObject_mem ho + obtain ⟨y, k, hk⟩ : ∃ y k, σ.kheap p = some (.schedContext y k) := by + rw [hk0] + generalize o.1 = ob at hg1 + cases ob with + | schedContext y k => exact ⟨y, k, rfl⟩ + | _ => exact absurd hg1 (Set.notMem_empty _) + rw [Sched.getSchedContext_eqA hk] at hg + obtain rfl : g = (y, σ) := hg + dsimp only at hr + have back' : ∀ u xu, σ.kheap u = some (.tcb xu) → xu.tcbSchedContext = some p → y.scTcb = some u := by + intro u xu hu hp + obtain ⟨y', k', hk', e⟩ := back u xu hu hp + rw [hk] at hk'; cases hk' + exact e + unfold Fl4Spec.NonDet.when at hr + split_ifs at hr with hc + · obtain ⟨t, ht⟩ := Option.ne_none_iff_exists'.1 hc.1 + exact ⟨(schedContextUnbindTcb_fia p σ ⟨FIA.ofValid sv, rq⟩ r hr).toValid, + unbindTcb_noBoundA hk ht back' hr⟩ + · have er : r = ((), σ) := hr + rw [er] + refine ⟨sv, fun u xu hu hp => ?_⟩ + exact hc ⟨by rw [back' u xu hu hp]; simp, hni⟩ + +/-- A `TKeepA` step keeps `SchedValidA` and the absence of bound TCBs. -/ +theorem HT.unbound_tp (p : ObjRef) {α : Type} {m : SMonad Unit α} (h : TPres m) : + HT (fun σ => SchedValidA σ ∧ NoBoundA p σ) m (fun _ σ => SchedValidA σ ∧ NoBoundA p σ) := + fun σ hσ r hr => + ⟨((FIA.ofValid hσ.1).ofTKeep (h σ r hr)).toValid, hσ.2.ofTKeep (h σ r hr)⟩ + +/-- (integration round 2) `sched_context_set_inactive` keeps `TmA`: the refill list is emptied together with +`refill_max := 0`, so the SC ends inactive (`CurKeepA.sc` is vacuous at it). -/ +theorem schedContextSetInactive_tmA (p : ObjRef) : NdTmA (schedContextSetInactive (Z := Unit) p) := fun σ r hr => by + unfold schedContextSetInactive at hr + obtain ⟨u1, h1, hr⟩ := mem_bindA.1 hr + obtain ⟨u2, h2, hr⟩ := mem_bindA.1 hr + obtain ⟨y, n, hy, e1⟩ := updateSchedContext_memA p _ h1 + obtain ⟨y2, n2, hy2, e2⟩ := updateSchedContext_memA p _ h2 + rw [e1] at hy2 + simp only [Function.update_self, Option.some.injEq, KernelObject.schedContext.injEq] at hy2 + obtain ⟨rfl, rfl⟩ := hy2 + have T12 : TmA σ u2.2 := by + have e : u2.2 = { σ with kheap := Function.update σ.kheap p (some (.schedContext + (scRefillMaxUpdate (fun _ => 0) (scRefillsUpdate (fun _ => []) y)) n)) } := by + rw [e2, e1]; simp [Function.update_idem] + rw [e] + exact scW_tmA hy (fun h => by simp [scActive, activeSc, scRefillMaxUpdate] at h) + have T34 : NdTmA (do + updateSchedContext (Z := Unit) p (scBudgetUpdate fun _ => 0) + updateSchedContext (Z := Unit) p (scSporadicUpdate fun _ => false)) := + NdTmA.bind (updateSchedContext_tmA _ _ fun _ h => ⟨h, rfl⟩) fun _ => + updateSchedContext_tmA _ _ fun _ h => ⟨h, rfl⟩ + exact tmA_trans T12 (T34 u2.2 r hr) + +/-- **The SchedContext finaliser's spec side keeps `SchedValidA`** (module docstring). -/ +theorem scFin_schedValid (p : ObjRef) (hni : p ≠ idleScPtr) (c : Word) (f : Bool) : + HT (fun σ => SchedValidA σ ∧ RqDomPrio σ ∧ + ∀ u xu, σ.kheap u = some (.tcb xu) → xu.tcbSchedContext = some p → + ∃ y k, σ.kheap p = some (.schedContext y k) ∧ y.scTcb = some u) + (do Fl4Spec.NonDet.when (f = true) (schedContextUnbindAllTcbs (Z := Unit) p) + Fl4Spec.NonDet.when (f = true) (schedContextUnbindNtfn p) + Fl4Spec.NonDet.when (f = true) (schedContextUnbindReply p) + Fl4Spec.NonDet.when (f = true) (schedContextUnbindYieldFrom p) + Fl4Spec.NonDet.when (f = true) (schedContextSetInactive p)) + (fun _ σ => SchedValidA σ) := by + cases f + · refine HT.bind (Q := fun _ σ => SchedValidA σ) (HT.when (fun h => by cases h) (fun _ _ h => h.1)) fun _ => ?_ + refine HT.bind (Q := fun _ σ => SchedValidA σ) (HT.when (fun h => by cases h) (fun _ _ h => h)) fun _ => ?_ + refine HT.bind (Q := fun _ σ => SchedValidA σ) (HT.when (fun h => by cases h) (fun _ _ h => h)) fun _ => ?_ + refine HT.bind (Q := fun _ σ => SchedValidA σ) (HT.when (fun h => by cases h) (fun _ _ h => h)) fun _ => ?_ + exact HT.when (fun h => by cases h) (fun _ _ h => h) + refine HT.bind (Q := fun _ σ => SchedValidA σ ∧ NoBoundA p σ) + (HT.when (fun _ => unbindAll_unbound p hni) (fun h => absurd rfl h)) fun _ => ?_ + refine HT.bind (Q := fun _ σ => SchedValidA σ ∧ NoBoundA p σ) + (HT.when (fun _ => HT.unbound_tp p (by + unfold schedContextUnbindNtfn doUnbindNtfnSc NonDet.maybeM; tpres)) (fun h => absurd rfl h)) fun _ => ?_ + refine HT.bind (Q := fun _ σ => SchedValidA σ ∧ NoBoundA p σ) + (HT.when (fun _ => HT.unbound_tp p (by unfold schedContextUnbindReply; tpres)) + (fun h => absurd rfl h)) fun _ => ?_ + refine HT.bind (Q := fun _ σ => SchedValidA σ ∧ NoBoundA p σ) + (HT.when (fun _ => HT.unbound_tp p (by + unfold schedContextUnbindYieldFrom completeYieldTo setConsumed schedContextUpdateConsumed + schedContextCancelYieldTo NonDet.maybeM + tpres)) (fun h => absurd rfl h)) fun _ => ?_ + exact HT.when (fun _ => setInactive_unbound p) (fun h => absurd rfl h) + +section +variable {m : Fl4SpecA64.McState} +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Pp Tb : Set U64} {Lv : U64 → Nat} {ia ie : Nat} + {N E Rp : Set U64} {κ : U64 → U64} {n : Nat} {ipa : U64} {Clock : A → MachineState → Prop} {s0 : S} + +/-- **The SchedContext hook at `DelWTopA`**. -/ +theorem finHookScSvA (Da : DelArchA st ar AR Clock) (s0 : S) + (Rv : S → A → State Unit → MdbGhostA → U64 → Prop) + (hA : P0.ArchTotal ar) (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (h1 : fl4A64Platform.numNodes = 1) : + FinHookKA st .schedContext ar Z.th OS fl4FinaliseCap + (DelWTopGA st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar ipa Clock s0) + (DelWxTopGA st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m Clock s0) (DelRdXA st ar Z H OS ipa Rv) (DelRzA st) := by + have cap : ∀ (c : capability.Cap) (ac : Cap), finKindA c = some .schedContext → CapEqA c ac → + ∃ p b, c = .SchedContext p b ∧ ac = absCapA c := by + intro c ac hk hac + cases c <;> simp [finKindA] at hk + exact ⟨_, _, rfl, hac.symm⟩ + have H0 := finHookScA (st := st) (Ag := Ag) (ar := ar) (AR := AR) (Z := Z) (H := H) (OS := OS) (U := U) (Pp := Pp) + (Tb := Tb) (Lv := Lv) (ia := ia) (ie := ie) (N := N) (E := E) (Rp := Rp) (κ := κ) (n := n) (m := m) (ipa := ipa) + s0 Rv hA hCs hmax htu h1 + refine FinHookKA.topA (FinHookKA.timeA H0 ?_ Da.clockFr ?_) ?_ + · intro c ac d σ x σ' hck hac hmem + obtain ⟨p, b, rfl, rfl⟩ := cap c ac hck hac + simp only [absCapA] at hmem + rw [fl4FinaliseCap_plainA (by intro h; exact h)] at hmem + have t : NdTmA (finaliseCap (Z := Unit) (absCapA (.SchedContext p b)) d) := by + simp only [absCapA] + unfold finaliseCap schedContextUnbindAllTcbs schedContextUnbindNtfn schedContextUnbindReply + schedContextUnbindYieldFrom doUnbindNtfnSc NonDet.unless NonDet.liftM + tm + all_goals first + | exact schedContextSetInactive_tmA _ + | exact setMrs_tmA _ _ _ + | ((try simp only [getScObjRef, getSkObjRef]); tm) + simp only [absCapA] at t + exact t σ (x, σ') hmem + · intro s a σ G sl c ac f D hW hsl hc hk hg hac hf hCl + exact FinHookKA.clockOfA (C := fun a => Clock a σ.machineState) (finHookScA (m := m) (ipa := ipa) s0 Rv hA hCs hmax htu h1) s a σ G sl c ac f D hW hsl hc hk hg hac hf hCl + · intro s a σ G sl c ac D hW hsl hc hk hac d x σ' hd hmem + obtain ⟨p, b, rfl, rfl⟩ := cap c ac hk hac + obtain ⟨⟨-, ⟨⟨Q, R, hw, Ac, Bf, stack, J, -⟩, -⟩, sv⟩, -⟩ := hW + have hOSx : cslotAddrA sl ∈ OS := D.mem_slot hsl + have hH : H p = some (.sc b.val) := (J.capTy _ hOSx).2.2.2.1 p b hc + have hni := J.ciSc _ hOSx p b hc + have hp : p ∈ Z.C := by + rw [← J.scs] + first + | exact ⟨_, hH⟩ + | (change H p ∈ _; rw [hH]; simp) + | simp [Heap.scs, hH] + have hni' : specPtr p ≠ idleScPtr := J.sr.sch.scOff p hp hni + simp only [absCapA] at hmem + rw [fl4FinaliseCap_plainA (by intro h; exact h)] at hmem + unfold finaliseCap at hmem + dsimp only at hmem + obtain ⟨y1, hy1, hr⟩ := mem_bindA.1 hmem + obtain ⟨y2, hy2, hr⟩ := mem_bindA.1 hr + obtain ⟨y3, hy3, hr⟩ := mem_bindA.1 hr + obtain ⟨y4, hy4, hr⟩ := mem_bindA.1 hr + obtain ⟨y5, hy5, hr⟩ := mem_bindA.1 hr + have er : σ' = y5.2 := congrArg Prod.snd (show (x, σ') = (_, y5.2) from hr) + rw [er] + exact scFin_schedValid (specPtr p) hni' 0 d σ ⟨sv, rqDomPrio_of_schedA J.sr, fun u xu hu hp => J.sr.inv.back u xu _ hu hp⟩ y5 + (mem_bindA.2 ⟨y1, hy1, mem_bindA.2 ⟨y2, hy2, mem_bindA.2 ⟨y3, hy3, mem_bindA.2 ⟨y4, hy4, hy5⟩⟩⟩⟩) + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3DelSvSuspendA.lean b/proofs/Fl4ProofsA64/P3/Stage3DelSvSuspendA.lean new file mode 100644 index 000000000..e8fc15490 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3DelSvSuspendA.lean @@ -0,0 +1,286 @@ +/- +P3 (AArch64, delete hooks, contract §88): **`SchedValidA` across the Thread finaliser's `unbind_notification`, +`unbind_from_sc` and `suspend`** (the composites over G1's primitives). + +`cancel_ipc` needs nothing on any arm; `suspend` needs nothing; `unbind_from_sc` needs `RqDomPrio` (its dequeue must +empty every ready queue of the unbound thread before its SC goes), which `unbind_notification` keeps. + +* **`cancelIpc_fia`**, **`dequeueW_fia`**, **`suspend_schedValid`**, **`unbindFromSc_schedValid`**, + **`unbindNotification_schedValid`**, **`threadFinPrefix_schedValid`**. + +No premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3SigSchedValidA +import Fl4ProofsA64.P3.Stage3DelXSchedValidA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P3 Classical + +noncomputable section + +instance (g : Option ObjRef → Option ObjRef) : TcbScFn (tcbBoundNotificationUpdate g) := ⟨fun _ => ⟨rfl, rfl⟩⟩ + +instance (t : ObjRef) (v : Option ObjRef) : TPresInst (setTcbObjRef (Z := Unit) tcbYieldToUpdate t v) := + ⟨threadSet_tpres (tcbYieldToUpdate fun _ => v) t⟩ + +instance (t : ObjRef) (v : Option ObjRef) : TPresInst (setTcbObjRef (Z := Unit) tcbBoundNotificationUpdate t v) := + ⟨threadSet_tpres (tcbBoundNotificationUpdate fun _ => v) t⟩ + +theorem frameOK_true : FrameOK (fun _ => True) := ⟨fun _ _ _ h => h, fun _ _ _ _ _ _ h => h, fun _ _ h => h⟩ + +theorem HT.tp {α : Type} {m : SMonad Unit α} (h : TPres m) : HT FIA m (fun _ σ => FIA σ) := + fun σ hσ r hr => hσ.ofTKeep (h σ r hr) + +theorem inactive_fia (t : ObjRef) : HT FIA (setThreadState (Z := Unit) t .inactive) (fun _ σ => FIA σ) := + HT.mono (setThreadState_fia t .inactive rfl (fun _ => True) frameOK_true.tcb frameOK_true.act) + (fun _ h => ⟨h, trivial⟩) (fun _ _ h => h.1) + +theorem unlink_fia (t r : ObjRef) : HT FIA (replyUnlinkTcb (Z := Unit) t r) (fun _ σ => FIA σ) := + HT.mono (replyUnlinkTcb_fia t r (fun _ => True) frameOK_true) (fun _ h => ⟨h, trivial⟩) (fun _ _ h => h.1) + +theorem blockedCancelIpc_fia (st : ThreadState) (t : ObjRef) (ro : Option ObjRef) : + HT FIA (blockedCancelIpc (Z := Unit) st t ro) (fun _ σ => FIA σ) := by + unfold blockedCancelIpc getBlockingObject + refine HT.bind (Q := fun _ σ => FIA σ) (HT.assertOpt _ fun _ _ _ h => h) fun ep => ?_ + refine HT.bind (Q := fun _ σ => FIA σ) (HT.tp (by unfold tcbEpDequeue; tpres)) fun _ => ?_ + cases ro with + | none => exact inactive_fia t + | some r => exact HT.bind (Q := fun _ σ => FIA σ) (unlink_fia t r) fun _ => inactive_fia t + +theorem replyRemoveTcb_fia (t r : ObjRef) : HT FIA (replyRemoveTcb (Z := Unit) t r) (fun _ σ => FIA σ) := by + unfold replyRemoveTcb + refine HT.bind (Q := fun _ σ => FIA σ) (HT.tp (by tpres)) fun _ => ?_ + refine HT.bind (Q := fun _ σ => FIA σ) (HT.tp (by tpres)) fun _ => ?_ + refine HT.bind (Q := fun _ σ => FIA σ) (HT.gets _ fun _ h => h) fun o => ?_ + cases o with + | none => exact unlink_fia t r + | some sc => + dsimp only + refine HT.bind (Q := fun _ σ => FIA σ) (HT.tp (by tpres)) fun _ => ?_ + refine HT.bind (Q := fun _ σ => FIA σ) (HT.tp (by tpres)) fun _ => ?_ + refine HT.bind (Q := fun _ σ => FIA σ) (HT.tp (by tpres)) fun _ => ?_ + refine HT.bind (Q := fun _ σ => FIA σ) (HT.tp (by tpres)) fun _ => ?_ + refine HT.bind (Q := fun _ σ => FIA σ) (HT.tp (by tpres)) fun _ => ?_ + exact unlink_fia t r + +/-- **`cancel_ipc` keeps `FIA`**, every arm. -/ +theorem cancelIpc_fia (t : ObjRef) : HT FIA (cancelIpc (Z := Unit) t) (fun _ σ => FIA σ) := by + unfold cancelIpc + refine HT.bind (Q := fun _ σ => FIA σ) (HT.tp (by tpres)) fun st => ?_ + refine HT.bind (Q := fun _ σ => FIA σ) ?_ fun _ => ?_ + · intro σ hσ r hr + obtain ⟨x, hx, hr2⟩ := threadSet_spec hr + rw [hr2] + exact hσ.ofTcbUpd hx (x' := { x with tcbFault := none }) rfl (fun _ => rfl) + cases st with + | blockedOnSend _ _ => exact blockedCancelIpc_fia _ t _ + | blockedOnReceive _ _ _ => exact blockedCancelIpc_fia _ t _ + | blockedOnNotification e => + dsimp only + unfold cancelSignal + exact HT.bind (Q := fun _ σ => FIA σ) (HT.tp (by unfold tcbNtfnDequeue; tpres)) fun _ => inactive_fia t + | blockedOnReply r => exact replyRemoveTcb_fia t r + | _ => exact HT.pure _ fun _ h => h + +/-- A dequeue only removes ready-queue members. -/ +theorem dequeueW_fia (t : ObjRef) : HT FIA (tcbSchedAction (Z := Unit) tcbSchedDequeue t) (fun _ σ => FIA σ) := by + unfold Fl4SpecA64.tcbSchedAction + refine HT.bind (Q := fun _ σ => FIA σ) (HT.threadGet _ t fun _ h _ _ => h) fun d => ?_ + refine HT.bind (Q := fun _ σ => FIA σ) (HT.threadGet _ t fun _ h _ _ => h) fun p => ?_ + unfold Fl4SpecA64.getTcbQueue + refine HT.bind (Q := fun q σ => FIA σ ∧ q = σ.readyQueues d p) + (HT.bind (Q := fun qs σ => FIA σ ∧ qs = σ.readyQueues) (HT.gets _ fun _ h => ⟨h, rfl⟩) fun qs => + HT.pure _ fun σ h => ⟨h.1, by rw [h.2]⟩) fun queue => ?_ + refine HT.bind (Q := fun q' σ => (FIA σ ∧ queue = σ.readyQueues d p) ∧ q' = tcbSchedDequeue t queue) + (HT.pure _ fun _ h => ⟨h, rfl⟩) fun queue' => ?_ + unfold Fl4SpecA64.setTcbQueue + refine HT.modify _ fun σ h => ?_ + obtain ⟨⟨hI, hq⟩, hq'⟩ := h + refine hI.updRq _ fun d' p' t' hm => Or.inl ?_ + by_cases e : d' = d ∧ p' = p + · rw [if_pos e, hq'] at hm + rw [e.1, e.2, ← hq]; exact (List.mem_filter.1 hm).1 + · rw [if_neg e] at hm; exact hm + +theorem suspend_fia (t : ObjRef) : HT FIA (Fl4SpecA64.suspend (Z := Unit) t) (fun _ σ => FIA σ) := by + unfold Fl4SpecA64.suspend + refine HT.bind (Q := fun _ σ => FIA σ) (cancelIpc_fia t) fun _ => ?_ + refine HT.bind (Q := fun _ σ => FIA σ) (HT.tp (by tpres)) fun st => ?_ + refine HT.bind (Q := fun _ σ => FIA σ) (HT.tp (by unfold updateRestartPc; tpres)) fun _ => ?_ + refine HT.bind (Q := fun _ σ => FIA σ) (dequeueW_fia t) fun _ => ?_ + refine HT.bind (Q := fun _ σ => FIA σ) + (HT.mono (relRemoveD t (fun _ => True) (fun _ _ h => h) (fun _ _ h => h)) (fun _ h => ⟨h, trivial⟩) + (fun _ _ h => h.1)) fun _ => ?_ + refine HT.bind (Q := fun _ σ => FIA σ) (HT.tp (by unfold schedContextCancelYieldTo NonDet.maybeM; tpres)) + fun _ => inactive_fia t + +/-- **`suspend` keeps `SchedValidA`.** -/ +theorem suspend_schedValid (t : ObjRef) : + HT SchedValidA (Fl4SpecA64.suspend (Z := Unit) t) (fun _ σ => SchedValidA σ) := + HT.mono (suspend_fia t) (fun _ h => FIA.ofValid h) (fun _ _ h => h.toValid) + +/-! ## `rescheduleRequired` and `RqDomPrio` -/ + +theorem enqueue_rq (t : ObjRef) : + HT RqDomPrio (tcbSchedAction (Z := Unit) tcbSchedEnqueue t) (fun _ σ => RqDomPrio σ) := by + unfold Fl4SpecA64.tcbSchedAction + refine HT.bind (Q := fun d σ => RqDomPrio σ ∧ ∃ x, σ.kheap t = some (.tcb x) ∧ d = x.tcbDomain) + (HT.threadGet _ t fun σ h x hx => ⟨h, x, hx, rfl⟩) fun d => ?_ + refine HT.bind (Q := fun p σ => RqDomPrio σ ∧ ∃ x, σ.kheap t = some (.tcb x) ∧ d = x.tcbDomain ∧ p = x.tcbPriority) + (HT.threadGet _ t fun σ h x hx => ?_) fun p => ?_ + · obtain ⟨hD, x0, e0, hd⟩ := h + rw [hx] at e0 + obtain rfl : x = x0 := KernelObject.tcb.inj (Option.some.inj e0) + exact ⟨hD, x, hx, hd, rfl⟩ + unfold Fl4SpecA64.getTcbQueue + refine HT.bind (Q := fun q σ => (RqDomPrio σ ∧ ∃ x, σ.kheap t = some (.tcb x) ∧ d = x.tcbDomain ∧ + p = x.tcbPriority) ∧ q = σ.readyQueues d p) + (HT.bind (Q := fun qs σ => (RqDomPrio σ ∧ ∃ x, σ.kheap t = some (.tcb x) ∧ d = x.tcbDomain ∧ + p = x.tcbPriority) ∧ qs = σ.readyQueues) (HT.gets _ fun _ h => ⟨h, rfl⟩) fun qs => + HT.pure _ fun σ h => ⟨h.1, by rw [h.2]⟩) fun queue => ?_ + refine HT.bind (Q := fun q' σ => ((RqDomPrio σ ∧ ∃ x, σ.kheap t = some (.tcb x) ∧ d = x.tcbDomain ∧ + p = x.tcbPriority) ∧ queue = σ.readyQueues d p) ∧ q' = tcbSchedEnqueue t queue) + (HT.pure _ fun _ h => ⟨h, rfl⟩) fun queue' => ?_ + unfold Fl4SpecA64.setTcbQueue + refine HT.modify _ fun σ h => ?_ + obtain ⟨⟨⟨hD, x, ex, hd, hp⟩, hq⟩, hq'⟩ := h + intro f d' p' y hy hm + dsimp only at hy hm + by_cases e : d' = d ∧ p' = p + · rw [if_pos e, hq'] at hm + have hm' : f = t ∨ f ∈ queue := by + unfold tcbSchedEnqueue at hm + split_ifs at hm + all_goals first | exact List.mem_cons.1 hm | exact Or.inr hm + rcases hm' with rfl | hm' + · rw [ex] at hy + obtain rfl := KernelObject.tcb.inj (Option.some.inj hy) + exact ⟨e.1.trans hd, e.2.trans hp⟩ + · rw [hq, ← e.1, ← e.2] at hm' + exact hD f d' p' y hy hm' + · rw [if_neg e] at hm + exact hD f d' p' y hy hm + +theorem HT.ro {α : Type} {m : SMonad Unit α} (h : CPres m) : + HT RqDomPrio m (fun _ σ => RqDomPrio σ) := fun σ hσ r hr => hσ.ofCells (h σ r hr) + +theorem reschedule_rq : + HT RqDomPrio (rescheduleRequired (Z := Unit)) + (fun _ σ => RqDomPrio σ ∧ σ.schedulerAction = .chooseNewThread) := by + unfold Fl4SpecA64.rescheduleRequired + have fin : HT RqDomPrio (setSchedulerAction (Z := Unit) .chooseNewThread) + (fun _ σ => RqDomPrio σ ∧ σ.schedulerAction = .chooseNewThread) := by + unfold Fl4SpecA64.setSchedulerAction + exact HT.modify _ fun σ h => ⟨h, rfl⟩ + refine HT.bind (Q := fun _ σ => RqDomPrio σ) (HT.gets _ fun _ h => h) fun action => ?_ + cases action with + | switchThread t => + dsimp only + refine HT.bind (Q := fun _ σ => RqDomPrio σ) (HT.gets _ fun _ h => h) fun sched => ?_ + refine HT.bind (Q := fun _ σ => RqDomPrio σ) ?_ fun _ => fin + refine HT.when (fun _ => ?_) (fun _ _ h => h) + refine HT.bind (Q := fun _ σ => RqDomPrio σ) (HT.threadGet _ t fun _ h _ _ => h) fun scOpt => ?_ + refine HT.bind (Q := fun _ σ => RqDomPrio σ) (HT.assertOpt _ fun _ _ _ h => h) fun scp => ?_ + refine HT.bind (Q := fun _ σ => RqDomPrio σ) (HT.getsThe _ fun _ h _ _ => h) fun suf => ?_ + refine HT.bind (Q := fun _ σ => RqDomPrio σ) (HT.getsThe _ fun _ h _ _ => h) fun rdy => ?_ + refine HT.bind (Q := fun _ σ => RqDomPrio σ) (HT.assert fun _ _ h => h) fun _ => ?_ + exact enqueue_rq t + | resumeCurThread => exact fin + | chooseNewThread => exact fin + +/-! ## `unbind_from_sc` -/ + +/-- If `t` is current, the action does not resume it. -/ +def NoResume (t : ObjRef) (σ : State Unit) : Prop := σ.curThread = t → σ.schedulerAction ≠ .resumeCurThread + +theorem schedContextUnbindTcb_fia (sc : ObjRef) : + HT (fun σ => FIA σ ∧ RqDomPrio σ) (schedContextUnbindTcb (Z := Unit) sc) (fun _ σ => FIA σ) := by + unfold schedContextUnbindTcb + have rd : ∀ {β : Type} {m : SMonad Unit β}, TPres m → CPres m → + HT (fun σ => FIA σ ∧ RqDomPrio σ) m (fun _ σ => FIA σ ∧ RqDomPrio σ) := + fun h1 h2 σ h r hr => ⟨h.1.ofTKeep (h1 σ r hr), h.2.ofCells (h2 σ r hr)⟩ + refine HT.bind (Q := fun _ σ => FIA σ ∧ RqDomPrio σ) (rd (by tpres) (by cpres)) fun x => ?_ + refine HT.bind (Q := fun _ σ => FIA σ ∧ RqDomPrio σ) (HT.assertOpt _ fun _ _ _ h => h) fun t => ?_ + refine HT.bind (Q := fun cur σ => (FIA σ ∧ RqDomPrio σ) ∧ cur = σ.curThread) + (HT.gets _ fun _ h => ⟨h, rfl⟩) fun cur => ?_ + refine HT.bind (Q := fun _ σ => FIA σ ∧ RqDomPrio σ ∧ NoResume t σ) ?_ fun _ => ?_ + · refine HT.when (fun _ => ?_) (fun hn σ h => ⟨h.1.1, h.1.2, fun e => absurd (h.2.trans e).symm hn⟩) + intro σ h r hr + have a := reschedX (fun _ => False) (fun _ => False) (fun _ => True) (fun _ _ h => h) (fun _ _ h => h) + σ ⟨h.1.1.toX _ _, trivial⟩ r hr + have b := reschedule_rq σ h.1.2 r hr + exact ⟨a.1.toA, b.1, fun _ => by rw [b.2]; intro e; cases e⟩ + refine HT.bind (Q := fun _ σ => FIA σ ∧ NoResume t σ ∧ NoQ t σ) + (HT.mono (deqD t (NoResume t) (fun _ _ h => h)) (fun _ h => h) (fun _ _ h => h)) fun _ => ?_ + refine HT.bind (Q := fun _ σ => FIA σ ∧ (NoResume t σ ∧ NoQ t σ) ∧ t ∉ σ.releaseQueue) + (HT.mono (relRemoveD t (fun σ => NoResume t σ ∧ NoQ t σ) (fun _ _ h => h) (fun _ _ h => h)) + (fun _ h => h) (fun _ _ h => h)) fun _ => ?_ + refine HT.bind (Q := fun _ σ => FIX (fun u => u = t) (fun _ => False) σ ∧ NoResume t σ) + (HT.mono (setScNoneD t (NoResume t) (fun _ _ _ _ _ _ h => h)) (fun _ h => ⟨h.1, h.2.1.1, h.2.1.2, h.2.2⟩) + (fun _ _ h => h)) fun _ => ?_ + refine HT.mono (P := FIA) (HT.tp (by tpres)) (fun σ h => ?_) (fun _ _ h => h) + by_cases e : σ.curThread = t + · exact ⟨⟨h.1.1.rq, fun e1 => absurd e1 (h.2 e), fun u x s a b c => h.1.1.blk u x s a b c id⟩, h.1.2⟩ + · exact h.1.toA' e + +theorem unbindFromSc_schedValid (t : ObjRef) : + HT (fun σ => SchedValidA σ ∧ RqDomPrio σ) (unbindFromSc (Z := Unit) t) (fun _ σ => SchedValidA σ) := by + refine HT.mono (P := fun σ => FIA σ ∧ RqDomPrio σ) (Q := fun _ σ => FIA σ) ?_ + (fun _ h => ⟨FIA.ofValid h.1, h.2⟩) (fun _ _ h => h.toValid) + unfold unbindFromSc + refine HT.bind (Q := fun _ σ => FIA σ ∧ RqDomPrio σ) + (fun σ h r hr => ?_) fun o => ?_ + · have h1 : TPres (getTcbObjRef (Z := Unit) Tcb.tcbSchedContext t) := by unfold getTcbObjRef; tpres + have h2 : CPres (getTcbObjRef (Z := Unit) Tcb.tcbSchedContext t) := by unfold getTcbObjRef; cpres + exact ⟨h.1.ofTKeep (h1 σ r hr), h.2.ofCells (h2 σ r hr)⟩ + refine HT.bind (Q := fun _ σ => FIA σ) ?_ fun _ => HT.tp ?_ + · cases o with + | none => exact HT.pure _ fun _ h => h.1 + | some sc => exact schedContextUnbindTcb_fia sc + · unfold completeYieldTo setConsumed schedContextUpdateConsumed schedContextCancelYieldTo + NonDet.maybeM + tpres + +/-! ## `unbind_notification` -/ + +theorem unbindNotification_schedValid (t : ObjRef) : + HT (fun σ => SchedValidA σ ∧ RqDomPrio σ) (unbindNotification (Z := Unit) t) + (fun _ σ => SchedValidA σ ∧ RqDomPrio σ) := by + refine HT.mono (P := fun σ => FIA σ ∧ RqDomPrio σ) (Q := fun _ σ => FIA σ ∧ RqDomPrio σ) ?_ + (fun _ h => ⟨FIA.ofValid h.1, h.2⟩) (fun _ _ h => ⟨h.1.toValid, h.2⟩) + unfold unbindNotification + have rd : ∀ {β : Type} {m : SMonad Unit β}, TPres m → CPres m → + HT (fun σ => FIA σ ∧ RqDomPrio σ) m (fun _ σ => FIA σ ∧ RqDomPrio σ) := + fun h1 h2 σ h r hr => ⟨h.1.ofTKeep (h1 σ r hr), h.2.ofCells (h2 σ r hr)⟩ + refine HT.bind (Q := fun _ σ => FIA σ ∧ RqDomPrio σ) + (rd (by unfold getTcbObjRef; tpres) (by unfold getTcbObjRef; cpres)) fun o => ?_ + cases o with + | none => exact HT.pure _ fun _ h => h + | some n => + show HT _ (doUnbindNotification (Z := Unit) n t) _ + unfold doUnbindNotification + refine HT.bind (Q := fun _ σ => FIA σ ∧ RqDomPrio σ) + (rd (by unfold setNtfnObjRef updateSkObjRef; tpres) (by unfold setNtfnObjRef updateSkObjRef; cpres)) + fun _ => ?_ + intro σ h r hr + have tk : TPres (setTcbObjRef (Z := Unit) tcbBoundNotificationUpdate t none) := TPresInst.pres + refine ⟨h.1.ofTKeep (tk σ r hr), ?_⟩ + obtain ⟨x, hx, e⟩ := threadSet_spec hr + rw [e] + exact h.2.ofTcbRepl hx rfl rfl + +/-- **The Thread finaliser's three spec steps** in order (`fl4FinaliseCap`'s `threadCap` arm). -/ +theorem threadFinPrefix_schedValid (t : ObjRef) : + HT (fun σ => SchedValidA σ ∧ RqDomPrio σ) + (do unbindNotification (Z := Unit) t + unbindFromSc t + Fl4SpecA64.suspend t) + (fun _ σ => SchedValidA σ) := + HT.bind (Q := fun _ σ => SchedValidA σ ∧ RqDomPrio σ) (unbindNotification_schedValid t) fun _ => + HT.bind (Q := fun _ σ => SchedValidA σ) (unbindFromSc_schedValid t) fun _ => suspend_schedValid t + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3DelSvThrA.lean b/proofs/Fl4ProofsA64/P3/Stage3DelSvThrA.lean new file mode 100644 index 000000000..4972fceb7 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3DelSvThrA.lean @@ -0,0 +1,394 @@ +/- +P3 (AArch64, delete hooks, contract §89): **`SchedValidA` across `fl4CancelRun`, `fl4VcpuEndRun` and +`prepareThreadDelete`**, the Thread finaliser's spec side, and its hook at `DelWTopA` / `DelWxTopA`. + +`SvK`: queues and current / idle thread kept; the scheduler action kept or `chooseNewThread`; every TCB keeps its SC, +domain and priority and keeps its state or becomes unblocked; every other object but `ArchObj`s kept. None of the three +enqueues anything (ending a run only sets the thread `Running`), so no active-SC fact is needed for a `runningVM` +thread. + +* **`SchedValidA.ofSvK`**, **`RqDomPrio.ofSvK`**; `NdSvKA` with its closure lemmas (`Stage3DelSvAkA`'s framework); +* **`fl4CancelRun_svK`**, **`fl4VcpuEndRun_svK`**, **`prepareThreadDelete_svK`**; +* **`threadFin_schedValid`**; +* **`FinHookKA.topSvA`** (a hook whose window carries `SchedValidA`, lifted) and **`finHookThreadSvA`** + (applied to `finHookThreadA`, whose S4 hypotheses stay the caller's). + +No named premise of its own. New module. +-/ +import Fl4ProofsA64.P3.Stage3DelSvReplyA +import Fl4ProofsA64.P3.Stage3DelSvSuspendA +import Fl4ProofsA64.P3.DelThrTimeA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +set_option maxHeartbeats 1600000 + +/-- What a TCB write keeps. -/ +def TcbSvK (x x' : Tcb) : Prop := + x'.tcbSchedContext = x.tcbSchedContext ∧ x'.tcbDomain = x.tcbDomain ∧ x'.tcbPriority = x.tcbPriority ∧ + (BlockedTsA x'.tcbState = true → x'.tcbState = x.tcbState) + +theorem TcbSvK.refl (x : Tcb) : TcbSvK x x := ⟨rfl, rfl, rfl, fun _ => rfl⟩ + +theorem TcbSvK.trans {a b c : Tcb} (h1 : TcbSvK a b) (h2 : TcbSvK b c) : TcbSvK a c := by + refine ⟨h2.1.trans h1.1, h2.2.1.trans h1.2.1, h2.2.2.1.trans h1.2.2.1, fun h => ?_⟩ + have e := h2.2.2.2 h + rw [e] at h ⊢ + exact h1.2.2.2 h + +/-- The object changes of an `SvK` step. -/ +inductive SvObjA : Option KernelObject → Option KernelObject → Prop + | same (o : Option KernelObject) : SvObjA o o + | arch (a a' : ArchKernelObj) : SvObjA (some (.archObj a)) (some (.archObj a')) + | tcb (x x' : Tcb) (h : TcbSvK x x') : SvObjA (some (.tcb x)) (some (.tcb x')) + +theorem SvObjA.trans {a b c : Option KernelObject} (h1 : SvObjA a b) (h2 : SvObjA b c) : SvObjA a c := by + cases h1 with + | same => exact h2 + | arch x y => + cases h2 with + | same => exact .arch _ _ + | arch _ z => exact .arch _ _ + | tcb x y h => + cases h2 with + | same => exact .tcb _ _ h + | tcb _ z h' => exact .tcb _ _ (h.trans h') + +/-- **A step keeping what `SchedValidA` and `RqDomPrio` read** (module docstring). -/ +structure SvK (σ σ' : State Unit) : Prop where + rq : σ'.readyQueues = σ.readyQueues + rls : σ'.releaseQueue = σ.releaseQueue + act : σ'.schedulerAction = σ.schedulerAction ∨ σ'.schedulerAction = .chooseNewThread + cur : σ'.curThread = σ.curThread + idle : σ'.idleThread = σ.idleThread + kh : ∀ p, SvObjA (σ.kheap p) (σ'.kheap p) + +theorem svK_refl (σ : State Unit) : SvK σ σ := ⟨rfl, rfl, Or.inl rfl, rfl, rfl, fun _ => .same _⟩ + +theorem svK_trans {a b c : State Unit} (h1 : SvK a b) (h2 : SvK b c) : SvK a c := by + refine ⟨h2.rq.trans h1.rq, h2.rls.trans h1.rls, ?_, h2.cur.trans h1.cur, h2.idle.trans h1.idle, + fun p => (h1.kh p).trans (h2.kh p)⟩ + rcases h2.act with e | e + · rw [e]; exact h1.act + · exact Or.inr e + +theorem AkA.svK {σ σ' : State Unit} (h : AkA σ σ') : SvK σ σ' := by + refine ⟨h.rq, h.rls, Or.inl h.act, h.cur, h.idle, fun p => ?_⟩ + rcases h.kh p with e | ⟨a, a', e, e'⟩ + · rw [e]; exact .same _ + · rw [e, e']; exact .arch _ _ + +theorem SvK.tcbFwd {σ σ' : State Unit} (H : SvK σ σ') {p : ObjRef} {x : Tcb} (e : σ.kheap p = some (.tcb x)) : + ∃ x', σ'.kheap p = some (.tcb x') ∧ TcbSvK x x' := by + have h := H.kh p + rw [e] at h + generalize σ'.kheap p = o at h ⊢ + cases h with + | same => exact ⟨x, rfl, .refl x⟩ + | tcb _ x' h => exact ⟨x', rfl, h⟩ + +theorem SvK.tcbBack {σ σ' : State Unit} (H : SvK σ σ') {p : ObjRef} {x' : Tcb} (e : σ'.kheap p = some (.tcb x')) : + ∃ x, σ.kheap p = some (.tcb x) ∧ TcbSvK x x' := by + have h := H.kh p + rw [e] at h + generalize σ.kheap p = o at h ⊢ + cases h with + | same => exact ⟨x', rfl, .refl x'⟩ + | tcb x _ h => exact ⟨x, rfl, h⟩ + +theorem SvK.scFwd {σ σ' : State Unit} (H : SvK σ σ') {p : ObjRef} {y : SchedContext} {k : Nat} + (e : σ.kheap p = some (.schedContext y k)) : σ'.kheap p = some (.schedContext y k) := by + have h := H.kh p + rw [e] at h + generalize σ'.kheap p = o at h ⊢ + cases h with + | same => rfl + +theorem SchedValidA.ofSvK {σ σ' : State Unit} (h : SchedValidA σ) (H : SvK σ σ') : SchedValidA σ' := by + have act : ∀ q, ScActiveAtA σ q → ScActiveAtA σ' q := fun q ⟨y, k, e, a⟩ => ⟨y, k, H.scFwd e, a⟩ + have actT : ∀ t, (∃ x sc, σ.kheap t = some (.tcb x) ∧ x.tcbSchedContext = some sc ∧ ScActiveAtA σ sc) → + ∃ x sc, σ'.kheap t = some (.tcb x) ∧ x.tcbSchedContext = some sc ∧ ScActiveAtA σ' sc := by + rintro t ⟨x, sc, e, es, a⟩ + obtain ⟨x', e', k⟩ := H.tcbFwd e + exact ⟨x', sc, e', k.1.trans es, act _ a⟩ + refine ⟨fun d p t hm => ?_, fun e1 e2 => ?_, fun t x' sc e1 e2 e3 => ?_, fun t ht => ?_⟩ + · rw [H.rq] at hm + obtain ⟨a, nr⟩ := h.rq d p t hm + exact ⟨actT t a, by rw [H.rls]; exact nr⟩ + · have ea : σ.schedulerAction = .resumeCurThread := by + rcases H.act with e | e + · rw [← e]; exact e1 + · rw [e] at e1; cases e1 + rw [H.cur, H.idle] at e2 + obtain ⟨a, nr⟩ := h.act ea e2 + rw [H.cur, H.rls] + exact ⟨actT _ a, nr⟩ + · obtain ⟨x, e, k⟩ := H.tcbBack e1 + have es := k.2.2.2 e2 + exact act _ (h.blk t x sc e (es ▸ e2) (k.1 ▸ e3)) + · rw [H.rls] at ht + exact actT t (h.rls t ht) + +theorem RqDomPrio.ofSvK {σ σ' : State Unit} (h : RqDomPrio σ) (H : SvK σ σ') : RqDomPrio σ' := by + intro f d p x' hx' hm + obtain ⟨x, e, k⟩ := H.tcbBack hx' + rw [H.rq] at hm + rw [k.2.1, k.2.2.1] + exact h f d p x e hm + +/-- **Every outcome keeps `SvK`**. -/ +def NdSvKA {α : Type} (m : NonDet (State Unit) α) : Prop := ∀ σ r, r ∈ (m σ).1 → SvK σ r.2 + +section +variable {m : Fl4SpecA64.McState} +variable {α β : Type} + +theorem NdAkA.svK {m : NonDet (State Unit) α} (h : NdAkA m) : NdSvKA m := fun σ r hr => (h σ r hr).svK + +theorem NdSvKA.bind {m : NonDet (State Unit) α} {f : α → NonDet (State Unit) β} (hm : NdSvKA m) + (hf : ∀ a, NdSvKA (f a)) : NdSvKA (m >>= f) := fun σ r hr => by + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + exact svK_trans (hm σ x hx) (hf x.1 x.2 r hr) + +theorem NdSvKA.nbind {m : NonDet (State Unit) α} {f : α → NonDet (State Unit) β} (hm : NdSvKA m) + (hf : ∀ a, NdSvKA (f a)) : NdSvKA (NonDet.bind m f) := NdSvKA.bind hm hf + +theorem NdSvKA.when {P : Prop} {inst : Decidable P} {m : NonDet (State Unit) Unit} (h : NdSvKA m) : + NdSvKA (NonDet.when P m) := by + unfold NonDet.when; split + · exact h + · exact (NdAkA.npure _).svK + +theorem NdSvKA.mapM {f : α → NonDet (State Unit) β} (h : ∀ a, NdSvKA (f a)) : + ∀ l : List α, NdSvKA (NonDet.mapM f l) + | [] => (NdAkA.npure _).svK + | a :: l => NdSvKA.nbind (h a) fun _ => NdSvKA.nbind (NdSvKA.mapM h l) fun _ => (NdAkA.npure _).svK + +theorem NdSvKA.mapMx {f : α → NonDet (State Unit) β} (h : ∀ a, NdSvKA (f a)) : + ∀ l : List α, NdSvKA (NonDet.mapMx f l) + | [] => (NdAkA.npure _).svK + | a :: l => NdSvKA.nbind (h a) fun _ => NdSvKA.mapMx h l + +theorem NdSvKA.zipWithMx {γ : Type} {f : α → β → NonDet (State Unit) γ} (h : ∀ a b, NdSvKA (f a b)) : + ∀ (l : List α) (l' : List β), NdSvKA (NonDet.zipWithMx f l l') + | a :: l, b :: l' => NdSvKA.nbind (h a b) fun _ => NdSvKA.zipWithMx h l l' + | [], _ => by unfold NonDet.zipWithMx; exact (NdAkA.npure _).svK + | _ :: _, [] => by unfold NonDet.zipWithMx; exact (NdAkA.npure _).svK + +theorem NdSvKA.modify {g : State Unit → State Unit} (h : ∀ σ, SvK σ (g σ)) : NdSvKA (NonDet.modify g) := + fun σ r hr => by rw [Set.mem_singleton_iff.1 hr]; exact h σ + +end + +theorem SvK.updTcb {σ : State Unit} {p : ObjRef} {x x' : Tcb} (hx : σ.kheap p = some (.tcb x)) (k : TcbSvK x x') : + SvK σ { σ with kheap := Function.update σ.kheap p (some (.tcb x')) } := by + refine ⟨rfl, rfl, Or.inl rfl, rfl, rfl, fun q => ?_⟩ + by_cases e : q = p + · subst e + show SvObjA _ (Function.update σ.kheap q _ q) + rw [Function.update_self, hx] + exact .tcb _ _ k + · show SvObjA _ (Function.update σ.kheap p _ q) + rw [Function.update_of_ne e] + exact .same _ + +theorem threadSet_svK (f : Tcb → Tcb) (hf : ∀ x, TcbSvK x (f x)) (p : ObjRef) : + NdSvKA (threadSet (Z := Unit) f p) := fun σ r hr => by + obtain ⟨x, hx, e⟩ := threadSet_memA f p hr + rw [e] + exact SvK.updTcb hx (hf x) + +theorem archThreadSet_svK (f : ArchTcb → ArchTcb) (p : ObjRef) : NdSvKA (archThreadSet (Z := Unit) f p) := by + have e : archThreadSet (Z := Unit) f p = threadSet (Z := Unit) (fun t => { t with tcbArch := f t.tcbArch }) p := rfl + rw [e] + refine threadSet_svK _ (fun _ => ?_) p + exact ⟨rfl, rfl, rfl, fun _ => rfl⟩ + +theorem asUser_svK {α : Type} (p : ObjRef) (f : UserMonad α) : NdSvKA (asUser (Z := Unit) p f) := fun σ r hr => by + unfold asUser at hr + obtain ⟨g, hg, hr⟩ := mem_bindA.1 hr + change g ∈ (NonDet.assertOpt (getTcb (Z := Unit) p σ) σ).1 at hg + cases hgt : getTcb (Z := Unit) p σ with + | none => rw [hgt] at hg; exact absurd hg (Set.notMem_empty _) + | some x => + rw [hgt] at hg + obtain rfl : g = (x, σ) := hg + have hx := getTcb_kheapA hgt + dsimp only at hr + obtain ⟨u, hu, hr⟩ := mem_bindA.1 hr + obtain rfl : u = (archTcbContextGet x.tcbArch, σ) := hu + dsimp only at hr + obtain ⟨v, hv, hr⟩ := mem_bindA.1 hr + have ev : v.2 = σ := hv.2 + obtain ⟨w, hw, hr⟩ := mem_bindA.1 hr + obtain rfl : w = ({ x with tcbArch := archTcbContextSet v.1.2 x.tcbArch }, v.2) := hw + dsimp only at hr + obtain ⟨z, hz, hr⟩ := mem_bindA.1 hr + have er : r.2 = z.2 := congrArg Prod.snd (show r = (v.1.1, z.2) from hr) + rw [ev] at hz + obtain ⟨x', ex'⟩ : ∃ x' : Tcb, x' = { x with tcbArch := archTcbContextSet v.1.2 x.tcbArch } := ⟨_, rfl⟩ + rw [← ex'] at hz + have ez : z.2 = { σ with kheap := Function.update σ.kheap p (some (.tcb x')) } := by + unfold setObject at hz + obtain ⟨o, ho, hz⟩ := mem_bindA.1 hz + obtain ⟨e1, -⟩ := getObject_mem ho + obtain ⟨y, hy, hz⟩ := mem_bindA.1 hz + obtain ⟨-, hy⟩ := hy + rw [hy] at hz + obtain ⟨q, hq, hz⟩ := mem_bindA.1 hz + obtain rfl : q = (o.2, o.2) := hq + have ez' : z = ((), { o.2 with kheap := Function.update o.2.kheap p (some (.tcb x')) }) := hz + rw [ez', e1] + rw [er, ez] + exact SvK.updTcb hx (by rw [ex']; exact ⟨rfl, rfl, rfl, fun _ => rfl⟩) + +theorem setSchedulerAction_choose_svK : NdSvKA (setSchedulerAction (Z := Unit) .chooseNewThread) := by + unfold setSchedulerAction + exact NdSvKA.modify fun _ => ⟨rfl, rfl, Or.inr rfl, rfl, rfl, fun _ => .same _⟩ + +theorem setThreadState_svK (t : ObjRef) (ts : ThreadState) (h : BlockedTsA ts = false) : + NdSvKA (setThreadState (Z := Unit) t ts) := by + unfold setThreadState setThreadStateAct + refine NdSvKA.bind ?_ fun _ => ?_ + · refine threadSet_svK _ (fun _ => ?_) t + refine ⟨rfl, rfl, rfl, fun c => ?_⟩ + change BlockedTsA ts = true at c + rw [h] at c; cases c + refine NdSvKA.bind (NdAkA.gets _).svK fun _ => NdSvKA.bind (NdAkA.gets _).svK fun _ => + NdSvKA.bind (NdAkA.gets _).svK fun _ => NdSvKA.when setSchedulerAction_choose_svK + +theorem setVcpu_svK (p : ObjRef) (v : Vcpu) : NdSvKA (setVcpu (Z := Unit) p v) := by + unfold setVcpu; exact (setObject_akA _ _).svK + +/-- Walk a spec program at `SvK` (`DelThrTimeA`'s `tm` with these primitives). -/ +macro "svk" : tactic => `(tactic| repeat' first + | with_reducible exact (NdAkA.pure _).svK | with_reducible exact (NdAkA.npure _).svK + | with_reducible exact (NdAkA.gets _).svK | with_reducible exact NdAkA.get.svK + | with_reducible exact (NdAkA.assert _).svK | with_reducible exact (NdAkA.assertOpt _).svK + | with_reducible exact (NdAkA.getsThe _).svK | with_reducible exact (NdAkA.getsMap _ _).svK + | with_reducible exact (NdAkA.selectF _).svK | with_reducible exact NdAkA.fail.svK + | with_reducible exact (getObject_akA _).svK | with_reducible exact (doMachineOp_akA _).svK + | with_reducible exact (setVmRoot_akA _).svK | with_reducible exact setVcpu_svK _ _ + | with_reducible exact asUser_svK _ _ | with_reducible exact archThreadSet_svK _ _ + | with_reducible exact setThreadState_svK _ _ rfl + | with_reducible refine threadSet_svK _ (fun _ => ?_) _ + | exact ⟨rfl, rfl, rfl, fun _ => rfl⟩ + | with_reducible refine NdSvKA.modify (fun _ => ⟨rfl, rfl, Or.inl rfl, rfl, rfl, fun _ => SvObjA.same _⟩) + | with_reducible refine NdSvKA.when ?_ + | with_reducible refine NdSvKA.mapMx (fun _ => ?_) _ | with_reducible refine NdSvKA.mapM (fun _ => ?_) _ + | with_reducible refine NdSvKA.zipWithMx (fun _ _ => ?_) _ _ + | with_reducible refine NdSvKA.bind ?_ (fun _ => ?_) | with_reducible refine NdSvKA.nbind ?_ (fun _ => ?_) + | split + | unfold NonDet.maybeM | unfold NonDet.liftM + | unfold threadGet | unfold getTcb | unfold getRunVcpu | unfold getVcpu | unfold fl4VcpuExtUpdate + | unfold setRunVcpu | unfold fl4CancelRun | unfold getThreadState | unfold dsb | unfold readVCPUHardwareReg + | unfold vcpuUpdate | unfold vcpuSaveReg | unfold getGicVcpuCtrlHcr | unfold vgicUpdate + | unfold writeVCPUHardwareReg | unfold checkExportArchTimer | unfold disableVirtTimer + | unfold getGicVcpuCtrlVmcr | unfold getGicVcpuCtrlApr | unfold getGicVcpuCtrlLr | unfold vgicUpdateLr + | unfold vcpuSaveRegRange | unfold vcpuSave | unfold setGicVcpuCtrlHcr | unfold setSCTLR | unfold writeTCR + | unfold setHCR | unfold isb | unfold enableFpuEL01 | unfold maskInterrupt | unfold vcpuDisable + | unfold vcpuInvalidateActive | unfold vcpuFlush | unfold fl4VcpuFlush | unfold getCap | unfold fl4VcpuGet + | unfold storeWordOffs | unfold copyRegsToFrame | unfold fl4ReadStateToFrame | unfold setMessageInfo + | unfold fl4CompleteRun | unfold fl4VcpuEndRun | unfold archThreadGet | unfold dissociateVcpuTcb + | unfold enableFpu | unfold readFpuState | unfold setFPUState | unfold saveFpuState | unfold disableFpu + | unfold getFPUState | unfold writeFpuState | unfold loadFpuState | unfold setArmCurrentFpuOwner + | unfold switchLocalFpuOwner | unfold fpuRelease | unfold prepareThreadDelete) + +theorem fl4CancelRun_svK (t : ObjRef) : NdSvKA (fl4CancelRun (Z := Unit) t) := by unfold fl4CancelRun; svk + +theorem fl4VcpuEndRun_svK (v : ObjRef) : NdSvKA (fl4VcpuEndRun (Z := Unit) v) := by unfold fl4VcpuEndRun; svk + +theorem prepareThreadDelete_svK (t : ObjRef) : NdSvKA (prepareThreadDelete (Z := Unit) t) := by + unfold prepareThreadDelete; svk + +theorem HT.svk {α : Type} {m : SMonad Unit α} (h : NdSvKA m) : + HT (fun σ => SchedValidA σ ∧ RqDomPrio σ) m (fun _ σ => SchedValidA σ ∧ RqDomPrio σ) := + fun σ hσ r hr => ⟨SchedValidA.ofSvK hσ.1 (h σ r hr), RqDomPrio.ofSvK hσ.2 (h σ r hr)⟩ + +/-- **The Thread finaliser's spec side keeps `SchedValidA`** (module docstring). -/ +theorem threadFin_schedValid (t : ObjRef) (d : Bool) : + HT (fun σ => SchedValidA σ ∧ RqDomPrio σ) (fl4FinaliseCap (Z := Unit) (.threadCap t) d) + (fun _ σ => SchedValidA σ) := by + simp only [fl4FinaliseCap, finaliseCap] + refine HT.bind (Q := fun _ σ => SchedValidA σ ∧ RqDomPrio σ) + (HT.when (fun _ => HT.svk ?_) (fun _ _ h => h)) fun _ => ?_ + · refine NdSvKA.bind (fl4CancelRun_svK t) fun _ => NdSvKA.bind (by unfold archThreadGet; svk) fun o => ?_ + cases o with + | none => exact (NdAkA.pure _).svK + | some v => exact fl4VcpuEndRun_svK v + refine HT.bind (Q := fun _ σ => SchedValidA σ ∧ RqDomPrio σ) + (HT.when (fun _ => unbindNotification_schedValid t) (fun _ _ h => h)) fun _ => ?_ + refine HT.bind (Q := fun _ σ => SchedValidA σ) + (HT.when (fun _ => unbindFromSc_schedValid t) (fun _ _ h => h.1)) fun _ => ?_ + refine HT.bind (Q := fun _ σ => SchedValidA σ) + (HT.when (fun _ => suspend_schedValid t) (fun _ _ h => h)) fun _ => ?_ + refine HT.bind (Q := fun _ σ => SchedValidA σ) + (HT.when (fun _ σ h r hr => h.ofSvK (prepareThreadDelete_svK t σ r hr)) (fun _ _ h => h)) fun _ => ?_ + exact HT.pure _ fun _ h => h + +section +variable {m : Fl4SpecA64.McState} +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Pp Tb : Set U64} {Lv : U64 → Nat} {ia ie : Nat} + {N E Rp : Set U64} {κ : U64 → U64} {n : Nat} {ipa : U64} {Clock : A → MachineState → Prop} {s0 : S} + {Rd Rz : S → A → State Unit → MdbGhostA → U64 → Prop} + +variable {Ag : Set U64} + +/-- **`FinHookKA.topA` for a hook whose window carries `SchedValidA`**. -/ +theorem FinHookKA.topSvA {k : FinKindA} + (H' : FinHookKA st k ar Z.th OS fl4FinaliseCap + (fun s a σ G => (DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) + (J7LA st (J7R2A st ar ipa))) s a σ G ∧ DelPpTimeA Clock a σ) ∧ SchedValidA σ) + (fun s a σ G => DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) + (J7LA st J7R2tA)) s a σ G ∧ DelPpTimeA Clock a σ) Rd Rz) + (hS : ∀ (s : S) (a : A) (σ : State Unit) (G : MdbGhostA) (sl : CslotPtr) (c : capability.Cap) (ac : Cap), + DStateA st Z.th OS s σ G → DelWTopGA st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar ipa Clock s0 s a σ G → + AbsSlotA Z.th sl → capA st s (cslotAddrA sl) = c → finKindA c = some k → CapEqA c ac → + ∀ (d : Bool) (x : Cap × Cap) (σ' : State Unit), (d = true ↔ isFinalCap' ac σ) → + (x, σ') ∈ (fl4FinaliseCap (Z := Unit) ac d σ).1 → SchedValidA σ') : + FinHookKA st k ar Z.th OS fl4FinaliseCap + (DelWTopGA st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar ipa Clock s0) + (DelWxTopGA st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m Clock s0) Rd Rz := by + intro s a σ G sl c ac f D hW hsl hc hk hg hac hf + have hW0 := hW + obtain ⟨⟨sc, X, sv⟩, T⟩ := hW0 + refine WP.spec_mono (H' s a σ G sl c ac f D ⟨⟨⟨sc, X⟩, T⟩, sv⟩ hsl hc hk hg hac hf) ?_ + rintro r ⟨h1, h2, h3, σ', G', ⟨d, hd, hmem⟩, D', hc', hoth, ⟨hWn, hWx⟩, hRd, hRz⟩ + refine ⟨h1, h2, h3, σ', G', ⟨d, hd, hmem⟩, D', hc', hoth, ⟨fun h => ?_, fun h => ?_⟩, hRd, hRz⟩ + · obtain ⟨⟨⟨sc', X'⟩, T'⟩, sv'⟩ := hWn h + exact ⟨⟨sc', X', sv'⟩, T'⟩ + · obtain ⟨⟨sc', X'⟩, T'⟩ := hWx h + exact ⟨⟨sc', X', hS s a σ G sl c ac D hW hsl hc hk hac d _ σ' hd hmem⟩, T'⟩ + +/-- **The Thread hook at `DelWTopA`**, from `finHookThreadA`. -/ +theorem finHookThreadSvA + (H' : FinHookKA st .thread ar Z.th OS fl4FinaliseCap + (fun s a σ G => (DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) + (J7LA st (J7R2A st ar ipa))) s a σ G ∧ DelPpTimeA Clock a σ) ∧ SchedValidA σ) + (fun s a σ G => DelJWQA st s0 (DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) + (J7LA st J7R2tA)) s a σ G ∧ DelPpTimeA Clock a σ) Rd Rz) : + FinHookKA st .thread ar Z.th OS fl4FinaliseCap + (DelWTopGA st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar ipa Clock s0) + (DelWxTopGA st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m Clock s0) Rd Rz := by + refine FinHookKA.topSvA H' ?_ + intro s a σ G sl c ac D hW hsl hc hk hac d x σ' hd hmem + obtain ⟨t, rfl⟩ : ∃ t, c = .Thread t := by + cases c <;> simp [finKindA] at hk + exact ⟨_, rfl⟩ + have eac : ac = .threadCap (specPtr t) := (show absCapA (.Thread t) = ac from hac).symm + rw [eac] at hmem + obtain ⟨⟨-, ⟨⟨Q, R, hw, Ac, Bf, stack, J, -⟩, -⟩, sv⟩, -⟩ := hW + exact threadFin_schedValid (specPtr t) d σ ⟨sv, rqDomPrio_of_schedA J.sr⟩ (x, σ') hmem + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3DelTopA.lean b/proofs/Fl4ProofsA64/P3/Stage3DelTopA.lean new file mode 100644 index 000000000..b8017997c --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3DelTopA.lean @@ -0,0 +1,212 @@ +/- +P3 (AArch64, delete hooks, the twin top; contract §84, Architect ruling 03:53Z): **CNode Delete / Revoke at a syscall +entry**, `JSyscallA → JStage3A` (frozen text `handoff/delh-twintop-stmt.lean`). + +* **`DelWTopA`** / **`DelWxTopA`**: the deletion's `W` / `Wx` + (the loop at `J7R2A` / at the relaxed `J7R2tA`, with the time facts `DelPpTimeA`); the Preempted outcome's `Wp` is + `W`. +* **`CnodeDelOutA`**: the outcome (twin of `InvPostDA`, ending at `JStage3A` at the entry's `McState` `m`, §91). +* **`JSyscallA.cnodeDeleteA`**, **`JSyscallA.cnodeRevokeA`**: `cnode_{delete,revoke}_refinesWA` at `W`, and A64 P1's + whole-decode `cnode.decode.rvDelQAt` (`DelIn` from `JStage3A.delIn`, `FinKZ` from R2) for `SysL` / `SymL` at the + final state; the final state's `W` record goes back to `JStage3A` by `DelXA6G.toStage3A`. + +Named premises: those of `cnode_{delete,revoke}_refinesWA` (`DelSiteA` [J], `hCan`, the hooks `Hk` at `W` / `Wx` / +`W`, `hXr` the loop invariant at the restarted state; Revoke's `hPA`), [P] `ArchTotal`, `ArchPlat`. New module. +-/ +import Fl4ProofsA64.P3.CnodeDecDelA +import Fl4ProofsA64.P3.DelRec6A +import Fl4ProofsA64.P3.DelPpA +import Fl4ProofsA64.P3.Stage3FinA +import Fl4ProofsA64.P3.Stage3RsSiteA +import Fl4ProofsA64.P3.Stage3SchedValidA +import Fl4ProofsA64.P3.Stage3DelXSchedValidA +import Fl4ProofsA64.P1.MdbCnDelArm +import Fl4ProofsA64.P1.S1aHiDelR + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +section +variable {m : Fl4SpecA64.McState} +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {ar : machine.Arch A} {Clock : A → Fl4SpecA64.MachineState → Prop} + +variable (st) in +/-- **`W`** of the deletion's `DelHypsA` with the frame-word guard `Ag`: the loop at `J7R2A` with the time facts. -/ +def DelWTopGA (Ag : Set U64) (AR : A → HwRootA → Prop) (Z : XSetsA) (H : Heap) (OS U Pp Tb : Set U64) (Lv : U64 → Nat) + (ia ie : Nat) (N E Rp : Set U64) (κ : U64 → U64) (n : Nat) (m : Fl4SpecA64.McState) (ar : machine.Arch A) (ipa : U64) + (Clock : A → Fl4SpecA64.MachineState → Prop) (s0 : S) : DelWA S A := fun s a σ G => + DelJWQA st s0 (DelXSVG st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st (J7R2A st ar ipa))) s a σ G ∧ + DelPpTimeA Clock a σ + +variable (st) in +/-- **`Wx`** with the guard `Ag`. -/ +def DelWxTopGA (Ag : Set U64) (AR : A → HwRootA → Prop) (Z : XSetsA) (H : Heap) (OS U Pp Tb : Set U64) + (Lv : U64 → Nat) (ia ie : Nat) (N E Rp : Set U64) (κ : U64 → U64) (n : Nat) (m : Fl4SpecA64.McState) + (Clock : A → Fl4SpecA64.MachineState → Prop) (s0 : S) : DelWA S A := fun s a σ G => + DelJWQA st s0 (DelXSVG st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st J7R2tA)) s a σ G ∧ + DelPpTimeA Clock a σ + +variable (st) in +/-- **`W`** of the deletion's `DelHypsA`: `DelWTopGA` at `Ag = ∅`. -/ +abbrev DelWTopA (AR : A → HwRootA → Prop) (Z : XSetsA) (H : Heap) (OS U Pp Tb : Set U64) (Lv : U64 → Nat) (ia ie : Nat) + (N E Rp : Set U64) (κ : U64 → U64) (n : Nat) (m : Fl4SpecA64.McState) (ar : machine.Arch A) (ipa : U64) + (Clock : A → Fl4SpecA64.MachineState → Prop) (s0 : S) : DelWA S A := + DelWTopGA st ∅ AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar ipa Clock s0 + +variable (st) in +/-- **`Wx`**: as `W` with the relaxed R2 (`J7R2tA`), the window of the emptying (`Ag = ∅`). -/ +abbrev DelWxTopA (AR : A → HwRootA → Prop) (Z : XSetsA) (H : Heap) (OS U Pp Tb : Set U64) (Lv : U64 → Nat) (ia ie : Nat) + (N E Rp : Set U64) (κ : U64 → U64) (n : Nat) (m : Fl4SpecA64.McState) + (Clock : A → Fl4SpecA64.MachineState → Prop) (s0 : S) : DelWA S A := + DelWxTopGA st ∅ AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m Clock s0 + +variable (st) in +/-- **The outcome of a CNode Revoke / Delete at a syscall entry** (twin of `InvPostDA`, ending at `JStage3A`). -/ +def CnodeDelOutA (AR : A → HwRootA → Prop) (Z : XSetsA) (H : Heap) (OS U Pp Tb : Set U64) (Lv : U64 → Nat) + (ia ie : Nat) (N E Rp : Set U64) (κ : U64 → U64) + (n : Nat) (m : Fl4SpecA64.McState) (ar : machine.Arch A) (ipa : U64) (s : S) (a : A) (σ : Fl4SpecA64.State Unit) + (msp : Fl4SpecA64.SEMonad Unit Fl4SpecA64.CnodeInvocation) : + core.result.Result Unit error.Exc × S × A → Prop + | (r, s3, a3) => + (∃ e e', r = .Err e ∧ s3 = s ∧ a3 = a ∧ msp.run σ = ({(.error e', σ)}, False) ∧ SysErrRelA e e') ∨ + ∃ inv σ1 σ3 G', msp.run σ = ({(.ok inv, σ)}, False) ∧ + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ((r = .Ok () ∧ (.ok (), σ3) ∈ ((Fl4SpecA64.fl4InvokeCnode (Z := Unit) inv).run σ1).1) ∨ + (r = .Err .Preempted ∧ (.error (), σ3) ∈ ((Fl4SpecA64.fl4InvokeCnode (Z := Unit) inv).run σ1).1)) ∧ + ∃ (Q : U64 → U64 → List U64) (R : U64 → List U64) (hw : HwRootA) (Ac' : Set U64) + (Bf' : Set (U64 × Fl4SpecA64.Word)) (stack' : U64 → List U64), + JStage3A st AR { Z with G := G', Q := Q, R := R, hw := hw } H OS U Ac' Pp Tb Lv ia ie N E Rp Bf' stack' κ n + (J5A st) (J7A st (J7R2A st ar ipa)) s3 a3 σ3 m + +/-- **From the deletion's post at `W = Wp = DelWTopA` and `SysL` / `SymL` at the final state, the outcome**. -/ +theorem cnodeDelOut_ofA {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {ipa : U64} {s0 : S} + {msp : Fl4SpecA64.SEMonad Unit Fl4SpecA64.CnodeInvocation} {r : core.result.Result Unit error.Exc} {s3 : S} + {a3 : A} + (h : InvPostDA st AR Z OS σ msp s a (DelWTopA st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar ipa Clock s0) + (DelWTopA st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar ipa Clock s0) (r, s3, a3)) + (sysl : SysL st H Z.Cs OS ia ie s3) (symL : SymL st H Z.Cs s3) : + CnodeDelOutA st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar ipa s a σ msp (r, s3, a3) := by + rcases h with ⟨e, hr, hs, ha, e', hrun, herr⟩ | ⟨inv, hinv, σ1, hset, σ', G', hres, -, hW⟩ + · exact Or.inl ⟨e, e', hr, hs, ha, hrun, herr⟩ + · have hW' : DelWTopA st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar ipa Clock s0 s3 a3 σ' G' := by + rcases hres with ⟨rfl, -⟩ | ⟨rfl, -⟩ <;> exact hW + obtain ⟨⟨-, X, sv⟩, -⟩ := hW' + obtain ⟨Q, R, hw, Ac', Bf', stack', J'⟩ := X.toStage3A sysl symL sv + exact Or.inr ⟨inv, σ1, σ', G', hinv, hset, hres, Q, R, hw, Ac', Bf', stack', J'⟩ + +/-- **`SysL` and `SymL` after `cnode::decode` at label 21 / 22** (A64 P1's `cnode.decode.rvDelQAt`, its `DelIn` / +`FinKZ` read off the syscall entry's record). -/ +theorem JSyscallA.cnodeRvDelSysA {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + {slot : U64} (hsl : slot ∈ OS) (hcov : SlotCov st OS s slot) + {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) {cap : capability.Cap} (hcap : capA st s slot = cap) + {lbl length buf : U64} (hl : lbl = fl4_abi.label.CNodeRevoke ∨ lbl = fl4_abi.label.CNodeDelete) + {r : core.result.Result Unit error.Exc} {s3 : S} {a3 : A} + (hdec : cnode.decode st ar s a lbl length cap buf extra = ok (r, s3, a3)) : + SysL st H Z.Cs OS ia ie s3 ∧ SymL st H Z.Cs s3 := by + obtain ⟨Pt, Ub, Pp7, Tb7, Hr, Te, Kt, Lv7, Re, bits, KLv, W⟩ := J7A.unp J.j7 + have I := J.toJStage3A.delIn hA hPl W + have hKZ := (HiDelTyR.ofR2 W.r2).finKZ I + have hPI : PoolIdx st OS s := Fl4ProofsA64.P3.S1aPoolCaps.poolIdxA W.r2.poolCaps + have D := J.decPreA hsl hext + rw [hcap] at D + have P := J.toJStage3A.sysPlace hext hsl hcov + have hrd : obj.Slot.cap st slot s = ok cap := by rw [capA_eq s slot, hcap] + have q := post_of_okA (cnode.decode.rvDelQAt I hKZ hPI D P J.sys.sysl J.symL hrd a hl) hdec + exact ⟨q.1.1, q.1.2.1⟩ + +set_option maxHeartbeats 4000000 in +/-- **`JStage3A` across CNode Delete** (label 22): `cnode.decode` (decode, `set_thread_state Restart`, +`fl4_invoke_cnode` = `fl4CapDelete`, the deletion loop with its finalisers, `IRQHandler` caps included) at a syscall +entry. Hypotheses: the decode-site facts (`hcov` / `hsl`, `site`, `hCan`, `hT` / `hsub` / `hnul`), `Hk` the hooks at +`W` / `Wx` / `W` (`DelWTopA` / `DelWxTopA`; the Preempted outcome is `Wp = W`), `hXr` the loop invariant at the +restarted state. -/ +theorem JSyscallA.cnodeDeleteA (s : S) (a : A) (σ : Fl4SpecA64.State Unit) (m : Fl4SpecA64.McState) + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + {slot : U64} (hsl : slot ∈ OS) (hcov : SlotCov st OS s slot) + {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hvs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → Aligned b ∧ b ≠ stA (L.curW s)) + (hvc : ∀ v ∈ Z.V, stA (L.curW s) ≠ v ∧ stA (L.curW s) ≠ core.num.U64.wrapping_add v 24#u64) + (hT : TreeA st OS Z.G.chains Z.G.par s) (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) (site : DelSiteA st Z.th OS s σ Z.G.chains) + (hCan : ∀ x ∈ OS, obj.sext48 x = ok x) {Rd Rz : S → A → Fl4SpecA64.State Unit → MdbGhostA → U64 → Prop} + (Hk : DelHypsA st ar H.tcbs Z.th OS Fl4SpecA64.fl4FinaliseCap + (DelWTopA st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar hPl.ipa Clock s) + (DelWxTopA st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m Clock s) + (DelWTopA st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar hPl.ipa Clock s) Rd Rz) + (hXr : ∀ x, σ.kheap σ.curThread = some (.tcb x) → + DelWTopA st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar hPl.ipa Clock s + (L.wr s (stA (L.curW s)) sched.TS_RESTART) a (stUpdA σ σ.curThread x .restart) Z.G) + (lbl : U64) (slbl : Fl4SpecA64.Data) (hlbl : Fl4SpecA64.genInvocationType slbl = cnodeLabelOfA lbl) + (hl : lbl.val = 22) (length buf : U64) (args : List U64) (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) + (cap : capability.Cap) (ac : Fl4SpecA64.Cap) (hca : CapRelA cap ac) (hcv : CapValidA Z.th cap) + (hcap : capA st s slot = cap) (excaps : List Fl4SpecA64.Cap) + {r : core.result.Result Unit error.Exc} {s3 : S} {a3 : A} + (hdec : cnode.decode st ar s a lbl length cap buf extra = ok (r, s3, a3)) : + CnodeDelOutA st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar hPl.ipa s a σ + (Fl4SpecA64.decodeCnodeInvocation slbl (args.map UScalar.bv) ac excaps) (r, s3, a3) := by + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hsl hcov J.actW J.running J.cur + have hl' : lbl = fl4_abi.label.CNodeDelete := by rw [u64_eq_iffA]; simp [hl] + have sy := J.cnodeRvDelSysA hA hPl hsl hcov hext hcap (Or.inr hl') hdec + have post := post_of_okA (cnode_delete_refinesWA ar J.toJEntA (J.toJStage3A.sysPlace hext hsl hcov) J.symL + J.running RS.cur hvs hvc hT hsub hnul site hCan Hk (fun x hkx _ => hXr x hkx) lbl slbl hlbl hl length buf args + hlen hargs cap ac hca hcv excaps) hdec + exact cnodeDelOut_ofA post sy.1 sy.2 + +set_option maxHeartbeats 4000000 in +/-- **`JStage3A` across CNode Revoke** (label 21): as `JSyscallA.cnodeDeleteA` (`fl4CapRevoke`), with `hPA`. -/ +theorem JSyscallA.cnodeRevokeA (s : S) (a : A) (σ : Fl4SpecA64.State Unit) (m : Fl4SpecA64.McState) + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + {slot : U64} (hsl : slot ∈ OS) (hcov : SlotCov st OS s slot) + {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hvs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → Aligned b ∧ b ≠ stA (L.curW s)) + (hvc : ∀ v ∈ Z.V, stA (L.curW s) ≠ v ∧ stA (L.curW s) ≠ core.num.U64.wrapping_add v 24#u64) + (hT : TreeA st OS Z.G.chains Z.G.par s) (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) (site : DelSiteA st Z.th OS s σ Z.G.chains) + (hCan : ∀ x ∈ OS, obj.sext48 x = ok x) {Rd Rz : S → A → Fl4SpecA64.State Unit → MdbGhostA → U64 → Prop} + (Hk : DelHypsA st ar H.tcbs Z.th OS Fl4SpecA64.fl4FinaliseCap + (DelWTopA st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar hPl.ipa Clock s) + (DelWxTopA st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m Clock s) + (DelWTopA st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar hPl.ipa Clock s) Rd Rz) + (hPA : ∀ s' a' σ' G', DStateA st Z.th OS s' σ' G' → + DelWTopA st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar hPl.ipa Clock s s' a' σ' G' → ParentAdjA st G'.chains G'.par s') + (hXr : ∀ x, σ.kheap σ.curThread = some (.tcb x) → + DelWTopA st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar hPl.ipa Clock s + (L.wr s (stA (L.curW s)) sched.TS_RESTART) a (stUpdA σ σ.curThread x .restart) Z.G) + (lbl : U64) (slbl : Fl4SpecA64.Data) (hlbl : Fl4SpecA64.genInvocationType slbl = cnodeLabelOfA lbl) + (hl : lbl.val = 21) (length buf : U64) (args : List U64) (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) + (cap : capability.Cap) (ac : Fl4SpecA64.Cap) (hca : CapRelA cap ac) (hcv : CapValidA Z.th cap) + (hcap : capA st s slot = cap) (excaps : List Fl4SpecA64.Cap) + {r : core.result.Result Unit error.Exc} {s3 : S} {a3 : A} + (hdec : cnode.decode st ar s a lbl length cap buf extra = ok (r, s3, a3)) : + CnodeDelOutA st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar hPl.ipa s a σ + (Fl4SpecA64.decodeCnodeInvocation slbl (args.map UScalar.bv) ac excaps) (r, s3, a3) := by + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hsl hcov J.actW J.running J.cur + have hl' : lbl = fl4_abi.label.CNodeRevoke := by rw [u64_eq_iffA]; simp [hl] + have sy := J.cnodeRvDelSysA hA hPl hsl hcov hext hcap (Or.inl hl') hdec + have post := post_of_okA (cnode_revoke_refinesWA ar J.toJEntA (J.toJStage3A.sysPlace hext hsl hcov) J.symL + J.running RS.cur hvs hvc hT hsub hnul site hCan Hk hPA (fun x hkx _ => hXr x hkx) lbl slbl hlbl hl length buf + args hlen hargs cap ac hca hcv excaps) hdec + exact cnodeDelOut_ofA post sy.1 sy.2 + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3DelXSchedValidA.lean b/proofs/Fl4ProofsA64/P3/Stage3DelXSchedValidA.lean new file mode 100644 index 000000000..8e1c9cd92 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3DelXSchedValidA.lean @@ -0,0 +1,124 @@ +/- +P3 (aarch64, A64-P3 Stage 3, T3 G4b / F8, delete hooks): **the record's `SchedValidA` across the cancel hooks**, and +`DelXSV`. + +Record edit #6 made `SchedValidA` (`P3/Stage3SchedValidA`) carry F8 (a) `rq`, (b) `act` on the current thread's own SC, +(c) `blk` and (d) `rls`; these are the four clauses of the temporary `FIA` of `P3/Stage3SchedValidStepA` +(`SchedValidB` ∧ release queue), field for field. `FIA.ofValid` / `FIA.toValid` are the two conversions (the Hoare +templates of items 1–4 keep working on the engineering form `FIA`; every statement at the hook boundary below is in +`SchedValidA`). + +* `cancelAllSignals_schedValid`, `cancelAllIpc_schedValid`, `cancelBadgedSends_schedValid`: from `SchedValidA` and the + one side premise `BAs queue σ` (the waiting queue's bound SCs are active), `SchedValidA` after the hook. + `BAs.ofBlockedV` discharges the premise from F8(c) and "the queue's threads are blocked". +* `DelXSV`: `DelXA6` with `SchedValidA` carried. `DelXA` (`JStage3TA`) does not state F8: `JStage3TA.toJA` takes + `schedValid` as a separate input, so the delete loop carries it (this module's hook lemmas maintain it). + +No premise; new module. +-/ +import Fl4ProofsA64.P3.Stage3DelSchedValidA +import Fl4ProofsA64.P3.Stage3DelReplyRemoveA +import Fl4ProofsA64.P3.DelRec6A +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 + +noncomputable section + +theorem Sched.FIA.ofValid {σ : State Unit} (h : SchedValidA σ) : Sched.FIA σ := + ⟨⟨fun d p t hm => by + obtain ⟨⟨x, sc, e, es, a⟩, n⟩ := h.rq d p t hm + exact ⟨⟨x, sc, e, es, a⟩, n⟩, + fun e1 e2 => by + obtain ⟨⟨x, sc, e, es, a⟩, n⟩ := h.act e1 e2 + exact ⟨⟨x, sc, e, es, a⟩, n⟩, + h.blk⟩, + fun t ht => by + obtain ⟨x, sc, e, es, a⟩ := h.rls t ht + exact ⟨x, sc, e, es, a⟩⟩ + +theorem Sched.FIA.toValid {σ : State Unit} (h : Sched.FIA σ) : SchedValidA σ := + ⟨fun d p t hm => by + obtain ⟨⟨x, sc, e, es, a⟩, n⟩ := h.1.rq d p t hm + exact ⟨⟨x, sc, e, es, a⟩, n⟩, + fun e1 e2 => by + obtain ⟨⟨x, sc, e, es, a⟩, n⟩ := h.1.act e1 e2 + exact ⟨⟨x, sc, e, es, a⟩, n⟩, + h.1.blk, + fun t ht => by + obtain ⟨x, sc, e, es, a⟩ := h.2 t ht + exact ⟨x, sc, e, es, a⟩⟩ + +namespace Sched + +/-- The side premise of the cancel hooks from F8(c): the queue's threads are blocked. -/ +theorem BAs.ofBlockedV {σ : State Unit} (h : SchedValidA σ) {q : List ObjRef} + (hb : ∀ t ∈ q, ∀ x, σ.kheap t = some (.tcb x) → BlockedTsA x.tcbState = true) : BAs q σ := + BAs.ofBlocked (FIA.ofValid h) hb + +/-- **`cancel_all_signals` keeps `SchedValidA`**. -/ +theorem cancelAllSignals_schedValid (n : ObjRef) : + HT (fun σ => SchedValidA σ ∧ ∀ x q, σ.kheap n = some (.notification x) → x.ntfnObj = .waitingNtfn q → BAs q σ) + (cancelAllSignals (Z := Unit) n) (fun _ σ => SchedValidA σ) := + HT.mono (cancelAllSignals_fia n) (fun _ h => ⟨FIA.ofValid h.1, h.2⟩) (fun _ _ h => h.toValid) + +/-- **`cancel_all_ipc` keeps `SchedValidA`**. -/ +theorem cancelAllIpc_schedValid (ep : ObjRef) : + HT (fun σ => SchedValidA σ ∧ ∀ x, σ.kheap ep = some (.endpoint x) → BAs x.epQueue σ) + (cancelAllIpc (Z := Unit) ep) (fun _ σ => SchedValidA σ) := + HT.mono (cancelAllIpc_fia ep) (fun _ h => ⟨FIA.ofValid h.1, h.2⟩) (fun _ _ h => h.toValid) + +/-- **`cancel_badged_sends` keeps `SchedValidA`**. -/ +theorem cancelBadgedSends_schedValid (ep : ObjRef) (badge : Badge) : + HT (fun σ => SchedValidA σ ∧ ∀ x, σ.kheap ep = some (.endpoint x) → BAs x.epQueue σ) + (cancelBadgedSends (Z := Unit) ep badge) (fun _ σ => SchedValidA σ) := + HT.mono (cancelBadgedSends_fia ep badge) (fun _ h => ⟨FIA.ofValid h.1, h.2⟩) (fun _ _ h => h.toValid) + +/-- **`reply_remove` keeps `SchedValidA`**, given `RqDomPrio` (every ready-queue member sits in its own (domain, priority) +queue; record source `SchedCoreA.inQ` through `SchedRelA`'s tcb prio/domain relation). -/ +theorem replyRemove_schedValid (caller r : ObjRef) : + HT (fun σ => SchedValidA σ ∧ RqDomPrio σ) (replyRemove (Z := Unit) caller r) (fun _ σ => SchedValidA σ) := + HT.mono (replyRemove_fia caller r) (fun _ h => ⟨FIA.ofValid h.1, h.2⟩) (fun _ _ h => h.toValid) + +end Sched + +section +variable {m : Fl4SpecA64.McState} +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {AR : A → HwRootA → Prop} + {Z : XSetsA} {H : Heap} {OS U Pp Tb : Set U64} {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} + {κ : U64 → U64} {n : Nat} {J5 J7 : JSlotA S} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {G : MdbGhostA} + +/-- **The loop invariant plus `SchedValidA`** (`DelXA6` with F8 carried; the delete-hooks record does not state +`SchedValidA`: `JStage3TA` has no `schedValid` field, `JStage3TA.toJA` takes it as the separate input `schedValid`, so the +loop carries it alongside; contract §85, Architect ruling 03:53Z). -/ +def DelXSVG {S : Type} (st : machine.Store S) [LawfulStore st] {A : Type} (Ag : Set U64) (AR : A → HwRootA → Prop) + (Z : XSetsA) (H : Heap) (OS U Pp Tb : Set U64) + (Lv : U64 → Nat) (ia ie : Nat) (N E Rp : Set U64) (κ : U64 → U64) (n : Nat) (m : Fl4SpecA64.McState) (J5 J7 : JSlotA S) : + DelWA S A := fun s a σ G => DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m J5 J7 s a σ G ∧ SchedValidA σ + +/-- `DelXSVG` at `Ag = ∅`. -/ +abbrev DelXSV {S : Type} (st : machine.Store S) [LawfulStore st] {A : Type} (AR : A → HwRootA → Prop) (Z : XSetsA) (H : Heap) (OS U Pp Tb : Set U64) + (Lv : U64 → Nat) (ia ie : Nat) (N E Rp : Set U64) (κ : U64 → U64) (n : Nat) (m : Fl4SpecA64.McState) (J5 J7 : JSlotA S) : + DelWA S A := DelXSVG st ∅ AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m J5 J7 + +variable {Ag : Set U64} + +theorem DelXSVG.schedValid (h : DelXSVG st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m J5 J7 s a σ G) : SchedValidA σ := h.2 + +theorem DelXSVG.delXA6 (h : DelXSVG st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m J5 J7 s a σ G) : + DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m J5 J7 s a σ G := h.1 + +theorem DelXSVG.delXA (h : DelXSVG st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m J5 J7 s a σ G) : + DelXA st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m J5 J7 s a σ G := DelXA6G.toDelXA h.1 + +/-- The record is rebuilt from `DelXA6G` and a `SchedValidA` post-state fact (what the hook lemmas above supply). -/ +theorem DelXSVG.mk' (h : DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m J5 J7 s a σ G) (v : SchedValidA σ) : + DelXSVG st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m J5 J7 s a σ G := ⟨h, v⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3DispBaseA.lean b/proofs/Fl4ProofsA64/P3/Stage3DispBaseA.lean new file mode 100644 index 000000000..c6b132f27 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3DispBaseA.lean @@ -0,0 +1,305 @@ +/- +P3 (aarch64, A64-P3 Stage 3, T3 dispatch): **the shape of the dispatch top** (after x86 `VEAnyX.outerS3`). + +* `A64RelS3`: **the record at a body's end** (the `KRec` of `P3/Stage3KEntryTopA`): the Stage-3 record at some ghost + sets whose cores are `Cs`, with the body-end facts `BodyOKA` (`P3/Stage3BodyOKA`). `AR` is arbitrary: a line that + also tracks a machine relation instantiates it (`withMRA AR0 MR ms`). +* `DispPostA`: what one system-call invocation gives. FL4's `decode_invocation`, **at the cap the invoked slot + holds** (`capA st s slot`), returns; the spec outcome `Out` holds of some spec state; the current thread word is + kept; `A64RelS3` holds there. + +A group of lifted arms is an inductive scope predicate (`VsScopeA`, `CnScopeA`) with a `sound` theorem into +`DispPostA`; the top (`Stage3DispTopA`) is their disjunction. A new group adds a module and a disjunct. + +* `A64RelPS3` (integration): the record at a **preempted** invocation's end, with `Sched.BodyPPA` (`BodyOKA` less + `OffOKA`, head readiness under `BodyExitA.budget`'s guard) for `BodyOKA`; `A64RelS3.toPS`. `A64RelRS3 r` / + `A64RelES3 e` pick it by FL4's result / the spec's outcome; `DispPostRA` is `DispPostA` with `A64RelRS3` + (`DispPostA.toR`), what the dispatch top gives. + +The post does not re-establish `JSyscallA`: `syscall.finish` does, from `A64RelS3`. + +`InvSpecA` is the spec side of one invocation, the shape each group discharges from its arms (`.invSpecA`, +e.g. `Stage3InvSpecIpcA`). + +Named premises: none here. +-/ +import Fl4ProofsA64.P3.Stage3FinA +import Fl4ProofsA64.P3.Stage3BodyOKA +import Fl4ProofsA64.P3.Stage3ChKeepA +import Fl4SpecA64.Ext.Invocation +import Fl4ProofsA64.P3.FrontEndExtraA +import Fl4ProofsA64.P3.EmptyA +import Fl4ProofsA64.P3.CnodeCopyA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST + +noncomputable section + +/-- The success epilogue of `fl4HandleInvocationR` (spec side, after the perform). -/ +def epiSpecA (calling : Bool) (t : Fl4SpecA64.ObjRef) (reply : List Fl4SpecA64.Data) : + Fl4SpecA64.SMonad Unit Unit := do + let state ← Fl4SpecA64.getThreadState t + match state with + | .restart => do + Fl4Spec.NonDet.when calling <| Fl4SpecA64.replyFromKernel t (0, reply) + Fl4SpecA64.setThreadState t .running + | _ => return () + +open Fl4Spec Fl4SpecA64 Classical in +/-- **The spec side of the perform of a done / preempted invocation.** A done one returns `reply`, either at the +state `σ'` the record holds at (the caller no longer `Restart` unless the reply is empty: the perform replies itself), +or at an earlier state `σ2` from which the spec's own epilogue (`epiSpecA`: reply and resume) reaches `σ'` (arms FL4 +replies for inside its decode: `ARMPageGetAddress`, `ARMPageTestAndClearAccessed`). `done` is `false` for a preempted +perform. -/ +def PerfSpecA (blocking calling donate done : Bool) (oper : Fl4Invocation) (t : ObjRef) (σ1 σ' : Fl4SpecA64.State Unit) : + Prop := + if done then + ∃ reply, ((.ok reply, σ') ∈ ((fl4PerformInvocation (Z := Unit) blocking calling donate oper).run σ1).1 ∧ + ∀ x, σ'.kheap t = some (.tcb x) → x.tcbState = .restart → reply = []) ∨ + ∃ σ2, (.ok reply, σ2) ∈ ((fl4PerformInvocation (Z := Unit) blocking calling donate oper).run σ1).1 ∧ + ((), σ') ∈ (epiSpecA calling t reply σ2).1 + else (.error (), σ') ∈ ((fl4PerformInvocation (Z := Unit) blocking calling donate oper).run σ1).1 + +/-- **What a preempted invocation's exit state keeps for the preemption path's charge** (G3 input, integration): no +switch is pending, the current thread is in no ready queue and not in the release queue, its TCB has no scheduling +context or the current one, it is runnable while the action is `resume_cur_thread`, and the thread bound to an active +non-idle current SC is outside the release queue, and the current SC is chargeable (`CurScChargeableA`, carried by +the deletion loop). Only +a `Preempted` invocation owes it (CNode Delete / Revoke today, from G2's `DelPreSchedA`; the other groups' `done = false` +case is closed by their decode result). -/ +def PreExitA (σ : Fl4SpecA64.State Unit) : Prop := + (∀ t, σ.schedulerAction ≠ .switchThread t) ∧ (∀ d p, σ.curThread ∉ σ.readyQueues d p) ∧ + (∀ x, σ.kheap σ.curThread = some (.tcb x) → + x.tcbSchedContext = none ∨ x.tcbSchedContext = some σ.curSc) ∧ + σ.curThread ∉ σ.releaseQueue ∧ + (σ.schedulerAction = .resumeCurThread → ∀ x, σ.kheap σ.curThread = some (.tcb x) → + Fl4SpecA64.runnable x.tcbState = true) ∧ + (∀ y n, σ.kheap σ.curSc = some (.schedContext y n) → Fl4SpecA64.scActive y = true → + σ.curSc ≠ Fl4SpecA64.idleScPtr → ∀ o, y.scTcb = some o → o ∉ σ.releaseQueue) ∧ + CurScChargeableA σ + +open Fl4Spec Fl4SpecA64 Classical in +/-- [J] **The spec side of one invocation** (module docstring). `done` is `false` for a preempted perform, which then +also leaves `PreExitA` at the final state. -/ +def InvSpecA (calling blocking donate done : Bool) (label : Fl4SpecA64.Data) (args : List Fl4SpecA64.Data) + (cptr : Fl4SpecA64.CapRef) (slot : Fl4SpecA64.CslotPtr) (cap : Fl4SpecA64.Cap) + (excaps : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) (buffer : Option Fl4SpecA64.ObjRef) + (σ σ' : Fl4SpecA64.State Unit) : Prop := + ∃ oper σ1, (.ok oper, σ) ∈ + ((fl4DecodeInvocation (Z := Unit) calling false label args cptr slot cap excaps buffer).run σ).1 ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + PerfSpecA blocking calling donate done oper σ.curThread σ1 σ' ∧ (done = false → PreExitA σ') + +/-! ## Pinning the scopes' spec data -/ + +section pin +open Fl4Spec Fl4SpecA64 Classical + +/-- **The front end's outcomes** of the current thread's invocation at the FL4 slot `slot`: message info, the spec +slot and its cap, the extra caps, the IPC buffer and the message words. Each is determined (`σ`, `slot`). -/ +structure FrontPinA (th : TypedHeapA) (σ : State Unit) (slot : U64) (mi : MessageInfo) (sl : CslotPtr) (c : Cap) + (excaps : List (Cap × CslotPtr)) (buffer : Option ObjRef) (args : List Data) : Prop where + hmi : getMessageInfo (Z := Unit) σ.curThread σ = ({(mi, σ)}, False) + hslot : slot = cslotAddrA sl + habs : AbsSlotA th sl + hcap : getCap (Z := Unit) sl σ = ({(c, σ)}, False) + hbuf : lookupIpcBuffer (Z := Unit) false σ.curThread σ = ({(buffer, σ)}, False) + hex : (lookupExtraCaps (Z := Unit) σ.curThread buffer mi).run σ = ({(.ok excaps, σ)}, False) + hargs : getMrs (Z := Unit) σ.curThread buffer mi σ = ({(args, σ)}, False) + +/-- **The pin of an arm's spec operation**: the spec decode of the actual cap and message has the outcome `oper` +(membership: l4v's ASID-pool assign picks any free ASID). Discharged by `P3/Stage3Pin{Vs,Cn}A`. -/ +def DecPinA (calling : Bool) (th : TypedHeapA) (σ : State Unit) (slot : U64) (oper : Fl4Invocation) : Prop := + ∃ mi sl c excaps buffer args, FrontPinA th σ slot mi sl c excaps buffer args ∧ + ∀ cptr : CapRef, (.ok oper, σ) ∈ + ((fl4DecodeInvocation (Z := Unit) calling false mi.miLabel args cptr sl c excaps buffer).run σ).1 + +/-- **`InvSpecA` from a pinned operation** and its perform from the restarted state. -/ +theorem InvSpecA.ofPin {th : TypedHeapA} {kb : U64} (hP : PlaceA th kb) {σ σ1 σ' : State Unit} {slot : U64} + {call blk don : Bool} {oper : Fl4Invocation} (pin : DecPinA call th σ slot oper) + {mi : MessageInfo} {sl : CslotPtr} {c : Cap} {excaps : List (Cap × CslotPtr)} {buffer : Option ObjRef} + {args : List Data} (F : FrontPinA th σ slot mi sl c excaps buffer args) (cptr : CapRef) + (hset : setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False)) + (hperf : (.ok [], σ') ∈ ((fl4PerformInvocation (Z := Unit) blk call don oper).run σ1).1) : + InvSpecA call blk don true mi.miLabel args cptr sl c excaps buffer σ σ' := by + obtain ⟨mi0, sl0, c0, ex0, b0, a0, F0, hdec⟩ := pin + have e1 := single_fstA (F0.hmi.symm.trans F.hmi) + subst e1 + have e2 := cslotAddrA_inj hP F0.habs F.habs (F0.hslot.symm.trans F.hslot) + subst e2 + have e3 := single_fstA (F0.hcap.symm.trans F.hcap) + have e4 := single_fstA (F0.hbuf.symm.trans F.hbuf) + subst e3 e4 + have e5 := Except.ok.inj (single_fstA (F0.hex.symm.trans F.hex)) + have e6 := single_fstA (F0.hargs.symm.trans F.hargs) + subst e5 e6 + refine ⟨oper, σ1, hdec cptr, hset, ?_, fun h => by cases h⟩ + unfold PerfSpecA + rw [if_pos rfl] + exact ⟨[], Or.inl ⟨hperf, fun _ _ _ => rfl⟩⟩ + +/-- `InvSpecA.ofPin` for a preempted perform (`done = false`). -/ +theorem InvSpecA.ofPinPre {th : TypedHeapA} {kb : U64} (hP : PlaceA th kb) {σ σ1 σ' : State Unit} {slot : U64} + {call blk don : Bool} {oper : Fl4Invocation} (pin : DecPinA call th σ slot oper) + {mi : MessageInfo} {sl : CslotPtr} {c : Cap} {excaps : List (Cap × CslotPtr)} {buffer : Option ObjRef} + {args : List Data} (F : FrontPinA th σ slot mi sl c excaps buffer args) (cptr : CapRef) + (hset : setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False)) + (hperf : (.error (), σ') ∈ ((fl4PerformInvocation (Z := Unit) blk call don oper).run σ1).1) + (pre : PreExitA σ') : + InvSpecA call blk don false mi.miLabel args cptr sl c excaps buffer σ σ' := by + obtain ⟨mi0, sl0, c0, ex0, b0, a0, F0, hdec⟩ := pin + have e1 := single_fstA (F0.hmi.symm.trans F.hmi) + subst e1 + have e2 := cslotAddrA_inj hP F0.habs F.habs (F0.hslot.symm.trans F.hslot) + subst e2 + have e3 := single_fstA (F0.hcap.symm.trans F.hcap) + have e4 := single_fstA (F0.hbuf.symm.trans F.hbuf) + subst e3 e4 + have e5 := Except.ok.inj (single_fstA (F0.hex.symm.trans F.hex)) + have e6 := single_fstA (F0.hargs.symm.trans F.hargs) + subst e5 e6 + refine ⟨oper, σ1, hdec cptr, hset, ?_, fun _ => pre⟩ + unfold PerfSpecA + rw [if_neg Bool.false_ne_true] + exact hperf + +/-- An invocation whose perform is a non-preemptible step and the empty reply. -/ +theorem perform_thenNilA {m : PMonad Unit Unit} {σ σ' : State Unit} (h : (.ok (), σ') ∈ (m.run σ).1) : + (.ok [], σ') ∈ ((do m; Fl4Spec.NonDetE.returnOk ([] : List Data) : PMonad Unit (List Data)).run σ).1 := by + rw [ExceptT.run_bind] + exact mem_bindA.2 ⟨(.ok (), σ'), h, Set.mem_singleton _⟩ + +end pin + +open Fl4SpecA64 in +/-- [J] **What a preempted invocation leaves for `after_invocation`'s preemption path** (integration): `BodyOKA` with +the head facts weakened to `OffReadyA` under `BodyExitA.budget`'s guard. Both a body end (`BodyOKA.toPP`) and the +deletion loop's `Preempted` exit (`BodyPreA.toPP`) give it. -/ +structure Sched.BodyPPA (σ : State Unit) (m : McState) : Prop where + validTime : ValidMachineTimeA σ + ready : ∀ y n, σ.kheap σ.curSc = some (.schedContext y n) → (σ.curSc = idleScPtr ∨ scActive y = true) → + σ.consumedTime ≠ 0 ∨ σ.schedulerAction = .resumeCurThread → OffReadyA σ + ctAct : σ.schedulerAction = .resumeCurThread → ∀ x, σ.kheap σ.curThread = some (.tcb x) → + ActivateStateOKA x.tcbState + sw : SwA σ + scNotIdle : σ.curSc ≠ idleScPtr → NotIdleScA m σ.curSc + owner : ∀ y n, σ.kheap σ.curSc = some (.schedContext y n) → scActive y = true → σ.curSc ≠ idleScPtr → + σ.consumedTime ≠ 0 → ∀ o, y.scTcb = some o → o ∉ σ.releaseQueue + +open Fl4SpecA64 in +theorem Sched.BodyOKA.toPP {σ : State Unit} {m : McState} (B : Sched.BodyOKA σ m) : Sched.BodyPPA σ m := + ⟨B.validTime, fun y n hk hg hc => (B.budget y n hk hg hc).1, B.ctAct, B.sw, B.scNotIdle, B.owner⟩ + +variable {S : Type} (st : machine.Store S) [L : LawfulStore st] {A : Type} + +/-- **The record at a body's end** (module docstring). `n` is the core bound (`finish` uses +`fl4A64Platform.numNodes`); `Cs` the entry's kernel cores (`XSetsA.Cs`, which no body changes). -/ +def A64RelS3 (AR : A → HwRootA → Prop) (ar : machine.Arch A) (nA : U64) (n : Nat) (Cs : Set U64) (s : S) (a : A) + (σ : Fl4SpecA64.State Unit) (m : Fl4SpecA64.McState) : Prop := + ∃ (Z : XSetsA) (H : Heap) (OS U Ac Pp Tb : Set U64) (Lv : U64 → Nat) (ia ie : Nat) (N E Rp : Set U64) + (Bf : Set (U64 × Fl4SpecA64.Word)) (stack : U64 → List U64) (κ : U64 → U64), + Z.Cs = Cs ∧ + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m ∧ + Sched.BodyOKA σ m + +/-- **The record at a preempted invocation's end** (integration): `A64RelS3` with `BodyPPA` for `BodyOKA`. -/ +def A64RelPS3 (AR : A → HwRootA → Prop) (ar : machine.Arch A) (nA : U64) (n : Nat) (Cs : Set U64) (s : S) (a : A) + (σ : Fl4SpecA64.State Unit) (m : Fl4SpecA64.McState) : Prop := + ∃ (Z : XSetsA) (H : Heap) (OS U Ac Pp Tb : Set U64) (Lv : U64 → Nat) (ia ie : Nat) (N E Rp : Set U64) + (Bf : Set (U64 × Fl4SpecA64.Word)) (stack : U64 → List U64) (κ : U64 → U64), + Z.Cs = Cs ∧ + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m ∧ + Sched.BodyPPA σ m + +/-- The record by FL4's invocation result: `A64RelPS3` after `Preempted`, `A64RelS3` otherwise. -/ +def A64RelRS3 (AR : A → HwRootA → Prop) (ar : machine.Arch A) (nA : U64) (n : Nat) (Cs : Set U64) + (r : core.result.Result Unit error.Exc) (s : S) (a : A) (σ : Fl4SpecA64.State Unit) (m : Fl4SpecA64.McState) : + Prop := + match r with + | .Err .Preempted => A64RelPS3 st AR ar nA n Cs s a σ m + | _ => A64RelS3 st AR ar nA n Cs s a σ m + +/-- The record by the spec's invocation outcome: `A64RelPS3` after a preemption, `A64RelS3` otherwise. -/ +def A64RelES3 (AR : A → HwRootA → Prop) (ar : machine.Arch A) (nA : U64) (n : Nat) (Cs : Set U64) + (e : Except Unit Unit) (s : S) (a : A) (σ : Fl4SpecA64.State Unit) (m : Fl4SpecA64.McState) + (Clock : A → Fl4SpecA64.MachineState → Prop) : Prop := + match e with + | .ok _ => A64RelS3 st AR ar nA n Cs s a σ m + | .error _ => A64RelPS3 st AR ar nA n Cs s a σ m ∧ PreExitA σ ∧ Clock a σ.machineState + +/-- **`DispPostA` with the record by result** (integration: what the dispatch top gives). -/ +def DispPostRA (AR : A → HwRootA → Prop) (ar : machine.Arch A) (nA : U64) (n : Nat) (Cs : Set U64) (s : S) (a : A) + (m : Fl4SpecA64.McState) (Clock : A → Fl4SpecA64.MachineState → Prop) (lbl len slot : U64) (blk call don fp : Bool) (buf : U64) + (extra : Aeneas.Std.Array obj.Slot 3#usize) (Out : Fl4SpecA64.State Unit → Prop) : Prop := + ∃ (r : core.result.Result Unit error.Exc) (s' : S) (a' : A) (σ' : Fl4SpecA64.State Unit), + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don fp buf extra = ok (r, s', a') ∧ + Out σ' ∧ L.curW s' = L.curW s ∧ A64RelRS3 st AR ar nA n Cs r s' a' σ' m ∧ + (r = .Err error.Exc.Preempted → Clock a' σ'.machineState) + +/-- **`DispPostA` with a successful decode** (every arm of the VSpace and CNode groups). -/ +def DispPostOkA (AR : A → HwRootA → Prop) (ar : machine.Arch A) (nA : U64) (n : Nat) (Cs : Set U64) (s : S) (a : A) + (m : Fl4SpecA64.McState) (lbl len slot : U64) (blk call don fp : Bool) (buf : U64) + (extra : Aeneas.Std.Array obj.Slot 3#usize) (Out : Fl4SpecA64.State Unit → Prop) : Prop := + ∃ (s' : S) (a' : A) (σ' : Fl4SpecA64.State Unit), + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don fp buf extra = + ok (core.result.Result.Ok (), s', a') ∧ + Out σ' ∧ L.curW s' = L.curW s ∧ A64RelS3 st AR ar nA n Cs s' a' σ' m + +/-- **One invocation's result** (module docstring). -/ +def DispPostA (AR : A → HwRootA → Prop) (ar : machine.Arch A) (nA : U64) (n : Nat) (Cs : Set U64) (s : S) (a : A) + (m : Fl4SpecA64.McState) (lbl len slot : U64) (blk call don fp : Bool) (buf : U64) + (extra : Aeneas.Std.Array obj.Slot 3#usize) (Out : Fl4SpecA64.State Unit → Prop) : Prop := + ∃ (r : core.result.Result Unit error.Exc) (s' : S) (a' : A) (σ' : Fl4SpecA64.State Unit), + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don fp buf extra = ok (r, s', a') ∧ + Out σ' ∧ L.curW s' = L.curW s ∧ A64RelS3 st AR ar nA n Cs s' a' σ' m ∧ r ≠ .Err error.Exc.Preempted + +variable {st} + +theorem DispPostOkA.post {AR : A → HwRootA → Prop} {ar : machine.Arch A} {nA : U64} {n : Nat} {Cs : Set U64} {s : S} + {a : A} {m : Fl4SpecA64.McState} {lbl len slot : U64} {blk call don fp : Bool} {buf : U64} + {extra : Aeneas.Std.Array obj.Slot 3#usize} {Out : Fl4SpecA64.State Unit → Prop} + (h : DispPostOkA st AR ar nA n Cs s a m lbl len slot blk call don fp buf extra Out) : + DispPostA st AR ar nA n Cs s a m lbl len slot blk call don fp buf extra Out := + let ⟨s', a', σ', h⟩ := h + ⟨_, s', a', σ', h.1, h.2.1, h.2.2.1, h.2.2.2, fun e => by cases e⟩ + +theorem VsErrRelA.ne_preempted {e : error.Exc} {e' : Fl4SpecA64.SyscallError} (h : VsErrRelA e e') : + e ≠ error.Exc.Preempted := by + rintro rfl + rcases h with h | ⟨n, h, -⟩ + · exact h + · cases h + +theorem A64RelS3.toPS {AR : A → HwRootA → Prop} {ar : machine.Arch A} {nA : U64} {n : Nat} {Cs : Set U64} {s : S} + {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} (R : A64RelS3 st AR ar nA n Cs s a σ m) : + A64RelPS3 st AR ar nA n Cs s a σ m := + let ⟨Z, H, OS, U, Ac, Pp, Tb, Lv, ia, ie, N, E, Rp, Bf, stack, κ, hCs, J, B⟩ := R + ⟨Z, H, OS, U, Ac, Pp, Tb, Lv, ia, ie, N, E, Rp, Bf, stack, κ, hCs, J, B.toPP⟩ + +theorem A64RelS3.toR {AR : A → HwRootA → Prop} {ar : machine.Arch A} {nA : U64} {n : Nat} {Cs : Set U64} {s : S} + {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} (R : A64RelS3 st AR ar nA n Cs s a σ m) + (r : core.result.Result Unit error.Exc) : A64RelRS3 st AR ar nA n Cs r s a σ m := by + rcases r with _ | (_ | _ | _ | _) <;> first | exact R | exact R.toPS + +theorem DispPostA.toR {AR : A → HwRootA → Prop} {ar : machine.Arch A} {nA : U64} {n : Nat} {Cs : Set U64} {s : S} + {a : A} {m : Fl4SpecA64.McState} {lbl len slot : U64} {blk call don fp : Bool} {buf : U64} + {extra : Aeneas.Std.Array obj.Slot 3#usize} {Out : Fl4SpecA64.State Unit → Prop} + {Clock : A → Fl4SpecA64.MachineState → Prop} + (h : DispPostA st AR ar nA n Cs s a m lbl len slot blk call don fp buf extra Out) : + DispPostRA st AR ar nA n Cs s a m Clock lbl len slot blk call don fp buf extra Out := + let ⟨r, s', a', σ', e, o, c, R, hnp⟩ := h + ⟨r, s', a', σ', e, o, c, R.toR r, fun h => absurd h hnp⟩ + +theorem A64RelS3.ofJ {AR : A → HwRootA → Prop} {ar : machine.Arch A} {nA : U64} {s : S} {a : A} + {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {Cs : Set U64} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (B : Sched.BodyOKA σ m) (hCs : Z.Cs = Cs := by rfl) : A64RelS3 st AR ar nA n Cs s a σ m := + ⟨_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, hCs, J, B⟩ + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3DispCnA.lean b/proofs/Fl4ProofsA64/P3/Stage3DispCnA.lean new file mode 100644 index 000000000..6129756c1 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3DispCnA.lean @@ -0,0 +1,517 @@ +/- +P3 (aarch64, A64-P3 Stage 3, T3 dispatch): **the CNode dispatch group** (`Stage3DispBaseA`): Copy / Mint, Move / +Mutate and Rotate on their success paths. Generated (WS/handoff/tools/gen_t3disp.py). + +Also **CNode Revoke / Delete** on their `Ok` returns (`cnodeDelRv`, from `JSyscallA.cnodeRevokeAsmA` / +`cnodeDeleteAsmA`, ending in `BodyEndA`) and on their `Preempted` returns (`cnodeDelRvPre`: `DelBodyEndA` keeps only +`BodyPreA`, so the arm ends in `A64RelPS3` via `BodyPreA.toPP`; `CnScopeA.sound` gives the record by result, +`DispPostRA`). The arm's platform premises (`DelPlatA` [P], `hCl` [P], `hCs` [S]) and the +`CounterStepA` fact are inputs of the constructor; `soundOk` needs `PlainStore` (the Frame finaliser hook). + +Pending lift: CancelBadgedSends, the other `Err` returns. +-/ +import Fl4ProofsA64.P3.Stage3T3ArmsCnA +import Fl4ProofsA64.P3.Stage3DispBaseA +import Fl4ProofsA64.P3.Stage3T3DecEqA +import Fl4ProofsA64.P3.Stage3DelAsmTopA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +theorem BodyPreA.toPP {σ : State Unit} {m : McState} (h : BodyPreA σ m) : BodyPPA σ m := + ⟨h.validTime, fun y n hk ha _ => h.ready y n hk ha, h.ctAct, h.sw, h.scNotIdle, h.owner⟩ + +section +variable {S : Type} (st : machine.Store S) [L : LawfulStore st] {A : Type} + +set_option maxHeartbeats 4000000 in +/-- **The Cn arms in scope** (module docstring), indexed by the invocation (label, length, slot, flags, buffer, extra +slots) and the spec outcome. -/ +inductive CnScopeA (AR : A → HwRootA → Prop) (Z : XSetsA) (H : Heap) (OS U Ac Pp Tb : Set U64) + (Lv : U64 → Nat) (ia ie : Nat) (N E Rp : Set U64) (Bf : Set (U64 × Fl4SpecA64.Word)) (stack : U64 → List U64) + (κ : U64 → U64) (n : Nat) (ar : machine.Arch A) (nA : U64) (s : S) (a : A) (σ : Fl4SpecA64.State Unit) + (m : Fl4SpecA64.McState) (Clock : A → Fl4SpecA64.MachineState → Prop) : + U64 → U64 → U64 → Bool → Bool → Bool → Bool → U64 → Aeneas.Std.Array obj.Slot 3#usize → + (Fl4SpecA64.State Unit → Prop) → Prop + | cnodeCopy + {ps pd : U64} + {nc : capability.Cap} + {anc : Fl4SpecA64.Cap} + {slot : U64} + {p r gs g : U64} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + {lbl length buf index depth x0 : U64} + {si sdp : U64} + {root : capability.Cap} + {bB bC bD : Bool} + (hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM) + (ss sd : Fl4SpecA64.CslotPtr) + (hCs : ss.1 = specPtr ps ∧ Z.th ps = some (.cnode ss.2.length)) + (hCd : sd.1 = specPtr pd ∧ Z.th pd = some (.cnode sd.2.length)) + (hsn : capA st s (cslotAddrA ss) ≠ .Null) + (hdn : capA st s (cslotAddrA sd) = .Null) + (hdc : obj.sext48 (cslotAddrA sd) = ok (cslotAddrA sd)) + (hwf : CapWfA nc) + (hanc : CapEqA nc anc) + (hw : CapRt.EncWf nc) + (hn : nc ≠ .Null) + (hD : Fl4ProofsA64.P3.DerivA nc (capA st s (cslotAddrA ss))) + (hal : UA nc (capA st s (cslotAddrA ss))) + (hk : SameBadgeKindA nc (capA st s (cslotAddrA ss))) + (hsh : Fl4ProofsA64.P3.shapeA nc = Fl4ProofsA64.P3.shapeA (capA st s (cslotAddrA ss))) + (hsh1 : Fl4ProofsA64.P1.W7.R.ST.shapeA nc = Fl4ProofsA64.P1.W7.R.ST.shapeA (capA st s (cslotAddrA ss))) + (hbd1 : badgeA nc = badgeA (capA st s (cslotAddrA ss)) ∨ badgeA (capA st s (cslotAddrA ss)) = 0#u64) + (hbd : ∀ r b rs, (absCapA (capA st s (cslotAddrA ss)) = .endpointCap r b rs ∨ + absCapA (capA st s (cslotAddrA ss)) = .notificationCap r b rs) → b ≠ 0 → + Fl4SpecA64.capEpBadge (absCapA nc) = b) + (hirq : ∀ i, nc = .IrqHandler i ↔ capA st s (cslotAddrA ss) = .IrqHandler i) + (hz : ∀ p t k, capA st s (cslotAddrA ss) ≠ .Zombie p t k) + (hut : isUtA (capA st s (cslotAddrA ss)) = true → ∀ y, ¬ Fl4Proofs.P3.Mdb.Desc Z.G.par y (cslotAddrA ss)) + (hdo : ¬ IrqNodeA (L.ksb s) (fl4Ptr sd.1) ∨ IrqCapA (absCapA nc)) + (hcv : CapValidA Z.th (capA st s (cslotAddrA ss)) → CapValidA Z.th nc) + (hsci : NoIdleCapV m n anc) + (hslot : slot ∈ OS) + (hslc : capA st s slot = .CNode p r gs g) + (hlb : lbl = fl4_abi.label.CNodeCopy ∨ lbl = fl4_abi.label.CNodeMint) + (hl4 : ¬ length < 4#u64) + (hl2 : ¬ length < 2#u64) + (hi : msg.syscall_arg st s (L.curW s) buf 0#u64 = ok index) + (hd0 : msg.syscall_arg st s (L.curW s) buf 1#u64 = ok depth) + (hres : cspace.lookup_target_slot st s (.CNode p r gs g) index depth = ok (.Ok (cslotAddrA sd))) + (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) + (h0 : x0 ≠ 0#u64) + (h2 : msg.syscall_arg st s (L.curW s) buf 2#u64 = ok si) + (h3 : msg.syscall_arg st s (L.curW s) buf 3#u64 = ok sdp) + (hroot : obj.Slot.cap st x0 s = ok root) + (hemp : cdt.ensure_empty_slot st s (cslotAddrA sd) = ok (.Ok ())) + (hsrc : cspace.lookup_source_slot st s root si sdp = ok (.Ok (cslotAddrA ss))) + (hcop : cnode.copied_cap st s lbl length buf (cslotAddrA ss) (capA st s (cslotAddrA ss)) = ok (.Ok (nc, false))) + (hcov : SlotCov st OS s slot) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) + (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hpin : DecPinA bC Z.th σ slot (.invokeCNode (.insertCall anc ss sd))) : + CnScopeA AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar nA s a σ m Clock lbl length slot bB bC bD false buf extra + (fun σ' => ∃ (x : Fl4SpecA64.Tcb), + σ.kheap σ.curThread = some (.tcb x) ∧ + ((), σ') ∈ (Fl4SpecA64.capInsert (Z := Unit) anc ss sd (stUpdA σ σ.curThread x .restart)).1) + | cnodeMove + {ps pd : U64} + {nc : capability.Cap} + {ac : Fl4SpecA64.Cap} + {slot : U64} + {p r gs g : U64} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + {lbl length buf index depth x0 : U64} + {si sdp : U64} + {root : capability.Cap} + {bB bC bD : Bool} + (hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM) + (ss sd : Fl4SpecA64.CslotPtr) + (hCs : ss.1 = specPtr ps ∧ Z.th ps = some (.cnode ss.2.length)) + (hCd : sd.1 = specPtr pd ∧ Z.th pd = some (.cnode sd.2.length)) + (hsn : capA st s (cslotAddrA ss) ≠ .Null) + (hdn : capA st s (cslotAddrA sd) = .Null) + (hdc : obj.sext48 (cslotAddrA sd) = ok (cslotAddrA sd)) + (hca : CapRelA nc ac) + (hw : CapRt.EncWf nc) + (hn : nc ≠ .Null) + (hD : DerivA nc (capA st s (cslotAddrA ss))) + (hal : UA nc (capA st s (cslotAddrA ss))) + (hsh : Fl4ProofsA64.P1.W7.R.ST.shapeA nc = Fl4ProofsA64.P1.W7.R.ST.shapeA (capA st s (cslotAddrA ss))) + (hbd : badgeA nc = badgeA (capA st s (cslotAddrA ss))) + (hirq : ∀ i, nc = .IrqHandler i ↔ capA st s (cslotAddrA ss) = .IrqHandler i) + (hno : CapObjOkA Z nc) + (hok : CapOkA σ.interruptIrqNode sd ac) + (hsci : NoIdleCapV m n ac) + (hslot : slot ∈ OS) + (hslc : capA st s slot = .CNode p r gs g) + (hlb : lbl = fl4_abi.label.CNodeMove ∨ lbl = fl4_abi.label.CNodeMutate) + (hl4 : ¬ length < 4#u64) + (hl2 : ¬ length < 2#u64) + (hi : msg.syscall_arg st s (L.curW s) buf 0#u64 = ok index) + (hd0 : msg.syscall_arg st s (L.curW s) buf 1#u64 = ok depth) + (hres : cspace.lookup_target_slot st s (.CNode p r gs g) index depth = ok (.Ok (cslotAddrA sd))) + (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) + (h0 : x0 ≠ 0#u64) + (h2 : msg.syscall_arg st s (L.curW s) buf 2#u64 = ok si) + (h3 : msg.syscall_arg st s (L.curW s) buf 3#u64 = ok sdp) + (hroot : obj.Slot.cap st x0 s = ok root) + (hemp : cdt.ensure_empty_slot st s (cslotAddrA sd) = ok (.Ok ())) + (hsrc : cspace.lookup_source_slot st s root si sdp = ok (.Ok (cslotAddrA ss))) + (hcop : cnode.copied_cap st s lbl length buf (cslotAddrA ss) (capA st s (cslotAddrA ss)) = ok (.Ok (nc, true))) + (hcov : SlotCov st OS s slot) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) + (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hpin : DecPinA bC Z.th σ slot (.invokeCNode (.moveCall ac ss sd))) : + CnScopeA AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar nA s a σ m Clock lbl length slot bB bC bD false buf extra + (fun σ' => ∃ (x : Fl4SpecA64.Tcb), + σ.kheap σ.curThread = some (.tcb x) ∧ + ((), σ') ∈ (capMove (Z := Unit) ac ss sd (stUpdA σ σ.curThread x .restart)).1) + | cnodeRotate + {ps pp pd : U64} + {np : capability.Cap} + {ap : Fl4SpecA64.Cap} + {ns : capability.Cap} + {an : Fl4SpecA64.Cap} + {slot : U64} + {p r gs g : U64} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + {lbl length buf index depth x0 : U64} + {x1' : U64} + {pdata sdata : U64} + {bB bC bD : Bool} + (hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM) + (ss sp sd : Fl4SpecA64.CslotPtr) + (hCs : ss.1 = specPtr ps ∧ Z.th ps = some (.cnode ss.2.length)) + (hCp : sp.1 = specPtr pp ∧ Z.th pp = some (.cnode sp.2.length)) + (hCd : sd.1 = specPtr pd ∧ Z.th pd = some (.cnode sd.2.length)) + (hsp : cslotAddrA ss ≠ cslotAddrA sp) + (hsd : cslotAddrA ss ≠ cslotAddrA sd) + (hsn : capA st s (cslotAddrA ss) ≠ .Null) + (hpn : capA st s (cslotAddrA sp) ≠ .Null) + (hdn : capA st s (cslotAddrA sd) = .Null) + (hpc : obj.sext48 (cslotAddrA sp) = ok (cslotAddrA sp)) + (hdc : obj.sext48 (cslotAddrA sd) = ok (cslotAddrA sd)) + (hcp : CapRelA np ap) + (hwp : CapRt.EncWf np) + (hnp : np ≠ .Null) + (hDp : Fl4ProofsA64.P3.DerivA np (capA st s (cslotAddrA sp))) + (halp : UA np (capA st s (cslotAddrA sp))) + (hshp : Fl4ProofsA64.P1.W7.R.ST.shapeA np = Fl4ProofsA64.P1.W7.R.ST.shapeA (capA st s (cslotAddrA sp))) + (hbdp : badgeA np = badgeA (capA st s (cslotAddrA sp))) + (hirqp : ∀ i, np = .IrqHandler i ↔ capA st s (cslotAddrA sp) = .IrqHandler i) + (hop : CapObjOkA Z np) + (hokp : CapOkA σ.interruptIrqNode sd ap) + (hscp : NoIdleCapV m n ap) + (hcs : CapRelA ns an) + (hws : CapRt.EncWf ns) + (hns : ns ≠ .Null) + (hDs : Fl4ProofsA64.P3.DerivA ns (capA st s (cslotAddrA ss))) + (hals : UA ns (capA st s (cslotAddrA ss))) + (hshs : Fl4ProofsA64.P1.W7.R.ST.shapeA ns = Fl4ProofsA64.P1.W7.R.ST.shapeA (capA st s (cslotAddrA ss))) + (hbds : badgeA ns = badgeA (capA st s (cslotAddrA ss))) + (hirqs : ∀ i, ns = .IrqHandler i ↔ capA st s (cslotAddrA ss) = .IrqHandler i) + (hos : CapObjOkA Z ns) + (hoks : CapOkA σ.interruptIrqNode sp an) + (hscs : NoIdleCapV m n an) + (hslot : slot ∈ OS) + (hslc : capA st s slot = .CNode p r gs g) + (hlb : lbl = fl4_abi.label.CNodeRotate) + (hl8 : ¬ length < 8#u64) + (hl2 : ¬ length < 2#u64) + (hi : msg.syscall_arg st s (L.curW s) buf 0#u64 = ok index) + (hd0 : msg.syscall_arg st s (L.curW s) buf 1#u64 = ok depth) + (hres : cspace.lookup_target_slot st s (.CNode p r gs g) index depth = ok (.Ok (cslotAddrA sd))) + (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) + (h0 : x0 ≠ 0#u64) + (hx1 : Aeneas.Std.Array.index_usize extra 1#usize = ok x1') + (h1 : x1' ≠ 0#u64) + (h2 : msg.syscall_arg st s (L.curW s) buf 2#u64 = ok pdata) + (h5 : msg.syscall_arg st s (L.curW s) buf 5#u64 = ok sdata) + (hrot : cnode.rotate_slots st s buf extra (cslotAddrA sd) = ok (.Ok (cslotAddrA ss, cslotAddrA sp))) + (hus : objects.update_cap_data true sdata (capA st s (cslotAddrA ss)) = ok ns) + (hup : objects.update_cap_data true pdata (capA st s (cslotAddrA sp)) = ok np) + (hcov : SlotCov st OS s slot) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) + (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hpin : DecPinA bC Z.th σ slot (.invokeCNode (.rotateCall an ap ss sp sd))) : + CnScopeA AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar nA s a σ m Clock lbl length slot bB bC bD false buf extra + (fun σ' => ∃ (x : Fl4SpecA64.Tcb) (σ1 : Fl4SpecA64.State Unit), + σ.kheap σ.curThread = some (.tcb x) ∧ + ((), σ1) ∈ (capMove (Z := Unit) ap sp sd (stUpdA σ σ.curThread x .restart)).1 ∧ + ((), σ') ∈ (capMove (Z := Unit) an ss sp σ1).1) + | cnodeRotateSw + {ps pp : U64} + {np : capability.Cap} + {ap : Fl4SpecA64.Cap} + {ns : capability.Cap} + {an : Fl4SpecA64.Cap} + {slot : U64} + {p r gs g : U64} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + {lbl length buf index depth x0 : U64} + {x1' : U64} + {pdata sdata : U64} + {bB bC bD : Bool} + (hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM) + (ss sp : Fl4SpecA64.CslotPtr) + (hCs : ss.1 = specPtr ps ∧ Z.th ps = some (.cnode ss.2.length)) + (hCp : sp.1 = specPtr pp ∧ Z.th pp = some (.cnode sp.2.length)) + (hsp : cslotAddrA ss ≠ cslotAddrA sp) + (hsn : capA st s (cslotAddrA ss) ≠ .Null) + (hpn : capA st s (cslotAddrA sp) ≠ .Null) + (hsx : obj.sext48 (cslotAddrA ss) = ok (cslotAddrA ss)) + (hpx : obj.sext48 (cslotAddrA sp) = ok (cslotAddrA sp)) + (hcp : CapRelA np ap) + (hwp : CapRt.EncWf np) + (hnp : np ≠ .Null) + (hDp : Fl4ProofsA64.P3.DerivA np (capA st s (cslotAddrA sp))) + (halp : UA np (capA st s (cslotAddrA sp))) + (hshp : Fl4ProofsA64.P1.W7.R.ST.shapeA np = Fl4ProofsA64.P1.W7.R.ST.shapeA (capA st s (cslotAddrA sp))) + (hbdp : badgeA np = badgeA (capA st s (cslotAddrA sp))) + (hirqp : ∀ i, np = .IrqHandler i ↔ capA st s (cslotAddrA sp) = .IrqHandler i) + (hop : CapObjOkA Z np) + (hokp : CapOkA σ.interruptIrqNode ss ap) + (hscp : NoIdleCapV m n ap) + (hcs : CapRelA ns an) + (hws : CapRt.EncWf ns) + (hns : ns ≠ .Null) + (hDs : Fl4ProofsA64.P3.DerivA ns (capA st s (cslotAddrA ss))) + (hals : UA ns (capA st s (cslotAddrA ss))) + (hshs : Fl4ProofsA64.P1.W7.R.ST.shapeA ns = Fl4ProofsA64.P1.W7.R.ST.shapeA (capA st s (cslotAddrA ss))) + (hbds : badgeA ns = badgeA (capA st s (cslotAddrA ss))) + (hirqs : ∀ i, ns = .IrqHandler i ↔ capA st s (cslotAddrA ss) = .IrqHandler i) + (hos : CapObjOkA Z ns) + (hoks : CapOkA σ.interruptIrqNode sp an) + (hscs : NoIdleCapV m n an) + (hslot : slot ∈ OS) + (hslc : capA st s slot = .CNode p r gs g) + (hlb : lbl = fl4_abi.label.CNodeRotate) + (hl8 : ¬ length < 8#u64) + (hl2 : ¬ length < 2#u64) + (hi : msg.syscall_arg st s (L.curW s) buf 0#u64 = ok index) + (hd0 : msg.syscall_arg st s (L.curW s) buf 1#u64 = ok depth) + (hres : cspace.lookup_target_slot st s (.CNode p r gs g) index depth = ok (.Ok (cslotAddrA ss))) + (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) + (h0 : x0 ≠ 0#u64) + (hx1 : Aeneas.Std.Array.index_usize extra 1#usize = ok x1') + (h1 : x1' ≠ 0#u64) + (h2 : msg.syscall_arg st s (L.curW s) buf 2#u64 = ok pdata) + (h5 : msg.syscall_arg st s (L.curW s) buf 5#u64 = ok sdata) + (hrot : cnode.rotate_slots st s buf extra (cslotAddrA ss) = ok (.Ok (cslotAddrA ss, cslotAddrA sp))) + (hus : objects.update_cap_data true sdata (capA st s (cslotAddrA ss)) = ok ns) + (hup : objects.update_cap_data true pdata (capA st s (cslotAddrA sp)) = ok np) + (hcov : SlotCov st OS s slot) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) + (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hpin : DecPinA bC Z.th σ slot (.invokeCNode (.rotateCall an ap ss sp ss))) : + CnScopeA AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar nA s a σ m Clock lbl length slot bB bC bD false buf extra + (fun σ' => ∃ (x : Fl4SpecA64.Tcb), + σ.kheap σ.curThread = some (.tcb x) ∧ + ((), σ') ∈ (Fl4SpecA64.capSwap (Z := Unit) an ss ap sp (stUpdA σ σ.curThread x .restart)).1) + | cnodeDelRv + {slot : U64} + {p r gs g : U64} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + {lbl length buf : U64} + {bB bC bD : Bool} + {slbl : Fl4SpecA64.Data} + {args : List U64} + {ac : Fl4SpecA64.Cap} + {excaps : List Fl4SpecA64.Cap} + {inv : Fl4SpecA64.CnodeInvocation} + {rr : core.result.Result Unit error.Exc} + {s3 : S} + {a3 : A} + (hCnt : CounterStepA σ) + (P : DelPlatA st ar AR Clock) + (hCl : Clock a σ.machineState) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (hslot : slot ∈ OS) + (hcov : SlotCov st OS s slot) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) + (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hslc : capA st s slot = .CNode p r gs g) + (hlb : lbl = fl4_abi.label.CNodeDelete ∨ lbl = fl4_abi.label.CNodeRevoke) + (hlbl : Fl4SpecA64.genInvocationType slbl = cnodeLabelOfA lbl) + (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) + (hca : CapRelA (.CNode p r gs g) ac) + (hcv : CapValidA Z.th (.CNode p r gs g)) + (hdec : cnode.decode st ar s a lbl length (.CNode p r gs g) buf extra = ok (rr, s3, a3)) + (hrr : rr = .Ok ()) + (hspec : (Fl4SpecA64.decodeCnodeInvocation slbl (args.map UScalar.bv) ac excaps).run σ = + ({(.ok inv, σ)}, False)) + (hpin : DecPinA bC Z.th σ slot (.invokeCNode inv)) : + CnScopeA AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar nA s a σ m Clock lbl length slot bB bC bD false buf extra + (fun σ' => ∃ (σ1 : Fl4SpecA64.State Unit), + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + (.ok (), σ') ∈ ((Fl4SpecA64.fl4InvokeCnode (Z := Unit) inv).run σ1).1) + + | cnodeDelRvPre + {slot : U64} + {p r gs g : U64} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + {lbl length buf : U64} + {bB bC bD : Bool} + {slbl : Fl4SpecA64.Data} + {args : List U64} + {ac : Fl4SpecA64.Cap} + {excaps : List Fl4SpecA64.Cap} + {inv : Fl4SpecA64.CnodeInvocation} + {rr : core.result.Result Unit error.Exc} + {s3 : S} + {a3 : A} + (hCnt : CounterStepA σ) + (P : DelPlatA st ar AR Clock) + (hCl : Clock a σ.machineState) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (hslot : slot ∈ OS) + (hcov : SlotCov st OS s slot) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) + (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hslc : capA st s slot = .CNode p r gs g) + (hlb : lbl = fl4_abi.label.CNodeDelete ∨ lbl = fl4_abi.label.CNodeRevoke) + (hlbl : Fl4SpecA64.genInvocationType slbl = cnodeLabelOfA lbl) + (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) + (hca : CapRelA (.CNode p r gs g) ac) + (hcv : CapValidA Z.th (.CNode p r gs g)) + (hdec : cnode.decode st ar s a lbl length (.CNode p r gs g) buf extra = ok (rr, s3, a3)) + (hrr : rr = .Err .Preempted) + (hspec : (Fl4SpecA64.decodeCnodeInvocation slbl (args.map UScalar.bv) ac excaps).run σ = + ({(.ok inv, σ)}, False)) + (hpin : DecPinA bC Z.th σ slot (.invokeCNode inv)) : + CnScopeA AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar nA s a σ m Clock lbl length slot bB bC bD false buf extra + (fun σ' => ∃ (σ1 : Fl4SpecA64.State Unit), + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + (.error (), σ') ∈ ((Fl4SpecA64.fl4InvokeCnode (Z := Unit) inv).run σ1).1 ∧ PreExitA σ') + +variable {st} + +set_option maxHeartbeats 8000000 in +/-- **Every Cn arm in scope runs and keeps the record**: its decode succeeds, or (`cnodeDelRvPre`) is preempted with +`A64RelPS3` and the spec's preempted perform of the pinned operation. -/ +theorem CnScopeA.soundOk [PlainStore st] {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} + {κ : U64 → U64} {n : Nat} {ar : machine.Arch A} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + {lbl len slot : U64} {blk call don fp : Bool} {buf : U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} + {Out : Fl4SpecA64.State Unit → Prop} + (h : CnScopeA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar hPl.ipa s a σ m Clock lbl len slot blk call don fp buf + extra Out) : + DispPostOkA st AR ar hPl.ipa n Z.Cs s a m lbl len slot blk call don fp buf extra Out ∨ + ∃ (s' : S) (a' : A) (σ' : Fl4SpecA64.State Unit), + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don fp buf extra = + ok (.Err .Preempted, s', a') ∧ Out σ' ∧ L.curW s' = L.curW s ∧ A64RelPS3 st AR ar hPl.ipa n Z.Cs s' a' σ' m ∧ + Clock a' σ'.machineState ∧ + ∃ inv, DecPinA call Z.th σ slot (.invokeCNode inv) ∧ ∀ σ'', Out σ'' → ∃ σ1, + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + (.error (), σ'') ∈ ((Fl4SpecA64.fl4InvokeCnode (Z := Unit) inv).run σ1).1 ∧ PreExitA σ'' := by + induction h with + | @cnodeCopy ps pd nc anc slot p r gs g extra lbl length buf index depth x0 si sdp root bB bC bD hnvm ss sd hCs hCd hsn hdn hdc hwf hanc hw hn hD hal hk hsh hsh1 hbd1 hbd hirq hz hut hdo hcv hsci hslot hslc hlb hl4 hl2 hi hd0 hres hx0 h0 h2 h3 hroot hemp hsrc hcop hcov hext hSC hpin => + left + obtain ⟨x, s2, σ2, hc0, hset, hcdt0, hc3, J3, K3⟩ := JSyscallA.cnodeCopyA (J := J) (hA := hA) (hM := hM) (hPl := hPl) (hfm := hfm) (hnvm := hnvm) (ss := ss) (sd := sd) (hCs := hCs) (hCd := hCd) (hsn := hsn) (hdn := hdn) (hdc := hdc) (hwf := hwf) (hanc := hanc) (hw := hw) (hn := hn) (hD := hD) (hal := hal) (hk := hk) (hsh := hsh) (hsh1 := hsh1) (hbd1 := hbd1) (hbd := hbd) (hirq := hirq) (hz := hz) (hut := hut) (hdo := hdo) (hcv := hcv) (hsci := hsci) (hslot := hslot) (hslc := hslc) (hlb := hlb) (hl4 := hl4) (hl2 := hl2) (hi := hi) (hd := hd0) (hres := hres) (hx0 := hx0) (h0 := h0) (h2 := h2) (h3 := h3) (hroot := hroot) (hemp := hemp) (hsrc := hsrc) (hcop := hcop) (hcov := hcov) (hext := hext) (hSC := hSC) + refine ⟨s2, a, σ2, ?_, ⟨x, hc0, hc3⟩, curW_keptA J.toJStage3A J3 K3.cur, A64RelS3.ofJ J3 (Sched.BodyOKA.ofKeep J hc0 K3)⟩ + have hlbl : ¬ lbl < fl4_abi.label.CNodeRevoke ∧ ¬ lbl > fl4_abi.label.CNodeRotate ∧ + lbl ≥ fl4_abi.label.CNodeCopy ∧ lbl ≤ fl4_abi.label.CNodeMutate := by + have := lblA_revoke; have := lblA_rotate; have := lblA_copy; have := lblA_mutate + have := lblA_mint; have := lblA_move + rcases hlb with h | h <;> subst h <;> refine ⟨?_, ?_, ?_, ?_⟩ <;> scalar_tac + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hslot hcov J.actW J.running J.cur + obtain ⟨x1, -, -, -, -, -, hOS1, -⟩ := J.toJStage3A.of_restartCurA RS.pl.choose_spec.choose_spec.choose_spec.choose_spec + RS.sym RS.run hnvm RS.cur RS.vs RS.vc + have xs : cslotAddrA ss ∈ OS := J.j5.2.1 _ ((show AbsSlotA Z.th ss from ⟨ps, hCs.1, Or.inl hCs.2⟩).isSlot J.j.1.place) + have e : cdt.insert st (L.wr s (stA (L.curW s)) sched.TS_RESTART) nc (cslotAddrA ss) (cslotAddrA sd) = + cdt.insert_with_cap st (L.wr s (stA (L.curW s)) sched.TS_RESTART) nc (capA st s (cslotAddrA ss)) + (cslotAddrA ss) (cslotAddrA sd) := by unfold cdt.insert; rw [capA_eq, bind_ok, hOS1.capA xs] + rw [hslc, decCnode_copyA (ar := ar) (a := a) (block := bB) (call := bC) (donate := bD) hlbl.1 hlbl.2.1 hlbl.2.2.1 + hlbl.2.2.2 hl2 hl4 hi hd0 hres hx0 h0 h2 h3 hroot hemp hsrc (capA_eq _ _) hsn hcop hn, hset, bind_ok, if_neg Bool.false_ne_true, ← e, hcdt0, bind_ok] + | @cnodeMove ps pd nc ac slot p r gs g extra lbl length buf index depth x0 si sdp root bB bC bD hnvm ss sd hCs hCd hsn hdn hdc hca hw hn hD hal hsh hbd hirq hno hok hsci hslot hslc hlb hl4 hl2 hi hd0 hres hx0 h0 h2 h3 hroot hemp hsrc hcop hcov hext hSC hpin => + left + obtain ⟨x, s2, σ2, hc0, hset, hcdt0, hc3, J3, K3⟩ := JSyscallA.cnodeMoveA (J := J) (hA := hA) (hM := hM) (hPl := hPl) (hfm := hfm) (hnvm := hnvm) (ss := ss) (sd := sd) (hCs := hCs) (hCd := hCd) (hsn := hsn) (hdn := hdn) (hdc := hdc) (hca := hca) (hw := hw) (hn := hn) (hD := hD) (hal := hal) (hsh := hsh) (hbd := hbd) (hirq := hirq) (hno := hno) (hok := hok) (hsci := hsci) (hslot := hslot) (hslc := hslc) (hlb := hlb) (hl4 := hl4) (hl2 := hl2) (hi := hi) (hd := hd0) (hres := hres) (hx0 := hx0) (h0 := h0) (h2 := h2) (h3 := h3) (hroot := hroot) (hemp := hemp) (hsrc := hsrc) (hcop := hcop) (hcov := hcov) (hext := hext) (hSC := hSC) + refine ⟨s2, a, σ2, ?_, ⟨x, hc0, hc3⟩, curW_keptA J.toJStage3A J3 K3.cur, A64RelS3.ofJ J3 (Sched.BodyOKA.ofKeep J hc0 K3)⟩ + have hlbl : ¬ lbl < fl4_abi.label.CNodeRevoke ∧ ¬ lbl > fl4_abi.label.CNodeRotate ∧ + lbl ≥ fl4_abi.label.CNodeCopy ∧ lbl ≤ fl4_abi.label.CNodeMutate := by + have := lblA_revoke; have := lblA_rotate; have := lblA_copy; have := lblA_mutate + have := lblA_mint; have := lblA_move + rcases hlb with h | h <;> subst h <;> refine ⟨?_, ?_, ?_, ?_⟩ <;> scalar_tac + rw [hslc, decCnode_copyA (ar := ar) (a := a) (block := bB) (call := bC) (donate := bD) hlbl.1 hlbl.2.1 hlbl.2.2.1 + hlbl.2.2.2 hl2 hl4 hi hd0 hres hx0 h0 h2 h3 hroot hemp hsrc (capA_eq _ _) hsn hcop hn, hset, bind_ok, if_pos rfl, hcdt0, bind_ok] + | @cnodeRotate ps pp pd np ap ns an slot p r gs g extra lbl length buf index depth x0 x1' pdata sdata bB bC bD hnvm ss sp sd hCs hCp hCd hsp hsd hsn hpn hdn hpc hdc hcp hwp hnp hDp halp hshp hbdp hirqp hop hokp hscp hcs hws hns hDs hals hshs hbds hirqs hos hoks hscs hslot hslc hlb hl8 hl2 hi hd0 hres hx0 h0 hx1 h1 h2 h5 hrot hus hup hcov hext hSC hpin => + left + obtain ⟨x, s1, s3, σ1, σ3, hc0, hset, hcdt0, hcdt1, hc4, hc5, J3, K3⟩ := JSyscallA.cnodeRotateA (J := J) (hA := hA) (hM := hM) (hPl := hPl) (hfm := hfm) (hnvm := hnvm) (ss := ss) (sp := sp) (sd := sd) (hCs := hCs) (hCp := hCp) (hCd := hCd) (hsp := hsp) (hsd := hsd) (hsn := hsn) (hpn := hpn) (hdn := hdn) (hpc := hpc) (hdc := hdc) (hcp := hcp) (hwp := hwp) (hnp := hnp) (hDp := hDp) (halp := halp) (hshp := hshp) (hbdp := hbdp) (hirqp := hirqp) (hop := hop) (hokp := hokp) (hscp := hscp) (hcs := hcs) (hws := hws) (hns := hns) (hDs := hDs) (hals := hals) (hshs := hshs) (hbds := hbds) (hirqs := hirqs) (hos := hos) (hoks := hoks) (hscs := hscs) (hslot := hslot) (hslc := hslc) (hlb := hlb) (hl8 := hl8) (hl2 := hl2) (hi := hi) (hd := hd0) (hres := hres) (hx0 := hx0) (h0 := h0) (hx1 := hx1) (h1 := h1) (h2 := h2) (h5 := h5) (hrot := hrot) (hus := hus) (hup := hup) (hcov := hcov) (hext := hext) (hSC := hSC) + refine ⟨s3, a, σ3, ?_, ⟨x, σ1, hc0, hc4, hc5⟩, curW_keptA J.toJStage3A J3 K3.cur, A64RelS3.ofJ J3 (Sched.BodyOKA.ofKeep J hc0 K3)⟩ + have hlbl : ¬ lbl < fl4_abi.label.CNodeRevoke ∧ ¬ lbl > fl4_abi.label.CNodeRotate := by + have := lblA_revoke; have := lblA_rotate + subst hlb; refine ⟨?_, ?_⟩ <;> scalar_tac + rw [hslc, decCnode_rotateA (ar := ar) (a := a) (block := bB) (call := bC) (donate := bD) hlb hlbl.1 hlbl.2 hl2 hl8 hi hd0 + hres hx0 h0 hx1 h1 h2 h5 hrot (capA_eq _ _) (capA_eq _ _) hus hup hns hnp, hset, bind_ok, if_neg hsd, hcdt0, bind_ok, hcdt1, bind_ok] + | @cnodeRotateSw ps pp np ap ns an slot p r gs g extra lbl length buf index depth x0 x1' pdata sdata bB bC bD hnvm ss sp hCs hCp hsp hsn hpn hsx hpx hcp hwp hnp hDp halp hshp hbdp hirqp hop hokp hscp hcs hws hns hDs hals hshs hbds hirqs hos hoks hscs hslot hslc hlb hl8 hl2 hi hd0 hres hx0 h0 hx1 h1 h2 h5 hrot hus hup hcov hext hSC hpin => + left + obtain ⟨x, s2, σ2, hc0, hset, hcdt0, hc3, J3, K3⟩ := JSyscallA.cnodeRotateSwA (J := J) (hA := hA) (hM := hM) (hPl := hPl) (hfm := hfm) (hnvm := hnvm) (ss := ss) (sp := sp) (hCs := hCs) (hCp := hCp) (hsp := hsp) (hsn := hsn) (hpn := hpn) (hsx := hsx) (hpx := hpx) (hcp := hcp) (hwp := hwp) (hnp := hnp) (hDp := hDp) (halp := halp) (hshp := hshp) (hbdp := hbdp) (hirqp := hirqp) (hop := hop) (hokp := hokp) (hscp := hscp) (hcs := hcs) (hws := hws) (hns := hns) (hDs := hDs) (hals := hals) (hshs := hshs) (hbds := hbds) (hirqs := hirqs) (hos := hos) (hoks := hoks) (hscs := hscs) (hslot := hslot) (hslc := hslc) (hlb := hlb) (hl8 := hl8) (hl2 := hl2) (hi := hi) (hd := hd0) (hres := hres) (hx0 := hx0) (h0 := h0) (hx1 := hx1) (h1 := h1) (h2 := h2) (h5 := h5) (hrot := hrot) (hus := hus) (hup := hup) (hcov := hcov) (hext := hext) (hSC := hSC) + refine ⟨s2, a, σ2, ?_, ⟨x, hc0, hc3⟩, curW_keptA J.toJStage3A J3 K3.cur, A64RelS3.ofJ J3 (Sched.BodyOKA.ofKeep J hc0 K3)⟩ + have hlbl : ¬ lbl < fl4_abi.label.CNodeRevoke ∧ ¬ lbl > fl4_abi.label.CNodeRotate := by + have := lblA_revoke; have := lblA_rotate + subst hlb; refine ⟨?_, ?_⟩ <;> scalar_tac + rw [hslc, decCnode_rotateA (ar := ar) (a := a) (block := bB) (call := bC) (donate := bD) hlb hlbl.1 hlbl.2 hl2 hl8 hi hd0 + hres hx0 h0 hx1 h1 h2 h5 hrot (capA_eq _ _) (capA_eq _ _) hus hup hns hnp, hset, bind_ok, if_pos rfl, hcdt0, bind_ok] + | @cnodeDelRv slot p r gs g extra lbl length buf bB bC bD slbl args ac excaps inv rr s3 a3 hCnt P hCl hCs hslot hcov hext hSC hslc hlb hlbl hlen hargs hca hcv hdec hrr hspec hpin => + left + subst hrr + have out : CnodeDelBodyOutA st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar Clock hPl.ipa s a σ + (Fl4SpecA64.decodeCnodeInvocation slbl (args.map UScalar.bv) ac excaps) (.Ok (), s3, a3) := by + rcases hlb with hl | hl + · have hv : lbl.val = 22 := by rw [hl]; exact lblA_delete + exact JSyscallA.cnodeDeleteAsmA s a σ m hPl J hCnt hslot hcov hext hSC P hCl hCs lbl slbl hlbl hv length buf + args hlen hargs _ ac hca hcv hslc excaps hdec + · have hv : lbl.val = 21 := by rw [hl]; exact lblA_revoke + exact JSyscallA.cnodeRevokeAsmA s a σ m hPl J hCnt hslot hcov hext hSC P hCl hCs lbl slbl hlbl hv length buf + args hlen hargs _ ac hca hcv hslc excaps hdec + rcases out with ⟨e, e', hr', -⟩ | ⟨inv', σ1, σ3, G', hrun, hset, hres, Q, R, hw, Ac', Bf', stack', J3, hend, -⟩ + · cases hr' + · have hinv : inv' = inv := Except.ok.inj (single_fstA (hrun.symm.trans hspec)) + subst hinv + rcases hres with ⟨-, hperf⟩ | ⟨h, -⟩ + · have hend' : BodyEndA σ σ3 m := hend + refine ⟨s3, a3, σ3, ?_, ⟨σ1, hset, hperf⟩, curW_keptA J.toJStage3A J3 hend'.2, A64RelS3.ofJ J3 hend'.1⟩ + rw [hslc, decodeCnode_programA, if_neg Bool.false_ne_true] + exact hdec + · cases h + | @cnodeDelRvPre slot p r gs g extra lbl length buf bB bC bD slbl args ac excaps inv rr s3 a3 hCnt P hCl hCs hslot hcov hext hSC hslc hlb hlbl hlen hargs hca hcv hdec hrr hspec hpin => + right + subst hrr + have out : CnodeDelBodyOutA st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar Clock hPl.ipa s a σ + (Fl4SpecA64.decodeCnodeInvocation slbl (args.map UScalar.bv) ac excaps) (.Err .Preempted, s3, a3) := by + rcases hlb with hl | hl + · have hv : lbl.val = 22 := by rw [hl]; exact lblA_delete + exact JSyscallA.cnodeDeleteAsmA s a σ m hPl J hCnt hslot hcov hext hSC P hCl hCs lbl slbl hlbl hv length buf + args hlen hargs _ ac hca hcv hslc excaps hdec + · have hv : lbl.val = 21 := by rw [hl]; exact lblA_revoke + exact JSyscallA.cnodeRevokeAsmA s a σ m hPl J hCnt hslot hcov hext hSC P hCl hCs lbl slbl hlbl hv length buf + args hlen hargs _ ac hca hcv hslc excaps hdec + rcases out with ⟨e, e', hr', -, -, hrun, -⟩ | ⟨inv', σ1, σ3, G', hrun, hset, hres, Q, R, hw, Ac', Bf', stack', J3, hend, hclk⟩ + · cases single_fstA (hrun.symm.trans hspec) + · have hinv : inv' = inv := Except.ok.inj (single_fstA (hrun.symm.trans hspec)) + subst hinv + rcases hres with ⟨h, -⟩ | ⟨-, hperf⟩ + · cases h + · obtain ⟨hpp, hcur, hns, hps, hown⟩ : BodyPreA σ3 m ∧ σ3.curThread = σ.curThread ∧ + (∀ t, σ3.schedulerAction ≠ .switchThread t) ∧ DelPreSchedA σ.curThread σ3 ∧ DelOwnA σ3 := hend + have pre : PreExitA σ3 := ⟨hns, fun d p => by rw [hcur]; exact hps.1 d p, + fun x hx => hps.2.1 x (by rw [← hcur]; exact hx), by rw [hcur]; exact hps.2.2.1, + fun hr x hx => hps.2.2.2 hr x (by rw [← hcur]; exact hx), hown.1, hown.2⟩ + refine ⟨s3, a3, σ3, ?_, ⟨σ1, hset, hperf, pre⟩, curW_keptA J.toJStage3A J3 hcur, + ⟨{ Z with G := G', Q := Q, R := R, hw := hw }, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, rfl, J3, + hpp.toPP⟩, hclk, inv', hpin, fun _ h => h⟩ + rw [hslc, decodeCnode_programA, if_neg Bool.false_ne_true] + exact hdec + +/-- **Every Cn arm in scope runs and keeps the record** (by result: `A64RelPS3` after the preempted deletion). -/ +theorem CnScopeA.sound [PlainStore st] {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} + {κ : U64 → U64} {n : Nat} {ar : machine.Arch A} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + {lbl len slot : U64} {blk call don fp : Bool} {buf : U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} + {Out : Fl4SpecA64.State Unit → Prop} + (h : CnScopeA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar hPl.ipa s a σ m Clock lbl len slot blk call don fp buf + extra Out) : + DispPostRA st AR ar hPl.ipa n Z.Cs s a m Clock lbl len slot blk call don fp buf extra Out := + (CnScopeA.soundOk hA hM hPl J hfm h).elim (fun h => h.post.toR) + fun ⟨s', a', σ', e, o, c, R, hclk, _⟩ => ⟨_, s', a', σ', e, o, c, R, fun _ => hclk⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3DispDomA.lean b/proofs/Fl4ProofsA64/P3/Stage3DispDomA.lean new file mode 100644 index 000000000..365503efd --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3DispDomA.lean @@ -0,0 +1,71 @@ +/- +P3 (aarch64, A64-P3 Lift 1b, T3 dispatch): **the Domain dispatch group** (`Stage3DispBaseA`): `DomainSetSet` on any +thread, the caller included (`P3/Stage3LiftDomainA`'s `setDomainA`). + +Named premises: [P] `hCs`; [S] `h1` (`numNodes = 1`: the target is on this core and its stall stalls nothing, +`P3/Stage3StallA`). +-/ +import Fl4ProofsA64.P3.Stage3LiftDomainA +import Fl4ProofsA64.P3.Stage3DispBaseA +import Fl4ProofsA64.P3.Stage3StallA +import Fl4ProofsA64.P3.Stage3LiftBodyA +import Fl4ProofsA64.P3.DelNtfnRecA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST + +noncomputable section + +section +variable {S : Type} (st : machine.Store S) [L : LawfulStore st] {A : Type} + +/-- **The Domain arms in scope** (module docstring). -/ +inductive DomScopeA (AR : A → HwRootA → Prop) (Z : XSetsA) (OS : Set U64) (ar : machine.Arch A) (s : S) (a : A) + (σ : Fl4SpecA64.State Unit) : + U64 → U64 → U64 → Bool → Bool → Bool → Bool → U64 → Aeneas.Std.Array obj.Slot 3#usize → + (Fl4SpecA64.State Unit → Prop) → Prop + | set (slot : U64) (hsl : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Domain) + (len buf x0 t : U64) (blk call don : Bool) (extra : Aeneas.Std.Array obj.Slot 3#usize) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hl : len ≠ 0#u64) (hd : msg.syscall_arg st s (L.curW s) buf 0#u64 = ok 0#u64) + (he : Array.index_usize extra 0#usize = ok x0) (hx : x0 ≠ 0#u64) (hx0 : x0 ∈ OS) + (hcx : capA st s x0 = .Thread t) + (hCs : ∀ c ∈ Z.Cs, c.val < Fl4SpecA64.fl4A64Platform.numNodes) (h1 : Fl4SpecA64.fl4A64Platform.numNodes = 1) : + DomScopeA AR Z OS ar s a σ fl4_abi.label.DomainSetSet len slot blk call don false buf extra + (fun σ' => ∃ σ1, + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + Fl4SpecA64.invokeDomain (Z := Unit) (.invokeDomainSet (specPtr t) 0) σ1 = ({((), σ')}, False)) + +variable {st} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {ar : machine.Arch A} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- **The Domain group is sound.** -/ +theorem DomScopeA.sound (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) + s a σ m) + (hfm : A64FrameMasks st s) + {lbl len slot : U64} {blk call don fp : Bool} {buf : U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} + {Out : Fl4SpecA64.State Unit → Prop} + (h : DomScopeA st AR Z OS ar s a σ lbl len slot blk call don fp buf extra Out) : + DispPostA st AR ar hPl.ipa n Z.Cs s a m lbl len slot blk call don fp buf extra Out := by + cases h with + | set slot hsl hcov hcap len buf x0 t blk call don extra hext hSC hl hd he hx hx0 hcx hCs h1 => + have ht : t ∈ Z.T := J.tcbs ▸ (J.toJStage3A.slotCapTy hx0).1.2.2.2.2.2 t hcx + have T := J.toJStage3A.toTA J.sact J.noSwitch + have hon : L.mem s (affA t) = L.cpu s := T.onCore_single hCs h1 ht + have hstall := J.stallTA hCs h1 ht + obtain ⟨σ1, σ2, s2, h1, h2, hdec, ⟨xc, hxc, hra⟩, Q, J2⟩ := + J.setDomainA hA hM hPl hfm hsl hcov hcap hext hSC hl hd he hx hx0 hcx hon hstall + have hcur : σ2.curThread = σ.curThread := by + obtain ⟨f, h | h⟩ := hra <;> rw [h] <;> rfl + exact ⟨_, s2, a, σ2, hdec, ⟨σ1, h1, h2⟩, Sched.curW_keptA J.toJStage3A J2 hcur, + A64RelS3.ofJ J2 (Sched.BodyOKA.ofRqAct J hxc hra), (by intro e; cases e)⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3DispErrA.lean b/proofs/Fl4ProofsA64/P3/Stage3DispErrA.lean new file mode 100644 index 000000000..9834705b3 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3DispErrA.lean @@ -0,0 +1,126 @@ +/- +P3 (aarch64, A64-P3 Lift 1b, T3 dispatch): **the `Err` returns of the non-arch caps** (`Stage3DispBaseA`). + +* `NonArchA cap`: the cap kinds outside the arch decoders (Null, Untyped, Endpoint, Notification, Reply, CNode, + Thread, IRQ handler, Zombie, Domain, SchedContext, SchedControl). IRQ control has its own constructor below. +* `syscall.decode_invocation.errKA`: on such a cap an `Err` other than `Preempted` comes with the entry state and arch + state (`P3/Stage3ErrKeepA`, `P3/Stage3ErrKeep2A`), whatever the label: the decode checks of every arm, the TCB + arms' included, come before the caller's Restart, and the perform steps after it fail only by preemption. +* `ErrScopeA.nonArch` / `ErrScopeA.sound`: such an outcome at record level; the spec state is kept (`Out σ' := σ' = σ`). + The spec decoder's matching error is not stated here (per cap kind). + +* `ErrScopeA.irqControl`: an `Err` of the IRQ control cap, the arch state possibly changed by `msi_map` + (`P3/Stage3ErrIrqA`). + +Named premises: [P] `ArchTotal`, `hCs`, `MsiKeepA`; [S] `h1` (`numNodes = 1`: the decoder stalls stall nothing, +`P3/Stage3StallA`). New module. +-/ +import Fl4ProofsA64.P3.Stage3ErrKeep2A +import Fl4ProofsA64.P3.Stage3DispBaseA +import Fl4ProofsA64.P3.Stage3StallA +import Fl4ProofsA64.P3.Stage3ErrIrqA +import Fl4ProofsA64.P3.MachKeepA +import Fl4ProofsA64.P3.IrqInvokeDispatchA +import Fl4ProofsA64.P3.Stage3LiftBodyA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST + +noncomputable section + +/-- The cap kinds outside the arch decoders (module docstring). -/ +def NonArchA : capability.Cap → Prop + | .Null | .Untyped .. | .Endpoint .. | .Notification .. | .Reply .. | .CNode .. | .Thread _ + | .IrqHandler _ | .Zombie .. | .Domain | .SchedContext .. | .SchedControl _ => True + | _ => False + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +set_option maxHeartbeats 8000000 in +/-- **A non-arch decode's `Err` other than `Preempted` keeps the state** (module docstring). -/ +theorem syscall.decode_invocation.errKA (hA : P0.ArchTotal ar) (s : S) (a : A) (lbl len : U64) (slot : obj.Slot) + (cap : capability.Cap) (blk call don fp : Bool) (buf : U64) (extra : Aeneas.Std.Array obj.Slot 3#usize) + (hcap : NonArchA cap) (hst : StallSitesA st ar s a cap) : + syscall.decode_invocation st ar s a lbl len slot cap blk call don fp buf extra ⦃ ErrKeepA s a ⦄ := by + have hS : P0.StoreTotal st := L.total + have h_ut := @untyped.decode.errKA S A st ar L hA s a + have h_cn := @cnode.decode.errKA S A st ar L hA s a + have h_irqh := @interrupt.decode_irq_handler.errKA S A st ar L hA s a + have h_dom := fun lbl len buf extra => @domain.decode.errKA S A st ar L hA s a lbl len buf extra + have h_scc := @schedcontext.decode_sched_control.errKA S A st ar L hA s a + unfold syscall.decode_invocation + cases cap + case Thread t => + have h_tcb := fun lbl c => tcb.decode.errKA hA s a lbl t c (hst.1 t rfl) + errk_steps <;> errk_close + case SchedContext p b => + have h_sc := fun lbl call extra => schedcontext.decode.errKA hA s a lbl p call extra (hst.2 p b rfl) + errk_steps <;> errk_close + all_goals (simp only [NonArchA] at hcap <;> (try exact hcap.elim) <;> errk_steps <;> errk_close) + +end + +section +variable {S : Type} (st : machine.Store S) [L : LawfulStore st] {A : Type} + +/-- **The non-arch `Err` returns** (module docstring). -/ +inductive ErrScopeA (AR : A → HwRootA → Prop) (Z : XSetsA) (OS : Set U64) (ar : machine.Arch A) (s : S) (a : A) + (σ : Fl4SpecA64.State Unit) : + U64 → U64 → U64 → Bool → Bool → Bool → Bool → U64 → Aeneas.Std.Array obj.Slot 3#usize → + (Fl4SpecA64.State Unit → Prop) → Prop + | nonArch (hA : P0.ArchTotal ar) (lbl len slot : U64) (blk call don fp : Bool) (buf : U64) + (extra : Aeneas.Std.Array obj.Slot 3#usize) (hcap : NonArchA (capA st s slot)) + (hCs : ∀ c ∈ Z.Cs, c.val < Fl4SpecA64.fl4A64Platform.numNodes) (h1 : Fl4SpecA64.fl4A64Platform.numNodes = 1) + (hsl : slot ∈ OS) (e : error.Exc) (s' : S) (a' : A) + (hd : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don fp buf extra = + ok (.Err e, s', a')) + (hne : e ≠ .Preempted) : + ErrScopeA AR Z OS ar s a σ lbl len slot blk call don fp buf extra (fun σ' => σ' = σ) + | irqControl (hA : P0.ArchTotal ar) (hM : MsiKeepA ar AR) (lbl len slot : U64) (blk call don fp : Bool) (buf : U64) + (extra : Aeneas.Std.Array obj.Slot 3#usize) (hcap : capA st s slot = .IrqControl) (e : error.Exc) (s' : S) + (a' : A) + (hd : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don fp buf extra = + ok (.Err e, s', a')) + (hne : e ≠ .Preempted) : + ErrScopeA AR Z OS ar s a σ lbl len slot blk call don fp buf extra (fun σ' => σ' = σ) + +variable {st} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {ar : machine.Arch A} {nA : U64} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- **The non-arch `Err` group is sound.** -/ +theorem ErrScopeA.sound + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {lbl len slot : U64} {blk call don fp : Bool} {buf : U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} + {Out : Fl4SpecA64.State Unit → Prop} + (h : ErrScopeA st AR Z OS ar s a σ lbl len slot blk call don fp buf extra Out) : + DispPostA st AR ar nA n Z.Cs s a m lbl len slot blk call don fp buf extra Out := by + cases h with + | nonArch hA _ _ _ _ _ _ _ _ _ hcap hCs h1 hsl e s' a' hd hne => + have hst := J.stallSitesA hCs h1 hsl + have k := syscall.decode_invocation.errKA hA s a lbl len slot (capA st s slot) blk call don fp buf extra hcap hst + rw [hd, WP.spec_ok] at k + have e2 := Prod.mk.inj (k e rfl hne) + refine ⟨_, s', a', σ, hd, rfl, by rw [e2.1], ?_, fun h => hne (by injection h)⟩ + rw [e2.1, e2.2] + exact A64RelS3.ofJ J.toJStage3A J.bodyOKA + | irqControl hA hM _ _ _ _ _ _ _ _ _ hcap e s' a' hd hne => + have k := interrupt.decode_irq_control.errKA (st := st) (ar := ar) hA s a (fun a => AR a Z.hw) J.j.1.ar + (fun irq dev ev cpu => hM.msi_map a irq dev ev cpu _ J.j.1.ar) lbl len slot buf extra + have hd' := hd + rw [hcap, decodeIrqControl_programA] at hd' + rw [hd', WP.spec_ok] at k + obtain ⟨es, har⟩ := k e rfl hne + dsimp only at es har + refine ⟨_, s', a', σ, hd, rfl, by rw [es], ?_, fun h => hne (by injection h)⟩ + rw [es] + exact A64RelS3.ofJ (J.toJStage3A.arStateA har) J.bodyOKA + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3DispFlushA.lean b/proofs/Fl4ProofsA64/P3/Stage3DispFlushA.lean new file mode 100644 index 000000000..52b5b60b7 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3DispFlushA.lean @@ -0,0 +1,140 @@ +/- +P3 (aarch64, A64-P3 Lift 1, T3 dispatch): **the cache-maintenance dispatch group** (`Stage3DispBaseA`): the +VSpace-root, Stage-2 root and frame flushes (`P3/Stage3LiftFlushA`). + +Named premises: [P] `ArchKeepsA`, `S2IpaE` (Stage-2 and frame constructors). + +Pinning: each constructor's extra caps and message words are the front end's (`FrontPinA`); the spec decode's +`CapRef` argument is `[]` (the cache-maintenance decoders do not read it). +-/ +import Fl4ProofsA64.P3.Stage3LiftFlushA +import Fl4ProofsA64.P3.Stage3DispBaseA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +section +variable {S : Type} (st : machine.Store S) [L : LawfulStore st] {A : Type} + +/-- The spec outcome of one cache-maintenance invocation with decoder result `D`. -/ +def FlushOutA (σ : Fl4SpecA64.State Unit) + (D : Set (Except Fl4SpecA64.SyscallError Fl4ArchInvocation × Fl4SpecA64.State Unit) × Prop) + (σ' : Fl4SpecA64.State Unit) : Prop := + (σ' = σ ∧ ∃ e', D = ({(.error e', σ)}, False)) ∨ + ∃ inv x rv, (.ok inv, σ) ∈ D.1 ∧ ¬ D.2 ∧ σ.kheap σ.curThread = some (.tcb x) ∧ + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + (.ok rv, σ') ∈ ((fl4ArchPerformInvocation (Z := Unit) inv).run (stUpdA σ σ.curThread x .restart)).1 + +/-- **The cache-maintenance arms in scope** (module docstring). -/ +inductive FlScopeA (AR : A → HwRootA → Prop) (Z : XSetsA) (OS : Set U64) (ar : machine.Arch A) (s : S) (a : A) + (σ : Fl4SpecA64.State Unit) : + U64 → U64 → U64 → Bool → Bool → Bool → Bool → U64 → Aeneas.Std.Array obj.Slot 3#usize → + (Fl4SpecA64.State Unit → Prop) → Prop + | vsRoot + (K : ArchKeepsA ar AR) (cte : Fl4SpecA64.CslotPtr) (hab : AbsSlotA Z.th cte) + (hcov : SlotCov st OS s (cslotAddrA cte)) (ptr asid : U64) (mp : Bool) + (hcap : capA st s (cslotAddrA cte) = .Pml4 ptr mp asid) + (lbl length buf : U64) (hl : 42 ≤ lbl.val ∧ lbl.val ≤ 45) (blk call don fp : Bool) + (extra : Aeneas.Std.Array obj.Slot 3#usize) (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) + (args : List U64) (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) + (exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) (mi : Fl4SpecA64.MessageInfo) (c : Fl4SpecA64.Cap) + (buffer : Option Fl4SpecA64.ObjRef) + (hF : FrontPinA Z.th σ (cslotAddrA cte) mi cte c exs buffer (args.map UScalar.bv)) : + FlScopeA AR Z OS ar s a σ lbl length (cslotAddrA cte) blk call don fp buf extra + (FlushOutA σ ((fl4ArchDecodeInvocation (Z := Unit) call (fl4LabelOf lbl.bv) (args.map UScalar.bv) [] cte + (.pageTableCap (specPtr ptr) .vsRootPtT (mapDataA mp asid 0#u64)) exs).run σ)) + | s2Root + (K : ArchKeepsA ar AR) (hIpa : S2IpaE ar a) (cte : Fl4SpecA64.CslotPtr) (hab : AbsSlotA Z.th cte) + (hcov : SlotCov st OS s (cslotAddrA cte)) (ptr asid va : U64) (mp : Bool) + (hcap : capA st s (cslotAddrA cte) = .Ept ptr 0#u64 mp asid va) + (lbl length buf : U64) (blk call don fp : Bool) + (extra : Aeneas.Std.Array obj.Slot 3#usize) (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) + (args : List U64) (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) + (exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) (mi : Fl4SpecA64.MessageInfo) (c : Fl4SpecA64.Cap) + (buffer : Option Fl4SpecA64.ObjRef) + (hF : FrontPinA Z.th σ (cslotAddrA cte) mi cte c exs buffer (args.map UScalar.bv)) : + FlScopeA AR Z OS ar s a σ lbl length (cslotAddrA cte) blk call don fp buf extra + (FlushOutA σ ((fl4ArchDecodeInvocation (Z := Unit) call (fl4LabelOf lbl.bv) (args.map UScalar.bv) [] cte + (.ext (.stage2VSpaceCap (specPtr ptr) (asidDataA mp asid))) exs).run σ)) + | frame + (K : ArchKeepsA ar AR) (hIpa : S2IpaE ar a) (cte : Fl4SpecA64.CslotPtr) (hab : AbsSlotA Z.th cte) + (hcov : SlotCov st OS s (cslotAddrA cte)) (fpa rt asid va : U64) (sz : capability.FrameSize) (dev mp e : Bool) + (hcap : capA st s (cslotAddrA cte) = .Frame fpa sz rt dev mp e asid va) + (lbl length buf : U64) (hl : 51 ≤ lbl.val ∧ lbl.val ≤ 54) (blk call don fp : Bool) + (extra : Aeneas.Std.Array obj.Slot 3#usize) (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) + (args : List U64) (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) + (x0 : U64) (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) + (exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) + (hex : ExtraRelA Z.th (L.mem s) x0 (exs.map Prod.fst)) (mi : Fl4SpecA64.MessageInfo) (c : Fl4SpecA64.Cap) + (buffer : Option Fl4SpecA64.ObjRef) + (hF : FrontPinA Z.th σ (cslotAddrA cte) mi cte c exs buffer (args.map UScalar.bv)) : + FlScopeA AR Z OS ar s a σ lbl length (cslotAddrA cte) blk call don fp buf extra + (FlushOutA σ ((fl4ArchDecodeInvocation (Z := Unit) call (fl4LabelOf lbl.bv) (args.map UScalar.bv) [] cte + (absFrameE e (specPtr fpa) (vmRightsOfA rt) (frameSizeOfA sz) dev (mapDataA mp asid va)) exs).run σ)) + +variable {st} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {ar : machine.Arch A} {nA : U64} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- `FlushPostA` gives the dispatch post. -/ +theorem FlushPostA.dispA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {lbl len slot : U64} {blk call don fp : Bool} {buf : U64} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + {D : Set (Except Fl4SpecA64.SyscallError Fl4ArchInvocation × Fl4SpecA64.State Unit) × Prop} + (h : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don fp buf extra + ⦃ r => FlushPostA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar nA m σ D r ⦄) : + DispPostA st AR ar nA n Z.Cs s a m lbl len slot blk call don fp buf extra (FlushOutA σ D) := by + obtain ⟨⟨r, s', a'⟩, e, p⟩ := spec_existsA h + cases r with + | Err er => + obtain ⟨J', e', hD, hrel⟩ := p + exact ⟨_, s', a', σ, e, Or.inl ⟨rfl, e', hD⟩, Sched.curW_keptA J.toJStage3A J' rfl, A64RelS3.ofJ J' J.bodyOKA, + fun h => hrel.ne_preempted (by injection h)⟩ + | Ok u => + cases u + obtain ⟨inv, x, σ', rv, h1, h2, h3, h4, h5, BK, J'⟩ := p + exact ⟨_, s', a', σ', e, Or.inr ⟨inv, x, rv, h1, h2, h3, h4, h5⟩, Sched.curW_keptA J.toJStage3A J' BK.cur, + A64RelS3.ofJ J' (Sched.BodyOKA.ofKeep J h3 BK), (by intro e; cases e)⟩ + +/-- **The cache-maintenance group is sound.** -/ +theorem FlScopeA.sound + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {lbl len slot : U64} {blk call don fp : Bool} {buf : U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} + {Out : Fl4SpecA64.State Unit → Prop} + (h : FlScopeA st AR Z OS ar s a σ lbl len slot blk call don fp buf extra Out) : + DispPostA st AR ar nA n Z.Cs s a m lbl len slot blk call don fp buf extra Out := by + cases h with + | vsRoot K cte hab hcov ptr asid mp hcap lbl length buf hl blk call don fp extra hext args hlen hargs exs => + refine FlushPostA.dispA J (H := H) (U := U) (Ac := Ac) (Pp := Pp) (Tb := Tb) (Lv := Lv) (ia := ia) (ie := ie) + (N := N) (E := E) (Rp := Rp) (Bf := Bf) (stack := stack) (κ := κ) (n := n) (Z := Z) (OS := OS) ?_ + rw [hcap] + exact J.vsFlushA K cte hab hcov ptr asid mp hcap lbl len buf hl blk call don fp extra hext args hlen hargs + [] exs + | s2Root K hIpa cte hab hcov ptr asid va mp hcap lbl length buf blk call don fp extra hext args hlen hargs exs => + refine FlushPostA.dispA J (H := H) (U := U) (Ac := Ac) (Pp := Pp) (Tb := Tb) (Lv := Lv) (ia := ia) (ie := ie) + (N := N) (E := E) (Rp := Rp) (Bf := Bf) (stack := stack) (κ := κ) (n := n) (Z := Z) (OS := OS) ?_ + rw [hcap] + exact J.s2FlushA K hIpa cte hab hcov ptr asid va mp hcap lbl len buf blk call don fp extra hext args hlen + hargs [] exs + | frame K hIpa cte hab hcov fpa rt asid va sz dev mp e hcap lbl length buf hl blk call don fp extra hext args hlen + hargs x0 hx0 exs hex => + refine FlushPostA.dispA J (H := H) (U := U) (Ac := Ac) (Pp := Pp) (Tb := Tb) (Lv := Lv) (ia := ia) (ie := ie) + (N := N) (E := E) (Rp := Rp) (Bf := Bf) (stack := stack) (κ := κ) (n := n) (Z := Z) (OS := OS) ?_ + rw [hcap] + exact J.frFlushA K hIpa cte hab hcov fpa rt asid va sz dev mp e hcap lbl len buf hl blk call don fp extra hext + args hlen hargs x0 hx0 [] exs hex + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3DispIpcA.lean b/proofs/Fl4ProofsA64/P3/Stage3DispIpcA.lean new file mode 100644 index 000000000..77e08677c --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3DispIpcA.lean @@ -0,0 +1,293 @@ +/- +P3 (aarch64, A64-P3 Stage 3, T3 dispatch): **the IPC dispatch group** (`Stage3DispBaseA`): Notification send on the arms +waking no thread (`JSyscallA.signalQuietA`), waking a waiting thread (`JSyscallA.signalWaitingA`) and waking a +receive-blocked bound thread, with or without a reply object (`JSyscallA.signalBoundRecvA`), CNode CancelBadgedSends +(`JSyscallA.cancelBadgedA`). + +Endpoint send / call (the `*DispA` wrappers of `P3/Stage3IpcBodyA`): non-blocking with no receiver, blocking with no +receiver, to a waiting receiver (plain or call without grant; donating call; non-donating call); Reply (detached, live, +to a faulted caller). + +Pending lift: Notification send to a bound thread running a VCPU, the `Err` returns. +A new arm is a constructor of `IpcScopeA` and a case of `IpcScopeA.sound`. + +Named premises: [P] `MaxSpi`, `ArchPlat`, `A64FrameMasks`, `hc`, `hCs`, `hmb`, `hmax`, `htu`; [S] `h1` +(`numNodes = 1`, CancelBadgedSends and the waking Notification sends). +New module. +-/ +import Fl4ProofsA64.P3.Stage3DispBaseA +import Fl4ProofsA64.P3.Stage3SignalIdleA +import Fl4ProofsA64.P3.Stage3SigBoundRecA +import Fl4ProofsA64.P3.Stage3CancelBadgedA +import Fl4ProofsA64.P3.Stage3IpcBodyA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +section +variable {S : Type} (st : machine.Store S) [L : LawfulStore st] {A : Type} + +/-- **The IPC arms in scope** (module docstring), indexed as the other groups. -/ +inductive IpcScopeA (Z : XSetsA) (OS N E Rp : Set U64) (ar : machine.Arch A) (s : S) (σ : State Unit) (m : McState) : + U64 → U64 → U64 → Bool → Bool → Bool → Bool → U64 → Aeneas.Std.Array obj.Slot 3#usize → + (State Unit → Prop) → Prop + | signalQuiet + {p badge slot lbl len buf : U64} {recv blk call don fp : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + {x : Notification} + (hc : m.curCore < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Notification p badge true recv) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hp : p ∈ N) (hk : σ.kheap (specPtr p) = some (.notification x)) (hq : QuietSigA σ x) : + IpcScopeA Z OS N E Rp ar s σ m lbl len slot blk call don fp buf extra + (fun σ' => ∃ xc σ1, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + sendSignal (Z := Unit) (specPtr p) badge.bv σ1 = ({((), σ')}, False)) + | signalWaiting + {p badge slot lbl len buf : U64} {recv blk call don fp : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + {x : Notification} {q : List ObjRef} + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Notification p badge true recv) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hp : p ∈ N) (hk : σ.kheap (specPtr p) = some (.notification x)) (hw : x.ntfnObj = .waitingNtfn q) : + IpcScopeA Z OS N E Rp ar s σ m lbl len slot blk call don fp buf extra + (fun σ' => ∃ xc σ1, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ((), σ') ∈ (sendSignal (Z := Unit) (specPtr p) badge.bv σ1).1) + | signalBoundRecv + {p badge slot lbl len buf : U64} {recv blk call don fp : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + {x : Notification} {t e' : ObjRef} {xt : Tcb} {ro : Option ObjRef} {d : ReceiverPayload} + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Notification p badge true recv) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hp : p ∈ N) (hk : σ.kheap (specPtr p) = some (.notification x)) (hidle : x.ntfnObj = .idleNtfn) + (hb : x.ntfnBoundTcb = some t) (hkt : σ.kheap t = some (.tcb xt)) + (hs : xt.tcbState = .blockedOnReceive e' ro d) : + IpcScopeA Z OS N E Rp ar s σ m lbl len slot blk call don fp buf extra + (fun σ' => ∃ xc σ1, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ((), σ') ∈ (sendSignal (Z := Unit) (specPtr p) badge.bv σ1).1) + | cancelBadged + {slot lbl len buf index depth dest p b c0 cr cgs cg : U64} {blk call don : Bool} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .CNode c0 cr cgs cg) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hlbl : lbl = fl4_abi.label.CNodeCancelBadgedSends) (hl2 : ¬ len < 2#u64) + (hi : msg.syscall_arg st s (L.curW s) buf 0#u64 = ok index) + (hd : msg.syscall_arg st s (L.curW s) buf 1#u64 = ok depth) + (hres : cspace.lookup_target_slot st s (.CNode c0 cr cgs cg) index depth = ok (.Ok dest)) + (hdc : capA st s dest = .Endpoint p b true true true true) (hdOS : dest ∈ OS) : + IpcScopeA Z OS N E Rp ar s σ m lbl len slot blk call don false buf extra + (fun σ' => ∃ xc σ1, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + (.ok (), σ') ∈ ((invokeCnode (Z := Unit) + (.cancelBadgedSendsCall (.endpointCap (specPtr p) b.bv Fl4SpecA64.allRights))).run σ1).1) + | sendNoblock + {p badge slot lbl len buf : U64} {recv grant grantReply call don fp : Bool} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + {x : Endpoint} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) + (hcap : capA st s slot = .Endpoint p badge true recv grant grantReply) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) + (hp : p ∈ E) (hk : σ.kheap (specPtr p) = some (.endpoint x)) (hc : CanBlockA false x) : + IpcScopeA Z OS N E Rp ar s σ m lbl len slot false call don fp buf extra + (fun σ' => setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ')}, False) ∧ + sendIpc (Z := Unit) false call badge.bv grant grantReply don σ.curThread (specPtr p) σ' = + ({((), σ')}, False)) + | sendBlock + {p badge slot lbl len buf : U64} {recv grant grantReply call don fp : Bool} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + {x : Endpoint} + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) + (hcap : capA st s slot = .Endpoint p badge true recv grant grantReply) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hp : p ∈ E) (hk : σ.kheap (specPtr p) = some (.endpoint x)) (hc : CanBlockA false x) : + IpcScopeA Z OS N E Rp ar s σ m lbl len slot true call don fp buf extra + (fun σ' => ∃ σ1, setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ((), σ') ∈ (sendIpc (Z := Unit) true call badge.bv grant grantReply don σ.curThread (specPtr p) σ1).1) + | sendRecv + {p badge slot lbl len buf : U64} {recv grant grantReply blk call don fp : Bool} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + {d : U64} {q : List ObjRef} + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) + (hcap : capA st s slot = .Endpoint p badge true recv grant grantReply) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hp : p ∈ E) (hk : σ.kheap (specPtr p) = some (.endpoint (.recvEP (specPtr d :: q)))) + (hsnd : ∀ x, σ.kheap σ.curThread = some (.tcb x) → x.tcbFault = none ∧ (grant = false ∨ + ipcBufOfA false x = none ∨ + unat (dataToMessageInfo (x.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0)) + (hbr0 : (call = false ∧ don = false) ∨ (call = true ∧ grant = false ∧ grantReply = false)) : + IpcScopeA Z OS N E Rp ar s σ m lbl len slot blk call don fp buf extra + (fun σ' => ∃ σ1, setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ((), σ') ∈ (sendIpc (Z := Unit) blk call badge.bv grant grantReply don σ.curThread (specPtr p) σ1).1) + | sendDonate + {p badge slot lbl len buf : U64} {recv grant grantReply blk fp : Bool} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + {d : U64} {q : List ObjRef} + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) + (hcap : capA st s slot = .Endpoint p badge true recv grant grantReply) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hp : p ∈ E) (hk : σ.kheap (specPtr p) = some (.endpoint (.recvEP (specPtr d :: q)))) + (hsnd : ∀ x, σ.kheap σ.curThread = some (.tcb x) → x.tcbFault = none ∧ (grant = false ∨ + ipcBufOfA false x = none ∨ + unat (dataToMessageInfo (x.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0)) + (hg : grant = true ∨ grantReply = true) + (hrcv : ∀ y, σ.kheap (specPtr d) = some (.tcb y) → + y.tcbSchedContext = none ∧ ∃ e r dd, y.tcbState = .blockedOnReceive e (some r) dd) + (hV : L.mem s (core.num.U64.wrapping_add (L.curW s) 3664#u64) = 0#u64) : + IpcScopeA Z OS N E Rp ar s σ m lbl len slot blk true true fp buf extra + (fun σ' => ∃ σ1, setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ((), σ') ∈ (sendIpc (Z := Unit) blk true badge.bv grant grantReply true σ.curThread (specPtr p) σ1).1) + | sendNoDon + {p badge slot lbl len buf : U64} {recv grant grantReply blk fp : Bool} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + {d : U64} {q : List ObjRef} + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) + (hcap : capA st s slot = .Endpoint p badge true recv grant grantReply) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hp : p ∈ E) (hk : σ.kheap (specPtr p) = some (.endpoint (.recvEP (specPtr d :: q)))) + (hsnd : ∀ x, σ.kheap σ.curThread = some (.tcb x) → x.tcbFault = none ∧ (grant = false ∨ + ipcBufOfA false x = none ∨ + unat (dataToMessageInfo (x.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0)) + (hg : grant = true ∨ grantReply = true) + (hrcv : ∀ y, σ.kheap (specPtr d) = some (.tcb y) → + y.tcbSchedContext ≠ none ∧ ∃ e r dd, y.tcbState = .blockedOnReceive e (some r) dd) : + IpcScopeA Z OS N E Rp ar s σ m lbl len slot blk true true fp buf extra + (fun σ' => ∃ σ1, setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ((), σ') ∈ (sendIpc (Z := Unit) blk true badge.bv grant grantReply true σ.curThread (specPtr p) σ1).1) + | replyDetached + {r slot lbl len buf : U64} {grant blk call don fp : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} {x : Reply} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Reply r grant) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) + (hr : r ∈ Rp) (hk : σ.kheap (specPtr r) = some (.reply x)) (hdet : x.replyTcb = none) : + IpcScopeA Z OS N E Rp ar s σ m lbl len slot blk call don fp buf extra + (fun σ' => setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ')}, False) ∧ + doReplyTransfer (Z := Unit) σ.curThread (specPtr r) grant σ' = ({((), σ')}, False)) + | replyLive + {r slot lbl len buf d : U64} {grant blk call don fp : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} {xr : Reply} + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hag : fl4MinBudget = MIN_BUDGET) (hmb : ∀ a, Fl4MinBudgetAgreeA ar a) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Reply r grant) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hr : r ∈ Rp) (hk : σ.kheap (specPtr r) = some (.reply xr)) (hcl : xr.replyTcb = some (specPtr d)) + (hsnd : ∀ x, σ.kheap σ.curThread = some (.tcb x) → x.tcbFault = none ∧ (grant = false ∨ + ipcBufOfA false x = none ∨ + unat (dataToMessageInfo (x.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0)) + (hrcv : ∀ y, σ.kheap (specPtr d) = some (.tcb y) → + y.tcbState = .blockedOnReply (specPtr r) ∧ y.tcbFault = none) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) (hconv : Fl4TicksToUsLegacyAgree) : + IpcScopeA Z OS N E Rp ar s σ m lbl len slot blk call don fp buf extra + (fun σ' => ∃ σ1, setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ((), σ') ∈ (fl4DoReplyTransfer (Z := Unit) σ.curThread (specPtr r) grant σ1).1) + | replyFault + {r slot lbl len buf d : U64} {grant blk call don fp : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} {xr : Reply} + {f : Fl4SpecA64.Fault} + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hag : fl4MinBudget = MIN_BUDGET) (hmb : ∀ a, Fl4MinBudgetAgreeA ar a) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Reply r grant) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hr : r ∈ Rp) (hk : σ.kheap (specPtr r) = some (.reply xr)) (hcl : xr.replyTcb = some (specPtr d)) + (hrcv : ∀ y, σ.kheap (specPtr d) = some (.tcb y) → + y.tcbState = .blockedOnReply (specPtr r) ∧ y.tcbFault = some f) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) (hconv : Fl4TicksToUsLegacyAgree) + (hok : ∀ x, σ.kheap σ.curThread = some (.tcb x) → + FaultReplyOKA (L := L) s d + (unat (dataToMessageInfo (x.tcbArch.tcbContext.userRegs msg_info_register)).miLength) f) : + IpcScopeA Z OS N E Rp ar s σ m lbl len slot blk call don fp buf extra + (fun σ' => ∃ σ1, setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ((), σ') ∈ (fl4DoReplyTransfer (Z := Unit) σ.curThread (specPtr r) grant σ1).1) + +variable {st} + +/-- **The IPC group is sound** (module docstring). -/ +theorem IpcScopeA.sound {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {ar : machine.Arch A} {s : S} {a : A} {σ : State Unit} {m : McState} + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + {lbl len slot : U64} {blk call don fp : Bool} {buf : U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} + {Out : State Unit → Prop} + (h : IpcScopeA st Z OS N E Rp ar s σ m lbl len slot blk call don fp buf extra Out) : + DispPostA st AR ar hPl.ipa n Z.Cs s a m lbl len slot blk call don fp buf extra Out := by + cases h with + | sendNoblock hslot hcov hcap hext hp hk hc => + exact J.sendNoblockDispA hslot hcov hcap hext hp hk hc + | sendBlock hCs h1 hmb hmax htu hslot hcov hcap hext hSC hp hk hc => + exact J.sendBlockDispA hA hM hPl hfm hCs h1 hmb hmax htu hslot hcov hcap hext hSC hp hk hc + | sendRecv hCs h1 hmb hmax htu hslot hcov hcap hext hSC hp hk hsnd hbr0 => + exact J.sendRecvDispA hA hM hPl hfm hCs h1 hmb hmax htu hslot hcov hcap hext hSC hp hk hsnd hbr0 + | sendDonate hCs h1 hmb hmax htu hslot hcov hcap hext hSC hp hk hsnd hg hrcv hV => + exact J.sendDonateDispA hA hM hPl hfm hCs h1 hmb hmax htu hslot hcov hcap hext hSC hp hk hsnd hg hrcv hV + | sendNoDon hCs h1 hmb hmax htu hslot hcov hcap hext hSC hp hk hsnd hg hrcv => + exact J.sendNoDonDispA hA hM hPl hfm hCs h1 hmb hmax htu hslot hcov hcap hext hSC hp hk hsnd hg hrcv + | replyDetached hslot hcov hcap hext hr hk hdet => + exact J.replyDetachedDispA hslot hcov hcap hext hr hk hdet + | replyLive hCs h1 hag hmb hslot hcov hcap hext hSC hr hk hcl hsnd hrcv hmax htu hconv => + exact J.replyLiveDispA hA hM hPl hfm hCs h1 hag hmb hslot hcov hcap hext hSC hr hk hcl hsnd hrcv hmax htu hconv + | replyFault hCs h1 hag hmb hslot hcov hcap hext hSC hr hk hcl hrcv hmax htu hconv hok => + exact J.replyFaultDispA hA hM hPl hfm hCs h1 hag hmb hslot hcov hcap hext hSC hr hk hcl hrcv hmax htu hconv hok + | signalQuiet hc hmb hmax htu hslot hcov hcap hext hSC hp hk hq => + obtain ⟨xc, σ1, σ2, s2, hkc, e1, hsp, hspec, hdec, ⟨B, hcur⟩, Q, Rl, J2⟩ := + J.signalQuietA hA hM hPl hfm hc hmb hmax htu hslot hcov hcap hext hSC hp hk hq + exact ⟨_, _, _, σ2, hdec, ⟨xc, σ1, hkc, e1, hsp, hspec⟩, curW_keptA J.toJStage3A J2 hcur, A64RelS3.ofJ J2 B, (by intro e; cases e)⟩ + | signalWaiting hCs h1 hmb hmax htu hslot hcov hcap hext hSC hp hk hw => + have hn : fl4A64Platform.numNodes ≤ n := + h1.symm ▸ Nat.succ_le_of_lt (Nat.lt_of_le_of_lt (Nat.zero_le _) J.sinv.mc.curCore_lt) + obtain ⟨xc, σ1, σ2, s2, hkc, e1, hsp, hspec, hdec, ⟨hI, hD, hid, hQ⟩, Q, Rl, J2⟩ := + J.signalWaitingA hA hM hPl hfm hCs h1 hn hmb hmax htu hslot hcov hcap hext hSC hp hk hw + subst e1 + have B := Sched.BodyEndA.ofSignalA J hkc hspec hI hD hid hQ J2.sr.inv J2.sinv.zc + exact ⟨_, _, _, σ2, hdec, ⟨xc, _, hkc, rfl, hsp, hspec⟩, curW_keptA J.toJStage3A J2 B.2, A64RelS3.ofJ J2 B.1, (by intro e; cases e)⟩ + | signalBoundRecv hCs h1 hmb hmax htu hslot hcov hcap hext hSC hp hk hidle hb hkt hs => + have hn : fl4A64Platform.numNodes ≤ n := + h1.symm ▸ Nat.succ_le_of_lt (Nat.lt_of_le_of_lt (Nat.zero_le _) J.sinv.mc.curCore_lt) + obtain ⟨xc, σ1, σ2, s2, hkc, e1, hsp, hspec, hdec, ⟨hI, hD, hid, hQ⟩, Q, Rl, J2⟩ := + J.signalBoundRecvA hA hM hPl hfm hCs h1 hn hmb hmax htu hslot hcov hcap hext hSC hp hk hidle hb hkt hs + subst e1 + have B := Sched.BodyEndA.ofSignalA J hkc hspec hI hD hid hQ J2.sr.inv J2.sinv.zc + exact ⟨_, _, _, σ2, hdec, ⟨xc, _, hkc, rfl, hsp, hspec⟩, curW_keptA J.toJStage3A J2 B.2, A64RelS3.ofJ J2 B.1, (by intro e; cases e)⟩ + | cancelBadged hCs h1 hmb hmax htu hslot hcov hcap hext hSC hlbl hl2 hi hd hres hdc hdOS => + obtain ⟨xc, σ1, σ2, s2, hkc, e1, hsp, hspec, hdec, ⟨B, hcur⟩, Q, Rl, J2⟩ := + J.cancelBadgedA hA hM hPl hfm hCs h1 hmb hmax htu hslot hcov hcap hext hSC hlbl hl2 hi hd hres hdc hdOS + exact ⟨_, _, _, σ2, hdec, ⟨xc, σ1, hkc, e1, hsp, hspec⟩, curW_keptA J.toJStage3A J2 hcur, A64RelS3.ofJ J2 B, (by intro e; cases e)⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3DispIrqA.lean b/proofs/Fl4ProofsA64/P3/Stage3DispIrqA.lean new file mode 100644 index 000000000..e3110f42b --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3DispIrqA.lean @@ -0,0 +1,291 @@ +/- +P3 (aarch64, A64-P3 Lift 1a/1b, T3 dispatch): **the IRQ dispatch group** (`Stage3DispBaseA`): `IRQAckIRQ` on an IRQ +handler cap (`P3/Stage3LiftMachA`'s `irqAckA`), `IRQClearIRQHandler` (`irqClearA`), `IRQSetIRQHandler` (`irqSetA`) +and the four `IRQControl` issue labels (plain, trigger, trigger-core, MSI; `Stage3LiftIrqIssueA`). The spec slots are +pinned by `AbsSlotA` and their FL4 address; the CNode, destination, null and IRQ-node facts come from the record and the +decode run (`Stage3LiftIrqDecA`). + +Named premises: [P] `VcpuArchKeepsA` (`mask_irq` keeps `AR`), `IrqCfgKeepA` (`configure_irq`), `MsiKeepA` (`msi_map`); +those of `irqClearA` / `irqSetA`; [J] `hvs`, `hvc`, `DelSiteA` (issue arms too). +-/ +import Fl4ProofsA64.P3.Stage3LiftMachA +import Fl4ProofsA64.P3.Stage3DispBaseA +import Fl4ProofsA64.P3.IrqInvokePerformA +import Fl4ProofsA64.P3.IrqInvokeDispatchA +import Fl4ProofsA64.P3.Stage3LiftIrqClearA +import Fl4ProofsA64.P3.Stage3LiftIrqSetA +import Fl4ProofsA64.P3.Stage3LiftIrqDecA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Vcpu Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +section +variable {S : Type} (st : machine.Store S) [L : LawfulStore st] {A : Type} + +/-- **The IRQ arms in scope** (module docstring). -/ +inductive IrqScopeA (AR : A → HwRootA → Prop) (Z : XSetsA) (H : Heap) (OS U Ac Pp Tb : Set U64) (Lv : U64 → Nat) + (ia ie : Nat) (N E Rp : Set U64) (Bf : Set (U64 × Fl4SpecA64.Word)) (stack : U64 → List U64) (κ : U64 → U64) + (n : Nat) (ar : machine.Arch A) (s : S) (a : A) (σ : Fl4SpecA64.State Unit) : + U64 → U64 → U64 → Bool → Bool → Bool → Bool → U64 → Aeneas.Std.Array obj.Slot 3#usize → + (Fl4SpecA64.State Unit → Prop) → Prop + | ack + (hv : VcpuArchKeepsA ar AR) (slot irq : U64) (hsl : slot ∈ OS) (hcov : SlotCov st OS s slot) + (hcap : capA st s slot = .IrqHandler irq) (len buf : U64) (blk call don fp : Bool) + (extra : Aeneas.Std.Array obj.Slot 3#usize) (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) : + IrqScopeA AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ fl4_abi.label.IRQAckIRQ len slot blk call don + fp buf extra + (fun σ' => ∃ σ1, + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ((), σ') ∈ (Fl4SpecA64.fl4InvokeIrqHandler (Z := Unit) (.aCKIrq (BitVec.ofNat 13 irq.val)) σ1).1) + | clear + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (slot irq : U64) (hsl : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .IrqHandler irq) + (len buf : U64) (blk call don fp : Bool) (extra : Aeneas.Std.Array obj.Slot 3#usize) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hvs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → Aligned b ∧ b ≠ stA (L.curW s)) + (hvc : ∀ v ∈ Z.V, stA (L.curW s) ≠ v ∧ stA (L.curW s) ≠ core.num.U64.wrapping_add v 24#u64) + (site : DelSiteA st Z.th OS s σ Z.G.chains) : + IrqScopeA AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ fl4_abi.label.IRQClearIRQHandler len slot blk + call don fp buf extra + (fun σ' => ∃ σ1, + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ((), σ') ∈ (Fl4SpecA64.fl4InvokeIrqHandler (Z := Unit) (.clearIRQHandler (BitVec.ofNat 13 irq.val)) σ1).1) + | set + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (slot irq : U64) (hsl : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .IrqHandler irq) + (len buf : U64) (blk call don fp : Bool) (extra : Aeneas.Std.Array obj.Slot 3#usize) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hvs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → Aligned b ∧ b ≠ stA (L.curW s)) + (hvc : ∀ v ∈ Z.V, stA (L.curW s) ≠ v ∧ stA (L.curW s) ≠ core.num.U64.wrapping_add v 24#u64) + (site : DelSiteA st Z.th OS s σ Z.G.chains) + (x0 pn bg : U64) (recv : Bool) (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) (hx : x0 ≠ 0#u64) + (hnc : capA st s x0 = .Notification pn bg true recv) (ss : Fl4SpecA64.CslotPtr) + (hAs : AbsSlotA Z.th ss) (hss : cslotAddrA ss = x0) : + IrqScopeA AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ fl4_abi.label.IRQSetIRQHandler len slot blk + call don fp buf extra + (fun σ' => ∃ σ1, + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ((), σ') ∈ (Fl4SpecA64.fl4InvokeIrqHandler (Z := Unit) (.setIRQHandler (BitVec.ofNat 13 irq.val) + (absCapA (.Notification pn bg true recv)) ss) σ1).1) + + | issuePlain + (hv : VcpuArchKeepsA ar AR) + (hvs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → Aligned b ∧ b ≠ stA (L.curW s)) + (hvc : ∀ v ∈ Z.V, stA (L.curW s) ≠ v ∧ stA (L.curW s) ≠ core.num.U64.wrapping_add v 24#u64) + (site : DelSiteA st Z.th OS s σ Z.G.chains) + (slot : U64) (hsl : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .IrqControl) + (len buf : U64) (blk call don fp : Bool) (extra : Aeneas.Std.Array obj.Slot 3#usize) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (root : capability.Cap) (id index depth max irq dest : U64) + (he : interrupt.extra0 st s extra = ok (some root)) (hlen : len ≥ 3#u64) + (h0 : msg.syscall_arg st s (L.curW s) buf 0#u64 = ok id) + (h1 : msg.syscall_arg st s (L.curW s) buf 1#u64 = ok index) + (h2 : msg.syscall_arg st s (L.curW s) buf 2#u64 = ok depth) + (hm : ar.max_spi a = ok max) (hi : ¬ id > max) (hnum : layout.irq.of id 0#u64 = ok irq) + (hactive : interrupt.is_irq_active st s irq = ok false) + (hslot : cspace.lookup_target_slot st s root index depth = ok (.Ok dest)) + (hempty : cdt.ensure_empty_slot st s dest = ok (.Ok ())) + (ss sd : Fl4SpecA64.CslotPtr) (hAs : AbsSlotA Z.th ss) (hss : cslotAddrA ss = slot) + (hAd : AbsSlotA Z.th sd) (hsd : cslotAddrA sd = dest) : + IrqScopeA AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ fl4_abi.label.IRQIssueIRQHandler len slot blk + call don fp buf extra + (fun σ' => ∃ σ1, + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + (.ok (), σ') ∈ ((invokeFl4IrqControl (Z := Unit) + (.spec (.irqControl (BitVec.ofNat 13 irq.val) sd ss))).run σ1).1) + | issueTrigger + (hv : VcpuArchKeepsA ar AR) (hK : IrqCfgKeepA ar AR) + (hvs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → Aligned b ∧ b ≠ stA (L.curW s)) + (hvc : ∀ v ∈ Z.V, stA (L.curW s) ≠ v ∧ stA (L.curW s) ≠ core.num.U64.wrapping_add v 24#u64) + (site : DelSiteA st Z.th OS s σ Z.G.chains) + (slot : U64) (hsl : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .IrqControl) + (len buf : U64) (blk call don fp : Bool) (extra : Aeneas.Std.Array obj.Slot 3#usize) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (root : capability.Cap) (id trigger index depth max irq dest : U64) + (he : interrupt.extra0 st s extra = ok (some root)) (hlen : len ≥ 4#u64) + (h0 : msg.syscall_arg st s (L.curW s) buf 0#u64 = ok id) + (h1 : msg.syscall_arg st s (L.curW s) buf 1#u64 = ok trigger) + (h2 : msg.syscall_arg st s (L.curW s) buf 2#u64 = ok index) + (h3 : msg.syscall_arg st s (L.curW s) buf 3#u64 = ok depth) + (hm : ar.max_spi a = ok max) (hi : ¬ id > max) (hspi : ¬ id < layout.irq.SPI_MIN) + (hnum : layout.irq.of id 0#u64 = ok irq) + (hactive : interrupt.is_irq_active st s irq = ok false) + (hslot : cspace.lookup_target_slot st s root index depth = ok (.Ok dest)) + (hempty : cdt.ensure_empty_slot st s dest = ok (.Ok ())) + (ss sd : Fl4SpecA64.CslotPtr) (hAs : AbsSlotA Z.th ss) (hss : cslotAddrA ss = slot) + (hAd : AbsSlotA Z.th sd) (hsd : cslotAddrA sd = dest) : + IrqScopeA AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ fl4_abi.label.ARMIRQIssueIRQHandlerTrigger len + slot blk call don fp buf extra + (fun σ' => ∃ σ1, + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + (.ok (), σ') ∈ ((invokeFl4IrqControl (Z := Unit) + (.triggerCore (BitVec.ofNat 13 irq.val) sd ss (trigger != 0#u64) (0#u64 : U64).bv)).run σ1).1) + | issueTriggerCore + (hv : VcpuArchKeepsA ar AR) (hK : IrqCfgKeepA ar AR) + (hvs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → Aligned b ∧ b ≠ stA (L.curW s)) + (hvc : ∀ v ∈ Z.V, stA (L.curW s) ≠ v ∧ stA (L.curW s) ≠ core.num.U64.wrapping_add v 24#u64) + (site : DelSiteA st Z.th OS s σ Z.G.chains) + (slot : U64) (hsl : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .IrqControl) + (len buf : U64) (blk call don fp : Bool) (extra : Aeneas.Std.Array obj.Slot 3#usize) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (root : capability.Cap) (id trigger index depth cpu max nodes irq dest : U64) + (he : interrupt.extra0 st s extra = ok (some root)) (hlen : len ≥ 5#u64) + (h0 : msg.syscall_arg st s (L.curW s) buf 0#u64 = ok id) + (h1 : msg.syscall_arg st s (L.curW s) buf 1#u64 = ok trigger) + (h2 : msg.syscall_arg st s (L.curW s) buf 2#u64 = ok index) + (h3 : msg.syscall_arg st s (L.curW s) buf 3#u64 = ok depth) + (h4 : msg.syscall_arg st s (L.curW s) buf 4#u64 = ok cpu) + (hm : ar.max_spi a = ok max) (hi : ¬ id > max) + (hnodes : obj.ks_get st s layout.ks.NUM_NODES = ok nodes) (hcpu : ¬ cpu ≥ nodes) + (hnum : layout.irq.of id cpu = ok irq) + (hactive : interrupt.is_irq_active st s irq = ok false) + (hslot : cspace.lookup_target_slot st s root index (depth &&& 255#u64) = ok (.Ok dest)) + (hempty : cdt.ensure_empty_slot st s dest = ok (.Ok ())) + (ss sd : Fl4SpecA64.CslotPtr) (hAs : AbsSlotA Z.th ss) (hss : cslotAddrA ss = slot) + (hAd : AbsSlotA Z.th sd) (hsd : cslotAddrA sd = dest) : + IrqScopeA AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ fl4_abi.label.ARMIRQIssueIRQHandlerTriggerCore + len slot blk call don fp buf extra + (fun σ' => ∃ σ1, + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + (.ok (), σ') ∈ ((invokeFl4IrqControl (Z := Unit) + (.triggerCore (BitVec.ofNat 13 irq.val) sd ss (trigger != 0#u64) cpu.bv)).run σ1).1) + | issueMsi + (hv : VcpuArchKeepsA ar AR) (hK : MsiKeepA ar AR) + (hvs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → Aligned b ∧ b ≠ stA (L.curW s)) + (hvc : ∀ v ∈ Z.V, stA (L.curW s) ≠ v ∧ stA (L.curW s) ≠ core.num.U64.wrapping_add v 24#u64) + (site : DelSiteA st Z.th OS s σ Z.G.chains) + (slot : U64) (hsl : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .IrqControl) + (len buf : U64) (blk call don fp : Bool) (extra : Aeneas.Std.Array obj.Slot 3#usize) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (root : capability.Cap) (nid device event index d cpu nodes base irq dest : U64) (a1 : A) + (he : interrupt.extra0 st s extra = ok (some root)) (hlen : len ≥ 6#u64) + (h0 : msg.syscall_arg st s (L.curW s) buf 0#u64 = ok nid) + (h1 : msg.syscall_arg st s (L.curW s) buf 1#u64 = ok device) + (h2 : msg.syscall_arg st s (L.curW s) buf 2#u64 = ok event) + (h3 : msg.syscall_arg st s (L.curW s) buf 3#u64 = ok index) + (h4 : msg.syscall_arg st s (L.curW s) buf 4#u64 = ok d) + (h5 : msg.syscall_arg st s (L.curW s) buf 5#u64 = ok cpu) + (hn : ¬ nid >= layout.irq.NUM_LPIS) + (hnodes : obj.ks_get st s layout.ks.NUM_NODES = ok nodes) (hcpu : ¬ cpu >= nodes) + (hbase : layout.irq.LPI_BASE = ok base) (hirq : base + nid = ok irq) + (hactive : interrupt.is_irq_active st s irq = ok false) + (hslot : cspace.lookup_target_slot st s root index (d &&& 255#u64) = ok (.Ok dest)) + (hempty : cdt.ensure_empty_slot st s dest = ok (.Ok ())) + (hmsi : ar.msi_map a irq device event cpu = ok (0#u64, a1)) + (ss sd : Fl4SpecA64.CslotPtr) (hAs : AbsSlotA Z.th ss) (hss : cslotAddrA ss = slot) + (hAd : AbsSlotA Z.th sd) (hsd : cslotAddrA sd = dest) : + IrqScopeA AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ fl4_abi.label.ARMIRQIssueIRQHandlerMSI len slot + blk call don fp buf extra + (fun σ' => ∃ σ1, + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + (.ok (), σ') ∈ ((invokeFl4IrqControl (Z := Unit) + (.msi (BitVec.ofNat 13 irq.val) sd ss device.bv event.bv cpu.bv)).run σ1).1) + +variable {st} + +/-- FL4's Ack (`Ext/IrqSignal`, the unmask on every GIC version) has the ported Ack's outcomes. -/ +theorem fl4InvokeIrqHandler_ackA (irq : Fl4SpecA64.Irq) : + Fl4SpecA64.fl4InvokeIrqHandler (Z := Unit) (.aCKIrq irq) = Fl4SpecA64.invokeIrqHandler (.aCKIrq irq) := + (archInvokeIrqHandler_ackA irq).symm + +/-- The slot an in-bounds index of the extra array reads is one of its entries. -/ +theorem extra_memA {extra : Aeneas.Std.Array obj.Slot 3#usize} {x : U64} + (h : Aeneas.Std.Array.index_usize extra 0#usize = ok x) : x ∈ extra.val := by + have hlen : extra.val.length = 3 := by simp + obtain ⟨v, hv, e⟩ := spec_existsA (Aeneas.Std.Array.index_usize_spec extra 0#usize (by simp [hlen])) + have ev : v = x := okinj (hv.symm.trans h) + rw [← ev, e] + exact List.getElem_mem _ + +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {ar : machine.Arch A} {nA : U64} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- **The IRQ group is sound.** -/ +theorem IrqScopeA.sound [PlainStore st] (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (hCnt : Sched.CounterStepA σ) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + {lbl len slot : U64} {blk call don fp : Bool} {buf : U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} + {Out : Fl4SpecA64.State Unit → Prop} + (h : IrqScopeA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ lbl len slot blk call don fp buf extra + Out) : + DispPostA st AR ar hPl.ipa n Z.Cs s a m lbl len slot blk call don fp buf extra Out := by + cases h with + | ack hv _ irq hsl hcov hcap _ _ _ _ _ _ _ hext => + obtain ⟨⟨r, s', a'⟩, e, hrO, σ1, σ2, h1, h2, ⟨xa, hxa, BK⟩, J2⟩ := spec_existsA (J.irqAckA hv hsl hcov irq extra hext) + refine ⟨r, s', a', σ2, ?_, ⟨σ1, h1, (congrArg (fun f => ((), σ2) ∈ (f σ1).1) (fl4InvokeIrqHandler_ackA (BitVec.ofNat 13 irq.val))).mpr h2⟩, Sched.curW_keptA J.toJStage3A J2 BK.cur, A64RelS3.ofJ J2 (Sched.BodyOKA.ofKeep J hxa BK), fun h => absurd (hrO.symm.trans h) (by intro e; cases e)⟩ + rw [hcap, decodeIrqHandler_programA] + exact e + | clear hCs h1 hmb hmax htu _ irq hsl hcov hcap _ _ _ _ _ _ _ hext hSC hvs hvc site => + obtain ⟨s2, a2, σ1, σ2, hdec, hset, hstep, hcur, hR⟩ := J.irqClearA (hCnt := hCnt) hA hM hPl hfm hCs h1 hmb hmax htu hsl hcov hcap hext hSC + hvs hvc site + exact ⟨.Ok (), s2, a2, σ2, hdec, ⟨σ1, hset, hstep⟩, hR.curW_eq J.toJStage3A hcur, hR, (by intro e; cases e)⟩ + | set hCs h1 hmb hmax htu _ irq hsl hcov hcap _ _ _ _ _ _ _ hext hSC hvs hvc site x0 pn bg recv hx0 hx hnc ss hAs + hss => + obtain ⟨ps, hCsS, hpn⟩ := J.toJStage3A.cnAbs_ofA (hSC x0 (extra_memA hx0) hx) hAs hss + obtain ⟨s3, a3, σ1, σ3, hdec, hset, hstep, hcur, hR⟩ := J.irqSetA (hCnt := hCnt) hA hM hPl hfm hCs h1 hmb hmax htu hsl hcov hcap hext hSC + hvs hvc site hx0 hx hnc ss hCsS hss hpn + exact ⟨.Ok (), s3, a3, σ3, hdec, ⟨σ1, hset, hstep⟩, hR.curW_eq J.toJStage3A hcur, hR, (by intro e; cases e)⟩ + | issuePlain hv hvs hvc site _ hsl hcov hcap _ _ _ _ _ _ _ hext hSC root id index depth max irq dest he hlen h0 h1 + h2 hm hi hnum hactive hslot hempty ss sd hAs hss hAd hsd => + obtain ⟨ps, hCs, -⟩ := J.toJStage3A.cnAbs_ofA hcov hAs hss + obtain ⟨sd', pd, d1, d2, d3, hdn, hpdn⟩ := J.toJStage3A.irqDest_ofA hext he hslot hempty + have e : sd = sd' := cslotAddrA_inj J.j.1.place hAd ⟨pd, d1, Or.inl d2⟩ (hsd.trans d3.symm) + subst e + obtain ⟨s3, a3, σ1, σ3, hd, hset, hstep, hcur, hR⟩ := J.irqIssuePlainA (hCnt := hCnt) hA hM hPl hfm (irq := irq) + (fun a hw h => hv.mask_irq a hw irq false h) hsl hcov hcap hext hSC hvs hvc site (irqOf_ltA hM hm hi hnum) + (irqInactive_ofRunA hactive) ss sd hCs ⟨d1, d2⟩ hss hsd hdn hpdn + (irqIssuePlain_acceptA he hlen h0 h1 h2 hm hi hnum hactive hslot hempty) + exact ⟨.Ok (), s3, a3, σ3, hd, ⟨σ1, hset, hstep⟩, hR.curW_eq J.toJStage3A hcur, hR, (by intro e; cases e)⟩ + | issueTrigger hv hK hvs hvc site _ hsl hcov hcap _ _ _ _ _ _ _ hext hSC root id trigger index depth max irq dest he + hlen h0 h1 h2 h3 hm hi hspi hnum hactive hslot hempty ss sd hAs hss hAd hsd => + obtain ⟨ps, hCs, -⟩ := J.toJStage3A.cnAbs_ofA hcov hAs hss + obtain ⟨sd', pd, d1, d2, d3, hdn, hpdn⟩ := J.toJStage3A.irqDest_ofA hext he hslot hempty + have e : sd = sd' := cslotAddrA_inj J.j.1.place hAd ⟨pd, d1, Or.inl d2⟩ (hsd.trans d3.symm) + subst e + obtain ⟨a1, hcfg, -⟩ := spec_existsA (hA.configure_irq a irq (trigger != 0#u64) 0#u64) + obtain ⟨s3, a3, σ1, σ3, hd, hset, hstep, hcur, hR⟩ := J.irqIssueRoutedA (hCnt := hCnt) hA hM hPl hfm (irq := irq) + (fun a hw h => hv.mask_irq a hw irq false h) hsl hcov hcap hext hSC hvs hvc site (irqOf_ltA hM hm hi hnum) + (irqInactive_ofRunA hactive) ss sd hCs ⟨d1, d2⟩ hss hsd hdn hpdn hK hcfg + (irqTrigger_acceptA he hlen h0 h1 h2 h3 hm hi hspi hnum hactive hslot hempty) + exact ⟨.Ok (), s3, a3, σ3, hd, ⟨σ1, hset, hstep⟩, hR.curW_eq J.toJStage3A hcur, hR, (by intro e; cases e)⟩ + | issueTriggerCore hv hK hvs hvc site _ hsl hcov hcap _ _ _ _ _ _ _ hext hSC root id trigger index depth cpu max nodes + irq dest he hlen h0 h1 h2 h3 h4 hm hi hnodes hcpu hnum hactive hslot hempty ss sd hAs hss hAd hsd => + obtain ⟨ps, hCs, -⟩ := J.toJStage3A.cnAbs_ofA hcov hAs hss + obtain ⟨sd', pd, d1, d2, d3, hdn, hpdn⟩ := J.toJStage3A.irqDest_ofA hext he hslot hempty + have e : sd = sd' := cslotAddrA_inj J.j.1.place hAd ⟨pd, d1, Or.inl d2⟩ (hsd.trans d3.symm) + subst e + obtain ⟨a1, hcfg, -⟩ := spec_existsA (hA.configure_irq a irq (trigger != 0#u64) cpu) + obtain ⟨s3, a3, σ1, σ3, hd, hset, hstep, hcur, hR⟩ := J.irqIssueRoutedA (hCnt := hCnt) hA hM hPl hfm (irq := irq) + (fun a hw h => hv.mask_irq a hw irq false h) hsl hcov hcap hext hSC hvs hvc site (irqOf_ltA hM hm hi hnum) + (irqInactive_ofRunA hactive) ss sd hCs ⟨d1, d2⟩ hss hsd hdn hpdn hK hcfg + (irqTriggerCore_acceptA he hlen h0 h1 h2 h3 h4 hm hi hnodes hcpu hnum hactive hslot hempty) + exact ⟨.Ok (), s3, a3, σ3, hd, ⟨σ1, hset, hstep⟩, hR.curW_eq J.toJStage3A hcur, hR, (by intro e; cases e)⟩ + | issueMsi hv hK hvs hvc site _ hsl hcov hcap _ _ _ _ _ _ _ hext hSC root nid device event index d cpu nodes base irq + dest a1 he hlen h0 h1 h2 h3 h4 h5 hn hnodes hcpu hbase hirq hactive hslot hempty hmsi ss sd hAs hss hAd hsd => + obtain ⟨ps, hCs, -⟩ := J.toJStage3A.cnAbs_ofA hcov hAs hss + obtain ⟨sd', pd, d1, d2, d3, hdn, hpdn⟩ := J.toJStage3A.irqDest_ofA hext he hslot hempty + have e : sd = sd' := cslotAddrA_inj J.j.1.place hAd ⟨pd, d1, Or.inl d2⟩ (hsd.trans d3.symm) + subst e + obtain ⟨s3, a3, σ1, σ3, hd, hset, hstep, hcur, hR⟩ := J.irqIssueMsiA (hCnt := hCnt) hA hM hPl hfm (irq := irq) + (fun a hw h => hv.mask_irq a hw irq false h) hsl hcov hcap hext hSC hvs hvc site (irqOfLpi_ltA hn hbase hirq) + (irqInactive_ofRunA hactive) ss sd hCs ⟨d1, d2⟩ hss hsd hdn hpdn hK hmsi + (irqMsi_acceptA he hlen h0 h1 h2 h3 h4 h5 hn hnodes hcpu hbase hirq hactive hslot hempty hmsi) + exact ⟨.Ok (), s3, a3, σ3, hd, ⟨σ1, hset, hstep⟩, hR.curW_eq J.toJStage3A hcur, hR, (by intro e; cases e)⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3DispReplyA.lean b/proofs/Fl4ProofsA64/P3/Stage3DispReplyA.lean new file mode 100644 index 000000000..348e0a44d --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3DispReplyA.lean @@ -0,0 +1,109 @@ +/- +P3 (aarch64, A64-P3 Lift 1a, T3 dispatch): **the one-word-reply dispatch group** (`Stage3DispBaseA`): +`ARMPageGetAddress` (`P3/Stage3LiftGetAddrA`) and `ARMPageTestAndClearAccessed` (`P3/Stage3LiftTestClearA`). + +Named premises: those of `getAddrA` and `testClearA` ([P] `ArchKeepsA` for the latter). + +Pinning: `testClear`'s message words and extra caps are the front end's (`FrontPinA`); the spec decode's `CapRef` +argument is `[]`. +-/ +import Fl4ProofsA64.P3.Stage3LiftGetAddrA +import Fl4ProofsA64.P3.Stage3LiftTestClearA +import Fl4ProofsA64.P3.Stage3DispBaseA +import Fl4ProofsA64.P3.Stage3LiftBodyA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 + +noncomputable section + +section +variable {S : Type} (st : machine.Store S) [L : LawfulStore st] {A : Type} + +/-- The spec outcome of a one-word reply invocation `inv`. -/ +def ReplySpecA (σ : Fl4SpecA64.State Unit) (call : Bool) (inv : Fl4ArchInvocation) (σ' : Fl4SpecA64.State Unit) : + Prop := + ∃ (x : Fl4SpecA64.Tcb) (σ2 : Fl4SpecA64.State Unit) (w : Fl4SpecA64.Word), + σ.kheap σ.curThread = some (.tcb x) ∧ + (.ok [w], σ2) ∈ ((fl4ArchPerformInvocation (Z := Unit) inv).run (stUpdA σ σ.curThread x .restart)).1 ∧ + ((), σ') ∈ ((do + Fl4Spec.NonDet.when call (Fl4SpecA64.replyFromKernel (Z := Unit) σ.curThread (0, [w])) + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .running) σ2).1 + +/-- The spec outcome of `ARMPageTestAndClearAccessed` with decoder result `D`: rejected, or the one-word reply. -/ +def TestClearOutA (σ : Fl4SpecA64.State Unit) (call : Bool) (p : Fl4SpecA64.ObjRef) + (D : Set (Except Fl4SpecA64.SyscallError Fl4ArchInvocation × Fl4SpecA64.State Unit) × Prop) + (σ' : Fl4SpecA64.State Unit) : Prop := + (σ' = σ ∧ ∃ e', D = ({(.error e', σ)}, False)) ∨ + (D = ({(.ok (.testAndClearAccessed p), σ)}, False) ∧ ReplySpecA σ call (.testAndClearAccessed p) σ') + +/-- **The one-word reply arms in scope** (module docstring). -/ +inductive ReplyScopeA (AR : A → HwRootA → Prop) (Z : XSetsA) (OS : Set U64) (ar : machine.Arch A) (s : S) + (σ : Fl4SpecA64.State Unit) : + U64 → U64 → U64 → Bool → Bool → Bool → Bool → U64 → Aeneas.Std.Array obj.Slot 3#usize → + (Fl4SpecA64.State Unit → Prop) → Prop + | getAddress + (cte : Fl4SpecA64.CslotPtr) (hab : AbsSlotA Z.th cte) (hcov : SlotCov st OS s (cslotAddrA cte)) + (fpa rt asid va : U64) (sz : capability.FrameSize) (dev mp e : Bool) + (hcap : capA st s (cslotAddrA cte) = .Frame fpa sz rt dev mp e asid va) + (len buf : U64) (blk call don fp : Bool) (extra : Aeneas.Std.Array obj.Slot 3#usize) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) : + ReplyScopeA AR Z OS ar s σ fl4_abi.label.ARMPageGetAddress len (cslotAddrA cte) blk call don fp buf extra + (ReplySpecA σ call (.spec (.invokePage (.pageGetAddr (specPtr fpa))))) + | testClear + (K : ArchKeepsA ar AR) (cte : Fl4SpecA64.CslotPtr) (hab : AbsSlotA Z.th cte) + (hcov : SlotCov st OS s (cslotAddrA cte)) (fpa rt asid va : U64) (sz : capability.FrameSize) (dev mp e : Bool) + (hcap : capA st s (cslotAddrA cte) = .Frame fpa sz rt dev mp e asid va) + (len buf : U64) (blk call don fp : Bool) (extra : Aeneas.Std.Array obj.Slot 3#usize) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (args : List U64) (exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) (mi : Fl4SpecA64.MessageInfo) + (c : Fl4SpecA64.Cap) (buffer : Option Fl4SpecA64.ObjRef) + (hF : FrontPinA Z.th σ (cslotAddrA cte) mi cte c exs buffer (args.map UScalar.bv)) : + ReplyScopeA AR Z OS ar s σ fl4_abi.label.ARMPageTestAndClearAccessed len (cslotAddrA cte) blk call don fp buf + extra (TestClearOutA σ call (specPtr fpa) ((fl4ArchDecodeInvocation (Z := Unit) call + (.ext .armPageTestAndClearAccessed) (args.map UScalar.bv) [] cte + (absFrameE e (specPtr fpa) (vmRightsOfA rt) (frameSizeOfA sz) dev (mapDataA mp asid va)) exs).run σ)) + +variable {st} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {ar : machine.Arch A} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- **The one-word reply group is sound.** -/ +theorem ReplyScopeA.sound (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) + s a σ m) + (hfm : A64FrameMasks st s) + {lbl len slot : U64} {blk call don fp : Bool} {buf : U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} + {Out : Fl4SpecA64.State Unit → Prop} + (h : ReplyScopeA st AR Z OS ar s σ lbl len slot blk call don fp buf extra Out) : + DispPostA st AR ar hPl.ipa n Z.Cs s a m lbl len slot blk call don fp buf extra Out := by + cases h with + | getAddress cte hab hcov fpa rt asid va sz dev mp e hcap _ _ _ _ _ _ _ hext hSC => + obtain ⟨⟨r, s', a'⟩, eq, -, hrO, x, σ2, σ3, w, hx, -, hp, hr, ⟨uc, hsh⟩, Q, Rl, J2⟩ := spec_existsA + (J.getAddrA hA hM hPl hfm cte hab hcov fpa rt asid va sz dev mp e hcap len buf blk call don fp extra hext hSC + [] [] []) + refine ⟨r, s', a', σ3, ?_, ⟨x, σ2, w, hx, hp, hr⟩, Sched.curW_keptA J.toJStage3A J2 (by rw [hsh]; rfl), + A64RelS3.ofJ J2 (Sched.BodyOKA.ofReply J hx hsh), fun h => absurd (hrO.symm.trans h) (by intro e; cases e)⟩ + rw [hcap]; exact eq + | testClear K cte hab hcov fpa rt asid va sz dev mp e hcap len buf blk call don fp extra hext hSC args exs => + obtain ⟨⟨r, s', a'⟩, eq, h⟩ := spec_existsA + (J.testClearA hA hM hPl hfm K cte hab hcov fpa rt asid va sz dev mp e hcap len buf blk call don fp extra hext + hSC args [] exs) + rcases h with ⟨er, he⟩ | ⟨hD, hrO, x, σ2, σ3, w, hx, -, hp, hr, ⟨uc, hsh⟩, Q, Rl, J2⟩ + · simp only [Prod.mk.injEq] at er + obtain ⟨e', hDe, ex, hrx, hrel⟩ := he + refine ⟨r, s', a', σ, by rw [hcap]; exact eq, Or.inl ⟨rfl, e', hDe⟩, by rw [er.1], ?_, + fun h => (hrel.ne_preempted) (by have := hrx.symm.trans h; injection this)⟩ + rw [er.1, er.2] + exact A64RelS3.ofJ J.toJStage3A J.bodyOKA + · exact ⟨r, s', a', σ3, by rw [hcap]; exact eq, Or.inr ⟨hD, x, σ2, w, hx, hp, hr⟩, + Sched.curW_keptA J.toJStage3A J2 (by rw [hsh]; rfl), A64RelS3.ofJ J2 (Sched.BodyOKA.ofReply J hx hsh), + fun h => absurd (hrO.symm.trans h) (by intro e; cases e)⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3DispScA.lean b/proofs/Fl4ProofsA64/P3/Stage3DispScA.lean new file mode 100644 index 000000000..f952373f3 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3DispScA.lean @@ -0,0 +1,305 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 3): **the SchedContext dispatch group** (`Stage3DispBaseA`): Bind of a notification +(`JSyscallA.scBindNtfnA`), UnbindObject of the SC's notification (`JSyscallA.scUnbindNtfnA`) Consumed +(`JSyscallA.scConsumedA`), YieldTo's reply arm (`JSyscallA.scYieldReplyA`) its requeue arm +(`JSyscallA.scYieldRequeueA`), its switch arm (`JSyscallA.scYieldSwitchRecA`) and SchedControl ConfigureFlags +(`JSyscallA.scCfgA`), Bind of a thread (`JSyscallA.scBindTcbArmA`) and UnbindObject of the SC's thread +(`JSyscallA.scUnbindTcbArmA`), and Unbind (`JSyscallA.scUnbindArmA`). + +A new arm is a constructor of `ScScopeA` and a case of `ScScopeA.sound`. + +Named premises: [P] `MaxSpi`, `ArchPlat`, `A64FrameMasks`, `hCs`; Consumed's [P] `hmax`, `htu`; YieldTo's [P] `hmb`, [J] `hbound`, [S] `hyf`, +`hon`, `hns` (reply arm), `hsch`, `hlt` (requeue arm) or `hsch`, `hnlt` (switch arm); [S] `h1` (`numNodes = 1`, the remote stall); ConfigureFlags' [P] `hT`, `hAgree`, [S] `hdec` +(the spec decoder accepts; the `Err` returns are not lifted); its `hex` is a decode-site case hypothesis (front end). +Thread Bind's [P] `hmb`; the thread arms' decode-site case hypotheses: the decoder's checks and `hsl` (the target's spec +thread cap at a slot). The index `m` is the record's machine state. +New module. +-/ +import Fl4ProofsA64.P3.Stage3DispBaseA +import Fl4ProofsA64.P3.Stage3ScNtfnA +import Fl4ProofsA64.P3.Stage3ScConsumedA +import Fl4ProofsA64.P3.Stage3ScYieldSwA +import Fl4ProofsA64.P3.Stage3ScCfgRecA +import Fl4ProofsA64.P3.Stage3ScBindArmA +import Fl4ProofsA64.P3.Stage3ScUnbA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +section +variable {S : Type} (st : machine.Store S) [L : LawfulStore st] {A : Type} + +/-- **The SchedContext arms in scope** (module docstring), indexed as the other groups. -/ +inductive ScScopeA (AR : A → HwRootA → Prop) (Z : XSetsA) (OS : Set U64) (ar : machine.Arch A) (s : S) (a : A) + (σ : State Unit) (m : McState) : + U64 → U64 → U64 → Bool → Bool → Bool → Bool → U64 → Aeneas.Std.Array obj.Slot 3#usize → + (State Unit → Prop) → Prop + | bindNtfn + {slot lbl len buf sc sz p badge : U64} {blk call don send recv : Bool} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .SchedContext sc sz) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hlbl : lbl = fl4_abi.label.SchedContextBind) + (he : schedcontext.extra_cap st s extra 0#usize = ok (some (.Notification p badge send recv))) + (hn : obj.Sc.ntfn st sc s = ok 0#u64) (hn' : obj.Ntfn.sc st p s = ok 0#u64) : + ScScopeA AR Z OS ar s a σ m lbl len slot blk call don false buf extra + (fun σ' => ∃ xc σ1, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + invokeSchedContext (Z := Unit) (.invokeSchedContextBind (specPtr sc) + (.notificationCap (specPtr p) badge.bv (rightsOfA send recv false false))) σ1 = ({([], σ')}, False)) + | unbindNtfn + {slot lbl len buf sc sz p badge : U64} {blk call don send recv : Bool} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .SchedContext sc sz) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hlbl : lbl = fl4_abi.label.SchedContextUnbindObject) + (he : schedcontext.extra_cap st s extra 0#usize = ok (some (.Notification p badge send recv))) + (hn : obj.Sc.ntfn st sc s = ok p) : + ScScopeA AR Z OS ar s a σ m lbl len slot blk call don false buf extra + (fun σ' => ∃ xc σ1, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + invokeSchedContext (Z := Unit) (.invokeSchedContextUnbindObject (specPtr sc) + (.notificationCap (specPtr p) badge.bv (rightsOfA send recv false false))) σ1 = ({([], σ')}, False)) + + | consumed + {slot lbl len buf sc sz : U64} {blk call don : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .SchedContext sc sz) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hlbl : lbl = fl4_abi.label.SchedContextConsumed) : + ScScopeA AR Z OS ar s a σ m lbl len slot blk call don false buf extra + (fun σ3 => ∃ xc σ1 σ2 d, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + invokeSchedContext (Z := Unit) (.invokeSchedContextConsumed (specPtr sc)) σ1 = ({(d, σ2)}, False) ∧ + getThreadState (Z := Unit) σ.curThread σ2 = ({(.restart, σ2)}, False) ∧ + ((), σ3) ∈ ((do + Fl4Spec.NonDet.when call (Fl4SpecA64.replyFromKernel (Z := Unit) σ.curThread (0, d)) + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .running) σ2).1) + + | yieldReply + {slot lbl len buf sc sz : U64} {blk call don : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .SchedContext sc sz) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hlbl : lbl = fl4_abi.label.SchedContextYieldTo) + (ht0 : L.mem s (scTcbA sc) ≠ 0#u64) (hne : L.mem s (scTcbA sc) ≠ L.curW s) + (hprio : ¬ L.mem s (prioA (L.mem s (scTcbA sc))) > L.mem s (mcpA (L.curW s))) + (hy : L.mem s (yieldA (L.curW s)) = 0#u64) (hyf : L.mem s (scYieldFromA sc) = 0#u64) + (hon : L.mem s (affA (L.mem s (scTcbA sc))) = L.cpu s) + (hbound : L.mem s (scA (L.mem s (scTcbA sc))) = sc) + (hns : ∀ xc, σ.kheap σ.curThread = some (.tcb xc) → + schedulable (specPtr (L.mem s (scTcbA sc))) (stUpdA σ σ.curThread xc .restart) = false) : + ScScopeA AR Z OS ar s a σ m lbl len slot blk call don false buf extra + (fun σ3 => ∃ xc σ1 σ2 d, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + invokeSchedContext (Z := Unit) (.invokeSchedContextYieldTo (specPtr sc)) σ1 = ({(d, σ2)}, False) ∧ + getThreadState (Z := Unit) σ.curThread σ2 = ({(.restart, σ2)}, False) ∧ + ((), σ3) ∈ ((do + Fl4Spec.NonDet.when call (Fl4SpecA64.replyFromKernel (Z := Unit) σ.curThread (0, d)) + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .running) σ2).1) + + | yieldRequeue + {slot lbl len buf sc sz : U64} {blk call don : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .SchedContext sc sz) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hlbl : lbl = fl4_abi.label.SchedContextYieldTo) + (ht0 : L.mem s (scTcbA sc) ≠ 0#u64) (hne : L.mem s (scTcbA sc) ≠ L.curW s) + (hy : L.mem s (yieldA (L.curW s)) = 0#u64) (hyf : L.mem s (scYieldFromA sc) = 0#u64) + (hon : L.mem s (affA (L.mem s (scTcbA sc))) = L.cpu s) + (hbound : L.mem s (scA (L.mem s (scTcbA sc))) = sc) + (hprio : ¬ L.mem s (prioA (L.mem s (scTcbA sc))) > L.mem s (mcpA (L.curW s))) + (hlt : L.mem s (prioA (L.mem s (scTcbA sc))) < L.mem s (prioA (L.curW s))) + (hsch : ∀ xc, σ.kheap σ.curThread = some (.tcb xc) → + schedulable (specPtr (L.mem s (scTcbA sc))) (stUpdA σ σ.curThread xc .restart) = true) : + ScScopeA AR Z OS ar s a σ m lbl len slot blk call don false buf extra + (fun σ3 => ∃ xc σ1 σ2 d, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + invokeSchedContext (Z := Unit) (.invokeSchedContextYieldTo (specPtr sc)) σ1 = ({(d, σ2)}, False) ∧ + getThreadState (Z := Unit) σ.curThread σ2 = ({(.restart, σ2)}, False) ∧ + ((), σ3) ∈ ((do + Fl4Spec.NonDet.when call (Fl4SpecA64.replyFromKernel (Z := Unit) σ.curThread (0, d)) + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .running) σ2).1) + + | yieldSwitch + {slot lbl len buf sc sz : U64} {blk call don : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .SchedContext sc sz) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hlbl : lbl = fl4_abi.label.SchedContextYieldTo) + (ht0 : L.mem s (scTcbA sc) ≠ 0#u64) (hne : L.mem s (scTcbA sc) ≠ L.curW s) + (hy : L.mem s (yieldA (L.curW s)) = 0#u64) (hyf : L.mem s (scYieldFromA sc) = 0#u64) + (hon : L.mem s (affA (L.mem s (scTcbA sc))) = L.cpu s) + (hbound : L.mem s (scA (L.mem s (scTcbA sc))) = sc) + (hprio : ¬ L.mem s (prioA (L.mem s (scTcbA sc))) > L.mem s (mcpA (L.curW s))) + (hnlt : ¬ L.mem s (prioA (L.mem s (scTcbA sc))) < L.mem s (prioA (L.curW s))) + (hsch : ∀ xc, σ.kheap σ.curThread = some (.tcb xc) → + schedulable (specPtr (L.mem s (scTcbA sc))) (stUpdA σ σ.curThread xc .restart) = true) : + ScScopeA AR Z OS ar s a σ m lbl len slot blk call don false buf extra + (fun σ2 => ∃ xc σ1, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + invokeSchedContext (Z := Unit) (.invokeSchedContextYieldTo (specPtr sc)) σ1 = ({([], σ2)}, False)) + + | configure + {slot lbl len buf cpu : U64} {blk call don : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hT : ScCfgTimeAgreeA ar a) (hAgree : fl4MinBudget = MIN_BUDGET) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .SchedControl cpu) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hlbl : lbl = fl4_abi.label.SchedControlConfigureFlags) + {slbl : Data} (hslbl : genInvocationType slbl = .schedControlConfigureFlags) + {args : List U64} (hlen : len.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) + {x0 p sb : U64} (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) (hx0n : x0 ≠ 0#u64) + (hc0 : capA st s x0 = .SchedContext p sb) + {excaps : List Fl4SpecA64.Cap} (hex : ExtraRelA Z.th (L.mem s) x0 excaps) + {bS pS : Ticks} {mr : Nat} {bd fl : Word} + (hdec : (decodeFl4SchedControlInvocation (Z := Unit) slbl (args.map UScalar.bv) excaps).run σ = + ({(.ok (.invokeSchedControlConfigureFlags (specPtr p) bS pS mr bd fl), σ)}, False)) : + ScScopeA AR Z OS ar s a σ m lbl len slot blk call don false buf extra + (fun σ2 => ∃ xc σ1, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + invokeFl4SchedControlConfigureFlags (Z := Unit) + (.invokeSchedControlConfigureFlags (specPtr p) bS pS mr bd fl) σ1 = ({((), σ2)}, False) ∧ + (∃ y k, σ1.kheap (specPtr p) = some (.schedContext y k) ∧ + σ2.consumedTime = if y.scTcb ≠ none ∧ σ1.curSc = specPtr p then 0 else σ1.consumedTime)) + + | bindTcb + {slot lbl len buf sc sz t : U64} {blk call don b : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .SchedContext sc sz) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hlbl : lbl = fl4_abi.label.SchedContextBind) + (he : schedcontext.extra_cap st s extra 0#usize = ok (some (.Thread t))) + {sl : CslotPtr} (hsl : specCapAtA σ sl = some (.threadCap (specPtr t))) + (hn : obj.Sc.tcb st sc s = ok 0#u64) (ht0 : obj.Tcb.sc st t s = ok 0#u64) + (hbl : sched.is_blocked st s t = ok b) (hrel : b = true → sporadic.sc_released st s sc = ok true) : + ScScopeA AR Z OS ar s a σ m lbl len slot blk call don false buf extra + (fun σ' => ∃ xc σ1, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + invokeSchedContext (Z := Unit) (.invokeSchedContextBind (specPtr sc) (.threadCap (specPtr t))) σ1 = + ({([], σ')}, False)) + | unbindTcb + {slot lbl len buf sc sz t : U64} {blk call don : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .SchedContext sc sz) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hlbl : lbl = fl4_abi.label.SchedContextUnbindObject) + (he : schedcontext.extra_cap st s extra 0#usize = ok (some (.Thread t))) + (htw : obj.Sc.tcb st sc s = ok t) (hne : t ≠ L.curW s) : + ScScopeA AR Z OS ar s a σ m lbl len slot blk call don false buf extra + (fun σ' => ∃ xc σ1, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + invokeSchedContext (Z := Unit) (.invokeSchedContextUnbindObject (specPtr sc) (.threadCap (specPtr t))) σ1 = + ({([], σ')}, False)) + | unbind + {slot lbl len buf sc sz t : U64} {blk call don : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .SchedContext sc sz) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hlbl : lbl = fl4_abi.label.SchedContextUnbind) + (htw : obj.Sc.tcb st sc s = ok t) (hne : t ≠ L.curW s) : + ScScopeA AR Z OS ar s a σ m lbl len slot blk call don false buf extra + (fun σ' => ∃ xc σ1, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + invokeSchedContext (Z := Unit) (.invokeSchedContextUnbind (specPtr sc)) σ1 = ({([], σ')}, False)) + +variable {st} + +/-- **The SchedContext group is sound** with a successful decode (module docstring). -/ +theorem ScScopeA.soundOk {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} + {κ : U64 → U64} {n : Nat} {ar : machine.Arch A} {s : S} {a : A} {σ : State Unit} {m : McState} + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + {lbl len slot : U64} {blk call don fp : Bool} {buf : U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} + {Out : State Unit → Prop} + (h : ScScopeA st AR Z OS ar s a σ m lbl len slot blk call don fp buf extra Out) : + DispPostOkA st AR ar hPl.ipa n Z.Cs s a m lbl len slot blk call don fp buf extra Out := by + cases h with + | bindNtfn hCs h1 hslot hcov hcap hext hSC hlbl he hn hn' => + obtain ⟨xc, σ1, σ2, s2, hkc, e1, hsp, hspec, hdec, Q, Rl, J2⟩ := + J.scBindNtfnA hA hM hPl hfm hCs h1 hslot hcov hcap hext hSC hlbl he hn hn' + exact ⟨_, _, σ2, hdec, ⟨xc, σ1, hkc, e1, hsp, hspec⟩, Sched.curW_keptA J.toJStage3A J2.1 J2.2.2, A64RelS3.ofJ J2.1 J2.2.1⟩ + | unbindNtfn hCs h1 hslot hcov hcap hext hSC hlbl he hn => + obtain ⟨xc, σ1, σ2, s2, hkc, e1, hsp, hspec, hdec, Q, Rl, J2⟩ := + J.scUnbindNtfnA hA hM hPl hfm hCs h1 hslot hcov hcap hext hSC hlbl he hn + exact ⟨_, _, σ2, hdec, ⟨xc, σ1, hkc, e1, hsp, hspec⟩, Sched.curW_keptA J.toJStage3A J2.1 J2.2.2, A64RelS3.ofJ J2.1 J2.2.1⟩ + + | consumed hCs h1 hmax htu hslot hcov hcap hext hSC hlbl => + obtain ⟨xc, σ1, σ2, d, σ3, s3, hkc, e1, hsp, hinv, hst2, hrun, hdec, Q, Rl, J2⟩ := + J.scConsumedA hA hM hPl hfm hCs h1 hmax htu hslot hcov hcap hext hSC hlbl + exact ⟨_, _, σ3, hdec, ⟨xc, σ1, σ2, d, hkc, e1, hsp, hinv, hst2, hrun⟩, Sched.curW_keptA J.toJStage3A J2.1 J2.2.2, A64RelS3.ofJ J2.1 J2.2.1⟩ + | yieldReply hCs h1 hmb hmax htu hslot hcov hcap hext hSC hlbl ht0 hne hprio hy hyf hon hbound hns => + obtain ⟨xc, σ1, σ2, d, σ3, s3, hkc, e1, hsp, hinv, hst2, hrun, hdec, Q, Rl, J2⟩ := + J.scYieldReplyA hA hM hPl hfm hCs h1 hmb hmax htu hslot hcov hcap hext hSC hlbl ht0 hne hprio hy hyf hon hbound + hns + exact ⟨_, _, σ3, hdec, ⟨xc, σ1, σ2, d, hkc, e1, hsp, hinv, hst2, hrun⟩, Sched.curW_keptA J.toJStage3A J2.1 J2.2.2, A64RelS3.ofJ J2.1 J2.2.1⟩ + | yieldRequeue hCs h1 hmb hmax htu hslot hcov hcap hext hSC hlbl ht0 hne hy hyf hon hbound hprio hlt hsch => + obtain ⟨xc, σ1, σ2, d, σ3, s3, hkc, e1, hsp, hinv, hst2, hrun, hdec, Q, Rl, J2⟩ := + J.scYieldRequeueA hA hM hPl hfm hCs h1 hmb hmax htu hslot hcov hcap hext hSC hlbl ht0 hne hy hyf hon hbound hprio + hlt hsch + exact ⟨_, _, σ3, hdec, ⟨xc, σ1, σ2, d, hkc, e1, hsp, hinv, hst2, hrun⟩, Sched.curW_keptA J.toJStage3A J2.1 J2.2.2, A64RelS3.ofJ J2.1 J2.2.1⟩ + + | configure hCs h1 hT hAgree hslot hcov hcap hext hSC hlbl hslbl hlen hargs hx0 hx0n hc0 hex hdec => + obtain ⟨xc, σ1, σ2, s2, hkc, e1, hsp, hspec, hcons, hdec', Q, Rl, Ac', J2⟩ := + J.scCfgA hA hM hPl hfm hCs h1 hT hAgree hslot hcov hcap hext hSC hlbl hslbl hlen hargs hx0 hx0n hc0 hex hdec + exact ⟨_, _, σ2, hdec', ⟨xc, σ1, hkc, e1, hsp, hspec, hcons⟩, Sched.curW_keptA J.toJStage3A J2.1 J2.2.2, + A64RelS3.ofJ J2.1 J2.2.1⟩ + | yieldSwitch hCs h1 hmb hslot hcov hcap hext hSC hlbl ht0 hne hy hyf hon hbound hprio hnlt hsch => + obtain ⟨xc, σ1, σ2, s2, hkc, e1, hsp, hinv, hdec, Q, Rl, J2⟩ := + J.scYieldSwitchRecA hA hM hPl hfm hCs h1 hmb hslot hcov hcap hext hSC hlbl ht0 hne hy hyf hon hbound hprio hnlt + hsch + exact ⟨_, _, σ2, hdec, ⟨xc, σ1, hkc, e1, hsp, hinv⟩, Sched.curW_keptA J.toJStage3A J2.1 J2.2.2, + A64RelS3.ofJ J2.1 J2.2.1⟩ + | bindTcb hCs h1 hmb hslot hcov hcap hext hSC hlbl he hsl hn ht0 hbl hrel => + obtain ⟨xc, σ1, σ2, s2, hkc, e1, hsp, hspec, hdec, Q, Rl, Ac', J2⟩ := + J.scBindTcbArmA hA hM hPl hfm hCs h1 hmb hslot hcov hcap hext hSC hlbl he hsl hn ht0 hbl hrel + exact ⟨_, _, σ2, hdec, ⟨xc, σ1, hkc, e1, hsp, hspec⟩, Sched.curW_keptA J.toJStage3A J2.1 J2.2.2, + A64RelS3.ofJ J2.1 J2.2.1⟩ + | unbindTcb hCs h1 hslot hcov hcap hext hSC hlbl he htw hne => + obtain ⟨xc, σ1, σ2, s2, hkc, e1, hsp, hspec, hdec, Q, Rl, Ac', J2⟩ := + J.scUnbindTcbArmA hA hM hPl hfm hCs h1 hslot hcov hcap hext hSC hlbl he htw hne + exact ⟨_, _, σ2, hdec, ⟨xc, σ1, hkc, e1, hsp, hspec⟩, Sched.curW_keptA J.toJStage3A J2.1 J2.2.2, + A64RelS3.ofJ J2.1 J2.2.1⟩ + | unbind hCs h1 hslot hcov hcap hext hSC hlbl htw hne => + obtain ⟨xc, σ1, σ2, s2, hkc, e1, hsp, hspec, hdec, Q, Rl, Ac', stack', J2⟩ := + J.scUnbindArmA hA hM hPl hfm hCs h1 hslot hcov hcap hext hSC hlbl htw hne + exact ⟨_, _, σ2, hdec, ⟨xc, σ1, hkc, e1, hsp, hspec⟩, Sched.curW_keptA J.toJStage3A J2.1 J2.2.2, + A64RelS3.ofJ J2.1 J2.2.1⟩ + +/-- **The SchedContext group is sound** (module docstring). -/ +theorem ScScopeA.sound {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} + {κ : U64 → U64} {n : Nat} {ar : machine.Arch A} {s : S} {a : A} {σ : State Unit} {m : McState} + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + {lbl len slot : U64} {blk call don fp : Bool} {buf : U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} + {Out : State Unit → Prop} + (h : ScScopeA st AR Z OS ar s a σ m lbl len slot blk call don fp buf extra Out) : + DispPostA st AR ar hPl.ipa n Z.Cs s a m lbl len slot blk call don fp buf extra Out := + (ScScopeA.soundOk hA hM hPl J hfm h).post + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3DispTcbA.lean b/proofs/Fl4ProofsA64/P3/Stage3DispTcbA.lean new file mode 100644 index 000000000..b4f45f036 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3DispTcbA.lean @@ -0,0 +1,703 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 3): **the TCB dispatch group** (`Stage3DispBaseA`): UnbindNotification on a bound +thread (`JSyscallA.unbindNtfnA`), SetMCPriority (`JSyscallA.setMcpA`), SetPriority of an inactive thread (`JSyscallA.setPrioInactiveA`), SetPriority writing only the priority word (`JSyscallA.setPrioPlainA`), of an endpoint-blocked thread (`JSyscallA.setPrioEpA`), of a notification-blocked thread (`JSyscallA.setPrioNtfnA`), of a runnable queued or current thread (`JSyscallA.setPrioQueuedA`), BindNotification (`JSyscallA.bindNtfnA`), +SetTLSBase on another thread (`JSyscallA.setTlsA`), SetPACKeys (`JSyscallA.setPacKeysA`), Resume of a runnable +thread (`JSyscallA.resumeRunnableA`), Resume of a stopped thread (`JSyscallA.resumeStoppedA`), WriteFPU (`JSyscallA.writeFpuA`), ReadFPU (`JSyscallA.readFpuA`), SetFlags (`JSyscallA.setFlagsA`), Suspend with a live VCPU run (`JSyscallA.tcbSuspendA`, +ending in `BodyEndA`: the cancel and the suspension are one `DelStepA` step, `Stage3DelFinThrA`), SetSchedParams with a +null fault handler and an unchanged SC (`JSyscallA.setSchedParamsNullA`), SetSchedParams with an endpoint fault handler +and an unchanged SC (`JSyscallA.setSchedParamsEpA`). + +A new arm is a constructor of `TcbScopeA` and a case of `TcbScopeA.sound`. + +Named premises: [P] `MaxSpi`, `ArchPlat`, `A64FrameMasks`, `hCs`; [P] `hmb`, `hmax`, `htu`; [P] SetPACKeys' `hprobe`, +`hsupported`, `hepoch`, `hk` (`TcbArchKeepsA`), `hbuf`; WriteFPU's [P] `hflush`, `hsan`, `hrel`, +`hk`, [S] `howner` (known gap: the FPU owner hand-over is not lifted); ReadFPU's [P] `hflush`, `hk`, [S] `howner`, +[S] `hbuf` (known scope limit: the destination frame holds no tracked IPC-buffer word; the index `Bf` of `TcbScopeA` +is the record's buffer set); SetFlags' [J] `hfl` (the flags word; missing +record fact), [P] `hX`, `hrel`, `hk`, [S] `howner`; [S] `h1` (`numNodes = 1`, the remote +stall). +New module. +-/ +import Fl4ProofsA64.P3.Stage3DispBaseA +import Fl4ProofsA64.P3.Stage3TcbUnbindA +import Fl4ProofsA64.P3.Stage3TcbMcpA +import Fl4ProofsA64.P3.Stage3TcbPrioA +import Fl4ProofsA64.P3.Stage3TcbPrioPlainA +import Fl4ProofsA64.P3.Stage3TcbPrioEpArmA +import Fl4ProofsA64.P3.Stage3TcbPrioNtfnA +import Fl4ProofsA64.P3.Stage3TcbPrioQArmA +import Fl4ProofsA64.P3.Stage3TcbBindA +import Fl4ProofsA64.P3.Stage3TcbTlsA +import Fl4ProofsA64.P3.Stage3TcbTlsSelfA +import Fl4ProofsA64.P3.Stage3TcbPacA +import Fl4ProofsA64.P3.Stage3TcbResumeNoopA +import Fl4ProofsA64.P3.Stage3TcbResumeRqA +import Fl4ProofsA64.P3.Stage3TcbFpuA +import Fl4ProofsA64.P3.Stage3TcbFpuRdA +import Fl4ProofsA64.P3.Stage3TcbFlagsA +import Fl4ProofsA64.P3.Stage3TcbSuspendArmA +import Fl4ProofsA64.P3.Stage3LiftTcbBufA +import Fl4ProofsA64.P3.Stage3LiftTcbBufNzA +import Fl4ProofsA64.P3.Stage3LiftTcbTimeoutA +import Fl4ProofsA64.P3.Stage3LiftTcbTimeoutNnA +import Fl4ProofsA64.P3.Stage3LiftTcbSspA +import Fl4ProofsA64.P3.Stage3LiftTcbSspEpA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +section +variable {S : Type} (st : machine.Store S) [L : LawfulStore st] {A : Type} + +/-- **The TCB arms in scope** (module docstring), indexed as the other groups. -/ +inductive TcbScopeA (AR : A → HwRootA → Prop) (Z : XSetsA) (OS : Set U64) (ar : machine.Arch A) (s : S) (a : A) + (σ : State Unit) (Bf : Set (U64 × Fl4SpecA64.Word)) : + U64 → U64 → U64 → Bool → Bool → Bool → Bool → U64 → Aeneas.Std.Array obj.Slot 3#usize → + (State Unit → Prop) → Prop + | unbindNtfn + {slot lbl len buf t : U64} {blk call don : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Thread t) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hlbl : lbl = fl4_abi.label.TCBUnbindNotification) (hb : L.mem s (boundA t) ≠ 0#u64) : + TcbScopeA AR Z OS ar s a σ Bf lbl len slot blk call don false buf extra + (fun σ' => ∃ xc σ1, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + (.ok [], σ') ∈ ((fl4InvokeTcb (.notificationControl (specPtr t) none)).run σ1).1) + | setMcp + {slot lbl len buf t p auth argSlot : U64} {blk call don : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Thread t) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hlbl : lbl = fl4_abi.label.TCBSetMCPriority) + (he : tcb.extra_with_length ⟨len, buf, call, slot, extra⟩ 0#usize 1#u64 = ok (.Ok argSlot)) + (hp : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 0#u64 = ok p) + (harg : argSlot ∈ OS) (hac : capA st s argSlot = .Thread auth) (hle : ¬ L.mem s (mcpA auth) < p) : + TcbScopeA AR Z OS ar s a σ Bf lbl len slot blk call don false buf extra + (fun σ' => ∃ xc σ1, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ∀ κs : CslotPtr, (.ok [], σ') ∈ ((fl4InvokeTcb (.threadControlSched (specPtr t) κs none + (some (BitVec.ofNat 8 p.val, specPtr auth)) none none)).run σ1).1) + | setPrioInactive + {slot lbl len buf t p auth argSlot : U64} {blk call don : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + {y : Tcb} + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Thread t) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hlbl : lbl = fl4_abi.label.TCBSetPriority) + (he : tcb.extra_with_length ⟨len, buf, call, slot, extra⟩ 0#usize 1#u64 = ok (.Ok argSlot)) + (hp : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 0#u64 = ok p) + (harg : argSlot ∈ OS) (hac : capA st s argSlot = .Thread auth) (hle : ¬ L.mem s (mcpA auth) < p) + (hy : σ.kheap (specPtr t) = some (.tcb y)) (hin : y.tcbState = .inactive) : + TcbScopeA AR Z OS ar s a σ Bf lbl len slot blk call don false buf extra + (fun σ' => ∃ xc σ1, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ∀ κs : CslotPtr, (.ok [], σ') ∈ ((fl4InvokeTcb (.threadControlSched (specPtr t) κs none none + (some (BitVec.ofNat 8 p.val, specPtr auth)) none)).run σ1).1) + | setPrioPlain + {slot lbl len buf t p auth argSlot : U64} {blk call don : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + {y : Tcb} + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Thread t) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hlbl : lbl = fl4_abi.label.TCBSetPriority) + (he : tcb.extra_with_length ⟨len, buf, call, slot, extra⟩ 0#usize 1#u64 = ok (.Ok argSlot)) + (hp : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 0#u64 = ok p) + (harg : argSlot ∈ OS) (hac : capA st s argSlot = .Thread auth) (hle : ¬ L.mem s (mcpA auth) < p) + (hy : σ.kheap (specPtr t) = some (.tcb y)) (htc : specPtr t ≠ σ.curThread) + (hpl : y.tcbState = .inactive ∨ (∃ r, y.tcbState = .blockedOnReply r) ∨ + (runnable y.tcbState = true ∧ ∀ d q, specPtr t ∉ σ.readyQueues d q)) : + TcbScopeA AR Z OS ar s a σ Bf lbl len slot blk call don false buf extra + (fun σ' => ∃ xc σ1, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ∀ κs : CslotPtr, (.ok [], σ') ∈ ((fl4InvokeTcb (.threadControlSched (specPtr t) κs none none + (some (BitVec.ofNat 8 p.val, specPtr auth)) none)).run σ1).1) + | setPrioEp + {slot lbl len buf t p auth argSlot : U64} {blk call don : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + {y : Tcb} + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Thread t) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hlbl : lbl = fl4_abi.label.TCBSetPriority) + (he : tcb.extra_with_length ⟨len, buf, call, slot, extra⟩ 0#usize 1#u64 = ok (.Ok argSlot)) + (hp : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 0#u64 = ok p) + (harg : argSlot ∈ OS) (hac : capA st s argSlot = .Thread auth) (hle : ¬ L.mem s (mcpA auth) < p) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p0 : ObjRef} (hy : σ.kheap (specPtr t) = some (.tcb y)) (htc : specPtr t ≠ σ.curThread) + (hb : epBlocked y.tcbState = some p0) : + TcbScopeA AR Z OS ar s a σ Bf lbl len slot blk call don false buf extra + (fun σ' => ∃ xc σ1, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ∀ κs : CslotPtr, (.ok [], σ') ∈ ((fl4InvokeTcb (.threadControlSched (specPtr t) κs none none + (some (BitVec.ofNat 8 p.val, specPtr auth)) none)).run σ1).1) + | setPrioNtfn + {slot lbl len buf t p auth argSlot : U64} {blk call don : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + {y : Tcb} + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Thread t) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hlbl : lbl = fl4_abi.label.TCBSetPriority) + (he : tcb.extra_with_length ⟨len, buf, call, slot, extra⟩ 0#usize 1#u64 = ok (.Ok argSlot)) + (hp : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 0#u64 = ok p) + (harg : argSlot ∈ OS) (hac : capA st s argSlot = .Thread auth) (hle : ¬ L.mem s (mcpA auth) < p) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p0 : ObjRef} (hy : σ.kheap (specPtr t) = some (.tcb y)) (htc : specPtr t ≠ σ.curThread) + (hb : y.tcbState = .blockedOnNotification p0) : + TcbScopeA AR Z OS ar s a σ Bf lbl len slot blk call don false buf extra + (fun σ' => ∃ xc σ1, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ∀ κs : CslotPtr, (.ok [], σ') ∈ ((fl4InvokeTcb (.threadControlSched (specPtr t) κs none none + (some (BitVec.ofNat 8 p.val, specPtr auth)) none)).run σ1).1) + | setPrioQueued + {slot lbl len buf t p auth argSlot : U64} {blk call don : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + {y : Tcb} + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Thread t) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hlbl : lbl = fl4_abi.label.TCBSetPriority) + (he : tcb.extra_with_length ⟨len, buf, call, slot, extra⟩ 0#usize 1#u64 = ok (.Ok argSlot)) + (hp : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 0#u64 = ok p) + (harg : argSlot ∈ OS) (hac : capA st s argSlot = .Thread auth) (hle : ¬ L.mem s (mcpA auth) < p) + (hy : σ.kheap (specPtr t) = some (.tcb y)) (hrun : runnable y.tcbState = true) + (hqc : (∃ d q, specPtr t ∈ σ.readyQueues d q) ∨ specPtr t = σ.curThread) : + TcbScopeA AR Z OS ar s a σ Bf lbl len slot blk call don false buf extra + (fun σ' => ∃ xc σ1, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ∀ κs : CslotPtr, (.ok [], σ') ∈ ((fl4InvokeTcb (.threadControlSched (specPtr t) κs none none + (some (BitVec.ofNat 8 p.val, specPtr auth)) none)).run σ1).1) + | bindNtfn + {slot lbl len buf t p badge ns : U64} {blk call don send : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Thread t) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hlbl : lbl = fl4_abi.label.TCBBindNotification) + (he : tcb.extra ⟨len, buf, call, slot, extra⟩ 0#usize = ok (some ns)) + (hns : ns ∈ OS) (hncap : capA st s ns = .Notification p badge send true) + (hfree : L.mem s (boundA t) = 0#u64) (hq : obj.Ntfn.head st p s = ok 0#u64) + (hnfree : obj.Ntfn.bound_tcb st p s = ok 0#u64) : + TcbScopeA AR Z OS ar s a σ Bf lbl len slot blk call don false buf extra + (fun σ' => ∃ xc σ1, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + (.ok [], σ') ∈ ((fl4InvokeTcb (.notificationControl (specPtr t) (some (specPtr p)))).run σ1).1) + | setTls + {slot lbl len buf t v : U64} {blk call don : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Thread t) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hlbl : lbl = fl4_abi.label.TCBSetTLSBase) (hlen : ¬ len < 1#u64) + (hv : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 0#u64 = ok v) (hne : t ≠ L.curW s) : + TcbScopeA AR Z OS ar s a σ Bf lbl len slot blk call don false buf extra + (fun σ' => ∃ xc σ1, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + (.ok [], σ') ∈ ((fl4InvokeTcb (.setTLSBase (specPtr t) v.bv)).run σ1).1) + | setTlsSelf + {slot lbl len buf t v : U64} {blk call don : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Thread t) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hlbl : lbl = fl4_abi.label.TCBSetTLSBase) (hlen : ¬ len < 1#u64) + (hv : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 0#u64 = ok v) (hself : t = L.curW s) : + TcbScopeA AR Z OS ar s a σ Bf lbl len slot blk call don false buf extra + (fun σ' => ∃ xc σ1, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + (.ok [], σ') ∈ ((fl4InvokeTcb (.setTLSBase (specPtr t) v.bv)).run σ1).1) + | setPacKeys + {slot lbl len buf t flags epoch : U64} {a' : A} {blk call don : Bool} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Thread t) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hlbl : lbl = fl4_abi.label.TCBSetPACKeys) (k0 k1 k2 k3 k4 k5 k6 k7 k8 k9 : U64) + (hlen : ¬ len < 11#u64) + (hflags : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 0#u64 = ok flags) + (hmask : flags &&& ~~~fl4_abi.consts.pac_flag.MASK = 0#u64) + (hprobe : flags ≠ 0#u64 → ar.has_pauth a = ok fl4A64Platform.hasPauth) + (hsupported : flags ≠ 0#u64 → fl4A64Platform.hasPauth = true) + (hepoch : ar.next_pac_epoch a = ok (epoch, a')) (hk : TcbArchKeepsA ar AR) + (hbuf : ∀ i : U64, i.val < 11 → ¬ i < msg.N_MSG_REGISTERS → buf ≠ 0#u64 → + let b := bufWA buf (core.num.U64.wrapping_add i 1#u64) + Aligned b ∧ b ≠ stA (L.curW s) ∧ b ≠ L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) + (g0 : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 1#u64 = ok k0) + (g1 : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 2#u64 = ok k1) + (g2 : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 3#u64 = ok k2) + (g3 : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 4#u64 = ok k3) + (g4 : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 5#u64 = ok k4) + (g5 : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 6#u64 = ok k5) + (g6 : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 7#u64 = ok k6) + (g7 : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 8#u64 = ok k7) + (g8 : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 9#u64 = ok k8) + (g9 : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 10#u64 = ok k9) : + TcbScopeA AR Z OS ar s a σ Bf lbl len slot blk call don false buf extra + (fun σ' => ∃ xc, σ.kheap σ.curThread = some (.tcb xc) ∧ σ' = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ')}, False) ∧ + ∀ f ks, (invokeFl4Tcb (.setPacKeys (specPtr t) f ks)).run σ' = ({(.ok [], σ')}, False)) + | resumeRunnable + {slot lbl len buf t : U64} {blk call don : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} {y : Tcb} + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Thread t) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) + (hlbl : lbl = fl4_abi.label.TCBResume) (hy : σ.kheap (specPtr t) = some (.tcb y)) + (hrun : y.tcbState = .running ∨ y.tcbState = .restart) : + TcbScopeA AR Z OS ar s a σ Bf lbl len slot blk call don false buf extra + (fun σ' => ∃ xc, σ.kheap σ.curThread = some (.tcb xc) ∧ σ' = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ')}, False) ∧ + (.ok [], σ') ∈ ((fl4InvokeTcb (.resume (specPtr t))).run σ').1) + + | resumeStopped + {slot lbl len buf t : U64} {blk call don : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} {y : Tcb} + {sl : CslotPtr} + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) (hmbF : ∀ a, Fl4MinBudgetAgreeA ar a) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Thread t) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hlbl : lbl = fl4_abi.label.TCBResume) (hsl : specCapAtA σ sl = some (.threadCap (specPtr t))) + (hy : σ.kheap (specPtr t) = some (.tcb y)) (hstop : ¬ runnable y.tcbState = true ∧ ¬ idle y.tcbState) : + TcbScopeA AR Z OS ar s a σ Bf lbl len slot blk call don false buf extra + (fun σ' => ∃ xc σ1, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + (.ok [], σ') ∈ ((fl4InvokeTcb (.resume (specPtr t))).run σ1).1) + + | writeFpu + {slot lbl len buf t fs offset ptr rights asid va : U64} {size : capability.FrameSize} {mapped stage2 : Bool} + {aF aS a' : A} {blk call don : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Thread t) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hlbl : lbl = fl4_abi.label.TCBWriteFPU) + (he : tcb.extra ⟨len, buf, call, slot, extra⟩ 0#usize = ok (some fs)) (hlen : len ≥ 1#u64) + (hoff : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 0#u64 = ok offset) + (hfcap : capA st s fs = .Frame ptr size rights false mapped stage2 asid va) + (hread : rights ≠ capability.VM_KERNEL_ONLY) (halign : offset.val % 64 = 0) + (hroom : offset.val + 520 ≤ fpuFrameBytesPubA size) + (hflush : ar.fpu_flush a t = ok aF) (hsan : ar.fpu_sanitise aF t = ok aS) (hrel : ar.fpu_reload aS t = ok a') + (hk : TcbArchKeepsA ar AR) + (howner : σ.archState.armCurrentFpuOwner ≠ some (specPtr t)) (hfs : fs ∈ OS) : + TcbScopeA AR Z OS ar s a σ Bf lbl len slot blk call don false buf extra + (fun σ' => ∃ xc σ1, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + (.ok [], σ') ∈ ((invokeFl4Tcb (.fpuTransfer (specPtr t) false + (specPtr (core.num.U64.wrapping_add ptr offset)))).run σ1).1) + + | setFlags + {slot lbl len buf t clear set : U64} {a' : A} {blk call don : Bool} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) (hX : StoreExt st) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Thread t) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hlbl : lbl = fl4_abi.label.TCBSetFlags) (hlen : ¬ len < 2#u64) + (hclear : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 0#u64 = ok clear) + (hset : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 1#u64 = ok set) + (hfl : ∀ y, σ.kheap (specPtr t) = some (.tcb y) → wordToTcbFlags (L.mem s (tTflA t)).bv = y.tcbFlags) + (hrel : (if (fl4TcbFlagsWord (L.mem s (tTflA t)).bv clear.bv set.bv &&& 1 ≠ 0) then ar.fpu_release a t + else ok a) = ok a') + (hk : TcbArchKeepsA ar AR) + (howner : fl4TcbFlagsWord (L.mem s (tTflA t)).bv clear.bv set.bv &&& 1 ≠ 0 → + σ.archState.armCurrentFpuOwner ≠ some (specPtr t)) : + TcbScopeA AR Z OS ar s a σ Bf lbl len slot blk call don false buf extra + (fun σ3 => ∃ xc σ1 σ2 d, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + (.ok d, σ2) ∈ ((invokeFl4Tcb (.setFlags (specPtr t) clear.bv set.bv)).run σ1).1 ∧ + σ2.curThread = σ.curThread ∧ + ((), σ3) ∈ ((do + Fl4Spec.NonDet.when call (Fl4SpecA64.replyFromKernel (Z := Unit) σ.curThread (0, d)) + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .running) σ2).1) + + | readFpu + {slot lbl len buf t fs offset ptr rights asid va bits : U64} {size : capability.FrameSize} + {mapped stage2 : Bool} {aF : A} {blk call don : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Thread t) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hlbl : lbl = fl4_abi.label.TCBReadFPU) + (he : tcb.extra ⟨len, buf, call, slot, extra⟩ 0#usize = ok (some fs)) (hlen : len ≥ 1#u64) + (hoff : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 0#u64 = ok offset) + (hfcap : capA st s fs = .Frame ptr size rights false mapped stage2 asid va) + (hwrite : rights = capability.VM_READ_WRITE) (halign : offset.val % 64 = 0) + (hbits : capability.FrameSize.bits size = ok bits) (hroom : offset.val + 520 ≤ fpuFrameBytesPubA size) + (hbuf : ∀ b, BufW Bf b → ¬ (ptr.val ≤ b.val ∧ b.val < ptr.val + 2 ^ bits.val)) + (hflush : ar.fpu_flush a t = ok aF) (hk : TcbArchKeepsA ar AR) + (howner : σ.archState.armCurrentFpuOwner ≠ some (specPtr t)) (hfs : fs ∈ OS) : + TcbScopeA AR Z OS ar s a σ Bf lbl len slot blk call don false buf extra + (fun σ' => ∃ xc σ1, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + (.ok [], σ') ∈ ((invokeFl4Tcb (.fpuTransfer (specPtr t) true + (specPtr (core.num.U64.wrapping_add ptr offset)))).run σ1).1) + + | suspend + {slot lbl len buf t : U64} {blk call don : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Thread t) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hlbl : lbl = fl4_abi.label.TCBSuspend) : + TcbScopeA AR Z OS ar s a σ Bf lbl len slot blk call don false buf extra + (fun σ' => ∃ xc σ1, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + (.ok [], σ') ∈ ((fl4InvokeTcb (.suspend (specPtr t))).run σ1).1) + | setIpcBufZero + {slot lbl len buf t src : U64} {blk call don : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + {Clock : A → Fl4SpecA64.MachineState → Prop} {rr : core.result.Result Unit error.Exc} {s3 : S} {a3 : A} + (hCnt : CounterStepA σ) (P : DelPlatA st ar AR Clock) (hCl : Clock a σ.machineState) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Thread t) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hlbl : lbl = fl4_abi.label.TCBSetIPCBuffer) + (he : tcb.extra_with_length ⟨len, buf, call, slot, extra⟩ 0#usize 1#u64 = ok (.Ok src)) + (harg : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 0#u64 = ok 0#u64) (hne : t ≠ L.curW s) + (hdec : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (rr, s3, a3)) (hrr : rr = .Ok ()) : + TcbScopeA AR Z OS ar s a σ Bf lbl len slot blk call don false buf extra + (fun σ' => ∃ xc σ1, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ∀ κs : CslotPtr, (.ok [], σ') ∈ ((fl4InvokeTcb (.threadControlCaps (specPtr t) κs none none none none + (some (Fl4SpecA64.dataToVref (0#u64).bv, none)))).run σ1).1) + /-- SetIPCBuffer with a non-zero 1024-aligned address and a non-device Frame source, the caller included + (`JSyscallA.setIpcBufNzA`); `hag` is the entry agreement of the 128 words of the new buffer ([S], ruling (b')). -/ + | setIpcBuf + {slot lbl len buf t src baddr ptr rights asid va bits : U64} {size : capability.FrameSize} {mp ep : Bool} + {blk call don : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + {Clock : A → Fl4SpecA64.MachineState → Prop} {rr : core.result.Result Unit error.Exc} {s3 : S} {a3 : A} + {κs κsrc : CslotPtr} + (hCnt : CounterStepA σ) (P : DelPlatA st ar AR Clock) (hCl : Clock a σ.machineState) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Thread t) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hlbl : lbl = fl4_abi.label.TCBSetIPCBuffer) + (he : tcb.extra_with_length ⟨len, buf, call, slot, extra⟩ 0#usize 1#u64 = ok (.Ok src)) + (harg : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 0#u64 = ok baddr) (hbn : baddr ≠ 0#u64) + (hbal : baddr.val % 1024 = 0) + (hsrcF : capA st s src = .Frame ptr size rights false mp ep asid va) (hbits : size.bits = ok bits) + (hag : FrAgA st OS {b | ∃ k : U64, k.val < 128 ∧ b = bufW (bufAddrOfA ptr baddr bits) k} s σ) + (hAκ : AbsSlotA Z.th κs) (hκ : cslotAddrA κs = slot) + (hAsrc : AbsSlotA Z.th κsrc) (hκsrc : cslotAddrA κsrc = src) + (hdec : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (rr, s3, a3)) (hrr : rr = .Ok ()) : + TcbScopeA AR Z OS ar s a σ Bf lbl len slot blk call don false buf extra + (fun σ' => ∃ xc σ1, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + (.ok [], σ') ∈ ((fl4InvokeTcb (.threadControlCaps (specPtr t) κs none none none none + (some (Fl4SpecA64.dataToVref baddr.bv, + some (absCapA (.Frame ptr size rights false false false 0#u64 0#u64), κsrc))))).run σ1).1) + | setTimeoutNull + {slot lbl len buf t src : U64} {blk call don : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + {Clock : A → Fl4SpecA64.MachineState → Prop} {rr : core.result.Result Unit error.Exc} {s3 : S} {a3 : A} + {κsrc : CslotPtr} + (hCnt : CounterStepA σ) (P : DelPlatA st ar AR Clock) (hCl : Clock a σ.machineState) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Thread t) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) + (hlbl : lbl = fl4_abi.label.TCBSetTimeoutEndpoint) + (he : tcb.extra_with_length ⟨len, buf, call, slot, extra⟩ 0#usize 0#u64 = ok (.Ok src)) + (hnullh : capA st s src = .Null) + (hdec : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (rr, s3, a3)) (hrr : rr = .Ok ()) : + TcbScopeA AR Z OS ar s a σ Bf lbl len slot blk call don false buf extra + (fun σ' => ∃ xc σ1, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ∀ κs : CslotPtr, (.ok [], σ') ∈ ((fl4InvokeTcb (.threadControlCaps (specPtr t) κs none + (some (.nullCap, κsrc)) none none none)).run σ1).1) + + | setTimeoutNonNull + {slot lbl len buf t src : U64} {blk call don : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + {Clock : A → Fl4SpecA64.MachineState → Prop} {rr : core.result.Result Unit error.Exc} {s3 : S} {a3 : A} + {κs κsrc : CslotPtr} + (hCnt : CounterStepA σ) (P : DelPlatA st ar AR Clock) (hCl : Clock a σ.machineState) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Thread t) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hlbl : lbl = fl4_abi.label.TCBSetTimeoutEndpoint) + (he : tcb.extra_with_length ⟨len, buf, call, slot, extra⟩ 0#usize 0#u64 = ok (.Ok src)) + (hnn : capA st s src ≠ .Null) (hValid : faults.valid_fault_handler (capA st s src) = ok true) + (hAκ : AbsSlotA Z.th κs) (hκ : cslotAddrA κs = slot) + (hAsrc : AbsSlotA Z.th κsrc) (hκsrc : cslotAddrA κsrc = src) + (hdec : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (rr, s3, a3)) (hrr : rr = .Ok ()) : + TcbScopeA AR Z OS ar s a σ Bf lbl len slot blk call don false buf extra + (fun σ' => ∃ xc σ1, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + (.ok [], σ') ∈ ((fl4InvokeTcb (.threadControlCaps (specPtr t) κs none + (some (absCapA (capA st s src), κsrc)) none none none)).run σ1).1) + + /-- SetSchedParams with a null fault handler, the SC unchanged (`target.sc = sc`, no SC step) and a target other + than the caller (`JSyscallA.setSchedParamsNullA`). Not covered by this constructor: an endpoint fault handler (the + insert after the deletion), `sc ≠ target.sc` (the SC `bind_tcb` / `unbind_tcb` steps), the caller as target. -/ + | setSchedParamsNull + {slot lbl len buf t authSlot scSlot fhSlot mcp prio auth v : U64} {blk call don : Bool} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + {Clock : A → Fl4SpecA64.MachineState → Prop} {rr : core.result.Result Unit error.Exc} {s3 : S} {a3 : A} + {κs κsrc : CslotPtr} + (hCnt : CounterStepA σ) (P : DelPlatA st ar AR Clock) (hCl : Clock a σ.machineState) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Thread t) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hlbl : lbl = fl4_abi.label.TCBSetSchedParams) + (he0 : tcb.extra_with_length ⟨len, buf, call, slot, extra⟩ 0#usize 2#u64 = ok (.Ok authSlot)) + (he1 : tcb.extra_with_length ⟨len, buf, call, slot, extra⟩ 1#usize 2#u64 = ok (.Ok scSlot)) + (he2 : tcb.extra_with_length ⟨len, buf, call, slot, extra⟩ 2#usize 2#u64 = ok (.Ok fhSlot)) + (hmcp : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 0#u64 = ok mcp) + (hprio : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 1#u64 = ok prio) + (harg : authSlot ∈ OS) (hac : capA st s authSlot = .Thread auth) + (hle1 : ¬ L.mem s (mcpA auth) < mcp) (hle2 : ¬ L.mem s (mcpA auth) < prio) + (hsc : tcb.sched_params_sc st s t scSlot = ok (.Ok v)) (hold : obj.Tcb.sc st t s = ok v) + (hfh : capA st s fhSlot = .Null) (hne : t ≠ L.curW s) + (hAκ : AbsSlotA Z.th κs) (hκ : cslotAddrA κs = slot) + (hdec : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (rr, s3, a3)) (hrr : rr = .Ok ()) : + TcbScopeA AR Z OS ar s a σ Bf lbl len slot blk call don false buf extra + (fun σ' => ∃ xc σ1, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + (.ok [], σ') ∈ ((fl4InvokeTcb (.threadControlSched (specPtr t) κs (some (.nullCap, κsrc)) + (some (BitVec.ofNat 8 mcp.val, specPtr auth)) (some (BitVec.ofNat 8 prio.val, specPtr auth)) none)).run + σ1).1) + + /-- SetSchedParams with an endpoint fault handler (installed into slot 3 after the deletion when `still_valid`), + the SC unchanged and a target other than the caller (`JSyscallA.setSchedParamsEpA`). Not covered: `sc ≠ target.sc`, + the caller as target. -/ + | setSchedParamsEp + {slot lbl len buf t authSlot scSlot fhSlot mcp prio auth v : U64} {blk call don : Bool} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + {Clock : A → Fl4SpecA64.MachineState → Prop} {rr : core.result.Result Unit error.Exc} {s3 : S} {a3 : A} + {κs κsrc : CslotPtr} + (hCnt : CounterStepA σ) (P : DelPlatA st ar AR Clock) (hCl : Clock a σ.machineState) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Thread t) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hlbl : lbl = fl4_abi.label.TCBSetSchedParams) + (he0 : tcb.extra_with_length ⟨len, buf, call, slot, extra⟩ 0#usize 2#u64 = ok (.Ok authSlot)) + (he1 : tcb.extra_with_length ⟨len, buf, call, slot, extra⟩ 1#usize 2#u64 = ok (.Ok scSlot)) + (he2 : tcb.extra_with_length ⟨len, buf, call, slot, extra⟩ 2#usize 2#u64 = ok (.Ok fhSlot)) + (hmcp : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 0#u64 = ok mcp) + (hprio : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 1#u64 = ok prio) + (harg : authSlot ∈ OS) (hac : capA st s authSlot = .Thread auth) + (hle1 : ¬ L.mem s (mcpA auth) < mcp) (hle2 : ¬ L.mem s (mcpA auth) < prio) + (hsc : tcb.sched_params_sc st s t scSlot = ok (.Ok v)) (hold : obj.Tcb.sc st t s = ok v) + (hnn : capA st s fhSlot ≠ .Null) (hValid : faults.valid_fault_handler (capA st s fhSlot) = ok true) + (hne : t ≠ L.curW s) + (hAκ : AbsSlotA Z.th κs) (hκ : cslotAddrA κs = slot) + (hAsrc : AbsSlotA Z.th κsrc) (hκsrc : cslotAddrA κsrc = fhSlot) + (hdec : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (rr, s3, a3)) (hrr : rr = .Ok ()) : + TcbScopeA AR Z OS ar s a σ Bf lbl len slot blk call don false buf extra + (fun σ' => ∃ xc σ1, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + (.ok [], σ') ∈ ((fl4InvokeTcb (.threadControlSched (specPtr t) κs + (some (absCapA (capA st s fhSlot), κsrc)) + (some (BitVec.ofNat 8 mcp.val, specPtr auth)) (some (BitVec.ofNat 8 prio.val, specPtr auth)) none)).run + σ1).1) + +variable {st} + +/-- **The TCB group is sound** (module docstring). -/ +theorem TcbScopeA.soundOk [PlainStore st] {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {ar : machine.Arch A} {s : S} {a : A} {σ : State Unit} {m : McState} + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + {lbl len slot : U64} {blk call don fp : Bool} {buf : U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} + {Out : State Unit → Prop} + (h : TcbScopeA st AR Z OS ar s a σ Bf lbl len slot blk call don fp buf extra Out) : + DispPostOkA st AR ar hPl.ipa n Z.Cs s a m lbl len slot blk call don fp buf extra Out := by + cases h with + | @resumeStopped _ _ _ _ t _ _ _ _ y _ hCs h1 hmb hmbF hmax htu hslot hcov hcap hext hSC hlbl hsl hy hstop => + obtain ⟨xc, σ1, σ2, s2, hkc, e1, hsp, hspec, hdec, Q, Rl, stk, J2, hm2, hsw, KB, htc⟩ := + J.resumeStoppedA hA hM hPl hfm hCs h1 hmb hmbF hmax htu hslot hcov hcap hext hSC hlbl hsl hy hstop + subst e1 + have hy1 : (stUpdA σ σ.curThread xc .restart).kheap (specPtr t) = some (.tcb y) := by + simp only [stUpdA, setObjA, Function.update_of_ne htc]; exact hy + have B := Sched.BodyEndA.ofRestartCurA J hkc htc hy1 hstop hm2 hsw KB J2.sr.inv J2.sinv.zc + exact ⟨_, _, σ2, hdec, ⟨xc, _, hkc, rfl, hsp, hspec⟩, Sched.curW_keptA J.toJStage3A J2 B.2, + A64RelS3.ofJ J2 B.1⟩ + | readFpu hCs h1 hslot hcov hcap hext hSC hlbl he hlen hoff hfcap hwrite halign hbits hroom hbuf hflush hk howner + hfs => + obtain ⟨xc, σ1, σ2, s2, hkc, e1, hsp, hspec, hdec, Q, Rl, J2⟩ := + J.readFpuA hA hM hPl hfm hCs h1 hslot hcov hcap hext hSC hlbl he hlen hoff hfcap hwrite halign hbits hroom hbuf + hflush hk howner hfs + exact ⟨_, _, σ2, hdec, ⟨xc, σ1, hkc, e1, hsp, hspec⟩, Sched.curW_keptA J.toJStage3A J2.1 J2.2.2, A64RelS3.ofJ J2.1 J2.2.1⟩ + | suspend hCs h1 hmb hmax htu hslot hcov hcap hext hSC hlbl => + obtain ⟨xc, σ1, σ2, s2, a2, hkc, e1, hsp, hspec, hdec, ea, Q, Rl, stk', J2, B2⟩ := + J.tcbSuspendA hA hM hPl hfm hCs h1 hmb hmax htu hslot hcov hcap hext hSC hlbl + subst ea + exact ⟨_, _, σ2, hdec, ⟨xc, σ1, hkc, e1, hsp, hspec⟩, Sched.curW_keptA J.toJStage3A J2 B2.2, A64RelS3.ofJ J2 B2.1⟩ + + | unbindNtfn hCs h1 hslot hcov hcap hext hSC hlbl hb => + obtain ⟨xc, σ1, σ2, s2, hkc, e1, hsp, hspec, hdec, Q, Rl, J2⟩ := + J.unbindNtfnA hA hM hPl hfm hCs h1 hslot hcov hcap hext hSC hlbl hb + exact ⟨_, _, σ2, hdec, ⟨xc, σ1, hkc, e1, hsp, hspec⟩, Sched.curW_keptA J.toJStage3A J2.1 J2.2.2, A64RelS3.ofJ J2.1 J2.2.1⟩ + | setMcp hCs h1 hslot hcov hcap hext hSC hlbl he hp harg hac hle => + obtain ⟨xc, σ1, σ2, s2, hkc, e1, hsp, hspec, hdec, Q, Rl, J2⟩ := + J.setMcpA hA hM hPl hfm hCs h1 hslot hcov hcap hext hSC hlbl he hp harg hac hle + exact ⟨_, _, σ2, hdec, ⟨xc, σ1, hkc, e1, hsp, hspec⟩, Sched.curW_keptA J.toJStage3A J2.1 J2.2.2, A64RelS3.ofJ J2.1 J2.2.1⟩ + | setPrioInactive hCs h1 hslot hcov hcap hext hSC hlbl he hp harg hac hle hy hin => + obtain ⟨xc, σ1, σ2, s2, hkc, e1, hsp, hspec, hdec, J2⟩ := + J.setPrioInactiveA hA hM hPl hfm hCs h1 hslot hcov hcap hext hSC hlbl he hp harg hac hle hy hin + exact ⟨_, _, σ2, hdec, ⟨xc, σ1, hkc, e1, hsp, hspec⟩, Sched.curW_keptA J.toJStage3A J2.1 J2.2.2, A64RelS3.ofJ J2.1 J2.2.1⟩ + | setPrioPlain hCs h1 hslot hcov hcap hext hSC hlbl he hp harg hac hle hy htc hpl => + obtain ⟨xc, σ1, σ2, s2, hkc, e1, hsp, hspec, hdec, J2⟩ := + J.setPrioPlainA hA hM hPl hfm hCs h1 hslot hcov hcap hext hSC hlbl he hp harg hac hle hy htc hpl + exact ⟨_, _, σ2, hdec, ⟨xc, σ1, hkc, e1, hsp, hspec⟩, Sched.curW_keptA J.toJStage3A J2.1 J2.2.2, A64RelS3.ofJ J2.1 J2.2.1⟩ + | setPrioEp hCs h1 hslot hcov hcap hext hSC hlbl he hp harg hac hle hmb hmax htu hy htc hb => + obtain ⟨xc, σ1, σ2, s2, hkc, e1, hsp, hspec, hdec, Q, Rl, J2⟩ := + J.setPrioEpA hA hM hPl hfm hmb hmax htu hCs h1 hslot hcov hcap hext hSC hlbl he hp harg hac hle hy htc hb + exact ⟨_, _, σ2, hdec, ⟨xc, σ1, hkc, e1, hsp, hspec⟩, Sched.curW_keptA J.toJStage3A J2.1 J2.2.2, A64RelS3.ofJ J2.1 J2.2.1⟩ + | setPrioNtfn hCs h1 hslot hcov hcap hext hSC hlbl he hp harg hac hle hmb hmax htu hy htc hb => + obtain ⟨xc, σ1, σ2, s2, hkc, e1, hsp, hspec, hdec, Q, Rl, J2⟩ := + J.setPrioNtfnA hA hM hPl hfm hmb hmax htu hCs h1 hslot hcov hcap hext hSC hlbl he hp harg hac hle hy htc hb + exact ⟨_, _, σ2, hdec, ⟨xc, σ1, hkc, e1, hsp, hspec⟩, Sched.curW_keptA J.toJStage3A J2.1 J2.2.2, A64RelS3.ofJ J2.1 J2.2.1⟩ + | setPrioQueued hCs h1 hslot hcov hcap hext hSC hlbl he hp harg hac hle hy hrun hqc => + obtain ⟨xc, σ1, σ2, s2, hkc, e1, hsp, hspec, hdec, Q, Rl, J2⟩ := + J.setPrioQueuedA hA hM hPl hfm hCs h1 hslot hcov hcap hext hSC hlbl he hp harg hac hle hy hrun hqc + exact ⟨_, _, σ2, hdec, ⟨xc, σ1, hkc, e1, hsp, hspec⟩, Sched.curW_keptA J.toJStage3A J2.1 J2.2.2, A64RelS3.ofJ J2.1 J2.2.1⟩ + | bindNtfn hCs h1 hmb hmax htu hslot hcov hcap hext hSC hlbl he hns hncap hfree hq hnfree => + obtain ⟨xc, σ1, σ2, s2, hkc, e1, hsp, hspec, hdec, Q, Rl, J2⟩ := + J.bindNtfnA hA hM hPl hfm hCs h1 hmb hmax htu hslot hcov hcap hext hSC hlbl he hns hncap hfree hq hnfree + exact ⟨_, _, σ2, hdec, ⟨xc, σ1, hkc, e1, hsp, hspec⟩, Sched.curW_keptA J.toJStage3A J2.1 J2.2.2, A64RelS3.ofJ J2.1 J2.2.1⟩ + | setTls hCs h1 hslot hcov hcap hext hSC hlbl hlen hv hne => + obtain ⟨xc, σ1, σ2, s2, hkc, e1, hsp, hspec, hdec, Q, Rl, J2⟩ := + J.setTlsA hA hM hPl hfm hCs h1 hslot hcov hcap hext hSC hlbl hlen hv hne + exact ⟨_, _, σ2, hdec, ⟨xc, σ1, hkc, e1, hsp, hspec⟩, Sched.curW_keptA J.toJStage3A J2.1 J2.2.2, A64RelS3.ofJ J2.1 J2.2.1⟩ + | setTlsSelf hCs h1 hslot hcov hcap hext hSC hlbl hlen hv hself => + obtain ⟨xc, σ1, σ2, s2, hkc, e1, hsp, hspec, hdec, Q, Rl, J2⟩ := + J.setTlsSelfA hA hM hPl hfm hCs h1 hslot hcov hcap hext hSC hlbl hlen hv hself + exact ⟨_, _, σ2, hdec, ⟨xc, σ1, hkc, e1, hsp, hspec⟩, Sched.curW_keptA J.toJStage3A J2.1 J2.2.2, A64RelS3.ofJ J2.1 J2.2.1⟩ + | setPacKeys hCs h1 hslot hcov hcap hext hSC hlbl k0 k1 k2 k3 k4 k5 k6 k7 k8 k9 hlen hflags hmask hprobe hsupported + hepoch hk hbuf g0 g1 g2 g3 g4 g5 g6 g7 g8 g9 => + obtain ⟨xc, σ1, s2, hkc, e1, hsp, hspec, hdec, Q, Rl, J2⟩ := + J.setPacKeysA hA hM hPl hfm hCs h1 hslot hcov hcap hext hSC hlbl k0 k1 k2 k3 k4 k5 k6 k7 k8 k9 hlen hflags hmask + hprobe hsupported hepoch hk hbuf g0 g1 g2 g3 g4 g5 g6 g7 g8 g9 + exact ⟨_, _, σ1, hdec, ⟨xc, hkc, e1, hsp, hspec⟩, Sched.curW_keptA J.toJStage3A J2.1 J2.2.2, A64RelS3.ofJ J2.1 J2.2.1⟩ + | resumeRunnable hCs h1 hslot hcov hcap hext hlbl hy hrun => + obtain ⟨xc, σ1, s2, hkc, e1, hsp, hspec, hdec, Q, Rl, J2⟩ := + J.resumeRunnableA hPl hCs h1 hslot hcov hcap hext hlbl hy hrun + exact ⟨_, _, σ1, hdec, ⟨xc, hkc, e1, hsp, hspec⟩, Sched.curW_keptA J.toJStage3A J2.1 J2.2.2, A64RelS3.ofJ J2.1 J2.2.1⟩ + + | writeFpu hCs h1 hslot hcov hcap hext hSC hlbl he hlen hoff hfcap hread halign hroom hflush hsan hrel hk howner + hfs => + obtain ⟨xc, σ1, σ2, s2, hkc, e1, hsp, hspec, hdec, Q, Rl, J2⟩ := + J.writeFpuA hA hM hPl hfm hCs h1 hslot hcov hcap hext hSC hlbl he hlen hoff hfcap hread halign hroom hflush hsan + hrel hk howner hfs + exact ⟨_, _, σ2, hdec, ⟨xc, σ1, hkc, e1, hsp, hspec⟩, Sched.curW_keptA J.toJStage3A J2.1 J2.2.2, A64RelS3.ofJ J2.1 J2.2.1⟩ + | setFlags hCs h1 hX hslot hcov hcap hext hSC hlbl hlen hclear hset hfl hrel hk howner => + obtain ⟨xc, σ1, σ2, d, σ3, s3, hkc, e1, hsp, hspec, hcur, hrun, hdec, -, Q, Rl, J2⟩ := + J.setFlagsA hA hM hPl hfm hCs h1 hX hslot hcov hcap hext hSC hlbl hlen hclear hset hfl hrel hk howner + exact ⟨_, _, σ3, hdec, ⟨xc, σ1, σ2, d, hkc, e1, hsp, hspec, hcur, hrun⟩, Sched.curW_keptA J.toJStage3A J2.1 J2.2.2, A64RelS3.ofJ J2.1 J2.2.1⟩ + + | @setIpcBufZero slot lbl len buf t src blk call don extra Clock rr s3 a3 hCnt P hCl hCs h1 hslot hcov hcap hext hSC hlbl + he harg hne hdec hrr => + obtain ⟨r', s3', a3', hdec', xc, σ1, σ3, hkc, e1, hsp, G', Q, R, hw, Ac', Bf', stack', J2, hres⟩ := + J.setIpcBufZeroA hA hM hPl s a σ m hfm hCnt P hCl hCs h1 hslot hcov hcap hext hSC hlbl he harg hne + have e := okinj (hdec.symm.trans hdec') + simp only [Prod.mk.injEq] at e + obtain ⟨e1', e2', e3'⟩ := e + subst e1' e2' e3' + rcases hres with ⟨-, hspec, B⟩ | ⟨hr, -⟩ + · exact ⟨_, _, σ3, hrr ▸ hdec, ⟨xc, σ1, hkc, e1, hsp, hspec⟩, Sched.curW_keptA J.toJStage3A J2 B.2, + A64RelS3.ofJ J2 B.1⟩ + · rw [hrr] at hr; cases hr + | @setIpcBuf slot lbl len buf t src baddr ptr rights asid va bits size mp ep blk call don extra Clock rr s3 a3 κs κsrc + hCnt P hCl hCs h1 hslot hcov hcap hext hSC hlbl he harg hbn hbal hsrcF hbits hag hAκ hκ hAsrc hκsrc hdec hrr => + obtain ⟨r', s3', a3', hdec', xc, σ1, σ3, hkc, e1, hsp, G', Q, R, hw, Ac', Bf', stack', J2, hres⟩ := + J.setIpcBufNzA hA hM hPl s a σ m hfm hCnt P hCl hCs h1 hslot hcov hcap hext hSC hlbl he harg hbn hbal hsrcF + hbits hag hAκ hκ hAsrc hκsrc + have e := okinj (hdec.symm.trans hdec') + simp only [Prod.mk.injEq] at e + obtain ⟨e1', e2', e3'⟩ := e + subst e1' e2' e3' + rcases hres with ⟨-, hspec, B⟩ | ⟨hr, -⟩ + · exact ⟨_, _, σ3, hrr ▸ hdec, ⟨xc, σ1, hkc, e1, hsp, hspec κs⟩, Sched.curW_keptA J.toJStage3A J2 B.2, + A64RelS3.ofJ J2 B.1⟩ + · rw [hrr] at hr; cases hr + | @setTimeoutNull slot lbl len buf t src blk call don extra Clock rr s3 a3 κsrc hCnt P hCl hCs h1 hslot hcov hcap hext hlbl + he hnullh hdec hrr => + obtain ⟨r', s3', a3', hdec', xc, σ1, σ3, hkc, e1, hsp, G', Q, R, hw, Ac', Bf', stack', J2, hres⟩ := + J.setTimeoutNullA hA hM hPl s a σ m hCnt P hCl hCs h1 hslot hcov hcap hext hlbl he hnullh κsrc + have e := okinj (hdec.symm.trans hdec') + simp only [Prod.mk.injEq] at e + obtain ⟨e1', e2', e3'⟩ := e + subst e1' e2' e3' + rcases hres with ⟨-, hspec, B⟩ | ⟨hr, -⟩ + · exact ⟨_, _, σ3, hrr ▸ hdec, ⟨xc, σ1, hkc, e1, hsp, hspec⟩, Sched.curW_keptA J.toJStage3A J2 B.2, + A64RelS3.ofJ J2 B.1⟩ + · rw [hrr] at hr; cases hr + | @setTimeoutNonNull slot lbl len buf t src blk call don extra Clock rr s3 a3 κs κsrc hCnt P hCl hCs h1 hslot hcov hcap + hext hSC hlbl he hnn hValid hAκ hκ hAsrc hκsrc hdec hrr => + obtain ⟨r', s3', a3', hdec', xc, σ1, σ3, hkc, e1, hsp, G', Q, R, hw, Ac', Bf', stack', J2, hres⟩ := + J.setTimeoutNonNullA hA hM hPl s a σ m hfm hCnt P hCl hCs h1 hslot hcov hcap hext hSC hlbl he hnn hValid + hAκ hκ hAsrc hκsrc + have e := okinj (hdec.symm.trans hdec') + simp only [Prod.mk.injEq] at e + obtain ⟨e1', e2', e3'⟩ := e + subst e1' e2' e3' + rcases hres with ⟨-, hspec, B⟩ | ⟨hr, -⟩ + · exact ⟨_, _, σ3, hrr ▸ hdec, ⟨xc, σ1, hkc, e1, hsp, hspec κs⟩, Sched.curW_keptA J.toJStage3A J2 B.2, + A64RelS3.ofJ J2 B.1⟩ + · rw [hrr] at hr; cases hr + | @setSchedParamsNull slot lbl len buf t authSlot scSlot fhSlot mcp prio auth v blk call don extra Clock rr s3 a3 κs + κsrc hCnt P hCl hCs h1 hmb hmax htu hslot hcov hcap hext hSC hlbl he0 he1 he2 hmcp hprio harg hac hle1 hle2 hsc + hold hfh hne hAκ hκ hdec hrr => + obtain ⟨r', s3', a3', hdec', xc, σ1, σ3, hkc, e1, hsp, G', Q, R, hw, Ac', Bf', stack', J2, hres⟩ := + J.setSchedParamsNullA hA hM hPl s a σ m hfm hCnt P hCl hCs h1 hmb hmax htu hslot hcov hcap hext hSC hlbl he0 he1 + he2 hmcp hprio harg hac hle1 hle2 hsc hold hfh hne hAκ hκ κsrc + have e := okinj (hdec.symm.trans hdec') + simp only [Prod.mk.injEq] at e + obtain ⟨e1', e2', e3'⟩ := e + subst e1' e2' e3' + rcases hres with ⟨-, hspec, B⟩ | ⟨hr, -⟩ + · exact ⟨_, _, σ3, hrr ▸ hdec, ⟨xc, σ1, hkc, e1, hsp, hspec κs⟩, Sched.curW_keptA J.toJStage3A J2 B.2, + A64RelS3.ofJ J2 B.1⟩ + · rw [hrr] at hr; cases hr + + | @setSchedParamsEp slot lbl len buf t authSlot scSlot fhSlot mcp prio auth v blk call don extra Clock rr s3 a3 κs + κsrc hCnt P hCl hCs h1 hmb hmax htu hslot hcov hcap hext hSC hlbl he0 he1 he2 hmcp hprio harg hac hle1 hle2 hsc + hold hnn hValid hne hAκ hκ hAsrc hκsrc hdec hrr => + obtain ⟨r', s3', a3', hdec', xc, σ1, σ3, hkc, e1, hsp, G', Q, R, hw, Ac', Bf', stack', J2, hres⟩ := + J.setSchedParamsEpA hA hM hPl s a σ m hfm hCnt P hCl hCs h1 hmb hmax htu hslot hcov hcap hext hSC hlbl he0 he1 + he2 hmcp hprio harg hac hle1 hle2 hsc hold hnn hValid hne hAκ hκ hAsrc hκsrc + have e := okinj (hdec.symm.trans hdec') + simp only [Prod.mk.injEq] at e + obtain ⟨e1', e2', e3'⟩ := e + subst e1' e2' e3' + rcases hres with ⟨-, hspec, B⟩ | ⟨hr, -⟩ + · exact ⟨_, _, σ3, hrr ▸ hdec, ⟨xc, σ1, hkc, e1, hsp, hspec κs⟩, Sched.curW_keptA J.toJStage3A J2 B.2, + A64RelS3.ofJ J2 B.1⟩ + · rw [hrr] at hr; cases hr + +/-- **The TCB group is sound** (module docstring). -/ +theorem TcbScopeA.sound [PlainStore st] {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {ar : machine.Arch A} {s : S} {a : A} {σ : State Unit} {m : McState} + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + {lbl len slot : U64} {blk call don fp : Bool} {buf : U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} + {Out : State Unit → Prop} + (h : TcbScopeA st AR Z OS ar s a σ Bf lbl len slot blk call don fp buf extra Out) : + DispPostA st AR ar hPl.ipa n Z.Cs s a m lbl len slot blk call don fp buf extra Out := + (TcbScopeA.soundOk hA hM hPl J hfm h).post + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3DispTopA.lean b/proofs/Fl4ProofsA64/P3/Stage3DispTopA.lean new file mode 100644 index 000000000..2e5f44e21 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3DispTopA.lean @@ -0,0 +1,96 @@ +/- +P3 (aarch64, A64-P3 Stage 3, T3): **the system-call dispatch top** over the arms lifted to the record +(`Stage3DispBaseA`): from `JSyscallA` and an invocation in scope, FL4's `decode_invocation` at the invoked slot's own +cap returns, the spec outcome holds, and the Stage-3 record holds again. + +Scope (`DispScopeA`): the VSpace group (`VsScopeA`), the CNode group (`CnScopeA`), the Untyped group +(`UtScopeA`, an empty slot until the Untyped line is merged), the IPC group (`IpcScopeA`), the SchedContext group (`ScScopeA`), the TCB group (`TcbScopeA`), the cache-maintenance +group (`FlScopeA`), the IRQ group (`IrqScopeA`: `IRQAckIRQ`, `IRQSetIRQHandler`, `IRQClearIRQHandler`), the one-word reply group (`ReplyScopeA`: +`ARMPageGetAddress`, `ARMPageTestAndClearAccessed`), the Domain group (`DomScopeA`: `DomainSetSet`), the VCPU group (`VcpuScopeA`: labels 61–64, 67, 68 of a VCPU cap with a successful decode), or the non-arch `Err` group (`ErrScopeA`: an `Err` other than `Preempted` of a non-arch, +non-IRQ-control cap, or any `Err` of the IRQ control cap). +**Not in scope** (an invocation whose decode succeeds outside `DispScopeA` makes the top's scope premise `hArm` false at that +state: [S] limits, listed in DESIGN §9): Untyped Retype / RetypeShared (`UtScopeA` has +no constructor); TCB Configure, SetSpace, ReadRegisters, WriteRegisters, CopyRegisters, SetTimer, SetHighVSpace, the breakpoint labels and +single stepping, SetSchedParams with a changed SC or another handler cap; +DomainScheduleConfigure / SetStart; ARMASIDControlMakePool; ARMPageDeriveSubframe; the Stage-2 frame map, the PT map into a high +window or Stage-2, the T2 unmap shrink; Notification send to a bound thread running a VCPU. The `Err` returns (other than `Preempted`) are not +in this scope: they are `hErr`'s (`decErrSpecA`) and `ErrScopeA`'s. + +Named premises: [J] `JSyscallA`; [P] `ArchTotal`, `MaxSpi`, `ArchPlat`, `A64FrameMasks`; those of each arm. +-/ +import Fl4ProofsA64.P3.Stage3DispVsA +import Fl4ProofsA64.P3.Stage3DispTcbA +import Fl4ProofsA64.P3.Stage3DispCnA +import Fl4ProofsA64.P3.Stage3DispUtA +import Fl4ProofsA64.P3.Stage3DispFlushA +import Fl4ProofsA64.P3.Stage3DispIrqA +import Fl4ProofsA64.P3.Stage3DispReplyA +import Fl4ProofsA64.P3.Stage3DispDomA +import Fl4ProofsA64.P3.Stage3DispErrA +import Fl4ProofsA64.P3.Stage3DispIpcA +import Fl4ProofsA64.P3.Stage3DispScA +import Fl4ProofsA64.P3.Stage3DispVcpuA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST + +noncomputable section + +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {ar : machine.Arch A} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} +variable {Clock : A → Fl4SpecA64.MachineState → Prop} + +variable (st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ m Clock) in +/-- **Plan B's four groups** (VSpace, CNode, Untyped, IPC) and the SchedContext and TCB groups (Lift 1c): the scope the +`InvSpecA` assembly of `P3/Stage3InvSpecTopA` covers. -/ +def DispScope4A (nA lbl len slot : U64) (blk call don fp : Bool) (buf : U64) + (extra : Aeneas.Std.Array obj.Slot 3#usize) (Out : Fl4SpecA64.State Unit → Prop) : Prop := + VsScopeA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar nA s a σ m lbl len slot blk call don fp buf extra + Out ∨ + CnScopeA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar nA s a σ m Clock lbl len slot blk call don fp buf extra + Out ∨ + UtScopeA lbl len slot blk call don fp buf extra Out ∨ + IpcScopeA st Z OS N E Rp ar s σ m lbl len slot blk call don fp buf extra Out ∨ + ScScopeA st AR Z OS ar s a σ m lbl len slot blk call don fp buf extra Out ∨ + TcbScopeA st AR Z OS ar s a σ Bf lbl len slot blk call don fp buf extra Out + +variable (st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ m Clock) in +/-- **The dispatch scope** (module docstring): Plan B's four groups, then one disjunct per Lift 1 group. -/ +def DispScopeA (nA lbl len slot : U64) (blk call don fp : Bool) (buf : U64) + (extra : Aeneas.Std.Array obj.Slot 3#usize) (Out : Fl4SpecA64.State Unit → Prop) : Prop := + DispScope4A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ m Clock nA lbl len slot blk call don fp buf extra + Out ∨ + FlScopeA st AR Z OS ar s a σ lbl len slot blk call don fp buf extra Out ∨ + IrqScopeA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ lbl len slot blk call don fp buf extra Out ∨ + ReplyScopeA st AR Z OS ar s σ lbl len slot blk call don fp buf extra Out ∨ + DomScopeA st AR Z OS ar s a σ lbl len slot blk call don fp buf extra Out ∨ + ErrScopeA st AR Z OS ar s a σ lbl len slot blk call don fp buf extra Out ∨ + VcpuScopeA st AR Z OS n ar s a σ lbl len slot blk call don fp buf extra Out + +/-- **The dispatch top** (module docstring): the record by result (`A64RelRS3`; `A64RelPS3` after `Preempted`). -/ +theorem JSyscallA.dispatchA [PlainStore st] (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (hCnt : Sched.CounterStepA σ) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + {lbl len slot : U64} {blk call don fp : Bool} {buf : U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} + {Out : Fl4SpecA64.State Unit → Prop} + (h : DispScopeA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ m Clock hPl.ipa lbl len slot blk call don + fp buf extra Out) : + DispPostRA st AR ar hPl.ipa n Z.Cs s a m Clock lbl len slot blk call don fp buf extra Out := + h.elim (fun h => h.elim (fun h => (VsScopeA.sound hA hM hPl J hfm h).toR) fun h => + h.elim (CnScopeA.sound hA hM hPl J hfm) fun h => + h.elim (fun h => (UtScopeA.sound h).toR) fun h => + h.elim (fun h => (IpcScopeA.sound hA hM hPl J hfm h).toR) fun h => + h.elim (fun h => (ScScopeA.sound hA hM hPl J hfm h).toR) fun h => (TcbScopeA.sound hA hM hPl J hfm h).toR) + fun h => h.elim (fun h => (FlScopeA.sound J h).toR) fun h => + h.elim (fun h => (IrqScopeA.sound (hCnt := hCnt) hA hM hPl J hfm h).toR) fun h => + h.elim (fun h => (ReplyScopeA.sound hA hM hPl J hfm h).toR) fun h => + h.elim (fun h => (DomScopeA.sound hA hM hPl J hfm h).toR) fun h => + h.elim (fun h => (ErrScopeA.sound J h).toR) fun h => (VcpuScopeA.sound hA hM hPl J hfm h).toR + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3DispUtA.lean b/proofs/Fl4ProofsA64/P3/Stage3DispUtA.lean new file mode 100644 index 000000000..f31bcbf7a --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3DispUtA.lean @@ -0,0 +1,32 @@ +/- +P3 (aarch64, A64-P3 Stage 3, T3 dispatch): **the Untyped dispatch group's slot**, empty. `UtScopeA` has no +constructor until the Untyped line is on record edit #6 and merged; its constructors then carry that line's record +tops (Untyped Retype), each with a case of `sound`. Until then no Untyped invocation is in scope. + +Named premises: none. +-/ +import Fl4ProofsA64.P3.Stage3DispBaseA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 + +noncomputable section + +/-- **The Untyped arms in scope**: none yet (module docstring). Indexed as the other groups. -/ +inductive UtScopeA : U64 → U64 → U64 → Bool → Bool → Bool → Bool → U64 → Aeneas.Std.Array obj.Slot 3#usize → + (Fl4SpecA64.State Unit → Prop) → Prop + +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-- The empty group's `sound`. -/ +theorem UtScopeA.sound {AR : A → HwRootA → Prop} {ar : machine.Arch A} {nA : U64} {n : Nat} {Cs : Set U64} {s : S} {a : A} + {m : Fl4SpecA64.McState} {lbl len slot : U64} {blk call don fp : Bool} {buf : U64} + {extra : Aeneas.Std.Array obj.Slot 3#usize} {Out : Fl4SpecA64.State Unit → Prop} + (h : UtScopeA lbl len slot blk call don fp buf extra Out) : + DispPostA st AR ar nA n Cs s a m lbl len slot blk call don fp buf extra Out := by + cases h + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3DispVcpuA.lean b/proofs/Fl4ProofsA64/P3/Stage3DispVcpuA.lean new file mode 100644 index 000000000..2d7ef11f5 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3DispVcpuA.lean @@ -0,0 +1,295 @@ +/- +P3 (aarch64, A64-P3 Stage 3, s4w): **the VCPU dispatch group** (`Stage3DispBaseA`) over the unified VCPU decode +`JSyscallA.vcpuDecodeA` (labels 60–64, 67–69 on a VCPU cap). + +`VcpuInA`: the data and premises of one VCPU invocation (all of `vcpuDecodeA`'s: the VCPU cap in the invoked slot, its +front-end pin, the user message words, the first extra cap, FL4's decode returning `Ok`). `VcpuOutA`: the spec outcome +(the caller's `Restart`, the VCPU perform, and for a reply the `replyFromKernel` and `Running`). + +Constructors and selectors (the label is the selector; the invocation shape follows from it): +* `plain`: `ARMVCPUInjectIRQ` (61), `ReadReg` (62), `WriteReg` (63), `AckVPPI` (64), `ReadState` (67), `WriteState` + (68): the label is neither `SetTCB` nor `Run`, and the perform is then a `DlA` step (`P3/Stage3VcpuKeepA`). +* `setTcb`: `ARMVCPUSetTCB` (60), the label equal to it; its perform is a `DcA` step (`P3/Stage3VcpuRunA`). +* `run`: `ARMVCPURun` (69), the label equal to it; the invocation's caller is the current thread, and its perform is a + `DrA` step (`P3/Stage3VcpuRunA`: the caller ends `Running` or `runningVM`). + +Named premises: [P] `VcpuPlatAgreeA`, `VcpuArchKeepsA`, `VcpuVmRootA`, `VcpuStage2RootA`; [S] `numNodes = 1`, `hCs`, +`hCsN`, `hNoS2` (the first extra cap is no Stage-2-mapped frame), `hAgreeWS` / `hAgreeRun` (`[scope limit]`: +`FrameInputAgreeA` on the 1 KiB input window when the mask is non-zero), and the decode returning `Ok` (a decode `Err` +of a VCPU cap is `DecErrSpecPA.vcpu`'s, not this group's). No new premise kind. New module. +-/ +import Fl4ProofsA64.P3.Stage3DispBaseA +import Fl4ProofsA64.P3.Stage3VcpuKeepA +import Fl4ProofsA64.P3.Stage3VcpuRunA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Vcpu Fl4Spec Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} (st : machine.Store S) [L : LawfulStore st] {A : Type} + +/-- **The spec outcome of a VCPU invocation** (module docstring). -/ +def VcpuOutA (Z : XSetsA) (σ : Fl4SpecA64.State Unit) (call : Bool) (slot : U64) + (P : Fl4VcpuInvocation → Prop) (rep : Prop) (σ3 : Fl4SpecA64.State Unit) : Prop := + ∃ (x : Fl4SpecA64.Tcb) (inv : Fl4VcpuInvocation), + P inv ∧ σ.kheap σ.curThread = some (.tcb x) ∧ + VcpuDecPinA call Z.th σ slot (.invokeArchObject (.vcpu inv)) ∧ + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + (((.ok [], σ3) ∈ ((fl4ArchPerformInvocation (Z := Unit) (.vcpu inv)).run + (stUpdA σ σ.curThread x .restart)).1) ∨ + (rep ∧ ∃ σ2 ws, + (.ok ws, σ2) ∈ ((fl4ArchPerformInvocation (Z := Unit) (.vcpu inv)).run + (stUpdA σ σ.curThread x .restart)).1 ∧ + ((), σ3) ∈ ((do + Fl4Spec.NonDet.when call (Fl4SpecA64.replyFromKernel (Z := Unit) σ.curThread (0, ws)) + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .running) σ2).1)) + +/-- **One VCPU invocation's data and premises** (module docstring): exactly `JSyscallA.vcpuDecodeA`'s. -/ +structure VcpuInA (AR : A → HwRootA → Prop) (Z : XSetsA) (OS : Set U64) (n : Nat) (ar : machine.Arch A) + (s : S) (a : A) (σ : Fl4SpecA64.State Unit) (label length slot : U64) (blk call don fp : Bool) (buf : U64) + (extra : Aeneas.Std.Array obj.Slot 3#usize) where + vc : U64 + hP : VcpuPlatAgreeA ar + hAK : VcpuArchKeepsA ar AR + hVR : VcpuVmRootA st ar AR + hS2R : VcpuStage2RootA st ar AR + hCs : ∀ c ∈ Z.Cs, c.val < Fl4SpecA64.fl4A64Platform.numNodes + hCsN : ∀ c ∈ Z.Cs, c.val < n + h1 : Fl4SpecA64.fl4A64Platform.numNodes = 1 + hslot : slot ∈ OS + hcov : SlotCov st OS s slot + hcap : capA st s slot = .Vcpu vc + hvc : vc ∈ Z.V + hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS + hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x + mi : MessageInfo + sl : CslotPtr + excaps : List (Fl4SpecA64.Cap × CslotPtr) + buffer : Option ObjRef + args : List U64 + hfp : VcpuFrontPinA Z.th σ slot mi sl (.archObjectCap (.vcpuCap (specPtr vc))) excaps buffer (args.map UScalar.bv) + hlbl : mi.miLabel = label.bv + hlen : length.val = args.length + hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x + hex0 : ∀ s1, Array.index_usize extra 0#usize = ok s1 → s1 ≠ 0#u64 → + ∃ c0 sl0, excaps[0]? = some (c0, sl0) ∧ CapRelA (capA st s s1) c0 + hNoZomb : ∀ s1 ptr, Array.index_usize extra 0#usize = ok s1 → capA st s s1 = .Thread ptr → + ∀ u ∈ OS, ∀ num, capA st s u ≠ .Zombie ptr capability.ZOMBIE_TCB num + hNoS2 : ∀ s1 p sz r d mp ept asid va, Array.index_usize extra 0#usize = ok s1 → + capA st s s1 = .Frame p sz r d mp ept asid va → ept = false + hAgreeWS : ∀ s1 p sz r mp ept asid va offset mask, + Array.index_usize extra 0#usize = ok s1 → s1 ≠ 0#u64 → + capA st s s1 = .Frame p sz r false mp ept asid va → + args[0]? = some offset → args[1]? = some mask → mask ≠ 0#u64 → + FrameInputAgreeA st s σ (wa p offset) + hAgreeRun : ∀ ptr sz rights mp ept asid va offset in_mask, + capA st s (sbWordA (L.curW s)) = .Frame ptr sz rights false mp ept asid va → + args[0]? = some offset → args[1]? = some in_mask → in_mask ≠ 0#u64 → + FrameInputAgreeA st s σ (wa ptr offset) + s3 : S + a3 : A + hdec : syscall.decode_invocation st ar s a label length slot (.Vcpu vc) blk call don fp buf extra = + ok (core.result.Result.Ok (), s3, a3) + +/-- **The VCPU arms in scope** (module docstring). + +Coverage of the VCPU surface (s4w): +* Labels 60-69, including `ARMVCPU_Run` (69), refine at the **body / dispatch level**: the three constructors give the + decode's spec outcome and the body-end record (`A64RelS3`, with `BodyOKA` from the `DlA` / `DcA` / `DrA` frames). +* `runExit` and `runVppi` (the VM-exit entries) refine **end to end**: `P3/Stage3VcpuExitWireA` wires + `JStage3A.vcpuRunExitA` / `vcpuRunVppiA` into `kernelEntry_recGA`. +* The `finish` after the body carries the ratified [S4] `hNative` (`kernelEntry_recGA`'s `hNat`: after `schedule` and + `activate_thread` the current thread is not `runningVM`), so an `ARMVCPU_Run` whose caller stays current and resumes + the guest is **outside** that top (a guest-resume `finish` variant is not provided). +* `vgic`, `vppi`, `vcpuExit` and `vcpuFault` (the other four `KGuestA` bodies) are **deferred** to the S4 guest-lifecycle + follow-up: no record theorem exists for them yet. -/ +inductive VcpuScopeA (AR : A → HwRootA → Prop) (Z : XSetsA) (OS : Set U64) (n : Nat) (ar : machine.Arch A) (s : S) + (a : A) (σ : Fl4SpecA64.State Unit) : + U64 → U64 → U64 → Bool → Bool → Bool → Bool → U64 → Aeneas.Std.Array obj.Slot 3#usize → + (Fl4SpecA64.State Unit → Prop) → Prop + | plain (label length slot : U64) (blk call don fp : Bool) (buf : U64) (extra : Aeneas.Std.Array obj.Slot 3#usize) + (I : VcpuInA st AR Z OS n ar s a σ label length slot blk call don fp buf extra) + (h60 : label ≠ fl4_abi.label.ARMVCPUSetTCB) (h69 : label ≠ fl4_abi.label.ARMVCPURun) : + VcpuScopeA AR Z OS n ar s a σ label length slot blk call don fp buf extra (VcpuOutA Z σ call slot VcpuPlainA True) + | setTcb (label length slot : U64) (blk call don fp : Bool) (buf : U64) (extra : Aeneas.Std.Array obj.Slot 3#usize) + (I : VcpuInA st AR Z OS n ar s a σ label length slot blk call don fp buf extra) + (hl : label = fl4_abi.label.ARMVCPUSetTCB) : + VcpuScopeA AR Z OS n ar s a σ label length slot blk call don fp buf extra + (VcpuOutA Z σ call slot (fun inv => ∃ v t, inv = .setTcb v t) False) + | run (label length slot : U64) (blk call don fp : Bool) (buf : U64) (extra : Aeneas.Std.Array obj.Slot 3#usize) + (I : VcpuInA st AR Z OS n ar s a σ label length slot blk call don fp buf extra) + (hl : label = fl4_abi.label.ARMVCPURun) : + VcpuScopeA AR Z OS n ar s a σ label length slot blk call don fp buf extra + (VcpuOutA Z σ call slot (fun inv => ∃ v fr o i p g, inv = .run v σ.curThread fr o i p g) False) + +variable {st} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {ar : machine.Arch A} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- **The VCPU group is sound.** -/ +theorem VcpuScopeA.sound (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) + s a σ m) + (hfm : A64FrameMasks st s) + {lbl len slot : U64} {blk call don fp : Bool} {buf : U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} + {Out : Fl4SpecA64.State Unit → Prop} + (h : VcpuScopeA st AR Z OS n ar s a σ lbl len slot blk call don fp buf extra Out) : + DispPostA st AR ar hPl.ipa n Z.Cs s a m lbl len slot blk call don fp buf extra Out := by + cases h with + | plain I h60 h69 => + obtain ⟨x, inv, hw', Ac', σ3, hx, hpin, hst, hperf, J3, hshape, -, -⟩ := + J.vcpuDecodeA hA hM hPl hfm I.hP I.hAK I.hVR I.hS2R I.hCs I.hCsN I.h1 + J.toJStage3EntryA.curNotVmA I.hslot I.hcov I.hcap I.hvc I.hext I.hSC I.hfp I.hlbl I.hlen I.hargs I.hex0 + I.hNoZomb I.hNoS2 I.hAgreeWS I.hAgreeRun I.hdec + have hpl := hshape h60 h69 + obtain ⟨hB, hcur⟩ := Sched.BodyOKA.ofVcpuPlain J hx hpl hperf + refine ⟨core.result.Result.Ok (), I.s3, I.a3, σ3, by rw [I.hcap]; exact I.hdec, + ⟨x, inv, hpl, hx, hpin, hst, hperf.imp_right fun h => ⟨trivial, h⟩⟩, + Sched.curW_keptA J.toJStage3A J3 hcur, A64RelS3.ofJ J3 hB, fun h => nomatch h⟩ + | setTcb I hl => + obtain ⟨x, inv, hw', Ac', σ3, hx, hpin, hst, -, J3, -, hsh, -⟩ := + J.vcpuDecodeA hA hM hPl hfm I.hP I.hAK I.hVR I.hS2R I.hCs I.hCsN I.h1 + J.toJStage3EntryA.curNotVmA I.hslot I.hcov I.hcap I.hvc I.hext I.hSC I.hfp I.hlbl I.hlen I.hargs I.hex0 + I.hNoZomb I.hNoS2 I.hAgreeWS I.hAgreeRun I.hdec + obtain ⟨⟨v, t, hinv⟩, hp⟩ := hsh hl + subst hinv + obtain ⟨hB, hcur⟩ := Sched.BodyOKA.ofVcpuSetTcb J hx hp + refine ⟨core.result.Result.Ok (), I.s3, I.a3, σ3, by rw [I.hcap]; exact I.hdec, + ⟨x, _, ⟨v, t, rfl⟩, hx, hpin, hst, Or.inl hp⟩, + Sched.curW_keptA J.toJStage3A J3 hcur, A64RelS3.ofJ J3 hB, fun h => nomatch h⟩ + | run I hl => + obtain ⟨x, inv, hw', Ac', σ3, hx, hpin, hst, -, J3, -, -, hrn⟩ := + J.vcpuDecodeA hA hM hPl hfm I.hP I.hAK I.hVR I.hS2R I.hCs I.hCsN I.h1 + J.toJStage3EntryA.curNotVmA I.hslot I.hcov I.hcap I.hvc I.hext I.hSC I.hfp I.hlbl I.hlen I.hargs I.hex0 + I.hNoZomb I.hNoS2 I.hAgreeWS I.hAgreeRun I.hdec + obtain ⟨⟨v, fr, o, i, p, g, hinv⟩, hp⟩ := hrn hl + subst hinv + have ct : σ.curThread = specPtr (L.curW s) := J.j.1.sch.curThread + rw [← ct] at hp hpin + obtain ⟨hB, hcur⟩ := Sched.BodyOKA.ofVcpuRun J hx hp + refine ⟨core.result.Result.Ok (), I.s3, I.a3, σ3, by rw [I.hcap]; exact I.hdec, + ⟨x, _, ⟨v, fr, o, i, p, g, rfl⟩, hx, hpin, hst, Or.inl hp⟩, + Sched.curW_keptA J.toJStage3A J3 hcur, A64RelS3.ofJ J3 hB, fun h => nomatch h⟩ + +open Fl4Spec Fl4SpecA64 Classical in +/-- **`InvSpecA` from a pinned operation and a spec outcome** (`InvSpecA.ofPin` with any `PerfSpecA`). -/ +theorem InvSpecA.ofPinPerfA {th : TypedHeapA} {kb : U64} (hP : PlaceA th kb) {σ σ1 σ' : State Unit} {slot : U64} + {call blk don : Bool} {oper : Fl4Invocation} (pin : DecPinA call th σ slot oper) + {mi : MessageInfo} {sl : CslotPtr} {c : Cap} {excaps : List (Cap × CslotPtr)} {buffer : Option ObjRef} + {args : List Data} (F : FrontPinA th σ slot mi sl c excaps buffer args) (cptr : CapRef) + (hset : setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False)) + (hperf : PerfSpecA blk call don true oper σ.curThread σ1 σ') : + InvSpecA call blk don true mi.miLabel args cptr sl c excaps buffer σ σ' := by + obtain ⟨mi0, sl0, c0, ex0, b0, a0, F0, hdec⟩ := pin + have e1 := single_fstA (F0.hmi.symm.trans F.hmi) + subst e1 + have e2 := cslotAddrA_inj hP F0.habs F.habs (F0.hslot.symm.trans F.hslot) + subst e2 + have e3 := single_fstA (F0.hcap.symm.trans F.hcap) + have e4 := single_fstA (F0.hbuf.symm.trans F.hbuf) + subst e3 e4 + have e5 := Except.ok.inj (single_fstA (F0.hex.symm.trans F.hex)) + have e6 := single_fstA (F0.hargs.symm.trans F.hargs) + subst e5 e6 + exact ⟨oper, σ1, hdec cptr, hset, hperf, fun h => by cases h⟩ + +/-- The VCPU front-end pin is the dispatch front-end pin. -/ +theorem VcpuDecPinA.toDecPinA {call : Bool} {th : TypedHeapA} {σ : State Unit} {slot : U64} {oper : Fl4Invocation} + (h : VcpuDecPinA call th σ slot oper) : DecPinA call th σ slot oper := by + obtain ⟨mi, sl, c, ex, b, a, F, hd⟩ := h + exact ⟨mi, sl, c, ex, b, a, ⟨F.hmi, F.hslot, F.habs, F.hcap, F.hbuf, F.hex, F.hargs⟩, hd⟩ + +theorem VcpuInA.done_true {ar : machine.Arch A} {AR : A → HwRootA → Prop} {Z : XSetsA} {OS : Set U64} {n : Nat} + {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {label length slot : U64} {blk call don fp : Bool} {buf : U64} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + (I : VcpuInA st AR Z OS n ar s a σ label length slot blk call don fp buf extra) + {r : core.result.Result Unit error.Exc} {s' : S} {a' : A} + (hd : syscall.decode_invocation st ar s a label length slot (capA st s slot) blk call don fp buf extra = + ok (r, s', a')) + {done : Bool} (hdone : (done = true ∧ r = .Ok ()) ∨ (done = false ∧ r = .Err error.Exc.Preempted)) : + done = true := by + rw [I.hcap] at hd + have hr : r = core.result.Result.Ok () := (Prod.mk.inj (okinj (hd.symm.trans I.hdec))).1 + rcases hdone with ⟨h2, -⟩ | ⟨-, h2⟩ + · exact h2 + · rw [hr] at h2; cases h2 + +open Fl4Spec Fl4SpecA64 Classical in +/-- **A VCPU invocation whose perform returns `[]`** (`InvSpecA`, the first `PerfSpecA` disjunct). -/ +theorem VcpuInv.invSpec0 (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s a σ m) + {call blk don : Bool} {slot : U64} {σ1 σ' : State Unit} {inv : Fl4VcpuInvocation} + (hpin : VcpuDecPinA call Z.th σ slot (.invokeArchObject (.vcpu inv))) + (hst : setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False)) + (hp : (.ok [], σ') ∈ ((fl4ArchPerformInvocation (Z := Unit) (.vcpu inv)).run σ1).1) + {mi : MessageInfo} {sl : CslotPtr} {c : Cap} {excaps : List (Cap × CslotPtr)} {buffer : Option ObjRef} + {args : List Data} (F : FrontPinA Z.th σ slot mi sl c excaps buffer args) (cptr : CapRef) : + InvSpecA call blk don true mi.miLabel args cptr sl c excaps buffer σ σ' := by + have run : fl4PerformInvocation (Z := Unit) blk call don (.invokeArchObject (.vcpu inv)) = + fl4ArchPerformInvocation (.vcpu inv) := by + cases blk <;> cases call <;> cases don <;> rfl + refine InvSpecA.ofPinPerfA J.j.1.place hpin.toDecPinA F cptr hst ?_ + unfold PerfSpecA + rw [if_pos rfl, run] + exact ⟨[], Or.inl ⟨hp, fun _ _ _ => rfl⟩⟩ + +/-- **The VCPU group's `InvSpecA`** (the shape of `JSyscallA.invocationA`'s `hScope`). -/ +theorem VcpuScopeA.invSpecA (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s a σ m) + {lbl len slot : U64} {blk call don fp : Bool} {buf : U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} + {Out : Fl4SpecA64.State Unit → Prop} + (h : VcpuScopeA st AR Z OS n ar s a σ lbl len slot blk call don fp buf extra Out) + {r : core.result.Result Unit error.Exc} {s' : S} {a' : A} {σ' : State Unit} + (hd : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don fp buf extra = + ok (r, s', a')) (hOut : Out σ') + {done : Bool} (hdone : (done = true ∧ r = .Ok ()) ∨ (done = false ∧ r = .Err error.Exc.Preempted)) + {mi : MessageInfo} {sl : CslotPtr} {c : Cap} {excaps : List (Cap × CslotPtr)} {buffer : Option ObjRef} + {args : List Data} (F : FrontPinA Z.th σ slot mi sl c excaps buffer args) (cptr : CapRef) : + InvSpecA call blk don done mi.miLabel args cptr sl c excaps buffer σ σ' := by + cases h with + | plain I h60 h69 => + have hdn := I.done_true hd hdone + subst hdn + obtain ⟨x, inv, hpl, hx, hpin, hst, hperf⟩ := hOut + rcases hperf with hp | ⟨-, σ2, ws, hp, hrep⟩ + · exact VcpuInv.invSpec0 J hpin hst hp F cptr + · have run : fl4PerformInvocation (Z := Unit) blk call don (.invokeArchObject (.vcpu inv)) = + fl4ArchPerformInvocation (.vcpu inv) := by + cases blk <;> cases call <;> cases don <;> rfl + obtain ⟨x2, hx2, hs2⟩ := vcpuPlain_restartAt hx hpl hp hrep + have getSt : getThreadState (Z := Unit) σ.curThread σ2 = ({(.restart, σ2)}, False) := by + show threadGet (Z := Unit) Tcb.tcbState σ.curThread σ2 = _ + rw [threadGet_eqA Tcb.tcbState hx2, hs2] + refine InvSpecA.ofPinPerfA J.j.1.place hpin.toDecPinA F cptr hst ?_ + unfold PerfSpecA + rw [if_pos rfl, run] + refine ⟨ws, Or.inr ⟨σ2, hp, ?_⟩⟩ + unfold epiSpecA + rw [nd_bind_oneA getSt] + exact hrep + | setTcb I hl => + have hdn := I.done_true hd hdone + subst hdn + obtain ⟨x, inv, -, hx, hpin, hst, hperf⟩ := hOut + rcases hperf with hp | ⟨h, -⟩ + · exact VcpuInv.invSpec0 J hpin hst hp F cptr + · exact h.elim + | run I hl => + have hdn := I.done_true hd hdone + subst hdn + obtain ⟨x, inv, -, hx, hpin, hst, hperf⟩ := hOut + rcases hperf with hp | ⟨h, -⟩ + · exact VcpuInv.invSpec0 J hpin hst hp F cptr + · exact h.elim + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3DispVsA.lean b/proofs/Fl4ProofsA64/P3/Stage3DispVsA.lean new file mode 100644 index 000000000..83e448402 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3DispVsA.lean @@ -0,0 +1,526 @@ +/- +P3 (aarch64, A64-P3 Stage 3, T3 dispatch): **the VSpace dispatch group** (`Stage3DispBaseA`). One constructor per +lifted arm; its premises are the arm's (`JSyscallA.`): the cap of the invoked slot, the arm's conditions and +FL4's decode-site results (`hK` / `hT` come from the record: `ksbAligned`, `offAsE`). Generated (WS/handoff/tools/gen_t3disp.py). + +Pending lift (not in scope): page / VSpace flush, GetAddress, TestAndClearAccessed, the `Err` returns. +Deferred (backlog): PT map into a high window or Stage-2, frame map Stage-2, the T2 unmap shrink, `derive_subframe`. +-/ +import Fl4ProofsA64.P3.Stage3OffAsEA +import Fl4ProofsA64.P3.Stage3T3ArmsA +import Fl4ProofsA64.P3.Stage3T3ArmHiA +import Fl4ProofsA64.P3.Stage3DispBaseA +import Fl4ProofsA64.P3.VsArmA +import Fl4ProofsA64.P3.Stage3T3DecEqA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} (st : machine.Store S) [L : LawfulStore st] {A : Type} + +set_option maxHeartbeats 4000000 in +/-- **The Vs arms in scope** (module docstring), indexed by the invocation (label, length, slot, flags, buffer, extra +slots) and the spec outcome. -/ +inductive VsScopeA (AR : A → HwRootA → Prop) (Z : XSetsA) (H : Heap) (OS U Ac Pp Tb : Set U64) + (Lv : U64 → Nat) (ia ie : Nat) (N E Rp : Set U64) (Bf : Set (U64 × Fl4SpecA64.Word)) (stack : U64 → List U64) + (κ : U64 → U64) (n : Nat) (ar : machine.Arch A) (nA : U64) (s : S) (a : A) (σ : Fl4SpecA64.State Unit) + (m : Fl4SpecA64.McState) : + U64 → U64 → U64 → Bool → Bool → Bool → Bool → U64 → Aeneas.Std.Array obj.Slot 3#usize → + (Fl4SpecA64.State Unit → Prop) → Prop + | frameMapS1 + {s3 : S} + {a3 : A} + {fp asid0 va0 rt : U64} + {sz : capability.FrameSize} + {dev mp : Bool} + {bB bD bF : Bool} + (K : ArchKeepsA ar AR) + (hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM) + (cte : Fl4SpecA64.CslotPtr) + (hab : AbsSlotA Z.th cte) + (hcap : capAtA (L.mem s) (cslotAddrA cte) = .Frame fp sz rt dev mp false asid0 va0) + (FP : FrPlA sz fp mp asid0) + (slbl : Fl4SpecA64.Data) + (hl : Fl4SpecA64.invocationType slbl = .archInvocationLabel .armPageMap) + (length buf : U64) + (args : List U64) + (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) + (call : Bool) + (extra : Aeneas.Std.Array obj.Slot 3#usize) + (x0 : U64) + (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) + (exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) + (hex : ExtraRelA Z.th (L.mem s) x0 (exs.map Prod.fst)) + (hS1 : ∀ p l mp' asid va, capAtA (L.mem s) x0 ≠ .Ept p l mp' asid va) + (hHi : ∀ p mp' asid, capAtA (L.mem s) x0 ≠ .HighVSpace p mp' asid) + (hdec : vspace.decode_frame st ar s a fl4_abi.label.ARMPageMap length (cslotAddrA cte) + (.Frame fp sz rt dev mp false asid0 va0) call buf extra = ok (core.result.Result.Ok (), s3, a3)) + (hcov : SlotCov st OS s (cslotAddrA cte)) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) + (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hpin : ∀ ivp, (Fl4SpecA64.decodeFrameInvocation (Z := Unit) slbl (args.map UScalar.bv) cte + (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev (mapDataA mp asid0 va0)) exs).run σ = ({(.ok (.invokePage ivp), σ)}, False) → + DecPinA call Z.th σ (cslotAddrA cte) (.invokeArchObject (.spec (.invokePage ivp)))) : + VsScopeA AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar nA s a σ m fl4_abi.label.ARMPageMap length (cslotAddrA cte) bB call bD bF buf extra + (fun σ' => ∃ (x : Fl4SpecA64.Tcb) (ivp : _), + σ.kheap σ.curThread = some (.tcb x) ∧ + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + (Fl4SpecA64.decodeFrameInvocation (Z := Unit) slbl (args.map UScalar.bv) cte + (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev (mapDataA mp asid0 va0)) exs).run σ = + ({(.ok (.invokePage ivp), σ)}, False) ∧ + (([] : List Fl4SpecA64.Data), σ') ∈ (Fl4SpecA64.performPageInvocation (Z := Unit) ivp + (stUpdA σ σ.curThread x .restart)).1) + | frameUnmapS1 + {bB bD bF : Bool} + (K : ArchKeepsA ar AR) + (hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM) + (hTO : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s q) + (length buf : U64) + (call : Bool) + (ex : Aeneas.Std.Array obj.Slot 3#usize) + (cte : Fl4SpecA64.CslotPtr) + (hab : AbsSlotA Z.th cte) + (fp rt asid va : U64) + (sz : capability.FrameSize) + (dev : Bool) + (h0 : asid ≠ 0#u64) + (hcap : capA st s (cslotAddrA cte) = .Frame fp sz rt dev true false asid va) + (hS1 : Fl4SpecA64.asidRootKind σ (BitVec.ofNat 16 asid.val) = none ∨ + Fl4SpecA64.asidRootKind σ (BitVec.ofNat 16 asid.val) = some .stage1) + (hcov : SlotCov st OS s (cslotAddrA cte)) + (hext : ∀ x ∈ ex.val, x ≠ 0#u64 → x ∈ OS) + (hSC : ∀ x ∈ ex.val, x ≠ 0#u64 → SlotCov st OS s x) + (hpin : DecPinA call Z.th σ (cslotAddrA cte) (.invokeArchObject (.spec (.invokePage (.pageUnmap (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev (some (BitVec.ofNat 16 asid.val, va.bv))) cte))))) : + VsScopeA AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar nA s a σ m fl4_abi.label.ARMPageUnmap length (cslotAddrA cte) bB call bD bF buf ex + (fun σ' => ∃ (x : Fl4SpecA64.Tcb), + σ.kheap σ.curThread = some (.tcb x) ∧ + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + (([] : List Fl4SpecA64.Data), σ') ∈ (Fl4SpecA64.performPageInvocation (Z := Unit) + (.pageUnmap (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev + (some (BitVec.ofNat 16 asid.val, va.bv))) cte) (stUpdA σ σ.curThread x .restart)).1) + | frameUnmapX + {bB bD bF : Bool} + (K : ArchKeepsA ar AR) + (hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM) + (hTO : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s q) + (hEO : ExtTabOffA (L := L) Z m s) + (hIpa : S2IpaE ar a) + (length buf : U64) + (call : Bool) + (ex : Aeneas.Std.Array obj.Slot 3#usize) + (cte : Fl4SpecA64.CslotPtr) + (hab : AbsSlotA Z.th cte) + (fp rt asid va : U64) + (sz : capability.FrameSize) + (dev isEpt : Bool) + (h0 : asid ≠ 0#u64) + (hcap : capA st s (cslotAddrA cte) = .Frame fp sz rt dev true isEpt asid va) + (k : Fl4SpecA64.ExtRootKind) + (hkA : isEpt = false → Fl4SpecA64.asidRootKind σ (BitVec.ofNat 16 asid.val) = some k ∧ k ≠ .stage1) + (hcov : SlotCov st OS s (cslotAddrA cte)) + (hext : ∀ x ∈ ex.val, x ≠ 0#u64 → x ∈ OS) + (hSC : ∀ x ∈ ex.val, x ≠ 0#u64 → SlotCov st OS s x) + (hpin : DecPinA call Z.th σ (cslotAddrA cte) (.invokeArchObject (.extVSpace (.pageUnmap k (absFrameE isEpt (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev + (some (BitVec.ofNat 16 asid.val, va.bv))) cte)))) : + VsScopeA AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar nA s a σ m fl4_abi.label.ARMPageUnmap length (cslotAddrA cte) bB call bD bF buf ex + (fun σ' => ∃ (x : Fl4SpecA64.Tcb), + σ.kheap σ.curThread = some (.tcb x) ∧ + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + (([] : List Fl4SpecA64.Data), σ') ∈ (Fl4SpecA64.performExtVSpace (Z := Unit) (.pageUnmap k + (absFrameE isEpt (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev + (some (BitVec.ofNat 16 asid.val, va.bv))) cte) (stUpdA σ σ.curThread x .restart)).1) + | frameUnmapZ + {bB bD bF : Bool} + (hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM) + (length buf : U64) + (call : Bool) + (ex : Aeneas.Std.Array obj.Slot 3#usize) + (cte : Fl4SpecA64.CslotPtr) + (hab : AbsSlotA Z.th cte) + (fp rt va : U64) + (sz : capability.FrameSize) + (dev : Bool) + (hcap : capA st s (cslotAddrA cte) = .Frame fp sz rt dev true false 0#u64 va) + (hcov : SlotCov st OS s (cslotAddrA cte)) + (hext : ∀ x ∈ ex.val, x ≠ 0#u64 → x ∈ OS) + (hSC : ∀ x ∈ ex.val, x ≠ 0#u64 → SlotCov st OS s x) + (hpin : DecPinA call Z.th σ (cslotAddrA cte) (.invokeArchObject (.spec (.invokePage (.pageUnmap (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev (some (BitVec.ofNat 16 (0#u64 : U64).val, va.bv))) cte))))) : + VsScopeA AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar nA s a σ m fl4_abi.label.ARMPageUnmap length (cslotAddrA cte) bB call bD bF buf ex + (fun σ' => ∃ (x : Fl4SpecA64.Tcb), + σ.kheap σ.curThread = some (.tcb x) ∧ + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + (([] : List Fl4SpecA64.Data), σ') ∈ (Fl4SpecA64.performPageInvocation (Z := Unit) + (.pageUnmap (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev + (some (BitVec.ofNat 16 (0#u64 : U64).val, va.bv))) cte) (stUpdA σ σ.curThread x .restart)).1) + | frameUnmapS2Z + {bB bD bF : Bool} + (hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM) + (length buf : U64) + (call : Bool) + (ex : Aeneas.Std.Array obj.Slot 3#usize) + (cte : Fl4SpecA64.CslotPtr) + (hab : AbsSlotA Z.th cte) + (fp rt va : U64) + (sz : capability.FrameSize) + (dev : Bool) + (hcap : capA st s (cslotAddrA cte) = .Frame fp sz rt dev true true 0#u64 va) + (hcov : SlotCov st OS s (cslotAddrA cte)) + (hext : ∀ x ∈ ex.val, x ≠ 0#u64 → x ∈ OS) + (hSC : ∀ x ∈ ex.val, x ≠ 0#u64 → SlotCov st OS s x) + (hpin : DecPinA call Z.th σ (cslotAddrA cte) (.invokeArchObject (.extVSpace (.pageUnmap .stage2 (.ext (.stage2FrameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev + (some (BitVec.ofNat 16 (0#u64 : U64).val, va.bv)))) cte)))) : + VsScopeA AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar nA s a σ m fl4_abi.label.ARMPageUnmap length (cslotAddrA cte) bB call bD bF buf ex + (fun σ' => ∃ (x : Fl4SpecA64.Tcb), + σ.kheap σ.curThread = some (.tcb x) ∧ + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + (([] : List Fl4SpecA64.Data), σ') ∈ (Fl4SpecA64.performExtVSpace (Z := Unit) + (.pageUnmap .stage2 (.ext (.stage2FrameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev + (some (BitVec.ofNat 16 (0#u64 : U64).val, va.bv)))) cte) (stUpdA σ σ.curThread x .restart)).1) + | frameUnmapU + {bB bD bF : Bool} + (hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM) + (length buf : U64) + (call : Bool) + (ex : Aeneas.Std.Array obj.Slot 3#usize) + (cte : Fl4SpecA64.CslotPtr) + (hab : AbsSlotA Z.th cte) + (fp rt asid va : U64) + (sz : capability.FrameSize) + (dev isEpt : Bool) + (hcap : capA st s (cslotAddrA cte) = .Frame fp sz rt dev false isEpt asid va) + (hcov : SlotCov st OS s (cslotAddrA cte)) + (hext : ∀ x ∈ ex.val, x ≠ 0#u64 → x ∈ OS) + (hSC : ∀ x ∈ ex.val, x ≠ 0#u64 → SlotCov st OS s x) + (hpin : DecPinA call Z.th σ (cslotAddrA cte) (.invokeArchObject (.spec (.invokePage (.pageUnmap (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev none) cte))))) + (hcs : ∀ x, σ.kheap σ.curThread = some (.tcb x) → + Fl4SpecA64.getCap (Z := Unit) cte (stUpdA σ σ.curThread x .restart) = + ({(.archObjectCap (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev none), + stUpdA σ σ.curThread x .restart)}, False)) : + VsScopeA AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar nA s a σ m fl4_abi.label.ARMPageUnmap length (cslotAddrA cte) bB call bD bF buf ex + (fun σ' => ∃ (x : Fl4SpecA64.Tcb), + σ.kheap σ.curThread = some (.tcb x) ∧ + ((), σ') ∈ (setCap (Z := Unit) (.archObjectCap (.frameCap (specPtr fp) (vmRightsOfA rt) + (frameSizeOfA sz) dev none)) cte (stUpdA σ σ.curThread x .restart)).1) + | ptMapS1 + {s3 : S} + {a3 : A} + {ptr asid0 va0 : U64} + {bB bC bD bF : Bool} + (hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM) + (cte : Fl4SpecA64.CslotPtr) + (hab : AbsSlotA Z.th cte) + (hcap : capAtA (L.mem s) (cslotAddrA cte) = .PageTable ptr false asid0 va0) + (UP : UnmPtA Z.S1 Z.Y OS H.tcbs (L.mem s) σ ptr) + (hS2 : ptr ∉ S2PgE Z.X Z.T2) + (hY : ptr ∉ Z.Y) + (slbl : Fl4SpecA64.Data) + (hl : Fl4SpecA64.invocationType slbl = .archInvocationLabel .armPageTableMap) + (length buf : U64) + (args : List U64) + (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) + (extra : Aeneas.Std.Array obj.Slot 3#usize) + (x0 : U64) + (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) + (exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) + (hex : ExtraRelA Z.th (L.mem s) x0 (exs.map Prod.fst)) + (hS1 : ∀ p l mp' asid va, capAtA (L.mem s) x0 ≠ .Ept p l mp' asid va) + (hHi : ∀ p mp' asid, capAtA (L.mem s) x0 ≠ .HighVSpace p mp' asid) + (hdec : vspace.decode_page_table st ar s a fl4_abi.label.ARMPageTableMap length (cslotAddrA cte) + (.PageTable ptr false asid0 va0) buf extra = ok (core.result.Result.Ok (), s3, a3)) + (hcov : SlotCov st OS s (cslotAddrA cte)) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) + (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hpin : ∀ ivp, (Fl4SpecA64.decodePageTableInvocation (Z := Unit) slbl (args.map UScalar.bv) cte + (.pageTableCap (specPtr ptr) .normalPtT (mapDataA false asid0 va0)) exs).run σ = ({(.ok (.invokePageTable ivp), σ)}, False) → + DecPinA bC Z.th σ (cslotAddrA cte) (.invokeArchObject (.spec (.invokePageTable ivp)))) : + VsScopeA AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar nA s a σ m fl4_abi.label.ARMPageTableMap length (cslotAddrA cte) bB bC bD bF buf extra + (fun σ' => ∃ (x : Fl4SpecA64.Tcb) (ivp : Fl4SpecA64.PageTableInvocation), + σ.kheap σ.curThread = some (.tcb x) ∧ + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + (Fl4SpecA64.decodePageTableInvocation (Z := Unit) slbl (args.map UScalar.bv) cte + (.pageTableCap (specPtr ptr) .normalPtT (mapDataA false asid0 va0)) exs).run σ = + ({(.ok (.invokePageTable ivp), σ)}, False) ∧ + ((), σ') ∈ (Fl4SpecA64.performPageTableInvocation (Z := Unit) ivp (stUpdA σ σ.curThread x .restart)).1) + | ptUnmapS1 + {bB bC bD bF : Bool} + (K : ArchKeepsA ar AR) + (hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM) + (hTO : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s q) + (hEO : ExtTabOffA (L := L) Z m s) + (length buf : U64) + (ex : Aeneas.Std.Array obj.Slot 3#usize) + (cte : Fl4SpecA64.CslotPtr) + (hab : AbsSlotA Z.th cte) + (ptr asid va : U64) + (hpS : ptr ∈ Z.S1 ∧ 1 ≤ Z.Lv ptr) + (hcap : capA st s (cslotAddrA cte) = .PageTable ptr true asid va) + (hf : cdt.is_final_capability st s (cslotAddrA cte) = ok true) + (hS1 : Fl4SpecA64.asidRootKind σ (BitVec.ofNat 16 asid.val) = none ∨ + Fl4SpecA64.asidRootKind σ (BitVec.ofNat 16 asid.val) = some .stage1) + (hcov : SlotCov st OS s (cslotAddrA cte)) + (hext : ∀ x ∈ ex.val, x ≠ 0#u64 → x ∈ OS) + (hSC : ∀ x ∈ ex.val, x ≠ 0#u64 → SlotCov st OS s x) + (hpin : DecPinA bC Z.th σ (cslotAddrA cte) (.invokeArchObject (.spec (.invokePageTable (.pageTableUnmap (.pageTableCap (specPtr ptr) .normalPtT (some (BitVec.ofNat 16 asid.val, va.bv))) cte))))) : + VsScopeA AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar nA s a σ m fl4_abi.label.ARMPageTableUnmap length (cslotAddrA cte) bB bC bD bF buf ex + (fun σ' => ∃ (x : Fl4SpecA64.Tcb), + σ.kheap σ.curThread = some (.tcb x) ∧ + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + ((), σ') ∈ (Fl4SpecA64.performPageTableInvocation (Z := Unit) (.pageTableUnmap (.pageTableCap (specPtr ptr) + .normalPtT (some (BitVec.ofNat 16 asid.val, va.bv))) cte) (stUpdA σ σ.curThread x .restart)).1) + | ptUnmapX + {bB bC bD bF : Bool} + (K : ArchKeepsA ar AR) + (hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM) + (hTO : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s q) + (hEO : ExtTabOffA (L := L) Z m s) + (hIpa : S2IpaE ar a) + (length buf : U64) + (ex : Aeneas.Std.Array obj.Slot 3#usize) + (cte : Fl4SpecA64.CslotPtr) + (hab : AbsSlotA Z.th cte) + (ptr asid va : U64) + (hpS : ptr ∈ Z.S1 ∧ 1 ≤ Z.Lv ptr) + (hcap : capA st s (cslotAddrA cte) = .PageTable ptr true asid va) + (hf : cdt.is_final_capability st s (cslotAddrA cte) = ok true) + (k : Fl4SpecA64.ExtRootKind) + (hkR : Fl4SpecA64.asidRootKind σ (BitVec.ofNat 16 asid.val) = some k) + (hkR1 : k ≠ .stage1) + (hcov : SlotCov st OS s (cslotAddrA cte)) + (hext : ∀ x ∈ ex.val, x ≠ 0#u64 → x ∈ OS) + (hSC : ∀ x ∈ ex.val, x ≠ 0#u64 → SlotCov st OS s x) + (hpin : DecPinA bC Z.th σ (cslotAddrA cte) (.invokeArchObject (.extVSpace (.tableUnmap k (.pageTableCap (specPtr ptr) .normalPtT (some (BitVec.ofNat 16 asid.val, va.bv))) cte)))) : + VsScopeA AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar nA s a σ m fl4_abi.label.ARMPageTableUnmap length (cslotAddrA cte) bB bC bD bF buf ex + (fun σ' => ∃ (x : Fl4SpecA64.Tcb), + σ.kheap σ.curThread = some (.tcb x) ∧ + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + (([] : List Fl4SpecA64.Data), σ') ∈ (Fl4SpecA64.performExtVSpace (Z := Unit) (.tableUnmap k (.pageTableCap + (specPtr ptr) .normalPtT (some (BitVec.ofNat 16 asid.val, va.bv))) cte) (stUpdA σ σ.curThread x .restart)).1) + | ptUnmapU + {bB bC bD bF : Bool} + (hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM) + (length buf : U64) + (ex : Aeneas.Std.Array obj.Slot 3#usize) + (cte : Fl4SpecA64.CslotPtr) + (hab : AbsSlotA Z.th cte) + (ptr asid va : U64) + (hcap : capA st s (cslotAddrA cte) = .PageTable ptr false asid va) + (hf : cdt.is_final_capability st s (cslotAddrA cte) = ok true) + (hcov : SlotCov st OS s (cslotAddrA cte)) + (hext : ∀ x ∈ ex.val, x ≠ 0#u64 → x ∈ OS) + (hSC : ∀ x ∈ ex.val, x ≠ 0#u64 → SlotCov st OS s x) + (hpin : DecPinA bC Z.th σ (cslotAddrA cte) (.invokeArchObject (.spec (.invokePageTable (.pageTableUnmap (.pageTableCap (specPtr ptr) .normalPtT none) cte))))) : + VsScopeA AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar nA s a σ m fl4_abi.label.ARMPageTableUnmap length (cslotAddrA cte) bB bC bD bF buf ex + (fun σ' => ∃ (x : Fl4SpecA64.Tcb), + σ.kheap σ.curThread = some (.tcb x) ∧ + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + ((), σ') ∈ (Fl4SpecA64.performPageTableInvocation (Z := Unit) + (.pageTableUnmap (.pageTableCap (specPtr ptr) .normalPtT none) cte) (stUpdA σ σ.curThread x .restart)).1) + | asidAssign + (hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM) + (cte : Fl4SpecA64.CslotPtr) + (hab : AbsSlotA Z.th cte) + (p base : U64) + (hpool : capA st s (cslotAddrA cte) = .AsidPool p base) + (length buf : U64) + (block call cd fp : Bool) + (ex : Aeneas.Std.Array obj.Slot 3#usize) + (dst : Fl4SpecA64.CslotPtr) + (hd0 : AbsSlotA Z.th dst) + (hex : vspace.extra ex 0#usize = ok (some (cslotAddrA dst))) + (k : Fl4SpecA64.ExtRootKind) + (root oldAsid oldVa : U64) + (hcap : capA st s (cslotAddrA dst) = assignUnmappedA k root oldAsid oldVa) + (htab : ∀ hi : U64, hi.val = base.val / 512 → L.mem s (atAF (L.ksb s) hi) = p) + (hp0 : p ≠ 0#u64) + (i : U64) + (hsearch : vspace.decode_loop1 st s base p 0#u64 = ok i) + (hfull : i ≠ 512#u64) + (hcov : SlotCov st OS s (cslotAddrA cte)) + (hext : ∀ x ∈ ex.val, x ≠ 0#u64 → x ∈ OS) + (hSC : ∀ x ∈ ex.val, x ≠ 0#u64 → SlotCov st OS s x) + (hpin : DecPinA call Z.th σ (cslotAddrA cte) (.invokeArchObject (assignInvA k (specPtr p) dst (asidOfA (wa base i))))) : + VsScopeA AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar nA s a σ m fl4_abi.label.ARMASIDPoolAssign length (cslotAddrA cte) block call cd fp buf ex + (fun σ' => ∃ (x : Fl4SpecA64.Tcb), + σ.kheap σ.curThread = some (.tcb x) ∧ + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + (∀ (calling : Bool) (args : List Fl4SpecA64.Data) (xSlot : Fl4SpecA64.CapRef) (c0 : Fl4SpecA64.CslotPtr) + (rest : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)), + (.ok (assignInvA k (specPtr p) dst (asidOfA (wa base i))), σ) ∈ ((fl4ArchDecodeInvocation (Z := Unit) + calling (archL .armASIDPoolAssign) args xSlot c0 (.asidPoolCap (specPtr p) (asidOfA base)) + ((.archObjectCap (assignRootCapA k (specPtr root) none), dst) :: rest)).run σ).1) ∧ + (.ok ([] : List Fl4SpecA64.Data), σ') ∈ ((fl4ArchPerformInvocation (Z := Unit) + (assignInvA k (specPtr p) dst (asidOfA (wa base i)))).run (stUpdA σ σ.curThread x .restart)).1) + | frameMapHi + {x0 : U64} + {D : Set (Except SyscallError Fl4ArchInvocation × State Unit) × Prop} + {dc : capability.Cap} + {ex : Aeneas.Std.Array obj.Slot 3#usize} + {s3 : S} + {a3 : A} + {callD : Bool} + {lenD bufD : U64} + {rr : capability.Rights} + {bB bD bF : Bool} + (K : ArchKeepsA ar AR) + (hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM) + (hTO : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s q) + (hEO : ExtTabOffA (L := L) Z m s) + (cte : Fl4SpecA64.CslotPtr) + (hab : AbsSlotA Z.th cte) + (fp rt asid va : U64) + (sz : capability.FrameSize) + (dev mp : Bool) + (hcap : capAtA (L.mem s) (cslotAddrA cte) = .Frame fp sz rt dev mp false asid va) + (FP : FrPlA sz fp mp asid) + (p1 asid1 : U64) + (mp1 : Bool) + (h16 : asid1.val < 2 ^ 16) + (hx0 : x0 ∈ OS) + (hx0c : capA st s x0 = .HighVSpace p1 mp1 asid1) + (vaddr rightsMask vm attr : U64) + (hvmEq : vmRightsOfA vm = maskVmRights (vmRightsOfA rt) (dataToRights rightsMask.bv)) + (hD : D = (NonDetE.liftME Fl4ArchInvocation.extVSpace + (decodeHighPageMap (Z := Unit) cte (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev + (mapDataA mp asid va)) (extRootCapA .high (specPtr p1) (asidDataA mp1 asid1)) + vaddr.bv rightsMask.bv attr.bv)).run σ) + (d : DecPre st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits U Ac s dc ex) + (hres : vspace.decode_high_page_map st ar s a (cslotAddrA cte) + { ptr := fp, size := sz, rights := rt, is_device := dev, mapped := mp, is_ept := false, asid := asid, vaddr := va } + (.HighVSpace p1 mp1 asid1) vaddr vm attr = ok (core.result.Result.Ok (), s3, a3)) + (hcov : SlotCov st OS s (cslotAddrA cte)) + (hext : ∀ x ∈ ex.val, x ≠ 0#u64 → x ∈ OS) + (hSC : ∀ x ∈ ex.val, x ≠ 0#u64 → SlotCov st OS s x) + (hx : vspace.extra ex 0#usize = ok (some x0)) + (hlen : lenD ≥ 3#u64) + (h0 : msg.syscall_arg st s (L.curW s) bufD 0#u64 = ok vaddr) + (h1 : msg.syscall_arg st s (L.curW s) bufD 1#u64 = ok rightsMask) + (h2 : msg.syscall_arg st s (L.curW s) bufD 2#u64 = ok attr) + (hr : capability.Rights.from_word rightsMask = ok rr) + (hvm : capability.mask_vm_rights rt rr = ok vm) + (hpin : ∀ iv, (.ok (.extVSpace iv), σ) ∈ D.1 → DecPinA callD Z.th σ (cslotAddrA cte) (.invokeArchObject (.extVSpace iv))) : + VsScopeA AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar nA s a σ m fl4_abi.label.ARMPageMap lenD (cslotAddrA cte) bB callD bD bF bufD ex + (fun σ' => ∃ (x : Fl4SpecA64.Tcb) (iv : ExtVSpaceInvocation), + σ.kheap σ.curThread = some (.tcb x) ∧ + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + (.ok (.extVSpace iv), σ) ∈ D.1 ∧ + (([] : List Fl4SpecA64.Data), σ') ∈ + (Fl4SpecA64.performExtVSpace (Z := Unit) iv (stUpdA σ σ.curThread x .restart)).1) + +variable {st} + +set_option maxHeartbeats 8000000 in +/-- **Every Vs arm in scope runs (its decode succeeds) and keeps the record.** -/ +theorem VsScopeA.soundOk {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} + {κ : U64 → U64} {n : Nat} {ar : machine.Arch A} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + {lbl len slot : U64} {blk call don fp : Bool} {buf : U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} + {Out : Fl4SpecA64.State Unit → Prop} + (h : VsScopeA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar hPl.ipa s a σ m lbl len slot blk call don fp buf + extra Out) : + DispPostOkA st AR ar hPl.ipa n Z.Cs s a m lbl len slot blk call don fp buf extra Out := by + induction h with + | @frameMapS1 s3 a3 fp asid0 va0 rt sz dev mp bB bD bF K hnvm cte hab hcap FP slbl hl length buf args hlen hargs call extra x0 hx0 exs hex hS1 hHi hdec hcov hext hSC hpin => + obtain ⟨x, ivp, σ3, Ac', hc0, hc1, hc2, hc3, J3, K3⟩ := JSyscallA.frameMapS1A (J := J) (hA := hA) (hM := hM) (hPl := hPl) (hfm := hfm) (K := K) (hnvm := hnvm) (cte := cte) (hab := hab) (hcap := hcap) (FP := FP) (slbl := slbl) (hl := hl) (length := length) (buf := buf) (args := args) (hlen := hlen) (hargs := hargs) (call := call) (extra := extra) (x0 := x0) (hx0 := hx0) (exs := exs) (hex := hex) (hS1 := hS1) (hHi := hHi) (hdec := hdec) (hcov := hcov) (hext := hext) (hSC := hSC) + refine ⟨s3, a3, σ3, ?_, ⟨x, ivp, hc0, hc1, hc2, hc3⟩, curW_keptA J.toJStage3A J3 K3.cur, A64RelS3.ofJ J3 (Sched.BodyOKA.ofKeep J hc0 K3)⟩ + rw [show capA st s (cslotAddrA cte) = (.Frame fp sz rt dev mp false asid0 va0) from (capAtA_eq (st := st) _ _).symm.trans hcap] + exact (decInv_frameA ar s a _ length _ fp sz rt dev mp false asid0 va0 bB call bD bF buf extra).trans hdec + | @frameUnmapS1 bB bD bF K hnvm hTO length buf call ex cte hab fp rt asid va sz dev h0 hcap hS1 hcov hext hSC hpin => + obtain ⟨x, s3, a', σ3, Ac', hc0, hc1, hdec1, hc3, J3, K3⟩ := JSyscallA.frameUnmapS1A (J := J) (hA := hA) (hM := hM) (hPl := hPl) (hfm := hfm) (hK := J.toJStage3A.ksbAligned) (hT := J.toJStage3A.offAsE) (K := K) (hnvm := hnvm) (hTO := hTO) (length := length) (buf := buf) (call := call) (ex := ex) (cte := cte) (hab := hab) (fp := fp) (rt := rt) (asid := asid) (va := va) (sz := sz) (dev := dev) (h0 := h0) (hcap := hcap) (hS1 := hS1) (hcov := hcov) (hext := hext) (hSC := hSC) + refine ⟨s3, a', σ3, ?_, ⟨x, hc0, hc1, hc3⟩, curW_keptA J.toJStage3A J3 K3.cur, A64RelS3.ofJ J3 (Sched.BodyOKA.ofKeep J hc0 K3)⟩ + rw [show capA st s (cslotAddrA cte) = (.Frame fp sz rt dev true false asid va) from hcap] + exact (decInv_frameA ar s a _ length _ fp sz rt dev true false asid va bB call bD bF buf ex).trans hdec1 + | @frameUnmapX bB bD bF K hnvm hTO hEO hIpa length buf call ex cte hab fp rt asid va sz dev isEpt h0 hcap k hkA hcov hext hSC hpin => + obtain ⟨x, s3, a', σ3, Ac', hc0, hc1, hdec1, hc3, J3, K3⟩ := JSyscallA.frameUnmapXA (J := J) (hA := hA) (hM := hM) (hPl := hPl) (hfm := hfm) (hK := J.toJStage3A.ksbAligned) (hT := J.toJStage3A.offAsE) (K := K) (hnvm := hnvm) (hTO := hTO) (hEO := hEO) (hIpa := hIpa) (length := length) (buf := buf) (call := call) (ex := ex) (cte := cte) (hab := hab) (fp := fp) (rt := rt) (asid := asid) (va := va) (sz := sz) (dev := dev) (isEpt := isEpt) (h0 := h0) (hcap := hcap) (k := k) (hkA := hkA) (hcov := hcov) (hext := hext) (hSC := hSC) + refine ⟨s3, a', σ3, ?_, ⟨x, hc0, hc1, hc3⟩, curW_keptA J.toJStage3A J3 K3.cur, A64RelS3.ofJ J3 (Sched.BodyOKA.ofKeep J hc0 K3)⟩ + rw [show capA st s (cslotAddrA cte) = (.Frame fp sz rt dev true isEpt asid va) from hcap] + exact (decInv_frameA ar s a _ length _ fp sz rt dev true isEpt asid va bB call bD bF buf ex).trans hdec1 + | @frameUnmapZ bB bD bF hnvm length buf call ex cte hab fp rt va sz dev hcap hcov hext hSC hpin => + obtain ⟨x, s3, σ3, hc0, hc1, hdec1, hc3, J3, K3⟩ := JSyscallA.frameUnmapZA (J := J) (hA := hA) (hM := hM) (hPl := hPl) (hfm := hfm) (hnvm := hnvm) (length := length) (buf := buf) (call := call) (ex := ex) (cte := cte) (hab := hab) (fp := fp) (rt := rt) (va := va) (sz := sz) (dev := dev) (hcap := hcap) (hcov := hcov) (hext := hext) (hSC := hSC) + refine ⟨s3, a, σ3, ?_, ⟨x, hc0, hc1, hc3⟩, curW_keptA J.toJStage3A J3 K3.cur, A64RelS3.ofJ J3 (Sched.BodyOKA.ofKeep J hc0 K3)⟩ + rw [show capA st s (cslotAddrA cte) = (.Frame fp sz rt dev true false 0#u64 va) from hcap] + exact (decInv_frameA ar s a _ length _ fp sz rt dev true false 0#u64 va bB call bD bF buf ex).trans hdec1 + | @frameUnmapS2Z bB bD bF hnvm length buf call ex cte hab fp rt va sz dev hcap hcov hext hSC hpin => + obtain ⟨x, s3, σ3, hc0, hc1, hdec1, hc3, J3, K3⟩ := JSyscallA.frameUnmapS2ZA (J := J) (hA := hA) (hM := hM) (hPl := hPl) (hfm := hfm) (hnvm := hnvm) (length := length) (buf := buf) (call := call) (ex := ex) (cte := cte) (hab := hab) (fp := fp) (rt := rt) (va := va) (sz := sz) (dev := dev) (hcap := hcap) (hcov := hcov) (hext := hext) (hSC := hSC) + refine ⟨s3, a, σ3, ?_, ⟨x, hc0, hc1, hc3⟩, curW_keptA J.toJStage3A J3 K3.cur, A64RelS3.ofJ J3 (Sched.BodyOKA.ofKeep J hc0 K3)⟩ + rw [show capA st s (cslotAddrA cte) = (.Frame fp sz rt dev true true 0#u64 va) from hcap] + exact (decInv_frameA ar s a _ length _ fp sz rt dev true true 0#u64 va bB call bD bF buf ex).trans hdec1 + | @frameUnmapU bB bD bF hnvm length buf call ex cte hab fp rt asid va sz dev isEpt hcap hcov hext hSC hpin hcs => + obtain ⟨x, s2, σ2, hc0, hdec1, hc2, J3, K3⟩ := JSyscallA.frameUnmapUA (J := J) (hA := hA) (hM := hM) (hPl := hPl) (hfm := hfm) (hnvm := hnvm) (length := length) (buf := buf) (call := call) (ex := ex) (cte := cte) (hab := hab) (fp := fp) (rt := rt) (asid := asid) (va := va) (sz := sz) (dev := dev) (isEpt := isEpt) (hcap := hcap) (hcov := hcov) (hext := hext) (hSC := hSC) + refine ⟨s2, a, σ2, ?_, ⟨x, hc0, hc2⟩, curW_keptA J.toJStage3A J3 K3.cur, A64RelS3.ofJ J3 (Sched.BodyOKA.ofKeep J hc0 K3)⟩ + rw [show capA st s (cslotAddrA cte) = (.Frame fp sz rt dev false isEpt asid va) from hcap] + exact (decInv_frameA ar s a _ length _ fp sz rt dev false isEpt asid va bB call bD bF buf ex).trans hdec1 + | @ptMapS1 s3 a3 ptr asid0 va0 bB bC bD bF hnvm cte hab hcap UP hS2 hY slbl hl length buf args hlen hargs extra x0 hx0 exs hex hS1 hHi hdec hcov hext hSC hpin => + obtain ⟨x, ivp, σ3, Ac', Tb', Lv', nL, hc0, hc1, hc2, hc3, J3, K3⟩ := JSyscallA.ptMapS1A (J := J) (hA := hA) (hM := hM) (hPl := hPl) (hfm := hfm) (hnvm := hnvm) (cte := cte) (hab := hab) (hcap := hcap) (UP := UP) (hS2 := hS2) (hY := hY) (slbl := slbl) (hl := hl) (length := length) (buf := buf) (args := args) (hlen := hlen) (hargs := hargs) (extra := extra) (x0 := x0) (hx0 := hx0) (exs := exs) (hex := hex) (hS1 := hS1) (hHi := hHi) (hdec := hdec) (hcov := hcov) (hext := hext) (hSC := hSC) + refine ⟨s3, a3, σ3, ?_, ⟨x, ivp, hc0, hc1, hc2, hc3⟩, curW_keptA J.toJStage3A J3 K3.cur, A64RelS3.ofJ J3 (Sched.BodyOKA.ofKeep J hc0 K3)⟩ + rw [show capA st s (cslotAddrA cte) = (.PageTable ptr false asid0 va0) from (capAtA_eq (st := st) _ _).symm.trans hcap] + exact (decInv_ptA ar s a _ length _ ptr false asid0 va0 bB bC bD bF buf extra).trans hdec + | @ptUnmapS1 bB bC bD bF K hnvm hTO hEO length buf ex cte hab ptr asid va hpS hcap hf hS1 hcov hext hSC hpin => + obtain ⟨x, s3, a', σ3, Ac', hc0, hc1, hdec1, hc3, J3, K3⟩ := JSyscallA.ptUnmapS1A (J := J) (hA := hA) (hM := hM) (hPl := hPl) (hfm := hfm) (hK := J.toJStage3A.ksbAligned) (hT := J.toJStage3A.offAsE) (K := K) (hnvm := hnvm) (hTO := hTO) (hEO := hEO) (length := length) (buf := buf) (ex := ex) (cte := cte) (hab := hab) (ptr := ptr) (asid := asid) (va := va) (hpS := hpS) (hcap := hcap) (hf := hf) (hS1 := hS1) (hcov := hcov) (hext := hext) (hSC := hSC) + refine ⟨s3, a', σ3, ?_, ⟨x, hc0, hc1, hc3⟩, curW_keptA J.toJStage3A J3 K3.cur, A64RelS3.ofJ J3 (Sched.BodyOKA.ofKeep J hc0 K3)⟩ + rw [show capA st s (cslotAddrA cte) = (.PageTable ptr true asid va) from hcap] + exact (decInv_ptA ar s a _ length _ ptr true asid va bB bC bD bF buf ex).trans hdec1 + | @ptUnmapX bB bC bD bF K hnvm hTO hEO hIpa length buf ex cte hab ptr asid va hpS hcap hf k hkR hkR1 hcov hext hSC hpin => + obtain ⟨x, s3, a', σ3, Ac', hc0, hc1, hdec1, hc3, J3, K3⟩ := JSyscallA.ptUnmapXA (J := J) (hA := hA) (hM := hM) (hPl := hPl) (hfm := hfm) (hK := J.toJStage3A.ksbAligned) (hT := J.toJStage3A.offAsE) (K := K) (hnvm := hnvm) (hTO := hTO) (hEO := hEO) (hIpa := hIpa) (length := length) (buf := buf) (ex := ex) (cte := cte) (hab := hab) (ptr := ptr) (asid := asid) (va := va) (hpS := hpS) (hcap := hcap) (hf := hf) (k := k) (hkR := hkR) (hkR1 := hkR1) (hcov := hcov) (hext := hext) (hSC := hSC) + refine ⟨s3, a', σ3, ?_, ⟨x, hc0, hc1, hc3⟩, curW_keptA J.toJStage3A J3 K3.cur, A64RelS3.ofJ J3 (Sched.BodyOKA.ofKeep J hc0 K3)⟩ + rw [show capA st s (cslotAddrA cte) = (.PageTable ptr true asid va) from hcap] + exact (decInv_ptA ar s a _ length _ ptr true asid va bB bC bD bF buf ex).trans hdec1 + | @ptUnmapU bB bC bD bF hnvm length buf ex cte hab ptr asid va hcap hf hcov hext hSC hpin => + obtain ⟨x, s2, σ2, hc0, hc1, hdec1, hc3, J3, K3⟩ := JSyscallA.ptUnmapUA (J := J) (hA := hA) (hM := hM) (hPl := hPl) (hfm := hfm) (hnvm := hnvm) (length := length) (buf := buf) (ex := ex) (cte := cte) (hab := hab) (ptr := ptr) (asid := asid) (va := va) (hcap := hcap) (hf := hf) (hcov := hcov) (hext := hext) (hSC := hSC) + refine ⟨s2, a, σ2, ?_, ⟨x, hc0, hc1, hc3⟩, curW_keptA J.toJStage3A J3 K3.cur, A64RelS3.ofJ J3 (Sched.BodyOKA.ofKeep J hc0 K3)⟩ + rw [show capA st s (cslotAddrA cte) = (.PageTable ptr false asid va) from hcap] + exact (decInv_ptA ar s a _ length _ ptr false asid va bB bC bD bF buf ex).trans hdec1 + | @asidAssign hnvm cte hab p base hpool length buf block call cd fp ex dst hd0 hex k root oldAsid oldVa hcap htab hp0 i hsearch hfull hcov hext hSC hpin => + obtain ⟨x, s3, σ3, Ac', Tb', Lv', hc0, hc1, hdec1, hc3, hc4, J3, K3⟩ := JSyscallA.asidAssignA (J := J) (hA := hA) (hM := hM) (hPl := hPl) (hfm := hfm) (hnvm := hnvm) (cte := cte) (hab := hab) (p := p) (base := base) (hpool := hpool) (length := length) (buf := buf) (block := block) (call := call) (cd := cd) (fp := fp) (ex := ex) (dst := dst) (hd := hd0) (hex := hex) (k := k) (root := root) (oldAsid := oldAsid) (oldVa := oldVa) (hcap := hcap) (htab := htab) (hp0 := hp0) (i := i) (hsearch := hsearch) (hfull := hfull) (hcov := hcov) (hext := hext) (hSC := hSC) + refine ⟨s3, a, σ3, ?_, ⟨x, hc0, hc1, hc3, hc4⟩, curW_keptA J.toJStage3A J3 K3.cur, A64RelS3.ofJ J3 (Sched.BodyOKA.ofKeep J hc0 K3)⟩ + rw [show capA st s (cslotAddrA cte) = .AsidPool p base from hpool] + exact hdec1 + | @frameMapHi x0 D dc ex s3 a3 callD lenD bufD rr bB bD bF K hnvm hTO hEO cte hab fp rt asid va sz dev mp hcap FP p1 asid1 mp1 h16 hx0 hx0c vaddr rightsMask vm attr hvmEq hD d hres hcov hext hSC hx hlen h0 h1 h2 hr hvm hpin => + obtain ⟨x, iv, σ3, Ac', hc0, hc1, hc2, hc3, J3, K3⟩ := JSyscallA.frameMapHiA (J := J) (hA := hA) (hM := hM) (hPl := hPl) (hfm := hfm) (callD := callD) (hK := J.toJStage3A.ksbAligned) (hT := J.toJStage3A.offAsE) (K := K) (hnvm := hnvm) (hTO := hTO) (hEO := hEO) (cte := cte) (hab := hab) (fp := fp) (rt := rt) (asid := asid) (va := va) (sz := sz) (dev := dev) (mp := mp) (hcap := hcap) (FP := FP) (p1 := p1) (asid1 := asid1) (mp1 := mp1) (h16 := h16) (hx0 := hx0) (hx0c := hx0c) (vaddr := vaddr) (rightsMask := rightsMask) (vm := vm) (attr := attr) (hvmEq := hvmEq) (hD := hD) (d := d) (hres := hres) (hcov := hcov) (hext := hext) (hSC := hSC) (hx := hx) (hlen := hlen) (h0 := h0) (h1 := h1) (h2 := h2) (hr := hr) (hvm := hvm) + refine ⟨s3, a3, σ3, ?_, ⟨x, iv, hc0, hc1, hc2, hc3⟩, curW_keptA J.toJStage3A J3 K3.cur, A64RelS3.ofJ J3 (Sched.BodyOKA.ofKeep J hc0 K3)⟩ + rw [show capA st s (cslotAddrA cte) = (.Frame fp sz rt dev mp false asid va) from (capAtA_eq (st := st) _ _).symm.trans hcap, + decInv_frameA ar s a _ lenD _ fp sz rt dev mp false asid va bB callD bD bF bufD ex, + decFrame_hiA hx hlen h0 h1 h2 hr hvm (by rw [capA_eq, hx0c])] + exact hres + +/-- **Every Vs arm in scope runs and keeps the record.** -/ +theorem VsScopeA.sound {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} + {κ : U64 → U64} {n : Nat} {ar : machine.Arch A} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + {lbl len slot : U64} {blk call don fp : Bool} {buf : U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} + {Out : Fl4SpecA64.State Unit → Prop} + (h : VsScopeA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar hPl.ipa s a σ m lbl len slot blk call don fp buf + extra Out) : + DispPostA st AR ar hPl.ipa n Z.Cs s a m lbl len slot blk call don fp buf extra Out := + (VsScopeA.soundOk hA hM hPl J hfm h).post + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3DomIpiA.lean b/proofs/Fl4ProofsA64/P3/Stage3DomIpiA.lean new file mode 100644 index 000000000..1df2d84b1 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3DomIpiA.lean @@ -0,0 +1,167 @@ +/- +P3 (aarch64, A64-P3 Stage 3, record edit #8b part B, items 5 and 6): the one-domain schedule words (`NdOKA`, +`P3/ScDomainA`) and this core's empty IPI pair (`SilentScheduleIpiA`, `P3/ScScheduleTimerA`) at the boot witnesses +`w0` / `w1`, and the frame of the IPI pair. The record field `domOK` is added by record edit #8b-B1 (`JStage3A.domOK`); `ipiZero` is not added yet. + +No premise. +-/ +import Fl4ProofsA64.P3.ScScheduleTimerA +import Fl4ProofsA64.P3.JBootW1SA +import Fl4ProofsA64.P3.JBootW0A +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P1.W7.R.ST.BootWit + Fl4ProofsA64.P3 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section frame +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- The IPI pair is framed by the two words. -/ +theorem SilentScheduleIpiA.keep {s s' : S} (H : SilentScheduleIpiA (st := st) s) + (lo : L.mem s' (L.nodeAt s' (L.cpu s') layout.node.IPI_PENDING) = + L.mem s (L.nodeAt s (L.cpu s) layout.node.IPI_PENDING)) + (hi : L.mem s' (L.nodeAt s' (L.cpu s') layout.node.IPI_PENDING_HI) = + L.mem s (L.nodeAt s (L.cpu s) layout.node.IPI_PENDING_HI)) : SilentScheduleIpiA (st := st) s' := + ⟨lo.trans H.1, hi.trans H.2⟩ + +end frame + +section boot +variable {kb ib r now mt sl : U64} {nc : Nat} {uw : U64 → U64} + +private theorem wa_kbA (hk : kb.val + 788736 ≤ 2 ^ 64) {o : U64} (ho : o.val < 788736) : + (wa kb o).val = kb.val + o.val := by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + +private theorem ksW_domA {o : Nat} (h1 : 24 ≤ o) (h2 : o < 224) (h3 : o ≠ 48) : + ksW ib nc r now o = if o = 200 then 72057594037927935#u64 else 0#u64 := by + unfold ksW + rw [if_neg (by omega), if_neg (by omega), if_neg (by omega), if_neg h3] + +private theorem ksW1_lowA {o : Nat} (h : o < 224) : ksW1 ib nc r now o = ksW ib nc r now o := by + unfold ksW1 + rw [if_neg (by omega), if_neg (by omega), if_neg (by omega)] + +private theorem ksW_nodesA (hn : nc < 2 ^ 64) : (ksW ib nc r now 0).val = nc := by + unfold ksW + rw [if_neg (by omega), if_pos rfl] + exact bwv_val hn + +private theorem ksW_krA {o : Nat} (h : o = 48) : ksW ib nc r now o = r := by + subst h + unfold ksW + rw [if_neg (by omega), if_neg (by omega), if_neg (by omega), if_pos rfl] + +/-- (record edit #8b-B, item 5; F1: `hn`, the boot's core count is the platform's; F4: `hr`, `KERNEL_ROOT = r` is the +spec's global user VSpace) **`NdOKA` at `w0`.** -/ +theorem boot_ndOKA (hr : specPtr r = Fl4SpecA64.armGlobalPtPtr) (hk : kb.val + 788736 ≤ 2 ^ 64) (ha : kb.val % 8 = 0) (hn : nc = fl4A64Platform.numNodes) + (hn' : nc ≤ 128) : + NdOKA (st := bst) (bootS kb ib nc r now mt) (bootSA kb ib nc now mt) := by + have w0 : ((bL).mem (bootS kb ib nc r now mt) (wa ((bL).ksb (bootS kb ib nc r now mt)) 0#u64)).val = + fl4A64Platform.numNodes := by + show (bootM kb ib nc r now mt (wa kb 0#u64)).val = _ + rw [bootM_ks hk (o := 0) (by omega) (wa_kbA hk (o := 0#u64) (by show (0 : Nat) < 788736; decide)), + ksW_nodesA (by omega)] + exact hn + have word (o : U64) (h1 : 24 ≤ o.val) (h2 : o.val < 224) (h3 : o.val ≠ 48) : + (bL).mem (bootS kb ib nc r now mt) (wa ((bL).ksb (bootS kb ib nc r now mt)) o) = + if o.val = 200 then 72057594037927935#u64 else 0#u64 := by + show bootM kb ib nc r now mt (wa kb o) = _ + rw [bootM_ks hk (o := o.val) (by omega) (wa_kbA hk (by omega)), ksW_domA h1 h2 h3] + have kword : (bL).mem (bootS kb ib nc r now mt) (wa ((bL).ksb (bootS kb ib nc r now mt)) 48#u64) = r := by + show bootM kb ib nc r now mt (wa kb 48#u64) = r + rw [bootM_ks hk (o := (48#u64 : U64).val) (by decide) (wa_kbA hk (by decide)), ksW_krA (by decide)] + refine NdOKA.of_bootWordsA (st := bst) rfl rfl rfl ?_ ?_ ?_ ?_ ?_ ?_ w0 ?_ ?_ ha (by show (0#u64 : U64).val < 128; decide) + · exact (word 24#u64 (by simp) (by simp) (by simp)).trans (if_neg (by simp)) + · exact (word 32#u64 (by simp) (by simp) (by simp)).trans (if_neg (by simp)) + · exact (word 192#u64 (by simp) (by simp) (by simp)).trans (if_neg (by simp)) + · exact (word 200#u64 (by simp) (by simp) (by simp)).trans (if_pos (by simp)) + · exact (word 208#u64 (by simp) (by simp) (by simp)).trans (if_neg (by simp)) + · exact (word 216#u64 (by simp) (by simp) (by simp)).trans (if_neg (by simp)) + · exact (congrArg specPtr kword).trans hr + · show kb.val + ksSizeA ≤ 2 ^ 64 + unfold ksSizeA; omega + +/-- (record edit #8b-B, item 5; F4: `hr`) **`NdOKA` at `w1`.** -/ +theorem w1_ndOKA (hr : specPtr r = Fl4SpecA64.armGlobalPtPtr) (P : BootLay1 kb ib nc) (ha : kb.val % 8 = 0) (hn : nc = fl4A64Platform.numNodes) : + NdOKA (st := bst) (w1S kb ib nc r now mt sl uw) (w1SA kb ib nc now mt sl) := by + have hk : kb.val + 788736 ≤ 2 ^ 64 := P.w0.kbw + have w0 : ((bL).mem (w1S kb ib nc r now mt sl uw) (wa ((bL).ksb (w1S kb ib nc r now mt sl uw)) 0#u64)).val = + fl4A64Platform.numNodes := by + show (w1M kb ib nc r now mt sl uw (wa kb 0#u64)).val = _ + rw [w1M_ks P (o := 0) (by omega) (wa_kbA hk (o := 0#u64) (by show (0 : Nat) < 788736; decide)), + ksW1_lowA (by omega), ksW_nodesA (by have := P.w0.nc128; omega)] + exact hn + have word (o : U64) (h1 : 24 ≤ o.val) (h2 : o.val < 224) (h3 : o.val ≠ 48) : + (bL).mem (w1S kb ib nc r now mt sl uw) (wa ((bL).ksb (w1S kb ib nc r now mt sl uw)) o) = + if o.val = 200 then 72057594037927935#u64 else 0#u64 := by + show w1M kb ib nc r now mt sl uw (wa kb o) = _ + rw [w1M_ks P (o := o.val) (by omega) (wa_kbA hk (by omega)), ksW1_lowA h2, ksW_domA h1 h2 h3] + have kword : (bL).mem (w1S kb ib nc r now mt sl uw) (wa ((bL).ksb (w1S kb ib nc r now mt sl uw)) 48#u64) = r := by + show w1M kb ib nc r now mt sl uw (wa kb 48#u64) = r + rw [w1M_ks P (o := (48#u64 : U64).val) (by decide) (wa_kbA hk (by decide)), ksW1_lowA (by decide), + ksW_krA (by decide)] + refine NdOKA.of_bootWordsA (st := bst) rfl rfl rfl ?_ ?_ ?_ ?_ ?_ ?_ w0 ?_ ?_ ha (by show (0#u64 : U64).val < 128; decide) + · exact (word 24#u64 (by simp) (by simp) (by simp)).trans (if_neg (by simp)) + · exact (word 32#u64 (by simp) (by simp) (by simp)).trans (if_neg (by simp)) + · exact (word 192#u64 (by simp) (by simp) (by simp)).trans (if_neg (by simp)) + · exact (word 200#u64 (by simp) (by simp) (by simp)).trans (if_pos (by simp)) + · exact (word 208#u64 (by simp) (by simp) (by simp)).trans (if_neg (by simp)) + · exact (word 216#u64 (by simp) (by simp) (by simp)).trans (if_neg (by simp)) + · exact (congrArg specPtr kword).trans hr + · show kb.val + ksSizeA ≤ 2 ^ 64 + unfold ksSizeA; omega + +private theorem ksW_ipiA {o : Nat} (hn : 0 < nc) (ho : o = 120 ∨ o = 128) : + ksW ib nc r now (223488 + o) = 0#u64 := by + unfold ksW + rw [if_pos (by omega), show (223488 + o - 223488) / 4288 = 0 by omega, + show (223488 + o - 223488) % 4288 = o by omega] + unfold nodeW + rw [if_neg (by omega), if_neg (by omega), if_neg (by omega), if_neg (by omega)] + +/-- (record edit #8b-B, item 6) **The IPI pair is empty at `w0`.** -/ +theorem boot_ipiZeroA (hk : kb.val + 788736 ≤ 2 ^ 64) (hn : 0 < nc) : + SilentScheduleIpiA (st := bst) (bootS kb ib nc r now mt) := by + have word (o : U64) (ho : o.val = 120 ∨ o.val = 128) : + (bL).mem (bootS kb ib nc r now mt) + ((bL).nodeAt (bootS kb ib nc r now mt) ((bL).cpu (bootS kb ib nc r now mt)) o) = 0#u64 := by + have v : ((bL).nodeAt (bootS kb ib nc r now mt) ((bL).cpu (bootS kb ib nc r now mt)) o).val = + kb.val + (223488 + o.val) := by + rw [nodeAt_valSc (by show kb.val + ksSizeA ≤ 2 ^ 64; unfold ksSizeA; omega) + (by show (0#u64 : U64).val < 128; decide) (by omega)] + show kb.val + 223488 + (0#u64 : U64).val * 4288 + o.val = _ + simp; omega + show bootM kb ib nc r now mt _ = 0#u64 + rw [bootM_ks hk (o := 223488 + o.val) (by omega) v, ksW_ipiA hn ho] + exact ⟨word _ (Or.inl (by simp [layout.node.IPI_PENDING])), word _ (Or.inr (by simp [layout.node.IPI_PENDING_HI]))⟩ + +/-- (record edit #8b-B, item 6) **The IPI pair is empty at `w1`.** -/ +theorem w1_ipiZeroA (P : BootLay1 kb ib nc) (hn : 0 < nc) : + SilentScheduleIpiA (st := bst) (w1S kb ib nc r now mt sl uw) := by + have hk : kb.val + 788736 ≤ 2 ^ 64 := P.w0.kbw + have word (o : U64) (ho : o.val = 120 ∨ o.val = 128) : + (bL).mem (w1S kb ib nc r now mt sl uw) + ((bL).nodeAt (w1S kb ib nc r now mt sl uw) ((bL).cpu (w1S kb ib nc r now mt sl uw)) o) = 0#u64 := by + have v : ((bL).nodeAt (w1S kb ib nc r now mt sl uw) ((bL).cpu (w1S kb ib nc r now mt sl uw)) o).val = + kb.val + (223488 + o.val) := by + rw [nodeAt_valSc (by show kb.val + ksSizeA ≤ 2 ^ 64; unfold ksSizeA; omega) + (by show (0#u64 : U64).val < 128; decide) (by omega)] + show kb.val + 223488 + (0#u64 : U64).val * 4288 + o.val = _ + simp; omega + show w1M kb ib nc r now mt sl uw _ = 0#u64 + rw [w1M_ks P (o := 223488 + o.val) (by omega) v] + unfold ksW1 + rw [if_neg (by omega), if_neg (by omega), if_neg (by omega), ksW_ipiA hn ho] + exact ⟨word _ (Or.inl (by simp [layout.node.IPI_PENDING])), word _ (Or.inr (by simp [layout.node.IPI_PENDING_HI]))⟩ + +end boot + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3EntryProjA.lean b/proofs/Fl4ProofsA64/P3/Stage3EntryProjA.lean new file mode 100644 index 000000000..3712b349f --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3EntryProjA.lean @@ -0,0 +1,82 @@ +/- +P3 (aarch64, Stage-3 step 4): the MCS entry-prefix inputs of `OuterEntryA` read off the unified record +`JStage3EntryA`. + +* `JStage3A.stEqA`, `JStage3EntryA.yieldQueueA`: `YieldQueueRelA` from `sr` / `ep` / `mcp` / `yieldRel`. +* `JStage3EntryA.budgetPlaceA`: `EntryBudgetPlaceA`. +* `budget_ok_stage3A`, `irq_budget_prefix_stage3A`: the two raw-clock prefixes (`ScBudgetTopA`) at the record. + +`m.WF` and "current thread in no ready queue" are the record's `SInvXA.mc` / `ctQ`, at +`n = fl4A64Platform.numNodes`. Premises not carried by the record: `hcore` [S] (the current SC's `CORE` word is +this core; the non-idle current SC fact is the record's `curScNotIdle`); the platform +clock / constants [P] and the timeout leaf `EntryTimeoutFromA` [J], as in `ScBudgetTopA`. +New module; no existing statement changed. +-/ +import Fl4ProofsA64.P3.Stage3A +import Fl4ProofsA64.P3.ScBudgetTopA + +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3 + +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Endp + +noncomputable section + +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +theorem JStage3A.stEqA (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) : + StEqA Z.T (L.mem s) σ := + fun _ ht _ hx => (J.ep.at_tcb ht hx).st + +theorem JStage3A.yieldQueueA (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) : + YieldQueueRelA (st := st) Z m κ s σ := + ⟨J.sr, J.stEqA, J.mcp, J.yieldRel⟩ + +theorem JStage3A.budgetPlaceA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes J5 J7 s a σ m) + (hcore : L.mem s (scCoreA (L.cscW s)) = L.cpu s) + (hni : σ.curSc ≠ idleScPtr → NotIdleScA m σ.curSc) (hact : L.actionW s = 0#u64) : EntryBudgetPlaceA st m κ s σ := + ⟨J.sinv.mc, (J.sr.scCore _ J.sr.cscC).symm.trans hcore, hni, + J.sinv.ctQ (by simp [J.j.1.sch.act, hact, actOfA])⟩ + +theorem budget_ok_stage3A + (J : JStage3EntryA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes J5 J7 s a σ m) + (hcore : L.mem s (scCoreA (L.cscW s)) = L.cpu s) (hact : L.actionW s = 0#u64) + {Clock : A → MachineState → Prop} (now : NowAgreeA ar Clock) (clk : Clock a σ.machineState) + (C : CounterStepA σ) + (T : EntryTimeoutFromA (st := st) (ar := ar) Z m κ Clock s σ) + (hag : fl4MinBudget = MIN_BUDGET) (hMin : ∀ a', Fl4MinBudgetAgreeA ar a') + (hLimit : ∀ a', sporadic.max_release_time ar a' ⦃ v => v.bv = fl4MaxReleaseTime ⦄) + (hnd : numDomains = 1) : + syscall.budget_ok st ar s a ⦃ r => ∃ σ1 τ Q' R', + fl4BudgetOk (Z := Unit) σ = ({(r.1, τ)}, False) ∧ + fl4UpdateTimestamp (Z := Unit) σ = ({((), σ1)}, False) ∧ + YieldQueueRelA (st := st) { Z with Q := Q', R := R' } m κ r.2.1 τ ∧ + CheckBudgetPostA Clock r.2.2 σ1 r.1 τ ∧ ValidMachineTimeA τ ⦄ := + budget_ok_refinesA J.toJStage3A.yieldQueueA J.curInv J.offReady J.actIdle + (J.toJStage3A.budgetPlaceA hcore J.curScNotIdle hact) a now clk J.validTime C T hag hMin hLimit hnd + +theorem irq_budget_prefix_stage3A + (J : JStage3EntryA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes J5 J7 s a σ m) + (hcore : L.mem s (scCoreA (L.cscW s)) = L.cpu s) (hact : L.actionW s = 0#u64) + {Clock : A → MachineState → Prop} (now : NowAgreeA ar Clock) (clk : Clock a σ.machineState) + (C : CounterStepA σ) + (T : EntryTimeoutFromA (st := st) (ar := ar) Z m κ Clock s σ) + (hag : fl4MinBudget = MIN_BUDGET) (hMin : ∀ a', Fl4MinBudgetAgreeA ar a') + (hLimit : ∀ a', sporadic.max_release_time ar a' ⦃ v => v.bv = fl4MaxReleaseTime ⦄) + (hnd : numDomains = 1) : + (do let (s1, a1) ← sched.update_timestamp st ar s a; sched.check_budget st ar s1 a1) ⦃ r => + ∃ σ1 τ Q' R', (fl4UpdateTimestamp >>= fun _ => fl4CheckBudget (Z := Unit)) σ = ({(r.1, τ)}, False) ∧ + fl4UpdateTimestamp (Z := Unit) σ = ({((), σ1)}, False) ∧ + YieldQueueRelA (st := st) { Z with Q := Q', R := R' } m κ r.2.1 τ ∧ + CheckBudgetPostA Clock r.2.2 σ1 r.1 τ ∧ ValidMachineTimeA τ ⦄ := + irq_budget_prefix_refinesA J.toJStage3A.yieldQueueA J.curInv J.offReady J.actIdle + (J.toJStage3A.budgetPlaceA hcore J.curScNotIdle hact) a now clk J.validTime C T hag hMin hLimit hnd + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3EntryValidA.lean b/proofs/Fl4ProofsA64/P3/Stage3EntryValidA.lean new file mode 100644 index 000000000..4ed30b9bb --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3EntryValidA.lean @@ -0,0 +1,133 @@ +/- +P3 (aarch64, A64-P3 Stage-3 integration, hpre): **the validity read of the timeout leaf from the record** +(`EntryValidFromA`, the `send`-free part of `EntryTimeoutFromA`). + +`faults.valid_timeout_handler` reads `isEpCap` of the cap in the thread's `SLOT_TIMEOUT_HANDLER` slot, and the spec's +`isValidTimeoutHandler` reads `isEpCap` of the TCB's timeout-handler field. The record relates the two at the state +where the leaf is read (`JStage3A.tmoRelA`: `J.j.1.st.tcb`, as `tmoSlotA`); the prefix frame (`MFrA`) keeps the slot's +two words (TCB words 128 / 136, off the scheduler words) and the TCB's handler field (`SKeepA.tcb`). + +* `JStage3A.entryValidA`: the premise `T` of every `can = false` / `NoTimeoutA` consumer, discharged. It is + recomputed from the record at the state it is read, not carried across a delete/revoke. + +No named premise. +-/ +import Fl4ProofsA64.P3.Stage3KEntryChargeA +import Fl4ProofsA64.P3.EpBufA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4SpecA64 Fl4Spec Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {J5 : JSlotA S} {R : Set U64 → JSlotA S} {s : S} {a : A} {σ : State Unit} + {m : McState} + +/-- `valid_timeout_handler` reads whether the related spec cap is an endpoint cap (`validTimeoutHandler_relA` of +`P3/Stage3ReplyTmoDispA`, copied: that module is downstream of `Stage3KEntrySendBlkA`). -/ +theorem validTimeoutHandler_relA' {s : S} {t : U64} {h : Fl4SpecA64.Cap} + (rel : CapRelA (capA st s (slotIdx t layout.tcb.SLOT_TIMEOUT_HANDLER)) h) : + faults.valid_timeout_handler st s t = ok (isEpCap h) := by + unfold faults.valid_timeout_handler + rw [tcb_slot_val, bind_ok, capA_eq, bind_ok] + have e := rel.2 + generalize capA st s (slotIdx t layout.tcb.SLOT_TIMEOUT_HANDLER) = c at e ⊢ + cases c with + | Endpoint => simp only [CapEqA, absCapA] at e; subst e; rfl + | PageTable q mm asid va => cases h <;> first | rfl | (cases mm <;> simp [CapEqA, absCapA] at e) + | _ => cases h <;> first | rfl | (simp [CapEqA, absCapA] at e) + +/-- **A TCB's timeout-handler slot cap is related to its handler field** (the `rel` of `tmoSlotA`, any cap). -/ +theorem JStage3A.tmoRelA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st R) s a σ m) + {t : U64} (ht : t ∈ Z.T) {y : Tcb} (hy : σ.kheap (specPtr t) = some (.tcb y)) : + CapRelA (capA st s (slotIdx t layout.tcb.SLOT_TIMEOUT_HANDLER)) y.tcbTimeoutHandler := by + have one4 : tcbCnodeMap y (tcbCnodeIndex 4) = some y.tcbTimeoutHandler := by + simp [tcbCnodeMap, tcbIdx_eq, tcbStage2Slot, tcbHighVSpaceSlot] + have hdth : Z.th t = some .tcb := (J.sr.thT t).2 ht + have es : slotIdx t layout.tcb.SLOT_TIMEOUT_HANDLER = slotAddrA (specPtr t) (BitVec.ofNat 64 4) := by + rw [slotIdx_addrA] + unfold layout.tcb.SLOT_TIMEOUT_HANDLER + rfl + obtain ⟨z, hz, h⟩ := J.j.1.st.tcb t hdth + rw [hy] at hz + cases hz + obtain ⟨ac, hac, h⟩ := h 4 (by unfold tcbSlotsA; omega) + rw [one4] at hac + cases hac + rw [es] + exact h + +/-- **The slot's two words are kept by the prefix frame**: the timeout-handler slot lies in the TCB's CSpace words +(offsets 128 and 136), off the four scheduler words and the kernel-state / SC footprints. -/ +theorem JStage3A.tmoSlotKeepA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st R) s a σ m) + {t : U64} (ht : t ∈ Z.T) {s' : S} {τ : State Unit} (F : MFrA (st := st) Z m s s' σ τ) : + capA st s' (slotIdx t layout.tcb.SLOT_TIMEOUT_HANDLER) = capA st s (slotIdx t layout.tcb.SLOT_TIMEOUT_HANDLER) := by + have e0 : slotIdx t layout.tcb.SLOT_TIMEOUT_HANDLER = wa t 128#u64 := by + unfold slotIdx layout.tcb.SLOT_TIMEOUT_HANDLER + congr 1 + have hT := J.sr.core.tcbs t ht + have h128 : (wa t 128#u64).val = t.val + 128 := field_val hT (by decide) + have h136 : (wa t 136#u64).val = t.val + 136 := field_val hT (by decide) + have e8 : wa (wa t 128#u64) 8#u64 = wa t 136#u64 := by + apply UScalar.eq_of_val_eq + rw [wa_valm, h128, h136] + have : t.val + 136 < 2 ^ 64 := by + have h := (wa t 136#u64).hSize + rw [h136] at h + simpa [U64.size_def, U64.numBits] using h + simp only [show (8#u64).val = 8 from rfl] + exact Nat.mod_eq_of_lt (by omega) + have k0 : L.mem s' (wa t 128#u64) = L.mem s (wa t 128#u64) := + F.wr.mem_eq (field_aligned hT (by decide) (by decide)) + (tcbNotMWrA J.sr.toSchedSRN ht (by decide) (by decide)) + have k8 : L.mem s' (wa t 136#u64) = L.mem s (wa t 136#u64) := + F.wr.mem_eq (field_aligned hT (by decide) (by decide)) + (tcbNotMWrA J.sr.toSchedSRN ht (by decide) (by decide)) + rw [e0] + unfold capA + rw [e8, k0, k8] + +/-- **The validity read of the timeout leaf, from the record at the state it is read** (module docstring). -/ +theorem JStage3A.entryValidA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st R) s a σ m) + {Clock : A → MachineState → Prop} : EntryValidFromA (st := st) Z m κ Clock s σ := by + intro s' a' τ F Hs _ _ _ _ hct _ + have htZ : L.curW s ∈ Z.T := J.sr.curT + have ct : σ.curThread = specPtr (L.curW s) := J.sr.sch.curThread + have ct' : τ.curThread = specPtr (L.curW s') := Hs.sch.curThread + have ec : L.curW s' = L.curW s := specPtr_inj (ct'.symm.trans (hct.trans ct)) + rw [ec] + obtain ⟨x, hx⟩ := J.sinv.curTcb + have hkx : σ.kheap (specPtr (L.curW s)) = some (.tcb x) := by rw [← ct]; exact hx + obtain ⟨y, hy, o, rfl⟩ := F.keep.tcb (specPtr (L.curW s)) x hkx + have one4 : ∀ z : Tcb, tcbCnodeMap z (tcbCnodeIndex 4) = some z.tcbTimeoutHandler := fun z => by + simp [tcbCnodeMap, tcbIdx_eq, tcbStage2Slot, tcbHighVSpaceSlot] + have rel := J.tmoRelA htZ hkx + rw [← J.tmoSlotKeepA htZ F] at rel + have hv := validTimeoutHandler_relA' (st := st) (s := s') (t := L.curW s) rel + unfold EntryValidA + rw [hv] + refine (WP.spec_ok _).2 ?_ + have hk' : τ.kheap (specPtr (L.curW s)) = some (.tcb { x with tcbSchedContext := o }) := by + exact hy + have hgc0 : getCap (Z := Unit) (getTcbTimeoutHandlerPtr (specPtr (L.curW s))) τ = + ({(x.tcbTimeoutHandler, τ)}, False) := getCap_tcb_eqA hk' (one4 _) + unfold isValidTimeoutHandler + rw [nd_pure_bindSc, nd_bind_oneSc hgc0] + rfl + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3EpWakeFrA.lean b/proofs/Fl4ProofsA64/P3/Stage3EpWakeFrA.lean new file mode 100644 index 000000000..65cf80366 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3EpWakeFrA.lean @@ -0,0 +1,228 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2a): **the endpoint relation across a Notification send waking a waiter**. +`EpStateRel.frameW` is the sibling of `EpStateRel.frameN` (`P3/DelNtfnEpA`) for a step that wakes one waiter `d` of the +notification `n`: FL4 writes the notification's words, the state word and `Ep` links of the queued threads `Q ∋ d` +(`SigWrA`) and `d`'s badge register; the spec keeps what `WakeKeepA` says (every TCB's fault, bound notification, +priority, IPC frame and buffer fields; the arch of every TCB but `d`; `d` changes only its badge register and goes +`running`), and the state words and `d`'s badge register are read off `NtfnStateRel`. No waiter is on an endpoint +queue, so no endpoint reads a written word. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.DelNtfnEpA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4ProofsA64.P3.Notif +open Fl4SpecA64 + +/-- What a wake of `d` keeps (module docstring). -/ +structure WakeKeepA (σ σ' : State Unit) (d : ObjRef) : Prop where + ep : ∀ p x, σ.kheap p = some (.endpoint x) → σ'.kheap p = some (.endpoint x) + reply : ∀ p x, σ.kheap p = some (.reply x) → σ'.kheap p = some (.reply x) + tcb : ∀ p x, σ.kheap p = some (.tcb x) → ∃ y, σ'.kheap p = some (.tcb y) ∧ y.tcbFault = x.tcbFault ∧ + y.tcbBoundNotification = x.tcbBoundNotification ∧ y.tcbPriority = x.tcbPriority ∧ + y.tcbIpcframe = x.tcbIpcframe ∧ y.tcbIpcBuffer = x.tcbIpcBuffer ∧ + (p ≠ d → y.tcbArch = x.tcbArch ∧ y.tcbState = x.tcbState) ∧ + (p = d → (∀ r, r ≠ badge_register → y.tcbArch.tcbContext.userRegs r = x.tcbArch.tcbContext.userRegs r) ∧ + y.tcbState = .running) + um : σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory + +private theorem otherTcbW {T E R : Set U64} {Bf : Set (U64 × Word)} (hP : EPlaced T E R Bf) {y q a : U64} + (hy : y ∈ T) (hq : q ∈ T) (hne : y ≠ q) (h1 : y.val ≤ a.val) (h2 : a.val < y.val + 4096) : + a ≠ stA q ∧ a ≠ nextA .Ep q ∧ a ≠ prevA .Ep q := by + have hqa := hP.tcb q hq + have := tcb_disj hP hy hq hne.symm + refine ⟨fun e => ?_, fun e => ?_, fun e => ?_⟩ + · have := val_st hqa; rw [← e] at this; omega + · have := val_next hqa; rw [← e] at this; omega + · have := val_prev hqa; rw [← e] at this; omega + +private theorem offTcbW {T E R : Set U64} {Bf : Set (U64 × Word)} (hP : EPlaced T E R Bf) {q a : U64} + (hq : q ∈ T) (h : a.val < q.val ∨ q.val + 4096 ≤ a.val) : + a ≠ stA q ∧ a ≠ nextA .Ep q ∧ a ≠ prevA .Ep q := by + have hqa := hP.tcb q hq + refine ⟨fun e => ?_, fun e => ?_, fun e => ?_⟩ + · have := val_st hqa; rw [← e] at this; omega + · have := val_next hqa; rw [← e] at this; omega + · have := val_prev hqa; rw [← e] at this; omega + +set_option maxHeartbeats 1000000 in +/-- **`EpStateRel` across a wake of `d`** whose queue set `Q` holds no endpoint-queued thread. -/ +theorem EpStateRel.frameWG {N T E R : Set U64} {Bf : Set (U64 × Word)} {m m' : U64 → U64} + {σ σ' : Fl4SpecA64.State Unit} (hR : EpStateRel T E R Bf m σ) (hP : IPlacedA N T E R Bf) {n d : U64} + (hn : n ∈ N) {Q : Set U64} (hQ : ∀ q ∈ Q, q ∈ T) (hdQ : d ∈ Q) + (hnq : ∀ e ∈ E, ∀ x, σ.kheap (specPtr e) = some (.endpoint x) → ∀ y ∈ T, specPtr y ∈ x.epQueue → y ∉ Q) + (hm : ∀ a, Aligned a → EFoot T E R Bf a → ¬ SigWrA Q n a → a ≠ badgeRegA d → m' a = m a) + (hst : ∀ t ∈ T, ∀ z, σ'.kheap (specPtr t) = some (.tcb z) → (m' (stA t)).val = tsCode z.tcbState) + (hbd : ∀ z, σ'.kheap (specPtr d) = some (.tcb z) → + (m' (badgeRegA d)).bv = z.tcbArch.tcbContext.userRegs badge_register) + (hk : WakeKeepA σ σ' (specPtr d)) : EpStateRel T E R Bf m' σ' := by + have hdT := hQ d hdQ + have hda := hP.e.tcb d hdT + have h0 : (regIdxA badge_register).val = 0 := rfl + have bv : (badgeRegA d).val = d.val + 256 := by + rw [badgeRegA_eq_regA, regA_val hda (regIdxA_lt _), h0] + have keep : ∀ a, Aligned a → EFoot T E R Bf a → + (∀ q ∈ Q, a ≠ stA q ∧ a ≠ nextA .Ep q ∧ a ≠ prevA .Ep q) → a ≠ badgeRegA d → m' a = m a := by + intro a ha hf h hb + refine hm a ha hf ?_ hb + rintro (⟨h1, h2⟩ | ⟨q, hq, e | e | e⟩) + · exact nw_not_efoot hP hn h1 h2 hf + · exact (h q hq).1 e + · exact (h q hq).2.1 e + · exact (h q hq).2.2 e + have nbd : ∀ (t : U64) (a : U64), (t = d → a.val ≠ d.val + 256) → (t = d → a ≠ badgeRegA d) := + fun t a h e hh => h e (by rw [hh, bv]) + have hTF : ∀ y ∈ T, ∀ a, Aligned a → TF y a → a ≠ stA y → (y = d → a ≠ badgeRegA d) → m' a = m a := by + intro y hy a ha hf hne hbd' + refine keep a ha (Or.inr (Or.inr (Or.inl ⟨y, hy, Or.inl hf⟩))) (fun q hq => ?_) ?_ + · by_cases e : y = q + · subst e + have hya := hP.e.tcb y hy + exact ⟨hne, fun e => TL.not_tf (e ▸ (a_next hya).2) hf, fun e => TL.not_tf (e ▸ (a_prev hya).2) hf⟩ + · exact otherTcbW hP.e hy (hQ q hq) e hf.1 (by have := TF.range hf; omega) + · by_cases e : y = d + · exact hbd' e + · intro hab + have := tcb_disj hP.e hdT hy e + have := TF.range hf + rw [hab, bv] at * + omega + refine ⟨fun e he => ?_, fun r hr => ?_, fun t ht => ?_, fun bp hb k hk' => ?_⟩ + · obtain ⟨x, hkx, hx⟩ := hR.ep e he + have hew : ∀ a : U64, (a.val = e.val ∨ a.val = e.val + 8) → + (∀ q ∈ Q, a ≠ stA q ∧ a ≠ nextA .Ep q ∧ a ≠ prevA .Ep q) ∧ a ≠ badgeRegA d := by + intro a ha + refine ⟨fun q hq => ?_, fun h => ?_⟩ + · have := hP.e.et e he q (hQ q hq) + exact offTcbW hP.e (hQ q hq) (by omega) + · have := hP.e.et e he d hdT + rw [h, bv] at ha + omega + refine ⟨x, hk.ep _ _ hkx, hx.frame + (keep _ (a_head hP.e he).1 (a_head hP.e he).2 (hew _ (Or.inl (val_head e))).1 (hew _ (Or.inl (val_head e))).2) + (keep _ (a_ts hP.e he).1 (a_ts hP.e he).2 (hew _ (Or.inr (val_ts hP.e he))).1 + (hew _ (Or.inr (val_ts hP.e he))).2) fun y hy hq => ?_⟩ + have hya := hP.e.tcb y hy + have hb : m (tBlockA y) = e := (hx.blocked hq).2 + have hyQ : y ∉ Q := hnq e he x hkx y hy hq + have hyd : y ≠ d := fun h => hyQ (h ▸ hdQ) + have hw : ∀ a : U64, y.val ≤ a.val → a.val < y.val + 4096 → + (∀ q ∈ Q, a ≠ stA q ∧ a ≠ nextA .Ep q ∧ a ≠ prevA .Ep q) ∧ a ≠ badgeRegA d := fun a h1 h2 => + ⟨fun q hq => otherTcbW hP.e hy (hQ q hq) (fun e => hyQ (e ▸ hq)) h1 h2, fun h => by + have := tcb_disj hP.e hdT hy hyd + rw [h, bv] at h1 h2 + omega⟩ + have hl : ∀ a, Aligned a → TL y a → m' a = m a := fun a ha hl => by + have hh := hw a (by unfold TL at hl; omega) (by unfold TL at hl; omega) + exact keep a ha (Or.inr (Or.inr (Or.inl ⟨y, hy, Or.inr hl⟩))) hh.1 hh.2 + have hf : ∀ a, Aligned a → TF y a → m' a = m a := fun a ha hf => by + have hh := hw a hf.1 (by have := TF.range hf; omega) + exact keep a ha (Or.inr (Or.inr (Or.inl ⟨y, hy, Or.inl hf⟩))) hh.1 hh.2 + exact ⟨hf _ (a_st hya).1 (a_st hya).2, hf _ (a_blk hya).1 (a_blk hya).2, + hl _ (a_prev hya).1 (a_prev hya).2, hl _ (a_next hya).1 (a_next hya).2⟩ + · obtain ⟨x, hkx, hx⟩ := hR.reply r hr + refine ⟨x, hk.reply _ _ hkx, ?_⟩ + have e1 : m' (rTcbA r) = m (rTcbA r) := keep _ (a_rtcb hP.e hr).1 (a_rtcb hP.e hr).2 (fun q hq => by + have := hP.e.rt r hr q (hQ q hq) + exact offTcbW hP.e (hQ q hq) (by rw [rTcbA_val]; omega)) (fun h => by + have := hP.e.rt r hr d hdT + have h2 := congrArg (fun w : U64 => w.val) h + rw [rTcbA_val, bv] at h2 + omega) + rw [hx, e1] + · obtain ⟨x, hkx, hx⟩ := hR.tcb t ht + obtain ⟨z, hkz, kf, kb, kp, ki, kbuf, kne, keq⟩ := hk.tcb _ _ hkx + have hta := hP.e.tcb t ht + have w : ∀ a, Aligned a ∧ TF t a → a.val ≠ t.val + 3328 → (t = d → a ≠ badgeRegA d) → m' a = m a := + fun a h hne hb => hTF t ht a h.1 h.2 (fun e => hne (by rw [e]; exact val_st hta)) hb + have nb : ∀ a : U64, a.val ≠ t.val + 256 → (t = d → a ≠ badgeRegA d) := + fun a h e hh => h (by rw [hh, bv, e]) + have wf : ∀ a, Aligned a ∧ TF t a → a.val ≠ t.val + 256 → a.val ≠ t.val + 3328 → m' a = m a := + fun a h h1 h2 => w a h h2 (nb a h1) + have hs : z.tcbState = x.tcbState ∨ (specPtr t = specPtr d ∧ z.tcbState = .running) := by + by_cases e : specPtr t = specPtr d + · exact Or.inr ⟨e, (keq e).2⟩ + · exact Or.inl (kne e).2 + have e_blk : m' (tBlockA t) = m (tBlockA t) := + wf _ (a_blk hta) (by rw [val_blk hta]; omega) (by rw [val_blk hta]; omega) + have e_badge : m' (tBadgeA t) = m (tBadgeA t) := + wf _ (a_badge hta) (by rw [val_badge hta]; omega) (by rw [val_badge hta]; omega) + have e_flags : m' (tFlagsA t) = m (tFlagsA t) := + wf _ (a_flags hta) (by rw [val_flags hta]; omega) (by rw [val_flags hta]; omega) + have e_reply : m' (tReplyA t) = m (tReplyA t) := + wf _ (a_reply hta) (by rw [val_reply hta]; omega) (by rw [val_reply hta]; omega) + have e_bound : m' (tBoundA t) = m (tBoundA t) := + wf _ (a_bound hta) (by rw [val_bound hta]; omega) (by rw [val_bound hta]; omega) + have e_fault : m' (tFaultA t) = m (tFaultA t) := + wf _ (a_fault hta) (by rw [val_fault hta]; omega) (by rw [val_fault hta]; omega) + have e_prio : m' (prioA t) = m (prioA t) := + wf _ (a_prio hta) (by rw [val_prio hta]; omega) (by rw [val_prio hta]; omega) + refine ⟨z, hkz, ⟨hst t ht z hkz, fun e hb => ?_, fun e d' hs' => ?_, fun e r d' hs' => ?_, fun r hs' => ?_, + fun r => ?_, by rw [e_prio, kp]; exact hx.prio, by rw [e_fault, kf]; exact hx.fault0, + by rw [e_fault, kf]; exact hx.faultT, by rw [e_bound, kb]; exact hx.bound, ?_⟩⟩ + · rcases hs with h | ⟨-, h⟩ + · rw [e_blk]; exact hx.blk e (by rw [← h]; exact hb) + · rw [h] at hb; simp [epBlocked] at hb + · rcases hs with h | ⟨-, h⟩ + · rw [e_badge, e_flags]; exact hx.send e d' (h ▸ hs') + · rw [h] at hs'; cases hs' + · rcases hs with h | ⟨-, h⟩ + · rw [e_reply]; exact hx.recv e r d' (h ▸ hs') + · rw [h] at hs'; cases hs' + · rcases hs with h | ⟨-, h⟩ + · rw [e_reply]; exact hx.reply r (h ▸ hs') + · rw [h] at hs'; cases hs' + · by_cases e : t = d + · by_cases hr : r = badge_register + · subst hr + have : regA t (regIdxA badge_register) = badgeRegA d := by rw [e, badgeRegA_eq_regA] + rw [this] + exact hbd z (by rw [← e]; exact hkz) + · have hi : (regIdxA r).val ≠ 0 := fun h => hr (regIdxA_inj (h.trans h0.symm)) + have hv := regA_val hta (regIdxA_lt r) + have hl := regIdxA_lt r + rw [w _ (a_reg hta r) (by omega) (nb _ (by omega)), (keq (by rw [e])).1 r hr] + exact hx.regs r + · have hv := regA_val hta (regIdxA_lt r) + have hl := regIdxA_lt r + rw [w _ (a_reg hta r) (by omega) (fun h => absurd h e), (kne (fun h => e (specPtr_inj h))).1] + exact hx.regs r + · rw [kf] + exact hx.faultR.congr e_fault + (wf _ (a_fw0 hta) (by rw [val_fw0 hta]; omega) (by rw [val_fw0 hta]; omega)) + (wf _ (a_fw1 hta) (by rw [val_fw1 hta]; omega) (by rw [val_fw1 hta]; omega)) + (wf _ (a_lf hta) (by rw [val_lf hta]; omega) (by rw [val_lf hta]; omega)) + (wf _ (a_lf0 hta) (by rw [val_lf0 hta]; omega) (by rw [val_lf0 hta]; omega)) + (wf _ (a_lf1 hta) (by rw [val_lf1 hta]; omega) (by rw [val_lf1 hta]; omega)) + (wf _ (a_fw2 hta) (by rw [val_fw2 hta]; omega) (by rw [val_fw2 hta]; omega)) + · have hw : BufW Bf (bufW bp.1 k) := ⟨bp, hb, k, hk', rfl⟩ + rw [keep _ (a_buf hP.e hb hk').1 (a_buf hP.e hb hk').2 + (fun q hq => offTcbW hP.e (hQ q hq) (hP.e.bt _ hw q (hQ q hq))) (fun h => by + have := hP.e.bt _ hw d hdT + rw [h, bv] at this + omega), hk.um] + exact hR.buf bp hb k hk' + +set_option maxHeartbeats 1000000 in +/-- **`EpStateRel` across the wake of `d` by a notification** (module docstring). -/ +theorem EpStateRel.frameW {N T E R : Set U64} {Bf : Set (U64 × Word)} {m m' : U64 → U64} + {σ σ' : Fl4SpecA64.State Unit} (hR : EpStateRel T E R Bf m σ) (hP : IPlacedA N T E R Bf) {n d : U64} + (hn : n ∈ N) {Q : Set U64} (hQ : ∀ q ∈ Q, q ∈ T ∧ m (tBlockA q) = n) (hdQ : d ∈ Q) + (hm : ∀ a, Aligned a → EFoot T E R Bf a → ¬ SigWrA Q n a → a ≠ badgeRegA d → m' a = m a) + (hst : ∀ t ∈ T, ∀ z, σ'.kheap (specPtr t) = some (.tcb z) → (m' (stA t)).val = tsCode z.tcbState) + (hbd : ∀ z, σ'.kheap (specPtr d) = some (.tcb z) → + (m' (badgeRegA d)).bv = z.tcbArch.tcbContext.userRegs badge_register) + (hk : WakeKeepA σ σ' (specPtr d)) : EpStateRel T E R Bf m' σ' := by + refine EpStateRel.frameWG hR hP hn (fun q hq => (hQ q hq).1) hdQ (fun e he x hkx y hy hq h => ?_) hm hst hbd hk + obtain ⟨x', hkx', hx⟩ := hR.ep e he + rw [hkx] at hkx' + cases hkx' + have hb : m (tBlockA y) = e := (hx.blocked hq).2 + have e1 : e = n := hb.symm.trans (hQ y h).2 + have := hP.ne n hn e he + rw [e1] at this + omega + +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/Stage3EpiP1A.lean b/proofs/Fl4ProofsA64/P3/Stage3EpiP1A.lean new file mode 100644 index 000000000..1badae656 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3EpiP1A.lean @@ -0,0 +1,116 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Plan B): **`EpiP1A`, the P1 invariants after the success epilogue.** + +The epilogue's register writes are A64 P1's Reply-walk steps (`XJX.regT`, as `msg.reply_success_empty.s1a`), the +`TS_RUNNING` write its `DMD` step (`dmdT`, `RpPost.dmdK`); J7 comes from the fixed-witness step +(`S1aStepFix.ofXfer2`, `J7A.of_stepFixedA`). + +Named premises: [P] `P0.ArchTotal`, `ArchPlat`. New module. +-/ +import Fl4ProofsA64.P3.Stage3WrapA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4Spec Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +set_option maxHeartbeats 8000000 in +/-- **`EpiP1A` holds** (module docstring). -/ +theorem epiP1A {AR : A → HwRootA → Prop} {ar : machine.Arch A} {n : Nat} {m : Fl4SpecA64.McState} + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) (calling : Bool) : + EpiP1A st AR ar hPl.ipa n m calling := by + intro Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ s a σ J hw s2 e2 σX s' hrun + obtain ⟨Pt, Ub, Pp7, Tb7, Hr, Te, Kt, Lv7, Re, bits, KLv, W⟩ := J7A.unp J.j7 + have I := J.delIn hA hPl W + have hH := I.del.heap + have hO := I.del.os + have hc0 := J.core0 + have B := J.sys.sysl + have Y := J.symL + have h : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s := by + obtain ⟨q, b, x, sg, sc⟩ := J.sys.ent.bxi + exact ⟨q, b, x, sc, sg⟩ + have ht : L.curW s ∈ H.tcbs := by rw [J.tcbs]; exact J.sr.curT + set t := L.curW s with htdef + -- the register writes + have regs : RpPost st H Z.Cs OS Z.V ia ie Ac s s2 ∧ L.mem s2 (stA t) = sched.TS_RESTART := by + unfold epiRegsA at e2 + cases calling with + | false => + cases okinj e2 + exact ⟨(RpW.start B Y h).post, hw⟩ + | true => + rw [if_pos rfl] at e2 + have i0 : XKoQ st H Z.Cs OS ia ie Ac ∅ s s ∧ XV st H OS Z.V s s := + ⟨⟨⟨XJX.start B Y h, OSKeep.rfl' _⟩, TabKeep.refl s⟩, XV.refl s⟩ + have hR : ∀ s1 r v, (XKoQ st H Z.Cs OS ia ie Ac ∅ s s1 ∧ XV st H OS Z.V s s1) → r.val < 384 → + obj.Tcb.set_reg st t s1 r v ⦃ s3 => XKoQ st H Z.Cs OS ia ie Ac ∅ s s3 ∧ XV st H OS Z.V s s3 ⦄ := + fun s1 _ _ ⟨h1, x1⟩ hr' => by + have hvr1 : VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s1) Pt := by + rw [h1.2.1]; exact I.del.vr + exact WP.spec_mono (XJX.regT (Vc := Z.V) hH hO hc0 h1.1.1 hvr1 ht hr') + fun _ ⟨h3, k3, t3, a3⟩ => ⟨⟨⟨h3, OSKeep.trans' h1.1.2 k3⟩, h1.2.trans t3⟩, x1.trans (XV.keep a3 k3)⟩ + have walk : msg.reply_success_empty st s t ⦃ q => RpPost st H Z.Cs OS Z.V ia ie Ac s q ∧ + L.mem q (stA t) = sched.TS_RESTART ⦄ := by + unfold msg.reply_success_empty + refine WP.spec_bind (hR s _ 0#u64 i0 (by simp [layout.reg.BADGE, layout.reg.X0])) fun s1 h1 => ?_ + refine WP.spec_bind (P0.msginfo.MessageInfo.new.p0 _ _ _ _) fun mi _ => ?_ + unfold msg.set_msg_info + refine WP.spec_bind (P0.msginfo.MessageInfo.to_word.p0 _) fun w _ => ?_ + refine WP.spec_mono (hR s1 _ w h1 (by simp [layout.reg.MSG_INFO, layout.reg.X1])) fun q hf => ?_ + obtain ⟨⟨⟨Jx, k⟩, tk⟩, x⟩ := hf + refine ⟨RpW.post ⟨Jx.sys, Jx.sym, Jx.blk, XC.keep k tk, x, + (XferSlots.refl s).keep fun z hz => k.capA hz⟩, ?_⟩ + rw [stA_eq] at hw ⊢ + rw [Jx.fr t ht _ (by simp) (by simp) (by simp) (by simp)] + exact hw + exact post_of_okA (P := fun q => RpPost st H Z.Cs OS Z.V ia ie Ac s q ∧ L.mem q (stA t) = sched.TS_RESTART) + walk e2 + obtain ⟨P2, hw2⟩ := regs + -- the state write + have P2' := P2 + obtain ⟨B2, Y2, h2, -, xv2, -⟩ := P2' + have bx2 : BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s2 := by + obtain ⟨q, b, x, sc, sg⟩ := h2 + exact ⟨q, b, x, sg, sc⟩ + have bx3 := post_of_okA (sched.set_thread_state.running_bxi bx2 ht hw2) hrun + have d := post_of_okA (sched.set_thread_state.dmdT t sched.TS_RUNNING h2 B2.ly.h0 Y2 B2.vr ht + (by rw [hw2]; simp [sched.TS_RESTART, sched.TS_BLOCKED_ON_REPLY])) hrun + have h3 : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s' := by + obtain ⟨q, b, x, sg, sc⟩ := bx3 + exact ⟨q, b, x, sc, sg⟩ + have hp : ∀ k, H t = some k → ¬ (ia ≤ t.val ∧ t.val < ie) → ∃ x ∈ OS, NamesA (capA st s2 x) t k := by + intro k hk hia + have e : H t = some .tcb := ht + rw [e] at hk + cases hk + exact B2.ln t .tcb ht (LiveQ.tcb ht (Or.inl (by rw [hw2]; simp [sched.TS_RESTART, sched.TS_INACTIVE]))) hia + have hvr' := I.del.vr + obtain ⟨P3, k3⟩ := P2.dmdK hH hO I.del.sca hc0 hvr' J.ko hp d h3 + -- the fixed-witness step + have d3 := (fun q => ⟨q.1, q.2, P3.2.2.2.2.1.1, P3.2.2.2.2.2⟩ : + ArmQIAt st H Z.Cs OS Z.V ia ie s s' ∧ TabKeepOrZero st s s' → + ArmQIAt st H Z.Cs OS Z.V ia ie s s' ∧ TabKeepOrZero st s s' ∧ ArmVsK st H OS Z.V s s' ∧ + XferSlots2 st H OS Z.V s s') + (ArmQIAt.ofXC (Vc := Z.V) ⟨P3.1, P3.2.1⟩ ⟨Ac, P3.2.2.1⟩ P3.2.2.2.1 P3.2.2.2.2.1.1.toVs) + have Fx := S1aStepFix.ofXfer2 (n := hPl.ipa) d3 I.del.vr I.fu I.vcc W.col W.df W.r2 W.sep W.hiK + have hk : L.ksb s' = L.ksb s := P3.2.2.2.2.1.1.1 + have hPN : PoolNamed st OS s' := d3.1.2.2.2.2.2.1 W.pn + have hsp : ∀ p, SchedPageA st Z OS s' p → p ∈ Pt := by + have Fx' := Fx + obtain ⟨_, -, -, -, ⟨_, _, _, _, _, _, _, R'⟩, -⟩ := Fx' + exact schedPage_ptA R' W.sp + have sym : SymL st H Z.Cs s' := P3.2.1 + refine ⟨P3.1, sym, bx3, J.sys.ent.user.ksb hk, ?_⟩ + exact J7A.of_stepFixedA Fx W.vr hk W.al W.vu hPN hsp (fun b hb o ho => by rw [hk]; exact W.ks b hb o ho) + (J7R2K.of_stepFixedA (σ' := σX) (m := m) Fx W.ktK) + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3EpilogueA.lean b/proofs/Fl4ProofsA64/P3/Stage3EpilogueA.lean new file mode 100644 index 000000000..7ff873f50 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3EpilogueA.lean @@ -0,0 +1,565 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Plan B step 3): **the success epilogue of `handle_invocation` at the record**. + +A call still `Restart` after `decode_invocation` gets the empty reply (`reply_success_empty`; spec +`reply_from_kernel (0, [])`), and every invocation still `Restart` is set `Running`. + +* `JStage3A.runRestartA`: the record across caller register writes (any `s2` with the IPC views at the spec's new user + context) followed by `set_thread_state Running`. The caller's scheduling context is read from `SchedValidA` when + resuming; with a switch pending the state write alone is `set_thread_state`. +* `BodyOKA.runA`: the body-end facts across that step. +* `JStage3A.epilogueA`: both forms of the epilogue (call and not). + +The record walk of `runRestartA` is lift 3's `replyOneA` made generic in the register writes +(`handoff/tools/gen_planb_epilogue.py`). + +Named premises: [P1] `fin` (`SysL`, `SymL`, `BXI`, `UserOK`, `J7A` at the final state). + +New module. +-/ +import Fl4ProofsA64.P3.Stage3IpcGlueA +import Fl4ProofsA64.P3.FrReplyA +import Fl4ProofsA64.P3.TcbRegViewA +import Fl4ProofsA64.P3.RestartA +import Fl4ProofsA64.P3.EpXferA +import Fl4ProofsA64.P3.Stage3RestartA +import Fl4ProofsA64.P3.UtTcbCreateA +import Fl4ProofsA64.P1.MdbVsMapA +import Fl4ProofsA64.P1.MdbVsGaQ +import Fl4ProofsA64.P1.QueueDecDel +import Fl4ProofsA64.P3.Stage3McsFrA +import Fl4ProofsA64.P3.Stage3BodyOKA +import Fl4ProofsA64.P1.QueueCallers +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-- The caller's user context after the empty reply. -/ +def emptyUcA (x : Fl4SpecA64.Tcb) : Fl4SpecA64.UserContext := + ⟨x.tcbArch.tcbContext.userFpuState, + Function.update (Function.update x.tcbArch.tcbContext.userRegs Fl4SpecA64.badge_register 0) + Fl4SpecA64.msg_info_register (messageInfoToData ⟨natToLen 0, 0, 0, 0⟩)⟩ + +/-- Spec `reply_from_kernel (0, [])`. -/ +theorem replyFromKernel_emptyA {σ : State Unit} {t : ObjRef} {x : Tcb} (hk : σ.kheap t = some (.tcb x)) : + replyFromKernel (Z := Unit) t (0, []) σ = ({((), ctxUpdA σ t x (emptyUcA x))}, False) := by + obtain ⟨buf, hbuf⟩ : ∃ buf, lookupIpcBuffer (Z := Unit) true t σ = ({(buf, σ)}, False) := + ⟨_, lookupIpcBuffer_oneA hk⟩ + have h1 := asUser_setReg_oneA hk badge_register 0 + set uc1 : UserContext := ⟨x.tcbArch.tcbContext.userFpuState, + Function.update x.tcbArch.tcbContext.userRegs badge_register 0⟩ with huc1 + set x1 : Tcb := { x with tcbArch := { x.tcbArch with tcbContext := uc1 } } with hx1 + have hk1 : (ctxUpdA σ t x uc1).kheap t = some (.tcb x1) := by simp [ctxUpdA, setObjA, x1] + have h2 : setMrs (Z := Unit) t buf [] (ctxUpdA σ t x uc1) = ({(natToLen 0, ctxUpdA σ t x uc1)}, False) := by + have hg : NonDet.getsThe (getTcb (Z := Unit) t) (ctxUpdA σ t x uc1) = ({(x1, ctxUpdA σ t x uc1)}, False) := by + simp [NonDet.getsThe, getTcb, readObject, hk1, NonDet.assertOpt]; rfl + have ho : getObject (Z := Unit) t (ctxUpdA σ t x uc1) = ({(.tcb x1, ctxUpdA σ t x uc1)}, False) := by + simp [getObject, NonDet.getsThe, readObject, hk1, NonDet.assertOpt]; rfl + set newRegs : Register → Word := fun reg => + if reg ∈ msg_registers.take ([] : List Message).length then the ([] : List Message)[theIndex msg_registers reg]? + else archTcbGetRegisters x1.tcbArch reg with hn + have en : archTcbSetRegisters newRegs x1.tcbArch = x1.tcbArch := by + have : newRegs = archTcbGetRegisters x1.tcbArch := by + funext r; simp [hn] + rw [this]; rfl + have hso : setObject (Z := Unit) t (.tcb { x1 with tcbArch := archTcbSetRegisters newRegs x1.tcbArch }) + (ctxUpdA σ t x uc1) = ({((), ctxUpdA σ t x uc1)}, False) := by + rw [en] + exact setObject_selfA hk1 + unfold setMrs + rw [nd_bind_oneA hg, nd_bind_oneA (show (pure (archTcbGetRegisters x1.tcbArch) : SMonad Unit _) _ = _ from rfl), + nd_bind_oneA (show (pure newRegs : SMonad Unit _) _ = _ from rfl), nd_bind_oneA hso, + nd_bind_oneA (show (pure (([] : List Message).drop msg_registers.length) : SMonad Unit _) _ = _ from rfl)] + cases buf with + | none => + have : min msg_registers.length ([] : List Message).length = 0 := by simp + rw [this]; rfl + | some pptr => + dsimp only + have hz : NonDet.zipWithMx (fun x => storeWordOffs (Z := Unit) pptr x) + (List.range' (msg_registers.length + 1) (msgMaxLength + 1 - (msg_registers.length + 1))) + (([] : List Message).drop msg_registers.length) (ctxUpdA σ t x uc1) = + ({((), ctxUpdA σ t x uc1)}, False) := by + simp [NonDet.zipWithMx]; rfl + rw [nd_bind_oneA hz] + have : min ([] : List Message).length msgMaxLength = 0 := by simp + rw [this]; rfl + have h3 := asUser_setReg_oneA hk1 msg_info_register (messageInfoToData ⟨natToLen 0, 0, 0, 0⟩) + have heq : ctxUpdA (ctxUpdA σ t x uc1) t x1 + ⟨x1.tcbArch.tcbContext.userFpuState, + Function.update x1.tcbArch.tcbContext.userRegs msg_info_register (messageInfoToData ⟨natToLen 0, 0, 0, 0⟩)⟩ = + ctxUpdA σ t x (emptyUcA x) := by + simp only [ctxUpdA, setObjA, Function.update_idem, emptyUcA, x1, uc1] + unfold replyFromKernel setMessageInfo + have hp : (pure ((0 : Data), ([] : List Data)) : SMonad Unit (Data × List Data)) σ = + ({(((0 : Data), []), σ)}, False) := rfl + rw [nd_bind_oneA hp] + dsimp only + rw [nd_bind_oneA hbuf, nd_bind_oneA h1, nd_bind_oneA h2, h3, heq] + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-- With a switch pending (`SCHED_ACTION` not `resume`) `schedule_tcb` does nothing. -/ +theorem schedule_tcb_pendingA (s : S) (t : U64) (h : L.actionW s ≠ 0#u64) : sched.schedule_tcb st s t = ok s := by + unfold sched.schedule_tcb + rw [eq_of_spec_fd (sched.cur_thread.spec (L := L) s)]; simp only [bind_ok] + rw [eq_of_spec_fd (obj.Tcb.eq.spec _ _)]; simp only [bind_ok] + split + · rw [eq_of_spec_fd (Fl4ProofsA64.P1.sched.action.spec (L := L) s)]; simp only [bind_ok] + rw [if_neg (show ¬ L.actionW s = sched.ACTION_RESUME_CURRENT by unfold sched.ACTION_RESUME_CURRENT; exact h)] + · rfl + +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {ar : machine.Arch A} {nA : U64} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- **The record across caller register writes and `set_thread_state Running`** (module docstring). -/ +theorem JStage3A.runRestartA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {t : U64} (ht : L.curW s = t) {x : Fl4SpecA64.Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hr : x.tcbState = .restart) (uc : Fl4SpecA64.UserContext) {s2 : S} + (V2 : TcbIpcViewsA st Z m κ N E Rp Bf stack s2 + (setObjA σ (specPtr t) (.tcb { x with tcbArch := { x.tcbArch with tcbContext := uc } }))) + (w2 : WritesIn st s s2 (ScConsumedRegsA t)) + (fin : ∀ σX : Fl4SpecA64.State Unit, + let s' := L.wr s2 (stA t) sched.TS_RUNNING + sched.set_thread_state st s2 t sched.TS_RUNNING = ok s' → + SysL st H Z.Cs OS ia ie s' ∧ SymL st H Z.Cs s' ∧ + BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s' ∧ + Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s' ∧ J7A st (J7R2A st ar nA) Z H OS s' σX m) : + sched.set_thread_state st s2 t sched.TS_RUNNING = ok (L.wr s2 (stA t) sched.TS_RUNNING) ∧ + ∃ σ', setThreadState (Z := Unit) (specPtr t) .running + (setObjA σ (specPtr t) (.tcb { x with tcbArch := { x.tcbArch with tcbContext := uc } })) = + ({((), σ')}, False) ∧ + σ' = setObjA σ (specPtr t) (.tcb { x with tcbArch := { x.tcbArch with tcbContext := uc }, tcbState := .running }) ∧ + ∃ Q Rl, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) (L.wr s2 (stA t) sched.TS_RUNNING) a σ' m := by + have ct : σ.curThread = specPtr t := ht ▸ J.j.1.sch.curThread + have htZ : t ∈ Z.T := ht ▸ J.sr.curT + have hta : TcbAddr t := J.sr.core.tcbs t htZ + have hct : t ∈ H.tcbs := by rw [J.tcbs]; exact htZ + have q := J.sq + have hbx := J.sys.ent.bxi + obtain ⟨-, -, hX, -, hS⟩ := hbx + set x1 : Fl4SpecA64.Tcb := { x with tcbArch := { x.tcbArch with tcbContext := uc } } with hx1 + set σ1 := setObjA σ (specPtr t) (.tcb x1) with hσ1 + set x3 : Fl4SpecA64.Tcb := { x1 with tcbState := .running } with hx3 + set σ' := stUpdA σ1 (specPtr t) x1 .running with hσ' + have hk1 : σ1.kheap (specPtr t) = some (.tcb x1) := by simp [σ1, setObjA] + have e' : σ' = setObjA σ (specPtr t) (.tcb x3) := by + simp only [σ', σ1, stUpdA, setObjA, Function.update_idem, x3] + have far : ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → K.val ≠ 256 → K.val ≠ 264 → K.val ≠ 272 → + L.mem s2 (wa t K) = L.mem s (wa t K) := by + intro K h1 h2 h3 h4 h5 + refine w2.mem_eq (field_aligned hta h1 h2) fun h => ?_ + rcases h with h | h | h + · exact field_ne hta hta h1 (by simp) (fun _ => by simpa using h3) h + · exact field_ne hta hta h1 (by simp) (fun _ => by simpa using h4) h + · exact field_ne hta hta h1 (by simp) (fun _ => by simpa using h5) h + have T2 : StEqA Z.T (L.mem s2) σ1 := fun u hu y hy => (V2.ep.at_tcb hu hy).st + obtain ⟨Q2, R2, H2⟩ := id V2.rel + have hni : specPtr t ≠ σ.idleThread := fun e => by + obtain ⟨y, hy, hs⟩ := J.sinv.idleTcb + rw [← e, hk] at hy; cases hy + rw [hr] at hs; cases hs + -- the state write and the spec's, resuming or not + have key : sched.set_thread_state st s2 t sched.TS_RUNNING = ok (L.wr s2 (stA t) sched.TS_RUNNING) ∧ + (specPtr t = σ1.curThread → σ1.schedulerAction = .resumeCurThread → + schedulable (specPtr t) (setObjA σ1 (specPtr t) (.tcb x3)) = true) := by + by_cases res : σ.schedulerAction = .resumeCurThread + · have hne : σ.curThread ≠ σ.idleThread := by rw [ct]; exact hni + obtain ⟨⟨y, p, ey, es, ysc, nsc, hysc, hact⟩, hnr⟩ := J.schedValid.act res hne + rw [ct, hk] at ey; cases ey + rw [ct] at hnr + obtain ⟨y, hy, -, hsx⟩ := J.j.1.sch.tcb t htZ + rw [hk] at hy; cases hy + have hsc : L.mem s (scA t) ≠ 0#u64 := fun e => by + rw [hsx] at es; unfold optScA at es; rw [if_pos e] at es; cases es + set w := L.mem s (scA t) with hw + have ep : p = scRenA m w := by + rw [hsx] at es; unfold optScA at es; rw [if_neg hsc] at es; exact (Option.some.inj es).symm + subst ep + have hwC : w ∈ Z.C := J.j.1.rest.scMem t htZ hsc + have hwH : w ∈ H.scs := by rw [J.scs]; exact hwC + have hrm : L.rmax s w ≠ 0#u64 := by + obtain ⟨y', k', ey, eyr⟩ := J.j.1.rest.refillMax w hwC + rw [hysc] at ey; cases ey + have e3 : L.rmax s w = L.mem s (wa w 72#u64) := by unfold LawfulStore.rmax; rw [maxA_eqA] + intro e + rw [e3] at e + have : ysc.scRefillMax = 0 := by rw [eyr, e]; rfl + simp [scActive, activeSc, this] at hact + have hmΩ : maxA w ∈ H.omega := hS.sub _ (hS.all _ hwH).2.1 + have hma : Aligned (maxA w) := (q.omega _ hmΩ).1 + have hsc2 : L.mem s2 (scA t) = w := far 3464#u64 (by simp) (by simp) (by simp) (by simp) (by simp) + have hiq : (L.mem s (inqA t) &&& sched.Q_RELEASE) = 0#u64 := + (J.sr.toSchedSRN.relOn htZ (ht ▸ J.sr.curOn)).2 hnr + have hiq2 : (L.mem s2 (inqA t) &&& sched.Q_RELEASE) = 0#u64 := by + show (L.mem s2 (wa t 3592#u64) &&& sched.Q_RELEASE) = 0#u64 + rw [far 3592#u64 (by simp) (by simp) (by simp) (by simp) (by simp)]; exact hiq + have hrm2 : L.rmax s2 w ≠ 0#u64 := by + show L.mem s2 (maxA w) ≠ 0#u64 + rw [w2.mem_eq hma fun h => ?_] + · exact hrm + rcases h with h | h | h <;> + exact out_ne_field (q.omega _ hmΩ) hct hta (by simp) (congrArg UScalar.val h.symm) + have hmx : stA t ≠ maxA w := fun h => + out_ne_field (q.omega _ hmΩ) hct hta (K := 3328#u64) (by simp) (by rw [← stA_eq]; exact congrArg UScalar.val h) + refine ⟨set_thread_state_run_wrA hta (by rw [hsc2]; exact hsc) (by rw [hsc2]; exact hrm2) hiq2 + (by rw [hsc2]; exact hma) (by rw [hsc2]; exact hmx), fun _ _ => ?_⟩ + have hscx : x.tcbSchedContext = some (scRenA m w) := by + rw [hsx]; unfold optScA; rw [if_neg hsc] + have ne : scRenA m w ≠ specPtr t := fun e => by rw [e, hk] at hysc; cases hysc + refine schedulable_ofA (x := x3) (n := nsc) (by simp [setObjA]) rfl hscx (y := ysc) ?_ ?_ hnr + · simpa [σ1, setObjA, Function.update_of_ne ne] using hysc + · have := hact; simp [scActive, activeSc] at this; omega + · have res1 : σ1.schedulerAction ≠ .resumeCurThread := res + have a2 : L.actionW s2 ≠ 0#u64 := fun e => by + have h := H2.sch.act + rw [e] at h + exact res1 (h.trans (by unfold actOfA; rfl)) + refine ⟨?_, fun _ h => absurd h res1⟩ + unfold sched.set_thread_state + rw [eq_of_spec_fd (obj.Tcb.set_state.spec (L := L) s2 t _)] + simp only [bind_ok] + refine schedule_tcb_pendingA _ _ ?_ + have hN : NodePlaced st H.tcbs s2 (L.cpu s2) := by + have h := q.nodes _ q.cur + have hnb : ∀ c, L.nodeBase s2 c = L.nodeBase s c := fun c => by simp [LawfulStore.nodeBase, w2.1] + rw [w2.2.1] + exact ⟨by rw [hnb]; exact h.align, by rw [hnb]; exact h.fits, fun u hu => by rw [hnb]; exact h.disj u hu⟩ + rw [act_wr_field (K := 3328#u64) hN hct hta (stA_eq t) (by simp) (by simp)] + exact a2 + obtain ⟨eRun, hschd⟩ := key + set s' := L.wr s2 (stA t) sched.TS_RUNNING with hs' + have hset : setThreadState (Z := Unit) (specPtr t) .running σ1 = ({((), σ')}, False) := + setThreadState_eqA σ1 (specPtr t) x1 hk1 .running hschd + -- the record + have sA : Aligned (stA t) := by rw [stA_eq]; exact field_aligned hta (by simp) (by simp) + have wAll : WritesIn st s s' (fun b => ScConsumedRegsA t b ∨ b = stA t) := + (w2.trans (writesIn_wr sA (v := sched.TS_RUNNING))).mono fun _ h => h + have wN : WritesIn st s s' (SchedStepNWA (st := st) N E Rp Z m σ s) := by + refine wAll.mono fun b h => ?_ + rcases h with h | h + · exact consumedRegs_stepNA J.sr.core.tcbs htZ h + · rw [h]; exact stA_stepNA J.sr.core.tcbs htZ + have tcbW : ∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → K.val ≠ 256 → K.val ≠ 264 → K.val ≠ 272 → + K.val ≠ 3328 → L.mem s' (wa u K) = L.mem s (wa u K) := by + intro u hu K h1 h2 h3 h4 h5 h6 + have hua := J.sr.core.tcbs u hu + refine wAll.mem_eq (field_aligned hua h1 h2) fun h => ?_ + rcases h with (h | h | h) | h + · exact field_ne hua hta h1 (by simp) (fun _ => by simpa using h3) h + · exact field_ne hua hta h1 (by simp) (fun _ => by simpa using h4) h + · exact field_ne hua hta h1 (by simp) (fun _ => by simpa using h5) h + · rw [stA_eq] at h + exact field_ne hua hta h1 (by simp) (fun _ => by simpa using h6) h + have same : ∀ p, p ≠ specPtr t → σ'.kheap p = σ.kheap p := fun p e => by + rw [e']; simp [setObjA, Function.update_of_ne e] + have atT : σ'.kheap (specPtr t) = some (.tcb x3) := by rw [e']; simp [setObjA] + have keepK : ∀ p ob, σ.kheap p = some ob → (∀ y, ob ≠ .tcb y) → σ'.kheap p = some ob := fun p ob h ho => by + have e : p ≠ specPtr t := fun e => by rw [e, hk] at h; exact ho x (Option.some.inj h).symm + rw [same p e]; exact h + have backK : ∀ p ob, σ'.kheap p = some ob → (∀ y, ob ≠ .tcb y) → σ.kheap p = some ob := fun p ob h ho => by + by_cases e : p = specPtr t + · rw [e, atT] at h; exact absurd (Option.some.inj h).symm (ho x3) + · rw [← same p e]; exact h + have tcbK : ∀ p y, σ.kheap p = some (.tcb y) → ∃ y', σ'.kheap p = some (.tcb y') ∧ + y'.tcbSchedContext = y.tcbSchedContext ∧ y'.tcbFault = y.tcbFault ∧ y'.tcbIpcframe = y.tcbIpcframe ∧ + y'.tcbIpcBuffer = y.tcbIpcBuffer ∧ y'.tcbMcpriority = y.tcbMcpriority ∧ y'.tcbYieldTo = y.tcbYieldTo ∧ y'.tcbFlags = y.tcbFlags ∧ + (p ≠ specPtr t → y' = y) ∧ (p = specPtr t → y'.tcbState = .running) := by + intro p y hy + by_cases e : p = specPtr t + · rw [e, hk] at hy; cases hy + exact ⟨x3, e ▸ atT, rfl, rfl, rfl, rfl, rfl, rfl, rfl, fun h => absurd e h, fun _ => rfl⟩ + · exact ⟨y, by rw [same p e]; exact hy, rfl, rfl, rfl, rfl, rfl, rfl, rfl, fun _ => rfl, fun h => absurd h e⟩ + have HS : SpecSchedA σ σ' := + { kh := fun p => by + by_cases e : p = specPtr t + · refine Or.inr (Or.inl ⟨x, x3, e ▸ hk, e ▸ atT, ⟨rfl, rfl, rfl⟩⟩) + · exact Or.inl (same p e) + irqS := by rw [e']; rfl + irqN := by rw [e']; rfl + asid := by rw [e']; rfl + vcur := by rw [e']; rfl + cdt := by rw [e']; rfl + orig := by rw [e']; rfl } + have SSz : SSzA σ σ' := fun p y k h => ⟨y, backK p _ h (fun _ e => by cases e)⟩ + have K : IpcKeepA σ σ' := + ⟨fun p y h => by + obtain ⟨y', h', -, f, g1, g2, g3, g4, g5, -⟩ := tcbK p y h + exact ⟨y', h', f, g1, g2, g3, ⟨g4, g5⟩⟩, + fun p y k h => ⟨y, k, keepK p _ h (fun _ e => by cases e), rfl⟩, + fun p y k h => ⟨y, k, backK p _ h (fun _ e => by cases e), rfl⟩⟩ + obtain ⟨sysl, symL, bxi', userOK, j7⟩ := fin σ' eRun + have T2 : StEqA Z.T (L.mem s2) σ1 := fun u hu y hy => (V2.ep.at_tcb hu hy).st + obtain ⟨Q2, R2, H2⟩ := id V2.rel + have sr : SchedSRA st { Z with Q := Q2, R := R2 } m κ s' σ' := by + have hvn : sched.TS_RUNNING.val = tsCodeA .running := by rw [ts_vals.1]; rfl + have h := sc_set_runnable_stateA (Z := { Z with Q := Q2, R := R2 }) H2 T2 htZ hk1 + (show runnable x1.tcbState = true by simp [x1, hr, runnable]) (ts := .running) rfl hvn + rw [eRun, WP.spec_ok] at h + obtain ⟨τ, hτ, Hs, -, -, -⟩ := h + have e : τ = σ' := by + rw [hset] at hτ + exact ((Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst hτ))).2).symm + rw [e] at Hs + exact Hs + have V2ep := V2.ep + have views : EpStateRel Z.T E Rp Bf (L.mem s') σ' ∧ NtfnStateRel N Z.T (L.mem s') σ' := by + have notQ : ∀ e ∈ E, ∀ z, σ1.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue := by + intro e he z hz hq + obtain ⟨z', hz', rel⟩ := V2.ep.ep e he + rw [hz] at hz' + obtain rfl := KernelObject.endpoint.inj (Option.some.inj hz') + obtain ⟨qs, _, hqs, members⟩ := rel.queue + rw [hqs, List.mem_map] at hq + obtain ⟨u, hu, hEq⟩ := hq + have eu : u = t := specPtr_inj hEq + subst u + have state := (members t hu).2 + rw [(V2.ep.at_tcb htZ hk1).st] at state + simp [x1, hr] at state + exact absurd state (by decide) + have code : sched.TS_RUNNING.val = Notif.tsCode .running := by rw [ts_vals.1]; rfl + have eσ : Notif.updK σ1 (specPtr t) (.tcb { x1 with tcbState := .running }) = σ' := rfl + refine ⟨?_, ?_⟩ + · have h := (Endp.write_state_rel J.iplaced.e s2 σ1 V2.ep htZ hk1 notQ .running (by decide) _ code).1 + rw [eσ] at h; exact h + · obtain ⟨writes, kept⟩ := Notif.twr_wr (st := st) J.iplaced.n htZ (K := 3328#u64) (Or.inl rfl) s2 + sched.TS_RUNNING + rw [← stA_eq] at writes kept + have nt := V2.ntfn.at_tcb htZ hk1 + have h : NtfnStateRel N Z.T (L.mem s') (Notif.updK σ1 (specPtr t) (.tcb { x1 with tcbState := .running })) := by + refine V2.ntfn.upd_tcb htZ _ writes.ntfn writes.tcb writes.links (Or.inl ?_) ?_ + · intro q hq z hz hm + obtain ⟨z', hz', rel⟩ := V2.ntfn.ntfn q hq + rw [hz] at hz' + obtain rfl := KernelObject.notification.inj (Option.some.inj hz') + have blocked := (rel.obj.blocked hm).2.1 + rw [nt.st] at blocked + simp [x1, hr] at blocked + exact absurd blocked (by decide) + · refine ⟨?_, (fun _ h => by cases h), ?_, ?_⟩ + · rw [L.mem_wr_same (Notif.foot_st J.iplaced.n htZ).1]; exact code + · show (L.mem s' (Notif.badgeRegA t)).bv = Notif.badgeOf x1 + rw [Notif.badgeRegA_eq, kept _ (by simp) (by simp) (by simp), ← Notif.badgeRegA_eq] + exact nt.badge + · show (L.mem s' (prioA t)).val = x1.tcbPriority.toNat + rw [Notif.prioA_eq', kept _ (by simp) (by simp) (by simp), ← Notif.prioA_eq'] + exact nt.prio + exact h + have reply : ReplyStackRelA (L.mem s') Rp Z.C σ' (scNameA m) stack := by + refine V2.stk.frame (fun r hr' => ?_) (fun c _ y k h => ⟨y, k, ?_, rfl⟩) (fun r _ x' h => ⟨x', ?_, rfl⟩) + · have hr3 := J.iplaced.e.rep r hr' + have hdis := J.iplaced.e.rt r hr' t htZ + have key : ∀ o : U64, o.val = 8 ∨ o.val = 16 → L.mem s' (wa r o) = L.mem s2 (wa r o) := by + intro o ho + have hv : (wa r o).val = r.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + refine mem_wr_ne' sA (by unfold Aligned; rw [hv]; omega) ?_ + intro e + have ev := congrArg UScalar.val e + rw [stA_eq, Notif.tfield_val hta (by decide : (3328#u64 : U64).val < 4096), hv] at ev + simp at ev + omega + exact ⟨key _ (Or.inr (by simp [layout.reply.NEXT])), key _ (Or.inl (by simp [layout.reply.PREV]))⟩ + · have e : scNameA m c ≠ specPtr t := fun e => by rw [e, hk1] at h; cases h + rw [same _ e] + simpa [σ1, setObjA, Function.update_of_ne e] using h + · have e : specPtr r ≠ specPtr t := fun e => by + rw [show σ'.kheap (specPtr r) = some (.tcb x3) by rw [e]; exact atT] at h; cases h + rw [same _ e] at h + simpa [σ1, setObjA, Function.update_of_ne e] using h + have hni : specPtr t ≠ σ.idleThread := fun e => by + obtain ⟨y, hy, hs⟩ := J.sinv.idleTcb + rw [← e, hk] at hy; cases hy + rw [hr] at hs; cases hs + have sinv : SInvXA σ' m n := by + have h1 : SInvXA σ1 m n := by + have same1 : ∀ p, p ≠ specPtr t → σ1.kheap p = σ.kheap p := fun p e => by + simp [σ1, setObjA, Function.update_of_ne e] + refine J.sinv.of_fr + { tcb := fun p y h => ?_, tcb' := fun p y h => ?_, sc := fun p y k => ?_, cur := rfl, idle := rfl, csc := rfl, + rq := rfl, rls := rfl, act := rfl, time := rfl, cons := rfl } (J.sinv.irqN.updTcbA hk rfl) + · by_cases e : p = specPtr t + · rw [e, hk] at h; cases h + exact ⟨x1, e ▸ hk1, rfl, rfl, rfl⟩ + · exact ⟨y, by rw [same1 p e]; exact h, rfl, rfl, rfl⟩ + · by_cases e : p = specPtr t + · rw [e, hk1] at h; cases h + exact ⟨x, e ▸ hk, rfl, rfl, rfl⟩ + · exact ⟨y, by rw [← same1 p e]; exact h, rfl, rfl, rfl⟩ + · by_cases e : p = specPtr t + · rw [e, hk1, hk] + constructor <;> intro h <;> cases h + · rw [same1 p e] + exact h1.restartA hk1 hni (ts := .running) rfl (fun h => by cases h) (by by_contra hf; have := J.sinv.faultSt.faultNotRun _ x hk hf; rw [hr] at this; exact absurd this (by decide)) + have sidle : SIdleA σ' m n := by + have hidle : ∀ c < n, m.idleThreadOf c ≠ specPtr t := fun c hc e => by + obtain ⟨y, hy, hs⟩ := J.sidle.f3 c hc + rw [e, hk] at hy; cases hy + rw [hr] at hs; cases hs + refine + { f3 := fun c hc => by + obtain ⟨y, hy, hs⟩ := J.sidle.f3 c hc + exact ⟨y, by rw [same _ (hidle c hc)]; exact hy, hs⟩ + f4 := fun y k h => J.sidle.f4 y k (backK _ _ h (fun _ e => by cases e)) + f5 := fun p y k h he => J.sidle.f5 p y k (backK _ _ h (fun _ e => by cases e)) (by rw [e'] at he; exact he) + f6 := J.sidle.f6.of_eq fun sl => by + unfold specCapAtA + by_cases e : sl.1 = specPtr t + · rw [e, atT, hk] + try rfl + · rw [same _ e] + f7 := fun y k h => J.sidle.f7 y k (backK _ _ h (fun _ e => by cases e)) + idleSlot := fun c hc => by + have h0 := J.sidle.idleSlot c hc + unfold vtCapA at h0 ⊢ + rw [same _ (hidle c hc)]; exact h0 + idleBound := fun c hc => by + have h0 := J.sidle.idleBound c hc + unfold vcpuOfA at h0 ⊢ + rw [same _ (hidle c hc)]; exact h0 } + have runVcpu : RunVcpuInv st Z.T s' := fun u hu h0 => by + have rvw : L.mem s' (runVcpuA u) = L.mem s (runVcpuA u) := + tcbW u hu 3672#u64 (by simp) (by simp) (by simp) (by simp) (by simp) (by simp) + rw [rvw] at h0 + have ne : u ≠ t := fun e => by + have h1 := J.runVcpu u hu h0 + have code := (J.ep.at_tcb htZ hk).st + rw [hr] at code + rw [e] at h1 + rw [h1] at code + simp [sched.TS_RUNNING_VM] at code + exact absurd code (by decide) + have hs : L.mem s' (stA u) = L.mem s (stA u) := by + have hua := J.sr.core.tcbs u hu + rw [stA_eq] + refine wAll.mem_eq (field_aligned hua (by simp) (by simp)) fun h => ?_ + rcases h with (h | h | h) | h + · exact field_ne hua hta (by simp) (by simp) (fun e => absurd e ne) h + · exact field_ne hua hta (by simp) (by simp) (fun e => absurd e ne) h + · exact field_ne hua hta (by simp) (by simp) (fun e => absurd e ne) h + · rw [stA_eq] at h + exact field_ne hua hta (by simp) (by simp) (fun e => absurd e ne) h + rw [hs] + exact J.runVcpu u hu h0 + have blkQ : BlkQueuedA Z.T E N (L.mem s') σ' := by + have tb : ∀ u ∈ Z.T, L.mem s' (tBlockA u) = L.mem s (tBlockA u) := fun u hu => + tcbW u hu 3336#u64 (by simp) (by simp) (by simp) (by simp) (by simp) (by simp) + have old : ∀ u, ∀ y, σ'.kheap (specPtr u) = some (.tcb y) → y.tcbState ≠ .running → + σ.kheap (specPtr u) = some (.tcb y) := fun u y h hn => by + by_cases e : specPtr u = specPtr t + · rw [e, atT] at h; cases h; exact absurd rfl hn + · rw [← same _ e]; exact h + refine ⟨fun u hu y h e hb => ?_, fun u hu y h p hb => ?_⟩ + · have hn : y.tcbState ≠ .running := fun h' => by rw [h'] at hb; cases hb + obtain ⟨c, z, hz, hm⟩ := J.blkQ.ep u hu y (old u y h hn) e hb + exact ⟨by rw [tb u hu]; exact c, z, keepK _ _ hz (fun _ e => by cases e), hm⟩ + · have hn : y.tcbState ≠ .running := fun h' => by rw [h'] at hb; cases hb + obtain ⟨c, z, l, hz, hl, hm⟩ := J.blkQ.ntfn u hu y (old u y h hn) p hb + exact ⟨by rw [tb u hu]; exact c, z, l, keepK _ _ hz (fun _ e => by cases e), hl, hm⟩ + have sv : SchedValidA σ' := by + have sc : ∀ p, ScActiveAtA σ p → ScActiveAtA σ' p := fun p ⟨y, k, e, act⟩ => + ⟨y, k, keepK p _ e (fun _ h => by cases h), act⟩ + have tcbAct : ∀ u, (∃ y c, σ.kheap u = some (.tcb y) ∧ y.tcbSchedContext = some c ∧ ScActiveAtA σ c) → + ∃ y c, σ'.kheap u = some (.tcb y) ∧ y.tcbSchedContext = some c ∧ ScActiveAtA σ' c := + fun u ⟨y, c, h, es, act⟩ => by + obtain ⟨y', h', es', -⟩ := tcbK u y h + exact ⟨y', c, h', es'.trans es, sc _ act⟩ + have hq : σ'.readyQueues = σ.readyQueues := by rw [e']; rfl + have hrl : σ'.releaseQueue = σ.releaseQueue := by rw [e']; rfl + have hact : σ'.schedulerAction = σ.schedulerAction := by rw [e']; rfl + have hc : σ'.curThread = σ.curThread := by rw [e']; rfl + have hi : σ'.idleThread = σ.idleThread := by rw [e']; rfl + refine ⟨fun d p u hm => ?_, fun e1 e2 => ?_, fun u y c e1 e2 e3 => ?_, fun u hu => ?_⟩ + · rw [hq] at hm + obtain ⟨act, nr⟩ := J.schedValid.rq d p u hm + exact ⟨tcbAct u act, by rw [hrl]; exact nr⟩ + · rw [hact] at e1 + rw [hc, hi] at e2 + obtain ⟨act, nr⟩ := J.schedValid.act e1 e2 + rw [hc, hrl] + exact ⟨tcbAct _ act, nr⟩ + · by_cases h : u = specPtr t + · rw [h, atT] at e1; cases e1; simp [x3, BlockedTsA] at e2 + · rw [same u h] at e1 + exact sc _ (J.schedValid.blk u y c e1 e2 e3) + · rw [hrl] at hu + exact tcbAct u (J.schedValid.rls u hu) + refine ⟨eRun, σ', hset, e', Q2, R2, ?_⟩ + exact J.of_ipcStepA (Q := Q2) (Rl := R2) (curOf := Z.curOf) (curScOf := Z.curScOf) wN HS SSz + (by rw [e']; exact QKeepA.upd hk (QObjA.ofTcb rfl)) + (by rw [e']; rfl) (fun _ _ => rfl) bxi' sysl symL userOK + j7 sr views.1 views.2 reply K + (fun u hu O h h8 => tcbW u hu O (by omega) h8 (by omega) (by omega) (by omega) (by omega)) + sinv sidle runVcpu blkQ sv + +end + +section +variable {σ σ' : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- **The body-end facts across the success epilogue**: the current thread goes from `Restart` to `Running` (its +register context free), nothing else changes. -/ +theorem Sched.BodyOKA.runA (B : BodyOKA σ m) {x : Fl4SpecA64.Tcb} (hk : σ.kheap σ.curThread = some (.tcb x)) + (hr : x.tcbState = .restart) {uc : Fl4SpecA64.UserContext} + (e : σ' = setObjA σ σ.curThread + (.tcb { x with tcbArch := { x.tcbArch with tcbContext := uc }, tcbState := .running })) : BodyOKA σ' m := by + set y : Fl4SpecA64.Tcb := { x with tcbArch := { x.tcbArch with tcbContext := uc }, tcbState := .running } with hy + subst e + have sc : ∀ p y' k, (setObjA σ σ.curThread (.tcb y)).kheap p = some (.schedContext y' k) ↔ + σ.kheap p = some (.schedContext y' k) := fun p y' k => by + by_cases h : p = σ.curThread + · subst h; simp [setObjA, hk] + · simp [setObjA, Function.update_of_ne h] + have sched : ∀ u, schedulable u (setObjA σ σ.curThread (.tcb y)) = schedulable u σ := fun u => by + have a : Fl4Spec.optMap (tcbsOf (setObjA σ σ.curThread (.tcb y))) Fl4SpecA64.Tcb.tcbSchedContext = + Fl4Spec.optMap (tcbsOf σ) Fl4SpecA64.Tcb.tcbSchedContext := by + funext p + by_cases h : p = σ.curThread + · subst h; simp [tcbsOf, tcbsOfKh, Fl4Spec.optMap, setObjA, hk, tcbOf, y] + · simp [tcbsOf, tcbsOfKh, Fl4Spec.optMap, setObjA, Function.update_of_ne h] + have b : scsOf (setObjA σ σ.curThread (.tcb y)) = scsOf σ := by + funext p + by_cases h : p = σ.curThread + · subst h; simp [scsOf, scsOfKh, Fl4Spec.optMap, setObjA, hk, scOf] + · simp [scsOf, scsOfKh, Fl4Spec.optMap, setObjA, Function.update_of_ne h] + have c : Fl4Spec.optPred (fun q => Fl4SpecA64.runnable q = true) + (Fl4Spec.optMapSome (tcbsOf (setObjA σ σ.curThread (.tcb y))) Fl4SpecA64.Tcb.tcbState) u = + Fl4Spec.optPred (fun q => Fl4SpecA64.runnable q = true) + (Fl4Spec.optMapSome (tcbsOf σ) Fl4SpecA64.Tcb.tcbState) u := by + by_cases h : u = σ.curThread + · subst h + simp [Fl4Spec.optPred, Fl4Spec.optMapSome, tcbsOf, tcbsOfKh, Fl4Spec.optMap, setObjA, hk, tcbOf, y, hr, + Fl4SpecA64.runnable] + · simp [Fl4Spec.optPred, Fl4Spec.optMapSome, tcbsOf, tcbsOfKh, Fl4Spec.optMap, setObjA, Function.update_of_ne h] + unfold schedulable + rw [a, b] + exact decide_eq_decide.2 (and_congr (Iff.of_eq c) Iff.rfl) + refine ⟨⟨B.validTime, fun y' k h c g => ?_, fun _ z hz => ?_⟩, fun u h => ?_, B.scNotIdle, + fun y' k h => B.owner y' k ((sc _ _ _).1 h)⟩ + · have h0 := (sc _ _ _).1 h + obtain ⟨r, o⟩ := B.budget y' k h0 c g + constructor + · intro z j hz; exact r z j ((sc _ _ _).1 hz) + · intro hp z j hz; exact o hp z j ((sc _ _ _).1 hz) + · have : (setObjA σ σ.curThread (.tcb y)).kheap σ.curThread = some (.tcb y) := by simp [setObjA] + change (setObjA σ σ.curThread (.tcb y)).kheap σ.curThread = some (.tcb z) at hz + rw [this] at hz + cases hz + exact Or.inl rfl + · rw [sched]; exact B.sw u h + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3ErrIrqA.lean b/proofs/Fl4ProofsA64/P3/Stage3ErrIrqA.lean new file mode 100644 index 000000000..21f743da9 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ErrIrqA.lean @@ -0,0 +1,72 @@ +/- +P3 (aarch64, A64-P3 Lift 1b): **an `Err` of the IRQ control decoder keeps the state and the arch relation**. +`decode_irq_control_msi` calls `msi_map`, which changes the arch state, before it returns an `Err`; every other +`Err` comes with the entry arch state. + +Named premises: [P] `ArchTotal`; [P] `hP`: `msi_map` keeps the predicate on the arch state (`MsiKeepA` of +`P3/MachKeepA` at the record's arch relation). New module. +-/ +import Fl4ProofsA64.P3.Stage3ErrKeep2A +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P0 + +noncomputable section + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : Fl4ProofsA64.P1.LawfulStore st] + +/-- An `Err` other than `Preempted` comes with the entry state and an arch state satisfying `P`. -/ +def ErrArchA (s : S) (P : A → Prop) (x : core.result.Result Unit error.Exc × S × A) : Prop := + ∀ e, x.1 = .Err e → e ≠ .Preempted → x.2.1 = s ∧ P x.2.2 + +/-- Close a returned leaf of the IRQ control decoder. -/ +macro "errirq_close" : tactic => `(tactic| ( + (try simp only [WP.spec_ok, ErrArchA]) + (try (intro e h _ + first + | (cases h; done) + | exact ⟨trivial, by assumption⟩ + | exact ⟨rfl, by assumption⟩)))) + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 4000000 in +/-- **The MSI path's `Err` returns**, `msi_map`'s three included. -/ +theorem interrupt.decode_irq_control_msi.errKA (hA : ArchTotal ar) (s : S) (a : A) (P : A → Prop) (ha : P a) + (hP : ∀ irq dev ev cpu, ar.msi_map a irq dev ev cpu ⦃ r => P r.2 ⦄) + (len : U64) (src : obj.Slot) (buf : U64) (extra : Aeneas.Std.Array obj.Slot 3#usize) : + interrupt.decode_irq_control_msi st ar s a len src buf extra ⦃ ErrArchA s P ⦄ := by + have hS : StoreTotal st := L.total + unfold interrupt.decode_irq_control_msi + struct_steps + all_goals errirq_close + -- the `?` operator's `from_residual` side goals + all_goals p0_close + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 4000000 in +/-- **`decode_irq_control`'s `Err` returns** (module docstring). -/ +theorem interrupt.decode_irq_control.errKA (hA : ArchTotal ar) (s : S) (a : A) (P : A → Prop) (ha : P a) + (hP : ∀ irq dev ev cpu, ar.msi_map a irq dev ev cpu ⦃ r => P r.2 ⦄) + (lbl len : U64) (src : obj.Slot) (buf : U64) (extra : Aeneas.Std.Array obj.Slot 3#usize) : + interrupt.decode_irq_control st ar s a lbl len src buf extra ⦃ ErrArchA s P ⦄ := by + have hS : StoreTotal st := L.total + have h_msi := fun len src buf extra => + interrupt.decode_irq_control_msi.errKA (st := st) (ar := ar) hA s a P ha hP len src buf extra + unfold interrupt.decode_irq_control + by_cases hmsi : lbl = fl4_abi.label.ARMIRQIssueIRQHandlerMSI + · rw [if_pos hmsi] + exact h_msi len src buf extra + rw [if_neg hmsi] + -- P0's walk of this function: the `let (edge, index, depth) ← if …` bind is stepped as a whole. + repeat' (first | step | p0_ite_head | (refine WP.spec_bind (Pₘ := fun _ => True) (by split <;> p0_split) ?_; intro x _; try (rcases x with ⟨_, _, _⟩)) | split | (simp only [bind_tc_ok, bind_ok]) | p0_cases_prod) + all_goals errirq_close + -- the `?` operator's `from_residual` side goals + all_goals p0_close + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3ErrKeep2A.lean b/proofs/Fl4ProofsA64/P3/Stage3ErrKeep2A.lean new file mode 100644 index 000000000..e3bdd65bd --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ErrKeep2A.lean @@ -0,0 +1,405 @@ +/- +P3 (aarch64, A64-P3 Lift 1b): **an `Err` other than `Preempted` keeps the state: the TCB, IRQ, SchedContext and +Untyped decoders** (IRQ control excluded: its MSI path changes the arch state before an `Err`; with `P3/Stage3ErrKeepA`'s CNode and Domain ones). + +* `*.preA`: the perform steps after the caller's Restart fail only by preemption (`PreOnlyA`). +* `*.errKA`: a decoder's `Err` other than `Preempted` comes with its entry state (`ErrKeepA` / `ErrKeep2A`). + +[S] `hst`: the stall the decoder runs first (`tcb.decode`: the invoked thread; `schedcontext.decode`: the invoked +SchedContext's bound thread) is a no-op at the entry state; the later stalls precede only `Ok` returns. P1's +`debug.check_bp_num.err` (AArch64 has no hardware breakpoints). P0's `ArchTotal`. +Generated by `handoff/tools/lift1b/gen_errk2.py`. New module. +-/ +import Fl4ProofsA64.P3.Stage3ErrKeepA +import Fl4ProofsA64.P1.QueueDecTcb +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P0 + +noncomputable section + +/-- `pre_steps` with a larger simp budget and P0's `p0_ite_head`. -/ +macro "pre_steps2" : tactic => + `(tactic| (repeat' (first | step | (simp (maxSteps := 4000000) only [bind_tc_ok, bind_ok]) | split | p0_ite_head | p0_cases_prod))) + +/-- The residual leaves without `simp_all` over the whole context. -/ +macro "pre_res2" : tactic => + `(tactic| ((try subst_vars); first | exact PreOnlyA.ok _ | exact PreOnlyA.pre | (pre_cases_res; all_goals ((try dsimp only at *); first | contradiction | assumption | (simp only [core.ops.control_flow.ControlFlow.Break.injEq, core.result.Result.Err.injEq] at *; subst_vars; assumption) | (injections; subst_vars; assumption))))) + +/-- P0's `p0_structural` order (for the large label chains, where `step*` times out in `whnf`). -/ +macro "struct_steps" : tactic => + `(tactic| (repeat' (first | p0_cases_sum | step | p0_ite_head | p0_whnf_head | (simp only [bind_tc_ok, bind_ok]) | p0_cases_prod | split))) + +/-- `errk_steps` with a larger simp budget and P0's `p0_ite_head`. -/ +macro "errk_steps2" : tactic => + `(tactic| ((try step*); all_goals (repeat' (first | step | split | (simp (maxSteps := 4000000) only [bind_tc_ok, bind_ok]) | p0_ite_head | p0_cases_prod)))) + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : Fl4ProofsA64.P1.LawfulStore st] + +set_option maxHeartbeats 4000000 in +theorem untyped.reset_untyped_loop.preA (hA : ArchTotal ar) (s : S) (a : A) (slot : obj.Slot) (ptr block : U64) + (device : Bool) (offset : U64) : + untyped.reset_untyped_loop st ar s a slot ptr block device offset ⦃ x => PreOnlyA x.1 ⦄ := by + have hS : StoreTotal st := L.total + have h_pp := @sched.preemption_point.preA S A st ar L hA + unfold untyped.reset_untyped_loop + apply loop.spec_decr_nat (measure := fun x => match x with | (_, _, x2) => x2.val) (inv := fun _ => True) + · rintro ⟨x0, x1, x2⟩ - + unfold untyped.reset_untyped_loop.body + pre_steps2 + all_goals (try pre_close) + all_goals (try pre_res) + all_goals (try (simp only [lift, bind_ok, WP.spec_ok, saturating_sub_u64_val]; have : untyped.RESET_CHUNK.val = 65536 := (by simp [untyped.RESET_CHUNK]); have h0 : x2.val ≠ 0 := (by intro h; apply ‹¬x2 = 0#u64›; scalar_tac); omega)) + · trivial + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 16000000 in +theorem untyped.reset_untyped.preA (hA : ArchTotal ar) (s : S) (a : A) (slot : obj.Slot) : + untyped.reset_untyped st ar s a slot ⦃ x => PreOnlyA x.1 ⦄ := by + have hS : StoreTotal st := L.total + have h_untyped_reset_untyped_loop := @untyped.reset_untyped_loop.preA S A st ar L hA + unfold untyped.reset_untyped + pre_steps2 + all_goals (try pre_close) + all_goals pre_res + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 16000000 in +theorem untyped.retype.preA (hA : ArchTotal ar) (s : S) (a : A) (slot : obj.Slot) (reset : Bool) (base : Std.U64) (new_type : Std.U64) (user_size : Std.U64) (dest : Std.U64) (offset : Std.U64) (length : Std.U64) (device : Bool) : + untyped.retype st ar s a slot reset base new_type user_size dest offset length device ⦃ x => PreOnlyA x.1 ⦄ := by + have hS : StoreTotal st := L.total + have h_untyped_reset_untyped := @untyped.reset_untyped.preA S A st ar L hA + unfold untyped.retype + pre_steps2 + all_goals (try pre_close) + all_goals pre_res + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 16000000 in +theorem untyped.retype_shared.errKA (hA : ArchTotal ar) (s : S) (a : A) (length : Std.U64) (slot : obj.Slot) (cap : capability.Cap) (buf : Std.U64) (extra : Array obj.Slot 3#usize) : + untyped.retype_shared st ar s a length slot cap buf extra ⦃ ErrKeepA s a ⦄ := by + have hS : StoreTotal st := L.total + have h_untyped_reset_untyped := @untyped.reset_untyped.preA S A st ar L hA + unfold untyped.retype_shared + errk_steps2 + all_goals (try errk_close) + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 16000000 in +theorem untyped.decode.errKA (hA : ArchTotal ar) (s : S) (a : A) (lbl : Std.U64) (length : Std.U64) (slot : obj.Slot) (cap : capability.Cap) (buf : Std.U64) (extra : Array obj.Slot 3#usize) : + untyped.decode st ar s a lbl length slot cap buf extra ⦃ ErrKeepA s a ⦄ := by + have hS : StoreTotal st := L.total + have h_untyped_retype := @untyped.retype.preA S A st ar L hA + have h_untyped_retype_shared := @untyped.retype_shared.errKA S A st ar L hA + unfold untyped.decode + errk_steps2 + all_goals (try errk_close) + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 16000000 in +theorem tcb.install_tcb_cap.preA (hA : ArchTotal ar) (s : S) (a : A) (target : obj.Tcb) (slot : obj.Slot) (index : Std.U64) (new_cap : capability.Cap) (src : obj.Slot) : + tcb.install_tcb_cap st ar s a target slot index new_cap src ⦃ x => PreOnlyA x.1 ⦄ := by + have hS : StoreTotal st := L.total + have h_cdt_delete := @cdt.delete.preA S A st ar L hA + unfold tcb.install_tcb_cap + pre_steps2 + all_goals (try pre_close) + all_goals pre_res + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 16000000 in +theorem tcb.install_buffer.preA (hA : ArchTotal ar) (s : S) (a : A) (target : obj.Tcb) (slot : obj.Slot) (buffer_addr : Std.U64) (buffer : (capability.Cap × obj.Slot)) : + tcb.install_buffer st ar s a target slot buffer_addr buffer ⦃ x => PreOnlyA x.1 ⦄ := by + have hS : StoreTotal st := L.total + have h_cdt_delete := @cdt.delete.preA S A st ar L hA + unfold tcb.install_buffer + pre_steps2 + all_goals (try pre_close) + all_goals pre_res + +set_option maxRecDepth 1000000 in +set_option maxHeartbeats 16000000 in +theorem tcb.thread_control_caps.preA (hA : ArchTotal ar) (s : S) (a : A) (target : obj.Tcb) (slot : obj.Slot) (fh : (capability.Cap × obj.Slot)) (th : (capability.Cap × obj.Slot)) (croot : (capability.Cap × obj.Slot)) (vroot : (capability.Cap × obj.Slot)) (buffer_addr : Std.U64) (buffer : (capability.Cap × obj.Slot)) (flags : Std.U64) : + tcb.thread_control_caps st ar s a target slot fh th croot vroot buffer_addr buffer flags ⦃ x => PreOnlyA x.1 ⦄ := by + have hS : StoreTotal st := L.total + have h_tcb_install_buffer := @tcb.install_buffer.preA S A st ar L hA + have h_tcb_install_tcb_cap := @tcb.install_tcb_cap.preA S A st ar L hA + obtain ⟨fh1, fh2⟩ := fh; obtain ⟨th1, th2⟩ := th; obtain ⟨cr1, cr2⟩ := croot; obtain ⟨vr1, vr2⟩ := vroot + unfold tcb.thread_control_caps + pre_steps + all_goals (try pre_close) + all_goals pre_res2 + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 16000000 in +theorem tcb.thread_control_sched.preA (hA : ArchTotal ar) (s : S) (a : A) (target : obj.Tcb) (slot : obj.Slot) (fh : (capability.Cap × obj.Slot)) (mcp : Std.U64) (prio : Std.U64) (sc : obj.Sc) (flags : Std.U64) : + tcb.thread_control_sched st ar s a target slot fh mcp prio sc flags ⦃ x => PreOnlyA x.1 ⦄ := by + have hS : StoreTotal st := L.total + have h_tcb_install_tcb_cap := @tcb.install_tcb_cap.preA S A st ar L hA + unfold tcb.thread_control_sched + pre_steps2 + all_goals (try pre_close) + all_goals pre_res + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 16000000 in +theorem tcb.copy_registers.preA (hA : ArchTotal ar) (s : S) (a : A) (dest : obj.Tcb) (src : obj.Tcb) (flags : Std.U64) : + tcb.copy_registers st ar s a dest src flags ⦃ x => PreOnlyA x.1 ⦄ := by + have hS : StoreTotal st := L.total + unfold tcb.copy_registers + pre_steps2 + all_goals (try pre_close) + all_goals pre_res + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 16000000 in +theorem tcb.fpu_transfer.errKA (hA : ArchTotal ar) (s : S) (a : A) (target : obj.Tcb) (c : tcb.Ctx) (to_frame : Bool) : + tcb.fpu_transfer st ar s a target c to_frame ⦃ ErrKeepA s a ⦄ := by + have hS : StoreTotal st := L.total + unfold tcb.fpu_transfer + errk_steps2 + all_goals (try errk_close) + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 16000000 in +theorem tcb.set_breakpoint_checked.errKA (hA : ArchTotal ar) (s : S) (t : obj.Tcb) (c : tcb.Ctx) : + tcb.set_breakpoint_checked st s t c ⦃ ErrKeep2A s ⦄ := by + have hS : StoreTotal st := L.total + unfold tcb.set_breakpoint_checked + have h_bp := Fl4ProofsA64.P1.debug.check_bp_num.err + errk_steps2 + all_goals (try errk_close) + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 16000000 in +theorem tcb.decode_bind_notification.errKA (hA : ArchTotal ar) (s : S) (target : obj.Tcb) (c : tcb.Ctx) : + tcb.decode_bind_notification st s target c ⦃ ErrKeep2A s ⦄ := by + have hS : StoreTotal st := L.total + unfold tcb.decode_bind_notification + errk_steps2 + all_goals (try errk_close) + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 16000000 in +theorem tcb.decode_configure.errKA (hA : ArchTotal ar) (s : S) (a : A) (target : obj.Tcb) (c : tcb.Ctx) : + tcb.decode_configure st ar s a target c ⦃ ErrKeepA s a ⦄ := by + have hS : StoreTotal st := L.total + have h_tcb_thread_control_caps := @tcb.thread_control_caps.preA S A st ar L hA + unfold tcb.decode_configure + errk_steps2 + all_goals (try errk_close) + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 16000000 in +theorem tcb.decode_configure_single_stepping.errKA (hA : ArchTotal ar) (s : S) (a : A) (target : obj.Tcb) (c : tcb.Ctx) : + tcb.decode_configure_single_stepping st ar s a target c ⦃ ErrKeepA s a ⦄ := by + have hS : StoreTotal st := L.total + unfold tcb.decode_configure_single_stepping + errk_steps2 + all_goals (try errk_close) + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 16000000 in +theorem tcb.decode_copy_registers.errKA (hA : ArchTotal ar) (s : S) (a : A) (target : obj.Tcb) (c : tcb.Ctx) : + tcb.decode_copy_registers st ar s a target c ⦃ ErrKeepA s a ⦄ := by + have hS : StoreTotal st := L.total + have h_tcb_copy_registers := @tcb.copy_registers.preA S A st ar L hA + unfold tcb.decode_copy_registers + errk_steps2 + all_goals (try errk_close) + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 16000000 in +theorem tcb.decode_get_breakpoint.errKA (hA : ArchTotal ar) (s : S) (target : obj.Tcb) (c : tcb.Ctx) : + tcb.decode_get_breakpoint st s target c ⦃ ErrKeep2A s ⦄ := by + have hS : StoreTotal st := L.total + unfold tcb.decode_get_breakpoint + errk_steps2 + all_goals (try errk_close) + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 16000000 in +theorem tcb.decode_read_registers.errKA (hA : ArchTotal ar) (s : S) (a : A) (target : obj.Tcb) (c : tcb.Ctx) : + tcb.decode_read_registers st ar s a target c ⦃ ErrKeepA s a ⦄ := by + have hS : StoreTotal st := L.total + unfold tcb.decode_read_registers + errk_steps2 + all_goals (try errk_close) + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 16000000 in +theorem tcb.decode_set_flags.errKA (hA : ArchTotal ar) (s : S) (a : A) (target : obj.Tcb) (c : tcb.Ctx) : + tcb.decode_set_flags st ar s a target c ⦃ ErrKeepA s a ⦄ := by + have hS : StoreTotal st := L.total + unfold tcb.decode_set_flags + errk_steps2 + all_goals (try errk_close) + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 16000000 in +theorem tcb.decode_set_high_vspace.errKA (hA : ArchTotal ar) (s : S) (a : A) (target : obj.Tcb) (c : tcb.Ctx) : + tcb.decode_set_high_vspace st ar s a target c ⦃ ErrKeepA s a ⦄ := by + have hS : StoreTotal st := L.total + have h_tcb_install_tcb_cap := @tcb.install_tcb_cap.preA S A st ar L hA + unfold tcb.decode_set_high_vspace + errk_steps2 + all_goals (try errk_close) + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 16000000 in +theorem tcb.decode_set_ipc_buffer.errKA (hA : ArchTotal ar) (s : S) (a : A) (target : obj.Tcb) (c : tcb.Ctx) : + tcb.decode_set_ipc_buffer st ar s a target c ⦃ ErrKeepA s a ⦄ := by + have hS : StoreTotal st := L.total + have h_tcb_thread_control_caps := @tcb.thread_control_caps.preA S A st ar L hA + unfold tcb.decode_set_ipc_buffer + errk_steps2 + all_goals (try errk_close) + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 16000000 in +theorem tcb.decode_set_pac_keys.errKA (hA : ArchTotal ar) (s : S) (a : A) (target : obj.Tcb) (c : tcb.Ctx) : + tcb.decode_set_pac_keys st ar s a target c ⦃ ErrKeepA s a ⦄ := by + have hS : StoreTotal st := L.total + unfold tcb.decode_set_pac_keys + errk_steps2 + all_goals (try errk_close) + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 16000000 in +theorem tcb.decode_arch.errKA (hA : ArchTotal ar) (s : S) (a : A) (lbl : Std.U64) (target : obj.Tcb) (c : tcb.Ctx) : + tcb.decode_arch st ar s a lbl target c ⦃ ErrKeepA s a ⦄ := by + have hS : StoreTotal st := L.total + have h_tcb_decode_set_high_vspace := @tcb.decode_set_high_vspace.errKA S A st ar L hA + have h_tcb_decode_set_pac_keys := @tcb.decode_set_pac_keys.errKA S A st ar L hA + unfold tcb.decode_arch + errk_steps2 + all_goals (try errk_close) + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 16000000 in +theorem tcb.decode_set_priority.errKA (hA : ArchTotal ar) (s : S) (a : A) (target : obj.Tcb) (c : tcb.Ctx) (prio : Bool) : + tcb.decode_set_priority st ar s a target c prio ⦃ ErrKeepA s a ⦄ := by + have hS : StoreTotal st := L.total + have h_tcb_thread_control_sched := @tcb.thread_control_sched.preA S A st ar L hA + unfold tcb.decode_set_priority + errk_steps2 + all_goals (try errk_close) + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 16000000 in +theorem tcb.decode_set_sched_params.errKA (hA : ArchTotal ar) (s : S) (a : A) (target : obj.Tcb) (c : tcb.Ctx) : + tcb.decode_set_sched_params st ar s a target c ⦃ ErrKeepA s a ⦄ := by + have hS : StoreTotal st := L.total + have h_tcb_thread_control_sched := @tcb.thread_control_sched.preA S A st ar L hA + unfold tcb.decode_set_sched_params + errk_steps2 + all_goals (try errk_close) + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 16000000 in +theorem tcb.decode_set_space.errKA (hA : ArchTotal ar) (s : S) (a : A) (target : obj.Tcb) (c : tcb.Ctx) : + tcb.decode_set_space st ar s a target c ⦃ ErrKeepA s a ⦄ := by + have hS : StoreTotal st := L.total + have h_tcb_thread_control_caps := @tcb.thread_control_caps.preA S A st ar L hA + unfold tcb.decode_set_space + errk_steps2 + all_goals (try errk_close) + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 16000000 in +theorem tcb.decode_set_timeout_endpoint.errKA (hA : ArchTotal ar) (s : S) (a : A) (target : obj.Tcb) (c : tcb.Ctx) : + tcb.decode_set_timeout_endpoint st ar s a target c ⦃ ErrKeepA s a ⦄ := by + have hS : StoreTotal st := L.total + have h_tcb_thread_control_caps := @tcb.thread_control_caps.preA S A st ar L hA + unfold tcb.decode_set_timeout_endpoint + errk_steps2 + all_goals (try errk_close) + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 16000000 in +theorem tcb.decode_set_tls_base.errKA (hA : ArchTotal ar) (s : S) (target : obj.Tcb) (c : tcb.Ctx) : + tcb.decode_set_tls_base st s target c ⦃ ErrKeep2A s ⦄ := by + have hS : StoreTotal st := L.total + unfold tcb.decode_set_tls_base + errk_steps2 + all_goals (try errk_close) + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 16000000 in +theorem tcb.decode_write_registers.errKA (hA : ArchTotal ar) (s : S) (a : A) (target : obj.Tcb) (c : tcb.Ctx) : + tcb.decode_write_registers st ar s a target c ⦃ ErrKeepA s a ⦄ := by + have hS : StoreTotal st := L.total + unfold tcb.decode_write_registers + errk_steps2 + all_goals (try errk_close) + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 16000000 in +theorem tcb.decode.errKA (hA : ArchTotal ar) (s : S) (a : A) (lbl : Std.U64) (target : obj.Tcb) (c : tcb.Ctx) + (hst : smp.remote_tcb_stall st ar s a target = ok (s, a)) : + tcb.decode st ar s a lbl target c ⦃ ErrKeepA s a ⦄ := by + have hS : StoreTotal st := L.total + have h_cdt_delete := @cdt.delete.preA S A st ar L hA + have h_tcb_decode_arch := @tcb.decode_arch.errKA S A st ar L hA + have h_tcb_decode_bind_notification := @tcb.decode_bind_notification.errKA S A st ar L hA + have h_tcb_decode_configure := @tcb.decode_configure.errKA S A st ar L hA + have h_tcb_decode_configure_single_stepping := @tcb.decode_configure_single_stepping.errKA S A st ar L hA + have h_tcb_decode_copy_registers := @tcb.decode_copy_registers.errKA S A st ar L hA + have h_tcb_decode_get_breakpoint := @tcb.decode_get_breakpoint.errKA S A st ar L hA + have h_tcb_decode_read_registers := @tcb.decode_read_registers.errKA S A st ar L hA + have h_tcb_decode_set_flags := @tcb.decode_set_flags.errKA S A st ar L hA + have h_tcb_decode_set_ipc_buffer := @tcb.decode_set_ipc_buffer.errKA S A st ar L hA + have h_tcb_decode_set_priority := @tcb.decode_set_priority.errKA S A st ar L hA + have h_tcb_decode_set_sched_params := @tcb.decode_set_sched_params.errKA S A st ar L hA + have h_tcb_decode_set_space := @tcb.decode_set_space.errKA S A st ar L hA + have h_tcb_decode_set_timeout_endpoint := @tcb.decode_set_timeout_endpoint.errKA S A st ar L hA + have h_tcb_decode_set_tls_base := @tcb.decode_set_tls_base.errKA S A st ar L hA + have h_tcb_decode_write_registers := @tcb.decode_write_registers.errKA S A st ar L hA + have h_tcb_fpu_transfer := @tcb.fpu_transfer.errKA S A st ar L hA + have h_tcb_set_breakpoint_checked := @tcb.set_breakpoint_checked.errKA S A st ar L hA + unfold tcb.decode + have hst' : smp.remote_tcb_stall st ar s a target ⦃ x => x = (s, a) ⦄ := by + rw [hst]; exact (WP.spec_ok _).2 rfl + have h_bp := Fl4ProofsA64.P1.debug.check_bp_num.err + struct_steps + all_goals (try errk_close) + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 16000000 in +theorem interrupt.decode_irq_handler.errKA (hA : ArchTotal ar) (s : S) (a : A) (lbl : Std.U64) (irq : Std.U64) (extra : Array obj.Slot 3#usize) : + interrupt.decode_irq_handler st ar s a lbl irq extra ⦃ ErrKeepA s a ⦄ := by + have hS : StoreTotal st := L.total + unfold interrupt.decode_irq_handler + errk_steps2 + all_goals (try errk_close) + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 16000000 in +theorem schedcontext.decode.errKA (hA : ArchTotal ar) (s : S) (a : A) (lbl : Std.U64) (sc : obj.Sc) (call : Bool) (extra : Array obj.Slot 3#usize) + (hst : ∃ i, obj.Sc.tcb st sc s = ok i ∧ + (if i != 0#u64 then smp.remote_tcb_stall st ar s a i else ok (s, a)) = ok (s, a)) : + schedcontext.decode st ar s a lbl sc call extra ⦃ ErrKeepA s a ⦄ := by + have hS : StoreTotal st := L.total + unfold schedcontext.decode + obtain ⟨i, hi, hi2⟩ := hst + rw [hi, bind_ok, hi2, bind_ok] + simp only [uncurry_apply_pair] + struct_steps + all_goals (try errk_close) + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 16000000 in +theorem schedcontext.decode_sched_control.errKA (hA : ArchTotal ar) (s : S) (a : A) (lbl : Std.U64) (cpu : Std.U64) (length : Std.U64) (buf : Std.U64) (extra : Array obj.Slot 3#usize) : + schedcontext.decode_sched_control st ar s a lbl cpu length buf extra ⦃ ErrKeepA s a ⦄ := by + have hS : StoreTotal st := L.total + unfold schedcontext.decode_sched_control + errk_steps2 + all_goals (try errk_close) + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3ErrKeepA.lean b/proofs/Fl4ProofsA64/P3/Stage3ErrKeepA.lean new file mode 100644 index 000000000..832899f32 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ErrKeepA.lean @@ -0,0 +1,90 @@ +/- +P3 (aarch64, A64-P3 Lift 1b): **an `Err` other than `Preempted` of a non-arch decoder keeps the state** (pilot). + +* `ErrKeepA s a x` / `ErrKeep2A s x`: an `Err` result other than `Preempted` comes with the entry state. +* `domain.decode.errKA`, `cnode.decode.errKA` (pilot). + +No stall premise: `domain.decode`'s stall precedes only an `Ok` return. New module. +-/ +import Fl4ProofsA64.P3.Stage3ErrPreA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P0 + +noncomputable section + +/-- An `Err` other than `Preempted` comes with the entry state and arch state. -/ +def ErrKeepA {S A : Type} (s : S) (a : A) (x : core.result.Result Unit error.Exc × S × A) : Prop := + ∀ e, x.1 = .Err e → e ≠ .Preempted → x.2 = (s, a) + +/-- The same for a decoder that returns no arch state. -/ +def ErrKeep2A {S : Type} (s : S) (x : core.result.Result Unit error.Exc × S) : Prop := + ∀ e, x.1 = .Err e → e ≠ .Preempted → x.2 = s + +/-- Close a leaf of a decoder with an `ErrKeepA` post. -/ +macro "errk_close" : tactic => `(tactic| ( + (try simp only [WP.spec_ok, ErrKeepA, ErrKeep2A]) + first + | done + | (intros; trivial) + | (intros; rfl) + | (intros; simp_all [PreOnlyA, ErrKeepA, ErrKeep2A]; done) + | (intros; (try subst_vars); pre_cases_res; all_goals (simp_all [PreOnlyA, ErrKeepA, ErrKeep2A]; done)))) + +/-- Step through a decoder (`step*`, then `pre_steps`). -/ +macro "errk_steps" : tactic => + `(tactic| ((try step*); all_goals (repeat' (first | step | (simp only [bind_tc_ok, bind_ok]) | split | p0_cases_prod)))) + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : Fl4ProofsA64.P1.LawfulStore st] + +set_option maxHeartbeats 4000000 in +theorem domain.decode.errKA (hA : ArchTotal ar) (s : S) (a : A) (lbl len buf : U64) + (extra : Aeneas.Std.Array obj.Slot 3#usize) : + domain.decode st ar s a lbl len buf extra ⦃ ErrKeepA s a ⦄ := by + have hS : StoreTotal st := L.total + unfold domain.decode + step* + all_goals (try errk_close) + +theorem cnode.decode_copy.errKA (s : S) (lbl len buf : U64) (extra : Aeneas.Std.Array obj.Slot 3#usize) + (dest : obj.Slot) : cnode.decode_copy st s lbl len buf extra dest ⦃ ErrKeep2A s ⦄ := by + have hS : StoreTotal st := L.total + unfold cnode.decode_copy + pre_steps + all_goals (try errk_close) + +theorem cnode.decode_cancel_badged_sends.errKA (s : S) (dest : obj.Slot) : + cnode.decode_cancel_badged_sends st s dest ⦃ ErrKeep2A s ⦄ := by + have hS : StoreTotal st := L.total + unfold cnode.decode_cancel_badged_sends + pre_steps + all_goals (try errk_close) + +theorem cnode.decode_rotate.errKA (s : S) (len buf : U64) (extra : Aeneas.Std.Array obj.Slot 3#usize) + (dest : obj.Slot) : cnode.decode_rotate st s len buf extra dest ⦃ ErrKeep2A s ⦄ := by + have hS : StoreTotal st := L.total + unfold cnode.decode_rotate + pre_steps + all_goals (try errk_close) + +set_option maxHeartbeats 4000000 in +theorem cnode.decode.errKA (hA : ArchTotal ar) (s : S) (a : A) (lbl len : U64) (cap : capability.Cap) (buf : U64) + (extra : Aeneas.Std.Array obj.Slot 3#usize) : + cnode.decode st ar s a lbl len cap buf extra ⦃ ErrKeepA s a ⦄ := by + have hS : StoreTotal st := L.total + have hdel := fun s a slot => cdt.delete.preA (st := st) hA s a slot true + have hrev := fun s a slot => cdt.revoke.preA (st := st) hA s a slot + have hcp := fun lbl len buf extra dest => cnode.decode_copy.errKA (st := st) s lbl len buf extra dest + have hcb := fun dest => cnode.decode_cancel_badged_sends.errKA (st := st) s dest + have hro := fun len buf extra dest => cnode.decode_rotate.errKA (st := st) s len buf extra dest + unfold cnode.decode + pre_steps + all_goals (try errk_close) + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3ErrKeepMacA.lean b/proofs/Fl4ProofsA64/P3/Stage3ErrKeepMacA.lean new file mode 100644 index 000000000..a5a45dacf --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ErrKeepMacA.lean @@ -0,0 +1,53 @@ +/- +P3 (aarch64, A64-P3 Lift 1b, batch a): tactic macros of the generated arch `errKA` modules (`gen_errk3.py`). New module. +-/ +import Fl4ProofsA64.P3.Stage3ErrKeep2A +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P0 + +noncomputable section + +/-- `errk_steps2` with `dsimp only` (let-pairs of constants) and `lift`. -/ +macro "errk_steps3" : tactic => + `(tactic| ((try step*); all_goals (repeat' (first | step | (simp only [uncurry_apply_pair]) | split | (simp (maxSteps := 4000000) only [bind_tc_ok, bind_ok, lift]) | p0_ite_head | p0_cases_prod)))) + +/-- `errk_close` with the error constructors unfolded and arithmetic side goals. -/ +macro "errk_close3" : tactic => `(tactic| first + | errk_close + | scalar_tac + | (simp only [uncurry_apply_pair, lift, bind_ok, WP.spec_ok, error.range, error.invalid_arg, error.invalid_cap, error.truncated, + error.illegal, ErrKeepA, ErrKeep2A] + intros + first | rfl | trivial | (simp_all; done))) + + +/-- A decode `Err` other than `Preempted` keeps the store and an arch state related by `AR` to the hardware root `hw` +(for the decoders that stall a VCPU's threads before an `Err`, whose arch state `vcpu_flush` may change). -/ +def VKeepA {S A Hw : Type} (AR : A → Hw → Prop) (hw : Hw) (s : S) + (x : core.result.Result Unit error.Exc × S × A) : Prop := + ∀ e, x.1 = .Err e → e ≠ .Preempted → x.2.1 = s ∧ AR x.2.2 hw + +theorem VKeepA.of_keep {S A Hw : Type} {AR : A → Hw → Prop} {hw : Hw} {s : S} {a : A} + {x : core.result.Result Unit error.Exc × S × A} (h0 : AR a hw) (h : ErrKeepA s a x) : VKeepA AR hw s x := + fun e he hne => by + have := h e he hne + rw [this] + exact ⟨rfl, h0⟩ + +set_option hygiene false in +/-- Close a leaf of a decoder with a `VKeepA` post (the stall's post and `hAR0` are in the context). -/ +macro "vk_close" : tactic => `(tactic| ( + (try simp only [WP.spec_ok, VKeepA]) + first + | done + | exact ⟨trivial, hAR0⟩ + | exact ⟨rfl, hAR0⟩ + | (constructor <;> assumption) + | (intro e he hne; first | exact ⟨rfl, hAR0⟩ | (cases he) | (simp_all; done)) + | (intros; simp_all; done))) + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3ErrKeepVcpuA.lean b/proofs/Fl4ProofsA64/P3/Stage3ErrKeepVcpuA.lean new file mode 100644 index 000000000..6b6d16cb6 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ErrKeepVcpuA.lean @@ -0,0 +1,99 @@ +/- +P3 (aarch64, A64-P3 Lift 1b, batch a): **an `Err` other than `Preempted` keeps the store and the arch state: the Vcpu decoders** +(`DecErrKeepA` for the arch caps). Generated by `handoff/tools/lift1b/gen_errk3.py Vcpu`; no premise beyond P0's `ArchTotal`. New module. +-/ +import Fl4ProofsA64.P3.Stage3ErrKeepMacA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P0 + +noncomputable section + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : Fl4ProofsA64.P1.LawfulStore st] + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 64000000 in +theorem vcpu.decode_set_tcb.errKA (hA : ArchTotal ar) (s : S) (a : A) (vc : Std.U64) (ex : Array obj.Slot 3#usize) : + vcpu.decode_set_tcb st ar s a vc ex ⦃ ErrKeepA s a ⦄ := by + have hS : StoreTotal st := L.total + unfold vcpu.decode_set_tcb + errk_steps3 + all_goals (try errk_close3) + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 64000000 in +theorem vcpu.decode_inject_irq.vkA {Hw : Type} {AR : A → Hw → Prop} {hw : Hw} (hA : ArchTotal ar) (s : S) (a : A) (hAR0 : AR a hw) (hst : ∀ vc', vcpu.stall_vcpu st ar s a vc' ⦃ r => r.1 = s ∧ AR r.2 hw ⦄) (vc : Std.U64) (length : Std.U64) (buf : Std.U64) : + vcpu.decode_inject_irq st ar s a vc length buf ⦃ VKeepA AR hw s ⦄ := by + have hS : StoreTotal st := L.total + unfold vcpu.decode_inject_irq + errk_steps3 + all_goals (try ((try simp only [WP.spec_ok, VKeepA]); first | exact ⟨trivial, hAR0⟩ | exact ⟨rfl, hAR0⟩ | (constructor <;> assumption) | (intro e he hne; first | exact ⟨trivial, hAR0⟩ | exact ⟨rfl, hAR0⟩ | (constructor <;> assumption) | (cases he) | (simp_all; done)))) + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 64000000 in +theorem vcpu.decode_read_reg.errKA (hA : ArchTotal ar) (s : S) (a : A) (vc : Std.U64) (length : Std.U64) (call : Bool) (buf : Std.U64) : + vcpu.decode_read_reg st ar s a vc length call buf ⦃ ErrKeepA s a ⦄ := by + have hS : StoreTotal st := L.total + unfold vcpu.decode_read_reg + errk_steps3 + all_goals (try errk_close3) + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 64000000 in +theorem vcpu.decode_write_reg.errKA (hA : ArchTotal ar) (s : S) (a : A) (vc : Std.U64) (length : Std.U64) (buf : Std.U64) : + vcpu.decode_write_reg st ar s a vc length buf ⦃ ErrKeepA s a ⦄ := by + have hS : StoreTotal st := L.total + unfold vcpu.decode_write_reg + errk_steps3 + all_goals (try errk_close3) + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 64000000 in +theorem vcpu.decode_ack_vppi.errKA (hA : ArchTotal ar) (s : S) (a : A) (vc : Std.U64) (length : Std.U64) (buf : Std.U64) : + vcpu.decode_ack_vppi st ar s a vc length buf ⦃ ErrKeepA s a ⦄ := by + have hS : StoreTotal st := L.total + unfold vcpu.decode_ack_vppi + errk_steps3 + all_goals (try errk_close3) + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 64000000 in +theorem vcpu.decode_state_transfer.vkA {Hw : Type} {AR : A → Hw → Prop} {hw : Hw} (hA : ArchTotal ar) (s : S) (a : A) (hAR0 : AR a hw) (hst : ∀ vc', vcpu.stall_vcpu st ar s a vc' ⦃ r => r.1 = s ∧ AR r.2 hw ⦄) (vc : Std.U64) (length : Std.U64) (buf : Std.U64) (ex : Array obj.Slot 3#usize) (to_frame : Bool) : + vcpu.decode_state_transfer st ar s a vc length buf ex to_frame ⦃ VKeepA AR hw s ⦄ := by + have hS : StoreTotal st := L.total + unfold vcpu.decode_state_transfer + errk_steps3 + all_goals (try ((try simp only [WP.spec_ok, VKeepA]); first | exact ⟨trivial, hAR0⟩ | exact ⟨rfl, hAR0⟩ | (constructor <;> assumption) | (intro e he hne; first | exact ⟨trivial, hAR0⟩ | exact ⟨rfl, hAR0⟩ | (constructor <;> assumption) | (cases he) | (simp_all; done)))) + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 64000000 in +theorem vcpu.decode_run.errKA (hA : ArchTotal ar) (s : S) (a : A) (vc : Std.U64) (length : Std.U64) (call : Bool) (buf : Std.U64) : + vcpu.decode_run st ar s a vc length call buf ⦃ ErrKeepA s a ⦄ := by + have hS : StoreTotal st := L.total + unfold vcpu.decode_run + errk_steps3 + all_goals (try errk_close3) + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 64000000 in +theorem vcpu.decode.vkA {Hw : Type} {AR : A → Hw → Prop} {hw : Hw} (hA : ArchTotal ar) (s : S) (a : A) (hAR0 : AR a hw) (hst : ∀ vc', vcpu.stall_vcpu st ar s a vc' ⦃ r => r.1 = s ∧ AR r.2 hw ⦄) (lbl : Std.U64) (length : Std.U64) (vc : Std.U64) (call : Bool) (buf : Std.U64) (ex : Array obj.Slot 3#usize) : + vcpu.decode st ar s a lbl length vc call buf ex ⦃ VKeepA AR hw s ⦄ := by + have hS : StoreTotal st := L.total + have h_vcpu_decode_ack_vppi := fun vc length buf => WP.spec_mono (@vcpu.decode_ack_vppi.errKA S A st ar L hA s a vc length buf) (fun _ => VKeepA.of_keep hAR0) + have h_vcpu_decode_inject_irq := @vcpu.decode_inject_irq.vkA S A st ar L Hw AR hw hA s a hAR0 hst + have h_vcpu_decode_read_reg := fun vc length call buf => WP.spec_mono (@vcpu.decode_read_reg.errKA S A st ar L hA s a vc length call buf) (fun _ => VKeepA.of_keep hAR0) + have h_vcpu_decode_run := fun vc length call buf => WP.spec_mono (@vcpu.decode_run.errKA S A st ar L hA s a vc length call buf) (fun _ => VKeepA.of_keep hAR0) + have h_vcpu_decode_set_tcb := fun vc ex => WP.spec_mono (@vcpu.decode_set_tcb.errKA S A st ar L hA s a vc ex) (fun _ => VKeepA.of_keep hAR0) + have h_vcpu_decode_state_transfer := @vcpu.decode_state_transfer.vkA S A st ar L Hw AR hw hA s a hAR0 hst + have h_vcpu_decode_write_reg := fun vc length buf => WP.spec_mono (@vcpu.decode_write_reg.errKA S A st ar L hA s a vc length buf) (fun _ => VKeepA.of_keep hAR0) + unfold vcpu.decode + errk_steps3 + all_goals (try ((try simp only [WP.spec_ok, VKeepA]); first | exact ⟨trivial, hAR0⟩ | exact ⟨rfl, hAR0⟩ | (constructor <;> assumption) | (intro e he hne; first | exact ⟨trivial, hAR0⟩ | exact ⟨rfl, hAR0⟩ | (constructor <;> assumption) | (cases he) | (simp_all; done)))) + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3ErrKeepVsA.lean b/proofs/Fl4ProofsA64/P3/Stage3ErrKeepVsA.lean new file mode 100644 index 000000000..52c840067 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ErrKeepVsA.lean @@ -0,0 +1,146 @@ +/- +P3 (aarch64, A64-P3 Lift 1b, batch a): **an `Err` other than `Preempted` keeps the store and the arch state: the Vs decoders** +(`DecErrKeepA` for the arch caps). Generated by `handoff/tools/lift1b/gen_errk3.py Vs`; no premise beyond P0's `ArchTotal`. New module. +-/ +import Fl4ProofsA64.P3.Stage3ErrKeepMacA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P0 + +noncomputable section + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : Fl4ProofsA64.P1.LawfulStore st] + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 64000000 in +theorem ept.flush_frame.errKA (hA : ArchTotal ar) (s : S) (a : A) (op : Std.U64) (invalidate : Bool) (asid : Std.U64) (ipa : Std.U64) (ptr : Std.U64) (size : capability.FrameSize) (is_device : Bool) (start : Std.U64) («end» : Std.U64) (hse : start.val ≤ «end».val) : + ept.flush_frame st ar s a op invalidate asid ipa ptr size is_device start «end» ⦃ ErrKeepA s a ⦄ := by + have hS : StoreTotal st := L.total + unfold ept.flush_frame + errk_steps3 + all_goals (try errk_close3) + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 64000000 in +theorem ept.map_table.errKA (hA : ArchTotal ar) (s : S) (a : A) (root : Std.U64) (asid : Std.U64) (ipa : Std.U64) (pt : Std.U64) (slot : obj.Slot) : + ept.map_table st ar s a root asid ipa pt slot ⦃ ErrKeepA s a ⦄ := by + have hS : StoreTotal st := L.total + unfold ept.map_table + errk_steps3 + all_goals (try errk_close3) + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 64000000 in +theorem ept.map_frame.errKA (hA : ArchTotal ar) (s : S) (a : A) (root : Std.U64) (asid : Std.U64) (ipa : Std.U64) (vm : Std.U64) (attr : Std.U64) (ptr : Std.U64) (size : capability.FrameSize) (rights : Std.U64) (is_device : Bool) (mapped_asid : Std.U64) (mapped_vaddr : Std.U64) (slot : obj.Slot) : + ept.map_frame st ar s a root asid ipa vm attr ptr size rights is_device mapped_asid mapped_vaddr slot ⦃ ErrKeepA s a ⦄ := by + have hS : StoreTotal st := L.total + unfold ept.map_frame + errk_steps3 + all_goals (try errk_close3) + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 64000000 in +theorem ept.decode.errKA (hA : ArchTotal ar) (s : S) (a : A) (lbl : Std.U64) (length : Std.U64) (cap : capability.Cap) (_slot : obj.Slot) (buf : Std.U64) (_ex : Array obj.Slot 3#usize) : + ept.decode st ar s a lbl length cap _slot buf _ex ⦃ ErrKeepA s a ⦄ := by + have hS : StoreTotal st := L.total + unfold ept.decode + errk_steps3 + all_goals (try errk_close3) + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 64000000 in +theorem vspace.derive_subframe.errKA (hA : ArchTotal ar) (s : S) (length : Std.U64) (src : obj.Slot) (cap : capability.Cap) (buf : Std.U64) (ex : Array obj.Slot 3#usize) : + vspace.derive_subframe st s length src cap buf ex ⦃ ErrKeep2A s ⦄ := by + have hS : StoreTotal st := L.total + unfold vspace.derive_subframe + errk_steps3 + all_goals (try errk_close3) + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 64000000 in +theorem vspace.map_high_page_table.errKA (hA : ArchTotal ar) (s : S) (slot : obj.Slot) (ptr : Std.U64) (vcap : capability.Cap) (vaddr : Std.U64) : + vspace.map_high_page_table st s slot ptr vcap vaddr ⦃ ErrKeep2A s ⦄ := by + have hS : StoreTotal st := L.total + unfold vspace.map_high_page_table + errk_steps3 + all_goals (try errk_close3) + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 64000000 in +theorem vspace.decode_high_page_map.errKA (hA : ArchTotal ar) (s : S) (a : A) (slot : obj.Slot) (f : vspace.FrameCap) (vcap : capability.Cap) (vaddr : Std.U64) (vm : Std.U64) (attr : Std.U64) : + vspace.decode_high_page_map st ar s a slot f vcap vaddr vm attr ⦃ ErrKeepA s a ⦄ := by + have hS : StoreTotal st := L.total + unfold vspace.decode_high_page_map + errk_steps3 + all_goals (try errk_close3) + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 64000000 in +theorem vspace.decode_page_flush.errKA (hA : ArchTotal ar) (s : S) (a : A) (lbl : Std.U64) (length : Std.U64) (f : vspace.FrameCap) (buf : Std.U64) : + vspace.decode_page_flush st ar s a lbl length f buf ⦃ ErrKeepA s a ⦄ := by + have hS : StoreTotal st := L.total + have h_ept_flush_frame := @ept.flush_frame.errKA S A st ar L hA + unfold vspace.decode_page_flush + errk_steps3 + all_goals (try errk_close3) + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 64000000 in +theorem vspace.decode_page_map.errKA (hA : ArchTotal ar) (s : S) (a : A) (length : Std.U64) (slot : obj.Slot) (f : vspace.FrameCap) (buf : Std.U64) (ex : Array obj.Slot 3#usize) : + vspace.decode_page_map st ar s a length slot f buf ex ⦃ ErrKeepA s a ⦄ := by + have hS : StoreTotal st := L.total + have h_ept_map_frame := @ept.map_frame.errKA S A st ar L hA + have h_vspace_decode_high_page_map := @vspace.decode_high_page_map.errKA S A st ar L hA + unfold vspace.decode_page_map + errk_steps3 + all_goals (try errk_close3) + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 64000000 in +theorem vspace.decode_test_and_clear_accessed.errKA (hA : ArchTotal ar) (s : S) (a : A) (f : vspace.FrameCap) (call : Bool) : + vspace.decode_test_and_clear_accessed st ar s a f call ⦃ ErrKeepA s a ⦄ := by + have hS : StoreTotal st := L.total + unfold vspace.decode_test_and_clear_accessed + errk_steps3 + all_goals (try errk_close3) + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 64000000 in +theorem vspace.decode_frame.errKA (hA : ArchTotal ar) (s : S) (a : A) (lbl : Std.U64) (length : Std.U64) (slot : obj.Slot) (cap : capability.Cap) (call : Bool) (buf : Std.U64) (ex : Array obj.Slot 3#usize) : + vspace.decode_frame st ar s a lbl length slot cap call buf ex ⦃ ErrKeepA s a ⦄ := by + have hS : StoreTotal st := L.total + have h_vspace_decode_page_flush := @vspace.decode_page_flush.errKA S A st ar L hA + have h_vspace_decode_page_map := @vspace.decode_page_map.errKA S A st ar L hA + have h_vspace_decode_test_and_clear_accessed := @vspace.decode_test_and_clear_accessed.errKA S A st ar L hA + have h_vspace_derive_subframe := @vspace.derive_subframe.errKA S A st ar L hA + unfold vspace.decode_frame + errk_steps3 + all_goals (try errk_close3) + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 64000000 in +theorem vspace.decode_page_table.errKA (hA : ArchTotal ar) (s : S) (a : A) (lbl : Std.U64) (length : Std.U64) (slot : obj.Slot) (cap : capability.Cap) (buf : Std.U64) (ex : Array obj.Slot 3#usize) : + vspace.decode_page_table st ar s a lbl length slot cap buf ex ⦃ ErrKeepA s a ⦄ := by + have hS : StoreTotal st := L.total + have h_ept_map_table := @ept.map_table.errKA S A st ar L hA + have h_vspace_map_high_page_table := @vspace.map_high_page_table.errKA S A st ar L hA + unfold vspace.decode_page_table + errk_steps3 + all_goals (try errk_close3) + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 64000000 in +theorem vspace.decode_vspace_flush.errKA (hA : ArchTotal ar) (s : S) (a : A) (lbl : Std.U64) (length : Std.U64) (cap : capability.Cap) (buf : Std.U64) : + vspace.decode_vspace_flush st ar s a lbl length cap buf ⦃ ErrKeepA s a ⦄ := by + have hS : StoreTotal st := L.total + unfold vspace.decode_vspace_flush + errk_steps3 + all_goals (try errk_close3) + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3ErrKeepVsDecA.lean b/proofs/Fl4ProofsA64/P3/Stage3ErrKeepVsDecA.lean new file mode 100644 index 000000000..6f6ac1e58 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ErrKeepVsDecA.lean @@ -0,0 +1,59 @@ +/- +P3 (aarch64, A64-P3 Lift 1b, batch a): **`vspace.decode`'s `Err` other than `Preempted` keeps the store and the arch +state** (`DecErrKeepA` for the VSpace / ASID / Frame caps). Split by cap: the AsidControl branch (a 28-way match inside +the make-pool body) is its own theorem. Hand-written from `gen_errk3.py`'s shape; no premise beyond P0's `ArchTotal`. +New module. +-/ +import Fl4ProofsA64.P3.Stage3ErrKeepMacA +import Fl4ProofsA64.P3.Stage3ErrKeepVsA +import Fl4ProofsA64.P3.Stage3ErrKeepVsPoolA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P0 + +noncomputable section + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : Fl4ProofsA64.P1.LawfulStore st] + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 16000000 in +theorem vspace.decode.errKA_asidCtl (hA : ArchTotal ar) (s : S) (a : A) (lbl length : Std.U64) (slot : obj.Slot) + (call : Bool) (buf : Std.U64) (ex : Array obj.Slot 3#usize) : + vspace.decode st ar s a lbl length slot capability.Cap.AsidControl call buf ex ⦃ ErrKeepA s a ⦄ := by + have hS : StoreTotal st := L.total + unfold vspace.decode + errk_steps3 + all_goals (try errk_close3) + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 32000000 in +theorem vspace.decode.errKA_other (hA : ArchTotal ar) (s : S) (a : A) (lbl length : Std.U64) (slot : obj.Slot) + (cap : capability.Cap) (hne : cap ≠ capability.Cap.AsidControl) + (hne2 : ∀ p b, cap ≠ capability.Cap.AsidPool p b) (call : Bool) (buf : Std.U64) + (ex : Array obj.Slot 3#usize) : + vspace.decode st ar s a lbl length slot cap call buf ex ⦃ ErrKeepA s a ⦄ := by + have hS : StoreTotal st := L.total + have h_vspace_decode_frame := @vspace.decode_frame.errKA S A st ar L hA + have h_vspace_decode_page_table := @vspace.decode_page_table.errKA S A st ar L hA + have h_vspace_decode_vspace_flush := @vspace.decode_vspace_flush.errKA S A st ar L hA + cases cap + all_goals (first | exact absurd rfl hne | exact absurd rfl (hne2 _ _) | + (unfold vspace.decode; errk_steps3; all_goals (try errk_close3))) + +theorem vspace.decode.errKA (hA : ArchTotal ar) (s : S) (a : A) (lbl length : Std.U64) (slot : obj.Slot) + (cap : capability.Cap) (call : Bool) (buf : Std.U64) (ex : Array obj.Slot 3#usize) : + vspace.decode st ar s a lbl length slot cap call buf ex ⦃ ErrKeepA s a ⦄ := by + by_cases h : cap = capability.Cap.AsidControl + · subst h; exact vspace.decode.errKA_asidCtl hA s a lbl length slot call buf ex + by_cases h2 : ∃ p b, cap = capability.Cap.AsidPool p b + · obtain ⟨p, b, rfl⟩ := h2 + exact vspace.decode.errKA_asidPool hA s a lbl length slot p b call buf ex + exact vspace.decode.errKA_other hA s a lbl length slot cap h (fun p b e => h2 ⟨p, b, e⟩) call buf ex + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3ErrKeepVsPoolA.lean b/proofs/Fl4ProofsA64/P3/Stage3ErrKeepVsPoolA.lean new file mode 100644 index 000000000..e0e5bff78 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ErrKeepVsPoolA.lean @@ -0,0 +1,366 @@ +/- +P3 (aarch64, A64-P3 Lift 1b, batch a): **`vspace.decode` on an ASID pool cap**: an `Err` other than `Preempted` keeps the +store and the arch state. + +The assign body (label guard, extra slot, then a 28-way match on the extra slot's cap whose `Pml4` and `HighVSpace` arms +hold the loops) is split into conditional lemmas: each takes the guard, the extra slot and the cap read as equations +(`hl`, `hx`, `hc`), so the match is a constructor match and the lemma is about one arm: +`asidPool_pml4`, `asidPool_high`, `asidPool_ept`, `asidPool_other` (the 26 `invalid_cap` arms); `errKA_asidPool` dispatches. No premise +beyond P0's `ArchTotal`. New module. +-/ +import Fl4ProofsA64.P3.Stage3ErrKeepMacA +import Fl4ProofsA64.P3.Stage3SiteA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P0 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST + +noncomputable section + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : Fl4ProofsA64.P1.LawfulStore st] + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 32000000 in +theorem vspace.decode.asidPool_pml4 (hA : ArchTotal ar) (s : S) (a : A) (lbl length : Std.U64) (slot : obj.Slot) + (ptr base : Std.U64) (call : Bool) (buf : Std.U64) (ex : Array obj.Slot 3#usize) (x : obj.Slot) + (p v : Std.U64) (b : Bool) + (hl : (lbl != fl4_abi.label.ARMASIDPoolAssign) = false) + (hx : vspace.extra ex 0#usize = ok (some x)) + (hc : obj.Slot.cap st x s = ok (capability.Cap.Pml4 p b v)) : + vspace.decode st ar s a lbl length slot (capability.Cap.AsidPool ptr base) call buf ex ⦃ ErrKeepA s a ⦄ := by + have hS : StoreTotal st := L.total + unfold vspace.decode + simp only [cur_thread_ok, bind_ok, hl, Bool.false_eq_true, ↓reduceIte, hx, hc] + errk_steps3 + all_goals (try errk_close3) + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 32000000 in +theorem vspace.decode.asidPool_high (hA : ArchTotal ar) (s : S) (a : A) (lbl length : Std.U64) (slot : obj.Slot) + (ptr base : Std.U64) (call : Bool) (buf : Std.U64) (ex : Array obj.Slot 3#usize) (x : obj.Slot) + (p v : Std.U64) (b : Bool) + (hl : (lbl != fl4_abi.label.ARMASIDPoolAssign) = false) + (hx : vspace.extra ex 0#usize = ok (some x)) + (hc : obj.Slot.cap st x s = ok (capability.Cap.HighVSpace p b v)) : + vspace.decode st ar s a lbl length slot (capability.Cap.AsidPool ptr base) call buf ex ⦃ ErrKeepA s a ⦄ := by + have hS : StoreTotal st := L.total + unfold vspace.decode + simp only [cur_thread_ok, bind_ok, hl, Bool.false_eq_true, ↓reduceIte, hx, hc] + errk_steps3 + all_goals (try errk_close3) + +/-- Proof-splitting tag of an ASID-pool assign's extra cap: groups 0–8 are the 24 `invalid_cap` arms, 9 the loop arms. -/ +def aspGrp : capability.Cap → Nat + | capability.Cap.Null .. => 0 + | capability.Cap.Untyped .. => 0 + | capability.Cap.Endpoint .. => 0 + | capability.Cap.Notification .. => 0 + | capability.Cap.Reply .. => 0 + | capability.Cap.CNode .. => 0 + | capability.Cap.Thread .. => 1 + | capability.Cap.IrqControl .. => 1 + | capability.Cap.IrqHandler .. => 1 + | capability.Cap.Zombie .. => 1 + | capability.Cap.Domain .. => 1 + | capability.Cap.SchedContext .. => 1 + | capability.Cap.SchedControl .. => 2 + | capability.Cap.Frame .. => 2 + | capability.Cap.PageTable .. => 2 + | capability.Cap.PageDirectory .. => 2 + | capability.Cap.Pdpt .. => 2 + | capability.Cap.Vcpu .. => 2 + | capability.Cap.Ept .. => 9 + | capability.Cap.AsidControl .. => 4 + | capability.Cap.AsidPool .. => 5 + | capability.Cap.IoPortControl .. => 6 + | capability.Cap.IoPort .. => 7 + | capability.Cap.TdxControl .. => 8 + | capability.Cap.Pml4 .. => 9 + | capability.Cap.HighVSpace .. => 9 + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 32000000 in +theorem vspace.decode.asidPool_ept (hA : ArchTotal ar) (s : S) (a : A) (lbl length : Std.U64) (slot : obj.Slot) + (ptr base : Std.U64) (call : Bool) (buf : Std.U64) (ex : Array obj.Slot 3#usize) (x : obj.Slot) + (p i q r : Std.U64) (b : Bool) + (hl : (lbl != fl4_abi.label.ARMASIDPoolAssign) = false) + (hx : vspace.extra ex 0#usize = ok (some x)) + (hc : obj.Slot.cap st x s = ok (capability.Cap.Ept p i b q r)) : + vspace.decode st ar s a lbl length slot (capability.Cap.AsidPool ptr base) call buf ex ⦃ ErrKeepA s a ⦄ := by + have hS : StoreTotal st := L.total + unfold vspace.decode + simp only [cur_thread_ok, bind_ok, hl, Bool.false_eq_true, ↓reduceIte, hx, hc] + split + · errk_steps3 + all_goals (try errk_close3) + · simp only [error.invalid_cap, bind_ok, WP.spec_ok, ErrKeepA] + intros + first | trivial | rfl + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 16000000 in +theorem vspace.decode.asidPool_other0 (hA : ArchTotal ar) (s : S) (a : A) (lbl length : Std.U64) (slot : obj.Slot) + (ptr base : Std.U64) (call : Bool) (buf : Std.U64) (ex : Array obj.Slot 3#usize) (x : obj.Slot) + (c : capability.Cap) (hg : aspGrp c = 0) + (hl : (lbl != fl4_abi.label.ARMASIDPoolAssign) = false) + (hx : vspace.extra ex 0#usize = ok (some x)) + (hc : obj.Slot.cap st x s = ok c) : + vspace.decode st ar s a lbl length slot (capability.Cap.AsidPool ptr base) call buf ex ⦃ ErrKeepA s a ⦄ := by + have hS : StoreTotal st := L.total + cases c + all_goals first | (simp [aspGrp] at hg; done) | + (unfold vspace.decode + simp only [cur_thread_ok, bind_ok, hl, Bool.false_eq_true, ↓reduceIte, hx, hc] + simp only [error.invalid_cap, bind_ok, WP.spec_ok, ErrKeepA] + intros + first | trivial | rfl) | + (unfold vspace.decode + simp only [cur_thread_ok, bind_ok, hl, Bool.false_eq_true, ↓reduceIte, hx, hc] + errk_steps3 + all_goals (try errk_close3)) + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 16000000 in +theorem vspace.decode.asidPool_other1 (hA : ArchTotal ar) (s : S) (a : A) (lbl length : Std.U64) (slot : obj.Slot) + (ptr base : Std.U64) (call : Bool) (buf : Std.U64) (ex : Array obj.Slot 3#usize) (x : obj.Slot) + (c : capability.Cap) (hg : aspGrp c = 1) + (hl : (lbl != fl4_abi.label.ARMASIDPoolAssign) = false) + (hx : vspace.extra ex 0#usize = ok (some x)) + (hc : obj.Slot.cap st x s = ok c) : + vspace.decode st ar s a lbl length slot (capability.Cap.AsidPool ptr base) call buf ex ⦃ ErrKeepA s a ⦄ := by + have hS : StoreTotal st := L.total + cases c + all_goals first | (simp [aspGrp] at hg; done) | + (unfold vspace.decode + simp only [cur_thread_ok, bind_ok, hl, Bool.false_eq_true, ↓reduceIte, hx, hc] + simp only [error.invalid_cap, bind_ok, WP.spec_ok, ErrKeepA] + intros + first | trivial | rfl) | + (unfold vspace.decode + simp only [cur_thread_ok, bind_ok, hl, Bool.false_eq_true, ↓reduceIte, hx, hc] + errk_steps3 + all_goals (try errk_close3)) + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 16000000 in +theorem vspace.decode.asidPool_other2 (hA : ArchTotal ar) (s : S) (a : A) (lbl length : Std.U64) (slot : obj.Slot) + (ptr base : Std.U64) (call : Bool) (buf : Std.U64) (ex : Array obj.Slot 3#usize) (x : obj.Slot) + (c : capability.Cap) (hg : aspGrp c = 2) + (hl : (lbl != fl4_abi.label.ARMASIDPoolAssign) = false) + (hx : vspace.extra ex 0#usize = ok (some x)) + (hc : obj.Slot.cap st x s = ok c) : + vspace.decode st ar s a lbl length slot (capability.Cap.AsidPool ptr base) call buf ex ⦃ ErrKeepA s a ⦄ := by + have hS : StoreTotal st := L.total + cases c + all_goals first | (simp [aspGrp] at hg; done) | + (unfold vspace.decode + simp only [cur_thread_ok, bind_ok, hl, Bool.false_eq_true, ↓reduceIte, hx, hc] + simp only [error.invalid_cap, bind_ok, WP.spec_ok, ErrKeepA] + intros + first | trivial | rfl) | + (unfold vspace.decode + simp only [cur_thread_ok, bind_ok, hl, Bool.false_eq_true, ↓reduceIte, hx, hc] + errk_steps3 + all_goals (try errk_close3)) + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 8000000 in +theorem vspace.decode.asidPool_other3 (hA : ArchTotal ar) (s : S) (a : A) (lbl length : Std.U64) (slot : obj.Slot) + (ptr base : Std.U64) (call : Bool) (buf : Std.U64) (ex : Array obj.Slot 3#usize) (x : obj.Slot) + (c : capability.Cap) (hg : aspGrp c = 3) + (hl : (lbl != fl4_abi.label.ARMASIDPoolAssign) = false) + (hx : vspace.extra ex 0#usize = ok (some x)) + (hc : obj.Slot.cap st x s = ok c) : + vspace.decode st ar s a lbl length slot (capability.Cap.AsidPool ptr base) call buf ex ⦃ ErrKeepA s a ⦄ := by + have hS : StoreTotal st := L.total + cases c + all_goals first | (simp [aspGrp] at hg; done) | + (unfold vspace.decode + simp only [cur_thread_ok, bind_ok, hl, Bool.false_eq_true, ↓reduceIte, hx, hc] + simp only [error.invalid_cap, bind_ok, WP.spec_ok, ErrKeepA] + intros + first | trivial | rfl) | + (unfold vspace.decode + simp only [cur_thread_ok, bind_ok, hl, Bool.false_eq_true, ↓reduceIte, hx, hc] + errk_steps3 + all_goals (try errk_close3)) + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 8000000 in +theorem vspace.decode.asidPool_other4 (hA : ArchTotal ar) (s : S) (a : A) (lbl length : Std.U64) (slot : obj.Slot) + (ptr base : Std.U64) (call : Bool) (buf : Std.U64) (ex : Array obj.Slot 3#usize) (x : obj.Slot) + (c : capability.Cap) (hg : aspGrp c = 4) + (hl : (lbl != fl4_abi.label.ARMASIDPoolAssign) = false) + (hx : vspace.extra ex 0#usize = ok (some x)) + (hc : obj.Slot.cap st x s = ok c) : + vspace.decode st ar s a lbl length slot (capability.Cap.AsidPool ptr base) call buf ex ⦃ ErrKeepA s a ⦄ := by + have hS : StoreTotal st := L.total + cases c + all_goals first | (simp [aspGrp] at hg; done) | + (unfold vspace.decode + simp only [cur_thread_ok, bind_ok, hl, Bool.false_eq_true, ↓reduceIte, hx, hc] + simp only [error.invalid_cap, bind_ok, WP.spec_ok, ErrKeepA] + intros + first | trivial | rfl) | + (unfold vspace.decode + simp only [cur_thread_ok, bind_ok, hl, Bool.false_eq_true, ↓reduceIte, hx, hc] + errk_steps3 + all_goals (try errk_close3)) + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 8000000 in +theorem vspace.decode.asidPool_other5 (hA : ArchTotal ar) (s : S) (a : A) (lbl length : Std.U64) (slot : obj.Slot) + (ptr base : Std.U64) (call : Bool) (buf : Std.U64) (ex : Array obj.Slot 3#usize) (x : obj.Slot) + (c : capability.Cap) (hg : aspGrp c = 5) + (hl : (lbl != fl4_abi.label.ARMASIDPoolAssign) = false) + (hx : vspace.extra ex 0#usize = ok (some x)) + (hc : obj.Slot.cap st x s = ok c) : + vspace.decode st ar s a lbl length slot (capability.Cap.AsidPool ptr base) call buf ex ⦃ ErrKeepA s a ⦄ := by + have hS : StoreTotal st := L.total + cases c + all_goals first | (simp [aspGrp] at hg; done) | + (unfold vspace.decode + simp only [cur_thread_ok, bind_ok, hl, Bool.false_eq_true, ↓reduceIte, hx, hc] + simp only [error.invalid_cap, bind_ok, WP.spec_ok, ErrKeepA] + intros + first | trivial | rfl) | + (unfold vspace.decode + simp only [cur_thread_ok, bind_ok, hl, Bool.false_eq_true, ↓reduceIte, hx, hc] + errk_steps3 + all_goals (try errk_close3)) + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 8000000 in +theorem vspace.decode.asidPool_other6 (hA : ArchTotal ar) (s : S) (a : A) (lbl length : Std.U64) (slot : obj.Slot) + (ptr base : Std.U64) (call : Bool) (buf : Std.U64) (ex : Array obj.Slot 3#usize) (x : obj.Slot) + (c : capability.Cap) (hg : aspGrp c = 6) + (hl : (lbl != fl4_abi.label.ARMASIDPoolAssign) = false) + (hx : vspace.extra ex 0#usize = ok (some x)) + (hc : obj.Slot.cap st x s = ok c) : + vspace.decode st ar s a lbl length slot (capability.Cap.AsidPool ptr base) call buf ex ⦃ ErrKeepA s a ⦄ := by + have hS : StoreTotal st := L.total + cases c + all_goals first | (simp [aspGrp] at hg; done) | + (unfold vspace.decode + simp only [cur_thread_ok, bind_ok, hl, Bool.false_eq_true, ↓reduceIte, hx, hc] + simp only [error.invalid_cap, bind_ok, WP.spec_ok, ErrKeepA] + intros + first | trivial | rfl) | + (unfold vspace.decode + simp only [cur_thread_ok, bind_ok, hl, Bool.false_eq_true, ↓reduceIte, hx, hc] + errk_steps3 + all_goals (try errk_close3)) + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 8000000 in +theorem vspace.decode.asidPool_other7 (hA : ArchTotal ar) (s : S) (a : A) (lbl length : Std.U64) (slot : obj.Slot) + (ptr base : Std.U64) (call : Bool) (buf : Std.U64) (ex : Array obj.Slot 3#usize) (x : obj.Slot) + (c : capability.Cap) (hg : aspGrp c = 7) + (hl : (lbl != fl4_abi.label.ARMASIDPoolAssign) = false) + (hx : vspace.extra ex 0#usize = ok (some x)) + (hc : obj.Slot.cap st x s = ok c) : + vspace.decode st ar s a lbl length slot (capability.Cap.AsidPool ptr base) call buf ex ⦃ ErrKeepA s a ⦄ := by + have hS : StoreTotal st := L.total + cases c + all_goals first | (simp [aspGrp] at hg; done) | + (unfold vspace.decode + simp only [cur_thread_ok, bind_ok, hl, Bool.false_eq_true, ↓reduceIte, hx, hc] + simp only [error.invalid_cap, bind_ok, WP.spec_ok, ErrKeepA] + intros + first | trivial | rfl) | + (unfold vspace.decode + simp only [cur_thread_ok, bind_ok, hl, Bool.false_eq_true, ↓reduceIte, hx, hc] + errk_steps3 + all_goals (try errk_close3)) + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 8000000 in +theorem vspace.decode.asidPool_other8 (hA : ArchTotal ar) (s : S) (a : A) (lbl length : Std.U64) (slot : obj.Slot) + (ptr base : Std.U64) (call : Bool) (buf : Std.U64) (ex : Array obj.Slot 3#usize) (x : obj.Slot) + (c : capability.Cap) (hg : aspGrp c = 8) + (hl : (lbl != fl4_abi.label.ARMASIDPoolAssign) = false) + (hx : vspace.extra ex 0#usize = ok (some x)) + (hc : obj.Slot.cap st x s = ok c) : + vspace.decode st ar s a lbl length slot (capability.Cap.AsidPool ptr base) call buf ex ⦃ ErrKeepA s a ⦄ := by + have hS : StoreTotal st := L.total + cases c + all_goals first | (simp [aspGrp] at hg; done) | + (unfold vspace.decode + simp only [cur_thread_ok, bind_ok, hl, Bool.false_eq_true, ↓reduceIte, hx, hc] + simp only [error.invalid_cap, bind_ok, WP.spec_ok, ErrKeepA] + intros + first | trivial | rfl) | + (unfold vspace.decode + simp only [cur_thread_ok, bind_ok, hl, Bool.false_eq_true, ↓reduceIte, hx, hc] + errk_steps3 + all_goals (try errk_close3)) + +theorem vspace.decode.asidPool_other (hA : ArchTotal ar) (s : S) (a : A) (lbl length : Std.U64) (slot : obj.Slot) + (ptr base : Std.U64) (call : Bool) (buf : Std.U64) (ex : Array obj.Slot 3#usize) (x : obj.Slot) + (c : capability.Cap) (h1 : ∀ p v b, c ≠ capability.Cap.Pml4 p b v) + (h2 : ∀ p v b, c ≠ capability.Cap.HighVSpace p b v) + (h3 : ∀ p i q r b, c ≠ capability.Cap.Ept p i b q r) + (hl : (lbl != fl4_abi.label.ARMASIDPoolAssign) = false) + (hx : vspace.extra ex 0#usize = ok (some x)) + (hc : obj.Slot.cap st x s = ok c) : + vspace.decode st ar s a lbl length slot (capability.Cap.AsidPool ptr base) call buf ex ⦃ ErrKeepA s a ⦄ := by + have hlt : aspGrp c < 9 := by + cases c + all_goals first | exact absurd rfl (h1 _ _ _) | exact absurd rfl (h2 _ _ _) | exact absurd rfl (h3 _ _ _ _ _) | (simp [aspGrp]) + have : aspGrp c = 0 ∨ aspGrp c = 1 ∨ aspGrp c = 2 ∨ aspGrp c = 3 ∨ aspGrp c = 4 ∨ aspGrp c = 5 ∨ aspGrp c = 6 ∨ + aspGrp c = 7 ∨ aspGrp c = 8 := by omega + rcases this with h | h | h | h | h | h | h | h | h + · exact vspace.decode.asidPool_other0 hA s a lbl length slot ptr base call buf ex x c h hl hx hc + · exact vspace.decode.asidPool_other1 hA s a lbl length slot ptr base call buf ex x c h hl hx hc + · exact vspace.decode.asidPool_other2 hA s a lbl length slot ptr base call buf ex x c h hl hx hc + · exact vspace.decode.asidPool_other3 hA s a lbl length slot ptr base call buf ex x c h hl hx hc + · exact vspace.decode.asidPool_other4 hA s a lbl length slot ptr base call buf ex x c h hl hx hc + · exact vspace.decode.asidPool_other5 hA s a lbl length slot ptr base call buf ex x c h hl hx hc + · exact vspace.decode.asidPool_other6 hA s a lbl length slot ptr base call buf ex x c h hl hx hc + · exact vspace.decode.asidPool_other7 hA s a lbl length slot ptr base call buf ex x c h hl hx hc + · exact vspace.decode.asidPool_other8 hA s a lbl length slot ptr base call buf ex x c h hl hx hc + +set_option maxRecDepth 100000 in +set_option maxHeartbeats 16000000 in +theorem vspace.decode.errKA_asidPool (hA : ArchTotal ar) (s : S) (a : A) (lbl length : Std.U64) (slot : obj.Slot) + (ptr base : Std.U64) (call : Bool) (buf : Std.U64) (ex : Array obj.Slot 3#usize) : + vspace.decode st ar s a lbl length slot (capability.Cap.AsidPool ptr base) call buf ex ⦃ ErrKeepA s a ⦄ := by + have hS : StoreTotal st := L.total + by_cases hl : (lbl != fl4_abi.label.ARMASIDPoolAssign) = true + · unfold vspace.decode + simp only [cur_thread_ok, bind_ok, hl, ↓reduceIte] + errk_steps3 + all_goals (try errk_close3) + have hl' : (lbl != fl4_abi.label.ARMASIDPoolAssign) = false := by simpa only [Bool.not_eq_true] using hl + obtain ⟨o, ho⟩ : ∃ o, vspace.extra ex 0#usize = ok o := by + have hsp := vspace.extra.p0 ex 0#usize + revert hsp + cases vspace.extra ex 0#usize <;> simp [WP.spec_vis, WP.spec_div, WP.spec_ok] + cases o with + | none => + unfold vspace.decode + simp only [cur_thread_ok, bind_ok, hl', Bool.false_eq_true, ↓reduceIte, ho] + errk_steps3 + all_goals (try errk_close3) + | some x => + have hc : obj.Slot.cap st x s = ok (capA st s x) := capA_eq s x + generalize capA st s x = c at hc + by_cases h1 : ∃ p v b, c = capability.Cap.Pml4 p b v + · obtain ⟨p, v, b, rfl⟩ := h1 + exact vspace.decode.asidPool_pml4 hA s a lbl length slot ptr base call buf ex x p v b hl' ho hc + by_cases h2 : ∃ p v b, c = capability.Cap.HighVSpace p b v + · obtain ⟨p, v, b, rfl⟩ := h2 + exact vspace.decode.asidPool_high hA s a lbl length slot ptr base call buf ex x p v b hl' ho hc + by_cases h3 : ∃ p i q r b, c = capability.Cap.Ept p i b q r + · obtain ⟨p, i, q, r, b, rfl⟩ := h3 + exact vspace.decode.asidPool_ept hA s a lbl length slot ptr base call buf ex x p i q r b hl' ho hc + exact vspace.decode.asidPool_other hA s a lbl length slot ptr base call buf ex x c + (fun p v b e => h1 ⟨p, v, b, e⟩) (fun p v b e => h2 ⟨p, v, b, e⟩) + (fun p i q r b e => h3 ⟨p, i, q, r, b, e⟩) hl' ho hc + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3ErrPreA.lean b/proofs/Fl4ProofsA64/P3/Stage3ErrPreA.lean new file mode 100644 index 000000000..47bf30cea --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ErrPreA.lean @@ -0,0 +1,159 @@ +/- +P3 (aarch64, A64-P3 Lift 1b): **the deletion and revocation loops fail only by preemption**. + +* `PreOnlyA r`: an `Err` of `r` is `Preempted`. +* `sched.preemption_point.preA`, `cdt.finalise_slot_deferred.preA`, `cdt.delete_deferred.preA`, + `cdt.finalise_slot_immediate.preA`, `cdt.delete.preA`, `cdt.revoke.preA`. + +Used by `P3/Stage3ErrKeepA` (an `Err` other than `Preempted` of a decoder keeps the state). No named premise beyond +P0's totality (`ArchTotal`). New module. +-/ +import Fl4ProofsA64.P0.Auto +import Fl4ProofsA64.P1.Store +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P0 + +noncomputable section + +/-- An `Err` of `r` is a preemption. -/ +def PreOnlyA {α : Type} (r : core.result.Result α error.Exc) : Prop := ∀ e, r = .Err e → e = .Preempted + +theorem PreOnlyA.ok {α : Type} (v : α) : PreOnlyA (.Ok v : core.result.Result α error.Exc) := + fun _ h => by cases h + +theorem PreOnlyA.pre {α : Type} : PreOnlyA (.Err .Preempted : core.result.Result α error.Exc) := + fun _ h => by cases h; rfl + +/-- Close a leaf of a loop body or of a function with a `PreOnlyA` post. -/ +macro "pre_close" : tactic => `(tactic| first + | exact PreOnlyA.ok _ + | exact PreOnlyA.pre + | exact (WP.spec_ok _).2 (PreOnlyA.ok _) + | exact (WP.spec_ok _).2 PreOnlyA.pre + | (intro e he; simp_all [PreOnlyA]; done) + | (simp_all [PreOnlyA]; done) + | (refine ⟨trivial, ?_⟩; dsimp only; scalar_tac) + | (simp only [WP.spec_ok] + first + | exact PreOnlyA.ok _ + | exact PreOnlyA.pre + | (refine ⟨trivial, ?_⟩; dsimp only; scalar_tac) + | (intro e he; simp_all [PreOnlyA]; done) + | (simp_all [PreOnlyA]; done))) + +open Lean Elab Tactic Meta in +/-- Case-split every `Result` local (so the `match`es of `branch`'s step spec reduce). -/ +elab "pre_cases_res" : tactic => do + let g ← getMainGoal + let fvs ← g.withContext do + let mut acc : Array FVarId := #[] + for d in (← getLCtx) do + if d.isImplementationDetail then continue + let ty ← instantiateMVars d.type + if ty.isAppOf ``core.result.Result then acc := acc.push d.fvarId + return acc + let mut goals : List MVarId := [g] + for fv in fvs do + let mut ng : List MVarId := [] + for g' in goals do + try + let subs ← g'.cases fv + ng := ng ++ (subs.toList.map (·.mvarId)) + catch _ => ng := ng ++ [g'] + goals := ng + replaceMainGoal goals + +/-- The residual leaves: case on the results and simplify. -/ +macro "pre_res" : tactic => `(tactic| ((try subst_vars); pre_cases_res; all_goals (simp_all [PreOnlyA]; done))) + +/-- Step through a body as P0's `p0_split` does. -/ +macro "pre_steps" : tactic => + `(tactic| (repeat' (first | step | (simp only [bind_tc_ok, bind_ok]) | split | p0_cases_prod))) + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : Fl4ProofsA64.P1.LawfulStore st] + +theorem sched.preemption_point.preA (hA : ArchTotal ar) (s : S) (a : A) : + sched.preemption_point st ar s a ⦃ x => PreOnlyA x.1 ⦄ := by + have hS : StoreTotal st := L.total + unfold sched.preemption_point + pre_steps + all_goals (try pre_close) + all_goals pre_res + +attribute [local step] sched.preemption_point.preA + +set_option maxHeartbeats 4000000 in +theorem cdt.finalise_slot_deferred.preA (hA : ArchTotal ar) (s : S) (a : A) (slot : obj.Slot) : + cdt.finalise_slot_deferred st ar s a slot ⦃ x => PreOnlyA x.1 ⦄ := by + have hS : StoreTotal st := L.total + unfold cdt.finalise_slot_deferred cdt.finalise_slot_deferred_loop + apply loop.spec_decr_nat (measure := fun x => match x with | (x0, x1, x2) => x2.val) (inv := fun _ => True) + · rintro ⟨x0, x1, x2⟩ - + unfold cdt.finalise_slot_deferred_loop.body + pre_steps + all_goals (try pre_close) + all_goals pre_res + · trivial + +set_option maxHeartbeats 4000000 in +theorem cdt.revoke_loop.preA (hA : ArchTotal ar) (s : S) (a : A) (slot : obj.Slot) (fuel : U64) + (hdel : ∀ s a slot, cdt.delete st ar s a slot true ⦃ x => PreOnlyA x.1 ⦄) : + cdt.revoke_loop st ar s a slot fuel ⦃ x => PreOnlyA x.1 ⦄ := by + have hS : StoreTotal st := L.total + unfold cdt.revoke_loop + apply loop.spec_decr_nat (measure := fun x => match x with | (x0, x1, x2) => x2.val) (inv := fun _ => True) + · rintro ⟨x0, x1, x2⟩ - + unfold cdt.revoke_loop.body + pre_steps + all_goals (try pre_close) + all_goals pre_res + · trivial + +set_option maxHeartbeats 4000000 in +theorem cdt.delete_deferred.preA (hA : ArchTotal ar) (s : S) (a : A) (slot : obj.Slot) : + cdt.delete_deferred st ar s a slot ⦃ x => PreOnlyA x.1 ⦄ := by + have hS : StoreTotal st := L.total + have hf := fun s a slot => cdt.finalise_slot_deferred.preA (st := st) hA s a slot + unfold cdt.delete_deferred + pre_steps + all_goals (try pre_close) + all_goals pre_res + +set_option maxHeartbeats 16000000 in +theorem cdt.finalise_slot_immediate.preA (hA : ArchTotal ar) (s : S) (a : A) (slot : obj.Slot) : + cdt.finalise_slot_immediate st ar s a slot ⦃ x => PreOnlyA x.1 ⦄ := by + have hS : StoreTotal st := L.total + have hdd := fun s a slot => cdt.delete_deferred.preA (st := st) hA s a slot + unfold cdt.finalise_slot_immediate cdt.finalise_slot_immediate_loop + apply loop.spec_decr_nat (measure := fun x => match x with | (x0, x1, x2) => x2.val) (inv := fun _ => True) + · rintro ⟨x0, x1, x2⟩ - + unfold cdt.finalise_slot_immediate_loop.body + pre_steps + all_goals (try pre_close) + all_goals pre_res + · trivial + +set_option maxHeartbeats 4000000 in +theorem cdt.delete.preA (hA : ArchTotal ar) (s : S) (a : A) (slot : obj.Slot) (exposed : Bool) : + cdt.delete st ar s a slot exposed ⦃ x => PreOnlyA x.1 ⦄ := by + have hS : StoreTotal st := L.total + have hfi := fun s a slot => cdt.finalise_slot_immediate.preA (st := st) hA s a slot + have hdd := fun s a slot => cdt.delete_deferred.preA (st := st) hA s a slot + unfold cdt.delete + pre_steps + all_goals (try pre_close) + all_goals pre_res + +theorem cdt.revoke.preA (hA : ArchTotal ar) (s : S) (a : A) (slot : obj.Slot) : + cdt.revoke st ar s a slot ⦃ x => PreOnlyA x.1 ⦄ := by + unfold cdt.revoke + exact cdt.revoke_loop.preA hA s a slot _ fun s a slot => cdt.delete.preA hA s a slot true + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3ExitAtA.lean b/proofs/Fl4ProofsA64/P3/Stage3ExitAtA.lean new file mode 100644 index 000000000..04c3854b6 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ExitAtA.lean @@ -0,0 +1,257 @@ +/- +P3 (aarch64, A64-P3 Stage 3, T3 G4b / F8, queue item 2 and the restated exit): **`hSw` across `awaken` and +`checkDomainTime`, and `exit_spec` at the one post-fire state `σ1` of the actual schedule**. + +* `RelRun σ`: every release-queue thread is runnable (`SchedSRN.run`, `J.sr.run.2`). +* `hSw_of`: from `FIA σ1`, `SwA σ1` and `RelRun σ1`, a pending switch target is `schedulable` after `awaken` and + `checkDomainTime` (`awaken`'s `possibleSwitchTo` makes the release-queue head the target, which is `schedulable` once + removed from the release queue: runnable by `RelRun`, active SC by F8(d)). `checkDomainTime` ends in + `rescheduleRequired`, which makes the action `chooseNewThread`. +* `FireRecA σ1` := `FIA σ1 ∧ IdleSt σ1 ∧ SwA σ1 ∧ RelRun σ1`: **the one named hypothesis** of the restated exit + theorems. It is "the record holds at `σ1` after the timer fires, with `YieldQueueRelA`": `FIA` is `schedValid` + (`JStage3A.schedValid`), `IdleSt` is `SInvXA.idleTcb`, `SwA` is `ActIdleA.1`, `RelRun` is `SchedSRN.run` (`sr.run`). + **Intended owner: the kernel-entry composition on entry**, which proves the record-level loop over `fl4TimerFires` + (Lift 2a's signal arms); `sendSignal_fia` (`P3/Stage3SigSchedValidA`) is the F8 and `IdleSt` part of one signal arm. + Not done here: `SwA` (`ActIdleA.1`) kept by one `sendSignal`. It needs the release-queue insertion of `postpone` + (the woken thread is not the pending target: it was blocked) and `possibleSwitchTo_swa` (below) for the `schedulable` + guard; it is the owner's part of the record-level signal arm, with `FireRecA`'s `SwA σ1` as its output. +* `exit_spec_at`, `JSyscallA.ofSchedExitAt`: `exit_spec` and `ofSchedExit` with `FireRecA σ1` at the actual `σ1` + (and the schedule split as the fires, then `schedTail`) instead of the universally quantified `hAwk`/`hSw`/`hidle`. + +Named premises: `FireRecA σ1` (above), the exit's non-idle current thread and the activation's `legal` / `hn`. + +No other premise; new module. +-/ +import Fl4ProofsA64.P3.Stage3ExitHcA +import Fl4ProofsA64.P3.Stage3FinA +import Fl4ProofsA64.P3.Stage3EntryProjA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Classical + +noncomputable section + +/-- Every release-queue thread is runnable. -/ +def RelRun (σ : State Unit) : Prop := + ∀ t ∈ σ.releaseQueue, ∃ x, σ.kheap t = some (.tcb x) ∧ runnable x.tcbState = true + +theorem HT.and {α : Type} {m : SMonad Unit α} {P : State Unit → Prop} {Q1 Q2 : α → State Unit → Prop} + (h1 : HT P m Q1) (h2 : HT P m Q2) : HT P m (fun a σ => Q1 a σ ∧ Q2 a σ) := + fun σ hσ r hr => ⟨h1 σ hσ r hr, h2 σ hσ r hr⟩ + +theorem swa_of_choose {σ : State Unit} (h : σ.schedulerAction = .chooseNewThread) : SwA σ := + fun t e => by rw [h] at e; cases e + +theorem schedulable_transfer {σ σ' : State Unit} {u : ObjRef} (h : schedulable u σ = true) + (hk : σ'.kheap = σ.kheap) (hr : ∀ x ∈ σ'.releaseQueue, x ∈ σ.releaseQueue) : schedulable u σ' = true := by + obtain ⟨a, n, x, ex, hrun⟩ := schedulable_actA h + exact schedulable_of_act (a.ofKh hk) (fun m => n (hr _ m)) ⟨x, by rw [hk]; exact ex, hrun⟩ + +theorem tcbSchedAction_keep (f : ObjRef → List ObjRef → List ObjRef) (t : ObjRef) (R : State Unit → Prop) + (hR : ∀ σ g, R σ → R { σ with readyQueues := g }) : + HT R (tcbSchedAction (Z := Unit) f t) (fun _ σ => R σ) := by + unfold Fl4SpecA64.tcbSchedAction + refine HT.bind (Q := fun _ σ => R σ) (HT.threadGet _ t fun σ hσ x hx => hσ) fun d => ?_ + refine HT.bind (Q := fun _ σ => R σ) (HT.threadGet _ t fun σ hσ x hx => hσ) fun p => ?_ + unfold Fl4SpecA64.getTcbQueue + refine HT.bind (Q := fun _ σ => R σ) + (HT.bind (Q := fun _ σ => R σ) (HT.gets _ fun σ hσ => hσ) fun qs => HT.pure _ fun σ hσ => hσ) fun queue => ?_ + refine HT.bind (Q := fun _ σ => R σ) (HT.pure _ fun σ hσ => hσ) fun queue' => ?_ + unfold Fl4SpecA64.setTcbQueue + exact HT.modify _ fun σ hσ => hR σ _ hσ + +/-- `rescheduleRequired` ends with the action `chooseNewThread`. -/ +theorem resched_choose : + HT (fun _ => True) (rescheduleRequired (Z := Unit)) (fun _ σ => σ.schedulerAction = .chooseNewThread) := by + unfold Fl4SpecA64.rescheduleRequired + have fin : HT (fun _ => True) (setSchedulerAction (Z := Unit) .chooseNewThread) + (fun _ σ => σ.schedulerAction = .chooseNewThread) := by + unfold Fl4SpecA64.setSchedulerAction + exact HT.modify _ fun _ _ => rfl + refine HT.bind (Q := fun _ _ => True) (HT.gets _ fun σ _ => trivial) fun action => ?_ + cases action with + | switchThread t => + dsimp only + refine HT.bind (Q := fun _ _ => True) (HT.gets _ fun σ _ => trivial) fun sched => ?_ + exact HT.bind (Q := fun _ _ => True) (HT.when (fun _ σ _ r hr => trivial) (fun _ σ _ => trivial)) fun _ => fin + | resumeCurThread => dsimp only; exact fin + | chooseNewThread => dsimp only; exact fin + +/-- **`possibleSwitchTo` keeps `SwA`** when its target is `schedulable` (the only branch that makes it the pending +target). -/ +theorem possibleSwitchTo_swa (target : ObjRef) : + HT (fun σ => SwA σ ∧ schedulable target σ = true) (possibleSwitchTo (Z := Unit) target) + (fun _ σ => SwA σ) := by + unfold Fl4SpecA64.possibleSwitchTo Fl4SpecA64.getTcbObjRef + refine HT.bind (Q := fun _ σ => SwA σ ∧ schedulable target σ = true) (HT.threadGet _ target fun σ hσ x hx => hσ) + fun scOpt => ?_ + refine HT.bind (Q := fun _ σ => SwA σ ∧ schedulable target σ = true) (HT.gets _ fun σ hσ => hσ) fun inq => ?_ + refine HT.when (fun hc => ?_) (fun _ σ hσ => hσ.1) + refine HT.bind (Q := fun _ σ => SwA σ ∧ schedulable target σ = true) (HT.gets _ fun σ hσ => hσ) fun curDom => ?_ + refine HT.bind (Q := fun _ σ => SwA σ ∧ schedulable target σ = true) + (HT.threadGet _ target fun σ hσ x hx => hσ) fun targetDom => ?_ + refine HT.bind (Q := fun _ σ => SwA σ ∧ schedulable target σ = true) (HT.gets _ fun σ hσ => hσ) fun action => ?_ + have enqT : HT (fun σ => SwA σ) (tcbSchedAction (Z := Unit) tcbSchedEnqueue target) (fun _ σ => SwA σ) := + tcbSchedAction_keep _ target (fun σ => SwA σ) (fun σ g h => h) + refine HT.mono (Q := fun _ σ => SwA σ) ?_ (fun σ hσ => hσ) (fun _ σ h => h) + refine HT.mono (P := fun σ => SwA σ ∧ schedulable target σ = true) ?_ (fun σ hσ => hσ) (fun _ σ h => h) + refine HT.ite (fun _ => HT.mono enqT (fun σ h => h.1) (fun _ _ h => h)) (fun _ => ?_) + refine HT.ite (fun _ => ?_) (fun _ => ?_) + · exact HT.bind (Q := fun _ σ => SwA σ) + (HT.mono resched_choose (fun _ _ => trivial) (fun _ σ h => swa_of_choose h)) fun _ => enqT + · unfold Fl4SpecA64.setSchedulerAction + exact HT.modify _ fun σ hσ t h => by + injection h with e + subst e + exact hσ.2 + +/-- The thread's TCB (if any) is runnable. -/ +def RunT (t : ObjRef) (σ : State Unit) : Prop := ∀ x, σ.kheap t = some (.tcb x) → runnable x.tcbState = true + +/-- Release-queue removal keeps `FIA`, `SwA` and `RelRun`; a thread with an active SC and a runnable TCB is +`schedulable` once removed. -/ +theorem relRemove_inv (t : ObjRef) : + HT (fun σ => (FIA σ ∧ SwA σ ∧ RelRun σ) ∧ ActTcbA σ t ∧ RunT t σ) (tcbReleaseRemove (Z := Unit) t) + (fun _ σ => (FIA σ ∧ SwA σ ∧ RelRun σ) ∧ schedulable t σ = true) := by + unfold Fl4SpecA64.tcbReleaseRemove + refine HT.bind (Q := fun qs σ => ((FIA σ ∧ SwA σ ∧ RelRun σ) ∧ ActTcbA σ t ∧ RunT t σ) ∧ qs = σ.releaseQueue) + (HT.gets _ fun σ hσ => ⟨hσ, rfl⟩) fun qs => ?_ + refine HT.bind (Q := fun _ σ => ((FIA σ ∧ SwA σ ∧ RelRun σ) ∧ ActTcbA σ t ∧ RunT t σ) ∧ qs = σ.releaseQueue) + (HT.when (fun _ => HT.modify _ fun σ hσ => + ⟨⟨⟨hσ.1.1.1.ofSame rfl rfl rfl rfl rfl rfl rfl, hσ.1.1.2.1, hσ.1.1.2.2⟩, hσ.1.2⟩, hσ.2⟩) + (fun _ σ hσ => hσ)) fun _ => ?_ + refine HT.bind (Q := fun qs' σ => (((FIA σ ∧ SwA σ ∧ RelRun σ) ∧ ActTcbA σ t ∧ RunT t σ) ∧ qs = σ.releaseQueue) ∧ + qs' = tcbSchedDequeue t qs) (HT.pure _ fun σ hσ => ⟨hσ, rfl⟩) fun qs' => ?_ + refine HT.modify _ fun σ hσ => ?_ + obtain ⟨⟨⟨⟨hI, hS, hR⟩, hA, hRun⟩, hq⟩, hq'⟩ := hσ + have sub : ∀ x ∈ qs', x ∈ σ.releaseQueue := fun x hx => by + rw [hq'] at hx + rw [← hq] + exact (List.mem_filter.1 hx).1 + have hn : t ∉ qs' := fun hm => by + rw [hq'] at hm + exact (by simpa using (List.mem_filter.1 hm).2 : False) + refine ⟨⟨hI.ofRelSub rfl rfl sub rfl rfl rfl rfl, fun u hu => schedulable_transfer (hS u hu) rfl sub, + fun u hu => hR u (sub u hu)⟩, ?_⟩ + exact schedulable_of_act (hA.ofKh rfl) hn (by obtain ⟨x, sc, ex, -⟩ := hA; exact ⟨x, ex, hRun x ex⟩) + +/-- One `awaken` iteration keeps `FIA`, `SwA` and `RelRun`. -/ +theorem tcbReleaseDequeue_swa : + HT (fun σ => FIA σ ∧ SwA σ ∧ RelRun σ) (tcbReleaseDequeue (Z := Unit)) + (fun _ σ => FIA σ ∧ SwA σ ∧ RelRun σ) := by + unfold Fl4SpecA64.tcbReleaseDequeue + refine HT.bind (Q := fun rq σ => (FIA σ ∧ SwA σ ∧ RelRun σ) ∧ rq = σ.releaseQueue) + (HT.gets _ fun σ hσ => ⟨hσ, rfl⟩) fun rq => ?_ + refine HT.bind (Q := fun _ σ => ((FIA σ ∧ SwA σ ∧ RelRun σ) ∧ rq = σ.releaseQueue) ∧ rq ≠ []) + (HT.assert fun hne σ hσ => ⟨hσ, hne⟩) fun _ => ?_ + refine HT.bind (Q := fun _ σ => (FIA σ ∧ SwA σ ∧ RelRun σ) ∧ schedulable (the rq.head?) σ = true) ?_ fun _ => ?_ + · refine HT.mono (relRemove_inv (the rq.head?)) (fun σ hσ => ?_) (fun _ _ h => h) + obtain ⟨⟨⟨hI, hS, hR⟩, hq⟩, hne⟩ := hσ + have hmem : the rq.head? ∈ σ.releaseQueue := by + rw [← hq] + cases rq with + | nil => exact absurd rfl hne + | cons a l => exact List.mem_cons_self + obtain ⟨x, ex, hrun⟩ := hR _ hmem + refine ⟨⟨hI, hS, hR⟩, hI.2 _ hmem, fun y hy => ?_⟩ + rw [ex] at hy + have := KernelObject.tcb.inj (Option.some.inj hy) + subst this + exact hrun + · refine HT.mono (HT.and (P := fun σ => (FIA σ ∧ SwA σ ∧ RelRun σ) ∧ schedulable (the rq.head?) σ = true) + (HT.mono (possibleSwitchTo_fia' (the rq.head?) RelRun (fun σ f h => h) (fun σ a h => h)) + (fun σ h => ⟨h.1.1, BoundActive.ofAct (schedulable_actA h.2).1, h.1.2.2⟩) (fun _ _ h => h)) + (HT.mono (possibleSwitchTo_swa (the rq.head?)) (fun σ h => ⟨h.1.2.1, h.2⟩) (fun _ _ h => h))) + (fun σ h => h) (fun _ σ h => ⟨h.1.1, h.2, h.1.2⟩) + +theorem awaken_swa : HT (fun σ => FIA σ ∧ SwA σ ∧ RelRun σ) (awaken (Z := Unit)) + (fun _ σ => FIA σ ∧ SwA σ ∧ RelRun σ) := by + unfold Fl4SpecA64.awaken + exact HT.whileLoop (fun σ => FIA σ ∧ SwA σ ∧ RelRun σ) () fun _ => tcbReleaseDequeue_swa + +theorem checkDomainTime_swa : HT (fun σ => SwA σ) (checkDomainTime (Z := Unit)) (fun _ σ => SwA σ) := by + unfold Fl4SpecA64.checkDomainTime + refine HT.bind (Q := fun _ σ => SwA σ) (HT.gets _ fun σ hσ => hσ) fun exp => ?_ + refine HT.when (fun _ => ?_) (fun _ σ hσ => hσ) + refine HT.bind (Q := fun _ σ => SwA σ) (HT.modify _ fun σ hσ => hσ) fun _ => ?_ + exact HT.mono resched_choose (fun _ _ => trivial) (fun _ σ h => swa_of_choose h) + +/-- **`hSw`** at the one post-fire state `σ1`: a pending switch target is `schedulable` after `awaken` and +`checkDomainTime`. -/ +theorem hSw_of {σ1 σ2 σ3 : State Unit} (hF : FIA σ1) (hS : SwA σ1) (hR : RelRun σ1) + (h2 : ((), σ2) ∈ (awaken (Z := Unit) σ1).1) (h3 : ((), σ3) ∈ (checkDomainTime (Z := Unit) σ2).1) : SwA σ3 := + checkDomainTime_swa σ2 (awaken_swa σ1 ⟨hF, hS, hR⟩ _ h2).2.1 _ h3 + +/-- **The record at the post-fire state** (module docstring): the one named hypothesis of the restated exit. -/ +def FireRecA (σ1 : State Unit) : Prop := FIA σ1 ∧ IdleSt σ1 ∧ SwA σ1 ∧ RelRun σ1 + +/-- The schedule after the timer pass. -/ +abbrev SchedTail (limit : Nat) (timer : State Unit → Fl4TimerView) : SMonad Unit Unit := + (do awaken (Z := Unit) + checkDomainTime + fl4SelectThread limit + fl4ScAndTimer timer) + +theorem fl4Schedule_of_tail (limit : Nat) (timer : State Unit → Fl4TimerView) {σ0 σ1 τ : State Unit} + (hf : fl4TimerFires σ0 σ1) (h : ((), τ) ∈ (SchedTail limit timer σ1).1) : + ((), τ) ∈ (fl4Schedule (Z := Unit) limit timer σ0).1 := by + unfold fl4Schedule + exact mem_bindA.2 ⟨((), σ1), mem_fl4Awaken.2 hf, h⟩ + +/-- **`exit_spec` at the actual post-fire state**: `schedule`'s tail from `σ1` (the record holding there) then +`activate_thread` leave an `FIA` state at `resumeCurThread`, with the idle TCB in `idleThreadState` after the tail. -/ +theorem exit_spec_at (limit : Nat) (timer : State Unit → Fl4TimerView) {σ0 σ1 τ1 τ2 : State Unit} + (hf : fl4TimerFires σ0 σ1) (hR : FireRecA σ1) (h1 : ((), τ1) ∈ (SchedTail limit timer σ1).1) + (h2 : ((), τ2) ∈ (fl4ActivateThread (Z := Unit) τ1).1) : + FIA τ2 ∧ τ2.schedulerAction = .resumeCurThread ∧ IdleSt τ1 := by + have hidle : IdleSt τ1 := hR.2.1.ofCells (schedTail_cells limit timer σ1 _ h1) + obtain ⟨⟨⟨⟩, σ2⟩, h21, h⟩ := mem_bindA.1 h1 + have f2 := awaken_fia σ1 hR.1 _ h21 + obtain ⟨⟨⟨⟩, σ3⟩, h31, h⟩ := mem_bindA.1 h + have f3 := checkDomainTime_fia σ2 f2 _ h31 + have sw := hSw_of hR.1 hR.2.2.1 hR.2.2.2 h21 h31 + obtain ⟨⟨⟨⟩, σ4⟩, h41, h⟩ := mem_bindA.1 h + have f4 := selectThread_fia limit σ3 ⟨f3, sw⟩ _ h41 + have F1 := scAndTimer_fia timer σ4 f4 _ h + have A1 := fl4Schedule_actA limit timer (fl4Schedule_of_tail limit timer hf h1) + refine ⟨?_, ?_, hidle⟩ + all_goals + have := activate_act τ1 A1 (fun x hx hrs => ?_) τ1 ⟨rfl, F1⟩ _ h2 + first | exact this.1 | exact this.2 + all_goals + have hne : τ1.curThread ≠ τ1.idleThread := fun e => by + rw [e] at hx + rw [hidle x hx] at hrs + cases hrs + exact F1.1.act A1 hne + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **`JSyscallA` at the state a kernel exit leaves, at the actual post-fire state `σ1`**: `ofSchedExit` with the one +hypothesis `FireRecA σ1` (module docstring) instead of `hAwk`/`hSw`/`hidle`, and `running`/`curSc`/`cur` derived from +the exit relation (`exit_fields_rel`). Remaining hypotheses: `FireRecA σ1` (owner: the entry composition), `hC0` +(`Z.C` has no null SC: the entry's), `hnid`, `legal`, `hn` (the exit's non-idle current thread and the activation's +state: the entry's). -/ +theorem JSyscallA.ofSchedExitAt {A : Type} {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} + {κ : U64 → U64} {n : Nat} {J5 J7 : JSlotA S} {s : S} {a : A} {m : McState} + (limit : Nat) (timer : State Unit → Fl4TimerView) {σ0 σ1 τ1 τ2 : State Unit} + (hf : fl4TimerFires σ0 σ1) (hR : FireRecA σ1) (h1 : ((), τ1) ∈ (SchedTail limit timer σ1).1) + (h2 : ((), τ2) ∈ (fl4ActivateThread (Z := Unit) τ1).1) + (J : JStage3EntryA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a τ2 m) + (hC0 : ∀ c ∈ Z.C, c ≠ 0#u64) (hnid : τ2.curThread ≠ τ2.idleThread) + (legal : ∀ y, τ1.kheap τ1.curThread = some (.tcb y) → ActivateStateOKA y.tcbState) + (hn : ∀ y, τ1.kheap τ1.curThread = some (.tcb y) → y.tcbState ≠ .idleThreadState) : + JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a τ2 m := by + have E := exit_spec_at limit timer hf hR h1 h2 + have F := exit_fields_rel limit timer (fl4Schedule_of_tail limit timer hf h1) h2 J.toJStage3A.yieldQueueA hC0 J.schedValid E.2.1 + hnid legal hn + exact JSyscallA.ofExit J E.2.1 F.1 F.2.1 F.2.2 + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3ExitHcA.lean b/proofs/Fl4ProofsA64/P3/Stage3ExitHcA.lean new file mode 100644 index 000000000..830ca8f37 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ExitHcA.lean @@ -0,0 +1,343 @@ +/- +P3 (aarch64, A64-P3 Stage 3, T3 G4b / F8, queue item 4): **the exit state's spec facts for `JSyscallA`**. + +At the end of a kernel exit (`fl4Schedule`, then `fl4ActivateThread`) from a state `σ0`: + +* **`exit_hc`**: the current thread's TCB has `σ.curSc` as its SC (`HcA`). `fl4ScAndTimer`'s `fl4SwitchSchedContext` + sets `curSc` to the current thread's SC (`fl4ScAndTimer_hc`) and nothing after it moves the current thread, an SC + binding or `curSc`; the activation keeps all three (`CcEq`). +* **`activate_run`**: after `fl4ActivateThread` the current thread's TCB is `running` if it was restarting, and + otherwise in the state it had (`running`, `runningVM`, `idleThreadState`), so a non-idle current thread that was not + in `idleThreadState` is `running` or `runningVM`. +* **`exit_fields_rel`**: with `YieldQueueRelA` at the exit state (`finish_refinesA`'s post), `JSyscallA`'s `running`, + `curSc` and `cur` fields. + +Named premises: the current thread at the exit is not the idle thread and, before the activation, not in +`idleThreadState` (`SInvXA.idleTcb` makes only the idle TCB so), the entry's. + +No premise otherwise; new module. +-/ +import Fl4ProofsA64.P3.Stage3SigSchedValidA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Classical + +noncomputable section + +/-- The current thread's TCB (if any) has `curSc` as its SC. -/ +def HcA (σ : State Unit) : Prop := + ∀ x, σ.kheap σ.curThread = some (.tcb x) → x.tcbSchedContext = some σ.curSc + +theorem HcA.ofTKeep {σ σ' : State Unit} (h : HcA σ) (k : TKeepA σ σ') : HcA σ' := by + obtain ⟨kc, kr, kq, ki, ka, kb, kf, ks, kcs⟩ := k + intro x hx + obtain ⟨x0, e0, sc0, -⟩ := kb _ x hx + rw [kc] at e0 + rw [sc0, kcs] + exact h x0 e0 + +theorem HT.ofTP {α : Type} {m : SMonad Unit α} (h : TPres m) (R : State Unit → Prop) + (hR : ∀ σ σ', TKeepA σ σ' → R σ → R σ') : HT R m (fun _ => R) := + fun σ hσ r hr => hR _ _ (h σ r hr) hσ + +theorem HT.ofAP {α : Type} {m : SMonad Unit α} (h : APres m) (R : State Unit → Prop) + (hR : ∀ σ σ', ActKeepA σ σ' → R σ → R σ') : HT R m (fun _ => R) := + fun σ hσ r hr => hR _ _ (h σ r hr) hσ + +instance (q : List ObjRef) : TPresInst (fl4ReleaseDeadline (Z := Unit) q) := ⟨by unfold fl4ReleaseDeadline; tpres⟩ +instance (timer : State Unit → Fl4TimerView) : TPresInst (fl4SetNextInterrupt (Z := Unit) timer) := + ⟨by unfold fl4SetNextInterrupt; tpres⟩ + +/-- **`fl4SwitchSchedContext` leaves the current thread's SC as `curSc`.** -/ +theorem fl4SwitchSchedContext_hc : + HT (fun _ => True) (fl4SwitchSchedContext (Z := Unit)) (fun _ τ => HcA τ) := by + unfold fl4SwitchSchedContext + refine HT.bind (Q := fun s σ => σ = s) (HT.get fun σ _ => rfl) fun s => ?_ + refine HT.bind (Q := fun target σ => ∀ x, σ.kheap σ.curThread = some (.tcb x) → x.tcbSchedContext = some target) + (HT.bind (Q := fun tOpt σ => σ = s ∧ ∀ x, σ.kheap σ.curThread = some (.tcb x) → x.tcbSchedContext = tOpt) ?_ + fun tOpt => ?_) fun target => ?_ + · show HT _ (threadGet (Z := Unit) Tcb.tcbSchedContext s.curThread) _ + refine HT.threadGet _ _ fun σ hσ x hx => ⟨hσ, fun y hy => ?_⟩ + subst hσ + rw [hx] at hy + have := KernelObject.tcb.inj (Option.some.inj hy) + subst this; rfl + · exact HT.assertOpt _ fun a ha σ hσ x hx => by rw [hσ.2 x hx]; exact ha + have hR : ∀ σ σ', ActKeepA σ σ' → (∀ x, σ.kheap σ.curThread = some (.tcb x) → x.tcbSchedContext = some target) → + (∀ x, σ'.kheap σ'.curThread = some (.tcb x) → x.tcbSchedContext = some target) := fun σ σ' k h x hx => by + rw [k.2.1] at hx + exact h x ((k.2.2.2.1 _ x).1 hx) + refine HT.bind (Q := fun _ σ => ∀ x, σ.kheap σ.curThread = some (.tcb x) → x.tcbSchedContext = some target) + (HT.ofAP (by apres) _ hR) fun _ => ?_ + refine HT.bind (Q := fun _ σ => ∀ x, σ.kheap σ.curThread = some (.tcb x) → x.tcbSchedContext = some target) + (HT.gets _ fun σ hσ => hσ) fun reprogram => ?_ + refine HT.bind (Q := fun _ σ => ∀ x, σ.kheap σ.curThread = some (.tcb x) → x.tcbSchedContext = some target) + (HT.when (fun _ => HT.ofAP (by apres) _ hR) (fun _ σ hσ => hσ)) fun _ => ?_ + exact HT.modify _ fun σ hσ x hx => hσ x hx + +theorem hcOfKeep {σ σ' : State Unit} (h : HcA σ) (k : TKeepA σ σ') : HcA σ' := h.ofTKeep k + +/-- **`fl4ScAndTimer` leaves the current thread's SC as `curSc`.** -/ +theorem fl4ScAndTimer_hc (timer : State Unit → Fl4TimerView) : + HT (fun _ => True) (fl4ScAndTimer (Z := Unit) timer) (fun _ τ => HcA τ) := by + unfold fl4ScAndTimer + refine HT.bind (Q := fun _ σ => HcA σ) fl4SwitchSchedContext_hc fun _ => ?_ + refine HT.bind (Q := fun _ σ => HcA σ) (HT.gets _ fun σ hσ => hσ) fun reprogram => ?_ + exact HT.when (fun _ => HT.ofTP (by tpres) _ fun σ σ' k h => h.ofTKeep k) (fun _ σ hσ => hσ) + +theorem fl4Schedule_hc (limit : Nat) (timer : State Unit → Fl4TimerView) : + HT (fun _ => True) (fl4Schedule (Z := Unit) limit timer) (fun _ τ => HcA τ) := by + unfold fl4Schedule + refine HT.bind (Q := fun _ _ => True) (fun σ _ r hr => trivial) fun _ => ?_ + refine HT.bind (Q := fun _ _ => True) (fun σ _ r hr => trivial) fun _ => ?_ + refine HT.bind (Q := fun _ _ => True) (fun σ _ r hr => trivial) fun _ => ?_ + refine HT.bind (Q := fun _ _ => True) (fun σ _ r hr => trivial) fun _ => ?_ + exact fl4ScAndTimer_hc timer + + +/-! ## The activation keeps the current thread, `curSc` and the current TCB's SC -/ + +def CcEq (σ σ' : State Unit) : Prop := + σ'.curThread = σ.curThread ∧ σ'.curSc = σ.curSc ∧ + ∀ x', σ'.kheap σ.curThread = some (.tcb x') → ∃ x, σ.kheap σ.curThread = some (.tcb x) ∧ + x'.tcbSchedContext = x.tcbSchedContext + +instance : PreO CcEq where + refl _ := ⟨rfl, rfl, fun x' h => ⟨x', h, rfl⟩⟩ + trans {a b c} h1 h2 := by + obtain ⟨c1, s1, t1⟩ := h1 + obtain ⟨c2, s2, t2⟩ := h2 + refine ⟨c2.trans c1, s2.trans s1, fun x'' hx => ?_⟩ + have hx' : c.kheap b.curThread = some (.tcb x'') := by rw [c1]; exact hx + obtain ⟨x', e', sc'⟩ := t2 x'' hx' + rw [c1] at e' + obtain ⟨x, e, sc⟩ := t1 x' e' + exact ⟨x, e, sc'.trans sc⟩ + +class QPresInst {α : Type} (m : SMonad Unit α) : Prop where + pres : PresR CcEq m + +instance (priority := 10) {α : Type} {m : SMonad Unit α} [h : TPresInst m] : QPresInst m := + ⟨fun s x hx => by + have k := h.pres s x hx + obtain ⟨kc, kr, kq, ki, ka, kb, kf, ks, kcs⟩ := k + refine ⟨kc, kcs, fun x' hx' => ?_⟩ + rw [← kc] at hx' + obtain ⟨x0, e0, sc0, -⟩ := kb _ x' hx' + rw [kc] at e0 + exact ⟨x0, e0, sc0⟩⟩ + +instance (priority := 900) {α β : Type} {m : SMonad Unit α} {f : α → SMonad Unit β} [hm : QPresInst m] + [hf : ∀ a, QPresInst (f a)] : QPresInst (m >>= f) := + ⟨PresR.bind hm.pres fun a => (hf a).pres⟩ +instance (priority := 900) {P : Prop} {d : Decidable P} {m : SMonad Unit Unit} [h : QPresInst m] : + QPresInst (@NonDet.when (State Unit) P d m) := ⟨PresR.when h.pres⟩ +instance (priority := 900) {α : Type} {P : Prop} {d : Decidable P} {a b : SMonad Unit α} [ha : QPresInst a] + [hb : QPresInst b] : QPresInst (@ite _ P d a b) := ⟨PresR.ite' ha.pres hb.pres⟩ + +macro "qpresHc" : tactic => `(tactic| + repeat' (first + | exact QPresInst.pres + | refine PresR.bind_pure _ _ ?_ + | refine PresR.bind ?_ (fun _ => ?_) + | refine PresR.when ?_ + | refine PresR.ite' ?_ ?_ + | (refine PresR.modify _ (fun _ => ⟨rfl, rfl, fun x' hx' => ⟨x', hx', rfl⟩⟩)) + | dsimp only + | split)) + +instance (r : ObjRef) (ts : ThreadState) : QPresInst (setThreadState (Z := Unit) r ts) := + ⟨by + unfold setThreadState setThreadStateAct + refine PresR.bind (fun σ x hx => ?_) (fun _ => ?_) + · obtain ⟨x0, hx0, hr2⟩ := threadSet_spec hx + rw [hr2] + refine ⟨rfl, rfl, fun x' hx' => ?_⟩ + have hx'' : Function.update σ.kheap r (some (.tcb { x0 with tcbState := ts })) σ.curThread = + some (.tcb x') := hx' + by_cases e : σ.curThread = r + · rw [e] at hx'' + simp only [Function.update_self] at hx'' + have := KernelObject.tcb.inj (Option.some.inj hx'') + subst this + exact ⟨x0, e ▸ hx0, rfl⟩ + · rw [Function.update_of_ne e] at hx'' + exact ⟨x', hx'', rfl⟩ + · qpresHc⟩ + +theorem activate_cc : PresR CcEq (fl4ActivateThread (Z := Unit)) := by + unfold fl4ActivateThread fl4ActivateState getTcbObjRef + qpresHc + +theorem HcA.ofCc {σ σ' : State Unit} (h : HcA σ) (c : CcEq σ σ') : HcA σ' := by + intro x hx + rw [c.1] at hx + obtain ⟨x0, e0, sc0⟩ := c.2.2 x hx + rw [sc0, c.2.1] + exact h x0 e0 + +/-- **`exit_hc`**: the current thread's SC is `curSc` after `fl4Schedule` and `fl4ActivateThread`. -/ +theorem exit_hc (limit : Nat) (timer : State Unit → Fl4TimerView) {σ0 τ1 τ2 : State Unit} + (h1 : ((), τ1) ∈ (fl4Schedule (Z := Unit) limit timer σ0).1) + (h2 : ((), τ2) ∈ (fl4ActivateThread (Z := Unit) τ1).1) : HcA τ2 := + (fl4Schedule_hc limit timer σ0 trivial ((), τ1) h1).ofCc (activate_cc τ1 ((), τ2) h2) + + +/-! ## The current thread's state after the activation -/ + +theorem setThreadStateSt (r : ObjRef) (ts : ThreadState) (R : State Unit → Prop) + (hT : ∀ σ x, σ.kheap r = some (.tcb x) → R σ → + R { σ with kheap := Function.update σ.kheap r (some (.tcb { x with tcbState := ts })) }) + (hA : ∀ σ a, R σ → R { σ with schedulerAction := a }) : + HT R (setThreadState (Z := Unit) r ts) (fun _ σ => R σ ∧ StIs r ts σ) := by + unfold Fl4SpecA64.setThreadState Fl4SpecA64.setThreadStateAct + refine HT.bind (Q := fun _ σ => R σ ∧ StIs r ts σ) ?_ fun _ => ?_ + · intro σ hσ r' hr + obtain ⟨x, hx, hr2⟩ := threadSet_spec hr + rw [hr2] + refine ⟨hT σ x hx hσ, fun y hy => ?_⟩ + have : ({ x with tcbState := ts } : Tcb) = y := by simpa using hy + rw [← this] + · refine HT.bind (Q := fun _ σ => R σ ∧ StIs r ts σ) (HT.gets _ fun σ hσ => hσ) fun cur => ?_ + refine HT.bind (Q := fun _ σ => R σ ∧ StIs r ts σ) (HT.gets _ fun σ hσ => hσ) fun act => ?_ + refine HT.bind (Q := fun _ σ => R σ ∧ StIs r ts σ) (HT.gets _ fun σ hσ => hσ) fun sched => ?_ + exact HT.when (fun _ => by + unfold Fl4SpecA64.setSchedulerAction + exact HT.modify _ fun σ hσ => ⟨hA σ _ hσ.1, hσ.2⟩) (fun _ σ hσ => hσ) + +/-- What the activation leaves of the current thread: it is the same thread, `running` if it was restarting, and +otherwise in the state it had, one of `running`, `runningVM`, `idleThreadState`. -/ +def ActPost (τ1 τ : State Unit) : Prop := + τ.curThread = τ1.curThread ∧ ∀ x, τ.kheap τ.curThread = some (.tcb x) → ∀ y, τ1.kheap τ1.curThread = some (.tcb y) → + (y.tcbState = .restart ∧ x.tcbState = .running) ∨ + ((y.tcbState = .running ∨ y.tcbState = .ext .runningVM ∨ y.tcbState = .idleThreadState) ∧ + x.tcbState = y.tcbState) + +theorem activate_run (τ1 : State Unit) : + HT (fun σ => σ = τ1) (fl4ActivateThread (Z := Unit)) (fun _ τ => ActPost τ1 τ) := by + unfold Fl4SpecA64.fl4ActivateThread Fl4SpecA64.getTcbObjRef + refine HT.bind (Q := fun ct σ => TKeepA τ1 σ ∧ ct = σ.curThread) + (HT.gets _ fun σ hσ => ⟨by subst hσ; exact PreO.refl _, rfl⟩) fun ct => ?_ + refine HT.bind (Q := fun _ σ => TKeepA τ1 σ ∧ ct = σ.curThread) + (HT.threadGet _ _ fun σ hσ x hx => hσ) fun yt => ?_ + refine HT.bind (Q := fun _ σ => TKeepA τ1 σ ∧ ct = σ.curThread) + (HT.when (fun _ σ hσ r hr => ?_) (fun _ σ hσ => hσ)) fun _ => ?_ + · have k := (inferInstance : TPresInst (fl4CompleteYieldTo (Z := Unit) ct)).pres σ r hr + exact ⟨PreO.trans hσ.1 k, hσ.2.trans k.1.symm⟩ + unfold Fl4SpecA64.fl4ActivateState + refine HT.bind (Q := fun st σ => (TKeepA τ1 σ ∧ ct = σ.curThread) ∧ + ∃ x, σ.kheap ct = some (.tcb x) ∧ x.tcbState = st) ?_ fun st => ?_ + · unfold Fl4SpecA64.getThreadState + exact HT.threadGet _ ct fun σ hσ x hx => ⟨hσ, x, hx, rfl⟩ + have fin : ∀ σ, ((TKeepA τ1 σ ∧ ct = σ.curThread) ∧ ∃ x, σ.kheap ct = some (.tcb x) ∧ x.tcbState = st) → + (st = .running ∨ st = .ext .runningVM ∨ st = .idleThreadState) → ActPost τ1 σ := by + intro σ ⟨⟨k, hct⟩, x0, e0, hs0⟩ hst + refine ⟨k.1, fun x hx y hy => ?_⟩ + obtain ⟨y', ey', -, st'⟩ := k.2.2.2.2.2.1 _ x hx + rw [k.1] at ey' + rw [ey'] at hy + have : y' = y := KernelObject.tcb.inj (Option.some.inj hy) + subst this + refine Or.inr ⟨?_, st'⟩ + rw [← st'] + rw [← hct] at hx + rw [e0] at hx + have := KernelObject.tcb.inj (Option.some.inj hx) + subst this + rw [hs0]; exact hst + have restartCase : HT (fun σ => ((TKeepA τ1 σ ∧ ct = σ.curThread) ∧ + ∃ x, σ.kheap ct = some (.tcb x) ∧ x.tcbState = .restart)) + (show SMonad Unit Unit from do + let pc ← asUser (Z := Unit) ct getRestartPC + asUser (Z := Unit) ct (setNextPC pc) + setThreadState (Z := Unit) ct .running) + (fun _ τ => ActPost τ1 τ) := by + have RS : ∀ σ, ((TKeepA τ1 σ ∧ ct = σ.curThread) ∧ ∃ x, σ.kheap ct = some (.tcb x) ∧ x.tcbState = .restart) → + (TKeepA τ1 σ ∧ ct = σ.curThread) ∧ ∀ y, τ1.kheap τ1.curThread = some (.tcb y) → y.tcbState = .restart := by + intro σ ⟨⟨k, hct⟩, x, ex, hs⟩ + refine ⟨⟨k, hct⟩, fun y hy => ?_⟩ + obtain ⟨y0, ey0, -, st0⟩ := k.2.2.2.2.2.1 ct x ex + have hc1 : τ1.curThread = ct := by rw [hct, k.1] + rw [hc1] at hy + rw [ey0] at hy + have := KernelObject.tcb.inj (Option.some.inj hy) + subst this + rw [← st0]; exact hs + refine HT.bind (Q := fun _ σ => (TKeepA τ1 σ ∧ ct = σ.curThread) ∧ + ∀ y, τ1.kheap τ1.curThread = some (.tcb y) → y.tcbState = .restart) + (HT.mono (HT.ofTP (m := asUser (Z := Unit) ct getRestartPC) (inferInstance : TPresInst _).pres + (fun σ => (TKeepA τ1 σ ∧ ct = σ.curThread) ∧ ∀ y, τ1.kheap τ1.curThread = some (.tcb y) → y.tcbState = .restart) + (fun σ σ' k h => ⟨⟨PreO.trans h.1.1 k, h.1.2.trans k.1.symm⟩, h.2⟩)) RS (fun _ _ h => h)) fun pc => ?_ + refine HT.bind (Q := fun _ σ => (TKeepA τ1 σ ∧ ct = σ.curThread) ∧ + ∀ y, τ1.kheap τ1.curThread = some (.tcb y) → y.tcbState = .restart) + (HT.ofTP (m := asUser (Z := Unit) ct (setNextPC pc)) (inferInstance : TPresInst _).pres + (fun σ => (TKeepA τ1 σ ∧ ct = σ.curThread) ∧ ∀ y, τ1.kheap τ1.curThread = some (.tcb y) → y.tcbState = .restart) + (fun σ σ' k h => ⟨⟨PreO.trans h.1.1 k, h.1.2.trans k.1.symm⟩, h.2⟩)) fun _ => ?_ + refine HT.mono (setThreadStateSt ct .running + (fun σ => σ.curThread = τ1.curThread ∧ ct = τ1.curThread ∧ + ∀ y, τ1.kheap τ1.curThread = some (.tcb y) → y.tcbState = .restart) + (fun σ x hx h => h) (fun σ a h => h)) + (fun σ h => ⟨h.1.1.1, h.1.2.trans h.1.1.1, h.2⟩) (fun _ σ h => ?_) + obtain ⟨⟨hc, hc2, hrs⟩, hst⟩ := h + refine ⟨hc, fun x hx y hy => Or.inl ⟨hrs y hy, ?_⟩⟩ + have : σ.curThread = ct := hc.trans hc2.symm + rw [this] at hx + exact hst x hx + cases st + all_goals dsimp only + all_goals first + | exact HT.fail + | exact restartCase + | exact HT.pure () (fun σ hσ => fin σ hσ (Or.inl rfl)) + | (unfold Fl4SpecA64.archActivateIdleThread + exact HT.pure () fun σ hσ => fin σ hσ (Or.inr (Or.inr rfl))) + | (rename_i e; cases e; exact HT.pure () fun σ hσ => fin σ hσ (Or.inr (Or.inl rfl))) + + +/-! ## `JSyscallA`'s `running`, `curSc` and `cur` at the exit state -/ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **`JSyscallA`'s exit fields from the exit relation**: with `YieldQueueRelA` at the word state `s'` and the spec state +`τ2` left by `fl4Schedule` and `fl4ActivateThread`, `running`, `curSc` and `cur`. +Named premises (all spec-level, the entry's): the current thread at the exit is not the idle thread (`hnid`); before the +activation the current TCB is in an activatable state (`legal`, `activate_thread_stepA`'s) and not in +`idleThreadState` (`hn`, `SInvXA.idleTcb`: only the idle TCB is); F8 at `τ2` and `resumeCurThread` come from +`exit_spec`. -/ +theorem exit_fields_rel {Z : XSetsA} {m : McState} {κ : U64 → U64} {s' : S} (limit : Nat) + (timer : State Unit → Fl4TimerView) {σ0 τ1 τ2 : State Unit} + (h1 : ((), τ1) ∈ (fl4Schedule (Z := Unit) limit timer σ0).1) + (h2 : ((), τ2) ∈ (fl4ActivateThread (Z := Unit) τ1).1) + (H : YieldQueueRelA (st := st) Z m κ s' τ2) (hC0 : ∀ c ∈ Z.C, c ≠ 0#u64) + (hF : SchedValidA τ2) (hA : τ2.schedulerAction = .resumeCurThread) (hnid : τ2.curThread ≠ τ2.idleThread) + (legal : ∀ y, τ1.kheap τ1.curThread = some (.tcb y) → ActivateStateOKA y.tcbState) + (hn : ∀ y, τ1.kheap τ1.curThread = some (.tcb y) → y.tcbState ≠ .idleThreadState) : + Runnable (L.mem s' (stA (L.curW s'))) ∧ + (L.mem s' (scA (L.curW s')) ≠ 0#u64 → L.cscW s' = L.mem s' (scA (L.curW s'))) ∧ + (L.mem s' (inqA (L.curW s')) &&& sched.Q_RELEASE) = 0#u64 ∧ L.mem s' (scA (L.curW s')) ≠ 0#u64 := by + have hc := exit_hc limit timer h1 h2 + obtain ⟨hcs, hcur⟩ := exit_cur_fields H hC0 hc hA hnid hF + refine ⟨?_, hcs, hcur⟩ + have ht := H.sched.curT + have hcw := H.sched.sch.curThread + obtain ⟨x2, k2, -⟩ := H.sched.sch.tcb (L.curW s') ht + have k2' : τ2.kheap τ2.curThread = some (.tcb x2) := by rw [hcw]; exact k2 + obtain ⟨y, ey, -⟩ := (activate_cc τ1 ((), τ2) h2).2.2 x2 (by + rw [(activate_cc τ1 ((), τ2) h2).1] at k2' + exact k2') + have post := (activate_run τ1 τ1 rfl ((), τ2) h2).2 x2 k2' y ey + refine H.runnable_cur k2' ?_ + rcases post with ⟨-, hx⟩ | ⟨hy, hx⟩ + · exact Or.inl hx + · rcases hy with hy | hy | hy + · exact Or.inl (hx.trans hy) + · exact Or.inr (hx.trans hy) + · exact absurd hy (hn y ey) + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3FBootA.lean b/proofs/Fl4ProofsA64/P3/Stage3FBootA.lean new file mode 100644 index 000000000..add0e4913 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3FBootA.lean @@ -0,0 +1,225 @@ +/- +P3 (AArch64, merge line; Architect 15:06Z F1–F6): **the boot witnesses of the idle-object facts** `SIdleA` (F3–F6) and +`SActA` (F1–F2) at A64-Boot's `w0` (`bootSA`) and `w1` (`w1SA`). At `w0` every cap is null, each idle SC at its idle +thread; at `w1` the root task's heap objects join: the root CNode and TCB hold no idle-thread cap and no idle-SC cap +(`capOf_noIdleA`, `rootTcbA_cap`), the root SC is bound to the root TCB (`idle_ne_rtA`), and the pending +`switchThread` target is the root TCB, which is neither the current nor the idle thread. +No new premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3A +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P1.W7.R.ST.BootWit + +noncomputable section + +/-- A cap a spec slot holds is a cap of the object. -/ +theorem specCapAtA_of_capOf {σ : Fl4SpecA64.State Unit} {sl : Fl4SpecA64.CslotPtr} {a : Fl4SpecA64.Cap} + (h : specCapAtA σ sl = some a) : ∃ o, σ.kheap sl.1 = some o ∧ Fl4SpecA64.capOf o sl.2 = some a := by + unfold specCapAtA at h + split at h + · rename_i sz cn hk + split at h + · exact ⟨_, hk, h⟩ + · cases h + · rename_i t hk + exact ⟨_, hk, h⟩ + · cases h + +theorem NoIdleCapV.null {m : Fl4SpecA64.McState} {n : Nat} : NoIdleCapV m n .nullCap := + ⟨fun _ _ e => (by cases e), fun _ e => (by cases e), fun _ _ _ e => (by cases e)⟩ + +section w0 +variable {kb ib now mt : U64} {nc : Nat} (hL : BootLayA ib nc) (hK : BootKsA kb ib nc) +include hL hK + +/-- **F3–F6 at `w0`**. -/ +theorem boot_sidleA : SIdleA (bootSA kb ib nc now mt) (bootMA ib nc) nc where + f3 := fun c hc => ⟨_, bootKA_tcb hL hc, rfl⟩ + f4 := fun y k hy => by + have e := bootKA_sc0 (kb := kb) hL (now := now) (mt := mt) + have hy' : bootKA kb ib nc now mt Fl4SpecA64.idleScPtr = some (.schedContext y k) := hy + rw [e] at hy' + injection hy' with h1 + injection h1 with h2 + subst h2 + rfl + f5 := fun p y k hy he => by + have hy' : bootKA kb ib nc now mt p = some (.schedContext y k) := hy + have he' : y.scTcb = some (specPtr (idleTA ib 0)) := he + by_contra hne + have h1 := hy' + unfold bootKA at h1 + by_cases b : ∃ c, c < nc ∧ p = specPtr (idleTA ib c) + · rw [if_neg hne, if_pos b] at h1; cases h1 + by_cases d : ∃ c, 0 < c ∧ c < nc ∧ p = specPtr (idleSA ib c) + · obtain ⟨c, h0, hc, rfl⟩ := d + have e := bootKA_sc (kb := kb) (now := now) (mt := mt) hL h0 hc + rw [e] at hy' + injection hy' with h1 + injection h1 with h2 h3 + subst h2 + have : specPtr (idleTA ib c) = specPtr (idleTA ib 0) := Option.some.inj he' + exact absurd (idleTA_inj hL hc hL.nc0 this) (by omega) + · rw [if_neg hne, if_neg b, if_neg d] at h1 + split_ifs at h1 <;> cases h1 + f6 := fun sl => by + have key : ∀ a, specCapAtA (bootSA kb ib nc now mt) sl = some a → a = .nullCap := fun a h => by + obtain ⟨o, ho, hc⟩ := specCapAtA_of_capOf h + exact bootKA_null hL hK ho hc + exact ⟨fun c _ e => (by have := key _ e; cases this), fun b e => (by have := key _ e; cases this), + fun a e => (by have := key _ e; subst this; exact fun _ _ _ e => by cases e)⟩ + f7 := fun y k hy => by + have e := bootKA_sc0 (kb := kb) hL (now := now) (mt := mt) + have hy' : bootKA kb ib nc now mt Fl4SpecA64.idleScPtr = some (.schedContext y k) := hy + rw [e] at hy' + injection hy' with h1 + injection h1 with h2 + subst h2 + exact ⟨rfl, BitVec.le_refl now.bv⟩ + idleSlot := fun c hc => by + rw [vtCapA_tcbI (bootKA_tcb (kb := kb) (now := now) (mt := mt) hL hc)]; rfl + idleBound := fun c hc => by + rw [vcpuOfA_tcbI (bootKA_tcb (kb := kb) (now := now) (mt := mt) hL hc)]; rfl + +/-- (record edit #8) No SC of `w0` has a reply. -/ +theorem boot_actRepA : ActRepA (bootSA kb ib nc now mt) := fun p y k hy hr => by + refine absurd ?_ hr + change bootKA kb ib nc now mt p = _ at hy + unfold bootKA at hy + split_ifs at hy <;> cases hy <;> rfl + +/-- **F1–F2 at `w0`**: the action is the initial one, the current thread is core 0's idle thread. -/ +theorem boot_sactA : SActA (bootSA kb ib nc now mt) where + f1 := fun u h => by simp [bootSA, Fl4SpecA64.initASt] at h + f2 := ⟨_, bootKA_tcb hL hL.nc0, Or.inr (Or.inr (Or.inr rfl))⟩ + +end w0 + +section w1 +variable {kb ib now mt sl : U64} {nc : Nat} (hL : BootLayA ib nc) (hK : BootKsA kb ib nc) (P : BootLay1 kb ib nc) +include hL hK P + +theorem w1_sc_ne_idleA : specPtr (bwv SCb) ≠ Fl4SpecA64.idleScPtr := + sp_ne_scA (by norm_num) (by norm_num) + +/-- Every cap of the root CNode is no idle-thread cap and no idle-SC cap. -/ +theorem capOf_noIdleA (i : Nat) : NoIdleCapV (w1MA ib nc) nc (absCapA (capOf nc i)) := by + have hT : ∀ c < nc, specPtr (idleTA ib c) ≠ specPtr (bwv RTb) := fun c hc => idle_ne_rtA hL P hc + have hS := w1_sc_ne_idleA hL hK P + unfold capOf + by_cases h1 : i < 16 + · rw [slotC_lo h1] + interval_cases i <;> refine ⟨fun c hc e => ?_, fun b e => ?_, fun _ _ _ e => ?_⟩ <;> + first + | (cases e; done) + | (injection e with e1; first | exact hT c hc e1.symm | exact hS e1) + by_cases h2 : i < 16 + nc + · rw [slotC_sch (by omega) h2] + exact ⟨fun c hc e => (by cases e), fun b e => (by cases e), fun _ _ _ e => (by cases e)⟩ + rw [slotC_hi (by omega)] + generalize i - (16 + nc) = k + by_cases h3 : k < 37 + · interval_cases k <;> refine ⟨fun c hc e => ?_, fun b e => ?_, fun _ _ _ e => ?_⟩ <;> cases e <;> + first + | done + | exact fun h => absurd h.1 (by decide) + | exact fun h => absurd h.1 (by simp [bwv]) + · rw [tailC_big (by omega)] + exact ⟨fun c hc e => (by cases e), fun b e => (by cases e), fun _ _ _ e => (by cases e)⟩ + +/-- **F3–F6 at `w1`**. -/ +theorem w1_sidleA : SIdleA (w1SA kb ib nc now mt sl) (w1MA ib nc) nc where + f3 := fun c hc => ⟨_, w1KA_idle hL P hc, rfl⟩ + f4 := fun y k hy => by + have e := w1KA_sc0 (kb := kb) (now := now) (mt := mt) (sl := sl) hL + have hy' : w1KA kb ib nc now mt sl Fl4SpecA64.idleScPtr = some (.schedContext y k) := hy + rw [e] at hy' + injection hy' with h1 + injection h1 with h2 + subst h2 + rfl + f5 := fun p y k hy he => by + change w1KA kb ib nc now mt sl p = _ at hy + change y.scTcb = some (specPtr (idleTA ib 0)) at he + rcases w1KA_cases hL hy with e | e | e | e | ⟨-, e⟩ | e | ⟨hb, e⟩ + · cases e + · cases e + · simp only [Fl4SpecA64.KernelObject.schedContext.injEq] at e + obtain ⟨rfl, rfl⟩ := e + exact absurd (Option.some.inj he).symm (idle_ne_rtA hL P hL.nc0) + · cases e + · cases e + · cases e + · exact (boot_sidleA hL hK (kb := kb) (now := now) (mt := mt)).f5 p y k hb he + f6 := fun sl' => by + have key : ∀ a, specCapAtA (w1SA kb ib nc now mt sl) sl' = some a → NoIdleCapV (w1MA ib nc) nc a := by + intro a h + obtain ⟨o, ho, hc⟩ := specCapAtA_of_capOf h + change w1KA kb ib nc now mt sl sl'.1 = some o at ho + rcases w1KA_cases hL ho with rfl | rfl | rfl | rfl | ⟨-, rfl⟩ | rfl | ⟨hb, -⟩ + · simp only [Fl4SpecA64.capOf, rootCsA] at hc + split_ifs at hc + have e := Option.some.inj hc + rw [← e] + exact capOf_noIdleA hL hK P _ + · rcases rootTcbA_cap hc with rfl | rfl | rfl | rfl + · exact NoIdleCapV.null + all_goals + refine ⟨fun c hc e => ?_, fun b e => ?_, fun _ _ _ e => ?_⟩ <;> + first + | (cases e; done) + | (simp [rtCapA, absCapA] at e; done) + · simp [Fl4SpecA64.capOf] at hc + · simp [Fl4SpecA64.capOf] at hc + · simp [Fl4SpecA64.capOf] at hc + · simp [Fl4SpecA64.capOf] at hc + · have := bootKA_null hL hK hb hc + rw [this] + exact NoIdleCapV.null + exact ⟨fun c hc e => (key _ e).1 c hc rfl, fun b e => (key _ e).2.1 b rfl, fun a e => (key _ e).2.2⟩ + f7 := fun y k hy => by + have e := w1KA_sc0 (kb := kb) (now := now) (mt := mt) (sl := sl) hL + have hy' : w1KA kb ib nc now mt sl Fl4SpecA64.idleScPtr = some (.schedContext y k) := hy + rw [e] at hy' + injection hy' with h1 + injection h1 with h2 + subst h2 + exact ⟨rfl, BitVec.le_refl now.bv⟩ + idleSlot := fun c hc => by + have e : (w1SA kb ib nc now mt sl).kheap (specPtr (idleTA ib c)) = some (.tcb _) := w1KA_idle hL P hc + rw [show (w1MA ib nc).idleThreadOf c = specPtr (idleTA ib c) from rfl, vtCapA_tcbI e]; rfl + idleBound := fun c hc => by + have e : (w1SA kb ib nc now mt sl).kheap (specPtr (idleTA ib c)) = some (.tcb _) := w1KA_idle hL P hc + rw [show (w1MA ib nc).idleThreadOf c = specPtr (idleTA ib c) from rfl, vcpuOfA_tcbI e]; rfl + +/-- (record edit #8) No SC of `w1` has a reply. -/ +theorem w1_actRepA : ActRepA (w1SA kb ib nc now mt sl) := fun p y k hy hr => by + refine absurd ?_ hr + change w1KA kb ib nc now mt sl p = _ at hy + rcases w1KA_cases hL hy with e | e | e | e | ⟨-, e⟩ | e | ⟨hb, e⟩ + · cases e + · cases e + · cases e; rfl + · cases e + · cases e + · cases e + · exact Classical.byContradiction fun h => h ((by + unfold bootKA at hb + split_ifs at hb <;> cases hb <;> rfl)) + +/-- **F1–F2 at `w1`**: the pending `switchThread` target is the root TCB, the current thread is core 0's idle thread. -/ +theorem w1_sactA : SActA (w1SA kb ib nc now mt sl) where + f1 := fun u h => by + change Fl4SpecA64.SchedulerAction.switchThread (specPtr (bwv RTb)) = .switchThread u at h + injection h with h + subst h + exact ⟨fun e => idle_ne_rtA hL P hL.nc0 e.symm, fun e => idle_ne_rtA hL P hL.nc0 e.symm⟩ + f2 := ⟨_, w1KA_idle hL P hL.nc0, Or.inr (Or.inr (Or.inr rfl))⟩ + +end w1 + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3FaultArmA.lean b/proofs/Fl4ProofsA64/P3/Stage3FaultArmA.lean new file mode 100644 index 000000000..7668eac2e --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3FaultArmA.lean @@ -0,0 +1,940 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2b, item 2): **`handle_fault` of the current thread at the record**, for a user or VM +fault (`UsrFaultA`), the arm the kernel-entry fault paths consume. + +* `handle_fault_sendProgA` / `handle_fault_nullProgA`: the program with an endpoint handler is `set_fault` then + `send_ipc` (blocking, non-call, donation allowed); with no handler it is `set_thread_state … Inactive`. +* **`JSyscallA.faultRecvA`**: the handler endpoint holds waiting receivers and no reply object is pushed (no grant and + no grant-reply, or the receiver has none): the receiver gets the fault message and runs, the sender is `Inactive`. +* **`JSyscallA.faultDonateA`**: grant or grant-reply and a receiver blocked with a reply object and no SC: the reply + is pushed on the sender's SC, which is donated to the receiver. +* **`JSyscallA.faultNullA`**: no handler: the thread is `Inactive`. + +[S] (scope selectors) the handler cap and the endpoint's state as above; `h1` (`numNodes = 1`); in the donating arm +`hrcv` (receiver with a reply object and no SC), `hV` (the sender's VCPU word is 0). Not covered: no waiting receiver +(the blocking send, record edit #8c `IpcSortedA`); a reply push to a receiver that has its own SC. +[P] `ArchTotal`, `MaxSpi`, `ArchPlat`, `hCs`, `hmb`, `hmax`, `htu`. New module. +-/ +import Fl4ProofsA64.P3.Stage3RecvPreA +import Fl4ProofsA64.P3.Stage3SendQA +import Fl4ProofsA64.P3.Stage3FaultTailA +import Fl4ProofsA64.P3.Stage3FaultSpecA +import Fl4ProofsA64.P3.Stage3IpcWakeA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + +/-- **The current thread written to a state that is not runnable**, and `set_thread_state_act` on it: the body end +(only the current thread's TCB changes; the action is kept or `chooseNewThread`, never resuming). -/ +theorem Sched.BodyEndA.ofStopA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} + {κ : U64 → U64} {n : Nat} {s : S} {a : A} {σ : State Unit} {m : McState} {J5 J7 : JSlotA S} + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {x2 : Tcb} (hns : runnable x2.tcbState = false) : + BodyEndA σ (stsAct (updK σ σ.curThread (.tcb x2)) σ.curThread) m := by + have ne : σ.curSc ≠ σ.curThread := fun h => by + obtain ⟨y0, j, h0⟩ := J.curInv.sc + obtain ⟨x, hx, -⟩ := J.curInv.tcb + rw [h, hx] at h0; cases h0 + refine ⟨BodyOKA.ofEntry J ?_ ?_ ?_ ?_ ?_ ?_ ?_ (fun y' k h => ?_) (fun h => ?_), ?_⟩ + all_goals first + | (unfold stsAct; split_ifs <;> first | exact Or.inl rfl | exact Or.inr rfl) + | (unfold stsAct; split_ifs <;> rfl) + | skip + · simp only [stsAct_kheap, updK_kheap, Function.update_of_ne ne] at h + exact ⟨y', h, rfl, rfl, rfl⟩ + · unfold stsAct at h + split_ifs at h with hc + all_goals first + | cases h + | (exfalso + refine hc ⟨rfl, h, fun hs => ?_⟩ + have := (schedulable_partsA hs).1 x2 (by rw [updK_kheap, Function.update_self]) + rw [hns] at this + cases this) + +theorem faultSlot_progA (t : U64) : + obj.Tcb.slot t layout.tcb.SLOT_FAULT_HANDLER = ok (slotIdx t layout.tcb.SLOT_FAULT_HANDLER) := by + simp [obj.Tcb.slot, lift, slotIdx, layout.cte.SIZE] + +/-- The program of `handle_fault` with an endpoint handler. -/ +theorem handle_fault_sendProgA (s : S) (a : A) (t : U64) {fe : error.Fault} {ty w0 w1 w2 : U64} {f : Fl4SpecA64.Fault} + (F : UsrFaultA st t fe ty w0 w1 w2 f) {p badge : U64} {sd rv g gr : Bool} + (hcap : capA st s (slotIdx t layout.tcb.SLOT_FAULT_HANDLER) = .Endpoint p badge sd rv g gr) + (hsc : L.mem s (scA t) ≠ 0#u64) ⦃s2 : S⦄ ⦃a2 : A⦄ + (eb : endpoint.send_ipc st ar (faultWordsA st s t ty w0 w1 w2) a true false badge g gr true t p = ok (s2, a2)) : + faults.handle_fault st ar s a t fe = ok (s2, a2) := by + obtain ⟨i, ei, hi⟩ := exists_of_specA (obj.Tcb.sc.spec (L := L) s t) + have hb : (i != 0#u64) = true := by rw [bne_iff_ne, hi]; exact hsc + unfold faults.handle_fault + rw [faultSlot_progA, bind_ok, capA_eq, bind_ok, hcap, ei, bind_ok] + unfold faults.send_fault_ipc + simp only [F.exact s, bind_ok, hb, eb, uncurry_apply_pair, ↓reduceIte] + +/-- The program of `handle_fault` with no handler. -/ +theorem handle_fault_nullProgA (s : S) (a : A) (t : U64) (fe : error.Fault) + (hcap : capA st s (slotIdx t layout.tcb.SLOT_FAULT_HANDLER) = .Null) ⦃s2 : S⦄ + (eb : sched.set_thread_state st s t sched.TS_INACTIVE = ok s2) : + faults.handle_fault st ar s a t fe = ok (s2, a) := by + obtain ⟨i, ei, -⟩ := exists_of_specA (obj.Tcb.sc.spec (L := L) s t) + unfold faults.handle_fault + rw [faultSlot_progA, bind_ok, capA_eq, bind_ok, hcap, ei, bind_ok] + unfold faults.send_fault_ipc + simp only [bind_ok, uncurry_apply_pair, Bool.false_eq_true, ↓reduceIte, eb] + +end + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {s : S} {a : A} {σ : State Unit} {m : McState} + +set_option maxHeartbeats 2000000 in +/-- **A user or VM fault to a waiting receiver without a reply push** (module docstring). -/ +theorem JSyscallA.faultRecvA + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {fe : error.Fault} {ty w0 w1 w2 : U64} {f : Fl4SpecA64.Fault} (F : UsrFaultA st (L.curW s) fe ty w0 w1 w2 f) + (hOk : ∀ (rbo : Option ObjRef) (x : Tcb), + FaultOKA (L := L) (faultWordsA st s (L.curW s) ty w0 w1 w2) (L.curW s) rbo x f) + {p badge : U64} {sd rv g gr : Bool} + (hcap : capA st s (slotIdx (L.curW s) layout.tcb.SLOT_FAULT_HANDLER) = .Endpoint p badge sd rv g gr) + (hgc : getCap (Z := Unit) (getTcbFaultHandlerPtr σ.curThread) σ = + ({(.endpointCap (specPtr p) badge.bv (rightsOfA sd rv g gr), σ)}, False)) + (hp : p ∈ E) {d : U64} {q : List ObjRef} + (hk : σ.kheap (specPtr p) = some (.endpoint (.recvEP (specPtr d :: q)))) + (hnp : (g = false ∧ gr = false) ∨ ∀ y, σ.kheap (specPtr d) = some (.tcb y) → + ∀ e r dd, y.tcbState ≠ .blockedOnReceive e (some r) dd) : + ∃ σ2 s2, ((), σ2) ∈ (handleFault (Z := Unit) σ.curThread f σ).1 ∧ + faults.handle_fault st ar s a (L.curW s) fe = ok (s2, a) ∧ + Sched.WakeEndA σ σ2 ∧ + ∃ Q Rl, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s2 a σ2 m := by + have htZ : L.curW s ∈ Z.T := J.sr.curT + have ct : σ.curThread = specPtr (L.curW s) := J.j.1.sch.curThread + obtain ⟨x, hkx, -⟩ := J.ep.tcb _ htZ + have P := J.faultPcA F hkx + have hscW : L.mem s (scA (L.curW s)) ≠ 0#u64 := J.cur.2 + have hprog := handle_fault_sendProgA (ar := ar) s a (L.curW s) F hcap hscW + have hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM := J.toJStage3EntryA.curNotVmA + have hr : runnable x.tcbState = true := + (runnable_iffA ((J.ep.at_tcb htZ hkx).st)).1 J.running + set t := L.curW s with ht_def + set s1 := faultWordsA st s t ty w0 w1 w2 with hs1 + set x1 : Tcb := { x with tcbFault := some f } with hx1 + set σ1 := updK σ (specPtr t) (.tcb x1) with hσ1 + have w1' : WritesIn st s s1 (FaultWA t) := faultWords_wrG (st := st) J.sr.core.tcbs htZ s ty w0 w1 w2 + have hta : TcbAddr t := J.sr.core.tcbs _ htZ + have tw : ∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → + (K.val ≠ 3376 ∧ K.val ≠ 3384 ∧ K.val ≠ 3392 ∧ K.val ≠ 3400) → L.mem s1 (wa u K) = L.mem s (wa u K) := + fun u hu K k1 k2 k3 => w1'.mem_eq (field_aligned (J.sr.core.tcbs u hu) k1 k2) + (not_faultWA hta (J.sr.core.tcbs u hu) k1 k3) + have hkt : σ1.kheap (specPtr t) = some (.tcb x1) := by simp [hσ1] + have far : ∀ p, p ≠ specPtr t → σ1.kheap p = σ.kheap p := fun p hp => by simp [hσ1, hp] + have hft : x1.tcbFault = some f := rfl + have ne1 : specPtr p ≠ specPtr t := fun e => by rw [e, hkx] at hk; cases hk + have hk1 : σ1.kheap (specPtr p) = some (.endpoint (.recvEP (specPtr d :: q))) := by rw [far _ ne1]; exact hk + -- the receiver + obtain ⟨hep2, xs, -, hq, hQ, hB⟩ := P.ep.at_ep hp hk1 + have hdx : d ∈ xs := by + have : specPtr d ∈ xs.map specPtr := by rw [← hq]; exact List.mem_cons_self + obtain ⟨y, hy, e⟩ := List.mem_map.1 this + rwa [specPtr_inj e] at hy + have hd : d ∈ Z.T := hQ.mem d hdx + have hdst : (L.mem s1 (stA d)).val = 3 := (hB d hdx).1 + have hdb : L.mem s1 (Notif.tBlockA d) = p := (hB d hdx).2 + have hdb0 : L.mem s (Notif.tBlockA d) = p := by rw [← hdb]; exact (tw d hd 3336#u64 (by simp) (by simp) (by simp)).symm + have hst0 : L.mem s (stA d) = L.mem s1 (stA d) := by + rw [stA_eq]; exact (tw d hd _ (by simp) (by simp) (by simp)).symm + obtain ⟨xd, hkd, rd⟩ := P.ep.tcb d hd + obtain ⟨e', rep, dd, hstd⟩ := tsCode_three (rd.st.symm.trans hdst) + have hnb : tsCode x1.tcbState ≠ 3 := (runnable_codeA (show runnable x1.tcbState = true from hr)).1 + have htd : t ≠ d := fun h => by + rw [← h, hkt] at hkd + cases hkd + exact hnb (by rw [hstd]; rfl) + have hdt : specPtr d ≠ specPtr t := fun h => htd (specPtr_inj h).symm + have hkdσ : σ.kheap (specPtr d) = some (.tcb xd) := by rw [← far _ hdt]; exact hkd + have hnp' : (g = false ∧ gr = false) ∨ ∀ e r dd, xd.tcbState ≠ .blockedOnReceive e (some r) dd := + hnp.imp id fun h => h xd hkdσ + have hnp2 : (g = false ∧ gr = false) ∨ rep = none := hnp'.imp id fun h => by + cases rep with + | none => rfl + | some r => exact absurd hstd (h _ r _) + have hn : ¬ ((g = true ∨ gr = true) ∧ rep ≠ none) := by + rintro ⟨c1, c2⟩ + rcases hnp2 with ⟨rfl, rfl⟩ | h + · simp at c1 + · exact c2 h + have hc : m.curCore < fl4A64Platform.numNodes := by rw [J.j.1.sch.core]; exact hCs _ J.j.1.sch.cpuCs + have hO : SchedOpsEA st Z.T E Rp Bf (IpcSchedRelA st Z m κ) := + (ipcSchedOpsA (ar := ar) J.iplaced J.ipcPlace hc hmb hmax htu).2.2.1 + have hF : SchedFootEA st Z.T E Rp Bf (IpcSchedRelA st Z m κ) (fun σ s => MWrA (st := st) Z m σ s) := + schedFootEA_of_sched + have hSR : IpcSchedRelA st Z m κ s1 σ1 := ⟨Z.Q, Z.R, P.sr⟩ + have hdth : Z.th d = some .tcb := (P.sr.thT d).2 hd + have hon : L.mem s1 (affA d) = L.cpu s1 := by + rw [show L.cpu s1 = L.cpu s from w1'.2.1] + have x := hCs _ (P.sr.core.aff d hd) + have y := hCs _ J.j.1.sch.cpuCs + exact UScalar.eq_of_val_eq (by omega) + have hu : ∀ rp, ipcBufOfA true xd = some rp → ∀ k, k < 128 → inUserFrame (bufAddr rp k) σ1 := + fun rp h k hk => inUserFrame_updTcbA hkx _ (J.toJStage3A.bufFrameA hd hkdσ true rp h k hk) + have hrep := J.toJStage3A.repOK_faultA hd hdt hkx (some f) + have hoff := J.toJStage3A.slotOffEwA + have hidle : specPtr t ≠ σ.idleThread ∧ specPtr d ≠ σ.idleThread := by + obtain ⟨y, hy, hs⟩ := J.sinv.idleTcb + refine ⟨fun h => ?_, fun h => ?_⟩ + · rw [← h, hkx] at hy + cases hy + rw [show x.tcbState = .idleThreadState from hs] at hr + exact absurd hr (by decide) + · rw [← h, hkdσ] at hy; cases hy; rw [hstd] at hs; cases hs + have hRun : SndRunA σ1 (specPtr t) x1 := + J.sndRunA hCs h1 (show runnable x1.tcbState = true from hr) (fun dm pr hm => by + refine J.sinv.ctQ J.act dm pr ?_ + rw [ct] + exact hm) rfl rfl hidle.1 + have hbr : (false = true ∨ x1.tcbFault ≠ none) ∧ + ((g = false ∧ gr = false) ∨ ∀ e r dd, xd.tcbState ≠ .blockedOnReceive e (some r) dd) ∧ + SndRunA σ1 (specPtr t) x1 := ⟨Or.inr (by rw [hft]; simp), hnp', hRun⟩ + have body := send_ipc_recvA (st := st) (ar := ar) hO hF J.iplaced.e P.sr.place s1 a σ1 P.ep hSR P.rel + P.cv P.bufCover P.bufNative hoff htZ ((P.sr.thT t).2 htZ) hkt hnb hp hk1 hdth hkd hu hrep hon badge g + gr true false true (Or.inr ⟨f, hft, hOk _ _⟩) (Or.inr hbr) + have fpw := send_ipc_recv_fpA (st := st) (ar := ar) hO hF J.iplaced.e P.sr.place s1 a σ1 P.ep hSR P.rel + P.cv P.bufCover P.bufNative hoff htZ ((P.sr.thT t).2 htZ) hkt hnb hp hk1 hdth hkd hu hrep hon badge g + gr true false true (Or.inr ⟨f, hft, hOk _ _⟩) (Or.inr hbr) + obtain ⟨⟨s2, a2⟩, eb, ⟨⟨ha2, -, σ2, hmem, E2, ⟨Q, Rl, sr2⟩, w⟩, wn⟩⟩ := exists_of_specA (WP.spec_and body fpw) + simp only at ha2 eb E2 sr2 w wn + subst ha2 + have hp2 := hprog eb + have hxsc : x.tcbSchedContext ≠ none := by + rw [Endp.schedTcbScA J.sr htZ hkx, optScA, if_neg hscW] + simp + have hspec : ((), σ2) ∈ (handleFault (Z := Unit) σ.curThread f σ).1 := by + rw [ct] + exact handleFault_sendA (σ1 := σ1) hkx hxsc (by rw [← ct]; exact hgc) (by rw [hσ1, hx1]) hmem + -- the spec outcome's record facts + set M : ObjRef → Prop := fun o => o = specPtr t ∨ o = specPtr d with hMdef + set Nq : ObjRef → Prop := fun o => o = specPtr d with hNdef + have hal := J.toJStage3A.bufAlign0A hd hkdσ true + have KB := sendIpc_recv_faultBlockA (i := σ.idleThread) (M := M) (N := Nq) P.sr.place s1 P.rel P.cv hdth + hk1 hkt hkd hal true false badge.bv g gr true hft (Or.inl rfl) (Or.inr rfl) rfl hdt.symm hstd hn _ hmem + have K := KB.1 + have hDon : DonA σ1 (specPtr t) x1 := by + intro sc hsc + obtain ⟨y, k, hy, hb⟩ := P.sr.inv.back (specPtr t) x1 sc hkt hsc + refine ⟨fun h => ?_, fun y' k' hy' => by rw [hy] at hy'; cases hy'; exact hb⟩ + obtain ⟨x', yi, ki, -, -, hyi, hti⟩ := J.sinv.idleSc + rw [h, far _ (fun e => by rw [e, hkx] at hyi; cases hyi), hyi] at hy + cases hy + exact hidle.1 (Option.some.inj (hb.symm.trans hti)) + have hact : ActTcbA σ1 (specPtr t) := by + have h := (P.sv.act J.act (fun c => hidle.1 (ct ▸ c))).1 + obtain ⟨y, sc', ex, es, a'⟩ := h + exact ⟨y, sc', ct ▸ ex, es, a'⟩ + have Qs : QSendA σ1 σ2 := + sendIpc_recv_qsA (i := σ.idleThread) (M := M) (N := Nq) P.sr.place s1 P.rel P.cv hdth hk1 hkt hkd hal + true false badge.bv g gr true (Or.inr ⟨f, hft⟩) (Or.inl rfl) (Or.inr rfl) rfl hidle.1 hidle.2 hDon + (fun _ => by + have h := hact + obtain ⟨y, sc', ex, es, -⟩ := h + rw [hkt] at ex + cases ex + rw [es]; simp) (fun _ => scActive_of_actTcbA hact hkt) + (fun e' r dd h => by + obtain ⟨xr, k, b⟩ := hrep xd hkd e' r dd h + exact ⟨xr, k, by rw [b]; simp⟩) _ hmem + have U2 : UFrA (BufBytesA (ipcBufOfA true xd)) σ1 σ2 := + sendIpc_recv_ufrFA P.sr.place s1 P.rel P.cv hdth hk1 hkt hkd hal true false badge.bv g gr true + (Or.inr ⟨f, hft⟩) _ hmem + have sv : SchedValidA σ2 := + sendIpc_recv_schedValidFA P.sr.place s1 P.rel P.cv hdth hk1 hkt hkd hal true false badge.bv g gr true + (Or.inr ⟨f, hft⟩) hstd (Or.inr (Or.inl ⟨Or.inr (by rw [hft]; simp), hnp2⟩)) P.sv (rqDomPrio_of_schedA P.sr) + _ hmem + have run : StIs (specPtr d) .running σ2 := sendIpc_recv_runningA hk1 true false badge.bv g gr true _ hmem + have after : ∀ u ∈ Z.T, M (specPtr u) → ∀ y, σ2.kheap (specPtr u) = some (.tcb y) → + epBlocked y.tcbState = none ∧ ∀ p, y.tcbState ≠ .blockedOnNotification p := by + intro u _ hu y hy + rcases hu with h | h + · rw [h] at hy + rw [KB.2 y hy] + exact ⟨rfl, fun _ h => by cases h⟩ + · rw [h] at hy + rw [run y hy] + exact ⟨rfl, fun _ h => by cases h⟩ + have wk : NtfnWaitKeepA Z.T N (L.mem s) (L.mem s2) := + J.toJStage3A.ntfnWait_sendXA htZ htZ hp hd hdb0 ((w1'.trans wn).mono fun _ h => + h.elim Or.inr fun h => Or.inl (Or.inl (J.toJStage3A.sendFp_faultA htZ _ ty w0 w1 w2 h))) + have wI : WritesIn st s1 s2 (fun b => IpcFpA st Z E Rp Bf m σ1 s1 b ∨ RepWA Rp b) := WritesIn.mono w fun _ => Or.inl + have wF : WritesIn st s s2 (fun b => IpcFpA st Z E Rp Bf m σ s b ∨ FaultWA t b) := + (w1'.trans w).mono fun _ h => h.elim Or.inr fun h => Or.inl (h.imp id (mwr_back rfl rfl (ssz_updK_tcb σ _ _) w1'.1)) + have notM : ∀ c < n, ¬ M (m.idleThreadOf c) := by + intro c hc' h + obtain ⟨y, hy, hs⟩ := J.sidle.f3 c hc' + rcases h with h | h + · rw [h, hkx] at hy + cases hy + rw [show x.tcbState = .idleThreadState from hs] at hr + exact absurd hr (by decide) + · rw [h, hkdσ] at hy; cases hy; rw [hstd] at hs; cases hs + have hMv : ∀ u ∈ Z.T, M (specPtr u) → L.mem s (stA u) ≠ sched.TS_RUNNING_VM := by + intro u _ hu h + rcases hu with e | e <;> rw [specPtr_inj e] at h + · exact hnvm h + · rw [hst0] at h; rw [h] at hdst; revert hdst; unfold sched.TS_RUNNING_VM; decide + have hwake : Sched.WakeEndA σ σ2 := by + have E3 := Sched.sendIpc_recv_endA P.sr.place s1 P.rel P.cv hdth hk1 hkt hkd hal true false badge.bv g gr true + (Or.inr ⟨f, hft⟩) hstd + (Or.inr (Or.inl ⟨Or.inr (by rw [hft]; simp), hnp2⟩)) + P.sv (rqDomPrio_of_schedA P.sr) J.noSwitch hDon hidle.1 hidle.2 _ hmem + have B : Sched.BLA σ1 σ2 := Sched.sendIpc_recv_blFA P.sr.place s1 P.rel P.cv hdth hk1 hkt hkd hal true false + badge.bv g gr true (Or.inr ⟨f, hft⟩) _ hmem + have B0 : Sched.BLA σ σ1 := Sched.BLA.tcbW hkx + exact ⟨PreO.trans B0 B, E3.1, + fun hres => absurd hres (E3.2 ⟨show σ1.curThread = specPtr t from ct, Or.inr (by rw [hft]; simp)⟩)⟩ + refine ⟨σ2, s2, hspec, hp2, hwake, Q, Rl, ?_⟩ + exact J.faultTailA hA hPl hCs h1 hkx hp2 wI K (Or.inl rfl) U2.1 U2.2.1 (fun h => QSendA.toBlk h Qs) + (J.toJStage3A.specZero_of_recvBufA hd hkdσ U2.2.2) sr2 E2 (fun K13 => J.toJStage3A.reply_ipcXA wF htZ K13) + J.deadExt.reply (NtfnWaitKeepA.toM wk) + (fun u _ hu y hy p h => by + rcases hu with e | e <;> rw [e] at hy + · rw [hkx] at hy; cases hy; rw [h] at hr; exact absurd hr (by simp [runnable]) + · rw [hkdσ] at hy; cases hy; rw [hstd] at h; cases h) + (fun u hu hm y hy => (after u hu hm y hy).2) (fun K13 => J.toJStage3A.blkQ_ipcXA K13 E2 wk after) sv notM + (fun h => h.elim (fun h => hidle.1 h.symm) fun h => hidle.2 h.symm) + (fun y hy dm pr hm => by + rcases hy with rfl | rfl + · exact J.sinv.ctQ J.act dm pr (ct ▸ hm) + · obtain ⟨z, hz, hr'⟩ := J.sinv.rq dm pr _ hm + rw [hkdσ] at hz; cases hz; rw [hstd] at hr'; cases hr') + (fun y hy => by + cases hy + exact ⟨fun h => hdt (h.trans ct), hidle.2, fun c hc' h => notM c hc' (Or.inr h), + fun z hz => by rw [run z hz]; rfl⟩) + (fun y hy => by cases hy; exact ⟨xd, hkdσ⟩) + (fun y hy hr' => by + rcases hy with rfl | rfl + · exact hRun.2.1.2 hr' + · obtain ⟨x', hx, hrn⟩ := J.sinv.rlsRun _ hr'; rw [hkdσ] at hx; cases hx; rw [hstd] at hrn; cases hrn) + (fun q hq y hy hf => by + rcases hq with rfl | rfl + · rw [KB.2 y hy]; exact Or.inr (Or.inr rfl) + · obtain ⟨x0, hx0, g⟩ := K.tcbB hy + rw [hkd] at hx0; cases hx0 + exact absurd (g.fault.trans (FaultStOkA.none J.sinv.faultSt hkdσ (by + rw [hstd]; rintro (⟨_, _, h⟩ | ⟨_, h⟩ | h) <;> cases h))) hf) + hMv + +set_option maxHeartbeats 2000000 in +/-- **A user or VM fault to a waiting receiver with a reply push and donation** (module docstring). -/ +theorem JSyscallA.faultDonateA + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {fe : error.Fault} {ty w0 w1 w2 : U64} {f : Fl4SpecA64.Fault} (F : UsrFaultA st (L.curW s) fe ty w0 w1 w2 f) + (hOk : ∀ (rbo : Option ObjRef) (x : Tcb), + FaultOKA (L := L) (faultWordsA st s (L.curW s) ty w0 w1 w2) (L.curW s) rbo x f) + {p badge : U64} {sd rv g gr : Bool} + (hcap : capA st s (slotIdx (L.curW s) layout.tcb.SLOT_FAULT_HANDLER) = .Endpoint p badge sd rv g gr) + (hgc : getCap (Z := Unit) (getTcbFaultHandlerPtr σ.curThread) σ = + ({(.endpointCap (specPtr p) badge.bv (rightsOfA sd rv g gr), σ)}, False)) + (hp : p ∈ E) {d : U64} {q : List ObjRef} + (hk : σ.kheap (specPtr p) = some (.endpoint (.recvEP (specPtr d :: q)))) + (hg : g = true ∨ gr = true) + (hrcv : ∀ y, σ.kheap (specPtr d) = some (.tcb y) → + y.tcbSchedContext = none ∧ ∃ e r dd, y.tcbState = .blockedOnReceive e (some r) dd) + (hV : L.mem s (wa (L.curW s) 3664#u64) = 0#u64) : + ∃ σ2 s2, ((), σ2) ∈ (handleFault (Z := Unit) σ.curThread f σ).1 ∧ + faults.handle_fault st ar s a (L.curW s) fe = ok (s2, a) ∧ + Sched.WakeEndA σ σ2 ∧ + ∃ Q Rl, ∃ r ∈ Rp, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf + (Function.update stack (L.mem s (scA (L.curW s))) (r :: stack (L.mem s (scA (L.curW s))))) κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s2 a σ2 m := by + have htZ : L.curW s ∈ Z.T := J.sr.curT + have ct : σ.curThread = specPtr (L.curW s) := J.j.1.sch.curThread + obtain ⟨x, hkx, -⟩ := J.ep.tcb _ htZ + have P := J.faultPcA F hkx + have hscW : L.mem s (scA (L.curW s)) ≠ 0#u64 := J.cur.2 + have hprog := handle_fault_sendProgA (ar := ar) s a (L.curW s) F hcap hscW + have hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM := J.toJStage3EntryA.curNotVmA + have hr : runnable x.tcbState = true := + (runnable_iffA ((J.ep.at_tcb htZ hkx).st)).1 J.running + set t := L.curW s with ht_def + set s1 := faultWordsA st s t ty w0 w1 w2 with hs1 + set x1 : Tcb := { x with tcbFault := some f } with hx1 + set σ1 := updK σ (specPtr t) (.tcb x1) with hσ1 + have w1' : WritesIn st s s1 (FaultWA t) := faultWords_wrG (st := st) J.sr.core.tcbs htZ s ty w0 w1 w2 + have hta : TcbAddr t := J.sr.core.tcbs _ htZ + have tw : ∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → + (K.val ≠ 3376 ∧ K.val ≠ 3384 ∧ K.val ≠ 3392 ∧ K.val ≠ 3400) → L.mem s1 (wa u K) = L.mem s (wa u K) := + fun u hu K k1 k2 k3 => w1'.mem_eq (field_aligned (J.sr.core.tcbs u hu) k1 k2) + (not_faultWA hta (J.sr.core.tcbs u hu) k1 k3) + have hkt : σ1.kheap (specPtr t) = some (.tcb x1) := by simp [hσ1] + have far : ∀ p, p ≠ specPtr t → σ1.kheap p = σ.kheap p := fun p hp => by simp [hσ1, hp] + have hft : x1.tcbFault = some f := rfl + have ne1 : specPtr p ≠ specPtr t := fun e => by rw [e, hkx] at hk; cases hk + have hk1 : σ1.kheap (specPtr p) = some (.endpoint (.recvEP (specPtr d :: q))) := by rw [far _ ne1]; exact hk + -- the receiver + obtain ⟨hep2, xs, -, hq, hQ, hB⟩ := P.ep.at_ep hp hk1 + have hdx : d ∈ xs := by + have : specPtr d ∈ xs.map specPtr := by rw [← hq]; exact List.mem_cons_self + obtain ⟨y, hy, e⟩ := List.mem_map.1 this + rwa [specPtr_inj e] at hy + have hd : d ∈ Z.T := hQ.mem d hdx + have hdst : (L.mem s1 (stA d)).val = 3 := (hB d hdx).1 + have hdb : L.mem s1 (Notif.tBlockA d) = p := (hB d hdx).2 + have hdb0 : L.mem s (Notif.tBlockA d) = p := by rw [← hdb]; exact (tw d hd 3336#u64 (by simp) (by simp) (by simp)).symm + have hst0 : L.mem s (stA d) = L.mem s1 (stA d) := by + rw [stA_eq]; exact (tw d hd _ (by simp) (by simp) (by simp)).symm + obtain ⟨xd, hkd, rd⟩ := P.ep.tcb d hd + obtain ⟨e', rep, dd, hstd⟩ := tsCode_three (rd.st.symm.trans hdst) + have hnb : tsCode x1.tcbState ≠ 3 := (runnable_codeA (show runnable x1.tcbState = true from hr)).1 + have htd : t ≠ d := fun h => by + rw [← h, hkt] at hkd + cases hkd + exact hnb (by rw [hstd]; rfl) + have hdt : specPtr d ≠ specPtr t := fun h => htd (specPtr_inj h).symm + have hkdσ : σ.kheap (specPtr d) = some (.tcb xd) := by rw [← far _ hdt]; exact hkd + have hc : m.curCore < fl4A64Platform.numNodes := by rw [J.j.1.sch.core]; exact hCs _ J.j.1.sch.cpuCs + have hdth : Z.th d = some .tcb := (P.sr.thT d).2 hd + have hu : ∀ rp, ipcBufOfA true xd = some rp → ∀ k, k < 128 → inUserFrame (bufAddr rp k) σ1 := + fun rp h k hk => inUserFrame_updTcbA hkx _ (J.toJStage3A.bufFrameA hd hkdσ true rp h k hk) + have hrep := J.toJStage3A.repOK_faultA hd hdt hkx (some f) + have hoff := J.toJStage3A.slotOffEwA + have RW := J.toJStage3A.faultRepA htZ ty w0 w1 w2 + have hidle : specPtr t ≠ σ.idleThread ∧ specPtr d ≠ σ.idleThread := by + obtain ⟨y, hy, hs⟩ := J.sinv.idleTcb + refine ⟨fun h => ?_, fun h => ?_⟩ + · rw [← h, hkx] at hy + cases hy + rw [show x.tcbState = .idleThreadState from hs] at hr + exact absurd hr (by decide) + · rw [← h, hkdσ] at hy; cases hy; rw [hstd] at hs; cases hs + -- the receiver's reply object, the sender's SC + obtain ⟨HRCV, er, rr, edd, hsr⟩ := hrcv xd hkdσ + have hrep0 : rep = some rr := by + have h := hstd.symm.trans hsr + injection h + subst hrep0 + have hrcvw := rd.recv _ _ _ hstd + obtain ⟨-, hrr⟩ := optRef_some hrcvw.1 + have hRrp : L.mem s1 (tReplyA d) ∈ Rp := hrcvw.2 (by simp) + set r : U64 := L.mem s1 (tReplyA d) with hr_def + subst hrr + have he' : e' = specPtr p := by + have h := rd.blk e' (by rw [hstd]; rfl) + rw [← h, hdb] + subst he' + obtain ⟨xr, hkr, hback⟩ := hrep xd hkd _ _ _ hstd + have hnbr0 : L.mem s (stA (L.mem s (Endp.rTcbA r))) ≠ sched.TS_BLOCKED_ON_REPLY := by + obtain ⟨yr, hyr, er⟩ := P.ep.reply r hRrp + rw [hkr] at hyr + cases hyr + rw [hback] at er + have eT : L.mem s (Endp.rTcbA r) = d := by + rw [show Endp.rTcbA r = wa r layout.reply.TCB from rfl, + ← RW r hRrp layout.reply.TCB (by simp [layout.reply.TCB]) (by simp [layout.reply.TCB])] + exact specPtr_inj (optRef_some er.symm).2 + rw [eT, hst0] + intro h + rw [h] at hdst + revert hdst + unfold sched.TS_BLOCKED_ON_REPLY + simp + have hri : ReplyIsolatedA (L.mem s1) Z.C stack r := by + obtain ⟨i1, i2, i3⟩ := J.toJStage3A.replyIsolatedA hRrp hnbr0 + exact ⟨i1, (RW r hRrp layout.reply.NEXT (by simp [layout.reply.NEXT]) (by simp [layout.reply.NEXT])).trans i2, + (RW r hRrp layout.reply.PREV (by simp [layout.reply.PREV]) (by simp [layout.reply.PREV])).trans i3⟩ + set sc : U64 := L.mem s1 (scA t) with hsc_def + have hscs : sc = L.mem s (scA t) := by rw [hsc_def, scA_eq]; exact tw t htZ _ (by simp) (by simp) (by simp) + have hsc0 : sc ≠ 0#u64 := by rw [hscs]; exact hscW + have hscC : sc ∈ Z.C := P.sr.rest.scMem t htZ hsc0 + have htSc : x1.tcbSchedContext = some (scNameA m sc) := by + rw [Endp.schedTcbScA P.sr htZ hkt, ← hsc_def, optScA, if_neg hsc0] + rfl + have hDon : DonA σ1 (specPtr t) x1 := by + intro sc' hsc' + obtain ⟨y, k, hy, hb⟩ := P.sr.inv.back (specPtr t) x1 sc' hkt hsc' + refine ⟨fun h => ?_, fun y' k' hy' => by rw [hy] at hy'; cases hy'; exact hb⟩ + obtain ⟨x0, yi, ki, -, -, hyi, hti⟩ := J.sinv.idleSc + rw [h, far _ (fun e => by rw [e, hkx] at hyi; cases hyi), hyi] at hy + cases hy + exact hidle.1 (Option.some.inj (hb.symm.trans hti)) + have hni : NotIdleScA m (scNameA m sc) := by + have h1' : scNameA m sc ≠ idleScPtr := (hDon _ htSc).1 + refine ⟨h1', fun c hc' e => ?_⟩ + have hcc : c = m.curCore := by + have a : c < 1 := h1 ▸ hc' + have b : m.curCore < 1 := h1 ▸ hc + exact (Nat.lt_one_iff.1 a).trans (Nat.lt_one_iff.1 b).symm + rw [hcc] at e + unfold scNameA at e h1' + split_ifs at e h1' with g + · exact h1' rfl + · exact g e + have affE : ∀ u ∈ Z.T, L.mem s1 (affA u) = L.cpu s1 := fun u hu => by + rw [show L.cpu s1 = L.cpu s from w1'.2.1] + have x := hCs _ (P.sr.core.aff u hu) + have y := hCs _ J.j.1.sch.cpuCs + exact UScalar.eq_of_val_eq (by omega) + have hRun : SndRunA σ1 (specPtr t) x1 := + J.sndRunA hCs h1 (show runnable x1.tcbState = true from hr) (fun dm pr hm => by + refine J.sinv.ctQ J.act dm pr ?_ + rw [ct] + exact hm) rfl rfl hidle.1 + have hal := J.toJStage3A.bufAlign0A hd hkdσ true + have hd0 := HRCV + have hmig : L.mem s1 (affA d) = L.mem s1 (scCoreA sc) := by + have x := hCs _ (P.sr.core.aff d hd) + have y := J.scCpu sc hscC + refine UScalar.eq_of_val_eq ?_ + rw [P.sr.scCore sc hscC] + omega + have hV1 : L.mem s1 (wa t 3664#u64) = 0#u64 := by + rw [tw t htZ _ (by simp) (by simp) (by simp)]; exact hV + have hRB : RelIfBound σ1 (specPtr t) := P.sr.rd _ _ hkt (Or.inl hRun.1) + have hRel := relIfBoundSc_donPushFA (st := st) P.sr.place s1 P.rel P.cv hdth hk1 + (epDeq (.recvEP (specPtr d :: q)) (specPtr d)) hkt hkd hft hal hdt.symm hd0 htSc hDon hidle.1 hidle.2 + hRun.2.1.2 hRB badge.bv g (specPtr r) + have pd := send_ipc_recv_pushDonate_fpA (st := st) (ar := ar) a P.sr J.ipcPlace J.iplaced.e P.ep P.reply + P.sr.place P.rel P.cv P.bufCover P.bufNative hoff htZ hkt hRun hp hk1 hd hkd hu hrep badge g + gr true false (Or.inr ⟨f, hft, hOk _ _⟩) (sc := sc) (r := r) hscC hsc0 hRrp hri rfl + (by + refine UScalar.eq_of_val_eq ?_ + have h := Endp.schedTcbScA P.sr hd hkd + rw [hd0] at h + unfold optScA at h + split_ifs at h with c + rw [c]) + (affE t htZ) (affE d hd) hmig hV1 hni hstd (Or.inr (by rw [hft]; simp)) hg hRel + obtain ⟨⟨s2, a2⟩, eb, ⟨ha2, σ2, hspecI, E2, ⟨Q, Rl, sr2⟩, rs2, wD⟩⟩ := exists_of_specA pd + simp only at ha2 eb E2 sr2 rs2 wD + subst ha2 + have hmem : ((), σ2) ∈ (sendIpc (Z := Unit) true false badge.bv g gr true (specPtr t) (specPtr p) σ1).1 := by + rw [hspecI]; exact Set.mem_singleton _ + have hp2 := hprog eb + have hxsc : x.tcbSchedContext ≠ none := by + rw [show x.tcbSchedContext = x1.tcbSchedContext from rfl, htSc] + simp + have hspec : ((), σ2) ∈ (handleFault (Z := Unit) σ.curThread f σ).1 := by + rw [ct] + exact handleFault_sendA (σ1 := σ1) hkx hxsc (by rw [← ct]; exact hgc) (by rw [hσ1, hx1]) hmem + -- the spec outcome's record facts + set M : ObjRef → Prop := fun o => o = specPtr t ∨ o = specPtr d with hMdef + set Nq : ObjRef → Prop := fun o => o = specPtr d with hNdef + have K : SIKA σ.idleThread M Nq true σ1 σ2 := + sendIpc_recv_sikFA (M := M) (N := Nq) P.sr.place s1 P.rel P.cv hdth hk1 hkt hkd hal true false + badge.bv g gr true (Or.inr ⟨f, hft⟩) (Or.inl rfl) (Or.inr rfl) rfl hidle.1 hidle.2 hDon + (fun _ => by rw [htSc]; simp) (fun c => by cases c) _ hmem + have hact : ActTcbA σ1 (specPtr t) := by + have h := (P.sv.act J.act (fun c => hidle.1 (ct ▸ c))).1 + obtain ⟨y, sc', ex, es, a'⟩ := h + exact ⟨y, sc', ct ▸ ex, es, a'⟩ + have Qs : QSendA σ1 σ2 := + sendIpc_recv_qsA (i := σ.idleThread) (M := M) (N := Nq) P.sr.place s1 P.rel P.cv hdth hk1 hkt hkd hal + true false badge.bv g gr true (Or.inr ⟨f, hft⟩) (Or.inl rfl) (Or.inr rfl) rfl hidle.1 hidle.2 hDon + (fun _ => by + have h := hact + obtain ⟨y, sc', ex, es, -⟩ := h + rw [hkt] at ex + cases ex + rw [es]; simp) (fun _ => scActive_of_actTcbA hact hkt) + (fun e' r dd h => by + obtain ⟨xr, k, b⟩ := hrep xd hkd e' r dd h + exact ⟨xr, k, by rw [b]; simp⟩) _ hmem + have U2 : UFrA (BufBytesA (ipcBufOfA true xd)) σ1 σ2 := + sendIpc_recv_ufrFA P.sr.place s1 P.rel P.cv hdth hk1 hkt hkd hal true false badge.bv g gr true + (Or.inr ⟨f, hft⟩) _ hmem + have sv : SchedValidA σ2 := + sendIpc_recv_schedValidFA P.sr.place s1 P.rel P.cv hdth hk1 hkt hkd hal true false badge.bv g gr true + (Or.inr ⟨f, hft⟩) hstd + (Or.inr (Or.inr (Or.inl ⟨Or.inr (by rw [hft]; simp), hg, by simp, hdt.symm, hact, fun _ => hd0⟩))) P.sv + (rqDomPrio_of_schedA P.sr) _ hmem + have run : StIs (specPtr d) .running σ2 := sendIpc_recv_runningA hk1 true false badge.bv g gr true _ hmem + have snt : StIs (specPtr t) (.blockedOnReply (specPtr r)) σ2 := + sendIpc_recv_faultPushA P.sr.place s1 P.rel P.cv hdth hk1 hkt hkd hal true false badge.bv g gr true hft + (M := fun _ => True) trivial hdt.symm hstd hg _ hmem + have after : ∀ u ∈ Z.T, M (specPtr u) → ∀ y, σ2.kheap (specPtr u) = some (.tcb y) → + epBlocked y.tcbState = none ∧ ∀ p, y.tcbState ≠ .blockedOnNotification p := by + intro u _ hu y hy + rcases hu with h | h + · rw [h] at hy + rw [snt y hy] + exact ⟨rfl, fun _ h => by cases h⟩ + · rw [h] at hy + rw [run y hy] + exact ⟨rfl, fun _ h => by cases h⟩ + have wk : NtfnWaitKeepA Z.T N (L.mem s) (L.mem s2) := + J.toJStage3A.ntfnWait_sendXA (W := True) htZ htZ hp hd hdb0 ((w1'.trans wD).mono fun _ h => + h.elim Or.inr fun h => Or.inl (h.imp (J.toJStage3A.sendFp_faultA htZ _ ty w0 w1 w2) + (Or.imp id fun c => ⟨t, htZ, c⟩))) + have wI : WritesIn st s1 s2 (fun b => IpcFpA st Z E Rp Bf m σ1 s1 b ∨ RepWA Rp b) := + wD.mono fun _ h => donFpA_splitA J.sr.core.tcbs htZ hp hd h + have notM : ∀ c < n, ¬ M (m.idleThreadOf c) := by + intro c hc' h + obtain ⟨y, hy, hs⟩ := J.sidle.f3 c hc' + rcases h with h | h + · rw [h, hkx] at hy + cases hy + rw [show x.tcbState = .idleThreadState from hs] at hr + exact absurd hr (by decide) + · rw [h, hkdσ] at hy; cases hy; rw [hstd] at hs; cases hs + have hMv : ∀ u ∈ Z.T, M (specPtr u) → L.mem s (stA u) ≠ sched.TS_RUNNING_VM := by + intro u _ hu h + rcases hu with e | e <;> rw [specPtr_inj e] at h + · exact hnvm h + · rw [hst0] at h; rw [h] at hdst; revert hdst; unfold sched.TS_RUNNING_VM; decide + have hkrσ : σ.kheap (specPtr r) = some (.reply xr) := by + rw [← far _ (fun e => by rw [e, hkt] at hkr; cases hkr)]; exact hkr + have HDR : ∀ q' ∈ Rp, DeadA st OS .reply s q' → ∀ c ∈ Z.C, q' ∉ Function.update stack sc (r :: stack sc) c := by + intro q' hq' hdead c hc' hm + have old : q' ∈ stack c → False := fun h => J.deadExt.reply q' hq' hdead c hc' h + by_cases ec : c = sc + · subst ec + rw [Function.update_self] at hm + rcases List.mem_cons.1 hm with e | h + · subst e + exact absurd (J.deadExt.replyTcb r hRrp hdead xr hkrσ) (by rw [hback]; simp) + · exact old h + · rw [Function.update_of_ne ec] at hm + exact old hm + have J2 := J.faultTailA hA hPl hCs h1 hkx hp2 wI K (Or.inl rfl) U2.1 U2.2.1 (fun h => QSendA.toBlk h Qs) + (J.toJStage3A.specZero_of_recvBufA hd hkdσ U2.2.2) sr2 E2 (fun _ => rs2) HDR (NtfnWaitKeepA.toM wk) + (fun u _ hu y hy p h => by + rcases hu with e | e <;> rw [e] at hy + · rw [hkx] at hy; cases hy; rw [h] at hr; exact absurd hr (by simp [runnable]) + · rw [hkdσ] at hy; cases hy; rw [hstd] at h; cases h) + (fun u hu hm y hy => (after u hu hm y hy).2) (fun K13 => J.toJStage3A.blkQ_ipcXA K13 E2 wk after) sv notM + (fun h => h.elim (fun h => hidle.1 h.symm) fun h => hidle.2 h.symm) + (fun y hy dm pr hm => by + rcases hy with rfl | rfl + · exact J.sinv.ctQ J.act dm pr (ct ▸ hm) + · obtain ⟨z, hz, hr'⟩ := J.sinv.rq dm pr _ hm + rw [hkdσ] at hz; cases hz; rw [hstd] at hr'; cases hr') + (fun y hy => by + cases hy + exact ⟨fun h => hdt (h.trans ct), hidle.2, fun c hc' h => notM c hc' (Or.inr h), + fun z hz => by rw [run z hz]; rfl⟩) + (fun y hy => by cases hy; exact ⟨xd, hkdσ⟩) + (fun y hy hr' => by + rcases hy with rfl | rfl + · exact hRun.2.1.2 hr' + · obtain ⟨x', hx, hrn⟩ := J.sinv.rlsRun _ hr'; rw [hkdσ] at hx; cases hx; rw [hstd] at hrn; cases hrn) + (fun q hq y hy hf => by + rcases hq with rfl | rfl + · rw [snt y hy]; exact Or.inr (Or.inl ⟨_, rfl⟩) + · obtain ⟨x0, hx0, g⟩ := K.tcbB hy + rw [hkd] at hx0; cases hx0 + exact absurd (g.fault.trans (FaultStOkA.none J.sinv.faultSt hkdσ (by + rw [hstd]; rintro (⟨_, _, h⟩ | ⟨_, h⟩ | h) <;> cases h))) hf) + hMv + have hwake : Sched.WakeEndA σ σ2 := by + have E3 := Sched.sendIpc_recv_endA P.sr.place s1 P.rel P.cv hdth hk1 hkt hkd hal true false badge.bv g gr true + (Or.inr ⟨f, hft⟩) hstd + (Or.inr (Or.inr (Or.inl ⟨Or.inr (by rw [hft]; simp), hg, by simp, hdt.symm, hact, fun _ => HRCV⟩))) + P.sv (rqDomPrio_of_schedA P.sr) J.noSwitch hDon hidle.1 hidle.2 _ hmem + have B : Sched.BLA σ1 σ2 := Sched.sendIpc_recv_blFA P.sr.place s1 P.rel P.cv hdth hk1 hkt hkd hal true false + badge.bv g gr true (Or.inr ⟨f, hft⟩) _ hmem + have B0 : Sched.BLA σ σ1 := Sched.BLA.tcbW hkx + exact ⟨PreO.trans B0 B, E3.1, + fun hres => absurd hres (E3.2 ⟨show σ1.curThread = specPtr t from ct, Or.inr (by rw [hft]; simp)⟩)⟩ + rw [hscs] at J2 + exact ⟨σ2, s2, hspec, hp2, hwake, Q, Rl, r, hRrp, J2⟩ + +/-! ## The `handle_fault` placement and P1 post over `RecvPreA` -/ + +/-- The current thread's fault handler slot is an `OS` slot. -/ +theorem RecvPreA.faultSlotA (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) : + slotIdx (L.curW s) layout.tcb.SLOT_FAULT_HANDLER ∈ OS := by + obtain ⟨Pt, Ub, Pp7, Tb7, Hr, Te, Kt, Lv7, Re, bits, KLv, W⟩ := J7A.unp J.j7 + have I := J.toJStage3A.delIn hA hPl W + have htH : L.curW s ∈ H.tcbs := by rw [J.tcbs]; exact J.sr.curT + exact I.ts _ htH _ (by simp [layout.tcb.SLOT_FAULT_HANDLER]) + +/-- P1's placement with the current thread's fault handler slot as the invoked slot. -/ +theorem RecvPreA.faultPlaceA (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) : + SysPlace st H Z.Cs H.frame H.replies H.ntfns OS s (Array.repeat 3#usize (0#u64 : obj.Slot)) + (slotIdx (L.curW s) layout.tcb.SLOT_FAULT_HANDLER) := by + have hx := J.faultSlotA hA hPl + have e0 : wa (slotIdx (L.curW s) layout.tcb.SLOT_FAULT_HANDLER) 0#u64 = + slotIdx (L.curW s) layout.tcb.SLOT_FAULT_HANDLER := by + apply UScalar.eq_of_val_eq; rw [wa_valm]; simp + refine ⟨J.sys.ent.heap, J.sys.ent.os, J.toJStage3A.winOK, J.toJStage3A.slotTy, J.toJStage3A.core0, faultExtraA, hx, + ⟨?_, fun o o1 o2 => ?_⟩, J.toJStage3A.wuApart⟩ + · have := J.sys.ent.os.al hx (k := 0#u64) (by simp) (by simp) + rwa [e0] at this + · have hfit := J.sys.ent.os.fit _ hx + have hv : (wa (slotIdx (L.curW s) layout.tcb.SLOT_FAULT_HANDLER) o).val = + (slotIdx (L.curW s) layout.tcb.SLOT_FAULT_HANDLER).val + o.val := by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have hta := J.sr.core.tcbs _ J.sr.curT + have hf := hta.fits + have es : (slotIdx (L.curW s) layout.tcb.SLOT_FAULT_HANDLER).val = (L.curW s).val + 96 := by + unfold slotIdx layout.tcb.SLOT_FAULT_HANDLER + rw [show core.num.U64.wrapping_mul 3#u64 32#u64 = 96#u64 from rfl, wa_valm] + simp + omega + have ek : (wa 96#u64 o).val = 96 + o.val := by + rw [wa_valm] + simp + omega + refine Or.inl ⟨L.curW s, by rw [J.tcbs]; exact J.sr.curT, wa 96#u64 o, by omega, ?_⟩ + apply UScalar.eq_of_val_eq + rw [hv, es, field_val hta (by omega : (wa 96#u64 o).val < 4096), ek] + omega + +/-- **P1's post of `handle_fault`** at a syscall-entry record (`faults.handle_fault.rpw`). -/ +theorem RecvPreA.faultRpA (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + {fe : error.Fault} {s' : S} {a' : A} + (hp : faults.handle_fault st ar s a (L.curW s) fe = ok (s', a')) : + RpPost st H Z.Cs OS Z.V ia ie Ac s s' := by + obtain ⟨Pt, Ub, Pp7, Tb7, Hr, Te, Kt, Lv7, Re, bits, KLv, W⟩ := J7A.unp J.j7 + have I := J.toJStage3A.delIn hA hPl W + have hrun : L.mem s (stA (L.curW s)) ≠ sched.TS_INACTIVE := by + rcases J.runW with h | h | h <;> rw [h] <;> unfold sched.TS_INACTIVE <;> + first + | (unfold sched.TS_RUNNING; decide) + | (unfold sched.TS_RESTART; decide) + | (unfold sched.TS_RUNNING_VM; decide) + have hRV : RunVcpuInv st H.tcbs s := by rw [J.tcbs]; exact J.runVcpu + have p := faults.handle_fault.rpw (st := st) (ar := ar) hA a (L.curW s) fe (J.faultPlaceA hA hPl) + (RpW.start (Vc := Z.V) J.sys.sysl J.symL (BXI.blk J.sys.ent.bxi)) I.del.sca I.ts J.ko I.del.vr J.canBlock hrun hRV + have h := post_of_okA p hp + simpa only [WP.uncurry'_pair] using h + +set_option maxHeartbeats 1000000 in +/-- **A fault with no handler, with the post state** (module docstring): the thread becomes `Inactive` and the +scheduler action `chooseNewThread`; `JSyscallA.faultNullA` is its projection. -/ +theorem RecvPreA.faultNullExA + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (fe : error.Fault) (f : Fl4SpecA64.Fault) + (hcap : capA st s (slotIdx (L.curW s) layout.tcb.SLOT_FAULT_HANDLER) = .Null) + (hgc : getCap (Z := Unit) (getTcbFaultHandlerPtr σ.curThread) σ = ({(.nullCap, σ)}, False)) : + ∃ σ2 s2, ∃ x : Tcb, σ.kheap σ.curThread = some (.tcb x) ∧ + σ2 = stsAct (updK σ σ.curThread (.tcb { x with tcbState := .inactive })) σ.curThread ∧ + σ2.schedulerAction ≠ .resumeCurThread ∧ + (σ.schedulerAction = .resumeCurThread → σ2.schedulerAction = .chooseNewThread) ∧ Sched.SwA σ2 ∧ ((), σ2) ∈ (handleFault (Z := Unit) σ.curThread f σ).1 ∧ + faults.handle_fault st ar s a (L.curW s) fe = ok (s2, a) ∧ + ∃ Q Rl, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s2 a σ2 m := by + have htZ : L.curW s ∈ Z.T := J.sr.curT + have ct : σ.curThread = specPtr (L.curW s) := J.j.1.sch.curThread + obtain ⟨x, hkx, -⟩ := J.ep.tcb _ htZ + have hprog := handle_fault_nullProgA (ar := ar) s a (L.curW s) fe hcap + have hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM := J.nvm + have hr : runnable x.tcbState = true := by + rcases J.state hkx with e | e <;> rw [e] <;> rfl + set t := L.curW s with ht_def + have hta : TcbAddr t := J.sr.core.tcbs _ htZ + have hidle : specPtr t ≠ σ.idleThread := by + obtain ⟨y, hy, hs⟩ := J.sinv.idleTcb + intro h + rw [← h, hkx] at hy + cases hy + rw [hs] at hr + exact absurd hr (by decide) + have hRun : SndRunA σ (specPtr t) x := J.sndRun hkx + have hqt := not_queued_codeA J.ep htZ hkx (runnable_codeA hr) + have HB : SchedSRB st Z m κ s σ (specPtr t) := ⟨J.sr.toSchedSRN, fun u y _ hy hc => J.sr.rd u y hy hc⟩ + have body := set_thread_state_blockBA (st := st) (m := m) (κ := κ) J.ipcPlace J.iplaced.e s σ J.ep HB htZ hkx hqt + hRun.2.1 + obtain ⟨s2, eb, E2, ⟨Q, Rl, sr2⟩, w⟩ := exists_of_specA body + set x2 : Tcb := { x with tcbState := .inactive } with hx2 + set σ2 := stsAct (updK σ (specPtr t) (.tcb x2)) (specPtr t) with hσ2 + have hp2 := hprog eb + have P1 := J.faultRpA hA hPl hp2 + have hset : setThreadState (Z := Unit) (specPtr t) .inactive σ = ({((), σ2)}, False) := setThreadState_eq _ hkx + have hspec : ((), σ2) ∈ (handleFault (Z := Unit) σ.curThread f σ).1 := by + rw [ct] + exact handleFault_nullA (σ2 := σ2) hkx (by rw [← ct]; exact hgc) (by rw [hσ2, hx2]) + set M : ObjRef → Prop := fun o => o = specPtr t with hMdef + have K : SIKA σ.idleThread M (fun _ => False) false σ σ2 := + setThreadState_sikA (i := σ.idleThread) (M := M) (N := fun _ => False) (don := false) (specPtr t) rfl .inactive + (by intro h; cases h) σ ((), σ2) (by rw [hset]; exact Set.mem_singleton _) + have kh : ∀ p, p ≠ specPtr t → σ2.kheap p = σ.kheap p := fun p hp => by + rw [hσ2, stsAct_kheap, updK_kheap, Function.update_of_ne hp] + have hk2 : σ2.kheap (specPtr t) = some (.tcb x2) := by rw [hσ2, stsAct_kheap, updK_kheap, Function.update_self] + have HS : SpecSchedA σ σ2 := + { kh := fun p => by + by_cases e : p = specPtr t + · right; left + subst e + exact ⟨x, x2, hkx, hk2, ⟨rfl, rfl, rfl⟩⟩ + · exact Or.inl (kh p e) + irqS := by rw [hσ2]; unfold stsAct; split_ifs <;> rfl + irqN := by rw [hσ2]; unfold stsAct; split_ifs <;> rfl + asid := by rw [hσ2]; unfold stsAct; split_ifs <;> rfl + vcur := by rw [hσ2]; unfold stsAct; split_ifs <;> rfl + cdt := by rw [hσ2]; unfold stsAct; split_ifs <;> rfl + orig := by rw [hσ2]; unfold stsAct; split_ifs <;> rfl } + have SSz : SSzA σ σ2 := (ssz_updK_tcb σ _ _).trans (ssz_stsAct _ _) + have wN : WritesIn st s s2 (SchedStepNWA (st := st) N E Rp Z m σ s) := w.mono fun b h => by + rcases h with h | h + · rw [h]; exact Or.inl (Or.inr (stA_tcbStepWA (fun t ht => J.sr.core.tcbs t ht) htZ)) + · exact Or.inl (Or.inl h) + have wI0 : WritesIn st s s2 (IpcFpA st Z E Rp Bf m σ s) := w.mono fun b h => by + rcases h with h | h + · rw [h]; exact Or.inl (stA_ew J.iplaced.e htZ) + · exact Or.inr h + have wI : WritesIn st s s2 (fun b => IpcFpA st Z E Rp Bf m σ s b ∨ RepWA Rp b) := wI0.mono fun _ => Or.inl + have kept : ∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → + (K.val ≠ 3328 ∧ K.val ≠ 3464 ∧ K.val ≠ 3480 ∧ K.val ≠ 3488 ∧ K.val ≠ 3592) → + L.mem s2 (wa u K) = L.mem s (wa u K) := fun u hu K k1 k2 k3 => by + refine w.mem_eq (field_aligned (J.sr.core.tcbs u hu) k1 k2) ?_ + rintro (c | c) + · exact field_ne (K1 := 3328#u64) (K2 := K) hta (J.sr.core.tcbs u hu) (by simp) k1 (fun _ => by simp; omega) + (by rw [← stA_eq]; exact c.symm) + · exact J.tcbWord_offMwrA hu k1 k3.2 c + have wk : NtfnWaitKeepA Z.T N (L.mem s) (L.mem s2) := fun y hy _ => by + refine ⟨?_, ?_, ?_⟩ + · rw [show Notif.tBlockA y = wa y 3336#u64 from rfl]; exact kept y hy _ (by simp) (by simp) (by simp) + · rw [show prevA .Ep y = wa y 3504#u64 from rfl]; exact kept y hy _ (by simp) (by simp) (by simp) + · rw [show nextA .Ep y = wa y 3496#u64 from rfl]; exact kept y hy _ (by simp) (by simp) (by simp) + have after : ∀ u ∈ Z.T, M (specPtr u) → ∀ y, σ2.kheap (specPtr u) = some (.tcb y) → + epBlocked y.tcbState = none ∧ ∀ p, y.tcbState ≠ .blockedOnNotification p := by + intro u _ hu y hy + rw [show specPtr u = specPtr t from hu, hk2] at hy + cases hy + exact ⟨rfl, fun _ h => by cases h⟩ + have notM : ∀ c < n, ¬ M (m.idleThreadOf c) := by + intro c hc' h + obtain ⟨y, hy, hs⟩ := J.sidle.f3 c hc' + rw [show m.idleThreadOf c = specPtr t from h, hkx] at hy + cases hy + rw [hs] at hr + exact absurd hr (by decide) + have sinv : SInvXA σ2 m n := by + refine K.sinvSw J.sinv J.sidle J.body.sw J.noSelf (fun u hu h => J.swCur u hu h) notM + (fun h => hidle (Eq.symm h)) ?_ (fun _ h => h.elim) (fun _ h => h.elim) + (fun y hy => by rw [show y = specPtr t from hy]; exact hRun.2.1.2) (J.sidle.rdyA J.sinv) + (fun q hq x y hx hy hf => by + rw [show q = specPtr t from hq, hk2] at hy; cases hy; exact Or.inr (Or.inr rfl)) + intro y hy dm pr hm + rw [show y = specPtr t from hy] at hm + exact J.noQ dm pr (ct.symm ▸ hm) + have hMv : ∀ u ∈ Z.T, M (specPtr u) → L.mem s (stA u) ≠ sched.TS_RUNNING_VM := by + intro u _ hu h + rw [specPtr_inj (show specPtr u = specPtr t from hu)] at h + exact hnvm h + have ntfn := J.ntfn_ipcA wI K E2 wk (fun u _ hm y hy p h => by + rw [show specPtr u = specPtr t from hm, hkx] at hy + cases hy + rw [h] at hr + exact absurd hr (by simp [runnable])) (fun u hu hm y hy => (after u hu hm y hy).2) + have glob : σ2.readyQueues = σ.readyQueues ∧ σ2.releaseQueue = σ.releaseQueue := by + rw [hσ2]; unfold stsAct; split_ifs <;> exact ⟨rfl, rfl⟩ + have actEq : σ2.schedulerAction = + if σ.schedulerAction = .resumeCurThread then .chooseNewThread else σ.schedulerAction := by + have hns : ¬ schedulable (specPtr t) (updK σ (specPtr t) (.tcb x2)) = true := fun h => by + have r := (schedulable_partsA h).1 x2 (by rw [updK_kheap, Function.update_self]) + rw [show x2.tcbState = .inactive from rfl] at r + exact absurd r (by simp [runnable]) + rw [hσ2] + unfold stsAct + by_cases c : σ.schedulerAction = .resumeCurThread + · rw [if_pos ⟨ct.symm, c, hns⟩, if_pos c] + · rw [if_neg (fun h => c h.2.1), if_neg c] + rfl + have act2 : σ2.schedulerAction ≠ .resumeCurThread := by + rw [actEq]; split_ifs with c + · intro h; cases h + · exact c + have act3 : σ.schedulerAction = .resumeCurThread → σ2.schedulerAction = .chooseNewThread := fun c => by + rw [actEq, if_pos c] + have sv : SchedValidA σ2 := by + have sc : ∀ p, ScActiveAtA σ p → ScActiveAtA σ2 p := fun p ⟨y, k, e, act⟩ => + ⟨y, k, by rw [kh p (fun h => by rw [h, hkx] at e; cases e)]; exact e, act⟩ + have tcbAct : ∀ u, u ≠ specPtr t → + (∃ y c, σ.kheap u = some (.tcb y) ∧ y.tcbSchedContext = some c ∧ ScActiveAtA σ c) → + ∃ y c, σ2.kheap u = some (.tcb y) ∧ y.tcbSchedContext = some c ∧ ScActiveAtA σ2 c := + fun u hu ⟨y, c, e, es, act⟩ => ⟨y, c, by rw [kh u hu]; exact e, es, sc _ act⟩ + refine ⟨fun dm pr u hm => ?_, fun e1 _ => ?_, fun u x' c e1 e2 e3 => ?_, fun u hu => ?_⟩ + · rw [glob.1] at hm + obtain ⟨act, nr⟩ := J.schedValid.rq dm pr u hm + exact ⟨tcbAct u (fun e => hRun.2.1.1 dm pr (e ▸ hm)) act, by rw [glob.2]; exact nr⟩ + · exact absurd e1 act2 + · by_cases h : u = specPtr t + · rw [h, hk2] at e1 + cases e1 + simp [hx2, BlockedTsA] at e2 + · rw [kh u h] at e1 + exact sc _ (J.schedValid.blk u x' c e1 e2 e3) + · rw [glob.2] at hu + exact tcbAct u (fun e => hRun.2.1.2 (e ▸ hu)) (J.schedValid.rls u hu) + have j7 : J7A st (J7R2A st ar hPl.ipa) Z H OS s2 σ2 m := + J7A.of_rpPostA hA hPl J.toJStage3A P1 (wN.mono fun _ h => Or.inl h) + have userOK : Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s2 := J.sys.ent.user.ksb wN.1 + have swa : SwA σ2 := K.swA J.body.sw (fun u hu h => J.swCur u hu h) (fun c => by cases c) (fun _ h _ => h.elim) + refine ⟨σ2, s2, x, by rw [ct]; exact hkx, by rw [ct], act2, act3, swa, hspec, hp2, Q, Rl, ?_⟩ + exact J.toJStage3A.of_ipcStepA (Q := Q) (Rl := Rl) (curOf := Z.curOf) (curScOf := Z.curScOf) wN HS SSz + ((QKeepA.upd hkx (QObjA.ofTcb (y := x2) rfl)).trans (QKeepA.of_kheap (by rw [hσ2, stsAct_kheap]; rfl) (by rw [hσ2]; exact ⟨by unfold stsAct updK; split_ifs <;> rfl, by unfold stsAct updK; split_ifs <;> rfl, by unfold stsAct updK; split_ifs <;> rfl, by unfold stsAct updK; split_ifs <;> rfl⟩))) + (by rw [hσ2]; unfold stsAct; split_ifs <;> rfl) (fun _ _ => rfl) (BlkOK.bxi P1.2.2.1) P1.1 P1.2.1 userOK j7 sr2 E2 + ntfn (J.reply_ipcA wI0 K) K.ipcKeep (J.tw_ipcA wI) sinv + (K.sidle J.sidle notM (J.sidle.rdyA J.sinv)) (J.runVcpu_ipcA wI K E2 hMv) (J.blkQ_ipcA K E2 wk after) sv + +set_option maxHeartbeats 1000000 in +/-- **A fault with no handler, with the post state** (module docstring): the thread becomes `Inactive` and the +scheduler action `chooseNewThread`; `JSyscallA.faultNullA` is its projection. -/ +theorem JSyscallA.faultNullExA + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (fe : error.Fault) (f : Fl4SpecA64.Fault) + (hcap : capA st s (slotIdx (L.curW s) layout.tcb.SLOT_FAULT_HANDLER) = .Null) + (hgc : getCap (Z := Unit) (getTcbFaultHandlerPtr σ.curThread) σ = ({(.nullCap, σ)}, False)) : + ∃ σ2 s2, ∃ x : Tcb, σ.kheap σ.curThread = some (.tcb x) ∧ + σ2 = stsAct (updK σ σ.curThread (.tcb { x with tcbState := .inactive })) σ.curThread ∧ + σ2.schedulerAction = .chooseNewThread ∧ ((), σ2) ∈ (handleFault (Z := Unit) σ.curThread f σ).1 ∧ + faults.handle_fault st ar s a (L.curW s) fe = ok (s2, a) ∧ + ∃ Q Rl, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s2 a σ2 m := by + obtain ⟨σ2, s2, x, h1', h2', -, h3', -, h4', h5', h6'⟩ := J.toRecvPreA.faultNullExA hA hPl hCs h1 fe f hcap hgc + exact ⟨σ2, s2, x, h1', h2', h3' J.act, h4', h5', h6'⟩ + +/-- **A fault with no handler**: the thread becomes `Inactive` (module docstring). -/ +theorem JSyscallA.faultNullA + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (fe : error.Fault) (f : Fl4SpecA64.Fault) + (hcap : capA st s (slotIdx (L.curW s) layout.tcb.SLOT_FAULT_HANDLER) = .Null) + (hgc : getCap (Z := Unit) (getTcbFaultHandlerPtr σ.curThread) σ = ({(.nullCap, σ)}, False)) : + ∃ σ2 s2, ((), σ2) ∈ (handleFault (Z := Unit) σ.curThread f σ).1 ∧ + faults.handle_fault st ar s a (L.curW s) fe = ok (s2, a) ∧ + Sched.BodyEndA σ σ2 m ∧ + ∃ Q Rl, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s2 a σ2 m := by + obtain ⟨σ2, s2, x, -, e, -, hm, hf, hJ⟩ := J.faultNullExA hA hPl hCs h1 fe f hcap hgc + have hend : Sched.BodyEndA σ σ2 m := by + rw [e] + exact Sched.BodyEndA.ofStopA J (x2 := { x with tcbState := .inactive }) rfl + exact ⟨σ2, s2, hm, hf, hend, hJ⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3FaultNoDonArmA.lean b/proofs/Fl4ProofsA64/P3/Stage3FaultNoDonArmA.lean new file mode 100644 index 000000000..7fb958957 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3FaultNoDonArmA.lean @@ -0,0 +1,333 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2b-2): **`handle_fault` to a waiting receiver that has its own SC** (the fault twin +of `JSyscallA.sendNoDonA`): a user or VM fault (`UsrFaultA`) whose handler endpoint holds a waiting receiver blocked +with a reply object and an SC, with grant or grant-reply. The reply is pushed (the faulted sender blocks on it), +nothing is donated, the receiver keeps its SC and runs, the reply stacks are unchanged. + +* **`JSyscallA.faultNoDonA`**: the non-donating version of `JSyscallA.faultDonateA`. + +[S] (scope selectors) the handler cap and the endpoint's state as in `FaultDonateCaseA`, `hrcv` (the receiver waits +with a reply object and has an SC). +[P] `ArchTotal`, `ArchPlat`, `hCs`, `h1` (`numNodes = 1`), `hmb`, `hmax`, `htu`. New module. +-/ +import Fl4ProofsA64.P3.Stage3FaultArmA +import Fl4ProofsA64.P3.Stage3SendNoDonKeepA +import Fl4ProofsA64.P3.Stage3FaultNoDonKeepA +import Fl4ProofsA64.P3.Stage3SendFaultValidA +import Fl4ProofsA64.P3.Stage3IpcWakeA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {s : S} {a : A} {σ : State Unit} {m : McState} + +set_option maxHeartbeats 2000000 in +/-- **A user or VM fault to a waiting receiver with a reply object and its own SC: a reply push, no donation** (module docstring). -/ +theorem JSyscallA.faultNoDonA + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {fe : error.Fault} {ty w0 w1 w2 : U64} {f : Fl4SpecA64.Fault} (F : UsrFaultA st (L.curW s) fe ty w0 w1 w2 f) + (hOk : ∀ (rbo : Option ObjRef) (x : Tcb), + FaultOKA (L := L) (faultWordsA st s (L.curW s) ty w0 w1 w2) (L.curW s) rbo x f) + {p badge : U64} {sd rv g gr : Bool} + (hcap : capA st s (slotIdx (L.curW s) layout.tcb.SLOT_FAULT_HANDLER) = .Endpoint p badge sd rv g gr) + (hgc : getCap (Z := Unit) (getTcbFaultHandlerPtr σ.curThread) σ = + ({(.endpointCap (specPtr p) badge.bv (rightsOfA sd rv g gr), σ)}, False)) + (hp : p ∈ E) {d : U64} {q : List ObjRef} + (hk : σ.kheap (specPtr p) = some (.endpoint (.recvEP (specPtr d :: q)))) + (hg : g = true ∨ gr = true) + (hrcv : ∀ y, σ.kheap (specPtr d) = some (.tcb y) → + y.tcbSchedContext ≠ none ∧ ∃ e r dd, y.tcbState = .blockedOnReceive e (some r) dd) : + ∃ σ2 s2, ((), σ2) ∈ (handleFault (Z := Unit) σ.curThread f σ).1 ∧ + faults.handle_fault st ar s a (L.curW s) fe = ok (s2, a) ∧ + Sched.WakeEndA σ σ2 ∧ + ∃ Q Rl, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf + stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s2 a σ2 m := by + have htZ : L.curW s ∈ Z.T := J.sr.curT + have ct : σ.curThread = specPtr (L.curW s) := J.j.1.sch.curThread + obtain ⟨x, hkx, -⟩ := J.ep.tcb _ htZ + have P := J.faultPcA F hkx + have hscW : L.mem s (scA (L.curW s)) ≠ 0#u64 := J.cur.2 + have hprog := handle_fault_sendProgA (ar := ar) s a (L.curW s) F hcap hscW + have hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM := J.toJStage3EntryA.curNotVmA + have hr : runnable x.tcbState = true := + (runnable_iffA ((J.ep.at_tcb htZ hkx).st)).1 J.running + set t := L.curW s with ht_def + set s1 := faultWordsA st s t ty w0 w1 w2 with hs1 + set x1 : Tcb := { x with tcbFault := some f } with hx1 + set σ1 := updK σ (specPtr t) (.tcb x1) with hσ1 + have w1' : WritesIn st s s1 (FaultWA t) := faultWords_wrG (st := st) J.sr.core.tcbs htZ s ty w0 w1 w2 + have hta : TcbAddr t := J.sr.core.tcbs _ htZ + have tw : ∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → + (K.val ≠ 3376 ∧ K.val ≠ 3384 ∧ K.val ≠ 3392 ∧ K.val ≠ 3400) → L.mem s1 (wa u K) = L.mem s (wa u K) := + fun u hu K k1 k2 k3 => w1'.mem_eq (field_aligned (J.sr.core.tcbs u hu) k1 k2) + (not_faultWA hta (J.sr.core.tcbs u hu) k1 k3) + have hkt : σ1.kheap (specPtr t) = some (.tcb x1) := by simp [hσ1] + have far : ∀ p, p ≠ specPtr t → σ1.kheap p = σ.kheap p := fun p hp => by simp [hσ1, hp] + have hft : x1.tcbFault = some f := rfl + have ne1 : specPtr p ≠ specPtr t := fun e => by rw [e, hkx] at hk; cases hk + have hk1 : σ1.kheap (specPtr p) = some (.endpoint (.recvEP (specPtr d :: q))) := by rw [far _ ne1]; exact hk + -- the receiver + obtain ⟨hep2, xs, -, hq, hQ, hB⟩ := P.ep.at_ep hp hk1 + have hdx : d ∈ xs := by + have : specPtr d ∈ xs.map specPtr := by rw [← hq]; exact List.mem_cons_self + obtain ⟨y, hy, e⟩ := List.mem_map.1 this + rwa [specPtr_inj e] at hy + have hd : d ∈ Z.T := hQ.mem d hdx + have hdst : (L.mem s1 (stA d)).val = 3 := (hB d hdx).1 + have hdb : L.mem s1 (Notif.tBlockA d) = p := (hB d hdx).2 + have hdb0 : L.mem s (Notif.tBlockA d) = p := by rw [← hdb]; exact (tw d hd 3336#u64 (by simp) (by simp) (by simp)).symm + have hst0 : L.mem s (stA d) = L.mem s1 (stA d) := by + rw [stA_eq]; exact (tw d hd _ (by simp) (by simp) (by simp)).symm + obtain ⟨xd, hkd, rd⟩ := P.ep.tcb d hd + obtain ⟨e', rep, dd, hstd⟩ := tsCode_three (rd.st.symm.trans hdst) + have hnb : tsCode x1.tcbState ≠ 3 := (runnable_codeA (show runnable x1.tcbState = true from hr)).1 + have htd : t ≠ d := fun h => by + rw [← h, hkt] at hkd + cases hkd + exact hnb (by rw [hstd]; rfl) + have hdt : specPtr d ≠ specPtr t := fun h => htd (specPtr_inj h).symm + have hkdσ : σ.kheap (specPtr d) = some (.tcb xd) := by rw [← far _ hdt]; exact hkd + have hc : m.curCore < fl4A64Platform.numNodes := by rw [J.j.1.sch.core]; exact hCs _ J.j.1.sch.cpuCs + have hdth : Z.th d = some .tcb := (P.sr.thT d).2 hd + have hu : ∀ rp, ipcBufOfA true xd = some rp → ∀ k, k < 128 → inUserFrame (bufAddr rp k) σ1 := + fun rp h k hk => inUserFrame_updTcbA hkx _ (J.toJStage3A.bufFrameA hd hkdσ true rp h k hk) + have hrep := J.toJStage3A.repOK_faultA hd hdt hkx (some f) + have hoff := J.toJStage3A.slotOffEwA + have RW := J.toJStage3A.faultRepA htZ ty w0 w1 w2 + have hidle : specPtr t ≠ σ.idleThread ∧ specPtr d ≠ σ.idleThread := by + obtain ⟨y, hy, hs⟩ := J.sinv.idleTcb + refine ⟨fun h => ?_, fun h => ?_⟩ + · rw [← h, hkx] at hy + cases hy + rw [show x.tcbState = .idleThreadState from hs] at hr + exact absurd hr (by decide) + · rw [← h, hkdσ] at hy; cases hy; rw [hstd] at hs; cases hs + -- the receiver's reply object, the sender's SC + obtain ⟨HRCV, er, rr, edd, hsr⟩ := hrcv xd hkdσ + have hrep0 : rep = some rr := by + have h := hstd.symm.trans hsr + injection h + subst hrep0 + have hrcvw := rd.recv _ _ _ hstd + obtain ⟨-, hrr⟩ := optRef_some hrcvw.1 + have hRrp : L.mem s1 (tReplyA d) ∈ Rp := hrcvw.2 (by simp) + set r : U64 := L.mem s1 (tReplyA d) with hr_def + subst hrr + have he' : e' = specPtr p := by + have h := rd.blk e' (by rw [hstd]; rfl) + rw [← h, hdb] + subst he' + obtain ⟨xr, hkr, hback⟩ := hrep xd hkd _ _ _ hstd + have hnbr0 : L.mem s (stA (L.mem s (Endp.rTcbA r))) ≠ sched.TS_BLOCKED_ON_REPLY := by + obtain ⟨yr, hyr, er⟩ := P.ep.reply r hRrp + rw [hkr] at hyr + cases hyr + rw [hback] at er + have eT : L.mem s (Endp.rTcbA r) = d := by + rw [show Endp.rTcbA r = wa r layout.reply.TCB from rfl, + ← RW r hRrp layout.reply.TCB (by simp [layout.reply.TCB]) (by simp [layout.reply.TCB])] + exact specPtr_inj (optRef_some er.symm).2 + rw [eT, hst0] + intro h + rw [h] at hdst + revert hdst + unfold sched.TS_BLOCKED_ON_REPLY + simp + have hri : ReplyIsolatedA (L.mem s1) Z.C stack r := by + obtain ⟨i1, i2, i3⟩ := J.toJStage3A.replyIsolatedA hRrp hnbr0 + exact ⟨i1, (RW r hRrp layout.reply.NEXT (by simp [layout.reply.NEXT]) (by simp [layout.reply.NEXT])).trans i2, + (RW r hRrp layout.reply.PREV (by simp [layout.reply.PREV]) (by simp [layout.reply.PREV])).trans i3⟩ + set sc : U64 := L.mem s1 (scA t) with hsc_def + have hscs : sc = L.mem s (scA t) := by rw [hsc_def, scA_eq]; exact tw t htZ _ (by simp) (by simp) (by simp) + have hsc0 : sc ≠ 0#u64 := by rw [hscs]; exact hscW + have hscC : sc ∈ Z.C := P.sr.rest.scMem t htZ hsc0 + have htSc : x1.tcbSchedContext = some (scNameA m sc) := by + rw [Endp.schedTcbScA P.sr htZ hkt, ← hsc_def, optScA, if_neg hsc0] + rfl + have hDon : DonA σ1 (specPtr t) x1 := by + intro sc' hsc' + obtain ⟨y, k, hy, hb⟩ := P.sr.inv.back (specPtr t) x1 sc' hkt hsc' + refine ⟨fun h => ?_, fun y' k' hy' => by rw [hy] at hy'; cases hy'; exact hb⟩ + obtain ⟨x0, yi, ki, -, -, hyi, hti⟩ := J.sinv.idleSc + rw [h, far _ (fun e => by rw [e, hkx] at hyi; cases hyi), hyi] at hy + cases hy + exact hidle.1 (Option.some.inj (hb.symm.trans hti)) + have hni : NotIdleScA m (scNameA m sc) := by + have h1' : scNameA m sc ≠ idleScPtr := (hDon _ htSc).1 + refine ⟨h1', fun c hc' e => ?_⟩ + have hcc : c = m.curCore := by + have a : c < 1 := h1 ▸ hc' + have b : m.curCore < 1 := h1 ▸ hc + exact (Nat.lt_one_iff.1 a).trans (Nat.lt_one_iff.1 b).symm + rw [hcc] at e + unfold scNameA at e h1' + split_ifs at e h1' with g + · exact h1' rfl + · exact g e + have affE : ∀ u ∈ Z.T, L.mem s1 (affA u) = L.cpu s1 := fun u hu => by + rw [show L.cpu s1 = L.cpu s from w1'.2.1] + have x := hCs _ (P.sr.core.aff u hu) + have y := hCs _ J.j.1.sch.cpuCs + exact UScalar.eq_of_val_eq (by omega) + have hRun : SndRunA σ1 (specPtr t) x1 := + J.sndRunA hCs h1 (show runnable x1.tcbState = true from hr) (fun dm pr hm => by + refine J.sinv.ctQ J.act dm pr ?_ + rw [ct] + exact hm) rfl rfl hidle.1 + have hal := J.toJStage3A.bufAlign0A hd hkdσ true + have hdsc := HRCV + have hrl : specPtr d ∉ σ1.releaseQueue := fun h => by + obtain ⟨y, hy, hr'⟩ := P.sr.run.2 _ h + rw [hkd] at hy + cases hy + rw [hstd] at hr' + cases hr' + have hRBd : RelIfBoundSc σ1 (specPtr d) := P.sr.rd _ _ hkd (Or.inr (Or.inr (Or.inr ⟨_, _, _, hstd⟩))) + have hRel := relIfBoundSc_noDonPushFA (st := st) P.sr.place s1 P.rel P.cv hdth hk1 + (epDeq (.recvEP (specPtr d :: q)) (specPtr d)) hkt hkd hft hal hdsc hrl hRBd badge.bv g (specPtr r) + have pd := send_ipc_recv_pushNoDon_fpA (st := st) (ar := ar) a P.sr J.ipcPlace J.iplaced.e P.ep P.reply + P.sr.place P.rel P.cv P.bufCover P.bufNative hoff htZ hkt hRun hp hk1 hd hkd hu hrep badge g + gr true false (Or.inr ⟨f, hft, hOk _ _⟩) (sc := sc) (r := r) hsc0 hRrp hri rfl hdsc (affE d hd) hni hstd + (Or.inr (by rw [hft]; simp)) hg hRel + obtain ⟨⟨s2, a2⟩, eb, ⟨ha2, σ2, hspecI, E2, ⟨Q, Rl, sr2⟩, rs2, wD⟩⟩ := exists_of_specA pd + simp only at ha2 eb E2 sr2 rs2 wD + subst ha2 + have hmem : ((), σ2) ∈ (sendIpc (Z := Unit) true false badge.bv g gr true (specPtr t) (specPtr p) σ1).1 := by + rw [hspecI]; exact Set.mem_singleton _ + have hp2 := hprog eb + have hxsc : x.tcbSchedContext ≠ none := by + rw [show x.tcbSchedContext = x1.tcbSchedContext from rfl, htSc] + simp + have hspec : ((), σ2) ∈ (handleFault (Z := Unit) σ.curThread f σ).1 := by + rw [ct] + exact handleFault_sendA (σ1 := σ1) hkx hxsc (by rw [← ct]; exact hgc) (by rw [hσ1, hx1]) hmem + -- the spec outcome's record facts + set M : ObjRef → Prop := fun o => o = specPtr t ∨ o = specPtr d with hMdef + set Nq : ObjRef → Prop := fun o => o = specPtr d with hNdef + have K : SIKA σ.idleThread M Nq true σ1 σ2 := + sendIpc_recv_sikFA (M := M) (N := Nq) P.sr.place s1 P.rel P.cv hdth hk1 hkt hkd hal true false + badge.bv g gr true (Or.inr ⟨f, hft⟩) (Or.inl rfl) (Or.inr rfl) rfl hidle.1 hidle.2 hDon + (fun _ => by rw [htSc]; simp) (fun c => by cases c) _ hmem + have hact : ActTcbA σ1 (specPtr t) := by + have h := (P.sv.act J.act (fun c => hidle.1 (ct ▸ c))).1 + obtain ⟨y, sc', ex, es, a'⟩ := h + exact ⟨y, sc', ct ▸ ex, es, a'⟩ + have Qs : QSendA σ1 σ2 := + sendIpc_recv_qsA (i := σ.idleThread) (M := M) (N := Nq) P.sr.place s1 P.rel P.cv hdth hk1 hkt hkd hal + true false badge.bv g gr true (Or.inr ⟨f, hft⟩) (Or.inl rfl) (Or.inr rfl) rfl hidle.1 hidle.2 hDon + (fun _ => by + have h := hact + obtain ⟨y, sc', ex, es, -⟩ := h + rw [hkt] at ex + cases ex + rw [es]; simp) (fun _ => scActive_of_actTcbA hact hkt) + (fun e' r dd h => by + obtain ⟨xr, k, b⟩ := hrep xd hkd e' r dd h + exact ⟨xr, k, by rw [b]; simp⟩) _ hmem + have U2 : UFrA (BufBytesA (ipcBufOfA true xd)) σ1 σ2 := + sendIpc_recv_ufrFA P.sr.place s1 P.rel P.cv hdth hk1 hkt hkd hal true false badge.bv g gr true + (Or.inr ⟨f, hft⟩) _ hmem + have sv : SchedValidA σ2 := + sendIpc_recv_schedValidFA P.sr.place s1 P.rel P.cv hdth hk1 hkt hkd hal true false badge.bv g gr true + (Or.inr ⟨f, hft⟩) hstd + (Or.inr (Or.inr (Or.inr (Or.inl ⟨Or.inr (by rw [hft]; simp), hg, by simp, rfl, hdt.symm, hdsc, hact⟩)))) P.sv + (rqDomPrio_of_schedA P.sr) _ hmem + have run : StIs (specPtr d) .running σ2 := sendIpc_recv_runningA hk1 true false badge.bv g gr true _ hmem + have snt : StIs (specPtr t) (.blockedOnReply (specPtr r)) σ2 := + sendIpc_recv_faultPushA P.sr.place s1 P.rel P.cv hdth hk1 hkt hkd hal true false badge.bv g gr true hft + (M := fun _ => True) trivial hdt.symm hstd hg _ hmem + have after : ∀ u ∈ Z.T, M (specPtr u) → ∀ y, σ2.kheap (specPtr u) = some (.tcb y) → + epBlocked y.tcbState = none ∧ ∀ p, y.tcbState ≠ .blockedOnNotification p := by + intro u _ hu y hy + rcases hu with h | h + · rw [h] at hy + rw [snt y hy] + exact ⟨rfl, fun _ h => by cases h⟩ + · rw [h] at hy + rw [run y hy] + exact ⟨rfl, fun _ h => by cases h⟩ + have wk : NtfnWaitKeepA Z.T N (L.mem s) (L.mem s2) := + J.toJStage3A.ntfnWait_sendXA (W := True) htZ htZ hp hd hdb0 ((w1'.trans wD).mono fun _ h => + h.elim Or.inr fun h => Or.inl (h.imp (J.toJStage3A.sendFp_faultA htZ _ ty w0 w1 w2) + (Or.imp id fun c => ⟨t, htZ, c⟩))) + have wI : WritesIn st s1 s2 (fun b => IpcFpA st Z E Rp Bf m σ1 s1 b ∨ RepWA Rp b) := + wD.mono fun _ h => donFpA_splitA J.sr.core.tcbs htZ hp hd h + have notM : ∀ c < n, ¬ M (m.idleThreadOf c) := by + intro c hc' h + obtain ⟨y, hy, hs⟩ := J.sidle.f3 c hc' + rcases h with h | h + · rw [h, hkx] at hy + cases hy + rw [show x.tcbState = .idleThreadState from hs] at hr + exact absurd hr (by decide) + · rw [h, hkdσ] at hy; cases hy; rw [hstd] at hs; cases hs + have hMv : ∀ u ∈ Z.T, M (specPtr u) → L.mem s (stA u) ≠ sched.TS_RUNNING_VM := by + intro u _ hu h + rcases hu with e | e <;> rw [specPtr_inj e] at h + · exact hnvm h + · rw [hst0] at h; rw [h] at hdst; revert hdst; unfold sched.TS_RUNNING_VM; decide + have hkrσ : σ.kheap (specPtr r) = some (.reply xr) := by + rw [← far _ (fun e => by rw [e, hkt] at hkr; cases hkr)]; exact hkr + have HDR : ∀ q' ∈ Rp, DeadA st OS .reply s q' → ∀ c ∈ Z.C, q' ∉ stack c := + fun q' hq' hdead c hc' hm => J.deadExt.reply q' hq' hdead c hc' hm + have J2 := J.faultTailA hA hPl hCs h1 hkx hp2 wI K (Or.inl rfl) U2.1 U2.2.1 (fun h => QSendA.toBlk h Qs) + (J.toJStage3A.specZero_of_recvBufA hd hkdσ U2.2.2) sr2 E2 (fun _ => rs2) HDR (NtfnWaitKeepA.toM wk) + (fun u _ hu y hy p h => by + rcases hu with e | e <;> rw [e] at hy + · rw [hkx] at hy; cases hy; rw [h] at hr; exact absurd hr (by simp [runnable]) + · rw [hkdσ] at hy; cases hy; rw [hstd] at h; cases h) + (fun u hu hm y hy => (after u hu hm y hy).2) (fun K13 => J.toJStage3A.blkQ_ipcXA K13 E2 wk after) sv notM + (fun h => h.elim (fun h => hidle.1 h.symm) fun h => hidle.2 h.symm) + (fun y hy dm pr hm => by + rcases hy with rfl | rfl + · exact J.sinv.ctQ J.act dm pr (ct ▸ hm) + · obtain ⟨z, hz, hr'⟩ := J.sinv.rq dm pr _ hm + rw [hkdσ] at hz; cases hz; rw [hstd] at hr'; cases hr') + (fun y hy => by + cases hy + exact ⟨fun h => hdt (h.trans ct), hidle.2, fun c hc' h => notM c hc' (Or.inr h), + fun z hz => by rw [run z hz]; rfl⟩) + (fun y hy => by cases hy; exact ⟨xd, hkdσ⟩) + (fun y hy hr' => by + rcases hy with rfl | rfl + · exact hRun.2.1.2 hr' + · obtain ⟨x', hx, hrn⟩ := J.sinv.rlsRun _ hr'; rw [hkdσ] at hx; cases hx; rw [hstd] at hrn; cases hrn) + (fun q hq y hy hf => by + rcases hq with rfl | rfl + · rw [snt y hy]; exact Or.inr (Or.inl ⟨_, rfl⟩) + · obtain ⟨x0, hx0, g⟩ := K.tcbB hy + rw [hkd] at hx0; cases hx0 + exact absurd (g.fault.trans (FaultStOkA.none J.sinv.faultSt hkdσ (by + rw [hstd]; rintro (⟨_, _, h⟩ | ⟨_, h⟩ | h) <;> cases h))) hf) + hMv + have hwake : Sched.WakeEndA σ σ2 := by + have E3 := Sched.sendIpc_recv_endA P.sr.place s1 P.rel P.cv hdth hk1 hkt hkd hal true false badge.bv g gr true + (Or.inr ⟨f, hft⟩) hstd + (Or.inr (Or.inr (Or.inr (Or.inl ⟨Or.inr (by rw [hft]; simp), hg, by simp, rfl, hdt.symm, hdsc, hact⟩)))) + P.sv (rqDomPrio_of_schedA P.sr) J.noSwitch hDon hidle.1 hidle.2 _ hmem + have B : Sched.BLA σ1 σ2 := Sched.sendIpc_recv_blFA P.sr.place s1 P.rel P.cv hdth hk1 hkt hkd hal true false + badge.bv g gr true (Or.inr ⟨f, hft⟩) _ hmem + have B0 : Sched.BLA σ σ1 := Sched.BLA.tcbW hkx + refine ⟨PreO.trans B0 B, E3.1, fun hres => absurd hres (E3.2 ⟨show σ1.curThread = specPtr t from ct, Or.inr (by rw [hft]; simp)⟩)⟩ + exact ⟨σ2, s2, hspec, hp2, hwake, Q, Rl, J2⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3FaultNoDonKeepA.lean b/proofs/Fl4ProofsA64/P3/Stage3FaultNoDonKeepA.lean new file mode 100644 index 000000000..e85cf3258 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3FaultNoDonKeepA.lean @@ -0,0 +1,96 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2b-2): **the receiver's `RelIfBoundSc` after the non-donating reply push of a +faulted sender** (`relIfBoundSc_noDonPushA`'s fault sibling, the body's `hReleased` of +`send_ipc_recv_pushNoDon_fpA` for a fault). + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3SendNoDonKeepA +import Fl4ProofsA64.P3.Stage3SendFaultSpecA +import Fl4ProofsA64.P3.Stage3FaultSpecA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P3 Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Classical + +noncomputable section + +section +open fl4_core_a64 Fl4ProofsA64.P1 +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **The receiver's `RelIfBoundSc` after the non-donating push of a faulted sender** +(`relIfBoundSc_noDonPushA`'s sibling). -/ +theorem relIfBoundSc_noDonPushFA {th : TypedHeapA} {kb : U64} (hPl : PlaceA th kb) (sf : S) {σ : State Unit} + (hR : StateRelA th (L.mem sf) σ) (hV : SlotsValidA th (L.mem sf)) {t d : U64} {e : ObjRef} {z : Endpoint} + (htr : th d = some .tcb) (hk : σ.kheap e = some (.endpoint z)) (z' : Endpoint) + {xt xd : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) (hkd : σ.kheap (specPtr d) = some (.tcb xd)) + {f : Fault} (hf : xt.tcbFault = some f) (hal : ∀ rb, ipcBufOfA true xd = some rb → (bufAddr rb 0).toNat % 8 = 0) + (hdsc : xd.tcbSchedContext ≠ none) (hrl : specPtr d ∉ σ.releaseQueue) + (hRBd : RelIfBoundSc σ (specPtr d)) (badge : Badge) (g : Bool) + (r : ObjRef) : + ∀ σ2 σ3 σ4, + doIpcTransfer (Z := Unit) (specPtr t) (some e) badge g (specPtr d) + (updK σ e (.endpoint z')) = ({((), σ2)}, False) → + replyUnlinkTcb (Z := Unit) (specPtr d) r σ2 = ({((), σ3)}, False) → + replyPush (Z := Unit) (specPtr t) (specPtr d) r true σ3 = ({((), σ4)}, False) → + RelIfBoundSc σ4 (specPtr d) := by + intro σ2 σ3 σ4 hT hU hP + set σ0 := updK σ e (.endpoint z') with hσ0 + have hte : specPtr t ≠ e := fun h => by rw [← h, hkt] at hk; cases hk + have hde : specPtr d ≠ e := fun h => by rw [← h, hkd] at hk; cases hk + have hkt0 : σ0.kheap (specPtr t) = some (.tcb xt) := by rw [hσ0, updK_kheap, Function.update_of_ne hte, hkt] + have hkd0 : σ0.kheap (specPtr d) = some (.tcb xd) := by rw [hσ0, updK_kheap, Function.update_of_ne hde, hkd] + have HS : SpecSchedA σ σ0 := by + refine ⟨fun p => ?_, rfl, rfl, rfl, rfl, rfl, rfl⟩ + by_cases h : p = e + · subst h + exact Or.inr (Or.inr ⟨_, _, hk, by rw [hσ0, updK_kheap, Function.update_self], + Or.inr (Or.inr (Or.inl ⟨_, _, rfl, rfl⟩))⟩) + · exact Or.inl (by rw [hσ0, updK_kheap, Function.update_of_ne h]) + have R0 : RfKA σ σ0 := RfKA.upd fun _ _ h => by cases h + have m2 : ((), σ2) ∈ (doIpcTransfer (Z := Unit) (specPtr t) (some e) badge g (specPtr d) σ0).1 := by + rw [hT]; exact Set.mem_singleton _ + have m3 : ((), σ3) ∈ (replyUnlinkTcb (Z := Unit) (specPtr d) r σ2).1 := by rw [hU]; exact Set.mem_singleton _ + have m4 : ((), σ4) ∈ (replyPush (Z := Unit) (specPtr t) (specPtr d) r true σ3).1 := by + rw [hP]; exact Set.mem_singleton _ + have R2 : RfKA σ0 σ2 := doIpcTransfer_fault_rfA hkt0 hkd0 hf _ _ _ _ m2 + have R3 : RfKA σ2 σ3 := replyUnlinkTcb_rfA _ _ _ _ m3 + have R4 : RfKA σ3 σ4 := replyPush_rfA _ _ _ _ _ _ m4 + have RF : RfKA σ σ4 := PreO.trans R0 (PreO.trans R2 (PreO.trans R3 R4)) + -- the receiver's TCB keeps its SC along the transfer, the unlink and the (non-donating) push + have K2 : SIKA σ.idleThread (fun p => p = specPtr d ∨ p = specPtr t) (fun _ => False) false σ0 σ2 := + doIpcTransfer_any_sikA hPl sf (hR.specSchedA HS) hV htr hkt0 hkd0 hal _ _ _ (Or.inr ⟨f, hf⟩) σ0 rfl _ m2 + have K3 : SIKA σ.idleThread (fun p => p = specPtr d ∨ p = specPtr t) (fun _ => False) false σ2 σ3 := + replyUnlinkTcb_sikA (i := σ.idleThread) (M := fun p => p = specPtr d ∨ p = specPtr t) (N := fun _ => False) + (don := false) (specPtr d) r (Or.inl rfl) _ _ m3 + have K23 := K2.trans K3 + obtain ⟨xt3, hkt3, -⟩ := K23.tcbF hkt0 + obtain ⟨xd3, hkd3, gd⟩ := K23.tcbF hkd0 + have ed : xd3.tcbSchedContext = xd.tcbSchedContext := gd.sc.resolve_right fun c => by cases c.1 + have hP' : replyPush (Z := Unit) (specPtr t) (specPtr d) r false σ3 = ({((), σ4)}, False) := + (replyPush_calleeScA (r := r) hkt3 hkd3 (by rw [ed]; exact hdsc)).symm.trans hP + have m4' : ((), σ4) ∈ (replyPush (Z := Unit) (specPtr t) (specPtr d) r false σ3).1 := by + rw [hP']; exact Set.mem_singleton _ + have K4 : SIKA σ.idleThread (fun p => p = specPtr d ∨ p = specPtr t) (fun _ => False) false σ3 σ4 := + replyPush_noDon_sikA (specPtr t) (specPtr d) r (Or.inr rfl) σ3 _ m4' + have K := K23.trans K4 + obtain ⟨xd4, hkd4, g4⟩ := K.tcbF hkd0 + have e4 : xd4.tcbSchedContext = xd.tcbSchedContext := g4.sc.resolve_right fun c => by cases c.1 + intro x scp y n hx hscp hy ha hnr + have hxd : xd4 = x := KernelObject.tcb.inj (Option.some.inj (hkd4.symm.trans hx)) + subst hxd + have hscp0 : xd.tcbSchedContext = some scp := e4.symm.trans hscp + obtain ⟨y0, hy0, er, em⟩ := RF.2.2 _ _ _ hy + have a0 : scActive y0 = true := by unfold scActive at ha ⊢; rw [← em]; exact ha + have rel := hRBd xd scp y0 n hkd hscp0 hy0 a0 hrl + unfold releasedSc at rel ⊢ + have eh : refillHd y = refillHd y0 := by unfold refillHd; rw [er] + rw [RF.1, eh] + exact rel + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3FaultSetA.lean b/proofs/Fl4ProofsA64/P3/Stage3FaultSetA.lean new file mode 100644 index 000000000..fb0dcab7c --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3FaultSetA.lean @@ -0,0 +1,501 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2b, item 2): **`set_fault` of a user or VM fault, at the record**. + +* `faultWordsA`, `setUserFault_exactA`, `setVmFault_exactA`: `set_fault` of a `UserException` or `VmFault` is the four + fault words (as `timeoutFaultWordsA`). +* `UsrFaultA`: a fault stored in those words (not the null or timeout type) and its spec fault. +* `SchedSRA.setUsrFaultA`, `EpStateRel.setUsrFaultA`: the scheduler and IPC relations across the write. +* `S1aStepFix.ofRpPost`, `J7A.of_rpPostA`: J7 across a body that has P1's `RpPost` (the fault IPC), from the body's + write set. +* **`FaultPcA`**, **`JSyscallA.faultPcA`**: the relations the fault send reads at the faulted state (Option A: no + `JStage3A` is claimed there, `SInvXA.faultSt` fails for the running faulted thread). + +[P] `ArchTotal`, `ArchPlat` (`faultRpA`). New module. +-/ +import Fl4ProofsA64.P3.Stage3TimeoutSendBA +import Fl4ProofsA64.P3.Stage3SendDonateArmA +import Fl4ProofsA64.P3.Stage3FinA +import Fl4ProofsA64.P3.DelSinvLoopA +import Fl4ProofsA64.P1.S1aCFault +import Fl4ProofsA64.P1.S1aStepFix +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P1.W7.R.ST +open fl4_core_a64 Fl4ProofsA64.P0.Consts Fl4Proofs.P3 + +variable {S : Type _} {st : machine.Store S} [L : LawfulStore st] {H : Heap} + {Cs OS Vc Ub Ac Pt : Set U64} {ia ie : Nat} {A : Type _} {ar : machine.Arch A} + +/-- **A Reply-walk post gives the S1a step at fixed witnesses** (`S1aStepR2K.ofRpPost` with `S1aStepFix.ofXfer2`). -/ +theorem S1aStepFix.ofRpPost {K : capability.Cap → Prop} {Ty : S → Prop} {Uq Pp Tb Hr Te Kt : Set U64} + {Lv Re bits KLv : U64 → Nat} {n : U64} {s s' : S} + (I : DelIn st ar H Cs OS Vc Uq Pt K Ty s) (P : RpPost st H Cs OS Vc ia ie Ac s s') (fu : FrUb st Ub OS s) + (C : S1aArmCol st H Cs OS Vc Ub s) (Df : DevFrAl st OS s) + (vr : VrOK H.omega H.frame H.scs H.scBits OS Vc Ub (L.ksb s) Pt) (vcc : VcCap st Vc OS s) + (R : S1aR2 st H OS Pt Pp Tb Hr Te Kt Lv Re bits n s) (sep : S1aKtUt st OS Kt s) (KC : S1aHiK st OS Kt KLv s) : + S1aStepFix st H Cs OS Vc Ub Pt Kt KLv ia ie n s' := + S1aStepFix.ofXfer2 ((fun h => ⟨h.1, h.2, P.2.2.2.2.1.1, P.2.2.2.2.2⟩) + (ArmQIAt.ofXC (Vc := Vc) ⟨P.1, P.2.1⟩ ⟨Ac, P.2.2.1⟩ P.2.2.2.1 P.2.2.2.2.1.1.toVs)) vr fu vcc C Df R sep KC + +end Fl4ProofsA64.P1.W7.R.ST + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +variable (st) in +/-- The four stores of `faults.set_fault` for a fault without a lookup failure. -/ +def faultWordsA (s : S) (t ty w0 w1 w2 : U64) : S := + L.wr (L.wr (L.wr (L.wr s (faultA t) ty) (tFw0A t) w0) (tFw1A t) w1) (tFw2A t) w2 + +theorem setUserFault_exactA (s : S) (t number code : U64) : + faults.set_fault st s t (.UserException number code) = + ok (faultWordsA st s t fl4_abi.consts.fault.UserException number code 0#u64) := by + unfold faults.set_fault faultWordsA + simp only [bind_ok, uncurry_apply_pair] + rw [eq_of_spec_fd (P1.obj.Tcb.set_fault_type.spec (st := st) s t _), bind_ok] + rw [eq_of_spec_fd (P1.obj.Tcb.set_fault_w0.wr (st := st) _ t number), bind_ok] + rw [eq_of_spec_fd (P1.obj.Tcb.set_fault_w1.wr (st := st) _ t code), bind_ok] + exact eq_of_spec_fd (P1.obj.Tcb.set_fault_w2.wr _ t 0#u64) + +theorem setVmFault_exactA (s : S) (t addr fsr : U64) (instr : Bool) : + faults.set_fault st s t (.VmFault addr fsr instr) = + ok (faultWordsA st s t fl4_abi.consts.fault.VMFault addr fsr (if instr then 1#u64 else 0#u64)) := by + unfold faults.set_fault faultWordsA + cases instr <;> + · simp only [Bool.false_eq_true, ↓reduceIte, bind_ok, uncurry_apply_pair] + rw [eq_of_spec_fd (P1.obj.Tcb.set_fault_type.spec (st := st) s t _), bind_ok] + rw [eq_of_spec_fd (P1.obj.Tcb.set_fault_w0.wr (st := st) _ t addr), bind_ok] + rw [eq_of_spec_fd (P1.obj.Tcb.set_fault_w1.wr (st := st) _ t fsr), bind_ok] + exact eq_of_spec_fd (P1.obj.Tcb.set_fault_w2.wr _ t _) + +/-- **A fault `fe` of the program stored in the four fault words**, and the spec's fault `f`: neither the null nor +the timeout type. -/ +structure UsrFaultA (st : machine.Store S) [L : LawfulStore st] (t : U64) (fe : error.Fault) (ty w0 w1 w2 : U64) + (f : Fault) : Prop where + exact : ∀ s : S, faults.set_fault st s t fe = ok (faultWordsA st s t ty w0 w1 w2) + ty0 : ty ≠ 0#u64 + ty5 : ty ≠ 5#u64 + tm : ∀ b, f ≠ .timeout b + rel : ∀ lt l0 l1, FaultRel' ty w0 w1 w2 lt l0 l1 (some f) + +theorem usrFault_userA (t number code : U64) : + UsrFaultA st t (.UserException number code) fl4_abi.consts.fault.UserException number code 0#u64 + (.userException number.bv code.bv) := by + have e : (fl4_abi.consts.fault.UserException : U64) = 3#u64 := by unfold fl4_abi.consts.fault.UserException; rfl + refine ⟨fun s => setUserFault_exactA s t number code, ?_, ?_, fun _ h => by (cases h), fun _ _ _ => ?_⟩ + · rw [e]; decide + · rw [e]; decide + · exact ⟨e, rfl, rfl⟩ + +theorem setUnknownFault_exactA (s : S) (t number : U64) : + faults.set_fault st s t (.UnknownSyscall number) = + ok (faultWordsA st s t fl4_abi.consts.fault.UnknownSyscall number 0#u64 0#u64) := by + unfold faults.set_fault faultWordsA + simp only [bind_ok, uncurry_apply_pair] + rw [eq_of_spec_fd (P1.obj.Tcb.set_fault_type.spec (st := st) s t _), bind_ok] + rw [eq_of_spec_fd (P1.obj.Tcb.set_fault_w0.wr (st := st) _ t number), bind_ok] + rw [eq_of_spec_fd (P1.obj.Tcb.set_fault_w1.wr (st := st) _ t 0#u64), bind_ok] + exact eq_of_spec_fd (P1.obj.Tcb.set_fault_w2.wr _ t 0#u64) + +/-- **An unknown-syscall fault** (type 2, word 0 = the syscall number). Unlike a user or VM fault, its transfer needs +`FaultOKA` (`rbo = none ∨ TRAP_SYSCALLS clear`), a fact about the state: it is a premise of the arms (`hOk`). -/ +theorem usrFault_unkA (t number : U64) : + UsrFaultA st t (.UnknownSyscall number) fl4_abi.consts.fault.UnknownSyscall number 0#u64 0#u64 + (.unknownSyscallException number.bv) := by + have e : (fl4_abi.consts.fault.UnknownSyscall : U64) = 2#u64 := by + unfold fl4_abi.consts.fault.UnknownSyscall; rfl + refine ⟨fun s => setUnknownFault_exactA s t number, ?_, ?_, fun _ h => by (cases h), fun _ _ _ => ?_⟩ + · rw [e]; decide + · rw [e]; decide + · exact ⟨e, rfl⟩ + +theorem usrFault_vmA (t addr esr : U64) : + UsrFaultA st t (.VmFault addr esr (decide (((esr.bv >>> 26) &&& 63) = 32))) fl4_abi.consts.fault.VMFault addr esr + (if decide (((esr.bv >>> 26) &&& 63) = 32) then 1#u64 else 0#u64) (fl4VmFault addr.bv esr.bv) := by + have e : (fl4_abi.consts.fault.VMFault : U64) = 6#u64 := by unfold fl4_abi.consts.fault.VMFault; rfl + refine ⟨fun s => setVmFault_exactA s t addr esr _, ?_, ?_, fun _ h => by (cases h), fun _ _ _ => ?_⟩ + · rw [e]; decide + · rw [e]; decide + · refine ⟨e, rfl, ?_⟩ + have k : ∀ (c : Prop) [Decidable c], (if c then (1#u64 : U64) else 0#u64).bv = (if c then (1#64) else 0#64) := + fun c _ => by split_ifs <;> rfl + simp only [decide_eq_true_eq, k] + rfl + +theorem faultWords_wrG {T : Set U64} (hP : TcbPlaced T) {t : U64} (ht : t ∈ T) (s : S) (ty w0 w1 w2 : U64) : + WritesIn st s (faultWordsA st s t ty w0 w1 w2) (FaultWA t) := by + have hta := hP t ht + unfold faultWordsA + refine (((writesIn_wr (a_fault hta).1).trans (writesIn_wr (a_fw0 hta).1)).trans + ((writesIn_wr (a_fw1 hta).1).trans (writesIn_wr (a_fw2 hta).1))).mono fun b hb => ?_ + rcases hb with (hb | hb) | (hb | hb) + · exact Or.inl hb + · exact Or.inr (Or.inl hb) + · exact Or.inr (Or.inr (Or.inl hb)) + · exact Or.inr (Or.inr (Or.inr hb)) + +/-- `set_fault` of the running thread keeps `SchedSRA`. -/ +theorem SchedSRA.setUsrFaultA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} {t : U64} + (H : SchedSRA st Z m κ s σ) (ht : t ∈ Z.T) {x : Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hr : runnable x.tcbState = true) (ty w0 w1 w2 : U64) (f : Option Fault) : + SchedSRA st Z m κ (faultWordsA st s t ty w0 w1 w2) (updK σ (specPtr t) (.tcb { x with tcbFault := f })) := by + have HB : SchedSRB st Z m κ s σ (specPtr t) := ⟨H.toSchedSRN, fun u y _ hy hc => H.rd u y hy hc⟩ + obtain ⟨hS, hrun, hrd⟩ := faultStepExA hk f HB.run HB.rdEx + have H' := HB.stableExBA (faultWords_wrG (st := st) HB.core.tcbs ht s ty w0 w1 w2) + (fun _ hb => faultOffReadA H.toSchedSRN ht hb) hS hrun hrd + refine ⟨H'.toSchedSRN, fun u y hy hc => ?_⟩ + by_cases e : u = specPtr t + · subst e + exact (H.rd _ x hk (Or.inl hr)).step hS hk + · exact H'.rdEx u y e hy hc + +/-- `set_fault` of a non-timeout, non-null fault moves `EpStateRel` to the faulted TCB. -/ +theorem EpStateRel.setUsrFaultA {T E R : Set U64} {Bf : Set (U64 × Word)} {s : S} {σ : State Unit} + (hR : EpStateRel T E R Bf (L.mem s) σ) (hP : EPlaced T E R Bf) {t : U64} (ht : t ∈ T) {x : Tcb} + (hk : σ.kheap (specPtr t) = some (.tcb x)) {fe : error.Fault} {ty w0 w1 w2 : U64} {f : Fault} + (F : UsrFaultA st t fe ty w0 w1 w2 f) : + EpStateRel T E R Bf (L.mem (faultWordsA st s t ty w0 w1 w2)) + (updK σ (specPtr t) (.tcb { x with tcbFault := some f })) := by + have hta := hP.tcb t ht + have w := faultWords_wrG (st := st) hP.tcb ht s ty w0 w1 w2 + have fw : ∀ b, FaultWA t b → TF t b := by + rintro b (rfl | rfl | rfl | rfl) + · exact (a_fault hta).2 + · exact (a_fw0 hta).2 + · exact (a_fw1 hta).2 + · exact (a_fw2 hta).2 + have keep : ∀ {K : U64}, K.val < 4096 → K.val % 8 = 0 → + (K.val ≠ 3376 ∧ K.val ≠ 3384 ∧ K.val ≠ 3392 ∧ K.val ≠ 3400) → + L.mem (faultWordsA st s t ty w0 w1 w2) (wa t K) = L.mem s (wa t K) := + fun h1 h2 h3 => w.mem_eq (field_aligned hta h1 h2) (not_faultWA hta hta h1 h3) + have hv : ∀ K : U64, K.val < 4096 → (wa t K).val = t.val + K.val := fun K hK => field_val hta hK + have v0 : L.mem (faultWordsA st s t ty w0 w1 w2) (tFaultA t) = ty := by + unfold faultWordsA + rw [show faultA t = tFaultA t from rfl] + rw [L.mem_wr_ne (a_fw2 hta).1 (a_fault hta).1 (by unfold tFw2A tFaultA; rw [hv _ (by simp), hv _ (by simp)]; simp), + L.mem_wr_ne (a_fw1 hta).1 (a_fault hta).1 (by unfold tFw1A tFaultA; rw [hv _ (by simp), hv _ (by simp)]; simp), + L.mem_wr_ne (a_fw0 hta).1 (a_fault hta).1 (by unfold tFw0A tFaultA; rw [hv _ (by simp), hv _ (by simp)]; simp), + L.mem_wr_same (a_fault hta).1] + have v1 : L.mem (faultWordsA st s t ty w0 w1 w2) (tFw0A t) = w0 := by + unfold faultWordsA + rw [L.mem_wr_ne (a_fw2 hta).1 (a_fw0 hta).1 (by unfold tFw2A tFw0A; rw [hv _ (by simp), hv _ (by simp)]; simp), + L.mem_wr_ne (a_fw1 hta).1 (a_fw0 hta).1 (by unfold tFw1A tFw0A; rw [hv _ (by simp), hv _ (by simp)]; simp), + L.mem_wr_same (a_fw0 hta).1] + have v2 : L.mem (faultWordsA st s t ty w0 w1 w2) (tFw1A t) = w1 := by + unfold faultWordsA + rw [L.mem_wr_ne (a_fw2 hta).1 (a_fw1 hta).1 (by unfold tFw2A tFw1A; rw [hv _ (by simp), hv _ (by simp)]; simp), + L.mem_wr_same (a_fw1 hta).1] + have v3 : L.mem (faultWordsA st s t ty w0 w1 w2) (tFw2A t) = w2 := by + unfold faultWordsA + rw [L.mem_wr_same (a_fw2 hta).1] + have hxt := hR.at_tcb ht hk + have hx : TcbRelE R (L.mem (faultWordsA st s t ty w0 w1 w2)) t { x with tcbFault := some f } := by + refine ⟨?_, fun e h => ?_, fun e d h => ?_, fun e r d h => ?_, fun r h => ?_, fun r => ?_, ?_, ?_, ?_, ?_, ?_⟩ + · rw [stA_eq, keep (K := 3328#u64) (by simp) (by simp) (by simp), ← stA_eq]; exact hxt.st + · rw [tBlockA, keep (K := 3336#u64) (by simp) (by simp) (by simp)]; exact hxt.blk e h + · rw [tBadgeA, tFlagsA, keep (K := 3344#u64) (by simp) (by simp) (by simp), + keep (K := 3352#u64) (by simp) (by simp) (by simp)] + exact hxt.send e d h + · rw [tReplyA, keep (K := 3360#u64) (by simp) (by simp) (by simp)]; exact hxt.recv e r d h + · rw [tReplyA, keep (K := 3360#u64) (by simp) (by simp) (by simp)]; exact hxt.reply r h + · rw [w.mem_eq (a_reg hta r).1 fun hb => ?_] + · exact hxt.regs r + · obtain ⟨K, hK, e⟩ := faultWA_eq hb + have h1 := regA_val hta (regIdxA_lt r) + have h2 := regIdxA_lt r + rw [e, hv K (by rcases hK with rfl | rfl | rfl | rfl <;> simp)] at h1 + rcases hK with rfl | rfl | rfl | rfl <;> simp at h1 <;> omega + · rw [prioA, keep (K := 3448#u64) (by simp) (by simp) (by simp)]; exact hxt.prio + · rw [v0]; exact ⟨fun h => absurd h F.ty0, fun h => by cases h⟩ + · rw [v0]; exact ⟨fun h => absurd h F.ty5, fun ⟨b, h⟩ => absurd (Option.some.inj h) (F.tm b)⟩ + · rw [tBoundA, keep (K := 3368#u64) (by simp) (by simp) (by simp)]; exact hxt.bound + · unfold FaultRel + rw [v0, v1, v2, v3] + exact F.rel _ _ _ + refine hR.upd_tcb hP ht _ (fun b hb hn => w.mem_eq hb fun h => hn.2 (fw b h)) (Or.inr ⟨?_, ?_⟩) hx + · rw [stA_eq]; exact keep (K := 3328#u64) (by simp) (by simp) (by simp) + · rw [tBlockA]; exact keep (K := 3336#u64) (by simp) (by simp) (by simp) + +end + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {s s' : S} {a : A} {σ σ' : State Unit} {m m' : McState} + +/-- **J7 across a body with P1's Reply-walk post**, from its write set (module docstring). -/ +theorem J7A.of_rpPostA (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (P : RpPost st H Z.Cs OS Z.V ia ie Ac s s') + (w : WritesIn st s s' (fun b => SchedStepNWA (st := st) N E Rp Z m σ s b ∨ BufW Bf b)) : + J7A st (J7R2A st ar hPl.ipa) Z H OS s' σ' m' := by + obtain ⟨Pt, Ub, Pp7, Tb7, Hr, Te, Kt, Lv7, Re, bits, KLv, W⟩ := J7A.unp J.j7 + have I := J.delIn hA hPl W + have Fx := S1aStepFix.ofRpPost I P W.fu W.col W.df W.vr W.vcc W.r2 W.sep W.hiK + have kb : L.ksb s' = L.ksb s := w.1 + have bks : ∀ b, (L.ksb s).val ≤ b.val → b.val < (L.ksb s).val + 788736 → ¬ BufW Bf b := fun b h1 h2 hb => by + have hksz : ksSizeA = 788736 := rfl + have := J.buf_ksA hb + omega + have osk : OSKeep st OS s s' := fun y hy => by + have hfit := J.sys.ent.os.fit y hy + have key : ∀ o : U64, o.val < 32 → o.val % 8 = 0 → L.mem s' (wa y o) = L.mem s (wa y o) := by + intro o o1 o2 + have hv : (wa y o).val = y.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have hal : Aligned (wa y o) := J.sys.ent.os.al hy o1 o2 + have hin : InOS OS (wa y o) := ⟨y, hy, by omega, by omega⟩ + exact w.mem_eq hal (fun h => h.elim (J.os_offStepNA hal hin) (fun hb => J.bufOff.os _ hb hin)) + have e0 : wa y 0#u64 = y := by apply UScalar.eq_of_val_eq; rw [wa_valm]; simp + exact ⟨by have := key 0#u64 (by simp) (by simp); rwa [e0] at this, key 8#u64 (by simp) (by simp), + key 16#u64 (by simp) (by simp), key 24#u64 (by simp) (by simp)⟩ + have tab : ∀ j, L.mem s' (wa (L.ksb s') (AtOff j)) = L.mem s (wa (L.ksb s) (AtOff j)) := fun j => by + have v := AtOff.v j + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.sys.ent.ksw + have ev : (wa (L.ksb s) (AtOff j)).val = (L.ksb s).val + (AtOff j).val := by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + rw [kb] + refine w.mem_eq ?_ (fun h => h.elim (J.asid_offStepNA (by omega) (by omega)) (bks _ (by omega) (by omega))) + have h32 := J.j.1.ksbAl + show (wa (L.ksb s) (AtOff j)).val % 8 = 0 + omega + have pn : PoolNamed st OS s' := PoolNamed.ofTab kb tab osk W.pn + have hsp : ∀ p, SchedPageA st Z OS s' p → p ∈ Pt := by + obtain ⟨_, -, -, -, ⟨_, _, _, _, _, _, _, R'⟩, -⟩ := id Fx + exact schedPage_ptA R' W.sp + exact J7A.of_stepFixedA Fx W.vr kb W.al W.vu pn hsp + (fun b hb o ho => by rw [kb]; exact W.ks b hb o ho) (J7R2K.of_stepFixedA (σ' := σ') (m := m') Fx W.ktK) + +end + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {s : S} {a : A} {σ : State Unit} {m : McState} + +/-- The all-zero extra caps of a fault entry. -/ +theorem faultExtraA {OS : Set U64} : + ∀ y ∈ (Array.repeat 3#usize (0#u64 : obj.Slot)).val, y ≠ 0#u64 → y ∈ OS := fun y hy h => by + simp [Array.repeat] at hy + exact absurd hy h + +/-- The current thread's fault handler slot is an `OS` slot. -/ +theorem JSyscallA.faultSlotA (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) : + slotIdx (L.curW s) layout.tcb.SLOT_FAULT_HANDLER ∈ OS := by + obtain ⟨Pt, Ub, Pp7, Tb7, Hr, Te, Kt, Lv7, Re, bits, KLv, W⟩ := J7A.unp J.j7 + have I := J.toJStage3A.delIn hA hPl W + have htH : L.curW s ∈ H.tcbs := by rw [J.tcbs]; exact J.sr.curT + exact I.ts _ htH _ (by simp [layout.tcb.SLOT_FAULT_HANDLER]) + +/-- P1's placement with the current thread's fault handler slot as the invoked slot. -/ +theorem JSyscallA.faultPlaceA (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) : + SysPlace st H Z.Cs H.frame H.replies H.ntfns OS s (Array.repeat 3#usize (0#u64 : obj.Slot)) + (slotIdx (L.curW s) layout.tcb.SLOT_FAULT_HANDLER) := by + have hx := J.faultSlotA hA hPl + have e0 : wa (slotIdx (L.curW s) layout.tcb.SLOT_FAULT_HANDLER) 0#u64 = + slotIdx (L.curW s) layout.tcb.SLOT_FAULT_HANDLER := by + apply UScalar.eq_of_val_eq; rw [wa_valm]; simp + refine ⟨J.sys.ent.heap, J.sys.ent.os, J.toJStage3A.winOK, J.toJStage3A.slotTy, J.toJStage3A.core0, faultExtraA, hx, + ⟨?_, fun o o1 o2 => ?_⟩, J.toJStage3A.wuApart⟩ + · have := J.sys.ent.os.al hx (k := 0#u64) (by simp) (by simp) + rwa [e0] at this + · have hfit := J.sys.ent.os.fit _ hx + have hv : (wa (slotIdx (L.curW s) layout.tcb.SLOT_FAULT_HANDLER) o).val = + (slotIdx (L.curW s) layout.tcb.SLOT_FAULT_HANDLER).val + o.val := by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have hta := J.sr.core.tcbs _ J.sr.curT + have hf := hta.fits + have es : (slotIdx (L.curW s) layout.tcb.SLOT_FAULT_HANDLER).val = (L.curW s).val + 96 := by + unfold slotIdx layout.tcb.SLOT_FAULT_HANDLER + rw [show core.num.U64.wrapping_mul 3#u64 32#u64 = 96#u64 from rfl, wa_valm] + simp + omega + have ek : (wa 96#u64 o).val = 96 + o.val := by + rw [wa_valm] + simp + omega + refine Or.inl ⟨L.curW s, by rw [J.tcbs]; exact J.sr.curT, wa 96#u64 o, by omega, ?_⟩ + apply UScalar.eq_of_val_eq + rw [hv, es, field_val hta (by omega : (wa 96#u64 o).val < 4096), ek] + omega + +/-- **P1's post of `handle_fault`** at a syscall-entry record (`faults.handle_fault.rpw`). -/ +theorem JSyscallA.faultRpA (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + {fe : error.Fault} {s' : S} {a' : A} + (hp : faults.handle_fault st ar s a (L.curW s) fe = ok (s', a')) : + RpPost st H Z.Cs OS Z.V ia ie Ac s s' := by + obtain ⟨Pt, Ub, Pp7, Tb7, Hr, Te, Kt, Lv7, Re, bits, KLv, W⟩ := J7A.unp J.j7 + have I := J.toJStage3A.delIn hA hPl W + have D := J.decPreA (J.faultSlotA hA hPl) (extra := Array.repeat 3#usize (0#u64 : obj.Slot)) faultExtraA + have hrun : L.mem s (stA (L.curW s)) ≠ sched.TS_INACTIVE := by + rcases J.running with h | h | h <;> rw [h] <;> unfold sched.TS_INACTIVE <;> + first + | (unfold sched.TS_RUNNING; decide) + | (unfold sched.TS_RESTART; decide) + | (unfold sched.TS_RUNNING_VM; decide) + have hRV : RunVcpuInv st H.tcbs s := by rw [J.tcbs]; exact J.runVcpu + have p := faults.handle_fault.rpw (st := st) (ar := ar) hA a (L.curW s) fe (J.faultPlaceA hA hPl) + (RpW.start (Vc := Z.V) J.sys.sysl J.symL (BXI.blk J.sys.ent.bxi)) I.del.sca I.ts J.ko I.del.vr D.cb hrun hRV + have h := post_of_okA p hp + simpa only [WP.uncurry'_pair] using h + +variable (st) in +/-- (Option A, lead ruling 2026-12-04) **The relations the fault send's bodies read at the faulted state.** `set_fault` +leaves the running current thread with a fault, so `SInvXA.faultSt` fails there and no `JStage3A` is claimed at it: +the arms compose the fault write with `send_ipc` from the entry state (`P3/Stage3FaultTailA`). -/ +structure FaultPcA (Z : XSetsA) (E Rp : Set U64) (Bf : Set (U64 × Fl4SpecA64.Word)) (stack : U64 → List U64) + (κ : U64 → U64) (m : McState) (s1 : S) (σ1 : State Unit) : Prop where + sr : SchedSRA st Z m κ s1 σ1 + ep : EpStateRel Z.T E Rp Bf (L.mem s1) σ1 + reply : ReplyStackRelA (L.mem s1) Rp Z.C σ1 (scNameA m) stack + rel : StateRelA Z.th (L.mem s1) σ1 + cv : SlotsValidA Z.th (L.mem s1) + bufCover : BufCover st Z.T Bf s1 σ1 + bufNative : BufNativeA Z.T (L.mem s1) + sv : SchedValidA σ1 + +set_option maxHeartbeats 2000000 in +/-- **`FaultPcA` across `set_fault` of the current thread** (a user or VM fault). -/ +theorem JSyscallA.faultPcA {nA : U64} + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {fe : error.Fault} {ty w0 w1 w2 : U64} {f : Fault} (F : UsrFaultA st (L.curW s) fe ty w0 w1 w2 f) + {x : Tcb} (hk : σ.kheap (specPtr (L.curW s)) = some (.tcb x)) : + FaultPcA st Z E Rp Bf stack κ m (faultWordsA st s (L.curW s) ty w0 w1 w2) + (updK σ (specPtr (L.curW s)) (.tcb { x with tcbFault := some f })) := by + set t := L.curW s with ht_def + set s1 := faultWordsA st s t ty w0 w1 w2 with hs1 + set x1 : Tcb := { x with tcbFault := some f } with hx1 + set σ1 := updK σ (specPtr t) (.tcb x1) with hσ1 + have htZ : t ∈ Z.T := J.sr.curT + have hta : TcbAddr t := J.sr.core.tcbs _ htZ + have states : StEqA Z.T (L.mem s) σ := fun u hu y hy => (J.ep.at_tcb hu hy).st + have hr : runnable x.tcbState = true := (runnable_iffA (states _ htZ x hk)).1 J.running + have w1' : WritesIn st s s1 (FaultWA t) := faultWords_wrG (st := st) J.sr.core.tcbs htZ s ty w0 w1 w2 + have wSS : WritesIn st s s1 (SchedStepWA (st := st) Z m σ s) := w1'.mono fun b hb => by + obtain ⟨K, hK, rfl⟩ := faultWA_eq hb + refine Or.inr (tcbStepWA_of_off J.sr.core.tcbs htZ K.val ?_ ?_ ?_ ?_ + (field_val hta (by rcases hK with rfl | rfl | rfl | rfl <;> simp))) <;> + rcases hK with rfl | rfl | rfl | rfl <;> simp + have tw : ∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → + (K.val ≠ 3376 ∧ K.val ≠ 3384 ∧ K.val ≠ 3392 ∧ K.val ≠ 3400) → L.mem s1 (wa u K) = L.mem s (wa u K) := + fun u hu K h1 h2 h3 => w1'.mem_eq (field_aligned (J.sr.core.tcbs u hu) h1 h2) + (not_faultWA hta (J.sr.core.tcbs u hu) h1 h3) + have far : ∀ p, p ≠ specPtr t → σ1.kheap p = σ.kheap p := fun p hp => by simp [hσ1, hp] + have hk1 : σ1.kheap (specPtr t) = some (.tcb x1) := by simp [hσ1] + have kT : ∀ p y, σ.kheap p = some (.tcb y) → ∃ y1, σ1.kheap p = some (.tcb y1) ∧ y1.tcbState = y.tcbState ∧ + y1.tcbSchedContext = y.tcbSchedContext ∧ y1.tcbIpcBuffer = y.tcbIpcBuffer ∧ y1.tcbIpcframe = y.tcbIpcframe := by + intro p y hy + by_cases e : p = specPtr t + · subst e + rw [hk] at hy + cases hy + exact ⟨x1, hk1, rfl, rfl, rfl, rfl⟩ + · exact ⟨y, (far p e).trans hy, rfl, rfl, rfl, rfl⟩ + have kB : ∀ p y1, σ1.kheap p = some (.tcb y1) → ∃ y, σ.kheap p = some (.tcb y) ∧ y1.tcbState = y.tcbState ∧ + y1.tcbSchedContext = y.tcbSchedContext := by + intro p y1 hy + by_cases e : p = specPtr t + · subst e + rw [hk1] at hy + cases hy + exact ⟨x, hk, rfl, rfl⟩ + · exact ⟨y1, (far p e).symm.trans hy, rfl, rfl⟩ + have keepO : ∀ p o, σ.kheap p = some o → (∀ y, o ≠ .tcb y) → σ1.kheap p = some o := fun p o h ho => by + rw [far p (fun e => by rw [e, hk] at h; exact ho x (Option.some.inj h).symm)]; exact h + have backO : ∀ p o, σ1.kheap p = some o → (∀ y, o ≠ .tcb y) → σ.kheap p = some o := fun p o h ho => by + rwa [far p (fun e => by rw [e, hk1] at h; exact ho x1 (Option.some.inj h).symm)] at h + have reply : ReplyStackRelA (L.mem s1) Rp Z.C σ1 (scNameA m) stack := by + refine J.reply.frame (fun r hr' => ?_) (fun c _ y k h => ⟨y, k, keepO _ _ h (fun _ e => by cases e), rfl⟩) + (fun r _ x' h => ⟨x', backO _ _ h (fun _ e => by cases e), rfl⟩) + have hr3 := J.iplaced.e.rep r hr' + have hdis := J.iplaced.e.rt r hr' t htZ + have key : ∀ o : U64, o.val = 8 ∨ o.val = 16 → L.mem s1 (wa r o) = L.mem s (wa r o) := by + intro o ho + have hv : (wa r o).val = r.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + refine w1'.mem_eq (by unfold Aligned; rw [hv]; omega) fun hw => ?_ + obtain ⟨K, hK, e⟩ := faultWA_eq hw + have ev := congrArg UScalar.val e + rw [hv, field_val hta (by rcases hK with rfl | rfl | rfl | rfl <;> simp)] at ev + have : 3376 ≤ K.val ∧ K.val ≤ 3400 := by rcases hK with rfl | rfl | rfl | rfl <;> simp + omega + exact ⟨key _ (Or.inr (by simp [layout.reply.NEXT])), key _ (Or.inl (by simp [layout.reply.PREV]))⟩ + have bufCover : BufCover st Z.T Bf s1 σ1 := + BufCover.frameK J.bufCover (fun u hu => by rw [ibWordA_eq]; exact tw u hu _ (by simp) (by simp) (by simp)) + (fun u hu => by + show capAtA (L.mem s1) (sbWordA u) = capAtA (L.mem s) (sbWordA u) + unfold capAtA + rw [sbWordA8_eq, sbWordA_eq, tw u hu (64#u64) (by simp) (by simp) (by simp), + tw u hu (72#u64) (by simp) (by simp) (by simp)]) + (fun u hu => let ⟨y, hy, _⟩ := J.ep.tcb u hu; ⟨y, hy⟩) + (fun p y hy => by + obtain ⟨y1, h1, -, -, h4, h5⟩ := kT p y hy + exact ⟨y1, h1, h5, h4⟩) + have slotK : ∀ y, IsSlotA Z.th y → capAtA (L.mem s1) y = capAtA (L.mem s) y := by + intro y hy + obtain ⟨h32, hfit⟩ := IsSlotA.al32 J.sr.place hy + have al : Aligned y := by unfold Aligned; omega + have v8 : (wa y 8#u64).val = y.val + 8 := by + rw [wa_valm] + have h8 : (8#u64 : U64).val = 8 := by simp + omega + have al8 : Aligned (wa y 8#u64) := by unfold Aligned; rw [v8]; omega + unfold capAtA + rw [wSS.mem_eq al (fun h => J.toJStage3A.slot_offStepA hy (le_refl _) (by omega) h), + wSS.mem_eq al8 (fun h => J.toJStage3A.slot_offStepA hy (by omega) (by omega) h)] + have hSt : StateRelA Z.th (L.mem s1) σ1 := by + have h := J.j.1.st.congrA (mem' := L.mem s1) fun y hy => slotK y (slotW_isSlot J.sr.place hy) + refine ⟨fun p r hp => ?_, fun u hu => ?_⟩ + · obtain ⟨cs, h1, h2', h3⟩ := h.cnode p r hp + exact ⟨cs, keepO _ _ h1 (fun _ e => by cases e), h2', h3⟩ + · obtain ⟨y, h1, h2'⟩ := h.tcb u hu + by_cases e : specPtr u = specPtr t + · rw [e, hk] at h1 + cases h1 + exact ⟨x1, by rw [e]; exact hk1, h2'⟩ + · exact ⟨y, (far _ e).trans h1, h2'⟩ + have sv : SchedValidA σ1 := by + have sc : ∀ p, ScActiveAtA σ p → ScActiveAtA σ1 p := fun p ⟨y, k, e, act⟩ => + ⟨y, k, keepO _ _ e (fun _ e => by cases e), act⟩ + have tcbAct : ∀ u, (∃ y c, σ.kheap u = some (.tcb y) ∧ y.tcbSchedContext = some c ∧ ScActiveAtA σ c) → + ∃ y c, σ1.kheap u = some (.tcb y) ∧ y.tcbSchedContext = some c ∧ ScActiveAtA σ1 c := + fun u ⟨y, c, e, es, act⟩ => by + obtain ⟨y1, h1, -, h2, -⟩ := kT _ y e + exact ⟨y1, c, h1, h2.trans es, sc _ act⟩ + refine ⟨fun d p u hm => ?_, fun e1 e2 => ?_, fun u x' c e1 e2 e3 => ?_, fun u hu => ?_⟩ + · obtain ⟨act, nr⟩ := J.schedValid.rq d p u hm + exact ⟨tcbAct u act, nr⟩ + · obtain ⟨act, nr⟩ := J.schedValid.act e1 e2 + exact ⟨tcbAct _ act, nr⟩ + rotate_left + · exact tcbAct u (J.schedValid.rls u hu) + · obtain ⟨y, hy, hs, hsc⟩ := kB _ x' e1 + exact sc _ (J.schedValid.blk u y c hy (hs ▸ e2) (hsc.symm.trans e3)) + exact + { sr := SchedSRA.setUsrFaultA J.sr htZ hk hr ty w0 w1 w2 (some f) + ep := EpStateRel.setUsrFaultA J.ep J.iplaced.e htZ hk F + reply := reply + rel := hSt + cv := fun y hy => by rw [slotK y hy]; exact J.j.1.cv y hy + bufCover := bufCover + bufNative := J.bufNative.writesIn J.iplaced.e.tcb w1' fun u hu K hK => + not_faultWA hta (J.sr.core.tcbs u hu) (by rcases hK with rfl | rfl <;> decide) + (by rcases hK with rfl | rfl <;> decide) + sv := sv } + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3FaultSpecA.lean b/proofs/Fl4ProofsA64/P3/Stage3FaultSpecA.lean new file mode 100644 index 000000000..781df7204 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3FaultSpecA.lean @@ -0,0 +1,365 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2b, item 2): **the spec side of `handle_fault`** for a faulted sender with a +waiting receiver, or with no handler. + +* `handleFault_sendA`: the spec's `handle_fault` with an endpoint handler is the faulted `send_ipc` of a thread with an + SC (donation allowed); `handleFault_nullA`: with no handler the thread becomes `Inactive`. +* `doIpcTransfer_fault_rfA`, **`relIfBoundSc_donPushFA`**: `relIfBoundSc_donPushA` for a faulted sender. +* `sendIpc_recv_faultA`: `send_ipc` of a faulted sender to a waiting receiver, up to the reply push or `Inactive`. +* **`sendIpc_recv_faultBlockA`**: without a reply push every outcome is `SIKA` without donation and leaves the sender + `Inactive`. +* **`sendIpc_recv_faultPushA`**: with a reply push every outcome leaves the sender `BlockedOnReply`. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3SendDonateArmA +import Fl4ProofsA64.P3.Stage3SendFaultValidA +import Fl4ProofsA64.P3.Stage3SendFaultSpecA +import Fl4ProofsA64.P3.Stage3TimeoutSendBA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P3 Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Classical + +noncomputable section + +/-- **The spec's `handle_fault` with an endpoint handler is the faulted `send_ipc`** (outcomes). -/ +theorem handleFault_sendA {σ σ1 σ' : State Unit} {t ep : ObjRef} {badge : Word} {x : Tcb} {send recv grant grantReply : Bool} + {f : Fault} (hk : σ.kheap t = some (.tcb x)) (hsc : x.tcbSchedContext ≠ none) + (hc : getCap (Z := Unit) (getTcbFaultHandlerPtr t) σ = + ({(.endpointCap ep badge (rightsOfA send recv grant grantReply), σ)}, False)) + (h1 : σ1 = updK σ t (.tcb { x with tcbFault := some f })) + (hs : ((), σ') ∈ (sendIpc (Z := Unit) true false badge grant grantReply true t ep σ1).1) : + ((), σ') ∈ (handleFault (Z := Unit) t f σ).1 := by + subst h1 + unfold handleFault + rw [nd_pure_bindSc, nd_bind_oneSc hc, nd_bind_oneSc (threadGet_eq _ hk)] + have hd (d : Decidable (x.tcbSchedContext ≠ none)) : @decide _ d = true := @decide_eq_true _ d hsc + have hg (d : Decidable (Rights.allowGrant ∈ rightsOfA send recv grant grantReply)) : + @decide _ d = grant := by + apply Bool.eq_iff_iff.2 + simp only [decide_eq_true_eq] + rfl + have hgr (d : Decidable (Rights.allowGrantReply ∈ rightsOfA send recv grant grantReply)) : + @decide _ d = grantReply := by + apply Bool.eq_iff_iff.2 + simp only [decide_eq_true_eq] + rfl + simp only [sendFaultIpc, capEpBadge, capEpPtr, hd, hg, hgr, nd_bind_assocSc] + rw [nd_bind_oneSc (threadSet_eqSc _ hk)] + try simp only [nd_bind_assocSc] + refine mem_bindA.2 ⟨((), σ'), hs, ?_⟩ + rw [nd_pure_bindSc] + exact mem_bindA.2 ⟨((), σ'), by unfold NonDet.unless; rw [if_pos rfl]; exact Set.mem_singleton _, + Set.mem_singleton _⟩ + +/-- **The spec's `handle_fault` with no handler** leaves the thread `Inactive`. -/ +theorem handleFault_nullA {σ σ2 : State Unit} {t : ObjRef} {x : Tcb} {f : Fault} (hk : σ.kheap t = some (.tcb x)) + (hc : getCap (Z := Unit) (getTcbFaultHandlerPtr t) σ = ({(.nullCap, σ)}, False)) + (h2 : σ2 = stsAct (updK σ t (.tcb { x with tcbState := .inactive })) t) : + ((), σ2) ∈ (handleFault (Z := Unit) t f σ).1 := by + subst h2 + unfold handleFault + rw [nd_pure_bindSc, nd_bind_oneSc hc, nd_bind_oneSc (threadGet_eq _ hk)] + simp only [sendFaultIpc] + rw [nd_pure_bindSc] + refine mem_bindA.2 ⟨((), stsAct (updK σ t (.tcb { x with tcbState := .inactive })) t), ?_, Set.mem_singleton _⟩ + show ((), stsAct (updK σ t (.tcb { x with tcbState := .inactive })) t) ∈ (handleNoFault (Z := Unit) t σ).1 + unfold handleNoFault + rw [setThreadState_eq _ hk] + exact Set.mem_singleton _ + +/-- The transfer of a faulted sender keeps the refills. -/ +theorem doIpcTransfer_fault_rfA {σ : State Unit} {t d : ObjRef} {xs xr : Tcb} (hks : σ.kheap t = some (.tcb xs)) + (hkr : σ.kheap d = some (.tcb xr)) {f : Fault} (hf : xs.tcbFault = some f) (ep : Option ObjRef) (badge : Badge) + (grant : Bool) : ∀ x ∈ (doIpcTransfer (Z := Unit) t ep badge grant d σ).1, RfKA σ x.2 := by + have key : PresRC RfKA (· = σ) (doIpcTransfer (Z := Unit) t ep badge grant d) := by + refine doIpcTransfer_fault_genA hks hkr hf ep badge grant ?_ + refine doFaultTransfer_genA (fun a b e => e ▸ PreO.refl a) (fun t g => asUser_rfA t g) (fun p => ?_) _ _ _ _ + (fun s x regs hx => RfKA.upd fun _ _ h => by cases h) (fun rb _ k _ v => ?_) + · unfold schedContextUpdateConsumed + refine PresR.bind (eqPres_rfA (getSchedContext_eqPres p)) fun _ => PresR.bind (PresR.pure _) fun _ => ?_ + exact PresR.ite' (PresR.bind (updateSc_rfA p _ fun _ => ⟨rfl, rfl⟩) fun _ => PresR.pure _) + (PresR.bind (updateSc_rfA p _ fun _ => ⟨rfl, rfl⟩) fun _ => PresR.pure _) + · unfold storeWordOffs + exact PresR.bind PresR.get fun _ => PresR.bind (PresR.assert _) fun _ => doMachineOp_rfA _ + exact fun x hx => key σ rfl x hx + +/-- `set_thread_state t Inactive` leaves `t` `Inactive`. -/ +theorem faultBlockStA (t : ObjRef) : + HT (fun _ => True) (setThreadState (Z := Unit) t .inactive) (fun _ σ => StIs t .inactive σ) := by + unfold Fl4SpecA64.setThreadState + refine HT.bind (Q := fun _ σ => StIs t .inactive σ) (fun σ _ r hr y hy => ?_) fun _ σ h r hr => ?_ + · obtain ⟨x, -, e⟩ := threadSet_memA _ t hr + rw [e] at hy + have : ({ x with tcbState := .inactive } : Tcb) = y := by simpa using hy + rw [← this] + · rw [setThreadStateAct_eq] at hr + have e : r = ((), stsAct σ t) := hr + rw [e] + exact fun y hy => h y (by rwa [stsAct_kheap] at hy) + +/-- `reply_push t d r` leaves the sender `t` `BlockedOnReply r`. -/ +theorem faultPushStA (t d r : ObjRef) (cd : Bool) : + HT (fun _ => True) (replyPush (Z := Unit) t d r cd) (fun _ σ => StIs t (.blockedOnReply r) σ) := by + have skip : ∀ {α β : Type} {m : SMonad Unit α} {f : α → SMonad Unit β} {Q : β → State Unit → Prop}, + (∀ a, HT (fun _ => True) (f a) Q) → HT (fun _ => True) (m >>= f) Q := + fun h => HT.bind (Q := fun _ _ => True) (fun _ _ _ _ => trivial) h + unfold replyPush + refine skip fun _ => skip fun _ => skip fun _ => ?_ + refine HT.bind (Q := fun _ σ => StIs t (.blockedOnReply r) σ) ?_ fun _ => ?_ + · unfold Fl4SpecA64.setThreadState + refine HT.bind (Q := fun _ σ => StIs t (.blockedOnReply r) σ) (fun σ _ z hz y hy => ?_) fun _ σ h z hz => ?_ + · obtain ⟨x, -, e⟩ := threadSet_memA _ t hz + rw [e] at hy + have : ({ x with tcbState := .blockedOnReply r } : Tcb) = y := by simpa using hy + rw [← this] + · rw [setThreadStateAct_eq] at hz + have e : z = ((), stsAct σ t) := hz + rw [e] + exact fun y hy => h y (by rwa [stsAct_kheap] at hy) + refine HT.when (fun _ => ?_) (fun _ _ h => h) + refine HT.bind (Q := fun _ σ => StIs t (.blockedOnReply r) σ) + (fun σ h z hz => h.stk (bindScReply_stkA _ _ σ z hz)) fun _ => ?_ + intro σ h z hz + exact h.stk (schedContextDonate_stkA _ _ σ z hz) + +/-- The receiver's wake-up keeps another thread's state. -/ +theorem faultTailStA (t d : ObjRef) (ts : ThreadState) (htd : t ≠ d) : + HT (StIs t ts) (do + setThreadState (Z := Unit) d .running + let destSc ← getTcbObjRef Tcb.tcbSchedContext d + ifSporadicCurScTestRefillUnblockCheck destSc + possibleSwitchTo d) (fun _ σ => StIs t ts σ) := by + refine HT.bind (Q := fun _ σ => StIs t ts σ) ?_ fun _ => ?_ + · unfold Fl4SpecA64.setThreadState + refine HT.bind (Q := fun _ σ => StIs t ts σ) (fun σ h r hr y hy => ?_) fun _ σ h r hr => ?_ + · obtain ⟨x, -, e⟩ := threadSet_memA _ d hr + rw [e] at hy + exact h y (by simpa only [Function.update_of_ne htd] using hy) + · rw [setThreadStateAct_eq] at hr + have e : r = ((), stsAct σ d) := hr + rw [e] + exact fun y hy => h y (by rwa [stsAct_kheap] at hy) + refine HT.bind (Q := fun _ σ => StIs t ts σ) (fun σ h r hr => by + have e : r.2 = σ := ((threadGet_eqPres _ _) σ r hr).symm + rw [e]; exact h) fun sc => ?_ + refine HT.bind (Q := fun _ σ => StIs t ts σ) (fun σ h r hr => + (StIs.frame t ts).keep σ _ ((by unfold ifSporadicCurScTestRefillUnblockCheck ifCondRefillUnblockCheck; tpres : + TPres (ifSporadicCurScTestRefillUnblockCheck (Z := Unit) sc)) σ r hr) h) fun _ => ?_ + intro σ h r hr y hy + exact h y (((KPresInst.pres : PresR TcbCellsEq (possibleSwitchTo (Z := Unit) d)) σ r hr).2 _ _ |>.2 hy) + +section +open fl4_core_a64 Fl4ProofsA64.P1 +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **The receiver's `RelIfBoundSc` after the donating push of a faulted sender** (`relIfBoundSc_donPushA`'s +sibling). -/ +theorem relIfBoundSc_donPushFA {th : TypedHeapA} {kb : U64} (hPl : PlaceA th kb) (sf : S) {σ : State Unit} + (hR : StateRelA th (L.mem sf) σ) (hV : SlotsValidA th (L.mem sf)) {t d : U64} {e : ObjRef} {z : Endpoint} + (htr : th d = some .tcb) (hk : σ.kheap e = some (.endpoint z)) (z' : Endpoint) + {xt xd : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) (hkd : σ.kheap (specPtr d) = some (.tcb xd)) + {f : Fault} (hf : xt.tcbFault = some f) (hal : ∀ rb, ipcBufOfA true xd = some rb → (bufAddr rb 0).toNat % 8 = 0) + (htd : specPtr t ≠ specPtr d) (hd0 : xd.tcbSchedContext = none) {sc : ObjRef} + (hsc : xt.tcbSchedContext = some sc) (hD : DonA σ (specPtr t) xt) + (hti : specPtr t ≠ σ.idleThread) (hdi : specPtr d ≠ σ.idleThread) + (hrl : specPtr t ∉ σ.releaseQueue) (hRB : RelIfBound σ (specPtr t)) + (badge : Badge) (g : Bool) (r : ObjRef) : + ∀ σ2 σ3 σ4, + doIpcTransfer (Z := Unit) (specPtr t) (some e) badge g (specPtr d) + (updK σ e (.endpoint z')) = ({((), σ2)}, False) → + replyUnlinkTcb (Z := Unit) (specPtr d) r σ2 = ({((), σ3)}, False) → + replyPush (Z := Unit) (specPtr t) (specPtr d) r true σ3 = ({((), σ4)}, False) → + RelIfBoundSc σ4 (specPtr d) := by + intro σ2 σ3 σ4 hT hU hP + set σ0 := updK σ e (.endpoint z') with hσ0 + have hte : specPtr t ≠ e := fun h => by rw [← h, hkt] at hk; cases hk + have hde : specPtr d ≠ e := fun h => by rw [← h, hkd] at hk; cases hk + have hkt0 : σ0.kheap (specPtr t) = some (.tcb xt) := by rw [hσ0, updK_kheap, Function.update_of_ne hte, hkt] + have hkd0 : σ0.kheap (specPtr d) = some (.tcb xd) := by rw [hσ0, updK_kheap, Function.update_of_ne hde, hkd] + have HS : SpecSchedA σ σ0 := by + refine ⟨fun p => ?_, rfl, rfl, rfl, rfl, rfl, rfl⟩ + by_cases h : p = e + · subst h + exact Or.inr (Or.inr ⟨_, _, hk, by rw [hσ0, updK_kheap, Function.update_self], + Or.inr (Or.inr (Or.inl ⟨_, _, rfl, rfl⟩))⟩) + · exact Or.inl (by rw [hσ0, updK_kheap, Function.update_of_ne h]) + have R0 : RfKA σ σ0 := RfKA.upd fun _ _ h => by cases h + have m2 : ((), σ2) ∈ (doIpcTransfer (Z := Unit) (specPtr t) (some e) badge g (specPtr d) σ0).1 := by + rw [hT]; exact Set.mem_singleton _ + have m3 : ((), σ3) ∈ (replyUnlinkTcb (Z := Unit) (specPtr d) r σ2).1 := by rw [hU]; exact Set.mem_singleton _ + have m4 : ((), σ4) ∈ (replyPush (Z := Unit) (specPtr t) (specPtr d) r true σ3).1 := by + rw [hP]; exact Set.mem_singleton _ + have R2 : RfKA σ0 σ2 := doIpcTransfer_fault_rfA hkt0 hkd0 hf _ _ _ _ m2 + have R3 : RfKA σ2 σ3 := replyUnlinkTcb_rfA _ _ _ _ m3 + have R4 : RfKA σ3 σ4 := replyPush_rfA _ _ _ _ _ _ m4 + have RF : RfKA σ σ4 := PreO.trans R0 (PreO.trans R2 (PreO.trans R3 R4)) + -- the SC bindings before the push + have K2 : SIKA σ.idleThread (fun p => p = specPtr d) (fun _ => False) false σ0 σ2 := + doIpcTransfer_any_sikA hPl sf (hR.specSchedA HS) hV htr hkt0 hkd0 hal _ _ _ (Or.inr ⟨f, hf⟩) σ0 rfl _ m2 + have K3 : SIKA σ.idleThread (fun p => p = specPtr d) (fun _ => False) false σ2 σ3 := + replyUnlinkTcb_sikA (i := σ.idleThread) (M := fun p => p = specPtr d) (N := fun _ => False) (don := false) + (specPtr d) r rfl _ _ m3 + have K := K2.trans K3 + obtain ⟨xt3, hkt3, gt⟩ := K.tcbF hkt0 + obtain ⟨xd3, hkd3, gd⟩ := K.tcbF hkd0 + have et : xt3.tcbSchedContext = some sc := (gt.sc.resolve_right fun c => by cases c.1).trans hsc + have ed : xd3.tcbSchedContext = none := (gd.sc.resolve_right fun c => by cases c.1).trans hd0 + -- the push binds the sender's SC to the receiver + have bound : ∀ y, σ4.kheap (specPtr d) = some (.tcb y) → y.tcbSchedContext = some sc := by + have H : HT (fun s => s = σ3) (replyPush (Z := Unit) (specPtr t) (specPtr d) r true) + (fun _ s => ∀ y, s.kheap (specPtr d) = some (.tcb y) → y.tcbSchedContext = some sc) := by + unfold replyPush + refine HT.bind (Q := fun a s => s = σ3 ∧ a = some sc) (fun s h x hx => ?_) fun a => ?_ + · subst h + unfold getTcbObjRef at hx + rw [threadGet_valA _ hkt3 hx] + exact ⟨rfl, et⟩ + refine HT.bind (Q := fun b s => a = some sc ∧ b = none) (fun s h x hx => ?_) fun b => ?_ + · obtain ⟨rfl, ha⟩ := h + unfold getTcbObjRef at hx + rw [threadGet_valA _ hkd3 hx] + exact ⟨ha, ed⟩ + refine HT.bind (Q := fun _ _ => a = some sc ∧ b = none) (fun _ h _ _ => h) fun _ => ?_ + refine HT.bind (Q := fun _ _ => a = some sc ∧ b = none) (fun _ h _ _ => h) fun _ => ?_ + intro s ⟨ha, hb⟩ x hx + subst ha hb + unfold NonDet.when at hx + rw [if_pos (by simp)] at hx + obtain ⟨u, -, hx⟩ := mem_bindA.1 hx + exact donate_bindsA sc (specPtr d) u.2 trivial x hx + exact H σ3 rfl _ m4 + intro x scp y n hx hscp hy ha _ + have e1 : scp = sc := Option.some.inj (hscp.symm.trans (bound x hx)) + subst e1 + obtain ⟨y0, hy0, er, em⟩ := RF.2.2 _ _ _ hy + have a0 : scActive y0 = true := by unfold scActive at ha ⊢; rw [← em]; exact ha + have rel := hRB xt scp y0 n hkt hsc hy0 a0 hrl + unfold releasedSc + unfold released at rel + have eh : refillHd y = refillHd y0 := by unfold refillHd; rw [er] + rw [RF.1, eh] + exact rel + + +end + +section +open fl4_core_a64 Fl4ProofsA64.P1 +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + {i : ObjRef} {M N : ObjRef → Prop} {th : TypedHeapA} {kb : U64} {σ : State Unit} {t d : U64} {e : ObjRef} + {q : List ObjRef} {xt xd : Tcb} {f : Fault} {r : Option ObjRef} {a0 : ObjRef} {dd : ReceiverPayload} + +/-- **`send_ipc` of a faulted sender to a waiting receiver**, up to the reply push or the sender's block. -/ +theorem sendIpc_recv_faultA (hPl : PlaceA th kb) (sf : S) (hR : StateRelA th (L.mem sf) σ) + (hV : SlotsValidA th (L.mem sf)) (htr : th d = some .tcb) + (hk : σ.kheap e = some (.endpoint (.recvEP (specPtr d :: q)))) + (hkt : σ.kheap (specPtr t) = some (.tcb xt)) (hkd : σ.kheap (specPtr d) = some (.tcb xd)) + (hal : ∀ rb, ipcBufOfA true xd = some rb → (bufAddr rb 0).toNat % 8 = 0) + (blk call : Bool) (badge : Badge) (g gr cd : Bool) (hf : xt.tcbFault = some f) (hMd : M (specPtr d)) + (hst : xd.tcbState = .blockedOnReceive a0 r dd) (Q : State Unit → Prop) + (H : ∀ s1, SIKA i M N false σ s1 → ∀ x ∈ (((if (g = true ∨ gr = true) ∧ r ≠ none then + replyPush (Z := Unit) (specPtr t) (specPtr d) (the r) cd + else setThreadState (specPtr t) .inactive) >>= fun _ => do + setThreadState (specPtr d) .running + let destSc ← getTcbObjRef Tcb.tcbSchedContext (specPtr d) + ifSporadicCurScTestRefillUnblockCheck destSc + possibleSwitchTo (Z := Unit) (specPtr d)) s1).1, Q x.2) : + ∀ x ∈ (sendIpc (Z := Unit) blk call badge g gr cd (specPtr t) e σ).1, Q x.2 := by + intro x hx + unfold sendIpc at hx + rw [nd_bind_oneSc (getEndpoint_eq hk)] at hx + cases blk <;> dsimp only at hx + all_goals + obtain ⟨y1, h1, hx⟩ := mem_bindA.1 hx + have K1 : SIKA i M N false σ y1.2 := tcbEpDequeue_sikA (i := i) (M := M) (N := N) (don := false) _ e hMd σ y1 h1 + have T1 : TcbKeepA σ y1.2 := TKInst.pres σ y1 h1 + have F1 : SchedFrameA σ y1.2 := SPresInst.pres σ y1 h1 + obtain ⟨y2, h2, hx⟩ := mem_bindA.1 hx + have e2 : y2 = (xd.tcbState, y1.2) := by + unfold getThreadState at h2 + exact threadGet_valA _ (T1 _ _ hkd) h2 + subst e2 + dsimp only at hx + rw [hst] at hx + dsimp only at hx + rw [nd_pure_bindSc] at hx + obtain ⟨y3, h3, hx⟩ := mem_bindA.1 hx + have K3 : SIKA i M N false y1.2 y3.2 := + doIpcTransfer_any_sikA hPl sf (hR.specSchedA F1.1) hV htr (T1 _ _ hkt) (T1 _ _ hkd) hal _ _ _ + (Or.inr ⟨f, hf⟩) y1.2 rfl y3 h3 + obtain ⟨y4, h4, hx⟩ := mem_bindA.1 hx + have K4 : SIKA i M N false y3.2 y4.2 := by + cases r with + | none => exact (PresR.pure _ : PresR (SIKA i M N false) _) y3.2 y4 h4 + | some r => exact replyUnlinkTcb_sikA _ _ hMd y3.2 y4 h4 + obtain ⟨y5, h5, hx⟩ := mem_bindA.1 hx + have e5 : y4.2 = y5.2 := by + unfold getTcbObjRef at h5 + exact threadGet_eqPres _ _ y4.2 y5 h5 + have K : SIKA i M N false σ y5.2 := by + rw [← e5] + exact K1.trans (K3.trans K4) + obtain ⟨y6, h6, hx⟩ := mem_bindA.1 hx + obtain ⟨z, hz, gz⟩ := K.tcbF hkt + have e6 : y6 = (some f, y5.2) := by + have h := threadGet_valA Tcb.tcbFault hz h6 + rw [gz.fault, hf] at h + exact h + subst e6 + dsimp only at hx + rw [if_pos (Or.inr (show some f ≠ none by simp))] at hx + refine H y5.2 K x ?_ + split_ifs at hx ⊢ <;> exact hx + +/-- **Without a reply push** (no grant, or no reply object at the receiver) every outcome is `SIKA` without donation +and leaves the faulted sender `Inactive`. -/ +theorem sendIpc_recv_faultBlockA (hPl : PlaceA th kb) (sf : S) (hR : StateRelA th (L.mem sf) σ) + (hV : SlotsValidA th (L.mem sf)) (htr : th d = some .tcb) + (hk : σ.kheap e = some (.endpoint (.recvEP (specPtr d :: q)))) + (hkt : σ.kheap (specPtr t) = some (.tcb xt)) (hkd : σ.kheap (specPtr d) = some (.tcb xd)) + (hal : ∀ rb, ipcBufOfA true xd = some rb → (bufAddr rb 0).toNat % 8 = 0) + (blk call : Bool) (badge : Badge) (g gr cd : Bool) (hf : xt.tcbFault = some f) + (hMt : M (specPtr t)) (hMd : M (specPtr d)) (hNd : N (specPtr d)) (htd : specPtr t ≠ specPtr d) + (hst : xd.tcbState = .blockedOnReceive a0 r dd) (hn : ¬ ((g = true ∨ gr = true) ∧ r ≠ none)) : + ∀ x ∈ (sendIpc (Z := Unit) blk call badge g gr cd (specPtr t) e σ).1, + SIKA i M N false σ x.2 ∧ StIs (specPtr t) .inactive x.2 := by + refine sendIpc_recv_faultA (i := i) (M := M) (N := N) hPl sf hR hV htr hk hkt hkd hal blk call badge g gr cd hf hMd hst + (fun s => SIKA i M N false σ s ∧ StIs (specPtr t) .inactive s) fun s1 K x hx => ?_ + rw [if_neg hn] at hx + obtain ⟨y, hy, hx⟩ := mem_bindA.1 hx + have Ky : SIKA i M N false s1 y.2 := setThreadState_sikA (don := false) _ hMt _ (by intro h; cases h) s1 y hy + have tailK : PresR (SIKA i M N false) (do + setThreadState (specPtr d) .running + let destSc ← getTcbObjRef Tcb.tcbSchedContext (specPtr d) + ifSporadicCurScTestRefillUnblockCheck destSc + possibleSwitchTo (Z := Unit) (specPtr d)) := + PresR.bind (setThreadState_sikA _ hMd _ (by intro h; cases h)) fun _ => + PresR.bind (eqPres_sikA (threadGet_eqPres _ _)) fun _ => + PresR.bind (refillCheck_sikA _) fun _ => possibleSwitchTo_sikA _ hNd + exact ⟨K.trans (Ky.trans (tailK y.2 x hx)), + faultTailStA (specPtr t) (specPtr d) .inactive htd y.2 (faultBlockStA (specPtr t) s1 trivial y hy) x hx⟩ + +/-- **With a reply push** every outcome leaves the faulted sender `BlockedOnReply`. -/ +theorem sendIpc_recv_faultPushA (hPl : PlaceA th kb) (sf : S) (hR : StateRelA th (L.mem sf) σ) + (hV : SlotsValidA th (L.mem sf)) (htr : th d = some .tcb) + (hk : σ.kheap e = some (.endpoint (.recvEP (specPtr d :: q)))) + (hkt : σ.kheap (specPtr t) = some (.tcb xt)) (hkd : σ.kheap (specPtr d) = some (.tcb xd)) + (hal : ∀ rb, ipcBufOfA true xd = some rb → (bufAddr rb 0).toNat % 8 = 0) + (blk call : Bool) (badge : Badge) (g gr cd : Bool) (hf : xt.tcbFault = some f) (hMd : M (specPtr d)) + (htd : specPtr t ≠ specPtr d) {rp : ObjRef} (hst : xd.tcbState = .blockedOnReceive a0 (some rp) dd) + (hg : g = true ∨ gr = true) : + ∀ x ∈ (sendIpc (Z := Unit) blk call badge g gr cd (specPtr t) e σ).1, + StIs (specPtr t) (.blockedOnReply rp) x.2 := by + refine sendIpc_recv_faultA (i := specPtr t) (M := fun _ => True) (N := fun _ => True) hPl sf hR hV htr hk hkt hkd + hal blk call badge g gr cd hf trivial hst (fun s => StIs (specPtr t) (.blockedOnReply rp) s) fun s1 _ x hx => ?_ + rw [if_pos ⟨hg, by simp⟩] at hx + obtain ⟨y, hy, hx⟩ := mem_bindA.1 hx + exact faultTailStA (specPtr t) (specPtr d) _ htd y.2 (faultPushStA (specPtr t) (specPtr d) rp cd s1 trivial y hy) x hx + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3FaultTailA.lean b/proofs/Fl4ProofsA64/P3/Stage3FaultTailA.lean new file mode 100644 index 000000000..6cc82e12a --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3FaultTailA.lean @@ -0,0 +1,231 @@ +/- +P3 (aarch64, A64-P3 Stage 3, fault arms, Option A of the lead's ruling 2026-12-04): **`handle_fault`'s fault write +and `send_ipc` as one step from the entry state.** `set_fault` leaves the running current thread with a fault, so no +`JStage3A` holds between the write and the send (`SInvXA.faultSt`); the arms read the faulted state's relations from +`FaultPcA` and close the record from the entry state, as the kernel-entry timeout sends do (`P3/Stage3KEntrySendA`). + +* `SIKXA.updFault` (moved here from `P3/Stage3SikxAlgA`): a TCB's fault rewritten. +* `JStage3A.faultRepA`, `inUserFrame_updTcbA`, `JStage3A.repOK_faultA`, `JStage3A.sendFp_faultA`: entry-state facts + at the faulted state. +* **`JSyscallA.faultTailA`**: the record after the fault write and an `SIKA` body from the faulted state; the composite + is an `SIKXA` step from the entry state, `SInvXA` by `SIKXA.sinv` with `SIKXA.faultStA`. + +Named premises: the callers' ([P] `ArchTotal`, `ArchPlat`, `hCs`; [S] `h1`). New module. +-/ +import Fl4ProofsA64.P3.Stage3FaultSetA +import Fl4ProofsA64.P3.Stage3KEntrySendA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4ProofsA64.P3 Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Endp Fl4SpecA64 + +/-- A TCB's fault rewritten. -/ +theorem SIKXA.updFault {i : ObjRef} {M N : ObjRef → Prop} {don : Bool} {σ : State Unit} {t : ObjRef} {x : Tcb} + (hk : σ.kheap t = some (.tcb x)) (f : Option Fault) : + SIKXA i M N don σ (updK σ t (.tcb { x with tcbFault := f })) := by + refine ⟨rfl, fun p => ?_, fun _ _ _ a => Or.inl a, fun _ a => a, Or.inl rfl, + fun g => RdyA.updNs g t _ (fun _ _ e => by cases e)⟩ + by_cases e1 : p = t + · subst e1 + exact Or.inr ⟨x, { x with tcbFault := f }, hk, by simp [updK], ⟨rfl, rfl, rfl, rfl, rfl, Or.inl rfl, Or.inl rfl, + rfl, rfl⟩⟩ + · refine Or.inl ?_ + have e : (updK σ t (.tcb { x with tcbFault := f })).kheap p = σ.kheap p := by simp [updK, e1] + rw [e] + exact .same _ + +end Fl4ProofsA64.P3.Sched + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-- A user frame is kept by a TCB write. -/ +theorem inUserFrame_updTcbA {σ : State Unit} {t p : ObjRef} {x : Tcb} (hk : σ.kheap t = some (.tcb x)) (y : Tcb) + (h : inUserFrame p σ) : inUserFrame p (updK σ t (.tcb y)) := by + obtain ⟨sz, h⟩ := h + refine ⟨sz, ?_⟩ + have e : p &&& ~~~(Fl4SpecA64.mask (pageBitsForSize sz)) ≠ t := fun e => by rw [e, hk] at h; cases h + rw [updK_kheap, Function.update_of_ne e] + exact h + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s : S} {a : A} {σ : State Unit} {m : McState} + +/-- The reply objects' words are off the fault words. -/ +theorem JStage3A.faultRepA (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {t : U64} (ht : t ∈ Z.T) (ty w0 w1 w2 : U64) : + ∀ r ∈ Rp, ∀ o : U64, o.val < 32 → o.val % 8 = 0 → + L.mem (faultWordsA st s t ty w0 w1 w2) (wa r o) = L.mem s (wa r o) := by + intro r hr o ho h8 + have hta : TcbAddr t := J.sr.core.tcbs _ ht + have hr3 := J.iplaced.e.rep r hr + have hdis := J.iplaced.e.rt r hr t ht + have hv : (wa r o).val = r.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + refine (faultWords_wrG (st := st) J.sr.core.tcbs ht s ty w0 w1 w2).mem_eq (by unfold Aligned; rw [hv]; omega) + fun hw => ?_ + obtain ⟨K, hK, e⟩ := faultWA_eq hw + have ev := congrArg UScalar.val e + rw [hv, field_val hta (by rcases hK with rfl | rfl | rfl | rfl <;> simp)] at ev + have : 3376 ≤ K.val ∧ K.val ≤ 3400 := by rcases hK with rfl | rfl | rfl | rfl <;> simp + omega + +/-- A receiver's reply object at the faulted state (`repOKA` at the entry state). -/ +theorem JStage3A.repOK_faultA (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {t d : U64} (hd : d ∈ Z.T) (hdt : specPtr d ≠ specPtr t) {x : Tcb} + (hkx : σ.kheap (specPtr t) = some (.tcb x)) (f : Option Fault) : + RepOKA (updK σ (specPtr t) (.tcb { x with tcbFault := f })) (specPtr d) := by + intro y hy ep r dd hs + rw [updK_kheap, Function.update_of_ne hdt] at hy + obtain ⟨xr, hk, hb⟩ := J.repOKA hd y hy ep r dd hs + have hrt : r ≠ specPtr t := fun e => by rw [e, hkx] at hk; cases hk + exact ⟨xr, by rw [updK_kheap, Function.update_of_ne hrt]; exact hk, hb⟩ + +/-- A send's write set at the faulted state, at the entry state. -/ +theorem JStage3A.sendFp_faultA (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {t : U64} (ht : t ∈ Z.T) {x : Tcb} (f : Option Fault) (ty w0 w1 w2 : U64) {t' e d b : U64} {W : Prop} + (h : SendFpA st Z.T Rp Bf (fun σ s => MWrA (st := st) Z m σ s) (updK σ (specPtr t) (.tcb { x with tcbFault := f })) + (faultWordsA st s t ty w0 w1 w2) t' e d W b) : + SendFpA st Z.T Rp Bf (fun σ s => MWrA (st := st) Z m σ s) σ s t' e d W b := by + have w1' := faultWords_wrG (st := st) J.sr.core.tcbs ht s ty w0 w1 w2 + have hta : TcbAddr t := J.sr.core.tcbs _ ht + rcases h with (h | ⟨w, hw, hb, c⟩ | h | h) | h | h | h | h + · exact Or.inl (Or.inl h) + · refine Or.inl (Or.inr (Or.inl ⟨w, hw, ?_, c⟩)) + have hwa := J.sr.core.tcbs w hw + rw [← hb] + exact (w1'.mem_eq (field_aligned hwa (K := 3336#u64) (by simp) (by simp)) + (not_faultWA hta hwa (by simp) (by simp))).symm + · exact Or.inl (Or.inr (Or.inr (Or.inl h))) + · exact Or.inl (Or.inr (Or.inr (Or.inr h))) + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr (Or.inl h)) + · exact Or.inr (Or.inr (Or.inr (Or.inl h))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (mwr_back rfl rfl (ssz_updK_tcb σ _ _) w1'.1 h)))) + +set_option maxHeartbeats 2000000 in +/-- **The record after `handle_fault`'s fault write and a `send_ipc` body** (module docstring). The body is an `SIKA` +step from the faulted state with the faulted thread a mover; `reply` and `blkQ` are given on the composite step. -/ +theorem JSyscallA.faultTailA (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + {ty w0 w1 w2 : U64} {f : Fault} {x : Tcb} (hkx : σ.kheap (specPtr (L.curW s)) = some (.tcb x)) + {fe : error.Fault} {s2 : S} {σ2 : State Unit} + (hp2 : faults.handle_fault st ar s a (L.curW s) fe = ok (s2, a)) + (wI : WritesIn st (faultWordsA st s (L.curW s) ty w0 w1 w2) s2 (fun b => + IpcFpA st Z E Rp Bf m (updK σ (specPtr (L.curW s)) (.tcb { x with tcbFault := some f })) + (faultWordsA st s (L.curW s) ty w0 w1 w2) b ∨ RepWA Rp b)) + {M Nq : ObjRef → Prop} {don : Bool} + (K : SIKA σ.idleThread M Nq don (updK σ (specPtr (L.curW s)) (.tcb { x with tcbFault := some f })) σ2) + (hMt : M (specPtr (L.curW s))) + (HS : SpecSchedA (updK σ (specPtr (L.curW s)) (.tcb { x with tcbFault := some f })) σ2) + (SSz : SSzA (updK σ (specPtr (L.curW s)) (.tcb { x with tcbFault := some f })) σ2) + (Qk : IpcSortedA (specPtr '' E) (specPtr '' N) (updK σ (specPtr (L.curW s)) (.tcb { x with tcbFault := some f })) → + QBlkA (specPtr '' E) (specPtr '' N) (updK σ (specPtr (L.curW s)) (.tcb { x with tcbFault := some f })) σ2) + (hz : ∀ x : Word, UtFreeA st OS s (fun d => d = false) (fl4Ptr x).val → + σ2.machineState.underlyingMemory x = σ.machineState.underlyingMemory x) + {Q : U64 → U64 → List U64} {Rl : U64 → List U64} + (sr : SchedSRA st { Z with Q := Q, R := Rl } m κ s2 σ2) (E2 : EpStateRel Z.T E Rp Bf (L.mem s2) σ2) + {stack' : U64 → List U64} + (reply : SIKXA σ.idleThread M Nq don σ σ2 → ReplyStackRelA (L.mem s2) Rp Z.C σ2 (scNameA m) stack') + (hdr : ∀ r ∈ Rp, DeadA st OS .reply s r → ∀ c ∈ Z.C, r ∉ stack' c) + (wk : NtfnWaitKeepMA M Z.T N (L.mem s) (L.mem s2)) + (hMs : ∀ u ∈ Z.T, M (specPtr u) → ∀ y, σ.kheap (specPtr u) = some (.tcb y) → + ∀ p, y.tcbState ≠ .blockedOnNotification p) + (hMs' : ∀ u ∈ Z.T, M (specPtr u) → ∀ y, σ2.kheap (specPtr u) = some (.tcb y) → + ∀ p, y.tcbState ≠ .blockedOnNotification p) + (blkQ : SIKXA σ.idleThread M Nq don σ σ2 → BlkQueuedA Z.T E N (L.mem s2) σ2) (sv : SchedValidA σ2) + (notM : ∀ c < n, ¬ M (m.idleThreadOf c)) (hMI : ¬ M σ.idleThread) + (hMq : ∀ y, M y → ∀ d p, y ∉ σ.readyQueues d p) + (hN : ∀ y, Nq y → y ≠ σ.curThread ∧ y ≠ σ.idleThread ∧ (∀ c < n, m.idleThreadOf c ≠ y) ∧ + ∀ z, σ2.kheap y = some (.tcb z) → runnable z.tcbState = true) + (hN' : ∀ y, Nq y → ∃ z, σ.kheap y = some (.tcb z)) + (hMrls : ∀ y, M y → y ∉ σ.releaseQueue) + (hMf : ∀ p, M p → ∀ y, σ2.kheap p = some (.tcb y) → y.tcbFault ≠ none → FaultStA y.tcbState) + (hMv : ∀ u ∈ Z.T, M (specPtr u) → L.mem s (stA u) ≠ sched.TS_RUNNING_VM) : + JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack' κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s2 a σ2 m := by + set t := L.curW s with ht_def + set x1 : Tcb := { x with tcbFault := some f } with hx1 + set σ1 := updK σ (specPtr t) (.tcb x1) with hσ1 + have htZ : t ∈ Z.T := J.sr.curT + have w1' : WritesIn st s (faultWordsA st s t ty w0 w1 w2) (FaultWA t) := + faultWords_wrG (st := st) J.sr.core.tcbs htZ s ty w0 w1 w2 + have SSz1 : SSzA σ σ1 := ssz_updK_tcb σ _ _ + have far : ∀ p, p ≠ specPtr t → σ1.kheap p = σ.kheap p := fun p hp => by simp [hσ1, hp] + have hk1 : σ1.kheap (specPtr t) = some (.tcb x1) := by simp [hσ1] + have K13 : SIKXA σ.idleThread M Nq don σ σ2 := (SIKXA.updFault hkx (some f)).transS K + have wX : WritesIn st s s2 (fun b => IpcFpA st Z E Rp Bf m σ s b ∨ FaultWA t b ∨ RepWA Rp b) := + (w1'.trans wI).mono fun b h => h.elim (fun h => Or.inr (Or.inl h)) + (fun h => h.elim (fun h => Or.inl (h.imp id (mwr_back rfl rfl SSz1 w1'.1))) (fun h => Or.inr (Or.inr h))) + have wall : WritesIn st s s2 (fun b => SchedStepNWA (st := st) N E Rp Z m σ s b ∨ BufW Bf b) := + wX.mono fun b h => h.elim + (fun h => h.elim (ewrite_stepNA_buf J.sr.core.tcbs) (fun h => Or.inl (Or.inl (Or.inl h)))) + (fun h => h.elim (fun h => Or.inl (Or.inl (Or.inr (faultTcbStepA J.sr.core.tcbs htZ h)))) + (fun h => Or.inl (Or.inr (Or.inr (Or.inr h))))) + have P1 := J.faultRpA hA hPl hp2 + have j7 : J7A st (J7R2A st ar hPl.ipa) Z H OS s2 σ2 m := J7A.of_rpPostA hA hPl J.toJStage3A P1 wall + have HS1 : SpecSchedA σ σ1 := + { kh := fun p => by + by_cases e : p = specPtr t + · right; left + subst e + exact ⟨x, x1, hkx, hk1, ⟨rfl, rfl, rfl⟩⟩ + · exact Or.inl (far p e) + irqS := rfl + irqN := rfl + asid := rfl + vcur := rfl + cdt := rfl + orig := rfl } + have Q01 : QBlkA (specPtr '' E) (specPtr '' N) σ σ1 := QSendA.toBlk J.ipcSorted + ((QKeepA.upd hkx (QObjA.ofTcb (y := x1) rfl)).trans + (QKeepA.of_kheap (by rw [hσ1]; rfl) (by rw [hσ1]; exact ⟨rfl, rfl, rfl, rfl⟩))).toSend + have Q12 := Qk Q01.sorted + have Q02 : QBlkA (specPtr '' E) (specPtr '' N) σ σ2 := + ⟨Q12.sorted, fun h => Q12.act (Q01.act h), fun p y h => by + obtain ⟨x', hx', g⟩ := Q12.rt p y h + obtain ⟨x0, hx0, g0⟩ := Q01.rt p x' hx' + exact ⟨x0, hx0, fun h => g (g0 h)⟩, Q01.dom.trans Q12.dom⟩ + have hcons : σ2.consumedTime = σ.consumedTime := by rw [K.frame] <;> rfl + have hRdy : RdyA σ := J.sidle.rdyA J.sinv + have hfk : ∀ p, ¬ M p → ∀ x' y, σ.kheap p = some (.tcb x') → σ2.kheap p = some (.tcb y) → + y.tcbFault = x'.tcbFault := by + intro p hp x' y hx hy + have hpt : p ≠ specPtr t := fun e => hp (e ▸ hMt) + obtain ⟨y', hy', g⟩ := K.tcbF (show σ1.kheap p = some (.tcb x') by rw [far p hpt]; exact hx) + rw [hy] at hy' + cases hy' + exact g.fault + have sinv : SInvXA σ2 m n := K13.sinv J.sinv notM hMI hMq hN hN' J.noSwitch hMrls (Or.inl hcons) hRdy + (K13.faultStA J.sinv.faultSt hfk hMf) + have hIdle : ∀ c < fl4A64Platform.numNodes, ¬ M (m.idleThreadOf c) := fun c hc h => by + have hcur : m.curCore < fl4A64Platform.numNodes := by rw [J.j.1.sch.core]; exact hCs _ J.j.1.sch.cpuCs + have hcc : c = m.curCore := by + have a : c < 1 := h1 ▸ hc + have b : m.curCore < 1 := h1 ▸ hcur + exact (Nat.lt_one_iff.1 a).trans (Nat.lt_one_iff.1 b).symm + rw [hcc, ← J.sinv.view] at h + exact hMI h + have idle : IdleNoFaultA m σ2 := fun c hc => by + obtain ⟨y, hy, hf⟩ := J.idleNoFault c hc + obtain ⟨y', hy', -⟩ := K13.tcbF hy + exact ⟨y', hy', (hfk _ (hIdle c hc) y y' hy hy').trans hf⟩ + exact J.toJStage3A.of_ipcStepUXSA (Q := Q) (Rl := Rl) (curOf := Z.curOf) (curScOf := Z.curScOf) wall + (HS1.trans HS) (SSz1.trans SSz) Q02 hz (fun _ _ => rfl) (BlkOK.bxi P1.2.2.1) P1.1 P1.2.1 + (J.sys.ent.user.ksb wall.1) j7 sr E2 (J.toJStage3A.ntfn_ipcXMA wX htZ K13 E2 wk hMs hMs') (reply K13) hdr + (SIKXA.ipcKeepN K13) idle (J.toJStage3A.tw_ipcXA wX htZ) sinv (K13.sidle J.sidle notM hRdy) + (J.toJStage3A.runVcpu_ipcXA wX htZ K13 E2 hMv) (blkQ K13) sv + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3FieldsA.lean b/proofs/Fl4ProofsA64/P3/Stage3FieldsA.lean new file mode 100644 index 000000000..79bb59e37 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3FieldsA.lean @@ -0,0 +1,361 @@ +/- +P3 (aarch64, A64-P3 Stage-3 D8/D1): **the record clauses added in one batch to `JStage3A`** (`P3/Stage3A`), with +their step-keeping lemmas. A leaf module upstream of the record. + +* **[J] `IdleAllA`** (J13; the Untyped line's `P3/UtIdleAllA`, structure unchanged): every core's idle thread and idle + SC lie in the idle area. +* **[J] `IpcHeapCovA`** (delete-hooks contract §3, N1): the converse of `UtIpcHeapA`. +* **[J] `ZombieTcbNoVcpuA`** (§3, N2): a TCB under a TCB zombie has no VCPU. +* **[J] `ZombieTcbTA`**: every `OS` TCB zombie's base is a heap TCB (the TCB arm of A64 P1's `DelInvP1`). It is what + keeps `ZombieTcbNoVcpuA` across a step: `CnOmega` only places the base in `Ω` or the TCBs. +* **[J] `BufOffA`**: the tracked IPC-buffer words are off the `OS` slots, the VSpace relation's words, the table pages + and the VCPU words; and (record edit #8b-C2, 12′) each tracked buffer is pinned (**`BufPinA`**) inside the non-device + Frame cap of a live TCB's `SLOT_BUFFER` slot, so the tracked words are frame words (`BufOffA.frameWordsA`, + `BufOffA.ubA`). +* `SchedPageA` moved here from `P3/Stage3J7A` (unchanged). + +The other new fields reuse `FrBaseApartA` / `FinCovA` (`P3/CnodeFinalA`) and `DevFrDisjA` (`P3/DevFrDisjA`). +-/ +import Fl4ProofsA64.P3.CnodeFinalA +import Fl4ProofsA64.P3.DevFrDisjA +import Fl4ProofsA64.P3.JointA +import Fl4ProofsA64.P3.VsRelA +import Fl4ProofsA64.P3.EpRelA +import Fl4ProofsA64.P1.QueueVcTy +import Fl4ProofsA64.P1.RunVcpu +import Fl4ProofsA64.P1.MdbFrK +import Fl4ProofsA64.P1.MdbFrUb + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +variable (st) in +/-- The pages whose words the scheduler frames need off the scheduler write set. -/ +def SchedPageA (Z : XSetsA) (OS : Set U64) (s : S) (p : U64) : Prop := + p ∈ Z.Pp ∨ p ∈ Z.S1 ∨ p ∈ Z.T2 ∨ p ∈ Z.X ∨ (∃ r ∈ Z.X, p = wa r 4096#u64) ∨ p ∈ Z.Y ∨ + (∃ y ∈ Z.Y, p = wa y 4096#u64) ∨ + ∃ u ∈ OS, (∃ m a v, capA st s u = .PageTable p m a v) ∨ (∃ m a, capA st s u = .Pml4 p m a) ∨ + (∃ a v, capA st s u = .Ept p 0#u64 false a v) ∨ + (∃ q a v, capA st s u = .Ept q 0#u64 false a v ∧ p = wa q 4096#u64) ∨ + ∃ y a, capA st s u = .HighVSpace y false a ∧ p = wa y 4096#u64 + +variable (st) in +/-- **The pin of a tracked buffer** (12′): the 128 words of the 1 KiB buffer at `b` are aligned words of the +non-device Frame cap in the `SLOT_BUFFER` slot of a TCB of `T`. -/ +def BufPinA (T OS : Set U64) (s : S) (b : U64) : Prop := + ∃ t ∈ T, sbWordA t ∈ OS ∧ ∃ ptr size rights x1 x2 x3 x4 bits, + capA st s (sbWordA t) = .Frame ptr size rights false x1 x2 x3 x4 ∧ size.bits = ok bits ∧ + ∀ k : U64, k.val < 128 → Aligned (bufW b k) ∧ ptr.val ≤ (bufW b k).val ∧ (bufW b k).val < ptr.val + 2 ^ bits.val + +/-- `BufPinA` along a step keeping each pinning slot's frame (its pointer and size; rights and mapping may change). -/ +theorem BufPinA.keepF {T T' OS OS' : Set U64} {s s' : S} {b : U64} (h : BufPinA st T OS s b) + (hk : ∀ t ∈ T, sbWordA t ∈ OS → ∀ ptr size rights x1 x2 x3 x4, + capA st s (sbWordA t) = .Frame ptr size rights false x1 x2 x3 x4 → + t ∈ T' ∧ sbWordA t ∈ OS' ∧ + ∃ r y1 y2 y3 y4, capA st s' (sbWordA t) = .Frame ptr size r false y1 y2 y3 y4) : + BufPinA st T' OS' s' b := by + obtain ⟨t, ht, hO, ptr, size, rights, x1, x2, x3, x4, bits, hc, hs, hw⟩ := h + obtain ⟨ht', hO', r, y1, y2, y3, y4, hc'⟩ := hk t ht hO ptr size rights x1 x2 x3 x4 hc + exact ⟨t, ht', hO', ptr, size, r, y1, y2, y3, y4, bits, hc', hs, hw⟩ + +/-- `BufPinA` along a step keeping every pinning slot's cap. -/ +theorem BufPinA.keepC {T T' OS OS' : Set U64} {s s' : S} {b : U64} (h : BufPinA st T OS s b) + (eT : T' = T) (eO : OS' = OS) (hc : ∀ t ∈ T, sbWordA t ∈ OS → capA st s' (sbWordA t) = capA st s (sbWordA t)) : + BufPinA st T' OS' s' b := + h.keepF fun t ht hO _ _ _ _ _ _ _ e => ⟨eT ▸ ht, eO ▸ hO, _, _, _, _, _, (hc t ht hO).trans e⟩ + +/-- The 128 words of a pinned buffer lie in every `Ub` bounding the live frames. -/ +theorem BufPinA.ubA {T OS Ub : Set U64} {s : S} {b : U64} (h : BufPinA st T OS s b) + (hF : Fl4ProofsA64.P1.W7.R.ST.FrUb st Ub OS s) {k : U64} (hk : k.val < 128) : bufW b k ∈ Ub := by + obtain ⟨t, -, hO, ptr, size, rights, x1, x2, x3, x4, bits, hc, hs, hw⟩ := h + obtain ⟨al, h1, h2⟩ := hw k hk + exact hF _ hO ptr size rights x1 x2 x3 x4 hc bits hs _ al h1 h2 + +variable (st) in +/-- **[J]** The buffer words' placement (module docstring). -/ +structure BufOffA (Z : XSetsA) (OS : Set U64) (s : S) (Bf : Set (U64 × Fl4SpecA64.Word)) : Prop where + os : ∀ b, BufW Bf b → ¬ InOS OS b + vs : ∀ b, BufW Bf b → ¬ VsFootA Z.Pp Z.S1 Z.Y (L.ksb s) b + page : ∀ b, BufW Bf b → ∀ p, SchedPageA st Z OS s p → ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → b ≠ wa p o + vcw : ∀ b, BufW Bf b → ∀ v ∈ Z.V, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → b ≠ wa v o + /-- (record edit #8b-C2, 12′) Every tracked buffer is pinned in a live TCB's `SLOT_BUFFER` frame. -/ + pin : ∀ bp ∈ Bf, BufPinA st Z.T OS s bp.1 + +/-- (12′) **The tracked buffer words are frame words of `OS`.** -/ +theorem BufOffA.frameWordsA {Z : XSetsA} {OS : Set U64} {s : S} {Bf : Set (U64 × Fl4SpecA64.Word)} + (B : BufOffA st Z OS s Bf) {b : U64} (hb : BufW Bf b) : + b ∈ Fl4ProofsA64.P1.W7.R.ST.FrameWords st OS s := by + obtain ⟨bp, hbp, k, hk, rfl⟩ := hb + obtain ⟨t, -, hO, ptr, size, rights, x1, x2, x3, x4, bits, hc, hs, hw⟩ := B.pin bp hbp + obtain ⟨al, h1, h2⟩ := hw k hk + exact ⟨al, _, hO, ptr, size, rights, x1, x2, x3, x4, bits, hc, hs, h1, h2⟩ + +/-- (12′) **The tracked buffer words are in every user-frame set `Ub` bounding the live frames.** -/ +theorem BufOffA.ubA {Z : XSetsA} {OS Ub : Set U64} {s : S} {Bf : Set (U64 × Fl4SpecA64.Word)} + (B : BufOffA st Z OS s Bf) (hF : Fl4ProofsA64.P1.W7.R.ST.FrUb st Ub OS s) {b : U64} (hb : BufW Bf b) : + b ∈ Ub := by + obtain ⟨bp, hbp, k, hk, rfl⟩ := hb + exact (B.pin bp hbp).ubA hF hk + +variable (st) in +/-- **[J] J13** (module docstring). -/ +structure IdleAllA (ia ie : Nat) (Cs : Set U64) (m : Fl4SpecA64.McState) (s : S) : Prop where + tcb : ∀ c ∈ Cs, ia ≤ (L.mem s (L.nodeAt s c layout.node.IDLE_THREAD)).val ∧ + (L.mem s (L.nodeAt s c layout.node.IDLE_THREAD)).val < ie + sc : ∀ c ∈ Cs, ia ≤ (L.mem s (L.nodeAt s c layout.node.IDLE_SC)).val ∧ + (L.mem s (L.nodeAt s c layout.node.IDLE_SC)).val < ie + spec : ∀ c < Fl4SpecA64.fl4A64Platform.numNodes, + ∃ t : U64, m.idleThreadOf c = specPtr t ∧ ia ≤ t.val ∧ t.val < ie + +/-- **[J] N1** (module docstring). -/ +structure IpcHeapCovA (H : Heap) (N E Rp : Set U64) : Prop where + ntfn : ∀ p, H p = some .ntfn → p ∈ N + ep : ∀ p, H p = some .ep → p ∈ E + reply : ∀ p, H p = some .reply → p ∈ Rp + +/-- **The heap object a pointing cap names**: an Endpoint, Notification, Reply or SchedContext cap points at a heap +object of that kind (Thread: `CapValidA`; IrqHandler: `HandlersActiveA`; Vcpu: J7's `S1aArmCol.vh`). -/ +def CapKindA (H : Heap) (c : capability.Cap) : Prop := + (∀ p b x y z w, c = .Endpoint p b x y z w → H p = some .ep) ∧ + (∀ p b x y, c = .Notification p b x y → H p = some .ntfn) ∧ + (∀ p g, c = .Reply p g → H p = some .reply) ∧ + (∀ p sz, c = .SchedContext p sz → H p = some (.sc sz.val)) ∧ + (∀ cpu, c = .SchedControl cpu → cpu.val < Fl4SpecA64.fl4A64Platform.numNodes) + +variable (st) in +/-- **[J] cap typing** (Architect 17:31Z): every `OS` cap satisfies `CapKindA`. -/ +def CapTyA (H : Heap) (OS : Set U64) (s : S) : Prop := + ∀ u ∈ OS, CapKindA H (capA st s u) + +variable (st) in +/-- **[J] The VCPU binding words** (record edit #3, Architect 19:30Z; A64 P1's `VcTy.bnd` and `VcObj`'s +`RUN_CALLER` clause): a TCB's VCPU word (`t + 3664`) is `0` or a VCPU of `V`; a VCPU's `RUN_CALLER` word (`v + 24`) +is `0` or a TCB of `T`. -/ +structure VcRunA (T V : Set U64) (s : S) : Prop where + tcb : ∀ t ∈ T, L.mem s (wa t 3664#u64) = 0#u64 ∨ L.mem s (wa t 3664#u64) ∈ V + vcpu : ∀ v ∈ V, L.mem s (wa v 24#u64) = 0#u64 ∨ L.mem s (wa v 24#u64) ∈ T + /-- (record edit #5) A VCPU's `TCB` word (`v + 0`) is null or a TCB. -/ + vtcb : ∀ v ∈ V, L.mem s v = 0#u64 ∨ L.mem s v ∈ T + /-- (record edit #5) A TCB's `RUN_VCPU` word is null or a VCPU. -/ + run : ∀ t ∈ T, L.mem s (runVcpuA t) = 0#u64 ∨ L.mem s (runVcpuA t) ∈ V + /-- (record edit #8b-C, item 18, `bind_run_disj`) A TCB bound to a VCPU (`+3664`) is not running one. -/ + disj : ∀ t ∈ T, L.mem s (wa t 3664#u64) ≠ 0#u64 → L.mem s (runVcpuA t) = 0#u64 + +variable (st) in +/-- **[J] N2** (module docstring). -/ +def ZombieTcbNoVcpuA (OS : Set U64) (s : S) : Prop := + ∀ u ∈ OS, ∀ p n, capA st s u = .Zombie p capability.ZOMBIE_TCB n → L.mem s (wa p 3664#u64) = 0#u64 + +variable (st) in +/-- **[J]** (module docstring) Every `OS` TCB zombie's base is a heap TCB. -/ +def ZombieTcbTA (H : Heap) (OS : Set U64) (s : S) : Prop := + ∀ u ∈ OS, ∀ p n, capA st s u = .Zombie p capability.ZOMBIE_TCB n → p ∈ H.tcbs + +variable (st) in +/-- **[J] D1b-4**: device untyped regions are apart from the record (x86 `DevApart` / `DevSep`). It is stated here, +not in `P3/Stage3D1bA`: `JointObjectA` is downstream of that module. -/ +structure DevUtOffA (Z : XSetsA) (H : Heap) (OS : Set U64) (ia ie : Nat) (s : S) : Prop where + obj : ∀ u ∈ OS, ∀ q b fi, capA st s u = .Untyped q b fi true → ∀ p bytes, JointObjectA Z H p bytes → + p.val + bytes ≤ q.val ∨ q.val + 2 ^ b.val ≤ p.val + ks : ∀ u ∈ OS, ∀ q b fi, capA st s u = .Untyped q b fi true → + RegionOffNodes st Z.Cs s q.val (q.val + 2 ^ b.val) + idle : ∀ u ∈ OS, ∀ q b fi, capA st s u = .Untyped q b fi true → ie ≤ q.val ∨ q.val + 2 ^ b.val ≤ ia + sep : ∀ u ∈ OS, ∀ q b fi, capA st s u = .Untyped q b fi true → ∀ v ∈ OS, ∀ q' b' fi', + capA st s v = .Untyped q' b' fi' false → q'.val + 2 ^ b'.val ≤ q.val ∨ q.val + 2 ^ b.val ≤ q'.val + +/-! ## Keeping the clauses across a step -/ + +variable {s s' : S} {OS : Set U64} + +theorem FrBaseApartA.keepA (capE : ∀ u ∈ OS, capA st s' u = capA st s u) (h : FrBaseApartA st OS s) : + FrBaseApartA st OS s' := fun y hy z hz fy kz => by + rw [capE y hy] at fy + rw [capE z hz] at kz + rw [capE y hy, capE z hz] + exact h y hy z hz fy kz + +variable (st) in +/-- **[J] (record edit #6, item 12) `UserFrSpecA`**, the non-device twin of `DevFrDisjA`: every `OS` non-device frame +cap's page is a spec non-device data page of the cap's size. -/ +def UserFrSpecA (OS : Set U64) (s : S) (σ : Fl4SpecA64.State Unit) : Prop := + ∀ y ∈ OS, ∀ p size rights mapped ept asid va, + capA st s y = .Frame p size rights false mapped ept asid va → + σ.kheap (specPtr p) = some (.archObj (.dataPage false (frameSizeOfA size))) + +theorem UserFrSpecA.keepA {σ σ' : Fl4SpecA64.State Unit} (capE : ∀ u ∈ OS, capA st s' u = capA st s u) + (dp : ∀ p dev pg, σ.kheap p = some (.archObj (.dataPage dev pg)) → + σ'.kheap p = some (.archObj (.dataPage dev pg))) + (h : UserFrSpecA st OS s σ) : UserFrSpecA st OS s' σ' := fun y hy p size rights mapped ept asid va hc => + dp _ _ _ (h y hy p size rights mapped ept asid va ((capE y hy).symm.trans hc)) + +variable (st) in +/-- **[J] (frth, Architect 21:53Z) `FrThA`**: every live non-device Frame cap of `OS` names an `other` object of the +typed heap `th` of its full size `2 ^ bits` (`size.bits = ok bits`; A64 frame types 7 / 9 / 10). -/ +def FrThA (th : TypedHeapA) (OS : Set U64) (s : S) : Prop := + ∀ y ∈ OS, ∀ p size rights x1 x2 x3 x4 bits, + capA st s y = .Frame p size rights false x1 x2 x3 x4 → size.bits = ok bits → + th p = some (.other (2 ^ bits.val)) + +/-- A frame cap after was a frame cap of the same page and size before (any other fields): `FrThA` is kept when the +typed heap keeps the `other` objects. -/ +theorem FrThA.ofSrc {th th' : TypedHeapA} {s' : S} + (src : ∀ y ∈ OS, ∀ p size rights x1 x2 x3 x4, capA st s' y = .Frame p size rights false x1 x2 x3 x4 → + ∃ z ∈ OS, ∃ r0 a b c d, capA st s z = .Frame p size r0 false a b c d) + (thE : ∀ p z, th p = some (.other z) → th' p = some (.other z)) + (h : FrThA st th OS s) : FrThA st th' OS s' := fun y hy p size rights x1 x2 x3 x4 bits hc hb => by + obtain ⟨z, hz, r0, a, b, c, d, e⟩ := src y hy p size rights x1 x2 x3 x4 hc + exact thE _ _ (h z hz p size r0 a b c d bits e hb) + +theorem FrThA.keepA {th th' : TypedHeapA} (capE : ∀ u ∈ OS, capA st s' u = capA st s u) + (thE : ∀ p z, th p = some (.other z) → th' p = some (.other z)) + (h : FrThA st th OS s) : FrThA st th' OS s' := + FrThA.ofSrc (fun y hy p size rights x1 x2 x3 x4 hc => ⟨y, hy, rights, x1, x2, x3, x4, (capE y hy).symm.trans hc⟩) thE h + +theorem DevFrDisjA.keepA {H : Heap} {σ σ' : Fl4SpecA64.State Unit} (capE : ∀ u ∈ OS, capA st s' u = capA st s u) + (dp : ∀ p dev pg, σ.kheap p = some (.archObj (.dataPage dev pg)) → + σ'.kheap p = some (.archObj (.dataPage dev pg))) + (h : DevFrDisjA st H OS s σ) : DevFrDisjA st H OS s' σ' := by + intro y hy p size rights mapped ept asid va hc k hk + obtain ⟨h1, dev, pg, h2⟩ := h y hy p size rights mapped ept asid va ((capE y hy).symm.trans hc) k hk + exact ⟨h1, dev, pg, dp _ _ _ h2⟩ + +theorem FinCovA.keepA {th : TypedHeapA} {σ σ' : Fl4SpecA64.State Unit} + (cn : ∀ q r cs, σ'.kheap q = some (.cnode r cs) → σ.kheap q = some (.cnode r cs)) + (tc : ∀ q x, σ'.kheap q = some (.tcb x) → ∃ y, σ.kheap q = some (.tcb y)) + (h : FinCovA th σ) : FinCovA th σ' := + ⟨fun q r cs hk w => h.cnode q r cs (cn q r cs hk) w, fun q x hk => let ⟨y, hy⟩ := tc q x hk; h.tcb q y hy⟩ + +theorem IdleAllA.keepA {ia ie : Nat} {Cs : Set U64} {m : Fl4SpecA64.McState} + (t : ∀ c ∈ Cs, L.mem s' (L.nodeAt s' c layout.node.IDLE_THREAD) = L.mem s (L.nodeAt s c layout.node.IDLE_THREAD)) + (k : ∀ c ∈ Cs, L.mem s' (L.nodeAt s' c layout.node.IDLE_SC) = L.mem s (L.nodeAt s c layout.node.IDLE_SC)) + (h : IdleAllA st ia ie Cs m s) : IdleAllA st ia ie Cs m s' := + ⟨fun c hc => by rw [t c hc]; exact h.tcb c hc, fun c hc => by rw [k c hc]; exact h.sc c hc, h.spec⟩ + +theorem DevUtOffA.keepA {Z Z' : XSetsA} {H : Heap} {ia ie : Nat} (capE : ∀ u ∈ OS, capA st s' u = capA st s u) + (hk : L.ksb s' = L.ksb s) (eJ : ∀ p bytes, JointObjectA Z' H p bytes → JointObjectA Z H p bytes) + (eC : Z'.Cs = Z.Cs) (h : DevUtOffA st Z H OS ia ie s) : DevUtOffA st Z' H OS ia ie s' := by + refine ⟨fun u hu q b fi hc p bytes hj => h.obj u hu q b fi ((capE u hu).symm.trans hc) p bytes (eJ p bytes hj), + fun u hu q b fi hc => ?_, fun u hu q b fi hc => h.idle u hu q b fi ((capE u hu).symm.trans hc), + fun u hu q b fi hc v hv q' b' fi' hc' => + h.sep u hu q b fi ((capE u hu).symm.trans hc) v hv q' b' fi' ((capE v hv).symm.trans hc')⟩ + have hnb : ∀ c, L.nodeBase s' c = L.nodeBase s c := fun c => by simp [LawfulStore.nodeBase, hk] + have r := h.ks u hu q b fi ((capE u hu).symm.trans hc) + unfold RegionOffNodes at r ⊢ + rw [eC, hk] + exact ⟨fun c hcs => by rw [hnb c]; exact r.1 c hcs, r.2⟩ + +theorem ZombieTcbTA.keepA {H : Heap} (capE : ∀ u ∈ OS, capA st s' u = capA st s u) (h : ZombieTcbTA st H OS s) : + ZombieTcbTA st H OS s' := fun u hu p n hc => h u hu p n ((capE u hu).symm.trans hc) + +theorem ZombieTcbNoVcpuA.keepA {H : Heap} (capE : ∀ u ∈ OS, capA st s' u = capA st s u) + (hT : ZombieTcbTA st H OS s) (w : ∀ t ∈ H.tcbs, L.mem s' (wa t 3664#u64) = L.mem s (wa t 3664#u64)) + (h : ZombieTcbNoVcpuA st OS s) : ZombieTcbNoVcpuA st OS s' := fun u hu p n hc => by + have hc0 := (capE u hu).symm.trans hc + rw [w p (hT u hu p n hc0)] + exact h u hu p n hc0 + +theorem BufOffA.keepA {Z Z' : XSetsA} {Bf : Set (U64 × Fl4SpecA64.Word)} (hk : L.ksb s' = L.ksb s) + (e1 : Z'.Pp = Z.Pp) (e2 : Z'.S1 = Z.S1) (e3 : Z'.T2 = Z.T2) (e4 : Z'.X = Z.X) (e5 : Z'.Y = Z.Y) (e6 : Z'.V = Z.V) + (e7 : Z'.T = Z.T) (capE : ∀ u ∈ OS, capA st s' u = capA st s u) (h : BufOffA st Z OS s Bf) : + BufOffA st Z' OS s' Bf := by + refine ⟨h.os, fun b hb => by rw [e1, e2, e5, hk]; exact h.vs b hb, fun b hb p hp => ?_, + fun b hb => by rw [e6]; exact h.vcw b hb, + fun bp hbp => (h.pin bp hbp).keepC e7 rfl fun _ _ hO => capE _ hO⟩ + refine h.page b hb p ?_ + unfold SchedPageA at hp ⊢ + rw [e1, e2, e3, e4, e5] at hp + rcases hp with hp | hp | hp | hp | hp | hp | hp | ⟨u, hu, hp⟩ + · exact Or.inl hp + · exact Or.inr (Or.inl hp) + · exact Or.inr (Or.inr (Or.inl hp)) + · exact Or.inr (Or.inr (Or.inr (Or.inl hp))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inl hp)))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl hp))))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl hp)))))) + · rw [capE u hu] at hp + exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ⟨u, hu, hp⟩)))))) + +/-- `CapTyA` across a step keeping every `OS` cap. -/ +theorem CapTyA.keepA {H : Heap} (h : CapTyA st H OS s) (e : ∀ u ∈ OS, capA st s' u = capA st s u) : + CapTyA st H OS s' := fun u hu => by rw [e u hu]; exact h u hu + +/-- `VcRunA` across a step keeping the TCBs' VCPU words and the VCPUs' `RUN_CALLER` words. -/ +theorem VcRunA.keepA {T V : Set U64} (h : VcRunA st T V s) + (ht : ∀ t ∈ T, L.mem s' (wa t 3664#u64) = L.mem s (wa t 3664#u64)) + (hv : ∀ v ∈ V, L.mem s' (wa v 24#u64) = L.mem s (wa v 24#u64)) + (hv0 : ∀ v ∈ V, L.mem s' v = L.mem s v) + (hr : ∀ t ∈ T, L.mem s' (runVcpuA t) = L.mem s (runVcpuA t)) : VcRunA st T V s' := + ⟨fun t h1 => by rw [ht t h1]; exact h.tcb t h1, fun v h1 => by rw [hv v h1]; exact h.vcpu v h1, + fun v h1 => by rw [hv0 v h1]; exact h.vtcb v h1, fun t h1 => by rw [hr t h1]; exact h.run t h1, + fun t h1 h0 => by rw [ht t h1] at h0; rw [hr t h1]; exact h.disj t h1 h0⟩ + +variable (st) in +/-- **[J] TCB → VCPU symmetry** (record edit #4, Architect 20:21Z): a TCB's VCPU names it back (word `0`). -/ +def VcSym1A (T : Set U64) (s : S) : Prop := + ∀ t ∈ T, L.mem s (wa t 3664#u64) ≠ 0#u64 → L.mem s (L.mem s (wa t 3664#u64)) = t + +variable (st) in +/-- **[J] VCPU → TCB symmetry** (record edit #4): a VCPU's `RUN_CALLER` thread `c` has it as its `RUN_VCPU`. -/ +def VcSym2A (V : Set U64) (s : S) : Prop := + ∀ v ∈ V, L.mem s (wa v 24#u64) ≠ 0#u64 → L.mem s (runVcpuA (L.mem s (wa v 24#u64))) = v + +theorem wa_zeroVcA (v : U64) : wa v 0#u64 = v := by + apply UScalar.eq_of_val_eq + simp [core.num.U64.wrapping_add_val_eq, U64.size_def, U64.numBits] + +theorem VcSym1A.keepA {T V : Set U64} (h : VcSym1A st T s) (r : VcRunA st T V s) + (ht : ∀ t ∈ T, L.mem s' (wa t 3664#u64) = L.mem s (wa t 3664#u64)) + (hv : ∀ v ∈ V, L.mem s' v = L.mem s v) : VcSym1A st T s' := fun t h1 h0 => by + rw [ht t h1] at h0 ⊢ + rcases r.tcb t h1 with e | e + · exact absurd e h0 + · rw [hv _ e]; exact h t h1 h0 + +theorem VcSym2A.keepA {T V : Set U64} (h : VcSym2A st V s) (r : VcRunA st T V s) + (hv : ∀ v ∈ V, L.mem s' (wa v 24#u64) = L.mem s (wa v 24#u64)) + (hc : ∀ t ∈ T, L.mem s' (runVcpuA t) = L.mem s (runVcpuA t)) : VcSym2A st V s' := fun v h1 h0 => by + rw [hv v h1] at h0 ⊢ + rcases r.vcpu v h1 with e | e + · exact absurd e h0 + · rw [hc _ e]; exact h v h1 h0 + +/-- **[J] blocked ⇒ queued** (record edit #4): a TCB of `T` blocked on an endpoint is in that endpoint's queue (and its +block word names an endpoint of `E`); likewise for notifications (`N`). The converse of `EpStateRel` / `NtfnStateRel`'s +queue membership. -/ +structure BlkQueuedA (T E N : Set U64) (m : U64 → U64) (σ : Fl4SpecA64.State Unit) : Prop where + ep : ∀ t ∈ T, ∀ x, σ.kheap (specPtr t) = some (.tcb x) → ∀ e, Fl4SpecA64.epBlocked x.tcbState = some e → + m (Notif.tBlockA t) ∈ E ∧ ∃ y, σ.kheap e = some (.endpoint y) ∧ specPtr t ∈ y.epQueue + ntfn : ∀ t ∈ T, ∀ x, σ.kheap (specPtr t) = some (.tcb x) → ∀ p, x.tcbState = .blockedOnNotification p → + m (Notif.tBlockA t) ∈ N ∧ + ∃ y q, σ.kheap p = some (.notification y) ∧ y.ntfnObj = .waitingNtfn q ∧ specPtr t ∈ q + +/-- `BlkQueuedA` across a step keeping the block words, the endpoint and notification objects and every TCB's state +(or writing `Restart`). -/ +theorem BlkQueuedA.keepA {T E N : Set U64} {m m' : U64 → U64} {σ σ' : Fl4SpecA64.State Unit} + (h : BlkQueuedA T E N m σ) (hm : ∀ t ∈ T, m' (Notif.tBlockA t) = m (Notif.tBlockA t)) + (ht : ∀ t ∈ T, ∀ x', σ'.kheap (specPtr t) = some (.tcb x') → x'.tcbState = .restart ∨ + ∃ x, σ.kheap (specPtr t) = some (.tcb x) ∧ x.tcbState = x'.tcbState) + (he : ∀ p y, σ.kheap p = some (.endpoint y) → σ'.kheap p = some (.endpoint y)) + (hn : ∀ p y, σ.kheap p = some (.notification y) → σ'.kheap p = some (.notification y)) : + BlkQueuedA T E N m' σ' where + ep := fun t h1 x' hk e hb => by + rcases ht t h1 x' hk with r | ⟨x, hx, es⟩ + · rw [r] at hb; simp [Fl4SpecA64.epBlocked] at hb + · obtain ⟨a, y, b, c⟩ := h.ep t h1 x hx e (es ▸ hb) + exact ⟨by rw [hm t h1]; exact a, y, he _ _ b, c⟩ + ntfn := fun t h1 x' hk p hb => by + rcases ht t h1 x' hk with r | ⟨x, hx, es⟩ + · rw [r] at hb; cases hb + · obtain ⟨a, y, q, b, c, d⟩ := h.ntfn t h1 x hx p (es.trans hb) + exact ⟨by rw [hm t h1]; exact a, y, q, hn _ _ b, c, d⟩ + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3FinA.lean b/proofs/Fl4ProofsA64/P3/Stage3FinA.lean new file mode 100644 index 000000000..426e8a937 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3FinA.lean @@ -0,0 +1,174 @@ +/- +P3 (aarch64, A64-P3 Stage 3, T3 steps 2–5): **the arms' final-state facts, once**, for the heap-keeping decode arms +(`DecArmS1aEnt`) at a syscall entry (`JSyscallA`). + +* **`JSyscallA.decPreA`** (step 3): `DecPre` at the decode site, the G4 facts (`act` / `running` / `curSc` / `cur`) + read off the record. +* **`JSyscallA.decodeFinA`** (step 2): from `decode_invocation st ar s … = ok (r, s3, a3)`, the arms' `fin` bundle at + `s3` and any spec state: `SysL`, `SymL`, `BXI` at some `Ac'`, `UserOK U` and `J7A st (J7R2A st ar hPl.ipa)` (and + `ksb s3 = ksb s`). The arm supplies the decode-site facts (the invoked slot and its `SlotCov`, the extra slots, + `hIq`, `hScC`). New scheduler pages at `s3` are covered by R2 at `s3` (`schedPage_ptA`). +* Step 5: `J7R2A`'s Stage-2 width is instantiated at `hPl.ipa` ([P] `ArchPlat`; the arm tie is `hPl.ipa_eq a`). + `S2IpaE` stays its own [P] (Architect ruling, T3 Gate-1 (v)). +* **`Sched.exit_noSwitch`** (step 4): after `schedule` and `activate_thread` (`exit_spec`) no thread switch is pending, + which is `JStage3TA.noSwitch` at the next syscall entry; **`Sched.JSyscallA.ofSchedExit`** builds `JSyscallA` with + `act` taken from that exit. + +Named premises: [P] `ArchTotal`, `MaxSpi`, `ArchPlat`, `A64FrameMasks`; `exit_spec`'s (`FIA`, `hAwk`, `hSw`, `hidle`). +-/ +import Fl4ProofsA64.P3.Stage3SchedExitFieldsA +import Fl4ProofsA64.P3.Stage3J7StepA +import Fl4ProofsA64.P3.Stage3DecExtraA +import Fl4ProofsA64.P3.Stage3DelInA +import Fl4ProofsA64.P3.Stage3SysPlaceA +import Fl4ProofsA64.P3.Stage3OffAsEA +import Fl4ProofsA64.P1.S1aStepPool +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-- A `Result` triple at an `ok` run. -/ +theorem post_of_okA {α : Type} {m : Result α} {x : α} {P : α → Prop} (h : m ⦃ r => P r ⦄) (e : m = ok x) : P x := by + rw [e] at h + exact (WP.spec_ok _).1 h + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J7 : JSlotA S} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- **Scheduler pages after an S1a step lie in `Pt`**: the record's table sets do (`hZ`, the entry's `J7A` link), and +R2 at the post state places every table cap of `OS`. -/ +theorem schedPage_ptA {Z : XSetsA} {H : Heap} {OS Pt Pp' Tb' Hr Te Kt : Set U64} {Lv' Re bits : U64 → Nat} {n : U64} + {s s' : S} (R : S1aR2 st H OS Pt Pp' Tb' Hr Te Kt Lv' Re bits n s') + (link : ∀ p, SchedPageA st Z OS s p → p ∈ Pt) : ∀ p, SchedPageA st Z OS s' p → p ∈ Pt := by + intro p hp + rcases hp with h | h | h | h | h | h | h | ⟨u, hu, h⟩ + · exact link p (Or.inl h) + · exact link p (Or.inr (Or.inl h)) + · exact link p (Or.inr (Or.inr (Or.inl h))) + · exact link p (Or.inr (Or.inr (Or.inr (Or.inl h)))) + · exact link p (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl h))))) + · exact link p (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl h)))))) + · exact link p (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl h))))))) + · rcases h with ⟨m, a, v, h⟩ | ⟨m, a, h⟩ | ⟨a, v, h⟩ | ⟨q, a, v, h, e⟩ | ⟨y, a, h, e⟩ + · exact R.pageTables u hu p m a v h + · exact (R.nativeRoots u hu p m a h).2 + · exact (R.eptRoots u hu p _ _ _ _ h).2 ⟨p, rfl, Or.inl rfl⟩ + · exact (R.eptRoots u hu q _ _ _ _ h).2 ⟨q, rfl, Or.inr ⟨rfl, e⟩⟩ + · exact R.highFootprint ⟨y, by rw [R.highRoots]; exact ⟨u, hu, false, a, h⟩, Or.inr ⟨rfl, e⟩⟩ + +/-- **`DecPre` at a syscall entry** (module docstring). -/ +theorem JSyscallA.decPreA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + {x : U64} (hx : x ∈ OS) {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hext : ∀ y ∈ extra.val, y ≠ 0#u64 → y ∈ OS) : + DecPre st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits U Ac s (capA st s x) extra := + J.toJStage3EntryA.decPreA J.actW J.running J.curSc J.cur hx hext + +/-- **The `fin` bundle of the heap-keeping decode arms** (module docstring). -/ +theorem JSyscallA.decodeFinA {ar : machine.Arch A} (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) + s a σ m) + (hfm : A64FrameMasks st s) + {cap : capability.Cap} {extra : Aeneas.Std.Array obj.Slot 3#usize} {slot : U64} + (hsl : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = cap) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + {lbl : U64} (hc : DecArmS1aEnt cap lbl) + (hIq : ∀ irq, cap = .IrqHandler irq → + (lbl = fl4_abi.label.IRQClearIRQHandler ∨ lbl = fl4_abi.label.IRQSetIRQHandler) → irq.val < 5376) + (hScC : ∀ cpu, cap = .SchedControl cpu → cpu ∈ Z.Cs ∧ + ∀ x ∈ extra.val, x ≠ 0#u64 → ∀ p sz, capA st s x = .SchedContext p sz → sz.val = H.scBits p) + {len : U64} {blk call don fp : Bool} {buf : U64} {r : core.result.Result Unit error.Exc} {s3 : S} {a3 : A} + (hd : syscall.decode_invocation st ar s a lbl len slot cap blk call don fp buf extra = ok (r, s3, a3)) + (σ3 : Fl4SpecA64.State Unit) (m3 : Fl4SpecA64.McState) : + L.ksb s3 = L.ksb s ∧ ∃ Ac' : Set U64, SysL st H Z.Cs OS ia ie s3 ∧ SymL st H Z.Cs s3 ∧ + BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac' s3 ∧ + Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s3 ∧ + J7A st (J7R2A st ar hPl.ipa) Z H OS s3 σ3 m3 := by + subst hcap + obtain ⟨Pt, Ub, Pp7, Tb7, Hr, Te, Kt, Lv7, Re, bits, KLv, W⟩ := J7A.unp J.j7 + have I := J.toJStage3A.delIn hA hPl W + have hPI : PoolIdx st OS s := Fl4ProofsA64.P3.S1aPoolCaps.poolIdxA W.r2.poolCaps + have D := J.decPreA hsl hext + have P := J.toJStage3A.sysPlace hext hsl hcov + have B := J.sys.sysl + have Y := J.symL + have hrd : obj.Slot.cap st slot s = ok (capA st s slot) := capA_eq s slot + have mcp := J.toJStage3A.mcpBound + have fh : ∀ t, capA st s slot = .Thread t → NullOrEp H (capA st s (slotIdx t layout.tcb.SLOT_FAULT_HANDLER)) := + fun t e => J.fhOK t (J.tcbs ▸ (J.toJStage3A.slotCapTy hsl).1.2.2.2.2.2 t e) + have e1 := post_of_okA (syscall.decode_invocation.s1aStepEntAll hA hM hPl W.ktK I hPI D P B Y hrd J.ko hfm W.pn + W.col W.cc W.al a hc hIq hSC mcp J.cur.2 fh hScC W.r2 W.sep W.df W.hiK len blk call don fp buf) hd + have e2 := post_of_okA (syscall.decode_invocation.s1aStepFix hA hM hPl I hPI D P B Y hrd J.ko W.pn W.col a hc hIq + hSC mcp J.cur.2 fh hScC W.r2 W.sep W.df W.hiK len blk call don fp buf) hd + have e3 := post_of_okA (syscall.decode_invocation.fixHeap hA hM hPl I hPI D P B Y hrd J.ko W.col a hc hIq hSC mcp + J.cur.2 fh hScC W.r2 W.df W.hiK len blk call don fp buf) hd + have e4 := post_of_okA (syscall.decode_invocation.poolNamedFix hA hM hPl I hPI D P B Y hrd J.ko W.col a hc hIq hSC + mcp J.cur.2 fh hScC W.r2 W.df W.hiK W.pn len blk call don fp buf) hd + have hk : L.ksb s3 = L.ksb s := e3.2.2 + obtain ⟨-, S1, -, ⟨Ac', bx⟩, -, us, -, -⟩ := e1 + have Fx : S1aStepFix st H Z.Cs OS Z.V Ub Pt Kt KLv ia ie hPl.ipa s3 := e2 + have hsp : ∀ p, SchedPageA st Z OS s3 p → p ∈ Pt := by + obtain ⟨_, -, -, -, ⟨_, _, _, _, _, _, _, R'⟩, -⟩ := Fx + exact schedPage_ptA R' W.sp + have e4' : PoolNamed st OS s3 := e4 + have sym : SymL st H Z.Cs s3 := by + obtain ⟨_, i, _⟩ := Fx + exact i.2.1 + exact ⟨hk, Ac', S1, sym, bx, us, + J7A.of_stepFixedA Fx W.vr hk W.al W.vu e4' hsp + (fun b hb o ho => by rw [hk]; exact W.ks b hb o ho) (J7R2K.of_stepFixedA (σ' := σ3) (m := m3) Fx W.ktK)⟩ + +end + +end + +end Fl4ProofsA64.P3 + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +/-- **No switch is pending after the kernel exit** (`schedule`, then `activate_thread`; module docstring). -/ +theorem exit_noSwitch (limit : Nat) (timer : State Unit → Fl4TimerView) {σ0 τ1 τ2 : State Unit} (hσ : FIA σ0) + (hAwk : ∀ σ1, fl4TimerFires σ0 σ1 → FIA σ1) + (hSw : ∀ σ1 σ2 σ3, fl4TimerFires σ0 σ1 → ((), σ2) ∈ (awaken (Z := Unit) σ1).1 → + ((), σ3) ∈ (checkDomainTime (Z := Unit) σ2).1 → SwA σ3) + (hidle : ∀ x, τ1.kheap τ1.idleThread = some (.tcb x) → x.tcbState = .idleThreadState) + (h1 : ((), τ1) ∈ (fl4Schedule (Z := Unit) limit timer σ0).1) + (h2 : ((), τ2) ∈ (fl4ActivateThread (Z := Unit) τ1).1) : + ∀ u, τ2.schedulerAction ≠ .switchThread u := fun u e => by + rw [(exit_spec limit timer hσ hAwk hSw hidle h1 h2).2] at e + cases e + +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **`JSyscallA` at the state a kernel exit leaves**, `act` from the exit (module docstring). -/ +theorem JSyscallA.ofSchedExit {A : Type} {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} + {κ : U64 → U64} {n : Nat} {J5 J7 : JSlotA S} {s : S} {a : A} {m : McState} + (limit : Nat) (timer : State Unit → Fl4TimerView) {σ0 τ1 τ2 : State Unit} (hσ : FIA σ0) + (hAwk : ∀ σ1, fl4TimerFires σ0 σ1 → FIA σ1) + (hSw : ∀ σ1 σ2 σ3, fl4TimerFires σ0 σ1 → ((), σ2) ∈ (awaken (Z := Unit) σ1).1 → + ((), σ3) ∈ (checkDomainTime (Z := Unit) σ2).1 → SwA σ3) + (hidle : ∀ x, τ1.kheap τ1.idleThread = some (.tcb x) → x.tcbState = .idleThreadState) + (h1 : ((), τ1) ∈ (fl4Schedule (Z := Unit) limit timer σ0).1) + (h2 : ((), τ2) ∈ (fl4ActivateThread (Z := Unit) τ1).1) + (J : JStage3EntryA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a τ2 m) + (running : Runnable (L.mem s (stA (L.curW s)))) + (curSc : L.mem s (scA (L.curW s)) ≠ 0#u64 → L.cscW s = L.mem s (scA (L.curW s))) + (cur : (L.mem s (inqA (L.curW s)) &&& sched.Q_RELEASE) = 0#u64 ∧ L.mem s (scA (L.curW s)) ≠ 0#u64) : + JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a τ2 m := + JSyscallA.ofExit J (exit_spec limit timer hσ hAwk hSw hidle h1 h2).2 running curSc cur + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3FireRecordA.lean b/proofs/Fl4ProofsA64/P3/Stage3FireRecordA.lean new file mode 100644 index 000000000..eac13ab0d --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3FireRecordA.lean @@ -0,0 +1,144 @@ +/- +P3 (aarch64, A64-P3 Stage 3, kernel-entry composition, step 1): **the record across `timer::fire`**. + +`JStage3A.fire_recA`: from the record and `SwA`, `timer.fire` has an `fl4TimerFires` outcome `σ1` at which the record +(at some ready / release queues) and `SwA` hold; so `FireRecA σ1`. The loop invariant carries A64 P1's walk bundle +`SW` (`P1/S1aCSched`), which gives `SysL` / `SymL` after each step. `timer.remove` and `REPROGRAM` are the record +lemmas of `P3/Stage3FireTwinsA` / `P3/Stage3FireReprA`. + +Named premises: `hcore` ([S], places core 0 in the record) and `hSig` ([J], Lift 2a): the record-level signal arm, all +cases of `send_signal`, on the bound notification. `SwA` across the signal is `sendSignal_swa` (`P3/Stage3SigSwA`), +its `SigQ` from the record (`JStage3A.sigQA`). +-/ +import Fl4ProofsA64.P3.Stage3FiresA +import Fl4ProofsA64.P3.Stage3FireReprA +import Fl4ProofsA64.P3.Stage3DelInA +import Fl4ProofsA64.P3.Stage3SigSwA +import Fl4ProofsA64.P1.S1aCSched +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3 Classical + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {ar : machine.Arch A} {nA : U64} {s : S} {a : A} {σ : State Unit} {m : McState} + +/-- **The record gives `SigQ`**: a record notification's waiting threads are blocked on it. -/ +theorem JStage3A.sigQA {J5 J7 : JSlotA S} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) {p : U64} (hp : p ∈ N) : + SigQ (specPtr p) σ := by + intro x q t hk hw ht + obtain ⟨x', hk', R⟩ := J.ntfn.ntfn p hp + rw [hk] at hk' + have := KernelObject.notification.inj (Option.some.inj hk') + subst this + have O := R.obj + rw [hw] at O + obtain ⟨-, xs, -, eq, Q, hb⟩ := O + rw [eq] at ht + obtain ⟨y, hy, rfl⟩ := List.mem_map.1 ht + obtain ⟨z, hz, T⟩ := J.ntfn.tcb y (Q.mem y hy) + obtain ⟨h6, hbl⟩ := hb y hy + have code : Notif.tsCode z.tcbState = 6 := by rw [← T.st]; exact h6 + refine ⟨z, hz, ?_⟩ + cases e : z.tcbState with + | blockedOnNotification o => + have := T.blk o e + rw [hbl] at this + rw [this] + | ext v => cases v; rw [e] at code; simp [Notif.tsCode] at code + | _ => rw [e] at code; simp [Notif.tsCode] at code + +/-- **The record across `timer::fire`** (module docstring). -/ +theorem JStage3A.fire_recA (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) + s a σ m) + (hcore : m.curCore < fl4A64Platform.numNodes) (hSw : SwA σ) + (hSig : ∀ (Q : U64 → U64 → List U64) (Rl : U64 → List U64) (s : S) (a : A) (σ : State Unit) (t badge : U64), + JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s a σ m → + SwA σ → t ∈ Z.T → L.mem s (boundA t) ≠ 0#u64 → + notification.send_signal st ar s a (L.mem s (boundA t)) badge ⦃ r => + ∃ σ' b Q' Rl', sendSignal (Z := Unit) (specPtr (L.mem s (boundA t))) b σ = ({((), σ')}, False) ∧ + JStage3A st AR { Z with Q := Q', R := Rl' } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) r.1 r.2 σ' m ⦄) : + timer.fire st ar s a ⦃ r => ∃ σ1 Q Rl, fl4TimerFires σ σ1 ∧ + JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) r.1 r.2 σ1 m ∧ FireRecA σ1 ⦄ := by + obtain ⟨Pt, Ub, Pp', Tb', Hr, Te, Kt, Lv', Re, bits, KLv, W⟩ := J7A.unp J.j7 + have I := J.delIn hA hPl W + have hc0 : 0#u64 ∈ Z.Cs := by + obtain ⟨c, hc, e⟩ := J.coresAll 0 (Nat.lt_of_le_of_lt (Nat.zero_le _) hcore) + have : c = 0#u64 := UScalar.eq_of_val_eq (by rw [e]; rfl) + exact this ▸ hc + have hks := J.ko + have W0 : SW st H Z.Cs OS Z.V ia ie Ac s s := SW.start J.sys.sysl J.symL J.blkA + let Inv : S → A → State Unit → Prop := fun s1 a1 σ1 => + (∃ Q Rl, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s1 a1 σ1 m) ∧ SwA σ1 ∧ SW st H Z.Cs OS Z.V ia ie Ac s s1 + have h := timer.fire_recA (st := st) (ar := ar) (T := Z.T) (Inv := Inv) ?_ ?_ ?_ ?_ s a σ + ⟨⟨Z.Q, Z.R, J⟩, hSw, W0⟩ + · refine WP.spec_mono h fun r ⟨σ1, hf, ⟨Q, Rl, J1⟩, sw1, _⟩ => ?_ + exact ⟨σ1, Q, Rl, hf, J1, JStage3A.fireRecA J1 sw1⟩ + · rintro s1 a1 σ1 ⟨⟨Q, Rl, J1⟩, -, -⟩ h0 + exact JStage3A.timerMinA (Z := { Z with Q := Q, R := Rl }) J1 h0 + · rintro s1 a1 σ1 t ⟨⟨Q, Rl, J1⟩, sw1, W1⟩ ht + have b1 := W1.1.blk + have h0 := W1.1.sys.ly.h0 + have htH : t ∈ H.tcbs := by rw [J1.tcbs]; exact ht + have SW1 : timer.remove st s1 t ⦃ s2 => SW st H Z.Cs OS Z.V ia ie Ac s s2 ⦄ := + WP.spec_mono (timer.remove.dmd 0#u64 t b1 h0 W1.1.sym W1.1.sys.vr htH) fun s2 ⟨d, b2⟩ => + W1.dmd0 I hc0 hks d b2 + have R := J1.timerRm_recA ht (WP.spec_mono SW1 fun _ w => ⟨w.1.sys, w.1.sym⟩) + refine WP.spec_mono (WP.spec_and R SW1) fun s2 ⟨⟨J2, _⟩, W2⟩ => ?_ + exact ⟨⟨⟨Q, Rl, J2⟩, sw1, W2⟩, trivial⟩ + · rintro s1 a1 σ1 t badge ⟨⟨Q, Rl, J1⟩, sw1, W1⟩ ht hb + have b1 := W1.1.blk + have h0 := W1.1.sys.ly.h0 + have htH : t ∈ H.tcbs := by rw [J1.tcbs]; exact ht + have r1 : RunVcpuInv st H.tcbs s1 := J1.tcbs ▸ J1.runVcpu + have hn : L.mem s1 (boundA t) ∈ H.ntfns := b1.2.2.1.2.2.bnd t htH hb + have hH : H (L.mem s1 (boundA t)) = some .ntfn := hn + have bt : L.mem s1 (ntfnBtA (L.mem s1 (boundA t))) = t := b1.2.2.1.2.2.bs.tb t htH hb + refine WP.spec_mono (WP.spec_and (hSig Q Rl s1 a1 σ1 t badge J1 sw1 ht hb) + (WP.spec_and (notification.send_signal.dmd hA a1 _ badge b1 hn hH h0 W1.1.sym W1.1.sys.vr + (fun y hy hb => r1.blocked hy hb)) + (notification.send_signal.tinv0 hA s1 a1 _ badge b1 hn hH r1))) ?_ + rintro ⟨s3, a3⟩ ⟨⟨σ', bd, Q', Rl', hs, J3⟩, hd⟩ + simp only [WP.uncurry'_pair] at hd J3 ⊢ + obtain ⟨d3, b3⟩ := hd + have hQ : SigQ (specPtr (L.mem s1 (boundA t))) σ1 := JStage3A.sigQA J1 (J1.ipcHeapCov.ntfn _ hH) + have sw3 : SwA σ' := sendSignal_swa _ bd σ1 ⟨sw1, hQ⟩ ((), σ') (by rw [hs]; rfl) + refine ⟨σ', ⟨_, bd, hs⟩, ⟨Q', Rl', J3⟩, sw3, W1.dmd I hc0 hks (fun k hk hia => ?_) d3 b3⟩ + rw [hH] at hk; cases hk + exact W1.1.sys.ln _ .ntfn hH (LiveQ.ntfn hH (Or.inr (Or.inl (by rw [bt]; exact ne0_of_heap h0 htH)))) hia + · rintro s1 a1 σ1 c ⟨⟨Q, Rl, J1⟩, sw1, W1⟩ hc + have b1 := W1.1.blk + have h0 := W1.1.sys.ly.h0 + have ec : c = L.cpu s1 := UScalar.eq_of_val_eq hc + step with obj.node_set.spec as ⟨s4, es4⟩ + subst es4 ec + have rp : layout.node.REPROGRAM.val = 56 := by simp [layout.node.REPROGRAM] + obtain ⟨Q1, R1, q⟩ := b1.1 + obtain ⟨d4, b4⟩ := nodeW_step 0#u64 b1 h0 W1.1.sym W1.1.sys.vr (by rw [rp]; unfold LowFree; omega) + (by rw [rp]; omega) (by rw [rp]; omega) + (writesIn_wr (nodeAt_aligned (q.nodes _ q.cur) (by rw [rp]; omega) (by rw [rp]))) + have W4 := W1.dmd0 I hc0 hks d4 b4 + have rv : RvSt st Z.T s1 (L.wr s1 (L.nodeAt s1 (L.cpu s1) layout.node.REPROGRAM) 1#u64) := fun y hy => by + have hyH : y ∈ H.tcbs := by rw [J1.tcbs]; exact hy + constructor + · rw [stA_eq, field_wr_node q q.cur (by rw [rp]; omega) (by rw [rp]) hyH (by simp) (by simp)] + · rw [show runVcpuA y = core.num.U64.wrapping_add y 3672#u64 from rfl, + field_wr_node q q.cur (by rw [rp]; omega) (by rw [rp]) hyH (by simp) (by simp)] + exact ⟨⟨Q, Rl, J1.reprog_recA W4.1.blk W4.1.sys W4.1.sym rv⟩, sw1, W4⟩ + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3FireReprA.lean b/proofs/Fl4ProofsA64/P3/Stage3FireReprA.lean new file mode 100644 index 000000000..90a31592f --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3FireReprA.lean @@ -0,0 +1,193 @@ +/- +P3 (aarch64, A64-P3 Stage 3, kernel-entry composition, step 1): **the record across the `REPROGRAM` write** of a fired +timer (spec `reprogramTimer := true`). The body follows `Stage3FireTwinsA`'s `rec_tdwA` +(`handoff/tools/gen_kentry_repr.py`). No new premise. +-/ +import Fl4ProofsA64.P3.Stage3FireTwinsA +import Fl4ProofsA64.P3.ScPostA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 Fl4ProofsA64.P3.Endp +open Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Notif + +local notation "wa" => core.num.U64.wrapping_add + +noncomputable section + +/-- The current core's `REPROGRAM` word. -/ +def RPWA {S : Type} (st : machine.Store S) [L : LawfulStore st] (s : S) (b : U64) : Prop := + b = L.nodeAt s (L.cpu s) layout.node.REPROGRAM + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {ar : machine.Arch A} {nA : U64} {s s' : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +section +variable (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) +include J + +theorem JStage3A.rp_valA {b : U64} (hb : RPWA st s b) : + (L.ksb s).val + 223488 ≤ b.val ∧ b.val < (L.ksb s).val + 772352 := by + obtain ⟨nn, -, hn, hcs⟩ := J.sr.csN + have hcl : (L.cpu s).val < 128 := by have := (hcs _).1 J.sr.sch.cpuCs; omega + have hr : layout.node.REPROGRAM.val = 56 := by simp [layout.node.REPROGRAM] + have hv := nodeAt_valSc (s := s) J.sr.ksFit hcl (o := layout.node.REPROGRAM) (by omega) + rw [hb]; omega + +theorem JStage3A.rp_tcbA {b : U64} (hb : RPWA st s b) {u : U64} (hu : u ∈ Z.T) (h1 : u.val ≤ b.val) + (h2 : b.val < u.val + 4096) : False := by + have hks := J.sr.tcb_ks hu + have hksz : ksSizeA = 788736 := rfl + have := J.rp_valA hb + omega + +theorem JStage3A.rp_tcbKA {u : U64} (hu : u ∈ Z.T) {K : U64} (hK : K.val < 4096) (hb : RPWA st s (wa u K)) : + False := by + have hfit := (J.sr.core.tcbs u hu).fits + have hv : (wa u K).val = u.val + K.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact J.rp_tcbA hb hu (by omega) (by omega) + +theorem JStage3A.rp_ipcA {b : U64} (hb : RPWA st s b) (h : IpcObjWA N E Rp b) : False := by + have other : ∀ {e : U64} {len : Nat}, Z.th e = some (.other len) → e.val ≤ b.val → b.val < e.val + len → False := by + intro e len he h1 h2 + have P := J.sr.place + have := P.ks e (.other len) he (fun hn => by have := P.irqK he hn; cases this) + have hksz : ksSizeA = 788736 := rfl + have hsz : ObjKindA.size (.other len) = len := rfl + have := J.rp_valA hb + omega + rcases h with ⟨q, hq, h1, h2⟩ | ⟨q, hq, h1, h2⟩ | ⟨q, hq, h1, h2⟩ + · exact other (J.ipcPlace.notification q hq) h1 h2 + · exact other (J.ipcPlace.endpoint q hq) h1 h2 + · exact other (J.ipcPlace.reply q hq) h1 h2 + +theorem JStage3A.rp_bufA {b : U64} (hb : RPWA st s b) (h : BufW Bf b) : False := by + obtain ⟨e, len, he, e1, e2⟩ := J.ipcPlace.buffer b h + have P := J.sr.place + have := P.ks e (.other len) he (fun hn => by have := P.irqK he hn; cases this) + have hksz : ksSizeA = 788736 := rfl + have hsz : ObjKindA.size (.other len) = len := rfl + have := J.rp_valA hb + omega + +end + +set_option maxHeartbeats 1000000 in +/-- **The record across the `REPROGRAM` write.** -/ +theorem JStage3A.reprog_recA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (blk' : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac + (L.wr s (L.nodeAt s (L.cpu s) layout.node.REPROGRAM) 1#u64)) + (sysl : SysL st H Z.Cs OS ia ie (L.wr s (L.nodeAt s (L.cpu s) layout.node.REPROGRAM) 1#u64)) + (symL : SymL st H Z.Cs (L.wr s (L.nodeAt s (L.cpu s) layout.node.REPROGRAM) 1#u64)) + (rv : RvSt st Z.T s (L.wr s (L.nodeAt s (L.cpu s) layout.node.REPROGRAM) 1#u64)) : + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) + (L.wr s (L.nodeAt s (L.cpu s) layout.node.REPROGRAM) 1#u64) a { σ with reprogramTimer := true } m := by + have H2 : SchedSRA st Z m κ (L.wr s (L.nodeAt s (L.cpu s) layout.node.REPROGRAM) 1#u64) + { σ with reprogramTimer := true } := ⟨J.sr.toSchedSRN.set_repr, J.sr.rd⟩ + have hA := J.sr.toSchedSRN.reprA.1 + have w0 : WritesIn st s (L.wr s (L.nodeAt s (L.cpu s) layout.node.REPROGRAM) 1#u64) (RPWA st s) := + (writesIn_wr hA).mono fun _ h => h + generalize hs' : L.wr s (L.nodeAt s (L.cpu s) layout.node.REPROGRAM) 1#u64 = s' at blk' sysl symL rv H2 w0 ⊢ + have hP := J.iplaced + have kp : ∀ b, Aligned b → (RPWA st s b → False) → L.mem s' b = L.mem s b := + fun b hb hn => w0.mem_eq hb hn + have tK : ∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → (K.val < 3528 ∨ 3592 ≤ K.val) → + L.mem s' (wa u K) = L.mem s (wa u K) := fun u hu K h1 h8 h2 => + kp _ (field_aligned (J.sr.core.tcbs u hu) h1 h8) (J.rp_tcbKA hu h1) + have w : WritesIn st s s' (SchedStepNWA (st := st) N E Rp Z m σ s) := w0.mono fun b hb => by + rw [hb] + exact Or.inl (Or.inl (Or.inr (Or.inl (J.sr.node_sks J.sr.sch.cpuCs (by simp [layout.node.REPROGRAM]))))) + have kb : L.ksb s' = L.ksb s := w.1 + have HS : SpecSchedA σ { σ with reprogramTimer := true } := + ⟨fun _ => Or.inl rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ + have SSz : SSzA σ { σ with reprogramTimer := true } := SSzA.refl σ + have rv' : RunVcpuInv st Z.T s' := J.runVcpu.rvSt rv + have sq : SchedQueuesInv st H.tcbs Z.Cs H.omega H s' Z.Q Z.R := by + obtain ⟨Q0, R0, h⟩ := blk'.1 + exact SchedQueuesInv.reQA h (by rw [J.tcbs]; exact H2.core) + have poolOff : PoolOff st H Z.Cs OS s' := fun i => by + have h0 := J.poolOff i + rw [vspace.asid_table.eq] at h0 ⊢ + have v := AtOff.v i + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.sys.ent.ksw + have ev : (wa (L.ksb s) (AtOff i)).val = (L.ksb s).val + (AtOff i).val := by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have e : L.mem s' (wa (L.ksb s') (AtOff i)) = L.mem s (wa (L.ksb s) (AtOff i)) := by + rw [kb] + refine w.mem_eq ?_ (J.asid_offStepNA (by omega) (by omega)) + have h32 := J.j.1.ksbAl + show (wa (L.ksb s) (AtOff i)).val % 8 = 0 + omega + rw [e] + refine (WP.spec_ok _).2 fun hne asid => ?_ + have q := (WP.spec_ok _).1 h0 hne asid + exact ⟨q.al, q.om, q.sg, q.os, RegionOffNodes.ksb kb q.ks⟩ + have ep : EpStateRel Z.T E Rp Bf (L.mem s') { σ with reprogramTimer := true } := by + refine J.ep.frame hP.e (fun b hb hf => kp b hb fun htw => ?_) + ⟨fun p x hk => hk, fun p x hk => ⟨x, hk, rfl⟩, fun p x hk => ⟨x, hk, rfl, rfl, rfl, rfl, rfl⟩, rfl⟩ + rcases hf with ⟨e, he, h⟩ | ⟨r, hr, h⟩ | ⟨u, hu, ⟨h1, h2⟩ | h⟩ | h + · exact J.rp_ipcA htw (Or.inr (Or.inl ⟨e, he, by omega, by omega⟩)) + · exact J.rp_ipcA htw (Or.inr (Or.inr ⟨r, hr, by omega, by omega⟩)) + · unfold TOffN at h2 + exact J.rp_tcbA htw hu h1 (by omega) + · unfold TL at h + exact J.rp_tcbA htw hu (by omega) (by omega) + · exact J.rp_bufA htw h + have ntfn : NtfnStateRel N Z.T (L.mem s') { σ with reprogramTimer := true } := by + refine J.ntfn.frame hP.n (fun b hb hf => kp b hb fun htw => ?_) + ⟨fun p x hk => hk, fun p x hk => ⟨x, hk, rfl, rfl, rfl⟩⟩ + rcases hf with ⟨q, hq, h1, h2⟩ | ⟨u, hu, e⟩ + · exact J.rp_ipcA htw (Or.inl ⟨q, hq, h1, by omega⟩) + · rcases e with rfl | rfl | rfl | rfl | rfl | rfl + · rw [stA_eq] at htw; exact J.rp_tcbKA hu (by simp) htw + · rw [tBlockA_eq] at htw; exact J.rp_tcbKA hu (by simp) htw + · rw [badgeRegA_eq] at htw; exact J.rp_tcbKA hu (by simp) htw + · rw [prioA_eq'] at htw; exact J.rp_tcbKA hu (by simp) htw + · rw [nextEp_eq] at htw; exact J.rp_tcbKA hu (by simp) htw + · rw [prevEp_eq] at htw; exact J.rp_tcbKA hu (by simp) htw + have reply : ReplyStackRelA (L.mem s') Rp Z.C { σ with reprogramTimer := true } (scNameA m) stack := by + refine J.reply.frame (fun r hr => ?_) (fun c _ x n hk => ⟨x, n, hk, rfl⟩) (fun r _ x hk => ⟨x, hk, rfl⟩) + obtain ⟨r32, -, rfit⟩ := hP.e.rep r hr + have one : ∀ o : U64, o.val < 32 → o.val % 8 = 0 → L.mem s' (wa r o) = L.mem s (wa r o) := by + intro o o1 o2 + have v : (wa r o).val = r.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + refine kp _ ?_ fun htw => J.rp_ipcA htw (Or.inr (Or.inr ⟨r, hr, by omega, by omega⟩)) + show (wa r o).val % 8 = 0 + omega + have n16 : (layout.reply.NEXT).val = 16 := by unfold layout.reply.NEXT; rfl + have p8 : (layout.reply.PREV).val = 8 := by unfold layout.reply.PREV; rfl + exact ⟨one _ (by omega) (by omega), one _ (by omega) (by omega)⟩ + have buf : BufCover st Z.T Bf s' { σ with reprogramTimer := true } := + J.bufCover_wA w0 (fun u hu K hK htw => J.rp_tcbKA hu (by omega) htw) + (fun q z h => ⟨z, h, rfl, rfl⟩) + have mcp : McpRelA Z.T m (L.mem s') { σ with reprogramTimer := true } := + J.mcp.congrA (fun u hu => ⟨tK u hu 3440#u64 (by simp) (by simp) (by simp), + tK u hu 3456#u64 (by simp) (by simp) (by simp)⟩) (fun u hu x hx => ⟨x, hx, rfl, rfl⟩) + have yr : YieldRelA Z.T Z.C m (L.mem s') { σ with reprogramTimer := true } := by + have k : ∀ u ∈ Z.T, L.mem s' (yieldA u) = L.mem s (yieldA u) := + fun u hu => tK u hu 3472#u64 (by simp) (by simp) (by simp) + refine ⟨fun u hu => ?_, fun u hu hnz => ?_, fun c hc y n hy p hp => ?_⟩ + · obtain ⟨x, hx, he⟩ := J.yieldRel.tcb u hu + exact ⟨x, hx, by rw [k u hu]; exact he⟩ + · rw [k u hu] at hnz ⊢ + exact J.yieldRel.toSc u hu hnz + · obtain ⟨u, hu, e1, e2⟩ := J.yieldRel.frm c hc y n hy p hp + exact ⟨u, hu, e1, by rw [k u hu]; exact e2⟩ + have bq : BlkQueuedA Z.T E N (L.mem s') { σ with reprogramTimer := true } := + J.blkQ.keepA (fun u hu => by rw [Notif.tBlockA_eq]; exact tK u hu 3336#u64 (by simp) (by simp) (by simp)) + (fun u hu x' hx' => Or.inr ⟨x', hx', rfl⟩) (fun _ _ h => h) (fun _ _ h => h) + have j := J.j57_stepNWA w { σ with reprogramTimer := true } + exact JStage3A.of_schedWrN (blkQ := bq) (symL := symL) (schedValid := { J.schedValid with }) J (Q := Z.Q) (Rl := Z.R) (curOf := Z.curOf) (curScOf := Z.curScOf) + (s' := s') (a' := a) (σ' := { σ with reprogramTimer := true }) w HS SSz (QKeepA.of_kheap rfl ⟨rfl, rfl, rfl, rfl⟩) rfl J.j.1.ar (fun _ _ => rfl) J.bufAl + ⟨J.ent_stepNWA w blk', sysl⟩ sq poolOff H2 J.scCov ep ntfn reply buf J.idleNoFault mcp yr { J.sinv with irqN := by cases J.sinv.irqN; constructor <;> assumption } { J.sidle with } rv' + j.1 j.2 + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3FireTwinsA.lean b/proofs/Fl4ProofsA64/P3/Stage3FireTwinsA.lean new file mode 100644 index 000000000..53563ae33 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3FireTwinsA.lean @@ -0,0 +1,506 @@ +/- +P3 (aarch64, A64-P3 Stage 3, kernel-entry composition, step 1): **`JStage3A` twins for the `timer.remove` step**. + +Generated by `handoff/tools/gen_kentry_twins.py` from the `JStage3TA` lemmas in the closure of `timerRm_recA` +(which needs `noSwitch`, false after a waking timer signal). The sources are unchanged. No new premise. +-/ +import Fl4ProofsA64.P3.DelNtfnEntA +import Fl4ProofsA64.P3.DelNtfnJ5A +import Fl4ProofsA64.P3.DelNtfnLoopFrA +import Fl4ProofsA64.P3.DelNtfnRecA +import Fl4ProofsA64.P3.DelThrTimerA +import Fl4ProofsA64.P3.Stage3SchedWrNA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 Fl4ProofsA64.P3.Endp +open Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Notif + +local notation "wa" => core.num.U64.wrapping_add + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {ar : machine.Arch A} {nA : U64} {s s' : S} {a : A} {σ σ' : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +-- from `P3/DelThrTimerA.lean` +/-- A timer word's value: a TCB's word 3528–3575, or a core's node word 104 / 112. -/ +theorem JStage3A.tw_valA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) {b : U64} + (hb : TDWA st Z s b) : + (∃ y ∈ Z.T, y.val + 3528 ≤ b.val ∧ b.val < y.val + 3592) ∨ + ((L.ksb s).val + 223488 ≤ b.val ∧ b.val < (L.ksb s).val + 772352 ∧ + ∃ c ∈ Z.Cs, ∃ o : U64, (o.val = 88 ∨ o.val = 104 ∨ o.val = 112) ∧ c.val < 128 ∧ + b.val = (L.ksb s).val + 223488 + c.val * 4288 + o.val) := by + rcases hb with ⟨y, hy, K, k1, k2, rfl⟩ | ⟨c, hc, o, ho, rfl⟩ + · have hfit := (J.sr.core.tcbs y hy).fits + have hv : (wa y K).val = y.val + K.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact Or.inl ⟨y, hy, by omega, by omega⟩ + · obtain ⟨nn, -, hn, hcs⟩ := J.sr.csN + have hcl : c.val < 128 := by have := (hcs c).1 hc; omega + have hv := nodeAt_valSc (s := s) J.sr.ksFit hcl (o := o) (by omega) + exact Or.inr ⟨by omega, by omega, c, hc, o, ho, hcl, hv⟩ + +-- from `P3/DelThrTimerA.lean` +/-- A timer word is no word of a TCB outside the timer fields. -/ +theorem JStage3A.tw_tcbA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) {b : U64} + (hb : TDWA st Z s b) {u : U64} (hu : u ∈ Z.T) (h1 : u.val ≤ b.val) + (h2 : b.val < u.val + 3528 ∨ (u.val + 3592 ≤ b.val ∧ b.val < u.val + 4096)) : False := by + have hks := J.sr.tcb_ks hu + have hksz : ksSizeA = 788736 := rfl + rcases J.tw_valA hb with ⟨y, hy, y1, y2⟩ | ⟨k1, k2, -⟩ + · by_cases e : u = y + · subst e; omega + · have := J.sr.place.apart u y .tcb .tcb ((J.sr.thT u).2 hu) ((J.sr.thT y).2 hy) e + have hsz : ObjKindA.size .tcb = 4096 := rfl + omega + · omega + +-- from `P3/DelThrTimerA.lean` +/-- A timer word of the TCB form, by offset. -/ +theorem JStage3A.tw_tcbKA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) {u : U64} (hu : u ∈ Z.T) + {K : U64} (hK : K.val < 4096) (hK' : K.val < 3528 ∨ 3592 ≤ K.val) + (hb : TDWA st Z s (wa u K)) : False := by + have hfit := (J.sr.core.tcbs u hu).fits + have hv : (wa u K).val = u.val + K.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact J.tw_tcbA hb hu (by omega) (by omega) + +-- from `P3/DelThrTimerA.lean` +/-- A timer word is no other node word. -/ +theorem JStage3A.tw_nodeA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) {b : U64} + (hb : TDWA st Z s b) {c o : U64} (hc : c ∈ Z.Cs) (ho : o.val < 4288) (h0 : o.val ≠ 88) + (h1 : o.val ≠ 104) (h2 : o.val ≠ 112) (e : b = L.nodeAt s c o) : False := by + obtain ⟨nn, -, hn, hcs⟩ := J.sr.csN + have hcl : c.val < 128 := by have := (hcs c).1 hc; omega + have hv := nodeAt_valSc (s := s) J.sr.ksFit hcl ho + rw [← e] at hv + have hksz : ksSizeA = 788736 := rfl + rcases J.tw_valA hb with ⟨y, hy, y1, y2⟩ | ⟨k1, k2, c', hc', o', ho', hcl', hv'⟩ + · have := J.sr.tcb_ks hy + omega + · omega + +-- from `P3/DelThrTimerA.lean` +/-- A timer word is not in the global words below the nodes. -/ +theorem JStage3A.tw_ksLowA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) {b : U64} + (hb : TDWA st Z s b) (h1 : (L.ksb s).val ≤ b.val) + (h2 : b.val < (L.ksb s).val + 223488) : False := by + have hksz : ksSizeA = 788736 := rfl + rcases J.tw_valA hb with ⟨y, hy, y1, y2⟩ | ⟨k1, k2, -⟩ + · have := J.sr.tcb_ks hy + omega + · omega + +-- from `P3/DelThrTimerA.lean` +theorem JStage3A.tw_scA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) {b : U64} + (hb : TDWA st Z s b) (h : ScFootA Z.C m σ b) : False := by + have hksz : ksSizeA = 788736 := rfl + rcases J.tw_valA hb with ⟨y, hy, y1, y2⟩ | ⟨k1, k2, -⟩ + · have := J.sr.scp.th_out h ((J.sr.thT y).2 hy) + have hsz : ObjKindA.size .tcb = 4096 := rfl + omega + · have := J.sr.scp.ks_out h + omega + +-- from `P3/DelThrTimerA.lean` +/-- **`SchedSRA` across timer writes** (module docstring). -/ +theorem JStage3A.sr_twA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (w : WritesIn st s s' (TDWA st Z s)) : SchedSRA st Z m κ s' σ := by + have H := J.sr + have kp : ∀ b, Aligned b → (TDWA st Z s b → False) → L.mem s' b = L.mem s b := + fun b hb hn => w.mem_eq hb hn + have tK : ∀ t ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → (K.val < 3528 ∨ 3592 ≤ K.val) → + L.mem s' (wa t K) = L.mem s (wa t K) := fun t ht K h1 h8 h2 => + kp _ (field_aligned (H.core.tcbs t ht) h1 h8) (J.tw_tcbKA ht h1 h2) + have nK : ∀ c ∈ Z.Cs, ∀ o : U64, o.val < 4288 → o.val % 8 = 0 → o.val ≠ 88 → o.val ≠ 104 → o.val ≠ 112 → + L.mem s' (L.nodeAt s c o) = L.mem s (L.nodeAt s c o) := + fun c hc o ho h8 h0 h1 h2 => kp _ (nodeAt_aligned (H.core.nodes c hc) ho h8) fun hb => + J.tw_nodeA hb hc ho h0 h1 h2 rfl + have hkb := H.ksb_al + have hf := H.ksFit + have gK : ∀ K : U64, (K = 8#u64 ∨ K = 16#u64) → L.mem s' (wa (L.ksb s) K) = L.mem s (wa (L.ksb s) K) := by + intro K hK + have hK' : K.val = 8 ∨ K.val = 16 := by rcases hK with rfl | rfl <;> simp + have hv : (wa (L.ksb s) K).val = (L.ksb s).val + K.val := by rw [wa_valm]; unfold ksSizeA at hf; omega + exact kp _ (by unfold Aligned; rw [hv]; omega) fun hb => J.tw_ksLowA hb (by omega) (by omega) + have scK : ∀ b, Aligned b → ScFootA Z.C m σ b → L.mem s' b = L.mem s b := + fun b hb hs => kp b hb fun h => J.tw_scA h hs + have hcore : SchedCoreA st Z.T Z.Cs s' Z.Q Z.R := + H.core.agree w.1 (fun y hy => ⟨tK y hy 3480#u64 (by simp) (by simp) (by simp), + tK y hy 3488#u64 (by simp) (by simp) (by simp)⟩) + (fun c hc o h1 h2 h3 => nK c hc o h1 h2 (by omega) (by omega) (by omega)) + (fun y hy => tK y hy 3592#u64 (by simp) (by simp) (by simp)) + (fun y hy => tK y hy 3520#u64 (by simp) (by simp) (by simp)) + (fun y hy => tK y hy 3448#u64 (by simp) (by simp) (by simp)) + have e3464 : ∀ t ∈ Z.T, L.mem s' (scA t) = L.mem s (scA t) := + fun t ht => tK t ht 3464#u64 (by simp) (by simp) (by simp) + refine ⟨H.toSchedSRN.rebuildG (Q' := Z.Q) (R' := Z.R) w.1 w.2.1 + (fun c hc o h1 h2 _ _ => nK c hc o (by omega) h2 (by omega) (by omega) (by omega)) gK + (fun t ht K hK => by + rcases hK with rfl | rfl | rfl + · exact tK t ht _ (by simp) (by simp) (by simp) + · exact tK t ht _ (by simp) (by simp) (by simp) + · exact tK t ht _ (by simp) (by simp) (by simp)) + (H.scr.frame scK (fun _ _ => rfl)) H.scp (fun t ht x hx => ⟨x, hx, rfl, rfl⟩) + (fun t ht => by + obtain ⟨x, h1, h3⟩ := H.sch.tcb t ht + exact ⟨x, h1, by rw [e3464 t ht]; exact h3⟩) + (fun t ht h => by + rw [e3464 t ht] at h ⊢ + exact H.rest.scMem t ht h) + (SFixA.refl σ) ?_ ?_ H.actOn H.inv hcore H.rest.rq H.rest.rqDom H.rest.rel H.run + (fun c hc => scK _ (H.scCoreFoot hc).1 (H.scCoreFoot hc).2), H.rd⟩ + · rw [nK _ H.sch.cpuCs layout.node.REPROGRAM (by simp [layout.node.REPROGRAM]) + (by simp [layout.node.REPROGRAM]) (by simp [layout.node.REPROGRAM]) (by simp [layout.node.REPROGRAM]) (by simp [layout.node.REPROGRAM])] + exact H.rest.repr + · rw [nK _ H.sch.cpuCs layout.node.SCHED_ACTION (by simp [layout.node.SCHED_ACTION]) + (by simp [layout.node.SCHED_ACTION]) (by simp [layout.node.SCHED_ACTION]) (by simp [layout.node.SCHED_ACTION]) (by simp [layout.node.SCHED_ACTION])] + exact H.sch.act + +-- from `P3/DelThrTimerA.lean` +/-- A timer word is in no typed `other` object (IPC objects, IPC buffers). -/ +theorem JStage3A.tw_otherA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) {b : U64} + (hb : TDWA st Z s b) {e : U64} {len : Nat} (he : Z.th e = some (.other len)) + (h1 : e.val ≤ b.val) (h2 : b.val < e.val + len) : False := by + have P := J.sr.place + have hsz : ObjKindA.size (.other len) = len := rfl + rcases J.tw_valA hb with ⟨y, hy, y1, y2⟩ | ⟨k1, k2, -⟩ + · have hth : Z.th y = some .tcb := (J.sr.thT y).2 hy + have hne : y ≠ e := fun h => by subst h; rw [hth] at he; cases he + have := P.apart y e .tcb (.other len) hth he hne + have hsz' : ObjKindA.size .tcb = 4096 := rfl + omega + · have := P.ks e (.other len) he (fun hn => by have := P.irqK he hn; cases this) + have hksz : ksSizeA = 788736 := rfl + omega + +-- from `P3/DelThrTimerA.lean` +theorem JStage3A.tw_ipcObjA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) {b : U64} + (hb : TDWA st Z s b) (h : IpcObjWA N E Rp b) : False := by + rcases h with ⟨q, hq, h1, h2⟩ | ⟨q, hq, h1, h2⟩ | ⟨q, hq, h1, h2⟩ + · exact J.tw_otherA hb (J.ipcPlace.notification q hq) h1 h2 + · exact J.tw_otherA hb (J.ipcPlace.endpoint q hq) h1 h2 + · exact J.tw_otherA hb (J.ipcPlace.reply q hq) h1 h2 + +-- from `P3/DelThrTimerA.lean` +theorem JStage3A.tw_bufA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) {b : U64} + (hb : TDWA st Z s b) (h : BufW Bf b) : False := by + obtain ⟨e, len, he, e1, e2⟩ := J.ipcPlace.buffer b h + exact J.tw_otherA hb he e1 e2 + +-- from `P3/DelNtfnRecA.lean` +/-- **`BufCover` across a step keeping the TCBs' buffer word and buffer slot** (words below offset 224 and the +word at 3512) and the spec's buffer fields. -/ +theorem JStage3A.bufCover_wA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {W : U64 → Prop} (w : WritesIn st s s' W) + (hW : ∀ t ∈ Z.T, ∀ K : U64, (K.val < 224 ∨ K.val = 3512) → ¬ W (wa t K)) + (K : ∀ p x, σ.kheap p = some (.tcb x) → ∃ y, σ'.kheap p = some (.tcb y) ∧ + y.tcbIpcframe = x.tcbIpcframe ∧ y.tcbIpcBuffer = x.tcbIpcBuffer) : + BufCover st Z.T Bf s' σ' := by + have kept : ∀ t ∈ Z.T, ∀ K : U64, (K.val < 224 ∨ K.val = 3512) → K.val % 8 = 0 → + L.mem s' (wa t K) = L.mem s (wa t K) := by + intro t ht K hK h8 + have htH : H t = some .tcb := by + have : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + exact this + have hal := J.sys.ent.heap.align t _ htH + have hfit := (J.sr.core.tcbs t ht).fits + have hv : (wa t K).val = t.val + K.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + simp only [HKind.bits] at hal + refine w.mem_eq ?_ (hW t ht K hK) + show (wa t K).val % 8 = 0 + omega + have sb : ∀ t : U64, sbWordA t = wa t 64#u64 := by + intro t + have h64 : core.num.U64.wrapping_mul layout.tcb.SLOT_BUFFER layout.cte.SIZE = 64#u64 := by + first + | rfl + | decide + | (unfold layout.tcb.SLOT_BUFFER layout.cte.SIZE; rfl) + unfold sbWordA + rw [h64] + have osk : OSKeep st {y | ∃ t ∈ Z.T, y = sbWordA t} s s' := by + rintro y ⟨t, ht, rfl⟩ + have hfit := (J.sr.core.tcbs t ht).fits + have e64 : (wa t 64#u64).val = t.val + 64 := by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by simp; omega) + have step : ∀ o : U64, o.val < 32 → o.val % 8 = 0 → + L.mem s' (wa (sbWordA t) o) = L.mem s (wa (sbWordA t) o) := by + intro o o1 o2 + have hK : (wa 64#u64 o).val = 64 + o.val := by + rw [wa_valm, show (64#u64 : U64).val = 64 by simp]; exact Nat.mod_eq_of_lt (by omega) + have e : wa (sbWordA t) o = wa t (wa 64#u64 o) := by + apply UScalar.eq_of_val_eq + have l : (wa (wa t 64#u64) o).val = t.val + 64 + o.val := by + rw [wa_valm, e64]; exact Nat.mod_eq_of_lt (by omega) + have r : (wa t (wa 64#u64 o)).val = t.val + (64 + o.val) := by + rw [wa_valm, hK]; exact Nat.mod_eq_of_lt (by omega) + rw [sb t, l, r]; omega + rw [e] + exact kept t ht _ (Or.inl (by omega)) (by omega) + have e0 : wa (sbWordA t) 0#u64 = sbWordA t := by apply UScalar.eq_of_val_eq; rw [wa_valm]; simp + exact ⟨by have := step 0#u64 (by simp) (by simp); rwa [e0] at this, step 8#u64 (by simp) (by simp), + step 16#u64 (by simp) (by simp), step 24#u64 (by simp) (by simp)⟩ + refine BufCover.frameK J.bufCover (fun t ht => ?_) (fun t ht => osk.capA ⟨t, ht, rfl⟩) (fun t ht => ?_) K + · rw [ibWordA_eq] + exact kept t ht _ (Or.inr (by simp)) (by simp) + · obtain ⟨x, hx, -⟩ := J.ntfn.tcb t ht + exact ⟨x, hx⟩ + +-- from `P3/DelNtfnLoopFrA.lean` +/-- The loop keeps the `OS` slot words. -/ +theorem JStage3A.sigW_osKeepA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (w : WritesIn st s s' (SchedStepNWA (st := st) N E Rp Z m σ s)) : OSKeep st OS s s' := + OSKeep.of_off J.sys.ent.os w fun y hy o o1 o2 h => by + have hv : (wa y o).val = y.val + o.val := by + have := J.sys.ent.os.fit y hy + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact J.os_offStepNA (J.sys.ent.os.al hy o1 o2) ⟨y, hy, by omega, by omega⟩ h + +-- from `P3/DelNtfnJ5A.lean` +/-- **J5 at the new state** from the kept `OS` slots and IRQ state words, at any ghost queues and spec state with the +same MDB ghost and typed heap. -/ +theorem JStage3A.j5_keepA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (osk : OSKeep st OS s s') (hk : L.ksb s' = L.ksb s) + (irqW : ∀ i : U64, i.val < 5376 → L.mem s' (irqStAddrA (L.ksb s) i) = L.mem s (irqStAddrA (L.ksb s) i)) + (σ' : Fl4SpecA64.State Unit) : J5A st Z H OS s' σ' m := by + obtain ⟨⟨mdb, irq, act⟩, sub, nul⟩ := J.j5 + have tr := J.j.1.cdt.tree + have osk' : OSKeep st {a | IsSlotA Z.th a} s s' := fun y hy => osk y (sub y hy) + exact ⟨⟨MdbInvA.keepO tr.lk tr.tree mdb osk', irq.keepO tr.lk osk', act.keepO tr.lk osk' hk irqW⟩, sub, + fun y hy hn => (osk.capA hy).trans (nul y hy hn)⟩ + +-- from `P3/DelNtfnLoopFrA.lean` +/-- **J5 and J7 across a step writing only `SchedStepNWA` words** (the loop, by `sigW_stepNWA`). -/ +theorem JStage3A.j57_stepNWA (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (w : WritesIn st s s' (SchedStepNWA (st := st) N E Rp Z m σ s)) (σ' : Fl4SpecA64.State Unit) : + J5A st Z H OS s' σ' m ∧ J7A st (J7R2A st ar nA) Z H OS s' σ' m := by + have osk := J.sigW_osKeepA w + have kb : L.ksb s' = L.ksb s := w.1 + have irqW : ∀ i : U64, i.val < 5376 → + L.mem s' (irqStAddrA (L.ksb s) i) = L.mem s (irqStAddrA (L.ksb s) i) := by + intro i hi + have hv := irqWordA_val J.sr.ksFit hi + have hkb := J.sr.toSchedSRN.ksb_al + refine w.mem_eq ?_ (J.irq_offStepNA hi) + show (irqWordA (L.ksb s) i).val % 8 = 0 + unfold Aligned at hkb + omega + refine ⟨J.j5_keepA osk kb irqW σ', J7PpKA.keep (st := st) _ _ _ _ _ σ σ' _ J.j7 kb osk ?_⟩ + intro Pt Ub vr b hb + refine w.mem_eq (vr.al _ hb) ?_ + by_cases h : AsW (L.ksb s) b + · obtain ⟨o, o1, o2, -, e⟩ := h + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.sys.ent.ksw + have ev : b.val = (L.ksb s).val + o.val := by + rw [e, wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact J.asid_offStepNA (by omega) (by omega) + · exact J.vsr_offStepNA vr hb h + +-- from `P3/DelNtfnEntA.lean` +/-- **`EntryInvA3` across a scheduler / IPC-object write step** that re-establishes `BlkOK`. -/ +theorem JStage3A.ent_stepNWA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (w : WritesIn st s s' (SchedStepNWA (st := st) N E Rp Z m σ s)) + (blk : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s') : + EntryInvA3 st H Z.Cs OS U Ac Pp Tb Lv s' := by + have kb : L.ksb s' = L.ksb s := w.1 + refine ppEntA3 J.sys.ent blk kb (utVsPt_keepA J.sys.ent.vs kb fun b hb => ?_).1 + have vr := J.vsOk + refine w.mem_eq (vr.al _ hb) ?_ + by_cases h : AsW (L.ksb s) b + · obtain ⟨o, o1, o2, -, e⟩ := h + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.sys.ent.ksw + have ev : b.val = (L.ksb s).val + o.val := by + rw [e, wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact J.asid_offStepNA (by omega) (by omega) + · exact J.vsr_offStepNA vr hb h + +-- from `P3/DelThrTimerA.lean` +/-- **The record across a step writing only `TDWA` words** (`timer.remove`, `debug_remove`), given P1's `BlkOK` and +the run-VCPU words at the end. The spec state is unchanged. -/ +theorem JStage3A.rec_tdwA (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) {s' : S} + (blk' : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s') + (rv : RvSt st Z.T s s') (w0 : WritesIn st s s' (TDWA st Z s)) + (sysl : SysL st H Z.Cs OS ia ie s') (symL : SymL st H Z.Cs s') : + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s' a σ m ∧ + WritesIn st s s' (SchedStepNWA (st := st) N E Rp Z m σ s) ∧ OSKeep st OS s s' ∧ + (∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → (K.val < 3528 ∨ 3592 ≤ K.val) → + L.mem s' (wa u K) = L.mem s (wa u K)) := by + have hP := J.iplaced + have kp : ∀ b, Aligned b → (TDWA st Z s b → False) → L.mem s' b = L.mem s b := + fun b hb hn => w0.mem_eq hb hn + have tK : ∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → (K.val < 3528 ∨ 3592 ≤ K.val) → + L.mem s' (wa u K) = L.mem s (wa u K) := fun u hu K h1 h8 h2 => + kp _ (field_aligned (J.sr.core.tcbs u hu) h1 h8) (J.tw_tcbKA hu h1 h2) + have w : WritesIn st s s' (SchedStepNWA (st := st) N E Rp Z m σ s) := w0.mono fun b hb => by + rcases hb with ⟨y, hy, K, k1, k2, rfl⟩ | ⟨c, hc, o, ho, rfl⟩ + · have hfit := (J.sr.core.tcbs y hy).fits + have hv : ∀ K' : U64, K'.val < 4096 → (wa y K').val = y.val + K'.val := fun K' h => by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have v1 := hv K (by omega) + have v2 := hv 3664#u64 (by simp) + have v3 := hv 3672#u64 (by simp) + have c1 : (3664#u64 : U64).val = 3664 := by simp + have c2 : (3672#u64 : U64).val = 3672 := by simp + refine Or.inl (Or.inr ⟨y, hy, by omega, by omega, fun e => ?_, fun e => ?_⟩) + · have := congrArg UScalar.val e + omega + · unfold runVcpuA at e + have := congrArg UScalar.val e + omega + · exact Or.inl (Or.inl (Or.inr (Or.inl (J.sr.node_sks hc (by omega))))) + have kb : L.ksb s' = L.ksb s := w.1 + have H2 : SchedSRA st Z m κ s' σ := J.sr_twA w0 + have rv' : RunVcpuInv st Z.T s' := J.runVcpu.rvSt rv + have sq : SchedQueuesInv st H.tcbs Z.Cs H.omega H s' Z.Q Z.R := by + obtain ⟨Q0, R0, h⟩ := blk'.1 + exact SchedQueuesInv.reQA h (by rw [J.tcbs]; exact H2.core) + have poolOff : PoolOff st H Z.Cs OS s' := fun i => by + have h0 := J.poolOff i + rw [vspace.asid_table.eq] at h0 ⊢ + have v := AtOff.v i + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.sys.ent.ksw + have ev : (wa (L.ksb s) (AtOff i)).val = (L.ksb s).val + (AtOff i).val := by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have e : L.mem s' (wa (L.ksb s') (AtOff i)) = L.mem s (wa (L.ksb s) (AtOff i)) := by + rw [kb] + refine w.mem_eq ?_ (J.asid_offStepNA (by omega) (by omega)) + have h32 := J.j.1.ksbAl + show (wa (L.ksb s) (AtOff i)).val % 8 = 0 + omega + rw [e] + refine (WP.spec_ok _).2 fun hne asid => ?_ + have q := (WP.spec_ok _).1 h0 hne asid + exact ⟨q.al, q.om, q.sg, q.os, RegionOffNodes.ksb kb q.ks⟩ + have ep : EpStateRel Z.T E Rp Bf (L.mem s') σ := by + refine J.ep.frame hP.e (fun b hb hf => kp b hb fun htw => ?_) + ⟨fun p x hk => hk, fun p x hk => ⟨x, hk, rfl⟩, fun p x hk => ⟨x, hk, rfl, rfl, rfl, rfl, rfl⟩, rfl⟩ + rcases hf with ⟨e, he, h⟩ | ⟨r, hr, h⟩ | ⟨u, hu, ⟨h1, h2⟩ | h⟩ | h + · exact J.tw_ipcObjA htw (Or.inr (Or.inl ⟨e, he, by omega, by omega⟩)) + · exact J.tw_ipcObjA htw (Or.inr (Or.inr ⟨r, hr, by omega, by omega⟩)) + · unfold TOffN at h2 + exact J.tw_tcbA htw hu h1 (Or.inl (by omega)) + · unfold TL at h + exact J.tw_tcbA htw hu (by omega) (Or.inl (by omega)) + · exact J.tw_bufA htw h + have ntfn : NtfnStateRel N Z.T (L.mem s') σ := by + refine J.ntfn.frame hP.n (fun b hb hf => kp b hb fun htw => ?_) + ⟨fun p x hk => hk, fun p x hk => ⟨x, hk, rfl, rfl, rfl⟩⟩ + rcases hf with ⟨q, hq, h1, h2⟩ | ⟨u, hu, e⟩ + · exact J.tw_ipcObjA htw (Or.inl ⟨q, hq, h1, by omega⟩) + · rcases e with rfl | rfl | rfl | rfl | rfl | rfl + · rw [stA_eq] at htw; exact J.tw_tcbKA hu (by simp) (Or.inl (by simp)) htw + · rw [tBlockA_eq] at htw; exact J.tw_tcbKA hu (by simp) (Or.inl (by simp)) htw + · rw [badgeRegA_eq] at htw; exact J.tw_tcbKA hu (by simp) (Or.inl (by simp)) htw + · rw [prioA_eq'] at htw; exact J.tw_tcbKA hu (by simp) (Or.inl (by simp)) htw + · rw [nextEp_eq] at htw; exact J.tw_tcbKA hu (by simp) (Or.inl (by simp)) htw + · rw [prevEp_eq] at htw; exact J.tw_tcbKA hu (by simp) (Or.inl (by simp)) htw + have reply : ReplyStackRelA (L.mem s') Rp Z.C σ (scNameA m) stack := by + refine J.reply.frame (fun r hr => ?_) (fun c _ x n hk => ⟨x, n, hk, rfl⟩) (fun r _ x hk => ⟨x, hk, rfl⟩) + obtain ⟨r32, -, rfit⟩ := hP.e.rep r hr + have one : ∀ o : U64, o.val < 32 → o.val % 8 = 0 → L.mem s' (wa r o) = L.mem s (wa r o) := by + intro o o1 o2 + have v : (wa r o).val = r.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + refine kp _ ?_ fun htw => J.tw_ipcObjA htw (Or.inr (Or.inr ⟨r, hr, by omega, by omega⟩)) + show (wa r o).val % 8 = 0 + omega + have n16 : (layout.reply.NEXT).val = 16 := by unfold layout.reply.NEXT; rfl + have p8 : (layout.reply.PREV).val = 8 := by unfold layout.reply.PREV; rfl + exact ⟨one _ (by omega) (by omega), one _ (by omega) (by omega)⟩ + have buf : BufCover st Z.T Bf s' σ := + J.bufCover_wA w0 (fun u hu K hK htw => J.tw_tcbKA hu (by omega) (Or.inl (by omega)) htw) + (fun q z h => ⟨z, h, rfl, rfl⟩) + have mcp : McpRelA Z.T m (L.mem s') σ := + J.mcp.congrA (fun u hu => ⟨tK u hu 3440#u64 (by simp) (by simp) (by simp), + tK u hu 3456#u64 (by simp) (by simp) (by simp)⟩) (fun _ _ x hx => ⟨x, hx, rfl, rfl⟩) + have yr : YieldRelA Z.T Z.C m (L.mem s') σ := by + have k : ∀ u ∈ Z.T, L.mem s' (yieldA u) = L.mem s (yieldA u) := + fun u hu => tK u hu 3472#u64 (by simp) (by simp) (by simp) + refine ⟨fun u hu => ?_, fun u hu hnz => ?_, fun c hc y n hy p hp => ?_⟩ + · obtain ⟨x, hx, he⟩ := J.yieldRel.tcb u hu + exact ⟨x, hx, by rw [k u hu]; exact he⟩ + · rw [k u hu] at hnz ⊢ + exact J.yieldRel.toSc u hu hnz + · obtain ⟨u, hu, e1, e2⟩ := J.yieldRel.frm c hc y n hy p hp + exact ⟨u, hu, e1, by rw [k u hu]; exact e2⟩ + have bq : BlkQueuedA Z.T E N (L.mem s') σ := + J.blkQ.keepA (fun u hu => by rw [Notif.tBlockA_eq]; exact tK u hu 3336#u64 (by simp) (by simp) (by simp)) + (fun u hu x' hx' => Or.inr ⟨x', hx', rfl⟩) (fun _ _ h => h) (fun _ _ h => h) + have j := J.j57_stepNWA w σ + exact ⟨JStage3A.of_schedWrN (blkQ := bq) (symL := symL) (schedValid := J.schedValid) J (Q := Z.Q) (Rl := Z.R) (curOf := Z.curOf) (curScOf := Z.curScOf) + (s' := s') (a' := a) (σ' := σ) w (SpecSchedA.refl _) (SSzA.refl _) (QKeepA.refl _) rfl J.j.1.ar (fun _ _ => rfl) J.bufAl + ⟨J.ent_stepNWA w blk', sysl⟩ sq poolOff H2 J.scCov ep ntfn reply buf J.idleNoFault mcp yr J.sinv J.sidle rv' + j.1 j.2, + w, J.sigW_osKeepA w, tK⟩ + +-- from `P3/DelNtfnEntA.lean` +/-- The record's `BXI` as `BlkOK`. -/ +theorem JStage3A.blkA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) : + BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s := by + obtain ⟨b1, b2, b3, b4, b5⟩ := J.sys.ent.bxi + exact ⟨b1, b2, b3, b5, b4⟩ + +-- from `P3/DelThrTimerA.lean` +/-- **The record across `timer.remove t`** (module docstring). The spec state is unchanged. -/ +theorem JStage3A.timerRm_recA (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {t : U64} (ht : t ∈ Z.T) + (hS : timer.remove st s t ⦃ s' => SysL st H Z.Cs OS ia ie s' ∧ SymL st H Z.Cs s' ⦄) : + timer.remove st s t ⦃ s' => + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s' a σ m ∧ + WritesIn st s s' (SchedStepNWA (st := st) N E Rp Z m σ s) ∧ OSKeep st OS s s' ∧ + (∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → (K.val < 3528 ∨ 3592 ≤ K.val) → + L.mem s' (wa u K) = L.mem s (wa u K)) ⦄ := by + have blk := J.blkA + have htH : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + refine WP.spec_mono (WP.spec_and (WP.spec_and hS (Fl4ProofsA64.P1.timer.remove.tw (st := st) s t blk.tinv htH)) + (Fl4ProofsA64.P1.timer.remove.rvst (st := st) s t blk.tinv htH)) ?_ + rintro s' ⟨⟨hs, ⟨blk', w0⟩⟩, -, rv⟩ + rw [J.tcbs] at w0 rv + exact J.rec_tdwA blk' rv (w0.mono fun b hb => by + rcases hb with ⟨y, hy, K, k1, k2, e⟩ | ⟨c, hc, o, ho, e⟩ + · exact Or.inl ⟨y, hy, K, k1, by omega, e⟩ + · exact Or.inr ⟨c, hc, o, by omega, e⟩) hs.1 hs.2 + +-- from `P3/Stage3IpcWakeA.lean` +/-- **The SC's thread is bound to it** at the record (moved from `P3/Stage3IpcWakeA`). -/ +theorem JStage3A.scBoundA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {c : U64} (hc : c ∈ Z.C) {y : SchedContext} {k : Nat} {o : ObjRef} + (hk : σ.kheap (scRenA m c) = some (.schedContext y k)) (ho : y.scTcb = some o) : + ∃ x, σ.kheap o = some (.tcb x) ∧ x.tcbSchedContext = some (scRenA m c) := by + obtain ⟨y', k', hy', ht⟩ := J.sr.sch.sc c hc + rw [hk] at hy' + obtain ⟨rfl, -⟩ := KernelObject.schedContext.inj (Option.some.inj hy') + obtain ⟨hz0, e0⟩ := optRef_some (ht.symm.trans ho) + have hz : L.mem s (scTcbA c) ≠ 0#u64 := by simpa [scTcbA, layout.sc.TCB] using hz0 + have e : specPtr (L.mem s (scTcbA c)) = o := by simpa [scTcbA, layout.sc.TCB] using e0 + have hscH : c ∈ H.scs := J.scs.symm ▸ hc + have hX := J.blkA.2.2.1.2.2 + have hT : L.mem s (scTcbA c) ∈ Z.T := J.tcbs ▸ hX.yt.1 c hscH hz + have hb := hX.tsc c hscH (J.scNZ c hc) hz + obtain ⟨x, hx, hxs⟩ := J.sr.sch.tcb _ hT + refine ⟨x, e ▸ hx, ?_⟩ + rw [hxs, hb, optScA, if_neg (J.scNZ c hc)] + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3FiresA.lean b/proofs/Fl4ProofsA64/P3/Stage3FiresA.lean new file mode 100644 index 000000000..da6bb0b81 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3FiresA.lean @@ -0,0 +1,123 @@ +/- +P3 (aarch64, A64-P3 Stage 3, kernel-entry composition, step 1): **the `timer::fire` loop at an invariant**. + +* `timer.fire_recA`: `timer.fire` keeps any invariant `Inv` kept by its three kinds of step, and the spec side is an + `fl4TimerFires` outcome. Each loop iteration is one `fl4TimerFire`: `timer.remove` (spec stutter), at most one + notification signal (`fl4TimerSignal`), then the `REPROGRAM` word (spec `reprogramTimer := true`). +* `JStage3A.fireRecA`: the record with `SwA` gives the exit's `FireRecA` (`P3/Stage3ExitAtA`). +* `JStage3A.timerMinA`: the record places the core's `TIMER_MIN` word (the loop's `hMin`). + +Named premises of `timer.fire_recA` (open, none discharged here): `hRem` ([J], `timer.remove` at the record; on +`JStage3TA` only today, `timerRm_recA`, whose `noSwitch` does not hold after a waking signal), `hSig` ([J], the +record-level signal arm, all four cases of `send_signal`), `hRep` ([J], the `REPROGRAM` write). +-/ +import Fl4ProofsA64.P3.Stage3ExitAtA +import Fl4ProofsA64.P1.IbKeepHolds +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3 Classical + +noncomputable section + +section loop +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {T : Set U64} {Inv : S → A → State Unit → Prop} + +/-- **`timer::fire` at an invariant** (module docstring). -/ +theorem timer.fire_recA + (hMin : ∀ s a σ, Inv s a σ → L.mem s (L.nodeAt s (L.cpu s) layout.node.TIMER_MIN) ≠ 0#u64 → + L.mem s (L.nodeAt s (L.cpu s) layout.node.TIMER_MIN) ∈ T) + (hRem : ∀ s a σ t, Inv s a σ → t ∈ T → timer.remove st s t ⦃ s' => Inv s' a σ ∧ True ⦄) + (hSig : ∀ s a σ t badge, Inv s a σ → t ∈ T → L.mem s (boundA t) ≠ 0#u64 → + notification.send_signal st ar s a (L.mem s (boundA t)) badge ⦃ r => + ∃ σ', fl4TimerSignal σ σ' ∧ Inv r.1 r.2 σ' ⦄) + (hRep : ∀ s a σ c, Inv s a σ → c.val = (L.cpu s).val → obj.node_set st s c layout.node.REPROGRAM 1#u64 ⦃ s' => + Inv s' a { σ with reprogramTimer := true } ⦄) + (s : S) (a : A) (σ : State Unit) (h : Inv s a σ) : + timer.fire st ar s a ⦃ r => ∃ σ1, fl4TimerFires σ σ1 ∧ Inv r.1 r.2 σ1 ⦄ := by + unfold timer.fire + refine WP.spec_bind (L.cur_cpu_spec s) fun c _ => ?_ + refine WP.spec_bind (obj.node_get.spec s c _) fun now _ => ?_ + unfold timer.fire_loop + apply loop.spec_decr_nat (measure := fun x => 32 - x.2.2.val) + (inv := fun x => ∃ σ1, fl4TimerFires σ σ1 ∧ Inv x.1 x.2.1 σ1) + · rintro ⟨s1, a1, i1⟩ ⟨σ1, hf, h1⟩ + simp only at hf h1 + unfold timer.fire_loop.body + dsimp only + split + · rename_i hi + have hi32 : i1.val < 32 := by simpa [timer.FIRE_BATCH] using hi + step with LawfulStore.cur_cpu_spec as ⟨c, ec⟩ + step with obj.node_get.spec as ⟨t, et⟩ + step with obj.Tcb.is_null.spec as ⟨b, eb⟩ + split + · exact (WP.spec_ok _).2 ⟨σ1, hf, h1⟩ + rename_i hb + have ht0 : t ≠ 0#u64 := fun e => hb (by rw [eb, e]; rfl) + have htT : t ∈ T := by + have := hMin s1 a1 σ1 h1 + have ec' : c = L.cpu s1 := by + first | exact ec | exact UScalar.eq_of_val_eq ec + rw [ec'] at et + rw [← et] at this + exact this ht0 + step with obj.Tcb.timer_deadline.val as ⟨d, ed⟩ + split + · exact (WP.spec_ok _).2 ⟨σ1, hf, h1⟩ + step with obj.Tcb.timer_badge.val as ⟨bd, ebd⟩ + refine WP.spec_bind (hRem s1 a1 σ1 t h1 htT) fun s2 h2 => ?_ + step with obj.Tcb.bound_ntfn.val as ⟨n, en⟩ + refine WP.spec_bind (Pₘ := fun r : S × A => ∃ σm, (σm = σ1 ∨ fl4TimerSignal σ1 σm) ∧ Inv r.1 r.2 σm) ?_ ?_ + · split + · rename_i hn0 + have hn0' : L.mem s2 (boundA t) ≠ 0#u64 := by rw [← en]; exact bne_ne hn0 + rw [en] + exact WP.spec_mono (hSig s2 a1 σ1 t bd h2.1 htT hn0') fun r ⟨σ', hs, hI⟩ => ⟨σ', Or.inr hs, hI⟩ + · exact (WP.spec_ok _).2 ⟨σ1, Or.inl rfl, h2.1⟩ + rintro ⟨s3, a3⟩ ⟨σm, hm, h3⟩ + simp only at h3 + try simp only [WP.uncurry'_pair] + step with LawfulStore.cur_cpu_spec as ⟨c3, ec3⟩ + refine WP.spec_bind (hRep s3 a3 σm c3 h3 (by rw [ec3])) fun s4 h4 => ?_ + step as ⟨i4, ei4⟩ + try simp only [WP.spec_ok] + exact ⟨⟨_, hf.tail ⟨σm, hm, rfl⟩, h4⟩, by omega⟩ + · exact (WP.spec_ok _).2 ⟨σ1, hf, h1⟩ + · exact ⟨σ, Relation.ReflTransGen.refl, h⟩ + +end loop + +section record +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s : S} {a : A} {σ : State Unit} {m : McState} + +/-- **`FireRecA` from the record and `SwA`.** -/ +theorem JStage3A.fireRecA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) (hSw : SwA σ) : FireRecA σ := by + refine ⟨SchedValidA.toFIA J.schedValid, fun x hx => ?_, hSw, J.sr.run.2⟩ + obtain ⟨y, hy, hs⟩ := J.sinv.idleTcb + rw [hx] at hy + have := KernelObject.tcb.inj (Option.some.inj hy) + subst this + exact hs + +/-- **The record places the core's `TIMER_MIN` word.** -/ +theorem JStage3A.timerMinA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (h0 : L.mem s (L.nodeAt s (L.cpu s) layout.node.TIMER_MIN) ≠ 0#u64) : + L.mem s (L.nodeAt s (L.cpu s) layout.node.TIMER_MIN) ∈ Z.T := by + obtain ⟨⟨Q1, R1, h1q⟩, -, hX1, -, -⟩ := J.sys.ent.bxi + have := hX1.2.2.tm.min _ h1q.cur + rcases this with e | e + · exact absurd e h0 + · exact J.tcbs ▸ e + +end record + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3FirstScopeA.lean b/proofs/Fl4ProofsA64/P3/Stage3FirstScopeA.lean new file mode 100644 index 000000000..20aee2cf8 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3FirstScopeA.lean @@ -0,0 +1,532 @@ +/- +P3 (aarch64, A64-P3 Stage 3, line recvcomb): **the first-half scope of the combined calls** (`ReplyRecv`, `NBSendRecv`, +`NBSendWait`) and their `dispatch_syscall`. + +* **`FirstScopeA st OS N E Rp s σ cptr`**: per combined call (indexed by the entry pre-state `(s, σ)` and the register + value `cptr` the call names: REPLY, or the NBSend destination), one constructor per existing first half, each with + its [S] selectors as data. `FirstPfxA` is the shared prefix (message info, IPC buffer, slot lookup, extra caps). + * Real first halves (each ends in `RecvPreA`): `nbSendEp` (endpoint, no receiver), `nbSignalQuiet`, + `nbSignalWaiting`, `nbSignalBound` (notification), `nbSendDeliver`, `nbSendDon` (a waiting receiver), `replyLive`, + `replyPassive` (live Reply with a caller). + * **F1**, failing first halves: `failDecode` (the cap found is one of `FirstFailA`: Null, Zombie, Endpoint / + Notification without the send right, and, because `first_phase = true`, Thread / CNode / SchedContext / + SchedControl / Domain: all `invalidCapability 0` on both sides), `lookupFail` and `extraFail` + (`lookup_slot` / `lookup_extra_caps` fail: with `blocking = false` both sides return without a fault). With `calling = false` the decode error is + swallowed: `handle_invocation` returns `Ok` with the store and the thread state unchanged (so the receive half runs at the *same* `(s, σ)`, `RecvPreA` from + `JSyscallA.toRecvPreA`). This includes the first ReplyRecv of every server (REPLY register Null). + * **Declared [S] scope limits.** F2: a first half that performs a real invocation (Untyped / IRQ / Arch, or a Reply + to a faulted caller: `do_reply_transfer` is a real transfer, not a decode error) has no constructor. F3: no + in-scope first half returns `Err Preempted` (every one returns `Ok` concretely), so `DispatchBranchesA.preempt` is + discharged by contradiction and `afterPreemptA` is not needed; real preemption belongs to F2. +* `JSyscallA.firstScopeA`: a scope member gives `handle_invocation = ok (.Ok (), s3, a)`, the spec outcome at `σ3` and + `RecvPreA` at `(s3, σ3)`. +* `JSyscallA.combinedBodyScopeA`: `dispatch_syscall` for the three calls: `DispatchBranchesA.inv` from the first half, + `done` absurd (`first = true`), `preempt` absurd, `combined` = `recvDispatchA` at `(s3, σ3)` over a `RecvScopeA` + member **pinned to that `(s3, σ3)`**: the premise `Sc2` is guarded by the first half's concrete equations. + +Named premises: those of the first halves ([P] `ArchTotal`, `MaxSpi`, `ArchPlat`, `A64FrameMasks`, `hCs`, `hmb`, +`hmax`, `htu`, `hag`, `hconv`; [S] `numNodes = 1`) and of `recvDispatchA`; [S] the constructors' selectors and `Sc2`. +No new kind. New module. +-/ +import Fl4ProofsA64.P3.Stage3CombinedFrontA +import Fl4ProofsA64.P3.Stage3NBWakeEntryA +import Fl4ProofsA64.P3.Stage3RecvScopeA +import Fl4ProofsA64.P3.Stage3ReplyErrTopA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +section +variable {S : Type} (st : machine.Store S) [L : LawfulStore st] + +/-- [S] **The prefix of `handle_invocation`** a first half starts from: the message info, the IPC buffer, the slot +lookup of the register value `cptr` and the extra caps all succeed. -/ +structure FirstPfxA (s : S) (cptr : U64) (info : msginfo.MessageInfo) (buf : U64) (r1 : cspace.Resolved) + (e : Aeneas.Std.Array obj.Slot 3#usize) : Prop where + einfo : msg.msg_info st s (L.curW s) = ok info + ebuf : msg.lookup_ipc_buffer st s false (L.curW s) = ok buf + elr : cspace.lookup_slot st s (L.curW s) cptr = ok (.Ok r1) + eex : ipc.lookup_extra_caps st s (L.curW s) buf info = ok (.Ok e) + +/-- The caps whose decode fails with `invalidCapability 0` on both sides at `first_phase = true` (F1). -/ +inductive FirstFailA : capability.Cap → Prop where + | null : FirstFailA .Null + | epNoSend (p badge : U64) (r g gr : Bool) : FirstFailA (.Endpoint p badge false r g gr) + | ntNoSend (p badge : U64) (r : Bool) : FirstFailA (.Notification p badge false r) + | thread (p : U64) : FirstFailA (.Thread p) + | domain : FirstFailA .Domain + | sc (p : U64) (sb : U64) : FirstFailA (.SchedContext p sb) + | schedControl (cpu : U64) : FirstFailA (.SchedControl cpu) + | cnode (p r gs g : U64) : FirstFailA (.CNode p r gs g) + | zombie (p zt n : U64) : FirstFailA (.Zombie p zt n) + +/-- **The first-half scope of a combined call** (module docstring). -/ +inductive FirstScopeA (OS N E Rp : Set U64) (s : S) (σ : State Unit) (cptr : U64) : Prop where + /-- NBSend to an endpoint with no receiver (`nbSendRecvEntryA`). -/ + | nbSendEp {info : msginfo.MessageInfo} {buf : U64} {r1 : cspace.Resolved} {e : Aeneas.Std.Array obj.Slot 3#usize} + {p badge : U64} {recv grant grantReply : Bool} {x : Endpoint} + (P : FirstPfxA st s cptr info buf r1 e) (hcap : capA st s r1.slot = .Endpoint p badge true recv grant grantReply) + (hp : p ∈ E) (hk : σ.kheap (specPtr p) = some (.endpoint x)) (hc : CanBlockA false x) : + FirstScopeA OS N E Rp s σ cptr + /-- NBSend to a notification, quiet (`nbSignalRecvEntryA`). -/ + | nbSignalQuiet {info : msginfo.MessageInfo} {buf : U64} {r1 : cspace.Resolved} + {e : Aeneas.Std.Array obj.Slot 3#usize} {p badge : U64} {recv : Bool} {x : Notification} + (P : FirstPfxA st s cptr info buf r1 e) (hcap : capA st s r1.slot = .Notification p badge true recv) + (hp : p ∈ N) (hk : σ.kheap (specPtr p) = some (.notification x)) (hq : QuietSigA σ x) : + FirstScopeA OS N E Rp s σ cptr + /-- NBSend to a notification with waiters (`nbSignalWaitingRecvEntryA`). -/ + | nbSignalWaiting {info : msginfo.MessageInfo} {buf : U64} {r1 : cspace.Resolved} + {e : Aeneas.Std.Array obj.Slot 3#usize} {p badge : U64} {recv : Bool} {x : Notification} {q : List ObjRef} + (P : FirstPfxA st s cptr info buf r1 e) (hcap : capA st s r1.slot = .Notification p badge true recv) + (hp : p ∈ N) (hk : σ.kheap (specPtr p) = some (.notification x)) (hw : x.ntfnObj = .waitingNtfn q) : + FirstScopeA OS N E Rp s σ cptr + /-- NBSend to an idle notification with a bound receiver (`nbSignalBoundRecvEntryA`). -/ + | nbSignalBound {info : msginfo.MessageInfo} {buf : U64} {r1 : cspace.Resolved} + {e : Aeneas.Std.Array obj.Slot 3#usize} {p badge : U64} {recv : Bool} {x : Notification} {t : ObjRef} + {xt : Tcb} {e' : ObjRef} {ro : Option ObjRef} {d : ReceiverPayload} + (P : FirstPfxA st s cptr info buf r1 e) (hcap : capA st s r1.slot = .Notification p badge true recv) + (hp : p ∈ N) (hk : σ.kheap (specPtr p) = some (.notification x)) (hidle : x.ntfnObj = .idleNtfn) + (hb : x.ntfnBoundTcb = some t) (hkt : σ.kheap t = some (.tcb xt)) + (hs : xt.tcbState = .blockedOnReceive e' ro d) : + FirstScopeA OS N E Rp s σ cptr + /-- NBSend delivering to a waiting receiver that has an SC (`nbSendDeliverRecvEntryA`). -/ + | nbSendDeliver {info : msginfo.MessageInfo} {buf : U64} {r1 : cspace.Resolved} + {e : Aeneas.Std.Array obj.Slot 3#usize} {p badge : U64} {recv grant grantReply : Bool} {d : U64} + {q : List ObjRef} + (P : FirstPfxA st s cptr info buf r1 e) (hcap : capA st s r1.slot = .Endpoint p badge true recv grant grantReply) + (hp : p ∈ E) (hk : σ.kheap (specPtr p) = some (.endpoint (.recvEP (specPtr d :: q)))) + (hsnd : ∀ x, σ.kheap σ.curThread = some (.tcb x) → x.tcbFault = none ∧ (grant = false ∨ + ipcBufOfA false x = none ∨ + unat (dataToMessageInfo (x.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0)) + (hrcv : ∀ y, σ.kheap (specPtr d) = some (.tcb y) → y.tcbSchedContext ≠ none) + (hdne : specPtr d ≠ σ.curThread) : + FirstScopeA OS N E Rp s σ cptr + /-- NBSend donating to a passive waiting receiver (`nbSendDonRecvEntryA`). [S] `hV`: no bound VCPU. -/ + | nbSendDon {info : msginfo.MessageInfo} {buf : U64} {r1 : cspace.Resolved} + {e : Aeneas.Std.Array obj.Slot 3#usize} {p badge : U64} {recv grant grantReply : Bool} {d : U64} + {q : List ObjRef} + (P : FirstPfxA st s cptr info buf r1 e) (hcap : capA st s r1.slot = .Endpoint p badge true recv grant grantReply) + (hp : p ∈ E) (hk : σ.kheap (specPtr p) = some (.endpoint (.recvEP (specPtr d :: q)))) + (hsnd : ∀ x, σ.kheap σ.curThread = some (.tcb x) → x.tcbFault = none ∧ (grant = false ∨ + ipcBufOfA false x = none ∨ + unat (dataToMessageInfo (x.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0)) + (hrcv : ∀ y, σ.kheap (specPtr d) = some (.tcb y) → y.tcbSchedContext = none) + (hV : L.mem s (core.num.U64.wrapping_add (L.curW s) 3664#u64) = 0#u64) : + FirstScopeA OS N E Rp s σ cptr + /-- A live Reply with a caller that has its own SC, the replier keeping its SC (`replyRecvEntryA`). -/ + | replyLive {info : msginfo.MessageInfo} {buf : U64} {r1 : cspace.Resolved} + {e : Aeneas.Std.Array obj.Slot 3#usize} {r : U64} {grant : Bool} {xr : Reply} {d : U64} + (P : FirstPfxA st s cptr info buf r1 e) (hcap : capA st s r1.slot = .Reply r grant) (hr : r ∈ Rp) + (hk : σ.kheap (specPtr r) = some (.reply xr)) (hcl : xr.replyTcb = some (specPtr d)) + (hsnd : ∀ x, σ.kheap σ.curThread = some (.tcb x) → x.tcbFault = none ∧ (grant = false ∨ + ipcBufOfA false x = none ∨ + unat (dataToMessageInfo (x.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0)) + (hrcv : ∀ y, σ.kheap (specPtr d) = some (.tcb y) → + y.tcbState = .blockedOnReply (specPtr r) ∧ y.tcbFault = none) + (hdsc : ∀ y, σ.kheap (specPtr d) = some (.tcb y) → y.tcbSchedContext ≠ none) + (hnt : ∀ y, σ.kheap (specPtr d) = some (.tcb y) → isEpCap y.tcbTimeoutHandler = false) : + FirstScopeA OS N E Rp s σ cptr + /-- A live Reply, the replier passive (`replyPassiveRecvEntryA`). -/ + | replyPassive {info : msginfo.MessageInfo} {buf : U64} {r1 : cspace.Resolved} + {e : Aeneas.Std.Array obj.Slot 3#usize} {r : U64} {grant : Bool} {xr : Reply} {d : U64} + (P : FirstPfxA st s cptr info buf r1 e) (hcap : capA st s r1.slot = .Reply r grant) (hr : r ∈ Rp) + (hk : σ.kheap (specPtr r) = some (.reply xr)) (hcl : xr.replyTcb = some (specPtr d)) + (hsnd : ∀ x, σ.kheap σ.curThread = some (.tcb x) → x.tcbFault = none ∧ (grant = false ∨ + ipcBufOfA false x = none ∨ + unat (dataToMessageInfo (x.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0)) + (hrcv : ∀ y, σ.kheap (specPtr d) = some (.tcb y) → + y.tcbState = .blockedOnReply (specPtr r) ∧ y.tcbFault = none) + (hdsc : ∀ y, σ.kheap (specPtr d) = some (.tcb y) → y.tcbSchedContext = none) + (hsw : scWithReply (specPtr r) σ = some σ.curSc) + (hhd : ∀ y k, σ.kheap σ.curSc = some (.schedContext y k) → y.scReplies.head? = some (specPtr r)) + (hnt : ∀ y, σ.kheap (specPtr d) = some (.tcb y) → isEpCap y.tcbTimeoutHandler = false) : + FirstScopeA OS N E Rp s σ cptr + /-- **F1**: the cap found fails to decode (`FirstFailA`): the first half is swallowed. This is the first ReplyRecv + of every server (REPLY register Null). -/ + | failDecode {info : msginfo.MessageInfo} {buf : U64} {r1 : cspace.Resolved} + {e : Aeneas.Std.Array obj.Slot 3#usize} {c : capability.Cap} + (P : FirstPfxA st s cptr info buf r1 e) (hcap : capA st s r1.slot = c) (hf : FirstFailA c) : + FirstScopeA OS N E Rp s σ cptr + /-- **F1**: the register's CSpace walk fails (`lookup_slot = Err`, a fact at `s`): with `blocking = false` the first + half is a no-op on both sides. -/ + | lookupFail {lf : error.LookupFault} (hl : cspace.lookup_slot st s (L.curW s) cptr = ok (.Err lf)) : + FirstScopeA OS N E Rp s σ cptr + /-- **F1**: the extra-caps lookup fails (`lookup_extra_caps = Err`, a fact at `s`): likewise a no-op. -/ + | extraFail {info : msginfo.MessageInfo} {buf : U64} {r1 : cspace.Resolved} {f : error.Fault} + (einfo : msg.msg_info st s (L.curW s) = ok info) (ebuf : msg.lookup_ipc_buffer st s false (L.curW s) = ok buf) + (elr : cspace.lookup_slot st s (L.curW s) cptr = ok (.Ok r1)) + (hx : ipc.lookup_extra_caps st s (L.curW s) buf info = ok (.Err f)) : + FirstScopeA OS N E Rp s σ cptr +end + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s : S} {a : A} {σ : State Unit} {m : McState} + +/-- The Rust decode of a failing cap at `first_phase = true`: the error, store and arch unchanged. -/ +theorem decodeFail_rustA {c : capability.Cap} (hf : FirstFailA c) (lbl length : U64) (slot : obj.Slot) + (block call don : Bool) (buf : U64) (extra : Aeneas.Std.Array obj.Slot 3#usize) (s : S) (a : A) : + syscall.decode_invocation st ar s a lbl length slot c block call don true buf extra = + ok (.Err (error.Exc.Sys (error.SysError.InvalidCapability 0#u64)), s, a) := by + unfold syscall.decode_invocation + rw [cur_thread_ok s, bind_ok] + cases hf <;> rfl + +/-- The spec decode of a failing cap at `firstPhase = true`: one `invalidCapability 0`. -/ +theorem decodeFail_specA {c : capability.Cap} (hf : FirstFailA c) (calling : Bool) (lbl : Data) (args : List Data) + (ci : CapRef) (sl : CslotPtr) (excaps : List (Cap × CslotPtr)) (buffer : Option ObjRef) (σ : State Unit) : + (fl4DecodeInvocation calling true lbl args ci sl (absCapA c) excaps buffer).run σ = + ({(.error (.invalidCapability 0), σ)}, False) := by + cases hf with + | null => simp [fl4DecodeInvocation, absCapA] + | epNoSend p badge r g gr => exact decodeEndpoint_specA.trans (if_neg Bool.false_ne_true) + | ntNoSend p badge r => exact decodeNotification_specA.trans (if_neg Bool.false_ne_true) + | thread p => simp [fl4DecodeInvocation, absCapA] + | domain => simp [fl4DecodeInvocation, absCapA] + | sc p sb => simp [fl4DecodeInvocation, absCapA] + | schedControl cpu => simp [fl4DecodeInvocation, absCapA] + | cnode p r gs g => simp [fl4DecodeInvocation, absCapA] + | zombie p zt n => simp [fl4DecodeInvocation, absCapA] + +set_option maxHeartbeats 2000000 in +/-- **F1: a failing first half is swallowed** (module docstring): `handle_invocation` of the first phase on a cap in +`FirstFailA` returns `Ok` with the store unchanged, and the spec's returns `ok` at the same `σ`. -/ +theorem JSyscallA.firstFailA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + (rep : Bool) {cptr : U64} {info : msginfo.MessageInfo} {buf : U64} {r1 : cspace.Resolved} + {e : Aeneas.Std.Array obj.Slot 3#usize} (P : FirstPfxA st s cptr info buf r1 e) {c : capability.Cap} + (hcap : capA st s r1.slot = c) (hf : FirstFailA c) : + syscall.handle_invocation st ar s a false false true true cptr = ok (.Ok (), s, a) ∧ + (.ok (), σ) ∈ ((fl4HandleInvocationR (Z := Unit) rep false false true true + (Fl4SpecA64.toBl cptr.bv)).run σ).1 := by + constructor + · unfold syscall.handle_invocation + rw [cur_thread_ok s, bind_ok, P.einfo, bind_ok, P.elr, bind_ok] + dsimp only + rw [capA_eq, bind_ok, P.ebuf, bind_ok, P.eex, bind_ok] + dsimp only + have elen : (if info.length > msg.N_MSG_REGISTERS then + if buf = 0#u64 then ok msg.N_MSG_REGISTERS else ok info.length + else ok info.length) = ok (msgLenA info buf) := by + unfold msgLenA; split <;> (try split) <;> rfl + rw [elen, bind_ok, hcap, decodeFail_rustA hf, bind_ok] + rfl + · have ct : σ.curThread = specPtr (L.curW s) := J.j.1.sch.curThread + obtain ⟨mi, sl, c', excaps, buffer, argsU, F⟩ := J.frontJA P.einfo P.ebuf P.elr P.eex + have gmi := F.pin.hmi + have front : (frontSpecA σ.curThread (Fl4SpecA64.toBl cptr.bv) mi).run σ = + ({(.ok (sl, c', excaps, buffer), σ)}, False) := by + have htZ : L.curW s ∈ Z.T := J.sr.curT + have hth : Z.th (L.curW s) = some .tcb := (J.j.1.thT _).2 htZ + obtain ⟨lr, elr', se, hse, hrel⟩ := spec_existsA (lookup_slot_refinesA Z.th s σ J.j.1.st J.j.1.cv _ hth cptr) + have el : lr = .Ok r1 := okinj (elr'.symm.trans P.elr) + subst el + cases se with + | error _ => exact hrel.1.elim + | ok v => + obtain ⟨sl', rest⟩ := v + obtain ⟨⟨hsl, -⟩, p, hp1, hp, -⟩ := hrel + obtain ⟨c'', -, hgc⟩ := J.j.1.st.slot_capA hp1 hp + have e1 : sl' = sl := cslotAddrA_inj J.j.1.place ⟨p, hp1, Or.inl hp⟩ F.pin.habs + (hsl.symm.trans F.pin.hslot) + subst e1 + have e2 : c'' = c' := single_fstA (hgc.symm.trans F.pin.hcap) + subst e2 + rw [← ct] at hse + unfold frontSpecA + rw [run_bind_okA (lcs_okA (ofBl (Fl4SpecA64.toBl cptr.bv)) hse hgc)] + dsimp only + rw [run_bind_okA (run_liftEA F.pin.hbuf), run_bind_okA F.pin.hex] + rfl + have gc : (NonDet.gets fun s : Fl4SpecA64.State Unit => s.curThread) σ = ({(σ.curThread, σ)}, False) := rfl + rw [fl4HandleInvocationR_firstA, run_bind_okA (run_liftEA gc)] + unfold withoutPreemption + rw [run_bind_okA (run_liftEA gmi)] + have cEq : absCapA c = c' := by + have h2 : CapEqA (capA st s r1.slot) c' := F.cap.2 + rw [hcap] at h2 + cases hf <;> exact h2 + refine ReplyErr.syscall_errA (e' := .invalidCapability 0) front ?_ ?_ + · dsimp only + rw [run_bind_okA (run_liftEA F.pin.hargs)] + rw [← cEq, decodeFail_specA hf] + exact Set.mem_singleton _ + · unfold Fl4Spec.NonDet.when + rw [if_neg Bool.false_ne_true] + exact Set.mem_singleton _ + + +set_option maxHeartbeats 4000000 in +/-- **F1: a failing slot lookup is a no-op** (`blocking = false`): FL4 returns `Ok` at `(s, a)`, the spec's fault +handler is `NonDet.when false`. -/ +theorem JSyscallA.lookupFailA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + (rep : Bool) {cptr : U64} {lf : error.LookupFault} + (hl : cspace.lookup_slot st s (L.curW s) cptr = ok (.Err lf)) : + syscall.handle_invocation st ar s a false false true true cptr = ok (.Ok (), s, a) ∧ + (.ok (), σ) ∈ ((fl4HandleInvocationR (Z := Unit) rep false false true true + (Fl4SpecA64.toBl cptr.bv)).run σ).1 := by + have ct : σ.curThread = specPtr (L.curW s) := J.j.1.sch.curThread + have htZ : L.curW s ∈ Z.T := J.sr.curT + have hth : Z.th (L.curW s) = some .tcb := (J.j.1.thT _).2 htZ + obtain ⟨x, hk, -⟩ := J.ep.tcb (L.curW s) htZ + constructor + · obtain ⟨info, einfo, -⟩ := spec_existsA (msg_info_specA (L := L) s (L.curW s)) + unfold syscall.handle_invocation + rw [cur_thread_ok s, bind_ok, einfo, bind_ok, hl, bind_ok] + simp + · have gmi : getMessageInfo (Z := Unit) σ.curThread σ = + ({(Fl4SpecA64.dataToMessageInfo (x.tcbArch.tcbContext.userRegs Fl4SpecA64.msg_info_register), σ)}, False) := by + rw [ct]; exact getMessageInfo_eqA hk + have gc : (NonDet.gets fun s : Fl4SpecA64.State Unit => s.curThread) σ = ({(σ.curThread, σ)}, False) := rfl + rw [fl4HandleInvocationR_firstA, run_bind_okA (run_liftEA gc)] + unfold withoutPreemption + rw [run_bind_okA (run_liftEA gmi)] + obtain ⟨lr, elr', se, hse, hrel⟩ := spec_existsA (lookup_slot_refinesA Z.th s σ J.j.1.st J.j.1.cv _ hth cptr) + have el : lr = .Err lf := okinj (elr'.symm.trans hl) + subst el + cases se with + | ok _ => exact hrel.1.elim + | error lf' => + rw [← ct] at hse + have front : (frontSpecA σ.curThread (Fl4SpecA64.toBl cptr.bv) + (Fl4SpecA64.dataToMessageInfo (x.tcbArch.tcbContext.userRegs Fl4SpecA64.msg_info_register))).run σ = + ({(.error (.capFault (ofBl (Fl4SpecA64.toBl cptr.bv)) false lf'), σ)}, False) := by + unfold frontSpecA + rw [run_bind_errorA (lcs_errA (ofBl (Fl4SpecA64.toBl cptr.bv)) hse)] + refine syscall_faultA (v := ()) (τ := σ) front ?_ + unfold Fl4Spec.NonDet.when + rw [if_neg Bool.false_ne_true] + exact Set.mem_singleton _ + +set_option maxHeartbeats 4000000 in +/-- **F1: a failing extra-caps lookup is a no-op** (`blocking = false`), as `lookupFailA`. -/ +theorem JSyscallA.extraFailA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + (rep : Bool) {cptr : U64} {info : msginfo.MessageInfo} {buf : U64} {r1 : cspace.Resolved} {f : error.Fault} + (einfo : msg.msg_info st s (L.curW s) = ok info) (ebuf : msg.lookup_ipc_buffer st s false (L.curW s) = ok buf) + (elr : cspace.lookup_slot st s (L.curW s) cptr = ok (.Ok r1)) + (hx : ipc.lookup_extra_caps st s (L.curW s) buf info = ok (.Err f)) : + syscall.handle_invocation st ar s a false false true true cptr = ok (.Ok (), s, a) ∧ + (.ok (), σ) ∈ ((fl4HandleInvocationR (Z := Unit) rep false false true true + (Fl4SpecA64.toBl cptr.bv)).run σ).1 := by + have ct : σ.curThread = specPtr (L.curW s) := J.j.1.sch.curThread + have htZ : L.curW s ∈ Z.T := J.sr.curT + have hth : Z.th (L.curW s) = some .tcb := (J.j.1.thT _).2 htZ + obtain ⟨x, hk, -⟩ := J.ep.tcb (L.curW s) htZ + constructor + · unfold syscall.handle_invocation + rw [cur_thread_ok s, bind_ok, einfo, bind_ok, elr, bind_ok] + dsimp only + rw [capA_eq, bind_ok, ebuf, bind_ok, hx, bind_ok] + simp + · set t := L.curW s with ht + obtain ⟨info', einfo', -, -, hext, -⟩ := spec_existsA (msg_info_specA (L := L) s t) + have ei : info' = info := okinj (einfo'.symm.trans einfo) + subst ei + set mi := Fl4SpecA64.dataToMessageInfo (x.tcbArch.tcbContext.userRegs Fl4SpecA64.msg_info_register) with hmi + have hmw := (J.ep.at_tcb htZ hk).regView.1 + have gmi : getMessageInfo (Z := Unit) (specPtr t) σ = ({(mi, σ)}, False) := getMessageInfo_eqA hk + obtain ⟨buf', ebuf', hbuf0, hbufS⟩ := spec_existsA + (lookup_buf_full s σ J.j.1.st hth htZ hk J.bufCover J.bufNative false) + have eb : buf' = buf := okinj (ebuf'.symm.trans ebuf) + subst eb + have eIB : lookupIpcBuffer (Z := Unit) false (specPtr t) σ = ({(ipcBufOfA false x, σ)}, False) := + lookupIpcBuffer_eqA hk false + have hBV : BufViewA (L.mem s) σ.machineState.underlyingMemory buf' (ipcBufOfA false x) := by + cases ho : ipcBufOfA false x with + | none => rw [hbuf0.2 ho]; exact bufViewA_none _ _ + | some sb => + refine J.ep.bufView (hbufS sb ho) fun h => ?_ + rw [hbuf0.1 h] at ho; cases ho + obtain ⟨lr, elr', se, hse, hrel⟩ := spec_existsA (lookup_slot_refinesA Z.th s σ J.j.1.st J.j.1.cv t hth cptr) + have el : lr = .Ok r1 := okinj (elr'.symm.trans elr) + subst el + cases se with + | error _ => exact hrel.1.elim + | ok v => + obtain ⟨sl, rest⟩ := v + obtain ⟨⟨hsl, -⟩, p, hp1, hp, -⟩ := hrel + obtain ⟨c, hcr, hgc⟩ := J.j.1.st.slot_capA hp1 hp + have hn : info'.extra_caps.val = unat mi.miExtraCaps := by + show info'.extra_caps.bv.toNat = _; rw [hext, hmw] + have hn3 : info'.extra_caps.val ≤ 3 := by + show info'.extra_caps.bv.toNat ≤ 3; rw [hext, hmw]; exact extra_le3A _ + obtain ⟨r2, er2, hx2⟩ := spec_existsA + (lookup_extra_relA J.j.1.st J.j.1.cv hth buf' (ipcBufOfA false x) hBV info' mi hn hn3) + have ee : r2 = .Err f := okinj (er2.symm.trans hx) + subst ee + obtain ⟨f', hrun, -⟩ := hx2 + have front : (frontSpecA σ.curThread (Fl4SpecA64.toBl cptr.bv) mi).run σ = + ({(.error f', σ)}, False) := by + rw [← ct] at hse + unfold frontSpecA + rw [run_bind_okA (lcs_okA (ofBl (Fl4SpecA64.toBl cptr.bv)) hse hgc)] + dsimp only + rw [run_bind_okA (run_liftEA (show lookupIpcBuffer (Z := Unit) false σ.curThread σ = + ({(ipcBufOfA false x, σ)}, False) by rw [ct]; exact eIB))] + rw [← ct] at hrun + rw [run_bind_errorA hrun] + have gc : (NonDet.gets fun s : Fl4SpecA64.State Unit => s.curThread) σ = ({(σ.curThread, σ)}, False) := rfl + rw [fl4HandleInvocationR_firstA, run_bind_okA (run_liftEA gc)] + unfold withoutPreemption + rw [run_bind_okA (run_liftEA (show getMessageInfo (Z := Unit) σ.curThread σ = ({(mi, σ)}, False) by + rw [ct]; exact gmi))] + refine syscall_faultA (v := ()) (τ := σ) front ?_ + unfold Fl4Spec.NonDet.when + rw [if_neg Bool.false_ne_true] + exact Set.mem_singleton _ + +set_option maxHeartbeats 4000000 in +/-- **A first-half scope member gives the first half and `RecvPreA`** (module docstring). -/ +theorem JSyscallA.firstScopeA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hn : fl4A64Platform.numNodes ≤ n) + (hag : fl4MinBudget = MIN_BUDGET) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) (hconv : Fl4TicksToUsLegacyAgree) + (rep : Bool) {cptr : U64} (Sc : FirstScopeA st OS N E Rp s σ cptr) : + ∃ s3 σ3, syscall.handle_invocation st ar s a false false true true cptr = ok (.Ok (), s3, a) ∧ + (.ok (), σ3) ∈ ((fl4HandleInvocationR (Z := Unit) rep false false true true + (Fl4SpecA64.toBl cptr.bv)).run σ).1 ∧ + ∃ Q Rl stack', RecvPreA st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack' κ n + (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s3 a σ3 m := by + have hmb' : ∀ a, Fl4MinBudgetAgreeA ar a := fun a => WP.spec_mono (hmb a) (fun mb h => h.trans hag.symm) + have hcore : m.curCore < fl4A64Platform.numNodes := by rw [J.j.1.sch.core]; exact hCs _ J.j.1.sch.cpuCs + cases Sc with + | nbSendEp P hcap hp hk hc => + obtain ⟨s3, σ3, e1, e2, -, -, -, Q, Rl, R⟩ := J.nbSendRecvEntryA hA hPl rep P.einfo P.ebuf P.elr P.eex hcap hp hk hc + exact ⟨s3, σ3, e1, e2, Q, Rl, stack, R⟩ + | nbSignalQuiet P hcap hp hk hq => + obtain ⟨s3, σ3, e1, e2, -, -, -, Q, Rl, R⟩ := J.nbSignalRecvEntryA hA hM hPl hfm hcore hmb hmax htu rep P.einfo P.ebuf + P.elr P.eex hcap hp hk hq + exact ⟨s3, σ3, e1, e2, Q, Rl, stack, R⟩ + | nbSignalWaiting P hcap hp hk hw => + obtain ⟨s3, σ3, e1, e2, -, -, -, Q, Rl, R⟩ := J.nbSignalWaitingRecvEntryA hA hM hPl hfm hCs h1 hn hmb hmax htu rep + P.einfo P.ebuf P.elr P.eex hcap hp hk hw + exact ⟨s3, σ3, e1, e2, Q, Rl, stack, R⟩ + | nbSignalBound P hcap hp hk hidle hb hkt hs => + obtain ⟨s3, σ3, e1, e2, -, -, -, Q, Rl, R⟩ := J.nbSignalBoundRecvEntryA hA hM hPl hfm hCs h1 hn hmb hmax htu rep + P.einfo P.ebuf P.elr P.eex hcap hp hk hidle hb hkt hs + exact ⟨s3, σ3, e1, e2, Q, Rl, stack, R⟩ + | nbSendDeliver P hcap hp hk hsnd hrcv hdne => + obtain ⟨s3, σ3, e1, e2, -, -, -, Q, Rl, R⟩ := J.nbSendDeliverRecvEntryA hA hM hPl hfm hCs h1 hmb hmax htu rep + P.einfo P.ebuf P.elr P.eex hcap hp hk hsnd hrcv hdne + exact ⟨s3, σ3, e1, e2, Q, Rl, stack, R⟩ + | nbSendDon P hcap hp hk hsnd hrcv hV => + obtain ⟨s3, σ3, e1, e2, -, -, -, Q, Rl, R⟩ := J.nbSendDonRecvEntryA hA hM hPl hfm hCs h1 hmb hmax htu rep + P.einfo P.ebuf P.elr P.eex hcap hp hk hsnd hrcv hV + exact ⟨s3, σ3, e1, e2, Q, Rl, stack, R⟩ + | replyLive P hcap hr hk hcl hsnd hrcv hdsc hnt => + obtain ⟨s3, σ3, e1, e2, -, -, -, Q, Rl, st', R⟩ := J.replyRecvEntryA hA hM hPl hfm hCs h1 hag hmb' rep P.einfo P.ebuf + P.elr P.eex hcap hr hk hcl hsnd hrcv hmax htu hconv hdsc hnt + exact ⟨s3, σ3, e1, e2, Q, Rl, st', R⟩ + | replyPassive P hcap hr hk hcl hsnd hrcv hdsc hsw hhd hnt => + obtain ⟨s3, σ3, e1, e2, -, -, -, Q, Rl, st', R⟩ := J.replyPassiveRecvEntryA hA hM hPl hfm hCs h1 hag hmb' rep + P.einfo P.ebuf P.elr P.eex hcap hr hk hcl hsnd hrcv hmax htu hconv hdsc hsw hhd hnt + exact ⟨s3, σ3, e1, e2, Q, Rl, st', R⟩ + | failDecode P hcap hf => + obtain ⟨e1, e2⟩ := J.firstFailA rep P hcap hf + refine ⟨s, σ, e1, e2, Z.Q, Z.R, stack, ?_⟩ + cases Z + exact J.toRecvPreA + | lookupFail hl => + obtain ⟨e1, e2⟩ := J.lookupFailA rep hl + refine ⟨s, σ, e1, e2, Z.Q, Z.R, stack, ?_⟩ + cases Z + exact J.toRecvPreA + | extraFail einfo ebuf elr hx => + obtain ⟨e1, e2⟩ := J.extraFailA rep einfo ebuf elr hx + refine ⟨s, σ, e1, e2, Z.Q, Z.R, stack, ?_⟩ + cases Z + exact J.toRecvPreA + +set_option maxHeartbeats 4000000 in +/-- **`dispatch_syscall` of ReplyRecv, NBSendRecv and NBSendWait over the scope** (module docstring). `Sc` selects the +first half at the entry state; `Sc2` selects the receive scope **at the state the first half reached**: it is guarded by +the first half's concrete equations (the FL4 `handle_invocation = ok` and the spec outcome). -/ +theorem JSyscallA.combinedBodyScopeA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hn : fl4A64Platform.numNodes ≤ n) + (hag : fl4MinBudget = MIN_BUDGET) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) (hconv : Fl4TicksToUsLegacyAgree) + (rep : Bool) (pending : SMonad Unit Unit) {call : Syscall} {receiveReply : Bool} + (hc : DispatchCombinedAtA call receiveReply) {cptr : U64} + (hreg : cptr = L.mem s (regWA (L.curW s) + (if call = .sysNBSendRecv then layout.reg.NBSEND_RECV_DEST else layout.reg.REPLY))) + (Sc : FirstScopeA st OS N E Rp s σ cptr) + (Sc2 : ∀ (s3 : S) (σ3 : State Unit), + syscall.handle_invocation st ar s a false false true true cptr = ok (.Ok (), s3, a) → + (.ok (), σ3) ∈ ((fl4HandleInvocationR (Z := Unit) rep false false true true + (Fl4SpecA64.toBl cptr.bv)).run σ).1 → + RecvScopeA st OS N E Rp Z.T s3 σ3 true receiveReply) : + syscall.dispatch_syscall st ar s a (syscallNumberA call) ⦃ r => + StepPostA (fun s' a' τ => A64RelS3 st AR ar hPl.ipa n Z.Cs s' a' τ m ∧ BodyOKA τ m) + (fl4SettleDispatch pending (fl4DispatchSyscall (fl4HandleInvocationR (Z := Unit) rep) call)) σ r ⦄ := by + obtain ⟨s3, σ3, hinv, hspec, Q, Rl, stk, P3⟩ := J.firstScopeA hA hM hPl hfm hCs h1 hn hag hmb hmax htu hconv rep Sc + have Sr := Sc2 s3 σ3 hinv hspec + have htZ : L.curW s ∈ Z.T := J.sr.curT + obtain ⟨x, hk, -⟩ := J.ep.tcb (L.curW s) htZ + have regs := (J.ep.at_tcb htZ hk).regs + have reg : ∀ (r : Fl4SpecA64.Register) (o : U64), Endp.regA (L.curW s) (regIdxA r) = regWA (L.curW s) o → + x.tcbArch.tcbContext.userRegs r = (L.mem s (regWA (L.curW s) o)).bv := fun r o e => by + rw [← e]; exact (regs r).symm + have off : ∀ (r : Fl4SpecA64.Register) (o : U64), (regIdxA r).val = o.val → + Endp.regA (L.curW s) (regIdxA r) = regWA (L.curW s) o := fun r o e => by + unfold Endp.regA regWA + rw [UScalar.eq_of_val_eq e] + have pins : ∀ calling blocking donate first (cp : U64), + DispatchInvokeAtA (L.mem s (regWA (L.curW s) layout.reg.CAP)) (L.mem s (regWA (L.curW s) layout.reg.REPLY)) + (L.mem s (regWA (L.curW s) layout.reg.NBSEND_RECV_DEST)) call calling blocking donate first cp → + calling = false ∧ blocking = false ∧ donate = true ∧ first = true ∧ cp = cptr := by + intro calling blocking donate first cp h + cases hc <;> cases h <;> refine ⟨rfl, rfl, rfl, rfl, ?_⟩ <;> rw [hreg] <;> simp + refine dispatchSyscall_refinesA (st := st) (ar := ar) (a := a) + (InvRel := fun _ s' a' τ => s' = s3 ∧ a' = a ∧ τ = σ3) + call (cur_thread_ok s) ?_ J.j.1.sch.curThread hk + (reg cap_register layout.reg.CAP (off _ _ (by simp [cap_register, capRegister, regIdxA, layout.reg.CAP, layout.reg.X0]))) + (reg replyRegister layout.reg.REPLY (off _ _ (by simp [replyRegister, regIdxA, layout.reg.REPLY, layout.reg.X6]))) + (reg nbsendRecvDest layout.reg.NBSEND_RECV_DEST + (off _ _ (by simp [nbsendRecvDest, regIdxA, layout.reg.NBSEND_RECV_DEST, layout.reg.X8]))) + refine { inv := ?_, done := ?_, preempt := ?_, combined := ?_, recv := ?_, yield := ?_ } + · intro calling blocking donate first cp h + obtain ⟨rfl, rfl, rfl, rfl, rfl⟩ := pins _ _ _ _ _ h + rw [hinv, WP.spec_ok] + exact ⟨.ok (), σ3, hspec, rfl, rfl, rfl, rfl⟩ + · intro calling blocking donate first cp s' a' τ h hf + exact absurd ((pins _ _ _ _ _ h).2.2.2.1.symm.trans hf) (by simp) + · intro calling blocking donate first cp s' a' τ h he + obtain ⟨rfl, rfl, rfl, rfl, rfl⟩ := pins _ _ _ _ _ h + rw [hinv] at he + cases (Prod.mk.inj (okinj he)).1 + · intro cp rr s' a' τ h hr _ _ R + obtain ⟨rfl, rfl, rfl⟩ := R + have er : rr = receiveReply := by cases hc <;> cases hr <;> rfl + subst er + exact recvDispatchA hA hPl P3 hCs h1 hmb hmax htu Sr + · intro b rr h + cases hc <;> cases h + · intro h + subst h + cases hc + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3HPreA.lean b/proofs/Fl4ProofsA64/P3/Stage3HPreA.lean new file mode 100644 index 000000000..07a1b3457 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3HPreA.lean @@ -0,0 +1,131 @@ +/- +P3 (aarch64, A64-P3 Stage-3 integration, hpre): **the callers' `hPre` from `afterPreemptA`**. + +* `hPre_ofA`: the body of every caller's plug-in `hPre` (`sendBodyScopeA`, `cnodeCopyBodyA`, `VsBodyA`, + `sendBodyErrA`, `SendScopeA`) with `pending := pendingIrqA poll preemptServiceA`: `afterPreemptA` at the exit state, + with `T` (`JStage3A.entryValidA`) and `hX` (`BufOffXA.ofBufOff`) derived from the record there, and `clk` the + `Clock` the delete/revoke loop carries to its preempted exit (`DispPostRA`'s `r = Preempted → Clock …`). +* `HPreA`: the plug-in's statement; `HPreEnvA`: the premises that discharge it, lifted once to the top: the IRQ set + (`IrqArchA`, `kernelConfig.maxIRQ`, the poll and its `pending`, the accepted line [J]), the platform / budget + agreements, and the timeout send without a waiting receiver `SendPA` [J] (`hSend`); `HPreA.ofEnv`. + +Named premises: those of `afterPreemptA` (module docstring of `P3/Stage3AfterPreemptA`). +-/ +import Fl4ProofsA64.P3.Stage3AfterPreemptA +import Fl4ProofsA64.P3.Stage3KEntryFaultA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4SpecA64 Fl4Spec Classical + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + +variable (st) in +/-- **`hX` from the record**: the tracked buffer words are in every `Ub` bounding the live frames (`BufOffA.ubA`) and +the J7 `Pt`'s words are off `Ub` (`VrOK.ub`). -/ +theorem BufOffXA.ofBufOff {Z : XSetsA} {H : Heap} {OS Pp Tb : Set U64} {Lv : U64 → Nat} + {Bf : Set (U64 × Fl4SpecA64.Word)} {s : S} (B : BufOffA st Z OS s Bf) : BufOffXA st Z H OS Pp Tb Lv Bf s := + fun _ _ fu vr b hv hb => vr.ub b hv (B.ubA fu hb) + +variable {AR : A → HwRootA → Prop} {nA : U64} {n : Nat} {Cs : Set U64} {s : S} {a : A} {m : McState} + +/-- **The callers' `hPre` from `afterPreemptA`** (module docstring). -/ +theorem hPre_ofA {Clock : A → MachineState → Prop} {poll : SMonad Unit (Option Word)} {Rch : S → A → Prop} + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) (hnA : nA = hPl.ipa) (K : IrqArchA ar AR (fun _ _ => True)) + (hMax : kernelConfig.maxIRQ = 5375) (hpoll : PollAgreeA ar AR poll) (hCs : ∀ c ∈ Cs, c.val < fl4A64Platform.numNodes) + (one : fl4A64Platform.numNodes = 1) (hnd : numDomains = 1) (hn : n = fl4A64Platform.numNodes) + (hag : fl4MinBudget = MIN_BUDGET) (hmb : ∀ a', Fl4MinBudgetAgreeA ar a') + (hLimit : ∀ a', sporadic.max_release_time ar a' ⦃ v => v.bv = fl4MaxReleaseTime ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) (hconv : Fl4TicksToUsLegacyAgree) + (hSend : ∀ (s' : S) (a' : A) (τ : State Unit), Rch s' a' → A64RelPS3 st AR ar nA n Cs s' a' τ m → PreExitA τ → + Clock a' τ.machineState → + ∀ (Z : XSetsA) (H : Heap) (OS U Ac Pp Tb : Set U64) (Lv : U64 → Nat) (ia ie : Nat) (N E Rp : Set U64) + (Bf : Set (U64 × Fl4SpecA64.Word)) (stack : U64 → List U64) (κ : U64 → U64), Z.Cs = Cs → + SendPA st ar AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ nA m Clock s' a' τ) + (line : ∀ (s' : S) (a' : A) (r : S × A) (τ : State Unit) (irq : U64) (a'' : A) (τ' : State Unit), + syscall.mcs_preemption_point st ar s' a' = ok r → A64RelS3 st AR ar nA n Cs r.1 r.2 τ m → + ar.take_pending_irq r.2 = ok (irq, a'') → irq ≠ syscall.NO_IRQ → (some irq.bv, τ') ∈ (poll τ).1 → + IrqLineBaseA st r.1 irq) : + ∀ (s' : S) (a' : A) (τ : State Unit), Rch s' a' → A64RelPS3 st AR ar nA n Cs s' a' τ m → PreExitA τ → + Clock a' τ.machineState → + syscall.after_invocation st ar s' a' (.Err .Preempted) ⦃ r => + r.1 = false ∧ StepPostA (fun s'' a'' υ => A64RelS3 st AR ar nA n Cs s'' a'' υ m) + (fl4PreemptionPath (pendingIrqA poll preemptServiceA)) τ r.2 ⦄ := by + intro s' a' τ hr hRel hPre clk + subst hn + exact afterPreemptA hA hPl hnA K hMax hpoll hCs one hnd rfl hag hmb hLimit hmax htu hconv clk + (fun _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ J => BufOffXA.ofBufOff st J.bufOff) (hSend s' a' τ hr hRel hPre clk) hRel hPre (line s' a') + +variable (st) in +/-- **The callers' plug-in `hPre`** (the statement of `sendBodyScopeA`'s / `cnodeCopyBodyA`'s / … premise). -/ +def HPreA (AR : A → HwRootA → Prop) (ar : machine.Arch A) (hPl : ArchPlat ar) (n : Nat) (Cs : Set U64) (m : McState) (Clock : A → MachineState → Prop) + (pending : SMonad Unit Unit) (s : S) (a : A) (call : Fl4SpecA64.Syscall) : Prop := + ∀ calling blocking donate s' a' τ, + DispatchInvokeAtA (L.mem s (regWA (L.curW s) layout.reg.CAP)) (L.mem s (regWA (L.curW s) layout.reg.REPLY)) + (L.mem s (regWA (L.curW s) layout.reg.NBSEND_RECV_DEST)) call calling blocking donate false + (L.mem s (regWA (L.curW s) layout.reg.CAP)) → + syscall.handle_invocation st ar s a calling blocking donate false (L.mem s (regWA (L.curW s) layout.reg.CAP)) = + ok (.Err error.Exc.Preempted, s', a') → + A64RelPS3 st AR ar hPl.ipa n Cs s' a' τ m ∧ PreExitA τ ∧ Clock a' τ.machineState → + syscall.after_invocation st ar s' a' (.Err error.Exc.Preempted) ⦃ r => + r.1 = false ∧ StepPostA (fun s a τ => A64RelS3 st AR ar hPl.ipa n Cs s a τ m) (fl4PreemptionPath pending) τ r.2 ⦄ + +variable (st) in +/-- [P] **The platform facts the preempted exit shares with the entry** (the entry's `FaultEnvA` and the top's `hag`, `hconv`, +`hnd`), so `HPreEnvA` does not copy them. -/ +structure ExitPlatA (ar : machine.Arch A) (Z : XSetsA) : Prop where + env : FaultEnvA st ar Z + hag : fl4MinBudget = MIN_BUDGET + hconv : Fl4TicksToUsLegacyAgree + hnd : numDomains = 1 + +variable (st) in +/-- **The premises of `HPreA`, lifted once to the top** (module docstring), without the platform facts of `ExitPlatA`. -/ +structure HPreEnvA (AR : A → HwRootA → Prop) (ar : machine.Arch A) (hPl : ArchPlat ar) (n : Nat) (Cs : Set U64) (m : McState) (Clock : A → MachineState → Prop) + (pending : SMonad Unit Unit) (s : S) (a : A) (call : Fl4SpecA64.Syscall) : Prop where + K : IrqArchA ar AR (fun _ _ => True) + hMax : kernelConfig.maxIRQ = 5375 + hn : n = fl4A64Platform.numNodes + hLimit : ∀ a', sporadic.max_release_time ar a' ⦃ v => v.bv = fl4MaxReleaseTime ⦄ + /-- [J] the timeout send without a waiting receiver, at the preempted state a run of this call reaches. -/ + hSend : ∀ calling blocking donate s' a' (τ : State Unit), + DispatchInvokeAtA (L.mem s (regWA (L.curW s) layout.reg.CAP)) (L.mem s (regWA (L.curW s) layout.reg.REPLY)) + (L.mem s (regWA (L.curW s) layout.reg.NBSEND_RECV_DEST)) call calling blocking donate false + (L.mem s (regWA (L.curW s) layout.reg.CAP)) → + syscall.handle_invocation st ar s a calling blocking donate false (L.mem s (regWA (L.curW s) layout.reg.CAP)) = + ok (.Err error.Exc.Preempted, s', a') → + A64RelPS3 st AR ar hPl.ipa n Cs s' a' τ m → PreExitA τ → Clock a' τ.machineState → + ∀ (Z : XSetsA) (H : Heap) (OS U Ac Pp Tb : Set U64) (Lv : U64 → Nat) (ia ie : Nat) (N E Rp : Set U64) + (Bf : Set (U64 × Fl4SpecA64.Word)) (stack : U64 → List U64) (κ : U64 → U64), Z.Cs = Cs → + SendPA st ar AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ hPl.ipa m Clock s' a' τ + /-- [P] the poll, its pending interrupt, and [J] the accepted line. -/ + poll : ∃ poll : SMonad Unit (Option Word), PollAgreeA ar AR poll ∧ pendingIrqA poll preemptServiceA = pending ∧ + ∀ (s' : S) (a' : A) (r : S × A) (τ : State Unit) (irq : U64) (a'' : A) (τ' : State Unit), + syscall.mcs_preemption_point st ar s' a' = ok r → A64RelS3 st AR ar hPl.ipa n Cs r.1 r.2 τ m → + ar.take_pending_irq r.2 = ok (irq, a'') → irq ≠ syscall.NO_IRQ → (some irq.bv, τ') ∈ (poll τ).1 → + IrqLineBaseA st r.1 irq + +/-- **`HPreA` from its premises** (module docstring). -/ +theorem HPreA.ofEnv {Z : XSetsA} {Clock : A → MachineState → Prop} {pending : SMonad Unit Unit} {call : Fl4SpecA64.Syscall} + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) (XP : ExitPlatA st ar Z) + (E : HPreEnvA st AR ar hPl n Z.Cs m Clock pending s a call) : + HPreA st AR ar hPl n Z.Cs m Clock pending s a call := by + intro calling blocking donate s' a' τ hd hh h + obtain ⟨poll, hpoll, hpend, line⟩ := E.poll + subst hpend + exact hPre_ofA (Rch := fun s2 a2 => syscall.handle_invocation st ar s a calling blocking donate false + (L.mem s (regWA (L.curW s) layout.reg.CAP)) = ok (.Err error.Exc.Preempted, s2, a2)) hA hPl rfl E.K E.hMax hpoll XP.env.hCs XP.env.h1 XP.hnd E.hn XP.hag + (fun a' => WP.spec_mono (XP.env.hmb a') fun mb h => h.trans XP.hag.symm) E.hLimit XP.env.hmax XP.env.htu XP.hconv + (fun s2 a2 τ2 hr2 hR hP clk => E.hSend calling blocking donate s2 a2 τ2 hd hr2 hR hP clk) line s' a' τ hh h.1 h.2.1 h.2.2 + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3IdleSchedA.lean b/proofs/Fl4ProofsA64/P3/Stage3IdleSchedA.lean new file mode 100644 index 000000000..31be16756 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3IdleSchedA.lean @@ -0,0 +1,227 @@ +/- +P3 (aarch64, A64-P3 Stage 3, record edit #8b-A): **the schedule tail keeps `idle_sc_ptr` active and ready** +(`SIdleA.f7` across `schedule`). `awaken`, `checkDomainTime`, the thread selection and the timer keep the heap; +the SC switch's refill check is `RefK`; `commit_time` skips `idle_sc_ptr`. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.DelSinvLoopA +import Fl4ProofsA64.P3.Stage3SchedExitFrA +import Fl4ProofsA64.P3.Stage3SchedBindA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec Classical Fl4ProofsA64.P3 + +noncomputable section + +/-- Current SC, clock and the `idle_sc_ptr` object kept. -/ +def IdleObjK (σ σ' : State Unit) : Prop := + σ'.curSc = σ.curSc ∧ σ'.curTime = σ.curTime ∧ σ'.kheap idleScPtr = σ.kheap idleScPtr + +instance : PreO IdleObjK := + ⟨fun _ => ⟨rfl, rfl, rfl⟩, fun a b => ⟨b.1.trans a.1, b.2.1.trans a.2.1, b.2.2.trans a.2.2⟩⟩ + +theorem updateSchedContext_idleObjA (p : ObjRef) (f : SchedContext → SchedContext) (hp : p ≠ idleScPtr) : + PresR IdleObjK (updateSchedContext (Z := Unit) p f) := by + intro σ r hr + obtain ⟨y, n, -, e⟩ := updateSchedContext_memA p f hr + rw [e] + exact ⟨rfl, rfl, Function.update_of_ne (Ne.symm hp) _ _⟩ + +macro "idlek" hp:ident : tactic => `(tactic| + repeat' (first + | exact PresR.pure _ + | exact PresR.gets _ + | exact PresR.getsThe _ + | exact PresR.assertOpt _ + | exact PresR.assert _ + | exact PresR.fail + | exact updateSchedContext_idleObjA _ _ $hp + | refine PresR.bind_pure _ _ ?_ + | refine PresR.bind ?_ (fun _ => ?_) + | refine PresR.when ?_ + | refine PresR.whileLoop (fun _ => ?_) _ + | unfold updateRefillHd + | unfold updateRefillTl + | unfold refillAddTail + | unfold refillPopHead + | unfold getRefills + | unfold getRefillHead + | unfold getRefillTail + | unfold refillSingle + | unfold refillFull + | unfold isRoundRobin + | unfold getSchedContext + | unfold getObject + | unfold scheduleUsed + | unfold handleOverrunWith + | unfold chargeEntireHeadRefill + | unfold headInsufficientLoopWith + | unfold mergeNonoverlappingHeadRefill + | dsimp only + | split)) + +/-- The periodic charge of a current SC other than `idle_sc_ptr`. -/ +theorem refillBudgetCheck_idleObjA (u : Ticks) {σ : State Unit} (h : σ.curSc ≠ idleScPtr) + {r : Unit × State Unit} (hr : r ∈ (fl4RefillBudgetCheck (Z := Unit) u σ).1) : IdleObjK σ r.2 := by + unfold fl4RefillBudgetCheck refillBudgetCheckWith at hr + obtain ⟨c, hc, hr⟩ := eqPres_mem (PresR.gets _) hr + have ec : c = σ.curSc := congrArg Prod.fst (show (c, σ) = (σ.curSc, σ) from hc) + have hp : c ≠ idleScPtr := ec ▸ h + refine (?_ : PresR IdleObjK _) σ r hr + idlek hp + +theorem refillRoundRobin_idleObjA (u : Ticks) {σ : State Unit} (h : σ.curSc ≠ idleScPtr) + {r : Unit × State Unit} (hr : r ∈ (refillBudgetCheckRoundRobin (Z := Unit) u σ).1) : IdleObjK σ r.2 := by + unfold refillBudgetCheckRoundRobin at hr + obtain ⟨c, hc, hr⟩ := eqPres_mem (PresR.gets _) hr + have ec : c = σ.curSc := congrArg Prod.fst (show (c, σ) = (σ.curSc, σ) from hc) + have hp : c ≠ idleScPtr := ec ▸ h + refine (?_ : PresR IdleObjK _) σ r hr + idlek hp + +/-- What the schedule tail keeps of `idle_sc_ptr` (record edit #8b-C2, item 11: `rdy` is `RdyA`, the idle and the +current SC's ready-head facts). -/ +structure IdleScK (σ σ' : State Unit) : Prop where + time : σ'.curTime = σ.curTime + rm : ∀ y' n, σ'.kheap idleScPtr = some (.schedContext y' n) → + ∃ y, σ.kheap idleScPtr = some (.schedContext y n) ∧ y'.scRefillMax = y.scRefillMax + rdy : RdyA σ → RdyA σ' + /-- (item 11) `cur_sc_in_release_q_imp_zero_consumed` is kept. -/ + zc : ZeroConsA σ → ZeroConsA σ' + +instance : PreO IdleScK := + ⟨fun _ => ⟨rfl, fun y _ h => ⟨y, h, rfl⟩, id, id⟩, fun a b => ⟨b.time.trans a.time, fun y'' n h => by + obtain ⟨y', h', e'⟩ := b.rm y'' n h + obtain ⟨y, h0, e⟩ := a.rm y' n h' + exact ⟨y, h0, e'.trans e⟩, fun g => b.rdy (a.rdy g), fun g => b.zc (a.zc g)⟩⟩ + +/-- A step keeping the heap, the clock, the current SC and the consumed time. -/ +theorem IdleScK.ofObj {σ σ' : State Unit} (ht : σ'.curTime = σ.curTime) (hk : σ'.kheap = σ.kheap) + (hs : σ'.curSc = σ.curSc) (hc : σ'.consumedTime = σ.consumedTime) + (hr : ∀ t ∈ σ'.releaseQueue, t ∈ σ.releaseQueue) : IdleScK σ σ' := + ⟨ht, fun y _ h => ⟨y, by rw [← hk]; exact h, rfl⟩, RdyA.ofFrameA hk ht hc hs, fun g => + ZeroConsA.back g hr hc hs fun t x' hx => ⟨x', by rw [← hk]; exact hx, rfl⟩⟩ + +/-- A step zeroing the consumed time and keeping the clock and the `idle_sc_ptr` object. -/ +theorem IdleScK.ofIdleObj {σ σ' : State Unit} (ht : σ'.curTime = σ.curTime) + (e : σ'.kheap idleScPtr = σ.kheap idleScPtr) (hc : σ'.consumedTime = 0) : IdleScK σ σ' := + ⟨ht, fun y _ h => ⟨y, by rw [← e]; exact h, rfl⟩, fun g => + ⟨fun y n h => by rw [ht]; exact g.1 y n (by rw [← e]; exact h), fun hne => absurd hc hne⟩, + fun _ _ _ _ _ _ => hc⟩ + +/-- A step after `K` that keeps the heap and the clock and zeroes the consumed time. -/ +theorem IdleScK.zeroCons {σ σ1 σ2 : State Unit} (K : IdleScK σ σ1) (hk : σ2.kheap = σ1.kheap) + (ht : σ2.curTime = σ1.curTime) (hc : σ2.consumedTime = 0) : IdleScK σ σ2 := + PreO.trans K (IdleScK.ofIdleObj ht (by rw [hk]) hc) + +theorem IdleScK.ofSf {α : Type} {m : SMonad Unit α} (h : PresR SfKeepA m) : PresR IdleScK m := + fun s x hx => IdleScK.ofObj (h s x hx).time (h s x hx).kh (h s x hx).csc (h s x hx).used (h s x hx).rls + +theorem IdleScK.ofRefK {σ σ' : State Unit} (h : RefK σ σ') : IdleScK σ σ' := by + refine ⟨by rw [h.1], fun y' n e => ?_, h.2.2, fun g => ZeroConsA.back g (fun t ht => by rw [h.1] at ht; exact ht) + (by rw [h.1]) (by rw [h.1]) fun t x' hx => ?_⟩ + · obtain ⟨x, hx, g⟩ := (h.2.1 idleScPtr).scB e + exact ⟨x, hx, g.2.2.1⟩ + · obtain ⟨x, hx0, g⟩ := (h.2.1 t).tcbB hx + exact ⟨x, hx0, g.2.1.symm⟩ + +/-- `commit_time` keeps `idle_sc_ptr` and ends with no time consumed. -/ +theorem fl4CommitTime_idleConsA {σ : State Unit} {r : Unit × State Unit} + (hr : r ∈ (fl4CommitTime (Z := Unit) σ).1) : IdleScK σ r.2 ∧ r.2.consumedTime = 0 := by + unfold fl4CommitTime at hr + obtain ⟨c, hc, hr⟩ := eqPres_mem (PresR.gets _) hr + have ec : c = σ.curSc := congrArg Prod.fst (show (c, σ) = (σ.curSc, σ) from hc) + obtain ⟨act, -, hr⟩ := eqPres_mem (by unfold getScActive; eqpres) hr + obtain ⟨⟨u, σ1⟩, h1, h2⟩ := mem_bindA.1 hr + have e : r = ((), { σ1 with consumedTime := 0 }) := h2 + have K : IdleObjK σ σ1 := by + unfold NonDet.when at h1 + split_ifs at h1 with g + swap + · have e : σ1 = σ := (Prod.mk.inj (show (u, σ1) = ((), σ) from h1)).2 + rw [e]; exact PreO.refl σ + have hp : c ≠ idleScPtr := g.2 + have hs : σ.curSc ≠ idleScPtr := ec ▸ hp + obtain ⟨used, -, h1⟩ := eqPres_mem (PresR.gets _) h1 + obtain ⟨⟨v, σ2⟩, h3, h4⟩ := mem_bindA.1 h1 + have K2 : IdleObjK σ2 σ1 := updateSchedContext_idleObjA c _ hp σ2 _ h4 + have K1 : IdleObjK σ σ2 := by + split_ifs at h3 + swap + · have e : σ2 = σ := (Prod.mk.inj (show (v, σ2) = ((), σ) from h3)).2 + rw [e]; exact PreO.refl σ + obtain ⟨robin, -, h3⟩ := eqPres_mem (by unfold isRoundRobin getSchedContext getObject; eqpres) h3 + split at h3 + · exact refillRoundRobin_idleObjA _ hs h3 + · exact refillBudgetCheck_idleObjA _ hs h3 + exact PreO.trans K1 K2 + rw [e] + exact ⟨IdleScK.ofIdleObj K.2.1 K.2.2 rfl, rfl⟩ + +theorem fl4CommitTime_idleScA : PresR IdleScK (fl4CommitTime (Z := Unit)) := fun _ _ hr => + (fl4CommitTime_idleConsA hr).1 + +theorem fl4SwitchSchedContext_idleScA : PresR IdleScK (fl4SwitchSchedContext (Z := Unit)) := by + intro σ r hr + unfold fl4SwitchSchedContext at hr + obtain ⟨s0, h0, hr⟩ := eqPres_mem (PresR.get : PresR (@Eq (State Unit)) NonDet.get) hr + have es : s0 = σ := congrArg Prod.fst (show (s0, σ) = (σ, σ) from h0) + subst es + obtain ⟨target, -, hr⟩ := eqPres_mem (by unfold getTcbObjRef threadGet; eqpres) hr + obtain ⟨⟨u1, σ1⟩, h1, hr⟩ := mem_bindA.1 hr + -- the cond block: nothing, or the timer flag set and the refill check (which keeps the flag) + have A : (target = s0.curSc ∧ σ1 = s0) ∨ + (target ≠ s0.curSc ∧ IdleScK s0 σ1 ∧ σ1.reprogramTimer = true) := by + unfold NonDet.when at h1 + split_ifs at h1 with g + · right + refine ⟨g, ?_⟩ + obtain ⟨⟨u2, σm⟩, hm, h2⟩ := mem_bindA.1 h1 + have em : σm = { s0 with reprogramTimer := true } := (Prod.mk.inj (show (u2, σm) = _ from hm)).2 + have R1 : RefK σm σ1 := ifCondRefillUnblockCheck_refK _ _ _ σm _ h2 + have T1 : TmKeepA σm σ1 := + (inferInstance : TmInst (ifCondRefillUnblockCheck (Z := Unit) (some target) none none)).pres σm _ h2 + refine ⟨?_, T1.rep (by rw [em])⟩ + have K0 : IdleScK s0 σm := by rw [em]; exact IdleScK.ofObj rfl rfl rfl rfl (fun _ h => h) + exact PreO.trans K0 (IdleScK.ofRefK R1) + · left + exact ⟨not_not.1 g, (Prod.mk.inj (show (u1, σ1) = ((), s0) from h1)).2⟩ + obtain ⟨rp, hrp, hr⟩ := eqPres_mem (PresR.gets _) hr + have erp : rp = σ1.reprogramTimer := congrArg Prod.fst (show (rp, σ1) = (σ1.reprogramTimer, σ1) from hrp) + obtain ⟨⟨u3, σ2⟩, h2, h3⟩ := mem_bindA.1 hr + have e : r = ((), { σ2 with curSc := target }) := h3 + have K1 : IdleScK s0 σ1 := by + rcases A with ⟨-, e1⟩ | ⟨-, K, -⟩ + · rw [e1]; exact PreO.refl s0 + · exact K + unfold NonDet.when at h2 + split_ifs at h2 with g + · obtain ⟨K2, c0⟩ := fl4CommitTime_idleConsA h2 + rw [e] + exact IdleScK.zeroCons (PreO.trans K1 K2) rfl rfl c0 + · have e2 : σ2 = σ1 := (Prod.mk.inj (show (u3, σ2) = ((), σ1) from h2)).2 + rcases A with ⟨ht, e1⟩ | ⟨-, -, hrep⟩ + · rw [e, e2, e1] + exact IdleScK.ofObj rfl rfl ht rfl (fun _ h => h) + · exact absurd (erp.trans hrep) g + +/-- **The schedule tail keeps `idle_sc_ptr` active and ready.** -/ +theorem schedTail_idleScA (limit : Nat) (timer : State Unit → Fl4TimerView) {σ1 τ : State Unit} + (h : ((), τ) ∈ (SchedTail limit timer σ1).1) : IdleScK σ1 τ := by + have P : PresR IdleScK (SchedTail limit timer) := by + unfold SchedTail + refine PresR.bind (IdleScK.ofSf (inferInstance : SfKInst (awaken (Z := Unit))).pres) (fun _ => + PresR.bind (IdleScK.ofSf (inferInstance : SfKInst (checkDomainTime (Z := Unit))).pres) (fun _ => + PresR.bind (IdleScK.ofSf (fl4SelectThread_qkA limit)) (fun _ => ?_))) + unfold fl4ScAndTimer + refine PresR.bind fl4SwitchSchedContext_idleScA (fun _ => + PresR.bind (IdleScK.ofSf (inferInstance : SfKInst (NonDet.gets _)).pres) (fun _ => PresR.when ?_)) + exact PresR.bind (IdleScK.ofSf (inferInstance : SfKInst (fl4SetNextInterrupt (Z := Unit) timer)).pres) + (fun _ => PresR.modify _ (fun s => IdleScK.ofObj (σ := s) rfl rfl rfl rfl (fun _ h => h))) + exact P σ1 _ h + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3InsP1A.lean b/proofs/Fl4ProofsA64/P3/Stage3InsP1A.lean new file mode 100644 index 000000000..08ab4f3e3 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3InsP1A.lean @@ -0,0 +1,146 @@ +/- +P3 (aarch64, A64-P3 Lift 3b labels): **the P1 facts across a `cdt::insert` from a record state**. + +The Stage-3 step lemmas that follow a cap write (`setMcp_stepA`, `prio*_stepA`, `p1StepA`) take the P1 facts of their +pre-state. At the final decode state they come from `JSyscallA.decodeFinA`; a `cdt::insert` that is followed by further +steps (SetSchedParams: the fault-handler install, then `set_mcp`, `set_priority`) reaches an intermediate state whose +facts the record and the decode site do not give. `JStage3A.p1StepA` handles a scheduler-word write +(`OSKeep`); this module is its twin for the cap write of `cdt::insert`. + +* **`cdt.insert.p1A`** (P1): `cdt::insert nc x y` of a cap `nc` (the `derive_cap` of the source's) keeps + `SysL ∧ SymL`, `BlkOK`, and gives P1's `XC` (the cap carry), `XV` (the VSpace words and the frame carry) and + `XferSlots` (one source→destination transfer): the pieces of `XJX.insQ` without the IPC package. +* **`JStage3A.insertP1A`**: from `J` at `s`, for every state `s2` that `cdt::insert nc x y` reaches: `SysL`, `SymL`, + `UserOK`, `BXI` and `J7A` at any spec state (`ArmQIAt.ofXC`, `S1aStepFix.ofXfer2` with `XferSlots2` the one + transfer, `J7A.of_stepFixedA`; as `J7A.of_rpPostA`, but the `OS` slots change). + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3P1StepA +import Fl4ProofsA64.P3.Stage3CapInsertTA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {H : Heap} {Cs OS Vc Ub Pt Ac : Set U64} {ia ie : Nat} + +set_option maxHeartbeats 4000000 in +/-- **`cdt::insert` of a derived cap: `SysL`, `SymL`, `BlkOK` and the carries** (module docstring). -/ +theorem cdt.insert.p1A (hO : OSOK H.tcbs H.omega H.frame OS) {s : S} + (hb : BXI st H.tcbs Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s) + (hH : HeapOK H) (B : SysL st H Cs OS ia ie s) (Y : SymL st H Cs s) (V : VcCap st Vc OS s) + (hvr : VrOK H.omega H.frame H.scs H.scBits OS Vc Ub (L.ksb s) Pt) + {x y : U64} {nc : capability.Cap} (hx : x ∈ OS) (hy : y ∈ OS) (hxy : x ≠ y) (hyc : obj.sext48 y = ok y) + (hy0 : capA st s y = .Null) (hyB : y ∉ BufS H.tcbs ∨ (BufCap nc ∧ CnOP H nc)) (hyE : y ∉ EptS H.tcbs) + (hcd : ∃ z ∈ OS, CovZ (capA st s z) y) (hw : CapRt.EncWf nc) (hnc : nc ≠ .Null) + (C : InsC nc (capA st s x)) (hE : ApE nc (capA st s x)) + (hdv : objects.derive_cap st s x (capA st s x) = ok (.Ok nc)) : + cdt.insert st s nc x y ⦃ s2 => (SysL st H Cs OS ia ie s2 ∧ SymL st H Cs s2) ∧ + BlkOK st H.tcbs Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s2 ∧ + XC st OS Vc s s2 ∧ XV st H OS Vc s s2 ∧ XferSlots st OS s s2 ⦄ := by + have hm := B.sys.stz.1.1.mdbS + have nn := (hm.lnk hO hx).2 + have R : cdt.insert st s nc x y ⦃ s2 => (SysL st H Cs OS ia ie s2 ∧ SymL st H Cs s2) ∧ + BlkOK st H.tcbs Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s2 ∧ + XC st OS Vc s s2 ∧ XV st H OS Vc s s2 ⦄ := by + unfold cdt.insert + rw [capA_eq, bind_ok] + refine WP.spec_mono (WP.spec_and (cdt.insert_with_cap.sysLVB hH hO hb B Y V hx hy hxy hyc rfl hy0 hyB hcd hw hnc C hyE) + (cdt.insert_with_cap.lrF hO.inOS (LR.self s) hx hy hxy hyc rfl C.nn hw nn)) ?_ + rintro s2 ⟨⟨hS, hY, -⟩, w, -, ed, -, -, ek, es⟩ + have w' : WritesIn st s s2 (W7.CapW H.tcbs {b | InOS OS b} ∅) := w.mono fun b hc => by + rcases hc with ⟨t, ht, -⟩ | hh + · exact absurd ht (Set.notMem_empty t) + · exact Or.inr hh + have wA : WritesIn st s s2 (fun b => Aligned b ∧ W7.CapW H.tcbs {b | InOS OS b} ∅ b) := writesIn_alAG w' + have hB : ∀ b, (Aligned b ∧ W7.CapW H.tcbs {b | InOS OS b} ∅ b) → W7.CapW H.tcbs H.omega H.frame b := + fun b hb' => os_capw hO hb'.1 hb'.2 + have blk2 := (W7.blk_of_capw (BXI.blk hb) wA hB).1 + have xv' : XV st H OS Vc s s2 := by + refine ⟨ArmVsK.ofW w fun _ _ hvr' _ b hw' hv => ?_, fun Ub' hf z hz => ?_⟩ + · rcases hw' with ⟨t, ht, -⟩ | ⟨hin, -⟩ + · exact absurd ht (Set.notMem_empty t) + · exact hvr'.os b hv hin + · by_cases e1 : z = y + · rw [e1, ed]; exact FrUbP.al (Or.inl C.al) C.fe (hf x hx) + by_cases e2 : z = x + · subst e2 + rcases es with e | ⟨_, _, _, _, e⟩ + · rw [e]; exact hf z hz + · rw [e]; exact fun _ _ _ _ _ _ _ h => by cases h + · rw [ek z hz e2 e1]; exact hf z hz + refine ⟨⟨hS, hY⟩, blk2, ⟨ApMap.ins hx hy ed es ek (ApI.of C.fe hE) hy0, ⟨fun V' z hz => ?_, fun z hz => ?_⟩, + tabKZ_cw hvr rfl w⟩, xv'⟩ + rotate_left + · by_cases e1 : z = y + · rw [e1, ed]; exact CapSrc.al (Or.inl C.al) C.fe (CapSrc.of_slot hx rfl) + by_cases e2 : z = x + · subst e2 + rcases es with e | ⟨p, b, j, dd, e⟩ + · rw [e]; exact CapSrc.of_slot hz rfl + · rw [e]; exact CapSrc.ut s p b j dd + · rw [ek z hz e2 e1]; exact CapSrc.of_slot hz rfl + by_cases e1 : z = y + · rw [e1, ed]; exact VcCapP.al (Or.inl C.al) C.fe (V' x hx) + by_cases e2 : z = x + · subst e2 + rcases es with e | ⟨p, b, j, dd, e⟩ + · rw [e]; exact V' z hz + · rw [e]; exact VcCapP.ut Vc p b j dd + · rw [ek z hz e2 e1]; exact V' z hz + refine WP.spec_mono (WP.spec_and R (cdt.insert.slots hO hm hx hy hxy hyc C.nn hw)) ?_ + rintro s2 ⟨hR, new, turned, others⟩ + exact ⟨hR.1, hR.2.1, hR.2.2.1, hR.2.2.2, + Or.inr ⟨x, hx, y, hy, hxy, hy0, ⟨s, nc, hdv, new⟩, turned, others⟩⟩ + +end + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {s : S} {a : A} {σ : State Unit} {m : McState} + +set_option maxHeartbeats 8000000 in +/-- **The P1 facts at the state a `cdt::insert` reaches** (module docstring). -/ +theorem JStage3A.insertP1A (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + {x y : U64} {nc : capability.Cap} (hx : x ∈ OS) (hy : y ∈ OS) (hxy : x ≠ y) (hyc : obj.sext48 y = ok y) + (hy0 : capA st s y = .Null) (hyB : y ∉ BufS H.tcbs ∨ (BufCap nc ∧ CnOP H nc)) (hyE : y ∉ EptS H.tcbs) + (hcd : ∃ z ∈ OS, CovZ (capA st s z) y) (hw : CapRt.EncWf nc) (hnc : nc ≠ .Null) + (C : InsC nc (capA st s x)) (hE : ApE nc (capA st s x)) + (hdv : objects.derive_cap st s x (capA st s x) = ok (.Ok nc)) (G' : MdbGhostA) : + cdt.insert st s nc x y ⦃ s2 => SysL st H Z.Cs OS ia ie s2 ∧ SymL st H Z.Cs s2 ∧ + Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s2 ∧ + BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s2 ∧ + ∀ σ' m', J7A st (J7R2A st ar hPl.ipa) { Z with G := G' } H OS s2 σ' m' ⦄ := by + obtain ⟨Pt, Ub, Pp7, Tb7, Hr, Te, Kt, Lv7, Re, bits, KLv, W⟩ := J7A.unp J.j7 + have I := J.delIn hA hPl W + refine WP.spec_mono (cdt.insert.p1A (Ub := Ub) (Pt := Pt) J.sys.ent.os J.sys.ent.bxi J.sys.ent.heap J.sys.sysl J.symL + W.vcc W.vr hx hy hxy hyc hy0 hyB hyE hcd hw hnc C hE hdv) ?_ + rintro s2 ⟨⟨sysl2, symL2⟩, blk2, xc, xv, xs⟩ + have kb : L.ksb s2 = L.ksb s := xv.1.1 + have A2 := ArmQIAt.ofXC (ia := ia) (ie := ie) ⟨sysl2, symL2⟩ ⟨Ac, blk2⟩ xc xv.1.toVs + have Fx := S1aStepFix.ofXfer2 ⟨A2.1, A2.2, xv.1, s2, xs, xv, XferSlots.refl s2, XV.refl s2⟩ W.vr W.fu W.vcc W.col W.df + W.r2 W.sep W.hiK + have pn : PoolNamed st OS s2 := A2.1.2.2.2.2.2.1 W.pn + have hsp : ∀ p, SchedPageA st { Z with G := G' } OS s2 p → p ∈ Pt := by + obtain ⟨_, -, -, -, ⟨_, _, _, _, _, _, _, R'⟩, -⟩ := id Fx + exact schedPage_ptA (Z := { Z with G := G' }) R' W.sp + refine ⟨sysl2, symL2, J.sys.ent.user.ksb kb, BlkOK.bxi blk2, fun σ' m' => ?_⟩ + exact J7A.of_stepFixedA (Z := { Z with G := G' }) Fx W.vr kb W.al W.vu pn hsp + (fun b hb o ho => by rw [kb]; exact W.ks b hb o ho) (J7R2K.of_stepFixedA (σ' := σ') (m := m') Fx W.ktK) + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3InvSpecCnA.lean b/proofs/Fl4ProofsA64/P3/Stage3InvSpecCnA.lean new file mode 100644 index 000000000..521b4ec17 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3InvSpecCnA.lean @@ -0,0 +1,97 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Plan B): **the Cn group's `InvSpecA`**, from the constructors' pins (`DecPinA`) and +the arm's outcome, for a decode that is not preempted (the preempted `cnodeDelRvPre` is `CnScopeA.soundOk`'s second +case, `Stage3InvSpecTopA`). + +Named premises: those of the scope's arms. New module. +-/ +import Fl4ProofsA64.P3.Stage3DispCnA +import Fl4ProofsA64.P3.DelSpecA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {Clock : A → Fl4SpecA64.MachineState → Prop} + +set_option maxHeartbeats 8000000 in +/-- **The Cn group's `InvSpecA`** (module docstring). -/ +theorem CnScopeA.invSpecA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {ar : machine.Arch A} {nA : U64} {s : S} {a : A} {σ : State Unit} {m : McState} + {kb : U64} (hP : PlaceA Z.th kb) + {lbl len slot : U64} {blk call don fp : Bool} {buf : U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} + {Out : State Unit → Prop} + (h : CnScopeA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar nA s a σ m Clock lbl len slot blk call don fp buf + extra Out) + (hst : ∀ x, σ.kheap σ.curThread = some (.tcb x) → + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), stUpdA σ σ.curThread x .restart)}, False)) + {σ' : State Unit} (hOut : Out σ') + {mi : MessageInfo} {sl : CslotPtr} {c : Cap} {excaps : List (Cap × CslotPtr)} {buffer : Option ObjRef} + {args' : List Data} (F : FrontPinA Z.th σ slot mi sl c excaps buffer args') (cptr : CapRef) + (hnp : ∀ s' a', syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don fp buf extra ≠ + ok (.Err .Preempted, s', a')) : + InvSpecA call blk don true mi.miLabel args' cptr sl c excaps buffer σ σ' := by + have flags : ∀ (i : Fl4ArchInvocation), + fl4PerformInvocation (Z := Unit) blk call don (.invokeArchObject i) = fl4ArchPerformInvocation i := fun _ => by + cases blk <;> cases call <;> cases don <;> rfl + have cn : ∀ (i : CnodeInvocation), fl4PerformInvocation (Z := Unit) blk call don (.invokeCNode i) = + (do fl4InvokeCnode i; Fl4Spec.NonDetE.returnOk []) := fun _ => by + cases blk <;> cases call <;> cases don <;> rfl + induction h with + | @cnodeCopy ps pd nc anc slot p r gs g extra lbl length buf index depth x0 si sdp root bB bC bD hnvm ss sd hCs hCd hsn hdn hdc hwf hanc hw hn hD hal hk hsh hsh1 hbd1 hbd hirq hz hut hdo hcv hsci hslot hslc hlb hl4 hl2 hi hd0 hres hx0 h0 h2 h3 hroot hemp hsrc hcop hcov hext hSC hpin => + obtain ⟨x, hx, hperf⟩ := hOut + obtain ⟨σ1, hset⟩ : ∃ σ1, σ1 = stUpdA σ σ.curThread x .restart := ⟨_, rfl⟩ + refine InvSpecA.ofPin hP hpin F cptr (σ1 := σ1) (by rw [hset]; exact hst x hx) ?_ + rw [← hset] at hperf + rw [cn] + refine perform_thenNilA ?_ + exact liftE_memA hperf + | @cnodeMove ps pd nc ac slot p r gs g extra lbl length buf index depth x0 si sdp root bB bC bD hnvm ss sd hCs hCd hsn hdn hdc hca hw hn hD hal hsh hbd hirq hno hok hsci hslot hslc hlb hl4 hl2 hi hd0 hres hx0 h0 h2 h3 hroot hemp hsrc hcop hcov hext hSC hpin => + obtain ⟨x, hx, hperf⟩ := hOut + obtain ⟨σ1, hset⟩ : ∃ σ1, σ1 = stUpdA σ σ.curThread x .restart := ⟨_, rfl⟩ + refine InvSpecA.ofPin hP hpin F cptr (σ1 := σ1) (by rw [hset]; exact hst x hx) ?_ + rw [← hset] at hperf + rw [cn] + refine perform_thenNilA ?_ + exact liftE_memA hperf + | @cnodeRotate ps pp pd np ap ns an slot p r gs g extra lbl length buf index depth x0 x1' pdata sdata bB bC bD hnvm ss sp sd hCs hCp hCd hsp hsd hsn hpn hdn hpc hdc hcp hwp hnp hDp halp hshp hbdp hirqp hop hokp hscp hcs hws hns hDs hals hshs hbds hirqs hos hoks hscs hslot hslc hlb hl8 hl2 hi hd0 hres hx0 h0 hx1 h1 h2 h5 hrot hus hup hcov hext hSC hpin => + obtain ⟨x, σ2, hx, h1, h2⟩ := hOut + obtain ⟨σ1, hset⟩ : ∃ σ1, σ1 = stUpdA σ σ.curThread x .restart := ⟨_, rfl⟩ + refine InvSpecA.ofPin hP hpin F cptr (σ1 := σ1) (by rw [hset]; exact hst x hx) ?_ + rw [← hset] at h1 + rw [cn] + refine perform_thenNilA (liftE_memA ?_) + rw [if_neg (fun e => hsd (congrArg cslotAddrA e))] + exact mem_bindA.2 ⟨_, h1, h2⟩ + | @cnodeRotateSw ps pp np ap ns an slot p r gs g extra lbl length buf index depth x0 x1' pdata sdata bB bC bD hnvm ss sp hCs hCp hsp hsn hpn hsx hpx hcp hwp hnp hDp halp hshp hbdp hirqp hop hokp hscp hcs hws hns hDs hals hshs hbds hirqs hos hoks hscs hslot hslc hlb hl8 hl2 hi hd0 hres hx0 h0 hx1 h1 h2 h5 hrot hus hup hcov hext hSC hpin => + obtain ⟨x, hx, hperf⟩ := hOut + obtain ⟨σ1, hset⟩ : ∃ σ1, σ1 = stUpdA σ σ.curThread x .restart := ⟨_, rfl⟩ + refine InvSpecA.ofPin hP hpin F cptr (σ1 := σ1) (by rw [hset]; exact hst x hx) ?_ + rw [← hset] at hperf + rw [cn] + refine perform_thenNilA ?_ + refine liftE_memA ?_ + rw [if_pos rfl] + exact hperf + | @cnodeDelRv slot p r gs g extra lbl length buf bB bC bD slbl args ac excaps inv rr s3 a3 hCnt P hCl hCs hslot hcov hext hSC hslc hlb hlbl hlen hargs hca hcv hdec hrr hspec hpin => + obtain ⟨σ1, hset, hperf⟩ := hOut + refine InvSpecA.ofPin hP hpin F cptr (σ1 := σ1) hset ?_ + rw [cn] + exact perform_thenNilA hperf + | @cnodeDelRvPre slot p r gs g extra lbl length buf bB bC bD slbl args ac excaps inv rr s3 a3 hCnt P hCl hCs hslot hcov hext hSC hslc hlb hlbl hlen hargs hca hcv hdec hrr hspec hpin => + subst hrr + refine absurd ?_ (hnp s3 a3) + rw [hslc, decodeCnode_programA, if_neg Bool.false_ne_true] + exact hdec + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3InvSpecFrontA.lean b/proofs/Fl4ProofsA64/P3/Stage3InvSpecFrontA.lean new file mode 100644 index 000000000..742d603a6 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3InvSpecFrontA.lean @@ -0,0 +1,71 @@ +/- +P3 (aarch64, A64-P3 Lift 1c): **front-end facts the groups' `InvSpecA` share**: the spec's generic label of an FL4 +label (`genLabel_specA`), and the spec message info and words against FL4's (`ArgsLabA`, +`JSyscallA.argsLabA`). Moved from `P3/Stage3InvSpecL1A` (statements unchanged), so that the groups upstream of the +`InvSpecA` assembly can use them. No premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3DispTopA +import Fl4ProofsA64.P3.Stage3InvSpecIpcA +import Fl4ProofsA64.P3.DomainSetDispatchA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +/-- The spec's generic invocation label of an FL4 label whose spec label is `.genInvocationLabel g`. -/ +theorem genLabel_specA {lbl : U64} {g : GenInvocationLabels} + (e : fl4LabelOf lbl.bv = .spec (.genInvocationLabel g)) : + genInvocationType (specLabelWord (fl4LabelOf lbl.bv)) = g := by + rw [e] + have e' : invocationType (specLabelWord (.spec (.genInvocationLabel g))) = .genInvocationLabel g := + invocationType_fromEnumA _ + unfold genInvocationType + rw [e'] + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-- **The spec message info's label and words against FL4's** (module docstring). -/ +def ArgsLabA (st : machine.Store S) [L : LawfulStore st] (s : S) (info : msginfo.MessageInfo) (buf : U64) + (mi : MessageInfo) (args : List Data) : Prop := + mi.miLabel = info.label.bv ∧ ∃ argsU : List U64, args = argsU.map UScalar.bv ∧ + (msgLenA info buf).val = argsU.length ∧ + ∀ (i : U64) x, argsU[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x + +set_option maxHeartbeats 4000000 in +theorem JSyscallA.argsLabA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} + {κ : U64 → U64} {n : Nat} {J5 J7 : _} {s : S} {a : A} {σ : State Unit} {m : McState} + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {info : msginfo.MessageInfo} {buf : U64} (hinfo : msg.msg_info st s (L.curW s) = ok info) + {buffer : Option ObjRef} (hBV : BufViewA (L.mem s) σ.machineState.underlyingMemory buf buffer) + {mi : MessageInfo} (hmi : getMessageInfo (Z := Unit) σ.curThread σ = ({(mi, σ)}, False)) + {args : List Data} (hmrs : getMrs (Z := Unit) σ.curThread buffer mi σ = ({(args, σ)}, False)) : + ArgsLabA st s info buf mi args := by + have ct : σ.curThread = specPtr (L.curW s) := J.j.1.sch.curThread + have hT : L.curW s ∈ Z.T := J.sr.curT + have hRV : RegViewA (L.mem s) σ (L.curW s) := fun xs h => (J.ep.at_tcb hT h).regView + have hmi' : getMessageInfo (Z := Unit) (specPtr (L.curW s)) σ = ({(mi, σ)}, False) := ct ▸ hmi + have e : (args.map fun w => (⟨w⟩ : U64)).map UScalar.bv = args := by + rw [List.map_map]; exact List.map_id _ + have hmrs' : getMrs (Z := Unit) (specPtr (L.curW s)) buffer mi σ = + ({((args.map fun w => (⟨w⟩ : U64)).map UScalar.bv, σ)}, False) := by + rw [e, ← ct]; exact hmrs + obtain ⟨hlen, hargs, -⟩ := J.j.1.argsJ hRV hinfo hmi' hBV hmrs' + obtain ⟨xs, hks, -, hmw, -⟩ := J.j.1.curViewA hRV + have hlab : mi.miLabel = info.label.bv := by + have e := single_fstA (hmi'.symm.trans (getMessageInfo_eqA hks)) + have hm := msg_info_specA (L := L) s (L.curW s) + rw [hinfo, WP.spec_ok] at hm + rw [e, hm.1, hmw] + exact label_maskA _ + exact ⟨hlab, args.map fun w => (⟨w⟩ : U64), e.symm, by simpa using hlen, hargs⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3InvSpecIpcA.lean b/proofs/Fl4ProofsA64/P3/Stage3InvSpecIpcA.lean new file mode 100644 index 000000000..c82d3fd3d --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3InvSpecIpcA.lean @@ -0,0 +1,353 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Plan B): **the IPC group's spec side** (`InvSpecA`): for each arm of `IpcScopeA`, the +spec decode at the looked-up cap and the perform, from the arm's own outcome. + +The shape every group follows: `.invSpecA` takes the group's scope, FL4's decode at the invoked slot, the +outcome `Out σ'`, and the front-end facts (message info, the looked-up slot and cap, the message words), and gives +`InvSpecA`. `done = false` (preemption) is excluded by the arm's own decode result. + +Named premises: those of `IpcScopeA`'s arms. New module. +-/ +import Fl4ProofsA64.P3.Stage3DispIpcA +import Fl4ProofsA64.P3.IpcInvokeDispatchA +import Fl4ProofsA64.P3.CnodeCancelA +import Fl4ProofsA64.P3.VsTopPtA +import Fl4ProofsA64.P3.FrontEndExtraA +import Fl4ProofsA64.P3.UtDecodeA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-- A detached Reply executes no transfer (FL4's `fl4DoReplyTransfer`). -/ +theorem fl4ReplyTransfer_detached_specA {σ : State Unit} {t r : ObjRef} {grant : Bool} {x : Reply} + (hk : σ.kheap r = some (.reply x)) (h : x.replyTcb = none) : + fl4DoReplyTransfer (Z := Unit) t r grant σ = ({((), σ)}, False) := by + unfold fl4DoReplyTransfer + rw [Sched.nd_bind_oneSc (Endp.getReplyTcb_eq hk), h] + rfl + +theorem single_fstA_pair {σ σ' : State Unit} + (h : (({((), σ)} : Set (Unit × State Unit)), False) = ({((), σ')}, False)) : σ = σ' := + (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (Prod.mk.inj h).1)).2 + +set_option maxHeartbeats 4000000 in +/-- **The IPC group's `InvSpecA`** (module docstring). -/ +theorem IpcScopeA.invSpecA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} + {κ : U64 → U64} {n : Nat} {ar : machine.Arch A} {s : S} {a : A} {σ : State Unit} {m : McState} + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + {info : msginfo.MessageInfo} {slot buf : U64} {blk call don fp : Bool} + {extra : Aeneas.Std.Array obj.Slot 3#usize} {Out : State Unit → Prop} + (h : IpcScopeA st Z OS N E Rp ar s σ m info.label (msgLenA info buf) slot blk call don fp buf extra Out) + (hinfo : msg.msg_info st s (L.curW s) = ok info) + {buffer : Option ObjRef} (hBV : BufViewA (L.mem s) σ.machineState.underlyingMemory buf buffer) + {r : core.result.Result Unit error.Exc} {s' : S} {a' : A} {σ' : State Unit} + (hd : syscall.decode_invocation st ar s a info.label (msgLenA info buf) slot (capA st s slot) blk call don fp buf + extra = ok (r, s', a')) (hOut : Out σ') + {done : Bool} (hdone : (done = true ∧ r = .Ok ()) ∨ (done = false ∧ r = .Err error.Exc.Preempted)) + {mi : MessageInfo} {sl : CslotPtr} {c : Cap} {excaps : List (Cap × CslotPtr)} {args : List Data} + (F : FrontPinA Z.th σ slot mi sl c excaps buffer args) (hc : CapRelA (capA st s slot) c) (cptr : CapRef) : + InvSpecA call blk don done mi.miLabel args cptr sl c excaps buffer σ σ' := by + have hmi := F.hmi + have hsl := F.hslot + have hab := F.habs + have hmrs := F.hargs + have ct : σ.curThread = specPtr (L.curW s) := J.j.1.sch.curThread + have done_of : r = .Ok () → done = true := fun e => by + rcases hdone with ⟨h, -⟩ | ⟨-, h⟩ + · exact h + · rw [e] at h; cases h + have sigTail : ∀ (p badge : U64) (recv : Bool) (σ1' : State Unit), + capA st s slot = .Notification p badge true recv → r = .Ok () → + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1')}, False) → + ((), σ') ∈ (sendSignal (Z := Unit) (specPtr p) badge.bv σ1').1 → + InvSpecA call blk don done mi.miLabel args cptr sl c excaps buffer σ σ' := by + intro p badge recv σ1' hcap hr hsp hspec + rw [done_of hr] + have hc2 : CapEqA (.Notification p badge true recv) c := hcap ▸ hc.2 + have e : absCapA (.Notification p badge true recv) = c := hc2 + refine ⟨.invokeNotification (specPtr p) badge.bv, σ1', ?_, hsp, ?_, fun h => by first | cases h | simp_all⟩ + · rw [← e, decodeNotification_specA, if_pos rfl] + exact rfl + · unfold PerfSpecA + rw [if_pos rfl] + refine ⟨[], Or.inl ⟨?_, fun _ _ _ => rfl⟩⟩ + have run : fl4PerformInvocation (Z := Unit) blk call don (.invokeNotification (specPtr p) badge.bv) = + (do withoutPreemption <| sendSignal (specPtr p) badge.bv + Fl4Spec.NonDetE.returnOk []) := by + cases blk <;> cases call <;> cases don <;> rfl + rw [run, ExceptT.run_bind] + exact mem_bindA.2 ⟨(.ok (), σ'), liftE_memA hspec, Set.mem_singleton _⟩ + have ctk : ∀ σ1', setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1')}, False) → + σ1'.curThread = σ.curThread := fun σ1' h => + ((KPresBInst.pres : KPresB (setThreadState (Z := Unit) σ.curThread .restart)) σ ((), σ1') + (by rw [h]; exact Set.mem_singleton _)).ct + have one : ∀ σ1 σ1' : State Unit, setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) → + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1')}, False) → σ1' = σ1 := fun _ _ h h' => + (single_fstA_pair (h'.symm.trans h)) + have epTail : ∀ (p badge : U64) (recv g gr : Bool) (σ1' : State Unit), + capA st s slot = .Endpoint p badge true recv g gr → r = .Ok () → + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1')}, False) → + ((), σ') ∈ (sendIpc (Z := Unit) blk call badge.bv g gr don σ.curThread (specPtr p) σ1').1 → + InvSpecA call blk don done mi.miLabel args cptr sl c excaps buffer σ σ' := by + intro p badge recv g gr σ1' hcap hr hsp hspec + rw [done_of hr] + have hc2 : CapEqA (.Endpoint p badge true recv g gr) c := hcap ▸ hc.2 + have e : absCapA (.Endpoint p badge true recv g gr) = c := hc2 + refine ⟨.invokeEndpoint (specPtr p) badge.bv g gr, σ1', ?_, hsp, ?_, fun h => by first | cases h | simp_all⟩ + · rw [← e, decodeEndpoint_specA, if_pos rfl] + exact rfl + · unfold PerfSpecA + rw [if_pos rfl] + refine ⟨[], Or.inl ⟨?_, fun _ _ _ => rfl⟩⟩ + have run : fl4PerformInvocation (Z := Unit) blk call don (.invokeEndpoint (specPtr p) badge.bv g gr) = + withoutPreemption (do + let thread ← Fl4Spec.NonDet.gets fun s : State Unit => s.curThread + sendIpc blk call badge.bv g gr don thread (specPtr p) + return []) := by + cases blk <;> cases call <;> cases don <;> rfl + rw [run] + refine liftE_memA ?_ + rw [nd_gets_bindSc, ctk _ hsp] + exact mem_bindA.2 ⟨((), σ'), hspec, Set.mem_singleton _⟩ + have repTail : ∀ (q : U64) (g : Bool) (σ1' : State Unit), + capA st s slot = .Reply q g → r = .Ok () → + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1')}, False) → + ((), σ') ∈ (fl4DoReplyTransfer (Z := Unit) σ.curThread (specPtr q) g σ1').1 → + InvSpecA call blk don done mi.miLabel args cptr sl c excaps buffer σ σ' := by + intro q g σ1' hcap hr hsp hspec + rw [done_of hr] + have hc2 : CapEqA (.Reply q g) c := hcap ▸ hc.2 + have e : absCapA (.Reply q g) = c := hc2 + refine ⟨.invokeReply (specPtr q) g, σ1', ?_, hsp, ?_, fun h => by first | cases h | simp_all⟩ + · rw [← e, decodeReply_specA] + exact rfl + · unfold PerfSpecA + rw [if_pos rfl] + refine ⟨[], Or.inl ⟨?_, fun _ _ _ => rfl⟩⟩ + have run : fl4PerformInvocation (Z := Unit) blk call don (.invokeReply (specPtr q) g) = + Fl4Spec.NonDetE.liftE (do + let sender ← Fl4Spec.NonDet.gets fun s : State Unit => s.curThread + fl4DoReplyTransfer sender (specPtr q) g + return []) := by + cases blk <;> cases call <;> cases don <;> rfl + rw [run] + refine liftE_memA ?_ + rw [nd_gets_bindSc, ctk _ hsp] + exact mem_bindA.2 ⟨((), σ'), hspec, Set.mem_singleton _⟩ + have hn : fl4A64Platform.numNodes = 1 → fl4A64Platform.numNodes ≤ n := fun h1 => + h1.symm ▸ Nat.succ_le_of_lt (Nat.lt_of_le_of_lt (Nat.zero_le _) J.sinv.mc.curCore_lt) + cases h with + | @sendNoblock p badge _ _ _ _ recv g gr _ _ _ _ x hslot hcov hcap hext hp hk hc0 => + obtain ⟨xc, σ1, s1, -, -, -, -, hdec, -⟩ := + J.sendNoblockA (lbl := info.label) (len := msgLenA info buf) (buf := buf) (call := call) (don := don) (fp := fp) + hslot hcov hcap hext hp hk hc0 + obtain ⟨hsp, hspec⟩ := hOut + exact epTail p badge recv g gr σ' hcap (Prod.mk.inj (okinj (hd.symm.trans hdec))).1 hsp + (by rw [hspec]; exact Set.mem_singleton _) + | @sendBlock p badge _ _ _ _ recv g gr _ _ _ _ x hCs h1 hmb hmax htu hslot hcov hcap hext hSC hp hk hc0 => + obtain ⟨_, _, _, _, _, _, _, _, hdec, -⟩ := + J.sendBlockA (lbl := info.label) (len := msgLenA info buf) (buf := buf) (call := call) (don := don) (fp := fp) + hA hM hPl hfm hCs h1 hmb hmax htu hslot hcov hcap hext hSC hp hk hc0 + obtain ⟨σ1', hsp, hspec⟩ := hOut + exact epTail p badge recv g gr σ1' hcap (Prod.mk.inj (okinj (hd.symm.trans hdec))).1 hsp hspec + | @sendRecv p badge _ _ _ _ recv g gr _ _ _ _ _ d q hCs h1 hmb hmax htu hslot hcov hcap hext hSC hp hk hsnd hbr0 => + obtain ⟨_, _, _, _, _, _, _, _, hdec, -⟩ := + J.sendRecvA (lbl := info.label) (len := msgLenA info buf) (buf := buf) (blk := blk) (fp := fp) + hA hM hPl hfm hCs h1 hmb hmax htu hslot hcov hcap hext hSC hp hk hsnd hbr0 + obtain ⟨σ1', hsp, hspec⟩ := hOut + exact epTail p badge recv g gr σ1' hcap (Prod.mk.inj (okinj (hd.symm.trans hdec))).1 hsp hspec + | @sendDonate p badge _ _ _ _ recv g gr _ _ _ d q hCs h1 hmb hmax htu hslot hcov hcap hext hSC hp hk hsnd hg hrcv hV => + obtain ⟨_, _, _, _, _, _, _, _, hdec, -⟩ := + J.sendDonateA (lbl := info.label) (len := msgLenA info buf) (buf := buf) (blk := blk) (fp := fp) + hA hM hPl hfm hCs h1 hmb hmax htu hslot hcov hcap hext hSC hp hk hsnd hg hrcv hV + obtain ⟨σ1', hsp, hspec⟩ := hOut + exact epTail p badge recv g gr σ1' hcap (Prod.mk.inj (okinj (hd.symm.trans hdec))).1 hsp hspec + | @sendNoDon p badge _ _ _ _ recv g gr _ _ _ d q hCs h1 hmb hmax htu hslot hcov hcap hext hSC hp hk hsnd hg hrcv => + obtain ⟨_, _, _, _, _, _, _, _, hdec, -⟩ := + J.sendNoDonA (lbl := info.label) (len := msgLenA info buf) (buf := buf) (blk := blk) (fp := fp) + hA hM hPl hfm hCs h1 hmb hmax htu hslot hcov hcap hext hSC hp hk hsnd hg hrcv + obtain ⟨σ1', hsp, hspec⟩ := hOut + exact epTail p badge recv g gr σ1' hcap (Prod.mk.inj (okinj (hd.symm.trans hdec))).1 hsp hspec + | @replyDetached q _ _ _ _ g _ _ _ _ _ x hslot hcov hcap hext hr hk hdet => + obtain ⟨xc, σ1, s1, hkc, e1, -, -, hdec, -⟩ := + J.replyDetachedA (lbl := info.label) (len := msgLenA info buf) (buf := buf) (blk := blk) (call := call) + (don := don) (fp := fp) hslot hcov hcap hext hr hk hdet + obtain ⟨hsp, -⟩ := hOut + have ne : specPtr q ≠ σ.curThread := fun e => by rw [e, hkc] at hk; cases hk + obtain ⟨xc2, σ12, s12, hkc2, e2, hsp2, -⟩ := + J.replyDetachedA (lbl := info.label) (len := msgLenA info buf) (buf := buf) (blk := blk) (call := call) + (don := don) (fp := fp) hslot hcov hcap hext hr hk hdet + have es : σ' = σ12 := one _ _ hsp2 hsp + have hk1 : σ'.kheap (specPtr q) = some (.reply x) := by + rw [es, e2]; simp only [stUpdA, setObjA, Function.update_of_ne ne]; exact hk + refine repTail q g σ' hcap (Prod.mk.inj (okinj (hd.symm.trans hdec))).1 hsp ?_ + rw [fl4ReplyTransfer_detached_specA (grant := g) (t := σ.curThread) hk1 hdet] + exact Set.mem_singleton _ + | @replyLive q _ _ _ _ _ g _ _ _ _ _ _ hCs h1 hag hmb hslot hcov hcap hext hSC hr hk hcl hsnd hrcv hmax htu hconv => + obtain ⟨_, _, _, _, _, _, _, _, hdec, -⟩ := + J.replyLiveA (lbl := info.label) (len := msgLenA info buf) (buf := buf) (blk := blk) (call := call) + (don := don) (fp := fp) hA hM hPl hfm hCs h1 hag hmb hslot hcov hcap hext hSC hr hk hcl hsnd hrcv hmax htu + hconv + obtain ⟨σ1', hsp, hspec⟩ := hOut + exact repTail q g σ1' hcap (Prod.mk.inj (okinj (hd.symm.trans hdec))).1 hsp hspec + | @replyFault q _ _ _ _ _ g _ _ _ _ _ _ _ hCs h1 hag hmb hslot hcov hcap hext hSC hr hk hcl hrcv hmax htu hconv hok => + obtain ⟨_, _, _, _, _, _, _, _, hdec, -⟩ := + J.replyFaultA (lbl := info.label) (len := msgLenA info buf) (buf := buf) (blk := blk) (call := call) + (don := don) (fp := fp) hA hM hPl hfm hCs h1 hag hmb hslot hcov hcap hext hSC hr hk hcl hrcv hmax htu + hconv hok + obtain ⟨σ1', hsp, hspec⟩ := hOut + exact repTail q g σ1' hcap (Prod.mk.inj (okinj (hd.symm.trans hdec))).1 hsp hspec + | @signalWaiting p badge _ _ _ _ recv _ _ _ _ _ x q hCs h1 hmb hmax htu hslot hcov hcap hext hSC hp hk hw => + obtain ⟨xc, σ1, σ2, s2, -, -, -, -, hdec, -⟩ := + J.signalWaitingA hA hM hPl hfm hCs h1 (hn h1) hmb hmax htu (blk := blk) (call := call) (don := don) (fp := fp) + (lbl := info.label) (len := msgLenA info buf) (buf := buf) hslot hcov hcap hext hSC hp hk hw + obtain ⟨xc', σ1', -, -, hsp, hspec⟩ := hOut + exact sigTail p badge recv σ1' hcap (Prod.mk.inj (okinj (hd.symm.trans hdec))).1 hsp hspec + | @signalBoundRecv p badge _ _ _ _ recv _ _ _ _ _ x t e' xt ro d0 hCs h1 hmb hmax htu hslot hcov hcap hext hSC hp hk + hidle hb hkt hs => + obtain ⟨xc, σ1, σ2, s2, -, -, -, -, hdec, -⟩ := + J.signalBoundRecvA hA hM hPl hfm hCs h1 (hn h1) hmb hmax htu (blk := blk) (call := call) (don := don) (fp := fp) + (lbl := info.label) (len := msgLenA info buf) (buf := buf) hslot hcov hcap hext hSC hp hk hidle hb hkt hs + obtain ⟨xc', σ1', -, -, hsp, hspec⟩ := hOut + exact sigTail p badge recv σ1' hcap (Prod.mk.inj (okinj (hd.symm.trans hdec))).1 hsp hspec + | @signalQuiet p badge _ _ _ _ recv _ _ _ _ _ x hcc hmb hmax htu hslot hcov hcap hext hSC hp hk hq => + obtain ⟨xc, σ1, σ2, s2, -, -, -, -, hdec, -⟩ := + J.signalQuietA hA hM hPl hfm hcc hmb hmax htu (blk := blk) (call := call) (don := don) (fp := fp) + (lbl := info.label) (len := msgLenA info buf) (buf := buf) hslot hcov hcap hext hSC hp hk hq + have hr : r = .Ok () := (Prod.mk.inj (okinj (hd.symm.trans hdec))).1 + obtain ⟨xc', σ1', -, -, hsp, hspec⟩ := hOut + rw [done_of hr] + have hc2 : CapEqA (.Notification p badge true recv) c := hcap ▸ hc.2 + have e : absCapA (.Notification p badge true recv) = c := hc2 + refine ⟨.invokeNotification (specPtr p) badge.bv, σ1', ?_, hsp, ?_, fun h => by first | cases h | simp_all⟩ + · rw [← e, decodeNotification_specA, if_pos rfl] + exact rfl + · unfold PerfSpecA + rw [if_pos rfl] + refine ⟨[], Or.inl ⟨?_, fun _ _ _ => rfl⟩⟩ + have run : fl4PerformInvocation (Z := Unit) blk call don (.invokeNotification (specPtr p) badge.bv) = + (do withoutPreemption <| sendSignal (specPtr p) badge.bv + Fl4Spec.NonDetE.returnOk []) := by + cases blk <;> cases call <;> cases don <;> rfl + rw [run, ExceptT.run_bind] + refine mem_bindA.2 ⟨(.ok (), σ'), liftE_memA ?_, Set.mem_singleton _⟩ + rw [hspec] + exact Set.mem_singleton _ + | @cancelBadged _ _ _ _ index depth dest p b c0 cr cgs cg _ _ _ _ hCs h1 hmb hmax htu hslot hcov hcap hext hSC hlbl + hl2 hi hdp hres hdc hdOS => + obtain ⟨xc, σ1, σ2, s2, -, -, -, -, hdec, -⟩ := + J.cancelBadgedA hA hM hPl hfm hCs h1 hmb hmax htu (blk := blk) (call := call) (don := don) + (lbl := info.label) (length := msgLenA info buf) (buf := buf) hslot hcov hcap hext hSC hlbl hl2 hi hdp hres hdc + hdOS + have hr : r = .Ok () := (Prod.mk.inj (okinj (hd.symm.trans hdec))).1 + obtain ⟨xc', σ1', -, -, hsp, hspec⟩ := hOut + rw [done_of hr] + -- the message words + have hT : L.curW s ∈ Z.T := J.sr.curT + have hRV : RegViewA (L.mem s) σ (L.curW s) := fun xs h => (J.ep.at_tcb hT h).regView + have hmi' : getMessageInfo (Z := Unit) (specPtr (L.curW s)) σ = ({(mi, σ)}, False) := ct ▸ hmi + have hmrs' : getMrs (Z := Unit) (specPtr (L.curW s)) buffer mi σ = + ({((args.map fun w => (⟨w⟩ : U64)).map UScalar.bv, σ)}, False) := by + have e : (args.map fun w => (⟨w⟩ : U64)).map UScalar.bv = args := by + rw [List.map_map]; exact List.map_id _ + rw [e, ← ct]; exact hmrs + obtain ⟨hlen, hargs, -⟩ := J.j.1.argsJ hRV hinfo hmi' hBV hmrs' + obtain ⟨xs, hks, -, hmw, -⟩ := J.j.1.curViewA hRV + have hlab : mi.miLabel = info.label.bv := by + have e := single_fstA (hmi'.symm.trans (getMessageInfo_eqA hks)) + have hm := msg_info_specA (L := L) s (L.curW s) + rw [hinfo, WP.spec_ok] at hm + rw [e, hm.1, hmw] + exact label_maskA _ + have h2 : 2 ≤ args.length := by + have : (2#u64 : U64).val ≤ (msgLenA info buf).val := by scalar_tac + rw [hlen, List.length_map] at this + simpa using this + obtain ⟨y0, y1, rest, rfl⟩ : ∃ y0 y1 rest, args = y0 :: y1 :: rest := by + match args, h2 with + | y0 :: y1 :: rest, _ => exact ⟨y0, y1, rest, rfl⟩ + have e0 : (⟨y0⟩ : U64) = index := okinj ((hargs 0#u64 ⟨y0⟩ rfl).symm.trans hi) + have e1 : (⟨y1⟩ : U64) = depth := okinj ((hargs 1#u64 ⟨y1⟩ rfl).symm.trans hdp) + -- the target lookup + have hP := J.j.1.place + have hca : CapRelA (.CNode c0 cr cgs cg) c := hcap ▸ hc + have hcv : CapValidA Z.th (.CNode c0 cr cgs cg) := by + have h := J.j.1.cv _ (hab.isSlot hP) + rw [capAtA_eq, ← hsl, hcap] at h + exact h + obtain ⟨r1, hr1, e2, he1, hrel1⟩ := spec_existsA + (lookup_slot_for_cnode_op_refinesA Z.th s σ J.j.1.st J.j.1.cv false _ c hca hcv index depth) + unfold cspace.lookup_target_slot at hres + rw [hr1] at hres + have er : r1 = .Ok dest := okinj hres + subst er + cases e2 with + | error _ => exact hrel1.elim + | ok dsl => + obtain ⟨hdsl, pd, hpd1, hpd, -⟩ := hrel1 + obtain ⟨ad, hcad, hgd⟩ := J.j.1.st.slot_capA hpd1 hpd + have hcdest : capA st s (cslotAddrA dsl) = .Endpoint p b true true true true := by + rw [← hdsl]; exact hdc + have hadE : ad = .endpointCap (specPtr p) b.bv Fl4SpecA64.allRights := by + have hcad' : CapRelA (capA st s (cslotAddrA dsl)) ad := hcad + rw [hcdest] at hcad' + have h : absCapA (.Endpoint p b true true true true) = ad := hcad'.2 + rw [← h] + show Fl4SpecA64.Cap.endpointCap _ _ _ = _ + rw [(rightsOfA_all _ _ _ _).2 ⟨rfl, rfl, rfl, rfl⟩] + have hg : genInvocationType (specLabelWord (fl4LabelOf mi.miLabel)) = .cnodeCancelBadgedSends := by + rw [hlab, hlbl, fl4LabelOf_bvA] + have e : labNA (fl4_abi.label.CNodeCancelBadgedSends).val = + .spec (.genInvocationLabel .cnodeCancelBadgedSends) := by + rw [show (fl4_abi.label.CNodeCancelBadgedSends).val = 23 by simp [fl4_abi.label.CNodeCancelBadgedSends]] + decide + rw [e] + have e' : invocationType (specLabelWord (.spec (.genInvocationLabel .cnodeCancelBadgedSends))) = + .genInvocationLabel .cnodeCancelBadgedSends := invocationType_fromEnumA _ + unfold genInvocationType + rw [e'] + have hlk : (lookupTargetSlot (Z := Unit) c (dataToCptr y0) (dataToNat y1)).run σ = ({(.ok dsl, σ)}, False) := by + rw [← e0, ← e1] at he1 + exact he1 + have hrun := (dec_cancel_okA (excaps := excaps.map Prod.fst) (rest := rest) hg hlk).trans + (cancelArm_runA (σ := σ) dsl hgd) + have hy : hasCancelSendRights ad := by rw [hadE]; rfl + rw [if_pos hy, hadE] at hrun + have ec : c = .cnodeCap (specPtr c0) cr.val (bitsOfA cg.val cgs.val) := by + have h : absCapA (.CNode c0 cr cgs cg) = c := hca.2 + exact h.symm + refine ⟨.invokeCNode (.cancelBadgedSendsCall (.endpointCap (specPtr p) b.bv Fl4SpecA64.allRights)), σ1', + ?_, hsp, ?_, fun h => by first | cases h | simp_all⟩ + · have dec : fl4DecodeInvocation (Z := Unit) call false mi.miLabel (y0 :: y1 :: rest) cptr sl c excaps buffer = + Fl4Spec.NonDetE.liftME Fl4Invocation.invokeCNode + (decodeCnodeInvocation (specLabelWord (fl4LabelOf mi.miLabel)) (y0 :: y1 :: rest) c + (excaps.map Prod.fst)) := by + rw [ec]; rfl + rw [dec, run_liftME_okA hrun] + exact rfl + · unfold PerfSpecA + rw [if_pos rfl] + refine ⟨[], Or.inl ⟨?_, fun _ _ _ => rfl⟩⟩ + have run : fl4PerformInvocation (Z := Unit) blk call don + (.invokeCNode (.cancelBadgedSendsCall (.endpointCap (specPtr p) b.bv Fl4SpecA64.allRights))) = + (do invokeCnode (.cancelBadgedSendsCall (.endpointCap (specPtr p) b.bv Fl4SpecA64.allRights)) + Fl4Spec.NonDetE.returnOk []) := by + cases blk <;> cases call <;> cases don <;> rfl + rw [run, ExceptT.run_bind] + exact mem_bindA.2 ⟨(.ok (), σ'), hspec, Set.mem_singleton _⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3InvSpecL1A.lean b/proofs/Fl4ProofsA64/P3/Stage3InvSpecL1A.lean new file mode 100644 index 000000000..8ed4614ee --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3InvSpecL1A.lean @@ -0,0 +1,167 @@ +/- +P3 (aarch64, A64-P3 Lift 1b, T3 dispatch): **`InvSpecA` for the Lift 1 groups** (the Domain, `Err`, and shared +front-end plumbing; the other groups are in `P3/Stage3InvSpecL1{Fl,Rp,Irq}A`). + +Each group's theorem has the shape of `IpcScopeA.invSpecA`: from a scope member, the FL4 decode's outcome `r`, the +scope's `Out`, and the front end's values (`FrontPinA`, `CapRelA`, `ExtraOKA`, `ArgsLabA`), the spec decode of the +actual cap and message has the operation the arm's `Out` performs. The scope constructors' spec data are pinned by +equations (`FrontPinA`), as Plan B's. + +* `ArgsLabA`, `JSyscallA.argsLabA`: the spec message info's label word is FL4's, and the spec message words are FL4's + `syscall_arg`s. +* `ErrScopeA.false_of`: an `Err` group member contradicts `done` (its `Err` is not `Preempted`): `InvSpecA` is vacuous. +* `DomScopeA.invSpecA`. + +Named premises: those of the arms (`DomScopeA.sound`). New module. +-/ +import Fl4ProofsA64.P3.Stage3DispTopA +import Fl4ProofsA64.P3.Stage3InvSpecIpcA +import Fl4ProofsA64.P3.Stage3InvSpecTopA +import Fl4ProofsA64.P3.DomainSetDispatchA +import Fl4ProofsA64.P3.Stage3PinCnA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-- A perform whose spec step is a lifted `m` followed by the empty reply. -/ +theorem perform_liftThenNilA {m : Fl4Spec.NonDet (State Unit) Unit} {σ σ' : State Unit} + (h : m σ = ({((), σ')}, False)) : + (.ok [], σ') ∈ ((Fl4Spec.NonDetE.liftE (ε := Unit) (do m; return ([] : List Data)) : + PMonad Unit (List Data)).run σ).1 := by + refine liftE_memA ?_ + exact mem_bindA.2 ⟨((), σ'), by rw [h]; exact Set.mem_singleton _, Set.mem_singleton _⟩ + +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {ar : machine.Arch A} {s : S} {a : A} {σ : State Unit} {m : McState} + +/-- **An `Err` group member contradicts a `done` outcome** (module docstring). -/ +theorem ErrScopeA.false_of {lbl len slot : U64} {blk call don fp : Bool} {buf : U64} + {extra : Aeneas.Std.Array obj.Slot 3#usize} {Out : State Unit → Prop} + (h : ErrScopeA st AR Z OS ar s a σ lbl len slot blk call don fp buf extra Out) + {r : core.result.Result Unit error.Exc} {s' : S} {a' : A} + (hd : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don fp buf extra = + ok (r, s', a')) + {done : Bool} (hdone : (done = true ∧ r = .Ok ()) ∨ (done = false ∧ r = .Err error.Exc.Preempted)) : False := by + have key : ∀ (e : error.Exc) (s'' : S) (a'' : A), e ≠ .Preempted → + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don fp buf extra = + ok (.Err e, s'', a'') → False := by + intro e s'' a'' hne hd' + have := (Prod.mk.inj (okinj (hd'.symm.trans hd))).1 + rcases hdone with ⟨-, h1⟩ | ⟨-, h1⟩ + · rw [h1] at this; cases this + · rw [h1] at this + exact hne (core.result.Result.Err.inj this) + cases h with + | nonArch hA _ _ _ _ _ _ _ _ _ hcap hCs h1 hsl e s' a' hd' hne => exact key e s' a' hne hd' + | irqControl hA hM _ _ _ _ _ _ _ _ _ hcap e s' a' hd' hne => exact key e s' a' hne hd' + +set_option maxHeartbeats 4000000 in +/-- **The Domain group's `InvSpecA`** (module docstring). -/ +theorem DomScopeA.invSpecA (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) + s a σ m) + (hfm : A64FrameMasks st s) + {lbl len slot : U64} {blk call don fp : Bool} {buf : U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} + {Out : State Unit → Prop} + (h : DomScopeA st AR Z OS ar s a σ lbl len slot blk call don fp buf extra Out) + {info : msginfo.MessageInfo} (hlI : lbl = info.label) (hlnI : len = msgLenA info buf) + {r : core.result.Result Unit error.Exc} {s' : S} {a' : A} {σ' : State Unit} + (hd : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don fp buf extra = + ok (r, s', a')) (hOut : Out σ') + {done : Bool} (hdone : (done = true ∧ r = .Ok ()) ∨ (done = false ∧ r = .Err error.Exc.Preempted)) + {mi : MessageInfo} {sl : CslotPtr} {c : Cap} {excaps : List (Cap × CslotPtr)} {buffer : Option ObjRef} + {args : List Data} (F : FrontPinA Z.th σ slot mi sl c excaps buffer args) (hc : CapRelA (capA st s slot) c) + (hx : ExtraOKA Z.th (L.mem s) extra excaps) (B : ArgsLabA st s info buf mi args) (cptr : CapRef) : + InvSpecA call blk don done mi.miLabel args cptr sl c excaps buffer σ σ' := by + cases h with + | set slot hsl hcov hcap len buf x0 t blk call don extra hext hSC hl0 hd0 he0 hx1 hx0 hcx hCs h1 => + have ht : t ∈ Z.T := J.tcbs ▸ (J.toJStage3A.slotCapTy hx0).1.2.2.2.2.2 t hcx + have T := J.toJStage3A.toTA J.sact J.noSwitch + have hon : L.mem s (affA t) = L.cpu s := T.onCore_single hCs h1 ht + have hstall := J.stallTA hCs h1 ht + obtain ⟨σ1, σ2, s2, -, -, hdec, -⟩ := + J.setDomainA hA hM hPl hfm hsl hcov hcap hext hSC hl0 hd0 he0 hx1 hx0 hcx hon hstall + have hr : r = .Ok () := (Prod.mk.inj (okinj (hd.symm.trans hdec))).1 + have hdn : done = true := by + rcases hdone with ⟨h2, -⟩ | ⟨-, h2⟩ + · exact h2 + · rw [hr] at h2; cases h2 + subst hdn + obtain ⟨σ1', e1', e2'⟩ := hOut + obtain ⟨hlab, argsU, hae, hlen, hargs⟩ := B + have hc2 : CapEqA .Domain c := by rw [← hcap]; exact hc.2 + have ec : c = .domainCap := (show absCapA .Domain = c from hc2).symm + -- the message words: the domain 0 + have hne : argsU ≠ [] := fun e => hl0 (by + apply UScalar.eq_of_val_eq + have : len.val = 0 := by rw [hlnI, hlen, e]; rfl + simpa using this) + obtain ⟨y0, rest, rfl⟩ : ∃ y0 rest, argsU = y0 :: rest := by + cases argsU with + | nil => exact absurd rfl hne + | cons y r => exact ⟨y, r, rfl⟩ + have ey : y0 = 0#u64 := okinj ((hargs 0#u64 y0 rfl).symm.trans hd0) + subst ey + -- the extra thread cap + have ex0 : x0 = exAtA extra 0 := okinj (he0.symm.trans (exAtA_idx0 extra)) + obtain ⟨cap0, sl0, erest, hexc, hcap0⟩ : ∃ cap0 sl0 erest, excaps = (cap0, sl0) :: erest ∧ + cap0 = .threadCap (specPtr t) := by + cases hcs : excaps with + | nil => exact absurd (ex0.trans (hx.2.2 0 (by simp [hcs]) (by omega))) hx1 + | cons y rest' => + obtain ⟨ps, -, -, -, hrel⟩ := hx.2.1 0 (by simp [hcs]) + refine ⟨y.1, y.2, rest', rfl, ?_⟩ + simp only [hcs, List.getElem_cons_zero] at hrel + rw [← ex0, capAtA_eq (st := st), hcx] at hrel + exact (show absCapA (.Thread t) = y.1 from hrel.2).symm + subst hcap0 + have hg : genInvocationType (specLabelWord (fl4LabelOf mi.miLabel)) = .domainSetSet := by + rw [hlab, ← hlI] + refine genLabel_specA ?_ + rw [fl4_abi.label.DomainSetSet] + rfl + have hsp : (decodeDomainInvocation (Z := Unit) (specLabelWord (fl4LabelOf mi.miLabel)) + ((0#u64).bv :: rest.map UScalar.bv) ((.threadCap (specPtr t), sl0) :: erest)).run σ = + ({(.ok (.invokeDomainSet (specPtr t) 0), σ)}, False) := by + unfold decodeDomainInvocation + rw [hg] + dsimp only + rw [domainSet_specA] + have hnd : ¬ (0#u64).bv.toNat ≥ numDomains := by + have := numDomains_not_zero + have h0 : (0#u64).bv.toNat = 0 := rfl + omega + simp only [hnd, ↓reduceIte] + rfl + refine ⟨.invokeDomain (.invokeDomainSet (specPtr t) 0), σ1', ?_, e1', ?_, fun h => by first | cases h | simp_all⟩ + · rw [hae, hexc, ec] + have : fl4DecodeInvocation (Z := Unit) call false mi.miLabel ((0#u64 :: rest).map UScalar.bv) cptr sl .domainCap + ((.threadCap (specPtr t), sl0) :: erest) buffer = + Fl4Spec.NonDetE.liftME Fl4Invocation.invokeDomain (decodeDomainInvocation + (specLabelWord (fl4LabelOf mi.miLabel)) ((0#u64).bv :: rest.map UScalar.bv) ((.threadCap (specPtr t), sl0) :: erest)) := rfl + rw [List.map_cons] at this ⊢ + rw [this, run_liftME_okA hsp] + exact rfl + · unfold PerfSpecA + rw [if_pos rfl] + refine ⟨[], Or.inl ⟨?_, fun _ _ _ => rfl⟩⟩ + have run : fl4PerformInvocation (Z := Unit) blk call don (.invokeDomain (.invokeDomainSet (specPtr t) 0)) = + Fl4Spec.NonDetE.liftE (do invokeDomain (Z := Unit) (.invokeDomainSet (specPtr t) 0) + return ([] : List Data)) := by + cases blk <;> cases call <;> cases don <;> rfl + rw [run] + exact perform_liftThenNilA e2' + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3InvSpecL1CtlA.lean b/proofs/Fl4ProofsA64/P3/Stage3InvSpecL1CtlA.lean new file mode 100644 index 000000000..fc93b3087 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3InvSpecL1CtlA.lean @@ -0,0 +1,244 @@ +/- +P3 (aarch64, A64-P3 Lift 1b, T3 dispatch): **the IRQ control issue arms' spec decode** (`IRQIssueIRQHandler`, +`ARMIRQIssueIRQHandlerTrigger`, `…TriggerCore`, `…MSI`), the spec side of `InvSpecA` for `IrqScopeA`'s issue +constructors (`IrqScopeA.invSpecA` in `P3/Stage3InvSpecL1IrqA`). + +* `irqOf_bvA`: FL4's `layout::irq::of` of an SPI/banked interrupt is the spec's `fl4IrqOf`. +* `extra0_exA`, `JSyscallA.irqTargetA`: the issue's extra CNode cap is the front end's first extra cap, and the spec's + `fl4IrqTarget` (IRQ inactive, `lookupTargetSlot`, `ensureEmpty`) returns the destination slot the decode found. +* `IrqCtlArm.invSpecA`: `InvSpecA` from the spec decode's outcome. + +Named premises: [P] `IrqPlatA` (the platform's `max_spi` is the spec platform's). The kernel's node count is not a +premise: it is read from the record (`JStage3A.domOK`'s `nodes`, `NdOKA.nodesGet`; record edit F1). New module. +-/ +import Fl4ProofsA64.P3.Stage3InvSpecL1IrqA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Vcpu Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +/-- **[P] The IRQ control decode's platform constant is the spec platform's**: `Arch::max_spi`. (The kernel's node +count word `NUM_NODES` is a store word, read from the record's `domOK`, not a premise: a `∀`-over-store-states +premise on a memory word is unsatisfiable, record edit F1.) -/ +structure IrqPlatA {S A : Type} (st : machine.Store S) (ar : machine.Arch A) : Prop where + maxSpi : ∀ a, ar.max_spi a ⦃ m => m.bv = fl4A64Platform.maxSpi ⦄ + +/-- An IRQ number word, truncated to the spec's IRQ width. -/ +theorem ucast_irqA {w : U64} {v : Word} (h : w.bv = v) : ucast irqLen v = BitVec.ofNat 13 w.val := by + subst h + show BitVec.setWidth 13 w.bv = BitVec.ofNat 13 w.bv.toNat + simp [BitVec.ofNat_toNat] + +/-- **FL4's `irq::of` of an SPI or banked interrupt is the spec's `fl4IrqOf`.** -/ +theorem layout.irq.of.bvA {id : U64} (cpu : U64) (h : id.val ≤ 1019) : + layout.irq.of id cpu ⦃ i => i.bv = fl4IrqOf id.bv cpu.bv ⦄ := by + unfold layout.irq.of + have h1 : ¬ (id >= layout.irq.LPI_ID_BASE) := by + simp only [layout.irq.LPI_ID_BASE, ge_iff_le, not_le]; scalar_tac + rw [if_neg h1] + have hs1 : ¬ (id.bv ≥ fl4LpiIdBase) := by + intro hh + have := BitVec.le_def.1 hh + have h8 : fl4LpiIdBase.toNat = 8192 := rfl + have : id.bv.toNat = id.val := rfl + omega + unfold fl4IrqOf + rw [if_neg hs1] + by_cases h2 : id < layout.irq.NUM_BANKED + · rw [if_pos h2] + have hb : id.bv < fl4NumBanked := by + simp only [layout.irq.NUM_BANKED] at h2 + rw [BitVec.lt_def] + have h32 : fl4NumBanked.toNat = 32 := rfl + have : id.bv.toNat = id.val := rfl + scalar_tac + rw [if_pos hb] + simp only [layout.irq.NUM_BANKED, layout.irq.BANKED_CORES, layout.irq.BANKED_BASE] at h2 ⊢ + step as ⟨q, hq⟩ + simp only [lift, bind_ok] + refine (WP.spec_ok _).2 ?_ + have hq' : q.val = cpu.val % 128 := by scalar_tac + apply BitVec.eq_of_toNat_eq + simp [core.num.U64.wrapping_add_val_eq, core.num.U64.wrapping_mul_val_eq, U64.size_def, U64.numBits] + have hc : cpu.bv.toNat = cpu.val := rfl + have hi : id.bv.toNat = id.val := rfl + have h2' : id.val < 32 := by scalar_tac + have hqb : q.val < 128 := by omega + have e1 : (fl4BankedBase).toNat = 1024 := rfl + have e2 : (fl4BankedCores).toNat = 128 := rfl + have e3 : (fl4NumBanked).toNat = 32 := rfl + simp only [BitVec.toNat_add, BitVec.toNat_mul, BitVec.toNat_umod, e1, e2, e3, hc, hi] + omega + · rw [if_neg h2] + have hb : ¬ id.bv < fl4NumBanked := by + intro hh + apply h2 + simp only [layout.irq.NUM_BANKED] + have := BitVec.lt_def.1 hh + have h32 : fl4NumBanked.toNat = 32 := rfl + have : id.bv.toNat = id.val := rfl + scalar_tac + rw [if_neg hb] + exact (WP.spec_ok _).2 rfl + +/-- **The first extra slot's cap**, from the decoder's `extra0` (the slot is non-empty). -/ +theorem extra0_exA {S : Type} {st : machine.Store S} [L : LawfulStore st] {s : S} + {extra : Aeneas.Std.Array obj.Slot 3#usize} {root : capability.Cap} + (he : interrupt.extra0 st s extra = ok (some root)) : + exAtA extra 0 ≠ 0#u64 ∧ root = capA st s (exAtA extra 0) := by + unfold interrupt.extra0 at he + obtain ⟨x, hx, he⟩ := bind_eq_ok he + obtain ⟨b, hb, he⟩ := bind_eq_ok he + have hx' : x = exAtA extra 0 := okinj (hx.symm.trans (exAtA_idx0 extra)) + subst hx' + unfold obj.Slot.is_null at hb + have hb' : b = decide (exAtA extra 0 = 0#u64) := (okinj hb).symm + by_cases h0 : exAtA extra 0 = 0#u64 + · rw [hb', decide_eq_true h0, if_pos rfl] at he + cases okinj he + · rw [hb', decide_eq_false h0] at he + simp only [Bool.false_eq_true, ↓reduceIte] at he + rw [capA_eq, bind_ok] at he + exact ⟨h0, (Option.some.inj (okinj he)).symm⟩ + + +/-- A U64 comparison against a constant, as a bit-vector one. -/ +theorem not_ge_bvA {x y : U64} {w : Word} (hw : y.bv = w) (h : ¬ x ≥ y) : ¬ x.bv ≥ w := by + intro hh + apply h + subst hw + show y ≤ x + rw [UScalar.le_equiv] + exact BitVec.le_def.1 hh + +theorem not_gt_bvA {x m : U64} {w : Word} (hw : m.bv = w) (h : ¬ x > m) : ¬ x.bv > w := by + intro hh + apply h + subst hw + show m < x + rw [UScalar.lt_equiv] + exact BitVec.lt_def.1 hh + +theorem bne_decideA (t : U64) : (t != 0#u64) = decide (t.bv ≠ (0 : Word)) := by + by_cases h : t = 0#u64 + · subst h; rfl + · have h1 : (t != 0#u64) = true := by simp [bne_iff_ne, h] + have h2 : t.bv ≠ 0 := fun e => h (UScalar.eq_of_val_eq (by + have := congrArg BitVec.toNat e + simpa using this)) + rw [h1]; exact (decide_eq_true h2).symm + + +/-- **The spec's MSI decode accepts** (with the `.ok` answer of the nondeterministic ITS backend). -/ +theorem decMsi_memA {σ : State Unit} {n device event index d cpu : Data} (rest : List Data) {src dest : CslotPtr} + {cap : Cap} (caps : List Cap) (hn : ¬ n ≥ fl4NumLpis) (hcpu : ¬ cpu.toNat ≥ fl4A64Platform.numNodes) + (ht : (fl4IrqTarget (Z := Unit) (ucast irqLen (fl4LpiBase + n)) cap index (d &&& 0xff)).run σ = + ({(.ok dest, σ)}, False)) : + (.ok (Fl4IrqControlInvocation.msi (ucast irqLen (fl4LpiBase + n)) dest src device event cpu), σ) ∈ + ((decodeFl4IrqMsi (Z := Unit) (n :: device :: event :: index :: d :: cpu :: rest) src (cap :: caps)).run σ).1 := by + have hl : (n :: device :: event :: index :: d :: cpu :: rest).length ≥ 6 ∧ (cap :: caps).length ≥ 1 := + ⟨by simp, by simp⟩ + unfold decodeFl4IrqMsi + rw [if_pos hl] + simp only [the, List.getElem?_cons_zero, List.getElem?_cons_succ, Option.getD_some, Fl4Spec.NonDetE.whenE, + hn, hcpu, ↓reduceIte, ndE_pure_bindD] + rw [run_bind_okD ht] + rw [ExceptT.run_bind] + exact mem_bindA.2 ⟨(.ok Fl4MsiAnswer.ok, σ), (by + unfold Fl4Spec.NonDetE.liftE + rw [ExceptT.run_lift] + exact ⟨(Fl4MsiAnswer.ok, σ), ⟨Set.mem_univ _, rfl⟩, rfl⟩), Set.mem_singleton _⟩ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s : S} {a : A} {σ : State Unit} {m : McState} + +set_option maxHeartbeats 4000000 in +/-- **The issue's spec target** (module docstring). -/ +theorem JSyscallA.irqTargetA (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + {extra : Aeneas.Std.Array obj.Slot 3#usize} {excaps : List (Cap × CslotPtr)} + (hx : ExtraOKA Z.th (L.mem s) extra excaps) (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) + {root : capability.Cap} {dest index depth irq : U64} + (he : interrupt.extra0 st s extra = ok (some root)) + (hslot : cspace.lookup_target_slot st s root index depth = ok (.Ok dest)) + (hempty : cdt.ensure_empty_slot st s dest = ok (.Ok ())) + (hactive : interrupt.is_irq_active st s irq = ok false) (hirq : irq.val < 5376) + {sd : CslotPtr} (hAd : AbsSlotA Z.th sd) (hsd : cslotAddrA sd = dest) : + ∃ cp sl0 erest, excaps = (cp, sl0) :: erest ∧ + (fl4IrqTarget (Z := Unit) (BitVec.ofNat 13 irq.val) cp index.bv depth.bv).run σ = ({(.ok sd, σ)}, False) := by + obtain ⟨hn0, hroot⟩ := extra0_exA he + obtain ⟨cp, sl0, erest, hexc, hsl0, hab0, hrel0⟩ := exCap0A hx (exAtA_idx0 extra) hn0 + refine ⟨cp, sl0, erest, hexc, ?_⟩ + have hrelR : CapRelA root cp := by rw [hroot]; rw [capAtA_eq] at hrel0; exact hrel0 + have hv : CapValidA Z.th root := by + have h := J.j.1.cv (exAtA extra 0) (by rw [← hsl0]; exact hab0.isSlot J.j.1.place) + rw [capAtA_eq] at h + rw [hroot]; exact h + unfold cspace.lookup_target_slot at hslot + have hl := lookup_slot_for_cnode_op_refinesA (L := L) Z.th s σ J.j.1.st J.j.1.cv false root cp hrelR hv index depth + obtain ⟨e1, he1, hrel⟩ := post_of_okA hl hslot + cases e1 with + | error _ => exact hrel.elim + | ok dsl => + obtain ⟨hd, pd, hpd1, hpd, -⟩ := hrel + have edsl : dsl = sd := cslotAddrA_inj J.j.1.place ⟨pd, hpd1, Or.inl hpd⟩ hAd (hd.symm.trans hsd.symm) + subst edsl + obtain ⟨sd', pd', d1, d2, d3, hdn, -⟩ := J.toJStage3A.irqDest_ofA hext he hslot hempty + have hdn' : capA st s (cslotAddrA dsl) = .Null := by rw [← hd]; exact hdn + obtain ⟨ad, hcad, hgd⟩ := J.j.1.st.slot_capA hpd1 hpd + replace hcad : CapRelA (capA st s (cslotAddrA dsl)) ad := hcad + have hadn : ad = .nullCap := by + have h := hcad.2; rw [hdn'] at h; exact h.symm + have hee := ensureEmpty_runA dsl σ hgd + rw [if_pos hadn] at hee + have hact : isIrqActive (Z := Unit) (BitVec.ofNat 13 irq.val) σ = ({(false, σ)}, False) := + (post_of_okA (irqActive_refinesA (st := st) (s := s) (irq := irq) J.j.1.irq hirq) hactive :) + have hlt : (lookupTargetSlot (Z := Unit) cp (dataToCptr index.bv) (unat depth.bv)).run σ = + ({(.ok dsl, σ)}, False) := he1 + unfold fl4IrqTarget + rw [run_bind_okD (run_liftEA hact)] + simp only [Fl4Spec.NonDetE.whenE, Bool.false_eq_true, ↓reduceIte, ndE_pure_bindD] + rw [run_bind_okD hlt, run_bind_okD hee] + rfl + +/-- **An IRQ control issue arm's `InvSpecA`**, from the spec decode's outcome `ii` (module docstring). -/ +theorem IrqCtlArm.invSpecA {call blk don : Bool} {ii : Fl4IrqControlInvocation} {σ σ1 σ' : State Unit} + {mi : MessageInfo} {sl : CslotPtr} {c : Cap} {excaps : List (Cap × CslotPtr)} {buffer : Option ObjRef} + {args : List Data} {cptr : CapRef} (hc : c = .irqControlCap) + (hdec : (.ok (Fl4Invocation.invokeIRQControl ii), σ) ∈ + ((fl4DecodeInvocation (Z := Unit) call false mi.miLabel args cptr sl c excaps buffer).run σ).1) + (hset : setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False)) + (hper : (.ok (), σ') ∈ ((invokeFl4IrqControl (Z := Unit) ii).run σ1).1) : + InvSpecA call blk don true mi.miLabel args cptr sl c excaps buffer σ σ' := by + refine ⟨.invokeIRQControl ii, σ1, ?_, hset, ?_, fun h => by first | cases h | simp_all⟩ + · exact hdec + · unfold PerfSpecA + rw [if_pos rfl] + refine ⟨[], Or.inl ⟨?_, fun _ _ _ => rfl⟩⟩ + have run : fl4PerformInvocation (Z := Unit) blk call don (.invokeIRQControl ii) = + (do invokeFl4IrqControl (Z := Unit) ii + Fl4Spec.NonDetE.returnOk ([] : List Data) : PMonad Unit (List Data)) := by + cases blk <;> cases call <;> cases don <;> rfl + rw [run] + exact perform_thenNilA hper + +/-- The spec's IRQ control decode does not read the `CapRef`, the buffer, `calling`, or the extra caps' slots. -/ +theorem dec_irqCtl_eqA (call : Bool) (lab : Data) (args : List Data) (cptr : CapRef) (sl : CslotPtr) + (excaps : List (Cap × CslotPtr)) (buffer : Option ObjRef) : + fl4DecodeInvocation (Z := Unit) call false lab args cptr sl .irqControlCap excaps buffer = + fl4DecodeInvocation (Z := Unit) false false lab args [] sl .irqControlCap (excaps.map fun p => (p.1, sl)) none := by + unfold fl4DecodeInvocation + dsimp only + rw [List.map_map] + rfl + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3InvSpecL1FlA.lean b/proofs/Fl4ProofsA64/P3/Stage3InvSpecL1FlA.lean new file mode 100644 index 000000000..501706f8f --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3InvSpecL1FlA.lean @@ -0,0 +1,224 @@ +/- +P3 (aarch64, A64-P3 Lift 1b, T3 dispatch): **the cache-maintenance group's `InvSpecA`** (`FlScopeA.invSpecA`). + +The decoder's `CapRef` argument is not read by the flush decoders (`flDec_xslot*A`), so the scope's decode (at `[]`) +is the actual one at any `cptr`. A flush perform returns `[]` (`flushPerf_nilA`); the spec decode of the actual cap is +the one `FlushOutA` runs, since the scope's cap is `absCapA` of FL4's (`CapEqA` is `absCapA`-equality off a mapped +page table). An `Err` outcome is a related `Sys` error, never `Preempted`: the group is always `done`. + +Named premises: those of the arms (`FlScopeA.sound`). New module. +-/ +import Fl4ProofsA64.P3.Stage3InvSpecL1A +import Fl4ProofsA64.P3.Stage3DispFlushA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +/-- A related cap-lookup / decode error is never `Preempted`. -/ +theorem VsErrRelA.ne_preemptedA {e : error.Exc} {e' : SyscallError} (h : VsErrRelA e e') : e ≠ .Preempted := by + rintro rfl + rcases h with h | ⟨n, h, -⟩ + · cases e' <;> exact h + · cases h + +/-- A lifted `m` then the empty reply returns `[]`. -/ +theorem liftE_bindNilA {m : Fl4Spec.NonDet (State Unit) Unit} {σ σ' : State Unit} {rv : List Data} + (h : (.ok rv, σ') ∈ ((Fl4Spec.NonDetE.liftE (ε := Unit) (do m; return ([] : List Data)) : + PMonad Unit (List Data)).run σ).1) : rv = [] := by + unfold Fl4Spec.NonDetE.liftE at h + rw [ExceptT.run_lift] at h + obtain ⟨y, hy, hyy⟩ := mem_bindA.1 h + obtain ⟨z, hz, hzz⟩ := mem_bindA.1 hy + have e1 : y = ([], z.2) := Set.mem_singleton_iff.1 hzz + have e2 : (Except.ok rv, σ') = (Except.ok y.1, y.2) := Set.mem_singleton_iff.1 hyy + have e3 := Except.ok.inj (Prod.mk.inj e2).1 + rw [e3, e1] + +/-- **A flush invocation's perform returns `[]`.** -/ +theorem flushPerf_nilA {inv : Fl4ArchInvocation} (h : FlushInvA inv) {σ σ' : State Unit} {rv : List Data} + (hp : (.ok rv, σ') ∈ ((fl4ArchPerformInvocation (Z := Unit) inv).run σ).1) : rv = [] := by + rcases inv with iv | iv | iv | iv | iv | iv + all_goals try exact h.elim + · cases iv with + | invokeVSpace o => + exact liftE_bindNilA (m := performVspaceInvocation (Z := Unit) o) hp + | invokePage p => + cases p with + | pageFlush t vs ve ps sp asd => + exact liftE_bindNilA (m := performFlush (Z := Unit) t vs ve ps sp asd) hp + | _ => exact h.elim + | _ => exact h.elim + · cases iv with + | flush o => exact liftE_bindNilA (m := performVspaceInvocation (Z := Unit) o) hp + | _ => exact h.elim + +/-- **The front end's values are unique.** -/ +theorem FrontPinA.unique {th : TypedHeapA} {kb : U64} (hP : PlaceA th kb) {σ : State Unit} {slot : U64} + {mi0 mi : MessageInfo} {sl0 sl : CslotPtr} {c0 c : Cap} {ex0 ex : List (Cap × CslotPtr)} + {b0 b : Option ObjRef} {a0 a : List Data} + (F0 : FrontPinA th σ slot mi0 sl0 c0 ex0 b0 a0) (F : FrontPinA th σ slot mi sl c ex b a) : + mi0 = mi ∧ sl0 = sl ∧ c0 = c ∧ ex0 = ex ∧ b0 = b ∧ a0 = a := by + have e1 := single_fstA (F0.hmi.symm.trans F.hmi) + subst e1 + have e2 := cslotAddrA_inj hP F0.habs F.habs (F0.hslot.symm.trans F.hslot) + subst e2 + have e3 := single_fstA (F0.hcap.symm.trans F.hcap) + have e4 := single_fstA (F0.hbuf.symm.trans F.hbuf) + subst e3 e4 + have e5 := Except.ok.inj (single_fstA (F0.hex.symm.trans F.hex)) + have e6 := single_fstA (F0.hargs.symm.trans F.hargs) + subst e5 e6 + exact ⟨rfl, rfl, rfl, rfl, rfl, rfl⟩ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {ar : machine.Arch A} {nA : U64} {s : S} {a : A} {σ : State Unit} {m : McState} + +/-- **One flush arm's `InvSpecA`**, from its post and `FlushOutA`. -/ +theorem FlushPostA.invSpecA {call blk don : Bool} {l : Fl4Label} {args0 : List Data} {cte : CslotPtr} + {ac : ArchCap} {exs : List (Cap × CslotPtr)} + (hxs : ∀ x : CapRef, fl4ArchDecodeInvocation (Z := Unit) call l args0 x cte ac exs = + fl4ArchDecodeInvocation (Z := Unit) call l args0 [] cte ac exs) + (hshape : ∀ inv, (.ok inv, σ) ∈ ((fl4ArchDecodeInvocation (Z := Unit) call l args0 [] cte ac exs).run σ).1 → + FlushInvA inv) + {r : core.result.Result Unit error.Exc} {s' : S} {a' : A} {σ' : State Unit} + (hP : FlushPostA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar nA m σ + ((fl4ArchDecodeInvocation (Z := Unit) call l args0 [] cte ac exs).run σ) (r, s', a')) + (hOut : FlushOutA σ ((fl4ArchDecodeInvocation (Z := Unit) call l args0 [] cte ac exs).run σ) σ') + {done : Bool} (hdone : (done = true ∧ r = .Ok ()) ∨ (done = false ∧ r = .Err error.Exc.Preempted)) + {mi : MessageInfo} {c : Cap} (hcc : c = .archObjectCap ac) (hlab : fl4LabelOf mi.miLabel = l) + {buffer : Option ObjRef} (cptr : CapRef) : + InvSpecA call blk don done mi.miLabel args0 cptr cte c exs buffer σ σ' := by + unfold FlushPostA at hP + dsimp only at hP + cases r with + | Err e => + obtain ⟨-, e', -, hrel⟩ := hP + exfalso + rcases hdone with ⟨-, h⟩ | ⟨-, h⟩ + · cases h + · exact hrel.ne_preemptedA (core.result.Result.Err.inj h) + | Ok u => + cases u + obtain ⟨inv0, x0, σ0, rv0, hD0, -⟩ := hP + rcases hOut with ⟨-, e', hD⟩ | ⟨inv, x, rv, hD1, hnf, hx, hset, hperf⟩ + · exfalso + rw [hD] at hD0 + have := Set.mem_singleton_iff.1 hD0 + cases this + · have hdn : done = true := by + rcases hdone with ⟨h2, -⟩ | ⟨-, h2⟩ + · exact h2 + · cases h2 + subst hdn + have hrv : rv = [] := flushPerf_nilA (hshape inv hD1) hperf + subst hrv + refine ⟨.invokeArchObject inv, stUpdA σ σ.curThread x .restart, ?_, hset, ?_, fun h => by first | cases h | simp_all⟩ + · have dec : fl4DecodeInvocation (Z := Unit) call false mi.miLabel args0 cptr cte c exs buffer = + Fl4Spec.NonDetE.liftME Fl4Invocation.invokeArchObject + (fl4ArchDecodeInvocation call (fl4LabelOf mi.miLabel) args0 cptr cte ac exs) := by + rw [hcc]; rfl + rw [dec, hlab, hxs cptr] + exact mem_liftME_okPinA hD1 + · unfold PerfSpecA + rw [if_pos rfl] + refine ⟨[], Or.inl ⟨?_, fun _ _ _ => rfl⟩⟩ + have run : fl4PerformInvocation (Z := Unit) blk call don (.invokeArchObject inv) = + fl4ArchPerformInvocation inv := by + cases blk <;> cases call <;> cases don <;> rfl + rw [run] + exact hperf + +set_option maxHeartbeats 4000000 in +/-- **The cache-maintenance group's `InvSpecA`** (module docstring). -/ +theorem FlScopeA.invSpecA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {lbl len slot : U64} {blk call don fp : Bool} {buf : U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} + {Out : State Unit → Prop} + (h : FlScopeA st AR Z OS ar s a σ lbl len slot blk call don fp buf extra Out) + {info : msginfo.MessageInfo} (hlI : lbl = info.label) + {r : core.result.Result Unit error.Exc} {s' : S} {a' : A} {σ' : State Unit} + (hd : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don fp buf extra = + ok (r, s', a')) (hOut : Out σ') + {done : Bool} (hdone : (done = true ∧ r = .Ok ()) ∨ (done = false ∧ r = .Err error.Exc.Preempted)) + {mi : MessageInfo} {sl : CslotPtr} {c : Cap} {excaps : List (Cap × CslotPtr)} {buffer : Option ObjRef} + {args : List Data} (F : FrontPinA Z.th σ slot mi sl c excaps buffer args) (hc : CapRelA (capA st s slot) c) + (B : ArgsLabA st s info buf mi args) (cptr : CapRef) : + InvSpecA call blk don done mi.miLabel args cptr sl c excaps buffer σ σ' := by + have hlab := B.1 + cases h with + | vsRoot K cte hab hcov ptr asid mp hcap _ _ _ hl blk call don fp extra hext args0 hlen hargs0 exs mi0 + c0 buffer0 hF => + obtain ⟨e1, e2, e3, e4, e5, e6⟩ := FrontPinA.unique J.j.1.place hF F + subst e1 e2 e3 e4 e5 e6 + have hf : isVSpaceFlushLabel (invocationType (specLabelWord (fl4LabelOf lbl.bv))) = true := by + rw [fl4LabelOf_bvA]; exact vsFlush_inNA hl + have hP := J.vsFlushA K cte hab hcov ptr asid mp hcap lbl len buf hl blk call don fp extra hext args0 hlen + hargs0 [] exs + rw [hcap] at hd + rw [hd, WP.spec_ok] at hP + have hc2 : CapEqA (.Pml4 ptr mp asid) c0 := by rw [← hcap]; exact hc.2 + refine FlushPostA.invSpecA (l := fl4LabelOf lbl.bv) (ac := .pageTableCap (specPtr ptr) .vsRootPtT + (mapDataA mp asid 0#u64)) (fun x => ?_) (fun inv h => ?_) hP hOut hdone + (show c0 = _ from (show absCapA (.Pml4 ptr mp asid) = c0 from hc2).symm) ?_ cptr + · rw [dec_vsFlushA call x cte exs (args0.map UScalar.bv) _ hf, dec_vsFlushA call [] cte exs (args0.map UScalar.bv) _ hf] + · exact dec_vsFlush_okA _ _ _ _ _ _ hf _ _ σ inv σ h + · rw [hlab, ← hlI] + | s2Root K hIpa cte hab hcov ptr asid va mp hcap _ _ _ blk call don fp extra hext args0 hlen hargs0 exs + mi0 c0 buffer0 hF => + obtain ⟨e1, e2, e3, e4, e5, e6⟩ := FrontPinA.unique J.j.1.place hF F + subst e1 e2 e3 e4 e5 e6 + have hP := J.s2FlushA K hIpa cte hab hcov ptr asid va mp hcap lbl len buf blk call don fp extra hext args0 + hlen hargs0 [] exs + rw [hcap] at hd + rw [hd, WP.spec_ok] at hP + have hc2 : CapEqA (.Ept ptr 0#u64 mp asid va) c0 := by rw [← hcap]; exact hc.2 + refine FlushPostA.invSpecA (l := fl4LabelOf lbl.bv) (ac := .ext (.stage2VSpaceCap (specPtr ptr) + (asidDataA mp asid))) (fun x => rfl) (fun inv h => ?_) hP hOut hdone + (show c0 = _ from (show absCapA (.Ept ptr 0#u64 mp asid va) = c0 from hc2).symm) ?_ cptr + · exact dec_s2Flush_okA _ _ _ _ _ _ _ _ σ inv σ h + · rw [hlab, ← hlI] + | frame K hIpa cte hab hcov fpa rt asid va sz dev mp e hcap lbl len buf hl blk call don fp extra hext args0 hlen + hargs0 x0 hx0 exs hex mi0 c0 buffer0 hF => + obtain ⟨e1, e2, e3, e4, e5, e6⟩ := FrontPinA.unique J.j.1.place hF F + subst e1 e2 e3 e4 e5 e6 + obtain ⟨hfl, -, hd1, ht1⟩ := labNA_frFlA lbl.val hl.1 hl.2 + have hf : isPageFlushLabel (invocationType (specLabelWord (fl4LabelOf lbl.bv))) = true := by + rw [fl4LabelOf_bvA, invType_specA]; exact hfl + rw [← fl4LabelOf_bvA] at hd1 ht1 + have hP := J.frFlushA K hIpa cte hab hcov fpa rt asid va sz dev mp e hcap lbl len buf hl blk call don fp + extra hext args0 hlen hargs0 x0 hx0 [] exs hex + rw [hcap] at hd + rw [hd, WP.spec_ok] at hP + have hc2 : CapEqA (.Frame fpa sz rt dev mp e asid va) c0 := by rw [← hcap]; exact hc.2 + refine FlushPostA.invSpecA (l := fl4LabelOf lbl.bv) (ac := (absFrameE e (specPtr fpa) (vmRightsOfA rt) + (frameSizeOfA sz) dev (mapDataA mp asid va))) (fun x => ?_) (fun inv h => ?_) hP hOut hdone + (show c0 = _ from (show absCapA (.Frame fpa sz rt dev mp e asid va) = c0 from hc2).symm) ?_ cptr + · cases e with + | true => + rw [show absFrameE true (specPtr fpa) (vmRightsOfA rt) (frameSizeOfA sz) dev (mapDataA mp asid va) = + .ext (.stage2FrameCap (specPtr fpa) (vmRightsOfA rt) (frameSizeOfA sz) dev (mapDataA mp asid va)) from rfl, + dec_frFlS2A call x cte exs (args0.map UScalar.bv) _ hf hd1 ht1, + dec_frFlS2A call [] cte exs (args0.map UScalar.bv) _ hf hd1 ht1] + | false => + show fl4ArchDecodeInvocation (Z := Unit) call (fl4LabelOf lbl.bv) (args0.map UScalar.bv) x cte + (.frameCap (specPtr fpa) (vmRightsOfA rt) (frameSizeOfA sz) dev (mapDataA mp asid va)) exs = + fl4ArchDecodeInvocation (Z := Unit) call (fl4LabelOf lbl.bv) (args0.map UScalar.bv) [] cte + (.frameCap (specPtr fpa) (vmRightsOfA rt) (frameSizeOfA sz) dev (mapDataA mp asid va)) exs + unfold fl4ArchDecodeInvocation + dsimp only + simp only [if_neg hd1, if_neg ht1, if_pos hf] + · exact dec_frFlush_okA _ _ _ _ _ _ hf hd1 ht1 _ _ _ _ _ _ σ inv σ h + · rw [hlab, ← hlI] + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3InvSpecL1IrqA.lean b/proofs/Fl4ProofsA64/P3/Stage3InvSpecL1IrqA.lean new file mode 100644 index 000000000..2ccf833fa --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3InvSpecL1IrqA.lean @@ -0,0 +1,167 @@ +/- +P3 (aarch64, A64-P3 Lift 1b, T3 dispatch): **the IRQ handler arms' `InvSpecA`** (`IRQAckIRQ`, `IRQClearIRQHandler`, +`IRQSetIRQHandler`; `IrqHandlerSpecA.invSpecA`). The IRQ control arms are in `P3/Stage3InvSpecL1IrqCtlA`. + +The spec decode of an IRQ handler cap is `irqHandler_specA` (exact branch selection by the generic label); the set +arm's extra cap is the first extra cap of the front end (`exCap0A`: `ExtraOKA` and the first extra slot). + +Named premises: those of the arms (`IrqScopeA.sound`). New module. +-/ +import Fl4ProofsA64.P3.Stage3InvSpecL1RpA +import Fl4ProofsA64.P3.Stage3DispIrqA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Vcpu Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +/-- **The first extra cap of the front end** at a non-empty first extra slot `x0`: the spec's first extra cap and slot +(the slot is `AbsSlotA`, at the address `x0`) and the cap's relation to FL4's. -/ +theorem exCap0A {th : TypedHeapA} {mem : U64 → U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} + {excaps : List (Cap × CslotPtr)} (hx : ExtraOKA th mem extra excaps) {x0 : U64} + (he : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) (hx0 : x0 ≠ 0#u64) : + ∃ cp sl0 erest, excaps = (cp, sl0) :: erest ∧ cslotAddrA sl0 = x0 ∧ AbsSlotA th sl0 ∧ + CapRelA (capAtA mem x0) cp := by + have ex0 : x0 = exAtA extra 0 := okinj (he.symm.trans (exAtA_idx0 extra)) + cases hcs : excaps with + | nil => exact absurd (ex0.trans (hx.2.2 0 (by simp [hcs]) (by omega))) hx0 + | cons y rest' => + obtain ⟨ps, hth, hsp, hxa, hrel⟩ := hx.2.1 0 (by simp [hcs]) + simp only [hcs, List.getElem_cons_zero] at hth hsp hxa hrel + refine ⟨y.1, y.2, rest', rfl, ?_, ⟨ps, hsp, Or.inl hth⟩, ?_⟩ + · rw [ex0]; exact hxa.symm + · rw [ex0]; exact hrel + +/-- **An IRQ handler arm's `InvSpecA`**, from the spec decoder's outcome and the invocation's perform. -/ +theorem IrqHArm.invSpecA {call blk don : Bool} {ii : IrqHandlerInvocation} {σ σ1 σ' : State Unit} {irq : Irq} + {mi : MessageInfo} {sl : CslotPtr} {c : Cap} {excaps : List (Cap × CslotPtr)} {buffer : Option ObjRef} + {args : List Data} {cptr : CapRef} (hc : c = .irqHandlerCap irq) + (hspec : (decodeIrqHandlerInvocation (Z := Unit) (specLabelWord (fl4LabelOf mi.miLabel)) irq excaps).run σ = + ({(.ok ii, σ)}, False)) + (hset : setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False)) + (hper : ((), σ') ∈ (fl4InvokeIrqHandler (Z := Unit) ii σ1).1) : + InvSpecA call blk don true mi.miLabel args cptr sl c excaps buffer σ σ' := by + subst hc + refine ⟨.invokeIRQHandler ii, σ1, ?_, hset, ?_, fun h => by first | cases h | simp_all⟩ + · have : fl4DecodeInvocation (Z := Unit) call false mi.miLabel args cptr sl (.archObjectCap .asidControlCap) + excaps buffer = _ := rfl + show (.ok (Fl4Invocation.invokeIRQHandler ii), σ) ∈ ((Fl4Spec.NonDetE.liftME Fl4Invocation.invokeIRQHandler + (decodeIrqHandlerInvocation (Z := Unit) (specLabelWord (fl4LabelOf mi.miLabel)) irq excaps)).run σ).1 + rw [run_liftME_okA hspec] + exact rfl + · unfold PerfSpecA + rw [if_pos rfl] + refine ⟨[], Or.inl ⟨?_, fun _ _ _ => rfl⟩⟩ + have run : fl4PerformInvocation (Z := Unit) blk call don (.invokeIRQHandler ii) = + (do Fl4Spec.NonDetE.liftE (fl4InvokeIrqHandler (Z := Unit) ii) + Fl4Spec.NonDetE.returnOk ([] : List Data) : PMonad Unit (List Data)) := by + cases blk <;> cases call <;> cases don <;> rfl + rw [run] + exact perform_thenNilA (liftE_memA hper) + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {ar : machine.Arch A} {s : S} {a : A} {σ : State Unit} {m : McState} + +set_option maxHeartbeats 4000000 in +/-- **The IRQ handler arms' `InvSpecA`** (module docstring). -/ +theorem IrqScopeA.handlerInvSpecA [PlainStore st] (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (hCnt : Sched.CounterStepA σ) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) + s a σ m) + (hfm : A64FrameMasks st s) + {lbl len slot : U64} {blk call don fp : Bool} {buf : U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} + {Out : State Unit → Prop} + (h : IrqScopeA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ lbl len slot blk call don fp buf + extra Out) + (hH : lbl = fl4_abi.label.IRQAckIRQ ∨ lbl = fl4_abi.label.IRQClearIRQHandler ∨ lbl = fl4_abi.label.IRQSetIRQHandler) + {info : msginfo.MessageInfo} (hlI : lbl = info.label) + {r : core.result.Result Unit error.Exc} {s' : S} {a' : A} {σ' : State Unit} + (hd : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don fp buf extra = + ok (r, s', a')) (hOut : Out σ') + {done : Bool} (hdone : (done = true ∧ r = .Ok ()) ∨ (done = false ∧ r = .Err error.Exc.Preempted)) + {mi : MessageInfo} {sl : CslotPtr} {c : Cap} {excaps : List (Cap × CslotPtr)} {buffer : Option ObjRef} + {args : List Data} (F : FrontPinA Z.th σ slot mi sl c excaps buffer args) (hc : CapRelA (capA st s slot) c) + (hx : ExtraOKA Z.th (L.mem s) extra excaps) (B : ArgsLabA st s info buf mi args) (cptr : CapRef) : + InvSpecA call blk don done mi.miLabel args cptr sl c excaps buffer σ σ' := by + have hdn : r = .Ok () → done = true := fun e => by + rcases hdone with ⟨h2, -⟩ | ⟨-, h2⟩ + · exact h2 + · rw [e] at h2; cases h2 + have hlab : mi.miLabel = lbl.bv := by rw [B.1, hlI] + cases h with + | ack hv _ irq hsl hcov hcap _ _ _ _ _ _ _ hext => + obtain ⟨⟨r0, s0, a0⟩, e, hr0, σ1, σ2, h1, h2, -, J2⟩ := spec_existsA (J.irqAckA hv hsl hcov irq extra hext) + rw [hcap, decodeIrqHandler_programA] at hd + have hr : r = r0 := (Prod.mk.inj (okinj (hd.symm.trans e))).1 + rw [hdn (hr.trans hr0)] + obtain ⟨σ1', e1, e2⟩ := hOut + have hc2 : CapEqA (.IrqHandler irq) c := by rw [← hcap]; exact hc.2 + have hgt : genInvocationType (specLabelWord (fl4LabelOf mi.miLabel)) = .irqAckIRQ := by + rw [hlab] + refine genLabel_specA ?_ + rw [fl4_abi.label.IRQAckIRQ]; rfl + refine IrqHArm.invSpecA (ii := .aCKIrq (BitVec.ofNat 13 irq.val)) (show c = _ from + (show absCapA (.IrqHandler irq) = c from hc2).symm) ?_ e1 e2 + refine (irqHandler_specA _).trans ?_ + rw [hgt] + | clear hCs h1 hmb hmax htu _ irq hsl hcov hcap _ _ _ _ _ _ _ hext hSC hvs hvc site => + obtain ⟨s2, a2, σ1, σ2, hdec, hset, hstep, hcur, hR⟩ := J.irqClearA (hCnt := hCnt) hA hM hPl hfm hCs h1 hmb hmax htu hsl hcov hcap + hext hSC hvs hvc site + have hr : r = .Ok () := (Prod.mk.inj (okinj (hd.symm.trans hdec))).1 + rw [hdn hr] + obtain ⟨σ1', e1, e2⟩ := hOut + have hc2 : CapEqA (.IrqHandler irq) c := by rw [← hcap]; exact hc.2 + have hgt : genInvocationType (specLabelWord (fl4LabelOf mi.miLabel)) = .irqClearIRQHandler := by + rw [hlab] + refine genLabel_specA ?_ + rw [fl4_abi.label.IRQClearIRQHandler]; rfl + refine IrqHArm.invSpecA (ii := .clearIRQHandler (BitVec.ofNat 13 irq.val)) (show c = _ from + (show absCapA (.IrqHandler irq) = c from hc2).symm) ?_ e1 e2 + refine (irqHandler_specA _).trans ?_ + rw [hgt] + | set hCs h1 hmb hmax htu _ irq hsl hcov hcap _ _ _ _ _ _ _ hext hSC hvs hvc site x0 pn bg recv hx0 hx1 hnc ss hAs + hss => + obtain ⟨ps, hCsS, hpn⟩ := J.toJStage3A.cnAbs_ofA (hSC x0 (extra_memA hx0) hx1) hAs hss + obtain ⟨s3, a3, σ1, σ3, hdec, hset, hstep, hcur, hR⟩ := J.irqSetA (hCnt := hCnt) hA hM hPl hfm hCs h1 hmb hmax htu hsl hcov hcap + hext hSC hvs hvc site hx0 hx1 hnc ss hCsS hss hpn + have hr : r = .Ok () := (Prod.mk.inj (okinj (hd.symm.trans hdec))).1 + rw [hdn hr] + obtain ⟨σ1', e1, e2⟩ := hOut + have hc2 : CapEqA (.IrqHandler irq) c := by rw [← hcap]; exact hc.2 + obtain ⟨cp, sl0, erest, hexc, hsl0, hab0, hrel0⟩ := exCap0A hx hx0 hx1 + have esl : sl0 = ss := cslotAddrA_inj J.j.1.place hab0 hAs (hsl0.trans hss.symm) + subst esl + have ecp : cp = absCapA (.Notification pn bg true recv) := by + have h3 : CapEqA (.Notification pn bg true recv) cp := by + rw [← hnc]; rw [capAtA_eq] at hrel0; exact hrel0.2 + exact (show absCapA (.Notification pn bg true recv) = cp from h3).symm + subst ecp + subst hexc + have hgt : genInvocationType (specLabelWord (fl4LabelOf mi.miLabel)) = .irqSetIRQHandler := by + rw [hlab] + refine genLabel_specA ?_ + rw [fl4_abi.label.IRQSetIRQHandler]; rfl + refine IrqHArm.invSpecA (ii := .setIRQHandler (BitVec.ofNat 13 irq.val) + (absCapA (.Notification pn bg true recv)) sl0) (show c = _ from + (show absCapA (.IrqHandler irq) = c from hc2).symm) ?_ e1 e2 + refine (irqHandler_specA _).trans ?_ + rw [hgt] + simp only [if_pos (show isNtfnCap (absCapA (.Notification pn bg true recv)) = true ∧ + AllowSend ∈ capRights (absCapA (.Notification pn bg true recv)) from ⟨rfl, rfl⟩)] + | _ => + exfalso + rcases hH with h | h | h <;> revert h <;> + simp only [fl4_abi.label.IRQIssueIRQHandler, fl4_abi.label.ARMIRQIssueIRQHandlerTrigger, + fl4_abi.label.ARMIRQIssueIRQHandlerTriggerCore, fl4_abi.label.ARMIRQIssueIRQHandlerMSI, + fl4_abi.label.IRQAckIRQ, fl4_abi.label.IRQClearIRQHandler, fl4_abi.label.IRQSetIRQHandler] <;> decide + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3InvSpecL1IrqTopA.lean b/proofs/Fl4ProofsA64/P3/Stage3InvSpecL1IrqTopA.lean new file mode 100644 index 000000000..fa80210c4 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3InvSpecL1IrqTopA.lean @@ -0,0 +1,319 @@ +/- +P3 (aarch64, A64-P3 Lift 1b, T3 dispatch): **the IRQ group's `InvSpecA`** (`IrqScopeA.invSpecA`: the handler arms of +`P3/Stage3InvSpecL1IrqA` and the four IRQ control issue arms), and **`DispScopeA.invSpecA`**: the assembly over every +dispatch group (`DispScope4A.invSpecA` for Plan B's four, the Lift 1 groups' for the five). + +Each issue arm: the front end's args are FL4's `syscall_arg`s (`ArgsLabA`), the first extra cap is the root CNode +(`JSyscallA.irqTargetA`), the spec's IRQ is `fl4IrqOf` of the id (`layout.irq.of.bvA`), and the spec decoder's checks +are FL4's (`IrqPlatA`: `max_spi`; the node count is the record's `domOK.nodes`). + +Named premises: those of the arms (`IrqScopeA.sound`), [P] `IrqPlatA`. New module. +-/ +import Fl4ProofsA64.P3.Stage3InvSpecL1CtlA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Vcpu Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {ar : machine.Arch A} {s : S} {a : A} {σ : State Unit} {m : McState} + +set_option maxHeartbeats 8000000 in +/-- **The IRQ group's `InvSpecA`** (module docstring). -/ +theorem IrqScopeA.invSpecA [PlainStore st] (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (hP : IrqPlatA st ar) + (hCnt : Sched.CounterStepA σ) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) + s a σ m) + (hfm : A64FrameMasks st s) + {lbl len slot : U64} {blk call don fp : Bool} {buf : U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} + {Out : State Unit → Prop} + (h : IrqScopeA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ lbl len slot blk call don fp buf + extra Out) + {info : msginfo.MessageInfo} (hlI : lbl = info.label) (hlnI : len = msgLenA info buf) + {r : core.result.Result Unit error.Exc} {s' : S} {a' : A} {σ' : State Unit} + (hd : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don fp buf extra = + ok (r, s', a')) (hOut : Out σ') + {done : Bool} (hdone : (done = true ∧ r = .Ok ()) ∨ (done = false ∧ r = .Err error.Exc.Preempted)) + {mi : MessageInfo} {sl : CslotPtr} {c : Cap} {excaps : List (Cap × CslotPtr)} {buffer : Option ObjRef} + {args : List Data} (F : FrontPinA Z.th σ slot mi sl c excaps buffer args) (hc : CapRelA (capA st s slot) c) + (hx : ExtraOKA Z.th (L.mem s) extra excaps) (B : ArgsLabA st s info buf mi args) (cptr : CapRef) : + InvSpecA call blk don done mi.miLabel args cptr sl c excaps buffer σ σ' := by + by_cases hH : lbl = fl4_abi.label.IRQAckIRQ ∨ lbl = fl4_abi.label.IRQClearIRQHandler ∨ + lbl = fl4_abi.label.IRQSetIRQHandler + · exact IrqScopeA.handlerInvSpecA (hCnt := hCnt) hA hM hPl J hfm h hH hlI hd hOut hdone F hc hx B cptr + have hdn : r = .Ok () → done = true := fun e => by + rcases hdone with ⟨h2, -⟩ | ⟨-, h2⟩ + · exact h2 + · rw [e] at h2; cases h2 + have hlab := B.1 + obtain ⟨-, argsU, hae, hlenA, hargs⟩ := B + subst hae + cases h with + | issuePlain hv hvs hvc site _ hsl hcov hcap _ _ _ _ _ _ _ hext hSC root id index depth max irq dest he hlen h0 + h1 h2 hm hi hnum hactive hslot hempty ss sd hAs hss hAd hsd => + obtain ⟨ps, hCs, -⟩ := J.toJStage3A.cnAbs_ofA hcov hAs hss + obtain ⟨sd', pd, d1, d2, d3, hdn0, hpdn⟩ := J.toJStage3A.irqDest_ofA hext he hslot hempty + have e : sd = sd' := cslotAddrA_inj J.j.1.place hAd ⟨pd, d1, Or.inl d2⟩ (hsd.trans d3.symm) + subst e + obtain ⟨s3, a3, σ1, σ3, hd', -, -, -, -⟩ := J.irqIssuePlainA (hCnt := hCnt) hA hM hPl hfm (irq := irq) + (fun a hw h => hv.mask_irq a hw irq false h) hsl hcov hcap hext hSC hvs hvc site (irqOf_ltA hM hm hi hnum) + (irqInactive_ofRunA hactive) ss sd hCs ⟨d1, d2⟩ hss hsd hdn0 hpdn + (irqIssuePlain_acceptA he hlen h0 h1 h2 hm hi hnum hactive hslot hempty) + have hr : r = .Ok () := (Prod.mk.inj (okinj (hd.symm.trans hd'))).1 + have hdt := hdn hr + subst hdt + obtain ⟨σ1', e1, e2⟩ := hOut + have hc2 : CapEqA .IrqControl c := by rw [← hcap]; exact hc.2 + have ec : c = .irqControlCap := (show absCapA .IrqControl = c from hc2).symm + subst ec + have hss' : ss = sl := cslotAddrA_inj J.j.1.place hAs F.habs (hss.trans F.hslot) + subst hss' + have h3 : 3 ≤ argsU.length := by + have h3' : (3#u64 : U64).val ≤ len.val := by scalar_tac + rw [hlnI, hlenA] at h3' + simpa using h3' + obtain ⟨y0, y1, y2, rest, rfl⟩ : ∃ y0 y1 y2 rest, argsU = y0 :: y1 :: y2 :: rest := by + match argsU, h3 with + | y0 :: y1 :: y2 :: rest, _ => exact ⟨y0, y1, y2, rest, rfl⟩ + have e0 : id = y0 := (okinj ((hargs 0#u64 y0 rfl).symm.trans h0)).symm + have e1' : index = y1 := (okinj ((hargs 1#u64 y1 rfl).symm.trans h1)).symm + have e2' : depth = y2 := (okinj ((hargs 2#u64 y2 rfl).symm.trans h2)).symm + subst e0 e1' e2' + obtain ⟨cp, sl0, erest, hexc, ht⟩ := J.irqTargetA hx hext he hslot hempty hactive (irqOf_ltA hM hm hi hnum) hAd hsd + subst hexc + have hmx : max.bv = fl4A64Platform.maxSpi := post_of_okA (P := fun m : U64 => m.bv = fl4A64Platform.maxSpi) (hP.maxSpi a) hm + have hmax : ¬ id.bv > fl4A64Platform.maxSpi := not_gt_bvA hmx hi + have hle : id.val ≤ 1019 := le1019_of hi (post_of_okA (P := fun m : U64 => m.val ≤ 1019) (hM a) hm) + have hirqbv : irq.bv = fl4IrqOf id.bv 0 := post_of_okA (P := fun i : U64 => i.bv = fl4IrqOf id.bv (0#u64).bv) (layout.irq.of.bvA 0#u64 hle) hnum + have hu := ucast_irqA hirqbv + have hlabB : mi.miLabel = (fl4_abi.label.IRQIssueIRQHandler).bv := by rw [hlab, ← hlI] + have hdecr := irqIssuePlain_specA (σ := σ) (src := ss) (dest := sd) (cap := cp) (id := id.bv) + (index := index.bv) (depth := depth.bv) (rest.map UScalar.bv) (erest.map fun p => (p.1, ss)) hmax + (by rw [hu]; exact ht) + rw [hu] at hdecr + refine IrqCtlArm.invSpecA (ii := .spec (.irqControl (BitVec.ofNat 13 irq.val) sd ss)) rfl ?_ e1 e2 + rw [dec_irqCtl_eqA, hlabB] + have hdecr' : (fl4DecodeInvocation (Z := Unit) false false (fl4_abi.label.IRQIssueIRQHandler).bv + (((id :: index :: depth :: rest).map UScalar.bv)) [] ss .irqControlCap + (((cp, sl0) :: erest).map fun p => (p.1, ss)) none).run σ = + ({(.ok (Fl4Invocation.invokeIRQControl (.spec (.irqControl (BitVec.ofNat 13 irq.val) sd ss))), σ)}, False) := + hdecr + rw [hdecr'] + exact rfl + | issueTrigger hv hK hvs hvc site _ hsl hcov hcap _ _ _ _ _ _ _ hext hSC root id trigger index depth max irq dest + he hlen h0 h1 h2 h3 hm hi hspi hnum hactive hslot hempty ss sd hAs hss hAd hsd => + obtain ⟨ps, hCs, -⟩ := J.toJStage3A.cnAbs_ofA hcov hAs hss + obtain ⟨sd', pd, d1, d2, d3, hdn0, hpdn⟩ := J.toJStage3A.irqDest_ofA hext he hslot hempty + have e : sd = sd' := cslotAddrA_inj J.j.1.place hAd ⟨pd, d1, Or.inl d2⟩ (hsd.trans d3.symm) + subst e + obtain ⟨a1, hcfg, -⟩ := spec_existsA (hA.configure_irq a irq (trigger != 0#u64) 0#u64) + obtain ⟨s3, a3, σ1, σ3, hd', -, -, -, -⟩ := J.irqIssueRoutedA (hCnt := hCnt) hA hM hPl hfm (irq := irq) + (fun a hw h => hv.mask_irq a hw irq false h) hsl hcov hcap hext hSC hvs hvc site (irqOf_ltA hM hm hi hnum) + (irqInactive_ofRunA hactive) ss sd hCs ⟨d1, d2⟩ hss hsd hdn0 hpdn hK hcfg + (irqTrigger_acceptA he hlen h0 h1 h2 h3 hm hi hspi hnum hactive hslot hempty) + have hr : r = .Ok () := (Prod.mk.inj (okinj (hd.symm.trans hd'))).1 + have hdt := hdn hr + subst hdt + obtain ⟨σ1', e1, e2⟩ := hOut + have hc2 : CapEqA .IrqControl c := by rw [← hcap]; exact hc.2 + have ec : c = .irqControlCap := (show absCapA .IrqControl = c from hc2).symm + subst ec + have hss' : ss = sl := cslotAddrA_inj J.j.1.place hAs F.habs (hss.trans F.hslot) + subst hss' + have h4 : 4 ≤ argsU.length := by + have h4' : (4#u64 : U64).val ≤ len.val := by scalar_tac + rw [hlnI, hlenA] at h4' + simpa using h4' + obtain ⟨y0, y1, y2, y3, rest, rfl⟩ : ∃ y0 y1 y2 y3 rest, argsU = y0 :: y1 :: y2 :: y3 :: rest := by + match argsU, h4 with + | y0 :: y1 :: y2 :: y3 :: rest, _ => exact ⟨y0, y1, y2, y3, rest, rfl⟩ + have e0 : id = y0 := (okinj ((hargs 0#u64 y0 rfl).symm.trans h0)).symm + have e1' : trigger = y1 := (okinj ((hargs 1#u64 y1 rfl).symm.trans h1)).symm + have e2' : index = y2 := (okinj ((hargs 2#u64 y2 rfl).symm.trans h2)).symm + have e3' : depth = y3 := (okinj ((hargs 3#u64 y3 rfl).symm.trans h3)).symm + subst e0 e1' e2' e3' + obtain ⟨cp, sl0, erest, hexc, ht⟩ := J.irqTargetA hx hext he hslot hempty hactive (irqOf_ltA hM hm hi hnum) hAd hsd + subst hexc + have hmx : max.bv = fl4A64Platform.maxSpi := post_of_okA (P := fun m : U64 => m.bv = fl4A64Platform.maxSpi) (hP.maxSpi a) hm + have hmax : ¬ id.bv > fl4A64Platform.maxSpi := not_gt_bvA hmx hi + have hspi' : ¬ id.bv < 32 := by + intro hh + apply hspi + show id < layout.irq.SPI_MIN + rw [UScalar.lt_equiv] + have := BitVec.lt_def.1 hh + simp only [layout.irq.SPI_MIN] + have e32 : (32 : Word).toNat = 32 := rfl + have : id.bv.toNat = id.val := rfl + scalar_tac + have hle : id.val ≤ 1019 := le1019_of hi (post_of_okA (P := fun m : U64 => m.val ≤ 1019) (hM a) hm) + have hirqbv : irq.bv = fl4IrqOf id.bv 0 := post_of_okA (P := fun i : U64 => i.bv = fl4IrqOf id.bv (0#u64).bv) (layout.irq.of.bvA 0#u64 hle) hnum + have hu := ucast_irqA hirqbv + have hlabB : mi.miLabel = (fl4_abi.label.ARMIRQIssueIRQHandlerTrigger).bv := by rw [hlab, ← hlI] + have hdecr := irqTrigger_specA (σ := σ) (src := ss) (dest := sd) (cap := cp) (id := id.bv) + (trigger := trigger.bv) (index := index.bv) (depth := depth.bv) (rest.map UScalar.bv) + (erest.map fun p => (p.1, ss)) hmax hspi' (by rw [hu]; exact ht) + rw [hu] at hdecr + refine IrqCtlArm.invSpecA (ii := .triggerCore (BitVec.ofNat 13 irq.val) sd ss (trigger != 0#u64) + (0#u64 : U64).bv) rfl ?_ e1 e2 + rw [dec_irqCtl_eqA, hlabB, bne_decideA] + have hdecr' : (fl4DecodeInvocation (Z := Unit) false false (fl4_abi.label.ARMIRQIssueIRQHandlerTrigger).bv + (((id :: trigger :: index :: depth :: rest).map UScalar.bv)) [] ss .irqControlCap + (((cp, sl0) :: erest).map fun p => (p.1, ss)) none).run σ = + ({(.ok (Fl4Invocation.invokeIRQControl (.triggerCore (BitVec.ofNat 13 irq.val) sd ss + (decide (trigger.bv ≠ (0 : Word))) (0#u64 : U64).bv)), σ)}, False) := hdecr + rw [hdecr'] + exact rfl + | issueTriggerCore hv hK hvs hvc site _ hsl hcov hcap _ _ _ _ _ _ _ hext hSC root id trigger index depth cpu max + nodes irq dest he hlen h0 h1 h2 h3 h4 hm hi hnodes hcpu hnum hactive hslot hempty ss sd hAs hss hAd hsd => + obtain ⟨ps, hCs, -⟩ := J.toJStage3A.cnAbs_ofA hcov hAs hss + obtain ⟨sd', pd, d1, d2, d3, hdn0, hpdn⟩ := J.toJStage3A.irqDest_ofA hext he hslot hempty + have e : sd = sd' := cslotAddrA_inj J.j.1.place hAd ⟨pd, d1, Or.inl d2⟩ (hsd.trans d3.symm) + subst e + obtain ⟨a1, hcfg, -⟩ := spec_existsA (hA.configure_irq a irq (trigger != 0#u64) cpu) + obtain ⟨s3, a3, σ1, σ3, hd', -, -, -, -⟩ := J.irqIssueRoutedA (hCnt := hCnt) hA hM hPl hfm (irq := irq) + (fun a hw h => hv.mask_irq a hw irq false h) hsl hcov hcap hext hSC hvs hvc site (irqOf_ltA hM hm hi hnum) + (irqInactive_ofRunA hactive) ss sd hCs ⟨d1, d2⟩ hss hsd hdn0 hpdn hK hcfg + (irqTriggerCore_acceptA he hlen h0 h1 h2 h3 h4 hm hi hnodes hcpu hnum hactive hslot hempty) + have hr : r = .Ok () := (Prod.mk.inj (okinj (hd.symm.trans hd'))).1 + have hdt := hdn hr + subst hdt + obtain ⟨σ1', e1, e2⟩ := hOut + have hc2 : CapEqA .IrqControl c := by rw [← hcap]; exact hc.2 + have ec : c = .irqControlCap := (show absCapA .IrqControl = c from hc2).symm + subst ec + have hss' : ss = sl := cslotAddrA_inj J.j.1.place hAs F.habs (hss.trans F.hslot) + subst hss' + have h5 : 5 ≤ argsU.length := by + have h5' : (5#u64 : U64).val ≤ len.val := by scalar_tac + rw [hlnI, hlenA] at h5' + simpa using h5' + obtain ⟨y0, y1, y2, y3, y4, rest, rfl⟩ : ∃ y0 y1 y2 y3 y4 rest, + argsU = y0 :: y1 :: y2 :: y3 :: y4 :: rest := by + match argsU, h5 with + | y0 :: y1 :: y2 :: y3 :: y4 :: rest, _ => exact ⟨y0, y1, y2, y3, y4, rest, rfl⟩ + have e0 : id = y0 := (okinj ((hargs 0#u64 y0 rfl).symm.trans h0)).symm + have e1' : trigger = y1 := (okinj ((hargs 1#u64 y1 rfl).symm.trans h1)).symm + have e2' : index = y2 := (okinj ((hargs 2#u64 y2 rfl).symm.trans h2)).symm + have e3' : depth = y3 := (okinj ((hargs 3#u64 y3 rfl).symm.trans h3)).symm + have e4' : cpu = y4 := (okinj ((hargs 4#u64 y4 rfl).symm.trans h4)).symm + subst e0 e1' e2' e3' e4' + obtain ⟨cp, sl0, erest, hexc, ht⟩ := J.irqTargetA hx hext he hslot hempty hactive (irqOf_ltA hM hm hi hnum) hAd hsd + subst hexc + have hmx : max.bv = fl4A64Platform.maxSpi := post_of_okA (P := fun m : U64 => m.bv = fl4A64Platform.maxSpi) (hP.maxSpi a) hm + have hmax : ¬ id.bv > fl4A64Platform.maxSpi := not_gt_bvA hmx hi + have hnn := J.toJStage3A.domOK.nodesGet hnodes + have hcpu' : ¬ cpu.bv.toNat ≥ fl4A64Platform.numNodes := by + intro hh + apply hcpu + show nodes ≤ cpu + rw [UScalar.le_equiv] + have : cpu.bv.toNat = cpu.val := rfl + omega + have hle : id.val ≤ 1019 := le1019_of hi (post_of_okA (P := fun m : U64 => m.val ≤ 1019) (hM a) hm) + have hirqbv : irq.bv = fl4IrqOf id.bv cpu.bv := post_of_okA (P := fun i : U64 => i.bv = fl4IrqOf id.bv cpu.bv) (layout.irq.of.bvA cpu hle) hnum + have hu := ucast_irqA hirqbv + have hlabB : mi.miLabel = (fl4_abi.label.ARMIRQIssueIRQHandlerTriggerCore).bv := by rw [hlab, ← hlI] + have hdecr := irqTriggerCore_specA (σ := σ) (src := ss) (dest := sd) (cap := cp) (id := id.bv) + (trigger := trigger.bv) (index := index.bv) (depth := depth.bv) (cpu := cpu.bv) (rest.map UScalar.bv) + (erest.map fun p => (p.1, ss)) hmax hcpu' (by rw [hu]; exact ht) + rw [hu] at hdecr + refine IrqCtlArm.invSpecA (ii := .triggerCore (BitVec.ofNat 13 irq.val) sd ss (trigger != 0#u64) cpu.bv) + rfl ?_ e1 e2 + rw [dec_irqCtl_eqA, hlabB, bne_decideA] + have hdecr' : (fl4DecodeInvocation (Z := Unit) false false (fl4_abi.label.ARMIRQIssueIRQHandlerTriggerCore).bv + (((id :: trigger :: index :: depth :: cpu :: rest).map UScalar.bv)) [] ss .irqControlCap + (((cp, sl0) :: erest).map fun p => (p.1, ss)) none).run σ = + ({(.ok (Fl4Invocation.invokeIRQControl (.triggerCore (BitVec.ofNat 13 irq.val) sd ss + (decide (trigger.bv ≠ (0 : Word))) cpu.bv)), σ)}, False) := hdecr + rw [hdecr'] + exact rfl + | issueMsi hv hK hvs hvc site _ hsl hcov hcap _ _ _ _ _ _ _ hext hSC root nid device event index d cpu nodes base + irq dest a1 he hlen h0 h1 h2 h3 h4 h5 hn hnodes hcpu hbase hirq hactive hslot hempty hmsi ss sd hAs hss hAd hsd => + obtain ⟨ps, hCs, -⟩ := J.toJStage3A.cnAbs_ofA hcov hAs hss + obtain ⟨sd', pd, d1, d2, d3, hdn0, hpdn⟩ := J.toJStage3A.irqDest_ofA hext he hslot hempty + have e : sd = sd' := cslotAddrA_inj J.j.1.place hAd ⟨pd, d1, Or.inl d2⟩ (hsd.trans d3.symm) + subst e + obtain ⟨s3, a3, σ1, σ3, hd', -, -, -, -⟩ := J.irqIssueMsiA (hCnt := hCnt) hA hM hPl hfm (irq := irq) + (fun a hw h => hv.mask_irq a hw irq false h) hsl hcov hcap hext hSC hvs hvc site (irqOfLpi_ltA hn hbase hirq) + (irqInactive_ofRunA hactive) ss sd hCs ⟨d1, d2⟩ hss hsd hdn0 hpdn hK hmsi + (irqMsi_acceptA he hlen h0 h1 h2 h3 h4 h5 hn hnodes hcpu hbase hirq hactive hslot hempty hmsi) + have hr : r = .Ok () := (Prod.mk.inj (okinj (hd.symm.trans hd'))).1 + have hdt := hdn hr + subst hdt + obtain ⟨σ1', e1, e2⟩ := hOut + have hc2 : CapEqA .IrqControl c := by rw [← hcap]; exact hc.2 + have ec : c = .irqControlCap := (show absCapA .IrqControl = c from hc2).symm + subst ec + have hss' : ss = sl := cslotAddrA_inj J.j.1.place hAs F.habs (hss.trans F.hslot) + subst hss' + have h6 : 6 ≤ argsU.length := by + have h6' : (6#u64 : U64).val ≤ len.val := by scalar_tac + rw [hlnI, hlenA] at h6' + simpa using h6' + obtain ⟨y0, y1, y2, y3, y4, y5, rest, rfl⟩ : ∃ y0 y1 y2 y3 y4 y5 rest, + argsU = y0 :: y1 :: y2 :: y3 :: y4 :: y5 :: rest := by + match argsU, h6 with + | y0 :: y1 :: y2 :: y3 :: y4 :: y5 :: rest, _ => exact ⟨y0, y1, y2, y3, y4, y5, rest, rfl⟩ + have e0 : nid = y0 := (okinj ((hargs 0#u64 y0 rfl).symm.trans h0)).symm + have e1' : device = y1 := (okinj ((hargs 1#u64 y1 rfl).symm.trans h1)).symm + have e2' : event = y2 := (okinj ((hargs 2#u64 y2 rfl).symm.trans h2)).symm + have e3' : index = y3 := (okinj ((hargs 3#u64 y3 rfl).symm.trans h3)).symm + have e4' : d = y4 := (okinj ((hargs 4#u64 y4 rfl).symm.trans h4)).symm + have e5' : cpu = y5 := (okinj ((hargs 5#u64 y5 rfl).symm.trans h5)).symm + subst e0 e1' e2' e3' e4' e5' + obtain ⟨cp, sl0, erest, hexc, ht⟩ := J.irqTargetA hx hext he hslot hempty hactive (irqOfLpi_ltA hn hbase hirq) + hAd hsd + subst hexc + have hn' : ¬ nid.bv ≥ fl4NumLpis := not_ge_bvA (by simp only [layout.irq.NUM_LPIS]; rfl) hn + have hnn := J.toJStage3A.domOK.nodesGet hnodes + have hcpu' : ¬ cpu.bv.toNat ≥ fl4A64Platform.numNodes := by + intro hh + apply hcpu + show nodes ≤ cpu + rw [UScalar.le_equiv] + have : cpu.bv.toNat = cpu.val := rfl + omega + have eb : base = 5120#u64 := post_of_okA (P := fun v : U64 => v = 5120#u64) + Fl4ProofsA64.P0.Consts.layout.irq.LPI_BASE.spec hbase + have hn256 : nid.val < 256 := by + simp only [layout.irq.NUM_LPIS, ge_iff_le, not_le] at hn; scalar_tac + subst eb + have hv5 := post_of_okA (P := fun v : U64 => v.val = (5120#u64 : U64).val + nid.val) + (U64.add_spec (x := 5120#u64) (y := nid) (by scalar_tac)) hirq + have hirqbv : irq.bv = fl4LpiBase + nid.bv := by + apply BitVec.eq_of_toNat_eq + have e1 : irq.bv.toNat = irq.val := rfl + have e2 : (fl4LpiBase + nid.bv).toNat = (fl4LpiBase.toNat + nid.bv.toNat) % 2 ^ 64 := BitVec.toNat_add _ _ + have e3 : fl4LpiBase.toNat = 5120 := rfl + have e4 : nid.bv.toNat = nid.val := rfl + have e5 : (5120#u64 : U64).val = 5120 := rfl + omega + have hu := ucast_irqA hirqbv + have hlabB : mi.miLabel = (fl4_abi.label.ARMIRQIssueIRQHandlerMSI).bv := by rw [hlab, ← hlI] + have hlabL : fl4LabelOf (fl4_abi.label.ARMIRQIssueIRQHandlerMSI).bv = .ext .armIRQIssueIRQHandlerMSI := by + rw [fl4_abi.label.ARMIRQIssueIRQHandlerMSI]; rfl + have hmsiMem := decMsi_memA (σ := σ) (src := ss) (dest := sd) (cap := cp) (n := nid.bv) (device := device.bv) + (event := event.bv) (index := index.bv) (d := d.bv) (cpu := cpu.bv) (rest.map UScalar.bv) + (erest.map Prod.fst) hn' hcpu' (by rw [hu]; exact ht) + rw [hu] at hmsiMem + refine IrqCtlArm.invSpecA (ii := .msi (BitVec.ofNat 13 irq.val) sd ss device.bv event.bv cpu.bv) rfl ?_ e1 e2 + rw [hlabB] + show (.ok (Fl4Invocation.invokeIRQControl _), σ) ∈ ((Fl4Spec.NonDetE.liftME Fl4Invocation.invokeIRQControl + (decodeFl4IrqControl (fl4LabelOf (fl4_abi.label.ARMIRQIssueIRQHandlerMSI).bv) _ ss _)).run σ).1 + rw [hlabL] + exact mem_liftME_okPinA hmsiMem + | _ => exact absurd (by first | exact Or.inl rfl | exact Or.inr (Or.inl rfl) | exact Or.inr (Or.inr rfl)) hH + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3InvSpecL1RpA.lean b/proofs/Fl4ProofsA64/P3/Stage3InvSpecL1RpA.lean new file mode 100644 index 000000000..d437ef3fa --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3InvSpecL1RpA.lean @@ -0,0 +1,204 @@ +/- +P3 (aarch64, A64-P3 Lift 1b, T3 dispatch): **the one-word reply group's `InvSpecA`** (`ReplyScopeA.invSpecA`). + +FL4 replies inside these two arms' decode (`ARMPageGetAddress`, `ARMPageTestAndClearAccessed`) and resumes the caller; +the spec's perform returns the one word and leaves the caller `Restart`, its own epilogue (`epiSpecA`) replying. So +the record's spec state is the state *after* that epilogue: the second disjunct of `PerfSpecA` (the perform ends at +`σ2`, the caller is `Restart` there, and `epiSpecA` reaches `σ'`). Both performs leave the state unchanged +(`ReplyArm.invSpecA`'s `hperfSt`). The spec decode does not read the `CapRef` argument of a frame's +`TestAndClearAccessed` (`tcDec_xslotA`). + +Named premises: those of the arms (`ReplyScopeA.sound`). New module. +-/ +import Fl4ProofsA64.P3.Stage3InvSpecL1FlA +import Fl4ProofsA64.P3.Stage3DispReplyA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +/-- A lifted state-keeping computation keeps the state. -/ +theorem liftE_stateA {α : Type} {m : Fl4Spec.NonDet (State Unit) α} (hm : ∀ σ x σ', (x, σ') ∈ (m σ).1 → σ' = σ) + {σ σ' : State Unit} {a : α} + (h : (.ok a, σ') ∈ ((Fl4Spec.NonDetE.liftE (ε := Unit) m : PMonad Unit α).run σ).1) : σ' = σ := by + unfold Fl4Spec.NonDetE.liftE at h + rw [ExceptT.run_lift] at h + obtain ⟨y, hy, hyy⟩ := mem_bindA.1 h + have e : (Except.ok a, σ') = (Except.ok y.1, y.2) := hyy + rw [(Prod.mk.inj e).2] + exact hm σ y.1 y.2 hy + +/-- `ARMPageGetAddress`'s perform keeps the state. -/ +theorem perfSt_getAddrA (p : ObjRef) (σ1 σ2 : State Unit) (rv : List Data) + (h : (.ok rv, σ2) ∈ ((fl4ArchPerformInvocation (Z := Unit) (.spec (.invokePage (.pageGetAddr p)))).run σ1).1) : + σ2 = σ1 := + liftE_stateA (m := performPgInvGetAddr (Z := Unit) p) (fun σ x σ' h => congrArg Prod.snd h) h + +/-- `ARMPageTestAndClearAccessed`'s perform keeps the state. -/ +theorem perfSt_testClearA (p : ObjRef) (σ1 σ2 : State Unit) (rv : List Data) + (h : (.ok rv, σ2) ∈ ((fl4ArchPerformInvocation (Z := Unit) (.testAndClearAccessed p)).run σ1).1) : + σ2 = σ1 := + liftE_stateA (m := performTestAndClearAccessed (Z := Unit) p) (fun σ x σ' h => h.2) h + +/-- The spec's `TestAndClearAccessed` decode does not read the `CapRef` argument. -/ +theorem tcDec_xslotA (calling : Bool) (args : List Data) (x y : CapRef) (cte : CslotPtr) (e : Bool) (p : ObjRef) + (R : CapRights) (sz : VMPageSize) (dev : Bool) (md : Option (Asid × VSpaceRef)) (exs : List (Cap × CslotPtr)) : + fl4ArchDecodeInvocation (Z := Unit) calling (.ext .armPageTestAndClearAccessed) args x cte + (absFrameE e p R sz dev md) exs = + fl4ArchDecodeInvocation (Z := Unit) calling (.ext .armPageTestAndClearAccessed) args y cte + (absFrameE e p R sz dev md) exs := by + have h1 : ¬ ((Fl4Label.ext .armPageTestAndClearAccessed) = Fl4Label.ext .armPageDeriveSubframe) := fun h => by + cases h + cases e + · show fl4ArchDecodeInvocation (Z := Unit) calling (.ext .armPageTestAndClearAccessed) args x cte + (.frameCap p R sz dev md) exs = fl4ArchDecodeInvocation (Z := Unit) calling + (.ext .armPageTestAndClearAccessed) args y cte (.frameCap p R sz dev md) exs + unfold fl4ArchDecodeInvocation + dsimp only + simp only [if_neg h1, eq_self_iff_true, if_true] + · show fl4ArchDecodeInvocation (Z := Unit) calling (.ext .armPageTestAndClearAccessed) args x cte + (.ext (.stage2FrameCap p R sz dev md)) exs = fl4ArchDecodeInvocation (Z := Unit) calling + (.ext .armPageTestAndClearAccessed) args y cte (.ext (.stage2FrameCap p R sz dev md)) exs + unfold fl4ArchDecodeInvocation + dsimp only + simp only [if_neg h1, eq_self_iff_true, if_true] + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {ar : machine.Arch A} {s : S} {a : A} {σ : State Unit} {m : McState} + +/-- **One reply arm's `InvSpecA`**, from its spec outcome `ReplySpecA`. -/ +theorem ReplyArm.invSpecA {call blk don : Bool} {inv : Fl4ArchInvocation} {σ σ' : State Unit} + (hperfSt : ∀ (σ1 σ2 : State Unit) (rv : List Data), + (.ok rv, σ2) ∈ ((fl4ArchPerformInvocation (Z := Unit) inv).run σ1).1 → σ2 = σ1) + (hset : ∀ x, σ.kheap σ.curThread = some (.tcb x) → + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), stUpdA σ σ.curThread x .restart)}, False)) + (rep : ReplySpecA σ call inv σ') {mi : MessageInfo} {sl : CslotPtr} {c : Cap} {excaps : List (Cap × CslotPtr)} + {buffer : Option ObjRef} {args : List Data} {cptr : CapRef} + (hdec : (.ok (Fl4Invocation.invokeArchObject inv), σ) ∈ + ((fl4DecodeInvocation (Z := Unit) call false mi.miLabel args cptr sl c excaps buffer).run σ).1) : + InvSpecA call blk don true mi.miLabel args cptr sl c excaps buffer σ σ' := by + obtain ⟨x, σ2, w, hx, hp, hr⟩ := rep + refine ⟨.invokeArchObject inv, stUpdA σ σ.curThread x .restart, hdec, hset x hx, ?_, fun h => by first | cases h | simp_all⟩ + unfold PerfSpecA + rw [if_pos rfl] + refine ⟨[w], Or.inr ⟨σ2, ?_, ?_⟩⟩ + · have run : fl4PerformInvocation (Z := Unit) blk call don (.invokeArchObject inv) = + fl4ArchPerformInvocation inv := by + cases blk <;> cases call <;> cases don <;> rfl + rw [run] + exact hp + · have e2 := hperfSt _ _ _ hp + have hk : σ2.kheap σ.curThread = some (.tcb { x with tcbState := .restart }) := by + rw [e2]; simp [stUpdA, setObjA] + have getSt : getThreadState (Z := Unit) σ.curThread σ2 = ({(.restart, σ2)}, False) := + threadGet_eqA Tcb.tcbState hk + unfold epiSpecA + rw [nd_bind_oneA getSt] + exact hr + +set_option maxHeartbeats 4000000 in +/-- **The one-word reply group's `InvSpecA`** (module docstring). -/ +theorem ReplyScopeA.invSpecA (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) + s a σ m) + (hfm : A64FrameMasks st s) + {lbl len slot : U64} {blk call don fp : Bool} {buf : U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} + {Out : State Unit → Prop} + (h : ReplyScopeA st AR Z OS ar s σ lbl len slot blk call don fp buf extra Out) + {info : msginfo.MessageInfo} (hlI : lbl = info.label) + {r : core.result.Result Unit error.Exc} {s' : S} {a' : A} {σ' : State Unit} + (hd : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don fp buf extra = + ok (r, s', a')) (hOut : Out σ') + {done : Bool} (hdone : (done = true ∧ r = .Ok ()) ∨ (done = false ∧ r = .Err error.Exc.Preempted)) + {mi : MessageInfo} {sl : CslotPtr} {c : Cap} {excaps : List (Cap × CslotPtr)} {buffer : Option ObjRef} + {args : List Data} (F : FrontPinA Z.th σ slot mi sl c excaps buffer args) (hc : CapRelA (capA st s slot) c) + (B : ArgsLabA st s info buf mi args) (cptr : CapRef) : + InvSpecA call blk don done mi.miLabel args cptr sl c excaps buffer σ σ' := by + have hst := J.setRestartA + cases h with + | getAddress cte hab hcov fpa rt asid va sz dev mp e hcap _ _ _ _ _ _ _ hext hSC => + obtain ⟨⟨r0, s0, a0⟩, eq, hD, hr1, hrest⟩ := spec_existsA + (J.getAddrA hA hM hPl hfm cte hab hcov fpa rt asid va sz dev mp e hcap len buf blk call don fp extra hext hSC + args cptr excaps) + rw [hcap] at hd + have hr : r = r0 := (Prod.mk.inj (okinj (hd.symm.trans eq))).1 + replace hr1 : r0 = .Ok () := hr1 + have hdn : done = true := by + rcases hdone with ⟨h2, -⟩ | ⟨-, h2⟩ + · exact h2 + · rw [hr, hr1] at h2; cases h2 + subst hdn + have hcs : cte = sl := cslotAddrA_inj J.j.1.place hab F.habs F.hslot + subst hcs + have hc2 : CapEqA (.Frame fpa sz rt dev mp e asid va) c := by rw [← hcap]; exact hc.2 + have ec : c = .archObjectCap (absFrameE e (specPtr fpa) (vmRightsOfA rt) (frameSizeOfA sz) dev + (mapDataA mp asid va)) := (show absCapA (.Frame fpa sz rt dev mp e asid va) = c from hc2).symm + have hl : fl4LabelOf mi.miLabel = archL .armPageGetAddress := by + rw [B.1, ← hlI, fl4_abi.label.ARMPageGetAddress] + rfl + refine ReplyArm.invSpecA (inv := .spec (.invokePage (.pageGetAddr (specPtr fpa)))) (perfSt_getAddrA _) hst hOut ?_ + rw [ec] + have dec : fl4DecodeInvocation (Z := Unit) call false mi.miLabel args cptr cte + (.archObjectCap (absFrameE e (specPtr fpa) (vmRightsOfA rt) (frameSizeOfA sz) dev (mapDataA mp asid va))) + excaps buffer = Fl4Spec.NonDetE.liftME Fl4Invocation.invokeArchObject + (fl4ArchDecodeInvocation call (fl4LabelOf mi.miLabel) args cptr cte + (absFrameE e (specPtr fpa) (vmRightsOfA rt) (frameSizeOfA sz) dev (mapDataA mp asid va)) excaps) := rfl + rw [dec, hl] + exact mem_liftME_okPinA (by rw [hD]; exact rfl) + | testClear K cte hab hcov fpa rt asid va sz dev mp e hcap _ _ _ _ _ _ _ hext hSC args0 exs mi0 c0 buffer0 hF => + obtain ⟨e1, e2, e3, e4, e5, e6⟩ := FrontPinA.unique J.j.1.place hF F + subst e1 e2 e3 e4 e5 e6 + obtain ⟨⟨r0, s0, a0⟩, eq, hpost⟩ := spec_existsA + (J.testClearA hA hM hPl hfm K cte hab hcov fpa rt asid va sz dev mp e hcap len buf blk call don fp extra hext + hSC args0 [] exs) + rw [hcap] at hd + have hr : r = r0 := (Prod.mk.inj (okinj (hd.symm.trans eq))).1 + have hc2 : CapEqA (.Frame fpa sz rt dev mp e asid va) c0 := by rw [← hcap]; exact hc.2 + have ec : c0 = .archObjectCap (absFrameE e (specPtr fpa) (vmRightsOfA rt) (frameSizeOfA sz) dev + (mapDataA mp asid va)) := (show absCapA (.Frame fpa sz rt dev mp e asid va) = c0 from hc2).symm + have hl : fl4LabelOf mi0.miLabel = .ext .armPageTestAndClearAccessed := by + rw [B.1, ← hlI, fl4_abi.label.ARMPageTestAndClearAccessed] + rfl + rcases hOut with ⟨-, e', hDe⟩ | ⟨hD, rep⟩ + · exfalso + rcases hpost with ⟨-, e'', hDe2, ex, hrx, hrel⟩ | ⟨hD2, -⟩ + · replace hrx : r0 = .Err ex := hrx + rcases hdone with ⟨-, h2⟩ | ⟨-, h2⟩ + · rw [hr, hrx] at h2; cases h2 + · rw [hr, hrx] at h2 + exact hrel.ne_preemptedA (core.result.Result.Err.inj h2) + · have := single_fstA (hDe.symm.trans hD2) + cases this + · rcases hpost with ⟨-, e'', hDe2, -⟩ | ⟨hD2, hr1, -⟩ + · exfalso + have := single_fstA (hDe2.symm.trans hD) + cases this + · replace hr1 : r0 = .Ok () := hr1 + have hdn : done = true := by + rcases hdone with ⟨h2, -⟩ | ⟨-, h2⟩ + · exact h2 + · rw [hr, hr1] at h2; cases h2 + subst hdn + refine ReplyArm.invSpecA (inv := .testAndClearAccessed (specPtr fpa)) (perfSt_testClearA _) hst rep ?_ + rw [ec] + have dec : fl4DecodeInvocation (Z := Unit) call false mi0.miLabel (args0.map UScalar.bv) cptr cte + (.archObjectCap (absFrameE e (specPtr fpa) (vmRightsOfA rt) (frameSizeOfA sz) dev (mapDataA mp asid va))) + exs buffer0 = Fl4Spec.NonDetE.liftME Fl4Invocation.invokeArchObject + (fl4ArchDecodeInvocation call (fl4LabelOf mi0.miLabel) (args0.map UScalar.bv) cptr cte + (absFrameE e (specPtr fpa) (vmRightsOfA rt) (frameSizeOfA sz) dev (mapDataA mp asid va)) exs) := rfl + rw [dec, hl, tcDec_xslotA call _ cptr []] + refine mem_liftME_okPinA ?_ + rw [hD] + exact rfl + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3InvSpecL1TopA.lean b/proofs/Fl4ProofsA64/P3/Stage3InvSpecL1TopA.lean new file mode 100644 index 000000000..c9bb080b8 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3InvSpecL1TopA.lean @@ -0,0 +1,76 @@ +/- +P3 (aarch64, A64-P3 Lift 1b, T3 dispatch): **`InvSpecA` over the whole dispatch scope** (`DispScopeA`: Plan B's four +groups and the five Lift 1 groups), in the shape of the `InvSpecA` clause of `JSyscallA.invocationA`'s `hScope`. +`P3/Stage3ScopeA`'s `hScopeA` takes a `DispScopeA` member at every invocation site and gives this clause. + +Named premises: those of the arms, [P] `IrqPlatA` (IRQ control issue). New module. +-/ +import Fl4ProofsA64.P3.Stage3InvSpecL1IrqTopA +import Fl4ProofsA64.P3.Stage3DispVcpuA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {Clock : A → Fl4SpecA64.MachineState → Prop} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {ar : machine.Arch A} {nA : U64} {s : S} {a : A} {σ : State Unit} {m : McState} + +/-- A Plan B scope member is a dispatch scope member. -/ +theorem DispScopeA.of4 {lbl len slot : U64} {blk call don fp : Bool} {buf : U64} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + (h : ∃ Out, DispScope4A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ m Clock nA lbl len slot blk call + don fp buf extra Out) : + ∃ Out, DispScopeA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ m Clock nA lbl len slot blk call don + fp buf extra Out := + h.imp fun _ h => Or.inl h + +/-- **`InvSpecA` over the dispatch scope** (module docstring). -/ +theorem DispScopeA.invSpecA [PlainStore st] (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (hIP : IrqPlatA st ar) + (hCnt : Sched.CounterStepA σ) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + {info : msginfo.MessageInfo} {slot buf : U64} {blk call don fp : Bool} + {extra : Aeneas.Std.Array obj.Slot 3#usize} {Out : State Unit → Prop} + (h : DispScopeA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ m Clock hPl.ipa info.label + (msgLenA info buf) slot blk call don fp buf extra Out) + (hinfo : msg.msg_info st s (L.curW s) = ok info) {cptr : CapRef} + (r : core.result.Result Unit error.Exc) (s' : S) (a' : A) (σ' : State Unit) + (hd : syscall.decode_invocation st ar s a info.label (msgLenA info buf) slot (capA st s slot) blk call don fp buf + extra = ok (r, s', a')) (hOut : Out σ') + (done : Bool) (hdone : (done = true ∧ r = .Ok ()) ∨ (done = false ∧ r = .Err error.Exc.Preempted)) + (mi : MessageInfo) (sl : CslotPtr) (c : Cap) (excaps : List (Cap × CslotPtr)) (buffer : Option ObjRef) + (args : List Data) + (hmi : getMessageInfo (Z := Unit) σ.curThread σ = ({(mi, σ)}, False)) + (front : (frontSpecA σ.curThread cptr mi).run σ = ({(.ok (sl, c, excaps, buffer), σ)}, False)) + (hsl : slot = cslotAddrA sl) (hc : CapRelA (capA st s slot) c) (hok : ExtraOKA Z.th (L.mem s) extra excaps) + (hab : AbsSlotA Z.th sl) (hBV : BufViewA (L.mem s) σ.machineState.underlyingMemory buf buffer) + (gc : getCap (Z := Unit) sl σ = ({(c, σ)}, False)) + (eib : lookupIpcBuffer (Z := Unit) false σ.curThread σ = ({(buffer, σ)}, False)) + (hex : (lookupExtraCaps (Z := Unit) σ.curThread buffer mi).run σ = ({(.ok excaps, σ)}, False)) + (gargs : getMrs (Z := Unit) σ.curThread buffer mi σ = ({(args, σ)}, False)) : + InvSpecA call blk don done mi.miLabel args cptr sl c excaps buffer σ σ' := by + have F : FrontPinA Z.th σ slot mi sl c excaps buffer args := ⟨hmi, hsl, hab, gc, eib, hex, gargs⟩ + have B : ArgsLabA st s info buf mi args := J.argsLabA hinfo hBV hmi gargs + rcases h with h | h | h | h | h | h | h + · exact DispScope4A.invSpecA hA hM hPl J hfm h hinfo r s' a' σ' hd hOut done hdone mi sl c excaps buffer args hmi + front hsl hc hok hab hBV gc eib hex gargs + · exact FlScopeA.invSpecA J h rfl hd hOut hdone F hc B cptr + · exact IrqScopeA.invSpecA (hCnt := hCnt) hA hM hPl hIP J hfm h rfl rfl hd hOut hdone F hc hok B cptr + · exact ReplyScopeA.invSpecA hA hM hPl J hfm h rfl hd hOut hdone F hc B cptr + · exact DomScopeA.invSpecA hA hM hPl J hfm h rfl rfl hd hOut hdone F hc hok B cptr + · exact (ErrScopeA.false_of h hd hdone).elim + · exact VcpuScopeA.invSpecA J h hd hOut hdone F cptr + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3InvSpecScA.lean b/proofs/Fl4ProofsA64/P3/Stage3InvSpecScA.lean new file mode 100644 index 000000000..437486439 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3InvSpecScA.lean @@ -0,0 +1,464 @@ +/- +P3 (aarch64, A64-P3 Lift 1c, T3 dispatch): **the SchedContext group's `InvSpecA`** (`ScScopeA.invSpecA`). + +For each arm of `ScScopeA`: the spec decode of the actual cap (the looked-up slot's), label and extra cap (the head of +the front end's `lookup_extra_caps`, `scExtraHeadA`) has the invocation the arm's `Out` performs, and the perform and +the spec's epilogue (`epiSpecA`) reach the arm's final state. The spec data are the FL4 data the arm's constructor +pins (the SC of the invoked cap, the notification of the extra cap, the SC's bound thread). + +Named premises: those of the arms (`ScScopeA.sound`). New module. +-/ +import Fl4ProofsA64.P3.Stage3DispScA +import Fl4ProofsA64.P3.Stage3InvSpecFrontA +import Fl4ProofsA64.P3.Stage3InvSpecIpcA +import Fl4ProofsA64.P3.IpcInvokeDispatchA +import Fl4ProofsA64.P3.ScDecodeOpA +import Fl4ProofsA64.P3.ScConsumeOpA +import Fl4ProofsA64.P3.ScYieldTopA +import Fl4ProofsA64.P3.ScBindTopA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +/-- An SC invocation's perform. -/ +theorem perfSc_memA {blk call don : Bool} {i : SchedContextInvocation} {σ1 σ2 : State Unit} {d : List Data} + (h : invokeSchedContext (Z := Unit) i σ1 = ({(d, σ2)}, False)) : + (.ok d, σ2) ∈ ((fl4PerformInvocation (Z := Unit) blk call don (.invokeSchedContext i)).run σ1).1 := by + have run : fl4PerformInvocation (Z := Unit) blk call don (.invokeSchedContext i) = + Fl4Spec.NonDetE.liftE (invokeSchedContext (Z := Unit) i) := by + cases blk <;> cases call <;> cases don <;> rfl + rw [run] + refine liftE_memA ?_ + rw [h] + exact Set.mem_singleton _ + +/-- The spec decode of UnbindObject of the SC's bound thread, not the current one. -/ +theorem decodeUnbindObject_tcb_eqA {σ : State Unit} {sc u : ObjRef} {xs : SchedContext} {ns : Nat} + (hk : σ.kheap sc = some (.schedContext xs ns)) (h : xs.scTcb = some u) (hne : u ≠ σ.curThread) + (rest : List Cap) : + (decodeSchedContextUnbindObject (Z := Unit) sc (.threadCap u :: rest)).run σ = + ({(.ok (.invokeSchedContextUnbindObject sc (.threadCap u)), σ)}, False) := by + unfold decodeSchedContextUnbindObject + simp only [List.length_cons, Nat.succ_ne_zero, NonDetE.whenE, ↓reduceIte, NonDetE.returnOk, ndE_pure_bindA, + List.head?_cons] + have ht : the (some (Cap.threadCap u)) = Cap.threadCap u := the_someA _ + simp only [ht] + rw [run_bind_okA (run_liftEA (getScTcb_eqSc hk))] + have hget : NonDet.gets (fun s : State Unit => s.curThread) σ = ({(σ.curThread, σ)}, False) := rfl + simp only [h, ne_eq, not_true_eq_false, NonDetE.whenE, ↓reduceIte, NonDetE.returnOk, ndE_pure_bindA] + rw [run_bind_okA (run_liftEA hget)] + simp [NonDetE.whenE, hne, NonDetE.returnOk] + +/-- `InvSpecA` of an SC invocation that returns no words. -/ +theorem InvSpecA.ofScNil {blk call don : Bool} {label : Data} {args : List Data} {cptr : CapRef} {sl : CslotPtr} + {c : Cap} {excaps : List (Cap × CslotPtr)} {buffer : Option ObjRef} {σ σ1 σ' : State Unit} + {i : SchedContextInvocation} + (hdec : (.ok (Fl4Invocation.invokeSchedContext i), σ) ∈ + ((fl4DecodeInvocation (Z := Unit) call false label args cptr sl c excaps buffer).run σ).1) + (hset : setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False)) + (hinv : invokeSchedContext (Z := Unit) i σ1 = ({([], σ')}, False)) : + InvSpecA call blk don true label args cptr sl c excaps buffer σ σ' := by + refine ⟨_, σ1, hdec, hset, ?_, fun h => by cases h⟩ + unfold PerfSpecA + rw [if_pos rfl] + exact ⟨[], Or.inl ⟨perfSc_memA hinv, fun _ _ _ => rfl⟩⟩ + +/-- `InvSpecA` of an SC invocation whose words the spec's epilogue replies. -/ +theorem InvSpecA.ofScReply {blk call don : Bool} {label : Data} {args : List Data} {cptr : CapRef} {sl : CslotPtr} + {c : Cap} {excaps : List (Cap × CslotPtr)} {buffer : Option ObjRef} {σ σ1 σ2 σ' : State Unit} + {i : SchedContextInvocation} {d : List Data} + (hdec : (.ok (Fl4Invocation.invokeSchedContext i), σ) ∈ + ((fl4DecodeInvocation (Z := Unit) call false label args cptr sl c excaps buffer).run σ).1) + (hset : setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False)) + (hinv : invokeSchedContext (Z := Unit) i σ1 = ({(d, σ2)}, False)) + (hst : getThreadState (Z := Unit) σ.curThread σ2 = ({(.restart, σ2)}, False)) + (hrun : ((), σ') ∈ ((do + Fl4Spec.NonDet.when call (Fl4SpecA64.replyFromKernel (Z := Unit) σ.curThread (0, d)) + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .running) σ2).1) : + InvSpecA call blk don true label args cptr sl c excaps buffer σ σ' := by + refine ⟨_, σ1, hdec, hset, ?_, fun h => by cases h⟩ + unfold PerfSpecA + rw [if_pos rfl] + refine ⟨d, Or.inr ⟨σ2, perfSc_memA hinv, ?_⟩⟩ + unfold epiSpecA + rw [nd_bind_oneA hst] + exact hrun + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-- **The first extra cap against FL4's `extra_cap`**. -/ +theorem scExtraHeadA {th : TypedHeapA} {s : S} {extra : Aeneas.Std.Array obj.Slot 3#usize} + {excaps : List (Cap × CslotPtr)} {c0 : capability.Cap} (hx : ExtraOKA th (L.mem s) extra excaps) + (he : schedcontext.extra_cap st s extra 0#usize = ok (some c0)) : + ∃ y rest, excaps = y :: rest ∧ CapRelA c0 y.1 := by + have h03 : ¬ (0#usize ≥ 3#usize) := by decide + unfold schedcontext.extra_cap at he + simp only [h03, ↓reduceIte, exAtA_idx0, bind_ok, obj.Slot.is_null] at he + by_cases h0 : exAtA extra 0 = 0#u64 + · exfalso + simp only [h0, decide_true, ↓reduceIte] at he + cases ok_inj_ff he + have hrd : obj.Slot.cap st (exAtA extra 0) s = ok (capAtA (L.mem s) (exAtA extra 0)) := by + rw [capA_eq, ← capAtA_eq] + simp only [h0, decide_false, Bool.false_eq_true, ↓reduceIte, hrd, bind_ok] at he + have ec : capAtA (L.mem s) (exAtA extra 0) = c0 := Option.some.inj (ok_inj_ff he) + cases excaps with + | nil => exact absurd (hx.2.2 0 (by simp) (by omega)) h0 + | cons y rest => + obtain ⟨ps, -, -, -, hrel⟩ := hx.2.1 0 (by simp) + refine ⟨y, rest, rfl, ?_⟩ + simpa only [List.getElem_cons_zero, ec] using hrel + +/-- The spec's ConfigureFlags decode reads the label only through `genInvocationType`, and the extra caps only +through their head. -/ +theorem decodeSchedCtl_congrA {l1 l2 : Data} {args : List Data} {a : Cap} {r1 r2 : List Cap} + (h : genInvocationType l1 = genInvocationType l2) : + decodeFl4SchedControlInvocation (Z := Unit) l1 args (a :: r1) = + decodeFl4SchedControlInvocation (Z := Unit) l2 args (a :: r2) := by + unfold decodeFl4SchedControlInvocation + first + | (simp only [NonDetE.whenE, NonDetE.unlessE, h, List.length_cons, Nat.add_one_ne_zero, ↓reduceIte]; done) + | (simp [NonDetE.whenE, NonDetE.unlessE, h]; done) + | (rw [h]; simp [NonDetE.whenE]; done) + +/-- **SchedControl ConfigureFlags** (`ScScopeA.configure`): the arm's spec decode (of its words and its extra cap) is +the front end's. -/ +theorem scCfg_invSpecA {Z : XSetsA} {s : S} {σ σ1 σ' : State Unit} {slot lbl len buf cpu x0 p sb : U64} + {call blk don : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} {info : msginfo.MessageInfo} + {mi : MessageInfo} {sl : CslotPtr} {c : Cap} {excF : List (Cap × CslotPtr)} {buffer : Option ObjRef} + {argsF : List Data} {cptr : CapRef} + (hcap : capA st s slot = .SchedControl cpu) (hc : CapRelA (capA st s slot) c) + (hx : ExtraOKA Z.th (L.mem s) extra excF) (B : ArgsLabA st s info buf mi argsF) (hlnI : len = msgLenA info buf) + (hlab : mi.miLabel = lbl.bv) (hlbl : lbl = fl4_abi.label.SchedControlConfigureFlags) + {slbl : Data} (hslbl : genInvocationType slbl = .schedControlConfigureFlags) + {args : List U64} (hlen : len.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) + (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) (hx0n : x0 ≠ 0#u64) + (hc0 : capA st s x0 = .SchedContext p sb) + {excaps : List Cap} (hex : ExtraRelA Z.th (L.mem s) x0 excaps) {iv : SchedControlInvocation} + (hdec : (decodeFl4SchedControlInvocation (Z := Unit) slbl (args.map UScalar.bv) excaps).run σ = + ({(.ok iv, σ)}, False)) + (hsp : setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False)) + (hinv : invokeFl4SchedControlConfigureFlags (Z := Unit) iv σ1 = ({((), σ')}, False)) : + InvSpecA call blk don true mi.miLabel argsF cptr sl c excF buffer σ σ' := by + have ec : c = .schedControlCap := by + have h2 := hc.2 + rw [hcap] at h2 + exact h2.symm + -- the words: the front end's are the arm's + have eargs : argsF = args.map UScalar.bv := by + obtain ⟨-, argsU, hae, hl, hargsU⟩ := B + rw [hae] + congr 1 + have hlU : argsU.length = len.val := by rw [hlnI]; exact hl.symm + refine List.ext_getElem (by omega) fun i h1 h2 => ?_ + have hlt : i < 2 ^ 64 := by + have := len.bv.isLt + have : len.val = len.bv.toNat := rfl + omega + have hj : (⟨BitVec.ofNat 64 i⟩ : U64).val = i := by + show (BitVec.ofNat 64 i).toNat = i + rw [BitVec.toNat_ofNat] + exact Nat.mod_eq_of_lt hlt + have e1 := hargsU ⟨BitVec.ofNat 64 i⟩ argsU[i] (by rw [hj]; exact List.getElem?_eq_getElem h1) + have e2 := hargs ⟨BitVec.ofNat 64 i⟩ args[i] (by rw [hj]; exact List.getElem?_eq_getElem h2) + exact ok_inj_ff (e1.symm.trans e2) + -- the extra caps: the same head + have hx0' : exAtA extra 0 = x0 := ok_inj_ff ((exAtA_idx0 extra).symm.trans hx0) + obtain ⟨y, restF, eF, hyrel⟩ : ∃ y restF, excF = y :: restF ∧ CapRelA (capAtA (L.mem s) x0) y.1 := by + cases excF with + | nil => exact absurd (hx.2.2 0 (by simp) (by omega)) (by rw [hx0']; exact hx0n) + | cons y rest => + obtain ⟨ps, -, -, -, hrel⟩ := hx.2.1 0 (by simp) + refine ⟨y, rest, rfl, ?_⟩ + rw [← hx0'] + simpa only [List.getElem_cons_zero] using hrel + obtain ⟨a0, rest0, ha0, eE⟩ : ∃ a0 rest0, CapRelA (capAtA (L.mem s) x0) a0 ∧ excaps = a0 :: rest0 := by + rcases hex with ⟨h0, -⟩ | ⟨-, -, a0, rest0, h, e⟩ + · exact absurd h0 hx0n + · exact ⟨a0, rest0, h, e⟩ + have hcx : capAtA (L.mem s) x0 = .SchedContext p sb := hc0 + have ey : y.1 = a0 := by + have h1 := hyrel.2 + have h2 := ha0.2 + rw [hcx] at h1 h2 + exact (show absCapA (.SchedContext p sb) = y.1 from h1).symm.trans h2 + have hg : genInvocationType (specLabelWord (fl4LabelOf mi.miLabel)) = .schedControlConfigureFlags := by + rw [hlab]; refine genLabel_specA ?_ + rw [hlbl, fl4_abi.label.SchedControlConfigureFlags]; rfl + have hd2 : (decodeFl4SchedControlInvocation (Z := Unit) (specLabelWord (fl4LabelOf mi.miLabel)) argsF + (excF.map Prod.fst)).run σ = ({(.ok iv, σ)}, False) := by + rw [eF, eargs, List.map_cons, ey, decodeSchedCtl_congrA (r2 := rest0) (hg.trans hslbl.symm), ← eE] + exact hdec + refine ⟨.invokeSchedControl iv, σ1, ?_, hsp, ?_, fun h => by cases h⟩ + · rw [ec] + show (.ok (Fl4Invocation.invokeSchedControl iv), σ) ∈ ((Fl4Spec.NonDetE.liftME Fl4Invocation.invokeSchedControl + (decodeFl4SchedControlInvocation (Z := Unit) (specLabelWord (fl4LabelOf mi.miLabel)) argsF + (excF.map Prod.fst))).run σ).1 + rw [run_liftME_okA hd2] + exact Set.mem_singleton _ + · unfold PerfSpecA + rw [if_pos rfl] + refine ⟨[], Or.inl ⟨?_, fun _ _ _ => rfl⟩⟩ + have run : fl4PerformInvocation (Z := Unit) blk call don (.invokeSchedControl iv) = + (do Fl4Spec.NonDetE.liftE (invokeFl4SchedControlConfigureFlags (Z := Unit) iv) + Fl4Spec.NonDetE.returnOk ([] : List Data) : PMonad Unit (List Data)) := by + cases blk <;> cases call <;> cases don <;> rfl + rw [run] + exact perform_thenNilA (liftE_memA (by rw [hinv]; exact Set.mem_singleton _)) + +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {ar : machine.Arch A} {s : S} {a : A} {σ : State Unit} {m : McState} + +/-- **The spec's YieldTo decode** of an SC whose bound thread is another one, at a priority within the caller's MCP, +with no yield pending (FL4's checks, at the words). -/ +theorem JSyscallA.scDecodeYieldSpecA {nA : U64} + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {sc : U64} (hc : sc ∈ Z.C) (hname : scNameA m sc = specPtr sc) + (ht0 : L.mem s (scTcbA sc) ≠ 0#u64) (hne : L.mem s (scTcbA sc) ≠ L.curW s) + (hprio : ¬ L.mem s (prioA (L.mem s (scTcbA sc))) > L.mem s (mcpA (L.curW s))) + (hy : L.mem s (yieldA (L.curW s)) = 0#u64) + {label : Data} (hl : genInvocationType label = .schedContextYieldTo) (caps : List Cap) : + (decodeSchedContextInvocation (Z := Unit) label (specPtr sc) caps).run σ = + ({(.ok (.invokeSchedContextYieldTo (specPtr sc)), σ)}, False) := by + have T0 := J.toJStage3A.toTA J.sact J.noSwitch + have Q := J.toJStage3A.yieldQueueA + have hcH : sc ∈ H.scs := by rw [J.scs]; exact hc + have ht : L.mem s (scTcbA sc) ∈ Z.T := by rw [← J.tcbs]; exact T0.blkA.2.2.1.2.2.yt.1 sc hcH ht0 + have hct : L.curW s ∈ Z.T := J.sr.curT + obtain ⟨y, k, hk, hyR⟩ := Q.sched.scr.sc sc hc + rw [hname] at hk + have htb : y.scTcb = some (specPtr (L.mem s (scTcbA sc))) := by + rw [hyR.tcb, ← scTcbA_eqSc, optRefS_ne ht0] + obtain ⟨xt, hxt, -, -, htP⟩ := Q.sched.rest.prio _ ht + obtain ⟨xm, hxm, hm, -⟩ := Q.mcp _ hct + obtain ⟨xy, hxy, hyC⟩ := Q.links.tcb _ hct + rw [hxm] at hxy + cases hxy + exact decodeYieldTo_specA hl caps hk htb hxt hxm Q.sched.sch.curThread (fun e => hne (specPtr_inj e)) + (by rw [GT.gt, BitVec.lt_def, ← htP, ← hm, ← UScalar.lt_equiv]; exact hprio) + (by rw [hyC, hy]; rfl) + +set_option maxHeartbeats 4000000 in +/-- **The SchedContext group's `InvSpecA`** (module docstring). -/ +theorem ScScopeA.invSpecA (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) + s a σ m) + (hfm : A64FrameMasks st s) + {lbl len slot : U64} {blk call don fp : Bool} {buf : U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} + {Out : State Unit → Prop} + (h : ScScopeA st AR Z OS ar s a σ m lbl len slot blk call don fp buf extra Out) + {r : core.result.Result Unit error.Exc} {s' : S} {a' : A} {σ' : State Unit} + (hd : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don fp buf extra = + ok (r, s', a')) (hOut : Out σ') + {done : Bool} (hdone : (done = true ∧ r = .Ok ()) ∨ (done = false ∧ r = .Err error.Exc.Preempted)) + {mi : MessageInfo} (hlab : mi.miLabel = lbl.bv) {sl : CslotPtr} {c : Cap} {excaps : List (Cap × CslotPtr)} + {buffer : Option ObjRef} {args : List Data} (hc : CapRelA (capA st s slot) c) + (hx : ExtraOKA Z.th (L.mem s) extra excaps) {info : msginfo.MessageInfo} (B : ArgsLabA st s info buf mi args) + (hlnI : len = msgLenA info buf) (cptr : CapRef) : + InvSpecA call blk don done mi.miLabel args cptr sl c excaps buffer σ σ' := by + have hdn : done = true := by + obtain ⟨s0, a0, σ0, e0, -⟩ := ScScopeA.soundOk hA hM hPl J hfm h + have e : r = .Ok () := (Prod.mk.inj (okinj (hd.symm.trans e0))).1 + rcases hdone with ⟨h2, -⟩ | ⟨-, h2⟩ + · exact h2 + · rw [e] at h2; cases h2 + subst hdn + have T0 := J.toJStage3A.toTA J.sact J.noSwitch + -- the invoked SC cap: its object and name + have front : ∀ {sc sz : U64}, slot ∈ OS → capA st s slot = .SchedContext sc sz → + sc ∈ Z.C ∧ scNameA m sc = specPtr sc ∧ c = .schedContextCap (specPtr sc) (sz.val - minSchedContextBits) := by + intro sc sz hslot hcap + have hH : H sc = some (.sc sz.val) := (T0.capTy slot hslot).2.2.2.1 sc sz hcap + have hcH : sc ∈ H.scs := ⟨_, hH⟩ + have hci : specPtr sc ≠ m.idleScOf m.curCore := T0.ciSc slot hslot sc sz hcap + refine ⟨by rw [← J.scs]; exact hcH, by unfold scNameA; rw [if_neg hci], ?_⟩ + have hc2 : CapEqA (.SchedContext sc sz) c := by rw [← hcap]; exact hc.2 + exact (show absCapA (.SchedContext sc sz) = c from hc2).symm + -- the extra notification cap + have ntfn : ∀ {slot p badge : U64} {send recv : Bool}, slot ∈ OS → + (∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) → + schedcontext.extra_cap st s extra 0#usize = ok (some (.Notification p badge send recv)) → + p ∈ N ∧ ∃ sl0 rest, excaps = (.notificationCap (specPtr p) badge.bv (rightsOfA send recv false false), sl0) :: rest := by + intro slot p badge send recv hslot hext he + refine ⟨?_, ?_⟩ + · have q := schedcontext.extra_cap.slot (J.decPreA hslot hext) (WritesIn.refl s _) + rw [he, WP.spec_ok] at q + obtain ⟨x, hx', hx0, hxc, -⟩ := q _ rfl + exact J.ipcHeapCov.ntfn p ((T0.capTy x (hext x hx' hx0)).2.1 p badge send recv hxc) + · obtain ⟨⟨c1, sl1⟩, rest, e, hrel⟩ := scExtraHeadA hx he + have e1 : absCapA (.Notification p badge send recv) = c1 := hrel.2 + exact ⟨sl1, rest, by rw [e, ← e1]; rfl⟩ + -- the extra TCB cap + have tcbx : ∀ {slot t : U64}, slot ∈ OS → (∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) → + schedcontext.extra_cap st s extra 0#usize = ok (some (.Thread t)) → + t ∈ Z.T ∧ ∃ sl0 rest, excaps = (.threadCap (specPtr t), sl0) :: rest := by + intro slot t hslot hext he + refine ⟨?_, ?_⟩ + · have q := schedcontext.extra_cap.slot (J.decPreA hslot hext) (WritesIn.refl s _) + rw [he, WP.spec_ok] at q + obtain ⟨x, hx', hx0, hxc, -⟩ := q _ rfl + exact J.tcbs ▸ (J.toJStage3A.slotCapTy (hext x hx' hx0)).1.2.2.2.2.2 t hxc + · obtain ⟨⟨c1, sl1⟩, rest, e, hrel⟩ := scExtraHeadA hx he + have e1 : absCapA (.Thread t) = c1 := hrel.2 + exact ⟨sl1, rest, by rw [e, ← e1]; rfl⟩ + have rdN : ∀ (p : obj.Ntfn) (v : U64), obj.Ntfn.sc st p s = ok v → L.mem s (nScSc p) = v := fun p v h => + ok_inj_ff ((eq_of_spec_fd (ntfn_sc_specSc (L := L) s p)).symm.trans h) + have rdS : ∀ (sc : obj.Sc) (v : U64), obj.Sc.ntfn st sc s = ok v → L.mem s (scWA sc 40#u64) = v := fun sc v h => + ok_inj_ff ((eq_of_spec_fd (obj.Sc.ntfn.valA (L := L) s sc)).symm.trans h) + cases h with + | bindNtfn hCs h1 hslot hcov hcap hext hSC hlbl he hn hn' => + obtain ⟨hcC, hname, ec⟩ := front hslot hcap + obtain ⟨hpN, sl0, rest, ex⟩ := ntfn hslot hext he + obtain ⟨xc, σ1, -, -, hsp, hinv⟩ := hOut + obtain ⟨y, k, hk, hy⟩ := J.sr.scr.sc _ hcC + rw [hname] at hk + obtain ⟨xn, hkn, hrel⟩ := J.ntfn.ntfn _ hpN + have hs1 : y.scNtfn = none := by rw [hy.ntfn, rdS _ _ hn, optRefS_zero] + have hs2 : xn.ntfnSc = none := by + rw [hrel.sc, show Notif.nScA _ = nScSc _ from rfl, rdN _ _ hn']; rfl + have hg : genInvocationType (specLabelWord (fl4LabelOf mi.miLabel)) = .schedContextBind := by + rw [hlab]; refine genLabel_specA ?_ + rw [hlbl, fl4_abi.label.SchedContextBind]; rfl + refine InvSpecA.ofScNil ?_ hsp hinv + rw [ec, decodeSc_okA (inv := .invokeSchedContextBind _ _) ?_] + · exact Set.mem_singleton _ + rw [ex] + unfold decodeSchedContextInvocation + rw [hg] + exact decodeBind_ntfn_eqA hk hkn hs1 hs2 _ _ _ + | unbindNtfn hCs h1 hslot hcov hcap hext hSC hlbl he hn => + obtain ⟨hcC, hname, ec⟩ := front hslot hcap + obtain ⟨hpN, sl0, rest, ex⟩ := ntfn hslot hext he + obtain ⟨xc, σ1, -, -, hsp, hinv⟩ := hOut + obtain ⟨y, k, hk, hy⟩ := J.sr.scr.sc _ hcC + rw [hname] at hk + have p0 := J.sr.place.nz _ _ (J.ipcPlace.notification _ hpN) + have hs1 := hy.ntfn + rw [rdS _ _ hn, optRefS_ne p0] at hs1 + have hg : genInvocationType (specLabelWord (fl4LabelOf mi.miLabel)) = .schedContextUnbindObject := by + rw [hlab]; refine genLabel_specA ?_ + rw [hlbl, fl4_abi.label.SchedContextUnbindObject]; rfl + refine InvSpecA.ofScNil ?_ hsp hinv + rw [ec, decodeSc_okA (inv := .invokeSchedContextUnbindObject _ _) ?_] + · exact Set.mem_singleton _ + rw [ex] + unfold decodeSchedContextInvocation + rw [hg] + exact decodeUnbindObject_ntfn_eqA hk hs1 _ _ _ + | consumed hCs h1 hmax htu hslot hcov hcap hext hSC hlbl => + obtain ⟨-, -, ec⟩ := front hslot hcap + obtain ⟨xc, σ1, σ2, d, -, -, hsp, hinv, hst, hrun⟩ := hOut + have hg : genInvocationType (specLabelWord (fl4LabelOf mi.miLabel)) = .schedContextConsumed := by + rw [hlab]; refine genLabel_specA ?_ + rw [hlbl, fl4_abi.label.SchedContextConsumed]; rfl + refine InvSpecA.ofScReply ?_ hsp hinv hst hrun + rw [ec, decodeSc_okA (decodeSchedContextConsumed_eqA hg _)] + exact Set.mem_singleton _ + | yieldReply hCs h1 hmb hmax htu hslot hcov hcap hext hSC hlbl ht0 hne hprio hy hyf hon hbound hns => + obtain ⟨hcC, hname, ec⟩ := front hslot hcap + obtain ⟨xc, σ1, σ2, d, -, -, hsp, hinv, hst, hrun⟩ := hOut + have hg : genInvocationType (specLabelWord (fl4LabelOf mi.miLabel)) = .schedContextYieldTo := by + rw [hlab]; refine genLabel_specA ?_ + rw [hlbl, fl4_abi.label.SchedContextYieldTo]; rfl + refine InvSpecA.ofScReply ?_ hsp hinv hst hrun + rw [ec, decodeSc_okA (J.scDecodeYieldSpecA hcC hname ht0 hne hprio hy hg _)] + exact Set.mem_singleton _ + | yieldRequeue hCs h1 hmb hmax htu hslot hcov hcap hext hSC hlbl ht0 hne hy hyf hon hbound hprio hlt hsch => + obtain ⟨hcC, hname, ec⟩ := front hslot hcap + obtain ⟨xc, σ1, σ2, d, -, -, hsp, hinv, hst, hrun⟩ := hOut + have hg : genInvocationType (specLabelWord (fl4LabelOf mi.miLabel)) = .schedContextYieldTo := by + rw [hlab]; refine genLabel_specA ?_ + rw [hlbl, fl4_abi.label.SchedContextYieldTo]; rfl + refine InvSpecA.ofScReply ?_ hsp hinv hst hrun + rw [ec, decodeSc_okA (J.scDecodeYieldSpecA hcC hname ht0 hne hprio hy hg _)] + exact Set.mem_singleton _ + | configure hCs h1 hT hAgree hslot hcov hcap hext hSC hlbl hslbl hlen hargs hx0 hx0n hc0 hex hdec => + obtain ⟨xc, σ1, -, -, hsp, hinv, -⟩ := hOut + exact scCfg_invSpecA hcap hc hx B hlnI hlab hlbl hslbl hlen hargs hx0 hx0n hc0 hex hdec hsp hinv + | yieldSwitch hCs h1 hmb hslot hcov hcap hext hSC hlbl ht0 hne hy hyf hon hbound hprio hnlt hsch => + obtain ⟨hcC, hname, ec⟩ := front hslot hcap + obtain ⟨xc, σ1, -, -, hsp, hinv⟩ := hOut + have hg : genInvocationType (specLabelWord (fl4LabelOf mi.miLabel)) = .schedContextYieldTo := by + rw [hlab]; refine genLabel_specA ?_ + rw [hlbl, fl4_abi.label.SchedContextYieldTo]; rfl + refine InvSpecA.ofScNil ?_ hsp hinv + rw [ec, decodeSc_okA (J.scDecodeYieldSpecA hcC hname ht0 hne hprio hy hg _)] + exact Set.mem_singleton _ + | bindTcb hCs h1 hmb hslot hcov hcap hext hSC hlbl he hsl hn ht0 hbl hrel => + obtain ⟨hcC, hname, ec⟩ := front hslot hcap + obtain ⟨ht, sl0, rest, ex⟩ := tcbx hslot hext he + obtain ⟨xc, σ1, -, -, hsp, hinv⟩ := hOut + obtain ⟨y, k, hk, hyR⟩ := J.sr.scr.sc _ hcC + obtain ⟨xt, hkt, hxo⟩ := J.sr.sch.tcb _ ht + have w0 := ok_inj_ff ((eq_of_spec_fd (obj.Sc.tcb.spec (L := L) s _)).symm.trans hn) + have wt0 := ok_inj_ff ((eq_of_spec_fd (obj.Tcb.sc.spec (L := L) s _)).symm.trans ht0) + have hxn : xt.tcbSchedContext = none := by rw [hxo, wt0, optScA, if_pos rfl] + have hyn : y.scTcb = none := by rw [hyR.tcb, ← scTcbA_eqSc, w0, optRefS_zero] + have hbx := ok_inj_ff (hbl.symm.trans (Sched.bind_isBlockedA (st := st) (J.stEqA _ ht xt hkt))) + have hb : ipcQueuedThreadState xt.tcbState = true → scReleased σ.curTime y = true := fun hq => + ok_inj_ff ((Sched.bind_scReleasedA J.sr.toSchedSRN hcC hk).symm.trans (hrel (hbx.trans hq))) + have hg : genInvocationType (specLabelWord (fl4LabelOf mi.miLabel)) = .schedContextBind := by + rw [hlab]; refine genLabel_specA ?_ + rw [hlbl, fl4_abi.label.SchedContextBind]; rfl + refine InvSpecA.ofScNil ?_ hsp hinv + rw [ec, decodeSc_okA (inv := .invokeSchedContextBind _ _) ?_] + · exact Set.mem_singleton _ + rw [ex] + unfold decodeSchedContextInvocation + rw [hg] + rw [hname] at hk + exact Sched.decodeBind_threadA hkt hk hxn hyn (J.sr.inv.ne hk) hb _ + | unbindTcb hCs h1 hslot hcov hcap hext hSC hlbl he htw hne => + obtain ⟨hcC, hname, ec⟩ := front hslot hcap + obtain ⟨ht, sl0, rest, ex⟩ := tcbx hslot hext he + obtain ⟨xc, σ1, -, -, hsp, hinv⟩ := hOut + obtain ⟨y, k, hk, hyR⟩ := J.sr.scr.sc _ hcC + rw [hname] at hk + have w0 := ok_inj_ff ((eq_of_spec_fd (obj.Sc.tcb.spec (L := L) s _)).symm.trans htw) + have hs1 := hyR.tcb + rw [← scTcbA_eqSc, w0, optRefS_ne (tcb_ne01Sc (J.sr.core.tcbs _ ht)).1] at hs1 + have htc : specPtr _ ≠ σ.curThread := fun e => hne (specPtr_inj (e.trans J.sr.sch.curThread)) + have hg : genInvocationType (specLabelWord (fl4LabelOf mi.miLabel)) = .schedContextUnbindObject := by + rw [hlab]; refine genLabel_specA ?_ + rw [hlbl, fl4_abi.label.SchedContextUnbindObject]; rfl + refine InvSpecA.ofScNil ?_ hsp hinv + rw [ec, decodeSc_okA (inv := .invokeSchedContextUnbindObject _ _) ?_] + · exact Set.mem_singleton _ + rw [ex] + unfold decodeSchedContextInvocation + rw [hg] + exact decodeUnbindObject_tcb_eqA hk hs1 htc _ + | unbind hCs h1 hslot hcov hcap hext hSC hlbl htw hne => + obtain ⟨hcC, hname, ec⟩ := front hslot hcap + obtain ⟨xc, σ1, -, -, hsp, hinv⟩ := hOut + obtain ⟨y, k, hk, hyR⟩ := J.sr.scr.sc _ hcC + rw [hname] at hk + have w0 := ok_inj_ff ((eq_of_spec_fd (obj.Sc.tcb.spec (L := L) s _)).symm.trans htw) + have hs1 := hyR.tcb + rw [← scTcbA_eqSc, w0] at hs1 + have htc : y.scTcb ≠ some σ.curThread := by + rw [hs1] + intro e + unfold optRefS at e + split at e + · cases e + · exact hne (specPtr_inj ((Option.some.inj e).trans J.sr.sch.curThread)) + have hg : genInvocationType (specLabelWord (fl4LabelOf mi.miLabel)) = .schedContextUnbind := by + rw [hlab]; refine genLabel_specA ?_ + rw [hlbl, fl4_abi.label.SchedContextUnbind]; rfl + refine InvSpecA.ofScNil ?_ hsp hinv + rw [ec, decodeSc_okA (decodeUnbind_eqA hk htc hg _)] + exact Set.mem_singleton _ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3InvSpecTcb2A.lean b/proofs/Fl4ProofsA64/P3/Stage3InvSpecTcb2A.lean new file mode 100644 index 000000000..700be0070 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3InvSpecTcb2A.lean @@ -0,0 +1,290 @@ +/- +P3 (aarch64, A64-P3 Lift 1c, TCB slice 2): **`InvSpecA` for SetFlags and SetPACKeys** (`TcbScopeA.setFlags`, +`TcbScopeA.setPacKeys`). + +SetFlags: the spec decode (`decodeFl4SetFlags`) of the two message words; the perform is `invokeFl4Tcb` followed by +`fl4FinishTcbReply` (the arm's `Out`, with the current thread kept across the invoke). +WriteFPU: the spec decode (`decodeFl4FpuTransfer`) of the extra frame cap and the offset word; the frame is named at +`specPtr (ptr + offset)` (Lift 3d's `bec4981e6` form of the arm). +SetPACKeys: the spec decode reads the flag word and ten key words from the caller's registers and IPC buffer +(`tcbSetPacKeys_selectedSpecA`); the perform is the empty `invokeFl4Tcb`. + +Named premises: those of the arms. New module. +-/ +import Fl4ProofsA64.P3.Stage3InvSpecTcbA +import Fl4ProofsA64.P3.TcbInvokePacSelectA +import Fl4ProofsA64.P3.Stage3TcbFpuA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-- The spec message word `i` is FL4's `tcb::arg i`. -/ +theorem tcbArgA {s : S} {info : msginfo.MessageInfo} {len buf slot p : U64} {call : Bool} + {extra : Aeneas.Std.Array obj.Slot 3#usize} {mi : MessageInfo} {args : List Data} {i : U64} + (B : ArgsLabA st s info buf mi args) (hlnI : len = msgLenA info buf) (hi : i.val < len.val) + (hp : tcb.arg st s ⟨len, buf, call, slot, extra⟩ i = ok p) : + args.length = len.val ∧ args[i.val]? = some p.bv := by + obtain ⟨-, argsU, hae, hl, hargs⟩ := B + have hlen : argsU.length = len.val := by rw [hlnI]; exact hl.symm + have hlt : i.val < argsU.length := by omega + obtain ⟨y, hy⟩ : ∃ y, argsU[i.val]? = some y := ⟨_, List.getElem?_eq_getElem hlt⟩ + have h := hargs i y hy + unfold tcb.arg at hp + rw [cur_thread_ok, bind_ok] at hp + have e : y = p := ok_inj_ff (h.symm.trans hp) + subst e + refine ⟨by rw [hae, List.length_map, hlen], ?_⟩ + rw [hae, List.getElem?_map, hy] + rfl + +/-- The `fl4PerformInvocation` of SetFlags from the arm's pieces. -/ +theorem perfSetFlags_memA {blk call don : Bool} {t : ObjRef} {clear set : Data} {σ0 σ1 σ2 σ3 : State Unit} + {d : List Data} + (hinv : (.ok d, σ2) ∈ ((invokeFl4Tcb (Z := Unit) (.setFlags t clear set)).run σ1).1) + (hcur : σ2.curThread = σ0.curThread) + (hrun : ((), σ3) ∈ ((do + Fl4Spec.NonDet.when call (Fl4SpecA64.replyFromKernel (Z := Unit) σ0.curThread (0, d)) + Fl4SpecA64.setThreadState (Z := Unit) σ0.curThread .running) σ2).1) : + (.ok [], σ3) ∈ ((fl4PerformInvocation (Z := Unit) blk call don (.invokeFl4TCB (.setFlags t clear set))).run σ1).1 := by + have e : fl4PerformInvocation (Z := Unit) blk call don (.invokeFl4TCB (.setFlags t clear set)) = + (do + let words ← invokeFl4Tcb (.setFlags t clear set) + Fl4Spec.NonDetE.liftE (fl4FinishTcbReply call words) + Fl4Spec.NonDetE.returnOk []) := rfl + rw [e] + have hf : ((), σ3) ∈ ((fl4FinishTcbReply (Z := Unit) call d) σ2).1 := by + unfold fl4FinishTcbReply + have hg : (Fl4Spec.NonDet.gets fun s : State Unit => s.curThread) σ2 = ({(σ2.curThread, σ2)}, False) := rfl + rw [nd_bind_oneA hg, hcur] + exact hrun + exact run_bind_memA hinv (run_bind_memA (liftE_memA' (a := ()) (s1 := σ3) hf) (Set.mem_singleton _)) + +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s : S} {a : A} {σ σ' : State Unit} {m : McState} + {call blk don : Bool} {mi : MessageInfo} {sl : CslotPtr} {c : Cap} {excaps : List (Cap × CslotPtr)} + {buffer : Option ObjRef} {args : List Data} {lbl len slot buf t : U64} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + +/-- **SetFlags** (`TcbScopeA.setFlags`). -/ +theorem JSyscallA.tcbSetFlags_invSpecA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + {info : msginfo.MessageInfo} {clear set : U64} + (hslot : slot ∈ OS) (hcap : capA st s slot = .Thread t) (hc : CapRelA (capA st s slot) c) + (B : ArgsLabA st s info buf mi args) (hlnI : len = msgLenA info buf) + (hlab : mi.miLabel = lbl.bv) (hlbl : lbl = fl4_abi.label.TCBSetFlags) (hlen : ¬ len < 2#u64) + (hclear : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 0#u64 = ok clear) + (hset : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 1#u64 = ok set) + {σ1 σ2 : State Unit} {d : List Data} + (hsp : setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False)) + (hinv : (.ok d, σ2) ∈ ((invokeFl4Tcb (Z := Unit) (.setFlags (specPtr t) clear.bv set.bv)).run σ1).1) + (hcur : σ2.curThread = σ.curThread) + (hrun : ((), σ') ∈ ((do + Fl4Spec.NonDet.when call (Fl4SpecA64.replyFromKernel (Z := Unit) σ.curThread (0, d)) + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .running) σ2).1) (cptr : CapRef) : + InvSpecA call blk don true mi.miLabel args cptr sl c excaps buffer σ σ' := by + obtain ⟨ec, -, -⟩ := J.tcbTargetA hslot hcap hc + have hl2 : 2 ≤ len.val := by scalar_tac + obtain ⟨hal, g0'⟩ := tcbArgA B hlnI (i := 0#u64) (by simp; omega) hclear + obtain ⟨-, g1'⟩ := tcbArgA B hlnI (i := 1#u64) (by simp; omega) hset + have g0 : args[0]? = some clear.bv := by simpa using g0' + have g1 : args[1]? = some set.bv := by simpa using g1' + have e : fl4LabelOf lbl.bv = .spec (.genInvocationLabel .tcbSetFlags) := by + rw [hlbl, fl4_abi.label.TCBSetFlags]; rfl + have hdec : (.ok (Fl4Invocation.invokeFl4TCB (.setFlags (specPtr t) clear.bv set.bv)), σ) ∈ + ((fl4DecodeInvocation (Z := Unit) call false mi.miLabel args cptr sl c excaps buffer).run σ).1 := by + rw [ec, hlab] + show (_ ∈ ((fl4DecodeTcbInvocation (fl4LabelOf lbl.bv) args (.threadCap (specPtr t)) sl excaps buffer).run σ).1) + rw [e] + show (_ ∈ ((NonDetE.liftME Fl4Invocation.invokeFl4TCB (decodeFl4SetFlags (Z := Unit) args (specPtr t))).run σ).1) + have hd : (decodeFl4SetFlags (Z := Unit) args (specPtr t)).run σ = + ({(.ok (.setFlags (specPtr t) clear.bv set.bv), σ)}, False) := by + unfold decodeFl4SetFlags + have : ¬ args.length < 2 := by omega + simp only [NonDetE.whenE, this, ↓reduceIte, NonDetE.returnOk, ndE_pure_bindA, g0, g1, the_someA] + rfl + rw [run_liftME_okA hd] + exact Set.mem_singleton _ + refine ⟨_, σ1, hdec, hsp, ?_, fun h => by cases h⟩ + unfold PerfSpecA + rw [if_pos rfl] + exact ⟨[], Or.inl ⟨perfSetFlags_memA hinv hcur hrun, fun _ _ _ => rfl⟩⟩ + +/-- **SetPACKeys** (`TcbScopeA.setPacKeys`). -/ +theorem JSyscallA.tcbSetPacKeys_invSpecA + {info : msginfo.MessageInfo} {flags : U64} {xc : Tcb} + (hslot : slot ∈ OS) (hcap : capA st s slot = .Thread t) (hc : CapRelA (capA st s slot) c) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + (B : ArgsLabA st s info buf mi args) (hlnI : len = msgLenA info buf) + (hBV : BufViewA (L.mem s) σ.machineState.underlyingMemory buf buffer) + (hmrs : getMrs (Z := Unit) σ.curThread buffer mi σ = ({(args, σ)}, False)) + (hlab : mi.miLabel = lbl.bv) (hlbl : lbl = fl4_abi.label.TCBSetPACKeys) (hlen : ¬ len < 11#u64) + (hflags : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 0#u64 = ok flags) + (hmask : flags &&& ~~~fl4_abi.consts.pac_flag.MASK = 0#u64) + (hsupported : flags ≠ 0#u64 → fl4A64Platform.hasPauth = true) + (hkc : σ.kheap σ.curThread = some (.tcb xc)) {σ1 : State Unit} + (hsp : setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False)) + (hinv : ∀ f ks, (invokeFl4Tcb (Z := Unit) (.setPacKeys (specPtr t) f ks)).run σ1 = ({(.ok [], σ1)}, False)) + (cptr : CapRef) : + InvSpecA call blk don true mi.miLabel args cptr sl c excaps buffer σ σ1 := by + obtain ⟨ec, -, -⟩ := J.tcbTargetA hslot hcap hc + have hl11 : 11 ≤ len.val := by scalar_tac + obtain ⟨hal, g0'⟩ := tcbArgA B hlnI (i := 0#u64) (by simp; omega) hflags + have g0 : args[0]? = some flags.bv := by simpa using g0' + have hmrs' := hmrs.symm.trans (getMrs_eqA hkc buffer hBV.al mi) + have hargs : args = specMrsA xc.tcbArch.tcbContext.userRegs buffer σ.machineState.underlyingMemory + (unat mi.miLength) := by + have := congrArg Prod.fst hmrs' + exact (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 this)).1 + have hlenLe : args.length ≤ unat mi.miLength := by rw [hargs]; exact specMrsA_len_le _ _ _ _ + have hv0 : specArgA xc.tcbArch.tcbContext.userRegs buffer σ.machineState.underlyingMemory 0 = flags.bv := by + obtain ⟨hlt, hlo⟩ := specMrsA_lo xc.tcbArch.tcbContext.userRegs buffer σ.machineState.underlyingMemory + (unat mi.miLength) 0 (by omega) (by omega) + rw [← hargs] at hlo hlt + rw [getElem!_pos args 0 hlt] at hlo + have h0 : args[0]? = some args[0] := List.getElem?_eq_getElem hlt + rw [g0] at h0 + have := Option.some.inj h0 + unfold specArgA + rw [if_pos (by omega), ← hlo, ← this] + have hd : (decodeFl4SetPacKeys (Z := Unit) args buffer (specPtr t)).run σ = + ({(.ok (.setPacKeys (specPtr t) flags.bv + ((List.range' 1 10).map (specArgA xc.tcbArch.tcbContext.userRegs buffer σ.machineState.underlyingMemory))), σ)}, + False) := by + have := tcbSetPacKeys_selectedSpecA (σ := σ) (caller := xc) (target := specPtr t) (buf := buffer) (args := args) + (value := specArgA xc.tcbArch.tcbContext.userRegs buffer σ.machineState.underlyingMemory) hkc hBV.al + (by omega) (fun _ _ => rfl) (by rw [hv0]; exact (tcbPacMask_eqA flags).1 hmask) + (fun h => hsupported (fun e => h (by rw [hv0, e]; rfl))) + rw [hv0] at this + exact this + have e : fl4LabelOf lbl.bv = .ext .tcbSetPACKeys := by + rw [hlbl, fl4_abi.label.TCBSetPACKeys]; rfl + have hdec : (.ok (Fl4Invocation.invokeFl4TCB (.setPacKeys (specPtr t) flags.bv + ((List.range' 1 10).map (specArgA xc.tcbArch.tcbContext.userRegs buffer σ.machineState.underlyingMemory)))), σ) ∈ + ((fl4DecodeInvocation (Z := Unit) call false mi.miLabel args cptr sl c excaps buffer).run σ).1 := by + rw [ec, hlab] + show (_ ∈ ((fl4DecodeTcbInvocation (fl4LabelOf lbl.bv) args (.threadCap (specPtr t)) sl excaps buffer).run σ).1) + rw [e] + show (_ ∈ ((NonDetE.liftME Fl4Invocation.invokeFl4TCB + (decodeFl4SetPacKeys (Z := Unit) args buffer (specPtr t))).run σ).1) + rw [run_liftME_okA hd] + exact Set.mem_singleton _ + refine ⟨_, σ1, hdec, hsp, ?_, fun h => by cases h⟩ + unfold PerfSpecA + rw [if_pos rfl] + have hp : ∀ f ks, (.ok [], σ1) ∈ ((fl4PerformInvocation (Z := Unit) blk call don + (.invokeFl4TCB (.setPacKeys (specPtr t) f ks))).run σ1).1 := fun f ks => by + show (.ok [], σ1) ∈ ((invokeFl4Tcb (Z := Unit) (.setPacKeys (specPtr t) f ks)).run σ1).1 + rw [hinv] + exact Set.mem_singleton _ + exact ⟨[], Or.inl ⟨hp _ _, fun _ _ _ => rfl⟩⟩ + +/-- A frame cap's rights word (`1 ≤ r ≤ 3`) other than `VM_KERNEL_ONLY` is not the spec's kernel-only rights. -/ +theorem vmRights_ne_koA {r : U64} (h : 1 ≤ r.val ∧ r.val ≤ 3) (hne : r ≠ capability.VM_KERNEL_ONLY) : + vmRightsOfA r ≠ vmKernelOnly := by + have h1 : capability.VM_KERNEL_ONLY.val = 1 := by unfold capability.VM_KERNEL_ONLY; rfl + intro e + unfold vmRightsOfA at e + by_cases h3 : r.val = 3 + · rw [if_pos h3] at e + have hm := (Set.ext_iff.1 e .allowRead).1 (by simp [vmReadWrite]) + simp [vmKernelOnly] at hm + · by_cases h2 : r.val = 2 + · rw [if_neg h3, if_pos h2] at e + have hm := (Set.ext_iff.1 e .allowRead).1 (by simp [vmReadOnly]) + simp [vmKernelOnly] at hm + · exact hne (UScalar.eq_of_val_eq (by omega)) + +local notation "wa" => core.num.U64.wrapping_add + +/-- **WriteFPU and ReadFPU** (`TcbScopeA.writeFpu`, `TcbScopeA.readFpu`): `tf` is the direction (`true`: the FPU +state goes to the frame, which must then be read-write). -/ +theorem JSyscallA.tcbFpu_invSpecA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + {info : msginfo.MessageInfo} {fs offset ptr rights asid va : U64} {size : capability.FrameSize} + {mapped stage2 tf : Bool} + (hslot : slot ∈ OS) (hcap : capA st s slot = .Thread t) (hc : CapRelA (capA st s slot) c) + (hx : ExtraOKA Z.th (L.mem s) extra excaps) + (B : ArgsLabA st s info buf mi args) (hlnI : len = msgLenA info buf) + (hlab : mi.miLabel = lbl.bv) (hlbl : lbl = if tf then fl4_abi.label.TCBReadFPU else fl4_abi.label.TCBWriteFPU) + (he : tcb.extra ⟨len, buf, call, slot, extra⟩ 0#usize = ok (some fs)) (hlen : len ≥ 1#u64) + (hoff : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 0#u64 = ok offset) + (hfcap : capA st s fs = .Frame ptr size rights false mapped stage2 asid va) + (hread : rights ≠ capability.VM_KERNEL_ONLY) + (hrw : tf = true → rights = capability.VM_READ_WRITE) (halign : offset.val % 64 = 0) + (hroom : offset.val + 520 ≤ fpuFrameBytesPubA size) + {σ1 : State Unit} (hsp : setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False)) + (hperf : (.ok [], σ') ∈ ((invokeFl4Tcb (Z := Unit) (.fpuTransfer (specPtr t) tf (specPtr (wa ptr offset)))).run + σ1).1) (cptr : CapRef) : + InvSpecA call blk don true mi.miLabel args cptr sl c excaps buffer σ σ' := by + obtain ⟨ec, -, -⟩ := J.tcbTargetA hslot hcap hc + obtain ⟨⟨c1, sl0⟩, rest, ex, hrel⟩ := tcbExtraHeadA hx he + have hwf := hrel.1 + rw [hfcap] at hwf + have e1 : absCapA (.Frame ptr size rights false mapped stage2 asid va) = c1 := by + have := hrel.2 + rw [hfcap] at this + exact this + have hF : fl4TcbFrame c1 = some (specPtr ptr, vmRightsOfA rights, frameSizeOfA size, false) := by + rw [← e1]; unfold absCapA; cases stage2 <;> rfl + have hko := vmRights_ne_koA ⟨hwf.1, hwf.2.1⟩ hread + have hsize : 2 ^ pageBitsForSize (frameSizeOfA size) = fpuFrameBytesPubA size := by cases size <;> rfl + have hl1 : 1 ≤ len.val := by scalar_tac + obtain ⟨hal, g0'⟩ := tcbArgA B hlnI (i := 0#u64) (by simp; omega) hoff + have g0 : args[0]? = some offset.bv := by simpa using g0' + have e : fl4LabelOf lbl.bv = if tf then .ext .tcbReadFpu else .ext .tcbWriteFpu := by + cases tf + · rw [hlbl, if_neg Bool.false_ne_true, fl4_abi.label.TCBWriteFPU]; rfl + · rw [hlbl, if_pos rfl, fl4_abi.label.TCBReadFPU]; rfl + have hn : ¬ ((((c1, sl0) :: rest : List (Cap × CslotPtr)) = []) ∨ args.length < 1) := by + exact not_or.2 ⟨List.cons_ne_nil _ _, by omega⟩ + have hcond : ¬ (false = true ∨ vmRightsOfA rights = vmKernelOnly ∨ (tf = true ∧ vmRightsOfA rights ≠ vmReadWrite)) := by + cases tf + · simp [hko] + · have h3 : rights.val = 3 := by rw [hrw rfl]; unfold capability.VM_READ_WRITE; rfl + have hv : vmRightsOfA rights = vmReadWrite := by unfold vmRightsOfA; rw [if_pos h3] + simp [hko, hv] + all_goals (try simp only [vmReadWrite, vmKernelOnly]) + all_goals exact (Set.insert_nonempty _ _).ne_empty + have hcond2 : ¬ (offset.bv.toNat % 64 ≠ 0 ∨ offset.bv.toNat ≥ 2 ^ pageBitsForSize (frameSizeOfA size) ∨ + 2 ^ pageBitsForSize (frameSizeOfA size) - offset.bv.toNat < fl4FpuStateSize) := by + unfold fl4FpuStateSize + have : offset.bv.toNat = offset.val := rfl + omega + have hd : (decodeFl4FpuTransfer (Z := Unit) tf args (specPtr t) ((c1, sl0) :: rest)).run σ = + ({(.ok (.fpuTransfer (specPtr t) tf (specPtr (wa ptr offset))), σ)}, False) := by + unfold decodeFl4FpuTransfer + simp only [NonDetE.whenE, hn, ↓reduceIte, NonDetE.returnOk, ndE_pure_bindA, g0, Sched.the_someA, + List.getElem?_cons_zero, hF, hcond, hcond2, specPtr_wa] + rfl + have hdec : (.ok (Fl4Invocation.invokeFl4TCB (.fpuTransfer (specPtr t) tf (specPtr (wa ptr offset)))), σ) ∈ + ((fl4DecodeInvocation (Z := Unit) call false mi.miLabel args cptr sl c excaps buffer).run σ).1 := by + rw [ec, hlab, ex] + show (_ ∈ ((fl4DecodeTcbInvocation (fl4LabelOf lbl.bv) args (.threadCap (specPtr t)) sl ((c1, sl0) :: rest) + buffer).run σ).1) + rw [e] + have hm : ((.ok (Fl4Invocation.invokeFl4TCB (.fpuTransfer (specPtr t) tf (specPtr (wa ptr offset)))), σ) ∈ + ((NonDetE.liftME Fl4Invocation.invokeFl4TCB + (decodeFl4FpuTransfer (Z := Unit) tf args (specPtr t) ((c1, sl0) :: rest))).run σ).1) := by + rw [run_liftME_okA hd] + exact Set.mem_singleton _ + cases tf <;> exact hm + refine ⟨_, σ1, hdec, hsp, ?_, fun h => by cases h⟩ + unfold PerfSpecA + rw [if_pos rfl] + have hp : (.ok [], σ') ∈ ((fl4PerformInvocation (Z := Unit) blk call don + (.invokeFl4TCB (.fpuTransfer (specPtr t) tf (specPtr (wa ptr offset))))).run σ1).1 := hperf + exact ⟨[], Or.inl ⟨hp, fun _ _ _ => rfl⟩⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3InvSpecTcb3A.lean b/proofs/Fl4ProofsA64/P3/Stage3InvSpecTcb3A.lean new file mode 100644 index 000000000..10d92a737 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3InvSpecTcb3A.lean @@ -0,0 +1,679 @@ +/- +P3 (aarch64, A64-P3 Lift 1c, TCB labels): **`InvSpecA` for the labels' TCB arms** (`TcbScopeA.setIpcBufZero`, +`setTimeoutNull`, `setTimeoutNonNull`, `setSchedParamsNull`, `setSchedParamsEp`). + +Per arm: the spec decode of the actual thread cap, label, message words and extra caps has the invocation the arm's +`Out` performs. With a null handler the install ignores the invoked slot and the handler's source slot +(`fl4InstallTcbCap_nullA`). `SetSchedParams` with an unchanged SC decodes to no SC change. + +Named premises: those of the arms. New module. +-/ +import Fl4ProofsA64.P3.Stage3InvSpecTcb2A +import Fl4ProofsA64.P3.TcbInvokeTimeoutA +import Fl4ProofsA64.P3.TcbInvokeSchedSelectA +import Fl4ProofsA64.P3.ScBindTopA +import Fl4ProofsA64.P3.Stage3TcbBufStepsA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +/-- A null-cap install is the deletion alone: neither the invoked slot nor the source slot matters. -/ +theorem fl4InstallTcbCap_nullA (t : ObjRef) (κ κ' a b : CslotPtr) (n : Nat) : + fl4InstallTcbCap (Z := Unit) t κ n (some (.nullCap, a)) = fl4InstallTcbCap t κ' n (some (.nullCap, b)) := by + unfold fl4InstallTcbCap + simp [NonDetE.unlessE] + +/-- A valid FL4 fault handler is represented by its abstract cap, which is a valid spec handler. -/ +theorem tcbHandlerA {c : capability.Cap} {y : Fl4SpecA64.Cap} (h : CapRelA c y) + (hv : faults.valid_fault_handler c = ok true) : absCapA c = y ∧ validFaultHandler y := by + have hn : ∀ p m a v, c ≠ .PageTable p m a v := by + rintro p m a v rfl + simp only [faults.valid_fault_handler] at hv + cases ok_inj_ff hv + have ey : absCapA c = y := by + have e := h.2 + revert e + unfold CapEqA + split + · exact absurd rfl (hn _ _ _ _) + · exact id + exact ⟨ey, ey ▸ (tcbValidFaultHandlerA c h.1).1 hv⟩ + +/-- `getScReleased` of an SC whose refills are non-empty when active. -/ +private theorem tcbGetScReleasedA {σ : State Unit} {p : ObjRef} {x : SchedContext} {n : Nat} + (hk : σ.kheap p = some (.schedContext x n)) (hne : scActive x = true → x.scRefills ≠ []) : + getScReleased (Z := Unit) p σ = ({(scReleased σ.curTime x, σ)}, False) := by + have hr : scActive x = true → readScRefillReady (Z := Unit) p σ = some (refillReady σ.curTime (refillHd x)) := by + intro ha + show Option.bind (readRefillHead p σ) (fun h => Option.bind (Lookup.asks (fun s : State Unit => s.curTime) σ) + (fun ct => Lookup.oreturn (refillReady ct h) σ)) = _ + rw [readRefillHead_eqA hk (hne ha)]; rfl + have ha : readScActive (Z := Unit) p σ = some (scActive x) := by + show Option.bind (readSchedContext p σ) (fun sc => Lookup.oreturn (scActive sc) σ) = _ + rw [readSchedContext_eqA hk]; rfl + have e : readScReleased (Z := Unit) p σ = some (scReleased σ.curTime x) := by + show Option.bind (readScActive p σ) (fun a => (if a then readScRefillReady p else Lookup.oreturn false) σ) = _ + rw [ha] + cases h : scActive x + · simp [scReleased, h, Lookup.oreturn] + · simp [scReleased, h, hr h] + simp [getScReleased, NonDet.getsThe, e, NonDet.assertOpt]; rfl + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-- A successful `tcb::extra_with_length`: the extra cap exists, the length suffices and the slot is that one. -/ +theorem tcbExtraAtA {th : TypedHeapA} {s : S} {excaps : List (Cap × CslotPtr)} + {len buf slot x n : U64} {call : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hX : ExtraOKA th (L.mem s) extra excaps) {i : Usize} (hi : i.val < 3) + (he : tcb.extra_with_length ⟨len, buf, call, slot, extra⟩ i n = ok (.Ok x)) : + i.val < excaps.length ∧ n ≤ len ∧ x = exAtA extra i.val := by + have hidx : Aeneas.Std.Array.index_usize extra i = ok (exAtA extra i.val) := index_exAtA extra i hi + have h3 : ¬ i ≥ 3#usize := by scalar_tac + unfold tcb.extra_with_length tcb.extra at he + simp only [h3, ↓reduceIte, hidx, bind_ok, obj.Slot.is_null] at he + by_cases h0 : exAtA extra i.val = 0#u64 + · simp only [h0, decide_true, ↓reduceIte, bind_ok, error.truncated] at he + cases ok_inj_ff he + simp only [h0, decide_false, Bool.false_eq_true, ↓reduceIte, bind_ok] at he + have hlt : i.val < excaps.length := Nat.lt_of_not_le fun c => h0 (hX.2.2 _ c hi) + by_cases h : len ≥ n + · rw [if_pos h] at he + exact ⟨hlt, h, (core.result.Result.Ok.inj (ok_inj_ff he)).symm⟩ + · rw [if_neg h] at he + simp only [error.truncated] at he + cases ok_inj_ff he + +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 : JSlotA S} {ar : machine.Arch A} {nA : U64} {s : S} {a : A} {σ σ' : State Unit} {m : McState} + {call blk don : Bool} {mi : MessageInfo} {sl : CslotPtr} {c : Cap} {excaps : List (Cap × CslotPtr)} + {buffer : Option ObjRef} {args : List Data} {lbl len slot buf t src : U64} + {extra : Aeneas.Std.Array obj.Slot 3#usize} {info : msginfo.MessageInfo} + +/-- **SetIPCBuffer with a zero address** (`TcbScopeA.setIpcBufZero`). -/ +theorem JSyscallA.tcbSetIpcBufZero_invSpecA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (hslot : slot ∈ OS) (hcap : capA st s slot = .Thread t) (hc : CapRelA (capA st s slot) c) + (hx : ExtraOKA Z.th (L.mem s) extra excaps) (B : ArgsLabA st s info buf mi args) (hlnI : len = msgLenA info buf) + (hlab : mi.miLabel = lbl.bv) (hlbl : lbl = fl4_abi.label.TCBSetIPCBuffer) + (he : tcb.extra_with_length ⟨len, buf, call, slot, extra⟩ 0#usize 1#u64 = ok (.Ok src)) + (harg : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 0#u64 = ok 0#u64) + {σ1 : State Unit} (hsp : setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False)) + (hperf : ∀ κs : CslotPtr, (.ok [], σ') ∈ ((fl4InvokeTcb (.threadControlCaps (specPtr t) κs none none none none + (some (Fl4SpecA64.dataToVref (0#u64).bv, none)))).run σ1).1) (cptr : CapRef) : + InvSpecA call blk don true mi.miLabel args cptr sl c excaps buffer σ σ' := by + obtain ⟨ec, -, -⟩ := J.tcbTargetA hslot hcap hc + obtain ⟨hex, hn, -⟩ := tcbExtraAtA (st := st) hx (by simp) he + have hl1 : 1 ≤ len.val := by scalar_tac + obtain ⟨hal, g0'⟩ := tcbArgA B hlnI (i := 0#u64) (by simp; omega) harg + have g0 : args[0]? = some (0#u64 : U64).bv := by simpa using g0' + have e : fl4LabelOf lbl.bv = .spec (.genInvocationLabel .tcbSetIPCBuffer) := by + rw [hlbl, fl4_abi.label.TCBSetIPCBuffer]; rfl + obtain ⟨hl, hg⟩ := tcbLabel_genA hlab e + refine InvSpecA.ofTcbNil (i := .threadControlCaps (specPtr t) sl none none none none + (some (Fl4SpecA64.dataToVref (0#u64).bv, none))) ?_ hsp + (by cases blk <;> cases call <;> cases don <;> rfl) (hperf sl) + rw [ec] + refine tcbDec_genA hl rfl hg fun h => ?_ + unfold decodeTcbInvocation + rw [h] + dsimp only + unfold decodeSetIpcBuffer + have ha : ¬ args.length = 0 := by omega + obtain ⟨y, ys, rfl⟩ : ∃ y ys, excaps = y :: ys := by + cases excaps with + | nil => simp at hex + | cons y ys => exact ⟨y, ys, rfl⟩ + have hx0 : ¬ (y :: ys).length = 0 := by simp + have hz : Fl4SpecA64.dataToVref (0#u64 : U64).bv = 0 := rfl + simp only [ha, hx0, NonDetE.whenE, ↓reduceIte, NonDetE.returnOk, ndE_pure_bindA, g0, List.getElem?_cons_zero] + dsimp only [Fl4SpecA64.the, Option.getD_some] + simp only [hz, ↓reduceIte, ndE_pure_bindA] + rfl + +/-- **SetTimeoutEndpoint with a null handler** (`TcbScopeA.setTimeoutNull`). -/ +theorem JSyscallA.tcbSetTimeoutNull_invSpecA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {κsrc : CslotPtr} + (hslot : slot ∈ OS) (hcap : capA st s slot = .Thread t) (hc : CapRelA (capA st s slot) c) + (hx : ExtraOKA Z.th (L.mem s) extra excaps) + (hlab : mi.miLabel = lbl.bv) (hlbl : lbl = fl4_abi.label.TCBSetTimeoutEndpoint) + (he : tcb.extra_with_length ⟨len, buf, call, slot, extra⟩ 0#usize 0#u64 = ok (.Ok src)) + (hnullh : capA st s src = .Null) + {σ1 : State Unit} (hsp : setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False)) + (hperf : ∀ κs : CslotPtr, (.ok [], σ') ∈ ((fl4InvokeTcb (.threadControlCaps (specPtr t) κs none + (some (.nullCap, κsrc)) none none none)).run σ1).1) (cptr : CapRef) : + InvSpecA call blk don true mi.miLabel args cptr sl c excaps buffer σ σ' := by + obtain ⟨ec, -, -⟩ := J.tcbTargetA hslot hcap hc + obtain ⟨hex, -, es⟩ := tcbExtraAtA (st := st) hx (by simp) he + simp only [UScalar.ofNatCore_val_eq] at hex es + match excaps, hx, hex with + | y0 :: ys, hx, _ => + have f := srcOK_factsA J.j.1.place (hx.2.1 0 (by simp)) + have c0 := f.2.2.2.1 + rw [capAtA_eq (st := st)] at c0 + simp only [List.getElem_cons_zero] at c0 + rw [← es, hnullh] at c0 + have ey : y0.1 = .nullCap := (show absCapA .Null = y0.1 from c0.2).symm + have e : fl4LabelOf lbl.bv = .spec (.genInvocationLabel .tcbSetTimeoutEndpoint) := by + rw [hlbl, fl4_abi.label.TCBSetTimeoutEndpoint]; rfl + obtain ⟨hl, hg⟩ := tcbLabel_genA hlab e + have hp : (.ok [], σ') ∈ ((fl4InvokeTcb (Z := Unit) (.threadControlCaps (specPtr t) sl none + (some (.nullCap, y0.2)) none none none)).run σ1).1 := by + have h := hperf sl + simp only [fl4InvokeTcb] at h ⊢ + rwa [fl4InstallTcbCap_nullA (specPtr t) sl sl κsrc y0.2 4] at h + refine InvSpecA.ofTcbNil (i := .threadControlCaps (specPtr t) sl none (some (.nullCap, y0.2)) none none none) + ?_ hsp (by cases blk <;> cases call <;> cases don <;> rfl) hp + rw [ec] + refine tcbDec_genA hl rfl hg fun h => ?_ + unfold decodeTcbInvocation + rw [h] + dsimp only + have hv : validFaultHandler y0.1 := Or.inr ey.symm + unfold decodeSetTimeoutEp + have hck : (checkHandlerEp (Z := Unit) 1 y0).run σ = ({(.ok y0, σ)}, False) := by + unfold checkHandlerEp + simp only [hv, NonDetE.unlessE, not_true_eq_false, ↓reduceIte, NonDetE.returnOk, ndE_pure_bindA] + rfl + have hl1 : ¬ (y0 :: ys).length < 1 := by simp + simp only [hl1, NonDetE.whenE, ↓reduceIte, NonDetE.returnOk, ndE_pure_bindA, List.getElem?_cons_zero] + rw [run_bind_okA (show (checkHandlerEp (Z := Unit) 1 (Fl4SpecA64.the (some y0))).run σ = ({(.ok y0, σ)}, False) + from hck)] + have ep : y0 = (.nullCap, y0.2) := Prod.ext ey rfl + rw [← ep] + rfl + +/-- **SetTimeoutEndpoint with an endpoint handler** (`TcbScopeA.setTimeoutNonNull`). -/ +theorem JSyscallA.tcbSetTimeoutNonNull_invSpecA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {κs κsrc : CslotPtr} + (hslot : slot ∈ OS) (hcap : capA st s slot = .Thread t) (hc : CapRelA (capA st s slot) c) + (hx : ExtraOKA Z.th (L.mem s) extra excaps) + (hlab : mi.miLabel = lbl.bv) (hlbl : lbl = fl4_abi.label.TCBSetTimeoutEndpoint) + (he : tcb.extra_with_length ⟨len, buf, call, slot, extra⟩ 0#usize 0#u64 = ok (.Ok src)) + (hValid : faults.valid_fault_handler (capA st s src) = ok true) + (hsl : slot = cslotAddrA sl) (hab : AbsSlotA Z.th sl) + (hAκ : AbsSlotA Z.th κs) (hκ : cslotAddrA κs = slot) + (hAsrc : AbsSlotA Z.th κsrc) (hκsrc : cslotAddrA κsrc = src) + {σ1 : State Unit} (hsp : setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False)) + (hperf : (.ok [], σ') ∈ ((fl4InvokeTcb (.threadControlCaps (specPtr t) κs none + (some (absCapA (capA st s src), κsrc)) none none none)).run σ1).1) (cptr : CapRef) : + InvSpecA call blk don true mi.miLabel args cptr sl c excaps buffer σ σ' := by + obtain ⟨ec, -, -⟩ := J.tcbTargetA hslot hcap hc + obtain ⟨hex, -, es⟩ := tcbExtraAtA (st := st) hx (by simp) he + simp only [UScalar.ofNatCore_val_eq] at hex es + have eκ : κs = sl := cslotAddrA_inj J.j.1.place hAκ hab (hκ.trans hsl) + subst eκ + match excaps, hx, hex with + | y0 :: ys, hx, _ => + have f := srcOK_factsA J.j.1.place (hx.2.1 0 (by simp)) + obtain ⟨-, -, hx0, c0, ps, hth, hy1⟩ := f + rw [capAtA_eq (st := st)] at c0 + simp only [List.getElem_cons_zero] at c0 hx0 hth hy1 + rw [← es] at c0 hx0 + obtain ⟨ey, hv⟩ := tcbHandlerA c0 hValid + have eκs : κsrc = y0.2 := + cslotAddrA_inj J.j.1.place hAsrc ⟨ps, hy1, Or.inl hth⟩ (hκsrc.trans hx0) + rw [ey, eκs] at hperf + have e : fl4LabelOf lbl.bv = .spec (.genInvocationLabel .tcbSetTimeoutEndpoint) := by + rw [hlbl, fl4_abi.label.TCBSetTimeoutEndpoint]; rfl + obtain ⟨hl, hg⟩ := tcbLabel_genA hlab e + refine InvSpecA.ofTcbNil (i := .threadControlCaps (specPtr t) κs none (some y0) none none none) + ?_ hsp (by cases blk <;> cases call <;> cases don <;> rfl) hperf + rw [ec] + refine tcbDec_genA hl rfl hg fun h => ?_ + unfold decodeTcbInvocation + rw [h] + dsimp only + unfold decodeSetTimeoutEp + have hck : (checkHandlerEp (Z := Unit) 1 y0).run σ = ({(.ok y0, σ)}, False) := by + unfold checkHandlerEp + simp only [hv, NonDetE.unlessE, not_true_eq_false, ↓reduceIte, NonDetE.returnOk, ndE_pure_bindA] + rfl + have hl1 : ¬ (y0 :: ys).length < 1 := by simp + simp only [hl1, NonDetE.whenE, ↓reduceIte, NonDetE.returnOk, ndE_pure_bindA, List.getElem?_cons_zero] + rw [run_bind_okA (show (checkHandlerEp (Z := Unit) 1 (Fl4SpecA64.the (some y0))).run σ = ({(.ok y0, σ)}, False) + from hck)] + rfl + +theorem nonDetE_assocBufA {σ ε α β γ : Type} + (m : Fl4Spec.NonDetE σ ε α) (f : α → Fl4Spec.NonDetE σ ε β) (g : β → Fl4Spec.NonDetE σ ε γ) : + ((m >>= f) >>= g) = (m >>= fun a => f a >>= g) := by + funext s + change ((m >>= f) >>= g).run s = (m >>= fun a => f a >>= g).run s + simp only [ExceptT.run, Bind.bind, ExceptT.bind, ExceptT.mk, Fl4Spec.NonDet.bind, Fl4Spec.NonDet.pure] + apply Prod.ext + · ext ⟨r, s'⟩; constructor + · rintro ⟨⟨b, s1⟩, ⟨⟨a, s0⟩, ha, hb⟩, hg⟩ + cases a with + | error e => + simp only [Prod.mk.injEq] at hb + rcases hb with ⟨rfl, rfl⟩ + exact ⟨(.error e, s1), ha, hg⟩ + | ok x => exact ⟨(.ok x, s0), ha, ⟨(b, s1), hb, hg⟩⟩ + · rintro ⟨⟨a, s0⟩, ha, hfg⟩ + cases a with + | error e => exact ⟨(.error e, s0), ⟨(.error e, s0), ha, Set.mem_singleton _⟩, hfg⟩ + | ok x => + rcases hfg with ⟨⟨b, s1⟩, hb, hg⟩ + exact ⟨(b, s1), ⟨(.ok x, s0), ha, hb⟩, hg⟩ + · apply propext; constructor + · rintro ((hm | ⟨⟨a, s0⟩, ha, hf⟩) | ⟨⟨b, s1⟩, ⟨⟨a, s0⟩, ha, hb⟩, hg⟩) + · exact Or.inl hm + · cases a with + | error e => exact False.elim hf + | ok x => exact Or.inr ⟨(.ok x, s0), ha, Or.inl hf⟩ + · cases a with + | error e => + simp only [Set.mem_singleton_iff, Prod.mk.injEq] at hb + rcases hb with ⟨rfl, rfl⟩ + exact False.elim hg + | ok x => exact Or.inr ⟨(.ok x, s0), ha, Or.inr ⟨(b, s1), hb, hg⟩⟩ + · rintro (hm | ⟨⟨a, s0⟩, ha, hfg⟩) + · exact Or.inl (Or.inl hm) + · cases a with + | error e => exact False.elim hfg + | ok x => + rcases hfg with hf | ⟨⟨b, s1⟩, hb, hg⟩ + · exact Or.inl (Or.inr ⟨(.ok x, s0), ha, hf⟩) + · exact Or.inr ⟨(b, s1), ⟨(.ok x, s0), ha, hb⟩, hg⟩ + +/-- **The spec's `derive_cap` of a Frame cap** is the unmapped copy of it. -/ +theorem deriveFrameSpecA (σ : State Unit) (sl : CslotPtr) (ptr rights asid va : U64) (size : capability.FrameSize) + (mp ep : Bool) : + (deriveCap (Z := Unit) sl (absCapA (.Frame ptr size rights false mp ep asid va))).run σ = + ({(.ok (absCapA (.Frame ptr size rights false false false 0#u64 0#u64)), σ)}, False) := by + cases ep <;> simp [absCapA, absFrameE, deriveCap, archDeriveCap, ExtArchCap.deriveWith, mapDataA] <;> rfl + +set_option maxHeartbeats 1600000 in +/-- **SetIPCBuffer with a non-zero address and a Frame source** (`TcbScopeA.setIpcBuf`). -/ +theorem JSyscallA.tcbSetIpcBuf_invSpecA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {κs κsrc : CslotPtr} {baddr ptr rights asid va : U64} {size : capability.FrameSize} {mp ep : Bool} + (hslot : slot ∈ OS) (hcap : capA st s slot = .Thread t) (hc : CapRelA (capA st s slot) c) + (hx : ExtraOKA Z.th (L.mem s) extra excaps) (B : ArgsLabA st s info buf mi args) (hlnI : len = msgLenA info buf) + (hlab : mi.miLabel = lbl.bv) (hlbl : lbl = fl4_abi.label.TCBSetIPCBuffer) + (he : tcb.extra_with_length ⟨len, buf, call, slot, extra⟩ 0#usize 1#u64 = ok (.Ok src)) + (harg : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 0#u64 = ok baddr) (hbn : baddr ≠ 0#u64) + (hbal : baddr.val % 1024 = 0) + (hsrcF : capA st s src = .Frame ptr size rights false mp ep asid va) + (hsl : slot = cslotAddrA sl) (hab : AbsSlotA Z.th sl) + (hAκ : AbsSlotA Z.th κs) (hκ : cslotAddrA κs = slot) + (hAsrc : AbsSlotA Z.th κsrc) (hκsrc : cslotAddrA κsrc = src) + {σ1 : State Unit} (hsp : setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False)) + (hperf : (.ok [], σ') ∈ ((fl4InvokeTcb (.threadControlCaps (specPtr t) κs none none none none + (some (Fl4SpecA64.dataToVref baddr.bv, + some (absCapA (.Frame ptr size rights false false false 0#u64 0#u64), κsrc))))).run σ1).1) + (cptr : CapRef) : + InvSpecA call blk don true mi.miLabel args cptr sl c excaps buffer σ σ' := by + obtain ⟨ec, -, -⟩ := J.tcbTargetA hslot hcap hc + obtain ⟨hex, hlen1, es⟩ := tcbExtraAtA (st := st) hx (by simp) he + simp only [UScalar.ofNatCore_val_eq] at hex es + have eκ : κs = sl := cslotAddrA_inj J.j.1.place hAκ hab (hκ.trans hsl) + subst eκ + have hl1 : 1 ≤ len.val := by scalar_tac + obtain ⟨hal, g0'⟩ := tcbArgA B hlnI (i := 0#u64) (by simp; omega) harg + have g0 : args[0]? = some baddr.bv := by simpa using g0' + match excaps, hx, hex with + | y0 :: ys, hx, _ => + have f := srcOK_factsA J.j.1.place (hx.2.1 0 (by simp)) + obtain ⟨-, -, hx0, c0, ps, hth, hy1⟩ := f + rw [capAtA_eq (st := st)] at c0 + simp only [List.getElem_cons_zero] at c0 hx0 hth hy1 + rw [← es] at c0 hx0 + rw [hsrcF] at c0 + have ey : absCapA (.Frame ptr size rights false mp ep asid va) = y0.1 := + CapEqA.eq_abs c0.2 (fun _ _ _ h => by cases h) + have eκs : κsrc = y0.2 := + cslotAddrA_inj J.j.1.place hAsrc ⟨ps, hy1, Or.inl hth⟩ (hκsrc.trans hx0) + rw [eκs] at hperf + have e : fl4LabelOf lbl.bv = .spec (.genInvocationLabel .tcbSetIPCBuffer) := by + rw [hlbl, fl4_abi.label.TCBSetIPCBuffer]; rfl + obtain ⟨hl, hg⟩ := tcbLabel_genA hlab e + refine InvSpecA.ofTcbNil (i := .threadControlCaps (specPtr t) κs none none none none + (some (Fl4SpecA64.dataToVref baddr.bv, + some (absCapA (.Frame ptr size rights false false false 0#u64 0#u64), y0.2)))) ?_ hsp + (by cases blk <;> cases call <;> cases don <;> rfl) hperf + rw [ec] + refine tcbDec_genA hl rfl hg fun h => ?_ + unfold decodeTcbInvocation + rw [h] + dsimp only + unfold decodeSetIpcBuffer + have ha : ¬ args.length = 0 := by omega + have hx0' : ¬ (y0 :: ys).length = 0 := by simp + have hz : ¬ Fl4SpecA64.dataToVref baddr.bv = 0 := fun h0 => hbn (UScalar.eq_of_val_eq (by + have := congrArg BitVec.toNat h0 + simpa [Fl4SpecA64.dataToVref] using this)) + have hder := deriveFrameSpecA σ y0.2 ptr rights asid va size mp ep + rw [ey] at hder + have hchk : (checkValidIpcBuffer (Z := Unit) (Fl4SpecA64.dataToVref baddr.bv) + (absCapA (.Frame ptr size rights false false false 0#u64 0#u64))).run σ = ({(.ok (), σ)}, False) := by + show (checkValidIpcBuffer (Z := Unit) baddr.bv _).run σ = _ + rw [tcbBufferCheck_specA] + simp [bufAligned_ofModA baddr hbal] + simp only [ha, hx0', NonDetE.whenE, ↓reduceIte, NonDetE.returnOk, ndE_pure_bindA, g0, List.getElem?_cons_zero] + dsimp only [Fl4SpecA64.the, Option.getD_some] + simp only [hz, ↓reduceIte, ndE_pure_bindA] + simp only [nonDetE_assocBufA] + rw [run_bind_okA hder, run_bind_okA hchk] + simp only [ndE_pure_bindA] + rfl + +/-- `decode_update_sc` of an accepted `sched_params_sc` that keeps the target's SC: no SC change. -/ +theorem JSyscallA.tcbUpdateScKeepA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {scSlot v : U64} {y : Cap} (hslot : slot ∈ OS) (hcap : capA st s slot = .Thread t) + (hOS : scSlot ≠ 0#u64 → scSlot ∈ OS) (hnz : scSlot ≠ 0#u64) (hy : CapRelA (capA st s scSlot) y) + (hsc : tcb.sched_params_sc st s t scSlot = ok (.Ok v)) (hold : obj.Tcb.sc st t s = ok v) + (hne : t ≠ L.curW s) : + (decodeUpdateSc (Z := Unit) (.threadCap (specPtr t)) sl y).run σ = ({(.ok none, σ)}, False) := by + have htT : t ∈ Z.T := J.tcbs ▸ (J.toJStage3A.slotCapTy hslot).1.2.2.2.2.2 t hcap + obtain ⟨u, hk, hks⟩ := J.sr.sch.tcb t htT + have hbl := Sched.bind_isBlockedA (st := st) (J.stEqA _ htT u hk) + have hcur : σ.curThread = specPtr (L.curW s) := J.j.1.sch.curThread + have hv : v = L.mem s (scA t) := ok_inj_ff (hold.symm.trans (eq_of_spec_fd (obj.Tcb.sc.spec (L := L) s t))) + have htcb : (Fl4Spec.NonDetE.liftE (getTcbObjRef Fl4SpecA64.Tcb.tcbSchedContext (specPtr t)) : + Fl4SpecA64.SEMonad Unit _).run σ = ({(.ok u.tcbSchedContext, σ)}, False) := + run_liftEA (threadGet_eqSc Fl4SpecA64.Tcb.tcbSchedContext hk) + have hcapA : obj.Slot.cap st scSlot s = ok (capA st s scSlot) := capA_eq (st := st) s scSlot + by_cases h0 : capA st s scSlot = .Null + · have ey : y = .nullCap := by + have e := hy.2 + rw [h0] at e + exact e.symm + subst ey + rw [tcbSchedParamsSc_nullA (h0 ▸ hcapA), if_neg hne] at hsc + have v0 : v = 0#u64 := (core.result.Result.Ok.inj (ok_inj_ff hsc)).symm + have hn : u.tcbSchedContext = none := by rw [hks, ← hv, v0, optScA, if_pos rfl] + have hct : (Fl4Spec.NonDetE.liftE (Fl4Spec.NonDet.gets fun s : Fl4SpecA64.State Unit => s.curThread) : + Fl4SpecA64.SEMonad Unit _).run σ = ({(.ok σ.curThread, σ)}, False) := run_liftEA rfl + unfold decodeUpdateSc + simp only [Fl4Spec.NonDetE.returnOk, ndE_pure_bindA, show objRefOf (.threadCap (specPtr t)) = specPtr t from rfl] + rw [run_bind_okA htcb] + try dsimp only + rw [run_bind_okA hct] + rw [show Fl4Spec.NonDetE.whenE (σ := Fl4SpecA64.State Unit) (specPtr t = σ.curThread) + (Fl4Spec.NonDetE.throwError Fl4SpecA64.SyscallError.illegalOperation) = pure () from + if_neg (by rw [hcur]; exact fun c => hne (specPtr_inj c)), ndE_pure_bindA] + simp only [ndE_pure_bindA, hn, ne_eq, not_true_eq_false, ↓reduceIte] + rfl + by_cases hS : ∃ p b, capA st s scSlot = .SchedContext p b + swap + · have hk' : ∀ p b, capA st s scSlot ≠ .SchedContext p b := fun p b c => hS ⟨p, b, c⟩ + rw [tcbSchedParamsSc_badCapA hcapA h0 hk'] at hsc + cases ok_inj_ff hsc + obtain ⟨p, b, hq⟩ := hS + have hxO := hOS hnz + have hH : H p = some (.sc b.val) := (J.capTy _ hxO).2.2.2.1 p b hq + have hp : p ∈ Z.C := by rw [← J.scs]; exact ⟨_, hH⟩ + have hni : specPtr p ≠ m.idleScOf m.curCore := (J.toJStage3A.toTA J.sact J.noSwitch).ciSc _ hxO p b hq + have hname : scNameA m p = specPtr p := by unfold scNameA; exact if_neg hni + have hren : scRenA m p = specPtr p := by unfold scRenA; exact if_neg hni + obtain ⟨z, k, hz, -⟩ := J.sr.scr.sc p hp + have hrel := Sched.bind_scReleasedA J.sr.toSchedSRN hp hz + rw [hname] at hz + have hgr := tcbGetScReleasedA hz (J.sr.inv.ne hz) + have ey : y = .schedContextCap (specPtr p) (b.val - Fl4SpecA64.minSchedContextBits) := by + have e := hy.2 + rw [hq] at e + exact e.symm + subst ey + rw [tcbSchedParamsSc_contextA (show obj.Slot.cap st scSlot s = ok (.SchedContext p b) by rw [hcapA, hq]) + hold] at hsc + have rel : (do + let blocked ← sched.is_blocked st s t + if blocked then + let released ← sporadic.sc_released st s p + if released then ok (.Ok p) else error.illegal obj.Sc + else ok (.Ok p)) = ok (core.result.Result.Ok v) → + v = p ∧ ¬ (ipcQueuedThreadState u.tcbState = true ∧ ¬ scReleased σ.curTime z = true) := by + intro h + rw [hbl, bind_ok] at h + cases hb : ipcQueuedThreadState u.tcbState + · rw [hb] at h + simp only [Bool.false_eq_true, ite_false] at h + exact ⟨(core.result.Result.Ok.inj (ok_inj_ff h)).symm, fun c => Bool.false_ne_true c.1⟩ + · rw [hb] at h + simp only [ite_true] at h + rw [hrel, bind_ok] at h + cases hrl : scReleased σ.curTime z + · rw [hrl] at h + simp only [Bool.false_eq_true, ite_false, error.illegal] at h + cases ok_inj_ff h + · rw [hrl] at h + simp only [ite_true] at h + exact ⟨(core.result.Result.Ok.inj (ok_inj_ff h)).symm, fun c => c.2 rfl⟩ + have hvp : v = p ∧ ¬ (ipcQueuedThreadState u.tcbState = true ∧ ¬ scReleased σ.curTime z = true) := by + by_cases h1 : v = p + · rw [if_pos h1] at hsc + exact rel hsc + rw [if_neg h1] at hsc + by_cases h2 : v = 0#u64 + · rw [if_pos h2] at hsc + rw [show obj.Sc.tcb st p s = ok (L.mem s (scTcbA p)) from eq_of_spec_fd (obj.Sc.tcb.spec (L := L) s p), + bind_ok] at hsc + split at hsc + · exact rel hsc + · simp only [error.illegal] at hsc + cases ok_inj_ff hsc + · rw [if_neg h2] at hsc + simp only [error.illegal] at hsc + cases ok_inj_ff hsc + obtain ⟨vp, hB⟩ := hvp + have hsome : u.tcbSchedContext = some (specPtr p) := by + rw [hks, ← hv, vp, optScA, if_neg (J.scNZ p hp), hren] + have hsct : getScObjRef (Z := Unit) Fl4SpecA64.SchedContext.scTcb (specPtr p) σ = ({(z.scTcb, σ)}, False) := by + unfold getScObjRef + rw [nd_bind_oneSc (Sched.getSchedContext_eqA hz)] + rfl + have hibk : isBlocked (Z := Unit) (specPtr t) σ = ({(ipcQueuedThreadState u.tcbState, σ)}, False) := by + have h : getThreadState (Z := Unit) (specPtr t) σ = ({(u.tcbState, σ)}, False) := threadGet_eqSc _ hk + unfold isBlocked Fl4Spec.NonDet.liftM + show (getThreadState (Z := Unit) (specPtr t) >>= fun x => pure (ipcQueuedThreadState x)) σ = _ + rw [nd_bind_oneSc h] + rfl + have hst : (Fl4Spec.NonDetE.liftE (getScObjRef Fl4SpecA64.SchedContext.scTcb (specPtr p)) : + Fl4SpecA64.SEMonad Unit _).run σ = ({(.ok z.scTcb, σ)}, False) := run_liftEA hsct + have hib : (Fl4Spec.NonDetE.liftE (isBlocked (specPtr t)) : Fl4SpecA64.SEMonad Unit _).run σ = + ({(.ok (ipcQueuedThreadState u.tcbState), σ)}, False) := run_liftEA hibk + have hgr' : (Fl4Spec.NonDetE.liftE (getScReleased (specPtr p)) : Fl4SpecA64.SEMonad Unit _).run σ = + ({(.ok (scReleased σ.curTime z), σ)}, False) := run_liftEA hgr + unfold decodeUpdateSc + simp only [Fl4Spec.NonDetE.returnOk, ndE_pure_bindA, show objRefOf (.threadCap (specPtr t)) = specPtr t from rfl] + rw [run_bind_okA htcb] + try dsimp only + dsimp only [objRefOf] + rw [run_bind_okA hst] + simp only [NonDetE.whenE, hsome, ne_eq, not_true_eq_false, false_and, ↓reduceIte, ndE_pure_bindA] + rw [run_bind_okA hib, run_bind_okA hgr'] + simp only [hB, ↓reduceIte, ndE_pure_bindA] + rfl + +/-- **The spec decode of SetSchedParams with an unchanged SC**, up to the fault handler's check. -/ +theorem JSyscallA.tcbSspDecodeA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {authSlot scSlot fhSlot mcp prio auth v : U64} + (hslot : slot ∈ OS) (hcap : capA st s slot = .Thread t) + (hx : ExtraOKA Z.th (L.mem s) extra excaps) (B : ArgsLabA st s info buf mi args) (hlnI : len = msgLenA info buf) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) + (he0 : tcb.extra_with_length ⟨len, buf, call, slot, extra⟩ 0#usize 2#u64 = ok (.Ok authSlot)) + (he1 : tcb.extra_with_length ⟨len, buf, call, slot, extra⟩ 1#usize 2#u64 = ok (.Ok scSlot)) + (he2 : tcb.extra_with_length ⟨len, buf, call, slot, extra⟩ 2#usize 2#u64 = ok (.Ok fhSlot)) + (hmcp : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 0#u64 = ok mcp) + (hprio : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 1#u64 = ok prio) + (harg : authSlot ∈ OS) (hac : capA st s authSlot = .Thread auth) + (hle1 : ¬ L.mem s (mcpA auth) < mcp) (hle2 : ¬ L.mem s (mcpA auth) < prio) + (hsc : tcb.sched_params_sc st s t scSlot = ok (.Ok v)) (hold : obj.Tcb.sc st t s = ok v) + (hne : t ≠ L.curW s) : + ∃ y : Cap × CslotPtr, CapRelA (capA st s fhSlot) y.1 ∧ fhSlot = cslotAddrA y.2 ∧ AbsSlotA Z.th y.2 ∧ + (validFaultHandler y.1 → + (decodeSetSchedParams (Z := Unit) args (.threadCap (specPtr t)) sl excaps).run σ = + ({(.ok (.threadControlSched (specPtr t) sl (some y) (some (BitVec.ofNat 8 mcp.val, specPtr auth)) + (some (BitVec.ofNat 8 prio.val, specPtr auth)) none), σ)}, False)) := by + have hP := J.j.1.place + obtain ⟨-, hn, es0⟩ := tcbExtraAtA (st := st) hx (by simp) he0 + obtain ⟨-, -, es1⟩ := tcbExtraAtA (st := st) hx (by simp) he1 + obtain ⟨hex, -, es2⟩ := tcbExtraAtA (st := st) hx (by simp) he2 + simp only [UScalar.ofNatCore_val_eq] at hex es0 es1 es2 + have hl2 : 2 ≤ len.val := by scalar_tac + obtain ⟨hal, g0'⟩ := tcbArgA B hlnI (i := 0#u64) (by simp; omega) hmcp + obtain ⟨-, g1'⟩ := tcbArgA B hlnI (i := 1#u64) (by simp; omega) hprio + have g0 : args[0]? = some mcp.bv := by simpa using g0' + have g1 : args[1]? = some prio.bv := by simpa using g1' + have haT : auth ∈ Z.T := J.tcbs ▸ (J.toJStage3A.slotCapTy harg).1.2.2.2.2.2 auth hac + have chk : ∀ p : U64, ¬ L.mem s (mcpA auth) < p → + (checkPrio (Z := Unit) p.bv (specPtr auth)).run σ = ({(.ok (), σ)}, False) := by + intro p hle + obtain ⟨z, hz, hw, -⟩ := J.mcp auth haT + have hnot : ¬ p.bv > ucast 64 z.tcbMcpriority := by + have h1 : p.val ≤ z.tcbMcpriority.toNat := by rw [← hw]; exact Nat.le_of_not_lt hle + have h2 : (ucast 64 z.tcbMcpriority).toNat = z.tcbMcpriority.toNat := by + have := z.tcbMcpriority.isLt + simp only [ucast, BitVec.toNat_setWidth] + exact Nat.mod_eq_of_lt (by omega) + rw [GT.gt, BitVec.lt_def, h2] + exact Nat.not_lt.2 h1 + unfold checkPrio + rw [run_bind_okA (run_liftEA (threadGet_eqSc Tcb.tcbMcpriority hz))] + simp only [NonDetE.whenE, hnot, ↓reduceIte] + rfl + have eu : ∀ p : U64, ucast 8 p.bv = BitVec.ofNat 8 p.val := by + intro p + apply BitVec.eq_of_toNat_eq + simp [ucast] + match excaps, hx, hex with + | y0 :: y1 :: y2 :: ys, hx, _ => + have s0 := srcOK_factsA hP (hx.2.1 0 (by simp)) + have s1 := srcOK_factsA hP (hx.2.1 1 (by simp)) + obtain ⟨-, -, hx2, c2, ps, hth, hy1⟩ := srcOK_factsA hP (hx.2.1 2 (by simp)) + have c0 := s0.2.2.2.1 + have c1 := s1.2.2.2.1 + have n1 := s1.1 + rw [capAtA_eq (st := st)] at c0 c1 c2 + simp only [List.getElem_cons_zero, List.getElem_cons_succ] at c0 c1 c2 n1 hx2 hth hy1 + rw [← es0, hac] at c0 + rw [← es1] at c1 n1 + rw [← es2] at c2 hx2 + have ey0 : y0.1 = .threadCap (specPtr auth) := (show absCapA (.Thread auth) = y0.1 from c0.2).symm + have hmem : scSlot ∈ extra.val := by + have hl : extra.val.length = 3 := by simpa using extra.property + have : exAtA extra 1 = extra.val[1]'(by omega) := by + simp [exAtA, List.getD_eq_getElem?_getD, List.getElem?_eq_getElem (show 1 < extra.val.length by omega)] + rw [es1, this]; exact List.getElem_mem _ + have U := J.tcbUpdateScKeepA (sl := sl) (y := y1.1) hslot hcap (fun h => hext _ hmem h) n1 c1 hsc hold hne + refine ⟨y2, c2, hx2, ⟨ps, hy1, Or.inl hth⟩, fun hv => ?_⟩ + have ha : ¬ args.length < 2 := by omega + have h3 : ¬ (y0 :: y1 :: y2 :: ys).length < 3 := by simp + unfold decodeSetSchedParams + simp only [ha, h3, NonDetE.whenE, ↓reduceIte, NonDetE.returnOk, ndE_pure_bindA, g0, g1, + List.getElem?_cons_zero, List.getElem?_cons_succ] + dsimp only [Fl4SpecA64.the, Option.getD_some] + rw [ey0] + dsimp only + rw [run_bind_okA (chk mcp hle1), run_bind_okA (chk prio hle2), run_bind_okA U] + unfold checkHandlerEp + simp only [hv, NonDetE.unlessE, not_true_eq_false, ↓reduceIte, NonDetE.returnOk, ndE_pure_bindA, eu] + rfl + +/-- **SetSchedParams with a null handler** (`TcbScopeA.setSchedParamsNull`). -/ +theorem JSyscallA.tcbSspNull_invSpecA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {authSlot scSlot fhSlot mcp prio auth v : U64} {κs κsrc : CslotPtr} + (hslot : slot ∈ OS) (hcap : capA st s slot = .Thread t) (hc : CapRelA (capA st s slot) c) + (hx : ExtraOKA Z.th (L.mem s) extra excaps) (B : ArgsLabA st s info buf mi args) (hlnI : len = msgLenA info buf) + (hlab : mi.miLabel = lbl.bv) (hlbl : lbl = fl4_abi.label.TCBSetSchedParams) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) + (he0 : tcb.extra_with_length ⟨len, buf, call, slot, extra⟩ 0#usize 2#u64 = ok (.Ok authSlot)) + (he1 : tcb.extra_with_length ⟨len, buf, call, slot, extra⟩ 1#usize 2#u64 = ok (.Ok scSlot)) + (he2 : tcb.extra_with_length ⟨len, buf, call, slot, extra⟩ 2#usize 2#u64 = ok (.Ok fhSlot)) + (hmcp : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 0#u64 = ok mcp) + (hprio : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 1#u64 = ok prio) + (harg : authSlot ∈ OS) (hac : capA st s authSlot = .Thread auth) + (hle1 : ¬ L.mem s (mcpA auth) < mcp) (hle2 : ¬ L.mem s (mcpA auth) < prio) + (hsc : tcb.sched_params_sc st s t scSlot = ok (.Ok v)) (hold : obj.Tcb.sc st t s = ok v) + (hfh : capA st s fhSlot = .Null) (hne : t ≠ L.curW s) + {σ1 : State Unit} (hsp : setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False)) + (hperf : (.ok [], σ') ∈ ((fl4InvokeTcb (.threadControlSched (specPtr t) κs (some (.nullCap, κsrc)) + (some (BitVec.ofNat 8 mcp.val, specPtr auth)) (some (BitVec.ofNat 8 prio.val, specPtr auth)) none)).run σ1).1) + (cptr : CapRef) : + InvSpecA call blk don true mi.miLabel args cptr sl c excaps buffer σ σ' := by + obtain ⟨ec, -, -⟩ := J.tcbTargetA hslot hcap hc + obtain ⟨y, cy, -, -, hdec⟩ := J.tcbSspDecodeA (sl := sl) hslot hcap hx B hlnI hext he0 he1 he2 hmcp hprio harg hac + hle1 hle2 hsc hold hne + rw [hfh] at cy + have ey : y.1 = .nullCap := (show absCapA .Null = y.1 from cy.2).symm + have ep : y = (.nullCap, y.2) := Prod.ext ey rfl + have e : fl4LabelOf lbl.bv = .spec (.genInvocationLabel .tcbSetSchedParams) := by + rw [hlbl, fl4_abi.label.TCBSetSchedParams]; rfl + obtain ⟨hl, hg⟩ := tcbLabel_genA hlab e + have hp : (.ok [], σ') ∈ ((fl4InvokeTcb (Z := Unit) (.threadControlSched (specPtr t) sl (some y) + (some (BitVec.ofNat 8 mcp.val, specPtr auth)) (some (BitVec.ofNat 8 prio.val, specPtr auth)) none)).run σ1).1 := by + rw [ep] + simp only [fl4InvokeTcb] at hperf ⊢ + rwa [fl4InstallTcbCap_nullA (specPtr t) κs sl κsrc y.2 3] at hperf + refine InvSpecA.ofTcbNil (i := .threadControlSched (specPtr t) sl (some y) + (some (BitVec.ofNat 8 mcp.val, specPtr auth)) (some (BitVec.ofNat 8 prio.val, specPtr auth)) none) ?_ hsp + (by cases blk <;> cases call <;> cases don <;> rfl) hp + rw [ec] + refine tcbDec_genA hl rfl hg fun h => ?_ + unfold decodeTcbInvocation + rw [h] + exact hdec (Or.inr ey.symm) + +/-- **SetSchedParams with an endpoint handler** (`TcbScopeA.setSchedParamsEp`). -/ +theorem JSyscallA.tcbSspEp_invSpecA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {authSlot scSlot fhSlot mcp prio auth v : U64} {κs κsrc : CslotPtr} + (hslot : slot ∈ OS) (hcap : capA st s slot = .Thread t) (hc : CapRelA (capA st s slot) c) + (hx : ExtraOKA Z.th (L.mem s) extra excaps) (B : ArgsLabA st s info buf mi args) (hlnI : len = msgLenA info buf) + (hlab : mi.miLabel = lbl.bv) (hlbl : lbl = fl4_abi.label.TCBSetSchedParams) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) + (he0 : tcb.extra_with_length ⟨len, buf, call, slot, extra⟩ 0#usize 2#u64 = ok (.Ok authSlot)) + (he1 : tcb.extra_with_length ⟨len, buf, call, slot, extra⟩ 1#usize 2#u64 = ok (.Ok scSlot)) + (he2 : tcb.extra_with_length ⟨len, buf, call, slot, extra⟩ 2#usize 2#u64 = ok (.Ok fhSlot)) + (hmcp : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 0#u64 = ok mcp) + (hprio : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 1#u64 = ok prio) + (harg : authSlot ∈ OS) (hac : capA st s authSlot = .Thread auth) + (hle1 : ¬ L.mem s (mcpA auth) < mcp) (hle2 : ¬ L.mem s (mcpA auth) < prio) + (hsc : tcb.sched_params_sc st s t scSlot = ok (.Ok v)) (hold : obj.Tcb.sc st t s = ok v) + (hValid : faults.valid_fault_handler (capA st s fhSlot) = ok true) (hne : t ≠ L.curW s) + (hsl : slot = cslotAddrA sl) (hab : AbsSlotA Z.th sl) + (hAκ : AbsSlotA Z.th κs) (hκ : cslotAddrA κs = slot) + (hAsrc : AbsSlotA Z.th κsrc) (hκsrc : cslotAddrA κsrc = fhSlot) + {σ1 : State Unit} (hsp : setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False)) + (hperf : (.ok [], σ') ∈ ((fl4InvokeTcb (.threadControlSched (specPtr t) κs + (some (absCapA (capA st s fhSlot), κsrc)) + (some (BitVec.ofNat 8 mcp.val, specPtr auth)) (some (BitVec.ofNat 8 prio.val, specPtr auth)) none)).run σ1).1) + (cptr : CapRef) : + InvSpecA call blk don true mi.miLabel args cptr sl c excaps buffer σ σ' := by + obtain ⟨ec, -, -⟩ := J.tcbTargetA hslot hcap hc + obtain ⟨y, cy, hys, hya, hdec⟩ := J.tcbSspDecodeA (sl := sl) hslot hcap hx B hlnI hext he0 he1 he2 hmcp hprio harg + hac hle1 hle2 hsc hold hne + obtain ⟨ey, hv⟩ := tcbHandlerA cy hValid + have eκ : κs = sl := cslotAddrA_inj J.j.1.place hAκ hab (hκ.trans hsl) + have eκs : κsrc = y.2 := cslotAddrA_inj J.j.1.place hAsrc hya (hκsrc.trans hys) + rw [ey, eκ, eκs] at hperf + have e : fl4LabelOf lbl.bv = .spec (.genInvocationLabel .tcbSetSchedParams) := by + rw [hlbl, fl4_abi.label.TCBSetSchedParams]; rfl + obtain ⟨hl, hg⟩ := tcbLabel_genA hlab e + refine InvSpecA.ofTcbNil (i := .threadControlSched (specPtr t) sl (some y) + (some (BitVec.ofNat 8 mcp.val, specPtr auth)) (some (BitVec.ofNat 8 prio.val, specPtr auth)) none) ?_ hsp + (by cases blk <;> cases call <;> cases don <;> rfl) hperf + rw [ec] + refine tcbDec_genA hl rfl hg fun h => ?_ + unfold decodeTcbInvocation + rw [h] + exact hdec hv + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3InvSpecTcbA.lean b/proofs/Fl4ProofsA64/P3/Stage3InvSpecTcbA.lean new file mode 100644 index 000000000..bb486244f --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3InvSpecTcbA.lean @@ -0,0 +1,458 @@ +/- +P3 (aarch64, A64-P3 Lift 1c, TCB slice 1): **`InvSpecA` for the TCB arms of the notification, priority, TLS and +Resume labels** (`TcbScopeA`: `unbindNtfn`, `bindNtfn`, `setMcp`, the five `setPrio*`, `setTls`, `setTlsSelf`, +`resumeRunnable`). + +One theorem per label, from the arm's FL4 facts (as its constructor states them) and its `Out`: the spec decode of the +actual thread cap, label, message words and extra cap (the head of the front end's `lookup_extra_caps`) has the +invocation `Out` performs. The spec data are pinned to those FL4 data. The remaining arms (`setPacKeys`, `writeFpu`, +`setFlags`) and the group's assembly are the next slice. + +Named premises: those of the arms. New module. +-/ +import Fl4ProofsA64.P3.Stage3DispTcbA +import Fl4ProofsA64.P3.Stage3InvSpecFrontA +import Fl4ProofsA64.P3.ScDecodeOpA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +/-- `InvSpecA` of a TCB invocation whose perform returns no words. -/ +theorem InvSpecA.ofTcbNil {blk call don : Bool} {label : Data} {args : List Data} {cptr : CapRef} {sl : CslotPtr} + {c : Cap} {excaps : List (Cap × CslotPtr)} {buffer : Option ObjRef} {σ σ1 σ' : State Unit} + {i : TcbInvocation} + (hdec : (.ok (Fl4Invocation.invokeTCB i), σ) ∈ + ((fl4DecodeInvocation (Z := Unit) call false label args cptr sl c excaps buffer).run σ).1) + (hset : setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False)) + (hrun : fl4PerformInvocation (Z := Unit) blk call don (.invokeTCB i) = fl4InvokeTcb i) + (hperf : (.ok [], σ') ∈ ((fl4InvokeTcb (Z := Unit) i).run σ1).1) : + InvSpecA call blk don true label args cptr sl c excaps buffer σ σ' := by + refine ⟨_, σ1, hdec, hset, ?_, fun h => by cases h⟩ + unfold PerfSpecA + rw [if_pos rfl] + exact ⟨[], Or.inl ⟨by rw [hrun]; exact hperf, fun _ _ _ => rfl⟩⟩ + +/-- The spec decode of a thread cap under a generic TCB label that FL4 leaves to the ported decoder. -/ +theorem tcbDec_genA {call : Bool} {label : Data} {args : List Data} {cptr : CapRef} {sl : CslotPtr} + {p : ObjRef} {excaps : List (Cap × CslotPtr)} {buffer : Option ObjRef} {σ : State Unit} {i : TcbInvocation} + {g : GenInvocationLabels} + (hl : fl4LabelOf label = .spec (.genInvocationLabel g)) + (hrfl : fl4DecodeTcbInvocation (Z := Unit) (.spec (.genInvocationLabel g)) args (.threadCap p) sl excaps buffer = + Fl4Spec.NonDetE.liftME Fl4Invocation.invokeTCB + (decodeTcbInvocation (specLabelWord (.spec (.genInvocationLabel g))) args (.threadCap p) sl excaps)) + (hg : genInvocationType (specLabelWord (.spec (.genInvocationLabel g))) = g) + (h : genInvocationType (specLabelWord (.spec (.genInvocationLabel g))) = g → + (decodeTcbInvocation (Z := Unit) (specLabelWord (.spec (.genInvocationLabel g))) args (.threadCap p) sl + excaps).run σ = ({(.ok i, σ)}, False)) : + (.ok (Fl4Invocation.invokeTCB i), σ) ∈ + ((fl4DecodeInvocation (Z := Unit) call false label args cptr sl (.threadCap p) excaps buffer).run σ).1 := by + have e : fl4DecodeInvocation (Z := Unit) call false label args cptr sl (.threadCap p) excaps buffer = + fl4DecodeTcbInvocation (fl4LabelOf label) args (.threadCap p) sl excaps buffer := rfl + rw [e, hl, hrfl, run_liftME_okA (h hg)] + exact Set.mem_singleton _ + +/-- The label facts of a generic TCB label from the FL4 label word. -/ +theorem tcbLabel_genA {lbl : U64} {label : Data} (hlab : label = lbl.bv) {g : GenInvocationLabels} + (e : fl4LabelOf lbl.bv = .spec (.genInvocationLabel g)) : + fl4LabelOf label = .spec (.genInvocationLabel g) ∧ + genInvocationType (specLabelWord (.spec (.genInvocationLabel g))) = g := by + have h := genLabel_specA e + rw [e] at h + exact ⟨by rw [hlab, e], h⟩ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-- **`tcb::extra` against the front end's first extra cap**. -/ +theorem tcbExtraHeadA {th : TypedHeapA} {s : S} {extra : Aeneas.Std.Array obj.Slot 3#usize} + {excaps : List (Cap × CslotPtr)} {len buf slot x : U64} {call : Bool} + (hx : ExtraOKA th (L.mem s) extra excaps) + (he : tcb.extra ⟨len, buf, call, slot, extra⟩ 0#usize = ok (some x)) : + ∃ y rest, excaps = y :: rest ∧ CapRelA (capA st s x) y.1 := by + have h03 : ¬ (0#usize ≥ 3#usize) := by decide + unfold tcb.extra at he + simp only [h03, ↓reduceIte, exAtA_idx0, bind_ok, obj.Slot.is_null] at he + by_cases h0 : exAtA extra 0 = 0#u64 + · exfalso + simp only [h0, decide_true, ↓reduceIte] at he + cases ok_inj_ff he + simp only [h0, decide_false, Bool.false_eq_true, ↓reduceIte, bind_ok] at he + have ex : exAtA extra 0 = x := Option.some.inj (ok_inj_ff he) + cases excaps with + | nil => exact absurd (hx.2.2 0 (by simp) (by omega)) h0 + | cons y rest => + obtain ⟨ps, -, -, -, hrel⟩ := hx.2.1 0 (by simp) + refine ⟨y, rest, rfl, ?_⟩ + simpa only [List.getElem_cons_zero, ex, capAtA_eq (st := st)] using hrel + +/-- `tcb::extra_with_length` succeeding gives the extra slot and the length bound. -/ +theorem tcbExtraLenA {len buf slot x m : U64} {call : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (he : tcb.extra_with_length ⟨len, buf, call, slot, extra⟩ 0#usize m = ok (.Ok x)) : + tcb.extra ⟨len, buf, call, slot, extra⟩ 0#usize = ok (some x) ∧ m.val ≤ len.val := by + have h03 : ¬ (0#usize ≥ 3#usize) := by decide + have hx : ∃ o, tcb.extra ⟨len, buf, call, slot, extra⟩ 0#usize = ok o := by + unfold tcb.extra + simp only [h03, ↓reduceIte, exAtA_idx0, bind_ok, obj.Slot.is_null] + by_cases h0 : exAtA extra 0 = 0#u64 + · exact ⟨none, by simp [h0]⟩ + · exact ⟨some (exAtA extra 0), by simp [h0]⟩ + obtain ⟨o, ho⟩ := hx + unfold tcb.extra_with_length at he + rw [ho, bind_ok] at he + cases o with + | none => + simp only [error.truncated] at he + cases ok_inj_ff he + | some y => + dsimp only at he + by_cases h : len ≥ m + · rw [if_pos h] at he + have e : y = x := core.result.Result.Ok.inj (ok_inj_ff he) + exact ⟨by rw [ho, e], by scalar_tac⟩ + · rw [if_neg h] at he + simp only [error.truncated] at he + cases ok_inj_ff he + +/-- The first spec message word is FL4's `tcb::arg 0`. -/ +theorem tcbArg0A {s : S} {info : msginfo.MessageInfo} {len buf slot p : U64} {call : Bool} + {extra : Aeneas.Std.Array obj.Slot 3#usize} {mi : MessageInfo} {args : List Data} + (B : ArgsLabA st s info buf mi args) (hlnI : len = msgLenA info buf) (hlen : 1 ≤ len.val) + (hp : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 0#u64 = ok p) : ∃ rest, args = p.bv :: rest := by + obtain ⟨-, argsU, hae, hl, hargs⟩ := B + cases argsU with + | nil => + have : len.val = 0 := by rw [hlnI, hl]; rfl + omega + | cons y r => + have h := hargs 0#u64 y rfl + unfold tcb.arg at hp + rw [cur_thread_ok, bind_ok] at hp + have e : y = p := ok_inj_ff (h.symm.trans hp) + exact ⟨r.map UScalar.bv, by rw [hae, e]; rfl⟩ + +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s : S} {a : A} {σ : State Unit} {m : McState} + +/-- The invoked thread cap: the spec cap and the target's TCB. -/ +theorem JSyscallA.tcbTargetA (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + {slot t : U64} (hslot : slot ∈ OS) (hcap : capA st s slot = .Thread t) {c : Cap} + (hc : CapRelA (capA st s slot) c) : + c = .threadCap (specPtr t) ∧ t ∈ Z.T ∧ ∃ y, σ.kheap (specPtr t) = some (.tcb y) := by + have ht : t ∈ Z.T := J.tcbs ▸ (J.toJStage3A.slotCapTy hslot).1.2.2.2.2.2 t hcap + obtain ⟨y, hy, -⟩ := J.mcp t ht + have hc2 : CapEqA (.Thread t) c := by rw [← hcap]; exact hc.2 + exact ⟨(show absCapA (.Thread t) = c from hc2).symm, ht, y, hy⟩ + +/-- **The priority checks' spec side**: the extra thread cap, and `check_prio` against its MCP. -/ +theorem JSyscallA.tcbAuthA (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + {len buf slot p auth argSlot : U64} {call : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + {excaps : List (Cap × CslotPtr)} (hx : ExtraOKA Z.th (L.mem s) extra excaps) + (he : tcb.extra_with_length ⟨len, buf, call, slot, extra⟩ 0#usize 1#u64 = ok (.Ok argSlot)) + (harg : argSlot ∈ OS) (hac : capA st s argSlot = .Thread auth) (hle : ¬ L.mem s (mcpA auth) < p) : + ∃ sl0 rest, excaps = (.threadCap (specPtr auth), sl0) :: rest ∧ + (checkPrio (Z := Unit) p.bv (specPtr auth)).run σ = ({(.ok (), σ)}, False) ∧ 1 ≤ len.val := by + obtain ⟨he', hlen⟩ := tcbExtraLenA he + obtain ⟨⟨c1, sl0⟩, rest, ex, hrel⟩ := tcbExtraHeadA hx he' + have e1 : absCapA (.Thread auth) = c1 := by + have := hrel.2 + rw [hac] at this + exact this + have haT : auth ∈ Z.T := J.tcbs ▸ (J.toJStage3A.slotCapTy harg).1.2.2.2.2.2 auth hac + obtain ⟨y, hy, hw, -⟩ := J.mcp auth haT + refine ⟨sl0, rest, by rw [ex, ← e1]; rfl, ?_, by simpa using hlen⟩ + have hn : ¬ p.bv > ucast 64 y.tcbMcpriority := by + have h1 : p.val ≤ y.tcbMcpriority.toNat := by rw [← hw]; exact Nat.le_of_not_lt hle + have h2 : (ucast 64 y.tcbMcpriority).toNat = y.tcbMcpriority.toNat := by + have := y.tcbMcpriority.isLt + simp only [ucast, BitVec.toNat_setWidth] + exact Nat.mod_eq_of_lt (by omega) + rw [GT.gt, BitVec.lt_def, h2] + exact Nat.not_lt.2 h1 + unfold checkPrio + rw [run_bind_okA (run_liftEA (threadGet_eqSc Tcb.tcbMcpriority hy))] + simp only [NonDetE.whenE, hn, ↓reduceIte] + rfl + +end + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s : S} {a : A} {σ σ' : State Unit} {m : McState} + {call blk don : Bool} {mi : MessageInfo} {sl : CslotPtr} {c : Cap} {excaps : List (Cap × CslotPtr)} + {buffer : Option ObjRef} {args : List Data} {lbl len slot buf t p auth argSlot : U64} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + +/-- **UnbindNotification** (`TcbScopeA.unbindNtfn`). -/ +theorem JSyscallA.tcbUnbindNtfn_invSpecA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + (hslot : slot ∈ OS) (hcap : capA st s slot = .Thread t) (hc : CapRelA (capA st s slot) c) + (hlab : mi.miLabel = lbl.bv) (hlbl : lbl = fl4_abi.label.TCBUnbindNotification) + (hb : L.mem s (boundA t) ≠ 0#u64) {σ1 : State Unit} + (hsp : setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False)) + (hperf : (.ok [], σ') ∈ ((fl4InvokeTcb (.notificationControl (specPtr t) none)).run σ1).1) (cptr : CapRef) : + InvSpecA call blk don true mi.miLabel args cptr sl c excaps buffer σ σ' := by + obtain ⟨ec, ht, y, hy⟩ := J.tcbTargetA hslot hcap hc + have hbound : y.tcbBoundNotification = some (specPtr (L.mem s (boundA t))) := by + have h := (J.ep.at_tcb ht hy).bound + rw [h, show tBoundA t = boundA t from rfl] + unfold optRef + rw [if_neg hb] + have e : fl4LabelOf lbl.bv = .spec (.genInvocationLabel .tcbUnbindNotification) := by + rw [hlbl, fl4_abi.label.TCBUnbindNotification]; rfl + obtain ⟨hl, hg⟩ := tcbLabel_genA hlab e + refine InvSpecA.ofTcbNil (i := .notificationControl (specPtr t) none) ?_ hsp + (by cases blk <;> cases call <;> cases don <;> rfl) hperf + rw [ec] + refine tcbDec_genA hl rfl hg fun h => ?_ + unfold decodeTcbInvocation + rw [h] + dsimp only + unfold decodeUnbindNotification + dsimp only + rw [run_bind_okA (run_liftEA (show getTcbObjRef (Z := Unit) Tcb.tcbBoundNotification (specPtr t) σ = + ({(some (specPtr (L.mem s (boundA t))), σ)}, False) by + unfold getTcbObjRef; rw [threadGet_eqSc _ hy, hbound]))] + dsimp only + simp only [NonDetE.returnOk, ndE_pure_bindA] + rfl + +/-- **BindNotification** (`TcbScopeA.bindNtfn`). -/ +theorem JSyscallA.tcbBindNtfn_invSpecA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + {ns badge : U64} {send : Bool} + (hslot : slot ∈ OS) (hcap : capA st s slot = .Thread t) (hc : CapRelA (capA st s slot) c) + (hx : ExtraOKA Z.th (L.mem s) extra excaps) + (hlab : mi.miLabel = lbl.bv) (hlbl : lbl = fl4_abi.label.TCBBindNotification) + (he : tcb.extra ⟨len, buf, call, slot, extra⟩ 0#usize = ok (some ns)) + (hns : ns ∈ OS) (hncap : capA st s ns = .Notification p badge send true) + (hfree : L.mem s (boundA t) = 0#u64) (hq : obj.Ntfn.head st p s = ok 0#u64) + (hnfree : obj.Ntfn.bound_tcb st p s = ok 0#u64) {σ1 : State Unit} + (hsp : setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False)) + (hperf : (.ok [], σ') ∈ ((fl4InvokeTcb (.notificationControl (specPtr t) (some (specPtr p)))).run σ1).1) + (cptr : CapRef) : + InvSpecA call blk don true mi.miLabel args cptr sl c excaps buffer σ σ' := by + obtain ⟨ec, ht, y, hy⟩ := J.tcbTargetA hslot hcap hc + have hbound : y.tcbBoundNotification = none := by + have h := (J.ep.at_tcb ht hy).bound + rw [h, show tBoundA t = boundA t from rfl, hfree] + rfl + have hpN : p ∈ N := by + have hH := (J.toJStage3A.slotCapTy hns).1.1 + rw [hncap] at hH + exact J.ipcHeapCov.ntfn p (hH.2.1 p badge send true rfl).2 + obtain ⟨⟨c1, sl0⟩, rest, ex, hrel⟩ := tcbExtraHeadA hx he + have e1 : absCapA (.Notification p badge send true) = c1 := by + have := hrel.2 + rw [hncap] at this + exact this + have ex' : excaps = (.notificationCap (specPtr p) badge.bv (rightsOfA send true false false), sl0) :: rest := by + rw [ex, ← e1]; rfl + obtain ⟨x, hk, hr⟩ := J.ntfn.ntfn p hpN + have hnb : x.ntfnBoundTcb = none := by + have h := eq_of_spec_fd (Notif.obj.Ntfn.bound_tcb.spec (st := st) s p) + rw [hnfree] at h + rw [hr.bound, ← ok_inj_ff h] + rfl + have hhead : L.mem s (epHeadA p) = 0#u64 := by + have e : core.num.U64.wrapping_add p layout.ntfn.HEAD = epHeadA p := by + unfold epHeadA layout.ntfn.HEAD layout.ep.HEAD + rfl + unfold obj.Ntfn.head at hq + simp only [lift, bind_ok, e] at hq + rw [show st.read s (epHeadA p) = ok (L.mem s (epHeadA p)) from L.read_eq s _] at hq + exact ok_inj_ff hq + have hobj : x.ntfnObj = .idleNtfn ∨ ∃ b, x.ntfnObj = .activeNtfn b := by + have h := hr.obj + cases hn : x.ntfnObj with + | idleNtfn => exact Or.inl rfl + | activeNtfn b => exact Or.inr ⟨b, rfl⟩ + | waitingNtfn q => + exfalso + rw [hn] at h + obtain ⟨-, xs, hne, -, Q, -⟩ := h + exact hne ((Q.empty_iff J.iplaced.n.tcb).1 hhead) + have e : fl4LabelOf lbl.bv = .spec (.genInvocationLabel .tcbBindNotification) := by + rw [hlbl, fl4_abi.label.TCBBindNotification]; rfl + obtain ⟨hl, hg⟩ := tcbLabel_genA hlab e + refine InvSpecA.ofTcbNil (i := .notificationControl (specPtr t) (some (specPtr p))) ?_ hsp + (by cases blk <;> cases call <;> cases don <;> rfl) hperf + rw [ec] + refine tcbDec_genA hl rfl hg fun h => ?_ + unfold decodeTcbInvocation + rw [h] + dsimp only + unfold decodeBindNotification + rw [ex'] + simp only [List.length_cons, Nat.succ_ne_zero, NonDetE.whenE, ↓reduceIte, NonDetE.returnOk, ndE_pure_bindA, + List.head?_cons, the_someA] + rw [run_bind_okA (run_liftEA (show getTcbObjRef (Z := Unit) Tcb.tcbBoundNotification (specPtr t) σ = + ({(none, σ)}, False) by unfold getTcbObjRef; rw [threadGet_eqSc _ hy, hbound]))] + have hrecv : ¬ (AllowRecv ∉ rightsOfA send true false false) := by + cases send <;> simp [rightsOfA, AllowRecv] + dsimp only [Fl4SpecA64.the] + simp only [ndE_pure_bindA, hrecv, NonDetE.whenE, ↓reduceIte, NonDetE.returnOk] + rw [run_bind_okA (run_liftEA (getNotification_eq hk))] + rcases hobj with h | ⟨b, h⟩ <;> rw [h, hnb] <;> rfl + +/-- **SetMCPriority** (`TcbScopeA.setMcp`). -/ +theorem JSyscallA.tcbSetMcp_invSpecA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + {info : msginfo.MessageInfo} + (hslot : slot ∈ OS) (hcap : capA st s slot = .Thread t) (hc : CapRelA (capA st s slot) c) + (hx : ExtraOKA Z.th (L.mem s) extra excaps) (B : ArgsLabA st s info buf mi args) (hlnI : len = msgLenA info buf) + (hlab : mi.miLabel = lbl.bv) (hlbl : lbl = fl4_abi.label.TCBSetMCPriority) + (he : tcb.extra_with_length ⟨len, buf, call, slot, extra⟩ 0#usize 1#u64 = ok (.Ok argSlot)) + (hp : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 0#u64 = ok p) + (harg : argSlot ∈ OS) (hac : capA st s argSlot = .Thread auth) (hle : ¬ L.mem s (mcpA auth) < p) + {σ1 : State Unit} (hsp : setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False)) + (hperf : ∀ κs : CslotPtr, (.ok [], σ') ∈ ((fl4InvokeTcb (.threadControlSched (specPtr t) κs none + (some (BitVec.ofNat 8 p.val, specPtr auth)) none none)).run σ1).1) (cptr : CapRef) : + InvSpecA call blk don true mi.miLabel args cptr sl c excaps buffer σ σ' := by + obtain ⟨ec, -, -⟩ := J.tcbTargetA hslot hcap hc + obtain ⟨sl0, rest, ex, hchk, hlen⟩ := J.tcbAuthA hx he harg hac hle + obtain ⟨arest, ea⟩ := tcbArg0A B hlnI hlen hp + have eu : ucast 8 p.bv = BitVec.ofNat 8 p.val := by + apply BitVec.eq_of_toNat_eq + simp [ucast] + have e : fl4LabelOf lbl.bv = .spec (.genInvocationLabel .tcbSetMCPriority) := by + rw [hlbl, fl4_abi.label.TCBSetMCPriority]; rfl + obtain ⟨hl, hg⟩ := tcbLabel_genA hlab e + refine InvSpecA.ofTcbNil (i := .threadControlSched (specPtr t) sl none + (some (BitVec.ofNat 8 p.val, specPtr auth)) none none) ?_ hsp + (by cases blk <;> cases call <;> cases don <;> rfl) (hperf sl) + rw [ec] + refine tcbDec_genA hl rfl hg fun h => ?_ + unfold decodeTcbInvocation + rw [h] + dsimp only + unfold decodeSetMcpriority + rw [ea, ex] + simp only [List.length_cons, Nat.succ_ne_zero, or_self, NonDetE.whenE, ↓reduceIte, NonDetE.returnOk, + ndE_pure_bindA, List.getElem?_cons_zero] + dsimp only [Fl4SpecA64.the] + rw [run_bind_okA hchk, eu] + rfl + +/-- **SetPriority** (the five `TcbScopeA.setPrio*` arms: they differ in the target's state only). -/ +theorem JSyscallA.tcbSetPrio_invSpecA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + {info : msginfo.MessageInfo} + (hslot : slot ∈ OS) (hcap : capA st s slot = .Thread t) (hc : CapRelA (capA st s slot) c) + (hx : ExtraOKA Z.th (L.mem s) extra excaps) (B : ArgsLabA st s info buf mi args) (hlnI : len = msgLenA info buf) + (hlab : mi.miLabel = lbl.bv) (hlbl : lbl = fl4_abi.label.TCBSetPriority) + (he : tcb.extra_with_length ⟨len, buf, call, slot, extra⟩ 0#usize 1#u64 = ok (.Ok argSlot)) + (hp : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 0#u64 = ok p) + (harg : argSlot ∈ OS) (hac : capA st s argSlot = .Thread auth) (hle : ¬ L.mem s (mcpA auth) < p) + {σ1 : State Unit} (hsp : setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False)) + (hperf : ∀ κs : CslotPtr, (.ok [], σ') ∈ ((fl4InvokeTcb (.threadControlSched (specPtr t) κs none none + (some (BitVec.ofNat 8 p.val, specPtr auth)) none)).run σ1).1) (cptr : CapRef) : + InvSpecA call blk don true mi.miLabel args cptr sl c excaps buffer σ σ' := by + obtain ⟨ec, -, -⟩ := J.tcbTargetA hslot hcap hc + obtain ⟨sl0, rest, ex, hchk, hlen⟩ := J.tcbAuthA hx he harg hac hle + obtain ⟨arest, ea⟩ := tcbArg0A B hlnI hlen hp + have eu : ucast 8 p.bv = BitVec.ofNat 8 p.val := by + apply BitVec.eq_of_toNat_eq + simp [ucast] + have e : fl4LabelOf lbl.bv = .spec (.genInvocationLabel .tcbSetPriority) := by + rw [hlbl, fl4_abi.label.TCBSetPriority]; rfl + obtain ⟨hl, hg⟩ := tcbLabel_genA hlab e + refine InvSpecA.ofTcbNil (i := .threadControlSched (specPtr t) sl none none + (some (BitVec.ofNat 8 p.val, specPtr auth)) none) ?_ hsp + (by cases blk <;> cases call <;> cases don <;> rfl) (hperf sl) + rw [ec] + refine tcbDec_genA hl rfl hg fun h => ?_ + unfold decodeTcbInvocation + rw [h] + dsimp only + unfold decodeSetPriority + rw [ea, ex] + simp only [List.length_cons, Nat.succ_ne_zero, or_self, NonDetE.whenE, ↓reduceIte, NonDetE.returnOk, + ndE_pure_bindA, List.getElem?_cons_zero] + dsimp only [Fl4SpecA64.the] + rw [run_bind_okA hchk, eu] + rfl + +/-- **SetTLSBase** (`TcbScopeA.setTls`, `setTlsSelf`). -/ +theorem JSyscallA.tcbSetTls_invSpecA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + {info : msginfo.MessageInfo} {v : U64} + (hslot : slot ∈ OS) (hcap : capA st s slot = .Thread t) (hc : CapRelA (capA st s slot) c) + (B : ArgsLabA st s info buf mi args) (hlnI : len = msgLenA info buf) + (hlab : mi.miLabel = lbl.bv) (hlbl : lbl = fl4_abi.label.TCBSetTLSBase) (hlen : ¬ len < 1#u64) + (hv : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 0#u64 = ok v) + {σ1 : State Unit} (hsp : setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False)) + (hperf : (.ok [], σ') ∈ ((fl4InvokeTcb (.setTLSBase (specPtr t) v.bv)).run σ1).1) (cptr : CapRef) : + InvSpecA call blk don true mi.miLabel args cptr sl c excaps buffer σ σ' := by + obtain ⟨ec, -, -⟩ := J.tcbTargetA hslot hcap hc + obtain ⟨arest, ea⟩ := tcbArg0A B hlnI (by scalar_tac) hv + have e : fl4LabelOf lbl.bv = .spec (.genInvocationLabel .tcbSetTLSBase) := by + rw [hlbl, fl4_abi.label.TCBSetTLSBase]; rfl + obtain ⟨hl, hg⟩ := tcbLabel_genA hlab e + refine InvSpecA.ofTcbNil (i := .setTLSBase (specPtr t) v.bv) ?_ hsp + (by cases blk <;> cases call <;> cases don <;> rfl) hperf + rw [ec] + refine tcbDec_genA hl rfl hg fun h => ?_ + unfold decodeTcbInvocation + rw [h] + dsimp only + unfold decodeSetTlsBase + rw [ea] + simp only [List.length_cons, Nat.succ_ne_zero, NonDetE.whenE, ↓reduceIte, NonDetE.returnOk, + ndE_pure_bindA, List.getElem?_cons_zero, the_someA] + have eu : ucast 64 v.bv = v.bv := by simp [ucast] + rw [eu] + rfl + +/-- **Resume** (`TcbScopeA.resumeRunnable`, `TcbScopeA.resumeStopped`). -/ +theorem JSyscallA.tcbResume_invSpecA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + (hslot : slot ∈ OS) (hcap : capA st s slot = .Thread t) (hc : CapRelA (capA st s slot) c) + (hlab : mi.miLabel = lbl.bv) (hlbl : lbl = fl4_abi.label.TCBResume) {σ1 : State Unit} + (hsp : setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False)) + (hperf : (.ok [], σ') ∈ ((fl4InvokeTcb (.resume (specPtr t))).run σ1).1) (cptr : CapRef) : + InvSpecA call blk don true mi.miLabel args cptr sl c excaps buffer σ σ' := by + obtain ⟨ec, -, -⟩ := J.tcbTargetA hslot hcap hc + have e : fl4LabelOf lbl.bv = .spec (.genInvocationLabel .tcbResume) := by + rw [hlbl, fl4_abi.label.TCBResume]; rfl + obtain ⟨hl, hg⟩ := tcbLabel_genA hlab e + refine InvSpecA.ofTcbNil (i := .resume (specPtr t)) ?_ hsp + (by cases blk <;> cases call <;> cases don <;> rfl) hperf + rw [ec] + refine tcbDec_genA hl rfl hg fun h => ?_ + unfold decodeTcbInvocation + rw [h] + rfl + + +/-- **Suspend** (`TcbScopeA.suspend`). -/ +theorem JSyscallA.tcbSuspend_invSpecA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + (hslot : slot ∈ OS) (hcap : capA st s slot = .Thread t) (hc : CapRelA (capA st s slot) c) + (hlab : mi.miLabel = lbl.bv) (hlbl : lbl = fl4_abi.label.TCBSuspend) {σ1 : State Unit} + (hsp : setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False)) + (hperf : (.ok [], σ') ∈ ((fl4InvokeTcb (.suspend (specPtr t))).run σ1).1) (cptr : CapRef) : + InvSpecA call blk don true mi.miLabel args cptr sl c excaps buffer σ σ' := by + obtain ⟨ec, -, -⟩ := J.tcbTargetA hslot hcap hc + have e : fl4LabelOf lbl.bv = .spec (.genInvocationLabel .tcbSuspend) := by + rw [hlbl, fl4_abi.label.TCBSuspend]; rfl + obtain ⟨hl, hg⟩ := tcbLabel_genA hlab e + refine InvSpecA.ofTcbNil (i := .suspend (specPtr t)) ?_ hsp + (by cases blk <;> cases call <;> cases don <;> rfl) hperf + rw [ec] + refine tcbDec_genA hl rfl hg fun h => ?_ + unfold decodeTcbInvocation + rw [h] + rfl + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3InvSpecTcbTopA.lean b/proofs/Fl4ProofsA64/P3/Stage3InvSpecTcbTopA.lean new file mode 100644 index 000000000..5f8615e2f --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3InvSpecTcbTopA.lean @@ -0,0 +1,148 @@ +/- +P3 (aarch64, A64-P3 Lift 1c, TCB assembly): **`TcbScopeA.invSpecA`**, the TCB group's `InvSpecA` over the whole of +`TcbScopeA` (slice 1: `P3/Stage3InvSpecTcbA`; slice 2: `P3/Stage3InvSpecTcb2A`; Resume of a stopped thread, ReadFPU and Suspend reuse +the Resume, FPU-transfer and Suspend lemmas there; SetIPCBuffer, SetTimeoutEndpoint and SetSchedParams: +`P3/Stage3InvSpecTcb3A`). + +Each arm: its decode succeeds (`TcbScopeA.soundOk`, so the invocation is done), the arm's `Out` gives the restart +and the perform, and the arm's theorem gives the spec decode of the actual cap, label, message words and extra cap. +Needs the front end's length (`len = msgLenA info buf`) and the IPC buffer view (SetPACKeys reads the caller's +registers and buffer words), and `slot`'s spec name `sl` (the arms that name the invoked slot). + +Named premises: those of the arms. New module. +-/ +import Fl4ProofsA64.P3.Stage3InvSpecTcb3A +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {ar : machine.Arch A} {s : S} {a : A} {σ : State Unit} {m : McState} + +set_option maxHeartbeats 4000000 in +/-- **The TCB group's `InvSpecA`** (module docstring). -/ +theorem TcbScopeA.invSpecA [PlainStore st] (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) + s a σ m) + (hfm : A64FrameMasks st s) + {lbl len slot : U64} {blk call don fp : Bool} {buf : U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} + {Out : State Unit → Prop} + (h : TcbScopeA st AR Z OS ar s a σ Bf lbl len slot blk call don fp buf extra Out) + {r : core.result.Result Unit error.Exc} {s' : S} {a' : A} {σ' : State Unit} + (hd : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don fp buf extra = + ok (r, s', a')) (hOut : Out σ') + {done : Bool} (hdone : (done = true ∧ r = .Ok ()) ∨ (done = false ∧ r = .Err error.Exc.Preempted)) + {info : msginfo.MessageInfo} {mi : MessageInfo} (hlab : mi.miLabel = lbl.bv) {sl : CslotPtr} {c : Cap} + {excaps : List (Cap × CslotPtr)} {buffer : Option ObjRef} {args : List Data} + (hc : CapRelA (capA st s slot) c) (hx : ExtraOKA Z.th (L.mem s) extra excaps) + (B : ArgsLabA st s info buf mi args) (hlnI : len = msgLenA info buf) + (hBV : BufViewA (L.mem s) σ.machineState.underlyingMemory buf buffer) + (hmrs : getMrs (Z := Unit) σ.curThread buffer mi σ = ({(args, σ)}, False)) (cptr : CapRef) (hsl : slot = cslotAddrA sl) (hab : AbsSlotA Z.th sl) : + InvSpecA call blk don done mi.miLabel args cptr sl c excaps buffer σ σ' := by + have hdn : done = true := by + obtain ⟨s0, a0, σ0, e0, -⟩ := TcbScopeA.soundOk hA hM hPl J hfm h + have e : r = .Ok () := (Prod.mk.inj (okinj (hd.symm.trans e0))).1 + rcases hdone with ⟨h2, -⟩ | ⟨-, h2⟩ + · exact h2 + · rw [e] at h2; cases h2 + subst hdn + cases h with + | unbindNtfn hCs h1 hslot hcov hcap hext hSC hlbl hb => + obtain ⟨xc, σ1, -, -, hsp, hperf⟩ := hOut + exact J.tcbUnbindNtfn_invSpecA hslot hcap hc hlab hlbl hb hsp hperf cptr + | setMcp hCs h1 hslot hcov hcap hext hSC hlbl he hp harg hac hle => + obtain ⟨xc, σ1, -, -, hsp, hperf⟩ := hOut + exact J.tcbSetMcp_invSpecA hslot hcap hc hx B hlnI hlab hlbl he hp harg hac hle hsp hperf cptr + | setPrioInactive hCs h1 hslot hcov hcap hext hSC hlbl he hp harg hac hle hy hin => + obtain ⟨xc, σ1, -, -, hsp, hperf⟩ := hOut + exact J.tcbSetPrio_invSpecA hslot hcap hc hx B hlnI hlab hlbl he hp harg hac hle hsp hperf cptr + | setPrioPlain hCs h1 hslot hcov hcap hext hSC hlbl he hp harg hac hle hy htc hpl => + obtain ⟨xc, σ1, -, -, hsp, hperf⟩ := hOut + exact J.tcbSetPrio_invSpecA hslot hcap hc hx B hlnI hlab hlbl he hp harg hac hle hsp hperf cptr + | setPrioEp hCs h1 hslot hcov hcap hext hSC hlbl he hp harg hac hle hmb hmax htu hy htc hb => + obtain ⟨xc, σ1, -, -, hsp, hperf⟩ := hOut + exact J.tcbSetPrio_invSpecA hslot hcap hc hx B hlnI hlab hlbl he hp harg hac hle hsp hperf cptr + | setPrioNtfn hCs h1 hslot hcov hcap hext hSC hlbl he hp harg hac hle hmb hmax htu hy htc hb => + obtain ⟨xc, σ1, -, -, hsp, hperf⟩ := hOut + exact J.tcbSetPrio_invSpecA hslot hcap hc hx B hlnI hlab hlbl he hp harg hac hle hsp hperf cptr + | setPrioQueued hCs h1 hslot hcov hcap hext hSC hlbl he hp harg hac hle hy hrun hqc => + obtain ⟨xc, σ1, -, -, hsp, hperf⟩ := hOut + exact J.tcbSetPrio_invSpecA hslot hcap hc hx B hlnI hlab hlbl he hp harg hac hle hsp hperf cptr + | bindNtfn hCs h1 hmb hmax htu hslot hcov hcap hext hSC hlbl he hns hncap hfree hq hnfree => + obtain ⟨xc, σ1, -, -, hsp, hperf⟩ := hOut + exact J.tcbBindNtfn_invSpecA hslot hcap hc hx hlab hlbl he hns hncap hfree hq hnfree hsp hperf cptr + | setTls hCs h1 hslot hcov hcap hext hSC hlbl hlen hv hne => + obtain ⟨xc, σ1, -, -, hsp, hperf⟩ := hOut + exact J.tcbSetTls_invSpecA hslot hcap hc B hlnI hlab hlbl hlen hv hsp hperf cptr + | setTlsSelf hCs h1 hslot hcov hcap hext hSC hlbl hlen hv hself => + obtain ⟨xc, σ1, -, -, hsp, hperf⟩ := hOut + exact J.tcbSetTls_invSpecA hslot hcap hc B hlnI hlab hlbl hlen hv hsp hperf cptr + | setPacKeys hCs h1 hslot hcov hcap hext hSC hlbl k0 k1 k2 k3 k4 k5 k6 k7 k8 k9 hlen hflags hmask hprobe hsupported + hepoch hk hbuf g0 g1 g2 g3 g4 g5 g6 g7 g8 g9 => + obtain ⟨xc, hkc, -, hsp, hinv⟩ := hOut + exact J.tcbSetPacKeys_invSpecA hslot hcap hc B hlnI hBV hmrs hlab hlbl hlen hflags hmask hsupported hkc hsp hinv cptr + | resumeRunnable hCs h1 hslot hcov hcap hext hlbl hy hrun => + obtain ⟨xc, hkc, -, hsp, hperf⟩ := hOut + exact J.tcbResume_invSpecA hslot hcap hc hlab hlbl hsp hperf cptr + | writeFpu hCs h1 hslot hcov hcap hext hSC hlbl he hlen hoff hfcap hread halign hroom hflush hsan hrel hk howner hfs => + obtain ⟨xc, σ1, -, -, hsp, hperf⟩ := hOut + exact J.tcbFpu_invSpecA hslot hcap hc hx B hlnI hlab (hlbl.trans (if_neg Bool.false_ne_true).symm) he hlen hoff + hfcap hread (fun h => nomatch h) halign hroom hsp hperf cptr + | setFlags hCs h1 hX hslot hcov hcap hext hSC hlbl hlen hclear hset hfl hrel hk howner => + obtain ⟨xc, σ1, σ2, d, -, -, hsp, hinv, hcur, hrun⟩ := hOut + exact J.tcbSetFlags_invSpecA hslot hcap hc B hlnI hlab hlbl hlen hclear hset hsp hinv hcur hrun cptr + + | resumeStopped hCs h1 hmb hmbF hmax htu hslot hcov hcap hext hSC hlbl hsl hy hstop => + obtain ⟨xc, σ1, -, -, hsp, hperf⟩ := hOut + exact J.tcbResume_invSpecA hslot hcap hc hlab hlbl hsp hperf cptr + | readFpu hCs h1 hslot hcov hcap hext hSC hlbl he hlen hoff hfcap hwrite halign hbits hroom hbuf hflush hk howner + hfs => + obtain ⟨xc, σ1, -, -, hsp, hperf⟩ := hOut + have hrd : capability.VM_READ_WRITE ≠ capability.VM_KERNEL_ONLY := by + have h1 : capability.VM_KERNEL_ONLY.val = 1 := by unfold capability.VM_KERNEL_ONLY; rfl + have h3 : capability.VM_READ_WRITE.val = 3 := by unfold capability.VM_READ_WRITE; rfl + intro e + have := congrArg (fun x : U64 => x.val) e + simp [h1, h3] at this + exact J.tcbFpu_invSpecA hslot hcap hc hx B hlnI hlab (hlbl.trans (if_pos rfl).symm) he hlen hoff hfcap + (hwrite.symm ▸ hrd) (fun _ => hwrite) halign hroom hsp hperf cptr + | suspend hCs h1 hmb hmax htu hslot hcov hcap hext hSC hlbl => + obtain ⟨xc, σ1, -, -, hsp, hperf⟩ := hOut + exact J.tcbSuspend_invSpecA hslot hcap hc hlab hlbl hsp hperf cptr + | setIpcBufZero hCnt P hCl hCs h1 hslot hcov hcap hext hSC hlbl he harg hne hdec hrr => + obtain ⟨xc, σ1, -, -, hsp, hperf⟩ := hOut + exact J.tcbSetIpcBufZero_invSpecA hslot hcap hc hx B hlnI hlab hlbl he harg hsp hperf cptr + | setIpcBuf hCnt P hCl hCs h1 hslot hcov hcap hext hSC hlbl he harg hbn hbal hsrcF hbits hag hAκ hκ hAsrc hκsrc + hdec hrr => + obtain ⟨xc, σ1, -, -, hsp, hperf⟩ := hOut + exact J.tcbSetIpcBuf_invSpecA hslot hcap hc hx B hlnI hlab hlbl he harg hbn hbal hsrcF hsl hab hAκ hκ hAsrc hκsrc + hsp hperf cptr + | setTimeoutNull hCnt P hCl hCs h1 hslot hcov hcap hext hlbl he hnullh hdec hrr => + obtain ⟨xc, σ1, -, -, hsp, hperf⟩ := hOut + exact J.tcbSetTimeoutNull_invSpecA hslot hcap hc hx hlab hlbl he hnullh hsp hperf cptr + | setTimeoutNonNull hCnt P hCl hCs h1 hslot hcov hcap hext hSC hlbl he hnn hValid hAκ hκ hAsrc hκsrc hdec hrr => + obtain ⟨xc, σ1, -, -, hsp, hperf⟩ := hOut + exact J.tcbSetTimeoutNonNull_invSpecA hslot hcap hc hx hlab hlbl he hValid hsl hab hAκ hκ hAsrc hκsrc hsp hperf + cptr + | setSchedParamsNull hCnt P hCl hCs h1 hmb hmax htu hslot hcov hcap hext hSC hlbl he0 he1 he2 hmcp hprio harg hac + hle1 hle2 hsc hold hfh hne hAκ hκ hdec hrr => + obtain ⟨xc, σ1, -, -, hsp, hperf⟩ := hOut + exact J.tcbSspNull_invSpecA hslot hcap hc hx B hlnI hlab hlbl hext he0 he1 he2 hmcp hprio harg hac hle1 hle2 hsc + hold hfh hne hsp hperf cptr + | setSchedParamsEp hCnt P hCl hCs h1 hmb hmax htu hslot hcov hcap hext hSC hlbl he0 he1 he2 hmcp hprio harg hac + hle1 hle2 hsc hold hnn hValid hne hAκ hκ hAsrc hκsrc hdec hrr => + obtain ⟨xc, σ1, -, -, hsp, hperf⟩ := hOut + exact J.tcbSspEp_invSpecA hslot hcap hc hx B hlnI hlab hlbl hext he0 he1 he2 hmcp hprio harg hac hle1 hle2 hsc + hold hValid hne hsl hab hAκ hκ hAsrc hκsrc hsp hperf cptr +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3InvSpecTopA.lean b/proofs/Fl4ProofsA64/P3/Stage3InvSpecTopA.lean new file mode 100644 index 000000000..be4259585 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3InvSpecTopA.lean @@ -0,0 +1,137 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Plan B): **the assembly of the groups' `InvSpecA`** over the dispatch scope +(`DispScope4A`), in the shape of the `InvSpecA` clause of `JSyscallA.invocationA`'s `hScope`: a provider of `hScope` +gives a scope member and then this theorem. + +Named premises: those of the scope's arms. New module. +-/ +import Fl4ProofsA64.P3.Stage3WrapA +import Fl4ProofsA64.P3.Stage3InvSpecVsA +import Fl4ProofsA64.P3.Stage3InvSpecCnA +import Fl4ProofsA64.P3.Stage3InvSpecIpcA +import Fl4ProofsA64.P3.Stage3InvSpecScA +import Fl4ProofsA64.P3.Stage3InvSpecTcbTopA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {Clock : A → Fl4SpecA64.MachineState → Prop} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s : S} {a : A} {σ : State Unit} {m : McState} + +/-- **The caller's restart at the syscall-entry record** (the spec side of `JEntA.restartA`). -/ +theorem JSyscallA.setRestartA (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) : + ∀ x, σ.kheap σ.curThread = some (.tcb x) → + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), stUpdA σ σ.curThread x .restart)}, False) := by + intro x hk + have E0 := J.toJEntA + have q := E0.sq + obtain ⟨-, -, -, -, hS⟩ := E0.sys.ent.bxi + set t := L.curW s with ht_def + have hct : t ∈ H.tcbs := q.curT + have htZ : t ∈ Z.T := by rw [← E0.tcbs]; exact hct + have hcT : σ.curThread = specPtr t := E0.j.1.sch.curThread + obtain ⟨x', hk', -, hscx⟩ := E0.j.1.sch.tcb t htZ + rw [hcT] at hk + have ex : x' = x := by + have h := hk'.symm.trans hk + injection h with h; injection h + subst ex + have hw0 : L.mem s (scA t) ≠ 0#u64 := J.cur.2 + have hwC : L.mem s (scA t) ∈ Z.C := E0.j.1.rest.scMem t htZ hw0 + have hwH : L.mem s (scA t) ∈ H.scs := by rw [E0.scs]; exact hwC + have hrm : L.rmax s (L.mem s (scA t)) ≠ 0#u64 := (hS.act _ hwH).1 (J.toJStage3A.curSc_act J.actW hw0) + have hq : specPtr t ∉ σ.releaseQueue := by + rw [E0.j.1.rest.rel] + intro hm + obtain ⟨r, hr, er⟩ := List.mem_map.1 hm + have e : r = t := specPtr_inj er + subst e + have haf := q.inR _ q.cur _ hr + exact ((q.released _ hct).2 (by rw [haf]; exact hr)) J.cur.1 + obtain ⟨y, k, ey, eyr⟩ := E0.j.1.rest.refillMax _ hwC + have hsch : schedulable (specPtr t) (stUpdA σ (specPtr t) x' .restart) = true := by + refine schedulable_ofA (x := { x' with tcbState := .restart }) (by simp [setObjA]) rfl + (sc := scRenA m (L.mem s (scA t))) + (by show x'.tcbSchedContext = _; rw [hscx]; unfold optScA; simp [hw0]) + (stUpd_keep hk ey (fun _ h => by cases h)) ?_ hq + rw [eyr] + have e2 : L.rmax s (L.mem s (scA t)) = L.mem s (core.num.U64.wrapping_add (L.mem s (scA t)) 72#u64) := by + unfold LawfulStore.rmax; rw [maxA_eqA] + rw [e2] at hrm + exact Nat.pos_of_ne_zero (fun e => hrm (UScalar.eq_of_val_eq (by simpa using e))) + rw [hcT] + exact setThreadState_eqA σ (specPtr t) x' hk .restart (fun _ _ => hsch) + +/-- **`InvSpecA` over the dispatch scope** (module docstring). -/ +theorem DispScope4A.invSpecA [PlainStore st] {ar : machine.Arch A} (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + {info : msginfo.MessageInfo} {slot buf : U64} {blk call don fp : Bool} + {extra : Aeneas.Std.Array obj.Slot 3#usize} {Out : State Unit → Prop} + (h : DispScope4A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ m Clock hPl.ipa info.label + (msgLenA info buf) slot blk call don fp buf extra Out) + (hinfo : msg.msg_info st s (L.curW s) = ok info) {cptr : CapRef} + (r : core.result.Result Unit error.Exc) (s' : S) (a' : A) (σ' : State Unit) + (hd : syscall.decode_invocation st ar s a info.label (msgLenA info buf) slot (capA st s slot) blk call don fp buf + extra = ok (r, s', a')) (hOut : Out σ') + (done : Bool) (hdone : (done = true ∧ r = .Ok ()) ∨ (done = false ∧ r = .Err error.Exc.Preempted)) + (mi : MessageInfo) (sl : CslotPtr) (c : Cap) (excaps : List (Cap × CslotPtr)) (buffer : Option ObjRef) + (args : List Data) + (hmi : getMessageInfo (Z := Unit) σ.curThread σ = ({(mi, σ)}, False)) + (_front : (frontSpecA σ.curThread cptr mi).run σ = ({(.ok (sl, c, excaps, buffer), σ)}, False)) + (hsl : slot = cslotAddrA sl) (hc : CapRelA (capA st s slot) c) + (_hok : ExtraOKA Z.th (L.mem s) extra excaps) (hab : AbsSlotA Z.th sl) + (hBV : BufViewA (L.mem s) σ.machineState.underlyingMemory buf buffer) + (gc : getCap (Z := Unit) sl σ = ({(c, σ)}, False)) + (eib : lookupIpcBuffer (Z := Unit) false σ.curThread σ = ({(buffer, σ)}, False)) + (hex : (lookupExtraCaps (Z := Unit) σ.curThread buffer mi).run σ = ({(.ok excaps, σ)}, False)) + (gargs : getMrs (Z := Unit) σ.curThread buffer mi σ = ({(args, σ)}, False)) : + InvSpecA call blk don done mi.miLabel args cptr sl c excaps buffer σ σ' := by + have F : FrontPinA Z.th σ slot mi sl c excaps buffer args := ⟨hmi, hsl, hab, gc, eib, hex, gargs⟩ + have hst := J.setRestartA + have done_of : r = .Ok () → done = true := fun e => by + rcases hdone with ⟨h, -⟩ | ⟨-, h⟩ + · exact h + · rw [e] at h; cases h + have B := J.argsLabA hinfo hBV hmi gargs + rcases h with h | h | h | h | h | h + · obtain ⟨s0, a0, σ0, e0, -⟩ := VsScopeA.soundOk hA hM hPl J hfm h + rw [done_of (Prod.mk.inj (okinj (hd.symm.trans e0))).1] + exact VsScopeA.invSpecA J.j.1.place h hst hOut F cptr + · rcases CnScopeA.soundOk hA hM hPl J hfm h with ⟨s0, a0, σ0, e0, -⟩ | ⟨s0, a0, σ0, e0, -, -, -, -, inv, hpin, hO⟩ + · have er := (Prod.mk.inj (okinj (hd.symm.trans e0))).1 + rw [done_of er] + refine CnScopeA.invSpecA J.j.1.place h hst hOut F cptr fun s2 a2 e2 => ?_ + rw [e2, er] at hd + cases (Prod.mk.inj (okinj hd)).1 + · have er := (Prod.mk.inj (okinj (hd.symm.trans e0))).1 + have hdf : done = false := by + rcases hdone with ⟨-, h⟩ | ⟨h, -⟩ + · rw [er] at h; cases h + · exact h + subst hdf + obtain ⟨σ1, hset, hperf, pre⟩ := hO σ' hOut + refine InvSpecA.ofPinPre J.j.1.place hpin F cptr hset ?_ pre + have cn : fl4PerformInvocation (Z := Unit) blk call don (.invokeCNode inv) = + (do fl4InvokeCnode inv; Fl4Spec.NonDetE.returnOk []) := by + cases blk <;> cases call <;> cases don <;> rfl + rw [cn, ExceptT.run_bind] + exact mem_bindA.2 ⟨(.error (), σ'), hperf, Set.mem_singleton _⟩ + · cases h + · exact IpcScopeA.invSpecA hA hM hPl J hfm h hinfo hBV hd hOut hdone F hc cptr + · exact ScScopeA.invSpecA hA hM hPl J hfm h hd hOut hdone B.1 hc _hok B rfl cptr + · exact TcbScopeA.invSpecA hA hM hPl J hfm h hd hOut hdone B.1 hc _hok B rfl hBV gargs cptr hsl hab + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3InvSpecVsA.lean b/proofs/Fl4ProofsA64/P3/Stage3InvSpecVsA.lean new file mode 100644 index 000000000..0fac04dad --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3InvSpecVsA.lean @@ -0,0 +1,128 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Plan B): **the Vs group's `InvSpecA`**, from the constructors' pins (`DecPinA`) and +the arm's outcome. + +Named premises: those of the scope's arms. New module. +-/ +import Fl4ProofsA64.P3.Stage3DispVsA +import Fl4ProofsA64.P3.DelSpecA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +set_option maxHeartbeats 8000000 in +/-- **The Vs group's `InvSpecA`** (module docstring). -/ +theorem VsScopeA.invSpecA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {ar : machine.Arch A} {nA : U64} {s : S} {a : A} {σ : State Unit} {m : McState} + {kb : U64} (hP : PlaceA Z.th kb) + {lbl len slot : U64} {blk call don fp : Bool} {buf : U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} + {Out : State Unit → Prop} + (h : VsScopeA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar nA s a σ m lbl len slot blk call don fp buf + extra Out) + (hst : ∀ x, σ.kheap σ.curThread = some (.tcb x) → + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), stUpdA σ σ.curThread x .restart)}, False)) + {σ' : State Unit} (hOut : Out σ') + {mi : MessageInfo} {sl : CslotPtr} {c : Cap} {excaps : List (Cap × CslotPtr)} {buffer : Option ObjRef} + {args' : List Data} (F : FrontPinA Z.th σ slot mi sl c excaps buffer args') (cptr : CapRef) : + InvSpecA call blk don true mi.miLabel args' cptr sl c excaps buffer σ σ' := by + have flags : ∀ (i : Fl4ArchInvocation), + fl4PerformInvocation (Z := Unit) blk call don (.invokeArchObject i) = fl4ArchPerformInvocation i := fun _ => by + cases blk <;> cases call <;> cases don <;> rfl + have cn : ∀ (i : CnodeInvocation), fl4PerformInvocation (Z := Unit) blk call don (.invokeCNode i) = + (do fl4InvokeCnode i; Fl4Spec.NonDetE.returnOk []) := fun _ => by + cases blk <;> cases call <;> cases don <;> rfl + induction h with + | @frameMapS1 s3 a3 fp asid0 va0 rt sz dev mp bB bD bF K hnvm cte hab hcap FP slbl hl length buf args hlen hargs call extra x0 hx0 exs hex hS1 hHi hdec hcov hext hSC hpin => + obtain ⟨x, ivp, -, hset, hdec, hperf⟩ := hOut + refine InvSpecA.ofPin hP (hpin ivp hdec) F cptr hset ?_ + rw [flags] + exact liftE_memA hperf + | @frameUnmapS1 bB bD bF K hnvm hTO length buf call ex cte hab fp rt asid va sz dev h0 hcap hS1 hcov hext hSC hpin => + obtain ⟨x, -, hset, hperf⟩ := hOut + refine InvSpecA.ofPin hP hpin F cptr hset ?_ + rw [flags] + exact liftE_memA hperf + | @frameUnmapX bB bD bF K hnvm hTO hEO hIpa length buf call ex cte hab fp rt asid va sz dev isEpt h0 hcap k hkA hcov hext hSC hpin => + obtain ⟨x, -, hset, hperf⟩ := hOut + refine InvSpecA.ofPin hP hpin F cptr hset ?_ + rw [flags] + exact liftE_memA hperf + | @frameUnmapZ bB bD bF hnvm length buf call ex cte hab fp rt va sz dev hcap hcov hext hSC hpin => + obtain ⟨x, -, hset, hperf⟩ := hOut + refine InvSpecA.ofPin hP hpin F cptr hset ?_ + rw [flags] + exact liftE_memA hperf + | @frameUnmapS2Z bB bD bF hnvm length buf call ex cte hab fp rt va sz dev hcap hcov hext hSC hpin => + obtain ⟨x, -, hset, hperf⟩ := hOut + refine InvSpecA.ofPin hP hpin F cptr hset ?_ + rw [flags] + exact liftE_memA hperf + | @frameUnmapU bB bD bF hnvm length buf call ex cte hab fp rt asid va sz dev isEpt hcap hcov hext hSC hpin hcs => + obtain ⟨x, hx, hperf⟩ := hOut + obtain ⟨σ1, hset⟩ : ∃ σ1, σ1 = stUpdA σ σ.curThread x .restart := ⟨_, rfl⟩ + refine InvSpecA.ofPin hP hpin F cptr (σ1 := σ1) ?_ ?_ + · rw [hset]; exact hst x hx + · rw [flags] + refine liftE_memA ?_ + have e : performPgInvUnmap (Z := Unit) (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev none) cte σ1 = + setCap (Z := Unit) (.archObjectCap (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev none)) cte σ1 := by + have hg := hcs x hx + rw [← hset] at hg + unfold performPgInvUnmap + rw [nd_bind_oneA (show (Fl4Spec.NonDet.assert (σ := State Unit) + ((ArchCap.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev none).isFrameCap = true)) σ1 = + ({((), σ1)}, False) from by simp [Fl4Spec.NonDet.assert, ArchCap.isFrameCap])] + dsimp only [ArchCap.acapMapData] + rw [nd_bind_oneA hg] + rfl + unfold performPageInvocation + refine mem_bindA.2 ⟨((), σ'), ?_, Set.mem_singleton _⟩ + rw [e, hset] + exact hperf + | @ptMapS1 s3 a3 ptr asid0 va0 bB bC bD bF hnvm cte hab hcap UP hS2 hY slbl hl length buf args hlen hargs extra x0 hx0 exs hex hS1 hHi hdec hcov hext hSC hpin => + obtain ⟨x, ivp, -, hset, hdec, hperf⟩ := hOut + refine InvSpecA.ofPin hP (hpin ivp hdec) F cptr hset ?_ + rw [flags] + refine liftE_memA ?_ + exact mem_bindA.2 ⟨_, hperf, Set.mem_singleton _⟩ + | @ptUnmapS1 bB bC bD bF K hnvm hTO hEO length buf ex cte hab ptr asid va hpS hcap hf hS1 hcov hext hSC hpin => + obtain ⟨x, -, hset, hperf⟩ := hOut + refine InvSpecA.ofPin hP hpin F cptr hset ?_ + rw [flags] + refine liftE_memA ?_ + exact mem_bindA.2 ⟨_, hperf, Set.mem_singleton _⟩ + | @ptUnmapX bB bC bD bF K hnvm hTO hEO hIpa length buf ex cte hab ptr asid va hpS hcap hf k hkR hkR1 hcov hext hSC hpin => + obtain ⟨x, -, hset, hperf⟩ := hOut + refine InvSpecA.ofPin hP hpin F cptr hset ?_ + rw [flags] + exact liftE_memA hperf + | @ptUnmapU bB bC bD bF hnvm length buf ex cte hab ptr asid va hcap hf hcov hext hSC hpin => + obtain ⟨x, -, hset, hperf⟩ := hOut + refine InvSpecA.ofPin hP hpin F cptr hset ?_ + rw [flags] + refine liftE_memA ?_ + exact mem_bindA.2 ⟨_, hperf, Set.mem_singleton _⟩ + | @asidAssign hnvm cte hab p base hpool length buf block call cd fp ex dst hd0 hex k root oldAsid oldVa hcap htab hp0 i hsearch hfull hcov hext hSC hpin => + obtain ⟨x, -, hset, -, hperf⟩ := hOut + refine InvSpecA.ofPin hP hpin F cptr hset ?_ + rw [flags] + exact hperf + | @frameMapHi x0 D dc ex s3 a3 callD lenD bufD rr bB bD bF K hnvm hTO hEO cte hab fp rt asid va sz dev mp hcap FP p1 asid1 mp1 h16 hx0 hx0c vaddr rightsMask vm attr hvmEq hD d hres hcov hext hSC hx hlen h0 h1 h2 hr hvm hpin => + obtain ⟨x, iv, -, hset, hD, hperf⟩ := hOut + refine InvSpecA.ofPin hP (hpin iv hD) F cptr hset ?_ + rw [flags] + exact liftE_memA hperf + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3IpcBodyA.lean b/proofs/Fl4ProofsA64/P3/Stage3IpcBodyA.lean new file mode 100644 index 000000000..0ef14834a --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3IpcBodyA.lean @@ -0,0 +1,283 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Integration round 2): **the IPC arms in the dispatch top's shape** (`DispPostA` with +`Sched.BodyOKA`), one wrapper per arm of lift 2b whose spec step changes nothing past the caller's `Restart`. + +* `JSyscallA.sendNoblockDispA`: `sendNoblockA`; `JSyscallA.replyDetachedDispA`: `replyDetachedA`. Both end at the + caller's `Restart`, so `BodyEndA` is `BodyEndA.ofKeep` with `BodyKeepA.refl`. + +* `JSyscallA.sendBlockDispA`: `sendBlockA` (blocking send / call with no receiver; its body end is the arm's). + +* The woken-receiver sends (`JSyscallA.sendRecvDispA`, `sendDonateDispA`, `sendNoDonDispA`, + `faultNoDonRelA` for the fault delivery) and the waking Replies (`replyLiveDispA`, `replyFaultDispA`): the arm exports `Sched.WakeEndA` and `BodyEndA.ofWakeA` closes the body + end from the end record. + +No named premise beyond those of the arms. New module. +-/ +import Fl4ProofsA64.P3.Stage3SendNoblockA +import Fl4ProofsA64.P3.Stage3ReplyDetachedA +import Fl4ProofsA64.P3.Stage3TcbBodyA +import Fl4ProofsA64.P3.Stage3SendRecvArmA +import Fl4ProofsA64.P3.Stage3SendDonateArmA +import Fl4ProofsA64.P3.Stage3SendNoDonArmA +import Fl4ProofsA64.P3.Stage3FaultNoDonArmA +import Fl4ProofsA64.P3.Stage3ReplyArmA +import Fl4ProofsA64.P3.Stage3ReplyFaultArmA +import Fl4ProofsA64.P3.Stage3SendBlkArmA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s : S} {a : A} {σ : State Unit} {m : McState} + +/-- **Non-blocking send with no receiver in the dispatch top's shape.** -/ +theorem JSyscallA.sendNoblockDispA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {p badge slot lbl len buf : U64} {recv grant grantReply call don fp : Bool} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) + (hcap : capA st s slot = .Endpoint p badge true recv grant grantReply) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) + (hp : p ∈ E) {x : Endpoint} (hk : σ.kheap (specPtr p) = some (.endpoint x)) (hc : CanBlockA false x) : + DispPostA st AR ar nA n Z.Cs s a m lbl len slot false call don fp buf extra (fun σ' => + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ')}, False) ∧ + sendIpc (Z := Unit) false call badge.bv grant grantReply don σ.curThread (specPtr p) σ' = + ({((), σ')}, False)) := by + obtain ⟨xc, σ1, s1, hkc, e1, hsp, hspec, hdec, J1⟩ := + J.sendNoblockA (lbl := lbl) (len := len) (buf := buf) (call := call) (don := don) (fp := fp) hslot hcov hcap hext + hp hk hc + have B : BodyEndA σ σ1 m := BodyEndA.ofKeep J hkc (e1 ▸ BodyKeepA.refl _) + exact ⟨.Ok (), s1, a, σ1, hdec, ⟨hsp, hspec⟩, curW_keptA J.toJStage3A J1 B.2, A64RelS3.ofJ J1 B.1, (by intro e; cases e)⟩ + +/-- **Reply on a detached reply object in the dispatch top's shape.** -/ +theorem JSyscallA.replyDetachedDispA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {r slot lbl len buf : U64} {grant blk call don fp : Bool} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Reply r grant) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) + (hr : r ∈ Rp) {x : Reply} (hk : σ.kheap (specPtr r) = some (.reply x)) (hdet : x.replyTcb = none) : + DispPostA st AR ar nA n Z.Cs s a m lbl len slot blk call don fp buf extra (fun σ' => + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ')}, False) ∧ + doReplyTransfer (Z := Unit) σ.curThread (specPtr r) grant σ' = ({((), σ')}, False)) := by + obtain ⟨xc, σ1, s1, hkc, e1, hsp, hspec, hdec, J1⟩ := + J.replyDetachedA (lbl := lbl) (len := len) (buf := buf) (blk := blk) (call := call) (don := don) (fp := fp) hslot + hcov hcap hext hr hk hdet + have B : BodyEndA σ σ1 m := BodyEndA.ofKeep J hkc (e1 ▸ BodyKeepA.refl _) + exact ⟨.Ok (), s1, a, σ1, hdec, ⟨hsp, hspec⟩, curW_keptA J.toJStage3A J1 B.2, A64RelS3.ofJ J1 B.1, (by intro e; cases e)⟩ + +/-- **Blocking send / call with no receiver waiting, in the dispatch top's shape.** -/ +theorem JSyscallA.sendBlockDispA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p badge slot lbl len buf : U64} {recv grant grantReply call don fp : Bool} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) + (hcap : capA st s slot = .Endpoint p badge true recv grant grantReply) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hp : p ∈ E) {x : Endpoint} (hk : σ.kheap (specPtr p) = some (.endpoint x)) (hc : CanBlockA false x) : + DispPostA st AR ar hPl.ipa n Z.Cs s a m lbl len slot true call don fp buf extra (fun σ' => + ∃ σ1, setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ((), σ') ∈ (sendIpc (Z := Unit) true call badge.bv grant grantReply don σ.curThread (specPtr p) σ1).1) := by + obtain ⟨-, σ1, σ2, s2, -, -, hsp, hspec, hdec, Q, Rl, J2, B⟩ := + J.sendBlockA (lbl := lbl) (len := len) (buf := buf) (call := call) (don := don) (fp := fp) hA hM hPl hfm hCs h1 + hmb hmax htu hslot hcov hcap hext hSC hp hk hc + exact ⟨_, _, _, σ2, hdec, ⟨σ1, hsp, by rw [hspec]; exact Set.mem_singleton _⟩, curW_keptA J.toJStage3A J2 B.2, + A64RelS3.ofJ J2 B.1, (by intro e; cases e)⟩ + +/-- **Send to a waiting receiver (plain, or a call without grant) in the dispatch top's shape.** -/ +theorem JSyscallA.sendRecvDispA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p badge slot lbl len buf : U64} {recv grant grantReply blk call don fp : Bool} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) + (hcap : capA st s slot = .Endpoint p badge true recv grant grantReply) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hp : p ∈ E) {d : U64} {q : List ObjRef} + (hk : σ.kheap (specPtr p) = some (.endpoint (.recvEP (specPtr d :: q)))) + (hsnd : ∀ x, σ.kheap σ.curThread = some (.tcb x) → x.tcbFault = none ∧ (grant = false ∨ + ipcBufOfA false x = none ∨ + unat (dataToMessageInfo (x.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0)) + (hbr0 : (call = false ∧ don = false) ∨ (call = true ∧ grant = false ∧ grantReply = false)) : + DispPostA st AR ar hPl.ipa n Z.Cs s a m lbl len slot blk call don fp buf extra (fun σ' => + ∃ σ1, setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ((), σ') ∈ (sendIpc (Z := Unit) blk call badge.bv grant grantReply don σ.curThread (specPtr p) σ1).1) := by + obtain ⟨-, σ1, σ2, s2, -, -, hsp, hspec, hdec, W, Q, Rl, J2⟩ := + J.sendRecvA (lbl := lbl) (len := len) (buf := buf) (blk := blk) (fp := fp) hA hM hPl hfm hCs h1 hmb hmax htu hslot + hcov hcap hext hSC hp hk hsnd hbr0 + have B := Sched.BodyEndA.ofWakeA J J2 W + exact ⟨_, _, _, σ2, hdec, ⟨σ1, hsp, hspec⟩, curW_keptA J.toJStage3A J2 B.2, A64RelS3.ofJ J2 B.1, (by intro e; cases e)⟩ + +/-- **The donating call to a waiting receiver in the dispatch top's shape.** -/ +theorem JSyscallA.sendDonateDispA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p badge slot lbl len buf : U64} {recv grant grantReply blk fp : Bool} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) + (hcap : capA st s slot = .Endpoint p badge true recv grant grantReply) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hp : p ∈ E) {d : U64} {q : List ObjRef} + (hk : σ.kheap (specPtr p) = some (.endpoint (.recvEP (specPtr d :: q)))) + (hsnd : ∀ x, σ.kheap σ.curThread = some (.tcb x) → x.tcbFault = none ∧ (grant = false ∨ + ipcBufOfA false x = none ∨ + unat (dataToMessageInfo (x.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0)) + (hg : grant = true ∨ grantReply = true) + (hrcv : ∀ y, σ.kheap (specPtr d) = some (.tcb y) → + y.tcbSchedContext = none ∧ ∃ e r dd, y.tcbState = .blockedOnReceive e (some r) dd) + (hV : L.mem s (core.num.U64.wrapping_add (L.curW s) 3664#u64) = 0#u64) : + DispPostA st AR ar hPl.ipa n Z.Cs s a m lbl len slot blk true true fp buf extra (fun σ' => + ∃ σ1, setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ((), σ') ∈ (sendIpc (Z := Unit) blk true badge.bv grant grantReply true σ.curThread (specPtr p) σ1).1) := by + obtain ⟨-, σ1, σ2, s2, -, -, hsp, hspec, hdec, W, Q, Rl, r, -, J2⟩ := + J.sendDonateA (lbl := lbl) (len := len) (buf := buf) (blk := blk) (fp := fp) hA hM hPl hfm hCs h1 hmb hmax htu + hslot hcov hcap hext hSC hp hk hsnd hg hrcv hV + have B := Sched.BodyEndA.ofWakeA J J2 W + exact ⟨_, _, _, σ2, hdec, ⟨σ1, hsp, hspec⟩, curW_keptA J.toJStage3A J2 B.2, A64RelS3.ofJ J2 B.1, (by intro e; cases e)⟩ + +/-- **The non-donating call to a waiting receiver in the dispatch top's shape.** -/ +theorem JSyscallA.sendNoDonDispA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p badge slot lbl len buf : U64} {recv grant grantReply blk fp : Bool} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) + (hcap : capA st s slot = .Endpoint p badge true recv grant grantReply) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hp : p ∈ E) {d : U64} {q : List ObjRef} + (hk : σ.kheap (specPtr p) = some (.endpoint (.recvEP (specPtr d :: q)))) + (hsnd : ∀ x, σ.kheap σ.curThread = some (.tcb x) → x.tcbFault = none ∧ (grant = false ∨ + ipcBufOfA false x = none ∨ + unat (dataToMessageInfo (x.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0)) + (hg : grant = true ∨ grantReply = true) + (hrcv : ∀ y, σ.kheap (specPtr d) = some (.tcb y) → + y.tcbSchedContext ≠ none ∧ ∃ e r dd, y.tcbState = .blockedOnReceive e (some r) dd) : + DispPostA st AR ar hPl.ipa n Z.Cs s a m lbl len slot blk true true fp buf extra (fun σ' => + ∃ σ1, setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ((), σ') ∈ (sendIpc (Z := Unit) blk true badge.bv grant grantReply true σ.curThread (specPtr p) σ1).1) := by + obtain ⟨-, σ1, σ2, s2, -, -, hsp, hspec, hdec, W, Q, Rl, J2⟩ := + J.sendNoDonA (lbl := lbl) (len := len) (buf := buf) (blk := blk) (fp := fp) hA hM hPl hfm hCs h1 hmb hmax htu + hslot hcov hcap hext hSC hp hk hsnd hg hrcv + have B := Sched.BodyEndA.ofWakeA J J2 W + exact ⟨_, _, _, σ2, hdec, ⟨σ1, hsp, hspec⟩, curW_keptA J.toJStage3A J2 B.2, A64RelS3.ofJ J2 B.1, (by intro e; cases e)⟩ + +/-- **The fault delivery to a receiver with its own SC, with the body end** (`A64RelS3`). -/ +theorem JSyscallA.faultNoDonRelA + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {fe : error.Fault} {ty w0 w1 w2 : U64} {f : Fl4SpecA64.Fault} (F : UsrFaultA st (L.curW s) fe ty w0 w1 w2 f) + (hOk : ∀ (rbo : Option ObjRef) (x : Tcb), + FaultOKA (L := L) (faultWordsA st s (L.curW s) ty w0 w1 w2) (L.curW s) rbo x f) + {p badge : U64} {sd rv g gr : Bool} + (hcap : capA st s (slotIdx (L.curW s) layout.tcb.SLOT_FAULT_HANDLER) = .Endpoint p badge sd rv g gr) + (hgc : getCap (Z := Unit) (getTcbFaultHandlerPtr σ.curThread) σ = + ({(.endpointCap (specPtr p) badge.bv (rightsOfA sd rv g gr), σ)}, False)) + (hp : p ∈ E) {d : U64} {q : List ObjRef} + (hk : σ.kheap (specPtr p) = some (.endpoint (.recvEP (specPtr d :: q)))) + (hg : g = true ∨ gr = true) + (hrcv : ∀ y, σ.kheap (specPtr d) = some (.tcb y) → + y.tcbSchedContext ≠ none ∧ ∃ e r dd, y.tcbState = .blockedOnReceive e (some r) dd) : + ∃ σ2 s2, ((), σ2) ∈ (handleFault (Z := Unit) σ.curThread f σ).1 ∧ + faults.handle_fault st ar s a (L.curW s) fe = ok (s2, a) ∧ + L.curW s2 = L.curW s ∧ A64RelS3 st AR ar hPl.ipa n Z.Cs s2 a σ2 m := by + obtain ⟨σ2, s2, hm, hf, W, Q, Rl, J2⟩ := + J.faultNoDonA hA hPl hCs h1 hmb hmax htu F hOk hcap hgc hp hk hg hrcv + have B := Sched.BodyEndA.ofWakeA J J2 W + exact ⟨σ2, s2, hm, hf, curW_keptA J.toJStage3A J2 B.2, A64RelS3.ofJ J2 B.1⟩ + +/-- **The live Reply in the dispatch top's shape.** -/ +theorem JSyscallA.replyLiveDispA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hag : fl4MinBudget = MIN_BUDGET) (hmb : ∀ a, Fl4MinBudgetAgreeA ar a) + {r slot lbl len buf : U64} {grant blk call don fp : Bool} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Reply r grant) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hr : r ∈ Rp) {xr : Reply} (hk : σ.kheap (specPtr r) = some (.reply xr)) {d : U64} + (hcl : xr.replyTcb = some (specPtr d)) + (hsnd : ∀ x, σ.kheap σ.curThread = some (.tcb x) → x.tcbFault = none ∧ (grant = false ∨ + ipcBufOfA false x = none ∨ + unat (dataToMessageInfo (x.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0)) + (hrcv : ∀ y, σ.kheap (specPtr d) = some (.tcb y) → + y.tcbState = .blockedOnReply (specPtr r) ∧ y.tcbFault = none) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) (hconv : Fl4TicksToUsLegacyAgree) : + DispPostA st AR ar hPl.ipa n Z.Cs s a m lbl len slot blk call don fp buf extra (fun σ' => + ∃ σ1, setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ((), σ') ∈ (fl4DoReplyTransfer (Z := Unit) σ.curThread (specPtr r) grant σ1).1) := by + obtain ⟨-, σ1, σ2, s2, -, -, hsp, hspec, hdec, W, Q, Rl, stack', J2⟩ := + J.replyLiveA (lbl := lbl) (len := len) (buf := buf) (blk := blk) (call := call) (don := don) (fp := fp) hA hM hPl + hfm hCs h1 hag hmb hslot hcov hcap hext hSC hr hk hcl hsnd hrcv hmax htu hconv + have B := Sched.BodyEndA.ofWakeA J J2 W + exact ⟨_, _, _, σ2, hdec, ⟨σ1, hsp, hspec⟩, curW_keptA J.toJStage3A J2 B.2, A64RelS3.ofJ J2 B.1, (by intro e; cases e)⟩ + +/-- **The Reply to a faulted caller in the dispatch top's shape.** -/ +theorem JSyscallA.replyFaultDispA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hag : fl4MinBudget = MIN_BUDGET) (hmb : ∀ a, Fl4MinBudgetAgreeA ar a) + {r slot lbl len buf : U64} {grant blk call don fp : Bool} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Reply r grant) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hr : r ∈ Rp) {xr : Reply} (hk : σ.kheap (specPtr r) = some (.reply xr)) {d : U64} + (hcl : xr.replyTcb = some (specPtr d)) + {f : Fault} + (hrcv : ∀ y, σ.kheap (specPtr d) = some (.tcb y) → + y.tcbState = .blockedOnReply (specPtr r) ∧ y.tcbFault = some f) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) (hconv : Fl4TicksToUsLegacyAgree) + (hok : ∀ x, σ.kheap σ.curThread = some (.tcb x) → + FaultReplyOKA (L := L) s d + (unat (dataToMessageInfo (x.tcbArch.tcbContext.userRegs msg_info_register)).miLength) f) : + DispPostA st AR ar hPl.ipa n Z.Cs s a m lbl len slot blk call don fp buf extra (fun σ' => + ∃ σ1, setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ((), σ') ∈ (fl4DoReplyTransfer (Z := Unit) σ.curThread (specPtr r) grant σ1).1) := by + obtain ⟨-, σ1, σ2, s2, -, -, hsp, hspec, hdec, W, Q, Rl, stack', J2⟩ := + J.replyFaultA (lbl := lbl) (len := len) (buf := buf) (blk := blk) (call := call) (don := don) (fp := fp) hA hM hPl + hfm hCs h1 hag hmb hslot hcov hcap hext hSC hr hk hcl hrcv hmax htu hconv hok + have B := Sched.BodyEndA.ofWakeA J J2 W + exact ⟨_, _, _, σ2, hdec, ⟨σ1, hsp, hspec⟩, curW_keptA J.toJStage3A J2 B.2, A64RelS3.ofJ J2 B.1, (by intro e; cases e)⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3IpcFl4KeepA.lean b/proofs/Fl4ProofsA64/P3/Stage3IpcFl4KeepA.lean new file mode 100644 index 000000000..1de3154f6 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3IpcFl4KeepA.lean @@ -0,0 +1,295 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2b-2): **the FL4 side of the record fields across `send_ipc` to a waiting +receiver**, over `send_ipc_recvA`'s write set `IpcFpA` (`FpA`'s predicate: endpoint-code words or `MWrA`) and the +spec step `SIKA`. + +* **`JStage3A.tw_ipcA`**: the TCB words `of_ipcStepUA`'s `tw` asks for (below 224, 3440, 3472, 3512). +* **`JStage3A.runVcpu_ipcA`**: `RunVcpuInv` (the movers run no VCPU). +* **`JStage3A.ntfn_ipcA`**: `NtfnStateRel` at the new state, and **`JStage3A.blkQ_ipcA`**: `BlkQueuedA`. +* **`JStage3A.reply_ipcA`**: `ReplyStackRelA` across a step without donation and reply push (`SIKA … false`). + +`ntfn_ipcA` / `blkQ_ipcA` take `NtfnWaitKeepA`: the block word and `Ep` links of the TCBs waiting on a notification +are kept. `send_ipc_recvA`'s footprint `FpA` does not give it (`EWrite` covers the links and block word of every TCB +of `T`). New module. +-/ +import Fl4ProofsA64.P3.Stage3IpcSchedValidA +import Fl4ProofsA64.P3.Stage3SchedWrNA +import Fl4ProofsA64.P3.IpcScPushA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {A : Type} {AR' : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} + {κ : U64 → U64} {n : Nat} {J5 : JSlotA S} {R : Set U64 → JSlotA S} {s s' : S} {a : A} + {σ σ' : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} {i : Fl4SpecA64.ObjRef} + {M Nq : Fl4SpecA64.ObjRef → Prop} {don : Bool} + +/-- The write set of an IPC body: endpoint-code words or `MWrA` (`FpA`'s predicate). -/ +def IpcFpA (st : machine.Store S) [L : LawfulStore st] (Z : XSetsA) (E Rp : Set U64) + (Bf : Set (U64 × Fl4SpecA64.Word)) (m : Fl4SpecA64.McState) (σ : Fl4SpecA64.State Unit) (s : S) (b : U64) : Prop := + EWrite Z.T E Rp Bf b ∨ MWrA (st := st) Z m σ s b + +/-- A word of one of the reply objects (the donating reply push's extra words over `IpcFpA`). -/ +def RepWA (Rp : Set U64) (b : U64) : Prop := ∃ r ∈ Rp, r.val ≤ b.val ∧ b.val < r.val + 32 + +/-- The block word and `Ep` links of the TCBs waiting on a notification are kept. -/ +def NtfnWaitKeepA (T N : Set U64) (m m' : U64 → U64) : Prop := + ∀ y ∈ T, m (Notif.tBlockA y) ∈ N → + m' (Notif.tBlockA y) = m (Notif.tBlockA y) ∧ m' (prevA .Ep y) = m (prevA .Ep y) ∧ + m' (nextA .Ep y) = m (nextA .Ep y) + +/-- `NtfnWaitKeepA` at the waiters outside the movers `M`. -/ +def NtfnWaitKeepMA (M : Fl4SpecA64.ObjRef → Prop) (T N : Set U64) (m m' : U64 → U64) : Prop := + ∀ y ∈ T, ¬ M (specPtr y) → m (Notif.tBlockA y) ∈ N → + m' (Notif.tBlockA y) = m (Notif.tBlockA y) ∧ m' (prevA .Ep y) = m (prevA .Ep y) ∧ + m' (nextA .Ep y) = m (nextA .Ep y) + +theorem NtfnWaitKeepA.toM {M : Fl4SpecA64.ObjRef → Prop} {T N : Set U64} {m m' : U64 → U64} + (h : NtfnWaitKeepA T N m m') : NtfnWaitKeepMA M T N m m' := fun y hy _ hN => h y hy hN + +variable (J : JStage3A st AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7A st R) s a σ m) +include J + +/-- An IPC object's word is off `MWrA`. -/ +theorem JStage3A.ipc_offMwrA {b : U64} (hw : IpcObjWA N E Rp b) : ¬ MWrA (st := st) Z m σ s b := by + rintro (c | c | ⟨u, hu, e⟩) + · obtain ⟨p, len, hp, h1, h2⟩ := J.ipc_thA hw + obtain ⟨c0, hc, x, k, hx, ha⟩ := c + have := (J.sr.scp.out c0 hc x k hx _ ha).1 p _ hp + have hsz : ObjKindA.size (.other len) = len := rfl + omega + · have := J.ipc_ksA hw + unfold SKsA at c + unfold ksSizeA at this; omega + · have hin := J.sr.swr_tcb_in hu e + have huH : H u = some .tcb := by + have : u ∈ H.tcbs := by rw [J.tcbs]; exact hu + exact this + exact J.ipc_off_hobjA (v := u) (kv := .tcb) huH (by simp) ⟨hin.1, by simp only [HKind.bits]; omega⟩ hw + +/-- A TCB word the endpoint, scheduler and SC code do not write. -/ +theorem JStage3A.tcbWord_offIpcA {t K : U64} (ht : t ∈ Z.T) (hK : K.val < 4096) (hw : ¬ TWOffN K.val) + (h : K.val ≠ 3496 ∧ K.val ≠ 3504 ∧ K.val ≠ 3464 ∧ K.val ≠ 3480 ∧ K.val ≠ 3488 ∧ K.val ≠ 3592) : + ¬ IpcFpA st Z E Rp Bf m σ s (wa t K) := by + have hfit := (J.sr.core.tcbs t ht).fits + have hv : (wa t K).val = t.val + K.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have hth := (J.sr.thT t).2 ht + have hsz : ObjKindA.size .tcb = 4096 := rfl + have other : ∀ u ∈ Z.T, ∀ K' : U64, K'.val < 4096 → K.val ≠ K'.val → wa t K ≠ wa u K' := by + intro u hu K' k1 k2 h + have hfu := (J.sr.core.tcbs u hu).fits + have hv' : (wa u K').val = u.val + K'.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have h' := congrArg (fun w : U64 => w.val) h + simp only [hv, hv'] at h' + by_cases e : u = t + · subst e; omega + · have := J.sr.place.apart u t .tcb .tcb ((J.sr.thT u).2 hu) hth e + omega + rintro (c | c | c | ⟨u, hu, e⟩) + · exact not_ew_offA J.iplaced.e ht hK hw h.1 h.2.1 c + · obtain ⟨c0, hc, x, k, hx, ha⟩ := c + have := (J.sr.scp.out c0 hc x k hx _ ha).1 t _ hth + omega + · have := J.sr.tcb_ks ht + unfold SKsA at c + unfold ksSizeA at this; omega + · rcases e with e | e | e | e + · rw [scA_eq] at e; exact other u hu _ (by simp) (by simpa using h.2.2.1) e + · exact other u hu 3480#u64 (by simp) (by simpa using h.2.2.2.1) e + · exact other u hu 3488#u64 (by simp) (by simpa using h.2.2.2.2.1) e + · rw [inqA_eq] at e; exact other u hu _ (by simp) (by simpa using h.2.2.2.2.2) e + +theorem JStage3A.tcbKept_ipcA (w : WritesIn st s s' (fun b => IpcFpA st Z E Rp Bf m σ s b ∨ RepWA Rp b)) + {t K : U64} (ht : t ∈ Z.T) + (hK : K.val < 4096) (h8 : K.val % 8 = 0) (hw : ¬ TWOffN K.val) + (h : K.val ≠ 3496 ∧ K.val ≠ 3504 ∧ K.val ≠ 3464 ∧ K.val ≠ 3480 ∧ K.val ≠ 3488 ∧ K.val ≠ 3592) : + L.mem s' (wa t K) = L.mem s (wa t K) := + w.mem_eq (field_aligned (J.sr.core.tcbs t ht) hK h8) fun c => c.elim (J.tcbWord_offIpcA ht hK hw h) fun ⟨r, hr, c⟩ => by + have hfit := (J.sr.core.tcbs t ht).fits + have hv : (wa t K).val = t.val + K.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have := J.iplaced.e.rt r hr t ht + omega + +/-- **`of_ipcStepUA`'s `tw`.** -/ +theorem JStage3A.tw_ipcA (w : WritesIn st s s' (fun b => IpcFpA st Z E Rp Bf m σ s b ∨ RepWA Rp b)) : + ∀ t ∈ Z.T, ∀ O : U64, O.val < 224 ∨ O.val = 3440 ∨ O.val = 3472 ∨ O.val = 3512 ∨ O.val = 3456 → O.val % 8 = 0 → + L.mem s' (wa t O) = L.mem s (wa t O) := fun t ht O h h8 => + J.tcbKept_ipcA w ht (by omega) h8 (by unfold TWOffN; omega) (by omega) + +/-- The state word of a thread whose spec state is kept. -/ +theorem JStage3A.stKept_ipcA (K : SIKA i M Nq don σ σ') (E' : EpStateRel Z.T E Rp Bf (L.mem s') σ') {t : U64} + (ht : t ∈ Z.T) (hM : ¬ M (specPtr t)) : + L.mem s' (Fl4ProofsA64.P1.stA t) = L.mem s (Fl4ProofsA64.P1.stA t) := by + obtain ⟨x, hx, r⟩ := J.ep.tcb t ht + obtain ⟨y, hy, r'⟩ := E'.tcb t ht + obtain ⟨y', hy', g⟩ := K.tcbF hx + rw [hy] at hy' + cases hy' + refine UScalar.eq_of_val_eq ?_ + rw [r'.st, r.st, g.st.resolve_right fun c => hM c.1] + +/-- **`RunVcpuInv`** across the send: the movers run no VCPU. -/ +theorem JStage3A.runVcpu_ipcA (w : WritesIn st s s' (fun b => IpcFpA st Z E Rp Bf m σ s b ∨ RepWA Rp b)) (K : SIKA i M Nq don σ σ') + (E' : EpStateRel Z.T E Rp Bf (L.mem s') σ') + (hMv : ∀ t ∈ Z.T, M (specPtr t) → L.mem s (Fl4ProofsA64.P1.stA t) ≠ sched.TS_RUNNING_VM) : + RunVcpuInv st Z.T s' := fun t ht h0 => by + have e : L.mem s' (runVcpuA t) = L.mem s (runVcpuA t) := + J.tcbKept_ipcA w (K := 3672#u64) ht (by simp) (by simp) (by unfold TWOffN; simp) (by simp) + rw [e] at h0 + by_cases hM : M (specPtr t) + · exact absurd (J.runVcpu.zero ht (hMv t ht hM)) h0 + · rw [J.stKept_ipcA K E' ht hM]; exact J.runVcpu t ht h0 + +/-- **`NtfnStateRel` at the new state** (module docstring): `hMs` the movers wait on no notification before, `hMs'` +nor after. -/ +theorem JStage3A.ntfn_ipcMA (w : WritesIn st s s' (fun b => IpcFpA st Z E Rp Bf m σ s b ∨ RepWA Rp b)) (K : SIKA i M Nq don σ σ') + (E' : EpStateRel Z.T E Rp Bf (L.mem s') σ') (wk : NtfnWaitKeepMA M Z.T N (L.mem s) (L.mem s')) + (hMs : ∀ t ∈ Z.T, M (specPtr t) → ∀ x, σ.kheap (specPtr t) = some (.tcb x) → + ∀ p, x.tcbState ≠ .blockedOnNotification p) + (hMs' : ∀ t ∈ Z.T, M (specPtr t) → ∀ x, σ'.kheap (specPtr t) = some (.tcb x) → + ∀ p, x.tcbState ≠ .blockedOnNotification p) : + NtfnStateRel N Z.T (L.mem s') σ' := by + have hP := J.iplaced + have nw : ∀ q ∈ N, ∀ O : U64, O.val < 40 → O.val % 8 = 0 → L.mem s' (wa q O) = L.mem s (wa q O) := by + intro q hq O h1 h2 + have hna := hP.n.ntfn q hq + have hv : (wa q O).val = q.val + O.val := Notif.nfield_val hna (by omega) + refine w.mem_eq (Notif.nfield_aligned hna (by omega) h2) ?_ + have hw : IpcObjWA N E Rp (wa q O) := Or.inl ⟨q, hq, by omega, by omega⟩ + rintro ((c | c) | ⟨r, hr, c⟩) + · exact nw_not_efoot hP hq (by omega) (by omega) c.foot + · exact J.ipc_offMwrA hw c + · have hqH : H q = some .ntfn := J.utIpc.ntfn q hq + have hrH : H r = some .reply := J.utIpc.reply r hr + have := J.sys.ent.heap.disj q _ r _ hqH hrH (fun e => by rw [e, hrH] at hqH; cases hqH) + have hv : (wa q O).val = q.val + O.val := Notif.nfield_val hna (by omega) + simp only [HKind.bits] at this + omega + refine ⟨fun q hq => ?_, fun t ht => ?_⟩ + · obtain ⟨x, hx, hr⟩ := J.ntfn.ntfn q hq + refine ⟨x, K.ntfnF hx, hr.frame ?_ fun y _ hm => ?_⟩ + · refine ⟨?_, ?_, ?_, ?_, ?_⟩ + · rw [Notif.epHeadA_eq]; exact nw q hq _ (by simp) (by simp) + · rw [Notif.epTSA_eq]; exact nw q hq _ (by simp) (by simp) + · rw [Notif.nBadgeA_eq]; exact nw q hq _ (by simp) (by simp) + · rw [Notif.nBoundA_eq]; exact nw q hq _ (by simp) (by simp) + · rw [Notif.nScA_eq]; exact nw q hq _ (by simp) (by simp) + · obtain ⟨hyT, hy6, hyb⟩ := hr.obj.blocked hm + have hM : ¬ M (specPtr y) := fun c => by + obtain ⟨z, hz, rz⟩ := J.ntfn.tcb y hyT + have h6 := rz.st + rw [hy6] at h6 + cases hs : z.tcbState with + | blockedOnNotification p => exact hMs y hyT c z hz p hs + | ext e => rw [hs] at h6; cases e <;> simp [tsCode] at h6 + | _ => rw [hs] at h6; simp [tsCode] at h6 + obtain ⟨k1, k2, k3⟩ := wk y hyT hM (by rw [hyb]; exact hq) + exact ⟨J.stKept_ipcA K E' hyT hM, k1, k2, k3⟩ + · obtain ⟨x, hx, r⟩ := J.ntfn.tcb t ht + obtain ⟨y, hy, r'⟩ := E'.tcb t ht + refine ⟨y, hy, ⟨r'.st, fun p hp => ?_, ?_, r'.prio⟩⟩ + · have hM : ¬ M (specPtr t) := fun c => hMs' t ht c y hy p hp + obtain ⟨y', hy', g⟩ := K.tcbF hx + rw [hy] at hy' + cases hy' + have hs : x.tcbState = .blockedOnNotification p := by + rw [← g.st.resolve_right fun c => hM c.1]; exact hp + have hb := r.blk p hs + have hN : L.mem s (Notif.tBlockA t) ∈ N := (J.blkQ.ntfn t ht x hx p hs).1 + rw [(wk t ht hM hN).1]; exact hb + · rw [badgeRegA_eq_regA] + exact r'.regs _ + +/-- **`BlkQueuedA` at the new state**: the movers end blocked on neither an endpoint nor a notification. -/ +theorem JStage3A.blkQ_ipcMA (K : SIKA i M Nq don σ σ') (E' : EpStateRel Z.T E Rp Bf (L.mem s') σ') + (wk : NtfnWaitKeepMA M Z.T N (L.mem s) (L.mem s')) + (hMs' : ∀ t ∈ Z.T, M (specPtr t) → ∀ x, σ'.kheap (specPtr t) = some (.tcb x) → + Fl4SpecA64.epBlocked x.tcbState = none ∧ ∀ p, x.tcbState ≠ .blockedOnNotification p) : + BlkQueuedA Z.T E N (L.mem s') σ' := by + have kept : ∀ t ∈ Z.T, ∀ y, σ'.kheap (specPtr t) = some (.tcb y) → ¬ M (specPtr t) → + ∃ x, σ.kheap (specPtr t) = some (.tcb x) ∧ x.tcbState = y.tcbState := fun t _ y hy hM => by + obtain ⟨x, hx, g⟩ := K.tcbB hy + exact ⟨x, hx, (g.st.resolve_right fun c => hM c.1).symm⟩ + refine ⟨fun t ht y hy e hb => ?_, fun t ht y hy p hp => ?_⟩ + · have hM : ¬ M (specPtr t) := fun c => by rw [(hMs' t ht c y hy).1] at hb; cases hb + obtain ⟨x, hx, es⟩ := kept t ht y hy hM + obtain ⟨hE, z, hz, hm⟩ := J.blkQ.ep t ht x hx e (es ▸ hb) + obtain ⟨z', hz', hq⟩ := K.epF hz + obtain ⟨x1, hx1, r⟩ := J.ep.tcb t ht + obtain ⟨y1, hy1, r'⟩ := E'.tcb t ht + rw [hx] at hx1; cases hx1 + rw [hy] at hy1; cases hy1 + have eq : L.mem s' (Notif.tBlockA t) = L.mem s (Notif.tBlockA t) := + specPtr_inj ((r'.blk e hb).trans (r.blk e (es ▸ hb)).symm) + exact ⟨by rw [eq]; exact hE, z', hz', hq _ hm hM⟩ + · have hM : ¬ M (specPtr t) := fun c => (hMs' t ht c y hy).2 p hp + obtain ⟨x, hx, es⟩ := kept t ht y hy hM + obtain ⟨hN, z, q, hz, ho, hm⟩ := J.blkQ.ntfn t ht x hx p (es.trans hp) + exact ⟨by rw [(wk t ht hM hN).1]; exact hN, z, q, K.ntfnF hz, ho, hm⟩ + +/-- `ntfn_ipcMA` with every notification waiter kept. -/ +theorem JStage3A.ntfn_ipcA (w : WritesIn st s s' (fun b => IpcFpA st Z E Rp Bf m σ s b ∨ RepWA Rp b)) (K : SIKA i M Nq don σ σ') + (E' : EpStateRel Z.T E Rp Bf (L.mem s') σ') (wk : NtfnWaitKeepA Z.T N (L.mem s) (L.mem s')) + (hMs : ∀ t ∈ Z.T, M (specPtr t) → ∀ x, σ.kheap (specPtr t) = some (.tcb x) → + ∀ p, x.tcbState ≠ .blockedOnNotification p) + (hMs' : ∀ t ∈ Z.T, M (specPtr t) → ∀ x, σ'.kheap (specPtr t) = some (.tcb x) → + ∀ p, x.tcbState ≠ .blockedOnNotification p) : + NtfnStateRel N Z.T (L.mem s') σ' := + J.ntfn_ipcMA w K E' (NtfnWaitKeepA.toM wk) hMs hMs' + +/-- `blkQ_ipcMA` with every notification waiter kept. -/ +theorem JStage3A.blkQ_ipcA (K : SIKA i M Nq don σ σ') (E' : EpStateRel Z.T E Rp Bf (L.mem s') σ') + (wk : NtfnWaitKeepA Z.T N (L.mem s) (L.mem s')) + (hMs' : ∀ t ∈ Z.T, M (specPtr t) → ∀ x, σ'.kheap (specPtr t) = some (.tcb x) → + Fl4SpecA64.epBlocked x.tcbState = none ∧ ∀ p, x.tcbState ≠ .blockedOnNotification p) : + BlkQueuedA Z.T E N (L.mem s') σ' := + J.blkQ_ipcMA K E' (NtfnWaitKeepA.toM wk) hMs' + +/-- **`ReplyStackRelA`** across a send without donation and reply push. -/ +theorem JStage3A.reply_ipcA (w : WritesIn st s s' (IpcFpA st Z E Rp Bf m σ s)) (K : SIKA i M Nq false σ σ') : + ReplyStackRelA (L.mem s') Rp Z.C σ' (scNameA m) stack := by + refine J.reply.frame (fun r hr => ?_) (fun c _ x k h => ?_) (fun r _ y h => K.replyB h) + swap + · obtain ⟨y, hy, g⟩ := K.scF h + exact ⟨y, k, hy, g.reps rfl⟩ + have hrH : H r = some .reply := J.utIpc.reply r hr + obtain ⟨r32, -, rfit⟩ := J.iplaced.e.rep r hr + have one : ∀ o : U64, 8 ≤ o.val → o.val < 32 → o.val % 8 = 0 → L.mem s' (wa r o) = L.mem s (wa r o) := by + intro o h1 h2 h3 + have hv : (wa r o).val = r.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + refine w.mem_eq (by unfold Aligned; rw [hv]; omega) ?_ + have hw : IpcObjWA N E Rp (wa r o) := Or.inr (Or.inr ⟨r, hr, by omega, by omega⟩) + have other : ∀ r' ∈ Rp, r' ≠ r → r'.val + 32 ≤ r.val ∨ r.val + 32 ≤ r'.val := fun r' hr' ne => by + have := J.sys.ent.heap.disj r' _ r _ (J.utIpc.reply r' hr') hrH ne + simp only [HKind.bits] at this + omega + rintro (c | c) + · rcases c with ⟨e, he, c⟩ | ⟨r', hr', c⟩ | ⟨t, ht, c⟩ | c + · have := J.iplaced.e.er e he r hr; omega + · by_cases er : r' = r + · subst er; omega + · have := other r' hr' er; omega + · have := J.iplaced.e.rt r hr t ht + unfold TWF TL at c + rcases c with ⟨c1, c2⟩ | c + · unfold TWOffN at c2; omega + · omega + · have := J.iplaced.e.br _ c r hr; omega + · exact J.ipc_offMwrA hw c + exact ⟨one _ (by simp [layout.reply.NEXT]) (by simp [layout.reply.NEXT]) (by simp [layout.reply.NEXT]), + one _ (by simp [layout.reply.PREV]) (by simp [layout.reply.PREV]) (by simp [layout.reply.PREV])⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3IpcFootA.lean b/proofs/Fl4ProofsA64/P3/Stage3IpcFootA.lean new file mode 100644 index 000000000..9bdb0f834 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3IpcFootA.lean @@ -0,0 +1,111 @@ +/- +P3 (aarch64, A64-P3 Stage-3 integration, D8/T1 step 2): **from the endpoint footprint `FpA` to the Stage-3 write set +`SchedStepNWA`** (the `PartFrA.wr` shape of the timeout-send / IRQ-arm / `fire` frames). + +* **`ewrite_stepNA`**: with no IPC buffer (`Bf = ∅`), an endpoint-code word (`EWrite`) is an endpoint / reply word + (`IpcObjWA`) or a TCB word of `TcbStepWA` (offsets 256..551, 3328..3376, 3496, 3504: all in `[224, 4096)`, none of + the two VCPU links 3664 / 3672). +* **`FpA.stepN`**: a footprint `FpA … (MWrA)` with `Bf = ∅` is `WritesIn … (SchedStepNWA)`. + +No named premise. +-/ +import Fl4ProofsA64.P3.IpcScFootA +import Fl4ProofsA64.P3.Stage3SchedWrNA +import Fl4ProofsA64.P3.IpcScReplyTimeoutA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4ProofsA64.P3.Sched + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- A TCB word at an offset in `[224, 4096)` other than the two VCPU links is a `TcbStepWA` word. -/ +theorem tcbStepWA_of_off {T : Set U64} (hP : TcbPlaced T) {t b : U64} (ht : t ∈ T) (K : Nat) + (h1 : 224 ≤ K) (h2 : K < 4096) (h3 : K ≠ 3664) (h4 : K ≠ 3672) (e : b.val = t.val + K) : + TcbStepWA T b := by + have hfit := (hP t ht).fits + have hv : ∀ K : U64, K.val < 4096 → (wa t K).val = t.val + K.val := by + intro K hK; rw [wa_valm]; omega + refine ⟨t, ht, by omega, by omega, fun c => ?_, fun c => ?_⟩ + · have := congrArg UScalar.val c + rw [hv _ (by simp)] at this + simp at this + omega + · unfold runVcpuA at c + have := congrArg UScalar.val c + rw [hv _ (by simp)] at this + simp at this + omega + +/-- A TCB word of the endpoint code (no buffer) is a `TcbStepWA` word. -/ +theorem tcbStepWA_of_ew {T : Set U64} (hP : TcbPlaced T) {t b : U64} (ht : t ∈ T) + (h : TWF t b ∨ TL t b) : TcbStepWA T b := by + have hfit := (hP t ht).fits + have hv : ∀ K : U64, K.val < 4096 → (wa t K).val = t.val + K.val := by + intro K hK; rw [wa_valm]; omega + have key : ∀ K : Nat, 224 ≤ K → K < 4096 → K ≠ 3664 → K ≠ 3672 → b.val = t.val + K → + TcbStepWA T b := by + intro K h1 h2 h3 h4 e + refine ⟨t, ht, by omega, by omega, fun c => ?_, fun c => ?_⟩ + · have := congrArg UScalar.val c + rw [hv _ (by simp)] at this + simp at this + omega + · unfold runVcpuA at c + have := congrArg UScalar.val c + rw [hv _ (by simp)] at this + simp at this + omega + rcases h with ⟨h1, h2⟩ | h + · unfold TWOffN at h2 + rcases h2 with h2 | h2 | h2 | h2 | h2 | ⟨h2, h3⟩ | h2 <;> + exact key (b.val - t.val) (by omega) (by omega) (by omega) (by omega) (by omega) + · rcases h with h | h <;> + exact key _ (by omega) (by omega) (by omega) (by omega) h + +/-- **An endpoint-code word, with no IPC buffer, is a Stage-3 scheduler-step word.** -/ +theorem ewrite_stepNA {N E R : Set U64} {Z : XSetsA} {m : Fl4SpecA64.McState} {σ : Fl4SpecA64.State Unit} {s : S} + (hP : TcbPlaced Z.T) {b : U64} (h : EWrite Z.T E R ∅ b) : SchedStepNWA (st := st) N E R Z m σ s b := by + rcases h with ⟨e, he, h⟩ | ⟨r, hr, h⟩ | ⟨t, ht, h⟩ | ⟨bp, hbp, -⟩ + · exact Or.inr (Or.inr (Or.inl ⟨e, he, by omega, by omega⟩)) + · exact Or.inr (Or.inr (Or.inr ⟨r, hr, by omega, by omega⟩)) + · exact Or.inl (Or.inr (tcbStepWA_of_ew hP ht h)) + · exact (Set.notMem_empty _ hbp).elim + +/-- **The IPC footprint with no buffer is a Stage-3 write set.** -/ +theorem FpA.stepN {N E R : Set U64} {Z : XSetsA} {m : Fl4SpecA64.McState} {σ : Fl4SpecA64.State Unit} {s s' : S} + (hP : TcbPlaced Z.T) + (h : FpA st Z.T E R ∅ (fun σ s => MWrA (st := st) Z m σ s) σ s s') : + WritesIn st s s' (SchedStepNWA (st := st) N E R Z m σ s) := + h.mono fun _ hb => hb.elim (ewrite_stepNA hP) fun hw => Or.inl (Or.inl hw) + +/-- **The four stores of the timeout fault** (`faults.set_fault … (.Timeout b)`) are `TcbStepWA` words of the TCB. -/ +theorem timeoutFaultWordsA_wr {T : Set U64} (hP : TcbPlaced T) {t : U64} (ht : t ∈ T) (s : S) (badge : U64) : + WritesIn st s (timeoutFaultWordsA st s t badge) (TcbStepWA T) := by + have hta := hP t ht + have hfit := hta.fits + have hal : Aligned t := by have := hta.align; unfold Aligned; omega + have hv : ∀ K : U64, K.val < 4096 → (wa t K).val = t.val + K.val := by + intro K hK; rw [wa_valm]; omega + have hw : ∀ (K : U64) (s0 : S) (v : U64), K.val % 8 = 0 → K.val < 4096 → 224 ≤ K.val → K.val ≠ 3664 → K.val ≠ 3672 → + WritesIn st s0 (L.wr s0 (wa t K) v) (TcbStepWA T) := fun K s0 v h8 h4 h1 h2 h3 => + (writesIn_wr (wa_al hal h8)).mono fun b hb => by + subst hb + exact tcbStepWA_of_off hP ht K.val h1 h4 h2 h3 (hv K h4) + unfold timeoutFaultWordsA faultA tFw0A tFw1A tFw2A + refine (((hw 3376#u64 s _ (by simp) (by simp) (by simp) (by simp) (by simp)).trans + (hw 3384#u64 _ _ (by simp) (by simp) (by simp) (by simp) (by simp))).trans + ((hw 3392#u64 _ _ (by simp) (by simp) (by simp) (by simp) (by simp)).trans + (hw 3400#u64 _ _ (by simp) (by simp) (by simp) (by simp) (by simp)))).mono fun b hb => ?_ + rcases hb with (hb | hb) | (hb | hb) <;> exact hb + +end + +end + +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/Stage3IpcGlueA.lean b/proofs/Fl4ProofsA64/P3/Stage3IpcGlueA.lean new file mode 100644 index 000000000..09fc13eab --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3IpcGlueA.lean @@ -0,0 +1,284 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2): **the shared record glue of the IPC arms** (`JStage3A` across an operation body). + +* **`JStage3A.of_stepFinA`**: `JStage3A.of_schedWrN` with the facts every body gets the same way derived here: the + system invariants (`SysInvA`, from the final `BXI` / `SysL` / `UserOK` and the kept VSpace words), the scheduler + queues (`SchedQueuesInv`, from `BXI` and `SchedSRA`'s lists), `PoolOff`, `ScCovA`, `J5A` and `J7A`'s change of the + queue ghosts. The body supplies what it changes: `SchedSRA`, the endpoint / notification relations, reply stacks, + buffer coverage, `mcp`, yield, the spec invariants, `RunVcpuInv`, `BlkQueuedA`, `SchedValidA`. +* **`IpcKeepA`**: the TCB and SC fields no IPC body changes (fault, IPC frame and buffer, `mcp`, yield links). +* **`JStage3A.of_ipcStepA`**: `of_stepFinA` with buffer coverage, buffer alignment, idle fault, `mcp` and yield derived + from `IpcKeepA` and the kept TCB words (offsets below 224, 3440, 3472, 3512). + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3SchedWrNA +import Fl4ProofsA64.P3.Stage3FinA +import Fl4ProofsA64.P3.EpSendRecvA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-- **The scheduler invariant at `SchedCoreA`'s queue lists**: the lists memory holds. -/ +theorem SchedQueuesInv.reCoreA {T Cs Ω : Set U64} {H : Heap} {s : S} {Q Q' : U64 → U64 → List U64} + {R R' : U64 → List U64} (h : SchedQueuesInv st T Cs Ω H s Q' R') (c : SchedCoreA st T Cs s Q R) : + SchedQueuesInv st T Cs Ω H s Q R := by + have eQ : ∀ k ∈ Cs, ∀ p : U64, p.val < 256 → Q k p = Q' k p := fun k hk p hp => + QueueRel.unique c.tcbs ((c.ready k hk).1.rel p hp) ((h.ready k hk).1.rel p hp) + have eR : ∀ k ∈ Cs, R k = R' k := fun k hk => QueueRel.unique c.tcbs (c.release k hk) (h.release k hk) + exact + { tcbs := c.tcbs, nodes := c.nodes, cur := h.cur, aff := c.aff, prio := c.prio, ready := c.ready, + release := c.release, inQ := c.inQ, inR := c.inR, queued := c.queued, released := c.released, excl := c.excl, + action := h.action + runQ := fun k hk p hp t ht => h.runQ k hk p hp t (eQ k hk p hp ▸ ht) + runR := fun k hk t ht => h.runR k hk t (eR k hk ▸ ht) + actOK := fun ha => ⟨(h.actOK ha).1, fun k hk => eR k hk ▸ (h.actOK ha).2 k hk⟩ + eps := h.eps, curT := h.curT, idleT := h.idleT, ksOut := h.ksOut, omega := h.omega } + +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {ar : machine.Arch A} {nA : U64} {s s' : S} {a : A} {σ σ' : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} {Q : U64 → U64 → List U64} {Rl : U64 → List U64} + {curOf curScOf : Fl4SpecA64.CoreId → Fl4SpecA64.ObjRef} + +set_option maxHeartbeats 1000000 in +/-- **`JStage3A` across an operation body** (module docstring). -/ +theorem JStage3A.of_stepFinA {Ac' : Set U64} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {stack' : U64 → List U64} + (w : WritesIn st s s' (SchedStepNWA (st := st) N E Rp Z m σ s)) (HS : SpecSchedA σ σ') (SSz : SSzA σ σ') (Qk : QKeepA σ σ') + (hum : σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory) + (hcurOf : ∀ k, k ≠ m.curCore → curOf k = Z.curOf k) + (bxi : BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac' s') + (sysl : SysL st H Z.Cs OS ia ie s') (symL : SymL st H Z.Cs s') + (userOK : Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s') + (j7 : J7A st (J7R2A st ar nA) Z H OS s' σ' m) + (sr : SchedSRA st { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } m κ s' σ') + (ep : EpStateRel Z.T E Rp Bf (L.mem s') σ') + (ntfn : NtfnStateRel N Z.T (L.mem s') σ') + (reply : ReplyStackRelA (L.mem s') Rp Z.C σ' (scNameA m) stack') + (bufCover : BufCover st Z.T Bf s' σ') (bufAl : UtBufAlA Z.T σ') + (idleNoFault : IdleNoFaultA m σ') (mcp : McpRelA Z.T m (L.mem s') σ') + (yieldRel : YieldRelA Z.T Z.C m (L.mem s') σ') + (sinv : SInvXA σ' m n) (sidle : SIdleA σ' m n) + (runVcpu : RunVcpuInv st Z.T s') (blkQ : BlkQueuedA Z.T E N (L.mem s') σ') + (schedValid : SchedValidA σ') + (hsub : ∀ c, ∀ r ∈ stack' c, r ∈ stack c := by exact fun _ _ h => h) : + JStage3A st AR { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } H OS U Ac' Pp Tb Lv ia ie N E Rp + Bf stack' κ n (J5A st) (J7A st (J7R2A st ar nA)) s' a σ' m := by + have kb : L.ksb s' = L.ksb s := w.1 + have osk : OSKeep st OS s s' := J.osKeep_stepNA w + have vsr : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → L.mem s' b = L.mem s b := fun b hb => by + refine w.mem_eq (J.vsOk.al b hb) ?_ + by_cases h : AsW (L.ksb s) b + · obtain ⟨o, o1, o2, -, e⟩ := h + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.sys.ent.ksw + have ev : b.val = (L.ksb s).val + o.val := by + rw [e, wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact J.asid_offStepNA (by omega) (by omega) + · exact J.vsr_offStepNA J.vsOk hb h + have sys : SysInvA st H Z.Cs OS U Ac' Pp Tb Lv ia ie s' := + ⟨⟨J.sys.ent.heap, bxi, J.sys.ent.os, userOK, (utVsPt_keepA J.sys.ent.vs kb vsr).1, + by rw [kb]; exact J.sys.ent.ksw⟩, sysl⟩ + have sq : SchedQueuesInv st H.tcbs Z.Cs H.omega H s' Q Rl := by + obtain ⟨Q0, R0, h0⟩ := bxi.1 + exact SchedQueuesInv.reCoreA h0 (by rw [J.tcbs]; exact sr.core) + have poolOff : PoolOff st H Z.Cs OS s' := fun i => by + have h0 := J.poolOff i + rw [vspace.asid_table.eq] at h0 ⊢ + have v := AtOff.v i + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.sys.ent.ksw + have ev : (wa (L.ksb s) (AtOff i)).val = (L.ksb s).val + (AtOff i).val := by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have e : L.mem s' (wa (L.ksb s') (AtOff i)) = L.mem s (wa (L.ksb s) (AtOff i)) := by + rw [kb] + refine w.mem_eq ?_ (J.asid_offStepNA (by omega) (by omega)) + have h32 := J.j.1.ksbAl + show (wa (L.ksb s) (AtOff i)).val % 8 = 0 + omega + rw [e] + refine (WP.spec_ok _).2 fun hne asid => ?_ + have q := (WP.spec_ok _).1 h0 hne asid + exact ⟨q.al, q.om, q.sg, q.os, RegionOffNodes.ksb kb q.ks⟩ + have scCov : ScCovA Z.C m σ' := fun q x k h => by + obtain ⟨y, hy⟩ := SSz q x k h + exact J.scCov q y k hy + have hTr : TreeA st OS Z.G.chains Z.G.par s := + J.j.1.cdt.tree.grow J.j5.2.1 (fun x hx hn => J.j5.2.2 x hx hn) + have j5 : J5A st { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } H OS s' σ' m := + ⟨⟨MdbInvA.keepO hTr.lk hTr.tree J.j5.1.1 osk, IrqRunA.keepO hTr.lk J.j5.1.2.1 osk, + HandlersActiveA.keepO hTr.lk J.j5.1.2.2 osk kb (fun i hi => by + have eI : irqStAddrA (L.ksb s) i = irqWordA (L.ksb s) i := rfl + have hv := irqWordA_val J.sr.ksFit hi + have hal : Aligned (irqWordA (L.ksb s) i) := by + have := J.sr.toSchedSRN.ksb_al + show _ % 8 = 0 + omega + rw [eI] + exact w.mem_eq hal (J.irq_offStepNA hi))⟩, J.j5.2.1, + fun y hy hn => by rw [osk.capA hy]; exact J.j5.2.2 y hy hn⟩ + exact JStage3A.of_schedWrN J (hsub := hsub) (Q := Q) (Rl := Rl) (curOf := curOf) (curScOf := curScOf) (a' := a) w HS SSz Qk hum + J.j.1.ar hcurOf bufAl sys sq poolOff sr scCov ep ntfn reply bufCover idleNoFault mcp yieldRel sinv sidle runVcpu + blkQ symL schedValid j5 j7 + +/-- **The TCB and SC fields an IPC body keeps** (module docstring). -/ +structure IpcKeepA (σ σ' : Fl4SpecA64.State Unit) : Prop where + tcb : ∀ p x, σ.kheap p = some (.tcb x) → ∃ y, σ'.kheap p = some (.tcb y) ∧ y.tcbFault = x.tcbFault ∧ + y.tcbIpcframe = x.tcbIpcframe ∧ y.tcbIpcBuffer = x.tcbIpcBuffer ∧ y.tcbMcpriority = x.tcbMcpriority ∧ + y.tcbYieldTo = x.tcbYieldTo ∧ y.tcbFlags = x.tcbFlags + scF : ∀ p y k, σ.kheap p = some (.schedContext y k) → + ∃ y' k', σ'.kheap p = some (.schedContext y' k') ∧ y'.scYieldFrom = y.scYieldFrom + scB : ∀ p y' k', σ'.kheap p = some (.schedContext y' k') → + ∃ y k, σ.kheap p = some (.schedContext y k) ∧ y'.scYieldFrom = y.scYieldFrom + +/-- (integration round 2) `IpcKeepA` without the TCB flags. -/ +structure IpcKeepFA (σ σ' : Fl4SpecA64.State Unit) : Prop where + tcb : ∀ p x, σ.kheap p = some (.tcb x) → ∃ y, σ'.kheap p = some (.tcb y) ∧ y.tcbFault = x.tcbFault ∧ + y.tcbIpcframe = x.tcbIpcframe ∧ y.tcbIpcBuffer = x.tcbIpcBuffer ∧ y.tcbMcpriority = x.tcbMcpriority ∧ + y.tcbYieldTo = x.tcbYieldTo + scF : ∀ p y k, σ.kheap p = some (.schedContext y k) → + ∃ y' k', σ'.kheap p = some (.schedContext y' k') ∧ y'.scYieldFrom = y.scYieldFrom + scB : ∀ p y' k', σ'.kheap p = some (.schedContext y' k') → + ∃ y k, σ.kheap p = some (.schedContext y k) ∧ y'.scYieldFrom = y.scYieldFrom + +theorem IpcKeepA.toF {σ σ' : Fl4SpecA64.State Unit} (K : IpcKeepA σ σ') : IpcKeepFA σ σ' := + ⟨fun p x h => by obtain ⟨y, hy, a1, a2, a3, a4, a5, -⟩ := K.tcb p x h; exact ⟨y, hy, a1, a2, a3, a4, a5⟩, K.scF, K.scB⟩ + +theorem IpcKeepFA.trans {σ1 σ2 σ3 : Fl4SpecA64.State Unit} (h1 : IpcKeepFA σ1 σ2) (h2 : IpcKeepFA σ2 σ3) : + IpcKeepFA σ1 σ3 := + ⟨fun p x h => by + obtain ⟨y, hy, a1, a2, a3, a4, a5⟩ := h1.tcb p x h + obtain ⟨z, hz, b1, b2, b3, b4, b5⟩ := h2.tcb p y hy + exact ⟨z, hz, b1.trans a1, b2.trans a2, b3.trans a3, b4.trans a4, b5.trans a5⟩, + fun p y k h => by + obtain ⟨y', k', h', e⟩ := h1.scF p y k h + obtain ⟨y'', k'', h'', e'⟩ := h2.scF p y' k' h' + exact ⟨y'', k'', h'', e'.trans e⟩, + fun p y k h => by + obtain ⟨y', k', h', e⟩ := h2.scB p y k h + obtain ⟨y'', k'', h'', e'⟩ := h1.scB p y' k' h' + exact ⟨y'', k'', h'', e.trans e'⟩⟩ + +/-- A step keeping every TCB and SC object. -/ +theorem IpcKeepA.of_objs {σ σ' : Fl4SpecA64.State Unit} + (ht : ∀ p x, σ.kheap p = some (.tcb x) → σ'.kheap p = some (.tcb x)) + (hf : ∀ p y k, σ.kheap p = some (.schedContext y k) → σ'.kheap p = some (.schedContext y k)) + (hb : ∀ p y k, σ'.kheap p = some (.schedContext y k) → σ.kheap p = some (.schedContext y k)) : + IpcKeepA σ σ' := + ⟨fun p x h => ⟨x, ht p x h, rfl, rfl, rfl, rfl, rfl, rfl⟩, fun p y k h => ⟨y, k, hf p y k h, rfl⟩, + fun p y k h => ⟨y, k, hb p y k h, rfl⟩⟩ + +set_option maxHeartbeats 1000000 in +/-- (integration round 2) `of_ipcStepA` with the MCP / flags relation given at the post state and `IpcKeepFA` (the TCB +flags may change: TCB SetFlags). -/ +theorem JStage3A.of_ipcStepFlA {Ac' : Set U64} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (w : WritesIn st s s' (SchedStepNWA (st := st) N E Rp Z m σ s)) (HS : SpecSchedA σ σ') (SSz : SSzA σ σ') (Qk : QKeepA σ σ') + (hum : σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory) + (hcurOf : ∀ k, k ≠ m.curCore → curOf k = Z.curOf k) + (bxi : BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac' s') + (sysl : SysL st H Z.Cs OS ia ie s') (symL : SymL st H Z.Cs s') + (userOK : Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s') + (j7 : J7A st (J7R2A st ar nA) Z H OS s' σ' m) + (sr : SchedSRA st { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } m κ s' σ') + (ep : EpStateRel Z.T E Rp Bf (L.mem s') σ') + (ntfn : NtfnStateRel N Z.T (L.mem s') σ') + (reply : ReplyStackRelA (L.mem s') Rp Z.C σ' (scNameA m) stack) + (K : IpcKeepFA σ σ') (mcp : McpRelA Z.T m (L.mem s') σ') + (tw : ∀ t ∈ Z.T, ∀ O : U64, O.val < 224 ∨ O.val = 3440 ∨ O.val = 3472 ∨ O.val = 3512 → O.val % 8 = 0 → + L.mem s' (wa t O) = L.mem s (wa t O)) + (sinv : SInvXA σ' m n) (sidle : SIdleA σ' m n) + (runVcpu : RunVcpuInv st Z.T s') (blkQ : BlkQueuedA Z.T E N (L.mem s') σ') + (schedValid : SchedValidA σ') : + JStage3A st AR { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } H OS U Ac' Pp Tb Lv ia ie N E Rp + Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s' a σ' m := by + have at_ : ∀ t ∈ Z.T, ∃ x, σ.kheap (specPtr t) = some (.tcb x) := fun t ht => by + obtain ⟨x, hx, -⟩ := J.ntfn.tcb t ht + exact ⟨x, hx⟩ + have bufCover : BufCover st Z.T Bf s' σ' := by + intro u hu y b sb v hy hb hl + obtain ⟨x, hx⟩ := at_ u hu + obtain ⟨y0, hy0, -, hfr, hbuf, -, -⟩ := K.tcb _ _ hx + rw [hy] at hy0 + cases hy0 + have hb0 : ipcBufOfA b x = some sb := by + unfold ipcBufOfA at hb ⊢ + rwa [hfr, hbuf] at hb + have hib : IbSame (L := L) s s' u := by + refine ⟨?_, ?_, ?_⟩ + · rw [ibWordA_eq]; exact tw u hu _ (by simp) (by simp) + · rw [sbWordA_eq]; exact tw u hu _ (by simp) (by simp) + · rw [sbWordA8_eq]; exact tw u hu _ (by simp) (by simp) + rw [lookup_ipc_buffer_sameA hib] at hl + exact J.bufCover u hu x b sb v hx hb0 hl + have bufAl : UtBufAlA Z.T σ' := fun t ht y hy => by + obtain ⟨x, hx⟩ := at_ t ht + obtain ⟨y0, hy0, -, -, hbuf, -, -⟩ := K.tcb _ _ hx + rw [hy] at hy0 + cases hy0 + rw [hbuf]; exact J.bufAl t ht x hx + have idle : IdleNoFaultA m σ' := fun c hc => by + obtain ⟨x, hx, hf⟩ := J.idleNoFault c hc + obtain ⟨y, hy, e, -⟩ := K.tcb _ _ hx + exact ⟨y, hy, e.trans hf⟩ + have ye : ∀ t ∈ Z.T, L.mem s' (yieldA t) = L.mem s (yieldA t) := fun t ht => by + unfold yieldA; exact tw t ht _ (by simp) (by simp) + have yr : YieldRelA Z.T Z.C m (L.mem s') σ' := by + refine ⟨fun t ht => ?_, fun t ht h => ?_, fun c hc y k h p hp => ?_⟩ + · obtain ⟨x, hx, e⟩ := J.yieldRel.tcb t ht + obtain ⟨y, hy, -, -, -, -, hyt⟩ := K.tcb _ _ hx + exact ⟨y, hy, by rw [ye t ht, hyt]; exact e⟩ + · rw [ye t ht] at h ⊢ + obtain ⟨hC, y, k, hy, e⟩ := J.yieldRel.toSc t ht h + obtain ⟨y', k', hy', e'⟩ := K.scF _ _ _ hy + exact ⟨hC, y', k', hy', e'.trans e⟩ + · obtain ⟨y0, k0, h0, e⟩ := K.scB _ _ _ h + obtain ⟨t, ht, e1, e2⟩ := J.yieldRel.frm c hc y0 k0 h0 p (e ▸ hp) + exact ⟨t, ht, e1, by rw [ye t ht]; exact e2⟩ + exact J.of_stepFinA w HS SSz Qk hum hcurOf bxi sysl symL userOK j7 sr ep ntfn reply bufCover bufAl idle mcp yr sinv + sidle runVcpu blkQ schedValid + + +set_option maxHeartbeats 1000000 in +/-- **`JStage3A` across an IPC body** (module docstring). -/ +theorem JStage3A.of_ipcStepA {Ac' : Set U64} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (w : WritesIn st s s' (SchedStepNWA (st := st) N E Rp Z m σ s)) (HS : SpecSchedA σ σ') (SSz : SSzA σ σ') (Qk : QKeepA σ σ') + (hum : σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory) + (hcurOf : ∀ k, k ≠ m.curCore → curOf k = Z.curOf k) + (bxi : BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac' s') + (sysl : SysL st H Z.Cs OS ia ie s') (symL : SymL st H Z.Cs s') + (userOK : Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s') + (j7 : J7A st (J7R2A st ar nA) Z H OS s' σ' m) + (sr : SchedSRA st { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } m κ s' σ') + (ep : EpStateRel Z.T E Rp Bf (L.mem s') σ') + (ntfn : NtfnStateRel N Z.T (L.mem s') σ') + (reply : ReplyStackRelA (L.mem s') Rp Z.C σ' (scNameA m) stack) + (K : IpcKeepA σ σ') + (tw : ∀ t ∈ Z.T, ∀ O : U64, O.val < 224 ∨ O.val = 3440 ∨ O.val = 3472 ∨ O.val = 3512 ∨ O.val = 3456 → O.val % 8 = 0 → + L.mem s' (wa t O) = L.mem s (wa t O)) + (sinv : SInvXA σ' m n) (sidle : SIdleA σ' m n) + (runVcpu : RunVcpuInv st Z.T s') (blkQ : BlkQueuedA Z.T E N (L.mem s') σ') + (schedValid : SchedValidA σ') : + JStage3A st AR { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } H OS U Ac' Pp Tb Lv ia ie N E Rp + Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s' a σ' m := + J.of_ipcStepFlA w HS SSz Qk hum hcurOf bxi sysl symL userOK j7 sr ep ntfn reply K.toF + (J.mcp.congrA (fun t ht => ⟨tw t ht _ (by simp) (by simp), tw t ht _ (by simp) (by simp)⟩) fun t ht x hx => by + obtain ⟨y, hy, -, -, -, hm, -, hfl⟩ := K.tcb _ _ hx + exact ⟨y, hy, hm, hfl⟩) + (fun t ht O h h8 => tw t ht O (by rcases h with h | h | h | h <;> simp [h]) h8) sinv sidle runVcpu blkQ schedValid + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3IpcGlueUA.lean b/proofs/Fl4ProofsA64/P3/Stage3IpcGlueUA.lean new file mode 100644 index 000000000..886c60cd5 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3IpcGlueUA.lean @@ -0,0 +1,330 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2b-2): **the IPC record glue over `of_schedWrU`** (an IPC body that writes the +receiver's buffer words and user memory). + +* **`JStage3A.of_stepFinUA`**: `of_stepFinA` with `BufW Bf` in the write set and user memory changed only off the free + RAM untyped parts (`hz`, J10's form: `JStage3A.specZero_of_bufA` from typed `other` objects, or + `P3/Stage3RecvBufA`'s `specZero_of_recvBufA` from a receive buffer's frame). The `OS` slots, kernel-state words (ASID table, IRQ state) are off the buffer words by `bufOff` / + `buf_ksA`. +* **`JStage3A.of_ipcStepUA`**: `of_ipcStepA` over it. + +Both take the reply stacks after the step (`stack'`) with the dead reply objects off them (`hdr`). The +buffer words are off the VSpace and VCPU words by `bufVs` / `bufOff.vcw`. New module. +-/ +import Fl4ProofsA64.P3.Stage3IpcGlueA +import Fl4ProofsA64.P3.Stage3BufWrA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {ar : machine.Arch A} {nA : U64} {s s' : S} {a : A} {σ σ' : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} {Q : U64 → U64 → List U64} {Rl : U64 → List U64} + {curOf curScOf : Fl4SpecA64.CoreId → Fl4SpecA64.ObjRef} + +set_option maxHeartbeats 1000000 in +/-- **`of_stepFinUSA` with `QBlkRA` and the dead replies' `replyTcb` as a hypothesis** (Lift 2c: a step that links a +reply object that no thread was blocked on). -/ +theorem JStage3A.of_stepFinURSA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (w : WritesIn st s s' (fun b => SchedStepNWA (st := st) N E Rp Z m σ s b ∨ BufW Bf b)) (HS : SpecSchedA σ σ') (SSz : SSzA σ σ') (Qk : QBlkRA (specPtr '' E) (specPtr '' N) σ σ') + (hrt : ∀ r ∈ Rp, DeadA st OS .reply s r → ∀ x, σ'.kheap (specPtr r) = some (.reply x) → x.replyTcb = none) + (hz : ∀ x : Fl4SpecA64.Word, UtFreeA st OS s (fun d => d = false) (fl4Ptr x).val → + σ'.machineState.underlyingMemory x = σ.machineState.underlyingMemory x) + (hcurOf : ∀ k, k ≠ m.curCore → curOf k = Z.curOf k) + (bxi : BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s') + (sysl : SysL st H Z.Cs OS ia ie s') (symL : SymL st H Z.Cs s') + (userOK : Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s') + (j7 : J7A st (J7R2A st ar nA) Z H OS s' σ' m) + (sr : SchedSRA st { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } m κ s' σ') + (ep : EpStateRel Z.T E Rp Bf (L.mem s') σ') + (ntfn : NtfnStateRel N Z.T (L.mem s') σ') + {stack' : U64 → List U64} (reply : ReplyStackRelA (L.mem s') Rp Z.C σ' (scNameA m) stack') + (hdr : ∀ r ∈ Rp, DeadA st OS .reply s r → ∀ c ∈ Z.C, r ∉ stack' c) + (bufCover : BufCover st Z.T Bf s' σ') (bufAl : UtBufAlA Z.T σ') + (idleNoFault : IdleNoFaultA m σ') (mcp : McpRelA Z.T m (L.mem s') σ') + (yieldRel : YieldRelA Z.T Z.C m (L.mem s') σ') + (sinv : SInvXA σ' m n) (sidle : SIdleA σ' m n) + (runVcpu : RunVcpuInv st Z.T s') (blkQ : BlkQueuedA Z.T E N (L.mem s') σ') + (schedValid : SchedValidA σ') : + JStage3A st AR { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } H OS U Ac Pp Tb Lv ia ie N E Rp + Bf stack' κ n (J5A st) (J7A st (J7R2A st ar nA)) s' a σ' m := by + have kb : L.ksb s' = L.ksb s := w.1 + have bks : ∀ b, (L.ksb s).val ≤ b.val → b.val < (L.ksb s).val + 788736 → ¬ BufW Bf b := fun b h1 h2 hb => by + have hksz : ksSizeA = 788736 := rfl + have := J.buf_ksA hb + omega + have osk : OSKeep st OS s s' := fun y hy => by + have hfit := J.sys.ent.os.fit y hy + have key : ∀ o : U64, o.val < 32 → o.val % 8 = 0 → L.mem s' (wa y o) = L.mem s (wa y o) := by + intro o o1 o2 + have hv : (wa y o).val = y.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have hal : Aligned (wa y o) := J.sys.ent.os.al hy o1 o2 + have hin : InOS OS (wa y o) := ⟨y, hy, by omega, by omega⟩ + exact w.mem_eq hal (fun h => h.elim (J.os_offStepNA hal hin) (fun hb => J.bufOff.os _ hb hin)) + have e0 : wa y 0#u64 = y := by apply UScalar.eq_of_val_eq; rw [wa_valm]; simp + exact ⟨by have := key 0#u64 (by simp) (by simp); rwa [e0] at this, key 8#u64 (by simp) (by simp), + key 16#u64 (by simp) (by simp), key 24#u64 (by simp) (by simp)⟩ + have vsr : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → L.mem s' b = L.mem s b := fun b hb => by + refine w.mem_eq (J.vsOk.al b hb) (fun h => h.elim ?_ (fun h => J.bufVs b h hb)) + by_cases h : AsW (L.ksb s) b + · obtain ⟨o, o1, o2, -, e⟩ := h + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.sys.ent.ksw + have ev : b.val = (L.ksb s).val + o.val := by + rw [e, wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact J.asid_offStepNA (by omega) (by omega) + · exact J.vsr_offStepNA J.vsOk hb h + have sys : SysInvA st H Z.Cs OS U Ac Pp Tb Lv ia ie s' := + ⟨⟨J.sys.ent.heap, bxi, J.sys.ent.os, userOK, (utVsPt_keepA J.sys.ent.vs kb vsr).1, + by rw [kb]; exact J.sys.ent.ksw⟩, sysl⟩ + have sq : SchedQueuesInv st H.tcbs Z.Cs H.omega H s' Q Rl := by + obtain ⟨Q0, R0, h0⟩ := bxi.1 + exact SchedQueuesInv.reCoreA h0 (by rw [J.tcbs]; exact sr.core) + have poolOff : PoolOff st H Z.Cs OS s' := fun i => by + have h0 := J.poolOff i + rw [vspace.asid_table.eq] at h0 ⊢ + have v := AtOff.v i + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.sys.ent.ksw + have ev : (wa (L.ksb s) (AtOff i)).val = (L.ksb s).val + (AtOff i).val := by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have e : L.mem s' (wa (L.ksb s') (AtOff i)) = L.mem s (wa (L.ksb s) (AtOff i)) := by + rw [kb] + refine w.mem_eq ?_ (fun h => h.elim (J.asid_offStepNA (by omega) (by omega)) (bks _ (by omega) (by omega))) + have h32 := J.j.1.ksbAl + show (wa (L.ksb s) (AtOff i)).val % 8 = 0 + omega + rw [e] + refine (WP.spec_ok _).2 fun hne asid => ?_ + have q := (WP.spec_ok _).1 h0 hne asid + exact ⟨q.al, q.om, q.sg, q.os, RegionOffNodes.ksb kb q.ks⟩ + have scCov : ScCovA Z.C m σ' := fun q x k h => by + obtain ⟨y, hy⟩ := SSz q x k h + exact J.scCov q y k hy + have hTr : TreeA st OS Z.G.chains Z.G.par s := + J.j.1.cdt.tree.grow J.j5.2.1 (fun x hx hn => J.j5.2.2 x hx hn) + have j5 : J5A st { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } H OS s' σ' m := + ⟨⟨MdbInvA.keepO hTr.lk hTr.tree J.j5.1.1 osk, IrqRunA.keepO hTr.lk J.j5.1.2.1 osk, + HandlersActiveA.keepO hTr.lk J.j5.1.2.2 osk kb (fun i hi => by + have eI : irqStAddrA (L.ksb s) i = irqWordA (L.ksb s) i := rfl + have hv := irqWordA_val J.sr.ksFit hi + have hal : Aligned (irqWordA (L.ksb s) i) := by + have := J.sr.toSchedSRN.ksb_al + show _ % 8 = 0 + omega + rw [eI] + have hksz : ksSizeA = 788736 := rfl + exact w.mem_eq hal (fun h => h.elim (J.irq_offStepNA hi) (bks _ (by omega) (by omega))))⟩, J.j5.2.1, + fun y hy hn => by rw [osk.capA hy]; exact J.j5.2.2 y hy hn⟩ + exact JStage3A.of_schedWrXSS (hdr := hdr) (symL := symL) (schedValid := schedValid) (vcX := fun v hv o a b hb => J.bufOff.vcw _ hb v hv o a b rfl) (Q := Q) (Rl := Rl) + (curOf := curOf) (curScOf := curScOf) (a' := a) J J.bufXOffA (fun b hv hb => J.bufVs b hb hv) bufAl w HS SSz Qk hrt hz J.j.1.ar hcurOf sys sq poolOff + sr scCov ep ntfn reply bufCover idleNoFault mcp yieldRel sinv sidle runVcpu blkQ j5 j7 + +set_option maxHeartbeats 1000000 in +/-- **`of_stepFinA` across a body that also writes buffer words and user memory** (module docstring). -/ +theorem JStage3A.of_stepFinUSA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (w : WritesIn st s s' (fun b => SchedStepNWA (st := st) N E Rp Z m σ s b ∨ BufW Bf b)) (HS : SpecSchedA σ σ') (SSz : SSzA σ σ') (Qk : QBlkA (specPtr '' E) (specPtr '' N) σ σ') + (hz : ∀ x : Fl4SpecA64.Word, UtFreeA st OS s (fun d => d = false) (fl4Ptr x).val → + σ'.machineState.underlyingMemory x = σ.machineState.underlyingMemory x) + (hcurOf : ∀ k, k ≠ m.curCore → curOf k = Z.curOf k) + (bxi : BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s') + (sysl : SysL st H Z.Cs OS ia ie s') (symL : SymL st H Z.Cs s') + (userOK : Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s') + (j7 : J7A st (J7R2A st ar nA) Z H OS s' σ' m) + (sr : SchedSRA st { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } m κ s' σ') + (ep : EpStateRel Z.T E Rp Bf (L.mem s') σ') + (ntfn : NtfnStateRel N Z.T (L.mem s') σ') + {stack' : U64 → List U64} (reply : ReplyStackRelA (L.mem s') Rp Z.C σ' (scNameA m) stack') + (hdr : ∀ r ∈ Rp, DeadA st OS .reply s r → ∀ c ∈ Z.C, r ∉ stack' c) + (bufCover : BufCover st Z.T Bf s' σ') (bufAl : UtBufAlA Z.T σ') + (idleNoFault : IdleNoFaultA m σ') (mcp : McpRelA Z.T m (L.mem s') σ') + (yieldRel : YieldRelA Z.T Z.C m (L.mem s') σ') + (sinv : SInvXA σ' m n) (sidle : SIdleA σ' m n) + (runVcpu : RunVcpuInv st Z.T s') (blkQ : BlkQueuedA Z.T E N (L.mem s') σ') + (schedValid : SchedValidA σ') : + JStage3A st AR { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } H OS U Ac Pp Tb Lv ia ie N E Rp + Bf stack' κ n (J5A st) (J7A st (J7R2A st ar nA)) s' a σ' m := + JStage3A.of_stepFinURSA (J := J) (w := w) (HS := HS) (SSz := SSz) (hz := hz) (hcurOf := hcurOf) (bxi := bxi) (sysl := sysl) (symL := symL) (userOK := userOK) (j7 := j7) (sr := sr) (ep := ep) (ntfn := ntfn) (reply := reply) (hdr := hdr) (bufCover := bufCover) (bufAl := bufAl) (idleNoFault := idleNoFault) (mcp := mcp) (yieldRel := yieldRel) (sinv := sinv) (sidle := sidle) (runVcpu := runVcpu) (blkQ := blkQ) (schedValid := schedValid) (stack' := stack') (Qk := Qk.toR) (hrt := J.hrt_qblkA Qk) + +set_option maxHeartbeats 1000000 in +/-- `of_stepFinUSA` for a step shrinking the endpoint and notification queues (`QSendA`). -/ +theorem JStage3A.of_stepFinUA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (w : WritesIn st s s' (fun b => SchedStepNWA (st := st) N E Rp Z m σ s b ∨ BufW Bf b)) (HS : SpecSchedA σ σ') (SSz : SSzA σ σ') (Qk : QSendA σ σ') + (hz : ∀ x : Fl4SpecA64.Word, UtFreeA st OS s (fun d => d = false) (fl4Ptr x).val → + σ'.machineState.underlyingMemory x = σ.machineState.underlyingMemory x) + (hcurOf : ∀ k, k ≠ m.curCore → curOf k = Z.curOf k) + (bxi : BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s') + (sysl : SysL st H Z.Cs OS ia ie s') (symL : SymL st H Z.Cs s') + (userOK : Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s') + (j7 : J7A st (J7R2A st ar nA) Z H OS s' σ' m) + (sr : SchedSRA st { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } m κ s' σ') + (ep : EpStateRel Z.T E Rp Bf (L.mem s') σ') + (ntfn : NtfnStateRel N Z.T (L.mem s') σ') + {stack' : U64 → List U64} (reply : ReplyStackRelA (L.mem s') Rp Z.C σ' (scNameA m) stack') + (hdr : ∀ r ∈ Rp, DeadA st OS .reply s r → ∀ c ∈ Z.C, r ∉ stack' c) + (bufCover : BufCover st Z.T Bf s' σ') (bufAl : UtBufAlA Z.T σ') + (idleNoFault : IdleNoFaultA m σ') (mcp : McpRelA Z.T m (L.mem s') σ') + (yieldRel : YieldRelA Z.T Z.C m (L.mem s') σ') + (sinv : SInvXA σ' m n) (sidle : SIdleA σ' m n) + (runVcpu : RunVcpuInv st Z.T s') (blkQ : BlkQueuedA Z.T E N (L.mem s') σ') + (schedValid : SchedValidA σ') : + JStage3A st AR { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } H OS U Ac Pp Tb Lv ia ie N E Rp + Bf stack' κ n (J5A st) (J7A st (J7R2A st ar nA)) s' a σ' m := + JStage3A.of_stepFinUSA (J := J) (w := w) (HS := HS) (SSz := SSz) (hz := hz) (hcurOf := hcurOf) (bxi := bxi) + (sysl := sysl) (symL := symL) (userOK := userOK) (j7 := j7) (sr := sr) (ep := ep) (ntfn := ntfn) + (stack' := stack') (reply := reply) (hdr := hdr) (bufCover := bufCover) (bufAl := bufAl) + (idleNoFault := idleNoFault) (mcp := mcp) (yieldRel := yieldRel) (sinv := sinv) (sidle := sidle) + (runVcpu := runVcpu) (blkQ := blkQ) (schedValid := schedValid) + (Qk := QSendA.toBlk J.ipcSorted Qk) + +set_option maxHeartbeats 1000000 in +/-- **`of_ipcStepUSA` with `QBlkRA` and the dead replies' `replyTcb` as a hypothesis** (Lift 2c). -/ +theorem JStage3A.of_ipcStepURSA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (w : WritesIn st s s' (fun b => SchedStepNWA (st := st) N E Rp Z m σ s b ∨ BufW Bf b)) (HS : SpecSchedA σ σ') (SSz : SSzA σ σ') (Qk : QBlkRA (specPtr '' E) (specPtr '' N) σ σ') + (hrt : ∀ r ∈ Rp, DeadA st OS .reply s r → ∀ x, σ'.kheap (specPtr r) = some (.reply x) → x.replyTcb = none) + (hz : ∀ x : Fl4SpecA64.Word, UtFreeA st OS s (fun d => d = false) (fl4Ptr x).val → + σ'.machineState.underlyingMemory x = σ.machineState.underlyingMemory x) + (hcurOf : ∀ k, k ≠ m.curCore → curOf k = Z.curOf k) + (bxi : BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s') + (sysl : SysL st H Z.Cs OS ia ie s') (symL : SymL st H Z.Cs s') + (userOK : Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s') + (j7 : J7A st (J7R2A st ar nA) Z H OS s' σ' m) + (sr : SchedSRA st { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } m κ s' σ') + (ep : EpStateRel Z.T E Rp Bf (L.mem s') σ') + (ntfn : NtfnStateRel N Z.T (L.mem s') σ') + {stack' : U64 → List U64} (reply : ReplyStackRelA (L.mem s') Rp Z.C σ' (scNameA m) stack') + (hdr : ∀ r ∈ Rp, DeadA st OS .reply s r → ∀ c ∈ Z.C, r ∉ stack' c) + (K : IpcKeepA σ σ') + (tw : ∀ t ∈ Z.T, ∀ O : U64, O.val < 224 ∨ O.val = 3440 ∨ O.val = 3472 ∨ O.val = 3512 ∨ O.val = 3456 → O.val % 8 = 0 → + L.mem s' (wa t O) = L.mem s (wa t O)) + (sinv : SInvXA σ' m n) (sidle : SIdleA σ' m n) + (runVcpu : RunVcpuInv st Z.T s') (blkQ : BlkQueuedA Z.T E N (L.mem s') σ') + (schedValid : SchedValidA σ') : + JStage3A st AR { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } H OS U Ac Pp Tb Lv ia ie N E Rp + Bf stack' κ n (J5A st) (J7A st (J7R2A st ar nA)) s' a σ' m := by + have at_ : ∀ t ∈ Z.T, ∃ x, σ.kheap (specPtr t) = some (.tcb x) := fun t ht => by + obtain ⟨x, hx, -⟩ := J.ntfn.tcb t ht + exact ⟨x, hx⟩ + have bufCover : BufCover st Z.T Bf s' σ' := by + intro u hu y b sb v hy hb hl + obtain ⟨x, hx⟩ := at_ u hu + obtain ⟨y0, hy0, -, hfr, hbuf, -, -⟩ := K.tcb _ _ hx + rw [hy] at hy0 + cases hy0 + have hb0 : ipcBufOfA b x = some sb := by + unfold ipcBufOfA at hb ⊢ + rwa [hfr, hbuf] at hb + have hib : IbSame (L := L) s s' u := by + refine ⟨?_, ?_, ?_⟩ + · rw [ibWordA_eq]; exact tw u hu _ (by simp) (by simp) + · rw [sbWordA_eq]; exact tw u hu _ (by simp) (by simp) + · rw [sbWordA8_eq]; exact tw u hu _ (by simp) (by simp) + rw [lookup_ipc_buffer_sameA hib] at hl + exact J.bufCover u hu x b sb v hx hb0 hl + have bufAl : UtBufAlA Z.T σ' := fun t ht y hy => by + obtain ⟨x, hx⟩ := at_ t ht + obtain ⟨y0, hy0, -, -, hbuf, -, -⟩ := K.tcb _ _ hx + rw [hy] at hy0 + cases hy0 + rw [hbuf]; exact J.bufAl t ht x hx + have idle : IdleNoFaultA m σ' := fun c hc => by + obtain ⟨x, hx, hf⟩ := J.idleNoFault c hc + obtain ⟨y, hy, e, -⟩ := K.tcb _ _ hx + exact ⟨y, hy, e.trans hf⟩ + have mcp : McpRelA Z.T m (L.mem s') σ' := + J.mcp.congrA (fun t ht => ⟨tw t ht _ (by simp) (by simp), tw t ht _ (by simp) (by simp)⟩) (fun t ht x hx => by + obtain ⟨y, hy, -, -, -, hm, hfl⟩ := K.tcb _ _ hx + exact ⟨y, hy, hm, hfl.2⟩) + have ye : ∀ t ∈ Z.T, L.mem s' (yieldA t) = L.mem s (yieldA t) := fun t ht => by + unfold yieldA; exact tw t ht _ (by simp) (by simp) + have yr : YieldRelA Z.T Z.C m (L.mem s') σ' := by + refine ⟨fun t ht => ?_, fun t ht h => ?_, fun c hc y k h p hp => ?_⟩ + · obtain ⟨x, hx, e⟩ := J.yieldRel.tcb t ht + obtain ⟨y, hy, -, -, -, -, hyt⟩ := K.tcb _ _ hx + exact ⟨y, hy, by rw [ye t ht, hyt.1]; exact e⟩ + · rw [ye t ht] at h ⊢ + obtain ⟨hC, y, k, hy, e⟩ := J.yieldRel.toSc t ht h + obtain ⟨y', k', hy', e'⟩ := K.scF _ _ _ hy + exact ⟨hC, y', k', hy', e'.trans e⟩ + · obtain ⟨y0, k0, h0, e⟩ := K.scB _ _ _ h + obtain ⟨t, ht, e1, e2⟩ := J.yieldRel.frm c hc y0 k0 h0 p (e ▸ hp) + exact ⟨t, ht, e1, by rw [ye t ht]; exact e2⟩ + exact J.of_stepFinURSA w HS SSz Qk hrt hz hcurOf bxi sysl symL userOK j7 sr ep ntfn reply hdr bufCover bufAl idle mcp yr sinv + sidle runVcpu blkQ schedValid + +set_option maxHeartbeats 1000000 in +/-- **`of_ipcStepA` across a body that also writes buffer words and user memory** (module docstring). -/ +theorem JStage3A.of_ipcStepUSA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (w : WritesIn st s s' (fun b => SchedStepNWA (st := st) N E Rp Z m σ s b ∨ BufW Bf b)) (HS : SpecSchedA σ σ') (SSz : SSzA σ σ') (Qk : QBlkA (specPtr '' E) (specPtr '' N) σ σ') + (hz : ∀ x : Fl4SpecA64.Word, UtFreeA st OS s (fun d => d = false) (fl4Ptr x).val → + σ'.machineState.underlyingMemory x = σ.machineState.underlyingMemory x) + (hcurOf : ∀ k, k ≠ m.curCore → curOf k = Z.curOf k) + (bxi : BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s') + (sysl : SysL st H Z.Cs OS ia ie s') (symL : SymL st H Z.Cs s') + (userOK : Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s') + (j7 : J7A st (J7R2A st ar nA) Z H OS s' σ' m) + (sr : SchedSRA st { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } m κ s' σ') + (ep : EpStateRel Z.T E Rp Bf (L.mem s') σ') + (ntfn : NtfnStateRel N Z.T (L.mem s') σ') + {stack' : U64 → List U64} (reply : ReplyStackRelA (L.mem s') Rp Z.C σ' (scNameA m) stack') + (hdr : ∀ r ∈ Rp, DeadA st OS .reply s r → ∀ c ∈ Z.C, r ∉ stack' c) + (K : IpcKeepA σ σ') + (tw : ∀ t ∈ Z.T, ∀ O : U64, O.val < 224 ∨ O.val = 3440 ∨ O.val = 3472 ∨ O.val = 3512 ∨ O.val = 3456 → O.val % 8 = 0 → + L.mem s' (wa t O) = L.mem s (wa t O)) + (sinv : SInvXA σ' m n) (sidle : SIdleA σ' m n) + (runVcpu : RunVcpuInv st Z.T s') (blkQ : BlkQueuedA Z.T E N (L.mem s') σ') + (schedValid : SchedValidA σ') : + JStage3A st AR { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } H OS U Ac Pp Tb Lv ia ie N E Rp + Bf stack' κ n (J5A st) (J7A st (J7R2A st ar nA)) s' a σ' m := + JStage3A.of_ipcStepURSA (J := J) (w := w) (HS := HS) (SSz := SSz) (hz := hz) (hcurOf := hcurOf) (bxi := bxi) (sysl := sysl) (symL := symL) (userOK := userOK) (j7 := j7) (sr := sr) (ep := ep) (ntfn := ntfn) (reply := reply) (hdr := hdr) (K := K) (tw := tw) (sinv := sinv) (sidle := sidle) (runVcpu := runVcpu) (blkQ := blkQ) (schedValid := schedValid) (stack' := stack') (Qk := Qk.toR) (hrt := J.hrt_qblkA Qk) + +set_option maxHeartbeats 1000000 in +/-- `of_ipcStepUSA` for a step shrinking the endpoint and notification queues (`QSendA`). -/ +theorem JStage3A.of_ipcStepUA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (w : WritesIn st s s' (fun b => SchedStepNWA (st := st) N E Rp Z m σ s b ∨ BufW Bf b)) (HS : SpecSchedA σ σ') (SSz : SSzA σ σ') (Qk : QSendA σ σ') + (hz : ∀ x : Fl4SpecA64.Word, UtFreeA st OS s (fun d => d = false) (fl4Ptr x).val → + σ'.machineState.underlyingMemory x = σ.machineState.underlyingMemory x) + (hcurOf : ∀ k, k ≠ m.curCore → curOf k = Z.curOf k) + (bxi : BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s') + (sysl : SysL st H Z.Cs OS ia ie s') (symL : SymL st H Z.Cs s') + (userOK : Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s') + (j7 : J7A st (J7R2A st ar nA) Z H OS s' σ' m) + (sr : SchedSRA st { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } m κ s' σ') + (ep : EpStateRel Z.T E Rp Bf (L.mem s') σ') + (ntfn : NtfnStateRel N Z.T (L.mem s') σ') + {stack' : U64 → List U64} (reply : ReplyStackRelA (L.mem s') Rp Z.C σ' (scNameA m) stack') + (hdr : ∀ r ∈ Rp, DeadA st OS .reply s r → ∀ c ∈ Z.C, r ∉ stack' c) + (K : IpcKeepA σ σ') + (tw : ∀ t ∈ Z.T, ∀ O : U64, O.val < 224 ∨ O.val = 3440 ∨ O.val = 3472 ∨ O.val = 3512 ∨ O.val = 3456 → O.val % 8 = 0 → + L.mem s' (wa t O) = L.mem s (wa t O)) + (sinv : SInvXA σ' m n) (sidle : SIdleA σ' m n) + (runVcpu : RunVcpuInv st Z.T s') (blkQ : BlkQueuedA Z.T E N (L.mem s') σ') + (schedValid : SchedValidA σ') : + JStage3A st AR { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } H OS U Ac Pp Tb Lv ia ie N E Rp + Bf stack' κ n (J5A st) (J7A st (J7R2A st ar nA)) s' a σ' m := + JStage3A.of_ipcStepUSA (J := J) (w := w) (HS := HS) (SSz := SSz) (hz := hz) (hcurOf := hcurOf) (bxi := bxi) + (sysl := sysl) (symL := symL) (userOK := userOK) (j7 := j7) (sr := sr) (ep := ep) (ntfn := ntfn) + (stack' := stack') (reply := reply) (hdr := hdr) (K := K) (tw := tw) (sinv := sinv) (sidle := sidle) + (runVcpu := runVcpu) (blkQ := blkQ) (schedValid := schedValid) + (Qk := QSendA.toBlk J.ipcSorted Qk) + + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3IpcKeepSA.lean b/proofs/Fl4ProofsA64/P3/Stage3IpcKeepSA.lean new file mode 100644 index 000000000..d7814b3b9 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3IpcKeepSA.lean @@ -0,0 +1,1157 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2b-2): **the spec keeps across `send_ipc` to a waiting receiver.** + +* **`SIKA i M N don`**: a preorder on spec states. Only the heap, the ready / release queues, the scheduler action, the + timer flag and the machine state change. Objects keep their kind (`ObjIpcA`): TCBs keep fault, IPC frame and buffer, + `mcp`, yield link and caps, their state changes only in `M` (never to idle), their SC only with `don` and off `i`; + SCs keep `yield_from`, `refill_max` and non-empty refills, `idle_sc_ptr` keeps its thread and replies, and a thread + link changes only with `don` and never to `i`. New ready-queue members are in `N` or the pending switch target. +* **`sendIpc_recv_sikA`**: every outcome of `send_ipc` to a waiting receiver (no fault, no extra caps) is `SIKA`. +* **`SIKA.ipcKeep`** (`IpcKeepA`), **`SIKA.sinv`** (`SInvXA`), **`SIKA.sidle`** (`SIdleA`). +* **`sendIpc_recv_runningA`**: the receiver ends `Running`. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3IpcGlueA +import Fl4ProofsA64.P3.Stage3SendSpecFrA +import Fl4ProofsA64.P3.Stage3SigSchedValidA +import Fl4ProofsA64.P3.DelSuspendSpecA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P3 Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Classical + +noncomputable section + +/-! ## The relation -/ + +structure TcbIpcA (i : ObjRef) (M : ObjRef → Prop) (don : Bool) (p : ObjRef) (x y : Tcb) : Prop where + fault : y.tcbFault = x.tcbFault + frame : y.tcbIpcframe = x.tcbIpcframe + buf : y.tcbIpcBuffer = x.tcbIpcBuffer + mcp : y.tcbMcpriority = x.tcbMcpriority + yt : y.tcbYieldTo = x.tcbYieldTo + caps : tcbCnodeMap y = tcbCnodeMap x + st : y.tcbState = x.tcbState ∨ (M p ∧ y.tcbState ≠ .idleThreadState) + /-- (ruling 14:18Z item 2) A binding changes only with donation, off `i`, and either at a mover or to `none` (only + a mover gains a binding). -/ + sc : y.tcbSchedContext = x.tcbSchedContext ∨ (don = true ∧ p ≠ i ∧ (M p ∨ y.tcbSchedContext = none)) + vcpu : y.tcbArch.tcbVcpu = x.tcbArch.tcbVcpu + /-- (item 11) The TCB flags are kept. -/ + flags : y.tcbFlags = x.tcbFlags + +structure ScIpcA (i : ObjRef) (don : Bool) (p : ObjRef) (x y : SchedContext) : Prop where + yf : y.scYieldFrom = x.scYieldFrom + rm : y.scRefillMax = x.scRefillMax + ne : x.scRefills ≠ [] → y.scRefills ≠ [] + idle : p = idleScPtr → y.scTcb = x.scTcb ∧ y.scReplies = x.scReplies + th : y.scTcb = x.scTcb ∨ (don = true ∧ y.scTcb ≠ some i) + reps : don = false → y.scReplies = x.scReplies + +inductive ObjIpcA (i : ObjRef) (M : ObjRef → Prop) (don : Bool) (p : ObjRef) : + Option KernelObject → Option KernelObject → Prop + | same (o : Option KernelObject) : ObjIpcA i M don p o o + | tcb (x y : Tcb) (h : TcbIpcA i M don p x y) : ObjIpcA i M don p (some (.tcb x)) (some (.tcb y)) + | sc (x y : SchedContext) (k : Nat) (h : ScIpcA i don p x y) : + ObjIpcA i M don p (some (.schedContext x k)) (some (.schedContext y k)) + | ep (a b : Endpoint) (h : ∀ u ∈ a.epQueue, ¬ M u → u ∈ b.epQueue) : + ObjIpcA i M don p (some (.endpoint a)) (some (.endpoint b)) + | reply (a b : Reply) (h : don = false → b.replySc = a.replySc) : + ObjIpcA i M don p (some (.reply a)) (some (.reply b)) + +structure SIKA (i : ObjRef) (M N : ObjRef → Prop) (don : Bool) (σ σ' : State Unit) : Prop where + frame : σ' = { σ with kheap := σ'.kheap, readyQueues := σ'.readyQueues, releaseQueue := σ'.releaseQueue, + schedulerAction := σ'.schedulerAction, reprogramTimer := σ'.reprogramTimer, + machineState := σ'.machineState } + obj : ∀ p, ObjIpcA i M don p (σ.kheap p) (σ'.kheap p) + rq : ∀ d p y, y ∈ σ'.readyQueues d p → y ∈ σ.readyQueues d p ∨ N y ∨ σ.schedulerAction = .switchThread y + rls : ∀ y ∈ σ'.releaseQueue, y ∈ σ.releaseQueue + act : σ'.schedulerAction = σ.schedulerAction ∨ σ'.schedulerAction = .chooseNewThread ∨ + ∃ y, N y ∧ σ'.schedulerAction = .switchThread y + /-- (record edit #8b, item 11) A ready `idle_sc_ptr` head and a ready current-SC head stay ready (`RdyA`). -/ + rdy : RdyA σ → RdyA σ' + +section +variable {i : ObjRef} {M N : ObjRef → Prop} {don : Bool} + +theorem ObjIpcA.trans {p : ObjRef} {a b c : Option KernelObject} (h1 : ObjIpcA i M don p a b) + (h2 : ObjIpcA i M don p b c) : ObjIpcA i M don p a c := by + cases h1 with + | same => exact h2 + | tcb x y h => + cases h2 with + | same => exact .tcb _ _ h + | tcb _ z h' => + refine .tcb _ _ ⟨h'.fault.trans h.fault, h'.frame.trans h.frame, h'.buf.trans h.buf, h'.mcp.trans h.mcp, + h'.yt.trans h.yt, h'.caps.trans h.caps, ?_, ?_, h'.vcpu.trans h.vcpu, h'.flags.trans h.flags⟩ + · rcases h'.st with e | e + · rw [e]; exact h.st + · exact Or.inr e + · rcases h'.sc with e | e + · rw [e]; exact h.sc + · exact Or.inr e + | sc x y k h => + cases h2 with + | same => exact .sc _ _ _ h + | sc _ z _ h' => + refine .sc _ _ _ ⟨h'.yf.trans h.yf, h'.rm.trans h.rm, fun e => h'.ne (h.ne e), + fun e => ⟨(h'.idle e).1.trans (h.idle e).1, (h'.idle e).2.trans (h.idle e).2⟩, ?_, + fun e => (h'.reps e).trans (h.reps e)⟩ + rcases h'.th with e | e + · rw [e]; exact h.th + · exact Or.inr e + | ep a b h => + cases h2 with + | same => exact .ep _ _ h + | ep _ z h' => exact .ep _ _ fun u hu n => h' u (h u hu n) n + | reply a b h => + cases h2 with + | same => exact .reply _ _ h + | reply _ z h' => exact .reply _ _ fun e => (h' e).trans (h e) + +theorem SIKA.refl (σ : State Unit) : SIKA i M N don σ σ := + ⟨rfl, fun _ => .same _, fun _ _ _ h => Or.inl h, fun _ h => h, Or.inl rfl, id⟩ + +theorem SIKA.trans {σ1 σ2 σ3 : State Unit} (h1 : SIKA i M N don σ1 σ2) (h2 : SIKA i M N don σ2 σ3) : + SIKA i M N don σ1 σ3 := by + refine ⟨?_, fun p => (h1.obj p).trans (h2.obj p), fun d p y hy => ?_, fun y hy => h1.rls y (h2.rls y hy), ?_, fun g => h2.rdy (h1.rdy g)⟩ + · have a := h1.frame + have b := h2.frame + rw [a] at b + exact b + · rcases h2.rq d p y hy with h | h | h + · exact h1.rq d p y h + · exact Or.inr (Or.inl h) + · rcases h1.act with e | e | ⟨z, hz, e⟩ + · exact Or.inr (Or.inr (e ▸ h)) + · rw [e] at h; cases h + · rw [e] at h; cases h; exact Or.inr (Or.inl hz) + · rcases h2.act with e | e | e + · rw [e]; exact h1.act + · exact Or.inr (Or.inl e) + · exact Or.inr (Or.inr e) + +instance : PreO (SIKA i M N don) := ⟨SIKA.refl, SIKA.trans⟩ + +theorem SIKA.up {σ σ' : State Unit} (h : SIKA i M N false σ σ') : SIKA i M N true σ σ' := by + refine ⟨h.frame, fun p => ?_, h.rq, h.rls, h.act, h.rdy⟩ + have o := h.obj p + generalize σ.kheap p = a at o ⊢ + generalize σ'.kheap p = b at o ⊢ + cases o with + | same => exact .same _ + | tcb x y h => + exact .tcb _ _ ⟨h.fault, h.frame, h.buf, h.mcp, h.yt, h.caps, h.st, + h.sc.imp id (fun e => (by cases e.1)), h.vcpu, h.flags⟩ + | sc x y k h => exact .sc _ _ _ ⟨h.yf, h.rm, h.ne, h.idle, h.th.imp id fun e => (by cases e.1), fun e => by cases e⟩ + | ep a b h => exact .ep _ _ h + | reply a b h => exact .reply _ _ fun e => by cases e + +theorem SIKA.upTo {σ σ' : State Unit} (h : SIKA i M N false σ σ') : SIKA i M N don σ σ' := by + cases don + · exact h + · exact h.up + +/-! ## State leaves -/ + +theorem idleRdy_nscA {σ : State Unit} {p : ObjRef} {o : KernelObject} (h : ∀ y n, o ≠ .schedContext y n) : + RdyA σ → RdyA { σ with kheap := Function.update σ.kheap p (some o) } := + fun g => g.updNs p o h + +theorem idleRdy_scA {σ : State Unit} {p : ObjRef} {x y : SchedContext} {n : Nat} + (hk : σ.kheap p = some (.schedContext x n)) (h : y.scRefills = x.scRefills) + (hm : y.scRefillMax = x.scRefillMax) : + RdyA σ → RdyA { σ with kheap := Function.update σ.kheap p (some (.schedContext y n)) } := fun g => + g.updSc hk hm fun hr => by + unfold refillHd at hr ⊢ + rw [h]; exact hr + +/-- One object replaced. -/ +theorem SIKA.upd {σ : State Unit} {p : ObjRef} {o o' : KernelObject} (h : σ.kheap p = some o) + (ho : ObjIpcA i M don p (some o) (some o')) + (hr : RdyA σ → RdyA { σ with kheap := Function.update σ.kheap p (some o') }) : + SIKA i M N don σ { σ with kheap := Function.update σ.kheap p (some o') } := by + refine ⟨rfl, fun q => ?_, fun _ _ _ a => Or.inl a, fun _ a => a, Or.inl rfl, hr⟩ + show ObjIpcA i M don q (σ.kheap q) (Function.update σ.kheap p (some o') q) + by_cases e : q = p + · subst e; rw [Function.update_self, h]; exact ho + · rw [Function.update_of_ne e]; exact .same _ + +theorem SIKA.ofQueueE {P : ObjRef → Prop} {σ σ' : State Unit} (Q : QueueE P σ σ') + (hP : ∀ y, P y → N y ∨ σ.schedulerAction = .switchThread y) + (hA : σ'.schedulerAction = σ.schedulerAction ∨ σ'.schedulerAction = .chooseNewThread ∨ + ∃ y, N y ∧ σ'.schedulerAction = .switchThread y) : SIKA i M N don σ σ' := by + have e := Q.1 + have hk : σ'.kheap = σ.kheap := by rw [e] + refine ⟨by rw [e], fun p => by rw [hk]; exact .same _, fun d p y hy => ?_, fun y hy => by rw [e] at hy; exact hy, + hA, fun g => RdyA.ofFrameA hk (by rw [e]) (by rw [e]) (by rw [e]) g⟩ + rcases Q.2 d p y hy with h | h + · exact Or.inl h + · exact Or.inr (hP y h) + +/-! ## Primitive computations -/ + +theorem eqPres_sikA {α : Type} {m : SMonad Unit α} (h : PresR (@Eq (State Unit)) m) : PresR (SIKA i M N don) m := + fun s x hx => by rw [← h s x hx]; exact SIKA.refl s + +theorem getSimpleKo_eqPres {α : Type} [Nonempty α] (C : α → KernelObject) (p : ObjRef) : + PresR (@Eq (State Unit)) (getSimpleKo (Z := Unit) C p) := by + unfold getSimpleKo + exact PresR.bind (getObject_eqPres p) (fun _ => by eqpres) + +theorem getReplyKo_memA {σ : State Unit} {p : ObjRef} {r : Reply × State Unit} + (h : r ∈ (getSimpleKo (Z := Unit) KernelObject.reply p σ).1) : σ.kheap p = some (.reply r.1) := by + unfold getSimpleKo at h + obtain ⟨y1, hy1, h1⟩ := mem_bindA.1 h + have hy1' : y1 ∈ (NonDet.assertOpt (σ.kheap p) σ).1 := hy1 + cases hk : σ.kheap p with + | none => rw [hk] at hy1'; exact absurd hy1' (Set.notMem_empty _) + | some ko => + rw [hk] at hy1' + have e1 : y1 = (ko, σ) := hy1' + subst e1 + obtain ⟨y2, hy2, h2⟩ := mem_bindA.1 h1 + obtain ⟨hP2, rfl⟩ := hy2 + cases hp : partialInv KernelObject.reply ko with + | none => rw [hp] at h2; exact absurd h2 (Set.notMem_empty _) + | some e => + rw [hp] at h2 + have e2 : r = (e, σ) := h2 + subst e2 + unfold partialInv at hp + by_cases hu : ∃! y, KernelObject.reply y = ko + · rw [if_pos hu] at hp + have := Option.some.inj hp + unfold The at this + rw [dif_pos hu] at this + rw [← this]; exact congrArg some (Classical.choose_spec hu).1.symm + · rw [if_neg hu] at hp; cases hp + +theorem setReplyObjRef_sikA (f : (Option ObjRef → Option ObjRef) → Reply → Reply) (p : ObjRef) (v : Option ObjRef) + (hf : don = false → ∀ g a, (f g a).replySc = a.replySc) : + PresR (SIKA i M N don) (setReplyObjRef (Z := Unit) f p v) := by + intro σ r hr + unfold setReplyObjRef updateSkObjRef at hr + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + have ex : σ = x.2 := getSimpleKo_eqPres _ p σ x hx + rw [← ex] at hr + have hk := getReplyKo_memA hx + obtain ⟨-, -, hr2⟩ := setSimpleKo_memA _ _ _ hr + rw [hr2] + exact SIKA.upd hk (.reply _ _ fun e => hf e _ _) (idleRdy_nscA (by intro _ _ c; cases c)) + +theorem asUser_sikA {α : Type} (t : ObjRef) (f : UserMonad α) : PresR (SIKA i M N don) (asUser (Z := Unit) t f) := by + intro σ r hr + obtain ⟨x, uc, hk, e⟩ := asUser_memA t f hr + rw [e] + exact SIKA.upd hk (.tcb _ _ ⟨rfl, rfl, rfl, rfl, rfl, rfl, Or.inl rfl, Or.inl rfl, rfl, rfl⟩) + (idleRdy_nscA (by intro _ _ c; cases c)) + +theorem doMachineOp_sikA {α : Type} (mop : NonDet MachineState α) : + PresR (SIKA i M N don) (doMachineOp (Z := Unit) mop) := by + intro σ r hr + obtain ⟨y1, hy1, hr⟩ := hr + have e1 : y1 = (σ.machineState, σ) := hy1 + subst e1 + obtain ⟨⟨⟨a, ms'⟩, s2⟩, hy2, hr⟩ := hr + have e2 : s2 = σ := hy2.2 + subst e2 + obtain ⟨y3, hy3, hr⟩ := hr + have e3 : y3 = ((), { s2 with machineState := ms' }) := hy3 + subst e3 + have e4 : r = (a, { s2 with machineState := ms' }) := hr + rw [e4] + exact ⟨rfl, fun _ => .same _, fun _ _ _ a => Or.inl a, fun _ a => a, Or.inl rfl, id⟩ + +theorem threadSetSt_sikA (t : ObjRef) (hM : M t) (ts : ThreadState) (hts : ts ≠ .idleThreadState) : + PresR (SIKA i M N don) (threadSet (Z := Unit) (fun tcb => { tcb with tcbState := ts }) t) := by + intro σ r hr + obtain ⟨x, hk, e⟩ := threadSet_memA _ t hr + rw [e] + exact SIKA.upd hk (.tcb _ _ ⟨rfl, rfl, rfl, rfl, rfl, rfl, Or.inr ⟨hM, hts⟩, Or.inl rfl, rfl, rfl⟩) + (idleRdy_nscA (by intro _ _ c; cases c)) + +theorem setThreadState_sikA (t : ObjRef) (hM : M t) (ts : ThreadState) (hts : ts ≠ .idleThreadState) : + PresR (SIKA i M N don) (setThreadState (Z := Unit) t ts) := by + unfold Fl4SpecA64.setThreadState + refine PresR.bind (threadSetSt_sikA t hM ts hts) (fun _ => fun σ r hr => ?_) + rw [setThreadStateAct_eq] at hr + have : r = ((), stsAct σ t) := hr + rw [this] + unfold stsAct + split_ifs + · exact SIKA.ofQueueE (P := fun _ => False) ⟨rfl, fun _ _ _ a => Or.inl a⟩ (fun _ f => f.elim) + (Or.inr (Or.inl rfl)) + · exact SIKA.refl σ + +theorem threadSetSc_sikA (t : ObjRef) (ht : t ≠ i) (v : Option ObjRef) (hv : M t ∨ v = none) : + PresR (SIKA i M N true) (setTcbObjRef (Z := Unit) tcbSchedContextUpdate t v) := by + intro σ r hr + obtain ⟨x, hk, e⟩ := threadSet_memA _ t hr + rw [e] + exact SIKA.upd hk (.tcb _ _ ⟨rfl, rfl, rfl, rfl, rfl, rfl, Or.inl rfl, Or.inr ⟨rfl, ht, hv⟩, rfl, rfl⟩) + (idleRdy_nscA (by intro _ _ c; cases c)) + +theorem updateSc_sikA (p : ObjRef) (f : SchedContext → SchedContext) (hf : ∀ x, ScIpcA i don p x (f x)) + (hrf : ∀ x, (f x).scRefills = x.scRefills) : + PresR (SIKA i M N don) (updateSchedContext (Z := Unit) p f) := by + intro σ r hr + obtain ⟨y, n, hk, e⟩ := updateSchedContext_memA p f hr + rw [e] + exact SIKA.upd hk (.sc _ _ _ (hf y)) (idleRdy_scA hk (hrf y) (hf y).rm) + +theorem tcbSchedDeq_sikA (t : ObjRef) : PresR (SIKA i M N don) (tcbSchedAction (Z := Unit) tcbSchedDequeue t) := by + intro σ r hr + unfold tcbSchedAction at hr + obtain ⟨d, -, hr⟩ := eqPres_mem (threadGet_eqPres _ t) hr + obtain ⟨pr, -, hr⟩ := eqPres_mem (threadGet_eqPres _ t) hr + have hq : PresR (@Eq (State Unit)) (getTcbQueue (Z := Unit) d pr) := by unfold getTcbQueue; eqpres + obtain ⟨q, hq', hr⟩ := eqPres_mem hq hr + have eq : q = σ.readyQueues d pr := by + unfold getTcbQueue at hq' + obtain ⟨z, hz, hq'⟩ := mem_bindA.1 hq' + have ez : z = (σ.readyQueues, σ) := hz + subst ez + exact congrArg Prod.fst (show (q, σ) = (σ.readyQueues d pr, σ) from hq') + obtain ⟨q', hq'', hr⟩ := mem_bindA.1 hr + have eq' : q' = (tcbSchedDequeue t q, σ) := hq'' + subst eq' + have er : r = ((), { σ with readyQueues := fun d' p => + if d' = d ∧ p = pr then tcbSchedDequeue t q else σ.readyQueues d' p }) := hr + rw [er] + refine SIKA.ofQueueE (P := fun _ => False) ⟨rfl, fun d' p y hy => Or.inl ?_⟩ (fun _ f => f.elim) (Or.inl rfl) + simp only at hy + split_ifs at hy with h + · rw [h.1, h.2, ← eq]; exact (List.mem_filter.1 hy).1 + · exact hy + +theorem tcbReleaseRemove_sikA (t : ObjRef) : PresR (SIKA i M N don) (tcbReleaseRemove (Z := Unit) t) := by + unfold tcbReleaseRemove + intro σ r hr + obtain ⟨qs, hqs, hr⟩ := mem_bindA.1 hr + have e : qs = (σ.releaseQueue, σ) := hqs + subst e + obtain ⟨u, hu, hr⟩ := mem_bindA.1 hr + have hu' : u.2.releaseQueue = σ.releaseQueue ∧ SIKA i M N don σ u.2 := by + unfold NonDet.when at hu + split_ifs at hu + · have : u = ((), { σ with reprogramTimer := true }) := hu + rw [this] + exact ⟨rfl, rfl, fun _ => .same _, fun _ _ _ a => Or.inl a, fun _ a => a, Or.inl rfl, id⟩ + · have : u = ((), σ) := hu + rw [this]; exact ⟨rfl, SIKA.refl σ⟩ + obtain ⟨q', hq', hr⟩ := mem_bindA.1 hr + have eq : q' = (tcbSchedDequeue t σ.releaseQueue, u.2) := hq' + subst eq + have er : r = ((), { u.2 with releaseQueue := tcbSchedDequeue t σ.releaseQueue }) := hr + rw [er] + refine hu'.2.trans ⟨rfl, fun _ => .same _, fun _ _ _ a => Or.inl a, fun y hy => ?_, Or.inl rfl, id⟩ + rw [hu'.1]; exact (List.mem_filter.1 hy).1 + +theorem rescheduleRequired_sikA : PresR (SIKA i M N don) (rescheduleRequired (Z := Unit)) := fun σ r hr => by + obtain ⟨q, e⟩ := rescheduleRequired_memA hr + exact SIKA.ofQueueE q (fun _ h => Or.inr h.1) (Or.inr (Or.inl e)) + +theorem possibleSwitchTo_sikA (t : ObjRef) (ht : N t) : PresR (SIKA i M N don) (possibleSwitchTo (Z := Unit) t) := + fun σ r hr => by + obtain ⟨q, e⟩ := possibleSwitchTo_memA t hr + refine SIKA.ofQueueE q (fun y h => ?_) ?_ + · rcases h with h | h + · exact Or.inl (h ▸ ht) + · exact Or.inr h.1 + · rcases e with e | e | e + · exact Or.inl e + · exact Or.inr (Or.inl e) + · exact Or.inr (Or.inr ⟨t, ht, e⟩) + +theorem testReschedule_sikA (t : ObjRef) : PresR (SIKA i M N don) (testReschedule (Z := Unit) t) := by + unfold testReschedule + refine PresR.bind (PresR.gets _) (fun _ => PresR.bind (PresR.gets _) (fun _ => ?_)) + exact PresR.bind (PresR.pure _) (fun _ => PresR.when rescheduleRequired_sikA) + +theorem tcbEpDequeue_sikA (t e : ObjRef) (hM : M t) : PresR (SIKA i M N don) (tcbEpDequeue (Z := Unit) t e) := by + intro σ r hr + unfold tcbEpDequeue at hr + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + obtain ⟨hk, hs⟩ := getEndpoint_mem hx + rw [hs] at hr + obtain ⟨q, hq, hr⟩ := eqPres_mem (PresR.pure _) hr + obtain ⟨_, -, hr⟩ := eqPres_mem (PresR.assert _) hr + obtain ⟨q', hq', hr⟩ := eqPres_mem (PresR.pure _) hr + obtain ⟨ep', hep, hr⟩ := eqPres_mem (PresR.pure _) hr + have e1 : q = x.1.epQueue := congrArg Prod.fst hq + have e2 : q' = removeAll t q := congrArg Prod.fst hq' + have e3 := congrArg Prod.fst hep + dsimp only at e3 + obtain ⟨y, hy, hr2⟩ := setSimpleKo_memA KernelObject.endpoint e ep' hr + have ey : x.1 = y := by + rw [hk] at hy + exact KernelObject.endpoint.inj (Option.some.inj hy) + rw [hr2] + refine SIKA.upd hy (.ep _ _ fun u hu hn => ?_) (idleRdy_nscA (by intro _ _ c; cases c)) + have hu' : u ∈ q' := by + rw [e2, e1, ey] + unfold removeAll + exact List.mem_filter.2 ⟨hu, decide_eq_true fun (c : t = u) => hn (c ▸ hM)⟩ + rw [e3, ey] + cases q' with + | nil => cases hu' + | cons a l => + dsimp only + cases y with + | idleEP => cases hu + | sendEP _ => exact hu' + | recvEP _ => exact hu' + +theorem replyUnlinkTcb_sikA (t r : ObjRef) (hM : M t) : PresR (SIKA i M N don) (replyUnlinkTcb (Z := Unit) t r) := by + unfold replyUnlinkTcb + refine PresR.bind (eqPres_sikA (getReply_eqPres r)) (fun _ => PresR.bind (PresR.assert _) (fun _ => ?_)) + refine PresR.bind (eqPres_sikA ?_) (fun _ => PresR.bind (PresR.assert _) (fun _ => ?_)) + · unfold getThreadState; exact threadGet_eqPres _ _ + exact PresR.bind (setReplyObjRef_sikA _ _ _ fun _ _ _ => rfl) (fun _ => setThreadState_sikA t hM _ (by intro h; cases h)) + +/-! ## The refill check -/ + +/-- A refill-list rewrite of one SC that keeps non-emptiness and a ready head. -/ +theorem SIKA.updRef {σ : State Unit} {p : ObjRef} {y : SchedContext} {n : Nat} + (hk : σ.kheap p = some (.schedContext y n)) (l : List Refill) (hne : y.scRefills ≠ [] → l ≠ []) + (hr : (refillHd y).rTime ≤ σ.curTime → (refillHd { y with scRefills := l }).rTime ≤ σ.curTime) : + SIKA i M N don σ { σ with kheap := Function.update σ.kheap p (some (.schedContext { y with scRefills := l } n)) } := + SIKA.upd hk (.sc _ _ _ ⟨rfl, rfl, hne, fun _ => ⟨rfl, rfl⟩, Or.inl rfl, fun _ => rfl⟩) + (fun g => g.updSc hk rfl hr) + +theorem updateRefillHd_sikA (p : ObjRef) (f : Refill → Refill) {σ : State Unit} + (hf : ∀ x : Refill, x.rTime ≤ σ.curTime → (f x).rTime ≤ σ.curTime) {r : Unit × State Unit} + (hr : r ∈ (updateRefillHd (Z := Unit) p f σ).1) : SIKA i M N don σ r.2 := by + unfold updateRefillHd at hr + obtain ⟨y, n, hk, e⟩ := updateSchedContext_memA p _ hr + rw [e] + exact SIKA.updRef hk _ (fun _ => List.cons_ne_nil _ _) (hf _) + +theorem mergeOverlappingRefills_sikA (p : ObjRef) : PresR (SIKA i M N don) (mergeOverlappingRefills (Z := Unit) p) := by + intro σ r hr + unfold mergeOverlappingRefills refillPopHead at hr + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + obtain ⟨hd, hhd, hx⟩ := mem_bindA.1 hx + have ehd : σ = hd.2 := by + have : PresR (@Eq (State Unit)) (getRefillHead (Z := Unit) p) := by + unfold getRefillHead; exact PresR.getsThe _ + exact this σ hd hhd + rw [← ehd] at hx + obtain ⟨u, hu, hx⟩ := mem_bindA.1 hx + have ex : x.2 = u.2 := by + have : x = (hd.1, u.2) := hx + rw [this] + obtain ⟨y, n, hk, e1⟩ := updateSchedContext_memA p _ hu + rw [ex] at hr + unfold updateRefillHd at hr + obtain ⟨y', n', hk', e2⟩ := updateSchedContext_memA p _ hr + rw [e1] at hk' e2 + simp only [Function.update_self] at hk' + injection hk' with hk' + injection hk' with hy hn + subst hn + rw [e2] + simp only [Function.update_idem] + rw [← hy] + have ehv : hd.1 = refillHd y := by + by_cases ne : y.scRefills = [] + · exfalso + have rn : readRefillHead (Z := Unit) p σ = none := by + show Option.bind (readSchedContext p σ) (fun sc => Option.bind (Fl4Spec.Lookup.oassert (sc.scRefills ≠ []) σ) + (fun _ => Fl4Spec.Lookup.oreturn (refillHd sc) σ)) = _ + rw [Sched.readSchedContext_eqA hk] + simp [Fl4Spec.Lookup.oassert, ne, Fl4Spec.Lookup.ofail] + have g := hhd + simp [getRefillHead, Fl4Spec.NonDet.getsThe, rn, Fl4Spec.NonDet.assertOpt, Fl4Spec.NonDet.fail] at g + · have g := hhd + rw [Sched.getRefillHead_eqA hk ne] at g + exact congrArg Prod.fst g + have ex1 : x.1 = hd.1 := by + have t : x = (hd.1, u.2) := hx + rw [t] + refine SIKA.updRef hk _ (fun _ => List.cons_ne_nil _ _) (fun g => ?_) + show x.1.rTime ≤ σ.curTime + rw [ex1, ehv]; exact g + +theorem refillCheck_sikA (o : Option ObjRef) : + PresR (SIKA i M N don) (ifSporadicCurScTestRefillUnblockCheck (Z := Unit) o) := by + have ruc : ∀ p, PresR (SIKA i M N don) (refillUnblockCheck (Z := Unit) p) := fun p => by + unfold refillUnblockCheck + refine PresR.bind (eqPres_sikA ?_) (fun _ => PresR.bind (eqPres_sikA (getScRefillReady_eqPres p)) (fun _ => ?_)) + · unfold isRoundRobin + exact PresR.bind (getSchedContext_eqPres p) (fun _ => PresR.pure _) + refine PresR.when ?_ + intro σ r hr + obtain ⟨ct, hct, hr⟩ := eqPres_mem (PresR.gets _) hr + have ect : ct = σ.curTime := congrArg Prod.fst (show (ct, σ) = (σ.curTime, σ) from hct) + obtain ⟨u, hu, hr⟩ := mem_bindA.1 hr + have R1 : SIKA i M N don σ u.2 := updateRefillHd_sikA _ _ (fun _ _ => by rw [ect]; exact BitVec.le_refl _) hu + have rest : PresR (SIKA i M N don) (NonDet.modify (fun s : State Unit => { s with reprogramTimer := true }) >>= fun _ => + refillHeadOverlappingLoop (Z := Unit) p) := by + refine PresR.bind (PresR.modify _ (fun σ => ?_)) (fun _ => ?_) + · exact ⟨rfl, fun _ => .same _, fun _ _ _ a => Or.inl a, fun _ a => a, Or.inl rfl, id⟩ + · unfold refillHeadOverlappingLoop + exact PresR.whileLoop (fun _ => mergeOverlappingRefills_sikA p) _ + exact R1.trans (rest u.2 r hr) + unfold ifSporadicCurScTestRefillUnblockCheck ifCondRefillUnblockCheck + cases o with + | none => exact PresR.pure _ + | some p => + show PresR _ (_ >>= _) + refine PresR.bind (eqPres_sikA (getSchedContext_eqPres p)) (fun _ => ?_) + refine PresR.bind (PresR.gets _) (fun _ => PresR.bind (PresR.pure _) (fun _ => ?_)) + exact PresR.when (PresR.when (ruc p)) + +/-! ## Donation and the reply push -/ + +/-- `sched_context_donate` of an SC bound to `t` (not `idle_sc_ptr`) to `d`. -/ +theorem schedContextDonate_sikA (sc d t : ObjRef) (hsc : sc ≠ idleScPtr) (hd : d ≠ i) (ht : t ≠ i) (hMd : M d) : + PresRC (SIKA i M N true) (fun s => ∀ y k, s.kheap sc = some (.schedContext y k) → y.scTcb = some t) + (schedContextDonate (Z := Unit) sc d) := by + intro σ hσ r hr + unfold schedContextDonate at hr + obtain ⟨⟨f, s1⟩, h1, hr⟩ := mem_bindA.1 hr + obtain ⟨hs, y, k, hk, hv⟩ := getScObjRef_mem h1 + simp only at hs hv hr + subst hs + have ef : f = some t := hv.trans (hσ y k hk) + subst ef + have tail : PresR (SIKA i M N true) (do + setScObjRef scTcbUpdate sc (some d) + setTcbObjRef (Z := Unit) tcbSchedContextUpdate d (some sc)) := by + refine PresR.bind ?_ (fun _ => threadSetSc_sikA d hd _ (Or.inl hMd)) + exact updateSc_sikA sc _ (fun _ => ⟨rfl, rfl, id, fun e => absurd e hsc, + Or.inr ⟨rfl, fun e => hd (Option.some.inj e)⟩, fun e => by cases e⟩) + (fun _ => rfl) + obtain ⟨u, hu, hr⟩ := mem_bindA.1 hr + refine SIKA.trans ?_ (tail _ _ hr) + unfold NonDet.when at hu + rw [if_pos (by simp)] at hu + have body : PresR (SIKA i M N true) (do + tcbSchedAction (Z := Unit) tcbSchedDequeue t + tcbReleaseRemove t + setTcbObjRef tcbSchedContextUpdate t none + testReschedule t) := + PresR.bind (tcbSchedDeq_sikA _) fun _ => PresR.bind (tcbReleaseRemove_sikA _) fun _ => + PresR.bind (threadSetSc_sikA _ ht _ (Or.inr rfl)) fun _ => testReschedule_sikA _ + exact PresR.bind_pure t _ body s1 u hu + +/-- SC thread links are kept (backwards). -/ +def ScThA (σ σ' : State Unit) : Prop := + ∀ p y k, σ'.kheap p = some (.schedContext y k) → ∃ x, σ.kheap p = some (.schedContext x k) ∧ y.scTcb = x.scTcb + +instance : PreO ScThA := + ⟨fun _ _ y _ h => ⟨y, h, rfl⟩, fun h1 h2 p y k h => by + obtain ⟨x, hx, e⟩ := h2 p y k h + obtain ⟨w, hw, e'⟩ := h1 p x k hx + exact ⟨w, hw, e.trans e'⟩⟩ + +theorem bindScReply_sikA (sc r : ObjRef) (hsc : sc ≠ idleScPtr) : + PresR (SIKA i M N true) (bindScReply (Z := Unit) sc r) := by + unfold bindScReply + refine PresR.bind (eqPres_sikA ?_) (fun l => ?_) + · show PresR (@Eq (State Unit)) (NonDet.liftM SchedContext.scReplies (getSchedContext (Z := Unit) sc)) + unfold NonDet.liftM + exact PresR.bind (getSchedContext_eqPres sc) (fun _ => PresR.pure _) + have rest : PresR (SIKA i M N true) (do + setScObjRef scRepliesUpdate sc (r :: l) + setReplyObjRef (Z := Unit) replyScUpdate r (some sc)) := by + refine PresR.bind ?_ (fun _ => setReplyObjRef_sikA _ _ _ fun e => by cases e) + exact updateSc_sikA sc _ (fun _ => ⟨rfl, rfl, id, fun e => absurd e hsc, Or.inl rfl, fun e => by cases e⟩) + (fun _ => rfl) + cases l with + | nil => exact rest + | cons a l => exact PresR.bind (setReplyObjRef_sikA _ _ _ fun e => by cases e) (fun _ => rest) + +theorem setReplyObjRef_scThA (f : (Option ObjRef → Option ObjRef) → Reply → Reply) (p : ObjRef) (v : Option ObjRef) : + PresR ScThA (setReplyObjRef (Z := Unit) f p v) := by + unfold setReplyObjRef updateSkObjRef + refine PresR.bind (fun s x hx => ?_) (fun _ => fun s x hx => ?_) + · rw [← getSimpleKo_eqPres _ p s x hx]; exact PreO.refl s + · obtain ⟨y, hk, e⟩ := setSimpleKo_memA _ _ _ hx + intro q z k hz + rw [e] at hz + change Function.update s.kheap p _ q = _ at hz + by_cases c : q = p + · rw [c, Function.update_self] at hz; cases hz + · rw [Function.update_of_ne c] at hz; exact ⟨z, hz, rfl⟩ + +theorem bindScReply_scThA (sc r : ObjRef) : PresR ScThA (bindScReply (Z := Unit) sc r) := by + have ro : ∀ {α : Type} {m : SMonad Unit α}, PresR (@Eq (State Unit)) m → PresR ScThA m := + fun h s x hx => by rw [← h s x hx]; exact PreO.refl s + unfold bindScReply + refine PresR.bind (ro ?_) (fun l => ?_) + · show PresR (@Eq (State Unit)) (NonDet.liftM SchedContext.scReplies (getSchedContext (Z := Unit) sc)) + unfold NonDet.liftM + exact PresR.bind (getSchedContext_eqPres sc) (fun _ => PresR.pure _) + have rest : PresR ScThA (do + setScObjRef scRepliesUpdate sc (r :: l) + setReplyObjRef (Z := Unit) replyScUpdate r (some sc)) := by + refine PresR.bind (fun s x hx => ?_) (fun _ => setReplyObjRef_scThA _ _ _) + obtain ⟨y, n, hk, e⟩ := updateSchedContext_memA sc _ hx + intro q z k hz + rw [e] at hz + change Function.update s.kheap sc _ q = _ at hz + by_cases c : q = sc + · subst c + rw [Function.update_self] at hz + cases hz + exact ⟨y, hk, rfl⟩ + · rw [Function.update_of_ne c] at hz; exact ⟨z, hz, rfl⟩ + cases l with + | nil => exact rest + | cons a l => exact PresR.bind (setReplyObjRef_scThA _ _ _) (fun _ => rest) + +/-- A step of the donation-free prefix, along "reached from `σ0` without donation". -/ +theorem PresRC.sik_stepA {α β : Type} {σ0 : State Unit} {m : SMonad Unit α} {f : α → SMonad Unit β} + (hm : PresR (SIKA i M N false) m) (hf : ∀ a, PresRC (SIKA i M N don) (SIKA i M N false σ0) (f a)) : + PresRC (SIKA i M N don) (SIKA i M N false σ0) (NonDet.bind m f) := + PresRC.bind_inv (fun s hs x hx => ⟨(hm s x hx).upTo, hs.trans (hm s x hx)⟩) hf + +theorem PresRC.bind_valA {σ α β : Type} {R : σ → σ → Prop} [PreO R] {P : σ → Prop} {Q : α → Prop} {m : NonDet σ α} + {f : α → NonDet σ β} (hm : ∀ s, P s → ∀ x ∈ (m s).1, R s x.2 ∧ P x.2 ∧ Q x.1) (hf : ∀ a, Q a → PresRC R P (f a)) : + PresRC R P (NonDet.bind m f) := fun s hs x hx => by + obtain ⟨y, hy, hx'⟩ := hx + obtain ⟨h1, h2, h3⟩ := hm s hs y hy + exact PreO.trans h1 (hf y.1 h3 y.2 h2 x hx') + +theorem SIKA.tcbF {σ σ' : State Unit} (R : SIKA i M N don σ σ') {p : ObjRef} {x : Tcb} (h : σ.kheap p = some (.tcb x)) : + ∃ y, σ'.kheap p = some (.tcb y) ∧ TcbIpcA i M don p x y := by + have o := R.obj p + rw [h] at o + generalize eb : σ'.kheap p = b at o + cases o with + | same => exact ⟨x, rfl, rfl, rfl, rfl, rfl, rfl, rfl, Or.inl rfl, Or.inl rfl, rfl, rfl⟩ + | tcb _ y g => exact ⟨y, rfl, g⟩ + +/-- The facts a donation of `t`'s SC needs at the start state. -/ +def DonA (σ : State Unit) (t : ObjRef) (x : Tcb) : Prop := + ∀ sc, x.tcbSchedContext = some sc → sc ≠ idleScPtr ∧ + ∀ y k, σ.kheap sc = some (.schedContext y k) → y.scTcb = some t + +theorem DonA.keep {σ s : State Unit} {t sc : ObjRef} {x : Tcb} (h : DonA σ t x) (hs : x.tcbSchedContext = some sc) + (R : SIKA i M N false σ s) : ∀ y k, s.kheap sc = some (.schedContext y k) → y.scTcb = some t := by + intro y k e + have o := R.obj sc + rw [e] at o + generalize ea : σ.kheap sc = a at o + cases o with + | same => exact (h sc hs).2 y k ea + | sc x0 _ _ g => + rcases g.th with g | g + · rw [g]; exact (h sc hs).2 x0 k ea + · cases g.1 + +theorem SIKA.tcbScA {σ s : State Unit} {t : ObjRef} {x : Tcb} (R : SIKA i M N false σ s) + (h : σ.kheap t = some (.tcb x)) : ∃ y, s.kheap t = some (.tcb y) ∧ y.tcbSchedContext = x.tcbSchedContext := by + have o := R.obj t + rw [h] at o + generalize eb : s.kheap t = b at o + cases o with + | same => exact ⟨x, rfl, rfl⟩ + | tcb _ y g => + rcases g.sc with e | e + · exact ⟨y, rfl, e⟩ + · cases e.1 + +theorem replyPush_sikA {σ : State Unit} {t d r : ObjRef} {x : Tcb} (hk : σ.kheap t = some (.tcb x)) + (hD : DonA σ t x) (hM : M t) (hMd : M d) (hti : t ≠ i) (hdi : d ≠ i) (cd : Bool) : + PresRC (SIKA i M N true) (SIKA i M N false σ) (replyPush (Z := Unit) t d r cd) := by + unfold replyPush + intro s hs z hz + obtain ⟨⟨c, s1⟩, h1, hz⟩ := mem_bindA.1 hz + unfold getTcbObjRef at h1 + obtain ⟨y, hy, ey⟩ := SIKA.tcbScA hs hk + have e1 := threadGet_valA Tcb.tcbSchedContext hy h1 + obtain ⟨ec, es⟩ := Prod.mk.inj e1 + subst es + rw [ey] at ec + subst ec + refine PresRC.sik_stepA (σ0 := σ) (eqPres_sikA (threadGet_eqPres _ d)) (fun cc => ?_) s1 hs z hz + refine PresRC.sik_stepA (setReplyObjRef_sikA _ _ _ fun _ _ _ => rfl) (fun _ => ?_) + refine PresRC.sik_stepA (setThreadState_sikA t hM _ (by intro h; cases h)) (fun _ => ?_) + intro s2 hs2 w hw + unfold NonDet.when at hw + split_ifs at hw with hg + swap + · have : w = ((), s2) := hw + rw [this]; exact SIKA.refl _ + cases hsc : x.tcbSchedContext with + | none => exact absurd hsc hg.1 + | some sc => + rw [hsc] at hw + obtain ⟨u, hu, hw⟩ := mem_bindA.1 hw + refine (bindScReply_sikA sc r (hD sc hsc).1 s2 u hu).trans + (schedContextDonate_sikA sc d t (hD sc hsc).1 hdi hti hMd u.2 (fun y k e => ?_) w hw) + obtain ⟨x0, e0, th⟩ := bindScReply_scThA sc r s2 u hu sc y k e + rw [th]; exact hD.keep hsc hs2 x0 k e0 + +/-! ## `send_ipc` -/ + +/-- The user-memory frame together with `SIKA`. -/ +def UKA (B : Word → Prop) (i : ObjRef) (M N : ObjRef → Prop) (σ σ' : State Unit) : Prop := + UFrA B σ σ' ∧ SIKA i M N false σ σ' + +instance (B : Word → Prop) : PreO (UKA B i M N) := + ⟨fun s => ⟨PreO.refl s, SIKA.refl s⟩, fun h1 h2 => ⟨PreO.trans h1.1 h2.1, h1.2.trans h2.2⟩⟩ + +section +open fl4_core_a64 Fl4ProofsA64.P1 +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- `do_ipc_transfer` (no fault, no extra caps) is `SIKA`. -/ +theorem doIpcTransfer_sikA {th : TypedHeapA} {kb : U64} (hPl : PlaceA th kb) (sf : S) {σ : State Unit} + (hR : StateRelA th (L.mem sf) σ) (hV : SlotsValidA th (L.mem sf)) {snd rcv : U64} + (htr : th rcv = some .tcb) {xs xr : Tcb} (hks : σ.kheap (specPtr snd) = some (.tcb xs)) + (hkr : σ.kheap (specPtr rcv) = some (.tcb xr)) (hf : xs.tcbFault = none) + (hal : ∀ rb, ipcBufOfA true xr = some rb → (bufAddr rb 0).toNat % 8 = 0) + (ep : Option ObjRef) (badge : Badge) (grant : Bool) + (hcaps : grant = false ∨ ipcBufOfA false xs = none ∨ + unat (dataToMessageInfo (xs.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0) : + PresRC (SIKA i M N false) (· = σ) (doIpcTransfer (Z := Unit) (specPtr snd) ep badge grant (specPtr rcv)) := by + intro s hs x hx + refine (doIpcTransfer_genA (R := UKA (BufBytesA (ipcBufOfA true xr)) i M N) (B := BufBytesA (ipcBufOfA true xr)) + (fun _ _ h => h.1) (fun t f s y hy => ⟨UFrA.of_fr (asUser_presA t f s y hy), asUser_sikA t f s y hy⟩) + (fun a s y hy => ⟨UFrA.of_fr (doMachineOp_presA _ (loadWord_keepA a) s y hy), doMachineOp_sikA _ s y hy⟩) + (rbo := ipcBufOfA true xr) (fun rb e k hk v s y hy => ⟨e ▸ storeWordOffs_ufrA rb hk v s y hy, ?_⟩) + hPl sf hR hV htr hks hkr rfl hf hal ep badge grant hcaps s hs x hx).2 + unfold storeWordOffs at hy + exact (PresR.bind (R := SIKA i M N false) PresR.get fun _ => PresR.bind (PresR.assert _) fun _ => + doMachineOp_sikA _) s y hy + +/-- **Every outcome of `send_ipc` to a waiting receiver `d` is `SIKA`** (module docstring). -/ +theorem sendIpc_recv_sikA {th : TypedHeapA} {kb : U64} (hPl : PlaceA th kb) (sf : S) {σ : State Unit} + (hR : StateRelA th (L.mem sf) σ) (hV : SlotsValidA th (L.mem sf)) {t d : U64} {e : ObjRef} {q : List ObjRef} + (htr : th d = some .tcb) (hk : σ.kheap e = some (.endpoint (.recvEP (specPtr d :: q)))) + {xt xd : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) (hkd : σ.kheap (specPtr d) = some (.tcb xd)) + (hf : xt.tcbFault = none) (hal : ∀ rb, ipcBufOfA true xd = some rb → (bufAddr rb 0).toNat % 8 = 0) + (blk call : Bool) (badge : Badge) (g gr cd : Bool) + (hcaps : g = false ∨ ipcBufOfA false xt = none ∨ + unat (dataToMessageInfo (xt.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0) + (hMt : M (specPtr t)) (hMd : M (specPtr d)) (hNd : N (specPtr d)) + (hti : specPtr t ≠ i) (hdi : specPtr d ≠ i) (hD : DonA σ (specPtr t) xt) + (hcd : cd = true → xt.tcbSchedContext ≠ none) {don : Bool} + (hdon : don = false → cd = false ∧ (call = false ∨ (g = false ∧ gr = false) ∨ + ∀ e' r dd, xd.tcbState ≠ .blockedOnReceive e' (some r) dd)) : + ∀ x ∈ (sendIpc (Z := Unit) blk call badge g gr cd (specPtr t) e σ).1, SIKA i M N don σ x.2 := by + suffices H : PresRC (SIKA i M N don) (· = σ) + (sendIpc (Z := Unit) blk call badge g gr cd (specPtr t) e) from fun x hx => H σ rfl x hx + unfold sendIpc + refine PresRC.bind_oneA (getEndpoint_eq hk) ?_ + refine PresRC.monoPA (P' := fun s => TcbKeepA σ s ∧ SchedFrameA σ s ∧ SIKA i M N false σ s) + (fun s h => h ▸ ⟨fun _ _ h => h, PreO.refl s, SIKA.refl s⟩) ?_ + cases blk <;> dsimp only + all_goals + refine PresRC.bind_inv (fun s hs x hx => ?_) (fun _ => ?_) + · have h1 := tcbEpDequeue_sikA (i := i) (M := M) (N := N) (don := false) _ e hMd s x hx + have h2 : SchedFrameA s x.2 := SPresInst.pres s x hx + exact ⟨h1.upTo, PreO.trans hs.1 (TKInst.pres s x hx), PreO.trans hs.2.1 h2, hs.2.2.trans h1⟩ + refine PresRC.bind_valA (P := fun s => TcbKeepA σ s ∧ SchedFrameA σ s ∧ SIKA i M N false σ s) + (Q := fun rs => rs = xd.tcbState) (fun s hs x hx => ?_) (fun recvState hrs => ?_) + · have hz : x = (xd.tcbState, s) := by + unfold getThreadState at hx + exact threadGet_valA _ (hs.1 _ _ hkd) hx + subst hz; exact ⟨SIKA.refl s, hs, rfl⟩ + cases recvState + case blockedOnReceive a r dd => + dsimp only + refine PresRC.bind_pureA r ?_ + refine PresRC.monoPA (P' := fun s => TcbKeepA σ s ∧ SchedFrameA σ s ∧ SIKA i M N false σ s) (fun _ h => h) ?_ + intro s hs x hx + obtain ⟨y, hy, hx⟩ := hx + have hT : SIKA i M N false s y.2 := + doIpcTransfer_sikA hPl sf (hR.specSchedA hs.2.1.1) hV htr (hs.1 _ _ hkt) (hs.1 _ _ hkd) hf hal _ _ _ hcaps + s rfl y hy + refine SIKA.trans hT.upTo ?_ + refine (?_ : PresRC (SIKA i M N don) (SIKA i M N false σ) _) y.2 (hs.2.2.trans hT) x hx + refine PresRC.sik_stepA ?_ (fun _ => ?_) + · cases r with + | none => exact PresR.pure _ + | some r => exact replyUnlinkTcb_sikA _ _ hMd + refine PresRC.sik_stepA (eqPres_sikA (threadGet_eqPres _ _)) (fun scOpt => ?_) + refine PresRC.bind_valA (Q := fun fault => fault = none) (fun s hs x hx => ?_) (fun fault hfault => ?_) + · obtain ⟨y, hy, g⟩ := SIKA.tcbF hs hkt + have hz := threadGet_valA Tcb.tcbFault hy hx + subst hz + exact ⟨SIKA.refl s, hs, g.fault.trans hf⟩ + have tail : PresR (SIKA i M N don) (do + setThreadState (specPtr d) .running + let destSc ← getTcbObjRef Tcb.tcbSchedContext (specPtr d) + ifSporadicCurScTestRefillUnblockCheck destSc + possibleSwitchTo (Z := Unit) (specPtr d)) := + PresR.bind (setThreadState_sikA _ hMd _ (by intro h; cases h)) fun _ => + PresR.bind (eqPres_sikA (threadGet_eqPres _ _)) fun _ => + PresR.bind (refillCheck_sikA _) fun _ => possibleSwitchTo_sikA _ hNd + have inact : PresRC (SIKA i M N don) (SIKA i M N false σ) (setThreadState (Z := Unit) (specPtr t) .inactive) := + PresRC.of_pres fun s x hx => (setThreadState_sikA (don := false) _ hMt _ (by intro h; cases h) s x hx).upTo + split_ifs with c1 c2 + · refine PresRC.bind_last ?_ (fun _ => tail) + cases r with + | none => exact absurd rfl c2.2 + | some r => + cases don with + | true => exact replyPush_sikA hkt hD hMt hMd hti hdi _ + | false => + rcases (hdon rfl).2 with rfl | ⟨rfl, rfl⟩ | hr + · exact absurd c1 (by simp [hfault]) + · exact absurd c2.1 (by simp) + · exact absurd hrs.symm (hr _ _ _) + · exact PresRC.bind_last inact (fun _ => tail) + · refine PresRC.bind_last ?_ (fun _ => tail) + intro s hs x hx + unfold NonDet.when at hx + split_ifs at hx with hg + swap + · have : x = ((), s) := hx + rw [this]; exact SIKA.refl _ + cases don with + | false => rw [(hdon rfl).1] at hg; exact absurd hg.1 (by simp) + | true => + obtain ⟨⟨c, s1⟩, h1, hx⟩ := mem_bindA.1 hx + unfold getTcbObjRef at h1 + obtain ⟨y, hy, ey⟩ := SIKA.tcbScA hs hkt + obtain ⟨ec, es⟩ := Prod.mk.inj (threadGet_valA Tcb.tcbSchedContext hy h1) + subst es + rw [ey] at ec + subst ec + cases hsc : xt.tcbSchedContext with + | none => exact absurd hsc (hcd hg.1) + | some sc => + rw [hsc] at hx + exact schedContextDonate_sikA sc (specPtr d) (specPtr t) (hD sc hsc).1 hdi hti hMd s1 (hD.keep hsc hs) x hx + all_goals (dsimp only; exact PresRC.bind_fail) + +end + +/-- **The receiver ends `Running`** in every outcome of `send_ipc` to it. -/ +theorem sendIpc_recv_runningA {σ : State Unit} {t d e : ObjRef} {q : List ObjRef} + (hk : σ.kheap e = some (.endpoint (.recvEP (d :: q)))) (blk call : Bool) (badge : Badge) (g gr cd : Bool) : + ∀ x ∈ (sendIpc (Z := Unit) blk call badge g gr cd t e σ).1, StIs d .running x.2 := by + have skip : ∀ {α β : Type} {m : SMonad Unit α} {f : α → SMonad Unit β} {Q : β → State Unit → Prop}, + (∀ a, HT (fun _ => True) (f a) Q) → HT (fun _ => True) (m >>= f) Q := + fun h => HT.bind (Q := fun _ _ => True) (fun _ _ _ _ => trivial) h + have tail : HT (fun _ => True) (do + setThreadState (Z := Unit) d .running + let destSc ← getTcbObjRef Tcb.tcbSchedContext d + ifSporadicCurScTestRefillUnblockCheck destSc + possibleSwitchTo d) (fun _ σ => StIs d .running σ) := by + refine HT.bind (Q := fun _ σ => StIs d .running σ) ?_ fun _ => ?_ + · unfold Fl4SpecA64.setThreadState + refine HT.bind (Q := fun _ σ => StIs d .running σ) (fun σ _ r hr y hy => ?_) fun _ σ h r hr => ?_ + · obtain ⟨x, -, e⟩ := threadSet_memA _ d hr + rw [e] at hy + have : ({ x with tcbState := .running } : Tcb) = y := by simpa using hy + rw [← this] + · rw [setThreadStateAct_eq] at hr + have e : r = ((), stsAct σ d) := hr + rw [e] + exact fun y hy => h y (by rwa [stsAct_kheap] at hy) + refine HT.bind (Q := fun _ σ => StIs d .running σ) (fun σ h r hr => by + have e : r.2 = σ := ((threadGet_eqPres _ _) σ r hr).symm + rw [e]; exact h) fun sc => ?_ + refine HT.bind (Q := fun _ σ => StIs d .running σ) (fun σ h r hr => + (StIs.frame d .running).keep σ _ ((by unfold ifSporadicCurScTestRefillUnblockCheck ifCondRefillUnblockCheck; tpres : + TPres (ifSporadicCurScTestRefillUnblockCheck (Z := Unit) sc)) σ r hr) h) fun _ => ?_ + intro σ h r hr y hy + exact h y (((KPresInst.pres : PresR TcbCellsEq (possibleSwitchTo (Z := Unit) d)) σ r hr).2 _ _ |>.2 hy) + suffices H : HT (fun s => s = σ) (sendIpc (Z := Unit) blk call badge g gr cd t e) (fun _ s => StIs d .running s) from + fun x hx => H σ rfl x hx + unfold sendIpc + intro s hs x hx + subst hs + obtain ⟨y, hy, hx'⟩ := mem_bindA.1 hx + clear hx + rw [getEndpoint_eq hk] at hy + have ey : y = (.recvEP (d :: q), s) := hy + subst ey + revert x hx' + cases blk <;> dsimp only + all_goals + intro x hx + refine (?_ : HT (fun _ => True) _ (fun _ s => StIs d .running s)) s trivial x hx + refine skip fun _ => skip fun rs => ?_ + cases rs + case blockedOnReceive a r dd => + dsimp only + refine skip fun _ => skip fun _ => skip fun _ => skip fun _ => skip fun fault => ?_ + split_ifs + · exact skip fun _ => tail + · exact skip fun _ => tail + · exact skip fun _ => tail + all_goals + dsimp only + intro s _ r hr + obtain ⟨z, hz, -⟩ := mem_bindA.1 hr + exact absurd hz (Set.notMem_empty _) + +/-! ## Transports -/ + +theorem SIKA.tcbB {σ σ' : State Unit} (R : SIKA i M N don σ σ') {p : ObjRef} {y : Tcb} (h : σ'.kheap p = some (.tcb y)) : + ∃ x, σ.kheap p = some (.tcb x) ∧ TcbIpcA i M don p x y := by + have o := R.obj p + rw [h] at o + generalize ea : σ.kheap p = a at o + cases o with + | same => exact ⟨y, rfl, rfl, rfl, rfl, rfl, rfl, rfl, Or.inl rfl, Or.inl rfl, rfl, rfl⟩ + | tcb x _ g => exact ⟨x, rfl, g⟩ + +theorem SIKA.scF {σ σ' : State Unit} (R : SIKA i M N don σ σ') {p : ObjRef} {x : SchedContext} {k : Nat} + (h : σ.kheap p = some (.schedContext x k)) : + ∃ y, σ'.kheap p = some (.schedContext y k) ∧ ScIpcA i don p x y := by + have o := R.obj p + rw [h] at o + generalize eb : σ'.kheap p = b at o + cases o with + | same => exact ⟨x, rfl, rfl, rfl, id, fun _ => ⟨rfl, rfl⟩, Or.inl rfl, fun _ => rfl⟩ + | sc _ y _ g => exact ⟨y, rfl, g⟩ + +theorem SIKA.scB {σ σ' : State Unit} (R : SIKA i M N don σ σ') {p : ObjRef} {y : SchedContext} {k : Nat} + (h : σ'.kheap p = some (.schedContext y k)) : + ∃ x, σ.kheap p = some (.schedContext x k) ∧ ScIpcA i don p x y := by + have o := R.obj p + rw [h] at o + generalize ea : σ.kheap p = a at o + cases o with + | same => exact ⟨y, rfl, rfl, rfl, id, fun _ => ⟨rfl, rfl⟩, Or.inl rfl, fun _ => rfl⟩ + | sc x _ _ g => exact ⟨x, rfl, g⟩ + +theorem SIKA.ipcKeep {σ σ' : State Unit} (R : SIKA i M N don σ σ') : IpcKeepA σ σ' := + ⟨fun _ _ h => by + obtain ⟨y, e, g⟩ := R.tcbF h + exact ⟨y, e, g.fault, g.frame, g.buf, g.mcp, g.yt, g.flags⟩, + fun _ _ _ h => by + obtain ⟨y, e, g⟩ := R.scF h + exact ⟨y, _, e, g.yf⟩, + fun _ _ _ h => by + obtain ⟨x, e, g⟩ := R.scB h + exact ⟨x, _, e, g.yf⟩⟩ + +theorem SIKA.ntfnF {σ σ' : State Unit} (R : SIKA i M N don σ σ') {p : ObjRef} {x : Notification} + (h : σ.kheap p = some (.notification x)) : σ'.kheap p = some (.notification x) := by + have o := R.obj p + rw [h] at o + generalize eb : σ'.kheap p = b at o + cases o with + | same => rfl + +theorem SIKA.epF {σ σ' : State Unit} (R : SIKA i M N don σ σ') {p : ObjRef} {x : Endpoint} + (h : σ.kheap p = some (.endpoint x)) : + ∃ y, σ'.kheap p = some (.endpoint y) ∧ ∀ u ∈ x.epQueue, ¬ M u → u ∈ y.epQueue := by + have o := R.obj p + rw [h] at o + generalize eb : σ'.kheap p = b at o + cases o with + | same => exact ⟨x, rfl, fun _ h _ => h⟩ + | ep _ y g => exact ⟨y, rfl, g⟩ + +theorem SIKA.replyB {σ σ' : State Unit} (R : SIKA i M N false σ σ') {p : ObjRef} {y : Reply} + (h : σ'.kheap p = some (.reply y)) : ∃ x, σ.kheap p = some (.reply x) ∧ y.replySc = x.replySc := by + have o := R.obj p + rw [h] at o + generalize ea : σ.kheap p = a at o + cases o with + | same => exact ⟨y, rfl, rfl⟩ + | reply x _ g => exact ⟨x, rfl, g rfl⟩ + +/-- A cap of an object of the new state is a cap of the old object. -/ +theorem SIKA.capOfA {σ σ' : State Unit} (R : SIKA i M N don σ σ') (p : ObjRef) (b : CnodeIndex) : + (σ'.kheap p).bind (fun o => capOf o b) = (σ.kheap p).bind (fun o => capOf o b) := by + have o := R.obj p + generalize σ.kheap p = a at o ⊢ + generalize σ'.kheap p = c at o ⊢ + cases o with + | same => rfl + | tcb x y g => simp [capOf, g.caps] + | sc => rfl + | ep => rfl + | reply => rfl + +theorem SIKA.specCap {σ σ' : State Unit} (R : SIKA i M N don σ σ') (sl : CslotPtr) : + specCapAtA σ' sl = specCapAtA σ sl := by + have o := R.obj sl.1 + unfold specCapAtA + generalize σ.kheap sl.1 = a at o ⊢ + generalize σ'.kheap sl.1 = c at o ⊢ + cases o with + | same => rfl + | tcb x y g => simp only [g.caps] + | sc => rfl + | ep => rfl + | reply => rfl + +theorem SIKA.irqA {σ σ' : State Unit} (R : SIKA i M N don σ σ') (h : SpecIrqA σ) : SpecIrqA σ' := by + have hnode : σ'.interruptIrqNode = σ.interruptIrqNode := by rw [R.frame] + have hct : ∀ k p, capTableAt k p σ' ↔ capTableAt k p σ := fun k p => by + have o := R.obj p + unfold capTableAt + generalize σ.kheap p = a at o ⊢ + generalize σ'.kheap p = c at o ⊢ + cases o <;> simp + have capB : ∀ p o' b c, σ'.kheap p = some o' → capOf o' b = some c → + ∃ o, σ.kheap p = some o ∧ capOf o b = some c := fun p o' b c hp hc => by + have e := R.capOfA p b + rw [hp] at e + cases hk : σ.kheap p with + | none => rw [hk] at e; simp [hc] at e + | some o => rw [hk] at e; exact ⟨o, rfl, e.symm.trans hc⟩ + obtain ⟨⟨⟨hinj, hcn⟩, hdom⟩, hsl, href⟩ := h + refine ⟨⟨⟨?_, fun irq hi => ?_⟩, fun irq hi k hk => ?_⟩, fun irq hi o b c ho hc => ?_, fun p o b c ho hc => ?_⟩ + · rw [hnode]; exact hinj + · rw [hnode]; exact (hct _ _).2 (hcn irq hi) + · rw [hnode] at hk; exact hdom irq hi k ((hct _ _).1 hk) + · rw [hnode] at ho + obtain ⟨o0, h0, hc0⟩ := capB _ _ _ _ ho hc + exact hsl irq hi o0 b c h0 hc0 + · rw [hnode] + obtain ⟨o0, h0, hc0⟩ := capB _ _ _ _ ho hc + exact href p o0 b c h0 hc0 + +/-- **`SInvXA`** across an `SIKA` step: the movers are no idle thread, the new queue members are neither current nor +idle and end runnable, no queued thread moves, and no switch is pending. -/ +theorem SIKA.sinv {σ σ' : State Unit} {m : McState} {n : Nat} (R : SIKA σ.idleThread M N don σ σ') + (h : SInvXA σ m n) (hMi : ∀ c < n, ¬ M (m.idleThreadOf c)) (hMI : ¬ M σ.idleThread) + (hMq : ∀ y, M y → ∀ d p, y ∉ σ.readyQueues d p) + (hN : ∀ y, N y → y ≠ σ.curThread ∧ y ≠ σ.idleThread ∧ (∀ c < n, m.idleThreadOf c ≠ y) ∧ + ∀ z, σ'.kheap y = some (.tcb z) → runnable z.tcbState = true) + (hN' : ∀ y, N y → ∃ z, σ.kheap y = some (.tcb z)) + (hsw : ∀ u, σ.schedulerAction ≠ .switchThread u) + (hMrls : ∀ y, M y → y ∉ σ.releaseQueue) + (hRdy : RdyA σ) + (hMf : ∀ p, M p → ∀ x y, σ.kheap p = some (.tcb x) → σ'.kheap p = some (.tcb y) → y.tcbFault ≠ none → + FaultStA y.tcbState) : SInvXA σ' m n := by + have e := R.frame + have hcons : σ'.consumedTime = σ.consumedTime := by rw [e] + have hcur : σ'.curThread = σ.curThread := by rw [e] + have hidle : σ'.idleThread = σ.idleThread := by rw [e] + have hcsc : σ'.curSc = σ.curSc := by rw [e] + have newq : ∀ d p y, y ∈ σ'.readyQueues d p → y ∈ σ.readyQueues d p ∨ N y := fun d p y hy => by + rcases R.rq d p y hy with a | a | a + · exact Or.inl a + · exact Or.inr a + · exact absurd a (hsw y) + refine + { mc := h.mc + view := by unfold McView; rw [hidle]; exact h.view + curTcb := by + obtain ⟨x, hx⟩ := h.curTcb + obtain ⟨y, hy, -⟩ := R.tcbF hx + exact ⟨y, by rw [hcur]; exact hy⟩ + idleTcb := by + obtain ⟨x, hx, hs⟩ := h.idleTcb + obtain ⟨y, hy, g⟩ := R.tcbF hx + exact ⟨y, by rw [hidle]; exact hy, (g.st.resolve_right fun a => hMI a.1).trans hs⟩ + onlyIdle := fun t y hy hs => by + obtain ⟨x, hx, g⟩ := R.tcbB hy + rcases g.st with a | a + · exact h.onlyIdle t x hx (a.symm.trans hs) + · exact absurd hs a.2 + idleSc := by + obtain ⟨x, y, k, h1, h2, h3, h4⟩ := h.idleSc + obtain ⟨x', h1', gx⟩ := R.tcbF h1 + obtain ⟨y', h3', gy⟩ := R.scF h3 + refine ⟨x', y', k, by rw [hidle]; exact h1', ?_, h3', by rw [hidle]; exact (gy.idle rfl).1.trans h4⟩ + exact (gx.sc.resolve_right fun a => a.2.1 rfl).trans h2 + curSc := by + obtain ⟨y, k, hy⟩ := h.curSc + obtain ⟨y', h', -⟩ := R.scF hy + exact ⟨y', k, by rw [hcsc]; exact h'⟩ + ctQ := fun ha d p hm => by + rw [hcur] at hm + have ha0 : σ.schedulerAction = .resumeCurThread := by + rcases R.act with e | e | ⟨y, -, e⟩ + · rw [← e]; exact ha + · rw [e] at ha; cases ha + · rw [e] at ha; cases ha + rcases newq d p _ hm with a | a + · exact h.ctQ ha0 d p a + · exact (hN _ a).1 rfl + idleQ := fun d p c hc hm => by + rcases newq d p _ hm with a | a + · exact h.idleQ d p c hc a + · exact (hN _ a).2.2.1 c hc rfl + rq := fun d p t ht => by + rcases newq d p t ht with a | a + · obtain ⟨x, hx, hr⟩ := h.rq d p t a + obtain ⟨y, hy, g⟩ := R.tcbF hx + exact ⟨y, hy, by rw [g.st.resolve_right fun b => hMq t b.1 d p a]; exact hr⟩ + · obtain ⟨x, hx⟩ := hN' t a + obtain ⟨y, hy, -⟩ := R.tcbF hx + exact ⟨y, hy, (hN t a).2.2.2 y hy⟩ + rls := fun t ht => by + obtain ⟨x, hx⟩ := h.rls t (R.rls t ht) + obtain ⟨y, hy, -⟩ := R.tcbF hx + exact ⟨y, hy⟩ + rpos := fun p y k hy a => by + obtain ⟨x, hx, g⟩ := R.scB hy + exact g.ne (h.rpos p x k hx (by unfold scActive at a ⊢; rw [← g.rm]; exact a)) + irqN := R.irqA h.irqN + zc := fun t x' hx e' hr => by + obtain ⟨x, hx0, g⟩ := R.tcbB hx + have hr0 := R.rls t hr + have es : x.tcbSchedContext = x'.tcbSchedContext := + g.sc.elim (fun a => a.symm) (fun d => d.2.2.elim (fun hm => absurd hr0 (hMrls t hm)) + (fun hn => absurd (hn.symm.trans e') (Option.some_ne_none _).symm)) + rw [hcons] + exact h.zc t x hx0 (by rw [es, e', hcsc]) hr0 + rlsRun := fun t ht => by + have hr0 := R.rls t ht + obtain ⟨x, hx, hr⟩ := h.rlsRun t hr0 + obtain ⟨y, hy, g⟩ := R.tcbF hx + exact ⟨y, hy, by rw [g.st.resolve_right fun b => hMrls t b.1 hr0]; exact hr⟩ + curScReady := (R.rdy hRdy).2 + faultSt := fun p y hy hf => by + obtain ⟨x, hx, g⟩ := R.tcbB hy + by_cases hM : M p + · exact hMf p hM x y hx hy hf + · have h0 := h.faultSt p x hx (fun e => hf (g.fault.trans e)) + rw [g.st.resolve_right fun b => hM b.1] + exact h0 } + +/-- (item 11) `SIKA.sinv`'s `hMf` when no mover starts in a `FaultStA` state: `SIKA` keeps faults, and +`FaultStOkA` gives such a mover none. -/ +theorem SIKA.mfA {σ σ' : State Unit} {m : McState} {n : Nat} {i : ObjRef} (R : SIKA i M N don σ σ') + (h : SInvXA σ m n) (hM : ∀ p, M p → ∀ x, σ.kheap p = some (.tcb x) → ¬ FaultStA x.tcbState) : + ∀ p, M p → ∀ x y, σ.kheap p = some (.tcb x) → σ'.kheap p = some (.tcb y) → y.tcbFault ≠ none → + FaultStA y.tcbState := fun p hp x y hx hy hf => by + obtain ⟨x0, hx0, g⟩ := R.tcbB hy + rw [hx] at hx0; cases hx0 + exact absurd (g.fault.trans (FaultStOkA.none h.faultSt hx (hM p hp x hx))) hf + +/-- **`SIdleA`** across an `SIKA` step. -/ +theorem SIKA.sidle {σ σ' : State Unit} {m : McState} {n : Nat} (R : SIKA σ.idleThread M N don σ σ') + (h : SIdleA σ m n) (hMi : ∀ c < n, ¬ M (m.idleThreadOf c)) (hRdy : RdyA σ) : SIdleA σ' m n := by + have hidle : σ'.idleThread = σ.idleThread := by rw [R.frame] + have htime : σ'.curTime = σ.curTime := by rw [R.frame] + have vt : ∀ c < n, vtCapA σ' (m.idleThreadOf c) = some .nullCap ∧ vcpuOfA σ' (m.idleThreadOf c) = none := by + intro c hc + obtain ⟨x, hx, -⟩ := h.f3 c hc + obtain ⟨y, hy, g⟩ := R.tcbF hx + have one : ∀ z : Tcb, tcbCnodeMap z (tcbCnodeIndex 1) = some z.tcbVtable := fun z => by + simp [tcbCnodeMap, tcbIdx_eq, tcbStage2Slot, tcbHighVSpaceSlot] + have ev : y.tcbVtable = x.tcbVtable := + Option.some.inj ((one y).symm.trans ((congrFun g.caps (tcbCnodeIndex 1)).trans (one x))) + exact idleVtKeepA hx hy ev g.vcpu (h.idleSlot c hc) (h.idleBound c hc) + refine ⟨fun c hc => ?_, fun y k hy => ?_, fun p y k hy ht => ?_, h.f6.of_eq R.specCap, fun y k hy => ?_, + fun c hc => (vt c hc).1, fun c hc => (vt c hc).2⟩ + rotate_left 3 + · obtain ⟨x, hx, g⟩ := R.scB hy + refine ⟨?_, (R.rdy hRdy).1 y k hy⟩ + have a := (h.f7 x k hx).1 + unfold scActive at a ⊢; rw [g.rm]; exact a + · obtain ⟨x, hx, hs⟩ := h.f3 c hc + obtain ⟨y, hy, g⟩ := R.tcbF hx + exact ⟨y, hy, (g.st.resolve_right fun a => hMi c hc a.1).trans hs⟩ + · obtain ⟨x, hx, g⟩ := R.scB hy + rw [(g.idle rfl).2]; exact h.f4 x k hx + · rw [hidle] at ht + obtain ⟨x, hx, g⟩ := R.scB hy + rcases g.th with a | a + · exact h.f5 p x k hx (a.symm.trans ht) + · exact absurd ht a.2 + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3IpcSchedValidA.lean b/proofs/Fl4ProofsA64/P3/Stage3IpcSchedValidA.lean new file mode 100644 index 000000000..c5b3721d7 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3IpcSchedValidA.lean @@ -0,0 +1,488 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2b-2): **`SchedValidA` across `send_ipc` to a waiting receiver.** + +* **`sendIpc_recv_schedValidA`**: a non-call send without donation; a call without grant or without a reply object; + a call with a reply push that donates the sender's SC to a receiver without one. + +Over G1's `replyUnlinkTcbD`, `setThreadStateD`, `donateR_fia` (`P3/Stage3SigSchedValidA`, `P3/Stage3DelReplyRemoveA`). +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3IpcKeepSA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P3 Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Classical + +noncomputable section + +/-- A TCB replaced by one with the same SC, that SC active (a thread may become blocked). -/ +theorem FIA.ofTcbBlk {σ : State Unit} (h : FIA σ) {p : ObjRef} {x x' : Tcb} (hx : σ.kheap p = some (.tcb x)) + (h1 : x'.tcbSchedContext = x.tcbSchedContext) (ha : ∀ sc, x.tcbSchedContext = some sc → ScActiveAtA σ sc) : + FIA { σ with kheap := Function.update σ.kheap p (some (.tcb x')) } := by + have scK : ∀ q y k, σ.kheap q = some (.schedContext y k) → + Function.update σ.kheap p (some (.tcb x')) q = some (.schedContext y k) := fun q y k e => by + by_cases hq : q = p + · subst hq; rw [hx] at e; cases e + · simpa [Function.update_of_ne hq] using e + have act : ∀ q, ScActiveAtA σ q → ScActiveAtA { σ with kheap := Function.update σ.kheap p (some (.tcb x')) } q := + fun q ⟨y, k, e, a⟩ => ⟨y, k, scK q y k e, a⟩ + have actT : ∀ t, ActTcbA σ t → ActTcbA { σ with kheap := Function.update σ.kheap p (some (.tcb x')) } t := + fun t ⟨z, sc, e, es, a⟩ => by + by_cases hq : t = p + · subst hq + rw [hx] at e + have hz : x = z := KernelObject.tcb.inj (Option.some.inj e) + subst hz + exact ⟨x', sc, by simp, h1.trans es, act sc a⟩ + · exact ⟨z, sc, by simpa [Function.update_of_ne hq] using e, es, act sc a⟩ + refine ⟨⟨fun d p' t hm => ?_, fun e1 e2 => ?_, fun t z sc e1 e2 e3 => ?_⟩, fun t ht => actT t (h.2 t ht)⟩ + · obtain ⟨a, n⟩ := h.1.rq d p' t hm + exact ⟨actT t a, n⟩ + · obtain ⟨a, n⟩ := h.1.act e1 e2 + exact ⟨actT _ a, n⟩ + · by_cases hq : t = p + · subst hq + have hz : x' = z := by simpa using e1 + subst hz + exact act sc (ha sc (h1.symm.trans e3)) + · exact act sc (h.1.blk t z sc (by simpa [Function.update_of_ne hq] using e1) e2 e3) + +/-- `set_thread_state t ts` of a thread whose SC is active. -/ +theorem setThreadStateB (t : ObjRef) (ts : ThreadState) (R : State Unit → Prop) + (hT : ∀ σ x, σ.kheap t = some (.tcb x) → R σ → + R { σ with kheap := Function.update σ.kheap t (some (.tcb { x with tcbState := ts })) }) + (hA : ∀ σ a, R σ → R { σ with schedulerAction := a }) : + HT (fun σ => FIA σ ∧ RqDomPrio σ ∧ ActTcbA σ t ∧ R σ) (setThreadState (Z := Unit) t ts) + (fun _ σ => FIA σ ∧ RqDomPrio σ ∧ R σ) := by + unfold Fl4SpecA64.setThreadState Fl4SpecA64.setThreadStateAct + refine HT.bind (Q := fun _ σ => FIA σ ∧ RqDomPrio σ ∧ R σ) ?_ fun _ => ?_ + · intro σ hσ r hr + obtain ⟨x, hx, hr2⟩ := threadSet_spec hr + rw [hr2] + obtain ⟨hI, hD, ⟨z, sc, e, es, a⟩, hR⟩ := hσ + have ez : x = z := KernelObject.tcb.inj (Option.some.inj (hx.symm.trans e)) + subst ez + refine ⟨hI.ofTcbBlk hx (x' := { x with tcbState := ts }) rfl (fun sc' h => ?_), + hD.ofTcbRepl hx rfl rfl, hT σ x hx hR⟩ + rw [es] at h; cases h; exact a + · refine HT.bind (Q := fun _ σ => FIA σ ∧ RqDomPrio σ ∧ R σ) (HT.gets _ fun σ hσ => hσ) fun cur => ?_ + refine HT.bind (Q := fun _ σ => FIA σ ∧ RqDomPrio σ ∧ R σ) (HT.gets _ fun σ hσ => hσ) fun act => ?_ + refine HT.bind (Q := fun _ σ => FIA σ ∧ RqDomPrio σ ∧ R σ) (HT.gets _ fun σ hσ => hσ) fun sched => ?_ + exact HT.when (fun _ => HT.mono (HT.setAct .chooseNewThread (by intro e; cases e) + (fun σ => RqDomPrio σ ∧ R σ) (fun σ a h => ⟨h.1, hA σ a h.2⟩)) + (fun σ h => ⟨h.1, h.2.1, h.2.2⟩) (fun _ _ h => ⟨h.1, h.2.1, h.2.2⟩)) (fun _ σ hσ => hσ) + +/-- Every outcome of `m` from `s` satisfies `Q`, from a triple at a precondition holding at `s`. -/ +theorem HT.atA {α : Type} {m : SMonad Unit α} {s : State Unit} {P Q : State Unit → Prop} + (h : HT P m (fun _ => Q)) (hP : P s) : ∀ x ∈ (m s).1, Q x.2 := fun x hx => h s hP x hx + +/-- The thread's TCB (if any) has no fault. -/ +def NoFaultA (t : ObjRef) (σ : State Unit) : Prop := ∀ x, σ.kheap t = some (.tcb x) → x.tcbFault = none + +theorem NoFaultA.sik {t i : ObjRef} {M N : ObjRef → Prop} {don : Bool} {σ σ' : State Unit} (h : NoFaultA t σ) + (R : SIKA i M N don σ σ') : NoFaultA t σ' := fun y hy => by + obtain ⟨x, hx, g⟩ := R.tcbB hy + rw [g.fault]; exact h x hx + +/-- The tail of `send_ipc` after the receiver's wait ends. -/ +theorem sendTail_fia (d : ObjRef) : + HT (fun σ => FIA σ ∧ BAs [d] σ) + (do setThreadState (Z := Unit) d .running + let destSc ← getTcbObjRef Tcb.tcbSchedContext d + ifSporadicCurScTestRefillUnblockCheck destSc + possibleSwitchTo d) + (fun _ σ => FIA σ) := by + refine HT.bind (Q := fun _ σ => FIA σ ∧ BAs [d] σ) + (setThreadState_fia d .running rfl (BAs [d]) (fun σ p x x' hx h1 h => h.tcbUpd hx h1) (fun σ a h => h.act a)) + fun _ => ?_ + refine HT.bind (Q := fun _ σ => FIA σ ∧ BAs [d] σ) + (HT.ofTPres (by unfold getTcbObjRef; tpres) (BAs [d]) fun _ _ k h => h.ofTKeep k) fun sc => ?_ + refine HT.bind (Q := fun _ σ => FIA σ ∧ BAs [d] σ) (refillCheckT_fia sc (BAs [d]) fun _ _ k h => h.ofTKeep k) + fun _ => ?_ + exact HT.mono (possibleSwitchTo_fia' d (fun _ => True) (fun _ _ h => h) (fun _ _ h => h)) + (fun _ h => ⟨h.1, h.2 d (List.mem_singleton_self d), trivial⟩) (fun _ _ h => h.1) + +/-- The SC at `p` stays active along the donation's frame. -/ +theorem scActive_frameA (c : ObjRef) (ts : ThreadState) (p : ObjRef) : + TFrame (fun σ => StIs c ts σ ∧ ScActiveAtA σ p) := + ⟨fun σ σ' k h => ⟨(StIs.frame c ts).keep σ σ' k h.1, by + obtain ⟨y, n, e, a⟩ := h.2 + obtain ⟨y', e', a'⟩ := k.2.2.2.2.2.2.2.1 p y n e + exact ⟨y', n, e', a'.trans a⟩⟩, + fun σ σ' hk hi h => ⟨(StIs.frame c ts).kh σ σ' hk hi h.1, by + obtain ⟨y, n, e, a⟩ := h.2 + exact ⟨y, n, by rw [hk]; exact e, a⟩⟩, + fun σ q x x' hx hst h => ⟨(StIs.frame c ts).repl σ q x x' hx hst h.1, by + obtain ⟨y, n, e, a⟩ := h.2 + have ne : p ≠ q := fun h => by rw [h, hx] at e; cases e + exact ⟨y, n, by simpa [Function.update_of_ne ne] using e, a⟩⟩⟩ + +/-- `sched_context_donate` leaves the SC bound to its new thread. -/ +theorem donate_bindsA (sc d : ObjRef) : + HT (fun _ => True) (schedContextDonate (Z := Unit) sc d) + (fun _ σ => ∀ y, σ.kheap d = some (.tcb y) → y.tcbSchedContext = some sc) := by + unfold schedContextDonate + refine HT.bind (Q := fun _ _ => True) (fun _ _ _ _ => trivial) fun _ => ?_ + refine HT.bind (Q := fun _ _ => True) (fun _ _ _ _ => trivial) fun _ => ?_ + refine HT.bind (Q := fun _ _ => True) (fun _ _ _ _ => trivial) fun _ => ?_ + intro σ _ r hr y hy + obtain ⟨x, -, e⟩ := threadSet_memA _ d hr + rw [e] at hy + have : tcbSchedContextUpdate (fun _ => some sc) x = y := by simpa using hy + rw [← this]; rfl + +/-- `reply_unlink_tcb` leaves the thread `Inactive`. -/ +theorem replyUnlinkTcb_stA (t r : ObjRef) : + HT (fun _ => True) (replyUnlinkTcb (Z := Unit) t r) (fun _ σ => StIs t .inactive σ) := by + unfold replyUnlinkTcb + refine HT.bind (Q := fun _ _ => True) (fun _ _ _ _ => trivial) fun _ => ?_ + refine HT.bind (Q := fun _ _ => True) (fun _ _ _ _ => trivial) fun _ => ?_ + refine HT.bind (Q := fun _ _ => True) (fun _ _ _ _ => trivial) fun _ => ?_ + refine HT.bind (Q := fun _ _ => True) (fun _ _ _ _ => trivial) fun _ => ?_ + refine HT.bind (Q := fun _ _ => True) (fun _ _ _ _ => trivial) fun _ => ?_ + unfold Fl4SpecA64.setThreadState + refine HT.bind (Q := fun _ σ => StIs t .inactive σ) (fun σ _ r hr y hy => ?_) fun _ σ h r hr => ?_ + · obtain ⟨x, -, e⟩ := threadSet_memA _ t hr + rw [e] at hy + have : ({ x with tcbState := .inactive } : Tcb) = y := by simpa using hy + rw [← this] + · rw [setThreadStateAct_eq] at hr + have e : r = ((), stsAct σ t) := hr + rw [e] + exact fun y hy => h y (by rwa [stsAct_kheap] at hy) + +/-- **The donating reply push** of a blocked-on-reply caller to a receiver without SC keeps `FIA` and leaves the +receiver's bound SC active. -/ +theorem replyPushDon_fia {t d r sc : ObjRef} (htd : d ≠ t) : + HT (fun σ => FIA σ ∧ RqDomPrio σ ∧ ActTcbA σ t ∧ ScIs t (some sc) σ ∧ ScIs d none σ ∧ StIs d .inactive σ) + (replyPush (Z := Unit) t d r true) (fun _ σ => FIA σ ∧ BAs [d] σ) := by + unfold replyPush + let P : State Unit → Prop := fun σ => + FIA σ ∧ RqDomPrio σ ∧ ActTcbA σ t ∧ ScIs t (some sc) σ ∧ ScIs d none σ ∧ StIs d .inactive σ + refine HT.bind (Q := fun a σ => P σ ∧ a = some sc) (fun σ h z hz => ?_) fun a => ?_ + · unfold getTcbObjRef at hz + obtain ⟨x, sc', e, es, -⟩ := h.2.2.1 + have ez := threadGet_valA Tcb.tcbSchedContext e hz + rw [ez] + exact ⟨h, h.2.2.2.1 x e⟩ + refine HT.bind (Q := fun b σ => (P σ ∧ a = some sc) ∧ b = none) (fun σ h z hz => ?_) fun b => ?_ + · unfold getTcbObjRef Fl4SpecA64.threadGet at hz + obtain ⟨y, hy, hz⟩ := mem_bindA.1 hz + have hy' : y ∈ (NonDet.assertOpt (getTcb (Z := Unit) d σ) σ).1 := hy + cases hg : getTcb (Z := Unit) d σ with + | none => rw [hg] at hy'; exact absurd hy' (Set.notMem_empty _) + | some x => + rw [hg] at hy' + have e1 : y = (x, σ) := hy' + subst e1 + have e2 : z = (x.tcbSchedContext, σ) := hz + rw [e2] + exact ⟨h, h.1.2.2.2.2.1 x (getTcb_kheapA hg)⟩ + have keep : ∀ σ σ', TKeepA σ σ' → CellsEq σ σ' → P σ → P σ' := fun σ σ' k c h => by + obtain ⟨h1, h2, ⟨x, sc', e, es, y, n, ey, ea⟩, h4, h5, h6⟩ := h + obtain ⟨y', ey', ea'⟩ := k.2.2.2.2.2.2.2.1 sc' y n ey + exact ⟨h1.ofTKeep k, h2.ofCells c, ⟨x, sc', (c.2 _ _).1 e, es, y', n, ey', ea'.trans ea⟩, h4.ofTKeep k, + h5.ofTKeep k, (StIs.frame d .inactive).keep σ σ' k h6⟩ + have T : ∀ {β : Type} {m : SMonad Unit β}, TPres m → CPres m → + HT (fun σ => (P σ ∧ a = some sc) ∧ b = none) m (fun _ σ => (P σ ∧ a = some sc) ∧ b = none) := + fun h1 h2 σ h z hz => ⟨⟨keep σ _ (h1 σ z hz) (h2 σ z hz) h.1.1, h.1.2⟩, h.2⟩ + refine HT.bind (T (by tpres) (by cpres)) fun _ => ?_ + refine HT.bind (Q := fun _ σ => (FIA σ ∧ RqDomPrio σ ∧ + (ScIs d none σ ∧ StIs d .inactive σ ∧ ScActiveAtA σ sc)) ∧ a = some sc ∧ b = none) (fun σ h z hz => ?_) + fun _ => ?_ + · obtain ⟨⟨⟨h1, h2, h3, h4, h5, h6⟩, ha⟩, hb⟩ := h + have act : ScActiveAtA σ sc := by + obtain ⟨x, sc', e, es, a⟩ := h3 + rw [h4 x e] at es; cases es; exact a + have := setThreadStateB t (.blockedOnReply r) (fun σ => ScIs d none σ ∧ StIs d .inactive σ ∧ ScActiveAtA σ sc) + (fun σ x hx h => by + refine ⟨fun y hy => h.1 y (by simpa [Function.update_of_ne htd] using hy), + fun y hy => h.2.1 y (by simpa [Function.update_of_ne htd] using hy), ?_⟩ + obtain ⟨y, n, e, a⟩ := h.2.2 + have ne : sc ≠ t := fun h => by rw [h, hx] at e; cases e + exact ⟨y, n, by simpa [Function.update_of_ne ne] using e, a⟩) + (fun _ _ h => h) σ ⟨h1, h2, h3, h5, h6, act⟩ z hz + exact ⟨this, ha, hb⟩ + intro σ ⟨h, ha, hb⟩ z hz + subst ha hb + unfold NonDet.when at hz + rw [if_pos (by simp)] at hz + revert z hz + refine HT.atA (P := fun s => FIA s ∧ RqDomPrio s ∧ (ScIs d none s ∧ StIs d .inactive s ∧ ScActiveAtA s sc)) + (Q := fun s => FIA s ∧ BAs [d] s) ?_ h + refine HT.bind (Q := fun _ s => FIA s ∧ RqDomPrio s ∧ (ScIs d none s ∧ StIs d .inactive s ∧ ScActiveAtA s sc)) + (HT.ofTC' (by unfold bindScReply; tpres) (by unfold bindScReply; cpres) _ fun σ σ' k h => + ⟨h.1.ofTKeep k, (scActive_frameA d .inactive sc).keep σ σ' k h.2⟩) fun _ => ?_ + intro s h z hz + have a1 := donateR_fia sc d _ (scActive_frameA d .inactive sc) s + ⟨h.1, h.2.2.1, Or.inl h.2.1, h.2.2.2⟩ z hz + have a2 := donate_bindsA sc d s trivial z hz + refine ⟨FIX.ofSt a1.1 a1.2.1 rfl, fun u hu x sc' e es => ?_⟩ + obtain rfl := List.mem_singleton.1 hu + rw [a2 x e] at es; cases es + exact ⟨x, sc, e, a2 x e, a1.2.2⟩ + +/-- **The reply push of a blocked-on-reply caller to a receiver that has its own SC** (`canDonate` set, nothing +donated) keeps `FIA` and the receiver's bound SC active. -/ +theorem replyPushCalleeSc_fia {t d r c : ObjRef} (htd : d ≠ t) : + HT (fun σ => FIA σ ∧ RqDomPrio σ ∧ ActTcbA σ t ∧ ScIs d (some c) σ ∧ BAs [d] σ) + (replyPush (Z := Unit) t d r true) (fun _ σ => FIA σ ∧ BAs [d] σ) := by + unfold replyPush + let P : State Unit → Prop := fun σ => FIA σ ∧ RqDomPrio σ ∧ ActTcbA σ t ∧ ScIs d (some c) σ ∧ BAs [d] σ + refine HT.bind (Q := fun _ σ => P σ) (fun σ h z hz => ?_) fun a => ?_ + · unfold getTcbObjRef at hz + obtain ⟨x, sc', e, es, -⟩ := h.2.2.1 + have ez := threadGet_valA Tcb.tcbSchedContext e hz + rw [ez] + exact h + refine HT.bind (Q := fun b σ => P σ ∧ b = some c) (fun σ h z hz => ?_) fun b => ?_ + · unfold getTcbObjRef Fl4SpecA64.threadGet at hz + obtain ⟨y, hy, hz⟩ := mem_bindA.1 hz + have hy' : y ∈ (NonDet.assertOpt (getTcb (Z := Unit) d σ) σ).1 := hy + cases hg : getTcb (Z := Unit) d σ with + | none => rw [hg] at hy'; exact absurd hy' (Set.notMem_empty _) + | some x => + rw [hg] at hy' + have e1 : y = (x, σ) := hy' + subst e1 + have e2 : z = (x.tcbSchedContext, σ) := hz + rw [e2] + exact ⟨h, h.2.2.2.1 x (getTcb_kheapA hg)⟩ + have keep : ∀ σ σ', TKeepA σ σ' → CellsEq σ σ' → P σ → P σ' := fun σ σ' k c' h => by + obtain ⟨h1, h2, ⟨x, sc', e, es, y, n, ey, ea⟩, h4, h5⟩ := h + obtain ⟨y', ey', ea'⟩ := k.2.2.2.2.2.2.2.1 sc' y n ey + exact ⟨h1.ofTKeep k, h2.ofCells c', ⟨x, sc', (c'.2 _ _).1 e, es, y', n, ey', ea'.trans ea⟩, h4.ofTKeep k, + h5.ofTKeep k⟩ + have T : ∀ {β : Type} {m : SMonad Unit β}, TPres m → CPres m → + HT (fun σ => P σ ∧ b = some c) m (fun _ σ => P σ ∧ b = some c) := + fun h1 h2 σ h z hz => ⟨keep σ _ (h1 σ z hz) (h2 σ z hz) h.1, h.2⟩ + refine HT.bind (T (by tpres) (by cpres)) fun _ => ?_ + refine HT.bind (Q := fun _ σ => (FIA σ ∧ RqDomPrio σ ∧ (ScIs d (some c) σ ∧ BAs [d] σ)) ∧ b = some c) + (fun σ h z hz => ?_) fun _ => ?_ + · obtain ⟨⟨h1, h2, h3, h4, h5⟩, hb⟩ := h + have := setThreadStateB t (.blockedOnReply r) (fun σ => ScIs d (some c) σ ∧ BAs [d] σ) + (fun σ x hx h => ⟨fun y hy => h.1 y (by simpa [Function.update_of_ne htd] using hy), h.2.tcbUpd hx rfl⟩) + (fun _ _ h => ⟨h.1, h.2.act _⟩) σ ⟨h1, h2, h3, h4, h5⟩ z hz + exact ⟨this, hb⟩ + intro σ ⟨h, hb⟩ z hz + subst hb + unfold NonDet.when at hz + rw [if_neg (by simp)] at hz + have e : z = ((), σ) := hz + subst e + exact ⟨h.1, h.2.2.2⟩ + +section +open fl4_core_a64 Fl4ProofsA64.P1 +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- `do_ipc_transfer` (no fault, no extra caps) keeps the scheduler's data and `RqDomPrio`. -/ +theorem doIpcTransfer_tkA {th : TypedHeapA} {kb : U64} (hPl : PlaceA th kb) (sf : S) {σ : State Unit} + (hR : StateRelA th (L.mem sf) σ) (hV : SlotsValidA th (L.mem sf)) {snd rcv : U64} + (htr : th rcv = some .tcb) {xs xr : Tcb} (hks : σ.kheap (specPtr snd) = some (.tcb xs)) + (hkr : σ.kheap (specPtr rcv) = some (.tcb xr)) (hf : xs.tcbFault = none) + (hal : ∀ rb, ipcBufOfA true xr = some rb → (bufAddr rb 0).toNat % 8 = 0) + (ep : Option ObjRef) (badge : Badge) (grant : Bool) + (hcaps : grant = false ∨ ipcBufOfA false xs = none ∨ + unat (dataToMessageInfo (xs.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0) : + ∀ x ∈ (doIpcTransfer (Z := Unit) (specPtr snd) ep badge grant (specPtr rcv) σ).1, + TKeepA σ x.2 ∧ (RqDomPrio σ → RqDomPrio x.2) := by + let B := BufBytesA (ipcBufOfA true xr) + let R : State Unit → State Unit → Prop := fun a b => UFrA B a b ∧ TKeepA a b ∧ (RqDomPrio a → RqDomPrio b) + haveI : PreO R := ⟨fun s => ⟨PreO.refl s, PreO.refl s, id⟩, + fun h1 h2 => ⟨PreO.trans h1.1 h2.1, PreO.trans h1.2.1 h2.2.1, fun h => h2.2.2 (h1.2.2 h)⟩⟩ + have hM : ∀ {α : Type} (m : NonDet MachineState α), + (∀ s, ∀ y ∈ (doMachineOp (Z := Unit) m s).1, RqDomPrio s → RqDomPrio y.2) := fun m s y hy h => by + have e := doMachineOp_sikA (i := s.idleThread) (M := fun _ => False) (N := fun _ => False) (don := false) m s y hy + have q : y.2.readyQueues = s.readyQueues ∧ y.2.kheap = s.kheap := by + obtain ⟨y1, hy1, hy⟩ := hy + have e1 : y1 = (s.machineState, s) := hy1 + subst e1 + obtain ⟨⟨⟨a, ms'⟩, s2⟩, hy2, hy⟩ := hy + have e2 : s2 = s := hy2.2 + subst e2 + obtain ⟨y3, hy3, hy⟩ := hy + have e3 : y3 = ((), { s2 with machineState := ms' }) := hy3 + subst e3 + have e4 : y = (a, { s2 with machineState := ms' }) := hy + rw [e4]; exact ⟨rfl, rfl⟩ + intro f d p x hx hm + rw [q.2] at hx; rw [q.1] at hm + exact h f d p x hx hm + have key := doIpcTransfer_genA (R := R) (B := B) (fun _ _ h => h.1) + (fun t f s y hy => ⟨UFrA.of_fr (asUser_presA t f s y hy), asUser_tpres t f s y hy, fun h => by + obtain ⟨x, uc, hx, e⟩ := asUser_spec hy + rw [e]; exact h.ofTcbRepl hx rfl rfl⟩) + (fun a s y hy => ⟨UFrA.of_fr (doMachineOp_presA _ (loadWord_keepA a) s y hy), doMachineOp_tpres _ s y hy, + hM _ s y hy⟩) + (rbo := ipcBufOfA true xr) (fun rb e k hk v s y hy => by + refine ⟨by show UFrA (BufBytesA (ipcBufOfA true xr)) _ _; rw [e]; exact storeWordOffs_ufrA rb hk v s y hy, ?_⟩ + have : PresR (fun a b : State Unit => TKeepA a b ∧ (RqDomPrio a → RqDomPrio b)) + (storeWordOffs (Z := Unit) rb k v) := by + haveI : PreO (fun a b : State Unit => TKeepA a b ∧ (RqDomPrio a → RqDomPrio b)) := + ⟨fun s => ⟨PreO.refl s, id⟩, fun h1 h2 => ⟨PreO.trans h1.1 h2.1, fun h => h2.2 (h1.2 h)⟩⟩ + unfold storeWordOffs + exact PresR.bind PresR.get fun _ => PresR.bind (PresR.assert _) fun _ s y hy => + ⟨doMachineOp_tpres _ s y hy, hM _ s y hy⟩ + exact this s y hy) + hPl sf hR hV htr hks hkr rfl hf hal ep badge grant hcaps + exact fun x hx => (key σ rfl x hx).2 + +/-- **`SchedValidA` across `send_ipc` to a waiting receiver** (module docstring): no donation, an inactive sender, or +the donating reply push. -/ +theorem sendIpc_recv_schedValidA {th : TypedHeapA} {kb : U64} (hPl : PlaceA th kb) (sf : S) {σ : State Unit} + (hR : StateRelA th (L.mem sf) σ) (hV : SlotsValidA th (L.mem sf)) {t d : U64} {e : ObjRef} {q : List ObjRef} + (htr : th d = some .tcb) (hk : σ.kheap e = some (.endpoint (.recvEP (specPtr d :: q)))) + {xt xd : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) (hkd : σ.kheap (specPtr d) = some (.tcb xd)) + (hf : xt.tcbFault = none) (hal : ∀ rb, ipcBufOfA true xd = some rb → (bufAddr rb 0).toNat % 8 = 0) + (blk call : Bool) (badge : Badge) (g gr cd : Bool) + (hcaps : g = false ∨ ipcBufOfA false xt = none ∨ + unat (dataToMessageInfo (xt.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0) + {e' : ObjRef} {rep : Option ObjRef} {dd : ReceiverPayload} (hst : xd.tcbState = .blockedOnReceive e' rep dd) + (hbr : (call = false ∧ cd = false) ∨ (call = true ∧ ((g = false ∧ gr = false) ∨ rep = none)) ∨ + (call = true ∧ (g = true ∨ gr = true) ∧ rep ≠ none ∧ cd = true ∧ specPtr t ≠ specPtr d ∧ + xd.tcbSchedContext = none ∧ ActTcbA σ (specPtr t)) ∨ + (call = true ∧ (g = true ∨ gr = true) ∧ rep ≠ none ∧ cd = true ∧ specPtr t ≠ specPtr d ∧ + xd.tcbSchedContext ≠ none ∧ ActTcbA σ (specPtr t))) + (hI : SchedValidA σ) (hD : RqDomPrio σ) : + ∀ x ∈ (sendIpc (Z := Unit) blk call badge g gr cd (specPtr t) e σ).1, SchedValidA x.2 := by + suffices H : HT (fun s => s = σ) (sendIpc (Z := Unit) blk call badge g gr cd (specPtr t) e) (fun _ s => FIA s) from + fun x hx => SchedValidB.toA (H σ rfl x hx).1 (H σ rfl x hx).2 + have hFI : FIA σ := SchedValidA.toFIA hI + have hBA : BAs [specPtr d] σ := fun u hu x sc ex hs => by + obtain rfl := List.mem_singleton.1 hu + have : x = xd := KernelObject.tcb.inj (Option.some.inj (ex.symm.trans hkd)) + subst this + exact ⟨x, sc, ex, hs, hFI.1.blk _ x sc ex (by rw [hst]; rfl) hs⟩ + unfold sendIpc + intro s hs x hx + subst hs + obtain ⟨y, hy, hx'⟩ := mem_bindA.1 hx + clear hx + rw [getEndpoint_eq hk] at hy + have ey : y = (.recvEP (specPtr d :: q), s) := hy + subst ey + revert x hx' + cases blk <;> dsimp only + all_goals + refine HT.atA (P := fun σ' => σ' = s) ?_ rfl + refine HT.bind (Q := fun _ s1 => FIA s1 ∧ RqDomPrio s1 ∧ BAs [specPtr d] s1 ∧ TcbKeepA s s1 ∧ SchedFrameA s s1 ∧ + SIKA s.idleThread (fun _ => True) (fun _ => False) false s s1) (fun σ' h z hz => ?_) fun _ => ?_ + · subst h + have h1 := (HT.ofTC' (by unfold tcbEpDequeue; tpres) (by unfold tcbEpDequeue; cpres) (BAs [specPtr d]) + (fun _ _ k h => h.ofTKeep k) : + HT _ (tcbEpDequeue (Z := Unit) (specPtr d) e) _) σ' ⟨hFI, hD, hBA⟩ z hz + exact ⟨h1.1, h1.2.1, h1.2.2, TKInst.pres σ' z hz, SPresInst.pres σ' z hz, tcbEpDequeue_sikA _ _ trivial σ' z hz⟩ + refine HT.bind (Q := fun rs s1 => (FIA s1 ∧ RqDomPrio s1 ∧ BAs [specPtr d] s1 ∧ TcbKeepA s s1 ∧ + SchedFrameA s s1 ∧ SIKA s.idleThread (fun _ => True) (fun _ => False) false s s1) ∧ + rs = .blockedOnReceive e' rep dd) (fun σ' h z hz => ?_) fun rs => ?_ + · have hz' : z = (xd.tcbState, σ') := by + unfold getThreadState at hz + exact threadGet_valA _ (h.2.2.2.1 _ _ hkd) hz + subst hz' + exact ⟨h, hst⟩ + intro σ' ⟨h, ers⟩ z hz + subst ers + dsimp only at hz + obtain ⟨w, hw, hz1⟩ := mem_bindA.1 hz + clear hz + have ew : w = (rep, σ') := hw + subst ew + obtain ⟨u, hu, hz2⟩ := mem_bindA.1 hz1 + clear hz1 + have hT := doIpcTransfer_tkA hPl sf (hR.specSchedA h.2.2.2.2.1.1) hV htr (h.2.2.2.1 _ _ hkt) + (h.2.2.2.1 _ _ hkd) hf hal _ _ _ hcaps u hu + have hS : SIKA s.idleThread (fun _ => True) (fun _ => False) false σ' u.2 := + doIpcTransfer_sikA hPl sf (hR.specSchedA h.2.2.2.2.1.1) hV htr (h.2.2.2.1 _ _ hkt) + (h.2.2.2.1 _ _ hkd) hf hal _ _ _ hcaps σ' rfl u hu + have hNF : NoFaultA (specPtr t) u.2 := NoFaultA.sik (fun x hx => by + rw [h.2.2.2.1 _ _ hkt] at hx + have : xt = x := KernelObject.tcb.inj (Option.some.inj hx) + rw [← this]; exact hf) hS + revert z hz2 + let K : State Unit → Prop := fun s1 => (FIA s1 ∧ RqDomPrio s1 ∧ BAs [specPtr d] s1) ∧ + SIKA s.idleThread (fun _ => True) (fun _ => False) false s s1 + refine HT.atA (P := fun s1 => K s1 ∧ NoFaultA (specPtr t) s1) ?_ + ⟨⟨⟨h.1.ofTKeep hT.1, hT.2 h.2.1, h.2.2.1.ofTKeep hT.1⟩, h.2.2.2.2.2.trans hS⟩, hNF⟩ + refine HT.bind (Q := fun _ s1 => (K s1 ∧ NoFaultA (specPtr t) s1) ∧ + (rep ≠ none → StIs (specPtr d) .inactive s1)) ?_ fun _ => ?_ + · cases rep with + | none => exact HT.pure () (fun _ h => ⟨h, fun c => absurd rfl c⟩) + | some r => + intro s1 h z hz + have a := replyUnlinkTcbD (specPtr d) r s1 s1 ⟨h.1.1.1, h.1.1.2.1, KI.refl _ s1, h.1.1.2.2⟩ z hz + have b := replyUnlinkTcb_sikA (i := s.idleThread) (M := fun _ => True) (N := fun _ => False) (don := false) + (specPtr d) r trivial s1 z hz + exact ⟨⟨⟨⟨a.1, a.2.1, a.2.2.2⟩, h.1.2.trans b⟩, h.2.sik b⟩, + fun _ => replyUnlinkTcb_stA (specPtr d) r s1 trivial z hz⟩ + refine HT.bind (Q := fun _ s1 => (K s1 ∧ NoFaultA (specPtr t) s1) ∧ + (rep ≠ none → StIs (specPtr d) .inactive s1)) + (fun s1 h z hz => by + have e : z.2 = s1 := ((threadGet_eqPres _ _) s1 z hz).symm + rw [e]; exact h) fun scOpt => ?_ + refine HT.bind (Q := fun fault s1 => (K s1 ∧ (rep ≠ none → StIs (specPtr d) .inactive s1)) ∧ fault = none) + (HT.threadGet _ (specPtr t) fun s1 h x hx => ⟨⟨h.1.1, h.2⟩, h.1.2 x hx⟩) fun fault => ?_ + intro s1 ⟨⟨⟨h1, hk⟩, hin⟩, ef⟩ z hz + subst ef + revert z hz + refine HT.atA (P := fun s2 => s2 = s1) ?_ rfl + refine HT.mono (P := fun s2 => (FIA s2 ∧ RqDomPrio s2 ∧ BAs [specPtr d] s2) ∧ s2 = s1) ?_ + (fun s2 e => ⟨e ▸ h1, e⟩) (fun _ _ h => h) + have inact : HT (fun s2 => (FIA s2 ∧ RqDomPrio s2 ∧ BAs [specPtr d] s2) ∧ s2 = s1) + (setThreadState (Z := Unit) (specPtr t) .inactive) (fun _ s2 => FIA s2 ∧ BAs [specPtr d] s2) := + HT.mono (setThreadStateD (specPtr t) .inactive rfl (BAs [specPtr d]) (fun σ x hx h => h.tcbUpd hx rfl) + (fun σ a h => h.act a)) (fun _ h => h.1) (fun _ _ h => ⟨h.1, h.2.2.1⟩) + split_ifs with c1 c2 + · refine HT.bind (Q := fun _ s2 => FIA s2 ∧ BAs [specPtr d] s2) ?_ fun _ => sendTail_fia _ + rcases hbr with ⟨rfl, -⟩ | ⟨-, hn⟩ | ⟨-, -, hr, rfl, htd, hd0, hact⟩ | ⟨-, -, -, rfl, htd, hdc, hact⟩ + · simp at c1 + · refine absurd c2 (fun c => ?_) + rcases hn with ⟨rfl, rfl⟩ | rfl + · simp at c + · exact c.2 rfl + · obtain ⟨x, sc, ex, es, y, n, ey, ea⟩ := hact + have hx : x = xt := KernelObject.tcb.inj (Option.some.inj (ex.symm.trans hkt)) + subst hx + refine HT.mono (replyPushDon_fia (sc := sc) (Ne.symm htd)) (fun s2 h => ?_) (fun _ _ h => h) + obtain ⟨⟨f, rq, -⟩, e⟩ := h + subst e + obtain ⟨xt', hxt', exs⟩ := hk.tcbScA ex + obtain ⟨xd', hxd', exd⟩ := hk.tcbScA hkd + obtain ⟨y', ey', gy⟩ := hk.scF ey + refine ⟨f, rq, ⟨xt', sc, hxt', exs.trans es, y', n, ey', by unfold scActive at ea ⊢; rw [gy.rm]; exact ea⟩, + fun w hw => ?_, fun w hw => ?_, hin hr⟩ + · rw [hxt'] at hw + obtain rfl : xt' = w := KernelObject.tcb.inj (Option.some.inj hw) + exact exs.trans es + · rw [hxd'] at hw + obtain rfl : xd' = w := KernelObject.tcb.inj (Option.some.inj hw) + exact exd.trans hd0 + · obtain ⟨c, hc⟩ := Option.ne_none_iff_exists'.1 hdc + obtain ⟨x, sc, ex, es, y, n, ey, ea⟩ := hact + have hx : x = xt := KernelObject.tcb.inj (Option.some.inj (ex.symm.trans hkt)) + subst hx + refine HT.mono (replyPushCalleeSc_fia (c := c) (Ne.symm htd)) (fun s2 h => ?_) (fun _ _ h => h) + obtain ⟨⟨f, rq, hba⟩, e⟩ := h + subst e + obtain ⟨xt', hxt', exs⟩ := hk.tcbScA ex + obtain ⟨xd', hxd', exd⟩ := hk.tcbScA hkd + obtain ⟨y', ey', gy⟩ := hk.scF ey + refine ⟨f, rq, ⟨xt', sc, hxt', exs.trans es, y', n, ey', by unfold scActive at ea ⊢; rw [gy.rm]; exact ea⟩, + fun w hw => ?_, hba⟩ + rw [hxd'] at hw + obtain rfl : xd' = w := KernelObject.tcb.inj (Option.some.inj hw) + exact exd.trans hc + · exact HT.bind inact fun _ => sendTail_fia _ + · refine HT.bind (Q := fun _ s2 => FIA s2 ∧ BAs [specPtr d] s2) + (HT.when (fun hg => ?_) (fun _ _ h => ⟨h.1.1, h.1.2.2⟩)) fun _ => sendTail_fia _ + rcases hbr with ⟨-, rfl⟩ | ⟨rfl, -⟩ | ⟨rfl, -⟩ | ⟨rfl, -⟩ + · exact absurd hg.1 (by simp) + · exact absurd (Or.inl rfl) c1 + · exact absurd (Or.inl rfl) c1 + · exact absurd (Or.inl rfl) c1 + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3IpcWakeA.lean b/proofs/Fl4ProofsA64/P3/Stage3IpcWakeA.lean new file mode 100644 index 000000000..48403957b --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3IpcWakeA.lean @@ -0,0 +1,336 @@ +/- +P3 (aarch64, A64-P3 Stage 3, dispatch restore): **what a send to a waiting receiver leaves for the body end.** + +* **`WakeEndA σ σ'`**: the budget frame (`BLA`), `SwA`, and the current thread's state when the action resumes. +* **`JStage3A.scBoundA`**: at the record, the thread of an SC is bound to it (FL4's `TSc`, the forward link). +* **`BodyEndA.ofWakeA`**: `BodyEndA` from `WakeEndA` and the end record: the current SC's thread is bound to it + (`scBoundA`), so `ZeroConsA` keeps it out of the release queue while time is consumed. No release-queue fact and no + donation case split. +* **`sendIpc_recv_endA`**: `send_ipc` to a waiting receiver, faulted senders included, from a state with no pending + switch: `SwA`; a current sender that calls or is faulted leaves the action not resuming. +* **`sendIpc_recv_blFA`**: the budget frame across the same, faulted senders included. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3SendFaultValidA +import Fl4ProofsA64.P3.Stage3RecvSendSwA +import Fl4ProofsA64.P3.Stage3SendBudgetA +import Fl4ProofsA64.P3.Stage3BodyCurA +import Fl4ProofsA64.P3.Stage3RecvSendCscA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Classical + +noncomputable section + +/-! ## The body end -/ + +/-- **What a waking send leaves** (module docstring). -/ +structure WakeEndA (σ σ' : State Unit) : Prop where + bl : BLA σ σ' + sw : SwA σ' + run : σ'.schedulerAction = .resumeCurThread → ∀ x, σ'.kheap σ.curThread = some (.tcb x) → + x.tcbState = .running ∨ x.tcbState = .restart + +/-- The budget frame across a TCB write. -/ +theorem BLA.tcbW {σ : State Unit} {p : ObjRef} {x y : Tcb} (hk : σ.kheap p = some (.tcb x)) : + BLA σ { σ with kheap := Function.update σ.kheap p (some (.tcb y)) } := by + refine ⟨rfl, rfl, rfl, rfl, rfl, rfl, id, fun z n h => ?_⟩ + have ne : σ.curSc ≠ p := fun e => by + have h' : Function.update σ.kheap p (some (.tcb y)) σ.curSc = some (.schedContext z n) := h + rw [e, Function.update_self] at h' + cases h' + have h' : σ.kheap σ.curSc = some (.schedContext z n) := by simpa [Function.update_of_ne ne] using h + exact ⟨z, h', rfl, rfl⟩ + +section +variable {S A : Type} {st : machine.Store S} [L : LawfulStore st] {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s : S} {a : A} {σ σ' : State Unit} {m : McState} + +/-- **`BodyEndA` from `WakeEndA`** (module docstring). -/ +theorem _root_.Fl4ProofsA64.P3.Sched.BodyEndA.ofWakeA {nA : U64} {Q : U64 → U64 → List U64} + {Rl : U64 → List U64} {stack' : U64 → List U64} {s' : S} {a' : A} + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (J2 : JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack' κ n (J5A st) + (J7A st (J7R2A st ar nA)) s' a' σ' m) + (E : WakeEndA σ σ') : BodyEndA σ σ' m := by + have hd : ∀ y' y : SchedContext, y'.scRefills = y.scRefills → refillHd y' = refillHd y := fun y' y e => by + unfold refillHd; rw [e] + have ha : ∀ y' y : SchedContext, y'.scRefillMax = y.scRefillMax → scActive y' = scActive y := fun y' y e => by + unfold scActive; rw [e] + have htime := E.bl.time + have hlast := E.bl.lmt + have hcsc := E.bl.csc + have hcon := E.bl.used + have hcur := E.bl.ct + have hobj := E.bl.sc + refine ⟨⟨⟨?_, fun y' k hk _ _ => ?_, fun h x hx => ?_⟩, E.sw, fun h => ?_, fun y' k hk _ _ hc0 o ho => ?_⟩, hcur⟩ + · have := J.validTime + unfold ValidMachineTimeA at this ⊢ + rw [htime, hlast]; exact this + · rw [hcsc] at hk + obtain ⟨y, hy, e1, e2⟩ := hobj y' k hk + constructor + · intro z j hz + rw [hcsc, hk] at hz + obtain ⟨rfl, -⟩ := KernelObject.schedContext.inj (Option.some.inj hz) + rw [hd _ _ e1, hcon, htime] + exact J.offReady y k hy + · intro hpos z j hz hactive + rw [hcsc, hk] at hz + obtain ⟨rfl, -⟩ := KernelObject.schedContext.inj (Option.some.inj hz) + rw [hd _ _ e1, hcon, htime] + rw [hcon] at hpos + refine J.offOK hpos y k hy fun h => ?_ + rw [← ha _ _ e2]; exact hactive (hcsc ▸ h) + · rw [hcur] at hx + rcases E.run h x hx with h | h <;> rw [h] + · exact Or.inl rfl + · exact Or.inr (Or.inl rfl) + · rw [hcsc] at h ⊢; exact J.curScNotIdle h + · rw [hcsc] at hk + have htZ := J.sr.curT + have h0 := J.cur.2 + obtain ⟨xc, hxc, hxcs, -⟩ := J.curInv.tcb + obtain ⟨z, hz, hzs⟩ := J.sr.sch.tcb _ htZ + rw [J.j.1.sch.curThread] at hxc + rw [hz] at hxc + obtain rfl := KernelObject.tcb.inj (Option.some.inj hxc) + have es : σ.curSc = scRenA m (L.mem s (scA (L.curW s))) := by + rw [hzs, optScA, if_neg h0] at hxcs + exact (Option.some.inj hxcs).symm + have hc : L.mem s (scA (L.curW s)) ∈ Z.C := J.sr.rest.scMem _ htZ h0 + rw [es] at hk + obtain ⟨x, hx, hxs⟩ := J2.scBoundA hc hk ho + exact fun hm => hc0 (J2.sinv.zc _ _ hx (hxs.trans (by rw [hcsc, es])) hm) + +end + +/-! ## The send -/ + +theorem SIKA.noSwA {i : ObjRef} {M : ObjRef → Prop} {don : Bool} {σ σ' : State Unit} + (h : SIKA i M (fun _ => False) don σ σ') (n : NoSwA σ) : NoSwA σ' := fun u e => by + rcases h.act with a | a | ⟨y, hy, -⟩ + · rw [a] at e; exact n u e + · rw [a] at e; cases e + · exact hy + +/-- A thread made not runnable while current: the action is not `resumeCurThread` after. -/ +theorem setThreadState_noResA (t : ObjRef) (ts : ThreadState) (hts : runnable ts = false) : + HT (fun σ => σ.curThread = t) (setThreadState (Z := Unit) t ts) + (fun _ σ => σ.schedulerAction ≠ .resumeCurThread) := by + unfold Fl4SpecA64.setThreadState + refine HT.bind (Q := fun _ σ => σ.curThread = t ∧ StIs t ts σ) (fun σ h r hr => ?_) fun _ σ h r hr => ?_ + · obtain ⟨x, -, e⟩ := threadSet_memA _ t hr + rw [e] + refine ⟨h, fun y hy => ?_⟩ + have : ({ x with tcbState := ts } : Tcb) = y := by simpa using hy + rw [← this] + · rw [setThreadStateAct_eq] at hr + have e : r = ((), stsAct σ t) := hr + rw [e] + unfold stsAct + split_ifs with c + · intro h'; cases h' + · intro hres + refine c ⟨h.1.symm, hres, fun hs => ?_⟩ + obtain ⟨-, -, x, hx, hr'⟩ := schedulable_actA hs + rw [h.2 x hx, hts] at hr' + cases hr' + +/-- A budget-keeping step keeps "the action does not resume". -/ +theorem noRes_kb {α : Type} {m : SMonad Unit α} (h : KPresB m) : + HT (fun σ => σ.schedulerAction ≠ .resumeCurThread) m (fun _ σ => σ.schedulerAction ≠ .resumeCurThread) := + fun σ hσ r hr e => hσ ((h σ r hr).act e) + +theorem replyPush_noResA (t d r : ObjRef) (cd : Bool) : + HT (fun σ => σ.curThread = t) (replyPush (Z := Unit) t d r cd) + (fun _ σ => σ.schedulerAction ≠ .resumeCurThread) := by + unfold replyPush + have ct : ∀ {β : Type} {m : SMonad Unit β}, KPresB m → + HT (fun σ => σ.curThread = t) m (fun _ σ => σ.curThread = t) := + fun h σ hσ r hr => (h σ r hr).ct.trans hσ + refine HT.bind (ct (by kbres)) fun a => ?_ + refine HT.bind (ct (by kbres)) fun b => ?_ + refine HT.bind (ct (by kbres)) fun _ => ?_ + refine HT.bind (setThreadState_noResA t _ rfl) fun _ => ?_ + exact noRes_kb (by kbres) + +/-- **The tail of `send_ipc` from a state with no pending switch** keeps "the action does not +resume" and leaves `SwA`. -/ +theorem sendTail_endA (d : ObjRef) (Nr : Prop) : + HT (fun σ => (FIA σ ∧ BAs [d] σ) ∧ (NoSwA σ ∧ (Nr → σ.schedulerAction ≠ .resumeCurThread))) + (do setThreadState (Z := Unit) d .running + let destSc ← getTcbObjRef Tcb.tcbSchedContext d + ifSporadicCurScTestRefillUnblockCheck destSc + possibleSwitchTo d) + (fun _ σ => SwA σ ∧ (Nr → σ.schedulerAction ≠ .resumeCurThread)) := by + have refill : ∀ sc : Option ObjRef, TPres (ifSporadicCurScTestRefillUnblockCheck (Z := Unit) sc) := fun sc => by + unfold ifSporadicCurScTestRefillUnblockCheck ifCondRefillUnblockCheck; tpres + have sw : HT (fun σ => (FIA σ ∧ BAs [d] σ) ∧ NoSwA σ) + (do setThreadState (Z := Unit) d .running + let destSc ← getTcbObjRef Tcb.tcbSchedContext d + ifSporadicCurScTestRefillUnblockCheck destSc + possibleSwitchTo d) (fun _ σ => SwA σ) := by + let P : State Unit → Prop := fun σ => (FIA σ ∧ BAs [d] σ) ∧ (NoSwA σ ∧ StIs d .running σ) + refine HT.bind (Q := fun _ σ => P σ) (htAnd (HT.mono (setThreadState_fia d .running rfl (BAs [d]) + (fun σ p x x' hx h1 h => h.tcbUpd hx h1) (fun σ a h => h.act a)) (fun _ h => h.1) (fun _ _ h => h)) ?_) + fun _ => ?_ + · unfold Fl4SpecA64.setThreadState + refine HT.bind (Q := fun _ σ => NoSwA σ ∧ StIs d .running σ) (fun σ h r hr => ?_) fun _ σ h r hr => ?_ + · obtain ⟨x, -, e⟩ := threadSet_memA _ d hr + rw [e] + refine ⟨fun u => h.2 u, fun y hy => ?_⟩ + have : ({ x with tcbState := .running } : Tcb) = y := by simpa using hy + rw [← this] + · rw [setThreadStateAct_eq] at hr + have e : r = ((), stsAct σ d) := hr + rw [e] + exact ⟨stsAct_noSwA σ d h.1, fun y hy => h.2 y (by rwa [stsAct_kheap] at hy)⟩ + refine HT.bind (Q := fun _ σ => P σ) (fun σ h z hz => by + have e : z.2 = σ := by + unfold getTcbObjRef at hz + exact ((threadGet_eqPres _ _) σ z hz).symm + rw [e]; exact h) fun sc => ?_ + refine HT.bind (Q := fun _ σ => P σ) (fun σ h z hz => by + have k := refill sc σ z hz + refine ⟨⟨h.1.1.ofTKeep k, h.1.2.ofTKeep k⟩, fun u e => h.2.1 u ?_, (StIs.frame d .running).keep σ _ k h.2.2⟩ + rw [← k.2.2.2.2.1]; exact e) fun _ => ?_ + exact HT.mono (possibleSwitchTo_swRunA d) + (fun σ h => ⟨fun u e => absurd e (h.2.1 u), fun x hx => by rw [h.2.2 x hx]; rfl, + h.1.2 d (List.mem_singleton_self d)⟩) (fun _ _ h => h) + intro σ h r hr + refine ⟨sw σ ⟨h.1, h.2.1⟩ r hr, fun c => ?_⟩ + have nr : HT (fun σ => σ.schedulerAction ≠ .resumeCurThread) + (do setThreadState (Z := Unit) d .running + let destSc ← getTcbObjRef Tcb.tcbSchedContext d + ifSporadicCurScTestRefillUnblockCheck destSc + possibleSwitchTo d) (fun _ σ => σ.schedulerAction ≠ .resumeCurThread) := by + refine HT.bind (noRes_kb KPresBInst.pres) fun _ => ?_ + refine HT.bind (noRes_kb KPresBInst.pres) fun sc => ?_ + refine HT.bind (noRes_kb (show KPresB (ifCondRefillUnblockCheck (Z := Unit) sc (some false) (some false)) from KPresBInst.pres)) fun _ => ?_ + exact noRes_kb KPresBInst.pres + exact nr σ (h.2.2 c) r hr + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **`send_ipc` to a waiting receiver from a state with no pending switch** (module docstring). -/ +theorem sendIpc_recv_endA {th : TypedHeapA} {kb : U64} (hPl : PlaceA th kb) (sf : S) {σ : State Unit} + (hR : StateRelA th (L.mem sf) σ) (hV : SlotsValidA th (L.mem sf)) {t d : U64} {e : ObjRef} {q : List ObjRef} + (htr : th d = some .tcb) (hk : σ.kheap e = some (.endpoint (.recvEP (specPtr d :: q)))) + {xt xd : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) (hkd : σ.kheap (specPtr d) = some (.tcb xd)) + (hal : ∀ rb, ipcBufOfA true xd = some rb → (bufAddr rb 0).toNat % 8 = 0) + (blk call : Bool) (badge : Badge) (g gr cd : Bool) + (hcase : XferCaseA xt g) + {e' : ObjRef} {rep : Option ObjRef} {dd : ReceiverPayload} (hst : xd.tcbState = .blockedOnReceive e' rep dd) + (hbr : (call = false ∧ cd = false ∧ xt.tcbFault = none) ∨ + ((call = true ∨ xt.tcbFault ≠ none) ∧ ((g = false ∧ gr = false) ∨ rep = none)) ∨ + ((call = true ∨ xt.tcbFault ≠ none) ∧ (g = true ∨ gr = true) ∧ rep ≠ none ∧ specPtr t ≠ specPtr d ∧ + ActTcbA σ (specPtr t) ∧ (cd = true → xd.tcbSchedContext = none)) ∨ + ((call = true ∨ xt.tcbFault ≠ none) ∧ (g = true ∨ gr = true) ∧ rep ≠ none ∧ cd = true ∧ + specPtr t ≠ specPtr d ∧ xd.tcbSchedContext ≠ none ∧ ActTcbA σ (specPtr t)) ∨ + (call = false ∧ cd = true ∧ xt.tcbFault = none ∧ xd.tcbSchedContext = none ∧ + ActTcbA σ (specPtr t))) + (hI : SchedValidA σ) (hD : RqDomPrio σ) + (hnsw : NoSwA σ) (hDon : DonA σ (specPtr t) xt) + (hti : specPtr t ≠ σ.idleThread) (hdi : specPtr d ≠ σ.idleThread) : + ∀ x ∈ (sendIpc (Z := Unit) blk call badge g gr cd (specPtr t) e σ).1, + SwA x.2 ∧ (σ.curThread = specPtr t ∧ (call = true ∨ xt.tcbFault ≠ none) → + x.2.schedulerAction ≠ .resumeCurThread) := by + exact sendIpc_recv_gFA hPl sf hR hV htr hk hkt hkd hal blk call badge g gr cd hcase hst hbr hI hD + (Mid := fun s => NoSwA s ∧ (σ.curThread = specPtr t ∧ (call = true ∨ xt.tcbFault ≠ none) → + s.schedulerAction ≠ .resumeCurThread)) + (Post := fun s => SwA s ∧ (σ.curThread = specPtr t ∧ (call = true ∨ xt.tcbFault ≠ none) → + s.schedulerAction ≠ .resumeCurThread)) + (sendTail_endA (specPtr d) _) + (fun s1 hs _ _ x hx => by + have k := setThreadState_sikA (i := σ.idleThread) (M := fun _ => True) (N := fun _ => False) (don := false) + (specPtr t) trivial .inactive (by intro h; cases h) s1 x hx + exact ⟨k.noSwA (hs.noSwA hnsw), fun h => setThreadState_noResA (specPtr t) .inactive rfl s1 + (by rw [hs.frame]; exact h.1) x hx⟩) + (fun s1 hs _ _ _ x hx => by + have k := replyPush_sikA (i := σ.idleThread) (M := fun _ => True) (N := fun _ => False) (d := specPtr d) + (r := the rep) hkt hDon trivial trivial hti hdi cd s1 hs x hx + exact ⟨k.noSwA (hs.noSwA hnsw), fun h => replyPush_noResA _ _ _ _ s1 + (by rw [hs.frame]; exact h.1) x hx⟩) + (fun s1 hs c1 => ⟨hs.noSwA hnsw, fun c => absurd c.2 c1⟩) + (fun s1 hs h x hx => by + obtain ⟨hcl, -, hf, -, ⟨x0, sc, ex, es, -⟩⟩ := h + have hx0 : x0 = xt := KernelObject.tcb.inj (Option.some.inj (ex.symm.trans hkt)) + subst hx0 + have k := donPlain_sikA (i := σ.idleThread) (M := fun _ => True) (N := fun _ => False) (d := specPtr d) + hkt hDon trivial hti hdi (by rw [es]; simp) s1 hs x hx + refine ⟨k.noSwA (hs.noSwA hnsw), fun c => ?_⟩ + rcases c.2 with h | h + · rw [hcl] at h; cases h + · exact absurd hf h) + +end + +/-! ## The budget frame from a faulted sender -/ + +theorem doFaultTransfer_blA (badge : Data) (snd rcv : ObjRef) (buf : Option ObjRef) : + PresR BLA (doFaultTransfer (Z := Unit) badge snd rcv buf) := by + refine doFaultTransfer_genA (R := BLA) (fun a b e => e ▸ PreO.refl a) (fun t f => PresR.blA (asUser_bkA t f)) + (fun p => ?_) _ _ _ _ (fun s x regs hx => BLA.tcbW hx) (fun rb _ k _ v => storeWordOffs_blA rb k v) + have w : ∀ c, PresR BLA (setScObjRef (Z := Unit) scConsumedUpdate p c) := fun c s r hr => by + obtain ⟨z, n, hk, e⟩ := updateSchedContext_memA p _ hr + rw [e] + refine ⟨rfl, rfl, rfl, rfl, rfl, rfl, id, fun y' k h => ?_⟩ + by_cases c' : s.curSc = p + · rw [c'] at h ⊢ + have h' := KernelObject.schedContext.inj (Option.some.inj ((Function.update_self p _ s.kheap).symm.trans h)) + obtain ⟨rfl, rfl⟩ := h' + exact ⟨z, hk, rfl, rfl⟩ + · exact ⟨y', by simpa [Function.update_of_ne c'] using h, rfl, rfl⟩ + unfold schedContextUpdateConsumed + refine PresR.bind (fun s x hx => by rw [← getSchedContext_eqPres p s x hx]; exact PreO.refl s) fun _ => + PresR.bind (PresR.pure _) fun _ => ?_ + exact PresR.ite' (PresR.bind (w _) fun _ => PresR.pure _) (PresR.bind (w _) fun _ => PresR.pure _) + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **`send_ipc` to a waiting receiver keeps the budget frame**, faulted senders included (module docstring). -/ +theorem sendIpc_recv_blFA {th : TypedHeapA} {kb : U64} (hPl : PlaceA th kb) (sf : S) {σ : State Unit} + (hR : StateRelA th (L.mem sf) σ) (hV : SlotsValidA th (L.mem sf)) {t d : U64} {e : ObjRef} {q : List ObjRef} + (htr : th d = some .tcb) (hk : σ.kheap e = some (.endpoint (.recvEP (specPtr d :: q)))) + {xt xd : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) (hkd : σ.kheap (specPtr d) = some (.tcb xd)) + (hal : ∀ rb, ipcBufOfA true xd = some rb → (bufAddr rb 0).toNat % 8 = 0) + (blk call : Bool) (badge : Badge) (g gr cd : Bool) (hcase : XferCaseA xt g) : + ∀ x ∈ (sendIpc (Z := Unit) blk call badge g gr cd (specPtr t) e σ).1, BLA σ x.2 := by + rcases hcase with ⟨hf, hcaps⟩ | ⟨f, hf⟩ + · exact sendIpc_recv_blA hPl sf hR hV htr hk hkt hkd hf hal blk call badge g gr cd hcaps + suffices H : PresRC (UBA (BufBytesA (ipcBufOfA true xd))) (· = σ) + (sendIpc (Z := Unit) blk call badge g gr cd (specPtr t) e) from fun x hx => (H σ rfl x hx).2 + unfold sendIpc + refine PresRC.bind_oneA (getEndpoint_eq hk) ?_ + refine PresRC.monoPA (P' := fun s => TcbKeepA σ s ∧ UBA (BufBytesA (ipcBufOfA true xd)) σ s) + (fun s h => h ▸ ⟨fun _ _ h => h, PreO.refl s⟩) ?_ + cases blk <;> dsimp only + all_goals + refine PresRC.bind_inv (stepKBA _) fun _ => ?_ + refine PresRC.bind_inv (stepKBA _) fun recvState => ?_ + cases recvState + case blockedOnReceive a r dd => + dsimp only + refine PresRC.bind_pureA r ?_ + refine PresRC.bind_last (fun s hs x hx => ?_) (fun _ => PresR.andA (PresR.ufrA (by spres)) (PresR.blA (by kbres))) + have hR' := hR.specSchedA hs.2.1.1 + have h1 := doIpcTransfer_any_ufrA hPl sf hR' hV htr (hs.1 _ _ hkt) (hs.1 _ _ hkd) hal _ _ _ + (Or.inr ⟨f, hf⟩) s rfl x hx + have h2 : PresRC BLA (· = s) (doIpcTransfer (Z := Unit) (specPtr t) (some e) badge g (specPtr d)) := + doIpcTransfer_fault_genA (hs.1 _ _ hkt) (hs.1 _ _ hkd) hf _ _ _ (doFaultTransfer_blA _ _ _ _) + exact ⟨h1, h2 s rfl x hx⟩ + all_goals (dsimp only; exact PresRC.bind_fail) + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3IpiKeepA.lean b/proofs/Fl4ProofsA64/P3/Stage3IpiKeepA.lean new file mode 100644 index 000000000..61997be5c --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3IpiKeepA.lean @@ -0,0 +1,121 @@ +/- +P3 (aarch64, A64-P3 Stage 3, record edit B2, item 6 `ipiZero`): **the IPI pair is kept by a step off the scheduler's +read words, and by a scheduler step** (the frames that carry `JStage3A.ipiZero`). + +* **`SilentScheduleIpiA.keepW`**: the pair's two words unchanged (read at the earlier state) keep `SilentScheduleIpiA`. +* **`SchedSRN.ipiKeepR`**: a step writing only words outside `SReadA` (every node word of every core of `Z.Cs` is in + `SReadA`) keeps the pair: the hypothesis is the one `SchedSRA.stable` takes. +* **`SchedSRN.ipiKeepW`**: a step writing inside the scheduler words `SWrA` keeps the pair: the narrowed `SKsA` (record + edit B2) leaves offsets 120 and 128 of every node out, and the TCB link / SC / flag words are off the nodes. + +No named premise. +-/ +import Fl4ProofsA64.P3.ScSWrA +import Fl4ProofsA64.P3.ScStableA +import Fl4ProofsA64.P3.ScScheduleTimerA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- The pair's two words of the current core are unchanged. -/ +theorem SilentScheduleIpiA.keepW {s s' : S} (H : SilentScheduleIpiA (st := st) s) (hk : L.ksb s' = L.ksb s) + (hc : L.cpu s' = L.cpu s) + (hw : ∀ o : U64, (o.val = 120 ∨ o.val = 128) → + L.mem s' (L.nodeAt s (L.cpu s) o) = L.mem s (L.nodeAt s (L.cpu s) o)) : + SilentScheduleIpiA (st := st) s' := + ⟨by rw [nodeAt_of_ksb hk, hc]; exact (hw _ (Or.inl (by simp [layout.node.IPI_PENDING]))).trans H.1, + by rw [nodeAt_of_ksb hk, hc]; exact (hw _ (Or.inr (by simp [layout.node.IPI_PENDING_HI]))).trans H.2⟩ + +/-- **A write off `SReadA` keeps the IPI pair.** -/ +theorem SchedSRN.ipiKeepR {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s s' : S} + {σ : Fl4SpecA64.State Unit} (H : SchedSRN st Z m κ s σ) {W : U64 → Prop} (hw : WritesIn st s s' W) + (hW : ∀ a, W a → ¬ SReadA st Z m s σ a) (Hz : SilentScheduleIpiA (st := st) s) : + SilentScheduleIpiA (st := st) s' := + Hz.keepW hw.1 hw.2.1 fun o ho => + hw.mem_eq (nodeAt_aligned (H.core.nodes _ H.sch.cpuCs) (by omega) (by omega)) + (fun h => hW _ h (Or.inl ⟨L.cpu s, H.sch.cpuCs, o, by omega, rfl⟩)) + +/-- The IPI pair's words are node words of a kernel core: their addresses. -/ +theorem SchedSRN.ipi_val {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} + {σ : Fl4SpecA64.State Unit} (H : SchedSRN st Z m κ s σ) {o : U64} (ho : o.val = 120 ∨ o.val = 128) : + (L.nodeAt s (L.cpu s) o).val = (L.ksb s).val + 223488 + (L.cpu s).val * 4288 + o.val ∧ + (L.cpu s).val < 128 := by + obtain ⟨nn, -, hn, hcs⟩ := H.csN + have hcl : (L.cpu s).val < 128 := by have := (hcs _).1 H.sch.cpuCs; omega + exact ⟨nodeAt_valSc (s := s) H.ksFit hcl (by omega), hcl⟩ + +/-- **The IPI pair's words are off the scheduler words `SWrA`.** -/ +theorem SchedSRN.ipi_swr {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} + {σ : Fl4SpecA64.State Unit} (H : SchedSRN st Z m κ s σ) {o : U64} (ho : o.val = 120 ∨ o.val = 128) : + ¬ SWrA (st := st) Z s (L.nodeAt s (L.cpu s) o) := by + intro h + obtain ⟨nn, -, hn, hcs⟩ := H.csN + have hcl : (L.cpu s).val < 128 := by have := (hcs _).1 H.sch.cpuCs; omega + have hN := H.core.nodes _ H.sch.cpuCs + have hT := H.core.tcbs + have ho' : o.val < 4288 := by omega + rcases h with h | ⟨u, hu, e⟩ + · have hv := nodeAt_valSc (s := s) H.ksFit hcl ho' + unfold SKsA at h + rw [hv] at h + omega + · rcases e with e | e | e | e + · rw [scA_eq] at e + have h1 := congrArg UScalar.val e + rw [field_val (hT u hu) (K := 3464#u64) (by simp)] at h1 + have := nodeAt_out hN ho' hu + simp at h1; omega + · exact nodeAt_ne_nextA hN ho' hu (hT u hu) _ e + · exact nodeAt_ne_prevA hN ho' hu (hT u hu) _ e + · exact inqA_ne_nodeAt hN ho' hu (hT u hu) e.symm + +/-- **A write inside `SWrA` keeps the IPI pair.** -/ +theorem SchedSRN.ipiKeepW {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s s' : S} + {σ : Fl4SpecA64.State Unit} (H : SchedSRN st Z m κ s σ) (hw : WritesIn st s s' (SWrA (st := st) Z s)) + (Hz : SilentScheduleIpiA (st := st) s) : SilentScheduleIpiA (st := st) s' := + Hz.keepW hw.1 hw.2.1 fun o ho => + hw.mem_eq (nodeAt_aligned (H.core.nodes _ H.sch.cpuCs) (by omega) (by omega)) (H.ipi_swr ho) + +/-- **The IPI pair's words are off the MCS write set `MWrA`** (the SC footprint is off the state block). -/ +theorem SchedSRN.ipi_mwr {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} + {σ : Fl4SpecA64.State Unit} (H : SchedSRN st Z m κ s σ) {o : U64} (ho : o.val = 120 ∨ o.val = 128) : + ¬ MWrA (st := st) Z m σ s (L.nodeAt s (L.cpu s) o) := by + intro h + rcases h with hs | hw + · obtain ⟨hv, hcl⟩ := H.ipi_val ho + have := H.scp.ks_out hs + unfold ksSizeA at this + omega + · exact H.ipi_swr ho hw + +/-- **A scheduler step with the MCS frame `MFrA` keeps the IPI pair** (the frame of `schedule`'s switch and +release-walk steps). -/ +theorem SchedSRN.ipiKeepM {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s s' : S} + {σ σ' : Fl4SpecA64.State Unit} (H : SchedSRN st Z m κ s σ) (F : MFrA (st := st) Z m s s' σ σ') + (Hz : SilentScheduleIpiA (st := st) s) : SilentScheduleIpiA (st := st) s' := + Hz.keepW F.wr.1 F.wr.2.1 fun o ho => + F.wr.mem_eq (nodeAt_aligned (H.core.nodes _ H.sch.cpuCs) (by omega) (by omega)) (H.ipi_mwr ho) + +/-- **The same across the frame `MFrDA` that includes the domain-schedule words** (`next_domain`; the selection). -/ +theorem SchedSRN.ipiKeepMD {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s s' : S} + {σ σ' : Fl4SpecA64.State Unit} (H : SchedSRN st Z m κ s σ) (F : MFrDA (st := st) Z m s s' σ σ') + (Hz : SilentScheduleIpiA (st := st) s) : SilentScheduleIpiA (st := st) s' := + Hz.keepW F.wr.1 F.wr.2.1 fun o ho => + F.wr.mem_eq (nodeAt_aligned (H.core.nodes _ H.sch.cpuCs) (by omega) (by omega)) (fun h => by + rcases h with h | h + · exact H.ipi_mwr ho h + · obtain ⟨hv, hcl⟩ := H.ipi_val ho + unfold DomW at h + omega) + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3J5A.lean b/proofs/Fl4ProofsA64/P3/Stage3J5A.lean new file mode 100644 index 000000000..656690819 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3J5A.lean @@ -0,0 +1,122 @@ +/- +P3 (aarch64, A64-P3 Stage-3 integration; Architect G4 and ruling of 2026-12-03 00:33Z): **the concrete J5 slot**, the +P1-MDB part of `P3/Stage3A`'s unified record. + +* **`J5A`**: the record's CDT ghost `Z.G` (`chains`, `par`) with A64 P1's `MdbInvA`, `IrqRunA` and `HandlersActiveA` + (`P1/MdbInvA`, `P1/MdbIrqInvA`), and the finality facts of `P3/CnodeFinalA`'s `FinStA` that the record lacked (ledger + "Untyped closure A"): every slot of the typed heap is an `OS` slot (`FinStA.sub`), and an `OS` word that is no + typed slot is a null cap (`FinStA.nul`). +* **Boot**: `w0_J5A` (empty chains) and `w1_J5A` (A64 P1's `w0_*` / `w1_*` witnesses of `P1/MdbBootInvA`, and the slot + sets of `P3/JBootW0A` / `JBootW1A`). + +R1 (Architect 01:46Z): `w1ZA.G.par := w1Par` (A64 P1's true post-boot parent map; `MdbInvA` / `ParentAdjA` hold at `w1` only for it) +and the spec boot state `w1SA` has the matching CDT (`w1CdtA`: the root TCB's CNode, VSpace and buffer caps are children +of root CNode slots 2, 3, 10), as l4v's `init_cdt`. + +New module; no existing statement changed. +-/ +import Fl4ProofsA64.P3.Stage3J7A +import Fl4ProofsA64.P3.Stage3KtA +import Fl4ProofsA64.P3.BootAddrA +import Fl4ProofsA64.P1.MdbBootInvA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST + +noncomputable section + +section +variable {S : Type} (st : machine.Store S) [L : LawfulStore st] + +/-- **J5, concrete** (module docstring). -/ +def J5A : JSlotA S := fun Z H OS s σ m => + (Fl4ProofsA64.P1.W7.R.ST.MdbInvA st Z.G.chains Z.G.par s ∧ + Fl4ProofsA64.P1.W7.R.ST.IrqRunA st Z.G.chains s ∧ Fl4ProofsA64.P1.W7.R.ST.HandlersActiveA st Z.G.chains s) ∧ + (∀ y, IsSlotA Z.th y → y ∈ OS) ∧ (∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) + +end + +section boot +open Fl4ProofsA64.P1.W7.R.ST.BootWit +variable {kb ib r now mt sl : U64} {nc : Nat} {uw : U64 → U64} + +/-- **J5 at `w0`**. -/ +theorem w0_J5A (P : BootLay kb ib nc) (hL : BootLayA ib nc) (hK : BootKsA kb ib nc) + (σ : Fl4SpecA64.State Unit) (m : Fl4SpecA64.McState) : + J5A bst (bootZA kb ib nc) (bootH kb ib nc) (bootOS kb ib nc) (bootS kb ib nc r now mt) σ m := by + have eq := Set.ext_iff.1 (bootOS_eq hL hK) + exact ⟨⟨w0_mdbInvA P, w0_irqRunA P, w0_handlersActiveA P⟩, fun y hy => (eq y).1 hy, + fun y hy hn => absurd ((eq y).2 hy) hn⟩ + +/-- **J5 at `w1`**. -/ +theorem w1_J5A (P : BootLay1 kb ib nc) (hL : BootLayA ib nc) (hK : BootKsA kb ib nc) (σ : Fl4SpecA64.State Unit) (m : Fl4SpecA64.McState) : + J5A bst (w1ZA kb ib nc) (w1H kb ib nc) (w1OS kb ib nc) (w1S kb ib nc r now mt sl uw) σ m := by + have eq := Set.ext_iff.1 (w1OS_eqA hL hK P) + exact ⟨⟨w1_mdbInvA P, w1_irqRunA P, w1_handlersActiveA P⟩, fun y hy => (eq y).1 hy, + fun y hy hn => absurd ((eq y).2 hy) hn⟩ + +end boot + +section conc +open Fl4ProofsA64.P1.W7.R.ST.BootWit Fl4ProofsA64.P3.Endp +local notation "KB" => (18446603337294938112#u64 : U64) +local notation "IB" => (18446603338368679936#u64 : U64) + +private theorem bootConstsA : BootLayA IB 128 ∧ BootKsA KB IB 128 := by + have hi : (IB : U64).val = 18446603338368679936 := by simp + have hk : (KB : U64).val = 18446603337294938112 := by simp + have hsc : 18446603336221208576 < (IB : U64).val ∨ (IB : U64).val + 128 * 8192 ≤ 18446603336221208576 := + Or.inl (by rw [hi]; omega) + have hsk : 18446603336221208576 < (KB : U64).val ∨ (KB : U64).val + 788736 ≤ 18446603336221208576 := + Or.inl (by rw [hk]; omega) + exact ⟨bootLayA_of bootLayC_ex hsc, bootKsA_of bootLayC_ex hsk⟩ + +/-- **The `w0` entry record with J5 and J7 concrete.** -/ +theorem boot_stage3_conc {A : Type} (AR : A → HwRootA → Prop) (ar : machine.Arch A) (hKT : NoKtDescA ar) {a : A} + (r now mt : U64) (Lv : U64 → Nat) (n : U64) + (hAR : AR a bootHwA) (hnc : 128 ≤ Fl4SpecA64.fl4A64Platform.numNodes) + (hnn : Fl4SpecA64.fl4A64Platform.numNodes ≤ 128) (hr : specPtr r = Fl4SpecA64.armGlobalPtPtr) : + JStage3EntryA bst AR (bootZA KB IB 128) (bootH KB IB 128) (bootOS KB IB 128) ∅ (bootH KB IB 128).scs ∅ ∅ Lv + (IB : U64).val ((IB : U64).val + 128 * 8192) ∅ ∅ ∅ ∅ (fun _ => []) + (bootSchedScCoreA IB) 128 (J5A bst) (J7A bst (J7R2A bst ar n)) (bootS KB IB 128 r now mt) a + (bootSA KB IB 128 now mt) (bootMA IB 128) := + boot_stage3_ex AR r now mt Lv hAR hnc hnn hr (J5A bst) (J7A bst (J7R2A bst ar n)) + (w0_J5A bootLayC_ex bootConstsA.1 bootConstsA.2 _ _) (w0_J7A ar hKT bootLayC_ex n _ _) + +/-- **The `w1` entry record with J5 and J7 concrete**, at the heap `KtH (w1H …) {kt}` and the arch `w1Ar ar` (one +kernel TTBR1 table). -/ +theorem w1_stage3_conc {A : Type} (AR : A → HwRootA → Prop) (ar : machine.Arch A) {a : A} + (r now mt sl : U64) (uw : U64 → U64) (n : U64) + (hAR : AR a bootHwA) (hnc : 128 ≤ Fl4SpecA64.fl4A64Platform.numNodes) + (hnn : Fl4SpecA64.fl4A64Platform.numNodes ≤ 128) (hsl : Fl4SpecA64.MIN_BUDGET ≤ sl.bv) (hr : specPtr r = Fl4SpecA64.armGlobalPtPtr) : + JStage3EntryA bst AR (w1ZA KB IB 128) (KtH (w1H KB IB 128) {bwv KTb}) (w1OS KB IB 128) w1U (w1H KB IB 128).scs + (Fl4ProofsA64.P2.BootEpB.w1Pp KB IB 128 r now mt sl uw) w1Tb w1Lv + (IB : U64).val ((IB : U64).val + 128 * 8192) ∅ ∅ ∅ w1BfA (fun _ => []) + (w1SchedScCoreA IB) 128 (J5A bst) (J7A bst (J7R2A bst (w1Ar ar) n)) (w1S KB IB 128 r now mt sl uw) a + (w1SUA KB IB 128 now mt sl uw) (w1MA IB 128) := by + have hi : (IB : U64).val = 18446603338368679936 := by simp + have hk : (KB : U64).val = 18446603337294938112 := by simp + have P : BootLay1 KB IB 128 := ⟨bootLayC_ex, by rw [hk]; norm_num, by rw [hi]; norm_num⟩ + have J := w1_stage3_ex AR (a := a) r now mt sl uw hAR hnc hnn hsl hr (fun _ _ _ _ _ _ => True) + (fun _ _ _ _ _ _ => True) trivial trivial + refine J.addKt (KtOut.noUb (w1K_out P)) (fun _ h => absurd h (Set.notMem_empty _)) (fun q hq b hb => ?_) + (fun q hq => by + have hq' := ktv hq + have eB := Fl4ProofsA64.P0.Consts.layout.PPTR_BASE.val_eq + omega) + (fun y hy p size rights mapped ept asid va h => ?_) + (w1_J5A (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) P bootConstsA.1 bootConstsA.2 + (w1SUA KB IB 128 now mt sl uw) (w1MA IB 128)) + (w1_J7A_full (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) ar P n (w1SUA KB IB 128 now mt sl uw) + (w1MA IB 128)) + · have hq' := ktv hq + obtain ⟨-, h1, h2⟩ := hb + omega + · exact (w1_noDevFrA P hy h).elim + +end conc + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3J7A.lean b/proofs/Fl4ProofsA64/P3/Stage3J7A.lean new file mode 100644 index 000000000..8efe01f78 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3J7A.lean @@ -0,0 +1,233 @@ +/- +P3 (aarch64, A64-P3 Stage-3 integration; Architect G4 and ruling of 2026-12-03 00:33Z): **the concrete J7 slot**, the +S1a inputs of `P3/Stage3A`'s unified record. + +* **`J7A R`**: at some page-table pages `Pt` and user words `Ub`: A64 P1's `VrOK` (VCPUs `Z.V`), `PoolNamed`, + `S1aArmCol`, `S1aCapCol`, `VcCap`, `DevFrAl`, `HiCap511`; and, for the scheduler frames: + * [J] the VSpace link: every page `SchedPageA` names (`Z`'s pools, tables, windows, Stage-2 pages, and the tables of + the `PageTable` / `Pml4` caps; merge of Design 1A, executor 16: the two pages of an unassigned Stage-2 root's + `Ept` cap and the private L1 page of an unassigned `HighVSpace` cap, for `UnmPtSpecA.s2` / `.hv`) is in `Pt`, so `VrOK.ks` / `.sc` / `.os` apply to its words; + * [J] `schedApart`: the VCPU words are off the kernel-state globals `[0, 223488)`. `VrOK` and `VcH` do not give it; + its producer is A64 P1's `VcTy.ks`. + `R Pt` is the remaining part: **`J7R2A`**, `S1aR2` with `S1aKtUt`. +* **Boot**: `w0_J7A` proves the whole of `J7A (J7R2A n)`. `w1_J7A` proves it at the heap `KtH (w1H …) {kt}` from + `R (w1Pt ∪ {kt})`, and `w1_J7A_full` supplies that part with A64 P1's `w1K_s1aStepEntryK` (`S1aR2` with the one kernel + table `kt`) at the arch `w1Ar ar`. +-/ +import Fl4ProofsA64.P3.Stage3BootA +import Fl4ProofsA64.P1.S1aStepR2Boot +import Fl4ProofsA64.P1.S1aBootW1 +import Fl4ProofsA64.P1.BootW1Items +import Fl4ProofsA64.P1.BootMdb1 +import Fl4ProofsA64.P1.S1aVsUnmB +import Fl4ProofsA64.P1.S1aKTab +import Fl4ProofsA64.P1.S1aBootK +import Fl4ProofsA64.P1.S1aStepR2KBoot +import Fl4ProofsA64.P1.S1aBootDel1 +import Fl4ProofsA64.P3.Stage3KtA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P1.W7.R.ST.BootWit + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} (st : machine.Store S) [L : LawfulStore st] + +/-- **The `S1aR2` / `S1aKtUt` / `KTab` part of J7** at the page-table pages `Pt`, the platform's address width `n` and +the machine arch `ar` (Architect 14:07Z; record edit #6: `J7R2K`, the conjunct `KTab ar Kt Lv` at R2's own level +function dropped, the P1 step does not re-establish it): `KTab ar Kt KLv` is A64 P1's [P] fact that the kernel TTBR1 level-0 table +descriptors name tables of `Kt`. -/ +def J7R2A {A : Type} (ar : machine.Arch A) (n : U64) (Pt : Set U64) : JSlotA S := fun _ H OS s _ _ => + ∃ (Pp Tb Hr Te Kt : Set U64) (Lv Re bits : U64 → Nat), + S1aR2 st H OS Pt Pp Tb Hr Te Kt Lv Re bits n s ∧ S1aKtUt st OS Kt s ∧ + ∃ KLv : U64 → Nat, KTab ar Kt KLv ∧ S1aHiK st OS Kt KLv s + +/-- **The boot-arch condition of the boot witnesses** (`KTab.empty`'s hypothesis): no kernel TTBR1 level-0 entry is a +table descriptor. A hypothesis of the `w0` witness only (`w1` takes the arch `w1Ar ar` with one kernel table instead). It speaks of the machine arch `ar` only, independently of `hAR : AR a bootHwA` +(`AR` relates the abstract arch state `a`). -/ +def NoKtDescA {A : Type} (ar : machine.Arch A) : Prop := + ∀ (a : A) (i e : U64), i.val < 511 → ar.kernel_ttbr1_entry a 0#u64 i = ok e → (e &&& 3#u64) ≠ 3#u64 + +/-- An arch whose kernel TTBR1 entries are all zero. -/ +def noKtAr {A : Type} (ar : machine.Arch A) : machine.Arch A := + { ar with kernel_ttbr1_entry := fun _ _ _ => ok 0#u64 } + +/-- **Satisfiability of the boot-arch condition**: `noKtAr ar` has `NoKtDescA` and `KTab` at `Kt = ∅` (at every level +function), so the hypothesis is not `False`, and it is independent of any `AR` / `hAR`. -/ +theorem noKtDescA_noKtAr {A : Type} (ar : machine.Arch A) : NoKtDescA (noKtAr ar) := fun a i e _ he h3 => by + have h0 : (0#u64 : U64) = e := okinj he + rw [← h0] at h3 + exact absurd h3 and3z + +theorem ktab_noKtAr {A : Type} (ar : machine.Arch A) (KLv : U64 → Nat) : KTab (noKtAr ar) ∅ KLv := + KTab.empty KLv (noKtDescA_noKtAr ar) + +/-- A word of a Stage-2 root's upper page (merge of Design 1A: `UnmS2RootA` across the scheduler frames). -/ +theorem peAF_hiA (p i : U64) (h1 : 512 ≤ i.val) (h2 : i.val < 1024) : + ∃ j : U64, j.val < 512 ∧ peAF p i = peAF (wa p 4096#u64) j := by + have hj : (⟨BitVec.ofNat 64 (i.val - 512)⟩ : U64).val = i.val - 512 := by + show (BitVec.ofNat 64 (i.val - 512)).toNat = _ + rw [BitVec.toNat_ofNat] + exact Nat.mod_eq_of_lt (by omega) + refine ⟨⟨BitVec.ofNat 64 (i.val - 512)⟩, by rw [hj]; omega, ?_⟩ + apply UScalar.eq_of_val_eq + unfold peAF + have e8 : (8#u64 : U64).val = 8 := rfl + have e4 : (4096#u64 : U64).val = 4096 := rfl + simp only [core.num.U64.wrapping_add_val_eq, core.num.U64.wrapping_mul_val_eq, U64.size_def, U64.numBits, hj, + e8, e4] + have hs : UScalar.size UScalarTy.U64 = 18446744073709551616 := by + first | rfl | decide | (simp [U64.size_def, U64.numBits]; done) | + (simp [UScalar.size, UScalarTy.numBits, U64.numBits]; done) | (simp [U64.size, U64.numBits]; done) + simp only [hs] + omega + +/-- **J7, concrete** (module docstring). -/ +def J7A (R : Set U64 → JSlotA S) : JSlotA S := fun Z H OS s σ m => + ∃ Pt Ub : Set U64, + VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt ∧ PoolNamed st OS s ∧ + S1aArmCol st H Z.Cs OS Z.V Ub s ∧ S1aCapCol st H OS Ub s ∧ VcCap st Z.V OS s ∧ DevFrAl st OS s ∧ + HiCap511 st OS s ∧ (∀ p, SchedPageA st Z OS s p → p ∈ Pt) ∧ + (∀ b, VcW Z.V b → ∀ o : U64, o.val < 223488 → wa (L.ksb s) o ≠ b) ∧ + FrUb st Ub OS s ∧ (∀ b ∈ Ub, Aligned b) ∧ (∀ b, VcW Z.V b → b ∉ Ub) ∧ R Pt Z H OS s σ m + +end + +private theorem w1Pt_vsA : bwv VSb ∈ w1Pt := Or.inl (bwv_val (by norm_num)) + +set_option maxHeartbeats 4000000 in +/-- Every `Pml4` cap of a `w1` root CNode slot names the root table. -/ +private theorem capOf_pml4A (nc i : Nat) : ∀ p m a, capOf nc i = .Pml4 p m a → p ∈ w1Pt := by + capsplit (refine fun p m a h => ?_ <;> first + | (cases h; done) + | (cases h; exact w1Pt_vsA)) + +set_option maxHeartbeats 4000000 in +/-- No `w1` root CNode slot holds a Stage-2 root cap. -/ +private theorem capOf_eptA (nc i : Nat) : ∀ p l m a v, capOf nc i ≠ .Ept p l m a v := by + capsplit (intro p l m a v h; cases h) + +set_option maxHeartbeats 4000000 in +private theorem capOf_hvA (nc i : Nat) : ∀ y m a, capOf nc i ≠ .HighVSpace y m a := by + capsplit (intro y m a h; cases h) + +section boot +variable {kb ib r now mt sl : U64} {nc : Nat} {uw : U64 → U64} + +/-- **J7 at `w0`**. -/ +theorem w0_J7A {A : Type} (ar : machine.Arch A) (hKT : NoKtDescA ar) (P : BootLay kb ib nc) (n : U64) (σ : Fl4SpecA64.State Unit) (m : Fl4SpecA64.McState) : + J7A bst (J7R2A bst ar n) (bootZA kb ib nc) (bootH kb ib nc) (bootOS kb ib nc) (bootS kb ib nc r now mt) σ m := by + have nul : ∀ x ∈ bootOS kb ib nc, capA bst (bootS kb ib nc r now mt) x = .Null := Fl4ProofsA64.P1.W7.R.ST.BootWit.boot_null P + have hk : bL.ksb (bootS kb ib nc r now mt) = kb := rfl + have hZ : ∀ b, AsW (bL.ksb (bootS kb ib nc r now mt)) b → bL.mem (bootS kb ib nc r now mt) b = 0#u64 := + fun b hb => boot_asZero P b hb + have vr : VrOK (bootH kb ib nc).omega (bootH kb ib nc).frame (bootH kb ib nc).scs (bootH kb ib nc).scBits + (bootOS kb ib nc) ∅ ∅ kb ∅ := boot_vrOK P + have uo : UserOffFr bst (bootOS kb ib nc) (bootS kb ib nc r now mt) := by + unfold UserOffFr UserOffUt + constructor <;> intro u hu p b i hc <;> (rw [nul u hu] at hc; cases hc) + obtain ⟨Pp, -, R2, sep⟩ := w0_s1aStepEntry (r := r) (now := now) (mt := mt) P (fun _ => 0) (fun _ => 0) + (fun _ => 0) n + refine ⟨∅, ∅, vr, PoolNamed.boot hZ, ?_, ⟨fun b h => h.elim, uo, FrameOK.empty nul⟩, + CapInv.empty (fun v h => by cases h) nul, CapInv.empty (fun _ _ _ _ _ _ _ h => by cases h) nul, + HiCap511.ofNoHv (fun y hy p mp a h => by rw [nul y hy] at h; cases h), ?_, + fun b hb => ?_, CapInv.empty (FrUbP.null _) nul, fun _ h => h.elim, fun _ _ h => h.elim, + Pp, ∅, ∅, ∅, ∅, _, _, _, R2, sep, fun _ => 0, KTab.empty _ hKT, + ⟨fun _ h => h.elim, fun y hy _ _ _ c => by rw [nul y hy] at c; cases c⟩⟩ + · exact ⟨S1aCase.boot hZ (boot_slotPlaced P) boot_tcbSlots, + KsBridge.boot (bootH_ksOff P) (boot_slotKs P) vr, NtOffU.empty _, fun _ _ _ h => h.elim, VcH.empty _⟩ + · rintro p (h | h | h | h | ⟨_, h, -⟩ | h | ⟨_, h, -⟩ | + ⟨u, hu, ⟨_, _, _, h⟩ | ⟨_, _, h⟩ | ⟨_, _, h⟩ | ⟨_, _, _, h, -⟩ | ⟨_, _, h, -⟩⟩) + all_goals first + | exact absurd h (Set.notMem_empty _) + | (rw [nul u hu] at h; cases h) + · obtain ⟨v, hv, -⟩ := hb + exact absurd hv (Set.notMem_empty v) + +/-- **J7 at `w1`**, at the record heap with the one kernel table `kt` (`KtH (w1H …) {kt}`), given the `S1aR2` part at +`Pt = w1Pt ∪ {kt}` (module docstring). -/ +theorem w1_J7A (P : BootLay1 kb ib nc) (R : Set U64 → JSlotA BS) (σ : Fl4SpecA64.State Unit) (m : Fl4SpecA64.McState) + (hR : R (w1Pt ∪ {bwv KTb}) (w1ZA kb ib nc) (KtH (w1H kb ib nc) {bwv KTb}) (w1OS kb ib nc) + (w1S kb ib nc r now mt sl uw) σ m) : + J7A bst R (w1ZA kb ib nc) (KtH (w1H kb ib nc) {bwv KTb}) (w1OS kb ib nc) (w1S kb ib nc r now mt sl uw) σ m := by + have col := w1_column (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) P Set.univ + obtain ⟨⟨hcase, -, hpn, -, -, -, -, -, hvc, -, huo, -⟩, hkb, hup, -, hnt⟩ := col + have X := w1K_out (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) P + have B := w1K_bundle (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) P + have ks := X.ksOff (w1H_ksOff P) + have eT : (KtH (w1H kb ib nc) {bwv KTb}).tcbs = (w1H kb ib nc).tcbs := X.addOKP.tcbs_eq (by simp) + have hg := X.hg + have arm : S1aArmCol bst (KtH (w1H kb ib nc) {bwv KTb}) (bootCs nc) (w1OS kb ib nc) ∅ w1Ub + (w1S kb ib nc r now mt sl uw) := by + refine ⟨⟨fun x hx => ?_, by rw [eT]; exact hcase.ts, PoolOff.addKt X hcase.po, + fun i => WP.spec_mono (hcase.pobj i) fun _ h hne => hg _ _ (h hne)⟩, + ⟨ks.1, ks.2, hkb.slotKs, hkb.ksU, fun hi h => X.addOKP.omega_sub (hkb.hT hi h)⟩, + fun b hb p h => hnt b hb p ((X.addOKP.some_ne (by simp)).1 h), hup, VcH.empty _⟩ + obtain ⟨h1, q, k, hq, h2⟩ := hcase.slot x hx + exact ⟨h1, q, k, hg _ _ hq, h2⟩ + refine ⟨w1Pt ∪ {bwv KTb}, w1Ub, B.2.2.2.2.1, hpn, arm, + ⟨w1_ubFr P, huo, fun y hy ptr size rights x1 x2 x3 x4 hc bits hb => + hg _ _ (w1_frameOK (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) P y hy ptr size rights x1 x2 x3 x4 hc + bits hb)⟩, + hvc, w1_devFrAl P, (w1_s1aPtCol P).hi, ?_, fun b hb => ?_, w1_frUb P, fun _ hb => hb.1, + fun _ h => absurd h.choose_spec.1 (Set.notMem_empty _), hR⟩ + · intro p hp + refine Or.inl ?_ + revert p hp + have tb : ∀ p, p ∈ w1Tb → p ∈ w1Pt := by + rintro p (h | h | h | h) + · exact Or.inl h + · exact Or.inr (Or.inr (Or.inl h)) + · exact Or.inr (Or.inr (Or.inr (Or.inl h))) + · exact Or.inr (Or.inr (Or.inr (Or.inr h))) + rintro p (h | h | h | h | ⟨_, h, -⟩ | h | ⟨_, h, -⟩ | ⟨u, hu, ⟨mp, a, v, h⟩ | ⟨mp, a, h⟩ | hx⟩) + · exact Or.inr (Or.inl h) + · exact tb p h + · exact absurd h (Set.notMem_empty _) + · exact absurd h (Set.notMem_empty _) + · exact absurd h (Set.notMem_empty _) + · exact absurd h (Set.notMem_empty _) + · exact absurd h (Set.notMem_empty _) + · exact (w1_ptCapK (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) P hu).1 p mp a v h + · rcases w1_capS (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) P hu with + e | ⟨i, -, -, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, -, -, -, e⟩ <;> rw [e] at h + · cases h + · exact capOf_pml4A _ _ _ _ _ h + · exact capOf_pml4A _ _ _ _ _ h + · exact capOf_pml4A _ _ _ _ _ h + · cases h + · cases h + · exfalso + obtain ⟨ce, ch⟩ : (∀ p l m a v, capA bst (w1S kb ib nc r now mt sl uw) u ≠ .Ept p l m a v) ∧ + ∀ y m a, capA bst (w1S kb ib nc r now mt sl uw) u ≠ .HighVSpace y m a := by + rcases w1_capS (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) P hu with + e | ⟨i, -, -, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, -, -, -, e⟩ <;> rw [e] + all_goals first + | exact And.intro (fun _ _ _ _ _ h => by cases h) (fun _ _ _ h => by cases h) + | exact And.intro (capOf_eptA _ _) (capOf_hvA _ _) + rcases hx with ⟨_, _, h⟩ | ⟨_, _, _, h, -⟩ | ⟨_, _, h, -⟩ + · exact ce _ _ _ _ _ h + · exact ce _ _ _ _ _ h + · exact ch _ _ _ h + · obtain ⟨v, hv, -⟩ := hb + exact absurd hv (Set.notMem_empty v) + +/-- **J7 at `w1`**, complete, at the arch `w1Ar ar` with its one kernel table (`w1K_s1aStepEntry`, `w1Ar`): +no condition on the boot arch's kernel TTBR1 entries. -/ +theorem w1_J7A_full {A : Type} (ar : machine.Arch A) (P : BootLay1 kb ib nc) (n : U64) (σ : Fl4SpecA64.State Unit) + (m : Fl4SpecA64.McState) : + J7A bst (J7R2A bst (w1Ar ar) n) (w1ZA kb ib nc) (KtH (w1H kb ib nc) {bwv KTb}) (w1OS kb ib nc) + (w1S kb ib nc r now mt sl uw) σ m := by + obtain ⟨Pp, R2, sep, hik⟩ := w1K_s1aStepEntryK (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) P + (fun _ => 0) (fun _ => 0) (fun _ => 1) n + exact w1_J7A P _ σ m ⟨_, _, _, _, _, _, _, _, R2, sep, _, w1Ar_ktab ar, hik⟩ + +end boot + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3J7StepA.lean b/proofs/Fl4ProofsA64/P3/Stage3J7StepA.lean new file mode 100644 index 000000000..9641b3000 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3J7StepA.lean @@ -0,0 +1,81 @@ +/- +P3 (aarch64, A64-P3 Stage 3, T3 G1 step 2): **J7 from the fixed-witness S1a step**. + +`S1aStepFix` (`P1/S1aStepFix`) is the post of `s1aStepFix` at the decode's own `H`, `OS`, `Z.V`, `Pt`. This module turns +it into the concrete J7 slot at the post state. + +* **`J7A.of_stepFixedA`**: `J7A st R Z H OS s' σ' m` from `S1aStepFix` at `s'`, the entry's `VrOK` (kept by the + `Ub' ⊆ Ub` shrink and `ksb s' = ksb s`), the entry's aligned / VCPU-apart `Ub`, and the post-state facts the step does + not export: `PoolNamed`, the `SchedPageA ⊆ Pt` link (the per-arm `SchedPageA.back*A`), `schedApart`; the remaining slot + part `R` at `Pt` is a hypothesis. +* **`J7R2K`**: `J7R2A`'s body (equal to it since record edit #6), and **`J7R2K.of_stepFixedA`**: `J7R2K` at the post + state from `S1aStepFix` and the entry's `KTab ar Kt KLv`. **`J7R2A.toK`**: `J7R2A` gives `J7R2K`. + +Named premises: none beyond the hypotheses above (decode / post-state facts owed by the arms). +-/ +import Fl4ProofsA64.P3.Stage3J7A +import Fl4ProofsA64.P1.S1aStepFix +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- `VrOK` at a smaller user-word set. -/ +theorem vrOK_ofSubA {Ω Sg Scs : Set U64} {Z : U64 → Nat} {OS Vc Ub Ub' : Set U64} {kb : U64} {Pt : Set U64} + (h : VrOK Ω Sg Scs Z OS Vc Ub kb Pt) (hs : Ub' ⊆ Ub) : VrOK Ω Sg Scs Z OS Vc Ub' kb Pt := + ⟨h.al, h.om, h.sg, h.sc, h.os, h.vc, fun b hb hb' => h.ub b hb (hs hb'), h.ks⟩ + +/-- **J7 from the fixed-witness S1a step** (module docstring). -/ +theorem J7A.of_stepFixedA {R : Set U64 → JSlotA S} {Z : XSetsA} {H : Heap} {OS Pt Kt : Set U64} {Ub0 : Set U64} + {KLv : U64 → Nat} {ia ie : Nat} {n : U64} {s s' : S} {σ' : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (Fx : S1aStepFix st H Z.Cs OS Z.V Ub0 Pt Kt KLv ia ie n s') + (vr : VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub0 (L.ksb s) Pt) (hk : L.ksb s' = L.ksb s) + (hal : ∀ b ∈ Ub0, Aligned b) (hvw : ∀ b, VcW Z.V b → b ∉ Ub0) + (hPN : PoolNamed st OS s') (hsp : ∀ p, SchedPageA st Z OS s' p → p ∈ Pt) + (hsa : ∀ b, VcW Z.V b → ∀ o : U64, o.val < 223488 → wa (L.ksb s') o ≠ b) + (hR : R Pt Z H OS s' σ' m) : + J7A st R Z H OS s' σ' m := by + obtain ⟨Ub', ⟨B, Y, sub, vc, C, Kc, pc, hv, hg, df, hi, ep, po, ho⟩, fu, kt, r2, col⟩ := Fx + rw [← hk] at vr + exact ⟨Pt, Ub', vrOK_ofSubA vr sub, hPN, C, Kc, vc, df, hi, hsp, hsa, fu, fun b hb => hal b (sub hb), + fun b hb h => hvw b hb (sub h), hR⟩ + +end + +section +variable {S : Type} (st : machine.Store S) [L : LawfulStore st] + +/-- **`J7R2A`'s body** (module docstring). -/ +def J7R2K {A : Type} (ar : machine.Arch A) (n : U64) (Pt : Set U64) : JSlotA S := fun _ H OS s _ _ => + ∃ (Pp Tb Hr Te Kt : Set U64) (Lv Re bits : U64 → Nat), + S1aR2 st H OS Pt Pp Tb Hr Te Kt Lv Re bits n s ∧ S1aKtUt st OS Kt s ∧ + ∃ KLv : U64 → Nat, KTab ar Kt KLv ∧ S1aHiK st OS Kt KLv s + +theorem J7R2A.toK {A : Type} {ar : machine.Arch A} {n : U64} {Pt : Set U64} {Z : XSetsA} {H : Heap} {OS : Set U64} + {s : S} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} (h : J7R2A st ar n Pt Z H OS s σ m) : + J7R2K st ar n Pt Z H OS s σ m := + let ⟨Pp, Tb, Hr, Te, Kt, Lv, Re, bits, r2, ku, KLv, kt, col⟩ := h + ⟨Pp, Tb, Hr, Te, Kt, Lv, Re, bits, r2, ku, KLv, kt, col⟩ + +variable {st} + +/-- **`J7R2K` from the fixed-witness S1a step** (module docstring). -/ +theorem J7R2K.of_stepFixedA {A : Type} {ar : machine.Arch A} {Z : XSetsA} {H : Heap} {OS Pt Kt Ub0 : Set U64} + {KLv : U64 → Nat} {ia ie : Nat} {n : U64} {s' : S} {σ' : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (Fx : S1aStepFix st H Z.Cs OS Z.V Ub0 Pt Kt KLv ia ie n s') (KT : KTab ar Kt KLv) : + J7R2K st ar n Pt Z H OS s' σ' m := by + obtain ⟨Ub', -, -, kt, ⟨Pp, Tb, Hr, Te, Lv, Re, bits, r2⟩, col⟩ := Fx + exact ⟨Pp, Tb, Hr, Te, Kt, Lv, Re, bits, r2, kt, KLv, KT, col⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3KEntryActA.lean b/proofs/Fl4ProofsA64/P3/Stage3KEntryActA.lean new file mode 100644 index 000000000..96767c597 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3KEntryActA.lean @@ -0,0 +1,50 @@ +/- +P3 (aarch64, A64-P3 Stage 3, kernel-entry composition, Plan A): **the activation inputs from the record**. + +`JStage3A.finishInputsA`: at a record state, `ScheduleFinishInputsA` holds. `nonnull` is the record's `scNZ`; the +activation's `pc` (the stored `FAULT_IP` word is the saved `faultIP` register) is the record's TCB register relation +(`TcbRelE.regs`) at the current thread. + +Named premises: [P] `Fl4TicksToUsAgreeA`; [J] `legal` (the current thread's state at activation); [J] `guest` (S4: +`FinishVcpuReadyA` on `TS_RUNNING_VM`). + +New module. +-/ +import Fl4ProofsA64.P3.Stage3KEntryFinishA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4ProofsA64.P3.Endp + +noncomputable section + +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s : S} {a : A} {σ : State Unit} {m : McState} + +/-- **The activation inputs from the record** (module docstring). -/ +theorem JStage3A.finishInputsA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (ticks : Fl4TicksToUsAgreeA ar a) + (legal : ∀ x, σ.kheap σ.curThread = some (.tcb x) → ActivateStateOKA x.tcbState) + (guest : ∀ s1 σ1 y, + sched.activate_thread st ar s a = ok (s1, a) → + fl4ActivateThread (Z := Unit) σ = ({((), σ1)}, False) → + σ1.kheap σ1.curThread = some (.tcb y) → y.tcbState = .ext .runningVM → + FinishVcpuReadyA (st := st) Z m κ s1 a σ1 y ar) : + ScheduleFinishInputsA (st := st) Z m κ s a σ ar := by + have H := J.yieldQueueA + have ht := H.sched.curT + have hcur := H.sched.sch.curThread + obtain ⟨x, hx, R⟩ := J.ep.tcb (L.curW s) ht + have hk : σ.kheap σ.curThread = some (.tcb x) := by rw [hcur]; exact hx + refine ⟨J.scNZ, ticks, ⟨x, hk, legal x hk, ?_⟩, guest⟩ + have r := R.regs .faultIP + have e : regIdxA .faultIP = layout.reg.FAULT_IP := by unfold regIdxA layout.reg.FAULT_IP; rfl + rw [e] at r + exact r + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3KEntryActRecA.lean b/proofs/Fl4ProofsA64/P3/Stage3KEntryActRecA.lean new file mode 100644 index 000000000..309bfe606 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3KEntryActRecA.lean @@ -0,0 +1,840 @@ +/- +P3 (aarch64, A64-P3 Stage 3, kernel-entry composition, Plan A, item 4): **the record across `activate_thread`** +(`hExit`), and `syscall.finish` at the record with it discharged. + +* `JStage3A.activateA`: from the record at the schedule exit and `exitFactsA`'s facts there, `activate_thread` returns + and the entry record `JStage3EntryA` holds at the activated state: the system invariants by A64 P1's walk + (`activate_thread.sw`), the state relations from the exact outcome (`activate_thread_exactA`). +* `JStage3A.finishEA`: `finishFA` with `hExit` proved; left: `SchedGapA`, `BodyExitA`, `hSig` (Lift 2a), + [P] `ArRootKeepA`, `Fl4TicksToUsAgreeA`, [S4] `hNative`, [S] `numNodes = 1`. + +Named premises of `activateA`: [P] `Fl4TicksToUsAgreeA`; [S4] `hNative` (the activated thread runs no guest); +[S] `numNodes = 1`. + +New module. +-/ +import Fl4ProofsA64.P3.Stage3KEntryActSpecA +import Fl4ProofsA64.P3.Stage3KEntryFinishFA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3 + Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.OuterEntry Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +set_option maxHeartbeats 4000000 in +/-- **The entry record across `activate_thread`** (module docstring). -/ +theorem JStage3A.activateA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {s : S} {a : A} {σ : State Unit} {m : McState} + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) (h1 : fl4A64Platform.numNodes = 1) + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (facts : CurInvA σ ∧ OffOKA σ ∧ OffReadyA σ ∧ ActIdleA σ ∧ (σ.curSc ≠ idleScPtr → NotIdleScA m σ.curSc) ∧ + ValidMachineTimeA σ ∧ σ.schedulerAction = .resumeCurThread ∧ + (∀ x, σ.kheap σ.curThread = some (.tcb x) → ActivateStateOKA x.tcbState)) + (toUs : Fl4TicksToUsAgreeA ar a) + (hNative : ∀ τ, ((), τ) ∈ (fl4ActivateThread (Z := Unit) σ).1 → + ∀ y, τ.kheap τ.curThread = some (.tcb y) → y.tcbState ≠ .ext .runningVM) : + sched.activate_thread st ar s a ⦃ r => r.2 = a ∧ ∃ τ, + fl4ActivateThread (Z := Unit) σ = ({((), τ)}, False) ∧ + JStage3EntryA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) r.1 r.2 τ m ∧ + (τ.curThread ≠ τ.idleThread → ∀ y, σ.kheap σ.curThread = some (.tcb y) → y.tcbState ≠ .idleThreadState) ∧ + (∀ y, τ.kheap τ.curThread = some (.tcb y) → y.tcbState ≠ .ext .runningVM) ⦄ := by + obtain ⟨curInv, offOK, offReady, actIdle, notIdle, validTime, hres, legalAll⟩ := facts + have H3 := J.yieldQueueA + have ht := H3.sched.curT + have hcur := H3.sched.sch.curThread + let t := L.curW s + let q := σ.curThread + have hq : q = specPtr t := hcur + obtain ⟨x, hx0, R⟩ := J.ep.tcb t ht + have hx : σ.kheap q = some (.tcb x) := by rw [hq]; exact hx0 + have legal := legalAll x hx + have hcore : m.curCore < fl4A64Platform.numNodes := J.sinv.mc.curCore_lt + have hc0 : 0#u64 ∈ Z.Cs := by + obtain ⟨c, hc, e⟩ := J.coresAll 0 (Nat.lt_of_le_of_lt (Nat.zero_le _) hcore) + have : c = 0#u64 := UScalar.eq_of_val_eq (by rw [e]; rfl) + exact this ▸ hc + -- A64 P1's walk of the whole activation + have walk : sched.activate_thread st ar s a ⦃ s' _ => SW st H Z.Cs OS Z.V ia ie Ac s s' ∧ + (L.actionW s' = L.actionW s ∨ L.actionW s' = 1#u64) ⦄ := by + obtain ⟨Pt, Ub, Pp', Tb', Hr, Te, Kt, Lv', Re, bits, KLv, W⟩ := J7A.unp J.j7 + have I := J.delIn hA hPl W + exact sched.activate_thread.sw (st := st) (ar := ar) hA a I hc0 J.ko (SW.start J.sys.sysl J.symL J.blkA) + -- the spec's scheduler validity and the action + have stIdle : ∀ y, σ.kheap σ.idleThread = some (.tcb y) → y.tcbState = .idleThreadState := fun y hy => by + obtain ⟨z, hz, hs⟩ := J.sinv.idleTcb + rw [hz] at hy + obtain rfl := KernelObject.tcb.inj (Option.some.inj hy) + exact hs + have hrs : ∀ y, σ.kheap σ.curThread = some (.tcb y) → y.tcbState = .restart → TgtA σ.curThread σ := by + intro y hy hr + have ne : σ.curThread ≠ σ.idleThread := fun e => by + have := stIdle y (e ▸ hy) + rw [hr] at this + cases this + obtain ⟨⟨z, sc, hz, es, ha⟩, nr⟩ := J.schedValid.act hres ne + exact ⟨⟨z, sc, hz, es, ha⟩, nr⟩ + refine WP.spec_mono (spec_andA (activate_thread_exactA H3 a J.scNZ toUs hx legal R.regs) walk) ?_ + rintro ⟨s', a'⟩ ⟨⟨ea, τ, x2, e1, H2, hC, SP, regs2, w0, SF, W⟩, post⟩ + change SW st H Z.Cs OS Z.V ia ie Ac s s' ∧ _ at post + obtain ⟨Wk, -⟩ := post + change a' = a at ea + subst a' + change WritesIn st s s' _ at w0 W + change YieldQueueRelA (st := st) Z m κ s' τ at H2 + change ∀ rg, (L.mem s' (Endp.regA t (regIdxA rg))).bv = x2.tcbArch.tcbContext.userRegs rg at regs2 + have mem : ((), τ) ∈ (fl4ActivateThread (Z := Unit) σ).1 := by rw [e1]; exact Set.mem_singleton _ + have fa := activate_act σ hres hrs σ ⟨rfl, SchedValidA.toFIA J.schedValid⟩ ((), τ) mem + have act : τ.schedulerAction = .resumeCurThread := fa.2 + have HS : SpecSchedA σ τ := SF.1 + have SSz : SSzA σ τ := SF.2.1 + have hum := SF.2.2 + have eτ : τ = { σ with kheap := τ.kheap } := by + conv_lhs => rw [SP.rest] + rw [act, ← hres] + have fcur : τ.curThread = σ.curThread := hC + have fidle : τ.idleThread = σ.idleThread := by + have h := congrArg (fun z : State Unit => z.idleThread) eτ + exact h + have frq : τ.readyQueues = σ.readyQueues := by + have h := congrArg (fun z : State Unit => z.readyQueues) eτ + exact h + have frel : τ.releaseQueue = σ.releaseQueue := by + have h := congrArg (fun z : State Unit => z.releaseQueue) eτ + exact h + have fsc : τ.curSc = σ.curSc := by + have h := congrArg (fun z : State Unit => z.curSc) eτ + exact h + have fct : τ.curTime = σ.curTime := by + have h := congrArg (fun z : State Unit => z.curTime) eτ + exact h + have fco : τ.consumedTime = σ.consumedTime := by + have h := congrArg (fun z : State Unit => z.consumedTime) eτ + exact h + have fms : τ.machineState = σ.machineState := by + have h := congrArg (fun z : State Unit => z.machineState) eτ + exact h + -- the current TCB's fields + obtain ⟨f, ex2⟩ := SP.tcb + have x2state : x2.tcbState = activatedStateA x.tcbState := by rw [ex2] + have x2sc : x2.tcbSchedContext = x.tcbSchedContext := by rw [ex2] + have x2fault : x2.tcbFault = x.tcbFault := by rw [ex2] + have x2bound : x2.tcbBoundNotification = x.tcbBoundNotification := by rw [ex2] + have x2prio : x2.tcbPriority = x.tcbPriority := by rw [ex2] + have x2dom : x2.tcbDomain = x.tcbDomain := by rw [ex2] + have x2frame : x2.tcbIpcframe = x.tcbIpcframe := by rw [ex2] + have x2buf : x2.tcbIpcBuffer = x.tcbIpcBuffer := by rw [ex2] + have x2mcp : x2.tcbMcpriority = x.tcbMcpriority := by rw [ex2] + have x2st : x2.tcbState = .running ∨ x2.tcbState = .idleThreadState := by + have nv : x2.tcbState ≠ .ext .runningVM := + hNative τ mem x2 (by rw [fcur]; exact SP.at1) + rw [x2state] at nv ⊢ + rcases legal with h | h | h | h <;> rw [h] at nv ⊢ + · exact Or.inl rfl + · exact Or.inl rfl + · exact Or.inr rfl + · exact (nv rfl).elim + have xNB : epBlocked x.tcbState = none ∧ (∀ p, x.tcbState ≠ .blockedOnNotification p) := by + rcases legal with h | h | h | h <;> rw [h] <;> exact ⟨rfl, fun _ h => by cases h⟩ + have x2NB : epBlocked x2.tcbState = none ∧ (∀ p, x2.tcbState ≠ .blockedOnNotification p) ∧ + (∀ e d, x2.tcbState ≠ .blockedOnSend e d) ∧ (∀ e r d, x2.tcbState ≠ .blockedOnReceive e r d) ∧ + (∀ r, x2.tcbState ≠ .blockedOnReply r) ∧ ipcQueuedThreadState x2.tcbState = false := by + rcases x2st with h | h <;> rw [h] <;> + exact ⟨rfl, (fun _ h => by cases h), (fun _ _ h => by cases h), (fun _ _ _ h => by cases h), + (fun _ h => by cases h), rfl⟩ + have x2idle : x.tcbState = .idleThreadState → x2.tcbState = .idleThreadState := fun h => by + rw [x2state, h]; rfl + have x2idle' : x2.tcbState = .idleThreadState → x.tcbState = .idleThreadState := fun h => by + rw [x2state] at h + revert h + cases x.tcbState <;> intro h <;> first | rfl | cases h + have x2run : runnable x.tcbState = true → runnable x2.tcbState = true := fun h => by + rcases x2st with e | e + · rw [e]; rfl + · rw [x2idle' e] at h; cases h + -- the heap + have tcbF : ∀ p z, σ.kheap p = some (.tcb z) → ∃ z', τ.kheap p = some (.tcb z') ∧ + ((p = q ∧ z = x ∧ z' = x2) ∨ (p ≠ q ∧ z' = z)) := fun p z h => by + by_cases e : p = q + · subst e + rw [hx] at h + exact ⟨x2, SP.at1, Or.inl ⟨rfl, (KernelObject.tcb.inj (Option.some.inj h)).symm, rfl⟩⟩ + rcases SP.other p e with o | ⟨y, n, c, h0, -⟩ + · exact ⟨z, o.trans h, Or.inr ⟨e, rfl⟩⟩ + · rw [h0] at h; cases h + have tcbB : ∀ p z', τ.kheap p = some (.tcb z') → + (p = q ∧ z' = x2) ∨ (p ≠ q ∧ σ.kheap p = some (.tcb z')) := fun p z' h => by + by_cases e : p = q + · subst e + rw [SP.at1] at h + exact Or.inl ⟨rfl, (KernelObject.tcb.inj (Option.some.inj h)).symm⟩ + rcases SP.other p e with o | ⟨y, n, c, -, h1'⟩ + · exact Or.inr ⟨e, o ▸ h⟩ + · rw [h1'] at h; cases h + have objF : ∀ p o, σ.kheap p = some o → (∀ z, o ≠ .tcb z) → (∀ y n, o ≠ .schedContext y n) → + τ.kheap p = some o := fun p o h nt ns => by + have e : p ≠ q := fun e => by rw [e, hx] at h; exact nt x (Option.some.inj h).symm + rcases SP.other p e with o' | ⟨y, n, c, h0, -⟩ + · exact o'.trans h + · rw [h0] at h; exact absurd (Option.some.inj h).symm (ns y n) + have objB : ∀ p o, τ.kheap p = some o → (∀ z, o ≠ .tcb z) → (∀ y n, o ≠ .schedContext y n) → + σ.kheap p = some o := fun p o h nt ns => by + have e : p ≠ q := fun e => by rw [e, SP.at1] at h; exact nt x2 (Option.some.inj h).symm + rcases SP.other p e with o' | ⟨y, n, c, -, h1'⟩ + · exact o' ▸ h + · rw [h1'] at h; exact absurd (Option.some.inj h).symm (ns _ n) + have scF : ∀ p y n, σ.kheap p = some (.schedContext y n) → ∃ y', τ.kheap p = some (.schedContext y' n) ∧ + (∃ c yf, y' = { y with scConsumed := c, scYieldFrom := yf }) := fun p y n h => by + have e : p ≠ q := fun e => by rw [e, hx] at h; cases h + rcases SP.other p e with o | ⟨y0, n0, c, h0, h1'⟩ + · exact ⟨y, o.trans h, y.scConsumed, y.scYieldFrom, rfl⟩ + · rw [h0] at h + obtain ⟨ey, en⟩ := KernelObject.schedContext.inj (Option.some.inj h) + subst ey en + exact ⟨_, h1', c, none, rfl⟩ + have scB : ∀ p y' n, τ.kheap p = some (.schedContext y' n) → ∃ y, σ.kheap p = some (.schedContext y n) ∧ + (∃ c yf, y' = { y with scConsumed := c, scYieldFrom := yf }) := fun p y' n h => by + have e : p ≠ q := fun e => by rw [e, SP.at1] at h; cases h + rcases SP.other p e with o | ⟨y0, n0, c, h0, h1'⟩ + · exact ⟨y', o ▸ h, y'.scConsumed, y'.scYieldFrom, rfl⟩ + · rw [h1'] at h + obtain ⟨ey, en⟩ := KernelObject.schedContext.inj (Option.some.inj h) + subst ey en + exact ⟨y0, h0, c, none, rfl⟩ + -- the memory + have N1 := J.sr.toSchedSRN + have htA := N1.core.tcbs t ht + have PE := J.iplaced.e + have mk : ∀ b, Aligned b → ¬ MWrA (st := st) Z m σ s b → ¬ ActWordA t b → L.mem s' b = L.mem s b := + fun b hb h1' h2 => w0.mem_eq hb fun h => h.elim h1' h2 + have actT : ∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → + (u ≠ t ∨ (K.val ≠ 256 ∧ K.val ≠ 264 ∧ K.val ≠ 272 ∧ K.val ≠ 3472 ∧ K.val ≠ 512 ∧ K.val ≠ 3328)) → + ¬ ActWordA t (wa u K) := by + intro u hu K hK h h' + have huA := N1.core.tcbs u hu + have ne : ∀ K' : U64, K'.val < 4096 → (K.val ≠ K'.val ∨ u ≠ t) → wa u K ≠ wa t K' := by + intro K' hK' hh + refine field_ne huA htA hK hK' fun e => ?_ + exact hh.resolve_right (fun c => c e) + have go : ∀ K' : U64, K'.val < 4096 → (K.val = K'.val → u ≠ t) → wa u K ≠ wa t K' := fun K' hK' hh => + ne K' hK' (by by_cases c : K.val = K'.val; exacts [Or.inr (hh c), Or.inl c]) + have side : ∀ v : Nat, (K.val ≠ 256 ∧ K.val ≠ 264 ∧ K.val ≠ 272 ∧ K.val ≠ 3472 ∧ K.val ≠ 512 ∧ + K.val ≠ 3328 → K.val ≠ v) → K.val = v → u ≠ t := fun v hv e => + h.resolve_right fun c => hv c e + have h3472 : (3472#u64 : U64).val = 3472 := by decide + rcases h' with e | e | e | e | e | e + · exact go 256#u64 (by decide) (side _ (fun c => by simpa using c.1)) e + · exact go 264#u64 (by decide) (side _ (fun c => by simpa using c.2.1)) e + · exact go 272#u64 (by decide) (side _ (fun c => by simpa using c.2.2.1)) e + · change wa u K = wa t 3472#u64 at e + exact go 3472#u64 (by decide) (side _ (fun c => by rw [h3472]; exact c.2.2.2.1)) e + · exact go 512#u64 (by decide) (side _ (fun c => by simpa using c.2.2.2.2.1)) e + · rw [stA_eq] at e + exact go 3328#u64 (by decide) (side _ (fun c => by simpa using c.2.2.2.2.2)) e + have kT : ∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → + (K.val ≠ 3464 ∧ K.val ≠ 3480 ∧ K.val ≠ 3488 ∧ K.val ≠ 3592) → + (u ≠ t ∨ (K.val ≠ 256 ∧ K.val ≠ 264 ∧ K.val ≠ 272 ∧ K.val ≠ 3472 ∧ K.val ≠ 512 ∧ K.val ≠ 3328)) → + L.mem s' (wa u K) = L.mem s (wa u K) := fun u hu K h1' h2 h3 h4 => + mk _ (field_aligned (N1.core.tcbs u hu) h1' h2) (tcbK_offMWrA N1 hu h1' h3) (actT u hu K h1' h4) + -- a word outside every TCB of `T` is no activation word + have actOff : ∀ b : U64, (b.val < t.val ∨ t.val + 4096 ≤ b.val) → ¬ ActWordA t b := by + intro b hb h' + have v : ∀ K : U64, K.val < 4096 → (wa t K).val = t.val + K.val := fun K hK => field_val htA hK + have go : ∀ K : U64, K.val < 4096 → b ≠ wa t K := fun K hK e => by + have := congrArg (fun w : U64 => w.val) e + simp only [v K hK] at this + omega + rcases h' with e | e | e | e | e | e + · exact go _ (by decide) e + · exact go _ (by decide) e + · exact go _ (by decide) e + · exact go 3472#u64 (by decide) e + · exact go _ (by decide) e + · rw [stA_eq] at e; exact go _ (by decide) e + have w : WritesIn st s s' (SchedStepWA (st := st) Z m σ s) := W + have wN : WritesIn st s s' (SchedStepNWA (st := st) N E Rp Z m σ s) := w.mono fun _ h => Or.inl h + have kb : L.ksb s' = L.ksb s := w.1 + have sq : SchedQueuesInv st H.tcbs Z.Cs H.omega H s' Z.Q Z.R := by + obtain ⟨Q0, R0, h⟩ := Wk.1.blk.1 + exact SchedQueuesInv.reQA h (by rw [J.tcbs]; exact H2.sched.core) + have poolOff : PoolOff st H Z.Cs OS s' := fun i => by + have h0 := J.poolOff i + rw [vspace.asid_table.eq] at h0 ⊢ + have v := AtOff.v i + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.sys.ent.ksw + have ev : (wa (L.ksb s) (AtOff i)).val = (L.ksb s).val + (AtOff i).val := by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have e : L.mem s' (wa (L.ksb s') (AtOff i)) = L.mem s (wa (L.ksb s) (AtOff i)) := by + rw [kb] + refine wN.mem_eq ?_ (J.asid_offStepNA (by omega) (by omega)) + have h32 := J.j.1.ksbAl + show (wa (L.ksb s) (AtOff i)).val % 8 = 0 + omega + rw [e] + refine (WP.spec_ok _).2 fun hne asid => ?_ + have q' := (WP.spec_ok _).1 h0 hne asid + exact ⟨q'.al, q'.om, q'.sg, q'.os, RegionOffNodes.ksb kb q'.ks⟩ + -- endpoints + let σc : State Unit := { σ with kheap := fun p => if p = q then σ.kheap p else τ.kheap p } + have cq : σc.kheap q = σ.kheap q := if_pos rfl + have cne : ∀ p, p ≠ q → σc.kheap p = τ.kheap p := fun p h => if_neg h + have cT : ∀ p z, σ.kheap p = some (.tcb z) → σc.kheap p = some (.tcb z) := fun p z h => by + by_cases e : p = q + · rw [e] at h ⊢; rw [cq]; exact h + obtain ⟨z', hz, c⟩ := tcbF p z h + rcases c with ⟨c, -⟩ | ⟨-, c⟩ + · exact absurd c e + · rw [cne p e, hz, c] + have cO : ∀ p o, σ.kheap p = some o → (∀ z, o ≠ .tcb z) → (∀ y n, o ≠ .schedContext y n) → + σc.kheap p = some o := fun p o h nt ns => by + have e : p ≠ q := fun e => by rw [e, hx] at h; exact nt x (Option.some.inj h).symm + rw [cne p e]; exact objF p o h nt ns + have eτc : τ = { σc with kheap := Function.update σc.kheap q (some (.tcb x2)) } := by + have : Function.update σc.kheap q (some (.tcb x2)) = τ.kheap := by + funext p + by_cases e : p = q + · rw [e, Function.update_self, SP.at1] + · rw [Function.update_of_ne e, cne p e] + conv_lhs => rw [eτ] + simp only [σc] + rw [show (Function.update (fun p => if p = q then σ.kheap p else τ.kheap p) q (some (.tcb x2))) = τ.kheap from this] + have epc : EpStateRel Z.T E Rp Bf (L.mem s) σc := by + refine J.ep.frame PE (fun _ _ _ => rfl) ⟨fun p z h => cO p _ h (by intro _ c; cases c) (by intro _ _ c; cases c), + fun p z h => ⟨z, cO p _ h (by intro _ c; cases c) (by intro _ _ c; cases c), rfl⟩, + fun p z h => ⟨z, cT p z h, rfl, rfl, rfl, rfl, rfl⟩, rfl⟩ + have eqT : ∀ y, y ∈ Z.T → specPtr y = q → y = t := fun y _ h => specPtr_inj (h.trans hq) + have epq : ∀ e ∈ E, ∀ z, σc.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue := by + intro e he z hz hm + obtain ⟨z0, hz0, hr⟩ := J.ep.ep e he + have := cO _ _ hz0 (by intro _ c; cases c) (by intro _ _ c; cases c) + rw [this] at hz + obtain rfl := KernelObject.endpoint.inj (Option.some.inj hz) + have code := R.st + have nb : tsCode x.tcbState ≠ 3 ∧ tsCode x.tcbState ≠ 4 := by + rcases legal with h | h | h | h <;> rw [h] <;> exact ⟨by decide, by decide⟩ + cases z0 with + | idleEP => exact absurd hm (by simp [Endpoint.epQueue]) + | sendEP l => + obtain ⟨-, xs, -, el, -, hall⟩ := hr + have hm' : specPtr t ∈ xs.map specPtr := el ▸ hm + have := (hall t (mem_map_specPtrSc.1 hm')).1 + exact nb.2 (code.symm.trans this) + | recvEP l => + obtain ⟨-, xs, -, el, -, hall⟩ := hr + have hm' : specPtr t ∈ xs.map specPtr := el ▸ hm + have := (hall t (mem_map_specPtrSc.1 hm')).1 + exact nb.1 (code.symm.trans this) + have efOff : ∀ b, Aligned b → EFoot Z.T E Rp Bf b → ¬ TF t b → L.mem s' b = L.mem s b := by + intro b hb hf ntf + refine mk b hb (endpFootOffWrite N1 J.ipcPlace hf) fun h' => ?_ + have v : ∀ K : U64, K.val < 4096 → (wa t K).val = t.val + K.val := fun K hK => field_val htA hK + have tf : ∀ K : U64, K.val < 4096 → TOffN K.val → b = wa t K → False := fun K hK hn e => by + refine ntf ⟨?_, ?_⟩ + · rw [e, v K hK]; omega + · rw [e, v K hK, Nat.add_sub_cancel_left]; exact hn + rcases h' with e | e | e | e | e | e + · exact tf 256#u64 (by decide) (by unfold TOffN; simp) e + · exact tf 264#u64 (by decide) (by unfold TOffN; simp) e + · exact tf 272#u64 (by decide) (by unfold TOffN; simp) e + · change b = wa t 3472#u64 at e + have tv : t.val = (L.curW s).val := rfl + have bv : b.val = t.val + 3472 := by + rw [e, v _ (by decide)]; rfl + rcases hf with ⟨e', he, h⟩ | ⟨r, hr, h⟩ | ⟨u, hu, h⟩ | h + · have := PE.et e' he t ht; omega + · have := PE.rt r hr t ht; omega + · by_cases eu : u = t + · subst eu + rcases h with h | h + · have := h.2; rw [bv, Nat.add_sub_cancel_left] at this; unfold TOffN at this; omega + · unfold TL at h; omega + · have d : u.val + 4096 ≤ t.val ∨ t.val + 4096 ≤ u.val := by + have := tcb_disj PE hu ht (Ne.symm eu) + omega + rcases h with h | h + · have := TF.range h; have := h.1; omega + · unfold TL at h; omega + · have := PE.bt b h t ht; omega + · exact tf 512#u64 (by decide) (by unfold TOffN; simp) e + · rw [stA_eq] at e + exact tf 3328#u64 (by decide) (by unfold TOffN; simp) e + have stK : (L.mem s' (stA t)).val = tsCode x2.tcbState := by + have h := H2.states t ht x2 (by rw [← hq]; exact SP.at1) + rw [h] + rcases x2st with e | e <;> rw [e] <;> rfl + have kf : ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → + (K.val ≠ 3464 ∧ K.val ≠ 3480 ∧ K.val ≠ 3488 ∧ K.val ≠ 3592) → + (K.val ≠ 256 ∧ K.val ≠ 264 ∧ K.val ≠ 272 ∧ K.val ≠ 3472 ∧ K.val ≠ 512 ∧ K.val ≠ 3328) → + L.mem s' (wa t K) = L.mem s (wa t K) := fun K a1 a2 a3 a4 => kT t ht K a1 a2 a3 (Or.inr a4) + have prioK : L.mem s' (prioA t) = L.mem s (prioA t) := by + rw [prioA_eq']; exact kf _ (by simp) (by simp) (by simp) (by simp) + have blkK : ∀ u ∈ Z.T, L.mem s' (Notif.tBlockA u) = L.mem s (Notif.tBlockA u) := fun u hu => by + rw [Notif.tBlockA_eq] + exact kT u hu _ (by simp) (by simp) (by simp) (Or.inr (by simp)) + have rel : TcbRelE Rp (L.mem s') t x2 := by + have w3 : ∀ K : U64, (K = 3368#u64 ∨ K = 3376#u64 ∨ K = 3384#u64 ∨ K = 3392#u64 ∨ K = 3400#u64 ∨ + K = 3408#u64 ∨ K = 3416#u64 ∨ K = 3424#u64) → L.mem s' (wa t K) = L.mem s (wa t K) := fun K h => by + rcases h with rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> + exact kf _ (by decide) (by decide) (by decide) (by decide) + have bound : L.mem s' (tBoundA t) = L.mem s (tBoundA t) := w3 _ (Or.inl rfl) + have fl : L.mem s' (tFaultA t) = L.mem s (tFaultA t) := w3 _ (Or.inr (Or.inl rfl)) + have f0 : L.mem s' (tFw0A t) = L.mem s (tFw0A t) := w3 _ (Or.inr (Or.inr (Or.inl rfl))) + have f1 : L.mem s' (tFw1A t) = L.mem s (tFw1A t) := w3 _ (Or.inr (Or.inr (Or.inr (Or.inl rfl)))) + have f2 : L.mem s' (tFw2A t) = L.mem s (tFw2A t) := w3 _ (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl rfl))))) + have l0 : L.mem s' (tLfA t) = L.mem s (tLfA t) := w3 _ (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl rfl)))))) + have l1 : L.mem s' (tLf0A t) = L.mem s (tLf0A t) := + w3 _ (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl rfl))))))) + have l2 : L.mem s' (tLf1A t) = L.mem s (tLf1A t) := + w3 _ (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr rfl))))))) + exact + { st := stK + blk := fun e h => by rw [x2NB.1] at h; cases h + send := fun e d h => absurd h (x2NB.2.2.1 e d) + recv := fun e r d h => absurd h (x2NB.2.2.2.1 e r d) + reply := fun r h => absurd h (x2NB.2.2.2.2.1 r) + regs := regs2 + prio := by rw [prioK, x2prio]; exact R.prio + fault0 := by rw [fl, x2fault]; exact R.fault0 + faultT := by rw [fl, x2fault]; exact R.faultT + bound := by rw [bound, x2bound]; exact R.bound + faultR := by + have := R.faultR + unfold FaultRel at this ⊢ + rw [fl, f0, f1, f2, l0, l1, l2, x2fault] + exact this } + have ep : EpStateRel Z.T E Rp Bf (L.mem s') τ := by + have h := epc.upd_tcb PE ht x2 (m' := L.mem s') (fun b hb h => efOff b hb h.1 h.2) (Or.inl epq) rel + rw [eτc] + rw [hq] at * + exact h + -- notifications + have nt0 : NtfnStateRel N Z.T (L.mem s) σc := by + refine J.ntfn.frame J.iplaced.n (fun _ _ _ => rfl) + ⟨fun p z h => cO p _ h (by intro _ c; cases c) (by intro _ _ c; cases c), + fun p z h => ⟨z, cT p z h, rfl, rfl, rfl⟩⟩ + have PN := J.iplaced.n + have nfK : ∀ b, Aligned b → Notif.NFoot N Z.T b → ¬ ActWordA t b → L.mem s' b = L.mem s b := fun b hb hf ha => + mk b hb (nfoot_offMWrA N1 J.ipcPlace.notification hf) ha + have ntq : ∀ n' ∈ N, ∀ z, σc.kheap (specPtr n') = some (.notification z) → specPtr t ∉ z.ntfnObj.ntfnQueue := by + intro n' hn z hz hm + obtain ⟨z0, hz0, hr⟩ := J.ntfn.ntfn n' hn + have := cO _ _ hz0 (by intro _ c; cases c) (by intro _ _ c; cases c) + rw [this] at hz + obtain rfl := KernelObject.notification.inj (Option.some.inj hz) + have hobj := hr.obj + have code := R.st + have nb : tsCode x.tcbState ≠ 6 := by + rcases legal with h | h | h | h <;> rw [h] <;> decide + revert hm hobj + cases z0.ntfnObj with + | idleNtfn => intro hm _; exact absurd hm (by simp [Ntfn.ntfnQueue]) + | activeNtfn _ => intro hm _; exact absurd hm (by simp [Ntfn.ntfnQueue]) + | waitingNtfn l => + intro hm hobj + obtain ⟨-, xs, -, el, -, hall⟩ := hobj + have hm' : specPtr t ∈ xs.map specPtr := el ▸ hm + have := (hall t (mem_map_specPtrSc.1 hm')).1 + exact nb (code.symm.trans this) + have ntfn : NtfnStateRel N Z.T (L.mem s') τ := by + have hN : ∀ n' ∈ N, NWordsEq (L.mem s) (L.mem s') n' := fun n' hn => by + have k : ∀ b, Aligned b → Notif.NFoot N Z.T b → (n'.val ≤ b.val ∧ b.val < n'.val + 40) → + L.mem s' b = L.mem s b := fun b hb hf hr => by + refine nfK b hb hf (actOff b ?_) + have := PN.disj n' hn t ht + omega + have go : ∀ b, Aligned b ∧ Notif.NFoot N Z.T b → (n'.val ≤ b.val ∧ b.val < n'.val + 40) → + L.mem s' b = L.mem s b := fun b h r => k b h.1 h.2 r + have ha := PN.ntfn n' hn + have v : ∀ K : U64, K.val < 40 → (wa n' K).val = n'.val + K.val := fun K h => + nfield_val ha (by omega : K.val < 64) + have e0 : (epHeadA n').val = n'.val + 0 := by rw [Notif.epHeadA_eq, v 0#u64 (by decide)]; rfl + have e1 : (epTSA n').val = n'.val + 8 := by rw [Notif.epTSA_eq, v 8#u64 (by decide)]; rfl + have e2 : (nBadgeA n').val = n'.val + 16 := by rw [nBadgeA_eq, v 16#u64 (by decide)]; rfl + have e3 : (nBoundA n').val = n'.val + 24 := by rw [nBoundA_eq, v 24#u64 (by decide)]; rfl + have e4 : (nScA n').val = n'.val + 32 := by rw [nScA_eq, v 32#u64 (by decide)]; rfl + exact ⟨go _ (foot_head PN hn) (by omega), go _ (foot_ts PN hn) (by omega), go _ (foot_badge PN hn) (by omega), + go _ (foot_bound PN hn) (by omega), go _ (foot_sc PN hn) (by omega)⟩ + have hT : ∀ y ∈ Z.T, y ≠ t → TFieldsEq (L.mem s) (L.mem s') y := fun y hy hne => by + have o : ∀ K : U64, K.val < 4096 → ¬ ActWordA t (wa y K) := fun K hK => actT y hy K hK (Or.inl hne) + refine ⟨nfK _ (foot_st PN hy).1 (foot_st PN hy).2 ?_, nfK _ (foot_blk PN hy).1 (foot_blk PN hy).2 ?_, + nfK _ (foot_breg PN hy).1 (foot_breg PN hy).2 ?_, nfK _ (foot_prio PN hy).1 (foot_prio PN hy).2 ?_⟩ + · rw [stA_eq]; exact o _ (by simp) + · rw [Notif.tBlockA_eq]; exact o _ (by simp) + · rw [Notif.badgeRegA_eq]; exact o _ (by simp) + · rw [prioA_eq']; exact o _ (by simp) + have hL : ∀ y ∈ Z.T, TLinksEq (L.mem s) (L.mem s') y := fun y hy => by + refine ⟨nfK _ (foot_prev PN hy).1 (foot_prev PN hy).2 ?_, nfK _ (foot_next PN hy).1 (foot_next PN hy).2 ?_⟩ + · rw [Notif.prevEp_eq]; exact actT y hy _ (by simp) (Or.inr (by simp)) + · rw [Notif.nextEp_eq]; exact actT y hy _ (by simp) (Or.inr (by simp)) + have rel1 : TcbRel1 (L.mem s') t x2 := by + refine ⟨stK, fun n' h => absurd h (x2NB.2.1 n'), ?_, ?_⟩ + · rw [badgeRegA_eq_regA]; exact regs2 badge_register + · rw [prioK, x2prio]; exact R.prio + have h := nt0.upd_tcb ht x2 (m' := L.mem s') hN hT hL (Or.inl ntq) rel1 + rw [eτc] + rw [hq] at * + exact h + -- replies + have reply : ReplyStackRelA (L.mem s') Rp Z.C τ (scNameA m) stack := by + refine J.reply.frame (fun r hr => ?_) (fun c _ y n h => ?_) (fun r hr y h => ?_) + · obtain ⟨r32, -, rfit⟩ := PE.rep r hr + have one : ∀ o : U64, o.val < 32 → o.val % 8 = 0 → L.mem s' (wa r o) = L.mem s (wa r o) := by + intro o o1 o2 + have v : (wa r o).val = r.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + refine mk _ ?_ (other_offMWrA N1 (J.ipcPlace.reply r hr) (by omega) (by omega)) (actOff _ ?_) + · show (wa r o).val % 8 = 0 + omega + · have := PE.rt r hr t ht + omega + have n16 : (layout.reply.NEXT).val = 16 := by unfold layout.reply.NEXT; rfl + have p8 : (layout.reply.PREV).val = 8 := by unfold layout.reply.PREV; rfl + exact ⟨one _ (by omega) (by omega), one _ (by omega) (by omega)⟩ + · obtain ⟨y', hy', c', yf, e⟩ := scF _ _ _ h + exact ⟨y', n, hy', by rw [e]⟩ + · exact ⟨y, objB _ _ h (by intro _ c; cases c) (by intro _ _ c; cases c), rfl⟩ + have bufCover : BufCover st Z.T Bf s' τ := by + refine J.bufCover_wA w0 (fun u hu K hK h => ?_) (fun p z h => ?_) + · have hK1 : K.val < 4096 := by omega + have off : K.val ≠ 3464 ∧ K.val ≠ 3480 ∧ K.val ≠ 3488 ∧ K.val ≠ 3592 := by omega + exact h.elim (tcbK_offMWrA N1 hu hK1 off) (actT u hu K hK1 (Or.inr (by omega))) + · obtain ⟨z', hz, c⟩ := tcbF p z h + rcases c with ⟨-, ez, ez'⟩ | ⟨-, ez'⟩ + · exact ⟨z', hz, by rw [ez', ez, x2frame], by rw [ez', ez, x2buf]⟩ + · exact ⟨z', hz, by rw [ez'], by rw [ez']⟩ + have bufAl : UtBufAlA Z.T τ := fun u hu y hy => by + rcases tcbB _ _ hy with ⟨e, ey⟩ | ⟨-, h⟩ + · rw [ey, x2buf] + exact J.bufAl u hu x (by rw [e]; exact hx) + · exact J.bufAl u hu y h + have idleNF : IdleNoFaultA m τ := fun c hc => by + obtain ⟨z, hz, hf⟩ := J.idleNoFault c hc + obtain ⟨z', hz', k⟩ := tcbF _ _ hz + refine ⟨z', hz', ?_⟩ + rcases k with ⟨-, ez, ez'⟩ | ⟨-, ez'⟩ + · rw [ez', x2fault, ← ez]; exact hf + · rw [ez']; exact hf + have scCov : ScCovA Z.C m τ := fun p y n h => by + obtain ⟨y0, h0⟩ := SSz p y n h + exact J.scCov p y0 n h0 + have f3 : ∀ c < fl4A64Platform.numNodes, ∃ z, τ.kheap (m.idleThreadOf c) = some (.tcb z) ∧ + z.tcbState = .idleThreadState := fun c hc => by + obtain ⟨z, hz, hs⟩ := J.sidle.f3 c hc + obtain ⟨z', hz', k⟩ := tcbF _ _ hz + refine ⟨z', hz', ?_⟩ + rcases k with ⟨-, ez, ez'⟩ | ⟨-, ez'⟩ + · rw [ez']; exact x2idle (ez ▸ hs) + · rw [ez']; exact hs + have idleVt : ∀ c < fl4A64Platform.numNodes, + vtCapA τ (m.idleThreadOf c) = some .nullCap ∧ vcpuOfA τ (m.idleThreadOf c) = none := fun c hc => by + obtain ⟨z, hz, -⟩ := J.sidle.f3 c hc + obtain ⟨z', hz', k⟩ := tcbF _ _ hz + have ev : z'.tcbVtable = z.tcbVtable ∧ z'.tcbArch.tcbVcpu = z.tcbArch.tcbVcpu := by + rcases k with ⟨-, ez, ez'⟩ | ⟨-, ez'⟩ + · exact ⟨by rw [ez', ez, ex2], by rw [ez', ez, ex2]; rfl⟩ + · exact ⟨by rw [ez'], by rw [ez']⟩ + exact idleVtKeepA hz hz' ev.1 ev.2 (J.sidle.idleSlot c hc) (J.sidle.idleBound c hc) + have sidle : SIdleA τ m fl4A64Platform.numNodes := by + refine ⟨f3, fun y n hy => ?_, fun p y n hy he => ?_, J.sidle.f6.of_eq HS.specCapAt, fun y n hy => ?_, + fun c hc => (idleVt c hc).1, fun c hc => (idleVt c hc).2⟩ + · obtain ⟨y0, h0, c, yf, e⟩ := scB _ _ _ hy + rw [e]; exact J.sidle.f4 y0 n h0 + · obtain ⟨y0, h0, c, yf, e⟩ := scB _ _ _ hy + exact J.sidle.f5 p y0 n h0 (by rw [fidle] at he; rw [e] at he; exact he) + · obtain ⟨y0, h0, c, yf, e⟩ := scB _ _ _ hy + rw [e, fct]; exact J.sidle.f7 y0 n h0 + have idleT : ∀ z, σ.kheap σ.idleThread = some (.tcb z) → ∃ z', τ.kheap σ.idleThread = some (.tcb z') ∧ + z'.tcbSchedContext = z.tcbSchedContext ∧ (z.tcbState = .idleThreadState → z'.tcbState = .idleThreadState) := by + intro z hz + obtain ⟨z', hz', k⟩ := tcbF _ _ hz + refine ⟨z', hz', ?_⟩ + rcases k with ⟨-, ez, ez'⟩ | ⟨-, ez'⟩ + · rw [ez', ez]; exact ⟨x2sc, x2idle⟩ + · rw [ez']; exact ⟨rfl, id⟩ + have capB : ∀ p o b c, τ.kheap p = some o → capOf o b = some c → + ∃ o0, σ.kheap p = some o0 ∧ capOf o0 b = some c := by + intro p o b c ho hc + cases o with + | cnode k tb => + exact ⟨_, objB p _ ho (by intro _ e; cases e) (by intro _ _ e; cases e), hc⟩ + | tcb z => + rcases tcbB p z ho with ⟨e, ez⟩ | ⟨-, h⟩ + · refine ⟨.tcb x, by rw [e]; exact hx, ?_⟩ + rw [← hc, ez, ex2] + rfl + · exact ⟨_, h, hc⟩ + | _ => cases hc + have sinv : SInvXA τ m fl4A64Platform.numNodes := by + refine + { mc := J.sinv.mc + view := ?_ + curTcb := ⟨x2, by rw [fcur]; exact SP.at1⟩ + idleTcb := ?_ + onlyIdle := fun p z h hs => ?_ + idleSc := ?_ + curSc := ?_ + ctQ := fun _ => by rw [frq, fcur]; exact J.sinv.ctQ hres + idleQ := by rw [frq]; exact J.sinv.idleQ + rq := ?_ + rls := ?_ + rpos := ?_ + irqN := ?_ + zc := ?_ + rlsRun := ?_ + curScReady := ?_ + faultSt := ?_ } + · show τ.idleThread = m.idleThreadOf m.curCore + rw [fidle]; exact J.sinv.view + · obtain ⟨z, hz, hs⟩ := J.sinv.idleTcb + obtain ⟨z', hz', -, k⟩ := idleT z hz + exact ⟨z', by rw [fidle]; exact hz', k hs⟩ + · rcases tcbB p z h with ⟨e, ez⟩ | ⟨-, h0⟩ + · rw [e] + exact J.sinv.onlyIdle q x hx (x2idle' (ez ▸ hs)) + · exact J.sinv.onlyIdle p z h0 hs + · obtain ⟨z, y, n, hz, es, hy, ht'⟩ := J.sinv.idleSc + obtain ⟨z', hz', e1', -⟩ := idleT z hz + obtain ⟨y', hy', c, yf, ey⟩ := scF _ _ _ hy + exact ⟨z', y', n, by rw [fidle]; exact hz', e1'.trans es, hy', by rw [ey, fidle]; exact ht'⟩ + · obtain ⟨y, n, hy⟩ := J.sinv.curSc + obtain ⟨y', hy', -⟩ := scF _ _ _ hy + exact ⟨y', n, by rw [fsc]; exact hy'⟩ + · intro d p u hm + rw [frq] at hm + obtain ⟨z, hz, hr⟩ := J.sinv.rq d p u hm + obtain ⟨z', hz', k⟩ := tcbF _ _ hz + refine ⟨z', hz', ?_⟩ + rcases k with ⟨-, ez, ez'⟩ | ⟨-, ez'⟩ + · rw [ez']; exact x2run (ez ▸ hr) + · rw [ez']; exact hr + · intro u hm + rw [frel] at hm + obtain ⟨z, hz⟩ := J.sinv.rls u hm + obtain ⟨z', hz', -⟩ := tcbF _ _ hz + exact ⟨z', hz'⟩ + · intro p y n h act' + obtain ⟨y0, h0, c, yf, e⟩ := scB _ _ _ h + rw [e] at act' ⊢ + exact J.sinv.rpos p y0 n h0 act' + · have cn : ∀ p k tb, σ.kheap p = some (.cnode k tb) ↔ τ.kheap p = some (.cnode k tb) := fun p k tb => + ⟨fun h => objF p _ h (by intro _ e; cases e) (by intro _ _ e; cases e), + fun h => objB p _ h (by intro _ e; cases e) (by intro _ _ e; cases e)⟩ + have ct : ∀ n p, capTableAt n p τ ↔ capTableAt n p σ := fun n p => by + unfold Fl4SpecA64.capTableAt + exact exists_congr fun cs => and_congr (cn p n cs).symm Iff.rfl + refine + { node := ?_ + slot := fun irq hi o b c ho hc => ?_ + ref := fun p o b c ho hc => ?_ } + · have h := J.sinv.irqN.node + unfold Fl4SpecA64.IrqNodeFl4 Fl4SpecA64.validIrqNodeFl4 Fl4SpecA64.irqNodeDomFl4 at h ⊢ + simp only [HS.irqN, ct] + exact h + · rw [HS.irqN] at ho + obtain ⟨o0, h0, c0⟩ := capB _ _ _ _ ho hc + exact J.sinv.irqN.slot irq hi o0 b c h0 c0 + · rw [HS.irqN] + obtain ⟨o0, h0, c0⟩ := capB _ _ _ _ ho hc + exact J.sinv.irqN.ref p o0 b c h0 c0 + · intro p z' h hs hr + rw [fco] + rcases tcbB p z' h with ⟨e, ez⟩ | ⟨-, h0⟩ + · rw [ez] at hs + exact J.sinv.zc p x (by rw [e]; exact hx) (by rw [← x2sc, ← fsc]; exact hs) (by rw [← frel]; exact hr) + · exact J.sinv.zc p z' h0 (by rw [← fsc]; exact hs) (by rw [← frel]; exact hr) + · intro u hm + rw [frel] at hm + obtain ⟨z, hz, hr⟩ := J.sinv.rlsRun u hm + obtain ⟨z', hz', k⟩ := tcbF _ _ hz + refine ⟨z', hz', ?_⟩ + rcases k with ⟨-, ez, ez'⟩ | ⟨-, ez'⟩ + · rw [ez']; exact x2run (ez ▸ hr) + · rw [ez']; exact hr + · intro hc y n h ha + rw [fsc] at h + rw [fco] at hc + obtain ⟨y0, h0, c, yf, e⟩ := scB _ _ _ h + have := J.sinv.curScReady hc y0 n h0 (by rw [e] at ha; exact ha) + unfold refillHd at this ⊢ + rw [e, fct]; exact this + · intro p z' h hf + rcases tcbB p z' h with ⟨e, ez⟩ | ⟨-, h0⟩ + · exfalso + rw [ez] at hf + have hf' := J.sinv.faultSt p x (by rw [e]; exact hx) (by rw [← x2fault]; exact hf) + rcases legal with h' | h' | h' | h' <;> rw [h'] at hf' <;> + rcases hf' with ⟨_, _, h''⟩ | ⟨_, h''⟩ | h'' <;> cases h'' + · exact J.sinv.faultSt p z' h0 hf + have rv : RunVcpuInv st Z.T s' := fun u hu hne => by + have kv : L.mem s' (runVcpuA u) = L.mem s (runVcpuA u) := by + unfold runVcpuA at * + exact kT u hu 3672#u64 (by simp) (by simp) (by simp) (Or.inr (by simp)) + rw [kv] at hne + have old := J.runVcpu u hu hne + by_cases e : u = t + · subst e + have vm : x.tcbState = .ext .runningVM := by + have c := R.st + rw [old] at c + have c8 : tsCode x.tcbState = 8 := c.symm.trans (by unfold sched.TS_RUNNING_VM; rfl) + revert c8 + rcases legal with h | h | h | h <;> rw [h] <;> intro c8 <;> first | rfl | exact absurd c8 (by decide) + refine UScalar.eq_of_val_eq (stK.trans ?_) + rw [x2state, vm] + unfold sched.TS_RUNNING_VM + rfl + · rw [← old] + rw [stA_eq] + exact kT u hu 3328#u64 (by simp) (by simp) (by simp) (Or.inl e) + have bq : BlkQueuedA Z.T E N (L.mem s') τ := by + refine ⟨fun u hu z hz e hb => ?_, fun u hu z hz p hb => ?_⟩ + · rcases tcbB _ _ hz with ⟨-, ez⟩ | ⟨-, h⟩ + · rw [ez, x2NB.1] at hb; cases hb + obtain ⟨k1, y, k2, k3⟩ := J.blkQ.ep u hu z h e hb + exact ⟨by rw [blkK u hu]; exact k1, y, objF _ _ k2 (by intro _ c; cases c) (by intro _ _ c; cases c), k3⟩ + · rcases tcbB _ _ hz with ⟨-, ez⟩ | ⟨-, h⟩ + · rw [ez] at hb; exact absurd hb (x2NB.2.1 p) + obtain ⟨k1, y, l, k2, k3, k4⟩ := J.blkQ.ntfn u hu z h p hb + exact ⟨by rw [blkK u hu]; exact k1, y, l, objF _ _ k2 (by intro _ c; cases c) (by intro _ _ c; cases c), k3, k4⟩ + have schedV : SchedValidA τ := SchedValidB.toA fa.1.1 fa.1.2 + have Qk : QKeepA σ τ := + ⟨fun p z' h => by + rcases tcbB p z' h with ⟨e, ez⟩ | ⟨-, h0⟩ + · exact ⟨x, by rw [e]; exact hx, by rw [ez, x2prio]⟩ + · exact ⟨z', h0, rfl⟩, + fun p x' h => ⟨x', objB p _ h (by intro _ c; cases c) (by intro _ _ c; cases c), List.Sublist.refl _⟩, + fun p x' h => ⟨x', objB p _ h (by intro _ c; cases c) (by intro _ _ c; cases c), List.Sublist.refl _⟩, + fun p y' n h => by + obtain ⟨y0, h0, c, yf, e⟩ := scB _ _ _ h + subst e + exact ⟨y0, n, h0, fun r => ⟨r, id⟩⟩, + fun p y h => ⟨y, objB p _ h (by intro _ c; cases c) (by intro _ _ c; cases c), id⟩, + ⟨congrArg (·.domainIndex) eτ, congrArg (·.domainStartIndex) eτ, congrArg (·.domainList) eτ, congrArg (·.archState.armUsGlobalVspace) eτ⟩⟩ + have j := J.j57_stepNWA wN τ + have J2 := JStage3A.of_schedWr (J5 := J5A st) (Rj := J7R2A st ar hPl.ipa) J (Q := Z.Q) (Rl := Z.R) + (curOf := Z.curOf) (curScOf := Z.curScOf) (s' := s') (a' := a) (σ' := τ) w HS SSz Qk hum J.j.1.ar + (fun _ _ => rfl) bufAl ⟨J.ent_stepNWA wN Wk.1.blk, Wk.1.sys⟩ sq poolOff H2.sched scCov ep ntfn reply bufCover + idleNF H2.mcp H2.links sinv sidle rv bq Wk.1.sym schedV j.1 j.2 + -- the entry extras + have scC : ∀ y n, τ.kheap τ.curSc = some (.schedContext y n) → + ∃ y0, σ.kheap σ.curSc = some (.schedContext y0 n) ∧ y.scRefills = y0.scRefills ∧ + scActive y = scActive y0 := fun y n h => by + rw [fsc] at h + obtain ⟨y0, h0, c, yf, e⟩ := scB _ _ _ h + exact ⟨y0, h0, by rw [e], by rw [e]⟩ + have curInv' : CurInvA τ := by + obtain ⟨⟨z, hz, es, -⟩, ⟨y, n, hy⟩, ha, nr⟩ := curInv + rw [hx] at hz + obtain rfl := KernelObject.tcb.inj (Option.some.inj hz) + obtain ⟨y', hy', -⟩ := scF _ _ _ hy + refine ⟨⟨x2, by rw [fcur]; exact SP.at1, by rw [x2sc, fsc]; exact es, x2NB.2.2.2.2.2⟩, + ⟨y', n, by rw [fsc]; exact hy'⟩, fun ni y1 n1 h => ?_, by rw [fcur, frel]; exact nr⟩ + obtain ⟨y0, h0, -, e⟩ := scC _ _ h + rw [e]; exact ha (by rw [← fsc]; exact ni) y0 n1 h0 + have offReady' : OffReadyA τ := fun y n h => by + obtain ⟨y0, h0, e, -⟩ := scC _ _ h + have := offReady y0 n h0 + unfold refillHd at this ⊢ + rw [e, fco, fct]; exact this + have offOK' : OffOKA τ := fun pos y n h ha => by + obtain ⟨y0, h0, e, ea'⟩ := scC _ _ h + have := offOK (by rw [← fco]; exact pos) y0 n h0 (fun ni => by rw [← ea']; exact ha (by rw [fsc]; exact ni)) + unfold refillHd at this ⊢ + rw [e, fco, fct]; exact this + have actIdle' : ActIdleA τ := by + refine ⟨fun u h => (by rw [act] at h; cases h), ?_⟩ + obtain ⟨z, hz, es, hs, nr⟩ := actIdle.2 + obtain ⟨z', hz', e1', k⟩ := idleT z hz + exact ⟨z', by rw [fidle]; exact hz', e1'.trans es, k hs, by rw [fidle, frel]; exact nr⟩ + have sact : SActA τ := by + refine ⟨fun u h => (by rw [act] at h; cases h), x2, by rw [fcur]; exact SP.at1, ?_⟩ + rcases x2st with h | h + · exact Or.inl h + · exact Or.inr (Or.inr (Or.inr h)) + refine ⟨rfl, τ, e1, ⟨J2, curInv', offOK', offReady', actIdle', fun h => ?_, ?_, sact⟩, fun hni y hy hs => ?_, + fun y hy => ?_⟩ + · rw [fsc] at h ⊢; exact notIdle h + · unfold ValidMachineTimeA at validTime ⊢ + rw [fct, fms]; exact validTime + · rw [hx] at hy + obtain rfl := KernelObject.tcb.inj (Option.some.inj hy) + obtain ⟨c, hc, ec⟩ := J.sinv.onlyIdle q x hx hs + have cc : c = m.curCore := + (Nat.lt_one_iff.1 (lt_of_lt_of_eq hc h1)).trans (Nat.lt_one_iff.1 (lt_of_lt_of_eq hcore h1)).symm + apply hni + rw [fcur, fidle] + change q = _ + rw [ec, cc]; exact J.sinv.view.symm + · exact hNative τ mem y hy + +set_option maxHeartbeats 1000000 in +/-- **`syscall.finish` at the record** with `hSched`, `hAct` and `hExit` discharged (module docstring). -/ +theorem JStage3A.finishEA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {s : S} {a : A} {σ : State Unit} {m : McState} + {tm : A → Nat → DeadlineViewA} {view : State Unit → Fl4TimerView} + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) (arch : ArchSwFA st ar) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hSw : SwA σ) (B : BodyExitA σ) + (hSig : ∀ (Q : U64 → U64 → List U64) (Rl : U64 → List U64) (s : S) (a : A) (σ : State Unit) (t badge : U64), + JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes + (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m → + SwA σ → t ∈ Z.T → L.mem s (boundA t) ≠ 0#u64 → + notification.send_signal st ar s a (L.mem s (boundA t)) badge ⦃ r => + ∃ σ' b Q' Rl', sendSignal (Z := Unit) (specPtr (L.mem s (boundA t))) b σ = ({((), σ')}, False) ∧ + JStage3A st AR { Z with Q := Q', R := Rl' } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ + fl4A64Platform.numNodes (J5A st) (J7A st (J7R2A st ar hPl.ipa)) r.1 r.2 σ' m ⦄) + (G : SchedGapA (st := st) Z m κ s a σ ar tm view) (arK : ArRootKeepA ar AR) + (toUs : ∀ a', Fl4TicksToUsAgreeA ar a') + (hNative : ∀ τ1 τ2, ((), τ1) ∈ (fl4Schedule kLimitA view σ).1 → + ((), τ2) ∈ (fl4ActivateThread (Z := Unit) τ1).1 → + ∀ y, τ2.kheap τ2.curThread = some (.tcb y) → y.tcbState ≠ .ext .runningVM) : + syscall.finish st ar s a ⦃ r => + StepPostA (KExitA st ar AR hPl.ipa m) (fl4Finish kLimitA view) σ r ⦄ := by + refine JStage3A.finishFA hA hPl arch hCs h1 J hSw B hSig G arK toUs hNative ?_ + intro f out σ1 τ1 τ2 Q Rl ef eout J1 hf tail hact facts atExit + obtain ⟨s1, a1, Q', Rl', Tcur, Ccur, hw, sch, J2⟩ := atExit + have step := fl4Schedule_of_tail kLimitA view hf tail + have hn : ∀ τ, ((), τ) ∈ (fl4ActivateThread (Z := Unit) τ1).1 → + ∀ y, τ.kheap τ.curThread = some (.tcb y) → y.tcbState ≠ .ext .runningVM := + fun τ h => hNative τ1 τ step h + obtain ⟨r, er, ea, τ, e1, J3, hidle, hvm⟩ := + spec_ex_fd (JStage3A.activateA (ar := ar) hA hPl h1 J2 facts (toUs a1) hn) + have eτ : τ2 = τ := by + rw [e1] at hact + exact (Prod.mk.inj (Set.mem_singleton_iff.1 hact)).2 + subst eτ + have H3 := J3.toJStage3A.yieldQueueA + have ht := H3.sched.curT + obtain ⟨y, hy⟩ := J3.toJStage3A.sinv.curTcb + have hy' : τ2.kheap (specPtr (L.curW r.1)) = some (.tcb y) := by rw [← H3.sched.sch.curThread]; exact hy + have run := (vcpu_check_off_vmA (ar := ar) (s := r.1) H3 r.2 ht hy' (hvm y hy)).1 + have eo : out = r := by + have : syscall.finish st ar s a = ok r := by + unfold syscall.finish + rw [sch, bind_ok] + simp only [uncurry_apply_pair] + rw [er, bind_ok] + obtain ⟨s2, a2⟩ := r + simp only [uncurry_apply_pair] + rw [eq_of_spec_fd (sched.cur_thread.spec s2), bind_ok] + exact run + exact ok_inj_ff (eout.symm.trans this) + rw [eo] + exact ⟨_, H, OS, U, Ac, Pp, Tb, Lv, ia, ie, N, E, Rp, Bf, stack, κ, J3, hidle⟩ + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3KEntryActSpecA.lean b/proofs/Fl4ProofsA64/P3/Stage3KEntryActSpecA.lean new file mode 100644 index 000000000..f947845af --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3KEntryActSpecA.lean @@ -0,0 +1,541 @@ +/- +P3 (aarch64, A64-P3 Stage 3, kernel-entry composition, Plan A, item 4): **the exact spec outcome of +`activate_thread` and FL4's words for it**. + +`activate_thread_exactA`: `sched.activate_thread` refines `fl4ActivateThread` with the outcome `τ` described +completely (`ActSpecA`): the current TCB `x` becomes `x2` (activated state, new registers, no yield target), at +most one SC loses its `scYieldFrom` and changes its consumed time, and nothing else of the heap moves. FL4's +register words of the current thread are `x2`'s registers, and the write set is the SC words and scheduler words of +`MWrA` plus the current thread's words `+256/264/272` (reply), `+3472` (yield target), `+512` (next IP) and `+3328` +(state). + +Named premises: the activation is legal (`legal`), the saved `FAULT_IP` word is the spec's register (`pc`), [P] +`Fl4TicksToUsAgreeA`. + +New module. +-/ +import Fl4ProofsA64.P3.Stage3SchedActA +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3.Sched + +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4ProofsA64.P3.Endp Fl4ProofsA64.P1.W7.R.ST + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-- The current thread's words `activate_thread` writes. -/ +def ActWordA (t b : U64) : Prop := + b = wa t 256#u64 ∨ b = wa t 264#u64 ∨ b = wa t 272#u64 ∨ b = yieldA t ∨ b = wa t 512#u64 ∨ b = stA t + +/-- **The spec outcome of activating the TCB at `q`** (module docstring). -/ +structure ActSpecA (σ τ : State Unit) (q : ObjRef) (x x2 : Tcb) : Prop where + at0 : σ.kheap q = some (.tcb x) + at1 : τ.kheap q = some (.tcb x2) + rest : τ = { σ with kheap := τ.kheap, schedulerAction := τ.schedulerAction } + other : ∀ p, p ≠ q → τ.kheap p = σ.kheap p ∨ ∃ y n c, σ.kheap p = some (.schedContext y n) ∧ + τ.kheap p = some (.schedContext { y with scConsumed := c, scYieldFrom := none } n) + tcb : ∃ f : Fl4SpecA64.Register → Fl4SpecA64.Word, + x2 = ({ x with tcbState := activatedStateA x.tcbState, tcbArch := archTcbSetRegisters f x.tcbArch, tcbYieldTo := none } : Tcb) + +private theorem yieldCancel_eqK {σ : State Unit} {p q : ObjRef} {x : Tcb} {y : SchedContext} {n : Nat} + (hk : σ.kheap q = some (.tcb x)) (hs : σ.kheap p = some (.schedContext y n)) + (ht : x.tcbYieldTo = some p) : + schedContextCancelYieldTo (Z := Unit) q σ = ({((), yieldPairStA σ p q x y n false)}, False) := by + have hne : q ≠ p := fun e => by rw [e, hs] at hk; cases hk + unfold schedContextCancelYieldTo getTcbObjRef + rw [nd_bind_oneSc (threadGet_eqSc _ hk), ht] + change (updateSchedContext p _ >>= _) σ = _ + rw [nd_bind_oneSc (updateSchedContext_eqA hs _)] + exact threadSet_eqSc _ (by rwa [updKSc_kheap, Function.update_of_ne hne]) + +private theorem yieldConsumed_exactA (cap : Ticks) (toUs : Ticks → Word64) {σ : State Unit} {p : ObjRef} + {y : SchedContext} {n : Nat} (hk : σ.kheap p = some (.schedContext y n)) : + ∃ tm c, schedContextUpdateConsumedWith (Z := Unit) cap toUs p σ = + ({(tm, updKSc σ p (.schedContext { y with scConsumed := c } n))}, False) := by + unfold schedContextUpdateConsumedWith + rw [nd_bind_oneSc (getSchedContext_eqA hk), nd_pure_bindSc] + split + · refine ⟨toUs cap, y.scConsumed - cap, ?_⟩ + change (updateSchedContext p _ >>= _) σ = _ + rw [nd_bind_oneSc (updateSchedContext_eqA hk _)] + rfl + · refine ⟨toUs y.scConsumed, 0, ?_⟩ + change (updateSchedContext p _ >>= _) σ = _ + rw [nd_bind_oneSc (updateSchedContext_eqA hk _)] + rfl + +/-- **The exact outcome of `complete_yield_to`** on a TCB yielding to the SC at `p`. -/ +theorem completeYield_stateA (cap : Ticks) (toUs : Ticks → Word64) {σ : State Unit} {p q : ObjRef} {x : Tcb} + {y : SchedContext} {n : Nat} (hk : σ.kheap q = some (.tcb x)) (hs : σ.kheap p = some (.schedContext y n)) + (hyt : x.tcbYieldTo = some p) : + ∃ c d, completeYieldToWith (Z := Unit) cap toUs q σ = + ({((), { σ with kheap := (Function.update (Function.update σ.kheap p + (some (.schedContext { y with scConsumed := c, scYieldFrom := none } n))) q + (some (.tcb (yieldDoneA x d)))) })}, False) := by + have hne : q ≠ p := fun e => by rw [e, hs] at hk; cases hk + obtain ⟨tm, c, hup⟩ := yieldConsumed_exactA cap toUs hs + let y' : SchedContext := { y with scConsumed := c } + let σ1 := updKSc σ p (.schedContext y' n) + have hz : σ1.kheap q = some (.tcb x) := by + change (updKSc σ p _).kheap q = _ + rw [updKSc_kheap, Function.update_of_ne hne] + exact hk + let z := yieldRegA (yieldRegA (yieldRegA x badge_register 0) .x2 (ucast 64 tm)) msg_info_register + (messageInfoToData ⟨natToLen 1, 0, 0, 0⟩) + let σ2 := updKSc σ1 q (.tcb z) + have hreply : replyFromKernel (Z := Unit) q (0, wordsFromTime tm) σ1 = ({((), σ2)}, False) := + yield_reply_wordA hz _ + have hz2 : σ2.kheap q = some (.tcb z) := updKSc_same _ _ _ + have hs2 : σ2.kheap p = some (.schedContext y' n) := by + change (updKSc σ1 q (.tcb z)).kheap p = _ + rw [updKSc_kheap, Function.update_of_ne hne.symm] + exact updKSc_same _ _ _ + have cancel := yieldCancel_eqK hz2 hs2 (show z.tcbYieldTo = some p from hyt) + refine ⟨c, ucast 64 tm, ?_⟩ + have e : yieldPairStA σ2 p q z y' n false = { σ with kheap := (Function.update (Function.update σ.kheap p + (some (.schedContext { y with scConsumed := c, scYieldFrom := none } n))) q + (some (.tcb (yieldDoneA x (ucast 64 tm))))) } := by + have k : (yieldPairStA σ2 p q z y' n false).kheap = Function.update (Function.update σ.kheap p + (some (.schedContext { y with scConsumed := c, scYieldFrom := none } n))) q + (some (.tcb (yieldDoneA x (ucast 64 tm)))) := by + funext r + change (updKSc (updKSc σ2 p (.schedContext { y' with scYieldFrom := none } n)) + q (.tcb { z with tcbYieldTo := none })).kheap r = _ + simp only [σ2, σ1, updKSc_kheap] + by_cases hq : r = q + · subst hq + rw [Function.update_self, Function.update_self] + rfl + rw [Function.update_of_ne hq, Function.update_of_ne hq] + by_cases hp : r = p + · subst hp + rw [Function.update_self, Function.update_self] + rw [Function.update_of_ne hp, Function.update_of_ne hq, Function.update_of_ne hp, + Function.update_of_ne hp] + rw [← k] + rfl + rw [← e] + unfold completeYieldToWith getTcbObjRef + rw [nd_bind_oneSc (threadGet_eqSc _ hk), hyt] + change (((schedContextUpdateConsumedWith cap toUs p >>= fun c => replyFromKernel q (0, wordsFromTime c)) >>= _) σ) = _ + rw [nd_bind_assocSc, nd_bind_oneSc hup, nd_bind_oneSc hreply] + exact cancel + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +/-- **The yield stage, exactly**: FL4's reply words are the spec TCB's registers. -/ +theorem activate_yield_exactA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {σ : State Unit} (H : YieldQueueRelA (st := st) Z m κ s σ) (a : A) {t : U64} + (ht : t ∈ Z.T) (hC0 : ∀ c ∈ Z.C, c ≠ 0#u64) + {x : Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + (htu : Fl4TicksToUsAgreeA ar a) : + (do + let i ← obj.Tcb.yield_to st t s + if i != 0#u64 then schedcontext.complete_yield_to st ar s a t else ok (s, a)) ⦃ r => r.2 = a ∧ + (x.tcbYieldTo = none ∧ r.1 = s ∨ + ∃ p y n c d, x.tcbYieldTo = some p ∧ σ.kheap p = some (.schedContext y n) ∧ + activationYieldA (specPtr t) σ = ({((), { σ with kheap := (Function.update (Function.update σ.kheap p + (some (.schedContext { y with scConsumed := c, scYieldFrom := none } n))) (specPtr t) + (some (.tcb (yieldDoneA x d)))) })}, False) ∧ + L.mem r.1 (wa t 256#u64) = 0#u64 ∧ + (L.mem r.1 (wa t 264#u64)).bv = messageInfoToData ⟨natToLen 1, 0, 0, 0⟩ ∧ + (L.mem r.1 (wa t 272#u64)).bv = d) ⦄ := by + obtain ⟨x0, h0, hv⟩ := H.links.tcb t ht + rw [hk] at h0; cases h0 + let sc := L.mem s (yieldA t) + rw [eq_of_spec_fd (obj.Tcb.yield_to.spec s t), bind_ok] + change (if sc != 0#u64 then _ else _) ⦃ _ ⦄ + by_cases hsc : sc = 0#u64 + · rw [if_neg (by simp [hsc]), WP.spec_ok] + refine ⟨rfl, Or.inl ⟨?_, rfl⟩⟩ + rw [hv] + change optScA m sc = none + rw [hsc]; simp [optScA] + · rw [if_pos (bne_iff_ne.2 hsc)] + obtain ⟨hc, y, n, hy, hFrom⟩ := H.links.toSc t ht hsc + have eq : L.mem s (scYieldFromA sc) = t := by + obtain ⟨y0, n0, e, R⟩ := H.sched.scr.sc sc hc + rw [hy] at e; cases e + have e := hFrom + have haddr : scYieldFromA sc = scWA sc 56#u64 := by unfold scYieldFromA scWA layout.sc.YIELD_FROM; rfl + rw [R.yieldFrom, ← haddr] at e + have en : L.mem s (scYieldFromA sc) ≠ 0#u64 := by + intro h; rw [h, optRefS_zero] at e; cases e + rw [optRefS_ne en] at e + exact specPtr_inj (Option.some.inj e) + have t0 : t ≠ 0#u64 := fun h => (H.sched.core.tcbs t ht).ne0 (by rw [h]; rfl) + have hyt : x.tcbYieldTo = some (scNameA m sc) := by + rw [hv] + change optScA m sc = some _ + simp [optScA, hsc, scNameA_eq] + obtain ⟨c, d, hspec⟩ := completeYield_stateA fl4MaxConsumedTicks fl4TicksToUs hk hy hyt + refine WP.spec_mono (yield_complete_liveWithA H.sched H.states H.mcp H.links a hc hC0 t0 eq + fl4MaxConsumedTicks fl4TicksToUs rfl htu) ?_ + rintro r ⟨arch, τ, _, _, _, _, _, _, _, _, _, _, _, he, -, d', done, w256, w264, w272⟩ + have eτ : τ = _ := (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 + (congrArg Prod.fst (he.symm.trans hspec)))).2 + have hd : d' = d := by + have h := done x hk + rw [eτ] at h + change Function.update _ (specPtr t) _ (specPtr t) = _ at h + rw [Function.update_self] at h + have h := KernelObject.tcb.inj (Option.some.inj h) + have h2 := congrArg (fun z : Tcb => z.tcbArch.tcbContext.userRegs .x2) h + simp only [yieldDoneA, yieldRegA, archTcbSetRegisters, archTcbGetRegisters, msg_info_register] at h2 + rw [Function.update_of_ne (show Register.x2 ≠ msgInfoRegister by decide), + Function.update_of_ne (show Register.x2 ≠ msgInfoRegister by decide), + Function.update_self, Function.update_self] at h2 + exact h2.symm + refine ⟨arch, Or.inr ⟨_, y, n, c, d, hyt, hy, ?_, w256, w264, hd ▸ w272⟩⟩ + unfold activationYieldA getTcbObjRef + rw [nd_bind_oneSc (threadGet_eqSc _ hk), hyt] + unfold NonDet.when + rw [if_pos (by simp)] + exact hspec + +theorem tcbRegOffA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRN st Z m κ s σ) {t K : U64} (ht : t ∈ Z.T) (h1 : 256 ≤ K.val) (h2 : K.val < 552) : + ¬ MWrA (st := st) Z m σ s (wa t K) := by + have off := tcbLowOffA (K := K) H ht (by omega) + have ne {v K' : U64} (hv : v ∈ Z.T) (hK : K'.val < 4096) (hn : K.val ≠ K'.val) : wa t K ≠ wa v K' := + field_ne (H.core.tcbs t ht) (H.core.tcbs v hv) (by omega) hK (fun _ => hn) + exact not_mwrA (fun e => off.2 (Or.inr (Or.inr (Or.inr (Or.inr e))))) + (fun v hv => by + rw [scA_eq, nextA_eq', prevA_eq', inqA_eq] + refine ⟨ne hv (by decide) ?_, ne hv (by decide) ?_, ne hv (by decide) ?_, ne hv (by decide) ?_⟩ <;> + (intro h; revert h2; rw [h]; decide)) off.1 + +/-- The state stage: nothing for a non-restarting thread; a restarting one gets its `FAULT_IP` word as `NEXT_IP`. -/ +theorem activate_state_wordA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {σ : State Unit} (H : YieldQueueRelA (st := st) Z m κ s σ) (a : A) + {t : U64} (ht : t ∈ Z.T) {x : Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + (legal : ActivateStateOKA x.tcbState) : + (do + let ts ← obj.Tcb.state st t s + if ts = sched.TS_RESTART then + let saved ← obj.Tcb.reg st t s layout.reg.FAULT_IP + let s' ← obj.Tcb.set_reg st t s layout.reg.NEXT_IP saved + let s'' ← sched.set_thread_state st s' t sched.TS_RUNNING + ok (s'', a) + else ok (s, a)) ⦃ r => + (x.tcbState ≠ .restart → r.1 = s) ∧ + (x.tcbState = .restart → L.mem r.1 (wa t 512#u64) = L.mem s (regWA t layout.reg.FAULT_IP)) ⦄ := by + have code := H.states t ht x hk + have stRead : L.mem s (stA t) = sched.TS_RESTART ↔ x.tcbState = .restart := by + have e : (u v : U64) → (u = v ↔ u.val = v.val) := + fun _ _ => ⟨congrArg UScalar.val, UScalar.eq_of_val_eq⟩ + rw [e, code] + unfold sched.TS_RESTART + rcases legal with h | h | h | h <;> rw [h] <;> decide + refine WP.spec_bind (obj.Tcb.state.spec s t) ?_ + intro ts hts + rw [hts] + by_cases hR : x.tcbState = .restart + · rw [if_pos (stRead.2 hR)] + rw [tcb_reg_eqA, bind_ok] + have htA := H.sched.core.tcbs t ht + have al : Aligned (wa t 512#u64) := field_aligned htA (by decide) (by decide) + refine WP.spec_bind (spec_andA + (activateWritePcA H ht hk (L.mem s (regWA t layout.reg.FAULT_IP)) .nextIP (offset := 512#u64) (Or.inl rfl) + (by simp [layout.reg.NEXT_IP, layout.reg.ELR_EL1]) (by simp [layout.reg.NEXT_IP, layout.reg.ELR_EL1])) + (obj.Tcb.set_reg.wrK s t layout.reg.NEXT_IP _ htA (by simp [layout.reg.NEXT_IP, layout.reg.ELR_EL1]))) ?_ + rintro s' ⟨H', k, hk', e⟩ + have ek : k = 512#u64 := UScalar.eq_of_val_eq (hk'.trans (by simp [layout.reg.NEXT_IP, layout.reg.ELR_EL1])) + have codeR : sched.TS_RUNNING.val = tsCodeA .running := by unfold sched.TS_RUNNING tsCodeA; rfl + refine WP.spec_bind (yieldRunnableA H' ht (updKSc_same _ _ _) + (by change runnable x.tcbState = _; rw [hR]; rfl) + (by rfl : runnable ThreadState.running = true) codeR) ?_ + rintro s'' ⟨τ, -, -, -, -, -, -, w2⟩ + rw [WP.spec_ok] + refine ⟨fun h => (h hR).elim, fun _ => ?_⟩ + change L.mem s'' (wa t 512#u64) = _ + have keep : L.mem s'' (wa t 512#u64) = L.mem s' (wa t 512#u64) := by + refine w2.mem_eq al ?_ + rintro (h | h) + · exact tcbRegOffA H'.sched.toSchedSRN ht (K := 512#u64) (by decide) (by decide) (Or.inr h) + · rw [stA_eq] at h + exact field_ne htA htA (by decide) (by decide) (fun _ => by decide) h + rw [keep, e, ek, L.mem_wr_same al] + · rw [if_neg (fun h => hR (stRead.1 h)), WP.spec_ok] + exact ⟨fun _ => rfl, fun h => (hR h).elim⟩ + +private theorem wordsK {t : U64} (ht : TcbAddr t) (r : Register) : + ∃ K : U64, K.val = 256 + 8 * (regIdxA r).val ∧ (regIdxA r).val < 37 ∧ Endp.regA t (regIdxA r) = wa t K := by + have h37 : (regIdxA r).val < 37 := by cases r <;> decide + refine ⟨⟨BitVec.ofNat 64 (256 + 8 * (regIdxA r).val)⟩, ?_, h37, ?_⟩ + · show (BitVec.ofNat 64 (256 + 8 * (regIdxA r).val)).toNat = _ + rw [BitVec.toNat_ofNat] + exact Nat.mod_eq_of_lt (by omega) + · have hK : (⟨BitVec.ofNat 64 (256 + 8 * (regIdxA r).val)⟩ : U64).val = 256 + 8 * (regIdxA r).val := by + show (BitVec.ofNat 64 (256 + 8 * (regIdxA r).val)).toNat = _ + rw [BitVec.toNat_ofNat] + exact Nat.mod_eq_of_lt (by omega) + apply UScalar.eq_of_val_eq + rw [regA_val ht h37, field_val ht (by rw [hK]; omega), hK] + omega + +set_option maxRecDepth 4096 in +set_option maxHeartbeats 1000000 in +private theorem activate_walkA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {σ : State Unit} (H : YieldQueueRelA (st := st) Z m κ s σ) (a : A) + (hC0 : ∀ c ∈ Z.C, c ≠ 0#u64) (htu : Fl4TicksToUsAgreeA ar a) + {x : Tcb} (hk : σ.kheap σ.curThread = some (.tcb x)) (legal : ActivateStateOKA x.tcbState) + (regs : ∀ r, (L.mem s (Endp.regA (L.curW s) (regIdxA r))).bv = x.tcbArch.tcbContext.userRegs r) : + sched.activate_thread st ar s a ⦃ r => ∃ τ x2, + fl4ActivateThread (Z := Unit) σ = ({((), τ)}, False) ∧ + ActSpecA σ τ σ.curThread x x2 ∧ + (∀ rg, (L.mem r.1 (Endp.regA (L.curW s) (regIdxA rg))).bv = x2.tcbArch.tcbContext.userRegs rg) ∧ + WritesIn st s r.1 (fun b => MWrA (st := st) Z m σ s b ∨ ActWordA (L.curW s) b) ⦄ := by + have pc : (L.mem s (regWA (L.curW s) layout.reg.FAULT_IP)).bv = x.tcbArch.tcbContext.userRegs .faultIP := by + have r := regs .faultIP + have e : regIdxA .faultIP = layout.reg.FAULT_IP := by unfold regIdxA layout.reg.FAULT_IP; rfl + rw [e] at r + exact r + let t := L.curW s + have ht := H.sched.curT + have htσ := H.sched.sch.curThread + have hk0 := hk + rw [htσ] at hk + have htA := H.sched.core.tcbs t ht + have N := H.sched.toSchedSRN + unfold sched.activate_thread + refine WP.spec_bind (sched.cur_thread.spec s) ?_ + intro ct hct + rw [show ct = t from hct, ← cfgResultAssocA] + refine WP.spec_bind (spec_andA (activate_yield_refinesA H a ht hC0 hk htu) + (spec_andA (activate_yield_footA H a ht hC0 hk htu) (activate_yield_exactA H a ht hC0 hk htu))) ?_ + rintro ⟨s1, a1⟩ ⟨⟨ea, σ1, x1, he, H1, hC1, h1, hS, hP, hWord⟩, W1, -, Y⟩ + simp only [uncurry_apply_pair] at W1 Y ⊢ + change a1 = a at ea + subst a1 + have legal1 : ActivateStateOKA x1.tcbState := hS.symm ▸ legal + have pc1 : (L.mem s1 (regWA t layout.reg.FAULT_IP)).bv = x1.tcbArch.tcbContext.userRegs .faultIP := by + rw [hWord, hP]; exact pc + -- the yield stage's spec state + have YS : (∃ y2 : Tcb, σ1.kheap (specPtr t) = some (.tcb y2) ∧ + (∃ f : Fl4SpecA64.Register → Fl4SpecA64.Word, + y2 = ({ x with tcbArch := archTcbSetRegisters f x.tcbArch, tcbYieldTo := none } : Tcb)) ∧ + (∀ p, p ≠ specPtr t → σ1.kheap p = σ.kheap p ∨ ∃ y n c, σ.kheap p = some (.schedContext y n) ∧ + σ1.kheap p = some (.schedContext { y with scConsumed := c, scYieldFrom := none } n)) ∧ + σ1 = { σ with kheap := σ1.kheap } ∧ + (∀ rg, (L.mem s1 (Endp.regA t (regIdxA rg))).bv = y2.tcbArch.tcbContext.userRegs rg)) := by + have far (K : U64) (h1 : 256 ≤ K.val) (h2 : K.val < 552) (hne : K.val ≠ 256 ∧ K.val ≠ 264 ∧ K.val ≠ 272) : + L.mem s1 (wa t K) = L.mem s (wa t K) → True := fun _ => trivial + have keepR (K : U64) (h1 : 256 ≤ K.val) (h2 : K.val < 552) (h8 : K.val % 8 = 0) + (hne : K.val ≠ 256 ∧ K.val ≠ 264 ∧ K.val ≠ 272) : L.mem s1 (wa t K) = L.mem s (wa t K) := by + refine W1.mem_eq (field_aligned htA (by omega) h8) ?_ + rintro (h | (h | h | h) | h) + · exact tcbRegOffA N ht h1 h2 h + · exact field_ne htA htA (by omega) (by decide) (fun _ => by simpa using hne.1) h + · exact field_ne htA htA (by omega) (by decide) (fun _ => by simpa using hne.2.1) h + · exact field_ne htA htA (by omega) (by decide) (fun _ => by simpa using hne.2.2) h + · change wa t K = wa t 3472#u64 at h + exact field_ne htA htA (by omega) (by decide) (fun _ => by simp; omega) h + rcases Y with ⟨hn, hs⟩ | ⟨p, y, n, c, d, hyt, hy, hact, w256, w264, w272⟩ + · have eσ : σ1 = σ := by + have h : activationYieldA (specPtr t) σ = ({((), σ)}, False) := by + unfold activationYieldA getTcbObjRef + rw [nd_bind_oneSc (threadGet_eqSc _ hk), hn] + simp [NonDet.when] + rfl + exact ((Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst (he.symm.trans h)))).2) + subst eσ + refine ⟨x, hk, ⟨x.tcbArch.tcbContext.userRegs, ?_⟩, fun _ _ => Or.inl rfl, rfl, fun rg => ?_⟩ + · cases x + simp only [archTcbSetRegisters] at hn ⊢ + subst hn + rfl + · rw [hs]; exact regs rg + · have eσ := (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst (he.symm.trans hact)))).2 + have hne : specPtr t ≠ p := fun e => by rw [e, hy] at hk; cases hk + refine ⟨yieldDoneA x d, ?_, ⟨(yieldDoneA x d).tcbArch.tcbContext.userRegs, rfl⟩, fun q hq => ?_, ?_, + fun rg => ?_⟩ + · rw [eσ]; exact Function.update_self _ _ _ + · rw [eσ] + change Function.update (Function.update σ.kheap p _) (specPtr t) _ q = σ.kheap q ∨ _ + rw [Function.update_of_ne hq] + by_cases hp : q = p + · subst hp + refine Or.inr ⟨y, n, c, hy, ?_⟩ + change Function.update (Function.update σ.kheap q _) (specPtr t) _ q = _ + rw [Function.update_of_ne hq, Function.update_self] + · exact Or.inl (Function.update_of_ne hp _ _) + · rw [eσ] + · obtain ⟨K, hK, h37, eK⟩ := wordsK htA rg + rw [eK] + have regsD : ∀ r, (yieldDoneA x d).tcbArch.tcbContext.userRegs r = + (if r = .x1 then messageInfoToData ⟨natToLen 1, 0, 0, 0⟩ + else if r = .x2 then d else if r = .x0 then 0 else x.tcbArch.tcbContext.userRegs r) := by + intro r + simp only [yieldDoneA, yieldRegA, archTcbSetRegisters, archTcbGetRegisters, badge_register, + msg_info_register] + simp only [Function.update_apply] + rfl + rw [regsD] + by_cases h0 : rg = .x0 + · subst h0 + have : K = 256#u64 := UScalar.eq_of_val_eq (by rw [hK]; rfl) + rw [this, w256, if_neg (by decide), if_neg (by decide), if_pos rfl] + rfl + by_cases h1' : rg = .x1 + · subst h1' + have : K = 264#u64 := UScalar.eq_of_val_eq (by rw [hK]; rfl) + rw [this, w264, if_pos rfl] + by_cases h2' : rg = .x2 + · subst h2' + have : K = 272#u64 := UScalar.eq_of_val_eq (by rw [hK]; rfl) + rw [this, w272, if_neg (by decide), if_pos rfl] + rw [if_neg h1', if_neg h2', if_neg h0] + have hne3 : K.val ≠ 256 ∧ K.val ≠ 264 ∧ K.val ≠ 272 := by + rw [hK] + revert h0 h1' h2' + cases rg <;> simp [regIdxA] + rw [keepR K (by omega) (by omega) (by omega) hne3, ← eK] + exact regs rg + obtain ⟨y2, hy2, ⟨f2, ey2⟩, other1, rest1, regs1⟩ := YS + have eState : y2.tcbState = x.tcbState := by rw [ey2] + have x1eq : x1 = y2 := by + rw [h1] at hy2 + exact KernelObject.tcb.inj (Option.some.inj hy2) + subst x1eq + refine WP.spec_mono (spec_andA (activate_state_refinesA H1 a ht h1 legal1 pc1) + (spec_andA (activate_state_footA H1 a ht h1 legal1 pc1) (activate_state_wordA H1 a ht h1 legal1))) ?_ + rintro r ⟨⟨arch, τ, hτ, -, hC2, -⟩, W2, same, word⟩ + have hCur := hC2.trans hC1 + have al : ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → Aligned (wa t K) := fun K h1 h2 => field_aligned htA h1 h2 + have hk1 : L.ksb s1 = L.ksb s := W1.1 + have hz1 : SSzA σ σ1 := fun p y n h => by + rcases eq_or_ne p (specPtr t) with e | e + · rw [e, h1] at h; cases h + · rcases other1 p e with o | ⟨y0, n0, c, h0, h0'⟩ + · exact ⟨y, o ▸ h⟩ + · rw [h0'] at h + injection Option.some.inj h with _ en + exact ⟨y0, en ▸ h0⟩ + have ww : WritesIn st s r.1 (fun b => MWrA (st := st) Z m σ s b ∨ ActWordA t b) := by + refine (W1.trans W2).mono fun b hb => ?_ + rcases hb with ((h | (h | h | h) | h) | (h | h | h)) + · exact Or.inl h + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr (Or.inl h)) + · exact Or.inr (Or.inr (Or.inr (Or.inl h))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inl h)))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl h))))) + · exact Or.inl (Or.inr (swr_ksb rfl hk1 h)) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr h))))) + -- the spec outcome + have F : ∃ x2 : Tcb, τ.kheap (specPtr t) = some (.tcb x2) ∧ (∀ p, p ≠ specPtr t → τ.kheap p = σ1.kheap p) ∧ + τ = { σ1 with kheap := τ.kheap, schedulerAction := τ.schedulerAction } ∧ + (∃ f : Fl4SpecA64.Register → Fl4SpecA64.Word, + x2 = ({ x with tcbState := activatedStateA x.tcbState, tcbArch := archTcbSetRegisters f x.tcbArch, tcbYieldTo := none } : Tcb)) ∧ + (∀ rg, (L.mem r.1 (Endp.regA t (regIdxA rg))).bv = x2.tcbArch.tcbContext.userRegs rg) := by + by_cases hR : x.tcbState = .restart + · have hR1 : x1.tcbState = .restart := by rw [eState]; exact hR + let pcv := x1.tcbArch.tcbContext.userRegs .faultIP + let x2 : Tcb := { yieldRegA x1 .nextIP pcv with tcbState := .running } + have spec : fl4ActivateState (Z := Unit) (specPtr t) σ1 = + ({((), stsActA (updKSc (updKSc σ1 (specPtr t) (.tcb (yieldRegA x1 .nextIP pcv))) (specPtr t) + (.tcb x2)) (specPtr t))}, False) := by + unfold fl4ActivateState getThreadState + rw [nd_bind_oneSc (threadGet_eqSc _ h1), hR1] + dsimp only + unfold getRestartPC setNextPC + rw [nd_bind_oneSc (asUser_getRegister_eqA h1 .faultIP), nd_bind_oneSc (yieldAsUserRegA h1 .nextIP _)] + exact yieldSetStateSpecA (updKSc_same _ _ _) _ + have eτ := (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst (hτ.symm.trans spec)))).2 + have kh : τ.kheap = Function.update σ1.kheap (specPtr t) (some (.tcb x2)) := by + rw [eτ] + unfold stsActA + split <;> exact Function.update_idem _ _ _ + refine ⟨x2, by rw [kh]; exact Function.update_self _ _ _, fun p hp => by rw [kh, Function.update_of_ne hp], + ?_, ⟨x2.tcbArch.tcbContext.userRegs, ?_⟩, fun rg => ?_⟩ + · rw [eτ]; unfold stsActA; split <;> rfl + · simp only [x2, hR, activatedStateA, ey2, yieldRegA, archTcbSetRegisters] + · obtain ⟨K, hK, h37, eK⟩ := wordsK htA rg + have xr : x2.tcbArch.tcbContext.userRegs rg = + if rg = .nextIP then pcv else x1.tcbArch.tcbContext.userRegs rg := by + simp only [x2, yieldRegA, archTcbSetRegisters, archTcbGetRegisters, Function.update_apply] + rw [xr] + by_cases hn : rg = .nextIP + · subst hn + have : K = 512#u64 := UScalar.eq_of_val_eq (by rw [hK]; rfl) + rw [if_pos rfl, eK, this, word hR1] + exact pc1 + · rw [if_neg hn, eK] + have hne : K.val ≠ 512 := by + rw [hK]; revert hn; cases rg <;> simp [regIdxA] + have keep : L.mem r.1 (wa t K) = L.mem s1 (wa t K) := by + refine W2.mem_eq (al K (by omega) (by omega)) ?_ + rintro (h | h | h) + · exact field_ne htA htA (by omega) (by decide) (fun _ => by simpa using hne) h + · exact tcbRegOffA H1.sched.toSchedSRN ht (K := K) (by omega) (by omega) (Or.inr h) + · rw [stA_eq] at h + exact field_ne htA htA (by omega) (by decide) (fun _ => by simp; omega) h + rw [keep, ← eK] + exact regs1 rg + · have hR1 : x1.tcbState ≠ .restart := by rw [eState]; exact hR + have spec : fl4ActivateState (Z := Unit) (specPtr t) σ1 = ({((), σ1)}, False) := by + unfold fl4ActivateState getThreadState + rw [nd_bind_oneSc (threadGet_eqSc _ h1)] + rcases legal1 with e | e | e | e + · rw [e]; rfl + · exact (hR1 e).elim + · rw [e]; rfl + · rw [e]; rfl + have eτ : τ = σ1 := + (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst (hτ.symm.trans spec)))).2 + refine ⟨x1, by rw [eτ]; exact h1, fun _ _ => by rw [eτ], by rw [eτ], ⟨f2, ?_⟩, fun rg => ?_⟩ + · rw [ey2] + have : activatedStateA x.tcbState = x.tcbState := by + revert hR; cases x.tcbState <;> simp [activatedStateA] + rw [this] + · rw [same hR1] + exact regs1 rg + obtain ⟨x2, hx2, o2, rest2, tcbx, regs2⟩ := F + have spec : fl4ActivateThread (Z := Unit) σ = ({((), τ)}, False) := by + unfold fl4ActivateThread + rw [nd_gets_bindSc, htσ] + rw [← nd_bind_assocSc] + change (activationYieldA (specPtr t) >>= fun _ => fl4ActivateState (specPtr t)) σ = _ + rw [nd_bind_oneSc he] + exact hτ + refine ⟨τ, x2, spec, ?_, regs2, ww⟩ + rw [htσ] + refine ⟨hk, hx2, ?_, fun p hp => ?_, tcbx⟩ + · conv_lhs => rw [rest2, rest1] + · rw [o2 p hp] + exact other1 p hp + +/-- **`activate_thread`, exactly** (module docstring). -/ +theorem activate_thread_exactA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {σ : State Unit} (H : YieldQueueRelA (st := st) Z m κ s σ) (a : A) + (hC0 : ∀ c ∈ Z.C, c ≠ 0#u64) (htu : Fl4TicksToUsAgreeA ar a) + {x : Tcb} (hk : σ.kheap σ.curThread = some (.tcb x)) (legal : ActivateStateOKA x.tcbState) + (regs : ∀ r, (L.mem s (Endp.regA (L.curW s) (regIdxA r))).bv = x.tcbArch.tcbContext.userRegs r) : + sched.activate_thread st ar s a ⦃ r => r.2 = a ∧ ∃ τ x2, + fl4ActivateThread (Z := Unit) σ = ({((), τ)}, False) ∧ + YieldQueueRelA (st := st) Z m κ r.1 τ ∧ τ.curThread = σ.curThread ∧ + ActSpecA σ τ σ.curThread x x2 ∧ + (∀ rg, (L.mem r.1 (Endp.regA (L.curW s) (regIdxA rg))).bv = x2.tcbArch.tcbContext.userRegs rg) ∧ + WritesIn st s r.1 (fun b => MWrA (st := st) Z m σ s b ∨ ActWordA (L.curW s) b) ∧ + SchedFrameA σ τ ∧ WritesIn st s r.1 (SchedStepWA (st := st) Z m σ s) ⦄ := by + have pc : (L.mem s (regWA (L.curW s) layout.reg.FAULT_IP)).bv = x.tcbArch.tcbContext.userRegs .faultIP := by + have r := regs .faultIP + have e : regIdxA .faultIP = layout.reg.FAULT_IP := by unfold regIdxA layout.reg.FAULT_IP; rfl + rw [e] at r + exact r + refine WP.spec_mono (spec_andA (activate_thread_stepA H a hC0 htu hk legal pc) + (activate_walkA H a hC0 htu hk legal regs)) ?_ + rintro r ⟨⟨ea, τ, e1, H2, hC, -, SF, W⟩, τ', x2, e2, SP, rw', ww⟩ + have : τ' = τ := (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst (e2.symm.trans e1)))).2 + subst this + exact ⟨ea, τ', x2, e1, H2, hC, SP, rw', ww, SF, W⟩ + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3KEntryArmsA.lean b/proofs/Fl4ProofsA64/P3/Stage3KEntryArmsA.lean new file mode 100644 index 000000000..e96135ab5 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3KEntryArmsA.lean @@ -0,0 +1,157 @@ +/- +P3 (aarch64, A64-P3 Stage 3, kernel-entry composition, step 2): **user fault, VM fault, unknown syscall and interrupt +entry at the record**. Each is the `P3/OuterEntryA` theorem at `Rel := JStage3QA`, with the budget prefix from +`P3/Stage3KEntryBudgetRecA`. + +Named premises: +* `KEntryBudgetInA`: the record and the [J]/[P] premises of `JStage3A.budgetOkA` / `irqBudgetA`; +* [J] `hCharge` (open, `KEntryChargeA`): the exhausted-budget branch (`charge_budget … true`); +* [J] `deliver` (open): `faults.handle_fault` keeps the record (its `send_ipc` arms are Lift 2b's); +* [J] `handle` (open): `interrupt.handle_interrupt` keeps the record (IRQ service and the timer's fires loop); +* [J] `finish` (open): `syscall.finish`. +-/ +import Fl4ProofsA64.P3.Stage3KEntryBudgetRecA +import Fl4ProofsA64.P3.OuterEntryA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4SpecA64 + +noncomputable section + +section +variable {S : Type} {A : Type} + +/-- The record at kernel entry with the premises of the budget prefix (`P3/Stage3KEntryBudgetRecA`). -/ +structure KEntryBudgetInA (st : machine.Store S) [L : LawfulStore st] (ar : machine.Arch A) + (AR AR' : A → HwRootA → Prop) (Z : XSetsA) (H : Heap) (OS U Ac Pp Tb : Set U64) (Lv : U64 → Nat) (ia ie : Nat) + (N E Rp : Set U64) (Bf : Set (U64 × Fl4SpecA64.Word)) (stack : U64 → List U64) (κ : U64 → U64) (nA : U64) + (m : McState) (Clock : A → MachineState → Prop) (s : S) (a : A) (σ : State Unit) : Prop where + J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar nA)) s a σ m + sact : SActA σ + noSwitch : ∀ u, σ.schedulerAction ≠ .switchThread u + hvp : ∀ b, VsR (L.ksb s) (Pp ∪ Tb) b → Aligned b ∧ ¬ PPW st s b + P : NowAgreeA ar Clock + hClock : Clock a σ.machineState + hnd : numDomains = 1 + V : ValidMachineTimeA σ + C : CounterStepA σ + CI : CurInvA σ + R : OffReadyA σ + Act : ActIdleA σ + hNow : fl4ReadRawCounter σ.machineState = getCurrentTime σ.machineState + hAr : sched.update_timestamp st ar s a ⦃ r => AR' r.2 Z.hw ⦄ + hmb : ∀ a', Fl4MinBudgetAgreeA ar a' + entry : ChargeEntryA st Ac s + +/-- [J] (open) The exhausted-budget branch of `code` (`check_budget_restart` at syscall and fault entries, +`check_budget` at the interrupt entry): the record at the post state. -/ +def KEntryChargeA (st : machine.Store S) [LawfulStore st] (ar : machine.Arch A) (AR' : A → HwRootA → Prop) + (Z : XSetsA) (H : Heap) (OS U Ac Pp Tb : Set U64) (Lv : U64 → Nat) (ia ie : Nat) (N E Rp : Set U64) + (Bf : Set (U64 × Fl4SpecA64.Word)) (stack : U64 → List U64) (κ : U64 → U64) (nA : U64) (m : McState) + (Clock : A → MachineState → Prop) (s : S) (a : A) (σ : State Unit) + (code : S → A → Result (Bool × S × A)) (spec : Fl4Spec.NonDet (State Unit) Bool) : Prop := + ∀ s1 a1 σ1, fl4UpdateTimestamp (Z := Unit) σ = ({((), σ1)}, False) → + JStage3A st AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar nA)) s1 a1 σ1 m → ChargeEntryA st Ac s1 → + ValidMachineTimeA σ1 → CurInvA σ1 → OffReadyA σ1 → ActIdleA σ1 → Clock a1 σ1.machineState → + fl4GetScRefillSufficient (Z := Unit) σ1.curSc σ1.consumedTime σ1 = ({(false, σ1)}, False) → + code s1 a1 ⦃ r => ∃ τ, spec σ1 = ({(r.1, τ)}, False) ∧ + JStage3QA st ar AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes nA m + r.2.1 r.2.2 τ ⦄ + +end + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {ar : machine.Arch A} +variable {AR AR' : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {nA : U64} {s : S} {a : A} {σ : State Unit} {m : McState} + {Final : S → A → State Unit → Prop} + +local notation "RecA" => JStage3QA st ar AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ + fl4A64Platform.numNodes nA m + +variable {Clock : A → MachineState → Prop} + +local notation "InA" => KEntryBudgetInA st ar AR AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ nA m Clock s a σ + +local notation "ChargeA" => KEntryChargeA st ar AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ nA m Clock s a σ + +private theorem budgetA + (I : KEntryBudgetInA st ar AR AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ nA m Clock s a σ) + (hCharge : KEntryChargeA st ar AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ nA m Clock s a σ + (sched.check_budget_restart st ar) fl4CheckBudgetRestart) : + syscall.budget_ok st ar s a ⦃ r => ∃ τ, + fl4BudgetOk (Z := Unit) σ = ({(r.1, τ)}, False) ∧ + JStage3QA st ar AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes nA m + r.2.1 r.2.2 τ ⦄ := + I.J.budgetOkA I.sact I.noSwitch I.hvp I.P I.hClock I.hnd I.V I.C I.CI I.R I.Act I.hNow I.hAr I.hmb I.entry hCharge + +/-- **User exception entry at the record.** -/ +theorem userException_recA (number code : U64) (limit : Nat) (view : State Unit → Fl4TimerView) + (I : InA) (hCharge : ChargeA (sched.check_budget_restart st ar) fl4CheckBudgetRestart) + (deliver : ∀ s' a' τ, syscall.budget_ok st ar s a = ok (true, s', a') → + fl4BudgetOk (Z := Unit) σ = ({(true, τ)}, False) → RecA s' a' τ → + (do let t ← sched.cur_thread st s'; faults.handle_fault st ar s' a' t (.UserException number code)) ⦃ r => + StepPostA RecA (do let t ← Fl4Spec.NonDet.gets fun k : State Unit => k.curThread + handleFault t (.userException number.bv code.bv)) τ r ⦄) + (finish : ∀ s' a' τ, ((), τ) ∈ (fl4BudgetFault (Z := Unit) (.userException number.bv code.bv) σ).1 → + RecA s' a' τ → syscall.finish st ar s' a' ⦃ r => StepPostA Final (fl4Finish limit view) τ r ⦄) : + syscall.handle_user_exception st ar s a number code ⦃ r => + StepPostA Final (fl4BudgetFault (.userException number.bv code.bv) >>= fun _ => fl4Finish limit view) σ r ⦄ := + userExceptionEntry_refinesA number code limit view (budgetA I hCharge) deliver finish + +/-- **VM fault entry at the record.** -/ +theorem vmFault_recA (addr esr : U64) (limit : Nat) (view : State Unit → Fl4TimerView) + (I : InA) (hCharge : ChargeA (sched.check_budget_restart st ar) fl4CheckBudgetRestart) + (deliver : ∀ s' a' τ, syscall.budget_ok st ar s a = ok (true, s', a') → + fl4BudgetOk (Z := Unit) σ = ({(true, τ)}, False) → RecA s' a' τ → + (do let t ← sched.cur_thread st s' + faults.handle_fault st ar s' a' t (.VmFault addr esr (decide (((esr.bv >>> 26) &&& 63) = 32)))) ⦃ r => + StepPostA RecA (do let t ← Fl4Spec.NonDet.gets fun k : State Unit => k.curThread + handleFault t (fl4VmFault addr.bv esr.bv)) τ r ⦄) + (finish : ∀ s' a' τ, ((), τ) ∈ (fl4BudgetFault (Z := Unit) (fl4VmFault addr.bv esr.bv) σ).1 → + RecA s' a' τ → syscall.finish st ar s' a' ⦃ r => StepPostA Final (fl4Finish limit view) τ r ⦄) : + syscall.handle_vm_fault st ar s a addr esr ⦃ r => + StepPostA Final (fl4BudgetFault (fl4VmFault addr.bv esr.bv) >>= fun _ => fl4Finish limit view) σ r ⦄ := + vmFaultEntry_refinesA addr esr limit view (budgetA I hCharge) deliver finish + +/-- **Unknown syscall entry at the record.** -/ +theorem unknownSyscall_recA (number : U64) (limit : Nat) (view : State Unit → Fl4TimerView) + (I : InA) (hCharge : ChargeA (sched.check_budget_restart st ar) fl4CheckBudgetRestart) + (deliver : ∀ s' a' τ, syscall.budget_ok st ar s a = ok (true, s', a') → + fl4BudgetOk (Z := Unit) σ = ({(true, τ)}, False) → RecA s' a' τ → + (do let t ← sched.cur_thread st s'; faults.handle_fault st ar s' a' t (.UnknownSyscall number)) ⦃ r => + StepPostA RecA (do let t ← Fl4Spec.NonDet.gets fun k : State Unit => k.curThread + handleFault t (.unknownSyscallException number.bv)) τ r ⦄) + (finish : ∀ s' a' τ, ((), τ) ∈ (fl4BudgetFault (Z := Unit) (.unknownSyscallException number.bv) σ).1 → + RecA s' a' τ → syscall.finish st ar s' a' ⦃ r => StepPostA Final (fl4Finish limit view) τ r ⦄) : + syscall.unknown_syscall_fault st ar s a number false ⦃ r => r.1 = false ∧ + StepPostA Final (fl4BudgetFault (.unknownSyscallException number.bv) >>= fun _ => fl4Finish limit view) σ r.2 ⦄ := + unknownSyscallFault_refinesA number limit view (budgetA I hCharge) deliver finish + +/-- **Interrupt entry at the record.** -/ +theorem interrupt_recA (irq : U64) (limit : Nat) (view : State Unit → Fl4TimerView) (service : SMonad Unit Unit) + (I : InA) (hCharge : ChargeA (sched.check_budget st ar) fl4CheckBudget) + (handle : ∀ b s' a' τ, + (do let (s0, a0) ← sched.update_timestamp st ar s a; sched.check_budget st ar s0 a0) = + ok (b, s', a') → + ((fl4UpdateTimestamp (Z := Unit)) >>= fun _ => fl4CheckBudget) σ = ({(b, τ)}, False) → + RecA s' a' τ → interrupt.handle_interrupt st ar s' a' irq ⦃ r => StepPostA RecA service τ r ⦄) + (finish : ∀ s' a' τ, ((), τ) ∈ (fl4AcceptedIrqBody service σ).1 → + RecA s' a' τ → syscall.finish st ar s' a' ⦃ r => StepPostA Final (fl4Finish limit view) τ r ⦄) : + syscall.handle_interrupt_entry st ar s a irq ⦃ r => + StepPostA Final (fl4AcceptedIrqBody service >>= fun _ => fl4Finish limit view) σ r ⦄ := + interruptEntry_refinesA irq limit view service + (I.J.irqBudgetA I.sact I.noSwitch I.hvp I.P I.hClock I.hnd I.V I.C I.CI I.R I.Act I.hNow I.hAr I.hmb I.entry hCharge) + handle finish + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3KEntryBudgetA.lean b/proofs/Fl4ProofsA64/P3/Stage3KEntryBudgetA.lean new file mode 100644 index 000000000..9acb76144 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3KEntryBudgetA.lean @@ -0,0 +1,303 @@ +/- +P3 (aarch64, A64-P3 Stage 3, kernel-entry composition, Plan A, item 2): **the body's exports across `timer::fire`**. + +`BodyExitA σ` is what a kernel body owes `syscall.finish` besides the record and `SwA` (Architect, 2026-12-04: G-A, +G-C; to become exports of `DispPostA`, Plan B): the clock bound `ValidMachineTimeA`, the current SC's `OffReadyA` / +`OffOKA` when it is the idle SC or active, and, when resuming, a current thread in a state `activate_thread` accepts. + +* `sendSignal_ctStA`: a signal keeps the current thread's state when it is one `activate_thread` accepts (the woken + thread was blocked, so it is another one). +* `BodyExitA.signal`: one `send_signal` keeps `BodyExitA`. +* `JStage3A.fire_budgetA`: `fire_recA` with `BodyExitA` carried through the fires loop. + +Named premises: [J] `BodyExitA σ` at the body's state; `hcore`, `hSig` as `fire_recA`'s. + +New module. +-/ +import Fl4ProofsA64.P3.Stage3KEntrySchedA +import Fl4ProofsA64.P3.Stage3SigBudgetA +import Fl4ProofsA64.P3.Stage3BodyOKA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3 + Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Classical + +noncomputable section + +theorem activate_not_blockedA {ts : ThreadState} (h : ActivateStateOKA ts) : + BlockedTsA ts = false ∧ receiveBlocked ts = false := by + rcases h with h | h | h | h <;> rw [h] <;> exact ⟨rfl, rfl⟩ + +/-- A signal keeps the state of a thread in a state `activate_thread` accepts (any outcome). -/ +theorem sendSignal_ctStMA (n : ObjRef) (badge : Badge) (σ0 : State Unit) {c : ObjRef} {x0 : Tcb} + (hc : σ0.kheap c = some (.tcb x0)) (ok0 : ActivateStateOKA x0.tcbState) + (hI : FIA σ0) (hD : RqDomPrio σ0) (hid : IdleSt σ0) (hQ : SigQ n σ0) + {σ : State Unit} (mem : ((), σ) ∈ (sendSignal (Z := Unit) n badge σ0).1) : + ∀ x, σ.kheap c = some (.tcb x) → x.tcbState = x0.tcbState := by + have T : HT (fun s => s = σ0) (sendSignal (Z := Unit) n badge) + (fun _ s => ∀ x, s.kheap c = some (.tcb x) → x.tcbState = x0.tcbState) := by + have same : ∀ (s : State Unit), (∀ p y, s.kheap p = some (.tcb y) → ∃ x, σ0.kheap p = some (.tcb x) ∧ + y.tcbState = x.tcbState) → ∀ x, s.kheap c = some (.tcb x) → x.tcbState = x0.tcbState := by + intro s k x hx + obtain ⟨x1, h1, e⟩ := k c x hx + rw [hc] at h1 + rw [e, KernelObject.tcb.inj (Option.some.inj h1)] + have other : ∀ (d : ObjRef) (s : State Unit), KI d σ0 s → c ≠ d → + ∀ x, s.kheap c = some (.tcb x) → x.tcbState = x0.tcbState := by + intro d s k ne x hx + obtain ⟨x1, h1, e⟩ := k.1 c x hx ne + rw [hc] at h1 + rw [e, KernelObject.tcb.inj (Option.some.inj h1)] + unfold sendSignal + refine HT.bind (Q := fun ntfn s => s = σ0 ∧ s.kheap n = some (.notification ntfn)) ?_ fun ntfn => ?_ + · intro s hs r hr + obtain ⟨hk, es⟩ := getNotification_mem hr + obtain ⟨a, s'⟩ := r + simp only at hk es ⊢ + subst es + exact ⟨hs, hk⟩ + have ns : ∀ (o : Notification), HT (fun s => s = σ0 ∧ s.kheap n = some (.notification ntfn)) + (setNotification (Z := Unit) n o) (fun _ s => ∀ x, s.kheap c = some (.tcb x) → x.tcbState = x0.tcbState) := by + intro o s hs r hr + have k := (by tpres : TPres (setNotification (Z := Unit) n o)) s r hr + refine same _ fun p y hy => ?_ + obtain ⟨-, -, -, -, -, kb, -⟩ := k + obtain ⟨x, hx, -, e⟩ := kb p y hy + exact ⟨x, hs.1 ▸ hx, e⟩ + rcases ntfn with ⟨o, bt, nsc⟩ + cases o with + | idleNtfn => + cases bt with + | none => exact ns _ + | some tcb => + dsimp only + refine HT.bind (Q := fun ts s => (s = σ0 ∧ s.kheap n = + some (.notification { ntfnObj := .idleNtfn, ntfnBoundTcb := some tcb, ntfnSc := nsc })) ∧ + ∃ x, s.kheap tcb = some (.tcb x) ∧ x.tcbState = ts) ?_ fun ts => ?_ + · show HT _ (threadGet (Z := Unit) Tcb.tcbState tcb) _ + exact HT.threadGet _ tcb fun s hs x hx => ⟨hs, x, hx, rfl⟩ + refine HT.ite (fun hr => ?_) (fun _ => HT.mono (ns _) (fun s h => h.1) (fun _ _ h => h)) + cases ts with + | blockedOnReceive ep rp d => + intro s hs r hr' + obtain ⟨⟨es, -⟩, x, hx, hst⟩ := hs + subst es + have ne : c ≠ tcb := fun e => by + rw [← e, hc] at hx + rw [← KernelObject.tcb.inj (Option.some.inj hx)] at hst + rw [hst] at ok0 + have := (activate_not_blockedA ok0).2 + cases this + have hBA : BAs [tcb] s := fun u hu y sc e hsc => by + have hu' : u = tcb := List.mem_singleton.1 hu + subst hu' + rw [hx] at e + have := KernelObject.tcb.inj (Option.some.inj e) + subst this + obtain ⟨y', k, ey, ea⟩ := hI.1.blk u x sc hx (by rw [hst]; rfl) hsc + exact ⟨x, sc, hx, hsc, y', k, ey, ea⟩ + have hrecv : RecvSt tcb s := fun y hy => by + rw [hx] at hy + have := KernelObject.tcb.inj (Option.some.inj hy) + subst this + exact ⟨ep, rp, d, hst⟩ + obtain ⟨⟨u, s1⟩, h1, h2⟩ := mem_bindA.1 hr' + have hC := cancelIpcRecv_fia tcb s s ⟨hI, hD, ⟨KI.refl tcb s, hBA⟩, hrecv⟩ ⟨u, s1⟩ h1 + obtain ⟨-, hKI⟩ := wakeTail_fia tcb n badge s s1 hC r h2 + exact other tcb _ hKI ne + | _ => simp [receiveBlocked] at hr + | waitingNtfn q => + dsimp only + rcases q with _ | ⟨hd, tl⟩ + · intro s _ r hr + unfold updateWaitingNtfn at hr + obtain ⟨x, hx, -⟩ := mem_bindA.1 hr + exact absurd rfl hx.1 + · intro s hs r hr + obtain ⟨es, hk⟩ := hs + subst es + obtain ⟨y, hy, hst⟩ := hQ _ (hd :: tl) hd hk rfl (List.mem_cons_self ..) + have hb : ∀ x, s.kheap (the (hd :: tl).head?) = some (.tcb x) → BlockedTsA x.tcbState = true := fun x hx => by + change s.kheap hd = some (.tcb x) at hx + rw [hy] at hx + have := KernelObject.tcb.inj (Option.some.inj hx) + subst this + rw [hst]; rfl + have ne : c ≠ hd := fun e => by + rw [← e, hc] at hy + rw [← KernelObject.tcb.inj (Option.some.inj hy)] at hst + rw [hst] at ok0 + have := (activate_not_blockedA ok0).1 + cases this + obtain ⟨-, hKI⟩ := updateWaitingNtfn_fia n (hd :: tl) badge s s ⟨rfl, hI, hD, List.cons_ne_nil _ _, hb⟩ r hr + exact other hd _ hKI ne + | activeNtfn b => exact ns _ + exact T σ0 rfl _ mem + +/-- A signal keeps the state of a thread in a state `activate_thread` accepts. -/ +theorem sendSignal_ctStA (n : ObjRef) (badge : Badge) (σ0 : State Unit) {c : ObjRef} {x0 : Tcb} + (hc : σ0.kheap c = some (.tcb x0)) (ok0 : ActivateStateOKA x0.tcbState) + (hI : FIA σ0) (hD : RqDomPrio σ0) (hid : IdleSt σ0) (hQ : SigQ n σ0) + {σ : State Unit} (h : sendSignal (Z := Unit) n badge σ0 = ({((), σ)}, False)) : + ∀ x, σ.kheap c = some (.tcb x) → x.tcbState = x0.tcbState := + sendSignal_ctStMA n badge σ0 hc ok0 hI hD hid hQ (by rw [h]; exact Set.mem_singleton _) + +/-- **One `send_signal` keeps `BodyExitA`.** -/ +theorem BodyExitA.signal {n : ObjRef} {badge : Badge} {σ σ' : State Unit} (B : BodyExitA σ) + (hI : FIA σ) (hD : RqDomPrio σ) (hid : IdleSt σ) (hQ : SigQ n σ) (hct : ∃ x, σ.kheap σ.curThread = some (.tcb x)) + (h : sendSignal (Z := Unit) n badge σ = ({((), σ')}, False)) : BodyExitA σ' := by + have mem : ((), σ') ∈ (sendSignal (Z := Unit) n badge σ).1 := by rw [h]; exact Set.mem_singleton _ + have K : BKeepA σ σ' := sendSignal_bkA n badge σ _ mem + refine ⟨?_, fun y' k hy hc g => ?_, fun ha x hx => ?_⟩ + · unfold ValidMachineTimeA + rw [K.time, K.ms] + exact B.validTime + · rw [K.csc] at hy hc + obtain ⟨y, h0, er, em⟩ := K.sc y' k hy + have act : σ.curSc = idleScPtr ∨ scActive y = true := hc.imp_right fun a => by + unfold scActive at a ⊢; rw [← em]; exact a + obtain ⟨R, O⟩ := B.budget y k h0 act (g.imp (fun h => by rwa [K.used] at h) K.act) + have hd : refillHd y' = refillHd y := by unfold refillHd; rw [er] + refine ⟨fun z j hz => ?_, fun hu z j hz _ => ?_⟩ + · rw [K.csc, hy] at hz + obtain ⟨rfl, -⟩ := KernelObject.schedContext.inj (Option.some.inj hz) + rw [hd, K.used, K.time] + exact R y k h0 + · rw [K.csc, hy] at hz + obtain ⟨rfl, -⟩ := KernelObject.schedContext.inj (Option.some.inj hz) + rw [K.used] at hu ⊢ + rw [hd, K.time] + exact O hu y k h0 fun ne => act.resolve_left ne + · obtain ⟨x0, h0⟩ := hct + have ok0 := B.ctAct (K.act ha) x0 h0 + rw [K.ct] at hx + rw [sendSignal_ctStA n badge σ h0 ok0 hI hD hid hQ h x hx] + exact ok0 + +/-- A reprogram request changes nothing `BodyExitA` reads. -/ +theorem BodyExitA.repr {σ : State Unit} (B : BodyExitA σ) : BodyExitA { σ with reprogramTimer := true } := + ⟨B.validTime, B.budget, B.ctAct⟩ + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {nA : U64} {s : S} {a : A} {σ : State Unit} {m : McState} + +/-- A record state's ready queues hold each thread at its own domain and priority. -/ +theorem JStage3A.rqDomPrioA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) : RqDomPrio σ := by + intro f d p x hx hm + by_cases hd : d = 0 + swap + · rw [J.sr.rest.rqDom d p hd] at hm; cases hm + subst hd + have hpv : (⟨BitVec.ofNat 64 p.toNat⟩ : U64).val = p.toNat := by + show (BitVec.ofNat 64 p.toNat).toNat = p.toNat + rw [BitVec.toNat_ofNat] + exact Nat.mod_eq_of_lt (by have := p.isLt; omega) + have hp256 : (⟨BitVec.ofNat 64 p.toNat⟩ : U64).val < 256 := by rw [hpv]; exact p.isLt + have e := J.sr.rest.rq ⟨BitVec.ofNat 64 p.toNat⟩ hp256 + rw [hpv, BitVec.ofNat_toNat, BitVec.setWidth_eq] at e + rw [e, List.mem_map] at hm + obtain ⟨u, hu, rfl⟩ := hm + have uT := J.sr.core.memT J.sr.sch.cpuCs hp256 hu + have pw := (J.sr.core.inQ _ J.sr.sch.cpuCs _ hp256 _ hu).2 + obtain ⟨x', hx', dom, -, pr⟩ := J.sr.rest.prio u uT + rw [hx] at hx' + obtain rfl := KernelObject.tcb.inj (Option.some.inj hx') + refine ⟨dom.symm, ?_⟩ + apply BitVec.eq_of_toNat_eq + rw [← pr, pw, hpv] + +/-- **`fire_recA` with the body's exports carried through the fires loop** (module docstring). -/ +theorem JStage3A.fire_budgetA (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) + s a σ m) + (hcore : m.curCore < fl4A64Platform.numNodes) (hSw : SwA σ) (B : BodyExitA σ) + (hSig : ∀ (Q : U64 → U64 → List U64) (Rl : U64 → List U64) (s : S) (a : A) (σ : State Unit) (t badge : U64), + JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s a σ m → + SwA σ → t ∈ Z.T → L.mem s (boundA t) ≠ 0#u64 → + notification.send_signal st ar s a (L.mem s (boundA t)) badge ⦃ r => + ∃ σ' b Q' Rl', sendSignal (Z := Unit) (specPtr (L.mem s (boundA t))) b σ = ({((), σ')}, False) ∧ + JStage3A st AR { Z with Q := Q', R := Rl' } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) r.1 r.2 σ' m ⦄) : + timer.fire st ar s a ⦃ r => ∃ σ1 Q Rl, fl4TimerFires σ σ1 ∧ + JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) r.1 r.2 σ1 m ∧ FireRecA σ1 ∧ BodyExitA σ1 ⦄ := by + obtain ⟨Pt, Ub, Pp', Tb', Hr, Te, Kt, Lv', Re, bits, KLv, W⟩ := J7A.unp J.j7 + have I := J.delIn hA hPl W + have hc0 : 0#u64 ∈ Z.Cs := by + obtain ⟨c, hc, e⟩ := J.coresAll 0 (Nat.lt_of_le_of_lt (Nat.zero_le _) hcore) + have : c = 0#u64 := UScalar.eq_of_val_eq (by rw [e]; rfl) + exact this ▸ hc + have hks := J.ko + have W0 : SW st H Z.Cs OS Z.V ia ie Ac s s := SW.start J.sys.sysl J.symL J.blkA + let Inv : S → A → State Unit → Prop := fun s1 a1 σ1 => + (∃ Q Rl, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s1 a1 σ1 m) ∧ SwA σ1 ∧ SW st H Z.Cs OS Z.V ia ie Ac s s1 ∧ BodyExitA σ1 + have h := timer.fire_recA (st := st) (ar := ar) (T := Z.T) (Inv := Inv) ?_ ?_ ?_ ?_ s a σ + ⟨⟨Z.Q, Z.R, J⟩, hSw, W0, B⟩ + · refine WP.spec_mono h fun r ⟨σ1, hf, ⟨Q, Rl, J1⟩, sw1, _, B1⟩ => ?_ + exact ⟨σ1, Q, Rl, hf, J1, JStage3A.fireRecA J1 sw1, B1⟩ + · rintro s1 a1 σ1 ⟨⟨Q, Rl, J1⟩, -, -⟩ h0 + exact JStage3A.timerMinA (Z := { Z with Q := Q, R := Rl }) J1 h0 + · rintro s1 a1 σ1 t ⟨⟨Q, Rl, J1⟩, sw1, W1, B1⟩ ht + have b1 := W1.1.blk + have h0 := W1.1.sys.ly.h0 + have htH : t ∈ H.tcbs := by rw [J1.tcbs]; exact ht + have SW1 : timer.remove st s1 t ⦃ s2 => SW st H Z.Cs OS Z.V ia ie Ac s s2 ⦄ := + WP.spec_mono (timer.remove.dmd 0#u64 t b1 h0 W1.1.sym W1.1.sys.vr htH) fun s2 ⟨d, b2⟩ => + W1.dmd0 I hc0 hks d b2 + have R := J1.timerRm_recA ht (WP.spec_mono SW1 fun _ w => ⟨w.1.sys, w.1.sym⟩) + refine WP.spec_mono (WP.spec_and R SW1) fun s2 ⟨⟨J2, _⟩, W2⟩ => ?_ + exact ⟨⟨⟨Q, Rl, J2⟩, sw1, W2, B1⟩, trivial⟩ + · rintro s1 a1 σ1 t badge ⟨⟨Q, Rl, J1⟩, sw1, W1, B1⟩ ht hb + have b1 := W1.1.blk + have h0 := W1.1.sys.ly.h0 + have htH : t ∈ H.tcbs := by rw [J1.tcbs]; exact ht + have r1 : RunVcpuInv st H.tcbs s1 := J1.tcbs ▸ J1.runVcpu + have hn : L.mem s1 (boundA t) ∈ H.ntfns := b1.2.2.1.2.2.bnd t htH hb + have hH : H (L.mem s1 (boundA t)) = some .ntfn := hn + have bt : L.mem s1 (ntfnBtA (L.mem s1 (boundA t))) = t := b1.2.2.1.2.2.bs.tb t htH hb + refine WP.spec_mono (WP.spec_and (hSig Q Rl s1 a1 σ1 t badge J1 sw1 ht hb) + (WP.spec_and (notification.send_signal.dmd hA a1 _ badge b1 hn hH h0 W1.1.sym W1.1.sys.vr + (fun y hy hb => r1.blocked hy hb)) + (notification.send_signal.tinv0 hA s1 a1 _ badge b1 hn hH r1))) ?_ + rintro ⟨s3, a3⟩ ⟨⟨σ', bd, Q', Rl', hs, J3⟩, hd⟩ + simp only [WP.uncurry'_pair] at hd J3 ⊢ + obtain ⟨d3, b3⟩ := hd + have hQ : SigQ (specPtr (L.mem s1 (boundA t))) σ1 := JStage3A.sigQA J1 (J1.ipcHeapCov.ntfn _ hH) + have sw3 : SwA σ' := sendSignal_swa _ bd σ1 ⟨sw1, hQ⟩ ((), σ') (by rw [hs]; rfl) + have R1 := JStage3A.fireRecA J1 sw1 + have B3 : BodyExitA σ' := B1.signal R1.1 (JStage3A.rqDomPrioA J1) R1.2.1 hQ J1.sinv.curTcb hs + refine ⟨σ', ⟨_, bd, hs⟩, ⟨Q', Rl', J3⟩, sw3, W1.dmd I hc0 hks (fun k hk hia => ?_) d3 b3, B3⟩ + rw [hH] at hk; cases hk + exact W1.1.sys.ln _ .ntfn hH (LiveQ.ntfn hH (Or.inr (Or.inl (by rw [bt]; exact ne0_of_heap h0 htH)))) hia + · rintro s1 a1 σ1 c ⟨⟨Q, Rl, J1⟩, sw1, W1, B1⟩ hc + have b1 := W1.1.blk + have h0 := W1.1.sys.ly.h0 + have ec : c = L.cpu s1 := UScalar.eq_of_val_eq hc + step with obj.node_set.spec as ⟨s4, es4⟩ + subst es4 ec + have rp : layout.node.REPROGRAM.val = 56 := by simp [layout.node.REPROGRAM] + obtain ⟨Q1, R1, q⟩ := b1.1 + obtain ⟨d4, b4⟩ := nodeW_step 0#u64 b1 h0 W1.1.sym W1.1.sys.vr (by rw [rp]; unfold LowFree; omega) + (by rw [rp]; omega) (by rw [rp]; omega) + (writesIn_wr (nodeAt_aligned (q.nodes _ q.cur) (by rw [rp]; omega) (by rw [rp]))) + have W4 := W1.dmd0 I hc0 hks d4 b4 + have rv : RvSt st Z.T s1 (L.wr s1 (L.nodeAt s1 (L.cpu s1) layout.node.REPROGRAM) 1#u64) := fun y hy => by + have hyH : y ∈ H.tcbs := by rw [J1.tcbs]; exact hy + constructor + · rw [stA_eq, field_wr_node q q.cur (by rw [rp]; omega) (by rw [rp]) hyH (by simp) (by simp)] + · rw [show runVcpuA y = core.num.U64.wrapping_add y 3672#u64 from rfl, + field_wr_node q q.cur (by rw [rp]; omega) (by rw [rp]) hyH (by simp) (by simp)] + exact ⟨⟨Q, Rl, J1.reprog_recA W4.1.blk W4.1.sys W4.1.sym rv⟩, sw1, W4, B1.repr⟩ + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3KEntryBudgetRecA.lean b/proofs/Fl4ProofsA64/P3/Stage3KEntryBudgetRecA.lean new file mode 100644 index 000000000..b6f0a3698 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3KEntryBudgetRecA.lean @@ -0,0 +1,425 @@ +/- +P3 (aarch64, A64-P3 Stage 3, kernel-entry composition, step 2): **the kernel-entry budget path at the record**. + +* `JStage3A.timestampA`: `sched.update_timestamp` keeps the record; the arch relation may change from `AR` to `AR'` + (Architect 03:10Z: the clock steps `MR` outside the record; instantiate `AR' := withMRA AR0 MR σ1.machineState`). +* `JStage3A.irqBudgetA`: the same for the interrupt entry's `check_budget` prefix. +* `JStage3A.budgetOkA`: `syscall.budget_ok` (timestamp, `check_budget_restart`) keeps the record. The sufficient + branch is proved; the other branch (`charge_budget`) is the named plug-in `hCharge`. + +Named premises: +* [J] `sact`, `noSwitch`: the entry's scheduler action. +* [J] `V`, `CI`, `R`, `Act`: the entry's timing facts (`JStage3EntryA`). +* [J] `hvp`: no VSpace word of `Pp ∪ Tb`, and no ASID-table word, is a preemption-point word. +* [P] `P`, `hClock`, `C`, `hNow`, `hAr`, `hnd`, `hmb`: the clock, minimum budget and single domain. +* [J] `hCharge` (open): the record across `check_budget_restart` when the current SC's head refill does not suffice. + +The proof of `timestampA` is `UtRstInvA.timestampA`'s record part (`P3/UtPpTsA`). New module. +-/ +import Fl4ProofsA64.P3.UtPpTsA +import Fl4ProofsA64.P3.MachKeepA +import Fl4ProofsA64.P3.ScCheckA +import Fl4ProofsA64.P3.ScEntryBudgetA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +set_option maxRecDepth 100000 + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {ar : machine.Arch A} +variable {AR AR' : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s : S} {a : A} {σ : State Unit} {m : McState} + +/-- **The entry facts a budget charge reads** (A64 P1's `sched.charge_budget.rpw`; the previous exit's `schedule`, G4): +the action is `resume`, the current thread is not in `TS_RUNNING_VM`, the current SC is active, and a runnable current +thread has the current SC as its SC and is in no release queue (the syscall-entry record: `JSyscallA.chargeEntryA`, +`P3/Stage3KEntryRestartA`). An idle current thread needs the first three only. -/ +structure ChargeEntryA (st : machine.Store S) [L : LawfulStore st] (Ac : Set U64) (s : S) : Prop where + act : L.actionW s = 0#u64 + nvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM + cA : L.cscW s ∈ Ac + run : Fl4ProofsA64.P1.Runnable (L.mem s (stA (L.curW s))) → + (L.mem s (scA (L.curW s)) ≠ 0#u64 → L.cscW s = L.mem s (scA (L.curW s))) ∧ + (L.mem s (inqA (L.curW s)) &&& sched.Q_RELEASE) = 0#u64 ∧ L.mem s (scA (L.curW s)) ≠ 0#u64 + +set_option maxHeartbeats 2000000 in +/-- **`update_timestamp` keeps the record** (module docstring). -/ +theorem JStage3A.timestampA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (sact : SActA σ) (noSwitch : ∀ u, σ.schedulerAction ≠ .switchThread u) + (hvp : ∀ b, VsR (L.ksb s) (Pp ∪ Tb) b → Aligned b ∧ ¬ PPW st s b) + {Clock : A → MachineState → Prop} (P : NowAgreeA ar Clock) (hClock : Clock a σ.machineState) + (hnd : numDomains = 1) (V : ValidMachineTimeA σ) (C : CounterStepA σ) (CI : CurInvA σ) (R : OffReadyA σ) + (Act : ActIdleA σ) (hNow : fl4ReadRawCounter σ.machineState = getCurrentTime σ.machineState) + (hAr : sched.update_timestamp st ar s a ⦃ r => AR' r.2 Z.hw ⦄) : + sched.update_timestamp st ar s a ⦃ r => ∃ σ1, fl4UpdateTimestamp (Z := Unit) σ = ({((), σ1)}, False) ∧ + JStage3A st AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) r.1 r.2 σ1 m ∧ + SActA σ1 ∧ (∀ u, σ1.schedulerAction ≠ .switchThread u) ∧ + ValidMachineTimeA σ1 ∧ CurInvA σ1 ∧ OffReadyA σ1 ∧ ActIdleA σ1 ∧ Clock r.2 σ1.machineState ∧ + (ChargeEntryA st Ac s → ChargeEntryA st Ac r.1) ⦄ := by + have J0 := J.arSwapA (AR' := fun _ _ => True) trivial + have E0 := J0.yieldQueueA + refine WP.spec_mono (WP.spec_and (WP.spec_and (WP.spec_and (update_timestamp_refinesA E0 a P hClock hnd V C CI R Act) + (update_timestamp_wordsA E0.sched.toSchedSRN a P hClock)) hAr) + ((J0.toTA sact noSwitch).timestampA (fun b hb => hvp b (hb.imp (ptw_mono utVsPtA_sub) id)) P hClock hnd V C hNow + (CurEntryGA.of_cur CI R J.sr.inv) + (by + refine WP.spec_mono (WP.spec_and hAr hAr) ?_ + intro _ _; trivial))) ?_ + rintro ⟨s', a'⟩ ⟨⟨⟨⟨τ, hτ, E1, hCl, V1, CI1, R1, Act1, -, -, F⟩, now, -, -, es⟩, har⟩, σT, memT, JT, -, -, -⟩ + dsimp only at hτ E1 hCl F es har memT JT ⊢ + have eτ := fl4UpdateTimestamp_eqA σ + rw [hτ] at eτ + have eτ' : τ = fl4TimestampState (rawNextA σ.machineState) { σ with machineState := rawStateA σ.machineState } := + (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst eτ))).2 + have mem : ((), τ) ∈ (updateTimeStamp (Z := Unit) σ).1 := by + rw [← fl4UpdateTimestamp_eq_l4vA σ hnd hNow, hτ] + exact Set.mem_singleton _ + have kh : τ.kheap = σ.kheap := by rw [eτ']; rfl + have um : τ.machineState.underlyingMemory = σ.machineState.underlyingMemory := by rw [eτ']; rfl + have irqN : τ.interruptIrqNode = σ.interruptIrqNode := by rw [eτ']; rfl + have cdt : τ.cdt = σ.cdt := by rw [eτ']; rfl + have orig : τ.isOriginalCap = σ.isOriginalCap := by rw [eτ']; rfl + have HS : SpecSchedA σ τ := + { kh := fun q => Or.inl (by rw [kh]) + irqS := by rw [eτ']; rfl + irqN := irqN + asid := by rw [eτ']; rfl + vcur := by rw [eτ']; rfl + cdt := cdt + orig := orig } + have SSm : SchedSameA σ { τ with curTime := σ.curTime, consumedTime := σ.consumedTime } := + { kh := fun q ko _ => by rw [kh] + cur := by rw [eτ']; rfl + idle := by rw [eτ']; rfl + csc := by rw [eτ']; rfl + rq := by rw [eτ']; rfl + rls := by rw [eτ']; rfl + time := rfl + cons := rfl } + have SSz : SSzA σ τ := fun q y k h => ⟨y, by rw [← kh]; exact h⟩ + -- the two node words + have hc0 := J.sq.cur + have nd := J.sq.nodes _ hc0 + have d48 : layout.node.CUR_TIME.val < 4288 := by simp [layout.node.CUR_TIME] + have d40 : layout.node.CONSUMED.val < 4288 := by simp [layout.node.CONSUMED] + have v48 : layout.node.CUR_TIME.val = 48 := by unfold layout.node.CUR_TIME; rfl + have v40 : layout.node.CONSUMED.val = 40 := by unfold layout.node.CONSUMED; rfl + have a48 := nodeAt_aligned nd d48 (by rw [v48]) + have a40 := nodeAt_aligned nd d40 (by rw [v40]) + obtain ⟨tA, etA⟩ : ∃ b, b = L.nodeAt s (L.cpu s) layout.node.CUR_TIME := ⟨_, rfl⟩ + obtain ⟨cA, ecA⟩ : ∃ b, b = L.nodeAt s (L.cpu s) layout.node.CONSUMED := ⟨_, rfl⟩ + rw [← etA] at a48 + rw [← ecA] at a40 + obtain ⟨used, es'⟩ : ∃ used, s' = L.wr (L.wr s tA now) cA used := by + have es2 : s' = L.wr (L.wr s tA now) cA (wa (L.mem s (L.nodeA s layout.node.CONSUMED)) + (core.num.U64.wrapping_sub now (L.curTime s))) := by + rw [etA, ecA]; exact es + exact ⟨_, es2⟩ + have hw : WritesIn st s s' (fun b => b = tA ∨ b = cA) := by + rw [es'] + exact (writesIn_wr (st := st) (s := s) (v := now) a48).trans (writesIn_wr (st := st) (v := used) a40) + have kb : L.ksb s' = L.ksb s := hw.1 + have cpu : L.cpu s' = L.cpu s := hw.2.1 + have tv : tA.val = L.nodeBase s (L.cpu s) + 48 := by rw [etA, nodeAt_val nd d48, v48] + have cv : cA.val = L.nodeBase s (L.cpu s) + 40 := by rw [ecA, nodeAt_val nd d40, v40] + have hnb : L.nodeBase s (L.cpu s) = (L.ksb s).val + 223488 + (L.cpu s).val * 4288 := rfl + have memk : ∀ b : U64, Aligned b → b.val ≠ tA.val → b.val ≠ cA.val → L.mem s' b = L.mem s b := fun b hb n1 n2 => + hw.mem_eq hb (fun h => h.elim (fun e => n1 (by rw [e])) (fun e => n2 (by rw [e]))) + have hcpu128 : (L.cpu s).val < 128 := by + obtain ⟨k, -, hk, h⟩ := J.j.1.csN + have := (h _).1 hc0 + omega + -- the words written are node words, in no object + have hblk := BXI.blk J.sys.ent.bxi + have Lc := LvC.of_blk hblk + have inKs : ∀ b : U64, (b = tA ∨ b = cA) → (L.ksb s).val + 223488 ≤ b.val ∧ b.val < (L.ksb s).val + 772352 := by + rintro b (e | e) <;> rw [e] + · rw [tv, hnb]; omega + · rw [cv, hnb]; omega + have inKs2 : ∀ b : U64, (b = tA ∨ b = cA) → (L.ksb s).val + 223488 ≤ b.val ∧ b.val < (L.ksb s).val + 772352 ∧ + (b.val - (L.ksb s).val - 223488) % 4288 ≠ 120 ∧ (b.val - (L.ksb s).val - 223488) % 4288 ≠ 128 := by + rintro b (e | e) <;> rw [e] + · rw [tv, hnb]; refine ⟨by omega, by omega, by omega, by omega⟩ + · rw [cv, hnb]; refine ⟨by omega, by omega, by omega, by omega⟩ + have offT : ∀ b : U64, (b = tA ∨ b = cA) → ∀ t ∈ H.tcbs, b.val < t.val ∨ t.val + 4096 ≤ b.val := by + intro b hb t ht + have d := nd.disj t ht + have g := inKs b hb + rcases hb with e | e <;> rw [e] + · rw [tv]; unfold LawfulStore.nodeBase at d ⊢; omega + · rw [cv]; unfold LawfulStore.nodeBase at d ⊢; omega + have offK : ∀ q k, H q = some k → (∀ y, k ≠ .other y) → ∀ b : U64, q.val ≤ b.val → b.val < q.val + 2 ^ k.bits → + ¬ (b = tA ∨ b = cA) := by + intro q k hq ne b h1 h2 hb + have g := inKs b hb + rcases J.ko.1 q k hq ne with g' | g' <;> omega + have offW : ∀ b : U64, Aligned b → ¬ (b = tA ∨ b = cA) → L.mem s' b = L.mem s b := fun b hb n => hw.mem_eq hb n + have wS : WritesIn st s s' (SchedStepWA (st := st) Z m σ s) := + hw.mono fun b hb => Or.inl (Or.inr (Or.inl (Or.inr (Or.inr (Or.inr (inKs2 b hb)))))) + have osk : OSKeep st OS s s' := J.osKeep_stepA wS + have tcbK : ∀ t ∈ Z.T, ∀ b : U64, Aligned b → t.val ≤ b.val → b.val < t.val + 4096 → L.mem s' b = L.mem s b := by + intro t ht b hb h1 h2 + have htH : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + refine offW b hb (fun h => ?_) + rcases offT b h t htH with g | g <;> omega + have tAl : ∀ t ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → Aligned (wa t K) := fun t ht K h1 h2 => + field_aligned (J.sr.core.tcbs t ht) h1 h2 + have tK : ∀ t ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → L.mem s' (wa t K) = L.mem s (wa t K) := by + intro t ht K h1 h2 + have f := (J.sr.core.tcbs t ht).fits + have ev : (wa t K).val = t.val + K.val := by rw [wa_valP]; exact Nat.mod_eq_of_lt (by omega) + exact tcbK t ht _ (tAl t ht K h1 h2) (by omega) (by omega) + -- the system invariants + have hN1 : L.wr s tA now = L.wr s (L.nodeAt s (L.cpu s) layout.node.CUR_TIME) now := by rw [etA] + have e2 : s' = L.wr (L.wr s tA now) (L.nodeAt (L.wr s tA now) (L.cpu (L.wr s tA now)) layout.node.CONSUMED) + used := by + rw [es', L.cpu_wr, nodeAt_of_ksb (L.ksb_wr _ _ _), ← ecA] + have lf48 : LowFree layout.node.CUR_TIME.val := by rw [v48]; unfold LowFree; omega + have lf40 : LowFree layout.node.CONSUMED.val := by rw [v40]; unfold LowFree; omega + have bx1 := nodew_bxc (o := layout.node.CUR_TIME) (v := now) J.sys.ent.bxi lf48 (by rw [v48]; omega) + (by rw [v48]; omega) + rw [← hN1] at bx1 + have bx2 := nodew_bxc (o := layout.node.CONSUMED) (v := used) bx1.1.1 lf40 (by rw [v40]; omega) + (by rw [v40]; omega) + rw [← e2] at bx2 + have blk2 := BXI.blk bx2.1.1 + have sq2 : SchedQueuesInv st H.tcbs Z.Cs H.omega H s' Z.Q Z.R := by + obtain ⟨-, hB, hX, hSg, -⟩ := J.sys.ent.bxi + have n1 := node_bx (o := layout.node.CUR_TIME) (v := now) J.sq hB hX hSg lf48 (by rw [v48]; omega) + (by rw [v48]; omega) + rw [← hN1] at n1 + obtain ⟨q1, hB1, hX1, hSg1, -⟩ := n1 + have n2 := node_bx (o := layout.node.CONSUMED) (v := used) q1 hB1 hX1 hSg1 lf40 (by rw [v40]; omega) + (by rw [v40]; omega) + rw [← e2] at n2 + exact n2.1 + have hKo : ∀ o : U64, o.val ∈ (∅ : Set Nat) → o.val < 224 → ∀ y ∈ OS, + ¬ (y.val ≤ (wa (L.ksb s) o).val ∧ (wa (L.ksb s) o).val < y.val + 32) := + fun _ h => absurd h (Set.notMem_empty _) + have dfw : ∀ b : U64, (b = tA ∨ b = cA) → + ∃ c ∈ Z.Cs, ∃ o : U64, o.val < 4288 ∧ o.val ≠ 88 ∧ o.val ≠ 8 ∧ b = L.nodeAt s c o := by + rintro b (e | e) + · exact ⟨_, hc0, _, d48, by rw [v48]; omega, by rw [v48]; omega, e.trans etA⟩ + · exact ⟨_, hc0, _, d40, by rw [v40]; omega, by rw [v40]; omega, e.trans ecA⟩ + have d : DF st H Z.Cs (L.curW s) ∅ s s' := by + refine ⟨hw.mono fun b hb => ?_, (LvF.ofCwN Lc hw fun b _ hb => ?_).mono (LvF.none_le _)⟩ + · obtain ⟨c, hc, o, o1, o2, -, e⟩ := dfw b hb + exact Or.inl (Or.inr (Or.inl ⟨c, hc, o, o1, o2, e⟩)) + · obtain ⟨c, hc, o, o1, o2, o3, e⟩ := dfw b hb + exact Or.inr ⟨c, hc, o, o1, o2, o3, s, rfl, e⟩ + have lvf : LvF st H.tcbs Z.Cs H.replies H.scs H.ntfns LvX.none s s' := by + refine LvF.ofCwN Lc hw fun b _ hb => ?_ + obtain ⟨c, hc, o, o1, o2, o3, e⟩ := dfw b hb + exact Or.inr ⟨c, hc, o, o1, o2, o3, s, rfl, e⟩ + obtain ⟨sysl2, Y2⟩ : SysL st H Z.Cs OS ia ie s' ∧ SymL st H Z.Cs s' := by + have hO : OSOK H.tcbs H.omega H.frame OS := J.sys.ent.os + obtain ⟨Q, R, q⟩ := J.sys.ent.bxi.blk.1 + have hSg := J.sys.ent.bxi.blk.2.2.2.2 + refine SysL.dfT (Bw := fun _ => False) J.sys.ent.heap hO J.sys.ent.bxi (Or.inr lvf) hKo (fun _ h => h.elim) + (fun _ _ _ _ _ h => h) J.sys.sysl J.symL (DFB.ofDF d) + (J.sys.sysl.ipc.keep (IbK.of_w q.tcbs d.w fun y hy hb => ?_)) + rcases hb with hk | ⟨o, -, hl, h8, e⟩ + · exact ib_not_kw8 q (fun c hc => absurd hc (Set.notMem_empty c)) hSg.sub + (fun e he => absurd he (Set.notMem_empty e)) hy (Or.inl hk) + · exact ib_not_ks q hSg hy (by omega) h8 e + + -- the record without `SysL` is the shared module's, at the same spec outcome + have eT : σT = τ := by + have e : (updateTimeStamp (Z := Unit) σ).1 = {((), τ)} := by + rw [← fl4UpdateTimestamp_eq_l4vA σ hnd hNow, hτ] + rw [e] at memT + exact (Prod.mk.inj (Set.mem_singleton_iff.1 memT)).2 + rw [eT] at JT + have X := utTA_extraA J0 JT kh + have sv : SchedValidA τ := J.schedValid.congr SSm.rq SSm.rls (by rw [eτ']; rfl) SSm.cur SSm.idle SSm.csc + (fun q y k h => by rw [kh]; exact h) (fun t y h => ⟨y, by rw [kh]; exact h, rfl⟩) + (fun t y h => ⟨y, by rw [← kh]; exact h, rfl, rfl⟩) + have K6 := utRec6_keepA J (s' := s') (σ' := τ) (fun u hu => osk.capA hu) kh + (fun v hv o ho h8 => by + obtain ⟨-, -, al, out, -⟩ := utVc_wordA J hv ho h8 + refine offW _ al (fun h => ?_) + have := inKs _ h + omega) + (fun t ht K hK => by + rcases hK with rfl | rfl <;> exact tK t ht _ (by simp) (by simp)) + have J2 := JT.toJA sysl2 X.1 X.2.1 (heapWin := J.heapWin) (coresAll := J.coresAll) + (utWin := K6.1) (devWin := J.devWin.keepA fun u hu => osk.capA hu) (vcBack := K6.2.1) (runBack := K6.2.2.1) (vcSnap := K6.2.2.2.1) (vcOS := J.vcOS) + (userFr := K6.2.2.2.2) (frTh := J.frTh.keepA (fun u hu => osk.capA hu) fun _ _ e => e) (bfSpec := J.bfSpec) + (ptCapIn := fun y hy q asid va h => J.ptCapIn y hy q asid va ((osk.capA (J.j5.2.1 y hy)).symm.trans h)) + (symL := Y2) + (fhOK := fun t ht => by + rw [osk.capA (J.j5.2.1 _ (isSlot_fhA ((J.sr.thT t).2 ht)))] + exact J.fhOK t ht) + (zwf := J.zwf.keepW fun y hy => osk.capA (J.j5.2.1 y hy)) + (schedValid := sv) + have entK : ChargeEntryA st Ac s → ChargeEntryA st Ac s' := by + intro En + have nodeK : ∀ o : U64, o.val < 4288 → o.val % 8 = 0 → o.val ≠ 40 → o.val ≠ 48 → + L.mem s' (L.nodeAt s' (L.cpu s') o) = L.mem s (L.nodeAt s (L.cpu s) o) := by + intro o h1 h2 h3 h4 + rw [cpu, nodeAt_of_ksb kb] + have ev := nodeAt_val nd h1 + exact memk _ (nodeAt_aligned nd h1 h2) (by omega) (by omega) + have eC : L.curW s' = L.curW s := by + unfold LawfulStore.curW + exact nodeK _ (by simp [layout.node.CUR_THREAD]) (by simp [layout.node.CUR_THREAD]) + (by simp [layout.node.CUR_THREAD]) (by simp [layout.node.CUR_THREAD]) + have eA : L.actionW s' = L.actionW s := by + unfold LawfulStore.actionW + exact nodeK _ (by rw [act_val]; omega) (by rw [act_val]) (by rw [act_val]; omega) (by rw [act_val]; omega) + have eS : L.cscW s' = L.cscW s := by + unfold LawfulStore.cscW LawfulStore.nodeA + exact nodeK _ (by simp [layout.node.CUR_SC]) (by simp [layout.node.CUR_SC]) + (by simp [layout.node.CUR_SC]) (by simp [layout.node.CUR_SC]) + have htZ : L.curW s ∈ Z.T := J.sr.curT + have est : L.mem s' (stA (L.curW s)) = L.mem s (stA (L.curW s)) := by + rw [stA_eq]; exact tK _ htZ _ (by simp) (by simp) + have esc : L.mem s' (scA (L.curW s)) = L.mem s (scA (L.curW s)) := by + rw [scA_eq]; exact tK _ htZ _ (by simp) (by simp) + have eq' : L.mem s' (inqA (L.curW s)) = L.mem s (inqA (L.curW s)) := by + rw [inqA_eq]; exact tK _ htZ _ (by simp) (by simp) + refine ⟨by rw [eA]; exact En.act, by rw [eC, est]; exact En.nvm, by rw [eS]; exact En.cA, ?_⟩ + rw [eC, est, esc, eq', eS] + exact En.run + exact ⟨τ, hτ, J2.arSwapA har, JT.sact, JT.noSwitch, V1, CI1, R1, Act1, hCl, entK⟩ + +/-- The record at some ready queues and current-owner projections. -/ +def JStage3QA (st : machine.Store S) [LawfulStore st] (ar : machine.Arch A) (AR : A → HwRootA → Prop) (Z : XSetsA) + (H : Heap) (OS U Ac Pp Tb : Set U64) (Lv : U64 → Nat) (ia ie : Nat) (N E Rp : Set U64) + (Bf : Set (U64 × Fl4SpecA64.Word)) (stack : U64 → List U64) (κ : U64 → U64) (n : Nat) (nA : U64) (m : McState) + (s : S) (a : A) (σ : State Unit) : Prop := + ∃ Q Rl curOf curScOf, JStage3A st AR { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } H OS U Ac Pp Tb + Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m + +theorem JStage3A.toQA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) : + JStage3QA st ar AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n nA m s a σ := + ⟨Z.Q, Z.R, Z.curOf, Z.curScOf, J⟩ + +/-- **A sufficient head refill: `check_budget` is `true` and changes nothing**, on both sides. -/ +theorem budgetSuffA {s : S} {a : A} {σ : State Unit} + (dec : (do + let sc ← sched.cur_sc st s + let c ← sched.consumed st s + sporadic.sufficient st ar s a sc c) = ok true) + (spec : fl4GetScRefillSufficient (Z := Unit) σ.curSc σ.consumedTime σ = ({(true, σ)}, False)) + (hnd : numDomains = 1) : + sched.check_budget st ar s a = ok (true, s, a) ∧ fl4CheckBudget (Z := Unit) σ = ({(true, σ)}, False) := by + refine ⟨?_, ?_⟩ + · unfold sched.check_budget + rw [eq_of_spec_fd (sched.cur_sc.spec s), bind_ok, eq_of_spec_fd (sched.consumed.specA s), bind_ok] + rw [eq_of_spec_fd (sched.cur_sc.spec s), bind_ok, eq_of_spec_fd (sched.consumed.specA s), bind_ok] at dec + rw [dec, bind_ok] + simp only [↓reduceIte, is_cur_domain_expired_eqA, bind_ok] + rfl + · unfold fl4CheckBudget + rw [nd_gets_bindSc, nd_gets_bindSc, nd_bind_oneSc spec, if_pos rfl, nd_gets_bindSc] + simp [Fl4Spec.NonDet.gets, Fl4Spec.NonDet.pure, isCurDomainExpired, hnd] + +set_option maxHeartbeats 1000000 in +/-- **`syscall.budget_ok` keeps the record** (module docstring). -/ +theorem JStage3A.budgetOkA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar nA)) s a σ m) + (sact : SActA σ) (noSwitch : ∀ u, σ.schedulerAction ≠ .switchThread u) + (hvp : ∀ b, VsR (L.ksb s) (Pp ∪ Tb) b → Aligned b ∧ ¬ PPW st s b) + {Clock : A → MachineState → Prop} (P : NowAgreeA ar Clock) (hClock : Clock a σ.machineState) + (hnd : numDomains = 1) (V : ValidMachineTimeA σ) (C : CounterStepA σ) (CI : CurInvA σ) (R : OffReadyA σ) + (Act : ActIdleA σ) (hNow : fl4ReadRawCounter σ.machineState = getCurrentTime σ.machineState) + (hAr : sched.update_timestamp st ar s a ⦃ r => AR' r.2 Z.hw ⦄) + (hmb : ∀ a', Fl4MinBudgetAgreeA ar a') (hEnt : ChargeEntryA st Ac s) + (hCharge : ∀ s1 a1 σ1, fl4UpdateTimestamp (Z := Unit) σ = ({((), σ1)}, False) → + JStage3A st AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar nA)) s1 a1 σ1 m → ChargeEntryA st Ac s1 → + ValidMachineTimeA σ1 → CurInvA σ1 → OffReadyA σ1 → ActIdleA σ1 → Clock a1 σ1.machineState → + fl4GetScRefillSufficient (Z := Unit) σ1.curSc σ1.consumedTime σ1 = ({(false, σ1)}, False) → + sched.check_budget_restart st ar s1 a1 ⦃ r => ∃ τ, + fl4CheckBudgetRestart (Z := Unit) σ1 = ({(r.1, τ)}, False) ∧ + JStage3QA st ar AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes nA m + r.2.1 r.2.2 τ ⦄) : + syscall.budget_ok st ar s a ⦃ r => ∃ τ, fl4BudgetOk (Z := Unit) σ = ({(r.1, τ)}, False) ∧ + JStage3QA st ar AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes nA m + r.2.1 r.2.2 τ ⦄ := by + unfold syscall.budget_ok + refine WP.spec_bind (J.timestampA sact noSwitch hvp P hClock hnd V C CI R Act hNow hAr) ?_ + rintro ⟨s1, a1⟩ ⟨σ1, e1, J1, -, -, V1, CI1, R1, Act1, Cl1, Ent1⟩ + simp only [uncurry_apply_pair] + obtain ⟨b, dec, spec, -⟩ := spec_existsA + (entryBudgetDecisionA J1.sr.toSchedSRN CI1 J1.sinv.mc a1 (hmb a1)) + have lift' : ∀ (r : Bool) (τ : State Unit), fl4CheckBudgetRestart (Z := Unit) σ1 = ({(r, τ)}, False) → + fl4BudgetOk (Z := Unit) σ = ({(r, τ)}, False) := fun r τ h => by + unfold fl4BudgetOk + rw [nd_bind_oneSc e1] + exact h + cases b + · refine WP.spec_mono (hCharge s1 a1 σ1 e1 J1 (Ent1 hEnt) V1 CI1 R1 Act1 Cl1 spec) ?_ + rintro r ⟨τ, h, Q⟩ + exact ⟨τ, lift' _ _ h, Q⟩ + · obtain ⟨code, cb⟩ := budgetSuffA (st := st) (ar := ar) dec spec hnd + unfold sched.check_budget_restart + rw [code, bind_ok] + simp only [uncurry_apply_pair] + refine WP.spec_bind (sched.cur_thread.spec s1) ?_ + intro t _ + simp only [↓reduceIte, WP.spec_ok] + obtain ⟨x, hk⟩ := J1.sinv.curTcb + refine ⟨σ1, lift' _ _ ?_, J1.toQA⟩ + unfold fl4CheckBudgetRestart + rw [nd_bind_oneSc cb, nd_gets_bindSc, nd_bind_oneSc (show getThreadState (Z := Unit) σ1.curThread σ1 = + ({(x.tcbState, σ1)}, False) from threadGet_eqSc _ hk)] + simp only [Fl4Spec.NonDet.when, not_true_eq_false, false_and, ↓reduceIte] + exact (nd_pure_bindSc () (fun _ => pure true) σ1).trans rfl + +set_option maxHeartbeats 1000000 in +/-- **The interrupt entry's budget prefix (`update_timestamp`, `check_budget`) keeps the record.** -/ +theorem JStage3A.irqBudgetA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar nA)) s a σ m) + (sact : SActA σ) (noSwitch : ∀ u, σ.schedulerAction ≠ .switchThread u) + (hvp : ∀ b, VsR (L.ksb s) (Pp ∪ Tb) b → Aligned b ∧ ¬ PPW st s b) + {Clock : A → MachineState → Prop} (P : NowAgreeA ar Clock) (hClock : Clock a σ.machineState) + (hnd : numDomains = 1) (V : ValidMachineTimeA σ) (C : CounterStepA σ) (CI : CurInvA σ) (R : OffReadyA σ) + (Act : ActIdleA σ) (hNow : fl4ReadRawCounter σ.machineState = getCurrentTime σ.machineState) + (hAr : sched.update_timestamp st ar s a ⦃ r => AR' r.2 Z.hw ⦄) + (hmb : ∀ a', Fl4MinBudgetAgreeA ar a') (hEnt : ChargeEntryA st Ac s) + (hCharge : ∀ s1 a1 σ1, fl4UpdateTimestamp (Z := Unit) σ = ({((), σ1)}, False) → + JStage3A st AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar nA)) s1 a1 σ1 m → ChargeEntryA st Ac s1 → + ValidMachineTimeA σ1 → CurInvA σ1 → OffReadyA σ1 → ActIdleA σ1 → Clock a1 σ1.machineState → + fl4GetScRefillSufficient (Z := Unit) σ1.curSc σ1.consumedTime σ1 = ({(false, σ1)}, False) → + sched.check_budget st ar s1 a1 ⦃ r => ∃ τ, + fl4CheckBudget (Z := Unit) σ1 = ({(r.1, τ)}, False) ∧ + JStage3QA st ar AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes nA m + r.2.1 r.2.2 τ ⦄) : + (do let (s1, a1) ← sched.update_timestamp st ar s a; sched.check_budget st ar s1 a1) ⦃ r => ∃ τ, + ((fl4UpdateTimestamp (Z := Unit)) >>= fun _ => fl4CheckBudget) σ = ({(r.1, τ)}, False) ∧ + JStage3QA st ar AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes nA m + r.2.1 r.2.2 τ ⦄ := by + refine WP.spec_bind (J.timestampA sact noSwitch hvp P hClock hnd V C CI R Act hNow hAr) ?_ + rintro ⟨s1, a1⟩ ⟨σ1, e1, J1, -, -, V1, CI1, R1, Act1, Cl1, Ent1⟩ + simp only [uncurry_apply_pair] + obtain ⟨b, dec, spec, -⟩ := spec_existsA + (entryBudgetDecisionA J1.sr.toSchedSRN CI1 J1.sinv.mc a1 (hmb a1)) + cases b + · refine WP.spec_mono (hCharge s1 a1 σ1 e1 J1 (Ent1 hEnt) V1 CI1 R1 Act1 Cl1 spec) ?_ + rintro r ⟨τ, h, Q⟩ + exact ⟨τ, by rw [nd_bind_oneSc e1]; exact h, Q⟩ + · obtain ⟨code, cb⟩ := budgetSuffA (st := st) (ar := ar) dec spec hnd + rw [code, WP.spec_ok] + exact ⟨σ1, by rw [nd_bind_oneSc e1]; exact cb, J1.toQA⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3KEntryBudgetSysA.lean b/proofs/Fl4ProofsA64/P3/Stage3KEntryBudgetSysA.lean new file mode 100644 index 000000000..c35731ef9 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3KEntryBudgetSysA.lean @@ -0,0 +1,215 @@ +/- +P3 (aarch64, A64-P3 Stage 3, kernel-entry composition, step 2): **`syscall.budget_ok` at the syscall-entry record**. + +* `JSyscallA.budgetOkSysA`: as `JStage3A.budgetOkA`, and a `true` result is at a `JSyscallA` again (the timestamp keeps + the action, the current thread and its SC; `OffOKA` from the sufficient head). The `false` branch is `hCharge` as + before and its result is `false` (`fl4CheckBudgetRestart_falseA`). +* `JSyscallA.budgetOkTrueA`: the same at a reached `true` outcome. +* (ruling (c)) Both also give `CounterStepA` at the `true` outcome (`fl4UpdateTimestamp_cntA`, from [P] `hC`, the raw + counter oracle is monotone); the decode tops take it as `hCnt`. + +Named premises: those of `budgetOkA` (`hvp`, the clock, `hCharge`); [P] `hC`. New module. +-/ +import Fl4ProofsA64.P3.Stage3KEntryArmsA +import Fl4ProofsA64.P3.Stage3KEntryP1A + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +set_option maxRecDepth 100000 + +/-! ## The timestamp and the budget decision -/ + +theorem fl4UpdateTimestamp_fieldsA {σ σ1 : State Unit} + (e : fl4UpdateTimestamp (Z := Unit) σ = ({((), σ1)}, False)) : + σ1.schedulerAction = σ.schedulerAction ∧ σ1.curSc = σ.curSc ∧ σ1.curThread = σ.curThread ∧ + σ1.kheap = σ.kheap := by + have eτ := fl4UpdateTimestamp_eqA σ + rw [e] at eτ + have h : σ1 = fl4TimestampState (rawNextA σ.machineState) + { σ with machineState := rawStateA σ.machineState } := + (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst eτ))).2 + rw [h] + exact ⟨rfl, rfl, rfl, rfl⟩ + +/-- (ruling (c), 2026-12-04) After the timestamp the counter step holds, from the oracle's monotonicity ([P] `hC`). -/ +theorem fl4UpdateTimestamp_cntA {σ σ1 : State Unit} + (e : fl4UpdateTimestamp (Z := Unit) σ = ({((), σ1)}, False)) + (hC : ∀ i, fl4RawCounterOracle i ≤ fl4RawCounterOracle (i + 1)) : CounterStepA σ1 := by + have eτ := fl4UpdateTimestamp_eqA σ + rw [e] at eτ + have h : σ1 = fl4TimestampState (rawNextA σ.machineState) + { σ with machineState := rawStateA σ.machineState } := + (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst eτ))).2 + rw [h] + exact hC _ + +theorem mem_bind_pureA {α β : Type} {m : Fl4Spec.NonDet (State Unit) α} {b : β} {σ : State Unit} + {r : β × State Unit} (h : r ∈ ((m >>= fun _ => (pure b : Fl4Spec.NonDet (State Unit) β)) σ).1) : + r.1 = b := by + obtain ⟨x, -, hx⟩ := mem_bindA.1 h + rw [nd_pure_applySc] at hx + exact congrArg Prod.fst (Set.mem_singleton_iff.1 hx) + +/-- **An insufficient head: `check_budget_restart` answers `false`.** -/ +theorem fl4CheckBudgetRestart_falseA {σ1 : State Unit} {r : Bool × State Unit} + (spec : fl4GetScRefillSufficient (Z := Unit) σ1.curSc σ1.consumedTime σ1 = ({(false, σ1)}, False)) + (h : r ∈ (fl4CheckBudgetRestart (Z := Unit) σ1).1) : r.1 = false := by + unfold fl4CheckBudgetRestart at h + obtain ⟨x, hx, hr⟩ := mem_bindA.1 h + have hx1 : x.1 = false := by + unfold fl4CheckBudget at hx + rw [nd_gets_bindSc, nd_gets_bindSc, nd_bind_oneSc spec] at hx + simp only [Bool.false_eq_true, ↓reduceIte] at hx + rw [nd_gets_bindSc] at hx + exact mem_bind_pureA hx + rw [nd_gets_bindSc] at hr + obtain ⟨y, -, hy⟩ := mem_bindA.1 hr + exact (mem_bind_pureA hy).trans hx1 + +/-! ## `budget_ok` at the syscall-entry record -/ + +section budget +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {ar : machine.Arch A} +variable {AR AR' : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {nA : U64} {s : S} {a : A} {σ : State Unit} {m : McState} + +set_option maxHeartbeats 2000000 in +/-- **`syscall.budget_ok` at the syscall-entry record**: as `JStage3A.budgetOkA`, and a `true` result is at the +syscall-entry record again. -/ +theorem JSyscallA.budgetOkSysA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar nA)) s a σ m) + (hvp : ∀ b, VsR (L.ksb s) (Pp ∪ Tb) b → Aligned b ∧ ¬ PPW st s b) + {Clock : A → MachineState → Prop} (P : NowAgreeA ar Clock) (hClock : Clock a σ.machineState) + (hnd : numDomains = 1) (C : CounterStepA σ) + (hC : ∀ i, fl4RawCounterOracle i ≤ fl4RawCounterOracle (i + 1)) + (hNow : fl4ReadRawCounter σ.machineState = getCurrentTime σ.machineState) + (hAr : sched.update_timestamp st ar s a ⦃ r => AR' r.2 Z.hw ⦄) + (hmb : ∀ a', Fl4MinBudgetAgreeA ar a') + (hCharge : ∀ s1 a1 σ1, fl4UpdateTimestamp (Z := Unit) σ = ({((), σ1)}, False) → + JStage3A st AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar nA)) s1 a1 σ1 m → ChargeEntryA st Ac s1 → + ValidMachineTimeA σ1 → CurInvA σ1 → OffReadyA σ1 → ActIdleA σ1 → Clock a1 σ1.machineState → + fl4GetScRefillSufficient (Z := Unit) σ1.curSc σ1.consumedTime σ1 = ({(false, σ1)}, False) → + sched.check_budget_restart st ar s1 a1 ⦃ r => ∃ τ, + fl4CheckBudgetRestart (Z := Unit) σ1 = ({(r.1, τ)}, False) ∧ + JStage3QA st ar AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes nA m + r.2.1 r.2.2 τ ⦄) : + syscall.budget_ok st ar s a ⦃ r => ∃ τ, fl4BudgetOk (Z := Unit) σ = ({(r.1, τ)}, False) ∧ + JStage3QA st ar AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes nA m + r.2.1 r.2.2 τ ∧ + (r.1 = true → JSyscallA st AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes + (J5A st) (J7A st (J7R2A st ar nA)) r.2.1 r.2.2 τ m ∧ CounterStepA τ) ⦄ := by + unfold syscall.budget_ok + refine WP.spec_bind (J.toJStage3A.timestampA J.sact J.noSwitch hvp P hClock hnd J.validTime C J.curInv + J.offReady J.actIdle hNow hAr) ?_ + rintro ⟨s1, a1⟩ ⟨σ1, e1, J1, sa1, -, V1, CI1, R1, Act1, Cl1, Ent1⟩ + simp only [uncurry_apply_pair] + obtain ⟨b, dec, spec, off⟩ := spec_existsA + (entryBudgetDecisionA J1.sr.toSchedSRN CI1 J1.sinv.mc a1 (hmb a1)) + have lift' : ∀ (r : Bool) (τ : State Unit), fl4CheckBudgetRestart (Z := Unit) σ1 = ({(r, τ)}, False) → + fl4BudgetOk (Z := Unit) σ = ({(r, τ)}, False) := fun r τ h => by + unfold fl4BudgetOk + rw [nd_bind_oneSc e1] + exact h + cases b + · refine WP.spec_mono (hCharge s1 a1 σ1 e1 J1 (Ent1 J.chargeEntryA) V1 CI1 R1 Act1 Cl1 spec) ?_ + rintro r ⟨τ, h, Q⟩ + refine ⟨τ, lift' _ _ h, Q, fun hr => ?_⟩ + have := fl4CheckBudgetRestart_falseA spec (r := (r.1, τ)) (by rw [h]; exact Set.mem_singleton _) + exact absurd (this.symm.trans hr) (by decide) + · obtain ⟨code, cb⟩ := budgetSuffA (st := st) (ar := ar) dec spec hnd + unfold sched.check_budget_restart + rw [code, bind_ok] + simp only [uncurry_apply_pair] + refine WP.spec_bind (sched.cur_thread.spec s1) ?_ + intro t _ + simp only [↓reduceIte, WP.spec_ok] + obtain ⟨x, hk⟩ := J1.sinv.curTcb + refine ⟨σ1, lift' _ _ ?_, J1.toQA, fun _ => ?_⟩ + · unfold fl4CheckBudgetRestart + rw [nd_bind_oneSc cb, nd_gets_bindSc, nd_bind_oneSc (show getThreadState (Z := Unit) σ1.curThread σ1 = + ({(x.tcbState, σ1)}, False) from threadGet_eqSc _ hk)] + simp only [Fl4Spec.NonDet.when, not_true_eq_false, false_and, ↓reduceIte] + exact (nd_pure_bindSc () (fun _ => pure true) σ1).trans rfl + · have fl := fl4UpdateTimestamp_fieldsA e1 + have htZ : L.curW s1 ∈ Z.T := J1.sr.curT + have ct1 : σ1.curThread = specPtr (L.curW s1) := J1.j.1.sch.curThread + have ct : σ.curThread = specPtr (L.curW s) := J.j.1.sch.curThread + have eC : L.curW s1 = L.curW s := specPtr_inj (ct1.symm.trans (fl.2.2.1.trans ct)) + obtain ⟨x0, hkx, -⟩ := J.ep.tcb _ J.sr.curT + have hr0 : runnable x0.tcbState = true := + (runnable_iffA ((J.ep.at_tcb J.sr.curT hkx).st)).1 J.running + have hk1 : σ1.kheap (specPtr (L.curW s1)) = some (.tcb x0) := by rw [fl.2.2.2, eC]; exact hkx + have run1 : Runnable (L.mem s1 (stA (L.curW s1))) := + (runnable_iffA ((J1.ep.at_tcb htZ hk1).st)).2 hr0 + have En1 := (Ent1 J.chargeEntryA).run run1 + refine ⟨?_, fl4UpdateTimestamp_cntA e1 hC⟩ + exact + { toJStage3A := J1 + curInv := CI1 + offOK := off rfl R1 + offReady := R1 + actIdle := Act1 + curScNotIdle := (by rw [fl.2.1]; exact J.curScNotIdle) + validTime := V1 + sact := sa1 + act := (by rw [fl.1]; exact J.act) + running := run1 + curSc := En1.1 + cur := ⟨En1.2.1, En1.2.2⟩ } + +/-- **A reached `true` budget outcome is at the syscall-entry record.** -/ +theorem JSyscallA.budgetOkTrueA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar nA)) s a σ m) + (hvp : ∀ b, VsR (L.ksb s) (Pp ∪ Tb) b → Aligned b ∧ ¬ PPW st s b) + {Clock : A → MachineState → Prop} (P : NowAgreeA ar Clock) (hClock : Clock a σ.machineState) + (hnd : numDomains = 1) (C : CounterStepA σ) + (hC : ∀ i, fl4RawCounterOracle i ≤ fl4RawCounterOracle (i + 1)) + (hNow : fl4ReadRawCounter σ.machineState = getCurrentTime σ.machineState) + (hAr : sched.update_timestamp st ar s a ⦃ r => AR' r.2 Z.hw ⦄) + (hmb : ∀ a', Fl4MinBudgetAgreeA ar a') + (hCharge : ∀ s1 a1 σ1, fl4UpdateTimestamp (Z := Unit) σ = ({((), σ1)}, False) → + JStage3A st AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar nA)) s1 a1 σ1 m → ChargeEntryA st Ac s1 → + ValidMachineTimeA σ1 → CurInvA σ1 → OffReadyA σ1 → ActIdleA σ1 → Clock a1 σ1.machineState → + fl4GetScRefillSufficient (Z := Unit) σ1.curSc σ1.consumedTime σ1 = ({(false, σ1)}, False) → + sched.check_budget_restart st ar s1 a1 ⦃ r => ∃ τ, + fl4CheckBudgetRestart (Z := Unit) σ1 = ({(r.1, τ)}, False) ∧ + JStage3QA st ar AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes nA m + r.2.1 r.2.2 τ ⦄) + {s' : S} {a' : A} {τ : State Unit} (eq : syscall.budget_ok st ar s a = ok (true, s', a')) + (bspec : fl4BudgetOk (Z := Unit) σ = ({(true, τ)}, False)) : + JSyscallA st AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar nA)) s' a' τ m ∧ CounterStepA τ := by + have h := J.budgetOkSysA hvp P hClock hnd C hC hNow hAr hmb hCharge + rw [eq, WP.spec_ok] at h + obtain ⟨τ', e, -, hs⟩ := h + have : τ' = τ := (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 + (congrArg Prod.fst (e.symm.trans bspec)))).2 + subst this + exact hs rfl + +/-- **`hXv7` from record edit #8b-C's `bufOffOmega`** (`∀ b, BufW Bf b → b ∉ H.omega`): `VrOK.om` puts every word of +J7's `Pt` (and of `Kt`) inside `H.omega`. Until #8b-C lands, `hXv7` stays a named premise. -/ +theorem hXv7_of_omegaA {H : Heap} {OS : Set U64} {V Ub : Set U64} {kb : U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + (hω : ∀ b, BufW Bf b → b ∉ H.omega) : + ∀ Pt, VrOK H.omega H.frame H.scs H.scBits OS V Ub kb Pt → ∀ b, VsR kb Pt b → ¬ BufW Bf b := + fun _ vr b hv hb => hω b hb (vr.om b hv) + +end budget + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3KEntryChargeA.lean b/proofs/Fl4ProofsA64/P3/Stage3KEntryChargeA.lean new file mode 100644 index 000000000..f8aaf2a72 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3KEntryChargeA.lean @@ -0,0 +1,873 @@ +/- +P3 (aarch64, A64-P3 Stage 3, kernel-entry composition, step 2): **the record across a budget charge without the +timeout send** (`charge_budget … can`, `can = false` or the spec's send condition failing, `NoTimeoutA`: round robin, +invalid timeout handler, running VM; the unschedulable current thread is the plain case). + +* `tcbSchedAction_anyMemA`, `entryRequeue_shapeA`, `chargeSteps_shapeA`: the spec queue shape of the charge + (`ChargeShapeA`): the ready queues gain at most the current thread with the release queue kept (append), or lose it + while the release queue gains at most it (postpone); `rescheduleRequired` enqueues nothing without a pending switch. +* `SpecIrqA.updScA`, `SchedValidA.chargeA` (F8), `SInvXA.chargeA`, `SIdleA.chargeA`: the spec invariants over that shape. +* `JStage3A.chargeNoSendA`: `charge_budget_noSendFrA` at the record, then `JStage3A.of_ipcStepA` (`P3/Stage3IpcGlueA`): + the endpoint / notification / reply relations by `mcsStepFramesA`, `J7A` across the scheduler words (as + `P3/Stage3RestartA`), F3–F8 and `SInvXA` by the shape. + +Named premises: +* `charge_budget_noSendFrA`'s: [J] `T` (timeout leaf, read for validity only), `hcore` [S], `hni`, [P] clock / budget. +New module. +-/ +import Fl4ProofsA64.P3.Stage3KEntryArmsA +import Fl4ProofsA64.P3.Stage3KEntryP1A +import Fl4ProofsA64.P3.Stage3IpcGlueA +import Fl4ProofsA64.P3.Stage3SigSchedValidA +import Fl4ProofsA64.P3.DelSinvLoopA +import Fl4ProofsA64.P3.TcbRestartFrameA +import Fl4ProofsA64.P3.Stage3McsFrA +import Fl4ProofsA64.P3.Stage3PartialSpecA +import Fl4ProofsA64.P3.Stage3LiftMachA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 Fl4Spec Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## The spec queue shape -/ + +theorem tcbSchedAction_anyMemA (f : ObjRef → List ObjRef → List ObjRef) (t : ObjRef) {σ : State Unit} + {r : Unit × State Unit} (hr : r ∈ (tcbSchedAction (Z := Unit) f t σ).1) : + ∃ d pr, r.2 = { σ with readyQueues := fun d' p => + if d' = d ∧ p = pr then f t (σ.readyQueues d pr) else σ.readyQueues d' p } := by + unfold tcbSchedAction at hr + obtain ⟨d, -, hr⟩ := eqPres_mem (threadGet_eqPres _ t) hr + obtain ⟨pr, -, hr⟩ := eqPres_mem (threadGet_eqPres _ t) hr + have hq : PresR (@Eq (State Unit)) (getTcbQueue (Z := Unit) d pr) := by unfold getTcbQueue; eqpres + obtain ⟨q, hq', hr⟩ := eqPres_mem hq hr + have eq : q = σ.readyQueues d pr := by + unfold getTcbQueue at hq' + obtain ⟨z, hz, hq'⟩ := mem_bindA.1 hq' + have ez : z = (σ.readyQueues, σ) := hz + subst ez + have := congrArg Prod.fst (show (q, σ) = (σ.readyQueues d pr, σ) from hq') + exact this + obtain ⟨q', hq'', hr⟩ := mem_bindA.1 hr + have eq' : q' = (f t q, σ) := hq'' + subst eq' + have er : r = ((), { σ with readyQueues := fun d' p => + if d' = d ∧ p = pr then f t q else σ.readyQueues d' p }) := hr + refine ⟨d, pr, ?_⟩ + rw [er, ← eq] + +instance (p : ObjRef) : QInst (getScRefillReady (Z := Unit) p) := + ⟨fun s x hx => by rw [← getScRefillReady_eqPres p s x hx]; exact QKeepA.refl _⟩ +instance (p : ObjRef) (u : Ticks) : QInst (fl4GetScRefillSufficient (Z := Unit) p u) := + ⟨fun s x hx => by + have h : PresR (@Eq (State Unit)) (fl4GetScRefillSufficient (Z := Unit) p u) := by + unfold fl4GetScRefillSufficient; eqpres + rw [← h s x hx]; exact QKeepA.refl _⟩ +instance (p t : ObjRef) : QInst (entryRequeueA p t) := ⟨by unfold entryRequeueA; qpres⟩ + +/-- **The queue shape of `entryRequeueA p t`** for the owner `t` of `p` in no ready queue: append (the ready queues gain +at most `t`, the release queue is kept) or postpone (the ready queues keep only others, the release queue gains at most +`t`). The heap and the scheduler globals are kept. -/ +theorem entryRequeue_shapeA {σ τ : State Unit} {p t : ObjRef} + (hp : ∀ y k, σ.kheap p = some (.schedContext y k) → y.scTcb = some t) + (hnq : ∀ d q, t ∉ σ.readyQueues d q) + (h : ((), τ) ∈ (entryRequeueA p t σ).1) : + τ.kheap = σ.kheap ∧ τ.curThread = σ.curThread ∧ τ.idleThread = σ.idleThread ∧ τ.curSc = σ.curSc ∧ + τ.schedulerAction = σ.schedulerAction ∧ τ.interruptIrqNode = σ.interruptIrqNode ∧ + ((τ.releaseQueue = σ.releaseQueue ∧ ∀ d q u, u ∈ τ.readyQueues d q → u ∈ σ.readyQueues d q ∨ u = t) ∨ + ((∀ d q u, u ∈ τ.readyQueues d q → u ∈ σ.readyQueues d q ∧ u ≠ t) ∧ + ∀ u ∈ τ.releaseQueue, u ∈ σ.releaseQueue ∨ u = t)) := by + unfold entryRequeueA at h + obtain ⟨b1, -, h⟩ := eqPres_mem (getScRefillReady_eqPres p) h + have hs : PresR (@Eq (State Unit)) (fl4GetScRefillSufficient (Z := Unit) p 0) := by + unfold fl4GetScRefillSufficient; eqpres + obtain ⟨b2, -, h⟩ := eqPres_mem hs h + split_ifs at h with hc + · obtain ⟨d, pr, e⟩ := tcbSchedAction_anyMemA _ t h + simp only at e + subst e + refine ⟨rfl, rfl, rfl, rfl, rfl, rfl, Or.inl ⟨rfl, fun d' q u hu => ?_⟩⟩ + simp only at hu + split_ifs at hu with hh + · unfold tcbSchedAppend at hu + have hu' : u ∈ σ.readyQueues d pr ∨ u = t := by + split_ifs at hu + all_goals first + | exact Or.inl hu + | (rcases List.mem_append.1 hu with h1 | h1 + · exact Or.inl h1 + · exact Or.inr (List.mem_singleton.1 h1)) + rw [hh.1, hh.2]; exact hu' + · exact Or.inl hu + · unfold postpone at h + obtain ⟨⟨o, σ1⟩, h1, h⟩ := mem_bindA.1 h + try dsimp only at h + obtain ⟨e1, y, k, hk, ho⟩ := getScObjRef_mem h1 + simp only at e1 ho + rw [hp y k hk] at ho + rw [e1, ho] at h + obtain ⟨⟨t', σ2⟩, h2, h⟩ := mem_bindA.1 h + try dsimp only at h + have e2 : ((t', σ2) : ObjRef × State Unit) = (t, σ) := h2 + rw [(Prod.mk.inj e2).1, (Prod.mk.inj e2).2] at h + obtain ⟨⟨u3, σ3⟩, h3, h⟩ := mem_bindA.1 h + try dsimp only at h + obtain ⟨d, pr, e3⟩ := tcbSchedAction_anyMemA _ t h3 + simp only at e3 + have q3 : ∀ d' q u, u ∈ σ3.readyQueues d' q → u ∈ σ.readyQueues d' q ∧ u ≠ t := by + intro d' q u hu + rw [e3] at hu + simp only at hu + split_ifs at hu with hh + · unfold tcbSchedDequeue at hu + obtain ⟨hm, hne⟩ := List.mem_filter.1 hu + refine ⟨by rw [hh.1, hh.2]; exact hm, fun e => ?_⟩ + subst e + simp at hne + · exact ⟨hu, fun e => hnq d' q (e ▸ hu)⟩ + have k3 : σ3.kheap = σ.kheap := by rw [e3] + have c3 : σ3.curThread = σ.curThread := by rw [e3] + have i3 : σ3.idleThread = σ.idleThread := by rw [e3] + have s3 : σ3.curSc = σ.curSc := by rw [e3] + have a3 : σ3.schedulerAction = σ.schedulerAction := by rw [e3] + have n3 : σ3.interruptIrqNode = σ.interruptIrqNode := by rw [e3] + have r3 : σ3.releaseQueue = σ.releaseQueue := by rw [e3] + obtain ⟨⟨u4, σ4⟩, h4, h⟩ := mem_bindA.1 h + try dsimp only at h + unfold tcbReleaseEnqueue at h4 + obtain ⟨⟨q, σ5⟩, h5, h4⟩ := mem_bindA.1 h4 + try dsimp only at h4 + have e5 : ((q, σ5) : List ObjRef × State Unit) = (σ3.releaseQueue, σ3) := h5 + rw [(Prod.mk.inj e5).1, (Prod.mk.inj e5).2] at h4 + obtain ⟨⟨q', σ6⟩, h6, h4⟩ := mem_bindA.1 h4 + try dsimp only at h4 + obtain ⟨hs6, hm6⟩ := orderedInsert_spec t _ readTcbReadyTime (· ≤ ·) h6 + simp only at hs6 hm6 + rw [hs6] at h4 + obtain ⟨⟨u7, σ7⟩, h7, h4⟩ := mem_bindA.1 h4 + try dsimp only at h4 + have e7 : ((u7, σ7) : Unit × State Unit) = ((), { σ3 with releaseQueue := q' }) := h7 + rw [(Prod.mk.inj e7).2] at h4 + have e4 : σ4 = { σ3 with releaseQueue := q' } ∨ + σ4 = { { σ3 with releaseQueue := q' } with reprogramTimer := true } := by + unfold NonDet.when at h4 + split_ifs at h4 + · have e : ((u4, σ4) : Unit × State Unit) = + ((), { { σ3 with releaseQueue := q' } with reprogramTimer := true }) := h4 + exact Or.inr (Prod.mk.inj e).2 + · have e : ((u4, σ4) : Unit × State Unit) = ((), { σ3 with releaseQueue := q' }) := h4 + exact Or.inl (Prod.mk.inj e).2 + have eτ : τ = { σ4 with reprogramTimer := true } := + (Prod.mk.inj (show (((), τ) : Unit × State Unit) = ((), { σ4 with reprogramTimer := true }) from h)).2 + have rls : ∀ u ∈ q', u ∈ σ.releaseQueue ∨ u = t := fun u hu => + (hm6 u hu).symm.imp_left fun e => r3 ▸ e + rcases e4 with e | e <;> rw [e] at eτ <;> subst eτ <;> + exact ⟨k3, c3, i3, s3, a3, n3, Or.inr ⟨q3, rls⟩⟩ + +/-- The spec scheduler state after a budget charge without the timeout send (`ChargeStepsA`). -/ +structure ChargeShapeA (σ τ : State Unit) : Prop where + cur : τ.curThread = σ.curThread + idle : τ.idleThread = σ.idleThread + csc : τ.curSc = σ.curSc + irq : τ.interruptIrqNode = σ.interruptIrqNode + kh : ∃ y n, τ.kheap = (updKSc σ σ.curSc (.schedContext y n)).kheap + q : (τ.readyQueues = σ.readyQueues ∧ τ.releaseQueue = σ.releaseQueue ∧ + τ.schedulerAction = σ.schedulerAction) ∨ + (τ.schedulerAction = .chooseNewThread ∧ ActTcbA τ σ.curThread ∧ σ.curThread ∉ σ.releaseQueue ∧ + (∃ x, τ.kheap σ.curThread = some (.tcb x) ∧ runnable x.tcbState = true) ∧ + ((τ.releaseQueue = σ.releaseQueue ∧ + ∀ d p u, u ∈ τ.readyQueues d p → u ∈ σ.readyQueues d p ∨ u = σ.curThread) ∨ + ((∀ d p u, u ∈ τ.readyQueues d p → u ∈ σ.readyQueues d p ∧ u ≠ σ.curThread) ∧ + ∀ u ∈ τ.releaseQueue, u ∈ σ.releaseQueue ∨ u = σ.curThread))) + /-- (record edit #8b) A ready `idle_sc_ptr` head stays ready. -/ + rdy : IdleRdyA σ → IdleRdyA τ + /-- (record edit #8b-B1) The domain schedule is kept. -/ + dom : DomKeepA σ τ + +/-- **The shape of the charge's spec steps** (module docstring). -/ +theorem chargeSteps_shapeA {σ τ : State Unit} (hS : ChargeStepsA σ τ) + (hsc : ∀ y k, σ.kheap σ.curSc = some (.schedContext y k) → y.scTcb = some σ.curThread) + (hcs : ∃ y k, σ.kheap σ.curSc = some (.schedContext y k)) + (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) (hsw : ∀ u, σ.schedulerAction ≠ .switchThread u) + (ht : τ.curTime = σ.curTime) : ChargeShapeA σ τ := by + obtain ⟨y, n, hy, hS, hR⟩ := hS + rcases hS with ⟨hsch, σ3, hreq, hres⟩ | ⟨-, rfl⟩ + swap + · exact ⟨rfl, rfl, rfl, rfl, ⟨y, n, rfl⟩, Or.inl ⟨rfl, rfl, rfl⟩, fun g => hR g, ⟨rfl, rfl, rfl, rfl⟩⟩ + have hp : ∀ z k, (updKSc σ σ.curSc (.schedContext y n)).kheap σ.curSc = some (.schedContext z k) → + z.scTcb = some σ.curThread := by + intro z k hz + rw [updKSc_same] at hz + cases hz + obtain ⟨z0, k0, hz0⟩ := hcs + rw [hy z0 k0 hz0] + exact hsc z0 k0 hz0 + obtain ⟨hk3, hc3, hi3, hcs3, ha3, hn3, hq3⟩ := entryRequeue_shapeA (σ := { updKSc σ σ.curSc + (.schedContext y n) with consumedTime := 0 }) hp hnq (by rw [hreq]; exact Set.mem_singleton _) + have hmem : ((), τ) ∈ (reschedReprogA σ3).1 := by + rw [hres]; exact Set.mem_singleton _ + have hmem0 := hmem + unfold reschedReprogA at hmem + obtain ⟨⟨u4, σ4⟩, h4, h5⟩ := mem_bindA.1 hmem + obtain ⟨hQ, ha4⟩ := rescheduleRequired_memA h4 + have eτ : τ = { σ4 with reprogramTimer := true } := + (Prod.mk.inj (show (((), τ) : Unit × State Unit) = ((), { σ4 with reprogramTimer := true }) from h5)).2 + subst eτ + have Q3 : QKeepA { updKSc σ σ.curSc (.schedContext y n) with consumedTime := 0 } σ3 := + (inferInstance : QInst (entryRequeueA σ.curSc σ.curThread)).pres _ _ (by rw [hreq]; exact Set.mem_singleton _) + have Q4 : QKeepA σ3 { σ4 with reprogramTimer := true } := + (show PresR QKeepA reschedReprogA by unfold reschedReprogA; qpres) σ3 _ hmem0 + have hD : DomKeepA σ { σ4 with reprogramTimer := true } := + (⟨rfl, rfl, rfl, rfl⟩ : DomKeepA σ { updKSc σ σ.curSc (.schedContext y n) with consumedTime := 0 }).trans + (Q3.dom.trans Q4.dom) + have hQ1 : σ4 = { σ3 with readyQueues := σ4.readyQueues, schedulerAction := σ4.schedulerAction } := hQ.1 + have k4 : σ4.kheap = σ3.kheap := by rw [hQ1] + have c4 : σ4.curThread = σ3.curThread := by rw [hQ1] + have i4 : σ4.idleThread = σ3.idleThread := by rw [hQ1] + have s4 : σ4.curSc = σ3.curSc := by rw [hQ1] + have n4 : σ4.interruptIrqNode = σ3.interruptIrqNode := by rw [hQ1] + have r4 : σ4.releaseQueue = σ3.releaseQueue := by rw [hQ1] + have q4 : ∀ d p u, u ∈ σ4.readyQueues d p → u ∈ σ3.readyQueues d p := fun d p u hu => by + rcases hQ.2 d p u hu with h | ⟨e, -⟩ + · exact h + · exact absurd (ha3.symm.trans e) (hsw u) + obtain ⟨act, nrel, run⟩ := schedulable_actA hsch + have rdy : IdleRdyA σ → IdleRdyA { σ4 with reprogramTimer := true } := fun g z k h => by + show (refillHd z).rTime ≤ σ4.curTime + have ht' : σ4.curTime = σ.curTime := ht + rw [ht'] + exact hR g z k (by + have h' : σ4.kheap idleScPtr = some (.schedContext z k) := h + rwa [k4, hk3] at h') + refine ⟨c4.trans hc3, i4.trans hi3, s4.trans hcs3, n4.trans hn3, ⟨y, n, k4.trans hk3⟩, Or.inr ⟨ha4, + act.ofKh (k4.trans hk3), nrel, ?_, ?_⟩, rdy, hD⟩ + · obtain ⟨x, hx, hr⟩ := run + exact ⟨x, by show σ4.kheap σ.curThread = _; rw [k4, hk3]; exact hx, hr⟩ + · rcases hq3 with ⟨hr, hq⟩ | ⟨hq, hr⟩ + · exact Or.inl ⟨r4.trans hr, fun d p u hu => hq d p u (q4 d p u hu)⟩ + · exact Or.inr ⟨fun d p u hu => hq d p u (q4 d p u hu), fun u hu => hr u (by rw [← r4]; exact hu)⟩ + +/-! ## The spec invariants over the shape -/ + +/-- **`ctQ` in record edit #8's form across the charge**: the charge either keeps the queues, the current thread and +the action, or leaves the action `chooseNewThread`. This is the post-state `SInvXA.ctQ` (record edit #8a); after an +append the current thread is in its ready queue and the action is `chooseNewThread`. -/ +theorem ChargeShapeA.ctQ8A {σ τ : State Unit} (S : ChargeShapeA σ τ) + (h : σ.schedulerAction = .resumeCurThread → ∀ d p, σ.curThread ∉ σ.readyQueues d p) : + τ.schedulerAction = .resumeCurThread → ∀ d p, τ.curThread ∉ τ.readyQueues d p := by + intro ha d p + rcases S.q with ⟨hq, -, ea⟩ | ⟨hc, -⟩ + · rw [S.cur, hq] + exact h (ea.symm.trans ha) d p + · rw [hc] at ha + cases ha + +/-- `SpecIrqA` across an SC replaced by an SC. -/ +theorem SpecIrqA.updScA {σ : State Unit} {p : ObjRef} {x y : SchedContext} {k n : Nat} (h : SpecIrqA σ) + (hk : σ.kheap p = some (.schedContext x k)) : SpecIrqA (updKSc σ p (.schedContext y n)) := by + have hct : ∀ c q, capTableAt c q (updKSc σ p (.schedContext y n)) ↔ capTableAt c q σ := fun c q => by + unfold capTableAt + by_cases e : q = p + · subst e; simp [updKSc, hk] + · simp [updKSc, Function.update_of_ne e] + obtain ⟨⟨⟨hinj, hcn⟩, hdom⟩, hsl, href⟩ := h + refine ⟨⟨⟨hinj, fun irq hi => (hct _ _).2 (hcn irq hi)⟩, fun irq hi c hc => hdom irq hi c ((hct _ _).1 hc)⟩, + fun irq hi o b c e hc' => ?_, fun q o b c e hc' => ?_⟩ + · by_cases ep : σ.interruptIrqNode irq = p + · obtain ⟨cs, hcs, -⟩ := hcn irq hi + rw [ep, hk] at hcs; cases hcs + · exact hsl irq hi o b c (by simpa [updKSc, Function.update_of_ne ep] using e) hc' + · by_cases ep : q = p + · subst ep + simp [updKSc] at e; subst e + simp [capOf] at hc' + · exact href q o b c (by simpa [updKSc, Function.update_of_ne ep] using e) hc' + +theorem scActive_of_maxA {y z : SchedContext} (h : y.scRefillMax = z.scRefillMax) : scActive y = scActive z := by + unfold scActive activeSc; rw [h] + +/-- The TCBs and active SCs across a charge (`ScChargeKeepA` forward, `SchedFrameA` backward). -/ +theorem chargeTcbBackA {σ τ : State Unit} (K : ScChargeKeepA σ τ) (F : SchedFrameA σ τ) {p : ObjRef} {x : Tcb} + (h : τ.kheap p = some (.tcb x)) : σ.kheap p = some (.tcb x) := by + obtain ⟨y, hy⟩ := F.1.tcbBack h + have e := (K.2 p y hy).symm.trans h + cases e + exact hy + +theorem chargeActA {σ τ : State Unit} (K : ScChargeKeepA σ τ) {p : ObjRef} (h : ScActiveAtA σ p) : + ScActiveAtA τ p := by + obtain ⟨y, k, hy, ha⟩ := h + obtain ⟨z, hz, -, -, -, hm, -⟩ := K.1 p y k hy + exact ⟨z, k, hz, (scActive_of_maxA hm).trans ha⟩ + +theorem chargeActTcbA {σ τ : State Unit} (K : ScChargeKeepA σ τ) {t : ObjRef} (h : ActTcbA σ t) : ActTcbA τ t := by + obtain ⟨x, sc, e1, e2, a⟩ := h + exact ⟨x, sc, K.2 t x e1, e2, chargeActA K a⟩ + +/-- **F8 across the charge** (module docstring). -/ +theorem SchedValidA.chargeA {σ τ : State Unit} (h : SchedValidA σ) (S : ChargeShapeA σ τ) (K : ScChargeKeepA σ τ) + (F : SchedFrameA σ τ) : SchedValidA τ := by + refine ⟨fun d p t hm => ?_, fun e1 e2 => ?_, fun t x sc e1 e2 e3 => ?_, fun t ht => ?_⟩ + · rcases S.q with ⟨hq, hr, -⟩ | ⟨-, act, nrel, -, hq⟩ + · rw [hq] at hm + obtain ⟨a, n⟩ := h.rq d p t hm + exact ⟨chargeActTcbA K a, by rw [hr]; exact n⟩ + · rcases hq with ⟨hr, hq⟩ | ⟨hq, hr⟩ + · rcases hq d p t hm with m | e + · obtain ⟨a, n⟩ := h.rq d p t m + exact ⟨chargeActTcbA K a, by rw [hr]; exact n⟩ + · subst e; exact ⟨act, by rw [hr]; exact nrel⟩ + · obtain ⟨m, ne⟩ := hq d p t hm + obtain ⟨a, n⟩ := h.rq d p t m + refine ⟨chargeActTcbA K a, fun hr' => ?_⟩ + rcases hr t hr' with e | e + · exact n e + · exact ne e + · rcases S.q with ⟨hq, hr, ha⟩ | ⟨ha, -⟩ + · rw [ha] at e1 + rw [S.cur, S.idle] at e2 + obtain ⟨a, n⟩ := h.act e1 e2 + rw [S.cur, hr] + exact ⟨chargeActTcbA K a, n⟩ + · rw [ha] at e1; cases e1 + · exact chargeActA K (h.blk t x sc (chargeTcbBackA K F e1) e2 e3) + · rcases S.q with ⟨-, hr, -⟩ | ⟨-, act, -, -, hq⟩ + · rw [hr] at ht + exact chargeActTcbA K (h.rls t ht) + · rcases hq with ⟨hr, -⟩ | ⟨-, hr⟩ + · rw [hr] at ht + exact chargeActTcbA K (h.rls t ht) + · rcases hr t ht with e | e + · exact chargeActTcbA K (h.rls t e) + · subst e; exact act + +/-- **The spec invariant across the charge** (module docstring), given the post-state `ctQ` and the queues' runnable +part (`SchedSRN.run`). -/ +theorem SInvXA.chargeA {σ τ : State Unit} {m : McState} {n : Nat} (h : SInvXA σ m n) (hI : SIdleA σ m n) + (S : ChargeShapeA σ τ) (K : ScChargeKeepA σ τ) (F : SchedFrameA σ τ) (run : RunQA τ) + (hc0 : τ.consumedTime = 0) : + SInvXA τ m n where + mc := h.mc + view := by unfold McView; rw [S.idle]; exact h.view + curTcb := by obtain ⟨x, e⟩ := h.curTcb; exact ⟨x, by rw [S.cur]; exact K.2 _ x e⟩ + idleTcb := by obtain ⟨x, e, e2⟩ := h.idleTcb; exact ⟨x, by rw [S.idle]; exact K.2 _ x e, e2⟩ + onlyIdle t x e e2 := h.onlyIdle t x (chargeTcbBackA K F e) e2 + idleSc := by + obtain ⟨x, y, k, e1, e2, e3, e4⟩ := h.idleSc + obtain ⟨z, hz, -, -, ht, -⟩ := K.1 _ y k e3 + exact ⟨x, z, k, by rw [S.idle]; exact K.2 _ x e1, e2, hz, by rw [ht, S.idle]; exact e4⟩ + curSc := by + obtain ⟨y, k, e⟩ := h.curSc + obtain ⟨z, hz, -⟩ := K.1 _ y k e + exact ⟨z, k, by rw [S.csc]; exact hz⟩ + ctQ := S.ctQ8A h.ctQ + idleQ d p c hc hm := by + rcases S.q with ⟨hq, -, -⟩ | ⟨-, -, -, ⟨x, hx, hr⟩, hq⟩ + · rw [hq] at hm; exact h.idleQ d p c hc hm + · rcases hq with ⟨-, hq⟩ | ⟨hq, -⟩ + · rcases hq d p _ hm with m' | e + · exact h.idleQ d p c hc m' + · obtain ⟨y, hy, hs⟩ := hI.f3 c hc + rw [e] at hy + have e' := (K.2 _ y hy).symm.trans hx + cases e' + rw [hs] at hr + exact absurd hr (by decide) + · exact h.idleQ d p c hc (hq d p _ hm).1 + rq d p t ht := run.1 d p t ht + rls t ht := by obtain ⟨x, e, -⟩ := run.2 t ht; exact ⟨x, e⟩ + rpos p y k e a := by + obtain ⟨z, hz⟩ := F.2.1 p y k e + obtain ⟨y', hy', -, -, -, hm, hne⟩ := K.1 p z k hz + have e' := hy'.symm.trans e + cases e' + have az : scActive z = true := (scActive_of_maxA hm).symm.trans a + exact hne az (h.rpos p z k hz az) + irqN := by + obtain ⟨y, k, e⟩ := h.curSc + obtain ⟨y', n', hkh⟩ := S.kh + exact (h.irqN.updScA (y := y') (n := n') e).of_eq hkh S.irq + zc _ _ _ _ _ := hc0 + rlsRun t ht := by obtain ⟨x, e, r⟩ := run.2 t ht; exact ⟨x, e, r⟩ + curScReady hne := absurd hc0 hne + faultSt p y hy hf := h.faultSt p y (chargeTcbBackA K F hy) hf + +/-- **F3–F6 across the charge.** -/ +theorem SIdleA.chargeA {σ τ : State Unit} {m : McState} {n : Nat} (h : SIdleA σ m n) (S : ChargeShapeA σ τ) + (K : ScChargeKeepA σ τ) (F : SchedFrameA σ τ) : SIdleA τ m n where + f3 c hc := by obtain ⟨x, e, e2⟩ := h.f3 c hc; exact ⟨x, K.2 _ x e, e2⟩ + f4 y k e := by + obtain ⟨z, hz⟩ := F.2.1 _ y k e + obtain ⟨y', hy', hr, -⟩ := K.1 _ z k hz + have e' := hy'.symm.trans e + cases e' + rw [hr]; exact h.f4 z k hz + f5 p y k e he := by + obtain ⟨z, hz⟩ := F.2.1 p y k e + obtain ⟨y', hy', -, -, ht, -⟩ := K.1 p z k hz + have e' := hy'.symm.trans e + cases e' + exact h.f5 p z k hz (by rw [← ht, he, S.idle]) + f6 := h.f6.of_eq fun sl => F.1.specCapAt sl + f7 y k e := by + obtain ⟨z, hz⟩ := F.2.1 _ y k e + obtain ⟨y', hy', -, -, -, hm, -⟩ := K.1 _ z k hz + have e' := hy'.symm.trans e + cases e' + exact ⟨(scActive_of_maxA hm).trans (h.f7 z k hz).1, S.rdy (fun y k h' => (h.f7 y k h').2) y k e⟩ + idleSlot c hc := by + obtain ⟨x, e, -⟩ := h.f3 c hc + exact (idleVtKeepA e (K.2 _ x e) rfl rfl (h.idleSlot c hc) (h.idleBound c hc)).1 + idleBound c hc := by + obtain ⟨x, e, -⟩ := h.f3 c hc + exact (idleVtKeepA e (K.2 _ x e) rfl rfl (h.idleSlot c hc) (h.idleBound c hc)).2 + + +/-! ## The record -/ + +section rec +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s : S} {a : A} {σ : State Unit} {m : McState} + +/-- A TCB word other than the scheduler link words (`SC`, `NEXT`, `PREV`, `IN_QUEUES`) is off `MWrA`. -/ +theorem tcbNotMWrA {κ : U64 → U64} (H : SchedSRN st Z m κ s σ) {t K : U64} (ht : t ∈ Z.T) (hb : K.val < 4096) + (hK : K.val ≠ 3464 ∧ K.val ≠ 3480 ∧ K.val ≠ 3488 ∧ K.val ≠ 3592) : + ¬ MWrA (st := st) Z m σ s (wa t K) := by + obtain ⟨hks, hsc⟩ := yieldTcbOffA H ht hb + refine not_mwrA hsc (fun u hu => ?_) hks + have field {k : U64} (hk : k.val < 4096) (hne : K.val ≠ k.val) : + wa t K ≠ wa u k := field_ne (H.core.tcbs t ht) (H.core.tcbs u hu) hb hk (fun _ => hne) + refine ⟨field (by decide) ?_, field (by decide) ?_, field (by decide) ?_, field (by decide) ?_⟩ + · exact fun e => hK.1 (by first | simpa using e | exact e.trans (by decide) | exact e.trans rfl) + · exact fun e => hK.2.1 (by first | simpa using e | exact e.trans (by decide) | exact e.trans rfl) + · exact fun e => hK.2.2.1 (by first | simpa using e | exact e.trans (by decide) | exact e.trans rfl) + · exact fun e => hK.2.2.2 (by first | simpa using e | exact e.trans (by decide) | exact e.trans rfl) + +/-- **`J7A` across scheduler-step writes** (`P3/Stage3RestartA`'s argument at any `SchedStepWA` write). -/ +theorem JStage3A.j7_schedWrA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {s' : S} (w : WritesIn st s s' (SchedStepWA (st := st) Z m σ s)) (osk : OSKeep st OS s s') (σ' : State Unit) : + J7A st (J7R2A st ar nA) Z H OS s' σ' m := by + have kb : L.ksb s' = L.ksb s := w.1 + obtain ⟨Pt, Ub, vr, pn, arm, cc, vcc, dfa, hi, link, vcw, fu, ual, vcu, Pp', Tb', Hr, Te, Kt, Lv', Re, bits, R2, Kut, + KLv, ktK, hik⟩ := J.j7 + have vsr : VsKeep st (L.ksb s) Pt s s' := fun b hb => by + refine w.mem_eq (vr.al _ hb) ?_ + by_cases h : AsW (L.ksb s) b + · obtain ⟨o, o1, o2, -, e⟩ := h + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.sys.ent.ksw + have ev : b.val = (L.ksb s).val + o.val := by + rw [e, wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact J.asid_offStepA (by omega) (by omega) + · exact J.vsr_offStepA vr hb h + have tab : TabKeep st s s' := ⟨kb, fun i => by rw [kb]; exact vsr _ (Or.inr (asW_at _ i))⟩ + refine ⟨Pt, Ub, by rw [kb]; exact vr, PoolNamed.ofTab kb (fun j => tab.2 j) osk pn, S1aArmCol.ofTab tab arm, + S1aCapCol.ofOSK osk cc, CapInv.keepO vcc osk, CapInv.keepO dfa osk, + HiCap511.keep hi (fun y hy => osk.capA hy) (fun y hy p m a e => ?_), fun p hp => link p ?_, + by rw [kb]; exact vcw, FrUb.keepO fu osk, ual, vcu, Pp', Tb', Hr, Te, Kt, Lv', Re, bits, + R2.ofImage kb vsr (S1aSlotImage.ofSame R2.capRefs.refs fun _ hy => Or.inl (osk.capA hy)), Kut.keep osk, KLv, ktK, + S1aHiK.keep hik ⟨EdgeKeepOn.ofVsKeep R2 vsr, fun y hy R m a c => ⟨y, hy, m, a, by rwa [osk.capA hy] at c⟩⟩⟩ + · have live : p ∈ Hr := by rw [R2.highRoots]; exact ⟨y, hy, m, a, e⟩ + exact vsr _ (Or.inl ⟨p, R2.place.tables (R2.place.high.sub live), 4088#u64, by decide, by decide, rfl⟩) + · unfold SchedPageA at hp ⊢ + rcases hp with hp | hp | hp | hp | hp | hp | hp | ⟨u, hu, hp⟩ + · exact Or.inl hp + · exact Or.inr (Or.inl hp) + · exact Or.inr (Or.inr (Or.inl hp)) + · exact Or.inr (Or.inr (Or.inr (Or.inl hp))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inl hp)))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl hp))))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl hp)))))) + · rw [osk.capA hu] at hp + exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ⟨u, hu, hp⟩)))))) + +set_option maxHeartbeats 4000000 in +/-- The scheduler-action word `0` is `resume_cur_thread`. -/ +theorem JStage3A.resumeA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) (h : L.actionW s = 0#u64) : + σ.schedulerAction = .resumeCurThread := by + rw [J.sr.sch.act] + unfold actOfA + rw [if_pos h] + +/-- A charge-shaped step keeps the queues: only the current SC changes, keeping its replies and refill maximum. -/ +theorem qk_chargeA {σ τ : State Unit} (KS : ScChargeKeepA σ τ) (SH : ChargeShapeA σ τ) (FR : SchedFrameA σ τ) : + QKeepA σ τ := by + obtain ⟨y, n, hk⟩ := SH.kh + have other : ∀ q o, τ.kheap q = some o → (∀ z k, o ≠ .schedContext z k) → σ.kheap q = some o := by + intro q o h ne + rw [hk, updKSc_kheap] at h + by_cases e : q = σ.curSc + · subst e + rw [Function.update_self] at h + exact absurd (Option.some.inj h).symm (ne _ _) + · rwa [Function.update_of_ne e] at h + refine ⟨fun q z h => ⟨z, other q _ h (fun _ _ e => by cases e), rfl⟩, + fun q z h => ⟨z, other q _ h (fun _ _ e => by cases e), List.Sublist.refl _⟩, + fun q z h => ⟨z, other q _ h (fun _ _ e => by cases e), List.Sublist.refl _⟩, + fun q z k h => ?_, fun q z h => ⟨z, other q _ h (fun _ _ e => by cases e), id⟩, + SH.dom⟩ + obtain ⟨z0, h0⟩ := FR.2.1 q z k h + obtain ⟨z', hz', hrep, -, -, hrm, -⟩ := KS.1 q z0 k h0 + rw [h] at hz' + cases hz' + exact ⟨z0, k, h0, fun r => ⟨by rwa [hrep] at r, fun a => (scActive_of_maxA hrm).trans a⟩⟩ + +/-- **The record across a charge-shaped scheduler step** (`MFrA`, the spec frame, the SC keep and the queue shape; +the back half of `JStage3A.chargeNoSendA`, shared with the yield). -/ +theorem JStage3A.of_chargeStepA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar nA)) s a σ m) + {s' : S} {τ : State Unit} {Q' : U64 → U64 → List U64} {R' : U64 → List U64} + (Y : YieldQueueRelA (st := st) { Z with Q := Q', R := R' } m κ s' τ) (F : MFrA (st := st) Z m s s' σ τ) + (FR : SchedFrameA σ τ) (KS : ScChargeKeepA σ τ) (SH : ChargeShapeA σ τ) + (sysl : SysL st H Z.Cs OS ia ie s') (symL : SymL st H Z.Cs s') + (bxi : BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s') + (hc0 : τ.consumedTime = 0) : + JStage3QA st ar AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes nA m s' a τ := by + have w : WritesIn st s s' (SchedStepWA (st := st) Z m σ s) := F.wr.mono fun _ h => Or.inl h + have wN : WritesIn st s s' (SchedStepNWA (st := st) N E Rp Z m σ s) := F.wr.mono fun _ h => Or.inl (Or.inl h) + have osk : OSKeep st OS s s' := J.osKeep_stepA w + have kb : L.ksb s' = L.ksb s := w.1 + have j7 := J.j7_schedWrA w osk τ + have userOK : Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s' := J.sys.ent.user.ksb kb + obtain ⟨RN, HE, HS, -, -, -⟩ := mcsStepFramesA J.iplaced J.ipcPlace J.sr.toSchedSRN F FR.2.2 + (fun q y k hy => by obtain ⟨y', hy', hr, hyf, -⟩ := KS.1 q y k hy; exact ⟨y', hy', hr, hyf⟩) + J.ntfn J.ep J.reply J.mcp J.yieldRel + have K : IpcKeepA σ τ := by + refine ⟨fun p x hx => ⟨x, KS.2 p x hx, rfl, rfl, rfl, rfl, rfl, rfl⟩, fun p y k hy => ?_, fun p y' k' hy' => ?_⟩ + · obtain ⟨z, hz, -, hyf, -⟩ := KS.1 p y k hy + exact ⟨z, k, hz, hyf⟩ + · obtain ⟨z, hz⟩ := FR.2.1 p y' k' hy' + obtain ⟨z', hz', -, hyf, -⟩ := KS.1 p z k' hz + have e := hz'.symm.trans hy' + cases e + exact ⟨z, k', hz, hyf⟩ + have tcbKeep : ∀ t ∈ Z.T, ∀ O : U64, O.val < 4096 → O.val % 8 = 0 → + (O.val ≠ 3464 ∧ O.val ≠ 3480 ∧ O.val ≠ 3488 ∧ O.val ≠ 3592) → L.mem s' (wa t O) = L.mem s (wa t O) := + fun t ht O h1 h8 hK => F.wr.mem_eq (field_aligned (J.sr.core.tcbs t ht) h1 h8) + (tcbNotMWrA J.sr.toSchedSRN ht h1 hK) + have tw : ∀ t ∈ Z.T, ∀ O : U64, O.val < 224 ∨ O.val = 3440 ∨ O.val = 3472 ∨ O.val = 3512 ∨ O.val = 3456 → O.val % 8 = 0 → + L.mem s' (wa t O) = L.mem s (wa t O) := fun t ht O hO h8 => + tcbKeep t ht O (by omega) h8 (by omega) + have rv : RunVcpuInv st Z.T s' := fun t ht h0 => by + have er : L.mem s' (runVcpuA t) = L.mem s (runVcpuA t) := by + unfold runVcpuA + exact tcbKeep t ht _ (by decide) (by decide) (by decide) + have es : L.mem s' (Fl4ProofsA64.P1.stA t) = L.mem s (Fl4ProofsA64.P1.stA t) := by + rw [Fl4ProofsA64.P1.stA_eq] + exact tcbKeep t ht _ (by decide) (by decide) (by decide) + rw [es] + exact J.runVcpu t ht (er ▸ h0) + have bq : BlkQueuedA Z.T E N (L.mem s') τ := + J.blkQ.keepA (fun t ht => by rw [Notif.tBlockA_eq]; exact tcbKeep t ht _ (by decide) (by decide) (by decide)) + (fun t _ x' h => Or.inr ⟨x', chargeTcbBackA KS FR h, rfl⟩) + (fun p y h => F.keep.obj p _ h (by intro _ _ e; cases e) (by intro _ e; cases e)) + (fun p y h => F.keep.obj p _ h (by intro _ _ e; cases e) (by intro _ e; cases e)) + have sv := J.schedValid.chargeA SH KS FR + have si := J.sinv.chargeA J.sidle SH KS FR Y.sched.run hc0 + have sid := J.sidle.chargeA SH KS FR + exact ⟨Q', R', Z.curOf, Z.curScOf, J.of_ipcStepA (Q := Q') (Rl := R') (curOf := Z.curOf) + (curScOf := Z.curScOf) wN FR.1 FR.2.1 (qk_chargeA KS SH FR) FR.2.2 (fun _ _ => rfl) bxi sysl symL userOK j7 Y.sched HE RN HS K tw + si sid rv bq sv⟩ + +set_option maxHeartbeats 4000000 in +/-- **The record across `charge_budget` without the timeout send** (module docstring). -/ +theorem JStage3A.chargeNoSendA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar nA)) s a σ m) + (noSwitch : ∀ u, σ.schedulerAction ≠ .switchThread u) (I : CurInvA σ) (Idle : ActIdleA σ) + (hcore : L.mem s (scCoreA (L.cscW s)) = L.cpu s) (hni : σ.curSc ≠ idleScPtr → NotIdleScA m σ.curSc) + {Clock : A → MachineState → Prop} (clk : Clock a σ.machineState) + (T : EntryValidFromA (st := st) Z m κ Clock s σ) + (hag : fl4MinBudget = MIN_BUDGET) (hMin : Fl4MinBudgetAgreeA ar a) + (hLimit : sporadic.max_release_time ar a ⦃ v => v.bv = fl4MaxReleaseTime ⦄) + (used : U64) (can : Bool) (hns : can = true → NoTimeoutA used.bv σ) + (hA : P0.ArchTotal ar) (En : ChargeEntryA st Ac s) : + sched.charge_budget st ar s a used can ⦃ r => ∃ τ, + fl4ChargeBudget (Z := Unit) used.bv can σ = ({((), τ)}, False) ∧ r.2 = a ∧ + JStage3QA st ar AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes nA m r.1 r.2 τ ∧ + Clock r.2 τ.machineState ∧ ActIdleA τ ∧ τ.consumedTime = 0 ∧ ChargeShapeA σ τ ∧ ChargeStepsA σ τ ∧ + ScChargeKeepA σ τ ⦄ := by + have E0 := J.yieldQueueA + have P := J.budgetPlaceA hcore hni En.act + refine WP.spec_mono (WP.spec_and (charge_budget_noSendFrA (ar := ar) E0 I Idle P a clk T hag hMin hLimit used can hns) + (J.chargeP1A hA En used can)) ?_ + rintro ⟨s', a'⟩ ⟨⟨τ, Q', R', he, heF, Y, hcl, htm, -, hc0, hId, -, F, KS, CS, ha'⟩, sysl, symL, bxi⟩ + dsimp only at he heF Y hcl hc0 hId F KS CS ha' sysl symL bxi ⊢ + subst ha' + have FR : SchedFrameA σ τ := + frame_of_runA (inferInstance : SPresInst (fl4ChargeBudget (Z := Unit) used.bv false)).pres heF + have hsc : ∀ y k, σ.kheap σ.curSc = some (.schedContext y k) → y.scTcb = some σ.curThread := by + intro y k hy + obtain ⟨t, hkt, hB, -⟩ := I.tcb + obtain ⟨z, j, hz, ho⟩ := J.sr.inv.back _ _ _ hkt hB + rw [hy] at hz; cases hz; exact ho + have SH := chargeSteps_shapeA CS hsc J.sinv.curSc (J.sinv.ctQ (J.resumeA En.act)) noSwitch htm + exact ⟨τ, he, rfl, J.of_chargeStepA Y F FR KS SH sysl symL bxi hc0, hcl, hId, hc0, SH, CS, KS⟩ + +set_option maxHeartbeats 2000000 in +/-- **`check_budget` at the record on an exhausted head refill without the timeout send** (the interrupt entry's +budget branch). -/ +theorem JStage3A.checkBudgetNoSendA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar nA)) s a σ m) + (noSwitch : ∀ u, σ.schedulerAction ≠ .switchThread u) (I : CurInvA σ) (Idle : ActIdleA σ) + (hcore : L.mem s (scCoreA (L.cscW s)) = L.cpu s) (hni : σ.curSc ≠ idleScPtr → NotIdleScA m σ.curSc) + {Clock : A → MachineState → Prop} (clk : Clock a σ.machineState) + (T : EntryValidFromA (st := st) Z m κ Clock s σ) + (hag : fl4MinBudget = MIN_BUDGET) (hMin : Fl4MinBudgetAgreeA ar a) + (hLimit : sporadic.max_release_time ar a ⦃ v => v.bv = fl4MaxReleaseTime ⦄) + (hex : fl4GetScRefillSufficient (Z := Unit) σ.curSc σ.consumedTime σ = ({(false, σ)}, False)) + (hns : NoTimeoutA σ.consumedTime σ) (hA : P0.ArchTotal ar) (En : ChargeEntryA st Ac s) : + sched.check_budget st ar s a ⦃ r => ∃ τ, fl4CheckBudget (Z := Unit) σ = ({(r.1, τ)}, False) ∧ + JStage3QA st ar AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes nA m + r.2.1 r.2.2 τ ∧ r.1 = false ∧ ChargeShapeA σ τ ∧ ChargeStepsA σ τ ∧ ScChargeKeepA σ τ ⦄ := by + have H0 := J.yieldQueueA + let used := L.mem s (L.nodeA s layout.node.CONSUMED) + have hUsed : σ.consumedTime = used.bv := H0.sched.rest.consumed + unfold sched.check_budget + rw [eq_of_spec_fd (sched.cur_sc.spec s), bind_ok, eq_of_spec_fd (sched.consumed.specA s), bind_ok] + have dec := entryBudgetDecisionA H0.sched.toSchedSRN I J.sinv.mc a hMin + rw [eq_of_spec_fd (sched.cur_sc.spec s), bind_ok, eq_of_spec_fd (sched.consumed.specA s), bind_ok] at dec + refine WP.spec_bind dec ?_ + rintro b ⟨e, -⟩ + have hb : b = false := by + have q := e.symm.trans hex + exact (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst q))).1 + subst hb + have spec : fl4CheckBudget (Z := Unit) σ = + (fl4ChargeBudget used.bv true >>= fun _ => (pure false : SMonad Unit Bool)) σ := by + unfold fl4CheckBudget + rw [nd_gets_bindSc, nd_gets_bindSc, nd_bind_oneSc e] + simp only [Bool.false_eq_true, ↓reduceIte] + rw [nd_gets_bindSc, hUsed] + simp only [Bool.false_eq_true, ↓reduceIte] + refine WP.spec_bind (J.chargeNoSendA noSwitch I Idle hcore hni clk T hag hMin hLimit used true + (fun _ => by rw [← hUsed]; exact hns) hA En) ?_ + rintro ⟨s1, a1⟩ ⟨τ, h, -, JQ, -, -, -, SH, CS, KS⟩ + change ok (false, s1, a1) ⦃ _ ⦄ + rw [WP.spec_ok] + refine ⟨τ, ?_, JQ, rfl, SH, CS, KS⟩ + rw [spec, nd_bind_oneSc h] + rfl + +set_option maxHeartbeats 1000000 in +/-- **The interrupt entry's exhausted-budget branch at the record** (`KEntryChargeA` at `check_budget`, the +`hCharge` of `interrupt_recA` / `JStage3A.irqBudgetA`): the no-send branches by `JStage3A.checkBudgetNoSendA`; the +timeout send is the named premise `hSend`. + +Named premises: [J] `hSend` (the timeout fault IPC at the record, Lift 2b's fault-sender send), [J] `T`, +`hni`; [P] `hA`, `hCs`, `one`, `hag`, `hmb`, `hLimit`. -/ +theorem kentryChargeIrqA {AR' : A → HwRootA → Prop} {Clock : A → MachineState → Prop} + (noSwitch : ∀ u, σ.schedulerAction ≠ .switchThread u) (hni : σ.curSc ≠ idleScPtr → NotIdleScA m σ.curSc) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (one : fl4A64Platform.numNodes = 1) + (hag : fl4MinBudget = MIN_BUDGET) (hmb : ∀ a', Fl4MinBudgetAgreeA ar a') + (hLimit : ∀ a', sporadic.max_release_time ar a' ⦃ v => v.bv = fl4MaxReleaseTime ⦄) + (T : ∀ s1 a1 σ1, JStage3A st AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar nA)) s1 a1 σ1 m → EntryValidFromA (st := st) Z m κ Clock s1 σ1) + (hA : P0.ArchTotal ar) + (hSend : ∀ s1 a1 σ1, fl4UpdateTimestamp (Z := Unit) σ = ({((), σ1)}, False) → + JStage3A st AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar nA)) s1 a1 σ1 m → + ChargeEntryA st Ac s1 → CurInvA σ1 → ActIdleA σ1 → Clock a1 σ1.machineState → + fl4GetScRefillSufficient (Z := Unit) σ1.curSc σ1.consumedTime σ1 = ({(false, σ1)}, False) → + ¬ NoTimeoutA σ1.consumedTime σ1 → + sched.check_budget st ar s1 a1 ⦃ r => ∃ τ, fl4CheckBudget (Z := Unit) σ1 = ({(r.1, τ)}, False) ∧ + JStage3QA st ar AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes nA m + r.2.1 r.2.2 τ ⦄) : + KEntryChargeA st ar AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ nA m Clock s a σ + (sched.check_budget st ar) fl4CheckBudget := by + intro s1 a1 σ1 e1 J1 En1 V1 CI1 R1 Act1 Cl1 hex + have eσ1 : σ1 = fl4TimestampState (rawNextA σ.machineState) { σ with machineState := rawStateA σ.machineState } := + (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst ((fl4UpdateTimestamp_eqA σ).symm.trans e1)))).2.symm + have sw1 : ∀ u, σ1.schedulerAction ≠ .switchThread u := by rw [eσ1]; exact noSwitch + have ni1 : σ1.curSc ≠ idleScPtr → NotIdleScA m σ1.curSc := by rw [eσ1]; exact hni + by_cases hns : NoTimeoutA σ1.consumedTime σ1 + · refine WP.spec_mono (J1.checkBudgetNoSendA sw1 CI1 Act1 (J1.curScOnCoreA hCs one) ni1 Cl1 (T s1 a1 σ1 J1) hag + (hmb a1) (hLimit a1) hex hns hA En1) ?_ + rintro r ⟨τ, h, Q, -⟩ + exact ⟨τ, h, Q⟩ + · exact hSend s1 a1 σ1 e1 J1 En1 CI1 Act1 Cl1 hex hns + +end rec + + +/-! ## Yield -/ + +theorem ScChargeKeepA.refl (σ : State Unit) : ScChargeKeepA σ σ := + ⟨fun _ z _ h => ⟨z, h, rfl, rfl, rfl, rfl, fun _ h' => h'⟩, fun _ _ h => h⟩ + +theorem ScChargeKeepA.trans {σ1 σ2 σ3 : State Unit} (h1 : ScChargeKeepA σ1 σ2) (h2 : ScChargeKeepA σ2 σ3) : + ScChargeKeepA σ1 σ3 := by + refine ⟨fun q z k hz => ?_, fun q x hx => h2.2 q x (h1.2 q x hx)⟩ + obtain ⟨z1, hz1, a1, b1, c1, d1, e1⟩ := h1.1 q z k hz + obtain ⟨z2, hz2, a2, b2, c2, d2, e2⟩ := h2.1 q z1 k hz1 + exact ⟨z2, hz2, a2.trans a1, b2.trans b1, c2.trans c1, d2.trans d1, + fun ha hn => e2 ((scActive_of_maxA d1).trans ha) (e1 ha hn)⟩ + +/-- An SC's consumed time rewritten keeps the charge's SC fields. -/ +theorem ScChargeKeepA.consumedA {σ : State Unit} {p : ObjRef} {y : SchedContext} {n : Nat} + (hk : σ.kheap p = some (.schedContext y n)) (c : Ticks) : + ScChargeKeepA σ (updKSc σ p (.schedContext { y with scConsumed := c } n)) := by + refine ⟨fun q z k hz => ?_, fun q x hx => ?_⟩ + · by_cases e : q = p + · subst e; rw [hk] at hz; cases hz + exact ⟨_, updKSc_same _ _ _, rfl, rfl, rfl, rfl, fun _ h => h⟩ + · exact ⟨z, by rw [updKSc_kheap, Function.update_of_ne e]; exact hz, rfl, rfl, rfl, rfl, fun _ h => h⟩ + · have e : q ≠ p := fun e => by subst e; rw [hk] at hx; cases hx + rw [updKSc_kheap, Function.update_of_ne e]; exact hx + +/-- The charge shape across a later rewrite of the current SC object. -/ +theorem ChargeShapeA.consumedA {σ τ1 τ : State Unit} (S : ChargeShapeA σ τ1) (K : ScChargeKeepA τ1 τ) + {y' : SchedContext} {n' : Nat} (hτ : τ = updKSc τ1 σ.curSc (.schedContext y' n')) + {x : SchedContext} {k : Nat} (hx : τ1.kheap σ.curSc = some (.schedContext x k)) + (href : y'.scRefills = x.scRefills) : ChargeShapeA σ τ := by + subst hτ + have rdy : IdleRdyA τ1 → IdleRdyA (updKSc τ1 σ.curSc (.schedContext y' n')) := fun g z j h => by + show (refillHd z).rTime ≤ τ1.curTime + rw [updKSc_kheap] at h + by_cases e : idleScPtr = σ.curSc + · rw [e, Function.update_self] at h + cases h + have := g x k (by rw [e]; exact hx) + unfold refillHd at this ⊢ + rw [href]; exact this + · rw [Function.update_of_ne e] at h + exact g z j h + obtain ⟨y, n, hkh⟩ := S.kh + refine ⟨S.cur, S.idle, S.csc, S.irq, ⟨y', n', ?_⟩, ?_, fun g => rdy (S.rdy g), + ⟨S.dom.index, S.dom.start, S.dom.table, S.dom.gvs⟩⟩ + · simp only [updKSc_kheap] at hkh ⊢ + rw [hkh, Function.update_idem] + · rcases S.q with h | ⟨h1, act, nrel, ⟨x, hx, hr⟩, h5⟩ + · exact Or.inl h + · exact Or.inr ⟨h1, chargeActTcbA K act, nrel, ⟨x, K.2 _ x hx, hr⟩, h5⟩ + +section yld +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {nA : U64} {s : S} {a : A} {σ : State Unit} {m : McState} + +/-- **The yield's consumed-time restore with its frame** (`OuterEntryA`'s private `restoreYieldConsumedA` with +`YieldQueueRelA` and `MFrA`). -/ +theorem restoreConsumedFrA (H0 : YieldQueueRelA (st := st) Z m κ s σ) {sc : U64} (hc : sc ∈ Z.C) (v : U64) : + obj.Sc.set_consumed st sc s v ⦃ s' => ∃ x n, σ.kheap (scNameA m sc) = some (.schedContext x n) ∧ + setScObjRef (Z := Unit) scConsumedUpdate (scNameA m sc) v.bv σ = + ({((), updKSc σ (scNameA m sc) (.schedContext { x with scConsumed := v.bv } n))}, False) ∧ + YieldQueueRelA (st := st) Z m κ s' (updKSc σ (scNameA m sc) (.schedContext { x with scConsumed := v.bv } n)) ∧ + MFrA (st := st) Z m s s' σ (updKSc σ (scNameA m sc) (.schedContext { x with scConsumed := v.bv } n)) ⦄ := by + obtain ⟨x, n, hk, R⟩ := H0.sched.scr.sc sc hc + refine WP.spec_mono (obj.Sc.set_consumed.specSc s sc v) ?_ + rintro s' rfl + obtain ⟨rel, writes⟩ := R.set_consumed v + have core : scCoreA sc = scWA sc 16#u64 := by unfold scCoreA scWA layout.sc.CORE; rfl + have keep : L.mem (L.wr s (scWA sc 8#u64) v) (scCoreA sc) = L.mem s (scCoreA sc) := by + rw [core] + apply L.mem_wr_ne (R.hdr (by simp : (8#u64).val < 96) (by simp)).1 + (R.hdr (by simp : (16#u64).val < 96) (by simp)).1 + have fit := R.bounds.2.2 + rw [scWA_val (sc := sc) (K := 8#u64) (by simp; omega), + scWA_val (sc := sc) (K := 16#u64) (by simp; omega)] + simp + have SR := H0.sched.scObjA hc hk writes rel rfl rfl rfl rfl keep + have F : MFrA (st := st) Z m s (L.wr s (scWA sc 8#u64) v) σ + (updKSc σ (scNameA m sc) (.schedContext { x with scConsumed := v.bv } n)) := cfgScFrameA hc hk writes + obtain ⟨E2, M2, Y2⟩ := entryScFrameA (y := { x with scConsumed := v.bv }) H0 hc hk rfl rfl F + exact ⟨x, n, hk, Sched.updateSchedContext_eqA hk _, ⟨SR, E2, M2, Y2⟩, F⟩ + +set_option maxHeartbeats 4000000 in +/-- **`handle_yield` at the record**: the reads, `charge_budget … false` (`charge_budget_noSendFrA`; no timeout send), +the SC's consumed time restored, then `JStage3A.of_chargeStepA`; A64 P1's invariants by `JStage3A.yieldP1A`. -/ +theorem JStage3A.yieldA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar nA)) s a σ m) + (noSwitch : ∀ u, σ.schedulerAction ≠ .switchThread u) (I : CurInvA σ) (Idle : ActIdleA σ) + (hcore : L.mem s (scCoreA (L.cscW s)) = L.cpu s) (hni : σ.curSc ≠ idleScPtr → NotIdleScA m σ.curSc) + (nonidle : σ.curSc ≠ idleScPtr) + {Clock : A → MachineState → Prop} (clk : Clock a σ.machineState) + (T : EntryValidFromA (st := st) Z m κ Clock s σ) + (hag : fl4MinBudget = MIN_BUDGET) (hMin : Fl4MinBudgetAgreeA ar a) + (hLimit : sporadic.max_release_time ar a ⦃ v => v.bv = fl4MaxReleaseTime ⦄) + (hA : P0.ArchTotal ar) (En : ChargeEntryA st Ac s) + (hrun : Fl4ProofsA64.P1.Runnable (L.mem s (stA (L.curW s)))) : + syscall.handle_yield st ar s a ⦃ r => ∃ τ, fl4HandleYield (Z := Unit) σ = ({((), τ)}, False) ∧ r.2 = a ∧ + JStage3QA st ar AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes nA m r.1 r.2 τ ∧ + Clock r.2 τ.machineState ⦄ := by + have E0 := J.yieldQueueA + have place := J.budgetPlaceA hcore hni En.act + suffices inner : (syscall.handle_yield st ar s a ⦃ r => ∃ τ Q' R', + fl4HandleYield (Z := Unit) σ = ({((), τ)}, False) ∧ r.2 = a ∧ + YieldQueueRelA (st := st) { Z with Q := Q', R := R' } m κ r.1 τ ∧ MFrA (st := st) Z m s r.1 σ τ ∧ + SchedFrameA σ τ ∧ ScChargeKeepA σ τ ∧ ChargeShapeA σ τ ∧ Clock r.2 τ.machineState ∧ τ.consumedTime = 0 ⦄) by + refine WP.spec_mono (WP.spec_and inner (J.yieldP1A hA En hrun)) ?_ + rintro ⟨s', a'⟩ ⟨⟨τ, Q', R', he, ha', Y, F, FR, KS, SH, hcl, hc0⟩, sysl, symL, bxi⟩ + dsimp only at ha' Y F hcl hc0 sysl symL bxi ⊢ + subst ha' + exact ⟨τ, he, rfl, J.of_chargeStepA Y F FR KS SH sysl symL bxi hc0, hcl⟩ + have hsc : ∀ y k, σ.kheap σ.curSc = some (.schedContext y k) → y.scTcb = some σ.curThread := by + intro y k hy + obtain ⟨t, hkt, hB, -⟩ := I.tcb + obtain ⟨z, j, hz, ho⟩ := J.sr.inv.back _ _ _ hkt hB + rw [hy] at hz; cases hz; exact ho + let sc := L.cscW s + have hc : sc ∈ Z.C := E0.sched.cscC + have hcur : σ.curSc = scNameA m sc := E0.sched.currentScA + obtain ⟨x, n, hk, R⟩ := E0.sched.scr.sc sc hc + have hx := R.strong (I.active nonidle _ _ (by rwa [hcur])) + have ne := refills_neL hx.wf + have hxne : x.scRefills ≠ [] := by simpa [hx.refills] using ne + unfold syscall.handle_yield + refine WP.spec_bind (sched.cur_sc.spec s) ?_ + intro csc hcsc + change csc = sc at hcsc + subst csc + refine WP.spec_bind (obj.Sc.consumed.specSc s sc) ?_ + intro old hOld + refine WP.spec_bind (sched.consumed.specA s) ?_ + intro elapsed hElapsed + simp only [lift, bind_ok] + let total := core.num.U64.wrapping_add old elapsed + have elapsedEq : (L.mem s (L.nodeA s layout.node.CONSUMED)).bv = σ.consumedTime := + E0.sched.rest.consumed.symm + have hTotal : total.bv = x.scConsumed + σ.consumedTime := by + unfold total + rw [core.num.U64.wrapping_add_bv_eq, hOld, ← R.consumed, hElapsed, elapsedEq] + refine WP.spec_bind (sporadic.head.spec s sc) ?_ + intro head hHead + have hAmount : (refillHd x).rAmount = head.amount.bv := by + rw [refillHd_mapA hx.refills ne, ← head_eqA hx.wf, ← hHead] + rfl + refine WP.spec_bind (charge_budget_noSendFrA (ar := ar) E0 I Idle place a clk T hag hMin hLimit head.amount false + (fun h => by cases h)) ?_ + rintro ⟨s1, a1⟩ ⟨τ1, Q, Rq, -, hCharge, H1, hClock, htm, -, hc1, -, -, F1, KS1, CS, ha1⟩ + dsimp only at hClock ha1 H1 F1 + have FR1 : SchedFrameA σ τ1 := + frame_of_runA (inferInstance : SPresInst (fl4ChargeBudget (Z := Unit) head.amount.bv false)).pres hCharge + have SH1 := chargeSteps_shapeA CS hsc J.sinv.curSc (J.sinv.ctQ (J.resumeA En.act)) noSwitch htm + refine WP.spec_bind (restoreConsumedFrA H1 hc total) ?_ + rintro s2 ⟨x1, n1, hk1, hRestore, H2, F2⟩ + rw [WP.spec_ok] + have KS2 := ScChargeKeepA.consumedA hk1 total.bv + have FR2 : SchedFrameA τ1 (updKSc τ1 (scNameA m sc) (.schedContext { x1 with scConsumed := total.bv } n1)) := + frame_of_runA (by unfold setScObjRef; spres) hRestore + refine ⟨_, Q, Rq, ?_, ha1, H2, F1.trans F2 rfl rfl, PreO.trans FR1 FR2, ScChargeKeepA.trans KS1 KS2, + SH1.consumedA KS2 (y' := { x1 with scConsumed := total.bv }) (n' := n1) (by rw [hcur]) (x := x1) (k := n1) (by rw [hcur]; exact hk1) rfl, hClock, hc1⟩ + unfold fl4HandleYield + rw [nd_gets_bindSc, hcur, nd_bind_oneSc (Sched.getSchedContext_eqA hk), nd_gets_bindSc, + nd_bind_oneSc (getRefillHead_eqA hk hxne), hAmount, nd_bind_oneSc hCharge, ← hTotal] + exact hRestore + +end yld + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3KEntryExitA.lean b/proofs/Fl4ProofsA64/P3/Stage3KEntryExitA.lean new file mode 100644 index 000000000..e433aac7a --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3KEntryExitA.lean @@ -0,0 +1,578 @@ +/- +P3 (aarch64, A64-P3 Stage 3, kernel-entry composition, Plan A, item 2): **`JStage3EntryA`'s facts at the schedule +exit**. + +* `RunS k σ`: the queued threads, the release-queue threads and a pending switch target are runnable in the heap `k`, + and when resuming the current thread is the idle thread or runnable. It holds through `awaken`, `checkDomainTime` + and `fl4SelectThread` (`schedTail_ctRunA`): after the tail the current thread is idle or runnable. +* `fl4SwitchSchedContext_usedA`: after the context switch the consumed time is zero, or the current SC, its heap and the + consumed time are those before it. +* **`JStage3A.scheduleExitA`**: the whole `schedule` from the body's state: record and facts at the exit. +* **`exitFactsA`**: from the record at the post-fire state `σ1` with `BodyExitA σ1`, and the record at the exit `τ`: + `CurInvA`, `OffOKA`, `OffReadyA`, `ActIdleA`, the non-idle current SC, `ValidMachineTimeA` and an activatable current + thread. + +Named premises: [J] `BodyExitA σ` at the body's state (`fire_budgetA` carries it to `σ1`); the idle SC's ready head at +`τ` is the record's (`JStage3A.idleGapA`, record edit #8b `SIdleA.f7`); [S] `numNodes = 1` (the only idle thread is this core's); `scheduleExitA` keeps `hSig` [J], `hSched` [J] and `ArRootKeepA` [P]. + +New module. +-/ +import Fl4ProofsA64.P3.Stage3KEntryBudgetA +import Fl4ProofsA64.P3.Stage3SchedExitFrA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Classical + +noncomputable section + +/-- A runnable TCB of the heap `k`. -/ +def RunK (k : ObjRef → Option KernelObject) (t : ObjRef) : Prop := + ∃ x, k t = some (.tcb x) ∧ runnable x.tcbState = true + +/-- Queued, released and pending threads are runnable, and a resumed current thread is idle or runnable (module +docstring). -/ +structure RunS (k : ObjRef → Option KernelObject) (σ : State Unit) : Prop where + rq : ∀ d p t, t ∈ σ.readyQueues d p → RunK k t + rel : ∀ t ∈ σ.releaseQueue, RunK k t + sw : ∀ t, σ.schedulerAction = .switchThread t → RunK k t + ct : σ.schedulerAction = .resumeCurThread → σ.curThread = σ.idleThread ∨ RunK k σ.curThread + +/-- The current thread is idle or runnable. -/ +def CtRunK (k : ObjRef → Option KernelObject) (σ : State Unit) : Prop := + σ.curThread = σ.idleThread ∨ RunK k σ.curThread + +section +variable {k : ObjRef → Option KernelObject} + +/-- A ready-queue write that only adds the runnable `t`. -/ +theorem tcbSchedAction_runA (f : ObjRef → List ObjRef → List ObjRef) (t : ObjRef) (ht : RunK k t) + (hf : ∀ q u, u ∈ f t q → u = t ∨ u ∈ q) : + HT (RunS k) (tcbSchedAction (Z := Unit) f t) (fun _ σ => RunS k σ) := by + intro σ hσ r hr + unfold Fl4SpecA64.tcbSchedAction Fl4SpecA64.getTcbQueue Fl4SpecA64.setTcbQueue at hr + obtain ⟨⟨d, σ1⟩, h1, hr⟩ := mem_bindA.1 hr + have e1 : σ1 = σ := (HT.threadGet (P := fun s => s = σ) (Q := fun _ s => s = σ) Tcb.tcbDomain t + (fun _ h _ _ => h)) σ rfl _ h1 + rw [e1] at hr + obtain ⟨⟨p, σ2⟩, h2, hr⟩ := mem_bindA.1 hr + have e2 : σ2 = σ := (HT.threadGet (P := fun s => s = σ) (Q := fun _ s => s = σ) Tcb.tcbPriority t + (fun _ h _ _ => h)) σ rfl _ h2 + rw [e2] at hr + obtain ⟨⟨q, σ3⟩, h3, hr⟩ := mem_bindA.1 hr + obtain ⟨⟨qs, σ4⟩, h4, h5⟩ := mem_bindA.1 h3 + have e4 : (qs, σ4) = (σ.readyQueues, σ) := h4 + cases e4 + have e5 : (q, σ3) = (σ.readyQueues d p, σ) := h5 + cases e5 + obtain ⟨⟨q', σ5⟩, h6, hr⟩ := mem_bindA.1 hr + have e6 : (q', σ5) = (f t (σ.readyQueues d p), σ) := h6 + cases e6 + have e7 : r = ((), _) := hr + rw [e7] + refine ⟨fun d' p' u hu => ?_, hσ.rel, hσ.sw, hσ.ct⟩ + dsimp only at hu + split at hu + · rcases hf _ _ hu with rfl | h + · exact ht + · exact hσ.rq _ _ _ h + · exact hσ.rq _ _ _ hu + +theorem enq_memA (q : List ObjRef) (t u : ObjRef) (h : u ∈ tcbSchedEnqueue t q) : u = t ∨ u ∈ q := by + unfold Fl4SpecA64.tcbSchedEnqueue at h + split at h + · exact List.mem_cons.1 h + · exact Or.inr h + +theorem app_memA (q : List ObjRef) (t u : ObjRef) (h : u ∈ tcbSchedAppend t q) : u = t ∨ u ∈ q := by + unfold Fl4SpecA64.tcbSchedAppend at h + split at h + · rcases List.mem_append.1 h with h | h + · exact Or.inr h + · exact Or.inl (List.mem_singleton.1 h) + · exact Or.inr h + +theorem deq_memA (q : List ObjRef) (t u : ObjRef) (h : u ∈ tcbSchedDequeue t q) : u = t ∨ u ∈ q := + Or.inr (List.mem_filter.1 h).1 + +theorem RunS.setNe {σ : State Unit} (h : RunS k σ) (a : SchedulerAction) (ne : a ≠ .resumeCurThread) + (hs : ∀ t, a = .switchThread t → RunK k t) : RunS k { σ with schedulerAction := a } := + ⟨h.rq, h.rel, fun t e => hs t e, fun e => absurd e ne⟩ + +theorem setAct_runA (a : SchedulerAction) (ne : a ≠ .resumeCurThread) (hs : ∀ t, a = .switchThread t → RunK k t) : + HT (RunS k) (setSchedulerAction (Z := Unit) a) (fun _ σ => RunS k σ) := by + unfold Fl4SpecA64.setSchedulerAction + exact HT.modify _ fun σ h => h.setNe a ne hs + +theorem resched_runA : HT (RunS k) (rescheduleRequired (Z := Unit)) (fun _ σ => RunS k σ) := by + have fin : HT (RunS k) (setSchedulerAction (Z := Unit) .chooseNewThread) (fun _ σ => RunS k σ) := + setAct_runA _ (by intro e; cases e) (by intro t e; cases e) + unfold Fl4SpecA64.rescheduleRequired + refine HT.bind (Q := fun a σ => RunS k σ ∧ σ.schedulerAction = a) (HT.gets _ fun σ h => ⟨h, rfl⟩) fun a => ?_ + cases a with + | resumeCurThread => dsimp only; exact HT.mono fin (fun _ h => h.1) (fun _ _ h => h) + | chooseNewThread => dsimp only; exact HT.mono fin (fun _ h => h.1) (fun _ _ h => h) + | switchThread t => + dsimp only + refine HT.mono (P := fun σ => RunS k σ ∧ RunK k t) ?_ (fun σ h => ⟨h.1, h.1.sw t h.2⟩) (fun _ _ h => h) + refine HT.bind (Q := fun _ σ => RunS k σ ∧ RunK k t) (HT.gets _ fun σ h => h) fun sched => ?_ + refine HT.bind (Q := fun _ σ => RunS k σ) ?_ fun _ => fin + refine HT.when (fun _ => ?_) (fun _ σ h => h.1) + refine HT.bind (Q := fun _ σ => RunS k σ ∧ RunK k t) (HT.threadGet _ t fun σ h _ _ => h) fun scOpt => ?_ + refine HT.bind (Q := fun _ σ => RunS k σ ∧ RunK k t) (HT.assertOpt _ fun _ _ σ h => h) fun scp => ?_ + refine HT.bind (Q := fun _ σ => RunS k σ ∧ RunK k t) + (by unfold Fl4SpecA64.getScRefillSufficient; exact HT.getsThe _ fun σ h _ _ => h) fun _ => ?_ + refine HT.bind (Q := fun _ σ => RunS k σ ∧ RunK k t) + (by unfold Fl4SpecA64.getScRefillReady; exact HT.getsThe _ fun σ h _ _ => h) fun _ => ?_ + refine HT.bind (Q := fun _ σ => RunS k σ ∧ RunK k t) (HT.assert fun _ σ h => h) fun _ => ?_ + intro σ h r hr + exact tcbSchedAction_runA _ t h.2 (fun q u hu => enq_memA q t u hu) σ h.1 r hr + +theorem possibleSwitchTo_runA (t : ObjRef) (ht : RunK k t) : + HT (RunS k) (possibleSwitchTo (Z := Unit) t) (fun _ σ => RunS k σ) := by + unfold Fl4SpecA64.possibleSwitchTo Fl4SpecA64.getTcbObjRef + refine HT.bind (Q := fun _ σ => RunS k σ) (HT.threadGet _ t fun σ h _ _ => h) fun scOpt => ?_ + refine HT.bind (Q := fun _ σ => RunS k σ) (HT.gets _ fun σ h => h) fun inq => ?_ + refine HT.when (fun _ => ?_) (fun _ σ h => h) + refine HT.bind (Q := fun _ σ => RunS k σ) (HT.gets _ fun σ h => h) fun curDom => ?_ + refine HT.bind (Q := fun _ σ => RunS k σ) (HT.threadGet _ t fun σ h _ _ => h) fun targetDom => ?_ + refine HT.bind (Q := fun _ σ => RunS k σ) (HT.gets _ fun σ h => h) fun action => ?_ + have enq := tcbSchedAction_runA (k := k) tcbSchedEnqueue t ht (fun q u hu => enq_memA q t u hu) + refine HT.ite (fun _ => enq) (fun _ => HT.ite (fun _ => HT.bind resched_runA fun _ => enq) (fun _ => ?_)) + exact setAct_runA _ (by intro e; cases e) (fun u e => by injection e with e; subst e; exact ht) + +theorem tcbReleaseDequeue_runA : HT (RunS k) (tcbReleaseDequeue (Z := Unit)) (fun _ σ => RunS k σ) := by + unfold Fl4SpecA64.tcbReleaseDequeue + refine HT.bind (Q := fun rq σ => RunS k σ ∧ rq = σ.releaseQueue) (HT.gets _ fun σ h => ⟨h, rfl⟩) fun rq => ?_ + refine HT.bind (Q := fun _ σ => RunS k σ ∧ (rq ≠ [] → RunK k (the rq.head?)) ∧ rq ≠ []) + (HT.assert fun hne σ h => ⟨h.1, fun _ => h.1.rel _ (by + rw [← h.2] + cases rq with + | nil => exact absurd rfl hne + | cons a l => exact List.mem_cons_self), hne⟩) fun _ => ?_ + refine HT.mono (P := fun σ => RunS k σ ∧ RunK k (the rq.head?)) ?_ (fun σ h => ⟨h.1, h.2.1 h.2.2⟩) (fun _ _ h => h) + intro σ h r hr + obtain ⟨⟨u, σ1⟩, h1, h2⟩ := mem_bindA.1 hr + have T : HT (RunS k) (tcbReleaseRemove (Z := Unit) (the rq.head?)) (fun _ σ => RunS k σ) := by + unfold Fl4SpecA64.tcbReleaseRemove + refine HT.bind (Q := fun qs σ => RunS k σ ∧ ∀ u ∈ qs, RunK k u) (HT.gets _ fun σ h => ⟨h, h.rel⟩) fun qs => ?_ + refine HT.bind (Q := fun _ σ => RunS k σ ∧ ∀ u ∈ qs, RunK k u) + (HT.when (fun _ => HT.modify _ fun σ h => ⟨⟨h.1.rq, h.1.rel, h.1.sw, h.1.ct⟩, h.2⟩) (fun _ σ h => h)) fun _ => ?_ + refine HT.bind (Q := fun qs' σ => RunS k σ ∧ ∀ u ∈ qs', RunK k u) + (HT.pure _ fun σ h => ⟨h.1, fun u hu => h.2 u (List.mem_filter.1 hu).1⟩) fun qs' => ?_ + exact HT.modify _ fun σ h => ⟨h.1.rq, h.2, h.1.sw, h.1.ct⟩ + have R1 := T σ h.1 _ h1 + exact possibleSwitchTo_runA _ h.2 σ1 R1 r h2 + +theorem awaken_runA : HT (RunS k) (awaken (Z := Unit)) (fun _ σ => RunS k σ) := by + unfold Fl4SpecA64.awaken + exact HT.whileLoop (RunS k) () fun _ => tcbReleaseDequeue_runA + +theorem checkDomainTime_runA : HT (RunS k) (checkDomainTime (Z := Unit)) (fun _ σ => RunS k σ) := by + unfold Fl4SpecA64.checkDomainTime + refine HT.bind (Q := fun _ σ => RunS k σ) (HT.gets _ fun σ h => h) fun exp => ?_ + refine HT.when (fun _ => ?_) (fun _ σ h => h) + refine HT.bind (Q := fun _ σ => RunS k σ) (HT.modify _ fun σ h => ⟨h.rq, h.rel, h.sw, h.ct⟩) fun _ => ?_ + exact resched_runA + +theorem switchTo_runA (t : ObjRef) (ht : RunK k t) : + HT (RunS k) (fl4SwitchToThread (Z := Unit) t) (fun _ σ => RunS k σ ∧ CtRunK k σ) := by + unfold Fl4SpecA64.fl4SwitchToThread + refine HT.bind (Q := fun _ σ => RunS k σ) (tcbSchedAction_runA _ t ht (fun q u hu => deq_memA q t u hu)) fun _ => ?_ + exact HT.modify _ fun σ h => ⟨⟨h.rq, h.rel, h.sw, fun _ => Or.inr ht⟩, Or.inr ht⟩ + +theorem chooseNew_runA (limit : Nat) : + HT (RunS k) (fl4ChooseNewThread (Z := Unit) limit) (fun _ σ => RunS k σ ∧ CtRunK k σ) := by + unfold Fl4SpecA64.fl4ChooseNewThread + refine HT.bind (Q := fun _ σ => RunS k σ) (HT.gets _ fun σ h => h) fun domTime => ?_ + refine HT.bind (Q := fun _ σ => RunS k σ) (HT.when (fun _ => ?_) (fun _ σ h => h)) fun _ => ?_ + · intro σ h r hr + have q := (by + unfold Fl4SpecA64.fl4NextDomain + refine HT.bind (Q := fun _ σ => RunS k σ) (HT.modify _ fun σ h => ⟨h.rq, h.rel, h.sw, h.ct⟩) fun _ => ?_ + intro σ h r hr + unfold Fl4SpecA64.doExtendedOp at hr + obtain ⟨⟨ex, a⟩, h1, hr⟩ := mem_bindA.1 hr + have e1 : (ex, a) = (σ, σ) := h1 + cases e1 + obtain ⟨⟨v, b⟩, h2, hr⟩ := mem_bindA.1 hr + have e2 : b = σ := h2.2 + rw [e2] at hr + have e3 : r = ((), _) := hr + rw [e3] + exact ⟨h.rq, h.rel, h.sw, h.ct⟩ : HT (RunS k) (fl4NextDomain (Z := Unit) limit) (fun _ σ => RunS k σ)) + exact q σ h r hr + unfold Fl4SpecA64.fl4ChooseThread + refine HT.bind (Q := fun s σ => RunS k σ ∧ s = σ) (HT.get fun σ h => ⟨h, rfl⟩) fun s => ?_ + refine HT.ite (fun _ => ?_) (fun hq => ?_) + · exact HT.modify _ fun σ h => by + have e : s.idleThread = σ.idleThread := by rw [h.2] + exact ⟨⟨h.1.rq, h.1.rel, h.1.sw, fun _ => Or.inl e⟩, Or.inl e⟩ + · intro σ h r hr + obtain ⟨p, hp, hm⟩ := maxNonEmptyQueue_mem (s.readyQueues s.curDomain) hq + have hmem : the (maxNonEmptyQueue (s.readyQueues s.curDomain)).head? ∈ s.readyQueues s.curDomain p := by + rw [hm] + cases hl : s.readyQueues s.curDomain p with + | nil => exact absurd hl hp + | cons a l => exact List.mem_cons_self + rw [h.2] at hmem + have := h.1.rq _ _ _ hmem + rw [← h.2] at this + exact switchTo_runA _ this σ h.1 r hr + +theorem selectCandidate_runA (limit : Nat) (cand cur idle : ObjRef) (dom : Domain) (wr : Bool) (hc : RunK k cand) : + HT (RunS k) (fl4SelectCandidate (Z := Unit) limit cand cur idle dom wr) (fun _ σ => RunS k σ ∧ CtRunK k σ) := by + unfold Fl4SpecA64.fl4SelectCandidate + refine HT.bind (Q := fun _ σ => RunS k σ) (HT.threadGet _ cand fun σ h _ _ => h) fun prio => ?_ + have chain : ∀ f : ObjRef → List ObjRef → List ObjRef, (∀ q u, u ∈ f cand q → u = cand ∨ u ∈ q) → + HT (RunS k) (do + tcbSchedAction (Z := Unit) f cand + setSchedulerAction .chooseNewThread + fl4ChooseNewThread limit) (fun _ σ => RunS k σ ∧ CtRunK k σ) := fun f hf => + HT.bind (tcbSchedAction_runA f cand hc hf) fun _ => + HT.bind (setAct_runA _ (by intro e; cases e) (by intro t e; cases e)) fun _ => chooseNew_runA limit + have tail : ∀ cp : Priority, HT (RunS k) (show SMonad Unit Unit from do + let highest ← NonDet.gets (isHighestPrio dom prio) + if (cur = idle ∨ prio < cp) ∧ ¬highest then + (do tcbSchedAction (Z := Unit) tcbSchedEnqueue cand + setSchedulerAction .chooseNewThread + fl4ChooseNewThread limit) + else if wr = true ∧ prio = cp then + (do tcbSchedAction (Z := Unit) tcbSchedAppend cand + setSchedulerAction .chooseNewThread + fl4ChooseNewThread limit) + else fl4SwitchToThread cand) (fun _ σ => RunS k σ ∧ CtRunK k σ) := fun cp => by + refine HT.bind (Q := fun _ σ => RunS k σ) (HT.gets _ fun σ h => h) fun highest => ?_ + exact HT.ite (fun _ => chain _ (fun q u hu => enq_memA q cand u hu)) + (fun _ => HT.ite (fun _ => chain _ (fun q u hu => app_memA q cand u hu)) (fun _ => switchTo_runA cand hc)) + dsimp only + exact HT.ite (fun _ => HT.bind (Q := fun _ σ => RunS k σ) (HT.pure _ fun σ h => h) tail) + (fun _ => HT.bind (Q := fun _ σ => RunS k σ) (HT.threadGet _ cur fun σ h _ _ => h) tail) + +theorem selectThread_runA (limit : Nat) : + HT (fun σ => RunS k σ ∧ σ.kheap = k) (fl4SelectThread (Z := Unit) limit) (fun _ σ => CtRunK k σ) := by + unfold Fl4SpecA64.fl4SelectThread Fl4SpecA64.setSchedulerAction + refine HT.bind (Q := fun _ σ => CtRunK k σ) ?_ fun _ => HT.modify _ fun σ h => h + unfold Fl4SpecA64.fl4SelectAction + refine HT.bind (Q := fun s σ => (RunS k σ ∧ σ.kheap = k) ∧ s = σ) (HT.get fun σ h => ⟨h, rfl⟩) fun s => ?_ + refine HT.when (fun hne => ?_) (fun hn σ h => h.1.1.ct (by rw [← h.2]; exact not_not.1 hn)) + dsimp only + refine HT.bind (Q := fun _ σ => RunS k σ ∧ (∀ t, s.schedulerAction = .switchThread t → RunK k t)) + (HT.when (fun hw => ?_) (fun _ σ h => ⟨h.1.1, fun t e => h.1.1.sw t (by rw [← h.2]; exact e)⟩)) fun _ => ?_ + · intro σ h r hr + have hs : s = σ := h.2 + obtain ⟨-, -, x, ex, hrun⟩ := schedulable_actA hw + have rc : RunK k s.curThread := ⟨x, by rw [← h.1.2, ← hs]; exact ex, hrun⟩ + have sw : ∀ t, s.schedulerAction = .switchThread t → RunK k t := fun t e => h.1.1.sw t (by rw [← hs]; exact e) + exact ⟨tcbSchedAction_runA _ _ rc (fun q u hu => enq_memA q _ u hu) σ h.1.1 r hr, sw⟩ + cases hA : s.schedulerAction with + | resumeCurThread => exact absurd hA hne + | chooseNewThread => + exact HT.mono (chooseNew_runA limit) (fun _ h => h.1) (fun _ _ h => h.2) + | switchThread c => + intro σ h r hr + exact (selectCandidate_runA limit c _ _ _ _ (h.2 c rfl) σ h.1 r hr).2 + +end + +/-! ## The context switch and the consumed time -/ + +theorem fl4CommitTime_zeroA : HT (fun _ => True) (fl4CommitTime (Z := Unit)) (fun _ σ => σ.consumedTime = 0) := by + unfold Fl4SpecA64.fl4CommitTime + refine HT.bind (Q := fun _ _ => True) (fun _ _ _ _ => trivial) fun _ => ?_ + refine HT.bind (Q := fun _ _ => True) (fun _ _ _ _ => trivial) fun _ => ?_ + refine HT.bind (Q := fun _ _ => True) (fun _ _ _ _ => trivial) fun _ => ?_ + exact HT.modify _ fun _ _ => rfl + +/-- After the context switch the consumed time is zero, or the current SC, the heap and the consumed time are kept. -/ +theorem fl4SwitchSchedContext_usedA (σ0 : State Unit) : + HT (fun σ => σ = σ0) (fl4SwitchSchedContext (Z := Unit)) + (fun _ σ => σ.consumedTime = 0 ∨ + (σ.curSc = σ0.curSc ∧ σ.kheap = σ0.kheap ∧ σ.consumedTime = σ0.consumedTime)) := by + unfold Fl4SpecA64.fl4SwitchSchedContext + refine HT.bind (Q := fun s σ => σ = σ0 ∧ s = σ0) (HT.get fun σ h => ⟨h, h⟩) fun s => ?_ + refine HT.bind (Q := fun _ σ => σ = σ0 ∧ s = σ0) ?_ fun target => ?_ + · refine HT.bind (Q := fun _ σ => σ = σ0 ∧ s = σ0) + (show HT _ (threadGet (Z := Unit) Tcb.tcbSchedContext s.curThread) _ from + HT.threadGet _ _ fun σ h _ _ => h) fun o => HT.assertOpt _ fun _ _ σ h => h + refine HT.bind (Q := fun _ σ => σ.reprogramTimer = true ∨ (σ = σ0 ∧ target = σ0.curSc)) + (HT.when (fun _ => ?_) (fun hn σ h => Or.inr ⟨h.1, by rw [← h.2]; exact not_not.1 hn⟩)) fun _ => ?_ + · refine HT.bind (Q := fun _ σ => σ.reprogramTimer = true) (HT.modify _ fun _ _ => rfl) fun _ => ?_ + intro σ h r hr + have P : TmPresA (ifConstantBandwidthRefillUnblockCheck (Z := Unit) (some target)) := + (inferInstance : TmInst (ifCondRefillUnblockCheck (Z := Unit) (some target) none none)).pres + exact Or.inl ((P σ r hr).rep h) + refine HT.bind (Q := fun rp σ => (σ.reprogramTimer = true ∨ (σ = σ0 ∧ target = σ0.curSc)) ∧ rp = σ.reprogramTimer) + (HT.gets _ fun σ h => ⟨h, rfl⟩) fun rp => ?_ + refine HT.bind (Q := fun _ σ => σ.consumedTime = 0 ∨ (σ = σ0 ∧ target = σ0.curSc)) + (HT.when (fun _ => HT.mono fl4CommitTime_zeroA (fun _ _ => trivial) (fun _ _ h => Or.inl h)) + (fun hn σ h => Or.inr (h.1.resolve_left (by rw [← h.2]; exact hn)))) fun _ => ?_ + refine HT.modify _ fun σ h => ?_ + rcases h with h | ⟨e, t⟩ + · exact Or.inl h + · subst e; exact Or.inr ⟨t, rfl, rfl⟩ + +/-! ## The exit facts -/ + +/-- **The idle facts of record edit #8b (`SIdleA`) at the record's own state**: the idle thread's slot-1 cap is null and +it has no VCPU; the idle SC is active and its head refill is ready. -/ +theorem _root_.Fl4ProofsA64.P3.JStage3A.idleGapA {S A : Type} {st : machine.Store S} [L : LawfulStore st] + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} {ia ie : Nat} + {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {J5 J7 : JSlotA S} {s : S} {a : A} {σ : State Unit} {m : McState} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes J5 J7 s a σ m) : + vtCapA σ σ.idleThread = some .nullCap ∧ vcpuOfA σ σ.idleThread = none ∧ + ∀ y k, σ.kheap idleScPtr = some (.schedContext y k) → + scActive y = true ∧ refillReady σ.curTime (refillHd y) = true := by + have hc : m.curCore < fl4A64Platform.numNodes := J.sinv.mc.curCore_lt + have v : σ.idleThread = m.idleThreadOf m.curCore := J.sinv.view + refine ⟨by rw [v]; exact J.sidle.idleSlot _ hc, by rw [v]; exact J.sidle.idleBound _ hc, fun y k hy => ?_⟩ + obtain ⟨ha, hr⟩ := J.sidle.f7 y k hy + refine ⟨ha, ?_⟩ + have h0 : (refillHd y).rTime + (0 : Ticks) = (refillHd y).rTime := by bv_omega + show decide ((refillHd y).rTime + (0 : Ticks) ≤ σ.curTime) = true + rw [h0]; exact decide_eq_true hr + +/-- **`JStage3EntryA`'s facts at the schedule exit** (module docstring). -/ +theorem exitFactsA {S A : Type} {st : machine.Store S} [L : LawfulStore st] {AR : A → HwRootA → Prop} + {Z Z' : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} + {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} {J5 J7 : JSlotA S} + {s1 s' : S} {a1 a' : A} {σ1 τ : State Unit} {m : McState} {limit : Nat} {view : State Unit → Fl4TimerView} + (h1 : fl4A64Platform.numNodes = 1) + (J1 : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes J5 J7 s1 a1 σ1 m) + (J2 : JStage3A st AR Z' H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes J5 J7 s' a' τ m) + (R1 : FireRecA σ1) (B1 : BodyExitA σ1) (tail : ((), τ) ∈ (SchedTail limit view σ1).1) : + CurInvA τ ∧ OffOKA τ ∧ OffReadyA τ ∧ ActIdleA τ ∧ (τ.curSc ≠ idleScPtr → NotIdleScA m τ.curSc) ∧ + ValidMachineTimeA τ ∧ τ.schedulerAction = .resumeCurThread ∧ + (∀ x, τ.kheap τ.curThread = some (.tcb x) → ActivateStateOKA x.tcbState) := by + have full := tail + obtain ⟨⟨⟨⟩, σ2⟩, h21, h⟩ := mem_bindA.1 tail + obtain ⟨⟨⟨⟩, σ3⟩, h31, h⟩ := mem_bindA.1 h + obtain ⟨⟨⟨⟩, σ4⟩, h41, h5⟩ := mem_bindA.1 h + have q2 : SfKeepA σ1 σ2 := awaken_qkA σ1 _ h21 + have q3 : SfKeepA σ2 σ3 := checkDomainTime_qkA σ2 _ h31 + have q4 : SfKeepA σ3 σ4 := fl4SelectThread_qkA limit σ3 _ h41 + have q : SfKeepA σ1 σ4 := PreO.trans q2 (PreO.trans q3 q4) + have r1 : RunS σ1.kheap σ1 := by + refine ⟨fun d p t ht => J1.sr.run.1 d p t ht, J1.sr.run.2, fun t e => ?_, fun e => ?_⟩ + · obtain ⟨-, -, x, ex, hr⟩ := schedulable_actA (R1.2.2.1 t e) + exact ⟨x, ex, hr⟩ + · obtain ⟨x, hx⟩ := J1.sinv.curTcb + have ok0 := B1.ctAct e x hx + -- [S] one core: the only idle thread is this core's + have own : x.tcbState = .idleThreadState → σ1.curThread = σ1.idleThread := fun h => by + obtain ⟨c, hc, ec⟩ := J1.sinv.onlyIdle _ x hx h + have : c = m.curCore := by + have lt : m.curCore < fl4A64Platform.numNodes := J1.sinv.mc.curCore_lt + exact (Nat.lt_one_iff.1 (lt_of_lt_of_eq hc h1)).trans (Nat.lt_one_iff.1 (lt_of_lt_of_eq lt h1)).symm + rw [ec, this]; exact J1.sinv.view.symm + rcases ok0 with h | h | h | h + · exact Or.inr ⟨x, hx, by rw [h]; rfl⟩ + · exact Or.inr ⟨x, hx, by rw [h]; rfl⟩ + · exact Or.inl (own h) + · exact Or.inr ⟨x, hx, by rw [h]; rfl⟩ + have r3 := checkDomainTime_runA σ2 (awaken_runA σ1 r1 _ h21) _ h31 + have c4 := selectThread_runA limit σ3 ⟨r3, q3.kh.trans q2.kh⟩ _ h41 + have act4 : σ4.schedulerAction = .resumeCurThread := by + unfold fl4SelectThread setSchedulerAction at h41 + obtain ⟨y, -, h⟩ := mem_bindA.1 h41 + have : (((), σ4) : Unit × State Unit) = ((), { y.2 with schedulerAction := .resumeCurThread }) := h + rw [(Prod.mk.inj this).2] + have K : ActKeepA σ4 τ := (fl4ScAndTimer_apres view) σ4 _ h5 + have act : τ.schedulerAction = .resumeCurThread := K.1.trans act4 + have tcb : ∀ p x, τ.kheap p = some (.tcb x) ↔ σ1.kheap p = some (.tcb x) := fun p x => by + rw [K.2.2.2.1, q.kh] + have ctR : τ.curThread = τ.idleThread ∨ RunK σ1.kheap τ.curThread := by + rw [K.2.1, K.2.2.2.2.2.1]; exact c4 + have hc : HcA τ := fl4ScAndTimer_hc view σ4 trivial _ h5 + -- the current thread + obtain ⟨xc, hxc⟩ := J2.sinv.curTcb + have hsc : xc.tcbSchedContext = some τ.curSc := hc xc hxc + obtain ⟨xi, hxi, sti⟩ := J2.sinv.idleTcb + have idleNR : τ.idleThread ∉ τ.releaseQueue := fun hm => by + obtain ⟨y, hy, hr⟩ := J2.sr.run.2 _ hm + rw [hxi] at hy + obtain rfl := KernelObject.tcb.inj (Option.some.inj hy) + rw [sti] at hr + cases hr + have idleSc : τ.curThread = τ.idleThread → τ.curSc = idleScPtr := fun e => by + obtain ⟨x, y, k, hx, es, -⟩ := J2.sinv.idleSc + rw [e, hx] at hxc + obtain rfl := KernelObject.tcb.inj (Option.some.inj hxc) + rw [es] at hsc + exact (Option.some.inj hsc).symm + have run : τ.curThread ≠ τ.idleThread → runnable xc.tcbState = true := fun ne => by + obtain ⟨x, hx, hr⟩ := ctR.resolve_left ne + rw [← tcb, hxc] at hx + obtain rfl := KernelObject.tcb.inj (Option.some.inj hx) + exact hr + have state : (τ.curThread = τ.idleThread ∧ xc.tcbState = .idleThreadState) ∨ + (τ.curThread ≠ τ.idleThread ∧ runnable xc.tcbState = true) := by + by_cases e : τ.curThread = τ.idleThread + · rw [e, hxi] at hxc + obtain rfl := KernelObject.tcb.inj (Option.some.inj hxc) + exact Or.inl ⟨e, sti⟩ + · exact Or.inr ⟨e, run e⟩ + have ok : ActivateStateOKA xc.tcbState ∧ ipcQueuedThreadState xc.tcbState = false := by + rcases state with ⟨-, h⟩ | ⟨-, h⟩ + · rw [h]; exact ⟨Or.inr (Or.inr (Or.inl rfl)), rfl⟩ + · revert h + unfold ActivateStateOKA + cases xc.tcbState with + | running => exact fun _ => ⟨Or.inl rfl, rfl⟩ + | restart => exact fun _ => ⟨Or.inr (Or.inl rfl), rfl⟩ + | ext v => cases v; exact fun _ => ⟨Or.inr (Or.inr (Or.inr rfl)), rfl⟩ + | _ => intro h; cases h + have active : τ.curSc ≠ idleScPtr → τ.curThread ≠ τ.idleThread ∧ + (∀ y k, τ.kheap τ.curSc = some (.schedContext y k) → scActive y = true) ∧ τ.curThread ∉ τ.releaseQueue := by + intro ni + have ne : τ.curThread ≠ τ.idleThread := fun e => ni (idleSc e) + obtain ⟨⟨x, sc, hx, es, y0, k0, h0, a0⟩, nr⟩ := J2.schedValid.act act ne + rw [hxc] at hx + obtain rfl := KernelObject.tcb.inj (Option.some.inj hx) + rw [hsc] at es + obtain rfl := Option.some.inj es + refine ⟨ne, fun y k hy => ?_, nr⟩ + rw [h0] at hy + have := Option.some.inj hy + injection this with e1 e2 + rw [← e1]; exact a0 + have notRel : τ.curThread ∉ τ.releaseQueue := by + by_cases e : τ.curThread = τ.idleThread + · rw [e]; exact idleNR + · exact (J2.schedValid.act act e).2 + have curInv : CurInvA τ := + ⟨⟨xc, hxc, hsc, ok.2⟩, J2.sinv.curSc, fun ni => (active ni).2.1, notRel⟩ + have actIdle : ActIdleA τ := by + refine ⟨fun t e => (by rw [act] at e; cases e), ?_⟩ + obtain ⟨x, y, k, hx, es, -⟩ := J2.sinv.idleSc + rw [hxi] at hx + obtain rfl := KernelObject.tcb.inj (Option.some.inj hx) + exact ⟨xi, hxi, es, sti, idleNR⟩ + have notIdle : τ.curSc ≠ idleScPtr → NotIdleScA m τ.curSc := by + intro ni + have ne := (active ni).1 + refine ⟨ni, fun c hcn e => ?_⟩ + have nt : ∀ c < fl4A64Platform.numNodes, τ.curThread ≠ m.idleThreadOf c := fun c hc' e' => by + obtain ⟨y, hy, hs⟩ := J2.sidle.f3 c hc' + rw [← e', hxc] at hy + obtain rfl := KernelObject.tcb.inj (Option.some.inj hy) + have := run ne + rw [hs] at this + cases this + have := J2.sr.inv.tcbScNotIdle _ xc _ hxc hsc nt + exact this.2 c hcn e + -- the context switch + have full5 := h5 + unfold fl4ScAndTimer at h5 + obtain ⟨⟨⟨⟩, σ5⟩, h51, h⟩ := mem_bindA.1 h5 + have tm : TmKeepA σ4 σ5 := fl4SwitchSchedContext_tmA σ4 _ h51 + have used := fl4SwitchSchedContext_usedA σ4 σ4 rfl _ h51 + have after : SfKeepA σ5 τ := by + have P : SfPresA (do + let reprogram ← NonDet.gets fun s : State Unit => s.reprogramTimer + NonDet.when reprogram do + fl4SetNextInterrupt (Z := Unit) view + NonDet.modify fun s => { s with reprogramTimer := false }) := by sfkres + exact P σ5 _ h + have time : τ.curTime = σ1.curTime := after.time.trans (tm.time.trans q.time) + have valid : ValidMachineTimeA τ := by + unfold ValidMachineTimeA + rw [time, after.lmt, tm.lmt, q.lmt] + exact B1.validTime + have off : OffOKA τ ∧ OffReadyA τ := by + have unused : τ.consumedTime = 0 → OffOKA τ ∧ OffReadyA τ := fun z' => by + refine ⟨fun h => ?_, fun y k hy => ?_⟩ + · rw [z'] at h; exact (BitVec.lt_irrefl _ h).elim + · have rdy : refillReady τ.curTime (refillHd y) = true := by + by_cases ni : τ.curSc = idleScPtr + · rw [ni] at hy; exact (J2.idleGapA.2.2 y k hy).2 + · obtain ⟨ne, ac, nr⟩ := active ni + exact (J2.sr.rd τ.curThread xc hxc (Or.inl (run ne)) xc τ.curSc y k hxc hsc hy (ac y k hy) nr).1 + have h0 : (refillHd y).rTime + (0 : Ticks) = (refillHd y).rTime := by bv_omega + change decide ((refillHd y).rTime + (0 : Ticks) ≤ τ.curTime) = true at rdy + rw [h0] at rdy + have := of_decide_eq_true rdy + rw [BitVec.le_def] at this + rw [z'] + simpa using this + rcases used with z | ⟨ecs, ek, eu⟩ + · exact unused (after.used.trans z) + by_cases hz : σ1.consumedTime = 0 + · exact unused (after.used.trans (eu.trans (q.used.trans hz))) + · have csc : τ.curSc = σ1.curSc := after.csc.trans (ecs.trans q.csc) + have kh : τ.kheap = σ1.kheap := after.kh.trans (ek.trans q.kh) + have us : τ.consumedTime = σ1.consumedTime := after.used.trans (eu.trans q.used) + have bud : σ1.consumedTime ≠ 0 → ∀ y k, τ.kheap τ.curSc = some (.schedContext y k) → OffReadyA σ1 ∧ OffOKA σ1 := + fun hne y k hy => by + have c : τ.curSc = idleScPtr ∨ scActive y = true := by + by_cases ni : τ.curSc = idleScPtr + · exact Or.inl ni + · exact Or.inr ((active ni).2.1 y k hy) + rw [csc] at c + rw [kh, csc] at hy + exact B1.budget y k hy c (Or.inl hne) + refine ⟨fun hu y k hy _ => ?_, fun y k hy => ?_⟩ + · obtain ⟨-, O⟩ := bud hz y k hy + rw [us] at hu ⊢ + rw [time] + rw [kh, csc] at hy + refine O hu y k hy fun ne => ?_ + have ne' : τ.curSc ≠ idleScPtr := by rw [csc]; exact ne + exact (active ne').2.1 y k (by rw [kh, csc]; exact hy) + · obtain ⟨R, -⟩ := bud hz y k hy + rw [us, time] + rw [kh, csc] at hy + exact R y k hy + refine ⟨curInv, off.1, off.2, actIdle, notIdle, valid, act, fun x hx => ?_⟩ + rw [hxc] at hx + rw [← KernelObject.tcb.inj (Option.some.inj hx)] + exact ok.1 + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +set_option maxHeartbeats 1000000 in +/-- **The scheduler at the record, with the entry facts of the exit**: from the record, `SwA` and `BodyExitA` at the +body's state, `schedule` returns with the record at the spec's `fl4Schedule` outcome `τ`, and `exitFactsA`'s facts. -/ +theorem JStage3A.scheduleExitA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {s : S} {a : A} {σ : State Unit} {m : McState} + {tm : A → Nat → DeadlineViewA} {view : State Unit → Fl4TimerView} + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) (arch : ArchSwFA st ar) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hSw : SwA σ) (B : BodyExitA σ) + (hSig : ∀ (Q : U64 → U64 → List U64) (Rl : U64 → List U64) (s : S) (a : A) (σ : State Unit) (t badge : U64), + JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes + (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m → + SwA σ → t ∈ Z.T → L.mem s (boundA t) ≠ 0#u64 → + notification.send_signal st ar s a (L.mem s (boundA t)) badge ⦃ r => + ∃ σ' b Q' Rl', sendSignal (Z := Unit) (specPtr (L.mem s (boundA t))) b σ = ({((), σ')}, False) ∧ + JStage3A st AR { Z with Q := Q', R := Rl' } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ + fl4A64Platform.numNodes (J5A st) (J7A st (J7R2A st ar hPl.ipa)) r.1 r.2 σ' m ⦄) + (hSched : timer.fire st ar s a ⦃ r => ∃ σ1 Q R, + fl4TimerFires σ σ1 ∧ YieldQueueRelA (st := st) {Z with Q := Q, R := R} m κ r.1 σ1 ∧ IdleThreadsA m σ1 ⦄ → + ScheduleInputsA (st := st) Z m κ s a σ ar tm view AR) + (arK : ArRootKeepA ar AR) : + sched.schedule st ar s a ⦃ r => ∃ (τ : State Unit) (Q : U64 → U64 → List U64) (Rl : U64 → List U64) + (Tcur Ccur : Fl4SpecA64.CoreId → ObjRef) (hw : HwRootA), + ((), τ) ∈ (fl4Schedule kLimitA view σ).1 ∧ + JStage3A st AR { Z with Q := Q, R := Rl, curOf := Tcur, curScOf := Ccur, hw := hw } H OS U Ac Pp Tb Lv ia ie + N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) (J7A st (J7R2A st ar hPl.ipa)) r.1 r.2 τ m ∧ + CurInvA τ ∧ OffOKA τ ∧ OffReadyA τ ∧ ActIdleA τ ∧ (τ.curSc ≠ idleScPtr → NotIdleScA m τ.curSc) ∧ + ValidMachineTimeA τ ∧ τ.schedulerAction = .resumeCurThread ∧ + (∀ x, τ.kheap τ.curThread = some (.tcb x) → ActivateStateOKA x.tcbState) ⦄ := by + have hcore : m.curCore < fl4A64Platform.numNodes := by + rw [J.sr.sch.core]; exact hCs _ J.sr.sch.cpuCs + refine WP.spec_mono (JStage3A.scheduleRecFA hA hPl arch hCs J + (JStage3A.fire_budgetA hA hPl J hcore hSw B hSig) hSched arK) ?_ + rintro r ⟨f, σ1, τ, Qf, Rf, Q, Rl, Tcur, Ccur, hw, -, J1, R1, B1, -, tail, sch, -, J2⟩ + exact ⟨τ, Q, Rl, Tcur, Ccur, hw, sch, J2, exitFactsA h1 J1 J2 R1 B1 tail⟩ + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3KEntryFaultA.lean b/proofs/Fl4ProofsA64/P3/Stage3KEntryFaultA.lean new file mode 100644 index 000000000..8b75da0ac --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3KEntryFaultA.lean @@ -0,0 +1,328 @@ +/- +P3 (aarch64, A64-P3 Stage 3, kernel-entry composition, step 2): **the fault entries' `deliver`** (`handle_fault` of the +current thread after the budget prefix), wired to Lift 2b's `JSyscallA.faultRecvA` / `faultDonateA` / `faultNullA`. + +* `JSyscallA.budgetOkSysA`: `syscall.budget_ok` at the syscall-entry record. Its `true` result is at a + `JSyscallA` again (the timestamp keeps the action, the current thread and its SC; `OffOKA` from the sufficient head); + `JSyscallA.budgetOkTrueA` states that at a reached `true` outcome. The `false` branch is `hCharge` as before and its + result is `false` (`fl4CheckBudgetRestart_falseA`). +* `JSyscallA.deliverFaultA`: `handle_fault` of the current thread for a fault of `UsrFaultA` (user, VM and unknown + syscall faults), by the handler cap's scope `FaultNullCaseA` / `FaultRecvCaseA` / `FaultDonateCaseA` / + `FaultNoDonCaseA` (a reply push to a receiver with its own SC, `JSyscallA.faultNoDonA`) / + `FaultBlockCaseA` (no receiver: the thread blocks, `JSyscallA.faultBlockA`), or by the named `FaultDeliverA` for the + scopes no arm covers. +* `userException_recFA`, `vmFault_recFA`, `unknownSyscall_recFA`: the three entries at `FaultRelA` (the record, with the + reply stack free: the donating arm pushes a reply). Every delivery arm keeps the body end (`Sched.BodyEndA`), and + the entries' `finish` receives `Sched.BodyOKA` at the body's end state, on either budget answer + (`P3/Stage3BudgetFalseA`). + +The unknown-syscall fault is `handle_fault` with `UnknownSyscall` through the same arms (`usrFault_unkA`), with one new +premise: `FaultOKA` of an unknown syscall is `rbo = none ∨ TRAP_SYSCALLS clear`, so the entry takes `hTrap` +([J], the faulting thread's `TRAP_SYSCALLS` flag is clear at the post-prefix state). + +Named premises: [J] `hcase` (the handler cap's scope at the post-prefix state; its last disjunct is the delivery +itself, for the uncovered scopes); [J] `hTrap`; [J] `hCharge` (the budget branch, `KEntryChargeA`); `finish`. +-/ +import Fl4ProofsA64.P3.Stage3KEntryBudgetSysA +import Fl4ProofsA64.P3.Stage3BudgetFalseA +import Fl4ProofsA64.P3.Stage3FaultArmA +import Fl4ProofsA64.P3.Stage3FaultNoDonArmA +import Fl4ProofsA64.P3.Stage3SendBlkArmA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +set_option maxRecDepth 100000 + +/-! ## `handle_fault` of the current thread -/ + +section fault +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {ar : machine.Arch A} +variable {AR AR' : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {s : S} {a : A} {σ : State Unit} {m : McState} + +/-- The environment the fault arms read (their [P] and [S] premises on the platform and the architecture). -/ +structure FaultEnvA (st : machine.Store S) [LawfulStore st] (ar : machine.Arch A) (Z : XSetsA) : Prop where + hA : P0.ArchTotal ar + hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes + h1 : fl4A64Platform.numNodes = 1 + hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄ + hmax : maxTicksToUs = core.num.U64.MAX.bv + htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄ + +variable (st) in +/-- [S] The current thread's fault handler is `Null` (`JSyscallA.faultNullA`). -/ +def FaultNullCaseA (s : S) (σ : State Unit) : Prop := + capA st s (slotIdx (L.curW s) layout.tcb.SLOT_FAULT_HANDLER) = .Null ∧ + getCap (Z := Unit) (getTcbFaultHandlerPtr σ.curThread) σ = ({(.nullCap, σ)}, False) + +variable (st) in +/-- [S] The handler endpoint holds waiting receivers and no reply is pushed (`JSyscallA.faultRecvA`). -/ +def FaultRecvCaseA (E : Set U64) (s : S) (σ : State Unit) : Prop := + ∃ (p badge : U64) (sd rv g gr : Bool) (d : U64) (q : List ObjRef), + capA st s (slotIdx (L.curW s) layout.tcb.SLOT_FAULT_HANDLER) = .Endpoint p badge sd rv g gr ∧ + getCap (Z := Unit) (getTcbFaultHandlerPtr σ.curThread) σ = + ({(.endpointCap (specPtr p) badge.bv (rightsOfA sd rv g gr), σ)}, False) ∧ + p ∈ E ∧ σ.kheap (specPtr p) = some (.endpoint (.recvEP (specPtr d :: q))) ∧ + ((g = false ∧ gr = false) ∨ ∀ y, σ.kheap (specPtr d) = some (.tcb y) → + ∀ e r dd, y.tcbState ≠ .blockedOnReceive e (some r) dd) + +variable (st) in +/-- [S] The handler endpoint holds a waiting receiver with a reply object and no SC; the reply is pushed +(`JSyscallA.faultDonateA`). -/ +def FaultDonateCaseA (E : Set U64) (s : S) (σ : State Unit) : Prop := + ∃ (p badge : U64) (sd rv g gr : Bool) (d : U64) (q : List ObjRef), + capA st s (slotIdx (L.curW s) layout.tcb.SLOT_FAULT_HANDLER) = .Endpoint p badge sd rv g gr ∧ + getCap (Z := Unit) (getTcbFaultHandlerPtr σ.curThread) σ = + ({(.endpointCap (specPtr p) badge.bv (rightsOfA sd rv g gr), σ)}, False) ∧ + p ∈ E ∧ σ.kheap (specPtr p) = some (.endpoint (.recvEP (specPtr d :: q))) ∧ (g = true ∨ gr = true) ∧ + (∀ y, σ.kheap (specPtr d) = some (.tcb y) → + y.tcbSchedContext = none ∧ ∃ e r dd, y.tcbState = .blockedOnReceive e (some r) dd) ∧ + L.mem s (wa (L.curW s) 3664#u64) = 0#u64 + +variable (st) in +/-- [S] The handler endpoint holds a waiting receiver with a reply object and its own SC; the reply is pushed and +nothing is donated (`JSyscallA.faultNoDonA`). -/ +def FaultNoDonCaseA (E : Set U64) (s : S) (σ : State Unit) : Prop := + ∃ (p badge : U64) (sd rv g gr : Bool) (d : U64) (q : List ObjRef), + capA st s (slotIdx (L.curW s) layout.tcb.SLOT_FAULT_HANDLER) = .Endpoint p badge sd rv g gr ∧ + getCap (Z := Unit) (getTcbFaultHandlerPtr σ.curThread) σ = + ({(.endpointCap (specPtr p) badge.bv (rightsOfA sd rv g gr), σ)}, False) ∧ + p ∈ E ∧ σ.kheap (specPtr p) = some (.endpoint (.recvEP (specPtr d :: q))) ∧ (g = true ∨ gr = true) ∧ + (∀ y, σ.kheap (specPtr d) = some (.tcb y) → + y.tcbSchedContext ≠ none ∧ ∃ e r dd, y.tcbState = .blockedOnReceive e (some r) dd) + +variable (st) in +/-- [S] No receiver waits on the handler endpoint: it is idle or holds waiting senders, and the faulted thread blocks +on it (`JSyscallA.faultBlockA`). -/ +def FaultBlockCaseA (E : Set U64) (s : S) (σ : State Unit) : Prop := + ∃ (p badge : U64) (sd rv g gr : Bool) (x : Endpoint), + capA st s (slotIdx (L.curW s) layout.tcb.SLOT_FAULT_HANDLER) = .Endpoint p badge sd rv g gr ∧ + getCap (Z := Unit) (getTcbFaultHandlerPtr σ.curThread) σ = + ({(.endpointCap (specPtr p) badge.bv (rightsOfA sd rv g gr), σ)}, False) ∧ + p ∈ E ∧ σ.kheap (specPtr p) = some (.endpoint x) ∧ CanBlockA false x + +/-- **The record after a fault delivery**: the entry record, with the reply stack free (a donated reply is pushed). -/ +def FaultRelA (st : machine.Store S) [LawfulStore st] (ar : machine.Arch A) (AR : A → HwRootA → Prop) + (Z : XSetsA) (H : Heap) (OS U Ac Pp Tb : Set U64) (Lv : U64 → Nat) (ia ie : Nat) (N E Rp : Set U64) + (Bf : Set (U64 × Fl4SpecA64.Word)) (stack : U64 → List U64) (κ : U64 → U64) (nA : U64) (m : McState) + (s : S) (a : A) (σ : State Unit) : Prop := + ∃ stack' : U64 → List U64, JStage3QA st ar AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack' κ + fl4A64Platform.numNodes nA m s a σ + +/-- **The delivery goal** of the entries' `deliver`, at a state. -/ +def FaultDeliverA (st : machine.Store S) [LawfulStore st] (ar : machine.Arch A) (AR : A → HwRootA → Prop) + (Z : XSetsA) (H : Heap) (OS U Ac Pp Tb : Set U64) (Lv : U64 → Nat) (ia ie : Nat) (N E Rp : Set U64) + (Bf : Set (U64 × Fl4SpecA64.Word)) (stack : U64 → List U64) (κ : U64 → U64) (nA : U64) (m : McState) + (fe : error.Fault) (f : Fl4SpecA64.Fault) (s : S) (a : A) (σ : State Unit) : Prop := + (do let t ← sched.cur_thread st s; faults.handle_fault st ar s a t fe) ⦃ r => + StepPostA (fun s' a' σ' => FaultRelA st ar AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ nA m s' a' σ' ∧ + Sched.BodyEndA σ σ' m) + (do let t ← Fl4Spec.NonDet.gets fun k : State Unit => k.curThread; handleFault t f) σ r ⦄ + +theorem faultWords_tflA {T : Set U64} (hP : TcbPlaced T) {t : U64} (ht : t ∈ T) (s : S) (ty w0 w1 w2 : U64) : + L.mem (faultWordsA st s t ty w0 w1 w2) (tTflA t) = L.mem s (tTflA t) := + (faultWords_wrG (st := st) hP ht s ty w0 w1 w2).mem_eq + (field_aligned (K := 3456#u64) (hP t ht) (by simp) (by simp)) + (not_faultWA (hP t ht) (hP t ht) (by simp) (by simp)) + +set_option maxHeartbeats 2000000 in +/-- **`handle_fault` of the current thread at the syscall-entry record**, for a fault of `UsrFaultA`. -/ +theorem JSyscallA.deliverFaultA (hPl : ArchPlat ar) (Env : FaultEnvA st ar Z) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + {fe : error.Fault} {ty w0 w1 w2 : U64} {f : Fl4SpecA64.Fault} (F : UsrFaultA st (L.curW s) fe ty w0 w1 w2 f) + (hOk : ∀ (rbo : Option ObjRef) (x : Tcb), + FaultOKA (L := L) (faultWordsA st s (L.curW s) ty w0 w1 w2) (L.curW s) rbo x f) + (hcase : FaultNullCaseA st s σ ∨ FaultRecvCaseA st E s σ ∨ FaultDonateCaseA st E s σ ∨ FaultNoDonCaseA st E s σ ∨ + FaultBlockCaseA st E s σ ∨ FaultDeliverA st ar AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ hPl.ipa m fe f s a σ) : + FaultDeliverA st ar AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ hPl.ipa m fe f s a σ := by + have htZ : L.curW s ∈ Z.T := J.sr.curT + obtain ⟨x, hkx, -⟩ := J.ep.tcb _ htZ + have fin : ∀ (σ2 : State Unit) (s2 : S) (stack' : U64 → List U64), + ((), σ2) ∈ (handleFault (Z := Unit) σ.curThread f σ).1 → + faults.handle_fault st ar s a (L.curW s) fe = ok (s2, a) → + Sched.BodyEndA σ σ2 m → + (∃ Q Rl, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack' κ + fl4A64Platform.numNodes (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s2 a σ2 m) → + FaultDeliverA st ar AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ hPl.ipa m fe f s a σ := by + rintro σ2 s2 stack' hm hf B ⟨Q, Rl, J2⟩ + unfold FaultDeliverA + refine WP.spec_bind (sched.cur_thread.spec s) ?_ + intro t ht + have ht' : t = L.curW s := ht + subst ht' + rw [hf, WP.spec_ok] + refine ⟨σ2, ?_, ⟨stack', Q, Rl, Z.curOf, Z.curScOf, J2⟩, B⟩ + rw [nd_gets_bindSc] + exact hm + rcases hcase with ⟨hcap, hgc⟩ | ⟨p, badge, sd, rv, g, gr, d, q, hcap, hgc, hp, hk, hnp⟩ | + ⟨p, badge, sd, rv, g, gr, d, q, hcap, hgc, hp, hk, hg, hrcv, hV⟩ | + ⟨p, badge, sd, rv, g, gr, d, q, hcap, hgc, hp, hk, hg, hrcv⟩ | + ⟨p, badge, sd, rv, g, gr, x', hcap, hgc, hp, hk, hc⟩ | h + · obtain ⟨σ2, s2, hm, hf, B, J2⟩ := J.faultNullA Env.hA hPl Env.hCs Env.h1 fe f hcap hgc + exact fin σ2 s2 stack hm hf B J2 + · obtain ⟨σ2, s2, hm, hf, W, Q, Rl, J2⟩ := J.faultRecvA Env.hA hPl Env.hCs Env.h1 Env.hmb Env.hmax Env.htu F hOk + hcap hgc hp hk hnp + exact fin σ2 s2 stack hm hf (Sched.BodyEndA.ofWakeA J J2 W) ⟨Q, Rl, J2⟩ + · obtain ⟨σ2, s2, hm, hf, W, Q, Rl, r, -, J2⟩ := J.faultDonateA Env.hA hPl Env.hCs Env.h1 Env.hmb Env.hmax Env.htu + F hOk hcap hgc hp hk hg hrcv hV + exact fin σ2 s2 _ hm hf (Sched.BodyEndA.ofWakeA J J2 W) ⟨Q, Rl, J2⟩ + · obtain ⟨σ2, s2, hm, hf, W, Q, Rl, J2⟩ := J.faultNoDonA Env.hA hPl Env.hCs Env.h1 Env.hmb Env.hmax Env.htu F + hOk hcap hgc hp hk hg hrcv + exact fin σ2 s2 stack hm hf (Sched.BodyEndA.ofWakeA J J2 W) ⟨Q, Rl, J2⟩ + · obtain ⟨σ2, s2, hm, hf, B, J2⟩ := J.faultBlockA Env.hA hPl Env.hCs Env.h1 Env.hmb Env.hmax Env.htu F hcap hgc + hp hk hc + exact fin σ2 s2 stack hm hf B J2 + · exact h + +end fault + +/-! ## The entries -/ + +section entries +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {ar : machine.Arch A} +variable {AR AR' : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {s : S} {a : A} {σ : State Unit} {m : McState} {Clock : A → MachineState → Prop} + {Final : S → A → State Unit → Prop} + +/-- The record at a fault entry with the premises of the budget prefix (`P3/Stage3KEntryBudgetRecA`). -/ +structure KEntryFaultInA (st : machine.Store S) [L : LawfulStore st] (ar : machine.Arch A) + (AR AR' : A → HwRootA → Prop) (Z : XSetsA) (H : Heap) (OS U Ac Pp Tb : Set U64) (Lv : U64 → Nat) + (ia ie : Nat) (N E Rp : Set U64) (Bf : Set (U64 × Fl4SpecA64.Word)) (stack : U64 → List U64) + (κ : U64 → U64) (nA : U64) (m : McState) (Clock : A → MachineState → Prop) (s : S) (a : A) (σ : State Unit) : + Prop where + J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar nA)) s a σ m + hvp : ∀ b, VsR (L.ksb s) (Pp ∪ Tb) b → Aligned b ∧ ¬ PPW st s b + P : NowAgreeA ar Clock + hClock : Clock a σ.machineState + hnd : numDomains = 1 + C : CounterStepA σ + /-- [P] (ruling (c)) The raw counter oracle is monotone. -/ + hC : ∀ i, fl4RawCounterOracle i ≤ fl4RawCounterOracle (i + 1) + hNow : fl4ReadRawCounter σ.machineState = getCurrentTime σ.machineState + hAr : sched.update_timestamp st ar s a ⦃ r => AR' r.2 Z.hw ⦄ + hmb : ∀ a', Fl4MinBudgetAgreeA ar a' + +/-- **The record at a fault entry's body end**: `FaultRelA` and `Sched.BodyOKA`. -/ +def FaultEndA (st : machine.Store S) [L : LawfulStore st] (ar : machine.Arch A) (AR : A → HwRootA → Prop) + (Z : XSetsA) (H : Heap) (OS U Ac Pp Tb : Set U64) (Lv : U64 → Nat) (ia ie : Nat) (N E Rp : Set U64) + (Bf : Set (U64 × Fl4SpecA64.Word)) (stack : U64 → List U64) (κ : U64 → U64) (nA : U64) (m : McState) + (s : S) (a : A) (σ : State Unit) : Prop := + FaultRelA st ar AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ nA m s a σ ∧ Sched.BodyOKA σ m + +/-- The budget prefix at a fault entry, with the relation `FaultEndA`. -/ +private theorem faultBudgetA (hPl : ArchPlat ar) + (I : KEntryFaultInA st ar AR AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ hPl.ipa m Clock s a σ) + (hCharge : KEntryChargeA st ar AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ hPl.ipa m Clock s a σ + (sched.check_budget_restart st ar) fl4CheckBudgetRestart) : + syscall.budget_ok st ar s a ⦃ r => ∃ τ, fl4BudgetOk (Z := Unit) σ = ({(r.1, τ)}, False) ∧ + FaultEndA st ar AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ hPl.ipa m r.2.1 r.2.2 τ ⦄ := + WP.spec_mono (I.J.budgetOkSysA I.hvp I.P I.hClock I.hnd I.C I.hC I.hNow I.hAr I.hmb + (fun s1 a1 σ1 e J1 En V CI R Act Cl suf => hCharge s1 a1 σ1 e J1 En V CI R Act Cl suf)) fun r h => by + obtain ⟨τ, e, Q, hs⟩ := h + refine ⟨τ, e, ⟨stack, Q⟩, ?_⟩ + obtain ⟨b, s1, a1⟩ := r + cases b + · exact I.J.bodyOKA_budgetFalse I.hvp I.P I.hClock I.hnd I.C I.hNow I.hAr I.hmb e + · exact (hs rfl).1.bodyOKA + +/-- **User exception entry at the record, with `handle_fault` wired** (module docstring). -/ +theorem userException_recFA (hPl : ArchPlat ar) (Env : FaultEnvA st ar Z) (number code : U64) (limit : Nat) + (view : State Unit → Fl4TimerView) + (I : KEntryFaultInA st ar AR AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ hPl.ipa m Clock s a σ) + (hCharge : KEntryChargeA st ar AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ hPl.ipa m Clock s a σ + (sched.check_budget_restart st ar) fl4CheckBudgetRestart) + (hcase : ∀ s' a' τ, JSyscallA st AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes + (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s' a' τ m → + FaultNullCaseA st s' τ ∨ FaultRecvCaseA st E s' τ ∨ FaultDonateCaseA st E s' τ ∨ FaultNoDonCaseA st E s' τ ∨ + FaultBlockCaseA st E s' τ ∨ FaultDeliverA st ar AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ hPl.ipa m (.UserException number code) + (.userException number.bv code.bv) s' a' τ) + (finish : ∀ s' a' τ, ((), τ) ∈ (fl4BudgetFault (Z := Unit) (.userException number.bv code.bv) σ).1 → + FaultRelA st ar AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ hPl.ipa m s' a' τ → + Sched.BodyOKA τ m → + syscall.finish st ar s' a' ⦃ r => StepPostA Final (fl4Finish limit view) τ r ⦄) : + syscall.handle_user_exception st ar s a number code ⦃ r => + StepPostA Final (fl4BudgetFault (.userException number.bv code.bv) >>= fun _ => fl4Finish limit view) σ r ⦄ := + userExceptionEntry_refinesA number code limit view (faultBudgetA hPl I hCharge) + (fun s' a' τ eq bspec _ => by + have JS := I.J.budgetOkTrueA I.hvp I.P I.hClock I.hnd I.C I.hC I.hNow I.hAr I.hmb + (fun s1 a1 σ1 e J1 En V CI R Act Cl suf => hCharge s1 a1 σ1 e J1 En V CI R Act Cl suf) eq bspec |>.1 + exact WP.spec_mono (JS.deliverFaultA hPl Env (usrFault_userA (L.curW s') number code) (fun _ _ => trivial) (hcase s' a' τ JS)) + fun _ ⟨υ, mem, h, B⟩ => ⟨υ, mem, h, B.1⟩) + (fun s' a' τ mem h => finish s' a' τ mem h.1 h.2) + +/-- **VM fault entry at the record, with `handle_fault` wired.** -/ +theorem vmFault_recFA (hPl : ArchPlat ar) (Env : FaultEnvA st ar Z) (addr esr : U64) (limit : Nat) + (view : State Unit → Fl4TimerView) + (I : KEntryFaultInA st ar AR AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ hPl.ipa m Clock s a σ) + (hCharge : KEntryChargeA st ar AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ hPl.ipa m Clock s a σ + (sched.check_budget_restart st ar) fl4CheckBudgetRestart) + (hcase : ∀ s' a' τ, JSyscallA st AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes + (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s' a' τ m → + FaultNullCaseA st s' τ ∨ FaultRecvCaseA st E s' τ ∨ FaultDonateCaseA st E s' τ ∨ FaultNoDonCaseA st E s' τ ∨ + FaultBlockCaseA st E s' τ ∨ FaultDeliverA st ar AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ hPl.ipa m + (.VmFault addr esr (decide (((esr.bv >>> 26) &&& 63) = 32))) (fl4VmFault addr.bv esr.bv) s' a' τ) + (finish : ∀ s' a' τ, ((), τ) ∈ (fl4BudgetFault (Z := Unit) (fl4VmFault addr.bv esr.bv) σ).1 → + FaultRelA st ar AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ hPl.ipa m s' a' τ → + Sched.BodyOKA τ m → + syscall.finish st ar s' a' ⦃ r => StepPostA Final (fl4Finish limit view) τ r ⦄) : + syscall.handle_vm_fault st ar s a addr esr ⦃ r => + StepPostA Final (fl4BudgetFault (fl4VmFault addr.bv esr.bv) >>= fun _ => fl4Finish limit view) σ r ⦄ := + vmFaultEntry_refinesA addr esr limit view (faultBudgetA hPl I hCharge) + (fun s' a' τ eq bspec _ => by + have JS := I.J.budgetOkTrueA I.hvp I.P I.hClock I.hnd I.C I.hC I.hNow I.hAr I.hmb + (fun s1 a1 σ1 e J1 En V CI R Act Cl suf => hCharge s1 a1 σ1 e J1 En V CI R Act Cl suf) eq bspec |>.1 + exact WP.spec_mono (JS.deliverFaultA hPl Env (usrFault_vmA (L.curW s') addr esr) (fun _ _ => trivial) (hcase s' a' τ JS)) + fun _ ⟨υ, mem, h, B⟩ => ⟨υ, mem, h, B.1⟩) + (fun s' a' τ mem h => finish s' a' τ mem h.1 h.2) + +/-- **Unknown syscall entry at the record, with `handle_fault` wired.** `hTrap` [J]: the faulting thread's +`TRAP_SYSCALLS` flag is clear at the post-prefix state (the receiver's buffer, if any, is then irrelevant to +`FaultOKA`). -/ +theorem unknownSyscall_recFA (hPl : ArchPlat ar) (Env : FaultEnvA st ar Z) (number : U64) (limit : Nat) + (view : State Unit → Fl4TimerView) + (I : KEntryFaultInA st ar AR AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ hPl.ipa m Clock s a σ) + (hCharge : KEntryChargeA st ar AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ hPl.ipa m Clock s a σ + (sched.check_budget_restart st ar) fl4CheckBudgetRestart) + (hTrap : ∀ s' a' τ, JSyscallA st AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes + (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s' a' τ m → + (L.mem s' (tTflA (L.curW s')) &&& fl4_abi.consts.tcb_flag.TRAP_SYSCALLS) = 0#u64) + (hcase : ∀ s' a' τ, JSyscallA st AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes + (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s' a' τ m → + FaultNullCaseA st s' τ ∨ FaultRecvCaseA st E s' τ ∨ FaultDonateCaseA st E s' τ ∨ FaultNoDonCaseA st E s' τ ∨ + FaultBlockCaseA st E s' τ ∨ FaultDeliverA st ar AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ hPl.ipa m (.UnknownSyscall number) + (.unknownSyscallException number.bv) s' a' τ) + (finish : ∀ s' a' τ, ((), τ) ∈ (fl4BudgetFault (Z := Unit) (.unknownSyscallException number.bv) σ).1 → + FaultRelA st ar AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ hPl.ipa m s' a' τ → + Sched.BodyOKA τ m → + syscall.finish st ar s' a' ⦃ r => StepPostA Final (fl4Finish limit view) τ r ⦄) : + syscall.unknown_syscall_fault st ar s a number false ⦃ r => r.1 = false ∧ + StepPostA Final (fl4BudgetFault (.unknownSyscallException number.bv) >>= fun _ => fl4Finish limit view) σ r.2 ⦄ := + unknownSyscallFault_refinesA number limit view (faultBudgetA hPl I hCharge) + (fun s' a' τ eq bspec _ => by + have JS := I.J.budgetOkTrueA I.hvp I.P I.hClock I.hnd I.C I.hC I.hNow I.hAr I.hmb + (fun s1 a1 σ1 e J1 En V CI R Act Cl suf => hCharge s1 a1 σ1 e J1 En V CI R Act Cl suf) eq bspec |>.1 + refine WP.spec_mono (JS.deliverFaultA hPl Env (usrFault_unkA (L.curW s') number) (fun rbo x => Or.inr ?_) + (hcase s' a' τ JS)) fun _ ⟨υ, mem, h, B⟩ => ⟨υ, mem, h, B.1⟩ + rw [faultWords_tflA JS.sr.core.tcbs JS.sr.curT] + exact hTrap s' a' τ JS) + (fun s' a' τ mem h => finish s' a' τ mem h.1 h.2) + +end entries + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3KEntryFaultFinA.lean b/proofs/Fl4ProofsA64/P3/Stage3KEntryFaultFinA.lean new file mode 100644 index 000000000..09a3cc4d3 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3KEntryFaultFinA.lean @@ -0,0 +1,181 @@ +/- +P3 (aarch64, A64-P3 dispatch restore step 5): **the producer of the fault entries' `finish`**. + +* `FinishInA`: the premises of `JStage3A.finishEA` past the record and `BodyOKA` (integration exec 8: `hSched`, + `hAct` and `hExit` of the old `finishA` route are discharged by `finishEA`; `hSig` by `JStage3A.fireSigA`); + `FinPlatA`: its platform facts. +* `userException_recFinA`, `vmFault_recFinA`, `unknownSyscall_recFinA`: the three entries with `finish` discharged. +* `kernelEntry_recA`: the unified top (`kernelEntry_stage3A`) with its `hfin` discharged for every event whose body + ends in the record with `BodyOKA`. + +Named premises: those of the `*_recFA` entries without `finish`; `FinishInA` at the body's end record: [J] `hGap` (`SchedGapA`: [P] `ticks`, `timer`, `minAgree`, [S] `oneDomain`), [S4] +`hNative`; [P] `ArchSwFA`, `FinPlatA` ([S] `h1`, [P] `arK`, `toUs`, `hmax`, `htu`). New module. +-/ +import Fl4ProofsA64.P3.Stage3KEntryFaultA +import Fl4ProofsA64.P3.Stage3KEntryFinishA +import Fl4ProofsA64.P3.Stage3KEntryActRecA +import Fl4ProofsA64.P3.Stage3KEntryFireSigA +import Fl4ProofsA64.P3.Stage3KEntryTopA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +/-! ## `finish` at a fault entry's body end -/ + +section finish +variable {S : Type} (st : machine.Store S) [L : LawfulStore st] {A : Type} (ar : machine.Arch A) + +/-- [P] / [S] **The platform facts `finish` uses** (module docstring). -/ +structure FinPlatA (ar : machine.Arch A) (AR : A → HwRootA → Prop) : Prop where + h1 : fl4A64Platform.numNodes = 1 + arK : ArRootKeepA ar AR + toUs : ∀ a', Fl4TicksToUsAgreeA ar a' + hmax : maxTicksToUs = core.num.U64.MAX.bv + htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄ + +/-- **The premises of `finish` past the record and `BodyOKA`** (module docstring): [J] `hGap` (`SchedGapA`), [S4] `hNative`. -/ +structure FinishInA (AR : A → HwRootA → Prop) (Z : XSetsA) (H : Heap) (OS U Ac Pp Tb : Set U64) + (Lv : U64 → Nat) (ia ie : Nat) (N E Rp : Set U64) (Bf : Set (U64 × Fl4SpecA64.Word)) + (stack : U64 → List U64) (κ : U64 → U64) (m : McState) (s : S) (a : A) (σ : State Unit) + (tm : A → Nat → DeadlineViewA) (view : State Unit → Fl4TimerView) (hPl : ArchPlat ar) : Prop where + hGap : SchedGapA (st := st) Z m κ s a σ ar tm view + hNative : ∀ τ1 τ2, ((), τ1) ∈ (fl4Schedule kLimitA view σ).1 → + ((), τ2) ∈ (fl4ActivateThread (Z := Unit) τ1).1 → + ∀ y, τ2.kheap τ2.curThread = some (.tcb y) → y.tcbState ≠ .ext .runningVM + +variable {st ar} +variable {AR AR' : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {s : S} {a : A} {σ : State Unit} {m : McState} {Clock : A → MachineState → Prop} + +variable (st ar) in +/-- The [J] inputs of `finish` at every record after the body: `FinishInA` at the record's own ghost sets. -/ +def FinishAtA (AR : A → HwRootA → Prop) (Z : XSetsA) (H : Heap) (OS U Ac Pp Tb : Set U64) + (Lv : U64 → Nat) (ia ie : Nat) (N E Rp : Set U64) (Bf : Set (U64 × Fl4SpecA64.Word)) (κ : U64 → U64) + (m : McState) (tm : A → Nat → DeadlineViewA) (view : State Unit → Fl4TimerView) (hPl : ArchPlat ar) + (s : S) (a : A) (σ : State Unit) : Prop := + ∀ (stack : U64 → List U64) (Q : U64 → U64 → List U64) (Rl : U64 → List U64) + (curOf curScOf : Fl4SpecA64.CoreId → ObjRef), + JStage3A st AR { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } H OS U Ac Pp Tb Lv ia ie N E Rp Bf + stack κ fl4A64Platform.numNodes (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m → + FinishInA st ar AR { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } H OS U Ac Pp Tb Lv ia ie N E Rp + Bf stack κ m s a σ tm view hPl + +set_option maxHeartbeats 2000000 in +/-- **`syscall.finish` after a fault delivery**, from `FaultRelA` and `BodyOKA` (`JStage3A.finishEA`). -/ +theorem FaultRelA.finishA {tm : A → Nat → DeadlineViewA} {view : State Unit → Fl4TimerView} + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) (arch : ArchSwFA st ar) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (P : FinPlatA ar AR) + (R : FaultRelA st ar AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ hPl.ipa m s a σ) (B : BodyOKA σ m) + (F : FinishAtA st ar AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf κ m tm view hPl s a σ) : + syscall.finish st ar s a ⦃ r => StepPostA (KExitA st ar AR hPl.ipa m) (fl4Finish kLimitA view) σ r ⦄ := by + obtain ⟨stack', Q, Rl, curOf, curScOf, J⟩ := R + have I := F stack' Q Rl curOf curScOf J + have hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄ := fun a => + WP.spec_mono (I.hGap.ticks a).minBudget fun _ h => h.trans I.hGap.minAgree + exact JStage3A.finishEA (Z := { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf }) hA hPl arch hCs P.h1 J + B.sw B.toBodyExitA (JStage3A.fireSigA hA hPl hCs P.h1 hmb P.hmax P.htu) I.hGap P.arK P.toUs I.hNative + +end finish + +/-! ## The entries with `finish` discharged -/ + +section entries +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {ar : machine.Arch A} +variable {AR AR' : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {s : S} {a : A} {σ : State Unit} {m : McState} {Clock : A → MachineState → Prop} + {tm : A → Nat → DeadlineViewA} + +/-- **`userException_recFA` with `finish` discharged** (module docstring). -/ +theorem userException_recFinA (hPl : ArchPlat ar) (Env : FaultEnvA st ar Z) (number code : U64) (view : State Unit → Fl4TimerView) + (I : KEntryFaultInA st ar AR AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ hPl.ipa m Clock s a σ) + (hCharge : KEntryChargeA st ar AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ hPl.ipa m Clock s a σ + (sched.check_budget_restart st ar) fl4CheckBudgetRestart) + (hcase : ∀ s' a' τ, JSyscallA st AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes + (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s' a' τ m → + FaultNullCaseA st s' τ ∨ FaultRecvCaseA st E s' τ ∨ FaultDonateCaseA st E s' τ ∨ FaultNoDonCaseA st E s' τ ∨ + FaultBlockCaseA st E s' τ ∨ FaultDeliverA st ar AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ hPl.ipa m (.UserException number code) + (.userException number.bv code.bv) s' a' τ) + (arch : ArchSwFA st ar) (P : FinPlatA ar AR') + (hFin : ∀ s' a' τ, ((), τ) ∈ (fl4BudgetFault (Z := Unit) (.userException number.bv code.bv) σ).1 → SwA τ → + FinishAtA st ar AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf κ m tm view hPl s' a' τ) : + syscall.handle_user_exception st ar s a number code ⦃ r => + StepPostA (KExitA st ar AR' hPl.ipa m) (fl4BudgetFault (.userException number.bv code.bv) >>= fun _ => fl4Finish kLimitA view) σ r ⦄ := + userException_recFA hPl Env number code kLimitA view I hCharge hcase fun s' a' τ mem R B => + FaultRelA.finishA Env.hA hPl arch Env.hCs P R B (hFin s' a' τ mem B.sw) + +/-- **`vmFault_recFA` with `finish` discharged** (module docstring). -/ +theorem vmFault_recFinA (hPl : ArchPlat ar) (Env : FaultEnvA st ar Z) (addr esr : U64) (view : State Unit → Fl4TimerView) + (I : KEntryFaultInA st ar AR AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ hPl.ipa m Clock s a σ) + (hCharge : KEntryChargeA st ar AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ hPl.ipa m Clock s a σ + (sched.check_budget_restart st ar) fl4CheckBudgetRestart) + (hcase : ∀ s' a' τ, JSyscallA st AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes + (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s' a' τ m → + FaultNullCaseA st s' τ ∨ FaultRecvCaseA st E s' τ ∨ FaultDonateCaseA st E s' τ ∨ FaultNoDonCaseA st E s' τ ∨ + FaultBlockCaseA st E s' τ ∨ FaultDeliverA st ar AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ hPl.ipa m + (.VmFault addr esr (decide (((esr.bv >>> 26) &&& 63) = 32))) (fl4VmFault addr.bv esr.bv) s' a' τ) + (arch : ArchSwFA st ar) (P : FinPlatA ar AR') + (hFin : ∀ s' a' τ, ((), τ) ∈ (fl4BudgetFault (Z := Unit) (fl4VmFault addr.bv esr.bv) σ).1 → SwA τ → + FinishAtA st ar AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf κ m tm view hPl s' a' τ) : + syscall.handle_vm_fault st ar s a addr esr ⦃ r => + StepPostA (KExitA st ar AR' hPl.ipa m) (fl4BudgetFault (fl4VmFault addr.bv esr.bv) >>= fun _ => fl4Finish kLimitA view) σ r ⦄ := + vmFault_recFA hPl Env addr esr kLimitA view I hCharge hcase fun s' a' τ mem R B => + FaultRelA.finishA Env.hA hPl arch Env.hCs P R B (hFin s' a' τ mem B.sw) + +/-- **`unknownSyscall_recFA` with `finish` discharged** (module docstring). -/ +theorem unknownSyscall_recFinA (hPl : ArchPlat ar) (Env : FaultEnvA st ar Z) (number : U64) (view : State Unit → Fl4TimerView) + (I : KEntryFaultInA st ar AR AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ hPl.ipa m Clock s a σ) + (hCharge : KEntryChargeA st ar AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ hPl.ipa m Clock s a σ + (sched.check_budget_restart st ar) fl4CheckBudgetRestart) + (hTrap : ∀ s' a' τ, JSyscallA st AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes + (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s' a' τ m → + (L.mem s' (tTflA (L.curW s')) &&& fl4_abi.consts.tcb_flag.TRAP_SYSCALLS) = 0#u64) + (hcase : ∀ s' a' τ, JSyscallA st AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes + (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s' a' τ m → + FaultNullCaseA st s' τ ∨ FaultRecvCaseA st E s' τ ∨ FaultDonateCaseA st E s' τ ∨ FaultNoDonCaseA st E s' τ ∨ + FaultBlockCaseA st E s' τ ∨ FaultDeliverA st ar AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ hPl.ipa m (.UnknownSyscall number) + (.unknownSyscallException number.bv) s' a' τ) + (arch : ArchSwFA st ar) (P : FinPlatA ar AR') + (hFin : ∀ s' a' τ, ((), τ) ∈ (fl4BudgetFault (Z := Unit) (.unknownSyscallException number.bv) σ).1 → SwA τ → + FinishAtA st ar AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf κ m tm view hPl s' a' τ) : + syscall.unknown_syscall_fault st ar s a number false ⦃ r => r.1 = false ∧ + StepPostA (KExitA st ar AR' hPl.ipa m) (fl4BudgetFault (.unknownSyscallException number.bv) >>= fun _ => fl4Finish kLimitA view) σ r.2 ⦄ := + unknownSyscall_recFA hPl Env number kLimitA view I hCharge hTrap hcase fun s' a' τ mem R B => + FaultRelA.finishA Env.hA hPl arch Env.hCs P R B (hFin s' a' τ mem B.sw) + +end entries + +/-! ## The unified kernel-entry top with `finish` discharged -/ + +section top +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {m : McState} {tm : A → Nat → DeadlineViewA} + +/-- **`kernelEntry_stage3A` with `hfin` discharged** (module docstring): an event whose body ends in the record +(`FaultRelA`) with `BodyOKA` reaches the next entry's record, given `FinishAtA` at the body's end. -/ +theorem kernelEntry_recA (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) (arch : ArchSwFA st ar) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (P : FinPlatA ar AR) {view : State Unit → Fl4TimerView} + (s : S) (a : A) (σ : State Unit) (e : KEventA) (run : kRuns st s e) + (hbody : kBodyA st ar s a e ⦃ r => StepPostA (fun s' a' τ => + FaultRelA st ar AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ hPl.ipa m s' a' τ ∧ BodyOKA τ m) + (kOpA e) σ r ⦄) + (hFin : ∀ s' a' τ, ((), τ) ∈ (kOpA e σ).1 → + FinishAtA st ar AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf κ m tm view hPl s' a' τ) : + kCodeA st ar s a e ⦃ out => + StepPostA (KExitA st ar AR hPl.ipa m) (kOpA e >>= fun _ => fl4Finish kLimitA view) σ out ⦄ := + kernelEntry_stage3A s a σ e run hbody fun r τ _ op RB => + FaultRelA.finishA hA hPl arch hCs P RB.1 RB.2 (hFin r.1 r.2 τ op) + +end top + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3KEntryFinishA.lean b/proofs/Fl4ProofsA64/P3/Stage3KEntryFinishA.lean new file mode 100644 index 000000000..4041a6d47 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3KEntryFinishA.lean @@ -0,0 +1,200 @@ +/- +P3 (aarch64, A64-P3 Stage 3, kernel-entry composition, step 3): **`syscall.finish` at the record**. + +Every kernel entry is a body followed by `syscall.finish` (`P3/Stage3KEntryTopA`); this is the one `finish` +theorem all of them use. From the record `JStage3A` and `SwA` at the body's state, FL4's `finish` returns, the spec +`fl4Finish` has the outcome, and the next entry's record holds (`KExitA`): `JStage3EntryA`, and `JSyscallA` when the +current thread is not the idle thread. The timer pass is `JStage3A.fire_recA`; the syscall record is +`JSyscallA.ofSchedExitAt` at the actual post-fire state. + +Named premises (owners in `handoff/a64p3-kentry.md`): +* `hSig` [J] (Lift 2a): the record-level `send_signal` on a bound notification (`fire_recA`'s, verbatim). +* `hSched` [J]: the scheduler's selection and tail inputs (`ScheduleInputsA`), given its `fire` field, which is proved + here. +* `hAct` [J]: `ScheduleFinishInputsA` (activation, VCPU check) at the actual scheduler successor. +* `hExit` [J]: the record after the schedule tail and the activation, from the record at the actual post-fire state. +* `hNative` [S]: the activated thread is not `TS_RUNNING_VM` (guest resume is S4's). +* [P] `ArchTotal`, `ArchPlat`, `ArchSwFA`, `hCs`. + +New module. +-/ +import Fl4ProofsA64.P3.Stage3FireRecordA +import Fl4ProofsA64.P3.Stage3SchedSchedA +import Fl4ProofsA64.P3.Stage3ExitAtA +import Fl4ProofsA64.P3.OuterEntryA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3 Fl4ProofsA64.P3.OuterEntry Classical + +noncomputable section + +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +/-- The local scheduler's table bound, as a `Nat`. -/ +abbrev kLimitA : Nat := layout.NUM_DOMAIN_SCHEDULES.val + +/-- **The scheduler with its post-fire state**: `schedule_stepA`'s sibling that keeps the tail run from the same +post-fire state `σ1` which the timer premise names. -/ +theorem schedule_tailMemA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {σ : State Unit} {a : A} {tm : A → Nat → DeadlineViewA} + {AR : A → HwRootA → Prop} (P : ArchSwFA st ar) (view : State Unit → Fl4TimerView) + (inputs : ScheduleInputsA (st := st) Z m κ s a σ ar tm view AR) + {FF : S → S → State Unit → State Unit → Prop} + (fireF : timer.fire st ar s a ⦃ r => ∃ σ1 Q R, + fl4TimerFires σ σ1 ∧ YieldQueueRelA (st := st) {Z with Q := Q, R := R} m κ r.1 σ1 ∧ IdleThreadsA m σ1 ∧ + FF s r.1 σ σ1 ⦄) : + sched.schedule st ar s a ⦃ r => ∃ s1 σ1 τ Q R Tcur Ccur, + FF s s1 σ σ1 ∧ fl4TimerFires σ σ1 ∧ ((), τ) ∈ (SchedTail kLimitA view σ1).1 ∧ + YieldQueueRelA (st := st) {Z with Q := Q, R := R, curOf := Tcur, curScOf := Ccur} m κ r.1 τ ⦄ := by + rw [schedule_phasesA] + refine WP.spec_bind (reachedResultA (awaken_stepA fireF)) ?_ + rintro ⟨sA, aA⟩ ⟨wake, sF, σ1, σ2, Q, R, FFh, fires, waking, wakeRel, _, _⟩ + simp only [uncurry_apply_pair] + have steps := inputs.selection (sA, aA) σ1 σ2 Q R wake fires waking wakeRel + refine WP.spec_bind (reachedResultA (select_action_refinesA wakeRel P steps.action steps.domain steps.roots)) ?_ + rintro ⟨s2, a2⟩ ⟨selected, σ3, Q', _, selection, _, H2, frame, _⟩ + simp only [uncurry_apply_pair] + have ready := inputs.tail (sA, aA) (s2, a2) σ1 σ2 σ3 Q R Q' wake selected fires waking selection frame H2 + refine WP.spec_mono (schedule_tail_refinesA H2 view inputs.oneDomain inputs.minAgree ready) ?_ + rintro r ⟨τ, C, tail, H3, _⟩ + refine ⟨sF, σ1, τ, Q', R, _, C, FFh, fires, ?_, H3⟩ + unfold SchedTail + rw [nd_bind_oneSc waking, nd_bind_oneSc (checkDomainTime_single_specA σ2 inputs.oneDomain)] + unfold fl4SelectThread + rw [nd_bind_assocSc, nd_bind_oneSc selection] + exact tail + +/-- The VCPU check of a thread that is not running a guest changes nothing. -/ +theorem fl4VcpuCheck_native (t : ObjRef) (τ : State Unit) + (h : ∀ y, τ.kheap t = some (.tcb y) → y.tcbState ≠ .ext .runningVM) : + HT (fun σ => σ = τ) (fl4VcpuCheck (Z := Unit) t) (fun _ σ => σ = τ) := by + unfold Fl4SpecA64.fl4VcpuCheck Fl4SpecA64.getThreadState + refine HT.bind (Q := fun v σ => σ = τ ∧ v ≠ .ext .runningVM) + (HT.threadGet _ t fun σ hσ x hx => ⟨hσ, h x (hσ ▸ hx)⟩) fun v => ?_ + exact HT.when (fun hv σ hσ => absurd hv hσ.2) (fun _ σ hσ => hσ.1) + +section +variable (st) (ar) + +/-- **The next entry's record**: `JStage3EntryA` at some ghost sets, and `JSyscallA` when the current thread is not +the idle thread. -/ +def KExitA (AR : A → HwRootA → Prop) (nA : U64) (m : McState) (s : S) (a : A) (σ : State Unit) : Prop := + ∃ (Z : XSetsA) (H : Heap) (OS U Ac Pp Tb : Set U64) (Lv : U64 → Nat) (ia ie : Nat) (N E Rp : Set U64) + (Bf : Set (U64 × Fl4SpecA64.Word)) (stack : U64 → List U64) (κ : U64 → U64), + JStage3EntryA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar nA)) s a σ m ∧ + (σ.curThread ≠ σ.idleThread → + JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar nA)) s a σ m) + +end + +/-- **`syscall.finish` at the record** (module docstring). -/ +theorem JStage3A.finishA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {s : S} {a : A} {σ : State Unit} {m : McState} + {tm : A → Nat → DeadlineViewA} {view : State Unit → Fl4TimerView} + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) (arch : ArchSwFA st ar) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hSw : SwA σ) + (hSig : ∀ (Q : U64 → U64 → List U64) (Rl : U64 → List U64) (s : S) (a : A) (σ : State Unit) (t badge : U64), + JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes + (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m → + SwA σ → t ∈ Z.T → L.mem s (boundA t) ≠ 0#u64 → + notification.send_signal st ar s a (L.mem s (boundA t)) badge ⦃ r => + ∃ σ' b Q' Rl', sendSignal (Z := Unit) (specPtr (L.mem s (boundA t))) b σ = ({((), σ')}, False) ∧ + JStage3A st AR { Z with Q := Q', R := Rl' } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ + fl4A64Platform.numNodes (J5A st) (J7A st (J7R2A st ar hPl.ipa)) r.1 r.2 σ' m ⦄) + (hSched : timer.fire st ar s a ⦃ r => ∃ σ1 Q R, + fl4TimerFires σ σ1 ∧ YieldQueueRelA (st := st) {Z with Q := Q, R := R} m κ r.1 σ1 ∧ IdleThreadsA m σ1 ⦄ → + ScheduleInputsA (st := st) Z m κ s a σ ar tm view AR) + (hAct : ∀ (r : S × A) τ Q R Tcur Ccur, + sched.schedule st ar s a = ok r → ((), τ) ∈ (fl4Schedule kLimitA view σ).1 → + YieldQueueRelA (st := st) {Z with Q := Q, R := R, curOf := Tcur, curScOf := Ccur} m κ r.1 τ → + ScheduleFinishInputsA (st := st) {Z with Q := Q, R := R, curOf := Tcur, curScOf := Ccur} m κ r.1 r.2 τ ar) + (hNative : ∀ τ1 τ2, ((), τ1) ∈ (fl4Schedule kLimitA view σ).1 → + ((), τ2) ∈ (fl4ActivateThread (Z := Unit) τ1).1 → + ∀ y, τ2.kheap τ2.curThread = some (.tcb y) → y.tcbState ≠ .ext .runningVM) + (hExit : ∀ (f out : S × A) σ1 τ1 τ2 Q Rl, + timer.fire st ar s a = ok f → syscall.finish st ar s a = ok out → + JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes + (J5A st) (J7A st (J7R2A st ar hPl.ipa)) f.1 f.2 σ1 m → + fl4TimerFires σ σ1 → ((), τ1) ∈ (SchedTail kLimitA view σ1).1 → + ((), τ2) ∈ (fl4ActivateThread (Z := Unit) τ1).1 → + ∃ (Z' : XSetsA) (H' : Heap) (OS' U' Ac' Pp' Tb' : Set U64) (Lv' : U64 → Nat) (ia' ie' : Nat) + (N' E' Rp' : Set U64) (Bf' : Set (U64 × Fl4SpecA64.Word)) (stack' : U64 → List U64) (κ' : U64 → U64), + JStage3EntryA st AR Z' H' OS' U' Ac' Pp' Tb' Lv' ia' ie' N' E' Rp' Bf' stack' κ' fl4A64Platform.numNodes + (J5A st) (J7A st (J7R2A st ar hPl.ipa)) out.1 out.2 τ2 m ∧ + (τ2.curThread ≠ τ2.idleThread → + ∀ y, τ1.kheap τ1.curThread = some (.tcb y) → y.tcbState ≠ .idleThreadState)) : + syscall.finish st ar s a ⦃ r => + StepPostA (KExitA st ar AR hPl.ipa m) (fl4Finish kLimitA view) σ r ⦄ := by + have hcore : m.curCore < fl4A64Platform.numNodes := by + rw [J.sr.sch.core]; exact hCs _ J.sr.sch.cpuCs + have F := JStage3A.fire_recA hA hPl J hcore hSw hSig + have F0 : timer.fire st ar s a ⦃ r => ∃ σ1 Q R, + fl4TimerFires σ σ1 ∧ YieldQueueRelA (st := st) {Z with Q := Q, R := R} m κ r.1 σ1 ∧ IdleThreadsA m σ1 ∧ + (∃ f : S × A, timer.fire st ar s a = ok f ∧ + JStage3A st AR { Z with Q := Q, R := R } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ + fl4A64Platform.numNodes (J5A st) (J7A st (J7R2A st ar hPl.ipa)) f.1 f.2 σ1 m ∧ FireRecA σ1) ⦄ := by + refine WP.spec_mono (reachedResultA F) ?_ + rintro r ⟨er, σ1, Q, Rl, hf, J1, R1⟩ + refine ⟨σ1, Q, Rl, hf, J1.yieldQueueA, fun c hc x hx => ?_, r, er, J1, R1⟩ + obtain ⟨y, hy, hs⟩ := J1.sidle.f3 c hc + rw [hy] at hx + have := KernelObject.tcb.inj (Option.some.inj hx) + subst this + rw [hs]; rfl + have inputs := hSched (WP.spec_mono F0 fun _ ⟨σ1, Q, R, h1, h2, h3, _⟩ => ⟨σ1, Q, R, h1, h2, h3⟩) + have T := schedule_tailMemA arch view inputs + (FF := fun _ _ _ σ1 => ∃ (f : S × A) (Q : U64 → U64 → List U64) (R : U64 → List U64), + timer.fire st ar s a = ok f ∧ + JStage3A st AR { Z with Q := Q, R := R } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ + fl4A64Platform.numNodes (J5A st) (J7A st (J7R2A st ar hPl.ipa)) f.1 f.2 σ1 m ∧ FireRecA σ1) + (WP.spec_mono F0 fun _ ⟨σ1, Q, R, h1, h2, h3, f, ef, J1, R1⟩ => ⟨σ1, Q, R, h1, h2, h3, f, Q, R, ef, J1, R1⟩) + have W : syscall.finish st ar s a ⦃ r => ∃ σ1 τ1 τ2 f Q Rl, + timer.fire st ar s a = ok f ∧ + JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ + fl4A64Platform.numNodes (J5A st) (J7A st (J7R2A st ar hPl.ipa)) f.1 f.2 σ1 m ∧ FireRecA σ1 ∧ + fl4TimerFires σ σ1 ∧ ((), τ1) ∈ (SchedTail kLimitA view σ1).1 ∧ + ((), τ2) ∈ (fl4ActivateThread (Z := Unit) τ1).1 ∧ + ((), τ2) ∈ (fl4Finish kLimitA view σ).1 ∧ + (∀ y, τ1.kheap τ1.curThread = some (.tcb y) → ActivateStateOKA y.tcbState) ⦄ := by + unfold syscall.finish + refine WP.spec_bind (reachedResultA T) ?_ + rintro ⟨s1, a1⟩ ⟨scheduled, _, σ1, τ1, Q, R, Tcur, Ccur, ⟨f, Qf, Rf, ef, J1, R1⟩, hf, tail, H3⟩ + simp only [uncurry_apply_pair] + have step := fl4Schedule_of_tail kLimitA view hf tail + obtain ⟨nonnull, ticks, ⟨x, htcb, legal, pc⟩, guest⟩ := hAct (s1, a1) τ1 Q R Tcur Ccur scheduled step H3 + refine WP.spec_mono (finish_after_schedule_refinesA H3 a1 nonnull ticks htcb legal pc guest) ?_ + rintro r ⟨ρ, abs, _⟩ + have mem : ((), ρ) ∈ (fl4FinishAfterSchedule (Z := Unit) τ1).1 := abs.symm ▸ Set.mem_singleton _ + have mem' := mem + unfold Fl4SpecA64.fl4FinishAfterSchedule at mem' + obtain ⟨⟨⟨⟩, τ2⟩, hact, rest⟩ := mem_bindA.1 mem' + obtain ⟨⟨ct, τg⟩, hg, check⟩ := mem_bindA.1 rest + have eg : ct = τ2.curThread ∧ τg = τ2 := by + have := Set.mem_singleton_iff.1 hg + exact ⟨(Prod.ext_iff.1 this).1, (Prod.ext_iff.1 this).2⟩ + obtain ⟨rfl, rfl⟩ := eg + have eρ : ρ = τg := + fl4VcpuCheck_native _ _ (hNative τ1 τg step hact) τg rfl _ check + subst eρ + refine ⟨σ1, τ1, ρ, f, Qf, Rf, ef, J1, R1, hf, tail, hact, mem_bindA.2 ⟨((), τ1), step, mem⟩, fun y hy => ?_⟩ + rw [htcb] at hy + have := KernelObject.tcb.inj (Option.some.inj hy) + subst this + exact legal + refine WP.spec_mono (reachedResultA W) ?_ + rintro out ⟨eout, σ1, τ1, τ2, f, Q, Rl, ef, J1, R1, hf, tail, hact, hfin, legal⟩ + obtain ⟨Z', H', OS', U', Ac', Pp', Tb', Lv', ia', ie', N', E', Rp', Bf', stack', κ', J2, hn⟩ := + hExit f out σ1 τ1 τ2 Q Rl ef eout J1 hf tail hact + refine ⟨τ2, hfin, Z', H', OS', U', Ac', Pp', Tb', Lv', ia', ie', N', E', Rp', Bf', stack', κ', J2, fun hni => ?_⟩ + exact JSyscallA.ofSchedExitAt kLimitA view hf R1 tail hact J2 J2.scNZ hni legal (hn hni) + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3KEntryFinishFA.lean b/proofs/Fl4ProofsA64/P3/Stage3KEntryFinishFA.lean new file mode 100644 index 000000000..5e3b2e8fa --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3KEntryFinishFA.lean @@ -0,0 +1,134 @@ +/- +P3 (aarch64, A64-P3 Stage 3, kernel-entry composition, Plan A, item 4, first half): **`syscall.finish` at the record +with `hSched` and `hAct` discharged**. + +`JStage3A.finishFA` is `JStage3A.finishA` with the scheduler's inputs derived (`scheduleExitFA`'s route) and the +activation inputs derived (`finishInputsA` at the record at the schedule exit; the activation is legal by +`exitFactsA`; `guest` is vacuous under `hNative`). `hExit` is also given the record and `exitFactsA`'s facts at the +schedule exit, so what is left of it is the record across `activate_thread` (discharged in +`P3/Stage3KEntryActRecA`). + +Named premises: `SchedGapA`, `BodyExitA`, `hSig` (Lift 2a), [P] `ArRootKeepA`, +`Fl4TicksToUsAgreeA`, [S4] `hNative`, [J] `hExit`, [S] `numNodes = 1`. + +New module. +-/ +import Fl4ProofsA64.P3.Stage3KEntryTailA +import Fl4ProofsA64.P3.Stage3KEntryActA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3 + Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.OuterEntry Classical + +noncomputable section + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +set_option maxHeartbeats 1000000 in +/-- **`syscall.finish` at the record** with `hSched` and `hAct` discharged (module docstring). -/ +theorem JStage3A.finishFA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {s : S} {a : A} {σ : State Unit} {m : McState} + {tm : A → Nat → DeadlineViewA} {view : State Unit → Fl4TimerView} + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) (arch : ArchSwFA st ar) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hSw : SwA σ) (B : BodyExitA σ) + (hSig : ∀ (Q : U64 → U64 → List U64) (Rl : U64 → List U64) (s : S) (a : A) (σ : State Unit) (t badge : U64), + JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes + (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m → + SwA σ → t ∈ Z.T → L.mem s (boundA t) ≠ 0#u64 → + notification.send_signal st ar s a (L.mem s (boundA t)) badge ⦃ r => + ∃ σ' b Q' Rl', sendSignal (Z := Unit) (specPtr (L.mem s (boundA t))) b σ = ({((), σ')}, False) ∧ + JStage3A st AR { Z with Q := Q', R := Rl' } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ + fl4A64Platform.numNodes (J5A st) (J7A st (J7R2A st ar hPl.ipa)) r.1 r.2 σ' m ⦄) + (G : SchedGapA (st := st) Z m κ s a σ ar tm view) (arK : ArRootKeepA ar AR) + (toUs : ∀ a', Fl4TicksToUsAgreeA ar a') + (hNative : ∀ τ1 τ2, ((), τ1) ∈ (fl4Schedule kLimitA view σ).1 → + ((), τ2) ∈ (fl4ActivateThread (Z := Unit) τ1).1 → + ∀ y, τ2.kheap τ2.curThread = some (.tcb y) → y.tcbState ≠ .ext .runningVM) + (hExit : ∀ (f out : S × A) σ1 τ1 τ2 Q Rl, + timer.fire st ar s a = ok f → syscall.finish st ar s a = ok out → + JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes + (J5A st) (J7A st (J7R2A st ar hPl.ipa)) f.1 f.2 σ1 m → + fl4TimerFires σ σ1 → ((), τ1) ∈ (SchedTail kLimitA view σ1).1 → + ((), τ2) ∈ (fl4ActivateThread (Z := Unit) τ1).1 → + (CurInvA τ1 ∧ OffOKA τ1 ∧ OffReadyA τ1 ∧ ActIdleA τ1 ∧ (τ1.curSc ≠ idleScPtr → NotIdleScA m τ1.curSc) ∧ + ValidMachineTimeA τ1 ∧ τ1.schedulerAction = .resumeCurThread ∧ + (∀ x, τ1.kheap τ1.curThread = some (.tcb x) → ActivateStateOKA x.tcbState)) → + (∃ (s1 : S) (a1 : A) (Q' : U64 → U64 → List U64) (Rl' : U64 → List U64) + (Tcur Ccur : Fl4SpecA64.CoreId → ObjRef) (hw : HwRootA), + sched.schedule st ar s a = ok (s1, a1) ∧ + JStage3A st AR { Z with Q := Q', R := Rl', curOf := Tcur, curScOf := Ccur, hw := hw } H OS U Ac Pp Tb Lv ia ie + N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s1 a1 τ1 m) → + ∃ (Z' : XSetsA) (H' : Heap) (OS' U' Ac' Pp' Tb' : Set U64) (Lv' : U64 → Nat) (ia' ie' : Nat) + (N' E' Rp' : Set U64) (Bf' : Set (U64 × Fl4SpecA64.Word)) (stack' : U64 → List U64) (κ' : U64 → U64), + JStage3EntryA st AR Z' H' OS' U' Ac' Pp' Tb' Lv' ia' ie' N' E' Rp' Bf' stack' κ' fl4A64Platform.numNodes + (J5A st) (J7A st (J7R2A st ar hPl.ipa)) out.1 out.2 τ2 m ∧ + (τ2.curThread ≠ τ2.idleThread → + ∀ y, τ1.kheap τ1.curThread = some (.tcb y) → y.tcbState ≠ .idleThreadState)) : + syscall.finish st ar s a ⦃ r => + StepPostA (KExitA st ar AR hPl.ipa m) (fl4Finish kLimitA view) σ r ⦄ := by + have hcore : m.curCore < fl4A64Platform.numNodes := by + rw [J.sr.sch.core]; exact hCs _ J.sr.sch.cpuCs + have T := JStage3A.scheduleRecFFA hA hPl arch hCs h1 J (JStage3A.fire_budgetA hA hPl J hcore hSw B hSig) G arK + have W : syscall.finish st ar s a ⦃ r => ∃ σ1 τ1 τ2 f Q Rl, + timer.fire st ar s a = ok f ∧ + JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ + fl4A64Platform.numNodes (J5A st) (J7A st (J7R2A st ar hPl.ipa)) f.1 f.2 σ1 m ∧ FireRecA σ1 ∧ + fl4TimerFires σ σ1 ∧ ((), τ1) ∈ (SchedTail kLimitA view σ1).1 ∧ + ((), τ2) ∈ (fl4ActivateThread (Z := Unit) τ1).1 ∧ + ((), τ2) ∈ (fl4Finish kLimitA view σ).1 ∧ + (CurInvA τ1 ∧ OffOKA τ1 ∧ OffReadyA τ1 ∧ ActIdleA τ1 ∧ (τ1.curSc ≠ idleScPtr → NotIdleScA m τ1.curSc) ∧ + ValidMachineTimeA τ1 ∧ τ1.schedulerAction = .resumeCurThread ∧ + (∀ x, τ1.kheap τ1.curThread = some (.tcb x) → ActivateStateOKA x.tcbState)) ∧ + (∃ (s1 : S) (a1 : A) (Q' : U64 → U64 → List U64) (Rl' : U64 → List U64) + (Tcur Ccur : Fl4SpecA64.CoreId → ObjRef) (hw : HwRootA), + sched.schedule st ar s a = ok (s1, a1) ∧ + JStage3A st AR { Z with Q := Q', R := Rl', curOf := Tcur, curScOf := Ccur, hw := hw } H OS U Ac Pp Tb Lv ia ie + N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s1 a1 τ1 m) ⦄ := by + unfold syscall.finish + refine WP.spec_bind (reachedResultA T) ?_ + rintro ⟨s1, a1⟩ ⟨scheduled, f, σ1, τ1, Qf, Rf, Q, Rl, Tcur, Ccur, hw, ef, J1, R1, B1, hf, tail, step', H3, J2⟩ + simp only [uncurry_apply_pair] + dsimp only at J2 H3 scheduled + have step := fl4Schedule_of_tail kLimitA view hf tail + have facts := exitFactsA h1 J1 J2 R1 B1 tail + have noGuest : ∀ σ2 y, fl4ActivateThread (Z := Unit) τ1 = ({((), σ2)}, False) → + σ2.kheap σ2.curThread = some (.tcb y) → y.tcbState ≠ .ext .runningVM := fun σ2 y e hy => by + have mem : ((), σ2) ∈ (fl4ActivateThread (Z := Unit) τ1).1 := by rw [e]; exact Set.mem_singleton _ + exact hNative τ1 σ2 step mem y hy + have inputs := JStage3A.finishInputsA (ar := ar) J2 (toUs a1) facts.2.2.2.2.2.2.2 + (fun _ σ2 y _ e hy run => absurd run (noGuest σ2 y e hy)) + obtain ⟨nonnull, ticks, ⟨x, htcb, legal, pc⟩, -⟩ := inputs + refine WP.spec_mono (finish_after_schedule_refinesA H3 a1 nonnull ticks htcb legal pc (fun _ σ2 y _ e hy run => absurd run (noGuest σ2 y e hy))) ?_ + rintro r ⟨ρ, abs, _⟩ + have mem : ((), ρ) ∈ (fl4FinishAfterSchedule (Z := Unit) τ1).1 := abs.symm ▸ Set.mem_singleton _ + have mem' := mem + unfold Fl4SpecA64.fl4FinishAfterSchedule at mem' + obtain ⟨⟨⟨⟩, τ2⟩, hact, rest⟩ := mem_bindA.1 mem' + obtain ⟨⟨ct, τg⟩, hg, check⟩ := mem_bindA.1 rest + have eg : ct = τ2.curThread ∧ τg = τ2 := by + have := Set.mem_singleton_iff.1 hg + exact ⟨(Prod.ext_iff.1 this).1, (Prod.ext_iff.1 this).2⟩ + obtain ⟨rfl, rfl⟩ := eg + have eρ : ρ = τg := + fl4VcpuCheck_native _ _ (hNative τ1 τg step hact) τg rfl _ check + subst eρ + exact ⟨σ1, τ1, ρ, f, Qf, Rf, ef, J1, R1, hf, tail, hact, mem_bindA.2 ⟨((), τ1), step, mem⟩, facts, + s1, a1, Q, Rl, Tcur, Ccur, hw, scheduled, J2⟩ + refine WP.spec_mono (reachedResultA W) ?_ + rintro out ⟨eout, σ1, τ1, τ2, f, Q, Rl, ef, J1, R1, hf, tail, hact, hfin, facts, atExit⟩ + obtain ⟨Z', H', OS', U', Ac', Pp', Tb', Lv', ia', ie', N', E', Rp', Bf', stack', κ', J2, hn⟩ := + hExit f out σ1 τ1 τ2 Q Rl ef eout J1 hf tail hact facts atExit + refine ⟨τ2, hfin, Z', H', OS', U', Ac', Pp', Tb', Lv', ia', ie', N', E', Rp', Bf', stack', κ', J2, fun hni => ?_⟩ + exact JSyscallA.ofSchedExitAt kLimitA view hf R1 tail hact J2 J2.scNZ hni facts.2.2.2.2.2.2.2 (hn hni) + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3KEntryFireSigA.lean b/proofs/Fl4ProofsA64/P3/Stage3KEntryFireSigA.lean new file mode 100644 index 000000000..eec47f6ee --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3KEntryFireSigA.lean @@ -0,0 +1,95 @@ +/- +P3 (aarch64, A64-P3 Stage 3, integration exec 8): **the fires loop's signal arm with its quiet cases discharged**. + +`JStage3A.fireSigA` gives `hSig` of `JStage3A.fire_recA` / `finishA` / `finishEA` (the record-level `send_signal` on a +fired thread's bound notification). The quiet arms (an active notification, or an idle one whose bound thread is neither +receive-blocked nor running a guest) are `JStage3A.irqSignalQuietA`, the guest-running arm is `JStage3A.sigVmA`, the +waking arms are `JStage3A.sigWakeA`; A64 P1's invariants after `send_signal` come from `send_signal.dmd`, as in the fires +loop. + +Named premises: [P] `ArchTotal`, `ArchPlat`, `hmb`, `hmax`, `htu`; `hCs`; [S] `numNodes = 1`. + +New module. +-/ +import Fl4ProofsA64.P3.Stage3KEntryIrqA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3 + Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.OuterEntry Classical + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + +set_option maxHeartbeats 1000000 in +/-- **The fires loop's signal arm at the record** (module docstring). -/ +theorem JStage3A.fireSigA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {m : McState} + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) : + ∀ (Q : U64 → U64 → List U64) (Rl : U64 → List U64) (s : S) (a : A) (σ : State Unit) (t badge : U64), + JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes + (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m → + SwA σ → t ∈ Z.T → L.mem s (boundA t) ≠ 0#u64 → + notification.send_signal st ar s a (L.mem s (boundA t)) badge ⦃ r => + ∃ σ' b Q' Rl', sendSignal (Z := Unit) (specPtr (L.mem s (boundA t))) b σ = ({((), σ')}, False) ∧ + JStage3A st AR { Z with Q := Q', R := Rl' } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ + fl4A64Platform.numNodes (J5A st) (J7A st (J7R2A st ar hPl.ipa)) r.1 r.2 σ' m ⦄ := by + intro Q Rl s a σ t badge J sw ht hb + have hcore : m.curCore < fl4A64Platform.numNodes := by + rw [J.sr.sch.core]; exact hCs _ J.sr.sch.cpuCs + have b1 := J.blkA + have htH : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + have hn : L.mem s (boundA t) ∈ H.ntfns := b1.2.2.1.2.2.bnd t htH hb + have hH : H (L.mem s (boundA t)) = some .ntfn := hn + have hp : L.mem s (boundA t) ∈ N := J.ipcHeapCov.ntfn _ hH + obtain ⟨x, hk, -⟩ := J.ntfn.ntfn _ hp + obtain ⟨_, _, _, _, _, _, _, _, _, _, _, Wj⟩ := J7A.unp J.j7 + have I := J.delIn hA hPl Wj + have hc0 : 0#u64 ∈ Z.Cs := by + obtain ⟨c, hc, e⟩ := J.coresAll 0 (Nat.lt_of_le_of_lt (Nat.zero_le _) hcore) + have : c = 0#u64 := UScalar.eq_of_val_eq (by rw [e]; rfl) + exact this ▸ hc + have W0 : SW st H Z.Cs OS Z.V ia ie Ac s s := SW.start J.sys.sysl J.symL J.blkA + have h0 := W0.1.sys.ly.h0 + have r1 : RunVcpuInv st H.tcbs s := J.tcbs ▸ J.runVcpu + have bt : L.mem s (ntfnBtA (L.mem s (boundA t))) = t := b1.2.2.1.2.2.bs.tb t htH hb + have dmd := WP.spec_and (notification.send_signal.dmd hA a _ badge b1 hn hH h0 W0.1.sym W0.1.sys.vr + (fun y hy hb => r1.blocked hy hb)) + (notification.send_signal.tinv0 hA s a _ badge b1 hn hH r1) + by_cases hq : QuietSigA σ x ∨ VmSigA σ x + · have still := hq.elim (fun q => J.irqSignalQuietA hA hcore hmb hmax htu hp hk q badge) + (fun v => J.sigVmA hA hCs h1 hmb hmax htu hp hk v badge) + refine WP.spec_mono (WP.spec_and still dmd) ?_ + rintro ⟨s3, a3⟩ ⟨⟨ha, x', hs, K⟩, hd⟩ + simp only [WP.uncurry'_pair] at ha K hd ⊢ + obtain ⟨d3, b3⟩ := hd + subst ha + refine (fun W3 => ?_) (W0.dmd I hc0 J.ko (fun k hk hia => ?_) d3 b3) + · obtain ⟨Q', Rl', J3⟩ := K W3.1.sys W3.1.sym (BlkOK.bxi W3.1.blk) + exact ⟨_, badge.bv, Q', Rl', hs, J3⟩ + · rw [hH] at hk; cases hk + exact W0.1.sys.ln _ .ntfn hH (LiveQ.ntfn hH (Or.inr (Or.inl (by rw [bt]; exact ne0_of_heap h0 htH)))) hia + obtain ⟨hq, hvm⟩ := not_or.1 hq + refine WP.spec_mono (WP.spec_and (J.sigWakeA hA hCs h1 hmb hmax htu hp hk hq hvm badge) dmd) ?_ + rintro ⟨s3, a3⟩ ⟨⟨ha, σ', hs, -, K⟩, hd⟩ + simp only [WP.uncurry'_pair] at ha K hd ⊢ + obtain ⟨d3, b3⟩ := hd + subst ha + refine (fun W3 => ?_) (W0.dmd I hc0 J.ko (fun k hk hia => ?_) d3 b3) + · obtain ⟨Q', Rl', J3⟩ := K W3.1.sys W3.1.sym + exact ⟨σ', badge.bv, Q', Rl', hs, J3⟩ + · rw [hH] at hk; cases hk + exact W0.1.sys.ln _ .ntfn hH (LiveQ.ntfn hH (Or.inr (Or.inl (by rw [bt]; exact ne0_of_heap h0 htH)))) hia + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3KEntryFiredA.lean b/proofs/Fl4ProofsA64/P3/Stage3KEntryFiredA.lean new file mode 100644 index 000000000..4724caca4 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3KEntryFiredA.lean @@ -0,0 +1,459 @@ +/- +P3 (aarch64, A64-P3 Stage 3, kernel-entry composition, Plan A, item 3): **the scheduler's inputs at the state the fires +loop produced**. + +`ScheduleInputsA`'s `selection` / `tail` fields are asked at every post-fire state; the record and the body's exports +hold only at the one the fire step produced. `ScheduleFiredInputsA` (`P3/Stage3KEntrySchedA`) asks them only at states +carrying the fire step's predicate `FF`, together with the frame of the release walk. + +* `JStage3A.jAtSchedA`: `JAtZAX` at any scheduler state of the schedule footprint, the domain-schedule words `DomW` + included (what `selection.roots` asks). +* `JStage3A.selectionA`: `ScheduleSelectionInputsA` from the record at the post-fire state (`NdOKA` is the record's + `domOK` across the release walk). + +Named premises of `selectionA`: +* [J] `roots`, `idleVt`: `VsRootOKA`, `IdleVtA` at the post-fire state (`VsRootOKA` and `.tcb`/`.run` are derived in `P3/Stage3KEntryRootA` + from the record; `.slot`/`.bound` are record edit #8b); +* [S] `numNodes = 1` (every TCB is on this core). + +Generated by `handoff/tools/gen_kentry_fired.py`. New module. +-/ +import Fl4ProofsA64.P3.Stage3KEntryExitA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3 + Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.OuterEntry Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +/-- The release walk keeps the arch state of the fire step. -/ +theorem awaken_archA {s : S} {a : A} {f r : S × A} (fire : timer.fire st ar s a = ok f) + (wake : sched.awaken st ar s a = ok r) : r.2 = f.2 := by + unfold sched.awaken at wake + rw [fire] at wake + simp only [bind_ok] at wake + obtain ⟨s1, a1⟩ := f + simp only [uncurry_apply_pair] at wake + obtain ⟨x, -, e⟩ := bind_eq_ok wake + rw [← ok_inj_ff e] + +/-- **`JAtZAX` at a scheduler state of the schedule footprint** (module docstring). -/ +theorem JStage3A.jAtSchedA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J5 : JSlotA S} {Rj : Set U64 → JSlotA S} {s s' : S} + {a : A} {σ σ' : State Unit} {m : McState} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7A st Rj) s a σ m) + {Q : U64 → U64 → List U64} {Rl : U64 → List U64} {curOf curScOf : Fl4SpecA64.CoreId → Fl4SpecA64.ObjRef} + (w : WritesIn st s s' (fun b => SchedStepWA (st := st) Z m σ s b ∨ DomW (L.ksb s) b)) (HS : SpecSchedA σ σ') + {a' : A} (har : AR a' Z.hw) + (hcurOf : ∀ k, k ≠ m.curCore → curOf k = Z.curOf k) + (Hq : YieldQueueRelA (st := st) { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } m κ s' σ') : + JAtZAX (L := L) AR { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } s' a' σ' m ∧ + SchedKeepMemA (st := st) Z s s' := by + have sr := Hq.sched + have hk := w.1 + have hc := w.2.1 + have osk := J.osKeep_stepDA w + have capE : ∀ u ∈ OS, capA st s' u = capA st s u := fun u hu => by + unfold capA; rw [(osk u hu).1, (osk u hu).2.1] + have keep : ∀ b, Aligned b → ¬ SchedStepWA (st := st) Z m σ s b → ¬ DomW (L.ksb s) b → L.mem s' b = L.mem s b := + fun b hb hn hd => w.mem_eq hb fun h => h.elim hn hd + obtain ⟨Pt, Ub, vr, -, arm, -, -, -, -, link, apart, -⟩ := J.j7 + have P := J.sr.place + have hT : ∀ t ∈ Z.T, t ∈ H.tcbs := fun t ht => by rw [J.tcbs]; exact ht + -- the page words + have page : ∀ p, SchedPageA st Z OS s p → ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → ¬ AsW (L.ksb s) (wa p o) → + L.mem s' (wa p o) = L.mem s (wa p o) := fun p hp o o1 o2 na => by + have hv : VsR (L.ksb s) Pt (wa p o) := Or.inl ⟨p, link p hp, o, o1, o2, rfl⟩ + exact keep _ (vr.al _ hv) (J.vsr_offStepA vr hv na) (J.omega_domA (vr.ks _ hv)) + have vsr : ∀ b, VsR (L.ksb s) Pt b → L.mem s' b = L.mem s b := fun b hb => by + refine keep _ (vr.al _ hb) ?_ (J.omega_domA (vr.ks b hb)) + by_cases h : AsW (L.ksb s) b + · obtain ⟨o, o1, o2, -, e⟩ := h + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.sys.ent.ksw + have ev : b.val = (L.ksb s).val + o.val := by + rw [e, wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact J.asid_offStepA (by omega) (by omega) + · exact J.vsr_offStepA vr hb h + have vsfoot : ∀ b, VsFootA Z.Pp Z.S1 Z.Y (L.ksb s) b → L.mem s' b = L.mem s b := fun b hb => by + refine vsr b ?_ + rcases hb.vsR with ⟨p, hp, h⟩ | h + · refine Or.inl ⟨p, link p ?_, h⟩ + rcases hp with hp | hp | hp + · exact Or.inl hp + · exact Or.inr (Or.inl hp) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl hp)))))) + · exact Or.inr h + have word : ∀ p, SchedPageA st Z OS s p → ∀ i : U64, i.val < 512 → L.mem s' (peAF p i) = L.mem s (peAF p i) := + fun p hp i hi => by + refine vsr _ (Or.inl ⟨p, link p hp, core.num.U64.wrapping_mul i 8#u64, ?_, ?_, rfl⟩) + · rw [wm8_valA hi]; omega + · rw [wm8_valA hi]; omega + have extfoot : ∀ b, ExtFootE Z.X Z.Y Z.T2 b → L.mem s' b = L.mem s b := fun b hb => by + cases hb with + | s2lo r i hr hi => exact word r (Or.inr (Or.inr (Or.inr (Or.inl hr)))) i hi + | s2hi r i hr hi => exact word _ (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨r, hr, rfl⟩))))) i hi + | s2pt t i ht hi => exact word t (Or.inr (Or.inr (Or.inl ht))) i hi + | wl0 y hy => exact word y (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl hy)))))) _ (by simp) + | wlo y i hy hi => exact word y (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl hy)))))) i (by omega) + -- the VCPU words + have vcw : ∀ v ∈ Z.V, ∀ o : U64, o.val = 0 ∨ o.val = 24 → L.mem s' (wa v o) = L.mem s (wa v o) := by + intro v hv o ho + have hH := arm.vh v hv + have hfit := J.sys.ent.heap.fits v _ hH + have hal := J.sys.ent.heap.align v _ hH + simp only [HKind.bits] at hfit hal + have ev : (wa v o).val = v.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have al : Aligned (wa v o) := by show _ % 8 = 0; rw [ev]; omega + have inObj : InObj v (.other 12) (wa v o) := by + unfold InObj; simp only [HKind.bits]; omega + have hgv : ∀ g : U64, g.val < 223488 → wa (L.ksb s) g ≠ wa v o := fun g hg e => + apart _ ⟨v, hv, o, by omega, by omega, rfl⟩ g hg e + refine keep _ al (J.omega_offStepA ⟨al, v, _, hH, Or.inr (Or.inr (Or.inl ⟨12, rfl⟩)), inObj⟩ ?_ hgv) + (J.omega_domA hgv) + rintro c ⟨z, hz⟩ ⟨c1, c2⟩ + have e : H.scBits c = z := by unfold Heap.scBits; rw [hz] + have ne : v ≠ c := fun h => by rw [h, hz] at hH; cases hH + have := J.sys.ent.heap.disj v _ c _ hH hz ne + simp only [HKind.bits] at this + rw [e] at c2 + omega + have vcwAll : ∀ v ∈ Z.V, ∀ o : U64, o.val < 4096 ∧ o.val % 8 = 0 → L.mem s' (wa v o) = L.mem s (wa v o) := by + intro v hv o ⟨ho1, ho2⟩ + have hH := arm.vh v hv + have hfit := J.sys.ent.heap.fits v _ hH + have hal := J.sys.ent.heap.align v _ hH + simp only [HKind.bits] at hfit hal + have ev : (wa v o).val = v.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have al : Aligned (wa v o) := by show _ % 8 = 0; rw [ev]; omega + have inObj : InObj v (.other 12) (wa v o) := by + unfold InObj; simp only [HKind.bits]; omega + have hgv : ∀ g : U64, g.val < 223488 → wa (L.ksb s) g ≠ wa v o := fun g hg e => + apart _ ⟨v, hv, o, by omega, by omega, rfl⟩ g hg e + refine keep _ al (J.omega_offStepA ⟨al, v, _, hH, Or.inr (Or.inr (Or.inl ⟨12, rfl⟩)), inObj⟩ ?_ hgv) + (J.omega_domA hgv) + rintro c ⟨z, hz⟩ ⟨c1, c2⟩ + have e : H.scBits c = z := by unfold Heap.scBits; rw [hz] + have ne : v ≠ c := fun h => by rw [h, hz] at hH; cases hH + have := J.sys.ent.heap.disj v _ c _ hH hz ne + simp only [HKind.bits] at this + rw [e] at c2 + omega + have slotw : ∀ y, IsSlotA Z.th y → ∀ x, (x = y ∨ x = wa y 8#u64 ∨ x = wa y 16#u64 ∨ x = wa y 24#u64) → + L.mem s' x = L.mem s x := by + intro y hy x hx + obtain ⟨a32, f32⟩ := hy.al32 P + have hv : ∀ o : U64, o.val < 32 → (wa y o).val = y.val + o.val := fun o ho => by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have key : x.val % 8 = 0 ∧ y.val ≤ x.val ∧ x.val < y.val + 32 := by + rcases hx with rfl | rfl | rfl | rfl + · omega + all_goals + rw [hv _ (by simp)]; simp; omega + exact keep _ key.1 (J.slot_offStepA hy key.2.1 key.2.2) (J.slot_domA hy key.2.1 key.2.2) + have cnw : ∀ y, SlotWA Z.th y → IsSlotA Z.th y := fun y hy => by + rcases hy with h | h + · exact h + · obtain ⟨p, r, hp, idx, hl, rfl⟩ := h + obtain ⟨e, hb, -⟩ := cn_slotV P hp hl + exact ⟨p, Or.inl ⟨r, hp, blvA idx, hb, e⟩⟩ + have tcbl : ∀ t ∈ Z.T, ∀ K : U64, K = 3664#u64 ∨ K = 3672#u64 → L.mem s' (wa t K) = L.mem s (wa t K) := by + intro t ht K hK + have hta := J.sr.core.tcbs t ht + refine keep _ (field_aligned (K := K) hta ?_ ?_) (J.link_offStepA ht hK) + (J.tcbK_domA ht (by rcases hK with rfl | rfl <;> simp)) + all_goals rcases hK with rfl | rfl <;> simp + have mem : SchedKeepMemA (st := st) Z s s' := + { ksb := hk + cpu := hc + slot := fun y hy x hx => slotw y (cnw y hy) x hx + tcbW := fun t ht => ⟨tcbl t ht _ (Or.inl rfl), tcbl t ht _ (Or.inr rfl)⟩ + vcW := fun v hv => by + have e0 : wa v 0#u64 = v := by apply UScalar.eq_of_val_eq; rw [wa_valm]; simp + have h0 := vcw v hv 0#u64 (Or.inl (by simp)) + rw [e0] at h0 + exact ⟨h0, vcw v hv _ (Or.inr (by simp))⟩ + irqW := fun i hi => by + refine keep _ ?_ (J.irq_offStepA hi) (J.irq_domA hi) + have := irqWordA_val J.sr.ksFit hi + have := J.sr.toSchedSRN.ksb_al + show _ % 8 = 0 + omega + vs := vsfoot + ext := extfoot } + -- the node cursors + have nodes : ∀ c ∈ Z.Cs, + L.mem s' (L.nodeAt s' c layout.node.IDLE_THREAD) ∈ Z.T ∧ L.mem s' (L.nodeAt s' c layout.node.IDLE_SC) ∈ Z.C ∧ + L.mem s' (L.nodeAt s' c layout.node.CUR_THREAD) ∈ Z.T := by + intro c hcs + refine ⟨?_, ?_, ?_⟩ + · have e : L.mem s' (L.nodeAt s' c layout.node.IDLE_THREAD) = L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) := + specPtr_inj ((sr.sch.idleOf c hcs).symm.trans (J.sr.sch.idleOf c hcs)) + rw [e]; exact J.j.1.idleT c hcs + · have e : L.mem s' (L.nodeAt s' c layout.node.IDLE_SC) = L.mem s (L.nodeAt s c layout.node.IDLE_SC) := + specPtr_inj ((sr.sch.idleScOf c hcs).symm.trans (J.sr.sch.idleScOf c hcs)) + rw [e]; exact J.j.1.idleC c hcs + · by_cases h : c = L.cpu s + · have := sr.curT + unfold LawfulStore.curW at this + rw [hc, ← h] at this + exact this + · have hne : c.val ≠ m.curCore := fun e => h (UScalar.eq_of_val_eq (e.trans J.sr.sch.core)) + have h1 := sr.sch.curOfW c hcs + rw [show ({ Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } : XSetsA).curOf = curOf from rfl, + hcurOf _ hne] at h1 + have e : L.mem s' (L.nodeAt s' c layout.node.CUR_THREAD) = L.mem s (L.nodeAt s c layout.node.CUR_THREAD) := + specPtr_inj (h1.symm.trans (J.sr.sch.curOfW c hcs)) + rw [e]; exact J.j.1.curT c hcs + have st' : StEqA Z.T (L.mem s') σ' := Hq.states + have jx := JAtZAX.of_sched J.j (a' := a') (Q := Q) (R := Rl) (curOf := curOf) (curScOf := curScOf) mem HS har + (sr.sch.withSt st') sr.rest (fun c h => (nodes c h).1) (fun c h => (nodes c h).2.1) + (fun c h => (nodes c h).2.2) sr.cscAll + exact ⟨jx, mem⟩ + +/-- (record edit F4) **The run links, the single core and the high windows' addresses from the record**: what `ArchSwFA`'s +`execThread` needs beyond `JAtZAX` (`VcRunA.run` / `.vtcb` of `JStage3A.vcRun`; `Z.Y` members are heap objects, off +address `0`; the one core is core `0`). -/ +theorem JStage3A.archRestA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J5 J7 : JSlotA S} {s : S} {a : A} {σ : State Unit} {m : McState} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) : + ArchRestA (L := L) Z s where + c0 := by + have := hCs _ J.sr.sch.cpuCs + omega + Y0 y hy hy0 := by + obtain ⟨q, z, hq, hle, -⟩ := J.setsInH.roots y (Or.inr hy) + have hw := (J.heapWin q _ hq).1 + have hpv : layout.PPTR_BASE.val = 18446603336221196288 := by unfold layout.PPTR_BASE; rfl + subst hy0 + have : (0#u64 : U64).val = 0 := rfl + omega + R t ht hne := by + rcases J.vcRun.run t ht with h | h + · exact absurd h hne + · refine ⟨h, fun hz => ?_⟩ + rcases J.vcRun.vtcb _ h with h' | h' + · exact absurd h' hz + · exact h' + +/-- The run links, core and window facts across a scheduler step (`SchedKeepMemA`). -/ +theorem ArchRestA.keepMemA {Z Z' : XSetsA} {s s' : S} (R : ArchRestA (L := L) Z s) + (M : SchedKeepMemA (st := st) Z s s') (hT : Z'.T = Z.T) (hV : Z'.V = Z.V) (hY : Z'.Y = Z.Y) : + ArchRestA (L := L) Z' s' where + c0 := by rw [M.cpu]; exact R.c0 + Y0 := by rw [hY]; exact R.Y0 + R t ht := by + rw [hT] at ht + have hr : L.mem s' (runVcpuA t) = L.mem s (runVcpuA t) := (M.tcbW t ht).2 + have h0 := R.R t ht + unfold RunLinkA at h0 ⊢ + rw [hr] + intro hne + obtain ⟨hv, hz⟩ := h0 hne + have e : L.mem s' (L.mem s (runVcpuA t)) = L.mem s (L.mem s (runVcpuA t)) := (M.vcW _ hv).1 + rw [e, hV, hT] + exact ⟨hv, hz⟩ + +/-! ## The root facts across a scheduler footprint -/ + +theorem SchedFrameA.aobjsA {σ σ' : State Unit} (F : SpecSchedA σ σ') : aobjsOf σ' = aobjsOf σ := by + funext p + unfold aobjsOf optMap + rcases F.kh p with e | ⟨x, x', e, e', -⟩ | ⟨o, o', e, e', h⟩ + · rw [e] + · rw [e, e']; rfl + · rw [e, e'] + rcases h with ⟨_, _, _, _, rfl, rfl⟩ | ⟨_, _, rfl, rfl⟩ | ⟨_, _, rfl, rfl⟩ | ⟨_, _, rfl, rfl⟩ <;> rfl + +/-- The TCB fields the root facts read: their value at `t`, when `g` ignores the SC binding. -/ +theorem tcbBind_keepA {β : Type} {σ σ' : State Unit} (F : SpecSchedA σ σ') (K : SKeepA σ σ') (g : Tcb → Option β) + (hg : ∀ x o, g { x with tcbSchedContext := o } = g x) (t : ObjRef) : + (match σ'.kheap t with | some (.tcb x) => g x | _ => none) = + (match σ.kheap t with | some (.tcb x) => g x | _ => none) := by + have fwd (x : Tcb) (h : σ.kheap t = some (.tcb x)) : ∃ o, σ'.kheap t = some (.tcb { x with tcbSchedContext := o }) := by + obtain ⟨y, hy, o, rfl⟩ := K.tcb t x h + exact ⟨o, hy⟩ + rcases F.kh t with e | ⟨x, x', e, -, -⟩ | ⟨o, o', e, e', h⟩ + · rw [e] + · obtain ⟨q, hq⟩ := fwd x e + rw [e, hq] + exact hg x q + · rw [e, e'] + rcases h with ⟨_, _, _, _, rfl, rfl⟩ | ⟨_, _, rfl, rfl⟩ | ⟨_, _, rfl, rfl⟩ | ⟨_, _, rfl, rfl⟩ <;> rfl + +section +variable {σ σ' : State Unit} (F : SpecSchedA σ σ') (K : SKeepA σ σ') +include F K + +theorem vtCapA_keepA (t : ObjRef) : vtCapA σ' t = vtCapA σ t := + tcbBind_keepA F K (fun x => tcbCnodeMap x (tcbCnodeIndex 1)) (fun _ _ => rfl) t + +theorem vcpuOfA_keepA (t : ObjRef) : vcpuOfA σ' t = vcpuOfA σ t := + tcbBind_keepA F K (fun x => x.tcbArch.tcbVcpu) (fun _ _ => rfl) t + +omit K in +theorem vcpusOf_keepA : vcpusOf σ' = vcpusOf σ := by + unfold vcpusOf + rw [SchedFrameA.aobjsA F] + +theorem runGuestA_keepA (t : ObjRef) : runGuestA σ' t = runGuestA σ t := by + have h := tcbBind_keepA F K (fun x => some x.tcbExt.tcbRunVcpu) (fun _ _ => rfl) t + have key (τ : State Unit) : runGuestA τ t = + (match τ.kheap t with | some (.tcb x) => some x.tcbExt.tcbRunVcpu | _ => none).bind fun o => + o.bind fun v => (vcpusOf τ v).bind fun vc => vc.vcpuTcb.bind fun g => some (v, g) := by + unfold runGuestA + cases τ.kheap t with + | none => rfl + | some o => cases o <;> rfl + rw [key, key, h, vcpusOf_keepA F] + +theorem runningVmA_keepA (t : ObjRef) : runningVmA σ' t ↔ runningVmA σ t := by + have h := tcbBind_keepA F K (fun x => some x.tcbState) (fun _ _ => rfl) t + have key (τ : State Unit) : runningVmA τ t ↔ + (match τ.kheap t with | some (.tcb x) => some x.tcbState | _ => none) = some (.ext .runningVM) := by + unfold runningVmA + constructor + · rintro ⟨x, e, s⟩ + rw [e] + exact congrArg some s + · intro q + split at q + · rename_i x e + exact ⟨x, e, Option.some.inj q⟩ + · cases q + rw [key, key, h] + +omit K in +theorem vspaceForAsid_keepA (asid : Asid) : vspaceForAsid asid σ' = vspaceForAsid asid σ := by + have a := SchedFrameA.aobjsA F + have p : asidPoolsOf σ' = asidPoolsOf σ := by unfold asidPoolsOf; rw [a] + have key (u v : State Unit) (h1 : u.archState.armAsidTable = v.archState.armAsidTable) + (h2 : asidPoolsOf u = asidPoolsOf v) : vspaceForAsid asid u = vspaceForAsid asid v := by + unfold vspaceForAsid entryForAsid poolForAsid asidTable Lookup.oassert Lookup.oreturn Lookup.ofail + simp only [Bind.bind, ReaderT.bind, h1, h2] + split <;> rfl + exact key _ _ F.asid p + +omit K in +theorem rootKindAt_keepA (r : ObjRef) : rootKindAt σ' r = rootKindAt σ r := by + have e : extObjAt σ' r = extObjAt σ r := by + unfold extObjAt + rcases F.kh r with e | ⟨x, x', e, e', -⟩ | ⟨o, o', e, e', h⟩ + · rw [e] + · rw [e, e'] + · rw [e, e'] + rcases h with ⟨_, _, _, _, rfl, rfl⟩ | ⟨_, _, rfl, rfl⟩ | ⟨_, _, rfl, rfl⟩ | ⟨_, _, rfl, rfl⟩ <;> rfl + unfold rootKindAt + rw [e] + +theorem VsRootOKA.keepA (H : VsRootOKA σ) : VsRootOKA σ' := by + refine ⟨fun t root asid va h1 h2 => ?_, fun t asid h => ?_, fun t v h => ?_, fun t v g h1 h2 => ?_⟩ + · rw [rootKindAt_keepA F] + rw [vtCapA_keepA F K] at h1 + rw [vspaceForAsid_keepA F] at h2 + exact H.s1 t root asid va h1 h2 + · have e : (vmRootA σ' t).asid = (vmRootA σ t).asid := by + unfold vmRootA + rw [vtCapA_keepA F K] + split + · rw [vspaceForAsid_keepA F] + split <;> rfl + · rfl + exact H.userAsid t asid (e.symm.trans h) + · rw [vcpuOfA_keepA F K] at h + rw [vcpusOf_keepA F] + exact H.bound t v h + · rw [runningVmA_keepA F K] at h1 + rw [runGuestA_keepA F K] at h2 + rw [vcpusOf_keepA F, vcpuOfA_keepA F K] + exact H.run t v g h1 h2 + +theorem IdleVtA.keepA (idle : σ'.idleThread = σ.idleThread) (H : IdleVtA σ) : IdleVtA σ' := by + obtain ⟨x, hx, hs⟩ := H.tcb + refine ⟨?_, ?_, ?_, ?_⟩ + · obtain ⟨y, hy, o, rfl⟩ := K.tcb _ x hx + exact ⟨{ x with tcbSchedContext := o }, by rw [idle]; exact hy, hs⟩ + · rw [idle, vtCapA_keepA F K]; exact H.slot + · rw [idle, vcpuOfA_keepA F K]; exact H.bound + · rw [idle, runGuestA_keepA F K]; exact H.run + +end + +/-! ## The selection inputs -/ + +/-- **`ScheduleSelectionInputsA` from facts at the post-fire state** (module docstring). `NdOKA` at the release +walk's outcome is the record's `domOK` across the walk's frame. -/ +theorem JStage3A.selectionA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J5 : JSlotA S} {Rj : Set U64 → JSlotA S} + {s1 s2 : S} {a1 a2 : A} {σ1 σ2 : State Unit} {m : McState} + {Q : U64 → U64 → List U64} {R : U64 → List U64} + (J1 : JStage3A st AR { Z with Q := Q, R := R } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7A st Rj) + s1 a1 σ1 m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (R1 : FireRecA σ1) (waking : awaken (Z := Unit) σ1 = ({((), σ2)}, False)) + {Q2 : U64 → U64 → List U64} {R2 : U64 → List U64} + (H2 : YieldQueueRelA (st := st) { Z with Q := Q2, R := R2 } m κ s2 σ2) + (F : MFrA (st := st) { Z with Q := Q, R := R } m s1 s2 σ1 σ2) (SF : SchedFrameA σ1 σ2) (ea : a2 = a1) + (roots : VsRootOKA σ1) (idleVt : IdleVtA σ1) : + ScheduleSelectionInputsA (st := st) { Z with Q := Q2, R := R2 } m κ s2 a2 σ2 AR := by + have hm : ((), σ2) ∈ (awaken (Z := Unit) σ1).1 := by rw [waking]; exact Set.mem_singleton _ + have SB : SBindA σ1 σ2 := awaken_bindA σ1 _ hm + have dom : NdOKA (st := st) s2 σ2 := J1.domOK.keepF (domKeep_ofQA _ waking) J1.sr.toSchedSRN F + refine ⟨?_, ?_, ?_⟩ + · have sw := (awaken_swa σ1 ⟨R1.1, R1.2.2.1, R1.2.2.2⟩ _ hm).2.1 + obtain ⟨x, y, k, hx, es, -⟩ := J1.sinv.idleSc + obtain ⟨z, hz, hs⟩ := J1.sinv.idleTcb + rw [hx] at hz + obtain rfl := KernelObject.tcb.inj (Option.some.inj hz) + have tK : σ2.kheap σ1.idleThread = some (.tcb x) := by + obtain ⟨y', hy, o, e⟩ := F.keep.tcb _ x hx + obtain ⟨x0, h0, es0⟩ := SB.tcb _ y' hy + rw [hx] at h0 + obtain rfl := KernelObject.tcb.inj (Option.some.inj h0) + have : y' = x := by + subst e + cases x + simp only at es0 ⊢ + rw [es0] + rw [hy, this] + refine ⟨sw, x, by rw [SB.idle]; exact tK, es, hs, fun hmem => ?_⟩ + obtain ⟨u, hu, hr⟩ := H2.sched.run.2 _ hmem + rw [SB.idle, tK] at hu + obtain rfl := KernelObject.tcb.inj (Option.some.inj hu) + rw [hs] at hr + cases hr + · intro Q3 s3 σ3 H3 F3 _ D + exact dom.keepF D H2.sched.toSchedSRN F3 + · intro Q3 s3 σ3 H3 F3 SF3 + have I3 : σ3.idleThread = σ2.idleThread := by + rw [H3.sched.sch.idle, H2.sched.sch.idle] + exact (H3.sched.sch.idleOf _ H3.sched.sch.cpuCs).symm.trans + (by rw [F3.wr.2.1]; exact H2.sched.sch.idleOf _ H2.sched.sch.cpuCs) + have F13 : MFrDA (st := st) { Z with Q := Q, R := R } m s1 s3 σ1 σ3 := F.transD F3 rfl rfl + have S13 : SchedFrameA σ1 σ3 := PreO.trans SF SF3 + have J3M : JAtZAX (L := L) AR { Z with Q := Q3, R := R2 } s3 a2 σ3 m ∧ SchedKeepMemA (st := st) { Z with Q := Q, R := R } s1 s3 := + JStage3A.jAtSchedA J1 (Q := Q3) (Rl := R2) (curOf := Z.curOf) (curScOf := Z.curScOf) + (F13.wr.mono fun _ h => h.elim (fun h => Or.inl (schedStepW_mwr h)) Or.inr) S13.1 (by rw [ea]; exact J1.j.1.ar) (fun _ _ => rfl) H3 + have J3 := J3M.1 + refine ⟨J3, roots.keepA S13.1 F13.keep, idleVt.keepA S13.1 F13.keep (I3.trans SB.idle), ?_, ?_⟩ + swap + · exact (JStage3A.archRestA J1 hCs h1).keepMemA J3M.2 rfl rfl rfl + have hI : L.idleW s3 ∈ Z.T := J3.1.idleT _ J3.1.sch.cpuCs + have x := hCs _ (H3.sched.core.aff _ hI) + have y := hCs _ H3.sched.sch.cpuCs + exact UScalar.eq_of_val_eq (by omega) + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3KEntryIrqA.lean b/proofs/Fl4ProofsA64/P3/Stage3KEntryIrqA.lean new file mode 100644 index 000000000..26f95f6af --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3KEntryIrqA.lean @@ -0,0 +1,607 @@ +/- +P3 (aarch64, A64-P3 Stage 3, kernel-entry composition, step 2): **interrupt service at the record**, the `handle` +hypothesis of the interrupt entry arm (`interrupt.handle_interrupt` against l4v's `handleInterrupt`), and the budget +entry facts at the next entry from the previous exit. + +* `JStage3A.irqP1A`: A64 P1's walk bundle across the whole `handle_interrupt` (`handle_interrupt.sw`). +* `JStage3A.irqSignalQuietA`: `send_signal` on a record notification, the arms that wake no thread. +* `JStage3A.handleInterruptA`: every arm of `handle_interrupt` at the record, with `SwA` / `BodyExitA` kept (what + `syscall.finish` takes): a number above `MAX` (mask, ack); a signal line (`send_signal` through the handler cap, mask, + ack); a timer line (`REPROGRAM`, `ARMED`; the fires loop runs in `finish`); a reserved line and a remote-call IPI + (ack). +* `interrupt_serviceA`: the interrupt entry arm (`P3/OuterEntryA.interruptEntry_refinesA`) with that service. +* `ChargeEntryA.ofExitA` / `.ofSchedExitA`: the budget entry facts of the next entry (an idle current thread included) + from the exit's record, its `resume` action and its current-SC binding `HcA`; the two are what `fl4Schedule` and the + activation leave. + +The arch-to-spec machine relation stays outside the record (`P3/MachKeepA`): the record is at +`withMRA AR0 MR σ.machineState`, before and after. + +Named premises: +* [P] `IrqArchA`: `mask_irq` / `ack_irq` keep `AR0` and step `MR` as the spec's mask / ack. Its `signalMask` is the + GICv3 case of a signal line: FL4 masks the line, l4v's `arch_mask_irq_signal` leaves it (the active interrupt is not + redelivered before its deactivation), so `MR` must allow a line masked by hardware only. +* [P] `hMax` (`maxIRQ = 5375`), [P] `ArchTotal`, `ArchPlat`, `hmb`, `hmax`, `htu`; `hCs`. +* `IrqLineA`: [P] `num` (13-bit numbers); [J] `active` (the accepted line is not inactive: l4v fails there); [S] + `noResched` (no reschedule IPI on one core), `reserved` (VGIC maintenance and the virtual timer arrive at their own + entries, S4). +* [S] `numNodes = 1` (the waking signal arms: `JStage3A.sigWakeA`, and `JStage3A.sigVmA` for a bound thread running a + guest). + +New module. +-/ +import Fl4ProofsA64.P3.Stage3KEntryP1A +import Fl4ProofsA64.P3.Stage3KEntryTimerIrqA +import Fl4ProofsA64.P3.Stage3KEntryBudgetA +import Fl4ProofsA64.P3.Stage3SignalIdleA +import Fl4ProofsA64.P3.Stage3SigWakeAnyA +import Fl4ProofsA64.P3.Stage3SigVmArmA +import Fl4ProofsA64.P3.Stage3LiftMachA +import Fl4ProofsA64.P3.Stage3KEntryFinishA +import Fl4ProofsA64.P3.IrqInvokeSlotsA +import Fl4ProofsA64.P3.IrqInvokePerformA +import Fl4ProofsA64.P1.S1aCIrq +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3 + Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.OuterEntry Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-- [P] **The interrupt controller operations** `handle_interrupt` uses (module docstring). -/ +structure IrqArchA {A : Type} (ar : machine.Arch A) (AR0 : A → HwRootA → Prop) (MR : A → MachineState → Prop) : + Prop where + maskKeep : ∀ a h irq b, AR0 a h → ar.mask_irq a irq b ⦃ a' => AR0 a' h ⦄ + ackKeep : ∀ a h irq, AR0 a h → ar.ack_irq a irq ⦃ a' => AR0 a' h ⦄ + mask : ∀ a ms (irq : U64) b, MR a ms → irq.val < 8192 → + ar.mask_irq a irq b ⦃ a' => ∃ ms', ((), ms') ∈ (maskInterrupt b (BitVec.ofNat 13 irq.val) ms).1 ∧ MR a' ms' ⦄ + ack : ∀ a ms irq, MR a ms → ar.ack_irq a irq ⦃ a' => MR a' ms ⦄ + signalMask : config_ARM_GIC_V3 = true → ∀ a ms (irq : U64), MR a ms → irq.val < 5376 → + ar.mask_irq a irq true ⦃ a' => MR a' ms ⦄ + +/-- **The accepted line** (module docstring), at the state `handle_interrupt` runs in. -/ +structure IrqLineA {S : Type} (st : machine.Store S) [L : LawfulStore st] (s : S) (irq : U64) : Prop where + num : irq.val < 8192 + active : irq.val < 5376 → L.mem s (irqWordA (L.ksb s) irq) ≠ 0#u64 + noResched : irq.val < 5376 → L.mem s (irqWordA (L.ksb s) irq) = 3#u64 → layout.irq.id irq ≠ ok 1#u64 + reserved : irq.val < 5376 → L.mem s (irqWordA (L.ksb s) irq) ≠ 1#u64 → + L.mem s (irqWordA (L.ksb s) irq) ≠ 2#u64 → + (BitVec.ofNat 13 irq.val : Irq) ≠ irqVGICMaintenance ∧ irqVppiEventIndex (BitVec.ofNat 13 irq.val) = none + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR AR0 : A → HwRootA → Prop} {MR : A → MachineState → Prop} {Z : XSetsA} {H : Heap} + {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} + {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {nA : U64} {s s1 : S} + {a a1 : A} {σ σ1 : State Unit} {m : McState} + +/-- **A64 P1's invariants after `handle_interrupt` at the record** (module docstring). -/ +theorem _root_.Fl4ProofsA64.P3.JStage3A.irqP1A (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) + s a σ m) (hc0 : 0#u64 ∈ Z.Cs) (irq : U64) : + interrupt.handle_interrupt st ar s a irq ⦃ r => SysL st H Z.Cs OS ia ie r.1 ∧ SymL st H Z.Cs r.1 ∧ + BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac r.1 ⦄ := by + obtain ⟨_, _, _, _, _, _, _, _, _, _, _, W⟩ := J7A.unp J.j7 + have I := J.delIn hA hPl W + have rv : RunVcpuInv st H.tcbs s := by rw [J.tcbs]; exact J.runVcpu + refine WP.spec_mono (interrupt.handle_interrupt.sw (Vc := Z.V) (ia := ia) (ie := ie) (Ac := Ac) hA a irq I + J.sys.sysl J.symL J.blkA J.ko J.irqPlaceA rv) ?_ + rintro ⟨s', a'⟩ W' + simp only [WP.uncurry'_pair] at W' ⊢ + exact ⟨W'.1.sys, W'.1.sym, BlkOK.bxi W'.1.blk⟩ + +/-- **`send_signal` on a record notification, waking no thread** (module docstring). -/ +theorem _root_.Fl4ProofsA64.P3.JStage3A.irqSignalQuietA (hA : P0.ArchTotal ar) + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (hc : m.curCore < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p : U64} (hp : p ∈ N) {x : Notification} (hk : σ.kheap (specPtr p) = some (.notification x)) + (hq : QuietSigA σ x) (badge : U64) : + notification.send_signal st ar s a p badge ⦃ r => r.2 = a ∧ ∃ x', + sendSignal (Z := Unit) (specPtr p) badge.bv σ = ({((), updK σ (specPtr p) (.notification x'))}, False) ∧ + (SysL st H Z.Cs OS ia ie r.1 → SymL st H Z.Cs r.1 → + BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac r.1 → + ∃ Q Rl, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) r.1 a (updK σ (specPtr p) (.notification x')) m) ⦄ := by + have P := J.iplaced + have pH : H p = some .ntfn := J.utIpc.ntfn p hp + have rv1 : RunVcpuInv st H.tcbs s := J.tcbs ▸ J.runVcpu + have rvi := Fl4ProofsA64.P1.notification.send_signal.rvi (ar := ar) hA s a p badge (BXI.blk J.sys.ent.bxi) + (show p ∈ H.ntfns from pH) pH rv1 + have body := send_signal_quietA (st := st) (ar := ar) P.n s a σ J.ntfn hp hk hq badge + refine WP.spec_mono (WP.spec_and body rvi) ?_ + rintro ⟨s', a'⟩ ⟨⟨ha, b, hs, R', w⟩, rv'⟩ + simp only [WP.uncurry'_pair] at ha R' w rv' ⊢ + subst ha + refine ⟨rfl, quietNtfnA x b, hs, fun sysl symL bxi => ?_⟩ + have wN : WritesIn st s s' (SchedStepNWA (st := st) N E Rp Z m σ s) := + w.mono fun c h => Or.inr (Or.inl ⟨p, hp, h.1, by unfold NObjW at h; omega⟩) + have nq : ∀ q, x.ntfnObj ≠ .waitingNtfn q := fun q e => by + rcases hq with ⟨h, -⟩ | ⟨_, h⟩ <;> rw [e] at h <;> cases h + exact J.of_ntfnQuietA hc hmb hmax htu hp hk nq b w R' bxi (J.tcbs ▸ rv') sysl symL + (J.sys.ent.user.ksb w.1) (J.j57_stepNWA wN _).2 + +/-- The spec state at another machine state: what `finish` takes besides the record is kept when the clock is. -/ +private theorem bodyMsA {ms : MachineState} (h : ms.lastMachineTime = σ.machineState.lastMachineTime) + (B : BodyExitA σ) : BodyExitA { σ with machineState := ms } := + ⟨by unfold ValidMachineTimeA; show σ.curTime ≤ ms.lastMachineTime; rw [h]; exact B.validTime, B.budget, B.ctAct⟩ + +/-- **The record across a machine-state step**, with the machine relation at the new state. -/ +theorem _root_.Fl4ProofsA64.P3.JStage3A.irqMsA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {a' : A} (ms : MachineState) (hm : ms.underlyingMemory = σ.machineState.underlyingMemory) + (har : AR0 a' Z.hw) (hmr : MR a' ms) : + JStage3A st (withMRA AR0 MR ms) Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s a' { σ with machineState := ms } m := + ((J.arSwapA (AR' := fun _ _ => True) trivial).of_machA (a2 := a') trivial ms hm).arSwapA + (AR' := withMRA AR0 MR ms) ⟨har, hmr⟩ + +private theorem maskStepA {irq : Irq} {b : Bool} {ms ms' : MachineState} + (h : ((), ms') ∈ (maskInterrupt b irq ms).1) : + ms'.underlyingMemory = ms.underlyingMemory ∧ ms'.lastMachineTime = ms.lastMachineTime := by + unfold maskInterrupt at h + cases h + exact ⟨rfl, rfl⟩ + +private theorem ackSpecA (irq : Irq) (σ : State Unit) : + ((), σ) ∈ (doMachineOp (Z := Unit) (ackInterrupt irq) σ).1 := + irqMachine_memA _ σ (ms' := σ.machineState) rfl + +/-- **`ack_irq` at the record**: the spec's ack changes nothing. -/ +private theorem ackRecA (K : IrqArchA ar AR0 MR) + (J : JStage3A st (withMRA AR0 MR σ.machineState) Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s a σ m) (irq : U64) : + ∃ a', ar.ack_irq a irq = ok a' ∧ + JStage3A st (withMRA AR0 MR σ.machineState) Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s a' σ m := by + have har : withMRA AR0 MR σ.machineState a Z.hw := J.j.1.ar + obtain ⟨a1, e1, p1⟩ := spec_existsA (K.ackKeep a _ irq har.1) + obtain ⟨a2, e2, p2⟩ := spec_existsA (K.ack a _ irq har.2) + have : a2 = a1 := by rw [e1] at e2; exact (ok_inj_ff e2).symm + subst this + have J' := J.irqMsA σ.machineState rfl p1 p2 + exact ⟨a2, e1, J'⟩ + +/-- **`mask_irq … true` then `ack_irq`** against the spec's mask and ack. -/ +private theorem maskAckRecA (K : IrqArchA ar AR0 MR) + (J : JStage3A st (withMRA AR0 MR σ.machineState) Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s a σ m) (irq : U64) (hi : irq.val < 8192) : + ∃ a1 a2 ms, ar.mask_irq a irq true = ok a1 ∧ ar.ack_irq a1 irq = ok a2 ∧ + ((), ms) ∈ (maskInterrupt true (BitVec.ofNat 13 irq.val) σ.machineState).1 ∧ + ms.lastMachineTime = σ.machineState.lastMachineTime ∧ + JStage3A st (withMRA AR0 MR ms) Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s a2 { σ with machineState := ms } m := by + have har : withMRA AR0 MR σ.machineState a Z.hw := J.j.1.ar + obtain ⟨a1, e1, p1⟩ := spec_existsA (K.maskKeep a _ irq true har.1) + obtain ⟨a1', e1', ms, hstep, hmr⟩ := spec_existsA (K.mask a _ irq true har.2 hi) + have : a1' = a1 := by rw [e1] at e1'; exact (ok_inj_ff e1').symm + subst this + have J1 := J.irqMsA ms (maskStepA hstep).1 p1 hmr + obtain ⟨a2, e2, J2⟩ := ackRecA (σ := { σ with machineState := ms }) K J1 irq + exact ⟨a1', a2, ms, e1, e2, hstep, (maskStepA hstep).2, J2⟩ + +/-- **The tail of a signal line**: FL4's mask and ack against `arch_mask_irq_signal` and the ack. -/ +private theorem signalTailA (K : IrqArchA ar AR0 MR) + (J : JStage3A st (withMRA AR0 MR σ.machineState) Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s a σ m) (irq : U64) (hi : irq.val < 5376) : + ∃ a1 a2 σ2, ar.mask_irq a irq true = ok a1 ∧ ar.ack_irq a1 irq = ok a2 ∧ + ((), σ2) ∈ ((archMaskIrqSignal (Z := Unit) (BitVec.ofNat 13 irq.val) >>= fun _ => + doMachineOp (ackInterrupt (BitVec.ofNat 13 irq.val))) σ).1 ∧ + (SwA σ → SwA σ2) ∧ (BodyExitA σ → BodyExitA σ2) ∧ σ2.curSc = σ.curSc ∧ + JStage3A st (withMRA AR0 MR σ2.machineState) Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s a2 σ2 m := by + have har : withMRA AR0 MR σ.machineState a Z.hw := J.j.1.ar + by_cases v3 : config_ARM_GIC_V3 = true + · obtain ⟨a1, e1, p1⟩ := spec_existsA (K.maskKeep a _ irq true har.1) + obtain ⟨a1', e1', p2⟩ := spec_existsA (K.signalMask v3 a _ irq har.2 hi) + have : a1' = a1 := by rw [e1] at e1'; exact (ok_inj_ff e1').symm + subst this + have J1 := J.irqMsA σ.machineState rfl p1 p2 + obtain ⟨a2, e2, J2⟩ := ackRecA (σ := σ) K J1 irq + refine ⟨a1', a2, σ, e1, e2, ?_, id, id, rfl, J2⟩ + refine mem_bind_ndD (x := ((), σ)) ?_ (ackSpecA _ σ) + unfold archMaskIrqSignal + simp only [v3, not_true_eq_false] + exact rfl + · obtain ⟨a1, a2, ms, e1, e2, hstep, ht, J2⟩ := maskAckRecA K J irq (by omega) + refine ⟨a1, a2, { σ with machineState := ms }, e1, e2, ?_, id, bodyMsA ht, rfl, J2⟩ + refine mem_bind_ndD (x := ((), { σ with machineState := ms })) ?_ (ackSpecA _ _) + unfold archMaskIrqSignal + simp only [v3, not_false_eq_true] + exact irqMachine_memA _ σ hstep + +set_option maxHeartbeats 2000000 in +/-- **Interrupt service at the record** (module docstring). -/ +theorem _root_.Fl4ProofsA64.P3.JStage3A.handleInterruptA (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) (K : IrqArchA ar AR0 MR) + (J : JStage3A st (withMRA AR0 MR σ.machineState) Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ + fl4A64Platform.numNodes (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (h1 : fl4A64Platform.numNodes = 1) + (hMax : kernelConfig.maxIRQ = 5375) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (irq : U64) (Ln : IrqLineA st s irq) : + interrupt.handle_interrupt st ar s a irq ⦃ r => ∃ σ', + ((), σ') ∈ (handleInterrupt (Z := Unit) (BitVec.ofNat 13 irq.val) σ).1 ∧ + JStage3QA st ar (withMRA AR0 MR σ'.machineState) Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ + fl4A64Platform.numNodes hPl.ipa m r.1 r.2 σ' ∧ + (SwA σ → SwA σ') ∧ (BodyExitA σ → BodyExitA σ') ∧ σ'.curSc = σ.curSc ⦄ := by + have hcore : m.curCore < fl4A64Platform.numNodes := by + rw [J.sr.sch.core]; exact hCs _ J.sr.sch.cpuCs + have hc0 : 0#u64 ∈ Z.Cs := by + obtain ⟨c, hc, e⟩ := J.coresAll 0 (Nat.lt_of_le_of_lt (Nat.zero_le _) hcore) + have : c = 0#u64 := UScalar.eq_of_val_eq (by rw [e]; rfl) + exact this ▸ hc + obtain ⟨r0, er, P1⟩ := spec_existsA (J.irqP1A hA hPl hc0 irq) + rw [er, WP.spec_ok] + obtain ⟨mx, emx, vmx⟩ := spec_existsA Fl4ProofsA64.P0.Consts.layout.irq.MAX.spec + have vmx' : mx.val = 5375 := by rw [vmx]; rfl + have specMax : ((BitVec.ofNat 13 irq.val : Irq) > maxIRQ) ↔ 5375 < irq.val := by + unfold maxIRQ + rw [hMax, gt_iff_lt, BitVec.lt_def, BitVec.toNat_ofNat, BitVec.toNat_ofNat, + Nat.mod_eq_of_lt (show irq.val < 2 ^ 13 from Ln.num)] + have run := er + unfold interrupt.handle_interrupt at run + rw [emx, bind_ok] at run + by_cases big : irq > mx + · -- a number above `MAX` + have hb : 5375 < irq.val := by have : mx.val < irq.val := big; omega + rw [if_pos big] at run + obtain ⟨a1, a2, ms, e1, e2, hstep, ht, J2⟩ := maskAckRecA K J irq Ln.num + rw [e1, bind_ok, e2, bind_ok] at run + obtain rfl := ok_inj_ff run + refine ⟨{ σ with machineState := ms }, ?_, J2.toQA, id, bodyMsA ht, rfl⟩ + unfold handleInterrupt + split + · exact irqMachine_memA _ σ (mem_bind_ndD (x := ((), ms)) hstep rfl) + · rename_i h + exact absurd (specMax.2 hb) h + rw [if_neg big] at run + have hi : irq.val < 5376 := by + have : ¬ mx.val < irq.val := big + omega + have ns : ¬ (BitVec.ofNat 13 irq.val : Irq) > maxIRQ := fun h => by have := specMax.1 h; omega + set w := L.mem s (irqWordA (L.ksb s) irq) with hw + have eSt : interrupt.irq_state st s irq = ok w := by + have const : layout.ks.IRQ_STATE = ok 224#u64 := + eq_of_spec_fd Fl4ProofsA64.P0.Consts.layout.ks.IRQ_STATE.spec + have off : interrupt.state_off irq = ok (irqOffA irq) := by + unfold interrupt.state_off irqOffA + simp only [lift, bind_ok, const] + unfold interrupt.irq_state + rw [off, bind_ok, eq_of_spec_fd (obj.ks_get.spec (L := L) s _)] + rfl + have sSt : σ.interruptStates (BitVec.ofNat 13 irq.val) = irqStOfA w := J.j.1.irq.st irq hi + have enter : ∀ x, irqStOfA w = x → + (x, σ) ∈ (getIrqState (Z := Unit) (BitVec.ofNat 13 irq.val) σ).1 := fun x h => by + rw [← h, ← sSt]; exact rfl + rw [eSt, bind_ok] at run + have c1 : interrupt.IRQ_SIGNAL = 1#u64 := by rw [interrupt.IRQ_SIGNAL] + have c2 : interrupt.IRQ_TIMER = 2#u64 := by rw [interrupt.IRQ_TIMER] + have c3 : interrupt.IRQ_IPI = 3#u64 := by rw [interrupt.IRQ_IPI] + have c0 : interrupt.IRQ_INACTIVE = 0#u64 := by rw [interrupt.IRQ_INACTIVE] + rw [c1, c2, c3, c0] at run + by_cases w1 : w = 1#u64 + · -- a signal line + rw [if_pos w1] at run + have est : irqStOfA w = .irqSignal := by rw [w1]; simp [irqStOfA] + obtain ⟨hAbs, eAddr, eSlot⟩ := irqSlot_relatedA (st := st) J.j.1.irq J.j.1.place hi + obtain ⟨ac, hg, hace⟩ := getCapOne_eqA (st := st) J.j.1.st hAbs + rw [eAddr] at hace + have hxO : irqSlotA (L.ksb s) irq ∈ OS := J.irqOK.1 irq hi + rw [eSlot, bind_ok, capA_eq, bind_ok] at run + -- from the slot's cap to the tail + have pre : ∀ (σ1 σ2 : State Unit), + ((), σ1) ∈ (NonDet.when (isNtfnCap ac ∧ AllowSend ∈ capRights ac) + (sendSignal (Z := Unit) (objRefOf ac) (capEpBadge ac)) σ).1 → + ((), σ2) ∈ ((archMaskIrqSignal (Z := Unit) (BitVec.ofNat 13 irq.val) >>= fun _ => + doMachineOp (ackInterrupt (BitVec.ofNat 13 irq.val))) σ1).1 → + ((), σ2) ∈ (handleInterrupt (Z := Unit) (BitVec.ofNat 13 irq.val) σ).1 := fun σ1 σ2 h1 h2 => by + unfold handleInterrupt + split + · rename_i g + exact absurd g ns + refine mem_bind_ndD (x := (IrqState.irqSignal, σ)) (enter _ est) ?_ + dsimp only + refine mem_bind_ndD (x := ((σ.interruptIrqNode (BitVec.ofNat 13 irq.val), []), σ)) rfl ?_ + refine mem_bind_ndD (x := (ac, σ)) (by rw [hg]; exact rfl) ?_ + exact mem_bind_ndD (x := ((), σ1)) h1 h2 + have quiet : ¬ (isNtfnCap ac ∧ AllowSend ∈ capRights ac) → + (do let (s1, a1) ← (do + let a3 ← ar.mask_irq a irq true + ok (s, a3)) + let a2 ← ar.ack_irq a1 irq + ok (s1, a2)) = ok r0 → + ∃ σ', ((), σ') ∈ (handleInterrupt (Z := Unit) (BitVec.ofNat 13 irq.val) σ).1 ∧ + JStage3QA st ar (withMRA AR0 MR σ'.machineState) Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ + fl4A64Platform.numNodes hPl.ipa m r0.1 r0.2 σ' ∧ + (SwA σ → SwA σ') ∧ (BodyExitA σ → BodyExitA σ') ∧ σ'.curSc = σ.curSc := fun nc run => by + obtain ⟨a1, a2, σ2, e1, e2, mem, sw, bd, cs, J2⟩ := signalTailA K J irq hi + simp only [e1, bind_ok, bind_tc_ok, uncurry_apply_pair, e2] at run + obtain rfl := ok_inj_ff run + refine ⟨σ2, pre σ σ2 ?_ mem, J2.toQA, sw, bd, cs⟩ + unfold NonDet.when + rw [if_neg nc] + exact rfl + have notN : (∀ p b0 b1 b2, capA st s (irqSlotA (L.ksb s) irq) ≠ .Notification p b0 b1 b2) → + ¬ (isNtfnCap ac ∧ AllowSend ∈ capRights ac) := fun h hc => by + rcases hace.eq_or_pt with e | ⟨_, _, _, e⟩ + · rw [e] at hc + revert h hc + generalize capA st s (irqSlotA (L.ksb s) irq) = c + intro h hc + cases c with + | Notification p b0 b1 b2 => exact h p b0 b1 b2 rfl + | _ => exact absurd hc.1 (by simp [absCapA, isNtfnCap]) + · rw [e] at hc + exact absurd hc.1 (by simp [isNtfnCap]) + generalize hcap : capA st s (irqSlotA (L.ksb s) irq) = c at run + cases c with + | Notification p badge b x => + have eac : ac = .notificationCap (specPtr p) badge.bv (rightsOfA b x false false) := by + rw [hcap] at hace + exact (show absCapA (.Notification p badge b x) = ac from hace).symm + have send : (AllowSend ∈ capRights ac) ↔ b = true := by + rw [eac]; simp [capRights, rightsOfA, AllowSend] + cases b with + | false => + refine quiet (fun h => by have := send.1 h.2; cases this) ?_ + simp only [Bool.false_eq_true, ↓reduceIte] at run + try dsimp only at run + simp only [bind_assoc_eq, bind_ok, bind_tc_ok, uncurry_apply_pair] at run + exact run + | true => + have hH : H p = some .ntfn := (J.capTy _ hxO).2.1 p badge true x hcap + have hp : p ∈ N := J.ipcHeapCov.ntfn _ hH + obtain ⟨y, hk, -⟩ := J.ntfn.ntfn p hp + have sig : notification.send_signal st ar s a p badge ⦃ r => + ∃ σ1, sendSignal (Z := Unit) (specPtr p) badge.bv σ = ({((), σ1)}, False) ∧ + (SysL st H Z.Cs OS ia ie r.1 → SymL st H Z.Cs r.1 → + BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac r.1 → + JStage3QA st ar (withMRA AR0 MR σ1.machineState) Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ + fl4A64Platform.numNodes hPl.ipa m r.1 r.2 σ1) ⦄ := by + by_cases q : QuietSigA σ y ∨ VmSigA σ y + · have still := q.elim (fun q => J.irqSignalQuietA hA hcore hmb hmax htu hp hk q badge) + (fun v => J.sigVmA hA hCs h1 hmb hmax htu hp hk v badge) + refine WP.spec_mono still ?_ + rintro r ⟨ha, x', hs, k⟩ + refine ⟨_, hs, fun sysl symL bxi => ?_⟩ + obtain ⟨Q, Rl, J1⟩ := k sysl symL bxi + rw [ha] + exact ⟨Q, Rl, Z.curOf, Z.curScOf, J1⟩ + obtain ⟨q, vm⟩ := not_or.1 q + refine WP.spec_mono (J.sigWakeA hA hCs h1 hmb hmax htu hp hk q vm badge) ?_ + rintro r ⟨ha, σ1, hs, hms, k⟩ + refine ⟨σ1, hs, fun sysl symL _ => ?_⟩ + obtain ⟨Q, Rl, J1⟩ := k sysl symL + rw [ha, hms] + exact ⟨Q, Rl, Z.curOf, Z.curScOf, J1⟩ + obtain ⟨⟨s1, a1⟩, es, σ1, hs, k⟩ := spec_existsA sig + simp only [↓reduceIte, es, bind_ok] at run + try dsimp only at run + simp only [es, bind_assoc_eq, bind_ok, bind_tc_ok, uncurry_apply_pair] at run + simp only [WP.uncurry'_pair] at k + obtain ⟨b1, eb1⟩ := spec_existsA (hA.mask_irq a1 irq true) + obtain ⟨b2, eb2⟩ := spec_existsA (hA.ack_irq b1 irq) + simp only [eb1.1, bind_ok, bind_tc_ok, uncurry_apply_pair, eb2.1] at run + have e0 : r0 = (s1, b2) := (ok_inj_ff run).symm + subst e0 + obtain ⟨Q, Rl, To, Co, J1⟩ := k P1.1 P1.2.1 P1.2.2 + obtain ⟨a2, a3, σ2, e1, e2, mem, sw, bd, cs, J2⟩ := signalTailA K J1 irq hi + have ea2 : a2 = b1 := by rw [eb1.1] at e1; exact (ok_inj_ff e1).symm + subst ea2 + have ea3 : a3 = b2 := by rw [eb2.1] at e2; exact (ok_inj_ff e2).symm + subst ea3 + have hQ : SigQ (specPtr p) σ := Sched.JStage3A.sigQA J hp + have m1 : ((), σ1) ∈ (sendSignal (Z := Unit) (specPtr p) badge.bv σ).1 := by + rw [hs]; exact Set.mem_singleton _ + refine ⟨σ2, pre σ1 σ2 ?_ mem, ⟨Q, Rl, To, Co, J2⟩, fun h => sw (sendSignal_swa _ _ σ ⟨h, hQ⟩ _ m1), + fun B => bd ?_, cs.trans (sendSignal_bkA (specPtr p) badge.bv σ _ m1).csc⟩ + · unfold NonDet.when + rw [if_pos ⟨by rw [eac]; rfl, send.2 rfl⟩, eac] + exact m1 + · have idle : IdleSt σ := fun x hx => by + obtain ⟨y, hy, hs⟩ := J.sinv.idleTcb + rw [hx] at hy + have := KernelObject.tcb.inj (Option.some.inj hy) + subst this + exact hs + exact B.signal (SchedValidA.toFIA J.schedValid) (Sched.JStage3A.rqDomPrioA J) idle hQ J.sinv.curTcb hs + | _ => + refine quiet (notN fun p b0 b1 b2 h => by rw [hcap] at h; cases h) ?_ + try dsimp only at run + simp only [bind_assoc_eq, bind_ok, bind_tc_ok, uncurry_apply_pair] at run + exact run + rw [if_neg w1] at run + by_cases w2 : w = 2#u64 + · -- a timer line + rw [if_pos w2] at run + have est : irqStOfA w = .irqTimer := by rw [w2]; simp [irqStOfA] + obtain ⟨c, ec, hc⟩ := spec_existsA (LawfulStore.cur_cpu_spec (st := st) s) + subst hc + obtain ⟨s2, e2, h2⟩ := spec_existsA (obj.node_set.spec (st := st) s (L.cpu s) layout.node.REPROGRAM 1#u64) + obtain ⟨s3, e3, h3⟩ := spec_existsA (obj.node_set.spec (st := st) s2 (L.cpu s) layout.node.ARMED 0#u64) + have es3 : s3 = twStA st s := by + subst h2 h3; unfold twStA; simp only [LawfulStore.cpu_wr] + try simp only [ec, bind_ok] at run + rw [e2, bind_ok, e3, bind_ok] at run + try simp only [bind_ok, bind_tc_ok, uncurry_apply_pair] at run + obtain ⟨b2, eb2⟩ := spec_existsA (hA.ack_irq a irq) + try simp only [eb2.1, bind_ok, bind_tc_ok, uncurry_apply_pair] at run + have e0 : r0 = (s3, b2) := (ok_inj_ff run).symm + subst e0 + have J1 := J.timerIrq_recA (es3 ▸ BXI.blk P1.2.2) (es3 ▸ P1.1) (es3 ▸ P1.2.1) + obtain ⟨a', ea', J2⟩ := ackRecA (σ := { σ with reprogramTimer := true }) K J1 irq + have : a' = b2 := by rw [eb2.1] at ea'; exact (ok_inj_ff ea').symm + subst this + rw [← es3] at J2 + refine ⟨{ σ with reprogramTimer := true }, ?_, J2.toQA, id, BodyExitA.repr, rfl⟩ + unfold handleInterrupt + split + · rename_i g + exact absurd g ns + refine mem_bind_ndD (x := (IrqState.irqTimer, σ)) (enter _ est) ?_ + dsimp only + refine mem_bind_ndD (x := ((), σ)) (irqMachine_memA _ σ (ms' := σ.machineState) rfl) ?_ + have md : ((), { σ with reprogramTimer := true }) ∈ + (NonDet.modify (fun k : State Unit => { k with reprogramTimer := true }) σ).1 := by + unfold NonDet.modify + exact rfl + exact mem_bind_ndD (x := ((), { σ with reprogramTimer := true })) md (ackSpecA _ _) + rw [if_neg w2] at run + -- a reserved line, a remote-call IPI + have w0 : w ≠ 0#u64 := Ln.active hi + have est : irqStOfA w = .irqReserved := by unfold irqStOfA; rw [if_neg w0, if_neg w1, if_neg w2] + have ack : r0.1 = s → ar.ack_irq a irq = ok r0.2 → + ∃ σ', ((), σ') ∈ (handleInterrupt (Z := Unit) (BitVec.ofNat 13 irq.val) σ).1 ∧ + JStage3QA st ar (withMRA AR0 MR σ'.machineState) Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ + fl4A64Platform.numNodes hPl.ipa m r0.1 r0.2 σ' ∧ + (SwA σ → SwA σ') ∧ (BodyExitA σ → BodyExitA σ') ∧ σ'.curSc = σ.curSc := fun es ea => by + obtain ⟨a', ea', J2⟩ := ackRecA K J irq + have : a' = r0.2 := by rw [ea] at ea'; exact (ok_inj_ff ea').symm + subst this + rw [es] + refine ⟨σ, ?_, J2.toQA, id, id, rfl⟩ + obtain ⟨nv, np⟩ := Ln.reserved hi w1 w2 + have res : ((), σ) ∈ (handleReservedIrq (Z := Unit) (BitVec.ofNat 13 irq.val) σ).1 := by + unfold handleReservedIrq NonDet.when + refine mem_bind_ndD (x := ((), σ)) ?_ ?_ <;> split + · rename_i h + exact absurd h nv + · exact rfl + · rename_i h + exact absurd np h + · exact rfl + unfold handleInterrupt + split + · rename_i g + exact absurd g ns + refine mem_bind_ndD (x := (IrqState.irqReserved, σ)) (enter _ est) ?_ + dsimp only + exact mem_bind_ndD (x := ((), σ)) res (ackSpecA _ _) + obtain ⟨b2, eb2⟩ := spec_existsA (hA.ack_irq a irq) + have done : r0 = (s, b2) → _ := fun e => ack (by rw [e]) (by rw [e]; exact eb2.1) + refine done ?_ + by_cases w3 : w = 3#u64 + · rw [if_pos w3] at run + unfold smp.handle_ipi at run + obtain ⟨i, ei⟩ := spec_existsA (P0.layout.irq.id.p0 irq) + have ni : i ≠ layout.irq.RESCHEDULE_ID := fun e => by + refine Ln.noResched hi w3 ?_ + rw [ei.1, e]; unfold layout.irq.RESCHEDULE_ID; rfl + simp only [ei.1, bind_ok, bind_tc_ok, if_neg ni, uncurry_apply_pair] at run + try simp only [eb2.1, bind_ok, bind_tc_ok, uncurry_apply_pair] at run + exact (ok_inj_ff run).symm + · rw [if_neg w3, if_neg w0] at run + simp only [bind_ok, bind_tc_ok, uncurry_apply_pair] at run + try simp only [eb2.1, bind_ok, bind_tc_ok, uncurry_apply_pair] at run + exact (ok_inj_ff run).symm + +/-- **The interrupt entry arm with its service at the record** (module docstring). -/ +theorem interrupt_serviceA (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) (K : IrqArchA ar AR0 MR) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (h1 : fl4A64Platform.numNodes = 1) + (hMax : kernelConfig.maxIRQ = 5375) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (irq : U64) (limit : Nat) (view : State Unit → Fl4TimerView) {Final : S → A → State Unit → Prop} + (budget : (do let (s1, a1) ← sched.update_timestamp st ar s a; sched.check_budget st ar s1 a1) ⦃ r => ∃ τ, + ((fl4UpdateTimestamp (Z := Unit)) >>= fun _ => fl4CheckBudget) σ = ({(r.1, τ)}, False) ∧ + JStage3QA st ar (withMRA AR0 MR τ.machineState) Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ + fl4A64Platform.numNodes hPl.ipa m r.2.1 r.2.2 τ ⦄) + (line : ∀ b s' a' τ, + (do let (s0, a0) ← sched.update_timestamp st ar s a; sched.check_budget st ar s0 a0) = ok (b, s', a') → + ((fl4UpdateTimestamp (Z := Unit)) >>= fun _ => fl4CheckBudget) σ = ({(b, τ)}, False) → + IrqLineA st s' irq) + (finish : ∀ s' a' τ, ((), τ) ∈ (fl4AcceptedIrqBody (handleInterrupt (BitVec.ofNat 13 irq.val)) σ).1 → + JStage3QA st ar (withMRA AR0 MR τ.machineState) Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ + fl4A64Platform.numNodes hPl.ipa m s' a' τ → + syscall.finish st ar s' a' ⦃ r => StepPostA Final (fl4Finish limit view) τ r ⦄) : + syscall.handle_interrupt_entry st ar s a irq ⦃ r => + StepPostA Final (fl4AcceptedIrqBody (handleInterrupt (BitVec.ofNat 13 irq.val)) >>= fun _ => + fl4Finish limit view) σ r ⦄ := by + refine interruptEntry_refinesA (Rel := fun s a σ => JStage3QA st ar (withMRA AR0 MR σ.machineState) Z H OS U Ac Pp + Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes hPl.ipa m s a σ) irq limit view + (handleInterrupt (BitVec.ofNat 13 irq.val)) budget (fun b s' a' τ eb sp R => ?_) finish + have Ln := line b s' a' τ eb sp + obtain ⟨Q, Rl, To, Co, J⟩ := R + refine WP.spec_mono (J.handleInterruptA hA hPl K hCs h1 hMax hmb hmax htu irq Ln) ?_ + rintro r ⟨σ', mem, ⟨Q', Rl', To', Co', J'⟩, -, -, -⟩ + exact ⟨σ', mem, Q', Rl', To', Co', J'⟩ + +end + +/-! ## The next entry's budget facts from the exit -/ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s : S} {a : A} {σ : State Unit} {m : McState} + +/-- **The budget entry facts at the state an exit leaves**, the current thread idle or not (module docstring). -/ +theorem _root_.Fl4ProofsA64.P3.ChargeEntryA.ofExitA + (J : JStage3EntryA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (act : σ.schedulerAction = .resumeCurThread) (hc : HcA σ) + (user : σ.curThread ≠ σ.idleThread → + JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) : + ChargeEntryA st Ac s := by + have Y := J.toJStage3A.yieldQueueA + have actW : L.actionW s = 0#u64 := by + have h := J.sr.sch.act + rw [act] at h + unfold actOfA at h + by_cases h0 : L.actionW s = 0#u64 + · exact h0 + · rw [if_neg h0] at h + by_cases h1 : L.actionW s = 1#u64 + · rw [if_pos h1] at h; cases h + · rw [if_neg h1] at h; cases h + have ht := Y.sched.curT + have hcur := Y.sched.sch.curThread + obtain ⟨x, kx, ex⟩ := Y.sched.sch.tcb (L.curW s) ht + have hx := hc x (by rw [hcur]; exact kx) + rw [ex] at hx + have nz : L.mem s (scA (L.curW s)) ≠ 0#u64 := by + intro e; rw [optScA, if_pos e] at hx; cases hx + have hs : σ.curSc = scRenA m (L.mem s (scA (L.curW s))) := by + rw [optScA, if_neg nz] at hx; exact (Option.some.inj hx).symm + have wC : L.mem s (scA (L.curW s)) ∈ Z.C := Y.sched.rest.scMem _ ht nz + have hcs : σ.curSc = scRenA m (L.cscW s) := by + have := Y.sched.sch + rw [← this.csc, this.core]; exact this.curScOfW _ this.cpuCs + have hcw : L.cscW s = L.mem s (scA (L.curW s)) := Y.sched.scp.inj _ Y.sched.cscC _ wC (hcs.symm.trans hs) + refine ⟨actW, J.curNotVmA, by rw [hcw]; exact J.toJStage3A.curSc_act actW nz, fun hr => ?_⟩ + have ni : σ.curThread ≠ σ.idleThread := fun e => by + obtain ⟨y, hy, hs⟩ := J.sinv.idleTcb + have ct : σ.curThread = specPtr (L.curW s) := J.j.1.sch.curThread + rw [← e, ct] at hy + have code := (J.ep.at_tcb J.sr.curT hy).st + rw [hs] at code + rcases hr with h | h | h <;> rw [h] at code <;> + exact absurd code (by simp [Notif.tsCode, tsCode, sched.TS_RUNNING, sched.TS_RESTART, sched.TS_RUNNING_VM]) + exact ⟨(user ni).curSc, (user ni).cur.1, (user ni).cur.2⟩ + +/-- **The same from the exit's run**: `schedule`'s tail from the fired state, then the activation. -/ +theorem _root_.Fl4ProofsA64.P3.ChargeEntryA.ofSchedExitA (limit : Nat) (timer : State Unit → Fl4TimerView) + {σ0 σ1 τ1 : State Unit} (hf : fl4TimerFires σ0 σ1) (hR : FireRecA σ1) + (h1 : ((), τ1) ∈ (SchedTail limit timer σ1).1) (h2 : ((), σ) ∈ (fl4ActivateThread (Z := Unit) τ1).1) + (J : JStage3EntryA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (user : σ.curThread ≠ σ.idleThread → + JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) : + ChargeEntryA st Ac s := + ChargeEntryA.ofExitA J (exit_spec_at limit timer hf hR h1 h2).2.1 + (exit_hc limit timer (fl4Schedule_of_tail limit timer hf h1) h2) user + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3KEntryP1A.lean b/proofs/Fl4ProofsA64/P3/Stage3KEntryP1A.lean new file mode 100644 index 000000000..f5157af3c --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3KEntryP1A.lean @@ -0,0 +1,119 @@ +/- +P3 (aarch64, A64-P3 Stage 3, kernel-entry composition, step 2): **A64 P1's `SysL` / `SymL` / `BXI` after a budget +charge and after Yield, at the record** (the former named premise `hP1` of `P3/Stage3KEntryChargeA`). + +* `JStage3A.irqPlaceA`: a `SysPlace` that needs no decode: no extra slots, the slot of IRQ 0 (an `OS` slot whose words + are state-block words, so cap-slot words). `charge_budget`'s timeout send takes a `SysPlace` and there is no invoked + slot at a budget check. +* `JStage3A.chargeP1A`: `sched.charge_budget.rpwC` (`P1/S1aCBudget`) from the record and the entry facts + `ChargeEntryA` (`P3/Stage3KEntryBudgetRecA`): `hna` from the `resume` action, `hcsc` / `CanBlock` (G3's + `JStage3A.canBlock`) for a runnable current thread only, `hcA` from the entry facts; everything else is the record's. +* `JStage3A.yieldP1A`: `syscall.handle_yield.rpw` (`P1/S1aCYield`) the same way, for the runnable current thread. +* `JSyscallA.chargeEntryA`: the entry facts from the syscall-entry record (`act`, `running`, `curSc`, `cur`). + +Named premises: [P] `P0.ArchTotal`. New module. +-/ +import Fl4ProofsA64.P1.S1aCYield +import Fl4ProofsA64.P3.Stage3KEntryBudgetRecA +import Fl4ProofsA64.P3.Stage3RsSiteA +import Fl4ProofsA64.P3.Stage3SyscallA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section rows +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J7 : JSlotA S} {nA : U64} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- **The IRQ 0 slot's placement** (module docstring). -/ +theorem JStage3A.irqPlaceA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) : + SysPlace st H Z.Cs H.frame H.replies H.ntfns OS s (Array.repeat 3#usize 0#u64) (irqSlotA (L.ksb s) 0#u64) := by + have hsrc : irqSlotA (L.ksb s) 0#u64 ∈ OS := J.irqOK.1 0#u64 (by simp) + refine ⟨J.sys.ent.heap, J.sys.ent.os, J.winOK, J.slotTy, J.core0, fun x hx h0 => ?_, hsrc, + ⟨(J.sys.ent.os.slot _ hsrc).1, fun o ho h8 => ?_⟩, J.wuApart⟩ + · simp [Array.repeat] at hx + exact absurd hx h0 + have hSg : SgOK st H.omega H.frame s := by + obtain ⟨-, -, -, h, -⟩ := J.sys.ent.bxi + exact h + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.sys.ent.ksw + have ho' : (wa 43232#u64 o).val = 43232 + o.val := by + simp [core.num.U64.wrapping_add_val_eq, U64.size_def, U64.numBits] + omega + have hv : (wa (L.ksb s) (wa 43232#u64 o)).val = (L.ksb s).val + 43232 + o.val := by + rw [core.num.U64.wrapping_add_val_eq, ho'] + simp [U64.size_def, U64.numBits] + omega + have e : wa (irqSlotA (L.ksb s) 0#u64) o = wa (L.ksb s) (wa 43232#u64 o) := by + apply UScalar.eq_of_val_eq + rw [hv] + unfold irqSlotA + simp [core.num.U64.wrapping_add_val_eq, core.num.U64.wrapping_mul_val_eq, U64.size_def, U64.numBits] + omega + have hΩ := hSg.ksΩ (wa 43232#u64 o) (by rw [ho']; omega) (by rw [ho']; omega) + rw [e] + refine Or.inr ⟨hΩ, fun hf => ?_⟩ + rcases hSg.ks _ hf with h | h <;> rw [hv] at h <;> omega + +/-- **A64 P1's invariants after `charge_budget` at the record** (module docstring). -/ +theorem JStage3A.chargeP1A (hA : P0.ArchTotal ar) + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (En : ChargeEntryA st Ac s) (used : U64) (cto : Bool) : + sched.charge_budget st ar s a used cto ⦃ r => SysL st H Z.Cs OS ia ie r.1 ∧ SymL st H Z.Cs r.1 ∧ + BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac r.1 ⦄ := by + obtain ⟨_, _, _, _, _, _, _, _, _, _, _, W⟩ := J.j7.unp + have BKh : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s := by + obtain ⟨q, hB, hX, hSg, hS⟩ := J.sys.ent.bxi + exact ⟨q, hB, hX, hS, hSg⟩ + have hna : L.actionW s ≠ L.curW s := by rw [En.act]; exact fun e => J.tcb_ne0 J.sq.curT e.symm + have hRV : RunVcpuInv st H.tcbs s := by rw [J.tcbs]; exact J.runVcpu + refine WP.spec_mono (sched.charge_budget.rpwC (Vc := Z.V) hA a used cto J.sys.sysl J.symL BKh J.irqPlaceA J.ap.sca + W.col.case.ts J.ko J.vsOk (fun r => (En.run r).1) hna En.cA + (fun r => J.canBlock En.act r (En.run r).2.1) hRV) ?_ + rintro ⟨s', a'⟩ P + simp only [WP.uncurry'_pair] at P ⊢ + obtain ⟨sysl, symL, ⟨q, hB, hX, hS, hSg⟩, -⟩ := P + exact ⟨sysl, symL, q, hB, hX, hSg, hS⟩ + +/-- **A64 P1's invariants after Yield at the record** (module docstring). -/ +theorem JStage3A.yieldP1A (hA : P0.ArchTotal ar) + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (En : ChargeEntryA st Ac s) (hrun : Fl4ProofsA64.P1.Runnable (L.mem s (stA (L.curW s)))) : + syscall.handle_yield st ar s a ⦃ r => SysL st H Z.Cs OS ia ie r.1 ∧ SymL st H Z.Cs r.1 ∧ + BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac r.1 ⦄ := by + obtain ⟨_, _, _, _, _, _, _, _, _, _, _, W⟩ := J.j7.unp + have BKh : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s := by + obtain ⟨q, hB, hX, hSg, hS⟩ := J.sys.ent.bxi + exact ⟨q, hB, hX, hS, hSg⟩ + have hna : L.actionW s ≠ L.curW s := by rw [En.act]; exact fun e => J.tcb_ne0 J.sq.curT e.symm + have hRV : RunVcpuInv st H.tcbs s := by rw [J.tcbs]; exact J.runVcpu + refine WP.spec_mono (syscall.handle_yield.rpw (Vc := Z.V) hA a J.sys.sysl J.symL BKh J.irqPlaceA J.ap.sca + W.col.case.ts J.ko J.vsOk (En.run hrun).1 hna En.cA (J.canBlock En.act hrun (En.run hrun).2.1) hRV) ?_ + rintro ⟨s', a'⟩ P + simp only [WP.uncurry'_pair] at P ⊢ + obtain ⟨sysl, symL, ⟨q, hB, hX, hS, hSg⟩, -⟩ := P + exact ⟨sysl, symL, q, hB, hX, hSg, hS⟩ + +/-- **The entry facts from the syscall-entry record** (module docstring). -/ +theorem JSyscallA.chargeEntryA {J5 : JSlotA S} + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) : + ChargeEntryA st Ac s where + act := J.actW + nvm := J.toJStage3EntryA.curNotVmA + cA := by rw [J.curSc J.cur.2]; exact J.toJStage3A.curSc_act J.actW J.cur.2 + run := fun _ => ⟨J.curSc, J.cur.1, J.cur.2⟩ + +end rows + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3KEntryRestartA.lean b/proofs/Fl4ProofsA64/P3/Stage3KEntryRestartA.lean new file mode 100644 index 000000000..4cd395846 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3KEntryRestartA.lean @@ -0,0 +1,255 @@ +/- +P3 (aarch64, A64-P3 Stage 3, kernel-entry composition, step 2): **`check_budget_restart` at the record on an +exhausted head refill without the timeout send** (the budget branch of the syscall and fault entries). + +* `JStage3A.curSchedulableA`: the runnable current thread of an entry is schedulable on the spec side. +* `chargeSteps_chooseA`: the charge of a schedulable current thread leaves the action `chooseNewThread`. +* `JStage3A.of_restartQueuedCurA`: the Restart write of the current thread when the action is not `resume`, whatever + queue the thread is in (after postpone it is in the release queue, so `JStage3A.of_restartCurA`, which needs + `Q_RELEASE = 0`, does not apply): `schedule_tcb` reads only the action, the spec's `set_thread_state_act` does + nothing. +* `JStage3A.checkBudgetRestartNoSendA`, `kentryChargeRestartA`: `KEntryChargeA` at `check_budget_restart`. + +Named premises: those of `kentryChargeIrqA` (`P3/Stage3KEntryChargeA`). New module. +-/ +import Fl4ProofsA64.P3.Stage3KEntryChargeA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 Fl4Spec Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-- **The charge of a schedulable current thread leaves the action `chooseNewThread`**: `ChargeStepsA`'s other branch +(an unschedulable current thread) contradicts the kept TCB, SC activity and release queue. -/ +theorem chargeSteps_chooseA {σ τ : State Unit} (CS : ChargeStepsA σ τ) (K : ScChargeKeepA σ τ) + (hsch : schedulable σ.curThread σ = true) : τ.schedulerAction = .chooseNewThread := by + obtain ⟨y, n, -, h, -⟩ := CS + rcases h with ⟨-, σ3, -, hres⟩ | ⟨hf, e⟩ + · have hmem : ((), τ) ∈ (reschedReprogA σ3).1 := by + rw [hres]; exact Set.mem_singleton _ + unfold reschedReprogA at hmem + obtain ⟨⟨u4, σ4⟩, h4, h5⟩ := mem_bindA.1 hmem + obtain ⟨-, ha4⟩ := rescheduleRequired_memA h4 + have eτ : τ = { σ4 with reprogramTimer := true } := + (Prod.mk.inj (show (((), τ) : Unit × State Unit) = ((), { σ4 with reprogramTimer := true }) from h5)).2 + rw [eτ] + exact ha4 + · exfalso + obtain ⟨⟨x, sc, hk, hs, act⟩, nrel, x', hk', hr⟩ := schedulable_actA hsch + rw [hk] at hk' + obtain rfl := KernelObject.tcb.inj (Option.some.inj hk') + obtain ⟨z, k, hz, hz2⟩ := chargeActA K act + have hkτ := K.2 _ x hk + have hrel : σ.curThread ∉ τ.releaseQueue := by rw [e]; exact nrel + have ht : schedulable σ.curThread τ = true := by + refine schedulable_ofA hkτ hr hs hz ?_ hrel + simpa [scActive, activeSc] using hz2 + rw [← e, ht] at hf + cases hf + +section rec +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s : S} {a : A} {σ : State Unit} {m : McState} + +/-- **The runnable current thread of an entry is schedulable** (module docstring). -/ +theorem JStage3A.curSchedulableA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (En : ChargeEntryA st Ac s) (hrun : Fl4ProofsA64.P1.Runnable (L.mem s (stA (L.curW s)))) : + schedulable σ.curThread σ = true := by + have q := J.sq + set t := L.curW s with ht_def + have hct : t ∈ H.tcbs := q.curT + have htZ : t ∈ Z.T := J.sr.curT + have cT : σ.curThread = specPtr t := J.j.1.sch.curThread + obtain ⟨x, hk, -, hscx⟩ := J.j.1.sch.tcb t htZ + have hr : runnable x.tcbState = true := (runnable_iffA (J.ep.at_tcb htZ hk).st).1 hrun + obtain ⟨-, hrel, hsc0⟩ := En.run hrun + rw [← ht_def] at hrel hsc0 + set w := L.mem s (scA t) with hw + have hwC : w ∈ Z.C := J.j.1.rest.scMem t htZ hsc0 + have hwH : w ∈ H.scs := by rw [J.scs]; exact hwC + have hrm : L.rmax s w ≠ 0#u64 := by + obtain ⟨-, -, -, -, hS⟩ := J.sys.ent.bxi + exact (hS.act w hwH).1 (J.curSc_act En.act hsc0) + have hq : specPtr t ∉ σ.releaseQueue := by + rw [J.j.1.rest.rel] + intro hm + obtain ⟨r, hr, er⟩ := List.mem_map.1 hm + have e : r = t := specPtr_inj er + rw [e] at hr + have haf := q.inR _ q.cur _ hr + exact ((q.released _ hct).2 (by rw [haf]; exact hr)) hrel + obtain ⟨y, k, ey, eyr⟩ := J.j.1.rest.refillMax w hwC + rw [cT] + refine schedulable_ofA hk hr (sc := scRenA m w) (by rw [hscx]; unfold optScA; simp [← hw, hsc0]) ey ?_ hq + rw [eyr] + have e2 : L.rmax s w = L.mem s (wa w 72#u64) := by unfold LawfulStore.rmax; rw [maxA_eqA] + rw [e2] at hrm + exact Nat.pos_of_ne_zero (fun e => hrm (UScalar.eq_of_val_eq (by simpa using e))) + +/-- **`JStage3A` across the Restart write of the current thread when the action is not `resume`** (module +docstring). -/ +theorem JStage3A.of_restartQueuedCurA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (hrun : Fl4ProofsA64.P1.Runnable (L.mem s (stA (L.curW s)))) + (hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM) (hna : σ.schedulerAction ≠ .resumeCurThread) : + ∃ x, σ.kheap σ.curThread = some (.tcb x) ∧ + sched.set_thread_state st s (L.curW s) sched.TS_RESTART = ok (L.wr s (stA (L.curW s)) sched.TS_RESTART) ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) + (L.wr s (stA (L.curW s)) sched.TS_RESTART) a (stUpdA σ σ.curThread x .restart) m ∧ + SymL st H Z.Cs (L.wr s (stA (L.curW s)) sched.TS_RESTART) ∧ + OSKeep st OS s (L.wr s (stA (L.curW s)) sched.TS_RESTART) ∧ + (∀ y, SlotWA Z.th y → capAtA (L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART)) y = capAtA (L.mem s) y) := by + have q := J.sq + have htZ : L.curW s ∈ Z.T := J.sr.curT + have hta : TcbAddr (L.curW s) := J.sr.core.tcbs _ htZ + have sA : Aligned (stA (L.curW s)) := by rw [stA_eq]; exact field_aligned hta (by simp) (by simp) + have hact : L.actionW s ≠ 0#u64 := fun e => by + have h := J.sr.sch.act + rw [e] at h + unfold actOfA at h + rw [if_pos rfl] at h + exact hna (by first | exact h | exact h.symm) + have hN := q.nodes _ q.cur + have o1 : layout.node.SCHED_ACTION.val < 4288 := by rw [act_val]; omega + have eA : L.actionW (L.wr s (stA (L.curW s)) sched.TS_RESTART) = L.actionW s := by + unfold LawfulStore.actionW + rw [L.cpu_wr, nodeAt_of_ksb (L.ksb_wr _ _ _)] + refine mem_wr_ne' sA (nodeAt_aligned hN o1 (by rw [act_val])) (fun e => ?_) + rw [stA_eq] at e + exact node_ne_field hN o1 q.curT hta (K := 3328#u64) (by simp) e.symm + have heq : sched.set_thread_state st s (L.curW s) sched.TS_RESTART = + ok (L.wr s (stA (L.curW s)) sched.TS_RESTART) := by + unfold sched.set_thread_state + rw [eq_of_spec_fd (obj.Tcb.set_state.spec (L := L) s _ _)] + simp only [bind_ok] + unfold sched.schedule_tcb + rw [eq_of_spec_fd (sched.cur_thread.spec (L := L) _)] + simp only [bind_ok] + rw [eq_of_spec_fd (obj.Tcb.eq.spec _ _)] + simp only [bind_ok] + split + · rw [eq_of_spec_fd (sched.action.spec (L := L) _)] + simp only [bind_ok] + rw [eA, if_neg (by unfold sched.ACTION_RESUME_CURRENT; exact hact)] + · rfl + exact J.of_restartCoreA hrun hnvm J.rsVcA (J.toJEntA.restartCoreA J.irqPlaceA J.symL hrun J.rsVsA J.rsVcA heq + (fun x hk => setThreadState_eqA σ _ x hk .restart (fun _ h => absurd h hna))) + +set_option maxHeartbeats 2000000 in +/-- **`check_budget_restart` at the record on an exhausted head refill without the timeout send** (module +docstring). -/ +theorem JStage3A.checkBudgetRestartNoSendA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar nA)) s a σ m) + (noSwitch : ∀ u, σ.schedulerAction ≠ .switchThread u) (I : CurInvA σ) (Idle : ActIdleA σ) + (hcore : L.mem s (scCoreA (L.cscW s)) = L.cpu s) (hni : σ.curSc ≠ idleScPtr → NotIdleScA m σ.curSc) + {Clock : A → MachineState → Prop} (clk : Clock a σ.machineState) + (T : EntryValidFromA (st := st) Z m κ Clock s σ) + (hag : fl4MinBudget = MIN_BUDGET) (hMin : Fl4MinBudgetAgreeA ar a) + (hLimit : sporadic.max_release_time ar a ⦃ v => v.bv = fl4MaxReleaseTime ⦄) + (hex : fl4GetScRefillSufficient (Z := Unit) σ.curSc σ.consumedTime σ = ({(false, σ)}, False)) + (hns : NoTimeoutA σ.consumedTime σ) (hA : P0.ArchTotal ar) (En : ChargeEntryA st Ac s) : + sched.check_budget_restart st ar s a ⦃ r => ∃ τ, fl4CheckBudgetRestart (Z := Unit) σ = ({(r.1, τ)}, False) ∧ + JStage3QA st ar AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes nA m + r.2.1 r.2.2 τ ⦄ := by + unfold sched.check_budget_restart + refine WP.spec_bind (J.checkBudgetNoSendA noSwitch I Idle hcore hni clk T hag hMin hLimit hex hns hA En) ?_ + rintro ⟨r, s1, a1⟩ ⟨τ, h, ⟨Q, Rl, co, cs, J1⟩, hr, SH, CS, KS⟩ + dsimp only at h J1 hr + subst hr + simp only [uncurry_apply_pair] + refine WP.spec_bind (sched.cur_thread.spec s1) ?_ + intro t ht + change t = L.curW s1 at ht + subst ht + simp only [Bool.false_eq_true, ↓reduceIte] + have cT : σ.curThread = specPtr (L.curW s) := J.j.1.sch.curThread + have cT1 : τ.curThread = specPtr (L.curW s1) := J1.j.1.sch.curThread + obtain ⟨x, hk, -, -⟩ := J.j.1.sch.tcb _ J.sr.curT + have hkσ : σ.kheap σ.curThread = some (.tcb x) := by rw [cT]; exact hk + have hkτ : τ.kheap τ.curThread = some (.tcb x) := by rw [SH.cur]; exact KS.2 _ x hkσ + have c0 := (J.ep.at_tcb J.sr.curT hk).st + have c1 := (J1.ep.at_tcb J1.sr.curT (x := x) (by rw [← cT1]; exact hkτ)).st + have spec : ∀ {τ' : State Unit}, (NonDet.when (¬(false = true) ∧ runnable x.tcbState = true) + (setThreadState (Z := Unit) τ.curThread .restart)) τ = ({((), τ')}, False) → + fl4CheckBudgetRestart (Z := Unit) σ = ({(false, τ')}, False) := by + intro τ' hw + unfold fl4CheckBudgetRestart + rw [nd_bind_oneSc h, nd_gets_bindSc, nd_bind_oneSc (show getThreadState (Z := Unit) τ.curThread τ = + ({(x.tcbState, τ)}, False) from threadGet_eqSc _ hkτ), nd_bind_oneSc hw] + rfl + rw [eq_of_spec_fd (sched.is_runnable.spec (L := L) s1 (L.curW s1))] + simp only [bind_ok] + by_cases hrun : Fl4ProofsA64.P1.Runnable (L.mem s1 (stA (L.curW s1))) + · have hrx : runnable x.tcbState = true := (runnable_iffA c1).1 hrun + have hrun0 : Fl4ProofsA64.P1.Runnable (L.mem s (stA (L.curW s))) := (runnable_iffA c0).2 hrx + have hch := chargeSteps_chooseA CS KS (J.curSchedulableA En hrun0) + have hnvm1 : L.mem s1 (stA (L.curW s1)) ≠ sched.TS_RUNNING_VM := fun e => + En.nvm (UScalar.eq_of_val_eq (by rw [c0, ← c1, e])) + obtain ⟨y, -, heq, hset, J2, -⟩ := J1.of_restartQueuedCurA hrun hnvm1 (by rw [hch]; intro h; cases h) + rw [decide_eq_true (show _ ∨ _ ∨ _ from hrun)] + simp only [↓reduceIte] + rw [heq] + simp only [bind_ok, WP.spec_ok] + refine ⟨_, spec (τ' := stUpdA τ τ.curThread y .restart) ?_, Q, Rl, co, cs, J2⟩ + unfold NonDet.when + rw [if_pos ⟨by simp, hrx⟩] + exact hset + · have hrx : ¬ runnable x.tcbState = true := fun e => hrun ((runnable_iffA c1).2 e) + rw [decide_eq_false (show ¬ (_ ∨ _ ∨ _) from hrun)] + simp only [Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + refine ⟨τ, spec ?_, Q, Rl, co, cs, J1⟩ + unfold NonDet.when + rw [if_neg (fun e => hrx e.2)] + rfl + +set_option maxHeartbeats 1000000 in +/-- **The syscall and fault entries' exhausted-budget branch at the record** (`KEntryChargeA` at +`check_budget_restart`, the `hCharge` of the arms of `P3/Stage3KEntryArmsA`): the no-send branches by +`JStage3A.checkBudgetRestartNoSendA`; the timeout send is the named premise `hSend`. + +Named premises as `kentryChargeIrqA`: [J] `hSend`, `T`, `hni`; [P] `hA`, `hCs`, `one`, `hag`, `hmb`, +`hLimit`. -/ +theorem kentryChargeRestartA {AR' : A → HwRootA → Prop} {Clock : A → MachineState → Prop} + (noSwitch : ∀ u, σ.schedulerAction ≠ .switchThread u) (hni : σ.curSc ≠ idleScPtr → NotIdleScA m σ.curSc) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (one : fl4A64Platform.numNodes = 1) + (hag : fl4MinBudget = MIN_BUDGET) (hmb : ∀ a', Fl4MinBudgetAgreeA ar a') + (hLimit : ∀ a', sporadic.max_release_time ar a' ⦃ v => v.bv = fl4MaxReleaseTime ⦄) + (T : ∀ s1 a1 σ1, JStage3A st AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar nA)) s1 a1 σ1 m → EntryValidFromA (st := st) Z m κ Clock s1 σ1) + (hA : P0.ArchTotal ar) + (hSend : ∀ s1 a1 σ1, fl4UpdateTimestamp (Z := Unit) σ = ({((), σ1)}, False) → + JStage3A st AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar nA)) s1 a1 σ1 m → + ChargeEntryA st Ac s1 → CurInvA σ1 → ActIdleA σ1 → Clock a1 σ1.machineState → + fl4GetScRefillSufficient (Z := Unit) σ1.curSc σ1.consumedTime σ1 = ({(false, σ1)}, False) → + ¬ NoTimeoutA σ1.consumedTime σ1 → + sched.check_budget_restart st ar s1 a1 ⦃ r => ∃ τ, fl4CheckBudgetRestart (Z := Unit) σ1 = ({(r.1, τ)}, False) ∧ + JStage3QA st ar AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes nA m + r.2.1 r.2.2 τ ⦄) : + KEntryChargeA st ar AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ nA m Clock s a σ + (sched.check_budget_restart st ar) fl4CheckBudgetRestart := by + intro s1 a1 σ1 e1 J1 En1 V1 CI1 R1 Act1 Cl1 hex + have eσ1 : σ1 = fl4TimestampState (rawNextA σ.machineState) { σ with machineState := rawStateA σ.machineState } := + (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst ((fl4UpdateTimestamp_eqA σ).symm.trans e1)))).2.symm + have sw1 : ∀ u, σ1.schedulerAction ≠ .switchThread u := by rw [eσ1]; exact noSwitch + have ni1 : σ1.curSc ≠ idleScPtr → NotIdleScA m σ1.curSc := by rw [eσ1]; exact hni + by_cases hns : NoTimeoutA σ1.consumedTime σ1 + · exact J1.checkBudgetRestartNoSendA sw1 CI1 Act1 (J1.curScOnCoreA hCs one) ni1 Cl1 (T s1 a1 σ1 J1) hag (hmb a1) + (hLimit a1) hex hns hA En1 + · exact hSend s1 a1 σ1 e1 J1 En1 CI1 Act1 Cl1 hex hns + +end rec + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3KEntryRootA.lean b/proofs/Fl4ProofsA64/P3/Stage3KEntryRootA.lean new file mode 100644 index 000000000..1c005c2cd --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3KEntryRootA.lean @@ -0,0 +1,199 @@ +/- +P3 (aarch64, A64-P3 Stage 3, kernel-entry composition, Plan A): **the selector's root premises from the record**. + +* `JStage3A.vsRootOK`: `VsRootOKA` at a record state (root caps from `JointA.pml` and the VSpace relation, the + VCPU links from `VcpuCoreA` and the record's VCPU symmetry words). +* `JStage3A.idleVt`: `IdleVtA` from the record and the two facts it does not give: the idle thread's vtable + slot 1 is null and it has no bound VCPU (record edit #8b `SIdleA`). + +No new premise; new module. +-/ +import Fl4ProofsA64.P3.Stage3KEntryFiredA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3 + Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +theorem JStage3A.vsRootOK {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J5 J7 : JSlotA S} {s : S} {a : A} + {σ : State Unit} {m : McState} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) : VsRootOKA σ := by + have jz : JAtZA (L := L) AR Z s a σ m := J.j.1 + have vc : VcpuCoreA (L := L) Z s σ := J.j.2.1 + -- every spec TCB is a related FL4 one + have cover : ∀ t x, σ.kheap t = some (.tcb x) → + ∃ u ∈ Z.T, specPtr u = t ∧ Z.th u = some .tcb ∧ u ≠ 0#u64 := by + intro t x hx + have th := J.finCov.tcb t x hx + have mem := (jz.thT _).1 th + refine ⟨fl4Ptr t, mem, specPtr_fl4Ptr t, th, fun z => ?_⟩ + have hm : fl4Ptr t ∈ H.tcbs := by rw [J.tcbs]; exact mem + have w := (J.heapWin _ _ hm).1 + rw [z] at w + exact absurd w (by simp [layout.PPTR_BASE]) + have words : ∀ u ∈ Z.T, ∀ x, σ.kheap (specPtr u) = some (.tcb x) → + x.tcbArch.tcbVcpu = optRefA (L.mem s (wa u 3664#u64)) ∧ + x.tcbExt.tcbRunVcpu = optRefA (L.mem s (runVcpuA u)) := by + intro u hu x hx + obtain ⟨y, hy, h1, h2⟩ := vc.tcb u hu + rw [hx] at hy + obtain rfl := KernelObject.tcb.inj (Option.some.inj hy) + exact ⟨h1, h2⟩ + have opt : ∀ (w : U64) p, optRefA w = some p → w ≠ 0#u64 ∧ p = specPtr w := by + intro w p h + unfold optRefA at h + split at h + · cases h + · rename_i ne + exact ⟨ne, (Option.some.inj h).symm⟩ + have some' : ∀ w : U64, w ≠ 0#u64 → optRefA w = some (specPtr w) := fun w ne => by + unfold optRefA + rw [if_neg ne] + have vobj : ∀ w ∈ Z.V, ∃ y, vcpusOf σ (specPtr w) = some y ∧ y.vcpuTcb = optRefA (L.mem s w) ∧ + y.vcpuExt.runCaller = optRefA (L.mem s (wa w 24#u64)) := by + intro w hw + obtain ⟨y, hy, h1, h2⟩ := vc.obj w hw + refine ⟨y, ?_, h1, h2⟩ + simp [vcpusOf, aobjsOf, optMap, hy, aobjOf, vcpuOf] + refine ⟨fun t root asid va h1 h2 => ?_, fun t asid h => ?_, fun t v h => ?_, fun t v g _ h2 => ?_⟩ + · have tcb : ∃ x, σ.kheap t = some (.tcb x) := by + unfold vtCapA at h1 + split at h1 + · rename_i x e + exact ⟨x, e⟩ + · cases h1 + obtain ⟨x, hx⟩ := tcb + obtain ⟨u, -, eu, th, -⟩ := cover t x hx + obtain ⟨y, hy, slots⟩ := jz.st.tcb u th + rw [eu, hx] at hy + obtain rfl := KernelObject.tcb.inj (Option.some.inj hy) + obtain ⟨ac, e, rel⟩ := slots 1 (by decide) + rw [vtCapA_of hx] at h1 + rw [h1] at e + obtain rfl := Option.some.inj e + have slot : IsSlotA Z.th (slotAddrA (specPtr u) (BitVec.ofNat 64 1)) := + ⟨u, Or.inr ⟨th, 1, by decide, rfl⟩⟩ + rcases rel.2.eq_or_pt with eq | ⟨p', asid', va', bad⟩ + swap + · injection bad with bad + injection bad with _ bad + cases bad + have pml4 : ∃ p mp asid', capAtA (L.mem s) (slotAddrA (specPtr u) (BitVec.ofNat 64 1)) = + .Pml4 p mp asid' ∧ root = specPtr p := by + revert eq + generalize capAtA (L.mem s) (slotAddrA (specPtr u) (BitVec.ofNat 64 1)) = c + intro eq + cases c <;> simp [absCapA] at eq + case Pml4 p mp asid' => exact ⟨p, mp, asid', rfl, eq.1⟩ + case Frame => + unfold absFrameE at eq + split at eq <;> cases eq + obtain ⟨p, mp, asid', hc, er⟩ := pml4 + obtain ⟨s1, -⟩ := jz.pml _ slot p mp asid' hc + obtain ⟨table, ht, -⟩ := jz.vs.pt p s1 + unfold rootKindAt extObjAt + rw [er, ht] + · intro z + subst z + unfold vmRootA at h + split at h + · split at h + · rename_i found + injection h with e + subst e + rw [show (0 : Asid) = BitVec.ofNat 16 0 from rfl, vspaceForAsid_zeroA] at found + cases found + · cases h + · cases h + · have tcb : ∃ x, σ.kheap t = some (.tcb x) ∧ x.tcbArch.tcbVcpu = some v := by + unfold vcpuOfA at h + split at h + · rename_i x e + exact ⟨x, e, h⟩ + · cases h + obtain ⟨x, hx, hv⟩ := tcb + obtain ⟨u, hu, eu, -, u0⟩ := cover t x hx + rw [(words u hu x (by rw [eu]; exact hx)).1] at hv + obtain ⟨ne, ev⟩ := opt _ _ hv + have mem : L.mem s (wa u 3664#u64) ∈ Z.V := (J.vcRun.tcb u hu).resolve_left ne + obtain ⟨y, hy, ytcb, -⟩ := vobj _ mem + have back : L.mem s (L.mem s (wa u 3664#u64)) = u := J.vcSym1 u hu ne + refine ⟨y, by rw [ev]; exact hy, ?_⟩ + rw [ytcb, back, some' u u0, eu] + · unfold runGuestA at h2 + cases hk : σ.kheap t with + | none => rw [hk] at h2; cases h2 + | some o => + rw [hk] at h2 + cases o with + | tcb x => + obtain ⟨u, hu, eu, -, u0⟩ := cover t x hk + have wd := words u hu x (by rw [eu]; exact hk) + cases hr : x.tcbExt.tcbRunVcpu with + | none => simp [hr] at h2 + | some v' => + rw [wd.2] at hr + obtain ⟨ne, ev⟩ := opt _ _ hr + have mem : L.mem s (runVcpuA u) ∈ Z.V := (J.vcRun.run u hu).resolve_left ne + obtain ⟨y, hy, ytcb, ycall⟩ := vobj _ mem + have hr' : x.tcbExt.tcbRunVcpu = some (specPtr (L.mem s (runVcpuA u))) := by + rw [wd.2]; exact some' _ ne + simp only [Option.bind_eq_bind, Option.bind_some, hr', hy] at h2 + cases hg : y.vcpuTcb with + | none => simp [hg] at h2 + | some g' => + simp only [hg, Option.bind_some, Option.pure_def, Option.some.injEq, Prod.mk.injEq] at h2 + obtain ⟨rfl, rfl⟩ := h2 + rw [ytcb] at hg + obtain ⟨gne, eg⟩ := opt _ _ hg + have gT : L.mem s (L.mem s (runVcpuA u)) ∈ Z.T := (J.vcRun.vtcb _ mem).resolve_left gne + have caller : L.mem s (wa (L.mem s (runVcpuA u)) 24#u64) = u := J.runBack u hu ne + refine ⟨y, hy, ?_, ?_⟩ + · rw [ycall, caller, some' u u0, eu] + · obtain ⟨z, hz, -, -⟩ := vc.tcb _ gT + have back : L.mem s (wa (L.mem s (L.mem s (runVcpuA u))) 3664#u64) = L.mem s (runVcpuA u) := + J.vcBack _ mem gne + rw [eg, vcpuOfA_of hz, (words _ gT z hz).1, back, some' _ ne] + | _ => cases h2 + +/-- `IdleVtA` from the record and the two facts it does not give. -/ +theorem JStage3A.idleVtOf {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J5 J7 : JSlotA S} {s : S} {a : A} + {σ : State Unit} {m : McState} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (slot : vtCapA σ σ.idleThread = some .nullCap) (bound : vcpuOfA σ σ.idleThread = none) : IdleVtA σ := by + obtain ⟨x, hx, hs⟩ := J.sinv.idleTcb + refine ⟨⟨x, hx, hs⟩, slot, bound, ?_⟩ + have th := J.finCov.tcb _ x hx + have mem := (J.j.1.thT _).1 th + obtain ⟨y, hy, -, hr⟩ := (J.j.2.1 : VcpuCoreA (L := L) Z s σ).tcb _ mem + rw [specPtr_fl4Ptr, hx] at hy + obtain rfl := KernelObject.tcb.inj (Option.some.inj hy) + have nr : x.tcbExt.tcbRunVcpu = none := by + rw [hr] + have zero : L.mem s (runVcpuA (fl4Ptr σ.idleThread)) = 0#u64 := by + refine J.runVcpu.zero mem fun running => ?_ + obtain ⟨z, hz, code, -⟩ := (J.j.1 : JAtZA (L := L) AR Z s a σ m).sch.tcb _ mem + rw [specPtr_fl4Ptr, hx] at hz + obtain rfl := KernelObject.tcb.inj (Option.some.inj hz) + rw [running, hs] at code + simp [sched.TS_RUNNING_VM, tsCodeA] at code + unfold optRefA + rw [if_pos zero] + unfold runGuestA + rw [hx] + simp [nr] + +end +end +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3KEntrySchedA.lean b/proofs/Fl4ProofsA64/P3/Stage3KEntrySchedA.lean new file mode 100644 index 000000000..b688950fb --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3KEntrySchedA.lean @@ -0,0 +1,921 @@ +/- +P3 (aarch64, A64-P3 Stage 3, kernel-entry composition, Plan A, item 1): **the record across `schedule`**. + +`JStage3A.acrossSchedA`: from the record at the entry (for A64 P1's walk of `schedule`), the record and `FireRecA` at +the post-fire state `σ1`, the schedule tail `σ1 → τ` with its footprint (`MFrA`, `SchedFrameA`, `schedule_exitA`) and +`YieldQueueRelA` at `τ`, the record holds at `τ`, at a hardware root related to the new arch state. +The schedule may advance the domain (`next_domain`): the record's `domOK` at `τ` is the refinement's `NdOKA` +(`schedule_exitA`), and the record step is taken over the write set widened by the domain-schedule words `DomW` +(`JStage3A.of_schedWrD`, `j57_stepDA`, `ent_stepDA`). + +Named premises: +* [P] `ArRootKeepA ar AR` (`scheduleRecA`): the root writes, `set_deadline` and `send_reschedule` keep the arch + state `AR`-related to some hardware root (`AR` is the platform's abstract relation). +* [J] `hC` (the weakened `ct_not_in_q` at the post-fire record, `JStage3A.sinv.ctQ` since record edit #8a). + +New module. +-/ +import Fl4ProofsA64.P3.Stage3IdleSchedA +import Fl4ProofsA64.P3.Stage3SchedTailQA +import Fl4ProofsA64.P3.Stage3KEntryFinishA +import Fl4ProofsA64.P3.Stage3SchedBindA +import Fl4ProofsA64.P3.Stage3SchedCtQA +import Fl4ProofsA64.P3.IpcScGlueA +import Fl4ProofsA64.P3.DelPoolKeepA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3 + Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.OuterEntry Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +/-- **The scheduler inputs at the fired state**: `ScheduleInputsA`'s `selection` / `tail` asked only at states +carrying the fire step's predicate `FF`, together with the frame of the release walk (and of the selection). -/ +structure ScheduleFiredInputsA (Z : XSetsA) (m : McState) (κ : U64 → U64) (s : S) (a : A) + (σ : State Unit) (ar : machine.Arch A) (timer : A → Nat → DeadlineViewA) (view : State Unit → Fl4TimerView) + (AR : A → HwRootA → Prop) (FF : S → S → State Unit → State Unit → Prop) : Prop where + oneDomain : numDomains = 1 + minAgree : fl4MinBudget = MIN_BUDGET + selection : ∀ (r : S × A) s1 σ1 σ2 Q R, + FF s s1 σ σ1 → sched.awaken st ar s a = ok r → + fl4TimerFires σ σ1 → awaken (Z := Unit) σ1 = ({((), σ2)}, False) → + YieldQueueRelA (st := st) {Z with Q := Q, R := R} m κ r.1 σ2 → + MFrA (st := st) Z m s1 r.1 σ1 σ2 → + ScheduleSelectionInputsA (st := st) {Z with Q := Q, R := R} m κ r.1 r.2 σ2 AR + tail : ∀ (start selected : S × A) s1 σ1 σ2 σ3 Q R Q', + FF s s1 σ σ1 → sched.awaken st ar s a = ok start → + scheduleSelectActionA (st := st) (ar := ar) start.1 start.2 = ok selected → + fl4TimerFires σ σ1 → awaken (Z := Unit) σ1 = ({((), σ2)}, False) → + MFrA (st := st) Z m s1 start.1 σ1 σ2 → + fl4SelectAction layout.NUM_DOMAIN_SCHEDULES.val σ2 = ({((), σ3)}, False) → + MFrDA (st := st) {Z with Q := Q, R := R} m start.1 selected.1 σ2 σ3 → + YieldQueueRelA (st := st) {Z with Q := Q', R := R, curOf := Function.update Z.curOf m.curCore σ3.curThread} + m κ selected.1 σ3 → + ScheduleTailInputsA (st := st) {Z with Q := Q', R := R, curOf := Function.update Z.curOf m.curCore σ3.curThread} + m κ selected.1 selected.2 σ3 ar timer view + +/-- **The scheduler with its post-fire state, footprint and spec frame**, from the inputs at the fired state: +`schedule_tailMemA` and `schedule_stepA` together, and the other cores' current threads are kept. -/ +theorem schedule_exitFA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {σ : State Unit} {a : A} {tm : A → Nat → DeadlineViewA} + {AR : A → HwRootA → Prop} (P : ArchSwFA st ar) (view : State Unit → Fl4TimerView) + {FF : S → S → State Unit → State Unit → Prop} + (inputs : ScheduleFiredInputsA (st := st) Z m κ s a σ ar tm view AR FF) + (fireF : timer.fire st ar s a ⦃ r => ∃ σ1 Q R, + fl4TimerFires σ σ1 ∧ YieldQueueRelA (st := st) {Z with Q := Q, R := R} m κ r.1 σ1 ∧ IdleThreadsA m σ1 ∧ + FF s r.1 σ σ1 ⦄) : + sched.schedule st ar s a ⦃ r => ∃ s1 σ1 τ Q R Tcur Ccur, + FF s s1 σ σ1 ∧ fl4TimerFires σ σ1 ∧ ((), τ) ∈ (SchedTail kLimitA view σ1).1 ∧ + YieldQueueRelA (st := st) {Z with Q := Q, R := R, curOf := Tcur, curScOf := Ccur} m κ r.1 τ ∧ + MFrDA (st := st) Z m s1 r.1 σ1 τ ∧ SchedFrameA σ1 τ ∧ (∀ k, k ≠ m.curCore → Tcur k = Z.curOf k) ∧ + (∀ Q' R', SchedSRN st {Z with Q := Q', R := R'} m κ s1 σ1 → NdOKA (st := st) s1 σ1 → + NdOKA (st := st) r.1 τ) ⦄ := by + rw [schedule_phasesA] + refine WP.spec_bind (reachedResultA (awaken_stepA fireF)) ?_ + rintro ⟨sA, aA⟩ ⟨wake, sF, σ1, σ2, Q, R, FFh, fires, waking, wakeRel, Fa, Sa⟩ + simp only [uncurry_apply_pair] + have steps := inputs.selection (sA, aA) sF σ1 σ2 Q R FFh wake fires waking wakeRel Fa + refine WP.spec_bind (reachedResultA (select_action_refinesA wakeRel P steps.action steps.domain steps.roots)) ?_ + rintro ⟨s2, a2⟩ ⟨selected, σ3, Q', _, selection, _, H2, frame, ndSel⟩ + simp only [uncurry_apply_pair] + have ready := inputs.tail (sA, aA) (s2, a2) sF σ1 σ2 σ3 Q R Q' FFh wake selected fires waking Fa selection frame H2 + refine WP.spec_mono (schedule_tail_refinesA H2 view inputs.oneDomain inputs.minAgree ready) ?_ + rintro r ⟨τ, C, tail, H3, Ftail⟩ + have ndTail : ∀ Q'' R'', SchedSRN st {Z with Q := Q'', R := R''} m κ sF σ1 → NdOKA (st := st) sF σ1 → + NdOKA (st := st) r.1 τ := fun _ _ HF N0 => by + have NA : NdOKA (st := st) sA σ2 := + N0.keepS HF (domKeep_ofQA _ waking) Fa.wr.1 Fa.wr.2.1 fun b hb hd => Fa.wr.2.2 b hb fun h => by + refine stepW_domA HF (Or.inl ?_) hd + exact h + have D : DomKeepA σ3 τ := + ((inferInstance : QInst (setSchedulerAction (Z := Unit) .resumeCurThread >>= fun _ => fl4ScAndTimer view)).pres + σ3 _ tail).dom + exact NdOKA.keepF (st := st) (ndSel NA) D H2.sched.toSchedSRN Ftail + refine ⟨sF, σ1, τ, Q', R, _, C, FFh, fires, ?_, H3, ?_, ?_, fun k hk => Function.update_of_ne hk _ _, ndTail⟩ + · unfold SchedTail + rw [nd_bind_oneSc waking, nd_bind_oneSc (checkDomainTime_single_specA σ2 inputs.oneDomain)] + unfold fl4SelectThread + rw [nd_bind_assocSc, nd_bind_oneSc selection] + exact tail + · exact (Fa.transD frame rfl rfl).transA Ftail rfl rfl + · have Sb : SchedFrameA σ2 σ3 := by + have hm : ((), σ3) ∈ (fl4SelectAction (Z := Unit) layout.NUM_DOMAIN_SCHEDULES.val σ2).1 := by + rw [selection]; exact Set.mem_singleton _ + exact fl4SelectAction_presA _ σ2 _ hm + have Sc : SchedFrameA σ3 τ := + (inferInstance : SPresInst (setSchedulerAction (Z := Unit) .resumeCurThread >>= fun _ => fl4ScAndTimer view)).pres + σ3 _ tail + exact PreO.trans Sa (PreO.trans Sb Sc) + +/-- `schedule_exitFA` with the inputs asked at every post-fire state. -/ +theorem schedule_exitA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {σ : State Unit} {a : A} {tm : A → Nat → DeadlineViewA} + {AR : A → HwRootA → Prop} (P : ArchSwFA st ar) (view : State Unit → Fl4TimerView) + (inputs : ScheduleInputsA (st := st) Z m κ s a σ ar tm view AR) + {FF : S → S → State Unit → State Unit → Prop} + (fireF : timer.fire st ar s a ⦃ r => ∃ σ1 Q R, + fl4TimerFires σ σ1 ∧ YieldQueueRelA (st := st) {Z with Q := Q, R := R} m κ r.1 σ1 ∧ IdleThreadsA m σ1 ∧ + FF s r.1 σ σ1 ⦄) : + sched.schedule st ar s a ⦃ r => ∃ s1 σ1 τ Q R Tcur Ccur, + FF s s1 σ σ1 ∧ fl4TimerFires σ σ1 ∧ ((), τ) ∈ (SchedTail kLimitA view σ1).1 ∧ + YieldQueueRelA (st := st) {Z with Q := Q, R := R, curOf := Tcur, curScOf := Ccur} m κ r.1 τ ∧ + MFrDA (st := st) Z m s1 r.1 σ1 τ ∧ SchedFrameA σ1 τ ∧ (∀ k, k ≠ m.curCore → Tcur k = Z.curOf k) ∧ + (∀ Q' R', SchedSRN st {Z with Q := Q', R := R'} m κ s1 σ1 → NdOKA (st := st) s1 σ1 → + NdOKA (st := st) r.1 τ) ⦄ := + schedule_exitFA P view ⟨inputs.oneDomain, inputs.minAgree, + fun r _ σ1 σ2 Q R _ wake fires waking H2 _ => inputs.selection r σ1 σ2 Q R wake fires waking H2, + fun start selected _ σ1 σ2 σ3 Q R Q' _ wake sel fires waking _ selection frame H3 => + inputs.tail start selected σ1 σ2 σ3 Q R Q' wake sel fires waking selection frame H3⟩ fireF + +/-! ## Words off the scheduler write set -/ + +/-- A scheduler write inside a typed object that is not an IRQ-node slot is one of a TCB's four scheduler words. -/ +theorem mwr_inThA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRN st Z m κ s σ) {p b : U64} {k : ObjKindA} (hp : Z.th p = some k) (hk : k ≠ .cnode 0) + (lo : p.val ≤ b.val) (hi : b.val < p.val + k.size) (h : MWrA (st := st) Z m σ s b) : + k = .tcb ∧ (b.val - p.val = 3464 ∨ b.val - p.val = 3480 ∨ b.val - p.val = 3488 ∨ b.val - p.val = 3592) := by + have ni : ¬ IrqNodeA (L.ksb s) p := by + rintro ⟨i, hi, e⟩ + rw [e, H.place.irq i hi] at hp + exact hk (Option.some.inj hp).symm + rcases h with ⟨c, hc, x, n, hx, hin⟩ | h | ⟨u, hu, e⟩ + · rcases (H.scp.out c hc x n hx b hin).1 p k hp with h | h <;> omega + · have ks := SKsA.ks h + rcases H.place.ks p k hp ni with h | h <;> omega + · have hin := H.swr_tcb_in hu e + have hu' := (H.thT u).2 hu + by_cases c : u = p + · subst c + rw [hu'] at hp + refine ⟨(Option.some.inj hp).symm, ?_⟩ + have hfit := (H.core.tcbs u hu).fits + have hv : ∀ K : U64, K.val < 4096 → (wa u K).val = u.val + K.val := by + intro K hK; rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + rcases e with e | e | e | e + · change b = wa u 3464#u64 at e + rw [e, hv _ (by simp)]; simp + · change b = wa u 3480#u64 at e + rw [e, hv _ (by simp)]; simp + · change b = wa u 3488#u64 at e + rw [e, hv _ (by simp)]; simp + · change b = wa u 3592#u64 at e + rw [e, hv _ (by simp)]; simp + · have sz : ObjKindA.tcb.size = 4096 := rfl + rcases H.place.apart u p _ _ hu' hp c with h | h <;> omega + +/-- A TCB word other than the four scheduler words is off the scheduler write set. -/ +theorem tcbK_offMWrA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRN st Z m κ s σ) {t K : U64} (ht : t ∈ Z.T) (hK : K.val < 4096) + (h : K.val ≠ 3464 ∧ K.val ≠ 3480 ∧ K.val ≠ 3488 ∧ K.val ≠ 3592) : ¬ MWrA (st := st) Z m σ s (wa t K) := by + intro w + have hv : (wa t K).val = t.val + K.val := field_val (H.core.tcbs t ht) hK + have sz : ObjKindA.tcb.size = 4096 := rfl + have := (mwr_inThA H ((H.thT t).2 ht) (by intro e; cases e) (by omega) (by omega) w).2 + omega + +/-- A word of an `other` object is off the scheduler write set. -/ +theorem other_offMWrA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRN st Z m κ s σ) {p b : U64} {len : Nat} (hp : Z.th p = some (.other len)) + (lo : p.val ≤ b.val) (hi : b.val < p.val + len) : ¬ MWrA (st := st) Z m σ s b := fun w => by + have := (mwr_inThA H hp (by intro e; cases e) lo hi w).1 + cases this + +/-- The notification relation's footprint is off the scheduler write set. -/ +theorem nfoot_offMWrA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} {N : Set U64} + (H : SchedSRN st Z m κ s σ) (hN : ∀ n ∈ N, Z.th n = some (.other 64)) {b : U64} (hb : Notif.NFoot N Z.T b) : + ¬ MWrA (st := st) Z m σ s b := by + rcases hb with ⟨n, hn, hlo, hhi⟩ | ⟨t, ht, h⟩ + · exact other_offMWrA H (hN n hn) hlo (by omega) + have hta := H.core.tcbs t ht + rcases h with h | h | h | h | h | h <;> rw [h] <;> + simp only [stA_eq, Notif.tBlockA_eq, Notif.badgeRegA_eq, Notif.prioA_eq', Notif.nextEp_eq, Notif.prevEp_eq] <;> + exact tcbK_offMWrA H ht (by simp) (by simp) + +/-! ## The record at a new hardware root -/ + +/-- **`JStage3A` at a new arch state and hardware root**: nothing else changes. -/ +theorem JStage3A.of_hwA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {nA : U64} {s : S} {a a' : A} + {σ : State Unit} {m : McState} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {hw : HwRootA} (har : AR a' hw) : + JStage3A st AR { Z with hw := hw } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s a' σ m := by + have w : WritesIn st s s (fun _ => False) := WritesIn.refl s _ + have hP : ∀ Pt Ub : Set U64, VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt → + (∀ p, SchedPageA st Z OS s p → p ∈ Pt) → ∀ b, False → VsR (L.ksb s) Pt b := fun _ _ _ _ _ h => h.elim + obtain ⟨Pt, Ub, vr, pn, arm, capc, vc, df, hicap, link, vcks, fu, ual, vcu, R⟩ := J.j7 + exact JStage3A.of_vsWrite (hw := hw) (a' := a') J w hP (fun _ h => h.elim) (VsSpecA.refl' σ) (fun _ _ _ h => h) + (JAtZAX.of_hwA J.j har) J.sys J.vsOk J.bufVs J.sq J.poolOff J.unmPt.hwA J.j5 + ⟨Pt, Ub, vr, pn, arm, capc, vc, df, hicap, link, vcks, fu, ual, vcu, R⟩ J.symL J.j5.2.1 + +/-! ## The arch relation across `schedule` -/ + +/-- **[P] The arch operations of `schedule` after the timer pass keep the arch state related** to some hardware root +(`MRKeepA` style, over every arch state; `AR` is the platform's abstract relation). -/ +structure ArRootKeepA {A C : Type} (arI : machine.Arch A) (AR : A → C → Prop) : Prop where + set_vspace : ∀ a x y c, AR a c → arI.set_vspace a x y ⦃ a' => ∃ c', AR a' c' ⦄ + set_stage2_vspace : ∀ a x y c, AR a c → arI.set_stage2_vspace a x y ⦃ a' => ∃ c', AR a' c' ⦄ + set_high_vspace : ∀ a x y c, AR a c → arI.set_high_vspace a x y ⦃ a' => ∃ c', AR a' c' ⦄ + set_deadline : ∀ a d c, AR a c → arI.set_deadline a d ⦃ a' => ∃ c', AR a' c' ⦄ + send_reschedule : ∀ a x c, AR a c → arI.send_reschedule a x ⦃ a' => ∃ c', AR a' c' ⦄ + +/-- Every returning run of `r` ends in an arch state satisfying `P`. -/ +def AKA {S A : Type} (P : A → Prop) (r : Result (S × A)) : Prop := ∀ v, r = ok v → P v.2 + +namespace AKA +variable {S A : Type} {P : A → Prop} + +theorem ret (s : S) {a : A} (h : P a) : AKA P (ok (s, a)) := fun v e => by + rw [← ok_inj_ff e]; exact h + +theorem bind {α : Type} {m : Result α} {f : α → Result (S × A)} (h : ∀ x, AKA P (f x)) : + AKA P (Std.bind m f) := fun v e => by + obtain ⟨x, -, e⟩ := bind_eq_ok e + exact h x v e + +theorem arch {m : Result A} {f : A → Result (S × A)} (h : ∀ a, m = ok a → P a) (k : ∀ a, P a → AKA P (f a)) : + AKA P (Std.bind m f) := fun v e => by + obtain ⟨x, ex, e⟩ := bind_eq_ok e + exact k x (h x ex) v e + +theorem step {S' : Type} {m : Result (S' × A)} {f : S' × A → Result (S × A)} (h : AKA P m) + (k : ∀ s a, P a → AKA P (f (s, a))) : AKA P (Std.bind m f) := fun v e => by + obtain ⟨⟨s, a⟩, ex, e⟩ := bind_eq_ok e + exact k s a (h _ ex) v e + +end AKA + +theorem reachedA_of_specA {α : Type} {r : Result α} {Q : α → Prop} (h : r ⦃ Q ⦄) : ∀ v, r = ok v → Q v := + fun v e => by rw [e, WP.spec_ok] at h; exact h + +section walk +variable {S A C : Type} (st : machine.Store S) {ar : machine.Arch A} {AR : A → C → Prop} (K : ArRootKeepA ar AR) +include K + +local notation "P" => fun a => ∃ c, AR a c + +theorem ArRootKeepA.vs {a : A} {x y : U64} (h : ∃ c, AR a c) : ∀ a', ar.set_vspace a x y = ok a' → ∃ c, AR a' c := + let ⟨c, h⟩ := h; reachedA_of_specA (K.set_vspace a x y c h) + +theorem ArRootKeepA.s2 {a : A} {x y : U64} (h : ∃ c, AR a c) : + ∀ a', ar.set_stage2_vspace a x y = ok a' → ∃ c, AR a' c := + let ⟨c, h⟩ := h; reachedA_of_specA (K.set_stage2_vspace a x y c h) + +theorem ArRootKeepA.hi {a : A} {x y : U64} (h : ∃ c, AR a c) : + ∀ a', ar.set_high_vspace a x y = ok a' → ∃ c, AR a' c := + let ⟨c, h⟩ := h; reachedA_of_specA (K.set_high_vspace a x y c h) + +theorem ArRootKeepA.pair {a : A} {x y z : U64} {q : Prop} [Decidable q] (h : ∃ c, AR a c) : + ∀ a', (if q then (do let a2 ← ar.set_high_vspace a 0#u64 y; ar.set_vspace a2 x y) + else (do let a2 ← ar.set_vspace a x y; ar.set_high_vspace a2 z y)) = ok a' → ∃ c, AR a' c := by + intro a' e + split at e + · obtain ⟨a2, e2, e⟩ := bind_eq_ok e + exact K.vs (K.hi h _ e2) _ e + · obtain ⟨a2, e2, e⟩ := bind_eq_ok e + exact K.hi (K.vs h _ e2) _ e + +theorem ArRootKeepA.kroot (s : S) {a : A} (h : ∃ c, AR a c) : AKA P (vspace.set_kernel_vm_root st ar s a) := by + unfold vspace.set_kernel_vm_root + exact AKA.bind fun _ => AKA.arch (K.hi h) fun _ h => AKA.arch (K.vs h) fun _ h => AKA.ret _ h + +theorem ArRootKeepA.stage2 (s : S) {a : A} (t : U64) (h : ∃ c, AR a c) : + AKA P (vspace.set_stage2_root st ar s a t) := by + unfold vspace.set_stage2_root + refine AKA.bind fun _ => AKA.bind fun c => ?_ + cases c + all_goals + repeat' (first + | exact AKA.ret _ h + | exact AKA.arch (K.s2 h) fun _ h' => AKA.ret _ h' + | refine AKA.bind fun _ => ?_ + | split) + +theorem ArRootKeepA.vmRoot (s : S) {a : A} (t : U64) (h : ∃ c, AR a c) : + AKA P (vspace.set_vm_root st ar s a t) := by + unfold vspace.set_vm_root + refine AKA.bind fun i => ?_ + split + · exact AKA.step (K.kroot st _ h) fun s1 a1 h1 => K.stage2 st _ _ h1 + refine AKA.bind fun _ => AKA.bind fun c => ?_ + refine AKA.step ?_ fun s2 a1 h1 => ?_ + · cases c + all_goals + repeat' (first + | exact K.kroot st _ h + | exact AKA.ret _ h + | refine AKA.arch (K.pair h) fun _ h' => AKA.ret _ h' + | refine AKA.arch (K.hi h) fun _ h' => AKA.arch (K.vs h') fun _ h'' => AKA.ret _ h'' + | refine AKA.bind fun _ => ?_ + | split) + · try simp only [uncurry_apply_pair] + repeat' (first + | exact AKA.ret _ h1 + | exact K.stage2 st _ _ h1 + | refine AKA.bind fun _ => ?_ + | split) + +theorem ArRootKeepA.switchT (s : S) {a : A} (t : U64) (h : ∃ c, AR a c) : + AKA P (sched.switch_to_thread st ar s a t) := by + unfold sched.switch_to_thread + exact AKA.step (K.vmRoot st _ _ h) fun _ _ h => AKA.bind fun _ => AKA.bind fun _ => AKA.ret _ h + +theorem ArRootKeepA.chooseNew (s : S) {a : A} (h : ∃ c, AR a c) : + AKA P (sched.schedule_choose_new_thread st ar s a) := by + unfold sched.schedule_choose_new_thread sched.choose_thread + refine AKA.bind fun _ => AKA.bind fun _ => AKA.bind fun _ => AKA.bind fun _ => AKA.bind fun _ => + AKA.bind fun _ => AKA.bind fun _ => ?_ + split + · exact AKA.bind fun _ => AKA.bind fun _ => AKA.bind fun _ => K.switchT st _ _ h + · unfold sched.switch_to_idle_thread + exact AKA.bind fun _ => AKA.step (K.kroot st _ h) fun _ _ h => AKA.bind fun _ => AKA.ret _ h + +theorem ArRootKeepA.nextIrq (s : S) {a : A} (h : ∃ c, AR a c) : AKA P (sched.set_next_interrupt st ar s a) := by + unfold sched.set_next_interrupt + have dl : ∀ d a', ar.set_deadline a d = ok a' → ∃ c, AR a' c := fun d => + let ⟨c, h⟩ := h; reachedA_of_specA (K.set_deadline a d c h) + repeat' (first + | exact AKA.ret _ h + | exact AKA.arch (dl _) fun _ h' => AKA.ret _ h' + | refine AKA.bind fun _ => ?_ + | split) + +theorem ArRootKeepA.pending (s : S) {a : A} (h : ∃ c, AR a c) : AKA P (smp.send_pending st ar s a) := by + unfold smp.send_pending + have sr : ∀ x a', ar.send_reschedule a x = ok a' → ∃ c, AR a' c := fun x => + let ⟨c, h⟩ := h; reachedA_of_specA (K.send_reschedule a x c h) + repeat' (first + | exact AKA.ret _ h + | exact AKA.arch (sr _) fun _ h' => AKA.ret _ h' + | refine AKA.bind fun _ => ?_ + | split) + +/-- **`schedule` after the timer pass keeps the arch state related.** -/ +theorem ArRootKeepA.schedule {s : S} {a : A} {f v : S × A} (fire : timer.fire st ar s a = ok f) + (h : ∃ c, AR f.2 c) (run : sched.schedule st ar s a = ok v) : ∃ c, AR v.2 c := by + refine (?_ : AKA P (sched.schedule st ar s a)) v run + unfold sched.schedule sched.awaken + rw [fire] + simp only [bind_ok] + obtain ⟨s1, a1⟩ := f + try simp only [uncurry_apply_pair] + refine AKA.step (AKA.bind fun _ => AKA.ret _ h) fun s2 a2 h2 => ?_ + try simp only [uncurry_apply_pair] + refine AKA.bind fun _ => AKA.bind fun act => ?_ + refine AKA.step ?_ fun s3 a3 h3 => ?_ + · repeat' (first + | exact AKA.ret _ h2 + | exact K.chooseNew st _ h2 + | exact K.switchT st _ _ h2 + | refine AKA.bind fun _ => ?_ + | split) + · try simp only [uncurry_apply_pair] + refine AKA.bind fun _ => AKA.step ?_ fun s5 a5 h5 => ?_ + · unfold sched.switch_sched_context + repeat' (first + | exact AKA.ret _ h3 + | refine AKA.bind fun _ => ?_) + try simp only [uncurry_apply_pair] + refine AKA.bind fun _ => AKA.bind fun _ => AKA.step ?_ fun _ _ h6 => ?_ + · split + · exact AKA.step (K.nextIrq st _ h5) fun _ _ h => AKA.bind fun _ => AKA.ret _ h + · exact AKA.ret _ h5 + try simp only [uncurry_apply_pair] + exact K.pending st _ h6 + +end walk + +/-! ## The record across `schedule` -/ + +/-! ## Footprints off the domain-schedule words, and the record steps over the wider write set -/ + +/-- A word of a typed object that is not an IRQ-node slot is off `DomW`. -/ +theorem th_domA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRN st Z m κ s σ) {p b : U64} {k : ObjKindA} (hp : Z.th p = some k) (hk : k ≠ .cnode 0) + (lo : p.val ≤ b.val) (hi : b.val < p.val + k.size) : ¬ DomW (L.ksb s) b := by + intro hd + have := H.place.ks p k hp (fun hn => hk (H.place.irqK hp hn)) + have hksz : ksSizeA = 788736 := rfl + unfold DomW at hd + omega + +/-- A word of a TCB of `T` is off `DomW` (from the scheduler relation). -/ +theorem tcbK_domSA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRN st Z m κ s σ) {t K : U64} (ht : t ∈ Z.T) (hK : K.val < 4096) : ¬ DomW (L.ksb s) (wa t K) := by + have hv : (wa t K).val = t.val + K.val := field_val (H.core.tcbs t ht) hK + have sz : ObjKindA.tcb.size = 4096 := rfl + exact th_domA H ((H.thT t).2 ht) (by intro e; cases e) (by omega) (by omega) + +/-- The notification relation's footprint is off `DomW`. -/ +theorem nfoot_domA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} {N : Set U64} + (H : SchedSRN st Z m κ s σ) (hN : ∀ n ∈ N, Z.th n = some (.other 64)) {b : U64} (hb : Notif.NFoot N Z.T b) : + ¬ DomW (L.ksb s) b := by + rcases hb with ⟨n, hn, hlo, hhi⟩ | ⟨t, ht, h⟩ + · exact th_domA H (hN n hn) (by intro e; cases e) hlo (show b.val < n.val + 64 by omega) + have hta := H.core.tcbs t ht + rcases h with h | h | h | h | h | h <;> rw [h] <;> + simp only [stA_eq, Notif.tBlockA_eq, Notif.badgeRegA_eq, Notif.prioA_eq', Notif.nextEp_eq, Notif.prevEp_eq] <;> + exact tcbK_domSA H ht (by simp) + +/-- The endpoint relation's footprint is off `DomW`. -/ +theorem efoot_domA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} {N E R : Set U64} + {Bf : Set (U64 × Fl4SpecA64.Word)} (H : SchedSRN st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) {b : U64} + (hb : EFoot Z.T E R Bf b) : ¬ DomW (L.ksb s) b := by + have sz : ObjKindA.tcb.size = 4096 := rfl + rcases hb with ⟨e, he, h⟩ | ⟨r, hr, h⟩ | ⟨t, ht, h | h⟩ | h + · exact th_domA H (P.endpoint e he) (by intro e; cases e) (by omega) (show b.val < e.val + 16 by omega) + · exact th_domA H (P.reply r hr) (by intro e; cases e) (by omega) (show b.val < r.val + 32 by omega) + · obtain ⟨hlo, hoff⟩ := h + unfold TOffN at hoff + exact th_domA H ((H.thT t).2 ht) (by intro e; cases e) hlo (by omega) + · unfold TL at h + exact th_domA H ((H.thT t).2 ht) (by intro e; cases e) (by omega) (by omega) + · obtain ⟨p, len, hp, hlo, hhi⟩ := P.buffer b h + exact th_domA H hp (by intro e; cases e) hlo hhi + +set_option maxHeartbeats 1000000 in +/-- `j57_stepNWA` over a scheduler step that may write the domain-schedule words. -/ +theorem JStage3A.j57_stepDA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {nA : U64} {s s' : S} {a : A} {σ : State Unit} {m : McState} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (w : WritesIn st s s' (fun b => SchedStepWA (st := st) Z m σ s b ∨ DomW (L.ksb s) b)) (σ' : State Unit) : + J5A st Z H OS s' σ' m ∧ J7A st (J7R2A st ar nA) Z H OS s' σ' m := by + have osk := J.osKeep_stepDA w + have kb : L.ksb s' = L.ksb s := w.1 + have irqW : ∀ i : U64, i.val < 5376 → + L.mem s' (irqStAddrA (L.ksb s) i) = L.mem s (irqStAddrA (L.ksb s) i) := by + intro i hi + have hv := irqWordA_val J.sr.ksFit hi + have hkb := J.sr.toSchedSRN.ksb_al + refine w.mem_eq ?_ (fun h => h.elim (J.irq_offStepA hi) (J.irq_domA hi)) + show (irqWordA (L.ksb s) i).val % 8 = 0 + unfold Aligned at hkb + omega + refine ⟨J.j5_keepA osk kb irqW σ', J7PpKA.keep (st := st) _ _ _ _ _ σ σ' _ J.j7 kb osk ?_⟩ + intro Pt Ub vr b hb + refine w.mem_eq (vr.al _ hb) (fun h' => h'.elim ?_ (J.omega_domA (vr.ks b hb))) + by_cases h : AsW (L.ksb s) b + · obtain ⟨o, o1, o2, -, e⟩ := h + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.sys.ent.ksw + have ev : b.val = (L.ksb s).val + o.val := by + rw [e, wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact J.asid_offStepA (by omega) (by omega) + · exact J.vsr_offStepA vr hb h + +/-- `ent_stepNWA` over a scheduler step that may write the domain-schedule words. -/ +theorem JStage3A.ent_stepDA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {nA : U64} {s s' : S} {a : A} {σ : State Unit} {m : McState} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (w : WritesIn st s s' (fun b => SchedStepWA (st := st) Z m σ s b ∨ DomW (L.ksb s) b)) + (blk : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s') : + EntryInvA3 st H Z.Cs OS U Ac Pp Tb Lv s' := by + have kb : L.ksb s' = L.ksb s := w.1 + refine ppEntA3 J.sys.ent blk kb (utVsPt_keepA J.sys.ent.vs kb fun b hb => ?_).1 + have vr := J.vsOk + refine w.mem_eq (vr.al _ hb) (fun h' => h'.elim ?_ (J.omega_domA (vr.ks b hb))) + by_cases h : AsW (L.ksb s) b + · obtain ⟨o, o1, o2, -, e⟩ := h + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.sys.ent.ksw + have ev : b.val = (L.ksb s).val + o.val := by + rw [e, wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact J.asid_offStepA (by omega) (by omega) + · exact J.vsr_offStepA vr hb h + +set_option maxHeartbeats 4000000 in +/-- **The record across `schedule`** (module docstring). -/ +theorem JStage3A.acrossSchedA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {s s1 s' : S} {a a1 a' : A} {σ σ1 τ : State Unit} {m : McState} + {view : State Unit → Fl4TimerView} + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) (hcore : m.curCore < fl4A64Platform.numNodes) + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + {Qf : U64 → U64 → List U64} {Rf : U64 → List U64} + (J1 : JStage3A st AR { Z with Q := Qf, R := Rf } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ + fl4A64Platform.numNodes (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s1 a1 σ1 m) + (R1 : FireRecA σ1) + (hC : σ1.schedulerAction = .resumeCurThread → ∀ d p, σ1.curThread ∉ σ1.readyQueues d p) + (run : sched.schedule st ar s a = ok (s', a')) + (tail : ((), τ) ∈ (SchedTail kLimitA view σ1).1) + {Q : U64 → U64 → List U64} {Rl : U64 → List U64} {Tcur Ccur : Fl4SpecA64.CoreId → ObjRef} + (H3 : YieldQueueRelA (st := st) { Z with Q := Q, R := Rl, curOf := Tcur, curScOf := Ccur } m κ s' τ) + (FD : MFrDA (st := st) { Z with Q := Qf, R := Rf } m s1 s' σ1 τ) (SF : SchedFrameA σ1 τ) + (hdom : NdOKA (st := st) s' τ) + (hcurOf : ∀ k, k ≠ m.curCore → Tcur k = Z.curOf k) + {hw : HwRootA} (har : AR a' hw) : + JStage3A st AR { Z with Q := Q, R := Rl, curOf := Tcur, curScOf := Ccur, hw := hw } H OS U Ac Pp Tb Lv ia ie + N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s' a' τ m := by + -- A64 P1's walk of the whole `schedule`, from the entry record + have hc0 : 0#u64 ∈ Z.Cs := by + obtain ⟨c, hc, e⟩ := J.coresAll 0 (Nat.lt_of_le_of_lt (Nat.zero_le _) hcore) + have : c = 0#u64 := UScalar.eq_of_val_eq (by rw [e]; rfl) + exact this ▸ hc + have W : SW st H Z.Cs OS Z.V ia ie Ac s s' := by + obtain ⟨Pt, Ub, Pp', Tb', Hr, Te, Kt, Lv', Re, bits, KLv, W⟩ := J7A.unp J.j7 + have I := J.delIn hA hPl W + have h := sched.schedule.sw (st := st) (ar := ar) hA a I J.sys.sysl J.symL J.blkA hc0 J.ko + (J.tcbs ▸ J.runVcpu) + rw [run, WP.spec_ok] at h + simpa only [WP.uncurry'_pair] using h + have SB : SBindA σ1 τ := schedTail_bindA _ _ tail + obtain ⟨fia, own⟩ := schedTail_ctQ _ _ R1 hC tail + have hc : HcA τ := by + have h := fl4ScAndTimer_hc view + obtain ⟨⟨⟨⟩, σ2⟩, -, t⟩ := mem_bindA.1 tail + obtain ⟨⟨⟨⟩, σ3⟩, -, t⟩ := mem_bindA.1 t + obtain ⟨⟨⟨⟩, σ4⟩, -, t⟩ := mem_bindA.1 t + exact h σ4 trivial _ t + have SK : SKeepA σ1 τ := FD.keep + have HS : SpecSchedA σ1 τ := SF.1 + have SSz : SSzA σ1 τ := SF.2.1 + have hum := SF.2.2 + -- the TCBs are kept + have tK : ∀ p x, σ1.kheap p = some (.tcb x) → τ.kheap p = some (.tcb x) := fun p x h => by + obtain ⟨y, hy, o, e⟩ := SK.tcb p x h + obtain ⟨x0, h0, es⟩ := SB.tcb p y hy + rw [h] at h0 + obtain rfl := KernelObject.tcb.inj (Option.some.inj h0) + have : y = x := by + subst e + cases x + simp only at es ⊢ + rw [es] + rw [hy, this] + have tB : ∀ p x, τ.kheap p = some (.tcb x) → σ1.kheap p = some (.tcb x) := fun p x h => by + obtain ⟨y, hy⟩ := HS.tcbBack h + rw [tK p y hy] at h + rw [hy, KernelObject.tcb.inj (Option.some.inj h)] + have scF : ∀ p x k, σ1.kheap p = some (.schedContext x k) → + ∃ y, τ.kheap p = some (.schedContext y k) ∧ y.scReplies = x.scReplies ∧ y.scTcb = x.scTcb := by + intro p x k h + have : ∃ y j, τ.kheap p = some (.schedContext y j) := by + rcases HS.kh p with e | ⟨u, u', e1, -⟩ | ⟨o, o', e1, e2, f⟩ + · exact ⟨x, k, e.trans h⟩ + · rw [h] at e1; cases e1 + · rw [h] at e1; cases e1 + rcases f with ⟨_, _, y', n', -, rfl⟩ | ⟨_, _, e, -⟩ | ⟨_, _, e, -⟩ | ⟨_, _, e, -⟩ + · exact ⟨y', n', e2⟩ + all_goals cases e + obtain ⟨y, j, hy⟩ := this + obtain ⟨y0, h0, r, t⟩ := SB.sc p y j hy + rw [h] at h0 + have h1 := Option.some.inj h0 + injection h1 with e1 e2 + subst e1 e2 + exact ⟨y, hy, r, t⟩ + -- the write set + have w : WritesIn st s1 s' (fun b => SchedStepWA (st := st) { Z with Q := Qf, R := Rf } m σ1 s1 b ∨ + DomW (L.ksb s1) b) := + FD.wr.mono fun _ h => h.elim (fun h => Or.inl (schedStepW_mwr h)) Or.inr + have N1 := J1.sr.toSchedSRN + have tw : ∀ t ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → + (K.val ≠ 3464 ∧ K.val ≠ 3480 ∧ K.val ≠ 3488 ∧ K.val ≠ 3592) → L.mem s' (wa t K) = L.mem s1 (wa t K) := + fun t ht K h1 h2 h3 => FD.wr.mem_eq (field_aligned (N1.core.tcbs t ht) h1 h2) + (fun h => h.elim (tcbK_offMWrA N1 ht h1 h3) (tcbK_domSA N1 ht h1)) + have kb : L.ksb s' = L.ksb s1 := FD.wr.1 + have sq : SchedQueuesInv st H.tcbs Z.Cs H.omega H s' Q Rl := by + obtain ⟨Q0, R0, h⟩ := W.1.blk.1 + exact SchedQueuesInv.reQA h (by rw [J.tcbs]; exact H3.sched.core) + have poolOff : PoolOff st H Z.Cs OS s' := fun i => by + have h0 := J1.poolOff i + rw [vspace.asid_table.eq] at h0 ⊢ + have v := AtOff.v i + have hks : (L.ksb s1).val + 788736 ≤ 2 ^ 64 := J1.sys.ent.ksw + have ev : (wa (L.ksb s1) (AtOff i)).val = (L.ksb s1).val + (AtOff i).val := by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have e : L.mem s' (wa (L.ksb s') (AtOff i)) = L.mem s1 (wa (L.ksb s1) (AtOff i)) := by + rw [kb] + refine w.mem_eq ?_ (fun h => h.elim (J1.asid_offStepA (by omega) (by omega)) (asid_domA (by omega))) + have h32 := J1.j.1.ksbAl + show (wa (L.ksb s1) (AtOff i)).val % 8 = 0 + omega + rw [e] + refine (WP.spec_ok _).2 fun hne asid => ?_ + have q := (WP.spec_ok _).1 h0 hne asid + exact ⟨q.al, q.om, q.sg, q.os, RegionOffNodes.ksb kb q.ks⟩ + have hP := J1.iplaced + have ep : EpStateRel Z.T E Rp Bf (L.mem s') τ := by + exact J1.ep.frame hP.e (fun b ha hb => FD.wr.mem_eq ha fun h => + h.elim (endpFootOffWrite N1 J1.ipcPlace hb) (efoot_domA N1 J1.ipcPlace hb)) (epFrameOfMcsA (st := st) (s' := s1) N1 J1.ipcPlace ⟨WritesIn.refl _ _, FD.keep, FD.sz⟩ hum).2 + have kv : Notif.KView σ1 τ := by + refine ⟨fun p x hx => SK.obj p _ hx (by intro _ _ e; cases e) (by intro _ e; cases e), fun p x hx => ?_⟩ + exact ⟨x, tK p x hx, rfl, rfl, rfl⟩ + have ntfn : NtfnStateRel N Z.T (L.mem s') τ := + J1.ntfn.frame hP.n (fun b hb hf => FD.wr.mem_eq hb fun h => + h.elim (nfoot_offMWrA N1 J1.ipcPlace.notification hf) (nfoot_domA N1 J1.ipcPlace.notification hf)) kv + have reply : ReplyStackRelA (L.mem s') Rp Z.C τ (scNameA m) stack := by + refine J1.reply.frame (fun r hr => ?_) (fun c _ x k hk => ?_) (fun r hr x hx => ?_) + · obtain ⟨r32, -, rfit⟩ := hP.e.rep r hr + have one : ∀ o : U64, o.val < 32 → o.val % 8 = 0 → L.mem s' (wa r o) = L.mem s1 (wa r o) := by + intro o o1 o2 + have v : (wa r o).val = r.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + refine FD.wr.mem_eq ?_ (fun h => h.elim + (other_offMWrA N1 (J1.ipcPlace.reply r hr) (by omega) (by omega)) + (th_domA N1 (J1.ipcPlace.reply r hr) (by intro e; cases e) (by omega) + (show (wa r o).val < r.val + 32 by omega))) + show (wa r o).val % 8 = 0 + omega + have n16 : (layout.reply.NEXT).val = 16 := by unfold layout.reply.NEXT; rfl + have p8 : (layout.reply.PREV).val = 8 := by unfold layout.reply.PREV; rfl + exact ⟨one _ (by omega) (by omega), one _ (by omega) (by omega)⟩ + · obtain ⟨y, hy, r, -⟩ := scF _ _ _ hk + exact ⟨y, k, hy, r⟩ + · obtain ⟨x0, h0⟩ := J1.ep.reply r hr + have h1 := SK.obj _ _ h0.1 (by intro _ _ e; cases e) (by intro _ e; cases e) + rw [h1] at hx + obtain rfl := KernelObject.reply.inj (Option.some.inj hx) + exact ⟨x0, h0.1, rfl⟩ + have bufCover : BufCover st Z.T Bf s' τ := + J1.bufCover_wA FD.wr (fun t ht K hK h => h.elim (tcbK_offMWrA N1 ht (by omega) (by omega)) + (tcbK_domSA N1 ht (by omega))) + (fun p x h => ⟨x, tK p x h, rfl, rfl⟩) + have bq : BlkQueuedA Z.T E N (L.mem s') τ := + J1.blkQ.keepA (fun u hu => by + rw [Notif.tBlockA_eq]; exact tw u hu 3336#u64 (by simp) (by simp) (by simp)) + (fun u hu x hx => Or.inr ⟨x, tB _ x hx, rfl⟩) + (fun p y h => SK.obj p _ h (by intro _ _ e; cases e) (by intro _ e; cases e)) + (fun p y h => SK.obj p _ h (by intro _ _ e; cases e) (by intro _ e; cases e)) + have rv : RunVcpuInv st Z.T s' := J1.runVcpu.rvSt fun y hy => by + refine ⟨?_, ?_⟩ + · rw [stA_eq]; exact tw y hy 3328#u64 (by simp) (by simp) (by simp) + · exact tw y hy 3672#u64 (by simp) (by simp) (by simp) + have scCov : ScCovA Z.C m τ := fun p x k h => by + obtain ⟨y, hy⟩ := SSz p x k h + exact J1.scCov p y k hy + have f3 : ∀ c < fl4A64Platform.numNodes, ∃ x, τ.kheap (m.idleThreadOf c) = some (.tcb x) ∧ + x.tcbState = .idleThreadState := fun c hc => by + obtain ⟨x, hx, hs⟩ := J1.sidle.f3 c hc + exact ⟨x, tK _ x hx, hs⟩ + have idle : τ.idleThread = σ1.idleThread := SB.idle + have IK : IdleScK σ1 τ := schedTail_idleScA _ _ tail + have sidle : SIdleA τ m fl4A64Platform.numNodes := by + have vt : ∀ c < fl4A64Platform.numNodes, + vtCapA τ (m.idleThreadOf c) = some .nullCap ∧ vcpuOfA τ (m.idleThreadOf c) = none := fun c hc => by + obtain ⟨x, hx, -⟩ := J1.sidle.f3 c hc + exact idleVtSameA ((tK _ x hx).trans hx.symm) (J1.sidle.idleSlot c hc) (J1.sidle.idleBound c hc) + refine ⟨f3, fun y k hy => ?_, fun p y k hy he => ?_, J1.sidle.f6.of_eq HS.specCapAt, fun y k hy => ?_, + fun c hc => (vt c hc).1, fun c hc => (vt c hc).2⟩ + rotate_left 2 + · obtain ⟨z, hz, hm⟩ := IK.rm y k hy + refine ⟨?_, (IK.rdy (J1.sidle.rdyA J1.sinv)).1 y k hy⟩ + have a := (J1.sidle.f7 z k hz).1 + unfold scActive at a ⊢; rw [hm]; exact a + · obtain ⟨y0, h0, r, -⟩ := SB.sc _ y k hy + rw [r]; exact J1.sidle.f4 y0 k h0 + · obtain ⟨y0, h0, -, t⟩ := SB.sc _ y k hy + exact J1.sidle.f5 p y0 k h0 (by rw [← t, he, idle]) + have run3 := H3.sched.run + have idleQ : ∀ d p, ∀ c < fl4A64Platform.numNodes, m.idleThreadOf c ∉ τ.readyQueues d p := by + intro d p c hc hm + obtain ⟨x, hx, hr⟩ := run3.1 d p _ hm + obtain ⟨y, hy, hs⟩ := f3 c hc + rw [hy] at hx + obtain rfl := KernelObject.tcb.inj (Option.some.inj hx) + rw [hs] at hr + cases hr + have curT : τ.curThread = specPtr (L.curW s') := H3.sched.sch.curThread + obtain ⟨xc, hxc, -⟩ := H3.sched.sch.tcb (L.curW s') H3.sched.curT + have ctQ : ∀ d p, τ.curThread ∉ τ.readyQueues d p := by + intro d p hm + by_cases hd : d = 0 + swap + · rw [H3.sched.rest.rqDom d p hd] at hm; cases hm + subst hd + have hpv : (⟨BitVec.ofNat 64 p.toNat⟩ : U64).val = p.toNat := by + show (BitVec.ofNat 64 p.toNat).toNat = p.toNat + rw [BitVec.toNat_ofNat] + exact Nat.mod_eq_of_lt (by have := p.isLt; omega) + have hp256 : (⟨BitVec.ofNat 64 p.toNat⟩ : U64).val < 256 := by rw [hpv]; exact p.isLt + have e := H3.sched.rest.rq ⟨BitVec.ofNat 64 p.toNat⟩ hp256 + rw [hpv, BitVec.ofNat_toNat, BitVec.setWidth_eq] at e + have hm0 := hm + rw [e, curT, List.mem_map] at hm + obtain ⟨u, hu, eu⟩ := hm + have := specPtr_inj eu + subst this + have pw := (H3.sched.core.inQ _ H3.sched.sch.cpuCs _ hp256 _ hu).2 + obtain ⟨x, hx, dom, -, pr⟩ := H3.sched.rest.prio (L.curW s') H3.sched.curT + have ep : x.tcbPriority = p := by + apply BitVec.eq_of_toNat_eq + rw [← pr, pw, hpv] + rcases own with o | o + · obtain ⟨y, hy, hr⟩ := run3.1 0 p _ hm0 + obtain ⟨z, hz, hs⟩ := J1.sinv.idleTcb + rw [o, idle, tK _ z hz] at hy + obtain rfl := KernelObject.tcb.inj (Option.some.inj hy) + rw [hs] at hr + cases hr + · have := o x (by rw [curT]; exact hx) + rw [dom, ep] at this + exact this hm0 + have sinv : SInvXA τ m fl4A64Platform.numNodes := by + refine + { mc := J1.sinv.mc + view := ?_ + curTcb := ⟨xc, by rw [curT]; exact hxc⟩ + idleTcb := ?_ + onlyIdle := fun t x h => J1.sinv.onlyIdle t x (tB t x h) + idleSc := ?_ + curSc := ?_ + ctQ := fun _ => ctQ + idleQ := idleQ + rq := run3.1 + rls := fun t ht => let ⟨x, h, _⟩ := run3.2 t ht; ⟨x, h⟩ + rpos := ?_ + irqN := ?_ + zc := IK.zc J1.sinv.zc + rlsRun := fun t ht => run3.2 t ht + curScReady := (IK.rdy (J1.sidle.rdyA J1.sinv)).2 + faultSt := fun p y hy hf => J1.sinv.faultSt p y (tB p y hy) hf } + · show τ.idleThread = m.idleThreadOf m.curCore + rw [idle]; exact J1.sinv.view + · obtain ⟨x, hx, hs⟩ := J1.sinv.idleTcb + exact ⟨x, by rw [idle]; exact tK _ x hx, hs⟩ + · obtain ⟨x, y, k, hx, e, hy, t⟩ := J1.sinv.idleSc + obtain ⟨y', hy', -, t'⟩ := scF _ _ _ hy + exact ⟨x, y', k, by rw [idle]; exact tK _ x hx, e, hy', by rw [t', t, idle]⟩ + · have e := hc xc (by rw [curT]; exact hxc) + obtain ⟨x, hx, sc⟩ := H3.sched.sch.tcb (L.curW s') H3.sched.curT + rw [hxc] at hx + obtain rfl := KernelObject.tcb.inj (Option.some.inj hx) + rw [sc] at e + by_cases z : L.mem s' (scA (L.curW s')) = 0#u64 + · rw [optScA, if_pos z] at e; cases e + rw [optScA, if_neg z] at e + obtain ⟨y, k, hy, -⟩ := H3.sched.scr.sc _ (H3.sched.rest.scMem _ H3.sched.curT z) + exact ⟨y, k, by rw [← Option.some.inj e]; exact hy⟩ + · intro p y k h act + obtain ⟨c, hcC, rfl⟩ := scCov p y k h + obtain ⟨y', k', hy, R⟩ := H3.sched.scr.sc c hcC + rw [h] at hy + have h1 := Option.some.inj hy + injection h1 with e1 e2 + subst e1 e2 + have pos : 0 < y.scRefillMax := of_decide_eq_true act + have R1 := R.act (by rw [R.max]; omega) + rw [R1.refills] + intro e + exact refills_neL R1.wf (List.map_eq_nil_iff.1 e) + · have cn : ∀ p k t, σ1.kheap p = some (.cnode k t) ↔ τ.kheap p = some (.cnode k t) := by + intro p k t + refine ⟨fun h => SK.obj p _ h (by intro _ _ e; cases e) (by intro _ e; cases e), fun h => ?_⟩ + rcases HS.kh p with e | ⟨_, _, -, e2, -⟩ | ⟨_, _, -, e2, f⟩ + · rw [← e]; exact h + · rw [h] at e2; cases e2 + · rw [h] at e2; cases e2 + rcases f with ⟨_, _, _, _, -, e⟩ | ⟨_, _, -, e⟩ | ⟨_, _, -, e⟩ | ⟨_, _, -, e⟩ <;> cases e + have cap : ∀ p o b c, τ.kheap p = some o → capOf o b = some c → + ∃ o0, σ1.kheap p = some o0 ∧ capOf o0 b = some c := by + intro p o b c ho hc + cases o with + | cnode k t => exact ⟨_, (cn p k t).2 ho, hc⟩ + | tcb x => exact ⟨_, tB p x ho, hc⟩ + | _ => cases hc + have ct : ∀ n p, capTableAt n p τ ↔ capTableAt n p σ1 := fun n p => by + unfold Fl4SpecA64.capTableAt + exact exists_congr fun cs => and_congr (cn p n cs).symm Iff.rfl + refine + { node := ?_ + slot := fun irq hi o b c ho hc => ?_ + ref := fun p o b c ho hc => ?_ } + · have h := J1.sinv.irqN.node + unfold Fl4SpecA64.IrqNodeFl4 Fl4SpecA64.validIrqNodeFl4 Fl4SpecA64.irqNodeDomFl4 at h ⊢ + simp only [HS.irqN, ct] + exact h + · rw [HS.irqN] at ho + obtain ⟨o0, h0, c0⟩ := cap _ _ _ _ ho hc + exact J1.sinv.irqN.slot irq hi o0 b c h0 c0 + · rw [HS.irqN] + obtain ⟨o0, h0, c0⟩ := cap _ _ _ _ ho hc + exact J1.sinv.irqN.ref p o0 b c h0 c0 + have j := JStage3A.j57_stepDA J1 w τ + have J2 := JStage3A.of_schedWrD (J5 := J5A st) (Rj := J7R2A st ar hPl.ipa) J1 (Q := Q) (Rl := Rl) (curOf := Tcur) + (curScOf := Ccur) (s' := s') (a' := a1) (σ' := τ) w HS SSz (schedTail_qnA _ _ tail) hum J1.j.1.ar hcurOf + (fun t ht y hy => J1.bufAl t ht y (tB _ y hy)) + ⟨JStage3A.ent_stepDA J1 w W.1.blk, W.1.sys⟩ sq poolOff H3.sched scCov ep ntfn reply bufCover + (J1.idleNoFault.of_skeep SK) H3.mcp H3.links sinv sidle rv bq W.1.sym (SchedValidB.toA fia.1 fia.2) j.1 j.2 hdom + exact JStage3A.of_hwA J2 har + +set_option maxHeartbeats 1000000 in +/-- **The record through the whole local scheduler**, given the fires loop `fireR` (`fire_recA`, or one that also +carries a predicate `X` to the post-fire state): `schedule` returns, the spec `fl4Schedule` has the outcome `τ` (a +post-fire state `σ1`, then the tail), and the record holds at `τ`. The inputs of the selection and the tail are +`ScheduleInputsA` given its `fire` field. -/ +theorem JStage3A.scheduleRecFA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {s : S} {a : A} {σ : State Unit} {m : McState} + {tm : A → Nat → DeadlineViewA} {view : State Unit → Fl4TimerView} + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) (arch : ArchSwFA st ar) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + {X : State Unit → Prop} + (fireR : timer.fire st ar s a ⦃ r => ∃ σ1 Q Rl, fl4TimerFires σ σ1 ∧ + JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes + (J5A st) (J7A st (J7R2A st ar hPl.ipa)) r.1 r.2 σ1 m ∧ FireRecA σ1 ∧ X σ1 ⦄) + (hSched : timer.fire st ar s a ⦃ r => ∃ σ1 Q R, + fl4TimerFires σ σ1 ∧ YieldQueueRelA (st := st) {Z with Q := Q, R := R} m κ r.1 σ1 ∧ IdleThreadsA m σ1 ⦄ → + ScheduleInputsA (st := st) Z m κ s a σ ar tm view AR) + (arK : ArRootKeepA ar AR) : + sched.schedule st ar s a ⦃ r => ∃ (f : S × A) (σ1 τ : State Unit) (Qf : U64 → U64 → List U64) + (Rf : U64 → List U64) (Q : U64 → U64 → List U64) (Rl : U64 → List U64) + (Tcur Ccur : Fl4SpecA64.CoreId → ObjRef) (hw : HwRootA), + timer.fire st ar s a = ok f ∧ + JStage3A st AR { Z with Q := Qf, R := Rf } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ + fl4A64Platform.numNodes (J5A st) (J7A st (J7R2A st ar hPl.ipa)) f.1 f.2 σ1 m ∧ + FireRecA σ1 ∧ X σ1 ∧ fl4TimerFires σ σ1 ∧ ((), τ) ∈ (SchedTail kLimitA view σ1).1 ∧ + ((), τ) ∈ (fl4Schedule kLimitA view σ).1 ∧ + YieldQueueRelA (st := st) { Z with Q := Q, R := Rl, curOf := Tcur, curScOf := Ccur } m κ r.1 τ ∧ + JStage3A st AR { Z with Q := Q, R := Rl, curOf := Tcur, curScOf := Ccur, hw := hw } H OS U Ac Pp Tb Lv ia ie + N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) (J7A st (J7R2A st ar hPl.ipa)) r.1 r.2 τ m ⦄ := by + have hcore : m.curCore < fl4A64Platform.numNodes := by + rw [J.sr.sch.core]; exact hCs _ J.sr.sch.cpuCs + have F0 : timer.fire st ar s a ⦃ r => ∃ σ1 Q R, + fl4TimerFires σ σ1 ∧ YieldQueueRelA (st := st) {Z with Q := Q, R := R} m κ r.1 σ1 ∧ IdleThreadsA m σ1 ∧ + (timer.fire st ar s a = ok r ∧ + JStage3A st AR { Z with Q := Q, R := R } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ + fl4A64Platform.numNodes (J5A st) (J7A st (J7R2A st ar hPl.ipa)) r.1 r.2 σ1 m ∧ FireRecA σ1 ∧ X σ1) ⦄ := by + refine WP.spec_mono (reachedResultA fireR) ?_ + rintro r ⟨er, σ1, Q, Rl, hf, J1, R1, X1⟩ + refine ⟨σ1, Q, Rl, hf, J1.yieldQueueA, fun c hc x hx => ?_, er, J1, R1, X1⟩ + obtain ⟨y, hy, hs⟩ := J1.sidle.f3 c hc + rw [hy] at hx + have := KernelObject.tcb.inj (Option.some.inj hx) + subst this + rw [hs]; rfl + have inputs := hSched (WP.spec_mono F0 fun _ ⟨σ1, Q, R, h1, h2, h3, _⟩ => ⟨σ1, Q, R, h1, h2, h3⟩) + have T := schedule_exitA arch view inputs + (FF := fun _ s1 _ σ1 => ∃ (a1 : A) (Q : U64 → U64 → List U64) (R : U64 → List U64), + timer.fire st ar s a = ok (s1, a1) ∧ + JStage3A st AR { Z with Q := Q, R := R } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ + fl4A64Platform.numNodes (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s1 a1 σ1 m ∧ FireRecA σ1 ∧ X σ1) + (WP.spec_mono F0 fun r ⟨σ1, Q, R, h1, h2, h3, ef, J1, R1, X1⟩ => ⟨σ1, Q, R, h1, h2, h3, r.2, Q, R, ef, J1, R1, X1⟩) + refine WP.spec_mono (reachedResultA T) ?_ + rintro r ⟨run, s1, σ1, τ, Q, Rl, Tcur, Ccur, ⟨a1, Qf, Rf, ef, J1, R1, X1⟩, hf, tail, H3, F, SF, hcur, nd⟩ + obtain ⟨hw, har⟩ := arK.schedule st ef ⟨_, J1.j.1.ar⟩ run + have J2 := JStage3A.acrossSchedA hA hPl hcore J J1 R1 J1.sinv.ctQ run tail H3 + ⟨F.wr, F.keep, F.sz⟩ SF (nd _ _ J1.sr.toSchedSRN J1.domOK) hcur har + exact ⟨(s1, a1), σ1, τ, Qf, Rf, Q, Rl, Tcur, Ccur, hw, ef, J1, R1, X1, hf, tail, + fl4Schedule_of_tail kLimitA view hf tail, H3, J2⟩ + +set_option maxHeartbeats 1000000 in +/-- `scheduleRecFA` at `fire_recA`: from the entry record and `SwA`, `schedule` returns, the spec +`fl4Schedule` has the outcome `τ` (a post-fire state `σ1`, then the tail), and the record holds at `τ`. The inputs of +the selection and the tail are `ScheduleInputsA` given its `fire` field (proved here). -/ +theorem JStage3A.scheduleRecA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {s : S} {a : A} {σ : State Unit} {m : McState} + {tm : A → Nat → DeadlineViewA} {view : State Unit → Fl4TimerView} + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) (arch : ArchSwFA st ar) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hSw : SwA σ) + (hSig : ∀ (Q : U64 → U64 → List U64) (Rl : U64 → List U64) (s : S) (a : A) (σ : State Unit) (t badge : U64), + JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes + (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m → + SwA σ → t ∈ Z.T → L.mem s (boundA t) ≠ 0#u64 → + notification.send_signal st ar s a (L.mem s (boundA t)) badge ⦃ r => + ∃ σ' b Q' Rl', sendSignal (Z := Unit) (specPtr (L.mem s (boundA t))) b σ = ({((), σ')}, False) ∧ + JStage3A st AR { Z with Q := Q', R := Rl' } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ + fl4A64Platform.numNodes (J5A st) (J7A st (J7R2A st ar hPl.ipa)) r.1 r.2 σ' m ⦄) + (hSched : timer.fire st ar s a ⦃ r => ∃ σ1 Q R, + fl4TimerFires σ σ1 ∧ YieldQueueRelA (st := st) {Z with Q := Q, R := R} m κ r.1 σ1 ∧ IdleThreadsA m σ1 ⦄ → + ScheduleInputsA (st := st) Z m κ s a σ ar tm view AR) + (arK : ArRootKeepA ar AR) : + sched.schedule st ar s a ⦃ r => ∃ (f : S × A) (σ1 τ : State Unit) (Qf : U64 → U64 → List U64) + (Rf : U64 → List U64) (Q : U64 → U64 → List U64) (Rl : U64 → List U64) + (Tcur Ccur : Fl4SpecA64.CoreId → ObjRef) (hw : HwRootA), + timer.fire st ar s a = ok f ∧ + JStage3A st AR { Z with Q := Qf, R := Rf } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ + fl4A64Platform.numNodes (J5A st) (J7A st (J7R2A st ar hPl.ipa)) f.1 f.2 σ1 m ∧ + FireRecA σ1 ∧ fl4TimerFires σ σ1 ∧ ((), τ) ∈ (SchedTail kLimitA view σ1).1 ∧ + ((), τ) ∈ (fl4Schedule kLimitA view σ).1 ∧ + YieldQueueRelA (st := st) { Z with Q := Q, R := Rl, curOf := Tcur, curScOf := Ccur } m κ r.1 τ ∧ + JStage3A st AR { Z with Q := Q, R := Rl, curOf := Tcur, curScOf := Ccur, hw := hw } H OS U Ac Pp Tb Lv ia ie + N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) (J7A st (J7R2A st ar hPl.ipa)) r.1 r.2 τ m ⦄ := by + have hcore : m.curCore < fl4A64Platform.numNodes := by + rw [J.sr.sch.core]; exact hCs _ J.sr.sch.cpuCs + have F : timer.fire st ar s a ⦃ r => ∃ σ1 Q Rl, fl4TimerFires σ σ1 ∧ + JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes + (J5A st) (J7A st (J7R2A st ar hPl.ipa)) r.1 r.2 σ1 m ∧ FireRecA σ1 ∧ True ⦄ := + WP.spec_mono (JStage3A.fire_recA hA hPl J hcore hSw hSig) fun _ ⟨σ1, Q, Rl, h, J1, R1⟩ => ⟨σ1, Q, Rl, h, J1, R1, trivial⟩ + refine WP.spec_mono (JStage3A.scheduleRecFA hA hPl arch hCs J F hSched arK) ?_ + rintro r ⟨f, σ1, τ, Qf, Rf, Q, Rl, Tcur, Ccur, hw, ef, J1, R1, -, h⟩ + exact ⟨f, σ1, τ, Qf, Rf, Q, Rl, Tcur, Ccur, hw, ef, J1, R1, h⟩ + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3KEntrySendA.lean b/proofs/Fl4ProofsA64/P3/Stage3KEntrySendA.lean new file mode 100644 index 000000000..74abf2d4f --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3KEntrySendA.lean @@ -0,0 +1,831 @@ +/- +P3 (aarch64, A64-P3 Stage 3, kernel-entry composition, step 2): **the record across `charge_budget … true` taking the +timeout send to a waiting receiver** (`kentryChargeIrqA` / `kentryChargeRestartA`'s `hSend`, waiting-receiver case). + +* `JStage3A.j7_stepNUA`: J7 across scheduler / IPC-object writes and buffer words (`hXv7`). +* `JStage3A.ntfnWait_sendXA`: `ntfnWait_sendA` with the timeout sender's fault words. +* `faultTcbStepA`: the fault words are per-step TCB words. +* `SIKXA.headFaultA`, `reschedReprog_sikA`, `reschedReprog_frameA`, `reschedReprog_svA`, `SchedValidA.faultA`, + `rqDomPrio_faultA`: the spec steps around the send (charge head, fault, `rescheduleRequired` with the timer flag). +* **`JStage3A.chargeSendRecvA`**: `charge_budget_sendFrA` (`P3/ScEntrySendA`) at the record: at the head's state + `end_timeslice_sendRecvA` (lift 2b's `handle_timeout_recvBA`) with its inputs carried from the record across the + head; the spec step from the entry state is `SIKXA` (head, fault, `sendIpc_recv_sikFA`, reschedule), the record is + rebuilt by `JStage3A.of_ipcStepUXA` (`P3/Stage3KEntrySendKeepA`). + +Named premises: [P] `hmax`, `htu`, `hconv` (lift 2b's), clock / budget as `chargeNoSendA`, `hCs`, `one` +(`numNodes = 1`: the receiver is on the current core). [J] `hXv7` (the J7 VSpace words are off the IPC buffers: `bufVs` for J7's `Pt`). Case +selectors: the current thread's timeout handler slot holds an endpoint cap (`hslot`, `hsl`, `hcap`, `hgc`) whose +endpoint has a waiting receiver (`he`, `hk`); the send condition (`hns : ¬ NoTimeoutA`). New module. +-/ +import Fl4ProofsA64.P3.Stage3SendQA +import Fl4ProofsA64.P3.Stage3KEntrySendKeepA +import Fl4ProofsA64.P3.ScEntrySendA +import Fl4ProofsA64.P3.Stage3TimeoutSenderA +import Fl4ProofsA64.P3.Stage3KEntryChargeA +import Fl4ProofsA64.P3.Stage3SendNarrowA +import Fl4ProofsA64.P3.Stage3SendRecvArmA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {A : Type} {AR' : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} + {κ : U64 → U64} {n : Nat} {J5 : JSlotA S} {R : Set U64 → JSlotA S} {s s' : S} {a : A} + {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- The fault words are per-step TCB words. -/ +theorem faultTcbStepA {T : Set U64} (hP : TcbPlaced T) {t b : U64} (ht : t ∈ T) (h : FaultWA t b) : + TcbStepWA T b := by + obtain ⟨K, hK, rfl⟩ := faultWA_eq h + have hKv : 3376 ≤ K.val ∧ K.val ≤ 3400 := by rcases hK with rfl | rfl | rfl | rfl <;> simp + have hv := field_val (hP t ht) (show K.val < 4096 by omega) + have hfit := (hP t ht).fits + refine ⟨t, ht, by omega, by omega, fun e => ?_, fun e => ?_⟩ + · have e' := congrArg UScalar.val e + have h1 := field_val (hP t ht) (show (3664#u64 : U64).val < 4096 by decide) + have h2 : (3664#u64 : U64).val = 3664 := rfl + omega + · have e' := congrArg UScalar.val e + have h1 : (runVcpuA t).val = t.val + (3672#u64 : U64).val := + field_val (hP t ht) (show (3672#u64 : U64).val < 4096 by decide) + have h2 : (3672#u64 : U64).val = 3672 := rfl + omega + +section +variable (J : JStage3A st AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7A st R) s a σ m) +include J + +theorem JStage3A.ntfnWait_sendXA {W : Prop} {t e d tf : U64} (ht : t ∈ Z.T) (htf : tf ∈ Z.T) (he : e ∈ E) (hd : d ∈ Z.T) + (hdb : L.mem s (Notif.tBlockA d) = e) + (w : WritesIn st s s' (fun b => (SendFpA st Z.T Rp Bf (fun σ s => MWrA (st := st) Z m σ s) σ s t e d W b ∨ + (∃ r ∈ Rp, r.val ≤ b.val ∧ b.val < r.val + 32) ∨ ∃ u ∈ Z.T, b = tReplyA u) ∨ FaultWA tf b)) : + NtfnWaitKeepA Z.T N (L.mem s) (L.mem s') := by + intro y hy hN + have hP := J.iplaced + have hya := hP.e.tcb y hy + have hfy := hya.fits + have notE : ∀ u ∈ Z.T, L.mem s (Notif.tBlockA u) = e → u ≠ y := fun u _ hu c => by + subst c + rw [hu] at hN + have := hP.ne e hN e he + omega + have key : ∀ (K : U64) (b : U64), (K.val = 3336 ∨ K.val = 3496 ∨ K.val = 3504) → b = wa y K → Aligned b → + L.mem s' b = L.mem s b := by + intro K b hK hb hal + have hbv : b.val = y.val + K.val := by rw [hb, wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have apart : ∀ u ∈ Z.T, u ≠ y → u.val + 4096 ≤ y.val ∨ y.val + 4096 ≤ u.val := fun u hu ne => by + have := J.sr.place.apart u y .tcb .tcb ((J.sr.thT u).2 hu) ((J.sr.thT y).2 hy) ne + have hsz : ObjKindA.size .tcb = 4096 := rfl + omega + refine w.mem_eq hal ?_ + rintro (((c | ⟨r, hr, c⟩ | ⟨-, c⟩ | c | c) | ⟨r, hr, c⟩ | ⟨u, hu, c⟩) | c) + · rcases c with c | ⟨u, hu, hub, c⟩ | ⟨r, c⟩ | c + · have := hP.e.et e he y hy; unfold EW at c; omega + · have := apart u hu (notE u hu hub) + have := (hP.e.tcb u hu).fits + unfold TL at c; omega + · have hfd := (hP.e.tcb d hd).fits + have h1 := regIdxA_lt r + rw [Endp.regA_val (hP.e.tcb d hd) h1] at c + have := apart d hd (notE d hd hdb) + omega + · have := hP.e.bt _ c y hy; omega + · have h1 := hP.e.rt r hr y hy + have h2 : b.val = r.val := by rw [c, Endp.rTcbA_val] + omega + · have h1 : b.val = t.val + 3328 := by rw [c]; exact Endp.val_st (hP.e.tcb t ht) + by_cases ety : t = y + · subst ety; omega + · have := apart t ht ety; omega + · have h1 : b.val = d.val + 3328 := by rw [c]; exact Endp.val_st (hP.e.tcb d hd) + have := apart d hd (notE d hd hdb) + omega + · rw [hb] at c + exact J.tcbWord_offMwrA hy (by omega) (by omega) c + · have := hP.e.rt r hr y hy + omega + · have h1 : b.val = u.val + 3360 := by rw [c]; exact Endp.val_reply (hP.e.tcb u hu) + by_cases euy : u = y + · subst euy; omega + · have := apart u hu euy; omega + · obtain ⟨K', hK', e'⟩ := faultWA_eq c + have hK'v : K'.val < 4096 ∧ 3376 ≤ K'.val ∧ K'.val ≤ 3400 := by + rcases hK' with rfl | rfl | rfl | rfl <;> simp + have hf := field_val (hP.e.tcb tf htf) hK'v.1 + rw [← e'] at hf + by_cases ety : tf = y + · subst ety; omega + · have := apart tf htf ety; omega + refine ⟨?_, ?_, ?_⟩ + · exact key 3336#u64 _ (by simp) (by unfold Notif.tBlockA; rfl) (Endp.a_blk hya).1 + · exact key 3504#u64 _ (by simp) (by rw [prevEp_eq]) (Endp.a_prev hya).1 + · exact key 3496#u64 _ (by simp) (by rw [nextEp_eq]) (Endp.a_next hya).1 + +end + +/-- **J7 across scheduler / IPC-object writes and buffer words** (`j57_stepNWA`'s J7 with `BufW`): the J7 VSpace words +are off the buffers (`hXv7`, [J] record edit #8's `bufVs` for J7's `Pt`). -/ +theorem JStage3A.j7_stepNUA {ar : machine.Arch A} {nA : U64} + (J : JStage3A st AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (w : WritesIn st s s' (fun b => SchedStepNWA (st := st) N E Rp Z m σ s b ∨ BufW Bf b)) + (hXv7 : ∀ Pt Ub, FrUb st Ub OS s → VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt → + ∀ b, VsR (L.ksb s) Pt b → ¬ BufW Bf b) + (σ' : Fl4SpecA64.State Unit) : J7A st (J7R2A st ar nA) Z H OS s' σ' m := by + have kb : L.ksb s' = L.ksb s := w.1 + have osk : OSKeep st OS s s' := fun y hy => by + have hfit := J.sys.ent.os.fit y hy + have key : ∀ o : U64, o.val < 32 → o.val % 8 = 0 → L.mem s' (wa y o) = L.mem s (wa y o) := by + intro o o1 o2 + have hv : (wa y o).val = y.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have hal : Aligned (wa y o) := J.sys.ent.os.al hy o1 o2 + have hin : InOS OS (wa y o) := ⟨y, hy, by omega, by omega⟩ + exact w.mem_eq hal (fun h => h.elim (J.os_offStepNA hal hin) (fun hb => J.bufOff.os _ hb hin)) + have e0 : wa y 0#u64 = y := by apply UScalar.eq_of_val_eq; rw [wa_valm]; simp + exact ⟨by have := key 0#u64 (by simp) (by simp); rwa [e0] at this, key 8#u64 (by simp) (by simp), + key 16#u64 (by simp) (by simp), key 24#u64 (by simp) (by simp)⟩ + refine J7PpKA.keepFr (st := st) _ _ _ _ _ σ σ' _ J.j7 kb osk ?_ + intro Pt Ub fu vr b hb + refine w.mem_eq (vr.al _ hb) (fun h => h.elim ?_ (hXv7 Pt Ub fu vr b hb)) + by_cases h : AsW (L.ksb s) b + · obtain ⟨o, o1, o2, -, e⟩ := h + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.sys.ent.ksw + have ev : b.val = (L.ksb s).val + o.val := by + rw [e, wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact J.asid_offStepNA (by omega) (by omega) + · exact J.vsr_offStepNA vr hb h + +end + +end + +end Fl4ProofsA64.P3 + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P3 Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Classical + +section +variable {i : ObjRef} {M N : ObjRef → Prop} {don : Bool} + +/-- **The charge head, the consumed reset and the fault of `t` as one `SIKXA` step.** -/ +theorem SIKXA.headFaultA {σ τ : State Unit} {q t : ObjRef} {x y : SchedContext} {n : Nat} {xt : Tcb} + (hq : σ.kheap q = some (.schedContext x n)) (hkt : σ.kheap t = some (.tcb xt)) (h : ScIpcA i don q x y) + (hqi : q ≠ idleScPtr) + (hτ : τ = { updKSc σ q (.schedContext y n) with consumedTime := 0 }) (f : Option Fault) : + SIKXA i M N don σ (updK τ t (.tcb { xt with tcbFault := f })) := by + subst hτ + have htq : t ≠ q := fun e => by rw [e, hq] at hkt; cases hkt + have rdy : RdyA σ → RdyA (updK { updKSc σ q (.schedContext y n) with consumedTime := 0 } t + (.tcb { xt with tcbFault := f })) := fun g => ⟨fun z k hz => by + have e1 : idleScPtr ≠ t := fun e => by + rw [← e] at hz; simp [updK] at hz + have e2 : idleScPtr ≠ q := fun e => hqi e.symm + exact g.1 z k (by simpa [updK, updKSc, e1, e2] using hz), fun hne => (hne rfl).elim⟩ + refine ⟨rfl, fun p => ?_, fun _ _ _ a => Or.inl a, fun _ a => a, Or.inl rfl, rdy⟩ + by_cases e1 : p = t + · subst e1 + refine Or.inr ⟨xt, { xt with tcbFault := f }, hkt, by simp [updK], rfl, rfl, rfl, rfl, rfl, Or.inl rfl, + Or.inl rfl, rfl, rfl⟩ + · refine Or.inl ?_ + by_cases e2 : p = q + · subst e2 + have e : (updK { updKSc σ p (.schedContext y n) with consumedTime := 0 } t + (.tcb { xt with tcbFault := f })).kheap p = some (.schedContext y n) := by + simp [updK, updKSc, e1] + rw [e, hq] + exact .sc _ _ _ h + · have e : (updK { updKSc σ q (.schedContext y n) with consumedTime := 0 } t + (.tcb { xt with tcbFault := f })).kheap p = σ.kheap p := by + simp [updK, updKSc, e1, e2] + rw [e] + exact .same _ + +/-- `rescheduleRequired` with the timer flag is an `SIKA` step. -/ +theorem reschedReprog_sikA {σ τ : State Unit} (h : ((), τ) ∈ (reschedReprogA σ).1) : SIKA i M N don σ τ := by + unfold reschedReprogA at h + obtain ⟨⟨u4, σ4⟩, h4, h5⟩ := mem_bindA.1 h + have e : τ = { σ4 with reprogramTimer := true } := + (Prod.mk.inj (show (((), τ) : Unit × State Unit) = ((), { σ4 with reprogramTimer := true }) from h5)).2 + subst e + exact (rescheduleRequired_sikA σ _ h4).trans ⟨rfl, fun _ => .same _, fun _ _ _ a => Or.inl a, fun _ a => a, Or.inl rfl, id⟩ + +end + +/-- `rescheduleRequired` with the timer flag keeps the scheduler frame. -/ +theorem reschedReprog_frameA {σ τ : State Unit} (h : ((), τ) ∈ (reschedReprogA σ).1) : SchedFrameA σ τ := by + unfold reschedReprogA at h + obtain ⟨⟨u4, σ4⟩, h4, h5⟩ := mem_bindA.1 h + obtain ⟨hQ, -⟩ := rescheduleRequired_memA h4 + have e : τ = { σ4 with reprogramTimer := true } := + (Prod.mk.inj (show (((), τ) : Unit × State Unit) = ((), { σ4 with reprogramTimer := true }) from h5)).2 + subst e + have hQ1 : σ4 = { σ with readyQueues := σ4.readyQueues, schedulerAction := σ4.schedulerAction } := hQ.1 + have k : σ4.kheap = σ.kheap := by rw [hQ1] + have i1 : σ4.interruptStates = σ.interruptStates := by rw [hQ1] + have i2 : σ4.interruptIrqNode = σ.interruptIrqNode := by rw [hQ1] + have i3 : σ4.archState = σ.archState := by rw [hQ1] + have i4 : σ4.cdt = σ.cdt := by rw [hQ1] + have i5 : σ4.isOriginalCap = σ.isOriginalCap := by rw [hQ1] + have i6 : σ4.machineState = σ.machineState := by rw [hQ1] + exact SchedFrameA.of_eq k i1 i2 (by rw [show ({ σ4 with reprogramTimer := true } : State Unit).archState = + σ4.archState from rfl, i3]) i4 i5 (by rw [show ({ σ4 with reprogramTimer := true } : State Unit).machineState = + σ4.machineState from rfl, i6]) (by rw [show ({ σ4 with reprogramTimer := true } : State Unit).archState = + σ4.archState from rfl, i3]) + +/-- `rescheduleRequired` with the timer flag keeps `SchedValidA`. -/ +theorem reschedReprog_svA {σ τ : State Unit} (hv : SchedValidA σ) (h : ((), τ) ∈ (reschedReprogA σ).1) : + SchedValidA τ := by + unfold reschedReprogA at h + obtain ⟨⟨u4, σ4⟩, h4, h5⟩ := mem_bindA.1 h + have e : τ = { σ4 with reprogramTimer := true } := + (Prod.mk.inj (show (((), τ) : Unit × State Unit) = ((), { σ4 with reprogramTimer := true }) from h5)).2 + subst e + have HF : HT FIA (rescheduleRequired (Z := Unit)) (fun _ σ' => FIA σ') := + HT.mono (resched_fia (fun _ => True) (fun _ _ _ => trivial) (fun _ _ _ => trivial)) + (fun _ h => ⟨h, trivial⟩) (fun _ _ h => h.1) + have F := HF σ (SchedValidA.toFIA hv) _ h4 + have h4' := SchedValidB.toA F.1 F.2 + exact ⟨h4'.rq, h4'.act, h4'.blk, h4'.rls⟩ + +/-- `SchedValidA` across a TCB's fault rewritten. -/ +theorem SchedValidA.faultA {σ : State Unit} (h : SchedValidA σ) {p : ObjRef} {x : Tcb} + (hk : σ.kheap p = some (.tcb x)) (f : Option Fault) : SchedValidA (updK σ p (.tcb { x with tcbFault := f })) := by + have far : ∀ q, q ≠ p → (updK σ p (.tcb { x with tcbFault := f })).kheap q = σ.kheap q := fun q hq => by + simp [updK, hq] + have hp : (updK σ p (.tcb { x with tcbFault := f })).kheap p = some (.tcb { x with tcbFault := f }) := by + simp [updK] + have scA : ∀ c, ScActiveAtA σ c → ScActiveAtA (updK σ p (.tcb { x with tcbFault := f })) c := by + rintro c ⟨y, k, hy, ha⟩ + exact ⟨y, k, by rw [far c (fun e => by rw [e, hk] at hy; cases hy)]; exact hy, ha⟩ + have actT : ∀ u, ActTcbA σ u → ActTcbA (updK σ p (.tcb { x with tcbFault := f })) u := by + rintro u ⟨x', sc, e1, e2, a⟩ + by_cases e : u = p + · subst e; rw [hk] at e1; cases e1 + exact ⟨_, sc, hp, e2, scA sc a⟩ + · exact ⟨x', sc, by rw [far u e]; exact e1, e2, scA sc a⟩ + have back : ∀ u x', (updK σ p (.tcb { x with tcbFault := f })).kheap u = some (.tcb x') → + ∃ x0, σ.kheap u = some (.tcb x0) ∧ x0.tcbState = x'.tcbState ∧ x0.tcbSchedContext = x'.tcbSchedContext := by + intro u x' hu + by_cases e : u = p + · subst e; rw [hp] at hu; cases hu + exact ⟨x, hk, rfl, rfl⟩ + · rw [far u e] at hu + exact ⟨x', hu, rfl, rfl⟩ + refine ⟨fun d q u hm => ?_, fun e1 e2 => ?_, fun u x' sc e1 e2 e3 => ?_, fun u hu => ?_⟩ + · obtain ⟨a, nr⟩ := h.rq d q u hm + exact ⟨actT u a, nr⟩ + · obtain ⟨a, nr⟩ := h.act e1 e2 + exact ⟨actT _ a, nr⟩ + · obtain ⟨x0, h0, s0, c0⟩ := back u x' e1 + exact scA sc (h.blk u x0 sc h0 (s0 ▸ e2) (c0 ▸ e3)) + · exact actT u (h.rls u hu) + +/-- `RqDomPrio` across a TCB's fault rewritten. -/ +theorem rqDomPrio_faultA {σ : State Unit} (h : RqDomPrio σ) {p : ObjRef} {x : Tcb} + (hk : σ.kheap p = some (.tcb x)) (f : Option Fault) : RqDomPrio (updK σ p (.tcb { x with tcbFault := f })) := by + intro g d q y hy hm + by_cases e : g = p + · subst e + simp [updK] at hy + subst hy + exact h g d q x hk hm + · exact h g d q y (by simpa [updK, e] using hy) hm + +end Fl4ProofsA64.P3.Sched + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {κ : U64 → U64} + {nA : U64} {s : S} {a : A} {σ : State Unit} {m : McState} + +set_option maxRecDepth 4096 in +set_option maxHeartbeats 1000000 in +/-- **The record across `charge_budget … true` taking the timeout send to a waiting receiver**, free of the +scheduler action (module docstring): the entry facts are replaced by "the current thread is in no ready queue" and A64 +P1's post `hP1`; the running-VM test is read off the spec's send condition. -/ +theorem JStage3A.chargeSendRecvCoreA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar nA)) s a σ m) + (noSwitch : ∀ u, σ.schedulerAction ≠ .switchThread u) (I : CurInvA σ) (Idle : ActIdleA σ) + (hcore : L.mem s (scCoreA (L.cscW s)) = L.cpu s) (hni : σ.curSc ≠ idleScPtr → NotIdleScA m σ.curSc) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (one : fl4A64Platform.numNodes = 1) + (hag : fl4MinBudget = MIN_BUDGET) (hMin : Fl4MinBudgetAgreeA ar a) + (hLimit : sporadic.max_release_time ar a ⦃ v => v.bv = fl4MaxReleaseTime ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) (hconv : Fl4TicksToUsLegacyAgree) + (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + (used : U64) + (hP1 : sched.charge_budget st ar s a used true ⦃ r => SysL st H Z.Cs OS ia ie r.1 ∧ SymL st H Z.Cs r.1 ∧ + BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac r.1 ⦄) + (hXv7 : ∀ Pt Ub, FrUb st Ub OS s → VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt → + ∀ b, VsR (L.ksb s) Pt b → ¬ BufW Bf b) + (hns : ¬ NoTimeoutA used.bv σ) + {xt : Tcb} (hkt : σ.kheap σ.curThread = some (.tcb xt)) + {slot ep hbg : U64} {sd rv g gr : Bool} + (hslot : obj.Tcb.slot (L.curW s) layout.tcb.SLOT_TIMEOUT_HANDLER = ok slot) (hsl : IsSlotA Z.th slot) + (hcap : capA st s slot = .Endpoint ep hbg sd rv g gr) + (hgc : tcbCnodeMap xt (tcbCnodeIndex 4) = some (.endpointCap (specPtr ep) hbg.bv (rightsOfA sd rv g gr))) + (he : ep ∈ E) {d : U64} {q : List ObjRef} + (hk : σ.kheap (specPtr ep) = some (.endpoint (.recvEP (specPtr d :: q)))) : + sched.charge_budget st ar s a used true ⦃ r => ∃ τ, + fl4ChargeBudget (Z := Unit) used.bv true σ = ({((), τ)}, False) ∧ r.2 = a ∧ + JStage3QA st ar AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes nA m r.1 r.2 τ ∧ + τ.curThread = σ.curThread ∧ ∀ x, τ.kheap τ.curThread = some (.tcb x) → runnable x.tcbState = false ⦄ := by + have E0 := J.yieldQueueA + have P : EntryBudgetPlaceA st m κ s σ := ⟨J.sinv.mc, (J.sr.scCore _ J.sr.cscC).symm.trans hcore, hni, hnq⟩ + set t := L.curW s with ht_def + have htZ : t ∈ Z.T := J.sr.curT + have hσct : σ.curThread = specPtr t := J.sr.sch.curThread + have hkt0 : σ.kheap (specPtr t) = some (.tcb xt) := hσct ▸ hkt + -- the send condition after the head + obtain ⟨σh, hh, hsh, xh, yh', nh', hxh, hyh, hnsa⟩ : ∃ σ1, fl4ChargeHead (Z := Unit) σ.curSc used.bv σ = + ({((), σ1)}, False) ∧ schedulable σ.curThread { σ1 with consumedTime := 0 } = true ∧ + ∃ x y n, σ1.kheap σ.curThread = some (.tcb x) ∧ σ1.kheap σ.curSc = some (.schedContext y n) ∧ + ¬ NoSendAtA { σ1 with consumedTime := 0 } σ.curThread y x := by + unfold NoTimeoutA at hns + push_neg at hns + exact hns + unfold NoSendAtA at hnsa + push_neg at hnsa + obtain ⟨hper, hvm, -⟩ := hnsa + have hsch : ∀ σ1, fl4ChargeHead (Z := Unit) σ.curSc used.bv σ = ({((), σ1)}, False) → + schedulable σ.curThread { σ1 with consumedTime := 0 } = true := fun σ1 h1 => by + have e : σ1 = σh := + (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst (h1.symm.trans hh)))).2 + rw [e]; exact hsh + -- the receiver + obtain ⟨-, xs, -, hq', hQ, hB⟩ := J.ep.at_ep he hk + have hdx : d ∈ xs := by + have : specPtr d ∈ xs.map specPtr := by rw [← hq']; exact List.mem_cons_self + obtain ⟨y, hy, e⟩ := List.mem_map.1 this + rwa [specPtr_inj e] at hy + have hd : d ∈ Z.T := hQ.mem d hdx + have hdst : (L.mem s (stA d)).val = 3 := (hB d hdx).1 + have hdb : L.mem s (Notif.tBlockA d) = ep := (hB d hdx).2 + obtain ⟨xd, hkd, rd⟩ := J.ep.tcb d hd + obtain ⟨e', rep, dd, hstd⟩ := tsCode_three (rd.st.symm.trans hdst) + have hdth : Z.th d = some .tcb := (J.sr.thT d).2 hd + have hal := J.bufAlign0A hd hkd true + obtain ⟨xt', hkt', -, hipc⟩ := I.tcb + have ext : xt' = xt := KernelObject.tcb.inj (Option.some.inj (hkt'.symm.trans hkt)) + subst xt' + have hdt : specPtr d ≠ specPtr t := fun e => by + rw [e, hkt0] at hkd; cases hkd; rw [hstd] at hipc; simp [ipcQueuedThreadState] at hipc + obtain ⟨x0, n0, hx0⟩ := I.sc + have hctcs : specPtr t ≠ σ.curSc := fun e => by rw [e, hx0] at hkt0; cases hkt0 + have hdcs : specPtr d ≠ σ.curSc := fun e => by rw [e, hx0] at hkd; cases hkd + have hepcs : specPtr ep ≠ σ.curSc := fun e => by rw [e, hx0] at hk; cases hk + have hept : specPtr ep ≠ specPtr t := fun e => by rw [e, hkt0] at hk; cases hk + let M : ObjRef → Prop := fun o => o = specPtr t ∨ o = specPtr d + let Nq : ObjRef → Prop := fun o => o = specPtr d + let Post : S → State Unit → Prop := fun s3 τ3 => + SIKXA σ.idleThread M Nq false σ τ3 ∧ UFrA (BufBytesA (ipcBufOfA true xd)) σ τ3 ∧ SchedValidA τ3 ∧ + StIs (specPtr d) .running τ3 ∧ + (StIs (specPtr t) .inactive τ3 ∨ ∃ r, StIs (specPtr t) (.blockedOnReply r) τ3) ∧ + (∀ p, p ≠ specPtr t → ∀ x, σ.kheap p = some (.tcb x) → ∃ y, τ3.kheap p = some (.tcb y) ∧ + y.tcbFault = x.tcbFault) ∧ + EpStateRel Z.T E Rp Bf (L.mem s3) τ3 ∧ + WritesIn st s s3 (fun b => TmoFpA st Z m Rp Bf σ s t ep d b ∨ FaultWA t b) ∧ + runnable xt.tcbState = true ∧ QSendA σ τ3 ∧ τ3.consumedTime = 0 ∧ + xt.tcbState ≠ .ext .runningVM + have SEND : ∀ s2 τ (sc : U64) (x : Tcb) (y : SchedContext) (n : Nat), ChargeHeadA st Z m κ used.bv s σ s2 τ → + sc ∈ Z.C → L.cscW s2 = sc → σ.curSc = scNameA m sc → sc ≠ 0#u64 → L.mem s2 (scA (L.curW s2)) = sc → + τ.kheap (specPtr (L.curW s2)) = some (.tcb x) → x.tcbSchedContext = some (scNameA m sc) → + τ.kheap (scNameA m sc) = some (.schedContext y n) → + sched.end_timeslice st ar s2 a true ⦃ r => r.2 = a ∧ ∃ τ3, + fl4EndTimeslice (Z := Unit) true τ = ({((), τ3)}, False) ∧ + (∃ Q' R', YieldQueueRelA (st := st) { Z with Q := Q', R := R' } m κ r.1 τ3) ∧ Post r.1 τ3 ⦄ := by + intro s2 τ sc x y n CH hsc hcs hσsc sc0 hWordS hkx hBack h2 + obtain ⟨yh, nh, hHead, hτ⟩ := CH.head + have hct2 : L.curW s2 = t := specPtr_inj (CH.sr.sch.curThread.symm.trans (CH.cur.trans hσct)) + rw [hct2] at hkx hWordS + have τkh : τ.kheap = Function.update σ.kheap σ.curSc (some (.schedContext yh nh)) := by rw [hτ]; rfl + have τfar : ∀ p, p ≠ σ.curSc → τ.kheap p = σ.kheap p := fun p hp => by + rw [τkh, Function.update_of_ne hp] + have τcsc : τ.kheap σ.curSc = some (.schedContext yh nh) := by rw [τkh, Function.update_self] + have hkτ : τ.kheap (specPtr t) = some (.tcb xt) := by rw [τfar _ hctcs]; exact hkt0 + have ex : x = xt := KernelObject.tcb.inj (Option.some.inj (hkx.symm.trans hkτ)) + subst x + rw [← hσsc] at h2 + obtain ⟨ey, en⟩ := KernelObject.schedContext.inj (Option.some.inj (h2.symm.trans τcsc)) + subst y n + -- the head's state is `σh` + have eσh : σh = updKSc σ σ.curSc (.schedContext yh nh) := + (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst (hh.symm.trans hHead)))).2 + have exh : xh = xt := by + rw [eσh, updKSc_kheap, Function.update_of_ne (hσct ▸ hctcs), hkt] at hxh + exact (KernelObject.tcb.inj (Option.some.inj hxh)).symm + have eyh : yh' = yh := by + rw [eσh, updKSc_kheap, Function.update_self] at hyh + exact (KernelObject.schedContext.inj (Option.some.inj hyh)).1.symm + subst xh yh' + -- the SC keep at the head + obtain ⟨z', hz', hrep', hyf', htcb', hrm', hne'⟩ := CH.keep.1 _ x0 n0 hx0 + rw [τcsc] at hz' + obtain ⟨e1, e2⟩ := KernelObject.schedContext.inj (Option.some.inj hz') + subst z' n0 + have hact0 : scActive x0 = true := CH.act x0 nh hx0 + have hScIpc : ScIpcA σ.idleThread false σ.curSc x0 yh := + ⟨hyf', hrm', hne' hact0, fun _ => ⟨htcb', hrep'⟩, Or.inl htcb', fun _ => hrep'⟩ + -- the current thread + have hRunS : schedulable (specPtr t) τ = true := by have := CH.run; rwa [CH.cur, hσct] at this + obtain ⟨-, nrelτ, xr, hxr, hrunr⟩ := schedulable_actA hRunS + have exr : xr = xt := KernelObject.tcb.inj (Option.some.inj (hxr.symm.trans hkτ)) + subst xr + have hnI : specPtr t ≠ σ.idleThread := fun e => by + obtain ⟨xi, hxi, hsi⟩ := J.sinv.idleTcb + rw [← e, hkt0] at hxi; cases hxi; rw [hsi] at hrunr; cases hrunr + have hxsc : xt.tcbSchedContext = some σ.curSc := by rw [hσsc]; exact hBack + obtain ⟨z0, j0, hz0, ho0⟩ := J.sr.inv.back _ _ _ hkt0 hxsc + obtain ⟨e3, e4⟩ := KernelObject.schedContext.inj (Option.some.inj (hz0.symm.trans hx0)) + subst z0 j0 + have hcsNI : σ.curSc ≠ idleScPtr := fun e => by + obtain ⟨xi, yi, ki, -, -, hyi, hti⟩ := J.sinv.idleSc + rw [← e, hx0] at hyi; cases hyi + exact hnI (Option.some.inj (ho0.symm.trans hti)) + have hname : scNameA m sc = specPtr sc := by + have h' := hσsc + unfold scNameA at h' ⊢ + by_cases hi : specPtr sc = m.idleScOf m.curCore + · rw [if_pos hi] at h'; exact absurd h' hcsNI + · rw [if_neg hi] + -- the scheduler frame and the facts at the head's state + have FR : SchedFrameA σ τ := by + rw [hτ] + exact PreO.trans (SchedFrameA.upd_sc (y' := yh) hx0) (SchedFrameA.of_eq rfl rfl rfl rfl rfl rfl rfl rfl) + have tw2 : ∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → + (K.val ≠ 3464 ∧ K.val ≠ 3480 ∧ K.val ≠ 3488 ∧ K.val ≠ 3592) → L.mem s2 (wa u K) = L.mem s (wa u K) := + fun u hu K h1 h2 h3 => CH.frame.wr.mem_eq (field_aligned (J.sr.core.tcbs u hu) h1 h2) + (tcbNotMWrA J.sr.toSchedSRN hu h1 h3) + obtain ⟨-, HE2, -, -, -, -⟩ := mcsStepFramesA J.iplaced J.ipcPlace J.sr.toSchedSRN CH.frame FR.2.2 + (fun q' y' k' hy' => by + obtain ⟨y'', hy'', hr, hyf, -⟩ := CH.keep.1 q' y' k' hy' + exact ⟨y'', hy'', hr, hyf⟩) + J.ntfn J.ep J.reply J.mcp J.yieldRel + have slotK : ∀ y, IsSlotA Z.th y → capAtA (L.mem s2) y = capAtA (L.mem s) y := by + intro y hy + obtain ⟨h32, hfit⟩ := IsSlotA.al32 J.sr.place hy + have al : Aligned y := by unfold Aligned; omega + have v8 : (wa y 8#u64).val = y.val + 8 := by + rw [wa_valm] + have h8 : (8#u64 : U64).val = 8 := by simp + omega + have al8 : Aligned (wa y 8#u64) := by unfold Aligned; rw [v8]; omega + unfold capAtA + rw [CH.frame.wr.mem_eq al (fun h => J.slot_offStepA hy (le_refl _) (by omega) (Or.inl h)), + CH.frame.wr.mem_eq al8 (fun h => J.slot_offStepA hy (by omega) (by omega) (Or.inl h))] + have hSt2 : StateRelA Z.th (L.mem s2) τ := by + have h := J.j.1.st.congrA (mem' := L.mem s2) fun y hy => slotK y (slotW_isSlot J.sr.place hy) + refine ⟨fun p r hp => ?_, fun u hu => ?_⟩ + · obtain ⟨cs, h1, h2', h3⟩ := h.cnode p r hp + have hne : specPtr p ≠ σ.curSc := fun e => by rw [e, hx0] at h1; cases h1 + exact ⟨cs, by rw [τfar _ hne]; exact h1, h2', h3⟩ + · obtain ⟨x1, h1, h2'⟩ := h.tcb u hu + exact ⟨x1, CH.keep.2 _ _ h1, h2'⟩ + have hV2 : SlotsValidA Z.th (L.mem s2) := fun y hy => by rw [slotK y hy]; exact J.j.1.cv y hy + have hC2 : BufCover st Z.T Bf s2 τ := + BufCover.frameK J.bufCover (fun u hu => by rw [ibWordA_eq]; exact tw2 u hu (3512#u64) (by decide) (by decide) (by decide)) + (fun u hu => by + show capAtA (L.mem s2) (sbWordA u) = capAtA (L.mem s) (sbWordA u) + unfold capAtA + rw [sbWordA8_eq, sbWordA_eq, tw2 u hu (64#u64) (by decide) (by decide) (by decide), + tw2 u hu (72#u64) (by decide) (by decide) (by decide)]) + (fun u hu => let ⟨x1, hx1, _⟩ := J.ep.tcb u hu; ⟨x1, hx1⟩) + (fun p x1 hx1 => ⟨x1, CH.keep.2 p x1 hx1, rfl, rfl⟩) + have hN2 : BufNativeA Z.T (L.mem s2) := + J.bufNative.writesIn J.iplaced.e.tcb CH.frame.wr fun u hu K hK => + tcbNotMWrA J.sr.toSchedSRN hu (by rcases hK with rfl | rfl <;> decide) + (by rcases hK with rfl | rfl <;> decide) + have hkdτ : τ.kheap (specPtr d) = some (.tcb xd) := by rw [τfar _ hdcs]; exact hkd + have hkτ' : τ.kheap (specPtr ep) = some (.endpoint (.recvEP (specPtr d :: q))) := by + rw [τfar _ hepcs]; exact hk + have hu2 : ∀ rp, ipcBufOfA true xd = some rp → ∀ k, k < 128 → inUserFrame (bufAddr rp k) τ := by + intro rp hrp k hk' + have h0 := J.bufFrameA hd hkd true rp hrp k hk' + have h1 := inUserFrame_updK (o' := .schedContext yh nh) hx0 (fun _ h => by cases h) _ h0 + rw [hτ]; exact h1 + have hrep2 : RepOKA τ (specPtr d) := by + intro x1 hx1 e0 r dd' hst' + rw [τfar _ hdcs] at hx1 + obtain ⟨xr', hkr, hbk⟩ := J.repOKA hd x1 hx1 e0 r dd' hst' + exact ⟨xr', by rw [τfar _ (fun e => by rw [e, hx0] at hkr; cases hkr)]; exact hkr, hbk⟩ + have hon2 : L.mem s2 (affA d) = L.cpu s2 := by + have x1 := hCs _ (CH.sr.core.aff d hd) + have y1 := hCs _ CH.sr.sch.cpuCs + exact UScalar.eq_of_val_eq (by omega) + have hni2 : ∀ c < fl4A64Platform.numNodes, specPtr t ≠ m.idleThreadOf c := fun c hc e => by + obtain ⟨y1, hy1, hs1⟩ := J.sidle.f3 c hc + rw [← e, hkt0] at hy1; cases hy1; rw [hs1] at hrunr; cases hrunr + have hSnd : SndRunA τ (specPtr t) xt := by + refine ⟨hrunr, ⟨fun dm pr hm => ?_, nrelτ⟩, fun u hu => ?_, ?_⟩ + · rw [hτ] at hm + exact P.notReady dm pr (by rw [hσct]; exact hm) + · rw [hτ] at hu; exact absurd hu (noSwitch u) + · rw [hτ]; exact hnI + have hD2 : DonA τ (specPtr t) xt := by + intro c hc + rw [hxsc] at hc + cases hc + refine ⟨hcsNI, fun y' k' hy' => ?_⟩ + rw [τcsc] at hy'; cases hy' + rw [htcb', ho0] + have H2' : SchedSRB st Z m κ s2 τ (specPtr t) := by rw [← hσct, ← CH.cur]; exact CH.sr + have hcap2 : obj.Slot.cap st slot s2 = ok (.Endpoint ep hbg sd rv g gr) := by + rw [capA_eq, show capA st s2 slot = capA st s slot from slotK slot hsl, hcap] + have hgc2 : getCap (Z := Unit) (getTcbTimeoutHandlerPtr (specPtr t)) τ = + ({(.endpointCap (specPtr ep) hbg.bv (rightsOfA sd rv g gr), τ)}, False) := getCap_tcb_eqA hkτ hgc + refine WP.spec_mono (end_timeslice_sendRecvA (st := st) (ar := ar) (N := N) a J.ipcPlace J.iplaced.e HE2 + J.sr.place hSt2 hV2 hC2 hN2 J.slotOffEwA hmax htu hconv H2' CH.stq CH.mcp CH.yr htZ hsc sc0 hct2 hcs + (CH.csc.trans hσsc) hname hWordS hkτ hBack hvm hSnd hD2 (by rw [← hσsc]; exact τcsc) hper hslot hcap2 hgc2 + he hkτ' hd hkdτ hu2 hal hrep2 hon2 hni2) ?_ + rintro ⟨s3, a3⟩ ⟨ha3, τ3, het, -, hsi, hQ3, HE3, hw, -⟩ + refine ⟨ha3, τ3, het, hQ3, ?_⟩ + -- the spec steps from the faulted state + let x1 : Tcb := { xt with tcbFault := some (.timeout yh.scBadge) } + let σ1 := updK τ (specPtr t) (.tcb x1) + have hsi' : sendIpc (Z := Unit) true false hbg.bv g gr false (specPtr t) (specPtr ep) σ1 = + ({((), τ3)}, False) := hsi + have hmem : ((), τ3) ∈ (sendIpc (Z := Unit) true false hbg.bv g gr false (specPtr t) (specPtr ep) σ1).1 := by + rw [hsi']; exact Set.mem_singleton _ + have far1 : ∀ p, p ≠ specPtr t → σ1.kheap p = τ.kheap p := fun p hp => by simp [σ1, updK, hp] + have hkt1 : σ1.kheap (specPtr t) = some (.tcb x1) := by simp [σ1, updK] + have hk1 : σ1.kheap (specPtr ep) = some (.endpoint (.recvEP (specPtr d :: q))) := by + rw [far1 _ hept]; exact hkτ' + have hkd1 : σ1.kheap (specPtr d) = some (.tcb xd) := by rw [far1 _ hdt]; exact hkdτ + have hSt1 : StateRelA Z.th (L.mem s2) σ1 := by + refine ⟨fun p r hp => ?_, fun u hu => ?_⟩ + · obtain ⟨cs, h1, h2', h3⟩ := hSt2.cnode p r hp + have hne : specPtr p ≠ specPtr t := fun e => by rw [e, hkτ] at h1; cases h1 + exact ⟨cs, by rw [far1 _ hne]; exact h1, h2', h3⟩ + · obtain ⟨x2, h1, h2'⟩ := hSt2.tcb u hu + by_cases e : specPtr u = specPtr t + · rw [e] at h1 + have e5 : x2 = xt := KernelObject.tcb.inj (Option.some.inj (h1.symm.trans hkτ)) + subst x2 + exact ⟨x1, by rw [e]; exact hkt1, h2'⟩ + · exact ⟨x2, by rw [far1 _ e]; exact h1, h2'⟩ + have hD1 : DonA σ1 (specPtr t) x1 := fun sc' hs' => + ⟨(hD2 sc' hs').1, fun y' k hy' => (hD2 sc' hs').2 y' k (by + have hne : sc' ≠ specPtr t := fun e => by rw [e, hkt1] at hy'; cases hy' + rwa [far1 _ hne] at hy')⟩ + have hdI : specPtr d ≠ σ.idleThread := fun e => by + obtain ⟨xi, hxi, hsi'⟩ := J.sinv.idleTcb + rw [← e, hkd] at hxi; cases hxi; rw [hstd] at hsi'; cases hsi' + have K2 : SIKA σ.idleThread M Nq false σ1 τ3 := + sendIpc_recv_sikFA (i := σ.idleThread) (M := M) (N := Nq) J.sr.place s2 hSt1 hV2 hdth hk1 hkt1 hkd1 hal + true false hbg.bv g gr false (Or.inr ⟨_, rfl⟩) (Or.inl rfl) (Or.inr rfl) rfl hnI hdI hD1 + (fun h => by cases h) (fun _ => rfl) _ hmem + have Qs : QSendA σ τ3 := by + have Qh : QKeepA σ τ := + (QKeepA.upd hx0 (QObjA.ofSc (y := yh) fun h => + ⟨by rwa [hrep'] at h, fun a => (scActive_of_maxA hrm').trans a⟩)).trans (QKeepA.of_kheap τkh ⟨by rw [hτ]; rfl, by rw [hτ]; rfl, by rw [hτ]; rfl, by rw [hτ]; rfl⟩) + have Qf : QKeepA τ σ1 := (QKeepA.upd hkτ (QObjA.ofTcb (y := x1) rfl)).trans (QKeepA.of_kheap rfl ⟨rfl, rfl, rfl, rfl⟩) + have Q2 : QSendA σ1 τ3 := + sendIpc_recv_qsA (i := σ.idleThread) (M := M) (N := Nq) J.sr.place s2 hSt1 hV2 hdth hk1 hkt1 hkd1 hal + true false hbg.bv g gr false (Or.inr ⟨_, rfl⟩) (Or.inl rfl) (Or.inr rfl) rfl hnI hdI hD1 + (fun h => by cases h) (fun h => by cases h) + (fun e' r dd h => by + obtain ⟨xr, k, b⟩ := hrep2 xd hkdτ e' r dd h + exact ⟨xr, by rw [far1 _ (fun e => by rw [e, hkτ] at k; cases k)]; exact k, by rw [b]; simp⟩) _ hmem + exact ((Qh.trans Qf).toSend).trans Q2 + have U2 : UFrA (BufBytesA (ipcBufOfA true xd)) σ1 τ3 := + sendIpc_recv_ufrFA J.sr.place s2 hSt1 hV2 hdth hk1 hkt1 hkd1 hal true false hbg.bv g gr false + (Or.inr ⟨_, rfl⟩) _ hmem + have rdy : IdleRdyA σ → IdleRdyA τ := fun g z k h => by + have ht : τ.curTime = σ.curTime := by rw [hτ]; rfl + rw [ht] + rw [τkh, Function.update_of_ne (Ne.symm hcsNI)] at h + exact g z k h + have SH : ChargeShapeA σ τ := + ⟨CH.cur, by rw [hτ]; rfl, CH.csc, by rw [hτ]; rfl, ⟨yh, nh, τkh⟩, + Or.inl ⟨by rw [hτ]; rfl, by rw [hτ]; rfl, by rw [hτ]; rfl⟩, rdy, + ⟨by rw [hτ]; rfl, by rw [hτ]; rfl, by rw [hτ]; rfl, by rw [hτ]; rfl⟩⟩ + have SV1 : SchedValidA σ1 := Sched.SchedValidA.faultA (J.schedValid.chargeA SH CH.keep FR) hkτ _ + have RQ1 : RqDomPrio σ1 := rqDomPrio_faultA (fun g' d' q'' y' hy' hm => + rqDomPrio_of_schedA J.sr g' d' q'' y' (chargeTcbBackA CH.keep FR hy') (by rw [hτ] at hm; exact hm)) hkτ _ + have actT1 : ActTcbA σ1 (specPtr t) := + ⟨x1, σ.curSc, hkt1, hxsc, yh, nh, by rw [far1 _ hctcs.symm]; exact τcsc, + (scActive_of_maxA hrm').trans hact0⟩ + have hbr : (false = false ∧ false = false ∧ x1.tcbFault = none) ∨ + ((false = true ∨ x1.tcbFault ≠ none) ∧ ((g = false ∧ gr = false) ∨ rep = none)) ∨ + ((false = true ∨ x1.tcbFault ≠ none) ∧ (g = true ∨ gr = true) ∧ rep ≠ none ∧ specPtr t ≠ specPtr d ∧ + ActTcbA σ1 (specPtr t) ∧ (false = true → xd.tcbSchedContext = none)) ∨ + ((false = true ∨ x1.tcbFault ≠ none) ∧ (g = true ∨ gr = true) ∧ rep ≠ none ∧ false = true ∧ + specPtr t ≠ specPtr d ∧ xd.tcbSchedContext ≠ none ∧ ActTcbA σ1 (specPtr t)) := by + by_cases hg' : g = true ∨ gr = true + · by_cases hr : rep = none + · exact Or.inr (Or.inl ⟨Or.inr (by simp [x1]), Or.inr hr⟩) + · exact Or.inr (Or.inr (Or.inl ⟨Or.inr (by simp [x1]), hg', hr, hdt.symm, actT1, fun h => by cases h⟩)) + · refine Or.inr (Or.inl ⟨Or.inr (by simp [x1]), Or.inl ?_⟩) + simp only [not_or, Bool.not_eq_true] at hg' + exact hg' + have SV3 : SchedValidA τ3 := + sendIpc_recv_schedValidFA J.sr.place s2 hSt1 hV2 hdth hk1 hkt1 hkd1 hal true false hbg.bv g gr false + (Or.inr ⟨_, rfl⟩) hstd (hbr.imp id (Or.imp id (Or.imp id Or.inl))) SV1 RQ1 _ hmem + have run3 : StIs (specPtr d) .running τ3 := sendIpc_recv_runningA hk1 true false hbg.bv g gr false _ hmem + have snd3 := sendIpc_fault_senderA J.sr.place s2 hSt1 hV2 hdth hk1 hkt1 hkd1 hal hstd hdt.symm + (by simp [x1]) true hbg.bv g gr (Or.inr ⟨_, rfl⟩) _ hmem + have K13 : SIKXA σ.idleThread M Nq false σ τ3 := + (SIKXA.headFaultA hx0 hkt0 hScIpc hcsNI hτ _).transS K2 + have U13 : UFrA (BufBytesA (ipcBufOfA true xd)) σ τ3 := + PreO.trans (PreO.trans (UFrA.of_fr FR) (UFrA.of_fr (SchedFrameA.upd_tcb (x' := x1) hkτ ⟨rfl, rfl, rfl⟩))) U2 + have fault3 : ∀ p, p ≠ specPtr t → ∀ x', σ.kheap p = some (.tcb x') → ∃ y', τ3.kheap p = some (.tcb y') ∧ + y'.tcbFault = x'.tcbFault := fun p hp x' hx' => by + have hpcs : p ≠ σ.curSc := fun e => by rw [e, hx0] at hx'; cases hx' + obtain ⟨y', hy', g'⟩ := K2.tcbF (show σ1.kheap p = some (.tcb x') by rw [far1 p hp, τfar p hpcs]; exact hx') + exact ⟨y', hy', g'.fault⟩ + -- the write set from the entry state + have tmoBack : ∀ b, TmoFpA st Z m Rp Bf τ s2 t ep d b → TmoFpA st Z m Rp Bf σ s t ep d b := by + intro b h + rcases h with ((h1 | ⟨w, hw', hb', hl⟩ | h3 | h4) | h5 | h6 | h7 | h8) | h9 + · exact Or.inl (Or.inl (Or.inl h1)) + · exact Or.inl (Or.inl (Or.inr (Or.inl ⟨w, hw', + (tw2 w hw' (3336#u64) (by decide) (by decide) (by decide)).symm.trans hb', hl⟩))) + · exact Or.inl (Or.inl (Or.inr (Or.inr (Or.inl h3)))) + · exact Or.inl (Or.inl (Or.inr (Or.inr (Or.inr h4)))) + · exact Or.inl (Or.inr (Or.inl h5)) + · exact Or.inl (Or.inr (Or.inr (Or.inl h6))) + · exact Or.inl (Or.inr (Or.inr (Or.inr (Or.inl h7)))) + · exact Or.inl (Or.inr (Or.inr (Or.inr (Or.inr (mwr_back rfl rfl CH.frame.sz CH.frame.wr.1 h8))))) + · exact Or.inr h9 + have w13 : WritesIn st s s3 (fun b => TmoFpA st Z m Rp Bf σ s t ep d b ∨ FaultWA t b) := + (CH.frame.wr.trans hw).mono fun b h => h.elim + (fun h => Or.inl (Or.inl (Or.inr (Or.inr (Or.inr (Or.inr h)))))) + (fun h => h.elim Or.inr (fun h => Or.inl (tmoBack b h))) + exact ⟨K13, U13, SV3, run3, snd3, fault3, HE3, w13, hrunr, Qs, by rw [K2.frame]; simp [σ1, updK, hτ], hvm⟩ + refine WP.spec_mono (WP.spec_and (charge_budget_sendFrA (ar := ar) E0 I Idle P a hag hMin hLimit used hsch + (Post := Post) SEND) hP1) ?_ + rintro ⟨s4, a4⟩ ⟨⟨ha4, s2, τ, s3, τ3, τ4, Q3, R3, Q4, -, -, H3, hP3, hres, hcb, H4, hk4, -, -, hm4, -, hc43, -, f4⟩, + sysl, symL, bxi⟩ + dsimp only at ha4 H4 sysl symL bxi f4 + subst ha4 + obtain ⟨K13, U13, SV3, run3, snd3, fault3, HE3, w13, hrunx, Q13, hc3, hnvm⟩ := hP3 + have hmem4 : ((), τ4) ∈ (reschedReprogA τ3).1 := by rw [hres]; exact Set.mem_singleton _ + have Q14 : QSendA σ τ4 := + Q13.trans ((show PresR QKeepA reschedReprogA by unfold reschedReprogA; qpres) τ3 _ hmem4).toSend + have K14 : SIKXA σ.idleThread M Nq false σ τ4 := K13.transS (reschedReprog_sikA hmem4) + have U14 : UFrA (BufBytesA (ipcBufOfA true xd)) σ τ4 := PreO.trans U13 (UFrA.of_fr (reschedReprog_frameA hmem4)) + have SV4 := reschedReprog_svA SV3 hmem4 + have kb3 : L.ksb s3 = L.ksb s := w13.1 + have w14 : WritesIn st s s4 (fun b => TmoFpA st Z m Rp Bf σ s t ep d b ∨ FaultWA t b) := + (w13.trans f4.wr).mono fun b h => h.elim id fun h => + Or.inl (Or.inl (Or.inr (Or.inr (Or.inr (Or.inr (mwr_back rfl rfl U13.2.1 kb3 h)))))) + have w14I : WritesIn st s s4 (fun b => IpcFpA st Z E Rp Bf m σ s b ∨ FaultWA t b) := + w14.mono fun b h => Or.imp (tmoFpA_splitA J.iplaced.e.tcb htZ he hd) id h + have w14N : WritesIn st s s4 (fun b => SchedStepNWA (st := st) N E Rp Z m σ s b ∨ BufW Bf b) := + w14I.mono fun b h => Or.elim h + (fun h => Or.elim h (ewrite_stepNA_buf J.sr.core.tcbs) (fun h => Or.inl (Or.inl (Or.inl h)))) + (fun h => Or.inl (Or.inl (Or.inr (faultTcbStepA J.sr.core.tcbs htZ h)))) + have HE4 : EpStateRel Z.T E Rp Bf (L.mem s4) τ4 := by + obtain ⟨FE, VIEW⟩ := epFrameOfMcsA H3.sched.toSchedSRN J.ipcPlace f4 (by rw [hm4]) + exact HE3.frame J.iplaced.e FE VIEW + have wk : NtfnWaitKeepA Z.T N (L.mem s) (L.mem s4) := + J.ntfnWait_sendXA (W := True) htZ htZ he hd hdb (w14.mono fun b h => Or.elim h + (fun h => Or.inl (Or.elim h (fun h => Or.inl h) (fun h => Or.inr (Or.inr ⟨t, htZ, h⟩)))) Or.inr) + -- the movers + have hdI : specPtr d ≠ σ.idleThread := fun e => by + obtain ⟨xi, hxi, hsi'⟩ := J.sinv.idleTcb + rw [← e, hkd] at hxi; cases hxi; rw [hstd] at hsi'; cases hsi' + have hnI : specPtr t ≠ σ.idleThread := fun e => by + obtain ⟨xi, hxi, hsi'⟩ := J.sinv.idleTcb + rw [← e, hkt0] at hxi; cases hxi; rw [hsi'] at hrunx; cases hrunx + have notM : ∀ c < fl4A64Platform.numNodes, ¬ M (m.idleThreadOf c) := by + intro c hc h + obtain ⟨y1, hy1, hs1⟩ := J.sidle.f3 c hc + rcases h with h | h + · rw [h, hkt0] at hy1; cases hy1; rw [hs1] at hrunx; cases hrunx + · rw [h, hkd] at hy1; cases hy1; rw [hstd] at hs1; cases hs1 + have run4 : ∀ z, τ4.kheap (specPtr d) = some (.tcb z) → z.tcbState = .running := fun z hz => + run3 z (by rw [← hk4]; exact hz) + have afterM : ∀ u ∈ Z.T, M (specPtr u) → ∀ x, τ4.kheap (specPtr u) = some (.tcb x) → + Fl4SpecA64.epBlocked x.tcbState = none ∧ ∀ p, x.tcbState ≠ .blockedOnNotification p := by + intro u _ hu x hx + rcases hu with h | h + · rw [h, hk4] at hx + rcases snd3 with h3 | ⟨r, h3⟩ + · rw [h3 x hx]; exact ⟨rfl, fun _ h => by cases h⟩ + · rw [h3 x hx]; exact ⟨rfl, fun _ h => by cases h⟩ + · rw [h] at hx + rw [run4 x hx]; exact ⟨rfl, fun _ h => by cases h⟩ + have hMs : ∀ u ∈ Z.T, M (specPtr u) → ∀ x, σ.kheap (specPtr u) = some (.tcb x) → + ∀ p, x.tcbState ≠ .blockedOnNotification p := by + intro u _ hu x hx p hp + rcases hu with h | h + · rw [h, hkt0] at hx; cases hx; rw [hp] at hrunx; cases hrunx + · rw [h, hkd] at hx; cases hx; rw [hstd] at hp; cases hp + have hMq : ∀ y, M y → ∀ dm pr, y ∉ σ.readyQueues dm pr := by + intro y hy dm pr hm + rcases hy with rfl | rfl + · exact P.notReady dm pr (by rw [hσct]; exact hm) + · obtain ⟨x, hx, hr⟩ := J.sinv.rq dm pr _ hm + rw [hkd] at hx; cases hx; rw [hstd] at hr; cases hr + have hMv : ∀ u ∈ Z.T, M (specPtr u) → L.mem s (Fl4ProofsA64.P1.stA u) ≠ sched.TS_RUNNING_VM := by + intro u _ hu h' + rcases hu with e | e <;> rw [specPtr_inj e] at h' + · have code := (J.ep.at_tcb htZ hkt0).st + rw [h'] at code + have h8 : (sched.TS_RUNNING_VM : U64).val = 8 := by unfold sched.TS_RUNNING_VM; rfl + rw [h8] at code + have key : ∀ ts : ThreadState, ts ≠ .ext .runningVM → tsCode ts ≠ 8 := fun ts => by + cases ts with + | ext e => cases e; exact fun h => (h rfl).elim + | _ => intro _ h; simp [tsCode] at h + exact key _ hnvm code.symm + · rw [h'] at hdst; revert hdst; unfold sched.TS_RUNNING_VM; decide + have sinv : SInvXA τ4 m fl4A64Platform.numNodes := + K14.sinv J.sinv notM (fun h => h.elim (fun e => hnI e.symm) (fun e => hdI e.symm)) hMq + (fun y hy => by + cases hy + exact ⟨by rw [hσct]; exact hdt, hdI, fun c hc e => notM c hc (Or.inr e), fun z hz => by + rw [run4 z hz]; rfl⟩) + (fun y hy => by cases hy; exact ⟨xd, hkd⟩) noSwitch + (fun y hy hr => by + rcases hy with rfl | rfl + · exact I.notRel (by rw [hσct]; exact hr) + · obtain ⟨x, hx, hrn⟩ := J.sinv.rlsRun _ hr; rw [hkd] at hx; cases hx; rw [hstd] at hrn; cases hrn) + (Or.inr (hc43.trans hc3)) (J.sidle.rdyA J.sinv) + (by + have F3 := K13.faultStA J.sinv.faultSt + (fun p hp x y hx hy => by + obtain ⟨y', hy', e⟩ := fault3 p (fun e => hp (Or.inl e)) x hx + rw [hy] at hy'; cases hy'; exact e) + (fun p hp y hy hf => by + rcases hp with rfl | rfl + · rcases snd3 with h3 | ⟨r, h3⟩ + · rw [h3 y hy]; exact Or.inr (Or.inr rfl) + · rw [h3 y hy]; exact Or.inr (Or.inl ⟨_, rfl⟩) + · obtain ⟨y', hy', e⟩ := fault3 _ hdt xd hkd + rw [hy] at hy'; cases hy' + exact absurd (e.trans (FaultStOkA.none J.sinv.faultSt hkd (by + rw [hstd]; rintro (⟨_, _, h⟩ | ⟨_, h⟩ | h) <;> cases h))) hf) + unfold FaultStOkA at F3 ⊢; rw [hk4]; exact F3) + have idle : IdleNoFaultA m τ4 := fun c hc => by + obtain ⟨x, hx, hf⟩ := J.idleNoFault c hc + obtain ⟨y, hy, e⟩ := fault3 _ (fun e => notM c hc (Or.inl e)) x hx + exact ⟨y, by rw [hk4]; exact hy, e.trans hf⟩ + have cur4 : τ4.curThread = σ.curThread := by rw [K14.frame] + have nr4 : ∀ x, τ4.kheap τ4.curThread = some (.tcb x) → runnable x.tcbState = false := by + intro x hx + rw [cur4, hσct, hk4] at hx + rcases snd3 with h3 | ⟨r, h3⟩ <;> rw [h3 x hx] <;> rfl + refine ⟨τ4, hcb, rfl, ⟨Q4, R3, Z.curOf, Z.curScOf, ?_⟩, cur4, nr4⟩ + exact J.of_ipcStepUXA (Q := Q4) (Rl := R3) (curOf := Z.curOf) (curScOf := Z.curScOf) w14N U14.1 U14.2.1 Q14 + (J.specZero_of_recvBufA hd hkd U14.2.2) (fun _ _ => rfl) bxi sysl symL + (J.sys.ent.user.ksb w14.1) (J.j7_stepNUA w14N hXv7 τ4) H4.sched HE4 + (J.ntfn_ipcXA (w14I.mono fun _ h => h.imp id Or.inl) htZ K14 HE4 wk hMs (fun u hu hm x hx => (afterM u hu hm x hx).2)) + (J.reply_ipcXA w14I htZ K14) J.deadExt.reply (SIKXA.ipcKeepN K14) idle (J.tw_ipcXA (w14I.mono fun _ h => h.imp id Or.inl) htZ) sinv + (K14.sidle J.sidle notM (J.sidle.rdyA J.sinv)) (J.runVcpu_ipcXA (w14I.mono fun _ h => h.imp id Or.inl) htZ K14 HE4 hMv) (J.blkQ_ipcXA K14 HE4 wk afterM) SV4 + +set_option maxRecDepth 4096 in +set_option maxHeartbeats 1000000 in +/-- **The record across `charge_budget … true` taking the timeout send to a waiting receiver** (module docstring). -/ +theorem JStage3A.chargeSendRecvA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar nA)) s a σ m) + (noSwitch : ∀ u, σ.schedulerAction ≠ .switchThread u) (I : CurInvA σ) (Idle : ActIdleA σ) + (hcore : L.mem s (scCoreA (L.cscW s)) = L.cpu s) (hni : σ.curSc ≠ idleScPtr → NotIdleScA m σ.curSc) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (one : fl4A64Platform.numNodes = 1) + (hag : fl4MinBudget = MIN_BUDGET) (hMin : Fl4MinBudgetAgreeA ar a) + (hLimit : sporadic.max_release_time ar a ⦃ v => v.bv = fl4MaxReleaseTime ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) (hconv : Fl4TicksToUsLegacyAgree) + (hA : P0.ArchTotal ar) (En : ChargeEntryA st Ac s) + (hXv7 : ∀ Pt Ub, FrUb st Ub OS s → VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt → + ∀ b, VsR (L.ksb s) Pt b → ¬ BufW Bf b) + (used : U64) (hns : ¬ NoTimeoutA used.bv σ) + {xt : Tcb} (hkt : σ.kheap σ.curThread = some (.tcb xt)) + {slot ep hbg : U64} {sd rv g gr : Bool} + (hslot : obj.Tcb.slot (L.curW s) layout.tcb.SLOT_TIMEOUT_HANDLER = ok slot) (hsl : IsSlotA Z.th slot) + (hcap : capA st s slot = .Endpoint ep hbg sd rv g gr) + (hgc : tcbCnodeMap xt (tcbCnodeIndex 4) = some (.endpointCap (specPtr ep) hbg.bv (rightsOfA sd rv g gr))) + (he : ep ∈ E) {d : U64} {q : List ObjRef} + (hk : σ.kheap (specPtr ep) = some (.endpoint (.recvEP (specPtr d :: q)))) : + sched.charge_budget st ar s a used true ⦃ r => ∃ τ, + fl4ChargeBudget (Z := Unit) used.bv true σ = ({((), τ)}, False) ∧ r.2 = a ∧ + JStage3QA st ar AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes nA m r.1 r.2 τ ∧ + τ.curThread = σ.curThread ∧ ∀ x, τ.kheap τ.curThread = some (.tcb x) → runnable x.tcbState = false ⦄ := by + have P := J.budgetPlaceA hcore hni En.act + exact J.chargeSendRecvCoreA noSwitch I Idle hcore hni hCs one hag hMin hLimit hmax htu hconv P.notReady used + (J.chargeP1A hA En used true) hXv7 hns hkt hslot hsl hcap hgc he hk + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3KEntrySendBlkA.lean b/proofs/Fl4ProofsA64/P3/Stage3KEntrySendBlkA.lean new file mode 100644 index 000000000..a875453ee --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3KEntrySendBlkA.lean @@ -0,0 +1,827 @@ +/- +P3 (aarch64, A64-P3 Stage 3, kernel-entry composition, step 2): **the budget charge's timeout send with no receiver +waiting** (`hSendO` of `kentryChargeIrqSendA` / `kentryChargeRestartSendA`): the timeout handler endpoint is idle or +holds waiting senders, and the current thread blocks on it (Lift 2b's `handle_timeout_blockBA`). + +* `JStage3A.ntfnWait_blkFA`, `JStage3A.blkQ_blkXA`: `ntfnWait_blkA` / `blkQ_sendA` with the sender's fault words and + on `SIKXA`. +* **`JStage3A.chargeSendBlkA`**: `charge_budget_sendFrA` at the record, `end_timeslice` by `end_timeslice_timeoutA` and + `handle_timeout_blockBA`; `PrioSortedA` of the endpoint queue is read from the record's `ipcSorted`. +* `TimeoutBlkA`, **`JStage3A.checkBudgetSendBlkA`**, **`checkBudgetRestartSendBlkA`**. +* **`kentryChargeIrqSendAllA`**, **`kentryChargeRestartSendAllA`**: `kentryChargeIrqSendA` / + `kentryChargeRestartSendA` with `hSendO` discharged. + +Named premises: `kentryChargeIrqA`'s [J] `T`, `hni`; [P] `hA`, `hCs`, `one`, `hag`, `hmb`, `hLimit`, `hmax`, `htu`, +`hconv`; [J] `hX` (`BufOffXA`). The send condition's handler scope (the current thread's timeout handler is an +endpoint cap on an endpoint of `E`, with a waiting receiver (`TimeoutRecvA`) or not (`TimeoutBlkA`)) is derived from +the record (`JStage3A.tmoSlotA`, `JStage3A.timeoutSelA`; `JStage3A.tmoHandlerEpA` gives Lift 2b's `hte`). New module. +-/ +import Fl4ProofsA64.P3.Stage3KEntrySendWireA +import Fl4ProofsA64.P3.Stage3TimeoutSendBlkA +import Fl4ProofsA64.P3.Stage3SendBlkA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 Fl4Spec + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +variable (st) in +/-- **The no-receiver case of the timeout send** at a state: as `TimeoutRecvA`, the endpoint idle or holding waiting +senders. -/ +def TimeoutBlkA (Z : XSetsA) (E : Set U64) (s : S) (σ : State Unit) : Prop := + ∃ (xt : Tcb) (slot ep hbg : U64) (sd rv g gr : Bool) (xe : Endpoint), + σ.kheap σ.curThread = some (.tcb xt) ∧ obj.Tcb.slot (L.curW s) layout.tcb.SLOT_TIMEOUT_HANDLER = ok slot ∧ + IsSlotA Z.th slot ∧ capA st s slot = .Endpoint ep hbg sd rv g gr ∧ + tcbCnodeMap xt (tcbCnodeIndex 4) = some (.endpointCap (specPtr ep) hbg.bv (rightsOfA sd rv g gr)) ∧ + ep ∈ E ∧ σ.kheap (specPtr ep) = some (.endpoint xe) ∧ CanBlockA false xe + +end + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {J5 : JSlotA S} {R : Set U64 → JSlotA S} {s s' : S} {a : A} {σ σ' : State Unit} + {m : McState} + +/-- **A TCB's timeout-handler slot from the record** (module docstring): the slot is a typed slot (`thT`), hence in +`OS` (J5); an endpoint handler cap there is the related FL4 `Endpoint` cap (J's `StateRelA`), naming a heap endpoint +(`capTy`), an endpoint of `E` (`ipcHeapCov`). -/ +theorem JStage3A.tmoSlotA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st R) s a σ m) + {t : U64} (ht : t ∈ Z.T) {y : Tcb} (hy : σ.kheap (specPtr t) = some (.tcb y)) + {p : ObjRef} {b : Badge} {r : CapRights} (hh : y.tcbTimeoutHandler = .endpointCap p b r) : + IsSlotA Z.th (slotIdx t layout.tcb.SLOT_TIMEOUT_HANDLER) ∧ ∃ (ep hbg : U64) (sd rv g gr : Bool), + capA st s (slotIdx t layout.tcb.SLOT_TIMEOUT_HANDLER) = .Endpoint ep hbg sd rv g gr ∧ + y.tcbTimeoutHandler = .endpointCap (specPtr ep) hbg.bv (rightsOfA sd rv g gr) ∧ ep ∈ E := by + have one4 : tcbCnodeMap y (tcbCnodeIndex 4) = some y.tcbTimeoutHandler := by + simp [tcbCnodeMap, tcbIdx_eq, tcbStage2Slot, tcbHighVSpaceSlot] + have hdth : Z.th t = some .tcb := (J.sr.thT t).2 ht + have es : slotIdx t layout.tcb.SLOT_TIMEOUT_HANDLER = slotAddrA (specPtr t) (BitVec.ofNat 64 4) := by + rw [slotIdx_addrA] + unfold layout.tcb.SLOT_TIMEOUT_HANDLER + rfl + have hsl : IsSlotA Z.th (slotIdx t layout.tcb.SLOT_TIMEOUT_HANDLER) := + ⟨t, Or.inr ⟨hdth, 4, by unfold tcbSlotsA; omega, es⟩⟩ + have hOS := J.j5.2.1 _ hsl + have rel : CapRelA (capAtA (L.mem s) (slotIdx t layout.tcb.SLOT_TIMEOUT_HANDLER)) y.tcbTimeoutHandler := by + obtain ⟨z, hz, h⟩ := J.j.1.st.tcb t hdth + rw [hy] at hz + cases hz + obtain ⟨ac, hac, h⟩ := h 4 (by unfold tcbSlotsA; omega) + rw [one4] at hac + cases hac + rw [es] + exact h + have e := rel.2 + have hcA : capA st s (slotIdx t layout.tcb.SLOT_TIMEOUT_HANDLER) = + capAtA (L.mem s) (slotIdx t layout.tcb.SLOT_TIMEOUT_HANDLER) := rfl + generalize hc : capAtA (L.mem s) (slotIdx t layout.tcb.SLOT_TIMEOUT_HANDLER) = c at e hcA + cases c with + | Endpoint ep hbg sd rv g gr => + simp only [CapEqA, absCapA] at e + exact ⟨hsl, ep, hbg, sd, rv, g, gr, hcA, e.symm, + J.ipcHeapCov.ep _ ((J.capTy _ hOS).1 ep hbg sd rv g gr hcA)⟩ + | PageTable q mm asid va => rw [hh] at e; cases mm <;> simp [CapEqA, absCapA] at e + | _ => rw [hh] at e; simp [CapEqA, absCapA] at e + +/-- **A TCB's timeout handler, if an endpoint cap, names an endpoint of `E`** (`tmoSlotA`): Lift 2b's `hte`. -/ +theorem JStage3A.tmoHandlerEpA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st R) s a σ m) + {t : U64} (ht : t ∈ Z.T) {y : Tcb} (hy : σ.kheap (specPtr t) = some (.tcb y)) : + ∀ p b r, y.tcbTimeoutHandler = .endpointCap p b r → ∃ ep ∈ E, p = specPtr ep := by + intro p b r hh + obtain ⟨-, ep, hbg, sd, rv, g, gr, -, e, he⟩ := J.tmoSlotA ht hy hh + rw [hh] at e + exact ⟨ep, he, (Cap.endpointCap.inj e).1⟩ + +set_option maxHeartbeats 1000000 in +/-- **The no-receiver timeout send from the record** (kernel entry 2's `hsel`): when the charge's send condition +holds (`¬ NoTimeoutA`) the current thread's timeout handler is an endpoint cap (`isValidTimeoutHandler` after the +head charge, which keeps the TCB's slots); its slot and endpoint are the record's (`tmoSlotA`, `EpStateRel`), and with +no receiver waiting (`¬ TimeoutRecvA`) the endpoint is idle or holds senders. -/ +theorem JStage3A.timeoutSelA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st R) s a σ m) + (hns : ¬ NoTimeoutA σ.consumedTime σ) (hR : ¬ TimeoutRecvA st Z E s σ) : TimeoutBlkA st Z E s σ := by + have htZ : L.curW s ∈ Z.T := J.sr.curT + have hσct : σ.curThread = specPtr (L.curW s) := J.sr.sch.curThread + obtain ⟨σh, hh, -, xh, yh, nh, hxh, -, hnsa⟩ : ∃ σ1, fl4ChargeHead (Z := Unit) σ.curSc σ.consumedTime σ = + ({((), σ1)}, False) ∧ schedulable σ.curThread { σ1 with consumedTime := 0 } = true ∧ + ∃ x y n, σ1.kheap σ.curThread = some (.tcb x) ∧ σ1.kheap σ.curSc = some (.schedContext y n) ∧ + ¬ NoSendAtA { σ1 with consumedTime := 0 } σ.curThread y x := by + unfold NoTimeoutA at hns + push_neg at hns + exact hns + unfold NoSendAtA at hnsa + push_neg at hnsa + obtain ⟨-, -, hv⟩ := hnsa + have F : SchedFrameA σ σh := + frame_of_runA (inferInstance : SPresInst (fl4ChargeHead (Z := Unit) σ.curSc σ.consumedTime)).pres hh + obtain ⟨xt, hkt⟩ := J.sinv.curTcb + have hmap : tcbCnodeMap xh = tcbCnodeMap xt := by + rcases F.1.kh σ.curThread with e | ⟨x, x', h1, h2, k⟩ | ⟨o, o', h1, h2, f⟩ + · rw [e, hkt] at hxh + cases hxh + rfl + · rw [hkt] at h1 + cases h1 + rw [hxh] at h2 + cases h2 + exact k.1 + · rw [hkt] at h1 + cases h1 + rcases f with ⟨_, _, _, _, h, _⟩ | ⟨_, _, h, _⟩ | ⟨_, _, h, _⟩ | ⟨_, _, h, _⟩ <;> cases h + have one4 : ∀ z : Tcb, tcbCnodeMap z (tcbCnodeIndex 4) = some z.tcbTimeoutHandler := fun z => by + simp [tcbCnodeMap, tcbIdx_eq, tcbStage2Slot, tcbHighVSpaceSlot] + have eh : xh.tcbTimeoutHandler = xt.tcbTimeoutHandler := + Option.some.inj ((one4 xh).symm.trans ((congrFun hmap _).trans (one4 xt))) + have hep : isEpCap xt.tcbTimeoutHandler = true := by + by_contra hc + apply hv + have hk' : ({ σh with consumedTime := 0 } : State Unit).kheap σ.curThread = some (.tcb xh) := hxh + have hval : isValidTimeoutHandler (Z := Unit) σ.curThread { σh with consumedTime := 0 } = + ({(isEpCap xh.tcbTimeoutHandler, { σh with consumedTime := 0 })}, False) := by + have hgc0 : getCap (Z := Unit) (getTcbTimeoutHandlerPtr σ.curThread) { σh with consumedTime := 0 } = + ({(xh.tcbTimeoutHandler, { σh with consumedTime := 0 })}, False) := getCap_tcb_eqA hk' (one4 xh) + unfold isValidTimeoutHandler + rw [nd_pure_bindSc, nd_bind_oneSc hgc0] + rfl + rw [hval, eh, Bool.eq_false_iff.mpr hc] + obtain ⟨p, b, rs, hh'⟩ : ∃ p b rs, xt.tcbTimeoutHandler = .endpointCap p b rs := by + cases h : xt.tcbTimeoutHandler <;> rw [h] at hep <;> simp [isEpCap] at hep + exact ⟨_, _, _, rfl⟩ + have hkt0 : σ.kheap (specPtr (L.curW s)) = some (.tcb xt) := by rw [← hσct]; exact hkt + obtain ⟨hsl, ep, hbg, sd, rv, g, gr, hcap, ha, he⟩ := J.tmoSlotA htZ hkt0 hh' + have hslot : obj.Tcb.slot (L.curW s) layout.tcb.SLOT_TIMEOUT_HANDLER = + ok (slotIdx (L.curW s) layout.tcb.SLOT_TIMEOUT_HANDLER) := tcb_slot_val _ _ + have hgc : tcbCnodeMap xt (tcbCnodeIndex 4) = some (.endpointCap (specPtr ep) hbg.bv (rightsOfA sd rv g gr)) := by + rw [one4, ha] + obtain ⟨xe, hk, rel_e⟩ := J.ep.ep ep he + cases xe with + | idleEP => exact ⟨xt, _, ep, hbg, sd, rv, g, gr, .idleEP, hkt, hslot, hsl, hcap, hgc, he, hk, trivial⟩ + | sendEP q => exact ⟨xt, _, ep, hbg, sd, rv, g, gr, .sendEP q, hkt, hslot, hsl, hcap, hgc, he, hk, rfl⟩ + | recvEP q => + obtain ⟨-, xs, hne, rfl, -, -⟩ := rel_e + cases xs with + | nil => exact absurd rfl hne + | cons d ds => exact absurd ⟨xt, _, ep, hbg, sd, rv, g, gr, d, _, hkt, hslot, hsl, hcap, hgc, he, hk⟩ hR + +/-- **`NtfnWaitKeepMA`** for a blocking send from another state (`s0`), its scheduler words mapped to the record's +(`hmw`), from its write set and the sender's fault words: the threads waiting on a notification are not queued on +the endpoint, so no `Ep` link or block word of theirs is written. -/ +theorem JStage3A.ntfnWait_blkGA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7A st R) s a σ m) + {Z0 : XSetsA} {s0 : S} {σ0 : State Unit} {t e : U64} (ht : t ∈ Z.T) (he : e ∈ E) + (hmw : ∀ b, MWrA (st := st) Z0 m σ0 s0 b → MWrA (st := st) Z m σ s b) + (w : WritesIn st s0 s' (fun b => BlkSendFpA st Z.T (fun σ s => MWrA (st := st) Z0 m σ s) σ0 s0 t e b ∨ + FaultWA t b)) : + NtfnWaitKeepMA (fun o => o = specPtr t) Z.T N (L.mem s0) (L.mem s') := by + intro y hy hM hN + have hP := J.iplaced + have hya := hP.e.tcb y hy + have hyt : y ≠ t := fun c => hM (by rw [c]) + have apart : ∀ u ∈ Z.T, u ≠ y → u.val + 4096 ≤ y.val ∨ y.val + 4096 ≤ u.val := fun u hu ne => by + have := J.sr.place.apart u y .tcb .tcb ((J.sr.thT u).2 hu) ((J.sr.thT y).2 hy) ne + have hsz : ObjKindA.size .tcb = 4096 := rfl + omega + have key : ∀ (K : U64) (b : U64), (K.val = 3336 ∨ K.val = 3496 ∨ K.val = 3504) → b = wa y K → Aligned b → + L.mem s' b = L.mem s0 b := by + intro K b hK hb hal + have hfy := hya.fits + have hbv : b.val = y.val + K.val := by rw [hb, wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have sw : ∀ (k : U64), b.val = t.val + k.val → k.val < 4096 → False := fun k h hk => by + have := apart t ht hyt.symm + omega + refine w.mem_eq hal ?_ + rintro ((c | c | c | c | c | ⟨v, hv, c⟩ | c) | c) + · exact sw 3328#u64 (by rw [c]; exact Endp.val_st (hP.e.tcb t ht)) (by simp) + · exact sw 3336#u64 (by rw [c]; exact Endp.val_blk (hP.e.tcb t ht)) (by simp) + · exact sw 3344#u64 (by rw [c]; exact Endp.val_badge (hP.e.tcb t ht)) (by simp) + · exact sw 3352#u64 (by rw [c]; exact Endp.val_flags (hP.e.tcb t ht)) (by simp) + · have := hP.e.et e he y hy; unfold Endp.EW at c; omega + · have hvT : v ∈ Z.T := by + rcases hv with rfl | hv + · exact ht + · exact hv.1 + have hvy : v ≠ y := by + rcases hv with rfl | hv + · exact hyt.symm + · intro h + subst h + rw [hv.2] at hN + have := hP.ne e hN e he + omega + have := apart v hvT hvy + have := (hP.e.tcb v hvT).fits + unfold TL at c + omega + · rw [hb] at c + exact J.tcbWord_offMwrA hy (by omega) (by omega) (hmw _ c) + · obtain ⟨K', hK', e'⟩ := faultWA_eq c + have hKv : K'.val < 4096 := by rcases hK' with rfl | rfl | rfl | rfl <;> simp + exact sw K' (by rw [e']; exact field_val (hP.e.tcb t ht) hKv) hKv + refine ⟨?_, ?_, ?_⟩ + · exact key 3336#u64 _ (by simp) (by unfold Notif.tBlockA; rfl) (Endp.a_blk hya).1 + · exact key 3504#u64 _ (by simp) (by rw [prevEp_eq]) (Endp.a_prev hya).1 + · exact key 3496#u64 _ (by simp) (by rw [nextEp_eq]) (Endp.a_next hya).1 + +/-- **`NtfnWaitKeepMA`** from the blocking send's write set and the sender's fault words: the threads waiting on a notification are not queued on +the endpoint, so no `Ep` link or block word of theirs is written. -/ +theorem JStage3A.ntfnWait_blkFA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7A st R) s a σ m) + {t e : U64} (ht : t ∈ Z.T) (he : e ∈ E) + (w : WritesIn st s s' (fun b => BlkSendFpA st Z.T (fun σ s => MWrA (st := st) Z m σ s) σ s t e b ∨ + FaultWA t b)) : + NtfnWaitKeepMA (fun o => o = specPtr t) Z.T N (L.mem s) (L.mem s') := + J.ntfnWait_blkGA ht he (fun _ h => h) w + +/-- **`blkQ_sendA` across an `SIKXA` step** ending with the sender queued on the endpoint it blocks on. -/ +theorem JStage3A.blkQ_blkXA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7A st R) s a σ m) + {t e : U64} (ht : t ∈ Z.T) (he : e ∈ E) + {i : ObjRef} {Nq : ObjRef → Prop} {don : Bool} (K : SIKXA i (fun o => o = specPtr t) Nq don σ σ') + (atT : ∃ (xt : Tcb) (pl : SenderPayload), σ'.kheap (specPtr t) = some (.tcb (blkTcbA xt (specPtr e) pl))) + (atE : ∃ q, σ'.kheap (specPtr e) = some (.endpoint (.sendEP q)) ∧ specPtr t ∈ q) + (E' : EpStateRel Z.T E Rp Bf (L.mem s') σ') + (wk : NtfnWaitKeepMA (fun o => o = specPtr t) Z.T N (L.mem s) (L.mem s')) : + BlkQueuedA Z.T E N (L.mem s') σ' := by + have kept : ∀ u ∈ Z.T, ∀ y, σ'.kheap (specPtr u) = some (.tcb y) → specPtr u ≠ specPtr t → + ∃ z, σ.kheap (specPtr u) = some (.tcb z) ∧ z.tcbState = y.tcbState := fun u _ y hy hM => by + obtain ⟨z, hz, g⟩ := K.tcbB hy + exact ⟨z, hz, (g.st.resolve_right fun c => hM c.1).symm⟩ + refine ⟨fun u hu y hy p hb => ?_, fun u hu y hy p hp => ?_⟩ + · by_cases hM : specPtr u = specPtr t + · have eu : u = t := specPtr_inj hM + subst eu + obtain ⟨y1, hy1, r'⟩ := E'.tcb u hu + rw [hy] at hy1; cases hy1 + have hb' := r'.blk p hb + obtain ⟨xt, pl, atT'⟩ := atT + rw [atT'] at hy; cases hy + simp only [blkTcbA, epBlocked, Option.some.injEq] at hb + subst hb + have eq : L.mem s' (Notif.tBlockA u) = e := specPtr_inj hb' + obtain ⟨q, hq, hm⟩ := atE + exact ⟨by rw [eq]; exact he, _, hq, hm⟩ + · obtain ⟨z, hz, es⟩ := kept u hu y hy hM + obtain ⟨hE, q, hq, hm⟩ := J.blkQ.ep u hu z hz p (es ▸ hb) + obtain ⟨q', hq', hmem⟩ := K.epF hq + obtain ⟨x1, hx1, r⟩ := J.ep.tcb u hu + obtain ⟨y1, hy1, r'⟩ := E'.tcb u hu + rw [hz] at hx1; cases hx1 + rw [hy] at hy1; cases hy1 + have eq : L.mem s' (Notif.tBlockA u) = L.mem s (Notif.tBlockA u) := + specPtr_inj ((r'.blk p hb).trans (r.blk p (es ▸ hb)).symm) + exact ⟨by rw [eq]; exact hE, q', hq', hmem _ hm hM⟩ + · have hM : specPtr u ≠ specPtr t := fun c => by + obtain ⟨xt, pl, atT'⟩ := atT + rw [c, atT'] at hy + cases hy + cases hp + obtain ⟨z, hz, es⟩ := kept u hu y hy hM + obtain ⟨hN, z', q, hq, ho, hm⟩ := J.blkQ.ntfn u hu z hz p (es.trans hp) + exact ⟨by rw [(wk u hu hM hN).1]; exact hN, z', q, K.ntfnF hq, ho, hm⟩ + +end + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {κ : U64 → U64} + {nA : U64} {s : S} {a : A} {σ : State Unit} {m : McState} + +set_option maxRecDepth 4096 in +set_option maxHeartbeats 1000000 in +/-- **The record across `charge_budget … true` taking the timeout send with no receiver waiting** (module docstring). -/ +theorem JStage3A.chargeSendBlkA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar nA)) s a σ m) + (noSwitch : ∀ u, σ.schedulerAction ≠ .switchThread u) (I : CurInvA σ) (Idle : ActIdleA σ) + (hcore : L.mem s (scCoreA (L.cscW s)) = L.cpu s) (hni : σ.curSc ≠ idleScPtr → NotIdleScA m σ.curSc) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (one : fl4A64Platform.numNodes = 1) + (hag : fl4MinBudget = MIN_BUDGET) (hMin : Fl4MinBudgetAgreeA ar a) + (hLimit : sporadic.max_release_time ar a ⦃ v => v.bv = fl4MaxReleaseTime ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) (hconv : Fl4TicksToUsLegacyAgree) + (hA : P0.ArchTotal ar) (En : ChargeEntryA st Ac s) + (hXv7 : ∀ Pt Ub, FrUb st Ub OS s → VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt → + ∀ b, VsR (L.ksb s) Pt b → ¬ BufW Bf b) + (used : U64) (hns : ¬ NoTimeoutA used.bv σ) + {xt : Tcb} (hkt : σ.kheap σ.curThread = some (.tcb xt)) + {slot ep hbg : U64} {sd rv g gr : Bool} + (hslot : obj.Tcb.slot (L.curW s) layout.tcb.SLOT_TIMEOUT_HANDLER = ok slot) (hsl : IsSlotA Z.th slot) + (hcap : capA st s slot = .Endpoint ep hbg sd rv g gr) + (hgc : tcbCnodeMap xt (tcbCnodeIndex 4) = some (.endpointCap (specPtr ep) hbg.bv (rightsOfA sd rv g gr))) + (he : ep ∈ E) {xe : Endpoint} (hk : σ.kheap (specPtr ep) = some (.endpoint xe)) (hc : CanBlockA false xe) : + sched.charge_budget st ar s a used true ⦃ r => ∃ τ, + fl4ChargeBudget (Z := Unit) used.bv true σ = ({((), τ)}, False) ∧ r.2 = a ∧ + JStage3QA st ar AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes nA m r.1 r.2 τ ∧ + τ.curThread = σ.curThread ∧ ∀ x, τ.kheap τ.curThread = some (.tcb x) → runnable x.tcbState = false ⦄ := by + have E0 := J.yieldQueueA + have P := J.budgetPlaceA hcore hni En.act + set t := L.curW s with ht_def + have htZ : t ∈ Z.T := J.sr.curT + have hσct : σ.curThread = specPtr t := J.sr.sch.curThread + have hkt0 : σ.kheap (specPtr t) = some (.tcb xt) := hσct ▸ hkt + -- the send condition after the head + obtain ⟨σh, hh, hsh, xh, yh', nh', hxh, hyh, hnsa⟩ : ∃ σ1, fl4ChargeHead (Z := Unit) σ.curSc used.bv σ = + ({((), σ1)}, False) ∧ schedulable σ.curThread { σ1 with consumedTime := 0 } = true ∧ + ∃ x y n, σ1.kheap σ.curThread = some (.tcb x) ∧ σ1.kheap σ.curSc = some (.schedContext y n) ∧ + ¬ NoSendAtA { σ1 with consumedTime := 0 } σ.curThread y x := by + unfold NoTimeoutA at hns + push_neg at hns + exact hns + unfold NoSendAtA at hnsa + push_neg at hnsa + obtain ⟨hper, hvm, -⟩ := hnsa + have hsch : ∀ σ1, fl4ChargeHead (Z := Unit) σ.curSc used.bv σ = ({((), σ1)}, False) → + schedulable σ.curThread { σ1 with consumedTime := 0 } = true := fun σ1 h1 => by + have e : σ1 = σh := + (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst (h1.symm.trans hh)))).2 + rw [e]; exact hsh + -- the endpoint + obtain ⟨xt', hkt', -, -⟩ := I.tcb + have ext : xt' = xt := KernelObject.tcb.inj (Option.some.inj (hkt'.symm.trans hkt)) + subst xt' + obtain ⟨x0, n0, hx0⟩ := I.sc + have hctcs : specPtr t ≠ σ.curSc := fun e => by rw [e, hx0] at hkt0; cases hkt0 + have hepcs : specPtr ep ≠ σ.curSc := fun e => by rw [e, hx0] at hk; cases hk + have hept : specPtr ep ≠ specPtr t := fun e => by rw [e, hkt0] at hk; cases hk + have hqT : ∀ a ∈ xe.epQueue, ∃ y, σ.kheap a = some (.tcb y) := by + intro a ha + obtain ⟨xs, hQ, hxs, -⟩ := (J.ep.at_ep he hk).queue + rw [hxs] at ha + obtain ⟨u, hu, rfl⟩ := List.mem_map.1 ha + obtain ⟨y, hy, -⟩ := J.ep.tcb u (hQ.mem u hu) + exact ⟨y, hy⟩ + have hsortσ : PrioSortedA σ xe.epQueue := J.ipcSorted.1 (specPtr ep) ⟨ep, he, rfl⟩ xe hk + let M : ObjRef → Prop := fun o => o = specPtr t + let Nq : ObjRef → Prop := fun _ => False + let Post : S → State Unit → Prop := fun s3 τ3 => + SIKXA σ.idleThread M Nq false σ τ3 ∧ + (SpecSchedA σ τ3 ∧ SSzA σ τ3 ∧ τ3.machineState = σ.machineState) ∧ SchedValidA τ3 ∧ + (∃ (x1 : Tcb) (pl : SenderPayload), τ3.kheap (specPtr t) = some (.tcb (blkTcbA x1 (specPtr ep) pl))) ∧ + (∃ q, τ3.kheap (specPtr ep) = some (.endpoint (.sendEP q)) ∧ specPtr t ∈ q) ∧ + (∀ p, p ≠ specPtr t → ∀ x, σ.kheap p = some (.tcb x) → ∃ y, τ3.kheap p = some (.tcb y) ∧ + y.tcbFault = x.tcbFault) ∧ + EpStateRel Z.T E Rp Bf (L.mem s3) τ3 ∧ + WritesIn st s s3 (fun b => BlkSendFpA st Z.T (fun σ s => MWrA (st := st) Z m σ s) σ s t ep b ∨ FaultWA t b) ∧ + runnable xt.tcbState = true ∧ QBlkA (specPtr '' E) (specPtr '' N) σ τ3 ∧ τ3.consumedTime = 0 + have SEND : ∀ s2 τ (sc : U64) (x : Tcb) (y : SchedContext) (n : Nat), ChargeHeadA st Z m κ used.bv s σ s2 τ → + sc ∈ Z.C → L.cscW s2 = sc → σ.curSc = scNameA m sc → sc ≠ 0#u64 → L.mem s2 (scA (L.curW s2)) = sc → + τ.kheap (specPtr (L.curW s2)) = some (.tcb x) → x.tcbSchedContext = some (scNameA m sc) → + τ.kheap (scNameA m sc) = some (.schedContext y n) → + sched.end_timeslice st ar s2 a true ⦃ r => r.2 = a ∧ ∃ τ3, + fl4EndTimeslice (Z := Unit) true τ = ({((), τ3)}, False) ∧ + (∃ Q' R', YieldQueueRelA (st := st) { Z with Q := Q', R := R' } m κ r.1 τ3) ∧ Post r.1 τ3 ⦄ := by + intro s2 τ sc x y n CH hsc hcs hσsc sc0 hWordS hkx hBack h2 + obtain ⟨yh, nh, hHead, hτ⟩ := CH.head + have hct2 : L.curW s2 = t := specPtr_inj (CH.sr.sch.curThread.symm.trans (CH.cur.trans hσct)) + rw [hct2] at hkx hWordS + have τkh : τ.kheap = Function.update σ.kheap σ.curSc (some (.schedContext yh nh)) := by rw [hτ]; rfl + have τfar : ∀ p, p ≠ σ.curSc → τ.kheap p = σ.kheap p := fun p hp => by + rw [τkh, Function.update_of_ne hp] + have τcsc : τ.kheap σ.curSc = some (.schedContext yh nh) := by rw [τkh, Function.update_self] + have hkτ : τ.kheap (specPtr t) = some (.tcb xt) := by rw [τfar _ hctcs]; exact hkt0 + have ex : x = xt := KernelObject.tcb.inj (Option.some.inj (hkx.symm.trans hkτ)) + subst x + rw [← hσsc] at h2 + obtain ⟨ey, en⟩ := KernelObject.schedContext.inj (Option.some.inj (h2.symm.trans τcsc)) + subst y n + -- the head's state is `σh` + have eσh : σh = updKSc σ σ.curSc (.schedContext yh nh) := + (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst (hh.symm.trans hHead)))).2 + have exh : xh = xt := by + rw [eσh, updKSc_kheap, Function.update_of_ne (hσct ▸ hctcs), hkt] at hxh + exact (KernelObject.tcb.inj (Option.some.inj hxh)).symm + have eyh : yh' = yh := by + rw [eσh, updKSc_kheap, Function.update_self] at hyh + exact (KernelObject.schedContext.inj (Option.some.inj hyh)).1.symm + subst xh yh' + -- the SC keep at the head + obtain ⟨z', hz', hrep', hyf', htcb', hrm', hne'⟩ := CH.keep.1 _ x0 n0 hx0 + rw [τcsc] at hz' + obtain ⟨e1, e2⟩ := KernelObject.schedContext.inj (Option.some.inj hz') + subst z' n0 + have hact0 : scActive x0 = true := CH.act x0 nh hx0 + have hScIpc : ScIpcA σ.idleThread false σ.curSc x0 yh := + ⟨hyf', hrm', hne' hact0, fun _ => ⟨htcb', hrep'⟩, Or.inl htcb', fun _ => hrep'⟩ + -- the current thread + have hRunS : schedulable (specPtr t) τ = true := by have := CH.run; rwa [CH.cur, hσct] at this + obtain ⟨-, nrelτ, xr, hxr, hrunr⟩ := schedulable_actA hRunS + have exr : xr = xt := KernelObject.tcb.inj (Option.some.inj (hxr.symm.trans hkτ)) + subst xr + have hnI : specPtr t ≠ σ.idleThread := fun e => by + obtain ⟨xi, hxi, hsi⟩ := J.sinv.idleTcb + rw [← e, hkt0] at hxi; cases hxi; rw [hsi] at hrunr; cases hrunr + have hxsc : xt.tcbSchedContext = some σ.curSc := by rw [hσsc]; exact hBack + obtain ⟨z0, j0, hz0, ho0⟩ := J.sr.inv.back _ _ _ hkt0 hxsc + obtain ⟨e3, e4⟩ := KernelObject.schedContext.inj (Option.some.inj (hz0.symm.trans hx0)) + subst z0 j0 + have hcsNI : σ.curSc ≠ idleScPtr := fun e => by + obtain ⟨xi, yi, ki, -, -, hyi, hti⟩ := J.sinv.idleSc + rw [← e, hx0] at hyi; cases hyi + exact hnI (Option.some.inj (ho0.symm.trans hti)) + have hname : scNameA m sc = specPtr sc := by + have h' := hσsc + unfold scNameA at h' ⊢ + by_cases hi : specPtr sc = m.idleScOf m.curCore + · rw [if_pos hi] at h'; exact absurd h' hcsNI + · rw [if_neg hi] + -- the scheduler frame and the facts at the head's state + have FR : SchedFrameA σ τ := by + rw [hτ] + exact PreO.trans (SchedFrameA.upd_sc (y' := yh) hx0) (SchedFrameA.of_eq rfl rfl rfl rfl rfl rfl rfl rfl) + have tw2 : ∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → + (K.val ≠ 3464 ∧ K.val ≠ 3480 ∧ K.val ≠ 3488 ∧ K.val ≠ 3592) → L.mem s2 (wa u K) = L.mem s (wa u K) := + fun u hu K h1 h2 h3 => CH.frame.wr.mem_eq (field_aligned (J.sr.core.tcbs u hu) h1 h2) + (tcbNotMWrA J.sr.toSchedSRN hu h1 h3) + obtain ⟨-, HE2, -, -, -, -⟩ := mcsStepFramesA J.iplaced J.ipcPlace J.sr.toSchedSRN CH.frame FR.2.2 + (fun q' y' k' hy' => by + obtain ⟨y'', hy'', hr, hyf, -⟩ := CH.keep.1 q' y' k' hy' + exact ⟨y'', hy'', hr, hyf⟩) + J.ntfn J.ep J.reply J.mcp J.yieldRel + have slotK : ∀ y, IsSlotA Z.th y → capAtA (L.mem s2) y = capAtA (L.mem s) y := by + intro y hy + obtain ⟨h32, hfit⟩ := IsSlotA.al32 J.sr.place hy + have al : Aligned y := by unfold Aligned; omega + have v8 : (wa y 8#u64).val = y.val + 8 := by + rw [wa_valm] + have h8 : (8#u64 : U64).val = 8 := by simp + omega + have al8 : Aligned (wa y 8#u64) := by unfold Aligned; rw [v8]; omega + unfold capAtA + rw [CH.frame.wr.mem_eq al (fun h => J.slot_offStepA hy (le_refl _) (by omega) (Or.inl h)), + CH.frame.wr.mem_eq al8 (fun h => J.slot_offStepA hy (by omega) (by omega) (Or.inl h))] + have hSt2 : StateRelA Z.th (L.mem s2) τ := by + have h := J.j.1.st.congrA (mem' := L.mem s2) fun y hy => slotK y (slotW_isSlot J.sr.place hy) + refine ⟨fun p r hp => ?_, fun u hu => ?_⟩ + · obtain ⟨cs, h1, h2', h3⟩ := h.cnode p r hp + have hne : specPtr p ≠ σ.curSc := fun e => by rw [e, hx0] at h1; cases h1 + exact ⟨cs, by rw [τfar _ hne]; exact h1, h2', h3⟩ + · obtain ⟨x1, h1, h2'⟩ := h.tcb u hu + exact ⟨x1, CH.keep.2 _ _ h1, h2'⟩ + have hV2 : SlotsValidA Z.th (L.mem s2) := fun y hy => by rw [slotK y hy]; exact J.j.1.cv y hy + have hC2 : BufCover st Z.T Bf s2 τ := + BufCover.frameK J.bufCover (fun u hu => by rw [ibWordA_eq]; exact tw2 u hu (3512#u64) (by decide) (by decide) (by decide)) + (fun u hu => by + show capAtA (L.mem s2) (sbWordA u) = capAtA (L.mem s) (sbWordA u) + unfold capAtA + rw [sbWordA8_eq, sbWordA_eq, tw2 u hu (64#u64) (by decide) (by decide) (by decide), + tw2 u hu (72#u64) (by decide) (by decide) (by decide)]) + (fun u hu => let ⟨x1, hx1, _⟩ := J.ep.tcb u hu; ⟨x1, hx1⟩) + (fun p x1 hx1 => ⟨x1, CH.keep.2 p x1 hx1, rfl, rfl⟩) + have hN2 : BufNativeA Z.T (L.mem s2) := + J.bufNative.writesIn J.iplaced.e.tcb CH.frame.wr fun u hu K hK => + tcbNotMWrA J.sr.toSchedSRN hu (by rcases hK with rfl | rfl <;> decide) + (by rcases hK with rfl | rfl <;> decide) + have hkτ' : τ.kheap (specPtr ep) = some (.endpoint xe) := by rw [τfar _ hepcs]; exact hk + have hsortτ : PrioSortedA τ xe.epQueue := by + refine hsortσ.imp_of_mem fun {a b} ha hb hab ya yb ea eb => ?_ + have back : ∀ c ∈ xe.epQueue, ∀ y, τ.kheap c = some (.tcb y) → σ.kheap c = some (.tcb y) := fun c hc y hy => by + obtain ⟨y0, hy0⟩ := hqT c hc + rwa [τfar c (fun e => by rw [e, hx0] at hy0; cases hy0)] at hy + exact hab ya yb (back a ha ya ea) (back b hb yb eb) + have hSnd : SndRunA τ (specPtr t) xt := by + refine ⟨hrunr, ⟨fun dm pr hm => ?_, nrelτ⟩, fun u hu => ?_, ?_⟩ + · rw [hτ] at hm + exact P.notReady dm pr (by rw [hσct]; exact hm) + · rw [hτ] at hu; exact absurd hu (noSwitch u) + · rw [hτ]; exact hnI + have hD2 : DonA τ (specPtr t) xt := by + intro c hc + rw [hxsc] at hc + cases hc + refine ⟨hcsNI, fun y' k' hy' => ?_⟩ + rw [τcsc] at hy'; cases hy' + rw [htcb', ho0] + have H2' : SchedSRB st Z m κ s2 τ (specPtr t) := by rw [← hσct, ← CH.cur]; exact CH.sr + have hcap2 : obj.Slot.cap st slot s2 = ok (.Endpoint ep hbg sd rv g gr) := by + rw [capA_eq, show capA st s2 slot = capA st s slot from slotK slot hsl, hcap] + have hgc2 : getCap (Z := Unit) (getTcbTimeoutHandlerPtr (specPtr t)) τ = + ({(.endpointCap (specPtr ep) hbg.bv (rightsOfA sd rv g gr), τ)}, False) := getCap_tcb_eqA hkτ hgc + have tail := fun bd : U64 => handle_timeout_blockBA (st := st) (ar := ar) (N := N) a J.ipcPlace J.iplaced.e HE2 + hmax hconv H2' CH.mcp CH.yr htZ hkτ hSnd hslot hcap2 hgc2 he hkτ' hc hsortτ bd + have G := fun {Pst : S × A → Prop} (TMO : ∀ bd : U64, bd.bv = yh.scBadge → + faults.handle_timeout st ar s2 a t (.Timeout bd) ⦃ r => Pst r ⦄) => + end_timeslice_timeoutA (st := st) (ar := ar) a H2' CH.stq htZ hsc hct2 hcs (CH.csc.trans hσsc) hkτ hvm + (by rw [← hσsc]; exact τcsc) hper hslot hcap2 hgc2 TMO + have spec := (G (Pst := fun _ => True) fun bd _ => WP.spec_mono (tail bd) fun _ _ => trivial).2 + refine (G fun bd hBd => WP.spec_mono (tail bd) ?_).1 + rintro ⟨s3, a3⟩ ⟨ha3, hsp, -, B, hQ3, HE3, hw, hz⟩ + let x1 : Tcb := { xt with tcbFault := some (.timeout yh.scBadge) } + let σ1 := updK τ (specPtr t) (.tcb x1) + let τ3 := blkSendA σ1 (specPtr t) (specPtr ep) xe x1 (sndPlA hbg.bv g gr false) + dsimp only at ha3 hsp B hQ3 HE3 hw hz + rw [hBd] at hsp B hQ3 HE3 hz + change BlkStepA σ1 τ3 (specPtr t) (specPtr ep) xe x1 (sndPlA hbg.bv g gr false) at B + change fl4HandleTimeout (Z := Unit) (specPtr t) (.timeout yh.scBadge) τ = ({((), τ3)}, False) at hsp + change ∃ Q' R', YieldQueueRelA (st := st) { Z with Q := Q', R := R' } m κ s3 τ3 at hQ3 + change EpStateRel Z.T E Rp Bf (L.mem s3) τ3 at HE3 + change SSzA τ τ3 at hz + refine ⟨ha3, τ3, spec.trans hsp, hQ3, ?_⟩ + have far1 : ∀ p, p ≠ specPtr t → σ1.kheap p = τ.kheap p := fun p hp => by simp [σ1, updK, hp] + have hkt1 : σ1.kheap (specPtr t) = some (.tcb x1) := by simp [σ1, updK] + have hk1 : σ1.kheap (specPtr ep) = some (.endpoint xe) := by rw [far1 _ hept]; exact hkτ' + have K2 : SIKA σ.idleThread M Nq false σ1 τ3 := B.sika hkt1 hk1 rfl + have K13 : SIKXA σ.idleThread M Nq false σ τ3 := (SIKXA.headFaultA hx0 hkt0 hScIpc hcsNI hτ _).transS K2 + have mem3 : τ3.machineState = σ.machineState := by + rw [B.machine] + show τ.machineState = σ.machineState + rw [hτ]; rfl + have SSch : SpecSchedA σ τ3 := + (FR.1.trans (SchedFrameA.upd_tcb (x' := x1) hkτ ⟨rfl, rfl, rfl⟩).1).trans (B.specSched hkt1 hk1) + have SSz3 : SSzA σ τ3 := SSzA.trans FR.2.1 hz + have rdy : IdleRdyA σ → IdleRdyA τ := fun g z k h => by + have ht : τ.curTime = σ.curTime := by rw [hτ]; rfl + rw [ht] + rw [τkh, Function.update_of_ne (Ne.symm hcsNI)] at h + exact g z k h + have SH : ChargeShapeA σ τ := + ⟨CH.cur, by rw [hτ]; rfl, CH.csc, by rw [hτ]; rfl, ⟨yh, nh, τkh⟩, + Or.inl ⟨by rw [hτ]; rfl, by rw [hτ]; rfl, by rw [hτ]; rfl⟩, rdy, + ⟨by rw [hτ]; rfl, by rw [hτ]; rfl, by rw [hτ]; rfl, by rw [hτ]; rfl⟩⟩ + have SV1 : SchedValidA σ1 := Sched.SchedValidA.faultA (J.schedValid.chargeA SH CH.keep FR) hkτ _ + have actT1 : ScActiveAtA σ1 σ.curSc := + ⟨yh, nh, by rw [far1 _ hctcs.symm]; exact τcsc, (scActive_of_maxA hrm').trans hact0⟩ + have SV3 : SchedValidA τ3 := B.schedValid SV1 hkt1 hk1 (fun sc' hs' => by + have e' : sc' = σ.curSc := Option.some.inj (hs'.symm.trans hxsc) + rw [e']; exact actT1) + have Qs : QBlkA (specPtr '' E) (specPtr '' N) σ τ3 := by + have Qh : QKeepA σ τ := + (QKeepA.upd hx0 (QObjA.ofSc (y := yh) fun h => + ⟨by rwa [hrep'] at h, fun a => (scActive_of_maxA hrm').trans a⟩)).trans (QKeepA.of_kheap τkh ⟨by rw [hτ]; rfl, by rw [hτ]; rfl, by rw [hτ]; rfl, by rw [hτ]; rfl⟩) + have Qf : QKeepA τ σ1 := (QKeepA.upd hkτ (QObjA.ofTcb (y := x1) rfl)).trans (QKeepA.of_kheap rfl ⟨rfl, rfl, rfl, rfl⟩) + have Q01 : QBlkA (specPtr '' E) (specPtr '' N) σ σ1 := QSendA.toBlk J.ipcSorted (Qh.trans Qf).toSend + have hq1 : ∀ a ∈ xe.epQueue, ∃ y, σ1.kheap a = some (.tcb y) := fun a ha => by + by_cases e : a = specPtr t + · exact ⟨x1, by rw [e]; exact hkt1⟩ + · obtain ⟨y, hy⟩ := hqT a ha + exact ⟨y, by rw [far1 a e, τfar a (fun c => by rw [c, hx0] at hy; cases hy)]; exact hy⟩ + have Q13 := B.qblk Q01.sorted hkt1 hk1 hq1 + exact ⟨Q13.sorted, fun h => Q13.act (Q01.act h), fun p y h => by + obtain ⟨x, hx, f⟩ := Q13.rt p y h + obtain ⟨x0', hx0', f0⟩ := Q01.rt p x hx + exact ⟨x0', hx0', fun h => f (f0 h)⟩, Q01.dom.trans Q13.dom⟩ + have atE3 : ∃ q, τ3.kheap (specPtr ep) = some (.endpoint (.sendEP q)) ∧ specPtr t ∈ q := + ⟨_, B.atE, mem_specInsA.2 (Or.inl rfl)⟩ + have fault3 : ∀ p, p ≠ specPtr t → ∀ x', σ.kheap p = some (.tcb x') → ∃ y', τ3.kheap p = some (.tcb y') ∧ + y'.tcbFault = x'.tcbFault := fun p hp x' hx' => by + have hpcs : p ≠ σ.curSc := fun e => by rw [e, hx0] at hx'; cases hx' + have hpe : p ≠ specPtr ep := fun e => by rw [e, hk] at hx'; cases hx' + exact ⟨x', by rw [B.far p hp hpe, far1 p hp, τfar p hpcs]; exact hx', rfl⟩ + -- the write set from the entry state + have blkBack : ∀ b, BlkSendFpA st Z.T (fun σ s => MWrA (st := st) Z m σ s) τ s2 t ep b → + BlkSendFpA st Z.T (fun σ s => MWrA (st := st) Z m σ s) σ s t ep b := by + intro b h + rcases h with h | h | h | h | h | ⟨v, hv, h⟩ | h + · exact Or.inl h + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr (Or.inl h)) + · exact Or.inr (Or.inr (Or.inr (Or.inl h))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inl h)))) + · refine Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨v, ?_, h⟩))))) + exact hv.imp id fun hv => + ⟨hv.1, (tw2 v hv.1 3336#u64 (by decide) (by decide) (by decide)).symm.trans hv.2⟩ + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + (mwr_back rfl rfl CH.frame.sz CH.frame.wr.1 h)))))) + have w13 : WritesIn st s s3 (fun b => + BlkSendFpA st Z.T (fun σ s => MWrA (st := st) Z m σ s) σ s t ep b ∨ FaultWA t b) := + (CH.frame.wr.trans hw).mono fun b h => h.elim + (fun h => Or.inl (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr h))))))) + (fun h => h.elim Or.inr (fun h => Or.inl (blkBack b h))) + exact ⟨K13, ⟨SSch, SSz3, mem3⟩, SV3, ⟨x1, _, B.atT⟩, atE3, fault3, HE3, w13, hrunr, Qs, + by rw [K2.frame]; simp [σ1, updK, hτ]⟩ + refine WP.spec_mono (WP.spec_and (charge_budget_sendFrA (ar := ar) E0 I Idle P a hag hMin hLimit used hsch + (Post := Post) SEND) (J.chargeP1A hA En used true)) ?_ + rintro ⟨s4, a4⟩ ⟨⟨ha4, s2, τ, s3, τ3, τ4, Q3, R3, Q4, -, -, H3, hP3, hres, hcb, H4, hk4, -, -, hm4, -, hc43, -, f4⟩, + sysl, symL, bxi⟩ + dsimp only at ha4 H4 sysl symL bxi f4 + subst ha4 + obtain ⟨K13, ⟨SS3, SZ3, mem3⟩, SV3, atT3, atE3, fault3, HE3, w13, hrunx, Q13, hc3⟩ := hP3 + have hmem4 : ((), τ4) ∈ (reschedReprogA τ3).1 := by rw [hres]; exact Set.mem_singleton _ + have FR4 := reschedReprog_frameA hmem4 + have Q14 : QBlkA (specPtr '' E) (specPtr '' N) σ τ4 := by + have Q34 : QBlkA (specPtr '' E) (specPtr '' N) τ3 τ4 := QSendA.toBlk Q13.sorted + ((show PresR QKeepA reschedReprogA by unfold reschedReprogA; qpres) τ3 _ hmem4).toSend + exact ⟨Q34.sorted, fun h => Q34.act (Q13.act h), fun p y h => by + obtain ⟨x, hx, f⟩ := Q34.rt p y h + obtain ⟨x0', hx0', f0⟩ := Q13.rt p x hx + exact ⟨x0', hx0', fun h => f (f0 h)⟩, Q13.dom.trans Q34.dom⟩ + have K14 : SIKXA σ.idleThread M Nq false σ τ4 := K13.transS (reschedReprog_sikA hmem4) + have SS4 : SpecSchedA σ τ4 := SS3.trans FR4.1 + have SZ4 : SSzA σ τ4 := SSzA.trans SZ3 FR4.2.1 + have mem4 : τ4.machineState = σ.machineState := hm4.trans mem3 + have SV4 := reschedReprog_svA SV3 hmem4 + have kb3 : L.ksb s3 = L.ksb s := w13.1 + have w14 : WritesIn st s s4 (fun b => + BlkSendFpA st Z.T (fun σ s => MWrA (st := st) Z m σ s) σ s t ep b ∨ FaultWA t b) := + (w13.trans f4.wr).mono fun b h => h.elim id fun h => + Or.inl (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (mwr_back rfl rfl SZ3 kb3 h))))))) + have w14I : WritesIn st s s4 (fun b => IpcFpA st Z E Rp Bf m σ s b ∨ FaultWA t b) := + w14.mono fun b h => h.imp (fun h => by + have hta := J.iplaced.e.tcb t htZ + have tw : ∀ (k : Nat), b.val = t.val + k → TWOffN k → IpcFpA st Z E Rp Bf m σ s b := fun k hv hk => + Or.inl (Or.inr (Or.inr (Or.inl ⟨t, htZ, Or.inl (twf_ofA k hv hk)⟩))) + rcases h with c | c | c | c | c | ⟨v, hv, c⟩ | c + · exact tw 3328 (by rw [c]; exact Endp.val_st hta) (by unfold TWOffN; omega) + · exact tw 3336 (by rw [c]; exact Endp.val_blk hta) (by unfold TWOffN; omega) + · exact tw 3344 (by rw [c]; exact Endp.val_badge hta) (by unfold TWOffN; omega) + · exact tw 3352 (by rw [c]; exact Endp.val_flags hta) (by unfold TWOffN; omega) + · exact Or.inl (Or.inl ⟨ep, he, c⟩) + · have hvT : v ∈ Z.T := by + rcases hv with rfl | hv + · exact htZ + · exact hv.1 + exact Or.inl (Or.inr (Or.inr (Or.inl ⟨v, hvT, Or.inr c⟩))) + · exact Or.inr c) id + have w14N : WritesIn st s s4 (fun b => SchedStepNWA (st := st) N E Rp Z m σ s b ∨ BufW Bf b) := + w14I.mono fun b h => Or.elim h + (fun h => Or.elim h (ewrite_stepNA_buf J.sr.core.tcbs) (fun h => Or.inl (Or.inl (Or.inl h)))) + (fun h => Or.inl (Or.inl (Or.inr (faultTcbStepA J.sr.core.tcbs htZ h)))) + have HE4 : EpStateRel Z.T E Rp Bf (L.mem s4) τ4 := by + obtain ⟨FE, VIEW⟩ := epFrameOfMcsA H3.sched.toSchedSRN J.ipcPlace f4 (by rw [hm4]) + exact HE3.frame J.iplaced.e FE VIEW + have wk : NtfnWaitKeepMA M Z.T N (L.mem s) (L.mem s4) := J.ntfnWait_blkFA htZ he w14 + -- the mover + have hnI : specPtr t ≠ σ.idleThread := fun e => by + obtain ⟨xi, hxi, hsi'⟩ := J.sinv.idleTcb + rw [← e, hkt0] at hxi; cases hxi; rw [hsi'] at hrunx; cases hrunx + have notM : ∀ c < fl4A64Platform.numNodes, ¬ M (m.idleThreadOf c) := by + intro c hc h + obtain ⟨y1, hy1, hs1⟩ := J.sidle.f3 c hc + rw [show m.idleThreadOf c = specPtr t from h, hkt0] at hy1; cases hy1; rw [hs1] at hrunx; cases hrunx + have atT4 : ∃ (x1 : Tcb) (pl : SenderPayload), τ4.kheap (specPtr t) = + some (.tcb (blkTcbA x1 (specPtr ep) pl)) := by rw [hk4]; exact atT3 + have atE4 : ∃ q, τ4.kheap (specPtr ep) = some (.endpoint (.sendEP q)) ∧ specPtr t ∈ q := by + rw [hk4]; exact atE3 + have hMs : ∀ u ∈ Z.T, M (specPtr u) → ∀ x, σ.kheap (specPtr u) = some (.tcb x) → + ∀ p, x.tcbState ≠ .blockedOnNotification p := by + intro u _ hu x hx p hp + rw [show specPtr u = specPtr t from hu, hkt0] at hx; cases hx; rw [hp] at hrunx; cases hrunx + have hMs' : ∀ u ∈ Z.T, M (specPtr u) → ∀ x, τ4.kheap (specPtr u) = some (.tcb x) → + ∀ p, x.tcbState ≠ .blockedOnNotification p := by + intro u _ hu x hx p hp + obtain ⟨x1, pl, h1⟩ := atT4 + rw [show specPtr u = specPtr t from hu, h1] at hx + cases hx + cases hp + have hMq : ∀ y, M y → ∀ dm pr, y ∉ σ.readyQueues dm pr := by + intro y hy dm pr hm + rw [show y = specPtr t from hy] at hm + exact P.notReady dm pr (by rw [hσct]; exact hm) + have hMv : ∀ u ∈ Z.T, M (specPtr u) → L.mem s (Fl4ProofsA64.P1.stA u) ≠ sched.TS_RUNNING_VM := by + intro u _ hu h' + rw [specPtr_inj (show specPtr u = specPtr t from hu)] at h' + exact En.nvm h' + have sinv : SInvXA τ4 m fl4A64Platform.numNodes := + K14.sinv J.sinv notM (fun h => hnI h.symm) hMq (fun _ h => h.elim) (fun _ h => h.elim) noSwitch + (fun y hy hr => by rw [show y = specPtr t from hy] at hr; exact I.notRel (by rw [hσct]; exact hr)) + (Or.inr (hc43.trans hc3)) (J.sidle.rdyA J.sinv) + (by + have F3 := K13.faultStA J.sinv.faultSt + (fun p hp x y hx hy => by + obtain ⟨y', hy', e⟩ := fault3 p hp x hx + rw [hy] at hy'; cases hy'; exact e) + (fun p hp y hy hf => by + obtain ⟨x1, pl, h1⟩ := atT3 + rw [show p = specPtr t from hp, h1] at hy; cases hy + exact Or.inl ⟨_, _, rfl⟩) + unfold FaultStOkA at F3 ⊢; rw [hk4]; exact F3) + have idle : IdleNoFaultA m τ4 := fun c hc => by + obtain ⟨x, hx, hf⟩ := J.idleNoFault c hc + obtain ⟨y, hy, e⟩ := fault3 _ (fun e => notM c hc e) x hx + exact ⟨y, by rw [hk4]; exact hy, e.trans hf⟩ + have cur4 : τ4.curThread = σ.curThread := by rw [K14.frame] + have nr4 : ∀ x, τ4.kheap τ4.curThread = some (.tcb x) → runnable x.tcbState = false := by + intro x hx + obtain ⟨x1, pl, h1⟩ := atT4 + rw [cur4, hσct, h1] at hx + cases hx + rfl + refine ⟨τ4, hcb, rfl, ⟨Q4, R3, Z.curOf, Z.curScOf, ?_⟩, cur4, nr4⟩ + exact J.of_ipcStepUXSA (Q := Q4) (Rl := R3) (curOf := Z.curOf) (curScOf := Z.curScOf) w14N SS4 SZ4 Q14 + (fun x _ => by rw [mem4]) (fun _ _ => rfl) bxi sysl symL + (J.sys.ent.user.ksb w14.1) (J.j7_stepNUA w14N hXv7 τ4) H4.sched HE4 + (J.ntfn_ipcXMA (w14I.mono fun _ h => h.imp id Or.inl) htZ K14 HE4 wk hMs hMs') + (J.reply_ipcXA w14I htZ K14) J.deadExt.reply (SIKXA.ipcKeepN K14) idle (J.tw_ipcXA (w14I.mono fun _ h => h.imp id Or.inl) htZ) sinv + (K14.sidle J.sidle notM (J.sidle.rdyA J.sinv)) (J.runVcpu_ipcXA (w14I.mono fun _ h => h.imp id Or.inl) htZ K14 HE4 hMv) (J.blkQ_blkXA htZ he K14 atT4 atE4 HE4 wk) SV4 + +end + +section rec +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {nA : U64} {s : S} {a : A} {σ : State Unit} {m : McState} + +set_option maxHeartbeats 2000000 in +/-- **`check_budget` at the record on an exhausted head refill taking the timeout send with no receiver +waiting.** -/ +theorem JStage3A.checkBudgetSendBlkA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar nA)) s a σ m) + (noSwitch : ∀ u, σ.schedulerAction ≠ .switchThread u) (I : CurInvA σ) (Idle : ActIdleA σ) + (hcore : L.mem s (scCoreA (L.cscW s)) = L.cpu s) (hni : σ.curSc ≠ idleScPtr → NotIdleScA m σ.curSc) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (one : fl4A64Platform.numNodes = 1) + (hag : fl4MinBudget = MIN_BUDGET) (hMin : Fl4MinBudgetAgreeA ar a) + (hLimit : sporadic.max_release_time ar a ⦃ v => v.bv = fl4MaxReleaseTime ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) (hconv : Fl4TicksToUsLegacyAgree) + (hA : P0.ArchTotal ar) (En : ChargeEntryA st Ac s) (hX : BufOffXA st Z H OS Pp Tb Lv Bf s) + (hex : fl4GetScRefillSufficient (Z := Unit) σ.curSc σ.consumedTime σ = ({(false, σ)}, False)) + (hns : ¬ NoTimeoutA σ.consumedTime σ) (hR : TimeoutBlkA st Z E s σ) : + sched.check_budget st ar s a ⦃ r => ∃ τ, fl4CheckBudget (Z := Unit) σ = ({(r.1, τ)}, False) ∧ + JStage3QA st ar AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes nA m + r.2.1 r.2.2 τ ∧ r.1 = false ∧ τ.curThread = σ.curThread ∧ + ∀ x, τ.kheap τ.curThread = some (.tcb x) → runnable x.tcbState = false ⦄ := by + obtain ⟨xt, slot, ep, hbg, sd, rv, g, gr, xe, hkt, hslot, hsl, hcap, hgc, he, hk, hc⟩ := hR + exact J.checkBudgetOfChargeA I hMin hex hns fun used hu => + J.chargeSendBlkA noSwitch I Idle hcore hni hCs one hag hMin hLimit hmax htu hconv hA En hX used hu hkt hslot + hsl hcap hgc he hk hc + +set_option maxHeartbeats 2000000 in +/-- **`check_budget_restart` at the record taking the timeout send with no receiver waiting.** -/ +theorem JStage3A.checkBudgetRestartSendBlkA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar nA)) s a σ m) + (noSwitch : ∀ u, σ.schedulerAction ≠ .switchThread u) (I : CurInvA σ) (Idle : ActIdleA σ) + (hcore : L.mem s (scCoreA (L.cscW s)) = L.cpu s) (hni : σ.curSc ≠ idleScPtr → NotIdleScA m σ.curSc) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (one : fl4A64Platform.numNodes = 1) + (hag : fl4MinBudget = MIN_BUDGET) (hMin : Fl4MinBudgetAgreeA ar a) + (hLimit : sporadic.max_release_time ar a ⦃ v => v.bv = fl4MaxReleaseTime ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) (hconv : Fl4TicksToUsLegacyAgree) + (hA : P0.ArchTotal ar) (En : ChargeEntryA st Ac s) (hX : BufOffXA st Z H OS Pp Tb Lv Bf s) + (hex : fl4GetScRefillSufficient (Z := Unit) σ.curSc σ.consumedTime σ = ({(false, σ)}, False)) + (hns : ¬ NoTimeoutA σ.consumedTime σ) (hR : TimeoutBlkA st Z E s σ) : + sched.check_budget_restart st ar s a ⦃ r => ∃ τ, fl4CheckBudgetRestart (Z := Unit) σ = ({(r.1, τ)}, False) ∧ + JStage3QA st ar AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes nA m + r.2.1 r.2.2 τ ⦄ := + J.checkBudgetRestartOfCheckA + (J.checkBudgetSendBlkA noSwitch I Idle hcore hni hCs one hag hMin hLimit hmax htu hconv hA En hX hex hns hR) + +set_option maxHeartbeats 1000000 in +/-- **The interrupt entry's exhausted-budget branch with both timeout send cases** (module docstring). -/ +theorem kentryChargeIrqSendAllA {AR' : A → HwRootA → Prop} {Clock : A → MachineState → Prop} + (noSwitch : ∀ u, σ.schedulerAction ≠ .switchThread u) (hni : σ.curSc ≠ idleScPtr → NotIdleScA m σ.curSc) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (one : fl4A64Platform.numNodes = 1) + (hag : fl4MinBudget = MIN_BUDGET) (hmb : ∀ a', Fl4MinBudgetAgreeA ar a') + (hLimit : ∀ a', sporadic.max_release_time ar a' ⦃ v => v.bv = fl4MaxReleaseTime ⦄) + (hA : P0.ArchTotal ar) (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) (hconv : Fl4TicksToUsLegacyAgree) + (hX : ∀ s1 a1 σ1, JStage3A st AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes + (J5A st) (J7A st (J7R2A st ar nA)) s1 a1 σ1 m → BufOffXA st Z H OS Pp Tb Lv Bf s1) + : + KEntryChargeA st ar AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ nA m Clock s a σ + (sched.check_budget st ar) fl4CheckBudget := by + refine kentryChargeIrqSendA noSwitch hni hCs one hag hmb hLimit hA hmax htu hconv hX + fun s1 a1 σ1 e1 J1 En1 CI1 Act1 Cl1 hex hns hR => ?_ + have eσ1 : σ1 = fl4TimestampState (rawNextA σ.machineState) { σ with machineState := rawStateA σ.machineState } := + (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst ((fl4UpdateTimestamp_eqA σ).symm.trans + e1)))).2.symm + have sw1 : ∀ u, σ1.schedulerAction ≠ .switchThread u := by rw [eσ1]; exact noSwitch + have ni1 : σ1.curSc ≠ idleScPtr → NotIdleScA m σ1.curSc := by rw [eσ1]; exact hni + have B := J1.timeoutSelA hns hR + refine WP.spec_mono (J1.checkBudgetSendBlkA sw1 CI1 Act1 (J1.curScOnCoreA hCs one) ni1 hCs one hag (hmb a1) + (hLimit a1) hmax htu hconv hA En1 (hX s1 a1 σ1 J1) hex hns B) ?_ + rintro r ⟨τ, h, Q, -⟩ + exact ⟨τ, h, Q⟩ + +set_option maxHeartbeats 1000000 in +/-- **The syscall and fault entries' exhausted-budget branch with both timeout send cases** (module docstring). -/ +theorem kentryChargeRestartSendAllA {AR' : A → HwRootA → Prop} {Clock : A → MachineState → Prop} + (noSwitch : ∀ u, σ.schedulerAction ≠ .switchThread u) (hni : σ.curSc ≠ idleScPtr → NotIdleScA m σ.curSc) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (one : fl4A64Platform.numNodes = 1) + (hag : fl4MinBudget = MIN_BUDGET) (hmb : ∀ a', Fl4MinBudgetAgreeA ar a') + (hLimit : ∀ a', sporadic.max_release_time ar a' ⦃ v => v.bv = fl4MaxReleaseTime ⦄) + (hA : P0.ArchTotal ar) (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) (hconv : Fl4TicksToUsLegacyAgree) + (hX : ∀ s1 a1 σ1, JStage3A st AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes + (J5A st) (J7A st (J7R2A st ar nA)) s1 a1 σ1 m → BufOffXA st Z H OS Pp Tb Lv Bf s1) + : + KEntryChargeA st ar AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ nA m Clock s a σ + (sched.check_budget_restart st ar) fl4CheckBudgetRestart := by + refine kentryChargeRestartSendA noSwitch hni hCs one hag hmb hLimit hA hmax htu hconv hX + fun s1 a1 σ1 e1 J1 En1 CI1 Act1 Cl1 hex hns hR => ?_ + have eσ1 : σ1 = fl4TimestampState (rawNextA σ.machineState) { σ with machineState := rawStateA σ.machineState } := + (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst ((fl4UpdateTimestamp_eqA σ).symm.trans + e1)))).2.symm + have sw1 : ∀ u, σ1.schedulerAction ≠ .switchThread u := by rw [eσ1]; exact noSwitch + have ni1 : σ1.curSc ≠ idleScPtr → NotIdleScA m σ1.curSc := by rw [eσ1]; exact hni + have B := J1.timeoutSelA hns hR + exact J1.checkBudgetRestartSendBlkA sw1 CI1 Act1 (J1.curScOnCoreA hCs one) ni1 hCs one hag (hmb a1) + (hLimit a1) hmax htu hconv hA En1 (hX s1 a1 σ1 J1) hex hns B + +end rec + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3KEntrySendKeepA.lean b/proofs/Fl4ProofsA64/P3/Stage3KEntrySendKeepA.lean new file mode 100644 index 000000000..421a2d6fd --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3KEntrySendKeepA.lean @@ -0,0 +1,730 @@ +/- +P3 (aarch64, A64-P3 Stage 3, kernel-entry composition, step 2): **the spec keeps and the record consumers across the +budget charge's timeout send** (`charge_budget … true` whose `end_timeslice` takes `handle_timeout`). + +The charge's head (the current SC's refills and consumed time, `consumedTime := 0`), the timeout fault written into the +current TCB, the send to the waiting receiver and `rescheduleRequired` together are not an `SIKA` step: the fault and +`consumedTime` change. **`SIKXA`** is `SIKA` up to those two: + +* `TcbIpcNA` (`TcbIpcA` without the fault), `SIKXA`, `SIKA.toX`, `SIKXA.transS` (an `SIKA` step after), the + transports `SIKXA.tcbF` … `SIKXA.irqA`, and `SIKXA.sinv` / `SIKXA.sidle` (`SIKA.sinv` / `SIKA.sidle` on `SIKXA`). +* `IpcKeepNA` (`IpcKeepA` without the fault), `SIKXA.ipcKeepN`. +* `JStage3A.stKept_ipcXA`, `runVcpu_ipcXA`, `ntfn_ipcXA`, `blkQ_ipcXA`, `reply_ipcXA`: `P3/Stage3IpcFl4KeepA`'s + consumers on `SIKXA`, the write set widened by the current thread's fault words (`FaultWA`). +* `JStage3A.of_ipcStepUXA`: `of_ipcStepUA` with `IpcKeepNA` and the idle threads' faults (`IdleNoFaultA`) given. + +The bodies are lift 2b's (`P3/Stage3IpcKeepSA`, `P3/Stage3IpcFl4KeepA`, `P3/Stage3IpcGlueUA`), generated by +`handoff/tools/kentry2_sikx_gen.py` and edited. No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3IpcGlueUA +import Fl4ProofsA64.P3.Stage3IpcFl4KeepA +import Fl4ProofsA64.P3.Stage3TimeoutSendBA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P3 Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Classical + +noncomputable section + +/-! ## The relation -/ + +/-- `TcbIpcA` without the fault. -/ +structure TcbIpcNA (i : ObjRef) (M : ObjRef → Prop) (don : Bool) (p : ObjRef) (x y : Tcb) : Prop where + frame : y.tcbIpcframe = x.tcbIpcframe + buf : y.tcbIpcBuffer = x.tcbIpcBuffer + mcp : y.tcbMcpriority = x.tcbMcpriority + yt : y.tcbYieldTo = x.tcbYieldTo + caps : tcbCnodeMap y = tcbCnodeMap x + st : y.tcbState = x.tcbState ∨ (M p ∧ y.tcbState ≠ .idleThreadState) + sc : y.tcbSchedContext = x.tcbSchedContext ∨ (don = true ∧ p ≠ i ∧ (M p ∨ y.tcbSchedContext = none)) + vcpu : y.tcbArch.tcbVcpu = x.tcbArch.tcbVcpu + flags : y.tcbFlags = x.tcbFlags + +/-- **`SIKA` up to the TCBs' faults and `consumedTime`** (module docstring). -/ +structure SIKXA (i : ObjRef) (M N : ObjRef → Prop) (don : Bool) (σ σ' : State Unit) : Prop where + frame : σ' = { σ with kheap := σ'.kheap, readyQueues := σ'.readyQueues, releaseQueue := σ'.releaseQueue, + schedulerAction := σ'.schedulerAction, reprogramTimer := σ'.reprogramTimer, + machineState := σ'.machineState, consumedTime := σ'.consumedTime } + obj : ∀ p, ObjIpcA i M don p (σ.kheap p) (σ'.kheap p) ∨ + ∃ x y, σ.kheap p = some (.tcb x) ∧ σ'.kheap p = some (.tcb y) ∧ TcbIpcNA i M don p x y + rq : ∀ d p y, y ∈ σ'.readyQueues d p → y ∈ σ.readyQueues d p ∨ N y ∨ σ.schedulerAction = .switchThread y + rls : ∀ y ∈ σ'.releaseQueue, y ∈ σ.releaseQueue + act : σ'.schedulerAction = σ.schedulerAction ∨ σ'.schedulerAction = .chooseNewThread ∨ + ∃ y, N y ∧ σ'.schedulerAction = .switchThread y + rdy : RdyA σ → RdyA σ' + +section +variable {i : ObjRef} {M N : ObjRef → Prop} {don : Bool} + +theorem TcbIpcA.toN {p : ObjRef} {x y : Tcb} (h : TcbIpcA i M don p x y) : TcbIpcNA i M don p x y := + ⟨h.frame, h.buf, h.mcp, h.yt, h.caps, h.st, h.sc, h.vcpu, h.flags⟩ + +theorem TcbIpcNA.refl (p : ObjRef) (x : Tcb) : TcbIpcNA i M don p x x := + ⟨rfl, rfl, rfl, rfl, rfl, Or.inl rfl, Or.inl rfl, rfl, rfl⟩ + +theorem TcbIpcNA.transA {p : ObjRef} {x y z : Tcb} (h : TcbIpcNA i M don p x y) (h' : TcbIpcA i M don p y z) : + TcbIpcNA i M don p x z := by + refine ⟨h'.frame.trans h.frame, h'.buf.trans h.buf, h'.mcp.trans h.mcp, h'.yt.trans h.yt, h'.caps.trans h.caps, + ?_, ?_, h'.vcpu.trans h.vcpu, h'.flags.trans h.flags⟩ + · rcases h'.st with e | e + · rw [e]; exact h.st + · exact Or.inr e + · rcases h'.sc with e | e + · rw [e]; exact h.sc + · exact Or.inr e + +theorem SIKA.toX {σ σ' : State Unit} (h : SIKA i M N don σ σ') : SIKXA i M N don σ σ' := by + have hc : σ'.consumedTime = σ.consumedTime := by rw [h.frame] + refine ⟨?_, fun p => Or.inl (h.obj p), h.rq, h.rls, h.act, h.rdy⟩ + rw [hc] + exact h.frame + +/-- An `SIKXA` step, then an `SIKA` step. -/ +theorem SIKXA.transS {σ1 σ2 σ3 : State Unit} (h1 : SIKXA i M N don σ1 σ2) (h2 : SIKA i M N don σ2 σ3) : + SIKXA i M N don σ1 σ3 := by + have hc : σ3.consumedTime = σ2.consumedTime := by rw [h2.frame] + refine ⟨?_, fun p => ?_, fun d p y hy => ?_, fun y hy => h1.rls y (h2.rls y hy), ?_, fun g => h2.rdy (h1.rdy g)⟩ + · have a := h1.frame + have b := h2.frame + rw [a] at b + rw [hc] + exact b + · rcases h1.obj p with o | ⟨x, y, e1, e2, g⟩ + · exact Or.inl (o.trans (h2.obj p)) + · obtain ⟨z, ez, g2⟩ := h2.tcbF e2 + exact Or.inr ⟨x, z, e1, ez, g.transA g2⟩ + · rcases h2.rq d p y hy with h | h | h + · exact h1.rq d p y h + · exact Or.inr (Or.inl h) + · rcases h1.act with e | e | ⟨z, hz, e⟩ + · exact Or.inr (Or.inr (e ▸ h)) + · rw [e] at h; cases h + · rw [e] at h; cases h; exact Or.inr (Or.inl hz) + · rcases h2.act with e | e | e + · rw [e]; exact h1.act + · exact Or.inr (Or.inl e) + · exact Or.inr (Or.inr e) + +/-! ## Transports -/ + +theorem SIKXA.tcbF {σ σ' : State Unit} (R : SIKXA i M N don σ σ') {p : ObjRef} {x : Tcb} + (h : σ.kheap p = some (.tcb x)) : ∃ y, σ'.kheap p = some (.tcb y) ∧ TcbIpcNA i M don p x y := by + rcases R.obj p with o | ⟨x', y, e1, e2, g⟩ + · rw [h] at o + generalize eb : σ'.kheap p = b at o + cases o with + | same => exact ⟨x, rfl, TcbIpcNA.refl p x⟩ + | tcb _ y g => exact ⟨y, rfl, g.toN⟩ + · rw [h] at e1; cases e1 + exact ⟨y, e2, g⟩ + +theorem SIKXA.tcbB {σ σ' : State Unit} (R : SIKXA i M N don σ σ') {p : ObjRef} {y : Tcb} + (h : σ'.kheap p = some (.tcb y)) : ∃ x, σ.kheap p = some (.tcb x) ∧ TcbIpcNA i M don p x y := by + rcases R.obj p with o | ⟨x, y', e1, e2, g⟩ + · rw [h] at o + generalize ea : σ.kheap p = a at o + cases o with + | same => exact ⟨y, rfl, TcbIpcNA.refl p y⟩ + | tcb x _ g => exact ⟨x, rfl, g.toN⟩ + · rw [h] at e2; cases e2 + exact ⟨x, e1, g⟩ + +theorem SIKXA.scF {σ σ' : State Unit} (R : SIKXA i M N don σ σ') {p : ObjRef} {x : SchedContext} {k : Nat} + (h : σ.kheap p = some (.schedContext x k)) : + ∃ y, σ'.kheap p = some (.schedContext y k) ∧ ScIpcA i don p x y := by + rcases R.obj p with o | ⟨x', y, e1, -, -⟩ + · rw [h] at o + generalize eb : σ'.kheap p = b at o + cases o with + | same => exact ⟨x, rfl, rfl, rfl, id, fun _ => ⟨rfl, rfl⟩, Or.inl rfl, fun _ => rfl⟩ + | sc _ y _ g => exact ⟨y, rfl, g⟩ + · rw [h] at e1; cases e1 + +theorem SIKXA.scB {σ σ' : State Unit} (R : SIKXA i M N don σ σ') {p : ObjRef} {y : SchedContext} {k : Nat} + (h : σ'.kheap p = some (.schedContext y k)) : + ∃ x, σ.kheap p = some (.schedContext x k) ∧ ScIpcA i don p x y := by + rcases R.obj p with o | ⟨x, y', -, e2, -⟩ + · rw [h] at o + generalize ea : σ.kheap p = a at o + cases o with + | same => exact ⟨y, rfl, rfl, rfl, id, fun _ => ⟨rfl, rfl⟩, Or.inl rfl, fun _ => rfl⟩ + | sc x _ _ g => exact ⟨x, rfl, g⟩ + · rw [h] at e2; cases e2 + +theorem SIKXA.ntfnF {σ σ' : State Unit} (R : SIKXA i M N don σ σ') {p : ObjRef} {x : Notification} + (h : σ.kheap p = some (.notification x)) : σ'.kheap p = some (.notification x) := by + rcases R.obj p with o | ⟨x', y, e1, -, -⟩ + · rw [h] at o + generalize eb : σ'.kheap p = b at o + cases o with + | same => rfl + · rw [h] at e1; cases e1 + +theorem SIKXA.epF {σ σ' : State Unit} (R : SIKXA i M N don σ σ') {p : ObjRef} {x : Endpoint} + (h : σ.kheap p = some (.endpoint x)) : + ∃ y, σ'.kheap p = some (.endpoint y) ∧ ∀ u ∈ x.epQueue, ¬ M u → u ∈ y.epQueue := by + rcases R.obj p with o | ⟨x', y, e1, -, -⟩ + · rw [h] at o + generalize eb : σ'.kheap p = b at o + cases o with + | same => exact ⟨x, rfl, fun _ h _ => h⟩ + | ep _ y g => exact ⟨y, rfl, g⟩ + · rw [h] at e1; cases e1 + +theorem SIKXA.replyB {σ σ' : State Unit} (R : SIKXA i M N false σ σ') {p : ObjRef} {y : Reply} + (h : σ'.kheap p = some (.reply y)) : ∃ x, σ.kheap p = some (.reply x) ∧ y.replySc = x.replySc := by + rcases R.obj p with o | ⟨x, y', -, e2, -⟩ + · rw [h] at o + generalize ea : σ.kheap p = a at o + cases o with + | same => exact ⟨y, rfl, rfl⟩ + | reply x _ g => exact ⟨x, rfl, g rfl⟩ + · rw [h] at e2; cases e2 + +/-- A cap of an object of the new state is a cap of the old object. -/ +theorem SIKXA.capOfA {σ σ' : State Unit} (R : SIKXA i M N don σ σ') (p : ObjRef) (b : CnodeIndex) : + (σ'.kheap p).bind (fun o => capOf o b) = (σ.kheap p).bind (fun o => capOf o b) := by + rcases R.obj p with o | ⟨x, y, e1, e2, g⟩ + · generalize σ.kheap p = a at o ⊢ + generalize σ'.kheap p = c at o ⊢ + cases o with + | same => rfl + | tcb x y g => simp [capOf, g.caps] + | sc => rfl + | ep => rfl + | reply => rfl + · rw [e1, e2] + simp [capOf, g.caps] + +theorem SIKXA.specCap {σ σ' : State Unit} (R : SIKXA i M N don σ σ') (sl : CslotPtr) : + specCapAtA σ' sl = specCapAtA σ sl := by + unfold specCapAtA + rcases R.obj sl.1 with o | ⟨x, y, e1, e2, g⟩ + · generalize σ.kheap sl.1 = a at o ⊢ + generalize σ'.kheap sl.1 = c at o ⊢ + cases o with + | same => rfl + | tcb x y g => simp only [g.caps] + | sc => rfl + | ep => rfl + | reply => rfl + · rw [e1, e2] + simp only [g.caps] + +theorem SIKXA.irqA {σ σ' : State Unit} (R : SIKXA i M N don σ σ') (h : SpecIrqA σ) : SpecIrqA σ' := by + have hnode : σ'.interruptIrqNode = σ.interruptIrqNode := by rw [R.frame] + have hct : ∀ k p, capTableAt k p σ' ↔ capTableAt k p σ := fun k p => by + rcases R.obj p with o | ⟨x, y, e1, e2, -⟩ + · unfold capTableAt + generalize σ.kheap p = a at o ⊢ + generalize σ'.kheap p = c at o ⊢ + cases o <;> simp + · unfold capTableAt + rw [e1, e2] + simp + have capB : ∀ p o' b c, σ'.kheap p = some o' → capOf o' b = some c → + ∃ o, σ.kheap p = some o ∧ capOf o b = some c := fun p o' b c hp hc => by + have e := R.capOfA p b + rw [hp] at e + cases hk : σ.kheap p with + | none => rw [hk] at e; simp [hc] at e + | some o => rw [hk] at e; exact ⟨o, rfl, e.symm.trans hc⟩ + obtain ⟨⟨⟨hinj, hcn⟩, hdom⟩, hsl, href⟩ := h + refine ⟨⟨⟨?_, fun irq hi => ?_⟩, fun irq hi k hk => ?_⟩, fun irq hi o b c ho hc => ?_, fun p o b c ho hc => ?_⟩ + · rw [hnode]; exact hinj + · rw [hnode]; exact (hct _ _).2 (hcn irq hi) + · rw [hnode] at hk; exact hdom irq hi k ((hct _ _).1 hk) + · rw [hnode] at ho + obtain ⟨o0, h0, hc0⟩ := capB _ _ _ _ ho hc + exact hsl irq hi o0 b c h0 hc0 + · rw [hnode] + obtain ⟨o0, h0, hc0⟩ := capB _ _ _ _ ho hc + exact href p o0 b c h0 hc0 + +/-- **`SInvXA`** across an `SIKXA` step (`SIKA.sinv`). -/ +theorem SIKXA.sinv {σ σ' : State Unit} {m : McState} {n : Nat} (R : SIKXA σ.idleThread M N don σ σ') + (h : SInvXA σ m n) (hMi : ∀ c < n, ¬ M (m.idleThreadOf c)) (hMI : ¬ M σ.idleThread) + (hMq : ∀ y, M y → ∀ d p, y ∉ σ.readyQueues d p) + (hN : ∀ y, N y → y ≠ σ.curThread ∧ y ≠ σ.idleThread ∧ (∀ c < n, m.idleThreadOf c ≠ y) ∧ + ∀ z, σ'.kheap y = some (.tcb z) → runnable z.tcbState = true) + (hN' : ∀ y, N y → ∃ z, σ.kheap y = some (.tcb z)) + (hsw : ∀ u, σ.schedulerAction ≠ .switchThread u) + (hMrls : ∀ y, M y → y ∉ σ.releaseQueue) + (hcons : σ'.consumedTime = σ.consumedTime ∨ σ'.consumedTime = 0) (hRdy : RdyA σ) + (hF : FaultStOkA σ') : SInvXA σ' m n := by + have e := R.frame + have hcur : σ'.curThread = σ.curThread := by rw [e] + have hidle : σ'.idleThread = σ.idleThread := by rw [e] + have hcsc : σ'.curSc = σ.curSc := by rw [e] + have newq : ∀ d p y, y ∈ σ'.readyQueues d p → y ∈ σ.readyQueues d p ∨ N y := fun d p y hy => by + rcases R.rq d p y hy with a | a | a + · exact Or.inl a + · exact Or.inr a + · exact absurd a (hsw y) + refine + { mc := h.mc + view := by unfold McView; rw [hidle]; exact h.view + curTcb := by + obtain ⟨x, hx⟩ := h.curTcb + obtain ⟨y, hy, -⟩ := R.tcbF hx + exact ⟨y, by rw [hcur]; exact hy⟩ + idleTcb := by + obtain ⟨x, hx, hs⟩ := h.idleTcb + obtain ⟨y, hy, g⟩ := R.tcbF hx + exact ⟨y, by rw [hidle]; exact hy, (g.st.resolve_right fun a => hMI a.1).trans hs⟩ + onlyIdle := fun t y hy hs => by + obtain ⟨x, hx, g⟩ := R.tcbB hy + rcases g.st with a | a + · exact h.onlyIdle t x hx (a.symm.trans hs) + · exact absurd hs a.2 + idleSc := by + obtain ⟨x, y, k, h1, h2, h3, h4⟩ := h.idleSc + obtain ⟨x', h1', gx⟩ := R.tcbF h1 + obtain ⟨y', h3', gy⟩ := R.scF h3 + refine ⟨x', y', k, by rw [hidle]; exact h1', ?_, h3', by rw [hidle]; exact (gy.idle rfl).1.trans h4⟩ + exact (gx.sc.resolve_right fun a => a.2.1 rfl).trans h2 + curSc := by + obtain ⟨y, k, hy⟩ := h.curSc + obtain ⟨y', h', -⟩ := R.scF hy + exact ⟨y', k, by rw [hcsc]; exact h'⟩ + ctQ := fun ha d p hm => by + rw [hcur] at hm + have ha0 : σ.schedulerAction = .resumeCurThread := by + rcases R.act with e | e | ⟨y, -, e⟩ + · rw [← e]; exact ha + · rw [e] at ha; cases ha + · rw [e] at ha; cases ha + rcases newq d p _ hm with a | a + · exact h.ctQ ha0 d p a + · exact (hN _ a).1 rfl + idleQ := fun d p c hc hm => by + rcases newq d p _ hm with a | a + · exact h.idleQ d p c hc a + · exact (hN _ a).2.2.1 c hc rfl + rq := fun d p t ht => by + rcases newq d p t ht with a | a + · obtain ⟨x, hx, hr⟩ := h.rq d p t a + obtain ⟨y, hy, g⟩ := R.tcbF hx + exact ⟨y, hy, by rw [g.st.resolve_right fun b => hMq t b.1 d p a]; exact hr⟩ + · obtain ⟨x, hx⟩ := hN' t a + obtain ⟨y, hy, -⟩ := R.tcbF hx + exact ⟨y, hy, (hN t a).2.2.2 y hy⟩ + rls := fun t ht => by + obtain ⟨x, hx⟩ := h.rls t (R.rls t ht) + obtain ⟨y, hy, -⟩ := R.tcbF hx + exact ⟨y, hy⟩ + rpos := fun p y k hy a => by + obtain ⟨x, hx, g⟩ := R.scB hy + exact g.ne (h.rpos p x k hx (by unfold scActive at a ⊢; rw [← g.rm]; exact a)) + irqN := R.irqA h.irqN + zc := fun t x' hx e' hr => by + rcases hcons with hc | hc + · obtain ⟨x, hx0, g⟩ := R.tcbB hx + have hr0 := R.rls t hr + have es : x.tcbSchedContext = x'.tcbSchedContext := + g.sc.elim (fun a => a.symm) (fun d => d.2.2.elim (fun hm => absurd hr0 (hMrls t hm)) + (fun hn => absurd (hn.symm.trans e') (Option.some_ne_none _).symm)) + rw [hc] + exact h.zc t x hx0 (by rw [es, e', hcsc]) hr0 + · exact hc + rlsRun := fun t ht => by + have hr0 := R.rls t ht + obtain ⟨x, hx, hr⟩ := h.rlsRun t hr0 + obtain ⟨y, hy, g⟩ := R.tcbF hx + exact ⟨y, hy, by rw [g.st.resolve_right fun b => hMrls t b.1 hr0]; exact hr⟩ + curScReady := (R.rdy hRdy).2 + faultSt := hF } + +/-- (item 11) `SIKXA.sinv`'s `hF`: off the movers the step keeps state and fault, the movers are given. -/ +theorem SIKXA.faultStA {σ σ' : State Unit} {i : ObjRef} (R : SIKXA i M N don σ σ') (h : FaultStOkA σ) + (hfk : ∀ p, ¬ M p → ∀ x y, σ.kheap p = some (.tcb x) → σ'.kheap p = some (.tcb y) → y.tcbFault = x.tcbFault) + (hM : ∀ p, M p → ∀ y, σ'.kheap p = some (.tcb y) → y.tcbFault ≠ none → FaultStA y.tcbState) : + FaultStOkA σ' := fun p y hy hf => by + by_cases hp : M p + · exact hM p hp y hy hf + · obtain ⟨x, hx, g⟩ := R.tcbB hy + rw [g.st.resolve_right fun a => hp a.1] + exact h p x hx (fun e => hf ((hfk p hp x y hx hy).trans e)) + +/-- **`SIdleA`** across an `SIKXA` step (`SIKA.sidle`). -/ +theorem SIKXA.sidle {σ σ' : State Unit} {m : McState} {n : Nat} (R : SIKXA σ.idleThread M N don σ σ') + (h : SIdleA σ m n) (hMi : ∀ c < n, ¬ M (m.idleThreadOf c)) (hRdy : RdyA σ) : SIdleA σ' m n := by + have hidle : σ'.idleThread = σ.idleThread := by rw [R.frame] + have vt : ∀ c < n, vtCapA σ' (m.idleThreadOf c) = some .nullCap ∧ vcpuOfA σ' (m.idleThreadOf c) = none := by + intro c hc + obtain ⟨x, hx, -⟩ := h.f3 c hc + obtain ⟨y, hy, g⟩ := R.tcbF hx + have one : ∀ z : Tcb, tcbCnodeMap z (tcbCnodeIndex 1) = some z.tcbVtable := fun z => by + simp [tcbCnodeMap, tcbIdx_eq, tcbStage2Slot, tcbHighVSpaceSlot] + have ev : y.tcbVtable = x.tcbVtable := + Option.some.inj ((one y).symm.trans ((congrFun g.caps (tcbCnodeIndex 1)).trans (one x))) + exact idleVtKeepA hx hy ev g.vcpu (h.idleSlot c hc) (h.idleBound c hc) + refine ⟨fun c hc => ?_, fun y k hy => ?_, fun p y k hy ht => ?_, h.f6.of_eq R.specCap, fun y k hy => ?_, + fun c hc => (vt c hc).1, fun c hc => (vt c hc).2⟩ + rotate_left 3 + · obtain ⟨x, hx, g⟩ := R.scB hy + refine ⟨?_, (R.rdy hRdy).1 y k hy⟩ + have a := (h.f7 x k hx).1 + unfold scActive at a ⊢; rw [g.rm]; exact a + · obtain ⟨x, hx, hs⟩ := h.f3 c hc + obtain ⟨y, hy, g⟩ := R.tcbF hx + exact ⟨y, hy, (g.st.resolve_right fun a => hMi c hc a.1).trans hs⟩ + · obtain ⟨x, hx, g⟩ := R.scB hy + rw [(g.idle rfl).2]; exact h.f4 x k hx + · rw [hidle] at ht + obtain ⟨x, hx, g⟩ := R.scB hy + rcases g.th with a | a + · exact h.f5 p x k hx (a.symm.trans ht) + · exact absurd ht a.2 + +end + +end + +end Fl4ProofsA64.P3.Sched + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-- `IpcKeepA` without the fault (the fault's place holds `True`, so `IpcKeepA`'s patterns read the same). -/ +structure IpcKeepNA (σ σ' : Fl4SpecA64.State Unit) : Prop where + tcb : ∀ p x, σ.kheap p = some (.tcb x) → ∃ y, σ'.kheap p = some (.tcb y) ∧ True ∧ + y.tcbIpcframe = x.tcbIpcframe ∧ y.tcbIpcBuffer = x.tcbIpcBuffer ∧ y.tcbMcpriority = x.tcbMcpriority ∧ + y.tcbYieldTo = x.tcbYieldTo ∧ y.tcbFlags = x.tcbFlags + scF : ∀ p y k, σ.kheap p = some (.schedContext y k) → + ∃ y' k', σ'.kheap p = some (.schedContext y' k') ∧ y'.scYieldFrom = y.scYieldFrom + scB : ∀ p y' k', σ'.kheap p = some (.schedContext y' k') → + ∃ y k, σ.kheap p = some (.schedContext y k) ∧ y'.scYieldFrom = y.scYieldFrom + +theorem SIKXA.ipcKeepN {i : Fl4SpecA64.ObjRef} {M N : Fl4SpecA64.ObjRef → Prop} {don : Bool} + {σ σ' : Fl4SpecA64.State Unit} (R : SIKXA i M N don σ σ') : IpcKeepNA σ σ' := + ⟨fun _ _ h => by + obtain ⟨y, e, g⟩ := R.tcbF h + exact ⟨y, e, trivial, g.frame, g.buf, g.mcp, g.yt, g.flags⟩, + fun _ _ _ h => by + obtain ⟨y, e, g⟩ := R.scF h + exact ⟨y, _, e, g.yf⟩, + fun _ _ _ h => by + obtain ⟨x, e, g⟩ := R.scB h + exact ⟨x, _, e, g.yf⟩⟩ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {A : Type} {AR' : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} + {κ : U64 → U64} {n : Nat} {J5 : JSlotA S} {R : Set U64 → JSlotA S} {s s' : S} {a : A} + {σ σ' : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} {i : Fl4SpecA64.ObjRef} + {M Nq : Fl4SpecA64.ObjRef → Prop} {don : Bool} {tf : U64} + +variable (J : JStage3A st AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7A st R) s a σ m) +include J + +/-- `tcbKept_ipcA` over the write set with the fault words of `tf`. -/ +theorem JStage3A.tcbKept_ipcXA (w : WritesIn st s s' (fun b => IpcFpA st Z E Rp Bf m σ s b ∨ FaultWA tf b ∨ RepWA Rp b)) (htf : tf ∈ Z.T) + {t K : U64} (ht : t ∈ Z.T) + (hK : K.val < 4096) (h8 : K.val % 8 = 0) (hw : ¬ TWOffN K.val) + (h : K.val ≠ 3496 ∧ K.val ≠ 3504 ∧ K.val ≠ 3464 ∧ K.val ≠ 3480 ∧ K.val ≠ 3488 ∧ K.val ≠ 3592) + (hF : K.val ≠ 3376 ∧ K.val ≠ 3384 ∧ K.val ≠ 3392 ∧ K.val ≠ 3400) : + L.mem s' (wa t K) = L.mem s (wa t K) := + w.mem_eq (field_aligned (J.sr.core.tcbs t ht) hK h8) fun c => c.elim + (J.tcbWord_offIpcA ht hK hw h) fun c => c.elim + (not_faultWA (J.sr.core.tcbs tf htf) (J.sr.core.tcbs t ht) hK hF) fun ⟨r, hr, c⟩ => by + have hfit := (J.sr.core.tcbs t ht).fits + have hv : (wa t K).val = t.val + K.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have := J.iplaced.e.rt r hr t ht + omega + +/-- **`of_ipcStepUXA`'s `tw`.** -/ +theorem JStage3A.tw_ipcXA (w : WritesIn st s s' (fun b => IpcFpA st Z E Rp Bf m σ s b ∨ FaultWA tf b ∨ RepWA Rp b)) (htf : tf ∈ Z.T) : + ∀ t ∈ Z.T, ∀ O : U64, O.val < 224 ∨ O.val = 3440 ∨ O.val = 3472 ∨ O.val = 3512 ∨ O.val = 3456 → O.val % 8 = 0 → + L.mem s' (wa t O) = L.mem s (wa t O) := fun t ht O h h8 => + J.tcbKept_ipcXA w htf ht (by omega) h8 (by unfold TWOffN; omega) (by omega) (by omega) + +/-- The state word of a thread whose spec state is kept. -/ +theorem JStage3A.stKept_ipcXA (K : SIKXA i M Nq don σ σ') (E' : EpStateRel Z.T E Rp Bf (L.mem s') σ') {t : U64} + (ht : t ∈ Z.T) (hM : ¬ M (specPtr t)) : + L.mem s' (Fl4ProofsA64.P1.stA t) = L.mem s (Fl4ProofsA64.P1.stA t) := by + obtain ⟨x, hx, r⟩ := J.ep.tcb t ht + obtain ⟨y, hy, r'⟩ := E'.tcb t ht + obtain ⟨y', hy', g⟩ := K.tcbF hx + rw [hy] at hy' + cases hy' + refine UScalar.eq_of_val_eq ?_ + rw [r'.st, r.st, g.st.resolve_right fun c => hM c.1] + +/-- **`RunVcpuInv`** across the send: the movers run no VCPU. -/ +theorem JStage3A.runVcpu_ipcXA (w : WritesIn st s s' (fun b => IpcFpA st Z E Rp Bf m σ s b ∨ FaultWA tf b ∨ RepWA Rp b)) (htf : tf ∈ Z.T) (K : SIKXA i M Nq don σ σ') + (E' : EpStateRel Z.T E Rp Bf (L.mem s') σ') + (hMv : ∀ t ∈ Z.T, M (specPtr t) → L.mem s (Fl4ProofsA64.P1.stA t) ≠ sched.TS_RUNNING_VM) : + RunVcpuInv st Z.T s' := fun t ht h0 => by + have e : L.mem s' (runVcpuA t) = L.mem s (runVcpuA t) := + J.tcbKept_ipcXA w htf (K := 3672#u64) ht (by simp) (by simp) (by unfold TWOffN; simp) (by simp) + (by simp) + rw [e] at h0 + by_cases hM : M (specPtr t) + · exact absurd (J.runVcpu.zero ht (hMv t ht hM)) h0 + · rw [J.stKept_ipcXA K E' ht hM]; exact J.runVcpu t ht h0 + +/-- **`NtfnStateRel` at the new state** (module docstring): `hMs` the movers wait on no notification before, `hMs'` +nor after. -/ +theorem JStage3A.ntfn_ipcXMA (w : WritesIn st s s' (fun b => IpcFpA st Z E Rp Bf m σ s b ∨ FaultWA tf b ∨ RepWA Rp b)) (htf : tf ∈ Z.T) (K : SIKXA i M Nq don σ σ') + (E' : EpStateRel Z.T E Rp Bf (L.mem s') σ') (wk : NtfnWaitKeepMA M Z.T N (L.mem s) (L.mem s')) + (hMs : ∀ t ∈ Z.T, M (specPtr t) → ∀ x, σ.kheap (specPtr t) = some (.tcb x) → + ∀ p, x.tcbState ≠ .blockedOnNotification p) + (hMs' : ∀ t ∈ Z.T, M (specPtr t) → ∀ x, σ'.kheap (specPtr t) = some (.tcb x) → + ∀ p, x.tcbState ≠ .blockedOnNotification p) : + NtfnStateRel N Z.T (L.mem s') σ' := by + have hP := J.iplaced + have nw : ∀ q ∈ N, ∀ O : U64, O.val < 40 → O.val % 8 = 0 → L.mem s' (wa q O) = L.mem s (wa q O) := by + intro q hq O h1 h2 + have hna := hP.n.ntfn q hq + have hv : (wa q O).val = q.val + O.val := Notif.nfield_val hna (by omega) + refine w.mem_eq (Notif.nfield_aligned hna (by omega) h2) ?_ + have hw : IpcObjWA N E Rp (wa q O) := Or.inl ⟨q, hq, by omega, by omega⟩ + rintro ((c | c) | c | ⟨r, hr, c⟩) + · exact nw_not_efoot hP hq (by omega) (by omega) c.foot + · exact J.ipc_offMwrA hw c + rotate_left + · have hqH : H q = some .ntfn := J.utIpc.ntfn q hq + have hrH : H r = some .reply := J.utIpc.reply r hr + have := J.sys.ent.heap.disj q _ r _ hqH hrH (fun e => by rw [e, hrH] at hqH; cases hqH) + simp only [HKind.bits] at this + omega + · obtain ⟨K, hK, e⟩ := faultWA_eq c + have hKv : K.val < 4096 ∧ 3376 ≤ K.val := by rcases hK with rfl | rfl | rfl | rfl <;> simp + have hf := field_val (J.sr.core.tcbs tf htf) hKv.1 + have := hP.n.disj q hq tf htf + rw [← e] at hf + omega + refine ⟨fun q hq => ?_, fun t ht => ?_⟩ + · obtain ⟨x, hx, hr⟩ := J.ntfn.ntfn q hq + refine ⟨x, K.ntfnF hx, hr.frame ?_ fun y _ hm => ?_⟩ + · refine ⟨?_, ?_, ?_, ?_, ?_⟩ + · rw [Notif.epHeadA_eq]; exact nw q hq _ (by simp) (by simp) + · rw [Notif.epTSA_eq]; exact nw q hq _ (by simp) (by simp) + · rw [Notif.nBadgeA_eq]; exact nw q hq _ (by simp) (by simp) + · rw [Notif.nBoundA_eq]; exact nw q hq _ (by simp) (by simp) + · rw [Notif.nScA_eq]; exact nw q hq _ (by simp) (by simp) + · obtain ⟨hyT, hy6, hyb⟩ := hr.obj.blocked hm + have hM : ¬ M (specPtr y) := fun c => by + obtain ⟨z, hz, rz⟩ := J.ntfn.tcb y hyT + have h6 := rz.st + rw [hy6] at h6 + cases hs : z.tcbState with + | blockedOnNotification p => exact hMs y hyT c z hz p hs + | ext e => rw [hs] at h6; cases e <;> simp [tsCode] at h6 + | _ => rw [hs] at h6; simp [tsCode] at h6 + obtain ⟨k1, k2, k3⟩ := wk y hyT hM (by rw [hyb]; exact hq) + exact ⟨J.stKept_ipcXA K E' hyT hM, k1, k2, k3⟩ + · obtain ⟨x, hx, r⟩ := J.ntfn.tcb t ht + obtain ⟨y, hy, r'⟩ := E'.tcb t ht + refine ⟨y, hy, ⟨r'.st, fun p hp => ?_, ?_, r'.prio⟩⟩ + · have hM : ¬ M (specPtr t) := fun c => hMs' t ht c y hy p hp + obtain ⟨y', hy', g⟩ := K.tcbF hx + rw [hy] at hy' + cases hy' + have hs : x.tcbState = .blockedOnNotification p := by + rw [← g.st.resolve_right fun c => hM c.1]; exact hp + have hb := r.blk p hs + have hN : L.mem s (Notif.tBlockA t) ∈ N := (J.blkQ.ntfn t ht x hx p hs).1 + rw [(wk t ht hM hN).1]; exact hb + · rw [badgeRegA_eq_regA] + exact r'.regs _ + +/-- `ntfn_ipcXMA` with every notification waiter kept. -/ +theorem JStage3A.ntfn_ipcXA (w : WritesIn st s s' (fun b => IpcFpA st Z E Rp Bf m σ s b ∨ FaultWA tf b ∨ RepWA Rp b)) (htf : tf ∈ Z.T) (K : SIKXA i M Nq don σ σ') + (E' : EpStateRel Z.T E Rp Bf (L.mem s') σ') (wk : NtfnWaitKeepA Z.T N (L.mem s) (L.mem s')) + (hMs : ∀ t ∈ Z.T, M (specPtr t) → ∀ x, σ.kheap (specPtr t) = some (.tcb x) → + ∀ p, x.tcbState ≠ .blockedOnNotification p) + (hMs' : ∀ t ∈ Z.T, M (specPtr t) → ∀ x, σ'.kheap (specPtr t) = some (.tcb x) → + ∀ p, x.tcbState ≠ .blockedOnNotification p) : + NtfnStateRel N Z.T (L.mem s') σ' := + J.ntfn_ipcXMA w htf K E' (NtfnWaitKeepA.toM wk) hMs hMs' + +/-- **`BlkQueuedA` at the new state**: the movers end blocked on neither an endpoint nor a notification. -/ +theorem JStage3A.blkQ_ipcXA (K : SIKXA i M Nq don σ σ') (E' : EpStateRel Z.T E Rp Bf (L.mem s') σ') + (wk : NtfnWaitKeepA Z.T N (L.mem s) (L.mem s')) + (hMs' : ∀ t ∈ Z.T, M (specPtr t) → ∀ x, σ'.kheap (specPtr t) = some (.tcb x) → + Fl4SpecA64.epBlocked x.tcbState = none ∧ ∀ p, x.tcbState ≠ .blockedOnNotification p) : + BlkQueuedA Z.T E N (L.mem s') σ' := by + have kept : ∀ t ∈ Z.T, ∀ y, σ'.kheap (specPtr t) = some (.tcb y) → ¬ M (specPtr t) → + ∃ x, σ.kheap (specPtr t) = some (.tcb x) ∧ x.tcbState = y.tcbState := fun t _ y hy hM => by + obtain ⟨x, hx, g⟩ := K.tcbB hy + exact ⟨x, hx, (g.st.resolve_right fun c => hM c.1).symm⟩ + refine ⟨fun t ht y hy e hb => ?_, fun t ht y hy p hp => ?_⟩ + · have hM : ¬ M (specPtr t) := fun c => by rw [(hMs' t ht c y hy).1] at hb; cases hb + obtain ⟨x, hx, es⟩ := kept t ht y hy hM + obtain ⟨hE, z, hz, hm⟩ := J.blkQ.ep t ht x hx e (es ▸ hb) + obtain ⟨z', hz', hq⟩ := K.epF hz + obtain ⟨x1, hx1, r⟩ := J.ep.tcb t ht + obtain ⟨y1, hy1, r'⟩ := E'.tcb t ht + rw [hx] at hx1; cases hx1 + rw [hy] at hy1; cases hy1 + have eq : L.mem s' (Notif.tBlockA t) = L.mem s (Notif.tBlockA t) := + specPtr_inj ((r'.blk e hb).trans (r.blk e (es ▸ hb)).symm) + exact ⟨by rw [eq]; exact hE, z', hz', hq _ hm hM⟩ + · have hM : ¬ M (specPtr t) := fun c => (hMs' t ht c y hy).2 p hp + obtain ⟨x, hx, es⟩ := kept t ht y hy hM + obtain ⟨hN, z, q, hz, ho, hm⟩ := J.blkQ.ntfn t ht x hx p (es.trans hp) + exact ⟨by rw [(wk t ht hN).1]; exact hN, z, q, K.ntfnF hz, ho, hm⟩ + +/-- **`ReplyStackRelA`** across a send without donation and reply push. -/ +theorem JStage3A.reply_ipcXA (w : WritesIn st s s' (fun b => IpcFpA st Z E Rp Bf m σ s b ∨ FaultWA tf b)) (htf : tf ∈ Z.T) (K : SIKXA i M Nq false σ σ') : + ReplyStackRelA (L.mem s') Rp Z.C σ' (scNameA m) stack := by + refine J.reply.frame (fun r hr => ?_) (fun c _ x k h => ?_) (fun r _ y h => K.replyB h) + swap + · obtain ⟨y, hy, g⟩ := K.scF h + exact ⟨y, k, hy, g.reps rfl⟩ + have hrH : H r = some .reply := J.utIpc.reply r hr + obtain ⟨r32, -, rfit⟩ := J.iplaced.e.rep r hr + have one : ∀ o : U64, 8 ≤ o.val → o.val < 32 → o.val % 8 = 0 → L.mem s' (wa r o) = L.mem s (wa r o) := by + intro o h1 h2 h3 + have hv : (wa r o).val = r.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + refine w.mem_eq (by unfold Aligned; rw [hv]; omega) ?_ + have hw : IpcObjWA N E Rp (wa r o) := Or.inr (Or.inr ⟨r, hr, by omega, by omega⟩) + have other : ∀ r' ∈ Rp, r' ≠ r → r'.val + 32 ≤ r.val ∨ r.val + 32 ≤ r'.val := fun r' hr' ne => by + have := J.sys.ent.heap.disj r' _ r _ (J.utIpc.reply r' hr') hrH ne + simp only [HKind.bits] at this + omega + rintro ((c | c) | c) + rotate_left + · exact J.ipc_offMwrA hw c + · obtain ⟨K, hK, e⟩ := faultWA_eq c + have hKv : K.val < 4096 ∧ 3376 ≤ K.val := by rcases hK with rfl | rfl | rfl | rfl <;> simp + have hf := field_val (J.sr.core.tcbs tf htf) hKv.1 + have := J.iplaced.e.rt r hr tf htf + rw [← e] at hf + omega + · rcases c with ⟨e, he, c⟩ | ⟨r', hr', c⟩ | ⟨t, ht, c⟩ | c + · have := J.iplaced.e.er e he r hr; omega + · by_cases er : r' = r + · subst er; omega + · have := other r' hr' er; omega + · have := J.iplaced.e.rt r hr t ht + unfold TWF TL at c + rcases c with ⟨c1, c2⟩ | c + · unfold TWOffN at c2; omega + · omega + · have := J.iplaced.e.br _ c r hr; omega + exact ⟨one _ (by simp [layout.reply.NEXT]) (by simp [layout.reply.NEXT]) (by simp [layout.reply.NEXT]), + one _ (by simp [layout.reply.PREV]) (by simp [layout.reply.PREV]) (by simp [layout.reply.PREV])⟩ + +end + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {ar : machine.Arch A} {nA : U64} {s s' : S} {a : A} {σ σ' : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} {Q : U64 → U64 → List U64} {Rl : U64 → List U64} + {curOf curScOf : Fl4SpecA64.CoreId → Fl4SpecA64.ObjRef} + +set_option maxHeartbeats 1000000 in +/-- **`of_ipcStepUA` with `IpcKeepNA`** and the idle threads' faults given (module docstring). -/ +theorem JStage3A.of_ipcStepUXSA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (w : WritesIn st s s' (fun b => SchedStepNWA (st := st) N E Rp Z m σ s b ∨ BufW Bf b)) (HS : SpecSchedA σ σ') (SSz : SSzA σ σ') (Qk : QBlkA (specPtr '' E) (specPtr '' N) σ σ') + (hz : ∀ x : Fl4SpecA64.Word, UtFreeA st OS s (fun d => d = false) (fl4Ptr x).val → + σ'.machineState.underlyingMemory x = σ.machineState.underlyingMemory x) + (hcurOf : ∀ k, k ≠ m.curCore → curOf k = Z.curOf k) + (bxi : BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s') + (sysl : SysL st H Z.Cs OS ia ie s') (symL : SymL st H Z.Cs s') + (userOK : Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s') + (j7 : J7A st (J7R2A st ar nA) Z H OS s' σ' m) + (sr : SchedSRA st { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } m κ s' σ') + (ep : EpStateRel Z.T E Rp Bf (L.mem s') σ') + (ntfn : NtfnStateRel N Z.T (L.mem s') σ') + {stack' : U64 → List U64} (reply : ReplyStackRelA (L.mem s') Rp Z.C σ' (scNameA m) stack') + (hdr : ∀ r ∈ Rp, DeadA st OS .reply s r → ∀ c ∈ Z.C, r ∉ stack' c) + (K : IpcKeepNA σ σ') (idle : IdleNoFaultA m σ') + (tw : ∀ t ∈ Z.T, ∀ O : U64, O.val < 224 ∨ O.val = 3440 ∨ O.val = 3472 ∨ O.val = 3512 ∨ O.val = 3456 → O.val % 8 = 0 → + L.mem s' (wa t O) = L.mem s (wa t O)) + (sinv : SInvXA σ' m n) (sidle : SIdleA σ' m n) + (runVcpu : RunVcpuInv st Z.T s') (blkQ : BlkQueuedA Z.T E N (L.mem s') σ') + (schedValid : SchedValidA σ') : + JStage3A st AR { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } H OS U Ac Pp Tb Lv ia ie N E Rp + Bf stack' κ n (J5A st) (J7A st (J7R2A st ar nA)) s' a σ' m := by + have at_ : ∀ t ∈ Z.T, ∃ x, σ.kheap (specPtr t) = some (.tcb x) := fun t ht => by + obtain ⟨x, hx, -⟩ := J.ntfn.tcb t ht + exact ⟨x, hx⟩ + have bufCover : BufCover st Z.T Bf s' σ' := by + intro u hu y b sb v hy hb hl + obtain ⟨x, hx⟩ := at_ u hu + obtain ⟨y0, hy0, -, hfr, hbuf, -, -⟩ := K.tcb _ _ hx + rw [hy] at hy0 + cases hy0 + have hb0 : ipcBufOfA b x = some sb := by + unfold ipcBufOfA at hb ⊢ + rwa [hfr, hbuf] at hb + have hib : IbSame (L := L) s s' u := by + refine ⟨?_, ?_, ?_⟩ + · rw [ibWordA_eq]; exact tw u hu _ (by simp) (by simp) + · rw [sbWordA_eq]; exact tw u hu _ (by simp) (by simp) + · rw [sbWordA8_eq]; exact tw u hu _ (by simp) (by simp) + rw [lookup_ipc_buffer_sameA hib] at hl + exact J.bufCover u hu x b sb v hx hb0 hl + have bufAl : UtBufAlA Z.T σ' := fun t ht y hy => by + obtain ⟨x, hx⟩ := at_ t ht + obtain ⟨y0, hy0, -, -, hbuf, -, -⟩ := K.tcb _ _ hx + rw [hy] at hy0 + cases hy0 + rw [hbuf]; exact J.bufAl t ht x hx + have mcp : McpRelA Z.T m (L.mem s') σ' := + J.mcp.congrA (fun t ht => ⟨tw t ht _ (by simp) (by simp), tw t ht _ (by simp) (by simp)⟩) fun t ht x hx => by + obtain ⟨y, hy, -, -, -, hm, -, hf⟩ := K.tcb _ _ hx + exact ⟨y, hy, hm, hf⟩ + have ye : ∀ t ∈ Z.T, L.mem s' (yieldA t) = L.mem s (yieldA t) := fun t ht => by + unfold yieldA; exact tw t ht _ (by simp) (by simp) + have yr : YieldRelA Z.T Z.C m (L.mem s') σ' := by + refine ⟨fun t ht => ?_, fun t ht h => ?_, fun c hc y k h p hp => ?_⟩ + · obtain ⟨x, hx, e⟩ := J.yieldRel.tcb t ht + obtain ⟨y, hy, -, -, -, -, hyt, -⟩ := K.tcb _ _ hx + exact ⟨y, hy, by rw [ye t ht, hyt]; exact e⟩ + · rw [ye t ht] at h ⊢ + obtain ⟨hC, y, k, hy, e⟩ := J.yieldRel.toSc t ht h + obtain ⟨y', k', hy', e'⟩ := K.scF _ _ _ hy + exact ⟨hC, y', k', hy', e'.trans e⟩ + · obtain ⟨y0, k0, h0, e⟩ := K.scB _ _ _ h + obtain ⟨t, ht, e1, e2⟩ := J.yieldRel.frm c hc y0 k0 h0 p (e ▸ hp) + exact ⟨t, ht, e1, by rw [ye t ht]; exact e2⟩ + exact J.of_stepFinUSA w HS SSz Qk hz hcurOf bxi sysl symL userOK j7 sr ep ntfn reply hdr bufCover bufAl idle mcp yr sinv + sidle runVcpu blkQ schedValid + +set_option maxHeartbeats 1000000 in +/-- `of_ipcStepUXSA` for a step shrinking the endpoint and notification queues (`QSendA`). -/ +theorem JStage3A.of_ipcStepUXA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (w : WritesIn st s s' (fun b => SchedStepNWA (st := st) N E Rp Z m σ s b ∨ BufW Bf b)) (HS : SpecSchedA σ σ') (SSz : SSzA σ σ') (Qk : QSendA σ σ') + (hz : ∀ x : Fl4SpecA64.Word, UtFreeA st OS s (fun d => d = false) (fl4Ptr x).val → + σ'.machineState.underlyingMemory x = σ.machineState.underlyingMemory x) + (hcurOf : ∀ k, k ≠ m.curCore → curOf k = Z.curOf k) + (bxi : BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s') + (sysl : SysL st H Z.Cs OS ia ie s') (symL : SymL st H Z.Cs s') + (userOK : Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s') + (j7 : J7A st (J7R2A st ar nA) Z H OS s' σ' m) + (sr : SchedSRA st { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } m κ s' σ') + (ep : EpStateRel Z.T E Rp Bf (L.mem s') σ') + (ntfn : NtfnStateRel N Z.T (L.mem s') σ') + {stack' : U64 → List U64} (reply : ReplyStackRelA (L.mem s') Rp Z.C σ' (scNameA m) stack') + (hdr : ∀ r ∈ Rp, DeadA st OS .reply s r → ∀ c ∈ Z.C, r ∉ stack' c) + (K : IpcKeepNA σ σ') (idle : IdleNoFaultA m σ') + (tw : ∀ t ∈ Z.T, ∀ O : U64, O.val < 224 ∨ O.val = 3440 ∨ O.val = 3472 ∨ O.val = 3512 ∨ O.val = 3456 → O.val % 8 = 0 → + L.mem s' (wa t O) = L.mem s (wa t O)) + (sinv : SInvXA σ' m n) (sidle : SIdleA σ' m n) + (runVcpu : RunVcpuInv st Z.T s') (blkQ : BlkQueuedA Z.T E N (L.mem s') σ') + (schedValid : SchedValidA σ') : + JStage3A st AR { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } H OS U Ac Pp Tb Lv ia ie N E Rp + Bf stack' κ n (J5A st) (J7A st (J7R2A st ar nA)) s' a σ' m := + JStage3A.of_ipcStepUXSA (J := J) (w := w) (HS := HS) (SSz := SSz) (hz := hz) (hcurOf := hcurOf) (bxi := bxi) (sysl := sysl) (symL := symL) (userOK := userOK) (j7 := j7) (sr := sr) (ep := ep) (ntfn := ntfn) (stack' := stack') (reply := reply) (hdr := hdr) (K := K) (idle := idle) (tw := tw) (sinv := sinv) (sidle := sidle) (runVcpu := runVcpu) (blkQ := blkQ) (schedValid := schedValid) + (Qk := QSendA.toBlk J.ipcSorted Qk) + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3KEntrySendWireA.lean b/proofs/Fl4ProofsA64/P3/Stage3KEntrySendWireA.lean new file mode 100644 index 000000000..ebddb04f7 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3KEntrySendWireA.lean @@ -0,0 +1,266 @@ +/- +P3 (aarch64, A64-P3 Stage 3, kernel-entry composition, step 2): **the entries' exhausted-budget branch with the +timeout send to a waiting receiver** (`hSend` of `kentryChargeIrqA` / `kentryChargeRestartA`, waiting-receiver case). + +* `TimeoutRecvA`: the case selector (the current thread's timeout handler slot holds an endpoint cap whose endpoint + has a waiting receiver); `BufOffXA`: the buffer words off the J7 VSpace words. +* **`JStage3A.checkBudgetSendRecvA`** (`check_budget`) and **`JStage3A.checkBudgetRestartSendRecvA`** + (`check_budget_restart`: the current thread is blocked after the send, so no Restart). +* **`kentryChargeIrqSendA`**, **`kentryChargeRestartSendA`**: `kentryChargeIrqA` / `kentryChargeRestartA` with + `hSend` discharged under `TimeoutRecvA`; the other send cases (no receiver: record edit #8c) stay the named premise + `hSendO`. + +Named premises: `kentryChargeIrqA`'s [J] `T`, `hni`; [P] `hA`, `hCs`, `one`, `hag`, `hmb`, `hLimit`; lift 2b's [P] +`hmax`, `htu`, `hconv`; [J] `hX` (`BufOffXA` at every record state: the J7 VSpace words are off the IPC buffers); [J] `hSendO` (the timeout send without a waiting +receiver). New module. +-/ +import Fl4ProofsA64.P3.Stage3KEntrySendA +import Fl4ProofsA64.P3.Stage3KEntryRestartA +import Fl4ProofsA64.P3.Stage3EntryValidA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 Fl4Spec + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +variable (st) in +/-- **The waiting-receiver case of the timeout send** at a state (module docstring). -/ +def TimeoutRecvA (Z : XSetsA) (E : Set U64) (s : S) (σ : State Unit) : Prop := + ∃ (xt : Tcb) (slot ep hbg : U64) (sd rv g gr : Bool) (d : U64) (q : List ObjRef), + σ.kheap σ.curThread = some (.tcb xt) ∧ obj.Tcb.slot (L.curW s) layout.tcb.SLOT_TIMEOUT_HANDLER = ok slot ∧ + IsSlotA Z.th slot ∧ capA st s slot = .Endpoint ep hbg sd rv g gr ∧ + tcbCnodeMap xt (tcbCnodeIndex 4) = some (.endpointCap (specPtr ep) hbg.bv (rightsOfA sd rv g gr)) ∧ + ep ∈ E ∧ σ.kheap (specPtr ep) = some (.endpoint (.recvEP (specPtr d :: q))) + +variable (st) in +/-- [J] The IPC buffer words are off the J7 VSpace words. -/ +def BufOffXA (Z : XSetsA) (H : Heap) (OS Pp Tb : Set U64) (Lv : U64 → Nat) (Bf : Set (U64 × Word)) (s : S) : + Prop := + (∀ Pt Ub, FrUb st Ub OS s → VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt → ∀ b, VsR (L.ksb s) Pt b → ¬ BufW Bf b) + +end + +section rec +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {nA : U64} {s : S} {a : A} {σ : State Unit} {m : McState} + +set_option maxHeartbeats 2000000 in +/-- **`check_budget` on an exhausted head refill, from the record across `charge_budget … true`** (`CH`, with the +current thread blocked after it). -/ +theorem JStage3A.checkBudgetOfChargeA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar nA)) s a σ m) + (I : CurInvA σ) (hMin : Fl4MinBudgetAgreeA ar a) + (hex : fl4GetScRefillSufficient (Z := Unit) σ.curSc σ.consumedTime σ = ({(false, σ)}, False)) + (hns : ¬ NoTimeoutA σ.consumedTime σ) + (CH : ∀ used : U64, ¬ NoTimeoutA used.bv σ → sched.charge_budget st ar s a used true ⦃ r => ∃ τ, + fl4ChargeBudget (Z := Unit) used.bv true σ = ({((), τ)}, False) ∧ r.2 = a ∧ + JStage3QA st ar AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes nA m r.1 r.2 τ ∧ + τ.curThread = σ.curThread ∧ ∀ x, τ.kheap τ.curThread = some (.tcb x) → runnable x.tcbState = false ⦄) : + sched.check_budget st ar s a ⦃ r => ∃ τ, fl4CheckBudget (Z := Unit) σ = ({(r.1, τ)}, False) ∧ + JStage3QA st ar AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes nA m + r.2.1 r.2.2 τ ∧ r.1 = false ∧ τ.curThread = σ.curThread ∧ + ∀ x, τ.kheap τ.curThread = some (.tcb x) → runnable x.tcbState = false ⦄ := by + have H0 := J.yieldQueueA + let used := L.mem s (L.nodeA s layout.node.CONSUMED) + have hUsed : σ.consumedTime = used.bv := H0.sched.rest.consumed + unfold sched.check_budget + rw [eq_of_spec_fd (sched.cur_sc.spec s), bind_ok, eq_of_spec_fd (sched.consumed.specA s), bind_ok] + have dec := entryBudgetDecisionA H0.sched.toSchedSRN I J.sinv.mc a hMin + rw [eq_of_spec_fd (sched.cur_sc.spec s), bind_ok, eq_of_spec_fd (sched.consumed.specA s), bind_ok] at dec + refine WP.spec_bind dec ?_ + rintro b ⟨e, -⟩ + have hb : b = false := by + have q := e.symm.trans hex + exact (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst q))).1 + subst hb + have spec : fl4CheckBudget (Z := Unit) σ = + (fl4ChargeBudget used.bv true >>= fun _ => (pure false : SMonad Unit Bool)) σ := by + unfold fl4CheckBudget + rw [nd_gets_bindSc, nd_gets_bindSc, nd_bind_oneSc e] + simp only [Bool.false_eq_true, ↓reduceIte] + rw [nd_gets_bindSc, hUsed] + simp only [Bool.false_eq_true, ↓reduceIte] + refine WP.spec_bind (CH used (by rw [← hUsed]; exact hns)) ?_ + rintro ⟨s1, a1⟩ ⟨τ, h, -, JQ, hc, hnr⟩ + change ok (false, s1, a1) ⦃ _ ⦄ + rw [WP.spec_ok] + refine ⟨τ, ?_, JQ, rfl, hc, hnr⟩ + rw [spec, nd_bind_oneSc h] + rfl + +set_option maxHeartbeats 2000000 in +/-- **`check_budget` at the record on an exhausted head refill taking the timeout send to a waiting receiver.** -/ +theorem JStage3A.checkBudgetSendRecvA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar nA)) s a σ m) + (noSwitch : ∀ u, σ.schedulerAction ≠ .switchThread u) (I : CurInvA σ) (Idle : ActIdleA σ) + (hcore : L.mem s (scCoreA (L.cscW s)) = L.cpu s) (hni : σ.curSc ≠ idleScPtr → NotIdleScA m σ.curSc) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (one : fl4A64Platform.numNodes = 1) + (hag : fl4MinBudget = MIN_BUDGET) (hMin : Fl4MinBudgetAgreeA ar a) + (hLimit : sporadic.max_release_time ar a ⦃ v => v.bv = fl4MaxReleaseTime ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) (hconv : Fl4TicksToUsLegacyAgree) + (hA : P0.ArchTotal ar) (En : ChargeEntryA st Ac s) (hX : BufOffXA st Z H OS Pp Tb Lv Bf s) + (hex : fl4GetScRefillSufficient (Z := Unit) σ.curSc σ.consumedTime σ = ({(false, σ)}, False)) + (hns : ¬ NoTimeoutA σ.consumedTime σ) (hR : TimeoutRecvA st Z E s σ) : + sched.check_budget st ar s a ⦃ r => ∃ τ, fl4CheckBudget (Z := Unit) σ = ({(r.1, τ)}, False) ∧ + JStage3QA st ar AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes nA m + r.2.1 r.2.2 τ ∧ r.1 = false ∧ τ.curThread = σ.curThread ∧ + ∀ x, τ.kheap τ.curThread = some (.tcb x) → runnable x.tcbState = false ⦄ := by + obtain ⟨xt, slot, ep, hbg, sd, rv, g, gr, d, q, hkt, hslot, hsl, hcap, hgc, he, hk⟩ := hR + exact J.checkBudgetOfChargeA I hMin hex hns fun used hu => + J.chargeSendRecvA noSwitch I Idle hcore hni hCs one hag hMin hLimit hmax htu hconv hA En hX used hu hkt hslot + hsl hcap hgc he hk + +set_option maxHeartbeats 2000000 in +/-- **`check_budget_restart` from `check_budget`'s outcome with the current thread blocked** (`CB`): no Restart. -/ +theorem JStage3A.checkBudgetRestartOfCheckA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar nA)) s a σ m) + (CB : sched.check_budget st ar s a ⦃ r => ∃ τ, fl4CheckBudget (Z := Unit) σ = ({(r.1, τ)}, False) ∧ + JStage3QA st ar AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes nA m + r.2.1 r.2.2 τ ∧ r.1 = false ∧ τ.curThread = σ.curThread ∧ + ∀ x, τ.kheap τ.curThread = some (.tcb x) → runnable x.tcbState = false ⦄) : + sched.check_budget_restart st ar s a ⦃ r => ∃ τ, fl4CheckBudgetRestart (Z := Unit) σ = ({(r.1, τ)}, False) ∧ + JStage3QA st ar AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes nA m + r.2.1 r.2.2 τ ⦄ := by + unfold sched.check_budget_restart + refine WP.spec_bind CB ?_ + rintro ⟨r, s1, a1⟩ ⟨τ, h, ⟨Q, Rl, co, cs, J1⟩, hr, -, hnr⟩ + dsimp only at h J1 hr + subst hr + simp only [uncurry_apply_pair] + refine WP.spec_bind (sched.cur_thread.spec s1) ?_ + intro t ht + change t = L.curW s1 at ht + subst ht + simp only [Bool.false_eq_true, ↓reduceIte] + have cT1 : τ.curThread = specPtr (L.curW s1) := J1.j.1.sch.curThread + obtain ⟨x, hkτ⟩ := J1.sinv.curTcb + have c1 := (J1.ep.at_tcb J1.sr.curT (x := x) (by rw [← cT1]; exact hkτ)).st + have hrx : ¬ runnable x.tcbState = true := by rw [hnr x hkτ]; simp + have spec : ∀ {τ' : State Unit}, (NonDet.when (¬(false = true) ∧ runnable x.tcbState = true) + (setThreadState (Z := Unit) τ.curThread .restart)) τ = ({((), τ')}, False) → + fl4CheckBudgetRestart (Z := Unit) σ = ({(false, τ')}, False) := by + intro τ' hw + unfold fl4CheckBudgetRestart + rw [nd_bind_oneSc h, nd_gets_bindSc, nd_bind_oneSc (show getThreadState (Z := Unit) τ.curThread τ = + ({(x.tcbState, τ)}, False) from threadGet_eqSc _ hkτ), nd_bind_oneSc hw] + rfl + rw [eq_of_spec_fd (sched.is_runnable.spec (L := L) s1 (L.curW s1))] + simp only [bind_ok] + have hrun : ¬ Fl4ProofsA64.P1.Runnable (L.mem s1 (stA (L.curW s1))) := fun e => hrx ((runnable_iffA c1).1 e) + rw [decide_eq_false (show ¬ (_ ∨ _ ∨ _) from hrun)] + simp only [Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + refine ⟨τ, spec ?_, Q, Rl, co, cs, J1⟩ + unfold NonDet.when + rw [if_neg (fun e => hrx e.2)] + rfl + +set_option maxHeartbeats 2000000 in +/-- **`check_budget_restart` at the record taking the timeout send to a waiting receiver**: the current thread is +blocked after the send, so no Restart. -/ +theorem JStage3A.checkBudgetRestartSendRecvA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar nA)) s a σ m) + (noSwitch : ∀ u, σ.schedulerAction ≠ .switchThread u) (I : CurInvA σ) (Idle : ActIdleA σ) + (hcore : L.mem s (scCoreA (L.cscW s)) = L.cpu s) (hni : σ.curSc ≠ idleScPtr → NotIdleScA m σ.curSc) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (one : fl4A64Platform.numNodes = 1) + (hag : fl4MinBudget = MIN_BUDGET) (hMin : Fl4MinBudgetAgreeA ar a) + (hLimit : sporadic.max_release_time ar a ⦃ v => v.bv = fl4MaxReleaseTime ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) (hconv : Fl4TicksToUsLegacyAgree) + (hA : P0.ArchTotal ar) (En : ChargeEntryA st Ac s) (hX : BufOffXA st Z H OS Pp Tb Lv Bf s) + (hex : fl4GetScRefillSufficient (Z := Unit) σ.curSc σ.consumedTime σ = ({(false, σ)}, False)) + (hns : ¬ NoTimeoutA σ.consumedTime σ) (hR : TimeoutRecvA st Z E s σ) : + sched.check_budget_restart st ar s a ⦃ r => ∃ τ, fl4CheckBudgetRestart (Z := Unit) σ = ({(r.1, τ)}, False) ∧ + JStage3QA st ar AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes nA m + r.2.1 r.2.2 τ ⦄ := + J.checkBudgetRestartOfCheckA + (J.checkBudgetSendRecvA noSwitch I Idle hcore hni hCs one hag hMin hLimit hmax htu hconv hA En hX hex hns hR) + +set_option maxHeartbeats 1000000 in +/-- **The interrupt entry's exhausted-budget branch with the waiting-receiver timeout send** (module docstring). -/ +theorem kentryChargeIrqSendA {AR' : A → HwRootA → Prop} {Clock : A → MachineState → Prop} + (noSwitch : ∀ u, σ.schedulerAction ≠ .switchThread u) (hni : σ.curSc ≠ idleScPtr → NotIdleScA m σ.curSc) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (one : fl4A64Platform.numNodes = 1) + (hag : fl4MinBudget = MIN_BUDGET) (hmb : ∀ a', Fl4MinBudgetAgreeA ar a') + (hLimit : ∀ a', sporadic.max_release_time ar a' ⦃ v => v.bv = fl4MaxReleaseTime ⦄) + (hA : P0.ArchTotal ar) (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) (hconv : Fl4TicksToUsLegacyAgree) + (hX : ∀ s1 a1 σ1, JStage3A st AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes + (J5A st) (J7A st (J7R2A st ar nA)) s1 a1 σ1 m → BufOffXA st Z H OS Pp Tb Lv Bf s1) + (hSendO : ∀ s1 a1 σ1, fl4UpdateTimestamp (Z := Unit) σ = ({((), σ1)}, False) → + JStage3A st AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar nA)) s1 a1 σ1 m → + ChargeEntryA st Ac s1 → CurInvA σ1 → ActIdleA σ1 → Clock a1 σ1.machineState → + fl4GetScRefillSufficient (Z := Unit) σ1.curSc σ1.consumedTime σ1 = ({(false, σ1)}, False) → + ¬ NoTimeoutA σ1.consumedTime σ1 → ¬ TimeoutRecvA st Z E s1 σ1 → + sched.check_budget st ar s1 a1 ⦃ r => ∃ τ, fl4CheckBudget (Z := Unit) σ1 = ({(r.1, τ)}, False) ∧ + JStage3QA st ar AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes nA m + r.2.1 r.2.2 τ ⦄) : + KEntryChargeA st ar AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ nA m Clock s a σ + (sched.check_budget st ar) fl4CheckBudget := by + refine kentryChargeIrqA noSwitch hni hCs one hag hmb hLimit (fun s1 a1 σ1 J1 => J1.entryValidA) hA fun s1 a1 σ1 e1 J1 En1 CI1 Act1 Cl1 hex hns => ?_ + by_cases hR : TimeoutRecvA st Z E s1 σ1 + · have eσ1 : σ1 = fl4TimestampState (rawNextA σ.machineState) { σ with machineState := rawStateA σ.machineState } := + (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst ((fl4UpdateTimestamp_eqA σ).symm.trans + e1)))).2.symm + have sw1 : ∀ u, σ1.schedulerAction ≠ .switchThread u := by rw [eσ1]; exact noSwitch + have ni1 : σ1.curSc ≠ idleScPtr → NotIdleScA m σ1.curSc := by rw [eσ1]; exact hni + refine WP.spec_mono (J1.checkBudgetSendRecvA sw1 CI1 Act1 (J1.curScOnCoreA hCs one) ni1 hCs one hag (hmb a1) + (hLimit a1) hmax htu hconv hA En1 (hX s1 a1 σ1 J1) hex hns hR) ?_ + rintro r ⟨τ, h, Q, -⟩ + exact ⟨τ, h, Q⟩ + · exact hSendO s1 a1 σ1 e1 J1 En1 CI1 Act1 Cl1 hex hns hR + +set_option maxHeartbeats 1000000 in +/-- **The syscall and fault entries' exhausted-budget branch with the waiting-receiver timeout send** (module +docstring). -/ +theorem kentryChargeRestartSendA {AR' : A → HwRootA → Prop} {Clock : A → MachineState → Prop} + (noSwitch : ∀ u, σ.schedulerAction ≠ .switchThread u) (hni : σ.curSc ≠ idleScPtr → NotIdleScA m σ.curSc) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (one : fl4A64Platform.numNodes = 1) + (hag : fl4MinBudget = MIN_BUDGET) (hmb : ∀ a', Fl4MinBudgetAgreeA ar a') + (hLimit : ∀ a', sporadic.max_release_time ar a' ⦃ v => v.bv = fl4MaxReleaseTime ⦄) + (hA : P0.ArchTotal ar) (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) (hconv : Fl4TicksToUsLegacyAgree) + (hX : ∀ s1 a1 σ1, JStage3A st AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes + (J5A st) (J7A st (J7R2A st ar nA)) s1 a1 σ1 m → BufOffXA st Z H OS Pp Tb Lv Bf s1) + (hSendO : ∀ s1 a1 σ1, fl4UpdateTimestamp (Z := Unit) σ = ({((), σ1)}, False) → + JStage3A st AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar nA)) s1 a1 σ1 m → + ChargeEntryA st Ac s1 → CurInvA σ1 → ActIdleA σ1 → Clock a1 σ1.machineState → + fl4GetScRefillSufficient (Z := Unit) σ1.curSc σ1.consumedTime σ1 = ({(false, σ1)}, False) → + ¬ NoTimeoutA σ1.consumedTime σ1 → ¬ TimeoutRecvA st Z E s1 σ1 → + sched.check_budget_restart st ar s1 a1 ⦃ r => ∃ τ, fl4CheckBudgetRestart (Z := Unit) σ1 = ({(r.1, τ)}, False) ∧ + JStage3QA st ar AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes nA m + r.2.1 r.2.2 τ ⦄) : + KEntryChargeA st ar AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ nA m Clock s a σ + (sched.check_budget_restart st ar) fl4CheckBudgetRestart := by + refine kentryChargeRestartA noSwitch hni hCs one hag hmb hLimit (fun s1 a1 σ1 J1 => J1.entryValidA) hA + fun s1 a1 σ1 e1 J1 En1 CI1 Act1 Cl1 hex hns => ?_ + by_cases hR : TimeoutRecvA st Z E s1 σ1 + · have eσ1 : σ1 = fl4TimestampState (rawNextA σ.machineState) { σ with machineState := rawStateA σ.machineState } := + (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst ((fl4UpdateTimestamp_eqA σ).symm.trans + e1)))).2.symm + have sw1 : ∀ u, σ1.schedulerAction ≠ .switchThread u := by rw [eσ1]; exact noSwitch + have ni1 : σ1.curSc ≠ idleScPtr → NotIdleScA m σ1.curSc := by rw [eσ1]; exact hni + exact J1.checkBudgetRestartSendRecvA sw1 CI1 Act1 (J1.curScOnCoreA hCs one) ni1 hCs one hag (hmb a1) + (hLimit a1) hmax htu hconv hA En1 (hX s1 a1 σ1 J1) hex hns hR + · exact hSendO s1 a1 σ1 e1 J1 En1 CI1 Act1 Cl1 hex hns hR + +end rec + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3KEntryTailA.lean b/proofs/Fl4ProofsA64/P3/Stage3KEntryTailA.lean new file mode 100644 index 000000000..813e9b171 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3KEntryTailA.lean @@ -0,0 +1,399 @@ +/- +P3 (aarch64, A64-P3 Stage 3, kernel-entry composition, Plan A, item 3): **the tail inputs, and `schedule` from the +record with `hSched` derived**. + +* `JStage3A.tailA`: `ScheduleTailInputsA` from the record at the post-fire state: `context` and the `OffOKA` / + `OffReadyA` part of `charge` are derived. +* `JStage3A.schedFiredA`: `ScheduleFiredInputsA` at the fires loop's predicate `FiredFFA`. +* `JStage3A.scheduleRecFFA`: `JStage3A.scheduleRecFA` with the inputs asked only at the fired state. +* `JStage3A.scheduleExitFA`: `JStage3A.scheduleExitA` with `hSched` replaced by `SchedGapA`. + +Named premises (`SchedGapA`, each asked only at states reached by the executed steps): +* (integration exec 8) the idle facts (`idleSlot`, `idleBound`, `idleActive`) come from the record's `SIdleA` at the + post-fire state (`JStage3A.idleGapA`); the charged SC's owner (was `own`) is out of the release queue by `ZeroConsA` + and the SC binding (`JStage3A.scBoundA`), and it is no idle SC by `JStage3A.curScNotIdleA` ([S] one node); +* [P] `ticks`, `timer`: the platform's time constants and timer-queue view; +* `oneDomain`, `minAgree`: the build's domain count and minimum budget; +* [S] `numNodes = 1`. + +New module. +-/ +import Fl4ProofsA64.P3.Stage3KEntryRootA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3 + Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.OuterEntry Classical + +noncomputable section + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +/-- The incoming unblock check changes no object but the target's. -/ +theorem incomingStep_onlyA (target : ObjRef) {σ σ' : State Unit} (ne : target ≠ σ.curSc) + (h : scheduleIncomingStepA σ target σ = ({((), σ')}, False)) : OnlyScA target σ σ' := by + have P : PresR (OnlyScA target) (do + NonDet.modify fun r : State Unit => {r with reprogramTimer := true} + ifConstantBandwidthRefillUnblockCheck (some target)) := by + refine PresR.bind (PresR.modify _ fun _ => ⟨⟨rfl, rfl, rfl, rfl, rfl, rfl, fun h => h⟩, fun _ _ => rfl⟩) fun _ => ?_ + have c : PresR (OnlyScA target) (refillUnblockCheck (Z := Unit) target) := refillUnblockCheck_onlyA + unfold ifConstantBandwidthRefillUnblockCheck ifCondRefillUnblockCheck NonDet.maybeM + opres + unfold scheduleIncomingStepA NonDet.when at h + rw [if_pos ne] at h + exact P σ ((), σ') (by rw [h]; exact Set.mem_singleton _) + +/-- The incoming unblock check keeps the release queue. -/ +theorem incomingStep_rlsA (target : ObjRef) {σ σ' : State Unit} + (h : scheduleIncomingStepA σ target σ = ({((), σ')}, False)) : σ'.releaseQueue = σ.releaseQueue := by + have P : PresR ActKeepA (do + NonDet.modify fun r : State Unit => {r with reprogramTimer := true} + ifConstantBandwidthRefillUnblockCheck (some target)) := by + unfold ifConstantBandwidthRefillUnblockCheck ifCondRefillUnblockCheck NonDet.maybeM + apres + unfold scheduleIncomingStepA NonDet.when at h + split at h + · exact (P σ ((), σ') (by rw [h]; exact Set.mem_singleton _)).2.2.1 + · rw [((Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst h))).2)] + +/-- **The current SC is no core's idle SC** unless it is the idle SC's own name ([S] one node). -/ +theorem JStage3A.curScNotIdleA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {J5 J7 : JSlotA S} {s : S} {a : A} {σ : State Unit} + {m : McState} (h1 : fl4A64Platform.numNodes = 1) + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes J5 J7 s a σ m) + (h : σ.curSc ≠ idleScPtr) : NotIdleScA m σ.curSc := by + refine ⟨h, fun c hc e => ?_⟩ + have c0 : c = m.curCore := by + have lt : m.curCore < fl4A64Platform.numNodes := J.sinv.mc.curCore_lt + exact (Nat.lt_one_iff.1 (lt_of_lt_of_eq hc h1)).trans (Nat.lt_one_iff.1 (lt_of_lt_of_eq lt h1)).symm + have w := J.sr.sch.curScOfW _ J.sr.sch.cpuCs + rw [← J.sr.sch.core, J.sr.sch.csc] at w + rw [c0] at e + unfold scRenA at w + split at w + · exact h w + · rename_i ne + exact ne (w.symm.trans e) + +/-- **The context and charge parts of `ScheduleTailInputsA`** from the record at the post-fire state. They need no +timer premise (`P3/A64Wit2bGapA` uses them to reach the state the timer premise is about). -/ +theorem JStage3A.tailCoreA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {nA : U64} {s1 s3 : S} {a1 : A} + {σ1 σ2 σ3 : State Unit} {m : McState} {limit : Nat} + (J1 : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar nA)) s1 a1 σ1 m) + (R1 : FireRecA σ1) (B1 : BodyExitA σ1) (h1 : fl4A64Platform.numNodes = 1) (oneDomain : numDomains = 1) + (waking : awaken (Z := Unit) σ1 = ({((), σ2)}, False)) + (selection : fl4SelectAction limit σ2 = ({((), σ3)}, False)) + {Z3 : XSetsA} (H3 : YieldQueueRelA (st := st) Z3 m κ s3 σ3) : + ScheduleContextInputsA (st := st) Z3 m s3 σ3 ∧ + ∀ s0 σ0 s4 σ4, YieldQueueRelA (st := st) Z3 m κ s0 σ0 → + σ0 = { σ3 with schedulerAction := .resumeCurThread } → MFrA (st := st) Z3 m s3 s0 σ3 σ0 → + YieldQueueRelA (st := st) Z3 m κ s4 σ4 → MFrA (st := st) Z3 m s0 s4 σ0 σ4 → + scheduleIncomingStepA σ0 (scNameA m (L.mem s0 (scA (L.curW s0)))) σ0 = ({((), σ4)}, False) → + σ4.reprogramTimer = true → OutgoingChargeA m σ4 := by + have h21 : ((), σ2) ∈ (awaken (Z := Unit) σ1).1 := by rw [waking]; exact Set.mem_singleton _ + have h31 : ((), σ2) ∈ (checkDomainTime (Z := Unit) σ2).1 := by + rw [checkDomainTime_single_specA σ2 oneDomain]; exact Set.mem_singleton _ + let σr : State Unit := { σ3 with schedulerAction := .resumeCurThread } + have h41 : ((), σr) ∈ (fl4SelectThread (Z := Unit) limit σ2).1 := by + unfold fl4SelectThread + rw [nd_bind_oneSc selection] + exact Set.mem_singleton _ + obtain ⟨hF, hI, hS, hRR⟩ := R1 + have fia : FIA σr := + selectThread_fia limit σ2 ⟨awaken_fia σ1 hF _ h21, hSw_of hF hS hRR h21 h31⟩ _ h41 + have q : SfKeepA σ1 σr := PreO.trans (awaken_qkA σ1 _ h21) (fl4SelectThread_qkA limit σ2 _ h41) + have kh : σ3.kheap = σ1.kheap := q.kh + have csc : σ3.curSc = σ1.curSc := q.csc + have used : σ3.consumedTime = σ1.consumedTime := q.used + have time : σ3.curTime = σ1.curTime := q.time + have idle : σ3.idleThread = σ1.idleThread := q.idle + have cur : σ3.curThread = specPtr (L.curW s3) := H3.sched.sch.curThread + obtain ⟨t, ht, link⟩ := H3.sched.sch.tcb (L.curW s3) H3.sched.curT + rw [← cur] at ht + have ht1 : σ1.kheap σ3.curThread = some (.tcb t) := by rw [← kh]; exact ht + have bound : ∃ p y k, t.tcbSchedContext = some p ∧ σ1.kheap p = some (.schedContext y k) ∧ + scActive y = true ∧ σ3.curThread ∉ σ3.releaseQueue := by + by_cases e : σ3.curThread = σ3.idleThread + · obtain ⟨x, y, k, hx, es, hy, -⟩ := J1.sinv.idleSc + rw [← idle, ← e, ht1] at hx + obtain rfl := KernelObject.tcb.inj (Option.some.inj hx) + refine ⟨_, y, k, es, hy, (J1.idleGapA.2.2 y k hy).1, fun hm => ?_⟩ + obtain ⟨u, hu, hr⟩ := H3.sched.run.2 _ hm + obtain ⟨z, hz, hs⟩ := J1.sinv.idleTcb + rw [e, idle, kh] at hu + rw [hu] at hz + obtain rfl := KernelObject.tcb.inj (Option.some.inj hz) + rw [hs] at hr + cases hr + · obtain ⟨⟨x, p, hx, es, y, k, hy, ha⟩, nr⟩ := fia.1.act rfl e + change σ3.kheap σ3.curThread = _ at hx + change σ3.kheap p = _ at hy + rw [ht] at hx + obtain rfl := KernelObject.tcb.inj (Option.some.inj hx) + exact ⟨p, y, k, es, by rw [← kh]; exact hy, ha, nr⟩ + obtain ⟨p, y, k, es, hy, ha, nr⟩ := bound + have nz : L.mem s3 (scA (L.curW s3)) ≠ 0#u64 := by + intro z + rw [es, optScA, if_pos z] at link + cases link + have pName : p = scNameA m (L.mem s3 (scA (L.curW s3))) := by + rw [es, optScA, if_neg nz] at link + exact Option.some.inj link + have context : ScheduleContextInputsA (st := st) Z3 m s3 σ3 := by + refine ⟨⟨t, ht, H3.sched.rest.scMem _ H3.sched.curT nz, by rw [es, pName]⟩, fun x j hx _ => ?_⟩ + rw [← pName, kh, hy] at hx + have inj := Option.some.inj hx + injection inj with e1 _ + refine ⟨e1 ▸ ha, fun u hu z hz bad => ?_⟩ + rw [← pName] at bad + rw [kh] at hz + obtain ⟨y1, k1, hy1, o1⟩ := J1.sr.inv.back u z p hz bad + obtain ⟨y2, k2, hy2, o2⟩ := J1.sr.inv.back _ t p ht1 es + rw [hy1] at hy2 + have inj2 := Option.some.inj hy2 + injection inj2 with e2 _ + rw [e2, o2] at o1 + exact nr (Option.some.inj o1 ▸ hu) + refine ⟨context, ?_⟩ + intro s0 σ0 s4 σ4 H0 e0 _ _ _ step _ x j hx hax ni hu + have word : scNameA m (L.mem s0 (scA (L.curW s0))) = p := by + have c0 : σ0.curThread = specPtr (L.curW s0) := H0.sched.sch.curThread + obtain ⟨t0, ht0, link0⟩ := H0.sched.sch.tcb (L.curW s0) H0.sched.curT + rw [← c0, e0] at ht0 + change σ3.kheap σ3.curThread = _ at ht0 + rw [ht] at ht0 + obtain rfl := KernelObject.tcb.inj (Option.some.inj ht0) + rw [es] at link0 + unfold optScA at link0 + split at link0 + · cases link0 + · exact (Option.some.inj link0).symm + rw [word] at step + subst e0 + have same : σ4.curSc = σ1.curSc ∧ σ4.consumedTime = σ1.consumedTime ∧ σ4.curTime = σ1.curTime ∧ + σ4.kheap σ4.curSc = σ1.kheap σ1.curSc := by + by_cases c : p = σ3.curSc + · have : σ4 = { σ3 with schedulerAction := .resumeCurThread } := by + unfold scheduleIncomingStepA NonDet.when at step + split at step + · rename_i ne + exact (ne c).elim + · exact ((Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst step))).2).symm + subst this + exact ⟨csc, used, time, by change σ3.kheap σ3.curSc = _; rw [kh, csc]⟩ + · have o := incomingStep_onlyA (σ := { σ3 with schedulerAction := .resumeCurThread }) p c step + have e1 : σ4.curSc = σ3.curSc := o.csc + refine ⟨e1.trans csc, o.used.trans used, o.time.trans time, ?_⟩ + rw [e1, o.other _ (Ne.symm c)] + change σ3.kheap σ3.curSc = _ + rw [kh, csc] + obtain ⟨c1, u1, t1, k1⟩ := same + have hx1 : σ1.kheap σ1.curSc = some (.schedContext x j) := by rw [← k1]; exact hx + -- the charged SC: no idle SC ([S] one node), and its owner is out of the release queue (`ZeroConsA`) + have nI : NotIdleScA m σ4.curSc := by + rw [c1]; exact JStage3A.curScNotIdleA h1 J1 (by rw [← c1]; exact ni) + have rls4 : ∀ u ∈ σ4.releaseQueue, u ∈ σ1.releaseQueue := fun u hm => + q.rls u (by rw [incomingStep_rlsA p step] at hm; exact hm) + have hcs : σ1.curSc = scRenA m (L.cscW s1) := by + have := J1.sr.sch + rw [← this.csc, this.core]; exact this.curScOfW _ this.cpuCs + have owner : ∃ owner, (x.scTcb = none ∨ x.scTcb = some owner) ∧ owner ∉ σ4.releaseQueue := by + cases ho : x.scTcb with + | none => + refine ⟨σ1.idleThread, Or.inl rfl, fun hm => ?_⟩ + obtain ⟨z, hz, hr⟩ := J1.sinv.rlsRun _ (rls4 _ hm) + obtain ⟨z', hz', hs⟩ := J1.sinv.idleTcb + rw [hz'] at hz + obtain rfl := KernelObject.tcb.inj (Option.some.inj hz) + rw [hs] at hr; cases hr + | some o => + refine ⟨o, Or.inr rfl, fun hm => ?_⟩ + rw [hcs] at hx1 + obtain ⟨z, hz, hzs⟩ := J1.scBoundA J1.sr.cscC hx1 ho + exact hu (by rw [u1]; exact J1.sinv.zc o z hz (hzs.trans (congrArg some hcs.symm)) (rls4 o hm)) + have budget := B1.budget x j hx1 (Or.inr hax) (Or.inl (by rw [← u1]; exact hu)) + have ready : OffReadyA σ4 := fun y' k' hy' => by + rw [k1] at hy' + rw [u1, t1] + exact budget.1 y' k' hy' + have off : OffOKA σ4 := fun pos y' k' hy' act' => by + rw [k1] at hy' + rw [u1] at pos ⊢ + rw [t1] + exact budget.2 pos y' k' hy' (fun ne => act' (by rw [c1]; exact ne)) + exact ⟨off, ready, nI, owner⟩ + +/-- **`ScheduleTailInputsA` from the record at the post-fire state** (module docstring). `σ3` is the selection's +outcome. -/ +theorem JStage3A.tailA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {nA : U64} {s1 s3 : S} {a1 a3 : A} + {σ1 σ2 σ3 : State Unit} {m : McState} {tm : A → Nat → DeadlineViewA} {view : State Unit → Fl4TimerView} + {limit : Nat} + (J1 : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar nA)) s1 a1 σ1 m) + (R1 : FireRecA σ1) (B1 : BodyExitA σ1) (h1 : fl4A64Platform.numNodes = 1) (oneDomain : numDomains = 1) + (waking : awaken (Z := Unit) σ1 = ({((), σ2)}, False)) + (selection : fl4SelectAction limit σ2 = ({((), σ3)}, False)) + {Z3 : XSetsA} (H3 : YieldQueueRelA (st := st) Z3 m κ s3 σ3) + (ticks : ScCfgTimeAgreeA ar a3) + (timer : ∀ C σ0 s0 s4 σ4, sched.set_action st s3 sched.ACTION_RESUME_CURRENT = ok s0 → + sched.switch_sched_context st ar s0 a3 = ok (s4, a3) → + fl4SwitchSchedContext (Z := Unit) σ0 = ({((), σ4)}, False) → + σ0 = { σ3 with schedulerAction := .resumeCurThread } → MFrA (st := st) Z3 m s3 s4 σ3 σ4 → + YieldQueueRelA (st := st) { Z3 with curScOf := C } m κ s4 σ4 → σ4.reprogramTimer = true → + ScheduleTimerInputsA (st := st) s4 σ4 view ∧ SniHypA ar tm a3 (L.cpu s4).val) + (silent : SilentScheduleIpiA (st := st) s3) : + ScheduleTailInputsA (st := st) Z3 m κ s3 a3 σ3 ar tm view := by + have core := JStage3A.tailCoreA J1 R1 B1 h1 oneDomain waking selection H3 + exact ⟨core.1, core.2, ticks, timer, silent⟩ + +/-- What the fires loop's outcome carries to the scheduler's phases. -/ +def FiredFFA (st : machine.Store S) (ar : machine.Arch A) [L : LawfulStore st] (AR : A → HwRootA → Prop) + (Z : XSetsA) (H : Heap) (OS U Ac Pp Tb : Set U64) (Lv : U64 → Nat) (ia ie : Nat) (N E Rp : Set U64) + (Bf : Set (U64 × Fl4SpecA64.Word)) (stack : U64 → List U64) (κ : U64 → U64) (n : Nat) (J5 J7 : JSlotA S) + (m : McState) (s : S) (a : A) (X : State Unit → Prop) (s1 : S) (σ1 : State Unit) : Prop := + ∃ (a1 : A) (Q : U64 → U64 → List U64) (R : U64 → List U64), + timer.fire st ar s a = ok (s1, a1) ∧ + JStage3A st AR { Z with Q := Q, R := R } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s1 a1 σ1 m ∧ + FireRecA σ1 ∧ X σ1 + +/-- **The premises left on the scheduler's inputs** (module docstring). -/ +structure SchedGapA (Z : XSetsA) (m : McState) (κ : U64 → U64) (s : S) (a : A) (σ : State Unit) + (ar : machine.Arch A) (tm : A → Nat → DeadlineViewA) (view : State Unit → Fl4TimerView) : Prop where + oneDomain : numDomains = 1 + minAgree : fl4MinBudget = MIN_BUDGET + ticks : ∀ a', ScCfgTimeAgreeA ar a' + timer : ∀ (start selected : S × A) (Z3 : XSetsA) σ3, sched.awaken st ar s a = ok start → + scheduleSelectActionA (st := st) (ar := ar) start.1 start.2 = ok selected → + ∀ C σ0 s0 s4 σ4, sched.set_action st selected.1 sched.ACTION_RESUME_CURRENT = ok s0 → + sched.switch_sched_context st ar s0 selected.2 = ok (s4, selected.2) → + fl4SwitchSchedContext (Z := Unit) σ0 = ({((), σ4)}, False) → + σ0 = { σ3 with schedulerAction := .resumeCurThread } → MFrA (st := st) Z3 m selected.1 s4 σ3 σ4 → + YieldQueueRelA (st := st) { Z3 with curScOf := C } m κ s4 σ4 → σ4.reprogramTimer = true → + ScheduleTimerInputsA (st := st) s4 σ4 view ∧ SniHypA ar tm selected.2 (L.cpu s4).val + +/-- **`ScheduleFiredInputsA` from the record at the fired state** (module docstring). -/ +theorem JStage3A.schedFiredA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {nA : U64} + {s : S} {a : A} {σ : State Unit} {m : McState} {tm : A → Nat → DeadlineViewA} + {view : State Unit → Fl4TimerView} + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (G : SchedGapA (st := st) Z m κ s a σ ar tm view) : + ScheduleFiredInputsA (st := st) Z m κ s a σ ar tm view AR + (fun _ s1 σ0 σ1 => σ0 = σ → fl4TimerFires σ σ1 → + FiredFFA st ar AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar nA)) m s a BodyExitA s1 σ1) := by + refine ⟨G.oneDomain, G.minAgree, ?_, ?_⟩ + · intro r s1 σ1 σ2 Q R FF wake fires waking H2 F + obtain ⟨a1, Qf, Rf, ef, J1, R1, -⟩ := FF rfl fires + have SF : SchedFrameA σ1 σ2 := + (inferInstance : SPresInst (awaken (Z := Unit))).pres σ1 ((), σ2) (by rw [waking]; exact Set.mem_singleton _) + exact JStage3A.selectionA (Z := Z) (Q := Qf) (R := Rf) J1 hCs h1 R1 waking H2 ⟨F.wr, F.keep, F.sz⟩ SF + (awaken_archA ef wake) (JStage3A.vsRootOK J1) (JStage3A.idleVtOf J1 J1.idleGapA.1 J1.idleGapA.2.1) + · intro start selected s1 σ1 σ2 σ3 Q R Q' FF wake sel fires waking Fa selection Fs H3 + obtain ⟨a1, Qf, Rf, -, J1, R1, B1⟩ := FF rfl fires + have Fa' : MFrA (st := st) { Z with Q := Qf, R := Rf } m s1 start.1 σ1 σ2 := ⟨Fa.wr, Fa.keep, Fa.sz⟩ + exact JStage3A.tailA J1 R1 B1 h1 G.oneDomain waking selection H3 (G.ticks _) (G.timer start selected _ σ3 wake sel) + (J1.sr.toSchedSRN.ipiKeepMD (Fa'.transD Fs rfl rfl) J1.ipiZero) + +set_option maxHeartbeats 1000000 in +/-- `JStage3A.scheduleRecFA` with the selection and tail inputs asked only at the fired state. -/ +theorem JStage3A.scheduleRecFFA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {s : S} {a : A} {σ : State Unit} {m : McState} + {tm : A → Nat → DeadlineViewA} {view : State Unit → Fl4TimerView} + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) (arch : ArchSwFA st ar) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (fireR : timer.fire st ar s a ⦃ r => ∃ σ1 Q Rl, fl4TimerFires σ σ1 ∧ + JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes + (J5A st) (J7A st (J7R2A st ar hPl.ipa)) r.1 r.2 σ1 m ∧ FireRecA σ1 ∧ BodyExitA σ1 ⦄) + (G : SchedGapA (st := st) Z m κ s a σ ar tm view) (arK : ArRootKeepA ar AR) : + sched.schedule st ar s a ⦃ r => ∃ (f : S × A) (σ1 τ : State Unit) (Qf : U64 → U64 → List U64) + (Rf : U64 → List U64) (Q : U64 → U64 → List U64) (Rl : U64 → List U64) + (Tcur Ccur : Fl4SpecA64.CoreId → ObjRef) (hw : HwRootA), + timer.fire st ar s a = ok f ∧ + JStage3A st AR { Z with Q := Qf, R := Rf } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ + fl4A64Platform.numNodes (J5A st) (J7A st (J7R2A st ar hPl.ipa)) f.1 f.2 σ1 m ∧ + FireRecA σ1 ∧ BodyExitA σ1 ∧ fl4TimerFires σ σ1 ∧ ((), τ) ∈ (SchedTail kLimitA view σ1).1 ∧ + ((), τ) ∈ (fl4Schedule kLimitA view σ).1 ∧ + YieldQueueRelA (st := st) { Z with Q := Q, R := Rl, curOf := Tcur, curScOf := Ccur } m κ r.1 τ ∧ + JStage3A st AR { Z with Q := Q, R := Rl, curOf := Tcur, curScOf := Ccur, hw := hw } H OS U Ac Pp Tb Lv ia ie + N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) (J7A st (J7R2A st ar hPl.ipa)) r.1 r.2 τ m ⦄ := by + have hcore : m.curCore < fl4A64Platform.numNodes := by + rw [J.sr.sch.core]; exact hCs _ J.sr.sch.cpuCs + have F0 : timer.fire st ar s a ⦃ r => ∃ σ1 Q R, + fl4TimerFires σ σ1 ∧ YieldQueueRelA (st := st) {Z with Q := Q, R := R} m κ r.1 σ1 ∧ IdleThreadsA m σ1 ∧ + (σ = σ → fl4TimerFires σ σ1 → FiredFFA st ar AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ + fl4A64Platform.numNodes (J5A st) (J7A st (J7R2A st ar hPl.ipa)) m s a BodyExitA r.1 σ1) ⦄ := by + refine WP.spec_mono (reachedResultA fireR) ?_ + rintro r ⟨er, σ1, Q, Rl, hf, J1, R1, X1⟩ + refine ⟨σ1, Q, Rl, hf, J1.yieldQueueA, fun c hc x hx => ?_, fun _ _ => ⟨r.2, Q, Rl, er, J1, R1, X1⟩⟩ + obtain ⟨y, hy, hs⟩ := J1.sidle.f3 c hc + rw [hy] at hx + have := KernelObject.tcb.inj (Option.some.inj hx) + subst this + rw [hs]; rfl + have inputs := JStage3A.schedFiredA (AR := AR) (H := H) (OS := OS) (U := U) (Ac := Ac) (Pp := Pp) (Tb := Tb) + (Lv := Lv) (ia := ia) (ie := ie) (N := N) (E := E) (Rp := Rp) (Bf := Bf) (stack := stack) + (nA := hPl.ipa) hCs h1 G + have T := schedule_exitFA arch view inputs F0 + refine WP.spec_mono (reachedResultA T) ?_ + rintro r ⟨run, s1, σ1, τ, Q, Rl, Tcur, Ccur, FF, hf, tail, H3, F, SF, hcur, nd⟩ + obtain ⟨a1, Qf, Rf, ef, J1, R1, B1⟩ := FF rfl hf + obtain ⟨hw, har⟩ := arK.schedule st ef ⟨_, J1.j.1.ar⟩ run + have J2 := JStage3A.acrossSchedA hA hPl hcore J J1 R1 J1.sinv.ctQ run tail H3 + ⟨F.wr, F.keep, F.sz⟩ SF (nd _ _ J1.sr.toSchedSRN J1.domOK) hcur har + exact ⟨(s1, a1), σ1, τ, Qf, Rf, Q, Rl, Tcur, Ccur, hw, ef, J1, R1, B1, hf, tail, + fl4Schedule_of_tail kLimitA view hf tail, H3, J2⟩ + +set_option maxHeartbeats 1000000 in +/-- `JStage3A.scheduleExitA` with `hSched` derived: the record at the `fl4Schedule` outcome and the exit facts, from +the record, `SwA`, `BodyExitA` and `SchedGapA`. -/ +theorem JStage3A.scheduleExitFA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {s : S} {a : A} {σ : State Unit} {m : McState} + {tm : A → Nat → DeadlineViewA} {view : State Unit → Fl4TimerView} + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) (arch : ArchSwFA st ar) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hSw : SwA σ) (B : BodyExitA σ) + (hSig : ∀ (Q : U64 → U64 → List U64) (Rl : U64 → List U64) (s : S) (a : A) (σ : State Unit) (t badge : U64), + JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes + (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m → + SwA σ → t ∈ Z.T → L.mem s (boundA t) ≠ 0#u64 → + notification.send_signal st ar s a (L.mem s (boundA t)) badge ⦃ r => + ∃ σ' b Q' Rl', sendSignal (Z := Unit) (specPtr (L.mem s (boundA t))) b σ = ({((), σ')}, False) ∧ + JStage3A st AR { Z with Q := Q', R := Rl' } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ + fl4A64Platform.numNodes (J5A st) (J7A st (J7R2A st ar hPl.ipa)) r.1 r.2 σ' m ⦄) + (G : SchedGapA (st := st) Z m κ s a σ ar tm view) + (arK : ArRootKeepA ar AR) : + sched.schedule st ar s a ⦃ r => ∃ (τ : State Unit) (Q : U64 → U64 → List U64) (Rl : U64 → List U64) + (Tcur Ccur : Fl4SpecA64.CoreId → ObjRef) (hw : HwRootA), + ((), τ) ∈ (fl4Schedule kLimitA view σ).1 ∧ + JStage3A st AR { Z with Q := Q, R := Rl, curOf := Tcur, curScOf := Ccur, hw := hw } H OS U Ac Pp Tb Lv ia ie + N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) (J7A st (J7R2A st ar hPl.ipa)) r.1 r.2 τ m ∧ + CurInvA τ ∧ OffOKA τ ∧ OffReadyA τ ∧ ActIdleA τ ∧ (τ.curSc ≠ idleScPtr → NotIdleScA m τ.curSc) ∧ + ValidMachineTimeA τ ∧ τ.schedulerAction = .resumeCurThread ∧ + (∀ x, τ.kheap τ.curThread = some (.tcb x) → ActivateStateOKA x.tcbState) ⦄ := by + have hcore : m.curCore < fl4A64Platform.numNodes := by + rw [J.sr.sch.core]; exact hCs _ J.sr.sch.cpuCs + refine WP.spec_mono (JStage3A.scheduleRecFFA hA hPl arch hCs h1 J + (JStage3A.fire_budgetA hA hPl J hcore hSw B hSig) G arK) ?_ + rintro r ⟨f, σ1, τ, Qf, Rf, Q, Rl, Tcur, Ccur, hw, -, J1, R1, B1, -, tail, sch, -, J2⟩ + exact ⟨τ, Q, Rl, Tcur, Ccur, hw, sch, J2, exitFactsA h1 J1 J2 R1 B1 tail⟩ + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3KEntryTimerIrqA.lean b/proofs/Fl4ProofsA64/P3/Stage3KEntryTimerIrqA.lean new file mode 100644 index 000000000..90344f08e --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3KEntryTimerIrqA.lean @@ -0,0 +1,235 @@ +/- +P3 (aarch64, A64-P3 Stage 3, kernel-entry composition, step 2): **the record across the timer IRQ arm's writes**, +`REPROGRAM := 1` and `ARMED := 0` (`interrupt.handle_interrupt` at a timer line; spec `reprogramTimer := true`). +Generated from `P3/Stage3FireReprA` by `handoff/tools/gen_kentry2_timerirq.py`: edit the generator, not this file. +No new premise. +-/ +import Fl4ProofsA64.P3.Stage3FireReprA +import Fl4ProofsA64.P3.ScSchedWordsA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 Fl4ProofsA64.P3.Endp +open Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Notif + +local notation "wa" => core.num.U64.wrapping_add + +noncomputable section + +/-- The current core's `REPROGRAM` and `ARMED` words. -/ +def TWWA {S : Type} (st : machine.Store S) [L : LawfulStore st] (s : S) (b : U64) : Prop := + b = L.nodeAt s (L.cpu s) layout.node.REPROGRAM ∨ b = L.nodeAt s (L.cpu s) layout.node.ARMED + +/-- The store after the timer IRQ arm's two writes. -/ +def twStA {S : Type} (st : machine.Store S) [L : LawfulStore st] (s : S) : S := + L.wr (L.wr s (L.nodeAt s (L.cpu s) layout.node.REPROGRAM) 1#u64) + (L.nodeAt (L.wr s (L.nodeAt s (L.cpu s) layout.node.REPROGRAM) 1#u64) + (L.cpu (L.wr s (L.nodeAt s (L.cpu s) layout.node.REPROGRAM) 1#u64)) layout.node.ARMED) 0#u64 + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {ar : machine.Arch A} {nA : U64} {s s' : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +section +variable (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) +include J + +theorem JStage3A.tmw_valA {b : U64} (hb : TWWA st s b) : + (L.ksb s).val + 223488 ≤ b.val ∧ b.val < (L.ksb s).val + 772352 := by + obtain ⟨nn, -, hn, hcs⟩ := J.sr.csN + have hcl : (L.cpu s).val < 128 := by have := (hcs _).1 J.sr.sch.cpuCs; omega + have hr : layout.node.REPROGRAM.val = 56 := by simp [layout.node.REPROGRAM] + have ha : layout.node.ARMED.val = 80 := by simp [layout.node.ARMED] + have hv := nodeAt_valSc (s := s) J.sr.ksFit hcl (o := layout.node.REPROGRAM) (by omega) + have hw := nodeAt_valSc (s := s) J.sr.ksFit hcl (o := layout.node.ARMED) (by omega) + rcases hb with hb | hb <;> rw [hb] <;> omega + +theorem JStage3A.tmw_tcbA {b : U64} (hb : TWWA st s b) {u : U64} (hu : u ∈ Z.T) (h1 : u.val ≤ b.val) + (h2 : b.val < u.val + 4096) : False := by + have hks := J.sr.tcb_ks hu + have hksz : ksSizeA = 788736 := rfl + have := J.tmw_valA hb + omega + +theorem JStage3A.tmw_tcbKA {u : U64} (hu : u ∈ Z.T) {K : U64} (hK : K.val < 4096) (hb : TWWA st s (wa u K)) : + False := by + have hfit := (J.sr.core.tcbs u hu).fits + have hv : (wa u K).val = u.val + K.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact J.tmw_tcbA hb hu (by omega) (by omega) + +theorem JStage3A.tmw_ipcA {b : U64} (hb : TWWA st s b) (h : IpcObjWA N E Rp b) : False := by + have other : ∀ {e : U64} {len : Nat}, Z.th e = some (.other len) → e.val ≤ b.val → b.val < e.val + len → False := by + intro e len he h1 h2 + have P := J.sr.place + have := P.ks e (.other len) he (fun hn => by have := P.irqK he hn; cases this) + have hksz : ksSizeA = 788736 := rfl + have hsz : ObjKindA.size (.other len) = len := rfl + have := J.tmw_valA hb + omega + rcases h with ⟨q, hq, h1, h2⟩ | ⟨q, hq, h1, h2⟩ | ⟨q, hq, h1, h2⟩ + · exact other (J.ipcPlace.notification q hq) h1 h2 + · exact other (J.ipcPlace.endpoint q hq) h1 h2 + · exact other (J.ipcPlace.reply q hq) h1 h2 + +theorem JStage3A.tmw_bufA {b : U64} (hb : TWWA st s b) (h : BufW Bf b) : False := by + obtain ⟨e, len, he, e1, e2⟩ := J.ipcPlace.buffer b h + have P := J.sr.place + have := P.ks e (.other len) he (fun hn => by have := P.irqK he hn; cases this) + have hksz : ksSizeA = 788736 := rfl + have hsz : ObjKindA.size (.other len) = len := rfl + have := J.tmw_valA hb + omega + +end + +set_option maxHeartbeats 1000000 in +/-- **The record across the timer IRQ arm's writes** (`REPROGRAM := 1`, `ARMED := 0`). -/ +theorem JStage3A.timerIrq_recA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (blk' : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac (twStA st s)) + (sysl : SysL st H Z.Cs OS ia ie (twStA st s)) + (symL : SymL st H Z.Cs (twStA st s)) : + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) + (twStA st s) a { σ with reprogramTimer := true } m := by + have ne : layout.node.ARMED ≠ layout.node.REPROGRAM := by + unfold layout.node.ARMED layout.node.REPROGRAM; decide + have Y1 : YieldQueueRelA (st := st) Z m κ (L.wr s (L.nodeA s layout.node.REPROGRAM) 1#u64) + { σ with reprogramTimer := true } := by + have h := (writeSchedulerTimerA J.yieldQueueA (o := layout.node.REPROGRAM) (Or.inl rfl) 1#u64).1 + have e1 : decide ((1#u64 : U64) ≠ 0#u64) = true := + decide_eq_true fun e => by + have := congrArg UScalar.val e + simp at this + rw [if_pos rfl, e1] at h + exact h + have H2 : SchedSRA st Z m κ (twStA st s) { σ with reprogramTimer := true } := by + have h := (writeSchedulerTimerA Y1 (o := layout.node.ARMED) (Or.inr rfl) 0#u64).1 + rw [if_neg ne] at h + exact h.sched + have hA := J.sr.toSchedSRN.reprA.1 + set s1 := L.wr s (L.nodeAt s (L.cpu s) layout.node.REPROGRAM) 1#u64 with hs1 + have ec : L.cpu s1 = L.cpu s := by rw [hs1]; simp only [LawfulStore.cpu_wr] + have eA : L.nodeAt s1 (L.cpu s1) layout.node.ARMED = L.nodeAt s (L.cpu s) layout.node.ARMED := by + have k : L.ksb s1 = L.ksb s := (writesIn_wr (st := st) (v := 1#u64) (s := s) hA).1 + rw [ec]; unfold LawfulStore.nodeAt; (try unfold LawfulStore.nodeBase); rw [k] + have hB : Aligned (L.nodeAt s (L.cpu s) layout.node.ARMED) := by + obtain ⟨nn, -, hn, hcs⟩ := J.sr.csN + have hcl : (L.cpu s).val < 128 := by have := (hcs _).1 J.sr.sch.cpuCs; omega + have ha : layout.node.ARMED.val = 80 := by simp [layout.node.ARMED] + have hv := nodeAt_valSc (s := s) J.sr.ksFit hcl (o := layout.node.ARMED) (by omega) + have hkb := J.sr.toSchedSRN.ksb_al + unfold Aligned at hkb ⊢; omega + have e2 : twStA st s = L.wr s1 (L.nodeAt s (L.cpu s) layout.node.ARMED) 0#u64 := by + unfold twStA; rw [← hs1, eA] + have w0 : WritesIn st s (twStA st s) (TWWA st s) := by + rw [e2] + exact ((writesIn_wr (st := st) (v := 1#u64) (s := s) hA).trans + (writesIn_wr (st := st) (v := 0#u64) (s := s1) hB)).mono fun _ h => h + generalize hs' : twStA st s = s' at blk' sysl symL H2 w0 ⊢ + have hP := J.iplaced + have kp : ∀ b, Aligned b → (TWWA st s b → False) → L.mem s' b = L.mem s b := + fun b hb hn => w0.mem_eq hb hn + have tK : ∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → (K.val < 3528 ∨ 3592 ≤ K.val) → + L.mem s' (wa u K) = L.mem s (wa u K) := fun u hu K h1 h8 h2 => + kp _ (field_aligned (J.sr.core.tcbs u hu) h1 h8) (J.tmw_tcbKA hu h1) + have w : WritesIn st s s' (SchedStepNWA (st := st) N E Rp Z m σ s) := w0.mono fun b hb => by + rcases hb with hb | hb <;> rw [hb] + · exact Or.inl (Or.inl (Or.inr (Or.inl (J.sr.node_sks J.sr.sch.cpuCs (by simp [layout.node.REPROGRAM]))))) + · exact Or.inl (Or.inl (Or.inr (Or.inl (J.sr.node_sks J.sr.sch.cpuCs (by simp [layout.node.ARMED]))))) + have kb : L.ksb s' = L.ksb s := w.1 + have HS : SpecSchedA σ { σ with reprogramTimer := true } := + ⟨fun _ => Or.inl rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ + have SSz : SSzA σ { σ with reprogramTimer := true } := SSzA.refl σ + have rv : RvSt st Z.T s s' := fun u hu => by + constructor + · rw [stA_eq]; exact tK u hu 3328#u64 (by simp) (by simp) (by simp) + · rw [show runVcpuA u = core.num.U64.wrapping_add u 3672#u64 from rfl] + exact tK u hu 3672#u64 (by simp) (by simp) (by simp) + have rv' : RunVcpuInv st Z.T s' := J.runVcpu.rvSt rv + have sq : SchedQueuesInv st H.tcbs Z.Cs H.omega H s' Z.Q Z.R := by + obtain ⟨Q0, R0, h⟩ := blk'.1 + exact SchedQueuesInv.reQA h (by rw [J.tcbs]; exact H2.core) + have poolOff : PoolOff st H Z.Cs OS s' := fun i => by + have h0 := J.poolOff i + rw [vspace.asid_table.eq] at h0 ⊢ + have v := AtOff.v i + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.sys.ent.ksw + have ev : (wa (L.ksb s) (AtOff i)).val = (L.ksb s).val + (AtOff i).val := by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have e : L.mem s' (wa (L.ksb s') (AtOff i)) = L.mem s (wa (L.ksb s) (AtOff i)) := by + rw [kb] + refine w.mem_eq ?_ (J.asid_offStepNA (by omega) (by omega)) + have h32 := J.j.1.ksbAl + show (wa (L.ksb s) (AtOff i)).val % 8 = 0 + omega + rw [e] + refine (WP.spec_ok _).2 fun hne asid => ?_ + have q := (WP.spec_ok _).1 h0 hne asid + exact ⟨q.al, q.om, q.sg, q.os, RegionOffNodes.ksb kb q.ks⟩ + have ep : EpStateRel Z.T E Rp Bf (L.mem s') { σ with reprogramTimer := true } := by + refine J.ep.frame hP.e (fun b hb hf => kp b hb fun htw => ?_) + ⟨fun p x hk => hk, fun p x hk => ⟨x, hk, rfl⟩, fun p x hk => ⟨x, hk, rfl, rfl, rfl, rfl, rfl⟩, rfl⟩ + rcases hf with ⟨e, he, h⟩ | ⟨r, hr, h⟩ | ⟨u, hu, ⟨h1, h2⟩ | h⟩ | h + · exact J.tmw_ipcA htw (Or.inr (Or.inl ⟨e, he, by omega, by omega⟩)) + · exact J.tmw_ipcA htw (Or.inr (Or.inr ⟨r, hr, by omega, by omega⟩)) + · unfold TOffN at h2 + exact J.tmw_tcbA htw hu h1 (by omega) + · unfold TL at h + exact J.tmw_tcbA htw hu (by omega) (by omega) + · exact J.tmw_bufA htw h + have ntfn : NtfnStateRel N Z.T (L.mem s') { σ with reprogramTimer := true } := by + refine J.ntfn.frame hP.n (fun b hb hf => kp b hb fun htw => ?_) + ⟨fun p x hk => hk, fun p x hk => ⟨x, hk, rfl, rfl, rfl⟩⟩ + rcases hf with ⟨q, hq, h1, h2⟩ | ⟨u, hu, e⟩ + · exact J.tmw_ipcA htw (Or.inl ⟨q, hq, h1, by omega⟩) + · rcases e with rfl | rfl | rfl | rfl | rfl | rfl + · rw [stA_eq] at htw; exact J.tmw_tcbKA hu (by simp) htw + · rw [tBlockA_eq] at htw; exact J.tmw_tcbKA hu (by simp) htw + · rw [badgeRegA_eq] at htw; exact J.tmw_tcbKA hu (by simp) htw + · rw [prioA_eq'] at htw; exact J.tmw_tcbKA hu (by simp) htw + · rw [nextEp_eq] at htw; exact J.tmw_tcbKA hu (by simp) htw + · rw [prevEp_eq] at htw; exact J.tmw_tcbKA hu (by simp) htw + have reply : ReplyStackRelA (L.mem s') Rp Z.C { σ with reprogramTimer := true } (scNameA m) stack := by + refine J.reply.frame (fun r hr => ?_) (fun c _ x n hk => ⟨x, n, hk, rfl⟩) (fun r _ x hk => ⟨x, hk, rfl⟩) + obtain ⟨r32, -, rfit⟩ := hP.e.rep r hr + have one : ∀ o : U64, o.val < 32 → o.val % 8 = 0 → L.mem s' (wa r o) = L.mem s (wa r o) := by + intro o o1 o2 + have v : (wa r o).val = r.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + refine kp _ ?_ fun htw => J.tmw_ipcA htw (Or.inr (Or.inr ⟨r, hr, by omega, by omega⟩)) + show (wa r o).val % 8 = 0 + omega + have n16 : (layout.reply.NEXT).val = 16 := by unfold layout.reply.NEXT; rfl + have p8 : (layout.reply.PREV).val = 8 := by unfold layout.reply.PREV; rfl + exact ⟨one _ (by omega) (by omega), one _ (by omega) (by omega)⟩ + have buf : BufCover st Z.T Bf s' { σ with reprogramTimer := true } := + J.bufCover_wA w0 (fun u hu K hK htw => J.tmw_tcbKA hu (by omega) htw) + (fun q z h => ⟨z, h, rfl, rfl⟩) + have mcp : McpRelA Z.T m (L.mem s') { σ with reprogramTimer := true } := + J.mcp.congrA (fun u hu => ⟨tK u hu 3440#u64 (by simp) (by simp) (by simp), + tK u hu 3456#u64 (by simp) (by simp) (by simp)⟩) (fun u hu x hx => ⟨x, hx, rfl, rfl⟩) + have yr : YieldRelA Z.T Z.C m (L.mem s') { σ with reprogramTimer := true } := by + have k : ∀ u ∈ Z.T, L.mem s' (yieldA u) = L.mem s (yieldA u) := + fun u hu => tK u hu 3472#u64 (by simp) (by simp) (by simp) + refine ⟨fun u hu => ?_, fun u hu hnz => ?_, fun c hc y n hy p hp => ?_⟩ + · obtain ⟨x, hx, he⟩ := J.yieldRel.tcb u hu + exact ⟨x, hx, by rw [k u hu]; exact he⟩ + · rw [k u hu] at hnz ⊢ + exact J.yieldRel.toSc u hu hnz + · obtain ⟨u, hu, e1, e2⟩ := J.yieldRel.frm c hc y n hy p hp + exact ⟨u, hu, e1, by rw [k u hu]; exact e2⟩ + have bq : BlkQueuedA Z.T E N (L.mem s') { σ with reprogramTimer := true } := + J.blkQ.keepA (fun u hu => by rw [Notif.tBlockA_eq]; exact tK u hu 3336#u64 (by simp) (by simp) (by simp)) + (fun u hu x' hx' => Or.inr ⟨x', hx', rfl⟩) (fun _ _ h => h) (fun _ _ h => h) + have j := J.j57_stepNWA w { σ with reprogramTimer := true } + exact JStage3A.of_schedWrN (blkQ := bq) (symL := symL) (schedValid := { J.schedValid with }) J (Q := Z.Q) (Rl := Z.R) (curOf := Z.curOf) (curScOf := Z.curScOf) + (s' := s') (a' := a) (σ' := { σ with reprogramTimer := true }) w HS SSz (QKeepA.of_kheap rfl ⟨rfl, rfl, rfl, rfl⟩) rfl J.j.1.ar (fun _ _ => rfl) J.bufAl + ⟨J.ent_stepNWA w blk', sysl⟩ sq poolOff H2 J.scCov ep ntfn reply buf J.idleNoFault mcp yr { J.sinv with irqN := by cases J.sinv.irqN; constructor <;> assumption } { J.sidle with } rv' + j.1 j.2 + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3KEntryTopA.lean b/proofs/Fl4ProofsA64/P3/Stage3KEntryTopA.lean new file mode 100644 index 000000000..1f6a5df8e --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3KEntryTopA.lean @@ -0,0 +1,245 @@ +/- +P3 (aarch64, A64-P3 Stage 3, kernel-entry composition, step 3): **the unified kernel-entry top**. + +Every slow-path kernel entry of FL4's AArch64 core is a body followed by `syscall.finish`. `KEventA` lists the +entries; `kBodyA` is FL4's body, `kCodeA` FL4's exported function and `kOpA` the spec body. `kernelEntry_stage3A`: +if the body refines the spec body and leaves the record with `SwA` (`KRecA`; the **plug-in `hbody`**, one per event, +owners in `handoff/a64p3-kentry.md`), and `finish` is `P3/Stage3KEntryFinishA`'s (`hfin`), then FL4's entry returns, +the spec entry `kOpA >>= fl4Finish` has the outcome and the next entry's record holds (`KExitA`). + +Out of scope: the fast paths and the local (beside-the-gate) paths, and `entry.rs`'s `set_vm_root` wrapper. +`kRuns` pins the syscall events to the branch FL4 takes on the actual entry state. + +New module. +-/ +import Fl4ProofsA64.P3.Stage3KEntryFinishA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4ProofsA64.P3.OuterEntry Classical + +noncomputable section + +/-- The guest entries (S4's), each with its spec body. -/ +inductive KGuestA where + | vcpuFault (hsr : U64) + | vgic (eisr : U64) + | vppi (irq : U64) + | vcpuExit (irq : U64) (stalled : Bool) + | runExit (stop w1 w2 w3 : U64) + | runVppi (irq : U64) + +/-- **The kernel entries** (module docstring). -/ +inductive KEventA where + /-- A system call in range of a thread that does not trap system calls. -/ + | syscall (number : I64) (rep : Bool) (call : Fl4SpecA64.Syscall) + | userException (number code : U64) + | vmFault (addr esr : U64) + | interrupt (irq : U64) (service : SMonad Unit Unit) + | guest (e : KGuestA) (op : SMonad Unit Unit) + +variable {S A : Type} (st : machine.Store S) (ar : machine.Arch A) [L : LawfulStore st] + +/-- FL4's guest-entry bodies: the exported function before `finish`. -/ +def kGuestBodyA (s : S) (a : A) : KGuestA → Result (S × A) + | .vcpuFault hsr => do + let (b, s1, a1) ← syscall.budget_ok st ar s a + if b then do + let t ← sched.cur_thread st s1 + let next ← obj.Tcb.reg st t s1 layout.reg.NEXT_IP + let s3 ← obj.Tcb.set_reg st t s1 layout.reg.FAULT_IP next + faults.handle_fault st ar s3 a1 t (error.Fault.VcpuFault hsr) + else ok (s1, a1) + | .vgic eisr => do + let (s1, a1) ← sched.update_timestamp st ar s a + let (_, s2, a2) ← sched.check_budget st ar s1 a1 + vcpu.handle_vgic_maintenance st ar s2 a2 eisr + | .vppi irq => do + let (s1, a1) ← sched.update_timestamp st ar s a + let (_, s2, a2) ← sched.check_budget st ar s1 a1 + vcpu.handle_vppi_event st ar s2 a2 irq + | .vcpuExit irq stalled => do + let (s1, a1) ← + if stalled then ok (s, a) + else do + let (s2, a2) ← sched.update_timestamp st ar s a + let ct ← sched.cur_thread st s2 + let (s3, a3) ← vcpu.check st ar s2 a2 ct + let (_, s4, a4) ← sched.check_budget st ar s3 a3 + ok (s4, a4) + if irq != syscall.NO_IRQ then interrupt.handle_interrupt st ar s1 a1 irq else ok (s1, a1) + | .runExit stop w1 w2 w3 => do + let (s1, a1) ← sched.update_timestamp st ar s a + let ct ← sched.cur_thread st s1 + let (s2, a2) ← vcpu.complete_run st ar s1 a1 ct stop w1 w2 w3 + let (_, s3, a3) ← sched.check_budget st ar s2 a2 + ok (s3, a3) + | .runVppi irq => do + let (s1, a1) ← sched.update_timestamp st ar s a + let ct ← sched.cur_thread st s1 + let i ← layout.tcb.RUN_VCPU + let i1 ← lift (core.num.U64.wrapping_add ct i) + let v ← st.read s1 i1 + let s2 ← + if v != 0#u64 then + if irq = layout.irq.TIMER_ID then do + let i2 ← lift (core.num.U64.wrapping_add v vcpu.VPPI_MASKED) + let masked ← st.read s1 i2 + let i3 ← lift (core.num.U64.wrapping_add v vcpu.VPPI_MASKED) + let i4 ← lift (masked ||| 1#u64) + st.write s1 i3 i4 + else ok s1 + else ok s1 + let i2 ← st.cur_cpu s2 + let i3 ← layout.irq.of irq i2 + let a2 ← ar.mask_irq a1 i3 true + let (s3, a3) ← vcpu.complete_run st ar s2 a2 ct fl4_abi.consts.vcpu_run_stop.VPPI irq 0#u64 0#u64 + let (_, s4, a4) ← sched.check_budget st ar s3 a3 + ok (s4, a4) + +/-- FL4's exported guest entries. -/ +def kGuestCodeA (s : S) (a : A) : KGuestA → Result (S × A) + | .vcpuFault hsr => syscall.handle_vcpu_fault st ar s a hsr + | .vgic eisr => syscall.handle_vgic_maintenance_entry st ar s a eisr + | .vppi irq => syscall.handle_vppi_event_entry st ar s a irq + | .vcpuExit irq stalled => syscall.handle_vcpu_exit st ar s a irq stalled + | .runExit stop w1 w2 w3 => syscall.handle_vcpu_run_exit st ar s a stop w1 w2 w3 + | .runVppi irq => syscall.handle_vcpu_run_vppi st ar s a irq + +/-- **FL4's entry bodies** (module docstring). -/ +def kBodyA (s : S) (a : A) : KEventA → Result (S × A) + | .syscall number _ _ => do + let t ← sched.cur_thread st s + let next ← obj.Tcb.reg st t s layout.reg.NEXT_IP + let i ← lift (core.num.U64.wrapping_sub next 4#u64) + let s1 ← obj.Tcb.set_reg st t s layout.reg.FAULT_IP i + let (b, s2, a1) ← syscall.budget_ok st ar s1 a + if b then syscall.dispatch_syscall st ar s2 a1 number else ok (s2, a1) + | .userException number code => do + let (b, s1, a1) ← syscall.budget_ok st ar s a + if b then do + let t ← sched.cur_thread st s1 + faults.handle_fault st ar s1 a1 t (error.Fault.UserException number code) + else ok (s1, a1) + | .vmFault addr esr => do + let (b, s1, a1) ← syscall.budget_ok st ar s a + if b then do + let t ← sched.cur_thread st s1 + let i ← esr >>> 26#i32 + let i1 ← lift (i &&& 63#u64) + let f ← vspace.vm_fault addr esr (i1 = 32#u64) + faults.handle_fault st ar s1 a1 t f + else ok (s1, a1) + | .interrupt irq _ => do + let (s1, a1) ← sched.update_timestamp st ar s a + let (_, s2, a2) ← sched.check_budget st ar s1 a1 + interrupt.handle_interrupt st ar s2 a2 irq + | .guest e _ => kGuestBodyA st ar s a e + +/-- FL4's exported entry functions. -/ +def kCodeA (s : S) (a : A) : KEventA → Result (S × A) + | .syscall number _ _ => syscall.handle_syscall_general st ar s a number + | .userException number code => syscall.handle_user_exception st ar s a number code + | .vmFault addr esr => syscall.handle_vm_fault st ar s a addr esr + | .interrupt irq _ => syscall.handle_interrupt_entry st ar s a irq + | .guest e _ => kGuestCodeA st ar s a e + +/-- The entry's state selects the branch the event names: for a system call, the number is in range and the current +thread does not trap system calls. -/ +def kRuns (s : S) : KEventA → Prop + | .syscall number _ _ => ¬ number < fl4_abi.syscall.SYSCALL_MIN ∧ ¬ number > fl4_abi.syscall.SYSCALL_MAX ∧ + ∃ t next s1 f, sched.cur_thread st s = ok t ∧ obj.Tcb.reg st t s layout.reg.NEXT_IP = ok next ∧ + obj.Tcb.set_reg st t s layout.reg.FAULT_IP (core.num.U64.wrapping_sub next 4#u64) = ok s1 ∧ + obj.Tcb.flags st t s1 = ok f ∧ f &&& fl4_abi.consts.tcb_flag.TRAP_SYSCALLS = 0#u64 + | _ => True + +/-- **The spec entry bodies**, at the entry state's current thread. -/ +def kOpA : KEventA → SMonad Unit Unit + | .syscall _ rep call => do + let ct ← NonDet.gets fun k : State Unit => k.curThread + fl4SyscallFaultIP ct + let _ ← ExceptT.run <| NonDetE.handleE (fl4HandleEvent rep (.syscallEvent call)) + (fun _ => withoutPreemption fl4PreemptionPathIrq) + pure () + | .userException number code => fl4BudgetFault (.userException number.bv code.bv) + | .vmFault addr esr => fl4BudgetFault (fl4VmFault addr.bv esr.bv) + | .interrupt _ service => fl4AcceptedIrqBody service + | .guest _ op => op + +variable {st ar} + +/-- Reassociate FL4's binds and compare the two programs step by step. -/ +local macro "kassocA" : tactic => `(tactic| ( + try simp only [Std.bind_assoc] + repeat' (first + | rfl + | (congr 1; funext x + try (obtain ⟨_, z⟩ : _ × _ := x + try (obtain ⟨_, _⟩ : _ × _ := z)) + try simp only [uncurry_apply_pair, Std.bind_assoc, bind_ok]) + | (rw [Std.bind_assoc]) + | split))) + +/-- The spec system-call entry is `fl4KernelEntrySyscall`. -/ +theorem kOpA_syscall (limit : Nat) (view : State Unit → Fl4TimerView) (number : I64) (rep : Bool) + (call : Fl4SpecA64.Syscall) : + (kOpA (.syscall number rep call) >>= fun _ => fl4Finish limit view) = + fl4KernelEntrySyscall limit view rep call := by + unfold kOpA fl4KernelEntrySyscall fl4CallKernel fl4Finish + have assoc : ∀ {α β γ : Type} (x : SMonad Unit α) (f : α → SMonad Unit β) (g : β → SMonad Unit γ), + ((x >>= f) >>= g) = (x >>= fun v => f v >>= g) := fun x f g => funext (nd_bind_assocSc x f g) + simp only [assoc, nd_pure_bindA] + +/-- **FL4's entry is its body followed by `finish`.** -/ +theorem kCodeA_body (s : S) (a : A) (e : KEventA) (h : kRuns st s e) : + kCodeA st ar s a e = (do let (s1, a1) ← kBodyA st ar s a e; syscall.finish st ar s1 a1) := by + cases e with + | syscall number rep call => + obtain ⟨lo, hi, t, next, s1, f, e1, e2, e3, e4, e5⟩ := h + simp only [kCodeA, kBodyA] + unfold syscall.handle_syscall_general syscall.syscall + simp only [e1, e2, e3, e4, e5, lift, bind_ok, Std.bind_assoc, bne_self_eq_false, Bool.false_eq_true, if_false, + if_neg lo, if_neg hi] + kassocA + all_goals try simp only [bind_ok, uncurry_apply_pair] + all_goals + conv => rhs; rw [← bind_pure (syscall.finish st ar _ _)] + congr 1; funext x + obtain ⟨_, _⟩ := x + simp only [uncurry_apply_pair, bind_ok] + rfl + | userException number code => + simp only [kCodeA, kBodyA]; unfold syscall.handle_user_exception; kassocA + | vmFault addr esr => + simp only [kCodeA, kBodyA]; unfold syscall.handle_vm_fault; kassocA + | interrupt irq service => + simp only [kCodeA, kBodyA]; unfold syscall.handle_interrupt_entry; kassocA + | guest g op => + cases g <;> simp only [kCodeA, kBodyA, kGuestCodeA, kGuestBodyA] + · unfold syscall.handle_vcpu_fault; kassocA + · unfold syscall.handle_vgic_maintenance_entry; kassocA + · unfold syscall.handle_vppi_event_entry; kassocA + · unfold syscall.handle_vcpu_exit; kassocA + · unfold syscall.handle_vcpu_run_exit; kassocA + · unfold syscall.handle_vcpu_run_vppi; kassocA + +/-- **The unified kernel-entry top** (module docstring). -/ +theorem kernelEntry_stage3A {AR : A → HwRootA → Prop} {nA : U64} {m : McState} + {view : State Unit → Fl4TimerView} (s : S) (a : A) (σ : State Unit) (e : KEventA) (run : kRuns st s e) + {KRec : S → A → State Unit → Prop} + (hbody : kBodyA st ar s a e ⦃ r => StepPostA KRec (kOpA e) σ r ⦄) + (hfin : ∀ r τ, kBodyA st ar s a e = ok r → ((), τ) ∈ (kOpA e σ).1 → KRec r.1 r.2 τ → + syscall.finish st ar r.1 r.2 ⦃ out => StepPostA (KExitA st ar AR nA m) (fl4Finish kLimitA view) τ out ⦄) : + kCodeA st ar s a e ⦃ out => + StepPostA (KExitA st ar AR nA m) (kOpA e >>= fun _ => fl4Finish kLimitA view) σ out ⦄ := by + rw [kCodeA_body s a e run] + refine WP.spec_bind (reachedResultA hbody) ?_ + rintro ⟨s1, a1⟩ ⟨eb, τ, op, R⟩ + simp only [uncurry_apply_pair] + refine WP.spec_mono (hfin (s1, a1) τ eb op R) ?_ + rintro out ⟨ρ, fin, X⟩ + exact ⟨ρ, mem_bindA.2 ⟨((), τ), op, fin⟩, X⟩ + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3KtA.lean b/proofs/Fl4ProofsA64/P3/Stage3KtA.lean new file mode 100644 index 000000000..34461b20c --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3KtA.lean @@ -0,0 +1,176 @@ +/- +P3 (aarch64, record edit #5 step A): **the unified record with kernel tables added to the heap** (`KtH H Kt`: an +`other 12` object at every table of `Kt`, A64 P1's `KtOut` placement). Used by the `w1` witness, whose record heap is +`KtH (w1H …) {kt}` so that J7's `S1aR2` / `KTab` part can name the one kernel table of `w1Ar ar`. + +* **`JStage3EntryA.addKt`**: every field of the entry record at `H` holds at `KtH H Kt`; J5 and J7 are supplied at the + new heap. Side conditions beyond `KtOut`: the user words `U` are off the kernel tables, and no device frame cap + names one. + +New module; no existing statement changed. +-/ +import Fl4ProofsA64.P3.Stage3A +import Fl4ProofsA64.P3.Stage3D1bA +import Fl4ProofsA64.P1.S1aKTab +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +theorem CapKindA.monoH {H H' : Heap} {c : capability.Cap} (hg : ∀ q k, H q = some k → H' q = some k) + (h : CapKindA H c) : CapKindA H' c := + ⟨fun p b x y z w e => hg _ _ (h.1 p b x y z w e), fun p b x y e => hg _ _ (h.2.1 p b x y e), + fun p g e => hg _ _ (h.2.2.1 p g e), fun p sz e => hg _ _ (h.2.2.2.1 p sz e), h.2.2.2.2⟩ + +theorem PtHA.monoH {H H' : Heap} {P : Set U64} (hg : ∀ q k, H q = some k → H' q = some k) (h : PtHA H P) : + PtHA H' P := fun p hp => by + obtain ⟨q, z, hq, h1, h2⟩ := h p hp + exact ⟨q, z, hg _ _ hq, h1, h2⟩ + +theorem PoolOff.addKt {H : Heap} {Cs OS Vc Ub Kt : Set U64} {s : S} (X : KtOut st H Cs OS Vc Ub Kt s) + (h : PoolOff st H Cs OS s) : PoolOff st (KtH H Kt) Cs OS s := fun i => + WP.spec_mono (h i) fun _ hp hne asid => + have w := hp hne asid + ⟨w.al, X.addOKP.omega_sub w.om, by rw [X.addOKP.frame_eq_other]; exact w.sg, w.os, w.ks⟩ + +/-- A `KtOut` placement needs no user-word set. -/ +theorem KtOut.noUb {H : Heap} {Cs OS Vc Ub Kt : Set U64} {s : S} (X : KtOut st H Cs OS Vc Ub Kt s) : + KtOut st H Cs OS Vc ∅ Kt s := + ⟨X.al, X.fits, X.nz, X.fresh, X.disj, X.nodes, X.ks, X.ut, X.os, X.vc, fun _ _ _ h => absurd h (Set.notMem_empty _)⟩ + +/-- **The entry record with the kernel tables added** (module docstring). -/ +theorem JStage3EntryA.addKt {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} + {κ : U64 → U64} {n : Nat} {J5 J7 J5' J7' : JSlotA S} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} {Kt : Set U64} + (J : JStage3EntryA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (X : KtOut st H Z.Cs OS Z.V ∅ Kt s) (hvf : ∀ v ∈ Z.V, v.val + 4096 ≤ 2 ^ 64) + (hU : ∀ q ∈ Kt, ∀ b ∈ U, b.val < q.val ∨ q.val + 4096 ≤ b.val) + (hKw : ∀ q ∈ Kt, layout.PPTR_BASE.val ≤ q.val ∧ q.val + 4096 ≤ layout.PPTR_BASE.val + 2 ^ 44) + (hdev : ∀ y ∈ OS, ∀ p size rights mapped ept asid va, + capA st s y = .Frame p size rights true mapped ept asid va → p ∉ Kt) + (h5 : J5' Z (KtH H Kt) OS s σ m) (h7 : J7' Z (KtH H Kt) OS s σ m) : + JStage3EntryA st AR Z (KtH H Kt) OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5' J7' s a σ m := by + have Aok := X.addOKP + have eT : (KtH H Kt).tcbs = H.tcbs := Aok.tcbs_eq (by simp) + have eS : (KtH H Kt).scs = H.scs := Aok.scs_eq (by simp) + have eR : (KtH H Kt).replies = H.replies := Aok.replies_eq (by simp) + have eN : (KtH H Kt).ntfns = H.ntfns := Aok.ntfns_eq (by simp) + have eZ : (KtH H Kt).scBits = H.scBits := Aok.scBits_eq (by simp) + have eF : (KtH H Kt).frame = H.frame := Aok.frame_eq_other + have hg := X.hg + have oS := Aok.omega_sub + have E0 := J.sys.ent + have newOm : ∀ b ∈ (KtH H Kt).omega, b ∈ H.omega ∨ (Aligned b ∧ ∃ q ∈ Kt, q.val ≤ b.val ∧ b.val < q.val + 4096) := + fun b hb => (Aok.omega_other hb).imp id fun ⟨hal, q, hq, h1, h2⟩ => + ⟨hal, q, hq, h1, by simpa [HKind.bits] using h2⟩ + have sq' : ∀ Q R, SchedQueuesInv st H.tcbs Z.Cs H.omega H s Q R → + SchedQueuesInv st (KtH H Kt).tcbs Z.Cs (KtH H Kt).omega (KtH H Kt) s Q R := fun Q R h0 => by + rw [eT] + exact + { h0 with + eps := h0.eps.addOther (by simp) Aok E0.heap fun a ha => (Aok.omega_other ha).imp id + fun ⟨hal, q, hq, h1, h2⟩ => ⟨q, hq, hal, h1, by simpa [HKind.bits] using h2⟩ + omega := fun b hb => by + rcases newOm b hb with hb | ⟨hal, q, hq, h1, h2⟩ + · exact h0.omega b hb + · refine ⟨hal, fun t ht => ?_, fun c hc => ?_⟩ + · have := Aok.disjH q hq t .tcb ht + rw [show (HKind.tcb).bits = 12 from rfl, show (HKind.other 12).bits = 12 from rfl] at this; omega + · have := Aok.nodes q hq c hc; rw [show (HKind.other 12).bits = 12 from rfl] at this; omega } + have bxi' : BXI st (KtH H Kt).tcbs Z.Cs (KtH H Kt).omega (KtH H Kt) (KtH H Kt).frame (KtH H Kt).replies + (KtH H Kt).scs (KtH H Kt).ntfns (KtH H Kt).scBits Ac s := by + have h := E0.bxi + have h' : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s := + ⟨h.1, h.2.1, h.2.2.1, h.2.2.2.2, h.2.2.2.1⟩ + have g := BlkOK.addOther (N := (· ∈ Kt)) (z := 12) Aok E0.heap h' + exact ⟨g.1, g.2.1, g.2.2.1, g.2.2.2.2, g.2.2.2.1⟩ + have user' : Fl4ProofsA64.P1.UserOK st (KtH H Kt).tcbs Z.Cs (KtH H Kt).omega (KtH H Kt) U s := fun b hb => by + obtain ⟨h1, h2, h3⟩ := E0.user b hb + refine ⟨by rw [eT]; exact h1, fun hm => ?_, fun e he => ?_⟩ + · rcases newOm b hm with hm | ⟨-, q, hq, l1, l2⟩ + · exact h2 hm + · rcases hU q hq b hb with h | h <;> omega + · refine h3 e (he.imp (fun h => ?_) fun h => ?_) + · exact (Aok.some_ne (by simp)).1 h + · exact (Aok.some_ne (by simp)).1 h + have jobj : ∀ p bytes, JointObjectA Z (KtH H Kt) p bytes → + JointObjectA Z H p bytes ∨ (p ∈ Kt ∧ bytes = 4096) := by + intro p bytes h + rcases h with h | h | h | ⟨k, hk, rfl⟩ + · exact Or.inl (Or.inl h) + · exact Or.inl (Or.inr (Or.inl h)) + · exact Or.inl (Or.inr (Or.inr (Or.inl h))) + · rcases Heap.add_eq_some.1 hk with ⟨hn, rfl⟩ | ⟨-, hk'⟩ + · exact Or.inr ⟨hn, two12⟩ + · exact Or.inl (Or.inr (Or.inr (Or.inr ⟨k, hk', rfl⟩))) + have vs0 : VrOK H.omega H.frame H.scs H.scBits OS Z.V ∅ (L.ksb s) (utVsPtA st Pp Tb Lv s) → + VrOK (KtH H Kt).omega (KtH H Kt).frame (KtH H Kt).scs (KtH H Kt).scBits OS Z.V ∅ (L.ksb s) + (utVsPtA st Pp Tb Lv s) := fun R => + vrOK_monoPtA (X.vrOK hvf E0.ksw R) Set.subset_union_left + exact + { J with + sys := + ⟨⟨X.heapOK E0.heap, bxi', X.osok E0.os, user', E0.vs, E0.ksw⟩, SysL.addKt X J.sys.sysl⟩ + tcbs := eT.trans J.tcbs + scs := eS.trans J.scs + sq := sq' _ _ J.sq + ap := + { J.ap with + sca := by rw [eS, eZ]; exact J.ap.sca + vc := by rw [eT]; exact J.ap.vc + vs := by rw [eT]; exact J.ap.vs + ext := by rw [eT]; exact J.ap.ext } + ko := X.ksOff J.ko + ptH := J.ptH.monoH hg + poolOff := PoolOff.addKt X J.poolOff + objAlloc := fun p bytes hp u hu q b i d hc => by + rcases jobj p bytes hp with h | ⟨hk, rfl⟩ + · exact J.objAlloc p bytes h u hu q b i d hc + · rcases X.ut p hk u hu q b i d hc with h | h + · exact Or.inl h + · exact Or.inr (Or.inl h) + scSize := by rw [eZ]; exact J.scSize + setsInH := + ⟨J.setsInH.pages.monoH hg, fun p hp => by + obtain ⟨q, z, hq, h1, h2⟩ := J.setsInH.roots p hp + exact ⟨q, z, hg _ _ hq, h1, h2⟩⟩ + utIpc := ⟨fun p h => hg _ _ (J.utIpc.ntfn p h), fun p h => hg _ _ (J.utIpc.ep p h), + fun p h => hg _ _ (J.utIpc.reply p h)⟩ + devFr := fun y hy p size rights mapped ept asid va hc k hk => by + rcases Heap.add_eq_some.1 hk with ⟨hn, -⟩ | ⟨-, hk'⟩ + · exact absurd hn (hdev y hy p size rights mapped ept asid va hc) + · exact J.devFr y hy p size rights mapped ept asid va hc k hk' + ipcHeapCov := + ⟨fun p h => J.ipcHeapCov.ntfn p ((Aok.some_ne (by simp)).1 h), + fun p h => J.ipcHeapCov.ep p ((Aok.some_ne (by simp)).1 h), + fun p h => J.ipcHeapCov.reply p ((Aok.some_ne (by simp)).1 h)⟩ + zombieTcb := fun u hu p k h => by rw [eT]; exact J.zombieTcb u hu p k h + vsOk := vs0 J.vsOk + devUtOff := + { J.devUtOff with + obj := fun u hu q b i hc p bytes hp => by + rcases jobj p bytes hp with h | ⟨hk, rfl⟩ + · exact J.devUtOff.obj u hu q b i hc p bytes h + · exact X.ut p hk u hu q b i true hc } + capTy := fun u hu => (J.capTy u hu).monoH hg + heapWin := fun p k hp => by + rcases Heap.add_eq_some.1 hp with ⟨hn, rfl⟩ | ⟨-, hp'⟩ + · rw [two12]; exact hKw p hn + · exact J.heapWin p k hp' + symL := SymL.addKt X J.symL + fhOK := fun t ht => (J.fhOK t ht).imp id fun ⟨ep, b0, b1, b2, b3, b4, h1, h2⟩ => + ⟨ep, b0, b1, b2, b3, b4, h1, hg _ _ h2⟩ + curVcpuNone := J.curVcpuNone + ipcSorted := J.ipcSorted + activeReplyScs := J.activeReplyScs + bufVs := J.bufVs + j5 := h5 + j7 := h7 } + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3Lift3QA.lean b/proofs/Fl4ProofsA64/P3/Stage3Lift3QA.lean new file mode 100644 index 000000000..29251695b --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3Lift3QA.lean @@ -0,0 +1,40 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 3): **`QKeepA` from a backward object relation.** `QKeepA.ofBwd`: if every object of the +post-state is the same-address pre-state object related by `QObjA`, the step is `QKeepA`. The lift-3 arms replace one or two +objects by a copy with a changed link field; each gives its `QKeepA` (the `Qk` argument of `of_stepFinA` / +`of_ipcStepA`) from its `key` lemma. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3SortA + +namespace Fl4ProofsA64.P3 +open Fl4SpecA64 + +/-- Every object is `QObjA`-related to itself. -/ +theorem QObjA.refl (o : KernelObject) : QObjA o o := + ⟨fun y h => ⟨y, h, rfl⟩, fun x' h => ⟨x', h, List.Sublist.refl _⟩, fun x' h => ⟨x', h, List.Sublist.refl _⟩, + fun y' k h => ⟨y', k, h, fun r => ⟨r, id⟩⟩, fun y h => ⟨y, h, id⟩⟩ + +/-- `QKeepA` from a backward object relation. -/ +theorem QKeepA.ofBwd {σ σ' : State Unit} + (bw : ∀ q o', σ'.kheap q = some o' → ∃ o, σ.kheap q = some o ∧ QObjA o o') + (D : DomKeepA σ σ' := by first | exact ⟨rfl, rfl, rfl, rfl⟩ | exact DomKeepA.refl _) : QKeepA σ σ' := by + refine ⟨fun q y e => ?_, fun q y e => ?_, fun q y e => ?_, fun q y k e => ?_, fun q y e => ?_, D⟩ + · obtain ⟨o, ho, R⟩ := bw q _ e + obtain ⟨x, hx, k⟩ := R.prio y rfl + exact ⟨x, by rw [ho, hx], k⟩ + · obtain ⟨o, ho, R⟩ := bw q _ e + obtain ⟨x, hx, k⟩ := R.ep y rfl + exact ⟨x, by rw [ho, hx], k⟩ + · obtain ⟨o, ho, R⟩ := bw q _ e + obtain ⟨x, hx, k⟩ := R.ntfn y rfl + exact ⟨x, by rw [ho, hx], k⟩ + · obtain ⟨o, ho, R⟩ := bw q _ e + obtain ⟨x, k0, hx, k⟩ := R.sc y k rfl + exact ⟨x, k0, by rw [ho, hx], k⟩ + · obtain ⟨o, ho, R⟩ := bw q _ e + obtain ⟨x, hx, k⟩ := R.rt y rfl + exact ⟨x, by rw [ho, hx], k⟩ + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3LiftBodyA.lean b/proofs/Fl4ProofsA64/P3/Stage3LiftBodyA.lean new file mode 100644 index 000000000..953a507d3 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3LiftBodyA.lean @@ -0,0 +1,135 @@ +/- +P3 (aarch64, A64-P3 Lift 1b, Plan B merge): **the body-end facts (`BodyOKA`) of the Lift 1 arms**, building blocks. + +* `BodyKeepA.ofMach`, `BodyKeepA.ofSlotL`: a machine-state step / a cap-slot step that keeps `lastMachineTime` + (`getCurrentTime` is the only spec operation that writes it; every other machine operation goes through + `machineOpLift`, which writes `machineStateRest` only). +* `JSyscallA.curRunA`: the caller's TCB, as the syscall-entry record has it, is running or restarting. +* `JSyscallA.bodyOKA`: `BodyOKA σ m` at the syscall entry itself (an arm that changes nothing: a decoder `Err`). +* `RqActA σ σ'`: `σ'` is `σ` with other ready queues and, optionally, `choose_new_thread` (the `set_domain` steps). + `BodyOKA.ofRqAct`: the body-end facts after `Restart` and such a step. + +Named premises: none. +-/ +import Fl4ProofsA64.P3.Stage3BodyOKA +import Fl4ProofsA64.P3.Stage3SyscallA +import Fl4ProofsA64.P3.Stage3RestartA +import Fl4ProofsA64.P3.Stage3RsSiteA +import Fl4ProofsA64.P3.Stage3DispBaseA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Sched Fl4SpecA64 Fl4Spec + +noncomputable section + +theorem Sched.BodyKeepA.ofMach (σ : State Unit) (ms : MachineState) + (hl : ms.lastMachineTime = σ.machineState.lastMachineTime) : + Sched.BodyKeepA σ { σ with machineState := ms } := + ⟨rfl, rfl, hl, rfl, rfl, rfl, rfl, fun _ _ _ h => h, fun _ x h => ⟨x, h, rfl⟩⟩ + +theorem Sched.BodyKeepA.ofSlotL {σ σ' : State Unit} (H : SpecSlotA σ σ') + (hl : σ'.machineState.lastMachineTime = σ.machineState.lastMachineTime) : Sched.BodyKeepA σ σ' := by + have K := Sched.BodyKeepA.ofSlot (σ := σ) (σ' := { σ' with machineState := σ.machineState }) (H := by + refine ⟨⟨H.cur, H.idle, H.csc, H.act, H.time, H.irqS, H.irqN, H.rq, H.rls, H.cons, H.repr, H.dom, H.domT, + H.asid, H.vcur, ⟨H.domk.index, H.domk.start, H.domk.table, H.domk.gvs⟩⟩, H.kh⟩) rfl + exact ⟨K.act, K.time, hl, K.cur, K.csc, K.cons, K.rel, K.sc, K.tcb⟩ + +section +variable {S A : Type} {st : machine.Store S} [L : LawfulStore st] +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s : S} {a : A} {σ σ' : State Unit} {m : McState} + +/-- **The caller's TCB at a syscall entry is running or restarting.** -/ +theorem JSyscallA.curRunA (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {x : Tcb} (hx : σ.kheap σ.curThread = some (.tcb x)) : x.tcbState = .running ∨ x.tcbState = .restart := by + have ct : σ.curThread = specPtr (L.curW s) := J.j.1.sch.curThread + have htZ : L.curW s ∈ Z.T := J.sr.curT + have hk : σ.kheap (specPtr (L.curW s)) = some (.tcb x) := ct ▸ hx + have code : (L.mem s (stA (L.curW s))).val = tsCodeA x.tcbState := (J.ep.at_tcb htZ hk).st + have hr := (runnable_iffA code).1 J.running + have nvm := J.toJStage3EntryA.curNotVmA + have h8 : (sched.TS_RUNNING_VM : U64).val = 8 := by unfold sched.TS_RUNNING_VM; rfl + cases e : x.tcbState with + | running => exact Or.inl rfl + | restart => exact Or.inr rfl + | ext q => + cases q + rw [e] at code + exact absurd (UScalar.eq_of_val_eq (code.trans h8.symm)) nvm + | _ => rw [e] at hr; cases hr + +/-- **The body-end facts at the syscall entry** (an arm that changes no spec state). -/ +theorem JSyscallA.bodyOKA (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) : + Sched.BodyOKA σ m := + Sched.BodyOKA.ofEntry J (Or.inl rfl) rfl rfl rfl rfl rfl rfl (fun y' k h => ⟨y', h, rfl, rfl, rfl⟩) + (fun _ _ hx => J.curRunA hx) + +end + +/-- `σ'` is `σ` with other ready queues and, optionally, the scheduler action `choose_new_thread`. -/ +def RqActA (σ σ' : State Unit) : Prop := + ∃ f, σ' = { σ with readyQueues := f } ∨ σ' = { σ with readyQueues := f, schedulerAction := .chooseNewThread } + +section +variable {S A : Type} {st : machine.Store S} [L : LawfulStore st] +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s : S} {a : A} {σ σ' : State Unit} {m : McState} + +/-- **The body-end facts after `Restart` and a ready-queue step** (module docstring). -/ +theorem Sched.BodyOKA.ofRqAct (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {x : Tcb} (hk : σ.kheap σ.curThread = some (.tcb x)) (h : RqActA (stUpdA σ σ.curThread x .restart) σ') : + Sched.BodyOKA σ' m := by + obtain ⟨f, h | h⟩ := h + · subst h + exact Sched.BodyOKA.ofKeep J hk ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, fun _ _ _ h => h, fun _ x h => ⟨x, h, rfl⟩⟩ + · subst h + refine Sched.BodyOKA.ofEntry J (Or.inr rfl) rfl rfl rfl rfl rfl rfl (fun y' k hy => ?_) (fun e => by cases e) + have ne : σ.curSc ≠ σ.curThread := fun e => by + have h1 := J.curInv.sc + obtain ⟨y, j, hy'⟩ := h1 + rw [e, hk] at hy'; cases hy' + simp only [stUpdA, setObjA, Function.update_of_ne ne] at hy + exact ⟨y', hy, rfl, rfl, rfl⟩ + +/-- **The `CUR_THREAD` word at a body's end** from the spec's current thread. -/ +theorem A64RelS3.curW_eq {S A : Type} {st : machine.Store S} [L : LawfulStore st] {AR : A → HwRootA → Prop} + {ar : machine.Arch A} {nA : U64} {n : Nat} {Cs : Set U64} {s s' : S} {a a' : A} {σ σ' : State Unit} + {m : McState} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} {ia ie : Nat} + {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + (R : A64RelS3 st AR ar nA n Cs s' a' σ' m) + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (h : σ'.curThread = σ.curThread) : L.curW s' = L.curW s := by + obtain ⟨Z', H', OS', U', Ac', Pp', Tb', Lv', ia', ie', N', E', Rp', Bf', stack', κ', -, J', -⟩ := R + exact Sched.curW_keptA J J' h + +/-- The caller's TCB after a one-word reply: its context replaced and its state `Running`. -/ +def replyTcbA (x : Tcb) (uc : UserContext) : Tcb := + { x with tcbArch := { x.tcbArch with tcbContext := uc }, tcbState := .running } + +/-- The caller's state after a one-word reply: its TCB is `replyTcbA`. -/ +def ReplyShapeA (σ0 σ' : State Unit) (x : Tcb) : Prop := + ∃ uc, σ' = setObjA σ0 σ0.curThread (.tcb (replyTcbA x uc)) + +/-- **The body-end facts after a one-word reply** of the caller (running or restarting at the entry). -/ +theorem Sched.BodyOKA.ofReply (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {x : Tcb} (hk : σ.kheap σ.curThread = some (.tcb x)) {uc : UserContext} + (h : σ' = setObjA σ σ.curThread (.tcb (replyTcbA x uc))) : Sched.BodyOKA σ' m := by + subst h + refine Sched.BodyOKA.ofEntry J (Or.inl rfl) rfl rfl rfl rfl rfl rfl (fun y' k hy => ?_) (fun _ x' hx' => ?_) + · have ne : σ.curSc ≠ σ.curThread := fun e => by + obtain ⟨y, j, hy'⟩ := J.curInv.sc + rw [e, hk] at hy'; cases hy' + simp only [setObjA, Function.update_of_ne ne] at hy + exact ⟨y', hy, rfl, rfl, rfl⟩ + · simp only [setObjA, Function.update_self, Option.some.injEq, KernelObject.tcb.injEq, replyTcbA] at hx' + subst hx' + exact Or.inl rfl + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3LiftDomainA.lean b/proofs/Fl4ProofsA64/P3/Stage3LiftDomainA.lean new file mode 100644 index 000000000..015a35a44 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3LiftDomainA.lean @@ -0,0 +1,554 @@ +/- +P3 (aarch64, A64-P3 Lift 1b): **`DomainSetSet` at record level**, on another thread and on the caller. + +* `RqOnlyA σ σ'`: `σ'` is `σ` with other ready queues. `SInvXA.rqA` / `SchedValidA.rqA`: the two spec invariants + across a ready-queue replacement whose new members are old members or a runnable, active, non-current, non-idle + thread outside the release queue. `SInvXA.chooseRqA` / `SchedValidA.chooseRqA`: the same with the action set to + `choose_new_thread`, the current thread allowed (`SInvXA.ctQ`, weakened by record edit #8a, is then vacuous). +* `setDomain_otherA`: FL4's `set_domain` of a local thread `t ≠` the current thread (dequeue, the zero domain word, + enqueue when schedulable, no reschedule) against l4v's `setDomain`, with `YieldQueueRelA`, the `MFrA` frame and both + spec invariants after. `setDomain_selfA`: the current thread, with the final `reschedule_required`. +* `JStage3A.setDomain_stepA`: the record across either step (`of_stepFinA`); `BlkOK` at the same `Ac` from P1's + `sched.set_domain.blkF`. +* `JSyscallA.setDomainA`: `decode_invocation` of `DomainSetSet` on the Domain cap at a syscall entry: the stall, the + caller's Restart (`of_restartCurA`), then `setDomain_stepA`; `fin` from `decodeFinA`. + +Named premises: [P] `ArchTotal`, `MaxSpi`, `ArchPlat`, `A64FrameMasks` (through `decodeFinA`); [S] `hstall` (the +remote stall of the target is a no-op) and `hon` (the target is on this core), discharged in `P3/Stage3DispDomA`. +Only the accepted decode is lifted. New module. +-/ +import Fl4ProofsA64.P3.DomainSetDispatchA +import Fl4ProofsA64.P3.Stage3IpcGlueA +import Fl4ProofsA64.P3.Stage3RestartA +import Fl4ProofsA64.P3.Stage3RsSiteA +import Fl4ProofsA64.P3.Stage3FinA +import Fl4ProofsA64.P3.Stage3SyscallA +import Fl4ProofsA64.P3.TcbRestartFrameA +import Fl4ProofsA64.P3.Stage3SchedValidStepA +import Fl4ProofsA64.P1.QueueKeeps +import Fl4ProofsA64.P3.ScSwitchA +import Fl4ProofsA64.P3.ScYieldQueueA +import Fl4ProofsA64.P3.Stage3LiftBodyA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-- `σ'` is `σ` with other ready queues. -/ +def RqOnlyA (σ σ' : State Unit) : Prop := σ' = { σ with readyQueues := σ'.readyQueues } + +theorem RqOnlyA.refl (σ : State Unit) : RqOnlyA σ σ := rfl + +theorem RqOnlyA.trans {σ1 σ2 σ3 : State Unit} (h1 : RqOnlyA σ1 σ2) (h2 : RqOnlyA σ2 σ3) : RqOnlyA σ1 σ3 := by + unfold RqOnlyA at h1 h2 ⊢ + rw [h2]; conv_lhs => rw [h1] + +theorem RqOnlyA.kheap {σ σ' : State Unit} (h : RqOnlyA σ σ') : σ'.kheap = σ.kheap := by rw [h] + +theorem RqOnlyA.spec {σ σ' : State Unit} (h : RqOnlyA σ σ') : SpecSchedA σ σ' := by + rw [h] + exact { kh := fun _ => Or.inl rfl, irqS := rfl, irqN := rfl, asid := rfl, vcur := rfl, cdt := rfl, orig := rfl } + +theorem RqOnlyA.setRq (σ : State Unit) (p : Priority) (q : List ObjRef) : RqOnlyA σ (setRqA σ p q) := rfl + +/-- A ready-queue action of a domain-0 TCB writes one ready queue. -/ +theorem rq_eqA {σ σ' : State Unit} {t : ObjRef} {x : Tcb} (hk : σ.kheap t = some (.tcb x)) (hd : x.tcbDomain = 0) + {f : ObjRef → List ObjRef → List ObjRef} (h : tcbSchedAction (Z := Unit) f t σ = ({((), σ')}, False)) : + σ' = setRqA σ x.tcbPriority (f t (σ.readyQueues 0 x.tcbPriority)) := by + rw [tcbSchedAction_eqA hk hd f] at h + exact (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst h))).2.symm + +theorem setRq_memA {σ : State Unit} {p : Priority} {q : List ObjRef} {d : Domain} {p' : Priority} {u : ObjRef} + (h : u ∈ (setRqA σ p q).readyQueues d p') : u ∈ σ.readyQueues d p' ∨ (d = 0 ∧ p' = p ∧ u ∈ q) := by + unfold setRqA at h + dsimp only at h + split at h + · next e => exact Or.inr ⟨e.1, e.2, h⟩ + · exact Or.inl h + +/-- **`SInvXA` across a ready-queue replacement** (module docstring). -/ +theorem SInvXA.rqA {σ : State Unit} {m : McState} {n : Nat} (h : SInvXA σ m n) (f : Domain → Priority → List ObjRef) + (hm : ∀ d p u, u ∈ f d p → u ∈ σ.readyQueues d p ∨ + (u ≠ σ.curThread ∧ (∀ c < n, u ≠ m.idleThreadOf c) ∧ ∃ x, σ.kheap u = some (.tcb x) ∧ runnable x.tcbState = true)) : + SInvXA { σ with readyQueues := f } m n where + mc := h.mc + view := h.view + curTcb := h.curTcb + idleTcb := h.idleTcb + onlyIdle := h.onlyIdle + idleSc := h.idleSc + curSc := h.curSc + ctQ ha d p hc := by + rcases hm d p _ hc with e | ⟨e, -⟩ + · exact h.ctQ ha d p e + · exact e rfl + idleQ d p c hc hi := by + rcases hm d p _ hi with e | ⟨-, e, -⟩ + · exact h.idleQ d p c hc e + · exact e c hc rfl + rq d p u hu := by + rcases hm d p u hu with e | ⟨-, -, e⟩ + · exact h.rq d p u e + · exact e + rls := h.rls + rpos := h.rpos + irqN := SpecIrqA.of_eq h.irqN rfl rfl + zc := h.zc + rlsRun := h.rlsRun + curScReady := h.curScReady + faultSt := h.faultSt + +/-- **`SchedValidA` across a ready-queue replacement** (module docstring). -/ +theorem SchedValidA.rqA {σ : State Unit} (h : SchedValidA σ) (f : Domain → Priority → List ObjRef) + (hm : ∀ d p u, u ∈ f d p → u ∈ σ.readyQueues d p ∨ (ActTcbA σ u ∧ u ∉ σ.releaseQueue)) : + SchedValidA { σ with readyQueues := f } := + ⟨fun d p u hu => (hm d p u hu).elim (h.rq d p u) id, h.act, h.blk, h.rls⟩ + +/-- **`SInvXA` across a ready-queue replacement that requests a new thread**: `ctQ` is vacuous, so the current +thread may be queued. -/ +theorem SInvXA.chooseRqA {σ : State Unit} {m : McState} {n : Nat} (h : SInvXA σ m n) + (f : Domain → Priority → List ObjRef) + (hm : ∀ d p u, u ∈ f d p → u ∈ σ.readyQueues d p ∨ + ((∀ c < n, u ≠ m.idleThreadOf c) ∧ ∃ x, σ.kheap u = some (.tcb x) ∧ runnable x.tcbState = true)) : + SInvXA { σ with readyQueues := f, schedulerAction := .chooseNewThread } m n where + mc := h.mc + view := h.view + curTcb := h.curTcb + idleTcb := h.idleTcb + onlyIdle := h.onlyIdle + idleSc := h.idleSc + curSc := h.curSc + ctQ e := by cases e + idleQ d p c hc hi := by + rcases hm d p _ hi with e | ⟨e, -⟩ + · exact h.idleQ d p c hc e + · exact e c hc rfl + rq d p u hu := by + rcases hm d p u hu with e | ⟨-, e⟩ + · exact h.rq d p u e + · exact e + rls := h.rls + rpos := h.rpos + irqN := SpecIrqA.of_eq h.irqN rfl rfl + zc := h.zc + rlsRun := h.rlsRun + curScReady := h.curScReady + faultSt := h.faultSt + +/-- **`SchedValidA` across the same step.** -/ +theorem SchedValidA.chooseRqA {σ : State Unit} (h : SchedValidA σ) (f : Domain → Priority → List ObjRef) + (hm : ∀ d p u, u ∈ f d p → u ∈ σ.readyQueues d p ∨ (ActTcbA σ u ∧ u ∉ σ.releaseQueue)) : + SchedValidA { σ with readyQueues := f, schedulerAction := .chooseNewThread } := + ⟨fun d p u hu => (hm d p u hu).elim (h.rq d p u) id, fun e => (nomatch e), h.blk, h.rls⟩ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- A TCB word other than the four scheduler-link words is outside `MWrA`. -/ +theorem tcbWordNotMWrA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRN st Z m κ s σ) {t K : U64} (ht : t ∈ Z.T) (hb : K.val < 4096) + (h1 : K.val ≠ 3464) (h2 : K.val ≠ 3480) (h3 : K.val ≠ 3488) (h4 : K.val ≠ 3592) : + ¬ MWrA (st := st) Z m σ s (wa t K) := by + have hv := field_val (H.core.tcbs t ht) hb + have hks : (wa t K).val < (L.ksb s).val ∨ (L.ksb s).val + ksSizeA ≤ (wa t K).val := by + have h := H.tcb_ks ht + rw [hv] + omega + have hsc : ¬ ScFootA Z.C m σ (wa t K) := by + intro h + have hn := H.scp.th_out h ((H.thT t).2 ht) + simp only [ObjKindA.size] at hn + rw [hv] at hn + omega + refine not_mwrA hsc (fun u hu => ?_) hks + have field {k : U64} (hk : k.val < 4096) (hne : K.val ≠ k.val) : + wa t K ≠ wa u k := field_ne (H.core.tcbs t ht) (H.core.tcbs u hu) hb hk (fun _ => hne) + exact ⟨field (k := 3464#u64) (by decide) h1, field (k := 3480#u64) (by decide) h2, + field (k := 3488#u64) (by decide) h3, field (k := 3592#u64) (by decide) h4⟩ + +open Sched in +/-- `domainWord_zeroA` (`P3/DomainSetDispatchA`, private there), restated: rewriting the zero domain changes no word +and no spec object. -/ +theorem domainWordZeroA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRA st Z m κ s σ) (hs : StEqA Z.T (L.mem s) σ) {t : U64} (ht : t ∈ Z.T) : + obj.Tcb.set_domain st t s 0#u64 ⦃ s' => + threadSetDomain (Z := Unit) (specPtr t) 0 σ = ({((), σ)}, False) ∧ + WritesIn st s s' (fun _ => False) ∧ SchedSRA st Z m κ s' σ ∧ StEqA Z.T (L.mem s') σ ⦄ := by + obtain ⟨x, hk, hd, hw, -⟩ := H.rest.prio t ht + have hspec : threadSetDomain (Z := Unit) (specPtr t) 0 σ = ({((), σ)}, False) := by + unfold threadSetDomain + rw [Notif.threadSet_eq _ hk] + have e : Notif.updK σ (specPtr t) (.tcb { x with tcbDomain := 0 }) = σ := by + rw [← hd] + unfold Notif.updK + rw [show ({ x with tcbDomain := x.tcbDomain } : Tcb) = x from rfl, ← hk, Function.update_eq_self] + rw [e] + have hal : Aligned (domA t) := field_aligned (H.core.tcbs t ht) (by decide) (by decide) + have hf : WritesIn st s (L.wr s (domA t) 0#u64) (fun _ => False) := by + refine ⟨L.ksb_wr _ _ _, L.cpu_wr _ _ _, fun b hb _ => ?_⟩ + rw [L.mem_wr _ _ _ _ hal hb] + split + · next h => rw [← h, hw] + · rfl + unfold obj.Tcb.set_domain + have off : layout.tcb.DOMAIN = ok 3432#u64 := eq_of_spec_fd Fl4ProofsA64.P0.Consts.layout.tcb.DOMAIN.spec + rw [off] + simp only [bind_ok, lift, L.write_eq] + refine (WP.spec_ok _).2 ⟨hspec, hf, ?_, ?_⟩ + · exact H.stableR hf (fun _ h => h.elim) + ⟨SFixA.refl _, rfl, rfl, rfl, fun _ _ _ _ => rfl, fun _ x h => ⟨x, h, rfl, rfl, rfl⟩, + fun _ _ _ _ => rfl, fun _ x h => ⟨x, h⟩, fun _ x h => ⟨x, h, rfl⟩⟩ H.run H.rd + · exact hs.keep (fun u hu => hf.mem_eq (by + rw [stA_eq] + exact field_aligned (H.core.tcbs u hu) (by decide) (by decide)) id) (fun _ x h => ⟨x, h, rfl⟩) + +/-- **`set_domain` of a local thread other than the current one** (module docstring). -/ +theorem setDomain_otherA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} {n : Nat} + (E : YieldQueueRelA (st := st) Z m κ s σ) (SI : SInvXA σ m n) (SD : SIdleA σ m n) (V : SchedValidA σ) + {t : U64} (ht : t ∈ Z.T) (hne : t ≠ L.curW s) (hon : L.mem s (affA t) = L.cpu s) : + sched.set_domain st s t 0#u64 ⦃ s' => ∃ σ' Q', setDomain (Z := Unit) (specPtr t) 0 σ = ({((), σ')}, False) ∧ + RqOnlyA σ σ' ∧ YieldQueueRelA (st := st) { Z with Q := Q' } m κ s' σ' ∧ MFrA (st := st) Z m s s' σ σ' ∧ + SInvXA σ' m n ∧ SchedValidA σ' ⦄ := by + have curE : σ.curThread = specPtr (L.curW s) := E.sched.sch.curThread + have hne' : specPtr t ≠ σ.curThread := fun e => hne (specPtr_inj (e.trans curE)) + obtain ⟨x0, hk0, hd0, -, -⟩ := E.sched.rest.prio t ht + -- the tail: no reschedule + have fin {Z' : XSetsA} {s3 : S} {σ3 : State Unit} (H3 : SchedSRA st Z' m κ s3 σ3) + (hc3 : σ3.curThread = σ.curThread) : + (do + let ct ← sched.cur_thread st s3 + let same ← obj.Tcb.Insts.CoreCmpPartialEqTcb.eq t ct + if same then sched.reschedule_required st s3 else ok s3) = ok s3 := by + simp only [cur_thread_ok, bind_ok, obj.Tcb.Insts.CoreCmpPartialEqTcb.eq, decide_eq_true_eq] + rw [if_neg] + intro e + exact hne' (by rw [← hc3, H3.sch.curThread, e]) + unfold sched.set_domain + refine WP.spec_bind (yieldDequeueA E ht hon) ?_ + rintro s1 ⟨σ1, Q1, hdeq, hk1, -, E1, F1⟩ + have R1 := rq_eqA hk0 hd0 hdeq + have O1 : RqOnlyA σ σ1 := by rw [R1]; exact RqOnlyA.setRq _ _ _ + have mem1 : ∀ d p u, u ∈ σ1.readyQueues d p → u ∈ σ.readyQueues d p := fun d p u hu => by + rw [R1] at hu + rcases setRq_memA hu with h | ⟨rfl, rfl, h⟩ + · exact h + · exact (List.mem_filter.1 h).1 + have SI1 : SInvXA σ1 m n := by + rw [O1]; exact SI.rqA _ fun d p u hu => Or.inl (mem1 d p u hu) + have V1 : SchedValidA σ1 := by + rw [O1]; exact V.rqA _ fun d p u hu => Or.inl (mem1 d p u hu) + refine WP.spec_bind (domainWordZeroA E1.sched E1.states ht) ?_ + rintro s2 ⟨hword, w2, H2, hs2⟩ + have M2 := yieldQueueFrameA E1.sched E1.states E1.mcp E1.links rfl (w2.mono fun _ h => h.elim) + have E2 : YieldQueueRelA (st := st) { Z with Q := Q1 } m κ s2 σ1 := ⟨H2, hs2, M2.2.1, M2.2.2⟩ + have F2 : MFrA (st := st) { Z with Q := Q1 } m s1 s2 σ1 σ1 := + ⟨w2.mono fun _ h => h.elim, SKeepA.refl _, SSzA.refl _⟩ + have F12 : MFrA (st := st) Z m s s2 σ σ1 := F1.trans F2 rfl rfl + have hon2 : L.mem s2 (affA t) = L.cpu s2 := yieldAffKeepA E.sched H2 ht ht hon + have hc1 : σ1.curThread = σ.curThread := by rw [O1] + rw [is_schedulable_eqA, bind_ok, schedulable_eqA H2.toSchedSRN ht (H2.relOn ht hon2) hs2] + have spec : setDomain (Z := Unit) (specPtr t) 0 σ = + (do + Fl4Spec.NonDet.when (schedulable (specPtr t) σ1 = true) + (tcbSchedAction (Z := Unit) tcbSchedEnqueue (specPtr t)) + Fl4Spec.NonDet.when (specPtr t = σ.curThread) (rescheduleRequired (Z := Unit))) σ1 := by + unfold setDomain + rw [nd_gets_bindSc, nd_bind_oneSc hdeq, nd_bind_oneSc hword, nd_gets_bindSc] + by_cases h : schedulable (specPtr t) σ1 = true + · rw [if_pos h, spec] + obtain ⟨hrun, hnr⟩ := schedulable_partsA h + obtain ⟨hact, -, xr, hxr, hxrr⟩ := schedulable_actA h + refine WP.spec_bind (yieldEnqueueA E2 ht hon2 hrun hnr) ?_ + rintro s3 ⟨σ3, Q3, henq, hk3, -, E3, F3⟩ + have R3 := rq_eqA (hk1 ▸ hk0) hd0 henq + have O3 : RqOnlyA σ1 σ3 := by rw [R3]; exact RqOnlyA.setRq _ _ _ + have mem3 : ∀ d p u, u ∈ σ3.readyQueues d p → u ∈ σ1.readyQueues d p ∨ u = specPtr t := fun d p u hu => by + rw [R3] at hu + rcases setRq_memA hu with h | ⟨rfl, rfl, h⟩ + · exact Or.inl h + · unfold tcbSchedEnqueue at h + split at h + · rcases List.mem_cons.1 h with e | e + · exact Or.inr e + · exact Or.inl e + · exact Or.inl h + have hidle : ∀ c < n, specPtr t ≠ m.idleThreadOf c := fun c hc e => by + obtain ⟨y, hy, hys⟩ := SD.f3 c hc + rw [← e, ← hk1, hxr] at hy + cases hy + rw [hys] at hxrr + cases hxrr + have SI3 : SInvXA σ3 m n := by + rw [O3] + exact SI1.rqA _ fun d p u hu => (mem3 d p u hu).imp id fun e => by + subst e; exact ⟨by rw [hc1]; exact hne', hidle, xr, hxr, hxrr⟩ + have V3 : SchedValidA σ3 := by + rw [O3] + exact V1.rqA _ fun d p u hu => (mem3 d p u hu).imp id fun e => by + subst e; exact ⟨hact, hnr⟩ + have hc3 : σ3.curThread = σ.curThread := by rw [O3]; exact hc1 + rw [fin E3.sched hc3] + refine (WP.spec_ok _).2 ⟨σ3, Q3, ?_, O1.trans O3, E3, F12.trans F3 rfl rfl, SI3, V3⟩ + rw [Fl4Spec.NonDet.when, if_pos h, nd_bind_oneSc henq, Fl4Spec.NonDet.when, if_neg hne'] + rfl + · rw [if_neg h, bind_ok, spec] + rw [fin E2.sched hc1] + refine (WP.spec_ok _).2 ⟨σ1, Q1, ?_, O1, E2, F12, SI1, V1⟩ + rw [Fl4Spec.NonDet.when, if_neg h] + rw [nd_bind_oneSc (show Fl4Spec.NonDet.pure () σ1 = ({((), σ1)}, False) from rfl)] + rw [Fl4Spec.NonDet.when, if_neg hne'] + rfl + +/-- **`set_domain` of the current thread** (module docstring): dequeue, the zero domain word, enqueue when +schedulable, then `reschedule_required`. -/ +theorem setDomain_selfA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} {n : Nat} + (E : YieldQueueRelA (st := st) Z m κ s σ) (SI : SInvXA σ m n) (SD : SIdleA σ m n) (V : SchedValidA σ) + {t : U64} (ht : t ∈ Z.T) (hself : t = L.curW s) (hon : L.mem s (affA t) = L.cpu s) + (hact : σ.schedulerAction = .resumeCurThread) : + sched.set_domain st s t 0#u64 ⦃ s' => ∃ σ3 Q', + setDomain (Z := Unit) (specPtr t) 0 σ = ({((), { σ3 with schedulerAction := .chooseNewThread })}, False) ∧ + RqOnlyA σ σ3 ∧ + YieldQueueRelA (st := st) { Z with Q := Q' } m κ s' { σ3 with schedulerAction := .chooseNewThread } ∧ + MFrA (st := st) Z m s s' σ { σ3 with schedulerAction := .chooseNewThread } ∧ + SInvXA { σ3 with schedulerAction := .chooseNewThread } m n ∧ + SchedValidA { σ3 with schedulerAction := .chooseNewThread } ⦄ := by + have curE : σ.curThread = specPtr (L.curW s) := E.sched.sch.curThread + have hcur : specPtr t = σ.curThread := by rw [curE, hself] + obtain ⟨x0, hk0, hd0, -, -⟩ := E.sched.rest.prio t ht + -- the tail: `reschedule_required` + have fin {Z' : XSetsA} {s3 : S} {σ3 : State Unit} + (E3 : YieldQueueRelA (st := st) Z' m κ s3 σ3) (O3 : RqOnlyA σ σ3) : + (do + let ct ← sched.cur_thread st s3 + let same ← obj.Tcb.Insts.CoreCmpPartialEqTcb.eq t ct + if same then sched.reschedule_required st s3 else ok s3) ⦃ s4 => ∃ Q4, + rescheduleRequired (Z := Unit) σ3 = ({((), { σ3 with schedulerAction := .chooseNewThread })}, False) ∧ + YieldQueueRelA (st := st) { Z' with Q := Q4 } m κ s4 { σ3 with schedulerAction := .chooseNewThread } ∧ + MFrA (st := st) Z' m s3 s4 σ3 { σ3 with schedulerAction := .chooseNewThread } ⦄ := by + have hc3 : σ3.curThread = σ.curThread := by rw [O3] + have e : t = L.curW s3 := specPtr_inj (hcur.trans (hc3.symm.trans E3.sched.sch.curThread)) + have hns : ∀ u, σ3.schedulerAction ≠ .switchThread u := fun u h => by + have h' : σ.schedulerAction = .switchThread u := by rw [O3] at h; exact h + rw [hact] at h' + cases h' + have hr := rescheduleRequired_noswitchA hns + simp only [cur_thread_ok, bind_ok, obj.Tcb.Insts.CoreCmpPartialEqTcb.eq, decide_eq_true_eq] + rw [if_pos e] + refine WP.spec_mono (yieldRescheduleA E3) ?_ + rintro s4 ⟨σ4, Q4, h4, -, -, E4, F4⟩ + have e4 : σ4 = { σ3 with schedulerAction := .chooseNewThread } := + (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst (h4.symm.trans hr)))).2 + subst e4 + exact ⟨Q4, hr, E4, F4⟩ + unfold sched.set_domain + refine WP.spec_bind (yieldDequeueA E ht hon) ?_ + rintro s1 ⟨σ1, Q1, hdeq, hk1, -, E1, F1⟩ + have R1 := rq_eqA hk0 hd0 hdeq + have O1 : RqOnlyA σ σ1 := by rw [R1]; exact RqOnlyA.setRq _ _ _ + have mem1 : ∀ d p u, u ∈ σ1.readyQueues d p → u ∈ σ.readyQueues d p := fun d p u hu => by + rw [R1] at hu + rcases setRq_memA hu with h | ⟨rfl, rfl, h⟩ + · exact h + · exact (List.mem_filter.1 h).1 + refine WP.spec_bind (domainWordZeroA E1.sched E1.states ht) ?_ + rintro s2 ⟨hword, w2, H2, hs2⟩ + have M2 := yieldQueueFrameA E1.sched E1.states E1.mcp E1.links rfl (w2.mono fun _ h => h.elim) + have E2 : YieldQueueRelA (st := st) { Z with Q := Q1 } m κ s2 σ1 := ⟨H2, hs2, M2.2.1, M2.2.2⟩ + have F2 : MFrA (st := st) { Z with Q := Q1 } m s1 s2 σ1 σ1 := + ⟨w2.mono fun _ h => h.elim, SKeepA.refl _, SSzA.refl _⟩ + have F12 : MFrA (st := st) Z m s s2 σ σ1 := F1.trans F2 rfl rfl + have hon2 : L.mem s2 (affA t) = L.cpu s2 := yieldAffKeepA E.sched H2 ht ht hon + rw [is_schedulable_eqA, bind_ok, schedulable_eqA H2.toSchedSRN ht (H2.relOn ht hon2) hs2] + have spec : setDomain (Z := Unit) (specPtr t) 0 σ = + (do + Fl4Spec.NonDet.when (schedulable (specPtr t) σ1 = true) + (tcbSchedAction (Z := Unit) tcbSchedEnqueue (specPtr t)) + Fl4Spec.NonDet.when (specPtr t = σ.curThread) (rescheduleRequired (Z := Unit))) σ1 := by + unfold setDomain + rw [nd_gets_bindSc, nd_bind_oneSc hdeq, nd_bind_oneSc hword, nd_gets_bindSc] + -- the spec state after the queue steps, with the new action + have out {σ3 : State Unit} (O3 : RqOnlyA σ σ3) + (mem : ∀ d p u, u ∈ σ3.readyQueues d p → u ∈ σ.readyQueues d p ∨ + (u = specPtr t ∧ (∀ c < n, u ≠ m.idleThreadOf c) ∧ (∃ x, σ.kheap u = some (.tcb x) ∧ runnable x.tcbState = true) ∧ + ActTcbA σ u ∧ u ∉ σ.releaseQueue)) : + SInvXA { σ3 with schedulerAction := .chooseNewThread } m n ∧ + SchedValidA { σ3 with schedulerAction := .chooseNewThread } := by + rw [O3] + exact ⟨SI.chooseRqA _ fun d p u hu => (mem d p u hu).imp id fun h => ⟨h.2.1, h.2.2.1⟩, + V.chooseRqA _ fun d p u hu => (mem d p u hu).imp id fun h => ⟨h.2.2.2.1, h.2.2.2.2⟩⟩ + by_cases h : schedulable (specPtr t) σ1 = true + · rw [if_pos h] + obtain ⟨hrun, hnr⟩ := schedulable_partsA h + obtain ⟨hact1, -, xr, hxr, hxrr⟩ := schedulable_actA h + refine WP.spec_bind (yieldEnqueueA E2 ht hon2 hrun hnr) ?_ + rintro s3 ⟨σ3, Q3, henq, hk3, -, E3, F3⟩ + have R3 := rq_eqA (hk1 ▸ hk0) hd0 henq + have O3 : RqOnlyA σ1 σ3 := by rw [R3]; exact RqOnlyA.setRq _ _ _ + have mem3 : ∀ d p u, u ∈ σ3.readyQueues d p → u ∈ σ1.readyQueues d p ∨ u = specPtr t := fun d p u hu => by + rw [R3] at hu + rcases setRq_memA hu with h | ⟨rfl, rfl, h⟩ + · exact Or.inl h + · unfold tcbSchedEnqueue at h + split at h + · rcases List.mem_cons.1 h with e | e + · exact Or.inr e + · exact Or.inl e + · exact Or.inl h + have hidle : ∀ c < n, specPtr t ≠ m.idleThreadOf c := fun c hc e => by + obtain ⟨y, hy, hys⟩ := SD.f3 c hc + rw [← e, ← hk1, hxr] at hy + cases hy + rw [hys] at hxrr + cases hxrr + have V1 : SchedValidA σ1 := by + rw [O1]; exact V.rqA _ fun d p u hu => Or.inl (mem1 d p u hu) + have act0 : ActTcbA σ (specPtr t) := by rw [O1] at hact1; exact hact1 + have nr0 : specPtr t ∉ σ.releaseQueue := by rw [O1] at hnr; exact hnr + have run0 : ∃ x, σ.kheap (specPtr t) = some (.tcb x) ∧ runnable x.tcbState = true := + ⟨xr, by rw [← hk1]; exact hxr, hxrr⟩ + obtain ⟨SI4, V4⟩ := out (O1.trans O3) fun d p u hu => (mem3 d p u hu).imp (mem1 d p u) fun e => by + subst e; exact ⟨rfl, hidle, run0, act0, nr0⟩ + refine WP.spec_mono (fin E3 (O1.trans O3)) ?_ + rintro s4 ⟨Q4, hr, E4, F4⟩ + refine ⟨σ3, Q4, ?_, O1.trans O3, E4, (F12.trans F3 rfl rfl).trans F4 rfl rfl, SI4, V4⟩ + rw [spec, Fl4Spec.NonDet.when, if_pos h, nd_bind_oneSc henq, Fl4Spec.NonDet.when, if_pos hcur] + exact hr + · rw [if_neg h, bind_ok] + obtain ⟨SI4, V4⟩ := out O1 fun d p u hu => Or.inl (mem1 d p u hu) + refine WP.spec_mono (fin E2 O1) ?_ + rintro s4 ⟨Q4, hr, E4, F4⟩ + refine ⟨σ1, Q4, ?_, O1, E4, F12.trans F4 rfl rfl, SI4, V4⟩ + rw [spec, Fl4Spec.NonDet.when, if_neg h] + rw [nd_bind_oneSc (show Fl4Spec.NonDet.pure () σ1 = ({((), σ1)}, False) from rfl)] + rw [Fl4Spec.NonDet.when, if_pos hcur] + exact hr + +end + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s : S} {a : A} {σ : State Unit} {m : McState} + +set_option maxHeartbeats 2000000 in +/-- **`set_domain` of a local thread at the record** (module docstring). -/ +theorem JStage3A.setDomain_stepA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {t : U64} (ht : t ∈ Z.T) (hon : L.mem s (affA t) = L.cpu s) (hact : σ.schedulerAction = .resumeCurThread) : + sched.set_domain st s t 0#u64 ⦃ s' => ∃ σ', setDomain (Z := Unit) (specPtr t) 0 σ = ({((), σ')}, False) ∧ + RqActA σ σ' ∧ + (SysL st H Z.Cs OS ia ie s' → SymL st H Z.Cs s' → Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s' → + J7A st (J7R2A st ar nA) Z H OS s' σ' m → + ∃ Q, JStage3A st AR { Z with Q := Q } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s' a σ' m) ⦄ := by + have E0 : YieldQueueRelA (st := st) Z m κ s σ := + ⟨J.sr, fun _ hu _ hx => (J.ep.at_tcb hu hx).st, J.mcp, J.yieldRel⟩ + have htH : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + have core : sched.set_domain st s t 0#u64 ⦃ s' => ∃ σ' Q', + setDomain (Z := Unit) (specPtr t) 0 σ = ({((), σ')}, False) ∧ SpecSchedA σ σ' ∧ RqActA σ σ' ∧ σ'.kheap = σ.kheap ∧ + σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory ∧ σ'.idleThread = σ.idleThread ∧ + YieldQueueRelA (st := st) { Z with Q := Q' } m κ s' σ' ∧ MFrA (st := st) Z m s s' σ σ' ∧ + SInvXA σ' m n ∧ SchedValidA σ' ⦄ := by + by_cases hne : t = L.curW s + · refine WP.spec_mono (setDomain_selfA E0 J.sinv J.sidle J.schedValid ht hne hon hact) ?_ + rintro s' ⟨σ3, Q', hD, O, E', F, SI', V'⟩ + refine ⟨_, Q', hD, ?_, ⟨σ3.readyQueues, Or.inr (by rw [O])⟩, by rw [O], by rw [O], by rw [O], E', F, SI', V'⟩ + rw [O] + exact { kh := fun _ => Or.inl rfl, irqS := rfl, irqN := rfl, asid := rfl, vcur := rfl, cdt := rfl, orig := rfl } + · refine WP.spec_mono (setDomain_otherA E0 J.sinv J.sidle J.schedValid ht hne hon) ?_ + rintro s' ⟨σ', Q', hD, O, E', F, SI', V'⟩ + exact ⟨σ', Q', hD, O.spec, ⟨σ'.readyQueues, Or.inl O⟩, O.kheap, by rw [O], by rw [O], E', F, SI', V'⟩ + refine WP.spec_mono (WP.spec_and core (sched.set_domain.blkF s t 0#u64 (BXI.blk J.sys.ent.bxi) htH)) ?_ + rintro s' ⟨⟨σ', Q', hD, O, hra, hk, hum, hidle, E', F, SI', V'⟩, blk', -⟩ + refine ⟨σ', hD, hra, fun sysl symL userOK j7 => ⟨Q', ?_⟩⟩ + obtain ⟨ntfn, ep, reply, -, -, -⟩ := mcsStepFramesA J.iplaced J.ipcPlace J.sr.toSchedSRN F hum + (fun q y k h => ⟨y, by rw [hk]; exact h, rfl, rfl⟩) J.ntfn J.ep J.reply J.mcp J.yieldRel + have tw : ∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → K.val ≠ 3464 → K.val ≠ 3480 → + K.val ≠ 3488 → K.val ≠ 3592 → L.mem s' (wa u K) = L.mem s (wa u K) := fun u hu K h0 h8 h1 h2 h3 h4 => + F.wr.mem_eq (field_aligned (J.sr.core.tcbs u hu) h0 h8) (tcbWordNotMWrA J.sr.toSchedSRN hu h0 h1 h2 h3 h4) + have bufCover : BufCover st Z.T Bf s' σ' := by + intro u hu z b sb v hz hb hl + have hib : IbSame (L := L) s s' u := by + refine ⟨?_, ?_, ?_⟩ + · rw [ibWordA_eq]; exact tw u hu _ (by simp) (by simp) (by simp) (by simp) (by simp) (by simp) + · rw [sbWordA_eq]; exact tw u hu _ (by simp) (by simp) (by simp) (by simp) (by simp) (by simp) + · rw [sbWordA8_eq]; exact tw u hu _ (by simp) (by simp) (by simp) (by simp) (by simp) (by simp) + rw [lookup_ipc_buffer_sameA hib] at hl + exact J.bufCover u hu z b sb v (by rw [← hk]; exact hz) hb hl + have bufAl : UtBufAlA Z.T σ' := fun u hu y hy => J.bufAl u hu y (by rw [← hk]; exact hy) + have idle : IdleNoFaultA m σ' := fun c hc => by + obtain ⟨y, hy, hf⟩ := J.idleNoFault c hc + exact ⟨y, by rw [hk]; exact hy, hf⟩ + have rv : RunVcpuInv st Z.T s' := J.runVcpu.keep + (fun u hu => by + rw [Fl4ProofsA64.P1.stA_eq] + exact tw u hu 3328#u64 (by simp) (by simp) (by simp) (by simp) (by simp) (by simp)) + (fun u hu => by + unfold runVcpuA + exact tw u hu 3672#u64 (by simp) (by simp) (by simp) (by simp) (by simp) (by simp)) + have blkQ : BlkQueuedA Z.T E N (L.mem s') σ' := J.blkQ.keepA + (fun u hu => by + unfold Notif.tBlockA + exact tw u hu 3336#u64 (by simp) (by simp) (by simp) (by simp) (by simp) (by simp)) + (fun u _ x' h => Or.inr ⟨x', by rw [← hk]; exact h, rfl⟩) + (fun q z h => by rw [hk]; exact h) (fun q z h => by rw [hk]; exact h) + exact J.of_stepFinA (Q := Q') (Rl := Z.R) (curOf := Z.curOf) (curScOf := Z.curScOf) + (F.wr.mono fun _ h => Or.inl (Or.inl h)) O (SSzA.of_kheap hk) + (QKeepA.of_kheap hk (by obtain ⟨f, h | h⟩ := hra <;> (subst h; exact ⟨rfl, rfl, rfl, rfl⟩))) hum (fun _ _ => rfl) + (BlkOK.bxi blk') sysl symL userOK j7 E'.sched ep ntfn reply bufCover bufAl idle E'.mcp E'.links SI' + (J.sidle.congr hk hidle + (by obtain ⟨f, h | h⟩ := hra <;> (subst h; exact Nat.le_refl _))) rv blkQ V' + +set_option maxHeartbeats 2000000 in +/-- **`DomainSetSet` at a syscall entry**, the caller included (module docstring). -/ +theorem JSyscallA.setDomainA (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + {slot len buf x0 t : U64} {blk call don : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Domain) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hl : len ≠ 0#u64) (hd : msg.syscall_arg st s (L.curW s) buf 0#u64 = ok 0#u64) + (he : Array.index_usize extra 0#usize = ok x0) (hx : x0 ≠ 0#u64) (hx0 : x0 ∈ OS) + (hcx : capA st s x0 = .Thread t) (hon : L.mem s (affA t) = L.cpu s) + (hstall : smp.remote_tcb_stall st ar s a t = ok (s, a)) : + ∃ σ1 σ2 s2, + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + invokeDomain (Z := Unit) (.invokeDomainSet (specPtr t) 0) σ1 = ({((), σ2)}, False) ∧ + syscall.decode_invocation st ar s a fl4_abi.label.DomainSetSet len slot (capA st s slot) blk call don false buf + extra = ok (.Ok (), s2, a) ∧ + (∃ xc, σ.kheap σ.curThread = some (.tcb xc) ∧ RqActA (stUpdA σ σ.curThread xc .restart) σ2) ∧ + ∃ Q, JStage3A st AR { Z with Q := Q } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s2 a σ2 m := by + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hslot hcov J.actW J.running J.cur + obtain ⟨xc, hxc, hst, hsp, J1, -, -, -⟩ := J.toJStage3A.of_restartCurA + RS.pl.choose_spec.choose_spec.choose_spec.choose_spec RS.sym RS.run J.toJStage3EntryA.curNotVmA RS.cur RS.vs + RS.vc + have ht : t ∈ Z.T := J.tcbs ▸ (J.toJStage3A.slotCapTy hx0).1.2.2.2.2.2 t hcx + have hon1 := yieldAffKeepA J.sr J1.sr ht ht hon + obtain ⟨s2, e2, σ2, hD, hra, K⟩ := spec_existsA (J1.setDomain_stepA ht hon1 J.act) + have hb : ¬ (0#u64 : U64) ≥ layout.NUM_DOMAINS := by + rw [layout.NUM_DOMAINS, fl4_abi.config.NUM_DOMAINS] + decide + have hextra : obj.Slot.cap st x0 s = ok (.Thread t) := by rw [capA_eq, hcx] + have hdec : syscall.decode_invocation st ar s a fl4_abi.label.DomainSetSet len slot (capA st s slot) blk call don + false buf extra = ok (.Ok (), s2, a) := by + rw [hcap, decodeDomain_programA, if_neg Bool.false_ne_true, domainSet_acceptA hl hd hb he hx hextra] + simp only [hstall, bind_ok, uncurry_apply_pair, hst, e2] + obtain ⟨-, -, sysl, symL, -, userOK, j7⟩ := J.decodeFinA hA hM hPl hfm hslot hcov + (rfl : capA st s slot = capA st s slot) hext hSC (lbl := fl4_abi.label.DomainSetSet) + (by rw [hcap]; simp [DecArmS1aEnt, DecArmQH, DecArmQZ2, DecArmKQ2, DecArmKQ, capFam]) + (fun _ h => by rw [hcap] at h; cases h) (fun _ h => by rw [hcap] at h; cases h) hdec σ2 m + obtain ⟨Q, J2⟩ := K sysl symL userOK j7 + exact ⟨_, σ2, s2, hsp, (invokeDomainSet_sameA J1.sr.rest.curDom.1).trans hD, hdec, ⟨xc, hxc, hra⟩, Q, J2⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3LiftFlushA.lean b/proofs/Fl4ProofsA64/P3/Stage3LiftFlushA.lean new file mode 100644 index 000000000..96d848f93 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3LiftFlushA.lean @@ -0,0 +1,501 @@ +/- +P3 (aarch64, A64-P3 Lift 1): **the cache-maintenance (flush) arms at record level** (VSpace-root, Stage-2 root and +frame flushes), from `JSyscallA` to `JStage3A`. + +The closed `JEntA` tops (`decInv_vspace_jA`, `decInv_s2Vspace_jA`, `decInv_frame_jA`) are used at the arch relation +`fun a _ => AR a Z.hw` (`ArchKeepsA.snap`), so their `∃ Z'` post still gives `AR a' Z.hw`. FL4's accepted flush writes +only the caller's restart (`FlushFootA`); every accepted decode is a flush invocation (`FlushInvA`), whose perform +changes only the machine state and keeps `underlyingMemory` (`UmA`). So the record at the end is +`JSyscallA.restartMachA`'s. + +Named premises: [P] `ArchKeepsA`. +-/ +import Fl4ProofsA64.P3.Stage3LiftMachA +import Fl4ProofsA64.P3.Stage3LiftBodyA +import Fl4ProofsA64.P3.Stage3VsFootFlushA +import Fl4ProofsA64.P3.Stage3VsFootA +import Fl4ProofsA64.P3.Stage3OffAsEA +import Fl4ProofsA64.P3.FrFlushA +import Fl4ProofsA64.P3.VsS2FlushA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Vcpu Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +/-! ## The accepted invocations and their performs -/ + +/-- Every `.ok` result of `m` satisfies `P`. -/ +def OkAllA {σ ε α : Type} (P : α → Prop) (m : NonDetE σ ε α) : Prop := + ∀ s x s', (Except.ok x, s') ∈ (m.run s).1 → P x + +section okall +variable {σ ε α β : Type} {P : α → Prop} + +theorem OkAllA.pure {x : α} (h : P x) : OkAllA P (Pure.pure x : NonDetE σ ε α) := fun s y s' hy => by + rw [ExceptT.run_pure] at hy + have e := Set.mem_singleton_iff.1 hy + injection e with e _; injection e with e; rw [e]; exact h + +theorem OkAllA.returnOk {x : α} (h : P x) : OkAllA P (NonDetE.returnOk x : NonDetE σ ε α) := OkAllA.pure h + +theorem OkAllA.throwE (e : ε) : OkAllA P (NonDetE.throwError e : NonDetE σ ε α) := fun s y s' hy => by + have e' := Set.mem_singleton_iff.1 hy + injection e' with e' _; cases e' + +theorem OkAllA.fail : OkAllA P (NonDetE.liftE NonDet.fail : NonDetE σ ε α) := fun s y s' hy => by + unfold NonDetE.liftE at hy + rw [ExceptT.run_lift] at hy + obtain ⟨x, hx, -⟩ := hy + exact (Set.notMem_empty x hx).elim + +theorem OkAllA.bind {m : NonDetE σ ε β} {f : β → NonDetE σ ε α} (h : ∀ x, OkAllA P (f x)) : OkAllA P (m >>= f) := + fun s y s' hy => by + rw [ExceptT.run_bind] at hy + obtain ⟨⟨r, s1⟩, -, hy⟩ := hy + cases r with + | ok x => exact h x s1 y s' hy + | error e => + have e' := Set.mem_singleton_iff.1 hy + injection e' with e' _; cases e' + +theorem OkAllA.ite {c : Prop} [Decidable c] {x y : NonDetE σ ε α} (hx : OkAllA P x) (hy : OkAllA P y) : + OkAllA P (if c then x else y) := by split <;> assumption + +theorem OkAllA.liftME {f : β → α} {m : NonDetE σ ε β} {Q : β → Prop} (hm : OkAllA Q m) (h : ∀ x, Q x → P (f x)) : + OkAllA P (NonDetE.liftME f m) := fun s y s' hy => by + unfold NonDetE.liftME at hy + rw [ExceptT.run_bind] at hy + obtain ⟨⟨r, s1⟩, hr, hy⟩ := hy + cases r with + | ok x => + have e' := Set.mem_singleton_iff.1 hy + injection e' with e' _; injection e' with e'; rw [e']; exact h x (hm s x s1 hr) + | error e => + have e' := Set.mem_singleton_iff.1 hy + injection e' with e' _; cases e' + +end okall + +/-- The flush (cache maintenance) invocations. -/ +def FlushInvA : Fl4ArchInvocation → Prop + | .spec (.invokeVSpace _) => True + | .spec (.invokePage (.pageFlush ..)) => True + | .extVSpace (.flush _) => True + | _ => False + +/-- A step of the spec state changing only the machine state, `underlyingMemory` and `lastMachineTime` kept. -/ +def UmA (σ σ' : Fl4SpecA64.State Unit) : Prop := + ∃ ms, σ' = { σ with machineState := ms } ∧ ms.underlyingMemory = σ.machineState.underlyingMemory ∧ + ms.lastMachineTime = σ.machineState.lastMachineTime + +theorem umA_refl (σ : Fl4SpecA64.State Unit) : UmA σ σ := ⟨σ.machineState, rfl, rfl, rfl⟩ + +theorem umA_trans : ∀ a b c : Fl4SpecA64.State Unit, UmA a b → UmA b c → UmA a c := by + rintro a b c ⟨m1, rfl, h1, l1⟩ ⟨m2, rfl, h2, l2⟩ + exact ⟨m2, rfl, h2.trans h1, l2.trans l1⟩ + +/-- A machine operation keeping `underlyingMemory` and `lastMachineTime`. -/ +def MUmA (m m' : Fl4SpecA64.MachineState) : Prop := + m'.underlyingMemory = m.underlyingMemory ∧ m'.lastMachineTime = m.lastMachineTime + +theorem mumA_trans : ∀ a b c : Fl4SpecA64.MachineState, MUmA a b → MUmA b c → MUmA a c := + fun _ _ _ h1 h2 => ⟨Eq.trans h2.1 h1.1, Eq.trans h2.2 h1.2⟩ + +theorem mumA_refl (m : Fl4SpecA64.MachineState) : MUmA m m := ⟨rfl, rfl⟩ + +theorem machineOpLift_umA (g : MachineRestMonad Unit) : NdOkA MUmA (machineOpLift g) := by + unfold machineOpLift machineRestLift + refine NdOkA.bind mumA_trans (NdOkA.gets mumA_refl _) fun mr => + NdOkA.bind mumA_trans (NdOkA.selectF mumA_refl ((ignoreFailure_okA g) mr).1 + ((ignoreFailure_okA g) mr).2.1) fun x => ?_ + rcases x with ⟨r, mr'⟩ + exact NdOkA.bind mumA_trans (NdOkA.modify (Q := MUmA) fun _ => ⟨rfl, rfl⟩) fun _ => NdOkA.pure mumA_refl r + +theorem doFlush_umA (t : FlushType) (v e p : Fl4SpecA64.Word) : NdOkA MUmA (doFlush t v e p) := by + cases t + · exact machineOpLift_umA _ + · exact machineOpLift_umA _ + · exact machineOpLift_umA _ + · exact NdOkA.bind mumA_trans (machineOpLift_umA _) fun _ => NdOkA.bind mumA_trans (machineOpLift_umA _) fun _ => + NdOkA.bind mumA_trans (machineOpLift_umA _) fun _ => NdOkA.bind mumA_trans (machineOpLift_umA _) fun _ => + machineOpLift_umA _ + +theorem doMachineOp_umA {α : Type} {mop : NonDet Fl4SpecA64.MachineState α} (h : NdOkA MUmA mop) : + NdOkA UmA (doMachineOp (Z := Unit) mop) := by + intro σ + have h0 := h σ.machineState + obtain ⟨hf, ⟨y, hy⟩, hq⟩ := h0 + have run : doMachineOp (Z := Unit) mop σ = + ({r | ∃ x ∈ (mop σ.machineState).1, r = (x.1, { σ with machineState := x.2 })}, + (mop σ.machineState).2) := by + unfold doMachineOp + refine Prod.ext ?_ ?_ + · ext r + constructor + · rintro ⟨a, ha, b, ⟨hb, hbs⟩, c, hc, hr⟩ + cases Set.mem_singleton_iff.1 ha + cases Set.mem_singleton_iff.1 hc + cases Set.mem_singleton_iff.1 hr + obtain ⟨b1, b2⟩ := b + cases hbs + exact ⟨b1, hb, rfl⟩ + · rintro ⟨x, hx, rfl⟩ + exact ⟨_, rfl, (x, σ), ⟨hx, rfl⟩, _, rfl, rfl⟩ + · apply propext + constructor + · rintro (h | ⟨a, ha, h⟩) + · exact h.elim + cases Set.mem_singleton_iff.1 ha + rcases h with h | ⟨b, -, h⟩ + · exact h + rcases h with h | ⟨c, -, h⟩ + exacts [h.elim, h.elim] + · exact fun h => Or.inr ⟨_, rfl, Or.inl h⟩ + rw [run] + refine ⟨hf, ⟨_, y, hy, rfl⟩, ?_⟩ + rintro r ⟨x, hx, rfl⟩ + exact ⟨x.2, rfl, (hq x hx).1, (hq x hx).2⟩ + +theorem performFlush_umA (t : FlushType) (vs ve : VSpaceRef) (ps : Paddr) (sp : ObjRef) (asd : Asid) : + NdOkA UmA (performFlush (Z := Unit) t vs ve ps sp asd) := by + unfold performFlush + refine NdOkA.bind umA_trans (NdOkA.pure umA_refl _) fun _ => NdOkA.bind umA_trans (NdOkA.pure umA_refl _) fun _ => ?_ + unfold NonDet.when + split + · exact doMachineOp_umA (doFlush_umA _ _ _ _) + · exact NdOkA.pure umA_refl _ + +theorem perfVsInv_umA (iv : VSpaceInvocation) : NdOkA UmA (performVspaceInvocation (Z := Unit) iv) := by + unfold performVspaceInvocation + cases iv with + | vspaceNothing => exact NdOkA.pure umA_refl _ + | vspaceFlush t vs ve ps sp asd => exact performFlush_umA t vs ve ps sp asd + +/-- **Every outcome of a flush invocation's perform keeps `underlyingMemory`**. -/ +theorem flushPerf_umA {inv : Fl4ArchInvocation} (h : FlushInvA inv) : + NdOkA UmA ((fl4ArchPerformInvocation (Z := Unit) inv).run) := by + rcases inv with iv | iv | iv | iv | iv | iv + all_goals try exact h.elim + · cases iv with + | invokeVSpace o => + show NdOkA UmA (NonDetE.liftE (ε := _) (archNoReturn (performVspaceInvocation (Z := Unit) o))).run + exact NdOkA.liftE umA_refl umA_trans + (NdOkA.bind umA_trans (perfVsInv_umA o) fun _ => NdOkA.pure umA_refl _) + | invokePage p => + cases p with + | pageFlush t vs ve ps sp asd => + show NdOkA UmA (NonDetE.liftE (ε := _) (performPageInvocation (Z := Unit) (.pageFlush t vs ve ps sp asd))).run + exact NdOkA.liftE umA_refl umA_trans + (NdOkA.bind umA_trans (performFlush_umA _ _ _ _ _ _) fun _ => NdOkA.pure umA_refl _) + | _ => exact h.elim + | _ => exact h.elim + · cases iv with + | flush o => + show NdOkA UmA (NonDetE.liftE (ε := _) (performExtVSpace (Z := Unit) (.flush o))).run + exact NdOkA.liftE umA_refl umA_trans + (NdOkA.bind umA_trans (perfVsInv_umA o) fun _ => NdOkA.pure umA_refl _) + | _ => exact h.elim + +/-! ## The decoders accept only flush invocations -/ + +theorem vsFlushFrame_okA (lab start finish : Data) (v : ObjRef) (asid : Asid) (fi : Option (VMPageSize × PTE)) : + OkAllA (fun iv => ∃ o, iv = ArchInvocation.invokeVSpace o) + (fl4VsFlushFrame (Z := Unit) lab start finish v asid fi) := by + cases fi with + | none => exact OkAllA.returnOk ⟨_, rfl⟩ + | some x => + obtain ⟨pg, pte⟩ := x + unfold fl4VsFlushFrame + dsimp only + exact OkAllA.ite (OkAllA.throwE _) (OkAllA.ite (OkAllA.throwE _) (OkAllA.returnOk ⟨_, rfl⟩)) + +theorem frFlushFrame_okA (lab start finish : Data) (p : ObjRef) (pg : VMPageSize) (va : VSpaceRef) (v : ObjRef) + (asid : Asid) (fi : Option (VMPageSize × PTE)) : + OkAllA (fun iv => ∃ t x y z w u, iv = ArchInvocation.invokePage (.pageFlush t x y z w u)) + (fl4FrFlushFrame (Z := Unit) lab start finish p pg va v asid fi) := by + cases fi with + | none => exact OkAllA.throwE _ + | some x => + obtain ⟨sz, pte⟩ := x + unfold fl4FrFlushFrame + dsimp only + exact OkAllA.ite (OkAllA.ite (OkAllA.throwE _) (OkAllA.returnOk ⟨_, _, _, _, _, _, rfl⟩)) (OkAllA.throwE _) + +theorem decodeVsFl_okA (lab : Data) (args : List Data) (cte : CslotPtr) (p : ObjRef) + (md : Option (Asid × VSpaceRef)) (exs : List (Cap × CslotPtr)) : + OkAllA (fun iv => ∃ o, iv = ArchInvocation.invokeVSpace o) + (decodeFl4VsInvFlush (Z := Unit) lab args cte (.pageTableCap p .vsRootPtT md) exs) := by + unfold decodeFl4VsInvFlush + dsimp only + refine OkAllA.ite ?_ (OkAllA.throwE _) + repeat' (first + | exact vsFlushFrame_okA _ _ _ _ _ _ + | (refine OkAllA.bind fun _ => ?_) + | (split)) + +theorem decodeFrFl_okA (lab : Data) (args : List Data) (cte : CslotPtr) (p : ObjRef) (R : CapRights) + (sz : VMPageSize) (dev : Bool) (md : Option (Asid × VSpaceRef)) (exs : List (Cap × CslotPtr)) : + OkAllA (fun iv => ∃ t x y z w u, iv = ArchInvocation.invokePage (.pageFlush t x y z w u)) + (decodeFl4FrInvFlush (Z := Unit) lab args cte (.frameCap p R sz dev md) exs) := by + unfold decodeFl4FrInvFlush + dsimp only + refine OkAllA.ite ?_ (OkAllA.throwE _) + repeat' (first + | exact frFlushFrame_okA _ _ _ _ _ _ _ _ _ + | (refine OkAllA.bind fun _ => ?_) + | (split)) + +theorem decodeExtFlAt_okA (k : ExtRootKind) (lab : Data) (args : List Data) (p : ObjRef) (sz : VMPageSize) + (dev : Bool) (md : Option (Asid × VSpaceRef)) : + OkAllA (fun iv => ∃ o, iv = ExtVSpaceInvocation.flush o) + (decodeExtPageFlushAt (Z := Unit) k lab args p sz dev md) := by + unfold decodeExtPageFlushAt + repeat' (first + | exact OkAllA.throwE _ + | exact OkAllA.returnOk ⟨_, rfl⟩ + | (refine OkAllA.bind fun _ => ?_) + | (refine OkAllA.ite ?_ ?_) + | (split)) + +theorem decodeS2Fl_okA (lab : Data) (args : List Data) (cap : Cap) : + OkAllA (fun iv => ∃ o, iv = ExtVSpaceInvocation.flush o) (decodeS2Flush (Z := Unit) lab args cap) := by + unfold decodeS2Flush + repeat' (first + | exact OkAllA.throwE _ + | exact OkAllA.returnOk ⟨_, rfl⟩ + | (refine OkAllA.bind fun _ => ?_) + | (refine OkAllA.ite ?_ ?_) + | (split)) + +theorem extFl_flushA {m : SEMonad Unit ExtVSpaceInvocation} + (h : OkAllA (fun iv => ∃ o, iv = ExtVSpaceInvocation.flush o) m) : + OkAllA FlushInvA (NonDetE.liftME Fl4ArchInvocation.extVSpace m) := + OkAllA.liftME h fun _ ⟨_, e⟩ => by rw [e]; trivial + +/-- **The dispatcher on a VSpace root at a cache label accepts only flushes.** -/ +theorem dec_vsFlush_okA (calling : Bool) (xSlot : CapRef) (cte : CslotPtr) (exs : List (Cap × CslotPtr)) + (args : List Data) (l : Fl4Label) (hf : isVSpaceFlushLabel (invocationType (specLabelWord l)) = true) + (p : ObjRef) (md : Option (Asid × VSpaceRef)) : + OkAllA FlushInvA (fl4ArchDecodeInvocation (Z := Unit) calling l args xSlot cte (.pageTableCap p .vsRootPtT md) + exs) := by + rw [dec_vsFlushA calling xSlot cte exs args l hf] + exact OkAllA.liftME (decodeVsFl_okA _ _ _ _ _ _) fun _ ⟨_, e⟩ => by rw [e]; trivial + +/-- **The dispatcher on a Stage-2 VSpace root accepts only flushes.** -/ +theorem dec_s2Flush_okA (calling : Bool) (xSlot : CapRef) (cte : CslotPtr) (exs : List (Cap × CslotPtr)) + (args : List Data) (l : Fl4Label) (p : ObjRef) (d : Option Asid) : + OkAllA FlushInvA (fl4ArchDecodeInvocation (Z := Unit) calling l args xSlot cte (.ext (.stage2VSpaceCap p d)) + exs) := by + unfold fl4ArchDecodeInvocation + exact extFl_flushA (decodeS2Fl_okA _ _ _) + +/-- **The dispatcher on a frame cap at a page-flush label accepts only flushes.** -/ +theorem dec_frFlush_okA (calling : Bool) (xSlot : CapRef) (cte : CslotPtr) (exs : List (Cap × CslotPtr)) + (args : List Data) (l : Fl4Label) (hf : isPageFlushLabel (invocationType (specLabelWord l)) = true) + (hd : l ≠ .ext .armPageDeriveSubframe) (ht : l ≠ .ext .armPageTestAndClearAccessed) (e : Bool) (p : ObjRef) + (R : CapRights) (sz : VMPageSize) (dev : Bool) (md : Option (Asid × VSpaceRef)) : + OkAllA FlushInvA (fl4ArchDecodeInvocation (Z := Unit) calling l args xSlot cte (absFrameE e p R sz dev md) + exs) := by + cases e with + | true => + rw [show absFrameE true p R sz dev md = .ext (.stage2FrameCap p R sz dev md) from rfl, + dec_frFlS2A calling xSlot cte exs args l hf hd ht] + exact extFl_flushA (decodeExtFlAt_okA _ _ _ _ _ _ _) + | false => + rw [show absFrameE false p R sz dev md = .frameCap p R sz dev md from rfl] + unfold fl4ArchDecodeInvocation + dsimp only + rw [if_neg hd, if_neg ht, if_pos hf] + cases md <;> dsimp only <;> refine OkAllA.bind fun k => ?_ <;> cases k <;> dsimp only <;> first + | exact OkAllA.liftME (decodeFrFl_okA _ _ _ _ _ _ _ _ _) fun _ ⟨_, _, _, _, _, _, e⟩ => by rw [e]; trivial + | exact extFl_flushA (decodeExtFlAt_okA _ _ _ _ _ _ _) + +/-! ## The record -/ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- `AR` at the fixed hardware roots `h`, at every second argument. -/ +def snapA (AR : A → HwRootA → Prop) (h : HwRootA) : A → HwRootA → Prop := fun a _ => AR a h + +theorem ArchKeepsA.snap {ar : machine.Arch A} (K : ArchKeepsA ar AR) (h : HwRootA) : ArchKeepsA ar (snapA AR h) := + { invPage := fun a _ x y ha => K.invPage a h x y ha + invAsid := fun a _ x ha => K.invAsid a h x ha + cacheOp := fun a _ o r s v n p ha => K.cacheOp a h o r s v n p ha + invS2Page := fun a _ x y ha => K.invS2Page a h x y ha + invS2Vmid := fun a _ x ha => K.invS2Vmid a h x ha + invHiPage := fun a _ x ha => K.invHiPage a h x ha + invHiVs := fun a _ x ha => K.invHiVs a h x ha + hwAccessFlag := K.hwAccessFlag } + +variable (st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n) in +/-- **One cache-maintenance invocation's result** (module docstring): an `Err` keeps the record and the spec throws +the related error; an accepted one is a flush invocation performed after the caller's restart, and the record holds. -/ +def FlushPostA (ar : machine.Arch A) (nA : U64) (m : Fl4SpecA64.McState) (σ : Fl4SpecA64.State Unit) + (D : Set (Except Fl4SpecA64.SyscallError Fl4ArchInvocation × Fl4SpecA64.State Unit) × Prop) + (r : core.result.Result Unit error.Exc × S × A) : Prop := + match r.1 with + | .Err e => + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) r.2.1 r.2.2 + σ m ∧ + ∃ e', D = ({(.error e', σ)}, False) ∧ VsErrRelA e e' + | .Ok () => + ∃ inv x σ' rv, (.ok inv, σ) ∈ D.1 ∧ ¬ D.2 ∧ σ.kheap σ.curThread = some (.tcb x) ∧ + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + (.ok rv, σ') ∈ ((fl4ArchPerformInvocation (Z := Unit) inv).run (stUpdA σ σ.curThread x .restart)).1 ∧ + Sched.BodyKeepA (stUpdA σ σ.curThread x .restart) σ' ∧ + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) r.2.1 r.2.2 + σ' m + +variable {ar : machine.Arch A} {nA : U64} + +set_option maxHeartbeats 1000000 in +/-- **A flush arm at record level** (module docstring), from its `JEntA` top at `snapA AR Z.hw`. -/ +theorem JSyscallA.flushLiftA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {slot : U64} (hsl : slot ∈ OS) (hcov : SlotCov st OS s slot) + {extra : Aeneas.Std.Array obj.Slot 3#usize} (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) + {f : Result (core.result.Result Unit error.Exc × S × A)} + {D : Set (Except Fl4SpecA64.SyscallError Fl4ArchInvocation × Fl4SpecA64.State Unit) × Prop} + (hfoot : FlushFootA st s f) (hshape : ∀ inv, (.ok inv, σ) ∈ D.1 → FlushInvA inv) + (top : f ⦃ res => VsTopPostA (L := L) (snapA AR Z.hw) Z OS m σ D s a res ⦄) : + f ⦃ r => FlushPostA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar nA m σ D r ⦄ := by + obtain ⟨res, e, p⟩ := spec_existsA top + rw [e, WP.spec_ok] + obtain ⟨y, s', a'⟩ := res + cases y with + | Err er => + obtain ⟨rfl, rfl, h⟩ := p + exact ⟨J.toJStage3A, h⟩ + | Ok u => + cases u + obtain ⟨inv, ⟨hD, hnf⟩, σ1, hset', σ', rv, hperf, Z', -, -, hJ, -, -⟩ := p + obtain ⟨x, hx, hst, hset, k⟩ := J.restartMachA hsl hcov hext + have e1 : σ1 = stUpdA σ σ.curThread x .restart := by + rw [hset] at hset' + have := Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst hset') + exact ((Prod.mk.inj this).2).symm + subst e1 + have hs' : s' = L.wr s (stA (L.curW s)) sched.TS_RESTART := by + have h := hfoot _ e rfl + rw [hst] at h + exact (ok_inj_ff h).symm + have har : AR a' Z.hw := hJ.1.ar + obtain ⟨ms, hms, hum, hlt⟩ := (flushPerf_umA (hshape _ hD) _).2.2 _ hperf + change σ' = { stUpdA σ σ.curThread x .restart with machineState := ms } at hms + have J2 := k a' har ms hum + have BK : Sched.BodyKeepA (stUpdA σ σ.curThread x .restart) σ' := by + rw [hms]; exact Sched.BodyKeepA.ofMach _ _ hlt + rw [← hms, ← hs'] at J2 + exact ⟨inv, x, σ', rv, hD, hnf, hx, hset, hperf, BK, J2⟩ + +/-- **The VSpace-root flush** (labels 42..45). -/ +theorem JSyscallA.vsFlushA (K : ArchKeepsA ar AR) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (cte : Fl4SpecA64.CslotPtr) (hab : AbsSlotA Z.th cte) (hcov : SlotCov st OS s (cslotAddrA cte)) + (ptr asid : U64) (mp : Bool) (hcap : capAtA (L.mem s) (cslotAddrA cte) = .Pml4 ptr mp asid) + (lbl length buf : U64) (hl : 42 ≤ lbl.val ∧ lbl.val ≤ 45) (block call cd fp : Bool) + (extra : Aeneas.Std.Array obj.Slot 3#usize) (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) + (args : List U64) (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) + (xSlot : Fl4SpecA64.CapRef) (exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) : + syscall.decode_invocation st ar s a lbl length (cslotAddrA cte) (.Pml4 ptr mp asid) block call cd fp buf extra + ⦃ r => FlushPostA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar nA m σ + ((fl4ArchDecodeInvocation (Z := Unit) call (fl4LabelOf lbl.bv) (args.map UScalar.bv) xSlot cte + (.pageTableCap (specPtr ptr) .vsRootPtT (mapDataA mp asid 0#u64)) exs).run σ) r ⦄ := by + have hsl : cslotAddrA cte ∈ OS := J.j5.2.1 _ (hab.isSlot J.j.1.place) + have J' := J.toJStage3A.arSwapA (AR' := snapA AR Z.hw) J.j.1.ar + have RS := J'.rsSiteA hext hsl hcov J.actW J.running J.cur + have hTr : TreeA st OS Z.G.chains Z.G.par s := + J.j.1.cdt.tree.grow J.j5.2.1 (fun x hx hn => J.j5.2.2 x hx hn) + refine J.flushLiftA hsl hcov hext ?_ ?_ + (decInv_vspace_jA (K.snap Z.hw) J'.toJEntA RS hTr cte hab ptr asid mp hcap lbl length buf block call cd fp call + extra args hlen hargs xSlot exs) + · rw [decode_vs_flushA ar lbl length _ ptr asid mp block call cd fp buf extra hl] + exact fun r h => vsFlush_footA h + · intro inv h + have hf : isVSpaceFlushLabel (invocationType (specLabelWord (fl4LabelOf lbl.bv))) = true := by + rw [fl4LabelOf_bvA]; exact vsFlush_inNA hl + exact dec_vsFlush_okA _ _ _ _ _ _ hf _ _ σ inv σ h + +/-- **The Stage-2 root flush** (every label; the others are `IllegalOperation`). -/ +theorem JSyscallA.s2FlushA (K : ArchKeepsA ar AR) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (hIpa : S2IpaE ar a) + (cte : Fl4SpecA64.CslotPtr) (hab : AbsSlotA Z.th cte) (hcov : SlotCov st OS s (cslotAddrA cte)) + (ptr asid va : U64) (mp : Bool) (hcap : capAtA (L.mem s) (cslotAddrA cte) = .Ept ptr 0#u64 mp asid va) + (lbl length buf : U64) (block call cd fp : Bool) + (extra : Aeneas.Std.Array obj.Slot 3#usize) (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) + (args : List U64) (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) + (xSlot : Fl4SpecA64.CapRef) (exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) : + syscall.decode_invocation st ar s a lbl length (cslotAddrA cte) (.Ept ptr 0#u64 mp asid va) block call cd fp buf + extra + ⦃ r => FlushPostA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar nA m σ + ((fl4ArchDecodeInvocation (Z := Unit) call (fl4LabelOf lbl.bv) (args.map UScalar.bv) xSlot cte + (.ext (.stage2VSpaceCap (specPtr ptr) (asidDataA mp asid))) exs).run σ) r ⦄ := by + have hsl : cslotAddrA cte ∈ OS := J.j5.2.1 _ (hab.isSlot J.j.1.place) + have J' := J.toJStage3A.arSwapA (AR' := snapA AR Z.hw) J.j.1.ar + have RS := J'.rsSiteA hext hsl hcov J.actW J.running J.cur + have hTr : TreeA st OS Z.G.chains Z.G.par s := + J.j.1.cdt.tree.grow J.j5.2.1 (fun x hx hn => J.j5.2.2 x hx hn) + refine J.flushLiftA hsl hcov hext ?_ ?_ + (decInv_s2Vspace_jA (K.snap Z.hw) J'.toJEntA RS hTr hIpa cte hab ptr asid va mp hcap lbl length buf block call cd + fp call extra args hlen hargs xSlot exs) + · unfold syscall.decode_invocation + simp only [cur_thread_ok, bind_ok] + exact ept_decode_footA + · exact fun inv h => dec_s2Flush_okA _ _ _ _ _ _ _ _ σ inv σ h + +/-- **The frame flush** (labels 51..54, every frame cap). -/ +theorem JSyscallA.frFlushA (K : ArchKeepsA ar AR) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (hIpa : S2IpaE ar a) + (cte : Fl4SpecA64.CslotPtr) (hab : AbsSlotA Z.th cte) (hcov : SlotCov st OS s (cslotAddrA cte)) + (fp rt asid va : U64) (sz : capability.FrameSize) (dev mp e : Bool) + (hcap : capAtA (L.mem s) (cslotAddrA cte) = .Frame fp sz rt dev mp e asid va) + (lbl length buf : U64) (hl : 51 ≤ lbl.val ∧ lbl.val ≤ 54) (block call cd fpb : Bool) + (extra : Aeneas.Std.Array obj.Slot 3#usize) (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) + (args : List U64) (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) + (x0 : U64) (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) + (xSlot : Fl4SpecA64.CapRef) (exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) + (hex : ExtraRelA Z.th (L.mem s) x0 (exs.map Prod.fst)) : + syscall.decode_invocation st ar s a lbl length (cslotAddrA cte) (.Frame fp sz rt dev mp e asid va) block call cd + fpb buf extra + ⦃ r => FlushPostA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar nA m σ + ((fl4ArchDecodeInvocation (Z := Unit) call (fl4LabelOf lbl.bv) (args.map UScalar.bv) xSlot cte + (absFrameE e (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev (mapDataA mp asid va)) exs).run σ) r ⦄ := by + have hsl : cslotAddrA cte ∈ OS := J.j5.2.1 _ (hab.isSlot J.j.1.place) + have J' := J.toJStage3A.arSwapA (AR' := snapA AR Z.hw) J.j.1.ar + have RS := J'.rsSiteA hext hsl hcov J.actW J.running J.cur + have hTr : TreeA st OS Z.G.chains Z.G.par s := + J.j.1.cdt.tree.grow J.j5.2.1 (fun x hx hn => J.j5.2.2 x hx hn) + have h49 : lbl ≠ fl4_abi.label.ARMPageMap := fun h => by + have : fl4_abi.label.ARMPageMap.val = 49 := by simp [fl4_abi.label.ARMPageMap] + rw [h, this] at hl; omega + refine J.flushLiftA hsl hcov hext ?_ ?_ + (decInv_frame_jA (K.snap Z.hw) J'.toJEntA RS J.tabS1 J.tabExt J.toJStage3A.ksbAligned J.toJStage3A.offAsE hIpa + hTr (fun y hy hn => J.j5.2.2 y hy hn) cte hab hsl fp rt asid va sz dev mp e hcap lbl length buf block call cd + fpb call extra args hlen hargs x0 hx0 xSlot exs hex (Or.inl (by omega)) (fun h => absurd h h49)) + · rw [decInv_frameA, decode_frame_flA ar lbl length _ fp rt asid va sz dev mp e call buf extra hl] + exact page_flush_footA + · intro inv h + obtain ⟨hfl, -, hd, ht⟩ := labNA_frFlA lbl.val hl.1 hl.2 + have hf : isPageFlushLabel (invocationType (specLabelWord (fl4LabelOf lbl.bv))) = true := by + rw [fl4LabelOf_bvA, invType_specA]; exact hfl + rw [← fl4LabelOf_bvA] at hd ht + exact dec_frFlush_okA _ _ _ _ _ _ hf hd ht _ _ _ _ _ _ σ inv σ h + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3LiftGetAddrA.lean b/proofs/Fl4ProofsA64/P3/Stage3LiftGetAddrA.lean new file mode 100644 index 000000000..0c40417dc --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3LiftGetAddrA.lean @@ -0,0 +1,134 @@ +/- +P3 (aarch64, A64-P3 Lift 1a): **`ARMPageGetAddress` at record level** (every frame cap): the caller's restart, +then the one-word reply `paddr` (`JStage3A.replyOneA`). + +Named premises: [P] `ArchTotal`, `MaxSpi`, `ArchPlat`, `A64FrameMasks` (through `decodeFinA`). +-/ +import Fl4ProofsA64.P3.Stage3ReplyOneA +import Fl4ProofsA64.P3.Stage3SyscallA +import Fl4ProofsA64.P3.Stage3FinA +import Fl4ProofsA64.P3.Stage3RsSiteA +import Fl4ProofsA64.P3.VsArmA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {ar : machine.Arch A} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- FL4's `paddr` of a kernel-window pointer is the spec's `addr_from_pptr`. -/ +theorem paddr_specA (fp : U64) : + (core.num.U64.wrapping_sub fp layout.PPTR_BASE).bv = Fl4SpecA64.addrFromPPtr (specPtr fp) := by + rw [addrFromPPtr_specPtr] + apply BitVec.eq_of_toNat_eq + have h1 : (core.num.U64.wrapping_sub fp layout.PPTR_BASE).bv.toNat = + (fp.val + (2 ^ 64 - layout.PPTR_BASE.val)) % 2 ^ 64 := by + change (core.num.U64.wrapping_sub fp layout.PPTR_BASE).val = _ + simp only [core.num.U64.wrapping_sub_val_eq, UScalar.size_UScalarTyU64, U64.size_def, U64.numBits, + UScalarTy.U64_numBits_eq] + have h2 : layout.PPTR_BASE.val = fl4PptrBaseA.toNat := by + unfold layout.PPTR_BASE fl4PptrBaseA; decide + change (core.num.U64.wrapping_sub fp layout.PPTR_BASE).bv.toNat = (fp.bv - fl4PptrBaseA).toNat + rw [h1, BitVec.toNat_sub, ← h2] + exact congrArg (· % 2 ^ 64) (Nat.add_comm _ _) + +variable (st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n) in +/-- **The result of a one-word reply arm**: the spec decodes `inv`, performs it after the caller's restart with the +one result word, replies (`call`) and resumes the caller; the record holds at some scheduler queues. -/ +def ReplyOutA (ar : machine.Arch A) (nA : U64) (m : Fl4SpecA64.McState) (σ : Fl4SpecA64.State Unit) (call : Bool) + (inv : Fl4ArchInvocation) (r : core.result.Result Unit error.Exc × S × A) : Prop := + r.1 = .Ok () ∧ ∃ (x : Fl4SpecA64.Tcb) (σ2 σ3 : Fl4SpecA64.State Unit) (w : Fl4SpecA64.Word), + σ.kheap σ.curThread = some (.tcb x) ∧ + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + (.ok [w], σ2) ∈ ((fl4ArchPerformInvocation (Z := Unit) inv).run (stUpdA σ σ.curThread x .restart)).1 ∧ + ((), σ3) ∈ ((do + Fl4Spec.NonDet.when call (Fl4SpecA64.replyFromKernel (Z := Unit) σ.curThread (0, [w])) + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .running) σ2).1 ∧ ReplyShapeA σ σ3 x ∧ + ∃ Q Rl, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) r.2.1 r.2.2 σ3 m + +/-- **`ARMPageGetAddress` at record level** (module docstring). -/ +theorem JSyscallA.getAddrA (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) + s a σ m) + (hfm : A64FrameMasks st s) + (cte : Fl4SpecA64.CslotPtr) (hab : AbsSlotA Z.th cte) (hcov : SlotCov st OS s (cslotAddrA cte)) + (fp rt asid va : U64) (sz : capability.FrameSize) (dev mp e : Bool) + (hcap : capA st s (cslotAddrA cte) = .Frame fp sz rt dev mp e asid va) + (len buf : U64) (blk call don fpb : Bool) + (extra : Aeneas.Std.Array obj.Slot 3#usize) (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) + (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (args : List Fl4SpecA64.Data) (xSlot : Fl4SpecA64.CapRef) (exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) : + syscall.decode_invocation st ar s a fl4_abi.label.ARMPageGetAddress len (cslotAddrA cte) + (.Frame fp sz rt dev mp e asid va) blk call don fpb buf extra + ⦃ r => (fl4ArchDecodeInvocation (Z := Unit) call (archL .armPageGetAddress) args xSlot cte + (absFrameE e (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev (mapDataA mp asid va)) exs).run σ = + ({(.ok (.spec (.invokePage (.pageGetAddr (specPtr fp)))), σ)}, False) ∧ + ReplyOutA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar hPl.ipa m σ call + (.spec (.invokePage (.pageGetAddr (specPtr fp)))) r ⦄ := by + have hsl : cslotAddrA cte ∈ OS := J.j5.2.1 _ (hab.isSlot J.j.1.place) + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hsl hcov J.actW J.running J.cur + obtain ⟨Rq, Nt, extra', sp, P⟩ := RS.pl + obtain ⟨x, hx, hst, hset, J1, -, -, -⟩ := JStage3A.of_restartCurA J.toJStage3A P RS.sym RS.run + J.toJStage3EntryA.curNotVmA RS.cur RS.vs RS.vc + set s1 := L.wr s (stA (L.curW s)) sched.TS_RESTART with hs1 + set σ1 := stUpdA σ σ.curThread x .restart with hσ1 + have ct : σ.curThread = specPtr (L.curW s) := J.j.1.sch.curThread + have ct1 : σ1.curThread = σ.curThread := rfl + have hcur1 : L.curW s1 = L.curW s := + specPtr_inj (J1.j.1.sch.curThread.symm.trans (ct1.trans ct)) + have htZ : L.curW s ∈ Z.T := J.sr.curT + have hta : TcbAddr (L.curW s) := J.sr.core.tcbs _ htZ + have sA : Aligned (stA (L.curW s)) := by rw [stA_eq]; exact field_aligned hta (by simp) (by simp) + have far : ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → K.val ≠ 3328 → + L.mem s1 (core.num.U64.wrapping_add (L.curW s) K) = L.mem s (core.num.U64.wrapping_add (L.curW s) K) := + fun K h1 h2 h3 => mem_wr_ne' sA (field_aligned hta h1 h2) + (by rw [stA_eq]; exact field_ne (K2 := K) hta hta (by simp) h1 (fun _ => by simpa using Ne.symm h3)) + have hsc1 : L.mem s1 (scA (L.curW s)) = L.mem s (scA (L.curW s)) := far 3464#u64 (by simp) (by simp) (by simp) + have hiq1 : L.mem s1 (inqA (L.curW s)) = L.mem s (inqA (L.curW s)) := far 3592#u64 (by simp) (by simp) (by simp) + have hrq1 : σ1.curThread ∉ σ1.releaseQueue := by + show σ.curThread ∉ σ.releaseQueue + rw [ct, J.j.1.rest.rel]; intro hm + obtain ⟨q, hq, eq⟩ := List.mem_map.1 hm + have e' : q = L.curW s := specPtr_inj eq + subst e' + have haf := J.sq.inR _ J.sq.cur _ hq + exact ((J.sq.released _ (by rw [J.tcbs]; exact htZ)).2 (by rw [haf]; exact hq)) RS.cur.1 + set pa := core.num.U64.wrapping_sub fp layout.PPTR_BASE with hpa + have hprog : syscall.decode_invocation st ar s a fl4_abi.label.ARMPageGetAddress len (cslotAddrA cte) + (.Frame fp sz rt dev mp e asid va) blk call don fpb buf extra = + (do let s2 ← vspace.reply1 st s1 (L.curW s) call pa; ok (core.result.Result.Ok (), s2, a)) := by + rw [decInv_frameA, decode_frame_getAddrA, hst, bind_ok, show layout.paddr fp = ok pa from rfl, bind_ok] + have hx1 : σ1.kheap σ1.curThread = some (.tcb { x with tcbState := .restart }) := by + simp [σ1, stUpdA, setObjA] + have R := J1.replyOneA (ar := ar) (nA := hPl.ipa) hx1 (Or.inl rfl) (by rw [hcur1, hsc1]; exact RS.cur.2.1) + (by rw [hcur1, hsc1]; exact RS.cur.2.2) (by rw [hcur1, hiq1]; exact RS.cur.1) hrq1 call pa + (fun s2 h2 σX => by + have hd : syscall.decode_invocation st ar s a fl4_abi.label.ARMPageGetAddress len (cslotAddrA cte) + (.Frame fp sz rt dev mp e asid va) blk call don fpb buf extra = ok (core.result.Result.Ok (), s2, a) := by + rw [hprog, ← hcur1, h2, bind_ok] + obtain ⟨-, Ac', sysl, symL, -, userOK, j7⟩ := J.decodeFinA hA hM hPl hfm hsl hcov hcap hext hSC + (lbl := fl4_abi.label.ARMPageGetAddress) (by simp [DecArmS1aEnt, DecArmQH]) + (fun _ h => by cases h) (fun _ h => by cases h) hd σX m + exact ⟨sysl, symL, userOK, j7⟩) + rw [hcur1] at R + obtain ⟨s2, e2, σ3, hrun, ⟨uc, hshape⟩, Q, Rl, J2⟩ := spec_existsA R + rw [hprog, e2, bind_ok, WP.spec_ok] + refine ⟨dec_pageGetAddrA _ _ _ _ _ _ _ _ _ _ _, rfl, x, σ1, σ3, pa.bv, hx, hset, ?_, hrun, + ⟨uc, by rw [hshape]; simp only [σ1, stUpdA, setObjA, Function.update_idem, replyTcbA]⟩, Q, Rl, J2⟩ + rw [show pa.bv = Fl4SpecA64.addrFromPPtr (specPtr fp) from paddr_specA fp] + exact mem_archPgA (by unfold Fl4SpecA64.performPageInvocation; exact Set.mem_singleton _) + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3LiftIrqClearA.lean b/proofs/Fl4ProofsA64/P3/Stage3LiftIrqClearA.lean new file mode 100644 index 000000000..dd2f0d49d --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3LiftIrqClearA.lean @@ -0,0 +1,221 @@ +/- +P3 (aarch64, A64-P3 Lift 1b): **`IRQClearIRQHandler` at record level**. + +FL4's arm is the caller's Restart and then `deleting_irq_handler` (`irq_slot`, `delete_one`), which is delete-hooks' +`deletingIrqHandler_recA` at the restarted state (`decDel_restartA`, `JSyscallA.delRestartWA`). The record returns to +`JStage3A` by `DelXA6G.toStage3A`, with `SysL` / `SymL` at the final state from `decodeFinA`. + +* `deletingIrqHandler_schedValidA`: `SchedValidA` across the spec's `deleting_irq_handler` (the argument of + `finHookIrqSvA`, at the node slot itself). +* `JSyscallA.irqClearA`: the arm. The record at the end is at the entry's `McState` `m` (the deletion loop's records carry + it, delete-hooks `ba9ddb574`), so the arm enters the dispatch (`IrqScopeA.clear`). + +Named premises: [P] `ArchTotal`, `MaxSpi`, `ArchPlat`, `A64FrameMasks`, `hCs`, `hmb`, `hmax`, `htu`; [S] `h1` +(`numNodes = 1`); [J] decode-site facts of the deletion (`hvs`, `hvc`, `DelSiteA`), as the CNode Delete tops. +New module. +-/ +import Fl4ProofsA64.P3.Stage3DelAsmA +import Fl4ProofsA64.P3.Stage3DelSvReplyA +import Fl4ProofsA64.P3.IrqInvokeDispatchA +import Fl4ProofsA64.P3.Stage3FinA +import Fl4ProofsA64.P3.Stage3DispBaseA +import Fl4ProofsA64.P3.Stage3DelBodyA +import Fl4ProofsA64.P3.Stage3DelFinIpcA +import Fl4ProofsA64.P3.Stage3LiftBodyA +import Fl4ProofsA64.P3.Stage3LiftMachA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Vcpu Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- **`SchedValidA` across `deleting_irq_handler`** (module docstring). -/ +theorem deletingIrqHandler_schedValidA {J5 J7 : JSlotA S} {G : MdbGhostA} {irq : U64} + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (D : DStateA st Z.th OS s σ G) + (sv : SchedValidA σ) (hi : irq.val < 5376) : + ∀ y ∈ (deletingIrqHandler (Z := Unit) (BitVec.ofNat 13 irq.val) σ).1, SchedValidA y.2 := by + intro y hy + have irqRel : IrqRelA (L.mem s) (L.ksb s) σ := J.j.1.irq + have place : PlaceA Z.th (L.ksb s) := J.j.1.place + obtain ⟨hA, he, -⟩ := irqSlot_relatedA (st := st) irqRel place hi + unfold deletingIrqHandler at hy + obtain ⟨q, hq, hy⟩ := mem_bindA.1 hy + obtain rfl : q = ((σ.interruptIrqNode (BitVec.ofNat 13 irq.val), []), σ) := hq + dsimp only at hy + generalize esl : ((σ.interruptIrqNode (BitVec.ofNat 13 irq.val), []) : CslotPtr) = isl at hA he hy + obtain ⟨ac0, hg, hace⟩ := getCapOne_eqA (st := st) D.rel hA + have hxO := D.mem_slot hA + unfold capDeleteOne at hy + obtain ⟨c0, hc0, hy⟩ := mem_bindA.1 hy + rw [hg] at hc0 + obtain rfl : c0 = (ac0, σ) := hc0 + dsimp only at hy + by_cases hnull : ac0 = .nullCap + · have ey : y = ((), σ) := by + simp only [NonDet.unless, hnull, not_true_eq_false, ↓reduceIte] at hy + exact hy + rw [ey]; exact sv + have hy2 : y ∈ ((isFinalCap (Z := Unit) ac0 >>= fun final => fastFinalise ac0 final >>= fun _ => + emptySlot isl .nullCap) σ).1 := by + simpa only [NonDet.unless, hnull, not_false_eq_true, ↓reduceIte] using hy + obtain ⟨fin, hfin, hy3⟩ := mem_bindA.1 hy2 + have efin : fin.2 = σ := (PresR.gets (R := @Eq (State Unit)) _ σ fin hfin).symm + rw [efin] at hy3 + obtain ⟨w, hw', hy4⟩ := mem_bindA.1 hy3 + have svw : SchedValidA w.2 := by + have hirqc : IrqCapA ac0 := irqSlot_capA D.rel irqRel place J.sinv.irqN hi (by rw [esl]; exact hg) + rcases hirqc with h | ⟨r, bd, rs, h⟩ + · exact absurd h hnull + have eabs : absCapA (capA st s (cslotAddrA isl)) = .notificationCap r bd rs := by + rcases hace.eq_or_pt with e | ⟨_, _, _, e⟩ + · rw [← e]; exact h + · rw [h] at e; cases e + obtain ⟨p, b0, b1, b2, hcp, er⟩ : ∃ p b0 b1 b2, capA st s (cslotAddrA isl) = .Notification p b0 b1 b2 ∧ + r = specPtr p := by + generalize capA st s (cslotAddrA isl) = c at eabs ⊢ + cases c <;> simp [absCapA] at eabs + exact ⟨_, _, _, _, rfl, eabs.1.symm⟩ + have hp : p ∈ N := J.ipcHeapCov.ntfn p ((J.capTy _ hxO).2.1 p b0 b1 b2 hcp) + rw [h, er] at hw' + unfold fastFinalise at hw' + cases hf : fin.1 + · rw [hf] at hw' + have ew : w = ((), σ) := hw' + rw [ew]; exact sv + · rw [hf] at hw' + exact ntfnFin_schedValidA J.ntfn hp sv w hw' + exact ((FIA.ofValid svw).ofTKeep (emptySlot_tkeepA _ _ _ hy4)).toValid + +/-- **The IRQ slot holds a notification cap or nothing** (the argument of `deletingIrqHandler_bodyA`). -/ +theorem irqSlot_ntfnOrNullA {J5 J7 : JSlotA S} {G : MdbGhostA} {irq : U64} + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (D : DStateA st Z.th OS s σ G) (hi : irq.val < 5376) : + ∃ ac0, getCap (Z := Unit) (σ.interruptIrqNode (BitVec.ofNat 13 irq.val), []) σ = ({(ac0, σ)}, False) ∧ + (ac0 = .nullCap ∨ ∃ r b rs, ac0 = .notificationCap r b rs) := by + have irqRel : IrqRelA (L.mem s) (L.ksb s) σ := J.j.1.irq + have place : PlaceA Z.th (L.ksb s) := J.j.1.place + obtain ⟨hA, he, -⟩ := irqSlot_relatedA (st := st) irqRel place hi + generalize esl : ((σ.interruptIrqNode (BitVec.ofNat 13 irq.val), []) : CslotPtr) = isl at hA he + obtain ⟨ac0, hg, hace⟩ := getCapOne_eqA (st := st) D.rel hA + have hirqc : IrqCapA ac0 := irqSlot_capA D.rel irqRel place J.sinv.irqN hi (by rw [esl]; exact hg) + subst esl + refine ⟨ac0, hg, ?_⟩ + rcases hirqc with h | ⟨r, bd, rs, h⟩ + · exact Or.inl h + · exact Or.inr ⟨r, bd, rs, h⟩ + +/-- **The IRQ slot's notification has no queue member that is the current thread, and its waiters are blocked.** -/ +theorem irqSlot_qneBA {J5 J7 : JSlotA S} {G : MdbGhostA} {irq : U64} + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (D : DStateA st Z.th OS s σ G) (hi : irq.val < 5376) {ac0 : Fl4SpecA64.Cap} + (hg : getCap (Z := Unit) (σ.interruptIrqNode (BitVec.ofNat 13 irq.val), []) σ = ({(ac0, σ)}, False)) : + ∀ r b rs, ac0 = .notificationCap r b rs → NtfnQNeA r σ ∧ + ∀ y q, σ.kheap r = some (.notification y) → y.ntfnObj = .waitingNtfn q → ∀ t ∈ q, NtfnBlockedA σ t := by + intro r bd rs h + have irqRel : IrqRelA (L.mem s) (L.ksb s) σ := J.j.1.irq + have place : PlaceA Z.th (L.ksb s) := J.j.1.place + obtain ⟨hA, he, -⟩ := irqSlot_relatedA (st := st) irqRel place hi + generalize esl : ((σ.interruptIrqNode (BitVec.ofNat 13 irq.val), []) : CslotPtr) = isl at hA he + obtain ⟨ac1, hg1, hace⟩ := getCapOne_eqA (st := st) D.rel hA + rw [esl, hg1] at hg + have eac : ac1 = ac0 := (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst hg))).1 + rw [eac] at hace + have hxO := D.mem_slot hA + have eabs : absCapA (capA st s (cslotAddrA isl)) = .notificationCap r bd rs := by + rcases hace.eq_or_pt with e | ⟨_, _, _, e⟩ + · rw [← e]; exact h + · rw [h] at e; cases e + obtain ⟨p, b0, b1, b2, hcp, er⟩ : ∃ p b0 b1 b2, capA st s (cslotAddrA isl) = .Notification p b0 b1 b2 ∧ + r = specPtr p := by + generalize capA st s (cslotAddrA isl) = c at eabs ⊢ + cases c <;> simp [absCapA] at eabs + exact ⟨_, _, _, _, rfl, eabs.1.symm⟩ + subst er + have hn := J.ipcHeapCov.ntfn p ((J.capTy _ hxO).2.1 p b0 b1 b2 hcp) + exact ⟨ntfnQNe_ofRecA J.ntfn J.sact hn, fun y q hk hq t hm => (ntfnQueue_blockedA J.ntfn hn hk q hq).2 t hm⟩ + +theorem irqSlot_qneA {J5 J7 : JSlotA S} {G : MdbGhostA} {irq : U64} + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (D : DStateA st Z.th OS s σ G) (hi : irq.val < 5376) {ac0 : Fl4SpecA64.Cap} + (hg : getCap (Z := Unit) (σ.interruptIrqNode (BitVec.ofNat 13 irq.val), []) σ = ({(ac0, σ)}, False)) : + ∀ r b rs, ac0 = .notificationCap r b rs → NtfnQNeA r σ := + fun r b rs h => (irqSlot_qneBA J D hi hg r b rs h).1 + +set_option maxHeartbeats 4000000 in +/-- **`IRQClearIRQHandler` at a syscall entry** (module docstring). -/ +theorem JSyscallA.irqClearA [PlainStore st] (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hCnt : CounterStepA σ) + (hfm : A64FrameMasks st s) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {slot irq len buf : U64} {blk call don fp : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hsl : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .IrqHandler irq) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hvs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → Aligned b ∧ b ≠ stA (L.curW s)) + (hvc : ∀ v ∈ Z.V, stA (L.curW s) ≠ v ∧ stA (L.curW s) ≠ core.num.U64.wrapping_add v 24#u64) + (site : DelSiteA st Z.th OS s σ Z.G.chains) : + ∃ s2 a2 σ1 σ2, + syscall.decode_invocation st ar s a fl4_abi.label.IRQClearIRQHandler len slot (capA st s slot) blk call don fp + buf extra = ok (.Ok (), s2, a2) ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ((), σ2) ∈ (fl4InvokeIrqHandler (Z := Unit) (.clearIRQHandler (BitVec.ofNat 13 irq.val)) σ1).1 ∧ + σ2.curThread = σ.curThread ∧ A64RelS3 st AR ar hPl.ipa n Z.Cs s2 a2 σ2 m := by + have hi : irq.val < 5376 := (J.toJStage3A.slotCapTy hsl).1.2.1 irq hcap + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hsl hcov J.actW J.running J.cur + have hT : TreeA st OS Z.G.chains Z.G.par s := J.j.1.cdt.tree.grow J.j5.2.1 (fun x hx hn => J.j5.2.2 x hx hn) + obtain ⟨x, heq, hset, D1, W1⟩ := decDel_restartA J.toJEntA (J.toJStage3A.sysPlace hext hsl hcov) J.symL J.running + RS.cur hvs hvc hT J.j5.2.1 J.j5.2.2 site + (X := DelWTopA st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar hPl.ipa (fun _ _ => True) s) + (fun x hkx => J.delRestartWA hPl hsl hcov hext trivial hCnt x hkx) + have hcore := W1.2.2.1.1 + have X6 := W1.2.2.1.2.1 + have sv := W1.2.2.1.2.2 + have sv2 : ∀ y ∈ (deletingIrqHandler (Z := Unit) (BitVec.ofNat 13 irq.val) + (stUpdA σ σ.curThread x .restart)).1, SchedValidA y.2 := by + obtain ⟨Q, R, hw, Ac', Bf', stack', J1, -⟩ := X6.1 + exact deletingIrqHandler_schedValidA J1 D1 sv hi + obtain ⟨⟨s2, a2⟩, e2, σ2, G', y, -, hstep, -, ⟨-, X2⟩, -⟩ := spec_existsA + (deletingIrqHandler_recA (ar := ar) (ipa := hPl.ipa) s (fun _ _ _ _ _ => True) (DelFootA.of_entA J.toJEntA) hCs hmb + hmax htu h1 hi D1 ⟨hcore, X6⟩) + have hdec : syscall.decode_invocation st ar s a fl4_abi.label.IRQClearIRQHandler len slot (capA st s slot) blk call + don fp buf extra = ok (.Ok (), s2, a2) := by + have e2' : (do + let isl ← interrupt.irq_slot st (L.wr s (stA (L.curW s)) sched.TS_RESTART) irq + cdt.delete_one st ar (L.wr s (stA (L.curW s)) sched.TS_RESTART) a isl) = ok (s2, a2) := e2 + obtain ⟨isl, hisl, hdel⟩ := bind_eq_ok e2' + rw [hcap, decodeIrqHandler_programA, irqClear_programA, heq, bind_ok, hisl, bind_ok, hdel, bind_ok] + rfl + obtain ⟨-, -, sysl, symL, -, -, -⟩ := J.decodeFinA hA hM hPl hfm hsl hcov hcap hext hSC + (lbl := fl4_abi.label.IRQClearIRQHandler) + (by simp [DecArmS1aEnt, DecArmQH, DecArmDelQ]) + (fun i h _ => by cases h; exact hi) (fun _ h => by cases h) (len := len) (blk := blk) (call := call) (don := don) (fp := fp) (buf := buf) + (r := .Ok ()) (s3 := s2) (a3 := a2) (by rw [hcap] at hdec; exact hdec) σ2 m + obtain ⟨Q, R, hw, Ac2, Bf2, stack2, J2⟩ := X2.toStage3A sysl symL (sv2 _ hstep) + obtain ⟨Q0, R0, hw0, Ac0', Bf0', stack0', J1', -⟩ := X6.1 + obtain ⟨ac0, hg, hac⟩ := irqSlot_ntfnOrNullA J1' D1 hi + have hbody := deletingIrqHandler_bodyA hg hac (irqSlot_qneA J1' D1 hi hg) hstep + obtain ⟨x', hx', -, hset', -⟩ := J.restartMachA hsl hcov hext + have e1 : stUpdA σ σ.curThread x .restart = stUpdA σ σ.curThread x' .restart := by + have := Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst (hset.symm.trans hset')) + exact (Prod.mk.inj this).2 + rw [e1] at hbody + obtain ⟨B, hcur⟩ := Sched.BodyOKA.ofDel J hx' hbody + exact ⟨s2, a2, _, σ2, hdec, hset, hstep, hcur, A64RelS3.ofJ J2 B⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3LiftIrqDecA.lean b/proofs/Fl4ProofsA64/P3/Stage3LiftIrqDecA.lean new file mode 100644 index 000000000..e01999724 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3LiftIrqDecA.lean @@ -0,0 +1,230 @@ +/- +P3 (aarch64, A64-P3 Lift 1b): **the decode-site facts of the IRQ arms from the record and the decode run**, discharging +the `IrqSetIRQHandler` / `IRQControl` issue premises. + +* `JStage3A.cnSlot_of_covA`: a covered slot (`SlotCov`: a slot of a CNode cap held in `OS`) is a CNode slot of the typed + heap, as the spec names it, and its CNode is no IRQ node. Discharges `hCsS` / `hss` / `hpn` (and the control slot's + `hCs` / `hss`). +* `JStage3A.cnAbs_ofA`: any spec slot of the typed heap at a covered address is that one (the constructors' spec data are + pinned by `AbsSlotA` and the address). +* `JStage3A.irqDest_ofA`: the issue's destination from the decode run (`extra0`, `lookup_target_slot`, + `ensure_empty_slot`): its spec slot, null, off the IRQ nodes (`hCd` / `hsd` / `hdn` / `hpdn`). +* `irqInactive_ofRunA`: the decoder's `is_irq_active` false is `hact`. +* `irqMsi_acceptA`: the MSI decode accept lemma (`irqIssueMsiA`'s `hdec`); `irqMsi_mapA`: its `msi_map` equation from + an `Ok` run of the decode. +* `irqOfLpi_ltA`, `irqOf_ltA`: the issued IRQ is below 5376. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3LiftIrqIssueA +import Fl4ProofsA64.P3.Stage3SysPlaceA +import Fl4ProofsA64.P3.LookupA +import Fl4ProofsA64.P3.CnodeCopyA +import Fl4ProofsA64.P1.QueueDecIrq +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J7 : JSlotA S} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- **A covered slot is a CNode slot off the IRQ nodes** (module docstring). -/ +theorem JStage3A.cnSlot_of_covA (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + {y : U64} (h : SlotCov st OS s y) : + ∃ (ss : Fl4SpecA64.CslotPtr) (ps : U64), ss.1 = specPtr ps ∧ Z.th ps = some (.cnode ss.2.length) ∧ + cslotAddrA ss = y ∧ ¬ IrqNodeA (L.ksb s) ps := by + obtain ⟨z, hz, p, r, g, b, hc, i, hi, rfl⟩ := h + have hzs : IsSlotA Z.th z := by + by_contra hn + have h0 := J.j5.2.2 z hz hn + rw [hc] at h0; cases h0 + have wf := J.capWfA hzs + have cv := J.capValidA hzs + rw [hc] at wf cv + have h64 : r.val < 64 := wf.1 + obtain ⟨hth, hr0⟩ := cv + rw [Nat.mod_eq_of_lt h64] at hi + refine ⟨(specPtr p, bitsOfA i.val r.val), p, rfl, by simpa using hth, ?_, fun hn => ?_⟩ + · unfold cslotAddrA + rw [ofBl_bitsOfA _ _ (by omega), Nat.mod_eq_of_lt hi, ← slotIdx_addrA] + · have e := J.j.1.place.irqK hth hn + injection e with e + exact hr0 e + +/-- **The pinned spec slot at a covered address** (module docstring). -/ +theorem JStage3A.cnAbs_ofA (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + {y : U64} (h : SlotCov st OS s y) {ss : Fl4SpecA64.CslotPtr} (hAs : AbsSlotA Z.th ss) (hss : cslotAddrA ss = y) : + ∃ ps : U64, (ss.1 = specPtr ps ∧ Z.th ps = some (.cnode ss.2.length)) ∧ ¬ IrqNodeA (L.ksb s) ps := by + obtain ⟨ss', ps, h1, h2, h3, h4⟩ := J.cnSlot_of_covA h + have e : ss = ss' := + cslotAddrA_inj J.j.1.place hAs ⟨ps, h1, Or.inl h2⟩ (hss.trans h3.symm) + subst e + exact ⟨ps, ⟨h1, h2⟩, h4⟩ + +/-- The extra cap the IRQ decoders read is the cap of the first extra slot, an `OS` slot. -/ +theorem extra0_someA {extra : Aeneas.Std.Array obj.Slot 3#usize} {root : capability.Cap} + (he : interrupt.extra0 st s extra = ok (some root)) : + ∃ x ∈ extra.val, x ≠ 0#u64 ∧ root = capA st s x := by + unfold interrupt.extra0 at he + obtain ⟨x, hx, he⟩ := bind_eq_ok he + obtain ⟨b, hb, he⟩ := bind_eq_ok he + have hm : x ∈ extra.val := by + have hlen : extra.val.length = 3 := by simp + obtain ⟨v, hv, e⟩ := spec_existsA (Aeneas.Std.Array.index_usize_spec extra 0#usize (by simp [hlen])) + have ev : v = x := okinj (hv.symm.trans hx) + rw [← ev, e] + exact List.getElem_mem _ + unfold obj.Slot.is_null at hb + have hb' : b = decide (x = 0#u64) := (okinj hb).symm + by_cases h0 : x = 0#u64 + · rw [hb', decide_eq_true h0, if_pos rfl] at he + cases okinj he + · rw [hb', decide_eq_false h0] at he + simp only [Bool.false_eq_true, ↓reduceIte] at he + rw [capA_eq, bind_ok] at he + exact ⟨x, hm, h0, (Option.some.inj (okinj he)).symm⟩ + +/-- **The issue's destination slot from the decode run** (module docstring). -/ +theorem JStage3A.irqDest_ofA (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + {extra : Aeneas.Std.Array obj.Slot 3#usize} (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) + {root : capability.Cap} {dest index depth : U64} + (he : interrupt.extra0 st s extra = ok (some root)) + (hslot : cspace.lookup_target_slot st s root index depth = ok (.Ok dest)) + (hempty : cdt.ensure_empty_slot st s dest = ok (.Ok ())) : + ∃ (sd : Fl4SpecA64.CslotPtr) (pd : U64), sd.1 = specPtr pd ∧ Z.th pd = some (.cnode sd.2.length) ∧ + cslotAddrA sd = dest ∧ capA st s dest = .Null ∧ ¬ IrqNodeA (L.ksb s) pd := by + obtain ⟨x, hx, hx0, hroot⟩ := extra0_someA he + have hxO := hext x hx hx0 + have hwv : CapWfA root ∧ CapValidA Z.th root := by + by_cases hs : IsSlotA Z.th x + · rw [hroot]; exact ⟨J.capWfA hs, J.capValidA hs⟩ + · rw [hroot, J.j5.2.2 x hxO hs] + exact ⟨trivial, trivial⟩ + unfold cspace.lookup_target_slot at hslot + have hl := lookup_slot_for_cnode_op_refinesA (L := L) Z.th s σ J.j.1.st J.j.1.cv false root (absCapA root) + ⟨hwv.1, CapEqA.of_abs rfl⟩ hwv.2 index depth + obtain ⟨e1, -, hrel⟩ := post_of_okA hl hslot + cases e1 with + | error _ => exact hrel.elim + | ok dsl => + obtain ⟨hd, pd, hpd1, hpd, hcr⟩ := hrel + refine ⟨dsl, pd, hpd1, hpd, hd.symm, ?_, noIrq_of_cnRefA J.j.1.place J.j.1.cv hwv.2 hcr⟩ + unfold cdt.ensure_empty_slot at hempty + rw [capA_eq, bind_ok] at hempty + obtain ⟨b, hb, h⟩ := bind_eq_ok hempty + generalize hc : capA st s dest = c at hb ⊢ + cases c <;> first + | rfl + | (unfold capability.Cap.is_null at hb + have e : b = false := (okinj hb).symm + rw [e] at h + simp only [Bool.false_eq_true, ↓reduceIte] at h + cases okinj h) + +/-- The decoder's `is_irq_active` false is the IRQ state word `0`. -/ +theorem irqInactive_ofRunA {irq : U64} (h : interrupt.is_irq_active st s irq = ok false) : + L.mem s (irqWordA (L.ksb s) irq) = 0#u64 := by + have off : interrupt.state_off irq = ok (irqOffA irq) := by + have const : layout.ks.IRQ_STATE = ok 224#u64 := + eq_of_spec_fd Fl4ProofsA64.P0.Consts.layout.ks.IRQ_STATE.spec + unfold interrupt.state_off irqOffA + simp only [lift, bind_ok, const] + unfold interrupt.is_irq_active interrupt.irq_state at h + rw [off, bind_ok, eq_of_spec_fd (obj.ks_get.spec (L := L) s _), bind_ok] at h + have e := okinj h + rw [interrupt.IRQ_INACTIVE] at e + unfold irqWordA + simpa [bne_eq_false_iff_eq] using e + +/-- An SPI / banked id up to `max_spi` names an IRQ below 5376. -/ +theorem irqOf_ltA (hM : MaxSpi ar) {id cpu max irq : U64} (hm : ar.max_spi a = ok max) (hi : ¬ id > max) + (hnum : layout.irq.of id cpu = ok irq) : irq.val < 5376 := + post_of_okA (P := fun i : U64 => i.val < 5376) (Fl4ProofsA64.P1.layout.irq.of.lt cpu (le1019_of hi (post_of_okA (P := fun m : U64 => m.val ≤ 1019) (hM a) hm))) hnum + +/-- An LPI `n < NUM_LPIS` is an IRQ below 5376. -/ +theorem irqOfLpi_ltA {n base irq : U64} (hn : ¬ n >= layout.irq.NUM_LPIS) (hb : layout.irq.LPI_BASE = ok base) + (h : base + n = ok irq) : irq.val < 5376 := by + have e : base = 5120#u64 := post_of_okA (P := fun v : U64 => v = 5120#u64) Fl4ProofsA64.P0.Consts.layout.irq.LPI_BASE.spec hb + have hn' : n.val < 256 := by + simp only [layout.irq.NUM_LPIS, ge_iff_le, not_le] at hn; scalar_tac + subst e + have := post_of_okA (P := fun v : U64 => v.val = (5120#u64 : U64).val + n.val) (U64.add_spec (x := 5120#u64) (y := n) (by scalar_tac)) h + scalar_tac + +/-- **`ARMIRQIssueIRQHandlerMSI`'s accepted decode** (module docstring): the checks pass, `msi_map` answers `0`, and the +program is the Restart then `invoke_irq_control` at `msi_map`'s arch state. -/ +theorem irqMsi_acceptA {extra : Aeneas.Std.Array obj.Slot 3#usize} {len buf slot : U64} + {root : capability.Cap} {nid device event index d cpu nodes base irq dest : U64} {a1 : A} + (he : interrupt.extra0 st s extra = ok (some root)) (hlen : len ≥ 6#u64) + (h0 : msg.syscall_arg st s (L.curW s) buf 0#u64 = ok nid) + (h1 : msg.syscall_arg st s (L.curW s) buf 1#u64 = ok device) + (h2 : msg.syscall_arg st s (L.curW s) buf 2#u64 = ok event) + (h3 : msg.syscall_arg st s (L.curW s) buf 3#u64 = ok index) + (h4 : msg.syscall_arg st s (L.curW s) buf 4#u64 = ok d) + (h5 : msg.syscall_arg st s (L.curW s) buf 5#u64 = ok cpu) + (hn : ¬ nid >= layout.irq.NUM_LPIS) + (hks : obj.ks_get st s layout.ks.NUM_NODES = ok nodes) (hcpu : ¬ cpu >= nodes) + (hbase : layout.irq.LPI_BASE = ok base) (hirq : base + nid = ok irq) + (hactive : interrupt.is_irq_active st s irq = ok false) + (hslot : cspace.lookup_target_slot st s root index (d &&& 255#u64) = ok (.Ok dest)) + (hempty : cdt.ensure_empty_slot st s dest = ok (.Ok ())) + (hmsi : ar.msi_map a irq device event cpu = ok (0#u64, a1)) : + interrupt.decode_irq_control st ar s a fl4_abi.label.ARMIRQIssueIRQHandlerMSI len slot buf extra = (do + let s1 ← sched.set_thread_state st s (L.curW s) sched.TS_RESTART + let (s2, a2) ← interrupt.invoke_irq_control st ar s1 a1 irq dest slot + ok (.Ok (), s2, a2)) := by + unfold interrupt.decode_irq_control + rw [if_pos rfl] + unfold interrupt.decode_irq_control_msi + simp only [cur_thread_ok, bind_ok, he, if_pos hlen, h0, h1, h2, h3, h4, h5, lift, if_neg hn, hks, if_neg hcpu, + hbase, hirq, hactive, hslot, hempty, P1.irq_branch_ok, Bool.false_eq_true, ↓reduceIte, hmsi, uncurry_apply_pair] + all_goals rfl + +/-- **`msi_map` from the decode run** (module docstring): where the checks pass and the MSI decode returns `Ok`, +`msi_map` answered `0` (`irqIssueMsiA`'s `msi_map` equation). -/ +theorem irqMsi_mapA {extra : Aeneas.Std.Array obj.Slot 3#usize} {len buf slot : U64} + {root : capability.Cap} {nid device event index d cpu nodes base irq dest : U64} {s3 : S} {a3 : A} + (he : interrupt.extra0 st s extra = ok (some root)) (hlen : len ≥ 6#u64) + (h0 : msg.syscall_arg st s (L.curW s) buf 0#u64 = ok nid) + (h1 : msg.syscall_arg st s (L.curW s) buf 1#u64 = ok device) + (h2 : msg.syscall_arg st s (L.curW s) buf 2#u64 = ok event) + (h3 : msg.syscall_arg st s (L.curW s) buf 3#u64 = ok index) + (h4 : msg.syscall_arg st s (L.curW s) buf 4#u64 = ok d) + (h5 : msg.syscall_arg st s (L.curW s) buf 5#u64 = ok cpu) + (hn : ¬ nid >= layout.irq.NUM_LPIS) + (hks : obj.ks_get st s layout.ks.NUM_NODES = ok nodes) (hcpu : ¬ cpu >= nodes) + (hbase : layout.irq.LPI_BASE = ok base) (hirq : base + nid = ok irq) + (hactive : interrupt.is_irq_active st s irq = ok false) + (hslot : cspace.lookup_target_slot st s root index (d &&& 255#u64) = ok (.Ok dest)) + (hempty : cdt.ensure_empty_slot st s dest = ok (.Ok ())) + (hrun : interrupt.decode_irq_control st ar s a fl4_abi.label.ARMIRQIssueIRQHandlerMSI len slot buf extra = + ok (.Ok (), s3, a3)) : + ∃ a1, ar.msi_map a irq device event cpu = ok (0#u64, a1) := by + unfold interrupt.decode_irq_control at hrun + rw [if_pos rfl] at hrun + unfold interrupt.decode_irq_control_msi at hrun + simp only [cur_thread_ok, bind_ok, he, if_pos hlen, h0, h1, h2, h3, h4, h5, lift, if_neg hn, hks, if_neg hcpu, + hbase, hirq, hactive, hslot, hempty, P1.irq_branch_ok, Bool.false_eq_true, ↓reduceIte] at hrun + obtain ⟨⟨code, a1⟩, hmsi, hrest⟩ := bind_eq_ok hrun + by_cases hc : code = 0#u64 + · exact ⟨a1, by rw [hmsi, hc]⟩ + exfalso + simp only [uncurry_apply_pair] at hrest + split at hrest + · exact hc rfl + · cases okinj hrest + · cases okinj hrest + · simp only [error.illegal, bind_ok] at hrest + cases okinj hrest + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3LiftIrqIssueA.lean b/proofs/Fl4ProofsA64/P3/Stage3LiftIrqIssueA.lean new file mode 100644 index 000000000..81488ff9f --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3LiftIrqIssueA.lean @@ -0,0 +1,480 @@ +/- +P3 (aarch64, A64-P3 Lift 1b): **`IRQControl` issue at record level**, the Restart, an optional machine/arch step, then +`interrupt::invoke_irq_control` (`set_irq_state(IRQ_SIGNAL)`, `cdt::insert` of `IrqHandler irq` from the control slot). + +* `DelXA6.machA`: the deletion loop's record across a step changing only the arch state and the spec's machine state + (`JStage3TA.machineA`), the issue's `configure_irq` / `msi_map`. +* `JSyscallA.irqIssueStepA`: the restart (`decDel_restartA`, as Clear / Set), the machine step, the IRQ-state write + (`DelXA6.irqSignalA`), `DelXA6G.toStage3A` with `SysL` / `SymL` from A64 P1's `interrupt.set_irq_state.sysL`, and the + insertion by `JStage3A.capInsertA` (the new cap derives from the control cap by `DerivA.irqH`; `J5A.insGenA`'s fresh + handler of an inactive IRQ). The final-state `SysL` / `SymL` / J7 are the arm's (`F`, from `decodeFinA` on its label). +* `JSyscallA.irqIssuePlainA`, `irqTriggerA`, `irqTriggerCoreA`, `irqMsiA`: the labels. + +Named premises: [P] `mask_irq` keeps `AR` (`VcpuArchKeepsA`), and for the labels `configure_irq` / `msi_map` keep `AR` +(`IrqCfgKeepA`, `MsiKeepA`) and the spec's machine operations exist (`IrqCfgKeepA`); [J] as `irqSetA` (`hCsS`-style +`ss` / `sd` slots, `hpdn`: the handler slot's CNode is not an IRQ node); `hact` the decoder's `is_irq_active` false. New +module. +-/ +import Fl4ProofsA64.P3.Stage3LiftIrqSetA +import Fl4ProofsA64.P3.Stage3LiftIrqSigA +import Fl4ProofsA64.P3.MachKeepA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Vcpu Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {m : Fl4SpecA64.McState} {ipa : U64} + +theorem handlerFromControlIA (irq : U64) : UA (.IrqHandler irq) .IrqControl := by + refine ⟨?_, ?_, ?_, rfl, ?_⟩ + · apply okinj + exact (physA_eq (.IrqHandler irq)).symm.trans (physA_eq .IrqControl) + · apply okinj + exact (ptrA_eq (.IrqHandler irq)).symm.trans (ptrA_eq .IrqControl) + · apply okinj + exact (sizeA_eq (.IrqHandler irq)).symm.trans (sizeA_eq .IrqControl) + · intro p b i e h + cases h + +theorem sraA_ctlA (irq : U64) : sraA .IrqControl (.IrqHandler irq) = true := by + apply sraA_of_eq; simp [objects.same_region_as] + +theorem isPFA_ctlA (rv : Bool) (irq : U64) : + isPFA rv .IrqControl (.IrqHandler irq) (revFA (.IrqHandler irq) .IrqControl) = rv := by + unfold isPFA; simp [sraA_ctlA, isBadgedA] + +theorem sbp_ctlA (io : Bool) (irq : U64) : + decide (Fl4SpecA64.shouldBeParentOf (absCapA .IrqControl) io (absCapA (.IrqHandler irq)) + (decide (Fl4SpecA64.isCapRevocable (absCapA (.IrqHandler irq)) (absCapA .IrqControl)))) = io := by + cases io + · simp [Fl4SpecA64.shouldBeParentOf] + · exact decide_eq_true ⟨rfl, Or.inr (Or.inl ⟨_, rfl⟩), trivial⟩ + +set_option maxHeartbeats 2000000 in +/-- **J5 across `insert_with_cap` of a fresh `IrqHandler irq` from the control cap** (`J5A.insGenA`'s twin on A64 P1's +`CtlCtxA`): the IRQ is active at `s` and no `OS` slot holds its handler. -/ +theorem J5A.insCtlA {s : S} {σ σ' : State Unit} {m m' : McState} + (hO : OSOK H.tcbs H.omega H.frame OS) (J : J5A st Z H OS s σ m) + (hT : TreeA st OS Z.G.chains Z.G.par s) {src dest irq : U64} (hsrc : src ∈ OS) (hd : dest ∈ OS) + (hSs : IsSlotA Z.th src) (hSd : IsSlotA Z.th dest) (hcs : capA st s src = .IrqControl) + (hdn : capA st s dest = .Null) (hdc : obj.sext48 dest = ok dest) + (hfr : ∀ y ∈ Z.G.chains.flatten, capA st s y ≠ .IrqHandler irq) (hi : irq.val < 5376) + (hact : L.mem s (irqStAddrA (L.ksb s) irq) ≠ 0#u64) + (hst : ∀ s', WritesIn st s s' (W7.CapW H.tcbs {b | InOS OS b} ∅) → + ∀ i : U64, i.val < 5376 → L.mem s' (irqStAddrA (L.ksb s) i) = L.mem s (irqStAddrA (L.ksb s) i)) : + cdt.insert_with_cap st s (.IrqHandler irq) (capA st s src) src dest ⦃ s' => + WritesIn st s s' (W7.CapW H.tcbs {b | InOS OS b} ∅) ∧ capA st s' dest = .IrqHandler irq ∧ + FullA (capA st s' src) (capA st s src) ∧ (∀ y ∈ OS, y ≠ src → y ≠ dest → capA st s' y = capA st s y) ∧ + J5A st { Z with G := ⟨insG src dest Z.G.chains, insParA Z.G.par src dest + (isPFA (Fl4ProofsA64.P1.W7.R.ST.revA st s src) (capA st s src) (.IrqHandler irq) + (revFA (.IrqHandler irq) (capA st s src)))⟩ } H OS s' σ' m' ⦄ := by + have hsn : capA st s src ≠ .Null := by rw [hcs]; simp + have hw : CapRt.EncWf (capability.Cap.IrqHandler irq) := trivial + have F := hT.lk.lkF hO.nz + have inOS : ∀ y ∈ Z.G.chains.flatten, y ∈ OS := fun y hy => ((hT.lk.members y).1 hy).1 + have hsG : src ∈ Z.G.chains.flatten := (hT.lk.members _).2 ⟨hsrc, hsn⟩ + have hdG : dest ∉ Z.G.chains.flatten := fun h => ((hT.lk.members _).1 h).2 hdn + have hsd : src ≠ dest := fun e => hdG (e ▸ hsG) + have hnN : NxA st s src ≠ 0#u64 → NxA st s src ∈ OS := fun h => inOS _ (F.nxm _ hsG h) + refine WP.spec_mono (WP.spec_and (WP.spec_and + (cdt.insert_with_cap.lrF (osokA hO) (LR.self s) hsrc hd hsd hdc rfl hsn hw hnN) + (cdt.insert_with_cap.srcF hO hsrc hd hsd rfl hw hnN)) + (insWC_bitsA hO hsrc hd hsd rfl hw hnN)) ?_ + rintro s' ⟨⟨⟨w, hL, ed, -, -, ek, -⟩, hfull⟩, hbk, hrd, hfd⟩ + have hcs' : capA st s' src = .IrqControl := by + rcases hfull with e | ⟨p, b, i, d, i1, h1, -⟩ + · rw [e, hcs] + · rw [hcs] at h1; cases h1 + have aOS : ∀ a, (a = dest ∨ a ∈ Z.G.chains.flatten) → a ∈ OS := fun a ha => ha.elim (fun e => e ▸ hd) (inOS a) + have C : CtlCtxA st OS Z.G.chains Z.G.par s s' src dest irq := by + refine ⟨hT.lk, hT.tree, J.1.1, hsG, hdG, hcs, hfr, fun a ha => ?_, fun a ha => ?_, fun a ha => ?_, fun a ha => ?_⟩ + · have e := (hL a (aOS a ha)).2 + rw [e] + by_cases e1 : a = dest <;> by_cases e2 : a = src <;> simp [e1, e2, hsd, Ne.symm hsd] + · by_cases e1 : a = dest + · rw [if_pos e1, e1]; exact ed + · rw [if_neg e1] + by_cases e2 : a = src + · rw [e2, hcs', hcs] + · exact ek a (aOS a ha) e2 e1 + · by_cases e1 : a = dest + · rw [if_pos e1, e1]; exact hrd.trans (by rw [hcs]; rfl) + · rw [if_neg e1]; exact (hbk a (aOS a ha) e1).1 + · by_cases e1 : a = dest + · rw [if_pos e1, e1]; exact hfd.trans (by rw [hcs]; rfl) + · rw [if_neg e1]; exact (hbk a (aOS a ha) e1).2 + have hk : ∀ y ∈ Z.G.chains.flatten, ∀ i, capA st s' y = .IrqHandler i ↔ capA st s y = .IrqHandler i := + fun y hy i => by + by_cases e2 : y = src + · rw [e2, hcs', hcs] + · rw [ek y (inOS y hy) e2 (fun e => hdG (e ▸ hy))] + have hdi : ∀ i, capA st s' dest = .IrqHandler i → i = irq := fun i h => + capability.Cap.IrqHandler.inj (h.symm.trans ed) + refine ⟨w, ed, hfull, ek, ⟨?_, ?_, ?_⟩, J.2.1, fun y hy hns => ?_⟩ + · rw [hcs, show revFA (.IrqHandler irq) .IrqControl = revFA (.IrqHandler irq) .IrqControl from rfl, isPFA_ctlA] + exact C.inv + · exact Fl4ProofsA64.P1.W7.R.ST.IrqRunA.ins J.1.2.1 hT.lk.nodup hsG hdG hk + (fun i h => Or.inr (by rw [hdi i h]; exact hfr)) (fun i h => by rw [hcs] at h; cases h) + · exact HandlersActiveA.insGenA J.1.2.2 hsG (fun y hy i h => (hk y hy i).1 h) + (fun i h => Or.inr ⟨by rw [hdi i h]; exact hi, by rw [hdi i h]; exact hact⟩) w.1 (hst s' w) + · have f1 : y ≠ src := fun e => hns (e ▸ hSs) + have f2 : y ≠ dest := fun e => hns (e ▸ hSd) + rw [ek y hy f1 f2]; exact J.2.2 y hy hns + +set_option maxHeartbeats 2000000 in +/-- **The loop record across a step changing only the arch state and the spec's machine state** (module docstring). -/ +theorem DelXA6.machA {s : S} {a a2 : A} {σ : State Unit} {G : MdbGhostA} {ms : MachineState} + (hm : ms.underlyingMemory = σ.machineState.underlyingMemory) (har : ∀ hw, AR a hw → AR a2 hw) + (D : DStateA st Z.th OS s σ G) + (X : DelXA6 st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st (J7R2A st ar ipa)) s a σ G) : + DStateA st Z.th OS s { σ with machineState := ms } G ∧ + DelXA6 st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st (J7R2A st ar ipa)) s a2 + { σ with machineState := ms } G := by + obtain ⟨⟨Q, R, hw, Ac, Bf, stack, J, Y⟩, hPA, hZT, hBH, hCT, hFx⟩ := X + have J' := J.machineA hm (har hw J.j.1.ar) + have osk : OSKeep st OS s s := fun y hy => ⟨rfl, rfl, rfl, rfl⟩ + have X' : Std6A st Z H OS Bf s { σ with machineState := ms } := + ksKeep_std6A J Y D.sub osk rfl (fun _ _ _ => rfl) rfl hm + exact DelXA.stepPpPA J' X' D osk rfl hPA hBH hCT hFx + +set_option maxHeartbeats 4000000 in +/-- **The issue's restart, machine step, IRQ-state write and insertion** (module docstring). -/ +theorem JSyscallA.irqIssueStepA [PlainStore st] (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) + s a σ m) + (hCnt : CounterStepA σ) + {slot irq dest : U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hmask : ∀ a hw, AR a hw → ar.mask_irq a irq false ⦃ a' => AR a' hw ⦄) + (hsl : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .IrqControl) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) + (hvs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → Aligned b ∧ b ≠ stA (L.curW s)) + (hvc : ∀ v ∈ Z.V, stA (L.curW s) ≠ v ∧ stA (L.curW s) ≠ core.num.U64.wrapping_add v 24#u64) + (site : DelSiteA st Z.th OS s σ Z.G.chains) + (hi : irq.val < 5376) (hact : L.mem s (irqWordA (L.ksb s) irq) = 0#u64) + (ss sd : Fl4SpecA64.CslotPtr) {ps pd : U64} (hCs : ss.1 = specPtr ps ∧ Z.th ps = some (.cnode ss.2.length)) + (hCd : sd.1 = specPtr pd ∧ Z.th pd = some (.cnode sd.2.length)) + (hss : cslotAddrA ss = slot) (hsd : cslotAddrA sd = dest) (hdn : capA st s dest = .Null) + (hpdn : ¬ IrqNodeA (L.ksb s) pd) + {a1 : A} {ms0 : MachineState} (hm0 : ms0.underlyingMemory = σ.machineState.underlyingMemory) + (hl0 : ms0.lastMachineTime = σ.machineState.lastMachineTime) + (ha1 : ∀ hw, AR a hw → AR a1 hw) + (F : ∀ s3 a3, interrupt.invoke_irq_control st ar (L.wr s (stA (L.curW s)) sched.TS_RESTART) a1 irq dest slot = + ok (s3, a3) → ∀ (σX : State Unit) (mX : McState), ∃ Ac' : Set U64, SysL st H Z.Cs OS ia ie s3 ∧ + SymL st H Z.Cs s3 ∧ BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac' s3 ∧ + Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s3 ∧ J7A st (J7R2A st ar hPl.ipa) Z H OS s3 σX mX) : + ∃ s3 a3 xt σ3, + sched.set_thread_state st s (L.curW s) sched.TS_RESTART = ok (L.wr s (stA (L.curW s)) sched.TS_RESTART) ∧ + interrupt.invoke_irq_control st ar (L.wr s (stA (L.curW s)) sched.TS_RESTART) a1 irq dest slot = ok (s3, a3) ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), stUpdA σ σ.curThread xt .restart)}, False) ∧ + ((), σ3) ∈ ((setIrqState (Z := Unit) .irqSignal (BitVec.ofNat 13 irq.val) >>= fun _ => + capInsert (absCapA (.IrqHandler irq)) ss sd) { stUpdA σ σ.curThread xt .restart with machineState := ms0 }).1 ∧ + σ3.curThread = σ.curThread ∧ A64RelS3 st AR ar hPl.ipa n Z.Cs s3 a3 σ3 m := by + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hsl hcov J.actW J.running J.cur + have hT : TreeA st OS Z.G.chains Z.G.par s := J.j.1.cdt.tree.grow J.j5.2.1 (fun x hx hn => J.j5.2.2 x hx hn) + obtain ⟨xt, heq, hset, D1, W1⟩ := decDel_restartA J.toJEntA (J.toJStage3A.sysPlace hext hsl hcov) J.symL J.running + RS.cur hvs hvc hT J.j5.2.1 J.j5.2.2 site + (X := DelWTopA st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar hPl.ipa (fun _ _ => True) s) + (fun x hkx => J.delRestartWA hPl hsl hcov hext trivial hCnt x hkx) + obtain ⟨xr, -, -, -, E1, Y1, hOK, -, -⟩ := J.toJEntA.restartA (J.toJStage3A.sysPlace hext hsl hcov) J.symL + J.running RS.cur hvs hvc + have X6 := W1.2.2.1.2.1 + have sv := W1.2.2.1.2.2 + obtain ⟨D2, X7⟩ := DelXA6.machA (ms := ms0) (σ := stUpdA σ σ.curThread xt .restart) hm0 ha1 D1 X6 + have hno := irqInactive_noHandlerA J.toJStage3A hact + have hno1 : ∀ y ∈ OS, capA st (L.wr s (stA (L.curW s)) sched.TS_RESTART) y ≠ .IrqHandler irq := + fun y hy h => hno y hy ((hOK.capA hy).symm.trans h) + obtain ⟨⟨s1, a1'⟩, e1, hr1, hk1, osk1, D3, X8⟩ := spec_existsA (DelXA6.irqSignalA hi hmask hno1 D2 X7) + -- `SysL` / `SymL` after the IRQ-state write + obtain ⟨-, -, sysl1, symL1⟩ := post_of_okA (interrupt.set_irq_state.sysL hA E1.sys.ent.heap E1.sys.ent.os a1 + interrupt.IRQ_SIGNAL irq (BXI.blk E1.sys.ent.bxi) E1.ap.irqSt hi E1.sys.sysl Y1) e1 + obtain ⟨Q2, R2, hw2, Ac2, Bf2, stack2, J2⟩ := X8.toStage3A sysl1 symL1 + (sv.congr rfl rfl rfl rfl rfl rfl (fun _ _ _ h => h) (fun _ x h => ⟨x, h, rfl⟩) + (fun _ x h => ⟨x, h, rfl, rfl⟩)) + have hAs : AbsSlotA Z.th ss := ⟨ps, hCs.1, Or.inl hCs.2⟩ + have hAd : AbsSlotA Z.th sd := ⟨pd, hCd.1, Or.inl hCd.2⟩ + have hdO : dest ∈ OS := hsd ▸ J2.j5.2.1 _ (hAd.isSlot J2.j.1.place) + have hcs1 : capA st s1 (cslotAddrA ss) = .IrqControl := by + rw [hss]; exact (osk1.capA hsl).trans ((hOK.capA hsl).trans hcap) + have hdn1 : capA st s1 (cslotAddrA sd) = .Null := by + rw [hsd]; exact (osk1.capA hdO).trans ((hOK.capA hdO).trans hdn) + have hksb : L.ksb s1 = L.ksb s := hk1.trans (L.ksb_wr _ _ _) + have hTr2 : TreeA st OS Z.G.chains Z.G.par s1 := J2.j.1.cdt.tree.grow J2.j5.2.1 (fun x hx hn => J2.j5.2.2 x hx hn) + have hinv : ∀ s3, cdt.insert st s1 (.IrqHandler irq) (cslotAddrA ss) (cslotAddrA sd) = ok s3 → + interrupt.invoke_irq_control st ar (L.wr s (stA (L.curW s)) sched.TS_RESTART) a1 irq dest slot = ok (s3, a1') := + fun s3 hs3 => by + rw [hss, hsd] at hs3 + unfold interrupt.invoke_irq_control + rw [e1, bind_ok] + change (do + let s2' ← cdt.insert st s1 (capability.Cap.IrqHandler irq) slot dest + ok (s2', a1')) = _ + rw [hs3] + exact bind_ok _ _ + have hirq := J2.j.1.irq + have hactive : L.mem s1 (irqStAddrA (L.ksb s1) irq) ≠ 0#u64 := fun h0 => by + have e : (irqSigPostA ({ stUpdA σ σ.curThread xt .restart with machineState := ms0 }) irq).interruptStates + (BitVec.ofNat 13 irq.val) = irqStOfA (L.mem s1 (irqWordA (L.ksb s1) irq)) := hirq.st irq hi + have e2 : (irqSigPostA ({ stUpdA σ σ.curThread xt .restart with machineState := ms0 }) irq).interruptStates + (BitVec.ofNat 13 irq.val) = .irqSignal := Function.update_self .. + have h0' : L.mem s1 (irqWordA (L.ksb s1) irq) = 0#u64 := h0 + rw [e2, h0'] at e + exact absurd e (by simp [irqStOfA]) + have hpar : Fl4SpecA64.shouldBeParentOf .irqControlCap true (.irqHandlerCap (BitVec.ofNat 13 irq.val)) + (decide (Fl4SpecA64.isCapRevocable (.irqHandlerCap (BitVec.ofNat 13 irq.val)) .irqControlCap)) := + ⟨rfl, Or.inr (Or.inl ⟨_, rfl⟩), trivial⟩ + have hfr1 : ∀ y ∈ Z.G.chains.flatten, capA st s1 y ≠ .IrqHandler irq := fun y hy hc => by + have hyO := ((hTr2.lk.members y).1 hy).1 + exact hno1 y hyO ((osk1.capA hyO).symm.trans hc) + obtain ⟨s3, σ3, hs3, hm3, hbk3, -, J3⟩ := JStage3A.capInsertGenA (J := J2) (nc := .IrqHandler irq) + (anc := absCapA (.IrqHandler irq)) ss sd (hCs := hCs) (hCd := hCd) (hsn := by rw [hcs1]; simp) (hdn := hdn1) + (hdc := OSOK.canon J2.sys.ent.os J2.winOK (hsd ▸ hdO)) (hwf := hi) (hanc := CapEqA.of_abs rfl) + (hw := trivial) (hn := by simp) (hD := by rw [hcs1]; exact DerivA.irqH irq) + (hal := by rw [hcs1]; exact handlerFromControlIA irq) (hk := by rw [hcs1]; trivial) + (hut := fun h => by rw [hcs1] at h; simp [isUtA] at h) + (hdo := Or.inl (by rw [hCd.1, fl4Ptr_specPtr, hksb]; exact hpdn)) + (hsci := ⟨fun c _ => by simp [absCapA], fun b => by simp [absCapA], fun _ _ _ h => by simp [absCapA] at h⟩) + (hno := ⟨(fun _ h => by cases h), (fun _ _ _ _ _ h => by cases h), (fun _ _ _ h => by cases h), + (fun _ _ _ h => by cases h), trivial⟩) + (hoffN := fun p r g b e => by cases e) + (hub := fun h => by rw [hcs1] at h; simp [isUtA] at h) + (hsib := by rw [hcs1]; exact hsib_ofA J2.j.1.cdt hAs hpar) + (hbeq := by + rw [hcs1] + unfold insBA + rw [J2.j.1.cdt.orig ss hAs] + exact (sbp_ctlA _ irq).trans (isPFA_ctlA _ irq).symm) + (hJ5 := fun hst σ' => by + have hsS := hAs.isSlot J2.j.1.place + have hdS := hAd.isSlot J2.j.1.place + have e := J5A.insCtlA (σ' := σ') (m := m) (m' := m) J2.sys.ent.os J2.j5 hTr2 (J2.j5.2.1 _ hsS) + (J2.j5.2.1 _ hdS) hsS hdS hcs1 hdn1 (OSOK.canon J2.sys.ent.os J2.winOK (hsd ▸ hdO)) hfr1 hi hactive hst + rw [hcs1] at e ⊢ + exact e) + (symL := fun s3 hs3 => (F s3 a1' (hinv s3 hs3) σ m).choose_spec.2.1) + (sysl := fun s3 hs3 => (F s3 a1' (hinv s3 hs3) σ m).choose_spec.1) + (j7 := fun s3 σ3 hs3 _ => (F s3 a1' (hinv s3 hs3) σ3 m).choose_spec.2.2.2.2) + obtain ⟨x, hx, -, hset', -⟩ := J.restartMachA hsl hcov hext + have e1 : stUpdA σ σ.curThread xt .restart = stUpdA σ σ.curThread x .restart := by + have := Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst (hset.symm.trans hset')) + exact (Prod.mk.inj this).2 + have K : Sched.BodyKeepA (stUpdA σ σ.curThread x .restart) σ3 := by + rw [← e1] + exact (Sched.BodyKeepA.ofMach (stUpdA σ σ.curThread xt .restart) ms0 hl0).trans + ((show Sched.BodyKeepA { stUpdA σ σ.curThread xt .restart with machineState := ms0 } (irqSigPostA { stUpdA σ σ.curThread xt .restart with machineState := ms0 } irq) from + ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, fun _ _ _ h => h, fun _ x h => ⟨x, h, rfl⟩⟩).trans hbk3) + refine ⟨s3, a1', xt, σ3, heq, hinv s3 hs3, hset, ?_, K.cur, A64RelS3.ofJ J3 (Sched.BodyOKA.ofKeep J hx K)⟩ + exact mem_bind_ndD (setIrqSignal_specA { stUpdA σ σ.curThread xt .restart with machineState := ms0 } irq) hm3 + +theorem decArmIrqCtlA (lbl : U64) : DecArmS1aEnt .IrqControl lbl := by + simp [DecArmS1aEnt, DecArmQH, DecArmQHK, DecArmDelQ] + +set_option maxHeartbeats 4000000 in +/-- **An `IRQControl` issue arm** (module docstring): `irqIssueStepA` with its final-state facts from `decodeFinA` on the +arm's label; `hprog`: the decode's program ends in `invoke_irq_control` at the restarted state. -/ +theorem JSyscallA.irqIssueArmA [PlainStore st] (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (hCnt : Sched.CounterStepA σ) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) + s a σ m) + (hfm : A64FrameMasks st s) + {slot irq dest len buf : U64} {blk call don fp : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} {lbl : U64} + (hmask : ∀ a hw, AR a hw → ar.mask_irq a irq false ⦃ a' => AR a' hw ⦄) + (hsl : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .IrqControl) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hvs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → Aligned b ∧ b ≠ stA (L.curW s)) + (hvc : ∀ v ∈ Z.V, stA (L.curW s) ≠ v ∧ stA (L.curW s) ≠ core.num.U64.wrapping_add v 24#u64) + (site : DelSiteA st Z.th OS s σ Z.G.chains) + (hi : irq.val < 5376) (hact : L.mem s (irqWordA (L.ksb s) irq) = 0#u64) + (ss sd : Fl4SpecA64.CslotPtr) {ps pd : U64} (hCs : ss.1 = specPtr ps ∧ Z.th ps = some (.cnode ss.2.length)) + (hCd : sd.1 = specPtr pd ∧ Z.th pd = some (.cnode sd.2.length)) + (hss : cslotAddrA ss = slot) (hsd : cslotAddrA sd = dest) (hdn : capA st s dest = .Null) + (hpdn : ¬ IrqNodeA (L.ksb s) pd) + {a1 : A} {ms0 : MachineState} (hm0 : ms0.underlyingMemory = σ.machineState.underlyingMemory) + (hl0 : ms0.lastMachineTime = σ.machineState.lastMachineTime) + (ha1 : ∀ hw, AR a hw → AR a1 hw) + (hprog : ∀ s3 a3, interrupt.invoke_irq_control st ar (L.wr s (stA (L.curW s)) sched.TS_RESTART) a1 irq dest slot = + ok (s3, a3) → + syscall.decode_invocation st ar s a lbl len slot .IrqControl blk call don fp buf extra = ok (.Ok (), s3, a3)) : + ∃ s3 a3 xt σ3, + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don fp buf extra = + ok (.Ok (), s3, a3) ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), stUpdA σ σ.curThread xt .restart)}, False) ∧ + ((), σ3) ∈ ((setIrqState (Z := Unit) .irqSignal (BitVec.ofNat 13 irq.val) >>= fun _ => + capInsert (absCapA (.IrqHandler irq)) ss sd) { stUpdA σ σ.curThread xt .restart with machineState := ms0 }).1 ∧ + σ3.curThread = σ.curThread ∧ A64RelS3 st AR ar hPl.ipa n Z.Cs s3 a3 σ3 m := by + obtain ⟨s3, a3, xt, σ3, -, hinv, hset, hmem, hcur, hR⟩ := J.irqIssueStepA (hCnt := hCnt) hA hPl hmask hsl hcov hcap hext hvs hvc site hi + hact ss sd hCs hCd hss hsd hdn hpdn hm0 hl0 ha1 (fun s3 a3 hinv σX mX => + (J.decodeFinA hA hM hPl hfm hsl hcov hcap hext hSC (lbl := lbl) (decArmIrqCtlA lbl) + (fun _ h _ => by cases h) (fun _ h => by cases h) (len := len) (blk := blk) (call := call) (don := don) + (fp := fp) (buf := buf) (r := .Ok ()) (s3 := s3) (a3 := a3) (hprog s3 a3 hinv) σX mX).2) + exact ⟨s3, a3, xt, σ3, by rw [hcap]; exact hprog s3 a3 hinv, hset, hmem, hcur, hR⟩ + +set_option maxHeartbeats 4000000 in +/-- **`IRQIssueIRQHandler`** (label 29) at a syscall entry. -/ +theorem JSyscallA.irqIssuePlainA [PlainStore st] (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (hCnt : Sched.CounterStepA σ) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) + s a σ m) + (hfm : A64FrameMasks st s) + {slot irq dest len buf : U64} {blk call don fp : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hmask : ∀ a hw, AR a hw → ar.mask_irq a irq false ⦃ a' => AR a' hw ⦄) + (hsl : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .IrqControl) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hvs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → Aligned b ∧ b ≠ stA (L.curW s)) + (hvc : ∀ v ∈ Z.V, stA (L.curW s) ≠ v ∧ stA (L.curW s) ≠ core.num.U64.wrapping_add v 24#u64) + (site : DelSiteA st Z.th OS s σ Z.G.chains) + (hi : irq.val < 5376) (hact : L.mem s (irqWordA (L.ksb s) irq) = 0#u64) + (ss sd : Fl4SpecA64.CslotPtr) {ps pd : U64} (hCs : ss.1 = specPtr ps ∧ Z.th ps = some (.cnode ss.2.length)) + (hCd : sd.1 = specPtr pd ∧ Z.th pd = some (.cnode sd.2.length)) + (hss : cslotAddrA ss = slot) (hsd : cslotAddrA sd = dest) (hdn : capA st s dest = .Null) + (hpdn : ¬ IrqNodeA (L.ksb s) pd) + (hdec : interrupt.decode_irq_control st ar s a fl4_abi.label.IRQIssueIRQHandler len slot buf extra = + irqIssuePlainProgramA st ar s a (L.curW s) irq dest slot) : + ∃ s3 a3 σ1 σ3, + syscall.decode_invocation st ar s a fl4_abi.label.IRQIssueIRQHandler len slot (capA st s slot) blk call don fp + buf extra = ok (.Ok (), s3, a3) ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + (.ok (), σ3) ∈ ((invokeFl4IrqControl (Z := Unit) + (.spec (.irqControl (BitVec.ofNat 13 irq.val) sd ss))).run σ1).1 ∧ + σ3.curThread = σ.curThread ∧ A64RelS3 st AR ar hPl.ipa n Z.Cs s3 a3 σ3 m := by + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hsl hcov J.actW J.running J.cur + obtain ⟨-, -, heq0, -, -, -, -, -, -⟩ := J.toJEntA.restartA (J.toJStage3A.sysPlace hext hsl hcov) J.symL + J.running RS.cur hvs hvc + obtain ⟨s3, a3, xt, σ3, hd, hset, hmem, hcur, hR⟩ := J.irqIssueArmA (hCnt := hCnt) hA hM hPl hfm (lbl := fl4_abi.label.IRQIssueIRQHandler) + (len := len) (buf := buf) (blk := blk) (call := call) (don := don) (fp := fp) hmask hsl hcov hcap hext hSC hvs hvc + site hi hact ss sd hCs hCd hss hsd hdn hpdn (a1 := a) (ms0 := σ.machineState) rfl rfl (fun _ h => h) + (fun s3 a3 hinv => by + rw [decodeIrqControl_programA, hdec] + unfold irqIssuePlainProgramA + rw [heq0, bind_ok, hinv] + exact bind_ok _ _) + exact ⟨s3, a3, _, σ3, hd, hset, mem_liftED hmem, hcur, hR⟩ + +/-- **[P] `configure_irq` keeps `AR`** (the routed issues' arch step; `msi_map`'s is `MsiKeepA`). -/ +structure IrqCfgKeepA {A C : Type} (arI : machine.Arch A) (AR : A → C → Prop) : Prop where + configure_irq : ∀ a irq trig cpu c, AR a c → arI.configure_irq a irq trig cpu ⦃ a' => AR a' c ⦄ + +set_option maxHeartbeats 4000000 in +/-- **`ARMIRQIssueIRQHandlerTrigger` / `TriggerCore`** (labels 65, 66) at a syscall entry: `configure_irq`, then the +issue. -/ +theorem JSyscallA.irqIssueRoutedA [PlainStore st] (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (hCnt : Sched.CounterStepA σ) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) + s a σ m) + (hfm : A64FrameMasks st s) + {slot irq dest len buf : U64} {blk call don fp : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hmask : ∀ a hw, AR a hw → ar.mask_irq a irq false ⦃ a' => AR a' hw ⦄) + (hsl : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .IrqControl) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hvs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → Aligned b ∧ b ≠ stA (L.curW s)) + (hvc : ∀ v ∈ Z.V, stA (L.curW s) ≠ v ∧ stA (L.curW s) ≠ core.num.U64.wrapping_add v 24#u64) + (site : DelSiteA st Z.th OS s σ Z.G.chains) + (hi : irq.val < 5376) (hact : L.mem s (irqWordA (L.ksb s) irq) = 0#u64) + (ss sd : Fl4SpecA64.CslotPtr) {ps pd : U64} (hCs : ss.1 = specPtr ps ∧ Z.th ps = some (.cnode ss.2.length)) + (hCd : sd.1 = specPtr pd ∧ Z.th pd = some (.cnode sd.2.length)) + (hss : cslotAddrA ss = slot) (hsd : cslotAddrA sd = dest) (hdn : capA st s dest = .Null) + (hpdn : ¬ IrqNodeA (L.ksb s) pd) + {lbl : U64} {edge : Bool} {cpu : U64} (hK : IrqCfgKeepA ar AR) {a1 : A} + (hcfg : ar.configure_irq a irq edge cpu = ok a1) + (hdec : interrupt.decode_irq_control st ar s a lbl len slot buf extra = + irqIssueProgramA st ar s a (L.curW s) irq dest slot edge cpu) : + ∃ s3 a3 σ1 σ3, + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don fp buf extra = + ok (.Ok (), s3, a3) ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + (.ok (), σ3) ∈ ((invokeFl4IrqControl (Z := Unit) + (.triggerCore (BitVec.ofNat 13 irq.val) sd ss edge cpu.bv)).run σ1).1 ∧ + σ3.curThread = σ.curThread ∧ A64RelS3 st AR ar hPl.ipa n Z.Cs s3 a3 σ3 m := by + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hsl hcov J.actW J.running J.cur + obtain ⟨-, -, heq0, -, -, -, -, -, -⟩ := J.toJEntA.restartA (J.toJStage3A.sysPlace hext hsl hcov) J.symL + J.running RS.cur hvs hvc + obtain ⟨⟨u, ms0⟩, hms⟩ := (machineOpLift_restA (setIRQTriggerCoreImpl (BitVec.ofNat 13 irq.val) edge cpu.bv) + σ.machineState).2.1 + obtain ⟨mr, e0⟩ := (machineOpLift_restA (setIRQTriggerCoreImpl (BitVec.ofNat 13 irq.val) edge cpu.bv) + σ.machineState).2.2 _ hms + have hm0 : ms0.underlyingMemory = σ.machineState.underlyingMemory := by + have e0' : ms0 = { σ.machineState with machineStateRest := mr } := e0 + rw [e0'] + have hl0 : ms0.lastMachineTime = σ.machineState.lastMachineTime := by + have e0' : ms0 = { σ.machineState with machineStateRest := mr } := e0 + rw [e0'] + obtain ⟨s3, a3, xt, σ3, hd, hset, hmem, hcur, hR⟩ := J.irqIssueArmA (hCnt := hCnt) hA hM hPl hfm (lbl := lbl) (len := len) (buf := buf) + (blk := blk) (call := call) (don := don) (fp := fp) hmask hsl hcov hcap hext hSC hvs hvc site hi hact ss sd hCs hCd + hss hsd hdn hpdn (a1 := a1) (ms0 := ms0) hm0 hl0 (fun hw h => post_of_okA (P := fun a' => AR a' hw) (hK.configure_irq a irq edge cpu hw h) hcfg) + (fun s3 a3 hinv => by + rw [decodeIrqControl_programA, hdec] + unfold irqIssueProgramA + rw [heq0, bind_ok, hcfg, bind_ok, hinv] + exact bind_ok _ _) + refine ⟨s3, a3, _, σ3, hd, hset, ?_, hcur, hR⟩ + apply mem_liftED + exact mem_bind_ndD (irqMachine_memA (setIRQTriggerCore (BitVec.ofNat 13 irq.val) edge cpu.bv) (stUpdA σ σ.curThread xt .restart) hms) hmem + +set_option maxHeartbeats 4000000 in +/-- **`ARMIRQIssueIRQHandlerMSI`** (label 67) at a syscall entry: `msi_map` (before the Restart), then the issue. -/ +theorem JSyscallA.irqIssueMsiA [PlainStore st] (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (hCnt : Sched.CounterStepA σ) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) + s a σ m) + (hfm : A64FrameMasks st s) + {slot irq dest len buf : U64} {blk call don fp : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hmask : ∀ a hw, AR a hw → ar.mask_irq a irq false ⦃ a' => AR a' hw ⦄) + (hsl : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .IrqControl) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hvs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → Aligned b ∧ b ≠ stA (L.curW s)) + (hvc : ∀ v ∈ Z.V, stA (L.curW s) ≠ v ∧ stA (L.curW s) ≠ core.num.U64.wrapping_add v 24#u64) + (site : DelSiteA st Z.th OS s σ Z.G.chains) + (hi : irq.val < 5376) (hact : L.mem s (irqWordA (L.ksb s) irq) = 0#u64) + (ss sd : Fl4SpecA64.CslotPtr) {ps pd : U64} (hCs : ss.1 = specPtr ps ∧ Z.th ps = some (.cnode ss.2.length)) + (hCd : sd.1 = specPtr pd ∧ Z.th pd = some (.cnode sd.2.length)) + (hss : cslotAddrA ss = slot) (hsd : cslotAddrA sd = dest) (hdn : capA st s dest = .Null) + (hpdn : ¬ IrqNodeA (L.ksb s) pd) + {device event cpu : U64} (hK : MsiKeepA ar AR) {a1 : A} {code : U64} + (hmsi : ar.msi_map a irq device event cpu = ok (code, a1)) + (hdec : interrupt.decode_irq_control st ar s a fl4_abi.label.ARMIRQIssueIRQHandlerMSI len slot buf extra = + (do + let s1 ← sched.set_thread_state st s (L.curW s) sched.TS_RESTART + let (s2, a2) ← interrupt.invoke_irq_control st ar s1 a1 irq dest slot + ok (.Ok (), s2, a2))) : + ∃ s3 a3 σ1 σ3, + syscall.decode_invocation st ar s a fl4_abi.label.ARMIRQIssueIRQHandlerMSI len slot (capA st s slot) blk call + don fp buf extra = ok (.Ok (), s3, a3) ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + (.ok (), σ3) ∈ ((invokeFl4IrqControl (Z := Unit) + (.msi (BitVec.ofNat 13 irq.val) sd ss device.bv event.bv cpu.bv)).run σ1).1 ∧ + σ3.curThread = σ.curThread ∧ A64RelS3 st AR ar hPl.ipa n Z.Cs s3 a3 σ3 m := by + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hsl hcov J.actW J.running J.cur + obtain ⟨-, -, heq0, -, -, -, -, -, -⟩ := J.toJEntA.restartA (J.toJStage3A.sysPlace hext hsl hcov) J.symL + J.running RS.cur hvs hvc + obtain ⟨⟨u, ms0⟩, hms⟩ := (machineOpLift_restA (msiMapImpl (BitVec.ofNat 13 irq.val) device.bv event.bv cpu.bv) + σ.machineState).2.1 + obtain ⟨mr, e0⟩ := (machineOpLift_restA (msiMapImpl (BitVec.ofNat 13 irq.val) device.bv event.bv cpu.bv) + σ.machineState).2.2 _ hms + have hm0 : ms0.underlyingMemory = σ.machineState.underlyingMemory := by + have e0' : ms0 = { σ.machineState with machineStateRest := mr } := e0 + rw [e0'] + have hl0 : ms0.lastMachineTime = σ.machineState.lastMachineTime := by + have e0' : ms0 = { σ.machineState with machineStateRest := mr } := e0 + rw [e0'] + obtain ⟨s3, a3, xt, σ3, hd, hset, hmem, hcur, hR⟩ := J.irqIssueArmA (hCnt := hCnt) hA hM hPl hfm + (lbl := fl4_abi.label.ARMIRQIssueIRQHandlerMSI) (len := len) (buf := buf) (blk := blk) (call := call) (don := don) + (fp := fp) hmask hsl hcov hcap hext hSC hvs hvc site hi hact ss sd hCs hCd hss hsd hdn hpdn (a1 := a1) + (ms0 := ms0) hm0 hl0 (fun hw h => post_of_okA (P := fun r => AR r.2 hw) (hK.msi_map a irq device event cpu hw h) hmsi) + (fun s3 a3 hinv => by + rw [decodeIrqControl_programA, hdec, heq0, bind_ok, hinv] + exact bind_ok _ _) + refine ⟨s3, a3, _, σ3, hd, hset, ?_, hcur, hR⟩ + apply mem_liftED + exact mem_bind_ndD (irqMachine_memA (msiMap (BitVec.ofNat 13 irq.val) device.bv event.bv cpu.bv) (stUpdA σ σ.curThread xt .restart) hms) hmem + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3LiftIrqSetA.lean b/proofs/Fl4ProofsA64/P3/Stage3LiftIrqSetA.lean new file mode 100644 index 000000000..7dc3626fb --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3LiftIrqSetA.lean @@ -0,0 +1,193 @@ +/- +P3 (aarch64, A64-P3 Lift 1b): **`IRQSetIRQHandler` at record level**. + +FL4's arm is the caller's Restart, `deleting_irq_handler` (`irq_slot`, `delete_one`: `Stage3LiftIrqClearA`'s deletion at the +restarted state), then `cdt::insert` of the Notification cap read at the decode site from `extra[0]`, into the IRQ's node +slot. The record returns to `JStage3A` after the deletion (`DelXA6G.toStage3A`; `SysL` / `SymL` there come from +`decodeFinA` on the Clear label, whose program is the prefix of Set's), then crosses the insertion by +`JStage3A.capInsertA` (`Stage3ArmCnCopyA`, the Copy arm's body without its Restart), with the final-state `SysL` / `SymL` / +J7 from `decodeFinA` on the Set label. + +* `JSyscallA.irqSetA`: the arm, at the entry's `McState` `m` (as `irqClearA`). + +Named premises: those of `irqClearA` ([P] `ArchTotal`, `MaxSpi`, `ArchPlat`, `A64FrameMasks`, `hCs`, `hmb`, `hmax`, `htu`; +[S] `h1`; [J] `hvs`, `hvc`, `DelSiteA`), and for the extra cap: [J] `hCsS` / `hss` (the spec slot of `extra[0]` is a CNode +slot at its address), `hpn` (that CNode is not an IRQ node; `noIrq_of_cnRefA` at the lookup, as the Copy decode's +`hpdn`). New module. +-/ +import Fl4ProofsA64.P3.Stage3LiftIrqClearA +import Fl4ProofsA64.P3.Stage3ArmCnCopyA +import Fl4ProofsA64.P3.Stage3DelBodyA +import Fl4ProofsA64.P3.Stage3DelBodyLoopA +import Fl4ProofsA64.P3.Stage3LiftBodyA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Vcpu Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +set_option maxHeartbeats 4000000 in +/-- **`IRQSetIRQHandler` at a syscall entry** (module docstring). -/ +theorem JSyscallA.irqSetA [PlainStore st] (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hCnt : CounterStepA σ) + (hfm : A64FrameMasks st s) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {slot irq len buf : U64} {blk call don fp : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hsl : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .IrqHandler irq) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hvs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → Aligned b ∧ b ≠ stA (L.curW s)) + (hvc : ∀ v ∈ Z.V, stA (L.curW s) ≠ v ∧ stA (L.curW s) ≠ core.num.U64.wrapping_add v 24#u64) + (site : DelSiteA st Z.th OS s σ Z.G.chains) + {x0 pn bg : U64} {recv : Bool} (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) (hx : x0 ≠ 0#u64) + (hnc : capA st s x0 = .Notification pn bg true recv) + (ss : Fl4SpecA64.CslotPtr) {ps : U64} (hCsS : ss.1 = specPtr ps ∧ Z.th ps = some (.cnode ss.2.length)) + (hss : cslotAddrA ss = x0) (hpn : ¬ IrqNodeA (L.ksb s) ps) : + ∃ s3 a3 σ1 σ3, + syscall.decode_invocation st ar s a fl4_abi.label.IRQSetIRQHandler len slot (capA st s slot) blk call don fp + buf extra = ok (.Ok (), s3, a3) ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ((), σ3) ∈ (fl4InvokeIrqHandler (Z := Unit) (.setIRQHandler (BitVec.ofNat 13 irq.val) + (absCapA (.Notification pn bg true recv)) ss) σ1).1 ∧ + σ3.curThread = σ.curThread ∧ A64RelS3 st AR ar hPl.ipa n Z.Cs s3 a3 σ3 m := by + have hi : irq.val < 5376 := (J.toJStage3A.slotCapTy hsl).1.2.1 irq hcap + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hsl hcov J.actW J.running J.cur + have hT : TreeA st OS Z.G.chains Z.G.par s := J.j.1.cdt.tree.grow J.j5.2.1 (fun x hx hn => J.j5.2.2 x hx hn) + obtain ⟨xt, heq, hset, D1, W1⟩ := decDel_restartA J.toJEntA (J.toJStage3A.sysPlace hext hsl hcov) J.symL J.running + RS.cur hvs hvc hT J.j5.2.1 J.j5.2.2 site + (X := DelWTopA st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar hPl.ipa (fun _ _ => True) s) + (fun x hkx => J.delRestartWA hPl hsl hcov hext trivial hCnt x hkx) + obtain ⟨-, -, -, -, -, -, hOK, -, -⟩ := J.toJEntA.restartA (J.toJStage3A.sysPlace hext hsl hcov) J.symL J.running + RS.cur hvs hvc + have hcore := W1.2.2.1.1 + have X6 := W1.2.2.1.2.1 + have sv := W1.2.2.1.2.2 + obtain ⟨Q1, R1, hw1, Ac1, Bf1, stack1, J1, -⟩ := X6.1 + have sv2 : ∀ y ∈ (deletingIrqHandler (Z := Unit) (BitVec.ofNat 13 irq.val) + (stUpdA σ σ.curThread xt .restart)).1, SchedValidA y.2 := + deletingIrqHandler_schedValidA J1 D1 sv hi + obtain ⟨⟨s2, a2⟩, e2, σ2, G', y, hy, hstep, -, ⟨-, X2⟩, hnull, hoth, -⟩ := spec_existsA + (deletingIrqHandler_recA (ar := ar) (ipa := hPl.ipa) s (fun _ _ _ _ _ => True) (DelFootA.of_entA J.toJEntA) hCs hmb + hmax htu h1 hi D1 ⟨hcore, X6⟩) + have e2' : (do + let isl ← interrupt.irq_slot st (L.wr s (stA (L.curW s)) sched.TS_RESTART) irq + cdt.delete_one st ar (L.wr s (stA (L.curW s)) sched.TS_RESTART) a isl) = ok (s2, a2) := e2 + obtain ⟨isl, hisl, hdel⟩ := bind_eq_ok e2' + -- the IRQ slot, as the spec names it + have place1 : PlaceA Z.th (L.ksb (L.wr s (stA (L.curW s)) sched.TS_RESTART)) := J1.j.1.place + obtain ⟨hAisl, hesl, hislot⟩ := irqSlot_relatedA (st := st) J1.j.1.irq place1 hi + have hislA : isl = irqSlotA (L.ksb (L.wr s (stA (L.curW s)) sched.TS_RESTART)) irq := okinj (hisl.symm.trans hislot) + set sd : Fl4SpecA64.CslotPtr := ((stUpdA σ σ.curThread xt .restart).interruptIrqNode (BitVec.ofNat 13 irq.val), []) + with hsd_def + have hsd : cslotAddrA sd = isl := hesl.trans hislA.symm + have hAss : AbsSlotA Z.th ss := ⟨ps, hCsS.1, Or.inl hCsS.2⟩ + have hx0O : x0 ∈ OS := hss ▸ J.j5.2.1 _ (hAss.isSlot J.j.1.place) + -- the source slot is not the IRQ's own slot + have hne : x0 ≠ y := by + intro e + have e1 : cslotAddrA ss = cslotAddrA sd := by rw [hss, hsd, hislA, e, hy] + have e2s : ss = sd := cslotAddrA_inj place1 hAss hAisl e1 + have e3 : specPtr ps = specPtr (irqSlotA (L.ksb (L.wr s (stA (L.curW s)) sched.TS_RESTART)) irq) := by + rw [← hCsS.1, e2s]; exact J1.j.1.irq.node irq hi + exact hpn (by rw [← L.ksb_wr s (stA (L.curW s)) sched.TS_RESTART]; exact ⟨irq, hi, specPtr_inj e3⟩) + have hcs2 : capA st s2 (cslotAddrA ss) = .Notification pn bg true recv := by + rw [hss]; exact (hoth x0 hx0O hne).trans ((hOK.capA hx0O).trans hnc) + -- `SysL` / `SymL` at the state after the deletion: the Clear label's program ends there + have hdecC : syscall.decode_invocation st ar s a fl4_abi.label.IRQClearIRQHandler len slot (.IrqHandler irq) blk call + don fp buf extra = ok (.Ok (), s2, a2) := by + rw [decodeIrqHandler_programA, irqClear_programA, heq, bind_ok, hisl, bind_ok, hdel, bind_ok] + rfl + obtain ⟨-, -, sysl2, symL2, -, -, -⟩ := J.decodeFinA hA hM hPl hfm hsl hcov hcap hext hSC + (lbl := fl4_abi.label.IRQClearIRQHandler) + (by simp [DecArmS1aEnt, DecArmQH, DecArmDelQ]) + (fun i h _ => by cases h; exact hi) (fun _ h => by cases h) (len := len) (blk := blk) (call := call) (don := don) + (fp := fp) (buf := buf) (r := .Ok ()) (s3 := s2) (a3 := a2) hdecC σ2 m + obtain ⟨Q2, R2, hw2, Ac2, Bf2, stack2, J2⟩ := X2.toStage3A sysl2 symL2 (sv2 _ hstep) + -- the insertion + have place2 : PlaceA Z.th (L.ksb s2) := J2.j.1.place + have hAd : AbsSlotA Z.th sd := hAisl + have hyO : cslotAddrA sd ∈ OS := J2.j5.2.1 _ (hAd.isSlot J2.j.1.place) + have hc0 : obj.Slot.cap st x0 s = ok (.Notification pn bg true recv) := (capA_eq s x0).trans (by rw [hnc]) + have hdecS : ∀ s3, cdt.insert st s2 (.Notification pn bg true recv) (cslotAddrA ss) (cslotAddrA sd) = ok s3 → + syscall.decode_invocation st ar s a fl4_abi.label.IRQSetIRQHandler len slot (.IrqHandler irq) blk call don fp buf + extra = ok (.Ok (), s3, a2) := fun s3 hs3 => by + rw [hss, hsd] at hs3 + rw [decodeIrqHandler_programA, irqSet_programA hx0 hx hc0, heq, bind_ok, hisl, bind_ok, hdel, bind_ok] + change (do + let s3' ← cdt.insert st s2 (capability.Cap.Notification pn bg true recv) x0 isl + ok (core.result.Result.Ok (), s3', a2)) = _ + rw [hs3] + exact bind_ok _ _ + have F : ∀ s3, cdt.insert st s2 (.Notification pn bg true recv) (cslotAddrA ss) (cslotAddrA sd) = ok s3 → + ∀ σX mX, ∃ Ac' : Set U64, SysL st H Z.Cs OS ia ie s3 ∧ SymL st H Z.Cs s3 ∧ + BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac' s3 ∧ + Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s3 ∧ + J7A st (J7R2A st ar hPl.ipa) Z H OS s3 σX mX := fun s3 hs3 σX mX => + (J.decodeFinA hA hM hPl hfm hsl hcov hcap hext hSC (lbl := fl4_abi.label.IRQSetIRQHandler) + (by simp [DecArmS1aEnt, DecArmQH, DecArmDelQ]) (fun i h _ => by cases h; exact hi) (fun _ h => by cases h) + (len := len) (blk := blk) (call := call) (don := don) (fp := fp) (buf := buf) (r := .Ok ()) (s3 := s3) + (a3 := a2) (hdecS s3 hs3) σX mX).2 + have hbd : ∀ r b rs, (absCapA (capA st s2 (cslotAddrA ss)) = .endpointCap r b rs ∨ + absCapA (capA st s2 (cslotAddrA ss)) = .notificationCap r b rs) → b ≠ 0 → + Fl4SpecA64.capEpBadge (absCapA (.Notification pn bg true recv)) = b := by + intro r b rs hb _ + rw [hcs2] at hb + rcases hb with hb | hb + · cases hb + · exact (Cap.notificationCap.inj hb).2.1 + obtain ⟨s3, σ3, hs3, hm3, hbk3, scF3, J3⟩ := JStage3A.capInsertA (J := J2) (nc := .Notification pn bg true recv) + (anc := absCapA (.Notification pn bg true recv)) ss sd (hCs := hCsS) + (hCd := ⟨J1.j.1.irq.node irq hi, place1.irq irq hi⟩) (hsn := by rw [hcs2]; simp) + (hdn := by rw [hesl, ← hy]; exact hnull) + (hdc := OSOK.canon J2.sys.ent.os J2.winOK hyO) (hwf := trivial) (hanc := CapEqA.of_abs rfl) + (hw := by rw [← hnc]; exact CapRt.decodeF_encWf _ _) (hn := by simp) + (hD := by rw [hcs2]; exact DerivA.refl _) (hal := by rw [hcs2]; exact UA.refl _) (hk := by rw [hcs2]; trivial) + (hsh := by rw [hcs2]) (hsh1 := by rw [hcs2]) (hbd1 := Or.inl (by rw [hcs2])) (hbd := hbd) + (hirqB := fun i h => hcs2.symm.trans h) (hirqN := fun i h => Or.inl (hcs2.trans h)) + (hirqA := fun i h => Or.inl (hcs2.trans h)) (hz := fun p t k => by rw [hcs2]; simp) + (hut := fun h => by rw [hcs2] at h; simp [isUtA] at h) (hdo := Or.inr (Or.inr ⟨_, _, _, rfl⟩)) + (hcv := by rw [hcs2]; exact id) (hsci := ⟨fun c _ => by simp [absCapA], fun b => by simp [absCapA], fun _ _ _ h => by simp [absCapA] at h⟩) + (symL := fun s3 hs3 => (F s3 hs3 σ2 m).choose_spec.2.1) + (sysl := fun s3 hs3 => (F s3 hs3 σ2 m).choose_spec.1) + (j7 := fun s3 σ3 hs3 _ => (F s3 hs3 σ3 m).choose_spec.2.2.2.2) + have hdec3 := hdecS s3 hs3 + obtain ⟨ac0, hg, hac⟩ := irqSlot_ntfnOrNullA J1 D1 hi + have hbody := deletingIrqHandler_bodyA hg hac (irqSlot_qneA J1 D1 hi hg) hstep + obtain ⟨x', hx', -, hset', -⟩ := J.restartMachA hsl hcov hext + have e1 : stUpdA σ σ.curThread xt .restart = stUpdA σ σ.curThread x' .restart := by + have := Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst (hset.symm.trans hset')) + exact (Prod.mk.inj this).2 + rw [e1] at hbody + have hbody3 : DSkA (stUpdA σ σ.curThread x' .restart) σ3 ∨ + (DWkA (stUpdA σ σ.curThread x' .restart) σ3 ∧ σ3.schedulerAction = .chooseNewThread) := by + rcases hbody with K | ⟨K, act⟩ + · exact Or.inl (PreO.trans (R := DSkA) K (capInsert_skA _ _ _ σ2 _ hm3)) + · exact Or.inr ⟨PreO.trans (R := DWkA) K (capInsert_skA _ _ _ σ2 _ hm3).toDWkA, hbk3.act.trans act⟩ + obtain ⟨B, hcur⟩ := Sched.BodyOKA.ofDel J hx' hbody3 + refine ⟨s3, a2, _, σ3, by rw [hcap]; exact hdec3, hset, ?_, hcur, A64RelS3.ofJ J3 B⟩ + show ((), σ3) ∈ (invokeIrqHandler (Z := Unit) (.setIRQHandler (BitVec.ofNat 13 irq.val) + (absCapA (.Notification pn bg true recv)) ss) (stUpdA σ σ.curThread xt .restart)).1 + unfold invokeIrqHandler + unfold deletingIrqHandler at hstep + obtain ⟨q, hq, hy2⟩ := mem_bindA.1 hstep + have hq' := hq + obtain rfl : q = ((((stUpdA σ σ.curThread xt .restart).interruptIrqNode (BitVec.ofNat 13 irq.val), []) : CslotPtr), + stUpdA σ σ.curThread xt .restart) := hq' + exact mem_bindA.2 ⟨_, hq, mem_bindA.2 ⟨((), σ2), hy2, hm3⟩⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3LiftIrqSigA.lean b/proofs/Fl4ProofsA64/P3/Stage3LiftIrqSigA.lean new file mode 100644 index 000000000..404ecb6a6 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3LiftIrqSigA.lean @@ -0,0 +1,167 @@ +/- +P3 (aarch64, A64-P3 Lift 1b): **the IRQ-state write of `IRQControl` issue** (`interrupt::set_irq_state(IRQ_SIGNAL, irq)`), +the signal twin of the cleanup's `set_irq_state(IRQ_INACTIVE)` (`P3/DelIrqPostA`). + +`set_irq_state` is `ks_set` of `IRQ_STATE[irq]` and `mask_irq irq false` (an unmask); the spec's `setIrqState .irqSignal` +is the IRQ state at `irq` and the machine's IRQ mask. The memory side is `JStage3TA.irqWriteA` (`P3/DelIrqWriteA`), the +loop record `DelXA6` is kept by `DelXA.stepPpPA` with `ksKeep_std6A` as in `postDelHookPA`: an issue's restarted state is +the loop's record at the entry's ghost, so the issue arms reach `JStage3A` by `DelXA6G.toStage3A`. + +* `setIrqSignal_specA`, `irqSigPostA`: the spec step. +* `set_irq_signalA`: the concrete write; [P] `hmask` (`mask_irq` keeps `AR`, `VcpuArchKeepsA.mask_irq`). +* `DelXA6.irqSignalA`: the loop record across the write; `hno`: no `OS` slot holds the IRQ's handler (an inactive IRQ, + `irqInactive_noHandlerA`). +* `irqInactive_noHandlerA`: an inactive IRQ has no handler cap in `OS` (J5's `HandlersActiveA`). + +Named premises: [P] `mask_irq` keeps `AR` (an argument). New module. +-/ +import Fl4ProofsA64.P3.DelIrqPostA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-- The spec's IRQ state after `setIrqState .irqSignal irq`. -/ +def irqSigStA (σ : State Unit) (irq : U64) : Irq → IrqState := + Function.update σ.interruptStates (BitVec.ofNat 13 irq.val) .irqSignal + +/-- The spec's machine state after `maskInterrupt false irq`. -/ +def irqUnmaskedMsA (ms : MachineState) (irq : U64) : MachineState := + { ms with irqMasks := Function.update ms.irqMasks (BitVec.ofNat 13 irq.val) false } + +/-- The spec state after the issue's IRQ-state write: the state at `irq` signal and its mask clear. -/ +def irqSigPostA (σ : State Unit) (irq : U64) : State Unit := + { σ with interruptStates := irqSigStA σ irq, machineState := irqUnmaskedMsA σ.machineState irq } + +theorem setIrqSignal_specA (σ : State Unit) (irq : U64) : + ((), irqSigPostA σ irq) ∈ (setIrqState (Z := Unit) .irqSignal (BitVec.ofNat 13 irq.val) σ).1 := by + unfold setIrqState + rw [nd_modify_bindA] + have hm : ((), irqUnmaskedMsA σ.machineState irq) ∈ + (maskInterrupt false (BitVec.ofNat 13 irq.val) σ.machineState).1 := by + unfold maskInterrupt irqUnmaskedMsA + exact rfl + have hn : (.irqSignal : IrqState) ≠ .irqInactive := by decide + simp only [decide_eq_false hn] + exact irqMachine_memA _ { σ with interruptStates := irqSigStA σ irq } hm + +variable {ar : machine.Arch A} + +set_option maxHeartbeats 2000000 in +/-- **The concrete IRQ-state write of an issue** (module docstring). -/ +theorem set_irq_signalA {AR : A → HwRootA → Prop} {hw : HwRootA} (s : S) (a : A) (σ : State Unit) (irq : U64) + (hi : irq.val < 5376) (hmask : ∀ a, AR a hw → ar.mask_irq a irq false ⦃ a' => AR a' hw ⦄) + (h : IrqRelA (L.mem s) (L.ksb s) σ) (hAR : AR a hw) (hf : (L.ksb s).val + ksSizeA ≤ 2 ^ 64) + (hal' : ∀ j : U64, j.val < 5376 → Aligned (irqWordA (L.ksb s) j)) : + interrupt.set_irq_state st ar s a interrupt.IRQ_SIGNAL irq ⦃ r => + r.1 = L.wr s (irqWordA (L.ksb s) irq) interrupt.IRQ_SIGNAL ∧ AR r.2 hw ∧ + IrqRelA (L.mem r.1) (L.ksb r.1) (irqSigPostA σ irq) ⦄ := by + have hs : interrupt.state_off irq = ok (irqOffA irq) := by + unfold interrupt.state_off + obtain ⟨v, hv, e⟩ := spec_existsA P0.Consts.layout.ks.IRQ_STATE.spec + subst v + simp only [lift, bind_ok, hv, irqOffA] + have hn : (interrupt.IRQ_SIGNAL : U64) ≠ interrupt.IRQ_INACTIVE := by + rw [interrupt.IRQ_SIGNAL, interrupt.IRQ_INACTIVE] + decide + unfold interrupt.set_irq_state + simp only [hs, bind_ok, decide_eq_false hn] + refine WP.spec_bind (obj.ks_set.spec s (irqOffA irq) interrupt.IRQ_SIGNAL) ?_ + rintro s' rfl + refine WP.spec_bind (hmask a hAR) ?_ + rintro a' ha' + refine (WP.spec_ok _).2 ⟨rfl, ha', ?_⟩ + have hal := hal' irq hi + have hk : L.ksb (L.wr s (irqWordA (L.ksb s) irq) interrupt.IRQ_SIGNAL) = L.ksb s := + (writesIn_wr (st := st) (v := interrupt.IRQ_SIGNAL) (s := s) hal).1 + change IrqRelA (L.mem (L.wr s (irqWordA (L.ksb s) irq) interrupt.IRQ_SIGNAL)) + (L.ksb (L.wr s (irqWordA (L.ksb s) irq) interrupt.IRQ_SIGNAL)) (irqSigPostA σ irq) + rw [hk] + constructor + · intro j hj + rw [L.mem_wr _ _ _ _ hal (hal' j hj)] + change irqSigStA σ irq (BitVec.ofNat 13 j.val) = _ + unfold irqSigStA + by_cases e : j = irq + · subst e; simp [irqStOfA, interrupt.IRQ_SIGNAL] <;> exact Function.update_self .. + · have en : irqWordA (L.ksb s) j ≠ irqWordA (L.ksb s) irq := by + intro eq + apply e + apply UScalar.eq_of_val_eq + have q := congrArg (fun u : U64 => u.val) eq + rw [irqWordA_val hf hj, irqWordA_val hf hi] at q + omega + have ei : BitVec.ofNat 13 j.val ≠ BitVec.ofNat 13 irq.val := by + intro eq + apply e + apply UScalar.eq_of_val_eq + have q := congrArg BitVec.toNat eq + have h1 : j.val < 2 ^ 13 := by omega + have h2 : irq.val < 2 ^ 13 := by omega + change j.val % 2 ^ 13 = irq.val % 2 ^ 13 at q + rwa [Nat.mod_eq_of_lt h1, Nat.mod_eq_of_lt h2] at q + exact (Function.update_of_ne ei _ _).trans (by rw [if_neg (Ne.symm en)]; exact h.st j hj) + · exact h.node + · exact h.nodeHi + +/-- **An inactive IRQ has no handler cap in `OS`** (J5's `HandlersActiveA`; the issue decoder's `is_irq_active` is false). -/ +theorem irqInactive_noHandlerA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} + {κ : U64 → U64} {n : Nat} {J7 : JSlotA S} {s : S} {a : A} {σ : State Unit} {m : McState} {irq : U64} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + (hact : L.mem s (irqWordA (L.ksb s) irq) = 0#u64) : ∀ y ∈ OS, capA st s y ≠ .IrqHandler irq := by + intro y hy hc + have hTr : TreeA st OS Z.G.chains Z.G.par s := J.j.1.cdt.tree.grow J.j5.2.1 (fun x hx hn => J.j5.2.2 x hx hn) + have hyG : y ∈ Z.G.chains.flatten := (hTr.lk.members y).2 ⟨hy, by rw [hc]; simp⟩ + exact (J.j5.1.2.2 y hyG irq hc).2 hact + +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {κ : U64 → U64} {n : Nat} {m : McState} {ipa : U64} + +set_option maxHeartbeats 2000000 in +/-- **The loop record across an issue's IRQ-state write** (module docstring). -/ +theorem DelXA6.irqSignalA {s : S} {a : A} {σ : State Unit} {G : MdbGhostA} {irq : U64} (hi : irq.val < 5376) + (hmask : ∀ a hw, AR a hw → ar.mask_irq a irq false ⦃ a' => AR a' hw ⦄) + (hno : ∀ y ∈ OS, capA st s y ≠ .IrqHandler irq) + (D : DStateA st Z.th OS s σ G) + (X : DelXA6 st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st (J7R2A st ar ipa)) s a σ G) : + interrupt.set_irq_state st ar s a interrupt.IRQ_SIGNAL irq ⦃ r => + r.1 = L.wr s (irqWordA (L.ksb s) irq) interrupt.IRQ_SIGNAL ∧ L.ksb r.1 = L.ksb s ∧ OSKeep st OS s r.1 ∧ + DStateA st Z.th OS r.1 (irqSigPostA σ irq) G ∧ + DelXA6 st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st (J7R2A st ar ipa)) r.1 r.2 + (irqSigPostA σ irq) G ⦄ := by + obtain ⟨⟨Q, R, hw, Ac, Bf, stack, J, Y⟩, hPA, hZT, hBH, hCT, hFx⟩ := X + have hirqJ : IrqRelA (L.mem s) (L.ksb s) σ := J.j.1.irq + have hAR : AR a hw := J.j.1.ar + have hf := J.sr.ksFit + have hal' : ∀ j : U64, j.val < 5376 → Aligned (irqWordA (L.ksb s) j) := fun j hj => by + have := irqWordA_val hf hj + have := J.sr.toSchedSRN.ksb_al + show _ % 8 = 0 + omega + refine WP.spec_mono (set_irq_signalA (ar := ar) (hw := hw) s a σ irq hi (fun a h => hmask a hw h) hirqJ hAR hf hal') ?_ + rintro r ⟨hr1, hA2, hIrq2⟩ + have w : WritesIn st s r.1 (fun b => b = irqWordA (L.ksb s) irq) := by + rw [hr1]; exact writesIn_wr (hal' irq hi) + obtain ⟨J', osk⟩ := J.irqWriteA hi w hno (U' := irqSigStA σ irq) (ms := irqUnmaskedMsA σ.machineState irq) rfl hIrq2 hA2 + have X' : Std6A st Z H OS Bf r.1 (irqSigPostA σ irq) := + ksKeep_std6A J Y D.sub osk w.1 (fun b hb off => w.mem_eq hb (fun e => off (by + have v := irqWordA_val hf hi + have ksz : ksSizeA = 788736 := rfl + rw [e, v]; omega))) rfl rfl + have D' := DelXA.stepPpPA J' X' D osk w.1 hPA hBH hCT hFx + exact ⟨hr1, w.1, osk, D'.1, D'.2⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3LiftMachA.lean b/proofs/Fl4ProofsA64/P3/Stage3LiftMachA.lean new file mode 100644 index 000000000..3605d4280 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3LiftMachA.lean @@ -0,0 +1,243 @@ +/- +P3 (aarch64, A64-P3 Lift 1): **record-level steps that keep FL4's store**: `Err` outcomes, spec machine-state steps +and `IRQAckIRQ`. + +* **`JSyscallA.errArchA`** / **`errVsA`**: an arch cap-kind top's `.Err` outcome (`ArchTopPostA` / `VsTopPostA`) keeps + FL4's state and arch state, so the record holds unchanged and the spec decoder throws the related error. +* **`JStage3A.of_machA`**: `JStage3A` across a spec step that changes only `machineState`, keeping + `underlyingMemory`, with an arch state still related by `AR`. +* **`JSyscallA.restartMachA`**: the caller's `set_thread_state Restart` followed by such a step. +* **`JSyscallA.irqAckA`**: FL4's `IRQAckIRQ` (every IRQ handler cap) at record level. + +* **`JStage3A.curScOnCoreA`**: the current SC is on this core (a lemma, not a record field; Architect ruling). +* **`JSyscallA.irqAckMRA`**: `IRQAckIRQ` keeping the machine relation `MR` (`P3/MachKeepA`). + +Named premises: [P] `VcpuArchKeepsA` (`mask_irq` keeps `AR`), `IrqHwA` (`irqAckMRA`), `hCs` (core numbers are below +the platform's); [S] one node (`curScOnCoreA`). +-/ +import Fl4ProofsA64.P3.Stage3SyscallA +import Fl4ProofsA64.P3.Stage3RestartA +import Fl4ProofsA64.P3.Stage3RsSiteA +import Fl4ProofsA64.P3.Stage3SysPlaceA +import Fl4ProofsA64.P3.Stage3FinA +import Fl4ProofsA64.P3.FrReplyA +import Fl4ProofsA64.P3.IrqInvokePerformA +import Fl4ProofsA64.P3.VcpuPremisesA +import Fl4ProofsA64.P3.Stage3PreemptTA +import Fl4ProofsA64.P3.MachKeepA +import Fl4ProofsA64.P3.Stage3LiftBodyA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Vcpu Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- **An `.Err` outcome of an arch cap-kind top, at record level** (module docstring). -/ +theorem JSyscallA.errArchA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {calling : Bool} + {D : Set (Except Fl4SpecA64.SyscallError Fl4SpecA64.Fl4ArchInvocation × Fl4SpecA64.State Unit) × Prop} + {e : error.Exc} {s' : S} {a' : A} + (h : ArchTopPostA (L := L) AR Z OS m calling σ D s a (.Err e, s', a')) : + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s' a' σ m ∧ + ∃ e', D = ({(.error e', σ)}, False) ∧ VsErrRelA e e' := by + obtain ⟨rfl, rfl, he⟩ := h + exact ⟨J.toJStage3A, he⟩ + +/-- **The same for a `VsTopPostA` outcome** (VSpace / frame / table tops). -/ +theorem JSyscallA.errVsA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {D : Set (Except Fl4SpecA64.SyscallError Fl4SpecA64.Fl4ArchInvocation × Fl4SpecA64.State Unit) × Prop} + {e : error.Exc} {s' : S} {a' : A} + (h : VsTopPostA (L := L) AR Z OS m σ D s a (.Err e, s', a')) : + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s' a' σ m ∧ + ∃ e', D = ({(.error e', σ)}, False) ∧ VsErrRelA e e' := by + obtain ⟨rfl, rfl, he⟩ := h + exact ⟨J.toJStage3A, he⟩ + +set_option maxHeartbeats 1000000 in +/-- **`JStage3A` across a spec machine-state step** (module docstring; the record form of `JStage3TA.machineA`). -/ +theorem JStage3A.of_machA {ar : machine.Arch A} {nA : U64} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {a2 : A} (har : AR a2 Z.hw) (ms : Fl4SpecA64.MachineState) + (hm : ms.underlyingMemory = σ.machineState.underlyingMemory) : + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a2 + { σ with machineState := ms } m := by + obtain ⟨σ2, e2⟩ : ∃ σ2 : Fl4SpecA64.State Unit, σ2 = { σ with machineState := ms } := ⟨_, rfl⟩ + rw [← e2] + have kh : σ2.kheap = σ.kheap := by rw [e2] + have um : σ2.machineState.underlyingMemory = σ.machineState.underlyingMemory := by rw [e2]; exact hm + have H2 : UtCnStepA σ σ2 := + ⟨by rw [e2]; exact ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, ⟨rfl, rfl, rfl, rfl⟩⟩, + fun q => Or.inl (by rw [kh]), um⟩ + have HS : SpecSchedA σ σ2 := + { kh := fun q => Or.inl (by rw [kh]) + irqS := by rw [e2] + irqN := by rw [e2] + asid := by rw [e2] + vcur := by rw [e2] + cdt := by rw [e2] + orig := by rw [e2] } + have SSz : SSzA σ σ2 := fun q y n h => ⟨y, by rw [← kh]; exact h⟩ + have hw : WritesIn st s s (fun _ => False) := WritesIn.refl _ _ + have hW : ∀ b : U64, False → ¬ UtRdA st Z N E Rp Bf m s σ b := fun _ h => h.elim + have hsbAl : ∀ t ∈ Z.T, Aligned (wa t 3512#u64) ∧ Aligned (sbWordA t) ∧ Aligned (wa (sbWordA t) 8#u64) := by + intro t ht + have hta := J.sr.core.tcbs t ht + have e : sbWordA t = wa t 64#u64 := by + unfold sbWordA layout.tcb.SLOT_BUFFER layout.cte.SIZE + apply UScalar.eq_of_val_eq + simp [core.num.U64.wrapping_add_val_eq, core.num.U64.wrapping_mul_val_eq, U64.size_def, U64.numBits] + have e8 : wa (sbWordA t) 8#u64 = wa t 72#u64 := by + rw [e] + apply UScalar.eq_of_val_eq + simp [core.num.U64.wrapping_add_val_eq, U64.size_def, U64.numBits, Nat.add_assoc] + refine ⟨field_aligned hta (by simp) (by simp), ?_, ?_⟩ + · rw [e]; exact field_aligned hta (by simp) (by simp) + · rw [e8]; exact field_aligned hta (by simp) (by simp) + have tAl : ∀ t ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → Aligned (wa t K) := fun t ht K h1 h2 => + field_aligned (J.sr.core.tcbs t ht) h1 h2 + have rAl : ∀ r ∈ Rp, Aligned (rNextA r) ∧ Aligned (rPrevA r) := by + intro r hr' + obtain ⟨g1, -, g3⟩ := J.iplaced.e.rep r hr' + have e8 : (rPrevA r).val = r.val + 8 := by + unfold rPrevA layout.reply.PREV + rw [wa_valP]; exact Nat.mod_eq_of_lt (by simp; omega) + have e16 : (rNextA r).val = r.val + 16 := by + unfold rNextA layout.reply.NEXT + rw [wa_valP]; exact Nat.mod_eq_of_lt (by simp; omega) + constructor <;> show _ % 8 = 0 <;> omega + have sv : SchedValidA σ2 := + J.schedValid.congr (by rw [e2]) (by rw [e2]) (by rw [e2]) (by rw [e2]) (by rw [e2]) (by rw [e2]) + (fun _ _ _ h => by rw [kh]; exact h) (fun _ x h => ⟨x, by rw [kh]; exact h, rfl⟩) + (fun _ x h => ⟨x, by rwa [kh] at h, rfl, rfl⟩) + exact + JStage3A.of_schedWr (Q := Z.Q) (Rl := Z.R) (curOf := Z.curOf) (curScOf := Z.curScOf) J + (WritesIn.refl _ _) HS SSz (QKeepA.of_kheap kh ⟨by rw [e2], by rw [e2], by rw [e2], by rw [e2]⟩) um har (fun _ _ => rfl) + (fun t ht y hy => J.bufAl t ht y (by rw [← kh]; exact hy)) J.sys J.sq J.poolOff + (utFr_schedSRA hw hW H2 J.sr) (utCn_scCovA J.scCov H2) (utFr_epA hw hW H2 J.ep J.iplaced.e) + (utFr_ntfnA hw hW H2 J.ntfn J.iplaced.n) (utFr_replyA hw hW H2 J.reply rAl) + (utFr_bufCoverA hw hW H2 J.bufCover hsbAl) (utCn_idleNoFaultA J.idleNoFault H2) + (utFr_mcpA hw hW H2 J.mcp fun t ht => ⟨tAl t ht 3440#u64 (by simp) (by simp), tAl t ht 3456#u64 (by simp) (by simp)⟩) + (utFr_yieldA hw hW H2 J.yieldRel fun t ht => tAl t ht _ (by simp) (by simp)) + (utCn_sinvA J.sinv H2 (SpecIrqA.of_eq J.sinv.irqN kh (by rw [e2]))) + (J.sidle.congr kh (by rw [e2]) (by rw [e2]; exact BitVec.le_refl _)) J.runVcpu + (J.blkQ.keepA (fun _ _ => rfl) + (fun _ _ x' hx => Or.inr ⟨x', by rwa [kh] at hx, rfl⟩) (fun _ _ h => by rw [kh]; exact h) + (fun _ _ h => by rw [kh]; exact h)) + J.symL sv (by rw [e2]; exact J.j5) + (J7PpKA.keep (st := st) _ _ _ _ _ σ σ2 _ J.j7 rfl (fun _ _ => ⟨rfl, rfl, rfl, rfl⟩) (fun _ _ _ _ _ => rfl)) + +/-- **The caller's restart, then a spec machine-state step** (module docstring). The FL4 state after it is the one +write of the caller's state word. -/ +theorem JSyscallA.restartMachA {ar : machine.Arch A} {nA : U64} + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {slot : U64} (hsl : slot ∈ OS) (hcov : SlotCov st OS s slot) + {extra : Aeneas.Std.Array obj.Slot 3#usize} (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) : + ∃ x, σ.kheap σ.curThread = some (.tcb x) ∧ + sched.set_thread_state st s (L.curW s) sched.TS_RESTART = ok (L.wr s (stA (L.curW s)) sched.TS_RESTART) ∧ + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + ∀ (a' : A), AR a' Z.hw → ∀ ms : Fl4SpecA64.MachineState, + ms.underlyingMemory = (stUpdA σ σ.curThread x .restart).machineState.underlyingMemory → + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) + (L.wr s (stA (L.curW s)) sched.TS_RESTART) a' + { stUpdA σ σ.curThread x .restart with machineState := ms } m := by + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hsl hcov J.actW J.running J.cur + obtain ⟨Rq, Nt, extra', sp, P⟩ := RS.pl + obtain ⟨x, hx, hst, hset, J1, -, -, -⟩ := JStage3A.of_restartCurA J.toJStage3A P RS.sym RS.run + J.toJStage3EntryA.curNotVmA RS.cur RS.vs RS.vc + exact ⟨x, hx, hst, hset, fun a' har ms hum => J1.of_machA har ms hum⟩ + +/-- **`IRQAckIRQ` at record level** (module docstring). -/ +theorem JSyscallA.irqAckA {ar : machine.Arch A} {nA : U64} (hv : VcpuArchKeepsA ar AR) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {slot : U64} (hsl : slot ∈ OS) (hcov : SlotCov st OS s slot) + (irq : U64) (extra : Aeneas.Std.Array obj.Slot 3#usize) (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) : + interrupt.decode_irq_handler st ar s a fl4_abi.label.IRQAckIRQ irq extra ⦃ r => + r.1 = .Ok () ∧ ∃ σ1 σ2, + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ((), σ2) ∈ (Fl4SpecA64.invokeIrqHandler (Z := Unit) (.aCKIrq (BitVec.ofNat 13 irq.val)) σ1).1 ∧ + (∃ x, σ.kheap σ.curThread = some (.tcb x) ∧ Sched.BodyKeepA (stUpdA σ σ.curThread x .restart) σ2) ∧ + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) + r.2.1 r.2.2 σ2 m ⦄ := by + obtain ⟨x, hx, hst, hset, k⟩ := J.restartMachA hsl hcov hext + rw [irqAck_programA, hst] + simp only [bind_ok] + refine WP.spec_bind (hv.mask_irq a _ irq false J.j.1.ar) ?_ + intro a' har + set σ1 := stUpdA σ σ.curThread x .restart + set ms : Fl4SpecA64.MachineState := + { σ1.machineState with irqMasks := Function.update σ1.machineState.irqMasks (BitVec.ofNat 13 irq.val) false } + refine (WP.spec_ok _).2 ⟨rfl, σ1, { σ1 with machineState := ms }, hset, ?_, ⟨x, hx, Sched.BodyKeepA.ofMach σ1 ms rfl⟩, + k a' har ms rfl⟩ + have e := archInvokeIrqHandler_ackA (BitVec.ofNat 13 irq.val) + exact (congrArg (fun f => ((), ({ σ1 with machineState := ms } : Fl4SpecA64.State Unit)) ∈ (f σ1).1) e).mpr + (irqMachine_memA _ σ1 rfl) + +/-- **The current SC is on this core** (module docstring). -/ +theorem JStage3A.curScOnCoreA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < Fl4SpecA64.fl4A64Platform.numNodes) + (one : Fl4SpecA64.fl4A64Platform.numNodes = 1) : L.mem s (scCoreA (L.cscW s)) = L.cpu s := by + have hc : L.cscW s ∈ Z.C := J.sr.cscC + rw [J.sr.scCore _ hc] + have h1 := J.scCpu _ hc + have h2 := hCs _ J.sr.sch.cpuCs + apply UScalar.eq_of_val_eq + omega + +/-- **`IRQAckIRQ` keeping the machine relation** (module docstring): the arch state's mask step matches the spec's. -/ +theorem JSyscallA.irqAckMRA {ar : machine.Arch A} {nA : U64} {AR0 : A → HwRootA → Prop} + {MR : A → Fl4SpecA64.MachineState → Prop} (hv : VcpuArchKeepsA ar AR0) (hw : IrqHwA ar MR) + (J : JSyscallA st (withMRA AR0 MR σ.machineState) Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s a σ m) + {slot : U64} (hsl : slot ∈ OS) (hcov : SlotCov st OS s slot) + (irq : U64) (hi : irq.val < 5376) (extra : Aeneas.Std.Array obj.Slot 3#usize) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) : + interrupt.decode_irq_handler st ar s a fl4_abi.label.IRQAckIRQ irq extra ⦃ r => + r.1 = .Ok () ∧ ∃ σ1 σ2, + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ((), σ2) ∈ (Fl4SpecA64.invokeIrqHandler (Z := Unit) (.aCKIrq (BitVec.ofNat 13 irq.val)) σ1).1 ∧ + JStage3A st (withMRA AR0 MR σ2.machineState) Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) r.2.1 r.2.2 σ2 m ⦄ := by + obtain ⟨x, -, hst, hset, k⟩ := J.restartMachA hsl hcov hext + have har : withMRA AR0 MR σ.machineState a Z.hw := J.j.1.ar + rw [irqAck_programA, hst] + simp only [bind_ok] + set σ1 := stUpdA σ σ.curThread x .restart + have hm : MR a σ1.machineState := har.2 + obtain ⟨a1, e1, p1⟩ := spec_existsA (hv.mask_irq a _ irq false har.1) + obtain ⟨a2, e2, ms, hstep, hmr⟩ := spec_existsA (hw.mask a σ1.machineState irq false hm hi) + have ea : a2 = a1 := by + rw [e1] at e2 + exact (ok_inj_ff e2).symm + subst ea + rw [e1, bind_ok] + have hum : ms.underlyingMemory = σ1.machineState.underlyingMemory := by + have h := hstep + unfold Fl4SpecA64.maskInterrupt at h + cases h + rfl + have J0 := k a har σ1.machineState rfl + have J1 := (J0.arSwapA (AR' := fun _ _ => True) trivial).of_machA (a2 := a2) trivial ms hum + refine (WP.spec_ok _).2 ⟨rfl, σ1, { σ1 with machineState := ms }, hset, ?_, J1.arSwapA (AR' := withMRA AR0 MR ms) ⟨p1, hmr⟩⟩ + have e := archInvokeIrqHandler_ackA (BitVec.ofNat 13 irq.val) + exact (congrArg (fun f => ((), ({ σ1 with machineState := ms } : Fl4SpecA64.State Unit)) ∈ (f σ1).1) e).mpr + (irqMachine_memA _ σ1 hstep) + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3LiftTcbBufA.lean b/proofs/Fl4ProofsA64/P3/Stage3LiftTcbBufA.lean new file mode 100644 index 000000000..766b8debb --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3LiftTcbBufA.lean @@ -0,0 +1,243 @@ +/- +P3 (aarch64, A64-P3 Lift 3b labels): **TCB SetIPCBuffer with address 0 on another thread, at the record**. + +* **`TcbDelOutA`**: the outcome shape of the TCB labels that first delete a TCB slot (DelBodyA frozen clauses): the + caller's restart, then `Ok` with the spec's invocation, `JStage3A` and `BodyEndA`, or `Preempted` with the spec's + error, `JStage3A` and `BodyPreA`. +* **`ipcBufNone_ofNullA`**: an emptied buffer slot leaves no usable IPC buffer in the spec TCB. +* **`setIpcBuf_invokeA`**: the spec's `fl4InvokeTcb (.threadControlCaps ..)` with only the buffer part, from its + `fl4CapDelete` outcome. +* **`JSyscallA.setIpcBufZeroA`**: `decode_invocation` on a Thread cap with the SetIPCBuffer label and buffer address 0 + at a syscall entry: `restartDelA`, the delete of slot 2, `set_ipc_buffer 0` (`setIpcBuf_stepA`), no insert, no + `reschedule_required` (the target is not the current thread). + +Named premises: those of `JSyscallA.restartDelA` (`DelPlatA`, `hCl`, `hCs`, `hCnt`), [P] +`ArchTotal`, `MaxSpi`, `ArchPlat`, `A64FrameMasks`; [S] `h1` (`numNodes = 1`, the remote stall). The decode reads +extra slot 0 and the address 0 (`he`, `harg`); the `Err` returns before the delete are not lifted. New module. +-/ +import Fl4ProofsA64.P3.Stage3TcbDelStepA +import Fl4ProofsA64.P3.Stage3TcbBufStepA +import Fl4ProofsA64.P3.Stage3TcbTlsA +import Fl4ProofsA64.P3.Stage3LiftMachA +import Fl4ProofsA64.P3.TcbInvokeBufferA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Vcpu Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + +variable (st) in +/-- **The outcome of a TCB label that deletes a TCB slot** (module docstring), for the invocation `Inv κs` +(`κs` the invoked slot). -/ +def TcbDelOutA (AR : A → HwRootA → Prop) (Z : XSetsA) (H : Heap) (OS U Ac Pp Tb : Set U64) (Lv : U64 → Nat) + (ia ie : Nat) (N E Rp : Set U64) (Bf : Set (U64 × Fl4SpecA64.Word)) (stack : U64 → List U64) (κ : U64 → U64) + (n : Nat) (m : Fl4SpecA64.McState) (ar : machine.Arch A) (ipa : U64) (s : S) (a : A) (σ : Fl4SpecA64.State Unit) + (Inv : CslotPtr → TcbInvocation) (r : core.result.Result Unit error.Exc) (s3 : S) (a3 : A) : Prop := + ∃ xc σ1 σ3, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ∃ (G' : MdbGhostA) (Q : U64 → U64 → List U64) (R : U64 → List U64) (hw : HwRootA) (Ac' : Set U64) + (Bf' : Set (U64 × Fl4SpecA64.Word)) (stack' : U64 → List U64), + JStage3A st AR { Z with G := G', Q := Q, R := R, hw := hw } H OS U Ac' Pp Tb Lv ia ie N E Rp Bf' stack' κ n + (J5A st) (J7A st (J7R2A st ar ipa)) s3 a3 σ3 m ∧ + ((r = .Ok () ∧ (∀ κs, (.ok [], σ3) ∈ ((fl4InvokeTcb (Inv κs)).run σ1).1) ∧ Sched.BodyEndA σ σ3 m) ∨ + (r = .Err .Preempted ∧ (∀ κs, (.error (), σ3) ∈ ((fl4InvokeTcb (Inv κs)).run σ1).1) ∧ + BodyPreA σ3 m ∧ σ3.curThread = σ.curThread)) + +/-- **An emptied buffer slot leaves no usable IPC buffer** (module docstring). -/ +theorem ipcBufNone_ofNullA {th : TypedHeapA} {s : S} {σ : Fl4SpecA64.State Unit} (hR : StateRelA th (L.mem s) σ) + {t : U64} (ht : th t = some .tcb) {x : Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hnull : capA st s (sbWordA t) = .Null) : ∀ b, ipcBufOfA b x = none := by + intro b + obtain ⟨x', hk', hs⟩ := hR.tcb t ht + rw [hk] at hk'; cases hk' + obtain ⟨ac, hac, hca⟩ := hs 2 (by decide) + rw [tcbCnodeMap_twoA] at hac + cases hac + obtain ⟨-, hab⟩ := hca + rw [capAtA_eq, bufSlot_eqA, hnull] at hab + have e : x.tcbIpcframe = .nullCap := hab.symm + unfold ipcBufOfA + rw [e] + +/-- **The spec's buffer-only `invoke_tcb`**, from the outcome of its `cap_delete`. -/ +theorem setIpcBuf_invokeA {σ1 σd : Fl4SpecA64.State Unit} {t : U64} {y : Tcb} (κs : CslotPtr) + (hdel : (.ok (), σd) ∈ ((fl4CapDelete (Z := Unit) (specPtr t, tcbCnodeIndex 2)).run σ1).1) + (hy : σd.kheap (specPtr t) = some (.tcb y)) (hne : σd.curThread ≠ specPtr t) : + (.ok [], bufStateA σd (specPtr t) y) ∈ + ((fl4InvokeTcb (.threadControlCaps (specPtr t) κs none none none none + (some (Fl4SpecA64.dataToVref (0#u64).bv, none)))).run σ1).1 := by + have hset : threadSet (Z := Unit) (fun x : Tcb => { x with tcbIpcBuffer := Fl4SpecA64.dataToVref (0#u64).bv }) + (specPtr t) σd = ({((), bufStateA σd (specPtr t) y)}, False) := threadSet_eqSc _ hy + have hrk : ∀ σ : Fl4SpecA64.State Unit, (.ok (), σ) ∈ ((NonDetE.returnOk (ε := Unit) ()).run σ).1 := fun σ => by + rw [run_returnOkD]; simp + have hs1 : ((), bufStateA σd (specPtr t) y) ∈ ((threadSet (Z := Unit) + (fun x : Tcb => { x with tcbIpcBuffer := Fl4SpecA64.dataToVref (0#u64).bv }) (specPtr t)) σd).1 := by + rw [hset]; simp + have hs2 : ((), bufStateA σd (specPtr t) y) ∈ ((Fl4Spec.NonDet.pure () : Fl4Spec.NonDet (State Unit) Unit) (bufStateA σd (specPtr t) y)).1 := by + have h : (Fl4Spec.NonDet.pure () : Fl4Spec.NonDet (State Unit) Unit) (bufStateA σd (specPtr t) y) = + ({((), bufStateA σd (specPtr t) y)}, False) := rfl + rw [h]; exact Set.mem_singleton _ + simp only [fl4InvokeTcb, fl4InstallTcbCap, fl4InstallTcbFrameCap] + refine mem_bindE_okD (a := ()) (s' := σ1) (hrk σ1) ?_ + refine mem_bindE_okD (a := ()) (s' := σ1) (hrk σ1) ?_ + refine mem_bindE_okD (a := ()) (s' := σ1) (hrk σ1) ?_ + refine mem_bindE_okD (a := ()) (s' := σ1) (hrk σ1) ?_ + refine mem_bindE_okD (a := ()) (s' := bufStateA σd (specPtr t) y) ?_ (by rw [run_returnOkD]; simp) + refine mem_bindE_okD (a := ()) (s' := σd) hdel ?_ + refine mem_bindE_okD (a := ()) (s' := bufStateA σd (specPtr t) y) (mem_liftED hs1) ?_ + refine mem_bindE_okD (a := ()) (s' := bufStateA σd (specPtr t) y) (mem_liftED hs2) ?_ + refine mem_bindE_okD (a := σd.curThread) (s' := bufStateA σd (specPtr t) y) (mem_liftED ?_) ?_ + · simp [Fl4Spec.NonDet.gets, bufStateA, updKSc] + · refine mem_liftED ?_ + simp [Fl4Spec.NonDet.when, if_neg (Ne.symm hne)] + all_goals first | exact Set.mem_singleton _ | exact rfl | simp [Fl4Spec.NonDet.pure] + + +/-- **The spec's buffer-only `invoke_tcb` when its `cap_delete` fails** (`Preempted`). -/ +theorem setIpcBuf_invokeErrA {σ1 σd : Fl4SpecA64.State Unit} {t : U64} (κs : CslotPtr) + (hdel : (.error (), σd) ∈ ((fl4CapDelete (Z := Unit) (specPtr t, tcbCnodeIndex 2)).run σ1).1) : + (.error (), σd) ∈ + ((fl4InvokeTcb (.threadControlCaps (specPtr t) κs none none none none + (some (Fl4SpecA64.dataToVref (0#u64).bv, none)))).run σ1).1 := by + have hrk : ∀ σ : Fl4SpecA64.State Unit, (.ok (), σ) ∈ ((NonDetE.returnOk (ε := Unit) ()).run σ).1 := fun σ => by + rw [run_returnOkD]; simp + simp only [fl4InvokeTcb, fl4InstallTcbCap, fl4InstallTcbFrameCap] + refine mem_bindE_okD (a := ()) (s' := σ1) (hrk σ1) ?_ + refine mem_bindE_okD (a := ()) (s' := σ1) (hrk σ1) ?_ + refine mem_bindE_okD (a := ()) (s' := σ1) (hrk σ1) ?_ + refine mem_bindE_okD (a := ()) (s' := σ1) (hrk σ1) ?_ + exact mem_bindE_errD (mem_bindE_errD hdel) + +section +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {Clock : A → Fl4SpecA64.MachineState → Prop} + +set_option maxHeartbeats 4000000 in +/-- **TCB SetIPCBuffer with address 0 on another thread at a syscall entry** (module docstring). -/ +theorem JSyscallA.setIpcBufZeroA [PlainStore st] + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (s : S) (a : A) (σ : Fl4SpecA64.State Unit) (m : Fl4SpecA64.McState) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) (hCnt : CounterStepA σ) + (P : DelPlatA st ar AR Clock) (hCl : Clock a σ.machineState) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + {slot lbl len buf t src : U64} {blk call don : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Thread t) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hlbl : lbl = fl4_abi.label.TCBSetIPCBuffer) + (he : tcb.extra_with_length ⟨len, buf, call, slot, extra⟩ 0#usize 1#u64 = ok (.Ok src)) + (harg : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 0#u64 = ok 0#u64) (hne : t ≠ L.curW s) : + ∃ r s3 a3, syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (r, s3, a3) ∧ + TcbDelOutA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n m ar hPl.ipa s a σ + (fun κs => .threadControlCaps (specPtr t) κs none none none none + (some (Fl4SpecA64.dataToVref (0#u64).bv, none))) r s3 a3 := by + have ht : t ∈ Z.T := J.tcbs ▸ (J.toJStage3A.slotCapTy hslot).1.2.2.2.2.2 t hcap + have htT : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + have hthT : Z.th t = some .tcb := (J.j.1.thT t).2 ht + have T0 := J.toJStage3A.toTA J.sact J.noSwitch + have hstall : smp.remote_tcb_stall st ar s a t = ok (s, a) := T0.stall_singleA hCs h1 ht + set sl : Fl4SpecA64.CslotPtr := (specPtr t, tcbCnodeIndex 2) with hsl + have hAd : AbsSlotA Z.th sl := ⟨t, rfl, Or.inr ⟨hthT, 2, by decide, rfl⟩⟩ + have sbw : cslotAddrA sl = sbWordA t := by + unfold cslotAddrA + show slotAddrA (specPtr t) (Fl4SpecA64.ofBl (tcbCnodeIndex 2)) = _ + rw [ofBl_tcbIdx 2 (by decide), bufSlot_eqA] + have hdest : obj.Tcb.slot t layout.tcb.SLOT_BUFFER = ok (cslotAddrA sl) := by + rw [sbw]; simp [obj.Tcb.slot, lift, sbWordA] + have hslotIdx : slotIdx t layout.tcb.SLOT_BUFFER = cslotAddrA sl := + ok_inj_ff ((tcb_slot_val t _).symm.trans hdest) + obtain ⟨xr, heq, hset, D1, hdelWP⟩ := J.restartDelA s a σ m hPl hCnt hslot hcov hext P hCl hCs sl hAd + obtain ⟨x', hx', -, hset', -⟩ := J.restartMachA hslot hcov hext + have e1 : stUpdA σ σ.curThread xr .restart = stUpdA σ σ.curThread x' .restart := by + have := Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst (hset.symm.trans hset')) + exact (Prod.mk.inj this).2 + have hr : tcb.restart_caller st s = ok (L.wr s (stA (L.curW s)) sched.TS_RESTART) := by + unfold tcb.restart_caller + rw [cur_thread_ok, bind_ok, heq] + obtain ⟨⟨r, sd, ad⟩, hdel0, σd, Gd, Dd, hres, hW⟩ := spec_existsA hdelWP + have sysd := J.sysDelSlotA hA hPl hslot hcov hext hcap layout.tcb.SLOT_BUFFER (by simp [layout.tcb.SLOT_BUFFER]) + hstall hr (r := r) (sd := sd) (ad := ad) (by rw [hslotIdx]; exact hdel0) + have hcur0 : σ.curThread = specPtr (L.curW s) := J.j.1.sch.curThread + have hdecFin := fun (rr : core.result.Result Unit error.Exc) (s3 : S) (a3 : A) + (hd : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (rr, s3, a3)) (σ3 : Fl4SpecA64.State Unit) => + J.decodeFinA hA hM hPl hfm hslot hcov (rfl : capA st s slot = capA st s slot) hext hSC (lbl := lbl) + (by rw [hcap, hlbl]; simp [DecArmS1aEnt, DecArmQH, DecArmDelQ]) + (fun _ h => by rw [hcap] at h; cases h) (fun _ h => by rw [hcap] at h; cases h) hd σ3 m + have hdecBase : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + tcb.install_buffer st ar (L.wr s (stA (L.curW s)) sched.TS_RESTART) a t slot 0#u64 (capA st s src, 0#u64) := by + rw [hcap, decodeTcb_programA, if_neg Bool.false_ne_true, hlbl, + tcbSetIPCBuffer_selectedA (sel := (capA st s src, 0#u64)) hstall he harg + (tcbDeriveBuffer_zeroA (capA_eq s src)) hr] + rcases hres with ⟨hr1, hnull, hspec, -⟩ | ⟨hr1, hspec⟩ + · subst hr1 + obtain ⟨⟨⟨-, X, sv⟩, -⟩, Binv, Own⟩ := hW + obtain ⟨Q, R, hw, Ac', Bf', stack', Jd⟩ := X.toStage3A sysd.1 sysd.2 sv + have ht' : t ∈ ({ Z with G := Gd, Q := Q, R := R, hw := hw } : XSetsA).T := ht + obtain ⟨yd, hyd, -⟩ := Jd.mcp t ht' + have hnb := ipcBufNone_ofNullA Dd.rel hthT hyd (by rw [← sbw]; exact hnull) + obtain ⟨blk', K⟩ := Jd.setIpcBuf_stepA (t := t) ht' hyd hnb + have hcurd : L.curW sd = L.curW s := + specPtr_inj ((Jd.j.1.sch.curThread).symm.trans (Binv.2.1.trans hcur0)) + have hcurw : L.curW (L.wr sd (wa t 3512#u64) 0#u64) = L.curW sd := by + have hq := Jd.sys.ent.bxi.1.choose_spec.choose_spec + have hn : NodePlaced st H.tcbs sd (L.cpu sd) := hq.nodes _ hq.cur + have hta : TcbAddr t := Jd.sr.core.tcbs t ht' + have c0 : layout.node.CUR_THREAD.val < 4288 := by simp + have al : Aligned (wa t 3512#u64) := field_aligned hta (K := 3512#u64) (by simp) (by simp) + have hw' : WritesIn st sd (L.wr sd (wa t 3512#u64) 0#u64) (· = wa t 3512#u64) := writesIn_wr al + have ecpu : L.cpu (L.wr sd (wa t 3512#u64) 0#u64) = L.cpu sd := L.cpu_wr _ _ _ + unfold LawfulStore.curW + rw [ecpu, nodeAt_of_ksb hw'.1] + refine hw'.mem_eq (nodeAt_aligned hn c0 (by simp)) fun e => ?_ + exact node_ne_field hn c0 htT hta (K := 3512#u64) (by simp) e + have hBuf : obj.Tcb.set_ipc_buffer st t sd 0#u64 = ok (L.wr sd (wa t 3512#u64) 0#u64) := + eq_of_spec_fd (Fl4ProofsA64.P1.obj.Tcb.set_ipc_buffer.wr sd t 0#u64) + have hneW : ¬ t = L.curW (L.wr sd (wa t 3512#u64) 0#u64) := by rw [hcurw, hcurd]; exact hne + have hdecOk : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (.Ok (), L.wr sd (wa t 3512#u64) 0#u64, ad) := by + rw [hdecBase, tcbInstallBuffer_clearA hdest hdel0 hBuf, if_neg hneW]; simp only [bind_ok] + obtain ⟨-, -, sysl, symL, -, userOK, j7⟩ := hdecFin _ _ _ hdecOk (bufStateA σd (specPtr t) yd) + have J2 := K sysl symL userOK j7 + have dsk : DSkA σd (bufStateA σd (specPtr t) yd) := + DSkA.upd (p := specPtr t) (o := .tcb yd) (o' := .tcb { yd with tcbIpcBuffer := 0 }) hyd + (fun y k h => by cases h) (fun y k h => by cases h) (fun x'' h => ⟨yd, rfl, by rw [← KernelObject.tcb.inj h]; exact ⟨rfl, Or.inl rfl, rfl⟩⟩) + have kstep : DelStepA σd (bufStateA σd (specPtr t) yd) := DelStepA.ofWk Jd.sinv.curSc dsk.toDWkA (Or.inl dsk) dsk.ch + have B2 := BodyOKA.stepA Binv kstep + have hne2 : σd.curThread ≠ specPtr t := fun e => hne (specPtr_inj (e.symm.trans (Binv.2.1.trans hcur0))) + refine ⟨.Ok (), _, ad, hdecOk, x', stUpdA σ σ.curThread x' .restart, _, hx', rfl, hset', Gd, Q, R, hw, Ac', Bf', + stack', J2, Or.inl ⟨rfl, fun κs => ?_, B2.1, B2.2.1⟩⟩ + rw [e1] at hspec + exact setIpcBuf_invokeA κs hspec hyd hne2 + · subst hr1 + obtain ⟨⟨⟨-, X, sv⟩, -⟩, hpre⟩ := hW + obtain ⟨Q, R, hw, Ac', Bf', stack', Jd⟩ := X.toStage3A sysd.1 sysd.2 sv + have hdecP : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (.Err .Preempted, sd, ad) := by + rw [hdecBase] + simp only [tcb.install_buffer, hdest, hdel0, bind_ok, uncurry_apply_pair, + core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame.from, + bind_tc_ok] + refine ⟨.Err .Preempted, sd, ad, hdecP, x', stUpdA σ σ.curThread x' .restart, σd, hx', rfl, hset', Gd, Q, R, hw, + Ac', Bf', stack', Jd, Or.inr ⟨rfl, fun κs => ?_, hpre.1, hpre.2.1⟩⟩ + rw [e1] at hspec + exact setIpcBuf_invokeErrA κs hspec + +end + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3LiftTcbBufNzA.lean b/proofs/Fl4ProofsA64/P3/Stage3LiftTcbBufNzA.lean new file mode 100644 index 000000000..b97c74d8b --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3LiftTcbBufNzA.lean @@ -0,0 +1,526 @@ +/- +P3 (aarch64, A64-P3 setipcbuf): **TCB SetIPCBuffer with a non-zero address on another thread, at the record**. + +* **`bufLiveOfFrameA`**: a live non-device frame cap of an `OS` slot, with a 1024-aligned user address `baddr ≠ 0`, gives the + kernel address `bufAddrOfA ptr baddr bits` of the buffer: `BufLiveA` (non-zero because the heap has no object at 0) + and the frame's alignment `ptr % 2^bits = 0` (from `FrameOK` and `HeapOK.align`). +* **`frameWord_ne_ibA`**: a live frame word is not the IPC-buffer word `[t + 3512]` of a TCB. +* **`bufMid*A`**: the middle of the spec's `invoke_tcb` for the buffer (the two `check_cap_at`s and `cap_insert`), + for the three outcomes (source changed, invoked slot changed, insert). +* **`JSyscallA.setIpcBufNzA`**: `decode_invocation` on a Thread cap with the SetIPCBuffer label, a non-zero 1024-aligned + address and a non-device Frame source: `restartDelGA` on `Ag` = the 128 words of the new buffer, the delete of slot 2, + `set_ipc_buffer baddr` (`setIpcBuf_stepVA`), `still_valid`, then `cdt::insert` of the unmapped copy + (`capInsertTcbBufA`, growing `Bf`; the P1 facts at the post-insert state from `JStage3A.insertP1A`) when it holds; + then `reschedule_required` when the target is the current thread (`JStage3A.reschedSelf_stepA`). + +Named premises: those of `JSyscallA.setIpcBufZeroA`, and the entry agreement `hag` ([S], Architect ruling (b'): the 128 +words of the new buffer agree between the store and the spec's user memory at entry). New module. +-/ +import Fl4ProofsA64.P3.Stage3TcbBufStepsA +import Fl4ProofsA64.P3.Stage3TcbReschedA +import Fl4ProofsA64.P3.Stage3InsP1A + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Vcpu Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + +/-- **The buffer of a live frame cap** (module docstring). -/ +theorem bufLiveOfFrameA {H : Heap} {OS : Set U64} {s : S} (hH : HeapOK H) (h0 : H 0#u64 = none) + (hfo : FrameOK st H OS s) {src ptr rights asid va baddr bits : U64} {size : capability.FrameSize} {mp ep : Bool} + (hsrc : src ∈ OS) (hc : capA st s src = .Frame ptr size rights false mp ep asid va) (hbits : size.bits = ok bits) + (hbal : baddr.val % 1024 = 0) (hbn : baddr ≠ 0#u64) : + ptr.val % 2 ^ bits.val = 0 ∧ BufLiveA st OS s (bufAddrOfA ptr baddr bits) := by + have hfp : H ptr = some (.other bits.val) := hfo src hsrc ptr size rights mp ep asid va hc bits hbits + have hal : ptr.val % 2 ^ bits.val = 0 := by + have h := hH.align ptr _ hfp + simpa [HKind.bits] using h + obtain ⟨-, h1024, hle, hlt⟩ := bufAddr_rangeA (frame_bitsLtA hbits) hal hbal + have hp0 : ptr.val ≠ 0 := fun e => by + have : ptr = 0#u64 := UScalar.eq_of_val_eq e + rw [this, h0] at hfp + cases hfp + refine ⟨hal, fun e => ?_, h1024, fun k hk => ⟨src, hsrc, ptr, size, rights, mp, ep, asid, va, bits, hc, hbits, ?_, ?_⟩⟩ + · have := congrArg UScalar.val e + rw [show (0#u64 : U64).val = 0 from rfl] at this + omega + · rw [bufW_valLiveA h1024 hk]; omega + · rw [bufW_valLiveA h1024 hk]; omega + +/-- **A live frame word is not the IPC-buffer word of a TCB.** -/ +theorem frameWord_ne_ibA {H : Heap} {OS : Set U64} {s : S} (hH : HeapOK H) (hfo : FrameOK st H OS s) {t b : U64} + (hta : TcbAddr t) (htH : H t = some .tcb) (lv : FrameWordLive st OS s b) : wa t 3512#u64 ≠ b := by + obtain ⟨u, hu, ptr, size, rights, x1, x2, x3, x4, bits, hc, hb, h1, h2⟩ := lv + have hfp : H ptr = some (.other bits.val) := hfo u hu ptr size rights x1 x2 x3 x4 hc bits hb + have := frameWord_offA hH hfp htH (fun z => by simp) h1 h2 + have vM : (wa t 3512#u64).val = t.val + 3512 := by + show (wa t 3512#u64).val = _ + rw [Notif.tfield_val hta (by decide : (3512#u64 : U64).val < 4096)]; rfl + intro e + have ev := congrArg UScalar.val e + simp only [HKind.bits] at this + omega + +end + +section spec + +/-- The spec's two `check_cap_at`s and `cap_insert` when the source's cap is no longer the new cap's object. -/ +theorem bufMidSrcA {σ : State Unit} {target : ObjRef} {κs κsrc : CslotPtr} {newCap source : Cap} + (hsource : (source, σ) ∈ (getCap (Z := Unit) κsrc σ).1) (hmatch : ¬ fl4TcbSameObject newCap source) : + ((), σ) ∈ ((getCap (Z := Unit) κsrc >>= fun live => + Fl4Spec.NonDet.when (fl4TcbSameObject newCap live) (getCap (Z := Unit) κs >>= fun cap => + Fl4Spec.NonDet.when (fl4TcbSameObject (.threadCap target) cap) + (capInsert (Z := Unit) newCap κsrc (target, tcbCnodeIndex 2)))) σ).1 := by + refine mem_bind_ndD hsource ?_ + rw [Fl4Spec.NonDet.when, if_neg hmatch] + exact Set.mem_singleton _ + +/-- … when the invoked slot no longer holds the thread's cap. -/ +theorem bufMidInvA {σ : State Unit} {target : ObjRef} {κs κsrc : CslotPtr} {newCap source current : Cap} + (hsource : (source, σ) ∈ (getCap (Z := Unit) κsrc σ).1) (hmatch : fl4TcbSameObject newCap source) + (hinvoked : (current, σ) ∈ (getCap (Z := Unit) κs σ).1) (htarget : ¬ fl4TcbSameObject (.threadCap target) current) : + ((), σ) ∈ ((getCap (Z := Unit) κsrc >>= fun live => + Fl4Spec.NonDet.when (fl4TcbSameObject newCap live) (getCap (Z := Unit) κs >>= fun cap => + Fl4Spec.NonDet.when (fl4TcbSameObject (.threadCap target) cap) + (capInsert (Z := Unit) newCap κsrc (target, tcbCnodeIndex 2)))) σ).1 := by + refine mem_bind_ndD hsource ?_ + rw [Fl4Spec.NonDet.when, if_pos hmatch] + refine mem_bind_ndD hinvoked ?_ + rw [Fl4Spec.NonDet.when, if_neg htarget] + exact Set.mem_singleton _ + +/-- … when both hold and the insert runs. -/ +theorem bufMidInsA {σ σ' : State Unit} {target : ObjRef} {κs κsrc : CslotPtr} {newCap source current : Cap} + (hsource : (source, σ) ∈ (getCap (Z := Unit) κsrc σ).1) (hmatch : fl4TcbSameObject newCap source) + (hinvoked : (current, σ) ∈ (getCap (Z := Unit) κs σ).1) (htarget : fl4TcbSameObject (.threadCap target) current) + (hinsert : ((), σ') ∈ (capInsert (Z := Unit) newCap κsrc (target, tcbCnodeIndex 2) σ).1) : + ((), σ') ∈ ((getCap (Z := Unit) κsrc >>= fun live => + Fl4Spec.NonDet.when (fl4TcbSameObject newCap live) (getCap (Z := Unit) κs >>= fun cap => + Fl4Spec.NonDet.when (fl4TcbSameObject (.threadCap target) cap) + (capInsert (Z := Unit) newCap κsrc (target, tcbCnodeIndex 2)))) σ).1 := by + refine mem_bind_ndD hsource ?_ + rw [Fl4Spec.NonDet.when, if_pos hmatch] + refine mem_bind_ndD hinvoked ?_ + rw [Fl4Spec.NonDet.when, if_pos htarget] + exact hinsert + +end spec + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {Clock : A → Fl4SpecA64.MachineState → Prop} + +set_option maxHeartbeats 16000000 in +/-- **TCB SetIPCBuffer with a non-zero address on another thread at a syscall entry** (module docstring). -/ +theorem JSyscallA.setIpcBufNzA [PlainStore st] + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (s : S) (a : A) (σ : Fl4SpecA64.State Unit) (m : Fl4SpecA64.McState) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) (hCnt : CounterStepA σ) + (P : DelPlatA st ar AR Clock) (hCl : Clock a σ.machineState) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + {slot lbl len buf t src baddr ptr rights asid va bits : U64} {size : capability.FrameSize} {mp ep : Bool} + {blk call don : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Thread t) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hlbl : lbl = fl4_abi.label.TCBSetIPCBuffer) + (he : tcb.extra_with_length ⟨len, buf, call, slot, extra⟩ 0#usize 1#u64 = ok (.Ok src)) + (harg : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 0#u64 = ok baddr) (hbn : baddr ≠ 0#u64) + (hbal : baddr.val % 1024 = 0) + (hsrcF : capA st s src = .Frame ptr size rights false mp ep asid va) (hbits : size.bits = ok bits) + (hag : FrAgA st OS {b | ∃ k : U64, k.val < 128 ∧ b = bufW (bufAddrOfA ptr baddr bits) k} s σ) + {κs κsrc : CslotPtr} (hAκ : AbsSlotA Z.th κs) (hκ : cslotAddrA κs = slot) + (hAsrc : AbsSlotA Z.th κsrc) (hκsrc : cslotAddrA κsrc = src) : + ∃ r s3 a3, syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (r, s3, a3) ∧ + TcbDelOutA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n m ar hPl.ipa s a σ + (fun _ => .threadControlCaps (specPtr t) κs none none none none + (some (Fl4SpecA64.dataToVref baddr.bv, + some (absCapA (.Frame ptr size rights false false false 0#u64 0#u64), κsrc)))) r s3 a3 := by + have ht : t ∈ Z.T := J.tcbs ▸ (J.toJStage3A.slotCapTy hslot).1.2.2.2.2.2 t hcap + have htT : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + have hthT : Z.th t = some .tcb := (J.j.1.thT t).2 ht + have T0 := J.toJStage3A.toTA J.sact J.noSwitch + have hstall : smp.remote_tcb_stall st ar s a t = ok (s, a) := T0.stall_singleA hCs h1 ht + obtain ⟨hsrcM, hsrc0⟩ := tcbExtra0_memA he + have hsrcOS : src ∈ OS := hext _ hsrcM hsrc0 + obtain ⟨ss', ps, hss1, hss2, hss3, -⟩ := J.toJStage3A.cnSlot_of_covA (hSC _ hsrcM hsrc0) + have ess : κsrc = ss' := + cslotAddrA_inj J.j.1.place hAsrc ⟨ps, hss1, Or.inl hss2⟩ (hκsrc.trans hss3.symm) + subst ess + obtain ⟨si', pi, hsi1, hsi2, hsi3, -⟩ := J.toJStage3A.cnSlot_of_covA hcov + have esi : κs = si' := + cslotAddrA_inj J.j.1.place hAκ ⟨pi, hsi1, Or.inl hsi2⟩ (hκ.trans hsi3.symm) + subst esi + set sl : Fl4SpecA64.CslotPtr := (specPtr t, tcbCnodeIndex 2) with hsl + have hAd : AbsSlotA Z.th sl := ⟨t, rfl, Or.inr ⟨hthT, 2, by decide, rfl⟩⟩ + have sbw : cslotAddrA sl = sbWordA t := by + unfold cslotAddrA + show slotAddrA (specPtr t) (Fl4SpecA64.ofBl (tcbCnodeIndex 2)) = _ + rw [ofBl_tcbIdx 2 (by decide), bufSlot_eqA] + have hdest : obj.Tcb.slot t layout.tcb.SLOT_BUFFER = ok (cslotAddrA sl) := by + rw [sbw]; simp [obj.Tcb.slot, lift, sbWordA] + have hslotIdx : slotIdx t layout.tcb.SLOT_BUFFER = cslotAddrA sl := + ok_inj_ff ((tcb_slot_val t _).symm.trans hdest) + have hneS : src ≠ cslotAddrA sl := fun e => by + have h := cslotAddrA_inj J.j.1.place hAsrc hAd (hκsrc.trans e) + have e1 : specPtr ps = specPtr t := by rw [← hss1, h] + rw [specPtr_inj e1, hthT] at hss2 + cases hss2 + have hneI : slot ≠ cslotAddrA sl := fun e => by + have h := cslotAddrA_inj J.j.1.place hAκ hAd (hκ.trans e) + have e1 : specPtr pi = specPtr t := by rw [← hsi1, h] + rw [specPtr_inj e1, hthT] at hsi2 + cases hsi2 + obtain ⟨xr, heq, hset, D1, hdelWP⟩ := J.restartDelGA s a σ m hPl hCnt hslot hcov hext P hCl hCs hag sl hAd + obtain ⟨x', hx', -, hset', -⟩ := J.restartMachA hslot hcov hext + have e1 : stUpdA σ σ.curThread xr .restart = stUpdA σ σ.curThread x' .restart := by + have := Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst (hset.symm.trans hset')) + exact (Prod.mk.inj this).2 + have hr : tcb.restart_caller st s = ok (L.wr s (stA (L.curW s)) sched.TS_RESTART) := by + unfold tcb.restart_caller + rw [cur_thread_ok, bind_ok, heq] + obtain ⟨⟨r, sd, ad⟩, hdel0, σd, Gd, Dd, hres, hW⟩ := spec_existsA hdelWP + have sysd := J.sysDelSlotA hA hPl hslot hcov hext hcap layout.tcb.SLOT_BUFFER (by simp [layout.tcb.SLOT_BUFFER]) + hstall hr (r := r) (sd := sd) (ad := ad) (by rw [hslotIdx]; exact hdel0) + have hcur0 : σ.curThread = specPtr (L.curW s) := J.j.1.sch.curThread + have hdecFin := fun (rr : core.result.Result Unit error.Exc) (s3 : S) (a3 : A) + (hd : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (rr, s3, a3)) (σ3 : Fl4SpecA64.State Unit) => + J.decodeFinA hA hM hPl hfm hslot hcov (rfl : capA st s slot = capA st s slot) hext hSC (lbl := lbl) + (by rw [hcap, hlbl]; simp [DecArmS1aEnt, DecArmQH, DecArmDelQ]) + (fun _ h => by rw [hcap] at h; cases h) (fun _ h => by rw [hcap] at h; cases h) hd σ3 m + -- the derived frame + set fr : capability.Cap := .Frame ptr size rights false false false 0#u64 0#u64 with hfr + have hCapF : obj.Slot.cap st src s = ok (.Frame ptr size rights false mp ep asid va) := + (capA_eq s src).trans (by rw [hsrcF]) + have hal : isAligned baddr.bv msgAlignBits := bufAligned_ofModA baddr hbal + have hder : tcb.derive_for_buffer st s baddr src = ok (.Ok (fr, src)) := by + rw [tcbDeriveBuffer_frameA hbn hCapF] + simp only [tcbBufferCheck_nativeA, bind_ok, Bool.false_eq_true, ite_false, hal, ite_true, hfr] + have hdecBase : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + tcb.install_buffer st ar (L.wr s (stA (L.curW s)) sched.TS_RESTART) a t slot baddr (fr, src) := by + rw [hcap, decodeTcb_programA, if_neg Bool.false_ne_true, hlbl, + tcbSetIPCBuffer_selectedA (sel := (fr, src)) hstall he harg hder hr] + rcases hres with ⟨hr1, hnull, hspec, hch⟩ | ⟨hr1, hspec⟩ + swap + · subst hr1 + obtain ⟨⟨⟨-, X, sv⟩, -⟩, hpre⟩ := hW + obtain ⟨Q, R, hw, Ac', Bf', stack', Jd, -⟩ := X.toStage3FrA sysd.1 sysd.2 sv + have hdecP : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (.Err .Preempted, sd, ad) := by + rw [hdecBase] + simp only [tcb.install_buffer, hdest, hdel0, bind_ok, uncurry_apply_pair, + core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame.from, + bind_tc_ok] + refine ⟨.Err .Preempted, sd, ad, hdecP, x', stUpdA σ σ.curThread x' .restart, σd, hx', rfl, hset', Gd, Q, R, hw, + Ac', Bf', stack', Jd, Or.inr ⟨rfl, fun _ => ?_, hpre.1, hpre.2.1⟩⟩ + rw [e1] at hspec + exact setIpcBufG_invokeErrA _ _ _ hspec + subst hr1 + obtain ⟨⟨⟨-, X, sv⟩, -⟩, Binv, Own⟩ := hW + have sactd : SActA σd := by + obtain ⟨⟨_, _, _, _, _, _, JT, -⟩, -⟩ := X + exact JT.sact + obtain ⟨Q, R, hw, Ac', Bf', stack', Jd, fa⟩ := X.toStage3FrA sysd.1 sysd.2 sv + rw [e1] at hspec + have ht' : t ∈ ({ Z with G := Gd, Q := Q, R := R, hw := hw } : XSetsA).T := ht + obtain ⟨yd, hyd, -⟩ := Jd.mcp t ht' + have hnb := ipcBufNone_ofNullA Dd.rel hthT hyd (by rw [← sbw]; exact hnull) + obtain ⟨blk', K⟩ := Jd.setIpcBuf_stepVA (t := t) ht' hyd hnb baddr hbal + have hcurd : L.curW sd = L.curW s := + specPtr_inj ((Jd.j.1.sch.curThread).symm.trans (Binv.2.1.trans hcur0)) + have hta : TcbAddr t := Jd.sr.core.tcbs t ht' + have aM : Aligned (wa t 3512#u64) := field_aligned hta (K := 3512#u64) (by simp) (by simp) + have vM : (wa t 3512#u64).val = t.val + 3512 := by + show (wa t 3512#u64).val = _ + rw [Notif.tfield_val hta (by decide : (3512#u64 : U64).val < 4096)]; rfl + have hcurw : L.curW (L.wr sd (wa t 3512#u64) baddr) = L.curW sd := by + have hq := Jd.sys.ent.bxi.1.choose_spec.choose_spec + have hn : NodePlaced st H.tcbs sd (L.cpu sd) := hq.nodes _ hq.cur + have c0 : layout.node.CUR_THREAD.val < 4288 := by simp + have al : Aligned (wa t 3512#u64) := aM + have hw' : WritesIn st sd (L.wr sd (wa t 3512#u64) baddr) (· = wa t 3512#u64) := writesIn_wr al + have ecpu : L.cpu (L.wr sd (wa t 3512#u64) baddr) = L.cpu sd := L.cpu_wr _ _ _ + unfold LawfulStore.curW + rw [ecpu, nodeAt_of_ksb hw'.1] + refine hw'.mem_eq (nodeAt_aligned hn c0 (by simp)) fun e => ?_ + exact node_ne_field hn c0 htT hta (K := 3512#u64) (by simp) e + have hBuf : obj.Tcb.set_ipc_buffer st t sd baddr = ok (L.wr sd (wa t 3512#u64) baddr) := + eq_of_spec_fd (Fl4ProofsA64.P1.obj.Tcb.set_ipc_buffer.wr sd t baddr) + set sB := L.wr sd (wa t 3512#u64) baddr with hsB + set σB := bufStateVA σd (specPtr t) yd baddr with hσB + have hcurB : L.curW sB = L.curW s := hcurw.trans hcurd + -- the P1 facts at the state after the buffer write + have wW : WritesIn st sd sB (SchedStepNWA (st := st) N E Rp + ({ Z with G := Gd, Q := Q, R := R, hw := hw } : XSetsA) m σd sd) := + (writesIn_wr aM).mono fun b hb => by + subst hb + exact Or.inl (Or.inr (tcbStepWA_of_off Jd.iplaced.e.tcb ht' 3512 (by omega) (by omega) (by omega) + (by omega) vM)) + obtain ⟨syslB, symLB⟩ := J.sysDelBufSetA hA hPl hslot hcov hext hcap hstall hr + (by rw [hslotIdx]; exact hdel0) baddr hbal + obtain ⟨userB, -, j7B⟩ := Jd.p1StepSyA hA hPl wW ⟨syslB, symLB⟩ blk' + have JB := K syslB symLB userB (j7B σB m) + have hkeepB : ∀ y ∈ OS, capA st sB y = capA st sd y := fun y hy => (OSKeep.capA (Jd.osKeep_stepNA wW) hy) + -- the source, the invoked slot and the destination after the delete + have hkeepS : capA st sd src = .Frame ptr size rights false mp ep asid va ∨ capA st sd src = .Null ∨ + HomeZA st sd src := by + rcases hch src hsrcOS hneS with h | h | h + · exact Or.inl (h.trans hsrcF) + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr h) + have hkeepI : capA st sd slot = .Thread t ∨ capA st sd slot = .Null ∨ HomeZA st sd slot := by + rcases hch slot hslot hneI with h | h | h + · exact Or.inl (h.trans hcap) + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr h) + have hkeepSB : capA st sB src = .Frame ptr size rights false mp ep asid va ∨ capA st sB src = .Null ∨ + HomeZA st sB src := by + rcases hkeepS with h | h | ⟨zt, k, h⟩ + · exact Or.inl ((hkeepB src hsrcOS).trans h) + · exact Or.inr (Or.inl ((hkeepB src hsrcOS).trans h)) + · exact Or.inr (Or.inr ⟨zt, k, (hkeepB src hsrcOS).trans h⟩) + have hkeepIB : capA st sB slot = .Thread t ∨ capA st sB slot = .Null ∨ HomeZA st sB slot := by + rcases hkeepI with h | h | ⟨zt, k, h⟩ + · exact Or.inl ((hkeepB slot hslot).trans h) + · exact Or.inr (Or.inl ((hkeepB slot hslot).trans h)) + · exact Or.inr (Or.inr ⟨zt, k, (hkeepB slot hslot).trans h⟩) + have hSV := tcbStillValidFr_keptA (st := st) sB t slot src hsrc0 ptr rights asid va size mp ep hkeepSB hkeepIB + have hdOS : cslotAddrA sl ∈ OS := J.j5.2.1 _ (hAd.isSlot J.j.1.place) + have hnullB : capA st sB (cslotAddrA sl) = .Null := (hkeepB _ hdOS).trans hnull + have hsrcSlot : IsSlotA Z.th (cslotAddrA κsrc) := hAsrc.isSlot J.j.1.place + -- the spec's two reads at the state after the buffer write + obtain ⟨cS, hgS, hcS⟩ := getCap_eqA JB.j.1.st (show AbsSlotA Z.th κsrc from hAsrc) + obtain ⟨cI, hgI, hcI⟩ := getCap_eqA JB.j.1.st (show AbsSlotA Z.th κs from hAκ) + rw [capAtA_eq (st := st), hκsrc] at hcS + rw [capAtA_eq (st := st), hκ] at hcI + have mS : (cS, σB) ∈ (getCap (Z := Unit) κsrc σB).1 := by rw [hgS]; exact Set.mem_singleton _ + have mI : (cI, σB) ∈ (getCap (Z := Unit) κs σB).1 := by rw [hgI]; exact Set.mem_singleton _ + have eS : capA st sB src = .Frame ptr size rights false mp ep asid va → + cS = absCapA (.Frame ptr size rights false mp ep asid va) := fun h => by + rw [h] at hcS + exact (CapEqA.eq_abs hcS (fun _ _ _ h => by cases h)).symm + have eI : capA st sB slot = .Thread t → cI = .threadCap (specPtr t) := fun h => by + rw [h] at hcI + exact (CapEqA.eq_abs hcI (fun _ _ _ h => by cases h)).symm + have hnSpec : absCapA fr ≠ .nullCap := by simp [hfr, absCapA, absFrameE] + have hsameT : fl4TcbSameObject (.threadCap (specPtr t)) (.threadCap (specPtr t)) := by + simp [fl4TcbSameObject, fl4TcbFrame, sameObjectAs, sameRegionAs, isEpCap, isThreadCap, objRefOf] + have hsameF : fl4TcbSameObject (absCapA fr) (absCapA (.Frame ptr size rights false mp ep asid va)) := by + cases ep <;> simp [ExtArchCap.stage2FrameCap, ExtArchCap.objRef, hfr, absCapA, absFrameE, ArchCap.isFrameCap, ArchCap.acapObj, ArchCap.acapRights, ArchCap.acapFsize, archCapIsDevice, fl4TcbSameObject, fl4TcbFrame, sameObjectAs, sameRegionAs, isEpCap, + isThreadCap, objRefOf] + have kstep : DelStepA σd σB := by + have dsk : DSkA σd σB := + DSkA.upd (p := specPtr t) (o := .tcb yd) (o' := .tcb { yd with tcbIpcBuffer := baddr.bv }) hyd + (fun y k h => by cases h) (fun y k h => by cases h) + (fun x'' h => ⟨yd, rfl, by rw [← KernelObject.tcb.inj h]; exact ⟨rfl, Or.inl rfl, rfl⟩⟩) + exact (DelStepA.ofWk Jd.sinv.curSc dsk.toDWkA (Or.inl dsk) dsk.ch).1 + have B1 := BodyOKA.stepA Binv kstep + have dskB : DSkA σd σB := + DSkA.upd (p := specPtr t) (o := .tcb yd) (o' := .tcb { yd with tcbIpcBuffer := baddr.bv }) hyd + (fun y k h => by cases h) (fun y k h => by cases h) + (fun x'' h => ⟨yd, rfl, by rw [← KernelObject.tcb.inj h]; exact ⟨rfl, Or.inl rfl, rfl⟩⟩) + -- the conditional reschedule, from the record before it + have tail : ∀ (G2 : MdbGhostA) (Ac2 : Set U64) (Bf2 : Set (U64 × Fl4SpecA64.Word)) (stack2 : U64 → List U64) + (sf : S) (σf : Fl4SpecA64.State Unit), + JStage3A st AR { Z with G := G2, Q := Q, R := R, hw := hw } H OS U Ac2 Pp Tb Lv ia ie N E Rp Bf2 stack2 κ n + (J5A st) (J7A st (J7R2A st ar hPl.ipa)) sf ad σf m → + L.curW sf = L.curW s → DelBodyInvA σ.curThread m σf → DSkA σd σf → + (∀ s', (if t = L.curW sf then sched.reschedule_required st sf else ok sf) = ok s' → + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (.Ok (), s', ad)) → + ∃ s' σ' Ac' Bf' stack', + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (.Ok (), s', ad) ∧ + ((σf.curThread ≠ specPtr t ∧ σ' = σf) ∨ + (σf.curThread = specPtr t ∧ ((), σ') ∈ (rescheduleRequired (Z := Unit) σf).1)) ∧ + JStage3A st AR { Z with G := G2, Q := Q, R := R, hw := hw } H OS U Ac' Pp Tb Lv ia ie N E Rp Bf' stack' κ n + (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s' ad σ' m ∧ + DelBodyInvA σ.curThread m σ' := by + intro G2 Ac2 Bf2 stack2 sf σf Jf hcf Bd dsk hd + have hct : σf.curThread = specPtr (L.curW sf) := Jf.sr.sch.curThread + by_cases hself : t = L.curW sf + swap + · refine ⟨sf, σf, _, _, _, hd sf (if_neg hself), Or.inl ⟨fun e => hself ?_, rfl⟩, Jf, Bd⟩ + exact specPtr_inj (e.symm.trans hct) + have nsw := Bd.2.2.1 + have sact : SActA σf := by + refine ⟨fun u e => absurd e (nsw u), ?_⟩ + obtain ⟨y, hy, -⟩ := Jf.mcp _ Jf.sr.curT + rw [← hct] at hy + obtain ⟨x, hx, e⟩ := dsk.tcb _ y hy + obtain ⟨x0, hx0, hs0⟩ := sactd.f2 + have ec : σf.curThread = σd.curThread := Bd.2.1.trans Binv.2.1.symm + rw [ec] at hx hy ⊢ + rw [hx0] at hx + cases hx + exact ⟨y, hy, by rw [e]; exact hs0⟩ + obtain ⟨s', σ', hr', -, hres', eσ, K⟩ := Jf.reschedSelf_stepA sact nsw + have hd' := hd s' ((if_pos hself).trans hr') + obtain ⟨-, Ac', sysl', symL', -, userOK', j7'⟩ := hdecFin _ s' ad hd' σ' + have k : DelStepA σf σ' := by + subst eσ + exact + { cur := rfl, time := rfl, last := rfl, csc := rfl, cons := rfl + rel := fun _ h => h + sc := fun y k h => ⟨y, h, Or.inl ⟨rfl, rfl, rfl⟩⟩ + act := Or.inr rfl + ctSt := fun h => by cases h + off := fun h => ⟨h.1, fun u e => by cases e⟩ + ctSc := CtScKA.refl _ σf } + exact ⟨s', σ', _, _, _, hd', Or.inr ⟨hct.trans (by rw [← hself]), by rw [hres']; exact Set.mem_singleton _⟩, + K sysl' symL' userOK' j7', BodyOKA.stepA Bd k⟩ + by_cases hv : capA st sB src = .Frame ptr size rights false mp ep asid va ∧ capA st sB slot = .Thread t + swap + · -- `still_valid` fails: no insert + rw [decide_eq_false hv] at hSV + obtain ⟨s', σ', Ac2, Bf2, stack2, hdecOk, hlast, J2, B2⟩ := tail Gd Ac' Bf' stack' sB σB JB hcurB B1 dskB + (fun s' h => by rw [hdecBase, tcbInstallBuffer_skipA hdest hdel0 hBuf hSV, h]; simp only [bind_ok]) + refine ⟨.Ok (), s', ad, hdecOk, x', stUpdA σ σ.curThread x' .restart, σ', hx', rfl, hset', Gd, Q, R, hw, Ac2, Bf2, + stack2, J2, Or.inl ⟨rfl, fun _ => ?_, B2.1, B2.2.1⟩⟩ + refine setIpcBufG_invokeA _ baddr hspec hyd ?_ hlast + by_cases hS : capA st sB src = .Frame ptr size rights false mp ep asid va + · have hI : capA st sB slot ≠ .Thread t := fun h => hv ⟨hS, h⟩ + have hnT : ¬ fl4TcbSameObject (.threadCap (specPtr t)) cI := by + rcases hkeepIB with h | h | ⟨zt, k, h⟩ + · exact absurd h hI + all_goals + rw [h] at hcI + rw [← CapEqA.eq_abs hcI (fun _ _ _ h => by cases h)] + simp [absCapA, absFrameE, ArchCap.isFrameCap, ArchCap.acapObj, ArchCap.acapRights, ArchCap.acapFsize, archCapIsDevice, fl4TcbSameObject, fl4TcbFrame, sameObjectAs, sameRegionAs, isEpCap, isThreadCap, objRefOf] + exact bufMidInvA mS (by rw [eS hS]; exact hsameF) mI hnT + · have hnE : ¬ fl4TcbSameObject (absCapA fr) cS := by + rcases hkeepSB with h | h | ⟨zt, k, h⟩ + · exact absurd h hS + all_goals + rw [h] at hcS + rw [← CapEqA.eq_abs hcS (fun _ _ _ h => by cases h)] + simp [hfr, absCapA, absFrameE, ArchCap.isFrameCap, ArchCap.acapObj, ArchCap.acapRights, ArchCap.acapFsize, archCapIsDevice, fl4TcbSameObject, fl4TcbFrame, sameObjectAs, sameRegionAs, isEpCap, + isThreadCap, objRefOf] + exact bufMidSrcA mS hnE + rw [decide_eq_true hv] at hSV + obtain ⟨hS, hI⟩ := hv + have hS' : capA st sB (cslotAddrA κsrc) = .Frame ptr size rights false mp ep asid va := by rw [hκsrc]; exact hS + have hP := JB.j.1.place + have hwf : CapWfA fr := by + have h := JB.capWfA (y := cslotAddrA κsrc) hsrcSlot + rw [hS'] at h + obtain ⟨hr1', hr3', -, hsp⟩ := h + exact ⟨hr1', hr3', by simp, hsp⟩ + have hw' : CapRt.EncWf fr := by + have h : CapRt.EncWf (capAtA (L.mem sB) (cslotAddrA κsrc)) := CapRt.decodeF_encWf _ _ + rw [capAtA_eq (st := st), hS'] at h + have hc0 : CapRt.Canonical 0#u64 := Or.inl fun j hj _ => by simp + have h' : CapRt.EncWf (.Frame ptr size rights false mp ep asid va) := h + show CapRt.EncWf (.Frame ptr size rights false false false 0#u64 0#u64) + cases ep <;> (simp only [CapRt.EncWf] at h' ⊢; exact ⟨h'.1, h'.2.1, by simp, hc0⟩) + have hno : ¬ IrqNodeA (L.ksb sB) (fl4Ptr sl.1) := by + change ¬ IrqNodeA (L.ksb sB) (fl4Ptr (specPtr t)) + rw [fl4Ptr_specPtr] + intro h + have := JB.j.1.place.irqK hthT h + cases this + -- the buffer's kernel address: liveness, alignment and the 128-word agreement + obtain ⟨hptrAl, hl⟩ := bufLiveOfFrameA JB.sys.ent.heap JB.sys.sysl.ly.h0 JB.sys.sysl.sys.fr hsrcOS hS hbits hbal hbn + have hw1024 := hl.2.1 + have faB : FrAgA st OS {b | ∃ k : U64, k.val < 128 ∧ b = bufW (bufAddrOfA ptr baddr bits) k} sB σB := + FrAgA.stepA fa (fun b lv => FrameWordLive.keepA hkeepB lv) + (fun b al lv => mem_wr_ne' aM al fun e => + frameWord_ne_ibA Jd.sys.ent.heap Jd.sys.sysl.sys.fr hta htT lv e) rfl + have hagB : ∀ k : U64, k.val < 128 → (L.mem sB (bufW (bufAddrOfA ptr baddr bits) k)).bv = + loadWordAt σB.machineState.underlyingMemory (specPtr (bufW (bufAddrOfA ptr baddr bits) k)) := + fun k hk => faB _ ⟨k, hk, rfl⟩ (bufW_alignedLiveA hw1024 hk) (hl.2.2 k hk) + have hdOS' : cslotAddrA sl ∈ OS := hdOS + have hxOS : cslotAddrA κsrc ∈ OS := by rw [hκsrc]; exact hsrcOS + have hxy : cslotAddrA κsrc ≠ cslotAddrA sl := by rw [hκsrc]; exact hneS + have hsc0 : capA st sB (cslotAddrA κsrc) ≠ .Null := by rw [hS']; simp + have hdecIns : ∀ s2, cdt.insert st sB fr (cslotAddrA κsrc) (cslotAddrA sl) = ok s2 → + L.curW s2 = L.curW sB ∧ + ∀ s', (if t = L.curW s2 then sched.reschedule_required st s2 else ok s2) = ok s' → + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (.Ok (), s', ad) := fun s2 h2 => by + have hc2 : L.curW s2 = L.curW sB := by + have h := cdt.insert.curWA J.sys.ent.os syslB.sys.stz.1.1.mdbS blk' hxOS hdOS' hxy (J.delCanA _ hdOS') + hsc0 hw' + rw [h2, WP.spec_ok] at h + exact h + refine ⟨hc2, fun s' h => ?_⟩ + rw [hκsrc] at h2 + rw [hdecBase, tcbInstallBuffer_liveA hdest hdel0 hBuf hSV h2, h] + simp only [bind_ok] + have hIP : ∀ G', cdt.insert st sB fr (cslotAddrA κsrc) (cslotAddrA sl) ⦃ s2 => SysL st H Z.Cs OS ia ie s2 ∧ + SymL st H Z.Cs s2 ∧ Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s2 ∧ + BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac' s2 ∧ + ∀ σ' m', J7A st (J7R2A st ar hPl.ipa) { Z with G := G' } H OS s2 σ' m' ⦄ := fun G' => by + have hi : layout.tcb.SLOT_BUFFER.val < 7 := by simp [layout.tcb.SLOT_BUFFER] + have hhp := JB.sys.ent.heap + exact JB.insertP1A hA hPl (x := cslotAddrA κsrc) (y := cslotAddrA sl) (nc := fr) hxOS hdOS' hxy + (J.delCanA _ hdOS') hnullB + (Or.inr ⟨Or.inr ⟨ptr, size, rights, false, false, 0#u64, 0#u64, hfr⟩, + ⟨fun _ _ _ _ h => (by rw [hfr] at h; cases h), fun _ _ _ h => (by rw [hfr] at h; cases h)⟩⟩) + (by rw [← hslotIdx]; exact tcb_slot_notEptS hhp htT hi (by simp [layout.tcb.SLOT_BUFFER])) + ⟨slot, hslot, by rw [hI, ← hslotIdx]; exact CovZ.tcbSlot hhp htT hi⟩ hw' (by simp [hfr]) + (by rw [hS']; exact InsC.frame) (fun _ _ h => by rw [hS'] at h; cases h) (by rw [hS']; rfl) G' + have hibw : L.mem sB (ibWordA t) = baddr := by rw [ibWordA_eq]; exact L.mem_wr_same aM + have hspecB : ∀ x, σB.kheap (specPtr t) = some (.tcb x) → x.tcbIpcBuffer = baddr.bv := fun x hx => by + have e : σB.kheap (specPtr t) = some (.tcb { yd with tcbIpcBuffer := baddr.bv }) := by + simp [hσB, bufStateVA, updKSc_kheap, Function.update_apply] + rw [e] at hx + cases hx + rfl + obtain ⟨s2, σ2, hs2, hm, hbk, -, J2⟩ := JStage3A.capInsertTcbBufA (J := JB) κsrc sl (ps := ps) ⟨hss1, hss2⟩ + (t0 := t) hthT hsl (by rw [hS']; simp) hnullB (J.delCanA _ hdOS') + (nc := fr) (anc := absCapA fr) hwf (CapEqA.of_abs rfl) hw' (by simp [hfr]) + (by rw [hS']; exact Fl4ProofsA64.P3.DerivA.frame ptr size rights false mp ep asid va rights false false 0#u64 0#u64) + (by rw [hS']; exact ⟨rfl, rfl, rfl, rfl, fun _ _ _ _ h => by simp [hfr] at h⟩) + (by rw [hS']; trivial) (by rw [hS']; rfl) (by rw [hS']; rfl) (by rw [hS']; exact Or.inl rfl) + (fun r b' rs h _ => by + rw [hS'] at h + rcases h with h | h <;> simp [absCapA, absFrameE] at h) + (fun i h => by rw [hS'] at h; cases h) (fun i h => by simp [hfr] at h) (fun i h => by simp [hfr] at h) + (fun q k j h => by rw [hS'] at h; cases h) (fun h => by rw [hS'] at h; simp [isUtA] at h) (Or.inl hno) + (fun _ => by simp [hfr, CapValidA]) + ⟨fun _ _ h => by simp [hfr, absCapA, absFrameE] at h, fun _ h => by simp [hfr, absCapA, absFrameE] at h, + by simp [NoHiPtV, hfr, absCapA, absFrameE]⟩ + hl hagB ⟨ptr, size, rights, false, 0#u64, 0#u64, bits, rfl, hbits, hptrAl, rfl, hbal⟩ hibw hspecB + (fun s2 h2 => by + have h := hIP Z.G + rw [h2, WP.spec_ok] at h + exact h.2.1) + (fun s2 h2 => by + have h := hIP Z.G + rw [h2, WP.spec_ok] at h + exact h.1) + (fun s2 σ2 h2 _ => by + have h := hIP (insGA Gd (cslotAddrA κsrc) (cslotAddrA sl) + (insBA σB κsrc (absCapA fr) (absCapA (capA st sB (cslotAddrA κsrc))))) + rw [h2, WP.spec_ok] at h + exact h.2.2.2.2 σ2 m) + have wk := (capInsert_skA _ _ _ σB ((), σ2) hm).toDWkA + have B2 := BodyOKA.stepA B1 (delStep_ofKeepA hbk wk.off wk.ctSc) + obtain ⟨hc2, hd2⟩ := hdecIns s2 hs2 + obtain ⟨s', σ', Ac2, Bf2, stack2, hdecOk, hlast, J3, B3⟩ := tail _ _ _ _ s2 σ2 J2 (hc2.trans hcurB) B2 + (PreO.trans (R := DSkA) dskB (capInsert_skA _ _ _ σB ((), σ2) hm)) hd2 + refine ⟨.Ok (), s', ad, hdecOk, x', stUpdA σ σ.curThread x' .restart, σ', hx', rfl, hset', _, Q, R, hw, Ac2, Bf2, + stack2, J3, Or.inl ⟨rfl, fun _ => ?_, B3.1, B3.2.1⟩⟩ + refine setIpcBufG_invokeA _ baddr hspec hyd ?_ hlast + exact bufMidInsA mS (by rw [eS hS]; exact hsameF) mI (by rw [eI hI]; exact hsameT) hm + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3LiftTcbSspA.lean b/proofs/Fl4ProofsA64/P3/Stage3LiftTcbSspA.lean new file mode 100644 index 000000000..27f5cc85a --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3LiftTcbSspA.lean @@ -0,0 +1,251 @@ +/- +P3 (aarch64, A64-P3 Lift 3b labels): **TCB SetSchedParams with a null fault handler and an unchanged SC, at the record**. + +`decode_set_sched_params` with the SC unchanged (`target.sc = sc`: flags `FAULT | MCP | PRIO`, no SC step) and a null +fault handler in extra slot 2, on a thread other than the caller: `restart_caller`, the deletion of TCB slot 3 +(`restartDelA`), no insert (`new_cap.is_null`), `set_mcp`, `set_priority`. + +* **`sspNull_invokeA`** / **`sspNull_invokeErrA`**: the spec's `fl4InvokeTcb (.threadControlSched ..)` from the + `fl4CapDelete` outcome of slot 3, `set_mcpriority` and `set_priority`. +* **`JSyscallA.setSchedParamsNullA`**: the arm. The record after the deletion is the deletion loop's + (`DelXA6.toStage3A`); `set_mcp` is `setMcp_stepA`, its post state's P1 facts are `JStage3A.p1StepA` (a `DMD` step, + as P1's `tcs_tailQ`); `set_priority` is `JStage3A.prioAnyA` at the post-`set_mcp` state (the deletion may have + cancelled the target's IPC), with `SActA` from the deletion's record (`delW_prA`); the final P1 facts are + `JSyscallA.decodeFinA`. + +Not covered (left outside `TcbScopeA.setSchedParamsNull`): a non-null (endpoint) fault handler (the `cdt::insert` +after the deletion needs the P1 facts at the post-insert state, an intermediate state); `sc ≠ target.sc` (the SC +`bind_tcb` / `unbind_tcb` steps, a separate line); the target being the caller; the `Err` returns before the +deletion. + +Named premises: those of `JSyscallA.restartDelA`, [P] `ArchTotal`, `MaxSpi`, `ArchPlat`, `A64FrameMasks`, `hmb`, +`hmax`, `htu`; [S] `h1`. New module. +-/ +import Fl4ProofsA64.P3.Stage3LiftTcbTimeoutA +import Fl4ProofsA64.P3.Stage3TcbPrioAnyA +import Fl4ProofsA64.P3.Stage3P1StepA +import Fl4ProofsA64.P3.TcbInvokeSchedControlA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Vcpu Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + +theorem sspNull_invokeA {σ1 σd σ3 : Fl4SpecA64.State Unit} {t auth : U64} (κs κsrc : CslotPtr) (mp pr : Priority) + {y : Tcb} + (hdel : (.ok (), σd) ∈ ((fl4CapDelete (Z := Unit) (specPtr t, tcbCnodeIndex 3)).run σ1).1) + (hy : σd.kheap (specPtr t) = some (.tcb y)) + (hSet : setPriority (Z := Unit) (specPtr t) pr (mcpStateA σd (specPtr t) y mp) = ({((), σ3)}, False)) : + (.ok [], σ3) ∈ ((fl4InvokeTcb (.threadControlSched (specPtr t) κs (some (.nullCap, κsrc)) + (some (mp, specPtr auth)) (some (pr, specPtr auth)) none)).run σ1).1 := by + have hset : setMcpriority (Z := Unit) (specPtr t) mp σd = ({((), mcpStateA σd (specPtr t) y mp)}, False) := + threadSet_eqSc _ hy + simp only [fl4InvokeTcb] + refine mem_bindE_okD (a := ()) (s' := σd) ?_ ?_ + · simp only [fl4InstallTcbCap] + refine mem_bindE_okD (a := ()) (s' := σd) hdel ?_ + simp [Fl4Spec.NonDetE.unlessE, run_returnOkD] + · simp only [Fl4Spec.NonDet.maybeM] + rw [run_bind_okD (run_liftE_stD hset), run_bind_okD (run_liftE_stD hSet), + run_bind_okD (run_liftE_stD (m := Fl4Spec.NonDet.pure ()) rfl)] + simp [run_returnOkD] + +theorem sspNull_invokeErrA {σ1 σd : Fl4SpecA64.State Unit} {t auth : U64} (κs κsrc : CslotPtr) (mp pr : Priority) + (hdel : (.error (), σd) ∈ ((fl4CapDelete (Z := Unit) (specPtr t, tcbCnodeIndex 3)).run σ1).1) : + (.error (), σd) ∈ ((fl4InvokeTcb (.threadControlSched (specPtr t) κs (some (.nullCap, κsrc)) + (some (mp, specPtr auth)) (some (pr, specPtr auth)) none)).run σ1).1 := by + simp only [fl4InvokeTcb, fl4InstallTcbCap] + exact mem_bindE_errD (mem_bindE_errD hdel) + +section +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {Clock : A → Fl4SpecA64.MachineState → Prop} + +set_option maxHeartbeats 8000000 in +/-- **TCB SetSchedParams with a null fault handler and an unchanged SC at a syscall entry** (module docstring). -/ +theorem JSyscallA.setSchedParamsNullA [PlainStore st] + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (s : S) (a : A) (σ : Fl4SpecA64.State Unit) (m : Fl4SpecA64.McState) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) (hCnt : CounterStepA σ) + (P : DelPlatA st ar AR Clock) (hCl : Clock a σ.machineState) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {slot lbl len buf t authSlot scSlot fhSlot mcp prio auth v : U64} {blk call don : Bool} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Thread t) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hlbl : lbl = fl4_abi.label.TCBSetSchedParams) + (he0 : tcb.extra_with_length ⟨len, buf, call, slot, extra⟩ 0#usize 2#u64 = ok (.Ok authSlot)) + (he1 : tcb.extra_with_length ⟨len, buf, call, slot, extra⟩ 1#usize 2#u64 = ok (.Ok scSlot)) + (he2 : tcb.extra_with_length ⟨len, buf, call, slot, extra⟩ 2#usize 2#u64 = ok (.Ok fhSlot)) + (hmcp : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 0#u64 = ok mcp) + (hprio : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 1#u64 = ok prio) + (harg : authSlot ∈ OS) (hac : capA st s authSlot = .Thread auth) + (hle1 : ¬ L.mem s (mcpA auth) < mcp) (hle2 : ¬ L.mem s (mcpA auth) < prio) + (hsc : tcb.sched_params_sc st s t scSlot = ok (.Ok v)) (hold : obj.Tcb.sc st t s = ok v) + (hfh : capA st s fhSlot = .Null) (hne : t ≠ L.curW s) + {κs : CslotPtr} (hAκ : AbsSlotA Z.th κs) (hκ : cslotAddrA κs = slot) (κsrc : CslotPtr) : + ∃ r s3 a3, syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (r, s3, a3) ∧ + TcbDelOutA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n m ar hPl.ipa s a σ + (fun κs' => .threadControlSched (specPtr t) κs' (some (.nullCap, κsrc)) + (some (BitVec.ofNat 8 mcp.val, specPtr auth)) (some (BitVec.ofNat 8 prio.val, specPtr auth)) none) + r s3 a3 := by + have ht : t ∈ Z.T := J.tcbs ▸ (J.toJStage3A.slotCapTy hslot).1.2.2.2.2.2 t hcap + have htT : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + have hthT : Z.th t = some .tcb := (J.j.1.thT t).2 ht + have haT : auth ∈ Z.T := J.tcbs ▸ (J.toJStage3A.slotCapTy harg).1.2.2.2.2.2 auth hac + have T0 := J.toJStage3A.toTA J.sact J.noSwitch + have hstall : smp.remote_tcb_stall st ar s a t = ok (s, a) := T0.stall_singleA hCs h1 ht + -- the widths of the new MCP and priority + have hw : ∀ q, ¬ L.mem s (mcpA auth) < q → q.val < 256 := fun q hle => by + obtain ⟨x, -, hw⟩ := J.mcp auth haT + have hv : q.val ≤ (L.mem s (mcpA auth)).val := Nat.le_of_not_lt hle + rw [hw.1] at hv + exact Nat.lt_of_le_of_lt hv x.tcbMcpriority.isLt + have hck : ∀ q, ¬ L.mem s (mcpA auth) < q → tcb.check_prio st s q auth = ok (.Ok ()) := fun q hle => by + simp only [tcb.check_prio, Fl4ProofsA64.P3.Sched.obj.Tcb.mcp.specA, bind_ok, gt_iff_lt, hle, ite_false] + have hAuth : tcb.thread_arg st s authSlot 1#u64 = ok (.Ok auth) := by + unfold tcb.thread_arg + rw [capA_eq, bind_ok, hac] + -- the target is no core's idle thread (the invoked slot names it) + have hidles : ∀ c < n, m.idleThreadOf c ≠ specPtr t := by + obtain ⟨cI, hgI, hcI⟩ := getCap_eqA J.j.1.st hAκ + rw [capAtA_eq (st := st), hκ, hcap] at hcI + have eI : cI = .threadCap (specPtr t) := (CapEqA.eq_abs hcI (fun _ _ _ h => by cases h)).symm + have hcapS : specCapAtA σ κs = some (.threadCap (specPtr t)) := by + rw [← eI]; exact (getCap_outA (c := cI) (σ' := σ) (by rw [hgI]; exact rfl)).2 + exact fun c hc e => (J.sidle.f6 κs).1 c hc (e ▸ hcapS) + set sl : Fl4SpecA64.CslotPtr := (specPtr t, tcbCnodeIndex 3) with hsl + have hAd : AbsSlotA Z.th sl := ⟨t, rfl, Or.inr ⟨hthT, 3, by decide, rfl⟩⟩ + have hslotIdx : slotIdx t layout.tcb.SLOT_FAULT_HANDLER = cslotAddrA sl := by + have h3 : layout.tcb.SLOT_FAULT_HANDLER.val = 3 := by simp [layout.tcb.SLOT_FAULT_HANDLER] + rw [slotIdx_addrA, h3] + unfold cslotAddrA + show _ = slotAddrA (specPtr t) (Fl4SpecA64.ofBl (tcbCnodeIndex 3)) + rw [ofBl_tcbIdx 3 (by decide)] + have hdest : obj.Tcb.slot t layout.tcb.SLOT_FAULT_HANDLER = ok (cslotAddrA sl) := by + rw [← hslotIdx]; exact tcb_slot_val _ _ + obtain ⟨xr, heq, hset, D1, hdelWP⟩ := J.restartDelA s a σ m hPl hCnt hslot hcov hext P hCl hCs sl hAd + obtain ⟨x', hx', -, hset', -⟩ := J.restartMachA hslot hcov hext + have e1 : stUpdA σ σ.curThread xr .restart = stUpdA σ σ.curThread x' .restart := by + have := Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst (hset.symm.trans hset')) + exact (Prod.mk.inj this).2 + have hr : tcb.restart_caller st s = ok (L.wr s (stA (L.curW s)) sched.TS_RESTART) := by + unfold tcb.restart_caller + rw [cur_thread_ok, bind_ok, heq] + obtain ⟨⟨r, sd, ad⟩, hdel0, σd, Gd, Dd, hres, hW⟩ := spec_existsA hdelWP + have sysd := J.sysDelSlotA hA hPl hslot hcov hext hcap layout.tcb.SLOT_FAULT_HANDLER + (by simp [layout.tcb.SLOT_FAULT_HANDLER]) hstall hr (r := r) (sd := sd) (ad := ad) + (by rw [hslotIdx]; exact hdel0) + have hFault : obj.Slot.cap st fhSlot s = ok .Null := by rw [capA_eq, hfh] + have hdecBase : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + tcb.thread_control_sched st ar (L.wr s (stA (L.curW s)) sched.TS_RESTART) a t slot (.Null, fhSlot) mcp prio v + 14#u64 := by + rw [hcap, decodeTcb_programA, if_neg Bool.false_ne_true, hlbl, + tcbSetSchedParams_controlA hstall he0 he1 he2 hmcp hprio hAuth (hck _ hle1) (hck _ hle2) hsc hold hFault rfl hr, + if_pos rfl] + have hfl : ((14#u64 &&& tcb.SCHED_UPDATE_FAULT) != 0#u64) = true := by rw [tcb.SCHED_UPDATE_FAULT]; decide + rcases hres with ⟨hr1, hnull, hspec, -⟩ | ⟨hr1, hspec⟩ + swap + · -- preempted deletion + subst hr1 + obtain ⟨⟨⟨-, X, sv⟩, -⟩, hpre⟩ := hW + obtain ⟨Q, R, hw', Ac', Bf', stack', Jd⟩ := X.toStage3A sysd.1 sysd.2 sv + have hdecP : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (.Err .Preempted, sd, ad) := by + rw [hdecBase] + simp only [tcb.thread_control_sched, lift, bind_ok, hfl, if_true, tcb.install_tcb_cap, hdest, hdel0, + uncurry_apply_pair, core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame.from, + bind_tc_ok] + refine ⟨.Err .Preempted, sd, ad, hdecP, x', stUpdA σ σ.curThread x' .restart, σd, hx', rfl, hset', Gd, Q, R, hw', + Ac', Bf', stack', Jd, Or.inr ⟨rfl, fun κs' => ?_, hpre.1, hpre.2.1⟩⟩ + rw [e1] at hspec + exact sspNull_invokeErrA κs' κsrc _ _ hspec + subst hr1 + obtain ⟨hWT, Binv, -⟩ := hW + have Pr := (delW_prA hWT).1 + obtain ⟨⟨-, X, sv⟩, -⟩ := hWT + obtain ⟨Q, R, hw', Ac', Bf', stack', Jd⟩ := X.toStage3A sysd.1 sysd.2 sv + rw [e1] at hspec + have hInst : tcb.install_tcb_cap st ar (L.wr s (stA (L.curW s)) sched.TS_RESTART) a t slot + layout.tcb.SLOT_FAULT_HANDLER .Null fhSlot = ok (.Ok (), sd, ad) := by + simp only [tcb.install_tcb_cap, hdest, hdel0, bind_ok, uncurry_apply_pair, + core.result.Result.Insts.CoreOpsTry.branch, cap_is_null_nullA, if_true] + -- `set_mcp` + obtain ⟨yd, hyd, -⟩ := Jd.mcp t ht + have hwm := hw mcp hle1 + have hwp := hw prio hle2 + set sm := L.wr sd (mcpA t) mcp with hsm + set σm := mcpStateA σd (specPtr t) yd (BitVec.ofNat 8 mcp.val) with hσm + have hMcp : obj.Tcb.set_mcp st t sd mcp = ok sm := eq_of_spec_fd (Fl4ProofsA64.P1.obj.Tcb.set_mcp.wr sd t mcp) + obtain ⟨blkM, KM⟩ := Jd.setMcp_stepA (st := st) ht hwm hyd + have hta : TcbAddr t := Jd.sr.core.tcbs t ht + have hbd : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac' sd := BXI.blk Jd.sys.ent.bxi + have h0 : H 0#u64 = none := sysd.1.ly.h0 + have dM : DMD st H Z.Cs 0#u64 sd sm := by + have ha := field_aligned (K := 3440#u64) hta (by simp) (by simp) + exact DMD.ofNone hbd h0 sysd.2 sysd.1.vr ((writesIn_wr ha).mono fun b e => by + subst e + exact Or.inl (Or.inl ⟨t, htT, 3440#u64, by simp, by simp, by simp, ⟨by simp, by simp, by simp, by simp⟩, + rfl⟩)) + ((LvF.wrT (LvC.of_blk hbd) htT (K0 := 3440#u64) (v := mcp) (by simp) (by simp) (j := 3440) (by simp)).junk + (fun _ _ e => by obtain ⟨-, rfl⟩ := e; unfold LvO; omega) (fun _ _ e => False.elim e) + (fun _ _ e => False.elim e) (fun _ _ e => False.elim e)) + have wM : WritesIn st sd sm (SchedStepNWA (st := st) N E Rp _ m σd sd) := + (tcbSetMCPriority_partA (PartFrA.refl N E Rp _ m sd σd) Jd.iplaced.e.tcb ht mcp + (BitVec.ofNat 8 mcp.val) hyd).wr + obtain ⟨syslM, symLM, userM, -, j7M⟩ := Jd.p1StepA hA hPl wM dM (fun k hk => by rw [h0] at hk; cases hk) blkM + have Jm := KM syslM symLM userM (j7M σm m) + -- the scheduler facts at the post-`set_mcp` state + have hcd : σd.curThread = σ.curThread := Binv.2.1 + have htcS : specPtr t ≠ σ.curThread := fun e => hne (specPtr_inj (e.trans J.sr.sch.curThread)) + have htcD : σd.curThread ≠ specPtr t := fun e => htcS (e.symm.trans hcd) + have khm : ∀ q, q ≠ specPtr t → σm.kheap q = σd.kheap q := fun q hq => by + simp only [hσm, mcpStateA, updKSc_kheap, Function.update_of_ne hq] + have hnsM : ∀ u, σm.schedulerAction ≠ .switchThread u := fun u => Binv.2.2.1 u + have sactM : SActA σm := + ⟨fun u hu => Pr.2.1.f1 u hu, by + obtain ⟨x, hx, hs⟩ := Pr.2.1.f2 + exact ⟨x, by rw [show σm.curThread = σd.curThread from rfl, khm _ htcD]; exact hx, hs⟩⟩ + have htcM : specPtr t ≠ σm.curThread := fun e => htcD e.symm + -- `set_priority` + obtain ⟨s3, σ3, hsp3, hSet3, DS3, blk3, J3⟩ := Jm.prioAnyA (st := st) (ar := ar) hCs h1 hmb hmax htu hnsM sactM ht hwp + htcM hidles + have hdecOk : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (.Ok (), s3, ad) := by + rw [hdecBase] + exact tcbSetSchedParams_effectsA false hInst hMcp hsp3 + obtain ⟨-, -, sysl, symL, -, userOK, j7⟩ := J.decodeFinA hA hM hPl hfm hslot hcov + (rfl : capA st s slot = capA st s slot) hext hSC (lbl := lbl) + (by rw [hcap, hlbl]; simp [DecArmS1aEnt, DecArmQH, DecArmQZ2, DecArmKQ2, DecArmKQ, DecArmKQ3, DecArmDelQ, + DecArmS1aC, capFam]) + (fun _ h => by rw [hcap] at h; cases h) (fun _ h => by rw [hcap] at h; cases h) hdecOk σ3 m + obtain ⟨Q3, R3, J3'⟩ := J3 sysl symL userOK j7 + have B2 := BodyOKA.stepA (BodyOKA.stepA Binv (delStep_ofKeepA + (BodyKeepA.ofTcbUpd (y' := { yd with tcbMcpriority := BitVec.ofNat 8 mcp.val }) hyd rfl) + (CtOffA.keep rfl (Or.inl rfl)) (CtScKA.ofUpd rfl (fun _ _ c => absurd c.symm htcM)))) DS3 + refine ⟨.Ok (), s3, ad, hdecOk, x', stUpdA σ σ.curThread x' .restart, σ3, hx', rfl, hset', Gd, Q3, R3, hw', Ac', Bf', + stack', J3', Or.inl ⟨rfl, fun κs' => ?_, B2.1, B2.2.1⟩⟩ + exact sspNull_invokeA κs' κsrc _ _ hspec hyd hSet3 + +end + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3LiftTcbSspEpA.lean b/proofs/Fl4ProofsA64/P3/Stage3LiftTcbSspEpA.lean new file mode 100644 index 000000000..338bbe399 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3LiftTcbSspEpA.lean @@ -0,0 +1,484 @@ +/- +P3 (aarch64, A64-P3 Lift 3b labels): **TCB SetSchedParams with an endpoint fault handler and an unchanged SC, at the +record**. + +`decode_set_sched_params` with the SC unchanged (flags `FAULT | MCP | PRIO`) and a valid endpoint fault handler in +extra slot 2, on a thread other than the caller: `restart_caller`, the deletion of TCB slot 3 (`restartDelA`), the +`still_valid` check and `cdt::insert` of the handler into slot 3, `set_mcp`, `set_priority`. + +* **`JStage3A.sspTailA`**: `set_mcp` then `set_priority` of a non-current, non-idle thread from any record state (the + tail of `JSyscallA.setSchedParamsNullA`, factored): `setMcp_stepA`, its post state's P1 facts by + `JStage3A.p1StepA`, `JStage3A.prioAnyA`. +* **`sspEp_invokeA`** / **`sspEp_invokeErrA`**: the spec's `fl4InvokeTcb (.threadControlSched ..)` from the outcome of + `fl4InstallTcbCap` of slot 3, `set_mcpriority` and `set_priority`. +* **`JSyscallA.setSchedParamsEpA`**: the arm. After the deletion, the insert (when `still_valid` holds) is + `JStage3A.capInsertTcbSlotA` with the P1 facts at the post-insert state (an intermediate state) from + `JStage3A.insertP1A`; otherwise the install is a no-op. The tail is `sspTailA`; the final P1 facts are + `JSyscallA.decodeFinA`. + +Not covered: `sc ≠ target.sc`; the target being the caller; the `Err` returns before the deletion. + +Named premises: those of `JSyscallA.setSchedParamsNullA`. New module. +-/ +import Fl4ProofsA64.P3.Stage3LiftTcbSspA +import Fl4ProofsA64.P3.Stage3InsP1A +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Vcpu Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +theorem tcbExtra2_memA {c : tcb.Ctx} {n : U64} {src : U64} + (he : tcb.extra_with_length c 2#usize n = ok (.Ok src)) : src ∈ c.extra.val ∧ src ≠ 0#u64 := by + unfold tcb.extra_with_length at he + obtain ⟨o, ho, he⟩ := bind_eq_ok he + cases o with + | none => + exfalso + simp only [error.truncated] at he + cases okinj he + | some x => + simp only at he + have ex : x = src := by + split at he + · exact core.result.Result.Ok.inj (okinj he) + · exfalso + simp only [error.truncated] at he + cases okinj he + subst ex + unfold tcb.extra at ho + split at ho + · cases okinj ho + obtain ⟨y, hy, ho⟩ := bind_eq_ok ho + obtain ⟨b, hb, ho⟩ := bind_eq_ok ho + have hlen : c.extra.val.length = 3 := by simp + have hm : y ∈ c.extra.val := by + obtain ⟨v, hv, e⟩ := spec_existsA (Aeneas.Std.Array.index_usize_spec c.extra 2#usize (by simp [hlen])) + have ev : v = y := okinj (hv.symm.trans hy) + rw [← ev, e] + exact List.getElem_mem _ + unfold obj.Slot.is_null at hb + have hb' : b = decide (y = 0#u64) := (okinj hb).symm + by_cases h0 : y = 0#u64 + · rw [hb', decide_eq_true h0, if_pos rfl] at ho + cases okinj ho + · rw [hb', decide_eq_false h0] at ho + simp only [Bool.false_eq_true, ↓reduceIte] at ho + have e := Option.some.inj (okinj ho) + exact e ▸ ⟨hm, h0⟩ + + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {Clock : A → Fl4SpecA64.MachineState → Prop} + +set_option maxHeartbeats 8000000 in +/-- **`set_mcp`, then `set_priority`, of a non-current thread from any record state** (module docstring). -/ +theorem JStage3A.sspTailA {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (hns : ∀ u, σ.schedulerAction ≠ .switchThread u) (sact : SActA σ) + {t mcp prio : U64} (ht : t ∈ Z.T) (hwm : mcp.val < 256) (hwp : prio.val < 256) + (htc : specPtr t ≠ σ.curThread) (hidles : ∀ c < n, m.idleThreadOf c ≠ specPtr t) : + ∃ sm s3 σ3 yd, obj.Tcb.set_mcp st t s mcp = ok sm ∧ sched.set_priority st sm t prio = ok s3 ∧ + σ.kheap (specPtr t) = some (.tcb yd) ∧ + setPriority (Z := Unit) (specPtr t) (BitVec.ofNat 8 prio.val) + (mcpStateA σ (specPtr t) yd (BitVec.ofNat 8 mcp.val)) = ({((), σ3)}, False) ∧ + Sched.BodyKeepA σ (mcpStateA σ (specPtr t) yd (BitVec.ofNat 8 mcp.val)) ∧ + DelStepA (mcpStateA σ (specPtr t) yd (BitVec.ofNat 8 mcp.val)) σ3 ∧ + (SysL st H Z.Cs OS ia ie s3 → SymL st H Z.Cs s3 → Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s3 → + J7A st (J7R2A st ar hPl.ipa) Z H OS s3 σ3 m → + ∃ Q Rl, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s3 a σ3 m) := by + have htT : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + obtain ⟨yd, hyd, -⟩ := J.mcp t ht + set sm := L.wr s (mcpA t) mcp with hsm + set σm := mcpStateA σ (specPtr t) yd (BitVec.ofNat 8 mcp.val) with hσm + have hMcp : obj.Tcb.set_mcp st t s mcp = ok sm := eq_of_spec_fd (Fl4ProofsA64.P1.obj.Tcb.set_mcp.wr s t mcp) + obtain ⟨blkM, KM⟩ := J.setMcp_stepA (st := st) ht hwm hyd + have hta : TcbAddr t := J.sr.core.tcbs t ht + have hbd : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s := BXI.blk J.sys.ent.bxi + have h0 : H 0#u64 = none := J.sys.sysl.ly.h0 + have dM : DMD st H Z.Cs 0#u64 s sm := by + have ha := field_aligned (K := 3440#u64) hta (by simp) (by simp) + exact DMD.ofNone hbd h0 J.symL J.sys.sysl.vr ((writesIn_wr ha).mono fun b e => by + subst e + exact Or.inl (Or.inl ⟨t, htT, 3440#u64, by simp, by simp, by simp, ⟨by simp, by simp, by simp, by simp⟩, + rfl⟩)) + ((LvF.wrT (LvC.of_blk hbd) htT (K0 := 3440#u64) (v := mcp) (by simp) (by simp) (j := 3440) (by simp)).junk + (fun _ _ e => by obtain ⟨-, rfl⟩ := e; unfold LvO; omega) (fun _ _ e => False.elim e) + (fun _ _ e => False.elim e) (fun _ _ e => False.elim e)) + have wM : WritesIn st s sm (SchedStepNWA (st := st) N E Rp _ m σ s) := + (tcbSetMCPriority_partA (PartFrA.refl N E Rp _ m s σ) J.iplaced.e.tcb ht mcp + (BitVec.ofNat 8 mcp.val) hyd).wr + obtain ⟨syslM, symLM, userM, -, j7M⟩ := J.p1StepA hA hPl wM dM (fun k hk => by rw [h0] at hk; cases hk) blkM + have Jm := KM syslM symLM userM (j7M σm m) + have khm : ∀ q, q ≠ specPtr t → σm.kheap q = σ.kheap q := fun q hq => by + simp only [hσm, mcpStateA, updKSc_kheap, Function.update_of_ne hq] + have hnsM : ∀ u, σm.schedulerAction ≠ .switchThread u := fun u => hns u + have sactM : SActA σm := + ⟨fun u hu => sact.f1 u hu, by + obtain ⟨x, hx, hs⟩ := sact.f2 + exact ⟨x, by rw [show σm.curThread = σ.curThread from rfl, khm _ (fun e => htc e.symm)]; exact hx, hs⟩⟩ + have htcM : specPtr t ≠ σm.curThread := htc + obtain ⟨s3, σ3, hsp3, hSet3, DS3, blk3, J3⟩ := Jm.prioAnyA (st := st) (ar := ar) hCs h1 hmb hmax htu hnsM sactM ht hwp + htcM hidles + exact ⟨sm, s3, σ3, yd, hMcp, hsp3, hyd, hSet3, BodyKeepA.ofTcbUpd (y' := { yd with tcbMcpriority := BitVec.ofNat 8 mcp.val }) hyd rfl, DS3, J3⟩ + + +theorem sactKeepA {σd σ2 : Fl4SpecA64.State Unit} (S0 : SActA σd) (hns : ∀ u, σd.schedulerAction ≠ .switchThread u) + (hk : Sched.BodyKeepA σd σ2) (hc : ∃ x', σ2.kheap σ2.curThread = some (.tcb x')) : SActA σ2 := by + refine ⟨fun u hu => absurd (hk.act ▸ hu) (hns u), ?_⟩ + obtain ⟨x, hx, hs⟩ := S0.f2 + obtain ⟨x', hx'⟩ := hc + obtain ⟨x0, hx0, he⟩ := hk.tcb _ _ hx' + rw [hk.cur] at hx0 + rw [hx] at hx0 + cases hx0 + exact ⟨x', hx', by rw [he]; exact hs⟩ + +/-- **The spec's `invoke_tcb` of SetSchedParams** (endpoint handler installed), from the install's outcome. -/ +theorem sspEp_invokeA {σ1 σi σ3 : Fl4SpecA64.State Unit} {t auth : U64} (κs κsrc : CslotPtr) (nc : Cap) + (mp pr : Priority) {y : Tcb} + (hinst : (.ok (), σi) ∈ ((fl4InstallTcbCap (Z := Unit) (specPtr t) κs 3 (some (nc, κsrc))).run σ1).1) + (hy : σi.kheap (specPtr t) = some (.tcb y)) + (hSet : setPriority (Z := Unit) (specPtr t) pr (mcpStateA σi (specPtr t) y mp) = ({((), σ3)}, False)) : + (.ok [], σ3) ∈ ((fl4InvokeTcb (.threadControlSched (specPtr t) κs (some (nc, κsrc)) + (some (mp, specPtr auth)) (some (pr, specPtr auth)) none)).run σ1).1 := by + have hset : setMcpriority (Z := Unit) (specPtr t) mp σi = ({((), mcpStateA σi (specPtr t) y mp)}, False) := + threadSet_eqSc _ hy + simp only [fl4InvokeTcb] + refine mem_bindE_okD (a := ()) (s' := σi) hinst ?_ + simp only [Fl4Spec.NonDet.maybeM] + rw [run_bind_okD (run_liftE_stD hset), run_bind_okD (run_liftE_stD hSet), + run_bind_okD (run_liftE_stD (m := Fl4Spec.NonDet.pure ()) rfl)] + simp [run_returnOkD] + +theorem sspEp_invokeErrA {σ1 σd : Fl4SpecA64.State Unit} {t auth : U64} (κs κsrc : CslotPtr) (nc : Cap) + (mp pr : Priority) + (hdel : (.error (), σd) ∈ ((fl4CapDelete (Z := Unit) (specPtr t, tcbCnodeIndex 3)).run σ1).1) : + (.error (), σd) ∈ ((fl4InvokeTcb (.threadControlSched (specPtr t) κs (some (nc, κsrc)) + (some (mp, specPtr auth)) (some (pr, specPtr auth)) none)).run σ1).1 := by + simp only [fl4InvokeTcb, fl4InstallTcbCap] + exact mem_bindE_errD (mem_bindE_errD hdel) + +set_option maxHeartbeats 16000000 in +/-- **TCB SetSchedParams with an endpoint fault handler and an unchanged SC at a syscall entry** (module docstring). -/ +theorem JSyscallA.setSchedParamsEpA [PlainStore st] + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (s : S) (a : A) (σ : Fl4SpecA64.State Unit) (m : Fl4SpecA64.McState) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) (hCnt : CounterStepA σ) + (P : DelPlatA st ar AR Clock) (hCl : Clock a σ.machineState) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {slot lbl len buf t authSlot scSlot fhSlot mcp prio auth v : U64} {blk call don : Bool} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Thread t) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hlbl : lbl = fl4_abi.label.TCBSetSchedParams) + (he0 : tcb.extra_with_length ⟨len, buf, call, slot, extra⟩ 0#usize 2#u64 = ok (.Ok authSlot)) + (he1 : tcb.extra_with_length ⟨len, buf, call, slot, extra⟩ 1#usize 2#u64 = ok (.Ok scSlot)) + (he2 : tcb.extra_with_length ⟨len, buf, call, slot, extra⟩ 2#usize 2#u64 = ok (.Ok fhSlot)) + (hmcp : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 0#u64 = ok mcp) + (hprio : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 1#u64 = ok prio) + (harg : authSlot ∈ OS) (hac : capA st s authSlot = .Thread auth) + (hle1 : ¬ L.mem s (mcpA auth) < mcp) (hle2 : ¬ L.mem s (mcpA auth) < prio) + (hsc : tcb.sched_params_sc st s t scSlot = ok (.Ok v)) (hold : obj.Tcb.sc st t s = ok v) + (hnn : capA st s fhSlot ≠ .Null) (hValid : faults.valid_fault_handler (capA st s fhSlot) = ok true) + (hne : t ≠ L.curW s) + {κs κsrc : CslotPtr} (hAκ : AbsSlotA Z.th κs) (hκ : cslotAddrA κs = slot) + (hAsrc : AbsSlotA Z.th κsrc) (hκsrc : cslotAddrA κsrc = fhSlot) : + ∃ r s3 a3, syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (r, s3, a3) ∧ + TcbDelOutA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n m ar hPl.ipa s a σ + (fun _ => .threadControlSched (specPtr t) κs (some (absCapA (capA st s fhSlot), κsrc)) + (some (BitVec.ofNat 8 mcp.val, specPtr auth)) (some (BitVec.ofNat 8 prio.val, specPtr auth)) none) + r s3 a3 := by + obtain ⟨p, b, sn, rv, g, gr, hnc⟩ : ∃ p b sn rv g gr, capA st s fhSlot = .Endpoint p b sn rv g gr := by + generalize capA st s fhSlot = c at hnn hValid + cases c with + | Null => exact absurd rfl hnn + | Endpoint p b sn rv g gr => exact ⟨p, b, sn, rv, g, gr, rfl⟩ + | _ => exact absurd (okinj hValid) Bool.false_ne_true + have ht : t ∈ Z.T := J.tcbs ▸ (J.toJStage3A.slotCapTy hslot).1.2.2.2.2.2 t hcap + have htT : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + have hthT : Z.th t = some .tcb := (J.j.1.thT t).2 ht + have haT : auth ∈ Z.T := J.tcbs ▸ (J.toJStage3A.slotCapTy harg).1.2.2.2.2.2 auth hac + have T0 := J.toJStage3A.toTA J.sact J.noSwitch + have hstall : smp.remote_tcb_stall st ar s a t = ok (s, a) := T0.stall_singleA hCs h1 ht + have hw : ∀ q, ¬ L.mem s (mcpA auth) < q → q.val < 256 := fun q hle => by + obtain ⟨x, -, hw⟩ := J.mcp auth haT + have hv : q.val ≤ (L.mem s (mcpA auth)).val := Nat.le_of_not_lt hle + rw [hw.1] at hv + exact Nat.lt_of_le_of_lt hv x.tcbMcpriority.isLt + have hck : ∀ q, ¬ L.mem s (mcpA auth) < q → tcb.check_prio st s q auth = ok (.Ok ()) := fun q hle => by + simp only [tcb.check_prio, Fl4ProofsA64.P3.Sched.obj.Tcb.mcp.specA, bind_ok, gt_iff_lt, hle, ite_false] + have hAuth : tcb.thread_arg st s authSlot 1#u64 = ok (.Ok auth) := by + unfold tcb.thread_arg + rw [capA_eq, bind_ok, hac] + have hidles : ∀ c < n, m.idleThreadOf c ≠ specPtr t := by + obtain ⟨cI, hgI, hcI⟩ := getCap_eqA J.j.1.st hAκ + rw [capAtA_eq (st := st), hκ, hcap] at hcI + have eI : cI = .threadCap (specPtr t) := (CapEqA.eq_abs hcI (fun _ _ _ h => by cases h)).symm + have hcapS : specCapAtA σ κs = some (.threadCap (specPtr t)) := by + rw [← eI]; exact (getCap_outA (c := cI) (σ' := σ) (by rw [hgI]; exact rfl)).2 + exact fun c hc e => (J.sidle.f6 κs).1 c hc (e ▸ hcapS) + obtain ⟨hsrcM, hsrc0⟩ := tcbExtra2_memA he2 + have hsrcOS : fhSlot ∈ OS := hext _ hsrcM hsrc0 + obtain ⟨ss', ps, hss1, hss2, hss3, -⟩ := J.toJStage3A.cnSlot_of_covA (hSC _ hsrcM hsrc0) + have ess : κsrc = ss' := + cslotAddrA_inj J.j.1.place hAsrc ⟨ps, hss1, Or.inl hss2⟩ (hκsrc.trans hss3.symm) + subst ess + obtain ⟨si', pi, hsi1, hsi2, hsi3, -⟩ := J.toJStage3A.cnSlot_of_covA hcov + have esi : κs = si' := + cslotAddrA_inj J.j.1.place hAκ ⟨pi, hsi1, Or.inl hsi2⟩ (hκ.trans hsi3.symm) + subst esi + set sl : Fl4SpecA64.CslotPtr := (specPtr t, tcbCnodeIndex 3) with hsl + have hAd : AbsSlotA Z.th sl := ⟨t, rfl, Or.inr ⟨hthT, 3, by decide, rfl⟩⟩ + have hslotIdx : slotIdx t layout.tcb.SLOT_FAULT_HANDLER = cslotAddrA sl := by + have h3 : layout.tcb.SLOT_FAULT_HANDLER.val = 3 := by simp [layout.tcb.SLOT_FAULT_HANDLER] + rw [slotIdx_addrA, h3] + unfold cslotAddrA + show _ = slotAddrA (specPtr t) (Fl4SpecA64.ofBl (tcbCnodeIndex 3)) + rw [ofBl_tcbIdx 3 (by decide)] + have hdest : obj.Tcb.slot t layout.tcb.SLOT_FAULT_HANDLER = ok (cslotAddrA sl) := by + rw [← hslotIdx]; exact tcb_slot_val _ _ + have hneS : fhSlot ≠ cslotAddrA sl := fun e => by + have h := cslotAddrA_inj J.j.1.place hAsrc hAd (hκsrc.trans e) + have e1 : specPtr ps = specPtr t := by rw [← hss1, h] + rw [specPtr_inj e1, hthT] at hss2 + cases hss2 + have hneI : slot ≠ cslotAddrA sl := fun e => by + have h := cslotAddrA_inj J.j.1.place hAκ hAd (hκ.trans e) + have e1 : specPtr pi = specPtr t := by rw [← hsi1, h] + rw [specPtr_inj e1, hthT] at hsi2 + cases hsi2 + obtain ⟨xr, heq, hset, D1, hdelWP⟩ := J.restartDelA s a σ m hPl hCnt hslot hcov hext P hCl hCs sl hAd + obtain ⟨x', hx', -, hset', -⟩ := J.restartMachA hslot hcov hext + have e1 : stUpdA σ σ.curThread xr .restart = stUpdA σ σ.curThread x' .restart := by + have := Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst (hset.symm.trans hset')) + exact (Prod.mk.inj this).2 + have hr : tcb.restart_caller st s = ok (L.wr s (stA (L.curW s)) sched.TS_RESTART) := by + unfold tcb.restart_caller + rw [cur_thread_ok, bind_ok, heq] + obtain ⟨⟨r, sd, ad⟩, hdel0, σd, Gd, Dd, hres, hW⟩ := spec_existsA hdelWP + have sysd := J.sysDelSlotA hA hPl hslot hcov hext hcap layout.tcb.SLOT_FAULT_HANDLER + (by simp [layout.tcb.SLOT_FAULT_HANDLER]) hstall hr (r := r) (sd := sd) (ad := ad) + (by rw [hslotIdx]; exact hdel0) + have hCap : obj.Slot.cap st fhSlot s = ok (capA st s fhSlot) := capA_eq s fhSlot + have hdecBase : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + tcb.thread_control_sched st ar (L.wr s (stA (L.curW s)) sched.TS_RESTART) a t slot + (capA st s fhSlot, fhSlot) mcp prio v 14#u64 := by + rw [hcap, decodeTcb_programA, if_neg Bool.false_ne_true, hlbl, + tcbSetSchedParams_controlA hstall he0 he1 he2 hmcp hprio hAuth (hck _ hle1) (hck _ hle2) hsc hold hCap hValid + hr, if_pos rfl] + have hfl : ((14#u64 &&& tcb.SCHED_UPDATE_FAULT) != 0#u64) = true := by rw [tcb.SCHED_UPDATE_FAULT]; decide + rcases hres with ⟨hr1, hnull, hspec, hch⟩ | ⟨hr1, hspec⟩ + swap + · -- preempted deletion + subst hr1 + obtain ⟨⟨⟨-, X, sv⟩, -⟩, hpre⟩ := hW + obtain ⟨Q, R, hw', Ac', Bf', stack', Jd⟩ := X.toStage3A sysd.1 sysd.2 sv + have hdecP : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (.Err .Preempted, sd, ad) := by + rw [hdecBase] + simp only [tcb.thread_control_sched, lift, bind_ok, hfl, if_true, tcb.install_tcb_cap, hdest, hdel0, + uncurry_apply_pair, core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame.from, + bind_tc_ok] + refine ⟨.Err .Preempted, sd, ad, hdecP, x', stUpdA σ σ.curThread x' .restart, σd, hx', rfl, hset', Gd, Q, R, hw', + Ac', Bf', stack', Jd, Or.inr ⟨rfl, fun κs' => ?_, hpre.1, hpre.2.1⟩⟩ + rw [e1] at hspec + exact sspEp_invokeErrA κs _ _ _ _ hspec + subst hr1 + obtain ⟨hWT, Binv, -⟩ := hW + have Pr := (delW_prA hWT).1 + obtain ⟨⟨-, X, sv⟩, -⟩ := hWT + obtain ⟨Q, R, hw', Ac', Bf', stack', Jd⟩ := X.toStage3A sysd.1 sysd.2 sv + rw [e1] at hspec + have hkeepS : capA st sd fhSlot = .Endpoint p b sn rv g gr ∨ capA st sd fhSlot = .Null ∨ HomeZA st sd fhSlot := by + rcases hch fhSlot hsrcOS hneS with h | h | h + · exact Or.inl (h.trans hnc) + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr h) + have hkeepI : capA st sd slot = .Thread t ∨ capA st sd slot = .Null ∨ HomeZA st sd slot := by + rcases hch slot hslot hneI with h | h | h + · exact Or.inl (h.trans hcap) + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr h) + have hSV := tcbStillValid_keptA (st := st) sd t slot fhSlot hsrc0 p b sn rv g gr hkeepS hkeepI + have hnotnull : capability.Cap.is_null (.Endpoint p b sn rv g gr) = ok false := rfl + have hnSpec : absCapA (.Endpoint p b sn rv g gr) ≠ .nullCap := by simp [absCapA] + obtain ⟨cS, hgS, hcS⟩ := getCap_eqA Jd.j.1.st (show AbsSlotA Z.th κsrc from hAsrc) + obtain ⟨cI, hgI, hcI⟩ := getCap_eqA Jd.j.1.st (show AbsSlotA Z.th κs from hAκ) + rw [capAtA_eq (st := st), hκsrc] at hcS + rw [capAtA_eq (st := st), hκ] at hcI + have mS : (cS, σd) ∈ (getCap (Z := Unit) κsrc σd).1 := by rw [hgS]; exact Set.mem_singleton _ + have mI : (cI, σd) ∈ (getCap (Z := Unit) κs σd).1 := by rw [hgI]; exact Set.mem_singleton _ + have eS : capA st sd fhSlot = .Endpoint p b sn rv g gr → cS = absCapA (.Endpoint p b sn rv g gr) := fun h => by + rw [h] at hcS + exact (CapEqA.eq_abs hcS (fun _ _ _ h => by cases h)).symm + have eI : capA st sd slot = .Thread t → cI = .threadCap (specPtr t) := fun h => by + rw [h] at hcI + exact (CapEqA.eq_abs hcI (fun _ _ _ h => by cases h)).symm + have hsameT : fl4TcbSameObject (.threadCap (specPtr t)) (.threadCap (specPtr t)) := by + simp [fl4TcbSameObject, fl4TcbFrame, sameObjectAs, sameRegionAs, isEpCap, isThreadCap, objRefOf] + have hsameE : fl4TcbSameObject (absCapA (.Endpoint p b sn rv g gr)) (absCapA (.Endpoint p b sn rv g gr)) := by + simp [absCapA, fl4TcbSameObject, fl4TcbFrame, sameObjectAs, sameRegionAs, isEpCap, isThreadCap, objRefOf] + have hcurd : σd.curThread = σ.curThread := Binv.2.1 + have htcS : specPtr t ≠ σ.curThread := fun e => hne (specPtr_inj (e.trans J.sr.sch.curThread)) + -- the install: either it inserts (still valid) or it does nothing + have key : ∃ (s2 : S) (σ2 : Fl4SpecA64.State Unit) (Gi : MdbGhostA), + tcb.install_tcb_cap st ar (L.wr s (stA (L.curW s)) sched.TS_RESTART) a t slot layout.tcb.SLOT_FAULT_HANDLER + (capA st s fhSlot) fhSlot = ok (.Ok (), s2, ad) ∧ + (.ok (), σ2) ∈ ((fl4InstallTcbCap (Z := Unit) (specPtr t) κs 3 + (some (absCapA (capA st s fhSlot), κsrc))).run (stUpdA σ σ.curThread x' .restart)).1 ∧ + Sched.BodyKeepA σd σ2 ∧ DWkA σd σ2 ∧ + JStage3A st AR { { Z with G := Gd, Q := Q, R := R, hw := hw' } with G := Gi } H OS U Ac' Pp Tb Lv ia ie N E Rp + Bf' stack' κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s2 ad σ2 m := by + by_cases hv : capA st sd fhSlot = .Endpoint p b sn rv g gr ∧ capA st sd slot = .Thread t + swap + · rw [decide_eq_false hv] at hSV + refine ⟨sd, σd, Gd, ?_, ?_, Sched.BodyKeepA.refl σd, PreO.refl (R := DWkA) σd, Jd⟩ + · rw [hnc] + simp only [tcb.install_tcb_cap, hdest, hdel0, bind_ok, uncurry_apply_pair, + core.result.Result.Insts.CoreOpsTry.branch, hnotnull, hSV, Bool.false_eq_true, ite_false] + · rw [hnc] + by_cases hS : capA st sd fhSlot = .Endpoint p b sn rv g gr + · have hI : capA st sd slot ≠ .Thread t := fun h => hv ⟨hS, h⟩ + have hnT : ¬ fl4TcbSameObject (.threadCap (specPtr t)) cI := by + rcases hkeepI with h | h | ⟨zt, k, h⟩ + · exact absurd h hI + all_goals + rw [h] at hcI + rw [← CapEqA.eq_abs hcI (fun _ _ _ h => by cases h)] + simp [absCapA, fl4TcbSameObject, fl4TcbFrame, sameObjectAs, sameRegionAs, isEpCap, isThreadCap, + objRefOf] + exact tcbSlotSkip_invA hspec hnSpec mS (by rw [eS hS]; exact hsameE) mI hnT + · have hnE : ¬ fl4TcbSameObject (absCapA (.Endpoint p b sn rv g gr)) cS := by + rcases hkeepS with h | h | ⟨zt, k, h⟩ + · exact absurd h hS + all_goals + rw [h] at hcS + rw [← CapEqA.eq_abs hcS (fun _ _ _ h => by cases h)] + simp [absCapA, fl4TcbSameObject, fl4TcbFrame, sameObjectAs, sameRegionAs, isEpCap, isThreadCap, + objRefOf] + exact tcbSlotSkip_srcA hspec hnSpec mS hnE + rw [decide_eq_true hv] at hSV + obtain ⟨hS, hI⟩ := hv + have hS' : capA st sd (cslotAddrA κsrc) = .Endpoint p b sn rv g gr := by rw [hκsrc]; exact hS + have hP := Jd.j.1.place + have hsrcSlot : IsSlotA Z.th (cslotAddrA κsrc) := hAsrc.isSlot J.j.1.place + have hdOS : cslotAddrA sl ∈ OS := J.j5.2.1 _ (hAd.isSlot J.j.1.place) + have hwf : CapWfA (.Endpoint p b sn rv g gr) := by + have h := Jd.capWfA (y := cslotAddrA κsrc) hsrcSlot + rwa [hS'] at h + have hw'' : CapRt.EncWf (.Endpoint p b sn rv g gr) := by + have h : CapRt.EncWf (capAtA (L.mem sd) (cslotAddrA κsrc)) := CapRt.decodeF_encWf _ _ + rwa [capAtA_eq (st := st), hS'] at h + have hno : ¬ IrqNodeA (L.ksb sd) (fl4Ptr sl.1) := by + change ¬ IrqNodeA (L.ksb sd) (fl4Ptr (specPtr t)) + rw [fl4Ptr_specPtr] + intro h + have := Jd.j.1.place.irqK hthT h + cases this + have hN : NullOrEp H (.Endpoint p b sn rv g gr) := by + have h := nullOrEp_validFhA J.capTy hsrcOS hValid + rwa [hnc] at h + have hIns : ∀ s2, cdt.insert st sd (.Endpoint p b sn rv g gr) (cslotAddrA κsrc) (cslotAddrA sl) = ok s2 → + tcb.install_tcb_cap st ar (L.wr s (stA (L.curW s)) sched.TS_RESTART) a t slot layout.tcb.SLOT_FAULT_HANDLER + (capA st s fhSlot) fhSlot = ok (.Ok (), s2, ad) := fun s2 h2 => by + rw [hκsrc] at h2 + rw [hnc] + simp only [tcb.install_tcb_cap, hdest, hdel0, bind_ok, uncurry_apply_pair, + core.result.Result.Insts.CoreOpsTry.branch, hnotnull, hSV, h2, Bool.false_eq_true, ite_false, ite_true] + have hi3 : layout.tcb.SLOT_FAULT_HANDLER.val < 7 := by simp [layout.tcb.SLOT_FAULT_HANDLER] + have hi23 : layout.tcb.SLOT_FAULT_HANDLER.val ≠ 2 := by simp [layout.tcb.SLOT_FAULT_HANDLER] + have hi53 : layout.tcb.SLOT_FAULT_HANDLER.val ≠ 5 := by simp [layout.tcb.SLOT_FAULT_HANDLER] + have hhp := Jd.sys.ent.heap + have hyB : cslotAddrA sl ∉ BufS H.tcbs := by + rw [← hslotIdx]; exact tcb_slot_notBufS hhp htT hi3 hi23 + have hyE : cslotAddrA sl ∉ EptS H.tcbs := by + rw [← hslotIdx]; exact tcb_slot_notEptS hhp htT hi3 hi53 + have hcd : ∃ z ∈ OS, CovZ (capA st sd z) (cslotAddrA sl) := + ⟨slot, hslot, by rw [hI, ← hslotIdx]; exact CovZ.tcbSlot hhp htT hi3⟩ + have hdn : capA st sd (cslotAddrA sl) = .Null := hnull + have hxOS : cslotAddrA κsrc ∈ OS := by rw [hκsrc]; exact hsrcOS + have hxy : cslotAddrA κsrc ≠ cslotAddrA sl := fun e => hneS (hκsrc ▸ e) + have hC : InsC (.Endpoint p b sn rv g gr) (capA st sd (cslotAddrA κsrc)) := by + rw [hS'] + exact InsC.of_soa (Or.inl ⟨p, b, sn, rv, g, gr, rfl⟩) (by simp [objects.same_object_as, objects.same_region_as]) + have hEp : ApE (.Endpoint p b sn rv g gr) (capA st sd (cslotAddrA κsrc)) := fun p' b' h => by + rw [hS'] at h; cases h + have hdv : objects.derive_cap st sd (cslotAddrA κsrc) (capA st sd (cslotAddrA κsrc)) = + ok (.Ok (.Endpoint p b sn rv g gr)) := by rw [hS']; rfl + obtain ⟨s2a, hs2a, hF⟩ := spec_existsA (Jd.insertP1A hA hPl (x := cslotAddrA κsrc) (y := cslotAddrA sl) + (nc := .Endpoint p b sn rv g gr) hxOS hdOS hxy (J.delCanA _ hdOS) hdn (Or.inl hyB) hyE hcd hw'' (by simp) hC hEp hdv + (insGA Gd (cslotAddrA κsrc) (cslotAddrA sl) (insBA σd κsrc (absCapA (.Endpoint p b sn rv g gr)) + (absCapA (capA st sd (cslotAddrA κsrc)))))) + have hFs : ∀ s2, cdt.insert st sd (.Endpoint p b sn rv g gr) (cslotAddrA κsrc) (cslotAddrA sl) = ok s2 → + s2 = s2a := fun s2 h2 => okinj (h2.symm.trans hs2a) + obtain ⟨s2, σ2, hs2, hm, hbk, -, J2⟩ := JStage3A.capInsertTcbSlotA (J := Jd) κsrc sl (ps := ps) ⟨hss1, hss2⟩ + (t0 := t) hthT (i := 3) (by decide) (by decide) (by decide) hsl + (by rw [hS']; simp) hdn (J.delCanA _ hdOS) + (nc := .Endpoint p b sn rv g gr) (anc := absCapA (.Endpoint p b sn rv g gr)) (fun _ => hN) hwf + (CapEqA.of_abs rfl) hw'' (by simp) + (by rw [hS']; exact .refl _) (by rw [hS']; exact UA.refl _) (by rw [hS']; trivial) (by rw [hS']) + (by rw [hS']) (by rw [hS']; exact Or.inl rfl) + (fun r b' rs h hb => by + rw [hS'] at h + rcases h with h | h <;> rw [h] <;> first | rfl | (simp [absCapA] at h)) + (fun i h => by rw [hS'] at h; cases h) (fun i h => by cases h) (fun i h => by cases h) + (fun q k j h => by rw [hS'] at h; cases h) (fun h => by rw [hS'] at h; cases h) (Or.inl hno) + (fun h => by rwa [hS'] at h) + ⟨fun _ _ h => by simp [absCapA] at h, fun _ h => by simp [absCapA] at h, + fun _ _ _ h => by simp [absCapA] at h⟩ + (fun s2' h2 => by rw [hFs s2' h2]; exact hF.2.1) + (fun s2' h2 => by rw [hFs s2' h2]; exact hF.1) + (fun s2' σ2' h2 _ => by rw [hFs s2' h2]; exact hF.2.2.2.2 σ2' m) + refine ⟨s2, σ2, _, hIns s2 hs2, ?_, hbk, (capInsert_skA _ _ _ σd ((), σ2) hm).toDWkA, J2⟩ + rw [hnc] + exact tcbSlotInsert_specA hspec hnSpec mS (by rw [eS hS]; exact hsameE) mI (by rw [eI hI]; exact hsameT) hm + obtain ⟨s2, σ2, Gi, hInst, hInstSpec, hbk, wk, J2⟩ := key + have hnsd : ∀ u, σd.schedulerAction ≠ .switchThread u := fun u => Binv.2.2.1 u + have sact2 : SActA σ2 := by + refine sactKeepA Pr.2.1 hnsd hbk ?_ + obtain ⟨x, hx, -⟩ := J2.ep.tcb _ J2.sr.curT + exact ⟨x, by rw [J2.sr.sch.curThread]; exact hx⟩ + have htc2 : specPtr t ≠ σ2.curThread := by rw [hbk.cur, hcurd]; exact htcS + have hns2 : ∀ u, σ2.schedulerAction ≠ .switchThread u := fun u h => hnsd u (hbk.act ▸ h) + obtain ⟨sm, s3, σ3, yd, hMcp, hsp3, hyd, hSet3, Bkm, DS3, J3⟩ := J2.sspTailA hA hPl hCs h1 hmb hmax htu hns2 sact2 + ht (hw mcp hle1) (hw prio hle2) htc2 hidles + have hdecOk : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (.Ok (), s3, ad) := by + rw [hdecBase] + exact tcbSetSchedParams_effectsA false hInst hMcp hsp3 + obtain ⟨-, -, sysl, symL, -, userOK, j7⟩ := J.decodeFinA hA hM hPl hfm hslot hcov + (rfl : capA st s slot = capA st s slot) hext hSC (lbl := lbl) + (by rw [hcap, hlbl]; simp [DecArmS1aEnt, DecArmQH, DecArmQZ2, DecArmKQ2, DecArmKQ, DecArmKQ3, DecArmDelQ, + DecArmS1aC, capFam]) + (fun _ h => by rw [hcap] at h; cases h) (fun _ h => by rw [hcap] at h; cases h) hdecOk σ3 m + obtain ⟨Q3, R3, J3'⟩ := J3 sysl symL userOK j7 + have B2 := BodyOKA.stepA (BodyOKA.stepA (BodyOKA.stepA Binv (delStep_ofKeepA hbk wk.off wk.ctSc)) + (delStep_ofKeepA Bkm (CtOffA.keep rfl (Or.inl rfl)) (CtScKA.ofUpd rfl (fun _ _ c => absurd c.symm htc2)))) DS3 + refine ⟨.Ok (), s3, ad, hdecOk, x', stUpdA σ σ.curThread x' .restart, σ3, hx', rfl, hset', Gi, Q3, R3, hw', Ac', Bf', + stack', J3', Or.inl ⟨rfl, fun κs' => ?_, B2.1, B2.2.1⟩⟩ + exact sspEp_invokeA κs κsrc _ _ _ hInstSpec hyd hSet3 +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3LiftTcbTimeoutA.lean b/proofs/Fl4ProofsA64/P3/Stage3LiftTcbTimeoutA.lean new file mode 100644 index 000000000..078dafd19 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3LiftTcbTimeoutA.lean @@ -0,0 +1,141 @@ +/- +P3 (aarch64, A64-P3 Lift 3b labels): **TCB SetTimeoutEndpoint with a null handler on another thread, at the record**. + +* **`setTimeoutNull_invokeA`** / **`setTimeoutNull_invokeErrA`**: the spec's `fl4InvokeTcb (.threadControlCaps ..)` with only + the timeout part (slot 4, a null cap: delete and no insert) from its `fl4CapDelete` outcome. +* **`JSyscallA.setTimeoutNullA`**: `decode_invocation` on a Thread cap with the SetTimeoutEndpoint label whose extra + slot holds a null cap: `restartDelA`, the delete of slot 4, `still_valid` skipped (`new_cap.is_null`). No word is + written, so the record is the deletion loop's (`DelXA6.toStage3A`), the body frame is the delete's. + +Named premises: those of `JSyscallA.restartDelA` and [P] `ArchTotal`, `MaxSpi`, `ArchPlat`, `A64FrameMasks`; [S] `h1`. +The non-null handler (a `cdt::insert` after the delete) and the `Err` returns before the delete are not lifted. New module. +-/ +import Fl4ProofsA64.P3.Stage3LiftTcbBufA +import Fl4ProofsA64.P3.TcbInvokeTimeoutA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Vcpu Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + +theorem setTimeoutNull_invokeA {σ1 σd : Fl4SpecA64.State Unit} {t : U64} (κs κsrc : CslotPtr) + (hdel : (.ok (), σd) ∈ ((fl4CapDelete (Z := Unit) (specPtr t, tcbCnodeIndex 4)).run σ1).1) : + (.ok [], σd) ∈ ((fl4InvokeTcb (.threadControlCaps (specPtr t) κs none (some (.nullCap, κsrc)) none none none)).run σ1).1 := by + refine tcbSetTimeoutEndpoint_specPostA ?_ + simp only [fl4InstallTcbCap] + refine mem_bindE_okD (a := ()) (s' := σd) hdel ?_ + simp [Fl4Spec.NonDetE.unlessE, run_returnOkD] + +theorem setTimeoutNull_invokeErrA {σ1 σd : Fl4SpecA64.State Unit} {t : U64} (κs κsrc : CslotPtr) + (hdel : (.error (), σd) ∈ ((fl4CapDelete (Z := Unit) (specPtr t, tcbCnodeIndex 4)).run σ1).1) : + (.error (), σd) ∈ ((fl4InvokeTcb (.threadControlCaps (specPtr t) κs none (some (.nullCap, κsrc)) none none none)).run σ1).1 := by + have hSkip (n : Nat) : fl4InstallTcbCap (Z := Unit) (specPtr t) κs n none = Fl4Spec.NonDetE.returnOk () := rfl + have hrk : (.ok (), σ1) ∈ ((NonDetE.returnOk (ε := Unit) ()).run σ1).1 := by rw [run_returnOkD]; simp + simp only [fl4InvokeTcb, hSkip] + refine mem_bindE_okD (a := ()) (s' := σ1) hrk ?_ + simp only [fl4InstallTcbCap] + exact mem_bindE_errD (mem_bindE_errD hdel) + +section +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {Clock : A → Fl4SpecA64.MachineState → Prop} + +set_option maxHeartbeats 4000000 in +/-- **TCB SetTimeoutEndpoint with a null handler on another thread at a syscall entry** (module docstring). -/ +theorem JSyscallA.setTimeoutNullA [PlainStore st] + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (s : S) (a : A) (σ : Fl4SpecA64.State Unit) (m : Fl4SpecA64.McState) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hCnt : CounterStepA σ) + (P : DelPlatA st ar AR Clock) (hCl : Clock a σ.machineState) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + {slot lbl len buf t src : U64} {blk call don : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Thread t) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) + (hlbl : lbl = fl4_abi.label.TCBSetTimeoutEndpoint) + (he : tcb.extra_with_length ⟨len, buf, call, slot, extra⟩ 0#usize 0#u64 = ok (.Ok src)) + (hnullh : capA st s src = .Null) (κsrc : CslotPtr) : + ∃ r s3 a3, syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (r, s3, a3) ∧ + TcbDelOutA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n m ar hPl.ipa s a σ + (fun κs => .threadControlCaps (specPtr t) κs none (some (.nullCap, κsrc)) none none none) r s3 a3 := by + have ht : t ∈ Z.T := J.tcbs ▸ (J.toJStage3A.slotCapTy hslot).1.2.2.2.2.2 t hcap + have htT : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + have hthT : Z.th t = some .tcb := (J.j.1.thT t).2 ht + have T0 := J.toJStage3A.toTA J.sact J.noSwitch + have hstall : smp.remote_tcb_stall st ar s a t = ok (s, a) := T0.stall_singleA hCs h1 ht + set sl : Fl4SpecA64.CslotPtr := (specPtr t, tcbCnodeIndex 4) with hsl + have hAd : AbsSlotA Z.th sl := ⟨t, rfl, Or.inr ⟨hthT, 4, by decide, rfl⟩⟩ + have hslotIdx : slotIdx t layout.tcb.SLOT_TIMEOUT_HANDLER = cslotAddrA sl := by + have h4 : layout.tcb.SLOT_TIMEOUT_HANDLER.val = 4 := by simp [layout.tcb.SLOT_TIMEOUT_HANDLER] + rw [slotIdx_addrA, h4] + unfold cslotAddrA + show _ = slotAddrA (specPtr t) (Fl4SpecA64.ofBl (tcbCnodeIndex 4)) + rw [ofBl_tcbIdx 4 (by decide)] + have hdest : obj.Tcb.slot t layout.tcb.SLOT_TIMEOUT_HANDLER = ok (cslotAddrA sl) := by + rw [← hslotIdx]; exact tcb_slot_val _ _ + obtain ⟨xr, heq, hset, D1, hdelWP⟩ := J.restartDelA s a σ m hPl hCnt hslot hcov hext P hCl hCs sl hAd + obtain ⟨x', hx', -, hset', -⟩ := J.restartMachA hslot hcov hext + have e1 : stUpdA σ σ.curThread xr .restart = stUpdA σ σ.curThread x' .restart := by + have := Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst (hset.symm.trans hset')) + exact (Prod.mk.inj this).2 + have hr : tcb.restart_caller st s = ok (L.wr s (stA (L.curW s)) sched.TS_RESTART) := by + unfold tcb.restart_caller + rw [cur_thread_ok, bind_ok, heq] + obtain ⟨⟨r, sd, ad⟩, hdel0, σd, Gd, Dd, hres, hW⟩ := spec_existsA hdelWP + have sysd := J.sysDelSlotA hA hPl hslot hcov hext hcap layout.tcb.SLOT_TIMEOUT_HANDLER + (by simp [layout.tcb.SLOT_TIMEOUT_HANDLER]) hstall hr (r := r) (sd := sd) (ad := ad) + (by rw [hslotIdx]; exact hdel0) + have hCap : obj.Slot.cap st src s = ok .Null := by rw [capA_eq, hnullh] + have hValid : faults.valid_fault_handler .Null = ok true := rfl + have hdecBase : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + (do + let (r, sFinal, aFinal) ← tcb.install_tcb_cap st ar (L.wr s (stA (L.curW s)) sched.TS_RESTART) a t + slot layout.tcb.SLOT_TIMEOUT_HANDLER .Null src + ok (r, sFinal, aFinal)) := by + rw [hcap, decodeTcb_programA, if_neg Bool.false_ne_true, hlbl, + tcbSetTimeoutEndpoint_selectedA hstall he hCap hValid hr] + rcases hres with ⟨hr1, hnull, hspec, -⟩ | ⟨hr1, hspec⟩ + · subst hr1 + obtain ⟨⟨⟨-, X, sv⟩, -⟩, Binv, Own⟩ := hW + obtain ⟨Q, R, hw, Ac', Bf', stack', Jd⟩ := X.toStage3A sysd.1 sysd.2 sv + have hdecOk : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (.Ok (), sd, ad) := by + rw [hdecBase] + simp only [tcb.install_tcb_cap, hdest, hdel0, bind_ok, uncurry_apply_pair, + core.result.Result.Insts.CoreOpsTry.branch, cap_is_null_nullA, if_true] + refine ⟨.Ok (), sd, ad, hdecOk, x', stUpdA σ σ.curThread x' .restart, σd, hx', rfl, hset', Gd, Q, R, hw, Ac', Bf', + stack', Jd, Or.inl ⟨rfl, fun κs => ?_, Binv.1, Binv.2.1⟩⟩ + rw [e1] at hspec + exact setTimeoutNull_invokeA κs κsrc hspec + · subst hr1 + obtain ⟨⟨⟨-, X, sv⟩, -⟩, hpre⟩ := hW + obtain ⟨Q, R, hw, Ac', Bf', stack', Jd⟩ := X.toStage3A sysd.1 sysd.2 sv + have hdecP : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (.Err .Preempted, sd, ad) := by + rw [hdecBase] + simp only [tcb.install_tcb_cap, hdest, hdel0, bind_ok, uncurry_apply_pair, + core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame.from, + bind_tc_ok] + refine ⟨.Err .Preempted, sd, ad, hdecP, x', stUpdA σ σ.curThread x' .restart, σd, hx', rfl, hset', Gd, Q, R, hw, + Ac', Bf', stack', Jd, Or.inr ⟨rfl, fun κs => ?_, hpre.1, hpre.2.1⟩⟩ + rw [e1] at hspec + exact setTimeoutNull_invokeErrA κs κsrc hspec + +end + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3LiftTcbTimeoutNnA.lean b/proofs/Fl4ProofsA64/P3/Stage3LiftTcbTimeoutNnA.lean new file mode 100644 index 000000000..227d5939b --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3LiftTcbTimeoutNnA.lean @@ -0,0 +1,389 @@ +/- +P3 (aarch64, A64-P3 Lift 3b labels): **TCB SetTimeoutEndpoint with a non-null handler, at the record**. + +* **`tcbExtra0_memA`**: the extra slot `extra_with_length` returns is a non-null member of `extra`. +* **`tcbStillValid_keptA`**: after the deletion, `still_valid` of an endpoint handler holds exactly when the source slot + still holds the handler and the invoked slot still holds the thread's cap (each other cap is that, Null or a zombie). +* **`tcbSlotSkip_srcA`** / **`tcbSlotSkip_invA`**: the spec's `install_tcb_cap` when one of its two `check_cap_at` fails. +* **`delStep_ofKeepA`**: a `BodyKeepA` step keeping the current thread's clauses is a `DelStepA`. +* **`JSyscallA.setTimeoutNonNullA`**: `decode_invocation` on a Thread cap with the SetTimeoutEndpoint label whose extra + slot holds a valid non-null handler: `restartDelA`, the delete of slot 4, `still_valid`, then `cdt::insert` + (`capInsertTcbSlotA`) when it holds. The source's cap after the delete is the handler (`ChgZA`), so no premise about + it is needed. + +Named premises: those of `JSyscallA.setTimeoutNullA`, and `A64FrameMasks`. The `Err` returns before the delete are not +lifted. New module. +-/ +import Fl4ProofsA64.P3.Stage3LiftTcbTimeoutA +import Fl4ProofsA64.P3.Stage3CapInsertTA +import Fl4ProofsA64.P3.Stage3LiftIrqDecA +import Fl4ProofsA64.P3.TcbInvokeSlotPostA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Vcpu Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +theorem tcbExtra0_memA {c : tcb.Ctx} {n : U64} {src : U64} + (he : tcb.extra_with_length c 0#usize n = ok (.Ok src)) : src ∈ c.extra.val ∧ src ≠ 0#u64 := by + unfold tcb.extra_with_length at he + obtain ⟨o, ho, he⟩ := bind_eq_ok he + cases o with + | none => + exfalso + simp only [error.truncated] at he + cases okinj he + | some x => + simp only at he + have ex : x = src := by + split at he + · exact core.result.Result.Ok.inj (okinj he) + · exfalso + simp only [error.truncated] at he + cases okinj he + subst ex + unfold tcb.extra at ho + split at ho + · cases okinj ho + obtain ⟨y, hy, ho⟩ := bind_eq_ok ho + obtain ⟨b, hb, ho⟩ := bind_eq_ok ho + have hlen : c.extra.val.length = 3 := by simp + have hm : y ∈ c.extra.val := by + obtain ⟨v, hv, e⟩ := spec_existsA (Aeneas.Std.Array.index_usize_spec c.extra 0#usize (by simp [hlen])) + have ev : v = y := okinj (hv.symm.trans hy) + rw [← ev, e] + exact List.getElem_mem _ + unfold obj.Slot.is_null at hb + have hb' : b = decide (y = 0#u64) := (okinj hb).symm + by_cases h0 : y = 0#u64 + · rw [hb', decide_eq_true h0, if_pos rfl] at ho + cases okinj ho + · rw [hb', decide_eq_false h0] at ho + simp only [Bool.false_eq_true, ↓reduceIte] at ho + have e := Option.some.inj (okinj ho) + exact e ▸ ⟨hm, h0⟩ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +theorem tcbStillValid_keptA (s : S) (target slot src : U64) (hsrc : src ≠ 0#u64) + (p b : U64) (x y z w : Bool) + (hl : capA st s src = .Endpoint p b x y z w ∨ capA st s src = .Null ∨ HomeZA st s src) + (hi : capA st s slot = .Thread target ∨ capA st s slot = .Null ∨ HomeZA st s slot) : + tcb.still_valid st s target slot (.Endpoint p b x y z w) src = + ok (decide (capA st s src = .Endpoint p b x y z w ∧ capA st s slot = .Thread target)) := by + have hn : obj.Slot.is_null src = ok false := by + unfold obj.Slot.is_null; rw [decide_eq_false hsrc] + rw [tcbStillValid_liveA s target slot src _ (capA st s src) hn (capA_eq s src), capA_eq s slot] + have key : objects.same_object_as (.Thread target) (capA st s slot) = + ok (decide (capA st s slot = .Thread target)) := by + rcases hi with e | e | ⟨zt, n, e⟩ <;> rw [e] <;> simp [objects.same_object_as, objects.same_region_as] + rcases hl with e | e | ⟨zt, n, e⟩ <;> rw [e] <;> + simp [objects.same_object_as, objects.same_region_as, bind_ok] + simpa [objects.same_object_as, objects.same_region_as] using key + +end + +theorem tcbSlotSkip_srcA {σ σd : Fl4SpecA64.State Unit} {target : ObjRef} {index : Nat} + {invoked src : CslotPtr} {newCap source : Cap} + (hdel : (.ok (), σd) ∈ ((fl4CapDelete (Z := Unit) (target, tcbCnodeIndex index)).run σ).1) + (hnew : newCap ≠ .nullCap) + (hsource : (source, σd) ∈ (getCap (Z := Unit) src σd).1) + (hmatch : ¬ fl4TcbSameObject newCap source) : + (.ok (), σd) ∈ ((fl4InstallTcbCap (Z := Unit) target invoked index (some (newCap, src))).run σ).1 := by + unfold fl4InstallTcbCap + refine mem_bindE_okD hdel ?_ + rw [Fl4Spec.NonDetE.unlessE, if_neg hnew] + apply mem_liftED + change ((), σd) ∈ ((getCap (Z := Unit) src >>= fun live => + Fl4Spec.NonDet.when (fl4TcbSameObject newCap live) (getCap (Z := Unit) invoked >>= fun cap => + Fl4Spec.NonDet.when (fl4TcbSameObject (.threadCap target) cap) + (capInsert (Z := Unit) newCap src (target, tcbCnodeIndex index)))) σd).1 + refine mem_bind_ndD hsource ?_ + rw [Fl4Spec.NonDet.when, if_neg hmatch] + exact Set.mem_singleton _ + +theorem tcbSlotSkip_invA {σ σd : Fl4SpecA64.State Unit} {target : ObjRef} {index : Nat} + {invoked src : CslotPtr} {newCap source current : Cap} + (hdel : (.ok (), σd) ∈ ((fl4CapDelete (Z := Unit) (target, tcbCnodeIndex index)).run σ).1) + (hnew : newCap ≠ .nullCap) + (hsource : (source, σd) ∈ (getCap (Z := Unit) src σd).1) + (hmatch : fl4TcbSameObject newCap source) + (hinvoked : (current, σd) ∈ (getCap (Z := Unit) invoked σd).1) + (htarget : ¬ fl4TcbSameObject (.threadCap target) current) : + (.ok (), σd) ∈ ((fl4InstallTcbCap (Z := Unit) target invoked index (some (newCap, src))).run σ).1 := by + unfold fl4InstallTcbCap + refine mem_bindE_okD hdel ?_ + rw [Fl4Spec.NonDetE.unlessE, if_neg hnew] + apply mem_liftED + change ((), σd) ∈ ((getCap (Z := Unit) src >>= fun live => + Fl4Spec.NonDet.when (fl4TcbSameObject newCap live) (getCap (Z := Unit) invoked >>= fun cap => + Fl4Spec.NonDet.when (fl4TcbSameObject (.threadCap target) cap) + (capInsert (Z := Unit) newCap src (target, tcbCnodeIndex index)))) σd).1 + refine mem_bind_ndD hsource ?_ + rw [Fl4Spec.NonDet.when, if_pos hmatch] + refine mem_bind_ndD hinvoked ?_ + rw [Fl4Spec.NonDet.when, if_neg htarget] + exact Set.mem_singleton _ + +theorem delStep_ofKeepA {σ σ' : Fl4SpecA64.State Unit} (K : Sched.BodyKeepA σ σ') + (off : CtOffA σ.curThread σ → CtOffA σ.curThread σ') (ctSc : CtScKA σ.curThread σ σ') : DelStepA σ σ' where + cur := K.cur + time := K.time + last := K.last + csc := K.csc + cons := K.cons + rel := fun t ht => by rw [K.rel] at ht; exact ht + sc := fun y' k h => ⟨y', K.sc _ _ _ h, Or.inl ⟨rfl, rfl, rfl⟩⟩ + act := Or.inl K.act + ctSt := fun _ x h => K.tcb _ _ h + off := off + ctSc := ctSc + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {Clock : A → Fl4SpecA64.MachineState → Prop} + +set_option maxHeartbeats 8000000 in +/-- **TCB SetTimeoutEndpoint with a non-null handler at a syscall entry** (module docstring). -/ +theorem JSyscallA.setTimeoutNonNullA [PlainStore st] + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (s : S) (a : A) (σ : Fl4SpecA64.State Unit) (m : Fl4SpecA64.McState) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) (hCnt : CounterStepA σ) + (P : DelPlatA st ar AR Clock) (hCl : Clock a σ.machineState) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + {slot lbl len buf t src : U64} {blk call don : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Thread t) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hlbl : lbl = fl4_abi.label.TCBSetTimeoutEndpoint) + (he : tcb.extra_with_length ⟨len, buf, call, slot, extra⟩ 0#usize 0#u64 = ok (.Ok src)) + (hnn : capA st s src ≠ .Null) (hValid : faults.valid_fault_handler (capA st s src) = ok true) + {κs κsrc : CslotPtr} (hAκ : AbsSlotA Z.th κs) (hκ : cslotAddrA κs = slot) + (hAsrc : AbsSlotA Z.th κsrc) (hκsrc : cslotAddrA κsrc = src) : + ∃ r s3 a3, syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (r, s3, a3) ∧ + TcbDelOutA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n m ar hPl.ipa s a σ + (fun _ => .threadControlCaps (specPtr t) κs none (some (absCapA (capA st s src), κsrc)) none none none) + r s3 a3 := by + obtain ⟨p, b, sn, rv, g, gr, hnc⟩ : ∃ p b sn rv g gr, capA st s src = .Endpoint p b sn rv g gr := by + generalize capA st s src = c at hnn hValid + cases c with + | Null => exact absurd rfl hnn + | Endpoint p b sn rv g gr => exact ⟨p, b, sn, rv, g, gr, rfl⟩ + | _ => exact absurd (okinj hValid) Bool.false_ne_true + have ht : t ∈ Z.T := J.tcbs ▸ (J.toJStage3A.slotCapTy hslot).1.2.2.2.2.2 t hcap + have hthT : Z.th t = some .tcb := (J.j.1.thT t).2 ht + have T0 := J.toJStage3A.toTA J.sact J.noSwitch + have hstall : smp.remote_tcb_stall st ar s a t = ok (s, a) := T0.stall_singleA hCs h1 ht + obtain ⟨hsrcM, hsrc0⟩ := tcbExtra0_memA he + have hsrcOS : src ∈ OS := hext _ hsrcM hsrc0 + obtain ⟨ss', ps, hss1, hss2, hss3, -⟩ := J.toJStage3A.cnSlot_of_covA (hSC _ hsrcM hsrc0) + have ess : κsrc = ss' := + cslotAddrA_inj J.j.1.place hAsrc ⟨ps, hss1, Or.inl hss2⟩ (hκsrc.trans hss3.symm) + subst ess + obtain ⟨si', pi, hsi1, hsi2, hsi3, -⟩ := J.toJStage3A.cnSlot_of_covA hcov + have esi : κs = si' := + cslotAddrA_inj J.j.1.place hAκ ⟨pi, hsi1, Or.inl hsi2⟩ (hκ.trans hsi3.symm) + subst esi + set sl : Fl4SpecA64.CslotPtr := (specPtr t, tcbCnodeIndex 4) with hsl + have hAd : AbsSlotA Z.th sl := ⟨t, rfl, Or.inr ⟨hthT, 4, by decide, rfl⟩⟩ + have hslotIdx : slotIdx t layout.tcb.SLOT_TIMEOUT_HANDLER = cslotAddrA sl := by + have h4 : layout.tcb.SLOT_TIMEOUT_HANDLER.val = 4 := by simp [layout.tcb.SLOT_TIMEOUT_HANDLER] + rw [slotIdx_addrA, h4] + unfold cslotAddrA + show _ = slotAddrA (specPtr t) (Fl4SpecA64.ofBl (tcbCnodeIndex 4)) + rw [ofBl_tcbIdx 4 (by decide)] + have hdest : obj.Tcb.slot t layout.tcb.SLOT_TIMEOUT_HANDLER = ok (cslotAddrA sl) := by + rw [← hslotIdx]; exact tcb_slot_val _ _ + -- the source and the invoked slot are CNode slots, not the destination + have hneS : src ≠ cslotAddrA sl := fun e => by + have h := cslotAddrA_inj J.j.1.place hAsrc hAd (hκsrc.trans e) + have e1 : specPtr ps = specPtr t := by rw [← hss1, h] + rw [specPtr_inj e1, hthT] at hss2 + cases hss2 + have hneI : slot ≠ cslotAddrA sl := fun e => by + have h := cslotAddrA_inj J.j.1.place hAκ hAd (hκ.trans e) + have e1 : specPtr pi = specPtr t := by rw [← hsi1, h] + rw [specPtr_inj e1, hthT] at hsi2 + cases hsi2 + obtain ⟨xr, heq, hset, D1, hdelWP⟩ := J.restartDelA s a σ m hPl hCnt hslot hcov hext P hCl hCs sl hAd + obtain ⟨x', hx', -, hset', -⟩ := J.restartMachA hslot hcov hext + have e1 : stUpdA σ σ.curThread xr .restart = stUpdA σ σ.curThread x' .restart := by + have := Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst (hset.symm.trans hset')) + exact (Prod.mk.inj this).2 + have hr : tcb.restart_caller st s = ok (L.wr s (stA (L.curW s)) sched.TS_RESTART) := by + unfold tcb.restart_caller + rw [cur_thread_ok, bind_ok, heq] + obtain ⟨⟨r, sd, ad⟩, hdel0, σd, Gd, Dd, hres, hW⟩ := spec_existsA hdelWP + have sysd := J.sysDelSlotA hA hPl hslot hcov hext hcap layout.tcb.SLOT_TIMEOUT_HANDLER + (by simp [layout.tcb.SLOT_TIMEOUT_HANDLER]) hstall hr (r := r) (sd := sd) (ad := ad) + (by rw [hslotIdx]; exact hdel0) + have hCap : obj.Slot.cap st src s = ok (capA st s src) := capA_eq s src + have hdecBase : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + (do + let (r, sFinal, aFinal) ← tcb.install_tcb_cap st ar (L.wr s (stA (L.curW s)) sched.TS_RESTART) a t + slot layout.tcb.SLOT_TIMEOUT_HANDLER (capA st s src) src + ok (r, sFinal, aFinal)) := by + rw [hcap, decodeTcb_programA, if_neg Bool.false_ne_true, hlbl, + tcbSetTimeoutEndpoint_selectedA hstall he hCap hValid hr] + rcases hres with ⟨hr1, hnull, hspec, hch⟩ | ⟨hr1, hspec⟩ + swap + · subst hr1 + obtain ⟨⟨⟨-, X, sv⟩, -⟩, hpre⟩ := hW + obtain ⟨Q, R, hw, Ac', Bf', stack', Jd⟩ := X.toStage3A sysd.1 sysd.2 sv + have hdecP : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (.Err .Preempted, sd, ad) := by + rw [hdecBase] + simp only [tcb.install_tcb_cap, hdest, hdel0, bind_ok, uncurry_apply_pair, + core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame.from, + bind_tc_ok] + refine ⟨.Err .Preempted, sd, ad, hdecP, x', stUpdA σ σ.curThread x' .restart, σd, hx', rfl, hset', Gd, Q, R, hw, + Ac', Bf', stack', Jd, Or.inr ⟨rfl, fun _ => ?_, hpre.1, hpre.2.1⟩⟩ + rw [e1] at hspec + have hSkip (k : Nat) : fl4InstallTcbCap (Z := Unit) (specPtr t) κs k none = Fl4Spec.NonDetE.returnOk () := rfl + have hrk : (.ok (), stUpdA σ σ.curThread x' .restart) ∈ + ((NonDetE.returnOk (ε := Unit) ()).run (stUpdA σ σ.curThread x' .restart)).1 := by rw [run_returnOkD]; simp + simp only [fl4InvokeTcb, hSkip] + refine mem_bindE_okD (a := ()) (s' := stUpdA σ σ.curThread x' .restart) hrk ?_ + simp only [fl4InstallTcbCap] + exact mem_bindE_errD (mem_bindE_errD hspec) + subst hr1 + obtain ⟨⟨⟨-, X, sv⟩, -⟩, Binv, Own⟩ := hW + obtain ⟨Q, R, hw, Ac', Bf', stack', Jd⟩ := X.toStage3A sysd.1 sysd.2 sv + rw [e1] at hspec + have hkeepS : capA st sd src = .Endpoint p b sn rv g gr ∨ capA st sd src = .Null ∨ HomeZA st sd src := by + rcases hch src hsrcOS hneS with h | h | h + · exact Or.inl (h.trans hnc) + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr h) + have hkeepI : capA st sd slot = .Thread t ∨ capA st sd slot = .Null ∨ HomeZA st sd slot := by + rcases hch slot hslot hneI with h | h | h + · exact Or.inl (h.trans hcap) + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr h) + have hSV := tcbStillValid_keptA (st := st) sd t slot src hsrc0 p b sn rv g gr hkeepS hkeepI + have hnotnull : capability.Cap.is_null (.Endpoint p b sn rv g gr) = ok false := rfl + have hnSpec : absCapA (.Endpoint p b sn rv g gr) ≠ .nullCap := by simp [absCapA] + -- the spec's two reads + obtain ⟨cS, hgS, hcS⟩ := getCap_eqA Jd.j.1.st (show AbsSlotA Z.th κsrc from hAsrc) + obtain ⟨cI, hgI, hcI⟩ := getCap_eqA Jd.j.1.st (show AbsSlotA Z.th κs from hAκ) + rw [capAtA_eq (st := st), hκsrc] at hcS + rw [capAtA_eq (st := st), hκ] at hcI + have mS : (cS, σd) ∈ (getCap (Z := Unit) κsrc σd).1 := by rw [hgS]; exact Set.mem_singleton _ + have mI : (cI, σd) ∈ (getCap (Z := Unit) κs σd).1 := by rw [hgI]; exact Set.mem_singleton _ + have eS : capA st sd src = .Endpoint p b sn rv g gr → cS = absCapA (.Endpoint p b sn rv g gr) := fun h => by + rw [h] at hcS + exact (CapEqA.eq_abs hcS (fun _ _ _ h => by cases h)).symm + have eI : capA st sd slot = .Thread t → cI = .threadCap (specPtr t) := fun h => by + rw [h] at hcI + exact (CapEqA.eq_abs hcI (fun _ _ _ h => by cases h)).symm + have hsameT : fl4TcbSameObject (.threadCap (specPtr t)) (.threadCap (specPtr t)) := by + simp [fl4TcbSameObject, fl4TcbFrame, sameObjectAs, sameRegionAs, isEpCap, isThreadCap, objRefOf] + have hsameE : fl4TcbSameObject (absCapA (.Endpoint p b sn rv g gr)) (absCapA (.Endpoint p b sn rv g gr)) := by + simp [absCapA, fl4TcbSameObject, fl4TcbFrame, sameObjectAs, sameRegionAs, isEpCap, isThreadCap, objRefOf] + by_cases hv : capA st sd src = .Endpoint p b sn rv g gr ∧ capA st sd slot = .Thread t + swap + · rw [decide_eq_false hv] at hSV + have hdecOk : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (.Ok (), sd, ad) := by + rw [hdecBase, hnc] + simp only [tcb.install_tcb_cap, hdest, hdel0, bind_ok, uncurry_apply_pair, + core.result.Result.Insts.CoreOpsTry.branch, hnotnull, hSV, Bool.false_eq_true, ite_false] + refine ⟨.Ok (), sd, ad, hdecOk, x', stUpdA σ σ.curThread x' .restart, σd, hx', rfl, hset', Gd, Q, R, hw, Ac', Bf', + stack', Jd, Or.inl ⟨rfl, fun _ => ?_, Binv.1, Binv.2.1⟩⟩ + rw [hnc] + refine tcbSetTimeoutEndpoint_specPostA ?_ + by_cases hS : capA st sd src = .Endpoint p b sn rv g gr + · have hI : capA st sd slot ≠ .Thread t := fun h => hv ⟨hS, h⟩ + have hnT : ¬ fl4TcbSameObject (.threadCap (specPtr t)) cI := by + rcases hkeepI with h | h | ⟨zt, k, h⟩ + · exact absurd h hI + all_goals + rw [h] at hcI + rw [← CapEqA.eq_abs hcI (fun _ _ _ h => by cases h)] + simp [absCapA, fl4TcbSameObject, fl4TcbFrame, sameObjectAs, sameRegionAs, isEpCap, isThreadCap, objRefOf] + exact tcbSlotSkip_invA hspec hnSpec mS (by rw [eS hS]; exact hsameE) mI hnT + · have hnE : ¬ fl4TcbSameObject (absCapA (.Endpoint p b sn rv g gr)) cS := by + rcases hkeepS with h | h | ⟨zt, k, h⟩ + · exact absurd h hS + all_goals + rw [h] at hcS + rw [← CapEqA.eq_abs hcS (fun _ _ _ h => by cases h)] + simp [absCapA, fl4TcbSameObject, fl4TcbFrame, sameObjectAs, sameRegionAs, isEpCap, isThreadCap, objRefOf] + exact tcbSlotSkip_srcA hspec hnSpec mS hnE + rw [decide_eq_true hv] at hSV + obtain ⟨hS, hI⟩ := hv + have hS' : capA st sd (cslotAddrA κsrc) = .Endpoint p b sn rv g gr := by rw [hκsrc]; exact hS + have hP := Jd.j.1.place + have hsrcSlot : IsSlotA Z.th (cslotAddrA κsrc) := hAsrc.isSlot J.j.1.place + have hdOS : cslotAddrA sl ∈ OS := J.j5.2.1 _ (hAd.isSlot J.j.1.place) + have hwf : CapWfA (.Endpoint p b sn rv g gr) := by + have h := Jd.capWfA (y := cslotAddrA κsrc) hsrcSlot + rwa [hS'] at h + have hw' : CapRt.EncWf (.Endpoint p b sn rv g gr) := by + have h : CapRt.EncWf (capAtA (L.mem sd) (cslotAddrA κsrc)) := CapRt.decodeF_encWf _ _ + rwa [capAtA_eq (st := st), hS'] at h + have hno : ¬ IrqNodeA (L.ksb sd) (fl4Ptr sl.1) := by + change ¬ IrqNodeA (L.ksb sd) (fl4Ptr (specPtr t)) + rw [fl4Ptr_specPtr] + intro h + have := Jd.j.1.place.irqK hthT h + cases this + have hdecIns : ∀ s2, cdt.insert st sd (.Endpoint p b sn rv g gr) (cslotAddrA κsrc) (cslotAddrA sl) = ok s2 → + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (.Ok (), s2, ad) := fun s2 h2 => by + rw [hκsrc] at h2 + rw [hdecBase, hnc] + simp only [tcb.install_tcb_cap, hdest, hdel0, bind_ok, uncurry_apply_pair, + core.result.Result.Insts.CoreOpsTry.branch, hnotnull, hSV, h2, Bool.false_eq_true, ite_false, ite_true] + have hdecFin := fun (s3 : S) + (hd : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (.Ok (), s3, ad)) (σ3 : Fl4SpecA64.State Unit) => + J.decodeFinA hA hM hPl hfm hslot hcov (rfl : capA st s slot = capA st s slot) hext hSC (lbl := lbl) + (by rw [hcap, hlbl]; simp [DecArmS1aEnt, DecArmQH, DecArmDelQ]) + (fun _ h => by rw [hcap] at h; cases h) (fun _ h => by rw [hcap] at h; cases h) hd σ3 m + obtain ⟨s2, σ2, hs2, hm, hbk, -, J2⟩ := JStage3A.capInsertTcbSlotA (J := Jd) κsrc sl (ps := ps) ⟨hss1, hss2⟩ + (t0 := t) hthT (i := 4) (by decide) (by decide) (by decide) hsl + (by rw [hS']; simp) (by exact hnull) (J.delCanA _ hdOS) + (nc := .Endpoint p b sn rv g gr) (anc := absCapA (.Endpoint p b sn rv g gr)) (fun h => by cases h) hwf (CapEqA.of_abs rfl) hw' (by simp) + (by rw [hS']; exact .refl _) (by rw [hS']; exact UA.refl _) (by rw [hS']; trivial) (by rw [hS']) + (by rw [hS']) (by rw [hS']; exact Or.inl rfl) + (fun r b' rs h hb => by + rw [hS'] at h + rcases h with h | h <;> rw [h] <;> first | rfl | (simp [absCapA] at h)) + (fun i h => by rw [hS'] at h; cases h) (fun i h => by cases h) (fun i h => by cases h) + (fun q k j h => by rw [hS'] at h; cases h) (fun h => by rw [hS'] at h; cases h) (Or.inl hno) + (fun h => by rwa [hS'] at h) + ⟨fun _ _ h => by simp [absCapA] at h, fun _ h => by simp [absCapA] at h, + fun _ _ _ h => by simp [absCapA] at h⟩ + (fun s2 h2 => by + obtain ⟨-, -, -, hsym, -⟩ := hdecFin s2 (hdecIns s2 h2) σd + exact hsym) + (fun s2 h2 => by + obtain ⟨-, -, hsys, -⟩ := hdecFin s2 (hdecIns s2 h2) σd + exact hsys) + (fun s2 σ2 h2 _ => by + obtain ⟨-, -, -, -, -, -, j7⟩ := hdecFin s2 (hdecIns s2 h2) σ2 + exact j7) + have hdecOk := hdecIns s2 hs2 + have wk := (capInsert_skA _ _ _ σd ((), σ2) hm).toDWkA + have B2 := BodyOKA.stepA Binv (delStep_ofKeepA hbk wk.off wk.ctSc) + refine ⟨.Ok (), s2, ad, hdecOk, x', stUpdA σ σ.curThread x' .restart, σ2, hx', rfl, hset', _, Q, R, hw, Ac', Bf', + stack', J2, Or.inl ⟨rfl, fun _ => ?_, B2.1, B2.2.1⟩⟩ + rw [hnc] + refine tcbSetTimeoutEndpoint_specPostA ?_ + have eSa : absCapA (capA st sd (cslotAddrA κsrc)) = absCapA (.Endpoint p b sn rv g gr) := by rw [hS'] + exact tcbSlotInsert_specA hspec hnSpec mS (by rw [eS hS]; exact hsameE) mI (by rw [eI hI]; exact hsameT) hm + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3LiftTestClearA.lean b/proofs/Fl4ProofsA64/P3/Stage3LiftTestClearA.lean new file mode 100644 index 000000000..03980acf7 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3LiftTestClearA.lean @@ -0,0 +1,386 @@ +/- +P3 (aarch64, A64-P3 Lift 1b): **`ARMPageTestAndClearAccessed` at record level** (every frame cap): +`JSyscallA.testClearA`. A rejected invocation keeps the state (`test_clear_accessed_jA`); an accepted one is the +access-flag clear of the leaf (`JSyscallA.clearAFA`) and then the one-word reply (`JStage3A.replyOneA`) on the still +running caller. The spec restarts the caller first; its reply and resume then give the same state. + +Named premises: [P] `ArchKeepsA`, `ArchTotal`, `MaxSpi`, `ArchPlat`, `A64FrameMasks` (through `decodeFinA`). +-/ +import Fl4ProofsA64.P3.Stage3ClearAfA +import Fl4ProofsA64.P3.Stage3LiftGetAddrA +import Fl4ProofsA64.P3.Stage3LiftMachA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +set_option hygiene false in +local macro "tcaErr" : tactic => `(tactic| + (simp only [error.illegal, error.invalid_cap, bind_ok] at h + cases congrArg Prod.fst (ok_inj_ff h))) + +/-- **An accepted `decode_test_and_clear_accessed` run** is the walk to a leaf, its access-flag clear, and a one-word +reply `0` or `1`. -/ +theorem tca_shapeA {ar : machine.Arch A} {s s' : S} {a a' : A} {fp rt asid va : U64} {sz : capability.FrameSize} + {dev mp e call : Bool} + (h : vspace.decode_test_and_clear_accessed st ar s a (frCapA fp sz rt dev mp e asid va) call = + ok (.Ok (), s', a')) : + e = false ∧ mp = true ∧ sz ≠ .Huge ∧ asid ≠ 0#u64 ∧ ar.hw_access_flag a = ok true ∧ a' = a ∧ + ∃ (root k : U64) (lu : vspace.PtSlot) (v : U64), + vspace.frame_root st s asid = ok (.Ok (root, k)) ∧ + vspace.mapped_entry st s (frCapA fp sz rt dev mp e asid va) root = ok (some lu) ∧ + (v = 0#u64 ∨ v = 1#u64) ∧ + vspace.reply1 st (L.wr s lu.slot (L.mem s lu.slot &&& ~~~1024#u64)) (L.curW s) call v = ok s' := by + unfold vspace.decode_test_and_clear_accessed at h + cases e with + | true => unfold frCapA at h; simp only [↓reduceIte] at h; tcaErr + | false => + have hsz : sz ≠ .Huge := by + rintro rfl; unfold frCapA at h; simp only [Bool.false_eq_true, ↓reduceIte, bind_ok] at h; tcaErr + have h' : (if mp = true then + if asid = 0#u64 then do let r ← error.illegal Unit; ok (r, s, a) + else do + let b1 ← ar.hw_access_flag a + if b1 = true then do + let r ← vspace.frame_root st s asid + match r with + | core.result.Result.Ok p => + let (r1, _) := p + let o ← vspace.mapped_entry st s (frCapA fp sz rt dev mp false asid va) r1 + match o with + | none => do let r2 ← error.invalid_cap Unit 0#u64; ok (r2, s, a) + | some x => do + let ct ← sched.cur_thread st s + let i ← vspace.D_AF + let i1 ← lift (~~~i) + let (old, s1) ← st.fetch_and s x.slot i1 + let i2 ← lift (old &&& i) + if i2 != 0#u64 then do + let s2 ← vspace.reply1 st s1 ct call 1#u64 + ok (core.result.Result.Ok (), s2, a) + else do + let s2 ← vspace.reply1 st s1 ct call 0#u64 + ok (core.result.Result.Ok (), s2, a) + | core.result.Result.Err _ => do let r1 ← error.invalid_cap Unit 0#u64; ok (r1, s, a) + else do let r ← error.illegal Unit; ok (r, s, a) + else do let r ← error.illegal Unit; ok (r, s, a)) = ok (.Ok (), s', a') := by + cases sz with + | Huge => exact absurd rfl hsz + | Small => unfold frCapA at h ⊢; simp only [Bool.false_eq_true, ↓reduceIte, bind_ok] at h; exact h + | Large => unfold frCapA at h ⊢; simp only [Bool.false_eq_true, ↓reduceIte, bind_ok] at h; exact h + clear h + rename' h' => h + cases mp with + | false => simp only [Bool.false_eq_true, ↓reduceIte] at h; tcaErr + | true => + simp only [↓reduceIte] at h + by_cases h0 : asid = 0#u64 + · rw [if_pos h0] at h; tcaErr + rw [if_neg h0] at h + obtain ⟨b1, hb1, h⟩ := bind_eq_ok h + cases b1 with + | false => simp only [Bool.false_eq_true, ↓reduceIte] at h; tcaErr + | true => + simp only [↓reduceIte] at h + obtain ⟨r, hr, h⟩ := bind_eq_ok h + rcases r with ⟨root, k⟩ | f + swap + · tcaErr + dsimp only at h + obtain ⟨o, ho, h⟩ := bind_eq_ok h + rcases o with _ | lu + · tcaErr + dsimp only at h + rw [cur_thread_ok, bind_ok, d_af_valA, bind_ok] at h + simp only [lift, bind_ok, L.fetch_and_eq] at h + change (if ((L.mem s lu.slot &&& 1024#u64) != 0#u64) = true then do + let s2 ← vspace.reply1 st (L.wr s lu.slot (L.mem s lu.slot &&& ~~~1024#u64)) (L.curW s) call 1#u64 + ok (core.result.Result.Ok (), s2, a) + else do + let s2 ← vspace.reply1 st (L.wr s lu.slot (L.mem s lu.slot &&& ~~~1024#u64)) (L.curW s) call 0#u64 + ok (core.result.Result.Ok (), s2, a)) = ok (.Ok (), s', a') at h + have fin : ∀ v : U64, (do + let s2 ← vspace.reply1 st (L.wr s lu.slot (L.mem s lu.slot &&& ~~~1024#u64)) (L.curW s) call v + ok (core.result.Result.Ok (), s2, a)) = ok ((.Ok () : core.result.Result Unit error.Exc), s', a') → + a' = a ∧ vspace.reply1 st (L.wr s lu.slot (L.mem s lu.slot &&& ~~~1024#u64)) (L.curW s) call v = ok s' := + fun v hv => by + obtain ⟨s2, e2, hv⟩ := bind_eq_ok hv + have p := ok_inj_ff hv + simp only [Prod.mk.injEq, true_and] at p + exact ⟨p.2.symm, p.1 ▸ e2⟩ + have done : ∃ v : U64, (v = 0#u64 ∨ v = 1#u64) ∧ a' = a ∧ + vspace.reply1 st (L.wr s lu.slot (L.mem s lu.slot &&& ~~~1024#u64)) (L.curW s) call v = ok s' := by + split at h + · exact ⟨_, Or.inr rfl, fin _ h⟩ + · exact ⟨_, Or.inl rfl, fin _ h⟩ + obtain ⟨v, vv, ea, hv⟩ := done + exact ⟨rfl, rfl, hsz, h0, hb1, ea, root, k, lu, v, hr, ho, vv, hv⟩ + +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {ar : machine.Arch A} {nA : U64} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- The current thread's register and IPC words (`[256, 3328)`) are no VCPU `TCB` / `RUN_CALLER` word. -/ +theorem JStage3A.regsOffVcA {J5 J7 : JSlotA S} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (vh : VcH H Z.V) : + ∀ v ∈ Z.V, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → wa (L.curW s) K ≠ v ∧ wa (L.curW s) K ≠ wa v 24#u64 := by + intro v hv K _ hK + have hH := J.sys.ent.heap + have ht : H (L.curW s) = some .tcb := J.sq.curT + have htv : (wa (L.curW s) K).val = (L.curW s).val + K.val := + field_val (J.sq.tcbs _ J.sq.curT) (by omega) + have ne : v ≠ L.curW s := fun e => by + have := vh v hv + rw [e, ht] at this + cases this + have hd := hH.disj v _ _ _ (vh v hv) ht ne + simp only [HKind.bits, Nat.reducePow] at hd + refine ⟨fun e => ?_, fun e => ?_⟩ + · have := congrArg UScalar.val e + rw [htv] at this + omega + · have := congrArg UScalar.val e + rw [htv, vcw_valA hH (vh v hv) (by simp)] at this + have e24 : (24#u64 : U64).val = 24 := rfl + omega + +/-- **The leaf an accepted walk reaches**, in the relation's sets, with the spec decoder's result there. -/ +theorem JEntA.tcaLeafA + (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + (cte : Fl4SpecA64.CslotPtr) (hab : AbsSlotA Z.th cte) + (fp rt asid va : U64) (sz : capability.FrameSize) (dev : Bool) + (hcap : capAtA (L.mem s) (cslotAddrA cte) = .Frame fp sz rt dev true false asid va) + (hsz : sz ≠ .Huge) (h0 : asid ≠ 0#u64) (h16 : asid.val < 65536) + (hhw : Fl4SpecA64.fl4A64Platform.hwAccessFlag = true) + {root k : U64} {lu : vspace.PtSlot} + (hfr : vspace.frame_root st s asid = ok (.Ok (root, k))) + (hme : vspace.mapped_entry st s (frCapA fp sz rt dev true false asid va) root = ok (some lu)) + (call : Bool) (args : List Fl4SpecA64.Data) (xSlot : Fl4SpecA64.CapRef) + (exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) : + (∃ P j, (P ∈ Z.S1 ∨ ∃ y ∈ Z.Y, P = wa y 4096#u64) ∧ j.val < 512 ∧ lu.slot = peAF P j) ∧ + (Fl4SpecA64.fl4ArchDecodeInvocation (Z := Unit) call (.ext .armPageTestAndClearAccessed) args xSlot cte + (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev (some (asidOfA asid, va.bv))) exs).run σ = + ({(.ok (.testAndClearAccessed (specPtr fp)), σ)}, False) := by + have ha0 : asidOfA asid ≠ 0 := asidOf_ne0A h16 h0 + have hcond : ¬ (frameSizeOfA sz = .armHugePage ∨ asidOfA asid = 0 ∨ + Fl4SpecA64.fl4A64Platform.hwAccessFlag = false) := by + rintro (h | h | h) + · cases sz <;> first | exact absurd rfl hsz | cases h + · exact ha0 h + · rw [hhw] at h; cases h + obtain ⟨r, hfind, hrel⟩ := spec_ex_fd (find_asid_rawA (st := st) E.j.1.vs h16) + rcases r with w | lf + swap + · rw [frame_root_errA hfind] at hfr + cases ok_inj_ff hfr + obtain ⟨hv, hkind⟩ := rawKindE E.j.1.vs E.j.2.2.1.obj hrel + have hv' : Fl4SpecA64.vspaceForAsid (asidOfA asid) σ = some (specPtr (untagA w)) := hv + rcases hkind with ⟨-, hk⟩ | ⟨ht1, -, -⟩ | ⟨ht2, hyY, hk⟩ + · have hS1 : ∀ r, Fl4SpecA64.vspaceForAsid (asidOfA asid) σ = some r → Fl4SpecA64.rootKindAt σ r = .stage1 := + fun r hr => by rw [hv'] at hr; cases hr; exact hk + obtain ⟨htag, hwS1, hw0, hv1⟩ := raw_s1A E.j.1.vs E.j.2.2.1.obj hS1 hrel + have er : root = w := by + rw [frame_root_s1A hfind htag] at hfr + have p := ok_inj_ff hfr + simp only [core.result.Result.Ok.injEq, Prod.mk.injEq] at p + exact p.1.symm + rw [er] at hme + have hreachW := vsReachA_of_vspaceForAsid E.j.1.vs E.j.2.2.1.obj hwS1 hv1 + obtain ⟨P, j, hPS1, hj, hsl⟩ := mapped_entry_s1_slotA E.j.1.vs hwS1 hw0 hreachW _ hme + refine ⟨⟨P, j, Or.inl hPS1, hj, hsl⟩, ?_⟩ + have hmeo := post_of_okA (mapped_entry_s1A E.j.1.vs hwS1 hw0 hreachW (frCapA fp sz rt dev true false asid va)) hme + obtain ⟨pte, hlf, hbase, -⟩ := hmeo + have hspecF : (Fl4SpecA64.findVspaceForAsid (Z := Unit) (asidOfA asid)).run σ = + ({(.ok (specPtr w), σ)}, False) := findVs_runA hv1 (hS1 _ hv1) + have hkk : Fl4SpecA64.asidRootKind σ (asidOfA asid) = some .stage1 := by + unfold Fl4SpecA64.asidRootKind; rw [hv']; simp [hk] + rw [dec_testClearS1A call _ xSlot cte exs _ _ _ _ _ (fun _ _ h => by cases h; exact Or.inr hkk)] + refine run_liftME_okA ?_ + unfold Fl4SpecA64.decodeTestAndClearAccessed + dsimp only + rw [if_neg hcond, run_bind_okD (run_handleE_okA hspecF), + run_bind_okD (liftE_runA (m := Fl4Spec.NonDet.gets _) rfl), lookupFrame_of_pteA, hlf] + dsimp only [Option.map] + rw [if_pos ⟨rfl, hbase⟩]; rfl + · rw [frame_root_s2A hfind ht1] at hfr + cases ok_inj_ff hfr + · have er : root = untagA w := by + rw [frame_root_hiA hfind ht2] at hfr + have p := ok_inj_ff hfr + simp only [core.result.Result.Ok.injEq, Prod.mk.injEq] at p + exact p.1.symm + rw [er] at hme + have hk0 : Fl4SpecA64.asidRootKind σ (asidOfA asid) = some .high := by + unfold Fl4SpecA64.asidRootKind; rw [hv']; simp [hk] + have hva : VaOkE va := E.j.1.vaOkE (hab.isSlot E.j.1.place) hcap + have hactY := activeHighRootA_of_vspaceForAsid E.j.1.vs E.j.2.2.1.obj hyY hv' + obtain ⟨P, j, hPY, hj, hsl⟩ := mapped_entry_hi_slotE E.j.1.vs E.j.1.ext hyY hactY _ hva hme + refine ⟨⟨P, j, hPY, hj, hsl⟩, ?_⟩ + have hmeo := post_of_okA (mapped_entry_hiE E.j.1.vs E.j.1.ext hyY hactY (frCapA fp sz rt dev true false asid va) + hva) hme + obtain ⟨pos, pte, hlf, -⟩ := hmeo + have hspecF : (Fl4SpecA64.findExtRoot (Z := Unit) .high (asidOfA asid)).run σ = + ({(.ok (specPtr (untagA w)), σ)}, False) := by + rw [findExtRoot_runX]; simp [extRootResX, hv', hk0, hk] + rw [dec_testClearExtA call _ xSlot cte exs _ _ _ _ _ _ .high hk0 (by decide)] + refine run_liftME_okA ?_ + unfold Fl4SpecA64.decodeExtTestAndClearAccessed + dsimp only + rw [if_neg hcond, run_bind_okD (run_handleE_okA hspecF), + run_bind_okD (liftE_runA (m := Fl4Spec.NonDet.gets _) rfl), hlf] + rfl + +set_option maxHeartbeats 2000000 in +/-- **`ARMPageTestAndClearAccessed` at record level** (module docstring). -/ +theorem JSyscallA.testClearA (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) + s a σ m) + (hfm : A64FrameMasks st s) (K : ArchKeepsA ar AR) + (cte : Fl4SpecA64.CslotPtr) (hab : AbsSlotA Z.th cte) (hcov : SlotCov st OS s (cslotAddrA cte)) + (fp rt asid va : U64) (sz : capability.FrameSize) (dev mp e : Bool) + (hcap : capA st s (cslotAddrA cte) = .Frame fp sz rt dev mp e asid va) + (len buf : U64) (blk call don fpb : Bool) + (extra : Aeneas.Std.Array obj.Slot 3#usize) (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) + (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (args : List U64) (xSlot : Fl4SpecA64.CapRef) (exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) : + syscall.decode_invocation st ar s a fl4_abi.label.ARMPageTestAndClearAccessed len (cslotAddrA cte) + (.Frame fp sz rt dev mp e asid va) blk call don fpb buf extra + ⦃ r => + let D := (fl4ArchDecodeInvocation (Z := Unit) call (.ext .armPageTestAndClearAccessed) + (args.map UScalar.bv) xSlot cte + (absFrameE e (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev (mapDataA mp asid va)) exs).run σ + (r.2 = (s, a) ∧ ∃ e', D = ({(.error e', σ)}, False) ∧ ∃ ex, r.1 = .Err ex ∧ VsErrRelA ex e') ∨ + (D = ({(.ok (.testAndClearAccessed (specPtr fp)), σ)}, False) ∧ + ReplyOutA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar hPl.ipa m σ call + (.testAndClearAccessed (specPtr fp)) r) ⦄ := by + have J3 := J.toJStage3A + have hsl : cslotAddrA cte ∈ OS := J.j5.2.1 _ (hab.isSlot J.j.1.place) + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J3.rsSiteA hext hsl hcov J.actW J.running J.cur + obtain ⟨Pt, Ub, Pp7, Tb7, Hr, Te, Kt, Lv7, Re, bits, KLv, W⟩ := J7A.unp J.j7 + have Je := J3.toJEntA + have hTr : TreeA st OS Z.G.chains Z.G.par s := + J.j.1.cdt.tree.grow J.j5.2.1 (fun x hx hn => J.j5.2.2 x hx hn) + have hprog : syscall.decode_invocation st ar s a fl4_abi.label.ARMPageTestAndClearAccessed len (cslotAddrA cte) + (.Frame fp sz rt dev mp e asid va) blk call don fpb buf extra = + vspace.decode_test_and_clear_accessed st ar s a (frCapA fp sz rt dev mp e asid va) call := by + rw [decInv_frameA, decode_frame_testClearA] + obtain ⟨res, eres, post⟩ := spec_ex_fd (test_clear_accessed_jA K Je RS (J3.regsOffVcA W.col.vh) J3.tabS1 J3.tabExt + hTr cte hab fp rt asid va sz dev mp e hcap call args xSlot exs) + rw [hprog, eres, WP.spec_ok] + obtain ⟨r, s', a'⟩ := res + cases r with + | Err err => + obtain ⟨es, ea, e', hD, hrel⟩ := post + exact Or.inl ⟨by rw [es, ea], e', hD, err, rfl, hrel⟩ + | Ok u => + clear post + obtain ⟨rfl, rfl, hsz, h0, hb, ea, root, k, lu, v, hfr, hme, hv01, hrep⟩ := tca_shapeA eres + have hhw : Fl4SpecA64.fl4A64Platform.hwAccessFlag = true := by + rw [K.hwAccessFlag a] at hb; exact ok_inj_ff hb + have h16 : asid.val < 65536 := by + obtain ⟨c0, -, hrel0⟩ := StateRelA.getAbsA J.j.1.st hab + rw [show capAtA (L.mem s) (cslotAddrA cte) = .Frame fp sz rt dev true false asid va from hcap] at hrel0 + obtain ⟨⟨-, -, ⟨h16a, -, -, -⟩, -⟩, -⟩ := hrel0 + exact lt_of_lt_of_eq h16a (by norm_num) + obtain ⟨⟨P, j, hP, hj, hlu⟩, hD⟩ := Je.tcaLeafA cte hab fp rt asid va sz dev hcap hsz h0 h16 hhw hfr hme call + (args.map UScalar.bv) xSlot exs + refine Or.inr ⟨hD, rfl, ?_⟩ + obtain ⟨J1, hcur1, hsc1, hiq1⟩ := J.clearAFA RS hfr hme hP hj hlu + rw [hlu] at hrep + set s1 := L.wr s (peAF P j) (L.mem s (peAF P j) &&& ~~~1024#u64) with hs1 + -- the caller's TCB and the spec restart + obtain ⟨Rq, Nt, extra', sp, Pl⟩ := RS.pl + obtain ⟨x, hx, -, hset, -, -, -, -⟩ := JStage3A.of_restartCurA J3 Pl RS.sym RS.run + J.toJStage3EntryA.curNotVmA RS.cur RS.vs RS.vc + have ct : σ.curThread = specPtr (L.curW s) := J.j.1.sch.curThread + have htZ : L.curW s ∈ Z.T := J.sr.curT + have hk : σ.kheap (specPtr (L.curW s)) = some (.tcb x) := ct ▸ hx + have hrun : x.tcbState = .restart ∨ x.tcbState = .running := by + have code : (L.mem s (stA (L.curW s))).val = tsCodeA x.tcbState := (J.ep.at_tcb htZ hk).st + have hr := (runnable_iffA code).1 J.running + have nvm := J.toJStage3EntryA.curNotVmA + have h8 : (sched.TS_RUNNING_VM : U64).val = 8 := by unfold sched.TS_RUNNING_VM; rfl + cases e : x.tcbState with + | running => exact Or.inr rfl + | restart => exact Or.inl rfl + | ext q => + cases q + rw [e] at code + exact absurd (UScalar.eq_of_val_eq (code.trans h8.symm)) nvm + | _ => rw [e] at hr; cases hr + have hrq : σ.curThread ∉ σ.releaseQueue := by + rw [ct, J.j.1.rest.rel]; intro hm + obtain ⟨q, hq, eq⟩ := List.mem_map.1 hm + have e' : q = L.curW s := specPtr_inj eq + subst e' + have haf := J.sq.inR _ J.sq.cur _ hq + exact ((J.sq.released _ (by rw [J.tcbs]; exact htZ)).2 (by rw [haf]; exact hq)) RS.cur.1 + have R := J1.replyOneA (ar := ar) (nA := hPl.ipa) hx hrun (by rw [hcur1, hsc1]; exact RS.cur.2.1) + (by rw [hcur1, hsc1]; exact RS.cur.2.2) (by rw [hcur1, hiq1]; exact RS.cur.1) hrq call v + (fun s2 h2 σX => by + rw [hcur1, hrep] at h2 + have e2 := ok_inj_ff h2 + have hd : syscall.decode_invocation st ar s a fl4_abi.label.ARMPageTestAndClearAccessed len (cslotAddrA cte) + (.Frame fp sz rt dev true false asid va) blk call don fpb buf extra = ok (core.result.Result.Ok (), s2, a) := by + rw [hprog, eres, ea, ← e2] + obtain ⟨-, Ac', sysl, symL, -, userOK, j7⟩ := J.decodeFinA hA hM hPl hfm hsl hcov hcap hext hSC + (lbl := fl4_abi.label.ARMPageTestAndClearAccessed) (by simp [DecArmS1aEnt, DecArmQH]) + (fun _ h => by cases h) (fun _ h => by cases h) hd σX m + exact ⟨sysl, symL, userOK, j7⟩) + rw [hcur1, hrep] at R + rw [WP.spec_ok] at R + obtain ⟨σ3, hrunS, hshape, Q, Rl, J2⟩ := R + refine ⟨x, stUpdA σ σ.curThread x .restart, σ3, v.bv, hx, hset, ?_, ?_, hshape, Q, Rl, ea ▸ J2⟩ + · refine mem_liftED ?_ + rcases hv01 with rfl | rfl <;> simp [Fl4SpecA64.performTestAndClearAccessed, Fl4Spec.NonDet.select] + · -- the reply from the restarted caller reaches the same state + have hscx : x.tcbSchedContext = some (scRenA m (L.mem s (scA (L.curW s)))) := by + obtain ⟨y, hy, -, hsx⟩ := J.j.1.sch.tcb _ htZ + rw [hk] at hy; cases hy + rw [hsx]; unfold optScA; rw [if_neg RS.cur.2.1] + have hwC : L.mem s (scA (L.curW s)) ∈ Z.C := J.j.1.rest.scMem _ htZ RS.cur.2.1 + obtain ⟨y, k', ey, hpos⟩ : ∃ y k', σ.kheap (scRenA m (L.mem s (scA (L.curW s)))) = some (.schedContext y k') ∧ + 0 < y.scRefillMax := by + obtain ⟨y, k', ey, eyr⟩ := J.j.1.rest.refillMax _ hwC + refine ⟨y, k', ey, ?_⟩ + have hwH : L.mem s (scA (L.curW s)) ∈ H.scs := by rw [J.scs]; exact hwC + have hrm : L.rmax s (L.mem s (scA (L.curW s))) ≠ 0#u64 := (J.sys.ent.bxi.2.2.2.2.act _ hwH).1 RS.cur.2.2 + rw [eyr] + have e3 : L.rmax s (L.mem s (scA (L.curW s))) = L.mem s (wa (L.mem s (scA (L.curW s))) 72#u64) := by + unfold LawfulStore.rmax; rw [maxA_eqA] + rw [e3] at hrm + exact Nat.pos_of_ne_zero (fun e => hrm (UScalar.eq_of_val_eq (by simpa using e))) + have hne : scRenA m (L.mem s (scA (L.curW s))) ≠ σ.curThread := fun e => by + rw [e, hx] at ey; cases ey + have h0' := replyRun_oneA hx call v.bv fun uc' _ _ => by + refine schedulable_ofA (x := { x with tcbArch := { x.tcbArch with tcbContext := uc' }, tcbState := .running }) + (n := k') (by simp [setObjA]) rfl hscx (y := y) ?_ hpos hrq + simpa [setObjA, Function.update_of_ne hne] using ey + have hx1 : (stUpdA σ σ.curThread x .restart).kheap σ.curThread = some (.tcb { x with tcbState := .restart }) := by + simp [stUpdA, setObjA] + have h1' := replyRun_oneA hx1 call v.bv fun uc' _ _ => by + refine schedulable_ofA (x := { { x with tcbState := .restart } with + tcbArch := { x.tcbArch with tcbContext := uc' }, tcbState := .running }) + (n := k') (by simp [setObjA]) rfl hscx (y := y) ?_ hpos (by simpa [stUpdA, setObjA] using hrq) + simpa [stUpdA, setObjA, Function.update_of_ne hne] using ey + rw [h0'] at hrunS + simp only [Set.mem_singleton_iff, Prod.mk.injEq, true_and] at hrunS + rw [h1'] + simp only [Set.mem_singleton_iff, Prod.mk.injEq, true_and] + rw [hrunS] + simp only [stUpdA, ctxUpdA, setObjA, Function.update_idem, replyCtxA] + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3MapAlikeA.lean b/proofs/Fl4ProofsA64/P3/Stage3MapAlikeA.lean new file mode 100644 index 000000000..ca5dec2c4 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3MapAlikeA.lean @@ -0,0 +1,360 @@ +/- +P3 (AArch64, merge line, D8 step 5 `of_capFrame`, part (i)): **the in-place single-slot cap class `MapAlikeA`** and +one generic keep lemma per cap-reading field of `JStage3A`. + +An in-place write (frame map / unmap, page-table map / unmap, ASID assign, frame-cap update) replaces the cap at one +slot by a cap that differs only in the mapping data of a `Frame` / `PageTable` / `Pml4` / `Ept` / `HighVSpace` cap +(`MapAlikeA d c`: `d` for `c`; the object pointer, the size and, for a frame, the rights and device flag are kept). +Every other cap kind is *stable* (`StableCapA`): `d = e ↔ c = e` for a stable `e`. The fields that read only stable +caps (untyped, zombie, VCPU, reply, pool, thread namers) or only the pointer / shape are kept with one lemma each, +parameterised by `hM : ∀ u ∈ OS, MapAlikeA (capA s' u) (capA s u)`: + +`ObjAllocA`, `IdleAnchorOffA`, `UtBlk39A`, `UtZombieA`, `ZombieTcbTA`, `ZombieTcbNoVcpuA`, `DevUtOffA`, +`FrBaseApartA`, `DevFrDisjA`, `SpecZeroFreeA`, `SpecFreeA`, `DeadExtA`; the MDB part of J5 (`MdbInvA` by shape). +The fields that read the mapped flag (`UnmPtSpecA`, `SchedPageA`, J7) are per arm. + +Also `setCap_khA`: `set_cap` changes only `kheap` (so the CDT, the original-cap map and the machine state are kept). +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3CapSlotA +import Fl4ProofsA64.P3.CnodeCopyA +import Fl4ProofsA64.P3.SpecFrA +import Fl4ProofsA64.P1.MdbPtCapA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add +local notation "shpA" => Fl4ProofsA64.P1.W7.R.ST.shapeA + +/-- **An in-place cap update** (module docstring): `MapAlikeA d c` is `d` for `c`. -/ +inductive MapAlikeA : capability.Cap → capability.Cap → Prop + | refl (c : capability.Cap) : MapAlikeA c c + | frame (p : U64) (sz : capability.FrameSize) (r : U64) (dv m e : Bool) (a v : U64) (m' e' : Bool) (a' v' : U64) : + MapAlikeA (.Frame p sz r dv m' e' a' v') (.Frame p sz r dv m e a v) + | pt (p : U64) (m : Bool) (a v : U64) (m' : Bool) (a' v' : U64) : + MapAlikeA (.PageTable p m' a' v') (.PageTable p m a v) + | pml4 (p : U64) (m : Bool) (a : U64) (m' : Bool) (a' : U64) : MapAlikeA (.Pml4 p m' a') (.Pml4 p m a) + | ept (p l : U64) (m : Bool) (a v : U64) (m' : Bool) (a' v' : U64) : + MapAlikeA (.Ept p l m' a' v') (.Ept p l m a v) + | hv (p : U64) (m : Bool) (a : U64) (m' : Bool) (a' : U64) : MapAlikeA (.HighVSpace p m' a') (.HighVSpace p m a) + +/-- `BufPinA` across a step that keeps every `OS` cap up to its mapping data. -/ +theorem BufPinA.keepM {S : Type} {st : machine.Store S} [L : Fl4ProofsA64.P1.LawfulStore st] {T OS : Set U64} + {s s' : S} {b : U64} (h : BufPinA st T OS s b) + (hk : ∀ u ∈ OS, MapAlikeA (Fl4ProofsA64.P1.W7.R.ST.capA st s' u) (Fl4ProofsA64.P1.W7.R.ST.capA st s u)) : + BufPinA st T OS s' b := + h.keepF fun t ht hO ptr size rights x1 x2 x3 x4 e => ⟨ht, hO, by + have m := hk _ hO + rw [e] at m + generalize Fl4ProofsA64.P1.W7.R.ST.capA st s' (sbWordA t) = c at m ⊢ + cases m <;> exact ⟨_, _, _, _, _, rfl⟩⟩ + +/-- The cap kinds an in-place update may change. -/ +def StableCapA : capability.Cap → Prop + | .Frame .. => False + | .PageTable .. => False + | .Pml4 .. => False + | .Ept .. => False + | .HighVSpace .. => False + | _ => True + +section alike +variable {d c : capability.Cap} + +theorem MapAlikeA.shape (h : MapAlikeA d c) : shpA d = shpA c := by cases h <;> rfl + +theorem MapAlikeA.shape3 (h : MapAlikeA d c) : Fl4ProofsA64.P3.shapeA d = Fl4ProofsA64.P3.shapeA c := by cases h <;> rfl + +theorem MapAlikeA.isFr (h : MapAlikeA d c) : isFrA d = isFrA c := isFrA_of_shape h.shape + +theorem MapAlikeA.ua (h : MapAlikeA d c) : UA d c := + UA.of_shapeA h.shape3 fun hu => by cases h <;> simp_all [isUtA] + +theorem MapAlikeA.back_eq (h : MapAlikeA d c) {e : capability.Cap} (he : StableCapA e) (hd : d = e) : c = e := by + cases h with + | refl => exact hd + | _ => subst hd; exact he.elim + +theorem MapAlikeA.fwd_eq (h : MapAlikeA d c) {e : capability.Cap} (he : StableCapA e) (hc : c = e) : d = e := by + cases h with + | refl => exact hc + | _ => subst hc; exact he.elim + +/-- `CapTyA` across in-place cap writes: the four pointing kinds are untouched. -/ +theorem CapKindA.mapA {H : Heap} (h : MapAlikeA d c) (k : CapKindA H c) : CapKindA H d := + ⟨fun p b x y z w e => k.1 p b x y z w (h.back_eq (e := .Endpoint p b x y z w) trivial e), + fun p b x y e => k.2.1 p b x y (h.back_eq (e := .Notification p b x y) trivial e), + fun p g e => k.2.2.1 p g (h.back_eq (e := .Reply p g) trivial e), + fun p sz e => k.2.2.2.1 p sz (h.back_eq (e := .SchedContext p sz) trivial e), + fun cpu e => k.2.2.2.2 cpu (h.back_eq (e := .SchedControl cpu) trivial e)⟩ + +theorem CapTyA.mapA {S : Type} {st : machine.Store S} [LawfulStore st] {H : Heap} {OS : Set U64} {s s' : S} + (hM : ∀ u ∈ OS, MapAlikeA (capA st s' u) (capA st s u)) (h : CapTyA st H OS s) : CapTyA st H OS s' := + fun u hu => CapKindA.mapA (hM u hu) (h u hu) + +theorem MapAlikeA.frame_back (h : MapAlikeA d c) {p : U64} {sz : capability.FrameSize} {r : U64} {dv m e : Bool} + {a v : U64} (hd : d = .Frame p sz r dv m e a v) : ∃ m0 e0 a0 v0, c = .Frame p sz r dv m0 e0 a0 v0 := by + cases h with + | refl => exact ⟨m, e, a, v, hd⟩ + | frame p1 sz1 r1 dv1 m1 e1 a1 v1 m2 e2 a2 v2 => + injection hd with h1 h2 h3 h4 h5 h6 h7 h8 + subst h1 h2 h3 h4 + exact ⟨m1, e1, a1, v1, rfl⟩ + | _ => cases hd + +/-- P1's in-place mapping-metadata update (`CapMeta`) is one of ours. -/ +theorem MapAlikeA.of_meta (h : CapMeta d c) : MapAlikeA d c := by + rcases h with ⟨p, sz, r, dv, m, e, a, v, m', e', a', v', rfl, rfl⟩ | ⟨p, m, a, v, m', a', v', rfl, rfl⟩ | + ⟨p, m, a, m', a', rfl, rfl⟩ | ⟨p, m, a, m', a', rfl, rfl⟩ | ⟨p, l, m, a, v, m', a', v', rfl, rfl⟩ + · exact .frame p sz r dv m e a v m' e' a' v' + · exact .pt p m a v m' a' v' + · exact .pml4 p m a m' a' + · exact .hv p m a m' a' + · exact .ept p l m a v m' a' v' + +/-- ... and conversely a `MapAlikeA` cap is the source or a `CapMeta` update of it (P1's `CapSrcClosed.remap`). -/ +theorem MapAlikeA.toMeta (h : MapAlikeA d c) : d = c ∨ CapMeta d c := by + cases h with + | refl => exact Or.inl rfl + | frame p sz r dv m e a v m' e' a' v' => exact Or.inr (Or.inl ⟨p, sz, r, dv, m, e, a, v, m', e', a', v', rfl, rfl⟩) + | pt p m a v m' a' v' => exact Or.inr (Or.inr (Or.inl ⟨p, m, a, v, m', a', v', rfl, rfl⟩)) + | pml4 p m a m' a' => exact Or.inr (Or.inr (Or.inr (Or.inl ⟨p, m, a, m', a', rfl, rfl⟩))) + | hv p m a m' a' => exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨p, m, a, m', a', rfl, rfl⟩)))) + | ept p l m a v m' a' v' => exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ⟨p, l, m, a, v, m', a', v', rfl, rfl⟩)))) + +/-- A class closed under P1's cap producers is closed under in-place updates. -/ +theorem MapAlikeA.closed {Q : capability.Cap → Prop} (hQ : CapSrcClosed Q) (h : MapAlikeA d c) (hc : Q c) : Q d := by + rcases h.toMeta with rfl | hm + · exact hc + · exact hQ.remap hm hc + +theorem MapAlikeA.badge (h : MapAlikeA d c) : badgeA d = badgeA c := by cases h <;> rfl + +theorem MapAlikeA.rngKey (h : MapAlikeA d c) : RngKeyA d ↔ RngKeyA c := by + have u := h.ua + unfold RngKeyA + rw [u.phys, u.isut, h.isFr] + +end alike + +section keep +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {OS : Set U64} {s s' : S} + +theorem UtFreeA.mapBack {dev : Bool → Prop} {a : Nat} (hM : ∀ u ∈ OS, MapAlikeA (capA st s' u) (capA st s u)) + (hf : UtFreeA st OS s' dev a) : UtFreeA st OS s dev a := by + obtain ⟨u, hu, q, b, fi, d, hc, hd, h1, h2⟩ := hf + exact ⟨u, hu, q, b, fi, d, (hM u hu).back_eq (e := .Untyped q b fi d) trivial hc, hd, h1, h2⟩ + +theorem ObjAllocA.mapA {Z Z' : XSetsA} {H : Heap} (hM : ∀ u ∈ OS, MapAlikeA (capA st s' u) (capA st s u)) + (hJ : ∀ p bytes, JointObjectA Z' H p bytes → JointObjectA Z H p bytes) (o : ObjAllocA (st := st) Z H OS s) : + ObjAllocA (st := st) Z' H OS s' := fun p bytes hp u hu q b fi d hc => + o p bytes (hJ p bytes hp) u hu q b fi d ((hM u hu).back_eq (e := .Untyped q b fi d) trivial hc) + +theorem IdleAnchorOffA.mapA (hM : ∀ u ∈ OS, MapAlikeA (capA st s' u) (capA st s u)) + (o : IdleAnchorOffA (st := st) OS s) : IdleAnchorOffA (st := st) OS s' := fun u hu q b fi d hc => + o u hu q b fi d ((hM u hu).back_eq (e := .Untyped q b fi d) trivial hc) + +theorem UtBlk39A.mapA (hM : ∀ u ∈ OS, MapAlikeA (capA st s' u) (capA st s u)) (o : UtBlk39A st OS s) : + UtBlk39A st OS s' := fun u hu q b fi d hc => o u hu q b fi d ((hM u hu).back_eq (e := .Untyped q b fi d) trivial hc) + +theorem UtWinCapA.mapA (hM : ∀ u ∈ OS, MapAlikeA (capA st s' u) (capA st s u)) (o : UtWinCapA st OS s) : + UtWinCapA st OS s' := fun u hu q b fi hc => o u hu q b fi ((hM u hu).back_eq (e := .Untyped q b fi false) trivial hc) + +theorem UtZombieA.mapA (hM : ∀ u ∈ OS, MapAlikeA (capA st s' u) (capA st s u)) (o : UtZombieA st OS s) : + UtZombieA st OS s' := fun u hu p t n hc => o u hu p t n ((hM u hu).back_eq (e := .Zombie p t n) trivial hc) + +theorem ZombieTcbTA.mapA {H : Heap} (hM : ∀ u ∈ OS, MapAlikeA (capA st s' u) (capA st s u)) + (o : ZombieTcbTA st H OS s) : ZombieTcbTA st H OS s' := fun u hu p n hc => + o u hu p n ((hM u hu).back_eq (e := .Zombie p capability.ZOMBIE_TCB n) trivial hc) + +theorem ZombieTcbNoVcpuA.mapA {H : Heap} (hM : ∀ u ∈ OS, MapAlikeA (capA st s' u) (capA st s u)) + (hT : ZombieTcbTA st H OS s) (w : ∀ t ∈ H.tcbs, L.mem s' (wa t 3664#u64) = L.mem s (wa t 3664#u64)) + (o : ZombieTcbNoVcpuA st OS s) : ZombieTcbNoVcpuA st OS s' := fun u hu p n hc => by + have hc0 := (hM u hu).back_eq (e := .Zombie p capability.ZOMBIE_TCB n) trivial hc + rw [w p (hT u hu p n hc0)] + exact o u hu p n hc0 + +theorem DevUtOffA.mapA {Z Z' : XSetsA} {H : Heap} {ia ie : Nat} (hM : ∀ u ∈ OS, MapAlikeA (capA st s' u) (capA st s u)) + (hk : L.ksb s' = L.ksb s) (eJ : ∀ p bytes, JointObjectA Z' H p bytes → JointObjectA Z H p bytes) + (eC : Z'.Cs = Z.Cs) (h : DevUtOffA st Z H OS ia ie s) : DevUtOffA st Z' H OS ia ie s' := by + have bk : ∀ u ∈ OS, ∀ q b fi d, capA st s' u = .Untyped q b fi d → capA st s u = .Untyped q b fi d := + fun u hu q b fi d hc => (hM u hu).back_eq (e := .Untyped q b fi d) trivial hc + refine ⟨fun u hu q b fi hc p bytes hj => h.obj u hu q b fi (bk u hu q b fi true hc) p bytes (eJ p bytes hj), + fun u hu q b fi hc => ?_, fun u hu q b fi hc => h.idle u hu q b fi (bk u hu q b fi true hc), + fun u hu q b fi hc v hv q' b' fi' hc' => + h.sep u hu q b fi (bk u hu q b fi true hc) v hv q' b' fi' (bk v hv q' b' fi' false hc')⟩ + have hnb : ∀ c, L.nodeBase s' c = L.nodeBase s c := fun c => by simp [LawfulStore.nodeBase, hk] + have r := h.ks u hu q b fi (bk u hu q b fi true hc) + unfold RegionOffNodes at r ⊢ + rw [eC, hk] + exact ⟨fun c hcs => by rw [hnb c]; exact r.1 c hcs, r.2⟩ + +theorem FrBaseApartA.mapA (hM : ∀ u ∈ OS, MapAlikeA (capA st s' u) (capA st s u)) (h : FrBaseApartA st OS s) : + FrBaseApartA st OS s' := fun y hy z hz fy kz => by + have my := hM y hy + have mz := hM z hz + rw [my.isFr] at fy + rw [mz.ua.ptr, my.ua.ptr] + exact h y hy z hz fy (mz.rngKey.1 kz) + +theorem DevWinCapA.mapA (hM : ∀ u ∈ OS, MapAlikeA (capA st s' u) (capA st s u)) (o : DevWinCapA st OS s) : + DevWinCapA st OS s' := fun u hu => + ⟨fun p sb fi hc => (o u hu).1 p sb fi ((hM u hu).back_eq (e := .Untyped p sb fi true) trivial hc), + fun p sz r m e a v hc bits hb => by + obtain ⟨m0, e0, a0, v0, hc0⟩ := (hM u hu).frame_back hc + exact (o u hu).2 p sz r m0 e0 a0 v0 hc0 bits hb⟩ + +theorem DevFrDisjA.mapA {H : Heap} {σ σ' : Fl4SpecA64.State Unit} (hM : ∀ u ∈ OS, MapAlikeA (capA st s' u) (capA st s u)) + (dp : ∀ p dev pg, σ.kheap p = some (.archObj (.dataPage dev pg)) → σ'.kheap p = some (.archObj (.dataPage dev pg))) + (h : DevFrDisjA st H OS s σ) : DevFrDisjA st H OS s' σ' := by + intro y hy p size rights mapped ept asid va hc k hk + obtain ⟨m0, e0, a0, v0, hc0⟩ := (hM y hy).frame_back hc + obtain ⟨h1, dev, pg, h2⟩ := h y hy p size rights m0 e0 a0 v0 hc0 k hk + exact ⟨h1, dev, pg, dp _ _ _ h2⟩ + +theorem UserFrSpecA.mapA {σ σ' : Fl4SpecA64.State Unit} (hM : ∀ u ∈ OS, MapAlikeA (capA st s' u) (capA st s u)) + (dp : ∀ p dev pg, σ.kheap p = some (.archObj (.dataPage dev pg)) → σ'.kheap p = some (.archObj (.dataPage dev pg))) + (h : UserFrSpecA st OS s σ) : UserFrSpecA st OS s' σ' := by + intro y hy p size rights mapped ept asid va hc + obtain ⟨m0, e0, a0, v0, hc0⟩ := (hM y hy).frame_back hc + exact dp _ _ _ (h y hy p size rights m0 e0 a0 v0 hc0) + +theorem FrThA.mapA {th th' : TypedHeapA} (hM : ∀ u ∈ OS, MapAlikeA (capA st s' u) (capA st s u)) + (thE : ∀ p z, th p = some (.other z) → th' p = some (.other z)) (h : FrThA st th OS s) : FrThA st th' OS s' := + FrThA.ofSrc (fun y hy p size rights x1 x2 x3 x4 hc => by + obtain ⟨m0, e0, a0, v0, hc0⟩ := (hM y hy).frame_back hc + exact ⟨y, hy, rights, m0, e0, a0, v0, hc0⟩) thE h + +theorem SpecZeroFreeA.mapA {σ σ' : Fl4SpecA64.State Unit} (hM : ∀ u ∈ OS, MapAlikeA (capA st s' u) (capA st s u)) + (um : σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory) (h : SpecZeroFreeA st OS s σ) : + SpecZeroFreeA st OS s' σ' := fun a hf => by rw [um]; exact h a (UtFreeA.mapBack hM hf) + +theorem DeadA.mapBack {k : DKindA} {p : U64} (hM : ∀ u ∈ OS, MapAlikeA (capA st s' u) (capA st s u)) + (hd : DeadA st OS k s' p) : DeadA st OS k s p := fun u hu hn => by + refine hd u hu ?_ + have stable : ∀ e : capability.Cap, NamesDA k p e → StableCapA e := by + intro e he + cases k + · have : e = .Vcpu p := he + subst this; trivial + · obtain ⟨g, rfl⟩ := he; trivial + · obtain ⟨b, rfl⟩ := he; trivial + · rcases he with rfl | ⟨n, rfl⟩ <;> trivial + have eq : ∀ e : capability.Cap, NamesDA k p e → capA st s u = e → capA st s' u = e := + fun e he hc => (hM u hu).fwd_eq (stable e he) hc + cases k + · have : capA st s u = .Vcpu p := hn + rw [eq _ (show NamesDA .vcpu p (.Vcpu p) from rfl) this]; rfl + · obtain ⟨g, hg⟩ := hn + rw [eq _ (show NamesDA .reply p (.Reply p g) from ⟨g, rfl⟩) hg]; exact ⟨g, rfl⟩ + · obtain ⟨b, hb⟩ := hn + rw [eq _ (show NamesDA .pool p (.AsidPool p b) from ⟨b, rfl⟩) hb]; exact ⟨b, rfl⟩ + · rcases hn with hn | ⟨n, hn⟩ + · rw [eq _ (show NamesDA .tcb p (.Thread p) from Or.inl rfl) hn]; exact Or.inl rfl + · rw [eq _ (show NamesDA .tcb p (.Zombie p capability.ZOMBIE_TCB n) from Or.inr ⟨n, rfl⟩) hn] + exact Or.inr ⟨n, rfl⟩ + +theorem DeadExtA.mapA {Z Z' : XSetsA} {Rp : Set U64} {stack : U64 → List U64} {σ σ' : Fl4SpecA64.State Unit} + (hM : ∀ u ∈ OS, MapAlikeA (capA st s' u) (capA st s u)) (HS : SpecSlotA σ σ') (eV : Z'.V = Z.V) + (eC : Z'.C = Z.C) (eT : Z'.T = Z.T) + (tw : ∀ t ∈ Z.T, L.mem s' (wa t 3664#u64) = L.mem s (wa t 3664#u64)) + (vw : ∀ v ∈ Z.V, L.mem s' (wa v 24#u64) = L.mem s (wa v 24#u64)) (h : DeadExtA st Z OS Rp stack s σ) : + DeadExtA st Z' OS Rp stack s' σ' where + vcpu := fun v hv hd x hx => by + rw [eV] at hv + refine h.vcpu v hv (DeadA.mapBack hM hd) x ?_ + exact HS.back hx (fun _ _ e => by cases e) (fun _ e => by cases e) + reply := fun r hr hd c hc => by + rw [eC] at hc + exact h.reply r hr (DeadA.mapBack hM hd) c hc + asid := fun i p hi hd => h.asid i p (by rw [← HS.asid]; exact hi) (DeadA.mapBack hM hd) + tvcpu := fun t ht hd => by + rw [eT] at ht + rw [tw t ht] + exact h.tvcpu t ht (DeadA.mapBack hM hd) + vcDeadRun := fun v hv hd => by + rw [eV] at hv + rw [vw v hv] + exact h.vcDeadRun v hv (DeadA.mapBack hM hd) + replyTcb := fun r hr hd x hx => + h.replyTcb r hr (DeadA.mapBack hM hd) x (HS.back hx (fun _ _ e => by cases e) (fun _ e => by cases e)) + +theorem SpecFreeA.mapA {σ σ' : Fl4SpecA64.State Unit} (hM : ∀ u ∈ OS, MapAlikeA (capA st s' u) (capA st s u)) + (HS : SpecSlotA σ σ') (hcdt : σ'.cdt = σ.cdt) (horig : σ'.isOriginalCap = σ.isOriginalCap) + (h : SpecFreeA st OS s σ) : SpecFreeA st OS s' σ' where + heap := fun p hf => by + have e := h.heap p (UtFreeA.mapBack hM hf) + rcases HS.kh (specPtr p) with e' | ⟨r, cs, cs', e1, -⟩ | ⟨x, x', e1, -⟩ + · rw [e', e] + · rw [e] at e1; cases e1 + · rw [e] at e1; cases e1 + cdt := fun x y hx p hf => h.cdt x y (by rw [← hcdt]; exact hx) p (UtFreeA.mapBack hM hf) + orig := fun x hx p hf => h.orig x (by rw [← horig]; exact hx) p (UtFreeA.mapBack hM hf) + +/-! ## The MDB part of J5 across an in-place write -/ + +end keep + +/-! ## `set_cap` changes only the heap -/ + +/-- Every outcome of `f` differs from the start only in `kheap`. -/ +def NdKhA {α : Type} (f : Fl4Spec.NonDet (State Unit) α) : Prop := + ∀ σ r, r ∈ (f σ).1 → ∃ kh, r.2 = { σ with kheap := kh } + +theorem NdKhA.bind {α β : Type} {m : Fl4Spec.NonDet (State Unit) α} {f : α → Fl4Spec.NonDet (State Unit) β} + (hm : NdKhA m) (hf : ∀ a, NdKhA (f a)) : NdKhA (m >>= f) := fun σ r hr => by + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + obtain ⟨kh1, e1⟩ := hm σ x hx + obtain ⟨kh2, e2⟩ := hf x.1 x.2 r hr + refine ⟨kh2, ?_⟩ + rw [e2, e1] + +theorem NdKhA.ro {α : Type} {f : Fl4Spec.NonDet (State Unit) α} (h : ∀ σ r, r ∈ (f σ).1 → r.2 = σ) : NdKhA f := + fun σ r hr => ⟨σ.kheap, by rw [h σ r hr]⟩ + +theorem NdKhA.pure {α : Type} (a : α) : NdKhA (Pure.pure a : Fl4Spec.NonDet (State Unit) α) := + NdKhA.ro fun σ r h => by obtain rfl : r = (a, σ) := h; rfl + +theorem NdKhA.fail {α : Type} : NdKhA (Fl4Spec.NonDet.fail : Fl4Spec.NonDet (State Unit) α) := + fun _ _ h => absurd h (Set.notMem_empty _) + +theorem NdKhA.assert (P : Prop) : NdKhA (Fl4Spec.NonDet.assert P : Fl4Spec.NonDet (State Unit) Unit) := + NdKhA.ro fun _ _ h => by obtain ⟨-, rfl⟩ := h; rfl + +theorem NdKhA.assertOpt {α : Type} (o : Option α) : + NdKhA (Fl4Spec.NonDet.assertOpt o : Fl4Spec.NonDet (State Unit) α) := by + cases o + · exact NdKhA.fail + · exact NdKhA.pure _ + +theorem getObject_khA (p : ObjRef) : NdKhA (getObject (Z := Unit) p) := NdKhA.ro fun _ _ h => (getObject_mem h).1 + +theorem setObject_khA (p : ObjRef) (o : KernelObject) : NdKhA (setObject (Z := Unit) p o) := fun σ r hr => by + obtain ⟨_, -, h⟩ := setObject_mem hr + exact ⟨_, h⟩ + +/-- **`set_cap` changes only `kheap`** (module docstring). -/ +theorem setCap_khA (cap : Cap) (sl : CslotPtr) : NdKhA (setCap (Z := Unit) cap sl) := by + obtain ⟨oref, cref⟩ := sl + refine NdKhA.bind (getObject_khA oref) fun obj => ?_ + cases obj <;> dsimp only <;> (try split_ifs) <;> first + | exact NdKhA.bind (NdKhA.pure _) fun _ => setObject_khA _ _ + | exact NdKhA.bind NdKhA.fail fun _ => setObject_khA _ _ + | exact NdKhA.fail + +/-- The CDT, the original-cap map and the machine state across `set_cap`. -/ +theorem setCap_keepsA {cap : Cap} {sl : CslotPtr} {σ σ' : State Unit} (h : ((), σ') ∈ (setCap (Z := Unit) cap sl σ).1) : + σ'.cdt = σ.cdt ∧ σ'.isOriginalCap = σ.isOriginalCap ∧ σ'.machineState = σ.machineState := by + obtain ⟨kh, e⟩ := setCap_khA cap sl σ ((), σ') h + have e' : σ' = { σ with kheap := kh } := e + subst e' + exact ⟨rfl, rfl, rfl⟩ + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3MapExtA.lean b/proofs/Fl4ProofsA64/P3/Stage3MapExtA.lean new file mode 100644 index 000000000..189dfe63e --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3MapExtA.lean @@ -0,0 +1,574 @@ +/- +P3 (AArch64, merge line, D8 step 5 (ii), the map arms): **shared facts for the Ext (Stage-2 / high-window) +`ARMPageMap` arm**. + +* `ept_map_invA` / `hi_map_invA`: what a successful FL4 `ept::map_frame` / `decode_high_page_map` did (the lookup, the + descriptor, the restart, the cap write, the install), by inverting the code. +* `s2At_memA` / `hiMap_memA`: the shape of the spec decode's successful outcome. +* `extWrite_vsA`: **any** Ext slot write is a `VsSpecA` step that keeps data pages (`P3/Stage3UnmapExtSpecA` shows + this for invalid entries only). +* `vsTyR_frameMapA`: the record's Stage-1 typing across P1's exact frame map (every root kind). +* `UnmPtSpecA.ofFootWrA`: **J9 across one *valid* write into a footprint word** (a Stage-2 page or a window page). + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3ArmFrMapA +import Fl4ProofsA64.P3.Stage3ArmFrUnmXA +import Fl4ProofsA64.P3.ExtMapFrA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +private theorem err_ne_okA {r1 : core.result.Result Unit error.Exc} {x : error.Exc} {s s3 : S} {a a3 : A} + (h1 : r1 = core.result.Result.Err x) + (h : (ok (r1, s, a) : Result _) = ok (core.result.Result.Ok (), s3, a3)) : False := by + have := ok_inj_ff h + rw [h1] at this + simp at this + +/-- **A successful `ept::map_frame`** (module docstring). -/ +theorem ept_map_invA {ar : machine.Arch A} {s s3 : S} {a a3 : A} {root asid ipa vm attr fp rt asid0 va0 : U64} + {sz : capability.FrameSize} {dev : Bool} {sl : U64} + (h : ept.map_frame st ar s a root asid ipa vm attr fp sz rt dev asid0 va0 sl = + ok (core.result.Result.Ok (), s3, a3)) : + ∃ (shift ipaB : U64) (lu : ept.S2Slot) (cr : Bool) (pa ew : U64) (s1 s2 : S), + sz.bits = ok shift ∧ ar.s2_ipa_bits a = ok ipaB ∧ ept.lookup_slot st s root ipa ipaB = ok lu ∧ + lu.shift = shift ∧ vspace.rights_readable rt = ok cr ∧ layout.paddr fp = ok pa ∧ + ept.make_s2_leaf pa vm cr attr shift = ok ew ∧ + sched.set_thread_state st s (L.curW s) sched.TS_RESTART = ok s1 ∧ + obj.Slot.set_cap st sl s1 (.Frame fp sz rt dev (ew != 0#u64) (ew != 0#u64) + (if ew = 0#u64 then 0#u64 else asid) (if ew = 0#u64 then 0#u64 else ipa)) = ok s2 ∧ + ept.install_s2_leaf st ar s2 a lu ew root asid ipa fp dev attr = ok (s3, a3) := by + unfold ept.map_frame at h + obtain ⟨shift, hsh, h⟩ := bind_eq_ok h + obtain ⟨ipaB, hib, h⟩ := bind_eq_ok h + obtain ⟨i, -, h⟩ := bind_eq_ok h + obtain ⟨i1, -, h⟩ := bind_eq_ok h + obtain ⟨i2, -, h⟩ := bind_eq_ok h + obtain ⟨i3, -, h⟩ := bind_eq_ok h + split at h + · exact (err_ne_okA rfl h).elim + have tail : ∀ (X : Result (core.result.Result Unit error.Exc × S × A)), + X = (do + let lu ← ept.lookup_slot st s root ipa ipaB + if lu.shift != shift then + ok (core.result.Result.Err (error.Exc.Sys (error.SysError.FailedLookup false + (error.LookupFault.MissingCapability lu.shift))), s, a) + else do + let cap_read ← vspace.rights_readable rt + let i4 ← layout.paddr fp + let e ← ept.make_s2_leaf i4 vm cap_read attr shift + let ct ← sched.cur_thread st s + let s1 ← sched.set_thread_state st s ct sched.TS_RESTART + if e != 0#u64 then + if e != 0#u64 then do + let s2 ← obj.Slot.set_cap st sl s1 (.Frame fp sz rt dev (e != 0#u64) (e != 0#u64) asid ipa) + let (s3, a1) ← ept.install_s2_leaf st ar s2 a lu e root asid ipa fp dev attr + ok (core.result.Result.Ok (), s3, a1) + else do + let s2 ← obj.Slot.set_cap st sl s1 (.Frame fp sz rt dev (e != 0#u64) (e != 0#u64) asid 0#u64) + let (s3, a1) ← ept.install_s2_leaf st ar s2 a lu e root asid ipa fp dev attr + ok (core.result.Result.Ok (), s3, a1) + else + if e != 0#u64 then do + let s2 ← obj.Slot.set_cap st sl s1 (.Frame fp sz rt dev (e != 0#u64) (e != 0#u64) 0#u64 ipa) + let (s3, a1) ← ept.install_s2_leaf st ar s2 a lu e root asid ipa fp dev attr + ok (core.result.Result.Ok (), s3, a1) + else do + let s2 ← obj.Slot.set_cap st sl s1 (.Frame fp sz rt dev (e != 0#u64) (e != 0#u64) 0#u64 0#u64) + let (s3, a1) ← ept.install_s2_leaf st ar s2 a lu e root asid ipa fp dev attr + ok (core.result.Result.Ok (), s3, a1)) → + X = ok (core.result.Result.Ok (), s3, a3) → + ∃ (shift' ipaB' : U64) (lu : ept.S2Slot) (cr : Bool) (pa ew : U64) (s1 s2 : S), + sz.bits = ok shift' ∧ ar.s2_ipa_bits a = ok ipaB' ∧ ept.lookup_slot st s root ipa ipaB' = ok lu ∧ + lu.shift = shift' ∧ vspace.rights_readable rt = ok cr ∧ layout.paddr fp = ok pa ∧ + ept.make_s2_leaf pa vm cr attr shift' = ok ew ∧ + sched.set_thread_state st s (L.curW s) sched.TS_RESTART = ok s1 ∧ + obj.Slot.set_cap st sl s1 (.Frame fp sz rt dev (ew != 0#u64) (ew != 0#u64) + (if ew = 0#u64 then 0#u64 else asid) (if ew = 0#u64 then 0#u64 else ipa)) = ok s2 ∧ + ept.install_s2_leaf st ar s2 a lu ew root asid ipa fp dev attr = ok (s3, a3) := by + intro X hX h + rw [hX] at h + obtain ⟨lu, hlu, h⟩ := bind_eq_ok h + split at h + · exact (err_ne_okA rfl h).elim + rename_i hne + have hls : lu.shift = shift := UScalar.eq_of_val_eq (by simpa using hne) + obtain ⟨cr, hcr, h⟩ := bind_eq_ok h + obtain ⟨pa, hpa, h⟩ := bind_eq_ok h + obtain ⟨ew, hew, h⟩ := bind_eq_ok h + obtain ⟨ct, hct, h⟩ := bind_eq_ok h + rw [cur_thread_ok] at hct + obtain rfl := ok_inj_ff hct + obtain ⟨s1, hs1, h⟩ := bind_eq_ok h + refine ⟨shift, ipaB, lu, cr, pa, ew, s1, ?_⟩ + have fin : ∀ (c : capability.Cap), (do + let s2 ← obj.Slot.set_cap st sl s1 c + let (s3, a1) ← ept.install_s2_leaf st ar s2 a lu ew root asid ipa fp dev attr + ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s3, a1)) = + ok (core.result.Result.Ok (), s3, a3) → + ∃ s2, obj.Slot.set_cap st sl s1 c = ok s2 ∧ + ept.install_s2_leaf st ar s2 a lu ew root asid ipa fp dev attr = ok (s3, a3) := by + intro c h + obtain ⟨s2, h2, h⟩ := bind_eq_ok h + obtain ⟨⟨t3, b3⟩, h3, h⟩ := bind_eq_ok h + have := ok_inj_ff h + simp only [Prod.mk.injEq, true_and] at this + obtain ⟨rfl, rfl⟩ := this + exact ⟨s2, h2, h3⟩ + by_cases hz : ew = 0#u64 + · have hb : (ew != 0#u64) = false := by simp [hz] + simp only [hb, Bool.false_eq_true, ↓reduceIte] at h + obtain ⟨s2, h2, h3⟩ := fin _ h + exact ⟨s2, hsh, hib, hlu, hls, hcr, hpa, hew, hs1, by simp only [hb, if_pos hz]; exact h2, h3⟩ + · have hb : (ew != 0#u64) = true := bne_iff_ne.2 hz + simp only [hb, ↓reduceIte] at h + obtain ⟨s2, h2, h3⟩ := fin _ h + exact ⟨s2, hsh, hib, hlu, hls, hcr, hpa, hew, hs1, by simp only [hb, if_neg hz]; exact h2, h3⟩ + split at h + · split at h + · obtain ⟨r, hr, h⟩ := bind_eq_ok h + exact (err_ne_okA (ok_inj_ff hr).symm h).elim + split at h + · obtain ⟨r, hr, h⟩ := bind_eq_ok h + exact (err_ne_okA (ok_inj_ff hr).symm h).elim + exact tail _ rfl h + · obtain ⟨x1, -, h⟩ := bind_eq_ok h + obtain ⟨x2, -, h⟩ := bind_eq_ok h + obtain ⟨x3, -, h⟩ := bind_eq_ok h + obtain ⟨last, -, h⟩ := bind_eq_ok h + obtain ⟨lim, -, h⟩ := bind_eq_ok h + split at h + · obtain ⟨r, hr, h⟩ := bind_eq_ok h + exact (err_ne_okA (ok_inj_ff hr).symm h).elim + split at h + · obtain ⟨r, hr, h⟩ := bind_eq_ok h + exact (err_ne_okA (ok_inj_ff hr).symm h).elim + exact tail _ rfl h + + +/-- **A successful `decode_high_page_map`** (module docstring). -/ +theorem hi_map_invA {ar : machine.Arch A} {s s3 : S} {a a3 : A} {vaddr vm attr : U64} {sl : U64} + {f : vspace.FrameCap} {vcap : capability.Cap} + (h : vspace.decode_high_page_map st ar s a sl f vcap vaddr vm attr = ok (core.result.Result.Ok (), s3, a3)) : + ∃ (root asid shift : U64) (lu : vspace.PtSlot) (cr : Bool) (pa ew : U64) (s1 s2 : S), + vspace.check_high_vspace st s vcap 1#u64 = ok (core.result.Result.Ok (root, asid)) ∧ + vspace.page_shift f.size = ok shift ∧ vspace.lookup_pt_slot st s root vaddr = ok lu ∧ lu.shift = shift ∧ + vspace.rights_readable f.rights = ok cr ∧ layout.paddr f.ptr = ok pa ∧ + vspace.make_user_page pa vm cr attr shift = ok ew ∧ + sched.set_thread_state st s (L.curW s) sched.TS_RESTART = ok s1 ∧ + obj.Slot.set_cap st sl s1 (.Frame f.ptr f.size f.rights f.is_device true false asid vaddr) = ok s2 ∧ + vspace.install_page st ar s2 a lu ew 0#u64 asid vaddr f attr true = ok (s3, a3) := by + unfold vspace.decode_high_page_map at h + obtain ⟨ct, hct, h⟩ := bind_eq_ok h + rw [cur_thread_ok] at hct + obtain rfl := ok_inj_ff hct + obtain ⟨r, hr, h⟩ := bind_eq_ok h + obtain ⟨cf, hcf, h⟩ := bind_eq_ok h + rcases r with ⟨root, asid⟩ | err + swap + · simp only [core.result.Result.Insts.CoreOpsTry.branch] at hcf + obtain rfl := ok_inj_ff hcf + simp only [core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, bind_tc_ok] at h + first + | exact (err_ne_okA rfl h).elim + | (obtain ⟨r1, hr1, h⟩ := bind_eq_ok h + first + | exact (err_ne_okA (ok_inj_ff hr1).symm h).elim + | (simp only [core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, bind_tc_ok] at hr1 + exact (err_ne_okA (ok_inj_ff hr1).symm h).elim)) + | (have := ok_inj_ff h + simp [core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual] at this) + simp only [core.result.Result.Insts.CoreOpsTry.branch] at hcf + obtain rfl := ok_inj_ff hcf + simp only at h + obtain ⟨shift, hsh, h⟩ := bind_eq_ok h + obtain ⟨i, -, h⟩ := bind_eq_ok h + obtain ⟨i1, -, h⟩ := bind_eq_ok h + obtain ⟨i2, -, h⟩ := bind_eq_ok h + obtain ⟨i3, -, h⟩ := bind_eq_ok h + split at h + · exact (err_ne_okA rfl h).elim + have tail : ∀ (X : Result (core.result.Result Unit error.Exc × S × A)), + X = (do + let lu ← vspace.lookup_pt_slot st s root vaddr + if lu.shift != shift then + ok (core.result.Result.Err (error.Exc.Sys (error.SysError.FailedLookup false + (error.LookupFault.MissingCapability lu.shift))), s, a) + else do + let cap_read ← vspace.rights_readable f.rights + let i7 ← layout.paddr f.ptr + let e ← vspace.make_user_page i7 vm cap_read attr shift + let s1 ← sched.set_thread_state st s (L.curW s) sched.TS_RESTART + let s2 ← obj.Slot.set_cap st sl s1 (.Frame f.ptr f.size f.rights f.is_device true false asid vaddr) + let (s3, a1) ← vspace.install_page st ar s2 a lu e 0#u64 asid vaddr f attr true + ok (core.result.Result.Ok (), s3, a1)) → + X = ok (core.result.Result.Ok (), s3, a3) → + ∃ (root' asid' shift' : U64) (lu : vspace.PtSlot) (cr : Bool) (pa ew : U64) (s1 s2 : S), + vspace.check_high_vspace st s vcap 1#u64 = ok (core.result.Result.Ok (root', asid')) ∧ + vspace.page_shift f.size = ok shift' ∧ vspace.lookup_pt_slot st s root' vaddr = ok lu ∧ lu.shift = shift' ∧ + vspace.rights_readable f.rights = ok cr ∧ layout.paddr f.ptr = ok pa ∧ + vspace.make_user_page pa vm cr attr shift' = ok ew ∧ + sched.set_thread_state st s (L.curW s) sched.TS_RESTART = ok s1 ∧ + obj.Slot.set_cap st sl s1 (.Frame f.ptr f.size f.rights f.is_device true false asid' vaddr) = ok s2 ∧ + vspace.install_page st ar s2 a lu ew 0#u64 asid' vaddr f attr true = ok (s3, a3) := by + intro X hX h + rw [hX] at h + obtain ⟨lu, hlu, h⟩ := bind_eq_ok h + split at h + · exact (err_ne_okA rfl h).elim + rename_i hne + have hls : lu.shift = shift := UScalar.eq_of_val_eq (by simpa using hne) + obtain ⟨cr, hcr, h⟩ := bind_eq_ok h + obtain ⟨pa, hpa, h⟩ := bind_eq_ok h + obtain ⟨ew, hew, h⟩ := bind_eq_ok h + obtain ⟨s1, hs1, h⟩ := bind_eq_ok h + obtain ⟨s2, h2, h⟩ := bind_eq_ok h + obtain ⟨⟨t3, b3⟩, h3, h⟩ := bind_eq_ok h + have := ok_inj_ff h + simp only [Prod.mk.injEq, true_and] at this + obtain ⟨rfl, rfl⟩ := this + exact ⟨root, asid, shift, lu, cr, pa, ew, s1, s2, hr, hsh, hlu, hls, hcr, hpa, hew, hs1, h2, h3⟩ + split at h + · split at h + · obtain ⟨r, hr, h⟩ := bind_eq_ok h + exact (err_ne_okA (ok_inj_ff hr).symm h).elim + split at h + · obtain ⟨r, hr, h⟩ := bind_eq_ok h + exact (err_ne_okA (ok_inj_ff hr).symm h).elim + obtain ⟨x1, -, h⟩ := bind_eq_ok h + obtain ⟨x2, -, h⟩ := bind_eq_ok h + obtain ⟨x3, -, h⟩ := bind_eq_ok h + obtain ⟨last, -, h⟩ := bind_eq_ok h + split at h + · obtain ⟨r, hr, h⟩ := bind_eq_ok h + exact (err_ne_okA (ok_inj_ff hr).symm h).elim + split at h + · obtain ⟨r, hr, h⟩ := bind_eq_ok h + exact (err_ne_okA (ok_inj_ff hr).symm h).elim + split at h + · obtain ⟨r, hr, h⟩ := bind_eq_ok h + exact (err_ne_okA (ok_inj_ff hr).symm h).elim + exact tail _ rfl h + · obtain ⟨x1, -, h⟩ := bind_eq_ok h + obtain ⟨x2, -, h⟩ := bind_eq_ok h + obtain ⟨x3, -, h⟩ := bind_eq_ok h + obtain ⟨last, -, h⟩ := bind_eq_ok h + split at h + · obtain ⟨r, hr, h⟩ := bind_eq_ok h + exact (err_ne_okA (ok_inj_ff hr).symm h).elim + split at h + · obtain ⟨r, hr, h⟩ := bind_eq_ok h + exact (err_ne_okA (ok_inj_ff hr).symm h).elim + split at h + · obtain ⟨r, hr, h⟩ := bind_eq_ok h + exact (err_ne_okA (ok_inj_ff hr).symm h).elim + exact tail _ rfl h + +end + +/-! ## The spec side -/ + +section +variable {α β σt εt : Type} + +/-- A successful outcome of a bind comes through a successful outcome of the first computation. -/ +theorem mem_bind_invD {m : NonDetE σt εt α} {f : α → NonDetE σt εt β} {s s' : σt} {b : β} + (h : (Except.ok b, s') ∈ ((m >>= f).run s).1) : + ∃ a s1, (Except.ok a, s1) ∈ (m.run s).1 ∧ (Except.ok b, s') ∈ ((f a).run s1).1 := by + rw [ExceptT.run_bind] at h + obtain ⟨⟨r, s1⟩, h1, h2⟩ := h + rcases r with e | a + · have := Set.mem_singleton_iff.1 h2 + simp at this + · exact ⟨a, s1, h1, h2⟩ + +/-- A throwing `whenE` / `unlessE` keeps the state when it succeeds. -/ +theorem mem_when_invD {P : Prop} [Decidable P] {e : εt} {s s' : σt} {u : Unit} + (h : (Except.ok u, s') ∈ ((NonDetE.whenE P (NonDetE.throwError e)).run s).1) : s' = s := by + unfold NonDetE.whenE at h + split at h + · have := Set.mem_singleton_iff.1 h + simp at this + · exact (Prod.mk.inj (Set.mem_singleton_iff.1 h)).2 + +theorem mem_unless_invD {P : Prop} [Decidable P] {e : εt} {s s' : σt} {u : Unit} + (h : (Except.ok u, s') ∈ ((NonDetE.unlessE P (NonDetE.throwError e)).run s).1) : s' = s := by + unfold NonDetE.unlessE at h + split at h + · exact (Prod.mk.inj (Set.mem_singleton_iff.1 h)).2 + · have := Set.mem_singleton_iff.1 h + simp at this + +theorem mem_gets_invD {g : σt → α} {s s' : σt} {x : α} + (h : (Except.ok x, s') ∈ ((NonDetE.liftE (ε := εt) (NonDet.gets g)).run s).1) : s' = s ∧ x = g s := by + rw [liftE_runA (m := NonDet.gets g) (a := g s) (s := s) (s' := s) rfl] at h + have := Prod.mk.inj (Set.mem_singleton_iff.1 h) + exact ⟨this.2, Except.ok.inj this.1⟩ + +end + +theorem checkAlign_keepA {sz : VMPageSize} {va : Fl4SpecA64.VSpaceRef} {s s' : State Unit} {u : Unit} + (h : (Except.ok u, s') ∈ ((checkVpAlignment (Z := Unit) sz va).run s).1) : s' = s := by + unfold checkVpAlignment at h + first + | exact mem_unless_invD h + | exact mem_when_invD h + +private theorem s2Tail_memA {cte : CslotPtr} {p : Fl4SpecA64.ObjRef} {R : CapRights} {sz : VMPageSize} {dev : Bool} + {r : Fl4SpecA64.ObjRef} {asid : Asid} {va rm attr : Data} {σ σ' : State Unit} {iv : ExtVSpaceInvocation} + (h : (Except.ok iv, σ') ∈ (((do + let pos ← NonDetE.liftE <| NonDet.gets fun s : State Unit => extLookupSlot s s2Geom r va + NonDetE.unlessE (pos.shift = pageBitsForSize sz) <| + NonDetE.throwError (.failedLookup false (.missingCapability pos.shift)) + let vm := maskVmRights R (dataToRights rm) + if vm = ∅ then + NonDetE.returnOk <| .map (.frameCap p R sz dev none) cte .invalidPTE pos.slot + else + NonDetE.returnOk <| .map (.ext (.stage2FrameCap p R sz dev (some (asid, va)))) cte + (makeUserPte (addrFromPPtr p) (attribsFromWord attr) vm R sz) pos.slot) : + SEMonad Unit ExtVSpaceInvocation).run σ).1) : + (maskVmRights R (dataToRights rm) = ∅ ∧ + iv = .map (.frameCap p R sz dev none) cte .invalidPTE (extLookupSlot σ s2Geom r va).slot) ∨ + (maskVmRights R (dataToRights rm) ≠ ∅ ∧ + iv = .map (.ext (.stage2FrameCap p R sz dev (some (asid, va)))) cte + (makeUserPte (addrFromPPtr p) (attribsFromWord attr) (maskVmRights R (dataToRights rm)) R sz) + (extLookupSlot σ s2Geom r va).slot) := by + obtain ⟨pos, s2, h1, h⟩ := mem_bind_invD h + obtain ⟨e2, rfl⟩ := mem_gets_invD h1 + subst e2 + obtain ⟨u3, s3, h3, h⟩ := mem_bind_invD h + obtain rfl := mem_unless_invD h3 + dsimp only at h + split at h + · rename_i hv + exact Or.inl ⟨hv, Except.ok.inj (Prod.mk.inj (Set.mem_singleton_iff.1 h)).1⟩ + · rename_i hv + exact Or.inr ⟨hv, Except.ok.inj (Prod.mk.inj (Set.mem_singleton_iff.1 h)).1⟩ + +/-- **The successful outcome of `decodeS2PageMapAt`**: a `map` of the looked-up slot. -/ +theorem s2At_memA {cte : CslotPtr} {p : Fl4SpecA64.ObjRef} {R : CapRights} {sz : VMPageSize} {dev : Bool} + {mapped : Option (Asid × Fl4SpecA64.VSpaceRef)} {r : Fl4SpecA64.ObjRef} {asid : Asid} {va rm attr : Data} + {σ σ' : State Unit} {iv : ExtVSpaceInvocation} + (h : (Except.ok iv, σ') ∈ ((decodeS2PageMapAt (Z := Unit) cte p R sz dev mapped r asid va rm attr).run σ).1) : + (maskVmRights R (dataToRights rm) = ∅ ∧ + iv = .map (.frameCap p R sz dev none) cte .invalidPTE (extLookupSlot σ s2Geom r va).slot) ∨ + (maskVmRights R (dataToRights rm) ≠ ∅ ∧ + iv = .map (.ext (.stage2FrameCap p R sz dev (some (asid, va)))) cte + (makeUserPte (addrFromPPtr p) (attribsFromWord attr) (maskVmRights R (dataToRights rm)) R sz) + (extLookupSlot σ s2Geom r va).slot) := by + unfold decodeS2PageMapAt at h + obtain ⟨u0, s0, h0, g⟩ := mem_bind_invD h + clear h + obtain rfl := checkAlign_keepA h0 + cases mapped with + | some w => + obtain ⟨asid', va'⟩ := w + dsimp only at g + obtain ⟨u1, s1, h1, g1⟩ := mem_bind_invD g + obtain rfl := mem_when_invD h1 + obtain ⟨u2, s2, h2, g2⟩ := mem_bind_invD g1 + obtain rfl := mem_when_invD h2 + exact s2Tail_memA g2 + | none => + dsimp only at g + obtain ⟨u1, s1, h1, g1⟩ := mem_bind_invD g + obtain rfl := mem_when_invD h1 + exact s2Tail_memA g1 + +/-! ## Any Ext slot write is a `VsSpecA` step -/ + +/-- **Any Ext table-slot write** changes only a table / root object, to one of the same kind: a `VsSpecA` step that +keeps data pages. -/ +theorem extWrite_vsA (σ : State Unit) (sl : ExtSlot) (pte : PTE) : + VsSpecA σ (extSlotWriteX σ sl pte) ∧ + ∀ q dv pg, σ.kheap q = some (.archObj (.dataPage dv pg)) → + (extSlotWriteX σ sl pte).kheap q = some (.archObj (.dataPage dv pg)) := by + have kh : ∀ q, (extSlotWriteX σ sl pte).kheap q = σ.kheap q ∨ ∃ o o', σ.kheap q = some (.archObj o) ∧ + (extSlotWriteX σ sl pte).kheap q = some (.archObj o') ∧ (∀ v, o ≠ .vcpu v) ∧ (∀ v, o' ≠ .vcpu v) ∧ + (∀ dv pg, o ≠ .dataPage dv pg) := fun q => by + show extSlotKhX σ sl pte q = σ.kheap q ∨ ∃ o o', σ.kheap q = some (.archObj o) ∧ + extSlotKhX σ sl pte q = some (.archObj o') ∧ (∀ v, o ≠ .vcpu v) ∧ (∀ v, o' ≠ .vcpu v) ∧ + (∀ dv pg, o ≠ .dataPage dv pg) + cases sl with + | root r i => + cases ho : extObjAt σ r with + | none => exact Or.inl (by simp only [extSlotKhX, ho]) + | some o => + have e : extSlotKhX σ (.root r i) pte = + Function.update σ.kheap r (some (.archObj (.ext (o.setEntry i pte)))) := by + simp only [extSlotKhX, ho] + rw [e] + by_cases hq : q = r + · subst hq + have hk : σ.kheap q = some (.archObj (.ext o)) := extObjAt_someX.1 ho + exact Or.inr ⟨_, .ext (o.setEntry i pte), hk, Function.update_self _ _ _, + fun _ h => (by cases h), fun _ h => (by cases h), fun _ _ h => (by cases h)⟩ + · exact Or.inl (Function.update_of_ne hq _ _) + | pt p => + cases hk : σ.kheap (tableBase .normalPtT p) with + | none => exact Or.inl (by simp only [extSlotKhX, hk]) + | some ko => + cases ko with + | archObj ao => + cases ao with + | pageTable t => + have e : extSlotKhX σ (.pt p) pte = Function.update σ.kheap (tableBase .normalPtT p) + (some (.archObj (.pageTable (ptUpd t (tableIndex .normalPtT p) pte)))) := by + simp only [extSlotKhX, hk] + rw [e] + by_cases hq : q = tableBase .normalPtT p + · subst hq + exact Or.inr ⟨_, .pageTable (ptUpd t (tableIndex .normalPtT p) pte), hk, Function.update_self _ _ _, + fun _ h => (by cases h), fun _ h => (by cases h), fun _ _ h => (by cases h)⟩ + · exact Or.inl (Function.update_of_ne hq _ _) + | _ => exact Or.inl (by simp only [extSlotKhX, hk]) + | _ => exact Or.inl (by simp only [extSlotKhX, hk]) + refine ⟨VsSpecA.of_qvA (vmA_refl _) (fun q => (kh q).imp_right fun ⟨o, o', a, b, c, d, _⟩ => ⟨o, o', a, b, c, d⟩) + rfl, fun q dv pg h => ?_⟩ + rcases kh q with e | ⟨o, o', a, -, -, -, n⟩ + · exact e.trans h + · rw [h] at a + have := Option.some.inj a + injection this with h4 + exact absurd h4.symm (n _ _) + +/-! ## The record's typing across an exact frame map, and J9 across a valid footprint write -/ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **The record's narrowed Stage-1 typing across P1's exact frame map** (`S1aFrameMap`, any root kind): the written +descriptor is no table descriptor of a reachable table (`S1aR2.mapStage1Source`, through the J7 witness's reach). -/ +theorem vsTyR_frameMapA {H : Heap} {OS Pt Pp PpJ Tb TbJ Hr Te Kt : Set U64} {Lv LvJ Re bits : U64 → Nat} + {n tag slot : U64} {s q : S} {f : vspace.FrameCap} + (h : S1aR2 st H OS Pt PpJ TbJ Hr Te Kt LvJ Re bits n s) (heap : HeapOK H) (kinds : KindA st OS s) + (ptKs : ∀ b, PtW Pt b → ¬ AsW (L.ksb s) b) (hJ : VsTyR st (L.ksb s) (PoolSet st Pp s) ∅ Tb Lv s) + (hpools : PoolSet st Pp s ⊆ PoolSet st PpJ s) (ev : S1aFrameMap st OS Pt PpJ n tag s q slot f) : + L.ksb q = L.ksb s ∧ VsTyR st (L.ksb q) (PoolSet st Pp q) ∅ Tb Lv q ∧ + utVsPtA st Pp Tb Lv q ⊆ utVsPtA st Pp Tb Lv s := by + obtain ⟨asid, va, addr, word, sh, _, selected, _, _, _, leaf, write⟩ := ev.edit + obtain ⟨tab, pools⟩ := write.keepAsid h heap kinds ptKs selected + have shape : word = 0#u64 ∨ s1aLeafValid word sh.val := leaf.imp And.left And.left + have toff : ∀ o : U64, 772352 ≤ o.val → o.val < 773376 → o.val % 8 = 0 → + L.mem q (wa (L.ksb s) o) = L.mem s (wa (L.ksb s) o) := fun o o1 o2 o3 => by + have := tab.off o1 o2 o3 + rwa [write.base] at this + have kAs : ∀ b, AsW (L.ksb s) b → L.mem q b = L.mem s b := fun b ⟨o, o1, o2, o3, e⟩ => by + rw [e]; exact toff o o1 o2 o3 + have ep : PoolSet st Pp q = PoolSet st Pp s := PoolSet.keepAsA write.base kAs + have kP : ∀ p ∈ PoolSet st Pp s, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → L.mem q (wa p o) = L.mem s (wa p o) := + fun p hp o o1 o2 => pools p (hpools hp) o o1 o2 + have br : ∀ p, VsReach st (PoolSet st Pp s) ∅ Lv s p → + VsReach st (PoolSet st PpJ s) ∅ LvJ s p ∧ p ∈ Tb ∧ p ∈ TbJ ∧ LvJ p = Lv p := + fun p r => vsReach_bridgeA hJ h.core.stage1 hpools r + have edge : ∀ p, VsReach st (PoolSet st Pp s) ∅ Lv s p → Lv p < 3 → ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → + (L.mem q (wa p o) &&& 3#u64) = 3#u64 → L.mem q (wa p o) = L.mem s (wa p o) := by + intro p r hl o o1 o2 valid + obtain ⟨r7, -, m7, el⟩ := br p r + have different : wa p o ≠ addr := fun e => by + have levelEq := (h.mapStage1Source heap kinds selected r7 o1 e).2 + rw [e, write.descriptor] at valid + exact s1aLeafNotTable shape (by omega) valid + exact write.memory _ (Or.inl ⟨p, h.place.tables m7, o, o1, o2, rfl⟩) different + have back : ∀ p, VsReach st (PoolSet st Pp q) ∅ Lv q p → VsReach st (PoolSet st Pp s) ∅ Lv s p := by + intro p hr + rw [ep] at hr + induction hr with + | hr h0 => exact absurd h0 (Set.notMem_empty _) + | @root0 p o hp o1 o2 hne h0 => + rw [kP p hp o o1 o2] at hne h0 ⊢; exact VsReach.root0 hp o1 o2 hne h0 + | @root2 p o hp o1 o2 h2 => + rw [kP p hp o o1 o2] at h2 ⊢; exact VsReach.root2 hp o1 o2 h2 + | @step p o _ hl o1 o2 h3 ih => + have e := edge p ih hl o o1 o2 h3 + rw [e] at h3 ⊢; exact VsReach.step ih hl o1 o2 h3 + refine ⟨write.base, ⟨fun o o1 o2 o3 hne => ?_, fun p hp o o1 o2 hne => ?_, fun p hp hr hl o o1 o2 h3 => ?_⟩, ?_⟩ + · rw [write.base] at hne ⊢ + rw [toff o o1 o2 o3] at hne ⊢ + rw [ep]; exact hJ.tbl o o1 o2 o3 hne + · rw [ep] at hp + rw [kP p hp o o1 o2] at hne ⊢ + exact hJ.pool p hp o o1 o2 hne + · have r0 := back p hr + have e := edge p r0 hl o o1 o2 h3 + rw [e] at h3 ⊢ + exact hJ.desc p hp r0 hl o o1 o2 h3 + · intro p hp + unfold utVsPtA at hp ⊢ + rw [ep] at hp + rcases hp with hp | ⟨ht, hr⟩ + · exact Or.inl hp + · exact Or.inr ⟨ht, back p (by rw [ep]; exact hr)⟩ + +end + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **J9 across one write into a footprint word** (module docstring). -/ +theorem UnmPtSpecA.ofFootWrA {Z : XSetsA} {OS : Set U64} {s s' : S} {σ σ' : State Unit} {m : McState} + {Pt : Set U64} {b : U64} {o : Fl4SpecA64.ObjRef} + (hfoot : FootA (L := L) { Z with S1 := ∅ } s b) (hobj : RelPA { Z with S1 := ∅ } m o) + (hbk : ∀ u ∈ OS, ∀ c, capA st s' u = c → (∀ p sz r d mp e a v, c ≠ .Frame p sz r d mp e a v) → + capA st s u = c) + (hword : ∀ q ∈ Pt, ∀ j : U64, j.val < 512 → peAF q j ≠ b → L.mem s' (peAF q j) = L.mem s (peAF q j)) + (link : ∀ q, SchedPageA st Z OS s q → q ∈ Pt) (hk : L.ksb s' = L.ksb s) (hcpu : L.cpu s' = L.cpu s) + (hset : ∀ q ao, q ≠ o → σ.kheap q = some (.archObj ao) → σ'.kheap q = some (.archObj ao)) + (hs2 : ∀ u ∈ OS, ∀ p a v, capA st s u = .Ept p 0#u64 false a v → + (∀ j : U64, j.val < 512 → peAF p j ≠ b ∧ peAF (wa p 4096#u64) j ≠ b) ∧ specPtr p ≠ o) + (hhv : ∀ u ∈ OS, ∀ p a, capA st s u = .HighVSpace p false a → + (∀ j : U64, j.val < 512 → peAF (wa p 4096#u64) j ≠ b) ∧ specPtr p ≠ o) + (h : UnmPtSpecA st Z m OS s σ) : UnmPtSpecA st Z m OS s' σ' := by + have tab : ∀ ty q, q ∈ Pt → UnmTabA st Z m s σ ty q → UnmTabA st Z m s' σ' ty q := fun ty q hq h => by + obtain ⟨t, k1, k2, k3⟩ := h.spec + refine ⟨fun j hj => ?_, ⟨t, hset _ _ (h.off.obj o hobj).symm k1, k2, k3⟩, h.off.of_ksA hk hcpu⟩ + rw [hword q hq j hj (fun e => h.off.mem j hj (e ▸ hfoot))] + exact h.inv j hj + refine ⟨fun u hu q asid va hc => ?_, fun u hu q asid hc => ?_, fun u hu q asid va hc => ?_, + fun u hu q asid hc => ?_⟩ + · have old := hbk u hu _ hc (fun _ _ _ _ _ _ _ _ f => by cases f) + exact tab _ _ (link q (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inl ⟨_, _, _, old⟩⟩)))))))) (h.pt u hu q asid va old) + · have old := hbk u hu _ hc (fun _ _ _ _ _ _ _ _ f => by cases f) + exact tab _ _ (link q (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inl ⟨_, _, old⟩)⟩)))))))) (h.vs u hu q asid old) + · have old := hbk u hu _ hc (fun _ _ _ _ _ _ _ _ f => by cases f) + obtain ⟨nw, no⟩ := hs2 u hu q asid va old + have l0 : q ∈ Pt := link q (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inr (Or.inl ⟨_, _, old⟩))⟩))))))) + have l1 : wa q 4096#u64 ∈ Pt := link _ (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inr (Or.inr (Or.inl ⟨q, _, _, old, rfl⟩)))⟩))))))) + have h0 := h.s2 u hu q asid va old + obtain ⟨es, k1, k2⟩ := h0.spec + refine ⟨fun j hj => ?_, es, hset _ _ no k1, k2⟩ + by_cases hlo : j.val < 512 + · rw [hword q l0 j hlo (nw j hlo).1]; exact h0.inv j hj + · obtain ⟨j', hj', ej⟩ := peAF_hiA q j (by omega) hj + have := h0.inv j hj + rw [ej] at this ⊢ + rw [hword _ l1 j' hj' (nw j' hj').2]; exact this + · have old := hbk u hu _ hc (fun _ _ _ _ _ _ _ _ f => by cases f) + obtain ⟨nw, no⟩ := hhv u hu q asid old + have l1 : wa q 4096#u64 ∈ Pt := link _ (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inr (Or.inr (Or.inr ⟨q, _, old, rfl⟩)))⟩))))))) + have h0 := h.hv u hu q asid old + obtain ⟨l, k1, k2⟩ := h0.spec + refine ⟨fun j hj => ?_, l, hset _ _ no k1, k2⟩ + rw [hword _ l1 j hj (nw j hj)]; exact h0.inv j hj + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3MapExtCoreA.lean b/proofs/Fl4ProofsA64/P3/Stage3MapExtCoreA.lean new file mode 100644 index 000000000..1b8acff1b --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3MapExtCoreA.lean @@ -0,0 +1,270 @@ +/- +P3 (AArch64, merge line, D8 step 5 (ii), the map arms): **the shared core of the Ext (Stage-2 / high-window) +`ARMPageMap` arm**. + +`JStage3A.frameMapExtCoreA`: `JStage3A` across the Restart, the in-place frame cap write and one descriptor write into a +Stage-2 page, a window page or a Stage-1 table (`XqA`), with the spec's `extSlotWriteX`. The wrapper of each root kind +supplies the facts of its own write: `restart_setcapJA`'s states, the install's `ExtWordWriteA`, the joint relation at +the final state, `ExtSlotOkX`, J9 across the write, and P1's exact frame map (`S1aFrameMap`, from the J7 witness) or +an unchanged state. The final-state facts owed at T3 are the hypothesis `fin`, as in `frameUnmapXA`. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3BodyKeepA +import Fl4ProofsA64.P3.Stage3MapExtA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +set_option maxHeartbeats 8000000 in +/-- **`JStage3A` across an Ext frame map's writes** (module docstring). -/ +theorem JStage3A.frameMapExtCoreA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {ar : machine.Arch A} {nA : U64} {s : S} {a : A} + {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) (hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM) + (hK : Aligned (L.ksb s)) (hT : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s)) + (cte : Fl4SpecA64.CslotPtr) (hab : AbsSlotA Z.th cte) {fp rt asid0 va0 : U64} {sz : capability.FrameSize} + {dev mp e0 : Bool} (hcap : capA st s (cslotAddrA cte) = .Frame fp sz rt dev mp e0 asid0 va0) + {c : capability.Cap} (hc : ∃ mp' e' a' v', c = .Frame fp sz rt dev mp' e' a' v' ∧ + (e' = false ∨ ∀ t ∈ Z.T, sbWordA t ≠ cslotAddrA cte)) {newSC : Fl4SpecA64.ArchCap} + (hEnc : CapRt.EncWf c) (hca : CapRelA c (.archObjectCap newSC)) (hok : CapObjOkA Z c) + {q j w : U64} (hq : XqA Z q) (hj : j.val < 512) (hal : Aligned (peAF q j)) + (sl : Fl4SpecA64.ExtSlot) (pte : Fl4SpecA64.PTE) + {x : Fl4SpecA64.Tcb} (hk : σ.kheap σ.curThread = some (.tcb x)) + {s2 s3 : S} {a3 : A} {σ2 : Fl4SpecA64.State Unit} + (hsc : obj.Slot.set_cap st (cslotAddrA cte) (L.wr s (stA (L.curW s)) sched.TS_RESTART) c = ok s2) + (hσ2 : ((), σ2) ∈ (Fl4SpecA64.setCap (Z := Unit) (.archObjectCap newSC) cte + (stUpdA σ σ.curThread x .restart)).1) + (hwP : ExtWordWriteA (st := st) s2 s3 (peAF q j) w) + (J3 : JAtZAX (L := L) AR Z s3 a3 (extSlotWriteX σ2 sl pte) m) (hsl : ExtSlotOkX σ2 sl) + (ev : ∀ (Pt Pp' Tb' Hr Te Kt : Set U64) (Lv' Re bits : U64 → Nat), + S1aR2 st H OS Pt Pp' Tb' Hr Te Kt Lv' Re bits nA s → + (Ub : Set U64) → VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt → + s3 = s ∨ ∃ tag f, S1aFrameMap st OS Pt Pp' nA tag s s3 (cslotAddrA cte) f) + (hunm : ∀ s1 : S, OSKeep st OS s s1 → L.ksb s1 = L.ksb s → L.cpu s1 = L.cpu s → + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s1 a + (stUpdA σ σ.curThread x .restart) m → + ∀ Pt : Set U64, (∀ p, SchedPageA st Z OS s1 p → p ∈ Pt) → + (∀ u ∈ OS, ∀ c, capA st s3 u = c → (∀ p sz r d mp e a v, c ≠ .Frame p sz r d mp e a v) → capA st s1 u = c) → + (∀ p ∈ Pt, ∀ i : U64, i.val < 512 → peAF p i ≠ peAF q j → L.mem s3 (peAF p i) = L.mem s1 (peAF p i)) → + L.ksb s3 = L.ksb s1 → L.cpu s3 = L.cpu s1 → + (∀ p ao, p ≠ extSlotObjX sl → (stUpdA σ σ.curThread x .restart).kheap p = some (.archObj ao) → + (extSlotWriteX σ2 sl pte).kheap p = some (.archObj ao)) → + UnmPtSpecA st Z m OS s3 (extSlotWriteX σ2 sl pte)) + (fin : ∃ Ac' : Set U64, SysL st H Z.Cs OS ia ie s3 ∧ SymL st H Z.Cs s3 ∧ + BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac' s3 ∧ + Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s3 ∧ + J7A st (J7R2A st ar nA) Z H OS s3 (extSlotWriteX σ2 sl pte) m) : + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + (([] : List Fl4SpecA64.Data), extSlotWriteX σ2 sl pte) ∈ (Fl4SpecA64.performExtVSpace (Z := Unit) + (.map newSC cte pte sl) (stUpdA σ σ.curThread x .restart)).1 ∧ + ∃ Ac' : Set U64, JStage3A st AR Z H OS U Ac' Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s3 a3 (extSlotWriteX σ2 sl pte) m ∧ + Sched.BodyKeepA (stUpdA σ σ.curThread x .restart) (extSlotWriteX σ2 sl pte) := by + have hx : cslotAddrA cte ∈ OS := RS.sub _ (hab.isSlot J.j.1.place) + have hO : OSOK H.tcbs H.omega H.frame OS := J.sys.ent.os + have E0 := J.toJEntA + obtain ⟨x', hk', -, hset, J1, -, hOS1, -⟩ := J.of_restartCurA RS.pl.choose_spec.choose_spec.choose_spec.choose_spec + RS.sym RS.run hnvm RS.cur RS.vs RS.vc + obtain rfl : x' = x := by + rw [hk] at hk' + injection hk' with h + injection h with h + exact h.symm + have hperf : (([] : List Fl4SpecA64.Data), extSlotWriteX σ2 sl pte) ∈ (Fl4SpecA64.performExtVSpace (Z := Unit) + (.map newSC cte pte sl) (stUpdA σ σ.curThread x' .restart)).1 := by + unfold Fl4SpecA64.performExtVSpace + have hst : (Fl4SpecA64.storeExtSlot (Z := Unit) sl pte >>= fun _ => pure ([] : List Fl4SpecA64.Data)) σ2 = + ({([], extSlotWriteX σ2 sl pte)}, False) := by + rw [nd_bind_oneD (storeExtSlot_runX hsl pte)]; rfl + exact ⟨((), σ2), hσ2, by dsimp only; rw [hst]; rfl⟩ + refine ⟨hset, hperf, ?_⟩ + have hks : L.ksb (L.wr s (stA (L.curW s)) sched.TS_RESTART) = L.ksb s := L.ksb_wr _ _ _ + have hcpu : L.cpu (L.wr s (stA (L.curW s)) sched.TS_RESTART) = L.cpu s := L.cpu_wr _ _ _ + have hs1m : ∀ b, Aligned b → b ≠ stA (L.curW s) → L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART) b = L.mem s b := + fun b hb hn => by + have : WritesIn st s (L.wr s (stA (L.curW s)) sched.TS_RESTART) (fun w => w = stA (L.curW s)) := by + refine writesIn_wr ?_ + have hc := J.sr.core.tcbs _ J.sr.curT + exact field_aligned hc (by simp) (by simp) + exact this.mem_eq hb hn + generalize L.wr s (stA (L.curW s)) sched.TS_RESTART = s1 at hsc J1 hOS1 hks hcpu hs1m + have c1 : capA st s1 (cslotAddrA cte) = .Frame fp sz rt dev mp e0 asid0 va0 := (hOS1.capA hx).trans hcap + have c1' : capAtA (L.mem s1) (cslotAddrA cte) = .Frame fp sz rt dev mp e0 asid0 va0 := c1 + have hTr : TreeA st OS Z.G.chains Z.G.par s1 := + J1.j.1.cdt.tree.grow J1.j5.2.1 (fun x hx hn => J1.j5.2.2 x hx hn) + have hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s1 y = .Null := J1.j5.2.2 + have E1 := J1.toJEntA + have hN := bxi_nodes E1.sys.ent.bxi + obtain ⟨mp', e', a', v', ec, hbn⟩ := hc + have hM : MapAlikeA c (.Frame fp sz rt dev mp e0 asid0 va0) := by rw [ec]; exact .frame .. + have hcn : c ≠ .Null := by rw [ec]; intro h; cases h + have hirq : CapOkA (stUpdA σ σ.curThread x' .restart).interruptIrqNode cte (.archObjectCap newSC) := by + refine ⟨fun irq hlt e => ?_, fun ⟨_, _, _, _, e⟩ => by cases e⟩ + have obj : ∃ o ac, (stUpdA σ σ.curThread x' .restart).kheap cte.1 = some o ∧ + Fl4SpecA64.capOf o cte.2 = some ac ∧ CapRelA (capAtA (L.mem s1) (cslotAddrA cte)) ac := by + obtain ⟨o, idx⟩ := cte + obtain ⟨p, e1, hc | ⟨ht, i, hi, e2⟩⟩ := hab + · simp only at e1 hc; subst e1 + obtain ⟨cs, hk, -, hr⟩ := J1.j.1.st.cnode p _ hc + obtain ⟨ac, hac, hrel⟩ := hr idx rfl + exact ⟨_, ac, hk, hac, hrel⟩ + · simp only at e1 e2; subst e1 e2 + obtain ⟨y, hk, hr⟩ := J1.j.1.st.tcb p ht + obtain ⟨ac, hac, hrel⟩ := hr i hi + refine ⟨_, ac, hk, hac, ?_⟩ + unfold cslotAddrA; rw [ofBl_tcbIdx i (by unfold tcbSlotsA at hi; omega)]; exact hrel + obtain ⟨o, ac, hko, hco, hrel⟩ := obj + rw [e] at hko + rw [c1'] at hrel + rcases J1.sinv.irqN.slot irq hlt o _ ac hko hco with rfl | ⟨_, _, _, rfl⟩ + · cases e0 <;> cases hrel.2 + · cases e0 <;> cases hrel.2 + have hsci : NoIdleCapV m n (.archObjectCap newSC) := + ⟨fun _ _ e => (by cases e), fun _ e => (by cases e), fun pp aa vv e => by + subst ec + have h2 : absCapA (.Frame fp sz rt dev mp' e' a' v') = .archObjectCap newSC := hca.2 + cases e + cases e' <;> simp [absCapA, absFrameE] at h2⟩ + have al_x : Aligned (cslotAddrA cte) := hO.al0 hx + have al_x8 : Aligned (wa (cslotAddrA cte) 8#u64) := hO.al hx (k := 8#u64) (by simp) (by simp) + have wS := writesIn_setCapA al_x al_x8 hsc + have wP : WritesIn st s2 s3 (fun b => b = peAF q j) := + ⟨hwP.1, hwP.2.1, fun b hb hn => by rw [hwP.2.2 b hb, if_neg (fun h => hn h.symm)]⟩ + have hk31 : L.ksb s3 = L.ksb s1 := wP.1.trans wS.1 + have hcpu31 : L.cpu s3 = L.cpu s1 := wP.2.1.trans wS.2.1 + have hk30 : L.ksb s3 = L.ksb s := hk31.trans hks + -- the record's typing, from P1's exact frame map at `s` + have vsT : VsTyR st (L.ksb s3) (PoolSet st Pp s3) ∅ Tb Lv s3 ∧ + utVsPtA st Pp Tb Lv s3 ⊆ utVsPtA st Pp Tb Lv s := by + have hty : VsTyR st (L.ksb s) (PoolSet st Z.Pp s) ∅ Tb Lv s := by rw [J.pp]; exact J.sys.ent.vs + obtain ⟨Pt, Ub, vr, -, -, -, -, -, -, -, -, -, -, -, r2⟩ := J.j7 + obtain ⟨PpJ, TbJ, HrJ, TeJ, KtJ, LvJ, ReJ, bitsJ, R2J, -, -⟩ := r2 + rcases ev Pt PpJ TbJ HrJ TeJ KtJ LvJ ReJ bitsJ R2J Ub vr with e | ⟨tag, f, event⟩ + · subst e; exact ⟨J.sys.ent.vs, Set.Subset.refl _⟩ + have hpools : PoolSet st Z.Pp s ⊆ PoolSet st PpJ s := fun p hp => by + obtain ⟨-, o, h1, h2, h3, h4, h5⟩ := hp + rw [← h4] + exact R2J.core.stage1.tbl o h1 h2 h3 (by rw [h4]; exact h5) + obtain ⟨-, -, -, kinds, -, -⟩ := J.sys.sysl.sys.stz.1.1.mdbS + have ptKs := J.ko.ptAsWG R2J.footprint J.sys.ent.ksw (R2J.footprint.fit J.sys.ent.heap) + obtain ⟨-, a1, a2⟩ := vsTyR_frameMapA R2J J.sys.ent.heap kinds ptKs hty hpools event + rw [J.pp] at a1 a2 + exact ⟨a1, a2⟩ + have main := J1.of_capVsFrameA (hpt := fun _ _ _ h => by rw [ec] at h; cases h) + (Q := fun s' a' σ'' hw => s' = s3 ∧ a' = a3 ∧ σ'' = extSlotWriteX σ2 sl pte ∧ hw = Z.hw) + RS.sub (footNOff_ksA rfl rfl (footN_offA E1)) (footNAl_ksA rfl rfl (FootNA.aligned E1.j.1 hN E1.scAl E1.ap.vcAl)) + cte hab hEnc hca hTr hnul hcn (by rw [c1]; exact fun h => by cases h) + (c := c) (by rw [c1]; exact hM) hok hirq hsci + (fun s2' σ2' hs2 hσ2' HSs w' jx hcx => by + obtain rfl : s2' = s2 := okinj (hs2.symm.trans hsc) + obtain rfl : σ2' = σ2 := setCap_detA hσ2' hσ2 + obtain ⟨Ac', hsys, hsy, hbx, hus, hj7⟩ := fin + obtain ⟨HS2, hdp⟩ := extWrite_vsA σ2' sl pte + obtain ⟨Pt, Ub, vr, -, -, -, -, -, -, link, -, -⟩ := J1.j7 + obtain ⟨Qb, Rb, q0⟩ := J1.sys.ent.bxi.1 + have vo : VrOK H.omega H.frame H.scs H.scBits OS Z.V ∅ (L.ksb s1) (utVsPtA st Pp Tb Lv s1) := J1.vsOk + have Psp : ∀ b, b = peAF q j → VsR (L.ksb s) Pt b := fun b hb => by + rw [hb, ← hks]; exact vsW_pageA link hq.sched hj + have Psp1 : VsR (L.ksb s1) Pt (peAF q j) := vsW_pageA link hq.sched hj + have osk3 : OSKeep st OS s2' s3 := osKeep_ofA hO fun b hb hin => + wP.mem_eq hb (fun h => vr.os b (by rw [h]; exact Psp1) hin) + obtain ⟨hcx', hco⟩ := capA_setCapA hO hx hEnc hs2 + have capE3 : ∀ u ∈ OS, capA st s3 u = capA st s2' u := fun u hu => osk3.capA hu + have hbk : ∀ u ∈ OS, ∀ c, capA st s3 u = c → (∀ p sz r d mp e a v, c ≠ .Frame p sz r d mp e a v) → + capA st s1 u = c := fun u hu c0 hc0 hn => by + by_cases e : u = cslotAddrA cte + · subst e + rw [capE3 _ hx, hcx', ec] at hc0 + exact absurd hc0.symm (hn _ _ _ _ _ _ _ _) + · rw [capE3 u hu, hco u hu e] at hc0 + exact hc0 + have hMs2 : ∀ u ∈ OS, MapAlikeA (capA st s2' u) (capA st s1 u) := fun u hu => by + by_cases e : u = cslotAddrA cte + · subst e; rw [hcx', c1]; exact hM + · rw [hco u hu e]; exact .refl _ + have sbS : ∀ t ∈ Z.T, sbWordA t ∈ OS := fun t ht => + RS.sub _ ⟨t, Or.inr ⟨(J1.j.1.thT t).2 ht, 2, by simp [tcbSlotsA], (bufSlot_eqA t).symm⟩⟩ + have wA : WritesIn st s1 s2' (fun b => Aligned b ∧ W7.CapW H.tcbs {b | InOS OS b} ∅ b) := writesIn_alAG w' + have hB : ∀ b, (Aligned b ∧ W7.CapW H.tcbs {b | InOS OS b} ∅ b) → W7.CapW H.tcbs H.omega H.frame b := + fun b hb => os_capw hO hb.1 hb.2 + obtain ⟨-, -, -, -, -, fld, -, -⟩ := W7.blk_of_capw (BXI.blk J1.sys.ent.bxi) wA hB + have ib : ∀ t ∈ Z.T, L.mem s2' (ibWordA t) = L.mem s1 (ibWordA t) := fun t ht => + fld t (by rw [J1.tcbs]; exact ht) 3512#u64 (by simp) (by simp) (by simp) + have tw : ∀ t ∈ Z.T, ∀ K : U64, K.val + 8 ≤ 4096 → K.val % 8 = 0 → + L.mem s3 (wa t K) = L.mem s2' (wa t K) := by + intro t ht K hK' h8 + have hta := J1.sr.core.tcbs t ht + have hv : (wa t K).val = t.val + K.val := field_val hta (K := K) (by omega) + refine wP.mem_eq (field_aligned hta (by omega) h8) (fun h => ?_) + have hth : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + have := (q0.omega (wa t K) (vr.om (wa t K) (by rw [h]; exact Psp1))).2.1 t hth + omega + have hib : ∀ t ∈ Z.T, IbSame (L := L) s2' s3 t := fun t ht => by + refine ⟨tw t ht _ (by simp) (by simp), ?_, ?_⟩ + · rw [sbWordA_eq]; exact tw t ht _ (by simp) (by simp) + · rw [sbWordA8_eq]; exact tw t ht _ (by simp) (by simp) + have hj7' := hj7 + obtain ⟨Pt', Ub', vr', -, ac', -⟩ := hj7' + have hBw : ∀ b, (SlotWordA (cslotAddrA cte) b ∨ b = peAF q j) → + L.Free s1 H.omega Z.Cs b ∨ W7.TLow H.tcbs b := fun b hb => by + rcases hb with hsl' | hb + · have hal' : Aligned b := by rcases hsl' with rfl | rfl; exact al_x; exact al_x8 + exact (os_capw hO hal' (Or.inr ⟨slotWord_inOS hO hx hsl', Set.notMem_empty _⟩)).elim Or.inr + fun h' => Or.inl (Or.inl h'.1) + · exact Or.inl (Or.inl (vr.om b (by rw [hb]; exact Psp1))) + have hsp : ∀ p, SchedPageA st Z OS s3 p → SchedPageA st Z OS s1 p := fun p h => SchedPageA.backA hbk h + have hword : ∀ p ∈ Pt, ∀ i : U64, i.val < 512 → peAF p i ≠ peAF q j → + L.mem s3 (peAF p i) = L.mem s1 (peAF p i) := fun p hp i hi hne => by + have hv : VsR (L.ksb s1) Pt (peAF p i) := Or.inl ⟨p, hp, core.num.U64.wrapping_mul i 8#u64, + (wm8_offA hi).1, (wm8_offA hi).2, rfl⟩ + have hbal : Aligned (peAF p i) := vr.al _ hv + have hns : ¬ SlotWordA (cslotAddrA cte) (peAF p i) := fun h => vr.os _ hv (slotWord_inOS hO hx h) + exact (wP.mem_eq hbal hne).trans (wS.mem_eq hbal hns) + have hset' : ∀ p ao, p ≠ extSlotObjX sl → (stUpdA σ σ.curThread x' .restart).kheap p = some (.archObj ao) → + (extSlotWriteX σ2' sl pte).kheap p = some (.archObj ao) := fun p ao hp h => by + rw [kheap_writeX hp] + exact HSs.keep h (fun _ _ e => by cases e) (fun _ e => by cases e) + refine ⟨s3, a3, extSlotWriteX σ2' sl pte, Z.hw, fun b => b = peAF q j, Ac', + ⟨wP, fun Pt'' Ub'' vr'' link'' b h => by rw [h]; exact vsW_pageA link'' hq.sched hj, + fun b h hbuf => J1.bufOff.page b hbuf q hq.sched (core.num.U64.wrapping_mul j 8#u64) + (by rw [wm8_valA hj]; omega) (by rw [wm8_valA hj]; omega) (by rw [h]; rfl), + HS2, rfl, hdp, J3, ⟨⟨J1.sys.ent.heap, hbx, hO, hus, vsT.1, by rw [hk31]; exact J1.sys.ent.ksw⟩, hsys⟩, + UtVsOkA.zeroStepA hk30 vsT.2 J.vsOk, bufVs_subA J.bufVs hk30 vsT.2, W7.inv_of_capw J1.sq (wS.trans wP) hBw, ac'.case.po, + ?_, ?_, ?_, ?_, hj7, hsy⟩, rfl, rfl, rfl, rfl⟩ + · exact BufCover.vsA (BufCover.mapA J1.bufCover J1.bufNative J1.j.1.st jx.1.st (fun t ht => (J1.j.1.thT t).2 ht) + HSs ib (fun t ht => hMs2 _ (sbS t ht))) HS2 hib + · exact BufNativeA.mapA J1.bufNative (fun t ht q' sz' rr dv' m' e'' a'' v'' h => by + by_cases e1 : sbWordA t = cslotAddrA cte + · rw [e1, capE3 _ hx, hcx', ec] at h + cases h + exact hbn.resolve_right (fun k => k t ht e1) + · rw [capE3 _ (sbS t ht), hco _ (sbS t ht) e1] at h + have hn := J1.bufNative t ht + rw [bufSlot_eqA, capAtA_eq (st := st), h] at hn + exact hn) + · exact hunm s1 hOS1 hks hcpu J1 Pt link hbk hword hk31 hcpu31 hset' + · exact ⟨J1.bufOff.os, fun b hb => by rw [hk31]; exact J1.bufOff.vs b hb, + fun b hb p hp => J1.bufOff.page b hb p (hsp p hp), J1.bufOff.vcw, + fun bp hbp => (J1.bufOff.pin bp hbp).keepM fun u hu => by rw [capE3 u hu]; exact hMs2 u hu⟩) + obtain ⟨_, _, s3', a3', σ3', hw, P, Ac', -, hσk, HSsk, HSvk, -, -, Jf, e1, e2, e3, e4⟩ := main + subst e1 e2 e3 e4 + exact ⟨Ac', Jf, (Sched.BodyKeepA.ofSlot HSsk (setCap_keepsA hσk).2.2).trans (Sched.BodyKeepA.ofVs HSvk)⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3MapShapeA.lean b/proofs/Fl4ProofsA64/P3/Stage3MapShapeA.lean new file mode 100644 index 000000000..1394b32c0 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3MapShapeA.lean @@ -0,0 +1,404 @@ +/- +P3 (AArch64, merge line, D8 step 5 (ii), the map arms): **`page_map_shapeA`**, the success path of the S3-owned +`page_map_refinesGA` (`P3/VsMapPageA`, not edited; same hypotheses) exporting its intermediate state: when FL4's +Stage-1 `ARMPageMap` decode succeeds, the restart and cap write (`restart_setcapJA`'s outputs), the looked-up table `p` +and index `i`, the descriptor `e` and its spec entry (`PteRelA`), the final memory, the spec's `storePte` and +`performPageInvocation`. Reusable shape lemma. Generated by `handoff/tools/gen_mapshape.py`. No named premise. +-/ +import Fl4ProofsA64.P3.Stage3MapStoreA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-- The state an arch-op chain returns. -/ +theorem FlA.ok_stateA {A B C : Type} {ar : machine.Arch A} {a : A} {r : B} {s s' : C} {a3 : A} {r' : B} + (h : FlA ar (fun a' => (r, s, a')) a (ok (r', s', a3))) : r' = r ∧ s' = s := by + generalize hm : (ok (r', s', a3) : Result (B × C × A)) = m at h + induction h with + | ret a => + have := ok_inj_ff hm + simp only [Prod.mk.injEq] at this + exact ⟨this.1, this.2.1⟩ + | inv a asid va k _ ih => + obtain ⟨y, -, hy⟩ := bind_eq_ok hm.symm + exact ih y hy.symm + | cache a op root asid va n pa k _ ih => + obtain ⟨y, -, hy⟩ := bind_eq_ok hm.symm + exact ih y hy.symm + +section top +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + + +/-- What a successful Stage-1 frame map did (module docstring). -/ +def MapShapeA (AR : A → HwRootA → Prop) (Z : XSetsA) (OS : Set U64) (sl fp rt : U64) (sz : capability.FrameSize) + (dev : Bool) (m : Fl4SpecA64.McState) (σ : Fl4SpecA64.State Unit) (cte : Fl4SpecA64.CslotPtr) + (D : Set (Except Fl4SpecA64.SyscallError Fl4SpecA64.ArchInvocation × Fl4SpecA64.State Unit) × Prop) + (s s' : S) (a : A) : Prop := + ∃ iv, D = ({(.ok (.invokePage iv), σ)}, False) ∧ + ∃ (x : Fl4SpecA64.Tcb) (s2 : S) (σ2 : Fl4SpecA64.State Unit) (asid va p i e : U64) (pte : Fl4SpecA64.PTE) + (σ3 σ' : Fl4SpecA64.State Unit), + σ.kheap σ.curThread = some (.tcb x) ∧ + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + obj.Slot.set_cap st sl (L.wr s (stA (L.curW s)) sched.TS_RESTART) + (.Frame fp sz rt dev true false asid va) = ok s2 ∧ + ((), σ2) ∈ (Fl4SpecA64.setCap (Z := Unit) (.archObjectCap (.frameCap (specPtr fp) (vmRightsOfA rt) + (frameSizeOfA sz) dev (some (asidOfA asid, va.bv)))) cte (stUpdA σ σ.curThread x .restart)).1 ∧ + SpecSlotA (stUpdA σ σ.curThread x .restart) σ2 ∧ + CapRt.EncWf (.Frame fp sz rt dev true false asid va) ∧ + CapRelA (.Frame fp sz rt dev true false asid va) (.archObjectCap (.frameCap (specPtr fp) (vmRightsOfA rt) + (frameSizeOfA sz) dev (some (asidOfA asid, va.bv)))) ∧ + L.ksb s2 = L.ksb s ∧ L.cpu s2 = L.cpu s ∧ + VsRelA Z.Pp Z.S1 Z.Lv Z.X Z.Y (L.mem s2) (L.ksb s2) σ2 ∧ + (TreeA st OS Z.G.chains Z.G.par s → (∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) → + (.Frame fp sz rt dev true false asid va : capability.Cap) ≠ .Null → capA st s sl ≠ .Null → + UA (.Frame fp sz rt dev true false asid va) (capA st s sl) → + CapObjOkA Z (.Frame fp sz rt dev true false asid va) → + JAtZAX (L := L) AR Z s2 a σ2 m ∧ TreeA st OS Z.G.chains Z.G.par s2) ∧ + SlotFrA (L := L) OS sl (.Frame fp sz rt dev true false asid va) s s2 ∧ + VaOkE va ∧ p ∈ Z.S1 ∧ VsReachA Z.Pp Z.S1 Z.Y (L.mem s) (L.ksb s) p ∧ i.val < 512 ∧ 0 < Z.Lv p ∧ Z.Lv p ≤ 3 ∧ + PteRelA Z.S1 Z.Lv (Z.Lv p) e pte ∧ (∃ pa sm att rg, pte = .pagePTE pa sm att rg) ∧ + ¬ AsW (L.ksb s2) (peAF p i) ∧ + L.ksb s' = L.ksb s2 ∧ L.cpu s' = L.cpu s2 ∧ + (∀ w, Aligned w → L.mem s' w = L.mem (L.wr s2 (peAF p i) e) w) ∧ + (∃ t', σ3 = ptSetA σ2 (specPtr p) t') ∧ MsA σ3 σ' ∧ + (([] : List Fl4SpecA64.Data), σ') ∈ + (Fl4SpecA64.performPageInvocation (Z := Unit) iv (stUpdA σ σ.curThread x .restart)).1 ∧ + VsRelA Z.Pp Z.S1 Z.Lv Z.X Z.Y (L.mem s') (L.ksb s') σ' ∧ VsSpecA σ2 σ' + +/-- `MapShapeA` whenever the decode succeeds. -/ +def PgShapeA (ar : machine.Arch A) (AR : A → HwRootA → Prop) (Z : XSetsA) (OS : Set U64) (sl fp rt : U64) + (sz : capability.FrameSize) (dev : Bool) (m : Fl4SpecA64.McState) (σ : Fl4SpecA64.State Unit) + (cte : Fl4SpecA64.CslotPtr) + (D : Set (Except Fl4SpecA64.SyscallError Fl4SpecA64.ArchInvocation × Fl4SpecA64.State Unit) × Prop) + (s : S) (a : A) (r : Result (core.result.Result Unit error.Exc × S × A)) : Prop := + ∀ s' a3, r = ok (core.result.Result.Ok (), s', a3) → + FlA ar (fun a' => ((.Ok () : core.result.Result Unit error.Exc), s', a')) a r ∧ + MapShapeA (L := L) AR Z OS sl fp rt sz dev m σ cte D s s' a + +theorem PgShapeA.errA {ar : machine.Arch A} {sl fp rt : U64} {sz : capability.FrameSize} {dev : Bool} + {cte : Fl4SpecA64.CslotPtr} + {D : Set (Except Fl4SpecA64.SyscallError Fl4SpecA64.ArchInvocation × Fl4SpecA64.State Unit) × Prop} + {e : error.Exc} {s1 : S} {a1 : A} : + PgShapeA (L := L) ar AR Z OS sl fp rt sz dev m σ cte D s a (ok (.Err e, s1, a1)) := by + intro s' a3 h + have := ok_inj_ff h + simp only [Prod.mk.injEq, reduceCtorEq, false_and] at this + +theorem PgShapeA.okA {ar : machine.Arch A} {sl fp rt : U64} {sz : capability.FrameSize} {dev : Bool} + {cte : Fl4SpecA64.CslotPtr} + {D : Set (Except Fl4SpecA64.SyscallError Fl4SpecA64.ArchInvocation × Fl4SpecA64.State Unit) × Prop} + {s1 : S} {r : Result (core.result.Result Unit error.Exc × S × A)} + (h : FlA ar (fun a' => ((.Ok () : core.result.Result Unit error.Exc), s1, a')) a r) + (hs : MapShapeA (L := L) AR Z OS sl fp rt sz dev m σ cte D s s1 a) : + PgShapeA (L := L) ar AR Z OS sl fp rt sz dev m σ cte D s a r := by + intro s' a3 e + subst e + obtain ⟨-, rfl⟩ := h.ok_stateA + exact ⟨h, hs⟩ + +set_option maxHeartbeats 4000000 in +/-- **The success path of the Stage-1 `ARMPageMap`** (module docstring). -/ +theorem page_map_shapeA (ar : machine.Arch A) (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) + (cte : Fl4SpecA64.CslotPtr) (hab : AbsSlotA Z.th cte) (hos : cslotAddrA cte ∈ OS) + {fp asid0 va0 rt : U64} {sz : capability.FrameSize} {dev mp : Bool} + (hcap : capAtA (L.mem s) (cslotAddrA cte) = .Frame fp sz rt dev mp false asid0 va0) + (FP : FrPlA sz fp mp asid0) + (slbl : Fl4SpecA64.Data) (hl : Fl4SpecA64.invocationType slbl = .archInvocationLabel .armPageMap) + (length buf : U64) (args : List U64) (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) (call : Bool) + (x0 : U64) (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) + (exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) (hex : ExtraRelA Z.th (L.mem s) x0 (exs.map Prod.fst)) + (hS1 : ∀ p l mp' asid va, capAtA (L.mem s) x0 ≠ .Ept p l mp' asid va) + (hHi : ∀ p mp' asid, capAtA (L.mem s) x0 ≠ .HighVSpace p mp' asid) : + PgShapeA (L := L) ar AR Z OS (cslotAddrA cte) fp rt sz dev m σ cte ((Fl4SpecA64.decodeFrameInvocation (Z := Unit) slbl (args.map UScalar.bv) cte + (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev (mapDataA mp asid0 va0)) exs).run σ) s a + (vspace.decode_frame st ar s a fl4_abi.label.ARMPageMap length (cslotAddrA cte) + (.Frame fp sz rt dev mp false asid0 va0) call buf extra) := by + rw [frInv_mapA hl] + have hct : sched.cur_thread st s = ok (L.curW s) := cur_thread_ok s + -- the invoked cap's facts + obtain ⟨c0, -, hrel0⟩ := StateRelA.getAbsA E.j.1.st hab + rw [hcap] at hrel0 + obtain ⟨⟨hr1, hr3, ⟨h16a, -⟩, hov⟩, -⟩ := hrel0 + have hEnc0 : CapRt.EncWf (capAtA (L.mem s) (cslotAddrA cte)) := CapRt.decodeF_encWf _ _ + rw [hcap] at hEnc0 + obtain ⟨hcanP, -, -, hcanV⟩ := hEnc0 + unfold vspace.decode_frame vspace.decode_page_map + simp only [vspace.as_frame, hct, bind_ok, eq_self_iff_true, ↓reduceIte, extra0_eqA hx0] + rcases hex with ⟨rfl, hnil⟩ | ⟨hx0n, -, vc', vrest, hvc', hmap⟩ + · have he : exs = [] := List.map_eq_nil_iff.1 hnil + subst he + simp only [↓reduceIte, error.truncated, bind_ok] + exact PgShapeA.errA + rw [if_neg hx0n] + obtain ⟨⟨vc'', xsl⟩, rest, rfl⟩ : ∃ e rest, exs = e :: rest := by + cases exs with + | nil => simp at hmap + | cons e rest => exact ⟨e, rest, rfl⟩ + simp only [List.map_cons, List.cons.injEq] at hmap + obtain ⟨rfl, -⟩ := hmap + dsimp only + by_cases h3 : length >= 3#u64 + swap + · rw [if_neg h3] + simp only [error.truncated, bind_ok] + exact PgShapeA.errA + rw [if_pos h3] + have h3' : 3 ≤ args.length := by + have : 3 ≤ length.val := by scalar_tac + omega + obtain ⟨a0, a1, a2, r', rfl⟩ : ∃ a0 a1 a2 r', args = a0 :: a1 :: a2 :: r' := by + match args, h3' with + | a0 :: a1 :: a2 :: r', _ => exact ⟨_, _, _, _, rfl⟩ + simp only [List.map_cons] + obtain ⟨vm, hvm, hvmr⟩ := spec_ex_fd (mask_vmA rt (rightsWA a1)) + rw [hargs 0#u64 a0 rfl, hargs 1#u64 a1 rfl, hargs 2#u64 a2 rfl] + simp only [bind_ok, from_word_eqA, hvm, capA_eq] + simp only [capAtA_eq] at hvc' hS1 hHi + generalize capA st s x0 = vc at hvc' hS1 hHi ⊢ + have nonroot : ∀ (vc : capability.Cap), CapRelA vc vc'' → (∀ r asid, vc ≠ .Pml4 r true asid) → + (∀ p l mp asid va, vc ≠ .Ept p l mp asid va) → (∀ p mp asid, vc ≠ .HighVSpace p mp asid) → + (Fl4SpecA64.decodeFrInvMap (Z := Unit) slbl (a0.bv :: a1.bv :: a2.bv :: List.map UScalar.bv r') cte + (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev (mapDataA mp asid0 va0)) + ((vc'', xsl) :: rest)).run σ = ({(.error (.invalidCapability 1), σ)}, False) := fun vc hr h1 h2 h3 => by + exact frDec_noRootA _ _ _ _ _ _ _ _ _ _ _ _ _ _ σ (by + cases vc with + | PageTable p m asid va => + cases m + · rw [← hr.2]; exact absCap_noRootA h1 h2 h3 + · obtain ⟨_, _, rfl, _, _⟩ := CapEqA.pt_mapped_iff.mp hr.2; intro _ _ _ h; cases h + | _ => rw [← hr.2]; exact absCap_noRootA h1 h2 h3) + have errNR : VsErrRelA (.Sys (.InvalidCapability 1#u64)) (.invalidCapability 1) := Or.inr ⟨1#u64, rfl, rfl⟩ + cases vc with + | Ept p l mp' asid va => exact absurd rfl (hS1 p l mp' asid va) + | HighVSpace p mp' asid => exact absurd rfl (hHi p mp' asid) + | Pml4 r0 mr asid1 => + cases mr + · simp only [bind_ok, Bool.false_eq_true, ↓reduceIte, vspace.check_vspace, error.invalid_cap, + core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame.from, + bind_tc_ok, ite_self] + exact PgShapeA.errA + -- the main path: a mapped Stage-1 root cap + have hwf : asid1.val < 2 ^ 16 := hvc'.1 + have hvc2 : vc'' = .archObjectCap (.pageTableCap (specPtr r0) .vsRootPtT + (some (asidOfA asid1, (0#u64 : U64).bv))) := hvc'.2.symm + subst hvc2 + simp only [bind_ok, Bool.false_eq_true, ↓reduceIte] + rw [frDec_bodyA] + have h16 : asid1.val < 65536 := lt_of_lt_of_eq hwf (by norm_num) + have R := E.j.1.vs + obtain ⟨res, hres, hpost⟩ := spec_ex_fd (check_vspaceGA R E.j.2.2.1.obj (r := r0) h16 + (frRestA (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev (mapDataA mp asid0 va0) (specPtr r0) + (asidOfA asid1) a0.bv a1.bv a2.bv cte)) + rw [hres] + rcases res with ⟨rr, aa⟩ | e + swap + · obtain ⟨e', he', hrel⟩ := hpost + simp only [bind_ok, core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame.from, + bind_tc_ok, ite_self, Bool.false_eq_true, ↓reduceIte] + exact PgShapeA.errA + obtain ⟨he, hrT, hrL, hasid1, hrun', hvRoot⟩ := hpost + simp only [Prod.mk.injEq] at he + obtain ⟨he1, he2⟩ := he + subst rr + subst aa + rw [hrun'] + have hps := psA_valA sz + have hs64 : (psA sz).val < 64 := by omega + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok, page_shift_eqA, psA_63A, uncurry_apply_pair, + Aeneas.Std.uncurry, lift, CapRt.shl_u64_eq 1#u64 (psA sz) hs64, ws1A (psA sz) hs64] + -- the lookup, at the spec's level + obtain ⟨lu, hlu, p, hp, l, hl3, hsh, hlv, hsl, -, hLs, hreach⟩ := + spec_ex_fd (lookup_pt_slotA R hrT hrL (vsReachA_of_vspaceForAsid R E.j.2.2.1.obj hrT hvRoot) a0) + rw [frRest_runA hLs, pbfs_eqA] + -- the alignment + by_cases hal0 : (a0 &&& (⟨BitVec.ofNat 64 (2 ^ (psA sz).val - 1)⟩ : U64)) = 0#u64 + swap + · have hna : ¬ Fl4SpecA64.isAligned a0.bv (psA sz).val := fun h => hal0 ((aligned_iffA a0 hs64).1 h) + simp only [hal0, bne_iff_ne, ne_eq, not_false_eq_true, ↓reduceIte, ite_self] + rw [if_pos hna] + exact PgShapeA.errA + have halg : Fl4SpecA64.isAligned a0.bv (psA sz).val := (aligned_iffA a0 hs64).2 hal0 + have hamod : a0.val % 2 ^ (psA sz).val = 0 := by + have := congrArg UScalar.val hal0 + rw [and_maskA a0 (psA sz).val _ (by + show (BitVec.ofNat 64 _).toNat = _ + rw [BitVec.toNat_ofNat] + rcases hps with e | e | e <;> rw [e] <;> norm_num)] at this + exact this + simp only [hal0, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + rw [if_neg (not_not.2 halg)] + by_cases hz : asid0 = 0#u64 + case' pos => + have hmp : mp = false := by + cases mp + · rfl + · exact absurd hz (FP.mz.1 rfl) + subst hmp + simp only [show (asid0 != 0#u64) = false by simp [hz], Bool.false_eq_true, ↓reduceIte] + have hlast := last_valA a0 sz hamod + have hc1 : ¬ frC1A (mapDataA false asid0 va0) (asidOfA asid1) := fun h => h + have hc2iff : frC2A (mapDataA false asid0 va0) a0.bv (frameSizeOfA sz) ↔ + wa a0 ⟨BitVec.ofNat 64 (2 ^ (psA sz).val - 1)⟩ > fl4_abi.consts.USER_TOP := by + show (a0.bv + Fl4SpecA64.mask (Fl4SpecA64.pageBitsForSize (frameSizeOfA sz)) > Fl4SpecA64.userVtop) ↔ _ + rw [mask_szA]; exact top_iffA _ + rw [if_neg hc1] + by_cases htop : wa a0 ⟨BitVec.ofNat 64 (2 ^ (psA sz).val - 1)⟩ > fl4_abi.consts.USER_TOP + · rw [if_pos htop, if_pos (hc2iff.2 htop)] + simp only [error.invalid_arg, bind_ok] + exact PgShapeA.errA + have hnl : ¬ wa a0 ⟨BitVec.ofNat 64 (2 ^ (psA sz).val - 1)⟩ < a0 := by + intro h; have h' : (wa a0 ⟨BitVec.ofNat 64 (2 ^ (psA sz).val - 1)⟩).val < a0.val := h + rw [hlast] at h'; omega + rw [if_neg htop, if_neg hnl] + have hc2 : ¬ frC2A (mapDataA false asid0 va0) a0.bv (frameSizeOfA sz) := fun h => htop (hc2iff.1 h) + have hcan : CapRt.Canonical a0 := by + have hU : fl4_abi.consts.USER_TOP.val = 140737488355327 := by unfold fl4_abi.consts.USER_TOP; rfl + have : ¬ fl4_abi.consts.USER_TOP.val < (wa a0 ⟨BitVec.ofNat 64 (2 ^ (psA sz).val - 1)⟩).val := htop + rw [hlast, hU] at this + exact canon_ltA (by omega) + have hva : VaOkE a0 := by + have hU : fl4_abi.consts.USER_TOP.val = 140737488355327 := by unfold fl4_abi.consts.USER_TOP; rfl + have : ¬ fl4_abi.consts.USER_TOP.val < (wa a0 ⟨BitVec.ofNat 64 (2 ^ (psA sz).val - 1)⟩).val := htop + rw [hlast, hU] at this + exact Or.inl (by omega) + rw [if_neg hc2] + case' neg => + have hmp : mp = true := FP.mz.2 hz + subst hmp + simp only [show (asid0 != 0#u64) = true by simpa using hz, ↓reduceIte, Bool.false_eq_true] + by_cases ha1 : asid0 = asid1 + swap + · have hc1 : frC1A (mapDataA true asid0 va0) (asidOfA asid1) := + show asidOfA asid0 ≠ asidOfA asid1 from fun e => ha1 (asidOf_injA h16a hwf e) + simp only [show (asid0 != asid1) = true by simpa using ha1, ↓reduceIte, error.invalid_cap, bind_ok] + rw [if_pos hc1] + exact PgShapeA.errA + subst asid0 + have hc1 : ¬ frC1A (mapDataA true asid1 va0) (asidOfA asid1) := fun h => h rfl + simp only [bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + rw [if_neg hc1] + by_cases hva : va0 = a0 + swap + · have hc2 : frC2A (mapDataA true asid1 va0) a0.bv (frameSizeOfA sz) := + show va0.bv ≠ a0.bv from fun e => hva (UScalar.eq_of_val_eq (congrArg BitVec.toNat e)) + simp only [show (va0 != a0) = true by simpa using hva, ↓reduceIte, error.invalid_arg, bind_ok] + rw [if_pos hc2] + exact PgShapeA.errA + subst va0 + have hc2 : ¬ frC2A (mapDataA true asid1 a0) a0.bv (frameSizeOfA sz) := fun h => h rfl + have hcan : CapRt.Canonical a0 := hcanV + have hva : VaOkE a0 := by + have h := E.j.1.cv _ (hab.isSlot E.j.1.place) + rw [hcap] at h + exact h rfl rfl + simp only [bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + rw [if_neg hc2] + all_goals + rw [hlu, bind_ok] + have hpb_iff : Fl4SpecA64.ptBitsLeft (lvA l) = (psA sz).val ↔ lu.shift = psA sz := by + rw [ptBitsLeft_lvA hl3, ← hsh] + exact ⟨fun e => UScalar.eq_of_val_eq e, fun e => by rw [e]⟩ + by_cases hls : lu.shift = psA sz + swap + · simp only [show (lu.shift != psA sz) = true by simpa using hls, ↓reduceIte] + rw [if_pos (fun e => hls (hpb_iff.1 e))] + exact PgShapeA.errA + simp only [hls, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + rw [if_neg (not_not.2 (hpb_iff.2 hls))] + rw [rights_readableA, bind_ok, + show layout.paddr fp = ok (core.num.U64.wrapping_sub fp layout.PPTR_BASE) from rfl, bind_ok] + obtain ⟨e, hme, he⟩ := spec_ex_fd (mup_specA (core.num.U64.wrapping_sub fp layout.PPTR_BASE) vm a2 (psA sz) + (decide (Fl4SpecA64.Rights.allowRead ∈ vmRightsOfA rt))) + rw [hme, bind_ok] + -- the restart and the cap write + have hEnc : CapRt.EncWf (.Frame fp sz rt dev true false asid1 a0) := by + simp only [CapRt.EncWf]; exact ⟨hcanP, by omega, by omega, hcan⟩ + have hca : CapRelA (.Frame fp sz rt dev true false asid1 a0) (.archObjectCap (.frameCap (specPtr fp) + (vmRightsOfA rt) (frameSizeOfA sz) dev (some (asidOfA asid1, a0.bv)))) := + ⟨⟨hr1, hr3, ⟨hwf, by simp, fun _ => hasid1, by simp⟩, hov⟩, rfl⟩ + obtain ⟨x, s2, σ2, hkx, hst, hset, hsc, hσ2, HS, hksb2, R2, hcpu2, -, hJ2, hFr2⟩ := + restart_setcapJA E RS cte hab hos hEnc hca + rw [hst, bind_ok, hsc, bind_ok] + -- the descriptor + obtain ⟨hKal, hS1A, -⟩ := E.vsLeafA + obtain ⟨tq, hkq, htq, -⟩ := R.pt p hp + have hidx := idxA_lt a0 lu.shift + have hlvp : Z.Lv p = 3 - l := by omega + have hl2 : l ≤ 2 := by + have := congrArg UScalar.val hls; rw [hsh] at this; omega + have hk1 : 0 < Z.Lv p := by omega + have hpg : decide (psA sz = 12#u64) = decide (Z.Lv p = 3) := by + have e12 : (12#u64 : U64).val = 12 := rfl + apply decide_eq_decide.2 + constructor + · intro e; have := congrArg UScalar.val (hls.trans e); rw [hsh, e12] at this; omega + · intro e; apply UScalar.eq_of_val_eq; rw [← hls, hsh, e12]; omega + have hsm : decide (frameSizeOfA sz = .armSmallPage) = decide (Z.Lv p = 3) := by + rw [← hpg]; cases sz <;> decide + have hshA : shiftA (Z.Lv p) = (psA sz).val := by + unfold shiftA; have := congrArg UScalar.val hls; rw [hsh] at this; omega + rw [hpg] at he + have hwr : PteRelA Z.S1 Z.Lv (Z.Lv p) e (Fl4SpecA64.makeUserPte + (Fl4SpecA64.addrFromPPtr (specPtr fp)) (Fl4SpecA64.attribsFromWord a2.bv) + (Fl4SpecA64.maskVmRights (vmRightsOfA rt) (Fl4SpecA64.dataToRights a1.bv)) (vmRightsOfA rt) + (frameSizeOfA sz)) := by + unfold Fl4SpecA64.makeUserPte + rw [addr_specA, hsm, ← rightsW_specA, ← hvmr] + exact pteRel_mupA hk1 (R.lv p hp) rfl he (by rw [hshA]; exact FP.al) FP.pa + have hA : ¬ AsW (L.ksb s2) (peAF p (idxA a0 lu.shift)) := by rw [hksb2]; exact hS1A p hp _ hidx + have hK2 : Aligned (L.ksb s2) := by rw [hksb2]; exact hKal + obtain ⟨-, σ3, hstore, R4, -⟩ := write_storePteA R2 hp hidx hwr hA hK2 + (fun _ h => by unfold Fl4SpecA64.makeUserPte at h; cases h) + obtain ⟨t', hσ3⟩ := storePte_eqA R2 hp hidx hstore + -- `install_page` + have hsla : Aligned lu.slot := by rw [hsl]; exact peAF_alignedA (R.al_mod hp) hidx + obtain ⟨s', hks', hcpu', hm', hfl⟩ := install_pageA (L := L) ar s2 a lu e r0 asid1 a0 a2 hsla + -- the spec's perform + have hkq1 : (stUpdA σ σ.curThread x .restart).kheap (specPtr p) = some (.archObj (.pageTable tq)) := + stUpd_keep hkx hkq (fun _ h => by cases h) + have hlt : Fl4SpecA64.levelType (lvA l) = ptTyA (Z.Lv p) := by rw [levelType_lvA hl3, hlvp] + have hpte := ptesOf_atA hkq1 (ptT := Fl4SpecA64.levelType (lvA l)) (by rw [htq, hlt]) (R.al_mod hp) hidx + rw [← hsl] at hpte + rw [← hsl] at hstore + rw [← hlt] at hstore + obtain ⟨σ', hσ', hms, hun⟩ := perfPgMap_unA (cap := .frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev + (some (asidOfA asid1, a0.bv))) rfl (getPte_runA hpte) hσ2 hstore + have R5 : VsRelA Z.Pp Z.S1 Z.Lv Z.X Z.Y (L.mem s') (L.ksb (L.wr s2 lu.slot e)) σ3 := by + rw [hsl] + refine R4.of_mem fun b hb => ?_ + rw [← hsl] + exact hm' b (R4.foot_alignedA (by rw [L.ksb_wr]; exact hK2) b hb) + have VR : VsRelA Z.Pp Z.S1 Z.Lv Z.X Z.Y (L.mem s') (L.ksb s') σ' := by + rw [hks', ← L.ksb_wr s2 lu.slot e] + exact R5.of_msA hms + refine PgShapeA.okA ((hfl _).bind (fun _ => rfl)) ⟨_, rfl, x, s2, σ2, asid1, a0, p, idxA a0 lu.shift, e, _, σ3, σ', + hkx, hset, hsc, hσ2, HS, hEnc, hca, hksb2, hcpu2, R2, hJ2, hFr2, hva, hp, hreach, hidx, hk1, by omega, hwr, + ⟨_, _, _, _, by unfold Fl4SpecA64.makeUserPte; rfl⟩, hA, hks', hcpu', + (fun w hw => by rw [hm' w hw, hsl]), ⟨_, hσ3⟩, hms, hσ', VR, by + obtain ⟨tw, hkw, -, -⟩ := R2.pt p hp + exact VsSpecA.of_ptStoreA hkw (hσ3 ▸ hun)⟩ + | _ => + simp only [bind_ok, Bool.false_eq_true, ↓reduceIte, vspace.check_vspace, error.invalid_cap, + core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame.from, + bind_tc_ok, ite_self] + exact PgShapeA.errA + +end top + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3MapStoreA.lean b/proofs/Fl4ProofsA64/P3/Stage3MapStoreA.lean new file mode 100644 index 000000000..ba892fa16 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3MapStoreA.lean @@ -0,0 +1,70 @@ +/- +P3 (AArch64, merge line, D8 step 5 (ii), the map arms): **l4v's `store_pte` followed by the map's machine steps is a +`VsSpecA` step** (`perfPgMap_unA`: the outcome of `perform_pg_inv_map` with the steps after the store `UnQ`-related; +`VsSpecA.of_ptStoreA`). Reusable. No named premise. New module. +-/ +import Fl4ProofsA64.P3.VsMapPageA +import Fl4ProofsA64.P3.Stage3UnmapSpecA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4Spec Fl4SpecA64 + +noncomputable section + +/-- `perfPgMap_memA` with the machine steps after the store `UnQ`-related. -/ +theorem perfPgMap_unA {σ1 σ2 σ3 : State Unit} {cap : ArchCap} {cte : CslotPtr} {pte old : PTE} {sl : ObjRef} + {lv : VmLevel} {asid : Asid} {v : VSpaceRef} (hmd : cap.acapMapData = some (asid, v)) + (h1 : getPte (Z := Unit) (levelType lv) sl σ1 = ({(old, σ1)}, False)) + (h2 : ((), σ2) ∈ (setCap (Z := Unit) (.archObjectCap cap) cte σ1).1) + (h3 : storePte (Z := Unit) (levelType lv) sl pte σ2 = ({((), σ3)}, False)) : + ∃ σ', (([] : List Data), σ') ∈ (performPageInvocation (Z := Unit) (.pageMap cap cte (pte, sl, lv)) σ1).1 ∧ + MsA σ3 σ' ∧ UnQ σ3 σ' := by + obtain ⟨-, ⟨⟨⟨⟩, σ4⟩, h4⟩, h4m⟩ := doMachineOp_okA (mop := cleanByVA_PoU sl (addrFromPPtr sl)) + (machineOpLift_okA _) σ3 + have hm4 : MsA σ3 σ4 := h4m _ h4 + have hu4 : UnQ σ3 σ4 := doMachineOp_unQ _ σ3 _ h4 + have hg : (old, σ1) ∈ (getPte (Z := Unit) (levelType lv) sl σ1).1 := by rw [h1]; exact Set.mem_singleton _ + have hs : ((), σ3) ∈ (storePte (Z := Unit) (levelType lv) sl pte σ2).1 := by rw [h3]; exact Set.mem_singleton _ + by_cases ho : old ≠ .invalidPTE + · obtain ⟨-, ⟨⟨⟨⟩, σ5⟩, h5⟩, h5m⟩ := invTlbVa_okA asid v σ4 + refine ⟨σ5, ?_, msA_trans _ _ _ hm4 (h5m _ h5), hu4.trans (invTlbVa_unQ asid v σ4 _ h5)⟩ + simp only [performPageInvocation, performPgInvMap, nd_bind_applyA, Set.mem_setOf_eq] + refine ⟨((), σ5), ⟨(old, σ1), hg, ((), σ2), h2, ((), σ3), hs, ((), σ4), h4, ?_⟩, Set.mem_singleton _⟩ + simp only [NonDet.when, hmd, NonDet.assertOpt] + rw [if_pos ho] + show ((), σ5) ∈ ((NonDet.pure (asid, v) >>= fun x => invalidateTlbByAsidVa (Z := Unit) x.1 x.2) σ4).1 + rw [nd_bind_applyA] + exact ⟨((asid, v), σ4), Set.mem_singleton _, h5⟩ + · refine ⟨σ4, ?_, hm4, hu4⟩ + simp only [performPageInvocation, performPgInvMap, nd_bind_applyA, Set.mem_setOf_eq] + refine ⟨((), σ4), ⟨(old, σ1), hg, ((), σ2), h2, ((), σ3), hs, ((), σ4), h4, ?_⟩, Set.mem_singleton _⟩ + simp only [NonDet.when, ho, ↓reduceIte] + exact Set.mem_singleton _ + +/-- **A page-table write then `UnQ` steps is a `VsSpecA` step.** -/ +theorem VsSpecA.of_ptStoreA {σ σ' : Fl4SpecA64.State Unit} {q : Fl4SpecA64.ObjRef} {t t' : Fl4SpecA64.PT} + (hk : σ.kheap q = some (.archObj (.pageTable t))) (h : UnQ (ptSetA σ q t') σ') : VsSpecA σ σ' := by + have h0 : QvA σ (ptSetA σ q t') := vmA_refl _ + refine VsSpecA.of_qvA (QvA.trans h0 h.1) (fun r => ?_) h.1.asidTable + have hs : ∀ a, (ptSetA σ q t').kheap r = some (.archObj a) → (∀ v, a ≠ .vcpu v) → + σ'.kheap r = (ptSetA σ q t').kheap r ∨ (∃ u u', (ptSetA σ q t').kheap r = some (.archObj (.pageTable u)) ∧ + σ'.kheap r = some (.archObj (.pageTable u'))) := fun _ _ _ => by + rcases h.2 r with e | ⟨u, u', a, b, -, -⟩ + · exact Or.inl e + · exact Or.inr ⟨u, u', a, b⟩ + by_cases hr : r = q + · subst hr + have e1 : (ptSetA σ r t').kheap r = some (.archObj (.pageTable t')) := by rw [ptSetA_kheap, if_pos rfl] + refine Or.inr ?_ + rcases h.2 r with e | ⟨u, u', -, b, -, -⟩ + · exact ⟨_, _, hk, e.trans e1, (fun _ e => by cases e), (fun _ e => by cases e)⟩ + · exact ⟨_, _, hk, b, (fun _ e => by cases e), (fun _ e => by cases e)⟩ + · have e1 : (ptSetA σ q t').kheap r = σ.kheap r := by rw [ptSetA_kheap, if_neg hr] + rcases h.2 r with e | ⟨u, u', a, b, -, -⟩ + · exact Or.inl (e.trans e1) + · exact Or.inr ⟨_, _, e1.symm.trans a, b, (fun _ e => by cases e), (fun _ e => by cases e)⟩ + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3MapUnmA.lean b/proofs/Fl4ProofsA64/P3/Stage3MapUnmA.lean new file mode 100644 index 000000000..a2fb2b101 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3MapUnmA.lean @@ -0,0 +1,142 @@ +/- +P3 (AArch64, merge line, D8 step 5 (ii), the map arms): **J9 (`UnmPtSpecA`) across one leaf write** into a reachable +Stage-1 table `p` of positive depth. + +* **`vsReach_bridgeA`**: two P1 VSpace typings of one state (pools included) agree on reach, tables and depths. It + carries the record's reach (`recReach_pathA`) over to J7's typing. +* **`UnmPtSpecA.ofLeafWrA`**: no unmapped object's word is the written word and no unmapped spec object is `p`'s: + a `PageTable` by the caller's `hJ` (J7 `VsLk.unmR1`), a root `Pml4` by depth, Stage-2 roots by `S2PgE`, high windows + by `VsRelA.ytb`. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3ArmFrUnmMA +import Fl4ProofsA64.P3.Stage3VsLeafA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +theorem vsReach_bridgeA {s : S} {kb : U64} {P1 P2 T1 T2 : Set U64} {L1 L2 : U64 → Nat} + (h1 : VsTyR st kb P1 ∅ T1 L1 s) (h2 : VsTyR st kb P2 ∅ T2 L2 s) (hs : P1 ⊆ P2) {t : U64} + (r : VsReach st P1 ∅ L1 s t) : VsReach st P2 ∅ L2 s t ∧ t ∈ T1 ∧ t ∈ T2 ∧ L2 t = L1 t := by + induction r with + | hr h => exact absurd h (Set.notMem_empty _) + | @root0 p o hp ho h8 hne h0 => + obtain ⟨a, b⟩ := (h1.pool p hp o ho h8 hne).1 h0 + obtain ⟨c, d⟩ := (h2.pool p (hs hp) o ho h8 hne).1 h0 + exact ⟨VsReach.root0 (hs hp) ho h8 hne h0, a, c, d.trans b.symm⟩ + | @root2 p o hp ho h8 h' => + have hne : L.mem s (wa p o) ≠ 0#u64 := fun e => by rw [e] at h'; exact absurd h' (by decide) + obtain ⟨a, b⟩ := (h1.pool p hp o ho h8 hne).2 h' + obtain ⟨c, d⟩ := (h2.pool p (hs hp) o ho h8 hne).2 h' + exact ⟨VsReach.root2 (hs hp) ho h8 h', a, c, d.trans b.symm⟩ + | @step q o r hl ho h8 h3 ih => + obtain ⟨r2, t1, t2, e⟩ := ih + have hl2 : L2 q < 3 := by omega + obtain ⟨a, b⟩ := h1.desc q t1 r hl o ho h8 h3 + obtain ⟨c, d⟩ := h2.desc q t2 r2 hl2 o ho h8 h3 + exact ⟨VsReach.step r2 hl2 ho h8 h3, a, c, by rw [d, b, e]⟩ + +/-- Entries of two distinct 4096-aligned tables are distinct words. -/ +theorem peAF_ne_tabA {p q i j : U64} (hp : p.val % 4096 = 0) (hq : q.val % 4096 = 0) (hi : i.val < 512) + (hj : j.val < 512) (h : q ≠ p) : peAF q j ≠ peAF p i := fun e => by + have hv := congrArg UScalar.val e + unfold peAF at hv + rw [wa_valm, wa_valm, wm8_valA hi, wm8_valA hj] at hv + have h1 := q.bv.isLt + have h2 := p.bv.isLt + have h3 : q.val = q.bv.toNat := rfl + have h4 : p.val = p.bv.toNat := rfl + have h5 : q.val ≠ p.val := fun e => h (UScalar.eq_of_val_eq e) + simp only [Nat.reducePow] at hv h1 h2 + omega + +/-- **J9 across one leaf write** (module docstring). -/ +theorem UnmPtSpecA.ofLeafWrA {Z : XSetsA} {OS : Set U64} {s s' : S} {σ σ' : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} {Pt : Set U64} {p i : U64} (hp : p.val % 4096 = 0) (hi : i.val < 512) + (hpS : p ∈ Z.S1) (hlv : 0 < Z.Lv p) (hS2 : p ∉ S2PgE Z.X Z.T2) + (hytb : ∀ y ∈ Z.Y, wa y 4096#u64 ∉ Z.S1) (hyal : ∀ y ∈ Z.Y, 8192 ∣ y.val) (hxal : ∀ r ∈ Z.X, 8192 ∣ r.val) + (hS1al : ∀ q ∈ Z.S1, q.val % 4096 = 0) + (hobj : ∀ u ∈ OS, CapObjOkA Z (capA st s u)) + (hJ : ∀ u ∈ OS, ∀ q a v, capA st s u = .PageTable q false a v → q ≠ p ∧ q.val % 4096 = 0) + (hbk : ∀ u ∈ OS, ∀ c, capA st s' u = c → (∀ p sz r d mp e a v, c ≠ .Frame p sz r d mp e a v) → + capA st s u = c) + (hword : ∀ q ∈ Pt, ∀ j : U64, j.val < 512 → peAF q j ≠ peAF p i → L.mem s' (peAF q j) = L.mem s (peAF q j)) + (link : ∀ q, SchedPageA st Z OS s q → q ∈ Pt) (hk : L.ksb s' = L.ksb s) (hcpu : L.cpu s' = L.cpu s) + (hset : ∀ q ao, q ≠ specPtr p → σ.kheap q = some (.archObj ao) → σ'.kheap q = some (.archObj ao)) + (hpk : ∃ tp, σ.kheap (specPtr p) = some (.archObj (.pageTable tp))) + (h : UnmPtSpecA st Z m OS s σ) : UnmPtSpecA st Z m OS s' σ' := by + have al8 : ∀ y : U64, 8192 ∣ y.val → y.val % 4096 = 0 ∧ (wa y 4096#u64).val % 4096 = 0 := fun y hy => by + have h1 := y.bv.isLt + have h2 : y.val = y.bv.toNat := rfl + have h3 : (4096#u64 : U64).val = 4096 := rfl + rw [wa_valm, h3] + simp only [Nat.reducePow] at h1 ⊢ + omega + have words : ∀ q ∈ Pt, q.val % 4096 = 0 → q ≠ p → ∀ j : U64, j.val < 512 → + L.mem s' (peAF q j) = L.mem s (peAF q j) := + fun q hq ha ne j hj => hword q hq j hj (peAF_ne_tabA hp ha hi hj ne) + have ne_sp : ∀ q, q ≠ p → specPtr q ≠ specPtr p := fun q hn e => hn (specPtr_inj e) + have tab : ∀ ty q, q ∈ Pt → q.val % 4096 = 0 → q ≠ p → UnmTabA st Z m s σ ty q → UnmTabA st Z m s' σ' ty q := + fun ty q hq ha ne h => by + obtain ⟨t, k1, k2, k3⟩ := h.spec + exact ⟨fun j hj => by rw [words q hq ha ne j hj]; exact h.inv j hj, + ⟨t, hset _ _ (ne_sp q ne) k1, k2, k3⟩, h.off.of_ksA hk hcpu⟩ + refine ⟨fun u hu q asid va hc => ?_, fun u hu q asid hc => ?_, fun u hu q asid va hc => ?_, + fun u hu q asid hc => ?_⟩ + · have o := hbk u hu _ hc (fun _ _ _ _ _ _ _ _ f => by cases f) + obtain ⟨ne, ha⟩ := hJ u hu q asid va o + exact tab _ _ (link q (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inl ⟨_, _, _, o⟩⟩)))))))) ha ne (h.pt u hu q asid va o) + · have o := hbk u hu _ hc (fun _ _ _ _ _ _ _ _ f => by cases f) + have hq := (hobj u hu).2.2.2.1 q false asid o + have ne : q ≠ p := fun e => by rw [e] at hq; omega + exact tab _ _ (link q (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inl ⟨_, _, o⟩)⟩)))))))) (hS1al q hq.1) ne (h.vs u hu q asid o) + · have o := hbk u hu _ hc (fun _ _ _ _ _ _ _ _ f => by cases f) + have hqX := (hobj u hu).2.1 q 0#u64 false asid va o + obtain ⟨a0, a1⟩ := al8 q (hxal q hqX) + have n0 : q ≠ p := fun e => hS2 (Or.inr ⟨q, hqX, Or.inl e.symm⟩) + have n1 : wa q 4096#u64 ≠ p := fun e => hS2 (Or.inr ⟨q, hqX, Or.inr e.symm⟩) + have l0 : q ∈ Pt := link q (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inr (Or.inl ⟨_, _, o⟩))⟩))))))) + have l1 : wa q 4096#u64 ∈ Pt := link _ (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inr (Or.inr (Or.inl ⟨q, _, _, o, rfl⟩)))⟩))))))) + have h0 := h.s2 u hu q asid va o + obtain ⟨es, k1, k2⟩ := h0.spec + refine ⟨fun j hj => ?_, es, hset _ _ (ne_sp q n0) k1, k2⟩ + by_cases hlo : j.val < 512 + · rw [words q l0 a0 n0 j hlo]; exact h0.inv j hj + · obtain ⟨j', hj', ej⟩ := peAF_hiA q j (by omega) hj + have := h0.inv j hj + rw [ej] at this ⊢ + rw [words _ l1 a1 n1 j' hj']; exact this + · have o := hbk u hu _ hc (fun _ _ _ _ _ _ _ _ f => by cases f) + have hqY := (hobj u hu).2.2.1 q false asid o + obtain ⟨a0, a1⟩ := al8 q (hyal q hqY) + have n1 : wa q 4096#u64 ≠ p := fun e => hytb q hqY (e ▸ hpS) + have n0 : specPtr q ≠ specPtr p := fun e => by + obtain ⟨l, k1, -⟩ := (h.hv u hu q asid o).spec + obtain ⟨tp, kp⟩ := hpk + rw [e, kp] at k1 + cases k1 + have l1 : wa q 4096#u64 ∈ Pt := link _ (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inr (Or.inr (Or.inr ⟨q, _, o, rfl⟩)))⟩))))))) + have h0 := h.hv u hu q asid o + obtain ⟨l, k1, k2⟩ := h0.spec + refine ⟨fun j hj => ?_, l, hset _ _ n0 k1, k2⟩ + rw [words _ l1 a1 n1 j hj]; exact h0.inv j hj + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3McsFrA.lean b/proofs/Fl4ProofsA64/P3/Stage3McsFrA.lean new file mode 100644 index 000000000..3a3d65a73 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3McsFrA.lean @@ -0,0 +1,140 @@ +/- +P3 (aarch64, A64-P3 Stage 3, D8 step 3): **spec frames of the MCS invocations**: `SPresInst` instances for the +scheduling-context invocation bodies (`invokeSchedContext`, bind / unbind / yield-to / consumed) and the SchedControl +configure body. No premise. +-/ +import Fl4ProofsA64.P3.SpecFrameSchedA +import Fl4ProofsA64.P3.SpecFrameIpcA +import Fl4ProofsA64.P3.Stage3IpcFootA +import Fl4ProofsA64.P3.ScConsumeMsgA +import Fl4ProofsA64.P3.ScDonA +import Fl4ProofsA64.P3.Stage3PartialA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec +noncomputable section + +instance (p : ObjRef) : SPresInst (maybeAddEmptyTail (Z := Unit) p) := ⟨by unfold maybeAddEmptyTail; spres⟩ +instance (p : ObjRef) (n : Nat) (b pr : Ticks) : SPresInst (refillNew (Z := Unit) p n b pr) := + ⟨by unfold refillNew; spres⟩ +instance (p : ObjRef) (a b : Ticks) (n : Nat) : SPresInst (refillUpdate (Z := Unit) p a b n) := + ⟨by unfold refillUpdate; spres⟩ +instance (p t : ObjRef) : SPresInst (schedContextBindTcb (Z := Unit) p t) := ⟨by unfold schedContextBindTcb; spres⟩ +instance (p n : ObjRef) : SPresInst (schedContextBindNtfn (Z := Unit) p n) := ⟨by unfold schedContextBindNtfn; spres⟩ +instance (p : ObjRef) : SPresInst (schedContextUnbindNtfn (Z := Unit) p) := ⟨by unfold schedContextUnbindNtfn; spres⟩ +instance (p : ObjRef) : SPresInst (schedContextUnbindReply (Z := Unit) p) := ⟨by unfold schedContextUnbindReply; spres⟩ +instance (p : ObjRef) : SPresInst (schedContextUnbindTcb (Z := Unit) p) := ⟨by unfold schedContextUnbindTcb; spres⟩ +instance (p : ObjRef) : SPresInst (schedContextUnbindAllTcbs (Z := Unit) p) := + ⟨by unfold schedContextUnbindAllTcbs; spres⟩ +instance (p : ObjRef) : SPresInst (returnConsumed (Z := Unit) p) := ⟨by unfold returnConsumed; spres⟩ +instance (p : ObjRef) : SPresInst (schedContextUpdateConsumed (Z := Unit) p) := + ⟨by unfold schedContextUpdateConsumed; spres⟩ +instance (p t : ObjRef) : SPresInst (setConsumed (Z := Unit) p t) := ⟨by unfold setConsumed; spres⟩ +instance (p : ObjRef) : SPresInst (completeYieldTo (Z := Unit) p) := ⟨by unfold completeYieldTo; spres⟩ +instance (p : ObjRef) : SPresInst (schedContextYieldTo (Z := Unit) p) := ⟨by unfold schedContextYieldTo; spres⟩ +instance (p : ObjRef) : SPresInst (fl4SchedContextResume (Z := Unit) p) := + ⟨by unfold fl4SchedContextResume; spres⟩ + +theorem invokeSchedContext_presA (iv : SchedContextInvocation) : SPresA (invokeSchedContext (Z := Unit) iv) := by + unfold invokeSchedContext + cases iv with + | invokeSchedContextConsumed p => exact (inferInstance : SPresInst (returnConsumed (Z := Unit) p)).pres + | invokeSchedContextBind p cap => cases cap <;> first | (spres) | exact PresR.fail + | invokeSchedContextUnbindObject p cap => cases cap <;> first | (spres) | exact PresR.fail + | invokeSchedContextUnbind p => spres + | invokeSchedContextYieldTo p => exact (inferInstance : SPresInst (schedContextYieldTo (Z := Unit) p)).pres + +theorem invokeFl4SchedControlConfigureFlags_presA (iv : SchedControlInvocation) : + SPresA (invokeFl4SchedControlConfigureFlags (Z := Unit) iv) := by + unfold invokeFl4SchedControlConfigureFlags + cases iv; spres + + +/-! ## The concrete words of the MCS invocation footprints are Stage-3 write-set words -/ + +section conc +open fl4_core_a64 Fl4ProofsA64.P1 +local notation "wa" => core.num.U64.wrapping_add +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +theorem stA_stepNA {N E Rp : Set U64} {Z : XSetsA} {m : McState} {σ : State Unit} {s : S} + (hP : TcbPlaced Z.T) {t : U64} (ht : t ∈ Z.T) : SchedStepNWA (st := st) N E Rp Z m σ s (stA t) := + Or.inl (Or.inr (Fl4ProofsA64.P3.Endp.tcbStepWA_of_off hP ht 3328 (by omega) (by omega) (by omega) (by omega) + (Fl4ProofsA64.P3.Endp.val_st (hP t ht)))) + +theorem tcbWord_stepNA {N E Rp : Set U64} {Z : XSetsA} {m : McState} {σ : State Unit} {s : S} + (hP : TcbPlaced Z.T) {t : U64} (ht : t ∈ Z.T) (K : U64) (hK : K.val < 4096) (h1 : 224 ≤ K.val) + (h3 : K.val ≠ 3664) (h4 : K.val ≠ 3672) : SchedStepNWA (st := st) N E Rp Z m σ s (wa t K) := + Or.inl (Or.inr (Fl4ProofsA64.P3.Endp.tcbStepWA_of_off hP ht K.val h1 hK h3 h4 + (by have := (hP t ht).fits; rw [wa_valm]; omega))) + +theorem yieldA_stepNA {N E Rp : Set U64} {Z : XSetsA} {m : McState} {σ : State Unit} {s : S} + (hP : TcbPlaced Z.T) {t : U64} (ht : t ∈ Z.T) : SchedStepNWA (st := st) N E Rp Z m σ s (yieldA t) := + tcbWord_stepNA hP ht 3472#u64 (by decide) (by decide) (by decide) (by decide) + +theorem consumedRegs_stepNA {N E Rp : Set U64} {Z : XSetsA} {m : McState} {σ : State Unit} {s : S} + (hP : TcbPlaced Z.T) {t : U64} (ht : t ∈ Z.T) {b : U64} (h : ScConsumedRegsA t b) : + SchedStepNWA (st := st) N E Rp Z m σ s b := by + rcases h with rfl | rfl | rfl <;> exact tcbWord_stepNA hP ht _ (by decide) (by decide) (by decide) (by decide) + +theorem nScSc_stepNA {N E Rp : Set U64} {Z : XSetsA} {m : McState} {σ : State Unit} {s : S} + {n : U64} (hn : n ∈ N) (hfit : n.val + 64 ≤ 2 ^ 64) : SchedStepNWA (st := st) N E Rp Z m σ s (nScSc n) := by + refine Or.inr (Or.inl ⟨n, hn, ?_, ?_⟩) <;> unfold nScSc <;> rw [wa_valm] <;> + (have : (layout.ntfn.SC).val = 32 := by unfold layout.ntfn.SC; rfl) <;> omega + +theorem rNextA_stepNA {N E Rp : Set U64} {Z : XSetsA} {m : McState} {σ : State Unit} {s : S} + {r : U64} (hr : r ∈ Rp) (hfit : r.val + 32 ≤ 2 ^ 64) : SchedStepNWA (st := st) N E Rp Z m σ s (rNextA r) := by + refine Or.inr (Or.inr (Or.inr ⟨r, hr, ?_, ?_⟩)) <;> unfold rNextA <;> rw [wa_valm] <;> + (have : (layout.reply.NEXT).val = 16 := by unfold layout.reply.NEXT; rfl) <;> omega + +end conc + +/-! ## Spec frames of the label bodies, and the `PartFrA` packaging -/ + +theorem frame_of_runA {α : Type} {m : SMonad Unit α} (hm : SPresA m) {σ σ' : State Unit} {a : α} + (h : m σ = ({(a, σ')}, False)) : SchedFrameA σ σ' := + hm σ (a, σ') (by rw [h]; exact rfl) + +/-- Restart then an SC invocation (bind / unbind-object / unbind / ntfn variants; the result data is arbitrary). -/ +theorem mcs_restart_invoke_frameA {iv : SchedContextInvocation} {ct : ObjRef} {σ σ1 σ' : State Unit} {d : List Data} + (hR : setThreadState (Z := Unit) ct .restart σ = ({((), σ1)}, False)) + (hI : invokeSchedContext (Z := Unit) iv σ1 = ({(d, σ')}, False)) : SchedFrameA σ σ' := + PreO.trans (frame_of_runA (inferInstance : SPresInst (setThreadState (Z := Unit) ct .restart)).pres hR) + (frame_of_runA (invokeSchedContext_presA iv) hI) + +/-- Restart, `schedContextYieldTo` and the optional reply / running tail of YieldTo. -/ +theorem mcs_yield_frameA {sc ct : ObjRef} {call : Bool} {σ σ0 σ1 σ' : State Unit} {data : List Data} + (hR : setThreadState (Z := Unit) ct .restart σ = ({((), σ0)}, False)) + (hI : invokeSchedContext (Z := Unit) (.invokeSchedContextYieldTo sc) σ0 = ({(data, σ1)}, False)) + (hT : (data = [] ∧ σ' = σ1) ∨ ∃ tm, data = wordsFromTime tm ∧ + (do NonDet.when (call = true) (replyFromKernel ct (0, data)) + setThreadState ct .running : SMonad Unit Unit) σ1 = ({((), σ')}, False)) : + SchedFrameA σ σ' := by + have h01 := mcs_restart_invoke_frameA hR hI + rcases hT with ⟨-, rfl⟩ | ⟨tm, rfl, h⟩ + · exact h01 + · refine PreO.trans h01 (frame_of_runA ?_ h) + spres + +theorem mcs_cfg_frameA {iv : SchedControlInvocation} {ct : ObjRef} {σ σ1 τ : State Unit} + (hR : setThreadState (Z := Unit) ct .restart σ = ({((), σ1)}, False)) + (hI : invokeFl4SchedControlConfigureFlags (Z := Unit) iv σ1 = ({((), τ)}, False)) : SchedFrameA σ τ := + PreO.trans (frame_of_runA (inferInstance : SPresInst (setThreadState (Z := Unit) ct .restart)).pres hR) + (frame_of_runA (invokeFl4SchedControlConfigureFlags_presA iv) hI) + +section pkg +open fl4_core_a64 Fl4ProofsA64.P1 +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **The `PartFrA` package of an MCS label**: the label's extra written words `X` are Stage-3 write-set words. -/ +theorem mcs_partA {N E Rp : Set U64} {Z : XSetsA} {m : McState} {s s' : S} {σ σ' : State Unit} + {X : U64 → Prop} (hX : ∀ b, X b → SchedStepNWA (st := st) N E Rp Z m σ s b) + (w : WritesIn st s s' (fun b => MWrA (st := st) Z m σ s b ∨ X b)) (F : SchedFrameA σ σ') : + PartFrA (st := st) N E Rp Z m s s' σ σ' := + ⟨w.mono fun b hb => hb.elim (fun h => Or.inl (Or.inl h)) (hX b), F⟩ + +end pkg + +end +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3McsUnbA.lean b/proofs/Fl4ProofsA64/P3/Stage3McsUnbA.lean new file mode 100644 index 000000000..504e3f581 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3McsUnbA.lean @@ -0,0 +1,65 @@ +/- +P3 (aarch64, A64-P3 Stage 3, D8 step 3): the MCS Unbind label packaged as `PartFrA` +(`sc_decode_unbind_localA` + conditional write bound + `mcs_partA`). No premise; the notification / reply +membership of the (non-null) words is a statement-level hypothesis of the caller. +-/ +import Fl4ProofsA64.P3.ScUnbTopA +import Fl4ProofsA64.P3.Stage3McsFrA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 +noncomputable section +open Fl4SpecA64 + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +local notation "wa" => core.num.U64.wrapping_add + +theorem sc_decode_unbind_partA (hA : P0.ArchTotal ar) {N E Rp : Set U64} + {Z : XSetsA} {m : McState} {κ : U64 → U64} {s s1 : S} {σ : State Unit} + (hP : TcbPlaced Z.T) (H : SchedSRA st Z m κ s σ) (hst : StEqA Z.T (L.mem s) σ) + {a a1 : A} {sc t ct : U64} (hc : sc ∈ Z.C) (h0 : sc ≠ 0#u64) (hni : scNameA m sc ≠ idleScPtr) + (hct : ct ∈ Z.T) {xc : Tcb} (hkc : σ.kheap (specPtr ct) = some (.tcb xc)) (hr : runnable xc.tcbState = true) + (htSc : obj.Sc.tcb st sc s = ok t) + (he : (if t != 0#u64 then smp.remote_tcb_stall st ar s a t else ok (s, a)) = ok (s1, a1)) + (hq : t != 0#u64 → smp.remote_tcb_stall st ar s a t = ok (s, a)) + (hcur : sched.cur_thread st s1 = ok ct) + (hne : L.mem s1 (scTcbA sc) ≠ ct) + (hbind : ∀ u, L.mem s1 (scTcbA sc) = u → u ≠ 0#u64 → u ∈ Z.T ∧ + L.mem s1 (scA u) = sc ∧ L.mem s1 (affA u) = L.cpu s1 ∧ L.mem s1 (wa u 3664#u64) = 0#u64) + (hn : let n := L.mem s1 (scWA sc 40#u64); n ≠ 0#u64 → + (∃ x, σ.kheap (specPtr n) = some (.notification x)) ∧ + Aligned (nScSc n) ∧ ¬ SReadA st Z m s1 σ (nScSc n)) + (hkr : ∀ x k, σ.kheap (scNameA m sc) = some (.schedContext x k) → ∀ r, + x.scReplies.head? = some r → ∃ xr, σ.kheap r = some (.reply xr)) + (hrr : let r := L.mem s1 (scReplyA sc); r ≠ 0#u64 → + Aligned (rNextA r) ∧ ¬ SReadA st Z m s1 σ (rNextA r)) + (hNm : L.mem s1 (scWA sc 40#u64) ≠ 0#u64 → L.mem s1 (scWA sc 40#u64) ∈ N ∧ + (L.mem s1 (scWA sc 40#u64)).val + 64 ≤ 2 ^ 64) + (hRm : L.mem s1 (scReplyA sc) ≠ 0#u64 → L.mem s1 (scReplyA sc) ∈ Rp ∧ + (L.mem s1 (scReplyA sc)).val + 32 ≤ 2 ^ 64) + {call : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} {label : Data} + (hl : genInvocationType label = .schedContextUnbind) (caps : List Cap) : + schedcontext.decode st ar s a fl4_abi.label.SchedContextUnbind sc call extra ⦃ r => + r.1 = .Ok () ∧ r.2.2 = a1 ∧ ∃ σ2 σ' Q' R', + (decodeSchedContextInvocation (Z := Unit) label (scNameA m sc) caps).run σ = + ({(.ok (.invokeSchedContextUnbind (scNameA m sc)), σ)}, False) ∧ + setThreadState (Z := Unit) (specPtr ct) .restart σ = ({((), σ2)}, False) ∧ + invokeSchedContext (Z := Unit) (.invokeSchedContextUnbind (scNameA m sc)) σ2 = ({([], σ')}, False) ∧ + SchedSRA st { Z with Q := Q', R := R' } m κ r.2.1 σ' ∧ + PartFrA (st := st) N E Rp Z m s r.2.1 σ σ' ⦄ := by + refine WP.spec_mono (sc_decode_unbind_localA hA H hst hc h0 hni hct hkc hr htSc he hq hcur hne hbind hn hkr hrr hl caps) ?_ + rintro r ⟨h1, h2, σ2, σ', Q', R', hD, hR, hI, H', w⟩ + refine ⟨h1, h2, σ2, σ', Q', R', hD, hR, hI, H', mcs_partA (X := fun b => b = stA ct ∨ + (L.mem s1 (scWA sc 40#u64) ≠ 0#u64 ∧ b = nScSc (L.mem s1 (scWA sc 40#u64))) ∨ + (L.mem s1 (scReplyA sc) ≠ 0#u64 ∧ b = rNextA (L.mem s1 (scReplyA sc)))) ?_ w + (mcs_restart_invoke_frameA hR hI)⟩ + rintro b (rfl | ⟨hn0, rfl⟩ | ⟨hr0, rfl⟩) + · exact stA_stepNA hP hct + · exact nScSc_stepNA (hNm hn0).1 (hNm hn0).2 + · exact rNextA_stepNA (hRm hr0).1 (hRm hr0).2 + +end +end +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3NBSendIpcA.lean b/proofs/Fl4ProofsA64/P3/Stage3NBSendIpcA.lean new file mode 100644 index 000000000..cfc072677 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3NBSendIpcA.lean @@ -0,0 +1,169 @@ +/- +P3 (aarch64, A64-P3 line RecvPreA): **`NBSendRecv` / `NBSendWait` whose send half finds a receiver** ends in +`RecvPreA`. + +The first phase of a combined call sends with `block = false`, `call = false`, `can_donate = true`. + +* **`JSyscallA.nbWakeEpiA`**: from an end record with `WakeEndA`, a caller that is still `Restart`, keeps its SC, is + in no queue and is not the pending switch, the success epilogue ends in `RecvPreA`. +* **`JSyscallA.nbSendDeliverFirstA`**: the receiver has its own SC (nothing is donated, `send_ipc_recvDonA`). + +[S] those of `JSyscallA.sendRecvGA`: a fault-free sender without extra caps to transfer; the receiver has an SC. +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3NBSignalWakeA +import Fl4ProofsA64.P3.Stage3SendRecvArmA +import Fl4ProofsA64.P3.Stage3IpcWakeA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4Spec Fl4SpecA64 + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {s : S} {a : A} {σ : State Unit} {m : McState} + +set_option maxHeartbeats 2000000 in +/-- **From a waking operation's end record to `RecvPreA`** (module docstring). -/ +theorem JSyscallA.nbWakeEpiA + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + {σ2 : State Unit} {s2 : S} {Q1 : U64 → U64 → List U64} {R1 : U64 → List U64} + (J2 : JStage3A st AR { Z with Q := Q1, R := R1 } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s2 a σ2 m) + (W : Sched.WakeEndA σ σ2) (off : OffQA σ.curThread σ2) + (hx : ∀ x, σ2.kheap σ.curThread = some (.tcb x) → x.tcbState = .restart ∧ x.tcbSchedContext = some σ.curSc) : + getThreadState (Z := Unit) σ.curThread σ2 = ({(.restart, σ2)}, False) ∧ + ∃ σ3 s3, ((), σ3) ∈ (setThreadState (Z := Unit) σ.curThread .running σ2).1 ∧ + obj.Tcb.state st (L.curW s) s2 = ok sched.TS_RESTART ∧ + sched.set_thread_state st s2 (L.curW s) sched.TS_RUNNING = ok s3 ∧ + syscall.after_invocation st ar s3 a (.Ok ()) = ok (true, s3, a) ∧ + L.curW s3 = L.curW s ∧ + (∀ x, σ3.kheap σ3.curThread = some (.tcb x) → x.tcbSchedContext = some σ3.curSc) ∧ + ∃ Q Rl, RecvPreA st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n + (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s3 a σ3 m := by + set t := L.curW s with ht_def + have ct : σ.curThread = specPtr t := J.j.1.sch.curThread + have P0' := J.toRecvPreA + have B := Sched.BodyEndA.ofWakeA (nA := hPl.ipa) J J2 W + have thr : σ2.curThread = σ.curThread := B.2 + have csc2 : σ2.curSc = σ.curSc := W.bl.csc + have htZ : t ∈ Z.T := J.sr.curT + obtain ⟨x2, hx2, -⟩ := J2.ep.tcb t htZ + have hk2c : σ2.kheap σ.curThread = some (.tcb x2) := by rw [ct]; exact hx2 + have hk2 : σ2.kheap σ2.curThread = some (.tcb x2) := by rw [thr]; exact hk2c + obtain ⟨hst2, hsc2⟩ := hx x2 hk2c + obtain ⟨xc, hkc, hxsc⟩ : ∃ xc : Tcb, σ.kheap σ.curThread = some (.tcb xc) ∧ xc.tcbSchedContext = some σ.curSc := by + obtain ⟨y, hy, e, -⟩ := J.curInv.tcb + exact ⟨y, hy, e⟩ + have act2 : ∀ sc, x2.tcbSchedContext = some sc → ScActiveAtA σ2 sc := fun sc hs => by + have e : σ.curSc = sc := Option.some.inj (hsc2.symm.trans hs) + subst e + obtain ⟨y', k, hy'⟩ := J2.sinv.curSc + rw [csc2] at hy' + refine ⟨y', k, hy', ?_⟩ + obtain ⟨y, hy, -, e2⟩ := W.bl.sc y' k hy' + obtain ⟨z, j, hz, ha⟩ := P0'.scAct xc _ hkc hxsc + rw [hy] at hz + obtain ⟨rfl, -⟩ := KernelObject.schedContext.inj (Option.some.inj hz) + unfold scActive at ha ⊢ + rw [e2]; exact ha + have get2 : getThreadState (Z := Unit) σ.curThread σ2 = ({(.restart, σ2)}, False) := by + rw [← hst2]; exact threadGet_eqA Tcb.tcbState hk2c + obtain ⟨σ3, s3, mem3, hstate, eRun, haft, hcur3, c3, own3, Q3, R3, P3⟩ := + J.epiBridgeA hA hPl J2 B.1 thr hk2 hst2 (fun d p => by rw [thr]; exact off.1 d p) (by rw [thr]; exact off.2.1) + act2 (fun e => off.2.2 _ e thr) + refine ⟨get2, σ3, s3, mem3, hstate, eRun, haft, hcur3, fun x hxx => ?_, Q3, R3, P3⟩ + rw [own3 x hxx, c3, csc2] + exact hsc2 + +set_option maxHeartbeats 2000000 in +/-- **`NBSendRecv` / `NBSendWait` delivering to a receiver with its own SC ends in `RecvPreA`** (module +docstring). -/ +theorem JSyscallA.nbSendDeliverFirstA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p badge slot lbl len buf : U64} {recv grant grantReply fp : Bool} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) + (hcap : capA st s slot = .Endpoint p badge true recv grant grantReply) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hp : p ∈ E) {d : U64} {q : List ObjRef} + (hk : σ.kheap (specPtr p) = some (.endpoint (.recvEP (specPtr d :: q)))) + (hsnd : ∀ x, σ.kheap σ.curThread = some (.tcb x) → x.tcbFault = none ∧ (grant = false ∨ + ipcBufOfA false x = none ∨ + unat (dataToMessageInfo (x.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0)) + (hrcv : ∀ y, σ.kheap (specPtr d) = some (.tcb y) → y.tcbSchedContext ≠ none) + (hdne : specPtr d ≠ σ.curThread) : + ∃ xc σ1 σ2 σ3 s2 s3, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ((), σ2) ∈ (sendIpc (Z := Unit) false false badge.bv grant grantReply true σ.curThread (specPtr p) σ1).1 ∧ + getThreadState (Z := Unit) σ.curThread σ2 = ({(.restart, σ2)}, False) ∧ + ((), σ3) ∈ (setThreadState (Z := Unit) σ.curThread .running σ2).1 ∧ + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) false false true fp buf extra = + ok (.Ok (), s2, a) ∧ + obj.Tcb.state st (L.curW s) s2 = ok sched.TS_RESTART ∧ + sched.set_thread_state st s2 (L.curW s) sched.TS_RUNNING = ok s3 ∧ + syscall.after_invocation st ar s3 a (.Ok ()) = ok (true, s3, a) ∧ + L.curW s3 = L.curW s ∧ + (∀ x, σ3.kheap σ3.curThread = some (.tcb x) → x.tcbSchedContext = some σ3.curSc) ∧ + ∃ Q Rl, RecvPreA st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n + (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s3 a σ3 m := by + obtain ⟨xc, σ1, σ2, s2, hkc, hσ1, hsp, hmem, hdec, W, K, hres, Q1, R1, J2⟩ := + J.sendRecvGA (lbl := lbl) (len := len) (buf := buf) (blk := false) (call := false) (don := true) (fp := fp) + hA hM hPl hfm hCs h1 hmb hmax htu hslot hcov hcap hext hSC hp hk hsnd (Or.inr (Or.inr ⟨rfl, rfl, hrcv⟩)) + have P0' := J.toRecvPreA + have f1 : σ1.curThread = σ.curThread ∧ σ1.readyQueues = σ.readyQueues ∧ σ1.releaseQueue = σ.releaseQueue ∧ + σ1.schedulerAction = σ.schedulerAction := by rw [hσ1]; exact ⟨rfl, rfl, rfl, rfl⟩ + obtain ⟨-, f1q, f1r, f1a⟩ := f1 + have off : OffQA σ.curThread σ2 := by + refine ⟨fun dm pr h => ?_, fun h => ?_, fun u e eu => ?_⟩ + · rcases K.rq dm pr _ h with h | h | h + · rw [f1q] at h; exact P0'.noQ dm pr h + · exact hdne h.symm + · rw [f1a] at h; exact J.noSwitch _ h + · have := K.rls _ h + rw [f1r] at this + exact P0'.notRel this + · rcases K.act with c | c | ⟨y, hy, c⟩ + · rw [c, f1a] at e; exact J.noSwitch u e + · rw [c] at e; cases e + · rw [c] at e + cases e + exact hdne (hy.symm.trans eu) + have hx : ∀ x, σ2.kheap σ.curThread = some (.tcb x) → + x.tcbState = .restart ∧ x.tcbSchedContext = some σ.curSc := by + intro x h + refine ⟨hres rfl x h, ?_⟩ + obtain ⟨y, hy, e, -⟩ := J.curInv.tcb + have hk1 : σ1.kheap σ.curThread = some (.tcb { y with tcbState := .restart }) := by + rw [hkc] at hy + cases hy + rw [hσ1]; simp [stUpdA, setObjA] + have o := K.obj σ.curThread + rw [hk1, h] at o + cases o with + | same => exact e + | tcb _ _ g => + rcases g.sc with c | c + · rw [c]; exact e + · exact absurd c.1 (by simp) + obtain ⟨get2, σ3, s3, mem3, hstate, eRun, haft, hcur3, own3, Q3, R3, P3⟩ := + J.nbWakeEpiA hA hPl J2 W off hx + exact ⟨xc, σ1, σ2, σ3, s2, s3, hkc, hσ1, hsp, hmem, get2, mem3, hdec, hstate, eRun, haft, hcur3, own3, Q3, R3, P3⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3NBSendRecvA.lean b/proofs/Fl4ProofsA64/P3/Stage3NBSendRecvA.lean new file mode 100644 index 000000000..3bf78e650 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3NBSendRecvA.lean @@ -0,0 +1,370 @@ +/- +P3 (aarch64, A64-P3 line RecvPreA): **`NBSendRecv` / `NBSendWait` whose send half finds no receiver**. + +Both run inside one kernel-entry body: `handle_invocation` (call = false, block = false) on the destination cap, the +success epilogue, `after_invocation` on `Ok`, then `handle_recv` (`blocking = true`; `can_reply` for `NBSendRecv` +only). With an idle endpoint, or one holding waiting senders, the non-blocking send does nothing +(`JSyscallA.sendNoblockA`); the epilogue takes the caller from `Restart` to `Running`. + +* **`JSyscallA.nbSendFirstA`**: the first half ends in `RecvPreA` at the sets of the entry, no switch pending, the + caller holding the current SC. +* **`JSyscallA.epiBridgeA`**: the general bridge: any operation ending in `JStage3A` and `BodyOKA` with the caller + `Restart` and unqueued continues to `RecvPreA`. +* **`JSyscallA.nbEpiA`**: the success epilogue of a non-blocking first half that leaves the scheduler alone. +* **`JSyscallA.nbSignalQuietFirstA`**: `NBSendRecv` / `NBSendWait` on a Notification send cap whose signal wakes no + thread ([S] `QuietSigA`) also ends in `RecvPreA`. +* **`JSyscallA.nbSendRecvA`**: any `HRecvPostA` from that state composes with the first half. + +[S] the destination is an Endpoint send cap whose endpoint is idle or holds waiting senders (the caller is in no ready +queue by the record). No named premise (`epiP1A`). Not here: `handle_invocation`'s front end, which the dispatch top +owns; an NBSend that finds a receiver (real IPC, possibly donating). New module. +-/ +import Fl4ProofsA64.P3.Stage3ReplyRecvA +import Fl4ProofsA64.P3.Stage3SendNoblockA +import Fl4ProofsA64.P3.Stage3SignalIdleA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4Spec Fl4SpecA64 + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {s : S} {a : A} {σ : State Unit} {m : McState} + +set_option maxHeartbeats 2000000 in +/-- **The bridge from a body-end record to `RecvPreA`**, for the first half of a combined call whatever its operation: +at the state after the operation (`JStage3A` and `BodyOKA`, same current thread), if the caller is still `Restart`, in +no ready or release queue, its SC (if any) is active and the pending switch is not to it ([S] selectors), the success +epilogue ends in `RecvPreA`. `cur ∉ readyQueues` excludes the operations that enqueue the caller itself. -/ +theorem JSyscallA.epiBridgeA + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + {σ2 : State Unit} {s2 : S} {a2 : A} {Z2 : XSetsA} {H2 : Heap} {OS2 U2 Ac2 Pp2 Tb2 : Set U64} + {Lv2 : U64 → Nat} {ia2 ie2 : Nat} {N2 E2 Rp2 : Set U64} {Bf2 : Set (U64 × Word)} {stack2 : U64 → List U64} + {κ2 : U64 → U64} + (J2 : JStage3A st AR Z2 H2 OS2 U2 Ac2 Pp2 Tb2 Lv2 ia2 ie2 N2 E2 Rp2 Bf2 stack2 κ2 n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s2 a2 σ2 m) + (B2 : BodyOKA σ2 m) (thr : σ2.curThread = σ.curThread) + {x2 : Tcb} (hk2 : σ2.kheap σ2.curThread = some (.tcb x2)) (hst : x2.tcbState = .restart) + (nq : ∀ d p, σ2.curThread ∉ σ2.readyQueues d p) (nrl : σ2.curThread ∉ σ2.releaseQueue) + (hsc : ∀ sc, x2.tcbSchedContext = some sc → ScActiveAtA σ2 sc) + (ns : σ2.schedulerAction ≠ .switchThread σ2.curThread) : + ∃ σ3 s3, ((), σ3) ∈ (setThreadState (Z := Unit) σ.curThread .running σ2).1 ∧ + obj.Tcb.state st (L.curW s) s2 = ok sched.TS_RESTART ∧ + sched.set_thread_state st s2 (L.curW s) sched.TS_RUNNING = ok s3 ∧ + syscall.after_invocation st ar s3 a2 (.Ok ()) = ok (true, s3, a2) ∧ + L.curW s3 = L.curW s ∧ σ3.curSc = σ2.curSc ∧ + (∀ x, σ3.kheap σ3.curThread = some (.tcb x) → x.tcbSchedContext = x2.tcbSchedContext) ∧ + ∃ Q Rl, RecvPreA st AR { Z2 with Q := Q, R := Rl } H2 OS2 U2 Ac2 Pp2 Tb2 Lv2 ia2 ie2 N2 E2 Rp2 Bf2 stack2 κ2 n + (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s3 a2 σ3 m := by + set t := L.curW s with ht_def + have ct : σ.curThread = specPtr t := J.j.1.sch.curThread + have ct2 : σ2.curThread = specPtr t := thr.trans ct + have k2t : σ2.kheap (specPtr t) = some (.tcb x2) := ct2 ▸ hk2 + have hcur2 : L.curW s2 = t := curW_keptA J.toJStage3A J2 thr + have htZ : t ∈ Z2.T := hcur2 ▸ J2.sr.curT + have hw : L.mem s2 (stA (L.curW s2)) = sched.TS_RESTART := by + rw [hcur2] + have c := (J2.ep.at_tcb htZ k2t).st + rw [hst] at c + exact UScalar.eq_of_val_eq (by rw [c, ts_vals.2.1]; rfl) + have V : TcbIpcViewsA st Z2 m κ2 N2 E2 Rp2 Bf2 stack2 s2 σ2 := + ⟨⟨_, _, J2.sr⟩, J2.ntfn, J2.ep, J2.reply, J2.mcp, J2.yieldRel⟩ + have e0 : setObjA σ2 (specPtr t) (.tcb { x2 with tcbArch := { x2.tcbArch with tcbContext := x2.tcbArch.tcbContext } }) = + σ2 := by + show { σ2 with kheap := Function.update σ2.kheap (specPtr t) (some (.tcb x2)) } = σ2 + rw [← k2t, Function.update_eq_self] + have hP1 : EpiP1A st AR ar hPl.ipa n m false := epiP1A hA hPl false + have fin := fun σX : State Unit => + hP1 _ H2 OS2 U2 Ac2 Pp2 Tb2 Lv2 ia2 ie2 N2 E2 Rp2 Bf2 stack2 κ2 s2 a2 σ2 J2 hw s2 rfl σX + rw [hcur2] at fin + obtain ⟨eRun, σ3, hset, e3, Q3, R3, J3⟩ := J2.runRestartA (t := t) hcur2 k2t hst x2.tcbArch.tcbContext + (by rw [e0]; exact V) (WritesIn.refl _ _) fin + rw [e0] at hset + have e3' : σ3 = setObjA σ2 σ2.curThread + (.tcb { x2 with tcbArch := { x2.tcbArch with tcbContext := x2.tcbArch.tcbContext }, tcbState := .running }) := by + rw [ct2]; exact e3 + have B3 : BodyOKA σ3 m := Sched.BodyOKA.runA (uc := x2.tcbArch.tcbContext) B2 hk2 hst e3' + set x3 : Tcb := { x2 with tcbArch := { x2.tcbArch with tcbContext := x2.tcbArch.tcbContext }, tcbState := .running } + with hx3 + have hk3 : σ3.kheap (specPtr t) = some (.tcb x3) := by rw [e3]; simp [setObjA] + have g3 : σ3.curThread = σ2.curThread ∧ σ3.curSc = σ2.curSc ∧ σ3.readyQueues = σ2.readyQueues ∧ + σ3.releaseQueue = σ2.releaseQueue ∧ σ3.schedulerAction = σ2.schedulerAction := by + rw [e3]; exact ⟨rfl, rfl, rfl, rfl, rfl⟩ + obtain ⟨g3t, g3c, g3q, g3r, g3a⟩ := g3 + have ct3 : σ3.curThread = specPtr t := g3t.trans ct2 + have hcur3 : L.curW (L.wr s2 (stA t) sched.TS_RUNNING) = t := curW_keptA J.toJStage3A J3 (g3t.trans thr) + have own3 : ∀ x, σ3.kheap σ3.curThread = some (.tcb x) → x.tcbSchedContext = x2.tcbSchedContext := fun x hx => by + rw [ct3, hk3] at hx + cases hx + rfl + have act3 : ∀ sc, x2.tcbSchedContext = some sc → ScActiveAtA σ3 sc := fun sc hs => by + obtain ⟨y, k, hy, ha⟩ := hsc sc hs + have ne : sc ≠ specPtr t := fun e => by rw [e, k2t] at hy; cases hy + exact ⟨y, k, by rw [e3]; simp only [setObjA, Function.update_of_ne ne]; exact hy, ha⟩ + have P3 : RecvPreA st AR { Z2 with Q := Q3, R := R3 } H2 OS2 U2 Ac2 Pp2 Tb2 Lv2 ia2 ie2 N2 E2 Rp2 Bf2 stack2 κ2 n + (J5A st) (J7A st (J7R2A st ar hPl.ipa)) (L.wr s2 (stA t) sched.TS_RUNNING) a2 σ3 m := + { toJStage3A := J3 + body := B3 + running := by + rw [hcur3] + refine Or.inl ?_ + have c := (J3.ep.at_tcb htZ hk3).st + exact UScalar.eq_of_val_eq (by rw [c, ts_vals.1]; rfl) + noQ := fun dm pr => by rw [g3q, ct3, ← ct2]; exact nq dm pr + notRel := by rw [g3r, ct3, ← ct2]; exact nrl + scAct := fun x sc hx hs => act3 sc ((own3 x hx).symm.trans hs) + noSelf := by rw [g3a, g3t]; exact ns } + have mem3 : ((), σ3) ∈ (setThreadState (Z := Unit) σ.curThread .running σ2).1 := by + rw [ct, hset]; exact Set.mem_singleton _ + have hstate : obj.Tcb.state st t s2 = ok sched.TS_RESTART := by + have e : obj.Tcb.state st t s2 = ok (L.mem s2 (stA t)) := eq_of_spec_fd (P1.obj.Tcb.state.spec (st := st) s2 t) + rw [e, ← hcur2, hw] + exact ⟨σ3, _, mem3, hstate, eRun, rfl, hcur3, g3c, own3, Q3, R3, P3⟩ + +set_option maxHeartbeats 1000000 in +/-- **The success epilogue of a non-blocking first half that leaves the scheduler alone**: from the state after the +operation (caller `Restart`, same current thread and SC, queues and scheduler action), `RUNNING` ends in `RecvPreA`. -/ +theorem JSyscallA.nbEpiA + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + {σ1 : State Unit} {s1 : S} {Q1 : U64 → U64 → List U64} {R1 : U64 → List U64} {xc : Tcb} + (J1 : JStage3A st AR { Z with Q := Q1, R := R1 } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s1 a σ1 m) + (hkc : σ.kheap σ.curThread = some (.tcb xc)) + (k1t : σ1.kheap (specPtr (L.curW s)) = some (.tcb { xc with tcbState := .restart })) + (ksc : σ1.kheap σ.curSc = σ.kheap σ.curSc) + (f1t : σ1.curThread = σ.curThread) (f1c : σ1.curSc = σ.curSc) (f1q : σ1.readyQueues = σ.readyQueues) + (f1r : σ1.releaseQueue = σ.releaseQueue) (f1a : σ1.schedulerAction = σ.schedulerAction) + (B1 : BodyOKA σ1 m) : + ∃ σ3 s3, ((), σ3) ∈ (setThreadState (Z := Unit) σ.curThread .running σ1).1 ∧ + obj.Tcb.state st (L.curW s) s1 = ok sched.TS_RESTART ∧ + sched.set_thread_state st s1 (L.curW s) sched.TS_RUNNING = ok s3 ∧ + syscall.after_invocation st ar s3 a (.Ok ()) = ok (true, s3, a) ∧ + L.curW s3 = L.curW s ∧ σ3.curSc = σ.curSc ∧ + (∀ x, σ3.kheap σ3.curThread = some (.tcb x) → x.tcbSchedContext = some σ3.curSc) ∧ + ∃ Q Rl, RecvPreA st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n + (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s3 a σ3 m := by + have ct : σ.curThread = specPtr (L.curW s) := J.j.1.sch.curThread + have hkt : σ.kheap (specPtr (L.curW s)) = some (.tcb xc) := ct ▸ hkc + have P0' := J.toRecvPreA + have hxsc : xc.tcbSchedContext = some σ.curSc := by + obtain ⟨y, hy, e, -⟩ := J.curInv.tcb + rw [hkc] at hy + cases hy + exact e + have curne : σ.curSc ≠ specPtr (L.curW s) := fun e => by + obtain ⟨y, k, hy⟩ := J.curInv.sc + rw [e, hkt] at hy + cases hy + have hk1c : σ1.kheap σ1.curThread = some (.tcb { xc with tcbState := .restart }) := by rw [f1t, ct]; exact k1t + obtain ⟨σ3, s3, mem3, hstate, eRun, haft, hcur3, c3, own3, Q3, R3, P3⟩ := + J.epiBridgeA hA hPl J1 B1 f1t hk1c rfl (fun d p => by rw [f1q, f1t]; exact P0'.noQ d p) + (by rw [f1r, f1t]; exact P0'.notRel) + (fun sc hs => by + have e : σ.curSc = sc := Option.some.inj (hxsc.symm.trans hs) + subst e + have act : ScActiveAtA σ σ.curSc := P0'.scAct xc _ hkc hxsc + obtain ⟨y, k, hy, ha⟩ := act + exact ⟨y, k, by rw [ksc]; exact hy, ha⟩) + (by rw [f1a]; exact J.noSwitch _) + refine ⟨σ3, s3, mem3, hstate, eRun, haft, hcur3, c3.trans f1c, fun x hx => ?_, Q3, R3, P3⟩ + rw [own3 x hx, c3, f1c] + exact hxsc + +set_option maxHeartbeats 2000000 in +/-- **The first half of `NBSendRecv` / `NBSendWait` with no receiver ends in `RecvPreA`** (module docstring). -/ +theorem JSyscallA.nbSendFirstA + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + {p badge slot lbl len buf : U64} {recv grant grantReply don fp : Bool} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) + (hcap : capA st s slot = .Endpoint p badge true recv grant grantReply) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) + (hp : p ∈ E) {x : Endpoint} (hk : σ.kheap (specPtr p) = some (.endpoint x)) (hc : CanBlockA false x) : + ∃ xc σ1 σ3 s1 s3, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + sendIpc (Z := Unit) false false badge.bv grant grantReply don σ.curThread (specPtr p) σ1 = + ({((), σ1)}, False) ∧ + ((), σ3) ∈ (setThreadState (Z := Unit) σ.curThread .running σ1).1 ∧ + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) false false don fp buf extra = + ok (.Ok (), s1, a) ∧ + obj.Tcb.state st (L.curW s) s1 = ok sched.TS_RESTART ∧ + sched.set_thread_state st s1 (L.curW s) sched.TS_RUNNING = ok s3 ∧ + syscall.after_invocation st ar s3 a (.Ok ()) = ok (true, s3, a) ∧ + L.curW s3 = L.curW s ∧ σ3.curSc = σ.curSc ∧ + (∀ x, σ3.kheap σ3.curThread = some (.tcb x) → x.tcbSchedContext = some σ3.curSc) ∧ + ∃ Q Rl, RecvPreA st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n + (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s3 a σ3 m := by + obtain ⟨xc, σ1, s1, hkc, hσ1, hsp, hsend, hdec, J1⟩ := + J.sendNoblockA (lbl := lbl) (len := len) (buf := buf) (call := false) (don := don) (fp := fp) hslot hcov hcap + hext hp hk hc + set t := L.curW s with ht_def + have htZ : t ∈ Z.T := J.sr.curT + have ct : σ.curThread = specPtr t := J.j.1.sch.curThread + have hkt : σ.kheap (specPtr t) = some (.tcb xc) := ct ▸ hkc + have P0' := J.toRecvPreA + have hrunc : xc.tcbState = .running ∨ xc.tcbState = .restart := P0'.state hkt + set x1 : Tcb := { xc with tcbState := .restart } with hx1 + have k1t : σ1.kheap (specPtr t) = some (.tcb x1) := by + rw [hσ1, ← ct]; simp [stUpdA, setObjA, hx1] + have k1 : ∀ q, q ≠ specPtr t → σ1.kheap q = σ.kheap q := fun q h => by + rw [hσ1, ct]; simp only [stUpdA, setObjA, Function.update_of_ne h] + have f1 : σ1.curThread = σ.curThread ∧ σ1.curSc = σ.curSc ∧ σ1.curTime = σ.curTime ∧ + σ1.consumedTime = σ.consumedTime ∧ σ1.readyQueues = σ.readyQueues ∧ σ1.releaseQueue = σ.releaseQueue ∧ + σ1.schedulerAction = σ.schedulerAction ∧ σ1.machineState = σ.machineState := by + rw [hσ1]; exact ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ + obtain ⟨f1t, f1c, f1tm, f1n, f1q, f1r, f1a, f1m⟩ := f1 + have hxsc : xc.tcbSchedContext = some σ.curSc := by + obtain ⟨y, hy, e, -⟩ := J.curInv.tcb + rw [hkc] at hy + cases hy + exact e + have curne : σ.curSc ≠ specPtr t := fun e => by + obtain ⟨y, k, hy⟩ := J.curInv.sc + rw [e, hkt] at hy + cases hy + have sw1 : SwA σ1 := fun u e => absurd (f1a ▸ e) (J.noSwitch u) + have B1 : BodyOKA σ1 m := by + refine P0'.body.stepEq sw1 f1tm (by rw [f1m]) f1t f1c f1n f1r (fun y' k h => ?_) (fun _ x hx => ?_) + · rw [k1 _ curne] at h + exact ⟨y', h, rfl, rfl, rfl⟩ + · rw [ct, k1t] at hx + cases hx + exact Or.inr rfl + obtain ⟨σ3, s3, mem3, hstate, eRun, haft, hcur3, c3, own3, Q3, R3, P3⟩ := + J.nbEpiA hA hPl (σ1 := σ1) (s1 := s1) J1 hkc k1t (k1 _ curne) f1t f1c f1q f1r f1a B1 + exact ⟨xc, σ1, σ3, s1, s3, hkc, hσ1, hsp, hsend, mem3, hdec, hstate, eRun, haft, hcur3, c3, own3, Q3, R3, P3⟩ + +set_option maxHeartbeats 2000000 in +/-- **The first half of `NBSendRecv` / `NBSendWait` as a quiet signal ends in `RecvPreA`** (module docstring). -/ +theorem JSyscallA.nbSignalQuietFirstA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hc : m.curCore < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p badge slot lbl len buf : U64} {recv blk call don fp : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Notification p badge true recv) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hp : p ∈ N) {x : Notification} (hk : σ.kheap (specPtr p) = some (.notification x)) + (hq : QuietSigA σ x) : + ∃ xc σ1 σ2 σ3 s2 s3, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + sendSignal (Z := Unit) (specPtr p) badge.bv σ1 = ({((), σ2)}, False) ∧ + getThreadState (Z := Unit) σ.curThread σ2 = ({(.restart, σ2)}, False) ∧ + ((), σ3) ∈ (setThreadState (Z := Unit) σ.curThread .running σ2).1 ∧ + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don fp buf extra = + ok (.Ok (), s2, a) ∧ + obj.Tcb.state st (L.curW s) s2 = ok sched.TS_RESTART ∧ + sched.set_thread_state st s2 (L.curW s) sched.TS_RUNNING = ok s3 ∧ + syscall.after_invocation st ar s3 a (.Ok ()) = ok (true, s3, a) ∧ + L.curW s3 = L.curW s ∧ + (∀ x, σ3.kheap σ3.curThread = some (.tcb x) → x.tcbSchedContext = some σ3.curSc) ∧ + ∃ Q Rl, RecvPreA st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n + (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s3 a σ3 m := by + obtain ⟨xc, σ1, σ2, s2, hkc, hσ1, hsp, hsig, hdec, ⟨B2, -⟩, Q1, R1, J2⟩ := + J.signalQuietA hA hM hPl hfm hc hmb hmax htu (lbl := lbl) (len := len) (buf := buf) (blk := blk) (call := call) + (don := don) (fp := fp) hslot hcov hcap hext hSC hp hk hq + -- the spec state after the signal + obtain ⟨b, e2⟩ : ∃ b, σ2 = updK σ1 (specPtr p) (.notification (quietNtfnA x b)) := by + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hslot hcov J.actW J.running J.cur + have hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM := J.toJStage3EntryA.curNotVmA + obtain ⟨xc', hkc', -, -, J1, -, -, -⟩ := J.toJStage3A.of_restartCurA + RS.pl.choose_spec.choose_spec.choose_spec.choose_spec RS.sym RS.run hnvm RS.cur RS.vs RS.vc + rw [hkc] at hkc' + cases hkc' + rw [← hσ1] at J1 + have ne1 : specPtr p ≠ σ.curThread := fun e => by rw [e, hkc] at hk; cases hk + have hk1 : σ1.kheap (specPtr p) = some (.notification x) := by + rw [hσ1]; simp only [stUpdA, setObjA, Function.update_of_ne ne1]; exact hk + have hq1 : QuietSigA σ1 x := by + rcases hq with ⟨hi, hb⟩ | h + · refine Or.inl ⟨hi, fun t ht => ?_⟩ + obtain ⟨y, hy, h1, h2⟩ := hb t ht + by_cases e : t = σ.curThread + · exact ⟨{ xc with tcbState := .restart }, by rw [hσ1, e]; simp [stUpdA, setObjA], rfl, by simp⟩ + · exact ⟨y, by rw [hσ1]; simp only [stUpdA, setObjA, Function.update_of_ne e]; exact hy, h1, h2⟩ + · exact Or.inr h + obtain ⟨-, -, -, b, hspec, -⟩ := exists_of_specA (send_signal_quietA (st := st) (ar := ar) J1.iplaced.n + (L.wr s (stA (L.curW s)) sched.TS_RESTART) a σ1 J1.ntfn hp hk1 hq1 badge) + have hb := congrArg Prod.fst (hsig.symm.trans hspec) + exact ⟨b, (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 hb)).2⟩ + set t := L.curW s with ht_def + have ct : σ.curThread = specPtr t := J.j.1.sch.curThread + have hkt : σ.kheap (specPtr t) = some (.tcb xc) := ct ▸ hkc + have pt : specPtr t ≠ specPtr p := fun e => by rw [← e, hkt] at hk; cases hk + have psc : σ.curSc ≠ specPtr p := fun e => by + obtain ⟨y, k, hy⟩ := J.curInv.sc + rw [e, hk] at hy + cases hy + have curne : σ.curSc ≠ specPtr t := fun e => by + obtain ⟨y, k, hy⟩ := J.curInv.sc + rw [e, hkt] at hy + cases hy + have k2t : σ2.kheap (specPtr t) = some (.tcb { xc with tcbState := .restart }) := by + rw [e2, updK_kheap, Function.update_of_ne pt, hσ1, ← ct] + simp [stUpdA, setObjA] + have ksc : σ2.kheap σ.curSc = σ.kheap σ.curSc := by + rw [e2, updK_kheap, Function.update_of_ne psc, hσ1, ct] + simp only [stUpdA, setObjA, Function.update_of_ne curne] + have f2 : σ2.curThread = σ.curThread ∧ σ2.curSc = σ.curSc ∧ σ2.readyQueues = σ.readyQueues ∧ + σ2.releaseQueue = σ.releaseQueue ∧ σ2.schedulerAction = σ.schedulerAction := by + rw [e2, hσ1]; exact ⟨rfl, rfl, rfl, rfl, rfl⟩ + obtain ⟨f2t, f2c, f2q, f2r, f2a⟩ := f2 + obtain ⟨σ3, s3, mem3, hstate, eRun, haft, hcur3, -, own3, Q3, R3, P3⟩ := + J.nbEpiA hA hPl (σ1 := σ2) (s1 := s2) J2 hkc k2t ksc f2t f2c f2q f2r f2a B2 + have get2 : getThreadState (Z := Unit) σ.curThread σ2 = ({(.restart, σ2)}, False) := + by rw [ct]; exact threadGet_eqA Tcb.tcbState k2t + exact ⟨xc, σ1, σ2, σ3, s2, s3, hkc, hσ1, hsp, hsig, get2, mem3, hdec, hstate, eRun, haft, hcur3, own3, Q3, R3, P3⟩ + +set_option maxHeartbeats 1000000 in +/-- **`NBSendRecv` / `NBSendWait` with no receiver, followed by any `Recv` arm** (module docstring). The arm is applied +at the `RecvPreA` state; its `HRecvPostA` composes with the first half. -/ +theorem JSyscallA.nbSendRecvA + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + {p badge slot lbl len buf : U64} {recv grant grantReply don fp : Bool} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) + (hcap : capA st s slot = .Endpoint p badge true recv grant grantReply) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) + (hp : p ∈ E) {x : Endpoint} (hk : σ.kheap (specPtr p) = some (.endpoint x)) (hc : CanBlockA false x) : + ∃ xc σ1 σ3 s1 s3, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + sendIpc (Z := Unit) false false badge.bv grant grantReply don σ.curThread (specPtr p) σ1 = + ({((), σ1)}, False) ∧ + ((), σ3) ∈ (setThreadState (Z := Unit) σ.curThread .running σ1).1 ∧ + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) false false don fp buf extra = + ok (.Ok (), s1, a) ∧ + obj.Tcb.state st (L.curW s) s1 = ok sched.TS_RESTART ∧ + sched.set_thread_state st s1 (L.curW s) sched.TS_RUNNING = ok s3 ∧ + syscall.after_invocation st ar s3 a (.Ok ()) = ok (true, s3, a) ∧ + (∀ x, σ3.kheap σ3.curThread = some (.tcb x) → x.tcbSchedContext = some σ3.curSc) ∧ + ∃ Q Rl, RecvPreA st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n + (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s3 a σ3 m ∧ + ∀ (blocking canReply : Bool) (Out : State Unit → Prop), + HRecvPostA st AR ar hPl.ipa n Z.Cs s3 a m blocking canReply σ3 Out → + ∃ s4 a4 σ4, syscall.handle_recv st ar s3 a blocking canReply = ok (s4, a4) ∧ + ((), σ4) ∈ (handleRecv (Z := Unit) blocking canReply σ3).1 ∧ Out σ4 ∧ + A64RelS3 st AR ar hPl.ipa n Z.Cs s4 a4 σ4 m ∧ L.curW s4 = L.curW s ∧ BodyOKA σ4 m := by + obtain ⟨xc, σ1, σ3, s1, s3, hkc, hσ1, hsp, hsend, mem3, hdec, hstate, eRun, haft, hcur3, -, own3, Q, Rl, P⟩ := + J.nbSendFirstA (lbl := lbl) (len := len) (buf := buf) (don := don) (fp := fp) hA hPl hslot hcov hcap hext hp hk hc + refine ⟨xc, σ1, σ3, s1, s3, hkc, hσ1, hsp, hsend, mem3, hdec, hstate, eRun, haft, own3, Q, Rl, P, + fun blocking canReply Out h => ?_⟩ + obtain ⟨s4, a4, σ4, h1, h2, h3, h4, h5, h6⟩ := h + exact ⟨s4, a4, σ4, h1, h2, h3, h4, h5.trans hcur3, h6⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3NBSignalWakeA.lean b/proofs/Fl4ProofsA64/P3/Stage3NBSignalWakeA.lean new file mode 100644 index 000000000..dcad5c84b --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3NBSignalWakeA.lean @@ -0,0 +1,523 @@ +/- +P3 (aarch64, A64-P3 line RecvPreA): **`NBSendRecv` / `NBSendWait` whose send half is a signal that wakes a thread** +ends in `RecvPreA`. + +* `OffQA c`: `c` is in no ready or release queue and is not the pending switch target. +* **`sendSignal_offQ`**: `send_signal` keeps `OffQA c` for a runnable `c`: the thread it wakes is blocked, so it is + not `c`, and only the woken thread is queued, postponed or switched to. +* **`JSyscallA.nbSignalWakeEpiA`**: from the end record of a waking signal, the success epilogue ends in `RecvPreA`. +* **`JSyscallA.nbSignalWaitingFirstA`**, **`JSyscallA.nbSignalBoundFirstA`**: the two waking arms + (`signalWaitingA`, `signalBoundRecvA`) as first halves. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3NBSendRecvA +import Fl4ProofsA64.P3.Stage3SigWakeRecA +import Fl4ProofsA64.P3.Stage3SigBoundRecA +import Fl4ProofsA64.P3.Stage3BodyCurA +import Fl4ProofsA64.P3.DelSinvLoopA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Classical + +noncomputable section + +/-- `c` is in no ready or release queue and is not the pending switch target. -/ +def OffQA (c : ObjRef) (σ : State Unit) : Prop := + (∀ d p, c ∉ σ.readyQueues d p) ∧ c ∉ σ.releaseQueue ∧ ∀ u, σ.schedulerAction = .switchThread u → u ≠ c + +theorem OffQA.congr {c : ObjRef} {σ σ' : State Unit} (h : OffQA c σ) (hq : σ'.readyQueues = σ.readyQueues) + (hr : σ'.releaseQueue = σ.releaseQueue) (ha : σ'.schedulerAction = σ.schedulerAction) : OffQA c σ' := + ⟨fun d p => by rw [hq]; exact h.1 d p, by rw [hr]; exact h.2.1, fun u e => h.2.2 u (ha ▸ e)⟩ + +theorem OffQA.tkeep {c : ObjRef} {σ σ' : State Unit} (k : TKeepA σ σ') (h : OffQA c σ) : OffQA c σ' := by + obtain ⟨-, er, eq, -, ea, -⟩ := k + exact h.congr eq er ea + +theorem HT.offqT {α : Type} {m : SMonad Unit α} (h : TPres m) (c : ObjRef) : HT (OffQA c) m (fun _ => OffQA c) := + fun σ hσ r hr => hσ.tkeep (h σ r hr) + +theorem threadSet_offq (c d : ObjRef) (f : Tcb → Tcb) : + HT (OffQA c) (threadSet (Z := Unit) f d) (fun _ => OffQA c) := + fun σ hσ r hr => by + obtain ⟨x, -, e⟩ := threadSet_spec hr + rw [e] + exact hσ.congr rfl rfl rfl + +theorem setThreadState_offq (c d : ObjRef) (ts : ThreadState) : + HT (OffQA c) (setThreadState (Z := Unit) d ts) (fun _ => OffQA c) := by + unfold setThreadState setThreadStateAct + refine HT.bind (Q := fun _ => OffQA c) (threadSet_offq c d _) fun _ => ?_ + refine HT.bind (Q := fun _ => OffQA c) (HT.gets _ fun σ h => h) fun cur => ?_ + refine HT.bind (Q := fun _ => OffQA c) (HT.gets _ fun σ h => h) fun act => ?_ + refine HT.bind (Q := fun _ => OffQA c) (HT.gets _ fun σ h => h) fun sch => ?_ + refine HT.when (fun _ => ?_) (fun _ σ h => h) + unfold setSchedulerAction + refine HT.modify _ fun σ h => ⟨h.1, h.2.1, fun u e => ?_⟩ + cases e + +theorem possibleSwitchTo_offq {c d : ObjRef} (ne : d ≠ c) : + HT (OffQA c) (possibleSwitchTo (Z := Unit) d) (fun _ => OffQA c) := fun σ hσ r hr => by + obtain ⟨Q, A⟩ := possibleSwitchTo_memA d hr + refine ⟨fun dm p h => ?_, ?_, fun u e => ?_⟩ + · rcases Q.2 dm p c h with h | h | h + · exact hσ.1 dm p h + · exact ne h.symm + · exact hσ.2.2 c h.1 rfl + · have er : r.2.releaseQueue = σ.releaseQueue := by rw [Q.1] + rw [er]; exact hσ.2.1 + · rcases A with a | a | a + · exact hσ.2.2 u (a ▸ e) + · rw [a] at e; cases e + · rw [a] at e; cases e; exact ne + +theorem tcbSchedDequeue_offq (c t : ObjRef) : + HT (OffQA c) (tcbSchedAction (Z := Unit) tcbSchedDequeue t) (fun _ => OffQA c) := fun σ hσ r hr => by + unfold tcbSchedAction at hr + obtain ⟨d, -, hr⟩ := eqPres_mem (threadGet_eqPres _ t) hr + obtain ⟨pr, -, hr⟩ := eqPres_mem (threadGet_eqPres _ t) hr + have hq : PresR (@Eq (State Unit)) (getTcbQueue (Z := Unit) d pr) := by unfold getTcbQueue; eqpres + obtain ⟨q, hq', hr⟩ := eqPres_mem hq hr + have eq : q = σ.readyQueues d pr := by + unfold getTcbQueue at hq' + obtain ⟨z, hz, hq'⟩ := mem_bindA.1 hq' + have ez : z = (σ.readyQueues, σ) := hz + subst ez + have := congrArg Prod.fst (show (q, σ) = (σ.readyQueues d pr, σ) from hq') + exact this + obtain ⟨q', hq'', hr⟩ := mem_bindA.1 hr + have eq' : q' = (tcbSchedDequeue t q, σ) := hq'' + subst eq' + have er : r = ((), { σ with readyQueues := fun d' p => + if d' = d ∧ p = pr then tcbSchedDequeue t q else σ.readyQueues d' p }) := hr + rw [er] + refine ⟨fun d' p h => ?_, hσ.2.1, hσ.2.2⟩ + simp only at h + split_ifs at h + · unfold tcbSchedDequeue at h + rw [eq] at h + exact hσ.1 d pr (List.mem_filter.1 h).1 + · exact hσ.1 d' p h + +theorem tcbReleaseEnqueue_offq {c t : ObjRef} (ne : t ≠ c) : + HT (OffQA c) (tcbReleaseEnqueue (Z := Unit) t) (fun _ => OffQA c) := by + unfold tcbReleaseEnqueue + refine HT.bind (Q := fun q σ => OffQA c σ ∧ c ∉ q) (HT.gets _ fun σ h => ⟨h, h.2.1⟩) fun q => ?_ + refine HT.bind (Q := fun q' σ => OffQA c σ ∧ c ∉ q') ?_ fun q' => ?_ + · intro σ hσ r hr + unfold readyTimesAppend at hr + obtain ⟨hs, hm⟩ := orderedInsert_spec _ _ _ _ hr + rw [hs] + exact ⟨hσ.1, fun h => (hm c h).elim (fun e => ne e.symm) hσ.2⟩ + refine HT.bind (Q := fun _ => OffQA c) ?_ fun _ => ?_ + · unfold setReleaseQueue + exact HT.modify _ fun σ h => ⟨h.1.1, h.2, h.1.2.2⟩ + · exact HT.when (fun _ => HT.modify _ fun σ h => h.congr rfl rfl rfl) (fun _ σ h => h) + +/-- The SC `p` is not bound to `c`. -/ +def ScNotA (c p : ObjRef) (σ : State Unit) : Prop := + ∀ y k, σ.kheap p = some (.schedContext y k) → y.scTcb ≠ some c + +theorem postpone_offq (c p : ObjRef) : + HT (fun σ => OffQA c σ ∧ ScNotA c p σ) (postpone (Z := Unit) p) (fun _ => OffQA c) := by + unfold postpone + refine HT.bind (Q := fun o σ => OffQA c σ ∧ o ≠ some c) ?_ fun o => ?_ + · intro σ hσ r hr + obtain ⟨e, y, k, hk, hv⟩ := getScObjRef_mem hr + rw [e, hv] + exact ⟨hσ.1, hσ.2 y k hk⟩ + refine HT.bind (Q := fun t σ => OffQA c σ ∧ t ≠ c) + (HT.assertOpt _ fun a e σ h => ⟨h.1, fun x => h.2 (by rw [e, x])⟩) fun t => ?_ + intro σ hσ r hr + have ne : t ≠ c := hσ.2 + have tail : HT (OffQA c) + (do tcbSchedAction (Z := Unit) tcbSchedDequeue t + tcbReleaseEnqueue t + NonDet.modify fun s => { s with reprogramTimer := true }) (fun _ => OffQA c) := + HT.bind (Q := fun _ => OffQA c) (tcbSchedDequeue_offq c t) fun _ => + HT.bind (Q := fun _ => OffQA c) (tcbReleaseEnqueue_offq ne) fun _ => + HT.modify _ fun σ h => h.congr rfl rfl rfl + exact tail σ hσ.1 r hr + +theorem schedContextResume_offq (c p : ObjRef) : + HT (fun σ => OffQA c σ ∧ ScNotA c p σ) (schedContextResume (Z := Unit) p) (fun _ => OffQA c) := by + let P : State Unit → Prop := fun σ => OffQA c σ ∧ ScNotA c p σ + have keep : ∀ {α : Type} {m : SMonad Unit α}, PresR (@Eq (State Unit)) m → HT P m (fun _ => P) := + fun h σ hσ r hr => by rw [← h σ r hr]; exact hσ + unfold schedContextResume + refine HT.mono (P := P) (Q := fun _ => OffQA c) ?_ (fun σ h => h) (fun _ _ h => h) + refine HT.bind (Q := fun _ => P) (keep (getSchedContext_eqPres p)) fun sc => ?_ + refine HT.bind (Q := fun _ => P) (HT.assertOpt _ fun a _ σ h => h) fun t => ?_ + refine HT.bind (Q := fun _ => P) (HT.gets _ fun σ h => h) fun sched => ?_ + refine HT.when (fun _ => ?_) (fun _ σ h => h.1) + refine HT.bind (Q := fun _ => P) (keep (threadGet_eqPres _ t)) fun ts => ?_ + refine HT.bind (Q := fun _ => P) (keep (getScRefillReady_eqPres p)) fun ready => ?_ + refine HT.bind (Q := fun _ => P) (keep (getScRefillSufficient_eqPres p 0)) fun suf => ?_ + refine HT.when (fun _ => ?_) (fun _ σ h => h.1) + refine HT.bind (Q := fun _ => P) (keep (threadGet_eqPres _ t)) fun dom => ?_ + refine HT.bind (Q := fun _ => P) (keep (threadGet_eqPres _ t)) fun prio => ?_ + refine HT.bind (Q := fun _ => P) (keep (by unfold getTcbQueue; eqpres)) fun q => ?_ + refine HT.bind (Q := fun _ => P) (HT.assert fun _ σ h => h) fun _ => ?_ + exact postpone_offq c p + +theorem maybeDonateSc_offq {c d : ObjRef} (ne : d ≠ c) (n : ObjRef) : + HT (OffQA c) (maybeDonateSc (Z := Unit) d n) (fun _ => OffQA c) := by + unfold maybeDonateSc + refine HT.bind (Q := fun _ => OffQA c) (HT.offqT (by unfold getTcbObjRef; tpres) c) fun scOpt => ?_ + refine HT.when (fun _ => ?_) (fun _ σ h => h) + refine HT.bind (Q := fun _ => OffQA c) (HT.offqT (by unfold getNtfnObjRef getSkObjRef; tpres) c) fun nsc => ?_ + cases nsc with + | none => exact HT.pure () (fun σ h => h) + | some p => + show HT _ ((do let scTcb ← getScObjRef SchedContext.scTcb p + NonDet.when (scTcb = none) (do schedContextDonate p d; schedContextResume p))) _ + refine HT.bind (Q := fun o σ => OffQA c σ ∧ (o = none → ∀ y k, σ.kheap p = some (.schedContext y k) → y.scTcb = none)) + ?_ fun o => ?_ + · intro σ hσ r hr + obtain ⟨e, y, k, hk, hv⟩ := getScObjRef_mem hr + obtain ⟨a, s'⟩ := r + simp only at e hv ⊢ + subst e + refine ⟨hσ, fun hn y' k' hk' => ?_⟩ + rw [hk] at hk' + have := Option.some.inj hk' + injection this with e1 e2 + subst e1 + rw [← hv]; exact hn + refine HT.when (fun hn => ?_) (fun _ σ h => h.1) + refine HT.bind (Q := fun _ σ => OffQA c σ ∧ ScNotA c p σ) ?_ fun _ => schedContextResume_offq c p + intro σ hσ r hr + unfold schedContextDonate at hr + obtain ⟨⟨fromOpt, σ1⟩, h1, h2⟩ := mem_bindA.1 hr + obtain ⟨e1, y, k, hk, hv⟩ := getScObjRef_mem h1 + have e1' : σ1 = σ := e1 + have hnone : fromOpt = none := by + have : fromOpt = y.scTcb := hv + rw [this]; exact hσ.2 hn y k hk + rw [e1'] at h2 + obtain ⟨⟨u, σ2⟩, h3, h4⟩ := mem_bindA.1 h2 + have e3 : ((u, σ2) : Unit × State Unit) = ((), σ) := by + unfold NonDet.when at h3 + rw [if_neg (by simp [hnone])] at h3 + exact h3 + have hσ2 : σ2 = σ := (Prod.mk.inj e3).2 + rw [hσ2] at h4 + obtain ⟨⟨u2, σ3⟩, h5, h6⟩ := mem_bindA.1 h4 + obtain ⟨y5, k5, -, e5⟩ := setScTcb_spec h5 + have e5' : σ3 = { σ with kheap := (Function.update σ.kheap p + (some (.schedContext (scTcbUpdate (fun _ => some d) y5) k5))) } := e5 + have h6' : r ∈ (threadSet (Z := Unit) (fun x => tcbSchedContextUpdate (fun _ => some p) x) d σ3).1 := h6 + obtain ⟨x6, -, e6⟩ := threadSet_spec h6' + rw [e6] + refine ⟨by rw [e5']; exact hσ.1.congr rfl rfl rfl, fun y' k' hy' => ?_⟩ + have hy'' : Function.update σ3.kheap d (some (.tcb (tcbSchedContextUpdate (fun _ => some p) x6))) p = + some (.schedContext y' k') := hy' + by_cases e : p = d + · rw [e, Function.update_self] at hy'' + cases hy'' + · rw [Function.update_of_ne e, e5'] at hy'' + simp only [Function.update_self] at hy'' + obtain ⟨rfl, -⟩ := KernelObject.schedContext.inj (Option.some.inj hy'') + exact fun h => ne (Option.some.inj h) + +theorem replyUnlinkTcb_offq (c d r : ObjRef) : + HT (OffQA c) (replyUnlinkTcb (Z := Unit) d r) (fun _ => OffQA c) := by + unfold replyUnlinkTcb + refine HT.bind (Q := fun _ => OffQA c) (HT.offqT (by tpres) c) fun _ => ?_ + refine HT.bind (Q := fun _ => OffQA c) (HT.offqT (by tpres) c) fun _ => ?_ + refine HT.bind (Q := fun _ => OffQA c) (HT.offqT (by tpres) c) fun _ => ?_ + refine HT.bind (Q := fun _ => OffQA c) (HT.offqT (by tpres) c) fun _ => ?_ + refine HT.bind (Q := fun _ => OffQA c) (HT.offqT (by tpres) c) fun _ => ?_ + exact setThreadState_offq c d _ + +theorem cancelIpcRecv_offq (c d : ObjRef) : + HT (fun σ => OffQA c σ ∧ RecvSt d σ) (cancelIpc (Z := Unit) d) (fun _ => OffQA c) := by + unfold cancelIpc + refine HT.bind (Q := fun st σ => OffQA c σ ∧ ∃ ep rp q, st = .blockedOnReceive ep rp q) ?_ fun st => ?_ + · show HT _ (threadGet (Z := Unit) Tcb.tcbState d) _ + exact HT.threadGet _ d fun σ hσ x hx => ⟨hσ.1, hσ.2 x hx⟩ + refine HT.bind (Q := fun _ σ => OffQA c σ ∧ ∃ ep rp q, st = .blockedOnReceive ep rp q) + (fun σ hσ r hr => ⟨threadSet_offq c d _ σ hσ.1 r hr, hσ.2⟩) fun _ => ?_ + cases st with + | blockedOnReceive ep rp q => + dsimp only + refine HT.mono (P := OffQA c) (Q := fun _ => OffQA c) ?_ (fun σ h => h.1) (fun _ _ h => h) + unfold blockedCancelIpc getBlockingObject + refine HT.bind (Q := fun _ => OffQA c) (HT.assertOpt _ fun a _ σ h => h) fun p => ?_ + refine HT.bind (Q := fun _ => OffQA c) (HT.offqT (by unfold tcbEpDequeue; tpres) c) fun _ => ?_ + cases rp with + | none => exact setThreadState_offq c d _ + | some r => + dsimp only + exact HT.bind (Q := fun _ => OffQA c) (replyUnlinkTcb_offq c d r) fun _ => setThreadState_offq c d _ + | _ => exact fun σ hσ _ _ => absurd hσ.2 (by rintro ⟨ep, rp, q, h⟩; cases h) + +theorem wakeTail_offq {c d : ObjRef} (ne : d ≠ c) (n : ObjRef) (badge : Badge) : + HT (OffQA c) + (do setThreadState (Z := Unit) d .running + asUser d (setRegister badge_register badge) + maybeDonateSc d n + let sched ← NonDet.gets (schedulable d) + NonDet.when sched <| possibleSwitchTo d + let scopt ← getTcbObjRef Tcb.tcbSchedContext d + ifSporadicCurScTestRefillUnblockCheck scopt) + (fun _ => OffQA c) := by + refine HT.bind (Q := fun _ => OffQA c) (setThreadState_offq c d _) fun _ => ?_ + refine HT.bind (Q := fun _ => OffQA c) (HT.offqT (by tpres) c) fun _ => ?_ + refine HT.bind (Q := fun _ => OffQA c) (maybeDonateSc_offq ne n) fun _ => ?_ + refine HT.bind (Q := fun _ => OffQA c) (HT.gets _ fun σ h => h) fun sched => ?_ + refine HT.bind (Q := fun _ => OffQA c) ?_ fun _ => ?_ + · exact HT.when (fun _ => possibleSwitchTo_offq ne) (fun _ σ h => h) + refine HT.bind (Q := fun _ => OffQA c) (HT.offqT (by unfold getTcbObjRef; tpres) c) fun _ => ?_ + exact HT.offqT (by unfold ifSporadicCurScTestRefillUnblockCheck ifCondRefillUnblockCheck; tpres) c + +/-- **`send_signal` keeps `OffQA c`** of a runnable `c` (module docstring). -/ +theorem sendSignal_offQ (c n : ObjRef) (badge : Badge) : + HT (fun σ => OffQA c σ ∧ (∃ x, σ.kheap c = some (.tcb x) ∧ runnable x.tcbState = true) ∧ SigQ n σ) + (sendSignal (Z := Unit) n badge) (fun _ => OffQA c) := by + unfold sendSignal + let P : State Unit → Prop := fun σ => + OffQA c σ ∧ (∃ x, σ.kheap c = some (.tcb x) ∧ runnable x.tcbState = true) ∧ SigQ n σ + refine HT.bind (Q := fun ntfn σ => P σ ∧ σ.kheap n = some (.notification ntfn)) ?_ fun ntfn => ?_ + · intro σ hσ r hr + obtain ⟨hk, hs⟩ := getNotification_mem hr + obtain ⟨a, s'⟩ := r + simp only at hk hs ⊢ + subst hs + exact ⟨hσ, hk⟩ + have ns : ∀ (o : Notification), HT (fun σ => P σ ∧ σ.kheap n = some (.notification ntfn)) + (setNotification (Z := Unit) n o) (fun _ => OffQA c) := fun o σ hσ r hr => + OffQA.tkeep ((by tpres : TPres (setNotification (Z := Unit) n o)) σ r hr) hσ.1.1 + rcases ntfn with ⟨o, bt, nsc⟩ + cases o with + | idleNtfn => + cases bt with + | none => exact ns _ + | some tcb => + dsimp only + refine HT.bind (Q := fun st σ => (P σ ∧ + σ.kheap n = some (.notification { ntfnObj := .idleNtfn, ntfnBoundTcb := some tcb, ntfnSc := nsc })) ∧ + ∃ x, σ.kheap tcb = some (.tcb x) ∧ x.tcbState = st) ?_ fun st => ?_ + · show HT _ (threadGet (Z := Unit) Tcb.tcbState tcb) _ + exact HT.threadGet _ tcb fun σ hσ x hx => ⟨hσ, x, hx, rfl⟩ + refine HT.ite (fun hc => ?_) (fun hc => HT.mono (ns _) (fun σ h => h.1) (fun _ _ h => h)) + cases st with + | blockedOnReceive ep rp q => + by_cases e : tcb = c + · intro σ hσ r _ + obtain ⟨⟨⟨-, ⟨x, hx, hr⟩, -⟩, -⟩, y, hy, hs⟩ := hσ + rw [e, hx] at hy + obtain rfl := KernelObject.tcb.inj (Option.some.inj hy) + rw [hs] at hr + simp [runnable] at hr + refine HT.bind (Q := fun _ => OffQA c) (HT.mono (cancelIpcRecv_offq c tcb) (fun σ h => ?_) (fun _ _ h => h)) + fun _ => wakeTail_offq e n badge + obtain ⟨⟨⟨hq, -, -⟩, -⟩, x, hx, hst⟩ := h + refine ⟨hq, fun y hy => ?_⟩ + rw [hx] at hy + obtain rfl := KernelObject.tcb.inj (Option.some.inj hy) + exact ⟨ep, rp, q, hst⟩ + | _ => simp [receiveBlocked] at hc + | waitingNtfn q => + dsimp only + unfold updateWaitingNtfn + refine HT.bind (Q := fun _ σ => (P σ ∧ + σ.kheap n = some (.notification { ntfnObj := .waitingNtfn q, ntfnBoundTcb := bt, ntfnSc := nsc })) ∧ q ≠ []) + (HT.assert fun h σ hσ => ⟨hσ, h⟩) fun _ => ?_ + refine HT.pure_bind ?_ + by_cases e : the q.head? = c + · intro σ hσ r _ + obtain ⟨⟨⟨-, ⟨x, hx, hr⟩, hS⟩, hk⟩, hne⟩ := hσ + have hm : the q.head? ∈ q := by + cases q with + | nil => exact absurd rfl hne + | cons a l => exact List.mem_cons_self + obtain ⟨y, hy, hs⟩ := hS _ q _ hk rfl hm + rw [e, hx] at hy + obtain rfl := KernelObject.tcb.inj (Option.some.inj hy) + rw [hs] at hr + simp [runnable] at hr + refine HT.bind (Q := fun _ => OffQA c) + (HT.mono (HT.offqT (by unfold tcbNtfnDequeue; tpres) c) (fun σ h => h.1.1.1) (fun _ _ h => h)) + fun _ => wakeTail_offq e n badge + | activeNtfn b => exact ns _ + +end + +end Fl4ProofsA64.P3.Sched + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4Spec Fl4SpecA64 + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {s : S} {a : A} {σ : State Unit} {m : McState} + +set_option maxHeartbeats 2000000 in +/-- **From the end record of a waking signal to `RecvPreA`** (module docstring). -/ +theorem JSyscallA.nbSignalWakeEpiA + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + {xc : Tcb} (hkc : σ.kheap σ.curThread = some (.tcb xc)) {p : ObjRef} {b : Badge} + {σ2 : State Unit} {s2 : S} + (hm : ((), σ2) ∈ (sendSignal (Z := Unit) p b (stUpdA σ σ.curThread xc .restart)).1) + (hI : FIA (stUpdA σ σ.curThread xc .restart)) (hD : RqDomPrio (stUpdA σ σ.curThread xc .restart)) + (hid : IdleSt (stUpdA σ σ.curThread xc .restart)) (hQ : SigQ p (stUpdA σ σ.curThread xc .restart)) + {Q1 : U64 → U64 → List U64} {R1 : U64 → List U64} + (J2 : JStage3A st AR { Z with Q := Q1, R := R1 } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s2 a σ2 m) : + getThreadState (Z := Unit) σ.curThread σ2 = ({(.restart, σ2)}, False) ∧ + ∃ σ3 s3, ((), σ3) ∈ (setThreadState (Z := Unit) σ.curThread .running σ2).1 ∧ + obj.Tcb.state st (L.curW s) s2 = ok sched.TS_RESTART ∧ + sched.set_thread_state st s2 (L.curW s) sched.TS_RUNNING = ok s3 ∧ + syscall.after_invocation st ar s3 a (.Ok ()) = ok (true, s3, a) ∧ + L.curW s3 = L.curW s ∧ + (∀ x, σ3.kheap σ3.curThread = some (.tcb x) → x.tcbSchedContext = some σ3.curSc) ∧ + ∃ Q Rl, RecvPreA st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n + (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s3 a σ3 m := by + set t := L.curW s with ht_def + have ct : σ.curThread = specPtr t := J.j.1.sch.curThread + have P0' := J.toRecvPreA + have B := Sched.BodyEndA.ofSignalA J hkc hm hI hD hid hQ J2.sr.inv J2.sinv.zc + have thr : σ2.curThread = σ.curThread := B.2 + set σ1 := stUpdA σ σ.curThread xc .restart with hσ1 + have hk1 : σ1.kheap σ.curThread = some (.tcb { xc with tcbState := .restart }) := by + simp [hσ1, stUpdA, setObjA] + have BK : BKeepA σ1 σ2 := sendSignal_bkA p b _ _ hm + have hxsc : xc.tcbSchedContext = some σ.curSc := by + obtain ⟨y, hy, e, -⟩ := J.curInv.tcb + rw [hkc] at hy + cases hy + exact e + have CB : CurBA σ2 := by + refine sendSignal_curBA p b σ1 ⟨⟨_, hk1, hxsc⟩, fun x hx => ?_, hQ⟩ _ hm + have hx' : σ1.kheap σ.curThread = some (.tcb x) := hx + rw [hk1] at hx' + obtain rfl := KernelObject.tcb.inj (Option.some.inj hx') + rfl + have off1 : OffQA σ.curThread σ1 := + ⟨fun d p => P0'.noQ d p, P0'.notRel, fun u e => absurd e (J.noSwitch u)⟩ + have off2 : OffQA σ.curThread σ2 := sendSignal_offQ σ.curThread p b σ1 ⟨off1, ⟨_, hk1, rfl⟩, hQ⟩ _ hm + have ST := sendSignal_ctStMA p b _ hk1 (Or.inr (Or.inl rfl)) hI hD hid hQ hm + obtain ⟨x2, hk2, hsc2⟩ := CB + have hk2c : σ2.kheap σ.curThread = some (.tcb x2) := thr ▸ hk2 + have hst2 : x2.tcbState = .restart := ST x2 hk2c + have csc2 : σ2.curSc = σ.curSc := BK.csc + have act2 : ∀ sc, x2.tcbSchedContext = some sc → ScActiveAtA σ2 sc := fun sc hs => by + have e : σ2.curSc = sc := Option.some.inj (hsc2.symm.trans hs) + subst e + obtain ⟨y', k, hy'⟩ := J2.sinv.curSc + refine ⟨y', k, hy', ?_⟩ + obtain ⟨y, hy, -, e2⟩ := BK.sc y' k (by + show σ2.kheap σ.curSc = _ + rw [← csc2]; exact hy') + have ne : σ.curSc ≠ σ.curThread := fun e => by + obtain ⟨y0, j, h0⟩ := J.curInv.sc + rw [e, hkc] at h0; cases h0 + have hy0 : σ.kheap σ.curSc = some (.schedContext y k) := by + have : σ1.kheap σ.curSc = some (.schedContext y k) := hy + simpa [hσ1, stUpdA, setObjA, Function.update_of_ne ne] using this + obtain ⟨z, j, hz, ha⟩ := P0'.scAct xc _ hkc hxsc + rw [hy0] at hz + obtain ⟨rfl, -⟩ := KernelObject.schedContext.inj (Option.some.inj hz) + unfold scActive at ha ⊢ + rw [e2]; exact ha + have get2 : getThreadState (Z := Unit) σ.curThread σ2 = ({(.restart, σ2)}, False) := by + rw [← hst2]; exact threadGet_eqA Tcb.tcbState hk2c + obtain ⟨σ3, s3, mem3, hstate, eRun, haft, hcur3, c3, own3, Q3, R3, P3⟩ := + J.epiBridgeA hA hPl J2 B.1 thr hk2 hst2 (fun d p => by rw [thr]; exact off2.1 d p) (by rw [thr]; exact off2.2.1) + act2 (fun e => off2.2.2 _ e thr) + refine ⟨get2, σ3, s3, mem3, hstate, eRun, haft, hcur3, fun x hx => ?_, Q3, R3, P3⟩ + rw [own3 x hx, c3] + exact hsc2 + +set_option maxHeartbeats 2000000 in +/-- **`NBSendRecv` / `NBSendWait` as a signal on a waiting notification ends in `RecvPreA`** (module docstring). -/ +theorem JSyscallA.nbSignalWaitingFirstA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hn : fl4A64Platform.numNodes ≤ n) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p badge slot lbl len buf : U64} {recv blk call don fp : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Notification p badge true recv) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hp : p ∈ N) {x : Notification} (hk : σ.kheap (specPtr p) = some (.notification x)) + {q : List ObjRef} (hw : x.ntfnObj = .waitingNtfn q) : + ∃ xc σ1 σ2 σ3 s2 s3, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ((), σ2) ∈ (sendSignal (Z := Unit) (specPtr p) badge.bv σ1).1 ∧ + getThreadState (Z := Unit) σ.curThread σ2 = ({(.restart, σ2)}, False) ∧ + ((), σ3) ∈ (setThreadState (Z := Unit) σ.curThread .running σ2).1 ∧ + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don fp buf extra = + ok (.Ok (), s2, a) ∧ + obj.Tcb.state st (L.curW s) s2 = ok sched.TS_RESTART ∧ + sched.set_thread_state st s2 (L.curW s) sched.TS_RUNNING = ok s3 ∧ + syscall.after_invocation st ar s3 a (.Ok ()) = ok (true, s3, a) ∧ + L.curW s3 = L.curW s ∧ + (∀ x, σ3.kheap σ3.curThread = some (.tcb x) → x.tcbSchedContext = some σ3.curSc) ∧ + ∃ Q Rl, RecvPreA st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n + (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s3 a σ3 m := by + obtain ⟨xc, σ1, σ2, s2, hkc, hσ1, hsp, hsig, hdec, ⟨hI, hD, hid, hQ⟩, Q1, R1, J2⟩ := + J.signalWaitingA hA hM hPl hfm hCs h1 hn hmb hmax htu (lbl := lbl) (len := len) (buf := buf) (blk := blk) + (call := call) (don := don) (fp := fp) hslot hcov hcap hext hSC hp hk hw + subst hσ1 + obtain ⟨get2, σ3, s3, mem3, hstate, eRun, haft, hcur3, own3, Q3, R3, P3⟩ := + J.nbSignalWakeEpiA hA hPl hkc hsig hI hD hid hQ J2 + exact ⟨xc, _, σ2, σ3, s2, s3, hkc, rfl, hsp, hsig, get2, mem3, hdec, hstate, eRun, haft, hcur3, own3, Q3, R3, P3⟩ + +set_option maxHeartbeats 2000000 in +/-- **`NBSendRecv` / `NBSendWait` as a signal to a bound receiver ends in `RecvPreA`** (module docstring). -/ +theorem JSyscallA.nbSignalBoundFirstA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hn : fl4A64Platform.numNodes ≤ n) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p badge slot lbl len buf : U64} {recv blk call don fp : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Notification p badge true recv) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hp : p ∈ N) {x : Notification} (hk : σ.kheap (specPtr p) = some (.notification x)) + (hidle : x.ntfnObj = .idleNtfn) {t : ObjRef} (hb : x.ntfnBoundTcb = some t) {xt : Tcb} + (hkt : σ.kheap t = some (.tcb xt)) {e' : ObjRef} {ro : Option ObjRef} {d : ReceiverPayload} + (hs : xt.tcbState = .blockedOnReceive e' ro d) : + ∃ xc σ1 σ2 σ3 s2 s3, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ((), σ2) ∈ (sendSignal (Z := Unit) (specPtr p) badge.bv σ1).1 ∧ + getThreadState (Z := Unit) σ.curThread σ2 = ({(.restart, σ2)}, False) ∧ + ((), σ3) ∈ (setThreadState (Z := Unit) σ.curThread .running σ2).1 ∧ + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don fp buf extra = + ok (.Ok (), s2, a) ∧ + obj.Tcb.state st (L.curW s) s2 = ok sched.TS_RESTART ∧ + sched.set_thread_state st s2 (L.curW s) sched.TS_RUNNING = ok s3 ∧ + syscall.after_invocation st ar s3 a (.Ok ()) = ok (true, s3, a) ∧ + L.curW s3 = L.curW s ∧ + (∀ x, σ3.kheap σ3.curThread = some (.tcb x) → x.tcbSchedContext = some σ3.curSc) ∧ + ∃ Q Rl, RecvPreA st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n + (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s3 a σ3 m := by + obtain ⟨xc, σ1, σ2, s2, hkc, hσ1, hsp, hsig, hdec, ⟨hI, hD, hid, hQ⟩, Q1, R1, J2⟩ := + J.signalBoundRecvA hA hM hPl hfm hCs h1 hn hmb hmax htu (lbl := lbl) (len := len) (buf := buf) (blk := blk) + (call := call) (don := don) (fp := fp) hslot hcov hcap hext hSC hp hk hidle hb hkt hs + subst hσ1 + obtain ⟨get2, σ3, s3, mem3, hstate, eRun, haft, hcur3, own3, Q3, R3, P3⟩ := + J.nbSignalWakeEpiA hA hPl hkc hsig hI hD hid hQ J2 + exact ⟨xc, _, σ2, σ3, s2, s3, hkc, rfl, hsp, hsig, get2, mem3, hdec, hstate, eRun, haft, hcur3, own3, Q3, R3, P3⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3NBWakeEntryA.lean b/proofs/Fl4ProofsA64/P3/Stage3NBWakeEntryA.lean new file mode 100644 index 000000000..ab3c1a26e --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3NBWakeEntryA.lean @@ -0,0 +1,232 @@ +/- +P3 (aarch64, A64-P3 line RecvPreA): **`NBSendRecv` / `NBSendWait` whose send half wakes a thread, from the syscall +entry to `handle_recv`** (`P3/Stage3CombinedFrontA`'s front end over the first halves of +`P3/Stage3NBSignalWakeA` and `P3/Stage3NBSendIpcA`). + +* **`JSyscallA.nbSignalWaitingRecvEntryA`**, **`nbSignalBoundRecvEntryA`**: the waking signals. +* **`JSyscallA.nbSendDeliverRecvEntryA`**: a send to a waiting receiver with its own SC. +* **`JSyscallA.nbSendDonRecvEntryA`**: a send donating the caller's SC to a passive receiver. + +Compose with `JSyscallA.combinedDispatchA` and a `Recv` arm's `HRecvPostA`. [S] those of the first half. No named +premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3CombinedFrontA +import Fl4ProofsA64.P3.Stage3NBSendIpcA +import Fl4ProofsA64.P3.Stage3SendDonPlainArmA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {s : S} {a : A} {σ : State Unit} {m : McState} + +set_option maxHeartbeats 1000000 in +/-- **A signal on a waiting notification, from the syscall entry to `handle_recv`** (module docstring). -/ +theorem JSyscallA.nbSignalWaitingRecvEntryA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hn : fl4A64Platform.numNodes ≤ n) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (rep : Bool) {cptr : U64} {info : msginfo.MessageInfo} {buf : U64} {r1 : cspace.Resolved} + {e : Aeneas.Std.Array obj.Slot 3#usize} + (einfo : msg.msg_info st s (L.curW s) = ok info) + (ebuf : msg.lookup_ipc_buffer st s false (L.curW s) = ok buf) + (elr : cspace.lookup_slot st s (L.curW s) cptr = ok (.Ok r1)) + (eex : ipc.lookup_extra_caps st s (L.curW s) buf info = ok (.Ok e)) + {p badge : U64} {recv : Bool} (hcap : capA st s r1.slot = .Notification p badge true recv) + (hp : p ∈ N) {x : Notification} (hk : σ.kheap (specPtr p) = some (.notification x)) + {q : List ObjRef} (hw : x.ntfnObj = .waitingNtfn q) : + ∃ s3 σ3, syscall.handle_invocation st ar s a false false true true cptr = ok (.Ok (), s3, a) ∧ + (.ok (), σ3) ∈ ((fl4HandleInvocationR (Z := Unit) rep false false true true + (Fl4SpecA64.toBl cptr.bv)).run σ).1 ∧ + syscall.after_invocation st ar s3 a (.Ok ()) = ok (true, s3, a) ∧ L.curW s3 = L.curW s ∧ + (∀ x, σ3.kheap σ3.curThread = some (.tcb x) → x.tcbSchedContext = some σ3.curSc) ∧ + ∃ Q Rl, RecvPreA st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n + (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s3 a σ3 m := by + obtain ⟨mi, sl, c, excaps, buffer, argsU, T⟩ := J.siteA einfo ebuf elr eex + obtain ⟨xc, σ1, σ2, σ3, s2, s3, hkc, hσ1, hsp, hop, get2, mem3, hdec, hstate, eRun, haft, hcur3, own3, Q, Rl, P⟩ := + J.nbSignalWaitingFirstA (lbl := info.label) (len := msgLenA info buf) (buf := buf) (blk := false) (call := false) + (don := true) (fp := true) hA hM hPl hfm hCs h1 hn hmb hmax htu T.hOS T.cov hcap T.hext T.hSC hp hk hw + have perf : (.ok [], σ2) ∈ ((fl4PerformInvocation (Z := Unit) false false true + (.invokeNotification (specPtr p) badge.bv)).run σ1).1 := by + have run : fl4PerformInvocation (Z := Unit) false false true (.invokeNotification (specPtr p) badge.bv) = + (do withoutPreemption <| sendSignal (specPtr p) badge.bv + Fl4Spec.NonDetE.returnOk []) := rfl + rw [run, ExceptT.run_bind] + exact mem_bindA.2 ⟨(.ok (), σ2), liftE_memA hop, Set.mem_singleton _⟩ + have F := J.frontOkA rep einfo ebuf elr eex (oper := .invokeNotification (specPtr p) badge.bv) + (σ1 := σ1) (σ2 := σ2) (σ3 := σ3) (s2 := s2) (s3 := s3) (fun c hcr lbl args ci sl excaps buffer => by + have h2 : CapEqA (.Notification p badge true recv) c := hcap ▸ hcr.2 + have ec : absCapA (.Notification p badge true recv) = c := h2 + rw [← ec, decodeNotification_specA, if_pos rfl] + exact Set.mem_singleton _) + hsp perf get2 mem3 hdec hstate eRun + exact ⟨s3, σ3, F.1, F.2, haft, hcur3, own3, Q, Rl, P⟩ + +set_option maxHeartbeats 1000000 in +/-- **A signal to a bound receiver, from the syscall entry to `handle_recv`** (module docstring). -/ +theorem JSyscallA.nbSignalBoundRecvEntryA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hn : fl4A64Platform.numNodes ≤ n) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (rep : Bool) {cptr : U64} {info : msginfo.MessageInfo} {buf : U64} {r1 : cspace.Resolved} + {e : Aeneas.Std.Array obj.Slot 3#usize} + (einfo : msg.msg_info st s (L.curW s) = ok info) + (ebuf : msg.lookup_ipc_buffer st s false (L.curW s) = ok buf) + (elr : cspace.lookup_slot st s (L.curW s) cptr = ok (.Ok r1)) + (eex : ipc.lookup_extra_caps st s (L.curW s) buf info = ok (.Ok e)) + {p badge : U64} {recv : Bool} (hcap : capA st s r1.slot = .Notification p badge true recv) + (hp : p ∈ N) {x : Notification} (hk : σ.kheap (specPtr p) = some (.notification x)) + (hidle : x.ntfnObj = .idleNtfn) {t : ObjRef} (hb : x.ntfnBoundTcb = some t) {xt : Tcb} + (hkt : σ.kheap t = some (.tcb xt)) {e' : ObjRef} {ro : Option ObjRef} {d : ReceiverPayload} + (hs : xt.tcbState = .blockedOnReceive e' ro d) : + ∃ s3 σ3, syscall.handle_invocation st ar s a false false true true cptr = ok (.Ok (), s3, a) ∧ + (.ok (), σ3) ∈ ((fl4HandleInvocationR (Z := Unit) rep false false true true + (Fl4SpecA64.toBl cptr.bv)).run σ).1 ∧ + syscall.after_invocation st ar s3 a (.Ok ()) = ok (true, s3, a) ∧ L.curW s3 = L.curW s ∧ + (∀ x, σ3.kheap σ3.curThread = some (.tcb x) → x.tcbSchedContext = some σ3.curSc) ∧ + ∃ Q Rl, RecvPreA st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n + (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s3 a σ3 m := by + obtain ⟨mi, sl, c, excaps, buffer, argsU, T⟩ := J.siteA einfo ebuf elr eex + obtain ⟨xc, σ1, σ2, σ3, s2, s3, hkc, hσ1, hsp, hop, get2, mem3, hdec, hstate, eRun, haft, hcur3, own3, Q, Rl, P⟩ := + J.nbSignalBoundFirstA (lbl := info.label) (len := msgLenA info buf) (buf := buf) (blk := false) (call := false) + (don := true) (fp := true) hA hM hPl hfm hCs h1 hn hmb hmax htu T.hOS T.cov hcap T.hext T.hSC hp hk hidle hb hkt hs + have perf : (.ok [], σ2) ∈ ((fl4PerformInvocation (Z := Unit) false false true + (.invokeNotification (specPtr p) badge.bv)).run σ1).1 := by + have run : fl4PerformInvocation (Z := Unit) false false true (.invokeNotification (specPtr p) badge.bv) = + (do withoutPreemption <| sendSignal (specPtr p) badge.bv + Fl4Spec.NonDetE.returnOk []) := rfl + rw [run, ExceptT.run_bind] + exact mem_bindA.2 ⟨(.ok (), σ2), liftE_memA hop, Set.mem_singleton _⟩ + have F := J.frontOkA rep einfo ebuf elr eex (oper := .invokeNotification (specPtr p) badge.bv) + (σ1 := σ1) (σ2 := σ2) (σ3 := σ3) (s2 := s2) (s3 := s3) (fun c hcr lbl args ci sl excaps buffer => by + have h2 : CapEqA (.Notification p badge true recv) c := hcap ▸ hcr.2 + have ec : absCapA (.Notification p badge true recv) = c := h2 + rw [← ec, decodeNotification_specA, if_pos rfl] + exact Set.mem_singleton _) + hsp perf get2 mem3 hdec hstate eRun + exact ⟨s3, σ3, F.1, F.2, haft, hcur3, own3, Q, Rl, P⟩ + +set_option maxHeartbeats 1000000 in +/-- **A send to a receiver with its own SC, from the syscall entry to `handle_recv`** (module docstring). -/ +theorem JSyscallA.nbSendDeliverRecvEntryA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (rep : Bool) {cptr : U64} {info : msginfo.MessageInfo} {buf : U64} {r1 : cspace.Resolved} + {e : Aeneas.Std.Array obj.Slot 3#usize} + (einfo : msg.msg_info st s (L.curW s) = ok info) + (ebuf : msg.lookup_ipc_buffer st s false (L.curW s) = ok buf) + (elr : cspace.lookup_slot st s (L.curW s) cptr = ok (.Ok r1)) + (eex : ipc.lookup_extra_caps st s (L.curW s) buf info = ok (.Ok e)) + {p badge : U64} {recv grant grantReply : Bool} + (hcap : capA st s r1.slot = .Endpoint p badge true recv grant grantReply) + (hp : p ∈ E) {d : U64} {q : List ObjRef} + (hk : σ.kheap (specPtr p) = some (.endpoint (.recvEP (specPtr d :: q)))) + (hsnd : ∀ x, σ.kheap σ.curThread = some (.tcb x) → x.tcbFault = none ∧ (grant = false ∨ + ipcBufOfA false x = none ∨ + unat (dataToMessageInfo (x.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0)) + (hrcv : ∀ y, σ.kheap (specPtr d) = some (.tcb y) → y.tcbSchedContext ≠ none) + (hdne : specPtr d ≠ σ.curThread) : + ∃ s3 σ3, syscall.handle_invocation st ar s a false false true true cptr = ok (.Ok (), s3, a) ∧ + (.ok (), σ3) ∈ ((fl4HandleInvocationR (Z := Unit) rep false false true true + (Fl4SpecA64.toBl cptr.bv)).run σ).1 ∧ + syscall.after_invocation st ar s3 a (.Ok ()) = ok (true, s3, a) ∧ L.curW s3 = L.curW s ∧ + (∀ x, σ3.kheap σ3.curThread = some (.tcb x) → x.tcbSchedContext = some σ3.curSc) ∧ + ∃ Q Rl, RecvPreA st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n + (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s3 a σ3 m := by + obtain ⟨mi, sl, c, excaps, buffer, argsU, T⟩ := J.siteA einfo ebuf elr eex + obtain ⟨xc, σ1, σ2, σ3, s2, s3, hkc, hσ1, hsp, hop, get2, mem3, hdec, hstate, eRun, haft, hcur3, own3, Q, Rl, P⟩ := + J.nbSendDeliverFirstA (lbl := info.label) (len := msgLenA info buf) (buf := buf) (fp := true) hA hM hPl hfm + hCs h1 hmb hmax htu T.hOS T.cov hcap T.hext T.hSC hp hk hsnd hrcv hdne + have f1 : σ1.curThread = σ.curThread := by rw [hσ1]; rfl + have perf : (.ok [], σ2) ∈ ((fl4PerformInvocation (Z := Unit) false false true + (.invokeEndpoint (specPtr p) badge.bv grant grantReply)).run σ1).1 := by + refine liftE_memA (mem_bindA.2 ⟨(σ.curThread, σ1), ?_, ?_⟩) + · rw [← f1]; exact Set.mem_singleton _ + · exact mem_bindA.2 ⟨((), σ2), hop, Set.mem_singleton _⟩ + have F := J.frontOkA rep einfo ebuf elr eex (oper := .invokeEndpoint (specPtr p) badge.bv grant grantReply) + (σ1 := σ1) (σ2 := σ2) (σ3 := σ3) (s2 := s2) (s3 := s3) (fun c hcr lbl args ci sl excaps buffer => by + have h2 : CapEqA (.Endpoint p badge true recv grant grantReply) c := hcap ▸ hcr.2 + have ec : absCapA (.Endpoint p badge true recv grant grantReply) = c := h2 + rw [← ec, decodeEndpoint_specA, if_pos rfl] + exact Set.mem_singleton _) + hsp perf get2 mem3 hdec hstate eRun + exact ⟨s3, σ3, F.1, F.2, haft, hcur3, own3, Q, Rl, P⟩ + +set_option maxHeartbeats 1000000 in +/-- **A send donating to a passive receiver, from the syscall entry to `handle_recv`** (module docstring). -/ +theorem JSyscallA.nbSendDonRecvEntryA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (rep : Bool) {cptr : U64} {info : msginfo.MessageInfo} {buf : U64} {r1 : cspace.Resolved} + {e : Aeneas.Std.Array obj.Slot 3#usize} + (einfo : msg.msg_info st s (L.curW s) = ok info) + (ebuf : msg.lookup_ipc_buffer st s false (L.curW s) = ok buf) + (elr : cspace.lookup_slot st s (L.curW s) cptr = ok (.Ok r1)) + (eex : ipc.lookup_extra_caps st s (L.curW s) buf info = ok (.Ok e)) + {p badge : U64} {recv grant grantReply : Bool} + (hcap : capA st s r1.slot = .Endpoint p badge true recv grant grantReply) + (hp : p ∈ E) {d : U64} {q : List ObjRef} + (hk : σ.kheap (specPtr p) = some (.endpoint (.recvEP (specPtr d :: q)))) + (hsnd : ∀ x, σ.kheap σ.curThread = some (.tcb x) → x.tcbFault = none ∧ (grant = false ∨ + ipcBufOfA false x = none ∨ + unat (dataToMessageInfo (x.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0)) + (hrcv : ∀ y, σ.kheap (specPtr d) = some (.tcb y) → y.tcbSchedContext = none) + (hV : L.mem s (core.num.U64.wrapping_add (L.curW s) 3664#u64) = 0#u64) : + ∃ s3 σ3, syscall.handle_invocation st ar s a false false true true cptr = ok (.Ok (), s3, a) ∧ + (.ok (), σ3) ∈ ((fl4HandleInvocationR (Z := Unit) rep false false true true + (Fl4SpecA64.toBl cptr.bv)).run σ).1 ∧ + syscall.after_invocation st ar s3 a (.Ok ()) = ok (true, s3, a) ∧ L.curW s3 = L.curW s ∧ + (∀ x, σ3.kheap σ3.curThread = some (.tcb x) → x.tcbSchedContext = none) ∧ + ∃ Q Rl, RecvPreA st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n + (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s3 a σ3 m := by + obtain ⟨mi, sl, c, excaps, buffer, argsU, T⟩ := J.siteA einfo ebuf elr eex + obtain ⟨xc, σ1, σ2, σ3, s2, s3, hkc, hσ1, hsp, hop, get2, mem3, hdec, hstate, eRun, haft, hcur3, own3, Q, Rl, P⟩ := + J.nbSendDonFirstA (lbl := info.label) (len := msgLenA info buf) (buf := buf) (fp := true) hA hM hPl hfm + hCs h1 hmb hmax htu T.hOS T.cov hcap T.hext T.hSC hp hk hsnd hrcv hV + have f1 : σ1.curThread = σ.curThread := by rw [hσ1]; rfl + have perf : (.ok [], σ2) ∈ ((fl4PerformInvocation (Z := Unit) false false true + (.invokeEndpoint (specPtr p) badge.bv grant grantReply)).run σ1).1 := by + refine liftE_memA (mem_bindA.2 ⟨(σ.curThread, σ1), ?_, ?_⟩) + · rw [← f1]; exact Set.mem_singleton _ + · exact mem_bindA.2 ⟨((), σ2), hop, Set.mem_singleton _⟩ + have F := J.frontOkA rep einfo ebuf elr eex (oper := .invokeEndpoint (specPtr p) badge.bv grant grantReply) + (σ1 := σ1) (σ2 := σ2) (σ3 := σ3) (s2 := s2) (s3 := s3) (fun c hcr lbl args ci sl excaps buffer => by + have h2 : CapEqA (.Endpoint p badge true recv grant grantReply) c := hcap ▸ hcr.2 + have ec : absCapA (.Endpoint p badge true recv grant grantReply) = c := h2 + rw [← ec, decodeEndpoint_specA, if_pos rfl] + exact Set.mem_singleton _) + hsp perf get2 mem3 hdec hstate eRun + exact ⟨s3, σ3, F.1, F.2, haft, hcur3, own3, Q, Rl, P⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3NtfnFootA.lean b/proofs/Fl4ProofsA64/P3/Stage3NtfnFootA.lean new file mode 100644 index 000000000..2f3b76870 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3NtfnFootA.lean @@ -0,0 +1,57 @@ +/- +P3 (aarch64, A64-P3 Stage-3 integration, D8/T1 step 2): **from the notification footprint `FpNA` to the Stage-3 +write set `SchedStepNWA`** (the IRQ-arm / `timer.fire` signal frames). + +* **`nwrite_stepNA`**: a word the notification code writes (`NWrite`: the first 40 bytes of a notification, the TCB + words `stA`, `tBlockA`, `badgeRegA`, the `Ep` links, the fault type) is a notification word (`IpcObjWA`) or a TCB word + of `TcbStepWA`. +* **`FpNA.stepN`**: `FpNA … MWrA` is `WritesIn … SchedStepNWA`. + +No named premise. +-/ +import Fl4ProofsA64.P3.Stage3IpcFootA +import Fl4ProofsA64.P3.SchedFootNA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Notif +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4ProofsA64.P3.Sched + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **A notification-code word is a Stage-3 scheduler-step word.** -/ +theorem nwrite_stepNA {N E R : Set U64} {Z : XSetsA} {m : Fl4SpecA64.McState} {σ : Fl4SpecA64.State Unit} {s : S} + (hP : TcbPlaced Z.T) {b : U64} (h : NWrite N Z.T b) : SchedStepNWA (st := st) N E R Z m σ s b := by + rcases h with ⟨n, hn, h1, h2⟩ | ⟨t, ht, h⟩ + · exact Or.inr (Or.inl ⟨n, hn, h1, by omega⟩) + · have hta := hP t ht + have hfit := hta.fits + have wv : ∀ K : U64, K.val < 4096 → (wa t K).val = t.val + K.val := by + intro K hK; rw [wa_valm]; omega + refine Or.inl (Or.inr ?_) + rcases h with rfl | rfl | rfl | rfl | rfl | rfl + · exact Endp.tcbStepWA_of_off hP ht 3328 (by omega) (by omega) (by omega) (by omega) (Endp.val_st hta) + · exact Endp.tcbStepWA_of_off hP ht 3336 (by omega) (by omega) (by omega) (by omega) (Endp.val_blk hta) + · refine Endp.tcbStepWA_of_off hP ht 256 (by omega) (by omega) (by omega) (by omega) ?_ + rw [badgeRegA_eq, wv _ (by simp)]; rfl + · exact Endp.tcbStepWA_of_off hP ht 3496 (by omega) (by omega) (by omega) (by omega) (Endp.val_next hta) + · exact Endp.tcbStepWA_of_off hP ht 3504 (by omega) (by omega) (by omega) (by omega) (Endp.val_prev hta) + · refine Endp.tcbStepWA_of_off hP ht 3376 (by omega) (by omega) (by omega) (by omega) ?_ + rw [wv _ (by simp)]; rfl + +/-- **The notification footprint is a Stage-3 write set.** -/ +theorem FpNA.stepN {N E R : Set U64} {Z : XSetsA} {m : Fl4SpecA64.McState} {σ : Fl4SpecA64.State Unit} {s s' : S} + (hP : TcbPlaced Z.T) + (h : FpNA st N Z.T (fun σ s => MWrA (st := st) Z m σ s) σ s s') : + WritesIn st s s' (SchedStepNWA (st := st) N E R Z m σ s) := + h.mono fun _ hb => hb.elim (nwrite_stepNA hP) fun hw => Or.inl (Or.inl hw) + +end + +end + +end Fl4ProofsA64.P3.Notif diff --git a/proofs/Fl4ProofsA64/P3/Stage3NtfnIdleFootA.lean b/proofs/Fl4ProofsA64/P3/Stage3NtfnIdleFootA.lean new file mode 100644 index 000000000..873d79610 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3NtfnIdleFootA.lean @@ -0,0 +1,120 @@ +/- +P3 (aarch64, A64-P3 Stage-3 integration, D8/T1 step 2): **the write footprint of `send_signal_idle_bound_recv`** +(`IpcCancelA`; the notification is idle and its bound TCB is blocked on a receive: the receive is cancelled, then the +TCB is woken). + +* **`send_signal_idle_bound_recvF`**: the posts of `send_signal_idle_bound_recv` and + `WritesIn s r.1 (EWrite ∨ NWrite ∨ Wr σ s ∨ action word)`: `cancel_ipc_footA` (the endpoint code's words, the + notification code's words, the action word) followed by `wake_refinesF` (`FpNA`), joined by `FpNA.trans`-style + transport of `Wr` along `SSzA σ σ1` (`cancelIpc_recv_out`: every SC of the post state is one of the pre-state). + `Bf` stays generic: `EWrite` keeps its `BufW Bf` disjunct, which a consumer maps to the buffer words + (`Stage3BufWrA`) or to `∅`. + +No named premise. +-/ +import Fl4ProofsA64.P3.Stage3NtfnFootA +import Fl4ProofsA64.P3.NtfnWakeFootA +import Fl4ProofsA64.P3.CancelIpcFootA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Notif +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4ProofsA64.P3.Sched Endp + +noncomputable section + +section +open Fl4SpecA64 +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + {N T E R : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {SR : S → Fl4SpecA64.State Unit → Prop} {DL : S → U64 → Prop} + {Wr : Fl4SpecA64.State Unit → S → U64 → Prop} + +/-- **Idle, bound TCB blocked on a receive, with its write footprint** (module docstring). -/ +theorem send_signal_idle_bound_recvF (hH : SchedHypsA st N T SR) (hO : SchedOpsA st ar N T SR DL) + (hF : SchedFootNA st ar N T SR DL Wr) + (hOE : SchedOpsEA st T E R Bf SR) (hP : IPlacedA N T E R Bf) (s : S) (a : A) + (σ : Fl4SpecA64.State Unit) (hR : NtfnStateRel N T (L.mem s) σ) + (hRE : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) {n : U64} (hn : n ∈ N) + {x : Fl4SpecA64.Notification} (hk : σ.kheap (specPtr n) = some (.notification x)) (hidle : x.ntfnObj = .idleNtfn) + {t : Fl4SpecA64.ObjRef} {tcb : Fl4SpecA64.Tcb} (hb : x.ntfnBoundTcb = some t) (hkt : σ.kheap t = some (.tcb tcb)) + (hrb : Fl4SpecA64.receiveBlocked tcb.tcbState = true) + (hrep : RepOKA σ t) (heE : L.mem s (tBlockA (L.mem s (nBoundA n))) ∈ E) + (hz : ∀ e', Fl4SpecA64.epBlocked tcb.tcbState = some e' → + ∃ z, σ.kheap e' = some (.endpoint z) ∧ t ∈ z.epQueue) (badge : U64) + (hon : L.mem s (affA (L.mem s (nBoundA n))) = L.cpu s) (hdl : DL s n) + (hAct : Aligned (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION)) : + notification.send_signal st ar s a n badge ⦃ r => ¬ (Fl4SpecA64.sendSignal (specPtr n) badge.bv σ).2 ∧ + ∃ σ', ((), σ') ∈ (Fl4SpecA64.sendSignal (specPtr n) badge.bv σ).1 ∧ NtfnStateRel N T (L.mem r.1) σ' ∧ + SR r.1 σ' ∧ WritesIn st s r.1 (fun b => EWrite T E R Bf b ∨ NWrite N T b ∨ Wr σ s b ∨ + b = L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) ⦄ := by + have hx := hR.at_ntfn hn hk + obtain ⟨hb0, hbt⟩ := optRef_some (hx.bound ▸ hb) + subst hbt + have hiT := hx.bound_mem hb0 + have hy := hR.at_tcb hiT hkt + have hs3 : L.mem s (stA (L.mem s (nBoundA n))) = sched.TS_BLOCKED_ON_RECV := by + have h3 := hy.st + rw [tsVals.2.2.1] + apply UScalar.eq_of_val_eq + revert hrb h3 + cases tcb.tcbState with + | ext e => cases e <;> simp [tsCode, Fl4SpecA64.receiveBlocked] + | _ => simp [tsCode, Fl4SpecA64.receiveBlocked] + unfold notification.send_signal + step with obj.Ntfn.state.spec as ⟨ns, hns⟩ + have hIdle : ns = obj.NTFN_IDLE := by rw [hns]; exact stOf_idle hx.obj hidle + simp only [hIdle, ↓reduceIte] + step with obj.Ntfn.bound_tcb.spec as ⟨i, hi⟩ + step with obj.Tcb.is_null.spec as ⟨b, hNull⟩ + subst i + simp only [hNull, hb0, decide_false, Bool.false_eq_true, ↓reduceIte] + step with obj.Tcb.state.spec as ⟨ts, hts⟩ + simp only [hts, hs3, ↓reduceIte] + have hnr : ∀ r, tcb.tcbState ≠ .blockedOnReply r := by + intro r h; rw [h] at hrb; simp [Fl4SpecA64.receiveBlocked] at hrb + refine WP.spec_bind (WP.spec_and + (cancelIpc_of_rel hH hO.scheduleTcb hOE hP s a σ hR hRE hSR hiT hkt hrb hrep heE hz) + (cancel_ipc_footA hP s a σ hR hRE hiT hkt hnr (fun e' hb' => ⟨heE, hz e' hb'⟩) + (fun n' h => by rw [h] at hrb; cases hrb) hAct)) ?_ + rintro ⟨s1, a1⟩ ⟨⟨σ1, x1, hc, hR1, -, hSR1, hk1, hin, hrb1⟩, hwc⟩ + have hspec : Fl4SpecA64.sendSignal (specPtr n) badge.bv σ = specWake (specPtr (L.mem s (nBoundA n))) (specPtr n) badge.bv σ1 := by + unfold Fl4SpecA64.sendSignal + rw [nd_bind_one (getNotification_eq hk)] + simp only [hidle, hb] + rw [nd_bind_one (getThreadState_eq hkt)] + simp only [hrb, ↓reduceIte] + rw [nd_bind_one hc] + rfl + rw [hspec] + have hq1 : ∀ n' ∈ N, ∀ z, σ1.kheap (specPtr n') = some (.notification z) → + specPtr (L.mem s (nBoundA n)) ∉ z.ntfnObj.ntfnQueue := by + intro n' hn' z hz hm + obtain ⟨-, h6, -⟩ := (hR1.at_ntfn hn' hz).obj.blocked hm + have := (hR1.at_tcb hiT hk1).st + rw [hin, h6] at this + cases this + obtain ⟨e, ro, d, hst⟩ : ∃ e ro d, tcb.tcbState = .blockedOnReceive e ro d := by + generalize tcb.tcbState = ts at hrb ⊢ + cases ts <;> first | exact ⟨_, _, _, rfl⟩ | simp [Fl4SpecA64.receiveBlocked] at hrb + obtain ⟨z, hkz, hq⟩ := hz e (by rw [hst]; rfl) + obtain ⟨σ', x', hc', -, -, -, -, -, hsc'⟩ := cancelIpc_recv_out hkt hst hkz hq hrep + have he : σ' = σ1 := congrArg Prod.snd (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst (hc'.symm.trans hc))) + have hssz : SSzA σ σ1 := fun q y n' h => ⟨y, hsc' q y n' (he ▸ h)⟩ + refine WP.spec_mono (wake_refinesF hH hO hF hP.n s1 a1 σ1 hR1 hSR1 hiT hn hk1 hq1 badge (Or.inr hrb1) + (by rw [hin]; rfl) (by rw [hin]; exact fun h => by cases h) + ((hO.coreKeep _ _ _ _ hSR hSR1).onCore hiT hon) hR hSR hdl (cancelIpc_recv_nscK hkt hrb hc)) ?_ + rintro r ⟨hnf, σ2, hm, h1, h2, h3⟩ + refine ⟨hnf, σ2, hm, h1, h2, ?_⟩ + refine (WritesIn.trans hwc h3).mono fun b hb => ?_ + rcases hb with (hb | hb | hb) | hb | hb + · exact Or.inl hb + · exact Or.inr (Or.inl hb) + · exact Or.inr (Or.inr (Or.inr hb)) + · exact Or.inr (Or.inl hb) + · exact Or.inr (Or.inr (Or.inl (hF.back hssz hwc.1 hb))) + +end + +end + +end Fl4ProofsA64.P3.Notif diff --git a/proofs/Fl4ProofsA64/P3/Stage3OffAsEA.lean b/proofs/Fl4ProofsA64/P3/Stage3OffAsEA.lean new file mode 100644 index 000000000..181006330 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3OffAsEA.lean @@ -0,0 +1,155 @@ +/- +P3 (aarch64, A64-P3 Stage-3 T3 item G3, part 3): **the VSpace-placement rows of the adapter** (G3 freeze table, D +rows `OffAsE` / `hK`, the unmapped page table's `ptr ∉ X` / `ptr ∉ Y`, `PoolIdx`, `mcp`). + +* `JStage3A.ptHAll`: every page of the Stage-2 set (`S2PgE X T2`: Stage-2 tables, both pages of each root) and of the + Stage-1 set (`VsPtA Pp S1 Y`: pools, tables, the windows' private level-1 pages) lies inside an `other` heap object + (`SetsInHA.pages` / `.roots`). +* **`JStage3A.offAsE`** (`OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 ksb`, by `ptHA_not_asW` over `ptHAll`, `HeapKsOff`) and + `JStage3A.ksbAligned` (`hK`). +* **`JStage3A.pt_notRootA`**: a page table object (`H ptr = other 12`) is neither page of a Stage-2 root nor a high + window (`ptr ∉ S2PgE X ∅`'s root part, `ptr ∉ Y`): the roots sit in objects of at least 8 KiB (`SetsInHA.roots`), + and `HeapOK.disj`. The `ptr ∉ T2` half stays with the "unmap shrinks the ghost" backlog item (T3 note, 21:42Z). +* `S1aPoolCaps.poolIdx` (R2's `poolCaps` gives A64 P1's `PoolIdx`), `JStage3A.mcpBound` (`McpRelA`: MCP words are + 8-bit priorities). + +Named premises: none. New module; no existing statement changed. +-/ +import Fl4ProofsA64.P3.Stage3SysPlaceA +import Fl4ProofsA64.P3.ExtVsUnmapA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- A page `r` or `r + 4096` of an 8 KiB root inside an `other` object lies inside that object. -/ +theorem root_pageA {H : Heap} (hH : HeapOK H) {r q : U64} {z : Nat} (hq : H q = some (.other z)) + (h1 : q.val ≤ r.val) (h2 : r.val + 8192 ≤ q.val + 2 ^ z) {p : U64} (hp : p = r ∨ p = wa r 4096#u64) : + q.val ≤ p.val ∧ p.val + 4096 ≤ q.val + 2 ^ z ∧ p.val ≤ r.val + 4096 ∧ r.val ≤ p.val := by + have hf := hH.fits q _ hq + simp only [HKind.bits, Nat.reducePow] at hf + rcases hp with rfl | rfl + · omega + · have e4 : (4096#u64 : U64).val = 4096 := rfl + have e : (wa r 4096#u64).val = r.val + 4096 := by + rw [wa_val, e4]; exact Nat.mod_eq_of_lt (by omega) + rw [e]; omega + +/-- **A 4 KiB `other` object is no page of an 8 KiB root** (module docstring). -/ +theorem notRoot_of12A {H : Heap} (hH : HeapOK H) {ptr : U64} (hp : H ptr = some (.other 12)) {r q : U64} {z : Nat} + (hq : H q = some (.other z)) (h1 : q.val ≤ r.val) (h2 : r.val + 8192 ≤ q.val + 2 ^ z) + (hr : ptr = r ∨ ptr = wa r 4096#u64) : False := by + obtain ⟨g1, g2, g3, g4⟩ := root_pageA hH hq h1 h2 hr + by_cases e : q = ptr + · subst e + rw [hp] at hq + have hz : z = 12 := by + have := Option.some.inj hq + injection this with h + exact h.symm + subst hz + simp only [Nat.reducePow] at h2 + omega + · have hd := hH.disj q _ ptr _ hq hp e + simp only [HKind.bits, Nat.reducePow] at hd + omega + +end + +section rows +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- Every Stage-2 and Stage-1 page of the relation lies inside an `other` heap object. -/ +theorem JStage3A.ptHAll (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) : + PtHA H (S2PgE Z.X Z.T2 ∪ VsPtA Z.Pp Z.S1 Z.Y) := by + have P := J.setsInH + have hH := J.sys.ent.heap + have rt : ∀ r ∈ Z.X ∪ Z.Y, ∀ q, (q = r ∨ q = wa r 4096#u64) → + ∃ q' z, H q' = some (.other z) ∧ q'.val ≤ q.val ∧ q.val + 4096 ≤ q'.val + 2 ^ z := by + intro r hr q hq + obtain ⟨q', z, hq', h1, h2⟩ := P.roots r hr + obtain ⟨g1, g2, -, -⟩ := root_pageA hH hq' h1 h2 hq + exact ⟨q', z, hq', g1, g2⟩ + rintro q (hq | hq) + · rcases hq with h | ⟨r, hr, e⟩ + · exact P.pages q (Or.inl (Or.inr h)) + · exact rt r (Or.inl hr) q e + · rcases hq with h | h | ⟨y, hy, e⟩ + · exact P.pages q (Or.inl (Or.inl (Or.inl h))) + · exact P.pages q (Or.inl (Or.inl (Or.inr h))) + · exact rt y (Or.inr hy) q (Or.inr e) + +/-- **`OffAsE`**: no relation page word is an ASID-table word (module docstring). -/ +theorem JStage3A.offAsE (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) : + OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s) := + fun _ _ hj hq => ptHA_not_asW J.sys.ent.heap J.ko J.sys.ent.ksw J.ptHAll hq hj + +/-- `hK`: the state block is word aligned. -/ +theorem JStage3A.ksbAligned (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) : + Aligned (L.ksb s) := + J.toJEntA.vsLeafA.1 + +/-- **A page-table object is no Stage-2 root page and no high window** (module docstring). -/ +theorem JStage3A.pt_notRootA (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {ptr : U64} (hp : H ptr = some (.other 12)) : + (∀ r ∈ Z.X, ptr ≠ r ∧ ptr ≠ wa r 4096#u64) ∧ ptr ∉ Z.Y := by + have hH := J.sys.ent.heap + have no : ∀ r ∈ Z.X ∪ Z.Y, ptr = r ∨ ptr = wa r 4096#u64 → False := fun r hr e => by + obtain ⟨q, z, hq, h1, h2⟩ := J.setsInH.roots r hr + exact notRoot_of12A hH hp hq h1 h2 e + exact ⟨fun r hr => ⟨fun e => no r (Or.inl hr) (Or.inl e), fun e => no r (Or.inl hr) (Or.inr e)⟩, + fun hy => no ptr (Or.inr hy) (Or.inl rfl)⟩ + +/-- `ptr ∉ S2PgE X ∅`: the root half of the unmapped page table's `ptr ∉ S2PgE X T2`. -/ +theorem JStage3A.pt_notS2RootA (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {ptr : U64} (hp : H ptr = some (.other 12)) : ptr ∉ S2PgE Z.X ∅ := by + rintro (h | ⟨r, hr, e⟩) + · exact h + · rcases e with e | e + · exact (J.pt_notRootA hp).1 r hr |>.1 e + · exact (J.pt_notRootA hp).1 r hr |>.2 e + +/-- `mcp`: every TCB's MCP word is an 8-bit priority (`McpRelA`). -/ +theorem JStage3A.mcpBound (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) : + ∀ y ∈ H.tcbs, (L.mem s (wa y 3440#u64)).val < 256 := by + intro y hy + rw [J.tcbs] at hy + obtain ⟨x, -, hx, -⟩ := J.mcp y hy + have e : (L.mem s (wa y 3440#u64)).val = x.tcbMcpriority.toNat := hx + rw [e] + have h := x.tcbMcpriority.isLt + first | omega | (simp at h; omega) | (norm_num at h; omega) + +end rows + +section pool +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **R2's pool caps give A64 P1's `PoolIdx`.** -/ +theorem S1aPoolCaps.poolIdxA {H : Heap} {OS Pt Pp : Set U64} {s : S} (h : S1aPoolCaps st H OS Pt Pp s) : + PoolIdx st OS s := by + intro y hy p base hc + obtain ⟨i, hi, hb, hp0, ht⟩ := h.cap y hy p base hc + have hsh : base.val >>> 9 = i.val := by + rw [hb, Nat.shiftRight_eq_div_pow]; omega + refine ⟨by rw [hsh]; exact hi, fun j hj => ?_⟩ + rw [vspace.asid_table.eq, WP.spec_ok] + intro e hne + rw [hsh] + exact congrArg UScalar.val (h.index j i p hj hi hne e ht) + +end pool + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3P1StepA.lean b/proofs/Fl4ProofsA64/P3/Stage3P1StepA.lean new file mode 100644 index 000000000..116ec8d46 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3P1StepA.lean @@ -0,0 +1,72 @@ +/- +P3 (aarch64, A64-P3 Lift 3b labels): **the P1 facts across a scheduler-word step from a record state**. + +The Stage-3 step lemmas (`setMcp_stepA`, `prio*_stepA`, `capInsertTcbSlotA`) take `SysL`, `SymL`, `UserOK`, `J7A` and +`BXI` at their post state. At the final decode state they come from `JSyscallA.decodeFinA`; this module gives them at +an intermediate state reached from a `JStage3A` state by a step that writes only `SchedStepNWA` words and is a P1 +`DMD` step. + +* **`JStage3A.p1StepA`**: from `J` at `s`, `WritesIn s s' SchedStepNWA`, a `DMD p s s'` step whose `p` is named by an + `OS` cap, and `BlkOK` at `s'`: `SysL` and `SymL` (`SysL.dfMDO`), `UserOK` (`UserOK.ksb`), `BXI`, and `J7A` at any + spec state (`ArmQIAt.ofWK`, `ArmVsK.ofW`, `S1aStepFix.ofKeep`, `J7A.of_stepFixedA`, as `J7A.of_rpPostA`). + +Named premises: [P] `ArchTotal`, `ArchPlat`. New module. +-/ +import Fl4ProofsA64.P3.Stage3FaultSetA +import Fl4ProofsA64.P1.MdbSspTail +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {s s' : S} {a : A} {σ : State Unit} {m : McState} + +/-- **The P1 facts across a scheduler-word `DMD` step** (module docstring). -/ +theorem JStage3A.p1StepA (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (w : WritesIn st s s' (SchedStepNWA (st := st) N E Rp Z m σ s)) + {p : U64} (d : DMD st H Z.Cs p s s') (hp : ∀ k, H p = some k → ∃ x ∈ OS, NamesA (capA st s x) p k) + {Ac' : Set U64} (hb : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac' s') : + SysL st H Z.Cs OS ia ie s' ∧ SymL st H Z.Cs s' ∧ Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s' ∧ + BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac' s' ∧ + ∀ σ' m', J7A st (J7R2A st ar hPl.ipa) Z H OS s' σ' m' := by + obtain ⟨Pt, Ub, Pp7, Tb7, Hr, Te, Kt, Lv7, Re, bits, KLv, W⟩ := J7A.unp J.j7 + have I := J.delIn hA hPl W + have kb : L.ksb s' = L.ksb s := w.1 + have SY : SysL st H Z.Cs OS ia ie s' ∧ SymL st H Z.Cs s' := + SysL.dfMDO I.del.heap I.del.os (BXI.blk J.sys.ent.bxi) I.del.sca hp J.sys.sysl d + have osk : OSKeep st OS s s' := J.osKeep_stepNA w + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.sys.ent.ksw + have notAs : ∀ b, SchedStepNWA (st := st) N E Rp Z m σ s b → ¬ AsW (L.ksb s) b := by + rintro b hb ⟨o, o1, o2, -, rfl⟩ + have ev : (wa (L.ksb s) o).val = (L.ksb s).val + o.val := by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact J.asid_offStepNA (by omega) (by omega) hb + have hvk : ArmVsK st H OS Z.V s s' := ArmVsK.ofW w fun Ub' Pt' hvr _ b hb hv => by + rcases hv with hv | hv + · exact J.vsr_offStepNA hvr (Or.inl hv) (notAs b hb) hb + · exact notAs b hb hv + obtain ⟨arm, tk⟩ := ArmQIAt.ofWK W.vr w notAs osk SY hb hvk.toVs + have Fx := S1aStepFix.ofKeep ⟨arm, tk.toDel, hvk, osk⟩ W.vr W.fu W.vcc W.col W.df W.r2 W.sep W.hiK + have pn : PoolNamed st OS s' := PoolNamed.ofTab kb tk.2 osk W.pn + have hsp : ∀ p, SchedPageA st Z OS s' p → p ∈ Pt := by + obtain ⟨_, -, -, -, ⟨_, _, _, _, _, _, _, R'⟩, -⟩ := id Fx + exact schedPage_ptA R' W.sp + refine ⟨SY.1, SY.2, J.sys.ent.user.ksb kb, BlkOK.bxi hb, fun σ' m' => ?_⟩ + exact J7A.of_stepFixedA Fx W.vr kb W.al W.vu pn hsp + (fun b hb o ho => by rw [kb]; exact W.ks b hb o ho) (J7R2K.of_stepFixedA (σ' := σ') (m := m') Fx W.ktK) + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3PartialA.lean b/proofs/Fl4ProofsA64/P3/Stage3PartialA.lean new file mode 100644 index 000000000..9919341e2 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3PartialA.lean @@ -0,0 +1,121 @@ +/- +P3 (aarch64, A64-P3 Stage-3 integration; queue item 1): **the partial-invocation successor with its footprint**. +`afterInvocationPreempt_stepA` is a sibling of `afterInvocationPreempt_refinesA` (`P3/OuterEntryA`, statement unchanged; +generic in `Rel` / `Final`) that also exports `PartFrA`: `WritesIn s s' (SchedStepNWA N E Rp Z m σ s)` and the spec frame +`SchedFrameA σ τ`, so `JStage3A.of_schedWrN` applies at the successor. + +* `charge_budget … false`: the premise carries `MFrA` (`charge_budget_falseFrA`, `P3/ScEntryBudgetA`); the spec frame is + `SPresInst (fl4ChargeBudget c false)` (`P3/Stage3PartialSpecA`). +* `clear`: the node CONSUMED word (`SchedSRB.clearConsumedA`, `SchedSRN.node_sks`) and a record update. +* `check_budget` (runnable branch) carries the timeout-send frame `SchedStepNWA` as a premise (ruling 00:33Z C), and the IRQ + arm (`take_pending_irq` + `check_interrupt`, which signals notifications) is an explicit frame premise. + +Edits to `P3/ScEntryEndA` / `P3/ScEntryBudgetA`: new theorems `end_timeslice_noSendA`, `entryRescheduleFrA`, +`charge_budget_falseFrA` (statements of the ratified theorems unchanged). +-/ +import Fl4ProofsA64.P3.OuterEntryA +import Fl4ProofsA64.P3.ScEntryBudgetA +import Fl4ProofsA64.P3.Stage3PartialSpecA +import Fl4ProofsA64.P3.Stage3SchedWrNA +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3.Sched + +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.OuterEntry + +noncomputable section + +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +/-- The footprint bundle of a (composite) scheduler / IPC-object step: concrete writes and the spec frame. -/ +structure PartFrA (N E Rp : Set U64) (Z : XSetsA) (m : McState) (s s' : S) (σ τ : State Unit) : Prop where + wr : WritesIn st s s' (SchedStepNWA (st := st) N E Rp Z m σ s) + fr : SchedFrameA σ τ + +theorem PartFrA.refl (N E Rp : Set U64) (Z : XSetsA) (m : McState) (s : S) (σ : State Unit) : + PartFrA (st := st) N E Rp Z m s s σ σ := ⟨WritesIn.refl _ _, PreO.refl σ⟩ + +theorem schedStepN_backA {N E Rp : Set U64} {Z : XSetsA} {m : McState} {s s1 : S} {σ σ1 : State Unit} + (hz : SSzA σ σ1) (hk : L.ksb s1 = L.ksb s) {b : U64} + (h : SchedStepNWA (st := st) N E Rp Z m σ1 s1 b) : SchedStepNWA (st := st) N E Rp Z m σ s b := by + rcases h with (h | h) | h + · exact Or.inl (Or.inl (mwr_back rfl rfl hz hk h)) + · exact Or.inl (Or.inr h) + · exact Or.inr h + +theorem PartFrA.trans {N E Rp : Set U64} {Z : XSetsA} {m : McState} {s s1 s2 : S} {σ σ1 σ2 : State Unit} + (F1 : PartFrA (st := st) N E Rp Z m s s1 σ σ1) (F2 : PartFrA (st := st) N E Rp Z m s1 s2 σ1 σ2) : + PartFrA (st := st) N E Rp Z m s s2 σ σ2 := + ⟨(F1.wr.trans F2.wr).mono fun _ hb => hb.elim id (schedStepN_backA F1.fr.2.1 F1.wr.1), + PreO.trans F1.fr F2.fr⟩ + +/-- A scheduler-frame step (`MFrA`) with its spec frame. -/ +theorem PartFrA.of_mfr {N E Rp : Set U64} {Z : XSetsA} {m : McState} {s s' : S} {σ τ : State Unit} + (F : MFrA (st := st) Z m s s' σ τ) (G : SchedFrameA σ τ) : PartFrA (st := st) N E Rp Z m s s' σ τ := + ⟨F.wr.mono fun _ h => Or.inl (Or.inl h), G⟩ + +/-- Clearing the CONSUMED word as `afterInvocationPreempt`'s `clear` step does. -/ +theorem clearConsumedWriteA (s : S) (a : A) : + (do let cpu ← st.cur_cpu s + let s' ← obj.node_set st s cpu layout.node.CONSUMED 0#u64 + ok (s', a)) ⦃ r => r.1 = L.wr s (L.nodeA s layout.node.CONSUMED) 0#u64 ∧ r.2 = a ⦄ := by + rw [L.cur_cpu_eq, bind_ok, eq_of_spec_fd (obj.node_set.spec s _ _ _), bind_ok] + exact (WP.spec_ok _).2 ⟨rfl, rfl⟩ + +/-- **`afterInvocationPreempt_refinesA` with its footprint** (module docstring). -/ +theorem afterInvocationPreempt_stepA {N E Rp : Set U64} {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {a : A} {σ : State Unit} {pending : SMonad Unit Unit} {Rel Final : S → A → State Unit → Prop} + {t u : U64} (H : SchedSRB st Z m κ s σ u) (thread : sched.cur_thread st s = ok t) + (eligible : sched.is_schedulable st s t = ok (schedulable σ.curThread σ)) + (check : schedulable σ.curThread σ = true → sched.check_budget st ar s a ⦃ r => + ∃ τ, (r.1, τ) ∈ (fl4CheckBudget σ).1 ∧ Rel r.2.1 r.2.2 τ ∧ + PartFrA (st := st) N E Rp Z m s r.2.1 σ τ ⦄) + (readSC : schedulable σ.curThread σ = false → ∃ (sc : U64) (active : Bool), + sched.cur_sc st s = ok sc ∧ sporadic.sc_active st s sc = ok active ∧ + getScActive σ.curSc σ = ({(active, σ)}, False)) + (consumed : sched.consumed st s = ok ⟨σ.consumedTime⟩) + (charge : schedulable σ.curThread σ = false → + getScActive σ.curSc σ = ({(true, σ)}, False) → + sched.charge_budget st ar s a ⟨σ.consumedTime⟩ false ⦃ r => + StepPostA (fun s' a' τ => Rel s' a' τ ∧ MFrA (st := st) Z m s s' σ τ) + (fl4ChargeBudget σ.consumedTime false) σ r ⦄) + (clear : schedulable σ.curThread σ = false → + getScActive σ.curSc σ = ({(false, σ)}, False) → + (do let cpu ← st.cur_cpu s + let s' ← obj.node_set st s cpu layout.node.CONSUMED 0#u64 + ok (s', a)) ⦃ r => Rel r.1 r.2 {σ with consumedTime := 0} ⦄) + (take : ∀ r τ, syscall.mcs_preemption_point st ar s a = ok r → Rel r.1 r.2 τ → + PartFrA (st := st) N E Rp Z m s r.1 σ τ → + (do let (irq, a') ← ar.take_pending_irq r.2 + syscall.check_interrupt st ar r.1 a' irq) ⦃ out => + ∃ υ, ((), υ) ∈ (pending τ).1 ∧ Final out.1 out.2 υ ∧ PartFrA (st := st) N E Rp Z m s out.1 σ υ ⦄) : + syscall.after_invocation st ar s a (.Err .Preempted) ⦃ r => + r.1 = false ∧ ∃ υ, ((), υ) ∈ (fl4PreemptionPath pending σ).1 ∧ Final r.2.1 r.2.2 υ ∧ + PartFrA (st := st) N E Rp Z m s r.2.1 σ υ ⦄ := by + refine WP.spec_mono (afterInvocationPreempt_refinesA (pending := pending) + (Rel := fun s' a' τ => Rel s' a' τ ∧ PartFrA (st := st) N E Rp Z m s s' σ τ) + (Final := fun s' a' τ => Final s' a' τ ∧ PartFrA (st := st) N E Rp Z m s s' σ τ) + thread eligible ?_ readSC consumed ?_ ?_ ?_) ?_ + · intro hs + exact WP.spec_mono (check hs) fun r ⟨τ, mem, rel, F⟩ => ⟨τ, mem, rel, F⟩ + · intro hs ha + refine WP.spec_mono (charge hs ha) ?_ + rintro r ⟨τ, mem, rel, F⟩ + exact ⟨τ, mem, rel, PartFrA.of_mfr F + ((inferInstance : SPresInst (fl4ChargeBudget (Z := Unit) σ.consumedTime false)).pres σ _ mem)⟩ + · intro hs ha + refine WP.spec_mono (spec_andA (clear hs ha) (clearConsumedWriteA (st := st) s a)) ?_ + rintro r ⟨rel, hr, -⟩ + refine ⟨rel, ?_, SchedFrameA.of_eq rfl rfl rfl rfl rfl rfl rfl rfl⟩ + rw [hr] + refine H.clearConsumedA.2.mono fun b hb => Or.inl (Or.inl (Or.inr (Or.inl ?_))) + rw [hb] + exact H.toSchedSRN.node_sks H.sch.cpuCs (by unfold layout.node.CONSUMED; decide) + · intro r τ mem ⟨rel, F⟩ + exact WP.spec_mono (take r τ mem rel F) fun out ⟨υ, h1, h2, h3⟩ => ⟨υ, h1, h2, h3⟩ + · rintro r ⟨hr, υ, mem, fin, F⟩ + exact ⟨hr, υ, mem, fin, F⟩ + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3PartialSpecA.lean b/proofs/Fl4ProofsA64/P3/Stage3PartialSpecA.lean new file mode 100644 index 000000000..208fbcbb7 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3PartialSpecA.lean @@ -0,0 +1,33 @@ +/- +P3 (aarch64, A64-P3 Stage-3 integration; queue item 1): **spec frame of the partial-invocation successor pieces**. +`fl4ChargeBudget c false` (the timeout-disabled charge of `mcs_preemption_point`) keeps `SchedFrameA` +(`P3/SpecFrameCalcA`); the `consumedTime := 0` reset of the inactive branch is a record update off the frame fields. +Pure spec statements. No premise; new module. +-/ +import Fl4ProofsA64.P3.SpecFrameSchedA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec + +noncomputable section + +instance (p : ObjRef) : SPresInst (refillResetRr (Z := Unit) p) := ⟨by unfold refillResetRr; spres⟩ + +instance (c : Ticks) (p : ObjRef) : SPresInst (fl4ChargeHead (Z := Unit) p c) := ⟨by unfold fl4ChargeHead; spres⟩ + +theorem fl4EndTimeslice_false_presA : SPresA (fl4EndTimeslice (Z := Unit) false) := by + unfold fl4EndTimeslice + simp only [Bool.false_eq_true, false_and, ↓reduceIte] + spres + +instance (c : Ticks) : SPresInst (fl4ChargeBudget (Z := Unit) c false) := + ⟨by + unfold fl4ChargeBudget + have := fl4EndTimeslice_false_presA + haveI : SPresInst (fl4EndTimeslice (Z := Unit) false) := ⟨this⟩ + spres⟩ + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3PinCnA.lean b/proofs/Fl4ProofsA64/P3/Stage3PinCnA.lean new file mode 100644 index 000000000..6d830ef1f --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3PinCnA.lean @@ -0,0 +1,537 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Plan B): **the CNode scope's pins** (`DecPinA`) at the real decode. + +* `cnCopy_pinA`: Copy / Mint / Move / Mutate (`cnodeCopy`, `cnodeMove`). The new cap `anc` is the real decode's + (`CapRelA nc anc`: for a mapped page-table source it is the source's representation, not `absCapA nc`); the + scope's provider instantiates the constructor with it. +* `cnRotate_pinA`: Rotate, both the three-slot form and the swap (`cnodeRotate`, `cnodeRotateSw`). + +Their hypotheses are the constructors' FL4 decode-site facts and the front end's values (`FrontJA`). + +Named premises: [J] `JSyscallA`. New module. +-/ +import Fl4ProofsA64.P3.Stage3PinFrontA +import Fl4ProofsA64.P3.CnodeCopyA +import Fl4ProofsA64.P3.CnodeRotA +import Fl4ProofsA64.P3.VsTopPtA +import Fl4ProofsA64.P3.CnodeCancelA +import Batteries.Tactic.OpenPrivate +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +/-! ### `NoHiPtV` of the pinned caps (entry item 13): a decode never makes a mapped page-table address -/ + +theorem noHiPtV_maskA (R : CapRights) {c : Cap} (h : NoHiPtV c) : NoHiPtV (maskCap R c) := by + intro p a v e + cases c with + | archObjectCap ac => + cases ac <;> simp [maskCap, capRightsUpdate, acapRightsUpdate] at e + exact h p a v (by simp [e]) + | _ => simp [maskCap, capRightsUpdate] at e + +theorem noHiPtV_updateA (b : Bool) (w : Data) {c : Cap} (h : NoHiPtV c) : NoHiPtV (updateCapData b w c) := by + intro p a v e + cases c with + | archObjectCap ac => + simp [updateCapData, isEpCap, isNtfnCap, isCnodeCap, isArchCap, archUpdateCapData, + Cap.theArchCap] at e + split at e + · split at e <;> cases e + · exact h p a v e + | _ => + simp [updateCapData, isEpCap, isNtfnCap, isCnodeCap, isArchCap, badgeUpdate] at e + all_goals (repeat' split at e) <;> cases e + +/-- An `.ok` outcome of a bind passes through an `.ok` outcome of its first part. -/ +theorem mem_bindE_ok_invPinA {α β : Type} {m : Fl4SpecA64.SEMonad Unit α} {f : α → Fl4SpecA64.SEMonad Unit β} + {σ σ' : State Unit} {b : β} (h : (.ok b, σ') ∈ ((m >>= f).run σ).1) : + ∃ a τ, (.ok a, τ) ∈ (m.run σ).1 ∧ (.ok b, σ') ∈ ((f a).run τ).1 := by + rw [ExceptT.run_bind] at h + obtain ⟨⟨r, τ⟩, h1, h2⟩ := mem_bindA.1 h + cases r with + | error e => cases (Set.mem_singleton_iff.mp h2) + | ok a => exact ⟨a, τ, h1, h2⟩ + +theorem noHiPtV_deriveA {sl : CslotPtr} {c r : Cap} {σ τ : State Unit} + (hr : (.ok r, τ) ∈ ((deriveCap (Z := Unit) sl c).run σ).1) (h : NoHiPtV c) : NoHiPtV r := by + intro p a v e + subst e + cases c with + | archObjectCap ac => + cases ac with + | pageTableCap p' t m => + cases m with + | none => cases (Set.mem_singleton_iff.mp hr) + | some x => + have := Set.mem_singleton_iff.mp hr + simp only [Prod.mk.injEq, Except.ok.injEq] at this + exact h p a v this.1.symm + | ext x => + cases x with + | stage2FrameCap _ _ _ _ _ => cases (Set.mem_singleton_iff.mp hr) + | stage2VSpaceCap _ m => cases m <;> cases (Set.mem_singleton_iff.mp hr) + | highVSpaceCap _ m => cases m <;> cases (Set.mem_singleton_iff.mp hr) + | _ => cases (Set.mem_singleton_iff.mp hr) + | untypedCap _ _ _ _ => + obtain ⟨_, _, -, h2⟩ := mem_bindE_ok_invPinA hr + cases (Set.mem_singleton_iff.mp h2) + | _ => cases (Set.mem_singleton_iff.mp hr) + +/-- A Copy / Mint / Move / Mutate tail's new cap keeps `NoHiPtV`. -/ +theorem copyRestA_noHiPtA {slbl : Data} {args : List Data} {ssl dsl : CslotPtr} {σ : State Unit} + {srcA anc : Cap} {mv : Bool} + (h : (copyRestA slbl args ssl dsl srcA).run σ = ({(finishResA mv ssl dsl (.ok anc), σ)}, False)) + (hn : anc ≠ .nullCap) (hs : NoHiPtV srcA) : NoHiPtV anc := by + have e : finishResA mv ssl dsl (.ok anc) = + .ok ((if mv then CnodeInvocation.moveCall else .insertCall) anc ssl dsl) := by + simp [finishResA, hn] + rw [e] at h + have hm : (Except.ok ((if mv then CnodeInvocation.moveCall else .insertCall) anc ssl dsl), σ) ∈ + ((copyRestA slbl args ssl dsl srcA).run σ).1 := by rw [h]; exact rfl + unfold copyRestA at hm + obtain ⟨⟨rights, capData, isMove⟩, τ1, -, h1⟩ := mem_bindE_ok_invPinA hm + dsimp only at h1 + obtain ⟨src', τ2, h2, h3⟩ := mem_bindE_ok_invPinA h1 + have es : src' = maskCap rights srcA := by + have := Set.mem_singleton_iff.mp h2 + simp only [Prod.mk.injEq, Except.ok.injEq] at this + exact this.1 + subst es + obtain ⟨nc, τ3, h4, h5⟩ := mem_bindE_ok_invPinA h3 + obtain ⟨_, τ4, -, h6⟩ := mem_bindE_ok_invPinA h5 + have enc : anc = nc := by + have := Set.mem_singleton_iff.mp h6 + simp only [Prod.mk.injEq, Except.ok.injEq] at this + have g := this.1 + cases mv <;> cases isMove <;> simp at g <;> first | exact g | exact g.1 | exact g.symm + subst enc + have hmk := noHiPtV_maskA rights hs + cases capData with + | none => + cases isMove with + | true => + have := Set.mem_singleton_iff.mp h4 + simp only [Prod.mk.injEq, Except.ok.injEq] at this + rw [this.1]; exact hmk + | false => exact noHiPtV_deriveA h4 hmk + | some w => + cases isMove with + | true => + have := Set.mem_singleton_iff.mp h4 + simp only [Prod.mk.injEq, Except.ok.injEq] at this + rw [this.1]; exact noHiPtV_updateA _ _ hmk + | false => exact noHiPtV_deriveA h4 (noHiPtV_updateA _ _ hmk) + +/-- The spec cap read from a typed CNode slot keeps `NoHiPtV` (`SIdleA.f6`). -/ +theorem noHiPtV_slotA {σ : State Unit} {m : McState} {n : Nat} (hi : SIdleA σ m n) {th : TypedHeapA} + {mem : U64 → U64} (hR : StateRelA th mem σ) {sl : CslotPtr} {p : U64} (h1 : sl.1 = specPtr p) + (hp : th p = some (.cnode sl.2.length)) {a : Cap} (hg : getCap (Z := Unit) sl σ = ({(a, σ)}, False)) : + NoHiPtV a := by + obtain ⟨cs, hk, hwf, hs⟩ := hR.cnode p _ hp + obtain ⟨a0, ha, -⟩ := hs sl.2 rfl + have hg' : getCap (Z := Unit) sl σ = ({(a0, σ)}, False) := by + have : sl = (specPtr p, sl.2) := by rw [← h1] + rw [this]; exact getCap_cnodeA hk hwf ha + have e := congrArg Prod.fst (hg.symm.trans hg') + simp only [Set.singleton_eq_singleton_iff, Prod.mk.injEq, and_true] at e + refine (hi.f6 sl).2.2 a ?_ + unfold specCapAtA + rw [h1, hk] + simp [hwf, ha, e] + +/-- An outcome of an arch decode is one of its `liftME`. -/ +theorem mem_liftME_okPinA {α β : Type} {f : α → β} {m : Fl4SpecA64.SEMonad Unit α} {σ : State Unit} {x : α} + (h : (.ok x, σ) ∈ (m.run σ).1) : (.ok (f x), σ) ∈ ((Fl4Spec.NonDetE.liftME f m).run σ).1 := by + unfold Fl4Spec.NonDetE.liftME + rw [ExceptT.run_bind] + exact mem_bindA.2 ⟨(.ok x, σ), h, Set.mem_singleton _⟩ + +/-- The pin from the spec decode at the front end's values. -/ +theorem DecPinA.of_frontA {call : Bool} {th : TypedHeapA} {σ : State Unit} {slot : U64} {mi : MessageInfo} + {sl : CslotPtr} {c : Cap} {excaps : List (Cap × CslotPtr)} {buffer : Option ObjRef} {args : List Data} + (F : FrontPinA th σ slot mi sl c excaps buffer args) {oper : Fl4Invocation} + (h : ∀ cptr : CapRef, (fl4DecodeInvocation (Z := Unit) call false mi.miLabel args cptr sl c excaps buffer).run σ = + ({(.ok oper, σ)}, False)) : DecPinA call th σ slot oper := + ⟨_, _, _, _, _, _, F, fun cptr => by rw [h cptr]; exact rfl⟩ + +/-- The spec's invocation label of a CNode operation's FL4 label. -/ +theorem cnLabel_specA {lbl : U64} {g : GenInvocationLabels} {v : Nat} (hv : lbl.val = v) + (e : labNA v = .spec (.genInvocationLabel g)) : + genInvocationType (specLabelWord (fl4LabelOf lbl.bv)) = g := by + rw [fl4LabelOf_bvA, hv, e] + have e' : invocationType (specLabelWord (.spec (.genInvocationLabel g))) = .genInvocationLabel g := + invocationType_fromEnumA _ + unfold genInvocationType + rw [e'] + +open private copyRestA_pt_ok from Fl4ProofsA64.P3.CnodeCopyA in +/-- **A successful Copy / Mint / Move / Mutate tail at any representation of the source cap** (`copied_relA` is stated +at `absCapA` of the source). -/ +theorem copyRest_okA {slbl : Data} {args : List Data} {ssl dsl : CslotPtr} {σ : State Unit} {src nc : capability.Cap} + {srcA : Cap} {mv : Bool} (hgt : IsCopyLabelA (genInvocationType slbl)) (h : CapEqA src srcA) + (hw : CapWfA nc) + (hR : (copyRestA slbl args ssl dsl (absCapA src)).run σ = ({(finishResA mv ssl dsl (.ok (absCapA nc)), σ)}, False)) : + ∃ anc, CapRelA nc anc ∧ + (copyRestA slbl args ssl dsl srcA).run σ = ({(finishResA mv ssl dsl (.ok anc), σ)}, False) := by + have same : srcA = absCapA src → ∃ anc, CapRelA nc anc ∧ + (copyRestA slbl args ssl dsl srcA).run σ = ({(finishResA mv ssl dsl (.ok anc), σ)}, False) := fun e => + ⟨_, CapRelA.of_abs hw rfl, e ▸ hR⟩ + cases src with + | PageTable p m asid va => + cases m + · exact same h.symm + · obtain ⟨asid', va', rfl, has, hva⟩ := CapEqA.pt_mapped_iff.mp h + rw [show absCapA (.PageTable p true asid va) = + Cap.archObjectCap (.pageTableCap (specPtr p) .normalPtT (some (asid', va.bv))) from by + simp [absCapA, mapDataA, has]] at hR + obtain ⟨habs, hok⟩ := copyRestA_pt_ok slbl args ssl dsl σ (specPtr p) asid' va.bv va' hgt mv (absCapA nc) hR + refine ⟨_, ⟨hw, ?_⟩, hok⟩ + cases nc with + | PageTable p' m' asid'' va'' => + cases m' + · simp [absCapA, mapDataA] at habs + · simp only [absCapA, mapDataA, ↓reduceIte, Cap.archObjectCap.injEq, ArchCap.pageTableCap.injEq, + Option.some.injEq, Prod.mk.injEq, true_and] at habs + exact ⟨habs.1, habs.2.1, by rw [habs.2.2]; exact hva⟩ + | Frame _ _ _ _ _ ept _ _ => cases ept <;> simp [absCapA, absFrameE] at habs + | _ => simp [absCapA] at habs + | _ => exact same (CapEqA.eq_abs h (fun _ _ _ e => by cases e)).symm + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s : S} {a : A} {σ : State Unit} {m : McState} + +set_option maxHeartbeats 4000000 in +/-- **CNode Copy / Mint / Move / Mutate's pin** (module docstring). -/ +theorem cnCopy_decodeEqA (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + {info : msginfo.MessageInfo} {buf slot : U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} {mi : MessageInfo} + {sl : CslotPtr} {c : Cap} {excaps : List (Cap × CslotPtr)} {buffer : Option ObjRef} {argsU : List U64} + (F : FrontJA st Z.th s σ info buf slot extra mi sl c excaps buffer argsU) + {lbl index depth x0 si sdp p r gs g : U64} {root nc : capability.Cap} {mv call : Bool} {ss sd : CslotPtr} + (hlbl : info.label = lbl) (h24 : 24 ≤ lbl.val ∧ lbl.val ≤ 27) + (hCs : AbsSlotA Z.th ss) (hCd : AbsSlotA Z.th sd) + (hsn : capA st s (cslotAddrA ss) ≠ .Null) (hdn : capA st s (cslotAddrA sd) = .Null) + (hn : nc ≠ .Null) (hslc : capA st s slot = .CNode p r gs g) + (hl4 : ¬ msgLenA info buf < 4#u64) + (hi : msg.syscall_arg st s (L.curW s) buf 0#u64 = ok index) + (hd0 : msg.syscall_arg st s (L.curW s) buf 1#u64 = ok depth) + (hres : cspace.lookup_target_slot st s (.CNode p r gs g) index depth = ok (.Ok (cslotAddrA sd))) + (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) (h0 : x0 ≠ 0#u64) + (h2 : msg.syscall_arg st s (L.curW s) buf 2#u64 = ok si) + (h3 : msg.syscall_arg st s (L.curW s) buf 3#u64 = ok sdp) + (hroot : obj.Slot.cap st x0 s = ok root) + (hsrc : cspace.lookup_source_slot st s root si sdp = ok (.Ok (cslotAddrA ss))) + (hcop : cnode.copied_cap st s lbl (msgLenA info buf) buf (cslotAddrA ss) (capA st s (cslotAddrA ss)) = + ok (.Ok (nc, mv))) : + ∃ anc, CapRelA nc anc ∧ NoHiPtV anc ∧ ∀ cptr : CapRef, + (fl4DecodeInvocation (Z := Unit) call false mi.miLabel (argsU.map UScalar.bv) cptr sl c excaps buffer).run σ = + ({(.ok (.invokeCNode (if mv then .moveCall anc ss sd else .insertCall anc ss sd)), σ)}, False) := by + have hR := J.j.1.st + have hV : SlotsValidA Z.th (L.mem s) := J.j.1.cv + have hP := J.j.1.place + -- the argument words + have h4 : 4 ≤ argsU.length := by + have := F.len + have : ¬ (msgLenA info buf).val < (4#u64 : U64).val := fun h => hl4 (by scalar_tac) + have : (4#u64 : U64).val = 4 := rfl + omega + obtain ⟨y0, y1, y2, y3, rest, rfl⟩ : ∃ y0 y1 y2 y3 rest, argsU = y0 :: y1 :: y2 :: y3 :: rest := by + match argsU, h4 with + | y0 :: y1 :: y2 :: y3 :: rest, _ => exact ⟨y0, y1, y2, y3, rest, rfl⟩ + have e0 : y0 = index := okinj ((F.args 0#u64 y0 rfl).symm.trans hi) + have e1 : y1 = depth := okinj ((F.args 1#u64 y1 rfl).symm.trans hd0) + have e2 : y2 = si := okinj ((F.args 2#u64 y2 rfl).symm.trans h2) + have e3 : y3 = sdp := okinj ((F.args 3#u64 y3 rfl).symm.trans h3) + subst e0 e1 e2 e3 + -- the first extra cap + have ex0 : x0 = exAtA extra 0 := okinj (hx0.symm.trans (exAtA_idx0 extra)) + obtain ⟨ac0, exSl, erest, hexc, hac0⟩ : ∃ ac0 exSl erest, excaps = (ac0, exSl) :: erest ∧ CapRelA root ac0 := by + cases hcs : excaps with + | nil => + exact absurd (ex0.trans (F.extra.2.2 0 (by simp [hcs]) (by omega))) h0 + | cons y rest' => + obtain ⟨ps, -, -, -, hrel⟩ := F.extra.2.1 0 (by simp [hcs]) + refine ⟨y.1, y.2, rest', rfl, ?_⟩ + simp only [hcs, List.getElem_cons_zero] at hrel + rw [← ex0, capAtA_eq (st := st)] at hrel + rwa [show root = capA st s x0 from okinj (hroot.symm.trans (capA_eq (st := st) s x0))] + have hx0v : CapValidA Z.th root := by + have hne : excaps ≠ [] := by rw [hexc]; simp + obtain ⟨ps, hth, hsp, hxa, -⟩ := F.extra.2.1 0 (by rw [hexc]; simp) + rw [show root = capA st s x0 from okinj (hroot.symm.trans (capA_eq (st := st) s x0))] + exact hV x0 (by + rw [ex0, hxa] + exact AbsSlotA.isSlot hP ⟨ps, hsp, Or.inl hth⟩) + -- the label + set slbl := specLabelWord (fl4LabelOf mi.miLabel) with hslbl + have hg : genInvocationType slbl = cnodeLabelOfA lbl := by + have key : ∀ (v : Nat) (g : GenInvocationLabels), lbl.val = v → labNA v = .spec (.genInvocationLabel g) → + genInvocationType slbl = g := fun v g hv e => by + rw [hslbl, F.lab, hlbl]; exact cnLabel_specA hv e + have : lbl.val = 24 ∨ lbl.val = 25 ∨ lbl.val = 26 ∨ lbl.val = 27 := by omega + rcases this with h | h | h | h + · rw [key 24 .cnodeCopy h (by decide)]; simp [cnodeLabelOfA, h] + · rw [key 25 .cnodeMint h (by decide)]; simp [cnodeLabelOfA, h] + · rw [key 26 .cnodeMove h (by decide)]; simp [cnodeLabelOfA, h] + · rw [key 27 .cnodeMutate h (by decide)]; simp [cnodeLabelOfA, h] + have hgl : IsCopyLabelA (genInvocationType slbl) := by + rw [hg]; unfold cnodeLabelOfA IsCopyLabelA + have : lbl.val = 24 ∨ lbl.val = 25 ∨ lbl.val = 26 ∨ lbl.val = 27 := by omega + rcases this with h | h | h | h <;> simp [h] + -- the destination + have hc : CapRelA (.CNode p r gs g) c := hslc ▸ F.cap + have hcv : CapValidA Z.th (.CNode p r gs g) := by + have h := hV _ (F.pin.habs.isSlot hP) + rw [capAtA_eq (st := st), ← F.pin.hslot, hslc] at h + exact h + obtain ⟨r1, hr1, f1, he1, hrel1⟩ := spec_existsA + (lookup_slot_for_cnode_op_refinesA Z.th s σ hR hV false _ c hc hcv y0 y1) + unfold cspace.lookup_target_slot at hres + rw [hr1] at hres + have er1 : r1 = .Ok (cslotAddrA sd) := okinj hres + subst er1 + cases f1 with + | error _ => exact hrel1.elim + | ok dsl => + obtain ⟨hd1, pd, hpd1, hpd, -⟩ := hrel1 + have edsl : dsl = sd := cslotAddrA_inj hP ⟨pd, hpd1, Or.inl hpd⟩ hCd hd1.symm + subst edsl + have hlt : (lookupTargetSlot (Z := Unit) c (dataToCptr y0.bv) (dataToNat y1.bv)).run σ = ({(.ok dsl, σ)}, False) := he1 + obtain ⟨ad, hcad, hgd⟩ := hR.slot_capA hpd1 hpd + replace hcad : CapRelA (capA st s (cslotAddrA dsl)) ad := hcad + have hadn : ad = .nullCap := by + have h := hcad.2; rw [hdn] at h; exact h.symm + have hee := ensureEmpty_runA dsl σ hgd + rw [if_pos hadn] at hee + -- the source + obtain ⟨r2, hr2, f2, he2, hrel2⟩ := spec_existsA + (lookup_slot_for_cnode_op_refinesA Z.th s σ hR hV true root ac0 hac0 hx0v y2 y3) + rw [show cspace.lookup_source_slot st s root y2 y3 = cspace.lookup_slot_for_cnode_op st s true root y2 y3 from rfl, + hr2] at hsrc + have er2 : r2 = .Ok (cslotAddrA ss) := okinj hsrc + subst er2 + cases f2 with + | error _ => exact hrel2.elim + | ok ssl => + obtain ⟨hs1, ps, hps1, hps, -⟩ := hrel2 + have essl : ssl = ss := cslotAddrA_inj hP ⟨ps, hps1, Or.inl hps⟩ hCs hs1.symm + subst essl + have hls : (lookupSourceSlot (Z := Unit) ac0 (dataToCptr y2.bv) (dataToNat y3.bv)).run σ = + ({(.ok ssl, σ)}, False) := he2 + have hsO : cslotAddrA ssl ∈ OS := J.j5.2.1 _ (hCs.isSlot hP) + obtain ⟨srcA, hsca, hgs⟩ := hR.slot_capA hps1 hps + replace hsca : CapRelA (capA st s (cslotAddrA ssl)) srcA := hsca + have hsan : srcA ≠ .nullCap := hsca.ne_null hsn + have hnc0 : isUtA (capA st s (cslotAddrA ssl)) = true → NoChildrenRelA (st := st) s σ (cslotAddrA ssl) ssl := by + intro hu + obtain ⟨q, b, i, ee, hcu⟩ := isUtA_ut hu + exact noChildren_of_adjA hP J.sys.ent.os (J.j.1.cdt.tree.grow J.j5.2.1 (fun x hx hn => J.j5.2.2 x hx hn)) + J.j.1.cdt hCs hsO hcu (J.sys.sysl.sys.stz.1.2.1 _ hsO hu) + -- the new cap + have hlen' : (msgLenA info buf).val = rest.length + 4 := by simpa using F.len + have hargs' : ∀ (i : U64) v, rest[i.val - 4]? = some v → 4 ≤ i.val → + msg.syscall_arg st s (L.curW s) buf i = ok v := by + intro i v hv hi4 + apply F.args i v + have : i.val = (i.val - 4) + 4 := by omega + rw [this]; simpa using hv + obtain ⟨r3, hr3, R, hR3, hrel3⟩ := copied_relA s σ lbl slbl hg h24 (msgLenA info buf) buf (L.curW s) + (cur_thread_ok s) rest hlen' hargs' ssl dsl _ hsca.1 (CapRt.decodeF_encWf _ _) hnc0 + have er3 : r3 = .Ok (nc, mv) := okinj (hr3.symm.trans hcop) + subst er3 + obtain ⟨hReq, hwf, -⟩ := hrel3 + rw [hReq] at hR3 + obtain ⟨anc, hanc, hrest⟩ := copyRest_okA hgl hsca.2 hwf hR3 + have hnn : anc ≠ .nullCap := hanc.ne_null hn + have hnh : NoHiPtV anc := + copyRestA_noHiPtA hrest hnn (noHiPtV_slotA J.sidle hR hps1 hps hgs) + refine ⟨anc, hanc, hnh, fun cptr => ?_⟩ + have hdec : (decodeCnodeInvocation (Z := Unit) slbl ((y0 :: y1 :: y2 :: y3 :: rest).map UScalar.bv) c + (excaps.map Prod.fst)).run σ = ({(.ok (if mv then .moveCall anc ssl dsl else .insertCall anc ssl dsl), σ)}, False) := by + simp only [List.map_cons] + rw [decode_lookupA slbl c (excaps.map Prod.fst) hgl.facts.1, run_bind_okA hlt, hexc] + simp only [List.map_cons] + rw [copyTail_bodyA _ hgl, run_bind_okA hee, run_bind_okA hls, run_bind_okA (run_liftEA hgs)] + simp only [hsan, Fl4Spec.NonDetE.whenE, ↓reduceIte, ndE_pure_bindA] + rw [hrest] + cases mv <;> simp [finishResA, hnn] + have ec : c = .cnodeCap (specPtr p) r.val (bitsOfA g.val gs.val) := by + have h : absCapA (.CNode p r gs g) = c := hc.2 + exact h.symm + have e : fl4DecodeInvocation (Z := Unit) call false mi.miLabel ((y0 :: y1 :: y2 :: y3 :: rest).map UScalar.bv) cptr + sl c excaps buffer = Fl4Spec.NonDetE.liftME Fl4Invocation.invokeCNode + (decodeCnodeInvocation slbl ((y0 :: y1 :: y2 :: y3 :: rest).map UScalar.bv) c (excaps.map Prod.fst)) := by + rw [ec]; rfl + rw [e] + exact run_liftME_okA hdec + +/-- **CNode Copy / Mint / Move / Mutate's pin**: `cnCopy_decodeEqA` as membership. -/ +theorem cnCopy_pinA (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + {info : msginfo.MessageInfo} {buf slot : U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} {mi : MessageInfo} + {sl : CslotPtr} {c : Cap} {excaps : List (Cap × CslotPtr)} {buffer : Option ObjRef} {argsU : List U64} + (F : FrontJA st Z.th s σ info buf slot extra mi sl c excaps buffer argsU) + {lbl index depth x0 si sdp p r gs g : U64} {root nc : capability.Cap} {mv call : Bool} {ss sd : CslotPtr} + (hlbl : info.label = lbl) (h24 : 24 ≤ lbl.val ∧ lbl.val ≤ 27) + (hCs : AbsSlotA Z.th ss) (hCd : AbsSlotA Z.th sd) + (hsn : capA st s (cslotAddrA ss) ≠ .Null) (hdn : capA st s (cslotAddrA sd) = .Null) + (hn : nc ≠ .Null) (hslc : capA st s slot = .CNode p r gs g) + (hl4 : ¬ msgLenA info buf < 4#u64) + (hi : msg.syscall_arg st s (L.curW s) buf 0#u64 = ok index) + (hd0 : msg.syscall_arg st s (L.curW s) buf 1#u64 = ok depth) + (hres : cspace.lookup_target_slot st s (.CNode p r gs g) index depth = ok (.Ok (cslotAddrA sd))) + (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) (h0 : x0 ≠ 0#u64) + (h2 : msg.syscall_arg st s (L.curW s) buf 2#u64 = ok si) + (h3 : msg.syscall_arg st s (L.curW s) buf 3#u64 = ok sdp) + (hroot : obj.Slot.cap st x0 s = ok root) + (hsrc : cspace.lookup_source_slot st s root si sdp = ok (.Ok (cslotAddrA ss))) + (hcop : cnode.copied_cap st s lbl (msgLenA info buf) buf (cslotAddrA ss) (capA st s (cslotAddrA ss)) = + ok (.Ok (nc, mv))) : + ∃ anc, CapRelA nc anc ∧ NoHiPtV anc ∧ DecPinA call Z.th σ slot + (.invokeCNode (if mv then .moveCall anc ss sd else .insertCall anc ss sd)) := by + obtain ⟨anc, hanc, hnh, hdec⟩ := cnCopy_decodeEqA J F hlbl h24 hCs hCd hsn hdn hn hslc hl4 hi hd0 hres hx0 h0 h2 h3 hroot hsrc hcop + exact ⟨anc, hanc, hnh, DecPinA.of_frontA F.pin hdec⟩ + +open private capEqA_update from Fl4ProofsA64.P3.CnodeRotA in +set_option maxHeartbeats 4000000 in +/-- **CNode Rotate's pin**, three slots or the swap (`dsl = ss`) (module docstring). -/ +theorem cnRotate_pinA (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {info : msginfo.MessageInfo} {buf slot : U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} {mi : MessageInfo} + {sl : CslotPtr} {c : Cap} {excaps : List (Cap × CslotPtr)} {buffer : Option ObjRef} {argsU : List U64} + (F : FrontJA st Z.th s σ info buf slot extra mi sl c excaps buffer argsU) + {index depth x0 x1 pdata sdata p r gs g : U64} {ns np : capability.Cap} {call : Bool} {ss sp dsl : CslotPtr} + (hlbl : info.label = fl4_abi.label.CNodeRotate) + (hCs : AbsSlotA Z.th ss) (hCp : AbsSlotA Z.th sp) (hCd : AbsSlotA Z.th dsl) + (hCdn : ∃ pd, dsl.1 = specPtr pd ∧ Z.th pd = some (.cnode dsl.2.length)) + (hns : ns ≠ .Null) (hnp : np ≠ .Null) (hslc : capA st s slot = .CNode p r gs g) + (hl8 : ¬ msgLenA info buf < 8#u64) + (hi : msg.syscall_arg st s (L.curW s) buf 0#u64 = ok index) + (hd0 : msg.syscall_arg st s (L.curW s) buf 1#u64 = ok depth) + (hres : cspace.lookup_target_slot st s (.CNode p r gs g) index depth = ok (.Ok (cslotAddrA dsl))) + (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) (h0 : x0 ≠ 0#u64) + (hx1 : Aeneas.Std.Array.index_usize extra 1#usize = ok x1) (h1 : x1 ≠ 0#u64) + (h2 : msg.syscall_arg st s (L.curW s) buf 2#u64 = ok pdata) + (h5 : msg.syscall_arg st s (L.curW s) buf 5#u64 = ok sdata) + (hrot : cnode.rotate_slots st s buf extra (cslotAddrA dsl) = ok (.Ok (cslotAddrA ss, cslotAddrA sp))) + (hus : objects.update_cap_data true sdata (capA st s (cslotAddrA ss)) = ok ns) + (hup : objects.update_cap_data true pdata (capA st s (cslotAddrA sp)) = ok np) : + ∃ an ap, CapRelA ns an ∧ CapRelA np ap ∧ NoHiPtV an ∧ NoHiPtV ap ∧ + DecPinA call Z.th σ slot (.invokeCNode (.rotateCall an ap ss sp dsl)) := by + have hR := J.j.1.st + have hV : SlotsValidA Z.th (L.mem s) := J.j.1.cv + have hP := J.j.1.place + have h8 : 8 ≤ argsU.length := by + have := F.len + have : ¬ (msgLenA info buf).val < (8#u64 : U64).val := fun h => hl8 (by scalar_tac) + have : (8#u64 : U64).val = 8 := rfl + omega + obtain ⟨a0, a1, a2, a3, a4, a5, a6, a7, rest, rfl⟩ : + ∃ a0 a1 a2 a3 a4 a5 a6 a7 rest, argsU = a0 :: a1 :: a2 :: a3 :: a4 :: a5 :: a6 :: a7 :: rest := by + match argsU, h8 with + | a0 :: a1 :: a2 :: a3 :: a4 :: a5 :: a6 :: a7 :: rest, _ => exact ⟨a0, a1, a2, a3, a4, a5, a6, a7, rest, rfl⟩ + have e0 : a0 = index := okinj ((F.args 0#u64 a0 rfl).symm.trans hi) + have e1 : a1 = depth := okinj ((F.args 1#u64 a1 rfl).symm.trans hd0) + have e2 : a2 = pdata := okinj ((F.args 2#u64 a2 rfl).symm.trans h2) + have e5 : a5 = sdata := okinj ((F.args 5#u64 a5 rfl).symm.trans h5) + subst e0 e1 e2 e5 + -- the two extra caps + have ex0 : x0 = exAtA extra 0 := okinj (hx0.symm.trans (exAtA_idx0 extra)) + have ex1 : x1 = exAtA extra 1 := okinj (hx1.symm.trans (index_exAtA extra 1#usize (by simp))) + have hlen2 : 1 < excaps.length := by + by_contra h + exact h1 (ex1.trans (F.extra.2.2 1 (by omega) (by omega))) + obtain ⟨y0, y1, erest, hexc⟩ : ∃ y0 y1 erest, excaps = y0 :: y1 :: erest := by + match excaps, hlen2 with + | y0 :: y1 :: erest, _ => exact ⟨y0, y1, erest, rfl⟩ + have exk : ∀ (j : Nat) (x : U64), x = exAtA extra j → ∀ (hj : j < excaps.length), + CapRelA (capA st s x) excaps[j].1 ∧ CapValidA Z.th (capA st s x) := by + intro j x ex hj + obtain ⟨ps, hth, hsp, hxa, hrel⟩ := F.extra.2.1 j hj + rw [← ex, capAtA_eq (st := st)] at hrel + exact ⟨hrel, hV x (by rw [ex, hxa]; exact AbsSlotA.isSlot hP ⟨ps, hsp, Or.inl hth⟩)⟩ + have k0 := exk 0 x0 ex0 (by omega) + have k1 := exk 1 x1 ex1 hlen2 + simp only [hexc, List.getElem_cons_zero, List.getElem_cons_succ] at k0 k1 + have hg : genInvocationType (specLabelWord (fl4LabelOf mi.miLabel)) = .cnodeRotate := by + rw [F.lab, hlbl] + exact cnLabel_specA (v := 28) (by simp [fl4_abi.label.CNodeRotate]) (by decide) + set slbl := specLabelWord (fl4LabelOf mi.miLabel) with hslbl + -- the destination + have hc : CapRelA (.CNode p r gs g) c := hslc ▸ F.cap + have hcv : CapValidA Z.th (.CNode p r gs g) := by + have h := hV _ (F.pin.habs.isSlot hP) + rw [capAtA_eq (st := st), ← F.pin.hslot, hslc] at h + exact h + obtain ⟨r1, hr1, f1, he1, hrel1⟩ := spec_existsA + (lookup_slot_for_cnode_op_refinesA Z.th s σ hR hV false _ c hc hcv a0 a1) + unfold cspace.lookup_target_slot at hres + rw [hr1] at hres + have er1 : r1 = .Ok (cslotAddrA dsl) := okinj hres + subst er1 + cases f1 with + | error _ => exact hrel1.elim + | ok d => + obtain ⟨hd1, pd, hpd1, hpd, -⟩ := hrel1 + have edsl : d = dsl := cslotAddrA_inj hP ⟨pd, hpd1, Or.inl hpd⟩ hCd hd1.symm + subst edsl + have hlt : (lookupTargetSlot (Z := Unit) c (dataToCptr a0.bv) (dataToNat a1.bv)).run σ = ({(.ok d, σ)}, False) := he1 + -- the two source slots + have hrel := rotate_slots_relA (st := st) s σ hR hV hP buf extra x0 x1 hx0 hx1 y0.1 y1.1 k0.1 k0.2 k1.1 k1.2 + a3 a4 a6 a7 (F.args 3#u64 a3 rfl) (F.args 4#u64 a4 rfl) (F.args 6#u64 a6 rfl) (F.args 7#u64 a7 rfl) d pd hpd1 hpd + (rotFinA a2.bv a5.bv d) + obtain ⟨res, hres', hrel⟩ := spec_existsA hrel + have er : res = .Ok (cslotAddrA ss, cslotAddrA sp) := okinj (hres'.symm.trans hrot) + subst er + obtain ⟨sl', pl', ⟨hs, ps, hps1, hps, -⟩, ⟨hp', pp, hpl1, hpl, -⟩, -, -, -, -, -, srcA, pivA, hsrcEq, hpivEq, + hgsA, hgpA, hmsp⟩ := hrel + have essl : sl' = ss := cslotAddrA_inj hP ⟨ps, hps1, Or.inl hps⟩ hCs hs.symm + have espl : pl' = sp := cslotAddrA_inj hP ⟨pp, hpl1, Or.inl hpl⟩ hCp hp'.symm + subst essl espl + simp only [capAtA_eq (st := st)] at hsrcEq hpivEq + have hwfS : CapWfA (capA st s (cslotAddrA sl')) := by + obtain ⟨_, h, -⟩ := hR.slot_capA hps1 hps; exact h.1 + have hwfP : CapWfA (capA st s (cslotAddrA pl')) := by + obtain ⟨_, h, -⟩ := hR.slot_capA hpl1 hpl; exact h.1 + obtain ⟨ns', hns', hnsw, hnsa, -⟩ := update_relA true a5 (capA st s (cslotAddrA sl')) hwfS (CapRt.decodeF_encWf _ _) + obtain ⟨np', hnp', hnpw, hnpa, -⟩ := update_relA true a2 (capA st s (cslotAddrA pl')) hwfP (CapRt.decodeF_encWf _ _) + have ens : ns' = ns := okinj (hns'.symm.trans hus) + have enp : np' = np := okinj (hnp'.symm.trans hup) + subst ens enp + have hnsEq : CapRelA ns' (updateCapData true a5.bv srcA) := ⟨hnsw, capEqA_update hsrcEq hnsa⟩ + have hnpEq : CapRelA np' (updateCapData true a2.bv pivA) := ⟨hnpw, capEqA_update hpivEq hnpa⟩ + have hnsan := hnsEq.ne_null hns + have hnpan := hnpEq.ne_null hnp + have hnhs : NoHiPtV (updateCapData true a5.bv srcA) := + noHiPtV_updateA _ _ (noHiPtV_slotA J.sidle hR hps1 hps hgsA) + have hnhp : NoHiPtV (updateCapData true a2.bv pivA) := + noHiPtV_updateA _ _ (noHiPtV_slotA J.sidle hR hpl1 hpl hgpA) + refine ⟨_, _, hnsEq, hnpEq, hnhs, hnhp, DecPinA.of_frontA F.pin fun cptr => ?_⟩ + have hdec : (decodeCnodeInvocation (Z := Unit) slbl + ((a0 :: a1 :: a2 :: a3 :: a4 :: a5 :: a6 :: a7 :: rest).map UScalar.bv) c (excaps.map Prod.fst)).run σ = + ({(.ok (.rotateCall (updateCapData true a5.bv srcA) (updateCapData true a2.bv pivA) sl' pl' d), σ)}, False) := by + simp only [List.map_cons] + rw [decode_lookupA slbl c (excaps.map Prod.fst) (by rw [hg]; decide), run_bind_okA hlt, hexc] + simp only [List.map_cons] + rw [copyTail_rotateA hg, hmsp, rotFin_runA, if_neg hnsan, if_neg hnpan] + have ec : c = .cnodeCap (specPtr p) r.val (bitsOfA g.val gs.val) := by + have h : absCapA (.CNode p r gs g) = c := hc.2 + exact h.symm + have e : fl4DecodeInvocation (Z := Unit) call false mi.miLabel + ((a0 :: a1 :: a2 :: a3 :: a4 :: a5 :: a6 :: a7 :: rest).map UScalar.bv) cptr sl c excaps buffer = + Fl4Spec.NonDetE.liftME Fl4Invocation.invokeCNode (decodeCnodeInvocation slbl + ((a0 :: a1 :: a2 :: a3 :: a4 :: a5 :: a6 :: a7 :: rest).map UScalar.bv) c (excaps.map Prod.fst)) := by + rw [ec]; rfl + rw [e] + exact run_liftME_okA hdec + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3PinFrontA.lean b/proofs/Fl4ProofsA64/P3/Stage3PinFrontA.lean new file mode 100644 index 000000000..2ceb739bf --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3PinFrontA.lean @@ -0,0 +1,114 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Plan B): **the front-end values at the syscall-entry record**, for discharging the +dispatch scopes' pins (`DecPinA`): from FL4's four prefix successes (message info, IPC buffer, capability lookup, +extra caps) at `JSyscallA`, the spec's front end has the single outcome `FrontPinA`, related to FL4's (`FrontJA`). + +Named premises: [J] `JSyscallA`. New module. +-/ +import Fl4ProofsA64.P3.Stage3DispBaseA +import Fl4ProofsA64.P3.Stage3FinA +import Fl4ProofsA64.P3.FrontEndExtraA +import Fl4ProofsA64.P3.EpBufA +import Fl4ProofsA64.P3.OuterEntryA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-- **The front end's values, related to FL4's** (module docstring). -/ +structure FrontJA (st : machine.Store S) [L : LawfulStore st] (th : TypedHeapA) (s : S) (σ : State Unit) + (info : msginfo.MessageInfo) (buf slot : U64) (e : Aeneas.Std.Array obj.Slot 3#usize) (mi : MessageInfo) + (sl : CslotPtr) (c : Cap) (excaps : List (Cap × CslotPtr)) (buffer : Option ObjRef) (argsU : List U64) : + Prop where + pin : FrontPinA th σ slot mi sl c excaps buffer (argsU.map UScalar.bv) + lab : mi.miLabel = info.label.bv + cap : CapRelA (capA st s slot) c + cnode : ∃ p, sl.1 = specPtr p ∧ th p = some (.cnode sl.2.length) + extra : ExtraOKA th (L.mem s) e excaps + len : (msgLenA info buf).val = argsU.length + args : ∀ (i : U64) x, argsU[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x + +set_option maxHeartbeats 4000000 in +/-- **`FrontJA` at the syscall-entry record** (module docstring). -/ +theorem JSyscallA.frontJA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} + {κ : U64 → U64} {n : Nat} {J5 : _} {J7 : _} {s : S} {a : A} {σ : State Unit} {m : McState} + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {cptr : U64} {info : msginfo.MessageInfo} {buf : U64} {r1 : cspace.Resolved} + {e : Aeneas.Std.Array obj.Slot 3#usize} + (einfo : msg.msg_info st s (L.curW s) = ok info) + (ebuf : msg.lookup_ipc_buffer st s false (L.curW s) = ok buf) + (elr : cspace.lookup_slot st s (L.curW s) cptr = ok (.Ok r1)) + (eex : ipc.lookup_extra_caps st s (L.curW s) buf info = ok (.Ok e)) : + ∃ mi sl c excaps buffer argsU, FrontJA st Z.th s σ info buf r1.slot e mi sl c excaps buffer argsU := by + have ct : σ.curThread = specPtr (L.curW s) := J.j.1.sch.curThread + have htZ : L.curW s ∈ Z.T := J.sr.curT + have hth : Z.th (L.curW s) = some .tcb := (J.j.1.thT _).2 htZ + obtain ⟨x, hk, -⟩ := J.ep.tcb (L.curW s) htZ + set t := L.curW s with ht + obtain ⟨info', einfo', -, -, hext, -⟩ := spec_existsA (msg_info_specA (L := L) s t) + have ei : info' = info := okinj (einfo'.symm.trans einfo) + subst ei + set mi := Fl4SpecA64.dataToMessageInfo (x.tcbArch.tcbContext.userRegs Fl4SpecA64.msg_info_register) with hmi + have hmw := (J.ep.at_tcb htZ hk).regView.1 + have gmi : getMessageInfo (Z := Unit) (specPtr t) σ = ({(mi, σ)}, False) := getMessageInfo_eqA hk + obtain ⟨buf', ebuf', hbuf0, hbufS⟩ := spec_existsA + (lookup_buf_full s σ J.j.1.st hth htZ hk J.bufCover J.bufNative false) + have eb : buf' = buf := okinj (ebuf'.symm.trans ebuf) + subst eb + have eIB : lookupIpcBuffer (Z := Unit) false (specPtr t) σ = ({(ipcBufOfA false x, σ)}, False) := + lookupIpcBuffer_eqA hk false + have hBV : BufViewA (L.mem s) σ.machineState.underlyingMemory buf' (ipcBufOfA false x) := by + cases ho : ipcBufOfA false x with + | none => rw [hbuf0.2 ho]; exact bufViewA_none _ _ + | some sb => + refine J.ep.bufView (hbufS sb ho) fun h => ?_ + rw [hbuf0.1 h] at ho; cases ho + obtain ⟨lr, elr', se, hse, hrel⟩ := spec_existsA (lookup_slot_refinesA Z.th s σ J.j.1.st J.j.1.cv t hth cptr) + have el : lr = .Ok r1 := okinj (elr'.symm.trans elr) + subst el + cases se with + | error _ => exact hrel.1.elim + | ok v => + obtain ⟨sl, rest⟩ := v + obtain ⟨⟨hsl, -⟩, p, hp1, hp, -⟩ := hrel + obtain ⟨c, hcr, hgc⟩ := J.j.1.st.slot_capA hp1 hp + have hn : info'.extra_caps.val = unat mi.miExtraCaps := by + show info'.extra_caps.bv.toNat = _; rw [hext, hmw] + have hn3 : info'.extra_caps.val ≤ 3 := by + show info'.extra_caps.bv.toNat ≤ 3; rw [hext, hmw]; exact extra_le3A _ + obtain ⟨r2, er2, hx⟩ := spec_existsA + (lookup_extra_relA J.j.1.st J.j.1.cv hth buf' (ipcBufOfA false x) hBV info' mi hn hn3) + have ee : r2 = .Ok e := okinj (er2.symm.trans eex) + subst ee + obtain ⟨excaps, hrun, hok⟩ := hx + obtain ⟨args, gargs⟩ : ∃ args, getMrs (Z := Unit) (specPtr t) (ipcBufOfA false x) mi σ = ({(args, σ)}, False) := + ⟨_, getMrs_eqA hk _ hBV.al mi⟩ + have eargs : (args.map fun w => (⟨w⟩ : U64)).map UScalar.bv = args := by + rw [List.map_map]; exact List.map_id _ + have gargs' : getMrs (Z := Unit) (specPtr t) (ipcBufOfA false x) mi σ = + ({((args.map fun w => (⟨w⟩ : U64)).map UScalar.bv, σ)}, False) := by rw [eargs]; exact gargs + have hRV : RegViewA (L.mem s) σ t := fun xs h => (J.ep.at_tcb htZ h).regView + obtain ⟨hlen, hargs, -⟩ := J.j.1.argsJ hRV einfo gmi hBV gargs' + have hlab : mi.miLabel = info'.label.bv := by + have hm := msg_info_specA (L := L) s t + rw [einfo, WP.spec_ok] at hm + rw [hm.1, hmw] + exact label_maskA _ + have hcap : CapRelA (capA st s r1.slot) c := by rw [hsl, ← capAtA_eq]; exact hcr + refine ⟨mi, sl, c, excaps, ipcBufOfA false x, args.map fun w => (⟨w⟩ : U64), ?_, hlab, hcap, ⟨p, hp1, hp⟩, hok, + by simpa using hlen, hargs⟩ + refine ⟨by rw [ct]; exact gmi, hsl, ⟨p, hp1, Or.inl hp⟩, hgc, by rw [ct]; exact eIB, by rw [ct]; exact hrun, ?_⟩ + rw [eargs, ct]; exact gargs + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3PinVsA.lean b/proofs/Fl4ProofsA64/P3/Stage3PinVsA.lean new file mode 100644 index 000000000..35f6005d6 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3PinVsA.lean @@ -0,0 +1,546 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Plan B): **the VSpace scope's pins** (`DecPinA`, and `frameUnmapU`'s `hcs`) at the real +decode, one theorem per constructor of `VsScopeA`. Hypotheses: the front end's values (`FrontJA`) and the +constructor's FL4 decode-site facts; the provider instantiates the constructor's spec data with the front end's +(`cte := sl`, `exs := excaps`, `args := argsU`, `slbl := specLabelWord (archL …)`). + +Two facts about the spec state are not consequences of `CapRelA` and are named premises of the arms that need them: +* [J] `hcan` (`ptUnmapS1`, `ptUnmapX`): the spec's mapped page-table cap at the invoked slot is the canonical + representation `absCapA` of FL4's (`CapEqA` allows another virtual address at a 48-bit sign extension). + `ptCanon_loA` discharges it for `va < 0xffff_0000_0000_0000`; a high-window table (`ptUnmapX`, `k = .high`) keeps it. +* `frameUnmapX`'s `hk2`: for a Stage-2 frame cap the root kind is `.stage2` (the decode's answer; the provider + chooses it). + +Spec finality (the `PageTableUnmap` arms) is derived from FL4's `is_final_capability` (`JSyscallA.finStA`). + +Named premises: [J] `JSyscallA`, `hcan`. New module. +-/ +import Fl4ProofsA64.P3.Stage3PinFrontA +import Fl4ProofsA64.P3.Stage3PinCnA +import Fl4ProofsA64.P3.Stage3DispVsA +import Fl4ProofsA64.P3.CnodeDecDelA +import Fl4ProofsA64.P3.CnodeMdbBridgeA +import Fl4ProofsA64.P3.UnmapDecJA +import Fl4ProofsA64.P3.VsTopFrA +import Fl4ProofsA64.P3.VsTopPtA +import Fl4ProofsA64.P3.VsFindA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +/-! ## The spec decoder at an arch cap -/ + +section dec + +/-- The pin of an arch invocation from the arch decoder. -/ +theorem DecPinA.of_archA {call : Bool} {th : TypedHeapA} {σ : State Unit} {slot : U64} {mi : MessageInfo} + {sl : CslotPtr} {c : Cap} {excaps : List (Cap × CslotPtr)} {buffer : Option ObjRef} {args : List Data} + (F : FrontPinA th σ slot mi sl c excaps buffer args) {ac : ArchCap} (hc : c = .archObjectCap ac) + {l : Fl4Label} (hl : fl4LabelOf mi.miLabel = l) {ai : Fl4ArchInvocation} + (h : ∀ cptr : CapRef, (.ok ai, σ) ∈ + ((fl4ArchDecodeInvocation (Z := Unit) call l args cptr sl ac excaps).run σ).1) : + DecPinA call th σ slot (.invokeArchObject ai) := by + refine ⟨_, _, _, _, _, _, F, fun cptr => ?_⟩ + have e : fl4DecodeInvocation (Z := Unit) call false mi.miLabel args cptr sl c excaps buffer = + Fl4Spec.NonDetE.liftME Fl4Invocation.invokeArchObject + (fl4ArchDecodeInvocation call (fl4LabelOf mi.miLabel) args cptr sl ac excaps) := by + rw [hc]; rfl + rw [e, hl] + exact mem_liftME_okPinA (h cptr) + +/-- A singleton outcome is a member. -/ +theorem run_mem_pinA {α : Type} {m : Fl4SpecA64.SEMonad Unit α} {σ : State Unit} {x : Except SyscallError α} + (h : m.run σ = ({(x, σ)}, False)) : (x, σ) ∈ (m.run σ).1 := by + rw [h]; exact rfl + +/-- **`ARMPageMap` of a plain frame whose first extra cap is no FL4 VSpace root is l4v's decoder** (a Stage-2 frame +cap there included; `dec_pgMap_specA` excludes every FL4 cap). -/ +theorem dec_pgMap_vsA (calling : Bool) (args : List Data) (xSlot : CapRef) (cte : CslotPtr) + (exs : List (Cap × CslotPtr)) (p : ObjRef) (R : CapRights) (sz : VMPageSize) (dev : Bool) + (mp : Option (Asid × VSpaceRef)) + (hN : ∀ c sl rest, exs = (c, sl) :: rest → + (∀ q m, c ≠ .archObjectCap (.ext (.stage2VSpaceCap q m))) ∧ + ∀ q m, c ≠ .archObjectCap (.ext (.highVSpaceCap q m))) : + fl4ArchDecodeInvocation (Z := Unit) calling (archL .armPageMap) args xSlot cte (.frameCap p R sz dev mp) exs = + Fl4Spec.NonDetE.liftME Fl4ArchInvocation.spec (decodeFrameInvocation (Z := Unit) + (specLabelWord (archL .armPageMap)) args cte (.frameCap p R sz dev mp) exs) := by + have h1 : ¬ (archL .armPageMap = Fl4Label.ext .armPageDeriveSubframe) := fun h => by cases h + have h2 : ¬ (archL .armPageMap = Fl4Label.ext .armPageTestAndClearAccessed) := fun h => by cases h + have h3 : ¬ (archL .armPageMap = archL .armPageUnmap) := fun h => by cases h + unfold fl4ArchDecodeInvocation + dsimp only + rw [if_neg h1, if_neg h2, flushLbl_pgMapA, if_neg Bool.false_ne_true] + by_cases h : archL .armPageMap = archL .armPageMap ∧ args.length > 2 ∧ exs.length > 0 + · rw [if_pos h] + obtain ⟨⟨c, sl⟩, rest, rfl⟩ : ∃ e rest, exs = e :: rest := by + cases exs with + | nil => simp at h + | cons e rest => exact ⟨e, rest, rfl⟩ + dsimp only [List.getElem?_cons_zero, Fl4SpecA64.the] + split + · exact absurd rfl ((hN _ _ _ rfl).1 _ _) + · exact absurd rfl ((hN _ _ _ rfl).2 _ _) + · rfl + · rw [if_neg h, if_neg h3] + rfl + +/-- **`ARMPageTableMap` of an unmapped table whose first extra cap is no FL4 VSpace root is l4v's decoder.** -/ +theorem dec_ptMap_vsA (calling : Bool) (args : List Data) (xSlot : CapRef) (cte : CslotPtr) + (exs : List (Cap × CslotPtr)) (p : ObjRef) + (hN : ∀ c sl rest, exs = (c, sl) :: rest → + (∀ q m, c ≠ .archObjectCap (.ext (.stage2VSpaceCap q m))) ∧ + ∀ q m, c ≠ .archObjectCap (.ext (.highVSpaceCap q m))) : + fl4ArchDecodeInvocation (Z := Unit) calling (archL .armPageTableMap) args xSlot cte + (.pageTableCap p .normalPtT none) exs = + Fl4Spec.NonDetE.liftME Fl4ArchInvocation.spec (decodePageTableInvocation (Z := Unit) + (specLabelWord (archL .armPageTableMap)) args cte (.pageTableCap p .normalPtT none) exs) := by + unfold fl4ArchDecodeInvocation + dsimp only + by_cases h : archL .armPageTableMap = archL .armPageTableMap ∧ args.length > 1 ∧ exs.length > 0 + · rw [if_pos h] + obtain ⟨⟨c, sl⟩, rest, rfl⟩ : ∃ e rest, exs = e :: rest := by + cases exs with + | nil => simp at h + | cons e rest => exact ⟨e, rest, rfl⟩ + dsimp only [List.getElem?_cons_zero, Fl4SpecA64.the] + split + · simp only [Fl4Spec.NonDetE.whenE, ne_eq, not_true_eq_false, ↓reduceIte, ndE_pure_bindA] + split + · exact absurd rfl ((hN _ _ _ rfl).1 _ _) + · exact absurd rfl ((hN _ _ _ rfl).2 _ _) + · rfl + · rfl + · rw [if_neg h] + rfl + +/-- **`hcan` below the high window**: for FL4 `va < 0xffff_0000_0000_0000` (every Stage-1 and Stage-2 address) the +spec's mapped page-table cap is `absCapA`. A high-window address is not determined by `CapEqA`. -/ +theorem ptCanon_loA {p asid va : U64} {c : Cap} (h : CapEqA (.PageTable p true asid va) c) + (hva : va.val < 0xffff000000000000) : c = absCapA (.PageTable p true asid va) := by + obtain ⟨asid', va', rfl, has, hv⟩ := CapEqA.pt_mapped_iff.mp h + have e : va.bv = va' := by + rcases hv with h1 | ⟨h1, -⟩ + · exact h1 + · unfold unpack48A at h1 + split_ifs at h1 + · exact h1 + · have : (0xffff000000000000 : Nat) ≤ va.bv.toNat := by + rw [h1, BitVec.toNat_or]; exact Nat.right_le_or + have : va.bv.toNat = va.val := rfl + omega + rw [← e] + simp [absCapA, mapDataA, has] + +end dec + +/-! ## At the syscall-entry record -/ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {R : Set U64 → JSlotA S} {s : S} {a : A} {σ : State Unit} {m : McState} + +/-- **The finality state facts at the syscall-entry record** (`DStateA.ofEntA`'s `FinStA` part). -/ +theorem JSyscallA.finStA (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st R) s a σ m) : + FinStA st Z.th OS Z.G.chains s σ := by + have E0 := J.toJEntA + have hT := J.j.1.cdt.tree.grow J.j5.2.1 (fun x hx hn => J.j5.2.2 x hx hn) + obtain ⟨G', par', lk', rng', kind', nl', cls', -, -⟩ := E0.sys.sysl.sys.stz.1.1 + exact + { rel := E0.j.1.st + place := E0.j.1.place + cov := J.finCov + lk := hT.lk + rng := rng'.congr (lk'.chains_iff hT.lk E0.sys.ent.os.nz) + kind := kind' + cls := cls' + zf := E0.sys.sysl.sys.stz.2.1 + nz := E0.sys.ent.os.nz + sub := J.j5.2.1 + nul := J.j5.2.2 + fr := J.frBase + irq := IrqRunA.ofP1 J.j5.1.2.1 } + +/-- **The spec's finality of a page-table cap from FL4's test.** -/ +theorem JSyscallA.finPtA (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st R) s a σ m) + {sl : CslotPtr} (hab : AbsSlotA Z.th sl) {ptr asid va : U64} {mp : Bool} + (hcap : capA st s (cslotAddrA sl) = .PageTable ptr mp asid va) + (hf : cdt.is_final_capability st s (cslotAddrA sl) = ok true) : + isFinalCap' (Z := Unit) (absCapA (.PageTable ptr mp asid va)) σ := by + obtain ⟨⟨b, hb, e⟩, -⟩ := finalHypA (st := st) Z.th OS Z.G.chains s σ sl _ J.finStA hab hcap trivial + have hw : cdt.is_final_capability st s (cslotAddrA sl) = + cdt.is_final_capability_with_cap st s (cslotAddrA sl) (.PageTable ptr mp asid va) := by + unfold cdt.is_final_capability + rw [capA_eq, hcap, bind_ok] + rw [← hw, hf] at hb + exact e.1 (okinj hb).symm + +variable {info : msginfo.MessageInfo} {buf slot : U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} {mi : MessageInfo} + {sl : CslotPtr} {c : Cap} {excaps : List (Cap × CslotPtr)} {buffer : Option ObjRef} {argsU : List U64} + +/-- The spec's label of the invocation. -/ +theorem FrontJA.labelA {th : TypedHeapA} (F : FrontJA st th s σ info buf slot extra mi sl c excaps buffer argsU) + {lbl : U64} (hlab : info.label = lbl) {v : Nat} (hv : lbl.val = v) {l : Fl4Label} (e : labNA v = l) : + fl4LabelOf mi.miLabel = l := by + rw [F.lab, hlab, fl4LabelOf_bvA, hv, e] + +/-- The invoked cap, not a mapped page table. -/ +theorem FrontJA.absA {th : TypedHeapA} (F : FrontJA st th s σ info buf slot extra mi sl c excaps buffer argsU) + {k : capability.Cap} (hcap : capA st s slot = k) (hne : ∀ p asid va, k ≠ .PageTable p true asid va) : + c = absCapA k := by + subst hcap; exact (F.cap.2.eq_abs hne).symm + +/-- A first extra cap that is neither `Ept` nor `HighVSpace` is no FL4 VSpace root for the spec. -/ +theorem FrontJA.noRootA {th : TypedHeapA} (F : FrontJA st th s σ info buf slot extra mi sl c excaps buffer argsU) + {x0 : U64} (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) + (hS1 : ∀ p l mp' asid va, capAtA (L.mem s) x0 ≠ .Ept p l mp' asid va) + (hHi : ∀ p mp' asid, capAtA (L.mem s) x0 ≠ .HighVSpace p mp' asid) : + ∀ c0 sl0 rest, excaps = (c0, sl0) :: rest → + (∀ q m, c0 ≠ .archObjectCap (.ext (.stage2VSpaceCap q m))) ∧ + ∀ q m, c0 ≠ .archObjectCap (.ext (.highVSpaceCap q m)) := by + intro c0 sl0 rest he + have ex0 : x0 = exAtA extra 0 := okinj (hx0.symm.trans (exAtA_idx0 extra)) + obtain ⟨ps, -, -, -, hrel⟩ := F.extra.2.1 0 (by simp [he]) + simp only [he, List.getElem_cons_zero] at hrel + rw [← ex0] at hrel + have h := hrel.2 + generalize capAtA (L.mem s) x0 = k at h hS1 hHi + have key : ∀ (e : ExtArchCap), c0 = .archObjectCap (.ext e) → absCapA k = .archObjectCap (.ext e) := by + intro e hc0 + rcases h.eq_or_pt with h1 | ⟨_, _, _, h1⟩ + · rw [← h1]; exact hc0 + · rw [hc0] at h1; cases h1 + constructor <;> intro q m hc0 <;> have k1 := key _ hc0 <;> cases k + all_goals + first + | exact hS1 _ _ _ _ _ rfl + | exact hHi _ _ _ rfl + | (simp [absCapA, absFrameE] at k1; done) + | skip + all_goals (rename_i ept _ _; cases ept <;> simp [absCapA, absFrameE] at k1) + +/-! ## The twelve arms -/ + +variable (F : FrontJA st Z.th s σ info buf slot extra mi sl c excaps buffer argsU) +include F + +/-- `VsScopeA.frameMapS1`'s pin. -/ +theorem frameMapS1_pinA (hlab : info.label = fl4_abi.label.ARMPageMap) {fp rt asid0 va0 : U64} + {sz : capability.FrameSize} {dev mp call : Bool} + (hcap : capA st s slot = .Frame fp sz rt dev mp false asid0 va0) + {x0 : U64} (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) + (hS1 : ∀ p l mp' asid va, capAtA (L.mem s) x0 ≠ .Ept p l mp' asid va) + (hHi : ∀ p mp' asid, capAtA (L.mem s) x0 ≠ .HighVSpace p mp' asid) : + ∀ ivp, (decodeFrameInvocation (Z := Unit) (specLabelWord (archL .armPageMap)) (argsU.map UScalar.bv) sl + (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev (mapDataA mp asid0 va0)) excaps).run σ = + ({(.ok (.invokePage ivp), σ)}, False) → + DecPinA call Z.th σ slot (.invokeArchObject (.spec (.invokePage ivp))) := by + intro ivp h + have hc : c = .archObjectCap (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev (mapDataA mp asid0 va0)) := + F.absA hcap (fun _ _ _ e => by cases e) + refine DecPinA.of_archA F.pin hc (F.labelA hlab (v := 49) (l := archL .armPageMap) (by simp [fl4_abi.label.ARMPageMap]) (by decide)) fun cptr => + run_mem_pinA ?_ + rw [dec_pgMap_vsA call _ cptr sl excaps _ _ _ _ _ (F.noRootA hx0 hS1 hHi)] + exact run_liftME_okA h + +/-- `VsScopeA.frameUnmapS1`'s pin. -/ +theorem frameUnmapS1_pinA (hlab : info.label = fl4_abi.label.ARMPageUnmap) {fp rt asid va : U64} + {sz : capability.FrameSize} {dev call : Bool} + (hcap : capA st s slot = .Frame fp sz rt dev true false asid va) + (hS1 : asidRootKind σ (BitVec.ofNat 16 asid.val) = none ∨ + asidRootKind σ (BitVec.ofNat 16 asid.val) = some .stage1) : + DecPinA call Z.th σ slot (.invokeArchObject (.spec (.invokePage (.pageUnmap (.frameCap (specPtr fp) + (vmRightsOfA rt) (frameSizeOfA sz) dev (some (BitVec.ofNat 16 asid.val, va.bv))) sl)))) := by + have hc : c = .archObjectCap (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev + (some (BitVec.ofNat 16 asid.val, va.bv))) := F.absA hcap (fun _ _ _ e => by cases e) + exact DecPinA.of_archA F.pin hc (F.labelA hlab (v := 50) (l := archL .armPageUnmap) (by simp [fl4_abi.label.ARMPageUnmap]) (by decide)) fun cptr => + run_mem_pinA (dec_pgUnmap_specA call _ cptr sl excaps _ _ _ _ _ hS1) + +/-- `VsScopeA.frameUnmapX`'s pin. `hk2`: the provider takes `k := .stage2` for a Stage-2 frame cap. -/ +theorem frameUnmapX_pinA (hlab : info.label = fl4_abi.label.ARMPageUnmap) {fp rt asid va : U64} + {sz : capability.FrameSize} {dev isEpt call : Bool} + (hcap : capA st s slot = .Frame fp sz rt dev true isEpt asid va) {k : ExtRootKind} + (hkA : isEpt = false → asidRootKind σ (BitVec.ofNat 16 asid.val) = some k ∧ k ≠ .stage1) + (hk2 : isEpt = true → k = .stage2) : + DecPinA call Z.th σ slot (.invokeArchObject (.extVSpace (.pageUnmap k (absFrameE isEpt (specPtr fp) + (vmRightsOfA rt) (frameSizeOfA sz) dev (some (BitVec.ofNat 16 asid.val, va.bv))) sl))) := by + have hc : c = .archObjectCap (absFrameE isEpt (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev + (some (BitVec.ofNat 16 asid.val, va.bv))) := F.absA hcap (fun _ _ _ e => by cases e) + exact DecPinA.of_archA F.pin hc (F.labelA hlab (v := 50) (l := archL .armPageUnmap) (by simp [fl4_abi.label.ARMPageUnmap]) (by decide)) fun cptr => + run_mem_pinA (dec_pgUnmapA call _ cptr sl excaps isEpt _ _ _ _ _ _ hkA hk2) + +/-- `VsScopeA.frameUnmapZ`'s pin. -/ +theorem frameUnmapZ_pinA (hlab : info.label = fl4_abi.label.ARMPageUnmap) {fp rt va : U64} + {sz : capability.FrameSize} {dev call : Bool} + (hcap : capA st s slot = .Frame fp sz rt dev true false 0#u64 va) : + DecPinA call Z.th σ slot (.invokeArchObject (.spec (.invokePage (.pageUnmap (.frameCap (specPtr fp) + (vmRightsOfA rt) (frameSizeOfA sz) dev (some (BitVec.ofNat 16 (0#u64 : U64).val, va.bv))) sl)))) := by + have e0 : (0#u64 : U64).val = 0 := rfl + exact frameUnmapS1_pinA F hlab hcap (Or.inl (by unfold asidRootKind; rw [e0, vspaceForAsid_zeroA]; rfl)) + +/-- `VsScopeA.frameUnmapS2Z`'s pin. -/ +theorem frameUnmapS2Z_pinA (hlab : info.label = fl4_abi.label.ARMPageUnmap) {fp rt va : U64} + {sz : capability.FrameSize} {dev call : Bool} + (hcap : capA st s slot = .Frame fp sz rt dev true true 0#u64 va) : + DecPinA call Z.th σ slot (.invokeArchObject (.extVSpace (.pageUnmap .stage2 (.ext (.stage2FrameCap (specPtr fp) + (vmRightsOfA rt) (frameSizeOfA sz) dev (some (BitVec.ofNat 16 (0#u64 : U64).val, va.bv)))) sl))) := by + have hc : c = .archObjectCap (.ext (.stage2FrameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev + (some (BitVec.ofNat 16 (0#u64 : U64).val, va.bv)))) := F.absA hcap (fun _ _ _ e => by cases e) + exact DecPinA.of_archA F.pin hc (F.labelA hlab (v := 50) (l := archL .armPageUnmap) (by simp [fl4_abi.label.ARMPageUnmap]) (by decide)) fun cptr => + run_mem_pinA (dec_pgUnmapS2A call _ cptr sl excaps _ _ _ _ _) + +/-- `VsScopeA.frameUnmapU`'s pin. -/ +theorem frameUnmapU_pinA (hlab : info.label = fl4_abi.label.ARMPageUnmap) {fp rt asid va : U64} + {sz : capability.FrameSize} {dev isEpt call : Bool} + (hcap : capA st s slot = .Frame fp sz rt dev false isEpt asid va) : + DecPinA call Z.th σ slot (.invokeArchObject (.spec (.invokePage (.pageUnmap (.frameCap (specPtr fp) + (vmRightsOfA rt) (frameSizeOfA sz) dev none) sl)))) := by + have he : isEpt = false := by + have hw := F.cap.1 + rw [hcap] at hw + cases isEpt + · rfl + · exact absurd (hw.2.2.1.2.2.2 rfl) Bool.false_ne_true + subst he + have hc : c = .archObjectCap (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev none) := + F.absA hcap (fun _ _ _ e => by cases e) + exact DecPinA.of_archA F.pin hc (F.labelA hlab (v := 50) (l := archL .armPageUnmap) (by simp [fl4_abi.label.ARMPageUnmap]) (by decide)) fun cptr => + run_mem_pinA (dec_pgUnmap_specA call _ cptr sl excaps _ _ _ _ _ trivial) + +/-- `VsScopeA.frameUnmapU`'s `hcs`: the invoked slot's cap after the caller's restart. -/ +theorem frameUnmapU_hcsA (hR : StateRelA Z.th (L.mem s) σ) {fp rt asid va : U64} + {sz : capability.FrameSize} {dev isEpt : Bool} + (hcap : capA st s slot = .Frame fp sz rt dev false isEpt asid va) : + ∀ x, σ.kheap σ.curThread = some (.tcb x) → + getCap (Z := Unit) sl (stUpdA σ σ.curThread x .restart) = + ({(.archObjectCap (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev none), + stUpdA σ σ.curThread x .restart)}, False) := by + intro x hx + have he : isEpt = false := by + have hw := F.cap.1 + rw [hcap] at hw + cases isEpt + · rfl + · exact absurd (hw.2.2.1.2.2.2 rfl) Bool.false_ne_true + subst he + have hc : c = .archObjectCap (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev none) := + F.absA hcap (fun _ _ _ e => by cases e) + obtain ⟨p, hp1, hp⟩ := F.cnode + obtain ⟨cs, hk, hwf, hsl⟩ := hR.cnode p _ hp + obtain ⟨a0, ha, -⟩ := hsl sl.2 rfl + have get : ∀ σ' : State Unit, σ'.kheap (specPtr p) = σ.kheap (specPtr p) → + getCap (Z := Unit) (specPtr p, sl.2) σ' = ({(a0, σ')}, False) := fun σ' e => + getCap_cnodeA (e.trans hk) hwf ha + have esl : sl = (specPtr p, sl.2) := by rw [← hp1] + have ea : a0 = c := by + have h1 := get σ rfl + rw [← esl] at h1 + exact single_fstA (h1.symm.trans F.pin.hcap) + have h2 := get (stUpdA σ σ.curThread x .restart) + ((stUpd_keep (ts := .restart) hx hk (fun _ h => by cases h)).trans hk.symm) + rw [← esl, ea, hc] at h2 + exact h2 + +/-- `VsScopeA.ptMapS1`'s pin. -/ +theorem ptMapS1_pinA (hlab : info.label = fl4_abi.label.ARMPageTableMap) {ptr asid0 va0 : U64} {call : Bool} + (hcap : capA st s slot = .PageTable ptr false asid0 va0) + {x0 : U64} (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) + (hS1 : ∀ p l mp' asid va, capAtA (L.mem s) x0 ≠ .Ept p l mp' asid va) + (hHi : ∀ p mp' asid, capAtA (L.mem s) x0 ≠ .HighVSpace p mp' asid) : + ∀ ivp, (decodePageTableInvocation (Z := Unit) (specLabelWord (archL .armPageTableMap)) (argsU.map UScalar.bv) sl + (.pageTableCap (specPtr ptr) .normalPtT (mapDataA false asid0 va0)) excaps).run σ = + ({(.ok (.invokePageTable ivp), σ)}, False) → + DecPinA call Z.th σ slot (.invokeArchObject (.spec (.invokePageTable ivp))) := by + intro ivp h + have hc : c = .archObjectCap (.pageTableCap (specPtr ptr) .normalPtT none) := + F.absA hcap (fun _ _ _ e => by cases e) + rw [show mapDataA false asid0 va0 = none from rfl] at h + refine DecPinA.of_archA F.pin hc (F.labelA hlab (v := 47) (l := archL .armPageTableMap) (by simp [fl4_abi.label.ARMPageTableMap]) (by decide)) fun cptr => + run_mem_pinA ?_ + rw [dec_ptMap_vsA call _ cptr sl excaps _ (F.noRootA hx0 hS1 hHi)] + exact run_liftME_okA h + +/-- `VsScopeA.ptUnmapU`'s pin. -/ +theorem ptUnmapU_pinA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st R) s a σ m) + (hlab : info.label = fl4_abi.label.ARMPageTableUnmap) {ptr asid va : U64} {call : Bool} + (hcap : capA st s slot = .PageTable ptr false asid va) + (hf : cdt.is_final_capability st s slot = ok true) : + DecPinA call Z.th σ slot (.invokeArchObject (.spec (.invokePageTable + (.pageTableUnmap (.pageTableCap (specPtr ptr) .normalPtT none) sl)))) := by + have hc : c = .archObjectCap (.pageTableCap (specPtr ptr) .normalPtT none) := + F.absA hcap (fun _ _ _ e => by cases e) + rw [F.pin.hslot] at hcap hf + have hfS := J.finPtA F.pin.habs hcap hf + exact DecPinA.of_archA F.pin hc (F.labelA hlab (v := 48) (l := archL .armPageTableUnmap) (by simp [fl4_abi.label.ARMPageTableUnmap]) (by decide)) + fun cptr => run_mem_pinA (dec_ptUnmap_specA call _ cptr sl excaps _ none trivial hfS) + +/-- `VsScopeA.ptUnmapS1`'s pin. [J] `hcan` (module docstring). -/ +theorem ptUnmapS1_pinA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st R) s a σ m) + (hlab : info.label = fl4_abi.label.ARMPageTableUnmap) {ptr asid va : U64} {call : Bool} + (hcap : capA st s slot = .PageTable ptr true asid va) + (hcan : c = absCapA (capA st s slot)) + (hf : cdt.is_final_capability st s slot = ok true) + (hS1 : asidRootKind σ (BitVec.ofNat 16 asid.val) = none ∨ + asidRootKind σ (BitVec.ofNat 16 asid.val) = some .stage1) : + DecPinA call Z.th σ slot (.invokeArchObject (.spec (.invokePageTable (.pageTableUnmap + (.pageTableCap (specPtr ptr) .normalPtT (some (BitVec.ofNat 16 asid.val, va.bv))) sl)))) := by + have hc : c = .archObjectCap (.pageTableCap (specPtr ptr) .normalPtT (some (BitVec.ofNat 16 asid.val, va.bv))) := by + rw [hcan, hcap]; rfl + rw [F.pin.hslot] at hcap hf + have hfS := J.finPtA F.pin.habs hcap hf + exact DecPinA.of_archA F.pin hc (F.labelA hlab (v := 48) (l := archL .armPageTableUnmap) (by simp [fl4_abi.label.ARMPageTableUnmap]) (by decide)) + fun cptr => run_mem_pinA (dec_ptUnmap_specA call _ cptr sl excaps _ _ hS1 hfS) + +/-- `VsScopeA.ptUnmapX`'s pin. [J] `hcan` (module docstring). -/ +theorem ptUnmapX_pinA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st R) s a σ m) + (hlab : info.label = fl4_abi.label.ARMPageTableUnmap) {ptr asid va : U64} {call : Bool} + (hcap : capA st s slot = .PageTable ptr true asid va) + (hcan : c = absCapA (capA st s slot)) + (hf : cdt.is_final_capability st s slot = ok true) {k : ExtRootKind} + (hk : asidRootKind σ (BitVec.ofNat 16 asid.val) = some k) (hk1 : k ≠ .stage1) : + DecPinA call Z.th σ slot (.invokeArchObject (.extVSpace (.tableUnmap k + (.pageTableCap (specPtr ptr) .normalPtT (some (BitVec.ofNat 16 asid.val, va.bv))) sl))) := by + have hc : c = .archObjectCap (.pageTableCap (specPtr ptr) .normalPtT (some (BitVec.ofNat 16 asid.val, va.bv))) := by + rw [hcan, hcap]; rfl + rw [F.pin.hslot] at hcap hf + have hfS := J.finPtA F.pin.habs hcap hf + exact DecPinA.of_archA F.pin hc (F.labelA hlab (v := 48) (l := archL .armPageTableUnmap) (by simp [fl4_abi.label.ARMPageTableUnmap]) (by decide)) + fun cptr => run_mem_pinA (dec_ptUnmapA call _ cptr sl excaps _ _ _ hk hk1 hfS) + +/-- `VsScopeA.frameMapHi`'s pin. -/ +theorem frameMapHi_pinA (hlab : info.label = fl4_abi.label.ARMPageMap) {fp rt asid va : U64} + {sz : capability.FrameSize} {dev mp call : Bool} + (hcap : capA st s slot = .Frame fp sz rt dev mp false asid va) + {x0 p1 asid1 : U64} {mp1 : Bool} (hx : vspace.extra extra 0#usize = ok (some x0)) + (hx0c : capA st s x0 = .HighVSpace p1 mp1 asid1) + {vaddr rightsMask attr : U64} (hlen : msgLenA info buf ≥ 3#u64) + (h0 : msg.syscall_arg st s (L.curW s) buf 0#u64 = ok vaddr) + (h1 : msg.syscall_arg st s (L.curW s) buf 1#u64 = ok rightsMask) + (h2 : msg.syscall_arg st s (L.curW s) buf 2#u64 = ok attr) + {D : Set (Except SyscallError Fl4ArchInvocation × State Unit) × Prop} + (hD : D = (Fl4Spec.NonDetE.liftME Fl4ArchInvocation.extVSpace + (decodeHighPageMap (Z := Unit) sl (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev + (mapDataA mp asid va)) (extRootCapA .high (specPtr p1) (asidDataA mp1 asid1)) + vaddr.bv rightsMask.bv attr.bv)).run σ) : + ∀ iv, (.ok (.extVSpace iv), σ) ∈ D.1 → DecPinA call Z.th σ slot (.invokeArchObject (.extVSpace iv)) := by + intro iv hiv + have hc : c = .archObjectCap (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev (mapDataA mp asid va)) := + F.absA hcap (fun _ _ _ e => by cases e) + -- the three words + have h3 : 3 ≤ argsU.length := by + have := F.len + have : (3#u64 : U64).val ≤ (msgLenA info buf).val := by scalar_tac + have : (3#u64 : U64).val = 3 := rfl + omega + obtain ⟨y0, y1, y2, rest, rfl⟩ : ∃ y0 y1 y2 rest, argsU = y0 :: y1 :: y2 :: rest := by + match argsU, h3 with + | y0 :: y1 :: y2 :: rest, _ => exact ⟨y0, y1, y2, rest, rfl⟩ + have e0 : y0 = vaddr := okinj ((F.args 0#u64 y0 rfl).symm.trans h0) + have e1 : y1 = rightsMask := okinj ((F.args 1#u64 y1 rfl).symm.trans h1) + have e2 : y2 = attr := okinj ((F.args 2#u64 y2 rfl).symm.trans h2) + subst e0 e1 e2 + -- the extra cap + have hread := extra0_eqA (exAtA_idx0 extra) + rw [hx] at hread + have hne : exAtA extra 0 ≠ 0#u64 ∧ x0 = exAtA extra 0 := by + by_cases h : exAtA extra 0 = 0#u64 + · rw [if_pos h] at hread; cases okinj hread + · rw [if_neg h] at hread; exact ⟨h, Option.some.inj (okinj hread)⟩ + obtain ⟨y, erest, hexc⟩ : ∃ y erest, excaps = y :: erest := by + cases h : excaps with + | nil => exact absurd (F.extra.2.2 0 (by simp [h]) (by omega)) hne.1 + | cons y erest => exact ⟨y, erest, rfl⟩ + have hy : y.1 = extRootCapA .high (specPtr p1) (asidDataA mp1 asid1) := by + obtain ⟨ps, -, -, -, hrel⟩ := F.extra.2.1 0 (by simp [hexc]) + simp only [hexc, List.getElem_cons_zero] at hrel + rw [← hne.2, capAtA_eq (st := st), hx0c] at hrel + exact (hrel.2.eq_abs (fun _ _ _ e => by cases e)).symm + refine DecPinA.of_archA F.pin hc (F.labelA hlab (v := 49) (l := archL .armPageMap) (by simp [fl4_abi.label.ARMPageMap]) (by decide)) fun cptr => ?_ + have e : fl4ArchDecodeInvocation (Z := Unit) call (archL .armPageMap) ((y0 :: y1 :: y2 :: rest).map UScalar.bv) + cptr sl (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev (mapDataA mp asid va)) excaps = + Fl4Spec.NonDetE.liftME Fl4ArchInvocation.extVSpace + (decodeHighPageMap (Z := Unit) sl (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev + (mapDataA mp asid va)) (extRootCapA .high (specPtr p1) (asidDataA mp1 asid1)) + y0.bv y1.bv y2.bv) := by + have a1 : ¬ (archL .armPageMap = Fl4Label.ext .armPageDeriveSubframe) := fun h => by cases h + have a2 : ¬ (archL .armPageMap = Fl4Label.ext .armPageTestAndClearAccessed) := fun h => by cases h + unfold fl4ArchDecodeInvocation + dsimp only + rw [if_neg a1, if_neg a2, flushLbl_pgMapA, if_neg Bool.false_ne_true] + rw [if_pos (by rw [hexc]; simp)] + subst hexc + dsimp only [List.getElem?_cons_zero, Fl4SpecA64.the, List.map_cons] + rw [hy] + rfl + rw [e, ← hD] + exact hiv + +omit F + +/-- `VsScopeA.asidAssign`'s pin (from the arm's own spec decode; any free ASID is one outcome). -/ +theorem asidAssign_pinA {ar : machine.Arch A} (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (F : FrontJA st Z.th s σ info buf slot extra mi sl c excaps buffer argsU) + (hlab : info.label = fl4_abi.label.ARMASIDPoolAssign) + (hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM) + {p base : U64} (hpool : capA st s slot = .AsidPool p base) {block call cd fp : Bool} + {dst : CslotPtr} (hd0 : AbsSlotA Z.th dst) + (hex : vspace.extra extra 0#usize = ok (some (cslotAddrA dst))) + {k : ExtRootKind} {root oldAsid oldVa : U64} + (hcap : capA st s (cslotAddrA dst) = assignUnmappedA k root oldAsid oldVa) + (htab : ∀ hi : U64, hi.val = base.val / 512 → L.mem s (atAF (L.ksb s) hi) = p) + (hp0 : p ≠ 0#u64) {i : U64} (hsearch : vspace.decode_loop1 st s base p 0#u64 = ok i) (hfull : i ≠ 512#u64) + (hcov : SlotCov st OS s slot) (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) + (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) : + DecPinA call Z.th σ slot (.invokeArchObject (assignInvA k (specPtr p) dst + (asidOfA (core.num.U64.wrapping_add base i)))) := by + have hsl := F.pin.hslot + rw [hsl] at hpool hcov + obtain ⟨-, -, -, -, -, -, -, -, -, hmem, -⟩ := JSyscallA.asidAssignA (J := J) (hA := hA) (hM := hM) (hPl := hPl) + (hfm := hfm) (hnvm := hnvm) (cte := sl) (hab := F.pin.habs) (p := p) (base := base) (hpool := hpool) + (length := msgLenA info buf) (buf := buf) (block := block) (call := call) (cd := cd) (fp := fp) (ex := extra) + (dst := dst) (hd := hd0) (hex := hex) (k := k) (root := root) (oldAsid := oldAsid) (oldVa := oldVa) + (hcap := hcap) (htab := htab) (hp0 := hp0) (i := i) (hsearch := hsearch) (hfull := hfull) (hcov := hcov) + (hext := hext) (hSC := hSC) + have hc : c = .archObjectCap (.asidPoolCap (specPtr p) (asidOfA base)) := by + rw [← hsl] at hpool + exact F.absA hpool (fun _ _ _ e => by cases e) + -- the first extra cap is the unassigned root + have hread := extra0_eqA (exAtA_idx0 extra) + rw [hex] at hread + have hne : exAtA extra 0 ≠ 0#u64 ∧ cslotAddrA dst = exAtA extra 0 := by + by_cases h : exAtA extra 0 = 0#u64 + · rw [if_pos h] at hread; cases okinj hread + · rw [if_neg h] at hread; exact ⟨h, Option.some.inj (okinj hread)⟩ + obtain ⟨⟨c0, sl0⟩, erest, hexc⟩ : ∃ y erest, excaps = y :: erest := by + cases h : excaps with + | nil => exact absurd (F.extra.2.2 0 (by simp [h]) (by omega)) hne.1 + | cons y erest => exact ⟨y, erest, rfl⟩ + obtain ⟨ps, hth, hsp, hxa, hrel⟩ := F.extra.2.1 0 (by simp [hexc]) + simp only [hexc, List.getElem_cons_zero] at hth hsp hxa hrel + have hP := J.j.1.place + have esl : sl0 = dst := cslotAddrA_inj hP ⟨ps, hsp, Or.inl hth⟩ hd0 (hxa.symm.trans hne.2.symm) + have ec0 : c0 = .archObjectCap (assignRootCapA k (specPtr root) none) := by + rw [← hne.2, capAtA_eq (st := st), hcap] at hrel + rw [← (hrel.2.eq_abs (by cases k <;> simp [assignUnmappedA]))] + cases k <;> rfl + refine DecPinA.of_archA F.pin hc (F.labelA hlab (lbl := fl4_abi.label.ARMASIDPoolAssign) + (Fl4ProofsA64.P0.Consts.fl4_abi.label.ARMASIDPoolAssign.val_eq) (l := archL .armASIDPoolAssign) (by decide)) + fun cptr => ?_ + rw [hexc, ec0, esl] + exact hmem call _ cptr sl erest + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3PreSchedA.lean b/proofs/Fl4ProofsA64/P3/Stage3PreSchedA.lean new file mode 100644 index 000000000..366ba462a --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3PreSchedA.lean @@ -0,0 +1,408 @@ +/- +P3 (aarch64, A64-P3 line RecvPreA): **`maybe_return_sc` under a pending switch**. + +When the current thread returns its SC, `reschedule_required` first enqueues a pending (schedulable) switch target +and then leaves `choose_new_thread`. `preschedA σ` is `σ` after that enqueue; it is `σ` when no switch is pending, +and in both cases has no switch pending. So `maybe_return_sc` is `unbWaitA` at `preschedA σ`, and the arms that +unbind run their record argument from `preschedA σ`. + +* `preschedA`, `preschedA_noswitchA`, `preschedA_cases` (what it changes). +* **`maybeReturnSc_unbSwA`**: `maybe_return_sc` unbinds (`maybeReturnSc_unbA` without "no switch pending"). +* `ActReadyA.congr`, `preschedA_sikaA`, `preschedA_schedValidA`. + +Needs `ActReadyA` (the pending target's refill is sufficient and ready; from `SchedSRA.actReady`). No premise. New +module. +-/ +import Fl4ProofsA64.P3.Stage3RecvWaitUnbA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched + Fl4Spec Fl4SpecA64 + +noncomputable section + +/-- The ready queues with `u` enqueued at its domain and priority. -/ +def enqRqA (σ : State Unit) (u : ObjRef) (x : Tcb) : Domain → Priority → ReadyQueue := + fun d p => if d = x.tcbDomain ∧ p = x.tcbPriority then tcbSchedEnqueue u (σ.readyQueues d p) + else σ.readyQueues d p + +/-- The state after `reschedule_required`'s enqueue of a pending switch target, with `choose_new_thread` left; +`σ` itself when no switch is pending. -/ +def preschedA (σ : State Unit) : State Unit := + match σ.schedulerAction with + | .switchThread u => + match σ.kheap u with + | some (.tcb x) => + if schedulable u σ = true then + { σ with readyQueues := enqRqA σ u x, schedulerAction := .chooseNewThread } + else { σ with schedulerAction := .chooseNewThread } + | _ => { σ with schedulerAction := .chooseNewThread } + | _ => σ + +theorem preschedA_noswitchA {σ : State Unit} (h : ∀ u, σ.schedulerAction ≠ .switchThread u) : preschedA σ = σ := by + unfold preschedA + cases e : σ.schedulerAction with + | switchThread u => exact absurd e (h u) + | _ => rfl + +theorem preschedA_notschedA {σ : State Unit} {u : ObjRef} (e : σ.schedulerAction = .switchThread u) + (hs : ¬ schedulable u σ = true) : preschedA σ = { σ with schedulerAction := .chooseNewThread } := by + unfold preschedA + simp only [e] + split + · rw [if_neg hs] + · rfl + +/-- `preschedA` changes only the ready queues (by the pending, schedulable target) and the scheduler action. -/ +theorem preschedA_casesA (σ : State Unit) : + ∃ q a, preschedA σ = { σ with readyQueues := q, schedulerAction := a } ∧ + (∀ d p y, y ∈ q d p → y ∈ σ.readyQueues d p ∨ (σ.schedulerAction = .switchThread y ∧ schedulable y σ = true)) ∧ + (∀ u, a ≠ .switchThread u) ∧ (a = .resumeCurThread → σ.schedulerAction = .resumeCurThread) := by + by_cases h : ∃ u, σ.schedulerAction = .switchThread u + swap + · refine ⟨σ.readyQueues, σ.schedulerAction, ?_, fun _ _ _ h => Or.inl h, fun u e => h ⟨u, e⟩, fun e => e⟩ + rw [preschedA_noswitchA (fun u e => h ⟨u, e⟩)] + obtain ⟨u, e⟩ := h + by_cases hs : schedulable u σ = true + · obtain ⟨-, -, x, hx, -⟩ := schedulable_actA hs + have eq : preschedA σ = { σ with readyQueues := enqRqA σ u x, schedulerAction := .chooseNewThread } := by + unfold preschedA + simp only [e, hx, hs, ↓reduceIte] + refine ⟨_, _, eq, fun d p y hy => ?_, (by intro u c; cases c), fun h => by cases h⟩ + unfold enqRqA at hy + split_ifs at hy + · unfold tcbSchedEnqueue at hy + split_ifs at hy + all_goals first + | exact Or.inl hy + | (rcases List.mem_cons.1 hy with rfl | hy + exacts [Or.inr ⟨e, hs⟩, Or.inl hy]) + · exact Or.inl hy + · exact ⟨σ.readyQueues, .chooseNewThread, preschedA_notschedA e hs, fun _ _ _ h => Or.inl h, + (by intro u c; cases c), fun h => by cases h⟩ + +/-- `reschedule_required` is `preschedA` with `choose_new_thread` left. -/ +theorem rescheduleRequired_preschedA {σ : State Unit} (hr : ActReadyA σ) : + rescheduleRequired (Z := Unit) σ = ({((), { preschedA σ with schedulerAction := .chooseNewThread })}, False) := by + cases e : σ.schedulerAction with + | resumeCurThread => + rw [preschedA_noswitchA (fun u c => by rw [e] at c; cases c)] + exact rescheduleRequired_noswitchA (fun u c => by rw [e] at c; cases c) + | chooseNewThread => + rw [preschedA_noswitchA (fun u c => by rw [e] at c; cases c)] + exact rescheduleRequired_noswitchA (fun u c => by rw [e] at c; cases c) + | switchThread u => + by_cases hs : schedulable u σ = true + · obtain ⟨-, -, x, hx, -⟩ := schedulable_actA hs + have he : tcbSchedAction (Z := Unit) tcbSchedEnqueue u σ = ({((), { σ with + readyQueues := enqRqA σ u x })}, False) := by + unfold tcbSchedAction + rw [nd_bind_oneSc (threadGet_eqSc _ hx), nd_bind_oneSc (threadGet_eqSc _ hx)] + unfold getTcbQueue + rw [nd_bind_oneSc (nd_bind_oneSc (show (Fl4Spec.NonDet.gets fun s : State Unit => s.readyQueues) σ = + ({(σ.readyQueues, σ)}, False) from rfl) _), nd_pure_bindSc] + have q : enqRqA σ u x = fun d p => if d = x.tcbDomain ∧ p = x.tcbPriority + then tcbSchedEnqueue u (σ.readyQueues x.tcbDomain x.tcbPriority) else σ.readyQueues d p := by + funext d p + unfold enqRqA + by_cases c : d = x.tcbDomain ∧ p = x.tcbPriority + · rw [if_pos c, if_pos c, c.1, c.2] + · rw [if_neg c, if_neg c] + rw [q] + rfl + rw [rescheduleRequired_schedA e hs hr he] + unfold preschedA + simp only [e, hx, hs, ↓reduceIte] + · have hs' : schedulable u σ = false := by simpa using hs + rw [rescheduleRequired_notschedA e hs', preschedA_notschedA e hs] + +theorem readScRefillSufficient_of_getA {σ : State Unit} {p : ObjRef} {u : Ticks} {b : Bool} + (hr : getScRefillSufficient (Z := Unit) p u σ = ({(b, σ)}, False)) : + readScRefillSufficient (Z := Unit) p u σ = some b := by + cases c : readScRefillSufficient (Z := Unit) p u σ with + | none => + simp [getScRefillSufficient, Fl4Spec.NonDet.getsThe, c, Fl4Spec.NonDet.assertOpt] at hr + exact absurd (congrArg Prod.snd hr) (by simp [Fl4Spec.NonDet.gets, Bind.bind, Fl4Spec.NonDet.bind, + Fl4Spec.NonDet.fail]) + | some b' => + have : getScRefillSufficient (Z := Unit) p u σ = ({(b', σ)}, False) := by + simp [getScRefillSufficient, Fl4Spec.NonDet.getsThe, c, Fl4Spec.NonDet.assertOpt]; rfl + rw [this] at hr + have := Set.singleton_eq_singleton_iff.1 (Prod.mk.inj hr).1 + rw [(Prod.mk.inj this).1] + +/-- A successful refill-ready read of an SC depends only on the object and the clock. -/ +theorem getScRefillReady_congrA {σ σ' : State Unit} {p : ObjRef} (h : σ'.kheap p = σ.kheap p) + (ht : σ'.curTime = σ.curTime) (hr : getScRefillReady (Z := Unit) p σ = ({(true, σ)}, False)) : + getScRefillReady (Z := Unit) p σ' = ({(true, σ')}, False) := by + have e : readScRefillReady (Z := Unit) p σ' = readScRefillReady (Z := Unit) p σ := by + have h1 : readSchedContext (Z := Unit) p σ' = readSchedContext (Z := Unit) p σ := by + show Option.bind (readObject p σ') _ = Option.bind (readObject p σ) _ + have h2 : readObject p σ' = readObject p σ := by simp [readObject, h] + rw [h2]; exact congrArg _ (funext fun k => by cases k <;> rfl) + have h3 : readRefillHead (Z := Unit) p σ' = readRefillHead (Z := Unit) p σ := by + show Option.bind (readSchedContext p σ') _ = Option.bind (readSchedContext p σ) _ + rw [h1] + refine congrArg _ (funext fun sc => ?_) + show Option.bind (Fl4Spec.Lookup.oassert (sc.scRefills ≠ []) σ') _ = + Option.bind (Fl4Spec.Lookup.oassert (sc.scRefills ≠ []) σ) _ + unfold Fl4Spec.Lookup.oassert + split_ifs <;> rfl + show Option.bind (readRefillHead p σ') (fun h => Option.bind (Fl4Spec.Lookup.asks + (fun s : State Unit => s.curTime) σ') (fun ct => Fl4Spec.Lookup.oreturn (refillReady ct h) σ')) = + Option.bind (readRefillHead p σ) (fun h => Option.bind (Fl4Spec.Lookup.asks + (fun s : State Unit => s.curTime) σ) (fun ct => Fl4Spec.Lookup.oreturn (refillReady ct h) σ)) + rw [h3, show Fl4Spec.Lookup.asks (fun s : State Unit => s.curTime) σ' = + Fl4Spec.Lookup.asks (fun s : State Unit => s.curTime) σ from congrArg some ht] + rfl + have v : readScRefillReady (Z := Unit) p σ = some true := by + cases c : readScRefillReady (Z := Unit) p σ with + | none => + simp [getScRefillReady, Fl4Spec.NonDet.getsThe, c, Fl4Spec.NonDet.assertOpt] at hr + exact absurd (congrArg Prod.snd hr) (by simp [Fl4Spec.NonDet.gets, Bind.bind, Fl4Spec.NonDet.bind, + Fl4Spec.NonDet.fail]) + | some b => + have : getScRefillReady (Z := Unit) p σ = ({(b, σ)}, False) := by + simp [getScRefillReady, Fl4Spec.NonDet.getsThe, c, Fl4Spec.NonDet.assertOpt]; rfl + rw [this] at hr + have := Set.singleton_eq_singleton_iff.1 (Prod.mk.inj hr).1 + rw [(Prod.mk.inj this).1] + simp [getScRefillReady, Fl4Spec.NonDet.getsThe, e, v, Fl4Spec.NonDet.assertOpt]; rfl + +/-- A successful refill-sufficient read of an SC depends only on the object. -/ +theorem getScRefillSufficient_congrA {σ σ' : State Unit} {p : ObjRef} {u : Ticks} (h : σ'.kheap p = σ.kheap p) + (hr : getScRefillSufficient (Z := Unit) p u σ = ({(true, σ)}, False)) : + getScRefillSufficient (Z := Unit) p u σ' = ({(true, σ')}, False) := by + have e : readScRefillSufficient (Z := Unit) p u σ' = readScRefillSufficient (Z := Unit) p u σ := by + have h1 : readSchedContext (Z := Unit) p σ' = readSchedContext (Z := Unit) p σ := by + show Option.bind (readObject p σ') _ = Option.bind (readObject p σ) _ + have h2 : readObject p σ' = readObject p σ := by simp [readObject, h] + rw [h2]; exact congrArg _ (funext fun k => by cases k <;> rfl) + have h3 : readRefillHead (Z := Unit) p σ' = readRefillHead (Z := Unit) p σ := by + show Option.bind (readSchedContext p σ') _ = Option.bind (readSchedContext p σ) _ + rw [h1] + refine congrArg _ (funext fun sc => ?_) + show Option.bind (Fl4Spec.Lookup.oassert (sc.scRefills ≠ []) σ') _ = + Option.bind (Fl4Spec.Lookup.oassert (sc.scRefills ≠ []) σ) _ + unfold Fl4Spec.Lookup.oassert + split_ifs <;> rfl + show Option.bind (readRefillHead p σ') (fun h => Fl4Spec.Lookup.oreturn (refillSufficient u h) σ') = + Option.bind (readRefillHead p σ) (fun h => Fl4Spec.Lookup.oreturn (refillSufficient u h) σ) + rw [h3]; rfl + have v := readScRefillSufficient_of_getA hr + simp [getScRefillSufficient, Fl4Spec.NonDet.getsThe, e, v, Fl4Spec.NonDet.assertOpt]; rfl + +/-- `ActReadyA` across a step keeping the pending target's TCB, its SC object, the clock and the release queue. -/ +theorem ActReadyA.congr {σ σ' : State Unit} (h : ActReadyA σ) (ha : σ'.schedulerAction = σ.schedulerAction) + (ht : σ'.curTime = σ.curTime) (hr : σ'.releaseQueue = σ.releaseQueue) + (hk : ∀ u, σ.schedulerAction = .switchThread u → σ'.kheap u = σ.kheap u ∧ + ∀ x sc, σ.kheap u = some (.tcb x) → x.tcbSchedContext = some sc → σ'.kheap sc = σ.kheap sc) : + ActReadyA σ' := by + intro t e hs + rw [ha] at e + obtain ⟨k1, k2⟩ := hk t e + have hs0 : schedulable t σ = true := by + obtain ⟨⟨x, sc, hx, hxs, y, n, hy, act⟩, hnr, z, hz, hrun⟩ := schedulable_actA hs + rw [hx] at hz; cases hz + rw [k1] at hx + rw [k2 x sc hx hxs] at hy + refine schedulable_ofA hx hrun hxs hy ?_ (hr ▸ hnr) + simpa [scActive, activeSc] using act + obtain ⟨x, scp, hx, hxs, h1, h2⟩ := h t e hs0 + have es := k2 x scp hx hxs + exact ⟨x, scp, k1 ▸ hx, hxs, getScRefillSufficient_congrA es h1, getScRefillReady_congrA es ht h2⟩ + +/-- **`maybe_return_sc` unbinds, a switch to another thread possibly pending** (`maybeReturnSc_unbA` at +`preschedA σ`). -/ +theorem maybeReturnSc_unbSwA {σ : State Unit} {n t sc : ObjRef} {xn : Notification} {xt : Tcb} {xs : SchedContext} + {ns : Nat} (hkn : σ.kheap n = some (.notification xn)) (hkt : σ.kheap t = some (.tcb xt)) + (hsn : xn.ntfnSc = some sc) (hst : xt.tcbSchedContext = some sc) + (hks : σ.kheap sc = some (.schedContext xs ns)) (hNT : sc ≠ t) (hct : t = σ.curThread) + (hr : ActReadyA σ) + (hu : ∀ u, σ.schedulerAction = .switchThread u → u ≠ t ∧ + ∀ x, σ.kheap u = some (.tcb x) → x.tcbSchedContext ≠ some sc) : + maybeReturnSc (Z := Unit) n t σ = + ({((), unbWaitA (preschedA σ) t sc xs ns xt)}, False) := by + have hstep : maybeReturnSc (Z := Unit) n t σ = rescheduleRequired (Z := Unit) (unbStA σ sc t xs ns xt) := by + unfold maybeReturnSc getTcbObjRef + rw [nd_bind_oneSc (getNtfnSc_eqSc hkn), nd_bind_oneSc (threadGet_eqSc _ hkt), hsn, hst] + rw [Fl4Spec.NonDet.when, if_pos ⟨rfl, by simp⟩] + rw [nd_bind_oneSc (show Fl4Spec.NonDet.assertOpt (σ := State Unit) (some sc) σ = ({(sc, σ)}, False) from rfl)] + rw [nd_bind_oneSc (threadSet_eqSc _ hkt)] + have hks1 : (updKSc σ t (.tcb (tcbSchedContextUpdate (fun _ => none) xt))).kheap sc = + some (.schedContext xs ns) := by simp [updKSc, hNT, hks] + show (updateSchedContext sc _ >>= _) _ = _ + rw [nd_bind_oneSc (Fl4ProofsA64.P3.Sched.updateSchedContext_eqA hks1 _), nd_gets_bindSc] + show Fl4Spec.NonDet.when (t = (unbStA σ sc t xs ns xt).curThread) rescheduleRequired + (unbStA σ sc t xs ns xt) = _ + rw [Fl4Spec.NonDet.when, if_pos (show t = (unbStA σ sc t xs ns xt).curThread from hct)] + have keep : ∀ p, p ≠ t → p ≠ sc → (unbStA σ sc t xs ns xt).kheap p = σ.kheap p := fun p h1 h2 => by + rw [unbStA_kheap hNT, if_neg h1, if_neg h2] + have hpend : ∀ u, σ.schedulerAction = .switchThread u → + (unbStA σ sc t xs ns xt).kheap u = σ.kheap u ∨ schedulable u σ = false := fun u e => by + obtain ⟨u1, u2⟩ := hu u e + by_cases c : u = sc + · refine Or.inr (by + cases hs : schedulable u σ + · rfl + · obtain ⟨-, -, x, hx, -⟩ := schedulable_actA hs + rw [c, hks] at hx; cases hx) + · exact Or.inl (keep u u1 c) + have sched_eq : ∀ u, σ.schedulerAction = .switchThread u → + schedulable u (unbStA σ sc t xs ns xt) = schedulable u σ := fun u e => by + obtain ⟨u1, u2⟩ := hu u e + have iff : schedulable u (unbStA σ sc t xs ns xt) = true ↔ schedulable u σ = true := by + constructor + · intro hs + obtain ⟨⟨x, c, hx, hxs, y, k, hy, act⟩, hnr, z, hz, hrun⟩ := schedulable_actA hs + rw [hx] at hz; cases hz + have e1 : u ≠ sc := fun h => by + rw [h, unbStA_kheap hNT, if_neg hNT, if_pos rfl] at hx; cases hx + rw [keep u u1 e1] at hx + have e2 : c ≠ sc := fun h => u2 x hx (h ▸ hxs) + have e3 : c ≠ t := fun h => by + rw [h, unbStA_kheap hNT, if_pos rfl] at hy; cases hy + rw [keep c e3 e2] at hy + refine schedulable_ofA hx hrun hxs hy ?_ hnr + simpa [scActive, activeSc] using act + · intro hs + obtain ⟨⟨x, c, hx, hxs, y, k, hy, act⟩, hnr, z, hz, hrun⟩ := schedulable_actA hs + rw [hx] at hz; cases hz + have e1 : u ≠ sc := fun h => by rw [h, hks] at hx; cases hx + have e2 : c ≠ sc := fun h => u2 x hx (h ▸ hxs) + have e3 : c ≠ t := fun h => by rw [h, hkt] at hy; cases hy + refine schedulable_ofA (by rw [keep u u1 e1]; exact hx) hrun hxs (by rw [keep c e3 e2]; exact hy) ?_ hnr + simpa [scActive, activeSc] using act + exact Bool.eq_iff_iff.2 iff + have hr' : ActReadyA (unbStA σ sc t xs ns xt) := by + intro u e hs + have e' : σ.schedulerAction = .switchThread u := e + obtain ⟨u1, u2⟩ := hu u e' + rw [sched_eq u e'] at hs + obtain ⟨x, scp, hx, hxs, h1, h2⟩ := hr u e' hs + have c1 : u ≠ sc := fun h => by rw [h, hks] at hx; cases hx + have c2 : scp ≠ sc := fun h => u2 x hx (h ▸ hxs) + have c3 : scp ≠ t := fun h => by + rw [h] at h1 + have v := readScRefillSufficient_of_getA h1 + have h2 : readSchedContext (Z := Unit) t σ = none := by + show Option.bind (readObject t σ) _ = none + simp [readObject, hkt, Fl4Spec.Lookup.ofail] + have : readScRefillSufficient (Z := Unit) t 0 σ = none := by + show Option.bind (Option.bind (readSchedContext t σ) _) _ = none + rw [h2]; rfl + rw [this] at v; cases v + have es : (unbStA σ sc t xs ns xt).kheap scp = σ.kheap scp := keep scp c3 c2 + exact ⟨x, scp, (keep u u1 c1).trans hx, hxs, getScRefillSufficient_congrA es h1, + getScRefillReady_congrA es rfl h2⟩ + rw [hstep, rescheduleRequired_preschedA hr'] + refine congrArg (fun σ2 : State Unit => (({((), σ2)} : Set (Unit × State Unit)), False)) ?_ + show ({ preschedA (unbStA σ sc t xs ns xt) with schedulerAction := .chooseNewThread } : State Unit) = + unbWaitA (preschedA σ) t sc xs ns xt + have flds : ∀ (π : State Unit), ({ unbStA π sc t xs ns xt with schedulerAction := .chooseNewThread } : State Unit) = + unbWaitA π t sc xs ns xt := fun _ => rfl + rw [← flds (preschedA σ)] + cases e : σ.schedulerAction with + | resumeCurThread => + have ns0 : ∀ u, σ.schedulerAction ≠ .switchThread u := fun u c => by rw [e] at c; cases c + rw [preschedA_noswitchA ns0, preschedA_noswitchA (σ := unbStA σ sc t xs ns xt) (fun u => ns0 u)] + | chooseNewThread => + have ns0 : ∀ u, σ.schedulerAction ≠ .switchThread u := fun u c => by rw [e] at c; cases c + rw [preschedA_noswitchA ns0, preschedA_noswitchA (σ := unbStA σ sc t xs ns xt) (fun u => ns0 u)] + | switchThread u => + have e1 : (unbStA σ sc t xs ns xt).schedulerAction = .switchThread u := e + have se := sched_eq u e + by_cases hs : schedulable u σ = true + · obtain ⟨-, -, x, hx, -⟩ := schedulable_actA hs + have c1 : u ≠ sc := fun h => by rw [h, hks] at hx; cases hx + have hx' : (unbStA σ sc t xs ns xt).kheap u = some (.tcb x) := (keep u (hu u e).1 c1).trans hx + unfold preschedA + simp only [e, e1, hx, hx', se, hs, ↓reduceIte] + rfl + · have hs' : ¬ schedulable u (unbStA σ sc t xs ns xt) = true := by rw [se]; exact hs + rw [preschedA_notschedA e hs, preschedA_notschedA e1 hs'] + rfl + +/-- `preschedA` is `SIKA`. -/ +theorem preschedA_sikaA {i : ObjRef} {M N : ObjRef → Prop} {don : Bool} {σ : State Unit} (hr : ActReadyA σ) : + SIKA i M N don σ (preschedA σ) := by + by_cases hp : ∃ u, σ.schedulerAction = .switchThread u + swap + · rw [preschedA_noswitchA (fun u e => hp ⟨u, e⟩)] + exact SIKA.refl σ + obtain ⟨u, hu⟩ := hp + have e : { preschedA σ with schedulerAction := .chooseNewThread } = preschedA σ := by + obtain ⟨q, a, h, -, -, -⟩ := preschedA_casesA σ + have : (preschedA σ).schedulerAction = .chooseNewThread := by + unfold preschedA + simp only [hu] + split + · split_ifs <;> rfl + · rfl + rw [← this] + have h := rescheduleRequired_sikA (i := i) (M := M) (N := N) (don := don) σ ((), preschedA σ) + (by rw [rescheduleRequired_preschedA hr, e]; exact Set.mem_singleton _) + exact h + +/-- `SchedValidA` across `preschedA`. -/ +theorem preschedA_schedValidA {σ : State Unit} (V : SchedValidA σ) : SchedValidA (preschedA σ) := by + obtain ⟨q, a, h, hq, -, ha⟩ := preschedA_casesA σ + rw [h] + have act : ∀ p, ScActiveAtA σ p → ScActiveAtA { σ with readyQueues := q, schedulerAction := a } p := fun _ h => h + refine ⟨fun d p y hy => ?_, fun e1 e2 => ?_, fun u x sc hx hb hs => act _ (V.blk u x sc hx hb hs), + fun u hu => V.rls u hu⟩ + · rcases hq d p y hy with hm | ⟨-, hs⟩ + · exact V.rq d p y hm + · obtain ⟨⟨x, sc, hx, hxs, k, n, hk, act'⟩, hnr, -⟩ := schedulable_actA hs + exact ⟨⟨x, sc, hx, hxs, k, n, hk, act'⟩, hnr⟩ + · exact V.act (ha e1) e2 + +/-- The fields `preschedA` keeps. -/ +structure PreSchedKeepA (σ σ' : State Unit) : Prop where + kh : σ'.kheap = σ.kheap + cur : σ'.curThread = σ.curThread + idle : σ'.idleThread = σ.idleThread + csc : σ'.curSc = σ.curSc + time : σ'.curTime = σ.curTime + con : σ'.consumedTime = σ.consumedTime + rls : σ'.releaseQueue = σ.releaseQueue + mach : σ'.machineState = σ.machineState + noSw : ∀ u, σ'.schedulerAction ≠ .switchThread u + spec : SpecSchedA σ σ' + qk : QKeepA σ σ' + notin : ∀ t, (∀ d p, t ∉ σ.readyQueues d p) → σ.schedulerAction ≠ .switchThread t → + ∀ d p, t ∉ σ'.readyQueues d p + +theorem preschedA_keepA (σ : State Unit) : PreSchedKeepA σ (preschedA σ) := by + obtain ⟨q, a, h, hq, hna, -⟩ := preschedA_casesA σ + rw [h] + refine ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, hna, ⟨fun _ => Or.inl rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩, + QKeepA.of_kheap rfl ⟨rfl, rfl, rfl, rfl⟩, fun t h1 h2 d p hm => ?_⟩ + rcases hq d p t hm with c | ⟨c, -⟩ + exacts [h1 d p c, h2 c] + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {AR : A → HwRootA → Prop} {Z : XSetsA} + {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} + {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J5 J7 : JSlotA S} {s : S} + {a : A} {σ : State Unit} {m : McState} + +/-- A pending switch target is not the current thread and does not hold the current thread's SC. -/ +theorem RecvPreA.pendingSc (J : RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {xt : Tcb} {sc : ObjRef} (hkt : σ.kheap (specPtr (L.curW s)) = some (.tcb xt)) + (hst : xt.tcbSchedContext = some sc) : + ∀ u, σ.schedulerAction = .switchThread u → u ≠ specPtr (L.curW s) ∧ + ∀ x, σ.kheap u = some (.tcb x) → x.tcbSchedContext ≠ some sc := fun u e => by + have u1 := (J.pending e).1 + refine ⟨u1, fun x hx h => ?_⟩ + obtain ⟨y1, j, hy1, hb1⟩ := J.sr.inv.back u x sc hx h + obtain ⟨y2, j2, hy2, hb2⟩ := J.sr.inv.back _ xt sc hkt hst + rw [hy2] at hy1; cases hy1 + exact u1 (Option.some.inj (hb1.symm.trans hb2)) + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3PreemptA.lean b/proofs/Fl4ProofsA64/P3/Stage3PreemptA.lean new file mode 100644 index 000000000..cfbbab12a --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3PreemptA.lean @@ -0,0 +1,229 @@ +/- +P3 (aarch64, A64-P3 Stage 3, J8): **the preemption point refinement**, generic over the loop invariant `R`. +-/ +import Fl4ProofsA64.P3.UtFrA +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4ProofsA64.P1.W7.R.ST + +noncomputable section + +/-! ## Membership in `ExceptT` runs -/ + +section runE +variable {σ ε α β : Type} + +theorem runE_bindA {m : NonDetE σ ε α} {f : α → NonDetE σ ε β} {s s1 : σ} {a : α} {z : Except ε β × σ} + (h1 : (Except.ok a, s1) ∈ (m.run s).1) (h2 : z ∈ ((f a).run s1).1) : z ∈ ((m >>= f).run s).1 := by + rw [ExceptT.run_bind] + exact ⟨_, h1, h2⟩ + +theorem runE_bind_errA {m : NonDetE σ ε α} {f : α → NonDetE σ ε β} {s s1 : σ} {e : ε} + (h1 : (Except.error e, s1) ∈ (m.run s).1) : (Except.error e, s1) ∈ ((m >>= f).run s).1 := by + rw [ExceptT.run_bind] + exact ⟨_, h1, rfl⟩ + +theorem runE_liftA {m : NonDet σ α} {s s1 : σ} {a : α} (h : (a, s1) ∈ (m s).1) : + (Except.ok a, s1) ∈ ((NonDetE.liftE m : NonDetE σ ε α).run s).1 := + ⟨_, h, rfl⟩ + +theorem runE_pureA {s : σ} (a : α) : (Except.ok a, s) ∈ ((pure a : NonDetE σ ε α).run s).1 := rfl + +theorem runE_throwA {s : σ} (e : ε) : (Except.error e, s) ∈ ((throw e : NonDetE σ ε α).run s).1 := rfl + +end runE + +theorem doExt_unitA (eop : DetExtMonad Unit) (σ : State Unit) : ((), σ) ∈ (doExtendedOp (Z := Unit) eop σ).1 := by + simp [doExtendedOp, StateExt.wrapExtOp, NonDet.mkEf, NonDet.selectF, NonDet.modify, NonDet.get, NonDet.pure, + Bind.bind, NonDet.bind] + +theorem choice_unitA (c : DetExtMonad Bool) (σ : State Unit) (b : Bool) : + ∃ σ' : State Unit, σ' = σ ∧ True := ⟨σ, rfl, trivial⟩ + +theorem pp_spec_shortA (σ : State Unit) : (Except.ok (), σ) ∈ ((preemptionPoint (Z := Unit)).run σ).1 := by + unfold preemptionPoint + refine runE_bindA (a := ()) (s1 := σ) (runE_liftA (doExt_unitA _ σ)) ?_ + unfold orChoiceE + refine runE_bindA (a := σ) (s1 := σ) (runE_liftA rfl) ?_ + refine runE_bindA (a := (false, σ)) (s1 := σ) (runE_liftA ?_) ?_ + · simp [StateExt.wrapExtBool, NonDet.select, NonDet.selectF, NonDet.mkEf, NonDet.mkEf] + · exact runE_pureA () + +/-- The long branch of the spec preemption point: after the timestamp (`σ1`) and the interrupt poll (`σ2`), it throws +exactly when an interrupt is pending, the domain is expired or the current SC is inactive or insufficient. -/ +theorem pp_spec_longA {σ σ1 σ2 : State Unit} {o : Option Irq} {act : Bool} + (h1 : ((), σ1) ∈ (updateTimeStamp (Z := Unit) σ).1) + (h2 : (o, σ2) ∈ (doMachineOp (getActiveIRQ true) σ1).1) + (hact : readScActive (Z := Unit) σ2.curSc σ2 = some act) : + ((¬ o = none ∨ isCurDomainExpired σ2 = true ∨ + ¬(act = true ∧ readScRefillSufficient (Z := Unit) σ2.curSc σ2.consumedTime σ2 = some true)) → + (Except.error (), σ2) ∈ ((preemptionPoint (Z := Unit)).run σ).1) ∧ + (¬ (¬ o = none ∨ isCurDomainExpired σ2 = true ∨ + ¬(act = true ∧ readScRefillSufficient (Z := Unit) σ2.curSc σ2.consumedTime σ2 = some true)) → + (Except.ok (), σ2) ∈ ((preemptionPoint (Z := Unit)).run σ).1) := by + have key : ∀ z : Except Unit Unit × State Unit, + (z ∈ ((NonDetE.whenE (¬ o = none ∨ isCurDomainExpired σ2 = true ∨ + ¬(act = true ∧ readScRefillSufficient (Z := Unit) σ2.curSc σ2.consumedTime σ2 = some true)) + (NonDetE.throwError ()) : NonDetE (State Unit) Unit Unit).run σ2).1 → z = (Except.error (), σ2) ∨ z = (Except.ok (), σ2)) → + True := fun _ _ => trivial + have chain : ∀ z : Except Unit Unit × State Unit, + z ∈ (((NonDetE.whenE (¬ o = none ∨ isCurDomainExpired σ2 = true ∨ + ¬(act = true ∧ readScRefillSufficient (Z := Unit) σ2.curSc σ2.consumedTime σ2 = some true)) + (NonDetE.throwError ()) : NonDetE (State Unit) Unit Unit)).run σ2).1 → + z ∈ ((preemptionPoint (Z := Unit)).run σ).1 := by + intro z hz + unfold preemptionPoint + refine runE_bindA (a := ()) (s1 := σ) (runE_liftA (doExt_unitA _ σ)) ?_ + unfold orChoiceE + refine runE_bindA (a := σ) (s1 := σ) (runE_liftA rfl) ?_ + refine runE_bindA (a := (true, σ)) (s1 := σ) (runE_liftA ?_) ?_ + · simp [StateExt.wrapExtBool, NonDet.select, NonDet.selectF, NonDet.mkEf] + dsimp only + rw [if_pos rfl] + refine runE_bindA (a := ()) (s1 := σ) (runE_liftA (doExt_unitA _ σ)) ?_ + refine runE_bindA (a := ()) (s1 := σ1) (runE_liftA h1) ?_ + refine runE_bindA (a := o) (s1 := σ2) (runE_liftA h2) ?_ + refine runE_bindA (a := isCurDomainExpired σ2) (s1 := σ2) (runE_liftA rfl) ?_ + refine runE_bindA (a := σ2.curSc) (s1 := σ2) (runE_liftA rfl) ?_ + refine runE_bindA (a := σ2.consumedTime) (s1 := σ2) (runE_liftA rfl) ?_ + refine runE_bindA (a := act) (s1 := σ2) (runE_liftA ?_) ?_ + · unfold getScActive NonDet.getsThe + rw [hact]; rfl + refine runE_bindA (a := readScRefillSufficient (Z := Unit) σ2.curSc σ2.consumedTime σ2) (s1 := σ2) + (runE_liftA rfl) ?_ + exact hz + constructor + · intro hb + refine chain _ ?_ + unfold NonDetE.whenE + rw [if_pos hb] + exact runE_throwA () + · intro hb + refine chain _ ?_ + unfold NonDetE.whenE + rw [if_neg hb] + exact runE_pureA () + +/-- Spec membership (opaque to `simp` / `dsimp`). -/ +def ppMemA (σ : State Unit) (e : Except Unit Unit) (σ' : State Unit) : Prop := + (e, σ') ∈ ((preemptionPoint (Z := Unit)).run σ).1 + +section main +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +/-- **The preemption point refines `preemptionPoint`, generically over the loop invariant `R`** (J8, Architect 08:17Z +option (b)). The spec's work-unit operations are no-ops on `Z = Unit` and the choice is nondeterministic, so no +work-unit relation is needed: the closure hypotheses are + +* `hW` [J]: `R` and the caps of `OS` survive FL4's `WORK_UNITS` write (both writes); +* `hTs` [J/P]: the timestamp step takes the spec `updateTimeStamp` and keeps `R` and the caps of `OS`; +* `hIrq` [P]: `irq_pending` is the spec's interrupt poll (`getActiveIRQ true`) and keeps `R`; +* `hAct`, `hSuf` [J]: `sc_active` / `sufficient` of the current SC agree with the spec's `readScActive` / + `readScRefillSufficient` at the post-poll state; +* `hnd` [P]: one domain, so `is_cur_domain_expired` is false on both sides. + +The post is `PPHypUtA`'s own (`Rp := R`). -/ +theorem preemption_point_mainA (OS : Set U64) (R : S → A → State Unit → Prop) + (hnd : numDomains = 1) + (hW : ∀ s a σ v s1, R s a σ → obj.ks_set st s layout.ks.WORK_UNITS v = ok s1 → + R s1 a σ ∧ ∀ y ∈ OS, capA st s1 y = capA st s y) + (hTs : ∀ s a σ, R s a σ → sched.update_timestamp st ar s a ⦃ r => ∃ σ1, + ((), σ1) ∈ (updateTimeStamp (Z := Unit) σ).1 ∧ R r.1 r.2 σ1 ∧ ∀ y ∈ OS, capA st r.1 y = capA st s y ⦄) + (hIrq : ∀ s a σ, R s a σ → ar.irq_pending a ⦃ r => ∃ o σ2, + (o, σ2) ∈ (doMachineOp (getActiveIRQ true) σ).1 ∧ (r.1 = true ↔ ¬ o = none) ∧ R s r.2 σ2 ⦄) + (hAct : ∀ s a σ, R s a σ → sporadic.sc_active st s (L.cscW s) ⦃ b => + readScActive (Z := Unit) σ.curSc σ = some b ⦄) + (hSuf : ∀ s a σ, R s a σ → sporadic.sufficient st ar s a (L.cscW s) + (L.mem s (L.nodeA s layout.node.CONSUMED)) ⦃ ok1 => + (ok1 = true ↔ readScRefillSufficient (Z := Unit) σ.curSc σ.consumedTime σ = some true) ⦄) : + ∀ (s : S) (a : A) (σ : State Unit), R s a σ → + sched.preemption_point st ar s a ⦃ r => + (∀ y ∈ OS, capA st r.2.1 y = capA st s y) ∧ ∃ σ', + (r.1 = .Ok () ∧ ppMemA σ (.ok ()) σ' ∧ R r.2.1 r.2.2 σ') ∨ + (r.1 = .Err .Preempted ∧ ppMemA σ (.error ()) σ' ∧ R r.2.1 r.2.2 σ') ⦄ := by + intro s a σ hR + unfold sched.preemption_point + obtain ⟨i, hi⟩ : ∃ i, obj.ks_get st s layout.ks.WORK_UNITS = ok i := ⟨_, eq_of_spec_fd (obj.ks_get.spec s _)⟩ + rw [hi, bind_ok] + simp only [lift, bind_ok] + generalize core.num.U64.wrapping_add i 1#u64 = w + obtain ⟨s1, h1⟩ : ∃ s1, obj.ks_set st s layout.ks.WORK_UNITS w = ok s1 := + ⟨_, eq_of_spec_fd (obj.ks_set.spec s _ _)⟩ + rw [h1, bind_ok] + obtain ⟨hR1, hc1⟩ := hW s a σ w s1 hR h1 + split + · obtain ⟨s2, h2⟩ : ∃ s2, obj.ks_set st s1 layout.ks.WORK_UNITS 0#u64 = ok s2 := + ⟨_, eq_of_spec_fd (obj.ks_set.spec s1 _ _)⟩ + rw [h2, bind_ok] + obtain ⟨hR2, hc2⟩ := hW s1 a σ 0#u64 s2 hR1 h2 + refine WP.spec_bind (hTs s2 a σ hR2) ?_ + rintro ⟨s3, a1⟩ ⟨σ1, hσ1, hR3, hc3⟩ + refine WP.spec_bind (hIrq s3 a1 σ1 hR3) ?_ + rintro ⟨irq, a2⟩ ⟨o, σ2, hσ2, hirq, hR4⟩ + dsimp only at hR4 hirq + rw [eq_of_spec_fd (sched.cur_sc.spec s3)] + simp only [bind_ok] + rw [eq_of_spec_fd (sched.consumed.specA s3)] + simp only [bind_ok] + refine WP.spec_bind (hAct s3 a2 σ2 hR4) ?_ + intro b hb + have capsAll : ∀ y ∈ OS, capA st s3 y = capA st s y := fun y hy => by + rw [hc3 y hy, hc2 y hy, hc1 y hy] + have hdom : isCurDomainExpired σ2 = false := by + unfold isCurDomainExpired; simp [hnd] + have L1 := pp_spec_longA hσ1 hσ2 hb + rw [hdom] at L1 + by_cases hb' : b = true + · rw [if_pos hb'] + refine WP.spec_bind (hSuf s3 a2 σ2 hR4) ?_ + intro ok1 hok1 + by_cases hirq' : irq = true + · rw [if_pos hirq', WP.spec_ok] + refine ⟨capsAll, σ2, Or.inr ⟨rfl, L1.1 (Or.inl (fun h => ?_)), hR4⟩⟩ + · exact absurd (hirq.1 hirq') (by simpa using h) + · rw [if_neg hirq'] + rw [is_cur_domain_expired_eqA]; simp only [bind_ok, Bool.false_eq_true, ↓reduceIte] + by_cases hk : ok1 = true + · rw [if_pos hk, WP.spec_ok] + have hs := hok1.1 hk + refine ⟨capsAll, σ2, Or.inl ⟨rfl, L1.2 ?_, hR4⟩⟩ + rintro (h | h | h) + · exact hirq' (hirq.2 h) + · simp [hdom] at h + · exact h ⟨hb', hs⟩ + · rw [if_neg hk, WP.spec_ok] + refine ⟨capsAll, σ2, Or.inr ⟨rfl, L1.1 (Or.inr (Or.inr fun h => hk (hok1.2 h.2))), hR4⟩⟩ + · have hbf : b = false := by simpa using hb' + rw [if_neg hb', bind_ok] + by_cases hirq' : irq = true + · rw [if_pos hirq', WP.spec_ok] + refine ⟨capsAll, σ2, Or.inr ⟨rfl, L1.1 (Or.inl (fun h => ?_)), hR4⟩⟩ + exact absurd (hirq.1 hirq') (by simpa using h) + · rw [if_neg hirq'] + rw [is_cur_domain_expired_eqA]; simp only [bind_ok, Bool.false_eq_true, ↓reduceIte] + rw [WP.spec_ok] + refine ⟨capsAll, σ2, Or.inr ⟨rfl, L1.1 (Or.inr (Or.inr fun h => hb' h.1)), hR4⟩⟩ + · rw [WP.spec_ok] + exact ⟨hc1, σ, Or.inl ⟨rfl, pp_spec_shortA σ, hR1⟩⟩ + +theorem preemption_point_refinesA (OS : Set U64) (R : S → A → State Unit → Prop) + (hnd : numDomains = 1) + (hW : ∀ s a σ v s1, R s a σ → obj.ks_set st s layout.ks.WORK_UNITS v = ok s1 → + R s1 a σ ∧ ∀ y ∈ OS, capA st s1 y = capA st s y) + (hTs : ∀ s a σ, R s a σ → sched.update_timestamp st ar s a ⦃ r => ∃ σ1, + ((), σ1) ∈ (updateTimeStamp (Z := Unit) σ).1 ∧ R r.1 r.2 σ1 ∧ ∀ y ∈ OS, capA st r.1 y = capA st s y ⦄) + (hIrq : ∀ s a σ, R s a σ → ar.irq_pending a ⦃ r => ∃ o σ2, + (o, σ2) ∈ (doMachineOp (getActiveIRQ true) σ).1 ∧ (r.1 = true ↔ ¬ o = none) ∧ R s r.2 σ2 ⦄) + (hAct : ∀ s a σ, R s a σ → sporadic.sc_active st s (L.cscW s) ⦃ b => + readScActive (Z := Unit) σ.curSc σ = some b ⦄) + (hSuf : ∀ s a σ, R s a σ → sporadic.sufficient st ar s a (L.cscW s) + (L.mem s (L.nodeA s layout.node.CONSUMED)) ⦃ ok1 => + (ok1 = true ↔ readScRefillSufficient (Z := Unit) σ.curSc σ.consumedTime σ = some true) ⦄) : + PPHypUtA st ar OS R R := + fun s a σ hR => preemption_point_mainA OS R hnd hW hTs hIrq hAct hSuf s a σ hR + +end main + +end +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3PreemptChargeA.lean b/proofs/Fl4ProofsA64/P3/Stage3PreemptChargeA.lean new file mode 100644 index 000000000..c1ef70fbf --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3PreemptChargeA.lean @@ -0,0 +1,519 @@ +/- +P3 (aarch64, A64-P3 Stage-3 integration, G3): **the preemption path's budget step at a preempted invocation's exit**. +At that state the scheduler action is `resume_cur_thread` or `choose_new_thread`, so the entry facts `ChargeEntryA` +(`actionW = 0`) and `CurInvA` are not available; the facts are those of `PreExitA` and the record. + +* `JStage3A.cscAc_of_activeA`: an active current SC is in `Ac` (`SCsOK.act`, `ScRelWA.max`). +* `ChargeFactsA`, `JStage3A.chargeP1PreA`: A64 P1's post of `charge_budget` without `actionW = 0`. +* `JStage3A.checkBudgetNoSendPreA`: `check_budget` on an exhausted head refill without the timeout send. +* `entryChargeHeadOwnA`, `JStage3A.chargeUnschedPreA`: `charge_budget … false` for an unschedulable current thread, + without `CurInvA` (the current SC may be unbound or bound to another thread). +* `JStage3A.clearConsumedRecA`: the record across the clear of the node's consumed time. + +Named premises: [J] `CurScChargeableA` (l4v `cur_sc_chargeable`'s analogue), `T`; [P] `hA`, `hag`, `hMin`, `hLimit`. +-/ +import Fl4ProofsA64.P3.Stage3KEntrySendWireA +import Fl4ProofsA64.P3.Stage3DispBaseA +import Fl4ProofsA64.P3.Stage3ChKeepA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 Fl4Spec Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-- A schedulable thread is runnable, outside the release queue, and bound to an active SC. -/ +theorem schedulable_tcbExA {σ : State Unit} {t : ObjRef} {x : Tcb} (h : schedulable t σ = true) + (hx : σ.kheap t = some (.tcb x)) : runnable x.tcbState = true ∧ t ∉ σ.releaseQueue ∧ + ∃ p y n, x.tcbSchedContext = some p ∧ σ.kheap p = some (.schedContext y n) ∧ scActive y = true := by + refine ⟨(schedulable_partsA h).1 x hx, (schedulable_partsA h).2, ?_⟩ + unfold schedulable at h + simp only [decide_eq_true_eq] at h + have h2 := h.2.1 + cases hp : x.tcbSchedContext with + | none => simp [optPred, optMap, optMapSome, tcbsOfKh, tcbOf, hx, hp] at h2 + | some p => + cases hk : σ.kheap p with + | none => simp [optPred, optMap, optMapSome, tcbsOfKh, scsOfKh, tcbOf, scOf, hx, hp, hk] at h2 + | some ko => + cases ko with + | schedContext y n => + refine ⟨p, y, n, rfl, hk, ?_⟩ + simpa [optPred, optMap, optMapSome, tcbsOfKh, scsOfKh, tcbOf, scOf, hx, hp, hk] using h2 + | _ => simp [optPred, optMap, optMapSome, tcbsOfKh, scsOfKh, tcbOf, scOf, hx, hp, hk] at h2 + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {nA : U64} {s : S} {a : A} {σ : State Unit} {m : McState} + +/-- **An active current SC is in `Ac`.** -/ +theorem JStage3A.cscAc_of_activeA (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {y : SchedContext} {k : Nat} (hk : σ.kheap σ.curSc = some (.schedContext y k)) (ha : scActive y = true) : + L.cscW s ∈ Ac := by + have hc : L.cscW s ∈ Z.C := J.sr.cscC + obtain ⟨x, j, hx, R⟩ := J.sr.scr.sc _ hc + change σ.kheap (scNameA m (L.cscW s)) = _ at hx + rw [J.sr.toSchedSRN.currentScA, hx] at hk + cases hk + have hB := J.sys.ent.bxi + refine (hB.2.2.2.2.act _ (by rw [J.scs]; exact hc)).2 (fun e => ?_) + unfold scActive activeSc at ha + rw [← R.max, e] at ha + simp at ha + +/-- With no switch pending, the scheduler-action word is not the current thread. -/ +theorem JStage3A.actNeCurA (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (noSwitch : ∀ u, σ.schedulerAction ≠ .switchThread u) : L.actionW s ≠ L.curW s := by + intro e + have h := J.sr.sch.act + have ha : TcbAddr (L.curW s) := J.sr.core.tcbs _ J.sr.curT + have h0 : L.curW s ≠ 0#u64 := fun e0 => ha.ne0 (by rw [e0]; rfl) + have h1 : L.curW s ≠ 1#u64 := fun e1 => by + have := ha.align; rw [e1] at this; exact absurd this (by decide) + rw [e, actOfA, if_neg h0, if_neg h1] at h + exact noSwitch _ h + +/-- **The charge's entry facts without the scheduler action** (`ChargeEntryA`'s `cA` and `run`). -/ +structure ChargeFactsA (st : machine.Store S) [L : LawfulStore st] (Ac : Set U64) (s : S) : Prop where + cA : L.cscW s ∈ Ac + run : Fl4ProofsA64.P1.Runnable (L.mem s (stA (L.curW s))) → + (L.mem s (scA (L.curW s)) ≠ 0#u64 → L.cscW s = L.mem s (scA (L.curW s))) ∧ + (L.mem s (inqA (L.curW s)) &&& sched.Q_RELEASE) = 0#u64 + +/-- The facts from the spec side: the current SC active, the current thread's SC none or the current one, and the +current thread outside the release queue. -/ +theorem JStage3A.chargeFactsA (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {y : SchedContext} {k : Nat} (hk : σ.kheap σ.curSc = some (.schedContext y k)) (ha : scActive y = true) + (hsc : ∀ x, σ.kheap σ.curThread = some (.tcb x) → + x.tcbSchedContext = none ∨ x.tcbSchedContext = some σ.curSc) + (hrel : σ.curThread ∉ σ.releaseQueue) (hon : L.mem s (affA (L.curW s)) = L.cpu s) : ChargeFactsA st Ac s := by + have ct : σ.curThread = specPtr (L.curW s) := J.sr.sch.curThread + refine ⟨J.cscAc_of_activeA hk ha, fun _ => ⟨fun h0 => ?_, (J.sr.toSchedSRN.relOn J.sr.curT hon).2 (ct ▸ hrel)⟩⟩ + obtain ⟨x, hx, hW⟩ := J.sr.sch.tcb _ J.sr.curT + have h := hsc x (by rw [ct]; exact hx) + rw [hW, optScA, if_neg h0] at h + rcases h with h | h + · cases h + apply scRenA_inj m (J.sr.sch.scOff _ J.sr.cscC) (J.sr.sch.scOff _ (J.sr.rest.scMem _ J.sr.curT h0)) + rw [J.sr.toSchedSRN.currentScA] at h + exact (Option.some.inj h).symm + +/-- **A64 P1's invariants after `charge_budget`, without `actionW = 0`.** -/ +theorem JStage3A.chargeP1PreA (hA : P0.ArchTotal ar) + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (F : ChargeFactsA st Ac s) (noSwitch : ∀ u, σ.schedulerAction ≠ .switchThread u) + (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) (used : U64) (cto : Bool) : + sched.charge_budget st ar s a used cto ⦃ r => SysL st H Z.Cs OS ia ie r.1 ∧ SymL st H Z.Cs r.1 ∧ + BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac r.1 ⦄ := by + obtain ⟨_, _, _, _, _, _, _, _, _, _, _, W⟩ := J.j7.unp + have BKh : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s := by + obtain ⟨q, hB, hX, hSg, hS⟩ := J.sys.ent.bxi + exact ⟨q, hB, hX, hS, hSg⟩ + have hna := J.actNeCurA noSwitch + have hRV : RunVcpuInv st H.tcbs s := by rw [J.tcbs]; exact J.runVcpu + have nq : (L.mem s (inqA (L.curW s)) &&& sched.Q_QUEUED) = 0#u64 := by + by_contra hq + have hc : L.curW s ∈ H.tcbs := J.sq.curT + have mem := (J.sq.queued (L.curW s) hc).1 hq + rw [J.sr.curOn] at mem + have hp : (L.mem s (prioA (L.curW s))).val < 256 := J.sq.prio _ hc + have hr := J.sr.rest.rq _ hp + have ct : σ.curThread = specPtr (L.curW s) := J.j.1.sch.curThread + refine hnq 0 (BitVec.ofNat 8 (L.mem s (prioA (L.curW s))).val) ?_ + rw [hr, ct] + exact List.mem_map_of_mem mem + have cb : Fl4ProofsA64.P1.Runnable (L.mem s (stA (L.curW s))) → CanBlock st H.tcbs Ac s (L.curW s) := fun hrun => + { mem := J.sq.curT + nb := fun hb => by + rcases hrun with h | h | h <;> rcases hb with h' | h' | h' <;> rw [h] at h' <;> + exact absurd (congrArg UScalar.val h') (by + first + | (simp [sched.TS_RUNNING, sched.TS_RESTART, sched.TS_RUNNING_VM, sched.TS_BLOCKED_ON_SEND, + sched.TS_BLOCKED_ON_RECV, sched.TS_BLOCKED_ON_NTFN]; done) + | decide) + nrp := fun h' => by + rcases hrun with h | h | h <;> rw [h] at h' <;> + exact absurd (congrArg UScalar.val h') (by + first + | (simp [sched.TS_RUNNING, sched.TS_RESTART, sched.TS_RUNNING_VM, sched.TS_BLOCKED_ON_REPLY]; done) + | decide) + nq := nq + nr := (F.run hrun).2 + na := hna + ni := J.cur_ne_idle hrun + ac := fun h0 => (F.run hrun).1 h0 ▸ F.cA } + refine WP.spec_mono (sched.charge_budget.rpwC (Vc := Z.V) hA a used cto J.sys.sysl J.symL BKh J.irqPlaceA J.ap.sca + W.col.case.ts J.ko J.vsOk (fun r => (F.run r).1) hna F.cA cb hRV) ?_ + rintro ⟨s', a'⟩ P + simp only [WP.uncurry'_pair] at P ⊢ + obtain ⟨sysl, symL, ⟨q, hB, hX, hS, hSg⟩, -⟩ := P + exact ⟨sysl, symL, q, hB, hX, hSg, hS⟩ + +set_option maxHeartbeats 4000000 in +/-- **`check_budget` on an exhausted head refill without the timeout send**, for a current thread with `CurInvA` and +any scheduler action but a pending switch. -/ +theorem JStage3A.checkBudgetNoSendPreA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar nA)) s a σ m) + (noSwitch : ∀ u, σ.schedulerAction ≠ .switchThread u) (I : CurInvA σ) (Idle : ActIdleA σ) + (hcore : L.mem s (scCoreA (L.cscW s)) = L.cpu s) (hni : σ.curSc ≠ idleScPtr → NotIdleScA m σ.curSc) + (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + {Clock : A → MachineState → Prop} (clk : Clock a σ.machineState) + (T : EntryValidFromA (st := st) Z m κ Clock s σ) + (hag : fl4MinBudget = MIN_BUDGET) (hMin : Fl4MinBudgetAgreeA ar a) + (hLimit : sporadic.max_release_time ar a ⦃ v => v.bv = fl4MaxReleaseTime ⦄) + (hex : fl4GetScRefillSufficient (Z := Unit) σ.curSc σ.consumedTime σ = ({(false, σ)}, False)) + (hns : NoTimeoutA σ.consumedTime σ) (hA : P0.ArchTotal ar) (F : ChargeFactsA st Ac s) : + sched.check_budget st ar s a ⦃ r => ∃ τ, fl4CheckBudget (Z := Unit) σ = ({(r.1, τ)}, False) ∧ + JStage3QA st ar AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes nA m + r.2.1 r.2.2 τ ⦄ := by + have H0 := J.yieldQueueA + let used := L.mem s (L.nodeA s layout.node.CONSUMED) + have hUsed : σ.consumedTime = used.bv := H0.sched.rest.consumed + unfold sched.check_budget + rw [eq_of_spec_fd (sched.cur_sc.spec s), bind_ok, eq_of_spec_fd (sched.consumed.specA s), bind_ok] + have dec := entryBudgetDecisionA H0.sched.toSchedSRN I J.sinv.mc a hMin + rw [eq_of_spec_fd (sched.cur_sc.spec s), bind_ok, eq_of_spec_fd (sched.consumed.specA s), bind_ok] at dec + refine WP.spec_bind dec ?_ + rintro b ⟨e, -⟩ + have hb : b = false := by + have q := e.symm.trans hex + exact (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst q))).1 + subst hb + have spec : fl4CheckBudget (Z := Unit) σ = + (fl4ChargeBudget used.bv true >>= fun _ => (pure false : SMonad Unit Bool)) σ := by + unfold fl4CheckBudget + rw [nd_gets_bindSc, nd_gets_bindSc, nd_bind_oneSc e] + simp only [Bool.false_eq_true, ↓reduceIte] + rw [nd_gets_bindSc, hUsed] + simp only [Bool.false_eq_true, ↓reduceIte] + have P : EntryBudgetPlaceA st m κ s σ := ⟨J.sinv.mc, (J.sr.scCore _ J.sr.cscC).symm.trans hcore, hni, hnq⟩ + refine WP.spec_bind (WP.spec_and (charge_budget_noSendFrA (ar := ar) H0 I Idle P a clk T hag hMin hLimit used true + (fun _ => by rw [← hUsed]; exact hns)) (J.chargeP1PreA hA F noSwitch hnq used true)) ?_ + rintro ⟨s1, a1⟩ ⟨⟨τ, Q', R', he, heF, Y, -, htm, -, hc0, -, -, Fr, KS, CS, ha'⟩, sysl, symL, bxi⟩ + dsimp only at he heF Y Fr KS CS ha' sysl symL bxi ⊢ + subst ha' + change ok (false, s1, a1) ⦃ _ ⦄ + rw [WP.spec_ok] + have FR : SchedFrameA σ τ := + frame_of_runA (inferInstance : SPresInst (fl4ChargeBudget (Z := Unit) used.bv false)).pres heF + have hsc : ∀ y k, σ.kheap σ.curSc = some (.schedContext y k) → y.scTcb = some σ.curThread := by + intro y k hy + obtain ⟨t, hkt, hB, -⟩ := I.tcb + obtain ⟨z, j, hz, ho⟩ := J.sr.inv.back _ _ _ hkt hB + rw [hy] at hz; cases hz; exact ho + have SH := chargeSteps_shapeA CS hsc J.sinv.curSc hnq noSwitch htm + refine ⟨τ, ?_, J.of_chargeStepA Y Fr FR KS SH sysl symL bxi hc0⟩ + rw [spec, nd_bind_oneSc he] + rfl + +/-- **The head charge of the current SC for any bound thread** (`entryChargeHeadEqA` without `CurInvA`): the SC is +unbound or bound to `u`, outside the release queue when the SC is charged. -/ +theorem entryChargeHeadOwnA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} {u : ObjRef} + (H : SchedSRB st Z m κ s σ u) (a : A) (hag : fl4MinBudget = MIN_BUDGET) + (hlimit : sporadic.max_release_time ar a ⦃ v => v.bv = fl4MaxReleaseTime ⦄) + (hmin : sporadic.min_budget ar a ⦃ v => v.bv = fl4MinBudget ⦄) + {sc : U64} (hc : sc ∈ Z.C) (hcs : L.cscW s = sc) (hcur : σ.curSc = scNameA m sc) + {x : SchedContext} {n : Nat} (hk : σ.kheap (scNameA m sc) = some (.schedContext x n)) + (hActive : scActive x = true) (hni : σ.curSc ≠ idleScPtr → NotIdleScA m σ.curSc) (c : U64) (b : Bool) + (hb : b = decide (σ.curSc ≠ idleScPtr)) (ho : x.scTcb = none ∨ x.scTcb = some u) + (hur : σ.curSc ≠ idleScPtr → u ∉ σ.releaseQueue) : + (if b then do + let rr ← sporadic.is_round_robin st s sc + let s3 ← if rr then do + let hi ← obj.Sc.refill_head st sc s + let ti ← obj.Sc.refill_tail st sc s + let h ← sporadic.get st s sc hi + let t ← sporadic.get st s sc ti + let i ← lift (core.num.U64.wrapping_add h.amount t.amount) + let s4 ← sporadic.put st s sc hi { h with amount := i } + sporadic.put st s4 sc ti { t with amount := 0#u64 } + else sporadic.budget_check st ar s a c + let i ← obj.Sc.consumed st sc s3 + let total ← lift (core.num.U64.wrapping_add i c) + obj.Sc.set_consumed st sc s3 total + else ok s) ⦃ s' => ∃ y, + let τ := updKSc σ (scNameA m sc) (.schedContext y n) + fl4ChargeHead σ.curSc c.bv σ = ({((), τ)}, False) ∧ SchedSRB st Z m κ s' τ u ∧ + y.scTcb = x.scTcb ∧ scActive y = scActive x ∧ y.scYieldFrom = x.scYieldFrom ∧ + y.scReplies = x.scReplies ∧ y.scRefillMax = x.scRefillMax ∧ (σ.curSc = idleScPtr → y = x) ∧ + WritesIn st s s' (ScObjA sc n) ⦄ := by + rw [hb] + by_cases hi : σ.curSc ≠ idleScPtr + · rw [decide_eq_true hi] + simp only [↓reduceIte, cfgResultAssocA] + have hNotIdle := hcur ▸ hni hi + rw [← cfgResultAssocA] + refine WP.spec_bind (entryChargeRingOwnA H a hc hcs hcur hk hActive hNotIdle ho (hur hi) hag hlimit hmin c) ?_ + rintro s' ⟨y, he, hS, hy, w⟩ + have hMax : y.scRefillMax = x.scRefillMax := by + have e := congrArg SchedContext.scRefillMax hy + exact e + have hY : scActive y = scActive x := by + unfold scActive activeSc + rw [hMax] + have htY : y.scTcb = x.scTcb := by + have e := congrArg SchedContext.scTcb hy + exact e + have hyf : y.scYieldFrom = x.scYieldFrom := by + have e := congrArg SchedContext.scYieldFrom hy + exact e + have hrp : y.scReplies = x.scReplies := by + have e := congrArg SchedContext.scReplies hy + exact e + refine WP.spec_mono (entryConsumedOwnA hS hc (updKSc_same _ _ _) (hY.trans hActive) hNotIdle + (htY.symm ▸ ho) (hur hi) c) ?_ + rintro s'' ⟨hU, hS', w'⟩ + rw [updKSc_updKSc] at hS' + refine ⟨{ y with scConsumed := y.scConsumed + c.bv }, ?_, hS', htY, hY, hyf, hrp, hMax, fun e => (hi e).elim, + (w.trans w').mono fun _ h => h.elim id id⟩ + unfold fl4ChargeHead + rw [NonDet.when, if_pos hi, hcur] + rw [nd_bind_oneSc (isRoundRobin_eqA hk)] at he ⊢ + split_ifs at he ⊢ <;> rw [nd_bind_oneSc he] <;> dsimp only <;> rw [hU, updKSc_updKSc] + · rw [decide_eq_false hi] + simp only [Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + refine ⟨x, ?_, ?_, rfl, rfl, rfl, rfl, rfl, fun _ => rfl, WritesIn.refl _ _⟩ + · unfold fl4ChargeHead + rw [NonDet.when, if_neg hi, updKSc_selfA hk] + rfl + · rw [updKSc_selfA hk] + exact H + +set_option maxRecDepth 4096 in +set_option maxHeartbeats 4000000 in +/-- **`charge_budget … false` for an unschedulable current thread** (the preemption path's charge): the current SC may +be unbound or bound to another thread (`CurScChargeableA`). -/ +theorem JStage3A.chargeUnschedPreA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar nA)) s a σ m) + (noSwitch : ∀ u, σ.schedulerAction ≠ .switchThread u) + (hni : σ.curSc ≠ idleScPtr → NotIdleScA m σ.curSc) (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + (hsc : ∀ x, σ.kheap σ.curThread = some (.tcb x) → + x.tcbSchedContext = none ∨ x.tcbSchedContext = some σ.curSc) + (hrel : σ.curThread ∉ σ.releaseQueue) + (hown : ∀ y n, σ.kheap σ.curSc = some (.schedContext y n) → scActive y = true → σ.curSc ≠ idleScPtr → + ∀ o, y.scTcb = some o → o ∉ σ.releaseQueue) + (hun : schedulable σ.curThread σ = false) + (hact : getScActive σ.curSc σ = ({(true, σ)}, False)) (hch : CurScChargeableA σ) + (hon : L.mem s (affA (L.curW s)) = L.cpu s) + (hA : P0.ArchTotal ar) (hag : fl4MinBudget = MIN_BUDGET) (hMin : Fl4MinBudgetAgreeA ar a) + (hLimit : sporadic.max_release_time ar a ⦃ v => v.bv = fl4MaxReleaseTime ⦄) (used : U64) : + sched.charge_budget st ar s a used false ⦃ r => ∃ τ, + fl4ChargeBudget (Z := Unit) used.bv false σ = ({((), τ)}, False) ∧ + JStage3QA st ar AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes nA m r.1 r.2 τ ∧ + τ.consumedTime = 0 ∧ τ.schedulerAction = σ.schedulerAction ∧ τ.curSc = σ.curSc ∧ τ.curTime = σ.curTime ∧ + τ.machineState = σ.machineState ⦄ := by + have E0 := J.yieldQueueA + let H0 := E0.sched + let sc := L.cscW s + let p := scNameA m sc + have hscC : sc ∈ Z.C := H0.cscC + have hcur : σ.curSc = p := H0.currentScA + obtain ⟨x, n, hk, R⟩ := H0.scr.sc sc hscC + have hk' : σ.kheap σ.curSc = some (.schedContext x n) := hcur ▸ hk + have hAx : scActive x = true := by + have e := (getScActive_eqA hk').symm.trans hact + exact (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst e))).1 + have Fc := J.chargeFactsA hk' hAx hsc hrel hon + have hid : m.idleScOf m.curCore = specPtr (L.mem s (L.nodeA s layout.node.IDLE_SC)) := by + rw [H0.sch.core] + exact H0.sch.idleScOf _ H0.sch.cpuCs + have hb : decide (sc ≠ L.mem s (L.nodeA s layout.node.IDLE_SC)) = decide (σ.curSc ≠ idleScPtr) := by + rw [hcur] + exact idle_testA hid (H0.sch.scOff sc hscC) + -- the thread the relation excepts: the bound one, else the current thread + obtain ⟨u, ho, hur, hu⟩ : ∃ u, (x.scTcb = none ∨ x.scTcb = some u) ∧ (σ.curSc ≠ idleScPtr → u ∉ σ.releaseQueue) ∧ + (x.scTcb = some u ∨ x.scTcb = none ∧ u = σ.curThread) := by + cases e : x.scTcb with + | none => exact ⟨σ.curThread, Or.inl rfl, fun _ => hrel, Or.inr ⟨rfl, rfl⟩⟩ + | some o => exact ⟨o, Or.inr rfl, fun hi => hown x n hk' hAx hi o e, Or.inl rfl⟩ + have core : sched.charge_budget st ar s a used false ⦃ r => r.2 = a ∧ ∃ τ y, + fl4ChargeBudget (Z := Unit) used.bv false σ = ({((), τ)}, False) ∧ + τ = { updKSc σ p (.schedContext y n) with consumedTime := 0 } ∧ + SchedSRA st Z m κ r.1 τ ∧ YieldQueueRelA (st := st) Z m κ r.1 τ ∧ MFrA (st := st) Z m s r.1 σ τ ∧ + ScChargeKeepA σ τ ∧ (IdleRdyA σ → IdleRdyA τ) ⦄ := by + unfold sched.charge_budget + refine WP.spec_bind (sched.cur_sc.spec s) ?_ + intro c hc + rw [show c = sc from hc] + rw [eq_of_spec_fd (sched.idle_sc.specA (st := st) s), bind_ok, sc_ne_eqA, bind_ok] + refine WP.spec_bind (entryChargeHeadOwnA (H0.toB u) a hag hLimit hMin hscC rfl hcur hk + hAx hni used _ hb ho hur) ?_ + rintro s1 ⟨y, hHead, H1, hOwn, hAct, hY, hRp, hMx, hIdleY, w1⟩ + let σ1 := updKSc σ p (.schedContext y n) + change SchedSRB st Z m κ s1 σ1 u at H1 + refine WP.spec_bind (sched.set_consumed.spec s1 0#u64) ?_ + rintro s2 rfl + let τ := { σ1 with consumedTime := 0 } + let s2 := L.wr s1 (L.nodeA s1 layout.node.CONSUMED) 0#u64 + obtain ⟨H2, w2⟩ := H1.clearConsumedA + change SchedSRB st Z m κ s2 τ u at H2 + have frame1 : MFrA (st := st) Z m s s1 σ σ1 := cfgScFrameA (st := st) hscC hk w1 + have frame2 : MFrA (st := st) Z m s1 s2 σ1 τ := + MFrA.of_sw rfl (w2.mono fun _ e => Or.inl (e ▸ H1.node_sks H1.sch.cpuCs (by + unfold layout.node.CONSUMED; decide))) + have frame : MFrA (st := st) Z m s s2 σ τ := frame1.trans frame2 rfl rfl + obtain ⟨E2, M2, Y2⟩ := entryScFrameA E0 hscC hk (show τ.kheap = σ1.kheap from rfl) hY frame + have h2 : τ.kheap p = some (.schedContext y n) := updKSc_same _ _ _ + have keep : ∀ q t, τ.kheap q = some (.tcb t) ↔ σ.kheap q = some (.tcb t) := fun q t => cfgTcbKeepA hk q t + -- no TCB bound to the current SC and outside the IPC queues is runnable + have notRun : ∀ z, σ.kheap σ.curThread = some (.tcb z) → z.tcbSchedContext = some σ.curSc → + runnable z.tcbState ≠ true := fun z hz hb' hr => by + have := cfgSchedulableA hz hb' hk' hr hAx hrel + rw [hun] at this; cases this + have unsch : schedulable τ.curThread τ ≠ true := by + intro h + obtain ⟨t, ht⟩ := J.sinv.curTcb + obtain ⟨hr, -, q, -, -, hq, -, -⟩ := schedulable_tcbExA h (x := t) ((keep _ _).2 ht) + rcases hsc t ht with e | e + · rw [e] at hq; cases hq + · exact notRun t ht e hr + refine WP.spec_bind (sched.cur_thread.spec s2) ?_ + intro c hc + let ct := L.curW s2 + have hCt : τ.curThread = specPtr ct := H2.sch.curThread + have hon2 : L.mem s2 (affA ct) = L.cpu s2 := H2.curOn + rw [show c = ct from hc, is_schedulable_eqA, schedulable_eqA H2.toSchedSRN H2.curT + (H2.relOn H2.curT hon2) E2, bind_ok, if_neg (hCt ▸ unsch), WP.spec_ok] + have H3 : SchedSRA st Z m κ s2 τ := ⟨H2.toSchedSRN, fun v z hz hClass => by + by_cases e : v = u + swap + · exact H2.rdEx v z e hz hClass + subst e + have hz0 := (keep _ _).1 hz + intro z' scp y' k hz' hb' _ _ _ + exfalso + rw [hz] at hz'; cases hz' + rcases hu with hu | ⟨hnone, hu⟩ + · rcases hch x n v z hk' hu hz0 with ⟨ev, hipc⟩ | hno + · have hr : runnable z.tcbState = true := by + rcases hClass with h | ⟨q, h⟩ | ⟨ep, d, h, _⟩ | ⟨ep, r, d, h⟩ + · exact h + all_goals rw [h] at hipc; cases hipc + rcases hsc z (ev ▸ hz0) with e | e + · rw [e] at hb'; cases hb' + · exact notRun z (ev ▸ hz0) e hr + · exact hno hClass + · rcases hsc z (hu ▸ hz0) with e | e + · rw [e] at hb'; cases hb' + · obtain ⟨w, j, hw, hwt⟩ := H0.inv.back _ _ _ hz0 e + rw [hk'] at hw; cases hw + rw [hnone] at hwt; cases hwt⟩ + refine ⟨rfl, τ, y, ?_, rfl, H3, ⟨H3, E2, M2, Y2⟩, frame, + scChargeKeep_updA hk hRp hY hOwn hMx (fun hA' => ?_) rfl, fun g z k hz => ?_⟩ + · unfold fl4ChargeBudget + rw [nd_gets_bindSc, nd_bind_oneSc hHead] + rw [nd_bind_oneSc (show NonDet.modify (fun s : State Unit => { s with consumedTime := 0 }) σ1 = + ({((), τ)}, False) from rfl)] + repeat rw [nd_gets_bindSc] + rw [NonDet.when, if_neg (hCt ▸ unsch)] + rfl + · obtain ⟨z0, n0, hz0, R1⟩ := H1.scr.sc sc hscC + change (updKSc σ p (.schedContext y n)).kheap (scNameA m sc) = _ at hz0 + rw [updKSc_same] at hz0; cases hz0 + have hx := R1.strong (hAct.trans hA') + have ne := refills_neL hx.wf + simpa [hx.refills] using ne + · by_cases e : σ.curSc = idleScPtr + · have ey := hIdleY e + subst ey + change (updKSc σ p (.schedContext y n)).kheap idleScPtr = _ at hz + rw [updKSc_selfA hk] at hz + exact g z k hz + · change (updKSc σ p (.schedContext y n)).kheap idleScPtr = _ at hz + rw [updKSc_kheap, Function.update_of_ne (by rw [← hcur]; exact Ne.symm e)] at hz + exact g z k hz + refine WP.spec_mono (WP.spec_and core (J.chargeP1PreA hA Fc noSwitch hnq used false)) ?_ + rintro ⟨s', a'⟩ ⟨⟨ha', τ, y, he, eτ, H3, Y, Fr, KS, rdy⟩, sysl, symL, bxi⟩ + dsimp only at ha' H3 Y Fr sysl symL bxi ⊢ + subst ha' + have FR : SchedFrameA σ τ := + frame_of_runA (inferInstance : SPresInst (fl4ChargeBudget (Z := Unit) used.bv false)).pres + (by rw [he]; try exact Set.mem_singleton _) + have SH : ChargeShapeA σ τ := by + subst eτ + exact ⟨rfl, rfl, rfl, rfl, ⟨y, n, by rw [hcur]⟩, Or.inl ⟨rfl, rfl, rfl⟩, rdy, ⟨rfl, rfl, rfl, rfl⟩⟩ + have hc0 : τ.consumedTime = 0 := by rw [eτ] + refine ⟨τ, he, J.of_chargeStepA (Q' := Z.Q) (R' := Z.R) Y Fr FR KS SH sysl symL bxi hc0, hc0, ?_, ?_, ?_, ?_⟩ <;> + rw [eτ] <;> rfl + +/-- **The record across the clear of the node's consumed time** (`after_invocation`'s inactive-SC branch). -/ +theorem JStage3A.clearConsumedRecA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar nA)) s a σ m) : + JStage3QA st ar AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes nA m + (L.wr s (L.nodeA s layout.node.CONSUMED) 0#u64) a { σ with consumedTime := 0 } := by + let τ : State Unit := { σ with consumedTime := 0 } + obtain ⟨cA, ecA⟩ : ∃ b, b = L.nodeAt s (L.cpu s) layout.node.CONSUMED := ⟨_, rfl⟩ + obtain ⟨s', es'⟩ : ∃ s', s' = L.wr s cA 0#u64 := ⟨_, rfl⟩ + have es : L.wr s (L.nodeA s layout.node.CONSUMED) 0#u64 = s' := by rw [es', ecA]; rfl + rw [es] + obtain ⟨H1, w1⟩ := (J.sr.toB σ.curThread).clearConsumedA + have H1' : SchedSRB st Z m κ s' τ σ.curThread := by rw [es', ecA]; exact H1 + have H3 : SchedSRA st Z m κ s' τ := ⟨H1'.toSchedSRN, J.sr.rd.of_eq rfl rfl rfl⟩ + have hcA : SKsA (L.ksb s) cA := by + rw [ecA] + exact J.sr.toSchedSRN.node_sks J.sr.sch.cpuCs (by unfold layout.node.CONSUMED; decide) + have w2 : WritesIn st s s' (· = cA) := by rw [es', ecA]; exact w1 + have frame : MFrA (st := st) Z m s s' σ τ := MFrA.of_sw rfl (w2.mono fun _ e => Or.inl (e ▸ hcA)) + obtain ⟨x, k, hk, -⟩ := J.sr.scr.sc _ J.sr.cscC + change σ.kheap (scNameA m (L.cscW s)) = _ at hk + obtain ⟨E2, M2, Y2⟩ := entryScFrameA J.yieldQueueA J.sr.cscC hk + (show τ.kheap = (updKSc σ (scNameA m (L.cscW s)) (.schedContext x k)).kheap by rw [updKSc_selfA hk]) rfl frame + have FR : SchedFrameA σ τ := SchedFrameA.of_eq rfl rfl rfl rfl rfl rfl rfl rfl + have KS : ScChargeKeepA σ τ := ⟨fun q z j hz => ⟨z, hz, rfl, rfl, rfl, rfl, fun _ h => h⟩, fun q y h => h⟩ + have SH : ChargeShapeA σ τ := ⟨rfl, rfl, rfl, rfl, + ⟨x, k, by rw [J.sr.toSchedSRN.currentScA, updKSc_selfA hk]⟩, Or.inl ⟨rfl, rfl, rfl⟩, fun g => g, + ⟨rfl, rfl, rfl, rfl⟩⟩ + -- A64 P1's invariants across the node word + have nd := J.sq.nodes _ J.sq.cur + have d40 : layout.node.CONSUMED.val < 4288 := by simp [layout.node.CONSUMED] + have v40 : layout.node.CONSUMED.val = 40 := by unfold layout.node.CONSUMED; rfl + have a40 := nodeAt_aligned nd d40 (by rw [v40]) + rw [← ecA] at a40 + have hw : WritesIn st s s' (fun b => b = cA) := w2 + have Lc := LvC.of_blk (BXI.blk J.sys.ent.bxi) + have lf40 : LowFree layout.node.CONSUMED.val := by rw [v40]; unfold LowFree; omega + have bx := nodew_bxc (o := layout.node.CONSUMED) (v := 0#u64) J.sys.ent.bxi lf40 (by rw [v40]; omega) + (by rw [v40]; omega) + rw [← ecA, ← es'] at bx + have dfw : ∀ b : U64, b = cA → ∃ c ∈ Z.Cs, ∃ o : U64, o.val < 4288 ∧ o.val ≠ 88 ∧ o.val ≠ 8 ∧ b = L.nodeAt s c o := + fun b e => ⟨_, J.sq.cur, _, d40, by rw [v40]; omega, by rw [v40]; omega, e.trans ecA⟩ + have d : DF st H Z.Cs (L.curW s) ∅ s s' := by + refine ⟨hw.mono fun b hb => ?_, (LvF.ofCwN Lc hw fun b _ hb => ?_).mono (LvF.none_le _)⟩ + · obtain ⟨c, hc, o, o1, o2, -, e⟩ := dfw b hb + exact Or.inl (Or.inr (Or.inl ⟨c, hc, o, o1, o2, e⟩)) + · obtain ⟨c, hc, o, o1, o2, o3, e⟩ := dfw b hb + exact Or.inr ⟨c, hc, o, o1, o2, o3, s, rfl, e⟩ + have lvf : LvF st H.tcbs Z.Cs H.replies H.scs H.ntfns LvX.none s s' := by + refine LvF.ofCwN Lc hw fun b _ hb => ?_ + obtain ⟨c, hc, o, o1, o2, o3, e⟩ := dfw b hb + exact Or.inr ⟨c, hc, o, o1, o2, o3, s, rfl, e⟩ + have hKo : ∀ o : U64, o.val ∈ (∅ : Set Nat) → o.val < 224 → ∀ y ∈ OS, + ¬ (y.val ≤ (wa (L.ksb s) o).val ∧ (wa (L.ksb s) o).val < y.val + 32) := + fun _ h => absurd h (Set.notMem_empty _) + obtain ⟨sysl, symL⟩ : SysL st H Z.Cs OS ia ie s' ∧ SymL st H Z.Cs s' := by + have hO : OSOK H.tcbs H.omega H.frame OS := J.sys.ent.os + obtain ⟨Q, R, q⟩ := J.sys.ent.bxi.blk.1 + have hSg := J.sys.ent.bxi.blk.2.2.2.2 + refine SysL.dfT (Bw := fun _ => False) J.sys.ent.heap hO J.sys.ent.bxi (Or.inr lvf) hKo (fun _ h => h.elim) + (fun _ _ _ _ _ h => h) J.sys.sysl J.symL (DFB.ofDF d) + (J.sys.sysl.ipc.keep (IbK.of_w q.tcbs d.w fun y hy hb => ?_)) + rcases hb with hk | ⟨o, -, hl, h8, e⟩ + · exact ib_not_kw8 q (fun c hc => absurd hc (Set.notMem_empty c)) hSg.sub + (fun e he => absurd he (Set.notMem_empty e)) hy (Or.inl hk) + · exact ib_not_ks q hSg hy (by omega) h8 e + exact J.of_chargeStepA (Q' := Z.Q) (R' := Z.R) ⟨H3, E2, M2, Y2⟩ frame FR KS SH sysl symL bx.1.1 rfl + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3PreemptIrqA.lean b/proofs/Fl4ProofsA64/P3/Stage3PreemptIrqA.lean new file mode 100644 index 000000000..e9dec261c --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3PreemptIrqA.lean @@ -0,0 +1,141 @@ +/- +P3 (aarch64, A64-P3 Stage-3 integration, asmsig item 2): **the pending interrupt at a preemption point** +(`take_ofA`), the `take` of `afterPreemptA` proved from the interrupt entry's premises. + +* `JStage3A.ownerA`: at the record, a thread bound to the current SC is outside the release queue while time is + consumed (`BodyOKA.owner`): the SC's forward link `scBoundA` and the record's `ZeroConsA`. No frame of the interrupt + service is needed: the record at the service's successor carries both. +* `BodyOKA.msA`: `BodyOKA` across a machine-state step that keeps the clock. +* `PollAgreeA`: [P] the poll. +* `take_ofA`: `mcs_preemption_point`'s successor, `take_pending_irq`, `check_interrupt`: `pendingIrq_refinesA` with + `JStage3A.handleInterruptA` at the polled state; the post is `A64RelS3`. + +Named premises (the interrupt entry's set `interrupt_serviceA`, same definitions and tags): +* [P] `PollAgreeA` (the poll agreement of `pendingIrq_refinesA`: the polled spec state differs from the previous one by + a machine state with the same memory and clock, and the arch relation survives `take_pending_irq`; after + fl4-kernel#154 the poll leaves the EL2 VGIC maintenance and virtual-timer PPIs pending at a preemption point, like + REMOTE_CALL, so it never returns a reserved line: `PollAgreeA` carries this as its last clause, which gives + `IrqLineA.reserved` on this path ([P]; the interrupt entry keeps `reserved` as [S])); +* [P] `IrqArchA ar AR (fun _ _ => True)` (mask / ack keep `AR` and are total), [P] `hMax`, [P] `ArchTotal`, `ArchPlat` + (with `nA = hPl.ipa`), `hmb`, `hmax`, `htu`; `hCs`; [S] `numNodes = 1`; +* `line`: `IrqLineBaseA` at the polled line: [P] `num`; [J] `active`; [S] `noResched` (`reserved` comes from `PollAgreeA`, [P]). + +New module. +-/ +import Fl4ProofsA64.P3.Stage3KEntryIrqA +import Fl4ProofsA64.P3.Stage3DispBaseA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4SpecA64 Fl4Spec Classical + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s : S} {a : A} {σ : State Unit} {m : McState} + +/-- **`BodyOKA.owner` from the record**: a thread bound to the current SC is outside the release queue while time is +consumed (module docstring). -/ +theorem _root_.Fl4ProofsA64.P3.JStage3A.ownerA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (y : SchedContext) (k : Nat) (hk : σ.kheap σ.curSc = some (.schedContext y k)) (hc : σ.consumedTime ≠ 0) + (o : ObjRef) (ho : y.scTcb = some o) : o ∉ σ.releaseQueue := by + have e : σ.curSc = scRenA m (L.cscW s) := J.sr.toSchedSRN.currentScA + obtain ⟨x, hx, hxs⟩ := J.scBoundA J.sr.cscC (y := y) (k := k) (o := o) (by rw [e] at hk; exact hk) ho + exact fun hr => hc (J.sinv.zc o x hx (by rw [e]; exact hxs) hr) + +end + +/-- `BodyOKA` across a machine-state step that keeps the clock. -/ +theorem Sched.BodyOKA.msA {σ : State Unit} {m : McState} (B : BodyOKA σ m) {ms : MachineState} + (h : ms.lastMachineTime = σ.machineState.lastMachineTime) : BodyOKA { σ with machineState := ms } m := + ⟨⟨by unfold ValidMachineTimeA; show σ.curTime ≤ ms.lastMachineTime; rw [h]; exact B.validTime, B.budget, + B.ctAct⟩, B.sw, B.scNotIdle, B.owner⟩ + +/-- [P] **The poll** (module docstring): `take_pending_irq` observes the spec's poll, whose successor changes the +machine state only, keeping memory and clock, and the arch relation survives. -/ +def PollAgreeA {A : Type} (ar : machine.Arch A) (AR : A → HwRootA → Prop) + (poll : SMonad Unit (Option Word)) : Prop := + ∀ (a : A) (c : HwRootA) (τ : State Unit), AR a c → + ar.take_pending_irq a ⦃ r => ∃ τ', (if r.1 = syscall.NO_IRQ then none else some r.1.bv, τ') ∈ (poll τ).1 ∧ + (∃ ms : MachineState, τ' = { τ with machineState := ms } ∧ + ms.underlyingMemory = τ.machineState.underlyingMemory ∧ + ms.lastMachineTime = τ.machineState.lastMachineTime) ∧ AR r.2 c ∧ + (r.1 ≠ syscall.NO_IRQ → r.1.val < 5376 → + (BitVec.ofNat 13 r.1.val : Irq) ≠ irqVGICMaintenance ∧ irqVppiEventIndex (BitVec.ofNat 13 r.1.val) = none) ⦄ + +/-- The accepted line without `reserved`: on the preemption path `reserved` comes from `PollAgreeA` ([P], #154). -/ +structure IrqLineBaseA {S : Type} (st : machine.Store S) [L : LawfulStore st] (s : S) (irq : U64) : Prop where + num : irq.val < 8192 + active : irq.val < 5376 → L.mem s (irqWordA (L.ksb s) irq) ≠ 0#u64 + noResched : irq.val < 5376 → L.mem s (irqWordA (L.ksb s) irq) = 3#u64 → layout.irq.id irq ≠ ok 1#u64 + +theorem IrqLineBaseA.toLine {S : Type} {st : machine.Store S} [L : LawfulStore st] {s : S} {irq : U64} + (B : IrqLineBaseA st s irq) + (hres : irq.val < 5376 → (BitVec.ofNat 13 irq.val : Irq) ≠ irqVGICMaintenance ∧ + irqVppiEventIndex (BitVec.ofNat 13 irq.val) = none) : IrqLineA st s irq := + ⟨B.num, B.active, B.noResched, fun h _ _ => hres h⟩ + +/-- The spec's service of a polled line: `handle_interrupt` at its 13-bit number. -/ +def preemptServiceA (w : Word) : SMonad Unit Unit := handleInterrupt (Z := Unit) (BitVec.ofNat 13 w.toNat) + +set_option maxHeartbeats 2000000 in +/-- **The pending interrupt after `mcs_preemption_point`** (module docstring): `afterPreemptA`'s `take`, with +`pending := pendingIrqA poll preemptServiceA`. -/ +theorem take_ofA {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {AR : A → HwRootA → Prop} {nA : U64} {Cs : Set U64} {m : McState} {s : S} {a : A} + {poll : SMonad Unit (Option Word)} + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) (hnA : nA = hPl.ipa) (K : IrqArchA ar AR (fun _ _ => True)) + (hCs : ∀ c ∈ Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hMax : kernelConfig.maxIRQ = 5375) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (hpoll : PollAgreeA ar AR poll) + (line : ∀ (r : S × A) (τ : State Unit) (irq : U64) (a' : A) (τ' : State Unit), + syscall.mcs_preemption_point st ar s a = ok r → + A64RelS3 st AR ar nA fl4A64Platform.numNodes Cs r.1 r.2 τ m → + ar.take_pending_irq r.2 = ok (irq, a') → irq ≠ syscall.NO_IRQ → (some irq.bv, τ') ∈ (poll τ).1 → + IrqLineBaseA st r.1 irq) : + ∀ r τ, syscall.mcs_preemption_point st ar s a = ok r → + A64RelS3 st AR ar nA fl4A64Platform.numNodes Cs r.1 r.2 τ m → + (do let (irq, a') ← ar.take_pending_irq r.2 + syscall.check_interrupt st ar r.1 a' irq) ⦃ out => + StepPostA (fun s' a' υ => A64RelS3 st AR ar nA fl4A64Platform.numNodes Cs s' a' υ m) + (pendingIrqA poll preemptServiceA) τ out ⦄ := by + subst hnA + intro r τ hr R + refine pendingIrq_refinesA (Rel := fun s' a' υ => A64RelS3 st AR ar hPl.ipa fl4A64Platform.numNodes Cs s' a' υ m) + ?take ?handle + case take => + obtain ⟨Z, H, OS, U, Ac, Pp, Tb, Lv, ia, ie, N, E, Rp, Bf, stack, κ, eCs, J, B⟩ := R + refine WP.spec_mono (hpoll r.2 Z.hw τ J.j.1.ar) ?_ + rintro r' ⟨τ', mem, ⟨ms, rfl, hm, hl⟩, har, -⟩ + exact ⟨_, mem, Z, H, OS, U, Ac, Pp, Tb, Lv, ia, ie, N, E, Rp, Bf, stack, κ, eCs, + J.of_machA har ms hm, B.msA hl⟩ + case handle => + intro irq a' τ' e1 hne mem R' + obtain ⟨Z, H, OS, U, Ac, Pp, Tb, Lv, ia, ie, N, E, Rp, Bf, stack, κ, eCs, J, B⟩ := R' + have hres : irq.val < 5376 → (BitVec.ofNat 13 irq.val : Irq) ≠ irqVGICMaintenance ∧ + irqVppiEventIndex (BitVec.ofNat 13 irq.val) = none := by + obtain ⟨Z0, H0, OS0, U0, Ac0, Pp0, Tb0, Lv0, ia0, ie0, N0, E0, Rp0, Bf0, stack0, κ0, eCs0, J0, B0⟩ := R + have hq := hpoll r.2 Z0.hw τ J0.j.1.ar + rw [e1, WP.spec_ok] at hq + exact hq.choose_spec.2.2.2 hne + have Ln : IrqLineA st r.1 irq := (line r τ irq a' τ' hr R e1 hne mem).toLine hres + have J' := J.arSwapA (AR' := withMRA AR (fun _ _ => True) τ'.machineState) ⟨J.j.1.ar, trivial⟩ + refine WP.spec_mono (J'.handleInterruptA hA hPl K (eCs ▸ hCs) h1 hMax hmb hmax htu irq Ln) ?_ + rintro r' ⟨σ'', mem2, ⟨Q, Rl, co, cs, J1⟩, sw, bd, hcs⟩ + have B' : BodyOKA σ'' m := + ⟨bd B.toBodyExitA, sw B.sw, fun h => by rw [hcs] at h ⊢; exact B.scNotIdle h, + fun y k hk _ _ hc o ho => J1.ownerA y k hk hc o ho⟩ + exact ⟨σ'', mem2, { Z with Q := Q, R := Rl, curOf := co, curScOf := cs }, H, OS, U, Ac, Pp, Tb, Lv, ia, ie, + N, E, Rp, Bf, stack, κ, eCs, J1.arSwapA (AR' := AR) J1.j.1.ar.1, B'⟩ + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3PreemptTA.lean b/proofs/Fl4ProofsA64/P3/Stage3PreemptTA.lean new file mode 100644 index 000000000..4de0a1954 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3PreemptTA.lean @@ -0,0 +1,1221 @@ +/- +P3 (AArch64, contract §36: **the shared preemption-point closure** over the SysL-free record `JStage3TA`, for the +untyped reset and the deletion loop; a `JStage3A` reaches it through `toTA`). + +* instances `J7PpA` / **`J7PpKA`** (the J7 slot kept across an `OS`-keeping step) for `J7A (J7R2A)` and + `J7LA (J7R2A)`; +* `JStage3TA.workUnitsA` (`hW`), `.timestampA` (`hTs`; needs only `ValidMachineTimeA` and the clock relation), + `.machineA` (`hIrq`), `.scActiveA` (`hAct`), `.sufficientA` (`hSuf`, at an active current SC); +* **`preemption_point_actA`**: `preemption_point_mainA` asking `hSuf` only at an active current SC. + +Generated by `handoff/tools/gen_preemptta.py` from `par/a64-p3-ut`'s `UtPp{Wu,Ts,Sc}A` / `UtClrA` (which state these +on `JStage3A`) and `P3/ScTimeA`. Named premises: the callers' (`NowAgreeA`, `CounterStepA`, `hNow`, `hAr` [P]). +New module. +-/ +import Fl4ProofsA64.P3.Stage3QKeepPresA +import Fl4ProofsA64.P3.Stage3SchedWrTA +import Fl4ProofsA64.P3.Stage3PreemptA +import Fl4ProofsA64.P3.Stage3RestartA +import Fl4ProofsA64.P3.Stage3EntryProjA +import Fl4ProofsA64.P3.ScTimeA +import Fl4ProofsA64.P3.ScRefillA +import Fl4ProofsA64.P3.ScSpecA +import Fl4ProofsA64.P1.QueueKeeps +import Fl4ProofsA64.P1.S1aArmTop + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P1 Fl4ProofsA64.P3 fl4_core_a64 + +noncomputable section + +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +private theorem pp_clockNodeWordsA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRN st Z m κ s σ) : + Aligned (L.nodeA s layout.node.CUR_TIME) ∧ Aligned (L.nodeA s layout.node.CONSUMED) ∧ + L.nodeA s layout.node.CUR_TIME ≠ L.nodeA s layout.node.CONSUMED := by + obtain ⟨nn, -, hkn, pcs⟩ := H.csN + have hCpu : (L.cpu s).val < 128 := by have := (pcs _).1 H.sch.cpuCs; omega + have e40 : layout.node.CONSUMED.val = 40 := by unfold layout.node.CONSUMED; rfl + have e48 : layout.node.CUR_TIME.val = 48 := by unfold layout.node.CUR_TIME; rfl + have c := nodeAt_valSc H.ksFit hCpu (by rw [e40]; decide : layout.node.CONSUMED.val < 4288) + have t := nodeAt_valSc H.ksFit hCpu (by rw [e48]; decide : layout.node.CUR_TIME.val < 4288) + change Aligned (L.nodeAt s (L.cpu s) layout.node.CUR_TIME) ∧ + Aligned (L.nodeAt s (L.cpu s) layout.node.CONSUMED) ∧ + L.nodeAt s (L.cpu s) layout.node.CUR_TIME ≠ L.nodeAt s (L.cpu s) layout.node.CONSUMED + have ha := H.ksb_al + unfold Aligned at ha ⊢ + rw [c, t, e40, e48] + refine ⟨by omega, by omega, ?_⟩ + intro e + have ev := congrArg (fun k : U64 => k.val) e + rw [c, t, e40, e48] at ev + omega + +set_option maxHeartbeats 800000 in +theorem update_timestamp_refinesTA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (E : YieldQueueRelA (st := st) Z m κ s σ) (a : A) {Clock : A → MachineState → Prop} + (P : NowAgreeA ar Clock) (hClock : Clock a σ.machineState) (hnd : numDomains = 1) + (V : ValidMachineTimeA σ) (C : CounterStepA σ) : + sched.update_timestamp st ar s a ⦃ r => ∃ τ, + fl4UpdateTimestamp σ = ({((), τ)}, False) ∧ YieldQueueRelA (st := st) Z m κ r.1 τ ∧ + Clock r.2 τ.machineState ∧ ValidMachineTimeA τ ∧ + τ.curTime = rawNextA σ.machineState ∧ τ.consumedTime = σ.consumedTime + (τ.curTime - σ.curTime) ∧ + MFrA (st := st) Z m s r.1 σ τ ⦄ := by + let H := E.sched.toSchedSRN + refine WP.spec_mono (update_timestamp_wordsA H a P hClock) ?_ + rintro ⟨s', a'⟩ ⟨now, hNow, hCl, hS⟩ + change s' = _ at hS + subst s' + let tAddr := L.nodeA s layout.node.CUR_TIME + let cAddr := L.nodeA s layout.node.CONSUMED + let used := core.num.U64.wrapping_add (L.mem s cAddr) (core.num.U64.wrapping_sub now (L.curTime s)) + let s1 := L.wr s tAddr now + let s2 := L.wr s1 cAddr used + let τ := fl4TimestampState (rawNextA σ.machineState) { σ with machineState := rawStateA σ.machineState } + obtain ⟨alT, alC, ne⟩ := pp_clockNodeWordsA H + have nv := fun e : tAddr.val = cAddr.val => ne (UScalar.eq_of_val_eq e) + have w : WritesIn st s s2 (fun b => b = tAddr ∨ b = cAddr) := + (writesIn_wr (st := st) (s := s) (v := now) alT).trans (writesIn_wr (st := st) (s := s1) (v := used) alC) + have oT : layout.node.CUR_TIME.val = 48 := by unfold layout.node.CUR_TIME; rfl + have oC : layout.node.CONSUMED.val = 40 := by unfold layout.node.CONSUMED; rfl + obtain ⟨nk, gK, tK⟩ := H.keepNodeWordsA (Offsets := fun o => o = layout.node.CUR_TIME ∨ o = layout.node.CONSUMED) + w (fun o h => by rcases h with rfl | rfl <;> simp [oT, oC]) + (fun b h => Or.inr (h.elim (fun e => ⟨_, Or.inl rfl, e⟩) (fun e => ⟨_, Or.inr rfl, e⟩))) + have nK (c : U64) (hc : c ∈ Z.Cs) (o : U64) (ho : o.val < 4288) (h8 : o.val % 8 = 0) + (h40 : o.val ≠ 40) (h48 : o.val ≠ 48) := + nk c hc o ho h8 (fun k h => by rcases h with rfl | rfl; rwa [oT]; rwa [oC]) + have tw (t : U64) (ht : t ∈ Z.T) (o : U64) + (ho : o = 3520#u64 ∨ o = 3448#u64 ∨ o = 3432#u64 ∨ o = 3464#u64 ∨ + o = 3592#u64 ∨ o = 3480#u64 ∨ o = 3488#u64) : + L.mem s2 (core.num.U64.wrapping_add t o) = L.mem s (core.num.U64.wrapping_add t o) := by + refine tK t ht o ?_ ?_ <;> rcases ho with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> decide + have keepSC : ∀ b, Aligned b → ScFootA Z.C m σ b → L.mem s2 b = L.mem s b := by + intro b hb hf + refine w.mem_eq hb ?_ + rintro (rfl | rfl) + · have he := H.node_in H.sch.cpuCs (o := layout.node.CUR_TIME) (by rw [oT]; decide) + have hh := H.scp.ks_out hf + change _ ≤ tAddr.val ∧ tAddr.val < _ at he + omega + · have he := H.node_in H.sch.cpuCs (o := layout.node.CONSUMED) (by rw [oC]; decide) + have hh := H.scp.ks_out hf + change _ ≤ cAddr.val ∧ cAddr.val < _ at he + omega + have hcore : SchedCoreA st Z.T Z.Cs s2 Z.Q Z.R := + H.core.agree w.1 (fun t ht => ⟨tw t ht 3480#u64 (by simp), tw t ht 3488#u64 (by simp)⟩) + (fun c hc o ho h8 hne => nK c hc o ho h8 (by omega) (by omega)) + (fun t ht => tw t ht 3592#u64 (by simp)) (fun t ht => tw t ht 3520#u64 (by simp)) + (fun t ht => tw t ht 3448#u64 (by simp)) + have hS2 := H.rebuildNodeTimeA (Q' := Z.Q) (R' := Z.R) (σ' := τ) (κ' := κ) w.1 w.2.1 + (fun c hc o h1 h2 _ h40 h48 _ => nK c hc o (by omega) h2 h40 h48) + (fun K hK => gK K (by rcases hK with rfl | rfl <;> decide) (by rcases hK with rfl | rfl <;> decide)) + (fun t ht K hK => tw t ht K (by rcases hK with rfl | rfl | rfl <;> simp)) + (H.scr.frame keepSC (fun _ _ => rfl)) (ScPlacedA.of_kheap H.scp (fun _ _ => rfl)) + (fun t ht x hx => ⟨x, hx, rfl, rfl⟩) + (fun t ht => by obtain ⟨x, hx, e⟩ := H.sch.tcb t ht; rw [scA_eq, tw t ht 3464#u64 (by simp), ← scA_eq]; exact ⟨x, hx, e⟩) + (fun t ht => by rw [scA_eq, tw t ht 3464#u64 (by simp), ← scA_eq]; exact H.rest.scMem t ht) + ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, by simp [τ, fl4TimestampState, hnd]⟩ + (by + change rawNextA σ.machineState = (L.mem s2 tAddr).bv + rw [L.mem_wr_ne alC alT (fun e => nv e.symm), L.mem_wr_same alT, hNow]) + (by + change σ.consumedTime + (rawNextA σ.machineState - σ.curTime) = (L.mem s2 cAddr).bv + rw [L.mem_wr_same alC] + unfold used + rw [core.num.U64.wrapping_add_bv_eq, core.num.U64.wrapping_sub_bv_eq, hNow, H.rest.consumed, H.sch.time] + rfl) + (by + change σ.reprogramTimer = _ + have e : layout.node.REPROGRAM.val = 56 := by unfold layout.node.REPROGRAM; rfl + rw [nK _ H.sch.cpuCs layout.node.REPROGRAM + (by rw [e] <;> decide) (by rw [e] <;> decide) (by rw [e] <;> decide) (by rw [e] <;> decide)] + exact H.rest.repr) + (by + change σ.schedulerAction = _ + have e : layout.node.SCHED_ACTION.val = 16 := by unfold layout.node.SCHED_ACTION; rfl + rw [nK _ H.sch.cpuCs layout.node.SCHED_ACTION + (by rw [e] <;> decide) (by rw [e] <;> decide) (by rw [e] <;> decide) (by rw [e] <;> decide)] + exact H.sch.act) + H.actOn (H.inv.of_kheap rfl rfl) hcore H.rest.rq H.rest.rqDom H.rest.rel H.run + (fun c hc => (keepSC _ (H.scCoreFoot hc).1 (H.scCoreFoot hc).2).trans (H.scCore c hc)) + have M : MFrA (st := st) Z m s s2 σ τ := by + refine ⟨w.mono (fun b h => Or.inr (Or.inl ?_)), SKeepA.of_kheap rfl, SSzA.of_kheap rfl⟩ + rcases h with rfl | rfl + · exact H.node_sks H.sch.cpuCs (by rw [oT]; decide) + · exact H.node_sks H.sch.cpuCs (by rw [oC]; decide) + obtain ⟨S2, M2, Y2⟩ := yieldQueueFrameA E.sched E.states E.mcp E.links (show τ.kheap = σ.kheap from rfl) M.wr + have rd : RdA τ := E.sched.rd.timeMonoA rfl rfl (BitVec.le_trans V C) + exact ⟨τ, fl4UpdateTimestamp_eqA σ, ⟨⟨hS2, rd⟩, S2, M2, Y2⟩, hCl, BitVec.le_refl _, + rfl, rfl, M⟩ + +end + +end Fl4ProofsA64.P3.Sched + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4ProofsA64.P1.W7.R.ST fl4_core_a64 + +noncomputable section + +section main +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +/-- **`preemption_point_mainA` with `hSuf` asked only at an active current SC** (contract §36: an inactive one takes +the "not active → preempt" branch). -/ +theorem preemption_point_actA (OS : Set U64) (R : S → A → State Unit → Prop) + (hnd : numDomains = 1) + (hW : ∀ s a σ v s1, R s a σ → obj.ks_set st s layout.ks.WORK_UNITS v = ok s1 → + R s1 a σ ∧ ∀ y ∈ OS, capA st s1 y = capA st s y) + (hTs : ∀ s a σ, R s a σ → sched.update_timestamp st ar s a ⦃ r => ∃ σ1, + ((), σ1) ∈ (updateTimeStamp (Z := Unit) σ).1 ∧ R r.1 r.2 σ1 ∧ ∀ y ∈ OS, capA st r.1 y = capA st s y ⦄) + (hIrq : ∀ s a σ, R s a σ → ar.irq_pending a ⦃ r => ∃ o σ2, + (o, σ2) ∈ (doMachineOp (getActiveIRQ true) σ).1 ∧ (r.1 = true ↔ ¬ o = none) ∧ R s r.2 σ2 ⦄) + (hAct : ∀ s a σ, R s a σ → sporadic.sc_active st s (L.cscW s) ⦃ b => + readScActive (Z := Unit) σ.curSc σ = some b ⦄) + (hSuf : ∀ s a σ, R s a σ → readScActive (Z := Unit) σ.curSc σ = some true → sporadic.sufficient st ar s a (L.cscW s) + (L.mem s (L.nodeA s layout.node.CONSUMED)) ⦃ ok1 => + (ok1 = true ↔ readScRefillSufficient (Z := Unit) σ.curSc σ.consumedTime σ = some true) ⦄) : + ∀ (s : S) (a : A) (σ : State Unit), R s a σ → + sched.preemption_point st ar s a ⦃ r => + (∀ y ∈ OS, capA st r.2.1 y = capA st s y) ∧ ∃ σ', + (r.1 = .Ok () ∧ ppMemA σ (.ok ()) σ' ∧ R r.2.1 r.2.2 σ') ∨ + (r.1 = .Err .Preempted ∧ ppMemA σ (.error ()) σ' ∧ R r.2.1 r.2.2 σ') ⦄ := by + intro s a σ hR + unfold sched.preemption_point + obtain ⟨i, hi⟩ : ∃ i, obj.ks_get st s layout.ks.WORK_UNITS = ok i := ⟨_, eq_of_spec_fd (obj.ks_get.spec s _)⟩ + rw [hi, bind_ok] + simp only [lift, bind_ok] + generalize core.num.U64.wrapping_add i 1#u64 = w + obtain ⟨s1, h1⟩ : ∃ s1, obj.ks_set st s layout.ks.WORK_UNITS w = ok s1 := + ⟨_, eq_of_spec_fd (obj.ks_set.spec s _ _)⟩ + rw [h1, bind_ok] + obtain ⟨hR1, hc1⟩ := hW s a σ w s1 hR h1 + split + · obtain ⟨s2, h2⟩ : ∃ s2, obj.ks_set st s1 layout.ks.WORK_UNITS 0#u64 = ok s2 := + ⟨_, eq_of_spec_fd (obj.ks_set.spec s1 _ _)⟩ + rw [h2, bind_ok] + obtain ⟨hR2, hc2⟩ := hW s1 a σ 0#u64 s2 hR1 h2 + refine WP.spec_bind (hTs s2 a σ hR2) ?_ + rintro ⟨s3, a1⟩ ⟨σ1, hσ1, hR3, hc3⟩ + refine WP.spec_bind (hIrq s3 a1 σ1 hR3) ?_ + rintro ⟨irq, a2⟩ ⟨o, σ2, hσ2, hirq, hR4⟩ + dsimp only at hR4 hirq + rw [eq_of_spec_fd (sched.cur_sc.spec s3)] + simp only [bind_ok] + rw [eq_of_spec_fd (sched.consumed.specA s3)] + simp only [bind_ok] + refine WP.spec_bind (hAct s3 a2 σ2 hR4) ?_ + intro b hb + have capsAll : ∀ y ∈ OS, capA st s3 y = capA st s y := fun y hy => by + rw [hc3 y hy, hc2 y hy, hc1 y hy] + have hdom : isCurDomainExpired σ2 = false := by + unfold isCurDomainExpired; simp [hnd] + have L1 := pp_spec_longA hσ1 hσ2 hb + rw [hdom] at L1 + by_cases hb' : b = true + · rw [if_pos hb'] + refine WP.spec_bind (hSuf s3 a2 σ2 hR4 (hb' ▸ hb)) ?_ + intro ok1 hok1 + by_cases hirq' : irq = true + · rw [if_pos hirq', WP.spec_ok] + refine ⟨capsAll, σ2, Or.inr ⟨rfl, L1.1 (Or.inl (fun h => ?_)), hR4⟩⟩ + · exact absurd (hirq.1 hirq') (by simpa using h) + · rw [if_neg hirq'] + rw [is_cur_domain_expired_eqA]; simp only [bind_ok, Bool.false_eq_true, ↓reduceIte] + by_cases hk : ok1 = true + · rw [if_pos hk, WP.spec_ok] + have hs := hok1.1 hk + refine ⟨capsAll, σ2, Or.inl ⟨rfl, L1.2 ?_, hR4⟩⟩ + rintro (h | h | h) + · exact hirq' (hirq.2 h) + · simp [hdom] at h + · exact h ⟨hb', hs⟩ + · rw [if_neg hk, WP.spec_ok] + refine ⟨capsAll, σ2, Or.inr ⟨rfl, L1.1 (Or.inr (Or.inr fun h => hk (hok1.2 h.2))), hR4⟩⟩ + · have hbf : b = false := by simpa using hb' + rw [if_neg hb', bind_ok] + by_cases hirq' : irq = true + · rw [if_pos hirq', WP.spec_ok] + refine ⟨capsAll, σ2, Or.inr ⟨rfl, L1.1 (Or.inl (fun h => ?_)), hR4⟩⟩ + exact absurd (hirq.1 hirq') (by simpa using h) + · rw [if_neg hirq'] + rw [is_cur_domain_expired_eqA]; simp only [bind_ok, Bool.false_eq_true, ↓reduceIte] + rw [WP.spec_ok] + refine ⟨capsAll, σ2, Or.inr ⟨rfl, L1.1 (Or.inr (Or.inr fun h => hb' h.1)), hR4⟩⟩ + · rw [WP.spec_ok] + exact ⟨hc1, σ, Or.inl ⟨rfl, pp_spec_shortA σ, hR1⟩⟩ + +end main + +end + +end Fl4ProofsA64.P3.Sched + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +set_option maxRecDepth 100000 + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {J7 : JSlotA S} {s s' : S} {a : A} {σ σ' : State Unit} {m : McState} + +variable (st) in +/-- **A J7 slot kept across a step that keeps the `OS` caps, the kernel-state base and its VSpace words.** -/ +class J7PpKA (J7 : JSlotA S) : Prop where + keepFr : ∀ Z H OS s s' σ σ' m, J7 Z H OS s σ m → L.ksb s' = L.ksb s → OSKeep st OS s s' → + (∀ Pt Ub : Set U64, FrUb st Ub OS s → + VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt → VsKeep st (L.ksb s) Pt s s') → + J7 Z H OS s' σ' m + +/-- `J7PpKA.keepFr` for a `VsKeep` free of the `FrUb` guard. -/ +theorem J7PpKA.keep {J7 : JSlotA S} [J7PpKA st J7] (Z : XSetsA) (H : Heap) (OS : Set U64) (s s' : S) + (σ σ' : State Unit) (m : McState) (hJ : J7 Z H OS s σ m) (kb : L.ksb s' = L.ksb s) (osk : OSKeep st OS s s') + (hv : ∀ Pt Ub : Set U64, VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt → VsKeep st (L.ksb s) Pt s s') : + J7 Z H OS s' σ' m := + J7PpKA.keepFr (st := st) Z H OS s s' σ σ' m hJ kb osk (fun Pt Ub _ => hv Pt Ub) + +instance j7PpA_j7A (R : Set U64 → JSlotA S) : J7PpA st (J7A st R) := + ⟨fun _ _ _ _ _ _ J => by + obtain ⟨Pt, Ub, vr, -, arm, -, -, -, -, link, vcks, -⟩ := J + exact ⟨Pt, Ub, vr, arm, link, vcks⟩⟩ + +instance j7PpA_j7LA (R : Set U64 → JSlotA S) : J7PpA st (J7LA st R) := + ⟨fun _ _ _ _ _ _ J => by + obtain ⟨Pt, Ub, vr, -, arm, -, -, -, -, link, vcks, -⟩ := J + exact ⟨Pt, Ub, vr, arm, link, vcks⟩⟩ + +/-- `SchedPageA` across a step keeping the `OS` caps. -/ +theorem pp_pageA {Z : XSetsA} {OS : Set U64} {s s' : S} (osk : OSKeep st OS s s') {p : U64} + (hp : SchedPageA st Z OS s' p) : SchedPageA st Z OS s p := by + unfold SchedPageA at hp ⊢ + rcases hp with hp | hp | hp | hp | hp | hp | hp | ⟨u, hu, hp⟩ + · exact Or.inl hp + · exact Or.inr (Or.inl hp) + · exact Or.inr (Or.inr (Or.inl hp)) + · exact Or.inr (Or.inr (Or.inr (Or.inl hp))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inl hp)))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl hp))))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl hp)))))) + · rw [osk.capA hu] at hp + exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ⟨u, hu, hp⟩)))))) + +instance j7PpKA_j7A {A : Type} (ar : machine.Arch A) (nA : U64) : J7PpKA st (J7A st (J7R2A st ar nA)) := + ⟨fun Z H OS s s' _ _ _ J kb osk hv => by + obtain ⟨Pt, Ub, vr, pn, arm, cc, vcc, dfa, hi, link, vcw, fu, ual, vcu, Pp', Tb', Hr, Te, Kt, Lv', Re, bits, R2, Kut, + KLv, ktK, hik⟩ := J + have vsr := hv Pt Ub fu vr + have tab : TabKeep st s s' := ⟨kb, fun i => by rw [kb]; exact vsr _ (Or.inr (asW_at _ i))⟩ + refine ⟨Pt, Ub, by rw [kb]; exact vr, PoolNamed.ofTab kb (fun j => tab.2 j) osk pn, S1aArmCol.ofTab tab arm, + S1aCapCol.ofOSK osk cc, CapInv.keepO vcc osk, CapInv.keepO dfa osk, + HiCap511.keep hi (fun y hy => osk.capA hy) (fun y hy p m a e => ?_), fun p hp => link p (pp_pageA osk hp), + by rw [kb]; exact vcw, FrUb.keepO fu osk, ual, vcu, Pp', Tb', Hr, Te, Kt, Lv', Re, bits, + R2.ofImage kb vsr (S1aSlotImage.ofSame R2.capRefs.refs fun _ hy => Or.inl (osk.capA hy)), Kut.keep osk, KLv, ktK, + S1aHiK.keep hik ⟨EdgeKeepOn.ofVsKeep R2 vsr, fun y hy R m a c => ⟨y, hy, m, a, by rwa [osk.capA hy] at c⟩⟩⟩ + · have live : p ∈ Hr := by rw [R2.highRoots]; exact ⟨y, hy, m, a, e⟩ + exact vsr _ (Or.inl ⟨p, R2.place.tables (R2.place.high.sub live), 4088#u64, by decide, by decide, rfl⟩)⟩ + +instance j7PpKA_j7LA {A : Type} (ar : machine.Arch A) (nA : U64) : J7PpKA st (J7LA st (J7R2A st ar nA)) := + ⟨fun Z H OS s s' _ _ _ J kb osk hv => by + obtain ⟨Pt, Ub, vr, pn, arm, cc, vcc, dfa, hi, link, vcw, fu, ual, vcu, Pp', Tb', Hr, Te, Kt, Lv', Re, bits, R2, Kut, + KLv, ktK, hik⟩ := J + have vsr := hv Pt Ub fu vr + have tab : TabKeep st s s' := ⟨kb, fun i => by rw [kb]; exact vsr _ (Or.inr (asW_at _ i))⟩ + have cc' : UbFr st OS Ub s' ∧ FrameOK st H OS s' := by + refine ⟨fun b hb => ?_, fun y hy ptr size rights x1 x2 x3 x4 hc => ?_⟩ + · obtain ⟨y, hy, ptr, size, rights, x1, x2, x3, x4, bits, hc, h⟩ := cc.1 b hb + exact ⟨y, hy, ptr, size, rights, x1, x2, x3, x4, bits, (osk.capA hy).trans hc, h⟩ + · exact cc.2 y hy ptr size rights x1 x2 x3 x4 ((osk.capA hy).symm.trans hc) + refine ⟨Pt, Ub, by rw [kb]; exact vr, PoolNamed.ofTab kb (fun j => tab.2 j) osk pn, S1aArmCol.ofTab tab arm, + cc', CapInv.keepO vcc osk, CapInv.keepO dfa osk, + HiCap511.keep hi (fun y hy => osk.capA hy) (fun y hy p m a e => ?_), fun p hp => link p (pp_pageA osk hp), + by rw [kb]; exact vcw, FrUb.keepO fu osk, ual, vcu, Pp', Tb', Hr, Te, Kt, Lv', Re, bits, + R2.ofImage kb vsr (S1aSlotImage.ofSame R2.capRefs.refs fun _ hy => Or.inl (osk.capA hy)), Kut.keep osk, KLv, ktK, + S1aHiK.keep hik ⟨EdgeKeepOn.ofVsKeep R2 vsr, fun y hy R m a c => ⟨y, hy, m, a, by rwa [osk.capA hy] at c⟩⟩⟩ + · have live : p ∈ Hr := by rw [R2.highRoots]; exact ⟨y, hy, m, a, e⟩ + exact vsr _ (Or.inl ⟨p, R2.place.tables (R2.place.high.sub live), 4088#u64, by decide, by decide, rfl⟩)⟩ + +/-- `EntryInvA3` across a chunk step (`utFr_entA` at the narrowed VSpace typing). -/ +theorem ppEntA3 {H : Heap} {Cs OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} {s s' : S} + (h : EntryInvA3 st H Cs OS U Ac Pp Tb Lv s) + (hb : BlkOK st H.tcbs Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s') (hk : L.ksb s' = L.ksb s) + (hvs : VsTyR st (L.ksb s') (PoolSet st Pp s') ∅ Tb Lv s') : EntryInvA3 st H Cs OS U Ac Pp Tb Lv s' := by + obtain ⟨b1, b2, b3, b4, b5⟩ := hb + exact ⟨h.heap, ⟨b1, b2, b3, b5, b4⟩, h.os, h.user.ksb hk, hvs, by rw [hk]; exact h.ksw⟩ + +/-- The VSpace words are kept by a scheduler step. -/ +theorem JStage3TA.pp_vsrA {J5 J7 : JSlotA S} [J7PpA st J7] + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (wS : WritesIn st s s' (SchedStepWA (st := st) Z m σ s)) : + ∀ Pt Ub : Set U64, VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt → VsKeep st (L.ksb s) Pt s s' := by + intro Pt Ub vr b hb + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.ent.ksw + refine wS.mem_eq (vr.al _ hb) ?_ + by_cases h : AsW (L.ksb s) b + · obtain ⟨o, o1, o2, -, e⟩ := h + have ev : b.val = (L.ksb s).val + o.val := by + rw [e, wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact J.asid_offStepA (by omega) (by omega) + · exact J.vsr_offStepA vr hb h + +/-- The kinds of word the record reads outside the cap slots. -/ +inductive PpWhereA (st : machine.Store S) [L : LawfulStore st] (Z : XSetsA) (H : Heap) (m : McState) (s : S) + (σ : State Unit) (a : U64) : Prop + | tcb (t : U64) : t ∈ Z.T → t.val + 224 ≤ a.val → a.val < t.val + 4096 → PpWhereA st Z H m s σ a + | ibuf (t : U64) : t ∈ Z.T → a = sbWordA t ∨ a = wa (sbWordA t) 8#u64 → PpWhereA st Z H m s σ a + | other (q : U64) (len : Nat) : Z.th q = some (.other len) → q.val ≤ a.val → a.val < q.val + len → + PpWhereA st Z H m s σ a + | sc (c : U64) (x : SchedContext) (k : Nat) : c ∈ Z.C → σ.kheap (scNameA m c) = some (.schedContext x k) → + ScObjA c k a → PpWhereA st Z H m s σ a + | node (c o : U64) : c ∈ Z.Cs → o.val < 4288 → a = L.nodeAt s c o → PpWhereA st Z H m s σ a + | glob (o : U64) : o.val = 8 ∨ o.val = 16 → a = wa (L.ksb s) o → PpWhereA st Z H m s σ a + + +section +variable {J5 : JSlotA S} (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) +include J + +theorem pp_tcbThA {t : U64} (ht : t ∈ Z.T) : Z.th t = some .tcb ∧ t.val + 4096 ≤ 2 ^ 64 := by + have h := (J.j.1.thT t).2 ht + exact ⟨h, J.j.1.place.fit t _ h⟩ + +theorem pp_tcbFieldA {t K : U64} (ht : t ∈ Z.T) (h1 : 224 ≤ K.val) (h2 : K.val < 4096) : + PpWhereA st Z H m s σ (wa t K) := by + have hf := (pp_tcbThA J ht).2 + have e : (wa t K).val = t.val + K.val := by + rw [wa_valP]; exact Nat.mod_eq_of_lt (by omega) + exact .tcb t ht (by omega) (by omega) + +/-- **Every word of `UtRdA` is of one of these kinds.** -/ +theorem pp_rd_whereA {b : U64} (h : UtRdA st Z N E Rp Bf m s σ b) : PpWhereA st Z H m s σ b := by + have tf : ∀ t ∈ Z.T, ∀ K : U64, 224 ≤ K.val → K.val < 4096 → PpWhereA st Z H m s σ (wa t K) := + fun t ht K h1 h2 => pp_tcbFieldA J ht h1 h2 + have other : ∀ q len, Z.th q = some (.other len) → ∀ c : U64, q.val ≤ c.val → c.val < q.val + len → + PpWhereA st Z H m s σ c := fun q len h c h1 h2 => .other q len h h1 h2 + rcases h with h | h | h | ⟨r, hr, h⟩ | ⟨t, ht, h⟩ + · rcases h with ⟨c, hc, o, ho, e⟩ | e | e | ⟨t, ht, K, hK, e⟩ | h + · exact .node c o hc ho e + · exact .glob 8#u64 (Or.inl rfl) e + · exact .glob 16#u64 (Or.inr rfl) e + · rw [e] + rcases hK with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> exact tf t ht _ (by simp) (by simp) + · obtain ⟨c, hc, x, k, hk, ha⟩ := h + exact .sc c x k hc hk ha + · rcases h with ⟨e, he, hv⟩ | ⟨r, hr, hv⟩ | ⟨t, ht, hv⟩ | hv + · exact other e _ (J.ipcPlace.endpoint e he) b (by omega) (by omega) + · exact other r _ (J.ipcPlace.reply r hr) b (by omega) (by omega) + · have hf := (pp_tcbThA J ht).2 + rcases hv with ⟨h1, h2⟩ | h2 + · refine .tcb t ht ?_ ?_ <;> (unfold TOffN at h2; omega) + · unfold TL at h2 + refine .tcb t ht ?_ ?_ <;> omega + · obtain ⟨q, len, h1, h2, h3⟩ := J.ipcPlace.buffer b hv + exact other q len h1 b h2 h3 + · rcases h with ⟨c, hc, h1, h2⟩ | ⟨t, ht, e⟩ + · exact other c _ (J.ipcPlace.notification c hc) b h1 (by omega) + · rcases e with e | e | e | e | e | e <;> rw [e] + · rw [stA_eq]; exact tf t ht _ (by simp) (by simp) + · exact tf t ht 3336#u64 (by simp) (by simp) + · have eb : badgeRegA t = wa t 256#u64 := by + unfold badgeRegA layout.tcb.REGS layout.reg.BADGE layout.reg.X0 + apply UScalar.eq_of_val_eq + simp [core.num.U64.wrapping_add_val_eq, core.num.U64.wrapping_mul_val_eq, U64.size_def, U64.numBits] + rw [eb]; exact tf t ht _ (by simp) (by simp) + · exact tf t ht 3448#u64 (by simp) (by simp) + · exact tf t ht 3496#u64 (by simp) (by simp) + · exact tf t ht 3504#u64 (by simp) (by simp) + · have hth := J.ipcPlace.reply r hr + have hf := J.j.1.place.fit r _ hth + simp only [ObjKindA.size] at hf + have e8 : (rPrevA r).val = r.val + 8 := by + unfold rPrevA layout.reply.PREV + rw [wa_valP]; exact Nat.mod_eq_of_lt (by simp; omega) + have e16 : (rNextA r).val = r.val + 16 := by + unfold rNextA layout.reply.NEXT + rw [wa_valP]; exact Nat.mod_eq_of_lt (by simp; omega) + rcases h with e | e <;> rw [e] + · exact other r _ hth _ (by omega) (by omega) + · exact other r _ hth _ (by omega) (by omega) + · rcases h with e | e | e | e | e | e | e + · rw [e]; exact tf t ht 3440#u64 (by simp) (by simp) + · rw [e]; exact tf t ht 3472#u64 (by simp) (by simp) + · exact .ibuf t ht (Or.inl e) + · exact .ibuf t ht (Or.inr e) + · rw [e]; exact tf t ht 3512#u64 (by simp) (by simp) + · rw [e]; exact tf t ht 3664#u64 (by simp) (by simp) + · rw [e]; exact tf t ht 3456#u64 (by simp) (by simp) + +theorem pp_sbValA {t : U64} (ht : t ∈ Z.T) : + (sbWordA t).val = t.val + 64 ∧ (wa (sbWordA t) 8#u64).val = t.val + 72 := by + have hf := (pp_tcbThA J ht).2 + have e : sbWordA t = wa t 64#u64 := by + unfold sbWordA layout.tcb.SLOT_BUFFER layout.cte.SIZE + apply UScalar.eq_of_val_eq + simp [core.num.U64.wrapping_add_val_eq, core.num.U64.wrapping_mul_val_eq, U64.size_def, U64.numBits] + have e1 : (sbWordA t).val = t.val + 64 := by + rw [e, wa_valP]; exact Nat.mod_eq_of_lt (by simp; omega) + refine ⟨e1, ?_⟩ + rw [wa_valP, e1]; exact Nat.mod_eq_of_lt (by simp; omega) + +end + +theorem pp_j5A + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + (osk : OSKeep st OS s s') (kb : L.ksb s' = L.ksb s) + (hirq : ∀ i : U64, i.val < 5376 → L.mem s' (irqStAddrA (L.ksb s) i) = L.mem s (irqStAddrA (L.ksb s) i)) + (σ' : State Unit) : J5A st Z H OS s' σ' m := by + have hTr : TreeA st OS Z.G.chains Z.G.par s := + J.j.1.cdt.tree.grow J.j5.2.1 (fun x hx hn => J.j5.2.2 x hx hn) + have j5 : J5A st Z H OS s' σ' m := + ⟨⟨MdbInvA.keepO hTr.lk hTr.tree J.j5.1.1 osk, IrqRunA.keepO hTr.lk J.j5.1.2.1 osk, + HandlersActiveA.keepO hTr.lk J.j5.1.2.2 osk kb hirq⟩, J.j5.2.1, + fun y hy hn => by rw [osk.capA hy]; exact J.j5.2.2 y hy hn⟩ + exact j5 + +/-- **The record's words outside the scheduler's are in a TCB or an `other` object of the typed heap** (the +non-scheduler disjuncts of `pp_rd_whereA`). -/ +theorem pp_objA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + {b : U64} + (h : EFoot Z.T E Rp Bf b ∨ NFoot N Z.T b ∨ (∃ r ∈ Rp, b = rNextA r ∨ b = rPrevA r) ∨ + ∃ t ∈ Z.T, b = mcpA t ∨ b = yieldA t ∨ b = sbWordA t ∨ b = wa (sbWordA t) 8#u64 ∨ b = wa t 3512#u64 ∨ + b = wa t 3664#u64 ∨ b = flagsA t) : + (∃ t ∈ Z.T, t.val ≤ b.val ∧ b.val < t.val + 4096) ∨ + ∃ q len, Z.th q = some (.other len) ∧ q.val ≤ b.val ∧ b.val < q.val + len := by + have tcb : ∀ t ∈ Z.T, ∀ {c : U64}, t.val + 224 ≤ c.val → c.val < t.val + 4096 → + (∃ t ∈ Z.T, t.val ≤ c.val ∧ c.val < t.val + 4096) ∨ + ∃ q len, Z.th q = some (.other len) ∧ q.val ≤ c.val ∧ c.val < q.val + len := + fun t ht c h1 h2 => Or.inl ⟨t, ht, by omega, h2⟩ + have other : ∀ q len, Z.th q = some (.other len) → ∀ c : U64, q.val ≤ c.val → c.val < q.val + len → + (∃ t ∈ Z.T, t.val ≤ c.val ∧ c.val < t.val + 4096) ∨ + ∃ q len, Z.th q = some (.other len) ∧ q.val ≤ c.val ∧ c.val < q.val + len := + fun q len h c h1 h2 => Or.inr ⟨q, len, h, h1, h2⟩ + have tf : ∀ t ∈ Z.T, ∀ K : U64, 224 ≤ K.val → K.val < 4096 → + (∃ t' ∈ Z.T, t'.val ≤ (wa t K).val ∧ (wa t K).val < t'.val + 4096) ∨ + ∃ q len, Z.th q = some (.other len) ∧ q.val ≤ (wa t K).val ∧ (wa t K).val < q.val + len := by + intro t ht K h1 h2 + have hf := (pp_tcbThA J ht).2 + have e : (wa t K).val = t.val + K.val := by rw [wa_valP]; exact Nat.mod_eq_of_lt (by omega) + exact Or.inl ⟨t, ht, by omega, by omega⟩ + have ibuf : ∀ t ∈ Z.T, b = sbWordA t ∨ b = wa (sbWordA t) 8#u64 → + (∃ t ∈ Z.T, t.val ≤ b.val ∧ b.val < t.val + 4096) ∨ + ∃ q len, Z.th q = some (.other len) ∧ q.val ≤ b.val ∧ b.val < q.val + len := by + intro t ht h + have g := pp_sbValA J ht + refine Or.inl ⟨t, ht, ?_, ?_⟩ <;> rcases h with h | h <;> rw [h] <;> omega + rcases h with h | h | ⟨r, hr, h⟩ | ⟨t, ht, h⟩ + · rcases h with ⟨e, he, hv⟩ | ⟨r, hr, hv⟩ | ⟨t, ht, hv⟩ | hv + · exact other e _ (J.ipcPlace.endpoint e he) b (by omega) (by omega) + · exact other r _ (J.ipcPlace.reply r hr) b (by omega) (by omega) + · have hf := (pp_tcbThA J ht).2 + rcases hv with ⟨h1, h2⟩ | h2 + · refine tcb t ht ?_ ?_ <;> (unfold TOffN at h2; omega) + · unfold TL at h2 + refine tcb t ht ?_ ?_ <;> omega + · obtain ⟨q, len, h1, h2, h3⟩ := J.ipcPlace.buffer b hv + exact other q len h1 b h2 h3 + · rcases h with ⟨c, hc, h1, h2⟩ | ⟨t, ht, e⟩ + · exact other c _ (J.ipcPlace.notification c hc) b h1 (by omega) + · rcases e with e | e | e | e | e | e <;> rw [e] + · rw [stA_eq]; exact tf t ht _ (by simp) (by simp) + · exact tf t ht 3336#u64 (by simp) (by simp) + · have eb : badgeRegA t = wa t 256#u64 := by + unfold badgeRegA layout.tcb.REGS layout.reg.BADGE layout.reg.X0 + apply UScalar.eq_of_val_eq + simp [core.num.U64.wrapping_add_val_eq, core.num.U64.wrapping_mul_val_eq, U64.size_def, U64.numBits] + rw [eb]; exact tf t ht _ (by simp) (by simp) + · exact tf t ht 3448#u64 (by simp) (by simp) + · exact tf t ht 3496#u64 (by simp) (by simp) + · exact tf t ht 3504#u64 (by simp) (by simp) + · have hth := J.ipcPlace.reply r hr + have hf := J.j.1.place.fit r _ hth + simp only [ObjKindA.size] at hf + have e8 : (rPrevA r).val = r.val + 8 := by + unfold rPrevA layout.reply.PREV + rw [wa_valP]; exact Nat.mod_eq_of_lt (by simp; omega) + have e16 : (rNextA r).val = r.val + 16 := by + unfold rNextA layout.reply.NEXT + rw [wa_valP]; exact Nat.mod_eq_of_lt (by simp; omega) + rcases h with e | e <;> rw [e] + · exact other r _ hth _ (by omega) (by omega) + · exact other r _ hth _ (by omega) (by omega) + · rcases h with e | e | e | e | e | e | e + · rw [e]; exact tf t ht 3440#u64 (by simp) (by simp) + · rw [e]; exact tf t ht 3472#u64 (by simp) (by simp) + · exact ibuf t ht (Or.inl e) + · exact ibuf t ht (Or.inr e) + · rw [e]; exact tf t ht 3512#u64 (by simp) (by simp) + · rw [e]; exact tf t ht 3664#u64 (by simp) (by simp) + · rw [e]; exact tf t ht 3456#u64 (by simp) (by simp) + +set_option maxHeartbeats 2000000 in +/-- **`JStage3A` across the `WORK_UNITS` write** (module docstring). -/ +theorem JStage3TA.workUnitsA [J7PpA st J7] [J7PpKA st J7] + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + (hvw : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → Aligned b ∧ b ≠ wa (L.ksb s) layout.ks.WORK_UNITS) (v : U64) : + JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 + (L.wr s (wa (L.ksb s) layout.ks.WORK_UNITS) v) a σ m ∧ + OSKeep st OS s (L.wr s (wa (L.ksb s) layout.ks.WORK_UNITS) v) := by + obtain ⟨wu, ewu⟩ : ∃ wu, wu = wa (L.ksb s) layout.ks.WORK_UNITS := ⟨_, rfl⟩ + rw [← ewu] + have hblk := BXI.blk J.ent.bxi + obtain ⟨ha, hΩ, hnS⟩ := ks_work_units hblk + rw [← ewu] at ha hΩ hnS + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.ent.ksw + have e40 : (layout.ks.WORK_UNITS : U64).val = 40 := by unfold layout.ks.WORK_UNITS; rfl + have wv : wu.val = (L.ksb s).val + 40 := by + rw [ewu, wa_valP, e40]; exact Nat.mod_eq_of_lt (by omega) + have kb : L.ksb (L.wr s wu v) = L.ksb s := L.ksb_wr _ _ _ + have hw : WritesIn st s (L.wr s wu v) (· = wu) := writesIn_wr ha + have memk : ∀ b, Aligned b → b ≠ wu → L.mem (L.wr s wu v) b = L.mem s b := fun b hb ne => hw.mem_eq hb ne + have C := LvC.of_blk hblk + -- the system invariants + have blk2 := blk_capw_wr (st := st) (v := v) hblk ha (Or.inr ⟨hΩ, hnS⟩) + have hvsT := (utVsPt_keepA (s' := L.wr s wu v) J.ent.vs kb (fun b hb => by + have h := hvw b hb + rw [← ewu] at h + exact memk b h.1 h.2)).1 + -- heap objects other than `other` ones are off the state block + have offK : ∀ q k, H q = some k → (∀ y, k ≠ .other y) → ∀ b : U64, q.val ≤ b.val → b.val < q.val + 2 ^ k.bits → + b ≠ wu := by + intro q k hq ne b h1 h2 e + rw [e, wv] at h1 h2 + rcases J.ko.1 q k hq ne with g | g <;> omega + -- the write is off the record's words + have hW : ∀ b, b = wu → ¬ UtRdA st Z N E Rp Bf m s σ b := by + intro b e hrd + have hom := C.om wu hΩ + have tcbOff : ∀ t ∈ Z.T, ¬ (t.val ≤ b.val ∧ b.val < t.val + 4096) := by + intro t ht h + have htH : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + rw [e] at h + rcases hom.2.1 t htH with g | g <;> omega + cases pp_rd_whereA J hrd with + | tcb t ht h1 h2 => exact tcbOff t ht ⟨by omega, h2⟩ + | ibuf t ht h => + have g := pp_sbValA J ht + refine tcbOff t ht ?_ + rcases h with h | h <;> rw [h] <;> omega + | other q len hq h1 h2 => + have g := J.j.1.place.ks q _ hq (fun h => by cases J.j.1.place.irqK hq h) + simp only [ObjKindA.size, ksSizeA] at g + rw [e, wv] at h1 h2 + omega + | sc c y k hc hk h => + have hcH : c ∈ H.scs := by rw [J.scs]; exact hc + obtain ⟨z, hz⟩ := hcH + have hsz := (J.scSize c hc y k hk).1 + have eb : H.scBits c = z := by simp [Heap.scBits, hz] + rw [eb] at hsz + obtain ⟨g1, g2⟩ := h + refine offK c _ hz (fun _ h => by cases h) b g1 (lt_of_lt_of_le g2 ?_) e + exact Nat.add_le_add_left (Nat.pow_le_pow_right (by norm_num) hsz) _ + | node c o hc ho h => + have v' : b.val = L.nodeBase s c + o.val := by rw [h]; exact nodeAt_val (J.sq.nodes c hc) ho + have hc' : c ∈ Z.Cs := hc + rcases hom.2.2 c hc' with g | g <;> (rw [e] at v'; omega) + | glob o ho h => + have v' : b.val = (L.ksb s).val + o.val := by + rw [h, wa_valP]; exact Nat.mod_eq_of_lt (by omega) + rw [e, wv] at v' + omega + have H2 : UtCnStepA σ σ := UtCnStepA.refl σ + have wS : WritesIn st s (L.wr s wu v) (SchedStepWA (st := st) Z m σ s) := + hw.mono fun b e => Or.inl (Or.inr (Or.inl (Or.inr (Or.inl (by rw [e, wv]; omega))))) + have osk : OSKeep st OS s (L.wr s wu v) := J.osKeep_stepA wS + -- the scheduler queues + have sq2 : SchedQueuesInv st H.tcbs Z.Cs H.omega H (L.wr s wu v) Z.Q Z.R := by + have hom := C.om wu hΩ + refine utFr_sqA (Sg := H.frame) J.sq hw (fun y hy K h1 h2 h3 => ?_) (fun c hc o ho h8 => ?_) (fun b hb => ?_) (fun q hq => ?_) + · have f := J.ent.heap.fits y .tcb hy + have al := J.ent.heap.align y .tcb hy + simp only [HKind.bits] at f al + have ev : (wa y K).val = y.val + K.val := by rw [wa_valP]; exact Nat.mod_eq_of_lt (by omega) + refine ⟨by unfold Aligned; rw [ev]; omega, fun e => ?_⟩ + have g := congrArg UScalar.val e + rw [ev] at g + rcases hom.2.1 y hy with k | k <;> omega + · refine ⟨nodeAt_aligned (J.sq.nodes c hc) ho h8, fun e => ?_⟩ + have g : (L.nodeAt s c o).val = L.nodeBase s c + o.val := nodeAt_val (J.sq.nodes c hc) ho + rw [e] at g + rcases hom.2.2 c hc with k | k <;> omega + · have g := C.sub b hb + exact ⟨(C.om b g).1, fun e => hnS (e ▸ hb)⟩ + · have hk : ∃ k, H q = some k ∧ (∀ y, k ≠ .other y) ∧ 4 ≤ k.bits := by + rcases hq with h | h + · exact ⟨_, h, fun _ g => (by cases g), by simp [HKind.bits]⟩ + · exact ⟨_, h, fun _ g => (by cases g), by simp [HKind.bits]⟩ + obtain ⟨k, hk, ne, b4⟩ := hk + have al := J.ent.heap.align q k hk + have f := J.ent.heap.fits q k hk + have h16 : 2 ^ 4 ≤ 2 ^ k.bits := Nat.pow_le_pow_right (by decide) b4 + have h8 : 8 ∣ q.val := by + have : 2 ^ 3 ∣ 2 ^ k.bits := Nat.pow_dvd_pow 2 (by omega) + exact (show (8 : Nat) ∣ 2 ^ k.bits from this).trans (Nat.dvd_of_mod_eq_zero al) + have eh : (epHeadA q).val = q.val := by simp [epHeadA] + have et : (epTSA q).val = q.val + 8 := by + unfold epTSA + have e8 : (layout.ep.TAIL_STATE : U64).val = 8 := by unfold layout.ep.TAIL_STATE; rfl + rw [wa_valP, e8] + exact Nat.mod_eq_of_lt (by omega) + exact ⟨by unfold Aligned; rw [eh]; omega, offK q k hk ne _ (by omega) (by omega), + by unfold Aligned; rw [et]; omega, offK q k hk ne _ (by omega) (by omega)⟩ + have hTable (i : U64) : L.mem (L.wr s wu v) (wa (L.ksb s) (AtOff i)) = L.mem s (wa (L.ksb s) (AtOff i)) := by + have h := hvw _ (.inr (asW_at _ i)) + rw [← ewu] at h + exact memk _ h.1 h.2 + have tAl : ∀ t ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → Aligned (wa t K) := fun t ht K h1 h2 => + field_aligned (J.sr.core.tcbs t ht) h1 h2 + have sbAl : ∀ t ∈ Z.T, Aligned (wa t 3512#u64) ∧ Aligned (sbWordA t) ∧ Aligned (wa (sbWordA t) 8#u64) := by + intro t ht + have g := pp_sbValA J ht + have al := (J.sr.core.tcbs t ht).align + refine ⟨tAl t ht _ (by simp) (by simp), by unfold Aligned; rw [g.1]; omega, by unfold Aligned; rw [g.2]; omega⟩ + have rAl : ∀ r ∈ Rp, Aligned (rNextA r) ∧ Aligned (rPrevA r) := by + intro r hr' + obtain ⟨g1, -, g3⟩ := J.iplaced.e.rep r hr' + have e8 : (rPrevA r).val = r.val + 8 := by + unfold rPrevA layout.reply.PREV + rw [wa_valP]; exact Nat.mod_eq_of_lt (by simp; omega) + have e16 : (rNextA r).val = r.val + 16 := by + unfold rNextA layout.reply.NEXT + rw [wa_valP]; exact Nat.mod_eq_of_lt (by simp; omega) + constructor <;> show _ % 8 = 0 <;> omega + have hTr : TreeA st OS Z.G.chains Z.G.par s := + J.j.1.cdt.tree.grow J.j5.2.1 (fun x hx hn => J.j5.2.2 x hx hn) + have j5 : J5A st Z H OS (L.wr s wu v) σ m := + ⟨⟨MdbInvA.keepO hTr.lk hTr.tree J.j5.1.1 osk, IrqRunA.keepO hTr.lk J.j5.1.2.1 osk, + HandlersActiveA.keepO hTr.lk J.j5.1.2.2 osk kb (fun i hi => by + have eI : irqStAddrA (L.ksb s) i = irqWordA (L.ksb s) i := rfl + have hv := irqWordA_val J.sr.ksFit hi + have hal : Aligned (irqWordA (L.ksb s) i) := by + have := J.sr.toSchedSRN.ksb_al + show _ % 8 = 0 + omega + rw [eI] + exact memk _ hal (fun e => by rw [e, wv] at hv; omega))⟩, J.j5.2.1, + fun y hy hn => by rw [osk.capA hy]; exact J.j5.2.2 y hy hn⟩ + have j7 : J7 Z H OS (L.wr s wu v) σ m := + J7PpKA.keep (st := st) _ _ _ _ _ σ σ _ J.j7 kb osk (J.pp_vsrA wS) + refine ⟨?_, osk⟩ + have tK : ∀ t ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → L.mem (L.wr s wu v) (wa t K) = L.mem s (wa t K) := by + intro t ht K h1 h2 + have hta := J.sr.core.tcbs t ht + refine memk _ (tAl t ht K h1 h2) (fun e => ?_) + have htH : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + have ev : (wa t K).val = t.val + K.val := by + have f := hta.fits + rw [wa_valP]; exact Nat.mod_eq_of_lt (by omega) + rw [e, wv] at ev + rcases (C.om wu hΩ).2.1 t htH with g | g <;> omega + have runVcpu : RunVcpuInv st Z.T (L.wr s wu v) := J.runVcpu.keep + (fun t ht => by rw [stA_eq]; exact tK t ht _ (by simp) (by simp)) + (fun t ht => tK t ht 3672#u64 (by simp) (by simp)) + have vr' : ∀ sc ∈ Z.C, ∀ r ∈ stack sc, RBlk st (L.wr s wu v) r := J.vr_keepA + (fun r hr => by + obtain ⟨e, al⟩ := J.rTcbValA hr + have rep := J.iplaced.e.rep r hr + exact memk _ al (offK r .reply (J.utIpc.reply r hr) (fun _ h => by cases h) _ (by omega) + (by simp only [HKind.bits]; omega))) + (fun t ht => by rw [stA_eq]; exact tK t ht _ (by simp) (by simp)) + exact JStage3TA.of_schedWr (blkQ := J.blkQ.keepA (fun t ht => tK t ht 3336#u64 (by simp) (by simp)) + (fun _ _ x' hx => Or.inr ⟨x', hx, rfl⟩) (fun _ _ h => h) (fun _ _ h => h)) J (Q := Z.Q) (Rl := Z.R) (curOf := Z.curOf) (curScOf := Z.curScOf) wS + (SpecSchedA.refl σ) (SSzA.refl σ) (QKeepA.refl σ) rfl J.j.1.ar (fun _ _ => rfl) J.bufAl + (ppEntA3 J.ent blk2 kb hvsT) sq2 (poolOff_keepTableA (Z := Z) J.poolOff kb hTable) + (utFr_schedSRA hw hW H2 J.sr) J.scCov (utFr_epA hw hW H2 J.ep J.iplaced.e) + (utFr_ntfnA hw hW H2 J.ntfn J.iplaced.n) (utFr_replyA hw hW H2 J.reply rAl) + (utFr_bufCoverA hw hW H2 J.bufCover sbAl) J.idleNoFault + (utFr_mcpA hw hW H2 J.mcp fun t ht => ⟨tAl t ht _ (by simp) (by simp), tAl t ht _ (by simp) (by simp)⟩) + (utFr_yieldA hw hW H2 J.yieldRel fun t ht => tAl t ht _ (by simp) (by simp)) J.sinv J.sidle runVcpu vr' J.noSwitch J.sact j5 j7 + +/-- (Task 0, Architect 12:42Z; replaces item 11's unguarded `CurEntryA`) **The entry facts at a timestamp** with no time consumed yet, **at an +active current SC**: its head is ready and no release-queue member is bound to it (`update_timestamp` makes the +consumed time positive, which `ZeroConsA` and `CurRdyA` then talk about). The guard makes a step that deactivates the +current SC (its deletion) keep it trivially; an inactive current SC has no queued TCB (`ScInvA.relSc`). Established at +entry from `CurInvA` / `OffReadyA` / `ScInvA.back` (`CurEntryGA.of_cur`) and carried by the Ut reset loop and the +deletion loop (`DelPpTimeA`, kept by `CurKeepA` steps). -/ +def CurEntryGA (σ : Fl4SpecA64.State Unit) : Prop := + σ.consumedTime = 0 → ∀ y k, σ.kheap σ.curSc = some (.schedContext y k) → Fl4SpecA64.scActive y = true → + Fl4SpecA64.refillReady σ.curTime (Fl4SpecA64.refillHd y) = true ∧ + ∀ t x, σ.kheap t = some (.tcb x) → x.tcbSchedContext = some σ.curSc → t ∉ σ.releaseQueue + +/-- (Task 0) **A step keeping `CurEntryGA`'s facts** (time apart): the current SC and consumed time are kept, the +release queue only shrinks, an active current SC was active with the same head refill, and a queued TCB bound to the +current SC was bound to it. -/ +structure CurKeepA (σ σ' : Fl4SpecA64.State Unit) : Prop where + csc : σ'.curSc = σ.curSc + cons : σ'.consumedTime = σ.consumedTime + rls : ∀ t ∈ σ'.releaseQueue, t ∈ σ.releaseQueue + sc : ∀ y' k', σ'.kheap σ.curSc = some (.schedContext y' k') → Fl4SpecA64.scActive y' = true → + ∃ y k, σ.kheap σ.curSc = some (.schedContext y k) ∧ Fl4SpecA64.scActive y = true ∧ + Fl4SpecA64.refillHd y = Fl4SpecA64.refillHd y' + tcb : ∀ t x', σ'.kheap t = some (.tcb x') → x'.tcbSchedContext = some σ.curSc → t ∈ σ'.releaseQueue → + ∃ x, σ.kheap t = some (.tcb x) ∧ x.tcbSchedContext = some σ.curSc + +theorem CurKeepA.refl (σ : Fl4SpecA64.State Unit) : CurKeepA σ σ := + ⟨rfl, rfl, fun _ h => h, fun y k h a => ⟨y, k, h, a, rfl⟩, fun _ x h e _ => ⟨x, h, e⟩⟩ + +/-- A step keeping the heap, the current SC, the consumed time and the release queue. -/ +theorem CurKeepA.of_eq {σ σ' : Fl4SpecA64.State Unit} (hk : σ'.kheap = σ.kheap) (hc : σ'.curSc = σ.curSc) + (hn : σ'.consumedTime = σ.consumedTime) (hr : σ'.releaseQueue = σ.releaseQueue) : CurKeepA σ σ' := + ⟨hc, hn, fun _ h => hr ▸ h, fun y k h a => ⟨y, k, hk ▸ h, a, rfl⟩, + fun _ x h e r => ⟨x, hk ▸ h, e⟩⟩ + +theorem CurKeepA.trans {σ σ' σ'' : Fl4SpecA64.State Unit} (h1 : CurKeepA σ σ') (h2 : CurKeepA σ' σ'') : + CurKeepA σ σ'' := by + refine ⟨h2.csc.trans h1.csc, h2.cons.trans h1.cons, fun t h => h1.rls t (h2.rls t h), fun y k h a => ?_, + fun t x h e r => ?_⟩ + · rw [← h1.csc] at h + obtain ⟨y1, k1, h1', a1, e1⟩ := h2.sc y k h a + rw [h1.csc] at h1' + obtain ⟨y0, k0, h0, a0, e0⟩ := h1.sc y1 k1 h1' a1 + exact ⟨y0, k0, h0, a0, e0.trans e1⟩ + · rw [← h1.csc] at e + obtain ⟨x1, hx1, e1⟩ := h2.tcb t x h e r + rw [h1.csc] at e1 + exact h1.tcb t x1 hx1 e1 (h2.rls t r) + +/-- **`CurEntryGA` across a `CurKeepA` step that does not move the time back.** -/ +theorem CurEntryGA.keepA {σ σ' : Fl4SpecA64.State Unit} (h : CurEntryGA σ) (k : CurKeepA σ σ') + (ht : σ.curTime ≤ σ'.curTime) : CurEntryGA σ' := fun hc y k' hy a => by + rw [k.csc] at hy + obtain ⟨y0, k0, hy0, a0, e0⟩ := k.sc y k' hy a + obtain ⟨r0, q0⟩ := h (k.cons ▸ hc) y0 k0 hy0 a0 + refine ⟨?_, fun t x hx e hr => ?_⟩ + · rw [← e0]; exact refillReady_monoA r0 ht + · rw [k.csc] at e + obtain ⟨x0, hx0, e1⟩ := k.tcb t x hx e hr + exact q0 t x0 hx0 e1 (k.rls t hr) + +/-- **`CurEntryGA` from the current-SC facts the reset loop carries**: the current TCB is bound to the current SC and +outside the release queue (`CurInvA`), the current head is ready with its consumption (`OffReadyA`), and a TCB bound +to an SC is that SC's thread (`ScInvA.back`). -/ +theorem CurEntryGA.of_cur {m : Fl4SpecA64.McState} {σ : State Unit} (CI : CurInvA σ) (R : OffReadyA σ) + (inv : ScInvA m σ) : CurEntryGA σ := fun _ y k hk _ => by + refine ⟨?_, fun t x hx hs hrq => ?_⟩ + · have h := R y k hk + have e0 : ((Fl4SpecA64.refillHd y).rTime + 0).toNat = (Fl4SpecA64.refillHd y).rTime.toNat := by simp + simp only [Fl4SpecA64.refillReady, Fl4SpecA64.refillReady', decide_eq_true_eq, BitVec.le_def] + omega + · obtain ⟨ct, hct, hsc, -⟩ := CI.tcb + obtain ⟨y, n, hy, hyt⟩ := inv.back t x _ hx hs + obtain ⟨y2, n2, hy2, hyt2⟩ := inv.back σ.curThread ct σ.curSc hct hsc + rw [hy] at hy2 + have e := KernelObject.schedContext.inj (Option.some.inj hy2) + obtain ⟨e1, -⟩ := e + subst e1 + rw [hyt2] at hyt + have := Option.some.inj hyt + subst this + exact CI.notRel hrq + +set_option maxHeartbeats 2000000 in +/-- **`preemption_point_refinesA`'s `hTs`** (module docstring). -/ +theorem JStage3TA.timestampA [J7PpA st J7] [J7PpKA st J7] {ar : machine.Arch A} + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + (hvp : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → Aligned b ∧ ¬ PPW st s b) + {Clock : A → MachineState → Prop} (P : NowAgreeA ar Clock) (hClock : Clock a σ.machineState) + (hnd : numDomains = 1) (V : ValidMachineTimeA σ) (C : CounterStepA σ) + (hNow : fl4ReadRawCounter σ.machineState = getCurrentTime σ.machineState) (hE : CurEntryGA σ) + (hAr : sched.update_timestamp st ar s a ⦃ r => AR r.2 Z.hw ⦄) : + sched.update_timestamp st ar s a ⦃ r => ∃ σ1, ((), σ1) ∈ (updateTimeStamp (Z := Unit) σ).1 ∧ + JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 r.1 r.2 σ1 m ∧ + (∀ y ∈ OS, capA st r.1 y = capA st s y) ∧ ValidMachineTimeA σ1 ∧ Clock r.2 σ1.machineState ∧ + OSKeep st OS s r.1 ∧ L.ksb r.1 = L.ksb s ∧ L.cpu r.1 = L.cpu s ∧ CurEntryGA σ1 ∧ + σ1.machineState = rawStateA σ.machineState ⦄ := by + have E0 : YieldQueueRelA (st := st) Z m κ s σ := + ⟨J.sr, fun _ ht _ hx => (J.ep.at_tcb ht hx).st, J.mcp, J.yieldRel⟩ + refine WP.spec_mono (WP.spec_and (WP.spec_and (update_timestamp_refinesTA E0 a P hClock hnd V C) + (update_timestamp_wordsA E0.sched.toSchedSRN a P hClock)) hAr) ?_ + rintro ⟨s', a'⟩ ⟨⟨⟨τ, hτ, E1, hCl, V1, -, -, F⟩, now, -, -, es⟩, har⟩ + dsimp only at hτ E1 hCl F es har ⊢ + have eτ := fl4UpdateTimestamp_eqA σ + rw [hτ] at eτ + have eτ' : τ = fl4TimestampState (rawNextA σ.machineState) { σ with machineState := rawStateA σ.machineState } := + (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst eτ))).2 + have mem : ((), τ) ∈ (updateTimeStamp (Z := Unit) σ).1 := by + rw [← fl4UpdateTimestamp_eq_l4vA σ hnd hNow, hτ] + exact Set.mem_singleton _ + have kh : τ.kheap = σ.kheap := by rw [eτ']; rfl + have um : τ.machineState.underlyingMemory = σ.machineState.underlyingMemory := by rw [eτ']; rfl + have irqN : τ.interruptIrqNode = σ.interruptIrqNode := by rw [eτ']; rfl + have cdt : τ.cdt = σ.cdt := by rw [eτ']; rfl + have orig : τ.isOriginalCap = σ.isOriginalCap := by rw [eτ']; rfl + have HS : SpecSchedA σ τ := + { kh := fun q => Or.inl (by rw [kh]) + irqS := by rw [eτ']; rfl + irqN := irqN + asid := by rw [eτ']; rfl + vcur := by rw [eτ']; rfl + cdt := cdt + orig := orig } + have SSm : SchedSameA σ { τ with curTime := σ.curTime, consumedTime := σ.consumedTime } := + { kh := fun q ko _ => by show τ.kheap q = some ko ↔ σ.kheap q = some ko; rw [kh] + cur := by rw [eτ']; rfl + idle := by rw [eτ']; rfl + csc := by rw [eτ']; rfl + rq := by rw [eτ']; rfl + rls := by rw [eτ']; rfl + time := rfl + cons := rfl } + have SSz : SSzA σ τ := fun q y k h => ⟨y, by rw [← kh]; exact h⟩ + -- the two node words + have hc0 := J.sq.cur + have nd := J.sq.nodes _ hc0 + have d48 : layout.node.CUR_TIME.val < 4288 := by simp [layout.node.CUR_TIME] + have d40 : layout.node.CONSUMED.val < 4288 := by simp [layout.node.CONSUMED] + have v48 : layout.node.CUR_TIME.val = 48 := by unfold layout.node.CUR_TIME; rfl + have v40 : layout.node.CONSUMED.val = 40 := by unfold layout.node.CONSUMED; rfl + have a48 := nodeAt_aligned nd d48 (by rw [v48]) + have a40 := nodeAt_aligned nd d40 (by rw [v40]) + obtain ⟨tA, etA⟩ : ∃ b, b = L.nodeAt s (L.cpu s) layout.node.CUR_TIME := ⟨_, rfl⟩ + obtain ⟨cA, ecA⟩ : ∃ b, b = L.nodeAt s (L.cpu s) layout.node.CONSUMED := ⟨_, rfl⟩ + rw [← etA] at a48 + rw [← ecA] at a40 + obtain ⟨used, es'⟩ : ∃ used, s' = L.wr (L.wr s tA now) cA used := by + have es2 : s' = L.wr (L.wr s tA now) cA (wa (L.mem s (L.nodeA s layout.node.CONSUMED)) + (core.num.U64.wrapping_sub now (L.curTime s))) := by + rw [etA, ecA]; exact es + exact ⟨_, es2⟩ + have hw : WritesIn st s s' (fun b => b = tA ∨ b = cA) := by + rw [es'] + exact (writesIn_wr (st := st) (s := s) (v := now) a48).trans (writesIn_wr (st := st) (v := used) a40) + have kb : L.ksb s' = L.ksb s := hw.1 + have cpu : L.cpu s' = L.cpu s := hw.2.1 + have tv : tA.val = L.nodeBase s (L.cpu s) + 48 := by rw [etA, nodeAt_val nd d48, v48] + have cv : cA.val = L.nodeBase s (L.cpu s) + 40 := by rw [ecA, nodeAt_val nd d40, v40] + have hnb : L.nodeBase s (L.cpu s) = (L.ksb s).val + 223488 + (L.cpu s).val * 4288 := rfl + have memk : ∀ b : U64, Aligned b → b.val ≠ tA.val → b.val ≠ cA.val → L.mem s' b = L.mem s b := fun b hb n1 n2 => + hw.mem_eq hb (fun h => h.elim (fun e => n1 (by rw [e])) (fun e => n2 (by rw [e]))) + have hcpu128 : (L.cpu s).val < 128 := by + obtain ⟨k, -, hk, h⟩ := J.j.1.csN + have := (h _).1 hc0 + omega + -- the words written are node words, in no object + have hblk := BXI.blk J.ent.bxi + have Lc := LvC.of_blk hblk + have inKs : ∀ b : U64, (b = tA ∨ b = cA) → (L.ksb s).val + 223488 ≤ b.val ∧ b.val < (L.ksb s).val + 772352 := by + rintro b (e | e) <;> rw [e] + · rw [tv, hnb]; omega + · rw [cv, hnb]; omega + have inKsS : ∀ b : U64, (b = tA ∨ b = cA) → (L.ksb s).val + 223488 ≤ b.val ∧ b.val < (L.ksb s).val + 772352 ∧ + (b.val - (L.ksb s).val - 223488) % 4288 ≠ 120 ∧ (b.val - (L.ksb s).val - 223488) % 4288 ≠ 128 := by + rintro b (e | e) <;> rw [e] + · rw [tv, hnb]; omega + · rw [cv, hnb]; omega + have offT : ∀ b : U64, (b = tA ∨ b = cA) → ∀ t ∈ H.tcbs, b.val < t.val ∨ t.val + 4096 ≤ b.val := by + intro b hb t ht + have d := nd.disj t ht + have g := inKs b hb + rcases hb with e | e <;> rw [e] + · rw [tv]; unfold LawfulStore.nodeBase at d ⊢; omega + · rw [cv]; unfold LawfulStore.nodeBase at d ⊢; omega + have offK : ∀ q k, H q = some k → (∀ y, k ≠ .other y) → ∀ b : U64, q.val ≤ b.val → b.val < q.val + 2 ^ k.bits → + ¬ (b = tA ∨ b = cA) := by + intro q k hq ne b h1 h2 hb + have g := inKs b hb + rcases J.ko.1 q k hq ne with g' | g' <;> omega + have offW : ∀ b : U64, Aligned b → ¬ (b = tA ∨ b = cA) → L.mem s' b = L.mem s b := fun b hb n => hw.mem_eq hb n + have wS : WritesIn st s s' (SchedStepWA (st := st) Z m σ s) := + hw.mono fun b hb => Or.inl (Or.inr (Or.inl (Or.inr (Or.inr (Or.inr (inKsS b hb)))))) + have osk : OSKeep st OS s s' := J.osKeep_stepA wS + have tcbK : ∀ t ∈ Z.T, ∀ b : U64, Aligned b → t.val ≤ b.val → b.val < t.val + 4096 → L.mem s' b = L.mem s b := by + intro t ht b hb h1 h2 + have htH : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + refine offW b hb (fun h => ?_) + rcases offT b h t htH with g | g <;> omega + have tAl : ∀ t ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → Aligned (wa t K) := fun t ht K h1 h2 => + field_aligned (J.sr.core.tcbs t ht) h1 h2 + have tK : ∀ t ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → L.mem s' (wa t K) = L.mem s (wa t K) := by + intro t ht K h1 h2 + have f := (J.sr.core.tcbs t ht).fits + have ev : (wa t K).val = t.val + K.val := by rw [wa_valP]; exact Nat.mod_eq_of_lt (by omega) + exact tcbK t ht _ (tAl t ht K h1 h2) (by omega) (by omega) + -- the system invariants + have hN1 : L.wr s tA now = L.wr s (L.nodeAt s (L.cpu s) layout.node.CUR_TIME) now := by rw [etA] + have e2 : s' = L.wr (L.wr s tA now) (L.nodeAt (L.wr s tA now) (L.cpu (L.wr s tA now)) layout.node.CONSUMED) + used := by + rw [es', L.cpu_wr, nodeAt_of_ksb (L.ksb_wr _ _ _), ← ecA] + have lf48 : LowFree layout.node.CUR_TIME.val := by rw [v48]; unfold LowFree; omega + have lf40 : LowFree layout.node.CONSUMED.val := by rw [v40]; unfold LowFree; omega + have bx1 := nodew_bxc (o := layout.node.CUR_TIME) (v := now) J.ent.bxi lf48 (by rw [v48]; omega) + (by rw [v48]; omega) + rw [← hN1] at bx1 + have bx2 := nodew_bxc (o := layout.node.CONSUMED) (v := used) bx1.1.1 lf40 (by rw [v40]; omega) + (by rw [v40]; omega) + rw [← e2] at bx2 + have blk2 := BXI.blk bx2.1.1 + have sq2 : SchedQueuesInv st H.tcbs Z.Cs H.omega H s' Z.Q Z.R := by + obtain ⟨-, hB, hX, hSg, -⟩ := J.ent.bxi + have n1 := node_bx (o := layout.node.CUR_TIME) (v := now) J.sq hB hX hSg lf48 (by rw [v48]; omega) + (by rw [v48]; omega) + rw [← hN1] at n1 + obtain ⟨q1, hB1, hX1, hSg1, -⟩ := n1 + have n2 := node_bx (o := layout.node.CONSUMED) (v := used) q1 hB1 hX1 hSg1 lf40 (by rw [v40]; omega) + (by rw [v40]; omega) + rw [← e2] at n2 + exact n2.1 + have ppw : ∀ b : U64, (b = tA ∨ b = cA) → PPW st s b := by + rintro b (e | e) + · exact Or.inr (Or.inl (e.trans etA)) + · exact Or.inr (Or.inr (e.trans ecA)) + have vsk : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → L.mem s' b = L.mem s b := fun b hb => + offW b (hvp b hb).1 (fun h => (hvp b hb).2 (ppw b h)) + have hvsT := (utVsPt_keepA (s' := s') J.ent.vs kb vsk).1 + have hTable (j : U64) : L.mem s' (wa (L.ksb s) (AtOff j)) = L.mem s (wa (L.ksb s) (AtOff j)) := + vsk _ (.inr (asW_at _ j)) + -- the IPC relations + have wordE : ∀ q k, H q = some k → (∀ y, k ≠ .other y) → 4 ≤ k.bits → ∀ b : U64, Aligned b → q.val ≤ b.val → + b.val < q.val + 2 ^ k.bits → L.mem s' b = L.mem s b := fun q k hq ne _ b hb h1 h2 => + offW b hb (offK q k hq ne b h1 h2) + have offObj : ∀ b : U64, + ((∃ t ∈ Z.T, t.val ≤ b.val ∧ b.val < t.val + 4096) ∨ + ∃ q len, Z.th q = some (.other len) ∧ q.val ≤ b.val ∧ b.val < q.val + len) → ¬ (b = tA ∨ b = cA) := by + intro b h hb + have g := inKs b hb + rcases h with ⟨t, ht, h1, h2⟩ | ⟨q, len, hq, h1, h2⟩ + · have htH : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + rcases offT b hb t htH with k | k <;> omega + · have k := J.j.1.place.ks q _ hq (fun h => by cases J.j.1.place.irqK hq h) + simp only [ObjKindA.size, ksSizeA] at k + omega + have ep : EpStateRel Z.T E Rp Bf (L.mem s') τ := by + refine J.ep.frame J.iplaced.e (fun b hb hf => ?_) + ⟨fun q y hk => by rw [kh]; exact hk, fun q y hk => ⟨y, by rw [kh]; exact hk, rfl⟩, + fun q y hk => ⟨y, by rw [kh]; exact hk, rfl, rfl, rfl, rfl, rfl⟩, um⟩ + exact offW b hb (offObj b (pp_objA J (Or.inl hf))) + have ntfn : NtfnStateRel N Z.T (L.mem s') τ := by + refine J.ntfn.frame J.iplaced.n (fun b hb hf => ?_) + ⟨fun q y hk => by rw [kh]; exact hk, fun q y hk => ⟨y, by rw [kh]; exact hk, rfl, rfl, rfl⟩⟩ + exact offW b hb (offObj b (pp_objA J (Or.inr (Or.inl hf)))) + have reply : ReplyStackRelA (L.mem s') Rp Z.C τ (scNameA m) stack := by + refine J.reply.frame (fun r hr => ?_) (fun c _ y k h => ⟨y, k, by rw [kh]; exact h, rfl⟩) + (fun r _ y h => ⟨y, by rw [← kh]; exact h, rfl⟩) + have al : Aligned (rNextA r) ∧ Aligned (rPrevA r) := by + obtain ⟨g1, -, g3⟩ := J.iplaced.e.rep r hr + have e8 : (rPrevA r).val = r.val + 8 := by + unfold rPrevA layout.reply.PREV + rw [wa_valP]; exact Nat.mod_eq_of_lt (by simp; omega) + have e16 : (rNextA r).val = r.val + 16 := by + unfold rNextA layout.reply.NEXT + rw [wa_valP]; exact Nat.mod_eq_of_lt (by simp; omega) + constructor <;> show _ % 8 = 0 <;> omega + have k : ∀ b, (b = rNextA r ∨ b = rPrevA r) → ¬ (b = tA ∨ b = cA) := fun b h => + offObj b (pp_objA J (Or.inr (Or.inr (Or.inl ⟨r, hr, h⟩)))) + exact ⟨offW _ al.1 (k _ (Or.inl rfl)), offW _ al.2 (k _ (Or.inr rfl))⟩ + have bufCover : BufCover st Z.T Bf s' τ := by + intro t ht y b sb w hy hb hl + have g := pp_sbValA J ht + have al := (J.sr.core.tcbs t ht).align + have f := (J.sr.core.tcbs t ht).fits + have hib : IbSame (L := L) s s' t := by + refine ⟨tK t ht _ (by simp) (by simp), ?_, ?_⟩ + · exact tcbK t ht _ (by unfold Aligned; rw [g.1]; omega) (by omega) (by omega) + · exact tcbK t ht _ (by unfold Aligned; rw [g.2]; omega) (by omega) (by omega) + rw [lookup_ipc_buffer_sameA hib] at hl + exact J.bufCover t ht y b sb w (by rw [← kh]; exact hy) hb hl + have scCov : ScCovA Z.C m τ := fun q y k h => J.scCov q y k (by rw [← kh]; exact h) + have idle : IdleNoFaultA m τ := fun c hc => by + obtain ⟨y, hy, hf⟩ := J.idleNoFault c hc + exact ⟨y, by rw [kh]; exact hy, hf⟩ + have ecs : τ.curSc = σ.curSc := by rw [eτ']; rfl + have ers : τ.releaseQueue = σ.releaseQueue := by rw [eτ']; rfl + have hmono : σ.curTime ≤ τ.curTime := by rw [eτ']; exact BitVec.le_trans V C + have sinv0 : SInvXA { τ with curTime := σ.curTime, consumedTime := σ.consumedTime } m n := + J.sinv.of_spec SSm (by rw [eτ']; rfl) (SpecIrqA.of_eq J.sinv.irqN kh irqN) + have sinv : SInvXA τ m n := + { sinv0 with + zc := fun t x hx e hr => by + rw [kh] at hx + rw [ecs] at e + rw [ers] at hr + by_cases hc0 : σ.consumedTime = 0 + · obtain ⟨x', p, y, k, hx', hp, hy, a⟩ := J.sr.inv.relSc t hr + rw [hx] at hx' + obtain rfl := KernelObject.tcb.inj (Option.some.inj hx') + rw [e] at hp + obtain rfl := Option.some.inj hp + exact absurd hr ((hE hc0 y k hy a).2 t x hx e) + · exact absurd (J.sinv.zc t x hx e hr) hc0 + rlsRun := fun t ht => by + rw [ers] at ht + obtain ⟨x, hx, hr⟩ := J.sinv.rlsRun t ht + exact ⟨x, by rw [kh]; exact hx, hr⟩ + curScReady := fun hc y k hy a => by + rw [ecs, kh] at hy + by_cases hc0 : σ.consumedTime = 0 + · exact refillReady_monoA ((hE hc0 y k hy a).1) hmono + · exact refillReady_monoA (J.sinv.curScReady hc0 y k hy a) hmono + irqN := SpecIrqA.of_eq J.sinv.irqN kh irqN } + have hirqW : ∀ j : U64, j.val < 5376 → L.mem s' (irqStAddrA (L.ksb s) j) = L.mem s (irqStAddrA (L.ksb s) j) := by + intro j hj + have eI : irqStAddrA (L.ksb s) j = irqWordA (L.ksb s) j := rfl + have hv := irqWordA_val J.sr.ksFit hj + have hal : Aligned (irqWordA (L.ksb s) j) := by + have := J.sr.toSchedSRN.ksb_al + show _ % 8 = 0 + omega + rw [eI] + refine offW _ hal (fun h => ?_) + have g := inKs _ h + omega + have runVcpu : RunVcpuInv st Z.T s' := J.runVcpu.keep + (fun t ht => by rw [stA_eq]; exact tK t ht _ (by simp) (by simp)) + (fun t ht => tK t ht 3672#u64 (by simp) (by simp)) + have vr' : ∀ sc ∈ Z.C, ∀ r ∈ stack sc, RBlk st s' r := J.vr_keepA + (fun r hr => by + obtain ⟨e, al⟩ := J.rTcbValA hr + exact offW _ al (offObj _ (pp_objA J (Or.inl (Or.inr (Or.inl ⟨r, hr, e⟩)))))) + (fun t ht => by rw [stA_eq]; exact tK t ht _ (by simp) (by simp)) + have noSwitch : ∀ u, τ.schedulerAction ≠ .switchThread u := fun u => by rw [eτ']; exact J.noSwitch u + have J2 : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s' a' τ m := + JStage3TA.of_schedWr (blkQ := J.blkQ.keepA (fun t ht => tK t ht 3336#u64 (by simp) (by simp)) + (fun _ _ x' hx => Or.inr ⟨x', by rwa [kh] at hx, rfl⟩) (fun _ _ h => by rw [kh]; exact h) + (fun _ _ h => by rw [kh]; exact h)) + J (Q := Z.Q) (Rl := Z.R) (curOf := Z.curOf) (curScOf := Z.curScOf) wS HS SSz (Sched.qk_of mem) um har + (fun _ _ => rfl) (fun t ht y hy => J.bufAl t ht y (by rw [← kh]; exact hy)) + (ppEntA3 J.ent blk2 kb hvsT) sq2 (poolOff_keepTableA (Z := Z) J.poolOff kb hTable) + E1.sched scCov ep ntfn reply bufCover idle E1.mcp E1.links sinv (J.sidle.congr kh SSm.idle (by rw [eτ']; exact BitVec.le_trans V C)) + runVcpu vr' noSwitch + (J.sact.congr kh (by rw [eτ']; rfl) SSm.cur SSm.idle) (pp_j5A J osk kb hirqW τ) + (J7PpKA.keep (st := st) _ _ _ _ _ σ τ _ J.j7 kb osk (J.pp_vsrA wS)) + have cE : CurEntryGA τ := fun _ y k hy a => by + rw [ecs, kh] at hy + by_cases hc0 : σ.consumedTime = 0 + · obtain ⟨r0, q0⟩ := hE hc0 y k hy a + refine ⟨refillReady_monoA r0 hmono, fun t x hx e hr => ?_⟩ + rw [kh] at hx; rw [ecs] at e; rw [ers] at hr + exact q0 t x hx e hr + · refine ⟨refillReady_monoA (J.sinv.curScReady hc0 y k hy a) hmono, fun t x hx e hr => ?_⟩ + rw [kh] at hx; rw [ecs] at e; rw [ers] at hr + exact hc0 (J.sinv.zc t x hx e hr) + have ems : τ.machineState = rawStateA σ.machineState := by rw [eτ']; rfl + exact ⟨τ, mem, J2, fun y hy => osk.capA hy, V1, hCl, osk, kb, cpu, cE, ems⟩ + +set_option maxHeartbeats 1000000 in +/-- **The invariant across a machine-state change that keeps the memory** (module docstring). -/ +theorem JStage3TA.machineA [J7PpA st J7] [J7PpKA st J7] + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + {ms : MachineState} (hm : ms.underlyingMemory = σ.machineState.underlyingMemory) {a2 : A} (har : AR a2 Z.hw) : + JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a2 { σ with machineState := ms } m := by + obtain ⟨σ2, e2⟩ : ∃ σ2 : State Unit, σ2 = { σ with machineState := ms } := ⟨_, rfl⟩ + rw [← e2] + have kh : σ2.kheap = σ.kheap := by rw [e2] + have um : σ2.machineState.underlyingMemory = σ.machineState.underlyingMemory := by rw [e2]; exact hm + have H2 : UtCnStepA σ σ2 := + ⟨by rw [e2]; exact ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, ⟨rfl, rfl, rfl, rfl⟩⟩, + fun q => Or.inl (by rw [kh]), um⟩ + have HS : SpecSchedA σ σ2 := + { kh := fun q => Or.inl (by rw [kh]) + irqS := by rw [e2] + irqN := by rw [e2] + asid := by rw [e2] + vcur := by rw [e2] + cdt := by rw [e2] + orig := by rw [e2] } + have SSz : SSzA σ σ2 := fun q y n h => ⟨y, by rw [← kh]; exact h⟩ + have hw : WritesIn st s s (fun _ => False) := WritesIn.refl _ _ + have hW : ∀ b : U64, False → ¬ UtRdA st Z N E Rp Bf m s σ b := fun _ h => h.elim + have hsbAl : ∀ t ∈ Z.T, Aligned (wa t 3512#u64) ∧ Aligned (sbWordA t) ∧ Aligned (wa (sbWordA t) 8#u64) := by + intro t ht + have g := pp_sbValA J ht + have al := (J.sr.core.tcbs t ht).align + refine ⟨field_aligned (J.sr.core.tcbs t ht) (by simp) (by simp), by unfold Aligned; rw [g.1]; omega, + by unfold Aligned; rw [g.2]; omega⟩ + have tAl : ∀ t ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → Aligned (wa t K) := fun t ht K h1 h2 => + field_aligned (J.sr.core.tcbs t ht) h1 h2 + have rAl : ∀ r ∈ Rp, Aligned (rNextA r) ∧ Aligned (rPrevA r) := by + intro r hr' + obtain ⟨g1, -, g3⟩ := J.iplaced.e.rep r hr' + have e8 : (rPrevA r).val = r.val + 8 := by + unfold rPrevA layout.reply.PREV + rw [wa_valP]; exact Nat.mod_eq_of_lt (by simp; omega) + have e16 : (rNextA r).val = r.val + 16 := by + unfold rNextA layout.reply.NEXT + rw [wa_valP]; exact Nat.mod_eq_of_lt (by simp; omega) + constructor <;> show _ % 8 = 0 <;> omega + exact + JStage3TA.of_schedWr (blkQ := J.blkQ.keepA (fun _ _ => rfl) + (fun _ _ x' hx => Or.inr ⟨x', by rwa [kh] at hx, rfl⟩) (fun _ _ h => by rw [kh]; exact h) + (fun _ _ h => by rw [kh]; exact h)) + J (Q := Z.Q) (Rl := Z.R) (curOf := Z.curOf) (curScOf := Z.curScOf) + (WritesIn.refl _ _) HS SSz (QKeepA.of_kheap (by rw [e2]) H2.domk) um har (fun _ _ => rfl) + (fun t ht y hy => J.bufAl t ht y (by rw [← kh]; exact hy)) J.ent J.sq J.poolOff + (utFr_schedSRA hw hW H2 J.sr) (utCn_scCovA J.scCov H2) (utFr_epA hw hW H2 J.ep J.iplaced.e) + (utFr_ntfnA hw hW H2 J.ntfn J.iplaced.n) (utFr_replyA hw hW H2 J.reply rAl) + (utFr_bufCoverA hw hW H2 J.bufCover hsbAl) (utCn_idleNoFaultA J.idleNoFault H2) + (utFr_mcpA hw hW H2 J.mcp fun t ht => ⟨tAl t ht _ (by simp) (by simp), tAl t ht _ (by simp) (by simp)⟩) + (utFr_yieldA hw hW H2 J.yieldRel fun t ht => tAl t ht _ (by simp) (by simp)) + (utCn_sinvA J.sinv H2 (SpecIrqA.of_eq J.sinv.irqN kh (by rw [e2]))) + (J.sidle.congr kh (by rw [e2]) (by rw [e2]; exact BitVec.le_refl _)) J.runVcpu J.vr (fun u => by rw [e2]; exact J.noSwitch u) (J.sact.congr kh (by rw [e2]) (by rw [e2]) (by rw [e2])) (by rw [e2]; exact J.j5) + (J7PpKA.keep (st := st) _ _ _ _ _ σ σ2 _ J.j7 rfl (fun _ _ => ⟨rfl, rfl, rfl, rfl⟩) (fun _ _ _ _ _ => rfl)) + +/-- The spec's current SC is the relation's name of FL4's, a related SC. -/ +theorem JStage3TA.pp_curScA {J7 : JSlotA S} (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) : L.cscW s ∈ Z.C ∧ σ.curSc = scNameA m (L.cscW s) := by + refine ⟨J.sr.cscC, ?_⟩ + have h := J.j.1.sch + rw [← h.csc, h.core, h.curScOfW _ h.cpuCs] + rfl + +/-- **`preemption_point_refinesA`'s `hAct`**. -/ +theorem JStage3TA.scActiveA {J7 : JSlotA S} (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) : + sporadic.sc_active st s (L.cscW s) ⦃ b => readScActive (Z := Unit) σ.curSc σ = some b ⦄ := by + obtain ⟨hc, ec⟩ := J.pp_curScA + have scr := J.sr.scr + obtain ⟨y, k, hk, -⟩ := scr.sc _ hc + refine WP.spec_mono (sc_active_refinesA scr hc hk) fun b hb => ?_ + rw [getScActive_eqA hk] at hb + have eb : b = scActive y := by + have := congrArg Prod.fst hb + exact (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 this)).1.symm + rw [ec, eb] + show Option.bind (readSchedContext (scNameA m (L.cscW s)) σ) (fun sc => Fl4Spec.Lookup.oreturn (scActive sc) σ) = _ + rw [readSchedContext_eqA hk]; rfl + +/-- **`preemption_point_refinesA`'s `hSuf`, for an active current SC** (module docstring). -/ +theorem JStage3TA.sufficientA {J7 : JSlotA S} {ar : machine.Arch A} (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + (hmb : sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hact : readScActive (Z := Unit) σ.curSc σ = some true) : + sporadic.sufficient st ar s a (L.cscW s) (L.mem s (L.nodeA s layout.node.CONSUMED)) ⦃ ok1 => + (ok1 = true ↔ readScRefillSufficient (Z := Unit) σ.curSc σ.consumedTime σ = some true) ⦄ := by + obtain ⟨hc, ec⟩ := J.pp_curScA + have scr := J.sr.scr + obtain ⟨y, k, hk, hr⟩ := scr.sc _ hc + have ha : scActive y = true := by + rw [ec] at hact + have h : Option.bind (readSchedContext (scNameA m (L.cscW s)) σ) (fun sc => Fl4Spec.Lookup.oreturn (scActive sc) σ) + = some true := hact + rw [readSchedContext_eqA hk] at h + exact Option.some.inj h + have hne : y.scRefills ≠ [] := by + have R' := hr.strong ha + rw [R'.refills] + exact refills_neA R'.wf + have cons : σ.consumedTime = (L.mem s (L.nodeA s layout.node.CONSUMED)).bv := J.j.1.rest.consumed + refine WP.spec_mono (sufficient_refinesA a scr hc hk ha _ hmb) fun b hb => ?_ + rw [getScRefillSufficient_eqA hk hne] at hb + have eb : b = refillSufficient (L.mem s (L.nodeA s layout.node.CONSUMED)).bv (refillHd y) := by + have := congrArg Prod.fst hb + exact (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 this)).1.symm + have rd : readScRefillSufficient (Z := Unit) σ.curSc σ.consumedTime σ = some b := by + rw [ec, cons, eb] + show Option.bind (readRefillHead (scNameA m (L.cscW s)) σ) + (fun h => Fl4Spec.Lookup.oreturn (refillSufficient _ h) σ) = _ + rw [readRefillHead_eqA hk hne]; rfl + rw [rd] + exact ⟨fun h => by rw [h], fun h => Option.some.inj h⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3PrioIpcA.lean b/proofs/Fl4ProofsA64/P3/Stage3PrioIpcA.lean new file mode 100644 index 000000000..87dca1fdb --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3PrioIpcA.lean @@ -0,0 +1,252 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 3e): **the IPC relations across SetPriority's priority write and `reorder_ep`** +(x86 `P3/IpcSetPrio`'s `TcbRelE.prio_wr`, `ntfn_upd_prio`, `reorderEp_eq`, `ep_reorder_rel`, ported). + +* `TcbRelE.prio_wrA`, **`EpStateRel.prioWrA`**, **`NtfnStateRel.prioWrA`**: the relations after FL4's `PRIO` word + write and the spec's `tcbPriority` update. +* `reorderEp_eqA`: the spec's `reorder_ep` of a queued thread. +* **`ep_reorder_relA`**: FL4's `reorder_ep` refines it, the queue without `t` priority-sorted (`hsort`), one + `SchedOpsEA.stable` step. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3PrioReorderA +import Fl4ProofsA64.P3.EpSendBlkA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {T E R : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + +/-- A TCB's endpoint relation across a write of its priority word (x86 `TcbRelE.prio_wr`). -/ +theorem TcbRelE.prio_wrA {m m' : U64 → U64} {t : U64} {x : Fl4SpecA64.Tcb} (ht : TcbAddr t) (h : TcbRelE R m t x) + (hA : ∀ b, Aligned b → b.val ≠ t.val + 3448 → m' b = m b) (pr : Fl4SpecA64.Priority) + (hp : (m' (prioA t)).val = pr.toNat) : TcbRelE R m' t { x with tcbPriority := pr } := by + have g : ∀ {b : U64}, Aligned b → b.val ≠ t.val + 3448 → m' b = m b := fun hb hv => hA _ hb hv + refine ⟨?_, fun e he => ?_, fun e d hs => ?_, fun e r d hs => ?_, fun r hs => ?_, fun r => ?_, hp, ?_, ?_, ?_, ?_⟩ + · rw [g (a_st ht).1 (by rw [val_st ht]; omega)]; exact h.st + · rw [g (a_blk ht).1 (by rw [val_blk ht]; omega)]; exact h.blk e he + · rw [g (a_badge ht).1 (by rw [val_badge ht]; omega), g (a_flags ht).1 (by rw [val_flags ht]; omega)] + exact h.send e d hs + · rw [g (a_reply ht).1 (by rw [val_reply ht]; omega)]; exact h.recv e r d hs + · rw [g (a_reply ht).1 (by rw [val_reply ht]; omega)]; exact h.reply r hs + · rw [g (a_reg ht r).1 (by rw [regA_val ht (regIdxA_lt r)]; have := regIdxA_lt r; omega)]; exact h.regs r + · rw [g (a_fault ht).1 (by rw [val_fault ht]; omega)]; exact h.fault0 + · rw [g (a_fault ht).1 (by rw [val_fault ht]; omega)]; exact h.faultT + · rw [g (a_bound ht).1 (by rw [val_bound ht]; omega)]; exact h.bound + · exact h.faultR.congr' ht (fun v => v ≠ t.val + 3448) hA fun k h1 h2 => by omega + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **`EpStateRel` across a priority write** (module docstring). -/ +theorem EpStateRel.prioWrA {s : S} {σ : Fl4SpecA64.State Unit} (hR : EpStateRel T E R Bf (L.mem s) σ) + (hP : EPlaced T E R Bf) {t : U64} (ht : t ∈ T) {x : Fl4SpecA64.Tcb} + (hk : σ.kheap (specPtr t) = some (.tcb x)) {v : U64} (hv : v.val < 256) : + EpStateRel T E R Bf (L.mem (L.wr s (prioA t) v)) + (updKSc σ (specPtr t) (.tcb { x with tcbPriority := BitVec.ofNat 8 v.val })) := by + have hta := hP.tcb t ht + have aP := (a_prio hta).1 + have vP := val_prio hta + have off : ∀ b, Aligned b → b.val ≠ t.val + 3448 → L.mem (L.wr s (prioA t) v) b = L.mem s b := + fun b hb h => mem_wr_ne' aP hb (fun e => h (by rw [← e]; exact vP)) + exact hR.upd_tcb hP ht _ (fun b hb ⟨_, hn⟩ => off b hb (fun e => hn (TF.of_val (a_prio hta).2 (by rw [vP]; exact e)))) + (Or.inr ⟨off _ (a_st hta).1 (by rw [val_st hta]; omega), off _ (a_blk hta).1 (by rw [val_blk hta]; omega)⟩) + (TcbRelE.prio_wrA hta (hR.at_tcb ht hk) off _ (by + rw [L.mem_wr_same aP]; simp only [BitVec.toNat_ofNat]; omega)) + +/-- **`NtfnStateRel` across a priority write** (module docstring). -/ +theorem NtfnStateRel.prioWrA {N : Set U64} {s : S} {σ : Fl4SpecA64.State Unit} (hR : NtfnStateRel N T (L.mem s) σ) + (hPN : NPlaced T N) (hP : EPlaced T E R Bf) {t : U64} (ht : t ∈ T) {x : Fl4SpecA64.Tcb} + (hk : σ.kheap (specPtr t) = some (.tcb x)) {v : U64} (hv : v.val < 256) : + NtfnStateRel N T (L.mem (L.wr s (prioA t) v)) + (updKSc σ (specPtr t) (.tcb { x with tcbPriority := BitVec.ofNat 8 v.val })) := by + have hta := hP.tcb t ht + have aP := (a_prio hta).1 + have vP := val_prio hta + have off : ∀ b, Aligned b → b.val ≠ t.val + 3448 → L.mem (L.wr s (prioA t) v) b = L.mem s b := + fun b hb h => mem_wr_ne' aP hb (fun e => h (by rw [← e]; exact vP)) + have nw : ∀ n ∈ N, ∀ K : U64, K.val < 40 → K.val % 8 = 0 → + L.mem (L.wr s (prioA t) v) (wa n K) = L.mem s (wa n K) := fun n hn K h1 h2 => + off _ (nword hPN hn h1 h2).1 (by + rw [nfield_val (hPN.ntfn n hn) (by omega)] + have := hPN.disj n hn t ht + omega) + have tw : ∀ y ∈ T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → (y ≠ t ∨ K.val ≠ 3448) → + L.mem (L.wr s (prioA t) v) (wa y K) = L.mem s (wa y K) := fun y hy K h1 h2 h3 => by + have hya := hP.tcb y hy + refine off _ (field_aligned hya h1 h2) ?_ + rw [Notif.tfield_val hya h1] + by_cases e : y = t + · subst e + rcases h3 with h3 | h3 + · exact absurd rfl h3 + · omega + · have := tcb_disj hP ht hy e + omega + have tl : ∀ y ∈ T, TLinksEq (L.mem s) (L.mem (L.wr s (prioA t) v)) y := fun y hy => + ⟨by rw [prevEp_eq]; exact tw y hy _ (by simp) (by simp) (Or.inr (by simp)), + by rw [nextEp_eq]; exact tw y hy _ (by simp) (by simp) (Or.inr (by simp))⟩ + have h1 := hR.at_tcb ht hk + refine hR.upd_tcb ht _ (fun n hn => ⟨?_, ?_, ?_, ?_, ?_⟩) (fun y hy e => ⟨?_, ?_, ?_, ?_⟩) tl + (Or.inr ⟨by rw [stA_eq]; exact tw t ht _ (by simp) (by simp) (Or.inr (by simp)), + by rw [tBlockA_eq]; exact tw t ht _ (by simp) (by simp) (Or.inr (by simp))⟩) ⟨?_, ?_, ?_, ?_⟩ + · rw [Notif.epHeadA_eq]; exact nw n hn _ (by simp) (by simp) + · rw [Notif.epTSA_eq]; exact nw n hn _ (by simp) (by simp) + · rw [Notif.nBadgeA_eq]; exact nw n hn _ (by simp) (by simp) + · rw [Notif.nBoundA_eq]; exact nw n hn _ (by simp) (by simp) + · rw [Notif.nScA_eq]; exact nw n hn _ (by simp) (by simp) + · rw [stA_eq]; exact tw y hy _ (by simp) (by simp) (Or.inl e) + · rw [tBlockA_eq]; exact tw y hy _ (by simp) (by simp) (Or.inl e) + · rw [badgeRegA_eq]; exact tw y hy _ (by simp) (by simp) (Or.inl e) + · rw [prioA_eq']; exact tw y hy _ (by simp) (by simp) (Or.inl e) + · rw [stA_eq, tw t ht _ (by simp) (by simp) (Or.inr (by simp)), ← stA_eq]; exact h1.st + · intro n hn + rw [tBlockA_eq, tw t ht _ (by simp) (by simp) (Or.inr (by simp)), ← tBlockA_eq]; exact h1.blk n hn + · rw [badgeRegA_eq, tw t ht _ (by simp) (by simp) (Or.inr (by simp)), ← badgeRegA_eq]; exact h1.badge + · show (L.mem (L.wr s (prioA t) v) (prioA t)).val = (BitVec.ofNat 8 v.val).toNat + rw [L.mem_wr_same aP]; simp only [BitVec.toNat_ofNat]; omega + +end + +section Spec +open Fl4SpecA64 Fl4Spec + +theorem assert_trueA {σ' : Type} {P : Prop} (h : P) (s : σ') : NonDet.assert P s = ({((), s)}, False) := by + simp [NonDet.assert, h] + +/-- The spec's `reorder_ep` of a queued thread (x86 `reorderEp_eq`). -/ +theorem reorderEp_eqA {σ : State Unit} {e t : ObjRef} {x : Endpoint} {xt : Tcb} + (hk : σ.kheap e = some (.endpoint x)) (hkt : σ.kheap t = some (.tcb xt)) (htq : t ∈ x.epQueue) + (g : ObjRef → Priority) + (hq : ∀ y ∈ removeAll t x.epQueue, ∃ z, σ.kheap y = some (.tcb z) ∧ z.tcbPriority = g y) : + reorderEp (Z := Unit) e t σ = ({((), updK σ e (.endpoint (updateEpQueue x + (specIns (removeAll t x.epQueue) g xt.tcbPriority t) true)))}, False) := by + have hne : x.epQueue ≠ [] := List.ne_nil_of_mem htq + have hni : x ≠ .idleEP := by rintro rfl; exact hne rfl + unfold reorderEp + rw [nd_bind_one (getEndpoint_eq hk), nd_bind_one (assert_trueA hni σ), nd_pure_bind', + nd_bind_one (assert_trueA hne σ), nd_pure_bind', nd_bind_one (tcbAppend_eq hkt _ g hq)] + exact setEndpoint_eq _ hk + +end Spec + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {SR : S → Fl4SpecA64.State Unit → Prop} + +set_option maxHeartbeats 4000000 in +/-- **`reorder_ep` refines `reorder_ep`** for a thread queued on the endpoint, the rest of the queue sorted by +priority (x86 `ep_reorder_rel`). -/ +theorem ep_reorder_relA (hO : SchedOpsE0A st T E R Bf SR) (hP : EPlaced T E R Bf) (s : S) + (σ : Fl4SpecA64.State Unit) (hR : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) {t : U64} (ht : t ∈ T) + {xt : Fl4SpecA64.Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) {e : U64} (he : e ∈ E) + {x : Fl4SpecA64.Endpoint} (hk : σ.kheap (specPtr e) = some (.endpoint x)) (htq : specPtr t ∈ x.epQueue) + (hsort : ∀ xs, QueueRel (L.mem s) T .Ep (qOf (L.mem s) e) xs → + PrioSorted (fun y => L.mem s (prioA y)) (xs.erase t)) : + endpoint.reorder_ep st s e t ⦃ s' => + ∃ σ', Fl4SpecA64.reorderEp (Z := Unit) (specPtr e) (specPtr t) σ = ({((), σ')}, False) ∧ + EpStateRel T E R Bf (L.mem s') σ' ∧ SR s' σ' ∧ WritesIn st s s' (EWrite T E R Bf) ∧ + (∃ y, σ' = updK σ (specPtr e) (.endpoint y) ∧ ∀ u, u ∈ y.epQueue ↔ u ∈ x.epQueue) ∧ + σ' = updK σ (specPtr e) (.endpoint (Fl4SpecA64.updateEpQueue x + (specIns (Fl4SpecA64.removeAll (specPtr t) x.epQueue) (prioOf σ) xt.tcbPriority (specPtr t)) + true)) ∧ + ∃ W : List U64, (∀ w ∈ W, w ∈ T ∧ specPtr w ∈ x.epQueue) ∧ QAgree T e W (L.mem s) (L.mem s') ⦄ := by + have hx := hR.at_ep he hk + have hxt := hR.at_tcb ht hkt + obtain ⟨xs, hQ, hxs, hB⟩ := hx.queue + have htxs : t ∈ xs := by + rw [hxs] at htq; obtain ⟨y, hy, ey⟩ := List.mem_map.1 htq; rw [← specPtr_inj ey]; exact hy + have hnd := hQ.nodup + have hsorted := hsort xs hQ + refine WP.spec_mono (ep_reorder_fullA hP s he t xs hQ htxs) ?_ + rintro s' ⟨hA1, hA2, hA3, hA4⟩ + set ys := insPrio (fun y => L.mem s (prioA y)) (L.mem s (prioA t)) t (xs.erase t) with hys + have hrm : Fl4SpecA64.removeAll (specPtr t) x.epQueue = (xs.erase t).map specPtr := by + rw [hxs]; exact removeAll_map hnd t + have hq : ∀ y ∈ Fl4SpecA64.removeAll (specPtr t) x.epQueue, ∃ z, σ.kheap y = some (.tcb z) ∧ + z.tcbPriority = prioOf σ y := by + intro y hy + rw [hrm] at hy + obtain ⟨y', hy'x, rfl⟩ := List.mem_map.1 hy + obtain ⟨z, hz, -⟩ := hR.tcb y' (hQ.mem y' (List.mem_of_mem_erase hy'x)) + exact ⟨z, hz, by simp [prioOf, hz]⟩ + have hlist : specIns (Fl4SpecA64.removeAll (specPtr t) x.epQueue) (prioOf σ) xt.tcbPriority (specPtr t) = + ys.map specPtr := by + rw [hrm] + exact specIns_map hsorted hxt.prio fun y hy => by + obtain ⟨z, hz, hzr⟩ := hR.tcb y (hQ.mem y (List.mem_of_mem_erase hy)) + exact ⟨z, hz, hzr.prio⟩ + have hmem : ∀ y ∈ ys, y ∈ xs := fun y hy => by + rcases mem_insPrio_sorted hsorted hy with rfl | hy' + · exact htxs + · exact List.mem_of_mem_erase hy' + have hmem' : ∀ y ∈ xs, y ∈ ys := fun y hy => by + rw [hys, insPrio_eq_sorted _ _ t _ hsorted, List.mem_append, List.mem_cons] + by_cases e : y = t + · exact Or.inr (Or.inl e) + · have h' := (List.mem_erase_of_ne e).2 hy + rw [← List.takeWhile_append_dropWhile (l := xs.erase t) (p := fun y => !decide (L.mem s (prioA t) > L.mem s (prioA y))), + List.mem_append] at h' + rcases h' with h' | h' + · exact Or.inl h' + · exact Or.inr (Or.inr h') + refine ⟨_, by rw [reorderEp_eqA hk hkt htq (prioOf σ) hq, hlist], ?_, + hO.stable _ _ _ _ hSR hA4 (EpStep.ep hk) (NoBlockE.updO _ _ fun _ h => by cases h), hA4, ⟨_, rfl, fun u => ?_⟩, + by rw [hlist], xs, fun w hw => ⟨hQ.mem w hw, by rw [hxs]; exact List.mem_map_of_mem hw⟩, hA3⟩ + · refine hR.upd_ep hP he _ (hA3.mono fun b ⟨_, h2, h3⟩ => ⟨h2, fun ⟨w, hw, hl⟩ => h3 ⟨w, hQ.mem w hw, + (hB w hw).1, hl⟩⟩) ?_ + have hne : ys ≠ [] := insPrio_ne_nil hsorted + have hkeep : ∀ y ∈ ys, L.mem s' (stA y) = L.mem s (stA y) ∧ L.mem s' (tBlockA y) = L.mem s (tBlockA y) := by + intro y hy + have hyT := hQ.mem y (hmem y hy) + have hya := hP.tcb y hyT + have g : ∀ a : U64, Aligned a → TF y a → L.mem s' a = L.mem s a := by + intro a ha hf + refine hA3 a ha ⟨fun hw => ?_, fun ⟨w, hw, hl⟩ => ?_⟩ + · have := hP.et e he y hyT; unfold EW at hw; have := hf.range; omega + · by_cases ew : w = y + · subst ew; exact hl.not_tf hf + · have := tcb_disj hP (hQ.mem w hw) hyT (Ne.symm ew); unfold TL at hl; have := hf.range; omega + exact ⟨g _ (a_st hya).1 (a_st hya).2, g _ (a_blk hya).1 (a_blk hya).2⟩ + cases x with + | idleEP => simp [Fl4SpecA64.Endpoint.epQueue] at htq + | sendEP q => + obtain ⟨h1, xs0, -, hq0, -, h4⟩ := hx + have hxs0 : xs0 = xs := List.map_injective_iff.2 (fun a b h => specPtr_inj h) (hq0.symm.trans hxs) + subst hxs0 + refine ⟨by rw [hA2]; exact h1, ys, hne, rfl, hA1, fun y hy => ?_⟩ + obtain ⟨k1, k2⟩ := hkeep y hy + obtain ⟨q1, q2⟩ := h4 y (hmem y hy) + exact ⟨by rw [k1]; exact q1, by rw [k2]; exact q2⟩ + | recvEP q => + obtain ⟨h1, xs0, -, hq0, -, h4⟩ := hx + have hxs0 : xs0 = xs := List.map_injective_iff.2 (fun a b h => specPtr_inj h) (hq0.symm.trans hxs) + subst hxs0 + refine ⟨by rw [hA2]; exact h1, ys, hne, rfl, hA1, fun y hy => ?_⟩ + obtain ⟨k1, k2⟩ := hkeep y hy + obtain ⟨q1, q2⟩ := h4 y (hmem y hy) + exact ⟨by rw [k1]; exact q1, by rw [k2]; exact q2⟩ + · cases x with + | idleEP => simp [Fl4SpecA64.Endpoint.epQueue] at htq + | sendEP q => + simp only [Fl4SpecA64.updateEpQueue, Fl4SpecA64.Endpoint.epQueue] at hxs ⊢ + rw [hxs, List.mem_map, List.mem_map] + exact ⟨fun ⟨a, ha, e⟩ => ⟨a, hmem a ha, e⟩, fun ⟨a, ha, e⟩ => ⟨a, hmem' a ha, e⟩⟩ + | recvEP q => + simp only [Fl4SpecA64.updateEpQueue, Fl4SpecA64.Endpoint.epQueue] at hxs ⊢ + rw [hxs, List.mem_map, List.mem_map] + exact ⟨fun ⟨a, ha, e⟩ => ⟨a, hmem a ha, e⟩, fun ⟨a, ha, e⟩ => ⟨a, hmem' a ha, e⟩⟩ + +end + +end + +end + +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/Stage3PrioReorderA.lean b/proofs/Fl4ProofsA64/P3/Stage3PrioReorderA.lean new file mode 100644 index 000000000..5a5c98912 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3PrioReorderA.lean @@ -0,0 +1,124 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 3e): **`reorder_ep` / `reorder_ntfn` with their frames** (x86 `P3/IpcSetPrio`'s +`ep_reorder_full` / `ntfn_reorder_full`, ported; FL4's AArch64 `reorder_ep` / `reorder_ntfn` are the x86 text): +`t` is taken out of the queue (`tcbqueue::remove`) and put back in priority order (`append_by_prio`), the object's +state kept; the writes are the object's two words and the `Ep` links of the queue's TCBs. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.EpQueueA +import Fl4ProofsA64.P3.NtfnQueueA +import Fl4ProofsA64.P3.NtfnBlockA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif + +noncomputable section + +section +variable {S : Type _} {st : machine.Store S} [L : LawfulStore st] {T E R : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + +/-- **`reorder_ep`** of a member, with its frame (x86 `ep_reorder_full`). -/ +theorem ep_reorder_fullA (hP : EPlaced T E R Bf) (s : S) {e : U64} (he : e ∈ E) (t : obj.Tcb) (xs : List U64) + (h : QueueRel (L.mem s) T .Ep (qOf (L.mem s) e) xs) (ht : t ∈ xs) : + endpoint.reorder_ep st s e t ⦃ s' => + QueueRel (L.mem s') T .Ep (qOf (L.mem s') e) + (insPrio (fun y => L.mem s (prioA y)) (L.mem s (prioA t)) t (xs.erase t)) ∧ + stOf (L.mem s') e = stOf (L.mem s) e ∧ QAgree T e xs (L.mem s) (L.mem s') ∧ + WritesIn st s s' (EWrite T E R Bf) ⦄ := by + have hE := hP.epPlaced he + have hT := hP.tcb + unfold endpoint.reorder_ep + step with Fl4ProofsA64.P1.obj.Ep.state.spec as ⟨st0, hst0⟩ + step with Fl4ProofsA64.P1.endpoint.get_queue.spec as ⟨q, q_post⟩ + subst q_post hst0 + refine WP.spec_bind (tcbqueue.remove.rel s .Ep _ t xs hT h ht) ?_ + rintro ⟨q1, s1⟩ h1 + simp only [WP.uncurry'_pair] at h1 + obtain ⟨hr1, hf1, -, -⟩ := h1 + have htT : t ∈ T := h.mem t ht + have hn : t ∉ xs.erase t := h.nodup.not_mem_erase + refine WP.spec_bind (tcbqueue.append_by_prio.rel s1 .Ep q1 t _ hT hr1 htT hn) ?_ + rintro ⟨q2, s2⟩ h2 + simp only [WP.uncurry'_pair] at h2 + obtain ⟨hr2, hf2⟩ := h2 + have hW : ∀ w ∈ xs, w ∈ T := h.mem + have hWa : ∀ w ∈ xs, TcbAddr w := h.addr hT + have hpr : ∀ y, TcbAddr y → L.mem s1 (prioA y) = L.mem s (prioA y) := fun y hy => prio_framed hy hWa hf1 + have hf : Framed st .Ep xs s s2 := hf1.trans (hf2.mono fun w hw => by + rcases List.mem_cons.1 hw with rfl | hw + · exact ht + · exact List.mem_of_mem_erase hw) + step with Fl4ProofsA64.P1.endpoint.set_queue.spec as ⟨s3, hs3⟩ + subst hs3 + obtain ⟨h3, h4⟩ := ep_set_queue (state := L.epState s e) hT hE hr2 + obtain ⟨q1', q2'⟩ := qagree_of hP he hW hf q2.head ((q2.end &&& ~~~3#u64) ||| (L.epState s e &&& 3#u64)) + refine ⟨?_, by rw [← epState_eq, h4, epState_and3, epState_eq], q1', q2'⟩ + have e' : insPrio (fun y => L.mem s1 (prioA y)) (L.mem s1 (prioA t)) t (xs.erase t) = + insPrio (fun y => L.mem s (prioA y)) (L.mem s (prioA t)) t (xs.erase t) := by + rw [hpr t (hWa t ht)] + exact insPrio_congr fun y hy => hpr y (hWa y (List.mem_of_mem_erase hy)) + rw [← e']; exact h3 + +end + +end + +end Fl4ProofsA64.P3.Endp + +namespace Fl4ProofsA64.P3.Notif +open fl4_core_a64 Fl4ProofsA64.P1 + +noncomputable section + +section +variable {S : Type _} {st : machine.Store S} [L : LawfulStore st] {N T : Set U64} + +/-- **`reorder_ntfn`** of a member, with its frame (x86 `ntfn_reorder_full`). -/ +theorem ntfn_reorder_fullA (hP : NPlaced T N) (s : S) {n : U64} (hn : n ∈ N) (t : obj.Tcb) (xs : List U64) + (h : QueueRel (L.mem s) T .Ep (qOf (L.mem s) n) xs) (ht : t ∈ xs) : + notification.reorder_ntfn st s n t ⦃ s' => + QueueRel (L.mem s') T .Ep (qOf (L.mem s') n) + (insPrio (fun y => L.mem s (prioA y)) (L.mem s (prioA t)) t (xs.erase t)) ∧ + stOf (L.mem s') n = stOf (L.mem s) n ∧ QKeeps st N T n xs s s' ⦄ := by + have hE : EpPlaced T n := + ⟨by have := (hP.ntfn n hn).align; omega, by have := (hP.ntfn n hn).fits; omega, + fun t ht => by have := hP.disj n hn t ht; omega⟩ + have hT := hP.tcb + unfold notification.reorder_ntfn + step with Fl4ProofsA64.P1.obj.Ntfn.state.spec as ⟨st0, hst0⟩ + step with Fl4ProofsA64.P1.notification.get_queue.spec as ⟨q, q_post⟩ + subst q_post hst0 + refine WP.spec_bind (tcbqueue.remove.rel s .Ep _ t xs hT h ht) ?_ + rintro ⟨q1, s1⟩ h1 + simp only [WP.uncurry'_pair] at h1 + obtain ⟨hr1, hf1, -, -⟩ := h1 + have htT : t ∈ T := h.mem t ht + have hn' : t ∉ xs.erase t := h.nodup.not_mem_erase + refine WP.spec_bind (tcbqueue.append_by_prio.rel s1 .Ep q1 t _ hT hr1 htT hn') ?_ + rintro ⟨q2, s2⟩ h2 + simp only [WP.uncurry'_pair] at h2 + obtain ⟨hr2, hf2⟩ := h2 + have hW : ∀ w ∈ xs, w ∈ T := h.mem + have hWa : ∀ w ∈ xs, TcbAddr w := h.addr hT + have hpr : ∀ y, TcbAddr y → L.mem s1 (prioA y) = L.mem s (prioA y) := fun y hy => prio_framed hy hWa hf1 + have hf : Framed st .Ep xs s s2 := hf1.trans (hf2.mono fun w hw => by + rcases List.mem_cons.1 hw with rfl | hw + · exact ht + · exact List.mem_of_mem_erase hw) + step with Fl4ProofsA64.P1.notification.set_queue.spec as ⟨s3, hs3⟩ + subst hs3 + obtain ⟨h3, h4⟩ := ep_set_queue (state := L.epState s n) hT hE hr2 + refine ⟨?_, by rw [← epState_eq, h4, epState_and3, epState_eq], qkeeps_of hP hn hW hf _ _⟩ + have e' : insPrio (fun y => L.mem s1 (prioA y)) (L.mem s1 (prioA t)) t (xs.erase t) = + insPrio (fun y => L.mem s (prioA y)) (L.mem s (prioA t)) t (xs.erase t) := by + rw [hpr t (hWa t ht)] + exact insPrio_congr fun y hy => hpr y (hWa y (List.mem_of_mem_erase hy)) + rw [← e']; exact h3 + +end + +end + +end Fl4ProofsA64.P3.Notif diff --git a/proofs/Fl4ProofsA64/P3/Stage3PrioSchedA.lean b/proofs/Fl4ProofsA64/P3/Stage3PrioSchedA.lean new file mode 100644 index 000000000..85de59c5b --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3PrioSchedA.lean @@ -0,0 +1,420 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 3e): **the scheduler relation across a priority write** of a thread in no ready +queue (x86 `P3/IpcSetPrio`'s `SchedRel.prio_wr`): FL4 writes the thread's `PRIO` word, the spec its `tcbPriority` +(`thread_set_priority`). + +* Spec side: `ScInvA.tcbUpdA`, `RdA.tcbUpdA`, `RunQA.tcbUpdA`: a TCB update keeping the SC, state and fault keeps + them (they read no priority). +* `SchedCoreA.agreePA`: `SchedCoreA.agree` with one thread's priority word changed, the thread in no ready queue. +* `SchedSRN.rebuildPrioA`: `SchedSRN.rebuildNodeDomainTimeA` with the TCBs' priorities given (`hpr`) instead of kept. +* **`SchedSRA.prioWrA`**: the composition. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3TcbF0A +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section Spec +open Fl4SpecA64 + +variable {σ : State Unit} {t : ObjRef} {x y : Tcb} + +/-- The TCBs after a TCB update, forwards. -/ +theorem tcbUpd_fwdA (hk : σ.kheap t = some (.tcb x)) (hsc : y.tcbSchedContext = x.tcbSchedContext) + (hst : y.tcbState = x.tcbState) {u : ObjRef} {z : Tcb} (hu : σ.kheap u = some (.tcb z)) : + ∃ w, (updKSc σ t (.tcb y)).kheap u = some (.tcb w) ∧ w.tcbSchedContext = z.tcbSchedContext ∧ + w.tcbState = z.tcbState := by + by_cases e : u = t + · subst e + rw [hk] at hu + cases hu + exact ⟨y, by simp, hsc, hst⟩ + · exact ⟨z, by rw [updKSc_kheap, Function.update_of_ne e]; exact hu, rfl, rfl⟩ + +/-- The TCBs after a TCB update, backwards. -/ +theorem tcbUpd_bwdA (hk : σ.kheap t = some (.tcb x)) (hsc : y.tcbSchedContext = x.tcbSchedContext) + (hst : y.tcbState = x.tcbState) (hf : y.tcbFault = x.tcbFault) {u : ObjRef} {w : Tcb} + (hu : (updKSc σ t (.tcb y)).kheap u = some (.tcb w)) : + ∃ z, σ.kheap u = some (.tcb z) ∧ w.tcbSchedContext = z.tcbSchedContext ∧ w.tcbState = z.tcbState ∧ + w.tcbFault = z.tcbFault := by + rw [updKSc_kheap, Function.update_apply] at hu + split at hu + · rename_i e + cases hu + exact ⟨x, e ▸ hk, hsc, hst, hf⟩ + · exact ⟨w, hu, rfl, rfl, rfl⟩ + +/-- Every other object is kept by a TCB update, forwards. -/ +theorem tcbUpd_othA (hk : σ.kheap t = some (.tcb x)) {q : ObjRef} {o : KernelObject} (hq : σ.kheap q = some o) + (hn : ∀ z, o ≠ .tcb z) : (updKSc σ t (.tcb y)).kheap q = some o := by + rw [updKSc_kheap, Function.update_of_ne (fun e => by rw [e, hk] at hq; exact hn x (Option.some.inj hq).symm)] + exact hq + +/-- Every other object is kept by a TCB update, backwards. -/ +theorem tcbUpd_othBA {q : ObjRef} {o : KernelObject} (hq : (updKSc σ t (.tcb y)).kheap q = some o) + (hn : ∀ z, o ≠ .tcb z) : σ.kheap q = some o := by + rw [updKSc_kheap, Function.update_apply] at hq + split at hq + · exact absurd (Option.some.inj hq).symm (hn y) + · exact hq + +/-- **`ScInvA` across a TCB update keeping the SC** (module docstring). -/ +theorem ScInvA.tcbUpdA {m : McState} (h : ScInvA m σ) (hk : σ.kheap t = some (.tcb x)) + (hsc : y.tcbSchedContext = x.tcbSchedContext) : ScInvA m (updKSc σ t (.tcb y)) := by + have scK : ∀ p z k, σ.kheap p = some (.schedContext z k) → (updKSc σ t (.tcb y)).kheap p = σ.kheap p := + fun p z k hp => by rw [tcbUpd_othA hk hp (fun _ c => by cases c), hp] + have hR := h.relKeep (σ' := updKSc σ t (.tcb y)) rfl + (fun u _ z hz => by + by_cases e : u = t + · subst e + rw [hk] at hz + cases hz + exact ⟨y, by simp, hsc⟩ + · exact ⟨z, by rw [updKSc_kheap, Function.update_of_ne e]; exact hz, rfl⟩) + (fun u _ z p hz hp => by + obtain ⟨w, n, hw, -⟩ := h.back u z p hz hp + exact scK p w n hw) + have back : ∀ u w, (updKSc σ t (.tcb y)).kheap u = some (.tcb w) → + ∃ z, σ.kheap u = some (.tcb z) ∧ w.tcbSchedContext = z.tcbSchedContext := by + intro u w hu + rw [updKSc_kheap, Function.update_apply] at hu + split at hu + · rename_i e + cases hu + exact ⟨x, e ▸ hk, hsc⟩ + · exact ⟨w, hu, rfl⟩ + refine ⟨fun p w n hw => h.refills p w n (tcbUpd_othBA hw (fun _ c => by cases c)), + fun p w n hw => h.rr p w n (tcbUpd_othBA hw (fun _ c => by cases c)), + fun q z hz => h.ntfnIdle q z (tcbUpd_othBA hz (fun _ c => by cases c)), + fun q z hz => h.ntfnIdleOf q z (tcbUpd_othBA hz (fun _ c => by cases c)), + hR.1, hR.2, fun u w p hw hp => ?_, fun u w p hw hp => ?_⟩ + · obtain ⟨z, hz, e⟩ := back u w hw + obtain ⟨v, n, hv, hb⟩ := h.back u z p hz (e.symm.trans hp) + exact ⟨v, n, tcbUpd_othA hk hv (fun _ c => by cases c), hb⟩ + · obtain ⟨z, hz, e⟩ := back u w hw + exact h.tcbScNotIdle u z p hz (e.symm.trans hp) + +/-- **`RdA` across a TCB update keeping the SC, state and fault**. -/ +theorem RdA.tcbUpdA (h : RdA σ) (hk : σ.kheap t = some (.tcb x)) (hsc : y.tcbSchedContext = x.tcbSchedContext) + (hst : y.tcbState = x.tcbState) (hf : y.tcbFault = x.tcbFault) : RdA (updKSc σ t (.tcb y)) := by + intro u w hu hcls + obtain ⟨z, hz, e1, e2, e3⟩ := tcbUpd_bwdA hk hsc hst hf hu + have R := h u z hz (by rw [← e2, ← e3]; exact hcls) + intro w' scp v n h1 h2 h3 h4 h5 + rw [hu] at h1 + cases h1 + exact R z scp v n hz (e1.symm.trans h2) (tcbUpd_othBA h3 (fun _ c => by cases c)) h4 h5 + +/-- **`RunQA` across a TCB update keeping the state**. -/ +theorem RunQA.tcbUpdA (h : RunQA σ) (hk : σ.kheap t = some (.tcb x)) (hsc : y.tcbSchedContext = x.tcbSchedContext) + (hst : y.tcbState = x.tcbState) : RunQA (updKSc σ t (.tcb y)) := by + have k : ∀ u, (∃ z, σ.kheap u = some (.tcb z) ∧ runnable z.tcbState = true) → + ∃ w, (updKSc σ t (.tcb y)).kheap u = some (.tcb w) ∧ runnable w.tcbState = true := by + rintro u ⟨z, hz, hr⟩ + obtain ⟨w, hw, -, e⟩ := tcbUpd_fwdA hk hsc hst hz + exact ⟨w, hw, by rw [e]; exact hr⟩ + exact ⟨fun d p u hu => k u (h.1 d p u hu), fun u hu => k u (h.2 u hu)⟩ + +end Spec + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **`SchedCoreA.agree` with one thread's priority word changed**, the thread in no ready queue (module +docstring). -/ +theorem SchedCoreA.agreePA {T Cs : Set U64} {s s' : S} {Q : U64 → U64 → List U64} {R : U64 → List U64} + (h : SchedCoreA st T Cs s Q R) (hk : L.ksb s' = L.ksb s) + (hlink : ∀ y ∈ T, L.mem s' (nextA .Sched y) = L.mem s (nextA .Sched y) ∧ + L.mem s' (prevA .Sched y) = L.mem s (prevA .Sched y)) + (hnode : ∀ c ∈ Cs, ∀ o : U64, o.val < 4288 → o.val % 8 = 0 → (o.val = 64 ∨ o.val = 72 ∨ 136 ≤ o.val) → + L.mem s' (L.nodeAt s c o) = L.mem s (L.nodeAt s c o)) + (hinq : ∀ y ∈ T, L.mem s' (inqA y) = L.mem s (inqA y)) + (haff : ∀ y ∈ T, L.mem s' (affA y) = L.mem s (affA y)) {t : U64} (ht : t ∈ T) + (hq0 : (L.mem s (inqA t) &&& sched.Q_QUEUED) = 0#u64) + (hprio : ∀ y ∈ T, y ≠ t → L.mem s' (prioA y) = L.mem s (prioA y)) (hpt : (L.mem s' (prioA t)).val < 256) : + SchedCoreA st T Cs s' Q R := by + have hT := h.tcbs + have hn : ∀ c ∈ Cs, ∀ o : U64, o.val < 4288 → o.val % 8 = 0 → (o.val = 64 ∨ o.val = 72 ∨ 136 ≤ o.val) → + L.mem s' (L.nodeAt s' c o) = L.mem s (L.nodeAt s c o) := by + intro c hc o h1 h2 h3; rw [nodeAt_of_ksb hk]; exact hnode c hc o h1 h2 h3 + have hrel : ∀ {q xs}, QueueRel (L.mem s) T .Sched q xs → QueueRel (L.mem s') T .Sched q xs := fun hq => + ⟨hq.nodup, hq.mem, hq.head, hq.last, hq.seg.congr fun y hy => + ⟨(hlink y (hq.mem y hy)).2, (hlink y (hq.mem y hy)).1⟩⟩ + have nq : ∀ c ∈ Cs, ∀ p : U64, p.val < 256 → t ∉ Q c p := fun c hc p hp => h.notQ ht hq0 hc hp + refine ⟨hT, fun c hc => (h.nodes c hc).ksb hk, fun y hy => (haff y hy) ▸ h.aff y hy, + fun y hy => ?_, fun c hc => ?_, fun c hc => ?_, ?_, ?_, ?_, ?_, ?_⟩ + · by_cases e : y = t + · subst e; exact hpt + · rw [hprio y hy e]; exact h.prio y hy + · have hv : ViewsEq st s s' c := by + refine ⟨fun p hp => ?_, ?_, fun g hg => ?_⟩ + · simp only [LawfulStore.rqQ] + rw [hn c hc _ (by rw [rqOff_val hp]; omega) (by rw [rqOff_val hp]; omega) (by rw [rqOff_val hp]; omega), + hn c hc _ (by rw [rqTailOff_val hp]; omega) (by rw [rqTailOff_val hp]; omega) + (by rw [rqTailOff_val hp]; omega)] + · exact hn c hc _ (by rw [l1Off_val]; omega) (by rw [l1Off_val]) (by rw [l1Off_val]; omega) + · exact hn c hc _ (by rw [l2Off_val hg]; omega) (by rw [l2Off_val hg]; omega) (by rw [l2Off_val hg]; omega) + refine ⟨(h.ready c hc).1.agree hv.1 fun p hp y hy => ?_, (h.ready c hc).2.views hv⟩ + exact hlink y (((h.ready c hc).1.rel p hp).mem y hy) + · have e : L.relQ s' c = L.relQ s c := by + simp only [LawfulStore.relQ] + rw [hn c hc _ (by rw [relh_val]; omega) (by rw [relh_val]) (by rw [relh_val]; simp), + hn c hc _ (by rw [relt_val]; omega) (by rw [relt_val]) (by rw [relt_val]; omega)] + rw [e]; exact hrel (h.release c hc) + · intro c hc p hp y hy + have := h.inQ c hc p hp y hy + have hyT := h.memT hc hp hy + have ne : y ≠ t := fun e => nq c hc p hp (e ▸ hy) + exact ⟨(haff y hyT).trans this.1, (hprio y hyT ne).trans this.2⟩ + · intro c hc y hy + exact (haff y (h.memTR hc hy)).trans (h.inR c hc y hy) + · intro y hy + by_cases e : y = t + · subst e + rw [hinq y hy, hq0] + have hc : L.mem s' (affA y) ∈ Cs := (haff y hy) ▸ h.aff y hy + exact ⟨fun h0 => absurd rfl h0, fun hm => absurd hm (nq _ hc _ hpt)⟩ + · rw [hinq y hy, haff y hy, hprio y hy e]; exact h.queued y hy + · intro y hy + rw [hinq y hy, haff y hy]; exact h.released y hy + · intro y hy hq + by_cases e : y = t + · subst e + have hc : L.mem s' (affA y) ∈ Cs := (haff y hy) ▸ h.aff y hy + exact absurd hq (nq _ hc _ hpt) + · rw [haff y hy, hprio y hy e] at hq + rw [haff y hy] + exact h.excl y hy hq + +/-- **`rebuildNodeDomainTimeA` with the TCBs' priorities given** (module docstring). -/ +theorem SchedSRN.rebuildPrioA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ κ' : U64 → U64} {s s' : S} {σ σ' : Fl4SpecA64.State Unit} + (H : SchedSRN st Z m κ s σ) {Q' : U64 → U64 → List U64} {R' : U64 → List U64} + (hks : L.ksb s' = L.ksb s) (hcpu : L.cpu s' = L.cpu s) + (hnode : ∀ c ∈ Z.Cs, ∀ o : U64, o.val < 56 → o.val % 8 = 0 → + L.mem s' (L.nodeAt s c o) = L.mem s (L.nodeAt s c o)) + (hdom : σ'.curDomain = 0 ∧ L.mem s' (wa (L.ksb s) 8#u64) = 0#u64) + (hdomTime : σ'.domainTime = (L.mem s' (wa (L.ksb s) 16#u64)).bv) + (htf : ∀ t ∈ Z.T, ∀ K : U64, (K = 3520#u64 ∨ K = 3432#u64) → L.mem s' (wa t K) = L.mem s (wa t K)) + (hscS : ScStateRelA st Z.C m s' σ') (hscP : ScPlacedA Z.th (L.ksb s) Z.C m σ') + (htcb : ∀ t ∈ Z.T, ∀ x, σ.kheap (specPtr t) = some (.tcb x) → ∃ y, σ'.kheap (specPtr t) = some (.tcb y) ∧ + y.tcbDomain = x.tcbDomain) + (hpr : ∀ t ∈ Z.T, ∃ y, σ'.kheap (specPtr t) = some (.tcb y) ∧ (L.mem s' (prioA t)).val = y.tcbPriority.toNat) + (htcbSc : ∀ t ∈ Z.T, ∃ x, σ'.kheap (specPtr t) = some (.tcb x) ∧ + x.tcbSchedContext = optScA m (L.mem s' (scA t))) + (hscMem : ∀ t ∈ Z.T, L.mem s' (scA t) ≠ 0#u64 → L.mem s' (scA t) ∈ Z.C) + (fCur : σ'.curThread = σ.curThread) (fCsc : σ'.curSc = σ.curSc) (fIdle : σ'.idleThread = σ.idleThread) + (htime : σ'.curTime = (L.mem s' (L.nodeAt s (L.cpu s) layout.node.CUR_TIME)).bv) + (hcons : σ'.consumedTime = (L.mem s' (L.nodeAt s (L.cpu s) layout.node.CONSUMED)).bv) + (hrepr : σ'.reprogramTimer = decide (L.mem s' (L.nodeAt s (L.cpu s) layout.node.REPROGRAM) ≠ 0#u64)) + (hact : σ'.schedulerAction = actOfA (L.mem s' (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION))) + (hactOn : ∀ u, σ'.schedulerAction = .switchThread (specPtr u) → u ∈ Z.T ∧ L.mem s (affA u) = L.cpu s) + (hinv : ScInvA m σ') + (hcore : SchedCoreA st Z.T Z.Cs s' Q' R') + (hrq : ∀ p : U64, p.val < 256 → σ'.readyQueues 0 (BitVec.ofNat 8 p.val) = (Q' (L.cpu s) p).map specPtr) + (hrqd : ∀ d p, d ≠ 0 → σ'.readyQueues d p = []) + (hrel : σ'.releaseQueue = (R' (L.cpu s)).map specPtr) (hrun : RunQA σ') + (hscCore : ∀ c ∈ Z.C, L.mem s' (scCoreA c) = κ' c) : + SchedSRN st { Z with Q := Q', R := R' } m κ' s' σ' := by + have nK : ∀ c ∈ Z.Cs, ∀ o : U64, o.val < 56 → o.val % 8 = 0 → + L.mem s' (L.nodeAt s' c o) = L.mem s (L.nodeAt s c o) := fun c hc o h1 h2 => by + rw [nodeAt_of_ksb hks]; exact hnode c hc o h1 h2 + have hct : L.curW s' = L.curW s := by + show L.mem _ (L.nodeAt _ (L.cpu _) _) = _ + rw [hcpu]; exact nK _ H.sch.cpuCs layout.node.CUR_THREAD (by simp [layout.node.CUR_THREAD]) + (by simp [layout.node.CUR_THREAD]) + refine + { sch := ?_, rest := ?_, scr := hscS, scp := ?_, thT := H.thT, place := by rw [hks]; exact H.place, + csN := H.csN, ksFit := by rw [hks]; exact H.ksFit, curT := by rw [hct]; exact H.curT, core := hcore, + curOn := ?_, run := hrun, actOn := fun u hu => ?_, cscC := ?_, inv := hinv, + scCore := hscCore, + cscAll := fun c hc => by + rw [nK c hc layout.node.CUR_SC (by simp [layout.node.CUR_SC]) (by simp [layout.node.CUR_SC])] + exact H.cscAll c hc } + · exact + { core := by rw [hcpu]; exact H.sch.core + cpuCs := by rw [hcpu]; exact H.sch.cpuCs + curOfW := fun c hc => by + rw [nK c hc layout.node.CUR_THREAD (by simp [layout.node.CUR_THREAD]) (by simp [layout.node.CUR_THREAD])] + exact H.sch.curOfW c hc + curScOfW := fun c hc => by + rw [nK c hc layout.node.CUR_SC (by simp [layout.node.CUR_SC]) (by simp [layout.node.CUR_SC])] + exact H.sch.curScOfW c hc + cur := by rw [fCur]; exact H.sch.cur + csc := by rw [fCsc]; exact H.sch.csc + idle := by + show σ'.idleThread = specPtr (L.mem _ (L.nodeAt _ (L.cpu _) layout.node.IDLE_THREAD)) + rw [hcpu, fIdle, nK _ H.sch.cpuCs layout.node.IDLE_THREAD (by simp [layout.node.IDLE_THREAD]) + (by simp [layout.node.IDLE_THREAD])] + exact H.sch.idle + idleOf := fun c hc => by + rw [nK c hc layout.node.IDLE_THREAD (by simp [layout.node.IDLE_THREAD]) (by simp [layout.node.IDLE_THREAD])] + exact H.sch.idleOf c hc + idleScOf := fun c hc => by + rw [nK c hc layout.node.IDLE_SC (by simp [layout.node.IDLE_SC]) (by simp [layout.node.IDLE_SC])] + exact H.sch.idleScOf c hc + act := by + show σ'.schedulerAction = actOfA (L.mem _ (L.nodeAt _ (L.cpu _) layout.node.SCHED_ACTION)) + rw [hcpu, nodeAt_of_ksb hks]; exact hact + time := by + show σ'.curTime = (L.mem _ (L.nodeAt _ (L.cpu _) layout.node.CUR_TIME)).bv + rw [hcpu, nodeAt_of_ksb hks] + exact htime + aff := fun t ht => by + rw [show affA t = wa t 3520#u64 from rfl, htf t ht 3520#u64 (by simp)]; exact H.sch.aff t ht + affT := H.sch.affT + tcb := htcbSc + sc := ScStateRelA.schedSc hscS + scOff := H.sch.scOff } + · exact + { rq := fun p hp => by rw [hcpu]; exact hrq p hp + rqDom := hrqd + rel := by rw [hcpu]; exact hrel + consumed := by rw [hcpu, nodeAt_of_ksb hks]; exact hcons + repr := by rw [hcpu, nodeAt_of_ksb hks]; exact hrepr + curDom := by rw [hks]; exact hdom + domTime := by rw [hks]; exact hdomTime + prio := fun t ht => by + obtain ⟨x, h1, h2, h3, -⟩ := H.rest.prio t ht + obtain ⟨y, hy, e2⟩ := htcb t ht x h1 + obtain ⟨y', hy', hp'⟩ := hpr t ht + rw [hy] at hy' + cases hy' + refine ⟨y, hy, by rw [e2]; exact h2, ?_, hp'⟩ + rw [show domA t = wa t 3432#u64 from rfl, htf t ht 3432#u64 (by simp)]; exact h3 + scMem := hscMem + refillMax := ScStateRelA.refillMax hscS } + · rw [hks]; exact hscP + · rw [hct, hcpu, show affA (L.curW s) = wa (L.curW s) 3520#u64 from rfl, htf _ H.curT 3520#u64 (by simp)] + exact H.curOn + · obtain ⟨hu1, hu2⟩ := hactOn u hu + exact ⟨hu1, by rw [hcpu, show affA u = wa u 3520#u64 from rfl, htf _ hu1 3520#u64 (by simp)]; exact hu2⟩ + · show L.mem _ (L.nodeAt _ (L.cpu _) _) ∈ Z.C + rw [hcpu, nK _ H.sch.cpuCs layout.node.CUR_SC (by simp [layout.node.CUR_SC]) (by simp [layout.node.CUR_SC])] + exact H.cscC + +set_option maxHeartbeats 2000000 in +/-- **`SchedSRA` across a priority write** of a thread in no ready queue (module docstring). -/ +theorem SchedSRA.prioWrA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} {σ : Fl4SpecA64.State Unit} + (H : SchedSRA st Z m κ s σ) {t : U64} (ht : t ∈ Z.T) {x : Fl4SpecA64.Tcb} + (hk : σ.kheap (specPtr t) = some (.tcb x)) (hq0 : (L.mem s (inqA t) &&& sched.Q_QUEUED) = 0#u64) + {v : U64} (hv : v.val < 256) : + SchedSRA st Z m κ (L.wr s (prioA t) v) + (updKSc σ (specPtr t) (.tcb { x with tcbPriority := BitVec.ofNat 8 v.val })) := by + set s' := L.wr s (prioA t) v with hs' + set y : Fl4SpecA64.Tcb := { x with tcbPriority := BitVec.ofNat 8 v.val } with hy + set σ' := updKSc σ (specPtr t) (.tcb y) with hσ' + have hta : TcbAddr t := H.core.tcbs t ht + have aP : Aligned (prioA t) := field_aligned hta (K := 3448#u64) (by simp) (by simp) + have vP : (prioA t).val = t.val + 3448 := by + show (wa t 3448#u64).val = _ + rw [Notif.tfield_val hta (by decide : (3448#u64 : U64).val < 4096)]; rfl + have off : ∀ b, Aligned b → b.val ≠ t.val + 3448 → L.mem s' b = L.mem s b := fun b hb h => + mem_wr_ne' aP hb (fun e => h (by rw [← e]; exact vP)) + have w1 : WritesIn st s s' (fun b => b = prioA t) := writesIn_wr aP + have hks : L.ksb s' = L.ksb s := w1.1 + have hcpu : L.cpu s' = L.cpu s := w1.2.1 + have hsz : ObjKindA.size .tcb = 4096 := rfl + have tw : ∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → (u ≠ t ∨ K.val ≠ 3448) → + L.mem s' (wa u K) = L.mem s (wa u K) := fun u hu K h1 h2 h3 => by + have hua := H.core.tcbs u hu + refine off _ (field_aligned hua h1 h2) ?_ + rw [Notif.tfield_val hua h1] + by_cases e : u = t + · subst e + rcases h3 with h3 | h3 + · exact absurd rfl h3 + · omega + · have := H.place.apart u t .tcb .tcb ((H.thT u).2 hu) ((H.thT t).2 ht) e + rw [hsz] at this + omega + have hnode : ∀ c ∈ Z.Cs, ∀ o : U64, o.val < 4288 → o.val % 8 = 0 → + L.mem s' (L.nodeAt s c o) = L.mem s (L.nodeAt s c o) := fun c hc o ho h8 => by + refine off _ (nodeAt_aligned (H.core.nodes c hc) ho h8) ?_ + have hv := H.node_in hc ho + have h2 := H.tcb_ks ht + omega + have hglob : ∀ K : U64, K.val < ksSizeA → K.val % 8 = 0 → + L.mem s' (wa (L.ksb s) K) = L.mem s (wa (L.ksb s) K) := fun K h1 h2 => by + have hf := H.ksFit + have hkb := H.ksb_al + have hv : (wa (L.ksb s) K).val = (L.ksb s).val + K.val := by rw [wa_valm]; omega + refine off _ (by unfold Aligned; rw [hv]; omega) ?_ + have h3 := H.tcb_ks ht + omega + have hsc : ∀ a, Aligned a → ScFootA Z.C m σ a → L.mem s' a = L.mem s a := fun a ha hf => off a ha (by + have hn := H.scp.th_out hf ((H.thT t).2 ht) + rw [hsz] at hn + omega) + have hkc : ∀ c ∈ Z.C, σ'.kheap (scNameA m c) = σ.kheap (scNameA m c) := fun c hc => by + obtain ⟨z, n, hkz, -⟩ := H.scr.sc c hc + rw [hσ', tcbUpd_othA hk hkz (fun _ c => by cases c), hkz] + have scW : ∀ u ∈ Z.T, L.mem s' (scA u) = L.mem s (scA u) := fun u hu => + tw u hu 3464#u64 (by simp) (by simp) (Or.inr (by simp)) + have fwd := fun {u : Fl4SpecA64.ObjRef} {z : Fl4SpecA64.Tcb} (hu : σ.kheap u = some (.tcb z)) => + tcbUpd_fwdA (y := y) hk rfl rfl hu + have nT : ∀ u ∈ Z.T, u ≠ t → σ'.kheap (specPtr u) = σ.kheap (specPtr u) := fun u _ e => by + rw [hσ', updKSc_kheap, Function.update_of_ne (fun h => e (specPtr_inj h))] + have hpt : L.mem s' (prioA t) = v := L.mem_wr_same aP + have hcore : SchedCoreA st Z.T Z.Cs s' Z.Q Z.R := + H.core.agreePA hks (fun u hu => ⟨tw u hu 3480#u64 (by simp) (by simp) (Or.inr (by simp)), + tw u hu 3488#u64 (by simp) (by simp) (Or.inr (by simp))⟩) + (fun c hc o h1 h2 _ => hnode c hc o h1 h2) (fun u hu => tw u hu 3592#u64 (by simp) (by simp) (Or.inr (by simp))) + (fun u hu => tw u hu 3520#u64 (by simp) (by simp) (Or.inr (by simp))) ht hq0 + (fun u hu e => tw u hu 3448#u64 (by simp) (by simp) (Or.inl e)) (by rw [hpt]; exact hv) + have N : SchedSRN st { Z with Q := Z.Q, R := Z.R } m κ s' σ' := + H.toSchedSRN.rebuildPrioA hks hcpu (fun c hc o h1 h2 => hnode c hc o (by omega) h2) + (by rw [hglob 8#u64 (by simp [ksSizeA]) (by simp)]; exact H.rest.curDom) + (by rw [hglob 16#u64 (by simp [ksSizeA]) (by simp)]; exact H.rest.domTime) + (fun u hu K hK => tw u hu K (by rcases hK with rfl | rfl <;> simp) (by rcases hK with rfl | rfl <;> simp) + (Or.inr (by rcases hK with rfl | rfl <;> simp))) + (H.scr.frame hsc hkc) (ScPlacedA.of_kheap H.scp hkc) + (fun u hu z hz => by + by_cases e : u = t + · subst e + rw [hk] at hz + cases hz + exact ⟨y, by simp [hσ'], rfl⟩ + · exact ⟨z, by rw [nT u hu e]; exact hz, rfl⟩) + (fun u hu => by + by_cases e : u = t + · subst e + refine ⟨y, by simp [hσ'], ?_⟩ + rw [hpt] + simp only [hy, BitVec.toNat_ofNat] + omega + · obtain ⟨z, hz, -, -, hp⟩ := H.rest.prio u hu + refine ⟨z, by rw [nT u hu e]; exact hz, ?_⟩ + show (L.mem s' (wa u 3448#u64)).val = _ + rw [tw u hu 3448#u64 (by simp) (by simp) (Or.inl e)] + exact hp) + (fun u hu => by + obtain ⟨z, hz, e⟩ := H.sch.tcb u hu + obtain ⟨w, hw, e1, -⟩ := fwd hz + exact ⟨w, hw, by rw [e1, scW u hu]; exact e⟩) + (fun u hu h0 => by rw [scW u hu] at h0 ⊢; exact H.rest.scMem u hu h0) + rfl rfl rfl + (by rw [hnode _ H.sch.cpuCs layout.node.CUR_TIME (by simp [layout.node.CUR_TIME]) + (by simp [layout.node.CUR_TIME])]; exact H.sch.time) + (by rw [hnode _ H.sch.cpuCs layout.node.CONSUMED (by simp [layout.node.CONSUMED]) + (by simp [layout.node.CONSUMED])]; exact H.rest.consumed) + (by rw [hnode _ H.sch.cpuCs layout.node.REPROGRAM (by simp [layout.node.REPROGRAM]) + (by simp [layout.node.REPROGRAM])]; exact H.rest.repr) + (by rw [hnode _ H.sch.cpuCs layout.node.SCHED_ACTION (by simp [layout.node.SCHED_ACTION]) + (by simp [layout.node.SCHED_ACTION])]; exact H.sch.act) + H.actOn (ScInvA.tcbUpdA H.inv hk rfl) hcore H.rest.rq H.rest.rqDom H.rest.rel + (RunQA.tcbUpdA H.run hk rfl rfl) + (fun c hc => (hsc _ (H.scCoreFoot hc).1 (H.scCoreFoot hc).2).trans (H.scCore c hc)) + exact ⟨N, RdA.tcbUpdA H.rd hk rfl rfl rfl⟩ + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3PtMapShapeA.lean b/proofs/Fl4ProofsA64/P3/Stage3PtMapShapeA.lean new file mode 100644 index 000000000..39758bae6 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3PtMapShapeA.lean @@ -0,0 +1,332 @@ +/- +P3 (aarch64, A64-P3 Stage-3, D8 step 5 (ii)): **the success path of the Stage-1 `ARMPageTableMap`** (`pt_map_shapeA`), +for the `JStage3A` arm: the states FL4 and l4v go through (restart, cap write, the one descriptor write) with +`pt_map_refinesGA`'s `MapJA`. Generated by `handoff/tools/ptu/gen_ptmapshape.py` from `VsMapA.pt_map_refinesGA` +(error exits dropped). No named premise. New module. +-/ +import Fl4ProofsA64.P3.VsMapA +import Fl4ProofsA64.P3.Stage3UnmapSpecA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section top +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- What a successful Stage-1 page-table map did (module docstring). -/ +def PtMapShapeA (AR : A → HwRootA → Prop) (Z : XSetsA) (OS : Set U64) (sl ptr : U64) (m : Fl4SpecA64.McState) + (σ : Fl4SpecA64.State Unit) (cte : Fl4SpecA64.CslotPtr) + (D : Set (Except Fl4SpecA64.SyscallError Fl4SpecA64.ArchInvocation × Fl4SpecA64.State Unit) × Prop) + (s s' : S) (a : A) : Prop := + ∃ iv, D = ({(.ok (.invokePageTable iv), σ)}, False) ∧ + ∃ (x : Fl4SpecA64.Tcb) (s2 : S) (σ2 : Fl4SpecA64.State Unit) (asid va p i v : U64) (n : Nat) + (σ3 σ' : Fl4SpecA64.State Unit) (w : Fl4SpecA64.Word), + σ.kheap σ.curThread = some (.tcb x) ∧ + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + obj.Slot.set_cap st sl (L.wr s (stA (L.curW s)) sched.TS_RESTART) (.PageTable ptr true asid va) = ok s2 ∧ + ((), σ2) ∈ (Fl4SpecA64.setCap (Z := Unit) (.archObjectCap (.pageTableCap (specPtr ptr) .normalPtT + (some (asidOfA asid, w)))) cte (stUpdA σ σ.curThread x .restart)).1 ∧ + SpecSlotA (stUpdA σ σ.curThread x .restart) σ2 ∧ + CapRt.EncWf (.PageTable ptr true asid va) ∧ + CapRelA (.PageTable ptr true asid va) (.archObjectCap (.pageTableCap (specPtr ptr) .normalPtT + (some (asidOfA asid, w)))) ∧ + L.ksb s2 = L.ksb s ∧ L.cpu s2 = L.cpu s ∧ p ∈ Z.S1 ∧ ptr ≠ p ∧ i.val < 512 ∧ ¬ AsW (L.ksb s2) (peAF p i) ∧ + 0 < n ∧ n ≤ 3 ∧ s' = L.wr s2 (peAF p i) v ∧ + VsReachA Z.Pp Z.S1 Z.Y (L.mem s) (L.ksb s) p ∧ + (∃ t', σ3 = ptSetA σ2 (specPtr p) t') ∧ (∃ t0, σ2.kheap (specPtr p) = some (.archObj (.pageTable t0))) ∧ + MsA σ3 σ' ∧ UnQ σ3 σ' ∧ v ≠ 0#u64 ∧ + ((), σ') ∈ (Fl4SpecA64.performPageTableInvocation (Z := Unit) iv (stUpdA σ σ.curThread x .restart)).1 ∧ + VsRelA Z.Pp (insert ptr Z.S1) (Function.update Z.Lv ptr n) Z.X Z.Y (L.mem s') (L.ksb s') σ' ∧ + MapJA (L := L) AR Z OS m s (ptr ∉ S2PgE Z.X Z.T2 ∧ ptr ∉ Z.Y) + { Z with S1 := insert ptr Z.S1, Lv := Function.update Z.Lv ptr n } s' a σ' sl (.PageTable ptr true asid va) ∧ + -- (record edit #8) the written descriptor is a table descriptor of `ptr`, whose words are kept + (v &&& 3#u64) = 3#u64 ∧ dTabA v = ptr ∧ (∀ j : U64, j.val < 512 → L.mem s' (peAF ptr j) = L.mem s (peAF ptr j)) ∧ + -- (record edit #8b-C2, item 13) the mapped address is a Stage-1 user address (`≤ USER_TOP < 2^47`) + w.toNat < 2 ^ 47 + +set_option maxHeartbeats 4000000 in +/-- **The success path of the Stage-1 `ARMPageTableMap`** (module docstring). -/ +theorem pt_map_shapeA (ar : machine.Arch A) (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) + (cte : Fl4SpecA64.CslotPtr) (hab : AbsSlotA Z.th cte) (hos : cslotAddrA cte ∈ OS) + {ptr asid0 va0 : U64} {mp : Bool} (hcap : capAtA (L.mem s) (cslotAddrA cte) = .PageTable ptr mp asid0 va0) + (UP : UnmPtA Z.S1 Z.Y OS H.tcbs (L.mem s) σ ptr) + (slbl : Fl4SpecA64.Data) (hl : Fl4SpecA64.invocationType slbl = .archInvocationLabel .armPageTableMap) + (length buf : U64) (args : List U64) (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) + (x0 : U64) (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) + (exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) (hex : ExtraRelA Z.th (L.mem s) x0 (exs.map Prod.fst)) + (hS1 : ∀ p l mp' asid va, capAtA (L.mem s) x0 ≠ .Ept p l mp' asid va) + (hHi : ∀ p mp' asid, capAtA (L.mem s) x0 ≠ .HighVSpace p mp' asid) : + vspace.decode_page_table st ar s a fl4_abi.label.ARMPageTableMap length (cslotAddrA cte) + (.PageTable ptr mp asid0 va0) buf extra ⦃ res => ∀ s' a', res = (core.result.Result.Ok (), s', a') → + a' = a ∧ PtMapShapeA (L := L) AR Z OS (cslotAddrA cte) ptr m σ cte + ((Fl4SpecA64.decodePageTableInvocation (Z := Unit) slbl (args.map UScalar.bv) cte + (.pageTableCap (specPtr ptr) .normalPtT (mapDataA mp asid0 va0)) exs).run σ) s s' a ⦄ := by + rw [ptInv_mapA hl] + have hct : sched.cur_thread st s = ok (L.curW s) := cur_thread_ok s + have hne : ¬ fl4_abi.label.ARMPageTableMap = fl4_abi.label.ARMPageTableUnmap := by + unfold fl4_abi.label.ARMPageTableMap fl4_abi.label.ARMPageTableUnmap; decide + unfold vspace.decode_page_table + simp only [hct, bind_ok, hne, ↓reduceIte, bne_self_eq_false, Bool.false_eq_true, extra0_eqA hx0] + rcases hex with ⟨rfl, hnil⟩ | ⟨hx0n, -, vc', vrest, hvc', hmap⟩ + · -- no extra cap + have he : exs = [] := List.map_eq_nil_iff.1 hnil + subst he + rw [if_pos rfl] + simp only [error.truncated, bind_ok, WP.spec_ok] + exact fun _ _ h => by cases h + rw [if_neg hx0n] + obtain ⟨⟨vc'', xsl⟩, rest, rfl⟩ : ∃ e rest, exs = e :: rest := by + cases exs with + | nil => simp at hmap + | cons e rest => exact ⟨e, rest, rfl⟩ + simp only [List.map_cons, List.cons.injEq] at hmap + obtain ⟨rfl, -⟩ := hmap + dsimp only + by_cases h2 : length >= 2#u64 + swap + · rw [if_neg h2] + simp only [error.truncated, bind_ok, WP.spec_ok] + exact fun _ _ h => by cases h + rw [if_pos h2] + have h2' : 2 ≤ args.length := by + have : 2 ≤ length.val := by scalar_tac + omega + obtain ⟨a0, a1, r', rfl⟩ : ∃ a0 a1 r', args = a0 :: a1 :: r' := by + match args, h2' with + | a0 :: a1 :: r', _ => exact ⟨_, _, _, rfl⟩ + simp only [List.map_cons] + cases mp + swap + · -- a mapped page-table cap + simp only [↓reduceIte, error.invalid_cap, bind_ok, WP.spec_ok] + exact fun _ _ h => by cases h + rw [show mapDataA false asid0 va0 = none from rfl] + simp only [Bool.false_eq_true, ↓reduceIte, hargs 0#u64 a0 (by simp), capA_eq, bind_ok] + simp only [capAtA_eq] at hvc' hS1 hHi + generalize capA st s x0 = vc at hvc' hS1 hHi ⊢ + have nonroot : ∀ (vc : capability.Cap), CapRelA vc vc'' → (∀ r asid, vc ≠ .Pml4 r true asid) → + (∀ p l mp asid va, vc ≠ .Ept p l mp asid va) → (∀ p mp asid, vc ≠ .HighVSpace p mp asid) → + ∃ e', (Fl4SpecA64.decodePtInvMap (Z := Unit) slbl (a0.bv :: a1.bv :: List.map UScalar.bv r') cte + (.pageTableCap (specPtr ptr) .normalPtT none) ((vc'', xsl) :: rest)).run σ = ({(.error e', σ)}, False) ∧ + VsErrRelA (.Sys (.InvalidCapability 1#u64)) e' := fun vc hr h1 h2 h3 => + ⟨.invalidCapability 1, ptDec_noRootA _ _ _ _ _ _ _ _ _ _ σ (by + cases vc with + | PageTable p m asid va => + cases m + · rw [← hr.2]; exact absCap_noRootA h1 h2 h3 + · obtain ⟨_, _, rfl, _, _⟩ := CapEqA.pt_mapped_iff.mp hr.2; intro _ _ _ h; cases h + | _ => rw [← hr.2]; exact absCap_noRootA h1 h2 h3), + Or.inr ⟨1#u64, rfl, rfl⟩⟩ + cases vc with + | Ept p l mp' asid va => exact absurd rfl (hS1 p l mp' asid va) + | HighVSpace p mp' asid => exact absurd rfl (hHi p mp' asid) + | Pml4 r0 mr asid1 => + cases mr + · simp only [bind_ok, Bool.false_eq_true, ↓reduceIte, vspace.is_valid_vtable_root, error.invalid_cap, + WP.spec_ok] + exact fun _ _ h => by cases h + -- the main path: a mapped Stage-1 root cap + have hwf : asid1.val < 2 ^ 16 := hvc'.1 + have hvc'' : vc'' = .archObjectCap (.pageTableCap (specPtr r0) .vsRootPtT + (some (asidOfA asid1, (0#u64 : U64).bv))) := hvc'.2.symm + subst hvc'' + simp only [bind_ok, Bool.false_eq_true, ↓reduceIte, vspace.is_valid_vtable_root, eq_self] + by_cases htop : a0 > fl4_abi.consts.USER_TOP + · rw [if_pos htop] + simp only [error.invalid_arg, bind_ok, WP.spec_ok] + exact fun _ _ h => by cases h + rw [if_neg htop, ptDec_bodyA _ _ _ _ _ _ _ _ _ _ _ _ σ (fun h => htop ((top_iffA a0).1 h))] + have h16 : asid1.val < 65536 := lt_of_lt_of_eq hwf (by norm_num) + refine WP.spec_bind (check_vspaceA E.j.1.vs E.j.2.2.1.obj (r := r0) h16 (specPtr ptr) .normalPtT a0.bv cte) + fun res hres => ?_ + rcases res with ⟨rr, aa⟩ | e + swap + · obtain ⟨e', he', hrel⟩ := hres + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame.from, + bind_tc_ok, WP.spec_ok] + exact fun _ _ h => by cases h + obtain ⟨he, hrT, hrL, hrun', hvRoot⟩ := hres + simp only [Prod.mk.injEq] at he + obtain ⟨he1, he2⟩ := he + subst rr + subst aa + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok] + rw [hrun'] + have R := E.j.1.vs + obtain ⟨hKal, hS1A, -⟩ := E.vsLeafA + refine WP.spec_bind (lookup_pt_slotA R hrT hrL (vsReachA_of_vspaceForAsid R E.j.2.2.1.obj hrT hvRoot) a0) + fun lu ⟨p, hp, l, hl3, hsh, hlv, hsl, _, hLs, hreachP⟩ => ?_ + obtain ⟨tq, hkq, htq, hrq⟩ := R.pt p hp + have hidx := idxA_lt a0 lu.shift + have hpte := ptesOf_atA hkq (ptT := Fl4SpecA64.levelType (lvA l)) + (by rw [htq, levelType_lvA hl3]; congr 1; omega) (R.al_mod hp) hidx + rw [← hsl] at hpte + rw [ptRest_runA hLs (getPte_runA hpte)] + have hpre : PteRelA Z.S1 Z.Lv (Z.Lv p) (L.mem s lu.slot) (Fl4SpecA64.ptApply tq (idxA a0 lu.shift).bv) := by + rw [hsl]; exact hrq hreachP _ hidx + have e12 : vspace.PAGE_BITS = 12#u64 := by unfold vspace.PAGE_BITS; rfl + have v12 : (12#u64 : U64).val = 12 := rfl + by_cases hl0 : l = 0 + · subst hl0 + have hpb : lu.shift = vspace.PAGE_BITS := by rw [e12]; exact UScalar.eq_of_val_eq (by rw [hsh, v12]) + rw [if_pos hpb, if_pos (Or.inl (by rw [ptBitsLeft_lvA (by omega)]; rfl))] + exact (WP.spec_ok _).2 (fun _ _ h => by cases h) + have hpb : ¬ lu.shift = vspace.PAGE_BITS := fun e => hl0 (by + have := congrArg UScalar.val e; rw [e12, hsh, v12] at this; omega) + rw [if_neg hpb, L.read_eq, bind_ok, eq_of_spec_fd P0.Consts.vspace.D_VALID.spec, bind_ok] + simp only [lift, bind_ok, valid_bitA] + have hb0 : ¬ Fl4SpecA64.ptBitsLeft (lvA l) = Fl4SpecA64.pageBits := by + rw [ptBitsLeft_lvA hl3]; unfold Fl4SpecA64.pageBits; omega + by_cases hv : dBitA (L.mem s lu.slot) 0 = true + · rw [if_pos hv, if_pos (Or.inr fun e => by rw [(pteRel_invA hpre).1 e] at hv; cases hv)] + exact (WP.spec_ok _).2 (fun _ _ h => by cases h) + have hv' : dBitA (L.mem s lu.slot) 0 = false := by simpa using hv + rw [if_neg hv, if_neg (by rintro (h | h); exact hb0 h; exact h ((pteRel_invA hpre).2 hv'))] + -- the base address + have hs64 : lu.shift.val < 64 := by omega + have e63 : (lu.shift &&& 63#u64) = lu.shift := + UScalar.eq_of_val_eq (by + rw [and_maskA lu.shift 6 _ rfl]; exact Nat.mod_eq_of_lt (by simp only [Nat.reducePow]; omega)) + rw [e63, CapRt.shl_u64_eq 1#u64 lu.shift hs64, bind_ok] + try simp only [lift, bind_ok] + rw [ws1A lu.shift hs64] + generalize hbase : a0 &&& ~~~(⟨BitVec.ofNat 64 (2 ^ lu.shift.val - 1)⟩ : U64) = base + have hbv : base.bv = a0.bv &&& ~~~(Fl4SpecA64.mask (Fl4SpecA64.ptBitsLeft (lvA l))) := by + rw [← hbase, ptBitsLeft_lvA hl3, mask_lvA (by omega) hl3, ← hsh]; rfl + have hU : fl4_abi.consts.USER_TOP.val = 140737488355327 := by unfold fl4_abi.consts.USER_TOP; rfl + have ha0 : a0.val ≤ 140737488355327 := by + have : ¬ fl4_abi.consts.USER_TOP.val < a0.val := htop + omega + have hbl : base.val < 2 ^ 47 := by + rw [← hbase, UScalar.val_and]; exact lt_of_le_of_lt Nat.and_le_left (by omega) + -- the restart and the cap write + have hcanon : CapRt.Canonical ptr := by + have h : CapRt.EncWf (capAtA (L.mem s) (cslotAddrA cte)) := CapRt.decodeF_encWf _ _ + rw [hcap] at h + simp only [CapRt.EncWf] at h + exact h.1 + have hEnc : CapRt.EncWf (.PageTable ptr true asid1 base) := by + simp only [CapRt.EncWf]; exact ⟨hcanon, by omega, canon_ltA hbl⟩ + have hca : CapRelA (.PageTable ptr true asid1 base) (.archObjectCap (.pageTableCap (specPtr ptr) + .normalPtT (some (asidOfA asid1, a0.bv &&& ~~~(Fl4SpecA64.mask (Fl4SpecA64.ptBitsLeft (lvA l))))))) := by + exact ⟨hwf, rfl, rfl, Or.inl hbv⟩ + obtain ⟨xc, s2, σ2, hkx, hst, hset, hsc, hσ2, HS, hksb, R2, hcpu2, hw2, hJ2, hFr2⟩ := + restart_setcapJA E RS cte hab hos hEnc hca + rw [hst, bind_ok, hsc, bind_ok] + -- the new table at the written states + have hpal : ptr.val % 4096 = 0 := Nat.mod_eq_zero_of_dvd UP.al + have hcurT : L.curW s ∈ H.tcbs := E.sq.curT + have hta := E.sq.tcbs _ hcurT + have sA : Aligned (stA (L.curW s)) := by rw [stA_eq]; exact field_aligned hta (by simp) (by simp) + have hmem2 : ∀ i : U64, i.val < 512 → L.mem s2 (peAF ptr i) = L.mem s (peAF ptr i) := by + intro i hi + have ha := peAF_alignedA hpal hi + have hT := UP.tcb i hi + have hnc : ¬ W7.CapW H.tcbs {b | InOS OS b} ∅ (peAF ptr i) := by + rintro (⟨t, ht, K, hK, he⟩ | ⟨hin, -⟩) + · have hfit := (E.sq.tcbs t ht).fits + simp only [Nat.reducePow] at hfit + have := congrArg UScalar.val he + rw [wa_valm] at this + rcases hT t ht with h | h <;> omega + · exact UP.os i hi hin + have hns : stA (L.curW s) ≠ peAF ptr i := fun e => by + have := congrArg UScalar.val e + have e3 : (3328#u64 : U64).val = 3328 := rfl + have hfit := hta.fits + simp only [Nat.reducePow] at hfit + rw [stA_eq, wa_valm, e3] at this + rcases hT _ hcurT with h | h <;> omega + rw [hw2.2.2 _ ha hnc, mem_wr_ne' sA ha hns] + obtain ⟨tp, hkp, htp, hep⟩ := UP.spec + have hkp1 : (stUpdA σ σ.curThread xc .restart).kheap (specPtr ptr) = some (.archObj (.pageTable tp)) := + stUpd_keep hkx hkp (fun _ h => by cases h) + have hkp2 := HS.keep hkp1 (fun _ _ h => by cases h) (fun _ h => by cases h) + have hpn : p ≠ ptr := fun e => UP.nt (by rw [← e]; exact hp) + have hn3 : Z.Lv p + 1 ≤ 3 := by omega + have R3 := R2.addPtA UP.nt UP.al UP.ytb (fun i hi => by rw [hmem2 i hi]; exact UP.inv i hi) hkp2 htp hep + (n := Z.Lv p + 1) (by omega) hn3 + have hLp : Function.update Z.Lv ptr (Z.Lv p + 1) p = Z.Lv p := Function.update_of_ne hpn _ _ + -- `make_user_table` and the descriptor write + rw [show layout.paddr ptr = ok (core.num.U64.wrapping_sub ptr layout.PPTR_BASE) from rfl, bind_ok] + have hpa := UP.pa + refine WP.spec_bind (s1tab_v _ (by rw [ws_valA]; omega) (by omega)) fun v ⟨hv3, hvm⟩ => ?_ + have hwr := pteRel_mutA (Tb := insert ptr Z.S1) (Lv := Function.update Z.Lv ptr (Z.Lv p + 1)) (k := Z.Lv p) + (by omega) hv3 hvm hpa (Set.mem_insert _ _) (by rw [Function.update_self]) + have hwr' : PteRelA (insert ptr Z.S1) (Function.update Z.Lv ptr (Z.Lv p + 1)) + (Function.update Z.Lv ptr (Z.Lv p + 1) p) v (.pageTablePTE (Fl4SpecA64.ppnFromPptr (specPtr ptr))) := by + rw [hLp]; exact hwr + have hA : ¬ AsW (L.ksb s2) (peAF p (idxA a0 lu.shift)) := by rw [hksb]; exact hS1A p hp _ hidx + have hK2 : Aligned (L.ksb s2) := by rw [hksb]; exact hKal + have hdTab : dTabA v = ptr := by + have hd : dAddrA v = core.num.U64.wrapping_sub ptr layout.PPTR_BASE := by + rw [← hvm]; unfold vspace.ADDR_MASK dAddrA; rfl + unfold dTabA; rw [hd, ← pptrBase_litA]; exact wa_ws ptr layout.PPTR_BASE + obtain ⟨hwrite, σ3, hstore, R4, -⟩ := write_storePteA R3 (Set.mem_insert_of_mem _ hp) hidx hwr' hA hK2 + (fun _ _ => ⟨by rw [hdTab]; exact hpn.symm, fun j hj => ⟨by rw [hdTab, hmem2 j hj]; exact UP.inv j hj, + fun t0 ht0 => by + rw [hdTab, hkp2] at ht0 + cases ht0 + exact hep j hj⟩⟩) + rw [hsl, hwrite, bind_ok] + obtain ⟨-, ⟨⟨⟨⟩, σ4⟩, h4⟩, h4m⟩ := doMachineOp_okA (mop := Fl4SpecA64.cleanByVA_PoU + (specPtr (peAF p (idxA a0 lu.shift))) (Fl4SpecA64.addrFromPPtr (specPtr (peAF p (idxA a0 lu.shift))))) + (machineOpLift_okA _) σ3 + have hms : MsA σ3 σ4 := h4m _ h4 + have hMJ : MapJA (L := L) AR Z OS m s (ptr ∉ S2PgE Z.X Z.T2 ∧ ptr ∉ Z.Y) + { Z with S1 := insert ptr Z.S1, Lv := Function.update Z.Lv ptr (Z.Lv p + 1) } + (L.wr s2 (peAF p (idxA a0 lu.shift)) v) a σ4 (cslotAddrA cte) (.PageTable ptr true asid1 base) := by + intro hT hnul hTO hc + have hco : capA st s (cslotAddrA cte) = .PageTable ptr false asid0 va0 := by rw [← capAtA_eq, hcap] + obtain ⟨J2, hT2⟩ := hJ2 hT hnul (by simp) (by rw [hco]; simp) + (UA.of_shapeA (by rw [hco]; rfl) fun h => by rw [hco] at h; simp [isUtA] at h) + ⟨fun _ h => (by cases h), fun _ _ _ _ _ h => (by cases h), fun _ _ _ h => (by cases h), fun _ _ _ h => (by cases h), trivial⟩ + obtain ⟨t', rfl⟩ := storePte_eqA R3 (Set.mem_insert_of_mem _ hp) hidx hstore + exact map_finJA E J2 hT2 hksb hcpu2 hp (hTO p hp) hidx (L.ksb_wr _ _ _) (L.cpu_wr _ _ _) + (fun w hw hne => mem_wr_ne' (peAF_alignedA (R.al_mod hp) hidx) hw (Ne.symm hne)) hms + (fun q hq => (Set.mem_insert_iff.1 hq).elim (fun e => by subst e; exact Or.inr hc) Or.inl) + (fun q hq hl => by + have hne : q ≠ ptr := fun e => UP.nt (e ▸ hq) + exact ⟨Set.mem_insert_of_mem _ hq, by rw [Function.update_of_ne hne]; exact hl⟩) + (R4.of_msA hms) hFr2 hos + refine (WP.spec_ok _).2 fun s' a' e => ?_ + simp only [Prod.mk.injEq, true_and] at e + obtain ⟨rfl, rfl⟩ := e + obtain ⟨t', hσ3⟩ := storePte_eqA R3 (Set.mem_insert_of_mem _ hp) hidx hstore + obtain ⟨tq2, hkq2, -, -⟩ := R2.pt p hp + refine ⟨rfl, _, rfl, xc, s2, σ2, asid1, base, p, idxA a0 lu.shift, v, Z.Lv p + 1, σ3, σ4, _, hkx, hset, hsc, hσ2, + HS, hEnc, hca, hksb, hcpu2, hp, hpn.symm, hidx, hA, by omega, hn3, rfl, hreachP, ⟨t', hσ3⟩, ⟨tq2, hkq2⟩, hms, doMachineOp_unQ _ σ3 _ h4, + (fun h => by rw [h] at hv3; exact absurd hv3 (by decide)), + ?_, R4.of_msA hms, hMJ, hv3, hdTab, fun j hj => by + have ne : peAF p (idxA a0 lu.shift) ≠ peAF ptr j := fun e => + hpn (peAF_injA (R.al_mod hp) hpal hidx hj e).1 + rw [mem_wr_ne' (peAF_alignedA (R.al_mod hp) hidx) (peAF_alignedA hpal hj) ne, hmem2 j hj], + by rw [← hbv]; exact hbl⟩ + simp only [Fl4SpecA64.performPageTableInvocation, Fl4SpecA64.performPtInvMap, nd_bind_applyA, + Set.mem_setOf_eq] + refine ⟨((), _), hσ2, ?_⟩ + dsimp only + have hlt : Fl4SpecA64.levelType (lvA l) = ptTyA (Function.update Z.Lv ptr (Z.Lv p + 1) p) := by + rw [levelType_lvA hl3, hLp]; congr 1; omega + rw [hlt, hstore] + exact ⟨((), σ3), Set.mem_singleton _, h4⟩ + | _ => + simp only [bind_ok, Bool.false_eq_true, ↓reduceIte, vspace.is_valid_vtable_root, error.invalid_cap, WP.spec_ok] + exact fun _ _ h => by cases h + +end top + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3QKeepPresA.lean b/proofs/Fl4ProofsA64/P3/Stage3QKeepPresA.lean new file mode 100644 index 000000000..5951983aa --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3QKeepPresA.lean @@ -0,0 +1,578 @@ +/- +P3 (aarch64, A64-P3 Stage 3, record edit #8c): **the spec calculus of the queue invariants' ingredients** (`QKeepA`: +TCB priorities kept, endpoint / notification queues shrunk to sublists, SC reply stacks not grown, SC activity kept, +no reply gains a caller). `QInst m`: every result of the spec computation `m` is `QKeepA`-related to its start state. +The instances cover the cancel and teardown bodies (`cancel_all_ipc`, `cancel_all_signals`, `cancel_badged_sends`, +`cancel_ipc`, the notification unbinds) and the timestamp / activation steps. The leaves that write an endpoint or a +notification are the two dequeues (`filter` of the queue just read). + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.SpecFrameIpcA +import Fl4ProofsA64.P3.SchedOpsA +import Fl4ProofsA64.P3.SpecFrameSchedA +import Fl4ProofsA64.P3.ScActivateA +import Fl4ProofsA64.P3.Stage3SortA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open Fl4SpecA64 Fl4Spec + +namespace Sched +open Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Endp + +instance : PreO QKeepA := ⟨QKeepA.refl, QKeepA.trans⟩ + +noncomputable section + +abbrev QPresA {α : Type} (m : SMonad Unit α) : Prop := PresR QKeepA m + +class QInst {α : Type} (m : SMonad Unit α) : Prop where + pres : QPresA m + +instance {α β : Type} {m : SMonad Unit α} {f : α → SMonad Unit β} [hm : QInst m] + [hf : ∀ a, QInst (f a)] : QInst (m >>= f) := + ⟨PresR.bind hm.pres fun a => (hf a).pres⟩ + +instance {α : Type} (a : α) : QInst (pure a : SMonad Unit α) := ⟨PresR.pure a⟩ +instance {α : Type} (f : State Unit → α) : QInst (NonDet.gets f : SMonad Unit α) := ⟨PresR.gets f⟩ +instance : QInst (NonDet.get : SMonad Unit (State Unit)) := ⟨PresR.get⟩ +instance {α : Type} (f : State Unit → Option α) : QInst (NonDet.getsThe f : SMonad Unit α) := + ⟨PresR.getsThe f⟩ +instance {α : Type} (o : Option α) : QInst (NonDet.assertOpt o : SMonad Unit α) := ⟨PresR.assertOpt o⟩ +instance (P : Prop) : QInst (NonDet.assert P : SMonad Unit Unit) := ⟨PresR.assert P⟩ +instance {α : Type} : QInst (NonDet.fail : SMonad Unit α) := ⟨PresR.fail⟩ + +instance {P : Prop} {d : Decidable P} {m : SMonad Unit Unit} [h : QInst m] : + QInst (@NonDet.when (State Unit) P d m) := ⟨PresR.when h.pres⟩ +instance {P : Prop} {d : Decidable P} {m : SMonad Unit Unit} [h : QInst m] : + QInst (@NonDet.unless (State Unit) P d m) := ⟨PresR.unless' h.pres⟩ + +instance {α : Type} {P : Prop} {d : Decidable P} {a b : SMonad Unit α} [ha : QInst a] [hb : QInst b] : + QInst (@ite _ P d a b) := ⟨PresR.ite' ha.pres hb.pres⟩ + +instance {β γ : Type} {f : γ → SMonad Unit β} [hf : ∀ a, QInst (f a)] (l : List γ) : + QInst (NonDet.mapM f l) := ⟨PresR.mapM (fun a => (hf a).pres) l⟩ + +instance {α : Type} {C : α → State Unit → Prop} {B : α → SMonad Unit α} [hB : ∀ a, QInst (B a)] (r : α) : + QInst (NonDet.whileLoop C B r) := ⟨PresR.whileLoop (fun a => (hB a).pres) r⟩ + +instance {α : Type} (S : Set α × Prop) : QInst (NonDet.selectF S : SMonad Unit α) := ⟨PresR.selectF S⟩ + +instance {α : Type} {f : α → SMonad Unit Unit} [hf : ∀ a, QInst (f a)] (o : Option α) : + QInst (NonDet.maybeM f o) := by + cases o + · exact ⟨PresR.pure _⟩ + · exact hf _ + +/-- A state modification that keeps the heap. -/ +theorem QPresA.modify_fix (f : State Unit → State Unit) (hk : ∀ σ, (f σ).kheap = σ.kheap) + (hd : ∀ σ, DomKeepA σ (f σ)) : + QPresA (NonDet.modify f : SMonad Unit Unit) := + PresR.modify f fun σ => QKeepA.of_kheap (hk σ) (hd σ) + +/-! ## Heap writers -/ + +theorem threadSet_qA (f : Tcb → Tcb) (hf : ∀ x, (f x).tcbPriority = x.tcbPriority) (t : ObjRef) : + QPresA (threadSet (Z := Unit) f t) := by + intro σ r hr + obtain ⟨y1, hy1, h⟩ := hr + have hy1' : y1 ∈ (NonDet.assertOpt (getTcb (Z := Unit) t σ) σ).1 := hy1 + cases hg : getTcb (Z := Unit) t σ with + | none => rw [hg] at hy1'; exact absurd hy1' (Set.notMem_empty _) + | some x => + rw [hg] at hy1' + have e1 : y1 = (x, σ) := hy1' + subst e1 + obtain ⟨ko, -, hr2⟩ := setObject_memA h + rw [hr2] + exact QKeepA.upd (getTcb_kheapA hg) (QObjA.ofTcb (hf x)) + +theorem updateSchedContext_qA (f : SchedContext → SchedContext) + (hf : ∀ y, (f y).scReplies ≠ [] → y.scReplies ≠ [] ∧ (scActive y = true → scActive (f y) = true)) + (p : ObjRef) : QPresA (updateSchedContext (Z := Unit) p f) := by + intro σ r hr + obtain ⟨y1, hy1, h⟩ := hr + have hy1' : y1 ∈ (NonDet.assertOpt (σ.kheap p) σ).1 := hy1 + cases hk : σ.kheap p with + | none => rw [hk] at hy1'; exact absurd hy1' (Set.notMem_empty _) + | some ko => + rw [hk] at hy1' + have e1 : y1 = (ko, σ) := hy1' + subst e1 + cases ko with + | schedContext sc n => + obtain ⟨ko', -, hr2⟩ := setObject_memA h + rw [hr2] + exact QKeepA.upd hk (QObjA.ofSc (hf sc)) + | _ => exact absurd h (Set.notMem_empty _) + +theorem asUser_qA {α : Type} (t : ObjRef) (f : UserMonad α) : QPresA (asUser (Z := Unit) t f) := by + intro σ r hr + obtain ⟨y1, hy1, hr⟩ := hr + have hy1' : y1 ∈ (NonDet.assertOpt (getTcb (Z := Unit) t σ) σ).1 := hy1 + cases hg : getTcb (Z := Unit) t σ with + | none => rw [hg] at hy1'; exact absurd hy1' (Set.notMem_empty _) + | some x => + rw [hg] at hy1' + have e1 : y1 = (x, σ) := hy1' + subst e1 + obtain ⟨y2, hy2, hr⟩ := hr + have e2 : y2 = (archTcbContextGet x.tcbArch, σ) := hy2 + subst e2 + obtain ⟨⟨⟨a, uc'⟩, s3⟩, hy3, hr⟩ := hr + have e3 : s3 = σ := hy3.2 + subst e3 + obtain ⟨y4, hy4, hr⟩ := hr + have e4 : y4 = ({ x with tcbArch := archTcbContextSet uc' x.tcbArch }, s3) := hy4 + subst e4 + obtain ⟨y5, hy5, hr⟩ := hr + obtain ⟨ko, -, hr5⟩ := setObject_memA hy5 + have e6 : r = (a, y5.2) := hr + rw [e6, hr5] + exact QKeepA.upd (getTcb_kheapA hg) (QObjA.ofTcb rfl) + +theorem doMachineOp_qA {α : Type} (mop : NonDet MachineState α) : QPresA (doMachineOp (Z := Unit) mop) := by + intro σ r hr + obtain ⟨y1, hy1, hr⟩ := hr + have e1 : y1 = (σ.machineState, σ) := hy1 + subst e1 + obtain ⟨⟨⟨a, ms'⟩, s2⟩, hy2, hr⟩ := hr + have e2 : s2 = σ := hy2.2 + subst e2 + obtain ⟨y3, hy3, hr⟩ := hr + have e3 : y3 = ((), { s2 with machineState := ms' }) := hy3 + subst e3 + have e4 : r = (a, { s2 with machineState := ms' }) := hr + rw [e4] + exact QKeepA.of_kheap rfl ⟨rfl, rfl, rfl, rfl⟩ + +/-- What `get_simple_ko` returns. -/ +theorem getSimpleKo_memQA {α : Type} [Nonempty α] (C : α → KernelObject) (hC : Function.Injective C) (ptr : ObjRef) + {σ : State Unit} {r : α × State Unit} (h : r ∈ (getSimpleKo (Z := Unit) C ptr σ).1) : + r.2 = σ ∧ σ.kheap ptr = some (C r.1) := by + obtain ⟨y1, hy1, h⟩ := h + have hy1' : y1 ∈ (NonDet.assertOpt (σ.kheap ptr) σ).1 := hy1 + cases hk : σ.kheap ptr with + | none => rw [hk] at hy1'; exact absurd hy1' (Set.notMem_empty _) + | some ko => + rw [hk] at hy1' + have e1 : y1 = (ko, σ) := hy1' + subst e1 + obtain ⟨y2, hy2, h⟩ := h + have e2 : y2 = ((), σ) := hy2.2 + subst e2 + dsimp only at h + cases hp : partialInv C ko with + | none => rw [hp] at h; exact absurd h (Set.notMem_empty _) + | some e => + rw [hp] at h + have e3 : r = (e, σ) := h + have hC' : C e = ko := by + unfold partialInv at hp + split_ifs at hp with hex + cases hp + unfold The + rw [dif_pos hex] + exact (Classical.choose_spec hex).1 + rw [e3] + exact ⟨rfl, by rw [hC']⟩ + +/-- The write half: read an object of a simple type, then replace it. -/ +theorem updateSkObjRef_qA {α : Type} [Nonempty α] (C : α → KernelObject) (hC : Function.Injective C) + (f : (Option ObjRef → Option ObjRef) → α → α) (ref : ObjRef) (new : Option ObjRef) + (hq : ∀ a, QObjA (C a) (C (f (fun _ => new) a))) : QPresA (updateSkObjRef (Z := Unit) C f ref new) := by + intro σ r hr + unfold updateSkObjRef at hr + obtain ⟨y1, hy1, h⟩ := hr + obtain ⟨e1, k1⟩ := getSimpleKo_memQA C hC ref hy1 + rw [e1] at h + obtain ⟨y, hk, h2⟩ := setSimpleKo_memA C ref _ h + rw [k1] at hk + have := hC (Option.some.inj hk) + rw [h2] + exact QKeepA.upd k1 (hq _) + +theorem injEndpointQA : Function.Injective KernelObject.endpoint := fun _ _ h => by injection h +theorem injNtfnQA : Function.Injective KernelObject.notification := fun _ _ h => by injection h +theorem injReplyQA : Function.Injective KernelObject.reply := fun _ _ h => by injection h + +instance (r : ObjRef) : QInst (setReplyObjRef (Z := Unit) replyTcbUpdate r none) := + ⟨updateSkObjRef_qA _ injReplyQA _ _ _ fun _ => QObjA.ofReply fun _ => rfl⟩ +instance (r : ObjRef) (n : Option ObjRef) : QInst (setReplyObjRef (Z := Unit) replyScUpdate r n) := + ⟨updateSkObjRef_qA _ injReplyQA _ _ _ fun _ => QObjA.ofReply id⟩ +instance (r : ObjRef) (n : Option ObjRef) : QInst (setNtfnObjRef (Z := Unit) ntfnScUpdate r n) := + ⟨updateSkObjRef_qA _ injNtfnQA _ _ _ fun _ => QObjA.ofNtfn (List.Sublist.refl _)⟩ +instance (r : ObjRef) (n : Option ObjRef) : QInst (setNtfnObjRef (Z := Unit) ntfnBoundTcbUpdate r n) := + ⟨updateSkObjRef_qA _ injNtfnQA _ _ _ fun _ => QObjA.ofNtfn (List.Sublist.refl _)⟩ + +instance {α : Type} (t : ObjRef) (f : UserMonad α) : QInst (asUser (Z := Unit) t f) := ⟨asUser_qA t f⟩ +instance {α : Type} (mop : NonDet MachineState α) : QInst (doMachineOp (Z := Unit) mop) := ⟨doMachineOp_qA mop⟩ + +theorem qMapMxA {α β : Type} {f : α → SMonad Unit β} (hf : ∀ x, QPresA (f x)) : ∀ l : List α, QPresA (NonDet.mapMx f l) + | [] => by unfold NonDet.mapMx; exact PresR.pure _ + | x :: l => by + unfold NonDet.mapMx + exact PresR.bind (hf x) fun _ => qMapMxA hf l + +theorem qMaybeMA {α : Type} {f : α → SMonad Unit Unit} (hf : ∀ x, QPresA (f x)) (o : Option α) : + QPresA (NonDet.maybeM f o) := by + cases o + · exact PresR.pure _ + · exact hf _ + +/-- The `QKeepA` side of an SC rewrite: reply stacks kept, popped, or emptied. -/ +macro "qsc" : tactic => `(tactic| first + | exact fun _ h => ⟨h, id⟩ + | exact fun y h => ⟨fun e => h (by simp [scRepliesUpdate, e]), id⟩ + | exact fun _ h => absurd rfl h) + +macro "qpres" : tactic => `(tactic| + repeat' (first + | exact QInst.pres + | (refine threadSet_qA _ (fun _ => ?_) _ <;> (first | rfl | done)) + | (refine updateSchedContext_qA _ ?_ _ <;> qsc) + | refine PresR.bind_pure _ _ ?_ + | refine PresR.bind ?_ (fun _ => ?_) + | exact PresR.pure _ + | refine qMapMxA (fun _ => ?_) _ + | refine qMaybeMA (fun _ => ?_) _ + | refine PresR.when ?_ + | refine PresR.unless' ?_ + | refine PresR.ite' ?_ ?_ + | (refine QPresA.modify_fix _ (fun _ => rfl) (fun _ => ⟨rfl, rfl, rfl, rfl⟩)) + | dsimp only + | split)) + +instance {α : Type} [Nonempty α] (C : α → KernelObject) (ptr : ObjRef) : QInst (getSimpleKo (Z := Unit) C ptr) := + ⟨by unfold getSimpleKo getObject; qpres⟩ +instance {α : Type} [Nonempty α] (C : α → KernelObject) (f : α → Option ObjRef) (ptr : ObjRef) : + QInst (getSkObjRef (Z := Unit) C f ptr) := ⟨by unfold getSkObjRef; qpres⟩ + +/-! ## Readers and small writers (copied from `P3/Stage3WkKeepA`) -/ + +instance {α : Type} (f : Tcb → α) (t : ObjRef) : QInst (threadGet (Z := Unit) f t) := + ⟨by unfold threadGet; qpres⟩ +instance (f : Tcb → Option ObjRef) (r : ObjRef) : QInst (getTcbObjRef (Z := Unit) f r) := + ⟨by unfold getTcbObjRef; qpres⟩ +instance (r : ObjRef) : QInst (getThreadState (Z := Unit) r) := ⟨by unfold getThreadState; qpres⟩ +instance (d : Domain) (p : Priority) : QInst (getTcbQueue (Z := Unit) d p) := ⟨by unfold getTcbQueue; qpres⟩ +instance (d : Domain) (p : Priority) (q : ReadyQueue) : QInst (setTcbQueue (Z := Unit) d p q) := + ⟨by unfold setTcbQueue; qpres⟩ +instance (a : ObjRef → List ObjRef → List ObjRef) (t : ObjRef) : QInst (tcbSchedAction (Z := Unit) a t) := + ⟨by unfold tcbSchedAction; qpres⟩ +instance (a : SchedulerAction) : QInst (setSchedulerAction (Z := Unit) a) := + ⟨by unfold setSchedulerAction; qpres⟩ +instance (t : ObjRef) : QInst (tcbReleaseRemove (Z := Unit) t) := ⟨by unfold tcbReleaseRemove; qpres⟩ + +instance (p : ObjRef) : QInst (getObject (Z := Unit) p) := ⟨by unfold getObject; qpres⟩ +instance (p : ObjRef) : QInst (getSchedContext (Z := Unit) p) := ⟨by unfold getSchedContext; qpres⟩ +instance (f : SchedContext → Option ObjRef) (p : ObjRef) : QInst (getScObjRef (Z := Unit) f p) := + ⟨by unfold getScObjRef; qpres⟩ +instance (p : ObjRef) : QInst (getRefills (Z := Unit) p) := ⟨by unfold getRefills; qpres⟩ +instance (p : ObjRef) : QInst (getRefillHead (Z := Unit) p) := ⟨by unfold getRefillHead; qpres⟩ +instance (p : ObjRef) : QInst (getRefillTail (Z := Unit) p) := ⟨by unfold getRefillTail; qpres⟩ +instance (p : ObjRef) : QInst (getScRefillReady (Z := Unit) p) := ⟨by unfold getScRefillReady; qpres⟩ +instance (p : ObjRef) (u : Ticks) : QInst (getScRefillSufficient (Z := Unit) p u) := + ⟨by unfold getScRefillSufficient; qpres⟩ +instance (p : ObjRef) (u : Ticks) : QInst (fl4GetScRefillSufficient (Z := Unit) p u) := + ⟨by unfold fl4GetScRefillSufficient; qpres⟩ +instance (p : ObjRef) : QInst (getScActive (Z := Unit) p) := ⟨by unfold getScActive; qpres⟩ +instance (p : ObjRef) : QInst (isRoundRobin (Z := Unit) p) := ⟨by unfold isRoundRobin; qpres⟩ + +/-! ## Scheduler queues -/ + +instance : QInst (rescheduleRequired (Z := Unit)) := ⟨by unfold rescheduleRequired; qpres⟩ +instance (t : ObjRef) : QInst (possibleSwitchTo (Z := Unit) t) := ⟨by unfold possibleSwitchTo; qpres⟩ + + +/-! ### Refills -/ + +instance (p : ObjRef) (f : Refill → Refill) : QInst (updateRefillHd (Z := Unit) p f) := + ⟨by unfold updateRefillHd; qpres⟩ +instance (p : ObjRef) (f : Refill → Refill) : QInst (updateRefillTl (Z := Unit) p f) := + ⟨by unfold updateRefillTl; qpres⟩ +instance (p : ObjRef) (r : Refill) : QInst (refillAddTail (Z := Unit) p r) := ⟨by unfold refillAddTail; qpres⟩ +instance (p : ObjRef) : QInst (refillPopHead (Z := Unit) p) := ⟨by unfold refillPopHead; qpres⟩ +instance (p : ObjRef) : QInst (refillSize (Z := Unit) p) := ⟨by unfold refillSize; qpres⟩ +instance (p : ObjRef) : QInst (refillSingle (Z := Unit) p) := ⟨by unfold refillSingle; qpres⟩ +instance (p : ObjRef) : QInst (refillFull (Z := Unit) p) := ⟨by unfold refillFull; qpres⟩ +instance (p : ObjRef) (r : Refill) : QInst (scheduleUsed (Z := Unit) p r) := ⟨by unfold scheduleUsed; qpres⟩ +instance (p : ObjRef) : QInst (mergeOverlappingRefills (Z := Unit) p) := ⟨by unfold mergeOverlappingRefills; qpres⟩ +instance (p : ObjRef) : QInst (refillHeadOverlappingLoop (Z := Unit) p) := + ⟨by unfold refillHeadOverlappingLoop; qpres⟩ +instance (p : ObjRef) : QInst (refillUnblockCheck (Z := Unit) p) := ⟨by unfold refillUnblockCheck; qpres⟩ +instance (o : Option ObjRef) (a b : Option Bool) : QInst (ifCondRefillUnblockCheck (Z := Unit) o a b) := + ⟨by unfold ifCondRefillUnblockCheck; qpres⟩ +instance (p : ObjRef) : QInst (mergeNonoverlappingHeadRefill (Z := Unit) p) := + ⟨by unfold mergeNonoverlappingHeadRefill; qpres⟩ +instance (m : Ticks) (p : ObjRef) : QInst (headInsufficientLoopWith (Z := Unit) m p) := + ⟨by unfold headInsufficientLoopWith; qpres⟩ +instance (p : ObjRef) (u : Ticks) : QInst (chargeEntireHeadRefill (Z := Unit) p u) := + ⟨by unfold chargeEntireHeadRefill; qpres⟩ +instance (l : Ticks) (p : ObjRef) (u : Ticks) : QInst (handleOverrunWith (Z := Unit) l p u) := + ⟨by unfold handleOverrunWith; qpres⟩ +instance (l m u : Ticks) : QInst (refillBudgetCheckWith (Z := Unit) l m u) := + ⟨by unfold refillBudgetCheckWith; qpres⟩ +instance (u : Ticks) : QInst (fl4RefillBudgetCheck (Z := Unit) u) := ⟨by unfold fl4RefillBudgetCheck; qpres⟩ +instance (u : Ticks) : QInst (refillBudgetCheckRoundRobin (Z := Unit) u) := + ⟨by unfold refillBudgetCheckRoundRobin; qpres⟩ + + +instance (t : ObjRef) : QInst (setThreadStateAct (Z := Unit) t) := ⟨by unfold setThreadStateAct; qpres⟩ +instance : QInst (rescheduleRequired (Z := Unit)) := ⟨by unfold rescheduleRequired; qpres⟩ +instance (t : ObjRef) : QInst (possibleSwitchTo (Z := Unit) t) := ⟨by unfold possibleSwitchTo; qpres⟩ + + +/-! ## The tail -/ + +instance {α β : Type} [Nonempty β] (t : α) (ts : List α) (f : α → RMonad Unit β) (R : β → β → Prop) : + QInst (orderedInsert (Z := Unit) t ts f R) := ⟨by unfold orderedInsert; qpres⟩ +instance (t : ObjRef) : QInst (tcbReleaseEnqueue (Z := Unit) t) := ⟨by unfold tcbReleaseEnqueue; qpres⟩ +instance (p : ObjRef) : QInst (postpone (Z := Unit) p) := ⟨by unfold postpone; qpres⟩ +instance (p : ObjRef) : QInst (schedContextResume (Z := Unit) p) := ⟨by unfold schedContextResume; qpres⟩ +instance (t : ObjRef) : QInst (testReschedule (Z := Unit) t) := ⟨by unfold testReschedule; qpres⟩ +instance (sc t : ObjRef) : QInst (schedContextDonate (Z := Unit) sc t) := ⟨by unfold schedContextDonate; qpres⟩ +instance (t n : ObjRef) : QInst (maybeDonateSc (Z := Unit) t n) := ⟨by unfold maybeDonateSc; qpres⟩ + +/-! ## The dequeues -/ + +theorem tcbEpDequeue_qA (t e : ObjRef) : QPresA (tcbEpDequeue (Z := Unit) t e) := by + intro σ r hr + unfold tcbEpDequeue at hr + obtain ⟨y1, hy1, h⟩ := hr + obtain ⟨e1, k1⟩ := getSimpleKo_memQA _ injEndpointQA e hy1 + rw [e1] at h + obtain ⟨y2, hy2, h⟩ := h + have e2 : y2 = (y1.1.epQueue, σ) := hy2 + subst e2 + obtain ⟨y3, hy3, h⟩ := h + have e3 : y3 = ((), σ) := hy3.2 + subst e3 + obtain ⟨y4, hy4, h⟩ := h + have e4 : y4 = (removeAll t y1.1.epQueue, σ) := hy4 + subst e4 + obtain ⟨y5, hy5, h⟩ := h + have e5 : y5 = ((match removeAll t y1.1.epQueue with + | [] => Endpoint.idleEP + | _ => updateEpQueue y1.1 (removeAll t y1.1.epQueue) true), σ) := hy5 + subst e5 + obtain ⟨y, hk, h2⟩ := setSimpleKo_memA _ e _ h + rw [h2] + refine QKeepA.upd k1 (QObjA.ofEp ?_) + have sub : (removeAll t y1.1.epQueue).Sublist y1.1.epQueue := List.filter_sublist + cases hq : removeAll t y1.1.epQueue with + | nil => exact List.nil_sublist _ + | cons a l => + rw [hq] at sub + cases hep : y1.1 with + | idleEP => rw [hep] at sub; cases sub + | sendEP q => rw [hep] at sub; exact sub + | recvEP q => rw [hep] at sub; exact sub + +instance (t e : ObjRef) : QInst (tcbEpDequeue (Z := Unit) t e) := ⟨tcbEpDequeue_qA t e⟩ + +theorem tcbNtfnDequeue_qA (t n : ObjRef) : QPresA (tcbNtfnDequeue (Z := Unit) t n) := by + intro σ r hr + unfold tcbNtfnDequeue at hr + obtain ⟨y1, hy1, h⟩ := hr + obtain ⟨e1, k1⟩ := getSimpleKo_memQA _ injNtfnQA n hy1 + rw [e1] at h + obtain ⟨y2, hy2, h⟩ := h + have e2 : y2 = (ntfnQueueOf y1.1, σ) := hy2 + subst e2 + obtain ⟨y3, hy3, h⟩ := h + have e3 : y3 = ((), σ) := hy3.2 + subst e3 + obtain ⟨y4, hy4, h⟩ := h + have e4 : y4 = (removeAll t (ntfnQueueOf y1.1), σ) := hy4 + subst e4 + obtain ⟨y5, hy5, h⟩ := h + have e5 : y5 = (ntfnObjUpdate (fun _ => match removeAll t (ntfnQueueOf y1.1) with + | [] => Ntfn.idleNtfn + | _ => Ntfn.waitingNtfn (removeAll t (ntfnQueueOf y1.1))) y1.1, σ) := hy5 + subst e5 + obtain ⟨y, hk, h2⟩ := setSimpleKo_memA _ n _ h + rw [h2] + refine QKeepA.upd k1 (QObjA.ofNtfn ?_) + have sub : (removeAll t (ntfnQueueOf y1.1)).Sublist (ntfnQueueOf y1.1) := List.filter_sublist + have eq : ntfnQueueOf y1.1 = y1.1.ntfnObj.ntfnQueue := by + unfold ntfnQueueOf; cases y1.1.ntfnObj <;> rfl + rw [← eq] + cases hq : removeAll t (ntfnQueueOf y1.1) with + | nil => exact List.nil_sublist _ + | cons a l => rw [hq] at sub; exact sub + +instance (t n : ObjRef) : QInst (tcbNtfnDequeue (Z := Unit) t n) := ⟨tcbNtfnDequeue_qA t n⟩ + +/-! ## Thread states, replies, the notification unbinds -/ + +instance (n sc : ObjRef) : QInst (doUnbindNtfnSc (Z := Unit) n sc) := ⟨by unfold doUnbindNtfnSc; qpres⟩ +instance (n t : ObjRef) : QInst (doUnbindNotification (Z := Unit) n t) := ⟨by unfold doUnbindNotification; qpres⟩ +instance (t : ObjRef) (s : ThreadState) : QInst (setThreadState (Z := Unit) t s) := ⟨by unfold setThreadState; qpres⟩ +instance (t : ObjRef) (n : Option ObjRef) : + QInst (setTcbObjRef (Z := Unit) tcbSchedContextUpdate t n) := ⟨by unfold setTcbObjRef; qpres⟩ +instance (t : ObjRef) (n : Option ObjRef) : QInst (setTcbObjRef (Z := Unit) tcbBoundNotificationUpdate t n) := + ⟨by unfold setTcbObjRef; qpres⟩ +instance (t r : ObjRef) : QInst (replyUnlinkTcb (Z := Unit) t r) := ⟨by unfold replyUnlinkTcb; qpres⟩ +instance (sc r : ObjRef) : QInst (replyUnlinkSc (Z := Unit) sc r) := ⟨by unfold replyUnlinkSc; qpres⟩ +instance (t r : ObjRef) : QInst (replyRemoveTcb (Z := Unit) t r) := ⟨by unfold replyRemoveTcb; qpres⟩ +instance (s : ThreadState) : QInst (getBlockingObject (Z := Unit) s) := ⟨by unfold getBlockingObject; qpres⟩ +instance (s : ThreadState) (t : ObjRef) (r : Option ObjRef) : QInst (blockedCancelIpc (Z := Unit) s t r) := + ⟨by unfold blockedCancelIpc; qpres⟩ +instance (t n : ObjRef) : QInst (cancelSignal (Z := Unit) t n) := ⟨by unfold cancelSignal; qpres⟩ +instance (t : ObjRef) : QInst (cancelIpc (Z := Unit) t) := ⟨by unfold cancelIpc; qpres⟩ +instance (t : ObjRef) : QInst (restartThreadIfNoFault (Z := Unit) t) := ⟨by unfold restartThreadIfNoFault; qpres⟩ +instance (t e : ObjRef) : QInst (removeAndRestartEpQueuedThread (Z := Unit) t e) := + ⟨by unfold removeAndRestartEpQueuedThread; qpres⟩ +instance (t e : ObjRef) (b : Badge) : QInst (removeAndRestartBadgedThread (Z := Unit) t e b) := + ⟨by unfold removeAndRestartBadgedThread; qpres⟩ +instance (t n : ObjRef) : QInst (removeAndRestartNtfnQueuedThread (Z := Unit) t n) := + ⟨by unfold removeAndRestartNtfnQueuedThread; qpres⟩ +instance (e : ObjRef) : QInst (cancelAllIpc (Z := Unit) e) := ⟨by unfold cancelAllIpc; qpres⟩ +instance (e : ObjRef) (b : Badge) : QInst (cancelBadgedSends (Z := Unit) e b) := ⟨by unfold cancelBadgedSends; qpres⟩ +instance (n : ObjRef) : QInst (cancelAllSignals (Z := Unit) n) := ⟨by unfold cancelAllSignals; qpres⟩ +instance (n : ObjRef) : QInst (schedContextMaybeUnbindNtfn (Z := Unit) n) := + ⟨by unfold schedContextMaybeUnbindNtfn; qpres⟩ +instance (n : ObjRef) : QInst (unbindMaybeNotification (Z := Unit) n) := ⟨by unfold unbindMaybeNotification; qpres⟩ + +instance (c r : ObjRef) : QInst (replyRemove (Z := Unit) c r) := ⟨by unfold replyRemove; qpres⟩ +instance (p : ObjRef) : QInst (schedContextUnbindTcb (Z := Unit) p) := ⟨by unfold schedContextUnbindTcb; qpres⟩ +instance (p : ObjRef) : QInst (schedContextUnbindAllTcbs (Z := Unit) p) := + ⟨by unfold schedContextUnbindAllTcbs; qpres⟩ +instance (p : ObjRef) : QInst (schedContextUnbindNtfn (Z := Unit) p) := ⟨by unfold schedContextUnbindNtfn; qpres⟩ +instance (p : ObjRef) : QInst (schedContextUnbindReply (Z := Unit) p) := ⟨by unfold schedContextUnbindReply; qpres⟩ + +/-- `QKeepA` from a result of a computation with an instance. -/ +theorem qk_of {α : Type} {m : SMonad Unit α} [h : QInst m] {σ σ' : State Unit} {a : α} (e : (a, σ') ∈ (m σ).1) : + QKeepA σ σ' := h.pres σ _ e + +/-! ## Timestamp and activation -/ + +/-- A one-word reply writes the TCB's message registers only. -/ +theorem setMrs_qA (thread : ObjRef) (buf : Option ObjRef) (msgs : List Message) + (h : msgs.length ≤ msg_registers.length) : QPresA (setMrs (Z := Unit) thread buf msgs) := by + intro σ r hr + obtain ⟨y1, hy1, hr⟩ := hr + have hy1' : y1 ∈ (NonDet.assertOpt (getTcb (Z := Unit) thread σ) σ).1 := hy1 + cases hg : getTcb (Z := Unit) thread σ with + | none => rw [hg] at hy1'; exact absurd hy1' (Set.notMem_empty _) + | some x => + rw [hg] at hy1' + have e1 : y1 = (x, σ) := hy1' + subst e1 + obtain ⟨y2, hy2, hr⟩ := hr + obtain rfl := mem_pureA hy2 + obtain ⟨y3, hy3, hr⟩ := hr + obtain rfl := mem_pureA hy3 + obtain ⟨y4, hy4, hr⟩ := hr + obtain ⟨ko, hko, hr4⟩ := setObject_memA hy4 + have F : QKeepA σ y4.2 := by + rw [hr4]; exact QKeepA.upd (getTcb_kheapA hg) (QObjA.ofTcb rfl) + obtain ⟨y5, hy5, hr⟩ := hr + obtain rfl := mem_pureA hy5 + have hd : msgs.drop msg_registers.length = [] := List.drop_eq_nil_of_le h + have hr2 : r.2 = y4.2 := by + cases buf with + | none => rw [mem_pureA hr] + | some pptr => + obtain ⟨y6, hy6, hr⟩ := hr + dsimp only at hy6 + rw [hd, zipWithMx_nil_rightA] at hy6 + obtain rfl := mem_pureA hy6 + rw [mem_pureA hr] + rw [hr2]; exact F + +instance (thread : ObjRef) (buf : Option ObjRef) (c : Time) : + QInst (setMrs (Z := Unit) thread buf (wordsFromTime c)) := + ⟨setMrs_qA _ _ _ (by simp [wordsFromTime, msg_registers, msgRegisters, Register.x2ToX5])⟩ + +instance (lookup : Bool) (t : ObjRef) : QInst (lookupIpcBuffer (Z := Unit) lookup t) := + ⟨by unfold lookupIpcBuffer; qpres⟩ +instance (thread : ObjRef) (l : Data) (c : Time) : QInst (replyFromKernel (Z := Unit) thread (l, wordsFromTime c)) := + ⟨by unfold replyFromKernel setMessageInfo; qpres⟩ +instance (scPtr : ObjRef) (cap : Ticks) (toUs : Ticks → Word64) : + QInst (schedContextUpdateConsumedWith (Z := Unit) cap toUs scPtr) := + ⟨by unfold schedContextUpdateConsumedWith; qpres⟩ +instance (t : ObjRef) : QInst (schedContextCancelYieldTo (Z := Unit) t) := + ⟨by unfold schedContextCancelYieldTo; qpres⟩ +instance (cap : Ticks) (toUs : Ticks → Word64) (t : ObjRef) : QInst (completeYieldToWith (Z := Unit) cap toUs t) := + ⟨by unfold completeYieldToWith; qpres⟩ +instance (t : ObjRef) : QInst (fl4CompleteYieldTo (Z := Unit) t) := ⟨by unfold fl4CompleteYieldTo; qpres⟩ +instance (t : ObjRef) : QInst (archActivateIdleThread (Z := Unit) t) := ⟨by unfold archActivateIdleThread; qpres⟩ + + +instance : QInst (updateTimeStamp (Z := Unit)) := ⟨by unfold updateTimeStamp; qpres⟩ +instance (t : ObjRef) : QInst (activationYieldA t) := ⟨by unfold activationYieldA; qpres⟩ +instance (t : ObjRef) : QInst (fl4ActivateState (Z := Unit) t) := ⟨by unfold fl4ActivateState; qpres⟩ + +instance (scPtr : ObjRef) : QInst (schedContextUpdateConsumed (Z := Unit) scPtr) := + ⟨by unfold schedContextUpdateConsumed; qpres⟩ +instance (t : ObjRef) : QInst (completeYieldTo (Z := Unit) t) := ⟨by unfold completeYieldTo; qpres⟩ +instance (p : ObjRef) : QInst (schedContextUnbindYieldFrom (Z := Unit) p) := + ⟨by unfold schedContextUnbindYieldFrom; qpres⟩ +instance (t : ObjRef) : QInst (unbindFromSc (Z := Unit) t) := ⟨by unfold unbindFromSc; qpres⟩ +instance (t : ObjRef) : QInst (updateRestartPc (Z := Unit) t) := ⟨by unfold updateRestartPc; qpres⟩ +instance (t : ObjRef) : QInst (suspend (Z := Unit) t) := ⟨by unfold suspend; qpres⟩ + +/-- What `update_sched_context` does. -/ +theorem updSc_memQA {f : SchedContext → SchedContext} {p : ObjRef} {σ : State Unit} {r : Unit × State Unit} + (hr : r ∈ (updateSchedContext (Z := Unit) p f σ).1) : + ∃ a n, σ.kheap p = some (.schedContext a n) ∧ + r.2 = { σ with kheap := Function.update σ.kheap p (some (.schedContext (f a) n)) } := by + obtain ⟨y1, hy1, h⟩ := hr + have hy1' : y1 ∈ (NonDet.assertOpt (σ.kheap p) σ).1 := hy1 + cases hk : σ.kheap p with + | none => rw [hk] at hy1'; exact absurd hy1' (Set.notMem_empty _) + | some ko => + rw [hk] at hy1' + have e1 : y1 = (ko, σ) := hy1' + subst e1 + cases ko with + | schedContext sc n => + obtain ⟨ko', -, hr2⟩ := setObject_memA h + exact ⟨sc, n, rfl, hr2⟩ + | _ => exact absurd h (Set.notMem_empty _) + +/-- One SC at `p` rewritten, its reply stack kept. -/ +def ScRwA (p : ObjRef) (σ σ' : State Unit) : Prop := + ∃ a b n, σ.kheap p = some (.schedContext a n) ∧ + σ' = { σ with kheap := Function.update σ.kheap p (some (.schedContext b n)) } ∧ b.scReplies = a.scReplies + +theorem ScRwA.of_upd {f : SchedContext → SchedContext} {p : ObjRef} {σ : State Unit} {r : Unit × State Unit} + (hr : r ∈ (updateSchedContext (Z := Unit) p f σ).1) (hf : ∀ y, (f y).scReplies = y.scReplies) : ScRwA p σ r.2 := by + obtain ⟨a, n, k, e⟩ := updSc_memQA hr + exact ⟨a, f a, n, k, e, hf a⟩ + +theorem ScRwA.trans {p : ObjRef} {σ1 σ2 σ3 : State Unit} (h1 : ScRwA p σ1 σ2) (h2 : ScRwA p σ2 σ3) : + ScRwA p σ1 σ3 := by + obtain ⟨a, b, n, k1, e1, r1⟩ := h1 + obtain ⟨a', b', n', k2, e2, r2⟩ := h2 + have k2' : some (KernelObject.schedContext b n) = some (.schedContext a' n') := by + rw [e1] at k2 + exact (Function.update_self p _ σ1.kheap).symm.trans k2 + cases k2' + refine ⟨a, b', n, k1, ?_, r2.trans r1⟩ + rw [e2, e1] + simp only [Function.update_idem] + +/-- **`sched_context_set_inactive` on an SC that ends with no replies** (the SC finaliser, after the reply unbinding). -/ +theorem setInactive_qA {p : ObjRef} {σ σ' : State Unit} (h : ((), σ') ∈ (schedContextSetInactive (Z := Unit) p σ).1) + (he : ∀ y k, σ'.kheap p = some (.schedContext y k) → y.scReplies = []) : QKeepA σ σ' := by + unfold schedContextSetInactive at h + obtain ⟨y1, h1, h⟩ := h + obtain ⟨y2, h2, h⟩ := h + obtain ⟨y3, h3, h4⟩ := h + have W : ScRwA p σ σ' := + (((ScRwA.of_upd h1 (fun _ => rfl)).trans (ScRwA.of_upd h2 (fun _ => rfl))).trans + (ScRwA.of_upd h3 (fun _ => rfl))).trans (ScRwA.of_upd h4 (fun _ => rfl)) + obtain ⟨a, b, n, k, e, r⟩ := W + have eb : b.scReplies = [] := he b n (by rw [e]; exact Function.update_self p _ σ.kheap) + rw [e] + exact QKeepA.upd k (QObjA.ofSc fun c => absurd eb c) + +end + +end Sched + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3Rec5A.lean b/proofs/Fl4ProofsA64/P3/Stage3Rec5A.lean new file mode 100644 index 000000000..e08e585d5 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3Rec5A.lean @@ -0,0 +1,37 @@ +/- +P3 (aarch64, record edit #5): **keeping the record's slot-cap fields** (`zwf`, `fhOK`) across a step that keeps the +caps of every typed slot. +-/ +import Fl4ProofsA64.P3.CnodeDelInvA +import Fl4ProofsA64.P3.FinKeepA +import Fl4ProofsA64.P1.MdbEptS +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- A TCB's fault-handler slot, as `slotIdx` and as the spec names it. -/ +theorem slotIdx_fhA (t : U64) : + slotIdx t layout.tcb.SLOT_FAULT_HANDLER = slotAddrA (specPtr t) (BitVec.ofNat 64 3) := by + rw [slotAddrA_sp] + apply UScalar.eq_of_val_eq + have e : layout.tcb.SLOT_FAULT_HANDLER.val = 3 := by simp [layout.tcb.SLOT_FAULT_HANDLER] + rw [slotIdx_valm t _ (by rw [e]; omega), e] + show _ = (t.bv + BitVec.ofNat 64 3 * 32#64).toNat + rw [BitVec.toNat_add, show (BitVec.ofNat 64 3 * 32#64).toNat = 96 by decide] + rfl + +theorem isSlot_fhA {th : TypedHeapA} {t : U64} (h : th t = some .tcb) : + IsSlotA th (slotIdx t layout.tcb.SLOT_FAULT_HANDLER) := + ⟨t, Or.inr ⟨h, 3, by unfold tcbSlotsA; omega, slotIdx_fhA t⟩⟩ + +theorem DelInvA.keepW {th : TypedHeapA} {s s' : S} (h : DelInvA st th s) + (e : ∀ y, IsSlotA th y → capA st s' y = capA st s y) : DelInvA st th s' := + fun y hy p zt n hc => h y hy p zt n (by rw [← e y hy]; exact hc) + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3RecvActiveA.lean b/proofs/Fl4ProofsA64/P3/Stage3RecvActiveA.lean new file mode 100644 index 000000000..a95f1e59b --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3RecvActiveA.lean @@ -0,0 +1,459 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2c): **`Wait` / `NBWait` on an active notification, at the record.** The caller gets +the badge and the notification becomes idle. The caller already has an SC, which is the current SC, so FL4's donation +and sporadic unblock check do nothing, and neither does the spec's tail. + +* **`JSyscallA.waitActiveA`** (any `blocking`). + +Every spec datum is pinned by the lookup equation, the invoked slot's cap and the spec heap at the entry state. + +[P] `ArchTotal`, `ArchPlat`, `hCs`, `hmb`, `hmax`, `htu`. New module. +-/ +import Fl4ProofsA64.P3.Stage3RecvPollA +import Fl4ProofsA64.P3.NtfnBlockA +import Fl4ProofsA64.P1.S1aCRecv +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4Spec Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {s : S} {a : A} {σ : State Unit} {m : McState} + +/-- The spec's tail of an active receive does nothing for a thread whose SC is the current SC. -/ +theorem specRecvTail_curScA {τ : State Unit} {t p : ObjRef} {x : Tcb} (hk : τ.kheap t = some (.tcb x)) + (hsc : x.tcbSchedContext = some τ.curSc) {y : SchedContext} {k : Nat} + (hy : τ.kheap τ.curSc = some (.schedContext y k)) : specRecvTail t p τ = ({((), τ)}, False) := by + unfold specRecvTail maybeDonateSc + have g := getTcbObjRef_eq (Z := Unit) Tcb.tcbSchedContext hk + rw [hsc] at g + have d : (do + let scOpt ← getTcbObjRef (Z := Unit) Tcb.tcbSchedContext t + NonDet.when (scOpt = none) <| + NonDet.bind (getNtfnObjRef Notification.ntfnSc p) <| NonDet.maybeM fun scPtr => do + let scTcb ← getScObjRef SchedContext.scTcb scPtr + NonDet.when (scTcb = none) do + schedContextDonate scPtr t + schedContextResume scPtr) τ = ({((), τ)}, False) := by + rw [nd_bind_one g] + show NonDet.when _ _ τ = _ + unfold NonDet.when + rw [if_neg (by simp)] + rfl + rw [nd_bind_one d, nd_bind_one g] + show ifCondRefillUnblockCheck (Z := Unit) (some τ.curSc) (some false) (some false) τ = _ + unfold ifCondRefillUnblockCheck NonDet.maybeM + dsimp only + rw [nd_bind_one (Endp.getSchedContext_eqA hy), nd_gets_bindSc, nd_pure_bind'] + unfold NonDet.when + split + · rename_i h + exact absurd rfl (h.2 rfl) + · rfl + +/-! ## A passive receiver (no SC) on a notification without an SC: nothing is donated -/ + +/-- **A passive current thread never resumes**: with no SC, `SchedValidA` leaves a switch pending, so the scheduler +must switch away from it at the exit. -/ +theorem RecvPreA.passive_noResumeA {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s : S} {a : A} {σ : State Unit} {m : McState} + (J : RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (hpas : ∀ x, σ.kheap σ.curThread = some (.tcb x) → x.tcbSchedContext = none) : + σ.schedulerAction ≠ .resumeCurThread := by + intro h + have hidle : σ.curThread ≠ σ.idleThread := by + intro e + obtain ⟨y, hy, hs⟩ := J.sinv.idleTcb + obtain ⟨xt, hkt, -⟩ := J.ep.tcb (L.curW s) J.sr.curT + have ct : σ.curThread = specPtr (L.curW s) := J.j.1.sch.curThread + rw [← e, ct, hkt] at hy + cases hy + rcases J.state hkt with r | r <;> rw [r] at hs <;> cases hs + obtain ⟨⟨x, sc, ex, es, -⟩, -⟩ := J.schedValid.act h hidle + rw [hpas x ex] at es + cases es + +/-- The spec's donation from a notification without an SC does nothing. -/ +theorem maybeDonateSc_nullA {τ : State Unit} {t p : ObjRef} {x : Tcb} (hk : τ.kheap t = some (.tcb x)) + (hsc : x.tcbSchedContext = none) {z : Notification} (hz : τ.kheap p = some (.notification z)) + (hzn : z.ntfnSc = none) : maybeDonateSc (Z := Unit) t p τ = ({((), τ)}, False) := by + unfold maybeDonateSc + have g := getTcbObjRef_eq (Z := Unit) Tcb.tcbSchedContext hk + rw [hsc] at g + rw [nd_bind_one g] + show NonDet.when _ _ τ = _ + unfold NonDet.when + rw [if_pos rfl] + change (getNtfnObjRef (Z := Unit) Notification.ntfnSc p >>= _) τ = _ + rw [nd_bind_one (getNtfnObjRef_eqA hz _), hzn] + rfl + +/-- The spec's tail of an active receive does nothing for a passive thread on a notification without an SC. -/ +theorem specRecvTail_nullA {τ : State Unit} {t p : ObjRef} {x : Tcb} (hk : τ.kheap t = some (.tcb x)) + (hsc : x.tcbSchedContext = none) {z : Notification} (hz : τ.kheap p = some (.notification z)) + (hzn : z.ntfnSc = none) : specRecvTail t p τ = ({((), τ)}, False) := by + unfold specRecvTail + have g := getTcbObjRef_eq (Z := Unit) Tcb.tcbSchedContext hk + rw [hsc] at g + rw [nd_bind_one (maybeDonateSc_nullA hk hsc hz hzn), nd_bind_one g] + rfl + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +/-- FL4's donation from a notification without an SC does nothing. -/ +theorem maybeDonate_nullA {s : S} {a : A} {t p : U64} (ht : L.mem s (scA t) = 0#u64) + (hn : L.mem s (Notif.nScA p) = 0#u64) : + notification.maybe_donate_sched_context st ar s a t p = ok (s, a) := by + unfold notification.maybe_donate_sched_context + obtain ⟨i, hi, ei⟩ := spec_existsA (obj.Tcb.sc.spec (st := st) s t) + rw [hi, bind_ok, ei, if_pos ht] + obtain ⟨j, hj, ej⟩ := spec_existsA (Notif.obj.Ntfn.sc.spec (st := st) s p) + rw [hj, bind_ok, ej, hn, eq_of_spec_fd (obj.Sc.is_null.spec _), bind_ok, if_pos (by rfl)] + +end + +/-- A passive thread's SC word is null. -/ +theorem RecvPreA.passiveWordA {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s : S} {a : A} {σ : State Unit} {m : McState} + (J : RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (hpas : ∀ x, σ.kheap σ.curThread = some (.tcb x) → x.tcbSchedContext = none) : + L.mem s (scA (L.curW s)) = 0#u64 := by + obtain ⟨x, kx, ex⟩ := J.sr.sch.tcb (L.curW s) J.sr.curT + have hx := hpas x (by rw [J.sr.sch.curThread]; exact kx) + rw [ex] at hx + by_contra nz + rw [optScA, if_neg nz] at hx + cases hx + +set_option maxHeartbeats 4000000 in +/-- **`Wait` / `NBWait` on an active notification** (module docstring). -/ +theorem RecvPreA.waitActiveA + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p badge slot : U64} {send canReply blk : Bool} + (hcap : cspace.lookup_cap st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.CAP)) = + ok (.Ok (.Notification p badge send true))) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hsc : capA st s slot = .Notification p badge send true) + (hp : p ∈ N) {x : Notification} (hk : σ.kheap (specPtr p) = some (.notification x)) + {b0 : Badge} (hact : x.ntfnObj = .activeNtfn b0) + (hbt : x.ntfnBoundTcb = some σ.curThread ∨ x.ntfnBoundTcb = none) + (hown : (∀ x, σ.kheap σ.curThread = some (.tcb x) → x.tcbSchedContext = some σ.curSc) ∨ + (x.ntfnSc = none ∧ ∀ y, σ.kheap σ.curThread = some (.tcb y) → y.tcbSchedContext = none)) : + HRecvPostA st AR ar hPl.ipa n Z.Cs s a m blk canReply σ + (fun σ' => ∃ y, σ.kheap σ.curThread = some (.tcb y) ∧ + σ' = rsPreσA σ σ.curThread (specPtr p) y x b0) := by + have htZ : L.curW s ∈ Z.T := J.sr.curT + set t := L.curW s with ht_def + have ct : σ.curThread = specPtr t := J.j.1.sch.curThread + obtain ⟨xt, hkt, r⟩ := J.ep.tcb t htZ + have hkc : σ.kheap σ.curThread = some (.tcb xt) := by rw [ct]; exact hkt + have hthT : Z.th t = some .tcb := (J.sr.thT t).2 htZ + have hP := J.iplaced + have hx := J.ntfn.at_ntfn hp hk + set v := L.mem s (nBadgeA p) with hv + have hb0 : v.bv = b0 := by + have := hx.obj; rw [hact] at this; exact this.2.2 + set s1 := L.wr s (Endp.regA t layout.reg.BADGE) v with hs1 + set s2 := rsPreA st s t p with hs2 + set σ1 := updK σ (specPtr t) (.tcb (setReg xt badge_register b0)) with hσ1 + set σ2 := rsPreσA σ (specPtr t) (specPtr p) xt x b0 with hσ2 + have hta := J.sr.core.tcbs t htZ + have hbn : badgeRegA t = Endp.regA t layout.reg.BADGE := by + apply UScalar.eq_of_val_eq + have := hta.fits + have h0 : (layout.reg.BADGE).val = 0 := by simp [layout.reg.BADGE, layout.reg.X0] + rw [badgeRegA_eq, Endp.regA_val (hP.e.tcb t htZ) (by rw [h0]; decide), h0, wa_valm, + Nat.mod_eq_of_lt (by simp; omega)] + simp + obtain ⟨e1, e2, hR2, hW2, -⟩ := recv_active_prefix (st := st) hP.n J.ntfn htZ hp hk hact hkt + -- the FL4 writes + have hs2' : s2 = L.wr s1 (epTSA p) ((L.mem s1 (epTSA p) &&& ~~~3#u64) ||| (obj.NTFN_IDLE &&& 3#u64)) := by + rw [hs2, rsPreA, hbn] + have hset : obj.Ntfn.set_state st p s1 obj.NTFN_IDLE = ok s2 := by + obtain ⟨q, hq, e⟩ := spec_existsA (obj.Ntfn.set_state.spec (st := st) s1 p obj.NTFN_IDLE) + rw [hq, e, ← hs2'] + have hbv : (Endp.regA t layout.reg.BADGE).val = t.val + 256 := by + have h0 : (layout.reg.BADGE).val = 0 := by simp [layout.reg.BADGE, layout.reg.X0] + rw [Endp.regA_val (hP.e.tcb t htZ) (by rw [h0]; decide), h0] + have hbal : Aligned (Endp.regA t layout.reg.BADGE) := by + have e : Endp.regA t layout.reg.BADGE = wa t 256#u64 := by + rw [← hbn, badgeRegA_eq] + rw [e]; exact field_aligned hta (by simp) (by simp) + have htsal : Aligned (epTSA p) := (foot_ts hP.n hp).1 + have htsv : p.val ≤ (epTSA p).val ∧ (epTSA p).val < p.val + 40 := by + rw [epTSA_eq, nfield_val (hP.n.ntfn p hp) (by simp)] + simp + have w1 : WritesIn st s s1 (fun b => b = Endp.regA t layout.reg.BADGE) := writesIn_wr hbal + have w2 : WritesIn st s1 s2 (fun b => b = epTSA p) := by + rw [hs2']; exact writesIn_wr htsal + have w : WritesIn st s s2 (fun b => b = Endp.regA t layout.reg.BADGE ∨ b = epTSA p) := + w1.trans w2 + have hfit := hta.fits + have htv : ∀ O : U64, O.val < 4096 → (wa t O).val = t.val + O.val := fun O hO => by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have tcbKeep : ∀ O : U64, O.val < 4096 → O.val % 8 = 0 → O.val ≠ 256 → L.mem s2 (wa t O) = L.mem s (wa t O) := by + intro O h1 h2 h3 + refine w.mem_eq (field_aligned hta h1 h2) fun c => ?_ + rcases c with c | c <;> have := congrArg (fun w : U64 => w.val) c <;> simp only [htv O h1] at this + · rw [hbv] at this; omega + · have := hP.n.disj p hp t htZ; omega + have hsc0 : L.mem s2 (scA t) = L.mem s (scA t) := by + rw [scA_eq]; exact tcbKeep _ (by simp) (by simp) (by simp) + have htH : t ∈ H.tcbs := by rw [J.tcbs]; exact htZ + have hH : H p = some .ntfn := J.utIpc.ntfn p hp + have hnsc2 : x.ntfnSc = none → L.mem s2 (Notif.nScA p) = 0#u64 := fun h => by + have h0 : L.mem s (Notif.nScA p) = 0#u64 := by + have e := hx.sc.symm.trans h + unfold optRef at e + split_ifs at e with c + exact c + rw [← h0] + refine w.mem_eq (foot_sc hP.n hp).1 fun c => ?_ + have vS : (Notif.nScA p).val = p.val + 32 := by + rw [nScA_eq, nfield_val (hP.n.ntfn p hp) (by simp)]; simp + rcases c with c | c + · have e := congrArg (fun w : U64 => w.val) c + simp only [vS, hbv] at e + have := hP.n.disj p hp t htZ + omega + · have e := congrArg (fun w : U64 => w.val) c + simp only [vS, epTSA_eq, nfield_val (hP.n.ntfn p hp) (K := 8#u64) (by simp)] at e + simp at e + have hcsc : (∀ x, σ.kheap σ.curThread = some (.tcb x) → x.tcbSchedContext = some σ.curSc) → + L.mem s2 (L.nodeA s2 layout.node.CUR_SC) = L.mem s (scA t) := fun hown => by + have ow := J.ownWords hown + have hcpu : L.cpu s2 = L.cpu s := w.2.1 + have hk2 : L.ksb s2 = L.ksb s := w.1 + have en : L.nodeA s2 layout.node.CUR_SC = L.nodeA s layout.node.CUR_SC := by + unfold LawfulStore.nodeA; rw [hcpu, nodeAt_of_ksb hk2] + rw [en, ← ow.2] + have hcl : (L.cpu s).val < 128 := by + obtain ⟨nn, -, hn, hcs⟩ := J.sr.csN + have := (hcs (L.cpu s)).1 J.sr.sch.cpuCs + omega + have ho : (layout.node.CUR_SC).val < 4288 := by simp [layout.node.CUR_SC] + have hnv := nodeAt_valSc (s := s) J.sr.ksFit hcl ho + have hr : (L.ksb s).val + 223488 ≤ (L.nodeA s layout.node.CUR_SC).val ∧ + (L.nodeA s layout.node.CUR_SC).val < (L.ksb s).val + 772352 := by + unfold LawfulStore.nodeA; rw [hnv]; omega + have al : Aligned (L.nodeA s layout.node.CUR_SC) := by + unfold LawfulStore.nodeA + exact nodeAt_aligned (J.sq.nodes _ J.sq.cur) ho (by simp [layout.node.CUR_SC]) + unfold LawfulStore.cscW + refine w.mem_eq al fun c => ?_ + rcases c with c | c + · refine J.heap_offNodesA (p := t) (b := Endp.regA t layout.reg.BADGE) htH (by omega) (by + rw [hbv]; simp [HKind.bits]) (c ▸ hr) + · refine J.heap_offNodesA (p := p) (b := epTSA p) hH htsv.1 (by + have := htsv.2; simp [HKind.bits]; omega) (c ▸ hr) + have hsig : notification.receive_signal st ar s a t p blk = ok (s2, a) := by + have h : notification.receive_signal st ar s a t p blk ⦃ q => q = (s2, a) ⦄ := by + have hsa : stOf (L.mem s) p = obj.NTFN_ACTIVE := stOf_active hx.obj hact + unfold notification.receive_signal + step with obj.Ntfn.state.spec as ⟨st0, hst0⟩ + rw [epState_eq, hsa] at hst0 + subst hst0 + rw [if_neg (by rw [ntfn_consts.1, ntfn_consts.2.2]; decide), + if_neg (by rw [ntfn_consts.2.1, ntfn_consts.2.2]; decide)] + step with obj.Ntfn.badge.spec as ⟨b, hb⟩ + refine WP.spec_bind (obj.Tcb.set_reg.spec s t _ _) ?_ + rintro q rfl + rw [hb, ← hv, ← hs1, hset, bind_ok] + have don : notification.maybe_donate_sched_context st ar s2 a t p = ok (s2, a) := by + rcases hown with hown | ⟨hnsc, hpas⟩ + · unfold notification.maybe_donate_sched_context + obtain ⟨i, hi, ei⟩ := spec_existsA (obj.Tcb.sc.spec (st := st) s2 t) + rw [hi, bind_ok, ei, hsc0, if_neg (J.ownWords hown).1] + · exact maybeDonate_nullA (by rw [hsc0]; exact J.passiveWordA hpas) (hnsc2 hnsc) + rw [don, bind_ok] + obtain ⟨i, hi, ei⟩ := spec_existsA (obj.Tcb.sc.spec (st := st) s2 t) + obtain ⟨c, hc', ec⟩ := spec_existsA (sched.cur_sc.spec (st := st) s2) + rcases hown with hown | ⟨-, hpas⟩ + · have eic : i = c := by rw [ei, ec, hsc0]; exact (hcsc hown).symm + simp [hi, hc', eic, core.cmp.PartialEq.ne.trait_default, core.cmp.PartialEq.ne.default, + obj.Sc.Insts.CoreCmpPartialEqSc, obj.Sc.Insts.CoreCmpPartialEqSc.eq] + · have i0 : i = 0#u64 := by rw [ei, hsc0]; exact J.passiveWordA hpas + simp [hi, hc', i0, core.cmp.PartialEq.ne.trait_default, core.cmp.PartialEq.ne.default, + obj.Sc.Insts.CoreCmpPartialEqSc, obj.Sc.Insts.CoreCmpPartialEqSc.eq, sc_sporadic_eq] + obtain ⟨q, hq, e⟩ := spec_existsA h + rw [hq, e] + -- the bound-thread check + have hb : L.mem s (Notif.nBoundA p) = t ∨ L.mem s (Notif.nBoundA p) = 0#u64 := by + by_cases h0 : L.mem s (Notif.nBoundA p) = 0#u64 + · exact Or.inr h0 + · rcases hbt with h | h + · rw [hx.bound, optRef, if_neg h0, ct] at h + exact Or.inl (specPtr_inj (Option.some.inj h)) + · rw [hx.bound, optRef, if_neg h0] at h + cases h + have hfl : syscall.handle_recv st ar s a blk canReply = ok (s2, a) := by + unfold syscall.handle_recv + rw [cur_thread_ok, bind_ok, tcb_reg_eqA, bind_ok] + rw [show L.curW s = t from rfl, hcap] + simp only [bind_ok, ↓reduceIte] + obtain ⟨v', hv', e⟩ := spec_existsA (Notif.obj.Ntfn.bound_tcb.spec (st := st) s p) + rw [hv', bind_ok, e] + rcases hb with h | h + · simp only [h, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte, ite_self] + exact hsig + · simp only [h, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + exact hsig + -- the spec + have tp : specPtr t ≠ specPtr p := fun e => by rw [e, hk] at hkt; cases hkt + obtain ⟨y, k, hy⟩ := J.sinv.curSc + have sct : σ.curSc ≠ specPtr t := fun e => by rw [e, hkt] at hy; cases hy + have scp : σ.curSc ≠ specPtr p := fun e => by rw [e, hk] at hy; cases hy + have hy2 : σ2.kheap σ.curSc = some (.schedContext y k) := by + show Function.update (Function.update σ.kheap _ _) _ _ σ.curSc = _ + rw [Function.update_of_ne scp, Function.update_of_ne sct]; exact hy + have ht2 : σ2.kheap (specPtr t) = some (.tcb (setReg xt badge_register b0)) := by + show Function.update (Function.update σ.kheap _ _) _ _ (specPtr t) = _ + rw [Function.update_of_ne tp, Function.update_self] + have hrecv : ((), σ2) ∈ (receiveSignal (Z := Unit) (specPtr t) + (absCapA (.Notification p badge send true)) blk σ).1 := by + have eq : receiveSignal (Z := Unit) (specPtr t) (.notificationCap (specPtr p) badge.bv + (rightsOfA send true false false)) blk σ = ({((), σ2)}, False) := by + rw [receiveSignal_cap hk] + simp only [hact] + rw [nd_bind_one e1, nd_bind_one e2] + rcases hown with hown | ⟨hnsc, hpas⟩ + · exact specRecvTail_curScA (τ := σ2) ht2 (hown xt hkc) hy2 + · have hn2 : σ2.kheap (specPtr p) = some (.notification (ntfnObjUpdate (fun _ => .idleNtfn) x)) := by + show Function.update (Function.update σ.kheap _ _) _ _ (specPtr p) = _ + rw [Function.update_self] + exact specRecvTail_nullA (τ := σ2) ht2 (hpas xt hkc) hn2 hnsc + rw [show absCapA (.Notification p badge send true) = + .notificationCap (specPtr p) badge.bv (rightsOfA send true false false) from rfl, eq] + exact Set.mem_singleton _ + have hmem : ((), σ2) ∈ (handleRecv (Z := Unit) blk canReply σ).1 := by + have capReg : (L.mem s (regWA t layout.reg.CAP)).bv = xt.tcbArch.tcbContext.userRegs cap_register := by + rw [← r.regs cap_register, regIdxA_capA]; rfl + have capRead : NonDet.liftM dataToCptr (asUser (Z := Unit) (specPtr t) (getRegister cap_register)) σ = + ({(toBl (L.mem s (regWA t layout.reg.CAP)).bv, σ)}, False) := by + unfold NonDet.liftM + rw [nd_bind_singleA' (asUser_getRegister_eqA hkt _), ← capReg] + rfl + have hc0 : (lookupCap (Z := Unit) (specPtr t) (toBl (L.mem s (regWA t layout.reg.CAP)).bv)).run σ = + ({(.ok (absCapA (.Notification p badge send true)), σ)}, False) := by + have l := post_of_okA (lookup_cap_recv_refinesA (st := st) _ s σ J.j.1.st J.j.1.cv t hthT _) hcap + obtain ⟨e, he, hrel⟩ := l + cases e with + | error _ => exact hrel.elim + | ok ac => + obtain rfl : absCapA (.Notification p badge send true) = ac := hrel.2 + exact he + have hc1 : (capFaultOnFailure (ofBl (toBl (L.mem s (regWA t layout.reg.CAP)).bv)) true + (lookupCap (Z := Unit) (specPtr t) (toBl (L.mem s (regWA t layout.reg.CAP)).bv))).run σ = + ({(.ok (absCapA (.Notification p badge send true)), σ)}, False) := run_handleE_okA hc0 + rw [handleRecv, nd_gets_bindSc, ct, nd_bind_oneSc capRead] + apply mem_bindA.2 + refine ⟨(.ok (), σ2), ?_, rfl⟩ + rw [run_bind_okA hc1] + dsimp only [absCapA] + have boundRead : (NonDetE.liftE (ε := Fault) + (getNtfnObjRef (Z := Unit) Notification.ntfnBoundTcb (specPtr p))).run σ = + ({(.ok x.ntfnBoundTcb, σ)}, False) := run_liftEA (getNtfnObjRef_eqA hk _) + rw [if_pos (by simp [AllowRecv, rightsOfA]), run_bind_okA boundRead, if_pos (by rwa [ct] at hbt)] + unfold NonDetE.liftE + rw [ExceptT.run_lift] + exact mem_bindA.2 ⟨((), σ2), hrecv, rfl⟩ + -- P1's walk + obtain ⟨Pt, Ub, Pp7, Tb7, Hr, Te, Kt, Lv7, Re, bits, KLv, W⟩ := J7A.unp J.j7 + have I := J.delIn hA hPl W + have h : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s := by + obtain ⟨q, b, x, sg, sc⟩ := J.sys.ent.bxi + exact ⟨q, b, x, sc, sg⟩ + have D := J.decPreA (extra := Aeneas.Std.Array.repeat 3#usize 0#u64) hslot (fun z hz hne => by + exact absurd (by simpa [Aeneas.Std.Array.repeat] using hz) hne) + have regs : RpPost st H Z.Cs OS Z.V ia ie Ac s s2 := by + have walk := Fl4ProofsA64.P1.W7.R.ST.receive_signal.sw (st := st) hA a t p blk I J.core0 J.ko + (SW.start J.sys.sysl J.symL h) D.cb + (by rcases J.runW with e | e | e <;> rw [e] <;> + simp [sched.TS_RUNNING, sched.TS_RESTART, sched.TS_RUNNING_VM, sched.TS_INACTIVE]) + (show p ∈ H.ntfns from hH) hH + (by rw [epState_eq, stOf_active hx.obj hact, ntfn_consts.2.2]; decide) + (J.runVcpu.zero htZ J.nvm) + (fun k hk' _ => by + rw [hH] at hk' + cases hk' + exact ⟨slot, hslot, by rw [hsc]; exact names_ntfn⟩) + exact (post_of_okA (P := fun q => SW st H Z.Cs OS Z.V ia ie Ac s q.1) walk hsig).1.post + obtain ⟨bxi, sysl, symL, userOK, j7⟩ := J.finRpA hA hPl rfl regs σ2 + have hc : m.curCore < fl4A64Platform.numNodes := by rw [J.j.1.sch.core]; exact hCs _ J.j.1.sch.cpuCs + obtain ⟨Q, Rl, J1⟩ := J.toJStage3A.of_badgeSwA hA hPl rfl hc hmb hmax htu J.body.sw J.noSelf htZ hkt v + rw [hb0, ← hs1] at J1 + have hk1 : σ1.kheap (specPtr p) = some (.notification x) := by + show Function.update σ.kheap _ _ (specPtr p) = _ + rw [Function.update_of_ne (Ne.symm tp)]; exact hk + have rv : RunVcpuInv st Z.T s2 := fun u hu hne => by + have keep : ∀ O : U64, O.val < 4096 → O.val % 8 = 0 → L.mem s2 (wa u O) = L.mem s1 (wa u O) := fun O h1 h2 => by + have hua := J.sr.core.tcbs u hu + have := hua.fits + refine w2.mem_eq (field_aligned hua h1 h2) fun c => ?_ + have e := congrArg (fun w : U64 => w.val) c + have ev : (wa u O).val = u.val + O.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have := hP.n.disj p hp u hu + simp only [ev] at e + omega + have e1' : L.mem s2 (runVcpuA u) = L.mem s1 (runVcpuA u) := keep _ (by simp) (by simp) + have e2' : L.mem s2 (stA u) = L.mem s1 (stA u) := by rw [stA_eq]; exact keep _ (by simp) (by simp) + rw [e2'] + exact J1.runVcpu u hu (e1' ▸ hne) + have R' : NtfnStateRel N Z.T (L.mem s2) σ2 := by + rw [hs2', hs1, ← hbn]; exact hR2 + obtain ⟨Q2, Rl2, J2⟩ := J1.of_ntfnObjA hc hmb hmax htu hp hk1 (fun q c => by rw [hact] at c; cases c) + (x' := ntfnObjUpdate (fun _ => .idleNtfn) x) rfl rfl rfl + (w2.mono fun b c => by subst c; exact ⟨htsv.1, by have := htsv.2; unfold NObjW at *; omega⟩) + R' bxi rv sysl symL userOK j7 + refine ⟨s2, a, σ2, hfl, hmem, ⟨xt, hkc, by rw [ct]⟩, A64RelS3.ofJ J2 ?bo, curW_keptA J.toJStage3A J2 rfl, ?bo⟩ + refine J.body.stepEq (σ' := σ2) ((J.swBadgeA hkt b0).updOther (p := specPtr p) + (by rw [updK_kheap, Function.update_of_ne (Ne.symm tp)]; exact hk) (fun _ c => by cases c) (fun _ _ c => by cases c)) rfl rfl rfl rfl rfl rfl (fun y' k' hz => ?_) (fun _ z hz => ?_) + · rw [hy2] at hz + cases hz + exact ⟨y, hy, rfl, rfl, rfl⟩ + · change σ2.kheap σ.curThread = some (.tcb z) at hz + rw [ct, ht2] at hz + cases hz + show xt.tcbState = .running ∨ xt.tcbState = .restart + exact J.state hkt + +set_option maxHeartbeats 1000000 in +/-- **`RecvPreA.waitActiveA` at a syscall entry.** -/ +theorem JSyscallA.waitActiveA + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p badge slot : U64} {send canReply blk : Bool} + (hcap : cspace.lookup_cap st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.CAP)) = + ok (.Ok (.Notification p badge send true))) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hsc : capA st s slot = .Notification p badge send true) + (hp : p ∈ N) {x : Notification} (hk : σ.kheap (specPtr p) = some (.notification x)) + {b0 : Badge} (hact : x.ntfnObj = .activeNtfn b0) + (hbt : x.ntfnBoundTcb = some σ.curThread ∨ x.ntfnBoundTcb = none) : + HRecvPostA st AR ar hPl.ipa n Z.Cs s a m blk canReply σ + (fun σ' => ∃ y, σ.kheap σ.curThread = some (.tcb y) ∧ + σ' = rsPreσA σ σ.curThread (specPtr p) y x b0) := + RecvPreA.waitActiveA hA hPl J.toRecvPreA hCs hmb hmax htu hcap hslot hcov hsc hp hk hact hbt (Or.inl fun x hx => by obtain ⟨y, hy, e, -⟩ := J.curInv.tcb; rw [hx] at hy; cases hy; exact e) + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3RecvBlkSpecA.lean b/proofs/Fl4ProofsA64/P3/Stage3RecvBlkSpecA.lean new file mode 100644 index 000000000..ed197d2b2 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3RecvBlkSpecA.lean @@ -0,0 +1,241 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2c): **the spec state after a blocking receive without a reply object** +(`blkRecvA`, the receive twin of `Stage3SendBlkSpecA`): the spec's `receive_ipc_blocked True` outcome, its objects +and scheduler data, and the record's spec-side facts across it: `SIKA` (movers: the receiver), `SpecSchedA`, +`SchedValidA`, `QBlkA`. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3SendBlkSpecA +import Fl4ProofsA64.P3.EpRecvBlkA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +/-- The receiver's TCB after a blocking receive on `e` without a reply object. -/ +abbrev rcvTcbA (xt : Tcb) (e : ObjRef) : Tcb := { xt with tcbState := .blockedOnReceive e none rcvPl0A } + +/-- **The spec's state after a blocking receive** (module docstring). -/ +def blkRecvA (σ : State Unit) (t e : ObjRef) (x : Endpoint) (xt : Tcb) : State Unit := + updK (stsAct (updK σ t (.tcb (rcvTcbA xt e))) t) e + (.endpoint (.recvEP (specIns x.epQueue (prioOf σ) xt.tcbPriority t))) + +/-- The spec's blocking receive without a reply object has this one outcome. -/ +theorem blkRecv_specA {σ : State Unit} {t e : ObjRef} {x : Endpoint} {xt : Tcb} + (hk : σ.kheap e = some (.endpoint x)) (hkt : σ.kheap t = some (.tcb xt)) (htq : t ∉ x.epQueue) + (hc : CanBlockA true x) + (hmq : ∀ y ∈ x.epQueue, ∃ z, σ.kheap y = some (.tcb z) ∧ z.tcbPriority = prioOf σ y) : + receiveIpcBlocked (Z := Unit) true t e none σ = ({((), blkRecvA σ t e x xt)}, False) := by + have hnt : e ≠ t := fun h => by rw [h, hkt] at hk; cases hk + obtain ⟨σ1, h1⟩ : ∃ σ1, σ1 = stsAct (updK σ t (.tcb (rcvTcbA xt e))) t := ⟨_, rfl⟩ + have hk2 : σ1.kheap e = some (.endpoint x) := by rw [h1, stsAct_kheap, updK_kheap, Function.update_of_ne hnt, hk] + have hkt2 : σ1.kheap t = some (.tcb (rcvTcbA xt e)) := by rw [h1, stsAct_kheap, updK_kheap, Function.update_self] + have app : tcbEpAppend (Z := Unit) t e true σ1 = ({((), blkRecvA σ t e x xt)}, False) := by + rw [tcbEpAppend_eq hk2 hkt2 (prioOf σ) (fun y hy => by + obtain ⟨z, hz, ez⟩ := hmq y hy + refine ⟨z, ?_, ez⟩ + rw [h1, stsAct_kheap, updK_kheap, Function.update_of_ne (show y ≠ t from fun h => htq (by rw [← h]; exact hy))] + exact hz), epIns_eqA hc, h1] + rfl + unfold receiveIpcBlocked + simp only [↓reduceIte] + rw [nd_bind_one (setThreadState_eq _ hkt)] + refine Eq.trans (nd_pure_bind' () (fun _ => tcbEpAppend (Z := Unit) t e true) _) ?_ + rw [← h1] + exact app + +/-- The facts of `blkRecvA` (module docstring): `t` blocked, `t` inserted into the queue of `e`, the other objects +and the scheduler queues kept. -/ +structure RcvStepA (σ σ' : State Unit) (t e : ObjRef) (x : Endpoint) (xt : Tcb) : Prop where + atT : σ'.kheap t = some (.tcb (rcvTcbA xt e)) + atE : σ'.kheap e = some (.endpoint (.recvEP (specIns x.epQueue (prioOf σ) xt.tcbPriority t))) + far : ∀ p, p ≠ t → p ≠ e → σ'.kheap p = σ.kheap p + frame : σ' = { σ with kheap := σ'.kheap, schedulerAction := σ'.schedulerAction } + act : σ'.schedulerAction = σ.schedulerAction ∨ σ'.schedulerAction = .chooseNewThread + cur : σ'.schedulerAction = .resumeCurThread → t ≠ σ.curThread + +theorem blkRecvA_step {σ : State Unit} {t e : ObjRef} {x : Endpoint} {xt : Tcb} + (hte : t ≠ e) : RcvStepA σ (blkRecvA σ t e x xt) t e x xt := by + have kh : (blkRecvA σ t e x xt).kheap = Function.update (Function.update σ.kheap t (some (.tcb (rcvTcbA xt e)))) e + (some (.endpoint (.recvEP (specIns x.epQueue (prioOf σ) xt.tcbPriority t)))) := by + unfold blkRecvA + rw [updK_kheap, stsAct_kheap, updK_kheap] + have sa : (blkRecvA σ t e x xt).schedulerAction = + (stsAct (updK σ t (.tcb (rcvTcbA xt e))) t).schedulerAction := rfl + refine ⟨?_, ?_, ?_, ?_, ?_, ?_⟩ + · rw [kh, Function.update_of_ne hte, Function.update_self] + · rw [kh, Function.update_self] + · intro p h1 h2 + rw [kh, Function.update_of_ne h2, Function.update_of_ne h1] + · unfold blkRecvA stsAct updK + split_ifs <;> rfl + · rw [sa] + unfold stsAct + split_ifs + · exact Or.inr rfl + · exact Or.inl rfl + · intro h ht + rw [sa] at h + unfold stsAct at h + split_ifs at h with c + refine c ⟨ht, h, ?_⟩ + have k : (updK σ t (.tcb (rcvTcbA xt e))).kheap t = some (.tcb (rcvTcbA xt e)) := by + rw [updK_kheap, Function.update_self] + intro hs + unfold schedulable at hs + simp only [decide_eq_true_eq] at hs + obtain ⟨h1, -, -⟩ := hs + have e : Fl4Spec.optMapSome (tcbsOf (updK σ t (.tcb (rcvTcbA xt e)))) Tcb.tcbState t = + some (ThreadState.blockedOnReceive e none rcvPl0A) := by simp [Fl4Spec.optMapSome, Fl4Spec.optMap, tcbsOf, tcbsOfKh, tcbOf, updK_kheap] + unfold Fl4Spec.optPred at h1 + rw [e] at h1 + simp [runnable] at h1 + +section +variable {σ σ' : State Unit} {t e : ObjRef} {x : Endpoint} {xt : Tcb} + +theorem RcvStepA.curThread (S : RcvStepA σ σ' t e x xt) : σ'.curThread = σ.curThread := by rw [S.frame] +theorem RcvStepA.idleThread (S : RcvStepA σ σ' t e x xt) : σ'.idleThread = σ.idleThread := by rw [S.frame] +theorem RcvStepA.readyQueues (S : RcvStepA σ σ' t e x xt) : σ'.readyQueues = σ.readyQueues := by rw [S.frame] +theorem RcvStepA.releaseQueue (S : RcvStepA σ σ' t e x xt) : σ'.releaseQueue = σ.releaseQueue := by rw [S.frame] +theorem RcvStepA.machine (S : RcvStepA σ σ' t e x xt) : σ'.machineState = σ.machineState := by rw [S.frame] + +/-- Every object of the new state: the receiver, the endpoint, or kept. -/ +theorem RcvStepA.cases (S : RcvStepA σ σ' t e x xt) {p : ObjRef} {o : KernelObject} (h : σ'.kheap p = some o) : + (p = t ∧ o = .tcb (rcvTcbA xt e)) ∨ + (p = e ∧ o = .endpoint (.recvEP (specIns x.epQueue (prioOf σ) xt.tcbPriority t))) ∨ + (p ≠ t ∧ p ≠ e ∧ σ.kheap p = some o) := by + by_cases h1 : p = t + · subst h1; rw [S.atT] at h; cases h; exact Or.inl ⟨rfl, rfl⟩ + · by_cases h2 : p = e + · subst h2; rw [S.atE] at h; cases h; exact Or.inr (Or.inl ⟨rfl, rfl⟩) + · exact Or.inr (Or.inr ⟨h1, h2, by rw [← S.far p h1 h2]; exact h⟩) + +/-- **`SIKA`** across the blocking receive: the receiver is the only mover. -/ +theorem RcvStepA.sika (S : RcvStepA σ σ' t e x xt) (hkt : σ.kheap t = some (.tcb xt)) + (hk : σ.kheap e = some (.endpoint x)) {i : ObjRef} {M N : ObjRef → Prop} {don : Bool} (hM : M t) : + SIKA i M N don σ σ' := by + refine ⟨?_, fun p => ?_, fun d pr y h => Or.inl (by rwa [S.readyQueues] at h), + fun y h => by rwa [S.releaseQueue] at h, S.act.imp_right Or.inl, fun g => ?_⟩ + rotate_left 2 + · have ht : σ'.curTime = σ.curTime := by rw [S.frame] + have hc : σ'.consumedTime = σ.consumedTime := by rw [S.frame] + have hs : σ'.curSc = σ.curSc := by rw [S.frame] + refine ⟨fun y n h => ?_, fun hne y k hy a => ?_⟩ + · rcases S.cases h with ⟨-, c⟩ | ⟨-, c⟩ | ⟨-, -, c⟩ + · cases c + · cases c + · rw [ht]; exact g.1 y n c + · rw [hc] at hne + rw [hs] at hy + rcases S.cases hy with ⟨-, c⟩ | ⟨-, c⟩ | ⟨-, -, c⟩ + · cases c + · cases c + · show refillReady σ'.curTime _ = true + rw [ht]; exact g.2 hne y k c a + · exact S.frame.trans (by rw [S.frame]) + · by_cases h1 : p = t + · subst h1 + rw [hkt, S.atT] + exact .tcb _ _ ⟨rfl, rfl, rfl, rfl, rfl, rfl, Or.inr ⟨hM, by simp⟩, Or.inl rfl, rfl, rfl⟩ + · by_cases h2 : p = e + · subst h2 + rw [hk, S.atE] + exact .ep _ _ fun u hu _ => mem_specInsA.2 (Or.inr hu) + · rw [S.far p h1 h2] + exact .same _ + +theorem RcvStepA.specSched (S : RcvStepA σ σ' t e x xt) (hkt : σ.kheap t = some (.tcb xt)) + (hk : σ.kheap e = some (.endpoint x)) : SpecSchedA σ σ' := by + refine ⟨fun p => ?_, by rw [S.frame], by rw [S.frame], by rw [S.frame], by rw [S.frame], by rw [S.frame], + by rw [S.frame]⟩ + by_cases h1 : p = t + · subst h1 + exact Or.inr (Or.inl ⟨_, _, hkt, S.atT, rfl, rfl, rfl⟩) + · by_cases h2 : p = e + · subst h2 + exact Or.inr (Or.inr ⟨_, _, hk, S.atE, Or.inr (Or.inr (Or.inl ⟨_, _, rfl, rfl⟩))⟩) + · exact Or.inl (S.far p h1 h2) + +/-- **`SchedValidA`** across the blocking receive: the receiver's SC (if any) is active. -/ +theorem RcvStepA.schedValid (S : RcvStepA σ σ' t e x xt) (V : SchedValidA σ) + (hkt : σ.kheap t = some (.tcb xt)) (hk : σ.kheap e = some (.endpoint x)) + (hsc : ∀ sc, xt.tcbSchedContext = some sc → ScActiveAtA σ sc) : SchedValidA σ' := by + have act : ∀ p, ScActiveAtA σ p → ScActiveAtA σ' p := fun p ⟨y, k, h, a⟩ => by + refine ⟨y, k, ?_, a⟩ + rw [S.far p (fun c => by rw [c, hkt] at h; cases h) (fun c => by rw [c, hk] at h; cases h)] + exact h + have tcb : ∀ u y, σ.kheap u = some (.tcb y) → ∃ y', σ'.kheap u = some (.tcb y') ∧ + y'.tcbSchedContext = y.tcbSchedContext := fun u y h => by + by_cases h1 : u = t + · subst h1; rw [hkt] at h; cases h; exact ⟨_, S.atT, rfl⟩ + · refine ⟨y, ?_, rfl⟩ + rw [S.far u h1 (fun c => by rw [c, hk] at h; cases h)] + exact h + have bound : ∀ u, (∃ y sc, σ.kheap u = some (.tcb y) ∧ y.tcbSchedContext = some sc ∧ ScActiveAtA σ sc) → + ∃ y sc, σ'.kheap u = some (.tcb y) ∧ y.tcbSchedContext = some sc ∧ ScActiveAtA σ' sc := + fun u ⟨y, sc, h, es, a⟩ => by + obtain ⟨y', h', e'⟩ := tcb u y h + exact ⟨y', sc, h', e'.trans es, act _ a⟩ + refine ⟨fun d p u hm => ?_, fun ha hi => ?_, fun u y sc hy hb es => ?_, fun u hu => ?_⟩ + · rw [S.readyQueues] at hm + obtain ⟨h, n⟩ := V.rq d p u hm + exact ⟨bound u h, by rw [S.releaseQueue]; exact n⟩ + · have ha0 : σ.schedulerAction = .resumeCurThread := by + rcases S.act with c | c + · rw [← c]; exact ha + · rw [c] at ha; cases ha + rw [S.curThread, S.idleThread] at hi + obtain ⟨h, n⟩ := V.act ha0 hi + rw [S.curThread, S.releaseQueue] + exact ⟨bound _ h, n⟩ + · rcases S.cases hy with ⟨-, ho⟩ | ⟨-, ho⟩ | ⟨-, -, hy0⟩ + · cases ho + exact act _ (hsc sc es) + · cases ho + · exact act _ (V.blk u y sc hy0 hb es) + · rw [S.releaseQueue] at hu + exact bound u (V.rls u hu) + +/-- **`QBlkA`** across the blocking receive (module docstring): `ipcSorted` at the new state. -/ +theorem RcvStepA.qblk (S : RcvStepA σ σ' t e x xt) {E N : Set ObjRef} (h : IpcSortedA E N σ) + (hkt : σ.kheap t = some (.tcb xt)) (hk : σ.kheap e = some (.endpoint x)) + (hq : ∀ a ∈ x.epQueue, ∃ y, σ.kheap a = some (.tcb y)) : QBlkA E N σ σ' := by + have hp : ∀ a y, σ.kheap a = some (.tcb y) → ∃ y', σ'.kheap a = some (.tcb y') ∧ y'.tcbPriority = y.tcbPriority := + fun a y ha => by + by_cases h1 : a = t + · subst h1; rw [hkt] at ha; cases ha; exact ⟨_, S.atT, rfl⟩ + · exact ⟨y, by rw [S.far a h1 (fun c => by rw [c, hk] at ha; cases ha)]; exact ha, rfl⟩ + have back : ∀ a y, σ'.kheap a = some (.tcb y) → ∃ y0, σ.kheap a = some (.tcb y0) ∧ y.tcbPriority = y0.tcbPriority := + fun a y ha => by + rcases S.cases ha with ⟨rfl, ho⟩ | ⟨-, ho⟩ | ⟨-, -, h0⟩ + · cases ho; exact ⟨xt, hkt, rfl⟩ + · cases ho + · exact ⟨y, h0, rfl⟩ + refine ⟨h.of_ins hkt hp back (fun p _ z hz => ?_) (fun p _ z hz => ?_), fun A p y k hy => ?_, fun p y hy => ?_, ⟨by rw [S.frame], by rw [S.frame], by rw [S.frame], by rw [S.frame]⟩⟩ + · rcases S.cases hz with ⟨-, ho⟩ | ⟨ep, ho⟩ | ⟨-, -, h0⟩ + · cases ho + · cases ho + exact ⟨x, by rw [ep]; exact hk, Or.inr ⟨rfl, hq⟩⟩ + · exact ⟨z, h0, Or.inl (List.Sublist.refl _)⟩ + · rcases S.cases hz with ⟨-, ho⟩ | ⟨-, ho⟩ | ⟨-, -, h0⟩ + · cases ho + · cases ho + · exact ⟨z, h0, Or.inl (List.Sublist.refl _)⟩ + · rcases S.cases hy with ⟨-, ho⟩ | ⟨-, ho⟩ | ⟨-, -, h0⟩ + · cases ho + · cases ho + · exact A p y k h0 + · rcases S.cases hy with ⟨-, ho⟩ | ⟨-, ho⟩ | ⟨-, -, h0⟩ + · cases ho + · cases ho + · exact ⟨y, h0, id⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3RecvBlkSpecRA.lean b/proofs/Fl4ProofsA64/P3/Stage3RecvBlkSpecRA.lean new file mode 100644 index 000000000..bdd1110b4 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3RecvBlkSpecRA.lean @@ -0,0 +1,268 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2c): **the spec state after a blocking receive with a reply object** +(`blkRecvRA`, `Stage3RecvBlkSpecA` plus the reply): the receiver blocks with `some r`, the reply `r` gets +`replyTcb := some t`, the receiver is inserted into the endpoint's queue. The record's spec-side facts across it: +`SIKA` (mover: the receiver; the reply keeps its SC), `SpecSchedA`, `SchedValidA`, `QBlkRA`. + +* `blkRecvRA`, `blkRecvR_specA`: the one outcome of `receiveIpcBlocked True t e (some r)`. +* `RcvStepRA` and its consequences (`sika`, `specSched`, `schedValid`, `qblkR`). + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3RecvBlkSpecA +import Fl4ProofsA64.P3.EpSpecA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +/-- The receiver's TCB after a blocking receive on `e` with the reply object `r`. -/ +abbrev rcvTcbRA (xt : Tcb) (e r : ObjRef) : Tcb := { xt with tcbState := .blockedOnReceive e (some r) rcvPl0A } + +/-- **The spec's state after a blocking receive with a reply object** (module docstring). -/ +def blkRecvRA (σ : State Unit) (t e r : ObjRef) (x : Endpoint) (xr : Reply) (xt : Tcb) : State Unit := + updK (updK (stsAct (updK σ t (.tcb (rcvTcbRA xt e r))) t) r (.reply { xr with replyTcb := some t })) e + (.endpoint (.recvEP (specIns x.epQueue (prioOf σ) xt.tcbPriority t))) + +/-- The spec's blocking receive with a reply object has this one outcome. -/ +theorem blkRecvR_specA {σ : State Unit} {t e r : ObjRef} {x : Endpoint} {xr : Reply} {xt : Tcb} + (hk : σ.kheap e = some (.endpoint x)) (hkt : σ.kheap t = some (.tcb xt)) (hkr : σ.kheap r = some (.reply xr)) + (htq : t ∉ x.epQueue) (hc : CanBlockA true x) + (hmq : ∀ y ∈ x.epQueue, ∃ z, σ.kheap y = some (.tcb z) ∧ z.tcbPriority = prioOf σ y) : + receiveIpcBlocked (Z := Unit) true t e (some r) σ = ({((), blkRecvRA σ t e r x xr xt)}, False) := by + have hnt : e ≠ t := fun h => by rw [h, hkt] at hk; cases hk + have hrt : r ≠ t := fun h => by rw [h, hkt] at hkr; cases hkr + have hre : r ≠ e := fun h => by rw [h, hk] at hkr; cases hkr + set σ1 := stsAct (updK σ t (.tcb (rcvTcbRA xt e r))) t with h1 + have hkr1 : σ1.kheap r = some (.reply xr) := by + rw [h1, stsAct_kheap, updK_kheap, Function.update_of_ne hrt, hkr] + set σ2 := updK σ1 r (.reply { xr with replyTcb := some t }) with h2 + have hk2 : σ2.kheap e = some (.endpoint x) := by + rw [h2, updK_kheap, Function.update_of_ne hre.symm, h1, stsAct_kheap, updK_kheap, Function.update_of_ne hnt, hk] + have hkt2 : σ2.kheap t = some (.tcb (rcvTcbRA xt e r)) := by + rw [h2, updK_kheap, Function.update_of_ne hrt.symm, h1, stsAct_kheap, updK_kheap, Function.update_self] + have app : tcbEpAppend (Z := Unit) t e true σ2 = ({((), blkRecvRA σ t e r x xr xt)}, False) := by + rw [tcbEpAppend_eq hk2 hkt2 (prioOf σ) (fun y hy => by + obtain ⟨z, hz, ez⟩ := hmq y hy + refine ⟨z, ?_, ez⟩ + have hyr : y ≠ r := fun h => by rw [h, hkr] at hz; cases hz + have hyt : y ≠ t := fun h => htq (by rw [← h]; exact hy) + rw [h2, updK_kheap, Function.update_of_ne hyr, h1, stsAct_kheap, updK_kheap, Function.update_of_ne hyt] + exact hz), epIns_eqA hc] + rfl + unfold receiveIpcBlocked + simp only [↓reduceIte] + rw [nd_bind_one (setThreadState_eq _ hkt)] + refine (nd_bind_one (setReplyTcb_eq (some t) hkr1) (fun _ => tcbEpAppend (Z := Unit) t e true)).trans ?_ + exact app + +/-- The facts of `blkRecvRA` (module docstring): `t` blocked, the reply `r` linked to `t`, `t` inserted into the queue +of `e`, the other objects and the scheduler queues kept. -/ +structure RcvStepRA (σ σ' : State Unit) (t e r : ObjRef) (x : Endpoint) (xr : Reply) (xt : Tcb) : Prop where + atT : σ'.kheap t = some (.tcb (rcvTcbRA xt e r)) + atR : σ'.kheap r = some (.reply { xr with replyTcb := some t }) + atE : σ'.kheap e = some (.endpoint (.recvEP (specIns x.epQueue (prioOf σ) xt.tcbPriority t))) + far : ∀ p, p ≠ t → p ≠ r → p ≠ e → σ'.kheap p = σ.kheap p + frame : σ' = { σ with kheap := σ'.kheap, schedulerAction := σ'.schedulerAction } + act : σ'.schedulerAction = σ.schedulerAction ∨ σ'.schedulerAction = .chooseNewThread + cur : σ'.schedulerAction = .resumeCurThread → t ≠ σ.curThread + +theorem blkRecvRA_step {σ : State Unit} {t e r : ObjRef} {x : Endpoint} {xr : Reply} {xt : Tcb} + (hte : t ≠ e) (hrt : r ≠ t) (hre : r ≠ e) : RcvStepRA σ (blkRecvRA σ t e r x xr xt) t e r x xr xt := by + have kh : (blkRecvRA σ t e r x xr xt).kheap = Function.update (Function.update (Function.update σ.kheap t + (some (.tcb (rcvTcbRA xt e r)))) r (some (.reply { xr with replyTcb := some t }))) e + (some (.endpoint (.recvEP (specIns x.epQueue (prioOf σ) xt.tcbPriority t)))) := by + unfold blkRecvRA + rw [updK_kheap, updK_kheap, stsAct_kheap, updK_kheap] + have sa : (blkRecvRA σ t e r x xr xt).schedulerAction = + (stsAct (updK σ t (.tcb (rcvTcbRA xt e r))) t).schedulerAction := rfl + refine ⟨?_, ?_, ?_, ?_, ?_, ?_, ?_⟩ + · rw [kh, Function.update_of_ne hte, Function.update_of_ne hrt.symm, Function.update_self] + · rw [kh, Function.update_of_ne hre, Function.update_self] + · rw [kh, Function.update_self] + · intro p h1 h2 h3 + rw [kh, Function.update_of_ne h3, Function.update_of_ne h2, Function.update_of_ne h1] + · unfold blkRecvRA stsAct updK + split_ifs <;> rfl + · rw [sa] + unfold stsAct + split_ifs + · exact Or.inr rfl + · exact Or.inl rfl + · intro h ht + rw [sa] at h + unfold stsAct at h + split_ifs at h with c + refine c ⟨ht, h, ?_⟩ + intro hs + unfold schedulable at hs + simp only [decide_eq_true_eq] at hs + obtain ⟨h1, -, -⟩ := hs + have e : Fl4Spec.optMapSome (tcbsOf (updK σ t (.tcb (rcvTcbRA xt e r)))) Tcb.tcbState t = + some (ThreadState.blockedOnReceive e (some r) rcvPl0A) := by + simp [Fl4Spec.optMapSome, Fl4Spec.optMap, tcbsOf, tcbsOfKh, tcbOf, updK_kheap] + unfold Fl4Spec.optPred at h1 + rw [e] at h1 + simp [runnable] at h1 + +section +variable {σ σ' : State Unit} {t e r : ObjRef} {x : Endpoint} {xr : Reply} {xt : Tcb} + +theorem RcvStepRA.curThread (S : RcvStepRA σ σ' t e r x xr xt) : σ'.curThread = σ.curThread := by rw [S.frame] +theorem RcvStepRA.idleThread (S : RcvStepRA σ σ' t e r x xr xt) : σ'.idleThread = σ.idleThread := by rw [S.frame] +theorem RcvStepRA.readyQueues (S : RcvStepRA σ σ' t e r x xr xt) : σ'.readyQueues = σ.readyQueues := by rw [S.frame] +theorem RcvStepRA.releaseQueue (S : RcvStepRA σ σ' t e r x xr xt) : σ'.releaseQueue = σ.releaseQueue := by rw [S.frame] +theorem RcvStepRA.machine (S : RcvStepRA σ σ' t e r x xr xt) : σ'.machineState = σ.machineState := by rw [S.frame] + +/-- Every object of the new state: the receiver, the reply, the endpoint, or kept. -/ +theorem RcvStepRA.cases (S : RcvStepRA σ σ' t e r x xr xt) {p : ObjRef} {o : KernelObject} + (h : σ'.kheap p = some o) : + (p = t ∧ o = .tcb (rcvTcbRA xt e r)) ∨ (p = r ∧ o = .reply { xr with replyTcb := some t }) ∨ + (p = e ∧ o = .endpoint (.recvEP (specIns x.epQueue (prioOf σ) xt.tcbPriority t))) ∨ + (p ≠ t ∧ p ≠ r ∧ p ≠ e ∧ σ.kheap p = some o) := by + by_cases h1 : p = t + · subst h1; rw [S.atT] at h; cases h; exact Or.inl ⟨rfl, rfl⟩ + · by_cases h2 : p = r + · subst h2; rw [S.atR] at h; cases h; exact Or.inr (Or.inl ⟨rfl, rfl⟩) + · by_cases h3 : p = e + · subst h3; rw [S.atE] at h; cases h; exact Or.inr (Or.inr (Or.inl ⟨rfl, rfl⟩)) + · exact Or.inr (Or.inr (Or.inr ⟨h1, h2, h3, by rw [← S.far p h1 h2 h3]; exact h⟩)) + +/-- **`SIKA`** across the blocking receive with a reply: the receiver is the only mover. -/ +theorem RcvStepRA.sika (S : RcvStepRA σ σ' t e r x xr xt) (hkt : σ.kheap t = some (.tcb xt)) + (hk : σ.kheap e = some (.endpoint x)) (hkr : σ.kheap r = some (.reply xr)) + {i : ObjRef} {M N : ObjRef → Prop} {don : Bool} (hM : M t) : + SIKA i M N don σ σ' := by + refine ⟨?_, fun p => ?_, fun d pr y h => Or.inl (by rwa [S.readyQueues] at h), + fun y h => by rwa [S.releaseQueue] at h, S.act.imp_right Or.inl, fun g => ?_⟩ + · exact S.frame.trans (by rw [S.frame]) + · by_cases h1 : p = t + · subst h1 + rw [hkt, S.atT] + exact .tcb _ _ ⟨rfl, rfl, rfl, rfl, rfl, rfl, Or.inr ⟨hM, by simp⟩, Or.inl rfl, rfl, rfl⟩ + · by_cases h2 : p = r + · subst h2 + rw [hkr, S.atR] + exact .reply _ _ fun _ => rfl + · by_cases h3 : p = e + · subst h3 + rw [hk, S.atE] + exact .ep _ _ fun u hu _ => mem_specInsA.2 (Or.inr hu) + · rw [S.far p h1 h2 h3] + exact .same _ + · have ht : σ'.curTime = σ.curTime := by rw [S.frame] + have hc' : σ'.consumedTime = σ.consumedTime := by rw [S.frame] + have hs : σ'.curSc = σ.curSc := by rw [S.frame] + refine ⟨fun y n h => ?_, fun hne y k hy a => ?_⟩ + · rcases S.cases h with ⟨-, c⟩ | ⟨-, c⟩ | ⟨-, c⟩ | ⟨-, -, -, c⟩ + · cases c + · cases c + · cases c + · rw [ht]; exact g.1 y n c + · rw [hs] at hy; rw [hc'] at hne; rw [ht] + rcases S.cases hy with ⟨-, c⟩ | ⟨-, c⟩ | ⟨-, c⟩ | ⟨-, -, -, c⟩ + · cases c + · cases c + · cases c + · exact g.2 hne y k c a + +theorem RcvStepRA.specSched (S : RcvStepRA σ σ' t e r x xr xt) (hkt : σ.kheap t = some (.tcb xt)) + (hk : σ.kheap e = some (.endpoint x)) (hkr : σ.kheap r = some (.reply xr)) : SpecSchedA σ σ' := by + refine ⟨fun p => ?_, by rw [S.frame], by rw [S.frame], by rw [S.frame], by rw [S.frame], by rw [S.frame], + by rw [S.frame]⟩ + by_cases h1 : p = t + · subst h1 + exact Or.inr (Or.inl ⟨_, _, hkt, S.atT, rfl, rfl, rfl⟩) + · by_cases h2 : p = r + · subst h2 + exact Or.inr (Or.inr ⟨_, _, hkr, S.atR, Or.inr (Or.inr (Or.inr ⟨_, _, rfl, rfl⟩))⟩) + · by_cases h3 : p = e + · subst h3 + exact Or.inr (Or.inr ⟨_, _, hk, S.atE, Or.inr (Or.inr (Or.inl ⟨_, _, rfl, rfl⟩))⟩) + · exact Or.inl (S.far p h1 h2 h3) + +/-- **`SchedValidA`** across the blocking receive with a reply: the receiver's SC (if any) is active. -/ +theorem RcvStepRA.schedValid (S : RcvStepRA σ σ' t e r x xr xt) (V : SchedValidA σ) + (hkt : σ.kheap t = some (.tcb xt)) (hk : σ.kheap e = some (.endpoint x)) (hkr : σ.kheap r = some (.reply xr)) + (hsc : ∀ sc, xt.tcbSchedContext = some sc → ScActiveAtA σ sc) : SchedValidA σ' := by + have act : ∀ p, ScActiveAtA σ p → ScActiveAtA σ' p := fun p ⟨y, k, h, a⟩ => by + refine ⟨y, k, ?_, a⟩ + rw [S.far p (fun c => by rw [c, hkt] at h; cases h) (fun c => by rw [c, hkr] at h; cases h) + (fun c => by rw [c, hk] at h; cases h)] + exact h + have tcb : ∀ u y, σ.kheap u = some (.tcb y) → ∃ y', σ'.kheap u = some (.tcb y') ∧ + y'.tcbSchedContext = y.tcbSchedContext := fun u y h => by + by_cases h1 : u = t + · subst h1; rw [hkt] at h; cases h; exact ⟨_, S.atT, rfl⟩ + · refine ⟨y, ?_, rfl⟩ + rw [S.far u h1 (fun c => by rw [c, hkr] at h; cases h) (fun c => by rw [c, hk] at h; cases h)] + exact h + have bound : ∀ u, (∃ y sc, σ.kheap u = some (.tcb y) ∧ y.tcbSchedContext = some sc ∧ ScActiveAtA σ sc) → + ∃ y sc, σ'.kheap u = some (.tcb y) ∧ y.tcbSchedContext = some sc ∧ ScActiveAtA σ' sc := + fun u ⟨y, sc, h, es, a⟩ => by + obtain ⟨y', h', e'⟩ := tcb u y h + exact ⟨y', sc, h', e'.trans es, act _ a⟩ + refine ⟨fun d p u hm => ?_, fun ha hi => ?_, fun u y sc hy hb es => ?_, fun u hu => ?_⟩ + · rw [S.readyQueues] at hm + obtain ⟨h, n⟩ := V.rq d p u hm + exact ⟨bound u h, by rw [S.releaseQueue]; exact n⟩ + · have ha0 : σ.schedulerAction = .resumeCurThread := by + rcases S.act with c | c + · rw [← c]; exact ha + · rw [c] at ha; cases ha + rw [S.curThread, S.idleThread] at hi + obtain ⟨h, n⟩ := V.act ha0 hi + rw [S.curThread, S.releaseQueue] + exact ⟨bound _ h, n⟩ + · rcases S.cases hy with ⟨-, ho⟩ | ⟨-, ho⟩ | ⟨-, ho⟩ | ⟨-, -, -, hy0⟩ + · cases ho + exact act _ (hsc sc es) + · cases ho + · cases ho + · exact act _ (V.blk u y sc hy0 hb es) + · rw [S.releaseQueue] at hu + exact bound u (V.rls u hu) + +/-- **`QBlkRA`** across the blocking receive with a reply: `ipcSorted` at the new state. -/ +theorem RcvStepRA.qblkR (S : RcvStepRA σ σ' t e r x xr xt) {E N : Set ObjRef} (h : IpcSortedA E N σ) + (hkt : σ.kheap t = some (.tcb xt)) (hk : σ.kheap e = some (.endpoint x)) (hkr : σ.kheap r = some (.reply xr)) + (hq : ∀ a ∈ x.epQueue, ∃ y, σ.kheap a = some (.tcb y)) : QBlkRA E N σ σ' := by + have hp : ∀ a y, σ.kheap a = some (.tcb y) → ∃ y', σ'.kheap a = some (.tcb y') ∧ y'.tcbPriority = y.tcbPriority := + fun a y ha => by + by_cases h1 : a = t + · subst h1; rw [hkt] at ha; cases ha; exact ⟨_, S.atT, rfl⟩ + · exact ⟨y, by rw [S.far a h1 (fun c => by rw [c, hkr] at ha; cases ha) + (fun c => by rw [c, hk] at ha; cases ha)]; exact ha, rfl⟩ + have back : ∀ a y, σ'.kheap a = some (.tcb y) → ∃ y0, σ.kheap a = some (.tcb y0) ∧ y.tcbPriority = y0.tcbPriority := + fun a y ha => by + rcases S.cases ha with ⟨rfl, ho⟩ | ⟨-, ho⟩ | ⟨-, ho⟩ | ⟨-, -, -, h0⟩ + · cases ho; exact ⟨xt, hkt, rfl⟩ + · cases ho + · cases ho + · exact ⟨y, h0, rfl⟩ + refine ⟨h.of_ins hkt hp back (fun p _ z hz => ?_) (fun p _ z hz => ?_), fun A p y k hy => ?_, + ⟨by rw [S.frame], by rw [S.frame], by rw [S.frame], by rw [S.frame]⟩⟩ + · rcases S.cases hz with ⟨-, ho⟩ | ⟨-, ho⟩ | ⟨ep, ho⟩ | ⟨-, -, -, h0⟩ + · cases ho + · cases ho + · cases ho + exact ⟨x, by rw [ep]; exact hk, Or.inr ⟨rfl, hq⟩⟩ + · exact ⟨z, h0, Or.inl (List.Sublist.refl _)⟩ + · rcases S.cases hz with ⟨-, ho⟩ | ⟨-, ho⟩ | ⟨-, ho⟩ | ⟨-, -, -, h0⟩ + · cases ho + · cases ho + · cases ho + · exact ⟨z, h0, Or.inl (List.Sublist.refl _)⟩ + · rcases S.cases hy with ⟨-, ho⟩ | ⟨-, ho⟩ | ⟨-, ho⟩ | ⟨-, -, -, h0⟩ + · cases ho + · cases ho + · cases ho + · exact A p y k h0 + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3RecvBlockA.lean b/proofs/Fl4ProofsA64/P3/Stage3RecvBlockA.lean new file mode 100644 index 000000000..a348a92ce --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3RecvBlockA.lean @@ -0,0 +1,401 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2c): **blocking `Recv` without a reply object, at the record**: `handle_recv` with +`blocking = true`, `can_reply = false` on an endpoint that is idle or holds waiting receivers. The receiver is +inserted by priority into the endpoint's queue (`blkRecvA`). P1's facts come from `receive_ipc.rpw`. + +* `JStage3A.ntfnWait_rcvA`, `JStage3A.blkQ_recvA`: the notification waiters and `BlkQueuedA` across it. +* **`JSyscallA.recvBlockA`**: `HRecvPostA`. + +Every spec datum is pinned by the lookup equation, the invoked slot's cap and the spec heap at the entry state. + +[S] the endpoint's state (idle or `RecvEP`); `can_reply = false`; no bound notification, or one that is not active and +whose SC is not the receiver's (`BndOKA`: `maybe_return_sc` is a no-op); `numNodes = 1`. +[P] `ArchTotal`, `ArchPlat`, `hCs`, `hmb`, `hmax`, `htu`. New module. +-/ +import Fl4ProofsA64.P3.Stage3SendBlkA +import Fl4ProofsA64.P3.Stage3RecvPollA +import Fl4ProofsA64.P3.Stage3RecvBlkSpecA +import Fl4ProofsA64.P3.Stage3RecvBlockKeepA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4Spec Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {J5 : JSlotA S} {R : Set U64 → JSlotA S} {s s' : S} {a : A} {σ σ' : State Unit} + {m : McState} + +/-- The write set of a blocking receive without a reply object (`wait_for_sender_block_narrowA`). -/ +def BlkRecvFpA (Wr : State Unit → S → U64 → Prop) (σ : State Unit) (s : S) (t e : U64) (xs : List U64) (b : U64) : + Prop := + (b.val = t.val + 3328 ∨ b.val = t.val + 3336 ∨ b.val = t.val + 3360) ∨ Wr σ s b ∨ EW e b ∨ ∃ w ∈ t :: xs, TL w b + +/-- **`NtfnWaitKeepMA`** across the blocking receive's write set. -/ +theorem JStage3A.ntfnWait_rcvA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7A st R) s a σ m) + {t e : U64} (ht : t ∈ Z.T) (he : e ∈ E) {xs : List U64} (hxs : ∀ y ∈ xs, y ∈ Z.T) + (hB : ∀ y ∈ xs, L.mem s (Notif.tBlockA y) = e) + (w : WritesIn st s s' (BlkRecvFpA (fun σ s => MWrA (st := st) Z m σ s) σ s t e xs)) : + NtfnWaitKeepMA (fun o => o = specPtr t) Z.T N (L.mem s) (L.mem s') := by + intro y hy hM hN + have hP := J.iplaced + have hya := hP.e.tcb y hy + have hyt : y ≠ t := fun c => hM (by rw [c]) + have hyx : y ∉ xs := fun h => by + have := hP.ne _ hN e he + rw [hB y h] at this + omega + have apart : ∀ u ∈ Z.T, u ≠ y → u.val + 4096 ≤ y.val ∨ y.val + 4096 ≤ u.val := fun u hu ne => by + have := J.sr.place.apart u y .tcb .tcb ((J.sr.thT u).2 hu) ((J.sr.thT y).2 hy) ne + have hsz : ObjKindA.size .tcb = 4096 := rfl + omega + have key : ∀ (K : U64) (b : U64), (K.val = 3336 ∨ K.val = 3496 ∨ K.val = 3504) → b = wa y K → Aligned b → + L.mem s' b = L.mem s b := by + intro K b hK hb hal + have hfy := hya.fits + have hbv : b.val = y.val + K.val := by rw [hb, wa_valm]; exact Nat.mod_eq_of_lt (by omega) + refine w.mem_eq hal ?_ + rintro (c | c | c | ⟨v, hv, c⟩) + · have := apart t ht hyt.symm + omega + · rw [hb] at c + exact J.tcbWord_offMwrA hy (by omega) (by omega) c + · have := hP.e.et e he y hy; unfold Endp.EW at c; omega + · have hvT : v ∈ Z.T := by + rcases List.mem_cons.1 hv with rfl | hv + exacts [ht, hxs v hv] + have hvy : v ≠ y := by + rcases List.mem_cons.1 hv with rfl | hv + · exact hyt.symm + · exact fun h => hyx (h ▸ hv) + have := apart v hvT hvy + have := (hP.e.tcb v hvT).fits + unfold TL at c + omega + refine ⟨?_, ?_, ?_⟩ + · exact key 3336#u64 _ (by simp) (by unfold Notif.tBlockA; rfl) (Endp.a_blk hya).1 + · exact key 3504#u64 _ (by simp) (by rw [prevEp_eq]) (Endp.a_prev hya).1 + · exact key 3496#u64 _ (by simp) (by rw [nextEp_eq]) (Endp.a_next hya).1 + +/-- **`BlkQueuedA`** across the blocking receive: the receiver is queued on the endpoint it blocks on. -/ +theorem JStage3A.blkQ_recvA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7A st R) s a σ m) + {t e : U64} (ht : t ∈ Z.T) (he : e ∈ E) {x : Endpoint} {xt : Tcb} {σb : State Unit} + (B : RcvStepA σb σ' (specPtr t) (specPtr e) x xt) {i : ObjRef} {Nq : ObjRef → Prop} {don : Bool} + (K : SIKA i (fun o => o = specPtr t) Nq don σ σ') (E' : EpStateRel Z.T E Rp Bf (L.mem s') σ') + (wk : NtfnWaitKeepMA (fun o => o = specPtr t) Z.T N (L.mem s) (L.mem s')) : + BlkQueuedA Z.T E N (L.mem s') σ' := by + have kept : ∀ u ∈ Z.T, ∀ y, σ'.kheap (specPtr u) = some (.tcb y) → specPtr u ≠ specPtr t → + ∃ z, σ.kheap (specPtr u) = some (.tcb z) ∧ z.tcbState = y.tcbState := fun u _ y hy hM => by + obtain ⟨z, hz, g⟩ := K.tcbB hy + exact ⟨z, hz, (g.st.resolve_right fun c => hM c.1).symm⟩ + refine ⟨fun u hu y hy p hb => ?_, fun u hu y hy p hp => ?_⟩ + · by_cases hM : specPtr u = specPtr t + · have eu : u = t := specPtr_inj hM + subst eu + obtain ⟨y1, hy1, r'⟩ := E'.tcb u hu + rw [hy] at hy1; cases hy1 + have hb' := r'.blk p hb + rw [B.atT] at hy; cases hy + simp only [rcvTcbA, epBlocked, Option.some.injEq] at hb + subst hb + have eq : L.mem s' (Notif.tBlockA u) = e := specPtr_inj hb' + exact ⟨by rw [eq]; exact he, _, B.atE, mem_specInsA.2 (Or.inl rfl)⟩ + · obtain ⟨z, hz, es⟩ := kept u hu y hy hM + obtain ⟨hE, q, hq, hm⟩ := J.blkQ.ep u hu z hz p (es ▸ hb) + obtain ⟨q', hq', hmem⟩ := K.epF hq + obtain ⟨x1, hx1, r⟩ := J.ep.tcb u hu + obtain ⟨y1, hy1, r'⟩ := E'.tcb u hu + rw [hz] at hx1; cases hx1 + rw [hy] at hy1; cases hy1 + have eq : L.mem s' (Notif.tBlockA u) = L.mem s (Notif.tBlockA u) := + specPtr_inj ((r'.blk p hb).trans (r.blk p (es ▸ hb)).symm) + exact ⟨by rw [eq]; exact hE, q', hq', hmem _ hm hM⟩ + · have hM : specPtr u ≠ specPtr t := fun c => by + rw [c, B.atT] at hy + cases hy + cases hp + obtain ⟨z, hz, es⟩ := kept u hu y hy hM + obtain ⟨hN, z', q, hq, ho, hm⟩ := J.blkQ.ntfn u hu z hz p (es.trans hp) + exact ⟨by rw [(wk u hu hM hN).1]; exact hN, z', q, K.ntfnF hq, ho, hm⟩ + +set_option maxHeartbeats 4000000 in +/-- **Blocking `Recv` without a reply object** (module docstring). -/ +theorem RecvPreA.recvBlockA + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p badge slot : U64} {send grant grantReply : Bool} + (hcap : cspace.lookup_cap st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.CAP)) = + ok (.Ok (.Endpoint p badge send true grant grantReply))) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) + (hsc : capA st s slot = .Endpoint p badge send true grant grantReply) + (hp : p ∈ E) {x : Endpoint} (hk : σ.kheap (specPtr p) = some (.endpoint x)) (hcb : CanBlockA true x) + (hbn : ∀ y, σ.kheap σ.curThread = some (.tcb y) → BndOKA N (L.mem s) σ (L.curW s) y true) : + HRecvPostA st AR ar hPl.ipa n Z.Cs s a m true false σ + (fun σ' => ∃ y, σ.kheap σ.curThread = some (.tcb y) ∧ + σ' = blkRecvA σ σ.curThread (specPtr p) x y) := by + have htZ : L.curW s ∈ Z.T := J.sr.curT + set t := L.curW s with ht_def + have ct : σ.curThread = specPtr t := J.j.1.sch.curThread + obtain ⟨xt, hkt, r⟩ := J.ep.tcb t htZ + have hkc : σ.kheap σ.curThread = some (.tcb xt) := by rw [ct]; exact hkt + have hbnd := hbn xt hkc + have hthT : Z.th t = some .tcb := (J.sr.thT t).2 htZ + have hnvm : L.mem s (stA t) ≠ sched.TS_RUNNING_VM := J.nvm + have hrunS : xt.tcbState = .running ∨ xt.tcbState = .restart := J.state hkt + have hr : runnable xt.tcbState = true := by rcases hrunS with e | e <;> rw [e] <;> rfl + have hidle : specPtr t ≠ σ.idleThread := by + obtain ⟨y, hy, hs⟩ := J.sinv.idleTcb + intro h + rw [← h, hkt] at hy + cases hy + rcases hrunS with e | e <;> rw [e] at hs <;> cases hs + have hRun : SndRunA σ (specPtr t) xt := J.sndRun hkt + have hsca : ∀ sc, xt.tcbSchedContext = some sc → ScActiveAtA σ sc := fun sc hs => J.scAct xt sc hkc hs + have hcm : m.curCore < fl4A64Platform.numNodes := by rw [J.j.1.sch.core]; exact hCs _ J.j.1.sch.cpuCs + have hO : SchedOpsEA st Z.T E Rp Bf (IpcSchedRelA st Z m κ) := + (ipcSchedOpsA (ar := ar) J.iplaced J.ipcPlace hcm hmb hmax htu).2.2.1 + have hF : SchedFootEA st Z.T E Rp Bf (IpcSchedRelA st Z m κ) (fun σ s => MWrA (st := st) Z m σ s) := + schedFootEA_of_sched + have hSR : IpcSchedRelA st Z m κ s σ := ⟨Z.Q, Z.R, J.sr⟩ + have hx := J.ep.at_ep hp hk + have hsort := prioSorted_of_specA J.iplaced.e.tcb J.ep hx (J.ipcSorted.1 (specPtr p) ⟨p, hp, rfl⟩ x hk) + obtain ⟨xs, hQ, hxs, hbq⟩ := hx.queue + have hxsT : ∀ y ∈ xs, y ∈ Z.T := fun y hy => hQ.mem y hy + have hxsB : ∀ y ∈ xs, L.mem s (Notif.tBlockA y) = p := fun y hy => (hbq y hy).1 + have hq : ∀ a ∈ x.epQueue, ∃ y, σ.kheap a = some (.tcb y) := by + intro a ha + rw [hxs] at ha + obtain ⟨u, hu, rfl⟩ := List.mem_map.1 ha + obtain ⟨y, hy, -⟩ := J.ep.tcb u (hxsT u hu) + exact ⟨y, hy⟩ + have hmq : ∀ y ∈ x.epQueue, ∃ z, σ.kheap y = some (.tcb z) ∧ z.tcbPriority = prioOf σ y := fun y hy => by + obtain ⟨z, hz⟩ := hq y hy + refine ⟨z, hz, ?_⟩ + simp [prioOf, Fl4Spec.optMapSome, Fl4Spec.optMap, tcbsOf, tcbsOfKh, tcbOf, hz] + have htq : specPtr t ∉ x.epQueue := not_queued_codeA J.ep htZ hkt (runnable_codeA hr) p hp x hk + have hte : specPtr t ≠ specPtr p := fun h => by rw [h, hk] at hkt; cases hkt + -- FL4 and spec: `wait_for_sender` + have body := wait_for_sender_blockA (st := st) hO hF J.iplaced.e s σ J.ep hSR htZ hkt hRun hp hk hcb hsort 0#u64 + (rep := optRef 0#u64) rfl (fun h => absurd optRef_zero h) + have narrow := wait_for_sender_block_narrowA (st := st) hO hF J.iplaced.e s σ J.ep hSR htZ hkt hRun hp hk hQ + obtain ⟨s2, ew, ⟨σ2, hspec, E2, ⟨Q, Rl, sr2⟩, -⟩, w⟩ := exists_of_specA (WP.spec_and body narrow) + rw [optRef_zero, blkRecv_specA hk hkt htq hcb hmq] at hspec + have eσ : σ2 = blkRecvA σ (specPtr t) (specPtr p) x xt := by + have h : ((), σ2) ∈ ({((), blkRecvA σ (specPtr t) (specPtr p) x xt)} : Set (Unit × State Unit)) := by + rw [(Prod.mk.inj hspec).1]; exact Set.mem_singleton _ + exact (Prod.mk.inj (Set.mem_singleton_iff.1 h)).2 + subst eσ + generalize hσ' : blkRecvA σ (specPtr t) (specPtr p) x xt = σ' at E2 sr2 + have B : RcvStepA σ σ' (specPtr t) (specPtr p) x xt := hσ' ▸ blkRecvA_step hte + -- FL4: `receive_ipc`, `handle_recv` + have hdisp : rcvDispA st ar s a t p true 0#u64 ⦃ q => q = (s2, a) ⦄ := by + have hwait : (do + let s3 ← endpoint.wait_for_sender st s t p true 0#u64 + ok (s3, a)) ⦃ q => q = (s2, a) ⦄ := by + rw [ew, bind_ok] + exact (WP.spec_ok _).2 rfl + unfold rcvDispA + step with obj.Ep.state.spec as ⟨st0, hst0⟩ + rw [epState_eq] at hst0 + cases x with + | idleEP => + rw [hx.1] at hst0 + subst hst0 + rw [if_pos ep_state_vals.1.symm] + exact hwait + | recvEP q => + rw [hx.1] at hst0 + subst hst0 + rw [if_neg (by rw [ep_state_vals.1]; decide), if_pos ep_state_vals.2.2.symm] + exact hwait + | sendEP q => simp [CanBlockA] at hcb + have hipc : endpoint.receive_ipc st ar s a t (.Endpoint p badge send true grant grantReply) true .Null = + ok (s2, a) := by + have h : endpoint.receive_ipc st ar s a t (.Endpoint p badge send true grant grantReply) true .Null ⦃ q => + q = (s2, a) ⦄ := by + rw [receive_ipc_eqA] + refine WP.spec_bind (take_reply_plainA (R := Rp) (rc' := .nullCap) + (Or.inl ⟨rfl, rfl, rfl⟩ : ReplyCapA Rp .Null .nullCap 0#u64) (fun _ h => by cases h)) ?_ + rintro q rfl + exact rcv_body_dispFlA J.ntfn J.ep htZ hkt hbnd hdisp + obtain ⟨q, hq', e⟩ := spec_existsA h + rw [hq', e] + have hfl : syscall.handle_recv st ar s a true false = ok (s2, a) := by + unfold syscall.handle_recv + rw [cur_thread_ok, bind_ok, tcb_reg_eqA, bind_ok] + rw [show L.curW s = t from rfl, hcap] + simp only [bind_ok, ↓reduceIte, Bool.false_eq_true] + exact hipc + -- the spec: `handle_recv` + have hSt := J.j.1.st + have hV := J.j.1.cv + have capReg : (L.mem s (regWA t layout.reg.CAP)).bv = xt.tcbArch.tcbContext.userRegs cap_register := by + rw [← r.regs cap_register, regIdxA_capA]; rfl + have hrecv : ((), σ') ∈ (receiveIpc (Z := Unit) (specPtr t) + (absCapA (.Endpoint p badge send true grant grantReply)) true .nullCap σ).1 := by + rw [show absCapA (.Endpoint p badge send true grant grantReply) = + .endpointCap (specPtr p) badge.bv (rightsOfA send true grant grantReply) from rfl, + receiveIpc_bodyA _ _ true (Or.inl ⟨rfl, rfl⟩ : SpecReplyPlainA σ (specPtr t) .nullCap none), + bndOK_specA hkt hk J.ntfn hbnd none, specDisp_waitA hcb, blkRecv_specA hk hkt htq hcb hmq, hσ'] + exact Set.mem_singleton _ + have hmem : ((), σ') ∈ (handleRecv (Z := Unit) true false σ).1 := by + have capRead : NonDet.liftM dataToCptr (asUser (Z := Unit) (specPtr t) (getRegister cap_register)) σ = + ({(toBl (L.mem s (regWA t layout.reg.CAP)).bv, σ)}, False) := by + unfold NonDet.liftM + rw [nd_bind_singleA' (asUser_getRegister_eqA hkt _), ← capReg] + rfl + have hc0 : (lookupCap (Z := Unit) (specPtr t) + (toBl (L.mem s (regWA t layout.reg.CAP)).bv)).run σ = + ({(.ok (absCapA (.Endpoint p badge send true grant grantReply)), σ)}, False) := by + have l := post_of_okA (lookup_cap_recv_refinesA (st := st) _ s σ hSt hV t hthT + (L.mem s (regWA t layout.reg.CAP))) hcap + obtain ⟨e, he, hrel⟩ := l + cases e with + | error _ => exact hrel.elim + | ok ac => + obtain rfl : ac = absCapA (.Endpoint p badge send true grant grantReply) := + (show absCapA (.Endpoint p badge send true grant grantReply) = ac from hrel.2).symm + exact he + rw [handleRecv, nd_gets_bindSc, ct, nd_bind_oneSc capRead] + apply mem_bindA.2 + refine ⟨(.ok (), σ'), ?_, rfl⟩ + have hc1 : (capFaultOnFailure (ofBl (toBl (L.mem s (regWA t layout.reg.CAP)).bv)) true + (lookupCap (Z := Unit) (specPtr t) (toBl (L.mem s (regWA t layout.reg.CAP)).bv))).run σ = + ({(.ok (absCapA (.Endpoint p badge send true grant grantReply)), σ)}, False) := run_handleE_okA hc0 + rw [run_bind_okA hc1] + dsimp only [absCapA] + rw [if_pos (by simp [AllowRecv, rightsOfA])] + simp only [Bool.false_eq_true, ↓reduceIte] + rw [run_bind_okA (run_returnOkA _ _)] + unfold NonDetE.liftE + rw [ExceptT.run_lift] + exact mem_bindA.2 ⟨((), σ'), hrecv, rfl⟩ + -- P1's walk + obtain ⟨Pt, Ub, Pp7, Tb7, Hr, Te, Kt, Lv7, Re, bits, KLv, W⟩ := J7A.unp J.j7 + have I := J.delIn hA hPl W + have h : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s := by + obtain ⟨q, b, x, sg, sc⟩ := J.sys.ent.bxi + exact ⟨q, b, x, sc, sg⟩ + have hext : ∀ z ∈ (Aeneas.Std.Array.repeat 3#usize 0#u64).val, z ≠ 0#u64 → z ∈ OS := fun z hz hne => by + exact absurd (by simpa [Aeneas.Std.Array.repeat] using hz) hne + have D := J.decPreA (extra := Aeneas.Std.Array.repeat 3#usize 0#u64) hslot hext + have Pz := J.toJStage3A.sysPlace hext hslot hcov + have hH : H p = some .ep := J.utIpc.ep p hp + have regs : RpPost st H Z.Cs OS Z.V ia ie Ac s s2 := by + have walk := Fl4ProofsA64.P1.W7.R.ST.receive_ipc.rpw (st := st) (ar := ar) hA a t + (.Endpoint p badge send true grant grantReply) true .Null p I J.core0 J.ko Pz (SW.start J.sys.sysl J.symL h) D.cb + (by rcases J.runW with e | e | e <;> rw [e] <;> + simp [sched.TS_RUNNING, sched.TS_RESTART, sched.TS_RUNNING_VM, sched.TS_INACTIVE]) + rfl hH + (fun k hk' => by + rw [hH] at hk' + cases hk' + exact ⟨slot, hslot, by rw [hsc]; exact names_ep⟩) + (fun _ _ c => by cases c) + (J.tcbs ▸ J.runVcpu) hnvm + exact (post_of_okA (P := fun q => RpW st H Z.Cs OS Z.V ia ie Ac s q.1) walk hipc).post + obtain ⟨bxi, sysl, symL, userOK, j7⟩ := J.toJStage3A.finRpA hA hPl rfl regs σ' + -- the record + have wI : WritesIn st s s2 (IpcFpA st Z E Rp Bf m σ s) := w.mono fun b h => by + have hta := J.iplaced.e.tcb t htZ + have tw : ∀ (k : Nat), b.val = t.val + k → TWOffN k → IpcFpA st Z E Rp Bf m σ s b := fun k hv hk' => + Or.inl (Or.inr (Or.inr (Or.inl ⟨t, htZ, Or.inl (twf_ofA k hv hk')⟩))) + rcases h with (c | c | c) | c | c | ⟨v, hv, c⟩ + · exact tw 3328 c (by unfold TWOffN; omega) + · exact tw 3336 c (by unfold TWOffN; omega) + · exact tw 3360 c (by unfold TWOffN; omega) + · exact Or.inr c + · exact Or.inl (Or.inl ⟨p, hp, c⟩) + · have hvT : v ∈ Z.T := by + rcases List.mem_cons.1 hv with rfl | hv + exacts [htZ, hxsT v hv] + exact Or.inl (Or.inr (Or.inr (Or.inl ⟨v, hvT, Or.inr c⟩))) + have wN : WritesIn st s s2 (fun b => SchedStepNWA (st := st) N E Rp Z m σ s b ∨ BufW Bf b) := + wI.mono fun _ h => h.elim (ewrite_stepNA_buf J.sr.core.tcbs) fun h => Or.inl (Or.inl (Or.inl h)) + have wI' : WritesIn st s s2 (fun b => IpcFpA st Z E Rp Bf m σ s b ∨ RepWA Rp b) := wI.mono fun _ => Or.inl + set M : ObjRef → Prop := fun o => o = specPtr t with hMdef + have K : SIKA σ.idleThread M (fun _ => False) false σ σ' := B.sika hkt hk rfl + have notM : ∀ c < n, ¬ M (m.idleThreadOf c) := by + intro c hc' h + obtain ⟨y, hy, hs⟩ := J.sidle.f3 c hc' + rw [h, hkt] at hy + cases hy + rcases hrunS with e | e <;> rw [e] at hs <;> cases hs + have sinv : SInvXA σ' m n := by + refine K.sinvSw J.sinv J.sidle J.body.sw J.noSelf (fun u hu h => J.swCur u hu h) notM (fun h => hRun.2.2.2 h.symm) ?_ (fun _ h => h.elim) (fun _ h => h.elim) + (fun y hy => by rw [show y = specPtr t from hy]; exact hRun.2.1.2) (J.sidle.rdyA J.sinv) + (K.mfA J.sinv fun q hq x hx => by + rw [show q = specPtr t from hq, hkt] at hx; cases hx + exact fun hf => by have h0 := FaultStA.notRun hf; rw [hRun.1] at h0; cases h0) + intro y hy dm pr + rw [show y = specPtr t from hy] + exact hRun.2.1.1 dm pr + have wk := J.toJStage3A.ntfnWait_rcvA (s' := s2) htZ hp hxsT hxsB w + have ntfn := J.toJStage3A.ntfn_ipcMA wI' K E2 wk (fun u hu hm y hy q h => by + rw [show specPtr u = specPtr t from hm, hkt] at hy + cases hy + rcases hrunS with e | e <;> rw [e] at h <;> cases h) (fun u hu hm y hy q h => by + rw [show specPtr u = specPtr t from hm, B.atT] at hy + cases hy + cases h) + have hMv : ∀ u ∈ Z.T, M (specPtr u) → L.mem s (Fl4ProofsA64.P1.stA u) ≠ sched.TS_RUNNING_VM := fun u _ hu => by + rw [specPtr_inj (show specPtr u = specPtr t from hu)]; exact hnvm + have ssz : SSzA σ σ' := fun q y k h => by + rcases B.cases h with ⟨-, c⟩ | ⟨-, c⟩ | ⟨-, -, c⟩ + · cases c + · cases c + · exact ⟨y, c⟩ + have J2 := J.toJStage3A.of_ipcStepUSA (Q := Q) (Rl := Rl) (curOf := Z.curOf) (curScOf := Z.curScOf) wN + (B.specSched hkt hk) ssz (B.qblk J.ipcSorted hkt hk hq) (fun _ _ => by rw [B.machine]) (fun _ _ => rfl) bxi sysl + symL userOK j7 sr2 E2 ntfn (J.toJStage3A.reply_ipcA wI K) J.deadExt.reply K.ipcKeep (J.toJStage3A.tw_ipcA wI') sinv + (K.sidle J.sidle notM (J.sidle.rdyA J.sinv)) (J.toJStage3A.runVcpu_ipcA wI' K E2 hMv) (J.toJStage3A.blkQ_recvA htZ hp B K E2 wk) + (B.schedValid J.schedValid hkt hk hsca) + refine ⟨s2, a, σ', hfl, hmem, ⟨xt, hkc, by rw [ct, hσ']⟩, A64RelS3.ofJ J2 ?bo, + curW_keptA J.toJStage3A J2 B.curThread, ?bo⟩ + refine J.body.stepEq (σ' := σ') (K.swA J.body.sw (fun u hu h => J.swCur u hu h) (fun c => by cases c) (fun _ h _ => h.elim)) (by rw [B.frame]) (by rw [B.machine]) B.curThread (by rw [B.frame]) + (by rw [B.frame]) B.releaseQueue (fun y' k' hz => ?_) (fun h => ?_) + · rcases B.cases hz with ⟨-, c⟩ | ⟨-, c⟩ | ⟨-, -, c⟩ + · cases c + · cases c + · exact ⟨y', c, rfl, rfl, rfl⟩ + · exact absurd ct.symm (B.cur h) + +set_option maxHeartbeats 1000000 in +/-- **`RecvPreA.recvBlockA` at a syscall entry.** -/ +theorem JSyscallA.recvBlockA + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p badge slot : U64} {send grant grantReply : Bool} + (hcap : cspace.lookup_cap st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.CAP)) = + ok (.Ok (.Endpoint p badge send true grant grantReply))) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) + (hsc : capA st s slot = .Endpoint p badge send true grant grantReply) + (hp : p ∈ E) {x : Endpoint} (hk : σ.kheap (specPtr p) = some (.endpoint x)) (hcb : CanBlockA true x) + (hbn : ∀ y, σ.kheap σ.curThread = some (.tcb y) → BndOKA N (L.mem s) σ (L.curW s) y true) : + HRecvPostA st AR ar hPl.ipa n Z.Cs s a m true false σ + (fun σ' => ∃ y, σ.kheap σ.curThread = some (.tcb y) ∧ + σ' = blkRecvA σ σ.curThread (specPtr p) x y) := + RecvPreA.recvBlockA hA hPl J.toRecvPreA hCs h1 hmb hmax htu hcap hslot hcov hsc hp hk hcb hbn + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3RecvBlockAnyA.lean b/proofs/Fl4ProofsA64/P3/Stage3RecvBlockAnyA.lean new file mode 100644 index 000000000..a3a51f9b9 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3RecvBlockAnyA.lean @@ -0,0 +1,97 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2c): **blocking `Recv` without a reply object for any bound notification**: +the two blocking arms behind one selector. `JStage3A.bndOK_blockA` splits `BndOKA ... false` into the arm that keeps +the receiver's SC (`recvBlockA`, `BndOKA ... true`) and the arm that returns it (`recvBlockUnbA`, `BndUnbA`), so +`hbn` is the weak `BndOKA ... false` and the post is the disjunction of the two arms' posts. + +[S] as `recvBlockA` / `recvBlockUnbA` (endpoint idle or with waiting receivers; `can_reply = false`; `numNodes = 1`). +[P] `ArchTotal`, `ArchPlat`, `hCs`, `hmb`, `hmax`, `htu`. New module. +-/ +import Fl4ProofsA64.P3.Stage3RecvBlockUnbA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4Spec Fl4SpecA64 + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s s' : S} {a : A} {σ σ' : State Unit} + {m : McState} + +/-- `HRecvPostA` is monotone in its outcome. -/ +theorem HRecvPostA.monoA {nA : U64} {n : Nat} {Cs : Set U64} {blocking canReply : Bool} {O O' : State Unit → Prop} + (h : HRecvPostA st AR ar nA n Cs s a m blocking canReply σ O) (hO : ∀ σ', O σ' → O' σ') : + HRecvPostA st AR ar nA n Cs s a m blocking canReply σ O' := by + obtain ⟨s1, a1, σ1, h1, h2, h3, h4, h5, h6⟩ := h + exact ⟨s1, a1, σ1, h1, h2, hO _ h3, h4, h5, h6⟩ + +set_option maxHeartbeats 4000000 in +/-- **Blocking `Recv` without a reply object** with any bound notification (`BndOKA ... false`). -/ +theorem RecvPreA.recvBlockAnyA + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p badge slot : U64} {send grant grantReply : Bool} + (hcap : cspace.lookup_cap st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.CAP)) = + ok (.Ok (.Endpoint p badge send true grant grantReply))) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) + (hsc : capA st s slot = .Endpoint p badge send true grant grantReply) + (hp : p ∈ E) {x : Endpoint} (hk : σ.kheap (specPtr p) = some (.endpoint x)) (hcb : CanBlockA true x) + (hbn : ∀ y, σ.kheap σ.curThread = some (.tcb y) → BndOKA N (L.mem s) σ (L.curW s) y false) : + HRecvPostA st AR ar hPl.ipa n Z.Cs s a m true false σ + (fun σ' => (∃ y, σ.kheap σ.curThread = some (.tcb y) ∧ + σ' = blkRecvA σ σ.curThread (specPtr p) x y) ∨ + (∃ y sc xs ns, σ.kheap σ.curThread = some (.tcb y) ∧ y.tcbSchedContext = some sc ∧ + σ.kheap sc = some (.schedContext xs ns) ∧ + σ' = blkRecvA (unbWaitA (preschedA σ) σ.curThread sc xs ns y) σ.curThread (specPtr p) x + { y with tcbSchedContext := none })) := by + have htZ : L.curW s ∈ Z.T := J.sr.curT + have ct : σ.curThread = specPtr (L.curW s) := J.j.1.sch.curThread + obtain ⟨xt, hkt, -⟩ := J.ep.tcb (L.curW s) htZ + have hkc : σ.kheap σ.curThread = some (.tcb xt) := by rw [ct]; exact hkt + have hcm : m.curCore < fl4A64Platform.numNodes := by rw [J.j.1.sch.core]; exact hCs _ J.j.1.sch.cpuCs + rcases J.toJStage3A.bndOK_blockA hcm htZ hkt (hbn xt hkc) with hb | hb + · exact (J.recvBlockA hA hPl hCs h1 hmb hmax htu hcap hslot hcov hsc hp hk hcb + (fun y hy => by rw [hkc] at hy; cases hy; exact hb)).monoA (fun _ h => Or.inl h) + · exact (J.recvBlockUnbA hA hPl hCs h1 hmb hmax htu hcap hslot hcov hsc hp hk hcb + (fun y hy => by rw [hkc] at hy; cases hy; exact hb)).monoA (fun _ h => Or.inr h) + +set_option maxHeartbeats 1000000 in +/-- **`RecvPreA.recvBlockAnyA` at a syscall entry.** -/ +theorem JSyscallA.recvBlockAnyA + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p badge slot : U64} {send grant grantReply : Bool} + (hcap : cspace.lookup_cap st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.CAP)) = + ok (.Ok (.Endpoint p badge send true grant grantReply))) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) + (hsc : capA st s slot = .Endpoint p badge send true grant grantReply) + (hp : p ∈ E) {x : Endpoint} (hk : σ.kheap (specPtr p) = some (.endpoint x)) (hcb : CanBlockA true x) + (hbn : ∀ y, σ.kheap σ.curThread = some (.tcb y) → BndOKA N (L.mem s) σ (L.curW s) y false) : + HRecvPostA st AR ar hPl.ipa n Z.Cs s a m true false σ + (fun σ' => (∃ y, σ.kheap σ.curThread = some (.tcb y) ∧ + σ' = blkRecvA σ σ.curThread (specPtr p) x y) ∨ + (∃ y sc xs ns, σ.kheap σ.curThread = some (.tcb y) ∧ y.tcbSchedContext = some sc ∧ + σ.kheap sc = some (.schedContext xs ns) ∧ + σ' = blkRecvA (unbWaitA σ σ.curThread sc xs ns y) σ.curThread (specPtr p) x + { y with tcbSchedContext := none })) := by + have h := RecvPreA.recvBlockAnyA hA hPl J.toRecvPreA hCs h1 hmb hmax htu hcap hslot hcov hsc hp hk hcb hbn + rw [preschedA_noswitchA J.noSwitch] at h + exact h + +end +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3RecvBlockKeepA.lean b/proofs/Fl4ProofsA64/P3/Stage3RecvBlockKeepA.lean new file mode 100644 index 000000000..2cda038db --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3RecvBlockKeepA.lean @@ -0,0 +1,172 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2c): **keeps for the caller blocking on a receive**. + +* **`ep_append_narrowA`**: `ep_append` writes only the endpoint's two words and the `Ep` links of the appended TCB and + of the queue's members (narrower than `ep_append_full`'s `EWrite`). +* **`ntfnWaitKeep_epAppendA`**: over that footprint, every notification waiter other than the appended thread keeps + its block word and `Ep` links (`NtfnWaitKeepMA`, the mover being the blocking thread). +* **`qblk_recvBlockA`**: `QBlkA` across the caller's spec TCB update and the ordered insertion into the endpoint's + queue (`IpcSortedA.of_ins`). + +* **`wait_for_sender_block_narrowA`**: the narrow write set of a blocking receive without a reply object. + +No premise. New module. +-/ +import Fl4ProofsA64.P3.EpRecvBlkA +import Fl4ProofsA64.P3.Stage3SortInsA +import Fl4ProofsA64.P3.Stage3IpcFl4KeepA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched + Fl4Spec Fl4SpecA64 + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {T E R N : Set U64} + {Bf : Set (U64 × Fl4SpecA64.Word)} + +/-- **`ep_append`'s narrow write set** (module docstring). -/ +theorem ep_append_narrowA (hP : EPlaced T E R Bf) (s : S) {e : U64} (he : e ∈ E) (t : obj.Tcb) (state : U64) + (xs : List U64) (h : P1.QueueRel (L.mem s) T .Ep (qOf (L.mem s) e) xs) (ht : t ∈ T) (hnt : t ∉ xs) : + endpoint.ep_append st s e t state ⦃ s' => + WritesIn st s s' (fun b => EW e b ∨ ∃ w ∈ t :: xs, TL w b) ⦄ := by + refine WP.spec_mono (ep_append_full hP s he t state xs h ht hnt) fun s' ⟨_, _, hA, hW⟩ => ?_ + exact ⟨hW.1, hW.2.1, fun b hb hn => hA b hb ⟨fun c => hn (Or.inl c), fun c => hn (Or.inr c)⟩⟩ + +/-- **Notification waiters other than the blocking thread are kept** (module docstring). -/ +theorem ntfnWaitKeep_epAppendA (hP : EPlaced T E R Bf) (hNE : ∀ q ∈ N, q ∉ E) {s s' : S} {e : U64} (he : e ∈ E) + {t : U64} (ht : t ∈ T) {xs : List U64} (hxs : ∀ y ∈ xs, y ∈ T) (hB : ∀ y ∈ xs, L.mem s (Notif.tBlockA y) = e) + (hee : ∀ y ∈ T, e.val + 16 ≤ y.val ∨ y.val + 4096 ≤ e.val) + (w : WritesIn st s s' (fun b => EW e b ∨ ∃ w ∈ t :: xs, TL w b)) : + NtfnWaitKeepMA (fun o => o = specPtr t) T N (L.mem s) (L.mem s') := by + intro y hy hM hN + have hyt : y ≠ t := fun h => hM (by rw [h]) + have hya := hP.tcb y hy + have hyx : y ∉ xs := fun h => hNE _ hN (by rw [hB y h]; exact he) + have key : ∀ b : U64, Aligned b → y.val + 3336 ≤ b.val → b.val < y.val + 4096 → + (b.val = y.val + 3336 ∨ b.val = y.val + 3496 ∨ b.val = y.val + 3504) → L.mem s' b = L.mem s b := by + intro b hb h1 h2 h3 + refine w.2.2 b hb ?_ + rintro (c | ⟨u, hu, c⟩) + · have := hee y hy + unfold EW at c + omega + · have huT : u ∈ T := by + rcases List.mem_cons.1 hu with rfl | hu + exacts [ht, hxs u hu] + have hne : u ≠ y := by + rcases List.mem_cons.1 hu with rfl | hu + · exact Ne.symm hyt + · exact fun h => hyx (h ▸ hu) + have := tcb_disj hP hy huT hne + unfold TL at c + omega + have v1 := val_blk hya + have v2 := val_next hya + have v3 := val_prev hya + exact ⟨key _ (a_blk hya).1 (by omega) (by omega) (Or.inl v1), + key _ (a_prev hya).1 (by omega) (by omega) (Or.inr (Or.inr v3)), + key _ (a_next hya).1 (by omega) (by omega) (Or.inr (Or.inl v2))⟩ + +/-- **A blocking receive without a reply object writes** the caller's state, block and reply words, the scheduler's +words, the endpoint's two words and the `Ep` links of the caller and the queue (`wait_for_sender_blockA`'s `FpA` +covers every TCB's links and block word). -/ +theorem wait_for_sender_block_narrowA {SR : S → State Unit → Prop} {Wr : State Unit → S → U64 → Prop} + (hO : SchedOpsEA st T E R Bf SR) (hF : SchedFootEA st T E R Bf SR Wr) (hP : EPlaced T E R Bf) (s : S) + (σ : State Unit) (hR : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) + {t : U64} (ht : t ∈ T) {xt : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) (hsr : SndRunA σ (specPtr t) xt) + {e : U64} (he : e ∈ E) {x : Endpoint} (hk : σ.kheap (specPtr e) = some (.endpoint x)) + {xs : List U64} (hQ : P1.QueueRel (L.mem s) T .Ep (qOf (L.mem s) e) xs) : + endpoint.wait_for_sender st s t e true 0#u64 ⦃ s' => WritesIn st s s' (fun b => + (b.val = t.val + 3328 ∨ b.val = t.val + 3336 ∨ b.val = t.val + 3360) ∨ Wr σ s b ∨ EW e b ∨ + ∃ w ∈ t :: xs, TL w b) ⦄ := by + have htq : specPtr t ∉ x.epQueue := not_queued_codeA hR ht hkt (runnable_codeA hsr.1) e he x hk + have hnt : specPtr e ≠ specPtr t := fun h => by rw [h, hkt] at hk; cases hk + have htxs : t ∉ xs := fun h => by + obtain ⟨zs, hz, hzx, -⟩ := (hR.at_ep he hk).queue + obtain rfl : xs = zs := hQ.unique hP.tcb hz + exact htq (by rw [hzx]; exact List.mem_map_of_mem h) + unfold endpoint.wait_for_sender + simp only [↓reduceIte] + step with obj.Tcb.set_state.spec as ⟨s1, hs1⟩ + step with obj.Tcb.set_block_obj.spec as ⟨s2, hs2⟩ + step with obj.Tcb.set_reply_obj.spec as ⟨s3, hs3⟩ + subst hs1 hs2 hs3 + obtain ⟨hA3, r1, r2, -, -⟩ := block_recv_writesA (L := L) (E := E) (R := R) (Bf := Bf) hP s ht e 0#u64 + obtain ⟨hR1, hSR1⟩ := block_recv_relA hO hP s σ hR hSR ht hkt hsr e 0#u64 (rep := none) + (by simp [optRef]) (fun h => absurd rfl h) rcvPl0A + refine WP.spec_bind (WP.spec_and (hO.scheduleTcb _ _ t hR1 hSR1 ht) (hF.scheduleTcb _ _ t hR1 hSR1 ht)) ?_ + rintro s4 ⟨⟨hf4, -⟩, hw4⟩ + have hR4 : EpStateRel T E R Bf (L.mem s4) + (stsAct (updK σ (specPtr t) (.tcb { xt with tcbState := .blockedOnReceive (specPtr e) none rcvPl0A })) + (specPtr t)) := hR1.frame hP hf4 (KView.stsAct' _ _) + have hk4 : (stsAct (updK σ (specPtr t) (.tcb { xt with tcbState := .blockedOnReceive (specPtr e) none rcvPl0A })) + (specPtr t)).kheap (specPtr e) = some (.endpoint x) := by + rw [stsAct_kheap, updK_kheap, Function.update_of_ne hnt, hk] + have hQ4 : P1.QueueRel (L.mem s4) T .Ep (qOf (L.mem s4) e) xs := by + obtain ⟨ys, hq4, -⟩ := (hR4.at_ep he hk4).queue + obtain rfl : xs = ys := hQ.unique hP.tcb (queue_sameA hP.tcb (hR.at_ep he hk) (hR4.at_ep he hk4) hq4) + exact hq4 + simp only [bne_self_eq_false, Bool.false_eq_true, ↓reduceIte, bind_ok] + refine WP.spec_mono (ep_append_narrowA hP _ he t _ xs hQ4 ht htxs) fun s' hW6 => ?_ + have W3 : WritesIn st s (L.wr (L.wr (L.wr s (stA t) sched.TS_BLOCKED_ON_RECV) (tBlockA t) e) + (tReplyA t) 0#u64) (fun b => b.val = t.val + 3328 ∨ b.val = t.val + 3336 ∨ b.val = t.val + 3360) := + ⟨by simp only [LawfulStore.ksb_wr], by simp only [LawfulStore.cpu_wr], fun b hb hn => + hA3 b hb ⟨fun h => hn (Or.inl h), fun h => hn (Or.inr (Or.inl h)), fun h => hn (Or.inr (Or.inr h))⟩⟩ + refine ((W3.trans hw4).trans hW6).mono fun b hb => ?_ + rcases hb with (h | h) | h + · exact Or.inl h + · exact Or.inr (Or.inl (hF.back (ssz_updK_tcb σ (specPtr t) _) W3.1 h)) + · exact Or.inr (Or.inr h) + +end + +/-- **`QBlkA` across a blocking receive on an endpoint** (module docstring): the caller's TCB changes keeping its +priority, then the endpoint's queue takes it by priority. -/ +theorem qblk_recvBlockA {E N : Set ObjRef} {σ σ' : State Unit} (hs : IpcSortedA E N σ) {t p : ObjRef} + {xt x1 : Tcb} {x x' : Endpoint} (hkt : σ.kheap t = some (.tcb xt)) (hk : σ.kheap p = some (.endpoint x)) + (hpr : x1.tcbPriority = xt.tcbPriority) (hmem : ∀ a ∈ x.epQueue, ∃ y, σ.kheap a = some (.tcb y)) + (hq : x'.epQueue = specIns x.epQueue (prioOf σ) xt.tcbPriority t) + (hk' : σ'.kheap = Function.update (Function.update σ.kheap t (some (.tcb x1))) p (some (.endpoint x'))) + (hd : DomKeepA σ σ') : QBlkA E N σ σ' := by + have hpt : p ≠ t := fun e => by rw [e, hkt] at hk; cases hk + have atp : σ'.kheap p = some (.endpoint x') := by rw [hk', Function.update_self] + have att : σ'.kheap t = some (.tcb x1) := by + rw [hk', Function.update_of_ne (Ne.symm hpt), Function.update_self] + have same : ∀ a, a ≠ p → a ≠ t → σ'.kheap a = σ.kheap a := fun a h1 h2 => by + rw [hk', Function.update_of_ne h1, Function.update_of_ne h2] + have back : ∀ a ob, σ'.kheap a = some ob → (∀ y, ob ≠ .tcb y) → (∀ y, ob ≠ .endpoint y) → + σ.kheap a = some ob := fun a ob h n1 n2 => by + have h1 : a ≠ p := fun e => by rw [e, atp] at h; exact n2 _ (Option.some.inj h).symm + have h2 : a ≠ t := fun e => by rw [e, att] at h; exact n1 _ (Option.some.inj h).symm + rwa [same a h1 h2] at h + refine ⟨?_, ?_, ?_, hd⟩ + · refine hs.of_ins hkt ?_ ?_ ?_ ?_ + · intro a z hz + by_cases c : a = t + · subst c; rw [hkt] at hz; cases hz; exact ⟨x1, att, hpr⟩ + · have cp : a ≠ p := fun e => by rw [e, hk] at hz; cases hz + exact ⟨z, by rw [same a cp c]; exact hz, rfl⟩ + · intro a z hz + by_cases c : a = t + · subst c; rw [att] at hz; cases hz; exact ⟨xt, hkt, hpr⟩ + · have cp : a ≠ p := fun e => by rw [e, atp] at hz; cases hz + exact ⟨z, by rwa [same a cp c] at hz, rfl⟩ + · intro q _ y hy + by_cases c : q = p + · subst c; rw [atp] at hy; cases hy + exact ⟨x, hk, Or.inr ⟨hq, hmem⟩⟩ + · have ct : q ≠ t := fun e => by rw [e, att] at hy; cases hy + exact ⟨y, by rwa [same q c ct] at hy, Or.inl (List.Sublist.refl _)⟩ + · intro q _ y hy + exact ⟨y, back q _ hy (fun _ e => by cases e) (fun _ e => by cases e), Or.inl (List.Sublist.refl _)⟩ + · intro h q y k hy + exact h q y k (back q _ hy (fun _ e => by cases e) (fun _ e => by cases e)) + · intro q y hy + exact ⟨y, back q _ hy (fun _ e => by cases e) (fun _ e => by cases e), id⟩ + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3RecvBlockKeepRA.lean b/proofs/Fl4ProofsA64/P3/Stage3RecvBlockKeepRA.lean new file mode 100644 index 000000000..4144fba77 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3RecvBlockKeepRA.lean @@ -0,0 +1,206 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2c): **keeps for the caller blocking on a receive with a reply object** +(`Stage3RecvBlockA`'s facts plus the reply object's TCB word). + +* **`wait_for_sender_block_narrowRA`**: the narrow write set of a blocking receive with a reply object `rv`: + `BlkRecvFpA` and the reply's TCB word. +* `JStage3A.ntfnWait_rcvRA`, `JStage3A.blkQ_recvRA`: the notification waiters and `BlkQueuedA` across it. + +No premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3RecvBlockA +import Fl4ProofsA64.P3.Stage3RecvBlkSpecRA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4Spec Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {J5 : JSlotA S} {R : Set U64 → JSlotA S} {s s' : S} {a : A} {σ σ' : State Unit} + {m : McState} + +/-- **A blocking receive with a reply object writes** `BlkRecvFpA`'s words and the reply's TCB word. -/ +theorem wait_for_sender_block_narrowRA {SR : S → State Unit → Prop} {Wr : State Unit → S → U64 → Prop} + {T E R : Set U64} + (hO : SchedOpsEA st T E R Bf SR) (hF : SchedFootEA st T E R Bf SR Wr) (hP : EPlaced T E R Bf) (s : S) + (σ : State Unit) (hR : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) + {t : U64} (ht : t ∈ T) {xt : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) (hsr : SndRunA σ (specPtr t) xt) + {e : U64} (he : e ∈ E) {x : Endpoint} (hk : σ.kheap (specPtr e) = some (.endpoint x)) + {xs : List U64} (hQ : P1.QueueRel (L.mem s) T .Ep (qOf (L.mem s) e) xs) {rv : U64} (hrR : rv ∈ R) : + endpoint.wait_for_sender st s t e true rv ⦃ s' => WritesIn st s s' (fun b => + ((b.val = t.val + 3328 ∨ b.val = t.val + 3336 ∨ b.val = t.val + 3360) ∨ Wr σ s b ∨ EW e b ∨ + ∃ w ∈ t :: xs, TL w b) ∨ b = Endp.rTcbA rv) ⦄ := by + have hne : rv ≠ 0#u64 := fun h => (hP.rep rv hrR).2.1 (by rw [h]; rfl) + have htq : specPtr t ∉ x.epQueue := not_queued_codeA hR ht hkt (runnable_codeA hsr.1) e he x hk + have hnt : specPtr e ≠ specPtr t := fun h => by rw [h, hkt] at hk; cases hk + have htxs : t ∉ xs := fun h => by + obtain ⟨zs, hz, hzx, -⟩ := (hR.at_ep he hk).queue + obtain rfl : xs = zs := hQ.unique hP.tcb hz + exact htq (by rw [hzx]; exact List.mem_map_of_mem h) + unfold endpoint.wait_for_sender + simp only [↓reduceIte] + step with obj.Tcb.set_state.spec as ⟨s1, hs1⟩ + step with obj.Tcb.set_block_obj.spec as ⟨s2, hs2⟩ + step with obj.Tcb.set_reply_obj.spec as ⟨s3, hs3⟩ + subst hs1 hs2 hs3 + obtain ⟨hA3, r1, r2, -, -⟩ := block_recv_writesA (L := L) (E := E) (R := R) (Bf := Bf) hP s ht e rv + obtain ⟨hR1, hSR1⟩ := block_recv_relA hO hP s σ hR hSR ht hkt hsr e rv (rep := some (specPtr rv)) + (optRef_ne hne) (fun _ => hrR) rcvPl0A + refine WP.spec_bind (WP.spec_and (hO.scheduleTcb _ _ t hR1 hSR1 ht) (hF.scheduleTcb _ _ t hR1 hSR1 ht)) ?_ + rintro s4 ⟨⟨hf4, -⟩, hw4⟩ + have hR4 : EpStateRel T E R Bf (L.mem s4) (stsAct (updK σ (specPtr t) (.tcb { xt with + tcbState := .blockedOnReceive (specPtr e) (some (specPtr rv)) rcvPl0A })) (specPtr t)) := + hR1.frame hP hf4 (KView.stsAct' _ _) + have hk4 : (stsAct (updK σ (specPtr t) (.tcb { xt with + tcbState := .blockedOnReceive (specPtr e) (some (specPtr rv)) rcvPl0A })) (specPtr t)).kheap (specPtr e) = + some (.endpoint x) := by + rw [stsAct_kheap, updK_kheap, Function.update_of_ne hnt, hk] + have hkt4 : (stsAct (updK σ (specPtr t) (.tcb { xt with + tcbState := .blockedOnReceive (specPtr e) (some (specPtr rv)) rcvPl0A })) (specPtr t)).kheap (specPtr t) = + some (.tcb { xt with tcbState := .blockedOnReceive (specPtr e) (some (specPtr rv)) rcvPl0A }) := by + rw [stsAct_kheap, updK_kheap, Function.update_self] + have hne' : (rv != 0#u64) = true := by simpa using hne + simp only [hne', ↓reduceIte] + have hB : (do + let s6 ← obj.Reply.set_tcb st rv s4 t + ok (s6, t)) ⦃ r => r = (L.wr s4 (Endp.rTcbA rv) t, t) ⦄ := + WP.spec_bind (Endp.obj.Reply.set_tcb.spec s4 rv t) fun s6 h6 => by rw [WP.spec_ok, h6] + refine WP.spec_bind hB ?_ + rintro ⟨s5, t5⟩ h5 + simp only [Prod.mk.injEq] at h5 + obtain ⟨rfl, ht5⟩ := h5 + subst t5 + have hal := (a_rtcb hP hrR).1 + have hag := agree_wr (L := L) (s := s4) (v := t) hal + obtain ⟨xr, hkr4, -⟩ := hR4.reply rv hrR + have htne : t ≠ 0#u64 := fun h => (hP.tcb t ht).ne0 (by rw [h]; rfl) + have hR5 := hR4.upd_reply hP hrR { xr with replyTcb := some (specPtr t) } + (hag.mono fun b hb => by rw [Endp.rTcbA_val]; exact hb.2) (by rw [rd_wr_eq hal, optRef_ne htne]) + have hre' : specPtr rv ≠ specPtr e := kheap_ne hkr4 hk4 (fun _ h1 h2 => by subst h1; cases h2) + have hk5 : (updK (stsAct (updK σ (specPtr t) (.tcb { xt with + tcbState := .blockedOnReceive (specPtr e) (some (specPtr rv)) rcvPl0A })) (specPtr t)) (specPtr rv) + (.reply { xr with replyTcb := some (specPtr t) })).kheap (specPtr e) = some (.endpoint x) := by + rw [updK_kheap, Function.update_of_ne hre'.symm]; exact hk4 + have hQ5 : P1.QueueRel (L.mem (L.wr s4 (Endp.rTcbA rv) t)) T .Ep (qOf (L.mem (L.wr s4 (Endp.rTcbA rv) t)) e) xs := by + obtain ⟨ys, hq5, -⟩ := (hR5.at_ep he hk5).queue + obtain rfl : xs = ys := hQ.unique hP.tcb (queue_sameA hP.tcb (hR.at_ep he hk) (hR5.at_ep he hk5) hq5) + exact hq5 + refine WP.spec_mono (ep_append_narrowA hP _ he t _ xs hQ5 ht htxs) fun s' hW6 => ?_ + have W3 : WritesIn st s (L.wr (L.wr (L.wr s (stA t) sched.TS_BLOCKED_ON_RECV) (tBlockA t) e) + (tReplyA t) rv) (fun b => b.val = t.val + 3328 ∨ b.val = t.val + 3336 ∨ b.val = t.val + 3360) := + ⟨by simp only [LawfulStore.ksb_wr], by simp only [LawfulStore.cpu_wr], fun b hb hn => + hA3 b hb ⟨fun h => hn (Or.inl h), fun h => hn (Or.inr (Or.inl h)), fun h => hn (Or.inr (Or.inr h))⟩⟩ + have W5 : WritesIn st s4 (L.wr s4 (Endp.rTcbA rv) t) (fun b => b = Endp.rTcbA rv) := writesIn_wr hal + refine (((W3.trans hw4).trans W5).trans hW6).mono fun b hb => ?_ + rcases hb with ((h | h) | h) | h | h + · exact Or.inl (Or.inl h) + · exact Or.inl (Or.inr (Or.inl (hF.back (ssz_updK_tcb σ (specPtr t) _) W3.1 h))) + · exact Or.inr h + · exact Or.inl (Or.inr (Or.inr (Or.inl h))) + · exact Or.inl (Or.inr (Or.inr (Or.inr h))) + +/-- **`NtfnWaitKeepMA`** across the blocking receive's write set with the reply object's TCB word. -/ +theorem JStage3A.ntfnWait_rcvRA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7A st R) s a σ m) + {t e r : U64} (ht : t ∈ Z.T) (he : e ∈ E) (hr : r ∈ Rp) {xs : List U64} (hxs : ∀ y ∈ xs, y ∈ Z.T) + (hB : ∀ y ∈ xs, L.mem s (Notif.tBlockA y) = e) + (w : WritesIn st s s' (fun b => BlkRecvFpA (fun σ s => MWrA (st := st) Z m σ s) σ s t e xs b ∨ b = Endp.rTcbA r)) : + NtfnWaitKeepMA (fun o => o = specPtr t) Z.T N (L.mem s) (L.mem s') := by + intro y hy hM hN + have hP := J.iplaced + have hya := hP.e.tcb y hy + have hyt : y ≠ t := fun c => hM (by rw [c]) + have hyx : y ∉ xs := fun h => by + have := hP.ne _ hN e he + rw [hB y h] at this + omega + have apart : ∀ u ∈ Z.T, u ≠ y → u.val + 4096 ≤ y.val ∨ y.val + 4096 ≤ u.val := fun u hu ne => by + have := J.sr.place.apart u y .tcb .tcb ((J.sr.thT u).2 hu) ((J.sr.thT y).2 hy) ne + have hsz : ObjKindA.size .tcb = 4096 := rfl + omega + have key : ∀ (K : U64) (b : U64), (K.val = 3336 ∨ K.val = 3496 ∨ K.val = 3504) → b = wa y K → Aligned b → + L.mem s' b = L.mem s b := by + intro K b hK hb hal + have hfy := hya.fits + have hbv : b.val = y.val + K.val := by rw [hb, wa_valm]; exact Nat.mod_eq_of_lt (by omega) + refine w.mem_eq hal ?_ + rintro ((c | c | c | ⟨v, hv, c⟩) | c) + · have := apart t ht hyt.symm + omega + · rw [hb] at c + exact J.tcbWord_offMwrA hy (by omega) (by omega) c + · have := hP.e.et e he y hy; unfold Endp.EW at c; omega + · have hvT : v ∈ Z.T := by + rcases List.mem_cons.1 hv with rfl | hv + exacts [ht, hxs v hv] + have hvy : v ≠ y := by + rcases List.mem_cons.1 hv with rfl | hv + · exact hyt.symm + · exact fun h => hyx (h ▸ hv) + have := apart v hvT hvy + have := (hP.e.tcb v hvT).fits + unfold TL at c + omega + · have := hP.e.rt r hr y hy + rw [c, Endp.rTcbA_val] at hbv + omega + refine ⟨?_, ?_, ?_⟩ + · exact key 3336#u64 _ (by simp) (by unfold Notif.tBlockA; rfl) (Endp.a_blk hya).1 + · exact key 3504#u64 _ (by simp) (by rw [prevEp_eq]) (Endp.a_prev hya).1 + · exact key 3496#u64 _ (by simp) (by rw [nextEp_eq]) (Endp.a_next hya).1 + +/-- **`BlkQueuedA`** across the blocking receive with a reply object: the receiver is queued on the endpoint it blocks on. -/ +theorem JStage3A.blkQ_recvRA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7A st R) s a σ m) + {t e r : U64} (ht : t ∈ Z.T) (he : e ∈ E) {x : Endpoint} {xr : Reply} {xt : Tcb} {σb : State Unit} + (B : RcvStepRA σb σ' (specPtr t) (specPtr e) (specPtr r) x xr xt) {i : ObjRef} {Nq : ObjRef → Prop} {don : Bool} + (K : SIKA i (fun o => o = specPtr t) Nq don σ σ') (E' : EpStateRel Z.T E Rp Bf (L.mem s') σ') + (wk : NtfnWaitKeepMA (fun o => o = specPtr t) Z.T N (L.mem s) (L.mem s')) : + BlkQueuedA Z.T E N (L.mem s') σ' := by + have kept : ∀ u ∈ Z.T, ∀ y, σ'.kheap (specPtr u) = some (.tcb y) → specPtr u ≠ specPtr t → + ∃ z, σ.kheap (specPtr u) = some (.tcb z) ∧ z.tcbState = y.tcbState := fun u _ y hy hM => by + obtain ⟨z, hz, g⟩ := K.tcbB hy + exact ⟨z, hz, (g.st.resolve_right fun c => hM c.1).symm⟩ + refine ⟨fun u hu y hy p hb => ?_, fun u hu y hy p hp => ?_⟩ + · by_cases hM : specPtr u = specPtr t + · have eu : u = t := specPtr_inj hM + subst eu + obtain ⟨y1, hy1, r'⟩ := E'.tcb u hu + rw [hy] at hy1; cases hy1 + have hb' := r'.blk p hb + rw [B.atT] at hy; cases hy + simp only [rcvTcbA, epBlocked, Option.some.injEq] at hb + subst hb + have eq : L.mem s' (Notif.tBlockA u) = e := specPtr_inj hb' + exact ⟨by rw [eq]; exact he, _, B.atE, mem_specInsA.2 (Or.inl rfl)⟩ + · obtain ⟨z, hz, es⟩ := kept u hu y hy hM + obtain ⟨hE, q, hq, hm⟩ := J.blkQ.ep u hu z hz p (es ▸ hb) + obtain ⟨q', hq', hmem⟩ := K.epF hq + obtain ⟨x1, hx1, r⟩ := J.ep.tcb u hu + obtain ⟨y1, hy1, r'⟩ := E'.tcb u hu + rw [hz] at hx1; cases hx1 + rw [hy] at hy1; cases hy1 + have eq : L.mem s' (Notif.tBlockA u) = L.mem s (Notif.tBlockA u) := + specPtr_inj ((r'.blk p hb).trans (r.blk p (es ▸ hb)).symm) + exact ⟨by rw [eq]; exact hE, q', hq', hmem _ hm hM⟩ + · have hM : specPtr u ≠ specPtr t := fun c => by + rw [c, B.atT] at hy + cases hy + cases hp + obtain ⟨z, hz, es⟩ := kept u hu y hy hM + obtain ⟨hN, z', q, hq, ho, hm⟩ := J.blkQ.ntfn u hu z hz p (es.trans hp) + exact ⟨by rw [(wk u hu hM hN).1]; exact hN, z', q, K.ntfnF hq, ho, hm⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3RecvBlockRA.lean b/proofs/Fl4ProofsA64/P3/Stage3RecvBlockRA.lean new file mode 100644 index 000000000..332bf7387 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3RecvBlockRA.lean @@ -0,0 +1,289 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2c): **blocking `Recv` with a reply object, at the record**: `handle_recv` with +`blocking = true`, `can_reply = true` on an endpoint that is idle or holds waiting receivers, the REPLY register +naming a reply object linked to no thread. The receiver is inserted by priority into the endpoint's queue and the +reply is linked to it (`blkRecvRA`). P1's facts come from `receive_ipc.rpw`. + +* **`RecvPreA.recvBlockRA`**: `HRecvPostA`. + +[S] the endpoint's state (idle or `RecvEP`); `can_reply = true`; the REPLY register names a reply object `rr` linked to +no thread (`hcapR`, `hfree`); no bound notification, or one that is not active and whose SC is not the receiver's +(`BndOKA`); `numNodes = 1`. +[P] `ArchTotal`, `ArchPlat`, `hCs`, `hmb`, `hmax`, `htu`. New module. +-/ +import Fl4ProofsA64.P3.Stage3RecvBlockKeepRA +import Fl4ProofsA64.P3.Stage3RecvFrontA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4Spec Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {J5 : JSlotA S} {R : Set U64 → JSlotA S} {s s' : S} {a : A} {σ σ' : State Unit} + {m : McState} + +set_option maxHeartbeats 4000000 in +/-- **Blocking `Recv` with a reply object** (module docstring). -/ +theorem RecvPreA.recvBlockRA_core + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p badge slot rr rslot : U64} {send grant grantReply rg : Bool} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) + (hsc : capA st s slot = .Endpoint p badge send true grant grantReply) + (hrslot : rslot ∈ OS) (hrsc : capA st s rslot = .Reply rr rg) (hrr : rr ∈ Rp) + (hfree : L.mem s (Endp.rTcbA rr) = 0#u64) + (hp : p ∈ E) {x : Endpoint} (hk : σ.kheap (specPtr p) = some (.endpoint x)) (hcb : CanBlockA true x) + (hbn : ∀ y, σ.kheap σ.curThread = some (.tcb y) → BndOKA N (L.mem s) σ (L.curW s) y true) : + RecvCoreA st AR ar hPl.ipa n Z.Cs s a m (.Endpoint p badge send true grant grantReply) true (.Reply rr rg) + (absCapA (.Endpoint p badge send true grant grantReply)) (.replyCap (specPtr rr) (replyRightsA rg)) σ + (fun σ' => ∃ y xr, σ.kheap σ.curThread = some (.tcb y) ∧ σ.kheap (specPtr rr) = some (.reply xr) ∧ + σ' = blkRecvRA σ σ.curThread (specPtr p) (specPtr rr) x xr y) := by + have htZ : L.curW s ∈ Z.T := J.sr.curT + set t := L.curW s with ht_def + have ct : σ.curThread = specPtr t := J.j.1.sch.curThread + obtain ⟨xt, hkt, r⟩ := J.ep.tcb t htZ + have hkc : σ.kheap σ.curThread = some (.tcb xt) := by rw [ct]; exact hkt + have hbnd := hbn xt hkc + have hthT : Z.th t = some .tcb := (J.sr.thT t).2 htZ + have hnvm : L.mem s (stA t) ≠ sched.TS_RUNNING_VM := J.nvm + have hrunS : xt.tcbState = .running ∨ xt.tcbState = .restart := J.state hkt + have hr : runnable xt.tcbState = true := by rcases hrunS with e | e <;> rw [e] <;> rfl + have hidle : specPtr t ≠ σ.idleThread := by + obtain ⟨y, hy, hs⟩ := J.sinv.idleTcb + intro h + rw [← h, hkt] at hy + cases hy + rcases hrunS with e | e <;> rw [e] at hs <;> cases hs + have hRun : SndRunA σ (specPtr t) xt := J.sndRun hkt + have hsca : ∀ sc, xt.tcbSchedContext = some sc → ScActiveAtA σ sc := fun sc hs => J.scAct xt sc hkc hs + have hcm : m.curCore < fl4A64Platform.numNodes := by rw [J.j.1.sch.core]; exact hCs _ J.j.1.sch.cpuCs + have hO : SchedOpsEA st Z.T E Rp Bf (IpcSchedRelA st Z m κ) := + (ipcSchedOpsA (ar := ar) J.iplaced J.ipcPlace hcm hmb hmax htu).2.2.1 + have hF : SchedFootEA st Z.T E Rp Bf (IpcSchedRelA st Z m κ) (fun σ s => MWrA (st := st) Z m σ s) := + schedFootEA_of_sched + have hSR : IpcSchedRelA st Z m κ s σ := ⟨Z.Q, Z.R, J.sr⟩ + have hx := J.ep.at_ep hp hk + have hsort := prioSorted_of_specA J.iplaced.e.tcb J.ep hx (J.ipcSorted.1 (specPtr p) ⟨p, hp, rfl⟩ x hk) + obtain ⟨xs, hQ, hxs, hbq⟩ := hx.queue + have hxsT : ∀ y ∈ xs, y ∈ Z.T := fun y hy => hQ.mem y hy + have hxsB : ∀ y ∈ xs, L.mem s (Notif.tBlockA y) = p := fun y hy => (hbq y hy).1 + have hq : ∀ a ∈ x.epQueue, ∃ y, σ.kheap a = some (.tcb y) := by + intro a ha + rw [hxs] at ha + obtain ⟨u, hu, rfl⟩ := List.mem_map.1 ha + obtain ⟨y, hy, -⟩ := J.ep.tcb u (hxsT u hu) + exact ⟨y, hy⟩ + have hmq : ∀ y ∈ x.epQueue, ∃ z, σ.kheap y = some (.tcb z) ∧ z.tcbPriority = prioOf σ y := fun y hy => by + obtain ⟨z, hz⟩ := hq y hy + refine ⟨z, hz, ?_⟩ + simp [prioOf, Fl4Spec.optMapSome, Fl4Spec.optMap, tcbsOf, tcbsOfKh, tcbOf, hz] + have htq : specPtr t ∉ x.epQueue := not_queued_codeA J.ep htZ hkt (runnable_codeA hr) p hp x hk + have hte : specPtr t ≠ specPtr p := fun h => by rw [h, hk] at hkt; cases hkt + have hr0 : rr ≠ 0#u64 := fun eq => (J.iplaced.e.rep rr hrr).2.1 (by rw [eq]; rfl) + obtain ⟨xr, hkr, hxr⟩ := J.ep.reply rr hrr + have hrt : specPtr rr ≠ specPtr t := kheap_ne hkr hkt (fun _ h1 h2 => by subst h1; cases h2) + have hre : specPtr rr ≠ specPtr p := kheap_ne hkr hk (fun _ h1 h2 => by subst h1; cases h2) + -- FL4 and spec: `wait_for_sender` + have body := wait_for_sender_blockA (st := st) hO hF J.iplaced.e s σ J.ep hSR htZ hkt hRun hp hk hcb hsort rr + (rep := some (specPtr rr)) (optRef_ne hr0) (fun _ => hrr) + have narrow := wait_for_sender_block_narrowRA (st := st) hO hF J.iplaced.e s σ J.ep hSR htZ hkt hRun hp hk hQ hrr + obtain ⟨s2, ew, ⟨σ2, hspec, E2, ⟨Q, Rl, sr2⟩, -⟩, w⟩ := exists_of_specA (WP.spec_and body narrow) + rw [blkRecvR_specA hk hkt hkr htq hcb hmq] at hspec + have eσ : σ2 = blkRecvRA σ (specPtr t) (specPtr p) (specPtr rr) x xr xt := by + have h : ((), σ2) ∈ ({((), blkRecvRA σ (specPtr t) (specPtr p) (specPtr rr) x xr xt)} : Set (Unit × State Unit)) := by + rw [(Prod.mk.inj hspec).1]; exact Set.mem_singleton _ + exact (Prod.mk.inj (Set.mem_singleton_iff.1 h)).2 + subst eσ + generalize hσ' : blkRecvRA σ (specPtr t) (specPtr p) (specPtr rr) x xr xt = σ' at E2 sr2 + have B : RcvStepRA σ σ' (specPtr t) (specPtr p) (specPtr rr) x xr xt := hσ' ▸ blkRecvRA_step hte hrt hre + -- FL4: `receive_ipc`, `handle_recv` + have hdisp : rcvDispA st ar s a t p true rr ⦃ q => q = (s2, a) ⦄ := by + have hwait : (do + let s3 ← endpoint.wait_for_sender st s t p true rr + ok (s3, a)) ⦃ q => q = (s2, a) ⦄ := by + rw [ew, bind_ok] + exact (WP.spec_ok _).2 rfl + unfold rcvDispA + step with obj.Ep.state.spec as ⟨st0, hst0⟩ + rw [epState_eq] at hst0 + cases x with + | idleEP => + rw [hx.1] at hst0 + subst hst0 + rw [if_pos ep_state_vals.1.symm] + exact hwait + | recvEP q => + rw [hx.1] at hst0 + subst hst0 + rw [if_neg (by rw [ep_state_vals.1]; decide), if_pos ep_state_vals.2.2.symm] + exact hwait + | sendEP q => simp [CanBlockA] at hcb + have hipc : endpoint.receive_ipc st ar s a t (.Endpoint p badge send true grant grantReply) true (.Reply rr rg) = + ok (s2, a) := by + have h : endpoint.receive_ipc st ar s a t (.Endpoint p badge send true grant grantReply) true (.Reply rr rg) ⦃ q => + q = (s2, a) ⦄ := by + rw [receive_ipc_eqA] + refine WP.spec_bind (take_reply_plainA (R := Rp) (rc' := .replyCap (specPtr rr) (replyRightsA rg)) + (Or.inr ⟨rg, _, rfl, rfl, hrr⟩ : ReplyCapA Rp (.Reply rr rg) (.replyCap (specPtr rr) (replyRightsA rg)) rr) + (fun _ _ => Or.inl hfree)) ?_ + rintro q rfl + exact rcv_body_dispFlA J.ntfn J.ep htZ hkt hbnd hdisp + obtain ⟨q, hq', e⟩ := spec_existsA h + rw [hq', e] + have hxr0 : xr.replyTcb = none := by rw [hxr, hfree, optRef_zero] + have hrecv : ((), σ') ∈ (receiveIpc (Z := Unit) (specPtr t) + (absCapA (.Endpoint p badge send true grant grantReply)) true (.replyCap (specPtr rr) (replyRightsA rg)) σ).1 := by + rw [show absCapA (.Endpoint p badge send true grant grantReply) = + .endpointCap (specPtr p) badge.bv (rightsOfA send true grant grantReply) from rfl, + receiveIpc_bodyA _ _ true (Or.inr ⟨specPtr rr, _, xr, rfl, rfl, hkr, Or.inl hxr0⟩ : + SpecReplyPlainA σ (specPtr t) (.replyCap (specPtr rr) (replyRightsA rg)) (some (specPtr rr))), + bndOK_specA hkt hk J.ntfn hbnd (some (specPtr rr)), specDisp_waitA hcb, + blkRecvR_specA hk hkt hkr htq hcb hmq, hσ'] + exact Set.mem_singleton _ + -- P1's walk + obtain ⟨Pt, Ub, Pp7, Tb7, Hr, Te, Kt, Lv7, Re, bits, KLv, W⟩ := J7A.unp J.j7 + have I := J.delIn hA hPl W + have h : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s := by + obtain ⟨q, b, x, sg, sc⟩ := J.sys.ent.bxi + exact ⟨q, b, x, sc, sg⟩ + have hext : ∀ z ∈ (Aeneas.Std.Array.repeat 3#usize 0#u64).val, z ≠ 0#u64 → z ∈ OS := fun z hz hne => by + exact absurd (by simpa [Aeneas.Std.Array.repeat] using hz) hne + have D := J.decPreA (extra := Aeneas.Std.Array.repeat 3#usize 0#u64) hslot hext + have Pz := J.toJStage3A.sysPlace hext hslot hcov + have hH : H p = some .ep := J.utIpc.ep p hp + have regs : RpPost st H Z.Cs OS Z.V ia ie Ac s s2 := by + have walk := Fl4ProofsA64.P1.W7.R.ST.receive_ipc.rpw (st := st) (ar := ar) hA a t + (.Endpoint p badge send true grant grantReply) true (.Reply rr rg) p I J.core0 J.ko Pz (SW.start J.sys.sysl J.symL h) D.cb + (by rcases J.runW with e | e | e <;> rw [e] <;> + simp [sched.TS_RUNNING, sched.TS_RESTART, sched.TS_RUNNING_VM, sched.TS_INACTIVE]) + rfl hH + (fun k hk' => by + rw [hH] at hk' + cases hk' + exact ⟨slot, hslot, by rw [hsc]; exact names_ep⟩) + (fun _ _ c => by cases c; exact ⟨J.utIpc.reply rr hrr, rslot, hrslot, rg, hrsc⟩) + (J.tcbs ▸ J.runVcpu) hnvm + exact (post_of_okA (P := fun q => RpW st H Z.Cs OS Z.V ia ie Ac s q.1) walk hipc).post + obtain ⟨bxi, sysl, symL, userOK, j7⟩ := J.toJStage3A.finRpA hA hPl rfl regs σ' + -- the record + have wI : WritesIn st s s2 (IpcFpA st Z E Rp Bf m σ s) := w.mono fun b h => by + have hta := J.iplaced.e.tcb t htZ + have tw : ∀ (k : Nat), b.val = t.val + k → TWOffN k → IpcFpA st Z E Rp Bf m σ s b := fun k hv hk' => + Or.inl (Or.inr (Or.inr (Or.inl ⟨t, htZ, Or.inl (twf_ofA k hv hk')⟩))) + rcases h with (((c | c | c) | c | c | ⟨v, hv, c⟩) | c) + · exact tw 3328 c (by unfold TWOffN; omega) + · exact tw 3336 c (by unfold TWOffN; omega) + · exact tw 3360 c (by unfold TWOffN; omega) + · exact Or.inr c + · exact Or.inl (Or.inl ⟨p, hp, c⟩) + · have hvT : v ∈ Z.T := by + rcases List.mem_cons.1 hv with rfl | hv + exacts [htZ, hxsT v hv] + exact Or.inl (Or.inr (Or.inr (Or.inl ⟨v, hvT, Or.inr c⟩))) + · exact Or.inl (Or.inr (Or.inl ⟨rr, hrr, by rw [c, Endp.rTcbA_val]⟩)) + have wN : WritesIn st s s2 (fun b => SchedStepNWA (st := st) N E Rp Z m σ s b ∨ BufW Bf b) := + wI.mono fun _ h => h.elim (ewrite_stepNA_buf J.sr.core.tcbs) fun h => Or.inl (Or.inl (Or.inl h)) + have wI' : WritesIn st s s2 (fun b => IpcFpA st Z E Rp Bf m σ s b ∨ RepWA Rp b) := wI.mono fun _ => Or.inl + set M : ObjRef → Prop := fun o => o = specPtr t with hMdef + have K : SIKA σ.idleThread M (fun _ => False) false σ σ' := B.sika hkt hk hkr rfl + have notM : ∀ c < n, ¬ M (m.idleThreadOf c) := by + intro c hc' h + obtain ⟨y, hy, hs⟩ := J.sidle.f3 c hc' + rw [h, hkt] at hy + cases hy + rcases hrunS with e | e <;> rw [e] at hs <;> cases hs + have sinv : SInvXA σ' m n := by + refine K.sinvSw J.sinv J.sidle J.body.sw J.noSelf (fun u hu h => J.swCur u hu h) notM (fun h => hRun.2.2.2 h.symm) ?_ (fun _ h => h.elim) (fun _ h => h.elim) + (fun y hy => by rw [show y = specPtr t from hy]; exact hRun.2.1.2) (J.sidle.rdyA J.sinv) + (K.mfA J.sinv fun q hq x hx => by + rw [show q = specPtr t from hq, hkt] at hx; cases hx + exact fun hf => by have h0 := FaultStA.notRun hf; rw [hRun.1] at h0; cases h0) + intro y hy dm pr + rw [show y = specPtr t from hy] + exact hRun.2.1.1 dm pr + have wk := J.toJStage3A.ntfnWait_rcvRA (s' := s2) htZ hp hrr hxsT hxsB w + have ntfn := J.toJStage3A.ntfn_ipcMA wI' K E2 wk (fun u hu hm y hy q h => by + rw [show specPtr u = specPtr t from hm, hkt] at hy + cases hy + rcases hrunS with e | e <;> rw [e] at h <;> cases h) (fun u hu hm y hy q h => by + rw [show specPtr u = specPtr t from hm, B.atT] at hy + cases hy + cases h) + have hMv : ∀ u ∈ Z.T, M (specPtr u) → L.mem s (Fl4ProofsA64.P1.stA u) ≠ sched.TS_RUNNING_VM := fun u _ hu => by + rw [specPtr_inj (show specPtr u = specPtr t from hu)]; exact hnvm + have ssz : SSzA σ σ' := fun q y k h => by + rcases B.cases h with ⟨-, c⟩ | ⟨-, c⟩ | ⟨-, c⟩ | ⟨-, -, -, c⟩ + · cases c + · cases c + · cases c + · exact ⟨y, c⟩ + have hrt' : ∀ r' ∈ Rp, DeadA st OS .reply s r' → ∀ y, σ'.kheap (specPtr r') = some (.reply y) → + y.replyTcb = none := by + intro r' hr' hd y hy + by_cases hrr' : r' = rr + · subst hrr' + exact absurd (⟨rslot, hrslot, rg, hrsc⟩ : ∃ x ∈ OS, ∃ g, capA st s x = .Reply r' g) (fun ⟨x, hx, g, hg⟩ => hd x hx ⟨g, hg⟩) + · have hne : specPtr r' ≠ specPtr rr := fun h => hrr' (specPtr_inj h) + have hnt : specPtr r' ≠ specPtr t := fun h => by rw [h, B.atT] at hy; cases hy + have hnp : specPtr r' ≠ specPtr p := fun h => by rw [h, B.atE] at hy; cases hy + rw [B.far _ hnt hne hnp] at hy + exact J.deadExt.replyTcb r' hr' hd y hy + have J2 := J.toJStage3A.of_ipcStepURSA (Q := Q) (Rl := Rl) (curOf := Z.curOf) (curScOf := Z.curScOf) wN + (B.specSched hkt hk hkr) ssz (B.qblkR J.ipcSorted hkt hk hkr hq) hrt' (fun _ _ => by rw [B.machine]) + (fun _ _ => rfl) bxi sysl + symL userOK j7 sr2 E2 ntfn (J.toJStage3A.reply_ipcA wI K) J.deadExt.reply K.ipcKeep (J.toJStage3A.tw_ipcA wI') sinv + (K.sidle J.sidle notM (J.sidle.rdyA J.sinv)) (J.toJStage3A.runVcpu_ipcA wI' K E2 hMv) (J.toJStage3A.blkQ_recvRA htZ hp B K E2 wk) + (B.schedValid J.schedValid hkt hk hkr hsca) + refine ⟨s2, σ', hipc, hrecv, ⟨xt, xr, hkc, hkr, by rw [ct, hσ']⟩, A64RelS3.ofJ J2 ?bo, + curW_keptA J.toJStage3A J2 B.curThread, ?bo⟩ + refine J.body.stepEq (σ' := σ') (K.swA J.body.sw (fun u hu h => J.swCur u hu h) (fun c => by cases c) (fun _ h _ => h.elim)) (by rw [B.frame]) (by rw [B.machine]) B.curThread (by rw [B.frame]) + (by rw [B.frame]) B.releaseQueue (fun y' k' hz => ?_) (fun h => ?_) + · rcases B.cases hz with ⟨-, c⟩ | ⟨-, c⟩ | ⟨-, c⟩ | ⟨-, -, -, c⟩ + · cases c + · cases c + · cases c + · exact ⟨y', c, rfl, rfl, rfl⟩ + · exact absurd ct.symm (B.cur h) + +set_option maxHeartbeats 4000000 in +/-- **Blocking `Recv` with a reply object** (module docstring). -/ +theorem RecvPreA.recvBlockRA + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p badge slot rr rslot : U64} {send grant grantReply rg : Bool} + (hcap : cspace.lookup_cap st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.CAP)) = + ok (.Ok (.Endpoint p badge send true grant grantReply))) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) + (hsc : capA st s slot = .Endpoint p badge send true grant grantReply) + (hcapR : cspace.lookup_cap st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.REPLY)) = ok (.Ok (.Reply rr rg))) + (hrslot : rslot ∈ OS) (hrsc : capA st s rslot = .Reply rr rg) (hrr : rr ∈ Rp) + (hfree : L.mem s (Endp.rTcbA rr) = 0#u64) + (hp : p ∈ E) {x : Endpoint} (hk : σ.kheap (specPtr p) = some (.endpoint x)) (hcb : CanBlockA true x) + (hbn : ∀ y, σ.kheap σ.curThread = some (.tcb y) → BndOKA N (L.mem s) σ (L.curW s) y true) : + HRecvPostA st AR ar hPl.ipa n Z.Cs s a m true true σ + (fun σ' => ∃ y xr, σ.kheap σ.curThread = some (.tcb y) ∧ σ.kheap (specPtr rr) = some (.reply xr) ∧ + σ' = blkRecvRA σ σ.curThread (specPtr p) (specPtr rr) x xr y) + := RecvPreA.recvFrontA J hcap hcapR + (RecvPreA.recvBlockRA_core hA hPl J hCs h1 hmb hmax htu hslot hcov hsc hrslot hrsc hrr hfree hp hk hcb hbn) + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3RecvBlockRUnbA.lean b/proofs/Fl4ProofsA64/P3/Stage3RecvBlockRUnbA.lean new file mode 100644 index 000000000..2e28c69e7 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3RecvBlockRUnbA.lean @@ -0,0 +1,460 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2c): **blocking `Recv` with a reply object, when the receiver's bound notification +holds the receiver's SC**: the reply-object twin of `Stage3RecvBlockUnbA`. `maybe_return_sc` unbinds the receiver +(`unbWaitA`), then the receiver blocks as in `recvBlockRA` (`blkRecvRA` at the unbound state). And the two reply arms +behind one selector. + +* `RecvPreA.recvBlockRUnbA` / `RecvPreA.recvBlockRAnyA` (`BndOKA ... false`; the post is the disjunction of + `recvBlockRA`'s and `recvBlockRUnbA`'s outcomes): from `RecvPreA`, a switch possibly pending (post at `preschedA`). + The syscall-entry form is `JSyscallA.recvBlockRAnyA_repA` (`Stage3RecvPushCancelA`). + +[S] the endpoint is idle or holds waiting receivers; `can_reply = true`; the REPLY register names a reply object linked +to no thread; `numNodes = 1`; (`recvBlockRUnbA`) the bound notification is inactive and its SC is the caller's +(`BndUnbA`). +[P] `ArchTotal`, `ArchPlat`, `hCs`, `hmb`, `hmax`, `htu`. New module. +-/ +import Fl4ProofsA64.P3.Stage3RecvFrontA +import Fl4ProofsA64.P3.Stage3PreSchedA +import Fl4ProofsA64.P3.Stage3RecvBlockRA +import Fl4ProofsA64.P3.Stage3RecvBlockAnyA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4Spec Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {J5 : JSlotA S} {R : Set U64 → JSlotA S} {s s' : S} {a : A} {σ σ' : State Unit} + {m : McState} + +set_option maxHeartbeats 4000000 in +/-- **Blocking `Recv` with a reply object whose bound notification returns the receiver's SC** (module +docstring). -/ +theorem RecvPreA.recvBlockRUnbA_core + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p badge slot rr rslot : U64} {send grant grantReply rg : Bool} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) + (hsc : capA st s slot = .Endpoint p badge send true grant grantReply) + (hrslot : rslot ∈ OS) (hrsc : capA st s rslot = .Reply rr rg) (hrr : rr ∈ Rp) + (hfree : L.mem s (Endp.rTcbA rr) = 0#u64) + (hp : p ∈ E) {x : Endpoint} (hk : σ.kheap (specPtr p) = some (.endpoint x)) (hcb : CanBlockA true x) + (hbn : ∀ y, σ.kheap σ.curThread = some (.tcb y) → BndUnbA N σ y) : + RecvCoreA st AR ar hPl.ipa n Z.Cs s a m (.Endpoint p badge send true grant grantReply) true (.Reply rr rg) + (absCapA (.Endpoint p badge send true grant grantReply)) (.replyCap (specPtr rr) (replyRightsA rg)) σ + (fun σ' => ∃ y xr sc xs ns, σ.kheap σ.curThread = some (.tcb y) ∧ σ.kheap (specPtr rr) = some (.reply xr) ∧ + y.tcbSchedContext = some sc ∧ σ.kheap sc = some (.schedContext xs ns) ∧ + σ' = blkRecvRA (unbWaitA (preschedA σ) σ.curThread sc xs ns y) σ.curThread (specPtr p) (specPtr rr) x xr + { y with tcbSchedContext := none }) := by + have htZ : L.curW s ∈ Z.T := J.sr.curT + set t := L.curW s with ht_def + have ct : σ.curThread = specPtr t := J.j.1.sch.curThread + obtain ⟨xt, hkt, r⟩ := J.ep.tcb t htZ + have hkc : σ.kheap σ.curThread = some (.tcb xt) := by rw [ct]; exact hkt + obtain ⟨nn, hnn, xn, sc, hbd, hkn, hna, hsn, hst0⟩ := hbn xt hkc + have hthT : Z.th t = some .tcb := (J.sr.thT t).2 htZ + have hnvm : L.mem s (stA t) ≠ sched.TS_RUNNING_VM := J.nvm + have hrunS : xt.tcbState = .running ∨ xt.tcbState = .restart := J.state hkt + have hr : runnable xt.tcbState = true := by rcases hrunS with e | e <;> rw [e] <;> rfl + have hidle : specPtr t ≠ σ.idleThread := by + obtain ⟨y, hy, hs⟩ := J.sinv.idleTcb + intro h + rw [← h, hkt] at hy + cases hy + rcases hrunS with e | e <;> rw [e] at hs <;> cases hs + have hRun : SndRunA σ (specPtr t) xt := J.sndRun hkt + have hcm : m.curCore < fl4A64Platform.numNodes := by rw [J.j.1.sch.core]; exact hCs _ J.j.1.sch.cpuCs + have hO : SchedOpsEA st Z.T E Rp Bf (IpcSchedRelA st Z m κ) := + (ipcSchedOpsA (ar := ar) J.iplaced J.ipcPlace hcm hmb hmax htu).2.2.1 + have hF : SchedFootEA st Z.T E Rp Bf (IpcSchedRelA st Z m κ) (fun σ s => MWrA (st := st) Z m σ s) := + schedFootEA_of_sched + have hte : specPtr t ≠ specPtr p := fun h => by rw [h, hk] at hkt; cases hkt + -- the unbinding + obtain ⟨xs0, ns0, hks0⟩ : ∃ xs0 ns0, σ.kheap sc = some (.schedContext xs0 ns0) := by + obtain ⟨z, j, hz, -⟩ := J.sr.inv.back (specPtr t) xt sc hkt hst0 + exact ⟨z, j, hz⟩ + have hNT : sc ≠ specPtr t := fun e => by rw [e, hkt] at hks0; cases hks0 + have hsi : sc ≠ idleScPtr := fun e => J.sr.inv.ntfnIdle (specPtr nn) xn hkn (by rw [hsn, e]) + have hr' : ActReadyA σ := J.sr.actReady + have P := preschedA_keepA σ + have hktp : (preschedA σ).kheap (specPtr t) = some (.tcb xt) := by rw [P.kh]; exact hkt + have hksp : (preschedA σ).kheap sc = some (.schedContext xs0 ns0) := by rw [P.kh]; exact hks0 + set UW : State Unit := unbWaitA (preschedA σ) (specPtr t) sc xs0 ns0 xt with hUW + have eU : maybeReturnSc (Z := Unit) (specPtr nn) (specPtr t) σ = ({((), UW)}, False) := + maybeReturnSc_unbSwA hkn hkt hsn hst0 hks0 hNT ct.symm hr' (J.pendingSc hkt hst0) + have hkt1 : UW.kheap (specPtr t) = some (.tcb { xt with tcbSchedContext := none }) := unbWait_atTA hNT + have hks1 : UW.kheap sc = some (.schedContext { xs0 with scTcb := none } ns0) := unbWait_atSA hNT + have hpS : specPtr p ≠ sc := fun e => by rw [e, hks0] at hk; cases hk + have hk1 : UW.kheap (specPtr p) = some (.endpoint x) := by + rw [show UW.kheap (specPtr p) = σ.kheap (specPtr p) from (unbWait_keepA hNT (Ne.symm hte) hpS).trans (congrFun P.kh _)]; exact hk + have hRun1 : SndRunA UW (specPtr t) { xt with tcbSchedContext := none } := by + refine ⟨hRun.1, ⟨fun d pr => P.notin _ hRun.2.1.1 (fun h => J.noSelf (by rw [← ct] at h; exact h)) d pr, + fun h => hRun.2.1.2 (P.rls ▸ h)⟩, fun u hu => ?_, fun h => hRun.2.2.2 (h.trans P.idle)⟩ + have e : UW.schedulerAction = .chooseNewThread := rfl + rw [e] at hu; cases hu + have Qu := (P.qk.trans (unbWait_qkeepA hNT hktp hksp)).toSend + have sorted1 := J.ipcSorted.of_qs Qu + have hr0 : rr ≠ 0#u64 := fun eq => (J.iplaced.e.rep rr hrr).2.1 (by rw [eq]; rfl) + obtain ⟨xr, hkr, hxr⟩ := J.ep.reply rr hrr + have hrt : specPtr rr ≠ specPtr t := kheap_ne hkr hkt (fun _ h1 h2 => by subst h1; cases h2) + have hre : specPtr rr ≠ specPtr p := kheap_ne hkr hk (fun _ h1 h2 => by subst h1; cases h2) + have hrS : specPtr rr ≠ sc := kheap_ne hkr hks0 (fun _ h1 h2 => by subst h1; cases h2) + have hkr1 : UW.kheap (specPtr rr) = some (.reply xr) := by + rw [show UW.kheap (specPtr rr) = σ.kheap (specPtr rr) from (unbWait_keepA hNT hrt hrS).trans (congrFun P.kh _)]; exact hkr + -- the bound notification word of the caller + have hz : nn ≠ 0#u64 ∧ L.mem s (tBoundA t) = nn := bound_wordA (J.ep.at_tcb htZ hkt) hbd + have hns : stOf (L.mem s) nn ≠ obj.NTFN_ACTIVE := + stOf_not_activeA (J.ntfn.at_ntfn hnn hkn).obj (fun b e => by simp [isActive, e] at hna) + -- FL4: `receive_ipc` after the reply cap: the SC return, then the endpoint dispatch + set σ' : State Unit := blkRecvRA UW (specPtr t) (specPtr p) (specPtr rr) x xr { xt with tcbSchedContext := none } with hσ' + have body : rcvBodyA st ar s a t p true rr ⦃ r => r.2 = a ∧ ∃ (s1 : S) (xs : List U64), + MFrA (st := st) Z m s s1 σ UW ∧ (∀ y ∈ xs, y ∈ Z.T) ∧ x.epQueue = xs.map specPtr ∧ + (∀ y ∈ xs, L.mem s (Notif.tBlockA y) = p) ∧ (∀ a ∈ x.epQueue, ∃ y, UW.kheap a = some (.tcb y)) ∧ + receiveIpcBlocked (Z := Unit) true (specPtr t) (specPtr p) (some (specPtr rr)) UW = ({((), σ')}, False) ∧ + EpStateRel Z.T E Rp Bf (L.mem r.1) σ' ∧ IpcSchedRelA st Z m κ r.1 σ' ∧ + WritesIn st s r.1 (fun b => BlkRecvFpA (fun σ s => MWrA (st := st) Z m σ s) σ s t p xs b ∨ b = Endp.rTcbA rr) ⦄ := by + rw [rcvReturningA (st := st) (ar := ar) s a t p nn rr hz.1 hz.2 hns] + refine WP.spec_bind (maybeReturnIpcA J.sr J.ipcPlace J.iplaced.e J.iplaced.n hcm J.ntfn J.ep hnn htZ + hRun.2.1.2) ?_ + rintro s1 ⟨σU, Q, h, H1, HN1, HE1, F⟩ + have eσU : σU = UW := single_outA (h.symm.trans eU) + subst eσU + have hSR1 : IpcSchedRelA st Z m κ s1 UW := ⟨Q, Z.R, H1⟩ + have hx1 := HE1.at_ep hp hk1 + obtain ⟨xs, hQ, hxs, hbq⟩ := hx1.queue + have hxsT : ∀ y ∈ xs, y ∈ Z.T := fun y hy => hQ.mem y hy + have hq1 : ∀ a ∈ x.epQueue, ∃ y, UW.kheap a = some (.tcb y) := by + intro a ha + rw [hxs] at ha + obtain ⟨u, hu, rfl⟩ := List.mem_map.1 ha + obtain ⟨y, hy, -⟩ := HE1.tcb u (hxsT u hu) + exact ⟨y, hy⟩ + have hmq1 : ∀ y ∈ x.epQueue, ∃ z, UW.kheap y = some (.tcb z) ∧ z.tcbPriority = prioOf UW y := fun y hy => by + obtain ⟨z, hz⟩ := hq1 y hy + refine ⟨z, hz, ?_⟩ + simp [prioOf, Fl4Spec.optMapSome, Fl4Spec.optMap, tcbsOf, tcbsOfKh, tcbOf, hz] + have htq1 : specPtr t ∉ x.epQueue := + not_queued_codeA HE1 htZ hkt1 (runnable_codeA hRun1.1) p hp x hk1 + have hsort1 := prioSorted_of_specA J.iplaced.e.tcb HE1 hx1 + (sorted1.1 (specPtr p) ⟨p, hp, rfl⟩ x hk1) + have bd := wait_for_sender_blockA (st := st) hO hF J.iplaced.e s1 UW HE1 hSR1 htZ hkt1 hRun1 hp hk1 hcb hsort1 + rr (rep := some (specPtr rr)) (optRef_ne hr0) (fun _ => hrr) + have nw := wait_for_sender_block_narrowRA (st := st) hO hF J.iplaced.e s1 UW HE1 hSR1 htZ hkt1 hRun1 hp hk1 hQ hrr + obtain ⟨s2, ew, ⟨σ2, hspec, E2, SR2, -⟩, w⟩ := exists_of_specA (WP.spec_and bd nw) + have hspecB := blkRecvR_specA hk1 hkt1 hkr1 htq1 hcb hmq1 + rw [hspecB] at hspec + have eσ : σ2 = σ' := by + have h : ((), σ2) ∈ ({((), σ')} : Set (Unit × State Unit)) := by + rw [(Prod.mk.inj hspec).1]; exact Set.mem_singleton _ + exact (Prod.mk.inj (Set.mem_singleton_iff.1 h)).2 + subst eσ + have hB : ∀ y ∈ xs, L.mem s (Notif.tBlockA y) = p := fun y hy => by + rw [← (hbq y hy).1] + symm + have hya := J.iplaced.e.tcb y (hxsT y hy) + exact F.wr.mem_eq (Endp.a_blk hya).1 (J.tcbWord_offMwrA (hxsT y hy) (K := 3336#u64) (by simp) (by simp)) + have wC : WritesIn st s s2 (fun b => BlkRecvFpA (fun σ s => MWrA (st := st) Z m σ s) σ s t p xs b ∨ + b = Endp.rTcbA rr) := + (F.wr.trans w).mono fun b hb => by + rcases hb with h | (((h | h | h) | h | h | h) | h) + · exact Or.inl (Or.inr (Or.inl h)) + · exact Or.inl (Or.inl (Or.inl h)) + · exact Or.inl (Or.inl (Or.inr (Or.inl h))) + · exact Or.inl (Or.inl (Or.inr (Or.inr h))) + · exact Or.inl (Or.inr (Or.inl (mwr_back (Z := Z) (Z' := Z) rfl rfl F.sz F.wr.1 h))) + · exact Or.inl (Or.inr (Or.inr (Or.inl h))) + · exact Or.inl (Or.inr (Or.inr (Or.inr h))) + · exact Or.inr h + have hdisp : rcvDispA st ar s1 a t p true rr ⦃ q => q = (s2, a) ⦄ := by + have hwait : (do + let s3 ← endpoint.wait_for_sender st s1 t p true rr + ok (s3, a)) ⦃ q => q = (s2, a) ⦄ := by + rw [ew, bind_ok] + exact (WP.spec_ok _).2 rfl + unfold rcvDispA + step with obj.Ep.state.spec as ⟨st0, hst0'⟩ + rw [epState_eq] at hst0' + cases x with + | idleEP => + rw [hx1.1] at hst0' + subst hst0' + rw [if_pos ep_state_vals.1.symm] + exact hwait + | recvEP q => + rw [hx1.1] at hst0' + subst hst0' + rw [if_neg (by rw [ep_state_vals.1]; decide), if_pos ep_state_vals.2.2.symm] + exact hwait + | sendEP q => simp [CanBlockA] at hcb + refine WP.spec_mono hdisp fun q hq => ?_ + subst hq + exact ⟨rfl, s1, xs, F, hxsT, hxs, hB, hq1, hspecB, E2, SR2, wC⟩ + obtain ⟨⟨s2, a2⟩, ew, ea, s1, xs, F, hxsT, hxs, hB, hq1, hspecB, E2, SR2, wC⟩ := spec_existsA body + simp only at ea E2 SR2 wC + subst a2 + obtain ⟨Q2, Rl, sr2⟩ := SR2 + have hipc : endpoint.receive_ipc st ar s a t (.Endpoint p badge send true grant grantReply) true (.Reply rr rg) = + ok (s2, a) := by + have h : endpoint.receive_ipc st ar s a t (.Endpoint p badge send true grant grantReply) true (.Reply rr rg) ⦃ q => + q = (s2, a) ⦄ := by + rw [receive_ipc_eqA] + refine WP.spec_bind (take_reply_plainA (R := Rp) (rc' := .replyCap (specPtr rr) (replyRightsA rg)) + (Or.inr ⟨rg, _, rfl, rfl, hrr⟩ : ReplyCapA Rp (.Reply rr rg) (.replyCap (specPtr rr) (replyRightsA rg)) rr) + (fun _ _ => Or.inl hfree)) ?_ + rintro q rfl + show rcvBodyA st ar s a t p true rr ⦃ q => q = (s2, a) ⦄ + rw [ew] + exact (WP.spec_ok _).2 rfl + obtain ⟨q, hq', e⟩ := spec_existsA h + rw [hq', e] + have hxr0 : xr.replyTcb = none := by rw [hxr, hfree, optRef_zero] + have hrecv : ((), σ') ∈ (receiveIpc (Z := Unit) (specPtr t) + (absCapA (.Endpoint p badge send true grant grantReply)) true (.replyCap (specPtr rr) (replyRightsA rg)) σ).1 := by + rw [show absCapA (.Endpoint p badge send true grant grantReply) = + .endpointCap (specPtr p) badge.bv (rightsOfA send true grant grantReply) from rfl, + receiveIpc_bodyA _ _ true (Or.inr ⟨specPtr rr, _, xr, rfl, rfl, hkr, Or.inl hxr0⟩ : + SpecReplyPlainA σ (specPtr t) (.replyCap (specPtr rr) (replyRightsA rg)) (some (specPtr rr))), + specBodyReturningA' hkt hk hkn hbd hna eU (some (specPtr rr)), specDisp_waitA hcb, hspecB] + exact Set.mem_singleton _ + -- P1's walk + obtain ⟨Pt, Ub, Pp7, Tb7, Hr, Te, Kt, Lv7, Re, bits, KLv, W⟩ := J7A.unp J.j7 + have I := J.delIn hA hPl W + have h : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s := by + obtain ⟨q, b, x, sg, sc⟩ := J.sys.ent.bxi + exact ⟨q, b, x, sc, sg⟩ + have hext : ∀ z ∈ (Aeneas.Std.Array.repeat 3#usize 0#u64).val, z ≠ 0#u64 → z ∈ OS := fun z hz hne => by + exact absurd (by simpa [Aeneas.Std.Array.repeat] using hz) hne + have D := J.decPreA (extra := Aeneas.Std.Array.repeat 3#usize 0#u64) hslot hext + have Pz := J.toJStage3A.sysPlace hext hslot hcov + have hH : H p = some .ep := J.utIpc.ep p hp + have regs : RpPost st H Z.Cs OS Z.V ia ie Ac s s2 := by + have walk := Fl4ProofsA64.P1.W7.R.ST.receive_ipc.rpw (st := st) (ar := ar) hA a t + (.Endpoint p badge send true grant grantReply) true (.Reply rr rg) p I J.core0 J.ko Pz (SW.start J.sys.sysl J.symL h) D.cb + (by rcases J.runW with e | e | e <;> rw [e] <;> + simp [sched.TS_RUNNING, sched.TS_RESTART, sched.TS_RUNNING_VM, sched.TS_INACTIVE]) + rfl hH + (fun k hk' => by + rw [hH] at hk' + cases hk' + exact ⟨slot, hslot, by rw [hsc]; exact names_ep⟩) + (fun _ _ c => by cases c; exact ⟨J.utIpc.reply rr hrr, rslot, hrslot, rg, hrsc⟩) + (J.tcbs ▸ J.runVcpu) hnvm + exact (post_of_okA (P := fun q => RpW st H Z.Cs OS Z.V ia ie Ac s q.1) walk hipc).post + obtain ⟨bxi, sysl, symL, userOK, j7⟩ := J.toJStage3A.finRpA hA hPl rfl regs σ' + -- the record over the composite step + have B : RcvStepRA UW σ' (specPtr t) (specPtr p) (specPtr rr) x xr { xt with tcbSchedContext := none } := + blkRecvRA_step hte hrt hre + have wI : WritesIn st s s2 (IpcFpA st Z E Rp Bf m σ s) := wC.mono fun b h => by + have hta := J.iplaced.e.tcb t htZ + have tw : ∀ (k : Nat), b.val = t.val + k → TWOffN k → IpcFpA st Z E Rp Bf m σ s b := fun k hv hk' => + Or.inl (Or.inr (Or.inr (Or.inl ⟨t, htZ, Or.inl (twf_ofA k hv hk')⟩))) + rcases h with (((c | c | c) | c | c | ⟨v, hv, c⟩) | c) + · exact tw 3328 c (by unfold TWOffN; omega) + · exact tw 3336 c (by unfold TWOffN; omega) + · exact tw 3360 c (by unfold TWOffN; omega) + · exact Or.inr c + · exact Or.inl (Or.inl ⟨p, hp, c⟩) + · have hvT : v ∈ Z.T := by + rcases List.mem_cons.1 hv with rfl | hv + exacts [htZ, hxsT v hv] + exact Or.inl (Or.inr (Or.inr (Or.inl ⟨v, hvT, Or.inr c⟩))) + · exact Or.inl (Or.inr (Or.inl ⟨rr, hrr, by rw [c, Endp.rTcbA_val]⟩)) + have wN : WritesIn st s s2 (fun b => SchedStepNWA (st := st) N E Rp Z m σ s b ∨ BufW Bf b) := + wI.mono fun _ h => h.elim (ewrite_stepNA_buf J.sr.core.tcbs) fun h => Or.inl (Or.inl (Or.inl h)) + have wI' : WritesIn st s s2 (fun b => IpcFpA st Z E Rp Bf m σ s b ∨ RepWA Rp b) := wI.mono fun _ => Or.inl + set M : ObjRef → Prop := fun o => o = specPtr t with hMdef + have hti : specPtr t ≠ σ.idleThread := hidle + have K1 : SIKA σ.idleThread M (fun _ => False) true σ UW := (preschedA_sikaA hr').trans (unbWait_sikaA hNT hktp hksp hti hsi P.noSw) + have K2 : SIKA σ.idleThread M (fun _ => False) false UW σ' := B.sika hkt1 hk1 hkr1 rfl + have K : SIKA σ.idleThread M (fun _ => False) true σ σ' := K1.trans K2.upTo + have notM : ∀ c < n, ¬ M (m.idleThreadOf c) := by + intro c hc' h + obtain ⟨y, hy, hs⟩ := J.sidle.f3 c hc' + rw [h, hkt] at hy + cases hy + rcases hrunS with e | e <;> rw [e] at hs <;> cases hs + have sinv : SInvXA σ' m n := by + refine K.sinvSw J.sinv J.sidle J.body.sw J.noSelf (fun u hu h => J.swCur u hu h) notM + (fun h => hRun.2.2.2 h.symm) ?_ (fun _ h => h.elim) (fun _ h => h.elim) + (fun y hy => by rw [show y = specPtr t from hy]; exact hRun.2.1.2) (J.sidle.rdyA J.sinv) + (K.mfA J.sinv fun q hq x hx => by + rw [show q = specPtr t from hq, hkt] at hx; cases hx + exact fun hf => by have h0 := FaultStA.notRun hf; rw [hRun.1] at h0; cases h0) + intro y hy dm pr + rw [show y = specPtr t from hy] + exact hRun.2.1.1 dm pr + have wk := J.toJStage3A.ntfnWait_rcvRA (s' := s2) htZ hp hrr hxsT hB wC + have ntfn := J.toJStage3A.ntfn_ipcMA wI' K E2 wk (fun u hu hm y hy q h => by + rw [show specPtr u = specPtr t from hm, hkt] at hy + cases hy + rcases hrunS with e | e <;> rw [e] at h <;> cases h) (fun u hu hm y hy q h => by + rw [show specPtr u = specPtr t from hm, B.atT] at hy + cases hy + cases h) + have hMv : ∀ u ∈ Z.T, M (specPtr u) → L.mem s (Fl4ProofsA64.P1.stA u) ≠ sched.TS_RUNNING_VM := fun u _ hu => by + rw [specPtr_inj (show specPtr u = specPtr t from hu)]; exact hnvm + have ssz1 : SSzA σ UW := fun q y k h => by + by_cases e : q = sc + · rw [e] at h ⊢ + rw [hks1] at h; cases h + exact ⟨xs0, hks0⟩ + · have e2 : q ≠ specPtr t := fun e' => by rw [e', hkt1] at h; cases h + exact ⟨y, by rw [← show UW.kheap q = σ.kheap q from (unbWait_keepA hNT e2 e).trans (congrFun P.kh q)]; exact h⟩ + have ssz2 : SSzA UW σ' := fun q y k h => by + rcases B.cases h with ⟨-, c⟩ | ⟨-, c⟩ | ⟨-, c⟩ | ⟨-, -, -, c⟩ + · cases c + · cases c + · cases c + · exact ⟨y, c⟩ + have ssz : SSzA σ σ' := ssz1.trans ssz2 + have HS : SpecSchedA σ σ' := (P.spec.trans (unbWait_specSchedA hNT hktp hksp)).trans (B.specSched hkt1 hk1 hkr1) + have Qb := B.qblkR sorted1 hkt1 hk1 hkr1 hq1 + have Qk : QBlkRA (specPtr '' E) (specPtr '' N) σ σ' := + ⟨Qb.sorted, fun h => Qb.act (Qu.act h), Qu.dom.trans Qb.dom⟩ + have sv1 : SchedValidA UW := unbWait_schedValidA hNT hktp hksp (preschedA_schedValidA J.schedValid) + (fun d pr => P.notin _ hRun.2.1.1 (fun h => J.noSelf (by rw [← ct] at h; exact h)) d pr) + (fun h => hRun.2.1.2 (P.rls ▸ h)) + have sv : SchedValidA σ' := B.schedValid sv1 hkt1 hk1 hkr1 (fun sc' h => by cases h) + have keepS : ∀ q y k, σ.kheap q = some (.schedContext y k) → ∃ y', σ'.kheap q = some (.schedContext y' k) ∧ + y'.scReplies = y.scReplies := fun q y k h => by + by_cases e : q = sc + · rw [e] at h ⊢ + rw [hks0] at h; cases h + exact ⟨{ xs0 with scTcb := none }, by rw [B.far sc hNT (Ne.symm hrS) (Ne.symm hpS)]; exact hks1, rfl⟩ + · have e2 : q ≠ specPtr t := fun e' => by rw [e', hkt] at h; cases h + have e3 : q ≠ specPtr p := fun e' => by rw [e', hk] at h; cases h + have e4 : q ≠ specPtr rr := fun e' => by rw [e', hkr] at h; cases h + exact ⟨y, by rw [B.far q e2 e4 e3, show UW.kheap q = σ.kheap q from (unbWait_keepA hNT e2 e).trans (congrFun P.kh q)]; exact h, rfl⟩ + have reply := J.toJStage3A.reply_ipcGA wI + (fun c _ y k h => by + obtain ⟨y', h2, e⟩ := keepS _ y k h + exact ⟨y', k, h2, e⟩) + (fun r' _ y h => by + rcases B.cases h with ⟨-, c⟩ | ⟨e, c⟩ | ⟨-, c⟩ | ⟨-, -, -, h1⟩ + · cases c + · cases c + exact ⟨xr, by rw [e]; exact hkr, rfl⟩ + · cases c + · rcases unbWait_casesA hNT h1 with ⟨-, c⟩ | ⟨-, c⟩ | ⟨-, -, h0⟩ + · cases c + · cases c + · exact ⟨y, by rw [P.kh] at h0; exact h0, rfl⟩) + have hrt' : ∀ r' ∈ Rp, DeadA st OS .reply s r' → ∀ y, σ'.kheap (specPtr r') = some (.reply y) → + y.replyTcb = none := by + intro r' hr' hd y hy + by_cases hrr' : r' = rr + · subst hrr' + exact absurd (⟨rslot, hrslot, rg, hrsc⟩ : ∃ x ∈ OS, ∃ g, capA st s x = .Reply r' g) (fun ⟨x, hx, g, hg⟩ => hd x hx ⟨g, hg⟩) + · have hne : specPtr r' ≠ specPtr rr := fun h => hrr' (specPtr_inj h) + have hnt : specPtr r' ≠ specPtr t := fun h => by rw [h, B.atT] at hy; cases hy + have hnp : specPtr r' ≠ specPtr p := fun h => by rw [h, B.atE] at hy; cases hy + rw [B.far _ hnt hne hnp] at hy + rcases unbWait_casesA hNT hy with ⟨-, c⟩ | ⟨-, c⟩ | ⟨-, -, h0⟩ + · cases c + · cases c + · exact J.deadExt.replyTcb r' hr' hd y (by rw [P.kh] at h0; exact h0) + have J2 := J.toJStage3A.of_ipcStepURSA (Q := Q2) (Rl := Rl) (curOf := Z.curOf) (curScOf := Z.curScOf) wN + HS ssz Qk hrt' (fun _ _ => by + show σ'.machineState.underlyingMemory _ = σ.machineState.underlyingMemory _ + rw [B.machine]; exact congrFun (congrArg (fun z => z.underlyingMemory) P.mach) _) (fun _ _ => rfl) bxi sysl symL userOK j7 sr2 E2 ntfn reply J.deadExt.reply K.ipcKeep + (J.toJStage3A.tw_ipcA wI') sinv (K.sidle J.sidle notM (J.sidle.rdyA J.sinv)) (J.toJStage3A.runVcpu_ipcA wI' K E2 hMv) + (J.toJStage3A.blkQ_recvRA htZ hp B K E2 wk) sv + have hcur : σ'.curThread = σ.curThread := B.curThread.trans P.cur + refine ⟨s2, σ', hipc, hrecv, ⟨xt, xr, sc, xs0, ns0, hkc, hkr, hst0, hks0, by rw [ct]⟩, A64RelS3.ofJ J2 ?bo, + curW_keptA J.toJStage3A J2 hcur, ?bo⟩ + have act' : σ'.schedulerAction = .chooseNewThread := by rcases B.act with h | h <;> exact h + refine J.body.step (σ' := σ') (fun u e => by rw [act'] at e; cases e) + (by show σ'.curTime = _; rw [B.frame]; exact P.time) + (by show σ'.machineState.lastMachineTime = _; rw [B.frame]; exact congrArg _ P.mach) hcur + (by show σ'.curSc = _; rw [B.frame]; exact P.csc) (by show σ'.consumedTime = _; rw [B.frame]; exact P.con) + (fun _ _ _ _ _ h => by rw [B.releaseQueue] at h; exact P.rls ▸ h) + (fun y' k' hz => ?_) (fun h => absurd h (by rw [act']; intro c; cases c)) + rcases B.cases hz with ⟨-, c⟩ | ⟨-, c⟩ | ⟨-, c⟩ | ⟨-, -, -, h0⟩ + · cases c + · cases c + · cases c + · rcases unbWait_casesA hNT h0 with ⟨-, c⟩ | ⟨e, c⟩ | ⟨-, -, h00⟩ + · cases c + · cases c + exact ⟨xs0, by rw [e]; exact hks0, rfl, rfl, Or.inr rfl⟩ + · exact ⟨y', by rw [P.kh] at h00; exact h00, rfl, rfl, Or.inl rfl⟩ + + +set_option maxHeartbeats 4000000 in +theorem RecvPreA.recvBlockRUnbA + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p badge slot rr rslot : U64} {send grant grantReply rg : Bool} + (hcap : cspace.lookup_cap st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.CAP)) = + ok (.Ok (.Endpoint p badge send true grant grantReply))) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) + (hsc : capA st s slot = .Endpoint p badge send true grant grantReply) + (hcapR : cspace.lookup_cap st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.REPLY)) = ok (.Ok (.Reply rr rg))) + (hrslot : rslot ∈ OS) (hrsc : capA st s rslot = .Reply rr rg) (hrr : rr ∈ Rp) + (hfree : L.mem s (Endp.rTcbA rr) = 0#u64) + (hp : p ∈ E) {x : Endpoint} (hk : σ.kheap (specPtr p) = some (.endpoint x)) (hcb : CanBlockA true x) + (hbn : ∀ y, σ.kheap σ.curThread = some (.tcb y) → BndUnbA N σ y) : + HRecvPostA st AR ar hPl.ipa n Z.Cs s a m true true σ + (fun σ' => ∃ y xr sc xs ns, σ.kheap σ.curThread = some (.tcb y) ∧ σ.kheap (specPtr rr) = some (.reply xr) ∧ + y.tcbSchedContext = some sc ∧ σ.kheap sc = some (.schedContext xs ns) ∧ + σ' = blkRecvRA (unbWaitA (preschedA σ) σ.curThread sc xs ns y) σ.curThread (specPtr p) (specPtr rr) x xr + { y with tcbSchedContext := none }) := RecvPreA.recvFrontA J hcap hcapR + (RecvPreA.recvBlockRUnbA_core hA hPl J hCs h1 hmb hmax htu hslot hcov hsc hrslot hrsc hrr hfree hp hk hcb hbn) + +set_option maxHeartbeats 4000000 in +/-- **Blocking `Recv` with a reply object** with any bound notification (`BndOKA ... false`). -/ +theorem RecvPreA.recvBlockRAnyA + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p badge slot rr rslot : U64} {send grant grantReply rg : Bool} + (hcap : cspace.lookup_cap st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.CAP)) = + ok (.Ok (.Endpoint p badge send true grant grantReply))) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) + (hsc : capA st s slot = .Endpoint p badge send true grant grantReply) + (hcapR : cspace.lookup_cap st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.REPLY)) = ok (.Ok (.Reply rr rg))) + (hrslot : rslot ∈ OS) (hrsc : capA st s rslot = .Reply rr rg) (hrr : rr ∈ Rp) + (hfree : L.mem s (Endp.rTcbA rr) = 0#u64) + (hp : p ∈ E) {x : Endpoint} (hk : σ.kheap (specPtr p) = some (.endpoint x)) (hcb : CanBlockA true x) + (hbn : ∀ y, σ.kheap σ.curThread = some (.tcb y) → BndOKA N (L.mem s) σ (L.curW s) y false) : + HRecvPostA st AR ar hPl.ipa n Z.Cs s a m true true σ + (fun σ' => (∃ y xr, σ.kheap σ.curThread = some (.tcb y) ∧ σ.kheap (specPtr rr) = some (.reply xr) ∧ + σ' = blkRecvRA σ σ.curThread (specPtr p) (specPtr rr) x xr y) ∨ + (∃ y xr sc xs ns, σ.kheap σ.curThread = some (.tcb y) ∧ σ.kheap (specPtr rr) = some (.reply xr) ∧ + y.tcbSchedContext = some sc ∧ σ.kheap sc = some (.schedContext xs ns) ∧ + σ' = blkRecvRA (unbWaitA (preschedA σ) σ.curThread sc xs ns y) σ.curThread (specPtr p) (specPtr rr) x xr + { y with tcbSchedContext := none })) := by + have htZ : L.curW s ∈ Z.T := J.sr.curT + have ct : σ.curThread = specPtr (L.curW s) := J.j.1.sch.curThread + obtain ⟨xt, hkt, -⟩ := J.ep.tcb (L.curW s) htZ + have hkc : σ.kheap σ.curThread = some (.tcb xt) := by rw [ct]; exact hkt + have hcm : m.curCore < fl4A64Platform.numNodes := by rw [J.j.1.sch.core]; exact hCs _ J.j.1.sch.cpuCs + rcases J.toJStage3A.bndOK_blockA hcm htZ hkt (hbn xt hkc) with hb | hb + · exact (J.recvBlockRA hA hPl hCs h1 hmb hmax htu hcap hslot hcov hsc hcapR hrslot hrsc hrr hfree hp hk hcb + (fun y hy => by rw [hkc] at hy; cases hy; exact hb)).monoA (fun _ h => Or.inl h) + · exact (J.recvBlockRUnbA hA hPl hCs h1 hmb hmax htu hcap hslot hcov hsc hcapR hrslot hrsc hrr hfree hp hk hcb + (fun y hy => by rw [hkc] at hy; cases hy; exact hb)).monoA (fun _ h => Or.inr h) + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3RecvBlockUnbA.lean b/proofs/Fl4ProofsA64/P3/Stage3RecvBlockUnbA.lean new file mode 100644 index 000000000..c6d530d06 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3RecvBlockUnbA.lean @@ -0,0 +1,563 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2c): **blocking `Recv` without a reply object, when the receiver's bound +notification holds the receiver's SC** (`maybe_return_sc` really unbinds: a passive server blocking on its endpoint). +`receive_ipc` runs `maybe_return_sched_context` *before* the endpoint dispatch, so the receiver first loses its SC +(`unbWaitA`: both sides of the binding cleared, `choose_new_thread` pending; Wait's `unbWait_*` transport of +`Stage3RecvWaitUnbA`), then blocks on the endpoint as `recvBlockA` (`blkRecvA` at the unbound state). + +* `rcvReturningA` / `specBodyReturningA'`: the programs of `receive_ipc` / `specRecvBodyA` with a bound inactive + notification and a blocking receive (copies of the private `IpcScRecvReturnA` ones). +* `JStage3A.reply_ipcGA`: `ReplyStackRelA` across a step that may rebind SCs but keeps every SC's reply stack. +* **`RecvPreA.recvBlockUnbA`**: `HRecvPostA`, a switch possibly pending (post at `preschedA`, `Stage3PreSchedA`). +* **`JSyscallA.recvBlockUnbA`**: its syscall-entry form. + +The record is carried over the composite step `σ → unbind → block → σ'` as one `SIKA` step with donation +(`SIKA.upTo` of the block), one `QBlkA`, one write set (`mwr_back` turns the block's scheduler words into the +entry's). + +[S] the endpoint is idle or holds waiting receivers; `can_reply = false`; the caller's bound notification `n` is +inactive and its SC is the caller's (`BndUnbA`); `numNodes = 1`. +[P] `ArchTotal`, `ArchPlat`, `hCs`, `hmb`, `hmax`, `htu`. New module. +-/ +import Fl4ProofsA64.P3.Stage3PreSchedA +import Fl4ProofsA64.P3.Stage3RecvBlockA +import Fl4ProofsA64.P3.Stage3RecvWaitUnbA +import Fl4ProofsA64.P3.IpcScGlueA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4SpecA64 Sched + +noncomputable section + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +/-- FL4's `receive_ipc` body with an inactive bound notification and a blocking receive (copy of the private +`rcvReturningProgramA`). -/ +theorem rcvReturningA (s : S) (a : A) (t e n rv : U64) + (hz : n ≠ 0#u64) (hb : L.mem s (tBoundA t) = n) + (hs : stOf (L.mem s) n ≠ obj.NTFN_ACTIVE) : + rcvBodyA st ar s a t e true rv = + (notification.maybe_return_sched_context st s n t >>= fun s' => rcvDispA st ar s' a t e true rv) := by + unfold rcvBodyA + have hbRead := eq_of_spec_fd (obj.Tcb.bound_ntfn.spec (L := L) s t) + have hsRead := eq_of_spec_fd (obj.Ntfn.state.spec (L := L) s n) + rw [hbRead, bind_ok, hb, if_pos (show (n != 0#u64) = true by simpa using hz), + hsRead, bind_ok, epState_eq, if_neg hs] + simp only [show (n != 0#u64) = true by simpa using hz, ↓reduceIte, Std.bind_tc_eq, + Std.bind_assoc, bind_ok] + rfl + +end + +theorem specBodyReturningA' {σ σ' : State Unit} {t e n : ObjRef} {xt : Tcb} + {x : Endpoint} {xn : Notification} (hkt : σ.kheap t = some (.tcb xt)) + (hke : σ.kheap e = some (.endpoint x)) (hkn : σ.kheap n = some (.notification xn)) + (hb : xt.tcbBoundNotification = some n) (hs : isActive xn = false) + (hret : maybeReturnSc (Z := Unit) n t σ = ({((), σ')}, False)) (reply : Option ObjRef) : + specRecvBodyA t e true reply σ = specDispA t e true reply x σ' := by + unfold specRecvBodyA + rw [nd_bind_one (getEndpoint_eq hke), nd_bind_one (getTcbObjRef_eq _ hkt)] + simp only [hb] + rw [nd_bind_one (getNotification_eq hkn), if_neg (by simp [hs]), + nd_when_true_bindA (by simp [hb]), the_someA, nd_bind_one hret] + cases x <;> rfl + +end + +end Fl4ProofsA64.P3.Endp + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4Spec Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-- (case selector) **The caller's bound notification is inactive and its SC is the caller's**: `maybe_return_sc` +returns the SC to the notification (the complement of `BndOKA`'s blocking clause). -/ +def BndUnbA (N : Set U64) (σ : State Unit) (xt : Tcb) : Prop := + ∃ n ∈ N, ∃ xn : Notification, ∃ sc : ObjRef, xt.tcbBoundNotification = some (specPtr n) ∧ + σ.kheap (specPtr n) = some (.notification xn) ∧ isActive xn = false ∧ xn.ntfnSc = some sc ∧ + xt.tcbSchedContext = some sc + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {J5 : JSlotA S} {R : Set U64 → JSlotA S} {s s' : S} {a : A} {σ σ' : State Unit} + {m : McState} + +/-- **`ReplyStackRelA` across a step keeping every SC's reply stack** (`reply_ipcA` without `SIKA ... false`: +the SCs may be unbound). -/ +theorem JStage3A.reply_ipcGA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7A st R) s a σ m) + (w : WritesIn st s s' (IpcFpA st Z E Rp Bf m σ s)) + (hs : ∀ c ∈ Z.C, ∀ x k, σ.kheap (scNameA m c) = some (.schedContext x k) → + ∃ y k', σ'.kheap (scNameA m c) = some (.schedContext y k') ∧ y.scReplies = x.scReplies) + (hr : ∀ r ∈ Rp, ∀ y, σ'.kheap (specPtr r) = some (.reply y) → + ∃ x, σ.kheap (specPtr r) = some (.reply x) ∧ y.replySc = x.replySc) : + ReplyStackRelA (L.mem s') Rp Z.C σ' (scNameA m) stack := by + refine J.reply.frame (fun r hr' => ?_) (fun c hc x k h => hs c hc x k h) (fun r hr' y h => hr r hr' y h) + have hrH : H r = some .reply := J.utIpc.reply r hr' + obtain ⟨r32, -, rfit⟩ := J.iplaced.e.rep r hr' + have one : ∀ o : U64, 8 ≤ o.val → o.val < 32 → o.val % 8 = 0 → L.mem s' (wa r o) = L.mem s (wa r o) := by + intro o h1 h2 h3 + have hv : (wa r o).val = r.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + refine w.mem_eq (by unfold Aligned; rw [hv]; omega) ?_ + have hw : IpcObjWA N E Rp (wa r o) := Or.inr (Or.inr ⟨r, hr', by omega, by omega⟩) + have other : ∀ r' ∈ Rp, r' ≠ r → r'.val + 32 ≤ r.val ∨ r.val + 32 ≤ r'.val := fun r' hr'' ne => by + have := J.sys.ent.heap.disj r' _ r _ (J.utIpc.reply r' hr'') hrH ne + simp only [HKind.bits] at this + omega + rintro (c | c) + · rcases c with ⟨e, he, c⟩ | ⟨r', hr'', c⟩ | ⟨t, ht, c⟩ | c + · have := J.iplaced.e.er e he r hr'; omega + · by_cases er : r' = r + · subst er; omega + · have := other r' hr'' er; omega + · have := J.iplaced.e.rt r hr' t ht + unfold TWF TL at c + rcases c with ⟨c1, c2⟩ | c + · unfold TWOffN at c2; omega + · omega + · have := J.iplaced.e.br _ c r hr'; omega + · exact J.ipc_offMwrA hw c + exact ⟨one _ (by simp [layout.reply.NEXT]) (by simp [layout.reply.NEXT]) (by simp [layout.reply.NEXT]), + one _ (by simp [layout.reply.PREV]) (by simp [layout.reply.PREV]) (by simp [layout.reply.PREV])⟩ + +set_option maxHeartbeats 4000000 in +/-- **Blocking `Recv` without a reply object whose bound notification returns the receiver's SC** (module +docstring). -/ +theorem RecvPreA.recvBlockUnbA + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p badge slot : U64} {send grant grantReply : Bool} + (hcap : cspace.lookup_cap st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.CAP)) = + ok (.Ok (.Endpoint p badge send true grant grantReply))) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) + (hsc : capA st s slot = .Endpoint p badge send true grant grantReply) + (hp : p ∈ E) {x : Endpoint} (hk : σ.kheap (specPtr p) = some (.endpoint x)) (hcb : CanBlockA true x) + (hbn : ∀ y, σ.kheap σ.curThread = some (.tcb y) → BndUnbA N σ y) : + HRecvPostA st AR ar hPl.ipa n Z.Cs s a m true false σ + (fun σ' => ∃ y sc xs ns, σ.kheap σ.curThread = some (.tcb y) ∧ y.tcbSchedContext = some sc ∧ + σ.kheap sc = some (.schedContext xs ns) ∧ + σ' = blkRecvA (unbWaitA (preschedA σ) σ.curThread sc xs ns y) σ.curThread (specPtr p) x + { y with tcbSchedContext := none }) := by + have htZ : L.curW s ∈ Z.T := J.sr.curT + set t := L.curW s with ht_def + have ct : σ.curThread = specPtr t := J.j.1.sch.curThread + obtain ⟨xt, hkt, r⟩ := J.ep.tcb t htZ + have hkc : σ.kheap σ.curThread = some (.tcb xt) := by rw [ct]; exact hkt + obtain ⟨nn, hnn, xn, sc, hbd, hkn, hna, hsn, hst0⟩ := hbn xt hkc + have hthT : Z.th t = some .tcb := (J.sr.thT t).2 htZ + have hnvm : L.mem s (stA t) ≠ sched.TS_RUNNING_VM := J.nvm + have hrunS : xt.tcbState = .running ∨ xt.tcbState = .restart := J.state hkt + have hr : runnable xt.tcbState = true := by rcases hrunS with e | e <;> rw [e] <;> rfl + have hidle : specPtr t ≠ σ.idleThread := by + obtain ⟨y, hy, hs⟩ := J.sinv.idleTcb + intro h + rw [← h, hkt] at hy + cases hy + rcases hrunS with e | e <;> rw [e] at hs <;> cases hs + have hRun : SndRunA σ (specPtr t) xt := J.sndRun hkt + have hcm : m.curCore < fl4A64Platform.numNodes := by rw [J.j.1.sch.core]; exact hCs _ J.j.1.sch.cpuCs + have hO : SchedOpsEA st Z.T E Rp Bf (IpcSchedRelA st Z m κ) := + (ipcSchedOpsA (ar := ar) J.iplaced J.ipcPlace hcm hmb hmax htu).2.2.1 + have hF : SchedFootEA st Z.T E Rp Bf (IpcSchedRelA st Z m κ) (fun σ s => MWrA (st := st) Z m σ s) := + schedFootEA_of_sched + have hte : specPtr t ≠ specPtr p := fun h => by rw [h, hk] at hkt; cases hkt + -- the unbinding + obtain ⟨xs0, ns0, hks0⟩ : ∃ xs0 ns0, σ.kheap sc = some (.schedContext xs0 ns0) := by + obtain ⟨z, j, hz, -⟩ := J.sr.inv.back (specPtr t) xt sc hkt hst0 + exact ⟨z, j, hz⟩ + have hNT : sc ≠ specPtr t := fun e => by rw [e, hkt] at hks0; cases hks0 + have hsi : sc ≠ idleScPtr := fun e => J.sr.inv.ntfnIdle (specPtr nn) xn hkn (by rw [hsn, e]) + have hr' : ActReadyA σ := J.sr.actReady + have P := preschedA_keepA σ + have hktp : (preschedA σ).kheap (specPtr t) = some (.tcb xt) := by rw [P.kh]; exact hkt + have hksp : (preschedA σ).kheap sc = some (.schedContext xs0 ns0) := by rw [P.kh]; exact hks0 + set UW : State Unit := unbWaitA (preschedA σ) (specPtr t) sc xs0 ns0 xt with hUW + have eU : maybeReturnSc (Z := Unit) (specPtr nn) (specPtr t) σ = ({((), UW)}, False) := + maybeReturnSc_unbSwA hkn hkt hsn hst0 hks0 hNT ct.symm hr' (J.pendingSc hkt hst0) + have hkt1 : UW.kheap (specPtr t) = some (.tcb { xt with tcbSchedContext := none }) := unbWait_atTA hNT + have hks1 : UW.kheap sc = some (.schedContext { xs0 with scTcb := none } ns0) := unbWait_atSA hNT + have hpS : specPtr p ≠ sc := fun e => by rw [e, hks0] at hk; cases hk + have hk1 : UW.kheap (specPtr p) = some (.endpoint x) := by + rw [show UW.kheap (specPtr p) = σ.kheap (specPtr p) from (unbWait_keepA hNT (Ne.symm hte) hpS).trans (congrFun P.kh _)]; exact hk + have hRun1 : SndRunA UW (specPtr t) { xt with tcbSchedContext := none } := by + refine ⟨hRun.1, ⟨fun d pr => P.notin _ hRun.2.1.1 (fun h => J.noSelf (by rw [← ct] at h; exact h)) d pr, + fun h => hRun.2.1.2 (P.rls ▸ h)⟩, fun u hu => ?_, fun h => hRun.2.2.2 (h.trans P.idle)⟩ + have e : UW.schedulerAction = .chooseNewThread := rfl + rw [e] at hu; cases hu + have Qu := (P.qk.trans (unbWait_qkeepA hNT hktp hksp)).toSend + have sorted1 := J.ipcSorted.of_qs Qu + -- the bound notification word of the caller + have hz : nn ≠ 0#u64 ∧ L.mem s (tBoundA t) = nn := bound_wordA (J.ep.at_tcb htZ hkt) hbd + have hns : stOf (L.mem s) nn ≠ obj.NTFN_ACTIVE := + stOf_not_activeA (J.ntfn.at_ntfn hnn hkn).obj (fun b e => by simp [isActive, e] at hna) + -- FL4: `receive_ipc` after the reply cap: the SC return, then the endpoint dispatch + set σ' : State Unit := blkRecvA UW (specPtr t) (specPtr p) x { xt with tcbSchedContext := none } with hσ' + have body : rcvBodyA st ar s a t p true 0#u64 ⦃ r => r.2 = a ∧ ∃ (s1 : S) (xs : List U64), + MFrA (st := st) Z m s s1 σ UW ∧ (∀ y ∈ xs, y ∈ Z.T) ∧ x.epQueue = xs.map specPtr ∧ + (∀ y ∈ xs, L.mem s (Notif.tBlockA y) = p) ∧ (∀ a ∈ x.epQueue, ∃ y, UW.kheap a = some (.tcb y)) ∧ + receiveIpcBlocked (Z := Unit) true (specPtr t) (specPtr p) none UW = ({((), σ')}, False) ∧ + EpStateRel Z.T E Rp Bf (L.mem r.1) σ' ∧ IpcSchedRelA st Z m κ r.1 σ' ∧ + WritesIn st s r.1 (BlkRecvFpA (fun σ s => MWrA (st := st) Z m σ s) σ s t p xs) ⦄ := by + rw [rcvReturningA (st := st) (ar := ar) s a t p nn 0#u64 hz.1 hz.2 hns] + refine WP.spec_bind (maybeReturnIpcA J.sr J.ipcPlace J.iplaced.e J.iplaced.n hcm J.ntfn J.ep hnn htZ + hRun.2.1.2) ?_ + rintro s1 ⟨σU, Q, h, H1, HN1, HE1, F⟩ + have eσU : σU = UW := single_outA (h.symm.trans eU) + subst eσU + have hSR1 : IpcSchedRelA st Z m κ s1 UW := ⟨Q, Z.R, H1⟩ + have hx1 := HE1.at_ep hp hk1 + obtain ⟨xs, hQ, hxs, hbq⟩ := hx1.queue + have hxsT : ∀ y ∈ xs, y ∈ Z.T := fun y hy => hQ.mem y hy + have hq1 : ∀ a ∈ x.epQueue, ∃ y, UW.kheap a = some (.tcb y) := by + intro a ha + rw [hxs] at ha + obtain ⟨u, hu, rfl⟩ := List.mem_map.1 ha + obtain ⟨y, hy, -⟩ := HE1.tcb u (hxsT u hu) + exact ⟨y, hy⟩ + have hmq1 : ∀ y ∈ x.epQueue, ∃ z, UW.kheap y = some (.tcb z) ∧ z.tcbPriority = prioOf UW y := fun y hy => by + obtain ⟨z, hz⟩ := hq1 y hy + refine ⟨z, hz, ?_⟩ + simp [prioOf, Fl4Spec.optMapSome, Fl4Spec.optMap, tcbsOf, tcbsOfKh, tcbOf, hz] + have htq1 : specPtr t ∉ x.epQueue := + not_queued_codeA HE1 htZ hkt1 (runnable_codeA hRun1.1) p hp x hk1 + have hsort1 := prioSorted_of_specA J.iplaced.e.tcb HE1 hx1 + (sorted1.1 (specPtr p) ⟨p, hp, rfl⟩ x hk1) + have bd := wait_for_sender_blockA (st := st) hO hF J.iplaced.e s1 UW HE1 hSR1 htZ hkt1 hRun1 hp hk1 hcb hsort1 + 0#u64 (rep := optRef 0#u64) rfl (fun h => absurd optRef_zero h) + have nw := wait_for_sender_block_narrowA (st := st) hO hF J.iplaced.e s1 UW HE1 hSR1 htZ hkt1 hRun1 hp hk1 hQ + obtain ⟨s2, ew, ⟨σ2, hspec, E2, SR2, -⟩, w⟩ := exists_of_specA (WP.spec_and bd nw) + have hspecB := blkRecv_specA hk1 hkt1 htq1 hcb hmq1 + rw [optRef_zero, hspecB] at hspec + have eσ : σ2 = σ' := by + have h : ((), σ2) ∈ ({((), σ')} : Set (Unit × State Unit)) := by + rw [(Prod.mk.inj hspec).1]; exact Set.mem_singleton _ + exact (Prod.mk.inj (Set.mem_singleton_iff.1 h)).2 + subst eσ + have hB : ∀ y ∈ xs, L.mem s (Notif.tBlockA y) = p := fun y hy => by + rw [← (hbq y hy).1] + symm + have hya := J.iplaced.e.tcb y (hxsT y hy) + exact F.wr.mem_eq (Endp.a_blk hya).1 (J.tcbWord_offMwrA (hxsT y hy) (K := 3336#u64) (by simp) (by simp)) + have wC : WritesIn st s s2 (BlkRecvFpA (fun σ s => MWrA (st := st) Z m σ s) σ s t p xs) := + (F.wr.trans w).mono fun b hb => by + rcases hb with h | ((h | h | h) | h | h | h) + · exact Or.inr (Or.inl h) + · exact Or.inl (Or.inl h) + · exact Or.inl (Or.inr (Or.inl h)) + · exact Or.inl (Or.inr (Or.inr h)) + · exact Or.inr (Or.inl (mwr_back (Z := Z) (Z' := Z) rfl rfl F.sz F.wr.1 h)) + · exact Or.inr (Or.inr (Or.inl h)) + · exact Or.inr (Or.inr (Or.inr h)) + have hdisp : rcvDispA st ar s1 a t p true 0#u64 ⦃ q => q = (s2, a) ⦄ := by + have hwait : (do + let s3 ← endpoint.wait_for_sender st s1 t p true 0#u64 + ok (s3, a)) ⦃ q => q = (s2, a) ⦄ := by + rw [ew, bind_ok] + exact (WP.spec_ok _).2 rfl + unfold rcvDispA + step with obj.Ep.state.spec as ⟨st0, hst0'⟩ + rw [epState_eq] at hst0' + cases x with + | idleEP => + rw [hx1.1] at hst0' + subst hst0' + rw [if_pos ep_state_vals.1.symm] + exact hwait + | recvEP q => + rw [hx1.1] at hst0' + subst hst0' + rw [if_neg (by rw [ep_state_vals.1]; decide), if_pos ep_state_vals.2.2.symm] + exact hwait + | sendEP q => simp [CanBlockA] at hcb + refine WP.spec_mono hdisp fun q hq => ?_ + subst hq + exact ⟨rfl, s1, xs, F, hxsT, hxs, hB, hq1, hspecB, E2, SR2, wC⟩ + obtain ⟨⟨s2, a2⟩, ew, ea, s1, xs, F, hxsT, hxs, hB, hq1, hspecB, E2, SR2, wC⟩ := spec_existsA body + simp only at ea E2 SR2 wC + subst a2 + obtain ⟨Q2, Rl, sr2⟩ := SR2 + have hipc : endpoint.receive_ipc st ar s a t (.Endpoint p badge send true grant grantReply) true .Null = + ok (s2, a) := by + have h : endpoint.receive_ipc st ar s a t (.Endpoint p badge send true grant grantReply) true .Null ⦃ q => + q = (s2, a) ⦄ := by + rw [receive_ipc_eqA] + refine WP.spec_bind (take_reply_plainA (R := Rp) (rc' := .nullCap) + (Or.inl ⟨rfl, rfl, rfl⟩ : ReplyCapA Rp .Null .nullCap 0#u64) (fun _ h => by cases h)) ?_ + rintro q rfl + show rcvBodyA st ar s a t p true 0#u64 ⦃ q => q = (s2, a) ⦄ + rw [ew] + exact (WP.spec_ok _).2 rfl + obtain ⟨q, hq', e⟩ := spec_existsA h + rw [hq', e] + have hfl : syscall.handle_recv st ar s a true false = ok (s2, a) := by + unfold syscall.handle_recv + rw [cur_thread_ok, bind_ok, tcb_reg_eqA, bind_ok] + rw [show L.curW s = t from rfl, hcap] + simp only [bind_ok, ↓reduceIte, Bool.false_eq_true] + exact hipc + -- the spec: `handle_recv` + have hSt := J.j.1.st + have hV := J.j.1.cv + have capReg : (L.mem s (regWA t layout.reg.CAP)).bv = xt.tcbArch.tcbContext.userRegs cap_register := by + rw [← r.regs cap_register, regIdxA_capA]; rfl + have hrecv : ((), σ') ∈ (receiveIpc (Z := Unit) (specPtr t) + (absCapA (.Endpoint p badge send true grant grantReply)) true .nullCap σ).1 := by + rw [show absCapA (.Endpoint p badge send true grant grantReply) = + .endpointCap (specPtr p) badge.bv (rightsOfA send true grant grantReply) from rfl, + receiveIpc_bodyA _ _ true (Or.inl ⟨rfl, rfl⟩ : SpecReplyPlainA σ (specPtr t) .nullCap none), + specBodyReturningA' hkt hk hkn hbd hna eU, specDisp_waitA hcb, hspecB] + exact Set.mem_singleton _ + have hmem : ((), σ') ∈ (handleRecv (Z := Unit) true false σ).1 := by + have capRead : NonDet.liftM dataToCptr (asUser (Z := Unit) (specPtr t) (getRegister cap_register)) σ = + ({(toBl (L.mem s (regWA t layout.reg.CAP)).bv, σ)}, False) := by + unfold NonDet.liftM + rw [nd_bind_singleA' (asUser_getRegister_eqA hkt _), ← capReg] + rfl + have hc0 : (lookupCap (Z := Unit) (specPtr t) + (toBl (L.mem s (regWA t layout.reg.CAP)).bv)).run σ = + ({(.ok (absCapA (.Endpoint p badge send true grant grantReply)), σ)}, False) := by + have l := post_of_okA (lookup_cap_recv_refinesA (st := st) _ s σ hSt hV t hthT + (L.mem s (regWA t layout.reg.CAP))) hcap + obtain ⟨e, he, hrel⟩ := l + cases e with + | error _ => exact hrel.elim + | ok ac => + obtain rfl : ac = absCapA (.Endpoint p badge send true grant grantReply) := + (show absCapA (.Endpoint p badge send true grant grantReply) = ac from hrel.2).symm + exact he + rw [handleRecv, nd_gets_bindSc, ct, nd_bind_oneSc capRead] + apply mem_bindA.2 + refine ⟨(.ok (), σ'), ?_, rfl⟩ + have hc1 : (capFaultOnFailure (ofBl (toBl (L.mem s (regWA t layout.reg.CAP)).bv)) true + (lookupCap (Z := Unit) (specPtr t) (toBl (L.mem s (regWA t layout.reg.CAP)).bv))).run σ = + ({(.ok (absCapA (.Endpoint p badge send true grant grantReply)), σ)}, False) := run_handleE_okA hc0 + rw [run_bind_okA hc1] + dsimp only [absCapA] + rw [if_pos (by simp [AllowRecv, rightsOfA])] + simp only [Bool.false_eq_true, ↓reduceIte] + rw [run_bind_okA (run_returnOkA _ _)] + unfold NonDetE.liftE + rw [ExceptT.run_lift] + exact mem_bindA.2 ⟨((), σ'), hrecv, rfl⟩ + -- P1's walk + obtain ⟨Pt, Ub, Pp7, Tb7, Hr, Te, Kt, Lv7, Re, bits, KLv, W⟩ := J7A.unp J.j7 + have I := J.delIn hA hPl W + have h : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s := by + obtain ⟨q, b, x, sg, sc⟩ := J.sys.ent.bxi + exact ⟨q, b, x, sc, sg⟩ + have hext : ∀ z ∈ (Aeneas.Std.Array.repeat 3#usize 0#u64).val, z ≠ 0#u64 → z ∈ OS := fun z hz hne => by + exact absurd (by simpa [Aeneas.Std.Array.repeat] using hz) hne + have D := J.decPreA (extra := Aeneas.Std.Array.repeat 3#usize 0#u64) hslot hext + have Pz := J.toJStage3A.sysPlace hext hslot hcov + have hH : H p = some .ep := J.utIpc.ep p hp + have regs : RpPost st H Z.Cs OS Z.V ia ie Ac s s2 := by + have walk := Fl4ProofsA64.P1.W7.R.ST.receive_ipc.rpw (st := st) (ar := ar) hA a t + (.Endpoint p badge send true grant grantReply) true .Null p I J.core0 J.ko Pz (SW.start J.sys.sysl J.symL h) D.cb + (by rcases J.runW with e | e | e <;> rw [e] <;> + simp [sched.TS_RUNNING, sched.TS_RESTART, sched.TS_RUNNING_VM, sched.TS_INACTIVE]) + rfl hH + (fun k hk' => by + rw [hH] at hk' + cases hk' + exact ⟨slot, hslot, by rw [hsc]; exact names_ep⟩) + (fun _ _ c => by cases c) + (J.tcbs ▸ J.runVcpu) hnvm + exact (post_of_okA (P := fun q => RpW st H Z.Cs OS Z.V ia ie Ac s q.1) walk hipc).post + obtain ⟨bxi, sysl, symL, userOK, j7⟩ := J.toJStage3A.finRpA hA hPl rfl regs σ' + -- the record over the composite step + have B : RcvStepA UW σ' (specPtr t) (specPtr p) x { xt with tcbSchedContext := none } := blkRecvA_step hte + have wI : WritesIn st s s2 (IpcFpA st Z E Rp Bf m σ s) := wC.mono fun b h => by + have hta := J.iplaced.e.tcb t htZ + have tw : ∀ (k : Nat), b.val = t.val + k → TWOffN k → IpcFpA st Z E Rp Bf m σ s b := fun k hv hk' => + Or.inl (Or.inr (Or.inr (Or.inl ⟨t, htZ, Or.inl (twf_ofA k hv hk')⟩))) + rcases h with (c | c | c) | c | c | ⟨v, hv, c⟩ + · exact tw 3328 c (by unfold TWOffN; omega) + · exact tw 3336 c (by unfold TWOffN; omega) + · exact tw 3360 c (by unfold TWOffN; omega) + · exact Or.inr c + · exact Or.inl (Or.inl ⟨p, hp, c⟩) + · have hvT : v ∈ Z.T := by + rcases List.mem_cons.1 hv with rfl | hv + exacts [htZ, hxsT v hv] + exact Or.inl (Or.inr (Or.inr (Or.inl ⟨v, hvT, Or.inr c⟩))) + have wN : WritesIn st s s2 (fun b => SchedStepNWA (st := st) N E Rp Z m σ s b ∨ BufW Bf b) := + wI.mono fun _ h => h.elim (ewrite_stepNA_buf J.sr.core.tcbs) fun h => Or.inl (Or.inl (Or.inl h)) + have wI' : WritesIn st s s2 (fun b => IpcFpA st Z E Rp Bf m σ s b ∨ RepWA Rp b) := wI.mono fun _ => Or.inl + set M : ObjRef → Prop := fun o => o = specPtr t with hMdef + have hti : specPtr t ≠ σ.idleThread := hidle + have K1 : SIKA σ.idleThread M (fun _ => False) true σ UW := (preschedA_sikaA hr').trans (unbWait_sikaA hNT hktp hksp hti hsi P.noSw) + have K2 : SIKA σ.idleThread M (fun _ => False) false UW σ' := B.sika hkt1 hk1 rfl + have K : SIKA σ.idleThread M (fun _ => False) true σ σ' := K1.trans K2.upTo + have notM : ∀ c < n, ¬ M (m.idleThreadOf c) := by + intro c hc' h + obtain ⟨y, hy, hs⟩ := J.sidle.f3 c hc' + rw [h, hkt] at hy + cases hy + rcases hrunS with e | e <;> rw [e] at hs <;> cases hs + have sinv : SInvXA σ' m n := by + refine K.sinvSw J.sinv J.sidle J.body.sw J.noSelf (fun u hu h => J.swCur u hu h) notM + (fun h => hRun.2.2.2 h.symm) ?_ (fun _ h => h.elim) (fun _ h => h.elim) + (fun y hy => by rw [show y = specPtr t from hy]; exact hRun.2.1.2) (J.sidle.rdyA J.sinv) + (K.mfA J.sinv fun q hq x hx => by + rw [show q = specPtr t from hq, hkt] at hx; cases hx + exact fun hf => by have h0 := FaultStA.notRun hf; rw [hRun.1] at h0; cases h0) + intro y hy dm pr + rw [show y = specPtr t from hy] + exact hRun.2.1.1 dm pr + have wk := J.toJStage3A.ntfnWait_rcvA (s' := s2) htZ hp hxsT hB wC + have ntfn := J.toJStage3A.ntfn_ipcMA wI' K E2 wk (fun u hu hm y hy q h => by + rw [show specPtr u = specPtr t from hm, hkt] at hy + cases hy + rcases hrunS with e | e <;> rw [e] at h <;> cases h) (fun u hu hm y hy q h => by + rw [show specPtr u = specPtr t from hm, B.atT] at hy + cases hy + cases h) + have hMv : ∀ u ∈ Z.T, M (specPtr u) → L.mem s (Fl4ProofsA64.P1.stA u) ≠ sched.TS_RUNNING_VM := fun u _ hu => by + rw [specPtr_inj (show specPtr u = specPtr t from hu)]; exact hnvm + have ssz1 : SSzA σ UW := fun q y k h => by + by_cases e : q = sc + · rw [e] at h ⊢ + rw [hks1] at h; cases h + exact ⟨xs0, hks0⟩ + · have e2 : q ≠ specPtr t := fun e' => by rw [e', hkt1] at h; cases h + exact ⟨y, by rw [← show UW.kheap q = σ.kheap q from (unbWait_keepA hNT e2 e).trans (congrFun P.kh q)]; exact h⟩ + have ssz2 : SSzA UW σ' := fun q y k h => by + have e1 : q ≠ specPtr t := fun e' => by rw [e', B.atT] at h; cases h + have e2 : q ≠ specPtr p := fun e' => by rw [e', B.atE] at h; cases h + exact ⟨y, by rw [← B.far q e1 e2]; exact h⟩ + have ssz : SSzA σ σ' := ssz1.trans ssz2 + have HS : SpecSchedA σ σ' := (P.spec.trans (unbWait_specSchedA hNT hktp hksp)).trans (B.specSched hkt1 hk1) + have Qb := B.qblk sorted1 hkt1 hk1 hq1 + have Qk : QBlkA (specPtr '' E) (specPtr '' N) σ σ' := + ⟨Qb.sorted, fun h => Qb.act (Qu.act h), fun p' y hp' => by + obtain ⟨x4, h4, a4⟩ := Qb.rt p' y hp' + obtain ⟨x0, h0, a0⟩ := Qu.rt p' x4 h4 + exact ⟨x0, h0, fun h => a4 (a0 h)⟩, Qu.dom.trans Qb.dom⟩ + have sv1 : SchedValidA UW := unbWait_schedValidA hNT hktp hksp (preschedA_schedValidA J.schedValid) + (fun d pr => P.notin _ hRun.2.1.1 (fun h => J.noSelf (by rw [← ct] at h; exact h)) d pr) + (fun h => hRun.2.1.2 (P.rls ▸ h)) + have sv : SchedValidA σ' := B.schedValid sv1 hkt1 hk1 (fun sc' h => by cases h) + have keepS : ∀ q y k, σ.kheap q = some (.schedContext y k) → ∃ y', σ'.kheap q = some (.schedContext y' k) ∧ + y'.scReplies = y.scReplies := fun q y k h => by + by_cases e : q = sc + · rw [e] at h ⊢ + rw [hks0] at h; cases h + exact ⟨{ xs0 with scTcb := none }, by rw [B.far sc hNT (Ne.symm hpS)]; exact hks1, rfl⟩ + · have e2 : q ≠ specPtr t := fun e' => by rw [e', hkt] at h; cases h + have e3 : q ≠ specPtr p := fun e' => by rw [e', hk] at h; cases h + exact ⟨y, by rw [B.far q e2 e3, show UW.kheap q = σ.kheap q from (unbWait_keepA hNT e2 e).trans (congrFun P.kh q)]; exact h, rfl⟩ + have reply := J.toJStage3A.reply_ipcGA wI + (fun c _ y k h => by + obtain ⟨y', h2, e⟩ := keepS _ y k h + exact ⟨y', k, h2, e⟩) + (fun r' _ y h => by + have e1 : specPtr r' ≠ specPtr t := fun e' => by rw [e', B.atT] at h; cases h + have e2 : specPtr r' ≠ specPtr p := fun e' => by rw [e', B.atE] at h; cases h + rw [B.far _ e1 e2] at h + rcases unbWait_casesA hNT h with ⟨-, c⟩ | ⟨-, c⟩ | ⟨-, -, h0⟩ + · cases c + · cases c + · exact ⟨y, by rw [P.kh] at h0; exact h0, rfl⟩) + have J2 := J.toJStage3A.of_ipcStepUSA (Q := Q2) (Rl := Rl) (curOf := Z.curOf) (curScOf := Z.curScOf) wN + HS ssz Qk (fun _ _ => by + show σ'.machineState.underlyingMemory _ = σ.machineState.underlyingMemory _ + rw [B.machine]; exact congrFun (congrArg (fun z => z.underlyingMemory) P.mach) _) (fun _ _ => rfl) bxi sysl symL userOK j7 sr2 E2 ntfn reply J.deadExt.reply K.ipcKeep + (J.toJStage3A.tw_ipcA wI') sinv (K.sidle J.sidle notM (J.sidle.rdyA J.sinv)) (J.toJStage3A.runVcpu_ipcA wI' K E2 hMv) + (J.toJStage3A.blkQ_recvA htZ hp B K E2 wk) sv + have hcur : σ'.curThread = σ.curThread := B.curThread.trans P.cur + refine ⟨s2, a, σ', hfl, hmem, ⟨xt, sc, xs0, ns0, hkc, hst0, hks0, by rw [ct]⟩, A64RelS3.ofJ J2 ?bo, + curW_keptA J.toJStage3A J2 hcur, ?bo⟩ + have act' : σ'.schedulerAction = .chooseNewThread := by rcases B.act with h | h <;> exact h + refine J.body.step (σ' := σ') (fun u e => by rw [act'] at e; cases e) + (by show σ'.curTime = _; rw [B.frame]; exact P.time) + (by show σ'.machineState.lastMachineTime = _; rw [B.frame]; exact congrArg _ P.mach) hcur + (by show σ'.curSc = _; rw [B.frame]; exact P.csc) (by show σ'.consumedTime = _; rw [B.frame]; exact P.con) + (fun _ _ _ _ _ h => by rw [B.releaseQueue] at h; exact P.rls ▸ h) + (fun y' k' hz => ?_) (fun h => absurd h (by rw [act']; intro c; cases c)) + rcases B.cases hz with ⟨-, c⟩ | ⟨-, c⟩ | ⟨-, -, h0⟩ + · cases c + · cases c + · rcases unbWait_casesA hNT h0 with ⟨-, c⟩ | ⟨e, c⟩ | ⟨-, -, h00⟩ + · cases c + · cases c + exact ⟨xs0, by rw [e]; exact hks0, rfl, rfl, Or.inr rfl⟩ + · exact ⟨y', by rw [P.kh] at h00; exact h00, rfl, rfl, Or.inl rfl⟩ + +set_option maxHeartbeats 1000000 in +/-- **`RecvPreA.recvBlockUnbA` at a syscall entry** (no switch pending: the enqueue is the identity). -/ +theorem JSyscallA.recvBlockUnbA + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p badge slot : U64} {send grant grantReply : Bool} + (hcap : cspace.lookup_cap st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.CAP)) = + ok (.Ok (.Endpoint p badge send true grant grantReply))) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) + (hsc : capA st s slot = .Endpoint p badge send true grant grantReply) + (hp : p ∈ E) {x : Endpoint} (hk : σ.kheap (specPtr p) = some (.endpoint x)) (hcb : CanBlockA true x) + (hbn : ∀ y, σ.kheap σ.curThread = some (.tcb y) → BndUnbA N σ y) : + HRecvPostA st AR ar hPl.ipa n Z.Cs s a m true false σ + (fun σ' => ∃ y sc xs ns, σ.kheap σ.curThread = some (.tcb y) ∧ y.tcbSchedContext = some sc ∧ + σ.kheap sc = some (.schedContext xs ns) ∧ + σ' = blkRecvA (unbWaitA σ σ.curThread sc xs ns y) σ.curThread (specPtr p) x + { y with tcbSchedContext := none }) := by + have h := RecvPreA.recvBlockUnbA hA hPl J.toRecvPreA hCs h1 hmb hmax htu hcap hslot hcov hsc hp hk hcb hbn + rw [preschedA_noswitchA J.noSwitch] at h + exact h + +/-- **`BndOKA` without its blocking clause splits into the two blocking arms**: a bound inactive notification +either keeps the receiver's SC out of `maybe_return_sc` (`BndOKA ... true`, `recvBlockA`) or holds it (`BndUnbA`, +`recvBlockUnbA`). So the blocking arms need only `BndOKA ... false` and this dispatcher. -/ +theorem JStage3A.bndOK_blockA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7A st R) s a σ m) + (hcm : m.curCore < fl4A64Platform.numNodes) {xt : Tcb} {t : U64} (ht : t ∈ Z.T) + (hkt : σ.kheap (specPtr t) = some (.tcb xt)) (h : BndOKA N (L.mem s) σ t xt false) : + BndOKA N (L.mem s) σ t xt true ∨ BndUnbA N σ xt := by + rcases h with hb | ⟨nn, hn, hb, hx⟩ + · exact Or.inl (Or.inl hb) + · obtain ⟨xn, hkn, -⟩ := J.ntfn.ntfn nn hn + obtain ⟨hna, -⟩ := hx xn hkn + by_cases hc : xn.ntfnSc = xt.tcbSchedContext ∧ xn.ntfnSc ≠ none + · obtain ⟨sc, hsc⟩ := Option.ne_none_iff_exists'.1 hc.2 + exact Or.inr ⟨nn, hn, xn, sc, hb, hkn, hna, hsc, hc.1.symm.trans hsc⟩ + · refine Or.inl (Or.inr ⟨nn, hn, hb, fun xn' hk' => ?_⟩) + obtain rfl : xn' = xn := by + rw [hkn] at hk'; exact (KernelObject.notification.inj (Option.some.inj hk')).symm + refine ⟨hna, fun _ => ⟨hc, ?_⟩⟩ + by_cases h0 : L.mem s (nScA nn) = 0#u64 + · exact Or.inl h0 + · refine Or.inr fun e => hc ?_ + obtain ⟨z, hz, hzs⟩ := notifScWord hn J.ntfn J.sr.inv hcm + obtain ⟨y, hy, hys⟩ := J.sr.sch.tcb t ht + rw [hkn] at hz; cases hz + rw [hkt] at hy; cases hy + have h1 : optScA m (L.mem s (nScSc nn)) = optScA m (L.mem s (scA t)) := by + have : L.mem s (nScSc nn) = L.mem s (scA t) := e + rw [this] + refine ⟨hzs.trans (h1.trans hys.symm), ?_⟩ + intro hn0 + have h2 := hzs.symm.trans hn0 + unfold optScA at h2 + split_ifs at h2 with hh + exact h0 hh + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3RecvBoundA.lean b/proofs/Fl4ProofsA64/P3/Stage3RecvBoundA.lean new file mode 100644 index 000000000..768bb7322 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3RecvBoundA.lean @@ -0,0 +1,435 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2c): **`Recv` on an endpoint when the receiver's bound notification is active**, at +the record: `handle_recv` with `can_reply = false` on an Endpoint cap, where `receive_ipc` completes the signal of the +bound notification (`complete_signal`) and never looks at the endpoint. The caller gets the badge and the +notification becomes idle. The caller already has an SC, which is the current SC, so FL4's donation and sporadic +unblock check do nothing, and neither does the spec's tail (the twin of `waitActiveA`). + +* `completeTail_curScA`: the spec's tail of `complete_signal` does nothing for a thread whose SC is the current SC. +* `completeBodyA_curScA`: FL4's body after the donation does nothing for such a thread. +* **`JSyscallA.recvBoundActiveA`** (any `blocking`): `HRecvPostA`. + +[S] the Endpoint cap is a receive cap, the thread's bound notification is active (`hbn`, `hact`), `can_reply = false`. +[P] `ArchTotal`, `ArchPlat`, `hCs`, `hmb`, `hmax`, `htu`. New module. +-/ +import Fl4ProofsA64.P3.Stage3RecvActiveA +import Fl4ProofsA64.P3.EpRecvBndA +import Fl4ProofsA64.P3.ScRecvA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4Spec Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-- The spec's tail of `complete_signal` does nothing for a thread whose SC is the current SC. -/ +theorem completeTail_curScA {τ : State Unit} {t p : ObjRef} {x : Tcb} {z : Notification} + (hk : τ.kheap t = some (.tcb x)) (hsc : x.tcbSchedContext = some τ.curSc) + {y : SchedContext} {k : Nat} (hy : τ.kheap τ.curSc = some (.schedContext y k)) + (hz : τ.kheap p = some (.notification z)) : + specCompleteTailA t p τ = ({((), τ)}, False) := by + unfold specCompleteTailA maybeDonateSc + have g := getTcbObjRef_eq (Z := Unit) Tcb.tcbSchedContext hk + rw [hsc] at g + have d : (do + let scOpt ← getTcbObjRef (Z := Unit) Tcb.tcbSchedContext t + NonDet.when (scOpt = none) <| + NonDet.bind (getNtfnObjRef Notification.ntfnSc p) <| NonDet.maybeM fun scPtr => do + let scTcb ← getScObjRef SchedContext.scTcb scPtr + NonDet.when (scTcb = none) do + schedContextDonate scPtr t + schedContextResume scPtr) τ = ({((), τ)}, False) := by + rw [nd_bind_one g] + show NonDet.when _ _ τ = _ + unfold NonDet.when + rw [if_neg (by simp)] + rfl + have hif : ifCondRefillUnblockCheck (Z := Unit) (some τ.curSc) (some false) (some false) τ = + ({((), τ)}, False) := by + unfold ifCondRefillUnblockCheck NonDet.maybeM + dsimp only + rw [nd_bind_one (Endp.getSchedContext_eqA hy), nd_gets_bindSc, nd_pure_bind'] + unfold NonDet.when + split + · rename_i h + exact absurd rfl (h.2 rfl) + · rfl + rw [nd_bind_one d, nd_bind_one g, + complete_spec_eqA hz (some τ.curSc) (fun q e => ⟨y, k, by cases e; exact hy⟩)] + split_ifs + · exact hif + · rfl + +/-- The spec's complete tail does nothing for a passive thread on a notification without an SC. -/ +theorem completeTail_nullA {τ : State Unit} {t p : ObjRef} {x : Tcb} {z : Notification} + (hk : τ.kheap t = some (.tcb x)) (hsc : x.tcbSchedContext = none) + (hz : τ.kheap p = some (.notification z)) (hzn : z.ntfnSc = none) : + specCompleteTailA t p τ = ({((), τ)}, False) := by + unfold specCompleteTailA + have g := getTcbObjRef_eq (Z := Unit) Tcb.tcbSchedContext hk + rw [hsc] at g + rw [nd_bind_one (maybeDonateSc_nullA hk hsc hz hzn), nd_bind_one g, + complete_spec_eqA hz none (fun q e => by cases e)] + split_ifs <;> rfl + +section +variable {S A : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- FL4's `complete_signal` body does nothing for a thread without an SC. -/ +theorem completeBodyA_nullA {s : S} {a : A} {t p : U64} (h : L.mem s (scA t) = 0#u64) : + completeBodyA st s a t p = ok (s, a) := by + unfold completeBodyA + rw [eq_of_spec_fd (obj.Tcb.sc.spec s t), bind_ok, h, sc_sporadic_eq] + simp + +/-- FL4's `complete_signal` body after the donation does nothing for a thread whose SC is the current SC. -/ +theorem completeBodyA_curScA {s : S} {a : A} {t p : U64} (hcsc : L.mem s (L.nodeA s layout.node.CUR_SC) = L.mem s (scA t)) : + completeBodyA st s a t p = ok (s, a) := by + rw [complete_body_eqA] + split_ifs with h + · unfold notification.unblock_sporadic + rw [eq_of_spec_fd (obj.Tcb.sc.spec s t), eq_of_spec_fd (sched.cur_sc.spec s)] + simp only [bind_ok, sc_ne_eqA, sc_sporadic_eq] + simp [hcsc] + · rfl + +end + +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {s : S} {a : A} {σ : State Unit} {m : McState} + +set_option maxHeartbeats 4000000 in +/-- **`Recv` on an endpoint with an active bound notification** (module docstring). -/ +theorem RecvPreA.recvBoundActiveA + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {pe badge slot : U64} {send grant grantReply blk : Bool} + (hcap : cspace.lookup_cap st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.CAP)) = + ok (.Ok (.Endpoint pe badge send true grant grantReply))) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) + (hsc : capA st s slot = .Endpoint pe badge send true grant grantReply) + (hpe : pe ∈ E) {xe : Endpoint} (hke : σ.kheap (specPtr pe) = some (.endpoint xe)) + {p : U64} (hp : p ∈ N) {x : Notification} (hk : σ.kheap (specPtr p) = some (.notification x)) + {b0 : Badge} (hact : x.ntfnObj = .activeNtfn b0) + (hbn : ∀ y, σ.kheap σ.curThread = some (.tcb y) → y.tcbBoundNotification = some (specPtr p)) + (hown : (∀ x, σ.kheap σ.curThread = some (.tcb x) → x.tcbSchedContext = some σ.curSc) ∨ + (x.ntfnSc = none ∧ ∀ y, σ.kheap σ.curThread = some (.tcb y) → y.tcbSchedContext = none)) : + HRecvPostA st AR ar hPl.ipa n Z.Cs s a m blk false σ + (fun σ' => ∃ y, σ.kheap σ.curThread = some (.tcb y) ∧ + σ' = rsPreσA σ σ.curThread (specPtr p) y x b0) := by + have htZ : L.curW s ∈ Z.T := J.sr.curT + set t := L.curW s with ht_def + have ct : σ.curThread = specPtr t := J.j.1.sch.curThread + obtain ⟨xt, hkt, r⟩ := J.ep.tcb t htZ + have hkc : σ.kheap σ.curThread = some (.tcb xt) := by rw [ct]; exact hkt + have hthT : Z.th t = some .tcb := (J.sr.thT t).2 htZ + have hP := J.iplaced + have hx := J.ntfn.at_ntfn hp hk + have hnbd := hbn xt hkc + obtain ⟨hp0, hpw⟩ := optRef_some (r.bound.symm.trans hnbd) + have hbound : L.mem s (tBoundA t) = p := specPtr_inj hpw + have hpn0 : p ≠ 0#u64 := hbound ▸ hp0 + set v := L.mem s (nBadgeA p) with hv + have hb0 : v.bv = b0 := by + have := hx.obj; rw [hact] at this; exact this.2.2 + set s1 := L.wr s (Endp.regA t layout.reg.BADGE) v with hs1 + set s2 := rsPreA st s t p with hs2 + set σ1 := updK σ (specPtr t) (.tcb (setReg xt badge_register b0)) with hσ1 + set σ2 := rsPreσA σ (specPtr t) (specPtr p) xt x b0 with hσ2 + have hta := J.sr.core.tcbs t htZ + have hbn' : badgeRegA t = Endp.regA t layout.reg.BADGE := by + apply UScalar.eq_of_val_eq + have := hta.fits + have h0 : (layout.reg.BADGE).val = 0 := by simp [layout.reg.BADGE, layout.reg.X0] + rw [badgeRegA_eq, Endp.regA_val (hP.e.tcb t htZ) (by rw [h0]; decide), h0, wa_valm, + Nat.mod_eq_of_lt (by simp; omega)] + simp + obtain ⟨e1, e2, hR2, hW2, -⟩ := recv_active_prefix (st := st) hP.n J.ntfn htZ hp hk hact hkt + -- the FL4 writes + have hs2' : s2 = L.wr s1 (epTSA p) ((L.mem s1 (epTSA p) &&& ~~~3#u64) ||| (obj.NTFN_IDLE &&& 3#u64)) := by + rw [hs2, rsPreA, hbn'] + have hset : obj.Ntfn.set_state st p s1 obj.NTFN_IDLE = ok s2 := by + obtain ⟨q, hq, e⟩ := spec_existsA (obj.Ntfn.set_state.spec (st := st) s1 p obj.NTFN_IDLE) + rw [hq, e, ← hs2'] + have hbv : (Endp.regA t layout.reg.BADGE).val = t.val + 256 := by + have h0 : (layout.reg.BADGE).val = 0 := by simp [layout.reg.BADGE, layout.reg.X0] + rw [Endp.regA_val (hP.e.tcb t htZ) (by rw [h0]; decide), h0] + have hbal : Aligned (Endp.regA t layout.reg.BADGE) := by + have e : Endp.regA t layout.reg.BADGE = wa t 256#u64 := by + rw [← hbn', badgeRegA_eq] + rw [e]; exact field_aligned hta (by simp) (by simp) + have htsal : Aligned (epTSA p) := (foot_ts hP.n hp).1 + have htsv : p.val ≤ (epTSA p).val ∧ (epTSA p).val < p.val + 40 := by + rw [epTSA_eq, nfield_val (hP.n.ntfn p hp) (by simp)] + simp + have w1 : WritesIn st s s1 (fun b => b = Endp.regA t layout.reg.BADGE) := writesIn_wr hbal + have w2 : WritesIn st s1 s2 (fun b => b = epTSA p) := by + rw [hs2']; exact writesIn_wr htsal + have w : WritesIn st s s2 (fun b => b = Endp.regA t layout.reg.BADGE ∨ b = epTSA p) := + w1.trans w2 + have hfit := hta.fits + have htv : ∀ O : U64, O.val < 4096 → (wa t O).val = t.val + O.val := fun O hO => by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have tcbKeep : ∀ O : U64, O.val < 4096 → O.val % 8 = 0 → O.val ≠ 256 → L.mem s2 (wa t O) = L.mem s (wa t O) := by + intro O h1 h2 h3 + refine w.mem_eq (field_aligned hta h1 h2) fun c => ?_ + rcases c with c | c <;> have := congrArg (fun w : U64 => w.val) c <;> simp only [htv O h1] at this + · rw [hbv] at this; omega + · have := hP.n.disj p hp t htZ; omega + have hsc0 : L.mem s2 (scA t) = L.mem s (scA t) := by + rw [scA_eq]; exact tcbKeep _ (by simp) (by simp) (by simp) + have htH : t ∈ H.tcbs := by rw [J.tcbs]; exact htZ + have hH : H p = some .ntfn := J.utIpc.ntfn p hp + have hnsc2 : x.ntfnSc = none → L.mem s2 (Notif.nScA p) = 0#u64 := fun h => by + have h0 : L.mem s (Notif.nScA p) = 0#u64 := by + have e := hx.sc.symm.trans h + unfold optRef at e + split_ifs at e with c + exact c + rw [← h0] + refine w.mem_eq (foot_sc hP.n hp).1 fun c => ?_ + have vS : (Notif.nScA p).val = p.val + 32 := by + rw [nScA_eq, nfield_val (hP.n.ntfn p hp) (by simp)]; simp + rcases c with c | c + · have e := congrArg (fun w : U64 => w.val) c + simp only [vS, hbv] at e + have := hP.n.disj p hp t htZ + omega + · have e := congrArg (fun w : U64 => w.val) c + simp only [vS, epTSA_eq, nfield_val (hP.n.ntfn p hp) (K := 8#u64) (by simp)] at e + simp at e + have hcsc : (∀ x, σ.kheap σ.curThread = some (.tcb x) → x.tcbSchedContext = some σ.curSc) → + L.mem s2 (L.nodeA s2 layout.node.CUR_SC) = L.mem s (scA t) := fun hown => by + have ow := J.ownWords hown + have hcpu : L.cpu s2 = L.cpu s := w.2.1 + have hk2 : L.ksb s2 = L.ksb s := w.1 + have en : L.nodeA s2 layout.node.CUR_SC = L.nodeA s layout.node.CUR_SC := by + unfold LawfulStore.nodeA; rw [hcpu, nodeAt_of_ksb hk2] + rw [en, ← ow.2] + have hcl : (L.cpu s).val < 128 := by + obtain ⟨nn, -, hn, hcs⟩ := J.sr.csN + have := (hcs (L.cpu s)).1 J.sr.sch.cpuCs + omega + have ho : (layout.node.CUR_SC).val < 4288 := by simp [layout.node.CUR_SC] + have hnv := nodeAt_valSc (s := s) J.sr.ksFit hcl ho + have hr : (L.ksb s).val + 223488 ≤ (L.nodeA s layout.node.CUR_SC).val ∧ + (L.nodeA s layout.node.CUR_SC).val < (L.ksb s).val + 772352 := by + unfold LawfulStore.nodeA; rw [hnv]; omega + have al : Aligned (L.nodeA s layout.node.CUR_SC) := by + unfold LawfulStore.nodeA + exact nodeAt_aligned (J.sq.nodes _ J.sq.cur) ho (by simp [layout.node.CUR_SC]) + unfold LawfulStore.cscW + refine w.mem_eq al fun c => ?_ + rcases c with c | c + · refine J.heap_offNodesA (p := t) (b := Endp.regA t layout.reg.BADGE) htH (by omega) (by + rw [hbv]; simp [HKind.bits]) (c ▸ hr) + · refine J.heap_offNodesA (p := p) (b := epTSA p) hH htsv.1 (by + have := htsv.2; simp [HKind.bits]; omega) (c ▸ hr) + -- the notification's `complete_signal` + have ht0 : t ≠ 0#u64 := fun e => (hP.n.tcb t htZ).ne0 (by rw [e]; rfl) + have hbody : completeBodyA st s2 a t p = ok (s2, a) := by + rcases hown with hown | ⟨-, hpas⟩ + · exact completeBodyA_curScA (a := a) (by rw [hcsc hown, hsc0]) + · exact completeBodyA_nullA (a := a) (by rw [hsc0]; exact J.passiveWordA hpas) + have hcs : notification.complete_signal st ar s a p t = ok (s2, a) := by + have h : notification.complete_signal st ar s a p t ⦃ q => q = (s2, a) ⦄ := by + have hsa : stOf (L.mem s) p = obj.NTFN_ACTIVE := stOf_active hx.obj hact + unfold notification.complete_signal + step with obj.Tcb.is_null.spec as ⟨bn, hbn0⟩ + rw [hbn0] + simp only [ht0, decide_false, Bool.false_eq_true, ↓reduceIte] + step with obj.Ntfn.state.spec as ⟨st0, hst0⟩ + rw [epState_eq, hsa] at hst0 + subst hst0 + simp only [bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + step with obj.Ntfn.badge.spec as ⟨b, hb⟩ + refine WP.spec_bind (obj.Tcb.set_reg.spec s t _ _) ?_ + rintro q rfl + rw [hb, ← hv, ← hs1, hset, bind_ok] + have don : notification.maybe_donate_sched_context st ar s2 a t p = ok (s2, a) := by + rcases hown with hown | ⟨hnsc, hpas⟩ + · unfold notification.maybe_donate_sched_context + obtain ⟨i, hi, ei⟩ := spec_existsA (obj.Tcb.sc.spec (st := st) s2 t) + rw [hi, bind_ok, ei, hsc0, if_neg (J.ownWords hown).1] + · exact maybeDonate_nullA (by rw [hsc0]; exact J.passiveWordA hpas) (hnsc2 hnsc) + rw [don, bind_ok] + exact (by rw [hbody]; exact (WP.spec_ok _).2 rfl : completeBodyA st s2 a t p ⦃ q => q = (s2, a) ⦄) + obtain ⟨q, hq, e⟩ := spec_existsA h + rw [hq, e] + have hipc : endpoint.receive_ipc st ar s a t (.Endpoint pe badge send true grant grantReply) blk .Null = + ok (s2, a) := by + have h : endpoint.receive_ipc st ar s a t (.Endpoint pe badge send true grant grantReply) blk .Null ⦃ q => + q = (s2, a) ⦄ := by + rw [receive_ipc_eqA] + refine WP.spec_bind (take_reply_plainA (R := Rp) (rc' := .nullCap) + (Or.inl ⟨rfl, rfl, rfl⟩ : ReplyCapA Rp .Null .nullCap 0#u64) (fun _ h => by cases h)) ?_ + rintro q rfl + refine rcv_body_actA hpn0 hbound (stOf_active hx.obj hact) ?_ + rw [hcs]; exact (WP.spec_ok _).2 rfl + obtain ⟨q, hq, e⟩ := spec_existsA h + rw [hq, e] + have hfl : syscall.handle_recv st ar s a blk false = ok (s2, a) := by + unfold syscall.handle_recv + rw [cur_thread_ok, bind_ok, tcb_reg_eqA, bind_ok] + rw [show L.curW s = t from rfl, hcap] + simp only [bind_ok, ↓reduceIte, Bool.false_eq_true] + exact hipc + -- the spec + have tp : specPtr t ≠ specPtr p := fun e => by rw [e, hk] at hkt; cases hkt + obtain ⟨y, k, hy⟩ := J.sinv.curSc + have sct : σ.curSc ≠ specPtr t := fun e => by rw [e, hkt] at hy; cases hy + have scp : σ.curSc ≠ specPtr p := fun e => by rw [e, hk] at hy; cases hy + have hy2 : σ2.kheap σ.curSc = some (.schedContext y k) := by + show Function.update (Function.update σ.kheap _ _) _ _ σ.curSc = _ + rw [Function.update_of_ne scp, Function.update_of_ne sct]; exact hy + have ht2 : σ2.kheap (specPtr t) = some (.tcb (setReg xt badge_register b0)) := by + show Function.update (Function.update σ.kheap _ _) _ _ (specPtr t) = _ + rw [Function.update_of_ne tp, Function.update_self] + have hn2 : σ2.kheap (specPtr p) = some (.notification (ntfnObjUpdate (fun _ => .idleNtfn) x)) := by + show Function.update (Function.update σ.kheap _ _) _ _ (specPtr p) = _ + rw [Function.update_self] + have hcomp : completeSignal (Z := Unit) (specPtr p) (specPtr t) σ = ({((), σ2)}, False) := by + unfold completeSignal + rw [nd_bind_one (getNotification_eq hk)] + simp only [hact] + rw [nd_bind_one e1, nd_bind_one e2] + rcases hown with hown | ⟨hnsc, hpas⟩ + · exact completeTail_curScA ht2 (hown xt hkc) hy2 hn2 + · exact completeTail_nullA ht2 (hpas xt hkc) hn2 hnsc + have hrecv : ((), σ2) ∈ (receiveIpc (Z := Unit) (specPtr t) + (absCapA (.Endpoint pe badge send true grant grantReply)) blk .nullCap σ).1 := by + rw [show absCapA (.Endpoint pe badge send true grant grantReply) = + .endpointCap (specPtr pe) badge.bv (rightsOfA send true grant grantReply) from rfl, + receiveIpc_bodyA _ _ blk (Or.inl ⟨rfl, rfl⟩ : SpecReplyPlainA σ (specPtr t) .nullCap none), + specBody_actA hkt hke blk none hnbd hk hact, hcomp] + exact Set.mem_singleton _ + have hmem : ((), σ2) ∈ (handleRecv (Z := Unit) blk false σ).1 := by + have capReg : (L.mem s (regWA t layout.reg.CAP)).bv = xt.tcbArch.tcbContext.userRegs cap_register := by + rw [← r.regs cap_register, regIdxA_capA]; rfl + have capRead : NonDet.liftM dataToCptr (asUser (Z := Unit) (specPtr t) (getRegister cap_register)) σ = + ({(toBl (L.mem s (regWA t layout.reg.CAP)).bv, σ)}, False) := by + unfold NonDet.liftM + rw [nd_bind_singleA' (asUser_getRegister_eqA hkt _), ← capReg] + rfl + have hc0 : (lookupCap (Z := Unit) (specPtr t) (toBl (L.mem s (regWA t layout.reg.CAP)).bv)).run σ = + ({(.ok (absCapA (.Endpoint pe badge send true grant grantReply)), σ)}, False) := by + have l := post_of_okA (lookup_cap_recv_refinesA (st := st) _ s σ J.j.1.st J.j.1.cv t hthT _) hcap + obtain ⟨e, he, hrel⟩ := l + cases e with + | error _ => exact hrel.elim + | ok ac => + obtain rfl : absCapA (.Endpoint pe badge send true grant grantReply) = ac := hrel.2 + exact he + have hc1 : (capFaultOnFailure (ofBl (toBl (L.mem s (regWA t layout.reg.CAP)).bv)) true + (lookupCap (Z := Unit) (specPtr t) (toBl (L.mem s (regWA t layout.reg.CAP)).bv))).run σ = + ({(.ok (absCapA (.Endpoint pe badge send true grant grantReply)), σ)}, False) := run_handleE_okA hc0 + rw [handleRecv, nd_gets_bindSc, ct, nd_bind_oneSc capRead] + apply mem_bindA.2 + refine ⟨(.ok (), σ2), ?_, rfl⟩ + rw [run_bind_okA hc1] + dsimp only [absCapA] + rw [if_pos (by simp [AllowRecv, rightsOfA])] + simp only [Bool.false_eq_true, ↓reduceIte] + rw [run_bind_okA (run_returnOkA _ _)] + unfold NonDetE.liftE + rw [ExceptT.run_lift] + exact mem_bindA.2 ⟨((), σ2), hrecv, rfl⟩ + -- P1's walk + obtain ⟨Pt, Ub, Pp7, Tb7, Hr, Te, Kt, Lv7, Re, bits, KLv, W⟩ := J7A.unp J.j7 + have I := J.delIn hA hPl W + have h : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s := by + obtain ⟨q, b, x, sg, sc⟩ := J.sys.ent.bxi + exact ⟨q, b, x, sc, sg⟩ + have hext : ∀ z ∈ (Aeneas.Std.Array.repeat 3#usize 0#u64).val, z ≠ 0#u64 → z ∈ OS := fun z hz hne => by + exact absurd (by simpa [Aeneas.Std.Array.repeat] using hz) hne + have D := J.decPreA (extra := Aeneas.Std.Array.repeat 3#usize 0#u64) hslot hext + have Pz := J.toJStage3A.sysPlace hext hslot hcov + have hHe : H pe = some .ep := J.utIpc.ep pe hpe + have regs : RpPost st H Z.Cs OS Z.V ia ie Ac s s2 := by + have walk := Fl4ProofsA64.P1.W7.R.ST.receive_ipc.rpw (st := st) (ar := ar) hA a t + (.Endpoint pe badge send true grant grantReply) blk .Null pe I J.core0 J.ko Pz + (SW.start J.sys.sysl J.symL h) D.cb + (by rcases J.runW with e | e | e <;> rw [e] <;> + simp [sched.TS_RUNNING, sched.TS_RESTART, sched.TS_RUNNING_VM, sched.TS_INACTIVE]) + rfl hHe + (fun k hk' => by + rw [hHe] at hk' + cases hk' + exact ⟨slot, hslot, by rw [hsc]; exact names_ep⟩) + (fun _ _ c => by cases c) + (J.tcbs ▸ J.runVcpu) J.nvm + exact (post_of_okA (P := fun q => RpW st H Z.Cs OS Z.V ia ie Ac s q.1) walk hipc).post + obtain ⟨bxi, sysl, symL, userOK, j7⟩ := J.toJStage3A.finRpA hA hPl rfl regs σ2 + have hc : m.curCore < fl4A64Platform.numNodes := by rw [J.j.1.sch.core]; exact hCs _ J.j.1.sch.cpuCs + obtain ⟨Q, Rl, J1⟩ := J.toJStage3A.of_badgeSwA hA hPl rfl hc hmb hmax htu J.body.sw J.noSelf htZ hkt v + rw [hb0, ← hs1] at J1 + have hk1 : σ1.kheap (specPtr p) = some (.notification x) := by + show Function.update σ.kheap _ _ (specPtr p) = _ + rw [Function.update_of_ne (Ne.symm tp)]; exact hk + have rv : RunVcpuInv st Z.T s2 := fun u hu hne => by + have keep : ∀ O : U64, O.val < 4096 → O.val % 8 = 0 → L.mem s2 (wa u O) = L.mem s1 (wa u O) := fun O h1 h2 => by + have hua := J.sr.core.tcbs u hu + have := hua.fits + refine w2.mem_eq (field_aligned hua h1 h2) fun c => ?_ + have e := congrArg (fun w : U64 => w.val) c + have ev : (wa u O).val = u.val + O.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have := hP.n.disj p hp u hu + simp only [ev] at e + omega + have e1' : L.mem s2 (runVcpuA u) = L.mem s1 (runVcpuA u) := keep _ (by simp) (by simp) + have e2' : L.mem s2 (stA u) = L.mem s1 (stA u) := by rw [stA_eq]; exact keep _ (by simp) (by simp) + rw [e2'] + exact J1.runVcpu u hu (e1' ▸ hne) + have R' : NtfnStateRel N Z.T (L.mem s2) σ2 := by + rw [hs2', hs1, ← hbn']; exact hR2 + obtain ⟨Q2, Rl2, J2⟩ := J1.of_ntfnObjA hc hmb hmax htu hp hk1 (fun q c => by rw [hact] at c; cases c) + (x' := ntfnObjUpdate (fun _ => .idleNtfn) x) rfl rfl rfl + (w2.mono fun b c => by subst c; exact ⟨htsv.1, by have := htsv.2; unfold NObjW at *; omega⟩) + R' bxi rv sysl symL userOK j7 + refine ⟨s2, a, σ2, hfl, hmem, ⟨xt, hkc, by rw [ct]⟩, A64RelS3.ofJ J2 ?bo, curW_keptA J.toJStage3A J2 rfl, ?bo⟩ + refine J.body.stepEq (σ' := σ2) ((J.swBadgeA hkt b0).updOther (p := specPtr p) + (by rw [updK_kheap, Function.update_of_ne (Ne.symm tp)]; exact hk) (fun _ c => by cases c) (fun _ _ c => by cases c)) rfl rfl rfl rfl rfl rfl (fun y' k' hz => ?_) (fun _ z hz => ?_) + · rw [hy2] at hz + cases hz + exact ⟨y, hy, rfl, rfl, rfl⟩ + · change σ2.kheap σ.curThread = some (.tcb z) at hz + rw [ct, ht2] at hz + cases hz + show xt.tcbState = .running ∨ xt.tcbState = .restart + exact J.state hkt + +set_option maxHeartbeats 1000000 in +/-- **`RecvPreA.recvBoundActiveA` at a syscall entry.** -/ +theorem JSyscallA.recvBoundActiveA + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {pe badge slot : U64} {send grant grantReply blk : Bool} + (hcap : cspace.lookup_cap st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.CAP)) = + ok (.Ok (.Endpoint pe badge send true grant grantReply))) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) + (hsc : capA st s slot = .Endpoint pe badge send true grant grantReply) + (hpe : pe ∈ E) {xe : Endpoint} (hke : σ.kheap (specPtr pe) = some (.endpoint xe)) + {p : U64} (hp : p ∈ N) {x : Notification} (hk : σ.kheap (specPtr p) = some (.notification x)) + {b0 : Badge} (hact : x.ntfnObj = .activeNtfn b0) + (hbn : ∀ y, σ.kheap σ.curThread = some (.tcb y) → y.tcbBoundNotification = some (specPtr p)) : + HRecvPostA st AR ar hPl.ipa n Z.Cs s a m blk false σ + (fun σ' => ∃ y, σ.kheap σ.curThread = some (.tcb y) ∧ + σ' = rsPreσA σ σ.curThread (specPtr p) y x b0) := + RecvPreA.recvBoundActiveA hA hPl J.toRecvPreA hCs hmb hmax htu hcap hslot hcov hsc hpe hke hp hk hact hbn (Or.inl fun x hx => by obtain ⟨y, hy, e, -⟩ := J.curInv.tcb; rw [hx] at hy; cases hy; exact e) + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3RecvBoundDonA.lean b/proofs/Fl4ProofsA64/P3/Stage3RecvBoundDonA.lean new file mode 100644 index 000000000..1020edd3d --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3RecvBoundDonA.lean @@ -0,0 +1,451 @@ +/- +P3 (aarch64, A64-P3 Stage 3, RecvPreA line): **`Recv` on an endpoint with an active bound notification, by a passive +caller**: `complete_signal`'s tail may donate the notification's SC to the caller (`complete_tail_frameA`); the record +by `JStage3A.of_recvDonA`. The twin of `waitActiveDonA`. + +* **`completeTail_curKeepA`**: across the spec's complete tail the budget scalars and the current SC's object are kept, + when the current SC is not the notification's. +* **`RecvPreA.recvBoundActiveDonA`** (any `blocking`, `can_reply = false`): the post-state is that of `specCompleteTail`. + +Hypotheses beyond `recvBoundActiveA`'s: `hpas`, [S] `hne` (a scope selector: `ntfnSc = curSc` is reachable and belongs +to the scope table), `hdl` (`DonLocalA`), as `waitActiveDonA`; they replace `hown`. +[P] as `recvBoundActiveA`. New module. +-/ +import Fl4ProofsA64.P3.Stage3RecvWaitDonA +import Fl4ProofsA64.P3.Stage3RecvBoundA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec Classical Fl4ProofsA64.P3 + +/-- **The complete tail keeps the scalars and the current SC's object** (module docstring). -/ +theorem completeTail_curKeepA (t np : ObjRef) {σ σ' : State Unit} {y : Notification} + (hkn : σ.kheap np = some (.notification y)) {y0 : SchedContext} {n0 : Nat} + (hc : σ.kheap σ.curSc = some (.schedContext y0 n0)) (hne : ∀ q, y.ntfnSc = some q → q ≠ σ.curSc) + (hr : ((), σ') ∈ (specCompleteTailA t np σ).1) : BScalA σ σ' ∧ σ'.kheap σ.curSc = σ.kheap σ.curSc := by + unfold specCompleteTailA at hr + obtain ⟨u, hu, hr⟩ := mem_bindA.1 hr + have D := maybeDonateSc_keepScA t np σ.curSc hkn hne hc hu + obtain ⟨o, -, hr⟩ := eqPres_mem (threadGet_eqPres _ t) hr + have e : u.2.curSc = σ.curSc := D.1.csc + have refl : ((), σ') = ((), u.2) → BScalA σ σ' ∧ σ'.kheap σ.curSc = σ.kheap σ.curSc := fun h => by + cases h; exact D + unfold completeSpecBodyA at hr + cases o with + | none => exact refl hr + | some p => + simp only [NonDet.maybeM] at hr + obtain ⟨⟨sc, σ1⟩, h1, hr⟩ := mem_bindA.1 hr + have e1 : σ1 = u.2 := (getSchedContext_mem h1).1 + rw [e1] at hr + unfold NonDet.when at hr + split at hr + · obtain ⟨q, -, hr⟩ := eqPres_mem (by unfold getNtfnObjRef getSkObjRef; eqpres) hr + rw [nd_gets_bindSc] at hr + split at hr + · rename_i c + have K := refillUnblockCheck_onlyA u.2 _ hr + rw [← e] + refine ⟨D.1.trans K.toBScalA, (K.other _ (Ne.symm c.2)).trans ?_⟩ + rw [e]; exact D.2 + · exact refl hr + · exact refl hr + +end Fl4ProofsA64.P3.Sched + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4Spec Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-- The two ports of the complete tail are one program (as `specRecvTail_eqA`). -/ +theorem specCompleteTail_eqA : specCompleteTail = specCompleteTailA := rfl + +section + +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {s : S} {a : A} {σ : State Unit} {m : McState} + +set_option maxHeartbeats 4000000 in +/-- **`Recv` with an active bound notification, the caller passive** (module docstring). -/ +theorem RecvPreA.recvBoundActiveDonA + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {pe badge slot : U64} {send grant grantReply blk : Bool} + (hcap : cspace.lookup_cap st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.CAP)) = + ok (.Ok (.Endpoint pe badge send true grant grantReply))) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) + (hsc : capA st s slot = .Endpoint pe badge send true grant grantReply) + (hpe : pe ∈ E) {xe : Endpoint} (hke : σ.kheap (specPtr pe) = some (.endpoint xe)) + {p : U64} (hp : p ∈ N) {x : Notification} (hk : σ.kheap (specPtr p) = some (.notification x)) + {b0 : Badge} (hact : x.ntfnObj = .activeNtfn b0) + (hbn : ∀ y, σ.kheap σ.curThread = some (.tcb y) → y.tcbBoundNotification = some (specPtr p)) + (hpas : ∀ y, σ.kheap σ.curThread = some (.tcb y) → y.tcbSchedContext = none) + (hne : x.ntfnSc ≠ some σ.curSc) (hdl : DonLocalA (st := st) Z.C s p) : + HRecvPostA st AR ar hPl.ipa n Z.Cs s a m blk false σ + (fun σ' => ∃ y, σ.kheap σ.curThread = some (.tcb y) ∧ + ((), σ') ∈ (specCompleteTail σ.curThread (specPtr p) (rsPreσA σ σ.curThread (specPtr p) y x b0)).1) := by + have htZ : L.curW s ∈ Z.T := J.sr.curT + set t := L.curW s with ht_def + have ct : σ.curThread = specPtr t := J.j.1.sch.curThread + obtain ⟨xt, hkt, r⟩ := J.ep.tcb t htZ + have hkc : σ.kheap σ.curThread = some (.tcb xt) := by rw [ct]; exact hkt + have hthT : Z.th t = some .tcb := (J.sr.thT t).2 htZ + have hP := J.iplaced + have hx := J.ntfn.at_ntfn hp hk + have hnbd := hbn xt hkc + obtain ⟨hp0, hpw⟩ := optRef_some (r.bound.symm.trans hnbd) + have hbound : L.mem s (tBoundA t) = p := specPtr_inj hpw + have hpn0 : p ≠ 0#u64 := hbound ▸ hp0 + set v := L.mem s (nBadgeA p) with hv + have hb0 : v.bv = b0 := by + have := hx.obj; rw [hact] at this; exact this.2.2 + set s1 := L.wr s (Endp.regA t layout.reg.BADGE) v with hs1 + set s2 := rsPreA st s t p with hs2 + set σ1 := updK σ (specPtr t) (.tcb (setReg xt badge_register b0)) with hσ1 + set σ2 := rsPreσA σ (specPtr t) (specPtr p) xt x b0 with hσ2 + have hta := J.sr.core.tcbs t htZ + have hbn' : badgeRegA t = Endp.regA t layout.reg.BADGE := by + apply UScalar.eq_of_val_eq + have := hta.fits + have h0 : (layout.reg.BADGE).val = 0 := by simp [layout.reg.BADGE, layout.reg.X0] + rw [badgeRegA_eq, Endp.regA_val (hP.e.tcb t htZ) (by rw [h0]; decide), h0, wa_valm, + Nat.mod_eq_of_lt (by simp; omega)] + simp + obtain ⟨e1, e2, hR2, hW2, -⟩ := recv_active_prefix (st := st) hP.n J.ntfn htZ hp hk hact hkt + -- the FL4 writes + have hs2' : s2 = L.wr s1 (epTSA p) ((L.mem s1 (epTSA p) &&& ~~~3#u64) ||| (obj.NTFN_IDLE &&& 3#u64)) := by + rw [hs2, rsPreA, hbn'] + have hset : obj.Ntfn.set_state st p s1 obj.NTFN_IDLE = ok s2 := by + obtain ⟨q, hq, e⟩ := spec_existsA (obj.Ntfn.set_state.spec (st := st) s1 p obj.NTFN_IDLE) + rw [hq, e, ← hs2'] + have hbv : (Endp.regA t layout.reg.BADGE).val = t.val + 256 := by + have h0 : (layout.reg.BADGE).val = 0 := by simp [layout.reg.BADGE, layout.reg.X0] + rw [Endp.regA_val (hP.e.tcb t htZ) (by rw [h0]; decide), h0] + have hbal : Aligned (Endp.regA t layout.reg.BADGE) := by + have e : Endp.regA t layout.reg.BADGE = wa t 256#u64 := by + rw [← hbn', badgeRegA_eq] + rw [e]; exact field_aligned hta (by simp) (by simp) + have htsal : Aligned (epTSA p) := (foot_ts hP.n hp).1 + have htsv : p.val ≤ (epTSA p).val ∧ (epTSA p).val < p.val + 40 := by + rw [epTSA_eq, nfield_val (hP.n.ntfn p hp) (by simp)] + simp + have w1 : WritesIn st s s1 (fun b => b = Endp.regA t layout.reg.BADGE) := writesIn_wr hbal + have w2 : WritesIn st s1 s2 (fun b => b = epTSA p) := by + rw [hs2']; exact writesIn_wr htsal + have w : WritesIn st s s2 (fun b => b = Endp.regA t layout.reg.BADGE ∨ b = epTSA p) := + w1.trans w2 + have htH : t ∈ H.tcbs := by rw [J.tcbs]; exact htZ + have hH : H p = some .ntfn := J.utIpc.ntfn p hp + -- the spec objects + have tp : specPtr t ≠ specPtr p := fun e => by rw [e, hk] at hkt; cases hkt + obtain ⟨y, k, hy⟩ := J.sinv.curSc + have sct : σ.curSc ≠ specPtr t := fun e => by rw [e, hkt] at hy; cases hy + have scp : σ.curSc ≠ specPtr p := fun e => by rw [e, hk] at hy; cases hy + have hy2 : σ2.kheap σ.curSc = some (.schedContext y k) := by + show Function.update (Function.update σ.kheap _ _) _ _ σ.curSc = _ + rw [Function.update_of_ne scp, Function.update_of_ne sct]; exact hy + have ht2 : σ2.kheap (specPtr t) = some (.tcb (setReg xt badge_register b0)) := by + show Function.update (Function.update σ.kheap _ _) _ _ (specPtr t) = _ + rw [Function.update_of_ne tp, Function.update_self] + have hk2 : σ2.kheap (specPtr p) = some (.notification (ntfnObjUpdate (fun _ => .idleNtfn) x)) := by + show Function.update (Function.update σ.kheap _ _) _ _ (specPtr p) = _ + rw [Function.update_self] + -- the record at the badge write, and across the notification write + have hc : m.curCore < fl4A64Platform.numNodes := by rw [J.j.1.sch.core]; exact hCs _ J.j.1.sch.cpuCs + obtain ⟨Q, Rl, J1⟩ := J.toJStage3A.of_badgeSwA hA hPl rfl hc hmb hmax htu J.body.sw J.noSelf htZ hkt v + rw [hb0, ← hs1] at J1 + have hk1 : σ1.kheap (specPtr p) = some (.notification x) := by + show Function.update σ.kheap _ _ (specPtr p) = _ + rw [Function.update_of_ne (Ne.symm tp)]; exact hk + have rv : RunVcpuInv st Z.T s2 := fun u hu hne => by + have keep : ∀ O : U64, O.val < 4096 → O.val % 8 = 0 → L.mem s2 (wa u O) = L.mem s1 (wa u O) := fun O h1 h2 => by + have hua := J.sr.core.tcbs u hu + have := hua.fits + refine w2.mem_eq (field_aligned hua h1 h2) fun c => ?_ + have e := congrArg (fun w : U64 => w.val) c + have ev : (wa u O).val = u.val + O.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have := hP.n.disj p hp u hu + simp only [ev] at e + omega + have e1' : L.mem s2 (runVcpuA u) = L.mem s1 (runVcpuA u) := keep _ (by simp) (by simp) + have e2' : L.mem s2 (stA u) = L.mem s1 (stA u) := by rw [stA_eq]; exact keep _ (by simp) (by simp) + rw [e2'] + exact J1.runVcpu u hu (e1' ▸ hne) + have R' : NtfnStateRel N Z.T (L.mem s2) σ2 := by + rw [hs2', hs1, ← hbn']; exact hR2 + have wp : WritesIn st s1 s2 (NObjW p) := + w2.mono fun b c => by subst c; exact ⟨htsv.1, by have := htsv.2; unfold NObjW at *; omega⟩ + obtain ⟨⟨Q1, Rl1, sr2u⟩, -, -, -, ep2, -, -, -, sinv2, sidle2, -, sv2⟩ := + J1.ntfnObj_factsA hc hmb hmax htu hp hk1 (fun q c => by rw [hact] at c; cases c) + (x' := ntfnObjUpdate (fun _ => .idleNtfn) x) rfl rfl rfl wp R' + -- FL4's tail + have sr2 : SchedSRA st { { Z with Q := Q, R := Rl } with Q := Q1, R := Rl1 } m κ s2 σ2 := sr2u + have c2 : σ2.curThread = specPtr t := ct + have hcw : L.curW s2 = t := specPtr_inj (sr2.sch.curThread.symm.trans c2) + have hon : L.mem s2 (affA t) = L.cpu s2 := by + have := sr2.curOn; rwa [hcw] at this + have hrun2 : ∀ z, σ2.kheap (specPtr t) = some (.tcb z) → runnable z.tcbState = true := fun z hz => by + cases KernelObject.tcb.inj (Option.some.inj (ht2.symm.trans hz)) + rw [setReg_state] + rcases J.state hkt with e | e <;> rw [e] <;> rfl + have vS : (nScSc p).val = p.val + 32 := by + show (Notif.nScA p).val = _ + rw [nScA_eq, nfield_val (hP.n.ntfn p hp) (by simp)]; simp + have hnw : L.mem s2 (nScSc p) = L.mem s (nScSc p) := by + refine w.mem_eq (foot_sc hP.n hp).1 fun c => ?_ + rcases c with c | c + · have e := congrArg (fun w : U64 => w.val) c + simp only [vS, hbv] at e + have := hP.n.disj p hp t htZ + omega + · have e := congrArg (fun w : U64 => w.val) c + simp only [vS, epTSA_eq, nfield_val (hP.n.ntfn p hp) (K := 8#u64) (by simp)] at e + simp at e + have T := complete_tail_frameA (st := st) (ar := ar) sr2 a (hmb a) (t := t) (n := p) htZ hon + (notifScWord hp R' sr2.inv hc) (fun u hu z hz => (ep2.at_tcb hu hz).st) + (fun d pr => by rw [← ct]; exact J.noQ d pr) (by rw [← ct]; exact J.notRel) hrun2 + (J.sr.donLocalKeepA sr2 rfl hnw hdl) (by unfold Aligned; rw [vS]; have := (hP.n.ntfn p hp).align; omega) + (fun h => by + have e := sr2.scp.th_out h (J.ipcPlace.notification p hp) + change (nScSc p).val < p.val ∨ p.val + 64 ≤ (nScSc p).val at e + omega) + (fun u hu => by have := hP.n.disj p hp u hu; omega) + (by + have hN := J.ipcPlace.notification p hp + have e := sr2.place.ks p (.other 64) hN (fun h => by cases sr2.place.irqK hN h) + change p.val + 64 ≤ (L.ksb s2).val ∨ (L.ksb s2).val + ksSizeA ≤ p.val at e + omega) + obtain ⟨⟨s3, a3⟩, hq3, ha3, σ3, R3, htail, sr3, F3⟩ := spec_existsA T + dsimp only at ha3 sr3 F3 + subst a3 + have ht0 : t ≠ 0#u64 := fun e => (hP.n.tcb t htZ).ne0 (by rw [e]; rfl) + have hcs : notification.complete_signal st ar s a p t = ok (s3, a) := by + have h : notification.complete_signal st ar s a p t = rustCompleteTailA st ar s2 a t p := by + have hsa : stOf (L.mem s) p = obj.NTFN_ACTIVE := stOf_active hx.obj hact + unfold notification.complete_signal + obtain ⟨bn, en, hbn0⟩ := spec_existsA (obj.Tcb.is_null.spec t) + rw [en, bind_ok, hbn0] + simp only [ht0, decide_false, Bool.false_eq_true, ↓reduceIte] + obtain ⟨st0, e0, hst0⟩ := spec_existsA (obj.Ntfn.state.spec (st := st) s p) + rw [epState_eq, hsa] at hst0 + rw [e0, bind_ok, hst0] + simp only [bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + obtain ⟨b, eb, hb⟩ := spec_existsA (obj.Ntfn.badge.spec (st := st) s p) + obtain ⟨q, eq1, hq1⟩ := spec_existsA (obj.Tcb.set_reg.spec (st := st) s t layout.reg.BADGE b) + rw [eb, bind_ok, eq1, bind_ok, hq1, hb, ← hv, ← hs1, hset, bind_ok] + rfl + rw [h, hq3] + have hipc : endpoint.receive_ipc st ar s a t (.Endpoint pe badge send true grant grantReply) blk .Null = + ok (s3, a) := by + have h : endpoint.receive_ipc st ar s a t (.Endpoint pe badge send true grant grantReply) blk .Null ⦃ q => + q = (s3, a) ⦄ := by + rw [receive_ipc_eqA] + refine WP.spec_bind (take_reply_plainA (R := Rp) (rc' := .nullCap) + (Or.inl ⟨rfl, rfl, rfl⟩ : ReplyCapA Rp .Null .nullCap 0#u64) (fun _ h => by cases h)) ?_ + rintro q rfl + refine rcv_body_actA hpn0 hbound (stOf_active hx.obj hact) ?_ + rw [hcs]; exact (WP.spec_ok _).2 rfl + obtain ⟨q, hq, e⟩ := spec_existsA h + rw [hq, e] + have hfl : syscall.handle_recv st ar s a blk false = ok (s3, a) := by + unfold syscall.handle_recv + rw [cur_thread_ok, bind_ok, tcb_reg_eqA, bind_ok] + rw [show L.curW s = t from rfl, hcap] + simp only [bind_ok, ↓reduceIte, Bool.false_eq_true] + exact hipc + -- the spec + have hm3 : ((), σ3) ∈ (specCompleteTailA (specPtr t) (specPtr p) σ2).1 := by + rw [htail] + exact Set.mem_singleton _ + have hcomp : completeSignal (Z := Unit) (specPtr p) (specPtr t) σ = ({((), σ3)}, False) := by + unfold completeSignal + rw [nd_bind_one (getNotification_eq hk)] + simp only [hact] + rw [nd_bind_one e1, nd_bind_one e2] + exact htail + have hrecv : ((), σ3) ∈ (receiveIpc (Z := Unit) (specPtr t) + (absCapA (.Endpoint pe badge send true grant grantReply)) blk .nullCap σ).1 := by + rw [show absCapA (.Endpoint pe badge send true grant grantReply) = + .endpointCap (specPtr pe) badge.bv (rightsOfA send true grant grantReply) from rfl, + receiveIpc_bodyA _ _ blk (Or.inl ⟨rfl, rfl⟩ : SpecReplyPlainA σ (specPtr t) .nullCap none), + specBody_actA hkt hke blk none hnbd hk hact, hcomp] + exact Set.mem_singleton _ + have hmem : ((), σ3) ∈ (handleRecv (Z := Unit) blk false σ).1 := by + have capReg : (L.mem s (regWA t layout.reg.CAP)).bv = xt.tcbArch.tcbContext.userRegs cap_register := by + rw [← r.regs cap_register, regIdxA_capA]; rfl + have capRead : NonDet.liftM dataToCptr (asUser (Z := Unit) (specPtr t) (getRegister cap_register)) σ = + ({(toBl (L.mem s (regWA t layout.reg.CAP)).bv, σ)}, False) := by + unfold NonDet.liftM + rw [nd_bind_singleA' (asUser_getRegister_eqA hkt _), ← capReg] + rfl + have hc0 : (lookupCap (Z := Unit) (specPtr t) (toBl (L.mem s (regWA t layout.reg.CAP)).bv)).run σ = + ({(.ok (absCapA (.Endpoint pe badge send true grant grantReply)), σ)}, False) := by + have l := post_of_okA (lookup_cap_recv_refinesA (st := st) _ s σ J.j.1.st J.j.1.cv t hthT _) hcap + obtain ⟨e, he, hrel⟩ := l + cases e with + | error _ => exact hrel.elim + | ok ac => + obtain rfl : absCapA (.Endpoint pe badge send true grant grantReply) = ac := hrel.2 + exact he + have hc1 : (capFaultOnFailure (ofBl (toBl (L.mem s (regWA t layout.reg.CAP)).bv)) true + (lookupCap (Z := Unit) (specPtr t) (toBl (L.mem s (regWA t layout.reg.CAP)).bv))).run σ = + ({(.ok (absCapA (.Endpoint pe badge send true grant grantReply)), σ)}, False) := run_handleE_okA hc0 + rw [handleRecv, nd_gets_bindSc, ct, nd_bind_oneSc capRead] + apply mem_bindA.2 + refine ⟨(.ok (), σ3), ?_, rfl⟩ + rw [run_bind_okA hc1] + dsimp only [absCapA] + rw [if_pos (by simp [AllowRecv, rightsOfA])] + simp only [Bool.false_eq_true, ↓reduceIte] + rw [run_bind_okA (run_returnOkA _ _)] + unfold NonDetE.liftE + rw [ExceptT.run_lift] + exact mem_bindA.2 ⟨((), σ3), hrecv, rfl⟩ + -- P1's walk + obtain ⟨Pt, Ub, Pp7, Tb7, Hr, Te, Kt, Lv7, Re, bits, KLv, W⟩ := J7A.unp J.j7 + have I := J.delIn hA hPl W + have h : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s := by + obtain ⟨q, b, x, sg, sc⟩ := J.sys.ent.bxi + exact ⟨q, b, x, sc, sg⟩ + have hext : ∀ z ∈ (Aeneas.Std.Array.repeat 3#usize 0#u64).val, z ≠ 0#u64 → z ∈ OS := fun z hz hne => by + exact absurd (by simpa [Aeneas.Std.Array.repeat] using hz) hne + have D := J.decPreA (extra := Aeneas.Std.Array.repeat 3#usize 0#u64) hslot hext + have Pz := J.toJStage3A.sysPlace hext hslot hcov + have hHe : H pe = some .ep := J.utIpc.ep pe hpe + have regs : RpPost st H Z.Cs OS Z.V ia ie Ac s s3 := by + have walk := Fl4ProofsA64.P1.W7.R.ST.receive_ipc.rpw (st := st) (ar := ar) hA a t + (.Endpoint pe badge send true grant grantReply) blk .Null pe I J.core0 J.ko Pz + (SW.start J.sys.sysl J.symL h) D.cb + (by rcases J.runW with e | e | e <;> rw [e] <;> + simp [sched.TS_RUNNING, sched.TS_RESTART, sched.TS_RUNNING_VM, sched.TS_INACTIVE]) + rfl hHe + (fun k hk' => by + rw [hHe] at hk' + cases hk' + exact ⟨slot, hslot, by rw [hsc]; exact names_ep⟩) + (fun _ _ c => by cases c) + (J.tcbs ▸ J.runVcpu) J.nvm + exact (post_of_okA (P := fun q => RpW st H Z.Cs OS Z.V ia ie Ac s q.1) walk hipc).post + obtain ⟨bxi, sysl, symL, userOK, j7⟩ := J.toJStage3A.finRpA hA hPl rfl regs σ3 + -- the spec invariants and the record at the end + have eT : donTailA (specPtr t) (specPtr p) (fun o => completeSpecBodyA o (specPtr p)) = + specCompleteTailA (specPtr t) (specPtr p) := rfl + have hm3' : ((), σ3) ∈ (donTailA (specPtr t) (specPtr p) + (fun o => completeSpecBodyA o (specPtr p)) σ2).1 := by rw [eT]; exact hm3 + have hnt : specPtr t ≠ σ.idleThread := J.notIdle + have hnr2 : specPtr t ∉ σ2.releaseQueue := by rw [← ct]; exact J.notRel + have sact2 : SActA σ2 := by + refine ⟨fun u e => ⟨fun h => J.noSelf (h ▸ e), fun h => ?_⟩, + ⟨_, by rw [show σ2.curThread = specPtr t from c2]; exact ht2, ?_⟩⟩ + · have e' : σ.schedulerAction = .switchThread u := e + obtain ⟨-, -, x0, e0, hr0⟩ := schedulable_actA (J.body.sw u e') + obtain ⟨z, hz, hs⟩ := J.sinv.idleTcb + rw [show u = σ.idleThread from h, hz] at e0 + cases e0 + rw [hs] at hr0 + cases hr0 + · rcases J.state hkt with e | e + · exact Or.inl e + · exact Or.inr (Or.inl e) + have hkT : ∀ o, PresR RefK (completeSpecBodyA o (specPtr p)) := fun o => completeSpecBodyA_refK o _ + have hkP : ∀ o, TPres (completeSpecBodyA o (specPtr p)) := fun o => by + unfold completeSpecBodyA getNtfnObjRef getSkObjRef + cases o <;> simp only [NonDet.maybeM] <;> tpres + have L2 : LoopInvA σ2 m n := ⟨sinv2, sidle2, sact2⟩ + have hnt2 : specPtr t ≠ σ2.idleThread := hnt + have hn2 : ∀ sc, x.ntfnSc = some sc → sc ≠ idleScPtr := fun sc h e => by subst e; exact J.sr.inv.ntfnIdle (specPtr p) x hk h + have hs2 : x.ntfnSc = some σ2.curSc → ∀ y k, σ2.kheap σ2.curSc = some (.schedContext y k) → scActive y = true → refillSufficient 0 (refillHd y) = true := fun h => absurd h hne + have hL : LoopInvA σ3 m n := donTail_loopA (σ := σ2) (specPtr t) (specPtr p) hkT L2 hnt2 hk2 hn2 hnr2 hs2 hm3' + have rq2 : RqDomPrio σ2 := fun f d pr z hz hm => by + have h0 := J.toJStage3A.rqDomPrio + change Function.update (Function.update σ.kheap (specPtr t) _) (specPtr p) _ f = _ at hz + by_cases e1 : f = specPtr p + · rw [e1, Function.update_self] at hz; cases hz + rw [Function.update_of_ne e1] at hz + by_cases e2 : f = specPtr t + · rw [e2, Function.update_self] at hz + cases hz + exact h0 f d pr xt (by rw [e2]; exact hkt) hm + · rw [Function.update_of_ne e2] at hz + exact h0 f d pr z hz hm + have hts : BlockedTsA xt.tcbState = false := by + rcases J.state hkt with e | e <;> rw [e] <;> rfl + have F2 : FIA σ2 := SchedValidA.toFIA sv2 + have hst2 : StIs (specPtr t) xt.tcbState σ2 := + fun z hz => by cases KernelObject.tcb.inj (Option.some.inj (ht2.symm.trans hz)); rfl + have fia : FIA σ3 := donTail_fiaA (σ := σ2) (σ' := σ3) (specPtr t) (specPtr p) hkP F2 rq2 hts hst2 hm3' + have sv3 : SchedValidA σ3 := SchedValidB.toA fia.1 fia.2 + have SF : SchedFrameA σ2 σ3 := by + have P : PresR SchedFrameA (specCompleteTailA (specPtr t) (specPtr p)) := by + unfold specCompleteTailA; spres + exact P σ2 _ hm3 + have Qk : QKeepA σ2 σ3 := by + have P : PresR QKeepA (specCompleteTailA (specPtr t) (specPtr p)) := by + unfold specCompleteTailA; qpres + exact P σ2 _ hm3 + have Wk : WkKeepA σ2 σ3 := by + have P : PresR WkKeepA (specCompleteTailA (specPtr t) (specPtr p)) := by + unfold specCompleteTailA; wkpres + exact P σ2 _ hm3 + have J3 := J1.of_recvDonA hc hmb hmax htu hp hk1 (fun q c => by rw [hact] at c; cases c) + (x' := ntfnObjUpdate (fun _ => .idleNtfn) x) rfl rfl rfl wp R' F3 sr3 SF Qk Wk hL sv3 rv bxi sysl symL + userOK j7 + -- the body end + have hpas2 := J.passive_noResumeA hpas + have B2 : BodyOKA σ2 m := by + refine J.body.stepEq (σ' := σ2) ((J.swBadgeA hkt b0).updOther (p := specPtr p) + (by rw [updK_kheap, Function.update_of_ne (Ne.symm tp)]; exact hk) (fun _ c => by cases c) + (fun _ _ c => by cases c)) rfl rfl rfl rfl rfl rfl (fun y' k' hz => ?_) (fun h => absurd h hpas2) + rw [hy2] at hz + cases hz + exact ⟨y, hy, rfl, rfl, rfl⟩ + have swn : SwN (specPtr t) σ2 := fun u e => by + refine ⟨B2.sw u e, fun h => J.noSelf ?_⟩ + rw [ct, ← h]; exact e + have keep := completeTail_curKeepA (specPtr t) (specPtr p) hk2 (y0 := y) (n0 := k) hy2 + (fun q h e => hne (h.trans (congrArg some e))) hm3 + have hown : ∀ y0 k0, σ.kheap σ.curSc = some (.schedContext y0 k0) → ∀ o, y0.scTcb = some o → o ≠ specPtr t := by + intro y0 k0 h0 o ho e + have hcs : σ.curSc = scRenA m (L.mem s (L.nodeAt s (L.cpu s) layout.node.CUR_SC)) := by + rw [← J.sr.sch.csc, J.sr.sch.core]; exact J.sr.sch.curScOfW _ J.sr.sch.cpuCs + obtain ⟨z, hz, hzs⟩ := J.toJStage3A.scBoundA J.sr.cscC (y := y0) (k := k0) (o := o) (by rw [← hcs]; exact h0) ho + rw [hpas z (by rw [ct, ← e]; exact hz)] at hzs + cases hzs + have B3 : BodyOKA σ3 m := by + refine B2.step (σ' := σ3) (donTail_swA (specPtr t) (specPtr p) hkP swn hm3') keep.1.time + (by rw [keep.1.ms]) keep.1.ct keep.1.csc keep.1.used (fun y0 k0 h0 o ho hm => ?_) (fun y' k' hz => ?_) + (fun h => absurd (keep.1.act h) hpas2) (fun h => absurd (keep.1.act h) hpas2) + · have h0' : σ.kheap σ.curSc = some (.schedContext y0 k0) := by + rw [show σ2.curSc = σ.curSc from rfl, hy2] at h0 + rw [hy]; exact h0 + by_contra c + exact donTail_notRelA (c := o) (t := specPtr t) (Ne.symm (hown y0 k0 h0' o ho)) (specPtr p) hkP c hm3' hm + · have k2 : σ3.kheap σ.curSc = σ2.kheap σ.curSc := keep.2 + rw [show σ2.curSc = σ.curSc from rfl] at hz ⊢ + rw [k2] at hz + exact ⟨y', hz, rfl, rfl, Or.inl rfl⟩ + have hOut : ∃ y, σ.kheap σ.curThread = some (.tcb y) ∧ + ((), σ3) ∈ (specCompleteTail σ.curThread (specPtr p) (rsPreσA σ σ.curThread (specPtr p) y x b0)).1 := by + have e : rsPreσA σ σ.curThread (specPtr p) xt x b0 = σ2 := by rw [ct] + refine ⟨xt, hkc, ?_⟩ + rw [e, ct, specCompleteTail_eqA] + exact hm3 + have hRel := A64RelS3.ofJ J3 B3 + have hcw := curW_keptA J.toJStage3A J3 keep.1.ct + exact ⟨s3, a, σ3, hfl, hmem, hOut, hRel, hcw, B3⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3RecvBufA.lean b/proofs/Fl4ProofsA64/P3/Stage3RecvBufA.lean new file mode 100644 index 000000000..99f16c972 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3RecvBufA.lean @@ -0,0 +1,214 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2b-2): **a TCB's IPC buffer lies in a user frame, from the record** (the `hu` of +the transfer theorems: `send_ipc_recvA`, `do_ipc_transfer_anyA`, `tcbReadRegisters_callJointA`). + +* `andNotMaskA_toNat`, `maskAndA_toNat`: the page masks of the three page sizes, as numbers. +* **`bufAddr_pageA`**: the page of word `k < 128` of a buffer `p + (w &&& mask n)` with `p` aligned to `2^n` and `w` + aligned to 1024 is `p`. +* **`JStage3A.bufFrameA`**: for a TCB of the record, every word of its IPC buffer (either lookup) is in a user frame. + Slot 2's FL4 cap is a non-device, non-EPT frame `q` (`StateRelA.tcb`, `CapRelA`); its page is a spec data page + (`userFr`), `q` is aligned to its size (J7's `S1aCapCol.fo` `FrameOK`: the heap object `H q = .other bits`, and + `HeapOK.align`), and the buffer is 1024-aligned (`bufAl`). + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3IpcGlueUA +import Fl4ProofsA64.P3.Stage3SysPlaceA +import Fl4ProofsA64.P3.Stage3SendSpecFrA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +theorem andNotMaskA_toNat (x : BitVec 64) {k : Nat} (hk : k ∈ [12, 21, 30]) : + (x &&& ~~~(Fl4SpecA64.mask k : BitVec 64)).toNat = x.toNat / 2 ^ k * 2 ^ k := by + have e : ~~~(Fl4SpecA64.mask k : BitVec 64) = BitVec.allOnes 64 <<< k := by + simp at hk; rcases hk with rfl | rfl | rfl <;> decide + rw [e, ← BitVec.shiftLeft_ushiftRight] + simp only [BitVec.toNat_shiftLeft, BitVec.toNat_ushiftRight, Nat.shiftLeft_eq, Nat.shiftRight_eq_div_pow] + rw [Nat.mod_eq_of_lt (lt_of_le_of_lt (Nat.div_mul_le_self _ _) x.isLt)] + +theorem maskAndA_toNat (x : BitVec 64) {k : Nat} (hk : k ∈ [12, 21, 30]) : + (x &&& (Fl4SpecA64.mask k : BitVec 64)).toNat = x.toNat % 2 ^ k := by + have h : (Fl4SpecA64.mask k : BitVec 64).toNat = 2 ^ k - 1 := by + simp at hk; rcases hk with rfl | rfl | rfl <;> decide + rw [BitVec.toNat_and, h, Nat.and_two_pow_sub_one_eq_mod] + +theorem pageBitsA_mem (vms : VMPageSize) : pageBitsForSize vms ∈ [12, 21, 30] := by + cases vms <;> simp [pageBitsForSize, pageBits, ptTranslationBits] + +/-- **The page of a buffer word** (module docstring). -/ +theorem bufAddr_pageA {q : U64} {n : Nat} (hn : n ∈ [12, 21, 30]) (hq : q.val % 2 ^ n = 0) (w : BitVec 64) + (hw : w.toNat % 1024 = 0) {k : Nat} (hk : k < 128) : + bufAddr (specPtr q + (w &&& Fl4SpecA64.mask n)) k &&& ~~~(Fl4SpecA64.mask n : BitVec 64) = specPtr q := by + have hn39 : n ≤ 39 := by simp at hn; omega + have hn10 : 10 ≤ n := by simp at hn; omega + apply BitVec.eq_of_toNat_eq + rw [andNotMaskA_toNat _ hn] + unfold bufAddr + rw [BitVec.toNat_add, BitVec.toNat_add, maskAndA_toNat w hn, BitVec.toNat_ofNat] + have hA : (specPtr q).toNat % 2 ^ n = 0 := (specPtr_mod q hn39).trans hq + have hAlt : (specPtr q).toNat < 2 ^ 64 := (specPtr q).isLt + have hwm : w.toNat % 2 ^ n % 1024 = 0 := by + rw [Nat.mod_mod_of_dvd _ (show 1024 ∣ 2 ^ n from Nat.pow_dvd_pow 2 hn10)]; exact hw + have hwl : w.toNat % 2 ^ n < 2 ^ n := Nat.mod_lt _ (by positivity) + obtain ⟨f, hf⟩ : 1024 ∣ 2 ^ n := Nat.pow_dvd_pow 2 hn10 + obtain ⟨e, he⟩ : 2 ^ n ∣ 2 ^ 64 := Nat.pow_dvd_pow 2 (by omega) + obtain ⟨c, hc⟩ : 2 ^ n ∣ (specPtr q).toNat := Nat.dvd_of_mod_eq_zero hA + have hPpos : 0 < 2 ^ n := by positivity + have hk8 : k * wordSize < 1024 := by unfold wordSize; omega + have h64 : (2 : Nat) ^ 64 = 18446744073709551616 := by norm_num + rw [h64] at hAlt he ⊢ + generalize (specPtr q).toNat = A at * + generalize w.toNat % 2 ^ n = o at * + generalize 2 ^ n = P at * + generalize k * wordSize = j at * + subst hc + have hce : c < e := Nat.lt_of_mul_lt_mul_left (a := P) (by omega) + have hle : P * c + P ≤ P * e := by rw [← Nat.mul_succ]; exact Nat.mul_le_mul_left _ hce + have hoj : o + j < P := by omega + rw [Nat.mod_eq_of_lt (show P * c + o < 18446744073709551616 by omega), + Nat.mod_eq_of_lt (show j < 18446744073709551616 by omega), + Nat.mod_eq_of_lt (show P * c + o + j < 18446744073709551616 by omega), Nat.add_assoc, + Nat.mul_add_div hPpos, Nat.div_eq_of_lt hoj, Nat.add_zero, Nat.mul_comm] + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {s : S} {a : A} {σ : State Unit} {m : McState} + +set_option maxHeartbeats 1000000 in +/-- **The frame of a record TCB's IPC buffer** (module docstring): a heap object `H q = .other n` aligned to `2^n`, a +spec data page of that size, and the buffer at `specPtr q + (tcbIpcBuffer &&& mask n)`. -/ +theorem JStage3A.bufFrameObjA {R7 : Set U64 → JSlotA S} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st R7) s a σ m) + {d : U64} (hd : d ∈ Z.T) {xd : Tcb} (hkd : σ.kheap (specPtr d) = some (.tcb xd)) (b : Bool) {rp : ObjRef} + (hrp : ipcBufOfA b xd = some rp) : + ∃ q vms, H q = some (.other (pageBitsForSize vms)) ∧ q.val % 2 ^ pageBitsForSize vms = 0 ∧ + rp = specPtr q + (xd.tcbIpcBuffer &&& Fl4SpecA64.mask (pageBitsForSize vms)) ∧ + σ.kheap (specPtr q) = some (.archObj (.dataPage false vms)) := by + have hth : Z.th d = some .tcb := (J.sr.thT d).2 hd + obtain ⟨x', hk', hs⟩ := J.j.1.st.tcb d hth + rw [hkd] at hk' + cases hk' + obtain ⟨ac, hac, hca⟩ := hs 2 (by decide) + rw [tcbCnodeMap_twoA] at hac + cases hac + have hyOS : slotAddrA (specPtr d) (BitVec.ofNat 64 2) ∈ OS := + J.j5.2.1 _ ⟨d, Or.inr ⟨hth, 2, by decide, rfl⟩⟩ + rw [capAtA_eq] at hca + obtain ⟨-, hab⟩ := hca + generalize hc : capA st s (slotAddrA (specPtr d) (BitVec.ofNat 64 2)) = c at hab + cases c with + | Frame q sz r dev mp ept asid va => + replace hab : absCapA (.Frame q sz r dev mp ept asid va) = xd.tcbIpcframe := hab + cases dev + · cases ept + · have erp : rp = specPtr q + (xd.tcbIpcBuffer &&& Fl4SpecA64.mask (pageBitsForSize (frameSizeOfA sz))) := by + unfold ipcBufOfA at hrp + rw [← hab] at hrp + simp only [absCapA, absFrameE, Bool.false_eq_true, ↓reduceIte] at hrp + split_ifs at hrp + exact (Option.some.inj hrp).symm + have hpg := J.userFr _ hyOS q sz r mp false asid va hc + obtain ⟨Pt, Ub, -, -, -, cc, -⟩ := J.j7 + have hfo := cc.fo _ hyOS q sz r mp false asid va hc + have hH : H q = some (.other (pageBitsForSize (frameSizeOfA sz))) := by + cases sz + · exact hfo 12#u64 rfl + · exact hfo 21#u64 rfl + · exact hfo 30#u64 rfl + exact ⟨q, frameSizeOfA sz, hH, J.sys.ent.heap.align q _ hH, erp, hpg⟩ + · unfold ipcBufOfA at hrp; rw [← hab] at hrp; simp [absCapA, absFrameE] at hrp + · unfold ipcBufOfA at hrp; rw [← hab] at hrp; cases ept <;> simp [absCapA, absFrameE] at hrp + | _ => + unfold ipcBufOfA at hrp + rcases CapEqA.eq_or_pt hab with e | ⟨p', a', v', e⟩ <;> rw [e] at hrp <;> simp [absCapA] at hrp + +/-- **A record TCB's IPC buffer is in a user frame** (module docstring). -/ +theorem JStage3A.bufFrameA {R7 : Set U64 → JSlotA S} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st R7) s a σ m) + {d : U64} (hd : d ∈ Z.T) {xd : Tcb} (hkd : σ.kheap (specPtr d) = some (.tcb xd)) (b : Bool) : + ∀ rp, ipcBufOfA b xd = some rp → ∀ k, k < 128 → inUserFrame (bufAddr rp k) σ := by + intro rp hrp k hk + obtain ⟨q, vms, -, hal, erp, hpg⟩ := J.bufFrameObjA hd hkd b hrp + refine ⟨vms, ?_⟩ + rw [erp, bufAddr_pageA (pageBitsA_mem _) hal _ (J.bufAl d hd xd hkd) hk] + exact hpg + +/-- The buffer's word 0 is aligned. -/ +theorem JStage3A.bufAlign0A {R7 : Set U64 → JSlotA S} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st R7) s a σ m) + {d : U64} (hd : d ∈ Z.T) {xd : Tcb} (hkd : σ.kheap (specPtr d) = some (.tcb xd)) (b : Bool) : + ∀ rp, ipcBufOfA b xd = some rp → (bufAddr rp 0).toNat % 8 = 0 := by + intro rp hrp + obtain ⟨q, vms, -, hal, erp, -⟩ := J.bufFrameObjA hd hkd b hrp + have hn := pageBitsA_mem vms + have hn39 : pageBitsForSize vms ≤ 39 := by simp at hn; omega + have hn10 : 10 ≤ pageBitsForSize vms := by simp at hn; omega + have hA : (specPtr q).toNat % 2 ^ pageBitsForSize vms = 0 := (specPtr_mod q hn39).trans hal + have h8 : 8 ∣ 2 ^ pageBitsForSize vms := Nat.pow_dvd_pow 2 (by omega : 3 ≤ pageBitsForSize vms) + have hw : (xd.tcbIpcBuffer &&& Fl4SpecA64.mask (pageBitsForSize vms)).toNat % 1024 = 0 := by + rw [maskAndA_toNat _ hn, Nat.mod_mod_of_dvd _ (show 1024 ∣ 2 ^ pageBitsForSize vms from Nat.pow_dvd_pow 2 hn10)] + exact J.bufAl d hd xd hkd + have hA8 : (specPtr q).toNat % 8 = 0 := Nat.mod_eq_zero_of_dvd (h8.trans (Nat.dvd_of_mod_eq_zero hA)) + rw [erp] + unfold bufAddr + rw [BitVec.toNat_add, BitVec.toNat_add, BitVec.toNat_ofNat] + omega + +/-- **The bytes of a buffer word lie in its frame**, under the FL4 naming. -/ +theorem fl4Ptr_bufA {q : U64} {k0 : Nat} (hn : k0 ∈ [12, 21, 30]) (hfit : q.val + 2 ^ k0 ≤ 2 ^ 64) (w : BitVec 64) + (hw : w.toNat % 1024 = 0) {k : Nat} (hk : k < 128) {y : Word} (hy : y.toNat < 8) : + q.val ≤ (fl4Ptr (bufAddr (specPtr q + (w &&& Fl4SpecA64.mask k0)) k + y)).val ∧ + (fl4Ptr (bufAddr (specPtr q + (w &&& Fl4SpecA64.mask k0)) k + y)).val < q.val + 2 ^ k0 := by + have hn10 : 10 ≤ k0 := by simp at hn; omega + have hm : (w &&& Fl4SpecA64.mask k0).toNat = w.toNat % 2 ^ k0 := maskAndA_toNat w hn + have hwm : w.toNat % 2 ^ k0 % 1024 = 0 := by + rw [Nat.mod_mod_of_dvd _ (show 1024 ∣ 2 ^ k0 from Nat.pow_dvd_pow 2 hn10)]; exact hw + obtain ⟨f, hf⟩ : 1024 ∣ 2 ^ k0 := Nat.pow_dvd_pow 2 hn10 + have hwl : w.toNat % 2 ^ k0 < 2 ^ k0 := Nat.mod_lt _ (by positivity) + have hk8 : k * wordSize + 8 ≤ 1024 := by unfold wordSize; omega + have ev : ∀ z : Fl4SpecA64.Word, (fl4Ptr z).val = (z - pptrShiftA).toNat := fun z => by rw [fl4Ptr_def]; rfl + rw [ev] + unfold bufAddr + rw [BitVec.toNat_sub, BitVec.toNat_add, BitVec.toNat_add, BitVec.toNat_add, specPtr_toNat, hm, + BitVec.toNat_ofNat, pptrShiftA_toNat] + have hq : q.val < 2 ^ 64 := q.hBounds + have h64 : (2 : Nat) ^ 64 = 18446744073709551616 := by norm_num + rw [h64] at hfit hq ⊢ + generalize w.toNat % 2 ^ k0 = o at * + generalize 2 ^ k0 = P at * + generalize k * wordSize = j at * + omega + +/-- **(D12) J10 across a step changing user memory only in a record TCB's receive buffer**: the buffer's frame is a +heap object (`bufFrameObjA`), off every free untyped part (`ObjAllocA`). -/ +theorem JStage3A.specZero_of_recvBufA {R7 : Set U64 → JSlotA S} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st R7) s a σ m) + {d : U64} (hd : d ∈ Z.T) {xd : Tcb} (hkd : σ.kheap (specPtr d) = some (.tcb xd)) {σ' : State Unit} + (hum : ∀ x, ¬ Sched.BufBytesA (ipcBufOfA true xd) x → + σ'.machineState.underlyingMemory x = σ.machineState.underlyingMemory x) : + ∀ x : Word, UtFreeA st OS s (fun d => d = false) (fl4Ptr x).val → + σ'.machineState.underlyingMemory x = σ.machineState.underlyingMemory x := by + intro x hfree + by_contra hne + have hB : Sched.BufBytesA (ipcBufOfA true xd) x := Classical.byContradiction fun h => hne (hum x h) + obtain ⟨rp, hrp, k, hk, y, hy, rfl⟩ := hB + obtain ⟨q, vms, hH, -, erp, -⟩ := J.bufFrameObjA hd hkd true hrp + obtain ⟨u, hu, qu, b, fi, dd, hc, -, f1, f2⟩ := hfree + have hO := J.objAlloc q (2 ^ pageBitsForSize vms) (Or.inr (Or.inr (Or.inr ⟨_, hH, rfl⟩))) u hu qu b fi dd hc + have hfit : q.val + 2 ^ pageBitsForSize vms ≤ 2 ^ 64 := J.sys.ent.heap.fits q _ hH + rw [erp] at f1 f2 + have hb := fl4Ptr_bufA (pageBitsA_mem vms) hfit xd.tcbIpcBuffer (J.bufAl d hd xd hkd) hk hy + omega + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3RecvCancelA.lean b/proofs/Fl4ProofsA64/P3/Stage3RecvCancelA.lean new file mode 100644 index 000000000..4b3c96577 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3RecvCancelA.lean @@ -0,0 +1,902 @@ +/- +P3 (aarch64, A64-P3 Stage 3, line e5): **`take_reply`'s cancel case of the reply-object receive arms**, case (A): the +reply object `rr` is still linked to another thread `i` that is blocked on a receive (`cancel_ipc i` of a receiver). + +* **`JStage3A.takeReply_rpA`**: P1's walk bundle across `take_reply` (`take_reply.sw`, any reply cap naming a heap reply) + as the `RpPost` the record's `finRpA` takes. This is the mid-state P1 fact that `receive_ipc.rpw` consumed internally and + did not export. +* **`JStage3A.takeReplyCancelRecvA`**: `take_reply` of a reply linked to a receive-blocked thread `i ≠ t` at the record: + the spec outcome `cancel_ipc i`, `JStage3A` at the successor (fresh queue ghosts), the write set and the frames. +* **Case (B)** (line e5c): the reply linked to a `BlockedOnReply` thread (`RecvPreA.replyLinkA`, + `JStage3A.takeReplyCancelReplyA`, `RecvPreA.cancelCoreRA`; the reply stacks shrink, SC `scReplies` words change), the + transport `CancelXA` of both cases and the dispatcher `RecvPreA.cancelCoreXA`; the block arm `recvBlockRAnyA_repA`. + +[S] none: a linked reply's thread is tracked and blocked on it by the record (`RecvPreA.replyLinkA`). [P] `ArchTotal`, `ArchPlat`, `hCs`, `hmb`, `hmax`, `htu`. No new named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3RecvFootA +import Fl4ProofsA64.P3.Stage3RecvPollA +import Fl4ProofsA64.P3.Stage3SigBoundRecA +import Fl4ProofsA64.P3.Stage3SigSchedValidA +import Fl4ProofsA64.P3.Stage3RecvPreA +import Fl4ProofsA64.P3.Stage3RecvFrontA +import Fl4ProofsA64.P3.Stage3RecvBlockRUnbA +import Fl4ProofsA64.P3.Stage3NBSignalWakeA +import Fl4ProofsA64.P3.Stage3SigSwA +import Fl4ProofsA64.P3.Stage3CancelReplyRecA +import Fl4ProofsA64.P3.Stage3CancelFootOffA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4Spec Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s : S} {a : A} {σ : State Unit} {m : McState} + +set_option maxHeartbeats 4000000 in +/-- **P1's walk bundle across `take_reply`** (module docstring). -/ +theorem JStage3A.takeReply_rpA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) (hnA : nA = hPl.ipa) + (t : U64) {rr : U64} {rg : Bool} (hrr : rr ∈ H.replies) : + endpoint.take_reply st ar s a t (.Reply rr rg) ⦃ r => + RpPost st H Z.Cs OS Z.V ia ie Ac s r.2.1 ⦄ := by + subst hnA + obtain ⟨Pt, Ub, Pp7, Tb7, Hr, Te, Kt, Lv7, Re, bits, KLv, W⟩ := J7A.unp J.j7 + have I := J.delIn hA hPl W + have h : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s := by + obtain ⟨q, b, x, sg, sc⟩ := J.sys.ent.bxi + exact ⟨q, b, x, sc, sg⟩ + refine WP.spec_mono (take_reply.sw (ar := ar) a t (.Reply rr rg) I J.core0 J.ko + (SW.start J.sys.sysl J.symL h) (fun p g e => ?_)) ?_ + · cases e; exact hrr + · rintro ⟨r, s1, a1⟩ ⟨W1, -⟩ + exact W1.1.post + +set_option maxHeartbeats 8000000 in +/-- **`take_reply` of a reply linked to a receive-blocked thread `i ≠ t`, at the record** (module docstring). -/ +theorem JStage3A.takeReplyCancelRecvA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) (hnA : nA = hPl.ipa) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (t : U64) {rr : U64} {rg : Bool} (hrr : rr ∈ H.replies) + {i : U64} (hi : L.mem s (Endp.rTcbA rr) = i) (hi0 : i ≠ 0#u64) (hit : i ≠ t) + (hiT : i ∈ Z.T) {xi : Tcb} (hki : σ.kheap (specPtr i) = some (.tcb xi)) + {e' : ObjRef} {ro : Option ObjRef} {d : ReceiverPayload} (hs : xi.tcbState = .blockedOnReceive e' ro d) : + endpoint.take_reply st ar s a t (.Reply rr rg) ⦃ r => + r.1 = rr ∧ r.2.2 = a ∧ ∃ σ1 x1 Q Rl, + cancelIpc (Z := Unit) (specPtr i) σ = ({((), σ1)}, False) ∧ + JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) r.2.1 a σ1 m ∧ + WritesIn st s r.2.1 (SchedStepNWA (st := st) N E Rp Z m σ s) ∧ + SchedFrameA σ σ1 ∧ QKeepA σ σ1 ∧ σ1.kheap (specPtr i) = some (.tcb x1) ∧ x1.tcbState = .inactive ∧ + (∀ q, q ≠ specPtr i → (∀ z, σ.kheap q ≠ some (.endpoint z)) → (∀ y, σ.kheap q ≠ some (.reply y)) → + σ1.kheap q = σ.kheap q) ∧ + σ1.curThread = σ.curThread ∧ σ1.idleThread = σ.idleThread ∧ σ1.curSc = σ.curSc ∧ + WritesIn st s r.2.1 (CancelFootA st Rp Z.T s i) ⦄ := by + have body : endpoint.take_reply st ar s a t (.Reply rr rg) ⦃ r => + r.1 = rr ∧ r.2.2 = a ∧ ∃ σ1 x1 Q Rl, + cancelIpc (Z := Unit) (specPtr i) σ = ({((), σ1)}, False) ∧ + JStage3BA st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) r.2.1 a σ1 m ∧ + WritesIn st s r.2.1 (SchedStepNWA (st := st) N E Rp Z m σ s) ∧ + SchedFrameA σ σ1 ∧ QKeepA σ σ1 ∧ σ1.kheap (specPtr i) = some (.tcb x1) ∧ x1.tcbState = .inactive ∧ + (∀ q, q ≠ specPtr i → (∀ z, σ.kheap q ≠ some (.endpoint z)) → (∀ y, σ.kheap q ≠ some (.reply y)) → + σ1.kheap q = σ.kheap q) ∧ + σ1.curThread = σ.curThread ∧ σ1.idleThread = σ.idleThread ∧ σ1.curSc = σ.curSc ∧ + WritesIn st s r.2.1 (CancelFootA st Rp Z.T s i) ⦄ := by + subst hi + simp only [endpoint.take_reply] + step with obj.Reply.tcb.spec as ⟨i', hi'⟩ + step with obj.Tcb.is_null.spec as ⟨bn, hbn⟩ + subst hi' + simp only [hbn, hi0, decide_false, Bool.false_eq_true, ↓reduceIte] + rw [tcb_ne_eqA] + apply spec_ok_bindA + simp only [hit, ne_eq, not_false_eq_true, decide_true, ↓reduceIte] + refine WP.spec_bind (JStage3A.toBA J |>.cancelRecvAny_recA hA hiT hki hs hCs hmb hmax htu) ?_ + rintro ⟨s1, a1⟩ ⟨ha1, σ1, x1, Q, R, hcan, J1, -, w1, -, F1, Qk1, hk1, hin, -, hoth, hct, hid, hcs, hft⟩ + dsimp only at ha1 J1 w1 ⊢ + exact (WP.spec_ok _).2 ⟨rfl, ha1, σ1, x1, Q, R, hcan, J1, w1, F1, Qk1, hk1, hin, hoth, hct, hid, hcs, hft⟩ + refine WP.spec_mono (WP.spec_and (J.takeReply_rpA hA hPl hnA t hrr) body) ?_ + rintro ⟨rv, s1, a1⟩ ⟨regs, hrv, ha, σ1, x1, Q, Rl, hcan, J1, w1, F1, Qk1, hk1, hin, hoth, hct, hid, hcs, hft⟩ + dsimp only at regs hrv ha J1 w1 ⊢ + have hmem : ((), σ1) ∈ (cancelIpc (Z := Unit) (specPtr i) σ).1 := by rw [hcan]; exact Set.mem_singleton _ + have SV : SchedValidA σ1 := by + have hI := SchedValidA.toFIA J.schedValid + have hBA : BAs [specPtr i] σ := fun u hu y sc e hsc => by + have hu' : u = specPtr i := List.mem_singleton.1 hu + subst hu' + rw [hki] at e + obtain rfl := KernelObject.tcb.inj (Option.some.inj e) + obtain ⟨y', k, ey, ea⟩ := hI.1.blk (specPtr i) xi sc hki (by rw [hs]; rfl) hsc + exact ⟨xi, sc, hki, hsc, y', k, ey, ea⟩ + have hrecv : RecvSt (specPtr i) σ := fun y hy => by + rw [hki] at hy + obtain rfl := KernelObject.tcb.inj (Option.some.inj hy) + exact ⟨e', ro, d, hs⟩ + have hC := cancelIpcRecv_fia (specPtr i) σ σ ⟨hI, J.rqDomPrio, ⟨KI.refl _ σ, hBA⟩, hrecv⟩ ((), σ1) hmem + exact SchedValidB.toA hC.1.1 hC.1.2 + obtain ⟨bxi, sysl, symL, userOK, j7⟩ := J.finRpA hA hPl hnA regs σ1 + exact ⟨hrv, ha, σ1, x1, Q, Rl, hcan, + J.of_stepFinA (Q := Q) (Rl := Rl) (curOf := Z.curOf) (curScOf := Z.curScOf) w1 F1.1 F1.2.1 Qk1 F1.2.2 + (fun _ _ => rfl) bxi sysl symL userOK j7 J1.sr J1.ep J1.ntfn J1.reply J1.bufCover J1.bufAl + J1.idleNoFault J1.mcp J1.yieldRel J1.sinv J1.sidle J1.runVcpu J1.blkQ SV, + w1, F1, Qk1, hk1, hin, hoth, hct, hid, hcs, hft⟩ + +/-- **`cancel_ipc` of a receiver blocked with a reply object, from another current thread, only rewrites the heap** +(all scalars, the queues and the scheduler action are kept). -/ +theorem cancelRecvR_frameA {σ : State Unit} {t e r : ObjRef} {x : Tcb} (hk : σ.kheap t = some (.tcb x)) + (hne : t ≠ σ.curThread) {z : Endpoint} (hkz : σ.kheap e = some (.endpoint z)) (hq : t ∈ z.epQueue) + {d : ReceiverPayload} (hs : x.tcbState = .blockedOnReceive e (some r) d) + {xr : Reply} (hkr : σ.kheap r = some (.reply xr)) (hback : xr.replyTcb = some t) + {σ1 : State Unit} (h : cancelIpc (Z := Unit) t σ = ({((), σ1)}, False)) : + σ1 = { σ with kheap := σ1.kheap } ∧ σ1.kheap r = some (.reply { xr with replyTcb := none }) ∧ + σ1.kheap e = some (.endpoint (epDeq z t)) ∧ ∀ q, q ≠ e → q ≠ r → q ≠ t → σ1.kheap q = σ.kheap q := by + have het : e ≠ t := by intro h'; rw [h', hk] at hkz; cases hkz + have hrt : r ≠ t := by intro h'; rw [h', hk] at hkr; cases hkr + have hre : r ≠ e := by intro h'; rw [h', hkz] at hkr; cases hkr + let σa := updK σ t (.tcb { x with tcbFault := none }) + have hkz1 : σa.kheap e = some (.endpoint z) := by + simp only [σa, updK_kheap, Function.update_of_ne het]; exact hkz + have hdq := tcbEpDequeue_eq (Z := Unit) hkz1 hq + let σ2 := updK σa e (.endpoint (epDeq z t)) + have hk2 : σ2.kheap t = some (.tcb { x with tcbFault := none }) := by + simp only [σ2, σa, updK_kheap, Function.update_of_ne het.symm, Function.update_self] + have hkr2 : σ2.kheap r = some (.reply xr) := by + simp only [σ2, σa, updK_kheap, Function.update_of_ne hre, Function.update_of_ne hrt]; exact hkr + have hst2 : ({ x with tcbFault := none } : Tcb).tcbState = .blockedOnReceive e (some r) d := hs + have hun := replyUnlinkTcb_eq hkr2 hback hk2 hst2 + let σ3 := updK (updK σ2 r (.reply { xr with replyTcb := none })) t + (.tcb { ({ x with tcbFault := none } : Tcb) with tcbState := .inactive }) + have hs3 : stsAct σ3 t = σ3 := stsAct_neA hne + rw [hs3] at hun + have hk4 : σ3.kheap t = some (.tcb { ({ x with tcbFault := none } : Tcb) with tcbState := .inactive }) := by + simp only [σ3, updK_kheap, Function.update_self] + have hbc := bci_reply (ts := x.tcbState) (by rw [hs]; rfl) hdq hun hk4 + have hc := cancelIpc_blocked hk (some r) (Or.inr ⟨e, d, hs⟩) hbc + have e1 := congrArg Prod.snd (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst (h.symm.trans hc))) + simp only at e1 + unfold stsAct at e1 + rw [if_neg (fun h' => hne h'.1)] at e1 + refine ⟨by rw [e1]; rfl, ?_, ?_, ?_⟩ + · rw [e1] + simp only [σ3, updK_kheap, Function.update_of_ne hrt, Function.update_self] + · rw [e1] + simp only [σ3, σ2, updK_kheap, Function.update_of_ne het, Function.update_of_ne hre.symm, Function.update_self] + · intro q hqe hqr hqt + rw [e1] + simp only [σ3, σ2, σa, updK_kheap, Function.update_of_ne hqt, Function.update_of_ne hqr, Function.update_of_ne hqe] + +set_option maxHeartbeats 8000000 in +/-- **`take_reply`'s cancel of a receiver, at `RecvPreA`**: the record, the body-end facts and the current thread's +facts at the state after the cancel (module docstring); [S] the reply's thread `i` is receive-blocked on this reply. -/ +theorem RecvPreA.takeReplyCancelA + (J : RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) (hnA : nA = hPl.ipa) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {rr : U64} {rg : Bool} (hrr : rr ∈ H.replies) + {i : U64} (hi : L.mem s (Endp.rTcbA rr) = i) (hi0 : i ≠ 0#u64) (hit : i ≠ L.curW s) + (hiT : i ∈ Z.T) {xi : Tcb} (hki : σ.kheap (specPtr i) = some (.tcb xi)) + {e' : ObjRef} {d : ReceiverPayload} (hs : xi.tcbState = .blockedOnReceive e' (some (specPtr rr)) d) : + endpoint.take_reply st ar s a (L.curW s) (.Reply rr rg) ⦃ r => + r.1 = rr ∧ r.2.2 = a ∧ ∃ σ1 x1 Q Rl, + cancelIpc (Z := Unit) (specPtr i) σ = ({((), σ1)}, False) ∧ + RecvPreA st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) r.2.1 a σ1 m ∧ L.curW r.2.1 = L.curW s ∧ + σ1.kheap (specPtr i) = some (.tcb x1) ∧ x1.tcbState = .inactive ∧ + (∀ q, q ≠ specPtr i → (∀ z, σ.kheap q ≠ some (.endpoint z)) → (∀ y, σ.kheap q ≠ some (.reply y)) → + σ1.kheap q = σ.kheap q) ∧ σ1 = { σ with kheap := σ1.kheap } ∧ + (∃ xr, σ.kheap (specPtr rr) = some (.reply xr) ∧ + σ1.kheap (specPtr rr) = some (.reply { xr with replyTcb := none })) ∧ OSKeep st OS s r.2.1 ∧ + ∃ z, σ.kheap e' = some (.endpoint z) ∧ σ1.kheap e' = some (.endpoint (epDeq z (specPtr i))) ∧ + (∀ q, q ≠ e' → q ≠ specPtr rr → q ≠ specPtr i → σ1.kheap q = σ.kheap q) ∧ + WritesIn st s r.2.1 (CancelFootA st Rp Z.T s i) ⦄ := by + refine WP.spec_mono (J.toJStage3A.takeReplyCancelRecvA hA hPl hnA hCs hmb hmax htu (L.curW s) hrr hi hi0 hit hiT + hki hs) ?_ + rintro ⟨rv, s1, a1⟩ ⟨hrv, ha, σ1, x1, Q, Rl, hcan, J2, w1, F1, Qk1, hk1, hin, hoth, hct, hid, hcs, hft⟩ + dsimp only at hrv ha J2 w1 ⊢ + have ct : σ.curThread = specPtr (L.curW s) := J.j.1.sch.curThread + have hne : specPtr i ≠ σ.curThread := by rw [ct]; exact fun h => hit (specPtr_inj h) + obtain ⟨-, z, hkz, hq⟩ := J.blkQ.ep i hiT xi hki e' (by rw [hs]; rfl) + obtain ⟨xr, hkr, hback⟩ := J.toJStage3A.repOKA hiT xi hki e' (specPtr rr) d hs + obtain ⟨hfr, hrp, hep, hqo⟩ := cancelRecvR_frameA hki hne hkz hq hs hkr hback hcan + have hmem : ((), σ1) ∈ (cancelIpc (Z := Unit) (specPtr i) σ).1 := by rw [hcan]; exact Set.mem_singleton _ + have f_act : σ1.schedulerAction = σ.schedulerAction := by + have h := congrArg (fun τ : State Unit => τ.schedulerAction) hfr + exact h + have f_time : σ1.curTime = σ.curTime := by + have h := congrArg (fun τ : State Unit => τ.curTime) hfr + exact h + have f_cons : σ1.consumedTime = σ.consumedTime := by + have h := congrArg (fun τ : State Unit => τ.consumedTime) hfr + exact h + have f_rel : σ1.releaseQueue = σ.releaseQueue := by + have h := congrArg (fun τ : State Unit => τ.releaseQueue) hfr + exact h + have f_last : σ1.machineState.lastMachineTime = σ.machineState.lastMachineTime := by + have h := congrArg (fun τ : State Unit => τ.machineState.lastMachineTime) hfr + exact h + have hcw : L.curW s1 = L.curW s := curW_keptA J.toJStage3A J2 hct + have hcT : L.curW s ∈ Z.T := J.sr.curT + obtain ⟨xc, hk0, -⟩ := J.ep.tcb (L.curW s) hcT + have hkc : σ.kheap σ.curThread = some (.tcb xc) := by rw [ct]; exact hk0 + have hnei : specPtr (L.curW s) ≠ specPtr i := fun h => hit (specPtr_inj h.symm) + have hk1c : σ1.kheap (specPtr (L.curW s)) = some (.tcb xc) := by + rw [hoth _ hnei (fun z c => by rw [hk0] at c; cases c) (fun y c => by rw [hk0] at c; cases c)]; exact hk0 + have hrecv : RecvSt (specPtr i) σ := fun y hy => by + rw [hki] at hy + obtain rfl := KernelObject.tcb.inj (Option.some.inj hy) + exact ⟨e', _, d, hs⟩ + have hsw : SwA σ1 := by + have hSwN : SwN (specPtr i) σ := fun u e => ⟨J.body.sw u e, fun h => by + subst h + obtain ⟨-, -, x, hx, hr⟩ := schedulable_actA (J.body.sw _ e) + rw [hki] at hx + obtain rfl := KernelObject.tcb.inj (Option.some.inj hx) + rw [hs] at hr + simp [runnable] at hr⟩ + exact (cancelIpcRecv_swn (specPtr i) σ ⟨hSwN, hrecv⟩ ((), σ1) hmem).swa + have off := cancelIpcRecv_offq σ.curThread (specPtr i) σ + ⟨⟨J.noQ, J.notRel, fun u e h => by subst h; exact J.noSelf e⟩, hrecv⟩ ((), σ1) hmem + have body : BodyOKA σ1 m := by + refine J.body.stepEq hsw f_time f_last hct hcs f_cons f_rel (fun y' k hy' => ?_) (fun _ x hx => ?_) + (fun h => by rw [f_act] at h; exact h) + · obtain ⟨y0, n0, hy0⟩ := J.sinv.curSc + have ne : σ.curSc ≠ specPtr i := fun e => by rw [e, hki] at hy0; cases hy0 + rw [hoth _ ne (fun z c => by rw [hy0] at c; cases c) (fun y c => by rw [hy0] at c; cases c), hy0] at hy' + obtain ⟨rfl, rfl⟩ := KernelObject.schedContext.inj (Option.some.inj hy').symm + exact ⟨_, hy0, rfl, rfl, rfl⟩ + · rw [hoth _ (fun e => hne e.symm) (fun z c => by rw [hkc] at c; cases c) + (fun y c => by rw [hkc] at c; cases c), hkc] at hx + obtain rfl := KernelObject.tcb.inj (Option.some.inj hx) + exact J.state hk0 + have rn : L.mem s1 (stA (L.curW s)) = sched.TS_RUNNING ∨ L.mem s1 (stA (L.curW s)) = sched.TS_RESTART := by + rcases J.state hk0 with e | e + · left + refine UScalar.eq_of_val_eq ?_ + have h1 := (J2.ep.at_tcb hcT hk1c).st + rw [e] at h1 + exact h1.trans (by unfold sched.TS_RUNNING; rfl) + · right + refine UScalar.eq_of_val_eq ?_ + have h1 := (J2.ep.at_tcb hcT hk1c).st + rw [e] at h1 + exact h1.trans (by unfold sched.TS_RESTART; rfl) + have run' : L.mem s1 (stA (L.curW s1)) = sched.TS_RUNNING ∨ L.mem s1 (stA (L.curW s1)) = sched.TS_RESTART := by + rw [hcw]; exact rn + have noQ' : ∀ d p, σ1.curThread ∉ σ1.readyQueues d p := fun d p => by rw [hct]; exact off.1 d p + have notRel' : σ1.curThread ∉ σ1.releaseQueue := by rw [hct]; exact off.2.1 + have noSelf' : σ1.schedulerAction ≠ .switchThread σ1.curThread := fun e => off.2.2 _ e hct + have scA : ∀ x sc, σ1.kheap σ1.curThread = some (.tcb x) → x.tcbSchedContext = some sc → + ScActiveAtA σ1 sc := by + intro x sc hx hxs + rw [hct, hoth _ (fun e => hne e.symm) (fun z c => by rw [hkc] at c; cases c) + (fun y c => by rw [hkc] at c; cases c), hkc] at hx + obtain rfl := KernelObject.tcb.inj (Option.some.inj hx) + obtain ⟨y, k, hy, ha'⟩ := J.scAct _ sc hkc hxs + have ne : sc ≠ specPtr i := fun e => by rw [e, hki] at hy; cases hy + exact ⟨y, k, by rw [hoth _ ne (fun z c => by rw [hy] at c; cases c) (fun y' c => by rw [hy] at c; cases c)]; exact hy, ha'⟩ + exact ⟨hrv, ha, σ1, x1, Q, Rl, hcan, ⟨J2, body, run', noQ', notRel', scA, noSelf'⟩, hcw, hk1, hin, hoth, hfr, ⟨xr, hkr, hrp⟩, J.toJStage3A.osKeep_stepNA w1, z, hkz, hep, hqo, hft⟩ + +set_option maxHeartbeats 8000000 in +/-- **The receive core across `take_reply`'s cancel** (arm-agnostic): given the arm's core at the state after the cancel, +the core at the entry state; `receive_ipc` and the spec's `receiveIpc` run the same from both (the cancel unlinks the +reply, so the second `take_reply` is plain). [S] `i` is receive-blocked on this reply. -/ +theorem RecvPreA.cancelCoreA + (J : RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) (hnA : nA = hPl.ipa) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {rr p badge : U64} {rg send grant grantReply blk : Bool} (hrr : rr ∈ Rp) + {i : U64} (hi : L.mem s (Endp.rTcbA rr) = i) (hi0 : i ≠ 0#u64) (hit : i ≠ L.curW s) + (hiT : i ∈ Z.T) {xi : Tcb} (hki : σ.kheap (specPtr i) = some (.tcb xi)) + {e' : ObjRef} {d : ReceiverPayload} (hs : xi.tcbState = .blockedOnReceive e' (some (specPtr rr)) d) + {Out1 : State Unit → State Unit → Prop} + (arm : ∀ (s1 : S) (σ1 : State Unit) (Q : U64 → U64 → List U64) (Rl : U64 → List U64) + (J1 : RecvPreA st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s1 a σ1 m), + L.curW s1 = L.curW s → cancelIpc (Z := Unit) (specPtr i) σ = ({((), σ1)}, False) → + σ1 = { σ with kheap := σ1.kheap } → + (∀ q, q ≠ specPtr i → (∀ z, σ.kheap q ≠ some (.endpoint z)) → (∀ y, σ.kheap q ≠ some (.reply y)) → + σ1.kheap q = σ.kheap q) → + L.mem s1 (Endp.rTcbA rr) = 0#u64 → OSKeep st OS s s1 → + ∀ z0 : Endpoint, σ.kheap e' = some (.endpoint z0) → σ1.kheap e' = some (.endpoint (epDeq z0 (specPtr i))) → + (∀ q, q ≠ e' → q ≠ specPtr rr → q ≠ specPtr i → σ1.kheap q = σ.kheap q) → + WritesIn st s s1 (CancelFootA st Rp Z.T s i) → + RecvCoreA st AR ar nA n Z.Cs s1 a m (.Endpoint p badge send true grant grantReply) blk (.Reply rr rg) + (absCapA (.Endpoint p badge send true grant grantReply)) (.replyCap (specPtr rr) (replyRightsA rg)) σ1 + (Out1 σ1)) : + RecvCoreA st AR ar nA n Z.Cs s a m (.Endpoint p badge send true grant grantReply) blk (.Reply rr rg) + (absCapA (.Endpoint p badge send true grant grantReply)) (.replyCap (specPtr rr) (replyRightsA rg)) σ + (fun σ' => ∃ σ1, cancelIpc (Z := Unit) (specPtr i) σ = ({((), σ1)}, False) ∧ Out1 σ1 σ') := by + obtain ⟨⟨rv, s1, a1⟩, htake, hrv, ha, σ1, x1, Q, Rl, hcan, J1, hcw, hk1, hin, hoth, hfr, ⟨xr, hkr, hrp⟩, osk, z, hkz, hep, hqo, hft⟩ := + spec_existsA (J.takeReplyCancelA hA hPl hnA hCs hmb hmax htu (rg := rg) (J.utIpc.reply rr hrr) hi hi0 hit hiT hki hs) + dsimp only at hrv ha hcw + rw [hrv, ha] at htake + obtain ⟨xr0, hkr0, hxr0⟩ := J.ep.reply rr hrr + rw [hkr0] at hkr + obtain rfl := KernelObject.reply.inj (Option.some.inj hkr) + have hback : xr0.replyTcb = some (specPtr i) := by rw [hxr0, hi, optRef_ne hi0] + obtain ⟨xr1, hkr1, hxr1⟩ := J1.ep.reply rr hrr + rw [hrp] at hkr1 + obtain rfl := KernelObject.reply.inj (Option.some.inj hkr1) + have hfree1 : L.mem s1 (Endp.rTcbA rr) = 0#u64 := by + by_contra hne + rw [optRef_ne hne] at hxr1 + cases hxr1 + obtain ⟨s2, σ', hipc, hrecv, hout, hrel, hcur, hbody⟩ := + arm s1 σ1 Q Rl J1 hcw hcan hfr hoth hfree1 osk z hkz hep hqo hft + obtain ⟨q1, htake1, e1⟩ := spec_existsA (take_reply_plainA (s := s1) (a := a) (t := L.curW s) (R := Rp) + (rc' := .replyCap (specPtr rr) (replyRightsA rg)) + (Or.inr ⟨rg, _, rfl, rfl, hrr⟩ : ReplyCapA Rp (.Reply rr rg) (.replyCap (specPtr rr) (replyRightsA rg)) rr) + (fun _ _ => Or.inl hfree1)) + subst e1 + rw [hcw, receive_ipc_eqA, htake1, bind_ok] at hipc + have hct : σ.curThread = specPtr (L.curW s) := J.j.1.sch.curThread + have hne : (L.curW s) ≠ i := fun h => hit h.symm + have hxn : specPtr i ≠ specPtr (L.curW s) := fun h => hit (specPtr_inj h) + have hs1 : receiveIpc (Z := Unit) (specPtr (L.curW s)) (absCapA (.Endpoint p badge send true grant grantReply)) blk + (.replyCap (specPtr rr) (replyRightsA rg)) σ = + receiveIpc (Z := Unit) (specPtr (L.curW s)) (absCapA (.Endpoint p badge send true grant grantReply)) blk + (.replyCap (specPtr rr) (replyRightsA rg)) σ1 := by + rw [show absCapA (.Endpoint p badge send true grant grantReply) = + .endpointCap (specPtr p) badge.bv (rightsOfA send true grant grantReply) from rfl, + receiveIpc_cancelA badge.bv (rightsOfA send true grant grantReply) blk hkr0 hback hxn hcan, + receiveIpc_bodyA _ _ blk (Or.inr ⟨specPtr rr, _, { xr0 with replyTcb := none }, rfl, rfl, hrp, + Or.inl rfl⟩ : SpecReplyPlainA σ1 (specPtr (L.curW s)) (.replyCap (specPtr rr) (replyRightsA rg)) + (some (specPtr rr)))] + rw [hcw] at hrecv + refine ⟨s2, σ', ?_, ?_, ⟨σ1, hcan, hout⟩, hrel, hcur.trans hcw, hbody⟩ + · rw [receive_ipc_eqA, htake, bind_ok] + exact hipc + · rw [hs1] + exact hrecv + +theorem RecvCoreA.mono {cap rc : capability.Cap} {blk : Bool} {scap src : Cap} {Cs : Set U64} {Out Out' : State Unit → Prop} + (h : RecvCoreA st AR ar nA n Cs s a m cap blk rc scap src σ Out) (hO : ∀ σ', Out σ' → Out' σ') : + RecvCoreA st AR ar nA n Cs s a m cap blk rc scap src σ Out' := by + obtain ⟨s2, σ', h1, h2, h3, h4, h5, h6⟩ := h + exact ⟨s2, σ', h1, h2, hO _ h3, h4, h5, h6⟩ + +theorem SlotCov.keepA {OS : Set U64} {s1 : S} (osk : OSKeep st OS s s1) {y : U64} (h : SlotCov st OS s y) : + SlotCov st OS s1 y := by + obtain ⟨z, hz, p, r, g, b, hc, i, hi, hy⟩ := h + exact ⟨z, hz, p, r, g, b, by rw [osk.capA hz]; exact hc, i, hi, hy⟩ + +/-- **A linked reply's thread** (module docstring, item 4). -/ +theorem RecvPreA.replyLinkA + (J : RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {rr : U64} (hrr : rr ∈ Rp) (h0 : L.mem s (Endp.rTcbA rr) ≠ 0#u64) : + L.mem s (Endp.rTcbA rr) ∈ Z.T ∧ L.mem s (Endp.rTcbA rr) ≠ L.curW s ∧ + ∃ xi, σ.kheap (specPtr (L.mem s (Endp.rTcbA rr))) = some (.tcb xi) ∧ + ((∃ e' d, xi.tcbState = .blockedOnReceive e' (some (specPtr rr)) d) ∨ + xi.tcbState = .blockedOnReply (specPtr rr)) := by + obtain ⟨hne, hiH0, hrep0, hst0⟩ := J.blkA.2.2.1.2.1.back rr (J.utIpc.reply rr hrr) h0 + have hiH : L.mem s (Endp.rTcbA rr) ∈ H.tcbs := hiH0 + have hst : L.mem s (stA (L.mem s (Endp.rTcbA rr))) = sched.TS_BLOCKED_ON_REPLY ∨ + L.mem s (stA (L.mem s (Endp.rTcbA rr))) = sched.TS_BLOCKED_ON_RECV := hst0 + have hiT : L.mem s (Endp.rTcbA rr) ∈ Z.T := by rw [← J.tcbs]; exact hiH + obtain ⟨xi, hki, rel⟩ := J.ep.tcb _ hiT + have hrep' : L.mem s (tReplyA (L.mem s (Endp.rTcbA rr))) = rr := hrep0 + refine ⟨hiT, fun e => ?_, xi, hki, ?_⟩ + · obtain ⟨xc, hk0, -⟩ := J.ep.tcb (L.curW s) J.sr.curT + have hx := (J.ep.at_tcb J.sr.curT hk0).st + rw [e] at hst + rcases J.state hk0 with c | c <;> rcases hst with h | h <;> + (rw [h, c] at hx; revert hx; first | decide | simp [sched.TS_BLOCKED_ON_REPLY, sched.TS_BLOCKED_ON_RECV, tsCode]) + · rcases hst with h | h + · right + have h5 : tsCode xi.tcbState = 5 := by + rw [← rel.st, h]; unfold sched.TS_BLOCKED_ON_REPLY; rfl + obtain ⟨q, hq⟩ := tsCode_five h5 + rw [hq, ← rel.reply q hq, hrep'] + · left + have h3 : tsCode xi.tcbState = 3 := by + rw [← rel.st, h]; unfold sched.TS_BLOCKED_ON_RECV; rfl + obtain ⟨e', ro, d, hq⟩ := Endp.tsCode_three h3 + refine ⟨e', d, ?_⟩ + have := (rel.recv e' ro d hq).1 + rw [hrep', optRef_ne hne] at this + rw [hq, ← this] + +set_option maxHeartbeats 8000000 in +/-- **`take_reply` of a reply linked to a reply-blocked thread `i ≠ t`, at the record** (module docstring). -/ +theorem JStage3A.takeReplyCancelReplyA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) (hnA : nA = hPl.ipa) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (t : U64) {rr : U64} {rg : Bool} (hrr : rr ∈ H.replies) (hrR : rr ∈ Rp) + {i : U64} (hi : L.mem s (Endp.rTcbA rr) = i) (hi0 : i ≠ 0#u64) (hit : i ≠ t) + (hiT : i ∈ Z.T) {xi : Tcb} (hki : σ.kheap (specPtr i) = some (.tcb xi)) + (hs : xi.tcbState = .blockedOnReply (specPtr rr)) {xr : Reply} (hkr : σ.kheap (specPtr rr) = some (.reply xr)) + (hback : xr.replyTcb = some (specPtr i)) (hne : specPtr i ≠ σ.curThread) : + endpoint.take_reply st ar s a t (.Reply rr rg) ⦃ r => + r.1 = rr ∧ r.2.2 = a ∧ ∃ σ1 stack' x1 Q Rl, + cancelIpc (Z := Unit) (specPtr i) σ = ({((), σ1)}, False) ∧ + JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack' κ n (J5A st) + (J7A st (J7R2A st ar nA)) r.2.1 a σ1 m ∧ + WritesIn st s r.2.1 (SchedStepNWA (st := st) N E Rp Z m σ s) ∧ + SchedFrameA σ σ1 ∧ QKeepA σ σ1 ∧ σ1.kheap (specPtr i) = some (.tcb x1) ∧ x1.tcbState = .inactive ∧ + (∀ q, q ≠ specPtr i → (∀ y n, σ.kheap q ≠ some (.schedContext y n)) → (∀ y, σ.kheap q ≠ some (.reply y)) → + σ1.kheap q = σ.kheap q) ∧ σ1 = { σ with kheap := σ1.kheap } ∧ + (∃ z, σ1.kheap (specPtr rr) = some (.reply z) ∧ z.replyTcb = none) ∧ + CancelKeepA σ σ1 ∧ KeepT st H.tcbs Z.Cs H.replies H.scs H.ntfns i s r.2.1 ∧ + (∀ c, ∀ q ∈ stack' c, q ∈ stack c) ⦄ := by + have body : endpoint.take_reply st ar s a t (.Reply rr rg) ⦃ r => + r.1 = rr ∧ r.2.2 = a ∧ ∃ σ1 stack' x1 Q Rl, + cancelIpc (Z := Unit) (specPtr i) σ = ({((), σ1)}, False) ∧ + JStage3BA st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack' κ n (J5A st) + (J7A st (J7R2A st ar nA)) r.2.1 a σ1 m ∧ + WritesIn st s r.2.1 (SchedStepNWA (st := st) N E Rp Z m σ s) ∧ + SchedFrameA σ σ1 ∧ QKeepA σ σ1 ∧ σ1.kheap (specPtr i) = some (.tcb x1) ∧ x1.tcbState = .inactive ∧ + (∀ q, q ≠ specPtr i → (∀ y n, σ.kheap q ≠ some (.schedContext y n)) → (∀ y, σ.kheap q ≠ some (.reply y)) → + σ1.kheap q = σ.kheap q) ∧ σ1 = { σ with kheap := σ1.kheap } ∧ + (∃ z, σ1.kheap (specPtr rr) = some (.reply z) ∧ z.replyTcb = none) ∧ + CancelKeepA σ σ1 ∧ KeepT st H.tcbs Z.Cs H.replies H.scs H.ntfns i s r.2.1 ∧ + (∀ c, ∀ q ∈ stack' c, q ∈ stack c) ⦄ := by + subst hi + simp only [endpoint.take_reply] + step with obj.Reply.tcb.spec as ⟨i', hi'⟩ + step with obj.Tcb.is_null.spec as ⟨bn, hbn⟩ + subst hi' + simp only [hbn, hi0, decide_false, Bool.false_eq_true, ↓reduceIte] + rw [tcb_ne_eqA] + apply spec_ok_bindA + simp only [hit, ne_eq, not_false_eq_true, decide_true, ↓reduceIte] + refine WP.spec_bind (JStage3A.toBA J |>.cancelReply_recA hiT hrR hki hs hkr hback hne hCs hmb hmax htu) ?_ + rintro ⟨s1, a1⟩ ⟨ha1, σ1, stk, x1, Q, R, hcan, J1, w1, F1, Qk1, hk1, hin, hoth, hfr, hz, hsub, kk, kp⟩ + dsimp only at ha1 J1 w1 ⊢ + exact (WP.spec_ok _).2 ⟨rfl, ha1, σ1, stk, x1, Q, R, hcan, J1, w1, F1, Qk1, hk1, hin, hoth, hfr, hz, kk, kp, hsub⟩ + refine WP.spec_mono (WP.spec_and (J.takeReply_rpA hA hPl hnA t hrr) body) ?_ + rintro ⟨rv, s1, a1⟩ ⟨regs, hrv, ha, σ1, stk, x1, Q, Rl, hcan, J1, w1, F1, Qk1, hk1, hin, hoth, hfr, hz, kk, kp, hsub⟩ + dsimp only at regs hrv ha J1 w1 ⊢ + have hmem : ((), σ1) ∈ (cancelIpc (Z := Unit) (specPtr i) σ).1 := by rw [hcan]; exact Set.mem_singleton _ + have SV : SchedValidA σ1 := by + have hI := SchedValidA.toFIA J.schedValid + have hBA : BAs [specPtr i] σ := fun u hu y sc e hsc => by + have hu' : u = specPtr i := List.mem_singleton.1 hu + subst hu' + rw [hki] at e + obtain rfl := KernelObject.tcb.inj (Option.some.inj e) + obtain ⟨y', k, ey, ea⟩ := hI.1.blk (specPtr i) xi sc hki (by rw [hs]; rfl) hsc + exact ⟨xi, sc, hki, hsc, y', k, ey, ea⟩ + have hrs : ReplySt (specPtr i) σ := fun y hy => by + rw [hki] at hy + obtain rfl := KernelObject.tcb.inj (Option.some.inj hy) + exact ⟨_, hs⟩ + have hC := cancelIpcReply_fia (specPtr i) σ σ ⟨hI, J.rqDomPrio, ⟨KI.refl _ σ, hBA⟩, hrs⟩ ((), σ1) hmem + exact SchedValidB.toA hC.1.1 hC.1.2 + obtain ⟨bxi, sysl, symL, userOK, j7⟩ := J.finRpA hA hPl hnA regs σ1 + exact ⟨hrv, ha, σ1, stk, x1, Q, Rl, hcan, + J.of_stepFinA (Q := Q) (Rl := Rl) (curOf := Z.curOf) (curScOf := Z.curScOf) (hsub := hsub) w1 F1.1 F1.2.1 Qk1 + F1.2.2 (fun _ _ => rfl) bxi sysl symL userOK j7 J1.sr J1.ep J1.ntfn J1.reply J1.bufCover J1.bufAl + J1.idleNoFault J1.mcp J1.yieldRel J1.sinv J1.sidle J1.runVcpu J1.blkQ SV, + w1, F1, Qk1, hk1, hin, hoth, hfr, hz, kk, kp, hsub⟩ + +set_option maxHeartbeats 8000000 in +/-- **`take_reply`'s cancel of a reply-blocked thread, at `RecvPreA`** (module docstring). -/ +theorem RecvPreA.takeReplyCancelRA + (J : RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) (hnA : nA = hPl.ipa) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {rr : U64} {rg : Bool} (hrr : rr ∈ H.replies) (hrR : rr ∈ Rp) + {i : U64} (hi : L.mem s (Endp.rTcbA rr) = i) (hi0 : i ≠ 0#u64) (hit : i ≠ L.curW s) + (hiT : i ∈ Z.T) {xi : Tcb} (hki : σ.kheap (specPtr i) = some (.tcb xi)) + (hs : xi.tcbState = .blockedOnReply (specPtr rr)) {xr : Reply} (hkr : σ.kheap (specPtr rr) = some (.reply xr)) + (hback : xr.replyTcb = some (specPtr i)) : + endpoint.take_reply st ar s a (L.curW s) (.Reply rr rg) ⦃ r => + r.1 = rr ∧ r.2.2 = a ∧ ∃ σ1 stack' x1 Q Rl, + cancelIpc (Z := Unit) (specPtr i) σ = ({((), σ1)}, False) ∧ + RecvPreA st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack' κ n (J5A st) + (J7A st (J7R2A st ar nA)) r.2.1 a σ1 m ∧ L.curW r.2.1 = L.curW s ∧ + σ1.kheap (specPtr i) = some (.tcb x1) ∧ x1.tcbState = .inactive ∧ + (∀ q, q ≠ specPtr i → (∀ y n, σ.kheap q ≠ some (.schedContext y n)) → (∀ y, σ.kheap q ≠ some (.reply y)) → + σ1.kheap q = σ.kheap q) ∧ σ1 = { σ with kheap := σ1.kheap } ∧ + (∃ z, σ1.kheap (specPtr rr) = some (.reply z) ∧ z.replyTcb = none) ∧ OSKeep st OS s r.2.1 ∧ + KeepT st H.tcbs Z.Cs H.replies H.scs H.ntfns i s r.2.1 ⦄ := by + have ct : σ.curThread = specPtr (L.curW s) := J.j.1.sch.curThread + have hne : specPtr i ≠ σ.curThread := by rw [ct]; exact fun h => hit (specPtr_inj h) + refine WP.spec_mono (J.toJStage3A.takeReplyCancelReplyA hA hPl hnA hCs hmb hmax htu (L.curW s) hrr hrR hi hi0 hit hiT + hki hs hkr hback hne) ?_ + rintro ⟨rv, s1, a1⟩ ⟨hrv, ha, σ1, stk, x1, Q, Rl, hcan, J2, w1, F1, Qk1, hk1, hin, hoth, hfr, hz, kk, kp, hsub⟩ + dsimp only at hrv ha J2 w1 ⊢ + have hmem : ((), σ1) ∈ (cancelIpc (Z := Unit) (specPtr i) σ).1 := by rw [hcan]; exact Set.mem_singleton _ + have hrs : ReplySt (specPtr i) σ := fun y hy => by + rw [hki] at hy + obtain rfl := KernelObject.tcb.inj (Option.some.inj hy) + exact ⟨_, hs⟩ + have fe : ∀ {β : Type} (f : State Unit → β), f σ1 = f { σ with kheap := σ1.kheap } := fun f => congrArg f hfr + have f_act : σ1.schedulerAction = σ.schedulerAction := fe (fun τ => τ.schedulerAction) + have f_time : σ1.curTime = σ.curTime := fe (fun τ => τ.curTime) + have f_cons : σ1.consumedTime = σ.consumedTime := fe (fun τ => τ.consumedTime) + have f_rel : σ1.releaseQueue = σ.releaseQueue := fe (fun τ => τ.releaseQueue) + have f_last : σ1.machineState.lastMachineTime = σ.machineState.lastMachineTime := + fe (fun τ => τ.machineState.lastMachineTime) + have hct : σ1.curThread = σ.curThread := fe (fun τ => τ.curThread) + have hcw : L.curW s1 = L.curW s := curW_keptA J.toJStage3A J2 hct + have hcT : L.curW s ∈ Z.T := J.sr.curT + obtain ⟨xc, hk0, -⟩ := J.ep.tcb (L.curW s) hcT + have hkc : σ.kheap σ.curThread = some (.tcb xc) := by rw [ct]; exact hk0 + have hnei : specPtr (L.curW s) ≠ specPtr i := fun h => hit (specPtr_inj h.symm) + have hk1c : σ1.kheap (specPtr (L.curW s)) = some (.tcb xc) := by + rw [hoth _ hnei (fun y n c => by rw [hk0] at c; cases c) (fun y c => by rw [hk0] at c; cases c)]; exact hk0 + have hsw : SwA σ1 := by + have hSwN : SwN (specPtr i) σ := fun u e => ⟨J.body.sw u e, fun h => by + subst h + obtain ⟨-, -, x, hx, hr⟩ := schedulable_actA (J.body.sw _ e) + rw [hki] at hx + obtain rfl := KernelObject.tcb.inj (Option.some.inj hx) + rw [hs] at hr + simp [runnable] at hr⟩ + exact (cancelIpcReply_swn (specPtr i) σ ⟨hSwN, hrs⟩ ((), σ1) hmem).swa + have off := cancelIpcReply_offq σ.curThread (specPtr i) σ + ⟨⟨J.noQ, J.notRel, fun u e h => by subst h; exact J.noSelf e⟩, hrs⟩ ((), σ1) hmem + have body : BodyOKA σ1 m := by + refine J.body.stepEq hsw f_time f_last hct (fe (fun τ => τ.curSc)) f_cons f_rel (fun y' k hy' => ?_) (fun _ x hx => ?_) + (fun h => by rw [f_act] at h; exact h) + · obtain ⟨y0, n0, hy0⟩ := J.sinv.curSc + obtain ⟨y2, h2, e⟩ := kk.sc _ y0 n0 hy0 + rw [h2] at hy' + obtain ⟨rfl, rfl⟩ := KernelObject.schedContext.inj (Option.some.inj hy').symm + exact ⟨y0, hy0, by rw [e], by rw [e], by rw [e]⟩ + · rw [hoth _ (fun e => hne e.symm) (fun y n c => by rw [hkc] at c; cases c) + (fun y c => by rw [hkc] at c; cases c), hkc] at hx + obtain rfl := KernelObject.tcb.inj (Option.some.inj hx) + exact J.state hk0 + have rn : L.mem s1 (stA (L.curW s)) = sched.TS_RUNNING ∨ L.mem s1 (stA (L.curW s)) = sched.TS_RESTART := by + rcases J.state hk0 with e | e + · left + refine UScalar.eq_of_val_eq ?_ + have h1 := (J2.ep.at_tcb hcT hk1c).st + rw [e] at h1 + exact h1.trans (by unfold sched.TS_RUNNING; rfl) + · right + refine UScalar.eq_of_val_eq ?_ + have h1 := (J2.ep.at_tcb hcT hk1c).st + rw [e] at h1 + exact h1.trans (by unfold sched.TS_RESTART; rfl) + have run' : L.mem s1 (stA (L.curW s1)) = sched.TS_RUNNING ∨ L.mem s1 (stA (L.curW s1)) = sched.TS_RESTART := by + rw [hcw]; exact rn + have noQ' : ∀ d p, σ1.curThread ∉ σ1.readyQueues d p := fun d p => by rw [hct]; exact off.1 d p + have notRel' : σ1.curThread ∉ σ1.releaseQueue := by rw [hct]; exact off.2.1 + have noSelf' : σ1.schedulerAction ≠ .switchThread σ1.curThread := fun e => off.2.2 _ e hct + have scA : ∀ x sc, σ1.kheap σ1.curThread = some (.tcb x) → x.tcbSchedContext = some sc → + ScActiveAtA σ1 sc := by + intro x sc hx hxs + rw [hct, hoth _ (fun e => hne e.symm) (fun y n c => by rw [hkc] at c; cases c) + (fun y c => by rw [hkc] at c; cases c), hkc] at hx + obtain rfl := KernelObject.tcb.inj (Option.some.inj hx) + obtain ⟨y, k, hy, ha'⟩ := J.scAct _ sc hkc hxs + obtain ⟨y2, h2, e⟩ := kk.sc sc y k hy + exact ⟨y2, k, h2, by rw [e]; exact ha'⟩ + exact ⟨hrv, ha, σ1, stk, x1, Q, Rl, hcan, ⟨J2, body, run', noQ', notRel', scA, noSelf'⟩, hcw, hk1, hin, hoth, hfr, + hz, J.toJStage3A.osKeep_stepNA w1, kp⟩ + +set_option maxHeartbeats 8000000 in +/-- **The receive core across `take_reply`'s cancel of a reply-blocked thread** (arm-agnostic): given the arm's core at +the state after the cancel, the core at the entry state (twin of `RecvPreA.cancelCoreA`). -/ +theorem RecvPreA.cancelCoreRA + (J : RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) (hnA : nA = hPl.ipa) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {rr p badge : U64} {rg send grant grantReply blk : Bool} (hrr : rr ∈ Rp) + {i : U64} (hi : L.mem s (Endp.rTcbA rr) = i) (hi0 : i ≠ 0#u64) (hit : i ≠ L.curW s) + (hiT : i ∈ Z.T) {xi : Tcb} (hki : σ.kheap (specPtr i) = some (.tcb xi)) + (hs : xi.tcbState = .blockedOnReply (specPtr rr)) + {Out1 : State Unit → State Unit → Prop} + (arm : ∀ (s1 : S) (σ1 : State Unit) (stack1 : U64 → List U64) (Q : U64 → U64 → List U64) (Rl : U64 → List U64) + (J1 : RecvPreA st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack1 κ n (J5A st) + (J7A st (J7R2A st ar nA)) s1 a σ1 m), + L.curW s1 = L.curW s → cancelIpc (Z := Unit) (specPtr i) σ = ({((), σ1)}, False) → + σ1 = { σ with kheap := σ1.kheap } → + (∀ q, q ≠ specPtr i → (∀ y n, σ.kheap q ≠ some (.schedContext y n)) → (∀ y, σ.kheap q ≠ some (.reply y)) → + σ1.kheap q = σ.kheap q) → + L.mem s1 (Endp.rTcbA rr) = 0#u64 → OSKeep st OS s s1 → + KeepT st H.tcbs Z.Cs H.replies H.scs H.ntfns i s s1 → + RecvCoreA st AR ar nA n Z.Cs s1 a m (.Endpoint p badge send true grant grantReply) blk (.Reply rr rg) + (absCapA (.Endpoint p badge send true grant grantReply)) (.replyCap (specPtr rr) (replyRightsA rg)) σ1 + (Out1 σ1)) : + RecvCoreA st AR ar nA n Z.Cs s a m (.Endpoint p badge send true grant grantReply) blk (.Reply rr rg) + (absCapA (.Endpoint p badge send true grant grantReply)) (.replyCap (specPtr rr) (replyRightsA rg)) σ + (fun σ' => ∃ σ1, cancelIpc (Z := Unit) (specPtr i) σ = ({((), σ1)}, False) ∧ Out1 σ1 σ') := by + obtain ⟨xr0, hkr0, hxr0⟩ := J.ep.reply rr hrr + have hback : xr0.replyTcb = some (specPtr i) := by rw [hxr0, hi, optRef_ne hi0] + obtain ⟨⟨rv, s1, a1⟩, htake, hrv, ha, σ1, stk, x1, Q, Rl, hcan, J1, hcw, hk1, hin, hoth, hfr, hz, osk, kp⟩ := + spec_existsA (J.takeReplyCancelRA hA hPl hnA hCs hmb hmax htu (rg := rg) (J.utIpc.reply rr hrr) hrr hi hi0 hit hiT + hki hs hkr0 hback) + dsimp only at hrv ha hcw + rw [hrv, ha] at htake + obtain ⟨z, hzk, hzn⟩ := hz + obtain ⟨xr1, hkr1, hxr1⟩ := J1.ep.reply rr hrr + rw [hzk] at hkr1 + obtain rfl := KernelObject.reply.inj (Option.some.inj hkr1) + have hfree1 : L.mem s1 (Endp.rTcbA rr) = 0#u64 := by + by_contra hne + rw [optRef_ne hne, hzn] at hxr1 + cases hxr1 + obtain ⟨s2, σ', hipc, hrecv, hout, hrel, hcur, hbody⟩ := arm s1 σ1 stk Q Rl J1 hcw hcan hfr hoth hfree1 osk kp + obtain ⟨q1, htake1, e1⟩ := spec_existsA (take_reply_plainA (s := s1) (a := a) (t := L.curW s) (R := Rp) + (rc' := .replyCap (specPtr rr) (replyRightsA rg)) + (Or.inr ⟨rg, _, rfl, rfl, hrr⟩ : ReplyCapA Rp (.Reply rr rg) (.replyCap (specPtr rr) (replyRightsA rg)) rr) + (fun _ _ => Or.inl hfree1)) + subst e1 + rw [hcw, receive_ipc_eqA, htake1, bind_ok] at hipc + have ct : σ.curThread = specPtr (L.curW s) := J.j.1.sch.curThread + have hxn : specPtr i ≠ specPtr (L.curW s) := fun h => hit (specPtr_inj h) + have hs1 : receiveIpc (Z := Unit) (specPtr (L.curW s)) (absCapA (.Endpoint p badge send true grant grantReply)) blk + (.replyCap (specPtr rr) (replyRightsA rg)) σ = + receiveIpc (Z := Unit) (specPtr (L.curW s)) (absCapA (.Endpoint p badge send true grant grantReply)) blk + (.replyCap (specPtr rr) (replyRightsA rg)) σ1 := by + rw [show absCapA (.Endpoint p badge send true grant grantReply) = + .endpointCap (specPtr p) badge.bv (rightsOfA send true grant grantReply) from rfl, + receiveIpc_cancelA badge.bv (rightsOfA send true grant grantReply) blk hkr0 hback hxn hcan, + receiveIpc_bodyA _ _ blk (Or.inr ⟨specPtr rr, _, z, rfl, rfl, hzk, Or.inl hzn⟩ : + SpecReplyPlainA σ1 (specPtr (L.curW s)) (.replyCap (specPtr rr) (replyRightsA rg)) (some (specPtr rr)))] + rw [hcw] at hrecv + refine ⟨s2, σ', ?_, ?_, ⟨σ1, hcan, hout⟩, hrel, hcur.trans hcw, hbody⟩ + · rw [receive_ipc_eqA, htake, bind_ok] + exact hipc + · rw [hs1] + exact hrecv + +/-- **What the arms need after the cancel of `i`** (module docstring). -/ +structure CancelXA (st : machine.Store S) [L : LawfulStore st] (Z : XSetsA) (OS E N : Set U64) (rr i : U64) (s s1 : S) + (σ σ1 : Fl4SpecA64.State Unit) : Prop where + fr : σ1 = { σ with kheap := σ1.kheap } + unl : L.mem s1 (Endp.rTcbA rr) = 0#u64 + osk : OSKeep st OS s s1 + tcb : ∀ q y, q ≠ specPtr i → σ.kheap q = some (.tcb y) → σ1.kheap q = some (.tcb y) + ntfn : ∀ q ∈ N, σ1.kheap (specPtr q) = σ.kheap (specPtr q) + epS : ∀ p ∈ E, ∀ l, σ.kheap (specPtr p) = some (.endpoint (.sendEP l)) → σ1.kheap (specPtr p) = σ.kheap (specPtr p) + epB : ∀ p x, σ.kheap (specPtr p) = some (.endpoint x) → CanBlockA true x → + ∃ x1, σ1.kheap (specPtr p) = some (.endpoint x1) ∧ CanBlockA true x1 + tw : ∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → K.val ≠ 3328 → K.val ≠ 3496 → K.val ≠ 3504 → + K.val ≠ 3376 → L.mem s1 (wa u K) = L.mem s (wa u K) + nsc : ∀ q ∈ N, L.mem s1 (Notif.nScA q) = L.mem s (Notif.nScA q) + +/-- **`CancelXA` after the cancel of a reply-blocked thread**. -/ +theorem CancelXA.ofReplyA {rr i : U64} {s1 : S} {σ1 : Fl4SpecA64.State Unit} {xi : Fl4SpecA64.Tcb} + (J : RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (hki : σ.kheap (specPtr i) = some (.tcb xi)) (hfr : σ1 = { σ with kheap := σ1.kheap }) + (hoth : ∀ q, q ≠ specPtr i → (∀ y n, σ.kheap q ≠ some (.schedContext y n)) → (∀ y, σ.kheap q ≠ some (.reply y)) → + σ1.kheap q = σ.kheap q) + (hfree : L.mem s1 (Endp.rTcbA rr) = 0#u64) (osk : OSKeep st OS s s1) + (kp : KeepT st H.tcbs Z.Cs H.replies H.scs H.ntfns i s s1) : + CancelXA st Z OS E N rr i s s1 σ σ1 where + fr := hfr + unl := hfree + osk := osk + tcb := fun q y hq hy => by + rw [hoth q hq (fun y' n c => by rw [hy] at c; cases c) (fun y' c => by rw [hy] at c; cases c)]; exact hy + ntfn := fun q hq => by + obtain ⟨x0, hkn0, -⟩ := J.ntfn.ntfn q hq + exact hoth _ (fun e => by rw [e, hki] at hkn0; cases hkn0) (fun y n c => by rw [hkn0] at c; cases c) + (fun y c => by rw [hkn0] at c; cases c) + epS := fun p hp l hk => hoth _ (fun e => by rw [e, hki] at hk; cases hk) (fun y n c => by rw [hk] at c; cases c) + (fun y c => by rw [hk] at c; cases c) + epB := fun p x hk hb => ⟨x, by + rw [hoth _ (fun e => by rw [e, hki] at hk; cases hk) (fun y n c => by rw [hk] at c; cases c) + (fun y c => by rw [hk] at c; cases c)]; exact hk, hb⟩ + tw := fun u hu K h1 h2 h5 h3 h4 h6 => kp.fld u (by rw [J.tcbs]; exact hu) K h1 h2 h3 h4 (fun _ => ⟨h5, h6⟩) + nsc := fun q hq => kp.nsc q (J.utIpc.ntfn q hq) + +set_option maxHeartbeats 4000000 in +/-- **`CancelXA` after the cancel of a receive-blocked thread** (`RecvPreA.cancelCoreA`'s callback arguments). -/ +theorem CancelXA.ofRecvA {rr i : U64} {s1 : S} {σ1 : Fl4SpecA64.State Unit} + (J : RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (hrr : rr ∈ Rp) (hiT : i ∈ Z.T) (hit : i ≠ L.curW s) {xi : Fl4SpecA64.Tcb} + (hki : σ.kheap (specPtr i) = some (.tcb xi)) {e' : ObjRef} {d : ReceiverPayload} + (hs : xi.tcbState = .blockedOnReceive e' (some (specPtr rr)) d) (hfr : σ1 = { σ with kheap := σ1.kheap }) + (hoth : ∀ q, q ≠ specPtr i → (∀ z, σ.kheap q ≠ some (.endpoint z)) → (∀ y, σ.kheap q ≠ some (.reply y)) → + σ1.kheap q = σ.kheap q) + (hqo : ∀ q, q ≠ e' → q ≠ specPtr rr → q ≠ specPtr i → σ1.kheap q = σ.kheap q) + (hfree : L.mem s1 (Endp.rTcbA rr) = 0#u64) (osk : OSKeep st OS s s1) {z0 : Endpoint} + (hz0 : σ.kheap e' = some (.endpoint z0)) (hz1 : σ1.kheap e' = some (.endpoint (epDeq z0 (specPtr i)))) + (hft : WritesIn st s s1 (CancelFootA st Rp Z.T s i)) : + CancelXA st Z OS E N rr i s s1 σ σ1 where + fr := hfr + unl := hfree + osk := osk + tcb := fun q y hq hy => by + rw [hoth q hq (fun z c => by rw [hy] at c; cases c) (fun y' c => by rw [hy] at c; cases c)]; exact hy + ntfn := fun q hq => by + obtain ⟨x0, hkn0, -⟩ := J.ntfn.ntfn q hq + exact hoth _ (fun e => by rw [e, hki] at hkn0; cases hkn0) (fun z c => by rw [hkn0] at c; cases c) + (fun y c => by rw [hkn0] at c; cases c) + epS := fun p hp l hk => by + obtain ⟨heE, z, hkz, hq⟩ := J.blkQ.ep i hiT xi hki e' (by rw [hs]; rfl) + obtain ⟨xr, hkr, -⟩ := J.ep.reply rr hrr + have hi3 : (L.mem s (stA i)).val = 3 := by rw [(J.ep.at_tcb hiT hki).st, hs]; rfl + have hpe : specPtr p ≠ e' := by + intro e + rw [← e, hk] at hkz + obtain rfl := KernelObject.endpoint.inj (Option.some.inj hkz) + obtain ⟨-, xs0, -, hq0, -, hB⟩ := J.ep.at_ep hp hk + have hmem : specPtr i ∈ l := hq + rw [hq0] at hmem + obtain ⟨y, hy, ey⟩ := List.mem_map.1 hmem + have := (hB y hy).1 + rw [specPtr_inj ey, hi3] at this + omega + exact hqo _ hpe (fun e => by rw [e, hkr] at hk; cases hk) (fun e => by rw [e, hki] at hk; cases hk) + epB := fun p x hk hb => by + obtain ⟨xr, hkr, -⟩ := J.ep.reply rr hrr + by_cases hpe : specPtr p = e' + · rw [hpe, hz0] at hk + obtain rfl := KernelObject.endpoint.inj (Option.some.inj hk) + refine ⟨_, by rw [hpe]; exact hz1, ?_⟩ + unfold epDeq + split + · exact trivial + · rw [epIns_eqA hb]; rfl + · exact ⟨x, by rw [hqo _ hpe (fun e => by rw [e, hkr] at hk; cases hk) (fun e => by rw [e, hki] at hk; cases hk)]; exact hk, hb⟩ + tw := fun u hu K h1 h2 h5 h3 h4 h6 => by + obtain ⟨heE, -⟩ := J.blkQ.ep i hiT xi hki e' (by rw [hs]; rfl) + exact hft.mem_eq (field_aligned (J.sr.core.tcbs u hu) h1 h2) + ((JStage3A.toBA J.toJStage3A).cancelFoot_tcbOffA heE hiT hu h1 ⟨h5, h3, h4⟩ h6) + nsc := fun q hq => by + obtain ⟨heE, -⟩ := J.blkQ.ep i hiT xi hki e' (by rw [hs]; rfl) + have hna := J.iplaced.n.ntfn q hq + have hv : (wa q 32#u64).val = q.val + 32 := Notif.nfield_val hna (by simp) + rw [Notif.nScA_eq] + exact hft.mem_eq (Notif.nfield_aligned hna (by simp) (by simp)) + ((JStage3A.toBA J.toJStage3A).cancelFoot_ntfnOffA heE hiT hq (by omega) (by omega)) + +set_option maxHeartbeats 4000000 in +/-- **The receive core across `take_reply`'s cancel, whichever thread the reply is linked to** (the thread is tracked, +not the current one, and receive-blocked on the reply or blocked on it: `RecvPreA.replyLinkA`): the arm gets the cancel's +`CancelXA`. -/ +theorem RecvPreA.cancelCoreXA + (J : RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) (hnA : nA = hPl.ipa) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {rr p badge : U64} {rg send grant grantReply blk : Bool} (hrr : rr ∈ Rp) + (hlnk : L.mem s (Endp.rTcbA rr) ≠ 0#u64) + {Out1 : Fl4SpecA64.State Unit → Fl4SpecA64.State Unit → Prop} + (arm : ∀ (i : U64), L.mem s (Endp.rTcbA rr) = i → i ≠ 0#u64 → i ≠ L.curW s → i ∈ Z.T → + (L.mem s (Fl4ProofsA64.P1.stA i)).val ≠ 4 → + ∀ (s1 : S) (σ1 : Fl4SpecA64.State Unit) (stack1 : U64 → List U64) (Q : U64 → U64 → List U64) + (Rl : U64 → List U64) + (J1 : RecvPreA st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack1 κ n (J5A st) + (J7A st (J7R2A st ar nA)) s1 a σ1 m), + L.curW s1 = L.curW s → cancelIpc (Z := Unit) (specPtr i) σ = ({((), σ1)}, False) → + CancelXA st Z OS E N rr i s s1 σ σ1 → + RecvCoreA st AR ar nA n Z.Cs s1 a m (.Endpoint p badge send true grant grantReply) blk (.Reply rr rg) + (absCapA (.Endpoint p badge send true grant grantReply)) (.replyCap (specPtr rr) (replyRightsA rg)) σ1 + (Out1 σ1)) : + RecvCoreA st AR ar nA n Z.Cs s a m (.Endpoint p badge send true grant grantReply) blk (.Reply rr rg) + (absCapA (.Endpoint p badge send true grant grantReply)) (.replyCap (specPtr rr) (replyRightsA rg)) σ + (fun σ' => ∃ i σ1, cancelIpc (Z := Unit) (specPtr i) σ = ({((), σ1)}, False) ∧ Out1 σ1 σ') := by + obtain ⟨hiT, hit, xi, hki, hcase⟩ := J.replyLinkA hrr hlnk + obtain ⟨i, hi⟩ : ∃ i, L.mem s (Endp.rTcbA rr) = i := ⟨_, rfl⟩ + rw [hi] at hiT hit hki + rw [hi] at hlnk + have hrel := (J.ep.at_tcb hiT hki).st + rcases hcase with ⟨e', d, hs⟩ | hs + · have hi4 : (L.mem s (Fl4ProofsA64.P1.stA i)).val ≠ 4 := by rw [hrel, hs]; simp [tsCode] + exact (RecvPreA.cancelCoreA J hA hPl hnA hCs hmb hmax htu (p := p) (badge := badge) (send := send) + (grant := grant) (grantReply := grantReply) (blk := blk) (rg := rg) hrr hi hlnk hit hiT hki hs (Out1 := Out1) + (fun s1 σ1 Q Rl J1 hcw hcan hfr hoth hfree1 osk z0 hz0 hz1 hqo hft => + arm i hi hlnk hit hiT hi4 s1 σ1 stack Q Rl J1 hcw hcan + (CancelXA.ofRecvA J hrr hiT hit hki hs hfr hoth hqo hfree1 osk hz0 hz1 hft))).mono + (fun σ' ⟨σ1, hcan, h⟩ => ⟨i, σ1, hcan, h⟩) + · have hi4 : (L.mem s (Fl4ProofsA64.P1.stA i)).val ≠ 4 := by rw [hrel, hs]; simp [tsCode] + exact (RecvPreA.cancelCoreRA J hA hPl hnA hCs hmb hmax htu (p := p) (badge := badge) (send := send) + (grant := grant) (grantReply := grantReply) (blk := blk) (rg := rg) hrr hi hlnk hit hiT hki hs (Out1 := Out1) + (fun s1 σ1 stack1 Q Rl J1 hcw hcan hfr hoth hfree1 osk kp => + arm i hi hlnk hit hiT hi4 s1 σ1 stack1 Q Rl J1 hcw hcan (CancelXA.ofReplyA J hki hfr hoth hfree1 osk kp))).mono + (fun σ' ⟨σ1, hcan, h⟩ => ⟨i, σ1, hcan, h⟩) + +/-- `recvBlockRAnyA`'s outcome. -/ +def RAnyOutA (p rr : U64) (x : Endpoint) (σ σ' : State Unit) : Prop := + (∃ y xr, σ.kheap σ.curThread = some (.tcb y) ∧ σ.kheap (specPtr rr) = some (.reply xr) ∧ + σ' = blkRecvRA σ σ.curThread (specPtr p) (specPtr rr) x xr y) ∨ + (∃ y xr sc xs ns, σ.kheap σ.curThread = some (.tcb y) ∧ σ.kheap (specPtr rr) = some (.reply xr) ∧ + y.tcbSchedContext = some sc ∧ σ.kheap sc = some (.schedContext xs ns) ∧ + σ' = blkRecvRA (unbWaitA (preschedA σ) σ.curThread sc xs ns y) σ.curThread (specPtr p) (specPtr rr) x xr + { y with tcbSchedContext := none }) + +set_option maxHeartbeats 8000000 in +/-- **Blocking `Recv` with a reply object, the reply possibly still linked to a receive-blocked thread** (the reply may be linked to any +thread): `recvBlockRAnyA` when unlinked, else the arm after `take_reply`'s cancel. -/ +theorem RecvPreA.recvBlockRAnyA_repA + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p badge slot rr rslot : U64} {send grant grantReply rg : Bool} + (hcap : cspace.lookup_cap st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.CAP)) = + ok (.Ok (.Endpoint p badge send true grant grantReply))) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) + (hsc : capA st s slot = .Endpoint p badge send true grant grantReply) + (hcapR : cspace.lookup_cap st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.REPLY)) = ok (.Ok (.Reply rr rg))) + (hrslot : rslot ∈ OS) (hrsc : capA st s rslot = .Reply rr rg) (hrr : rr ∈ Rp) + (hp : p ∈ E) {x : Endpoint} (hk : σ.kheap (specPtr p) = some (.endpoint x)) (hcb : CanBlockA true x) + (hbn : ∀ y, σ.kheap σ.curThread = some (.tcb y) → BndOKA N (L.mem s) σ (L.curW s) y false) : + HRecvPostA st AR ar hPl.ipa n Z.Cs s a m true true σ + (fun σ' => RAnyOutA p rr x σ σ' ∨ + ∃ σ1, ∃ i : U64, cancelIpc (Z := Unit) (specPtr i) σ = ({((), σ1)}, False) ∧ + ∃ x1, σ1.kheap (specPtr p) = some (.endpoint x1) ∧ RAnyOutA p rr x1 σ1 σ') := by + by_cases h0 : L.mem s (Endp.rTcbA rr) = 0#u64 + · exact (J.recvBlockRAnyA hA hPl hCs h1 hmb hmax htu hcap hslot hcov hsc hcapR hrslot hrsc hrr h0 hp hk hcb + hbn).monoA (fun _ h => Or.inl h) + · have ct : σ.curThread = specPtr (L.curW s) := J.j.1.sch.curThread + have hcT : L.curW s ∈ Z.T := J.sr.curT + obtain ⟨xc, hk0, -⟩ := J.ep.tcb (L.curW s) hcT + have hkc : σ.kheap σ.curThread = some (.tcb xc) := by rw [ct]; exact hk0 + obtain ⟨xr, hkr, -⟩ := J.ep.reply rr hrr + have core := RecvPreA.cancelCoreXA J hA hPl rfl hCs hmb hmax htu (p := p) (badge := badge) (send := send) + (grant := grant) (grantReply := grantReply) (blk := true) (rg := rg) hrr h0 + (Out1 := fun σ1 σ' => ∃ x1, σ1.kheap (specPtr p) = some (.endpoint x1) ∧ RAnyOutA p rr x1 σ1 σ') + (fun i hi hi0 hit hiT hi4 s1 σ1 stk Q Rl J1 hcw hcan X => ?_) + · refine (RecvPreA.recvFrontA J hcap hcapR core).monoA (fun σ' h => Or.inr ?_) + obtain ⟨i, σ1, hcan, hh⟩ := h + exact ⟨σ1, i, hcan, hh⟩ + · have hct1 : σ1.curThread = σ.curThread := by + have h := congrArg (fun τ : State Unit => τ.curThread) X.fr + exact h + have hcT1 : L.curW s1 ∈ Z.T := by rw [hcw]; exact hcT + have hkc1 : σ1.kheap σ1.curThread = some (.tcb xc) := by + rw [hct1, X.tcb _ xc (fun e => hit (specPtr_inj (ct.symm.trans e)).symm) hkc] + have ct1 : σ1.curThread = specPtr (L.curW s1) := J1.j.1.sch.curThread + obtain ⟨x1, hk1, hcb1⟩ := X.epB p x hk hcb + have hbn1 : ∀ y, σ1.kheap σ1.curThread = some (.tcb y) → + BndOKA N (L.mem s1) σ1 (L.curW s1) y false := by + intro y hy + rw [hkc1] at hy + obtain rfl := KernelObject.tcb.inj (Option.some.inj hy) + rcases hbn xc hkc with hb | ⟨nn, hn, hb, hx⟩ + · exact Or.inl hb + · refine Or.inr ⟨nn, hn, hb, fun xn hxn => ?_⟩ + rw [X.ntfn nn hn] at hxn + exact ⟨(hx xn hxn).1, fun h => by cases h⟩ + have hcm1 : m.curCore < fl4A64Platform.numNodes := by + rw [J1.j.1.sch.core]; exact hCs _ J1.j.1.sch.cpuCs + have hkt1 : σ1.kheap (specPtr (L.curW s1)) = some (.tcb xc) := by rw [← ct1]; exact hkc1 + have hcov1 := SlotCov.keepA X.osk hcov + have hsc1 : capA st s1 slot = .Endpoint p badge send true grant grantReply := by + rw [X.osk.capA hslot]; exact hsc + have hrsc1 : capA st s1 rslot = .Reply rr rg := by rw [X.osk.capA hrslot]; exact hrsc + rcases J1.toJStage3A.bndOK_blockA hcm1 hcT1 hkt1 (hbn1 xc hkc1) with hb | hb + · exact (RecvPreA.recvBlockRA_core hA hPl J1 hCs h1 hmb hmax htu hslot hcov1 hsc1 hrslot hrsc1 hrr X.unl hp hk1 + hcb1 (fun y hy => by rw [hkc1] at hy; obtain rfl := KernelObject.tcb.inj (Option.some.inj hy); exact hb)).mono + (fun σ' h => ⟨x1, hk1, Or.inl h⟩) + · exact (RecvPreA.recvBlockRUnbA_core hA hPl J1 hCs h1 hmb hmax htu hslot hcov1 hsc1 hrslot hrsc1 hrr X.unl hp + hk1 hcb1 (fun y hy => by rw [hkc1] at hy; obtain rfl := KernelObject.tcb.inj (Option.some.inj hy); exact hb)).mono + (fun σ' h => ⟨x1, hk1, Or.inr h⟩) + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3RecvCapFaultA.lean b/proofs/Fl4ProofsA64/P3/Stage3RecvCapFaultA.lean new file mode 100644 index 000000000..088907973 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3RecvCapFaultA.lean @@ -0,0 +1,146 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2c): **`handle_recv` whose endpoint lookup fails, for a thread without a fault +handler**. + +`handle_recv` reads the CAP register and looks the cap up; a failed CSpace walk (`Err (Lookup lf)`) is delivered as the +cap fault `Fault::Cap { addr = CAP register, in_recv = true, lf }` to the current thread's fault handler +(`cap_fault_recv`; spec: `capFaultOnFailure ... true (lookupCap ...)` caught by `handleFault thread`). + +* **`JSyscallA.recvCapFaultNullA`**: the handler cap is `Null`: nothing is stored (`handle_fault` without a handler does + not call `set_fault`), the thread becomes `Inactive` and the scheduler action `chooseNewThread` + (`JSyscallA.faultNullExA`, which is `faultNullA` with its post state). + +[S] the lookup fails with a lookup fault; the handler slot is `Null` (the spec's `getCap` agrees, `hgc`). +Not covered (needs `set_fault` of a cap fault, which also stores the three lookup-failure words `tLfA`, `tLf0A`, `tLf1A` +outside `FaultWA`, so the endpoint-handler arms `faultRecvA` / `faultDonateA` / `faultBlockA` do not apply): a handler +endpoint. Also not covered here: a found cap that is not a receivable endpoint / notification (same fault with +`MissingCapability 0`) and a failed REPLY lookup. +[P] `ArchTotal`, `ArchPlat`, `hCs`, `h1`. New module. +-/ +import Fl4ProofsA64.P3.Stage3RecvPollA +import Fl4ProofsA64.P3.Stage3FaultArmA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4Spec Fl4SpecA64 + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {s : S} {a : A} {σ : State Unit} {m : McState} + +private theorem recvLookupFaultCA {m : LFMonad Unit Cap} {σ : State Unit} {e : LookupFailure} + (h : m.run σ = ({(.error e, σ)}, False)) (p : Word) : + (capFaultOnFailure p true m).run σ = ({(.error (.capFault p true e), σ)}, False) := by + unfold capFaultOnFailure + rw [run_handleE_errA h] + rfl + +set_option maxHeartbeats 2000000 in +/-- **`handle_recv` with a failed endpoint lookup and no fault handler** (module docstring). -/ +theorem RecvPreA.recvCapFaultNullA + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + {blocking canReply : Bool} {lf : error.LookupFault} + (hcap : cspace.lookup_cap st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.CAP)) = + ok (.Err (.Lookup lf))) + (hnull : capA st s (slotIdx (L.curW s) layout.tcb.SLOT_FAULT_HANDLER) = .Null) + (hgc : getCap (Z := Unit) (getTcbFaultHandlerPtr σ.curThread) σ = ({(.nullCap, σ)}, False)) : + HRecvPostA st AR ar hPl.ipa n Z.Cs s a m blocking canReply σ + (fun σ' => ∃ x, σ.kheap σ.curThread = some (.tcb x) ∧ + σ' = stsAct (updK σ σ.curThread (.tcb { x with tcbState := .inactive })) σ.curThread) := by + have htZ : L.curW s ∈ Z.T := J.sr.curT + set t := L.curW s with ht_def + have ct : σ.curThread = specPtr t := J.j.1.sch.curThread + obtain ⟨xt, hkt, r⟩ := J.ep.tcb t htZ + have hkc : σ.kheap σ.curThread = some (.tcb xt) := by rw [ct]; exact hkt + have hthT : Z.th t = some .tcb := (J.sr.thT t).2 htZ + set cp := L.mem s (regWA t layout.reg.CAP) with hcp + -- the spec lookup fails too + have l := post_of_okA (lookup_cap_recv_refinesA (st := st) _ s σ J.j.1.st J.j.1.cv t hthT cp) hcap + obtain ⟨e, he, hrel⟩ := l + cases e with + | ok _ => exact hrel.elim + | error fault => + obtain ⟨σ2, s2, x, hx, hσ2, hact, -, hsw, hspec, hfault, Q, Rl, J2⟩ := + J.faultNullExA hA hPl hCs h1 (.Cap cp true lf) (.capFault cp.bv true fault) hnull hgc + rw [ct] at hspec + have capReg : cp.bv = xt.tcbArch.tcbContext.userRegs cap_register := by + rw [hcp, ← r.regs cap_register, regIdxA_capA]; rfl + have capRead : NonDet.liftM dataToCptr (asUser (Z := Unit) (specPtr t) (getRegister cap_register)) σ = + ({(toBl cp.bv, σ)}, False) := by + unfold NonDet.liftM + rw [nd_bind_singleA' (asUser_getRegister_eqA hkt _), ← capReg] + rfl + have hmem : ((), σ2) ∈ (handleRecv (Z := Unit) blocking canReply σ).1 := by + rw [handleRecv, nd_gets_bindSc, ct, nd_bind_oneSc capRead] + apply mem_bindA.2 + refine ⟨(.error (.capFault cp.bv true fault), σ), ?_, hspec⟩ + simp only [ofBl_toBlA] + rw [run_bind_errorA (recvLookupFaultCA he cp.bv)] + rfl + have hfl : syscall.handle_recv st ar s a blocking canReply = ok (s2, a) := by + have e1 : syscall.handle_recv st ar s a blocking canReply = + faults.handle_fault st ar s a t (.Cap cp true lf) := by + unfold syscall.handle_recv + rw [cur_thread_ok, bind_ok, tcb_reg_eqA, bind_ok] + rw [show L.curW s = t from rfl, hcap] + simp only [bind_ok] + unfold syscall.cap_fault_recv + rw [cur_thread_ok, bind_ok] + rw [e1] + exact hfault + have fld : ∀ (π : State Unit) (u : ObjRef), + (stsAct π u).curTime = π.curTime ∧ (stsAct π u).machineState.lastMachineTime = π.machineState.lastMachineTime ∧ + (stsAct π u).curThread = π.curThread ∧ (stsAct π u).curSc = π.curSc ∧ + (stsAct π u).consumedTime = π.consumedTime ∧ (stsAct π u).releaseQueue = π.releaseQueue := by + intro π u + unfold stsAct + split_ifs <;> exact ⟨rfl, rfl, rfl, rfl, rfl, rfl⟩ + have fU : (updK σ σ.curThread (.tcb { x with tcbState := .inactive })).curTime = σ.curTime ∧ + (updK σ σ.curThread (.tcb { x with tcbState := .inactive })).machineState.lastMachineTime = + σ.machineState.lastMachineTime ∧ + (updK σ σ.curThread (.tcb { x with tcbState := .inactive })).curThread = σ.curThread ∧ + (updK σ σ.curThread (.tcb { x with tcbState := .inactive })).curSc = σ.curSc ∧ + (updK σ σ.curThread (.tcb { x with tcbState := .inactive })).consumedTime = σ.consumedTime ∧ + (updK σ σ.curThread (.tcb { x with tcbState := .inactive })).releaseQueue = σ.releaseQueue := + ⟨rfl, rfl, rfl, rfl, rfl, rfl⟩ + obtain ⟨f1, f2, f3, f4, f5, f6⟩ := fld (updK σ σ.curThread (.tcb { x with tcbState := .inactive })) σ.curThread + rw [← hσ2] at f1 f2 f3 f4 f5 f6 + have hthr : σ2.curThread = σ.curThread := f3.trans fU.2.2.1 + refine ⟨s2, a, σ2, hfl, hmem, ⟨x, hx, hσ2⟩, A64RelS3.ofJ J2 ?bo, curW_keptA J.toJStage3A J2 hthr, ?bo⟩ + refine J.body.stepEq hsw (f1.trans fU.1) (f2.trans fU.2.1) hthr + (f4.trans fU.2.2.2.1) (f5.trans fU.2.2.2.2.1) (f6.trans fU.2.2.2.2.2) (fun y' k h => ?_) + (fun h => absurd h hact) + have ne : σ.curSc ≠ σ.curThread := fun e => by + rw [e, hσ2, stsAct_kheap, updK_kheap, Function.update_self] at h + simp at h + rw [hσ2, stsAct_kheap, updK_kheap, Function.update_of_ne ne] at h + exact ⟨y', h, rfl, rfl, rfl⟩ + +set_option maxHeartbeats 1000000 in +/-- **`RecvPreA.recvCapFaultNullA` at a syscall entry.** -/ +theorem JSyscallA.recvCapFaultNullA + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + {blocking canReply : Bool} {lf : error.LookupFault} + (hcap : cspace.lookup_cap st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.CAP)) = + ok (.Err (.Lookup lf))) + (hnull : capA st s (slotIdx (L.curW s) layout.tcb.SLOT_FAULT_HANDLER) = .Null) + (hgc : getCap (Z := Unit) (getTcbFaultHandlerPtr σ.curThread) σ = ({(.nullCap, σ)}, False)) : + HRecvPostA st AR ar hPl.ipa n Z.Cs s a m blocking canReply σ + (fun σ' => ∃ x, σ.kheap σ.curThread = some (.tcb x) ∧ + σ' = stsAct (updK σ σ.curThread (.tcb { x with tcbState := .inactive })) σ.curThread) := + RecvPreA.recvCapFaultNullA hA hPl J.toRecvPreA hCs h1 hcap hnull hgc + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3RecvDonA.lean b/proofs/Fl4ProofsA64/P3/Stage3RecvDonA.lean new file mode 100644 index 000000000..af3978940 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3RecvDonA.lean @@ -0,0 +1,143 @@ +/- +P3 (aarch64, A64-P3 line RecvPreA): **the record across an active receive's notification write followed by the +receive tail** (`maybe_donate_sc` with the notification's SC, `sched_context_resume`, the sporadic unblock check). + +* **`JStage3A.of_recvDonA`**: `JStage3A` at the tail's end state, from the record before the write, the write + (`ntfnObj_factsA`), the tail's frame (`MFrA`), the scheduler relation and spec invariants at the end, and the P1 facts + there. + +Shared by `waitActiveA` (`recv_tail_frameA`) and `recvBoundActiveA` (`complete_tail_frameA`). No named premise. +New module. +-/ +import Fl4ProofsA64.P3.Stage3SignalIdleA +import Fl4ProofsA64.P3.Stage3WkKeepA +import Fl4ProofsA64.P3.CancelIpcReplyProgA +import Fl4ProofsA64.P3.IpcScGlueA +import Fl4ProofsA64.P3.Stage3SigWakeRecA +import Fl4ProofsA64.P3.Stage3SendNarrowA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s s2 s3 : S} {a : A} {σ σ3 : State Unit} {m : McState} + +set_option maxHeartbeats 2000000 in +/-- **The record across a notification write and the receive tail** (module docstring). -/ +theorem JStage3A.of_recvDonA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (hc : m.curCore < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p : U64} (hp : p ∈ N) {x : Notification} (hk : σ.kheap (specPtr p) = some (.notification x)) + (hq : ∀ q, x.ntfnObj ≠ .waitingNtfn q) {x' : Notification} (hsub : x'.ntfnObj.ntfnQueue = []) + (hbt : x'.ntfnBoundTcb = x.ntfnBoundTcb) (hsc : x'.ntfnSc = x.ntfnSc) + (w : WritesIn st s s2 (NObjW p)) + (R2 : NtfnStateRel N Z.T (L.mem s2) (updK σ (specPtr p) (.notification x'))) + {Q1 : U64 → U64 → List U64} {Rl1 R' : U64 → List U64} + (F : MFrA (st := st) { Z with Q := Q1, R := Rl1 } m s2 s3 (updK σ (specPtr p) (.notification x')) σ3) + (sr3 : SchedSRA st { Z with Q := Q1, R := R' } m κ s3 σ3) + (SF : SchedFrameA (updK σ (specPtr p) (.notification x')) σ3) + (Qk3 : QKeepA (updK σ (specPtr p) (.notification x')) σ3) + (Wk : WkKeepA (updK σ (specPtr p) (.notification x')) σ3) + (hL : LoopInvA σ3 m n) (sv3 : SchedValidA σ3) (rv2 : RunVcpuInv st Z.T s2) + (bxi : BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s3) + (sysl : SysL st H Z.Cs OS ia ie s3) (symL : SymL st H Z.Cs s3) + (userOK : Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s3) + (j7 : J7A st (J7R2A st ar nA) Z H OS s3 σ3 m) : + JStage3A st AR { Z with Q := Q1, R := R' } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s3 a σ3 m := by + obtain ⟨-, HS2, SSz2, Qk2, ep2, reply2, K2, tcbW, -, -, blkQ2, -⟩ := + J.ntfnObj_factsA hc hmb hmax htu hp hk hq hsub hbt hsc w R2 + set σ2 := updK σ (specPtr p) (.notification x') with hσ2 + have hP := J.iplaced + have back : ∀ b, MWrA (st := st) { Z with Q := Q1, R := Rl1 } m σ2 s2 b → MWrA (st := st) Z m σ s b := + fun b h => mwr_back (Z := Z) (Z' := { Z with Q := Q1, R := Rl1 }) rfl rfl SSz2 w.1 h + have keep23 : ∀ b, Aligned b → ¬ MWrA (st := st) Z m σ s b → L.mem s3 b = L.mem s2 b := + fun b hb h => F.wr.mem_eq hb fun c => h (back b c) + have tw23 : ∀ t ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → + (K.val ≠ 3464 ∧ K.val ≠ 3480 ∧ K.val ≠ 3488 ∧ K.val ≠ 3592) → L.mem s3 (wa t K) = L.mem s2 (wa t K) := + fun t ht K k1 k2 k3 => keep23 _ (field_aligned (J.sr.core.tcbs t ht) k1 k2) (J.tcbWord_offMwrA ht k1 k3) + have hN : ∀ n' ∈ N, Z.th n' = some (.other 64) := J.ipcPlace.notification + have ntfn : NtfnStateRel N Z.T (L.mem s3) σ3 := + R2.frame hP.n (fun b hb hf => keep23 b hb (notifFootOffMWrA J.sr.toSchedSRN hN hf)) (notifKView F.keep) + have ep : EpStateRel Z.T E Rp Bf (L.mem s3) σ3 := + ep2.frame hP.e (fun b hb hf => keep23 b hb (endpFootOffWrite J.sr.toSchedSRN J.ipcPlace hf)) + (endpKView F.keep SF.2.2) + have NK : NtfnSpecKeepA σ2 σ3 := Wk.1 + have reply : ReplyStackRelA (L.mem s3) Rp Z.C σ3 (scNameA m) stack := by + refine reply2.ntfnFr NK fun r' hr' => ?_ + obtain ⟨r32, -, rfit⟩ := hP.e.rep r' hr' + have one : ∀ o : U64, o.val < 32 → o.val % 8 = 0 → L.mem s3 (wa r' o) = L.mem s2 (wa r' o) := by + intro o o1 o2 + have v : (wa r' o).val = r'.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + refine keep23 _ ?_ (replyWordOffSchedA J.sr.toSchedSRN J.ipcPlace hr' (by omega) (by omega)).2 + show (wa r' o).val % 8 = 0 + omega + have n16 : (layout.reply.NEXT).val = 16 := by unfold layout.reply.NEXT; rfl + have p8 : (layout.reply.PREV).val = 8 := by unfold layout.reply.PREV; rfl + exact ⟨one _ (by omega) (by omega), one _ (by omega) (by omega)⟩ + have K23 : IpcKeepA σ2 σ3 := by + refine ⟨fun t xq h => ?_, fun p0 y k h => ?_, fun p0 y' k' h => ?_⟩ + · obtain ⟨y, hy, e⟩ := Wk.2.2.2.1 t xq h + refine ⟨y, hy, ?_⟩ + rw [e] + exact ⟨rfl, rfl, rfl, rfl, rfl, rfl⟩ + · obtain ⟨z, hz, -, k2⟩ := NK.sc h + exact ⟨z, k, hz, k2⟩ + · obtain ⟨z, hz, -, k2⟩ := NK.sc_back h + exact ⟨z, k', hz, k2⟩ + have K : IpcKeepA σ σ3 := by + refine ⟨fun t xq h => ?_, fun p0 y k h => ?_, fun p0 y' k' h => ?_⟩ + · obtain ⟨y, hy, a1, a2, a3, a4, a5, a6⟩ := K2.tcb t xq h + obtain ⟨z, hz, b1, b2, b3, b4, b5, b6⟩ := K23.tcb t y hy + exact ⟨z, hz, b1.trans a1, b2.trans a2, b3.trans a3, b4.trans a4, b5.trans a5, b6.trans a6⟩ + · obtain ⟨y1, k1, h1, e1⟩ := K2.scF p0 y k h + obtain ⟨y2, k2, h2, e2⟩ := K23.scF p0 y1 k1 h1 + exact ⟨y2, k2, h2, e2.trans e1⟩ + · obtain ⟨y1, k1, h1, e1⟩ := K23.scB p0 y' k' h + obtain ⟨y2, k2, h2, e2⟩ := K2.scB p0 y1 k1 h1 + exact ⟨y2, k2, h2, e1.trans e2⟩ + have blkQ : BlkQueuedA Z.T E N (L.mem s3) σ3 := by + refine blkQ2.keepA (fun t ht => ?_) (fun t _ x3 hx3 => Or.inr ?_) + (fun q y h => Wk.2.1 q _ h (fun _ _ e => by cases e) (fun _ e => by cases e)) + (fun q y h => Wk.2.1 q _ h (fun _ _ e => by cases e) (fun _ e => by cases e)) + · rw [Notif.tBlockA_eq]; exact tw23 t ht 3336#u64 (by simp) (by simp) (by simp) + · obtain ⟨x2, hx2⟩ := Wk.2.2.2.2.1 _ _ hx3 + obtain ⟨y, hy, e⟩ := Wk.2.2.2.1 _ _ hx2 + rw [hx3] at hy + cases hy + exact ⟨x2, hx2, by rw [e]⟩ + have rv : RunVcpuInv st Z.T s3 := fun t ht h => by + have e1 : L.mem s3 (runVcpuA t) = L.mem s2 (runVcpuA t) := + tw23 t ht 3672#u64 (by simp) (by simp) (by simp) + have e2 : L.mem s3 (stA t) = L.mem s2 (stA t) := by + rw [stA_eq]; exact tw23 t ht 3328#u64 (by simp) (by simp) (by simp) + rw [e2] + exact rv2 t ht (e1 ▸ h) + have wN : WritesIn st s s3 (SchedStepNWA (st := st) N E Rp Z m σ s) := + (w.trans F.wr).mono fun b h => h.elim + (fun h => Or.inr (Or.inl ⟨p, hp, h.1, by unfold NObjW at h; omega⟩)) + (fun h => Or.inl (Or.inl (back b h))) + have SF2 : SchedFrameA σ σ2 := ⟨HS2, SSz2, rfl⟩ + have SF3 : SchedFrameA σ σ3 := PreO.trans SF2 SF + exact J.of_ipcStepA (Q := Q1) (Rl := R') (curOf := Z.curOf) (curScOf := Z.curScOf) wN SF3.1 SF3.2.1 (Qk2.trans Qk3) + SF3.2.2 (fun _ _ => rfl) bxi sysl symL userOK j7 sr3 ep ntfn reply K + (fun t ht O h h8 => (tw23 t ht O (by omega) h8 (by omega)).trans (tcbW t ht O (by omega) h8)) + hL.sinv hL.sidle rv blkQ sv3 + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3RecvDonFrameA.lean b/proofs/Fl4ProofsA64/P3/Stage3RecvDonFrameA.lean new file mode 100644 index 000000000..b27124571 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3RecvDonFrameA.lean @@ -0,0 +1,257 @@ +/- +P3 (aarch64, A64-P3 Stage 3, RecvPreA line): **`maybe_donate_sc` leaves an SC other than the notification's alone**. + +`DKA p σ σ'`: the scalars of the budget frame (`BScalA`) and every SC other than the one at `p` is kept (TCB writes +and queue edits are allowed). Instances in the style of `P3/Stage3SigBudgetA` (`KPresBInst`): the writers are +`threadSet` and `updateSchedContext p`. `maybeDonateSc_keepScA`: the spec donate-and-resume keeps the heap entry of an +SC `c` that is not the SC of the notification (the `scTcb` of the current SC across a donating receive tail). + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3SigBudgetA +import Fl4ProofsA64.P3.Stage3WakeInvA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec Classical Fl4ProofsA64.P3 + +noncomputable section + +/-- Scalars kept and every SC other than `p` kept. -/ +structure DKA (p : ObjRef) (σ σ' : State Unit) : Prop extends BScalA σ σ' where + other : ∀ q, q ≠ p → ∀ y n, σ.kheap q = some (.schedContext y n) → σ'.kheap q = σ.kheap q + +instance (p : ObjRef) : PreO (DKA p) where + refl σ := ⟨BScalA.refl σ, fun _ _ _ _ _ => rfl⟩ + trans h1 h2 := ⟨h1.toBScalA.trans h2.toBScalA, fun q hq y n h => + have e1 := h1.other q hq y n h + (h2.other q hq y n (by rw [e1]; exact h)).trans e1⟩ + +theorem DKA.of_kheap {p : ObjRef} {σ σ' : State Unit} (s : BScalA σ σ') (hk : σ'.kheap = σ.kheap) : DKA p σ σ' := + ⟨s, fun q _ _ _ _ => by rw [hk]⟩ + +/-- One object replaced: not an SC, or the SC at `p`. -/ +theorem DKA.upd {σ : State Unit} {p ptr : ObjRef} {o : KernelObject} + (h : ptr ≠ p → ∀ y n, σ.kheap ptr ≠ some (.schedContext y n)) : + DKA p σ { σ with kheap := Function.update σ.kheap ptr (some o) } := by + refine ⟨⟨rfl, rfl, rfl, rfl, rfl, rfl, fun h => h⟩, fun q hq y n hs => ?_⟩ + by_cases e : ptr = q + · subst e; exact absurd hs (h hq y n) + · exact Function.update_of_ne (fun h => e h.symm) _ _ + +variable {p : ObjRef} + +abbrev DKPres (p : ObjRef) {α : Type} (m : SMonad Unit α) : Prop := PresR (DKA p) m + +class DKInst (p : ObjRef) {α : Type} (m : SMonad Unit α) : Prop where + pres : DKPres p m + + +instance {α β : Type} {m : SMonad Unit α} {f : α → SMonad Unit β} [hm : DKInst p m] + [hf : ∀ a, DKInst p (f a)] : DKInst p (m >>= f) := + ⟨PresR.bind hm.pres fun a => (hf a).pres⟩ + +instance {α : Type} (a : α) : DKInst p (pure a : SMonad Unit α) := ⟨PresR.pure a⟩ + +instance {α : Type} (f : State Unit → α) : DKInst p (NonDet.gets f : SMonad Unit α) := ⟨PresR.gets f⟩ + +instance : DKInst p (NonDet.get : SMonad Unit (State Unit)) := ⟨PresR.get⟩ + +instance {α : Type} (f : State Unit → Option α) : DKInst p (NonDet.getsThe f : SMonad Unit α) := + ⟨PresR.getsThe f⟩ + +instance {α : Type} (o : Option α) : DKInst p (NonDet.assertOpt o : SMonad Unit α) := ⟨PresR.assertOpt o⟩ + +instance (P : Prop) : DKInst p (NonDet.assert P : SMonad Unit Unit) := ⟨PresR.assert P⟩ + +instance {α : Type} : DKInst p (NonDet.fail : SMonad Unit α) := ⟨PresR.fail⟩ + +instance {P : Prop} {d : Decidable P} {m : SMonad Unit Unit} [h : DKInst p m] : + DKInst p (@NonDet.when (State Unit) P d m) := ⟨PresR.when h.pres⟩ + +instance {P : Prop} {d : Decidable P} {m : SMonad Unit Unit} [h : DKInst p m] : + DKInst p (@NonDet.unless (State Unit) P d m) := ⟨PresR.unless' h.pres⟩ + +instance {α : Type} {P : Prop} {d : Decidable P} {a b : SMonad Unit α} [ha : DKInst p a] [hb : DKInst p b] : + DKInst p (@ite _ P d a b) := ⟨PresR.ite' ha.pres hb.pres⟩ + +instance {β γ : Type} {f : γ → SMonad Unit β} [hf : ∀ a, DKInst p (f a)] (l : List γ) : + DKInst p (NonDet.mapM f l) := ⟨PresR.mapM (fun a => (hf a).pres) l⟩ + +instance {α : Type} {C : α → State Unit → Prop} {B : α → SMonad Unit α} [hB : ∀ a, DKInst p (B a)] (r : α) : + DKInst p (NonDet.whileLoop C B r) := ⟨PresR.whileLoop (fun a => (hB a).pres) r⟩ + +instance {α : Type} (S : Set α × Prop) : DKInst p (NonDet.selectF S : SMonad Unit α) := ⟨PresR.selectF S⟩ + +instance {α : Type} {f : α → SMonad Unit Unit} [hf : ∀ a, DKInst p (f a)] (o : Option α) : + DKInst p (NonDet.maybeM f o) := by + cases o + · exact ⟨PresR.pure _⟩ + · exact hf _ + + +/-- A state modification that keeps the heap and the scalars. -/ +theorem DKPres.modify_fix (f : State Unit → State Unit) (s : ∀ σ, BScalA σ (f σ)) + (hk : ∀ σ, (f σ).kheap = σ.kheap) : DKPres p (NonDet.modify f : SMonad Unit Unit) := + PresR.modify f fun σ => DKA.of_kheap (s σ) (hk σ) + +theorem threadSet_dkA (f : Tcb → Tcb) (t : ObjRef) : DKPres p (threadSet (Z := Unit) f t) := by + intro σ r hr + obtain ⟨x, hx, e⟩ := threadSet_spec hr + rw [e] + exact DKA.upd (fun _ y n h => by rw [hx] at h; cases h) + +theorem updateSchedContext_dkA (f : SchedContext → SchedContext) : DKPres p (updateSchedContext (Z := Unit) p f) := by + intro σ r hr + obtain ⟨y1, hy1, h⟩ := hr + have hy1' : y1 ∈ (NonDet.assertOpt (σ.kheap p) σ).1 := hy1 + cases hk : σ.kheap p with + | none => rw [hk] at hy1'; exact absurd hy1' (Set.notMem_empty _) + | some ko => + rw [hk] at hy1' + have e1 : y1 = (ko, σ) := hy1' + subst e1 + cases ko with + | schedContext sc n => + obtain ⟨ko', hko, hr2⟩ := setObject_memA h + rw [hr2] + exact DKA.upd (fun h => absurd rfl h) + | _ => exact absurd h (Set.notMem_empty _) + +theorem setSchedulerAction_dkA (a : SchedulerAction) (h : a ≠ .resumeCurThread) : + DKPres p (setSchedulerAction (Z := Unit) a) := by + unfold setSchedulerAction + exact PresR.modify _ fun σ => DKA.of_kheap ⟨rfl, rfl, rfl, rfl, rfl, rfl, fun e => absurd e h⟩ rfl + +macro "dkres" : tactic => `(tactic| + repeat' (first + | exact DKInst.pres + | refine PresR.bind_pure _ _ ?_ + | refine PresR.bind ?_ (fun _ => ?_) + | refine PresR.when ?_ + | refine PresR.unless' ?_ + | refine PresR.ite' ?_ ?_ + | (refine PresR.modify _ (fun _ => ⟨⟨rfl, rfl, rfl, rfl, rfl, rfl, fun h => h⟩, fun _ _ _ _ _ => rfl⟩)) + | dsimp only + | split)) + +instance : DKInst p (setSchedulerAction (Z := Unit) .chooseNewThread) := + ⟨setSchedulerAction_dkA _ (by intro e; cases e)⟩ +instance (t : ObjRef) : DKInst p (setSchedulerAction (Z := Unit) (.switchThread t)) := + ⟨setSchedulerAction_dkA _ (by intro e; cases e)⟩ +instance (f : Tcb → Tcb) (t : ObjRef) : DKInst p (threadSet (Z := Unit) f t) := ⟨threadSet_dkA f t⟩ +instance (f : SchedContext → SchedContext) : DKInst p (updateSchedContext (Z := Unit) p f) := + ⟨updateSchedContext_dkA f⟩ +instance {α : Type} (a : α) : DKInst p (NonDet.pure a : SMonad Unit α) := ⟨PresR.pure a⟩ + +/-! ## Readers and small writers -/ + +instance {α : Type} (f : Tcb → α) (t : ObjRef) : DKInst p (threadGet (Z := Unit) f t) := + ⟨by unfold threadGet; dkres⟩ +instance (f : Tcb → Option ObjRef) (r : ObjRef) : DKInst p (getTcbObjRef (Z := Unit) f r) := + ⟨by unfold getTcbObjRef; dkres⟩ +instance (r : ObjRef) : DKInst p (getThreadState (Z := Unit) r) := ⟨by unfold getThreadState; dkres⟩ +instance (d : Domain) (pr : Priority) : DKInst p (getTcbQueue (Z := Unit) d pr) := ⟨by unfold getTcbQueue; dkres⟩ +instance (d : Domain) (pr : Priority) (q : ReadyQueue) : DKInst p (setTcbQueue (Z := Unit) d pr q) := + ⟨by unfold setTcbQueue; dkres⟩ +instance (a : ObjRef → List ObjRef → List ObjRef) (t : ObjRef) : DKInst p (tcbSchedAction (Z := Unit) a t) := + ⟨by unfold tcbSchedAction; dkres⟩ +instance (t : ObjRef) : DKInst p (tcbReleaseRemove (Z := Unit) t) := ⟨by unfold tcbReleaseRemove; dkres⟩ + +instance (c : ObjRef) : DKInst p (getObject (Z := Unit) c) := ⟨by unfold getObject; dkres⟩ +instance (c : ObjRef) : DKInst p (getSchedContext (Z := Unit) c) := ⟨by unfold getSchedContext; dkres⟩ +instance (f : SchedContext → Option ObjRef) (c : ObjRef) : DKInst p (getScObjRef (Z := Unit) f c) := + ⟨by unfold getScObjRef; dkres⟩ +instance (c : ObjRef) : DKInst p (getRefills (Z := Unit) c) := ⟨by unfold getRefills; dkres⟩ +instance (c : ObjRef) : DKInst p (getRefillHead (Z := Unit) c) := ⟨by unfold getRefillHead; dkres⟩ +instance (c : ObjRef) : DKInst p (getRefillTail (Z := Unit) c) := ⟨by unfold getRefillTail; dkres⟩ +instance (c : ObjRef) : DKInst p (getScRefillReady (Z := Unit) c) := ⟨by unfold getScRefillReady; dkres⟩ +instance (c : ObjRef) (u : Ticks) : DKInst p (getScRefillSufficient (Z := Unit) c u) := + ⟨by unfold getScRefillSufficient; dkres⟩ +instance (c : ObjRef) (u : Ticks) : DKInst p (fl4GetScRefillSufficient (Z := Unit) c u) := + ⟨by unfold fl4GetScRefillSufficient; dkres⟩ +instance (c : ObjRef) : DKInst p (getScActive (Z := Unit) c) := ⟨by unfold getScActive; dkres⟩ +instance (c : ObjRef) : DKInst p (isRoundRobin (Z := Unit) c) := ⟨by unfold isRoundRobin; dkres⟩ + +theorem setSimpleKo_dkA {α : Type} [Nonempty α] (C : α → KernelObject) [SimpleCtorA C] (ptr : ObjRef) (a : α) : + DKPres p (setSimpleKo (Z := Unit) C ptr a) := by + intro σ r hr + obtain ⟨y, hk, hr2⟩ := setSimpleKo_memA C ptr a hr + rw [hr2] + exact DKA.upd (fun _ y' n e => by rw [hk] at e; exact SimpleCtorA.nosc y y' n (Option.some.inj e)) + +instance {α : Type} [Nonempty α] (C : α → KernelObject) [SimpleCtorA C] (ptr : ObjRef) (a : α) : + DKInst p (setSimpleKo (Z := Unit) C ptr a) := ⟨setSimpleKo_dkA C ptr a⟩ +instance {α : Type} [Nonempty α] (C : α → KernelObject) (ptr : ObjRef) : + DKInst p (getSimpleKo (Z := Unit) C ptr) := ⟨by unfold getSimpleKo; dkres⟩ +instance {α : Type} [Nonempty α] (C : α → KernelObject) (f : α → Option ObjRef) (ptr : ObjRef) : + DKInst p (getSkObjRef (Z := Unit) C f ptr) := ⟨by unfold getSkObjRef; dkres⟩ +instance {α : Type} [Nonempty α] (C : α → KernelObject) [SimpleCtorA C] + (f : (Option ObjRef → Option ObjRef) → α → α) (ref : ObjRef) (new : Option ObjRef) : + DKInst p (updateSkObjRef (Z := Unit) C f ref new) := ⟨by unfold updateSkObjRef; dkres⟩ + +/-! ## Scheduler queues -/ + +instance : DKInst p (rescheduleRequired (Z := Unit)) := ⟨by unfold rescheduleRequired; dkres⟩ +instance (t : ObjRef) : DKInst p (possibleSwitchTo (Z := Unit) t) := ⟨by unfold possibleSwitchTo; dkres⟩ + + + +/-! ## Queue operations -/ + +instance {α β : Type} [Nonempty β] (t : α) (ts : List α) (f : α → RMonad Unit β) (R : β → β → Prop) : + DKInst p (orderedInsert (Z := Unit) t ts f R) := ⟨by unfold orderedInsert; dkres⟩ +instance (t : ObjRef) (qs : List ObjRef) : DKInst p (tcbAppend (Z := Unit) t qs) := ⟨by unfold tcbAppend; dkres⟩ +instance (t e : ObjRef) : DKInst p (tcbEpDequeue (Z := Unit) t e) := ⟨by unfold tcbEpDequeue; dkres⟩ +instance (t e : ObjRef) : DKInst p (tcbNtfnDequeue (Z := Unit) t e) := ⟨by unfold tcbNtfnDequeue; dkres⟩ +instance (t e : ObjRef) (b : Bool) : DKInst p (tcbEpAppend (Z := Unit) t e b) := ⟨by unfold tcbEpAppend; dkres⟩ +instance (t e : ObjRef) : DKInst p (tcbNtfnAppend (Z := Unit) t e) := ⟨by unfold tcbNtfnAppend; dkres⟩ + +/-! ## Scheduling-context donation and replies -/ + +instance (t : ObjRef) : DKInst p (tcbReleaseEnqueue (Z := Unit) t) := ⟨by unfold tcbReleaseEnqueue; dkres⟩ +instance : DKInst p (postpone (Z := Unit) p) := ⟨by unfold postpone; dkres⟩ +instance : DKInst p (schedContextResume (Z := Unit) p) := ⟨by unfold schedContextResume; dkres⟩ +instance (t : ObjRef) : DKInst p (testReschedule (Z := Unit) t) := ⟨by unfold testReschedule; dkres⟩ + +instance (t : ObjRef) : DKInst p (schedContextDonate (Z := Unit) p t) := + ⟨by unfold schedContextDonate; dkres⟩ + +/-- **`maybe_donate_sc` keeps the heap entry of an SC that is not the notification's SC.** -/ +theorem maybeDonateSc_keepScA (t np c : ObjRef) {σ : State Unit} {y : Notification} + (hkn : σ.kheap np = some (.notification y)) (hne : ∀ q, y.ntfnSc = some q → q ≠ c) {y0 : SchedContext} {n0 : Nat} + (hc : σ.kheap c = some (.schedContext y0 n0)) {r : Unit × State Unit} + (hr : r ∈ (maybeDonateSc (Z := Unit) t np σ).1) : BScalA σ r.2 ∧ r.2.kheap c = σ.kheap c := by + unfold maybeDonateSc at hr + obtain ⟨scOpt, -, hr⟩ := eqPres_mem (threadGet_eqPres _ t) hr + have refl : r = ((), σ) → BScalA σ r.2 ∧ r.2.kheap c = σ.kheap c := fun e => by rw [e]; exact ⟨BScalA.refl σ, rfl⟩ + unfold NonDet.when at hr + split_ifs at hr with h0 + · obtain ⟨u, hu, hr⟩ := mem_bindA.1 hr + rw [getNtfnObjRef_eqA hkn] at hu + have hu2 : u = (y.ntfnSc, σ) := hu + subst hu2 + dsimp only at hr + rcases e : y.ntfnSc with _ | scPtr + · rw [e] at hr + exact refl hr + · rw [e] at hr + have hp := hne scPtr e + simp only [NonDet.maybeM] at hr + have hgs : PresR (@Eq (State Unit)) (getScObjRef (Z := Unit) SchedContext.scTcb scPtr) := by + unfold getScObjRef + refine PresR.bind (getSchedContext_eqPres scPtr) (fun _ => ?_) + eqpres + obtain ⟨fo, hfo, hr⟩ := eqPres_mem hgs hr + split_ifs at hr with h1 + · obtain ⟨u, hu, hr⟩ := mem_bindA.1 hr + have D1 := (inferInstance : DKInst scPtr (schedContextDonate (Z := Unit) scPtr t)).pres σ u hu + have D2 := (inferInstance : DKInst scPtr (schedContextResume (Z := Unit) scPtr)).pres u.2 r hr + have e1 := D1.other c (Ne.symm hp) y0 n0 hc + exact ⟨D1.toBScalA.trans D2.toBScalA, (D2.other c (Ne.symm hp) y0 n0 (by rw [e1]; exact hc)).trans e1⟩ + · exact refl hr + · exact refl hr + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3RecvDonSpecA.lean b/proofs/Fl4ProofsA64/P3/Stage3RecvDonSpecA.lean new file mode 100644 index 000000000..960a1f45d --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3RecvDonSpecA.lean @@ -0,0 +1,238 @@ +/- +P3 (aarch64, A64-P3 line RecvPreA): **the spec tail of an active receive that donates** to a passive receiver +(`maybe_donate_sc`, then a body `k` that reads the receiver's SC: the sporadic unblock check). + +* **`donTail_loopA`**, **`donTail_fiaA`**, **`donTail_swA`**: `LoopInvA`, `FIA` and `SwA` across it. +* **`donTail_notRelA`**: a thread other than the receiver does not enter the release queue. +* `completeSpecBodyA`'s preservation instances. + +Used by `waitActiveA` (`specRecvTail`) and `recvBoundActiveA` (`specCompleteTailA`). No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3WakeInvA +import Fl4ProofsA64.P3.Stage3SigSchedValidA +import Fl4ProofsA64.P3.Stage3NBSignalWakeA +import Fl4ProofsA64.P3.ScRecvA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4Spec Fl4SpecA64 Fl4ProofsA64.P3 Fl4ProofsA64.P3.Notif Classical + +noncomputable section + +/-- `c` is not in the release queue. -/ +def NotRelA (c : ObjRef) (σ : State Unit) : Prop := c ∉ σ.releaseQueue + +theorem HT.notRelT {α : Type} {m : SMonad Unit α} (h : TPres m) (c : ObjRef) : + HT (NotRelA c) m (fun _ => NotRelA c) := fun σ hσ r hr => by + obtain ⟨-, er, -⟩ := h σ r hr + unfold NotRelA at hσ ⊢ + rw [er]; exact hσ + +theorem tcbSchedDequeue_notRel (c t : ObjRef) : + HT (NotRelA c) (tcbSchedAction (Z := Unit) tcbSchedDequeue t) (fun _ => NotRelA c) := fun σ hσ r hr => by + unfold tcbSchedAction at hr + obtain ⟨d, -, hr⟩ := eqPres_mem (threadGet_eqPres _ t) hr + obtain ⟨pr, -, hr⟩ := eqPres_mem (threadGet_eqPres _ t) hr + have hq : PresR (@Eq (State Unit)) (getTcbQueue (Z := Unit) d pr) := by unfold getTcbQueue; eqpres + obtain ⟨q, -, hr⟩ := eqPres_mem hq hr + obtain ⟨q', hq', hr⟩ := mem_bindA.1 hr + have eq' : q' = (tcbSchedDequeue t q, σ) := hq' + subst eq' + have er : r = ((), { σ with readyQueues := fun d' p => + if d' = d ∧ p = pr then tcbSchedDequeue t q else σ.readyQueues d' p }) := hr + rw [er] + exact hσ + +theorem postpone_notRel (c p : ObjRef) : + HT (fun σ => NotRelA c σ ∧ ScNotA c p σ) (postpone (Z := Unit) p) (fun _ => NotRelA c) := by + unfold postpone + refine HT.bind (Q := fun o σ => NotRelA c σ ∧ o ≠ some c) ?_ fun o => ?_ + · intro σ hσ r hr + obtain ⟨e, y, k, hk, hv⟩ := getScObjRef_mem hr + rw [e, hv] + exact ⟨hσ.1, hσ.2 y k hk⟩ + refine HT.bind (Q := fun t σ => NotRelA c σ ∧ t ≠ c) + (HT.assertOpt _ fun a e σ h => ⟨h.1, fun x => h.2 (by rw [e, x])⟩) fun t => ?_ + intro σ hσ r hr + have ne : t ≠ c := hσ.2 + have enq : HT (NotRelA c) (tcbReleaseEnqueue (Z := Unit) t) (fun _ => NotRelA c) := by + unfold tcbReleaseEnqueue + refine HT.bind (Q := fun q σ => NotRelA c σ ∧ c ∉ q) (HT.gets _ fun σ h => ⟨h, h⟩) fun q => ?_ + refine HT.bind (Q := fun q' σ => NotRelA c σ ∧ c ∉ q') ?_ fun q' => ?_ + · intro σ hσ r hr + unfold readyTimesAppend at hr + obtain ⟨hs, hm⟩ := orderedInsert_spec _ _ _ _ hr + rw [hs] + exact ⟨hσ.1, fun h => (hm c h).elim (fun e => ne e.symm) hσ.2⟩ + refine HT.bind (Q := fun _ => NotRelA c) ?_ fun _ => ?_ + · unfold setReleaseQueue + exact HT.modify _ fun σ h => h.2 + · exact HT.when (fun _ => HT.modify _ fun σ h => h) (fun _ σ h => h) + have tail : HT (NotRelA c) + (do tcbSchedAction (Z := Unit) tcbSchedDequeue t + tcbReleaseEnqueue t + NonDet.modify fun s => { s with reprogramTimer := true }) (fun _ => NotRelA c) := + HT.bind (Q := fun _ => NotRelA c) (tcbSchedDequeue_notRel c t) + fun _ => HT.bind (Q := fun _ => NotRelA c) enq fun _ => HT.modify _ fun σ h => h + exact tail σ hσ.1 r hr + +theorem schedContextResume_notRel (c p : ObjRef) : + HT (fun σ => NotRelA c σ ∧ ScNotA c p σ) (schedContextResume (Z := Unit) p) (fun _ => NotRelA c) := by + let P : State Unit → Prop := fun σ => NotRelA c σ ∧ ScNotA c p σ + have keep : ∀ {α : Type} {m : SMonad Unit α}, PresR (@Eq (State Unit)) m → HT P m (fun _ => P) := + fun h σ hσ r hr => by rw [← h σ r hr]; exact hσ + unfold schedContextResume + refine HT.mono (P := P) (Q := fun _ => NotRelA c) ?_ (fun σ h => h) (fun _ _ h => h) + refine HT.bind (Q := fun _ => P) (keep (getSchedContext_eqPres p)) fun sc => ?_ + refine HT.bind (Q := fun _ => P) (HT.assertOpt _ fun a _ σ h => h) fun t => ?_ + refine HT.bind (Q := fun _ => P) (HT.gets _ fun σ h => h) fun sched => ?_ + refine HT.when (fun _ => ?_) (fun _ σ h => h.1) + refine HT.bind (Q := fun _ => P) (keep (threadGet_eqPres _ t)) fun ts => ?_ + refine HT.bind (Q := fun _ => P) (keep (getScRefillReady_eqPres p)) fun ready => ?_ + refine HT.bind (Q := fun _ => P) (keep (getScRefillSufficient_eqPres p 0)) fun suf => ?_ + refine HT.when (fun _ => ?_) (fun _ σ h => h.1) + refine HT.bind (Q := fun _ => P) (keep (threadGet_eqPres _ t)) fun dom => ?_ + refine HT.bind (Q := fun _ => P) (keep (threadGet_eqPres _ t)) fun prio => ?_ + refine HT.bind (Q := fun _ => P) (keep (by unfold getTcbQueue; eqpres)) fun q => ?_ + refine HT.bind (Q := fun _ => P) (HT.assert fun _ σ h => h) fun _ => ?_ + exact postpone_notRel c p + +theorem maybeDonateSc_notRel {c d : ObjRef} (ne : d ≠ c) (n : ObjRef) : + HT (NotRelA c) (maybeDonateSc (Z := Unit) d n) (fun _ => NotRelA c) := by + unfold maybeDonateSc + refine HT.bind (Q := fun _ => NotRelA c) (HT.notRelT (by unfold getTcbObjRef; tpres) c) fun scOpt => ?_ + refine HT.when (fun _ => ?_) (fun _ σ h => h) + refine HT.bind (Q := fun _ => NotRelA c) (HT.notRelT (by unfold getNtfnObjRef getSkObjRef; tpres) c) fun nsc => ?_ + cases nsc with + | none => exact HT.pure () (fun σ h => h) + | some p => + show HT _ ((do let scTcb ← getScObjRef SchedContext.scTcb p + NonDet.when (scTcb = none) (do schedContextDonate p d; schedContextResume p))) _ + refine HT.bind (Q := fun o σ => NotRelA c σ ∧ (o = none → ∀ y k, σ.kheap p = some (.schedContext y k) → y.scTcb = none)) + ?_ fun o => ?_ + · intro σ hσ r hr + obtain ⟨e, y, k, hk, hv⟩ := getScObjRef_mem hr + obtain ⟨a, s'⟩ := r + simp only at e hv ⊢ + subst e + refine ⟨hσ, fun hn y' k' hk' => ?_⟩ + rw [hk] at hk' + have := Option.some.inj hk' + injection this with e1 e2 + subst e1 + rw [← hv]; exact hn + refine HT.when (fun hn => ?_) (fun _ σ h => h.1) + refine HT.bind (Q := fun _ σ => NotRelA c σ ∧ ScNotA c p σ) ?_ fun _ => schedContextResume_notRel c p + intro σ hσ r hr + unfold schedContextDonate at hr + obtain ⟨⟨fromOpt, σ1⟩, h1, h2⟩ := mem_bindA.1 hr + obtain ⟨e1, y, k, hk, hv⟩ := getScObjRef_mem h1 + have e1' : σ1 = σ := e1 + have hnone : fromOpt = none := by + have : fromOpt = y.scTcb := hv + rw [this]; exact hσ.2 hn y k hk + rw [e1'] at h2 + obtain ⟨⟨u, σ2⟩, h3, h4⟩ := mem_bindA.1 h2 + have e3 : ((u, σ2) : Unit × State Unit) = ((), σ) := by + unfold NonDet.when at h3 + rw [if_neg (by simp [hnone])] at h3 + exact h3 + have hσ2 : σ2 = σ := (Prod.mk.inj e3).2 + rw [hσ2] at h4 + obtain ⟨⟨u2, σ3⟩, h5, h6⟩ := mem_bindA.1 h4 + obtain ⟨y5, k5, -, e5⟩ := setScTcb_spec h5 + have e5' : σ3 = { σ with kheap := (Function.update σ.kheap p + (some (.schedContext (scTcbUpdate (fun _ => some d) y5) k5))) } := e5 + have h6' : r ∈ (threadSet (Z := Unit) (fun x => tcbSchedContextUpdate (fun _ => some p) x) d σ3).1 := h6 + obtain ⟨x6, -, e6⟩ := threadSet_spec h6' + rw [e6] + refine ⟨by rw [e5']; exact (show NotRelA c σ from hσ.1), fun y' k' hy' => ?_⟩ + have hy'' : Function.update σ3.kheap d (some (.tcb (tcbSchedContextUpdate (fun _ => some p) x6))) p = + some (.schedContext y' k') := hy' + by_cases e : p = d + · rw [e, Function.update_self] at hy'' + cases hy'' + · rw [Function.update_of_ne e, e5'] at hy'' + simp only [Function.update_self] at hy'' + obtain ⟨rfl, -⟩ := KernelObject.schedContext.inj (Option.some.inj hy'') + exact fun h => ne (Option.some.inj h) + +instance (o : Option ObjRef) (n : ObjRef) : SPresInst (completeSpecBodyA o n) := + ⟨by + unfold completeSpecBodyA getNtfnObjRef getSkObjRef + cases o <;> simp only [NonDet.maybeM] <;> spres⟩ +instance (o : Option ObjRef) (n : ObjRef) : QInst (completeSpecBodyA o n) := + ⟨by + unfold completeSpecBodyA getNtfnObjRef getSkObjRef + cases o <;> simp only [NonDet.maybeM] <;> qpres⟩ +instance (o : Option ObjRef) (n : ObjRef) : WkInst (completeSpecBodyA o n) := + ⟨by + unfold completeSpecBodyA getNtfnObjRef getSkObjRef + cases o <;> simp only [NonDet.maybeM] <;> wkpres⟩ +instance (o : Option ObjRef) (n : ObjRef) : TPresInst (completeSpecBodyA o n) := + ⟨by + unfold completeSpecBodyA getNtfnObjRef getSkObjRef + cases o <;> simp only [NonDet.maybeM] <;> tpres⟩ + +theorem completeSpecBodyA_refK (o : Option ObjRef) (n : ObjRef) : PresR RefK (completeSpecBodyA o n) := by + unfold completeSpecBodyA + cases o with + | none => exact PresR.pure _ + | some p => + show PresR RefK (_ >>= _) + refine PresR.bind (eqPres_refK (getSchedContext_eqPres p)) (fun _ => ?_) + refine PresR.when ?_ + refine PresR.bind (eqPres_refK (by unfold getNtfnObjRef getSkObjRef; eqpres)) (fun _ => ?_) + exact PresR.bind (PresR.gets _) (fun _ => PresR.when (refillUnblockCheck_refK p)) + +/-- The spec tail: donate, read the receiver's SC, run `k`. -/ +def donTailA (t np : ObjRef) (k : Option ObjRef → SMonad Unit Unit) : SMonad Unit Unit := do + maybeDonateSc t np + let o ← getTcbObjRef Tcb.tcbSchedContext t + k o + +theorem donTail_loopA {m : McState} {n : Nat} (t np : ObjRef) {k : Option ObjRef → SMonad Unit Unit} + (hk : ∀ o, PresR RefK (k o)) {σ σ' : State Unit} (h : LoopInvA σ m n) + (ht : t ≠ σ.idleThread) {y : Notification} (hkn : σ.kheap np = some (.notification y)) + (hsc : ∀ sc, y.ntfnSc = some sc → sc ≠ idleScPtr) (hnr : t ∉ σ.releaseQueue) + (hsuf : y.ntfnSc = some σ.curSc → ∀ y k, σ.kheap σ.curSc = some (.schedContext y k) → scActive y = true → + refillSufficient 0 (refillHd y) = true) + (hr : ((), σ') ∈ (donTailA t np k σ).1) : LoopInvA σ' m n := by + unfold donTailA at hr + obtain ⟨u, hu, hr⟩ := mem_bindA.1 hr + have D := maybeDonateSc_stepA t np h ht hkn hsc hnr hsuf hu + obtain ⟨o, -, hr⟩ := eqPres_mem (threadGet_eqPres _ t) hr + exact D.inv.refK (hk o u.2 _ hr) + +theorem donTail_fiaA (t np : ObjRef) {k : Option ObjRef → SMonad Unit Unit} (hk : ∀ o, TPres (k o)) + {σ σ' : State Unit} (h : FIA σ) (hq : RqDomPrio σ) {ts : ThreadState} (hts : BlockedTsA ts = false) + (hst : StIs t ts σ) + (hr : ((), σ') ∈ (donTailA t np k σ).1) : FIA σ' := by + have T : HT (fun σ => FIA σ ∧ RqDomPrio σ ∧ True ∧ StIs t ts σ) (donTailA t np k) + (fun _ σ => FIA σ ∧ True) := by + unfold donTailA + refine HT.bind (maybeDonateSc_fia t np hts (fun _ => True) + ⟨fun _ _ _ h => h, fun _ _ _ _ h => h, fun _ _ _ _ _ _ _ => trivial⟩) fun _ => ?_ + refine HT.ofTPres (PresR.bind (by unfold getTcbObjRef; tpres) hk) (fun _ => True) (fun _ _ _ h => h) + exact (T σ ⟨h, hq, trivial, hst⟩ _ hr).1 + +theorem donTail_swA (t np : ObjRef) {k : Option ObjRef → SMonad Unit Unit} (hk : ∀ o, TPres (k o)) + {σ σ' : State Unit} (h : SwN t σ) (hr : ((), σ') ∈ (donTailA t np k σ).1) : SwA σ' := by + have T : HT (SwN t) (donTailA t np k) (fun _ => SwN t) := by + unfold donTailA + refine HT.bind (maybeDonateSc_swn t np) fun _ => ?_ + exact HT.swn (PresR.bind (by unfold getTcbObjRef; tpres) hk) t + exact (T σ h _ hr).swa + +theorem donTail_notRelA {c t : ObjRef} (ne : t ≠ c) (np : ObjRef) {k : Option ObjRef → SMonad Unit Unit} + (hk : ∀ o, TPres (k o)) {σ σ' : State Unit} (h : c ∉ σ.releaseQueue) + (hr : ((), σ') ∈ (donTailA t np k σ).1) : c ∉ σ'.releaseQueue := by + have T : HT (NotRelA c) (donTailA t np k) (fun _ => NotRelA c) := by + unfold donTailA + refine HT.bind (maybeDonateSc_notRel ne np) fun _ => ?_ + exact HT.notRelT (PresR.bind (by unfold getTcbObjRef; tpres) hk) c + exact T σ h _ hr + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3RecvFootA.lean b/proofs/Fl4ProofsA64/P3/Stage3RecvFootA.lean new file mode 100644 index 000000000..25a2a7d41 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3RecvFootA.lean @@ -0,0 +1,133 @@ +/- +P3 (aarch64, A64-P3 Stage-3 integration, D8/T1 step 2): **the write footprints of the bound-notification receive +arms** (`EpRecvBndA`). + +* **`complete_signal_footF`**: `notification.complete_signal` on an active notification writes `FpNA`: the two prefix + writes (the badge register, the notification state word: `NWrite`) and the scheduler tail (`SchedFootNA.completeTail` + at the state after the prefix, transported back along `SSzA`). +* **`rcv_body_completeAF`**: the same for `rcvBodyA` with an active bound notification. +* **`take_reply_cancelAF`**: `take_reply` of a reply linked to another thread: the `cancel_ipc` footprint + (`cancel_ipc_footA`), as `WritesIn (CancelWA …)`, together with the posts of `take_reply_cancelA`. + +The ratified statements are unchanged. No named premise. +-/ +import Fl4ProofsA64.P3.EpRecvBndA +import Fl4ProofsA64.P3.CancelIpcFootA +import Fl4ProofsA64.P3.NtfnWakeFootA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Notif +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Sched + +noncomputable section + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] {N T : Set U64} + {SR : S → Fl4SpecA64.State Unit → Prop} {DL : S → U64 → Prop} + {Wr : Fl4SpecA64.State Unit → S → U64 → Prop} + +/-- **The footprint of `complete_signal` on an active notification** (module docstring). -/ +theorem complete_signal_footF (hH : SchedHypsA st N T SR) (hO : SchedOpsA st ar N T SR DL) + (hF : SchedFootNA st ar N T SR DL Wr) (hP : NPlaced T N) + (s : S) (a : A) (σ : Fl4SpecA64.State Unit) (hR : NtfnStateRel N T (L.mem s) σ) (hSR : SR s σ) + {t : U64} (ht : t ∈ T) {n : U64} (hn : n ∈ N) {x : Fl4SpecA64.Notification} + (hk : σ.kheap (specPtr n) = some (.notification x)) {b0 : Fl4SpecA64.Badge} (hact : x.ntfnObj = .activeNtfn b0) + {xt : Fl4SpecA64.Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hNQ : NotQueued σ (specPtr t)) (hRun : ∀ x, σ.kheap (specPtr t) = some (.tcb x) → Fl4SpecA64.runnable x.tcbState = true) + (hon : L.mem s (affA t) = L.cpu s) (hdl : DL s n) : + notification.complete_signal st ar s a n t ⦃ r => FpNA st N T Wr σ s r.1 ⦄ := by + obtain ⟨e1, e2, hR2, hW, hS⟩ := recv_active_prefix (st := st) hP hR ht hn hk hact hkt + have hnt : specPtr n ≠ specPtr t := by intro e; rw [e, hkt] at hk; cases hk + have hSR2 := hH.stable _ _ _ _ hSR hW hS (Endp.NoBlockE.recvPrefix hkt hnt _ _ _) + have hx := hR.at_ntfn hn hk + have hloc := recv_prefix_local hO hR hR2 hSR hSR2 ht hn hk hkt hnt hon hdl + have hRun2 : ∀ z, (updK (updK σ (specPtr t) (.tcb (setReg xt Fl4SpecA64.badge_register b0))) (specPtr n) + (.notification (Fl4SpecA64.ntfnObjUpdate (fun _ => .idleNtfn) x))).kheap (specPtr t) = some (.tcb z) → + Fl4SpecA64.runnable z.tcbState = true := by + intro y hy + simp only [updK_kheap, Function.update_apply, Ne.symm hnt, ↓reduceIte] at hy + cases hy; exact hRun xt hkt + have hsa : stOf (L.mem s) n = obj.NTFN_ACTIVE := stOf_active hx.obj hact + have ht0 : t ≠ 0#u64 := fun e => (hP.tcb t ht).ne0 (by rw [e]; rfl) + have hz : SSzA σ (updK (updK σ (specPtr t) (.tcb (setReg xt Fl4SpecA64.badge_register b0))) (specPtr n) + (.notification (Fl4SpecA64.ntfnObjUpdate (fun _ => .idleNtfn) x))) := + (Endp.ssz_updK_tcb σ _ _).trans (ssz_updK_ntfn _ _ _) + unfold notification.complete_signal + step with obj.Tcb.is_null.spec as ⟨b, b_post⟩ + simp only [b_post, ht0, decide_false, Bool.false_eq_true, ↓reduceIte] + step with obj.Ntfn.state.spec as ⟨i, i_post⟩ + rw [i_post, epState_eq, hsa] + simp only [bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + step with obj.Ntfn.badge.spec as ⟨b1, b1_post⟩ + step with Endp.obj.Tcb.set_reg.spec as ⟨s1, s1_post⟩ + step with obj.Ntfn.set_state.spec as ⟨s2, s2_post⟩ + subst_vars + refine WP.spec_mono (hF.completeTail _ a _ t n hR2 hSR2 ht hn hNQ hRun2 hloc.1 hloc.2) ?_ + intro r hw + exact FpNA.trans hF.back (FpNA.of_nw hW) hz (FpNA.of_wr hw) + + +end + +end + +end Fl4ProofsA64.P3.Notif + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched + +noncomputable section + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + {N T E R : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {SR : S → Fl4SpecA64.State Unit → Prop} {DL : S → U64 → Prop} + {Wr : Fl4SpecA64.State Unit → S → U64 → Prop} + +/-- **The body with an active bound notification, with its write footprint** (`rcv_body_completeA`). -/ +theorem rcv_body_completeAF (hH : SchedHypsA st N T SR) (hO : SchedOpsA st ar N T SR DL) + (hF : SchedFootNA st ar N T SR DL Wr) (hP : NPlaced T N) + (s : S) (a : A) (σ : Fl4SpecA64.State Unit) (hRN : NtfnStateRel N T (L.mem s) σ) + (hRE : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) {t : U64} (ht : t ∈ T) {xt : Fl4SpecA64.Tcb} + (hkt : σ.kheap (specPtr t) = some (.tcb xt)) {n : U64} (hn : n ∈ N) + (hb : xt.tcbBoundNotification = some (specPtr n)) {x : Fl4SpecA64.Notification} + (hk : σ.kheap (specPtr n) = some (.notification x)) {b0 : Fl4SpecA64.Badge} + (hact : x.ntfnObj = .activeNtfn b0) (hNQ : NotQueued σ (specPtr t)) + (hRun : ∀ x, σ.kheap (specPtr t) = some (.tcb x) → Fl4SpecA64.runnable x.tcbState = true) + (hon : L.mem s (affA t) = L.cpu s) (hdl : DL s n) (e : U64) (blk : Bool) (rv : U64) : + rcvBodyA st ar s a t e blk rv ⦃ fun r => FpNA st N T Wr σ s r.1 ⦄ := by + obtain ⟨hn0, hmn⟩ := bound_wordA (hRE.at_tcb ht hkt) hb + exact rcv_body_actA hn0 hmn (stOf_active (hRN.at_ntfn hn hk).obj hact) + (complete_signal_footF hH hO hF hP s a σ hRN hSR ht hn hk hact hkt hNQ hRun hon hdl) + +/-- **`take_reply` of a reply linked to another thread, with the `cancel_ipc` footprint** (`take_reply_cancelA`). -/ +theorem take_reply_cancelAF (hH : SchedHypsA st N T SR) (hO : SchedOpsA st ar N T SR DL) + (hOE : SchedOpsEA st T E R Bf SR) (hP : IPlacedA N T E R Bf) (s : S) (a : A) (σ : Fl4SpecA64.State Unit) + (hRN : NtfnStateRel N T (L.mem s) σ) (hRE : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) {t rv : U64} {b : Bool} + (hi0 : L.mem s (rTcbA rv) ≠ 0#u64) (hit : L.mem s (rTcbA rv) ≠ t) + (hc : CancelOKA N T E (L.mem s) σ (L.mem s (rTcbA rv))) + (hAct : Aligned (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION)) : + endpoint.take_reply st ar s a t (.Reply rv b) ⦃ r => r.1 = rv ∧ r.2.2 = a ∧ ∃ σ1, + Fl4SpecA64.cancelIpc (Z := Unit) (specPtr (L.mem s (rTcbA rv))) σ = ({((), σ1)}, False) ∧ + NtfnStateRel N T (L.mem r.2.1) σ1 ∧ EpStateRel T E R Bf (L.mem r.2.1) σ1 ∧ SR r.2.1 σ1 ∧ + WritesIn st s r.2.1 (CancelWA N T E R Bf (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION)) ⦄ := by + obtain ⟨hiT, x, hki, hnr, hE, hN, hrep⟩ := hc + simp only [endpoint.take_reply] + step with obj.Reply.tcb.spec as ⟨i, hi⟩ + step with obj.Tcb.is_null.spec as ⟨bn, hbn⟩ + subst hi + simp only [hbn, hi0, decide_false, Bool.false_eq_true, ↓reduceIte] + rw [tcb_ne_eqA] + apply spec_ok_bindA + simp only [hit, ne_eq, not_false_eq_true, decide_true, ↓reduceIte] + refine WP.spec_bind (WP.spec_and + (cancel_ipc_rel hH hO.scheduleTcb hOE hP s a σ hRN hRE hSR hiT hki hnr hE hN hrep) + (cancel_ipc_footA hP s a σ hRN hRE hiT hki hnr hE hN hAct)) ?_ + rintro ⟨s1, a1⟩ ⟨⟨ha, σ1, h1, h2, h3, h4⟩, hw⟩ + exact (WP.spec_ok _).2 ⟨rfl, ha, σ1, h1, h2, h3, h4, hw⟩ + +end + +end + +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/Stage3RecvFrontA.lean b/proofs/Fl4ProofsA64/P3/Stage3RecvFrontA.lean new file mode 100644 index 000000000..33b120e7c --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3RecvFrontA.lean @@ -0,0 +1,134 @@ +/- +P3 (aarch64, A64-P3 line e5): **the front of `handle_recv` for a reply-object receive**: the two capability lookups, the +spec's `handleRecv` unfolding and the arms' common tail, factored out of the `recvBlockRA` family. + +* **`RecvPreA.recvFrontA`**: from the receive core (`receive_ipc` on the looked-up endpoint cap with the reply cap, and the + spec's `receiveIpc` on the abstracted caps) to `HRecvPostA` of `handle_recv blk true`. + +Named premises: none. New module. +-/ +import Fl4ProofsA64.P3.Stage3RecvPollA +import Fl4ProofsA64.P3.Stage3RecvPreA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4Spec Fl4SpecA64 + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s : S} {a : A} {σ : State Unit} {m : McState} + +variable (st) in +/-- **The receive core of a reply-object receive**: `receive_ipc` on the looked-up caps and the spec's `receiveIpc` on the +abstracted ones relate, with the arms' post (`Out`, `A64RelS3`, the current thread, `BodyOKA`). -/ +def RecvCoreA (AR : A → HwRootA → Prop) (ar : machine.Arch A) (nA : U64) (n : Nat) (Cs : Set U64) (s : S) (a : A) + (m : McState) (cap : capability.Cap) (blk : Bool) (rc : capability.Cap) (scap src : Cap) (σ : State Unit) + (Out : State Unit → Prop) : Prop := + ∃ s2 σ', endpoint.receive_ipc st ar s a (L.curW s) cap blk rc = ok (s2, a) ∧ + ((), σ') ∈ (receiveIpc (Z := Unit) (specPtr (L.curW s)) scap blk src σ).1 ∧ + Out σ' ∧ A64RelS3 st AR ar nA n Cs s2 a σ' m ∧ L.curW s2 = L.curW s ∧ BodyOKA σ' m + +set_option maxHeartbeats 4000000 in +theorem RecvPreA.recvFrontA + (J : RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {p badge rr : U64} {send grant grantReply rg blk : Bool} {Out : State Unit → Prop} + (hcap : cspace.lookup_cap st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.CAP)) = + ok (.Ok (.Endpoint p badge send true grant grantReply))) + (hcapR : cspace.lookup_cap st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.REPLY)) = + ok (.Ok (.Reply rr rg))) + (core : RecvCoreA st AR ar nA n Z.Cs s a m (.Endpoint p badge send true grant grantReply) blk (.Reply rr rg) + (absCapA (.Endpoint p badge send true grant grantReply)) (.replyCap (specPtr rr) (replyRightsA rg)) σ Out) : + HRecvPostA st AR ar nA n Z.Cs s a m blk true σ Out := by + have htZ : L.curW s ∈ Z.T := J.sr.curT + set t := L.curW s with ht_def + have ct : σ.curThread = specPtr t := J.j.1.sch.curThread + obtain ⟨xt, hkt, r⟩ := J.ep.tcb t htZ + have hthT : Z.th t = some .tcb := (J.sr.thT t).2 htZ + obtain ⟨s2, σ', hipc, hrecv, hout, hrel, hcur, hbody⟩ := core + refine ⟨s2, a, σ', ?_, ?_, hout, hrel, hcur, hbody⟩ + · unfold syscall.handle_recv + rw [cur_thread_ok, bind_ok, tcb_reg_eqA, bind_ok] + rw [show L.curW s = t from rfl, hcap] + simp only [bind_ok, ↓reduceIte] + rw [tcb_reg_eqA, bind_ok, hcapR] + simp only [bind_ok] + exact hipc + · + have hSt := J.j.1.st + have hV := J.j.1.cv + have capReg : (L.mem s (regWA t layout.reg.CAP)).bv = xt.tcbArch.tcbContext.userRegs cap_register := by + rw [← r.regs cap_register, regIdxA_capA]; rfl + have hmem : ((), σ') ∈ (handleRecv (Z := Unit) blk true σ).1 := by + have capRead : NonDet.liftM dataToCptr (asUser (Z := Unit) (specPtr t) (getRegister cap_register)) σ = + ({(toBl (L.mem s (regWA t layout.reg.CAP)).bv, σ)}, False) := by + unfold NonDet.liftM + rw [nd_bind_singleA' (asUser_getRegister_eqA hkt _), ← capReg] + rfl + have hc0 : (lookupCap (Z := Unit) (specPtr t) + (toBl (L.mem s (regWA t layout.reg.CAP)).bv)).run σ = + ({(.ok (absCapA (.Endpoint p badge send true grant grantReply)), σ)}, False) := by + have l := post_of_okA (lookup_cap_recv_refinesA (st := st) _ s σ hSt hV t hthT + (L.mem s (regWA t layout.reg.CAP))) hcap + obtain ⟨e, he, hrel⟩ := l + cases e with + | error _ => exact hrel.elim + | ok ac => + obtain rfl : ac = absCapA (.Endpoint p badge send true grant grantReply) := + (show absCapA (.Endpoint p badge send true grant grantReply) = ac from hrel.2).symm + exact he + have replyReg : (L.mem s (regWA t layout.reg.REPLY)).bv = xt.tcbArch.tcbContext.userRegs replyRegister := by + rw [← r.regs replyRegister, regIdxA_replyA]; rfl + have replyRead : getCapReg (Z := Unit) replyRegister σ = + ({(toBl (L.mem s (regWA t layout.reg.REPLY)).bv, σ)}, False) := by + unfold getCapReg NonDet.liftM + rw [nd_gets_bindSc, ct, nd_bind_singleA' (asUser_getRegister_eqA hkt _), ← replyReg] + rfl + have hcR : (lookupCap (Z := Unit) (specPtr t) + (toBl (L.mem s (regWA t layout.reg.REPLY)).bv)).run σ = + ({(.ok (.replyCap (specPtr rr) (replyRightsA rg)), σ)}, False) := by + have l := post_of_okA (lookup_cap_recv_refinesA (st := st) _ s σ hSt hV t hthT + (L.mem s (regWA t layout.reg.REPLY))) hcapR + obtain ⟨e, he, hrel⟩ := l + cases e with + | error _ => exact hrel.elim + | ok ac => + obtain rfl : ac = .replyCap (specPtr rr) (replyRightsA rg) := + (show absCapA (.Reply rr rg) = ac from hrel.2).symm + exact he + have hlr : (lookupReply (Z := Unit)).run σ = ({(.ok (.replyCap (specPtr rr) (replyRightsA rg)), σ)}, False) := by + have gt : (NonDet.gets fun s : State Unit => s.curThread) σ = ({(specPtr t, σ)}, False) := by + change ({(σ.curThread, σ)}, False) = _ + rw [ct] + have hc2 : (capFaultOnFailure (ofBl (toBl (L.mem s (regWA t layout.reg.REPLY)).bv)) true + (lookupCap (Z := Unit) (specPtr t) (toBl (L.mem s (regWA t layout.reg.REPLY)).bv))).run σ = + ({(.ok (.replyCap (specPtr rr) (replyRightsA rg)), σ)}, False) := run_handleE_okA hcR + unfold lookupReply + rw [run_bind_okA (run_liftEA replyRead), run_bind_okA (run_liftEA gt), run_bind_okA hc2] + rw [if_pos (by rfl)] + exact run_returnOkA _ _ + rw [handleRecv, nd_gets_bindSc, ct, nd_bind_oneSc capRead] + apply mem_bindA.2 + refine ⟨(.ok (), σ'), ?_, rfl⟩ + have hc1 : (capFaultOnFailure (ofBl (toBl (L.mem s (regWA t layout.reg.CAP)).bv)) true + (lookupCap (Z := Unit) (specPtr t) (toBl (L.mem s (regWA t layout.reg.CAP)).bv))).run σ = + ({(.ok (absCapA (.Endpoint p badge send true grant grantReply)), σ)}, False) := run_handleE_okA hc0 + rw [run_bind_okA hc1] + dsimp only [absCapA] + rw [if_pos (by simp [AllowRecv, rightsOfA])] + simp only [↓reduceIte] + rw [run_bind_okA hlr] + unfold NonDetE.liftE + rw [ExceptT.run_lift] + exact mem_bindA.2 ⟨((), σ'), hrecv, rfl⟩ + exact hmem + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3RecvPassiveA.lean b/proofs/Fl4ProofsA64/P3/Stage3RecvPassiveA.lean new file mode 100644 index 000000000..2b6c80d39 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3RecvPassiveA.lean @@ -0,0 +1,92 @@ +/- +P3 (aarch64, A64-P3 line RecvPreA): **a passive server's `ReplyRecv` that finds a waiting sender**, end to end: the +reply half from the syscall entry (`replyPassiveRecvEntryA`) composed with `handle_recv` on an endpoint with a waiting +sender that pushes no reply (`RecvPreA.recvSendA`). + +* **`JSyscallA.replyPassiveRecvSendEntryA`**. + +[S] those of the two halves; the receive half's are on the state after the reply half. No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3CombinedFrontA +import Fl4ProofsA64.P3.Stage3RecvSendA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {s : S} {a : A} {σ : State Unit} {m : McState} + +set_option maxHeartbeats 1000000 in +/-- **A passive server's `ReplyRecv` with a waiting non-pushing sender, from the syscall entry** (module docstring). -/ +theorem JSyscallA.replyPassiveRecvSendEntryA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hag : fl4MinBudget = MIN_BUDGET) (hmb : ∀ a, Fl4MinBudgetAgreeA ar a) + (rep : Bool) {cptr : U64} {info : msginfo.MessageInfo} {buf : U64} {r1 : cspace.Resolved} + {e : Aeneas.Std.Array obj.Slot 3#usize} + (einfo : msg.msg_info st s (L.curW s) = ok info) + (ebuf : msg.lookup_ipc_buffer st s false (L.curW s) = ok buf) + (elr : cspace.lookup_slot st s (L.curW s) cptr = ok (.Ok r1)) + (eex : ipc.lookup_extra_caps st s (L.curW s) buf info = ok (.Ok e)) + {r : U64} {grant : Bool} (hcap : capA st s r1.slot = .Reply r grant) + (hr : r ∈ Rp) {xr : Reply} (hk : σ.kheap (specPtr r) = some (.reply xr)) {d : U64} + (hcl : xr.replyTcb = some (specPtr d)) + (hsnd : ∀ x, σ.kheap σ.curThread = some (.tcb x) → x.tcbFault = none ∧ (grant = false ∨ + ipcBufOfA false x = none ∨ + unat (dataToMessageInfo (x.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0)) + (hrcv : ∀ y, σ.kheap (specPtr d) = some (.tcb y) → + y.tcbState = .blockedOnReply (specPtr r) ∧ y.tcbFault = none) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) (hconv : Fl4TicksToUsLegacyAgree) + (hdsc : ∀ y, σ.kheap (specPtr d) = some (.tcb y) → y.tcbSchedContext = none) + (hsw : scWithReply (specPtr r) σ = some σ.curSc) + (hhd : ∀ y k, σ.kheap σ.curSc = some (.schedContext y k) → y.scReplies.head? = some (specPtr r)) + (hnt : ∀ y, σ.kheap (specPtr d) = some (.tcb y) → isEpCap y.tcbTimeoutHandler = false) : + ∃ s3 σ3, syscall.handle_invocation st ar s a false false true true cptr = ok (.Ok (), s3, a) ∧ + (.ok (), σ3) ∈ ((fl4HandleInvocationR (Z := Unit) rep false false true true + (Fl4SpecA64.toBl cptr.bv)).run σ).1 ∧ + syscall.after_invocation st ar s3 a (.Ok ()) = ok (true, s3, a) ∧ L.curW s3 = L.curW s ∧ + (∀ x, σ3.kheap σ3.curThread = some (.tcb x) → x.tcbSchedContext = none) ∧ + ∀ {p badge slot rv : U64} {send pg pgr blk canReply : Bool} {rc : capability.Cap} {sd : U64} {q : List ObjRef}, + cspace.lookup_cap st s3 (L.curW s3) (L.mem s3 (regWA (L.curW s3) layout.reg.CAP)) = + ok (.Ok (.Endpoint p badge send true pg pgr)) → + slot ∈ OS → SlotCov st OS s3 slot → capA st s3 slot = .Endpoint p badge send true pg pgr → + RecvReplyPinA (st := st) s3 (L.curW s3) Rp canReply rc → + ((rc = .Null ∧ rv = 0#u64) ∨ ∃ g, rc = .Reply rv g) → + (∀ r g, rc = .Reply r g → ∃ rslot ∈ OS, capA st s3 rslot = .Reply r g) → + p ∈ E → σ3.kheap (specPtr p) = some (.endpoint (.sendEP (specPtr sd :: q))) → + (∀ y, σ3.kheap σ3.curThread = some (.tcb y) → BndOKA N (L.mem s3) σ3 (L.curW s3) y blk) → + (∀ y xt, σ3.kheap (specPtr sd) = some (.tcb y) → σ3.kheap σ3.curThread = some (.tcb xt) → + (y.tcbFault = none ∧ ∀ e' dd, y.tcbState = .blockedOnSend e' dd → dd.senderCanGrant = false ∨ + ipcBufOfA false y = none ∨ + unat (dataToMessageInfo (y.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0) ∨ + ∃ f, y.tcbFault = some f ∧ + ((∀ b, f ≠ .timeout b) → Endp.FaultOKA (L := L) s3 sd (ipcBufOfA true xt) y f)) → + (rv ≠ 0#u64 → ∀ y, σ3.kheap (specPtr sd) = some (.tcb y) → ∀ e' dd, y.tcbState = .blockedOnSend e' dd → + (dd.senderIsCall = true ∨ y.tcbFault ≠ none) → + dd.senderCanGrant = false ∧ dd.senderCanGrantReply = false) → + HRecvPostA st AR ar hPl.ipa n Z.Cs s3 a m blk canReply σ3 + (fun σ' => ((), σ') ∈ (specRecvSendA σ3.curThread (specPtr p) (optRef rv) (specPtr sd :: q) σ3).1) := by + obtain ⟨s3, σ3, h1', h2, h3, h4, h5, Q, Rl, stack', P⟩ := + J.replyPassiveRecvEntryA hA hM hPl hfm hCs h1 hag hmb rep einfo ebuf elr eex hcap hr hk hcl hsnd hrcv hmax htu + hconv hdsc hsw hhd hnt + refine ⟨s3, σ3, h1', h2, h3, h4, h5, ?_⟩ + intro p badge slot rv send pg pgr blk canReply rc sd q c1 c2 c3 c4 c5 c6 c7 c8 c9 c10 c11 c12 + exact RecvPreA.recvSendA (Z := { Z with Q := Q, R := Rl }) hA hPl P hCs h1 + (fun b => by have := hmb b; unfold Fl4MinBudgetAgreeA at this; rwa [hag] at this) hmax htu c1 c2 c3 c4 c5 c6 c7 + c8 c9 c10 c11 c12 + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3RecvPassiveDonA.lean b/proofs/Fl4ProofsA64/P3/Stage3RecvPassiveDonA.lean new file mode 100644 index 000000000..68636c72a --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3RecvPassiveDonA.lean @@ -0,0 +1,277 @@ +/- +P3 (aarch64, A64-P3 line asmrecv): **the consumers of the three donating receive arms**. + +* **`RecvPreA.waitActiveDon_oneNodeA`**, **`recvBoundActiveDon_oneNodeA`**: `hdl` (`DonLocalA`) discharged by + `JStage3BA.donLocal_single` (`numNodes = 1`). +* **`JSyscallA.replyPassiveRecvWaitDonEntryA`**, **`replyPassiveRecvPushDonEntryA`**, + **`nbSendDonRecvBoundDonEntryA`**: a first half that leaves the caller passive, composed with the arm; `hpas` comes + from the first half. + +[S] `hne` (notification's SC is not the current SC; `ntfnSc = curSc` is reachable and in the scope table), `hV` + (sender +without a bound VCPU), and those of both halves, the receive half's on the state after the first. No named premise. +New module. +-/ +import Fl4ProofsA64.P3.Stage3NBWakeEntryA +import Fl4ProofsA64.P3.Stage3RecvBoundDonA +import Fl4ProofsA64.P3.Stage3RecvPushDonA +import Fl4ProofsA64.P3.Stage3SigWakeRecA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {s : S} {a : A} {σ : State Unit} {m : McState} + +set_option maxHeartbeats 1000000 in +/-- **`waitActiveDonA` with `hdl` discharged** (`numNodes = 1`). [S] `hne`: the notification's SC is +not the current SC (the case `ntfnSc = curSc` is reachable and belongs to the scope table). -/ +theorem RecvPreA.waitActiveDon_oneNodeA + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ + m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p badge slot : U64} {send canReply blk : Bool} + (hcap : cspace.lookup_cap st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.CAP)) = + ok (.Ok (.Notification p badge send true))) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hsc : capA st s slot = .Notification p badge send true) + (hp : p ∈ N) {x : Notification} (hk : σ.kheap (specPtr p) = some (.notification x)) + {b0 : Badge} (hact : x.ntfnObj = .activeNtfn b0) + (hbt : x.ntfnBoundTcb = some σ.curThread ∨ x.ntfnBoundTcb = none) + (hpas : ∀ y, σ.kheap σ.curThread = some (.tcb y) → y.tcbSchedContext = none) + (hne : x.ntfnSc ≠ some σ.curSc) : + HRecvPostA st AR ar hPl.ipa n Z.Cs s a m blk canReply σ + (fun σ' => ∃ y, σ.kheap σ.curThread = some (.tcb y) ∧ + ((), σ') ∈ (specRecvTail σ.curThread (specPtr p) (rsPreσA σ σ.curThread (specPtr p) y x b0)).1) := + RecvPreA.waitActiveDonA hA hPl J hCs hmb hmax htu hcap hslot hcov hsc hp hk hact hbt hpas hne + (J.toJStage3A.toBA.donLocal_single hCs h1 hp) + +set_option maxHeartbeats 1000000 in +/-- **`recvBoundActiveDonA` with `hdl` discharged** (`numNodes = 1`). [S] `hne`: the notification's SC is +not the current SC (the case `ntfnSc = curSc` is reachable and belongs to the scope table). -/ +theorem RecvPreA.recvBoundActiveDon_oneNodeA + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ + m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {pe badge slot : U64} {send grant grantReply blk : Bool} + (hcap : cspace.lookup_cap st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.CAP)) = + ok (.Ok (.Endpoint pe badge send true grant grantReply))) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) + (hsc : capA st s slot = .Endpoint pe badge send true grant grantReply) + (hpe : pe ∈ E) {xe : Endpoint} (hke : σ.kheap (specPtr pe) = some (.endpoint xe)) + {p : U64} (hp : p ∈ N) {x : Notification} (hk : σ.kheap (specPtr p) = some (.notification x)) + {b0 : Badge} (hact : x.ntfnObj = .activeNtfn b0) + (hbn : ∀ y, σ.kheap σ.curThread = some (.tcb y) → y.tcbBoundNotification = some (specPtr p)) + (hpas : ∀ y, σ.kheap σ.curThread = some (.tcb y) → y.tcbSchedContext = none) + (hne : x.ntfnSc ≠ some σ.curSc) : + HRecvPostA st AR ar hPl.ipa n Z.Cs s a m blk false σ + (fun σ' => ∃ y, σ.kheap σ.curThread = some (.tcb y) ∧ + ((), σ') ∈ (specCompleteTail σ.curThread (specPtr p) (rsPreσA σ σ.curThread (specPtr p) y x b0)).1) := + RecvPreA.recvBoundActiveDonA hA hPl J hCs hmb hmax htu hcap hslot hcov hsc hpe hke hp hk hact hbn hpas hne + (J.toJStage3A.toBA.donLocal_single hCs h1 hp) + +set_option maxHeartbeats 1000000 in +/-- **A passive server's `ReplyRecv` on an active notification with an SC**, from the syscall entry. [S] `hne`. -/ +theorem JSyscallA.replyPassiveRecvWaitDonEntryA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ + m) + (hfm : A64FrameMasks st s) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hag : fl4MinBudget = MIN_BUDGET) (hmb : ∀ a, Fl4MinBudgetAgreeA ar a) + (rep : Bool) {cptr : U64} {info : msginfo.MessageInfo} {buf : U64} {r1 : cspace.Resolved} + {e : Aeneas.Std.Array obj.Slot 3#usize} + (einfo : msg.msg_info st s (L.curW s) = ok info) + (ebuf : msg.lookup_ipc_buffer st s false (L.curW s) = ok buf) + (elr : cspace.lookup_slot st s (L.curW s) cptr = ok (.Ok r1)) + (eex : ipc.lookup_extra_caps st s (L.curW s) buf info = ok (.Ok e)) + {r : U64} {grant : Bool} (hcap : capA st s r1.slot = .Reply r grant) + (hr : r ∈ Rp) {xr : Reply} (hk : σ.kheap (specPtr r) = some (.reply xr)) {d : U64} + (hcl : xr.replyTcb = some (specPtr d)) + (hsnd : ∀ x, σ.kheap σ.curThread = some (.tcb x) → x.tcbFault = none ∧ (grant = false ∨ + ipcBufOfA false x = none ∨ + unat (dataToMessageInfo (x.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0)) + (hrcv : ∀ y, σ.kheap (specPtr d) = some (.tcb y) → + y.tcbState = .blockedOnReply (specPtr r) ∧ y.tcbFault = none) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) (hconv : Fl4TicksToUsLegacyAgree) + (hdsc : ∀ y, σ.kheap (specPtr d) = some (.tcb y) → y.tcbSchedContext = none) + (hsw : scWithReply (specPtr r) σ = some σ.curSc) + (hhd : ∀ y k, σ.kheap σ.curSc = some (.schedContext y k) → y.scReplies.head? = some (specPtr r)) + (hnt : ∀ y, σ.kheap (specPtr d) = some (.tcb y) → isEpCap y.tcbTimeoutHandler = false) : + ∃ s3 σ3, syscall.handle_invocation st ar s a false false true true cptr = ok (.Ok (), s3, a) ∧ + (.ok (), σ3) ∈ ((fl4HandleInvocationR (Z := Unit) rep false false true true + (Fl4SpecA64.toBl cptr.bv)).run σ).1 ∧ + syscall.after_invocation st ar s3 a (.Ok ()) = ok (true, s3, a) ∧ L.curW s3 = L.curW s ∧ + (∀ x, σ3.kheap σ3.curThread = some (.tcb x) → x.tcbSchedContext = none) ∧ + ∀ {pr badge' slot : U64} {send canReply blk : Bool} {x : Notification} {b0 : Badge}, + cspace.lookup_cap st s3 (L.curW s3) (L.mem s3 (regWA (L.curW s3) layout.reg.CAP)) = + ok (.Ok (.Notification pr badge' send true)) → + slot ∈ OS → + SlotCov st OS s3 slot → + capA st s3 slot = .Notification pr badge' send true → + pr ∈ N → + σ3.kheap (specPtr pr) = some (.notification x) → + x.ntfnObj = .activeNtfn b0 → + (x.ntfnBoundTcb = some σ3.curThread ∨ x.ntfnBoundTcb = none) → + x.ntfnSc ≠ some σ3.curSc → + HRecvPostA st AR ar hPl.ipa n Z.Cs s3 a m blk canReply σ3 + (fun σ' => ∃ y, σ3.kheap σ3.curThread = some (.tcb y) ∧ + ((), σ') ∈ (specRecvTail σ3.curThread (specPtr pr) (rsPreσA σ3 σ3.curThread (specPtr pr) y x b0)).1) := by + obtain ⟨s3, σ3, h1', h2, h3, h4, h5, Q, Rl, stack', P⟩ := + J.replyPassiveRecvEntryA hA hM hPl hfm hCs h1 hag hmb rep einfo ebuf elr eex hcap hr hk hcl hsnd hrcv hmax + htu hconv hdsc hsw hhd hnt + refine ⟨s3, σ3, h1', h2, h3, h4, h5, ?_⟩ + intro pr badge' slot send canReply blk x b0 c1 c2 c3 c4 c5 c6 c7 c8 c9 + exact RecvPreA.waitActiveDon_oneNodeA (Z := { Z with Q := Q, R := Rl }) hA hPl P hCs h1 + (fun b => by have := hmb b; unfold Fl4MinBudgetAgreeA at this; rwa [hag] at this) hmax htu + c1 c2 c3 c4 c5 c6 c7 c8 h5 c9 + +set_option maxHeartbeats 1000000 in +/-- **A passive server's `ReplyRecv` that finds a pushing sender with an SC**, from the syscall entry. [S] `hV`. -/ +theorem JSyscallA.replyPassiveRecvPushDonEntryA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ + m) + (hfm : A64FrameMasks st s) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hag : fl4MinBudget = MIN_BUDGET) (hmb : ∀ a, Fl4MinBudgetAgreeA ar a) + (rep : Bool) {cptr : U64} {info : msginfo.MessageInfo} {buf : U64} {r1 : cspace.Resolved} + {e : Aeneas.Std.Array obj.Slot 3#usize} + (einfo : msg.msg_info st s (L.curW s) = ok info) + (ebuf : msg.lookup_ipc_buffer st s false (L.curW s) = ok buf) + (elr : cspace.lookup_slot st s (L.curW s) cptr = ok (.Ok r1)) + (eex : ipc.lookup_extra_caps st s (L.curW s) buf info = ok (.Ok e)) + {r : U64} {grant : Bool} (hcap : capA st s r1.slot = .Reply r grant) + (hr : r ∈ Rp) {xr : Reply} (hk : σ.kheap (specPtr r) = some (.reply xr)) {d : U64} + (hcl : xr.replyTcb = some (specPtr d)) + (hsnd : ∀ x, σ.kheap σ.curThread = some (.tcb x) → x.tcbFault = none ∧ (grant = false ∨ + ipcBufOfA false x = none ∨ + unat (dataToMessageInfo (x.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0)) + (hrcv : ∀ y, σ.kheap (specPtr d) = some (.tcb y) → + y.tcbState = .blockedOnReply (specPtr r) ∧ y.tcbFault = none) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) (hconv : Fl4TicksToUsLegacyAgree) + (hdsc : ∀ y, σ.kheap (specPtr d) = some (.tcb y) → y.tcbSchedContext = none) + (hsw : scWithReply (specPtr r) σ = some σ.curSc) + (hhd : ∀ y k, σ.kheap σ.curSc = some (.schedContext y k) → y.scReplies.head? = some (specPtr r)) + (hnt : ∀ y, σ.kheap (specPtr d) = some (.tcb y) → isEpCap y.tcbTimeoutHandler = false) : + ∃ s3 σ3, syscall.handle_invocation st ar s a false false true true cptr = ok (.Ok (), s3, a) ∧ + (.ok (), σ3) ∈ ((fl4HandleInvocationR (Z := Unit) rep false false true true + (Fl4SpecA64.toBl cptr.bv)).run σ).1 ∧ + syscall.after_invocation st ar s3 a (.Ok ()) = ok (true, s3, a) ∧ L.curW s3 = L.curW s ∧ + (∀ x, σ3.kheap σ3.curThread = some (.tcb x) → x.tcbSchedContext = none) ∧ + ∀ {pr badge' slot rr rslot : U64} {send grant' grantReply' blk rg : Bool} {sd : U64} {qr : List ObjRef}, + cspace.lookup_cap st s3 (L.curW s3) (L.mem s3 (regWA (L.curW s3) layout.reg.CAP)) = + ok (.Ok (.Endpoint pr badge' send true grant' grantReply')) → + slot ∈ OS → + SlotCov st OS s3 slot → + capA st s3 slot = .Endpoint pr badge' send true grant' grantReply' → + cspace.lookup_cap st s3 (L.curW s3) (L.mem s3 (regWA (L.curW s3) layout.reg.REPLY)) = ok (.Ok (.Reply rr rg)) + → + rslot ∈ OS → + capA st s3 rslot = .Reply rr rg → + rr ∈ Rp → + L.mem s3 (Endp.rTcbA rr) = 0#u64 → + pr ∈ E → + σ3.kheap (specPtr pr) = some (.endpoint (.sendEP (specPtr sd :: qr))) → + (∀ y, σ3.kheap σ3.curThread = some (.tcb y) → BndOKA N (L.mem s3) σ3 (L.curW s3) y blk) → + (∀ y xt, σ3.kheap (specPtr sd) = some (.tcb y) → σ3.kheap σ3.curThread = some (.tcb xt) → + y.tcbSchedContext ≠ none ∧ ∀ e' dd, + y.tcbState = .blockedOnSend e' dd → (dd.senderIsCall = true ∨ y.tcbFault ≠ none) ∧ + (dd.senderCanGrant = true ∨ dd.senderCanGrantReply = true)) → + (∀ y xt, σ3.kheap (specPtr sd) = some (.tcb y) → σ3.kheap σ3.curThread = some (.tcb xt) → + (y.tcbFault = none ∧ ∀ e' dd, y.tcbState = .blockedOnSend e' dd → dd.senderCanGrant = false ∨ + ipcBufOfA false y = none ∨ + unat (dataToMessageInfo (y.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0) ∨ + ∃ f, y.tcbFault = some f ∧ Endp.FaultOKA (L := L) s3 sd (ipcBufOfA true xt) y f) → + L.mem s3 (core.num.U64.wrapping_add sd 3664#u64) = 0#u64 → + HRecvPostA st AR ar hPl.ipa n Z.Cs s3 a m blk true σ3 + (fun σ' => ((), σ') ∈ (specRecvSendA σ3.curThread (specPtr pr) (some (specPtr rr)) (specPtr sd :: qr) + σ3).1) := by + obtain ⟨s3, σ3, h1', h2, h3, h4, h5, Q, Rl, stack', P⟩ := + J.replyPassiveRecvEntryA hA hM hPl hfm hCs h1 hag hmb rep einfo ebuf elr eex hcap hr hk hcl hsnd hrcv hmax + htu hconv hdsc hsw hhd hnt + refine ⟨s3, σ3, h1', h2, h3, h4, h5, ?_⟩ + intro pr badge' slot rr rslot send grant' grantReply' blk rg sd qr c1 c2 c3 c4 c5 c6 c7 c8 c9 c10 c11 c12 c13 c14 + c15 + exact RecvPreA.recvPushDonA (Z := { Z with Q := Q, R := Rl }) hA hPl P hCs h1 + (fun b => by have := hmb b; unfold Fl4MinBudgetAgreeA at this; rwa [hag] at this) hmax htu + c1 c2 c3 c4 c5 c6 c7 c8 c9 c10 c11 c12 c13 c14 h5 c15 + +set_option maxHeartbeats 1000000 in +/-- **A donating `NBSendWait` on an endpoint with an active bound notification**, from the syscall entry. [S] `hne`. + -/ +theorem JSyscallA.nbSendDonRecvBoundDonEntryA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ + m) + (hfm : A64FrameMasks st s) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (rep : Bool) {cptr : U64} {info : msginfo.MessageInfo} {buf : U64} {r1 : cspace.Resolved} + {e : Aeneas.Std.Array obj.Slot 3#usize} + (einfo : msg.msg_info st s (L.curW s) = ok info) + (ebuf : msg.lookup_ipc_buffer st s false (L.curW s) = ok buf) + (elr : cspace.lookup_slot st s (L.curW s) cptr = ok (.Ok r1)) + (eex : ipc.lookup_extra_caps st s (L.curW s) buf info = ok (.Ok e)) + {p badge : U64} {recv grant grantReply : Bool} + (hcap : capA st s r1.slot = .Endpoint p badge true recv grant grantReply) + (hp : p ∈ E) {d : U64} {q : List ObjRef} + (hk : σ.kheap (specPtr p) = some (.endpoint (.recvEP (specPtr d :: q)))) + (hsnd : ∀ x, σ.kheap σ.curThread = some (.tcb x) → x.tcbFault = none ∧ (grant = false ∨ + ipcBufOfA false x = none ∨ + unat (dataToMessageInfo (x.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0)) + (hrcv : ∀ y, σ.kheap (specPtr d) = some (.tcb y) → y.tcbSchedContext = none) + (hV : L.mem s (core.num.U64.wrapping_add (L.curW s) 3664#u64) = 0#u64) : + ∃ s3 σ3, syscall.handle_invocation st ar s a false false true true cptr = ok (.Ok (), s3, a) ∧ + (.ok (), σ3) ∈ ((fl4HandleInvocationR (Z := Unit) rep false false true true + (Fl4SpecA64.toBl cptr.bv)).run σ).1 ∧ + syscall.after_invocation st ar s3 a (.Ok ()) = ok (true, s3, a) ∧ L.curW s3 = L.curW s ∧ + (∀ x, σ3.kheap σ3.curThread = some (.tcb x) → x.tcbSchedContext = none) ∧ + ∀ {pe badge' slot : U64} {send grant' grantReply' blk : Bool} {xe : Endpoint} {pr : U64} {x : Notification} {b0 + : Badge}, + cspace.lookup_cap st s3 (L.curW s3) (L.mem s3 (regWA (L.curW s3) layout.reg.CAP)) = + ok (.Ok (.Endpoint pe badge' send true grant' grantReply')) → + slot ∈ OS → + SlotCov st OS s3 slot → + capA st s3 slot = .Endpoint pe badge' send true grant' grantReply' → + pe ∈ E → + σ3.kheap (specPtr pe) = some (.endpoint xe) → + pr ∈ N → + σ3.kheap (specPtr pr) = some (.notification x) → + x.ntfnObj = .activeNtfn b0 → + (∀ y, σ3.kheap σ3.curThread = some (.tcb y) → y.tcbBoundNotification = some (specPtr pr)) → + x.ntfnSc ≠ some σ3.curSc → + HRecvPostA st AR ar hPl.ipa n Z.Cs s3 a m blk false σ3 + (fun σ' => ∃ y, σ3.kheap σ3.curThread = some (.tcb y) ∧ + ((), σ') ∈ (specCompleteTail σ3.curThread (specPtr pr) (rsPreσA σ3 σ3.curThread (specPtr pr) y x b0)).1) + := by + obtain ⟨s3, σ3, h1', h2, h3, h4, h5, Q, Rl, P⟩ := + J.nbSendDonRecvEntryA hA hM hPl hfm hCs h1 hmb hmax htu rep einfo ebuf elr eex hcap hp hk hsnd hrcv hV + refine ⟨s3, σ3, h1', h2, h3, h4, h5, ?_⟩ + intro pe badge' slot send grant' grantReply' blk xe pr x b0 c1 c2 c3 c4 c5 c6 c7 c8 c9 c10 c11 + exact RecvPreA.recvBoundActiveDon_oneNodeA (Z := { Z with Q := Q, R := Rl }) hA hPl P hCs h1 hmb hmax htu c1 c2 c3 + c4 c5 c6 c7 c8 c9 c10 h5 c11 + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3RecvPollA.lean b/proofs/Fl4ProofsA64/P3/Stage3RecvPollA.lean new file mode 100644 index 000000000..082087660 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3RecvPollA.lean @@ -0,0 +1,654 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2c): **`handle_recv`, the non-blocking arms with nothing to receive, at the record**: +`NBRecv` on an idle / receiving endpoint and `NBWait` on a notification that is not active. FL4 writes only the caller's +badge register (0); the spec is `do_nbrecv_failed_transfer`. + +* `HRecvPostA`: what a `handle_recv` arm exports: FL4's outcome, a spec outcome of `handleRecv` with the arm's `Out`, + the record there, `CUR_THREAD` kept, and `BodyOKA` (Plan B). +* `JStage3A.of_badgeA`: the record across the badge-register write of a thread. +* **`JSyscallA.nbRecvIdleA`**, **`JSyscallA.nbWaitIdleA`**. + +Every spec datum is pinned by the lookup equations and the spec heap at the entry state. + +[S] the caller has no bound notification, or one that is not active (`BndOKA`; endpoint arm); the reply object (if any) is unlinked. +[P] `ArchTotal`, `ArchPlat`, `hCs`, `hmb`, `hmax`, `htu`. New module. +-/ +import Fl4ProofsA64.P3.Stage3RecvPreA +import Fl4ProofsA64.P3.Stage3SendRecvArmA +import Fl4ProofsA64.P3.Stage3BodyOKA +import Fl4ProofsA64.P3.Stage3DispBaseA +import Fl4ProofsA64.P3.Stage3QKeepPresA +import Fl4ProofsA64.P3.OuterEntryA +import Fl4ProofsA64.P3.EpRecvTopA +import Fl4ProofsA64.P3.EpRecvBndA +import Fl4ProofsA64.P3.NtfnRecvA +import Fl4ProofsA64.P3.Stage3WakeInvA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4Spec Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +variable {S : Type} (st : machine.Store S) [L : LawfulStore st] {A : Type} + +/-- **What a `handle_recv` arm exports** (module docstring). -/ +def HRecvPostA (AR : A → HwRootA → Prop) (ar : machine.Arch A) (nA : U64) (n : Nat) (Cs : Set U64) (s : S) (a : A) (m : McState) + (blocking canReply : Bool) (σ : State Unit) (Out : State Unit → Prop) : Prop := + ∃ (s' : S) (a' : A) (σ' : State Unit), + syscall.handle_recv st ar s a blocking canReply = ok (s', a') ∧ + ((), σ') ∈ (handleRecv (Z := Unit) blocking canReply σ).1 ∧ Out σ' ∧ + A64RelS3 st AR ar nA n Cs s' a' σ' m ∧ L.curW s' = L.curW s ∧ BodyOKA σ' m + +variable {st} {ar : machine.Arch A} + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s : S} {a : A} {σ : State Unit} {m : McState} + +/-- The saved registers `reg::CAP` and `reg::REPLY` are the spec's. -/ +theorem regIdxA_capA : regIdxA cap_register = layout.reg.CAP := by + unfold layout.reg.CAP layout.reg.X0; rfl + +theorem regIdxA_replyA : regIdxA replyRegister = layout.reg.REPLY := by + unfold layout.reg.REPLY layout.reg.X6; rfl + +set_option maxHeartbeats 2000000 in +/-- **P1's invariants and J7 at a state reached by an `RpPost` walk** from the record's state, for any spec +successor. -/ +theorem JStage3A.finRpA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) (hnA : nA = hPl.ipa) {s' : S} + (regs : RpPost st H Z.Cs OS Z.V ia ie Ac s s') (σ' : State Unit) : + BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s' ∧ SysL st H Z.Cs OS ia ie s' ∧ + SymL st H Z.Cs s' ∧ Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s' ∧ + J7A st (J7R2A st ar nA) Z H OS s' σ' m := by + subst hnA + obtain ⟨Pt, Ub, Pp7, Tb7, Hr, Te, Kt, Lv7, Re, bits, KLv, W⟩ := J7A.unp J.j7 + have I := J.delIn hA hPl W + have d3 := (fun q => ⟨q.1, q.2, regs.2.2.2.2.1.1, regs.2.2.2.2.2⟩ : + ArmQIAt st H Z.Cs OS Z.V ia ie s s' ∧ TabKeepOrZero st s s' → + ArmQIAt st H Z.Cs OS Z.V ia ie s s' ∧ TabKeepOrZero st s s' ∧ ArmVsK st H OS Z.V s s' ∧ + XferSlots2 st H OS Z.V s s') + (ArmQIAt.ofXC (Vc := Z.V) ⟨regs.1, regs.2.1⟩ ⟨Ac, regs.2.2.1⟩ regs.2.2.2.1 regs.2.2.2.2.1.1.toVs) + have Fx := S1aStepFix.ofXfer2 (n := hPl.ipa) d3 I.del.vr I.fu I.vcc W.col W.df W.r2 W.sep W.hiK + have hk : L.ksb s' = L.ksb s := regs.2.2.2.2.1.1.1 + have hPN : PoolNamed st OS s' := d3.1.2.2.2.2.2.1 W.pn + have hsp : ∀ p, SchedPageA st Z OS s' p → p ∈ Pt := by + have Fx' := Fx + obtain ⟨_, -, -, -, ⟨_, _, _, _, _, _, _, R'⟩, -⟩ := Fx' + exact schedPage_ptA R' W.sp + have bxi : BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s' := by + obtain ⟨q, b, x, sc, sg⟩ := regs.2.2.1 + exact ⟨q, b, x, sg, sc⟩ + have j7 : J7A st (J7R2A st ar hPl.ipa) Z H OS s' σ' m := + J7A.of_stepFixedA Fx W.vr hk W.al W.vu hPN hsp (fun b hb o ho => by rw [hk]; exact W.ks b hb o ho) + (J7R2K.of_stepFixedA (σ' := σ') (m := m) Fx W.ktK) + exact ⟨bxi, regs.1, regs.2.1, J.sys.ent.user.ksb hk, j7⟩ + +/-- **The record across a write into thread `t`'s badge register** (module docstring). -/ +theorem JStage3A.of_badgeSwA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) (hnA : nA = hPl.ipa) + (hc : m.curCore < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (hsw : SwA σ) (hns : σ.schedulerAction ≠ .switchThread σ.curThread) + {t : U64} (ht : t ∈ Z.T) {xt : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) (v : U64) : + ∃ Q Rl, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) (L.wr s (Endp.regA t layout.reg.BADGE) v) a + (updK σ (specPtr t) (.tcb (setReg xt badge_register v.bv))) m := by + subst hnA + set s' := L.wr s (Endp.regA t layout.reg.BADGE) v with hs' + set x' := setReg xt badge_register v.bv with hx' + set σ' := updK σ (specPtr t) (.tcb x') with hσ' + have hP := J.iplaced + have htH : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + have hta := J.sr.core.tcbs t ht + -- the addresses + have hbv : (Endp.regA t layout.reg.BADGE).val = t.val + 256 := by + have h0 : (layout.reg.BADGE).val = 0 := by simp [layout.reg.BADGE, layout.reg.X0] + rw [Endp.regA_val (hP.e.tcb t ht) (by rw [h0]; decide), h0] + have hbal : Aligned (Endp.regA t layout.reg.BADGE) := by + have e : Endp.regA t layout.reg.BADGE = wa t 256#u64 := by + apply UScalar.eq_of_val_eq + have := hta.fits + rw [hbv, wa_valm, Nat.mod_eq_of_lt (by simp; omega)]; simp + rw [e]; exact field_aligned hta (by simp) (by simp) + have w1 : WritesIn st s s' (fun b => b = Endp.regA t layout.reg.BADGE) := writesIn_wr hbal + have off : ∀ b : U64, Aligned b → b.val ≠ t.val + 256 → L.mem s' b = L.mem s b := fun b hb h => + w1.mem_eq hb fun c => h (by rw [c, hbv]) + have tcbW : ∀ u ∈ Z.T, ∀ O : U64, O.val < 4096 → O.val % 8 = 0 → O.val ≠ 256 → L.mem s' (wa u O) = L.mem s (wa u O) := by + intro u hu O h1 h2 h3 + have hua := J.sr.core.tcbs u hu + have hfit := hua.fits + have hv : (wa u O).val = u.val + O.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + refine off _ (field_aligned hua h1 h2) fun c => ?_ + by_cases e : u = t + · subst e; omega + · have := J.sr.place.apart u t .tcb .tcb ((J.sr.thT u).2 hu) ((J.sr.thT t).2 ht) e + have hsz : ObjKindA.size .tcb = 4096 := rfl + omega + -- the spec step + have mem : ((), σ') ∈ (asUser (Z := Unit) (specPtr t) (setRegister badge_register v.bv) σ).1 := by + rw [asUser_setRegister_eq _ _ hkt]; rfl + have K : SIKA σ.idleThread (fun _ => False) (fun _ => False) false σ σ' := asUser_sikA _ _ σ _ mem + have Qk : QKeepA σ σ' := asUser_qA _ _ σ _ mem + have same : ∀ q, q ≠ specPtr t → σ'.kheap q = σ.kheap q := fun q e => by + rw [hσ', updK_kheap, Function.update_of_ne e] + have atp : σ'.kheap (specPtr t) = some (.tcb x') := by rw [hσ', updK_kheap, Function.update_self] + have keepK : ∀ q ob, σ.kheap q = some ob → (∀ y, ob ≠ .tcb y) → σ'.kheap q = some ob := fun q ob h ne => by + rw [same q fun e => by rw [e, hkt] at h; exact ne xt (Option.some.inj h).symm] + exact h + have backK : ∀ q ob, σ'.kheap q = some ob → (∀ y, ob ≠ .tcb y) → σ.kheap q = some ob := fun q ob h ne => by + by_cases e : q = specPtr t + · rw [e, atp] at h; exact absurd (Option.some.inj h).symm (ne _) + · rwa [same q e] at h + have tcbF : ∀ q y, σ.kheap q = some (.tcb y) → ∃ y', σ'.kheap q = some (.tcb y') ∧ + y'.tcbState = y.tcbState ∧ y'.tcbSchedContext = y.tcbSchedContext := fun q y h => by + by_cases e : q = specPtr t + · rw [e, hkt] at h; cases h + exact ⟨x', e ▸ atp, rfl, rfl⟩ + · exact ⟨y, by rw [same q e]; exact h, rfl, rfl⟩ + have tcbB : ∀ q y', σ'.kheap q = some (.tcb y') → ∃ y, σ.kheap q = some (.tcb y) ∧ + y'.tcbState = y.tcbState ∧ y'.tcbSchedContext = y.tcbSchedContext := fun q y' h => by + by_cases e : q = specPtr t + · rw [e, atp] at h; cases h + exact ⟨xt, e ▸ hkt, rfl, rfl⟩ + · exact ⟨y', by rwa [same q e] at h, rfl, rfl⟩ + have HS : SpecSchedA σ σ' := + { kh := fun q => by + by_cases e : q = specPtr t + · exact Or.inr (Or.inl ⟨xt, x', e ▸ hkt, e ▸ atp, rfl, rfl, rfl⟩) + · exact Or.inl (same q e) + irqS := rfl + irqN := rfl + asid := rfl + vcur := rfl + cdt := rfl + orig := rfl } + have SSz : SSzA σ σ' := ssz_updK_tcb σ _ _ + -- FL4 / spec relations + obtain ⟨ep, wE, hE⟩ := write_reg_rel hP.e s σ J.ep ht hkt badge_register v + rw [regIdxA_badge] at ep wE + have hbn : badgeRegA t = Endp.regA t layout.reg.BADGE := by + apply UScalar.eq_of_val_eq + have := hta.fits + rw [badgeRegA_eq, hbv, wa_valm, Nat.mod_eq_of_lt (by simp; omega)]; simp + have ntfn := (set_badge_reg_rel hP.n J.ntfn ht hkt v).1 + rw [hbn] at ntfn + have ops := (ipcSchedOpsA (st := st) (ar := ar) (κ := κ) hP J.ipcPlace hc hmb hmax htu).2.2.1 + obtain ⟨Q, Rl, sr⟩ := ops.stable s s' σ σ' ⟨Z.Q, Z.R, J.sr⟩ wE hE (NoBlockE.tcbK hkt x' rfl rfl) + have wN : WritesIn st s s' (SchedStepNWA (st := st) N E Rp Z m σ s) := w1.mono fun b h => by + have hfit := hta.fits + have hv : ∀ O : U64, O.val < 4096 → (wa t O).val = t.val + O.val := fun O hO => by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + refine Or.inl (Or.inr ⟨t, ht, by rw [h, hbv]; omega, by rw [h, hbv]; omega, fun c => ?_, fun c => ?_⟩) + · have := congrArg (fun w : U64 => w.val) (h.symm.trans c) + simp only [hbv, hv 3664#u64 (by simp)] at this + simp at this + · have := congrArg (fun w : U64 => w.val) (h.symm.trans c) + rw [show runVcpuA t = wa t 3672#u64 from rfl] at this + simp only [hbv, hv 3672#u64 (by simp)] at this + simp at this + have reply : ReplyStackRelA (L.mem s') Rp Z.C σ' (scNameA m) stack := by + refine J.reply.frame (fun r hr => ?_) (fun c _ y k h => ⟨y, k, keepK _ _ h (fun _ e => by cases e), rfl⟩) + (fun r _ x1 h => ⟨x1, backK _ _ h (fun _ e => by cases e), rfl⟩) + have hr3 := hP.e.rep r hr + have dis := hP.e.rt r hr t ht + have key : ∀ O : U64, O.val = 8 ∨ O.val = 16 → L.mem s' (wa r O) = L.mem s (wa r O) := by + intro O hO + have hv : (wa r O).val = r.val + O.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact off _ (by unfold Aligned; rw [hv]; omega) (by omega) + exact ⟨key _ (Or.inr (by simp [layout.reply.NEXT])), key _ (Or.inl (by simp [layout.reply.PREV]))⟩ + have sinv : SInvXA σ' m n := + K.sinvSw J.sinv J.sidle hsw hns (fun _ _ h => h) (fun _ _ h => h) id (fun _ h => h.elim) (fun _ h => h.elim) + (fun _ h => h.elim) (fun _ h => h.elim) (J.sidle.rdyA J.sinv) (fun _ h => h.elim) + have sidle : SIdleA σ' m n := K.sidle J.sidle (fun _ _ h => h) (J.sidle.rdyA J.sinv) + have rv : RunVcpuInv st Z.T s' := fun u hu h => by + have e1 : L.mem s' (runVcpuA u) = L.mem s (runVcpuA u) := tcbW u hu _ (by simp) (by simp) (by simp) + have e2 : L.mem s' (stA u) = L.mem s (stA u) := by + rw [stA_eq]; exact tcbW u hu _ (by simp) (by simp) (by simp) + rw [e2] + exact J.runVcpu u hu (e1 ▸ h) + have blkQ : BlkQueuedA Z.T E N (L.mem s') σ' := + J.blkQ.keepA (fun u hu => tcbW u hu 3336#u64 (by simp) (by simp) (by simp)) + (fun u _ y h => Or.inr (by + obtain ⟨z, hz, e, -⟩ := tcbB _ _ h + exact ⟨z, hz, e.symm⟩)) + (fun q y h => keepK q _ h (fun _ e => by cases e)) (fun q y h => keepK q _ h (fun _ e => by cases e)) + have sv : SchedValidA σ' := J.schedValid.congr rfl rfl rfl rfl rfl rfl + (fun q y k h => keepK q _ h (fun _ e => by cases e)) + (fun u y h => by obtain ⟨z, hz, -, e⟩ := tcbF u y h; exact ⟨z, hz, e⟩) + (fun u y h => tcbB u y h) + -- P1's invariants after the write + obtain ⟨Pt, Ub, Pp7, Tb7, Hr, Te, Kt, Lv7, Re, bits, KLv, W⟩ := J7A.unp J.j7 + have I := J.delIn hA hPl W + have hH := I.del.heap + have hO := I.del.os + have hc0 := J.core0 + have B := J.sys.sysl + have Y := J.symL + have h : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s := by + obtain ⟨q, b, x, sg, sc⟩ := J.sys.ent.bxi + exact ⟨q, b, x, sc, sg⟩ + have regs : RpPost st H Z.Cs OS Z.V ia ie Ac s s' := by + have i0 : XKoQ st H Z.Cs OS ia ie Ac ∅ s s ∧ XV st H OS Z.V s s := + ⟨⟨⟨XJX.start B Y h, OSKeep.rfl' _⟩, TabKeep.refl s⟩, XV.refl s⟩ + have walk : obj.Tcb.set_reg st t s layout.reg.BADGE v ⦃ q => RpPost st H Z.Cs OS Z.V ia ie Ac s q ⦄ := by + have hvr1 : VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt := I.del.vr + refine WP.spec_mono (XJX.regT (Vc := Z.V) hH hO hc0 i0.1.1.1 hvr1 htH + (by simp [layout.reg.BADGE, layout.reg.X0])) fun q ⟨h3, k3, t3, a3⟩ => ?_ + exact RpW.post ⟨h3.sys, h3.sym, h3.blk, XC.keep (OSKeep.trans' i0.1.1.2 k3) (i0.1.2.trans t3), + i0.2.trans (XV.keep a3 k3), (XferSlots.refl s).keep fun z hz => (OSKeep.trans' i0.1.1.2 k3).capA hz⟩ + have e : obj.Tcb.set_reg st t s layout.reg.BADGE v = ok s' := by + obtain ⟨q, hq, e⟩ := spec_existsA (obj.Tcb.set_reg.spec (st := st) s t layout.reg.BADGE v) + rw [hq, e] + exact post_of_okA (P := fun q => RpPost st H Z.Cs OS Z.V ia ie Ac s q) walk e + obtain ⟨bxi, sysl, symL, userOK, j7⟩ := J.finRpA hA hPl rfl regs σ' + refine ⟨Q, Rl, ?_⟩ + exact J.of_ipcStepA (Q := Q) (Rl := Rl) (curOf := Z.curOf) (curScOf := Z.curScOf) wN HS SSz Qk rfl (fun _ _ => rfl) + bxi sysl symL userOK j7 sr ep ntfn reply K.ipcKeep + (fun u hu O h1 h2 => tcbW u hu O (by omega) h2 (by omega)) + sinv sidle rv blkQ sv + +/-- `of_badgeSwA` with no switch pending. -/ +theorem JStage3A.of_badgeA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) (hnA : nA = hPl.ipa) + (hc : m.curCore < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (hsw : ∀ u, σ.schedulerAction ≠ .switchThread u) + {t : U64} (ht : t ∈ Z.T) {xt : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) (v : U64) : + ∃ Q Rl, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) (L.wr s (Endp.regA t layout.reg.BADGE) v) a + (updK σ (specPtr t) (.tcb (setReg xt badge_register v.bv))) m := + J.of_badgeSwA hA hPl hnA hc hmb hmax htu (fun u e => absurd e (hsw u)) (hsw _) ht hkt v + +/-- `SwA` across a badge-register write of the current thread. -/ +theorem RecvPreA.swBadgeA + (J : RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {xt : Tcb} (hkt : σ.kheap (specPtr (L.curW s)) = some (.tcb xt)) (v : Fl4SpecA64.Word) : + SwA (updK σ (specPtr (L.curW s)) (.tcb (setReg xt badge_register v))) := by + have mem : ((), updK σ (specPtr (L.curW s)) (.tcb (setReg xt badge_register v))) ∈ + (asUser (Z := Unit) (specPtr (L.curW s)) (setRegister badge_register v) σ).1 := by + rw [asUser_setRegister_eq _ _ hkt]; rfl + have K : SIKA σ.idleThread (fun _ => False) (fun _ => False) false σ + (updK σ (specPtr (L.curW s)) (.tcb (setReg xt badge_register v))) := asUser_sikA _ _ σ _ mem + exact K.swA J.body.sw (fun _ _ h => h) (fun c => by cases c) (fun _ h => h.elim) + +/-- **`BodyOKA` after a badge-register write of the current thread at a syscall entry.** -/ +theorem RecvPreA.bodyOK_badgeA + (J : RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {xt : Tcb} (hkt : σ.kheap σ.curThread = some (.tcb xt)) : + BodyOKA (updK σ σ.curThread (.tcb (setReg xt badge_register 0))) m := by + have ct : σ.curThread = specPtr (L.curW s) := J.sr.sch.curThread + obtain ⟨y, hy, r⟩ := J.ep.tcb (L.curW s) J.sr.curT + rw [← ct, hkt] at hy + cases hy + have hrun : xt.tcbState = .running ∨ xt.tcbState = .restart := J.state (ct ▸ hkt) + have sw : SwA (updK σ σ.curThread (.tcb (setReg xt badge_register 0))) := by + rw [ct]; exact J.swBadgeA (ct ▸ hkt) 0 + refine J.body.stepEq sw rfl rfl rfl rfl rfl rfl (fun y' k h => ?_) (fun _ z hz => ?_) + · have ne : σ.curSc ≠ σ.curThread := fun e => by + rw [e, updK_kheap, Function.update_self] at h; cases h + rw [updK_kheap, Function.update_of_ne ne] at h + exact ⟨y', h, rfl, rfl, rfl⟩ + · rw [updK_kheap, Function.update_self] at hz + cases hz + exact hrun + +/-- `RecvPreA.bodyOK_badgeA` at a syscall entry. -/ +theorem JSyscallA.bodyOK_badgeA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {xt : Tcb} (hkt : σ.kheap σ.curThread = some (.tcb xt)) : + BodyOKA (updK σ σ.curThread (.tcb (setReg xt badge_register 0))) m := + J.toRecvPreA.bodyOK_badgeA hkt + +/-- The reply argument of a receive: none, or the looked-up reply cap of an unlinked reply object. -/ +def RecvReplyPinA (s : S) (t : U64) (Rp : Set U64) (canReply : Bool) (rc : capability.Cap) : Prop := + (canReply = false ∧ rc = .Null) ∨ + (canReply = true ∧ ∃ r g, rc = .Reply r g ∧ r ∈ Rp ∧ L.mem s (Endp.rTcbA r) = 0#u64 ∧ + cspace.lookup_cap st s t (L.mem s (regWA t layout.reg.REPLY)) = ok (.Ok rc)) + +set_option maxHeartbeats 2000000 in +/-- **`NBRecv` on an endpoint with no waiting sender** (module docstring). -/ +theorem RecvPreA.nbRecvIdleA + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p badge : U64} {send grant grantReply canReply : Bool} {rc : capability.Cap} + (hcap : cspace.lookup_cap st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.CAP)) = + ok (.Ok (.Endpoint p badge send true grant grantReply))) + (hrp : RecvReplyPinA (st := st) s (L.curW s) Rp canReply rc) + (hp : p ∈ E) {x : Endpoint} (hk : σ.kheap (specPtr p) = some (.endpoint x)) (hcb : CanBlockA true x) + (hbn : ∀ y, σ.kheap σ.curThread = some (.tcb y) → BndOKA N (L.mem s) σ (L.curW s) y false) : + HRecvPostA st AR ar hPl.ipa n Z.Cs s a m false canReply σ + (fun σ' => ∃ y, σ.kheap σ.curThread = some (.tcb y) ∧ + σ' = updK σ σ.curThread (.tcb (setReg y badge_register 0))) := by + have htZ : L.curW s ∈ Z.T := J.sr.curT + set t := L.curW s with ht_def + have ct : σ.curThread = specPtr t := J.j.1.sch.curThread + obtain ⟨xt, hkt, r⟩ := J.ep.tcb t htZ + have hkc : σ.kheap σ.curThread = some (.tcb xt) := by rw [ct]; exact hkt + have hbnd := hbn xt hkc + have hthT : Z.th t = some .tcb := (J.sr.thT t).2 htZ + set s' := L.wr s (Endp.regA t layout.reg.BADGE) 0#u64 with hs' + set σ' := updK σ (specPtr t) (.tcb (setReg xt badge_register 0)) with hσ' + have hrc : ∃ rc' rv, ReplyArgA Rp (L.mem s) rc rc' rv ∧ rc' = absCapA rc := by + rcases hrp with ⟨-, rfl⟩ | ⟨-, r0, g, rfl, hr, h0, -⟩ + · exact ⟨.nullCap, 0#u64, Or.inl ⟨rfl, rfl, rfl⟩, rfl⟩ + · exact ⟨absCapA (.Reply r0 g), r0, Or.inr ⟨g, _, rfl, rfl, hr, h0⟩, rfl⟩ + obtain ⟨rc', rv, hra, hrc'⟩ := hrc + have hx := J.ep.at_ep hp hk + -- FL4: `receive_ipc` + have hdisp : rcvDispA st ar s a t p false rv ⦃ q => q = (s', a) ⦄ := by + have hwait : (do + let s3 ← endpoint.wait_for_sender st s t p false rv + ok (s3, a)) ⦃ q => q = (s', a) ⦄ := by + unfold endpoint.wait_for_sender + simp only [Bool.false_eq_true, ↓reduceIte] + refine WP.spec_bind (obj.Tcb.set_reg.spec s t _ _) ?_ + rintro s1 rfl + rw [WP.spec_ok] + unfold rcvDispA + step with obj.Ep.state.spec as ⟨st0, hst0⟩ + rw [epState_eq] at hst0 + cases x with + | idleEP => + rw [hx.1] at hst0 + subst hst0 + rw [if_pos ep_state_vals.1.symm] + exact hwait + | recvEP q => + rw [hx.1] at hst0 + subst hst0 + rw [if_neg (by rw [ep_state_vals.1]; decide), if_pos ep_state_vals.2.2.symm] + exact hwait + | sendEP q => simp [CanBlockA] at hcb + have hipc : endpoint.receive_ipc st ar s a t (.Endpoint p badge send true grant grantReply) false rc = + ok (s', a) := by + have hrcA : ReplyCapA Rp rc rc' rv := by + rcases hra with h | ⟨b', r', h1, h2, h3, -⟩ + · exact Or.inl h + · exact Or.inr ⟨b', r', h1, h2, h3⟩ + have hpl : ∀ b, rc = .Reply rv b → L.mem s (Endp.rTcbA rv) = 0#u64 ∨ L.mem s (Endp.rTcbA rv) = t := by + rcases hra with ⟨h, -, -⟩ | ⟨b', r', h1, h2, h3, h4⟩ + · intro b hb; rw [h] at hb; cases hb + · intro b _; exact Or.inl h4 + have h : endpoint.receive_ipc st ar s a t (.Endpoint p badge send true grant grantReply) false rc ⦃ q => + q = (s', a) ⦄ := by + rw [receive_ipc_eqA] + refine WP.spec_bind (take_reply_plainA (R := Rp) hrcA hpl) ?_ + rintro q rfl + exact rcv_body_dispFlA J.ntfn J.ep htZ hkt hbnd hdisp + obtain ⟨q, hq, e⟩ := spec_existsA h + rw [hq, e] + -- FL4: `handle_recv` + have hfl : syscall.handle_recv st ar s a false canReply = ok (s', a) := by + unfold syscall.handle_recv + rw [cur_thread_ok, bind_ok, tcb_reg_eqA, bind_ok] + rw [show L.curW s = t from rfl, hcap] + simp only [bind_ok, ↓reduceIte] + rcases hrp with ⟨rfl, rfl⟩ | ⟨rfl, r0, g, rfl, -, -, hl⟩ + · simp only [Bool.false_eq_true, ↓reduceIte] + exact hipc + · simp only [↓reduceIte, tcb_reg_eqA, bind_ok, hl] + exact hipc + -- the spec + have hSt := J.j.1.st + have hV := J.j.1.cv + have capReg : (L.mem s (regWA t layout.reg.CAP)).bv = xt.tcbArch.tcbContext.userRegs cap_register := by + rw [← r.regs cap_register, regIdxA_capA]; rfl + have replyReg : (L.mem s (regWA t layout.reg.REPLY)).bv = xt.tcbArch.tcbContext.userRegs replyRegister := by + rw [← r.regs replyRegister, regIdxA_replyA]; rfl + have look : ∀ cp c, cspace.lookup_cap st s t cp = ok (.Ok c) → (∀ ac, CapRelA c ac → ac = absCapA c) → + (lookupCap (Z := Unit) (specPtr t) (toBl cp.bv)).run σ = ({(.ok (absCapA c), σ)}, False) := + fun cp c h habs => by + have l := post_of_okA (lookup_cap_recv_refinesA (st := st) _ s σ hSt hV t hthT cp) h + obtain ⟨e, he, hrel⟩ := l + cases e with + | error _ => exact hrel.elim + | ok ac => + obtain rfl := habs ac hrel + exact he + have repAbs : ∀ r0 g, ∀ ac, CapRelA (.Reply r0 g) ac → ac = absCapA (.Reply r0 g) := + fun r0 g ac h => (show absCapA (.Reply r0 g) = ac from h.2).symm + have hspec : ((), σ') ∈ (receiveIpc (Z := Unit) (specPtr t) + (absCapA (.Endpoint p badge send true grant grantReply)) false (absCapA rc) σ).1 := by + obtain ⟨hsr, -⟩ := replyArg_specA J.iplaced.e J.ep hra + have hsr' : SpecReplyPlainA σ (specPtr t) rc' (optRef rv) := by + rcases hsr with h | ⟨r1, rr, xr, h1, h2, h3, h4⟩ + · exact Or.inl h + · exact Or.inr ⟨r1, rr, xr, h1, h2, h3, Or.inl h4⟩ + rw [← hrc', show absCapA (.Endpoint p badge send true grant grantReply) = + .endpointCap (specPtr p) badge.bv (rightsOfA send true grant grantReply) from rfl, + receiveIpc_bodyA _ _ false hsr', bndOK_specA hkt hk J.ntfn hbnd (optRef rv), specDisp_waitA hcb] + unfold receiveIpcBlocked doNbrecvFailedTransfer + simp only [Bool.false_eq_true, ↓reduceIte] + rw [nd_bind_one (asUser_setRegister_eq _ _ hkt)] + exact Set.mem_singleton _ + have hmem : ((), σ') ∈ (handleRecv (Z := Unit) false canReply σ).1 := by + have capRead : NonDet.liftM dataToCptr (asUser (Z := Unit) (specPtr t) (getRegister cap_register)) σ = + ({(toBl (L.mem s (regWA t layout.reg.CAP)).bv, σ)}, False) := by + unfold NonDet.liftM + rw [nd_bind_singleA' (asUser_getRegister_eqA hkt _), ← capReg] + rfl + have hc0 := look _ _ hcap fun ac h => + (show absCapA (.Endpoint p badge send true grant grantReply) = ac from h.2).symm + rw [handleRecv, nd_gets_bindSc, ct, nd_bind_oneSc capRead] + apply mem_bindA.2 + refine ⟨(.ok (), σ'), ?_, rfl⟩ + have hc1 : (capFaultOnFailure (ofBl (toBl (L.mem s (regWA t layout.reg.CAP)).bv)) true + (lookupCap (Z := Unit) (specPtr t) (toBl (L.mem s (regWA t layout.reg.CAP)).bv))).run σ = + ({(.ok (absCapA (.Endpoint p badge send true grant grantReply)), σ)}, False) := run_handleE_okA hc0 + rw [run_bind_okA hc1] + dsimp only [absCapA] + rw [if_pos (by simp [AllowRecv, rightsOfA])] + have lift : ∀ {f : SMonad Unit Unit}, ((), σ') ∈ (f σ).1 → + (.ok (), σ') ∈ ((NonDetE.liftE (ε := Fault) f).run σ).1 := fun h => by + unfold NonDetE.liftE + rw [ExceptT.run_lift] + exact mem_bindA.2 ⟨((), σ'), h, rfl⟩ + rcases hrp with ⟨rfl, rfl⟩ | ⟨rfl, r0, g, rfl, -, -, hl⟩ + · simp only [Bool.false_eq_true, ↓reduceIte] + rw [run_bind_okA (run_returnOkA _ _)] + exact lift hspec + · simp only [↓reduceIte] + have currentRead : (NonDet.gets fun s : State Unit => s.curThread) σ = ({(specPtr t, σ)}, False) := by + change ({(σ.curThread, σ)}, False) = _ + rw [ct] + have replyRead : getCapReg (Z := Unit) replyRegister σ = + ({(toBl (L.mem s (regWA t layout.reg.REPLY)).bv, σ)}, False) := by + unfold getCapReg NonDet.liftM + rw [nd_gets_bindSc, ct, nd_bind_singleA' (asUser_getRegister_eqA hkt _), ← replyReg] + rfl + have hr1 : (capFaultOnFailure (ofBl (toBl (L.mem s (regWA t layout.reg.REPLY)).bv)) true + (lookupCap (Z := Unit) (specPtr t) (toBl (L.mem s (regWA t layout.reg.REPLY)).bv))).run σ = + ({(.ok (absCapA (.Reply r0 g)), σ)}, False) := run_handleE_okA (look _ _ hl (repAbs r0 g)) + have hReply : (lookupReply (Z := Unit)).run σ = ({(.ok (absCapA (.Reply r0 g)), σ)}, False) := by + unfold lookupReply + rw [run_bind_okA (run_liftEA replyRead), run_bind_okA (run_liftEA currentRead), ofBl_toBlA, + run_bind_okA (by rw [ofBl_toBlA] at hr1; exact hr1)] + rfl + rw [run_bind_okA hReply] + exact lift hspec + have hc : m.curCore < fl4A64Platform.numNodes := by rw [J.j.1.sch.core]; exact hCs _ J.j.1.sch.cpuCs + obtain ⟨Q, Rl, J2⟩ := J.toJStage3A.of_badgeSwA hA hPl rfl hc hmb hmax htu J.body.sw J.noSelf htZ hkt 0#u64 + refine ⟨s', a, σ', hfl, hmem, ⟨xt, hkc, by rw [ct]⟩, A64RelS3.ofJ J2 ?bo, curW_keptA J.toJStage3A J2 rfl, ?bo⟩ + have := J.bodyOK_badgeA hkc + rwa [ct] at this + +set_option maxHeartbeats 1000000 in +/-- **`RecvPreA.nbRecvIdleA` at a syscall entry.** -/ +theorem JSyscallA.nbRecvIdleA + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p badge : U64} {send grant grantReply canReply : Bool} {rc : capability.Cap} + (hcap : cspace.lookup_cap st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.CAP)) = + ok (.Ok (.Endpoint p badge send true grant grantReply))) + (hrp : RecvReplyPinA (st := st) s (L.curW s) Rp canReply rc) + (hp : p ∈ E) {x : Endpoint} (hk : σ.kheap (specPtr p) = some (.endpoint x)) (hcb : CanBlockA true x) + (hbn : ∀ y, σ.kheap σ.curThread = some (.tcb y) → BndOKA N (L.mem s) σ (L.curW s) y false) : + HRecvPostA st AR ar hPl.ipa n Z.Cs s a m false canReply σ + (fun σ' => ∃ y, σ.kheap σ.curThread = some (.tcb y) ∧ + σ' = updK σ σ.curThread (.tcb (setReg y badge_register 0))) := + RecvPreA.nbRecvIdleA hA hPl J.toRecvPreA hCs hmb hmax htu hcap hrp hp hk hcb hbn + +set_option maxHeartbeats 2000000 in +/-- **`NBWait` on a notification that is not active** (module docstring). -/ +theorem RecvPreA.nbWaitIdleA + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p badge : U64} {send canReply : Bool} + (hcap : cspace.lookup_cap st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.CAP)) = + ok (.Ok (.Notification p badge send true))) + (hp : p ∈ N) {x : Notification} (hk : σ.kheap (specPtr p) = some (.notification x)) + (hna : ∀ b, x.ntfnObj ≠ .activeNtfn b) + (hbt : x.ntfnBoundTcb = some σ.curThread ∨ x.ntfnBoundTcb = none) : + HRecvPostA st AR ar hPl.ipa n Z.Cs s a m false canReply σ + (fun σ' => ∃ y, σ.kheap σ.curThread = some (.tcb y) ∧ + σ' = updK σ σ.curThread (.tcb (setReg y badge_register 0))) := by + have htZ : L.curW s ∈ Z.T := J.sr.curT + set t := L.curW s with ht_def + have ct : σ.curThread = specPtr t := J.j.1.sch.curThread + obtain ⟨xt, hkt, r⟩ := J.ep.tcb t htZ + have hkc : σ.kheap σ.curThread = some (.tcb xt) := by rw [ct]; exact hkt + have hthT : Z.th t = some .tcb := (J.sr.thT t).2 htZ + set s' := L.wr s (Endp.regA t layout.reg.BADGE) 0#u64 with hs' + set σ' := updK σ (specPtr t) (.tcb (setReg xt badge_register 0)) with hσ' + have hx := J.ntfn.at_ntfn hp hk + have hst : stOf (L.mem s) p = obj.NTFN_IDLE ∨ stOf (L.mem s) p = obj.NTFN_WAITING := by + have h := hx.obj + cases ho : x.ntfnObj with + | idleNtfn => rw [ho] at h; exact Or.inl (h.1.trans ntfn_consts.1.symm) + | waitingNtfn q => rw [ho] at h; exact Or.inr (h.1.trans ntfn_consts.2.1.symm) + | activeNtfn b => exact absurd ho (hna b) + have hsig : notification.receive_signal st ar s a t p false = ok (s', a) := by + have h : notification.receive_signal st ar s a t p false ⦃ q => q = (s', a) ⦄ := by + unfold notification.receive_signal + step with obj.Ntfn.state.spec as ⟨st0, hst0⟩ + rw [epState_eq] at hst0 + have poll : (do + let s1 ← obj.Tcb.set_reg st t s layout.reg.BADGE 0#u64 + ok (s1, a)) ⦃ q => q = (s', a) ⦄ := by + refine WP.spec_bind (obj.Tcb.set_reg.spec s t _ _) ?_ + rintro s1 rfl + rw [WP.spec_ok] + rcases hst with h | h <;> rw [h] at hst0 <;> subst hst0 + · rw [if_pos rfl] + simp only [Bool.false_eq_true, ↓reduceIte] + exact poll + · rw [if_neg (by rw [ntfn_consts.1, ntfn_consts.2.1]; decide), if_pos rfl] + simp only [Bool.false_eq_true, ↓reduceIte] + exact poll + obtain ⟨q, hq, e⟩ := spec_existsA h + rw [hq, e] + -- the bound-thread check + have hb : L.mem s (Notif.nBoundA p) = t ∨ L.mem s (Notif.nBoundA p) = 0#u64 := by + by_cases h0 : L.mem s (Notif.nBoundA p) = 0#u64 + · exact Or.inr h0 + · rcases hbt with h | h + · rw [hx.bound, optRef, if_neg h0, ct] at h + exact Or.inl (specPtr_inj (Option.some.inj h)) + · rw [hx.bound, optRef, if_neg h0] at h + cases h + have hfl : syscall.handle_recv st ar s a false canReply = ok (s', a) := by + unfold syscall.handle_recv + rw [cur_thread_ok, bind_ok, tcb_reg_eqA, bind_ok] + rw [show L.curW s = t from rfl, hcap] + simp only [bind_ok, ↓reduceIte] + obtain ⟨v, hv, e⟩ := spec_existsA (Notif.obj.Ntfn.bound_tcb.spec (st := st) s p) + rw [hv, bind_ok, e] + rcases hb with h | h + · simp only [h, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte, ite_self] + exact hsig + · simp only [h, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + exact hsig + -- the spec + have hmem : ((), σ') ∈ (handleRecv (Z := Unit) false canReply σ).1 := by + have capReg : (L.mem s (regWA t layout.reg.CAP)).bv = xt.tcbArch.tcbContext.userRegs cap_register := by + rw [← r.regs cap_register, regIdxA_capA]; rfl + have capRead : NonDet.liftM dataToCptr (asUser (Z := Unit) (specPtr t) (getRegister cap_register)) σ = + ({(toBl (L.mem s (regWA t layout.reg.CAP)).bv, σ)}, False) := by + unfold NonDet.liftM + rw [nd_bind_singleA' (asUser_getRegister_eqA hkt _), ← capReg] + rfl + have hc0 : (lookupCap (Z := Unit) (specPtr t) (toBl (L.mem s (regWA t layout.reg.CAP)).bv)).run σ = + ({(.ok (absCapA (.Notification p badge send true)), σ)}, False) := by + have l := post_of_okA (lookup_cap_recv_refinesA (st := st) _ s σ J.j.1.st J.j.1.cv t hthT _) hcap + obtain ⟨e, he, hrel⟩ := l + cases e with + | error _ => exact hrel.elim + | ok ac => + obtain rfl : absCapA (.Notification p badge send true) = ac := hrel.2 + exact he + have hc1 : (capFaultOnFailure (ofBl (toBl (L.mem s (regWA t layout.reg.CAP)).bv)) true + (lookupCap (Z := Unit) (specPtr t) (toBl (L.mem s (regWA t layout.reg.CAP)).bv))).run σ = + ({(.ok (absCapA (.Notification p badge send true)), σ)}, False) := run_handleE_okA hc0 + have hrecv : ((), σ') ∈ (receiveSignal (Z := Unit) (specPtr t) + (absCapA (.Notification p badge send true)) false σ).1 := by + rw [show absCapA (.Notification p badge send true) = + .notificationCap (specPtr p) badge.bv (rightsOfA send true false false) from rfl, + receiveSignal_notActive hk hna] + unfold receiveSignalBlocked doNbrecvFailedTransfer + simp only [Bool.false_eq_true, ↓reduceIte] + rw [nd_bind_one (asUser_setRegister_eq _ _ hkt)] + exact Set.mem_singleton _ + rw [handleRecv, nd_gets_bindSc, ct, nd_bind_oneSc capRead] + apply mem_bindA.2 + refine ⟨(.ok (), σ'), ?_, rfl⟩ + rw [run_bind_okA hc1] + dsimp only [absCapA] + have boundRead : (NonDetE.liftE (ε := Fault) + (getNtfnObjRef (Z := Unit) Notification.ntfnBoundTcb (specPtr p))).run σ = + ({(.ok x.ntfnBoundTcb, σ)}, False) := run_liftEA (getNtfnObjRef_eqA hk _) + rw [if_pos (by simp [AllowRecv, rightsOfA]), run_bind_okA boundRead, if_pos (by rwa [ct] at hbt)] + unfold NonDetE.liftE + rw [ExceptT.run_lift] + exact mem_bindA.2 ⟨((), σ'), hrecv, rfl⟩ + have hc : m.curCore < fl4A64Platform.numNodes := by rw [J.j.1.sch.core]; exact hCs _ J.j.1.sch.cpuCs + obtain ⟨Q, Rl, J2⟩ := J.toJStage3A.of_badgeSwA hA hPl rfl hc hmb hmax htu J.body.sw J.noSelf htZ hkt 0#u64 + refine ⟨s', a, σ', hfl, hmem, ⟨xt, hkc, by rw [ct]⟩, A64RelS3.ofJ J2 ?bo, curW_keptA J.toJStage3A J2 rfl, ?bo⟩ + have := J.bodyOK_badgeA hkc + rwa [ct] at this + +set_option maxHeartbeats 1000000 in +/-- **`RecvPreA.nbWaitIdleA` at a syscall entry.** -/ +theorem JSyscallA.nbWaitIdleA + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p badge : U64} {send canReply : Bool} + (hcap : cspace.lookup_cap st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.CAP)) = + ok (.Ok (.Notification p badge send true))) + (hp : p ∈ N) {x : Notification} (hk : σ.kheap (specPtr p) = some (.notification x)) + (hna : ∀ b, x.ntfnObj ≠ .activeNtfn b) + (hbt : x.ntfnBoundTcb = some σ.curThread ∨ x.ntfnBoundTcb = none) : + HRecvPostA st AR ar hPl.ipa n Z.Cs s a m false canReply σ + (fun σ' => ∃ y, σ.kheap σ.curThread = some (.tcb y) ∧ + σ' = updK σ σ.curThread (.tcb (setReg y badge_register 0))) := + RecvPreA.nbWaitIdleA hA hPl J.toRecvPreA hCs hmb hmax htu hcap hp hk hna hbt + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3RecvPreA.lean b/proofs/Fl4ProofsA64/P3/Stage3RecvPreA.lean new file mode 100644 index 000000000..cd9b8268a --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3RecvPreA.lean @@ -0,0 +1,506 @@ +/- +P3 (aarch64, A64-P3 line RecvPreA): **the precondition of the `Recv` / `Wait` arms**, `RecvPreA`. + +`handle_recv` runs either at a syscall entry (`SysRecv`, `SysWait`, ...) or after the first half of `ReplyRecv`, +`NBSendRecv` or `NBSendWait` inside the same kernel-entry body. `RecvPreA` is what both give: the unified Stage-3 +record, the body-end facts `BodyOKA`, and the current thread running or restarting (not in a VM), in no ready queue, +outside the release queue, with an optional scheduling context (active when bound) and not itself the pending switch +target. A switch to another thread may be pending. + +* `RecvPreA`, **`JSyscallA.toRecvPreA`**. +* `SIKA.swKeep`, `SIKA.swA`: `schedulable` / `SwA` across a `SIKA` step. +* **`SIKA.sinvSw`**: `SIKA.sinv` with a pending switch allowed. +* **`BodyOKA.stepOwn`** / **`BodyOKA.step`**: the body-end facts across a step that keeps what they read (`stepOwn` + takes the owner clause at the end state). + +Named premises: none (the fields are [J] facts the first half proves). New module. +-/ +import Fl4ProofsA64.P3.Stage3SendQA +import Fl4ProofsA64.P3.Stage3SendNarrowA +import Fl4ProofsA64.P3.Stage3SendRecvFactsA +import Fl4ProofsA64.P3.Stage3RecvBufA +import Fl4ProofsA64.P3.Stage3RqDomPrioA +import Fl4ProofsA64.P3.Stage3SchedOpsTA +import Fl4ProofsA64.P3.Stage3SignalFootA +import Fl4ProofsA64.P3.Stage3DispBaseA +import Fl4ProofsA64.P3.Stage3SignalIdleA +import Fl4ProofsA64.P3.Stage3BodyOKA +import Fl4ProofsA64.P3.Stage3IpcKeepSA +import Fl4ProofsA64.P3.Stage3RestartA +import Fl4ProofsA64.P3.Stage3SchedHoareA +import Fl4ProofsA64.P3.Stage3SysPlaceA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + +noncomputable section + +section +variable {i : ObjRef} {M N : ObjRef → Prop} {don : Bool} {σ σ' : State Unit} + +/-- **A non-moving schedulable thread stays schedulable** across a `SIKA` step that keeps its bound SC. -/ +theorem SIKA.swKeep (R : SIKA i M N don σ σ') {u : ObjRef} (hs : schedulable u σ = true) (hM : ¬ M u) + (hsc : don = true → ∀ x y, σ.kheap u = some (.tcb x) → σ'.kheap u = some (.tcb y) → + y.tcbSchedContext = x.tcbSchedContext) : schedulable u σ' = true := by + obtain ⟨⟨x, sc, hx, hxs, k, n, hk, act⟩, hrel, z, hz, hr⟩ := schedulable_actA hs + rw [hx] at hz + cases hz + obtain ⟨y, hy, g⟩ := R.tcbF hx + obtain ⟨k', hk', gs⟩ := R.scF hk + have es : y.tcbSchedContext = some sc := by + rw [← hxs] + cases hd : don with + | false => + rcases g.sc with e | e + · exact e + · rw [hd] at e; cases e.1 + | true => exact hsc hd x y hx hy + refine schedulable_ofA hy (by rw [g.st.resolve_right fun a => hM a.1]; exact hr) es hk' ?_ + (fun h => hrel (R.rls u h)) + rw [gs.rm] + simpa [scActive, activeSc] using act + +/-- **`SwA`** across a `SIKA` step: a kept pending target does not move; a new one is `schedulable` at the end. -/ +theorem SIKA.swA (R : SIKA i M N don σ σ') (hsw : SwA σ) + (hM : ∀ u, σ.schedulerAction = .switchThread u → ¬ M u) + (hsc : don = true → ∀ u, σ.schedulerAction = .switchThread u → ∀ x y, σ.kheap u = some (.tcb x) → + σ'.kheap u = some (.tcb y) → y.tcbSchedContext = x.tcbSchedContext) + (hN : ∀ y, N y → σ'.schedulerAction = .switchThread y → schedulable y σ' = true) : SwA σ' := by + intro t ht + rcases R.act with e | e | ⟨y, hy, e⟩ + · rw [e] at ht + exact R.swKeep (hsw t ht) (hM t ht) (fun d => hsc d t ht) + · rw [e] at ht; cases ht + · rw [e] at ht + cases ht + exact hN _ hy e + +/-- **`SInvXA` across a `SIKA` step with a switch to another thread possibly pending** (`SIKA.sinv` without +`noSwitch`): the pending target is `schedulable`, is not the current thread and does not move. -/ +theorem SIKA.sinvSw {m : McState} {n : Nat} (R : SIKA σ.idleThread M N don σ σ') + (h : SInvXA σ m n) (hI : SIdleA σ m n) (hsw : SwA σ) + (hns : σ.schedulerAction ≠ .switchThread σ.curThread) + (hMsw : ∀ u, σ.schedulerAction = .switchThread u → ¬ M u) + (hMi : ∀ c < n, ¬ M (m.idleThreadOf c)) (hMI : ¬ M σ.idleThread) + (hMq : ∀ y, M y → ∀ d p, y ∉ σ.readyQueues d p) + (hN : ∀ y, N y → y ≠ σ.curThread ∧ y ≠ σ.idleThread ∧ (∀ c < n, m.idleThreadOf c ≠ y) ∧ + ∀ z, σ'.kheap y = some (.tcb z) → runnable z.tcbState = true) + (hN' : ∀ y, N y → ∃ z, σ.kheap y = some (.tcb z)) + (hMrls : ∀ y, M y → y ∉ σ.releaseQueue) + (hRdy : RdyA σ) + (hMf : ∀ p, M p → ∀ x y, σ.kheap p = some (.tcb x) → σ'.kheap p = some (.tcb y) → y.tcbFault ≠ none → + FaultStA y.tcbState) : SInvXA σ' m n := by + by_cases hp : ∃ u, σ.schedulerAction = .switchThread u + swap + · exact R.sinv h hMi hMI hMq hN hN' (fun u e => hp ⟨u, e⟩) hMrls hRdy hMf + obtain ⟨u, hu⟩ := hp + obtain ⟨-, -, x, hx, hr⟩ := schedulable_actA (hsw u hu) + let σ0 : State Unit := { σ with schedulerAction := .chooseNewThread } + have h0 : SInvXA σ0 m n := + { mc := h.mc, view := h.view, curTcb := h.curTcb, idleTcb := h.idleTcb, onlyIdle := h.onlyIdle, + idleSc := h.idleSc, curSc := h.curSc, ctQ := (fun e => by cases e), idleQ := h.idleQ, rq := h.rq, + rls := h.rls, rpos := h.rpos, irqN := SpecIrqA.of_eq h.irqN rfl rfl, zc := h.zc, rlsRun := h.rlsRun, curScReady := h.curScReady, + faultSt := h.faultSt } + have R0 : SIKA σ0.idleThread M (fun y => N y ∨ y = u) don σ0 σ' := by + refine ⟨R.frame, R.obj, fun d p y hy => ?_, R.rls, ?_, fun g => R.rdy ?_⟩ + · rcases R.rq d p y hy with a | a | a + · exact Or.inl a + · exact Or.inr (Or.inl (Or.inl a)) + · rw [hu] at a; cases a + exact Or.inr (Or.inl (Or.inr rfl)) + · rcases R.act with e | e | ⟨y, hy, e⟩ + · exact Or.inr (Or.inr ⟨u, Or.inr rfl, e.trans hu⟩) + · exact Or.inr (Or.inl e) + · exact Or.inr (Or.inr ⟨y, Or.inl hy, e⟩) + · exact RdyA.ofFrameA rfl rfl rfl rfl g + have hui : u ≠ σ.idleThread := fun e => by + obtain ⟨z, hz, hs⟩ := h.idleTcb + rw [← e, hx] at hz + cases hz + rw [hs] at hr + cases hr + have huc : ∀ c < n, m.idleThreadOf c ≠ u := fun c hc e => by + obtain ⟨z, hz, hs⟩ := hI.f3 c hc + rw [e, hx] at hz + cases hz + rw [hs] at hr + cases hr + refine R0.sinv h0 hMi hMI hMq (fun y hy => ?_) (fun y hy => ?_) (fun v e => by cases e) hMrls + (RdyA.ofFrameA rfl rfl rfl rfl hRdy) hMf + · rcases hy with hy | rfl + · exact hN y hy + refine ⟨fun e => hns (e ▸ hu), hui, huc, fun z hz => ?_⟩ + obtain ⟨y1, hy1, g⟩ := R.tcbF hx + rw [hz] at hy1 + cases hy1 + rw [g.st.resolve_right fun a => hMsw _ hu a.1] + exact hr + · rcases hy with hy | rfl + exacts [hN' y hy, ⟨x, hx⟩] + +end + +/-- **The body-end facts across a step**, with the owner clause given at the end: the clock, the current thread and +SC, the consumed time and the current SC's refills and bound are kept; `SwA` holds at the end, and when resuming the +current thread is running or restarting. -/ +theorem BodyOKA.stepOwn {σ σ' : State Unit} {m : McState} (B : BodyOKA σ m) (hsw : SwA σ') + (htime : σ'.curTime = σ.curTime) + (hlast : σ'.machineState.lastMachineTime = σ.machineState.lastMachineTime) + (hthr : σ'.curThread = σ.curThread) (hsc : σ'.curSc = σ.curSc) (hcon : σ'.consumedTime = σ.consumedTime) + (hobj : ∀ y' k, σ'.kheap σ.curSc = some (.schedContext y' k) → ∃ y, σ.kheap σ.curSc = some (.schedContext y k) ∧ + y'.scRefills = y.scRefills ∧ y'.scRefillMax = y.scRefillMax) + (hown : ∀ y' k, σ'.kheap σ.curSc = some (.schedContext y' k) → scActive y' = true → σ.curSc ≠ idleScPtr → + σ.consumedTime ≠ 0 → ∀ o, y'.scTcb = some o → o ∉ σ'.releaseQueue) + (hrun : σ'.schedulerAction = .resumeCurThread → ∀ x, σ'.kheap σ.curThread = some (.tcb x) → + x.tcbState = .running ∨ x.tcbState = .restart) + (hres : σ'.schedulerAction = .resumeCurThread → σ.schedulerAction = .resumeCurThread := by + first + | exact fun h => h + | (intro h; first | assumption | (rcases (‹SIKA _ _ _ _ _ _›).act with e | e | ⟨y, -, e⟩ <;> simp_all)) + | (intro h; simp_all)) : + BodyOKA σ' m := by + have hd : ∀ y' y : SchedContext, y'.scRefills = y.scRefills → refillHd y' = refillHd y := fun y' y e => by + unfold refillHd; rw [e] + have ha : ∀ y' y : SchedContext, y'.scRefillMax = y.scRefillMax → scActive y' = scActive y := fun y' y e => by + unfold scActive; rw [e] + refine ⟨⟨?_, fun y' k hk hc hg => ?_, fun h x hx => ?_⟩, hsw, fun h => ?_, fun y' k hk act hni hpos o ho => ?_⟩ + · have := B.validTime + unfold ValidMachineTimeA at this ⊢ + rw [htime, hlast]; exact this + · rw [hsc] at hk + obtain ⟨y, hy, e1, e2⟩ := hobj y' k hk + have b := B.budget y k hy (by rw [hsc, ha _ _ e2] at hc; exact hc) (by + rcases hg with h | h + · exact Or.inl (by rw [hcon] at h; exact h) + · exact Or.inr (hres h)) + constructor + · intro z j hz + rw [hsc, hk] at hz + obtain ⟨rfl, -⟩ := KernelObject.schedContext.inj (Option.some.inj hz) + rw [hd _ _ e1, hcon, htime] + exact b.1 y k hy + · intro hp z j hz hactive + rw [hsc, hk] at hz + obtain ⟨rfl, -⟩ := KernelObject.schedContext.inj (Option.some.inj hz) + rw [hd _ _ e1, hcon, htime] + rw [hcon] at hp + refine b.2 hp y k hy fun h => ?_ + rw [← ha _ _ e2]; exact hactive (hsc ▸ h) + · rcases hrun h x (hthr ▸ hx) with h | h <;> rw [h] + · exact Or.inl rfl + · exact Or.inr (Or.inl rfl) + · rw [hsc] at h ⊢; exact B.scNotIdle h + · rw [hsc] at hk hni + rw [hcon] at hpos + exact hown y' k hk act hni hpos o ho + +/-- **The body-end facts across a step that keeps what they read**: the clock, the current thread and SC, the +consumed time and the current SC's refills and bound; the current SC's thread does not enter the release queue, +the current SC may lose its bound thread, `SwA` holds at the end, and when resuming the current thread is running or restarting. -/ +theorem BodyOKA.step {σ σ' : State Unit} {m : McState} (B : BodyOKA σ m) (hsw : SwA σ') + (htime : σ'.curTime = σ.curTime) + (hlast : σ'.machineState.lastMachineTime = σ.machineState.lastMachineTime) + (hthr : σ'.curThread = σ.curThread) (hsc : σ'.curSc = σ.curSc) (hcon : σ'.consumedTime = σ.consumedTime) + (hrel : ∀ y k, σ.kheap σ.curSc = some (.schedContext y k) → ∀ o, y.scTcb = some o → o ∈ σ'.releaseQueue → + o ∈ σ.releaseQueue) + (hobj : ∀ y' k, σ'.kheap σ.curSc = some (.schedContext y' k) → ∃ y, σ.kheap σ.curSc = some (.schedContext y k) ∧ + y'.scRefills = y.scRefills ∧ y'.scRefillMax = y.scRefillMax ∧ (y'.scTcb = y.scTcb ∨ y'.scTcb = none)) + (hrun : σ'.schedulerAction = .resumeCurThread → ∀ x, σ'.kheap σ.curThread = some (.tcb x) → + x.tcbState = .running ∨ x.tcbState = .restart) + (hres : σ'.schedulerAction = .resumeCurThread → σ.schedulerAction = .resumeCurThread := by + first + | exact fun h => h + | (intro h; first | assumption | (rcases (‹SIKA _ _ _ _ _ _›).act with e | e | ⟨y, -, e⟩ <;> simp_all)) + | (intro h; simp_all)) : + BodyOKA σ' m := by + refine B.stepOwn hsw htime hlast hthr hsc hcon (fun y' k h => ?_) (fun y' k hk act hni hpos o ho => ?_) hrun hres + · obtain ⟨y, a, b, c, -⟩ := hobj y' k h + exact ⟨y, a, b, c⟩ + · obtain ⟨y, hy, -, e2, e3⟩ := hobj y' k hk + rcases e3 with e3 | e3 + · rw [e3] at ho + have : scActive y = true := by + rw [show scActive y = scActive y' by unfold scActive; rw [e2]]; exact act + exact fun hm => B.owner y k hy this hni hpos o ho (hrel y k hy o ho hm) + · rw [e3] at ho; cases ho + +end + +/-- `BodyOKA.step` with the release queue and the current SC's bound thread kept. -/ +theorem BodyOKA.stepEq {σ σ' : State Unit} {m : McState} (B : BodyOKA σ m) (hsw : SwA σ') + (htime : σ'.curTime = σ.curTime) + (hlast : σ'.machineState.lastMachineTime = σ.machineState.lastMachineTime) + (hthr : σ'.curThread = σ.curThread) (hsc : σ'.curSc = σ.curSc) (hcon : σ'.consumedTime = σ.consumedTime) + (hrel : σ'.releaseQueue = σ.releaseQueue) + (hobj : ∀ y' k, σ'.kheap σ.curSc = some (.schedContext y' k) → ∃ y, σ.kheap σ.curSc = some (.schedContext y k) ∧ + y'.scRefills = y.scRefills ∧ y'.scRefillMax = y.scRefillMax ∧ y'.scTcb = y.scTcb) + (hrun : σ'.schedulerAction = .resumeCurThread → ∀ x, σ'.kheap σ.curThread = some (.tcb x) → + x.tcbState = .running ∨ x.tcbState = .restart) + (hres : σ'.schedulerAction = .resumeCurThread → σ.schedulerAction = .resumeCurThread := by + first + | exact fun h => h + | (intro h; first | assumption | (rcases (‹SIKA _ _ _ _ _ _›).act with e | e | ⟨y, -, e⟩ <;> simp_all)) + | (intro h; simp_all)) : + BodyOKA σ' m := + B.step hsw htime hlast hthr hsc hcon (fun _ _ _ _ _ h => hrel ▸ h) + (fun y' k h => let ⟨y, a, b, c, d⟩ := hobj y' k h; ⟨y, a, b, c, Or.inl d⟩) hrun hres + +/-- `SwA` across a write of an object that is neither a TCB nor an SC. -/ +theorem SwA.updOther {σ : State Unit} (h : SwA σ) {p : ObjRef} {o0 o : KernelObject} (hk : σ.kheap p = some o0) + (h1 : ∀ x, o0 ≠ .tcb x) (h2 : ∀ y k, o0 ≠ .schedContext y k) : SwA (updK σ p o) := by + intro t ht + obtain ⟨⟨x, sc, hx, hxs, y, k, hy, act⟩, hrel, z, hz, hr⟩ := schedulable_actA (h t ht) + rw [hx] at hz + cases hz + have n1 : t ≠ p := fun e => by + rw [e, hk] at hx + exact h1 _ (Option.some.inj hx) + have n2 : sc ≠ p := fun e => by + rw [e, hk] at hy + exact h2 _ _ (Option.some.inj hy) + refine schedulable_ofA (x := x) (by rw [updK_kheap, Function.update_of_ne n1]; exact hx) hr hxs + (y := y) (n := k) (by rw [updK_kheap, Function.update_of_ne n2]; exact hy) ?_ hrel + simpa [scActive, activeSc] using act + +end Fl4ProofsA64.P3.Sched + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 + +noncomputable section + +section +variable {S : Type} (st : machine.Store S) [L : LawfulStore st] {A : Type} + +/-- **The precondition of the `Recv` / `Wait` arms** (module docstring). -/ +structure RecvPreA (AR : A → HwRootA → Prop) (Z : XSetsA) (H : Heap) (OS U Ac Pp Tb : Set U64) + (Lv : U64 → Nat) (ia ie : Nat) (N E Rp : Set U64) (Bf : Set (U64 × Fl4SpecA64.Word)) (stack : U64 → List U64) + (κ : U64 → U64) (n : Nat) (J5 J7 : JSlotA S) (s : S) (a : A) (σ : Fl4SpecA64.State Unit) + (m : Fl4SpecA64.McState) : Prop + extends JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m where + /-- [J] The body-end facts. -/ + body : Sched.BodyOKA σ m + /-- [J] The current thread is running or restarting (not in a VM). -/ + running : L.mem s (stA (L.curW s)) = sched.TS_RUNNING ∨ L.mem s (stA (L.curW s)) = sched.TS_RESTART + /-- [J] The current thread is in no ready queue. -/ + noQ : ∀ d p, σ.curThread ∉ σ.readyQueues d p + /-- [J] The current thread is not in the release queue. -/ + notRel : σ.curThread ∉ σ.releaseQueue + /-- [J] The current thread's SC is optional; a bound one is active. -/ + scAct : ∀ x sc, σ.kheap σ.curThread = some (.tcb x) → x.tcbSchedContext = some sc → ScActiveAtA σ sc + /-- [J] A pending switch is to another thread. -/ + noSelf : σ.schedulerAction ≠ .switchThread σ.curThread + +variable {st} {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +theorem RecvPreA.runW (J : RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) : + Runnable (L.mem s (stA (L.curW s))) := + J.running.imp_right Or.inl + +theorem RecvPreA.nvm (J : RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) : + L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM := by + rcases J.running with e | e <;> rw [e] <;> + simp [sched.TS_RUNNING, sched.TS_RESTART, sched.TS_RUNNING_VM] + +/-- The current thread's spec state. -/ +theorem RecvPreA.state (J : RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {x : Tcb} (hk : σ.kheap (specPtr (L.curW s)) = some (.tcb x)) : + x.tcbState = .running ∨ x.tcbState = .restart := by + have h1 := (J.ep.at_tcb J.sr.curT hk).st + rcases J.running with e | e <;> rw [e] at h1 <;> + (rcases hs : x.tcbState with _ | _ | _ | _ | _ | _ | _ | _ | ⟨⟨⟩⟩ <;> rw [hs] at h1 <;> + simp [tsCode, sched.TS_RUNNING, sched.TS_RESTART] at h1 ⊢) + +theorem RecvPreA.notIdle (J : RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) : + specPtr (L.curW s) ≠ σ.idleThread := by + intro h + obtain ⟨y, hy, hs⟩ := J.sinv.idleTcb + rw [← h] at hy + rcases J.state hy with e | e <;> rw [e] at hs <;> cases hs + +/-- The current thread is no core's idle thread. -/ +theorem RecvPreA.notCoreIdle (J : RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {c : Nat} (hc : c < n) : specPtr (L.curW s) ≠ m.idleThreadOf c := by + intro h + obtain ⟨y, hy, hs⟩ := J.sidle.f3 c hc + rw [← h] at hy + rcases J.state hy with e | e <;> rw [e] at hs <;> cases hs + +/-- The current thread as a sender / receiver: runnable, queued nowhere, not the pending target, not idle. -/ +theorem RecvPreA.sndRun (J : RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {x : Tcb} (hk : σ.kheap (specPtr (L.curW s)) = some (.tcb x)) : SndRunA σ (specPtr (L.curW s)) x := by + have ct : σ.curThread = specPtr (L.curW s) := J.sr.sch.curThread + refine ⟨?_, ⟨fun d p => ct ▸ J.noQ d p, ct ▸ J.notRel⟩, fun u hu e => ?_, J.notIdle⟩ + · rcases J.state hk with e | e <;> rw [e] <;> rfl + · exact J.noSelf (by rw [hu, e, ct]) + +theorem RecvPreA.boundActive (J : RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) : + Sched.BoundActive (specPtr (L.curW s)) σ := by + have ct : σ.curThread = specPtr (L.curW s) := J.sr.sch.curThread + exact fun x sc hx hs => ⟨x, sc, hx, hs, J.scAct x sc (ct ▸ hx) hs⟩ + +/-- The pending switch target is runnable, and is not the current thread. -/ +theorem RecvPreA.pending (J : RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {u : ObjRef} (hu : σ.schedulerAction = .switchThread u) : + u ≠ specPtr (L.curW s) ∧ u ∉ σ.releaseQueue ∧ + ∃ x, σ.kheap u = some (.tcb x) ∧ Fl4SpecA64.runnable x.tcbState = true := by + have ct : σ.curThread = specPtr (L.curW s) := J.sr.sch.curThread + obtain ⟨-, h1, h2⟩ := Sched.schedulable_actA (J.body.sw u hu) + exact ⟨fun e => J.noSelf (by rw [hu, e, ct]), h1, h2⟩ + +/-- **`CanBlock` of the current thread**: queued nowhere, not the switch-to candidate, its SC (if any) active. -/ +theorem RecvPreA.canBlock (J : RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) : + CanBlock st H.tcbs Ac s (L.curW s) where + mem := J.sq.curT + nb := fun hb => by + rcases J.running with h | h <;> rcases hb with h' | h' | h' <;> rw [h] at h' <;> + exact absurd (congrArg UScalar.val h') (by + first + | (simp [sched.TS_RUNNING, sched.TS_RESTART, sched.TS_BLOCKED_ON_SEND, sched.TS_BLOCKED_ON_RECV, + sched.TS_BLOCKED_ON_NTFN]; done) + | decide) + nrp := fun h' => by + rcases J.running with h | h <;> rw [h] at h' <;> + exact absurd (congrArg UScalar.val h') (by + first + | (simp [sched.TS_RUNNING, sched.TS_RESTART, sched.TS_BLOCKED_ON_REPLY]; done) + | decide) + nq := by + by_contra hq + have hc : L.curW s ∈ H.tcbs := J.sq.curT + have mem := (J.sq.queued (L.curW s) hc).1 hq + rw [J.sr.curOn] at mem + have hp : (L.mem s (prioA (L.curW s))).val < 256 := J.sq.prio _ hc + have hr := J.sr.rest.rq _ hp + have ct : σ.curThread = specPtr (L.curW s) := J.sr.sch.curThread + refine J.noQ 0 (BitVec.ofNat 8 (L.mem s (prioA (L.curW s))).val) ?_ + rw [hr, ct] + exact List.mem_map_of_mem mem + nr := by + by_contra hb + have h := (J.sr.core.released (L.curW s) J.sr.curT).1 hb + rw [J.sr.curOn] at h + refine J.notRel ?_ + rw [J.sr.rest.rel, J.sr.sch.curThread] + exact List.mem_map.2 ⟨_, h, rfl⟩ + na := fun e => by + have h0 : L.curW s ≠ 0#u64 := J.toJStage3A.tcb_ne0 J.sq.curT + have h1 : L.curW s ≠ 1#u64 := fun c => by + have hH : H (L.curW s) = some .tcb := J.sq.curT + have w := (J.heapWin _ _ hH).1 + rw [c] at w + simp [layout.PPTR_BASE] at w + refine J.noSelf ?_ + rw [J.sr.sch.act, e, J.sr.sch.curThread] + unfold actOfA + rw [if_neg h0, if_neg h1] + ni := J.toJStage3A.cur_ne_idle J.runW + ac := fun h0 => by + have ht := J.sr.curT + obtain ⟨x, hx, ex⟩ := J.sr.sch.tcb (L.curW s) ht + have hs : x.tcbSchedContext = some (scRenA m (L.mem s (scA (L.curW s)))) := by + rw [ex, optScA, if_neg h0] + obtain ⟨y, k, hy, act⟩ := J.scAct x _ (by rw [J.sr.sch.curThread]; exact hx) hs + have p : Fl4Spec.optPred (fun sc => Fl4SpecA64.scActive sc = true) + (Fl4Spec.optMap (Fl4Spec.optMap (Fl4SpecA64.tcbsOf σ) Tcb.tcbSchedContext) (Fl4SpecA64.scsOf σ)) + (specPtr (L.curW s)) := by + simpa [Fl4Spec.optPred, Fl4Spec.optMap, tcbsOfKh, tcbOf, scsOfKh, scOf, hx, hs, hy] using act + have r := (J.sr.toSchedSRN.scPart ht).1 p + have hc : L.mem s (scA (L.curW s)) ∈ H.scs := J.scs.symm ▸ J.sr.rest.scMem _ ht h0 + refine (J.sys.ent.bxi.2.2.2.2.act _ hc).2 fun c => ?_ + have := r.2 + rw [c] at this + exact absurd this (by simp) + +/-- The walk's blocking precondition (`DecPre.cb`). -/ +structure RecvCbA (st : machine.Store S) [L : LawfulStore st] (T Ac : Set U64) (s : S) : Prop where + cb : CanBlock st T Ac s (L.curW s) + +theorem RecvPreA.decPreA (J : RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {x : U64} (_ : x ∈ OS) {extra : Aeneas.Std.Array obj.Slot 3#usize} + (_ : ∀ y ∈ extra.val, y ≠ 0#u64 → y ∈ OS) : RecvCbA st H.tcbs Ac s := + ⟨J.canBlock⟩ + +/-- A pending switch target is not the current thread. -/ +theorem RecvPreA.swCur (J : RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (u : ObjRef) (hu : σ.schedulerAction = .switchThread u) : ¬ u = specPtr (L.curW s) := + (J.pending hu).1 + +/-- A pending switch target is not a thread whose state is not runnable. -/ +theorem RecvPreA.swBlocked (J : RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {t : ObjRef} {x : Tcb} (hk : σ.kheap t = some (.tcb x)) (hb : Fl4SpecA64.runnable x.tcbState = false) + (u : ObjRef) (hu : σ.schedulerAction = .switchThread u) : ¬ u = t := fun e => by + obtain ⟨-, -, z, hz, hr⟩ := J.pending hu + rw [e, hk] at hz + cases hz + rw [hb] at hr + cases hr + +/-- **The current SC's words** when the current thread holds the current SC. -/ +theorem RecvPreA.ownWords (J : RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (hown : ∀ x, σ.kheap σ.curThread = some (.tcb x) → x.tcbSchedContext = some σ.curSc) : + L.mem s (scA (L.curW s)) ≠ 0#u64 ∧ L.cscW s = L.mem s (scA (L.curW s)) := by + have ht := J.sr.curT + have hcur := J.sr.sch.curThread + obtain ⟨x, kx, ex⟩ := J.sr.sch.tcb (L.curW s) ht + have hx := hown x (by rw [hcur]; exact kx) + rw [ex] at hx + have nz : L.mem s (scA (L.curW s)) ≠ 0#u64 := by + intro e; rw [optScA, if_pos e] at hx; cases hx + have hs : σ.curSc = scRenA m (L.mem s (scA (L.curW s))) := by + rw [optScA, if_neg nz] at hx; exact (Option.some.inj hx).symm + have wC : L.mem s (scA (L.curW s)) ∈ Z.C := J.sr.rest.scMem _ ht nz + have hcs : σ.curSc = scRenA m (L.cscW s) := by + have := J.sr.sch + rw [← this.csc, this.core]; exact this.curScOfW _ this.cpuCs + exact ⟨nz, J.sr.scp.inj _ J.sr.cscC _ wC (hcs.symm.trans hs)⟩ + +/-- **From the syscall-entry record.** -/ +theorem JSyscallA.toRecvPreA (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) : + RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m := by + have ct : σ.curThread = specPtr (L.curW s) := J.sr.sch.curThread + have nvm := J.toJStage3EntryA.curNotVmA + have run : L.mem s (stA (L.curW s)) = sched.TS_RUNNING ∨ L.mem s (stA (L.curW s)) = sched.TS_RESTART := by + rcases J.running with e | e | e + exacts [Or.inl e, Or.inr e, absurd e nvm] + obtain ⟨xt, hkt, r⟩ := J.ep.tcb (L.curW s) J.sr.curT + have hs : xt.tcbState = .running ∨ xt.tcbState = .restart := by + have h1 := r.st + rcases run with e | e <;> rw [e] at h1 <;> + (rcases hs : xt.tcbState with _ | _ | _ | _ | _ | _ | _ | _ | ⟨⟨⟩⟩ <;> rw [hs] at h1 <;> + simp [tsCode, sched.TS_RUNNING, sched.TS_RESTART] at h1 ⊢) + have hidle : σ.curThread ≠ σ.idleThread := by + intro h + obtain ⟨y, hy, hy'⟩ := J.sinv.idleTcb + rw [← h, ct, hkt] at hy + cases hy + rcases hs with e | e <;> rw [e] at hy' <;> cases hy' + refine + { toJStage3A := J.toJStage3A + body := ?_ + running := run + noQ := fun d p => J.sinv.ctQ J.act d p + notRel := J.curInv.notRel + scAct := fun x sc hx hxs => ?_ + noSelf := J.noSwitch _ } + · refine Sched.BodyOKA.ofEntry J (Or.inl rfl) rfl rfl rfl rfl rfl rfl + (fun y k h => ⟨y, h, rfl, rfl, rfl⟩) (fun _ x hx => ?_) + rw [ct, hkt] at hx + cases hx + exact hs + · obtain ⟨y, c, ey, ec, a⟩ := (J.schedValid.act J.act hidle).1 + rw [hx] at ey + cases ey + rw [hxs] at ec + cases ec + exact a + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3RecvPushA.lean b/proofs/Fl4ProofsA64/P3/Stage3RecvPushA.lean new file mode 100644 index 000000000..b30de9fa7 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3RecvPushA.lean @@ -0,0 +1,508 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2c): **`Recv` with a call or faulted sender waiting and a reply object, at the record** +(the reply-push arm): `handle_recv` with `can_reply = true` on an endpoint whose head sender is a call or faulted +sender with grant or grant-reply, with an unlinked reply object named by the REPLY register and a receiver holding its own SC. The +message is transferred, the reply is pushed (nothing is donated), and the sender becomes `BlockedOnReply`. P1's facts +come from `receive_ipc.rpw`. + +* `recvPushFp_subA`: `RecvPushFpA`'s words are endpoint-code words, scheduler words or reply-object words. +* `JStage3A.ntfnWait_recvPushA`: the notification waiters across the push write set. +* **`RecvPreA.recvPushA`**: `HRecvPostA`. + +[S] the endpoint holds waiting senders; its head sender has no fault and transfers no extra caps, or has a fault in +`FaultOKA`, or a timeout fault (`hsndF`; the timeout sender holds an SC, nothing is donated: `can_donate = false`), +holds an SC and is a call or faulted sender with grant or grant-reply (`hsnd`); the REPLY register names a reply object `rr` linked to no thread +(`hcapR`, `hfree`); `can_reply = true`; no bound notification, or one that is not +active and (when blocking) whose SC is not the receiver's (`BndOKA`); `numNodes = 1`. +The receiver holds an SC unless the sender has a timeout fault (`htsc0`; a timeout push never donates, so a passive +receiver is covered there). At a syscall entry it always does (`CurInvA.tcb`). +[P] `ArchTotal`, `ArchPlat`, `hCs`, `hmb`, `hmax`, `htu`. New module. +-/ +import Fl4ProofsA64.P3.Stage3RecvFrontA +import Fl4ProofsA64.P3.Stage3RecvSendA +import Fl4ProofsA64.P3.Stage3RecvPushFpA +import Fl4ProofsA64.P3.Stage3RecvTmoPushFpA +import Fl4ProofsA64.P3.Stage3RecvPollA +import Fl4ProofsA64.P3.Stage3IpcGlueUA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4Spec Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {J5 J7 : JSlotA S} {R : Set U64 → JSlotA S} {s s' : S} {a : A} {σ σ' : State Unit} + {m : McState} + +/-- **`RecvPushFpA`'s words** (module docstring). -/ +theorem recvPushFp_subA (PE : EPlaced Z.T E Rp Bf) {t e sd b : U64} (ht : t ∈ Z.T) (he : e ∈ E) (hsd : sd ∈ Z.T) + (h : RecvPushFpA st Z m Rp Bf σ s t e sd b) : + EWrite Z.T E Rp Bf b ∨ MWrA (st := st) Z m σ s b ∨ RepWA Rp b := by + have fld : ∀ u ∈ Z.T, ∀ k, TWOffN k → b.val = u.val + k → EWrite Z.T E Rp Bf b := fun u hu k hk hb => + Or.inr (Or.inr (Or.inl ⟨u, hu, Or.inl ⟨by omega, by rw [hb, Nat.add_sub_cancel_left]; exact hk⟩⟩)) + have st_ : ∀ u ∈ Z.T, b = stA u → EWrite Z.T E Rp Bf b := fun u hu c => + fld u hu 3328 (by unfold TWOffN; omega) (by rw [c]; exact Endp.val_st (PE.tcb u hu)) + rcases h with (c | c | c) | c | c + · rcases c with c | ⟨u, hu, -, c⟩ | ⟨r, c⟩ | c + · exact Or.inl (Or.inl ⟨e, he, c⟩) + · exact Or.inl (Or.inr (Or.inr (Or.inl ⟨u, hu, Or.inr c⟩))) + · have h1 := regIdxA_lt r + rw [Endp.regA_val (PE.tcb t ht) h1] at c + exact Or.inl (fld t ht (256 + 8 * (regIdxA r).val) (by unfold TWOffN; omega) (by omega)) + · exact Or.inl (Or.inr (Or.inr (Or.inr c))) + · exact Or.inl (st_ sd hsd c) + · exact Or.inr (Or.inl c) + · exact Or.inr (Or.inr c) + · exact Or.inl (fld sd hsd 3360 (by unfold TWOffN; omega) (by rw [c]; exact Endp.val_reply (PE.tcb sd hsd))) + +variable (J0 : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7A st R) s a σ m) in +include J0 in +/-- **`NtfnWaitKeepA`** across the push write set (module docstring). -/ +theorem JStage3A.ntfnWait_recvPushA {t e sd : U64} (ht : t ∈ Z.T) (he : e ∈ E) (hsd : sd ∈ Z.T) + (hsb : L.mem s (Notif.tBlockA sd) = e) + (w : WritesIn st s s' (RecvPushFpA st Z m Rp Bf σ s t e sd)) : + NtfnWaitKeepA Z.T N (L.mem s) (L.mem s') := by + intro y hy hN + have hP := J0.iplaced + have hya := hP.e.tcb y hy + have hfy := hya.fits + have notE : ∀ u ∈ Z.T, L.mem s (Notif.tBlockA u) = e → u ≠ y := fun u _ hu c => by + subst c + rw [hu] at hN + have := hP.ne e hN e he + omega + have key : ∀ (K : U64) (b : U64), (K.val = 3336 ∨ K.val = 3496 ∨ K.val = 3504) → b = wa y K → Aligned b → + L.mem s' b = L.mem s b := by + intro K b hK hb hal + have hbv : b.val = y.val + K.val := by rw [hb, wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have apart : ∀ u ∈ Z.T, u ≠ y → u.val + 4096 ≤ y.val ∨ y.val + 4096 ≤ u.val := fun u hu ne => by + have := J0.sr.place.apart u y .tcb .tcb ((J0.sr.thT u).2 hu) ((J0.sr.thT y).2 hy) ne + have hsz : ObjKindA.size .tcb = 4096 := rfl + omega + refine w.mem_eq hal ?_ + rintro ((c | c | c) | ⟨q', hq', c1, c2⟩ | c) + · rcases c with c | ⟨u, hu, hub, c⟩ | ⟨r, c⟩ | c + · have := hP.e.et e he y hy; unfold EW at c; omega + · have := apart u hu (notE u hu hub) + have := (hP.e.tcb u hu).fits + unfold TL at c; omega + · have hft := (hP.e.tcb t ht).fits + have h1 := regIdxA_lt r + rw [Endp.regA_val (hP.e.tcb t ht) h1] at c + by_cases ety : t = y + · subst ety; omega + · have := apart t ht ety; omega + · have := hP.e.bt _ c y hy; omega + · have h1 : b.val = sd.val + 3328 := by rw [c]; exact Endp.val_st (hP.e.tcb sd hsd) + have := apart sd hsd (notE sd hsd hsb) + omega + · rw [hb] at c + exact J0.tcbWord_offMwrA hy (by omega) (by omega) c + · have := hP.e.rt q' hq' y hy + omega + · have h1 : b.val = sd.val + 3360 := by rw [c]; exact Endp.val_reply (hP.e.tcb sd hsd) + have := apart sd hsd (notE sd hsd hsb) + omega + refine ⟨?_, ?_, ?_⟩ + · exact key 3336#u64 _ (by simp) (by unfold Notif.tBlockA; rfl) (Endp.a_blk hya).1 + · exact key 3504#u64 _ (by simp) (by rw [prevEp_eq]) (Endp.a_prev hya).1 + · exact key 3496#u64 _ (by simp) (by rw [nextEp_eq]) (Endp.a_next hya).1 + +set_option maxHeartbeats 4000000 in +/-- **`Recv` with a call sender waiting, pushing the reply** (module docstring). -/ +theorem RecvPreA.recvPushA_core + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p badge slot rr rslot : U64} {send grant grantReply blk rg : Bool} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) + (hsc : capA st s slot = .Endpoint p badge send true grant grantReply) + (hrslot : rslot ∈ OS) (hrsc : capA st s rslot = .Reply rr rg) (hrr : rr ∈ Rp) + (hfree : L.mem s (Endp.rTcbA rr) = 0#u64) + (hp : p ∈ E) {sd : U64} {q : List ObjRef} + (hk : σ.kheap (specPtr p) = some (.endpoint (.sendEP (specPtr sd :: q)))) + (hbn : ∀ y, σ.kheap σ.curThread = some (.tcb y) → BndOKA N (L.mem s) σ (L.curW s) y blk) + (hsnd : ∀ y xt, σ.kheap (specPtr sd) = some (.tcb y) → σ.kheap σ.curThread = some (.tcb xt) → + y.tcbSchedContext ≠ none ∧ ∀ e' dd, + y.tcbState = .blockedOnSend e' dd → (dd.senderIsCall = true ∨ y.tcbFault ≠ none) ∧ + (dd.senderCanGrant = true ∨ dd.senderCanGrantReply = true)) + (hsndF : ∀ y xt, σ.kheap (specPtr sd) = some (.tcb y) → σ.kheap σ.curThread = some (.tcb xt) → + (y.tcbFault = none ∧ ∀ e' dd, y.tcbState = .blockedOnSend e' dd → dd.senderCanGrant = false ∨ + ipcBufOfA false y = none ∨ + unat (dataToMessageInfo (y.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0) ∨ + (∃ f, y.tcbFault = some f ∧ Endp.FaultOKA (L := L) s sd (ipcBufOfA true xt) y f) ∨ + ∃ b, y.tcbFault = some (.timeout b)) + (htsc0 : ∀ x y, σ.kheap σ.curThread = some (.tcb x) → σ.kheap (specPtr sd) = some (.tcb y) → + ¬ (y.tcbFault.elim false isTimeoutFault = true) → x.tcbSchedContext ≠ none) : + RecvCoreA st AR ar hPl.ipa n Z.Cs s a m (.Endpoint p badge send true grant grantReply) blk (.Reply rr rg) + (absCapA (.Endpoint p badge send true grant grantReply)) (.replyCap (specPtr rr) (replyRightsA rg)) σ + (fun σ' => ((), σ') ∈ (specRecvSendA σ.curThread (specPtr p) (some (specPtr rr)) (specPtr sd :: q) σ).1) := by + have htZ : L.curW s ∈ Z.T := J.sr.curT + set t := L.curW s with ht_def + have hSt := J.j.1.st + have hV := J.j.1.cv + have ct : σ.curThread = specPtr t := J.j.1.sch.curThread + obtain ⟨xt, hkt, r⟩ := J.ep.tcb t htZ + have hkc : σ.kheap σ.curThread = some (.tcb xt) := by rw [ct]; exact hkt + have hbnd := hbn xt hkc + have hthT : Z.th t = some .tcb := (J.sr.thT t).2 htZ + have hnvm : L.mem s (stA t) ≠ sched.TS_RUNNING_VM := J.nvm + have hrunS : xt.tcbState = .running ∨ xt.tcbState = .restart := J.state hkt + have hidle : specPtr t ≠ σ.idleThread := by + obtain ⟨y, hy, hs⟩ := J.sinv.idleTcb + intro h + rw [← h, hkt] at hy + cases hy + rcases hrunS with e | e <;> rw [e] at hs <;> cases hs + have hcm : m.curCore < fl4A64Platform.numNodes := by rw [J.j.1.sch.core]; exact hCs _ J.j.1.sch.cpuCs + have hO : SchedOpsEA st Z.T E Rp Bf (IpcSchedRelA st Z m κ) := + (ipcSchedOpsA (ar := ar) J.iplaced J.ipcPlace hcm hmb hmax htu).2.2.1 + have hF : SchedFootEA st Z.T E Rp Bf (IpcSchedRelA st Z m κ) (fun σ s => MWrA (st := st) Z m σ s) := + schedFootEA_of_sched + have hSR : IpcSchedRelA st Z m κ s σ := ⟨Z.Q, Z.R, J.sr⟩ + -- the sender + obtain ⟨hst1, xs0, -, hq0, hQ, hB⟩ := J.ep.at_ep hp hk + have hsdx : sd ∈ xs0 := by + have : specPtr sd ∈ xs0.map specPtr := by rw [← hq0]; exact List.mem_cons_self + obtain ⟨y, hy, e⟩ := List.mem_map.1 this + rwa [specPtr_inj e] at hy + have hsdT : sd ∈ Z.T := hQ.mem sd hsdx + have hs4 : (L.mem s (stA sd)).val = 4 := (hB sd hsdx).1 + have hsb : L.mem s (Notif.tBlockA sd) = p := (hB sd hsdx).2 + obtain ⟨xs, hks, rs⟩ := J.ep.tcb sd hsdT + obtain ⟨e', dd, hsst⟩ := tsCode_four (rs.st.symm.trans hs4) + obtain ⟨hssc, hsnd'⟩ := hsnd xs xt hks hkc + obtain ⟨hcall, hgr⟩ := hsnd' e' dd hsst + have hX : XferCaseA xs dd.senderCanGrant := by + rcases hsndF xs xt hks hkc with g | ⟨f, hf, -⟩ | ⟨b, hb⟩ + · exact Or.inl ⟨g.1, g.2 e' dd hsst⟩ + · exact Or.inr ⟨f, hf⟩ + · exact Or.inr ⟨_, hb⟩ + have htsc : ¬ (xs.tcbFault.elim false isTimeoutFault = true) → xt.tcbSchedContext ≠ none := htsc0 xt xs hkc hks + have hr0 : rr ≠ 0#u64 := fun eq => (J.iplaced.e.rep rr hrr).2.1 (by rw [eq]; rfl) + obtain ⟨xr, hkr, hxr⟩ := J.ep.reply rr hrr + have hxr0 : xr.replyTcb = none := by rw [hxr, hfree, optRef_zero] + have hts : specPtr t ≠ specPtr sd := fun h => by + rw [h, hks] at hkt + cases hkt + rcases hrunS with e | e <;> rw [e] at hsst <;> cases hsst + have hsdi : specPtr sd ≠ σ.idleThread := by + obtain ⟨x, hx, hs⟩ := J.sinv.idleTcb + intro h + rw [← h, hks] at hx + cases hx + rw [hsst] at hs + cases hs + have hsdth : Z.th sd = some .tcb := (J.sr.thT sd).2 hsdT + have hnb : tsCode xt.tcbState ≠ 4 := by rcases hrunS with e | e <;> rw [e] <;> simp [tsCode] + have hu := J.toJStage3A.bufFrameA htZ hkt true + have hal := J.toJStage3A.bufAlign0A htZ hkt true + have hoff := J.toJStage3A.slotOffEwA + -- FL4 and the spec: `receive_from_sender` + have push : endpoint.receive_from_sender st ar s a t p rr ⦃ out => out.2 = a ∧ + ∃ σ', specRecvSendA (specPtr t) (specPtr p) (some (specPtr rr)) (specPtr sd :: q) σ = ({((), σ')}, False) ∧ + EpStateRel Z.T E Rp Bf (L.mem out.1) σ' ∧ IpcSchedRelA st Z m κ out.1 σ' ∧ + ReplyStackRelA (L.mem out.1) Rp Z.C σ' (scNameA m) stack ∧ + WritesIn st s out.1 (RecvPushFpA st Z m Rp Bf σ s t p sd) ⦄ := by + by_cases htm : ∃ b, xs.tcbFault = some (.timeout b) + · -- a timeout fault of a sender with an SC: `update_consumed` writes the SC, nothing is donated + obtain ⟨bd, hf⟩ := htm + have hTO : TimeoutScOpA st ar Z.T E Rp Bf (IpcSchedRelA st Z m κ) + (fun u => ∀ c < fl4A64Platform.numNodes, specPtr u ≠ m.idleThreadOf c) := + (ipcSchedOpsA (ar := ar) J.iplaced J.ipcPlace hcm hmb hmax htu).2.2.2.1 + have hni := J.toJStage3A.idleNoFault.notIdle hks hf + obtain ⟨x0, hx0, hp0⟩ := J.sr.sch.tcb sd hsdT + have hscT : xs.tcbSchedContext = optScA m (L.mem s (scA sd)) := + (congrArg Tcb.tcbSchedContext (KernelObject.tcb.inj (Option.some.inj (hks.symm.trans hx0)))).trans hp0 + have hsc0 : L.mem s (scA sd) ≠ 0#u64 := fun h => hssc (by rw [hscT, h, optScA, if_pos rfl]) + have hname := J.sr.scName_of_notIdle hsdT hsc0 hni + have hsc' : xs.tcbSchedContext = some (specPtr (L.mem s (scA sd))) := by + rw [hscT, optScA, if_neg hsc0] + exact congrArg some hname + obtain ⟨y, k, hy, -⟩ := J.sr.inv.back _ xs _ hks hsc' + have hscW : ∀ b : U64, (L.mem s (scA sd)).val ≤ b.val ∧ + b.val < (L.mem s (scA sd)).val + 2 ^ (k + minSchedContextBits) → MWrA (st := st) Z m σ s b := + fun b hb => Or.inl ⟨_, J.sr.rest.scMem sd hsdT hsc0, y, k, by rw [hname]; exact hy, hb⟩ + exact receive_from_sender_pushTmoFpA (st := st) (ar := ar) a J.sr J.ipcPlace J.iplaced.e J.ep J.reply + J.sr.place J.j.1.st J.j.1.cv J.bufCover J.bufNative hoff htZ hkt hnb hal hp hk hsdT hks hf hTO hni hsc0 + hsc' hy hscW hrr (fun e'' dd' h => (hsnd' e'' dd' h).2) + · have hcaseF : ∀ e'' dd', xs.tcbState = .blockedOnSend e'' dd' → + (xs.tcbFault = none ∧ (dd'.senderCanGrant = false ∨ ipcBufOfA false xs = none ∨ + unat (dataToMessageInfo (xs.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0)) ∨ + ∃ f, xs.tcbFault = some f ∧ Endp.FaultOKA (L := L) s sd (ipcBufOfA true xt) xs f := + fun e'' dd' h => by + rcases hsndF xs xt hks hkc with g | g | g + · exact Or.inl ⟨g.1, g.2 e'' dd' h⟩ + · exact Or.inr g + · exact absurd g htm + exact receive_from_sender_pushNoDon_fpA (st := st) (ar := ar) a J.sr J.ipcPlace J.iplaced.e J.ep J.reply + J.sr.place J.j.1.st J.j.1.cv J.bufCover J.bufNative hoff htZ hkt hnb (htsc fun h => htm (by + cases hf : xs.tcbFault with + | none => rw [hf] at h; cases h + | some f => + cases f with + | timeout b => exact ⟨b, rfl⟩ + | _ => rw [hf] at h; simp [isTimeoutFault] at h)) + hp hk hsdT hks hssc hu + hcaseF hrr hsnd' (fun u hu => J.swBlocked hks (by rw [hsst]; rfl) u hu) hsdi + obtain ⟨⟨s2, a2⟩, eb, ha2, σ2, heq, E2, ⟨Q, Rl, sr2⟩, reply2, wn⟩ := exists_of_specA push + simp only at ha2 E2 sr2 reply2 wn + subst ha2 + have hmem : ((), σ2) ∈ (specRecvSendA (specPtr t) (specPtr p) (some (specPtr rr)) (specPtr sd :: q) σ).1 := by + rw [heq]; exact Set.mem_singleton _ + -- FL4: `receive_ipc`, `handle_recv` + have hdisp : rcvDispA st ar s a2 t p blk rr ⦃ q' => q' = (s2, a2) ⦄ := by + unfold rcvDispA + step with obj.Ep.state.spec as ⟨st0, hst0⟩ + rw [epState_eq, hst1] at hst0 + subst hst0 + rw [if_neg (by rw [ep_state_vals.1]; decide), if_neg (by rw [ep_state_vals.2.2]; decide)] + rw [eb] + exact (WP.spec_ok _).2 rfl + have hipc : endpoint.receive_ipc st ar s a2 t (.Endpoint p badge send true grant grantReply) blk (.Reply rr rg) = + ok (s2, a2) := by + have h : endpoint.receive_ipc st ar s a2 t (.Endpoint p badge send true grant grantReply) blk (.Reply rr rg) ⦃ q => + q = (s2, a2) ⦄ := by + rw [receive_ipc_eqA] + refine WP.spec_bind (take_reply_plainA (R := Rp) (rc' := .replyCap (specPtr rr) (replyRightsA rg)) + (Or.inr ⟨rg, _, rfl, rfl, hrr⟩ : ReplyCapA Rp (.Reply rr rg) (.replyCap (specPtr rr) (replyRightsA rg)) rr) + (fun _ _ => Or.inl hfree)) ?_ + rintro q rfl + exact rcv_body_dispFlA J.ntfn J.ep htZ hkt hbnd hdisp + obtain ⟨q', hq', e⟩ := spec_existsA h + rw [hq', e] + have hrecv : ((), σ2) ∈ (receiveIpc (Z := Unit) (specPtr t) + (absCapA (.Endpoint p badge send true grant grantReply)) blk (.replyCap (specPtr rr) (replyRightsA rg)) σ).1 := by + rw [show absCapA (.Endpoint p badge send true grant grantReply) = + .endpointCap (specPtr p) badge.bv (rightsOfA send true grant grantReply) from rfl, + receiveIpc_bodyA _ _ blk (Or.inr ⟨specPtr rr, _, xr, rfl, rfl, hkr, Or.inl hxr0⟩ : + SpecReplyPlainA σ (specPtr t) (.replyCap (specPtr rr) (replyRightsA rg)) (some (specPtr rr))), + bndOK_specA hkt hk J.ntfn hbnd (some (specPtr rr))] + exact hmem + -- P1's walk + obtain ⟨Pt, Ub, Pp7, Tb7, Hr, Te, Kt, Lv7, Re, bits, KLv, W⟩ := J7A.unp J.j7 + have I := J.delIn hA hPl W + have h : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s := by + obtain ⟨q, b, x, sg, sc⟩ := J.sys.ent.bxi + exact ⟨q, b, x, sc, sg⟩ + have hext : ∀ z ∈ (Aeneas.Std.Array.repeat 3#usize 0#u64).val, z ≠ 0#u64 → z ∈ OS := fun z hz hne => by + exact absurd (by simpa [Aeneas.Std.Array.repeat] using hz) hne + have D := J.decPreA (extra := Aeneas.Std.Array.repeat 3#usize 0#u64) hslot hext + have Pz := J.toJStage3A.sysPlace hext hslot hcov + have hH : H p = some .ep := J.utIpc.ep p hp + have regs : RpPost st H Z.Cs OS Z.V ia ie Ac s s2 := by + have walk := Fl4ProofsA64.P1.W7.R.ST.receive_ipc.rpw (st := st) (ar := ar) hA a2 t + (.Endpoint p badge send true grant grantReply) blk (.Reply rr rg) p I J.core0 J.ko Pz (SW.start J.sys.sysl J.symL h) + D.cb + (by rcases J.runW with e | e | e <;> rw [e] <;> + simp [sched.TS_RUNNING, sched.TS_RESTART, sched.TS_RUNNING_VM, sched.TS_INACTIVE]) + rfl hH + (fun k hk' => by + rw [hH] at hk' + cases hk' + exact ⟨slot, hslot, by rw [hsc]; exact names_ep⟩) + (fun _ _ c => by cases c; exact ⟨J.utIpc.reply rr hrr, rslot, hrslot, rg, hrsc⟩) + (J.tcbs ▸ J.runVcpu) hnvm + exact (post_of_okA (P := fun q => RpW st H Z.Cs OS Z.V ia ie Ac s q.1) walk hipc).post + obtain ⟨bxi, sysl, symL, userOK, j7⟩ := J.toJStage3A.finRpA hA hPl rfl regs σ2 + -- the spec outcome's facts + set M : ObjRef → Prop := fun o => o = specPtr t ∨ o = specPtr sd with hMdef + set Nq : ObjRef → Prop := fun o => o = specPtr sd ∧ dd.senderIsCall = false ∧ xs.tcbFault = none with hNdef + have K : SIKA σ.idleThread M Nq false σ σ2 := + recvSend_run_sikA (M := M) (N := Nq) J.sr.place s hSt hV hthT hk hkt hks hal (some (specPtr rr)) hsst + (fun _ _ _ h => htsc h) hX (Or.inl rfl) (Or.inr rfl) (fun h h' => ⟨rfl, h, h'⟩) _ hmem + have Qr : QBlkRA (specPtr '' E) (specPtr '' N) σ σ2 ∧ QStepA (some (specPtr rr)) none σ σ2 := + recvSend_run_qbRA (i := σ.idleThread) (M := M) (N := Nq) J.sr.place s hSt hV hthT hk hkt hks hal + (some (specPtr rr)) hsst (fun _ _ _ h => htsc h) hX (Or.inl rfl) (Or.inr rfl) (fun h h' => ⟨rfl, h, h'⟩) J.ipcSorted _ hmem + obtain ⟨U2, -, run''⟩ := recvSend_run_frA J.sr.place s hSt hV hthT hkt hks hal (some (specPtr rr)) hsst + (fun _ _ _ h => htsc h) hX _ hmem + have run' : ∀ z, σ2.kheap (specPtr sd) = some (.tcb z) → z.tcbState = .blockedOnReply (specPtr rr) := + (run'' hcall).1 ⟨hgr, Option.some_ne_none _⟩ _ rfl + have hBt : BoundActive (specPtr t) σ := + J.boundActive + have sv : SchedValidA σ2 := + recvSend_run_schedValidA J.sr.place s hSt hV hthT hk hkt hks hal (some (specPtr rr)) hsst (fun _ _ _ h => htsc h) + (fun _ _ _ => Option.ne_none_iff_exists'.1 hssc) hBt hts hX J.schedValid J.toJStage3A.rqDomPrioA _ hmem + obtain ⟨sw2, lmt2⟩ := recvSend_run_swA J.sr.place s hSt hV hthT hk hkt hks hal (some (specPtr rr)) hsst + (fun _ _ _ h => htsc h) hX J.schedValid J.toJStage3A.rqDomPrioA J.body.sw (fun u hu => ⟨J.swCur u hu, J.swBlocked hks (by rw [hsst]; rfl) u hu⟩) _ hmem + have C2 : CscKA σ σ2 := recvSend_run_cscA J.sr.place s hSt hV hthT hkt hks hal (some (specPtr rr)) hsst hX _ hmem + have hrt : ∀ r' ∈ Rp, DeadA st OS .reply s r' → ∀ y, σ2.kheap (specPtr r') = some (.reply y) → y.replyTcb = none := by + intro r' hr' hd y hy + obtain ⟨x, hx, keep⟩ := Qr.2.rt (specPtr r') y hy + refine keep (fun h => ?_) (J.deadExt.replyTcb r' hr' hd x hx) + have e : r' = rr := specPtr_inj (Option.some.inj h) + exact hd rslot hrslot (e ▸ ⟨rg, hrsc⟩) + -- the receiver's state word is kept + have hst2 : L.mem s2 (stA t) = L.mem s (stA t) := by + have hta := J.sr.core.tcbs t htZ + refine wn.mem_eq (a_st hta).1 ?_ + have hv := Endp.val_st hta + have apart : ∀ u ∈ Z.T, u ≠ t → u.val + 4096 ≤ t.val ∨ t.val + 4096 ≤ u.val := fun u hu ne => by + have := J.sr.place.apart u t .tcb .tcb ((J.sr.thT u).2 hu) ((J.sr.thT t).2 htZ) ne + have hsz : ObjKindA.size .tcb = 4096 := rfl + omega + rintro ((c | c | c) | ⟨q', hq', c1, c2⟩ | c) + rotate_left 3 + · have := J.iplaced.e.rt q' hq' t htZ + omega + · have h' := congrArg (fun w : U64 => w.val) c + simp only [hv, val_reply (J.iplaced.e.tcb sd hsdT)] at h' + have := apart sd hsdT (fun e => hts (by rw [e])) + omega + · rcases c with c | ⟨u, hu, -, c⟩ | ⟨r', c⟩ | c + · have := J.iplaced.e.et p hp t htZ; unfold Endp.EW at c; omega + · have := (J.iplaced.e.tcb u hu).fits + unfold TL at c + by_cases e : u = t + · subst e; omega + · have := apart u hu e; omega + · rw [Endp.regA_val (J.iplaced.e.tcb t htZ) (regIdxA_lt r')] at c + have := regIdxA_lt r' + omega + · have := J.iplaced.e.bt _ c t htZ; omega + · have h' := congrArg (fun w : U64 => w.val) c + simp only [hv, Endp.val_st (J.iplaced.e.tcb sd hsdT)] at h' + exact hts (congrArg specPtr (UScalar.eq_of_val_eq (by omega))) + · rw [stA_eq] at c + exact J.toJStage3A.tcbWord_offMwrA htZ (by simp) (by simp) c + have hxst : ∀ x, σ2.kheap (specPtr t) = some (.tcb x) → x.tcbState = .running ∨ x.tcbState = .restart := by + intro x hx + obtain ⟨y, hy, r2⟩ := E2.tcb t htZ + rw [hx] at hy + cases hy + have h2 : tsCode x.tcbState = tsCode xt.tcbState := by rw [← r2.st, hst2, r.st] + rcases hrunS with e | e <;> rw [e] at h2 <;> + (rcases hs : x.tcbState with _ | _ | _ | _ | _ | _ | _ | _ | ⟨⟨⟩⟩ <;> rw [hs] at h2 <;> + simp [tsCode] at h2 ⊢) + have after : ∀ u ∈ Z.T, M (specPtr u) → ∀ x, σ2.kheap (specPtr u) = some (.tcb x) → + epBlocked x.tcbState = none ∧ ∀ p, x.tcbState ≠ .blockedOnNotification p := by + intro u _ hu x hx + rcases hu with h | h + · rw [specPtr_inj h] at hx + rcases hxst x hx with e | e <;> rw [e] <;> exact ⟨rfl, fun _ h => by cases h⟩ + · rw [h] at hx + · rw [run' x hx]; exact ⟨rfl, fun _ h => by cases h⟩ + have wk : NtfnWaitKeepA Z.T N (L.mem s) (L.mem s2) := J.toJStage3A.ntfnWait_recvPushA htZ hp hsdT hsb wn + have hsub : ∀ b, RecvPushFpA st Z m Rp Bf σ s t p sd b → + EWrite Z.T E Rp Bf b ∨ MWrA (st := st) Z m σ s b ∨ RepWA Rp b := + fun b h => recvPushFp_subA J.iplaced.e htZ hp hsdT h + have wI : WritesIn st s s2 (fun b => IpcFpA st Z E Rp Bf m σ s b ∨ RepWA Rp b) := wn.mono fun b h => by + rcases hsub b h with h | h | h + · exact Or.inl (Or.inl h) + · exact Or.inl (Or.inr h) + · exact Or.inr h + have wN : WritesIn st s s2 (fun b => SchedStepNWA (st := st) N E Rp Z m σ s b ∨ BufW Bf b) := wn.mono fun b h => by + rcases hsub b h with h | h | h + · exact ewrite_stepNA_buf J.sr.core.tcbs h + · exact Or.inl (Or.inl (Or.inl h)) + · exact Or.inl (Or.inr (Or.inr (Or.inr h))) + have notM : ∀ c < n, ¬ M (m.idleThreadOf c) := by + intro c hc' h + obtain ⟨y, hy, hs⟩ := J.sidle.f3 c hc' + rcases h with h | h + · rw [h, hkt] at hy; cases hy; rcases hrunS with e | e <;> rw [e] at hs <;> cases hs + · rw [h, hks] at hy; cases hy; rw [hsst] at hs; cases hs + have sinv : SInvXA σ2 m n := by + refine K.sinvSw J.sinv J.sidle J.body.sw J.noSelf (fun u hu h => h.elim (J.swCur u hu) (J.swBlocked hks (by rw [hsst]; rfl) u hu)) notM (fun h => h.elim (fun h => hidle h.symm) fun h => hsdi h.symm) ?_ ?_ ?_ + (fun y hy hr => by + rcases hy with rfl | rfl + · exact J.notRel (by rw [ct]; exact hr) + · obtain ⟨x, hx, hrn⟩ := J.sinv.rlsRun _ hr; rw [hks] at hx; cases hx; rw [hsst] at hrn; cases hrn) + (J.sidle.rdyA J.sinv) + (fun q hq x y hx hy hf => by + rcases hq with rfl | rfl + · obtain ⟨x0, hx0, g⟩ := K.tcbB hy + rw [hkt] at hx0; cases hx0 + exact absurd (g.fault.trans (FaultStOkA.none J.sinv.faultSt hkt (by + rcases hrunS with e | e <;> rw [e] <;> rintro (⟨_, _, h⟩ | ⟨_, h⟩ | h) <;> cases h))) hf + · rw [run' y hy]; exact Or.inr (Or.inl ⟨_, rfl⟩)) + · intro y hy dm pr hm + rcases hy with rfl | rfl + · exact J.noQ dm pr (by rw [ct]; exact hm) + · obtain ⟨x, hx, hr⟩ := J.sinv.rq dm pr _ hm + rw [hks] at hx; cases hx; rw [hsst] at hr; cases hr + · intro y hy + obtain ⟨-, hcl, hfn⟩ := hy + rcases hcall with h | h + · rw [h] at hcl; cases hcl + · exact absurd hfn h + · intro y hy + obtain ⟨rfl, -⟩ := hy + exact ⟨xs, hks⟩ + have hMv : ∀ u ∈ Z.T, M (specPtr u) → L.mem s (stA u) ≠ sched.TS_RUNNING_VM := by + intro u _ hu h + rcases hu with e | e <;> rw [specPtr_inj e] at h + · exact hnvm h + · rw [h] at hs4; revert hs4; unfold sched.TS_RUNNING_VM; decide + have ntfn := J.toJStage3A.ntfn_ipcA wI K E2 wk (fun u hu hm x hx p' h => by + rcases hm with hm | hm + · rw [hm, hkt] at hx; cases hx; rcases hrunS with e | e <;> rw [e] at h <;> cases h + · rw [hm, hks] at hx; cases hx; rw [hsst] at h; cases h) (fun u hu hm x hx => (after u hu hm x hx).2) + have J2 := J.toJStage3A.of_ipcStepURSA (Q := Q) (Rl := Rl) (curOf := Z.curOf) (curScOf := Z.curScOf) wN U2.1 U2.2.1 + Qr.1 hrt (J.toJStage3A.specZero_of_recvBufA htZ hkt U2.2.2) (fun _ _ => rfl) bxi sysl symL userOK j7 sr2 E2 ntfn + reply2 J.deadExt.reply K.ipcKeep (J.toJStage3A.tw_ipcA wI) sinv + (K.sidle J.sidle notM (J.sidle.rdyA J.sinv)) (J.toJStage3A.runVcpu_ipcA wI K E2 hMv) (J.toJStage3A.blkQ_ipcA K E2 wk after) sv + have hcur : σ2.curThread = σ.curThread := by rw [K.frame] + refine ⟨s2, σ2, hipc, hrecv, by rw [ct]; exact hmem, A64RelS3.ofJ J2 ?bo, curW_keptA J.toJStage3A J2 hcur, ?bo⟩ + refine J.body.step (σ' := σ2) sw2 (by rw [K.frame]) lmt2 hcur (by rw [K.frame]) (by rw [K.frame]) (fun _ _ _ _ _ h => K.rls _ h) + (fun y' k hz => ?_) (fun _ z hz => ?_) + · obtain ⟨y, hy, e1, e2⟩ := C2.sc y' k hz + refine ⟨y, hy, e1, e2, Or.inl ?_⟩ + have o := K.obj σ.curSc + rw [hy, hz] at o + cases o with + | same => rfl + | sc _ _ _ g => exact g.th.resolve_right (fun c => by cases c.1) + · rw [ct] at hz + exact hxst z hz + +set_option maxHeartbeats 4000000 in +theorem RecvPreA.recvPushA + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p badge slot rr rslot : U64} {send grant grantReply blk rg : Bool} + (hcap : cspace.lookup_cap st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.CAP)) = + ok (.Ok (.Endpoint p badge send true grant grantReply))) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) + (hsc : capA st s slot = .Endpoint p badge send true grant grantReply) + (hcapR : cspace.lookup_cap st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.REPLY)) = ok (.Ok (.Reply rr rg))) + (hrslot : rslot ∈ OS) (hrsc : capA st s rslot = .Reply rr rg) (hrr : rr ∈ Rp) + (hfree : L.mem s (Endp.rTcbA rr) = 0#u64) + (hp : p ∈ E) {sd : U64} {q : List ObjRef} + (hk : σ.kheap (specPtr p) = some (.endpoint (.sendEP (specPtr sd :: q)))) + (hbn : ∀ y, σ.kheap σ.curThread = some (.tcb y) → BndOKA N (L.mem s) σ (L.curW s) y blk) + (hsnd : ∀ y xt, σ.kheap (specPtr sd) = some (.tcb y) → σ.kheap σ.curThread = some (.tcb xt) → + y.tcbSchedContext ≠ none ∧ ∀ e' dd, + y.tcbState = .blockedOnSend e' dd → (dd.senderIsCall = true ∨ y.tcbFault ≠ none) ∧ + (dd.senderCanGrant = true ∨ dd.senderCanGrantReply = true)) + (hsndF : ∀ y xt, σ.kheap (specPtr sd) = some (.tcb y) → σ.kheap σ.curThread = some (.tcb xt) → + (y.tcbFault = none ∧ ∀ e' dd, y.tcbState = .blockedOnSend e' dd → dd.senderCanGrant = false ∨ + ipcBufOfA false y = none ∨ + unat (dataToMessageInfo (y.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0) ∨ + (∃ f, y.tcbFault = some f ∧ Endp.FaultOKA (L := L) s sd (ipcBufOfA true xt) y f) ∨ + ∃ b, y.tcbFault = some (.timeout b)) + (htsc0 : ∀ x y, σ.kheap σ.curThread = some (.tcb x) → σ.kheap (specPtr sd) = some (.tcb y) → + ¬ (y.tcbFault.elim false isTimeoutFault = true) → x.tcbSchedContext ≠ none) : + HRecvPostA st AR ar hPl.ipa n Z.Cs s a m blk true σ + (fun σ' => ((), σ') ∈ (specRecvSendA σ.curThread (specPtr p) (some (specPtr rr)) (specPtr sd :: q) σ).1) := RecvPreA.recvFrontA J hcap hcapR + (RecvPreA.recvPushA_core hA hPl J hCs h1 hmb hmax htu hslot hcov hsc hrslot hrsc hrr hfree hp hk hbn hsnd hsndF htsc0) + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3RecvPushCancelA.lean b/proofs/Fl4ProofsA64/P3/Stage3RecvPushCancelA.lean new file mode 100644 index 000000000..6cf8564c7 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3RecvPushCancelA.lean @@ -0,0 +1,388 @@ +/- +P3 (aarch64, A64-P3 Stage 3, e5b): **the push arms (`recvPushA`, `recvPushDonA`) with the reply object possibly still linked +to a receive-blocked thread** (the reply may be linked to any thread). + +After the cancel of `take_reply` (case (A) or (B), `CancelXA`: the cancelled thread is the only TCB that changes, the +sender queues and the notification objects are kept, the TCB / notification words are kept off the footprint): +* `Endp.FaultOKA.xport`, `BndOKA.xport`: the selectors at the new memory and heap. +* `RecvPreA.recvPushA_repA`, `RecvPreA.recvPushDonA_repA`. +* `JSyscallA.recvBlockRAnyA_repA`, `JSyscallA.recvPushA_repA`, `JSyscallA.recvPushDonA_repA`: the syscall-entry forms. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3RecvCancelA +import Fl4ProofsA64.P3.Stage3RecvPushA +import Fl4ProofsA64.P3.Stage3RecvPushDonA +import Fl4ProofsA64.P3.Stage3CancelFootOffA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 Fl4ProofsA64.P3.Endp +open Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Notif + +local notation "wa" => core.num.U64.wrapping_add + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- `FaultOKA` at another memory, from the two words it reads. -/ +theorem Endp.FaultOKA.xport {s s1 : S} {snd : U64} {rbo : Option Fl4SpecA64.ObjRef} {x : Fl4SpecA64.Tcb} + {f : Fl4SpecA64.Fault} (e1 : L.mem s1 (tTflA snd) = L.mem s (tTflA snd)) + (e2 : L.mem s1 (scA snd) = L.mem s (scA snd)) (h : Endp.FaultOKA (L := L) s snd rbo x f) : + Endp.FaultOKA (L := L) s1 snd rbo x f := by + cases f with + | timeout _ => exact ⟨h.1, e2.trans h.2⟩ + | unknownSyscallException _ => exact h.imp_right (fun h' => by rw [e1]; exact h') + | archFault af => + cases af with + | vgicMaintenance idx => cases idx <;> exact h + | _ => exact h + | _ => exact h + +/-- `BndOKA` at another memory and heap, from the notification words and the current thread's `scA` word. -/ +theorem BndOKA.xport {N : Set U64} {m m1 : U64 → U64} {σ σ1 : Fl4SpecA64.State Unit} {t : U64} + {xt : Fl4SpecA64.Tcb} {blk : Bool} (h : BndOKA N m σ t xt blk) + (hn : ∀ n ∈ N, σ1.kheap (specPtr n) = σ.kheap (specPtr n)) (hw : ∀ n ∈ N, m1 (nScA n) = m (nScA n)) + (ht : m1 (scA t) = m (scA t)) : BndOKA N m1 σ1 t xt blk := by + rcases h with h | ⟨n, hn', hb, hx⟩ + · exact Or.inl h + · refine Or.inr ⟨n, hn', hb, fun xn hxn => ?_⟩ + rw [hn n hn'] at hxn + obtain ⟨c1, c2⟩ := hx xn hxn + refine ⟨c1, fun hb' => ?_⟩ + obtain ⟨b1, b2⟩ := c2 hb' + exact ⟨b1, by rw [hw n hn', ht]; exact b2⟩ + +variable [J7PpA st (J7A st (J7R2A st ar nA))] + +set_option maxHeartbeats 8000000 in +/-- **`recvPushA` with the reply possibly still linked to a receive-blocked thread** (the reply may be linked to any thread). -/ +theorem RecvPreA.recvPushA_repA + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p badge slot rr rslot : U64} {send grant grantReply blk rg : Bool} + (hcap : cspace.lookup_cap st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.CAP)) = + ok (.Ok (.Endpoint p badge send true grant grantReply))) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) + (hsc : capA st s slot = .Endpoint p badge send true grant grantReply) + (hcapR : cspace.lookup_cap st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.REPLY)) = ok (.Ok (.Reply rr rg))) + (hrslot : rslot ∈ OS) (hrsc : capA st s rslot = .Reply rr rg) (hrr : rr ∈ Rp) + (hp : p ∈ E) {sd : U64} {q : List ObjRef} + (hk : σ.kheap (specPtr p) = some (.endpoint (.sendEP (specPtr sd :: q)))) + (hbn : ∀ y, σ.kheap σ.curThread = some (.tcb y) → BndOKA N (L.mem s) σ (L.curW s) y blk) + (hsnd : ∀ y xt, σ.kheap (specPtr sd) = some (.tcb y) → σ.kheap σ.curThread = some (.tcb xt) → + y.tcbSchedContext ≠ none ∧ ∀ e' dd, + y.tcbState = .blockedOnSend e' dd → (dd.senderIsCall = true ∨ y.tcbFault ≠ none) ∧ + (dd.senderCanGrant = true ∨ dd.senderCanGrantReply = true)) + (hsndF : ∀ y xt, σ.kheap (specPtr sd) = some (.tcb y) → σ.kheap σ.curThread = some (.tcb xt) → + (y.tcbFault = none ∧ ∀ e' dd, y.tcbState = .blockedOnSend e' dd → dd.senderCanGrant = false ∨ + ipcBufOfA false y = none ∨ + unat (dataToMessageInfo (y.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0) ∨ + (∃ f, y.tcbFault = some f ∧ Endp.FaultOKA (L := L) s sd (ipcBufOfA true xt) y f) ∨ + ∃ b, y.tcbFault = some (.timeout b)) + (htsc0 : ∀ x y, σ.kheap σ.curThread = some (.tcb x) → σ.kheap (specPtr sd) = some (.tcb y) → + ¬ (y.tcbFault.elim false isTimeoutFault = true) → x.tcbSchedContext ≠ none) : + HRecvPostA st AR ar hPl.ipa n Z.Cs s a m blk true σ + (fun σ' => ((), σ') ∈ (specRecvSendA σ.curThread (specPtr p) (some (specPtr rr)) (specPtr sd :: q) σ).1 ∨ + ∃ σ1, ∃ i : U64, cancelIpc (Z := Unit) (specPtr i) σ = ({((), σ1)}, False) ∧ + ((), σ') ∈ (specRecvSendA σ1.curThread (specPtr p) (some (specPtr rr)) (specPtr sd :: q) σ1).1) := by + by_cases h0 : L.mem s (Endp.rTcbA rr) = 0#u64 + · exact (J.recvPushA hA hPl hCs h1 hmb hmax htu hcap hslot hcov hsc hcapR hrslot hrsc hrr h0 hp hk hbn hsnd hsndF htsc0).monoA (fun _ h => Or.inl h) + · have ct : σ.curThread = specPtr (L.curW s) := J.j.1.sch.curThread + have hcT : L.curW s ∈ Z.T := J.sr.curT + obtain ⟨xc, hk0, -⟩ := J.ep.tcb (L.curW s) hcT + have hkc : σ.kheap σ.curThread = some (.tcb xc) := by rw [ct]; exact hk0 + have core := RecvPreA.cancelCoreXA J hA hPl rfl hCs hmb hmax htu (p := p) (badge := badge) (send := send) + (grant := grant) (grantReply := grantReply) (blk := blk) (rg := rg) hrr h0 + (Out1 := fun σ1 σ' => ((), σ') ∈ (specRecvSendA σ1.curThread (specPtr p) (some (specPtr rr)) (specPtr sd :: q) σ1).1) + (fun i hi hi0 hit hiT hi4 s1 σ1 stk Q Rl J1 hcw hcan X => ?_) + · refine (RecvPreA.recvFrontA J hcap hcapR core).monoA (fun σ' h => Or.inr ?_) + obtain ⟨i, σ1, hcan, hh⟩ := h + exact ⟨σ1, i, hcan, hh⟩ + · obtain ⟨-, xs0, -, hq0, hQ, hB⟩ := J.ep.at_ep hp hk + have hsdx : sd ∈ xs0 := by + have : specPtr sd ∈ xs0.map specPtr := by rw [← hq0]; exact List.mem_cons_self + obtain ⟨y, hy, e⟩ := List.mem_map.1 this + rwa [specPtr_inj e] at hy + have hsdT : sd ∈ Z.T := hQ.mem sd hsdx + have hne_sd : specPtr sd ≠ specPtr i := fun e => hi4 (by rw [← specPtr_inj e]; exact (hB sd hsdx).1) + obtain ⟨xs, hks, -⟩ := J.ep.tcb sd hsdT + have hk1 : σ1.kheap (specPtr p) = σ.kheap (specPtr p) := X.epS p hp _ hk + have hsd1 : σ1.kheap (specPtr sd) = σ.kheap (specPtr sd) := by rw [X.tcb _ xs hne_sd hks, hks] + have hct1 : σ1.curThread = σ.curThread := by + have h := congrArg (fun τ : State Unit => τ.curThread) X.fr + exact h + have hcur1 : σ1.kheap σ.curThread = σ.kheap σ.curThread := by + rw [X.tcb _ xc (fun e => hit (specPtr_inj (ct.symm.trans e)).symm) hkc, hkc] + have tw := X.tw + have hnk := X.ntfn + have hbn1 : ∀ y, σ1.kheap σ1.curThread = some (.tcb y) → + BndOKA N (L.mem s1) σ1 (L.curW s1) y blk := by + intro y hy + rw [hct1, hcur1, hkc] at hy + have hyx : y = xc := KernelObject.tcb.inj (Option.some.inj hy.symm) + rw [hyx, hcw] + exact BndOKA.xport (hbn xc hkc) hnk + X.nsc + (by rw [scA_eq]; exact tw _ hcT 3464#u64 (by simp) (by simp) (by simp) (by simp) (by simp) (by simp)) + have hcov1 := SlotCov.keepA X.osk hcov + have hsc1 : capA st s1 slot = .Endpoint p badge send true grant grantReply := by + rw [X.osk.capA hslot]; exact hsc + have hrsc1 : capA st s1 rslot = .Reply rr rg := by rw [X.osk.capA hrslot]; exact hrsc + have hsnd1 : ∀ y xt, σ1.kheap (specPtr sd) = some (.tcb y) → σ1.kheap σ1.curThread = some (.tcb xt) → + y.tcbSchedContext ≠ none ∧ ∀ e' dd, + y.tcbState = .blockedOnSend e' dd → (dd.senderIsCall = true ∨ y.tcbFault ≠ none) ∧ + (dd.senderCanGrant = true ∨ dd.senderCanGrantReply = true) := by + intro y xt hy hxt + rw [hsd1] at hy + rw [hct1, hcur1] at hxt + exact hsnd y xt hy hxt + have e1 : L.mem s1 (tTflA sd) = L.mem s (tTflA sd) := + tw sd hsdT 3456#u64 (by simp) (by simp) (by simp) (by simp) (by simp) (by simp) + have e2 : L.mem s1 (scA sd) = L.mem s (scA sd) := by + rw [scA_eq] + exact tw sd hsdT 3464#u64 (by simp) (by simp) (by simp) (by simp) (by simp) (by simp) + have hsndF1 : ∀ y xt, σ1.kheap (specPtr sd) = some (.tcb y) → σ1.kheap σ1.curThread = some (.tcb xt) → + (y.tcbFault = none ∧ ∀ e' dd, y.tcbState = .blockedOnSend e' dd → dd.senderCanGrant = false ∨ + ipcBufOfA false y = none ∨ + unat (dataToMessageInfo (y.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0) ∨ + (∃ f, y.tcbFault = some f ∧ Endp.FaultOKA (L := L) s1 sd (ipcBufOfA true xt) y f) ∨ + ∃ b, y.tcbFault = some (.timeout b) := by + intro y xt hy hxt + rw [hsd1] at hy + rw [hct1, hcur1] at hxt + rcases hsndF y xt hy hxt with g | ⟨f, hf, hok⟩ | b + · exact Or.inl g + · exact Or.inr (Or.inl ⟨f, hf, Endp.FaultOKA.xport e1 e2 hok⟩) + · exact Or.inr (Or.inr b) + have htsc01 : ∀ x y, σ1.kheap σ1.curThread = some (.tcb x) → σ1.kheap (specPtr sd) = some (.tcb y) → + ¬ (y.tcbFault.elim false isTimeoutFault = true) → x.tcbSchedContext ≠ none := by + intro x y hx hy hn + rw [hct1, hcur1] at hx + rw [hsd1] at hy + exact htsc0 x y hx hy hn + exact (RecvPreA.recvPushA_core hA hPl J1 hCs h1 hmb hmax htu hslot hcov1 hsc1 hrslot hrsc1 hrr X.unl hp (by rw [hk1]; exact hk) hbn1 hsnd1 hsndF1 htsc01) + +set_option maxHeartbeats 8000000 in +/-- **`recvPushDonA` with the reply possibly still linked to a receive-blocked thread** (the reply may be linked to any thread). -/ +theorem RecvPreA.recvPushDonA_repA + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p badge slot rr rslot : U64} {send grant grantReply blk rg : Bool} + (hcap : cspace.lookup_cap st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.CAP)) = + ok (.Ok (.Endpoint p badge send true grant grantReply))) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) + (hsc : capA st s slot = .Endpoint p badge send true grant grantReply) + (hcapR : cspace.lookup_cap st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.REPLY)) = ok (.Ok (.Reply rr rg))) + (hrslot : rslot ∈ OS) (hrsc : capA st s rslot = .Reply rr rg) (hrr : rr ∈ Rp) + (hp : p ∈ E) {sd : U64} {q : List ObjRef} + (hk : σ.kheap (specPtr p) = some (.endpoint (.sendEP (specPtr sd :: q)))) + (hbn : ∀ y, σ.kheap σ.curThread = some (.tcb y) → BndOKA N (L.mem s) σ (L.curW s) y blk) + (hsnd : ∀ y xt, σ.kheap (specPtr sd) = some (.tcb y) → σ.kheap σ.curThread = some (.tcb xt) → + y.tcbSchedContext ≠ none ∧ ∀ e' dd, + y.tcbState = .blockedOnSend e' dd → (dd.senderIsCall = true ∨ y.tcbFault ≠ none) ∧ + (dd.senderCanGrant = true ∨ dd.senderCanGrantReply = true)) + (hsndF : ∀ y xt, σ.kheap (specPtr sd) = some (.tcb y) → σ.kheap σ.curThread = some (.tcb xt) → + (y.tcbFault = none ∧ ∀ e' dd, y.tcbState = .blockedOnSend e' dd → dd.senderCanGrant = false ∨ + ipcBufOfA false y = none ∨ + unat (dataToMessageInfo (y.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0) ∨ + ∃ f, y.tcbFault = some f ∧ Endp.FaultOKA (L := L) s sd (ipcBufOfA true xt) y f) + (hpas : ∀ y, σ.kheap σ.curThread = some (.tcb y) → y.tcbSchedContext = none) + (hV : L.mem s (core.num.U64.wrapping_add sd 3664#u64) = 0#u64) : + HRecvPostA st AR ar hPl.ipa n Z.Cs s a m blk true σ + (fun σ' => ((), σ') ∈ (specRecvSendA σ.curThread (specPtr p) (some (specPtr rr)) (specPtr sd :: q) σ).1 ∨ + ∃ σ1, ∃ i : U64, cancelIpc (Z := Unit) (specPtr i) σ = ({((), σ1)}, False) ∧ + ((), σ') ∈ (specRecvSendA σ1.curThread (specPtr p) (some (specPtr rr)) (specPtr sd :: q) σ1).1) := by + by_cases h0 : L.mem s (Endp.rTcbA rr) = 0#u64 + · exact (J.recvPushDonA hA hPl hCs h1 hmb hmax htu hcap hslot hcov hsc hcapR hrslot hrsc hrr h0 hp hk hbn hsnd hsndF hpas hV).monoA (fun _ h => Or.inl h) + · have ct : σ.curThread = specPtr (L.curW s) := J.j.1.sch.curThread + have hcT : L.curW s ∈ Z.T := J.sr.curT + obtain ⟨xc, hk0, -⟩ := J.ep.tcb (L.curW s) hcT + have hkc : σ.kheap σ.curThread = some (.tcb xc) := by rw [ct]; exact hk0 + have core := RecvPreA.cancelCoreXA J hA hPl rfl hCs hmb hmax htu (p := p) (badge := badge) (send := send) + (grant := grant) (grantReply := grantReply) (blk := blk) (rg := rg) hrr h0 + (Out1 := fun σ1 σ' => ((), σ') ∈ (specRecvSendA σ1.curThread (specPtr p) (some (specPtr rr)) (specPtr sd :: q) σ1).1) + (fun i hi hi0 hit hiT hi4 s1 σ1 stk Q Rl J1 hcw hcan X => ?_) + · refine (RecvPreA.recvFrontA J hcap hcapR core).monoA (fun σ' h => Or.inr ?_) + obtain ⟨i, σ1, hcan, hh⟩ := h + exact ⟨σ1, i, hcan, hh⟩ + · obtain ⟨-, xs0, -, hq0, hQ, hB⟩ := J.ep.at_ep hp hk + have hsdx : sd ∈ xs0 := by + have : specPtr sd ∈ xs0.map specPtr := by rw [← hq0]; exact List.mem_cons_self + obtain ⟨y, hy, e⟩ := List.mem_map.1 this + rwa [specPtr_inj e] at hy + have hsdT : sd ∈ Z.T := hQ.mem sd hsdx + have hne_sd : specPtr sd ≠ specPtr i := fun e => hi4 (by rw [← specPtr_inj e]; exact (hB sd hsdx).1) + obtain ⟨xs, hks, -⟩ := J.ep.tcb sd hsdT + have hk1 : σ1.kheap (specPtr p) = σ.kheap (specPtr p) := X.epS p hp _ hk + have hsd1 : σ1.kheap (specPtr sd) = σ.kheap (specPtr sd) := by rw [X.tcb _ xs hne_sd hks, hks] + have hct1 : σ1.curThread = σ.curThread := by + have h := congrArg (fun τ : State Unit => τ.curThread) X.fr + exact h + have hcur1 : σ1.kheap σ.curThread = σ.kheap σ.curThread := by + rw [X.tcb _ xc (fun e => hit (specPtr_inj (ct.symm.trans e)).symm) hkc, hkc] + have tw := X.tw + have hnk := X.ntfn + have hbn1 : ∀ y, σ1.kheap σ1.curThread = some (.tcb y) → + BndOKA N (L.mem s1) σ1 (L.curW s1) y blk := by + intro y hy + rw [hct1, hcur1, hkc] at hy + have hyx : y = xc := KernelObject.tcb.inj (Option.some.inj hy.symm) + rw [hyx, hcw] + exact BndOKA.xport (hbn xc hkc) hnk + X.nsc + (by rw [scA_eq]; exact tw _ hcT 3464#u64 (by simp) (by simp) (by simp) (by simp) (by simp) (by simp)) + have hcov1 := SlotCov.keepA X.osk hcov + have hsc1 : capA st s1 slot = .Endpoint p badge send true grant grantReply := by + rw [X.osk.capA hslot]; exact hsc + have hrsc1 : capA st s1 rslot = .Reply rr rg := by rw [X.osk.capA hrslot]; exact hrsc + have hsnd1 : ∀ y xt, σ1.kheap (specPtr sd) = some (.tcb y) → σ1.kheap σ1.curThread = some (.tcb xt) → + y.tcbSchedContext ≠ none ∧ ∀ e' dd, + y.tcbState = .blockedOnSend e' dd → (dd.senderIsCall = true ∨ y.tcbFault ≠ none) ∧ + (dd.senderCanGrant = true ∨ dd.senderCanGrantReply = true) := by + intro y xt hy hxt + rw [hsd1] at hy + rw [hct1, hcur1] at hxt + exact hsnd y xt hy hxt + have e1 : L.mem s1 (tTflA sd) = L.mem s (tTflA sd) := + tw sd hsdT 3456#u64 (by simp) (by simp) (by simp) (by simp) (by simp) (by simp) + have e2 : L.mem s1 (scA sd) = L.mem s (scA sd) := by + rw [scA_eq] + exact tw sd hsdT 3464#u64 (by simp) (by simp) (by simp) (by simp) (by simp) (by simp) + have hsndF1 : ∀ y xt, σ1.kheap (specPtr sd) = some (.tcb y) → σ1.kheap σ1.curThread = some (.tcb xt) → + (y.tcbFault = none ∧ ∀ e' dd, y.tcbState = .blockedOnSend e' dd → dd.senderCanGrant = false ∨ + ipcBufOfA false y = none ∨ + unat (dataToMessageInfo (y.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0) ∨ + ∃ f, y.tcbFault = some f ∧ Endp.FaultOKA (L := L) s1 sd (ipcBufOfA true xt) y f := by + intro y xt hy hxt + rw [hsd1] at hy + rw [hct1, hcur1] at hxt + rcases hsndF y xt hy hxt with g | ⟨f, hf, hok⟩ + · exact Or.inl g + · exact Or.inr ⟨f, hf, Endp.FaultOKA.xport e1 e2 hok⟩ + have hpas1 : ∀ y, σ1.kheap σ1.curThread = some (.tcb y) → y.tcbSchedContext = none := by + intro y hy + rw [hct1, hcur1] at hy + exact hpas y hy + have hV1 : L.mem s1 (core.num.U64.wrapping_add sd 3664#u64) = 0#u64 := by + rw [tw sd hsdT 3664#u64 (by simp) (by simp) (by simp) (by simp) (by simp) (by simp)] + exact hV + exact (RecvPreA.recvPushDonA_core hA hPl J1 hCs h1 hmb hmax htu hslot hcov1 hsc1 hrslot hrsc1 hrr X.unl hp (by rw [hk1]; exact hk) hbn1 hsnd1 hsndF1 hpas1 hV1) + +set_option maxHeartbeats 4000000 in +/-- **`RecvPreA.recvBlockRAnyA_repA` at a syscall entry.** -/ +theorem JSyscallA.recvBlockRAnyA_repA + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p badge slot rr rslot : U64} {send grant grantReply rg : Bool} + (hcap : cspace.lookup_cap st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.CAP)) = + ok (.Ok (.Endpoint p badge send true grant grantReply))) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) + (hsc : capA st s slot = .Endpoint p badge send true grant grantReply) + (hcapR : cspace.lookup_cap st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.REPLY)) = ok (.Ok (.Reply rr rg))) + (hrslot : rslot ∈ OS) (hrsc : capA st s rslot = .Reply rr rg) (hrr : rr ∈ Rp) + (hp : p ∈ E) {x : Endpoint} (hk : σ.kheap (specPtr p) = some (.endpoint x)) (hcb : CanBlockA true x) + (hbn : ∀ y, σ.kheap σ.curThread = some (.tcb y) → BndOKA N (L.mem s) σ (L.curW s) y false) : + HRecvPostA st AR ar hPl.ipa n Z.Cs s a m true true σ + (fun σ' => RAnyOutA p rr x σ σ' ∨ + ∃ σ1, ∃ i : U64, cancelIpc (Z := Unit) (specPtr i) σ = ({((), σ1)}, False) ∧ + ∃ x1, σ1.kheap (specPtr p) = some (.endpoint x1) ∧ RAnyOutA p rr x1 σ1 σ') := + RecvPreA.recvBlockRAnyA_repA hA hPl J.toRecvPreA hCs h1 hmb hmax htu hcap hslot hcov hsc hcapR hrslot hrsc hrr hp + hk hcb hbn + +set_option maxHeartbeats 4000000 in +/-- **`RecvPreA.recvPushA_repA` at a syscall entry.** -/ +theorem JSyscallA.recvPushA_repA + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p badge slot rr rslot : U64} {send grant grantReply blk rg : Bool} + (hcap : cspace.lookup_cap st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.CAP)) = + ok (.Ok (.Endpoint p badge send true grant grantReply))) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) + (hsc : capA st s slot = .Endpoint p badge send true grant grantReply) + (hcapR : cspace.lookup_cap st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.REPLY)) = ok (.Ok (.Reply rr rg))) + (hrslot : rslot ∈ OS) (hrsc : capA st s rslot = .Reply rr rg) (hrr : rr ∈ Rp) + (hp : p ∈ E) {sd : U64} {q : List ObjRef} + (hk : σ.kheap (specPtr p) = some (.endpoint (.sendEP (specPtr sd :: q)))) + (hbn : ∀ y, σ.kheap σ.curThread = some (.tcb y) → BndOKA N (L.mem s) σ (L.curW s) y blk) + (hsnd : ∀ y xt, σ.kheap (specPtr sd) = some (.tcb y) → σ.kheap σ.curThread = some (.tcb xt) → + y.tcbSchedContext ≠ none ∧ ∀ e' dd, + y.tcbState = .blockedOnSend e' dd → (dd.senderIsCall = true ∨ y.tcbFault ≠ none) ∧ + (dd.senderCanGrant = true ∨ dd.senderCanGrantReply = true)) + (hsndF : ∀ y xt, σ.kheap (specPtr sd) = some (.tcb y) → σ.kheap σ.curThread = some (.tcb xt) → + (y.tcbFault = none ∧ ∀ e' dd, y.tcbState = .blockedOnSend e' dd → dd.senderCanGrant = false ∨ + ipcBufOfA false y = none ∨ + unat (dataToMessageInfo (y.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0) ∨ + (∃ f, y.tcbFault = some f ∧ Endp.FaultOKA (L := L) s sd (ipcBufOfA true xt) y f) ∨ + ∃ b, y.tcbFault = some (.timeout b)) : + HRecvPostA st AR ar hPl.ipa n Z.Cs s a m blk true σ + (fun σ' => ((), σ') ∈ (specRecvSendA σ.curThread (specPtr p) (some (specPtr rr)) (specPtr sd :: q) σ).1 ∨ + ∃ σ1, ∃ i : U64, cancelIpc (Z := Unit) (specPtr i) σ = ({((), σ1)}, False) ∧ + ((), σ') ∈ (specRecvSendA σ1.curThread (specPtr p) (some (specPtr rr)) (specPtr sd :: q) σ1).1) := + RecvPreA.recvPushA_repA hA hPl J.toRecvPreA hCs h1 hmb hmax htu hcap hslot hcov hsc hcapR hrslot hrsc hrr hp + hk hbn hsnd hsndF + (fun x _ hx _ _ => by obtain ⟨y, hy, e, -⟩ := J.curInv.tcb; rw [hx] at hy; cases hy; rw [e]; exact Option.some_ne_none _) + +set_option maxHeartbeats 4000000 in +/-- **`RecvPreA.recvPushDonA_repA` at a syscall entry.** -/ +theorem JSyscallA.recvPushDonA_repA + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p badge slot rr rslot : U64} {send grant grantReply blk rg : Bool} + (hcap : cspace.lookup_cap st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.CAP)) = + ok (.Ok (.Endpoint p badge send true grant grantReply))) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) + (hsc : capA st s slot = .Endpoint p badge send true grant grantReply) + (hcapR : cspace.lookup_cap st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.REPLY)) = ok (.Ok (.Reply rr rg))) + (hrslot : rslot ∈ OS) (hrsc : capA st s rslot = .Reply rr rg) (hrr : rr ∈ Rp) + (hp : p ∈ E) {sd : U64} {q : List ObjRef} + (hk : σ.kheap (specPtr p) = some (.endpoint (.sendEP (specPtr sd :: q)))) + (hbn : ∀ y, σ.kheap σ.curThread = some (.tcb y) → BndOKA N (L.mem s) σ (L.curW s) y blk) + (hsnd : ∀ y xt, σ.kheap (specPtr sd) = some (.tcb y) → σ.kheap σ.curThread = some (.tcb xt) → + y.tcbSchedContext ≠ none ∧ ∀ e' dd, + y.tcbState = .blockedOnSend e' dd → (dd.senderIsCall = true ∨ y.tcbFault ≠ none) ∧ + (dd.senderCanGrant = true ∨ dd.senderCanGrantReply = true)) + (hsndF : ∀ y xt, σ.kheap (specPtr sd) = some (.tcb y) → σ.kheap σ.curThread = some (.tcb xt) → + (y.tcbFault = none ∧ ∀ e' dd, y.tcbState = .blockedOnSend e' dd → dd.senderCanGrant = false ∨ + ipcBufOfA false y = none ∨ + unat (dataToMessageInfo (y.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0) ∨ + ∃ f, y.tcbFault = some f ∧ Endp.FaultOKA (L := L) s sd (ipcBufOfA true xt) y f) + (hpas : ∀ y, σ.kheap σ.curThread = some (.tcb y) → y.tcbSchedContext = none) + (hV : L.mem s (core.num.U64.wrapping_add sd 3664#u64) = 0#u64) : + HRecvPostA st AR ar hPl.ipa n Z.Cs s a m blk true σ + (fun σ' => ((), σ') ∈ (specRecvSendA σ.curThread (specPtr p) (some (specPtr rr)) (specPtr sd :: q) σ).1 ∨ + ∃ σ1, ∃ i : U64, cancelIpc (Z := Unit) (specPtr i) σ = ({((), σ1)}, False) ∧ + ((), σ') ∈ (specRecvSendA σ1.curThread (specPtr p) (some (specPtr rr)) (specPtr sd :: q) σ1).1) := + RecvPreA.recvPushDonA_repA hA hPl J.toRecvPreA hCs h1 hmb hmax htu hcap hslot hcov hsc hcapR hrslot hrsc hrr hp + hk hbn hsnd hsndF hpas hV + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3RecvPushDonA.lean b/proofs/Fl4ProofsA64/P3/Stage3RecvPushDonA.lean new file mode 100644 index 000000000..ae05b5bf6 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3RecvPushDonA.lean @@ -0,0 +1,574 @@ +/- +P3 (aarch64, A64-P3 Stage 3): **`Recv` by a passive receiver with a call or faulted sender waiting and a reply object: +the donating reply push** (`recvPushA`'s arm with a receiver holding no SC): the reply goes on the sender SC's stack, +the receiver (the current thread) gets that SC, the sender becomes `BlockedOnReply`. + +* `recvSendDon_run_holderA`: only the donated SC changes its thread, and the receiver is bound to it. +* **`RecvPreA.recvPushDonA`**: `HRecvPostA`. + +[S] the endpoint holds waiting senders; its head sender holds an SC, is a call or faulted sender with grant or +grant-reply (`hsnd`), has no fault and transfers no extra caps, or has a non-timeout fault in `FaultOKA` (`hsndF`); the +receiver holds no SC (`hpas`); donating push arm covers VCPU-less senders; VCPU-bound sender is a separate scope case +(same as sendDonPlainA) (`hV`); the REPLY register names a reply object `rr` linked to no thread; `can_reply = true`; +no bound notification, or one that is not active and (when blocking) whose SC is not the receiver's (`BndOKA`); +`numNodes = 1`. [P] `ArchTotal`, `ArchPlat`, `hCs`, `hmb`, `hmax`, `htu`. New module. +-/ +import Fl4ProofsA64.P3.Stage3RecvFrontA +import Fl4ProofsA64.P3.Stage3RecvPushA +import Fl4ProofsA64.P3.Stage3RecvPushDonFpA +import Fl4ProofsA64.P3.Stage3RecvSendDonA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P3 Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Classical +open fl4_core_a64 Fl4ProofsA64.P1 + +noncomputable section + +/-- **A donating `reply_push` changes the thread of the donated SC only**, and binds the callee to it. -/ +theorem replyPushDon_holderA {σ : State Unit} {t d r sc : ObjRef} {x y : Tcb} + (hk : σ.kheap t = some (.tcb x)) (hsc : x.tcbSchedContext = some sc) (hkd : σ.kheap d = some (.tcb y)) + (hd0 : y.tcbSchedContext = none) : ∀ z ∈ (replyPush (Z := Unit) t d r true σ).1, + (∀ p, p ≠ sc → ∀ w k, z.2.kheap p = some (.schedContext w k) → + ∃ w0, σ.kheap p = some (.schedContext w0 k) ∧ w.scTcb = w0.scTcb) ∧ + (∀ w, z.2.kheap d = some (.tcb w) → w.tcbSchedContext = some sc) := by + intro z hz + unfold replyPush at hz + rw [nd_bind_one (getTcbObjRef_eq _ hk), nd_bind_one (getTcbObjRef_eq _ hkd), hsc, hd0] at hz + obtain ⟨u1, h1, hz⟩ := mem_bindA.1 hz + obtain ⟨u2, h2, hz⟩ := mem_bindA.1 hz + unfold NonDet.when at hz + rw [if_pos ⟨Option.some_ne_none _, rfl, rfl⟩] at hz + obtain ⟨u3, h3, hz⟩ := mem_bindA.1 hz + refine ⟨?_, donate_bindsA sc d u3.2 trivial _ hz⟩ + have upd : ∀ {a b : State Unit} {p : ObjRef} {o : KernelObject}, b.kheap = Function.update a.kheap p (some o) → + (∀ w k, o ≠ .schedContext w k) → ScThA a b := fun {a b p o} e ho q w k hq => by + rw [e] at hq + by_cases c : q = p + · subst c + rw [Function.update_self] at hq + exact absurd (Option.some.inj hq) (ho w k) + · rw [Function.update_of_ne c] at hq; exact ⟨w, hq, rfl⟩ + have kh : ∀ {a b : State Unit}, b.kheap = a.kheap → ScThA a b := fun e q w k hq => ⟨w, e ▸ hq, rfl⟩ + have tset : ∀ p (f : Tcb → Tcb), PresR ScThA (threadSet (Z := Unit) f p) := fun p f s r hr => by + obtain ⟨q, -, e⟩ := threadSet_memA _ p hr + rw [e] + exact upd rfl (fun _ _ h => by cases h) + have k2 : ScThA u1.2 u2.2 := by + unfold Fl4SpecA64.setThreadState at h2 + obtain ⟨q, hq, h2⟩ := mem_bindA.1 h2 + refine PreO.trans (tset t _ _ _ hq) ?_ + rw [setThreadStateAct_eq] at h2 + have e : u2 = ((), stsAct q.2 t) := h2 + rw [e]; exact kh (stsAct_kheap _ _) + have s3 : ScThA σ u3.2 := + PreO.trans (setReplyObjRef_scThA _ _ _ _ _ h1) (PreO.trans k2 (bindScReply_scThA _ _ _ _ h3)) + unfold schedContextDonate at hz + obtain ⟨⟨f, s4⟩, h4, hz⟩ := mem_bindA.1 hz + obtain ⟨hs, -⟩ := getScObjRef_mem h4 + simp only at hs hz + subst hs + obtain ⟨u5, h5, hz⟩ := mem_bindA.1 hz + have res : PresR ScThA (rescheduleRequired (Z := Unit)) := fun s r hr => by + obtain ⟨q, -⟩ := rescheduleRequired_memA hr + exact kh (by rw [q.1]) + have s5 : ScThA u3.2 u5.2 := by + refine (?_ : PresR ScThA _) _ _ h5 + refine PresR.when ?_ + refine PresR.bind (fun s r hr => by + rw [← (show PresR (@Eq (State Unit)) _ by eqpres) s r hr]; exact PreO.refl s) (fun u => ?_) + refine PresR.bind (fun s r hr => kh (tcbSchedDeq_khA _ s r hr)) fun _ => + PresR.bind (fun s r hr => kh (tcbReleaseRemove_khA _ s r hr)) fun _ => + PresR.bind (tset _ _) fun _ => ?_ + unfold testReschedule + refine PresR.bind (PresR.gets _) (fun _ => PresR.bind (PresR.gets _) (fun _ => ?_)) + exact PresR.bind (PresR.pure _) (fun _ => PresR.when res) + obtain ⟨u6, h6, hz⟩ := mem_bindA.1 hz + intro p hp w k hw + obtain ⟨w6, hw6, e6⟩ := tset _ _ _ _ hz p w k hw + have hw5 : u5.2.kheap p = some (.schedContext w6 k) := by + unfold setScObjRef at h6 + obtain ⟨q, n, -, e⟩ := updateSchedContext_memA sc _ h6 + rw [e] at hw6 + simpa [Function.update_of_ne hp] using hw6 + obtain ⟨w0, hw0, e0⟩ := (PreO.trans s3 s5 : ScThA σ u5.2) p w6 k hw5 + exact ⟨w0, hw0, e6.trans e0⟩ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- `replyPushDon_holderA` over the donating `SendEP` arm. -/ +theorem recvSendDon_run_holderA {th : TypedHeapA} {kb : U64} (hPl : PlaceA th kb) + (sf : S) {σ : State Unit} (hR : StateRelA th (L.mem sf) σ) (hV : SlotsValidA th (L.mem sf)) {t sd : U64} + (htr : th t = some .tcb) {e : ObjRef} {q : List ObjRef} + {xt xs : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) (hks : σ.kheap (specPtr sd) = some (.tcb xs)) + (hal : ∀ rb, ipcBufOfA true xt = some rb → (bufAddr rb 0).toNat % 8 = 0) + (r : ObjRef) {e' : ObjRef} {dd : SenderPayload} (hst : xs.tcbState = .blockedOnSend e' dd) + (hcase : XferCaseA xs dd.senderCanGrant) + (hcf : dd.senderIsCall = true ∨ xs.tcbFault ≠ none) + (hg : dd.senderCanGrant = true ∨ dd.senderCanGrantReply = true) + (htmo : ¬ (xs.tcbFault.elim false isTimeoutFault = true)) {sc : ObjRef} (hsc : xs.tcbSchedContext = some sc) + (ht0 : xt.tcbSchedContext = none) : + ∀ x ∈ (specRecvSendA (specPtr t) e (some r) (specPtr sd :: q) σ).1, + (∀ p, p ≠ sc → ∀ w k, x.2.kheap p = some (.schedContext w k) → + ∃ w0, σ.kheap p = some (.schedContext w0 k) ∧ w.scTcb = w0.scTcb) ∧ + (∀ w, x.2.kheap (specPtr t) = some (.tcb w) → w.tcbSchedContext = some sc) := by + intro x hx + obtain ⟨s1, hp, k, -⟩ := recvSendDon_prefixA (i := σ.idleThread) (M := fun _ => True) (N := fun _ => False) + hPl sf hR hV htr hkt hks hal r hst hcase hcf hg htmo hsc trivial x hx + obtain ⟨ys, hys, es⟩ := k.tcbScA hks + obtain ⟨yt, hyt, et⟩ := k.tcbScA hkt + have h := replyPushDon_holderA (r := r) hys (es.trans hsc) hyt (et.trans ht0) x hp + refine ⟨fun p hp' w n hw => ?_, h.2⟩ + obtain ⟨w1, hw1, e1⟩ := h.1 p hp' w n hw + obtain ⟨w0, hw0, g⟩ := k.scB hw1 + exact ⟨w0, hw0, e1.trans (g.th.resolve_right fun c => by cases c.1)⟩ + +end +end +end Fl4ProofsA64.P3.Sched + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4Spec Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {J5 J7 : JSlotA S} {R : Set U64 → JSlotA S} {s s' : S} {a : A} {σ σ' : State Unit} + {m : McState} + +set_option maxHeartbeats 4000000 in +/-- **`Recv` by a passive receiver with a call sender waiting: the reply push donates the sender's SC** (module +docstring). [S] donating push arm covers VCPU-less senders; VCPU-bound sender is a separate scope case (same as +sendDonPlainA). -/ +theorem RecvPreA.recvPushDonA_core + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p badge slot rr rslot : U64} {send grant grantReply blk rg : Bool} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) + (hsc : capA st s slot = .Endpoint p badge send true grant grantReply) + (hrslot : rslot ∈ OS) (hrsc : capA st s rslot = .Reply rr rg) (hrr : rr ∈ Rp) + (hfree : L.mem s (Endp.rTcbA rr) = 0#u64) + (hp : p ∈ E) {sd : U64} {q : List ObjRef} + (hk : σ.kheap (specPtr p) = some (.endpoint (.sendEP (specPtr sd :: q)))) + (hbn : ∀ y, σ.kheap σ.curThread = some (.tcb y) → BndOKA N (L.mem s) σ (L.curW s) y blk) + (hsnd : ∀ y xt, σ.kheap (specPtr sd) = some (.tcb y) → σ.kheap σ.curThread = some (.tcb xt) → + y.tcbSchedContext ≠ none ∧ ∀ e' dd, + y.tcbState = .blockedOnSend e' dd → (dd.senderIsCall = true ∨ y.tcbFault ≠ none) ∧ + (dd.senderCanGrant = true ∨ dd.senderCanGrantReply = true)) + (hsndF : ∀ y xt, σ.kheap (specPtr sd) = some (.tcb y) → σ.kheap σ.curThread = some (.tcb xt) → + (y.tcbFault = none ∧ ∀ e' dd, y.tcbState = .blockedOnSend e' dd → dd.senderCanGrant = false ∨ + ipcBufOfA false y = none ∨ + unat (dataToMessageInfo (y.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0) ∨ + ∃ f, y.tcbFault = some f ∧ Endp.FaultOKA (L := L) s sd (ipcBufOfA true xt) y f) + (hpas : ∀ y, σ.kheap σ.curThread = some (.tcb y) → y.tcbSchedContext = none) + (hV : L.mem s (core.num.U64.wrapping_add sd 3664#u64) = 0#u64) : + RecvCoreA st AR ar hPl.ipa n Z.Cs s a m (.Endpoint p badge send true grant grantReply) blk (.Reply rr rg) + (absCapA (.Endpoint p badge send true grant grantReply)) (.replyCap (specPtr rr) (replyRightsA rg)) σ + (fun σ' => ((), σ') ∈ (specRecvSendA σ.curThread (specPtr p) (some (specPtr rr)) (specPtr sd :: q) σ).1) := by + have htZ : L.curW s ∈ Z.T := J.sr.curT + set t := L.curW s with ht_def + have ct : σ.curThread = specPtr t := J.j.1.sch.curThread + obtain ⟨xt, hkt, r⟩ := J.ep.tcb t htZ + have hkc : σ.kheap σ.curThread = some (.tcb xt) := by rw [ct]; exact hkt + have hbnd := hbn xt hkc + have hthT : Z.th t = some .tcb := (J.sr.thT t).2 htZ + have hnvm : L.mem s (stA t) ≠ sched.TS_RUNNING_VM := J.nvm + have hrunS : xt.tcbState = .running ∨ xt.tcbState = .restart := J.state hkt + have hidle : specPtr t ≠ σ.idleThread := by + obtain ⟨y, hy, hs⟩ := J.sinv.idleTcb + intro h + rw [← h, hkt] at hy + cases hy + rcases hrunS with e | e <;> rw [e] at hs <;> cases hs + have hcm : m.curCore < fl4A64Platform.numNodes := by rw [J.j.1.sch.core]; exact hCs _ J.j.1.sch.cpuCs + have hO : SchedOpsEA st Z.T E Rp Bf (IpcSchedRelA st Z m κ) := + (ipcSchedOpsA (ar := ar) J.iplaced J.ipcPlace hcm hmb hmax htu).2.2.1 + have hF : SchedFootEA st Z.T E Rp Bf (IpcSchedRelA st Z m κ) (fun σ s => MWrA (st := st) Z m σ s) := + schedFootEA_of_sched + have hSR : IpcSchedRelA st Z m κ s σ := ⟨Z.Q, Z.R, J.sr⟩ + -- the sender + obtain ⟨hst1, xs0, -, hq0, hQ, hB⟩ := J.ep.at_ep hp hk + have hsdx : sd ∈ xs0 := by + have : specPtr sd ∈ xs0.map specPtr := by rw [← hq0]; exact List.mem_cons_self + obtain ⟨y, hy, e⟩ := List.mem_map.1 this + rwa [specPtr_inj e] at hy + have hsdT : sd ∈ Z.T := hQ.mem sd hsdx + have hs4 : (L.mem s (stA sd)).val = 4 := (hB sd hsdx).1 + have hsb : L.mem s (Notif.tBlockA sd) = p := (hB sd hsdx).2 + obtain ⟨xs, hks, rs⟩ := J.ep.tcb sd hsdT + obtain ⟨e', dd, hsst⟩ := tsCode_four (rs.st.symm.trans hs4) + obtain ⟨hssc, hsnd'⟩ := hsnd xs xt hks hkc + obtain ⟨hcall, hgr⟩ := hsnd' e' dd hsst + have hX : XferCaseA xs dd.senderCanGrant := by + rcases hsndF xs xt hks hkc with g | ⟨f, hf, -⟩ + · exact Or.inl ⟨g.1, g.2 e' dd hsst⟩ + · exact Or.inr ⟨f, hf⟩ + have hr0 : rr ≠ 0#u64 := fun eq => (J.iplaced.e.rep rr hrr).2.1 (by rw [eq]; rfl) + obtain ⟨xr, hkr, hxr⟩ := J.ep.reply rr hrr + have hxr0 : xr.replyTcb = none := by rw [hxr, hfree, optRef_zero] + have hts : specPtr t ≠ specPtr sd := fun h => by + rw [h, hks] at hkt + cases hkt + rcases hrunS with e | e <;> rw [e] at hsst <;> cases hsst + have hsdi : specPtr sd ≠ σ.idleThread := by + obtain ⟨x, hx, hs⟩ := J.sinv.idleTcb + intro h + rw [← h, hks] at hx + cases hx + rw [hsst] at hs + cases hs + have hsdth : Z.th sd = some .tcb := (J.sr.thT sd).2 hsdT + have hnb : tsCode xt.tcbState ≠ 4 := by rcases hrunS with e | e <;> rw [e] <;> simp [tsCode] + have hu := J.toJStage3A.bufFrameA htZ hkt true + have hal := J.toJStage3A.bufAlign0A htZ hkt true + have hoff := J.toJStage3A.slotOffEwA + -- the sender's SC and the donation's side conditions + have htSpec : xt.tcbSchedContext = none := hpas xt hkc + have ht0 : L.mem s (scA t) = 0#u64 := by + have h := Endp.schedTcbScA J.sr htZ hkt + rw [htSpec] at h + unfold optScA at h + split_ifs at h with c + exact c + set sc : U64 := L.mem s (scA sd) with hsc_def + have hsc0 : sc ≠ 0#u64 := fun h => hssc (by rw [Endp.schedTcbScA J.sr hsdT hks, ← hsc_def, h, optScA, if_pos rfl]) + have hscC : sc ∈ Z.C := J.sr.rest.scMem sd hsdT hsc0 + have hsSc : xs.tcbSchedContext = some (scNameA m sc) := by + rw [Endp.schedTcbScA J.sr hsdT hks, ← hsc_def, optScA, if_neg hsc0] + rfl + have hDon : DonA σ (specPtr sd) xs := by + intro sc' hsc' + obtain ⟨y, k, hy, hb⟩ := J.sr.inv.back (specPtr sd) xs sc' hks hsc' + refine ⟨fun h => ?_, fun y' k' hy' => by rw [hy] at hy'; cases hy'; exact hb⟩ + obtain ⟨x, yi, ki, -, -, hyi, hti⟩ := J.sinv.idleSc + rw [h, hyi] at hy + cases hy + exact hsdi (Option.some.inj (hb.symm.trans hti)) + have hni : NotIdleScA m (scNameA m sc) := by + have h1' : scNameA m sc ≠ idleScPtr := (hDon _ hsSc).1 + refine ⟨h1', fun c hc' e => ?_⟩ + have hcc : c = m.curCore := by + have a : c < 1 := h1 ▸ hc' + have b : m.curCore < 1 := h1 ▸ hcm + exact (Nat.lt_one_iff.1 a).trans (Nat.lt_one_iff.1 b).symm + rw [hcc] at e + unfold scNameA at e h1' + split_ifs at e h1' with g + · exact h1' rfl + · exact g e + have hfon : L.mem s (affA sd) = L.cpu s := by + have x := hCs _ (J.sr.core.aff sd hsdT) + have y := hCs _ J.j.1.sch.cpuCs + exact UScalar.eq_of_val_eq (by omega) + have hmig : L.mem s (affA t) = L.mem s (scCoreA sc) := by + have x := hCs _ (J.sr.core.aff t htZ) + have y := J.scCpu sc hscC + refine UScalar.eq_of_val_eq ?_ + rw [J.sr.scCore sc hscC] + omega + have hcaseF : ∀ e'' dd', xs.tcbState = .blockedOnSend e'' dd' → + (xs.tcbFault = none ∧ (dd'.senderCanGrant = false ∨ ipcBufOfA false xs = none ∨ + unat (dataToMessageInfo (xs.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0)) ∨ + ∃ f, xs.tcbFault = some f ∧ Endp.FaultOKA (L := L) s sd (ipcBufOfA true xt) xs f := fun e'' dd' h => by + rcases hsndF xs xt hks hkc with g | g + · exact Or.inl ⟨g.1, g.2 e'' dd' h⟩ + · exact Or.inr g + have htmo : ¬ (xs.tcbFault.elim false isTimeoutFault = true) := by + cases h : xs.tcbFault with + | none => exact Bool.false_ne_true + | some f => + cases f + case timeout b => + rcases hsndF xs xt hks hkc with g | ⟨g, hg, hok⟩ + · rw [g.1] at h; cases h + · rw [h] at hg + cases hg + exact absurd (show xs.tcbSchedContext = none ∧ _ from hok).1 hssc + all_goals exact Bool.false_ne_true + -- FL4 and the spec: `receive_from_sender` + have push := Endp.receive_from_sender_pushDon_fpA (st := st) (ar := ar) a J.sr J.ipcPlace J.iplaced.e J.ep J.reply + J.sr.place J.j.1.st J.j.1.cv J.bufCover J.bufNative hoff htZ hkt hnb ht0 hp hk hsdT hks hssc hscC hni hfon hmig + hV hu hcaseF hrr (J.toJStage3A.replyIsolatedUnlA hrr hfree) hsnd' (fun u hu => J.swBlocked hks (by rw [hsst]; rfl) u hu) + hsdi + obtain ⟨⟨s2, a2⟩, eb, ha2, σ2, heq, E2, ⟨Q, Rl, sr2⟩, reply2, wn⟩ := exists_of_specA push + rw [← hsc_def] at reply2 + simp only at ha2 E2 sr2 reply2 wn + subst ha2 + have hmem : ((), σ2) ∈ (specRecvSendA (specPtr t) (specPtr p) (some (specPtr rr)) (specPtr sd :: q) σ).1 := by + rw [heq]; exact Set.mem_singleton _ + -- FL4: `receive_ipc`, `handle_recv` + have hdisp : rcvDispA st ar s a2 t p blk rr ⦃ q' => q' = (s2, a2) ⦄ := by + unfold rcvDispA + step with obj.Ep.state.spec as ⟨st0, hst0⟩ + rw [epState_eq, hst1] at hst0 + subst hst0 + rw [if_neg (by rw [ep_state_vals.1]; decide), if_neg (by rw [ep_state_vals.2.2]; decide)] + rw [eb] + exact (WP.spec_ok _).2 rfl + have hipc : endpoint.receive_ipc st ar s a2 t (.Endpoint p badge send true grant grantReply) blk (.Reply rr rg) = + ok (s2, a2) := by + have h : endpoint.receive_ipc st ar s a2 t (.Endpoint p badge send true grant grantReply) blk (.Reply rr rg) ⦃ q => + q = (s2, a2) ⦄ := by + rw [receive_ipc_eqA] + refine WP.spec_bind (take_reply_plainA (R := Rp) (rc' := .replyCap (specPtr rr) (replyRightsA rg)) + (Or.inr ⟨rg, _, rfl, rfl, hrr⟩ : ReplyCapA Rp (.Reply rr rg) (.replyCap (specPtr rr) (replyRightsA rg)) rr) + (fun _ _ => Or.inl hfree)) ?_ + rintro q rfl + exact rcv_body_dispFlA J.ntfn J.ep htZ hkt hbnd hdisp + obtain ⟨q', hq', e⟩ := spec_existsA h + rw [hq', e] + -- the spec: `handle_recv` + have hSt := J.j.1.st + have hV' := J.j.1.cv + have capReg : (L.mem s (regWA t layout.reg.CAP)).bv = xt.tcbArch.tcbContext.userRegs cap_register := by + rw [← r.regs cap_register, regIdxA_capA]; rfl + have hrecv : ((), σ2) ∈ (receiveIpc (Z := Unit) (specPtr t) + (absCapA (.Endpoint p badge send true grant grantReply)) blk (.replyCap (specPtr rr) (replyRightsA rg)) σ).1 := by + rw [show absCapA (.Endpoint p badge send true grant grantReply) = + .endpointCap (specPtr p) badge.bv (rightsOfA send true grant grantReply) from rfl, + receiveIpc_bodyA _ _ blk (Or.inr ⟨specPtr rr, _, xr, rfl, rfl, hkr, Or.inl hxr0⟩ : + SpecReplyPlainA σ (specPtr t) (.replyCap (specPtr rr) (replyRightsA rg)) (some (specPtr rr))), + bndOK_specA hkt hk J.ntfn hbnd (some (specPtr rr))] + exact hmem + -- P1's walk + obtain ⟨Pt, Ub, Pp7, Tb7, Hr, Te, Kt, Lv7, Re, bits, KLv, W⟩ := J7A.unp J.j7 + have I := J.delIn hA hPl W + have h : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s := by + obtain ⟨q, b, x, sg, sc⟩ := J.sys.ent.bxi + exact ⟨q, b, x, sc, sg⟩ + have hext : ∀ z ∈ (Aeneas.Std.Array.repeat 3#usize 0#u64).val, z ≠ 0#u64 → z ∈ OS := fun z hz hne => by + exact absurd (by simpa [Aeneas.Std.Array.repeat] using hz) hne + have D := J.decPreA (extra := Aeneas.Std.Array.repeat 3#usize 0#u64) hslot hext + have Pz := J.toJStage3A.sysPlace hext hslot hcov + have hH : H p = some .ep := J.utIpc.ep p hp + have regs : RpPost st H Z.Cs OS Z.V ia ie Ac s s2 := by + have walk := Fl4ProofsA64.P1.W7.R.ST.receive_ipc.rpw (st := st) (ar := ar) hA a2 t + (.Endpoint p badge send true grant grantReply) blk (.Reply rr rg) p I J.core0 J.ko Pz (SW.start J.sys.sysl J.symL h) + D.cb + (by rcases J.runW with e | e | e <;> rw [e] <;> + simp [sched.TS_RUNNING, sched.TS_RESTART, sched.TS_RUNNING_VM, sched.TS_INACTIVE]) + rfl hH + (fun k hk' => by + rw [hH] at hk' + cases hk' + exact ⟨slot, hslot, by rw [hsc]; exact names_ep⟩) + (fun _ _ c => by cases c; exact ⟨J.utIpc.reply rr hrr, rslot, hrslot, rg, hrsc⟩) + (J.tcbs ▸ J.runVcpu) hnvm + exact (post_of_okA (P := fun q => RpW st H Z.Cs OS Z.V ia ie Ac s q.1) walk hipc).post + obtain ⟨bxi, sysl, symL, userOK, j7⟩ := J.toJStage3A.finRpA hA hPl rfl regs σ2 + -- the spec outcome's facts + set M : ObjRef → Prop := fun o => o = specPtr t ∨ o = specPtr sd with hMdef + set Nq : ObjRef → Prop := fun _ => False with hNdef + have K : SIKA σ.idleThread M Nq true σ σ2 := + recvSendDon_run_sikA (M := M) (N := Nq) J.sr.place s hSt hV' hthT hkt hks hal (specPtr rr) hsst hX hcall hgr + htmo hsSc hDon (Or.inl rfl) (Or.inr rfl) hsdi hidle _ hmem + have hAct : ScActiveAtA σ (scNameA m sc) := + (SchedValidA.toFIA J.schedValid).1.blk _ xs _ hks (by rw [hsst]; rfl) hsSc + have Qr : QBlkRA (specPtr '' E) (specPtr '' N) σ σ2 ∧ QStepA (some (specPtr rr)) (some (scNameA m sc)) σ σ2 := + recvSendDon_run_qbRA (i := σ.idleThread) (M := M) (N := Nq) J.sr.place s hSt hV' hthT hkt hks hal + (specPtr rr) hsst hX hcall hgr htmo hsSc hAct hDon (Or.inl rfl) (Or.inr rfl) hsdi hidle J.ipcSorted _ hmem + obtain ⟨U2, run'⟩ := recvSendDon_run_frA J.sr.place s hSt hV' hthT hkt hks hal (specPtr rr) hsst hX hcall hgr + htmo hsSc _ hmem + have hBt : BoundActive (specPtr t) σ := + J.boundActive + have sv : SchedValidA σ2 := + recvSendDon_run_schedValidA J.sr.place s hSt hV' hthT hkt hks hal (specPtr rr) hsst hX hcall hgr htmo hsSc + htSpec hts J.schedValid J.toJStage3A.rqDomPrioA _ hmem + obtain ⟨sw2, lmt2⟩ := recvSendDon_run_swA J.sr.place s hSt hV' hthT hkt hks hal (specPtr rr) hsst hX hcall hgr + htmo hsSc htSpec hDon hsdi hidle J.body.sw (fun u hu => ⟨J.swCur u hu, J.swBlocked hks (by rw [hsst]; rfl) u hu⟩) + _ hmem + have C2 : CscKA σ σ2 := recvSend_run_cscA J.sr.place s hSt hV' hthT hkt hks hal (some (specPtr rr)) hsst hX _ hmem + obtain ⟨hOth, hBnd⟩ := recvSendDon_run_holderA J.sr.place s hSt hV' hthT hkt hks hal (specPtr rr) hsst hX hcall hgr + htmo hsSc htSpec _ hmem + have hrt : ∀ r' ∈ Rp, DeadA st OS .reply s r' → ∀ y, σ2.kheap (specPtr r') = some (.reply y) → y.replyTcb = none := by + intro r' hr' hd y hy + obtain ⟨x, hx, keep⟩ := Qr.2.rt (specPtr r') y hy + refine keep (fun h => ?_) (J.deadExt.replyTcb r' hr' hd x hx) + have e : r' = rr := specPtr_inj (Option.some.inj h) + exact hd rslot hrslot (e ▸ ⟨rg, hrsc⟩) + -- the receiver's state word is kept + have hst2 : L.mem s2 (stA t) = L.mem s (stA t) := by + have hta := J.sr.core.tcbs t htZ + refine wn.mem_eq (a_st hta).1 ?_ + have hv := Endp.val_st hta + have apart : ∀ u ∈ Z.T, u ≠ t → u.val + 4096 ≤ t.val ∨ t.val + 4096 ≤ u.val := fun u hu ne => by + have := J.sr.place.apart u t .tcb .tcb ((J.sr.thT u).2 hu) ((J.sr.thT t).2 htZ) ne + have hsz : ObjKindA.size .tcb = 4096 := rfl + omega + rintro ((c | c | c) | ⟨q', hq', c1, c2⟩ | c) + rotate_left 3 + · have := J.iplaced.e.rt q' hq' t htZ + omega + · have h' := congrArg (fun w : U64 => w.val) c + simp only [hv, val_reply (J.iplaced.e.tcb sd hsdT)] at h' + have := apart sd hsdT (fun e => hts (by rw [e])) + omega + · rcases c with c | ⟨u, hu, -, c⟩ | ⟨r', c⟩ | c + · have := J.iplaced.e.et p hp t htZ; unfold Endp.EW at c; omega + · have := (J.iplaced.e.tcb u hu).fits + unfold TL at c + by_cases e : u = t + · subst e; omega + · have := apart u hu e; omega + · rw [Endp.regA_val (J.iplaced.e.tcb t htZ) (regIdxA_lt r')] at c + have := regIdxA_lt r' + omega + · have := J.iplaced.e.bt _ c t htZ; omega + · have h' := congrArg (fun w : U64 => w.val) c + simp only [hv, Endp.val_st (J.iplaced.e.tcb sd hsdT)] at h' + exact hts (congrArg specPtr (UScalar.eq_of_val_eq (by omega))) + · rw [stA_eq] at c + exact J.toJStage3A.tcbWord_offMwrA htZ (by simp) (by simp) c + have hxst : ∀ x, σ2.kheap (specPtr t) = some (.tcb x) → x.tcbState = .running ∨ x.tcbState = .restart := by + intro x hx + obtain ⟨y, hy, r2⟩ := E2.tcb t htZ + rw [hx] at hy + cases hy + have h2 : tsCode x.tcbState = tsCode xt.tcbState := by rw [← r2.st, hst2, r.st] + rcases hrunS with e | e <;> rw [e] at h2 <;> + (rcases hs : x.tcbState with _ | _ | _ | _ | _ | _ | _ | _ | ⟨⟨⟩⟩ <;> rw [hs] at h2 <;> + simp [tsCode] at h2 ⊢) + have after : ∀ u ∈ Z.T, M (specPtr u) → ∀ x, σ2.kheap (specPtr u) = some (.tcb x) → + epBlocked x.tcbState = none ∧ ∀ p, x.tcbState ≠ .blockedOnNotification p := by + intro u _ hu x hx + rcases hu with h | h + · rw [specPtr_inj h] at hx + rcases hxst x hx with e | e <;> rw [e] <;> exact ⟨rfl, fun _ h => by cases h⟩ + · rw [h] at hx + · rw [run' x hx]; exact ⟨rfl, fun _ h => by cases h⟩ + have wk : NtfnWaitKeepA Z.T N (L.mem s) (L.mem s2) := J.toJStage3A.ntfnWait_recvPushA htZ hp hsdT hsb wn + have hsub : ∀ b, RecvPushFpA st Z m Rp Bf σ s t p sd b → + EWrite Z.T E Rp Bf b ∨ MWrA (st := st) Z m σ s b ∨ RepWA Rp b := + fun b h => recvPushFp_subA J.iplaced.e htZ hp hsdT h + have wI : WritesIn st s s2 (fun b => IpcFpA st Z E Rp Bf m σ s b ∨ RepWA Rp b) := wn.mono fun b h => by + rcases hsub b h with h | h | h + · exact Or.inl (Or.inl h) + · exact Or.inl (Or.inr h) + · exact Or.inr h + have wN : WritesIn st s s2 (fun b => SchedStepNWA (st := st) N E Rp Z m σ s b ∨ BufW Bf b) := wn.mono fun b h => by + rcases hsub b h with h | h | h + · exact ewrite_stepNA_buf J.sr.core.tcbs h + · exact Or.inl (Or.inl (Or.inl h)) + · exact Or.inl (Or.inr (Or.inr (Or.inr h))) + have notM : ∀ c < n, ¬ M (m.idleThreadOf c) := by + intro c hc' h + obtain ⟨y, hy, hs⟩ := J.sidle.f3 c hc' + rcases h with h | h + · rw [h, hkt] at hy; cases hy; rcases hrunS with e | e <;> rw [e] at hs <;> cases hs + · rw [h, hks] at hy; cases hy; rw [hsst] at hs; cases hs + have sinv : SInvXA σ2 m n := by + refine K.sinvSw J.sinv J.sidle J.body.sw J.noSelf (fun u hu h => h.elim (J.swCur u hu) (J.swBlocked hks (by rw [hsst]; rfl) u hu)) notM (fun h => h.elim (fun h => hidle h.symm) fun h => hsdi h.symm) ?_ ?_ ?_ + (fun y hy hr => by + rcases hy with rfl | rfl + · exact J.notRel (by rw [ct]; exact hr) + · obtain ⟨x, hx, hrn⟩ := J.sinv.rlsRun _ hr; rw [hks] at hx; cases hx; rw [hsst] at hrn; cases hrn) + (J.sidle.rdyA J.sinv) + (fun q hq x y hx hy hf => by + rcases hq with rfl | rfl + · obtain ⟨x0, hx0, g⟩ := K.tcbB hy + rw [hkt] at hx0; cases hx0 + exact absurd (g.fault.trans (FaultStOkA.none J.sinv.faultSt hkt (by + rcases hrunS with e | e <;> rw [e] <;> rintro (⟨_, _, h⟩ | ⟨_, h⟩ | h) <;> cases h))) hf + · rw [run' y hy]; exact Or.inr (Or.inl ⟨_, rfl⟩)) + · intro y hy dm pr hm + rcases hy with rfl | rfl + · exact J.noQ dm pr (by rw [ct]; exact hm) + · obtain ⟨x, hx, hr⟩ := J.sinv.rq dm pr _ hm + rw [hks] at hx; cases hx; rw [hsst] at hr; cases hr + · exact fun y hy => hy.elim + · exact fun y hy => hy.elim + have hMv : ∀ u ∈ Z.T, M (specPtr u) → L.mem s (stA u) ≠ sched.TS_RUNNING_VM := by + intro u _ hu h + rcases hu with e | e <;> rw [specPtr_inj e] at h + · exact hnvm h + · rw [h] at hs4; revert hs4; unfold sched.TS_RUNNING_VM; decide + have ntfn := J.toJStage3A.ntfn_ipcA wI K E2 wk (fun u hu hm x hx p' h => by + rcases hm with hm | hm + · rw [hm, hkt] at hx; cases hx; rcases hrunS with e | e <;> rw [e] at h <;> cases h + · rw [hm, hks] at hx; cases hx; rw [hsst] at h; cases h) (fun u hu hm x hx => (after u hu hm x hx).2) + have hdr : ∀ r' ∈ Rp, DeadA st OS .reply s r' → ∀ c ∈ Z.C, + r' ∉ Function.update stack sc (rr :: stack sc) c := by + intro r' hr' hd c hc hm + have old := J.deadExt.reply r' hr' hd + by_cases e : c = sc + · rw [e, Function.update_self] at hm + rcases List.mem_cons.1 hm with h | h + · exact hd rslot hrslot (h ▸ ⟨rg, hrsc⟩) + · exact old sc hscC h + · rw [Function.update_of_ne e] at hm + exact old c hc hm + have J2 := J.toJStage3A.of_ipcStepURSA (Q := Q) (Rl := Rl) (curOf := Z.curOf) (curScOf := Z.curScOf) wN U2.1 U2.2.1 + Qr.1 hrt (J.toJStage3A.specZero_of_recvBufA htZ hkt U2.2.2) (fun _ _ => rfl) bxi sysl symL userOK j7 sr2 E2 ntfn + reply2 hdr K.ipcKeep (J.toJStage3A.tw_ipcA wI) sinv + (K.sidle J.sidle notM (J.sidle.rdyA J.sinv)) (J.toJStage3A.runVcpu_ipcA wI K E2 hMv) (J.toJStage3A.blkQ_ipcA K E2 wk after) sv + have hcur : σ2.curThread = σ.curThread := by rw [K.frame] + refine ⟨s2, σ2, hipc, hrecv, by rw [ct]; exact hmem, A64RelS3.ofJ J2 ?bo, curW_keptA J.toJStage3A J2 hcur, ?bo⟩ + refine J.body.stepOwn (σ' := σ2) sw2 (by rw [K.frame]) lmt2 hcur (by rw [K.frame]) (by rw [K.frame]) + (fun y' k h => C2.sc y' k h) (fun y' k hk' act hni' hpos o ho hm => ?_) (fun _ z hz => ?_) + · by_cases hcs : σ.curSc = scNameA m sc + · obtain ⟨z, hz, -⟩ := E2.tcb t htZ + obtain ⟨y2, k2, hy2, hb⟩ := J2.sr.inv.back _ z _ hz (hBnd z hz) + rw [← hcs, hk'] at hy2 + cases hy2 + obtain rfl : specPtr t = o := Option.some.inj (hb.symm.trans ho) + exact J.notRel (by rw [ct]; exact K.rls _ hm) + · obtain ⟨y0, hy0, e⟩ := hOth _ hcs y' k hk' + obtain ⟨y1, hy1, -, erm⟩ := C2.sc y' k hk' + rw [hy0] at hy1 + cases hy1 + refine J.body.owner y0 k hy0 ?_ hni' hpos o (e ▸ ho) (K.rls _ hm) + unfold scActive at act ⊢ + rw [← erm]; exact act + · rw [ct] at hz + exact hxst z hz + +set_option maxHeartbeats 4000000 in +theorem RecvPreA.recvPushDonA + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p badge slot rr rslot : U64} {send grant grantReply blk rg : Bool} + (hcap : cspace.lookup_cap st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.CAP)) = + ok (.Ok (.Endpoint p badge send true grant grantReply))) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) + (hsc : capA st s slot = .Endpoint p badge send true grant grantReply) + (hcapR : cspace.lookup_cap st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.REPLY)) = ok (.Ok (.Reply rr rg))) + (hrslot : rslot ∈ OS) (hrsc : capA st s rslot = .Reply rr rg) (hrr : rr ∈ Rp) + (hfree : L.mem s (Endp.rTcbA rr) = 0#u64) + (hp : p ∈ E) {sd : U64} {q : List ObjRef} + (hk : σ.kheap (specPtr p) = some (.endpoint (.sendEP (specPtr sd :: q)))) + (hbn : ∀ y, σ.kheap σ.curThread = some (.tcb y) → BndOKA N (L.mem s) σ (L.curW s) y blk) + (hsnd : ∀ y xt, σ.kheap (specPtr sd) = some (.tcb y) → σ.kheap σ.curThread = some (.tcb xt) → + y.tcbSchedContext ≠ none ∧ ∀ e' dd, + y.tcbState = .blockedOnSend e' dd → (dd.senderIsCall = true ∨ y.tcbFault ≠ none) ∧ + (dd.senderCanGrant = true ∨ dd.senderCanGrantReply = true)) + (hsndF : ∀ y xt, σ.kheap (specPtr sd) = some (.tcb y) → σ.kheap σ.curThread = some (.tcb xt) → + (y.tcbFault = none ∧ ∀ e' dd, y.tcbState = .blockedOnSend e' dd → dd.senderCanGrant = false ∨ + ipcBufOfA false y = none ∨ + unat (dataToMessageInfo (y.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0) ∨ + ∃ f, y.tcbFault = some f ∧ Endp.FaultOKA (L := L) s sd (ipcBufOfA true xt) y f) + (hpas : ∀ y, σ.kheap σ.curThread = some (.tcb y) → y.tcbSchedContext = none) + (hV : L.mem s (core.num.U64.wrapping_add sd 3664#u64) = 0#u64) : + HRecvPostA st AR ar hPl.ipa n Z.Cs s a m blk true σ + (fun σ' => ((), σ') ∈ (specRecvSendA σ.curThread (specPtr p) (some (specPtr rr)) (specPtr sd :: q) σ).1) := RecvPreA.recvFrontA J hcap hcapR + (RecvPreA.recvPushDonA_core hA hPl J hCs h1 hmb hmax htu hslot hcov hsc hrslot hrsc hrr hfree hp hk hbn hsnd hsndF hpas hV) + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3RecvPushDonFpA.lean b/proofs/Fl4ProofsA64/P3/Stage3RecvPushDonFpA.lean new file mode 100644 index 000000000..4f533fb88 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3RecvPushDonFpA.lean @@ -0,0 +1,266 @@ +/- +P3 (aarch64, A64-P3 Stage 3): **`receive_ipc` from a waiting call sender with a reply object, donating to a passive +receiver**: the FL4 side with the narrow write set. + +* `JStage3A.replyIsolatedUnlA`: an unlinked reply (`rTcb = 0`) is isolated. +* `relIfBoundSc_pushDonA`: the donating `reply_push` leaves the receiver's SC (the sender's) released. +* **`receive_from_sender_pushDon_fpA`**: FL4's `receive_from_sender` runs the spec's `receive_ipc` `SendEP` arm (one + outcome, `reply_push` donating the sender's SC to the receiver), writes only `RecvPushFpA`, and the reply goes on + the sender SC's stack. + +[S] the sender has no fault and no extra caps (or a non-timeout `FaultOKA` fault), and holds an SC; the receiver holds +none; call with grant or grant-reply; donating push arm covers VCPU-less senders; VCPU-bound sender is a separate +scope case (same as sendDonPlainA). No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3RecvPushFpA +import Fl4ProofsA64.P3.Stage3SendRecvFactsA +import Fl4ProofsA64.P3.IpcScPushTopA +import Fl4ProofsA64.P3.IpcScPushBlockA +import Fl4ProofsA64.P3.IpcScRecvPushA +import Fl4ProofsA64.P3.Stage3SendDonateKeepA + +open Aeneas Aeneas.Std Result +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 Fl4Spec + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- **An unlinked reply is isolated** (`replyIsolatedA` for a reply no thread holds). -/ +theorem JStage3A.replyIsolatedUnlA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) {r : U64} (hr : r ∈ Rp) + (hfree : L.mem s (Endp.rTcbA r) = 0#u64) : + ReplyIsolatedA (L.mem s) Z.C stack r := by + have VR : ValidReplies st H s := J.sys.sysl.vr + have R : ReplyInv st H.tcbs H.replies H.scs s := J.sys.ent.bxi.2.2.1.2.1 + have nR : ¬ RBlk st s r := fun h => h.tcb hfree + have inH : ∀ q ∈ Rp, q ∈ H.replies := fun q hq => J.utIpc.reply q hq + have ev : ∀ q ∈ Rp, q &&& ~~~1#u64 = q ∧ headBit q = false := fun q hq => + ut_even_maskA (by have := (J.iplaced.e.rep q hq).1; omega) + have r0 : r ≠ 0#u64 := fun e => (J.iplaced.e.rep r hr).2.1 (by rw [e]; rfl) + have notin : ∀ c ∈ Z.C, r ∉ stack c := by + intro c hc hm + obtain ⟨-, hmem, -, hw⟩ := J.reply.stackRel c hc + obtain ⟨i, hi, e⟩ := List.getElem_of_mem hm + cases i with + | zero => + have w := (hw 0 hi).2 + rw [e] at w + simp only at w + have cal : c.val % 8 = 0 := J.toJEntA.scAl c hc + have ce : c.val % 2 = 0 := by omega + have hb : headBit (L.mem s (rNextA r)) = true := by + rw [w, ← mask1_of_even ce]; exact Fl4ProofsA64.P1.nx_head1 c + have hp : L.mem s (rNextA r) &&& ~~~1#u64 = c := by rw [w]; exact replyHeadMaskA ce + have c0 : c ≠ 0#u64 := J.scNZ c hc + have hh := R.head r (inH r hr) hb (by rw [hp]; exact c0) + rw [hp] at hh + have cS : c ∈ H.scs := by rw [J.scs]; exact hc + have := VR.screp c cS (by rw [hh]; exact r0) + rw [hh] at this + exact nR this + | succ j => + have hj : j < (stack c).length := by omega + have w := (hw j hj).1 + rw [List.getD_eq_getElem _ _ hi, e] at w + have qR : (stack c)[j] ∈ Rp := hmem _ (List.getElem_mem hj) + have := VR.rprev _ (inH _ qR) (by rw [w, (ev r hr).1]; exact r0) + rw [w, (ev r hr).1] at this + exact nR this + obtain ⟨-, hn, hp⟩ := J.reply.detached r hr notin + refine ⟨notin, ?_, ?_⟩ + · by_contra h + obtain ⟨qR, -, hq⟩ := hn h + have := VR.rprev _ (inH _ qR) (by rw [hq, (ev r hr).1]; exact r0) + rw [hq, (ev r hr).1] at this + exact nR this + · by_contra h + obtain ⟨qR, -, hq⟩ := hp h + have := VR.rnext _ (inH _ qR) (by rw [hq, (ev r hr).1]; exact r0) (by rw [hq]; exact (ev r hr).2) + rw [hq, (ev r hr).1] at this + exact nR this + +end +end +end Fl4ProofsA64.P3 + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4SpecA64 Fl4Spec Sched +noncomputable section + +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + +/-- **The donating `reply_push` leaves the receiver's SC released**: the SC is the sender's, released at the push +(`RelIfBound` of the runnable sender) with the same refills and clock. -/ +theorem relIfBoundSc_pushDonA {σ σ' : State Unit} {t d r sc : ObjRef} {x y : Tcb} (hk : σ.kheap t = some (.tcb x)) + (hsc : x.tcbSchedContext = some sc) (hkd : σ.kheap d = some (.tcb y)) (hd0 : y.tcbSchedContext = none) + (hrl : t ∉ σ.releaseQueue) (hRB : RelIfBound σ t) + (hP : replyPush (Z := Unit) t d r true σ = ({((), σ')}, False)) : RelIfBoundSc σ' d := by + have m0 : ((), σ') ∈ (replyPush (Z := Unit) t d r true σ).1 := by rw [hP]; exact Set.mem_singleton _ + have RF : RfKA σ σ' := replyPush_rfA t d r true σ _ m0 + have bound : ∀ z, σ'.kheap d = some (.tcb z) → z.tcbSchedContext = some sc := by + unfold replyPush at m0 + rw [nd_bind_one (getTcbObjRef_eq _ hk), nd_bind_one (getTcbObjRef_eq _ hkd), hsc, hd0] at m0 + obtain ⟨u1, -, m1⟩ := mem_bindA.1 m0 + obtain ⟨u2, -, m2⟩ := mem_bindA.1 m1 + unfold NonDet.when at m2 + rw [if_pos ⟨Option.some_ne_none _, rfl, rfl⟩] at m2 + obtain ⟨u3, -, m3⟩ := mem_bindA.1 m2 + exact donate_bindsA sc d u3.2 trivial _ m3 + intro z scp w n hz hscp hw ha _ + have e1 : scp = sc := Option.some.inj (hscp.symm.trans (bound z hz)) + subst e1 + obtain ⟨y0, hy0, er, em⟩ := RF.2.2 _ _ _ hw + have a0 : scActive y0 = true := by unfold scActive at ha ⊢; rw [← em]; exact ha + have rel := hRB x scp y0 n hk hsc hy0 a0 hrl + unfold releasedSc + unfold released at rel + have eh : refillHd w = refillHd y0 := by unfold refillHd; rw [er] + rw [RF.1, eh] + exact rel + +set_option maxHeartbeats 400000 in +/-- **`receive_from_sender` from a waiting call sender, the reply push donating to a passive receiver** (module +docstring). [S] donating push arm covers VCPU-less senders; VCPU-bound sender is a separate scope case (same as +sendDonPlainA). -/ +theorem receive_from_sender_pushDon_fpA {s : S} {σ : State Unit} (a : A) {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {kb : U64} (hPl : PlaceA Z.th kb) (hSt : StateRelA Z.th (L.mem s) σ) (hVld : SlotsValidA Z.th (L.mem s)) + (hC : BufCover st Z.T Bf s σ) (hNative : BufNativeA Z.T (L.mem s)) + (hoff : ∀ y, IsSlotA Z.th y → ¬ EWrite Z.T E R Bf y ∧ + ¬ EWrite Z.T E R Bf (core.num.U64.wrapping_add y 8#u64)) + {t : U64} (ht : t ∈ Z.T) {xt : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hnb : tsCode xt.tcbState ≠ 4) (ht0 : L.mem s (scA t) = 0#u64) + {e f : U64} (he : e ∈ E) {q : List ObjRef} + (hk : σ.kheap (specPtr e) = some (.endpoint (.sendEP (specPtr f :: q)))) + (hf : f ∈ Z.T) {xs : Tcb} (hks : σ.kheap (specPtr f) = some (.tcb xs)) + (hssc : xs.tcbSchedContext ≠ none) + (hc : L.mem s (scA f) ∈ Z.C) (hni : NotIdleScA m (scNameA m (L.mem s (scA f)))) + (hfon : L.mem s (affA f) = L.cpu s) (hmig : L.mem s (affA t) = L.mem s (scCoreA (L.mem s (scA f)))) + (hV : L.mem s (core.num.U64.wrapping_add f 3664#u64) = 0#u64) + (hu : ∀ rp, ipcBufOfA true xt = some rp → ∀ k, k < 128 → inUserFrame (bufAddr rp k) σ) + (hcase : ∀ e' dd, xs.tcbState = .blockedOnSend e' dd → + (xs.tcbFault = none ∧ (dd.senderCanGrant = false ∨ ipcBufOfA false xs = none ∨ + unat (dataToMessageInfo (xs.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0)) ∨ + ∃ g, xs.tcbFault = some g ∧ FaultOKA (L := L) s f (ipcBufOfA true xt) xs g) + {r : U64} (hr : r ∈ R) (hi : ReplyIsolatedA (L.mem s) Z.C stack r) + (hbranch : ∀ e' d, xs.tcbState = .blockedOnSend e' d → + (d.senderIsCall = true ∨ xs.tcbFault ≠ none) ∧ (d.senderCanGrant = true ∨ d.senderCanGrantReply = true)) + (hAct : ∀ u, σ.schedulerAction = .switchThread u → u ≠ specPtr f) (hIdle : specPtr f ≠ σ.idleThread) : + endpoint.receive_from_sender st ar s a t e r ⦃ out => out.2 = a ∧ + ∃ σ', specRecvSendA (specPtr t) (specPtr e) (some (specPtr r)) (specPtr f :: q) σ = ({((), σ')}, False) ∧ + EpStateRel Z.T E R Bf (L.mem out.1) σ' ∧ IpcSchedRelA st Z m κ out.1 σ' ∧ + ReplyStackRelA (L.mem out.1) R Z.C σ' (scNameA m) + (Function.update stack (L.mem s (scA f)) (r :: stack (L.mem s (scA f)))) ∧ + WritesIn st s out.1 (RecvPushFpA st Z m R Bf σ s t e f) ⦄ := by + let O := schedOpsEAOfPlacementA (st := st) (m := m) (κ := κ) P + have F : SchedFootEA st Z.T E R Bf (IpcSchedRelA st Z m κ) (fun σ s => MWrA (st := st) Z m σ s) := + schedFootEA_of_sched + refine receive_from_senderCont2A O PE hPl s a σ HE ⟨Z.Q, Z.R, H⟩ hSt hVld hC hNative hoff ht + ((H.thT t).2 ht) hkt hnb he hk ((H.thT f).2 hf) hks hu r + hcase ?_ + intro s2 σ2 dd _ htf hfe hst E2 hS2 hW hE hk2 hq2 hT hfp + obtain ⟨Q2, R2, H2⟩ := hS2 + obtain ⟨xt2, hkt2, _⟩ := E2.tcb t ht + have htSpec : xt.tcbSchedContext = none := by + rw [schedTcbScA H ht hkt, ht0, optScA, if_pos (show (0#u64 : U64) = 0#u64 from rfl)] + have htSpec2 : xt2.tcbSchedContext = none := (epStepTcbScA hE hkt hkt2).trans htSpec + set sc : U64 := L.mem s (scA f) with hscdef + have hsc0 : sc ≠ 0#u64 := fun h => hssc (by rw [schedTcbScA H hf hks, ← hscdef, h, optScA, if_pos rfl]) + have hsSpec : xs.tcbSchedContext = some (scNameA m sc) := by + rw [schedTcbScA H hf hks, ← hscdef, optScA, if_neg hsc0]; rfl + have hi2 := endpointKeepsIsolatedA H.toSchedSRN P PE hr hi hW + have hon2 : L.mem s2 (affA f) = L.cpu s2 := by rw [H.aff_eq H2 hf hf, H.cpu_eq H2, hfon] + have hmig2 : L.mem s2 (affA t) = L.mem s2 (scCoreA sc) := by + rw [H.aff_eq H2 ht ht, H2.scCore sc hc, ← H.scCore sc hc, hmig] + have hv2 := (endpointKeepsVcpuA (Z := Z) PE hf hW).trans hV + have HS2 := endpointStackFrameA H.toSchedSRN P PE HE HS hW hE + obtain ⟨hcall, hgrant⟩ := hbranch _ dd hst + have hnr : runnable xs.tcbState = false := by rw [hst]; rfl + have hr0 : r ≠ 0#u64 := fun eq => (PE.rep r hr).2.1 (by rw [eq]; rfl) + have htmo : ∀ b, xs.tcbFault ≠ some (.timeout b) := fun b h => by + rcases hcase _ _ hst with ⟨hn, -⟩ | ⟨g, hg, hok⟩ + · rw [hn] at h; cases h + · rw [h] at hg + cases hg + exact hssc (show xs.tcbSchedContext = none ∧ _ from hok).1 + unfold recvSendAfterTransferA + refine WP.spec_bind (WP.spec_and (recvPushSporadicA H2 P PE E2 HS2 hf hk2 hnr) + (endpointPushSporadicA H2 P PE E2 HS2 hf hr hi2 hk2 hnr)) ?_ + rintro s1 ⟨⟨σ1, hA, H1, E1, S1, w1, keep, hact, hidle⟩, σ1', hA', -, -, -, I1, V1, -⟩ + have eσ : σ1' = σ1 := by + have h : ((), σ1') ∈ (ifSporadicCurScAssertRefillUnblockCheck (Z := Unit) xs.tcbSchedContext σ2).1 := by + rw [hA']; exact Set.mem_singleton _ + rw [hA] at h + exact congrArg Prod.snd (Set.mem_singleton_iff.1 h) + subst σ1' + have hk1 := keep (specPtr f) (.tcb xs) hk2 (fun _ _ e' => by cases e') + have ht1 := keep (specPtr t) (.tcb xt2) hkt2 (fun _ _ e' => by cases e') + have hEp1 : ∀ e' ∈ E, ∀ z, σ1.kheap (specPtr e') = some (.endpoint z) → specPtr f ∉ z.epQueue := by + intro e' he' z hz + obtain ⟨x, hx, _⟩ := E2.ep e' he' + have hx' := keep (specPtr e') (.endpoint x) hx (fun _ _ e'' => by cases e'') + have hEq := KernelObject.endpoint.inj (Option.some.inj (hz.symm.trans hx')) + exact hq2 e' he' z (hEq.symm ▸ hx) + have hE1 := E1.at_tcb hf hk1 + have hftT : L.mem s1 (tFaultA f) ≠ fl4_abi.consts.fault.Timeout := fun c => by + obtain ⟨b, hb⟩ := hE1.faultT.1 (c.trans (by unfold fl4_abi.consts.fault.Timeout; rfl)) + exact htmo b hb + have hcf : dd.senderIsCall = true ∨ L.mem s1 (tFaultA f) ≠ fl4_abi.consts.fault.NullFault := + hcall.imp id fun h c => h (hE1.fault0.1 (c.trans (by unfold fl4_abi.consts.fault.NullFault; rfl))) + obtain ⟨hsf1, hst1⟩ := pushAfterBlockScWordsA H1 hf ht hsc0 hni hk1 ht1 hsSpec htSpec2 + have hword : L.mem s1 (scA f) ≠ 0#u64 := fun h => hsc0 (hsf1.symm.trans h) + have act2 : σ2.schedulerAction = σ.schedulerAction := by + have eq := congrArg AbstractState.schedulerAction hE.2.1 + exact eq + have idle2 : σ2.idleThread = σ.idleThread := (SchedKeepA.step hE).2.2.1 + rw [recvSendSelectedDonateA (ar := ar) s1 a t f r dd hr0 hgrant hcf hword hftT] + have hRB : RelIfBound σ1 (specPtr f) := + H1.rd _ _ hk1 (Or.inr (Or.inr (Or.inl ⟨_, _, hst, htmo⟩))) + have hnq := H1.notQueued hk1 hnr + have hfon1 : L.mem s1 (affA f) = L.cpu s1 := by rw [H2.aff_eq H1 hf hf, H2.cpu_eq H1, hon2] + have hmig1 : L.mem s1 (affA t) = L.mem s1 (scCoreA sc) := by + rw [H2.aff_eq H1 ht ht, H1.scCore sc hc, ← H2.scCore sc hc, hmig2] + refine WP.spec_mono (reply_push_donateFpA a H1 P PE E1 S1 (sc := sc) hc hsc0 hr I1 hf ht htf.symm hsf1 hst1 + hfon1 hmig1 (V1.trans hv2) hni hEp1 hnq hRB (fun u hu => hAct u (act2.symm.trans (hact.symm.trans hu))) + (fun h => hIdle (h.trans (hidle.trans idle2))) + (fun σ' hp => relIfBoundSc_pushDonA hk1 hsSpec ht1 htSpec2 hnq.2 hRB hp)) ?_ + rintro out ⟨ha, σ', hD, H', E', S', -, w2⟩ + have hflag : decide (xs.tcbSchedContext ≠ none ∧ ¬ (xs.tcbFault.elim false isTimeoutFault = true)) = true := + decide_eq_true ⟨hssc, by + cases h : xs.tcbFault with + | none => exact Bool.false_ne_true + | some g => + cases g + case timeout b => exact absurd h (htmo b) + all_goals exact Bool.false_ne_true⟩ + have hspec : specRecvSendA (specPtr t) (specPtr e) (some (specPtr r)) (specPtr f :: q) σ = ({((), σ')}, False) := by + rw [recvSend_pushSpecA hk hks hfe hst hT hk2 hA hk1 hcall hgrant, hflag] + exact hD + refine ⟨ha, σ', hspec, E', H', S', ?_⟩ + have hz2 : SSzA σ σ2 := ssz_of_epStep hE + have hz1 : SSzA σ σ1 := hz2.trans (F.unblockSz _ _ _ hA) + have w01 : WritesIn st s s2 (RecvPushFpA st Z m R Bf σ s t e f) := hfp.mono fun _ h => Or.inl (Or.inl h) + have w12 : WritesIn st s2 s1 (RecvPushFpA st Z m R Bf σ s t e f) := + w1.mono fun _ h => Or.inl (Or.inr (Or.inr (F.back hz2 hfp.1 h))) + have ks1 : L.ksb s1 = L.ksb s := w1.1.trans hfp.1 + have w23 : WritesIn st s1 out.1 (RecvPushFpA st Z m R Bf σ s t e f) := w2.mono fun b h => by + rcases h with h | h | h | h + · exact Or.inr (Or.inl h) + · exact Or.inl (Or.inr (Or.inl h)) + · exact Or.inr (Or.inr h) + · exact Or.inl (Or.inr (Or.inr (F.back hz1 ks1 h))) + exact (WritesIn.trans (st := st) (WritesIn.trans (st := st) w01 w12) w23).mono fun _ h => + h.elim (fun h => h.elim id id) id + + +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/Stage3RecvPushFpA.lean b/proofs/Fl4ProofsA64/P3/Stage3RecvPushFpA.lean new file mode 100644 index 000000000..8aeae39c5 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3RecvPushFpA.lean @@ -0,0 +1,176 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2c): **`receive_ipc` from a waiting call sender with a reply object, the receiver +having its own SC** (the reply is pushed, nothing is donated), with the narrow write set. + +* `recvPushSporadicA`: the sender's sporadic check, keeping the reply stacks, with its scheduler write set + (`endpointPushSporadicA` without the isolated-reply selector). +* **`receive_from_sender_pushNoDon_fpA`**: FL4's `receive_from_sender` runs the spec's `receive_ipc` `SendEP` arm + (one outcome) and writes only `RecvPushFpA`: the dequeue's and the transfer's words, the sender's state and reply + words, reply objects' words and scheduler words. + +[S] the sender has no fault and no extra caps, and holds an SC; the receiver holds an SC; call with grant or +grant-reply. No named premise. New module. +-/ +import Fl4ProofsA64.P3.IpcScRecvPushA +import Fl4ProofsA64.P3.Stage3RecvSendFpA +import Fl4ProofsA64.P3.IpcScFootA + +open Aeneas Aeneas.Std Result +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4SpecA64 Sched +noncomputable section + +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + +variable (st Z m R Bf) in +/-- The write set of the pushing receive (module docstring). -/ +def RecvPushFpA (σ : State Unit) (s : S) (t e f b : U64) : Prop := + RecvSFpA st Z.T Bf (fun σ s => MWrA (st := st) Z m σ s) σ s t e f b ∨ + (∃ q ∈ R, q.val ≤ b.val ∧ b.val < q.val + 32) ∨ b = tReplyA f + +/-- The sender's sporadic check before `reply_push`. -/ +theorem recvPushSporadicA {s : S} {σ : State Unit} {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) + (PE : EPlaced Z.T E R Bf) (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {f : U64} (hf : f ∈ Z.T) {x : Tcb} (hk : σ.kheap (specPtr f) = some (.tcb x)) + (hnr : runnable x.tcbState = false) : + endpoint.unblock_sporadic st s f ⦃ s' => ∃ σ', + ifSporadicCurScAssertRefillUnblockCheck (Z := Unit) x.tcbSchedContext σ = ({((), σ')}, False) ∧ + SchedSRA st Z m κ s' σ' ∧ EpStateRel Z.T E R Bf (L.mem s') σ' ∧ + ReplyStackRelA (L.mem s') R Z.C σ' (scNameA m) stack ∧ + WritesIn st s s' (fun b => MWrA (st := st) Z m σ s b) ∧ + (∀ q o, σ.kheap q = some o → (∀ y n, o ≠ .schedContext y n) → σ'.kheap q = some o) ∧ + σ'.schedulerAction = σ.schedulerAction ∧ σ'.idleThread = σ.idleThread ⦄ := by + obtain ⟨x', hk', hSC⟩ := H.sch.tcb f hf + have hSCeq := congrArg Tcb.tcbSchedContext (KernelObject.tcb.inj (Option.some.inj (hk.symm.trans hk'))) + have hsct := hSCeq.trans hSC + rw [endpoint_unblock_sporadic_eqA] + refine WP.spec_mono (unblock_sporadic_SRA H hf true (fun h => absurd h (H.notQueued hk hnr).2)) ?_ + rintro s' ⟨σ', eq, H', F⟩ + have hA : ifSporadicCurScAssertRefillUnblockCheck (Z := Unit) x.tcbSchedContext σ = ({((), σ')}, False) := by + rw [hsct] + exact eq + have hres : ((), σ') ∈ (ifSporadicCurScAssertRefillUnblockCheck (Z := Unit) x.tcbSchedContext σ).1 := by + rw [hA] + exact Set.mem_singleton _ + have preserve := condUnblockReplyListsA true (fun p hp => by + obtain ⟨y, n, hy, _⟩ := H.inv.back (specPtr f) x p hk hp + exact ⟨y, n, hy⟩) hres + have fm := F.mfr H.toSchedSRN + obtain ⟨FE, VIEW⟩ := epFrameOfMcsA H.toSchedSRN P fm preserve.mem + exact ⟨σ', hA, H', HE.frame PE FE VIEW, mcsReplyFrameA H.toSchedSRN P PE HE HS fm preserve.sc, fm.wr, + F.obj, F.act, preserve.idle⟩ + +set_option maxHeartbeats 400000 in +/-- **`receive_from_sender` from a waiting call sender, pushing the reply, the receiver holding an SC** (module +docstring). -/ +theorem receive_from_sender_pushNoDon_fpA {s : S} {σ : State Unit} (a : A) {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {kb : U64} (hPl : PlaceA Z.th kb) (hSt : StateRelA Z.th (L.mem s) σ) (hVld : SlotsValidA Z.th (L.mem s)) + (hC : BufCover st Z.T Bf s σ) (hNative : BufNativeA Z.T (L.mem s)) + (hoff : ∀ y, IsSlotA Z.th y → ¬ EWrite Z.T E R Bf y ∧ + ¬ EWrite Z.T E R Bf (core.num.U64.wrapping_add y 8#u64)) + {t : U64} (ht : t ∈ Z.T) {xt : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hnb : tsCode xt.tcbState ≠ 4) (htsc : xt.tcbSchedContext ≠ none) + {e f : U64} (he : e ∈ E) {q : List ObjRef} + (hk : σ.kheap (specPtr e) = some (.endpoint (.sendEP (specPtr f :: q)))) + (hf : f ∈ Z.T) {xs : Tcb} (hks : σ.kheap (specPtr f) = some (.tcb xs)) + (hssc : xs.tcbSchedContext ≠ none) + (hu : ∀ rp, ipcBufOfA true xt = some rp → ∀ k, k < 128 → inUserFrame (bufAddr rp k) σ) + (hcase : ∀ e' dd, xs.tcbState = .blockedOnSend e' dd → + (xs.tcbFault = none ∧ (dd.senderCanGrant = false ∨ ipcBufOfA false xs = none ∨ + unat (dataToMessageInfo (xs.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0)) ∨ + ∃ g, xs.tcbFault = some g ∧ FaultOKA (L := L) s f (ipcBufOfA true xt) xs g) + {r : U64} (hr : r ∈ R) + (hbranch : ∀ e' d, xs.tcbState = .blockedOnSend e' d → + (d.senderIsCall = true ∨ xs.tcbFault ≠ none) ∧ (d.senderCanGrant = true ∨ d.senderCanGrantReply = true)) + (hAct : ∀ u, σ.schedulerAction = .switchThread u → u ≠ specPtr f) (hIdle : specPtr f ≠ σ.idleThread) : + endpoint.receive_from_sender st ar s a t e r ⦃ out => out.2 = a ∧ + ∃ σ', specRecvSendA (specPtr t) (specPtr e) (some (specPtr r)) (specPtr f :: q) σ = ({((), σ')}, False) ∧ + EpStateRel Z.T E R Bf (L.mem out.1) σ' ∧ IpcSchedRelA st Z m κ out.1 σ' ∧ + ReplyStackRelA (L.mem out.1) R Z.C σ' (scNameA m) stack ∧ + WritesIn st s out.1 (RecvPushFpA st Z m R Bf σ s t e f) ⦄ := by + let O := schedOpsEAOfPlacementA (st := st) (m := m) (κ := κ) P + have F : SchedFootEA st Z.T E R Bf (IpcSchedRelA st Z m κ) (fun σ s => MWrA (st := st) Z m σ s) := + schedFootEA_of_sched + refine receive_from_senderCont2A O PE hPl s a σ HE ⟨Z.Q, Z.R, H⟩ hSt hVld hC hNative hoff ht + ((H.thT t).2 ht) hkt hnb he hk ((H.thT f).2 hf) hks hu r + hcase ?_ + intro s2 σ2 dd _ htf hfe hst E2 hS2 hW hE hk2 hq2 hT hfp + obtain ⟨Q2, R2, H2⟩ := hS2 + obtain ⟨xt2, hkt2, _⟩ := E2.tcb t ht + have htsc2 : xt2.tcbSchedContext ≠ none := by rw [epStepTcbScA hE hkt hkt2]; exact htsc + have HS2 := endpointStackFrameA H.toSchedSRN P PE HE HS hW hE + obtain ⟨hcall, hgrant⟩ := hbranch _ dd hst + have hnr : runnable xs.tcbState = false := by rw [hst]; rfl + have hr0 : r ≠ 0#u64 := fun eq => (PE.rep r hr).2.1 (by rw [eq]; rfl) + have htmo : ∀ b, xs.tcbFault ≠ some (.timeout b) := fun b h => by + rcases hcase _ _ hst with ⟨hn, -⟩ | ⟨g, hg, hok⟩ + · rw [hn] at h; cases h + · rw [h] at hg + cases hg + exact hssc (show xs.tcbSchedContext = none ∧ _ from hok).1 + unfold recvSendAfterTransferA + refine WP.spec_bind (recvPushSporadicA H2 P PE E2 HS2 hf hk2 hnr) ?_ + rintro s1 ⟨σ1, hA, H1, E1, S1, w1, keep, hact, hidle⟩ + have hk1 := keep (specPtr f) (.tcb xs) hk2 (fun _ _ e' => by cases e') + have ht1 := keep (specPtr t) (.tcb xt2) hkt2 (fun _ _ e' => by cases e') + have hEp1 : ∀ e' ∈ E, ∀ z, σ1.kheap (specPtr e') = some (.endpoint z) → specPtr f ∉ z.epQueue := by + intro e' he' z hz + obtain ⟨x, hx, _⟩ := E2.ep e' he' + have hx' := keep (specPtr e') (.endpoint x) hx (fun _ _ e'' => by cases e'') + have hEq := KernelObject.endpoint.inj (Option.some.inj (hz.symm.trans hx')) + exact hq2 e' he' z (hEq.symm ▸ hx) + have hE1 := E1.at_tcb hf hk1 + have hftT : L.mem s1 (tFaultA f) ≠ fl4_abi.consts.fault.Timeout := fun c => by + obtain ⟨b, hb⟩ := hE1.faultT.1 (c.trans (by unfold fl4_abi.consts.fault.Timeout; rfl)) + exact htmo b hb + have hcf : dd.senderIsCall = true ∨ L.mem s1 (tFaultA f) ≠ fl4_abi.consts.fault.NullFault := + hcall.imp id fun h c => h (hE1.fault0.1 (c.trans (by unfold fl4_abi.consts.fault.NullFault; rfl))) + have hword : L.mem s1 (scA f) ≠ 0#u64 := fun h0 => by + have h := schedTcbScA H1 hf hk1 + rw [h0, optScA, if_pos rfl] at h + exact hssc h + have act2 : σ2.schedulerAction = σ.schedulerAction := by + have eq := congrArg AbstractState.schedulerAction hE.2.1 + exact eq + have idle2 : σ2.idleThread = σ.idleThread := (SchedKeepA.step hE).2.2.1 + rw [recvSendSelectedDonateA (ar := ar) s1 a t f r dd hr0 hgrant hcf hword hftT] + have hRB : RelIfBound σ1 (specPtr f) := + H1.rd _ _ hk1 (Or.inr (Or.inr (Or.inl ⟨_, _, hst, htmo⟩))) + refine WP.spec_mono (reply_push_calleeScFpA a H1 P PE E1 S1 (sc := L.mem s1 (scA f)) hword hr hf ht htf.symm rfl + ht1 htsc2 hEp1 (H1.notQueued hk1 hnr) hRB (fun u hu => hAct u (act2.symm.trans (hact.symm.trans hu))) + (fun h => hIdle (h.trans (hidle.trans idle2)))) ?_ + rintro out ⟨ha, σ', hD, H', E', S', -, w2⟩ + have hflag : decide (xs.tcbSchedContext ≠ none ∧ ¬ (xs.tcbFault.elim false isTimeoutFault = true)) = true := + decide_eq_true ⟨hssc, by + cases h : xs.tcbFault with + | none => exact Bool.false_ne_true + | some g => + cases g + case timeout b => exact absurd h (htmo b) + all_goals exact Bool.false_ne_true⟩ + have hspec : specRecvSendA (specPtr t) (specPtr e) (some (specPtr r)) (specPtr f :: q) σ = ({((), σ')}, False) := by + rw [recvSend_pushSpecA hk hks hfe hst hT hk2 hA hk1 hcall hgrant, hflag] + exact hD + refine ⟨ha, σ', hspec, E', H', S', ?_⟩ + have hz2 : SSzA σ σ2 := ssz_of_epStep hE + have hz1 : SSzA σ σ1 := hz2.trans (F.unblockSz _ _ _ hA) + have w01 : WritesIn st s s2 (RecvPushFpA st Z m R Bf σ s t e f) := hfp.mono fun _ h => Or.inl (Or.inl h) + have w12 : WritesIn st s2 s1 (RecvPushFpA st Z m R Bf σ s t e f) := + w1.mono fun _ h => Or.inl (Or.inr (Or.inr (F.back hz2 hfp.1 h))) + have ks1 : L.ksb s1 = L.ksb s := w1.1.trans hfp.1 + have w23 : WritesIn st s1 out.1 (RecvPushFpA st Z m R Bf σ s t e f) := w2.mono fun b h => by + rcases h with h | h | h | h + · exact Or.inr (Or.inl h) + · exact Or.inl (Or.inr (Or.inl h)) + · exact Or.inr (Or.inr h) + · exact Or.inl (Or.inr (Or.inr (F.back hz1 ks1 h))) + exact (WritesIn.trans (st := st) (WritesIn.trans (st := st) w01 w12) w23).mono fun _ h => + h.elim (fun h => h.elim id id) id + +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/Stage3RecvReplyPinA.lean b/proofs/Fl4ProofsA64/P3/Stage3RecvReplyPinA.lean new file mode 100644 index 000000000..8069ffa76 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3RecvReplyPinA.lean @@ -0,0 +1,51 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2c): **the reply argument of a receive** (`RecvReplyPinA`) as the data every receive +arm needs: the spec's reply argument (`ReplyArgA`), FL4's (`ReplyCapA`), the unlinked-reply fact for `take_reply`, and +the reply object's membership. Shared by the reply-case arms (`recvSendA`, `recvBlockRA`). + +* **`RecvReplyPinA.data`**: from `RecvReplyPinA` and the register value `rv` (0, or the reply named by the cap). + +[P] none. New module. +-/ +import Fl4ProofsA64.P3.Stage3RecvPollA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4Spec Fl4SpecA64 + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {s : S} {t : U64} {Rp : Set U64} + +/-- **The reply data of a receive** (module docstring). -/ +theorem RecvReplyPinA.data {canReply : Bool} {rc : capability.Cap} {rv : U64} + (hrp : RecvReplyPinA (st := st) s t Rp canReply rc) + (hrv : (rc = .Null ∧ rv = 0#u64) ∨ ∃ g, rc = .Reply rv g) : + ReplyArgA Rp (L.mem s) rc (absCapA rc) rv ∧ ReplyCapA Rp rc (absCapA rc) rv ∧ + (∀ b, rc = .Reply rv b → L.mem s (Endp.rTcbA rv) = 0#u64 ∨ L.mem s (Endp.rTcbA rv) = t) ∧ + (∀ r g, rc = .Reply r g → r ∈ Rp ∧ L.mem s (Endp.rTcbA r) = 0#u64) := by + rcases hrp with ⟨-, rfl⟩ | ⟨-, r0, g, rfl, hr, h0, -⟩ + · obtain ⟨-, rfl⟩ : (capability.Cap.Null = .Null ∧ rv = 0#u64) := by + rcases hrv with h | ⟨g, h⟩ + · exact h + · cases h + refine ⟨Or.inl ⟨rfl, rfl, rfl⟩, Or.inl ⟨rfl, rfl, rfl⟩, ?_, ?_⟩ + · intro b h; cases h + · intro r g h; cases h + · have hrv' : rv = r0 := by + rcases hrv with h | ⟨g', h⟩ + · cases h.1 + · exact (capability.Cap.Reply.inj h).1.symm + subst hrv' + exact ⟨Or.inr ⟨g, _, rfl, rfl, hr, h0⟩, Or.inr ⟨g, _, rfl, rfl, hr⟩, fun _ _ => Or.inl h0, + fun r g' h => by + obtain ⟨e, -⟩ := capability.Cap.Reply.inj h + subst e + exact ⟨hr, h0⟩⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3RecvScopeA.lean b/proofs/Fl4ProofsA64/P3/Stage3RecvScopeA.lean new file mode 100644 index 000000000..dd23274b0 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3RecvScopeA.lean @@ -0,0 +1,420 @@ +/- +P3 (aarch64, A64-P3 Stage 3, line recvglue): **the receive scope and the standalone receive bodies** (Recv, NBRecv, +Wait, NBWait), the analogue of `DispScopeA` / `sendBodyScopeA` for `handle_recv`. + +* **`RecvScopeA st OS N E Rp T s σ blocking canReply`**: per `handle_recv` call, one constructor per existing arm family + (the audit's §2 coverage table), each carrying the arm's [S] selectors as data. It is indexed only by the + `handle_recv` pre-state `(s, σ)` (so a combined syscall can instantiate it at `(s3, σ3)` after a first half) and by + `(blocking, canReply)`. A state that no constructor describes is outside the scope: that is the declared scope limit. +* **`recvDispatchA`**: from `RecvPreA` and a `RecvScopeA` member, `handle_recv ⦃ StepPostA (A64RelS3 ∧ BodyOKA) (handleRecv ..) ⦄`. + Every arm exports `HRecvPostA` for the *whole* `handle_recv` (FL4 outcome, spec outcome, record, `BodyOKA`), so the + dispatch is the arm followed by `HRecvPostA → StepPostA`; `handle_recv_refinesA`'s `RecvBranchesA` leaves are + `receive_ipc` / `receive_signal` level and no arm exports one, so that theorem is not needed for these members. +* **`recvBody_of_scopeA`**: `dispatch_syscall` for `call` ∈ {Recv, NBRecv, Wait, NBWait}: `DispatchBranchesA.recv` + filled from a per-`(blocking, canReply)` receive body, `inv` / `done` / `preempt` / `combined` / `yield` absurd. + `JSyscallA.recvBodyScopeA` is it with `recvDispatchA` and `JSyscallA.toRecvPreA`. +* `recvSettle_eqA`: `fl4SettleDispatch pending (fl4DispatchSyscall _ call)` is the front's `NonDetE.catch (fl4HandleSyscall ..)`. + +**Declared [S] scope limits** (states with no constructor, or excluded by one selector of a constructor): +- E1 `canReply = true` with an active bound notification: `boundActive` / `boundActiveDon` are `canReply = false` + (the constructor index). +- E2 [S] `hbn` of `pushDon`: a blocking receive whose bound notification has the receiver's own SC (`BndUnbA`) + against a waiting sender that donates its SC has no arm; with a reply object and a call / faulted sender it is + `pushUnb` (`recvPushUnbA`), without a reply push it is `sendUnb` (`recvSendUnbA`). +- E3 [S] `hsnd` of `send` / `push` / `pushDon`: the sender transfers no extra caps. +- E4 [S] `hV` of `pushDon`: the sender has no bound VCPU. +- E6 / N2: a found cap that is not a receivable endpoint / notification, an endpoint without the receive right, a + notification without it or with a bound TCB other than the receiver, a failing REPLY lookup: no constructor. +- N1 [S] `hnsc` of `waitBlock` / `hsn` of `waitBlockSc`: a blocking wait on a notification with an SC that is not + the waiter's has no constructor. +- cap-fault handler endpoint: [S] `hnull` of `capFaultNull` (the handler slot is `Null`). +- `ntfnSc = curSc` gap: [S] `hne` of `boundActiveDon` / `waitActiveDon`. + +Named premises: [P] `ArchTotal`, `ArchPlat`, `hCs`, `hmb`, `hmax`, `htu`; [S] `numNodes = 1` (`h1`), the selectors above. +No new named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3RecvPassiveDonA +import Fl4ProofsA64.P3.Stage3RecvCapFaultA +import Fl4ProofsA64.P3.Stage3RecvBlockAnyA +import Fl4ProofsA64.P3.Stage3RecvBlockRUnbA +import Fl4ProofsA64.P3.Stage3RecvPushDonA +import Fl4ProofsA64.P3.Stage3RecvWaitScA +import Fl4ProofsA64.P3.Stage3RecvPollA +import Fl4ProofsA64.P3.Stage3RecvActiveA +import Fl4ProofsA64.P3.Stage3RecvSendA +import Fl4ProofsA64.P3.Stage3RecvPushA +import Fl4ProofsA64.P3.Stage3RecvUnbArmA +import Fl4ProofsA64.P3.Stage3RecvWaitA +import Fl4ProofsA64.P3.Stage3ScopeA +import Fl4ProofsA64.P3.Stage3RecvPushCancelA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4Spec Fl4SpecA64 + +noncomputable section + +section +variable {S : Type} (st : machine.Store S) [L : LawfulStore st] + +/-- [S] **The endpoint cap selected by `handle_recv`**: the lookup result, its slot and the spec endpoint. -/ +structure RecvEpSelA (OS E : Set U64) (s : S) (σ : State Unit) (p badge slot : U64) (send grant grantReply : Bool) + (x : Endpoint) : Prop where + hcap : cspace.lookup_cap st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.CAP)) = + ok (.Ok (.Endpoint p badge send true grant grantReply)) + hslot : slot ∈ OS + hcov : SlotCov st OS s slot + hsc : capA st s slot = .Endpoint p badge send true grant grantReply + hp : p ∈ E + hk : σ.kheap (specPtr p) = some (.endpoint x) + +/-- [S] **The notification cap selected by `handle_recv`**. -/ +structure RecvNtfSelA (OS N : Set U64) (s : S) (σ : State Unit) (p badge slot : U64) (send : Bool) + (x : Notification) : Prop where + hcap : cspace.lookup_cap st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.CAP)) = + ok (.Ok (.Notification p badge send true)) + hslot : slot ∈ OS + hcov : SlotCov st OS s slot + hsc : capA st s slot = .Notification p badge send true + hp : p ∈ N + hk : σ.kheap (specPtr p) = some (.notification x) + +/-- **The REPLY cap selected by `handle_recv`** (`can_reply`): a Reply cap (its object is unlinked, or linked to a +receive-blocked or reply-blocked thread: `take_reply` cancels it). -/ +structure RecvRepA (OS Rp : Set U64) (s : S) (rr rslot : U64) (rg : Bool) : Prop where + hcapR : cspace.lookup_cap st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.REPLY)) = ok (.Ok (.Reply rr rg)) + hrslot : rslot ∈ OS + hrsc : capA st s rslot = .Reply rr rg + hrr : rr ∈ Rp + +/-- **The `handle_recv` scope**, per call (module docstring): one constructor per arm family. -/ +inductive RecvScopeA (OS N E Rp T : Set U64) (s : S) (σ : State Unit) : Bool → Bool → Prop where + /-- Blocking receive without a reply object on an endpoint with no sender: the bound notification is absent or + inactive (with or without the receiver's own SC: `recvBlockAnyA`, over `recvBlockA` / `recvBlockUnbA`). -/ + | blockAny {p badge slot : U64} {send grant grantReply : Bool} {x : Endpoint} + (e : RecvEpSelA st OS E s σ p badge slot send grant grantReply x) (hcb : CanBlockA true x) + (hbn : ∀ y, σ.kheap σ.curThread = some (.tcb y) → BndOKA N (L.mem s) σ (L.curW s) y false) : + RecvScopeA OS N E Rp T s σ true false + /-- The same with a reply object (`recvBlockRAnyA`). -/ + | blockRAny {p badge slot rr rslot : U64} {send grant grantReply rg : Bool} {x : Endpoint} + (e : RecvEpSelA st OS E s σ p badge slot send grant grantReply x) (r : RecvRepA st OS Rp s rr rslot rg) + (hcb : CanBlockA true x) + (hbn : ∀ y, σ.kheap σ.curThread = some (.tcb y) → BndOKA N (L.mem s) σ (L.curW s) y false) : + RecvScopeA OS N E Rp T s σ true true + /-- Non-blocking receive on an endpoint with no sender (`nbRecvIdleA`). -/ + | nbRecvIdle {canReply : Bool} {p badge : U64} {send grant grantReply : Bool} {rc : capability.Cap} {x : Endpoint} + (hcap : cspace.lookup_cap st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.CAP)) = + ok (.Ok (.Endpoint p badge send true grant grantReply))) + (hrp : RecvReplyPinA (st := st) s (L.curW s) Rp canReply rc) (hp : p ∈ E) + (hk : σ.kheap (specPtr p) = some (.endpoint x)) (hcb : CanBlockA true x) + (hbn : ∀ y, σ.kheap σ.curThread = some (.tcb y) → BndOKA N (L.mem s) σ (L.curW s) y false) : + RecvScopeA OS N E Rp T s σ false canReply + /-- Non-blocking wait on a notification that is not active (`nbWaitIdleA`). -/ + | nbWaitIdle {canReply : Bool} {p badge : U64} {send : Bool} {x : Notification} + (hcap : cspace.lookup_cap st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.CAP)) = + ok (.Ok (.Notification p badge send true))) + (hp : p ∈ N) (hk : σ.kheap (specPtr p) = some (.notification x)) (hna : ∀ b, x.ntfnObj ≠ .activeNtfn b) + (hbt : x.ntfnBoundTcb = some σ.curThread ∨ x.ntfnBoundTcb = none) : + RecvScopeA OS N E Rp T s σ false canReply + /-- Endpoint receive with an active bound notification and `can_reply = false`, the receiver has an SC + (`recvBoundActiveA`). E1 is the index `canReply = false`. -/ + | boundActive {blk : Bool} {pe badge slot p : U64} {send grant grantReply : Bool} {xe : Endpoint} {x : Notification} + {b0 : Badge} (e : RecvEpSelA st OS E s σ pe badge slot send grant grantReply xe) (hp : p ∈ N) + (hk : σ.kheap (specPtr p) = some (.notification x)) (hact : x.ntfnObj = .activeNtfn b0) + (hbn : ∀ y, σ.kheap σ.curThread = some (.tcb y) → y.tcbBoundNotification = some (specPtr p)) + (hown : (∀ x, σ.kheap σ.curThread = some (.tcb x) → x.tcbSchedContext = some σ.curSc) ∨ + (x.ntfnSc = none ∧ ∀ y, σ.kheap σ.curThread = some (.tcb y) → y.tcbSchedContext = none)) : + RecvScopeA OS N E Rp T s σ blk false + /-- The same for a passive receiver (`recvBoundActiveDon_oneNodeA`). [S] `hne`: the `ntfnSc = curSc` gap. -/ + | boundActiveDon {blk : Bool} {pe badge slot p : U64} {send grant grantReply : Bool} {xe : Endpoint} + {x : Notification} {b0 : Badge} (e : RecvEpSelA st OS E s σ pe badge slot send grant grantReply xe) + (hp : p ∈ N) (hk : σ.kheap (specPtr p) = some (.notification x)) (hact : x.ntfnObj = .activeNtfn b0) + (hbn : ∀ y, σ.kheap σ.curThread = some (.tcb y) → y.tcbBoundNotification = some (specPtr p)) + (hpas : ∀ y, σ.kheap σ.curThread = some (.tcb y) → y.tcbSchedContext = none) + (hne : x.ntfnSc ≠ some σ.curSc) : + RecvScopeA OS N E Rp T s σ blk false + /-- A waiting sender, no reply push (`recvSendA`). [S] `hsnd` (E3), `hbn` (E2). -/ + | send {blk canReply : Bool} {p badge slot rv : U64} {send grant grantReply : Bool} {rc : capability.Cap} + {sd : U64} {q : List ObjRef} + (e : RecvEpSelA st OS E s σ p badge slot send grant grantReply (.sendEP (specPtr sd :: q))) + (hrp : RecvReplyPinA (st := st) s (L.curW s) Rp canReply rc) + (hrv : (rc = .Null ∧ rv = 0#u64) ∨ ∃ g, rc = .Reply rv g) + (hrcap : ∀ r g, rc = .Reply r g → ∃ rslot ∈ OS, capA st s rslot = .Reply r g) + (hbn : ∀ y, σ.kheap σ.curThread = some (.tcb y) → BndOKA N (L.mem s) σ (L.curW s) y blk) + (hsnd : ∀ y xt, σ.kheap (specPtr sd) = some (.tcb y) → σ.kheap σ.curThread = some (.tcb xt) → + (y.tcbFault = none ∧ ∀ e' dd, y.tcbState = .blockedOnSend e' dd → dd.senderCanGrant = false ∨ + ipcBufOfA false y = none ∨ + unat (dataToMessageInfo (y.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0) ∨ + ∃ f, y.tcbFault = some f ∧ ((∀ b, f ≠ .timeout b) → Endp.FaultOKA (L := L) s sd (ipcBufOfA true xt) y f)) + (hng : rv ≠ 0#u64 → ∀ y, σ.kheap (specPtr sd) = some (.tcb y) → ∀ e' dd, y.tcbState = .blockedOnSend e' dd → + (dd.senderIsCall = true ∨ y.tcbFault ≠ none) → dd.senderCanGrant = false ∧ dd.senderCanGrantReply = false) : + RecvScopeA OS N E Rp T s σ blk canReply + /-- A waiting sender, no reply push, the receiver's bound notification holds its own SC (`BndUnbA`): + `maybe_return_sc`, then the send arm (`recvSendUnbA`). [S] `hsnd` (E3). -/ + | sendUnb {canReply : Bool} {p badge slot rv : U64} {send grant grantReply : Bool} {rc : capability.Cap} + {sd : U64} {q : List ObjRef} + (e : RecvEpSelA st OS E s σ p badge slot send grant grantReply (.sendEP (specPtr sd :: q))) + (hrp : RecvReplyPinA (st := st) s (L.curW s) Rp canReply rc) + (hrv : (rc = .Null ∧ rv = 0#u64) ∨ ∃ g, rc = .Reply rv g) + (hrcap : ∀ r g, rc = .Reply r g → ∃ rslot ∈ OS, capA st s rslot = .Reply r g) + (hbn : ∀ y, σ.kheap σ.curThread = some (.tcb y) → BndUnbA N σ y) + (hsnd : ∀ y xt, σ.kheap (specPtr sd) = some (.tcb y) → σ.kheap σ.curThread = some (.tcb xt) → + (y.tcbFault = none ∧ ∀ e' dd, y.tcbState = .blockedOnSend e' dd → dd.senderCanGrant = false ∨ + ipcBufOfA false y = none ∨ + unat (dataToMessageInfo (y.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0) ∨ + ∃ f, y.tcbFault = some f ∧ ((∀ b, f ≠ .timeout b) → Endp.FaultOKA (L := L) s sd (ipcBufOfA true xt) y f)) + (hng : rv ≠ 0#u64 → ∀ y, σ.kheap (specPtr sd) = some (.tcb y) → ∀ e' dd, y.tcbState = .blockedOnSend e' dd → + (dd.senderIsCall = true ∨ y.tcbFault ≠ none) → dd.senderCanGrant = false ∧ dd.senderCanGrantReply = false) : + RecvScopeA OS N E Rp T s σ true canReply + /-- A waiting call / faulted sender with a reply object, the receiver has its own SC (`recvPushA`). -/ + | push {blk : Bool} {p badge slot rr rslot : U64} {send grant grantReply rg : Bool} {sd : U64} {q : List ObjRef} + (e : RecvEpSelA st OS E s σ p badge slot send grant grantReply (.sendEP (specPtr sd :: q))) + (r : RecvRepA st OS Rp s rr rslot rg) + (hbn : ∀ y, σ.kheap σ.curThread = some (.tcb y) → BndOKA N (L.mem s) σ (L.curW s) y blk) + (hsnd : ∀ y xt, σ.kheap (specPtr sd) = some (.tcb y) → σ.kheap σ.curThread = some (.tcb xt) → + y.tcbSchedContext ≠ none ∧ ∀ e' dd, + y.tcbState = .blockedOnSend e' dd → (dd.senderIsCall = true ∨ y.tcbFault ≠ none) ∧ + (dd.senderCanGrant = true ∨ dd.senderCanGrantReply = true)) + (hsndF : ∀ y xt, σ.kheap (specPtr sd) = some (.tcb y) → σ.kheap σ.curThread = some (.tcb xt) → + (y.tcbFault = none ∧ ∀ e' dd, y.tcbState = .blockedOnSend e' dd → dd.senderCanGrant = false ∨ + ipcBufOfA false y = none ∨ + unat (dataToMessageInfo (y.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0) ∨ + (∃ f, y.tcbFault = some f ∧ Endp.FaultOKA (L := L) s sd (ipcBufOfA true xt) y f) ∨ + ∃ b, y.tcbFault = some (.timeout b)) + (htsc0 : ∀ x y, σ.kheap σ.curThread = some (.tcb x) → σ.kheap (specPtr sd) = some (.tcb y) → + ¬ (y.tcbFault.elim false isTimeoutFault = true) → x.tcbSchedContext ≠ none) : + RecvScopeA OS N E Rp T s σ blk true + /-- The same for a passive receiver whose sender has an SC (`recvPushDonA`). [S] `hV` (E4). -/ + | pushDon {blk : Bool} {p badge slot rr rslot : U64} {send grant grantReply rg : Bool} {sd : U64} {q : List ObjRef} + (e : RecvEpSelA st OS E s σ p badge slot send grant grantReply (.sendEP (specPtr sd :: q))) + (r : RecvRepA st OS Rp s rr rslot rg) + (hbn : ∀ y, σ.kheap σ.curThread = some (.tcb y) → BndOKA N (L.mem s) σ (L.curW s) y blk) + (hsnd : ∀ y xt, σ.kheap (specPtr sd) = some (.tcb y) → σ.kheap σ.curThread = some (.tcb xt) → + y.tcbSchedContext ≠ none ∧ ∀ e' dd, + y.tcbState = .blockedOnSend e' dd → (dd.senderIsCall = true ∨ y.tcbFault ≠ none) ∧ + (dd.senderCanGrant = true ∨ dd.senderCanGrantReply = true)) + (hsndF : ∀ y xt, σ.kheap (specPtr sd) = some (.tcb y) → σ.kheap σ.curThread = some (.tcb xt) → + (y.tcbFault = none ∧ ∀ e' dd, y.tcbState = .blockedOnSend e' dd → dd.senderCanGrant = false ∨ + ipcBufOfA false y = none ∨ + unat (dataToMessageInfo (y.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0) ∨ + ∃ f, y.tcbFault = some f ∧ Endp.FaultOKA (L := L) s sd (ipcBufOfA true xt) y f) + (hpas : ∀ y, σ.kheap σ.curThread = some (.tcb y) → y.tcbSchedContext = none) + (hV : L.mem s (core.num.U64.wrapping_add sd 3664#u64) = 0#u64) : + RecvScopeA OS N E Rp T s σ blk true + /-- A waiting call / faulted sender with a reply object, the receiver's bound notification holds its own SC + (`BndUnbA`): `maybe_return_sc`, then the timeout push or the donating push (`recvPushUnbA`). [S] `hV` (E4). -/ + | pushUnb {p badge slot rr rslot : U64} {send grant grantReply rg : Bool} {sd : U64} {q : List ObjRef} + (e : RecvEpSelA st OS E s σ p badge slot send grant grantReply (.sendEP (specPtr sd :: q))) + (r : RecvRepA st OS Rp s rr rslot rg) + (hbn : ∀ y, σ.kheap σ.curThread = some (.tcb y) → BndUnbA N σ y) + (hsnd : ∀ y xt, σ.kheap (specPtr sd) = some (.tcb y) → σ.kheap σ.curThread = some (.tcb xt) → + y.tcbSchedContext ≠ none ∧ ∀ e' dd, + y.tcbState = .blockedOnSend e' dd → (dd.senderIsCall = true ∨ y.tcbFault ≠ none) ∧ + (dd.senderCanGrant = true ∨ dd.senderCanGrantReply = true)) + (hsndF : ∀ y xt, σ.kheap (specPtr sd) = some (.tcb y) → σ.kheap σ.curThread = some (.tcb xt) → + (y.tcbFault = none ∧ ∀ e' dd, y.tcbState = .blockedOnSend e' dd → dd.senderCanGrant = false ∨ + ipcBufOfA false y = none ∨ + unat (dataToMessageInfo (y.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0) ∨ + (∃ f, y.tcbFault = some f ∧ Endp.FaultOKA (L := L) s sd (ipcBufOfA true xt) y f) ∨ + ∃ b, y.tcbFault = some (.timeout b)) + (hV : L.mem s (core.num.U64.wrapping_add sd 3664#u64) = 0#u64) : + RecvScopeA OS N E Rp T s σ true true + /-- A failed CSpace walk of the cap register, delivered to a Null fault handler (`recvCapFaultNullA`). + [S] `hnull`: the cap-fault handler endpoint case has no arm. -/ + | capFaultNull {blocking canReply : Bool} {lf : error.LookupFault} + (hcap : cspace.lookup_cap st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.CAP)) = + ok (.Err (.Lookup lf))) + (hnull : capA st s (slotIdx (L.curW s) layout.tcb.SLOT_FAULT_HANDLER) = .Null) + (hgc : getCap (Z := Unit) (getTcbFaultHandlerPtr σ.curThread) σ = ({(.nullCap, σ)}, False)) : + RecvScopeA OS N E Rp T s σ blocking canReply + /-- Blocking wait on a notification that is not active and has no SC (`waitBlockA`). [S] `hnsc` (N1). -/ + | waitBlock {canReply : Bool} {p badge slot : U64} {send : Bool} {x : Notification} + (e : RecvNtfSelA st OS N s σ p badge slot send x) (hna : ∀ b, x.ntfnObj ≠ .activeNtfn b) + (hnsc : x.ntfnSc = none) (hbt : x.ntfnBoundTcb = some σ.curThread ∨ x.ntfnBoundTcb = none) : + RecvScopeA OS N E Rp T s σ true canReply + /-- Blocking wait on a notification whose SC is the waiter's (`waitBlockScA`). -/ + | waitBlockSc {canReply : Bool} {p badge slot : U64} {send : Bool} {x : Notification} {sc : ObjRef} + (e : RecvNtfSelA st OS N s σ p badge slot send x) (hna : ∀ b, x.ntfnObj ≠ .activeNtfn b) + (hsn : x.ntfnSc = some sc) (hsct : ∀ y, σ.kheap σ.curThread = some (.tcb y) → y.tcbSchedContext = some sc) + (hbt : x.ntfnBoundTcb = some σ.curThread ∨ x.ntfnBoundTcb = none) : + RecvScopeA OS N E Rp T s σ true canReply + /-- An active notification, the receiver has an SC (`waitActiveA`). -/ + | waitActive {blk canReply : Bool} {p badge slot : U64} {send : Bool} {x : Notification} {b0 : Badge} + (e : RecvNtfSelA st OS N s σ p badge slot send x) (hact : x.ntfnObj = .activeNtfn b0) + (hbt : x.ntfnBoundTcb = some σ.curThread ∨ x.ntfnBoundTcb = none) + (hown : (∀ x, σ.kheap σ.curThread = some (.tcb x) → x.tcbSchedContext = some σ.curSc) ∨ + (x.ntfnSc = none ∧ ∀ y, σ.kheap σ.curThread = some (.tcb y) → y.tcbSchedContext = none)) : + RecvScopeA OS N E Rp T s σ blk canReply + /-- The same for a passive receiver (`waitActiveDon_oneNodeA`). [S] `hne`: the `ntfnSc = curSc` gap. -/ + | waitActiveDon {blk canReply : Bool} {p badge slot : U64} {send : Bool} {x : Notification} {b0 : Badge} + (e : RecvNtfSelA st OS N s σ p badge slot send x) (hact : x.ntfnObj = .activeNtfn b0) + (hbt : x.ntfnBoundTcb = some σ.curThread ∨ x.ntfnBoundTcb = none) + (hpas : ∀ y, σ.kheap σ.curThread = some (.tcb y) → y.tcbSchedContext = none) + (hne : x.ntfnSc ≠ some σ.curSc) : + RecvScopeA OS N E Rp T s σ blk canReply + +end + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {s : S} {a : A} {σ : State Unit} {m : McState} + +set_option maxHeartbeats 4000000 in +/-- **`handle_recv` over the scope** (module docstring), at any `RecvPreA` state. -/ +theorem recvDispatchA + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {blocking canReply : Bool} (Sc : RecvScopeA st OS N E Rp Z.T s σ blocking canReply) : + syscall.handle_recv st ar s a blocking canReply ⦃ r => + StepPostA (fun s' a' τ => A64RelS3 st AR ar hPl.ipa n Z.Cs s' a' τ m ∧ BodyOKA τ m) + (handleRecv blocking canReply) σ r ⦄ := by + have fin : ∀ Out, HRecvPostA st AR ar hPl.ipa n Z.Cs s a m blocking canReply σ Out → + syscall.handle_recv st ar s a blocking canReply ⦃ r => + StepPostA (fun s' a' τ => A64RelS3 st AR ar hPl.ipa n Z.Cs s' a' τ m ∧ BodyOKA τ m) + (handleRecv blocking canReply) σ r ⦄ := by + rintro Out ⟨s', a', σ', eq, mem, -, R, -, body⟩ + rw [eq, WP.spec_ok] + exact ⟨σ', mem, R, body⟩ + cases Sc with + | blockAny e hcb hbn => + exact fin _ (RecvPreA.recvBlockAnyA hA hPl J hCs h1 hmb hmax htu e.hcap e.hslot e.hcov e.hsc e.hp e.hk hcb hbn) + | blockRAny e r hcb hbn => + exact fin _ (RecvPreA.recvBlockRAnyA_repA hA hPl J hCs h1 hmb hmax htu e.hcap e.hslot e.hcov e.hsc r.hcapR r.hrslot + r.hrsc r.hrr e.hp e.hk hcb hbn) + | nbRecvIdle hcap hrp hp hk hcb hbn => + exact fin _ (RecvPreA.nbRecvIdleA hA hPl J hCs hmb hmax htu hcap hrp hp hk hcb hbn) + | nbWaitIdle hcap hp hk hna hbt => + exact fin _ (RecvPreA.nbWaitIdleA hA hPl J hCs hmb hmax htu hcap hp hk hna hbt) + | boundActive e hp hk hact hbn hown => + exact fin _ (RecvPreA.recvBoundActiveA hA hPl J hCs hmb hmax htu e.hcap e.hslot e.hcov e.hsc e.hp e.hk hp hk hact + hbn hown) + | boundActiveDon e hp hk hact hbn hpas hne => + exact fin _ (RecvPreA.recvBoundActiveDon_oneNodeA hA hPl J hCs h1 hmb hmax htu e.hcap e.hslot e.hcov e.hsc e.hp + e.hk hp hk hact hbn hpas hne) + | send e hrp hrv hrcap hbn hsnd hng => + exact fin _ (RecvPreA.recvSendA hA hPl J hCs h1 hmb hmax htu e.hcap e.hslot e.hcov e.hsc hrp hrv hrcap e.hp e.hk + hbn hsnd hng) + | push e r hbn hsnd hsndF htsc0 => + exact fin _ (RecvPreA.recvPushA_repA hA hPl J hCs h1 hmb hmax htu e.hcap e.hslot e.hcov e.hsc r.hcapR r.hrslot r.hrsc + r.hrr e.hp e.hk hbn hsnd hsndF htsc0) + | pushDon e r hbn hsnd hsndF hpas hV => + exact fin _ (RecvPreA.recvPushDonA_repA hA hPl J hCs h1 hmb hmax htu e.hcap e.hslot e.hcov e.hsc r.hcapR r.hrslot + r.hrsc r.hrr e.hp e.hk hbn hsnd hsndF hpas hV) + | pushUnb e r hbn hsnd hsndF hV => + exact fin _ (RecvPreA.recvPushUnbA hA hPl J hCs h1 hmb hmax htu e.hcap e.hslot e.hcov e.hsc r.hcapR r.hrslot + r.hrsc r.hrr e.hp e.hk hbn hsnd hsndF hV) + | sendUnb e hrp hrv hrcap hbn hsnd hng => + exact fin _ (RecvPreA.recvSendUnbA hA hPl J hCs h1 hmb hmax htu e.hcap e.hslot e.hcov e.hsc hrp hrv hrcap e.hp + e.hk hbn hsnd hng) + | capFaultNull hcap hnull hgc => + exact fin _ (RecvPreA.recvCapFaultNullA hA hPl J hCs h1 hcap hnull hgc) + | waitBlock e hna hnsc hbt => + exact fin _ (RecvPreA.waitBlockA hA hPl J hCs h1 hmb hmax htu e.hcap e.hslot e.hcov e.hsc e.hp e.hk hna hnsc hbt) + | waitBlockSc e hna hsn hsct hbt => + exact fin _ (RecvPreA.waitBlockScA hA hPl J hCs h1 hmb hmax htu e.hcap e.hslot e.hcov e.hsc e.hp e.hk hna hsn hsct + hbt) + | waitActive e hact hbt hown => + exact fin _ (RecvPreA.waitActiveA hA hPl J hCs hmb hmax htu e.hcap e.hslot e.hcov e.hsc e.hp e.hk hact hbt hown) + | waitActiveDon e hact hbt hpas hne => + exact fin _ (RecvPreA.waitActiveDon_oneNodeA hA hPl J hCs h1 hmb hmax htu e.hcap e.hslot e.hcov e.hsc e.hp e.hk + hact hbt hpas hne) + +end + +/-- **The four standalone receive syscalls' `dispatch_syscall`** (module docstring): `DispatchBranchesA.recv` from a +per-`(blocking, canReply)` receive body, every other field absurd (`DispatchInvokeAtA` / `call = .sysYield` mention +other syscalls). -/ +theorem recvBody_of_scopeA {S A : Type} {st : machine.Store S} [L : LawfulStore st] {ar : machine.Arch A} + {s : S} {a : A} {σ : State Unit} {invoke : Bool → Bool → Bool → Bool → CapRef → PMonad Unit Unit} + {pending : SMonad Unit Unit} {Rel : S → A → State Unit → Prop} (call : Syscall) + (hc : call = .sysRecv ∨ call = .sysNBRecv ∨ call = .sysWait ∨ call = .sysNBWait) {t : U64} {x : Tcb} + (hct : sched.cur_thread st s = ok t) (hcur : σ.curThread = specPtr t) + (hk : σ.kheap (specPtr t) = some (.tcb x)) + (cap : x.tcbArch.tcbContext.userRegs cap_register = (L.mem s (regWA t layout.reg.CAP)).bv) + (reply : x.tcbArch.tcbContext.userRegs replyRegister = (L.mem s (regWA t layout.reg.REPLY)).bv) + (dest : x.tcbArch.tcbContext.userRegs nbsendRecvDest = (L.mem s (regWA t layout.reg.NBSEND_RECV_DEST)).bv) + (recv : ∀ blocking canReply, DispatchRecvAtA call blocking canReply → + syscall.handle_recv st ar s a blocking canReply ⦃ r => StepPostA Rel (handleRecv blocking canReply) σ r ⦄) : + syscall.dispatch_syscall st ar s a (syscallNumberA call) ⦃ r => + StepPostA Rel (fl4SettleDispatch pending (fl4DispatchSyscall invoke call)) σ r ⦄ := by + refine dispatchSyscall_refinesA (InvRel := fun _ _ _ _ => False) (invoke := invoke) (pending := pending) call hct ?_ + hcur hk cap reply dest + refine { inv := ?_, done := ?_, preempt := ?_, combined := ?_, recv := ?_, yield := ?_ } + · intro calling blocking donate first cptr h + rcases hc with rfl | rfl | rfl | rfl <;> cases h + · intro calling blocking donate first cptr s' a' τ h + rcases hc with rfl | rfl | rfl | rfl <;> cases h + · intro calling blocking donate first cptr s' a' τ h + rcases hc with rfl | rfl | rfl | rfl <;> cases h + · intro cptr rr s' a' τ h + rcases hc with rfl | rfl | rfl | rfl <;> cases h + · exact recv + · intro h + rcases hc with rfl | rfl | rfl | rfl <;> cases h + +/-- The settle wrapper of the dispatcher is the front's `catch` for the receive syscalls, given the preemption-path +agreement ([P], the one `syscallSend_refinesA` uses). -/ +theorem recvSettle_eqA {invoke : Bool → Bool → Bool → Bool → CapRef → PMonad Unit Unit} {pending : SMonad Unit Unit} + (hpend : fl4PreemptionPath (Z := Unit) pending = fl4PreemptionPathIrq) (rep : Bool) (call : Syscall) + (hc : call = .sysRecv ∨ call = .sysNBRecv ∨ call = .sysWait ∨ call = .sysNBWait) : + fl4SettleDispatch pending (fl4DispatchSyscall invoke call) = + NonDetE.catch (fl4HandleSyscall (Z := Unit) rep call) (fun _ => fl4PreemptionPathIrq) := by + rw [← hpend] + unfold fl4SettleDispatch + rcases hc with rfl | rfl | rfl | rfl <;> + simp only [fl4DispatchSyscall, fl4HandleSyscall, fl4HandleSyscallWith] + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {s : S} {a : A} {σ : State Unit} {m : McState} + +set_option maxHeartbeats 4000000 in +/-- **Recv, NBRecv, Wait and NBWait from the syscall-entry record** (module docstring): `dispatch_syscall` over the +receive scope at each of the call's `(blocking, canReply)`. -/ +theorem JSyscallA.recvBodyScopeA + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {invoke : Bool → Bool → Bool → Bool → CapRef → PMonad Unit Unit} {pending : SMonad Unit Unit} (call : Syscall) + (hc : call = .sysRecv ∨ call = .sysNBRecv ∨ call = .sysWait ∨ call = .sysNBWait) + (Sc : ∀ blocking canReply, DispatchRecvAtA call blocking canReply → + RecvScopeA st OS N E Rp Z.T s σ blocking canReply) : + syscall.dispatch_syscall st ar s a (syscallNumberA call) ⦃ r => + StepPostA (fun s' a' τ => A64RelS3 st AR ar hPl.ipa n Z.Cs s' a' τ m ∧ BodyOKA τ m) + (fl4SettleDispatch pending (fl4DispatchSyscall invoke call)) σ r ⦄ := by + have htZ : L.curW s ∈ Z.T := J.sr.curT + obtain ⟨x, hk, -⟩ := J.ep.tcb (L.curW s) htZ + have regs := (J.ep.at_tcb htZ hk).regs + have reg : ∀ (r : Fl4SpecA64.Register) (o : U64), Endp.regA (L.curW s) (regIdxA r) = regWA (L.curW s) o → + x.tcbArch.tcbContext.userRegs r = (L.mem s (regWA (L.curW s) o)).bv := fun r o e => by + rw [← e]; exact (regs r).symm + have off : ∀ (r : Fl4SpecA64.Register) (o : U64), (regIdxA r).val = o.val → + Endp.regA (L.curW s) (regIdxA r) = regWA (L.curW s) o := fun r o e => by + unfold Endp.regA regWA + rw [UScalar.eq_of_val_eq e] + refine recvBody_of_scopeA (st := st) (ar := ar) (a := a) (invoke := invoke) (pending := pending) call hc + (cur_thread_ok s) J.j.1.sch.curThread hk + (reg cap_register layout.reg.CAP (off _ _ (by simp [cap_register, capRegister, regIdxA, layout.reg.CAP, layout.reg.X0]))) + (reg replyRegister layout.reg.REPLY (off _ _ (by simp [replyRegister, regIdxA, layout.reg.REPLY, layout.reg.X6]))) + (reg nbsendRecvDest layout.reg.NBSEND_RECV_DEST + (off _ _ (by simp [nbsendRecvDest, regIdxA, layout.reg.NBSEND_RECV_DEST, layout.reg.X8]))) + (fun blocking canReply h => ?_) + exact recvDispatchA hA hPl J.toRecvPreA hCs h1 hmb hmax htu (Sc blocking canReply h) + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3RecvSendA.lean b/proofs/Fl4ProofsA64/P3/Stage3RecvSendA.lean new file mode 100644 index 000000000..21d98b0d7 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3RecvSendA.lean @@ -0,0 +1,672 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2c): **`Recv` with a sender waiting, at the record**: `handle_recv` on an endpoint holding waiting senders whose head sender has no fault and transfers no extra caps. +The message (or, for a faulted sender, the fault message) is transferred to the receiver. A non-call sender without a +fault runs and may become the pending switch target; a call or faulted sender, without a reply object to push, becomes +`Inactive` and no switch target. P1's facts come from +`receive_ipc.rpw`. + +* `BodyOKA.ofEntrySwA`: `BodyOKA.ofEntry` allowing a pending switch to a `schedulable` thread and a release queue + that only shrinks. +* `JStage3A.ntfnWait_recvSA`: the notification waiters across `receive_from_sender_fpA`'s write set. +* **`JSyscallA.recvSendA`**: `HRecvPostA`. + +[S] the endpoint holds waiting senders; its head sender has no fault and transfers no extra caps, or has a fault in +`FaultOKA`, or has any timeout fault (`hsnd`; `tmoSelA` splits it by the sender's SC; a call or a non-call sender); `can_reply` with an unlinked reply object named by the REPLY register (`hrp`, `hrcap`) or without one; a call or faulted sender then cannot grant (`hng`: it becomes `Inactive`, nothing is pushed; the push is `recvPushA`); no bound notification, or one that is not active and (when blocking) whose SC is not the +receiver's (`BndOKA`); `numNodes = 1`. The receiver needs no SC (a passive replier): no reply push happens here. +[P] `ArchTotal`, `ArchPlat`, `hCs`, `hmb`, `hmax`, `htu`. New module. +-/ +import Fl4ProofsA64.P3.Stage3RecvBlockA +import Fl4ProofsA64.P3.Stage3SendRecvArmA +import Fl4ProofsA64.P3.Stage3SendNarrowA +import Fl4ProofsA64.P3.Stage3RecvSendQA +import Fl4ProofsA64.P3.Stage3RecvSendFpA +import Fl4ProofsA64.P3.Stage3RecvSendFrA +import Fl4ProofsA64.P3.Stage3RecvSendSwA +import Fl4ProofsA64.P3.Stage3RecvSendCscA +import Fl4ProofsA64.P3.Stage3RecvTmoFpA +import Fl4ProofsA64.P3.Stage3RecvReplyPinA +import Fl4ProofsA64.P3.Stage3RecvTmoSelA +import Fl4ProofsA64.P3.Stage3RecvFrontA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4Spec Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {J5 J7 : JSlotA S} {R : Set U64 → JSlotA S} {s s' : S} {a : A} {σ σ' : State Unit} + {m : McState} + +/-- **`BodyOKA.ofEntry` with a pending switch to a `schedulable` thread** (module docstring). -/ +theorem BodyOKA.ofEntrySwA (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (hact : σ'.schedulerAction = σ.schedulerAction ∨ σ'.schedulerAction = .chooseNewThread ∨ + ∃ u, σ'.schedulerAction = .switchThread u ∧ schedulable u σ' = true) + (htime : σ'.curTime = σ.curTime) + (hlast : σ'.machineState.lastMachineTime = σ.machineState.lastMachineTime) + (hthr : σ'.curThread = σ.curThread) (hsc : σ'.curSc = σ.curSc) (hcon : σ'.consumedTime = σ.consumedTime) + (hrel : ∀ y ∈ σ'.releaseQueue, y ∈ σ.releaseQueue) + (hobj : ∀ y' k, σ'.kheap σ.curSc = some (.schedContext y' k) → ∃ y, σ.kheap σ.curSc = some (.schedContext y k) ∧ + y'.scRefills = y.scRefills ∧ y'.scRefillMax = y.scRefillMax ∧ y'.scTcb = y.scTcb) + (hrun : σ'.schedulerAction = .resumeCurThread → ∀ x, σ'.kheap σ.curThread = some (.tcb x) → + x.tcbState = .running ∨ x.tcbState = .restart) : + BodyOKA σ' m := by + have hd : ∀ y' y : SchedContext, y'.scRefills = y.scRefills → refillHd y' = refillHd y := fun y' y e => by + unfold refillHd; rw [e] + have ha : ∀ y' y : SchedContext, y'.scRefillMax = y.scRefillMax → scActive y' = scActive y := fun y' y e => by + unfold scActive; rw [e] + refine ⟨⟨?_, fun y' k hk _ _ => ?_, fun h x hx => ?_⟩, fun t e => ?_, fun h => ?_, fun y' k hk _ _ _ o ho => ?_⟩ + · have := J.validTime + unfold ValidMachineTimeA at this ⊢ + rw [htime, hlast]; exact this + · rw [hsc] at hk + obtain ⟨y, hy, e1, e2, -⟩ := hobj y' k hk + constructor + · intro z j hz + rw [hsc, hk] at hz + obtain ⟨rfl, -⟩ := KernelObject.schedContext.inj (Option.some.inj hz) + rw [hd _ _ e1, hcon, htime] + exact J.offReady y k hy + · intro hpos z j hz hactive + rw [hsc, hk] at hz + obtain ⟨rfl, -⟩ := KernelObject.schedContext.inj (Option.some.inj hz) + rw [hd _ _ e1, hcon, htime] + rw [hcon] at hpos + refine J.offOK hpos y k hy fun h => ?_ + rw [← ha _ _ e2]; exact hactive (hsc ▸ h) + · rcases hrun h x (hthr ▸ hx) with h | h <;> rw [h] + · exact Or.inl rfl + · exact Or.inr (Or.inl rfl) + · rcases hact with h | h | ⟨u, h, hu⟩ + · rw [h, J.act] at e; cases e + · rw [h] at e; cases e + · rw [h] at e; cases e; exact hu + · rw [hsc] at h ⊢; exact J.curScNotIdle h + · rw [hsc] at hk + obtain ⟨y, hy, -, -, e3⟩ := hobj y' k hk + obtain ⟨x, hx, hxs, -⟩ := J.curInv.tcb + obtain ⟨z, j, hz, hzt⟩ := J.sr.inv.back _ x _ hx hxs + rw [hy] at hz + obtain ⟨rfl, -⟩ := KernelObject.schedContext.inj (Option.some.inj hz) + rw [e3, hzt] at ho + exact fun hm => J.curInv.notRel (by have h := hrel _ hm; rwa [← Option.some.inj ho] at h) + +variable (J0 : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7A st R) s a σ m) in +include J0 in +/-- **`NtfnWaitKeepA`** across `receive_from_sender_fpA`'s write set: the threads waiting on a notification are not on +the endpoint (the sender is), so no `Ep` link or block word of theirs is written; the receiver's registers are below +its links and block word. -/ +theorem JStage3A.ntfnWait_recvSA {t e sd : U64} (ht : t ∈ Z.T) (he : e ∈ E) (hsd : sd ∈ Z.T) + (hsb : L.mem s (Notif.tBlockA sd) = e) + (w : WritesIn st s s' (RecvSFpA st Z.T Bf (fun σ s => MWrA (st := st) Z m σ s) σ s t e sd)) : + NtfnWaitKeepA Z.T N (L.mem s) (L.mem s') := by + intro y hy hN + have hP := J0.iplaced + have hya := hP.e.tcb y hy + have hfy := hya.fits + have notE : ∀ u ∈ Z.T, L.mem s (Notif.tBlockA u) = e → u ≠ y := fun u _ hu c => by + subst c + rw [hu] at hN + have := hP.ne e hN e he + omega + have key : ∀ (K : U64) (b : U64), (K.val = 3336 ∨ K.val = 3496 ∨ K.val = 3504) → b = wa y K → Aligned b → + L.mem s' b = L.mem s b := by + intro K b hK hb hal + have hbv : b.val = y.val + K.val := by rw [hb, wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have apart : ∀ u ∈ Z.T, u ≠ y → u.val + 4096 ≤ y.val ∨ y.val + 4096 ≤ u.val := fun u hu ne => by + have := J0.sr.place.apart u y .tcb .tcb ((J0.sr.thT u).2 hu) ((J0.sr.thT y).2 hy) ne + have hsz : ObjKindA.size .tcb = 4096 := rfl + omega + refine w.mem_eq hal ?_ + rintro (c | c | c) + · rcases c with c | ⟨u, hu, hub, c⟩ | ⟨r, c⟩ | c + · have := hP.e.et e he y hy; unfold EW at c; omega + · have := apart u hu (notE u hu hub) + have := (hP.e.tcb u hu).fits + unfold TL at c; omega + · have hft := (hP.e.tcb t ht).fits + have h1 := regIdxA_lt r + rw [Endp.regA_val (hP.e.tcb t ht) h1] at c + by_cases ety : t = y + · subst ety; omega + · have := apart t ht ety; omega + · have := hP.e.bt _ c y hy; omega + · have h1 : b.val = sd.val + 3328 := by rw [c]; exact Endp.val_st (hP.e.tcb sd hsd) + have := apart sd hsd (notE sd hsd hsb) + omega + · rw [hb] at c + exact J0.tcbWord_offMwrA hy (by omega) (by omega) c + refine ⟨?_, ?_, ?_⟩ + · exact key 3336#u64 _ (by simp) (by unfold Notif.tBlockA; rfl) (Endp.a_blk hya).1 + · exact key 3504#u64 _ (by simp) (by rw [prevEp_eq]) (Endp.a_prev hya).1 + · exact key 3496#u64 _ (by simp) (by rw [nextEp_eq]) (Endp.a_next hya).1 + + +set_option maxHeartbeats 4000000 in +/-- **The front of `handle_recv` for either reply argument**: a `Null` reply register (no REPLY lookup), or the unlinked +reply object looked up (`recvFrontA`), from the receive core. -/ +theorem RecvPreA.recvFrontRpA + (J : RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {p badge : U64} {send grant grantReply blk canReply : Bool} {rc : capability.Cap} {Out : State Unit → Prop} + (hcap : cspace.lookup_cap st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.CAP)) = + ok (.Ok (.Endpoint p badge send true grant grantReply))) + (hrp : RecvReplyPinA (st := st) s (L.curW s) Rp canReply rc) + (core : RecvCoreA st AR ar nA n Z.Cs s a m (.Endpoint p badge send true grant grantReply) blk rc + (absCapA (.Endpoint p badge send true grant grantReply)) (absCapA rc) σ Out) : + HRecvPostA st AR ar nA n Z.Cs s a m blk canReply σ Out := by + rcases hrp with ⟨rfl, rfl⟩ | ⟨rfl, r0, g, rfl, -, -, hl⟩ + · have htZ : L.curW s ∈ Z.T := J.sr.curT + set t := L.curW s with ht_def + have ct : σ.curThread = specPtr t := J.j.1.sch.curThread + obtain ⟨xt, hkt, r⟩ := J.ep.tcb t htZ + have hthT : Z.th t = some .tcb := (J.sr.thT t).2 htZ + obtain ⟨s2, σ2, hipc, hrecv, hout, hrel, hcur, hbody⟩ := core + have hSt := J.j.1.st + have hV := J.j.1.cv + have capReg : (L.mem s (regWA t layout.reg.CAP)).bv = xt.tcbArch.tcbContext.userRegs cap_register := by + rw [← r.regs cap_register, regIdxA_capA]; rfl + refine ⟨s2, a, σ2, ?_, ?_, hout, hrel, hcur, hbody⟩ + · unfold syscall.handle_recv + rw [cur_thread_ok, bind_ok, tcb_reg_eqA, bind_ok] + rw [show L.curW s = t from rfl, hcap] + simp only [bind_ok, Bool.false_eq_true, ↓reduceIte] + exact hipc + · have capRead : NonDet.liftM dataToCptr (asUser (Z := Unit) (specPtr t) (getRegister cap_register)) σ = + ({(toBl (L.mem s (regWA t layout.reg.CAP)).bv, σ)}, False) := by + unfold NonDet.liftM + rw [nd_bind_singleA' (asUser_getRegister_eqA hkt _), ← capReg] + rfl + have hc0 : (lookupCap (Z := Unit) (specPtr t) + (toBl (L.mem s (regWA t layout.reg.CAP)).bv)).run σ = + ({(.ok (absCapA (.Endpoint p badge send true grant grantReply)), σ)}, False) := by + have l := post_of_okA (lookup_cap_recv_refinesA (st := st) _ s σ hSt hV t hthT + (L.mem s (regWA t layout.reg.CAP))) hcap + obtain ⟨e, he, hrel⟩ := l + cases e with + | error _ => exact hrel.elim + | ok ac => + obtain rfl : ac = absCapA (.Endpoint p badge send true grant grantReply) := + (show absCapA (.Endpoint p badge send true grant grantReply) = ac from hrel.2).symm + exact he + rw [handleRecv, nd_gets_bindSc, ct, nd_bind_oneSc capRead] + apply mem_bindA.2 + refine ⟨(.ok (), σ2), ?_, rfl⟩ + have hc1 : (capFaultOnFailure (ofBl (toBl (L.mem s (regWA t layout.reg.CAP)).bv)) true + (lookupCap (Z := Unit) (specPtr t) (toBl (L.mem s (regWA t layout.reg.CAP)).bv))).run σ = + ({(.ok (absCapA (.Endpoint p badge send true grant grantReply)), σ)}, False) := run_handleE_okA hc0 + rw [run_bind_okA hc1] + dsimp only [absCapA] + rw [if_pos (by simp [AllowRecv, rightsOfA])] + simp only [Bool.false_eq_true, ↓reduceIte] + rw [run_bind_okA (run_returnOkA _ _)] + unfold NonDetE.liftE + rw [ExceptT.run_lift] + exact mem_bindA.2 ⟨((), σ2), hrecv, rfl⟩ + · exact RecvPreA.recvFrontA J hcap hl core + +set_option maxHeartbeats 4000000 in +/-- **`Recv` with a sender waiting, with or without a reply object** (module docstring). -/ +theorem RecvPreA.recvSendA_core + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p badge slot rv : U64} {send grant grantReply blk : Bool} {rc : capability.Cap} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) + (hsc : capA st s slot = .Endpoint p badge send true grant grantReply) + (hra : ReplyArgA Rp (L.mem s) rc (absCapA rc) rv) (hrcA : ReplyCapA Rp rc (absCapA rc) rv) + (hrv : (rc = .Null ∧ rv = 0#u64) ∨ ∃ g, rc = .Reply rv g) + (hrcap : ∀ r g, rc = .Reply r g → ∃ rslot ∈ OS, capA st s rslot = .Reply r g) + (hp : p ∈ E) {sd : U64} {q : List ObjRef} + (hk : σ.kheap (specPtr p) = some (.endpoint (.sendEP (specPtr sd :: q)))) + (hbn : ∀ y, σ.kheap σ.curThread = some (.tcb y) → BndOKA N (L.mem s) σ (L.curW s) y blk) + (hsnd : ∀ y xt, σ.kheap (specPtr sd) = some (.tcb y) → σ.kheap σ.curThread = some (.tcb xt) → + (y.tcbFault = none ∧ ∀ e' dd, y.tcbState = .blockedOnSend e' dd → dd.senderCanGrant = false ∨ + ipcBufOfA false y = none ∨ + unat (dataToMessageInfo (y.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0) ∨ + ∃ f, y.tcbFault = some f ∧ ((∀ b, f ≠ .timeout b) → Endp.FaultOKA (L := L) s sd (ipcBufOfA true xt) y f)) + (hng : rv ≠ 0#u64 → ∀ y, σ.kheap (specPtr sd) = some (.tcb y) → ∀ e' dd, y.tcbState = .blockedOnSend e' dd → + (dd.senderIsCall = true ∨ y.tcbFault ≠ none) → dd.senderCanGrant = false ∧ dd.senderCanGrantReply = false) : + RecvCoreA st AR ar hPl.ipa n Z.Cs s a m (.Endpoint p badge send true grant grantReply) blk rc + (absCapA (.Endpoint p badge send true grant grantReply)) (absCapA rc) σ + (fun σ' => ((), σ') ∈ (specRecvSendA σ.curThread (specPtr p) (optRef rv) (specPtr sd :: q) σ).1) := by + have htZ : L.curW s ∈ Z.T := J.sr.curT + set t := L.curW s with ht_def + have ct : σ.curThread = specPtr t := J.j.1.sch.curThread + obtain ⟨xt, hkt, r⟩ := J.ep.tcb t htZ + have hkc : σ.kheap σ.curThread = some (.tcb xt) := by rw [ct]; exact hkt + have hbnd := hbn xt hkc + have hthT : Z.th t = some .tcb := (J.sr.thT t).2 htZ + have hnvm : L.mem s (stA t) ≠ sched.TS_RUNNING_VM := J.nvm + have hrunS : xt.tcbState = .running ∨ xt.tcbState = .restart := J.state hkt + have hidle : specPtr t ≠ σ.idleThread := by + obtain ⟨y, hy, hs⟩ := J.sinv.idleTcb + intro h + rw [← h, hkt] at hy + cases hy + rcases hrunS with e | e <;> rw [e] at hs <;> cases hs + have hcm : m.curCore < fl4A64Platform.numNodes := by rw [J.j.1.sch.core]; exact hCs _ J.j.1.sch.cpuCs + have hO : SchedOpsEA st Z.T E Rp Bf (IpcSchedRelA st Z m κ) := + (ipcSchedOpsA (ar := ar) J.iplaced J.ipcPlace hcm hmb hmax htu).2.2.1 + have hF : SchedFootEA st Z.T E Rp Bf (IpcSchedRelA st Z m κ) (fun σ s => MWrA (st := st) Z m σ s) := + schedFootEA_of_sched + have hSR : IpcSchedRelA st Z m κ s σ := ⟨Z.Q, Z.R, J.sr⟩ + -- the sender + obtain ⟨hst1, xs0, -, hq0, hQ, hB⟩ := J.ep.at_ep hp hk + have hsdx : sd ∈ xs0 := by + have : specPtr sd ∈ xs0.map specPtr := by rw [← hq0]; exact List.mem_cons_self + obtain ⟨y, hy, e⟩ := List.mem_map.1 this + rwa [specPtr_inj e] at hy + have hsdT : sd ∈ Z.T := hQ.mem sd hsdx + have hs4 : (L.mem s (stA sd)).val = 4 := (hB sd hsdx).1 + have hsb : L.mem s (Notif.tBlockA sd) = p := (hB sd hsdx).2 + obtain ⟨xs, hks, rs⟩ := J.ep.tcb sd hsdT + obtain ⟨e', dd, hsst⟩ := tsCode_four (rs.st.symm.trans hs4) + have hsnd' : (xs.tcbFault = none ∧ ∀ e' dd, xs.tcbState = .blockedOnSend e' dd → dd.senderCanGrant = false ∨ + ipcBufOfA false xs = none ∨ + unat (dataToMessageInfo (xs.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0) ∨ + (∃ f, xs.tcbFault = some f ∧ Endp.FaultOKA (L := L) s sd (ipcBufOfA true xt) xs f) ∨ + ∃ b, xs.tcbFault = some (.timeout b) ∧ xs.tcbSchedContext ≠ none := + (hsnd xs xt hks hkc).imp_right fun ⟨_, hf, hok⟩ => J.toJStage3A.sndFaultSelA hsdT hks _ hf hok + have hX : XferCaseA xs dd.senderCanGrant := by + rcases hsnd' with g | ⟨f, hf, -⟩ | ⟨b, hf, -⟩ + · exact Or.inl ⟨g.1, g.2 e' dd hsst⟩ + · exact Or.inr ⟨f, hf⟩ + · exact Or.inr ⟨_, hf⟩ + have hrdat : ∀ r' g', rc = .Reply r' g' → r' ∈ Rp ∧ L.mem s (Endp.rTcbA r') = 0#u64 := fun r' g' c => by + rcases hra with ⟨h0, -⟩ | ⟨b', r2, h', -, hr, h0⟩ + · rw [h0] at c; cases c + · rw [h'] at c; cases c; exact ⟨hr, h0⟩ + have hpl : ∀ b, rc = .Reply rv b → L.mem s (Endp.rTcbA rv) = 0#u64 ∨ L.mem s (Endp.rTcbA rv) = t := + fun b h => Or.inl (hrdat rv b h).2 + have hrnp : ∀ e'' dd', xs.tcbState = .blockedOnSend e'' dd' → (dd'.senderIsCall = true ∨ xs.tcbFault ≠ none) → + rv = 0#u64 ∨ (dd'.senderCanGrant = false ∧ dd'.senderCanGrantReply = false) := fun e'' dd' h hcf => by + by_cases h0 : rv = 0#u64 + · exact Or.inl h0 + · exact Or.inr (hng h0 xs hks e'' dd' h hcf) + have hcfOf : ∀ dd' : SenderPayload, ¬ (dd'.senderIsCall = false ∧ xs.tcbFault = none) → + (dd'.senderIsCall = true ∨ xs.tcbFault ≠ none) := fun dd' hr => by + cases hc : dd'.senderIsCall + · exact Or.inr fun c => hr ⟨hc, c⟩ + · exact Or.inl rfl + -- no reply push happens, so the receiver needs no SC (a passive replier) + have hsct : (dd.senderIsCall = true ∨ xs.tcbFault ≠ none) → + (dd.senderCanGrant = true ∨ dd.senderCanGrantReply = true) → optRef rv ≠ none → + ¬ (xs.tcbFault.elim false isTimeoutFault = true) → + xt.tcbSchedContext ≠ none := fun hcf hg hne _ => by + rcases hrnp e' dd hsst hcf with h0 | h0 + · exact absurd (by rw [h0]; exact optRef_zero) hne + · rcases hg with c | c + · rw [h0.1] at c; cases c + · rw [h0.2] at c; cases c + have hts : specPtr t ≠ specPtr sd := fun h => by + rw [h, hks] at hkt + cases hkt + rcases hrunS with e | e <;> rw [e] at hsst <;> cases hsst + have hsdi : specPtr sd ≠ σ.idleThread := by + obtain ⟨x, hx, hs⟩ := J.sinv.idleTcb + intro h + rw [← h, hks] at hx + cases hx + rw [hsst] at hs + cases hs + have hsdth : Z.th sd = some .tcb := (J.sr.thT sd).2 hsdT + have hon : L.mem s (affA sd) = L.cpu s := by + have x := hCs _ (J.sr.core.aff sd hsdT) + have y := hCs _ J.j.1.sch.cpuCs + exact UScalar.eq_of_val_eq (by omega) + have hnb : tsCode xt.tcbState ≠ 4 := by rcases hrunS with e | e <;> rw [e] <;> simp [tsCode] + have hu := J.toJStage3A.bufFrameA htZ hkt true + have hal := J.toJStage3A.bufAlign0A htZ hkt true + have hoff := J.toJStage3A.slotOffEwA + -- FL4 and the spec: `receive_from_sender` + have both : endpoint.receive_from_sender st ar s a t p rv ⦃ r => + (r.2 = a ∧ ¬ (specRecvSendA (specPtr t) (specPtr p) (optRef rv) (specPtr sd :: q) σ).2 ∧ + ∃ σ', ((), σ') ∈ (specRecvSendA (specPtr t) (specPtr p) (optRef rv) (specPtr sd :: q) σ).1 ∧ + EpStateRel Z.T E Rp Bf (L.mem r.1) σ' ∧ IpcSchedRelA st Z m κ r.1 σ' ∧ + FpA st Z.T E Rp Bf (fun σ s => MWrA (st := st) Z m σ s) σ s r.1) ∧ + WritesIn st s r.1 (RecvSFpA st Z.T Bf (fun σ s => MWrA (st := st) Z m σ s) σ s t p sd) ⦄ := by + by_cases htm : ∃ b, xs.tcbFault = some (.timeout b) ∧ xs.tcbSchedContext ≠ none + · -- a timeout fault of a sender with an SC: `update_consumed` writes the SC + obtain ⟨bd, hf, hsc⟩ := htm + have hTO : TimeoutScOpA st ar Z.T E Rp Bf (IpcSchedRelA st Z m κ) + (fun u => ∀ c < fl4A64Platform.numNodes, specPtr u ≠ m.idleThreadOf c) := + (ipcSchedOpsA (ar := ar) J.iplaced J.ipcPlace hcm hmb hmax htu).2.2.2.1 + have hni := J.toJStage3A.idleNoFault.notIdle hks hf + obtain ⟨x0, hx0, hp0⟩ := J.sr.sch.tcb sd hsdT + have hscT : xs.tcbSchedContext = optScA m (L.mem s (scA sd)) := + (congrArg Tcb.tcbSchedContext (KernelObject.tcb.inj (Option.some.inj (hks.symm.trans hx0)))).trans hp0 + have hsc0 : L.mem s (scA sd) ≠ 0#u64 := fun h => hsc (by rw [hscT, h, optScA, if_pos rfl]) + have hname := J.sr.scName_of_notIdle hsdT hsc0 hni + have hsc' : xs.tcbSchedContext = some (specPtr (L.mem s (scA sd))) := by + rw [hscT, optScA, if_neg hsc0] + exact congrArg some hname + obtain ⟨y, k, hy, -⟩ := J.sr.inv.back _ xs _ hks hsc' + have hscW : ∀ b : U64, (L.mem s (scA sd)).val ≤ b.val ∧ + b.val < (L.mem s (scA sd)).val + 2 ^ (k + minSchedContextBits) → MWrA (st := st) Z m σ s b := + fun b hb => Or.inl ⟨_, J.sr.rest.scMem sd hsdT hsc0, y, k, by rw [hname]; exact hy, hb⟩ + exact WP.spec_mono (receive_from_sender_tmoScA (ar := ar) hO hF hTO J.iplaced.e J.sr.place s a σ J.ep hSR + J.j.1.st J.bufCover J.bufNative hoff htZ hthT hkt hnb hp hk hsdth hks hf rv + (fun e'' dd' h => hrnp e'' dd' h (Or.inr (by rw [hf]; exact fun c => by cases c))) hni hsc0 hsc' hy hscW) + fun _ h => ⟨⟨h.1, h.2.1, h.2.2.1⟩, h.2.2.2⟩ + have hcaseF : ∀ e'' dd', xs.tcbState = .blockedOnSend e'' dd' → + (xs.tcbFault = none ∧ (dd'.senderCanGrant = false ∨ ipcBufOfA false xs = none ∨ + unat (dataToMessageInfo (xs.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0)) ∨ + ∃ f, xs.tcbFault = some f ∧ Endp.FaultOKA (L := L) s sd (ipcBufOfA true xt) xs f := fun e'' dd' h => by + rcases hsnd' with g | g | g + · exact Or.inl ⟨g.1, g.2 e'' dd' h⟩ + · exact Or.inr g + · exact absurd g htm + have body := receive_from_senderA (ar := ar) hO hF J.iplaced.e J.sr.place s a σ J.ep hSR J.j.1.st J.j.1.cv J.bufCover + J.bufNative hoff htZ hthT hkt hnb hp hk hsdth hks hu rv rfl hcaseF + (fun e'' dd' h => by + by_cases hr : dd'.senderIsCall = false ∧ xs.tcbFault = none + · exact Or.inl ⟨hr.1, hr.2, hon⟩ + · have hcf := hcfOf dd' hr + refine Or.inr ⟨hcf, ?_⟩ + rcases hrnp e'' dd' h hcf with h0 | h0 + · exact Or.inr (by rw [h0]; exact optRef_zero) + · exact Or.inl h0) + have narrow := receive_from_sender_fpA (ar := ar) hO hF J.iplaced.e J.sr.place s a σ J.ep hSR J.j.1.st J.j.1.cv J.bufCover + J.bufNative hoff htZ hthT hkt hnb hp hk hsdth hks hu rv hcaseF + (fun e'' dd' h => by + by_cases hr : dd'.senderIsCall = false ∧ xs.tcbFault = none + · exact Or.inl ⟨hr.1, hr.2, hon⟩ + · have hcf := hcfOf dd' hr + exact Or.inr ⟨hcf, hrnp e'' dd' h hcf⟩) + exact WP.spec_and body narrow + obtain ⟨⟨s2, a2⟩, eb, ⟨ha2, -, σ2, hmem, E2, ⟨Q, Rl, sr2⟩, w⟩, wn⟩ := exists_of_specA both + simp only at ha2 eb E2 sr2 w wn + subst ha2 + -- FL4: `receive_ipc`, `handle_recv` + have hdisp : rcvDispA st ar s a2 t p blk rv ⦃ q' => q' = (s2, a2) ⦄ := by + unfold rcvDispA + step with obj.Ep.state.spec as ⟨st0, hst0⟩ + rw [epState_eq, hst1] at hst0 + subst hst0 + rw [if_neg (by rw [ep_state_vals.1]; decide), if_neg (by rw [ep_state_vals.2.2]; decide)] + rw [eb] + exact (WP.spec_ok _).2 rfl + have hipc : endpoint.receive_ipc st ar s a2 t (.Endpoint p badge send true grant grantReply) blk rc = + ok (s2, a2) := by + have h : endpoint.receive_ipc st ar s a2 t (.Endpoint p badge send true grant grantReply) blk rc ⦃ q => + q = (s2, a2) ⦄ := by + rw [receive_ipc_eqA] + refine WP.spec_bind (take_reply_plainA (R := Rp) hrcA hpl) ?_ + rintro q rfl + exact rcv_body_dispFlA J.ntfn J.ep htZ hkt hbnd hdisp + obtain ⟨q', hq', e⟩ := spec_existsA h + rw [hq', e] + -- the spec: `handle_recv` + have hSt := J.j.1.st + have hV := J.j.1.cv + have capReg : (L.mem s (regWA t layout.reg.CAP)).bv = xt.tcbArch.tcbContext.userRegs cap_register := by + rw [← r.regs cap_register, regIdxA_capA]; rfl + have hrecv : ((), σ2) ∈ (receiveIpc (Z := Unit) (specPtr t) + (absCapA (.Endpoint p badge send true grant grantReply)) blk (absCapA rc) σ).1 := by + obtain ⟨hsr, -⟩ := replyArg_specA J.iplaced.e J.ep hra + have hsr' : SpecReplyPlainA σ (specPtr t) (absCapA rc) (optRef rv) := by + rcases hsr with h | ⟨r1, rr, xr, h1, h2, h3, h4⟩ + · exact Or.inl h + · exact Or.inr ⟨r1, rr, xr, h1, h2, h3, Or.inl h4⟩ + rw [show absCapA (.Endpoint p badge send true grant grantReply) = + .endpointCap (specPtr p) badge.bv (rightsOfA send true grant grantReply) from rfl, + receiveIpc_bodyA _ _ blk hsr', bndOK_specA hkt hk J.ntfn hbnd (optRef rv)] + exact hmem + -- P1's walk + obtain ⟨Pt, Ub, Pp7, Tb7, Hr, Te, Kt, Lv7, Re, bits, KLv, W⟩ := J7A.unp J.j7 + have I := J.delIn hA hPl W + have h : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s := by + obtain ⟨q, b, x, sg, sc⟩ := J.sys.ent.bxi + exact ⟨q, b, x, sc, sg⟩ + have hext : ∀ z ∈ (Aeneas.Std.Array.repeat 3#usize 0#u64).val, z ≠ 0#u64 → z ∈ OS := fun z hz hne => by + exact absurd (by simpa [Aeneas.Std.Array.repeat] using hz) hne + have D := J.decPreA (extra := Aeneas.Std.Array.repeat 3#usize 0#u64) hslot hext + have Pz := J.toJStage3A.sysPlace hext hslot hcov + have hH : H p = some .ep := J.utIpc.ep p hp + have regs : RpPost st H Z.Cs OS Z.V ia ie Ac s s2 := by + have walk := Fl4ProofsA64.P1.W7.R.ST.receive_ipc.rpw (st := st) (ar := ar) hA a2 t + (.Endpoint p badge send true grant grantReply) blk rc p I J.core0 J.ko Pz (SW.start J.sys.sysl J.symL h) + D.cb + (by rcases J.runW with e | e | e <;> rw [e] <;> + simp [sched.TS_RUNNING, sched.TS_RESTART, sched.TS_RUNNING_VM, sched.TS_INACTIVE]) + rfl hH + (fun k hk' => by + rw [hH] at hk' + cases hk' + exact ⟨slot, hslot, by rw [hsc]; exact names_ep⟩) + (fun r' g' c => by + obtain ⟨hr', -⟩ := hrdat r' g' c + obtain ⟨rslot, hrs, hc'⟩ := hrcap r' g' c + exact ⟨J.utIpc.reply r' hr', rslot, hrs, g', hc'⟩) + (J.tcbs ▸ J.runVcpu) hnvm + exact (post_of_okA (P := fun q => RpW st H Z.Cs OS Z.V ia ie Ac s q.1) walk hipc).post + obtain ⟨bxi, sysl, symL, userOK, j7⟩ := J.toJStage3A.finRpA hA hPl rfl regs σ2 + -- the spec outcome's facts + set M : ObjRef → Prop := fun o => o = specPtr t ∨ o = specPtr sd with hMdef + set Nq : ObjRef → Prop := fun o => o = specPtr sd ∧ dd.senderIsCall = false ∧ xs.tcbFault = none with hNdef + have K : SIKA σ.idleThread M Nq false σ σ2 := + recvSend_run_sikA (M := M) (N := Nq) J.sr.place s hSt hV hthT hk hkt hks hal (optRef rv) hsst hsct hX + (Or.inl rfl) (Or.inr rfl) (fun h h' => ⟨rfl, h, h'⟩) _ hmem + have Qr : QBlkRA (specPtr '' E) (specPtr '' N) σ σ2 ∧ QStepA (optRef rv) none σ σ2 := + recvSend_run_qbRA (i := σ.idleThread) (M := M) (N := Nq) J.sr.place s hSt hV hthT hk hkt hks hal + (optRef rv) hsst hsct hX (Or.inl rfl) (Or.inr rfl) (fun h h' => ⟨rfl, h, h'⟩) J.ipcSorted _ hmem + obtain ⟨U2, run, run''⟩ := recvSend_run_frA J.sr.place s hSt hV hthT hkt hks hal (optRef rv) hsst hsct hX _ hmem + have run' : ¬ (dd.senderIsCall = false ∧ xs.tcbFault = none) → ∀ z, σ2.kheap (specPtr sd) = some (.tcb z) → + z.tcbState = .inactive := fun hr => by + refine (run'' (hcfOf dd hr)).2 (fun h => ?_) + rcases hrnp e' dd hsst (hcfOf dd hr) with h0 | h0 + · exact h.2 (by rw [h0]; exact optRef_zero) + · rcases h.1 with c | c + · rw [h0.1] at c; cases c + · rw [h0.2] at c; cases c + + have sv : SchedValidA σ2 := + recvSend_run_schedValidA J.sr.place s hSt hV hthT hk hkt hks hal (optRef rv) hsst hsct + (fun hcf hg hne => by + rcases hrnp e' dd hsst hcf with h0 | h0 + · exact absurd (by rw [h0]; exact optRef_zero) hne + · rcases hg with c | c + · rw [h0.1] at c; cases c + · rw [h0.2] at c; cases c) (J.boundActive) hts hX J.schedValid J.toJStage3A.rqDomPrioA _ hmem + obtain ⟨sw2, lmt2⟩ := recvSend_run_swA J.sr.place s hSt hV hthT hk hkt hks hal (optRef rv) hsst hsct hX J.schedValid + J.toJStage3A.rqDomPrioA J.body.sw (fun u hu => ⟨J.swCur u hu, J.swBlocked hks (by rw [hsst]; rfl) u hu⟩) _ hmem + have C2 : CscKA σ σ2 := recvSend_run_cscA J.sr.place s hSt hV hthT hkt hks hal _ hsst hX _ hmem + have hrt : ∀ r' ∈ Rp, DeadA st OS .reply s r' → ∀ y, σ2.kheap (specPtr r') = some (.reply y) → y.replyTcb = none := by + intro r' hr' hd y hy + obtain ⟨x, hx, keep⟩ := Qr.2.rt (specPtr r') y hy + refine keep (fun h => ?_) (J.deadExt.replyTcb r' hr' hd x hx) + have hne : rv ≠ 0#u64 := fun h0 => by rw [h0, optRef_zero] at h; cases h + obtain ⟨g, hrc⟩ := hrv.resolve_left (fun hh => hne hh.2) + obtain ⟨rslot, hrs, hc'⟩ := hrcap rv g hrc + have e : r' = rv := specPtr_inj (Option.some.inj (h.trans (optRef_ne hne))) + exact hd rslot hrs (e ▸ ⟨g, hc'⟩) + -- the receiver's state word is kept + have hst2 : L.mem s2 (stA t) = L.mem s (stA t) := by + have hta := J.sr.core.tcbs t htZ + refine wn.mem_eq (a_st hta).1 ?_ + have hv := Endp.val_st hta + have apart : ∀ u ∈ Z.T, u ≠ t → u.val + 4096 ≤ t.val ∨ t.val + 4096 ≤ u.val := fun u hu ne => by + have := J.sr.place.apart u t .tcb .tcb ((J.sr.thT u).2 hu) ((J.sr.thT t).2 htZ) ne + have hsz : ObjKindA.size .tcb = 4096 := rfl + omega + rintro (c | c | c) + · rcases c with c | ⟨u, hu, -, c⟩ | ⟨r', c⟩ | c + · have := J.iplaced.e.et p hp t htZ; unfold Endp.EW at c; omega + · have := (J.iplaced.e.tcb u hu).fits + unfold TL at c + by_cases e : u = t + · subst e; omega + · have := apart u hu e; omega + · rw [Endp.regA_val (J.iplaced.e.tcb t htZ) (regIdxA_lt r')] at c + have := regIdxA_lt r' + omega + · have := J.iplaced.e.bt _ c t htZ; omega + · have h' := congrArg (fun w : U64 => w.val) c + simp only [hv, Endp.val_st (J.iplaced.e.tcb sd hsdT)] at h' + exact hts (congrArg specPtr (UScalar.eq_of_val_eq (by omega))) + · rw [stA_eq] at c + exact J.toJStage3A.tcbWord_offMwrA htZ (by simp) (by simp) c + have hxst : ∀ x, σ2.kheap (specPtr t) = some (.tcb x) → x.tcbState = .running ∨ x.tcbState = .restart := by + intro x hx + obtain ⟨y, hy, r2⟩ := E2.tcb t htZ + rw [hx] at hy + cases hy + have h2 : tsCode x.tcbState = tsCode xt.tcbState := by rw [← r2.st, hst2, r.st] + rcases hrunS with e | e <;> rw [e] at h2 <;> + (rcases hs : x.tcbState with _ | _ | _ | _ | _ | _ | _ | _ | ⟨⟨⟩⟩ <;> rw [hs] at h2 <;> + simp [tsCode] at h2 ⊢) + have after : ∀ u ∈ Z.T, M (specPtr u) → ∀ x, σ2.kheap (specPtr u) = some (.tcb x) → + epBlocked x.tcbState = none ∧ ∀ p, x.tcbState ≠ .blockedOnNotification p := by + intro u _ hu x hx + rcases hu with h | h + · rw [specPtr_inj h] at hx + rcases hxst x hx with e | e <;> rw [e] <;> exact ⟨rfl, fun _ h => by cases h⟩ + · rw [h] at hx + by_cases hr : dd.senderIsCall = false ∧ xs.tcbFault = none + · rw [run hr.1 hr.2 x hx]; exact ⟨rfl, fun _ h => by cases h⟩ + · rw [run' hr x hx]; exact ⟨rfl, fun _ h => by cases h⟩ + have wk : NtfnWaitKeepA Z.T N (L.mem s) (L.mem s2) := J.toJStage3A.ntfnWait_recvSA htZ hp hsdT hsb wn + have wI : WritesIn st s s2 (fun b => IpcFpA st Z E Rp Bf m σ s b ∨ RepWA Rp b) := WritesIn.mono w fun _ => Or.inl + have wN : WritesIn st s s2 (fun b => SchedStepNWA (st := st) N E Rp Z m σ s b ∨ BufW Bf b) := + w.mono fun _ h => h.elim (ewrite_stepNA_buf J.sr.core.tcbs) fun h => Or.inl (Or.inl (Or.inl h)) + have notM : ∀ c < n, ¬ M (m.idleThreadOf c) := by + intro c hc' h + obtain ⟨y, hy, hs⟩ := J.sidle.f3 c hc' + rcases h with h | h + · rw [h, hkt] at hy; cases hy; rcases hrunS with e | e <;> rw [e] at hs <;> cases hs + · rw [h, hks] at hy; cases hy; rw [hsst] at hs; cases hs + have sinv : SInvXA σ2 m n := by + refine K.sinvSw J.sinv J.sidle J.body.sw J.noSelf (fun u hu h => h.elim (J.swCur u hu) (J.swBlocked hks (by rw [hsst]; rfl) u hu)) notM (fun h => h.elim (fun h => hidle h.symm) fun h => hsdi h.symm) ?_ ?_ ?_ + (fun y hy hr => by + rcases hy with rfl | rfl + · exact J.notRel (by rw [ct]; exact hr) + · obtain ⟨x, hx, hrn⟩ := J.sinv.rlsRun _ hr; rw [hks] at hx; cases hx; rw [hsst] at hrn; cases hrn) + (J.sidle.rdyA J.sinv) + (fun q hq x y hx hy hf => by + rcases hq with rfl | rfl + · obtain ⟨x0, hx0, g⟩ := K.tcbB hy + rw [hkt] at hx0; cases hx0 + exact absurd (g.fault.trans (FaultStOkA.none J.sinv.faultSt hkt (by + rcases hrunS with e | e <;> rw [e] <;> rintro (⟨_, _, h⟩ | ⟨_, h⟩ | h) <;> cases h))) hf + · by_cases hr : dd.senderIsCall = false ∧ xs.tcbFault = none + · obtain ⟨x0, hx0, g⟩ := K.tcbB hy + rw [hks] at hx0; cases hx0 + exact absurd (g.fault.trans hr.2) hf + · rw [run' hr y hy]; exact Or.inr (Or.inr rfl)) + · intro y hy dm pr hm + rcases hy with rfl | rfl + · exact J.noQ dm pr (by rw [ct]; exact hm) + · obtain ⟨x, hx, hr⟩ := J.sinv.rq dm pr _ hm + rw [hks] at hx; cases hx; rw [hsst] at hr; cases hr + · intro y hy + obtain ⟨rfl, hcl, hfn⟩ := hy + exact ⟨fun h => hts (h.trans ct).symm, hsdi, fun c hc' h => notM c hc' (Or.inr h), + fun z hz => by rw [run hcl hfn z hz]; rfl⟩ + · intro y hy + obtain ⟨rfl, -⟩ := hy + exact ⟨xs, hks⟩ + have hMv : ∀ u ∈ Z.T, M (specPtr u) → L.mem s (stA u) ≠ sched.TS_RUNNING_VM := by + intro u _ hu h + rcases hu with e | e <;> rw [specPtr_inj e] at h + · exact hnvm h + · rw [h] at hs4; revert hs4; unfold sched.TS_RUNNING_VM; decide + have ntfn := J.toJStage3A.ntfn_ipcA wI K E2 wk (fun u hu hm x hx p' h => by + rcases hm with hm | hm + · rw [hm, hkt] at hx; cases hx; rcases hrunS with e | e <;> rw [e] at h <;> cases h + · rw [hm, hks] at hx; cases hx; rw [hsst] at h; cases h) (fun u hu hm x hx => (after u hu hm x hx).2) + have J2 := J.toJStage3A.of_ipcStepURSA (Q := Q) (Rl := Rl) (curOf := Z.curOf) (curScOf := Z.curScOf) wN U2.1 U2.2.1 + Qr.1 hrt (J.toJStage3A.specZero_of_recvBufA htZ hkt U2.2.2) (fun _ _ => rfl) bxi sysl symL userOK j7 sr2 E2 ntfn + (J.toJStage3A.reply_ipcA (WritesIn.mono w fun _ h => h) K) J.deadExt.reply K.ipcKeep (J.toJStage3A.tw_ipcA wI) sinv + (K.sidle J.sidle notM (J.sidle.rdyA J.sinv)) (J.toJStage3A.runVcpu_ipcA wI K E2 hMv) (J.toJStage3A.blkQ_ipcA K E2 wk after) sv + have hcur : σ2.curThread = σ.curThread := by rw [K.frame] + refine ⟨s2, σ2, hipc, hrecv, by rw [ct]; exact hmem, A64RelS3.ofJ J2 ?bo, curW_keptA J.toJStage3A J2 hcur, ?bo⟩ + refine J.body.step (σ' := σ2) sw2 (by rw [K.frame]) lmt2 hcur (by rw [K.frame]) (by rw [K.frame]) (fun _ _ _ _ _ h => K.rls _ h) + (fun y' k hz => ?_) (fun _ z hz => ?_) + · obtain ⟨y, hy, e1, e2⟩ := C2.sc y' k hz + refine ⟨y, hy, e1, e2, Or.inl ?_⟩ + have o := K.obj σ.curSc + rw [hy, hz] at o + cases o with + | same => rfl + | sc _ _ _ g => exact g.th.resolve_right (fun c => by cases c.1) + · rw [ct] at hz + exact hxst z hz + +set_option maxHeartbeats 1000000 in +/-- **`Recv` with a sender waiting, with or without a reply object** (module docstring). -/ +theorem RecvPreA.recvSendA + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p badge slot rv : U64} {send grant grantReply blk canReply : Bool} {rc : capability.Cap} + (hcap : cspace.lookup_cap st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.CAP)) = + ok (.Ok (.Endpoint p badge send true grant grantReply))) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) + (hsc : capA st s slot = .Endpoint p badge send true grant grantReply) + (hrp : RecvReplyPinA (st := st) s (L.curW s) Rp canReply rc) + (hrv : (rc = .Null ∧ rv = 0#u64) ∨ ∃ g, rc = .Reply rv g) + (hrcap : ∀ r g, rc = .Reply r g → ∃ rslot ∈ OS, capA st s rslot = .Reply r g) + (hp : p ∈ E) {sd : U64} {q : List ObjRef} + (hk : σ.kheap (specPtr p) = some (.endpoint (.sendEP (specPtr sd :: q)))) + (hbn : ∀ y, σ.kheap σ.curThread = some (.tcb y) → BndOKA N (L.mem s) σ (L.curW s) y blk) + (hsnd : ∀ y xt, σ.kheap (specPtr sd) = some (.tcb y) → σ.kheap σ.curThread = some (.tcb xt) → + (y.tcbFault = none ∧ ∀ e' dd, y.tcbState = .blockedOnSend e' dd → dd.senderCanGrant = false ∨ + ipcBufOfA false y = none ∨ + unat (dataToMessageInfo (y.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0) ∨ + ∃ f, y.tcbFault = some f ∧ ((∀ b, f ≠ .timeout b) → Endp.FaultOKA (L := L) s sd (ipcBufOfA true xt) y f)) + (hng : rv ≠ 0#u64 → ∀ y, σ.kheap (specPtr sd) = some (.tcb y) → ∀ e' dd, y.tcbState = .blockedOnSend e' dd → + (dd.senderIsCall = true ∨ y.tcbFault ≠ none) → dd.senderCanGrant = false ∧ dd.senderCanGrantReply = false) : + HRecvPostA st AR ar hPl.ipa n Z.Cs s a m blk canReply σ + (fun σ' => ((), σ') ∈ (specRecvSendA σ.curThread (specPtr p) (optRef rv) (specPtr sd :: q) σ).1) := by + obtain ⟨hra, hrcA, -, -⟩ := RecvReplyPinA.data hrp hrv + exact RecvPreA.recvFrontRpA J hcap hrp + (RecvPreA.recvSendA_core hA hPl J hCs h1 hmb hmax htu hslot hcov hsc hra hrcA hrv hrcap hp hk hbn hsnd hng) + +set_option maxHeartbeats 1000000 in +/-- **`RecvPreA.recvSendA` at a syscall entry.** -/ +theorem JSyscallA.recvSendA + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p badge slot rv : U64} {send grant grantReply blk canReply : Bool} {rc : capability.Cap} + (hcap : cspace.lookup_cap st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.CAP)) = + ok (.Ok (.Endpoint p badge send true grant grantReply))) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) + (hsc : capA st s slot = .Endpoint p badge send true grant grantReply) + (hrp : RecvReplyPinA (st := st) s (L.curW s) Rp canReply rc) + (hrv : (rc = .Null ∧ rv = 0#u64) ∨ ∃ g, rc = .Reply rv g) + (hrcap : ∀ r g, rc = .Reply r g → ∃ rslot ∈ OS, capA st s rslot = .Reply r g) + (hp : p ∈ E) {sd : U64} {q : List ObjRef} + (hk : σ.kheap (specPtr p) = some (.endpoint (.sendEP (specPtr sd :: q)))) + (hbn : ∀ y, σ.kheap σ.curThread = some (.tcb y) → BndOKA N (L.mem s) σ (L.curW s) y blk) + (hsnd : ∀ y xt, σ.kheap (specPtr sd) = some (.tcb y) → σ.kheap σ.curThread = some (.tcb xt) → + (y.tcbFault = none ∧ ∀ e' dd, y.tcbState = .blockedOnSend e' dd → dd.senderCanGrant = false ∨ + ipcBufOfA false y = none ∨ + unat (dataToMessageInfo (y.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0) ∨ + ∃ f, y.tcbFault = some f ∧ ((∀ b, f ≠ .timeout b) → Endp.FaultOKA (L := L) s sd (ipcBufOfA true xt) y f)) + (hng : rv ≠ 0#u64 → ∀ y, σ.kheap (specPtr sd) = some (.tcb y) → ∀ e' dd, y.tcbState = .blockedOnSend e' dd → + (dd.senderIsCall = true ∨ y.tcbFault ≠ none) → dd.senderCanGrant = false ∧ dd.senderCanGrantReply = false) : + HRecvPostA st AR ar hPl.ipa n Z.Cs s a m blk canReply σ + (fun σ' => ((), σ') ∈ (specRecvSendA σ.curThread (specPtr p) (optRef rv) (specPtr sd :: q) σ).1) := + RecvPreA.recvSendA hA hPl J.toRecvPreA hCs h1 hmb hmax htu hcap hslot hcov hsc hrp hrv hrcap hp hk hbn hsnd hng + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3RecvSendCscA.lean b/proofs/Fl4ProofsA64/P3/Stage3RecvSendCscA.lean new file mode 100644 index 000000000..fcddf6888 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3RecvSendCscA.lean @@ -0,0 +1,210 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2c): **the current SC's budget across `receive_ipc` from a waiting non-call sender +without a fault** (what `BodyOKA.ofEntry`'s `hobj` reads). + +* `CscKA σ σ'`: the current SC pointer is kept and the current SC keeps its refills and refill maximum (`BKeepA` + without the scalars). +* `refillCheckAssert_bkA`: `if_sporadic_cur_sc_assert_refill_unblock_check` is `BKeepA` (its refill write is to an SC + other than the current one). +* **`recvSend_run_cscA`**: every outcome of `specRecvSendA` is `CscKA`. The message transfer writes the receiver's + buffer (the machine state changes), so `BKeepA` does not hold across it; `CscKA` does. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3RecvSendSpecA +import Fl4ProofsA64.P3.Stage3SigBudgetA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P3 Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Classical + +noncomputable section + +/-- **The current SC's budget kept** (module docstring). -/ +structure CscKA (σ σ' : State Unit) : Prop where + csc : σ'.curSc = σ.curSc + sc : ∀ y' n, σ'.kheap σ.curSc = some (.schedContext y' n) → ∃ y, σ.kheap σ.curSc = some (.schedContext y n) ∧ + y'.scRefills = y.scRefills ∧ y'.scRefillMax = y.scRefillMax + +instance : PreO CscKA where + refl σ := ⟨rfl, fun y _ h => ⟨y, h, rfl, rfl⟩⟩ + trans h1 h2 := ⟨h2.csc.trans h1.csc, fun y'' n h => by + obtain ⟨y', h', a', b'⟩ := h2.sc y'' n (by rw [h1.csc]; exact h) + rw [h1.csc] at h' + obtain ⟨y, h0, a, b⟩ := h1.sc y' n h' + exact ⟨y, h0, a'.trans a, b'.trans b⟩⟩ + +theorem BKeepA.toCsc {σ σ' : State Unit} (h : BKeepA σ σ') : CscKA σ σ' := ⟨h.csc, h.sc⟩ + +theorem refillCheckAssert_bkA (o : Option ObjRef) : + KPresB (ifCondRefillUnblockCheck (Z := Unit) o (some false) (some true)) := by + unfold ifCondRefillUnblockCheck + cases o with + | none => exact PresR.pure _ + | some p => + intro σ r hr + simp only [NonDet.maybeM] at hr + obtain ⟨⟨sc, σ1⟩, h1, hr⟩ := mem_bindA.1 hr + have e1 : σ1 = σ := (getSchedContext_mem h1).1 + rw [e1] at hr + dsimp only at hr + rw [nd_gets_bindSc, nd_pure_bindSc] at hr + try dsimp only at hr + unfold NonDet.when at hr + split at hr + · split at hr + · rename_i c + have ne : p ≠ σ.curSc := c rfl + exact (refillUnblockCheck_onlyA σ r hr).bkeep ne + · have : r = ((), σ) := hr + rw [this]; exact PreO.refl _ + · have : r = ((), σ) := hr + rw [this]; exact PreO.refl _ + +instance (o : Option ObjRef) : KPresBInst (ifCondRefillUnblockCheck (Z := Unit) o (some false) (some true)) := + ⟨refillCheckAssert_bkA o⟩ + +instance (t : ObjRef) (ts : ThreadState) : KPresBInst (setThreadState (Z := Unit) t ts) := + ⟨by unfold setThreadState setThreadStateAct; kbres⟩ + +theorem doMachineOp_cscA {α : Type} (m : NonDet MachineState α) : PresR CscKA (doMachineOp (Z := Unit) m) := by + intro σ r hr + unfold doMachineOp at hr + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + rw [Set.mem_singleton_iff.1 hx] at hr + obtain ⟨y, hy, hr⟩ := mem_bindA.1 hr + obtain ⟨-, hy2⟩ := hy + obtain ⟨z, hz, hr⟩ := mem_bindA.1 hr + rw [Set.mem_singleton_iff.1 hz] at hr + rw [Set.mem_singleton_iff.1 hr, hy2] + exact ⟨rfl, fun y n h => ⟨y, h, rfl, rfl⟩⟩ + +theorem storeWordOffs_cscA (p : ObjRef) (x : Nat) (v : Word) : + PresR CscKA (storeWordOffs (Z := Unit) p x v) := by + intro σ r hr + unfold storeWordOffs at hr + obtain ⟨y1, hy1, hr⟩ := hr + have e1 : y1 = (σ, σ) := hy1 + subst e1 + obtain ⟨y2, hy2, hr⟩ := hr + have e2 : y2 = ((), σ) := hy2.2 + subst e2 + obtain ⟨y3, hy3, hr⟩ := hr + have e3 : y3 = (σ.machineState, σ) := hy3 + subst e3 + obtain ⟨⟨⟨a, ms'⟩, s2⟩, hy4, hr⟩ := hr + have e4 : s2 = σ := hy4.2 + subst e4 + obtain ⟨y5, hy5, hr⟩ := hr + have e5 : y5 = ((), { s2 with machineState := ms' }) := hy5 + subst e5 + have e6 : r = (a, { s2 with machineState := ms' }) := hr + rw [e6] + exact ⟨rfl, fun y n h => ⟨y, h, rfl, rfl⟩⟩ + +/-- The transfer's frame with the current SC's budget. -/ +def UCscA (B : Word → Prop) (a b : State Unit) : Prop := UFrA B a b ∧ CscKA a b + +instance (B : Word → Prop) : PreO (UCscA B) where + refl σ := ⟨PreO.refl σ, PreO.refl σ⟩ + trans h1 h2 := ⟨PreO.trans h1.1 h2.1, PreO.trans h1.2 h2.2⟩ + +section +open fl4_core_a64 Fl4ProofsA64.P1 +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +set_option maxHeartbeats 1000000 in +/-- `sched_context_update_consumed` keeps the current SC's budget. -/ +theorem updateConsumed_cscA (p : ObjRef) : PresR CscKA (schedContextUpdateConsumed (Z := Unit) p) := by + have w : ∀ c, PresR CscKA (setScObjRef scConsumedUpdate p c) := fun c s r hr => by + obtain ⟨z, n, hk, e⟩ := updateSchedContext_memA p _ hr + rw [e] + refine ⟨rfl, fun y' k h => ?_⟩ + by_cases hp : s.curSc = p + · subst hp + simp only [Function.update_self, Option.some.injEq, KernelObject.schedContext.injEq] at h + obtain ⟨rfl, rfl⟩ := h + exact ⟨z, hk, rfl, rfl⟩ + · exact ⟨y', by simpa only [Function.update_of_ne hp] using h, rfl, rfl⟩ + unfold schedContextUpdateConsumed + refine PresR.bind (fun s x hx => by rw [← getSchedContext_eqPres p s x hx]; exact PreO.refl s) fun _ => + PresR.bind (PresR.pure _) fun _ => ?_ + exact PresR.ite' (PresR.bind (w _) fun _ => PresR.pure _) (PresR.bind (w _) fun _ => PresR.pure _) + +/-- The fault branch of `do_ipc_transfer` keeps the current SC's budget. -/ +theorem doIpcTransfer_fault_cscA {σ : State Unit} {snd rcv : ObjRef} {xs xr : Tcb} + (hks : σ.kheap snd = some (.tcb xs)) (hkr : σ.kheap rcv = some (.tcb xr)) {f : Fault} + (hf : xs.tcbFault = some f) (ep : Option ObjRef) (badge : Badge) (grant : Bool) : + ∀ x ∈ (doIpcTransfer (Z := Unit) snd ep badge grant rcv σ).1, CscKA σ x.2 := by + have h : PresRC CscKA (· = σ) (doIpcTransfer (Z := Unit) snd ep badge grant rcv) := by + refine doIpcTransfer_fault_genA hks hkr hf ep badge grant ?_ + refine doFaultTransfer_genA (fun a b e => e ▸ PreO.refl a) + (fun t g σ0 r hr => ((KPresBInst.pres : KPresB (asUser (Z := Unit) t g)) σ0 r hr).toCsc) + (fun p => updateConsumed_cscA p) _ _ _ _ (fun s x regs hx => ?_) + (fun rb _ k _ v => storeWordOffs_cscA rb k v) + refine ⟨rfl, fun y' k h => ?_⟩ + by_cases hp : s.curSc = rcv + · simp only [hp, Function.update_self, Option.some.injEq, reduceCtorEq] at h + · exact ⟨y', by simpa only [Function.update_of_ne hp] using h, rfl, rfl⟩ + exact fun x hx => h σ rfl x hx + +/-- **The current SC's budget across the `SendEP` arm of `receive_ipc`** (module docstring). -/ +theorem recvSend_run_cscA {th : TypedHeapA} {kb : U64} (hPl : PlaceA th kb) (sf : S) {σ : State Unit} + (hR : StateRelA th (L.mem sf) σ) (hV : SlotsValidA th (L.mem sf)) {t sd : U64} + (htr : th t = some .tcb) {e : ObjRef} {q : List ObjRef} + {xt xs : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) (hks : σ.kheap (specPtr sd) = some (.tcb xs)) + (hal : ∀ rb, ipcBufOfA true xt = some rb → (bufAddr rb 0).toNat % 8 = 0) + (rep : Option ObjRef) {e' : ObjRef} {dd : SenderPayload} (hst : xs.tcbState = .blockedOnSend e' dd) + (hcase : XferCaseA xs dd.senderCanGrant) : + ∀ x ∈ (specRecvSendA (specPtr t) e rep (specPtr sd :: q) σ).1, CscKA σ x.2 := by + suffices H : PresRC CscKA (· = σ) (specRecvSendA (specPtr t) e rep (specPtr sd :: q)) + from fun x hx => H σ rfl x hx + refine PresRC.monoPA (P' := fun s => TcbKeepA σ s ∧ SchedFrameA σ s ∧ CscKA σ s) + (fun s h => h ▸ ⟨fun _ _ h => h, PreO.refl s, PreO.refl s⟩) ?_ + unfold specRecvSendA + refine PresRC.bind_inv (fun s hs x hx => ?_) (fun _ => ?_) + · have e0 : s = x.2 := (PresR.assert _ : PresR (@Eq (State Unit)) (NonDet.assert _)) s x hx + rw [← e0] + exact ⟨PreO.refl s, hs⟩ + refine PresRC.bind_pureA (specPtr sd) ?_ + refine PresRC.bind_inv (fun s hs x hx => ?_) (fun _ => ?_) + · have h1 : CscKA s x.2 := ((KPresBInst.pres : KPresB (tcbEpDequeue (Z := Unit) (specPtr sd) e)) s x hx).toCsc + have h2 : SchedFrameA s x.2 := SPresInst.pres s x hx + exact ⟨h1, PreO.trans hs.1 (TKInst.pres s x hx), PreO.trans hs.2.1 h2, PreO.trans hs.2.2 h1⟩ + refine PresRC.bind_valA (P := fun s => TcbKeepA σ s ∧ SchedFrameA σ s ∧ CscKA σ s) + (Q := fun rs => rs = xs.tcbState) (fun s hs x hx => ?_) (fun senderState hrs => ?_) + · have hz : x = (xs.tcbState, s) := by + unfold getThreadState at hx + exact threadGet_valA _ (hs.1 _ _ hks) hx + subst hz; exact ⟨PreO.refl s, hs, rfl⟩ + rw [hrs, hst] + dsimp only + refine PresRC.bind_pureA dd ?_ + intro s hs x hx + obtain ⟨y, hy, hx⟩ := hx + rcases hcase with ⟨hf, hcaps⟩ | ⟨f, hf⟩ + swap + · refine PreO.trans (doIpcTransfer_fault_cscA (hs.1 _ _ hks) (hs.1 _ _ hkt) hf _ _ _ y hy) + ((?_ : KPresB _) y.2 x hx).toCsc + kbres + have hS : ∀ rb, ipcBufOfA true xt = some rb → ∀ x < 128, ∀ v, + PresR (UCscA (BufBytesA (ipcBufOfA true xt))) (storeWordOffs (Z := Unit) rb x v) := by + intro rb erb x hx v σ0 r hr + have a : UFrA (BufBytesA (ipcBufOfA true xt)) σ0 r.2 := by + rw [erb]; exact storeWordOffs_ufrA rb hx v σ0 r hr + exact ⟨a, storeWordOffs_cscA rb x v σ0 r hr⟩ + have hT : UCscA (BufBytesA (ipcBufOfA true xt)) s y.2 := + doIpcTransfer_genA (R := UCscA (BufBytesA (ipcBufOfA true xt))) (fun _ _ h => h.1) + (fun t f σ0 r hr => ⟨PresR.ufrA (asUser_presA t f) σ0 r hr, + ((KPresBInst.pres : KPresB (asUser (Z := Unit) t f)) σ0 r hr).toCsc⟩) + (fun a σ0 r hr => ⟨PresR.ufrA (doMachineOp_presA _ (loadWord_keepA a)) σ0 r hr, doMachineOp_cscA _ σ0 r hr⟩) + (rbo := ipcBufOfA true xt) hS hPl sf (hR.specSchedA hs.2.1.1) hV htr (hs.1 _ _ hks) (hs.1 _ _ hkt) rfl hf hal + _ _ _ hcaps s rfl y hy + refine PreO.trans hT.2 ((?_ : KPresB _) y.2 x hx).toCsc + kbres + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3RecvSendDonA.lean b/proofs/Fl4ProofsA64/P3/Stage3RecvSendDonA.lean new file mode 100644 index 000000000..e07f9d6b3 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3RecvSendDonA.lean @@ -0,0 +1,541 @@ +/- +P3 (aarch64, A64-P3 Stage 3): **the spec facts of `receive_ipc` from a waiting call (or faulted) sender whose reply +push donates its SC to a passive receiver**: the donating forms of `recvSend_run_{sik, step, qbR, fr, sw, schedValid}A`. +`recvSend_run_cscA` has no hypothesis on the receiver's SC and applies to this case unchanged. + +* `recvSendDon_prefixA`: every outcome is a donating `reply_push` from a state `s1` reached without donation. +* `replyPushDonR_fia`: `replyPushDon_fia` without the callee being inactive (the receiver is running). + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3RecvSendSwA +import Fl4ProofsA64.P3.Stage3RecvSendCscA +import Fl4ProofsA64.P3.Stage3RecvSendQA +import Fl4ProofsA64.P3.Stage3RecvSendFrA +import Fl4ProofsA64.P3.Stage3SendDonateKeepA +import Fl4ProofsA64.P3.Stage3SendFaultValidA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P3 Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Classical + +noncomputable section + +/-- **The donating reply push** to a callee without SC, in any state, keeps `FIA` and leaves the callee's bound SC +active. -/ +theorem replyPushDonR_fia {t d r sc : ObjRef} (htd : d ≠ t) : + HT (fun σ => FIA σ ∧ RqDomPrio σ ∧ ActTcbA σ t ∧ ScIs t (some sc) σ ∧ ScIs d none σ) + (replyPush (Z := Unit) t d r true) (fun _ σ => FIA σ ∧ BAs [d] σ) := by + unfold replyPush + let P : State Unit → Prop := fun σ => FIA σ ∧ RqDomPrio σ ∧ ActTcbA σ t ∧ ScIs t (some sc) σ ∧ ScIs d none σ + refine HT.bind (Q := fun a σ => P σ ∧ a = some sc) (fun σ h z hz => ?_) fun a => ?_ + · unfold getTcbObjRef at hz + obtain ⟨x, sc', e, es, -⟩ := h.2.2.1 + have ez := threadGet_valA Tcb.tcbSchedContext e hz + rw [ez] + exact ⟨h, h.2.2.2.1 x e⟩ + refine HT.bind (Q := fun b σ => (P σ ∧ a = some sc) ∧ b = none) (fun σ h z hz => ?_) fun b => ?_ + · unfold getTcbObjRef Fl4SpecA64.threadGet at hz + obtain ⟨y, hy, hz⟩ := mem_bindA.1 hz + have hy' : y ∈ (NonDet.assertOpt (getTcb (Z := Unit) d σ) σ).1 := hy + cases hg : getTcb (Z := Unit) d σ with + | none => rw [hg] at hy'; exact absurd hy' (Set.notMem_empty _) + | some x => + rw [hg] at hy' + have e1 : y = (x, σ) := hy' + subst e1 + have e2 : z = (x.tcbSchedContext, σ) := hz + rw [e2] + exact ⟨h, h.1.2.2.2.2 x (getTcb_kheapA hg)⟩ + have keep : ∀ σ σ', TKeepA σ σ' → CellsEq σ σ' → P σ → P σ' := fun σ σ' k c h => by + obtain ⟨h1, h2, ⟨x, sc', e, es, y, n, ey, ea⟩, h4, h5⟩ := h + obtain ⟨y', ey', ea'⟩ := k.2.2.2.2.2.2.2.1 sc' y n ey + exact ⟨h1.ofTKeep k, h2.ofCells c, ⟨x, sc', (c.2 _ _).1 e, es, y', n, ey', ea'.trans ea⟩, h4.ofTKeep k, + h5.ofTKeep k⟩ + have T : ∀ {β : Type} {m : SMonad Unit β}, TPres m → CPres m → + HT (fun σ => (P σ ∧ a = some sc) ∧ b = none) m (fun _ σ => (P σ ∧ a = some sc) ∧ b = none) := + fun h1 h2 σ h z hz => ⟨⟨keep σ _ (h1 σ z hz) (h2 σ z hz) h.1.1, h.1.2⟩, h.2⟩ + refine HT.bind (T (by tpres) (by cpres)) fun _ => ?_ + refine HT.bind (Q := fun _ σ => (FIA σ ∧ RqDomPrio σ ∧ (ScIs d none σ ∧ ScActiveAtA σ sc)) ∧ + a = some sc ∧ b = none) (fun σ h z hz => ?_) fun _ => ?_ + · obtain ⟨⟨⟨h1, h2, h3, h4, h5⟩, ha⟩, hb⟩ := h + have act : ScActiveAtA σ sc := by + obtain ⟨x, sc', e, es, a⟩ := h3 + rw [h4 x e] at es; cases es; exact a + have := setThreadStateB t (.blockedOnReply r) (fun σ => ScIs d none σ ∧ ScActiveAtA σ sc) + (fun σ x hx h => by + refine ⟨fun y hy => h.1 y (by simpa [Function.update_of_ne htd] using hy), ?_⟩ + obtain ⟨y, n, e, a⟩ := h.2 + have ne : sc ≠ t := fun h => by rw [h, hx] at e; cases e + exact ⟨y, n, by simpa [Function.update_of_ne ne] using e, a⟩) + (fun _ _ h => h) σ ⟨h1, h2, h3, h5, act⟩ z hz + exact ⟨this, ha, hb⟩ + intro σ ⟨h, ha, hb⟩ z hz + subst ha hb + unfold NonDet.when at hz + rw [if_pos (by simp)] at hz + revert z hz + refine HT.atA (P := fun s => FIA s ∧ RqDomPrio s ∧ (ScIs d none s ∧ ScActiveAtA s sc)) + (Q := fun s => FIA s ∧ BAs [d] s) ?_ h + refine HT.bind (Q := fun _ s => FIA s ∧ RqDomPrio s ∧ (ScIs d none s ∧ ScActiveAtA s sc)) + (HT.ofTC' (by unfold bindScReply; tpres) (by unfold bindScReply; cpres) _ fun σ σ' k h => + ⟨h.1.ofTKeep k, (scActive_onlyFrameA sc).keep σ σ' k h.2⟩) fun _ => ?_ + intro s h z hz + have a1 := donateR_fia sc d _ (scActive_onlyFrameA sc) s ⟨h.1, h.2.2.1, Or.inl h.2.1, h.2.2.2⟩ z hz + have a2 := donate_bindsA sc d s trivial z hz + have fia : FIA z.2 := by + refine ⟨⟨a1.1.1.rq, fun e1 e2 => a1.1.1.act e1 e2 id, fun u x sc' e1 e2 e3 => ?_⟩, a1.1.2⟩ + by_cases hu : u = d + · subst u + rw [a2 x e1] at e3 + cases e3 + exact a1.2 + · exact a1.1.1.blk u x sc' e1 e2 e3 hu + refine ⟨fia, fun u hu x sc' e es => ?_⟩ + obtain rfl := List.mem_singleton.1 hu + rw [a2 x e] at es; cases es + exact ⟨x, sc, e, a2 x e, a1.2⟩ + +/-- `reply_push` leaves the caller blocked on the reply, donating or not. -/ +theorem replyPush_anyStIsA (t d r : ObjRef) (cd : Bool) : + HT (fun _ => True) (replyPush (Z := Unit) t d r cd) (fun _ σ => StIs t (.blockedOnReply r) σ) := by + unfold replyPush + refine HT.bind (Q := fun _ _ => True) (fun _ _ _ _ => trivial) fun a => ?_ + refine HT.bind (Q := fun _ _ => True) (fun _ _ _ _ => trivial) fun _ => ?_ + refine HT.bind (Q := fun _ _ => True) (fun _ _ _ _ => trivial) fun _ => ?_ + refine HT.bind (Q := fun _ σ => StIs t (.blockedOnReply r) σ) ?_ fun _ σ h z hz => ?_ + · unfold Fl4SpecA64.setThreadState + refine HT.bind (Q := fun _ σ => StIs t (.blockedOnReply r) σ) (fun σ _ z hz y hy => ?_) fun _ σ h z hz => ?_ + · obtain ⟨x, -, e⟩ := threadSet_memA _ t hz + rw [e] at hy + have : ({ x with tcbState := .blockedOnReply r } : Tcb) = y := by simpa using hy + rw [← this] + · rw [setThreadStateAct_eq] at hz + have e : z = ((), stsAct σ t) := hz + rw [e] + exact fun y hy => h y (by rwa [stsAct_kheap] at hy) + exact h.stk ((show PresR StKA _ from PresR.when (PresR.bind (bindScReply_stkA _ _) fun _ => + schedContextDonate_stkA _ _)) σ z hz) + +/-- Writing a reply object keeps the clock. -/ +theorem setReplyObjRef_tmPushA (f : (Option ObjRef → Option ObjRef) → Reply → Reply) (r : ObjRef) (v : Option ObjRef) : + TmPresA (setReplyObjRef (Z := Unit) f r v) := by + intro σ r' hr + unfold setReplyObjRef updateSkObjRef setSimpleKo at hr + obtain ⟨y, hy, hr⟩ := mem_bindA.1 hr + have e1 : σ = y.2 := (getSimpleKo_eqPres _ _ : PresR (@Eq (State Unit)) (getSimpleKo KernelObject.reply r)) σ y hy + obtain ⟨z, hz, hr⟩ := mem_bindA.1 hr + have e2 : y.2 = z.2 := (show PresR (@Eq (State Unit)) (getObject (Z := Unit) r) by + unfold getObject; exact PresR.getsThe _) y.2 z hz + obtain ⟨u, hu, hr⟩ := mem_bindA.1 hr + have e3 : z.2 = u.2 := (PresR.assert _ : PresR (@Eq (State Unit)) (NonDet.assert _)) z.2 u hu + obtain ⟨w, hw, hr⟩ := mem_bindA.1 hr + have e4 : u.2 = w.2 := (PresR.assert _ : PresR (@Eq (State Unit)) (NonDet.assert _)) u.2 w hw + rw [e1, e2, e3, e4] + exact setObject_tmA hr + +/-- `reply_push` keeps the clock, donating or not. -/ +theorem replyPush_anyTmA (a b r : ObjRef) (cd : Bool) : TmPresA (replyPush (Z := Unit) a b r cd) := by + unfold replyPush + refine PresR.bind (by tmres) (fun _ => PresR.bind (by tmres) fun _ => ?_) + refine PresR.bind (setReplyObjRef_tmPushA _ _ _) (fun _ => PresR.bind ?_ fun _ => PresR.when ?_) + · unfold Fl4SpecA64.setThreadState Fl4SpecA64.setThreadStateAct; tmres + refine PresR.bind ?_ (fun _ => by unfold schedContextDonate; tmres) + unfold bindScReply + refine PresR.bind (by unfold NonDet.liftM; exact PresR.bind (by tmres) fun _ => PresR.pure _) (fun l => ?_) + cases l with + | nil => exact PresR.bind (by tmres) fun _ => setReplyObjRef_tmPushA _ _ _ + | cons x l => exact PresR.bind (setReplyObjRef_tmPushA _ _ _) fun _ => + PresR.bind (by tmres) fun _ => setReplyObjRef_tmPushA _ _ _ + +/-- The SC binding of the TCB at `u` stays `v`. -/ +def ScKpA (u : ObjRef) (v : Option ObjRef) (a b : State Unit) : Prop := ScIs u v a → ScIs u v b + +instance (u : ObjRef) (v : Option ObjRef) : PreO (ScKpA u v) := + ⟨fun _ h => h, fun h1 h2 h => h2 (h1 h)⟩ + +/-- **A donating `reply_push` keeps the SC binding of every other thread.** -/ +theorem replyPushDon_scKeepA {σ : State Unit} {t d r sc u : ObjRef} {v : Option ObjRef} {x y : Tcb} + (hk : σ.kheap t = some (.tcb x)) (hsc : x.tcbSchedContext = some sc) (hkd : σ.kheap d = some (.tcb y)) + (hd0 : y.tcbSchedContext = none) (hth : ∀ z k, σ.kheap sc = some (.schedContext z k) → z.scTcb = some t) + (hut : u ≠ t) (hud : u ≠ d) : ∀ z ∈ (replyPush (Z := Unit) t d r true σ).1, ScKpA u v σ z.2 := by + have kh : ∀ {a b : State Unit}, b.kheap = a.kheap → ScKpA u v a b := fun e h z hz => h z (by rwa [e] at hz) + have upd : ∀ {a b : State Unit} {p : ObjRef} {o : KernelObject}, b.kheap = Function.update a.kheap p (some o) → + (p ≠ u ∨ ∀ z, o ≠ .tcb z) → ScKpA u v a b := fun {a b p o} e hp h z hz => by + rw [e] at hz + by_cases c : u = p + · subst c + rw [Function.update_self] at hz + rcases hp with hp | hp + · exact absurd rfl hp + · exact absurd (Option.some.inj hz) (hp z) + · rw [Function.update_of_ne c] at hz; exact h z hz + have ro : ∀ (f : (Option ObjRef → Option ObjRef) → Reply → Reply) p w, + PresR (ScKpA u v) (setReplyObjRef (Z := Unit) f p w) := fun f p w s z hz => by + unfold setReplyObjRef updateSkObjRef at hz + obtain ⟨q, hq, hz⟩ := mem_bindA.1 hz + have eq : s = q.2 := getSimpleKo_eqPres _ p s q hq + rw [← eq] at hz + obtain ⟨-, -, e⟩ := setSimpleKo_memA _ _ _ hz + rw [e] + exact upd rfl (Or.inr fun _ h => by cases h) + have sco : ∀ p (f : SchedContext → SchedContext), PresR (ScKpA u v) (updateSchedContext (Z := Unit) p f) := + fun p f s z hz => by + obtain ⟨q, n, -, e⟩ := updateSchedContext_memA p f hz + rw [e] + exact upd rfl (Or.inr fun _ h => by cases h) + have eqp : ∀ {α : Type} {m : SMonad Unit α}, PresR (@Eq (State Unit)) m → PresR (ScKpA u v) m := + fun h s z hz => by rw [← h s z hz]; exact PreO.refl s + have tset : ∀ p (f : Tcb → Tcb), p ≠ u → PresR (ScKpA u v) (threadSet (Z := Unit) f p) := fun p f hp s z hz => by + obtain ⟨q, -, e⟩ := threadSet_memA _ p hz + rw [e] + exact upd rfl (Or.inl hp) + intro z hz + unfold replyPush at hz + rw [nd_bind_one (getTcbObjRef_eq _ hk), nd_bind_one (getTcbObjRef_eq _ hkd), hsc, hd0] at hz + obtain ⟨u1, h1, hz⟩ := mem_bindA.1 hz + obtain ⟨u2, h2, hz⟩ := mem_bindA.1 hz + unfold NonDet.when at hz + rw [if_pos ⟨Option.some_ne_none _, rfl, rfl⟩] at hz + obtain ⟨u3, h3, hz⟩ := mem_bindA.1 hz + have k1 : ScKpA u v σ u1.2 := ro _ _ _ _ _ h1 + have k2 : ScKpA u v u1.2 u2.2 := by + unfold Fl4SpecA64.setThreadState at h2 + obtain ⟨q, hq, h2⟩ := mem_bindA.1 h2 + refine PreO.trans (tset t _ (Ne.symm hut) _ _ hq) ?_ + rw [setThreadStateAct_eq] at h2 + have e : u2 = ((), stsAct q.2 t) := h2 + rw [e]; exact kh (stsAct_kheap _ _) + have k3 : ScKpA u v u2.2 u3.2 := by + refine (?_ : PresR (ScKpA u v) (bindScReply (Z := Unit) (the (some sc)) r)) _ _ h3 + unfold bindScReply + refine PresR.bind (eqp ?_) (fun l => ?_) + · show PresR (@Eq (State Unit)) (NonDet.liftM SchedContext.scReplies (getSchedContext (Z := Unit) sc)) + unfold NonDet.liftM + exact PresR.bind (getSchedContext_eqPres sc) (fun _ => PresR.pure _) + cases l with + | nil => exact PresR.bind (sco _ _) fun _ => ro _ _ _ + | cons a l => exact PresR.bind (ro _ _ _) fun _ => PresR.bind (sco _ _) fun _ => ro _ _ _ + have th3 : ∀ w k, u3.2.kheap sc = some (.schedContext w k) → w.scTcb = some t := by + intro w k e + obtain ⟨w2, e2, t2⟩ := bindScReply_scThA _ _ _ _ h3 sc w k e + have s2 : ScThA σ u2.2 := by + refine PreO.trans (setReplyObjRef_scThA _ _ _ _ _ h1) (fun p w' k' hp => ?_) + unfold Fl4SpecA64.setThreadState at h2 + obtain ⟨q, hq, h2⟩ := mem_bindA.1 h2 + obtain ⟨q0, -, eq⟩ := threadSet_memA _ t hq + rw [setThreadStateAct_eq] at h2 + have eu : u2 = ((), stsAct q.2 t) := h2 + rw [eu, stsAct_kheap, eq] at hp + change Function.update u1.2.kheap t _ p = _ at hp + by_cases c : p = t + · rw [c, Function.update_self] at hp; cases hp + · rw [Function.update_of_ne c] at hp; exact ⟨w', hp, rfl⟩ + obtain ⟨w0, e0, t0⟩ := s2 sc w2 k e2 + rw [t2, t0]; exact hth w0 k e0 + refine PreO.trans k1 (PreO.trans k2 (PreO.trans k3 ?_)) + unfold schedContextDonate at hz + obtain ⟨⟨f, s4⟩, h4, hz⟩ := mem_bindA.1 hz + obtain ⟨hs, w, k, hkk, hv⟩ := getScObjRef_mem h4 + simp only at hs hv hz + subst hs + have ef : f = some t := hv.trans (th3 w k hkk) + subst ef + refine (?_ : PresR (ScKpA u v) _) _ _ hz + have res : PresR (ScKpA u v) (rescheduleRequired (Z := Unit)) := fun s z hz => by + obtain ⟨q, -⟩ := rescheduleRequired_memA hz + exact kh (by rw [q.1]) + refine PresR.bind (PresR.when ?_) fun _ => PresR.bind (sco _ _) fun _ => tset d _ (Ne.symm hud) + have body : PresR (ScKpA u v) (do + tcbSchedAction (Z := Unit) tcbSchedDequeue t + tcbReleaseRemove t + setTcbObjRef tcbSchedContextUpdate t none + testReschedule t) := by + refine PresR.bind (fun s z hz => kh (tcbSchedDeq_khA t s z hz)) fun _ => + PresR.bind (fun s z hz => kh (tcbReleaseRemove_khA t s z hz)) fun _ => + PresR.bind (tset t _ (Ne.symm hut)) fun _ => ?_ + unfold testReschedule + refine PresR.bind (PresR.gets _) (fun _ => PresR.bind (PresR.gets _) (fun _ => ?_)) + exact PresR.bind (PresR.pure _) (fun _ => PresR.when res) + exact PresR.bind_pure t _ body + +section +open fl4_core_a64 Fl4ProofsA64.P1 +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +set_option maxHeartbeats 1000000 in +/-- **Every outcome of the donating `SendEP` arm is a donating `reply_push`** from a state reached without +donation (module docstring). -/ +theorem recvSendDon_prefixA {i : ObjRef} {M N : ObjRef → Prop} {th : TypedHeapA} {kb : U64} (hPl : PlaceA th kb) + (sf : S) {σ : State Unit} (hR : StateRelA th (L.mem sf) σ) (hV : SlotsValidA th (L.mem sf)) {t sd : U64} + (htr : th t = some .tcb) {e : ObjRef} {q : List ObjRef} + {xt xs : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) (hks : σ.kheap (specPtr sd) = some (.tcb xs)) + (hal : ∀ rb, ipcBufOfA true xt = some rb → (bufAddr rb 0).toNat % 8 = 0) + (r : ObjRef) {e' : ObjRef} {dd : SenderPayload} (hst : xs.tcbState = .blockedOnSend e' dd) + (hcase : XferCaseA xs dd.senderCanGrant) + (hcf : dd.senderIsCall = true ∨ xs.tcbFault ≠ none) + (hg : dd.senderCanGrant = true ∨ dd.senderCanGrantReply = true) + (htmo : ¬ (xs.tcbFault.elim false isTimeoutFault = true)) {sc : ObjRef} (hsc : xs.tcbSchedContext = some sc) + (hMs : M (specPtr sd)) : + ∀ x ∈ (specRecvSendA (specPtr t) e (some r) (specPtr sd :: q) σ).1, ∃ s1, + x ∈ (replyPush (Z := Unit) (specPtr sd) (specPtr t) r true s1).1 ∧ SIKA i M N false σ s1 ∧ QKeepA σ s1 ∧ + UFrA (BufBytesA (ipcBufOfA true xt)) σ s1 ∧ LmKA σ s1 ∧ + (FIA σ → RqDomPrio σ → BAs [specPtr sd] σ → FIA s1 ∧ RqDomPrio s1 ∧ BAs [specPtr sd] s1) := by + intro x hx + unfold specRecvSendA at hx + rw [nd_bind_one (assert_true (by simp) σ), nd_pure_bind'] at hx + rw [show the (specPtr sd :: q).head? = specPtr sd from rfl] at hx + obtain ⟨u0, h0, hx⟩ := mem_bindA.1 hx + have tk0 : TcbKeepA σ u0.2 := TKInst.pres σ u0 h0 + have sf0 : SchedFrameA σ u0.2 := SPresInst.pres σ u0 h0 + have k0 : SIKA i M N false σ u0.2 := tcbEpDequeue_sikA _ _ hMs σ u0 h0 + have q0 : QKeepA σ u0.2 := tcbEpDequeue_qA _ _ σ u0 h0 + have l0 : LmKA σ u0.2 := ((show NdTmA (tcbEpDequeue (Z := Unit) (specPtr sd) e) by unfold tcbEpDequeue; tm) σ u0 h0).2.1 + have f0 : FIA σ → RqDomPrio σ → BAs [specPtr sd] σ → FIA u0.2 ∧ RqDomPrio u0.2 ∧ BAs [specPtr sd] u0.2 := + fun a b c => (HT.ofTC' (by unfold tcbEpDequeue; tpres) (by unfold tcbEpDequeue; cpres) (BAs [specPtr sd]) + (fun _ _ k h => h.ofTKeep k) : HT _ (tcbEpDequeue (Z := Unit) (specPtr sd) e) _) σ ⟨a, b, c⟩ u0 h0 + obtain ⟨u1, h1, hx⟩ := mem_bindA.1 hx + have e1 : u1 = (xs.tcbState, u0.2) := by + unfold getThreadState at h1 + exact threadGet_valA _ (tk0 _ _ hks) h1 + subst e1 + simp only [hst] at hx + obtain ⟨w, hw, hx⟩ := mem_bindA.1 hx + have ew : w = (dd, u0.2) := hw + subst ew + obtain ⟨u, hu, hx⟩ := mem_bindA.1 hx + have R0 := hR.specSchedA sf0.1 + have tT := doIpcTransfer_any_tkA hPl sf R0 hV htr (tk0 _ _ hks) (tk0 _ _ hkt) hal _ _ _ hcase u hu + have kT : SIKA i M N false u0.2 u.2 := + doIpcTransfer_any_sikA hPl sf R0 hV htr (tk0 _ _ hks) (tk0 _ _ hkt) hal _ _ _ hcase u0.2 rfl u hu + have qT : QKeepA u0.2 u.2 := + doIpcTransfer_any_qA hPl sf R0 hV htr (tk0 _ _ hks) (tk0 _ _ hkt) hal _ _ _ hcase u0.2 rfl u hu + have uT : UFrA (BufBytesA (ipcBufOfA true xt)) u0.2 u.2 := + doIpcTransfer_any_ufrA hPl sf R0 hV htr (tk0 _ _ hks) (tk0 _ _ hkt) hal _ _ _ hcase u0.2 rfl u hu + have hSt : ∀ rb, ipcBufOfA true xt = some rb → ∀ x < 128, ∀ v, + PresR (UTmA (BufBytesA (ipcBufOfA true xt))) (storeWordOffs (Z := Unit) rb x v) := by + intro rb erb x hx v σ0 r0 hr + have a : UFrA (BufBytesA (ipcBufOfA true xt)) σ0 r0.2 := by + rw [erb]; exact storeWordOffs_ufrA rb hx v σ0 r0 hr + exact ⟨a, (show NdTmA (storeWordOffs (Z := Unit) rb x v) by unfold storeWordOffs; tm) σ0 r0 hr⟩ + have lT : LmKA u0.2 u.2 := by + rcases hcase with ⟨hf, hcaps⟩ | ⟨f, hf⟩ + swap + · exact doIpcTransfer_fault_lmA (tk0 _ _ hks) (tk0 _ _ hkt) hf _ _ _ u hu + exact (doIpcTransfer_genA (R := UTmA (BufBytesA (ipcBufOfA true xt))) (fun _ _ h => h.1) + (fun t f σ0 r0 hr => ⟨PresR.ufrA (asUser_presA t f) σ0 r0 hr, asUser_tmA t f σ0 r0 hr⟩) + (fun a σ0 r0 hr => ⟨PresR.ufrA (doMachineOp_presA _ (loadWord_keepA a)) σ0 r0 hr, + doMachineOp_mtmA (loadWord_mtmA a) σ0 r0 hr⟩) + (rbo := ipcBufOfA true xt) hSt hPl sf R0 hV htr (tk0 _ _ hks) (tk0 _ _ hkt) rfl hf hal _ _ _ hcaps + u0.2 rfl u hu).2.2.1 + have k1 : SIKA i M N false σ u.2 := k0.trans kT + obtain ⟨⟨scOpt, s2⟩, h2, hx⟩ := mem_bindA.1 hx + obtain ⟨ys, hys, eys⟩ := k1.tcbScA hks + obtain ⟨eo, es⟩ := Prod.mk.inj (threadGet_valA Tcb.tcbSchedContext hys h2) + subst es + rw [eys, hsc] at eo + subst eo + obtain ⟨u3, h3, hx⟩ := mem_bindA.1 hx + have k3 : SIKA i M N false σ u3.2 := k1.trans (refillCheckAssert_sikA _ _ _ h3) + have q3 : QKeepA u.2 u3.2 := (show PresR QKeepA + (ifSporadicCurScAssertRefillUnblockCheck (Z := Unit) (some sc)) by + unfold ifSporadicCurScAssertRefillUnblockCheck; qpres) _ _ h3 + have uu : UFrA (BufBytesA (ipcBufOfA true xt)) u.2 u3.2 := PresR.ufrA + (by unfold ifSporadicCurScAssertRefillUnblockCheck ifCondRefillUnblockCheck; spres) _ _ h3 + have l3 : LmKA u.2 u3.2 := + ((TmInst.pres : TmPresA (ifSporadicCurScAssertRefillUnblockCheck (Z := Unit) (some sc))) _ _ h3).lmt + have f3 : FIA σ → RqDomPrio σ → BAs [specPtr sd] σ → FIA u3.2 ∧ RqDomPrio u3.2 ∧ BAs [specPtr sd] u3.2 := by + intro a b c + obtain ⟨a0, b0, c0⟩ := f0 a b c + have g := (HT.ofTPres (by unfold ifSporadicCurScAssertRefillUnblockCheck ifCondRefillUnblockCheck; tpres) + (BAs [specPtr sd]) (fun _ _ k h => h.ofTKeep k) : + HT _ (ifSporadicCurScAssertRefillUnblockCheck (Z := Unit) (some sc)) _) u.2 + ⟨a0.ofTKeep tT.1, c0.ofTKeep tT.1⟩ _ h3 + exact ⟨g.1, (tT.2 b0).ofCells ((show CPres (ifSporadicCurScAssertRefillUnblockCheck (Z := Unit) (some sc)) by + unfold ifSporadicCurScAssertRefillUnblockCheck ifCondRefillUnblockCheck Fl4Spec.NonDet.maybeM; cpres) _ _ h3), + g.2⟩ + obtain ⟨⟨fault, s4⟩, h4, hx⟩ := mem_bindA.1 hx + obtain ⟨yf, hyf, gf⟩ := SIKA.tcbF k3 hks + obtain ⟨ef, es⟩ := Prod.mk.inj (threadGet_valA Tcb.tcbFault hyf h4) + subst es + rw [gf.fault] at ef + subst ef + rw [if_pos hcf, if_pos ⟨hg, Option.some_ne_none r⟩] at hx + obtain ⟨yt, hyt, -⟩ := SIKA.tcbF k3 hks + rw [nd_bind_one (getTcbObjRef_eq _ hyt), nd_pure_bind'] at hx + have eyt : yt.tcbSchedContext = some sc := by + obtain ⟨z, hz, ez⟩ := k3.tcbScA hks + rw [hyt] at hz; cases hz; exact ez.trans hsc + have flag : decide (yt.tcbSchedContext ≠ none ∧ ¬ (xs.tcbFault.elim false isTimeoutFault = true)) = true := + decide_eq_true ⟨by rw [eyt]; exact Option.some_ne_none _, htmo⟩ + have hp : x ∈ (replyPush (Z := Unit) (specPtr sd) (specPtr t) r true u3.2).1 := by + rw [← flag]; exact hx + exact ⟨u3.2, hp, k3, q0.trans (qT.trans q3), PreO.trans (UFrA.of_fr sf0) (PreO.trans uT uu), + PreO.trans l0 (PreO.trans lT l3), f3⟩ + +/-- **The donating arm is `SIKA` with donation.** -/ +theorem recvSendDon_run_sikA {i : ObjRef} {M N : ObjRef → Prop} {th : TypedHeapA} {kb : U64} (hPl : PlaceA th kb) + (sf : S) {σ : State Unit} (hR : StateRelA th (L.mem sf) σ) (hV : SlotsValidA th (L.mem sf)) {t sd : U64} + (htr : th t = some .tcb) {e : ObjRef} {q : List ObjRef} + {xt xs : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) (hks : σ.kheap (specPtr sd) = some (.tcb xs)) + (hal : ∀ rb, ipcBufOfA true xt = some rb → (bufAddr rb 0).toNat % 8 = 0) + (r : ObjRef) {e' : ObjRef} {dd : SenderPayload} (hst : xs.tcbState = .blockedOnSend e' dd) + (hcase : XferCaseA xs dd.senderCanGrant) + (hcf : dd.senderIsCall = true ∨ xs.tcbFault ≠ none) + (hg : dd.senderCanGrant = true ∨ dd.senderCanGrantReply = true) + (htmo : ¬ (xs.tcbFault.elim false isTimeoutFault = true)) {sc : ObjRef} (hsc : xs.tcbSchedContext = some sc) + (hD : DonA σ (specPtr sd) xs) (hMt : M (specPtr t)) (hMs : M (specPtr sd)) + (hti : specPtr sd ≠ i) (hdi : specPtr t ≠ i) : + ∀ x ∈ (specRecvSendA (specPtr t) e (some r) (specPtr sd :: q) σ).1, SIKA i M N true σ x.2 := by + intro x hx + obtain ⟨s1, hp, k, -⟩ := recvSendDon_prefixA (i := i) (M := M) (N := N) hPl sf hR hV htr hkt hks hal r hst hcase + hcf hg htmo hsc hMs x hx + exact (k.upTo).trans (replyPush_sikA (i := i) (M := M) (N := N) hks hD hMs hMt hti hdi true s1 k x hp) + +/-- **The donating arm is `QStepA`**: the reply gains a caller, the sender's SC gains the reply. -/ +theorem recvSendDon_run_stepA {th : TypedHeapA} {kb : U64} (hPl : PlaceA th kb) + (sf : S) {σ : State Unit} (hR : StateRelA th (L.mem sf) σ) (hV : SlotsValidA th (L.mem sf)) {t sd : U64} + (htr : th t = some .tcb) {e : ObjRef} {q : List ObjRef} + {xt xs : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) (hks : σ.kheap (specPtr sd) = some (.tcb xs)) + (hal : ∀ rb, ipcBufOfA true xt = some rb → (bufAddr rb 0).toNat % 8 = 0) + (r : ObjRef) {e' : ObjRef} {dd : SenderPayload} (hst : xs.tcbState = .blockedOnSend e' dd) + (hcase : XferCaseA xs dd.senderCanGrant) + (hcf : dd.senderIsCall = true ∨ xs.tcbFault ≠ none) + (hg : dd.senderCanGrant = true ∨ dd.senderCanGrantReply = true) + (htmo : ¬ (xs.tcbFault.elim false isTimeoutFault = true)) {sc : ObjRef} (hsc : xs.tcbSchedContext = some sc) : + ∀ x ∈ (specRecvSendA (specPtr t) e (some r) (specPtr sd :: q) σ).1, QStepA (some r) (some sc) σ x.2 := by + intro x hx + obtain ⟨s1, hp, k, qk, -⟩ := recvSendDon_prefixA (i := σ.idleThread) (M := fun _ => True) (N := fun _ => False) + hPl sf hR hV htr hkt hks hal r hst hcase hcf hg htmo hsc trivial x hx + have b := replyPush_stepA (i := σ.idleThread) (M := fun _ => True) (N := fun _ => False) (d := specPtr t) (r := r) + hks true s1 k x hp + rw [if_pos rfl, hsc] at b + exact PreO.trans qk.toStep b + +/-- **The donating arm is `QBlkRA`**: the SC gaining the reply is the sender's, active. -/ +theorem recvSendDon_run_qbRA {i : ObjRef} {M N : ObjRef → Prop} {E' N' : Set ObjRef} {th : TypedHeapA} {kb : U64} + (hPl : PlaceA th kb) + (sf : S) {σ : State Unit} (hR : StateRelA th (L.mem sf) σ) (hV : SlotsValidA th (L.mem sf)) {t sd : U64} + (htr : th t = some .tcb) {e : ObjRef} {q : List ObjRef} + {xt xs : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) (hks : σ.kheap (specPtr sd) = some (.tcb xs)) + (hal : ∀ rb, ipcBufOfA true xt = some rb → (bufAddr rb 0).toNat % 8 = 0) + (r : ObjRef) {e' : ObjRef} {dd : SenderPayload} (hst : xs.tcbState = .blockedOnSend e' dd) + (hcase : XferCaseA xs dd.senderCanGrant) + (hcf : dd.senderIsCall = true ∨ xs.tcbFault ≠ none) + (hg : dd.senderCanGrant = true ∨ dd.senderCanGrantReply = true) + (htmo : ¬ (xs.tcbFault.elim false isTimeoutFault = true)) {sc : ObjRef} (hsc : xs.tcbSchedContext = some sc) + (hAct : ScActiveAtA σ sc) (hD : DonA σ (specPtr sd) xs) (hMt : M (specPtr t)) (hMs : M (specPtr sd)) + (hti : specPtr sd ≠ i) (hdi : specPtr t ≠ i) (hS : IpcSortedA E' N' σ) : + ∀ x ∈ (specRecvSendA (specPtr t) e (some r) (specPtr sd :: q) σ).1, + QBlkRA E' N' σ x.2 ∧ QStepA (some r) (some sc) σ x.2 := by + intro x hx + have K := recvSendDon_run_sikA (i := i) (M := M) (N := N) hPl sf hR hV htr hkt hks hal r hst hcase hcf hg htmo hsc + hD hMt hMs hti hdi x hx + have Q := recvSendDon_run_stepA hPl sf hR hV htr hkt hks hal r hst hcase hcf hg htmo hsc x hx + refine ⟨⟨hS.of_sub Q.prio Q.ep Q.ntfn, fun A p y' k hp hr => ?_, K.domk⟩, Q⟩ + obtain ⟨y, k0, hy, a⟩ := Q.sc p y' k hp + obtain ⟨y1, hy1, s⟩ := SIKA.scF K hy + have rm : y'.scRefillMax = y.scRefillMax := by + rw [hp] at hy1 + cases hy1 + exact s.rm + have act : scActive y = true := (a hr).elim (A p y k0 hy) fun h => by + cases h + obtain ⟨z, n, hz, az⟩ := hAct + rw [hy] at hz + cases hz + exact az + unfold scActive at act ⊢ + rw [rm]; exact act + +/-- **The frame of the donating arm**: only the receiver's buffer bytes change in user memory, and the sender is +blocked on the reply. -/ +theorem recvSendDon_run_frA {th : TypedHeapA} {kb : U64} (hPl : PlaceA th kb) + (sf : S) {σ : State Unit} (hR : StateRelA th (L.mem sf) σ) (hV : SlotsValidA th (L.mem sf)) {t sd : U64} + (htr : th t = some .tcb) {e : ObjRef} {q : List ObjRef} + {xt xs : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) (hks : σ.kheap (specPtr sd) = some (.tcb xs)) + (hal : ∀ rb, ipcBufOfA true xt = some rb → (bufAddr rb 0).toNat % 8 = 0) + (r : ObjRef) {e' : ObjRef} {dd : SenderPayload} (hst : xs.tcbState = .blockedOnSend e' dd) + (hcase : XferCaseA xs dd.senderCanGrant) + (hcf : dd.senderIsCall = true ∨ xs.tcbFault ≠ none) + (hg : dd.senderCanGrant = true ∨ dd.senderCanGrantReply = true) + (htmo : ¬ (xs.tcbFault.elim false isTimeoutFault = true)) {sc : ObjRef} (hsc : xs.tcbSchedContext = some sc) : + ∀ x ∈ (specRecvSendA (specPtr t) e (some r) (specPtr sd :: q) σ).1, + UFrA (BufBytesA (ipcBufOfA true xt)) σ x.2 ∧ StIs (specPtr sd) (.blockedOnReply r) x.2 := by + intro x hx + obtain ⟨s1, hp, -, -, u1, -⟩ := recvSendDon_prefixA (i := σ.idleThread) (M := fun _ => True) (N := fun _ => False) + hPl sf hR hV htr hkt hks hal r hst hcase hcf hg htmo hsc trivial x hx + refine ⟨PreO.trans u1 (PresR.ufrA (by unfold replyPush; spres) s1 x hp), ?_⟩ + exact replyPush_anyStIsA (specPtr sd) (specPtr t) r true s1 trivial x hp + +/-- **`SwA` and the last machine time across the donating arm.** -/ +theorem recvSendDon_run_swA {th : TypedHeapA} {kb : U64} (hPl : PlaceA th kb) + (sf : S) {σ : State Unit} (hR : StateRelA th (L.mem sf) σ) (hV : SlotsValidA th (L.mem sf)) {t sd : U64} + (htr : th t = some .tcb) {e : ObjRef} {q : List ObjRef} + {xt xs : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) (hks : σ.kheap (specPtr sd) = some (.tcb xs)) + (hal : ∀ rb, ipcBufOfA true xt = some rb → (bufAddr rb 0).toNat % 8 = 0) + (r : ObjRef) {e' : ObjRef} {dd : SenderPayload} (hst : xs.tcbState = .blockedOnSend e' dd) + (hcase : XferCaseA xs dd.senderCanGrant) + (hcf : dd.senderIsCall = true ∨ xs.tcbFault ≠ none) + (hg : dd.senderCanGrant = true ∨ dd.senderCanGrantReply = true) + (htmo : ¬ (xs.tcbFault.elim false isTimeoutFault = true)) {sc : ObjRef} (hsc : xs.tcbSchedContext = some sc) + (ht0 : xt.tcbSchedContext = none) (hD : DonA σ (specPtr sd) xs) + (hti : specPtr sd ≠ σ.idleThread) (hdi : specPtr t ≠ σ.idleThread) (hsw : SwA σ) + (hpt : ∀ u, σ.schedulerAction = .switchThread u → u ≠ specPtr t ∧ u ≠ specPtr sd) : + ∀ x ∈ (specRecvSendA (specPtr t) e (some r) (specPtr sd :: q) σ).1, + SwA x.2 ∧ x.2.machineState.lastMachineTime = σ.machineState.lastMachineTime := by + intro x hx + let M : ObjRef → Prop := fun o => o = specPtr t ∨ o = specPtr sd + obtain ⟨s1, hp, k, -, -, l1, -⟩ := recvSendDon_prefixA (i := σ.idleThread) (M := M) (N := fun _ => False) + hPl sf hR hV htr hkt hks hal r hst hcase hcf hg htmo hsc (Or.inr rfl) x hx + have g := swStepNDA k hsw (fun u e c => by rcases c with c | c; exacts [(hpt u e).1 c, (hpt u e).2 c]) + have K := replyPush_sikA (i := σ.idleThread) (M := M) (N := fun _ => False) hks hD (Or.inr rfl) (Or.inl rfl) hti hdi + true s1 k x hp + obtain ⟨ys, hys, es⟩ := k.tcbScA hks + obtain ⟨yt, hyt, et⟩ := k.tcbScA hkt + refine ⟨K.swA g.1 (fun u eu c => ?_) (fun _ u eu a b ha hb => ?_) (fun _ c => c.elim), + ((replyPush_anyTmA _ _ _ _ s1 x hp).lmt).trans l1⟩ + · rcases c with c | c + exacts [(hpt u (g.2 u eu)).1 c, (hpt u (g.2 u eu)).2 c] + · have n := hpt u (g.2 u eu) + have keep := replyPushDon_scKeepA (r := r) (v := a.tcbSchedContext) hys (es.trans hsc) hyt (et.trans ht0) + (hD.keep hsc k) n.2 n.1 x hp + exact keep (fun z hz => by rw [ha] at hz; cases hz; rfl) b hb + +/-- **`SchedValidA` across the donating arm.** -/ +theorem recvSendDon_run_schedValidA {th : TypedHeapA} {kb : U64} (hPl : PlaceA th kb) + (sf : S) {σ : State Unit} (hR : StateRelA th (L.mem sf) σ) (hV : SlotsValidA th (L.mem sf)) {t sd : U64} + (htr : th t = some .tcb) {e : ObjRef} {q : List ObjRef} + {xt xs : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) (hks : σ.kheap (specPtr sd) = some (.tcb xs)) + (hal : ∀ rb, ipcBufOfA true xt = some rb → (bufAddr rb 0).toNat % 8 = 0) + (r : ObjRef) {e' : ObjRef} {dd : SenderPayload} (hst : xs.tcbState = .blockedOnSend e' dd) + (hcase : XferCaseA xs dd.senderCanGrant) + (hcf : dd.senderIsCall = true ∨ xs.tcbFault ≠ none) + (hg : dd.senderCanGrant = true ∨ dd.senderCanGrantReply = true) + (htmo : ¬ (xs.tcbFault.elim false isTimeoutFault = true)) {sc : ObjRef} (hsc : xs.tcbSchedContext = some sc) + (ht0 : xt.tcbSchedContext = none) (hts : specPtr t ≠ specPtr sd) + (hI : SchedValidA σ) (hDom : RqDomPrio σ) : + ∀ x ∈ (specRecvSendA (specPtr t) e (some r) (specPtr sd :: q) σ).1, SchedValidA x.2 := by + intro x hx + have hFI : FIA σ := SchedValidA.toFIA hI + have hBA : BAs [specPtr sd] σ := fun u hu y c ey hs' => by + obtain rfl := List.mem_singleton.1 hu + have : y = xs := KernelObject.tcb.inj (Option.some.inj (ey.symm.trans hks)) + subst this + exact ⟨y, c, ey, hs', hFI.1.blk _ y c ey (by rw [hst]; rfl) hs'⟩ + obtain ⟨s1, hp, k, -, -, -, f⟩ := recvSendDon_prefixA (i := σ.idleThread) (M := fun _ => True) (N := fun _ => False) + hPl sf hR hV htr hkt hks hal r hst hcase hcf hg htmo hsc trivial x hx + obtain ⟨a, b, c⟩ := f hFI hDom hBA + obtain ⟨ys, hys, es⟩ := k.tcbScA hks + obtain ⟨yt, hyt, et⟩ := k.tcbScA hkt + have act : ActTcbA s1 (specPtr sd) := c _ (List.mem_singleton.2 rfl) ys sc hys (es.trans hsc) + have g := replyPushDonR_fia (r := r) (sc := sc) (d := specPtr t) (t := specPtr sd) hts s1 + ⟨a, b, act, fun z hz => by rw [hys] at hz; cases hz; exact es.trans hsc, + fun z hz => by rw [hyt] at hz; cases hz; exact et.trans ht0⟩ x hp + exact SchedValidB.toA g.1.1 g.1.2 + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3RecvSendFpA.lean b/proofs/Fl4ProofsA64/P3/Stage3RecvSendFpA.lean new file mode 100644 index 000000000..a7ecfa1ab --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3RecvSendFpA.lean @@ -0,0 +1,266 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2c): **the narrow write set of `receive_ipc` from a waiting sender without a reply push**, the twin of `EpSendFpA.send_ipc_recv_fpA` (`receive_from_senderA`'s `FpA` covers the links and block word of +every TCB). + +* `receive_from_senderCont2A`: `receive_from_senderContA` passing on the narrow write set of the dequeue and the + transfer (a new declaration; the old one is unchanged). +* **`receive_from_sender_fpA`**: FL4 writes only `RecvSFpA`: the dequeue's and the transfer's words (`RecvFpA` with + the receiver `t`), the sender's state word and scheduler words. The sender is a non-call sender without a fault on the + current core (it runs and is switched to), or a call or faulted sender (`FaultOKA`) and `rv = 0` (it becomes + `Inactive`). + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.EpRecvSendA +import Fl4ProofsA64.P3.EpSendFpA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched + +noncomputable section + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] {T E R : Set U64} + {Bf : Set (U64 × Fl4SpecA64.Word)} {SR : S → Fl4SpecA64.State Unit → Prop} + {Wr : Fl4SpecA64.State Unit → S → U64 → Prop} + +variable (st T Bf Wr) in +/-- The write set of `receive_ipc`'s `SendEP` arm for the receiver `t` and the non-call sender `sd` (module +docstring). -/ +def RecvSFpA (σ : Fl4SpecA64.State Unit) (s : S) (t e sd : U64) (b : U64) : Prop := + RecvFpA st T Bf s e t b ∨ b = stA sd ∨ Wr σ s b + +open Fl4SpecA64 in +/-- `receive_from_senderContA` with the narrow write set of the dequeue and the transfer (`RecvFpA`, the receiver +`t`) passed on. -/ +theorem receive_from_senderCont2A (hO : SchedOpsEA st T E R Bf SR) + (hP : EPlaced T E R Bf) {th : TypedHeapA} {kb : U64} (hPl : PlaceA th kb) + (s : S) (a : A) (σ : State Unit) (hR : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) + (hSt : StateRelA th (L.mem s) σ) (hV : SlotsValidA th (L.mem s)) (hC : BufCover st T Bf s σ) + (hNative : BufNativeA T (L.mem s)) + (hoff : ∀ y, IsSlotA th y → ¬ EWrite T E R Bf y ∧ ¬ EWrite T E R Bf (core.num.U64.wrapping_add y 8#u64)) + {t : U64} (ht : t ∈ T) (hth : th t = some .tcb) {xt : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hnb : tsCode xt.tcbState ≠ 4) + {e sd : U64} (he : e ∈ E) {q : List ObjRef} + (hk : σ.kheap (specPtr e) = some (.endpoint (.sendEP (specPtr sd :: q)))) + (hsh : th sd = some .tcb) {xs : Tcb} (hks : σ.kheap (specPtr sd) = some (.tcb xs)) + (hu : ∀ rp, ipcBufOfA true xt = some rp → ∀ k, k < 128 → inUserFrame (bufAddr rp k) σ) + (rv : U64) + (hcase : ∀ e' dd, xs.tcbState = .blockedOnSend e' dd → + (xs.tcbFault = none ∧ (dd.senderCanGrant = false ∨ ipcBufOfA false xs = none ∨ + unat (dataToMessageInfo (xs.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0)) ∨ + ∃ f, xs.tcbFault = some f ∧ FaultOKA (L := L) s sd (ipcBufOfA true xt) xs f) + {Post : S × A → Prop} + (hFinish : ∀ {s2 : S} {σ2 : State Unit} {dd : SenderPayload}, + sd ∈ T → t ≠ sd → specPtr sd ≠ specPtr e → + xs.tcbState = .blockedOnSend (specPtr e) dd → + EpStateRel T E R Bf (L.mem s2) σ2 → SR s2 σ2 → + WritesIn st s s2 (EWrite T E R Bf) → EpStep σ σ2 → + σ2.kheap (specPtr sd) = some (.tcb xs) → + (∀ e' ∈ E, ∀ z, σ2.kheap (specPtr e') = some (.endpoint z) → specPtr sd ∉ z.epQueue) → + (doIpcTransfer (Z := Unit) (specPtr sd) (some (specPtr e)) dd.senderBadge dd.senderCanGrant (specPtr t) + (updK σ (specPtr e) (.endpoint (epDeq (.sendEP (specPtr sd :: q)) (specPtr sd)))) = + ({((), σ2)}, False)) → + WritesIn st s s2 (RecvFpA st T Bf s e t) → + recvSendAfterTransferA (st := st) (ar := ar) s2 a t sd rv dd ⦃ Post ⦄) : + endpoint.receive_from_sender st ar s a t e rv ⦃ Post ⦄ := by + obtain ⟨-, xs0, hxs0, hq, hQ, hB⟩ := hR.at_ep he hk + obtain ⟨sd', xs', rfl⟩ := List.exists_cons_of_ne_nil hxs0 + simp only [List.map_cons, List.cons.injEq] at hq + obtain ⟨hd', hq'⟩ := hq + have hdd : sd = sd' := specPtr_inj hd' + subst hdd + have hsT : sd ∈ T := hQ.mem sd (by simp) + have hsa := hP.tcb sd hsT + have hxs := hR.at_tcb hsT hks + have hxt := hR.at_tcb ht hkt + have hs4 : (L.mem s (stA sd)).val = 4 := (hB sd (by simp)).1 + have hsb : L.mem s (tBlockA sd) = e := (hB sd (by simp)).2 + obtain ⟨e', dd, hstd⟩ := tsCode_four (hxs.st.symm.trans hs4) + have he' : e' = specPtr e := by + have := hxs.blk e' (by rw [hstd]; rfl); rw [hsb] at this; exact this.symm + subst he' + obtain ⟨hbadge, hflags⟩ := hxs.send _ _ hstd + have hts : t ≠ sd := by intro h; subst h; exact hnb (hxt.st.symm.trans hs4) + have hse : specPtr sd ≠ specPtr e := kheap_ne hks hk (fun _ h1 h2 => by subst h1; cases h2) + have hte : specPtr t ≠ specPtr e := kheap_ne hkt hk (fun _ h1 h2 => by subst h1; cases h2) + have hts' : specPtr t ≠ specPtr sd := fun h => hts (specPtr_inj h) + -- FL4: the queue's head and the dequeue + unfold endpoint.receive_from_sender + step with endpoint.get_queue.spec as ⟨q0, hq0⟩ + have hhead : q0.head = sd := by rw [hq0, epQ_eq]; exact hQ.head + rw [hhead] + have hd : specPtr sd ∈ (Endpoint.sendEP (specPtr sd :: q)).epQueue := by simp [Endpoint.epQueue] + refine WP.spec_bind (WP.spec_and (ep_dequeue_raw hP s σ hR he hk hd) + (ep_dequeue_full hP s he sd (sd :: xs') hQ (by simp))) ?_ + rintro s1 ⟨⟨-, hR1, hW1, hE1, hA1⟩, -, -, hQA, -⟩ + have hS1 := hO.stable _ _ _ _ hSR hW1 hE1 (NoBlockE.updO _ _ fun _ h => by cases h) + have hkt1 : (updK σ (specPtr e) (.endpoint (epDeq (.sendEP (specPtr sd :: q)) (specPtr sd)))).kheap (specPtr t) = + some (.tcb xt) := by rw [updK_kheap, Function.update_of_ne hte, hkt] + have hks1 : (updK σ (specPtr e) (.endpoint (epDeq (.sendEP (specPtr sd :: q)) (specPtr sd)))).kheap (specPtr sd) = + some (.tcb xs) := by rw [updK_kheap, Function.update_of_ne hse, hks] + -- the sender's payload words + have hbw : L.mem s1 (tBadgeA sd) = L.mem s (tBadgeA sd) := hA1 _ (a_badge hsa).1 ⟨sd, hsT, (a_badge hsa).2⟩ + have hfw : L.mem s1 (tFlagsA sd) = L.mem s (tFlagsA sd) := hA1 _ (a_flags hsa).1 ⟨sd, hsT, (a_flags hsa).2⟩ + step with obj.Tcb.block_badge.spec as ⟨bdg, hbdg⟩ + step with obj.Tcb.block_flags.spec as ⟨fl, hfl⟩ + simp only [lift, bind_ok] + have hg : (fl &&& endpoint.FLAG_GRANT != 0#u64) = dd.senderCanGrant := by + rw [hfl, hfw, hflags]; exact flags_grantA dd + have hgr : (fl &&& endpoint.FLAG_GRANT_REPLY != 0#u64) = dd.senderCanGrantReply := by + rw [hfl, hfw, hflags]; exact flags_grantReplyA dd + have hc : (fl &&& endpoint.FLAG_CALL != 0#u64) = dd.senderIsCall := by + rw [hfl, hfw, hflags]; exact flags_callA dd + have hbb : bdg.bv = dd.senderBadge := by rw [hbdg, hbw]; exact hbadge + rw [hg, hgr, hc] + -- the transfer's placement at the dequeued state + have hmem : ∀ y, IsSlotA th y → L.mem s1 y = L.mem s y ∧ + L.mem s1 (core.num.U64.wrapping_add y 8#u64) = L.mem s (core.num.U64.wrapping_add y 8#u64) := by + intro y hy + have hal : Aligned y := by have := (IsSlotA.al32 hPl hy).1; unfold Aligned; omega + exact ⟨hW1.2.2 y hal (hoff y hy).1, hW1.2.2 _ (wa_al hal (by simp)) (hoff y hy).2⟩ + have hSt1 := hSt.updEpA hPl hmem hk (epDeq (.sendEP (specPtr sd :: q)) (specPtr sd)) + have hV1 : SlotsValidA th (L.mem s1) := slotsValid_xferA hV hmem + have hC1 : BufCover st T Bf s1 (updK σ (specPtr e) (.endpoint (epDeq (.sendEP (specPtr sd :: q)) (specPtr sd)))) := + BufCover.ewA hP hC hW1 fun u _ x hx => by + have hne : specPtr u ≠ specPtr e := fun h => by + rw [h, updK_kheap, Function.update_self] at hx; cases hx + rwa [updK_kheap, Function.update_of_ne hne] at hx + have hu1 : ∀ rp, ipcBufOfA true xt = some rp → ∀ k, k < 128 → + inUserFrame (bufAddr rp k) (updK σ (specPtr e) (.endpoint (epDeq (.sendEP (specPtr sd :: q)) (specPtr sd)))) := + fun rp hrp k hk' => inUserFrame_updK hk (fun _ h => by cases h) _ (hu rp hrp k hk') + -- the transfer + refine WP.spec_bind (do_ipc_transfer_anyA hO hP hPl s1 a _ hR1 hS1 hSt1 hV1 hC1 (hNative.ewA hP hW1) hsT ht (Ne.symm hts) hsh hth hks1 + hkt1 hu1 hoff e bdg dd.senderCanGrant + ((hcase _ _ hstd).imp id fun ⟨f, hf, hok⟩ => ⟨f, hf, FaultOKA.ewA hP hW1 hsT hok⟩) (some (specPtr e))) ?_ + rintro s2 ⟨σ2, hT, hpost⟩ + obtain ⟨hR2, hS2, hag2, hW2, hE2, hfr2, x2, hkt2, -⟩ := hpost + have hfp : WritesIn st s s2 (RecvFpA st T Bf s e t) := + ⟨hW2.1.trans hW1.1, hW2.2.1.trans hW1.2.1, fun b hb hn => + (hag2 b hb ⟨fun h => hn (Or.inr (Or.inr (Or.inl h))), fun h => hn (Or.inr (Or.inr (Or.inr h)))⟩).trans + (hQA b hb ⟨fun h => hn (Or.inl h), fun ⟨w, hw, hl⟩ => + hn (Or.inr (Or.inl ⟨w, hQ.mem w hw, (hB w hw).2, hl⟩))⟩)⟩ + rw [hbb] at hT + have hks2 : σ2.kheap (specPtr sd) = some (.tcb xs) := by rw [hfr2 _ (Ne.symm hts'), hks1] + have hk1e : (updK σ (specPtr e) (.endpoint (epDeq (.sendEP (specPtr sd :: q)) (specPtr sd)))).kheap (specPtr e) = + some (.endpoint (epDeq (.sendEP (specPtr sd :: q)) (specPtr sd))) := by rw [updK_kheap, Function.update_self] + have hsb1 : L.mem s1 (tBlockA sd) = e := by rw [hA1 _ (a_blk hsa).1 ⟨sd, hsT, (a_blk hsa).2⟩, hsb] + have hq1 := dequeued_not_queued hR1 he hsb1 hk1e + (epDeq_not_mem (z := .sendEP (specPtr sd :: q)) (xs := sd :: xs') (by rw [hq']; rfl) hQ.nodup sd) + have hq2 : ∀ e'' ∈ E, ∀ z, σ2.kheap (specPtr e'') = some (.endpoint z) → specPtr sd ∉ z.epQueue := by + intro e'' he'' z hz + have hne : specPtr e'' ≠ specPtr t := by intro h; rw [h, hkt2] at hz; cases hz + exact hq1 e'' he'' z (by rwa [hfr2 _ hne] at hz) + exact hFinish hsT hts hse hstd hR2 hS2 (WritesIn.ew hW1 hW2) (hE1.trans hE2) hks2 hq2 hT hfp + + +open Fl4SpecA64 in +/-- **The narrow write set of `receive_from_sender`** without a reply push (module docstring): a non-call sender +without a fault, or a call / faulted sender and `rv = 0` or no grant and no grant-reply. -/ +theorem receive_from_sender_fpA (hO : SchedOpsEA st T E R Bf SR) (hF : SchedFootEA st T E R Bf SR Wr) + (hP : EPlaced T E R Bf) {th : TypedHeapA} {kb : U64} (hPl : PlaceA th kb) + (s : S) (a : A) (σ : State Unit) (hR : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) + (hSt : StateRelA th (L.mem s) σ) (hV : SlotsValidA th (L.mem s)) (hC : BufCover st T Bf s σ) + (hNative : BufNativeA T (L.mem s)) + (hoff : ∀ y, IsSlotA th y → ¬ EWrite T E R Bf y ∧ ¬ EWrite T E R Bf (core.num.U64.wrapping_add y 8#u64)) + {t : U64} (ht : t ∈ T) (hth : th t = some .tcb) {xt : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hnb : tsCode xt.tcbState ≠ 4) + {e sd : U64} (he : e ∈ E) {q : List ObjRef} + (hk : σ.kheap (specPtr e) = some (.endpoint (.sendEP (specPtr sd :: q)))) + (hsh : th sd = some .tcb) {xs : Tcb} (hks : σ.kheap (specPtr sd) = some (.tcb xs)) + (hu : ∀ rp, ipcBufOfA true xt = some rp → ∀ k, k < 128 → inUserFrame (bufAddr rp k) σ) + (rv : U64) + (hcase : ∀ e' dd, xs.tcbState = .blockedOnSend e' dd → + (xs.tcbFault = none ∧ (dd.senderCanGrant = false ∨ ipcBufOfA false xs = none ∨ + unat (dataToMessageInfo (xs.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0)) ∨ + ∃ f, xs.tcbFault = some f ∧ FaultOKA (L := L) s sd (ipcBufOfA true xt) xs f) + (hbr : ∀ e' dd, xs.tcbState = .blockedOnSend e' dd → + (dd.senderIsCall = false ∧ xs.tcbFault = none ∧ L.mem s (affA sd) = L.cpu s) ∨ + ((dd.senderIsCall = true ∨ xs.tcbFault ≠ none) ∧ + (rv = 0#u64 ∨ (dd.senderCanGrant = false ∧ dd.senderCanGrantReply = false)))) : + endpoint.receive_from_sender st ar s a t e rv ⦃ r => WritesIn st s r.1 (RecvSFpA st T Bf Wr σ s t e sd) ⦄ := by + refine receive_from_senderCont2A hO hP hPl s a σ hR hSR hSt hV hC hNative hoff ht hth hkt hnb + he hk hsh hks hu rv hcase ?_ + intro s2 σ2 dd hsT hts hse hstd hR2 hS2 hWf hE hks2 hq2 hT hfp + unfold recvSendAfterTransferA recvSendAfterCheckA + have hnr : runnable xs.tcbState = false := by rw [hstd]; rfl + have hnq2 := hO.notQueued _ _ sd xs hS2 hks2 hnr + refine WP.spec_bind (WP.spec_and (hO.unblockSporadic s2 σ2 sd xs hR2 hS2 hsT hks2 hnr hnq2) + (hF.unblockSporadic s2 σ2 sd xs hR2 hS2 hsT hks2 hnr hnq2)) ?_ + rintro s3 ⟨⟨σ3, hA, hf3, hv3, hS3⟩, hw3⟩ + have hz2 : SSzA σ σ2 := ssz_of_epStep hE + have hz3 : SSzA σ σ3 := hz2.trans (hF.unblockSz _ _ _ hA) + have h3 : WritesIn st s s3 (RecvSFpA st T Bf Wr σ s t e sd) := + WritesIn.schedA hF (hfp.mono fun _ h => Or.inl h) hz2 (B := fun _ => False) (hw3.mono fun _ h => Or.inr h) + (fun _ h => h.elim) (fun _ h => Or.inr (Or.inr h)) + have hR3 := hR2.frame hP hf3 hv3 + obtain ⟨xs3, hks3, hs3st, -, -, hs3f, -⟩ := hv3.2.2.1 _ _ hks2 + have hq3 : ∀ e'' ∈ E, ∀ z, σ3.kheap (specPtr e'') = some (.endpoint z) → specPtr sd ∉ z.epQueue := by + intro e'' he'' z hz + obtain ⟨z2, hz2', -⟩ := hR2.ep e'' he'' + rw [hv3.1 _ _ hz2'] at hz + injection hz with hz; injection hz with hz; subst hz + exact hq2 e'' he'' z2 hz2' + have hxs3 := hR3.at_tcb hsT hks3 + have hst3 : xs3.tcbState = .blockedOnSend (specPtr e) dd := hs3st.trans hstd + step with obj.Tcb.fault_type.spec as ⟨ft, hft⟩ + rcases hbr _ _ hstd with ⟨hc0, hfn, hon⟩ | ⟨hcf, hrv0⟩ + swap + · -- a call or faulted sender without a reply object becomes inactive + have htail : (dd.senderIsCall || (ft != fl4_abi.consts.fault.NullFault)) = true := by + rcases hcf with h | h + · rw [h]; rfl + · have hne : (ft != fl4_abi.consts.fault.NullFault) = true := by + rw [bne_iff_ne, hft] + intro c + exact h (hs3f.symm.trans (hxs3.fault0.1 (c.trans (by unfold fl4_abi.consts.fault.NullFault; rfl)))) + rw [hne, Bool.or_true] + have hred : ∀ {α : Type} (x y : α), (if dd.senderIsCall = true then x + else if (ft != fl4_abi.consts.fault.NullFault) = true then x else y) = x := by + intro α x y + rcases Bool.or_eq_true_iff.1 htail with h | h + · rw [if_pos h] + · by_cases c : dd.senderIsCall = true + · rw [if_pos c] + · rw [if_neg c, if_pos h] + rcases hrv0 with hrv0 | ⟨hng, hngr⟩ <;> + [simp only [hred, show (rv != 0#u64) = false by rw [hrv0]; simp, Bool.false_eq_true, ↓reduceIte, ite_self]; + simp only [hred, hng, hngr, Bool.false_eq_true, ↓reduceIte, ite_self]] + all_goals + have hNBI := NoBlockE.updT hks3 { xs3 with tcbState := .inactive } + (Or.inr (Or.inr ⟨rfl, (by rw [hst3]; rfl), (by rw [hst3]; exact fun h => by cases h)⟩)) + refine WP.spec_bind (set_thread_state_fpA hO.to0 hF.to0 hP hS3 s3 σ3 hR3 (WritesIn.refl _ _) (EpStep.refl _) hsT + hks3 hq3 .inactive (by decide) sched.TS_INACTIVE (by unfold sched.TS_INACTIVE; rfl) hNBI) fun s4 hw4 => ?_ + rw [WP.spec_ok] + exact WritesIn.schedA hF h3 hz3 hw4 (fun _ h => Or.inr (Or.inl h)) (fun _ h => Or.inr (Or.inr h)) + have hf3' : xs3.tcbFault = none := hs3f.trans hfn + have hft0 : ft = fl4_abi.consts.fault.NullFault := by + rw [hft, hxs3.fault0.2 hf3']; unfold fl4_abi.consts.fault.NullFault; rfl + simp only [hc0, hft0, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + have hcls : inClassR xs3.tcbState xs3.tcbFault := + Or.inr (Or.inr (Or.inl ⟨specPtr e, dd, hst3, fun b h => by rw [hf3'] at h; cases h⟩)) + have hNB := NoBlockE.updT hks3 { xs3 with tcbState := .running } + (Or.inr (Or.inl ⟨rfl, (by rw [hst3]; exact fun h => by cases h), Or.inl hcls⟩)) + refine WP.spec_bind (WP.spec_and + (set_thread_state_relF hO hF hP s3 σ3 hR3 hS3 hsT hks3 hq3 .running (by decide) sched.TS_RUNNING + (by unfold sched.TS_RUNNING; rfl) hNB) + (set_thread_state_fpA hO.to0 hF.to0 hP hS3 s3 σ3 hR3 (WritesIn.refl _ _) (EpStep.refl _) hsT hks3 hq3 .running + (by decide) sched.TS_RUNNING (by unfold sched.TS_RUNNING; rfl) hNB)) ?_ + rintro s4 ⟨⟨hR4, hS4, -⟩, hw4⟩ + have h4 : WritesIn st s s4 (RecvSFpA st T Bf Wr σ s t e sd) := + WritesIn.schedA hF h3 hz3 hw4 (fun _ h => Or.inr (Or.inl h)) (fun _ h => Or.inr (Or.inr h)) + obtain ⟨hk4, hq4, -, -⟩ := sts_facts hks3 hq3 .running + have hcore := (hO.coreKeep _ _ _ _ hSR hS4).onCore hsT hon + have hnq : NotQueued (stsAct (updK σ3 (specPtr sd) (.tcb { xs3 with tcbState := .running })) (specPtr sd)) + (specPtr sd) := (hO.notQueued _ _ sd xs3 hS3 hks3 (by rw [hst3]; rfl)).sts _ _ _ + refine WP.spec_bind (hF.switchTo s4 _ sd _ hR4 hS4 hsT hk4 rfl hq4 hcore hnq) fun s5 hw5 => ?_ + rw [WP.spec_ok] + exact WritesIn.schedA hF h4 (hz3.trans ((ssz_updK_tcb _ _ _).trans (ssz_stsAct _ _))) (B := fun _ => False) + (hw5.mono fun _ h => Or.inr h) (fun _ h => h.elim) (fun _ h => Or.inr (Or.inr h)) + +end + +end + +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/Stage3RecvSendFrA.lean b/proofs/Fl4ProofsA64/P3/Stage3RecvSendFrA.lean new file mode 100644 index 000000000..e2d9f6597 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3RecvSendFrA.lean @@ -0,0 +1,173 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2c): **the spec frame of `receive_ipc` from a waiting non-call sender without a +fault** (the twin of `sendIpc_recv_ufrA` and `sendIpc_recv_runningA`). + +* **`recvSend_run_frA`**: every outcome of `specRecvSendA` without a reply object is `UFrA` of the receiver's buffer + bytes, and the sender ends `Running` (non-call) or `Inactive` (call). + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3RecvSendValidA +import Fl4ProofsA64.P3.Stage3TimeoutSenderA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P3 Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Classical + +noncomputable section + +/-- The arm's tail leaves the sender `Running`. -/ +theorem recvTail_runningA (d : ObjRef) : + HT (fun _ => True) (do setThreadState (Z := Unit) d .running + possibleSwitchTo d) (fun _ σ => StIs d .running σ) := by + refine HT.bind (Q := fun _ σ => StIs d .running σ) ?_ fun _ => ?_ + · unfold Fl4SpecA64.setThreadState + refine HT.bind (Q := fun _ σ => StIs d .running σ) (fun σ _ r hr y hy => ?_) fun _ σ h r hr => ?_ + · obtain ⟨x, -, e⟩ := threadSet_memA _ d hr + rw [e] at hy + have : ({ x with tcbState := .running } : Tcb) = y := by simpa using hy + rw [← this] + · rw [setThreadStateAct_eq] at hr + have e : r = ((), stsAct σ d) := hr + rw [e] + exact fun y hy => h y (by rwa [stsAct_kheap] at hy) + intro σ h r hr y hy + exact h y (((KPresInst.pres : PresR TcbCellsEq (possibleSwitchTo (Z := Unit) d)) σ r hr).2 _ _ |>.2 hy) + +/-- The tail for a call sender without a reply object leaves the sender `Inactive`. -/ +theorem recvTailI_inactiveA (d : ObjRef) : + HT (fun _ => True) (setThreadState (Z := Unit) d .inactive) (fun _ σ => StIs d .inactive σ) := by + unfold Fl4SpecA64.setThreadState + refine HT.bind (Q := fun _ σ => StIs d .inactive σ) (fun σ _ r hr y hy => ?_) fun _ σ h r hr => ?_ + · obtain ⟨x, -, e⟩ := threadSet_memA _ d hr + rw [e] at hy + have : ({ x with tcbState := .inactive } : Tcb) = y := by simpa using hy + rw [← this] + · rw [setThreadStateAct_eq] at hr + have e : r = ((), stsAct σ d) := hr + rw [e] + exact fun y hy => h y (by rwa [stsAct_kheap] at hy) + +section +open fl4_core_a64 Fl4ProofsA64.P1 +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +set_option maxHeartbeats 1000000 in +/-- **The frame of the `SendEP` arm of `receive_ipc`** (module docstring). -/ +theorem recvSend_run_frA {th : TypedHeapA} {kb : U64} (hPl : PlaceA th kb) (sf : S) {σ : State Unit} + (hR : StateRelA th (L.mem sf) σ) (hV : SlotsValidA th (L.mem sf)) {t sd : U64} + (htr : th t = some .tcb) {e : ObjRef} {q : List ObjRef} + {xt xs : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) (hks : σ.kheap (specPtr sd) = some (.tcb xs)) + (hal : ∀ rb, ipcBufOfA true xt = some rb → (bufAddr rb 0).toNat % 8 = 0) + (rep : Option ObjRef) {e' : ObjRef} {dd : SenderPayload} (hst : xs.tcbState = .blockedOnSend e' dd) + (hsct : (dd.senderIsCall = true ∨ xs.tcbFault ≠ none) → + (dd.senderCanGrant = true ∨ dd.senderCanGrantReply = true) → rep ≠ none → + ¬ (xs.tcbFault.elim false isTimeoutFault = true) → xt.tcbSchedContext ≠ none) + (hcase : XferCaseA xs dd.senderCanGrant) : + ∀ x ∈ (specRecvSendA (specPtr t) e rep (specPtr sd :: q) σ).1, + UFrA (BufBytesA (ipcBufOfA true xt)) σ x.2 ∧ + (dd.senderIsCall = false → xs.tcbFault = none → StIs (specPtr sd) .running x.2) ∧ + (dd.senderIsCall = true ∨ xs.tcbFault ≠ none → ((dd.senderCanGrant = true ∨ dd.senderCanGrantReply = true) ∧ rep ≠ none → + ∀ r, rep = some r → StIs (specPtr sd) (.blockedOnReply r) x.2) ∧ + (¬ ((dd.senderCanGrant = true ∨ dd.senderCanGrantReply = true) ∧ rep ≠ none) → + StIs (specPtr sd) .inactive x.2)) := by + intro x hx + unfold specRecvSendA at hx + rw [nd_bind_one (assert_true (by simp) σ), nd_pure_bind'] at hx + rw [show the (specPtr sd :: q).head? = specPtr sd from rfl] at hx + revert x hx + refine HT.atA (P := fun σ' => σ' = σ) + (Q := fun s => UFrA (BufBytesA (ipcBufOfA true xt)) σ s ∧ + (dd.senderIsCall = false → xs.tcbFault = none → StIs (specPtr sd) .running s) ∧ + (dd.senderIsCall = true ∨ xs.tcbFault ≠ none → ((dd.senderCanGrant = true ∨ dd.senderCanGrantReply = true) ∧ rep ≠ none → + ∀ r, rep = some r → StIs (specPtr sd) (.blockedOnReply r) s) ∧ + (¬ ((dd.senderCanGrant = true ∨ dd.senderCanGrantReply = true) ∧ rep ≠ none) → + StIs (specPtr sd) .inactive s))) ?_ rfl + refine HT.bind (Q := fun _ s1 => TcbKeepA σ s1 ∧ UFrA (BufBytesA (ipcBufOfA true xt)) σ s1 ∧ + SIKA σ.idleThread (fun _ => True) (fun _ => False) false σ s1) (fun σ' h z hz => ?_) fun _ => ?_ + · subst σ' + exact ⟨TKInst.pres σ z hz, UFrA.of_fr (SPresInst.pres σ z hz), tcbEpDequeue_sikA _ _ trivial σ z hz⟩ + refine HT.bind (Q := fun rs s1 => (TcbKeepA σ s1 ∧ UFrA (BufBytesA (ipcBufOfA true xt)) σ s1 ∧ + SIKA σ.idleThread (fun _ => True) (fun _ => False) false σ s1) ∧ + rs = .blockedOnSend e' dd) (fun σ' h z hz => ?_) fun rs => ?_ + · have hz' : z = (xs.tcbState, σ') := by + unfold getThreadState at hz + exact threadGet_valA _ (h.1 _ _ hks) hz + subst hz' + exact ⟨h, hst⟩ + intro σ' ⟨h, ers⟩ z hz + subst ers + dsimp only at hz + obtain ⟨w, hw, hz1⟩ := mem_bindA.1 hz + clear hz + have ew : w = (dd, σ') := hw + subst ew + obtain ⟨u, hu, hz2⟩ := mem_bindA.1 hz1 + clear hz1 + have hU : UFrA (BufBytesA (ipcBufOfA true xt)) σ' u.2 := + doIpcTransfer_any_ufrA hPl sf (hR.specSchedA h.2.1.1) hV htr (h.1 _ _ hks) (h.1 _ _ hkt) hal _ _ _ hcase + σ' rfl u hu + have hS : SIKA σ.idleThread (fun _ => True) (fun _ => False) false σ' u.2 := + doIpcTransfer_any_sikA hPl sf (hR.specSchedA h.2.1.1) hV htr (h.1 _ _ hks) + (h.1 _ _ hkt) hal _ _ _ hcase σ' rfl u hu + have hNF : FaultIsA (specPtr sd) xs.tcbFault u.2 := FaultIsA.sik (fun x hx => by + rw [h.1 _ _ hks] at hx + have : xs = x := KernelObject.tcb.inj (Option.some.inj hx) + rw [← this]) hS + revert z hz2 + let K : State Unit → Prop := fun s1 => UFrA (BufBytesA (ipcBufOfA true xt)) σ s1 ∧ + SIKA σ.idleThread (fun _ => True) (fun _ => False) false σ s1 ∧ FaultIsA (specPtr sd) xs.tcbFault s1 + refine HT.atA (P := K) + (Q := fun s => UFrA (BufBytesA (ipcBufOfA true xt)) σ s ∧ + (dd.senderIsCall = false → xs.tcbFault = none → StIs (specPtr sd) .running s) ∧ + (dd.senderIsCall = true ∨ xs.tcbFault ≠ none → ((dd.senderCanGrant = true ∨ dd.senderCanGrantReply = true) ∧ rep ≠ none → + ∀ r, rep = some r → StIs (specPtr sd) (.blockedOnReply r) s) ∧ + (¬ ((dd.senderCanGrant = true ∨ dd.senderCanGrantReply = true) ∧ rep ≠ none) → + StIs (specPtr sd) .inactive s))) ?_ ⟨PreO.trans h.2.1 hU, h.2.2.trans hS, hNF⟩ + refine HT.bind (Q := fun _ s1 => K s1) (fun s1 h z hz => by + have e : z.2 = s1 := ((threadGet_eqPres _ _) s1 z hz).symm + rw [e]; exact h) fun scOpt => ?_ + refine HT.bind (Q := fun _ s1 => K s1) (fun s1 h z hz => ?_) fun _ => ?_ + · have b := refillCheckAssert_sikA (i := σ.idleThread) (M := fun _ => True) (N := fun _ => False) (don := false) + scOpt s1 z hz + have a : UFrA (BufBytesA (ipcBufOfA true xt)) s1 z.2 := PresR.ufrA + (by unfold ifSporadicCurScAssertRefillUnblockCheck ifCondRefillUnblockCheck; spres) s1 z hz + exact ⟨PreO.trans h.1 a, h.2.1.trans b, h.2.2.sik b⟩ + refine HT.bind (Q := fun fault s1 => (UFrA (BufBytesA (ipcBufOfA true xt)) σ s1 ∧ + SIKA σ.idleThread (fun _ => True) (fun _ => False) false σ s1) ∧ fault = xs.tcbFault) + (HT.threadGet _ (specPtr sd) fun s1 h x hx => ⟨⟨h.1, h.2.1⟩, h.2.2 x hx⟩) fun fault => ?_ + intro s1 ⟨⟨hU1, hK1⟩, ef⟩ z hz + subst ef + by_cases hrun : dd.senderIsCall = false ∧ xs.tcbFault = none + · rw [if_neg (by simp [hrun.1, hrun.2])] at hz + have a : UFrA (BufBytesA (ipcBufOfA true xt)) s1 z.2 := PresR.ufrA (by spres) s1 z hz + exact ⟨PreO.trans hU1 a, fun _ _ => recvTail_runningA (specPtr sd) s1 trivial z hz, + fun h => absurd h (by simp [hrun.1, hrun.2])⟩ + · have hcf : dd.senderIsCall = true ∨ xs.tcbFault ≠ none := by + cases h : dd.senderIsCall + · exact Or.inr fun c => hrun ⟨h, c⟩ + · exact Or.inl rfl + rw [if_pos hcf] at hz + by_cases hg : (dd.senderCanGrant = true ∨ dd.senderCanGrantReply = true) ∧ rep ≠ none + · rw [if_pos hg] at hz + obtain ⟨r, hr⟩ := Option.ne_none_iff_exists'.1 hg.2 + subst hr + obtain ⟨yt, hyt, eyt⟩ := hK1.tcbScA hkt + obtain ⟨ys, hys, -⟩ := SIKA.tcbF hK1 hks + rw [nd_bind_one (getTcbObjRef_eq _ hys), nd_pure_bind', + replyPush_anyDonTA hys hyt _ (fun hcd => by + rw [eyt]; exact hsct hcf hg.1 hg.2 (of_decide_eq_true hcd).2)] at hz + have a : UFrA (BufBytesA (ipcBufOfA true xt)) s1 z.2 := PresR.ufrA (by unfold replyPush; spres) s1 z hz + exact ⟨PreO.trans hU1 a, fun h1 h2 => absurd ⟨h1, h2⟩ hrun, fun _ => + ⟨fun _ r' hr' => by cases hr'; exact replyPush_noDon_stIsA _ _ _ s1 trivial z hz, + fun hn => absurd hg hn⟩⟩ + · rw [if_neg hg] at hz + have a : UFrA (BufBytesA (ipcBufOfA true xt)) s1 z.2 := PresR.ufrA (by spres) s1 z hz + exact ⟨PreO.trans hU1 a, fun h1 h2 => absurd ⟨h1, h2⟩ hrun, fun _ => + ⟨fun h => absurd h hg, fun _ => recvTailI_inactiveA (specPtr sd) s1 trivial z hz⟩⟩ + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3RecvSendQA.lean b/proofs/Fl4ProofsA64/P3/Stage3RecvSendQA.lean new file mode 100644 index 000000000..2d927ad0d --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3RecvSendQA.lean @@ -0,0 +1,180 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2c): **the queue invariants' spec side across `receive_ipc` from a waiting +non-call sender without a fault** (the twin of `sendIpc_recv_stepA` / `sendIpc_recv_qsA`). + +* **`recvSend_run_stepA`**: every outcome is `QStepA none none` (no reply gains a caller, no SC gains replies), for a + non-call sender or a call sender without a reply object (it becomes `Inactive`). +* **`recvSend_run_qsA`**: every outcome is `QSendA`. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3SendQA +import Fl4ProofsA64.P3.Stage3RecvSendSpecA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P3 Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Classical + +noncomputable section + +section +open fl4_core_a64 Fl4ProofsA64.P1 +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **Every outcome of the `SendEP` arm for a non-call sender without a fault is `QStepA rep none`.** -/ +theorem recvSend_run_stepA {i : ObjRef} {M N : ObjRef → Prop} {th : TypedHeapA} {kb : U64} (hPl : PlaceA th kb) + (sf : S) {σ : State Unit} (hR : StateRelA th (L.mem sf) σ) (hV : SlotsValidA th (L.mem sf)) {t sd : U64} + (htr : th t = some .tcb) {e : ObjRef} {q : List ObjRef} + (hk : σ.kheap e = some (.endpoint (.sendEP (specPtr sd :: q)))) + {xt xs : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) (hks : σ.kheap (specPtr sd) = some (.tcb xs)) + (hal : ∀ rb, ipcBufOfA true xt = some rb → (bufAddr rb 0).toNat % 8 = 0) + (rep : Option ObjRef) {e' : ObjRef} {dd : SenderPayload} (hst : xs.tcbState = .blockedOnSend e' dd) + (hsct : (dd.senderIsCall = true ∨ xs.tcbFault ≠ none) → + (dd.senderCanGrant = true ∨ dd.senderCanGrantReply = true) → rep ≠ none → + ¬ (xs.tcbFault.elim false isTimeoutFault = true) → xt.tcbSchedContext ≠ none) + (hcase : XferCaseA xs dd.senderCanGrant) + (hMt : M (specPtr t)) (hMs : M (specPtr sd)) (hNs : dd.senderIsCall = false → xs.tcbFault = none → N (specPtr sd)) : + ∀ x ∈ (specRecvSendA (specPtr t) e rep (specPtr sd :: q) σ).1, QStepA rep none σ x.2 := by + suffices H : PresRC (QStepA rep none) (· = σ) (specRecvSendA (specPtr t) e rep (specPtr sd :: q)) + from fun x hx => H σ rfl x hx + refine PresRC.monoPA (P' := fun s => TcbKeepA σ s ∧ SchedFrameA σ s ∧ SIKA i M N false σ s) + (fun s h => h ▸ ⟨fun _ _ h => h, PreO.refl s, SIKA.refl s⟩) ?_ + unfold specRecvSendA + refine PresRC.bind_inv (fun s hs x hx => ?_) (fun _ => ?_) + · have e0 : s = x.2 := (PresR.assert _ : PresR (@Eq (State Unit)) (NonDet.assert _)) s x hx + rw [← e0] + exact ⟨PreO.refl s, hs⟩ + refine PresRC.bind_pureA (specPtr sd) ?_ + refine PresRC.bind_inv (fun s hs x hx => ?_) (fun _ => ?_) + · have h1 := tcbEpDequeue_sikA (i := i) (M := M) (N := N) (don := false) _ e hMs s x hx + have h2 : SchedFrameA s x.2 := SPresInst.pres s x hx + exact ⟨(tcbEpDequeue_qA _ e s x hx).toStep, PreO.trans hs.1 (TKInst.pres s x hx), PreO.trans hs.2.1 h2, + hs.2.2.trans h1⟩ + refine PresRC.bind_valA (P := fun s => TcbKeepA σ s ∧ SchedFrameA σ s ∧ SIKA i M N false σ s) + (Q := fun rs => rs = xs.tcbState) (fun s hs x hx => ?_) (fun senderState hrs => ?_) + · have hz : x = (xs.tcbState, s) := by + unfold getThreadState at hx + exact threadGet_valA _ (hs.1 _ _ hks) hx + subst hz; exact ⟨PreO.refl s, hs, rfl⟩ + rw [hrs, hst] + dsimp only + refine PresRC.bind_pureA dd ?_ + refine PresRC.monoPA (P' := fun s => TcbKeepA σ s ∧ SchedFrameA σ s ∧ SIKA i M N false σ s) (fun _ h => h) ?_ + intro s hs x hx + obtain ⟨y, hy, hx⟩ := hx + have hT : SIKA i M N false s y.2 := + doIpcTransfer_any_sikA hPl sf (hR.specSchedA hs.2.1.1) hV htr (hs.1 _ _ hks) (hs.1 _ _ hkt) hal _ _ _ hcase + s rfl y hy + have hQ : QKeepA s y.2 := + doIpcTransfer_any_qA hPl sf (hR.specSchedA hs.2.1.1) hV htr (hs.1 _ _ hks) (hs.1 _ _ hkt) hal _ _ _ hcase + s rfl y hy + refine PreO.trans hQ.toStep ?_ + refine (?_ : PresRC (QStepA rep none) (SIKA i M N false σ) _) y.2 (hs.2.2.trans hT) x hx + have step : ∀ {α β : Type} {m : SMonad Unit α} {f : α → SMonad Unit β}, PresR (SIKA i M N false) m → + PresR QKeepA m → (∀ a, PresRC (QStepA rep none) (SIKA i M N false σ) (f a)) → + PresRC (QStepA rep none) (SIKA i M N false σ) (NonDet.bind m f) := + fun h1 h2 hf => PresRC.bind_inv (fun s hs x hx => ⟨(h2 s x hx).toStep, hs.trans (h1 s x hx)⟩) hf + refine step (eqPres_sikA (threadGet_eqPres _ _)) (fun s x hx => ?_) (fun scOpt => ?_) + · rw [← threadGet_eqPres _ _ s x hx]; exact QKeepA.refl s + refine step (refillCheckAssert_sikA _) (by unfold ifSporadicCurScAssertRefillUnblockCheck; qpres) (fun _ => ?_) + refine PresRC.bind_valA (Q := fun fault => fault = xs.tcbFault) (fun s hs x hx => ?_) (fun fault hfault => ?_) + · obtain ⟨y, hy, g⟩ := SIKA.tcbF hs hks + have hz := threadGet_valA Tcb.tcbFault hy hx + subst hz + exact ⟨PreO.refl s, hs, g.fault⟩ + by_cases hrun : dd.senderIsCall = false ∧ xs.tcbFault = none + · rw [if_neg (by simp [hfault, hrun.1, hrun.2])] + exact PresRC.of_pres (qk_stepA (by qpres)) + · have hcf : dd.senderIsCall = true ∨ fault ≠ none := by + cases h : dd.senderIsCall + · exact Or.inr fun c => hrun ⟨h, hfault.symm.trans c⟩ + · exact Or.inl rfl + rw [if_pos hcf] + by_cases hg : (dd.senderCanGrant = true ∨ dd.senderCanGrantReply = true) ∧ rep ≠ none + · rw [if_pos hg] + obtain ⟨r, hr⟩ := Option.ne_none_iff_exists'.1 hg.2 + subst hr + intro s hs x hx + obtain ⟨yt, hyt, eyt⟩ := SIKA.tcbScA hs hkt + obtain ⟨ys, hys, -⟩ := SIKA.tcbF hs hks + rw [nd_bind_one (getTcbObjRef_eq _ hys), nd_pure_bind', + replyPush_anyDonTA hys hyt _ (fun hcd => by + rw [eyt]; exact hsct (by rw [← hfault]; exact hcf) hg.1 hg.2 (by rw [← hfault]; exact (of_decide_eq_true hcd).2))] at hx + have := replyPush_stepA (i := i) (M := M) (N := N) (σ := σ) (t := specPtr sd) (d := specPtr t) (r := r) hks + false s hs x hx + simpa using this + · rw [if_neg hg] + exact PresRC.of_pres (qk_stepA (by qpres)) + +/-- **Every outcome of the `SendEP` arm for a non-call sender without a fault is `QSendA`.** -/ +theorem recvSend_run_qsA {i : ObjRef} {M N : ObjRef → Prop} {th : TypedHeapA} {kb : U64} (hPl : PlaceA th kb) + (sf : S) {σ : State Unit} (hR : StateRelA th (L.mem sf) σ) (hV : SlotsValidA th (L.mem sf)) {t sd : U64} + (htr : th t = some .tcb) {e : ObjRef} {q : List ObjRef} + (hk : σ.kheap e = some (.endpoint (.sendEP (specPtr sd :: q)))) + {xt xs : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) (hks : σ.kheap (specPtr sd) = some (.tcb xs)) + (hal : ∀ rb, ipcBufOfA true xt = some rb → (bufAddr rb 0).toNat % 8 = 0) + (rep : Option ObjRef) {e' : ObjRef} {dd : SenderPayload} (hst : xs.tcbState = .blockedOnSend e' dd) + (hrep : rep = none) + (hcase : XferCaseA xs dd.senderCanGrant) + (hMt : M (specPtr t)) (hMs : M (specPtr sd)) (hNs : dd.senderIsCall = false → xs.tcbFault = none → N (specPtr sd)) : + ∀ x ∈ (specRecvSendA (specPtr t) e rep (specPtr sd :: q) σ).1, QSendA σ x.2 := by + subst hrep + intro x hx + have K : SIKA i M N false σ x.2 := + recvSend_run_sikA (i := i) (M := M) (N := N) hPl sf hR hV htr hk hkt hks hal none hst + (fun _ _ h _ => absurd rfl h) hcase hMt hMs hNs x hx + have Q : QStepA none none σ x.2 := + recvSend_run_stepA (i := i) (M := M) (N := N) hPl sf hR hV htr hk hkt hks hal none hst + (fun _ _ h _ => absurd rfl h) hcase hMt hMs hNs x hx + refine ⟨Q.prio, Q.ep, Q.ntfn, fun A p y' k hp hr => ?_, fun p y hp => ?_, ⟨by rw [K.frame], by rw [K.frame], by rw [K.frame], by rw [K.frame]⟩⟩ + · obtain ⟨y, k0, hy, a⟩ := Q.sc p y' k hp + obtain ⟨y1, hy1, s⟩ := SIKA.scF K hy + have rm : y'.scRefillMax = y.scRefillMax := by + rw [hp] at hy1 + cases hy1 + exact s.rm + have act : scActive y = true := (a hr).elim (A p y k0 hy) fun h => by cases h + unfold scActive at act ⊢ + rw [rm]; exact act + · obtain ⟨z, hz, a⟩ := Q.rt p y hp + exact ⟨z, hz, fun n => a (fun h => by cases h) n⟩ + +/-- **Every outcome of the `SendEP` arm is `QBlkRA`** (and `QStepA rep none`): a call sender pushed onto an unlinked +reply object included (the reply gains a caller; `QSendA.rt` would forbid it). -/ +theorem recvSend_run_qbRA {i : ObjRef} {M N : ObjRef → Prop} {E' N' : Set ObjRef} {th : TypedHeapA} {kb : U64} + (hPl : PlaceA th kb) + (sf : S) {σ : State Unit} (hR : StateRelA th (L.mem sf) σ) (hV : SlotsValidA th (L.mem sf)) {t sd : U64} + (htr : th t = some .tcb) {e : ObjRef} {q : List ObjRef} + (hk : σ.kheap e = some (.endpoint (.sendEP (specPtr sd :: q)))) + {xt xs : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) (hks : σ.kheap (specPtr sd) = some (.tcb xs)) + (hal : ∀ rb, ipcBufOfA true xt = some rb → (bufAddr rb 0).toNat % 8 = 0) + (rep : Option ObjRef) {e' : ObjRef} {dd : SenderPayload} (hst : xs.tcbState = .blockedOnSend e' dd) + (hsct : (dd.senderIsCall = true ∨ xs.tcbFault ≠ none) → + (dd.senderCanGrant = true ∨ dd.senderCanGrantReply = true) → rep ≠ none → + ¬ (xs.tcbFault.elim false isTimeoutFault = true) → xt.tcbSchedContext ≠ none) + (hcase : XferCaseA xs dd.senderCanGrant) + (hMt : M (specPtr t)) (hMs : M (specPtr sd)) (hNs : dd.senderIsCall = false → xs.tcbFault = none → N (specPtr sd)) + (hS : IpcSortedA E' N' σ) : + ∀ x ∈ (specRecvSendA (specPtr t) e rep (specPtr sd :: q) σ).1, + QBlkRA E' N' σ x.2 ∧ QStepA rep none σ x.2 := by + intro x hx + have K : SIKA i M N false σ x.2 := + recvSend_run_sikA (i := i) (M := M) (N := N) hPl sf hR hV htr hk hkt hks hal rep hst hsct hcase hMt hMs hNs x hx + have Q : QStepA rep none σ x.2 := + recvSend_run_stepA (i := i) (M := M) (N := N) hPl sf hR hV htr hk hkt hks hal rep hst hsct hcase hMt hMs hNs x hx + refine ⟨⟨hS.of_sub Q.prio Q.ep Q.ntfn, fun A p y' k hp hr => ?_, K.domk⟩, Q⟩ + obtain ⟨y, k0, hy, a⟩ := Q.sc p y' k hp + obtain ⟨y1, hy1, s⟩ := SIKA.scF K hy + have rm : y'.scRefillMax = y.scRefillMax := by + rw [hp] at hy1 + cases hy1 + exact s.rm + have act : scActive y = true := (a hr).elim (A p y k0 hy) fun h => by cases h + unfold scActive at act ⊢ + rw [rm]; exact act + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3RecvSendSpecA.lean b/proofs/Fl4ProofsA64/P3/Stage3RecvSendSpecA.lean new file mode 100644 index 000000000..816eab674 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3RecvSendSpecA.lean @@ -0,0 +1,156 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2c): **the spec keeps across `receive_ipc` from a waiting sender** (the twin of +`sendIpc_recv_sikA` for `specRecvSendA`), for a non-call sender without a fault. + +* `refillCheckAssert_sikA`: `if_sporadic_cur_sc_assert_refill_unblock_check` is `SIKA`. +* **`recvSend_run_sikA`**: every outcome of `specRecvSendA` without a reply object for a sender without a fault, whose + caps need no transfer, is `SIKA i M N false` (`M` holds the sender and the receiver, `N` the sender when it is not a + call: a call sender becomes `Inactive` and is no switch target). + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3IpcKeepSA +import Fl4ProofsA64.P3.EpRecvSendA +import Fl4ProofsA64.P3.Stage3SendNoDonKeepA +import Fl4ProofsA64.P3.Stage3SendFaultValidA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P3 Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Classical + +noncomputable section + +/-- **A reply push to a callee that has an SC donates nothing**, whatever the donation flag. -/ +theorem replyPush_anyDonA {σ : State Unit} {t d r : ObjRef} {x y : Tcb} (ht : σ.kheap t = some (.tcb x)) + (hd : σ.kheap d = some (.tcb y)) (hsc : y.tcbSchedContext ≠ none) (cd : Bool) : + replyPush (Z := Unit) t d r cd σ = replyPush (Z := Unit) t d r false σ := by + cases cd + · rfl + · exact replyPush_calleeScA ht hd hsc + +/-- `replyPush_anyDonA` where the callee needs an SC only if the flag asks to donate. -/ +theorem replyPush_anyDonTA {σ : State Unit} {t d r : ObjRef} {x y : Tcb} (ht : σ.kheap t = some (.tcb x)) + (hd : σ.kheap d = some (.tcb y)) (cd : Bool) (hsc : cd = true → y.tcbSchedContext ≠ none) : + replyPush (Z := Unit) t d r cd σ = replyPush (Z := Unit) t d r false σ := by + cases cd + · rfl + · exact replyPush_calleeScA ht hd (hsc rfl) + +section +variable {i : ObjRef} {M N : ObjRef → Prop} {don : Bool} + +theorem refillCheckAssert_sikA (o : Option ObjRef) : + PresR (SIKA i M N don) (ifSporadicCurScAssertRefillUnblockCheck (Z := Unit) o) := by + have ruc : ∀ p, PresR (SIKA i M N don) (refillUnblockCheck (Z := Unit) p) := fun p => by + unfold refillUnblockCheck + refine PresR.bind (eqPres_sikA ?_) (fun _ => PresR.bind (eqPres_sikA (getScRefillReady_eqPres p)) (fun _ => ?_)) + · unfold isRoundRobin + exact PresR.bind (getSchedContext_eqPres p) (fun _ => PresR.pure _) + refine PresR.when ?_ + intro σ r hr + obtain ⟨ct, hct, hr⟩ := eqPres_mem (PresR.gets _) hr + have ect : ct = σ.curTime := congrArg Prod.fst (show (ct, σ) = (σ.curTime, σ) from hct) + obtain ⟨u, hu, hr⟩ := mem_bindA.1 hr + have R1 : SIKA i M N don σ u.2 := updateRefillHd_sikA _ _ (fun _ _ => by rw [ect]; exact BitVec.le_refl _) hu + have rest : PresR (SIKA i M N don) (NonDet.modify (fun s : State Unit => { s with reprogramTimer := true }) >>= fun _ => + refillHeadOverlappingLoop (Z := Unit) p) := by + refine PresR.bind (PresR.modify _ (fun σ => ?_)) (fun _ => ?_) + · exact ⟨rfl, fun _ => .same _, fun _ _ _ a => Or.inl a, fun _ a => a, Or.inl rfl, id⟩ + · unfold refillHeadOverlappingLoop + exact PresR.whileLoop (fun _ => mergeOverlappingRefills_sikA p) _ + exact R1.trans (rest u.2 r hr) + unfold ifSporadicCurScAssertRefillUnblockCheck ifCondRefillUnblockCheck + cases o with + | none => exact PresR.pure _ + | some p => + show PresR _ (_ >>= _) + refine PresR.bind (eqPres_sikA (getSchedContext_eqPres p)) (fun _ => ?_) + refine PresR.bind (PresR.gets _) (fun _ => PresR.bind (PresR.pure _) (fun _ => ?_)) + exact PresR.when (PresR.when (ruc p)) + +section +open fl4_core_a64 Fl4ProofsA64.P1 +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **Every outcome of the `SendEP` arm of `receive_ipc` for a non-call sender without a fault is `SIKA`.** -/ +theorem recvSend_run_sikA {th : TypedHeapA} {kb : U64} (hPl : PlaceA th kb) (sf : S) {σ : State Unit} + (hR : StateRelA th (L.mem sf) σ) (hV : SlotsValidA th (L.mem sf)) {t sd : U64} + (htr : th t = some .tcb) {e : ObjRef} {q : List ObjRef} + (hk : σ.kheap e = some (.endpoint (.sendEP (specPtr sd :: q)))) + {xt xs : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) (hks : σ.kheap (specPtr sd) = some (.tcb xs)) + (hal : ∀ rb, ipcBufOfA true xt = some rb → (bufAddr rb 0).toNat % 8 = 0) + (rep : Option ObjRef) {e' : ObjRef} {dd : SenderPayload} (hst : xs.tcbState = .blockedOnSend e' dd) + (hsct : (dd.senderIsCall = true ∨ xs.tcbFault ≠ none) → + (dd.senderCanGrant = true ∨ dd.senderCanGrantReply = true) → rep ≠ none → + ¬ (xs.tcbFault.elim false isTimeoutFault = true) → xt.tcbSchedContext ≠ none) + (hcase : XferCaseA xs dd.senderCanGrant) + (hMt : M (specPtr t)) (hMs : M (specPtr sd)) (hNs : dd.senderIsCall = false → xs.tcbFault = none → N (specPtr sd)) : + ∀ x ∈ (specRecvSendA (specPtr t) e rep (specPtr sd :: q) σ).1, SIKA i M N false σ x.2 := by + suffices H : PresRC (SIKA i M N false) (· = σ) (specRecvSendA (specPtr t) e rep (specPtr sd :: q)) + from fun x hx => H σ rfl x hx + refine PresRC.monoPA (P' := fun s => TcbKeepA σ s ∧ SchedFrameA σ s ∧ SIKA i M N false σ s) + (fun s h => h ▸ ⟨fun _ _ h => h, PreO.refl s, SIKA.refl s⟩) ?_ + unfold specRecvSendA + refine PresRC.bind_inv (fun s hs x hx => ?_) (fun _ => ?_) + · have e0 : s = x.2 := (PresR.assert _ : PresR (@Eq (State Unit)) (NonDet.assert _)) s x hx + rw [← e0] + exact ⟨SIKA.refl s, hs⟩ + refine PresRC.bind_pureA (specPtr sd) ?_ + refine PresRC.bind_inv (fun s hs x hx => ?_) (fun _ => ?_) + · have h1 := tcbEpDequeue_sikA (i := i) (M := M) (N := N) (don := false) _ e hMs s x hx + have h2 : SchedFrameA s x.2 := SPresInst.pres s x hx + exact ⟨h1.upTo, PreO.trans hs.1 (TKInst.pres s x hx), PreO.trans hs.2.1 h2, hs.2.2.trans h1⟩ + refine PresRC.bind_valA (P := fun s => TcbKeepA σ s ∧ SchedFrameA σ s ∧ SIKA i M N false σ s) + (Q := fun rs => rs = xs.tcbState) (fun s hs x hx => ?_) (fun senderState hrs => ?_) + · have hz : x = (xs.tcbState, s) := by + unfold getThreadState at hx + exact threadGet_valA _ (hs.1 _ _ hks) hx + subst hz; exact ⟨SIKA.refl s, hs, rfl⟩ + rw [hrs, hst] + dsimp only + refine PresRC.bind_pureA dd ?_ + refine PresRC.monoPA (P' := fun s => TcbKeepA σ s ∧ SchedFrameA σ s ∧ SIKA i M N false σ s) (fun _ h => h) ?_ + intro s hs x hx + obtain ⟨y, hy, hx⟩ := hx + have hT : SIKA i M N false s y.2 := + doIpcTransfer_any_sikA hPl sf (hR.specSchedA hs.2.1.1) hV htr (hs.1 _ _ hks) (hs.1 _ _ hkt) hal _ _ _ hcase + s rfl y hy + refine SIKA.trans hT ?_ + refine (?_ : PresRC (SIKA i M N false) (SIKA i M N false σ) _) y.2 (hs.2.2.trans hT) x hx + refine PresRC.sik_stepA (eqPres_sikA (threadGet_eqPres _ _)) (fun scOpt => ?_) + refine PresRC.sik_stepA (refillCheckAssert_sikA _) (fun _ => ?_) + refine PresRC.bind_valA (Q := fun fault => fault = xs.tcbFault) (fun s hs x hx => ?_) (fun fault hfault => ?_) + · obtain ⟨y, hy, g⟩ := SIKA.tcbF hs hks + have hz := threadGet_valA Tcb.tcbFault hy hx + subst hz + exact ⟨SIKA.refl s, hs, g.fault⟩ + by_cases hrun : dd.senderIsCall = false ∧ xs.tcbFault = none + · rw [if_neg (by simp [hfault, hrun.1, hrun.2])] + exact PresRC.of_pres (PresR.bind (setThreadState_sikA _ hMs _ (by intro h; cases h)) fun _ => + possibleSwitchTo_sikA _ (hNs hrun.1 hrun.2)) + · have hcf : dd.senderIsCall = true ∨ fault ≠ none := by + cases h : dd.senderIsCall + · exact Or.inr fun c => hrun ⟨h, hfault.symm.trans c⟩ + · exact Or.inl rfl + rw [if_pos hcf] + by_cases hg : (dd.senderCanGrant = true ∨ dd.senderCanGrantReply = true) ∧ rep ≠ none + · rw [if_pos hg] + obtain ⟨r, hr⟩ := Option.ne_none_iff_exists'.1 hg.2 + subst hr + intro s hs x hx + obtain ⟨yt, hyt, eyt⟩ := hs.tcbScA hkt + obtain ⟨ys, hys, -⟩ := SIKA.tcbF hs hks + rw [nd_bind_one (getTcbObjRef_eq _ hys), nd_pure_bind', + replyPush_anyDonTA hys hyt _ (fun hcd => by + rw [eyt]; exact hsct (by rw [← hfault]; exact hcf) hg.1 hg.2 (by rw [← hfault]; exact (of_decide_eq_true hcd).2))] at hx + exact replyPush_noDon_sikA _ _ _ hMs s x hx + · rw [if_neg hg] + exact PresRC.of_pres (setThreadState_sikA _ hMs _ (by intro h; cases h)) + +end + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3RecvSendSwA.lean b/proofs/Fl4ProofsA64/P3/Stage3RecvSendSwA.lean new file mode 100644 index 000000000..c1ab6ffff --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3RecvSendSwA.lean @@ -0,0 +1,327 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2c): **the body-end facts of `receive_ipc` from a waiting non-call sender without a +fault** that `BodyOKA` reads beyond the record: a pending switch is to a `schedulable` thread, and the last machine +time is kept. + +* `possibleSwitchTo_swRunA`: `possible_switch_to` of a runnable target whose bound SC is active keeps `SwA`. +* `recvTail_swA`: the arm's tail (`set_thread_state sender Running`, `possible_switch_to sender`) from a state with no + pending switch leaves `SwA`. +* `recvTailI_swA`: the tail for a call sender without a reply object (`set_thread_state sender Inactive`). +* **`recvSend_run_swA`**: every outcome of `specRecvSendA` is `SwA` and keeps `lastMachineTime`; with a reply object + (`rep ≠ none`) the receiver has an SC, so nothing is donated. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3RecvSendValidA +import Fl4ProofsA64.P3.Stage3RecvPreA +import Fl4ProofsA64.P3.Stage3ExitAtA +import Fl4ProofsA64.P3.Stage3SchedExitFrA +import Fl4ProofsA64.P3.DelThrTimeA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P3 Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Classical + +noncomputable section + +/-- **`possible_switch_to` keeps `SwA`** for a runnable target whose bound SC (if any) is active: it makes the target +the pending one only when it has an SC and is outside the release queue. -/ +theorem possibleSwitchTo_swRunA (target : ObjRef) : + HT (fun σ => SwA σ ∧ RunT target σ ∧ BoundActive target σ) (possibleSwitchTo (Z := Unit) target) + (fun _ σ => SwA σ) := by + unfold Fl4SpecA64.possibleSwitchTo Fl4SpecA64.getTcbObjRef + refine HT.bind (Q := fun scOpt σ => (SwA σ ∧ RunT target σ) ∧ (scOpt ≠ none → ActTcbA σ target)) + (HT.threadGet _ target fun σ hσ x hx => ⟨⟨hσ.1, hσ.2.1⟩, fun hne => ?_⟩) fun scOpt => ?_ + · cases hs : x.tcbSchedContext with + | none => exact absurd hs hne + | some sc => exact hσ.2.2 x sc hx hs + refine HT.bind (Q := fun inq σ => ((SwA σ ∧ RunT target σ) ∧ (scOpt ≠ none → ActTcbA σ target)) ∧ + inq = inReleaseQueue target σ) (HT.gets _ fun σ hσ => ⟨hσ, rfl⟩) fun inq => ?_ + refine HT.when (fun hc => ?_) (fun _ σ hσ => hσ.1.1.1) + refine HT.mono (P := fun σ => SwA σ ∧ schedulable target σ = true) ?_ (fun σ hσ => ⟨hσ.1.1.1, ?_⟩) + (fun _ _ h => h) + swap + · obtain ⟨x, sc, hx, hs, y, k, hy, ha⟩ := hσ.1.2 hc.1 + exact schedulable_ofA hx (hσ.1.1.2 x hx) hs hy (by simpa [scActive, activeSc] using ha) + (fun m => hc.2 (by rw [hσ.2]; simp [inReleaseQueue, m])) + refine HT.bind (Q := fun _ σ => SwA σ ∧ schedulable target σ = true) (HT.gets _ fun σ hσ => hσ) fun curDom => ?_ + refine HT.bind (Q := fun _ σ => SwA σ ∧ schedulable target σ = true) + (HT.threadGet _ target fun σ hσ x hx => hσ) fun targetDom => ?_ + refine HT.bind (Q := fun _ σ => SwA σ ∧ schedulable target σ = true) (HT.gets _ fun σ hσ => hσ) fun action => ?_ + have enqT : HT (fun σ => SwA σ) (tcbSchedAction (Z := Unit) tcbSchedEnqueue target) (fun _ σ => SwA σ) := + tcbSchedAction_keep _ target (fun σ => SwA σ) (fun σ g h => h) + refine HT.ite (fun _ => HT.mono enqT (fun σ h => h.1) (fun _ _ h => h)) (fun _ => ?_) + refine HT.ite (fun _ => ?_) (fun _ => ?_) + · exact HT.bind (Q := fun _ σ => SwA σ) + (HT.mono resched_choose (fun _ _ => trivial) (fun _ σ h => swa_of_choose h)) fun _ => enqT + · unfold Fl4SpecA64.setSchedulerAction + exact HT.modify _ fun σ hσ t h => by + injection h with e + subst e + exact hσ.2 + +/-- A non-donating `reply_push` keeps the times. -/ +theorem replyPush_noDon_tmA (a b r : ObjRef) : TmPresA (replyPush (Z := Unit) a b r false) := by + unfold replyPush + refine PresR.bind (by tmres) (fun _ => PresR.bind (by tmres) fun _ => ?_) + refine PresR.bind ?_ (fun _ => PresR.bind ?_ fun _ => ?_) + · intro σ r' hr + unfold setReplyObjRef updateSkObjRef setSimpleKo at hr + obtain ⟨y, hy, hr⟩ := mem_bindA.1 hr + have e1 : σ = y.2 := (getSimpleKo_eqPres _ _ : PresR (@Eq (State Unit)) (getSimpleKo KernelObject.reply r)) σ y hy + obtain ⟨z, hz, hr⟩ := mem_bindA.1 hr + have e2 : y.2 = z.2 := (show PresR (@Eq (State Unit)) (getObject (Z := Unit) r) by + unfold getObject; exact PresR.getsThe _) y.2 z hz + obtain ⟨u, hu, hr⟩ := mem_bindA.1 hr + have e3 : z.2 = u.2 := (PresR.assert _ : PresR (@Eq (State Unit)) (NonDet.assert _)) z.2 u hu + obtain ⟨v, hv, hr⟩ := mem_bindA.1 hr + have e4 : u.2 = v.2 := (PresR.assert _ : PresR (@Eq (State Unit)) (NonDet.assert _)) u.2 v hv + rw [e1, e2, e3, e4] + exact setObject_tmA hr + · unfold Fl4SpecA64.setThreadState Fl4SpecA64.setThreadStateAct; tmres + · unfold Fl4Spec.NonDet.when + rw [if_neg (by simp)] + exact PresR.pure _ + +/-- **`SwA` across a non-donating step that wakes nobody**: the pending target does not move. -/ +theorem swStepNDA {i : ObjRef} {M : ObjRef → Prop} {a b : State Unit} (R : SIKA i M (fun _ => False) false a b) + (h : SwA a) (hp : ∀ u, a.schedulerAction = .switchThread u → ¬ M u) : + SwA b ∧ ∀ u, b.schedulerAction = .switchThread u → a.schedulerAction = .switchThread u := by + refine ⟨R.swA h hp (fun c => by cases c) (fun _ c => c.elim), fun u e => ?_⟩ + rcases R.act with c | c | ⟨_, c, -⟩ + · rw [← c]; exact e + · rw [c] at e; cases e + · exact c.elim + +/-- **The arm's tail leaves `SwA`** from a state whose pending switch target is not the sender. -/ +theorem recvTail_swA (d : ObjRef) : + HT (fun σ => (FIA σ ∧ BAs [d] σ) ∧ (SwA σ ∧ ∀ u, σ.schedulerAction = .switchThread u → u ≠ d)) + (do setThreadState (Z := Unit) d .running + possibleSwitchTo d) + (fun _ σ => SwA σ) := by + refine HT.bind (Q := fun _ σ => (FIA σ ∧ BAs [d] σ) ∧ (SwA σ ∧ StIs d .running σ)) + (HT.and (HT.mono (setThreadState_fia d .running rfl (BAs [d]) + (fun σ p x x' hx h1 h => h.tcbUpd hx h1) (fun σ a h => h.act a)) (fun _ h => h.1) (fun _ _ h => h)) ?_) + fun _ => ?_ + · intro σ h r hr + have R : SIKA σ.idleThread (· = d) (fun _ => False) false σ r.2 := + setThreadState_sikA (i := σ.idleThread) (M := (· = d)) (N := fun _ => False) (don := false) d rfl .running + (fun c => by cases c) σ r hr + refine ⟨(swStepNDA R h.2.1 h.2.2).1, ?_⟩ + unfold Fl4SpecA64.setThreadState at hr + obtain ⟨y, hy, hr⟩ := mem_bindA.1 hr + obtain ⟨x, -, e⟩ := threadSet_memA _ d hy + have s : StIs d .running y.2 := by + rw [e] + intro z hz + have : ({ x with tcbState := .running } : Tcb) = z := by simpa using hz + rw [← this] + rw [setThreadStateAct_eq] at hr + have e : r = ((), stsAct y.2 d) := hr + rw [e] + exact fun z hz => s z (by rwa [stsAct_kheap] at hz) + exact HT.mono (possibleSwitchTo_swRunA d) + (fun σ h => ⟨h.2.1, fun x hx => by rw [h.2.2 x hx]; rfl, + h.1.2 d (List.mem_singleton_self d)⟩) (fun _ _ h => h) + +/-- **The tail for a call sender without a reply object leaves `SwA`**. -/ +theorem recvTailI_swA (d : ObjRef) : + HT (fun σ => SwA σ ∧ ∀ u, σ.schedulerAction = .switchThread u → u ≠ d) (setThreadState (Z := Unit) d .inactive) + (fun _ σ => SwA σ) := by + intro σ h r hr + have R : SIKA σ.idleThread (· = d) (fun _ => False) false σ r.2 := + setThreadState_sikA (i := σ.idleThread) (M := (· = d)) (N := fun _ => False) (don := false) d rfl .inactive + (fun c => by cases c) σ r hr + exact (swStepNDA R h.1 h.2).1 + +/-- The last machine time is kept. -/ +def LmKA (a b : State Unit) : Prop := b.machineState.lastMachineTime = a.machineState.lastMachineTime + +instance : PreO LmKA where + refl _ := rfl + trans h1 h2 := h2.trans h1 + +/-- The frame of the message transfer with the time kept. -/ +def UTmA (B : Word → Prop) (a b : State Unit) : Prop := UFrA B a b ∧ TmA a b + +instance (B : Word → Prop) : PreO (UTmA B) where + refl σ := ⟨PreO.refl σ, tmA_refl σ⟩ + trans h1 h2 := ⟨PreO.trans h1.1 h2.1, tmA_trans h1.2 h2.2⟩ + +theorem loadWord_mtmA (a : Word) : MTmA (loadWord a) := MTmA.ro fun ms x hx => by + obtain ⟨y1, hy1, hx⟩ := hx + have e1 : y1 = (ms.underlyingMemory, ms) := hy1 + subst e1 + obtain ⟨y2, hy2, hx⟩ := hx + have e2 : y2 = ((), ms) := hy2.2 + subst e2 + rw [mem_pureA hx] + +section +open fl4_core_a64 Fl4ProofsA64.P1 +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +set_option maxHeartbeats 1000000 in +/-- **`do_ipc_transfer`** (both cases) keeps the last machine time (the fault branch). -/ +theorem doIpcTransfer_fault_lmA {σ : State Unit} {snd rcv : ObjRef} {xs xr : Tcb} + (hks : σ.kheap snd = some (.tcb xs)) (hkr : σ.kheap rcv = some (.tcb xr)) {f : Fault} + (hf : xs.tcbFault = some f) (ep : Option ObjRef) (badge : Badge) (grant : Bool) : + ∀ x ∈ (doIpcTransfer (Z := Unit) snd ep badge grant rcv σ).1, LmKA σ x.2 := by + have h : PresRC LmKA (· = σ) (doIpcTransfer (Z := Unit) snd ep badge grant rcv) := by + refine doIpcTransfer_fault_genA hks hkr hf ep badge grant ?_ + refine doFaultTransfer_genA (fun a b e => e ▸ PreO.refl a) + (fun t g σ0 r hr => (asUser_tmA t g σ0 r hr).2.1) (fun p σ0 r hr => ?_) _ _ _ _ + (fun _ _ _ _ => rfl) (fun rb _ k _ v σ0 r hr => + ((show NdTmA (storeWordOffs (Z := Unit) rb k v) by unfold storeWordOffs; tm) σ0 r hr).2.1) + have tm : TmPresA (schedContextUpdateConsumed (Z := Unit) p) := by + unfold schedContextUpdateConsumed; tmres + exact (tm σ0 r hr).lmt + exact fun x hx => h σ rfl x hx + +/-- **`SwA` and the last machine time across the `SendEP` arm of `receive_ipc`** (module docstring). -/ +theorem recvSend_run_swA {th : TypedHeapA} {kb : U64} (hPl : PlaceA th kb) (sf : S) {σ : State Unit} + (hR : StateRelA th (L.mem sf) σ) (hV : SlotsValidA th (L.mem sf)) {t sd : U64} + (htr : th t = some .tcb) {e : ObjRef} {q : List ObjRef} + (hk : σ.kheap e = some (.endpoint (.sendEP (specPtr sd :: q)))) + {xt xs : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) (hks : σ.kheap (specPtr sd) = some (.tcb xs)) + (hal : ∀ rb, ipcBufOfA true xt = some rb → (bufAddr rb 0).toNat % 8 = 0) + (rep : Option ObjRef) {e' : ObjRef} {dd : SenderPayload} (hst : xs.tcbState = .blockedOnSend e' dd) + (hsct : (dd.senderIsCall = true ∨ xs.tcbFault ≠ none) → + (dd.senderCanGrant = true ∨ dd.senderCanGrantReply = true) → rep ≠ none → + ¬ (xs.tcbFault.elim false isTimeoutFault = true) → xt.tcbSchedContext ≠ none) + (hcase : XferCaseA xs dd.senderCanGrant) + (hI : SchedValidA σ) (hD : RqDomPrio σ) (hsw : SwA σ) + (hpt : ∀ u, σ.schedulerAction = .switchThread u → u ≠ specPtr t ∧ u ≠ specPtr sd) : + ∀ x ∈ (specRecvSendA (specPtr t) e rep (specPtr sd :: q) σ).1, + SwA x.2 ∧ x.2.machineState.lastMachineTime = σ.machineState.lastMachineTime := by + have hFI : FIA σ := SchedValidA.toFIA hI + have hBA : BAs [specPtr sd] σ := fun u hu x sc ex hs' => by + obtain rfl := List.mem_singleton.1 hu + have : x = xs := KernelObject.tcb.inj (Option.some.inj (ex.symm.trans hks)) + subst this + exact ⟨x, sc, ex, hs', hFI.1.blk _ x sc ex (by rw [hst]; rfl) hs'⟩ + have nsw : ∀ s1, SIKA σ.idleThread (fun o => o = specPtr t ∨ o = specPtr sd) (fun _ => False) false σ s1 → + SwA s1 ∧ ∀ u, s1.schedulerAction = .switchThread u → u ≠ specPtr sd := fun s1 k => by + have g := swStepNDA k hsw (fun u e c => by rcases c with c | c; exacts [(hpt u e).1 c, (hpt u e).2 c]) + exact ⟨g.1, fun u e => (hpt u (g.2 u e)).2⟩ + intro x hx + unfold specRecvSendA at hx + rw [nd_bind_one (assert_true (by simp) σ), nd_pure_bind'] at hx + rw [show the (specPtr sd :: q).head? = specPtr sd from rfl] at hx + revert x hx + refine HT.atA (P := fun σ' => σ' = σ) + (Q := fun s => SwA s ∧ s.machineState.lastMachineTime = σ.machineState.lastMachineTime) ?_ rfl + refine HT.bind (Q := fun _ s1 => FIA s1 ∧ RqDomPrio s1 ∧ BAs [specPtr sd] s1 ∧ TcbKeepA σ s1 ∧ + SchedFrameA σ s1 ∧ SIKA σ.idleThread (fun o => o = specPtr t ∨ o = specPtr sd) (fun _ => False) false σ s1 ∧ LmKA σ s1) + (fun σ' h z hz => ?_) fun _ => ?_ + · subst σ' + have h1 := (HT.ofTC' (by unfold tcbEpDequeue; tpres) (by unfold tcbEpDequeue; cpres) (BAs [specPtr sd]) + (fun _ _ k h => h.ofTKeep k) : + HT _ (tcbEpDequeue (Z := Unit) (specPtr sd) e) _) σ ⟨hFI, hD, hBA⟩ z hz + exact ⟨h1.1, h1.2.1, h1.2.2, TKInst.pres σ z hz, SPresInst.pres σ z hz, tcbEpDequeue_sikA _ _ (Or.inr rfl) σ z hz, + ((show NdTmA (tcbEpDequeue (Z := Unit) (specPtr sd) e) by unfold tcbEpDequeue; tm) σ z hz).2.1⟩ + refine HT.bind (Q := fun rs s1 => (FIA s1 ∧ RqDomPrio s1 ∧ BAs [specPtr sd] s1 ∧ TcbKeepA σ s1 ∧ + SchedFrameA σ s1 ∧ SIKA σ.idleThread (fun o => o = specPtr t ∨ o = specPtr sd) (fun _ => False) false σ s1 ∧ LmKA σ s1) ∧ + rs = .blockedOnSend e' dd) (fun σ' h z hz => ?_) fun rs => ?_ + · have hz' : z = (xs.tcbState, σ') := by + unfold getThreadState at hz + exact threadGet_valA _ (h.2.2.2.1 _ _ hks) hz + subst hz' + exact ⟨h, hst⟩ + intro σ' ⟨h, ers⟩ z hz + subst ers + dsimp only at hz + obtain ⟨w, hw, hz1⟩ := mem_bindA.1 hz + clear hz + have ew : w = (dd, σ') := hw + subst ew + obtain ⟨u, hu, hz2⟩ := mem_bindA.1 hz1 + clear hz1 + have hT := doIpcTransfer_any_tkA hPl sf (hR.specSchedA h.2.2.2.2.1.1) hV htr (h.2.2.2.1 _ _ hks) + (h.2.2.2.1 _ _ hkt) hal _ _ _ hcase u hu + have hS : SIKA σ.idleThread (fun o => o = specPtr t ∨ o = specPtr sd) (fun _ => False) false σ' u.2 := + doIpcTransfer_any_sikA hPl sf (hR.specSchedA h.2.2.2.2.1.1) hV htr (h.2.2.2.1 _ _ hks) + (h.2.2.2.1 _ _ hkt) hal _ _ _ hcase σ' rfl u hu + have hSt : ∀ rb, ipcBufOfA true xt = some rb → ∀ x < 128, ∀ v, + PresR (UTmA (BufBytesA (ipcBufOfA true xt))) (storeWordOffs (Z := Unit) rb x v) := by + intro rb erb x hx v σ0 r hr + have a : UFrA (BufBytesA (ipcBufOfA true xt)) σ0 r.2 := by + rw [erb]; exact storeWordOffs_ufrA rb hx v σ0 r hr + exact ⟨a, (show NdTmA (storeWordOffs (Z := Unit) rb x v) by unfold storeWordOffs; tm) σ0 r hr⟩ + have hLm : LmKA σ' u.2 := by + rcases hcase with ⟨hf, hcaps⟩ | ⟨f, hf⟩ + swap + · exact doIpcTransfer_fault_lmA (h.2.2.2.1 _ _ hks) (h.2.2.2.1 _ _ hkt) hf _ _ _ u hu + exact (doIpcTransfer_genA (R := UTmA (BufBytesA (ipcBufOfA true xt))) (fun _ _ h => h.1) + (fun t f σ0 r hr => ⟨PresR.ufrA (asUser_presA t f) σ0 r hr, asUser_tmA t f σ0 r hr⟩) + (fun a σ0 r hr => ⟨PresR.ufrA (doMachineOp_presA _ (loadWord_keepA a)) σ0 r hr, + doMachineOp_mtmA (loadWord_mtmA a) σ0 r hr⟩) + (rbo := ipcBufOfA true xt) hSt hPl sf (hR.specSchedA h.2.2.2.2.1.1) hV htr (h.2.2.2.1 _ _ hks) + (h.2.2.2.1 _ _ hkt) rfl hf hal _ _ _ hcaps σ' rfl u hu).2.2.1 + have hNF : FaultIsA (specPtr sd) xs.tcbFault u.2 := FaultIsA.sik (fun x hx => by + rw [h.2.2.2.1 _ _ hks] at hx + have : xs = x := KernelObject.tcb.inj (Option.some.inj hx) + rw [← this]) hS + revert z hz2 + let K : State Unit → Prop := fun s1 => (FIA s1 ∧ BAs [specPtr sd] s1) ∧ + SIKA σ.idleThread (fun o => o = specPtr t ∨ o = specPtr sd) (fun _ => False) false σ s1 ∧ FaultIsA (specPtr sd) xs.tcbFault s1 ∧ LmKA σ s1 + refine HT.atA (P := K) + (Q := fun s => SwA s ∧ s.machineState.lastMachineTime = σ.machineState.lastMachineTime) ?_ + ⟨⟨h.1.ofTKeep hT.1, h.2.2.1.ofTKeep hT.1⟩, h.2.2.2.2.2.1.trans hS, hNF, PreO.trans h.2.2.2.2.2.2 hLm⟩ + refine HT.bind (Q := fun _ s1 => K s1) (fun s1 h z hz => by + have e : z.2 = s1 := ((threadGet_eqPres _ _) s1 z hz).symm + rw [e]; exact h) fun scOpt => ?_ + refine HT.bind (Q := fun _ s1 => K s1) (fun s1 h z hz => ?_) fun _ => ?_ + · have a := (HT.ofTPres (by unfold ifSporadicCurScAssertRefillUnblockCheck ifCondRefillUnblockCheck; tpres) + (BAs [specPtr sd]) (fun _ _ k h => h.ofTKeep k) : + HT _ (ifSporadicCurScAssertRefillUnblockCheck (Z := Unit) scOpt) _) s1 ⟨h.1.1, h.1.2⟩ z hz + have b := refillCheckAssert_sikA (i := σ.idleThread) (M := fun o => o = specPtr t ∨ o = specPtr sd) (N := fun _ => False) (don := false) + scOpt s1 z hz + have c : LmKA s1 z.2 := + ((TmInst.pres : TmPresA (ifSporadicCurScAssertRefillUnblockCheck (Z := Unit) scOpt)) s1 z hz).lmt + exact ⟨⟨a.1, a.2⟩, h.2.1.trans b, h.2.2.1.sik b, PreO.trans h.2.2.2 c⟩ + refine HT.bind (Q := fun fault s1 => (((FIA s1 ∧ BAs [specPtr sd] s1) ∧ + (SwA s1 ∧ ∀ u, s1.schedulerAction = .switchThread u → u ≠ specPtr sd) ∧ LmKA σ s1) ∧ + SIKA σ.idleThread (fun o => o = specPtr t ∨ o = specPtr sd) (fun _ => False) false σ s1) ∧ fault = xs.tcbFault) + (HT.threadGet _ (specPtr sd) fun s1 h x hx => ⟨⟨⟨h.1, nsw s1 h.2.1, h.2.2.2⟩, h.2.1⟩, h.2.2.1 x hx⟩) fun fault => ?_ + intro s1 ⟨⟨hFB, hK1⟩, ef⟩ z hz + subst ef + by_cases hrun : dd.senderIsCall = false ∧ xs.tcbFault = none + · rw [if_neg (by simp [hrun.1, hrun.2])] at hz + have tl : PresR LmKA (setThreadState (Z := Unit) (specPtr sd) .running >>= fun _ => + possibleSwitchTo (specPtr sd)) := + PresR.bind (fun σ0 r hr => ((show TmPresA (setThreadState (Z := Unit) (specPtr sd) .running) by + unfold Fl4SpecA64.setThreadState Fl4SpecA64.setThreadStateAct; tmres) σ0 r hr).lmt) + (fun _ σ0 r hr => ((TmInst.pres : TmPresA (possibleSwitchTo (Z := Unit) (specPtr sd))) σ0 r hr).lmt) + exact ⟨recvTail_swA (specPtr sd) s1 ⟨hFB.1, hFB.2.1⟩ z hz, (tl s1 z hz).trans hFB.2.2⟩ + · have hcf : dd.senderIsCall = true ∨ xs.tcbFault ≠ none := by + cases h : dd.senderIsCall + · exact Or.inr fun c => hrun ⟨h, c⟩ + · exact Or.inl rfl + rw [if_pos hcf] at hz + by_cases hg : (dd.senderCanGrant = true ∨ dd.senderCanGrantReply = true) ∧ rep ≠ none + · rw [if_pos hg] at hz + obtain ⟨r, hr⟩ := Option.ne_none_iff_exists'.1 hg.2 + subst hr + obtain ⟨yt, hyt, eyt⟩ := hK1.tcbScA hkt + obtain ⟨ys, hys, -⟩ := SIKA.tcbF hK1 hks + rw [nd_bind_one (getTcbObjRef_eq _ hys), nd_pure_bind', + replyPush_anyDonTA hys hyt _ (fun hcd => by + rw [eyt]; exact hsct hcf hg.1 hg.2 (of_decide_eq_true hcd).2)] at hz + have K2 : SIKA σ.idleThread (fun o => o = specPtr t ∨ o = specPtr sd) (fun _ => False) false σ z.2 := + hK1.trans (replyPush_noDon_sikA (i := σ.idleThread) (M := fun o => o = specPtr t ∨ o = specPtr sd) (N := fun _ => False) _ _ _ (Or.inr rfl) + s1 z hz) + have tm : TmPresA (replyPush (Z := Unit) (specPtr sd) (specPtr t) r false) := replyPush_noDon_tmA _ _ _ + have tl : LmKA s1 z.2 := (tm s1 z hz).lmt + exact ⟨(nsw z.2 K2).1, tl.trans hFB.2.2⟩ + · rw [if_neg hg] at hz + have tl : PresR LmKA (setThreadState (Z := Unit) (specPtr sd) .inactive) := fun σ0 r hr => + ((show TmPresA (setThreadState (Z := Unit) (specPtr sd) .inactive) by + unfold Fl4SpecA64.setThreadState Fl4SpecA64.setThreadStateAct; tmres) σ0 r hr).lmt + exact ⟨recvTailI_swA (specPtr sd) s1 hFB.2.1 z hz, (tl s1 z hz).trans hFB.2.2⟩ + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3RecvSendValidA.lean b/proofs/Fl4ProofsA64/P3/Stage3RecvSendValidA.lean new file mode 100644 index 000000000..0c3de2309 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3RecvSendValidA.lean @@ -0,0 +1,182 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2c): **`SchedValidA` across `receive_ipc` from a waiting non-call sender without a +fault** (the twin of `sendIpc_recv_schedValidA`). + +* `recvTail_fia`: the tail of the arm (`set_thread_state sender Running`, `possible_switch_to sender`) keeps `FIA`. +* `recvTailI_fia`: the tail for a call sender without a reply object (`set_thread_state sender Inactive`). +* `refillUnblockCheck_cells`: `refill_unblock_check` keeps the ready queues and every TCB cell. +* **`recvSend_run_schedValidA`**: every outcome of `specRecvSendA` is `SchedValidA`; with a reply object (`rep ≠ none`, + the push arm) the sender holds an SC and the receiver has one, so nothing is donated. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3IpcSchedValidA +import Fl4ProofsA64.P3.Stage3RecvSendSpecA +import Fl4ProofsA64.P3.Stage3SendFaultValidA +import Fl4ProofsA64.P3.Stage3SendFaultSpecA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P3 Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Classical + +noncomputable section + +instance {α : Type} (a : α) : CPresInst (NonDet.pure a : SMonad Unit α) := ⟨PresR.pure a⟩ + +/-- `refill_unblock_check` writes only the SC's refills and `reprogramTimer`. -/ +theorem refillUnblockCheck_cells (p : ObjRef) : CPres (refillUnblockCheck (Z := Unit) p) := by + have merge : CPres (mergeOverlappingRefills (Z := Unit) p) := by + unfold mergeOverlappingRefills refillPopHead updateRefillHd getRefillHead; cpres + unfold refillUnblockCheck + refine PresR.bind (by unfold isRoundRobin; cpres) (fun _ => PresR.bind (by unfold getScRefillReady; cpres) + fun _ => PresR.when ?_) + refine PresR.bind (PresR.gets _) (fun _ => PresR.bind (by unfold updateRefillHd; cpres) fun _ => + PresR.bind (PresR.modify _ fun _ => ⟨rfl, fun _ _ => Iff.rfl⟩) fun _ => ?_) + unfold refillHeadOverlappingLoop + exact PresR.whileLoop (fun _ => merge) _ + +instance (p : ObjRef) : CPresInst (refillUnblockCheck (Z := Unit) p) := ⟨refillUnblockCheck_cells p⟩ + +/-- The tail of the `SendEP` arm after the sender's wait ends. -/ +theorem recvTail_fia (d : ObjRef) : + HT (fun σ => FIA σ ∧ BAs [d] σ) + (do setThreadState (Z := Unit) d .running + possibleSwitchTo d) + (fun _ σ => FIA σ) := by + refine HT.bind (Q := fun _ σ => FIA σ ∧ BAs [d] σ) + (setThreadState_fia d .running rfl (BAs [d]) (fun σ p x x' hx h1 h => h.tcbUpd hx h1) (fun σ a h => h.act a)) + fun _ => ?_ + exact HT.mono (possibleSwitchTo_fia' d (fun _ => True) (fun _ _ h => h) (fun _ _ h => h)) + (fun _ h => ⟨h.1, h.2 d (List.mem_singleton_self d), trivial⟩) (fun _ _ h => h.1) + +/-- The tail of the `SendEP` arm for a call sender without a reply object: the sender becomes `Inactive`. -/ +theorem recvTailI_fia (d : ObjRef) : + HT (fun σ => FIA σ ∧ BAs [d] σ) (setThreadState (Z := Unit) d .inactive) (fun _ σ => FIA σ) := + HT.mono (setThreadState_fia d .inactive rfl (BAs [d]) (fun σ p x x' hx h1 h => h.tcbUpd hx h1) + (fun σ a h => h.act a)) (fun _ h => h) (fun _ _ h => h.1) + +section +open fl4_core_a64 Fl4ProofsA64.P1 +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +set_option maxHeartbeats 1000000 in +/-- **`SchedValidA` across the `SendEP` arm of `receive_ipc`** (module docstring). -/ +theorem recvSend_run_schedValidA {th : TypedHeapA} {kb : U64} (hPl : PlaceA th kb) (sf : S) {σ : State Unit} + (hR : StateRelA th (L.mem sf) σ) (hV : SlotsValidA th (L.mem sf)) {t sd : U64} + (htr : th t = some .tcb) {e : ObjRef} {q : List ObjRef} + (hk : σ.kheap e = some (.endpoint (.sendEP (specPtr sd :: q)))) + {xt xs : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) (hks : σ.kheap (specPtr sd) = some (.tcb xs)) + (hal : ∀ rb, ipcBufOfA true xt = some rb → (bufAddr rb 0).toNat % 8 = 0) + (rep : Option ObjRef) {e' : ObjRef} {dd : SenderPayload} (hst : xs.tcbState = .blockedOnSend e' dd) + (hsct : (dd.senderIsCall = true ∨ xs.tcbFault ≠ none) → + (dd.senderCanGrant = true ∨ dd.senderCanGrantReply = true) → rep ≠ none → + ¬ (xs.tcbFault.elim false isTimeoutFault = true) → xt.tcbSchedContext ≠ none) (hsdSc : (dd.senderIsCall = true ∨ xs.tcbFault ≠ none) → + (dd.senderCanGrant = true ∨ dd.senderCanGrantReply = true) → rep ≠ none → ∃ sc, xs.tcbSchedContext = some sc) + (hBt : BoundActive (specPtr t) σ) (hts : specPtr t ≠ specPtr sd) + (hcase : XferCaseA xs dd.senderCanGrant) + (hI : SchedValidA σ) (hD : RqDomPrio σ) : + ∀ x ∈ (specRecvSendA (specPtr t) e rep (specPtr sd :: q) σ).1, SchedValidA x.2 := by + suffices H : ∀ x ∈ (specRecvSendA (specPtr t) e rep (specPtr sd :: q) σ).1, FIA x.2 from + fun x hx => SchedValidB.toA (H x hx).1 (H x hx).2 + have hFI : FIA σ := SchedValidA.toFIA hI + have hBA : BAs [specPtr sd, specPtr t] σ := fun u hu x sc ex hs' => by + rcases List.mem_cons.1 hu with rfl | hu' + · have : x = xs := KernelObject.tcb.inj (Option.some.inj (ex.symm.trans hks)) + subst this + exact ⟨x, sc, ex, hs', hFI.1.blk _ x sc ex (by rw [hst]; rfl) hs'⟩ + · obtain rfl := List.mem_singleton.1 hu' + exact hBt x sc ex hs' + intro x hx + unfold specRecvSendA at hx + rw [nd_bind_one (assert_true (by simp) σ), nd_pure_bind'] at hx + rw [show the (specPtr sd :: q).head? = specPtr sd from rfl] at hx + revert x hx + refine HT.atA (P := fun σ' => σ' = σ) ?_ rfl + refine HT.bind (Q := fun _ s1 => FIA s1 ∧ RqDomPrio s1 ∧ BAs [specPtr sd, specPtr t] s1 ∧ TcbKeepA σ s1 ∧ + SchedFrameA σ s1 ∧ SIKA σ.idleThread (fun _ => True) (fun _ => False) false σ s1) (fun σ' h z hz => ?_) + fun _ => ?_ + · subst σ' + have h1 := (HT.ofTC' (by unfold tcbEpDequeue; tpres) (by unfold tcbEpDequeue; cpres) (BAs [specPtr sd, specPtr t]) + (fun _ _ k h => h.ofTKeep k) : + HT _ (tcbEpDequeue (Z := Unit) (specPtr sd) e) _) σ ⟨hFI, hD, hBA⟩ z hz + exact ⟨h1.1, h1.2.1, h1.2.2, TKInst.pres σ z hz, SPresInst.pres σ z hz, tcbEpDequeue_sikA _ _ trivial σ z hz⟩ + refine HT.bind (Q := fun rs s1 => (FIA s1 ∧ RqDomPrio s1 ∧ BAs [specPtr sd, specPtr t] s1 ∧ TcbKeepA σ s1 ∧ + SchedFrameA σ s1 ∧ SIKA σ.idleThread (fun _ => True) (fun _ => False) false σ s1) ∧ + rs = .blockedOnSend e' dd) (fun σ' h z hz => ?_) fun rs => ?_ + · have hz' : z = (xs.tcbState, σ') := by + unfold getThreadState at hz + exact threadGet_valA _ (h.2.2.2.1 _ _ hks) hz + subst hz' + exact ⟨h, hst⟩ + intro σ' ⟨h, ers⟩ z hz + subst ers + dsimp only at hz + obtain ⟨w, hw, hz1⟩ := mem_bindA.1 hz + clear hz + have ew : w = (dd, σ') := hw + subst ew + obtain ⟨u, hu, hz2⟩ := mem_bindA.1 hz1 + clear hz1 + have hT := doIpcTransfer_any_tkA hPl sf (hR.specSchedA h.2.2.2.2.1.1) hV htr (h.2.2.2.1 _ _ hks) + (h.2.2.2.1 _ _ hkt) hal _ _ _ hcase u hu + have hS : SIKA σ.idleThread (fun _ => True) (fun _ => False) false σ' u.2 := + doIpcTransfer_any_sikA hPl sf (hR.specSchedA h.2.2.2.2.1.1) hV htr (h.2.2.2.1 _ _ hks) + (h.2.2.2.1 _ _ hkt) hal _ _ _ hcase σ' rfl u hu + have hNF : FaultIsA (specPtr sd) xs.tcbFault u.2 := FaultIsA.sik (fun x hx => by + rw [h.2.2.2.1 _ _ hks] at hx + have : xs = x := KernelObject.tcb.inj (Option.some.inj hx) + rw [← this]) hS + revert z hz2 + let K : State Unit → Prop := fun s1 => (FIA s1 ∧ BAs [specPtr sd, specPtr t] s1) ∧ + SIKA σ.idleThread (fun _ => True) (fun _ => False) false σ s1 ∧ FaultIsA (specPtr sd) xs.tcbFault s1 ∧ RqDomPrio s1 + refine HT.atA (P := K) ?_ + ⟨⟨h.1.ofTKeep hT.1, h.2.2.1.ofTKeep hT.1⟩, h.2.2.2.2.2.trans hS, hNF, hT.2 h.2.1⟩ + refine HT.bind (Q := fun _ s1 => K s1) (fun s1 h z hz => by + have e : z.2 = s1 := ((threadGet_eqPres _ _) s1 z hz).symm + rw [e]; exact h) fun scOpt => ?_ + refine HT.bind (Q := fun _ s1 => K s1) (fun s1 h z hz => ?_) fun _ => ?_ + · have a := (HT.ofTPres (by unfold ifSporadicCurScAssertRefillUnblockCheck ifCondRefillUnblockCheck; tpres) + (BAs [specPtr sd, specPtr t]) (fun _ _ k h => h.ofTKeep k) : + HT _ (ifSporadicCurScAssertRefillUnblockCheck (Z := Unit) scOpt) _) s1 ⟨h.1.1, h.1.2⟩ z hz + have b := refillCheckAssert_sikA (i := σ.idleThread) (M := fun _ => True) (N := fun _ => False) (don := false) + scOpt s1 z hz + exact ⟨⟨a.1, a.2⟩, h.2.1.trans b, h.2.2.1.sik b, h.2.2.2.ofCells + ((show CPres (ifSporadicCurScAssertRefillUnblockCheck (Z := Unit) scOpt) by + unfold ifSporadicCurScAssertRefillUnblockCheck ifCondRefillUnblockCheck Fl4Spec.NonDet.maybeM; cpres) s1 z hz)⟩ + refine HT.bind (Q := fun fault s1 => (((FIA s1 ∧ BAs [specPtr sd, specPtr t] s1) ∧ RqDomPrio s1) ∧ + SIKA σ.idleThread (fun _ => True) (fun _ => False) false σ s1) ∧ fault = xs.tcbFault) + (HT.threadGet _ (specPtr sd) fun s1 h x hx => ⟨⟨⟨h.1, h.2.2.2⟩, h.2.1⟩, h.2.2.1 x hx⟩) fun fault => ?_ + intro s1 ⟨⟨⟨hFB, hRq⟩, hK1⟩, ef⟩ z hz + subst ef + have hFB' : FIA s1 ∧ BAs [specPtr sd] s1 := + ⟨hFB.1, fun u hu => hFB.2 u (List.mem_cons.2 (Or.inl (List.mem_singleton.1 hu)))⟩ + by_cases hrun : dd.senderIsCall = false ∧ xs.tcbFault = none + · rw [if_neg (by simp [hrun.1, hrun.2])] at hz + exact recvTail_fia (specPtr sd) s1 hFB' z hz + · have hcf : dd.senderIsCall = true ∨ xs.tcbFault ≠ none := by + cases h : dd.senderIsCall + · exact Or.inr fun c => hrun ⟨h, c⟩ + · exact Or.inl rfl + rw [if_pos hcf] at hz + by_cases hg : (dd.senderCanGrant = true ∨ dd.senderCanGrantReply = true) ∧ rep ≠ none + · rw [if_pos hg] at hz + obtain ⟨r, hr⟩ := Option.ne_none_iff_exists'.1 hg.2 + subst hr + obtain ⟨yt, hyt, eyt⟩ := hK1.tcbScA hkt + obtain ⟨ys, hys, eys⟩ := hK1.tcbScA hks + obtain ⟨sc, hsc⟩ := hsdSc hcf hg.1 hg.2 + have hAct : ActTcbA s1 (specPtr sd) := + hFB.2 (specPtr sd) (List.mem_cons_self) ys sc hys (by rw [eys]; exact hsc) + rw [nd_bind_one (getTcbObjRef_eq _ hys), nd_pure_bind', + replyPush_anyDonTA hys hyt _ (fun hcd => by + rw [eyt]; exact hsct hcf hg.1 hg.2 (of_decide_eq_true hcd).2)] at hz + exact (replyPushNoDon_fia (d := specPtr t) (t := specPtr sd) hts s1 + ⟨hFB.1, hRq, hAct, fun u hu => hFB.2 u (List.mem_cons_of_mem _ hu)⟩ z hz).1 + · rw [if_neg hg] at hz + exact recvTailI_fia (specPtr sd) s1 hFB' z hz + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3RecvSigKeepA.lean b/proofs/Fl4ProofsA64/P3/Stage3RecvSigKeepA.lean new file mode 100644 index 000000000..79078d632 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3RecvSigKeepA.lean @@ -0,0 +1,170 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2c): **the narrow write set of a blocking receive on a notification** +(`receive_signal_block`'s sibling; the ratified statement is unchanged). + +* **`receive_signal_block_narrowA`**: a blocking `receive_signal` on an idle or waiting notification writes the + caller's state and block words, the scheduler's words (`Wr`: `schedule_tcb`, `maybe_return_sched_context`), the + notification's two queue words and the `Ep` links of the caller and the queue. The arch state is unchanged. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.NtfnBlockA +import Fl4ProofsA64.P3.SchedFootNA +import Fl4ProofsA64.P3.NtfnWakeFootA +import Fl4ProofsA64.P3.EpFootHelpA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Notif +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 + +noncomputable section + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + {N T : Set U64} {SR : S → Fl4SpecA64.State Unit → Prop} {DL : S → U64 → Prop} + {Wr : Fl4SpecA64.State Unit → S → U64 → Prop} + +/-- The write set of a blocking receive on a notification (module docstring). -/ +def NtfnBlkFpA (Wr : Fl4SpecA64.State Unit → S → U64 → Prop) (σ : Fl4SpecA64.State Unit) (s : S) (t n : U64) + (xs : List U64) (b : U64) : Prop := + (b = stA t ∨ b = tBlockA t) ∨ Wr σ s b ∨ (b = epHeadA n ∨ b = epTSA n) ∨ + ∃ w ∈ t :: xs, b = nextA .Ep w ∨ b = prevA .Ep w + +/-- **Blocking `receive_signal`'s narrow write set** (module docstring). -/ +theorem receive_signal_block_narrowA (hH : SchedHypsA st N T SR) (hO : SchedOpsA st ar N T SR DL) + (hF : SchedFootNA st ar N T SR DL Wr) (hP : NPlaced T N) + (s : S) (a : A) (σ : Fl4SpecA64.State Unit) (hR : NtfnStateRel N T (L.mem s) σ) (hSR : SR s σ) + {t : U64} (ht : t ∈ T) {xt : Fl4SpecA64.Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hnb : ∀ m, xt.tcbState ≠ .blockedOnNotification m) + {n : U64} (hn : n ∈ N) {x : Fl4SpecA64.Notification} (hk : σ.kheap (specPtr n) = some (.notification x)) + (hna : ∀ b, x.ntfnObj ≠ .activeNtfn b) + {xs : List U64} (hQ : QueueRel (L.mem s) T .Ep (qOf (L.mem s) n) xs) + (hxs : x.ntfnObj.ntfnQueue = xs.map specPtr) + (hsorted : PrioSorted (fun y => L.mem s (prioA y)) xs) + (hAR : ActReady σ) (hNQ : NotQueued σ (specPtr t)) + (hRB : RelIfBound σ (specPtr t)) (hsane : ∀ u, σ.schedulerAction = .switchThread u → u ≠ (specPtr t)) + (hnidle : (specPtr t) ≠ σ.idleThread) : + notification.receive_signal st ar s a t n true ⦃ r => + r.2 = a ∧ WritesIn st s r.1 (NtfnBlkFpA Wr σ s t n xs) ⦄ := by + have hx := hR.at_ntfn hn hk + have hxt := hR.at_tcb ht hkt + have hnm := not_member hR ht hkt hnb + have hst : stOf (L.mem s) n = obj.NTFN_IDLE ∨ stOf (L.mem s) n = obj.NTFN_WAITING := by + cases h : x.ntfnObj with + | idleNtfn => exact Or.inl (stOf_idle (T := T) (h ▸ hx.obj) rfl) + | waitingNtfn q => exact Or.inr (stOf_waiting (T := T) (h ▸ hx.obj) rfl) + | activeNtfn b => exact absurd h (hna b) + have htxs : t ∉ xs := fun h => hnm n hn x hk (by rw [hxs]; exact List.mem_map_of_mem h) + set x1 : Fl4SpecA64.Tcb := { xt with tcbState := .blockedOnNotification (specPtr n) } + set σ1 := updK σ (specPtr t) (.tcb x1) + set s2 := L.wr (L.wr s (stA t) sched.TS_BLOCKED_ON_NTFN) (tBlockA t) n + obtain ⟨hw1, hoth1⟩ := twr_wr (st := st) hP ht (K := 3328#u64) (Or.inl rfl) s sched.TS_BLOCKED_ON_NTFN + obtain ⟨hw2, hoth2⟩ := twr_wr (st := st) hP ht (K := 3336#u64) (Or.inr (Or.inl rfl)) + (L.wr s (stA t) sched.TS_BLOCKED_ON_NTFN) n + rw [← stA_eq] at hw1 hoth1 + rw [← tBlockA_eq] at hw2 hoth2 + have hw := hw1.trans hw2 + have W2 : WritesIn st s s2 (fun b => b = stA t ∨ b = tBlockA t) := + ⟨by simp only [s2, LawfulStore.ksb_wr], by simp only [s2, LawfulStore.cpu_wr], fun b hb hnb' => by + show L.mem (L.wr (L.wr s (stA t) sched.TS_BLOCKED_ON_NTFN) (tBlockA t) n) b = L.mem s b + rw [rd_wr_ne (foot_blk hP ht).1 hb (Ne.symm fun h => hnb' (Or.inr h)), + rd_wr_ne (foot_st hP ht).1 hb (Ne.symm fun h => hnb' (Or.inl h))]⟩ + have hR1 : NtfnStateRel N T (L.mem s2) σ1 := by + refine hR.upd_tcb ht _ hw.ntfn hw.tcb hw.links (Or.inl hnm) ⟨?_, ?_, ?_, ?_⟩ + · show (L.mem s2 (stA t)).val = 6 + rw [stA_eq, hoth2 _ (by simp) (by simp) (by simp), ← stA_eq, rd_wr_eq (foot_st hP ht).1, six_val] + · intro m hm + cases hm + show specPtr (L.mem s2 (tBlockA t)) = (specPtr n) + rw [rd_wr_eq (foot_blk hP ht).1] + · show (L.mem s2 (badgeRegA t)).bv = badgeOf xt + rw [badgeRegA_eq, hoth2 _ (by simp) (by simp) (by simp), hoth1 _ (by simp) (by simp) (by simp), + ← badgeRegA_eq]; exact hxt.badge + · show (L.mem s2 (prioA t)).val = xt.tcbPriority.toNat + rw [prioA_eq', hoth2 _ (by simp) (by simp) (by simp), hoth1 _ (by simp) (by simp) (by simp), + ← prioA_eq']; exact hxt.prio + have hSR1 := hH.block _ _ _ _ t hSR hNQ hRB hsane hnidle hw.foot (NtfnStep.tcb hkt (.blockedOnNotification (specPtr n)) xt.tcbArch) + (fun p x y h1 h2 hp => by + simp only [Function.update_apply, hp, ↓reduceIte] at h2; rw [h1] at h2; cases h2; exact ⟨rfl, id⟩) + have hnt : (specPtr n) ≠ (specPtr t) := by intro e; rw [e, hkt] at hk; cases hk + obtain ⟨σ2, hσ2⟩ : ∃ σ2, stsAct σ1 (specPtr t) = σ2 := ⟨_, rfl⟩ + have hkh2 : σ2.kheap = σ1.kheap := by rw [← hσ2, stsAct_kheap] + have hk2 : σ2.kheap (specPtr n) = some (.notification x) := by + rw [hkh2]; simp [σ1, hnt, hk] + have hkt2 : σ2.kheap (specPtr t) = some (.tcb x1) := by rw [hkh2]; simp [σ1] + have hsa2 : σ2.schedulerAction = σ1.schedulerAction ∨ σ2.schedulerAction = .chooseNewThread := by + rw [← hσ2]; exact stsAct_sa _ _ + have hct2 : σ2.curTime = σ1.curTime := by rw [← hσ2]; exact stsAct_curTime _ _ + have hrq2 : σ2.releaseQueue = σ1.releaseQueue := by rw [← hσ2]; exact stsAct_releaseQueue _ _ + have hid2 : σ2.idleThread = σ1.idleThread := by rw [← hσ2]; exact stsAct_idleThread _ _ + have hkv2 : KView σ1 σ2 := by rw [← hσ2]; exact KView.stsAct _ _ + have key : (do + let s1 ← obj.Tcb.set_state st t s sched.TS_BLOCKED_ON_NTFN + let s2 ← obj.Tcb.set_block_obj st t s1 n + let s3 ← sched.schedule_tcb st s2 t + let s4 ← notification.ntfn_append st s3 n t + let s5 ← notification.maybe_return_sched_context st s4 n t + ok (s5, a)) ⦃ r => r.2 = a ∧ WritesIn st s r.1 (NtfnBlkFpA Wr σ s t n xs) ⦄ := by + step with obj.Tcb.set_state.spec as ⟨s1', h1'⟩ + step with obj.Tcb.set_block_obj.spec as ⟨s2', h2'⟩ + subst h1' h2' + refine WP.spec_bind (WP.spec_and (hO.scheduleTcb _ σ1 t hR1 hSR1 ht) (hF.scheduleTcb _ σ1 t hR1 hSR1 ht)) ?_ + rintro s3 ⟨⟨hf3, hS3⟩, hw3⟩ + rw [hσ2] at hS3 + have hR3 : NtfnStateRel N T (L.mem s3) σ2 := hR1.frame hP hf3 hkv2 + have hwp : ∀ y ∈ T, y ≠ t → L.mem s2 (prioA y) = L.mem s (prioA y) := + fun y hyT hyt => (hw.tcb y hyT hyt).prio + obtain ⟨xs3, hQ3, hxs3, -⟩ := (hR3.at_ntfn hn hk2).obj.queue + have e3 : xs3 = xs := by + rw [hxs] at hxs3 + exact (List.map_injective_iff.2 (fun a b h => specPtr_inj h) hxs3).symm + subst e3 + refine WP.spec_bind (WP.spec_and (block_append hP s s2 s3 σ2 ht hn hk2 hkt2 hQ hxs htxs hsorted hwp hf3 hR3) + (ntfn_append_full hP s3 hn t xs3 hQ3 ht htxs)) ?_ + rintro s4 ⟨⟨hR4, hW4⟩, -, -, hA3⟩ + have hsorted3 : PrioSorted (fun y => L.mem s3 (prioA y)) xs3 := prioSorted_congr hsorted (fun y hy => + (hf3.tfields hP (hQ.mem y hy)).prio.trans (hwp y (hQ.mem y hy) (fun e => htxs (e ▸ hy)))) + obtain ⟨l4, hl4⟩ : ∃ l : List Fl4SpecA64.ObjRef, + l = (insPrio (fun y => L.mem s3 (prioA y)) (L.mem s3 (prioA t)) t xs3).map specPtr := ⟨_, rfl⟩ + rw [← hl4] at hR4 + obtain ⟨y4, hy4⟩ : ∃ y : Fl4SpecA64.Notification, y = { x with ntfnObj := .waitingNtfn l4 } := ⟨_, rfl⟩ + rw [← hy4] at hR4 + have hSR4 := hH.stable _ _ _ _ hS3 hW4 (NtfnStep.ntfnK (y := y4) hk2 (by rw [hy4]) (by rw [hy4])) + (Endp.NoBlockE.ntfnK _ _ _) + have hbs := block_step (σ2 := σ2) hk hkt hkt2 (fun p hp => by rw [hkh2]; simp [σ1, hp]) hsa2 hct2 hrq2 y4 + have hni : specPtr t ≠ σ2.idleThread := by rw [hid2]; exact hnidle + refine WP.spec_bind (WP.spec_and (hO.returnSc s4 σ _ t n hR4 hSR4 ht hn hAR hbs hni) + (hF.returnSc s4 σ _ t n hR4 hSR4 ht hn hAR hbs hni)) ?_ + rintro s5 ⟨-, hw5⟩ + simp only [WP.spec_ok] + refine ⟨trivial, ?_⟩ + have Wa : WritesIn st s3 s4 (fun b => (b = epHeadA n ∨ b = epTSA n) ∨ + ∃ w ∈ t :: xs3, b = nextA .Ep w ∨ b = prevA .Ep w) := + ⟨hA3.foot.1, hA3.foot.2.1, fun b hb hnb' => hA3.agree b hb + (fun w hw => ⟨fun h => hnb' (Or.inr ⟨w, hw, Or.inl h⟩), fun h => hnb' (Or.inr ⟨w, hw, Or.inr h⟩)⟩) + (fun h => hnb' (Or.inl (Or.inl h))) (fun h => hnb' (Or.inl (Or.inr h)))⟩ + have z1 : SSzA σ σ1 := Endp.ssz_updK_tcb σ (specPtr t) _ + have z2 : SSzA σ (updK σ2 (specPtr n) (.notification y4)) := + (z1.trans (SSzA.of_kheap hkh2)).trans (ssz_updK_ntfn σ2 (specPtr n) _) + have k2 : L.ksb s2 = L.ksb s := W2.1 + have k4 : L.ksb s4 = L.ksb s := (Wa.1.trans hw3.1).trans k2 + refine (((W2.trans hw3).trans Wa).trans hw5).mono fun b h => ?_ + rcases h with ((h | h) | h) | h + · exact Or.inl h + · exact Or.inr (Or.inl (hF.back z1 k2 h)) + · exact Or.inr (Or.inr h) + · exact Or.inr (Or.inl (hF.back z2 k4 h)) + unfold notification.receive_signal + step with obj.Ntfn.state.spec as ⟨v, hv⟩ + rw [hv, epState_eq] + rcases hst with hst | hst + · simp only [hst, ↓reduceIte] + exact key + · simp only [hst, ntfn_consts.1, ntfn_consts.2.1, show (1#u64 : U64) ≠ 0#u64 by decide, ↓reduceIte] + exact key + +end + +end + +end Fl4ProofsA64.P3.Notif diff --git a/proofs/Fl4ProofsA64/P3/Stage3RecvTmoFpA.lean b/proofs/Fl4ProofsA64/P3/Stage3RecvTmoFpA.lean new file mode 100644 index 000000000..87873f4fd --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3RecvTmoFpA.lean @@ -0,0 +1,244 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2c): **`receive_from_sender` from a waiting sender with a timeout fault and an SC**, +without a reply push (`rv = 0`, or no grant and no grant-reply): the transfer is `do_fault_transfer`'s timeout arm (`EpFaultTmoA`), which also writes +the sender's SC (`update_consumed`); the sender then becomes `Inactive`. + +`EpFaultXferA.FaultOKA` excludes this fault, so `receive_from_senderA` / `receive_from_sender_fpA` do not cover it. +The SC words are in the scheduler write footprint `Wr` (`hscW`), so both of their conclusions hold here too. + +* **`receive_from_sender_tmoScA`**: one spec outcome (`specRecvSendA`), `EpStateRel`, `SR`, `FpA` and the narrow + write set `RecvSFpA`. + +Case selectors / placement: the sender's timeout fault `hf`, its SC (`hsc`, `hsc0`, the SC object `hky`), `hni` (not an +idle thread), `rv = 0` or the sender cannot grant. No named premise beyond `hTO` (discharged by `ipcSchedOpsA`). New module. +-/ +import Fl4ProofsA64.P3.Stage3RecvSendFpA +import Fl4ProofsA64.P3.EpFaultTmoA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched + +noncomputable section + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] {T E R : Set U64} + {Bf : Set (U64 × Fl4SpecA64.Word)} {SR : S → Fl4SpecA64.State Unit → Prop} + {Wr : Fl4SpecA64.State Unit → S → U64 → Prop} {NotIdle : U64 → Prop} + +open Fl4SpecA64 in +set_option maxHeartbeats 1000000 in +/-- **`receive_from_sender` from a sender with a timeout fault and an SC, without a reply push** (module docstring). -/ +theorem receive_from_sender_tmoScA (hO : SchedOpsEA st T E R Bf SR) (hF : SchedFootEA st T E R Bf SR Wr) + (hTO : TimeoutScOpA st ar T E R Bf SR NotIdle) + (hP : EPlaced T E R Bf) {th : TypedHeapA} {kb : U64} (hPl : PlaceA th kb) + (s : S) (a : A) (σ : State Unit) (hR : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) + (hSt : StateRelA th (L.mem s) σ) (hC : BufCover st T Bf s σ) + (hNative : BufNativeA T (L.mem s)) + (hoff : ∀ y, IsSlotA th y → ¬ EWrite T E R Bf y ∧ ¬ EWrite T E R Bf (core.num.U64.wrapping_add y 8#u64)) + {t : U64} (ht : t ∈ T) (hth : th t = some .tcb) {xt : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hnb : tsCode xt.tcbState ≠ 4) + {e sd : U64} (he : e ∈ E) {q : List ObjRef} + (hk : σ.kheap (specPtr e) = some (.endpoint (.sendEP (specPtr sd :: q)))) + (hsh : th sd = some .tcb) {xs : Tcb} (hks : σ.kheap (specPtr sd) = some (.tcb xs)) + {bd : Word} (hf : xs.tcbFault = some (.timeout bd)) (rv : U64) + (hnp : ∀ e' dd, xs.tcbState = .blockedOnSend e' dd → + rv = 0#u64 ∨ (dd.senderCanGrant = false ∧ dd.senderCanGrantReply = false)) + (hni : NotIdle sd) (hsc0 : L.mem s (scA sd) ≠ 0#u64) + (hsc : xs.tcbSchedContext = some (specPtr (L.mem s (scA sd)))) + {y : SchedContext} {n : Nat} (hky : σ.kheap (specPtr (L.mem s (scA sd))) = some (.schedContext y n)) + (hscW : ∀ b : U64, (L.mem s (scA sd)).val ≤ b.val ∧ + b.val < (L.mem s (scA sd)).val + 2 ^ (n + minSchedContextBits) → Wr σ s b) : + endpoint.receive_from_sender st ar s a t e rv ⦃ r => r.2 = a ∧ + ¬ (specRecvSendA (specPtr t) (specPtr e) (optRef rv) (specPtr sd :: q) σ).2 ∧ + (∃ σ', ((), σ') ∈ (specRecvSendA (specPtr t) (specPtr e) (optRef rv) (specPtr sd :: q) σ).1 ∧ + EpStateRel T E R Bf (L.mem r.1) σ' ∧ SR r.1 σ' ∧ FpA st T E R Bf Wr σ s r.1) ∧ + WritesIn st s r.1 (RecvSFpA st T Bf Wr σ s t e sd) ⦄ := by + obtain ⟨hst1, xs0, hxs0, hq, hQ, hB⟩ := hR.at_ep he hk + obtain ⟨sd', xs', rfl⟩ := List.exists_cons_of_ne_nil hxs0 + simp only [List.map_cons, List.cons.injEq] at hq + obtain ⟨hd', hq'⟩ := hq + have hdd : sd = sd' := specPtr_inj hd' + subst hdd + have hsT : sd ∈ T := hQ.mem sd (by simp) + have hsa := hP.tcb sd hsT + have hxs := hR.at_tcb hsT hks + have hxt := hR.at_tcb ht hkt + have hs4 : (L.mem s (stA sd)).val = 4 := (hB sd (by simp)).1 + have hsb : L.mem s (tBlockA sd) = e := (hB sd (by simp)).2 + obtain ⟨e', dd, hstd⟩ := tsCode_four (hxs.st.symm.trans hs4) + have he' : e' = specPtr e := by + have := hxs.blk e' (by rw [hstd]; rfl); rw [hsb] at this; exact this.symm + subst he' + obtain ⟨hbadge, hflags⟩ := hxs.send _ _ hstd + have hts : t ≠ sd := by intro h; subst h; exact hnb (hxt.st.symm.trans hs4) + have hse : specPtr sd ≠ specPtr e := kheap_ne hks hk (fun _ h1 h2 => by subst h1; cases h2) + have hte : specPtr t ≠ specPtr e := kheap_ne hkt hk (fun _ h1 h2 => by subst h1; cases h2) + have hts' : specPtr t ≠ specPtr sd := fun h => hts (specPtr_inj h) + have hsw : specPtr sd ≠ specPtr (L.mem s (scA sd)) := fun h => by rw [h, hky] at hks; cases hks + have htw : specPtr t ≠ specPtr (L.mem s (scA sd)) := fun h => by rw [h, hky] at hkt; cases hkt + have hwe : specPtr (L.mem s (scA sd)) ≠ specPtr e := fun h => by rw [h, hk] at hky; cases hky + unfold endpoint.receive_from_sender + step with endpoint.get_queue.spec as ⟨q0, hq0⟩ + have hhead : q0.head = sd := by rw [hq0, epQ_eq]; exact hQ.head + rw [hhead] + have hd : specPtr sd ∈ (Endpoint.sendEP (specPtr sd :: q)).epQueue := by simp [Endpoint.epQueue] + refine WP.spec_bind (WP.spec_and (ep_dequeue_raw hP s σ hR he hk hd) + (ep_dequeue_full hP s he sd (sd :: xs') hQ (by simp))) ?_ + rintro s1 ⟨⟨-, hR1, hW1, hE1, hA1⟩, -, -, hQA, -⟩ + have hS1 := hO.stable _ _ _ _ hSR hW1 hE1 (NoBlockE.updO _ _ fun _ h => by cases h) + set σ1 := updK σ (specPtr e) (.endpoint (epDeq (.sendEP (specPtr sd :: q)) (specPtr sd))) with hσ1 + have hkt1 : σ1.kheap (specPtr t) = some (.tcb xt) := by rw [hσ1, updK_kheap, Function.update_of_ne hte, hkt] + have hks1 : σ1.kheap (specPtr sd) = some (.tcb xs) := by rw [hσ1, updK_kheap, Function.update_of_ne hse, hks] + have hky1 : σ1.kheap (specPtr (L.mem s (scA sd))) = some (.schedContext y n) := by + rw [hσ1, updK_kheap, Function.update_of_ne hwe, hky] + have hsc1 : L.mem s1 (scA sd) = L.mem s (scA sd) := + hW1.2.2 _ (field_aligned (hP.tcb sd hsT) (by simp) (by simp)) + (not_ew_offA hP hsT (K := 3464#u64) (by simp) (by unfold TWOffN; simp) (by simp) (by simp)) + have hbw : L.mem s1 (tBadgeA sd) = L.mem s (tBadgeA sd) := hA1 _ (a_badge hsa).1 ⟨sd, hsT, (a_badge hsa).2⟩ + have hfw : L.mem s1 (tFlagsA sd) = L.mem s (tFlagsA sd) := hA1 _ (a_flags hsa).1 ⟨sd, hsT, (a_flags hsa).2⟩ + step with obj.Tcb.block_badge.spec as ⟨bdg, hbdg⟩ + step with obj.Tcb.block_flags.spec as ⟨fl, hfl⟩ + simp only [lift, bind_ok] + have hg : (fl &&& endpoint.FLAG_GRANT != 0#u64) = dd.senderCanGrant := by + rw [hfl, hfw, hflags]; exact flags_grantA dd + have hgr : (fl &&& endpoint.FLAG_GRANT_REPLY != 0#u64) = dd.senderCanGrantReply := by + rw [hfl, hfw, hflags]; exact flags_grantReplyA dd + have hc : (fl &&& endpoint.FLAG_CALL != 0#u64) = dd.senderIsCall := by + rw [hfl, hfw, hflags]; exact flags_callA dd + have hbb : bdg.bv = dd.senderBadge := by rw [hbdg, hbw]; exact hbadge + rw [hg, hgr, hc] + have hmem : ∀ z, IsSlotA th z → L.mem s1 z = L.mem s z ∧ + L.mem s1 (core.num.U64.wrapping_add z 8#u64) = L.mem s (core.num.U64.wrapping_add z 8#u64) := by + intro z hz + have hal : Aligned z := by have := (IsSlotA.al32 hPl hz).1; unfold Aligned; omega + exact ⟨hW1.2.2 z hal (hoff z hz).1, hW1.2.2 _ (wa_al hal (by simp)) (hoff z hz).2⟩ + have hSt1 := hSt.updEpA hPl hmem hk (epDeq (.sendEP (specPtr sd :: q)) (specPtr sd)) + have hC1 : BufCover st T Bf s1 σ1 := + BufCover.ewA hP hC hW1 fun u _ x hx => by + have hne : specPtr u ≠ specPtr e := fun h => by + rw [h, hσ1, updK_kheap, Function.update_self] at hx; cases hx + rwa [hσ1, updK_kheap, Function.update_of_ne hne] at hx + -- the transfer, with `update_consumed` + have htr : ipc.do_ipc_transfer st ar s1 a sd e bdg dd.senderCanGrant t ⦃ s2 => ∃ σ2, + doIpcTransfer (Z := Unit) (specPtr sd) (some (specPtr e)) bdg.bv dd.senderCanGrant (specPtr t) σ1 = + ({((), σ2)}, False) ∧ + EpStateRel T E R Bf (L.mem s2) σ2 ∧ SR s2 σ2 ∧ + (∃ s0, WritesIn st s0 s2 (EWrite T E R Bf) ∧ + WritesIn st s1 s0 (fun b => (L.mem s1 (scA sd)).val ≤ b.val ∧ + b.val < (L.mem s1 (scA sd)).val + 2 ^ (n + minSchedContextBits)) ∧ + Agree (L.mem s0) (L.mem s2) (fun b => ¬ RcvRegA t b ∧ ¬ BufW Bf b)) ∧ + (∃ y', σ2.kheap (specPtr (L.mem s1 (scA sd))) = some (.schedContext y' n)) ∧ + (∀ p, p ≠ specPtr t → p ≠ specPtr (L.mem s1 (scA sd)) → σ2.kheap p = σ1.kheap p) ∧ + ∃ x', σ2.kheap (specPtr t) = some (.tcb x') ⦄ := by + have hspec : doIpcTransfer (Z := Unit) (specPtr sd) (some (specPtr e)) bdg.bv dd.senderCanGrant (specPtr t) σ1 = + doFaultTransfer (Z := Unit) bdg.bv (specPtr sd) (specPtr t) (ipcBufOfA true xt) σ1 := by + unfold doIpcTransfer + rw [nd_bind_one (lookupIpcBuffer_eqA hkt1 true), nd_bind_one (threadGet_eq _ hks1), hf] + unfold ipc.do_ipc_transfer + refine WP.spec_bind (lookup_buf_full s1 σ1 hSt1 hth ht hkt1 hC1 (hNative.ewA hP hW1) true) ?_ + rintro rbw ⟨hrw0, hrwB⟩ + step with obj.Tcb.fault_type.spec as ⟨ft, hft⟩ + have hft0 : ¬ ft = fl4_abi.consts.fault.NullFault := by + intro h + have : L.mem s1 (tFaultA sd) = 0#u64 := by rw [← hft, h]; unfold fl4_abi.consts.fault.NullFault; rfl + rw [(hR1.at_tcb hsT hks1).fault0.1 this] at hf; cases hf + rw [if_neg hft0] + refine WP.spec_mono (do_fault_transfer_timeoutScA hTO hO.to0 hP s1 a σ1 hR1 hS1 hsT ht (Ne.symm hts) hks1 hkt1 hf + (by rw [hsc1]; exact hsc) hni (by rw [hsc1]; exact hsc0) (by rw [hsc1]; exact hky1) + (buf_caseA hrw0 hrwB) bdg) ?_ + rintro s2 ⟨σ2, h1, b1, b2, ⟨s0, -, c2, c3, c4⟩, -, b5, b6, x', b7, -⟩ + exact ⟨σ2, hspec.trans h1, b1, b2, ⟨s0, c2, c3, c4⟩, b5, b6, x', b7⟩ + refine WP.spec_bind htr ?_ + rintro s2 ⟨σ2, hT, hR2, hS2, ⟨s0, hW2, hWsc, hAg⟩, ⟨y', hky2⟩, hfr2, x2, hkt2⟩ + rw [hsc1] at hky2 hfr2 hWsc + rw [hbb] at hT + -- the writes up to here + have hfp : WritesIn st s s2 (RecvSFpA st T Bf Wr σ s t e sd) := by + refine ⟨hW2.1.trans (hWsc.1.trans hW1.1), hW2.2.1.trans (hWsc.2.1.trans hW1.2.1), fun b hb hn => ?_⟩ + have a1 : L.mem s2 b = L.mem s0 b := hAg b hb + ⟨fun h => hn (Or.inl (Or.inr (Or.inr (Or.inl h)))), fun h => hn (Or.inl (Or.inr (Or.inr (Or.inr h))))⟩ + have a2 : L.mem s0 b = L.mem s1 b := hWsc.2.2 b hb fun h => hn (Or.inr (Or.inr (hscW b h))) + have a3 : L.mem s1 b = L.mem s b := hQA b hb ⟨fun h => hn (Or.inl (Or.inl h)), + fun ⟨w, hw, hl⟩ => hn (Or.inl (Or.inr (Or.inl ⟨w, hQ.mem w hw, (hB w hw).2, hl⟩)))⟩ + exact a1.trans (a2.trans a3) + have hz1 : SSzA σ σ1 := ssz_of_epStep hE1 + have hz2 : SSzA σ σ2 := by + refine hz1.trans fun p z k hz => ?_ + by_cases hpw : p = specPtr (L.mem s (scA sd)) + · rw [hpw, hky2] at hz + cases hz + exact ⟨y, by rw [hpw]; exact hky1⟩ + by_cases hpt : p = specPtr t + · rw [hpt, hkt2] at hz; cases hz + exact ⟨z, by rw [← hfr2 p hpt hpw]; exact hz⟩ + have hpre2 : FpA st T E R Bf Wr σ s s2 := + ((hW1.mono fun _ h => Or.inl h).trans1 (hWsc.mono fun b h => Or.inr (hscW b h))).trans1 + (hW2.mono fun _ h => Or.inl h) + have hks2 : σ2.kheap (specPtr sd) = some (.tcb xs) := by rw [hfr2 _ (Ne.symm hts') hsw, hks1] + have hk1e : σ1.kheap (specPtr e) = some (.endpoint (epDeq (.sendEP (specPtr sd :: q)) (specPtr sd))) := by + rw [hσ1, updK_kheap, Function.update_self] + have hsb1 : L.mem s1 (tBlockA sd) = e := by rw [hA1 _ (a_blk hsa).1 ⟨sd, hsT, (a_blk hsa).2⟩, hsb] + have hq1 := dequeued_not_queued hR1 he hsb1 hk1e + (epDeq_not_mem (z := .sendEP (specPtr sd :: q)) (xs := sd :: xs') (by rw [hq']; rfl) hQ.nodup sd) + have hq2 : ∀ e'' ∈ E, ∀ z, σ2.kheap (specPtr e'') = some (.endpoint z) → specPtr sd ∉ z.epQueue := by + intro e'' he'' z hz + have hne : specPtr e'' ≠ specPtr t := by intro h; rw [h, hkt2] at hz; cases hz + have hnw : specPtr e'' ≠ specPtr (L.mem s (scA sd)) := by intro h; rw [h, hky2] at hz; cases hz + exact hq1 e'' he'' z (by rwa [hfr2 _ hne hnw] at hz) + -- the sporadic unblock and `Inactive` + have hnr : runnable xs.tcbState = false := by rw [hstd]; rfl + have hnq2 := hO.notQueued _ _ sd xs hS2 hks2 hnr + refine WP.spec_bind (WP.spec_and (hO.unblockSporadic s2 σ2 sd xs hR2 hS2 hsT hks2 hnr hnq2) + (hF.unblockSporadic s2 σ2 sd xs hR2 hS2 hsT hks2 hnr hnq2)) ?_ + rintro s3 ⟨⟨σ3, hA, hf3, hv3, hS3⟩, hw3⟩ + have hz3 : SSzA σ σ3 := hz2.trans (hF.unblockSz _ _ _ hA) + have h3 : WritesIn st s s3 (RecvSFpA st T Bf Wr σ s t e sd) := + WritesIn.schedA hF hfp hz2 (B := fun _ => False) (hw3.mono fun _ h => Or.inr h) + (fun _ h => h.elim) (fun _ h => Or.inr (Or.inr h)) + have hpre3 : FpA st T E R Bf Wr σ s s3 := FpA.trans hF hpre2 hz2 (FpA.of_wr hw3) + have hR3 := hR2.frame hP hf3 hv3 + obtain ⟨xs3, hks3, hs3st, -, -, hs3f, -⟩ := hv3.2.2.1 _ _ hks2 + have hq3 : ∀ e'' ∈ E, ∀ z, σ3.kheap (specPtr e'') = some (.endpoint z) → specPtr sd ∉ z.epQueue := by + intro e'' he'' z hz + obtain ⟨z2, hz2', -⟩ := hR2.ep e'' he'' + rw [hv3.1 _ _ hz2'] at hz + injection hz with hz; injection hz with hz; subst hz + exact hq2 e'' he'' z2 hz2' + have hxs3 := hR3.at_tcb hsT hks3 + have hst3 : xs3.tcbState = .blockedOnSend (specPtr e) dd := hs3st.trans hstd + step with obj.Tcb.fault_type.spec as ⟨ft, hft⟩ + have hf3' : xs3.tcbFault ≠ none := by rw [hs3f, hf]; exact fun h => by cases h + have hne : (ft != fl4_abi.consts.fault.NullFault) = true := by + rw [bne_iff_ne, hft] + intro c + exact hf3' (hxs3.fault0.1 (c.trans (by unfold fl4_abi.consts.fault.NullFault; rfl))) + have hred : ∀ {α : Type} (x z : α), (if dd.senderIsCall = true then x + else if (ft != fl4_abi.consts.fault.NullFault) = true then x else z) = x := by + intro α x z + by_cases c : dd.senderIsCall = true + · rw [if_pos c] + · rw [if_neg c, if_pos hne] + rcases hnp _ _ hstd with hrv0 | ⟨hng, hngr⟩ <;> + [simp only [hred, show (rv != 0#u64) = false by rw [hrv0]; simp, Bool.false_eq_true, ↓reduceIte, ite_self]; + simp only [hred, hng, hngr, Bool.false_eq_true, ↓reduceIte, ite_self]] + all_goals + have hNBI := NoBlockE.updT hks3 { xs3 with tcbState := .inactive } + (Or.inr (Or.inr ⟨rfl, (by rw [hst3]; rfl), (by rw [hst3]; exact fun h => by cases h)⟩)) + have hI := set_thread_state_relF hO hF hP s3 σ3 hR3 hS3 hsT hks3 hq3 .inactive (by decide) sched.TS_INACTIVE + (by unfold sched.TS_INACTIVE; rfl) hNBI + refine WP.spec_bind (WP.spec_and hI (set_thread_state_fpA hO.to0 hF.to0 hP hS3 s3 σ3 hR3 (WritesIn.refl _ _) + (EpStep.refl _) hsT hks3 hq3 .inactive (by decide) sched.TS_INACTIVE (by unfold sched.TS_INACTIVE; rfl) hNBI)) ?_ + rintro s4 ⟨⟨h4a, h4b, h4c⟩, hw4⟩ + rw [WP.spec_ok] + have hrun := recvSend_inactA (optRef rv) hk hks hse hstd hT hks2 hA hks3 (Or.inr hf3') + (Or.imp id (fun h => by rw [h]; exact optRef_zero) (hnp _ _ hstd).symm) + refine ⟨rfl, by rw [hrun]; exact not_false, ⟨_, by rw [hrun]; exact Set.mem_singleton _, h4a, h4b, + FpA.trans hF hpre3 hz3 h4c⟩, ?_⟩ + exact WritesIn.schedA hF h3 hz3 hw4 (fun _ h => Or.inr (Or.inl h)) (fun _ h => Or.inr (Or.inr h)) + +end + +end + +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/Stage3RecvTmoPushFpA.lean b/proofs/Fl4ProofsA64/P3/Stage3RecvTmoPushFpA.lean new file mode 100644 index 000000000..abbc0651c --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3RecvTmoPushFpA.lean @@ -0,0 +1,606 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2c): **`receive_from_sender` from a waiting sender with a timeout fault and an SC, +pushing the reply** (`rv ≠ 0`, call with grant or grant-reply semantics of a faulted sender), the receiver holding its own +SC. `reply_push` is called with `can_donate = false` (the fault is a timeout), so nothing is donated. + +* `SchedSRA.blockOutA`, `blockOnReplyOutA`, `pushPrefixRelOutA`: blocking a thread that is *outside* `inClassRSc` (the + timeout-faulted sender, whose SC may be unreleased: no `RelIfBound`) on a reply. +* `replyPushNoDonProgramA`, **`reply_push_noDonFpA`**: `reply_push .. false` (the donation branch never runs), with the + reply stacks kept and the write set `PushFpA`. +* `recvSendSelectedNoDonateA`: the program `recvSendAfterCheckA` selects for a timeout sender. +* **`receive_from_senderContTmoA`**: the dequeue and the timeout transfer (`update_consumed` writes the sender's SC) as a + continuation lemma, passing on the narrow write set (`RecvFpA` or the scheduler words), the frame facts and the + sender's SC with its reply list kept (from `doIpcTransfer_any_sikA`). +* **`receive_from_sender_pushTmoFpA`**: FL4's `receive_from_sender` runs the spec's `receive_ipc` `SendEP` arm (one outcome) + and writes only `RecvPushFpA`; `ReplyStackRelA` is carried across the `update_consumed` write. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3RecvTmoFpA +import Fl4ProofsA64.P3.Stage3RecvPushFpA +import Fl4ProofsA64.P3.Stage3SendFaultSpecA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4SpecA64 Fl4Spec Fl4ProofsA64.P3 Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Classical + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- `SchedSRA.block` for a thread whose new state is outside `inClassRSc` (no `RelIfBound` needed). -/ +theorem SchedSRA.blockOutA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s s' : S} {σ σ' : State Unit} + (H : SchedSRA st Z m κ s σ) {W : U64 → Prop} (hw : WritesIn st s s' W) (hW : ∀ a, W a → ¬ SReadA st Z m s σ a) + (hS : SStepA σ σ') {p : ObjRef} (hnq : (∀ d q, p ∉ σ.readyQueues d q) ∧ p ∉ σ.releaseQueue) + (hk : ∀ u x y, σ.kheap u = some (.tcb x) → σ'.kheap u = some (.tcb y) → u ≠ p → + y.tcbState = x.tcbState ∧ (inClassRSc y.tcbState y.tcbFault → inClassRSc x.tcbState x.tcbFault)) + {y0 : Tcb} (hy0 : σ'.kheap p = some (.tcb y0)) (hc : ¬ inClassRSc y0.tcbState y0.tcbFault) : + SchedSRA st Z m κ s' σ' := by + have k : ∀ u, u ≠ p → (∃ x, σ.kheap u = some (.tcb x) ∧ runnable x.tcbState = true) → + ∃ y, σ'.kheap u = some (.tcb y) ∧ runnable y.tcbState = true := by + rintro u hu ⟨x, hx, hr⟩ + obtain ⟨y, hy, -⟩ := hS.tcb u x hx + exact ⟨y, hy, by rw [(hk u x y hx hy hu).1]; exact hr⟩ + refine H.stableR hw hW hS ⟨fun d q u hu => ?_, fun u hu => ?_⟩ ?_ + · rw [hS.rq] at hu + exact k u (fun e => hnq.1 d q (e ▸ hu)) (H.run.1 d q u hu) + · rw [hS.rel] at hu + exact k u (fun e => hnq.2 (e ▸ hu)) (H.run.2 u hu) + · intro u y hy hcls + obtain ⟨x, hx⟩ := hS.tcbBack u y hy + by_cases e : u = p + · subst e + rw [hy0] at hy + cases hy + exact absurd hcls hc + · exact (H.rd u x hx ((hk u x y hx hy e).2 hcls)).step hS hx + +end + +end + +end Fl4ProofsA64.P3.Sched + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4SpecA64 Sched + +noncomputable section + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + +/-- Block a thread that is outside `inClassRSc` on a reply (the scheduler relation stays). -/ +theorem blockOnReplyOutA (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + {s : S} {σ : State Unit} (hR : EpStateRel Z.T E R Bf (L.mem s) σ) (hSR : IpcSchedRelA st Z m κ s σ) + {t : U64} (ht : t ∈ Z.T) (r : U64) {x : Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hEp : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) + (hQ : NotQueued σ (specPtr t)) : + sched.set_blocked_on_reply st s t r ⦃ s' => + EpStateRel Z.T E R Bf (L.mem s') + (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := .blockedOnReply (specPtr r) })) (specPtr t)) ∧ + IpcSchedRelA st Z m κ s' + (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := .blockedOnReply (specPtr r) })) (specPtr t)) ⦄ := by + let O := schedOpsEAOfPlacementA (st := st) (m := m) (κ := κ) P + obtain ⟨Q, R', H⟩ := hSR + unfold sched.set_blocked_on_reply + refine WP.spec_bind (obj.Tcb.set_state.spec (L := L) s t _) ?_ + rintro s1 rfl + refine WP.spec_bind (obj.Tcb.set_reply_obj.wr (L := L) _ t r) ?_ + rintro s2 rfl + obtain ⟨hAgree, hFields, hst, hrep, hW⟩ := blockOnReplyWrites (st := st) PE (s := s) (r := r) ht + let y : Tcb := { x with tcbState := .blockedOnReply (specPtr r) } + have hX : TcbRelE R (L.mem (L.wr (L.wr s (stA t) sched.TS_BLOCKED_ON_REPLY) (tReplyA t) r)) t y := by + refine (hR.at_tcb ht hk).restateA (PE.tcb t ht) hFields rfl rfl rfl rfl hst ?_ ?_ ?_ ?_ + · intro e h + cases h + · intro e d h + cases h + · intro e r d h + cases h + · intro r' h + cases h + rw [hrep] + have hRel := hR.upd_tcb PE ht y hAgree (Or.inl hEp) hX + have hy : (updK σ (specPtr t) (.tcb y)).kheap (specPtr t) = some (.tcb y) := by + rw [updK_kheap, Function.update_self] + have hS : IpcSchedRelA st Z m κ (L.wr (L.wr s (stA t) sched.TS_BLOCKED_ON_REPLY) (tReplyA t) r) + (updK σ (specPtr t) (.tcb y)) := + ⟨Q, R', H.blockOutA hW (fun _ h => endpWriteOffRead H.toSchedSRN P h) + (endpSchedStep (EpStep.tcb hk y.tcbState x.tcbArch)) hQ + (fun p x' y' h0 h1 hp => by + rw [updK_kheap, Function.update_of_ne hp, h0] at h1 + cases h1 + exact ⟨rfl, id⟩) hy (by simp [y, inClassRSc, runnable])⟩ + refine WP.spec_mono (O.scheduleTcb _ _ _ hRel hS ht) ?_ + rintro s' ⟨hf, hnew⟩ + exact ⟨hRel.frame PE hf (KView.stsAct' _ _), hnew⟩ + +set_option maxHeartbeats 300000 in +/-- The prefix of `reply_push` (link, block) for a thread outside `inClassRSc`. -/ +theorem pushPrefixRelOutA (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + {s : S} {σ : State Unit} (H : IpcSchedRelA st Z m κ s σ) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) {f r : U64} (hf : f ∈ Z.T) (hr : r ∈ R) + {xf : Tcb} {xr : Reply} (hkf : σ.kheap (specPtr f) = some (.tcb xf)) (hkr : σ.kheap (specPtr r) = some (.reply xr)) + (hEp : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr f ∉ z.epQueue) + (hQ : NotQueued σ (specPtr f)) : + pushPrefixProgramA (st := st) s f r ⦃ out => + EpStateRel Z.T E R Bf (L.mem out) (pushBlockedStateA σ (specPtr f) (specPtr r) xf xr) ∧ + IpcSchedRelA st Z m κ out (pushBlockedStateA σ (specPtr f) (specPtr r) xf xr) ⦄ := by + let O := schedOpsEAOfPlacementA (st := st) (m := m) (κ := κ) P + let σ1 := updK σ (specPtr r) (.reply { xr with replyTcb := some (specPtr f) }) + have hne : specPtr f ≠ specPtr r := by intro e; rw [e, hkr] at hkf; cases hkf + have hf1 : σ1.kheap (specPtr f) = some (.tcb xf) := by simp [σ1, hne, hkf] + have hEp1 : ∀ e ∈ E, ∀ z, σ1.kheap (specPtr e) = some (.endpoint z) → specPtr f ∉ z.epQueue := by + intro e he z hz + by_cases h : specPtr e = specPtr r + · simp [σ1, h] at hz + · exact hEp e he z (by simpa [σ1, h] using hz) + have hQ1 : NotQueued σ1 (specPtr f) := hQ + unfold pushPrefixProgramA + refine WP.spec_bind (linkReplyA O.to0 PE HE H hf hr hkr) ?_ + rintro s1 ⟨E1, H1⟩ + exact blockOnReplyOutA P PE E1 H1 hf r hf1 hEp1 hQ1 + +set_option maxHeartbeats 300000 in +def pushNoDonBranchA (s : S) (a : A) (sc d : U64) : Result (S × A) := do + let b ← obj.Sc.is_null sc + if b then ok (s, a) + else + let ds ← obj.Tcb.sc st d s + if ds = 0#u64 then ok (s, a) else ok (s, a) + +set_option maxHeartbeats 300000 in +theorem pushNoDonBranchEqA (s : S) (a : A) (sc d : U64) : + pushNoDonBranchA (st := st) s a sc d = ok (s, a) := by + unfold pushNoDonBranchA + rw [eq_of_spec_fd (obj.Sc.is_null.spec sc), bind_ok] + by_cases h0 : sc = 0#u64 + · rw [decide_eq_true h0] + rfl + · rw [decide_eq_false h0, if_neg Bool.false_ne_true, eq_of_spec_fd (obj.Tcb.sc.spec (L := L) s d), bind_ok] + split <;> rfl + +set_option maxHeartbeats 300000 in +theorem replyPushNoDonProgramA (s : S) (a : A) (f d r sc : U64) + (h : obj.Tcb.sc st f s = ok sc) : + ipc.reply_push st ar s a f d r false = + (pushPrefixProgramA (st := st) s f r >>= fun s' => pushNoDonBranchA (st := st) s' a sc d) := by + unfold ipc.reply_push + rw [h, bind_ok] + simp only [pushPrefixProgramA, pushNoDonBranchA, Std.bind_tc_eq, Std.bind_assoc, Bool.false_eq_true, ↓reduceIte, + ite_self] + +set_option maxHeartbeats 400000 in +/-- **`reply_push` without donation** (`can_donate = false`) of a thread outside `inClassRSc` to the receiver `d`: the +reply stacks are unchanged, the write set is `PushFpA`. -/ +theorem reply_push_noDonFpA {s : S} {σ : State Unit} (a : A) {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {r f d : U64} (hr : r ∈ R) (hf : f ∈ Z.T) (hfd : f ≠ d) {xd : Tcb} + (hkd : σ.kheap (specPtr d) = some (.tcb xd)) + (hEp : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr f ∉ z.epQueue) + (hQ : NotQueued σ (specPtr f)) : + ipc.reply_push st ar s a f d r false ⦃ out => out.2 = a ∧ ∃ σ', + replyPush (Z := Unit) (specPtr f) (specPtr d) (specPtr r) false σ = ({((), σ')}, False) ∧ + IpcSchedRelA st Z m κ out.1 σ' ∧ EpStateRel Z.T E R Bf (L.mem out.1) σ' ∧ + ReplyStackRelA (L.mem out.1) R Z.C σ' (scNameA m) stack ∧ + WritesIn st s out.1 (PushFpA st Z m R σ s f) ⦄ := by + obtain ⟨xf, hkf, -⟩ := H.sch.tcb f hf + obtain ⟨xr, hkr, -⟩ := HE.reply r hr + have hSCRead : obj.Tcb.sc st f s = ok (L.mem s (scA f)) := + eq_of_spec_fd (obj.Tcb.sc.spec (L := L) s f) + have hn : specPtr f ≠ specPtr r := by intro e; rw [e, hkr] at hkf; cases hkf + have hf1 : (updK σ (specPtr r) (.reply { xr with replyTcb := some (specPtr f) })).kheap (specPtr f) = + some (.tcb xf) := by + rw [updK_kheap, Function.update_of_ne hn] + exact hkf + have hSpec : replyPush (Z := Unit) (specPtr f) (specPtr d) (specPtr r) false σ = + ({((), pushBlockedStateA σ (specPtr f) (specPtr r) xf xr)}, False) := by + unfold replyPush + rw [nd_bind_one (getTcbObjRef_eq _ hkf), nd_bind_one (getTcbObjRef_eq _ hkd), + nd_bind_one (setReplyTcb_eq _ hkr), nd_bind_one (setThreadState_eq _ hf1)] + simp [Fl4Spec.NonDet.when, Fl4Spec.NonDet.pure, pushBlockedStateA] + rw [replyPushNoDonProgramA (ar := ar) s a f d r _ hSCRead] + have hPrefix := WP.spec_and (pushPrefixRelOutA P PE ⟨Z.Q, Z.R, H⟩ HE hf hr hkf hkr hEp hQ) + (pushPrefixWrites H PE hf hr) + obtain ⟨keep, linked, hf2, hd2, hQ2⟩ := pushBlockedHeapFactsA hkf hkd hkr hfd hQ + refine WP.spec_bind hPrefix ?_ + rintro s2 ⟨⟨E2, S2⟩, w2⟩ + obtain ⟨-, links⟩ := pushPrefixVcpuAndLinks H P PE hf hr w2 + have HS2 : ReplyStackRelA (L.mem s2) R Z.C (pushBlockedStateA σ (specPtr f) (specPtr r) xf xr) (scNameA m) stack := + pushFrameAfterBlockA HS HE hkf hkr keep linked links + rw [pushNoDonBranchEqA] + refine (WP.spec_ok _).2 ⟨rfl, _, hSpec, S2, E2, HS2, ?_⟩ + refine w2.mono fun b h => ?_ + rcases h with h | h | h | h + · exact Or.inl ⟨r, hr, by rw [h, rTcbA_val], by rw [h, rTcbA_val]; omega⟩ + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr (Or.inl h)) + · refine Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ?_)))) + rw [h]; exact H.node_sks H.sch.cpuCs (by simp [layout.node.SCHED_ACTION]) + +/-- The program `recvSendAfterCheckA` selects for a sender with a timeout fault that pushes its reply: no donation. -/ +theorem recvSendSelectedNoDonateA (s : S) (a : A) (t f r : U64) (dd : SenderPayload) + (hr : r ≠ 0#u64) (hg : dd.senderCanGrant = true ∨ dd.senderCanGrantReply = true) + (hcf : dd.senderIsCall = true ∨ L.mem s (tFaultA f) ≠ fl4_abi.consts.fault.NullFault) + (hft : L.mem s (tFaultA f) = fl4_abi.consts.fault.Timeout) : + recvSendAfterCheckA (st := st) (ar := ar) s a t f r dd = + ipc.reply_push st ar s a f t r false := by + have hr' : (r != 0#u64) = true := bne_iff_ne.mpr hr + have hnt : (L.mem s (tFaultA f) != fl4_abi.consts.fault.Timeout) = false := by + rw [hft]; exact bne_self_eq_false _ + unfold recvSendAfterCheckA + rw [eq_of_spec_fd (obj.Tcb.fault_type.spec (L := L) s f), bind_ok] + by_cases hcall : dd.senderIsCall = true + all_goals + first + | rw [if_pos hcall] + | have hFault : (L.mem s (tFaultA f) != fl4_abi.consts.fault.NullFault) = true := + bne_iff_ne.mpr (hcf.resolve_left hcall) + rw [if_neg hcall, if_pos hFault] + by_cases h : dd.senderCanGrant = true + · rw [if_pos h, if_pos hr', eq_of_spec_fd (obj.Tcb.sc.spec (L := L) s f), bind_ok] + by_cases hsc : (L.mem s (scA f) != 0#u64) = true + · rw [if_pos hsc, hnt, bind_ok] + · rw [if_neg hsc, bind_ok] + · rw [if_neg h, if_pos (hg.resolve_left h), if_pos hr', eq_of_spec_fd (obj.Tcb.sc.spec (L := L) s f), bind_ok] + by_cases hsc : (L.mem s (scA f) != 0#u64) = true + · rw [if_pos hsc, hnt, bind_ok] + · rw [if_neg hsc, bind_ok] + +end + +end + +end Fl4ProofsA64.P3.Endp + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched + +noncomputable section + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] {T E R : Set U64} + {Bf : Set (U64 × Fl4SpecA64.Word)} {SR : S → Fl4SpecA64.State Unit → Prop} + {Wr : Fl4SpecA64.State Unit → S → U64 → Prop} {NotIdle : U64 → Prop} + +open Fl4SpecA64 in +set_option maxHeartbeats 1000000 in +/-- **`receive_from_sender` from a sender with a timeout fault and an SC, as a continuation lemma** (module +docstring). -/ +theorem receive_from_senderContTmoA (hO : SchedOpsEA st T E R Bf SR) + (hTO : TimeoutScOpA st ar T E R Bf SR NotIdle) + (hP : EPlaced T E R Bf) {th : TypedHeapA} {kb : U64} (hPl : PlaceA th kb) + (s : S) (a : A) (σ : State Unit) (hR : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) + (hSt : StateRelA th (L.mem s) σ) (hV : SlotsValidA th (L.mem s)) (hC : BufCover st T Bf s σ) + (hNative : BufNativeA T (L.mem s)) + (hoff : ∀ y, IsSlotA th y → ¬ EWrite T E R Bf y ∧ ¬ EWrite T E R Bf (core.num.U64.wrapping_add y 8#u64)) + {t : U64} (ht : t ∈ T) (hth : th t = some .tcb) {xt : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hnb : tsCode xt.tcbState ≠ 4) + (hal : ∀ rb, ipcBufOfA true xt = some rb → (bufAddr rb 0).toNat % 8 = 0) + {e sd : U64} (he : e ∈ E) {q : List ObjRef} + (hk : σ.kheap (specPtr e) = some (.endpoint (.sendEP (specPtr sd :: q)))) + (hsh : th sd = some .tcb) {xs : Tcb} (hks : σ.kheap (specPtr sd) = some (.tcb xs)) + {bd : Word} (hf : xs.tcbFault = some (.timeout bd)) + (hni : NotIdle sd) (hsc0 : L.mem s (scA sd) ≠ 0#u64) + (hsc : xs.tcbSchedContext = some (specPtr (L.mem s (scA sd)))) + {y : SchedContext} {n : Nat} (hky : σ.kheap (specPtr (L.mem s (scA sd))) = some (.schedContext y n)) + (hscW : ∀ b : U64, (L.mem s (scA sd)).val ≤ b.val ∧ + b.val < (L.mem s (scA sd)).val + 2 ^ (n + minSchedContextBits) → Wr σ s b) + (rv : U64) {Post : S × A → Prop} + (hFinish : ∀ {s2 : S} {σ2 : State Unit} {dd : SenderPayload}, + sd ∈ T → t ≠ sd → specPtr sd ≠ specPtr e → + xs.tcbState = .blockedOnSend (specPtr e) dd → + EpStateRel T E R Bf (L.mem s2) σ2 → SR s2 σ2 → + σ2.kheap (specPtr sd) = some (.tcb xs) → + (∀ e' ∈ E, ∀ z, σ2.kheap (specPtr e') = some (.endpoint z) → specPtr sd ∉ z.epQueue) → + (doIpcTransfer (Z := Unit) (specPtr sd) (some (specPtr e)) dd.senderBadge dd.senderCanGrant (specPtr t) + (updK σ (specPtr e) (.endpoint (epDeq (.sendEP (specPtr sd :: q)) (specPtr sd)))) = + ({((), σ2)}, False)) → + WritesIn st s s2 (fun b => RecvFpA st T Bf s e t b ∨ Wr σ s b) → + SSzA σ σ2 → σ2.schedulerAction = σ.schedulerAction → σ2.idleThread = σ.idleThread → + (∃ y', σ2.kheap (specPtr (L.mem s (scA sd))) = some (.schedContext y' n) ∧ y'.scReplies = y.scReplies) → + (∀ p, p ≠ specPtr t → p ≠ specPtr (L.mem s (scA sd)) → p ≠ specPtr e → σ2.kheap p = σ.kheap p) → + (∃ x', σ2.kheap (specPtr t) = some (.tcb x') ∧ x'.tcbSchedContext = xt.tcbSchedContext) → + recvSendAfterTransferA (st := st) (ar := ar) s2 a t sd rv dd ⦃ Post ⦄) : + endpoint.receive_from_sender st ar s a t e rv ⦃ Post ⦄ := by + obtain ⟨hst1, xs0, hxs0, hq, hQ, hB⟩ := hR.at_ep he hk + obtain ⟨sd', xs', rfl⟩ := List.exists_cons_of_ne_nil hxs0 + simp only [List.map_cons, List.cons.injEq] at hq + obtain ⟨hd', hq'⟩ := hq + have hdd : sd = sd' := specPtr_inj hd' + subst hdd + have hsT : sd ∈ T := hQ.mem sd (by simp) + have hsa := hP.tcb sd hsT + have hxs := hR.at_tcb hsT hks + have hxt := hR.at_tcb ht hkt + have hs4 : (L.mem s (stA sd)).val = 4 := (hB sd (by simp)).1 + have hsb : L.mem s (tBlockA sd) = e := (hB sd (by simp)).2 + obtain ⟨e', dd, hstd⟩ := tsCode_four (hxs.st.symm.trans hs4) + have he' : e' = specPtr e := by + have := hxs.blk e' (by rw [hstd]; rfl); rw [hsb] at this; exact this.symm + subst he' + obtain ⟨hbadge, hflags⟩ := hxs.send _ _ hstd + have hts : t ≠ sd := by intro h; subst h; exact hnb (hxt.st.symm.trans hs4) + have hse : specPtr sd ≠ specPtr e := kheap_ne hks hk (fun _ h1 h2 => by subst h1; cases h2) + have hte : specPtr t ≠ specPtr e := kheap_ne hkt hk (fun _ h1 h2 => by subst h1; cases h2) + have hts' : specPtr t ≠ specPtr sd := fun h => hts (specPtr_inj h) + have hsw : specPtr sd ≠ specPtr (L.mem s (scA sd)) := fun h => by rw [h, hky] at hks; cases hks + have htw : specPtr t ≠ specPtr (L.mem s (scA sd)) := fun h => by rw [h, hky] at hkt; cases hkt + have hwe : specPtr (L.mem s (scA sd)) ≠ specPtr e := fun h => by rw [h, hk] at hky; cases hky + unfold endpoint.receive_from_sender + step with endpoint.get_queue.spec as ⟨q0, hq0⟩ + have hhead : q0.head = sd := by rw [hq0, epQ_eq]; exact hQ.head + rw [hhead] + have hd : specPtr sd ∈ (Endpoint.sendEP (specPtr sd :: q)).epQueue := by simp [Endpoint.epQueue] + refine WP.spec_bind (WP.spec_and (ep_dequeue_raw hP s σ hR he hk hd) + (ep_dequeue_full hP s he sd (sd :: xs') hQ (by simp))) ?_ + rintro s1 ⟨⟨-, hR1, hW1, hE1, hA1⟩, -, -, hQA, -⟩ + have hS1 := hO.stable _ _ _ _ hSR hW1 hE1 (NoBlockE.updO _ _ fun _ h => by cases h) + set σ1 := updK σ (specPtr e) (.endpoint (epDeq (.sendEP (specPtr sd :: q)) (specPtr sd))) with hσ1 + have hkt1 : σ1.kheap (specPtr t) = some (.tcb xt) := by rw [hσ1, updK_kheap, Function.update_of_ne hte, hkt] + have hks1 : σ1.kheap (specPtr sd) = some (.tcb xs) := by rw [hσ1, updK_kheap, Function.update_of_ne hse, hks] + have hky1 : σ1.kheap (specPtr (L.mem s (scA sd))) = some (.schedContext y n) := by + rw [hσ1, updK_kheap, Function.update_of_ne hwe, hky] + have hsc1 : L.mem s1 (scA sd) = L.mem s (scA sd) := + hW1.2.2 _ (field_aligned (hP.tcb sd hsT) (by simp) (by simp)) + (not_ew_offA hP hsT (K := 3464#u64) (by simp) (by unfold TWOffN; simp) (by simp) (by simp)) + have hbw : L.mem s1 (tBadgeA sd) = L.mem s (tBadgeA sd) := hA1 _ (a_badge hsa).1 ⟨sd, hsT, (a_badge hsa).2⟩ + have hfw : L.mem s1 (tFlagsA sd) = L.mem s (tFlagsA sd) := hA1 _ (a_flags hsa).1 ⟨sd, hsT, (a_flags hsa).2⟩ + step with obj.Tcb.block_badge.spec as ⟨bdg, hbdg⟩ + step with obj.Tcb.block_flags.spec as ⟨fl, hfl⟩ + simp only [lift, bind_ok] + have hg : (fl &&& endpoint.FLAG_GRANT != 0#u64) = dd.senderCanGrant := by + rw [hfl, hfw, hflags]; exact flags_grantA dd + have hgr : (fl &&& endpoint.FLAG_GRANT_REPLY != 0#u64) = dd.senderCanGrantReply := by + rw [hfl, hfw, hflags]; exact flags_grantReplyA dd + have hc : (fl &&& endpoint.FLAG_CALL != 0#u64) = dd.senderIsCall := by + rw [hfl, hfw, hflags]; exact flags_callA dd + have hbb : bdg.bv = dd.senderBadge := by rw [hbdg, hbw]; exact hbadge + rw [hg, hgr, hc] + have hmem : ∀ z, IsSlotA th z → L.mem s1 z = L.mem s z ∧ + L.mem s1 (core.num.U64.wrapping_add z 8#u64) = L.mem s (core.num.U64.wrapping_add z 8#u64) := by + intro z hz + have hal' : Aligned z := by have := (IsSlotA.al32 hPl hz).1; unfold Aligned; omega + exact ⟨hW1.2.2 z hal' (hoff z hz).1, hW1.2.2 _ (wa_al hal' (by simp)) (hoff z hz).2⟩ + have hSt1 := hSt.updEpA hPl hmem hk (epDeq (.sendEP (specPtr sd :: q)) (specPtr sd)) + have hV1 : SlotsValidA th (L.mem s1) := slotsValid_xferA hV hmem + have hC1 : BufCover st T Bf s1 σ1 := + BufCover.ewA hP hC hW1 fun u _ x hx => by + have hne : specPtr u ≠ specPtr e := fun h => by + rw [h, hσ1, updK_kheap, Function.update_self] at hx; cases hx + rwa [hσ1, updK_kheap, Function.update_of_ne hne] at hx + -- the transfer, with `update_consumed` + have htr : ipc.do_ipc_transfer st ar s1 a sd e bdg dd.senderCanGrant t ⦃ s2 => ∃ σ2, + doIpcTransfer (Z := Unit) (specPtr sd) (some (specPtr e)) bdg.bv dd.senderCanGrant (specPtr t) σ1 = + ({((), σ2)}, False) ∧ + EpStateRel T E R Bf (L.mem s2) σ2 ∧ SR s2 σ2 ∧ + (∃ s0, WritesIn st s0 s2 (EWrite T E R Bf) ∧ + WritesIn st s1 s0 (fun b => (L.mem s1 (scA sd)).val ≤ b.val ∧ + b.val < (L.mem s1 (scA sd)).val + 2 ^ (n + minSchedContextBits)) ∧ + Agree (L.mem s0) (L.mem s2) (fun b => ¬ RcvRegA t b ∧ ¬ BufW Bf b)) ∧ + (σ2.idleThread = σ1.idleThread ∧ σ2.schedulerAction = σ1.schedulerAction) ∧ + (∃ y', σ2.kheap (specPtr (L.mem s1 (scA sd))) = some (.schedContext y' n)) ∧ + (∀ p, p ≠ specPtr t → p ≠ specPtr (L.mem s1 (scA sd)) → σ2.kheap p = σ1.kheap p) ∧ + ∃ x', σ2.kheap (specPtr t) = some (.tcb x') ∧ x' = { xt with tcbArch := x'.tcbArch } ⦄ := by + have hspec : doIpcTransfer (Z := Unit) (specPtr sd) (some (specPtr e)) bdg.bv dd.senderCanGrant (specPtr t) σ1 = + doFaultTransfer (Z := Unit) bdg.bv (specPtr sd) (specPtr t) (ipcBufOfA true xt) σ1 := by + unfold doIpcTransfer + rw [nd_bind_one (lookupIpcBuffer_eqA hkt1 true), nd_bind_one (threadGet_eq _ hks1), hf] + unfold ipc.do_ipc_transfer + refine WP.spec_bind (lookup_buf_full s1 σ1 hSt1 hth ht hkt1 hC1 (hNative.ewA hP hW1) true) ?_ + rintro rbw ⟨hrw0, hrwB⟩ + step with obj.Tcb.fault_type.spec as ⟨ft, hft⟩ + have hft0 : ¬ ft = fl4_abi.consts.fault.NullFault := by + intro h + have : L.mem s1 (tFaultA sd) = 0#u64 := by rw [← hft, h]; unfold fl4_abi.consts.fault.NullFault; rfl + rw [(hR1.at_tcb hsT hks1).fault0.1 this] at hf; cases hf + rw [if_neg hft0] + refine WP.spec_mono (do_fault_transfer_timeoutScA hTO hO.to0 hP s1 a σ1 hR1 hS1 hsT ht (Ne.symm hts) hks1 hkt1 hf + (by rw [hsc1]; exact hsc) hni (by rw [hsc1]; exact hsc0) (by rw [hsc1]; exact hky1) + (buf_caseA hrw0 hrwB) bdg) ?_ + rintro s2 ⟨σ2, h1, b1, b2, ⟨s0, -, c2, c3, c4⟩, ⟨-, -, d3, d4⟩, b5, b6, x', b7, b8⟩ + exact ⟨σ2, hspec.trans h1, b1, b2, ⟨s0, c2, c3, c4⟩, d3, d4, b5, b6, x', b7, b8⟩ + refine WP.spec_bind htr ?_ + rintro s2 ⟨σ2, hT, hR2, hS2, ⟨s0, hW2, hWsc, hAg⟩, ⟨hidle2, hact2⟩, ⟨y', hky2⟩, hfr2, x2, hkt2, hx2⟩ + rw [hsc1] at hky2 hfr2 hWsc + rw [hbb] at hT + -- the writes up to here + have hfp : WritesIn st s s2 (fun b => RecvFpA st T Bf s e t b ∨ Wr σ s b) := by + refine ⟨hW2.1.trans (hWsc.1.trans hW1.1), hW2.2.1.trans (hWsc.2.1.trans hW1.2.1), fun b hb hn => ?_⟩ + have a1 : L.mem s2 b = L.mem s0 b := hAg b hb + ⟨fun h => hn (Or.inl (Or.inr (Or.inr (Or.inl h)))), fun h => hn (Or.inl (Or.inr (Or.inr (Or.inr h))))⟩ + have a2 : L.mem s0 b = L.mem s1 b := hWsc.2.2 b hb fun h => hn (Or.inr (hscW b h)) + have a3 : L.mem s1 b = L.mem s b := hQA b hb ⟨fun h => hn (Or.inl (Or.inl h)), + fun ⟨w, hw, hl⟩ => hn (Or.inl (Or.inr (Or.inl ⟨w, hQ.mem w hw, (hB w hw).2, hl⟩)))⟩ + exact a1.trans (a2.trans a3) + have hz1 : SSzA σ σ1 := ssz_of_epStep hE1 + have hz2 : SSzA σ σ2 := by + refine hz1.trans fun p z k hz => ?_ + by_cases hpw : p = specPtr (L.mem s (scA sd)) + · rw [hpw, hky2] at hz + cases hz + exact ⟨y, by rw [hpw]; exact hky1⟩ + by_cases hpt : p = specPtr t + · rw [hpt, hkt2] at hz; cases hz + exact ⟨z, by rw [← hfr2 p hpt hpw]; exact hz⟩ + have hks2 : σ2.kheap (specPtr sd) = some (.tcb xs) := by rw [hfr2 _ (Ne.symm hts') hsw, hks1] + have hk1e : σ1.kheap (specPtr e) = some (.endpoint (epDeq (.sendEP (specPtr sd :: q)) (specPtr sd))) := by + rw [hσ1, updK_kheap, Function.update_self] + have hsb1 : L.mem s1 (tBlockA sd) = e := by rw [hA1 _ (a_blk hsa).1 ⟨sd, hsT, (a_blk hsa).2⟩, hsb] + have hq1 := dequeued_not_queued hR1 he hsb1 hk1e + (epDeq_not_mem (z := .sendEP (specPtr sd :: q)) (xs := sd :: xs') (by rw [hq']; rfl) hQ.nodup sd) + have hq2 : ∀ e'' ∈ E, ∀ z, σ2.kheap (specPtr e'') = some (.endpoint z) → specPtr sd ∉ z.epQueue := by + intro e'' he'' z hz + have hne : specPtr e'' ≠ specPtr t := by intro h; rw [h, hkt2] at hz; cases hz + have hnw : specPtr e'' ≠ specPtr (L.mem s (scA sd)) := by intro h; rw [h, hky2] at hz; cases hz + exact hq1 e'' he'' z (by rwa [hfr2 _ hne hnw] at hz) + -- the sender's SC keeps its reply list (`doIpcTransfer_any_sikA`) + have sk : SIKA (specPtr sd) (fun _ => False) (fun _ => False) false σ1 σ2 := by + have hTm : ((), σ2) ∈ (doIpcTransfer (Z := Unit) (specPtr sd) (some (specPtr e)) dd.senderBadge + dd.senderCanGrant (specPtr t) σ1).1 := by + rw [hT]; exact Set.mem_singleton _ + exact (doIpcTransfer_any_sikA (i := specPtr sd) (M := fun _ => False) (N := fun _ => False) hPl s1 hSt1 hV1 hth + hks1 hkt1 hal (some (specPtr e)) dd.senderBadge dd.senderCanGrant (Or.inr ⟨_, hf⟩)) σ1 rfl _ hTm + have hrep : y'.scReplies = y.scReplies := by + have o := sk.obj (specPtr (L.mem s (scA sd))) + rw [hky1, hky2] at o + cases o with + | same => rfl + | sc _ _ _ g => exact g.reps rfl + have act1 : σ1.schedulerAction = σ.schedulerAction := by + have eq := congrArg AbstractState.schedulerAction hE1.2.1 + exact eq + have idle1 : σ1.idleThread = σ.idleThread := (SchedKeepA.step hE1).2.2.1 + exact hFinish hsT hts hse hstd hR2 hS2 hks2 hq2 hT hfp hz2 (hact2.trans act1) (hidle2.trans idle1) + ⟨y', hky2, hrep⟩ + (fun p h1 h2 h3 => by + rw [hfr2 p h1 h2, hσ1, updK_kheap, Function.update_of_ne h3]) + ⟨x2, hkt2, by have := congrArg Tcb.tcbSchedContext hx2; exact this⟩ + +end + +end + +end Fl4ProofsA64.P3.Endp + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4SpecA64 Sched + +noncomputable section + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + +/-- `RecvFpA`'s words are endpoint-code words. -/ +theorem recvFp_ewA {s : S} (PE : EPlaced Z.T E R Bf) {t e b : U64} (ht : t ∈ Z.T) (he : e ∈ E) + (h : RecvFpA st Z.T Bf s e t b) : EWrite Z.T E R Bf b := by + have fld : ∀ u ∈ Z.T, ∀ k, TWOffN k → b.val = u.val + k → EWrite Z.T E R Bf b := fun u hu k hk hb => + Or.inr (Or.inr (Or.inl ⟨u, hu, Or.inl ⟨by omega, by rw [hb, Nat.add_sub_cancel_left]; exact hk⟩⟩)) + rcases h with c | ⟨u, hu, -, c⟩ | ⟨r, c⟩ | c + · exact Or.inl ⟨e, he, c⟩ + · exact Or.inr (Or.inr (Or.inl ⟨u, hu, Or.inr c⟩)) + · have h1 := regIdxA_lt r + rw [Endp.regA_val (PE.tcb t ht) h1] at c + exact fld t ht (256 + 8 * (regIdxA r).val) (by unfold TWOffN; omega) (by omega) + · exact Or.inr (Or.inr (Or.inr c)) + +set_option maxHeartbeats 1000000 in +/-- **`receive_from_sender` from a sender with a timeout fault and an SC, pushing the reply** (module docstring). -/ +theorem receive_from_sender_pushTmoFpA {s : S} {σ : State Unit} (a : A) {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {kb : U64} (hPl : PlaceA Z.th kb) (hSt : StateRelA Z.th (L.mem s) σ) (hVld : SlotsValidA Z.th (L.mem s)) + (hC : BufCover st Z.T Bf s σ) (hNative : BufNativeA Z.T (L.mem s)) + (hoff : ∀ y, IsSlotA Z.th y → ¬ EWrite Z.T E R Bf y ∧ + ¬ EWrite Z.T E R Bf (core.num.U64.wrapping_add y 8#u64)) + {t : U64} (ht : t ∈ Z.T) {xt : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hnb : tsCode xt.tcbState ≠ 4) + (hal : ∀ rb, ipcBufOfA true xt = some rb → (bufAddr rb 0).toNat % 8 = 0) + {e f : U64} (he : e ∈ E) {q : List ObjRef} + (hk : σ.kheap (specPtr e) = some (.endpoint (.sendEP (specPtr f :: q)))) + (hf : f ∈ Z.T) {xs : Tcb} (hks : σ.kheap (specPtr f) = some (.tcb xs)) + {bd : Word} (hft : xs.tcbFault = some (.timeout bd)) + (hTO : TimeoutScOpA st ar Z.T E R Bf (IpcSchedRelA st Z m κ) + (fun u => ∀ c < fl4A64Platform.numNodes, specPtr u ≠ m.idleThreadOf c)) + (hni : ∀ c < fl4A64Platform.numNodes, specPtr f ≠ m.idleThreadOf c) + (hsc0 : L.mem s (scA f) ≠ 0#u64) (hsc : xs.tcbSchedContext = some (specPtr (L.mem s (scA f)))) + {y : SchedContext} {kk : Nat} (hky : σ.kheap (specPtr (L.mem s (scA f))) = some (.schedContext y kk)) + (hscW : ∀ b : U64, (L.mem s (scA f)).val ≤ b.val ∧ + b.val < (L.mem s (scA f)).val + 2 ^ (kk + minSchedContextBits) → MWrA (st := st) Z m σ s b) + {r : U64} (hr : r ∈ R) + (hgrant : ∀ e' d, xs.tcbState = .blockedOnSend e' d → d.senderCanGrant = true ∨ d.senderCanGrantReply = true) : + endpoint.receive_from_sender st ar s a t e r ⦃ out => out.2 = a ∧ + ∃ σ', specRecvSendA (specPtr t) (specPtr e) (some (specPtr r)) (specPtr f :: q) σ = ({((), σ')}, False) ∧ + EpStateRel Z.T E R Bf (L.mem out.1) σ' ∧ IpcSchedRelA st Z m κ out.1 σ' ∧ + ReplyStackRelA (L.mem out.1) R Z.C σ' (scNameA m) stack ∧ + WritesIn st s out.1 (RecvPushFpA st Z m R Bf σ s t e f) ⦄ := by + let O := schedOpsEAOfPlacementA (st := st) (m := m) (κ := κ) P + have F : SchedFootEA st Z.T E R Bf (IpcSchedRelA st Z m κ) (fun σ s => MWrA (st := st) Z m σ s) := + schedFootEA_of_sched + refine receive_from_senderContTmoA (Wr := fun σ s => MWrA (st := st) Z m σ s) O hTO PE hPl s a σ HE ⟨Z.Q, Z.R, H⟩ + hSt hVld hC hNative hoff ht ((H.thT t).2 ht) hkt hnb hal he hk ((H.thT f).2 hf) hks hft hni hsc0 hsc hky hscW r ?_ + intro s2 σ2 dd hsT htf hfe hst E2 hS2 hk2 hq2 hT hfp hz2 act2 idle2 ⟨y2, hy2, hrep2⟩ hfr2 ⟨x2, hkt2, hx2⟩ + obtain ⟨Q2, R2, H2⟩ := hS2 + have HS2 : ReplyStackRelA (L.mem s2) R Z.C σ2 (scNameA m) stack := by + refine HS.frame (fun r' hr' => ?_) (fun c _ x kk' hkx => ?_) (fun r' hr' x' hx' => ?_) + · obtain ⟨na, nv⟩ := replyNextPlaceA (Z := Z) PE hr' + obtain ⟨pa, pv⟩ := replyPrevPlaceA (Z := Z) PE hr' + have no : ∀ b : U64, Aligned b → r'.val < b.val → b.val < r'.val + 32 → L.mem s2 b = L.mem s b := by + intro b hb hl hh + refine hfp.mem_eq hb ?_ + rintro (hw | hw) + · exact replyLinkOffEp H.toSchedSRN P PE hr' hl hh (recvFp_ewA PE ht he hw).foot + · exact (replyWordOffSchedA H.toSchedSRN P hr' hl.le hh).2 hw + exact ⟨no _ na (by omega) (by omega), no _ pa (by omega) (by omega)⟩ + · by_cases hcs : scNameA m c = specPtr (L.mem s (scA f)) + · rw [hcs, hky] at hkx + cases hkx + exact ⟨y2, _, by rw [hcs]; exact hy2, hrep2⟩ + · refine ⟨x, kk', ?_, rfl⟩ + have hne_t : scNameA m c ≠ specPtr t := fun h => by rw [h, hkt] at hkx; cases hkx + have hne_e : scNameA m c ≠ specPtr e := fun h => by rw [h, hk] at hkx; cases hkx + rw [hfr2 _ hne_t hcs hne_e]; exact hkx + · have hne_t : specPtr r' ≠ specPtr t := fun h => by rw [h, hkt2] at hx'; cases hx' + have hne_s : specPtr r' ≠ specPtr (L.mem s (scA f)) := fun h => by rw [h, hy2] at hx'; cases hx' + have hne_e : specPtr r' ≠ specPtr e := by + obtain ⟨z, hz, -⟩ := E2.ep e he + intro h; rw [h, hz] at hx'; cases hx' + exact ⟨x', by rw [← hfr2 _ hne_t hne_s hne_e]; exact hx', rfl⟩ + obtain ⟨hcall, hgr⟩ : (dd.senderIsCall = true ∨ xs.tcbFault ≠ none) ∧ + (dd.senderCanGrant = true ∨ dd.senderCanGrantReply = true) := + ⟨Or.inr (by rw [hft]; exact fun h => by cases h), hgrant _ dd hst⟩ + have hnr : runnable xs.tcbState = false := by rw [hst]; rfl + have hr0 : r ≠ 0#u64 := fun eq => (PE.rep r hr).2.1 (by rw [eq]; rfl) + unfold recvSendAfterTransferA + refine WP.spec_bind (recvPushSporadicA H2 P PE E2 HS2 hf hk2 hnr) ?_ + rintro s1 ⟨σ1, hA, H1, E1, S1, w1, keep, hact, hidle⟩ + have hk1 := keep (specPtr f) (.tcb xs) hk2 (fun _ _ e' => by cases e') + have ht1 := keep (specPtr t) (.tcb x2) hkt2 (fun _ _ e' => by cases e') + have hEp1 : ∀ e' ∈ E, ∀ z, σ1.kheap (specPtr e') = some (.endpoint z) → specPtr f ∉ z.epQueue := by + intro e' he' z hz + obtain ⟨x, hx, _⟩ := E2.ep e' he' + have hx' := keep (specPtr e') (.endpoint x) hx (fun _ _ e'' => by cases e'') + have hEq := KernelObject.endpoint.inj (Option.some.inj (hz.symm.trans hx')) + exact hq2 e' he' z (hEq.symm ▸ hx) + have hE1 := E1.at_tcb hf hk1 + have hftT : L.mem s1 (tFaultA f) = fl4_abi.consts.fault.Timeout := + (hE1.faultT.2 ⟨bd, hft⟩).trans (by unfold fl4_abi.consts.fault.Timeout; rfl) + have hcf : dd.senderIsCall = true ∨ L.mem s1 (tFaultA f) ≠ fl4_abi.consts.fault.NullFault := + Or.inr fun c => by + rw [hftT] at c + exact absurd c (by unfold fl4_abi.consts.fault.Timeout fl4_abi.consts.fault.NullFault; decide) + rw [recvSendSelectedNoDonateA (ar := ar) s1 a t f r dd hr0 hgr hcf hftT] + refine WP.spec_mono (reply_push_noDonFpA a H1 P PE E1 S1 hr hf (Ne.symm htf) (xd := x2) ht1 hEp1 + (H1.notQueued hk1 hnr)) ?_ + rintro out ⟨ha, σ', hD, H', E', S', w2⟩ + have hflag : decide (xs.tcbSchedContext ≠ none ∧ ¬ (xs.tcbFault.elim false isTimeoutFault = true)) = false := + decide_eq_false fun h => h.2 (by rw [hft]; rfl) + have hspec : specRecvSendA (specPtr t) (specPtr e) (some (specPtr r)) (specPtr f :: q) σ = ({((), σ')}, False) := by + rw [recvSend_pushSpecA hk hks hfe hst hT hk2 hA hk1 hcall hgr, hflag] + exact hD + refine ⟨ha, σ', hspec, E', H', S', ?_⟩ + have hzz2 : SSzA σ σ2 := hz2 + have hz1 : SSzA σ σ1 := hzz2.trans (F.unblockSz _ _ _ hA) + have w01 : WritesIn st s s2 (RecvPushFpA st Z m R Bf σ s t e f) := + hfp.mono fun b h => h.elim (fun h => Or.inl (Or.inl h)) fun h => Or.inl (Or.inr (Or.inr h)) + have w12 : WritesIn st s2 s1 (RecvPushFpA st Z m R Bf σ s t e f) := + w1.mono fun _ h => Or.inl (Or.inr (Or.inr (F.back hzz2 hfp.1 h))) + have ks1 : L.ksb s1 = L.ksb s := w1.1.trans hfp.1 + have w23 : WritesIn st s1 out.1 (RecvPushFpA st Z m R Bf σ s t e f) := w2.mono fun b h => by + rcases h with h | h | h | h + · exact Or.inr (Or.inl h) + · exact Or.inl (Or.inr (Or.inl h)) + · exact Or.inr (Or.inr h) + · exact Or.inl (Or.inr (Or.inr (F.back hz1 ks1 h))) + exact (WritesIn.trans (st := st) (WritesIn.trans (st := st) w01 w12) w23).mono fun _ h => + h.elim (fun h => h.elim id id) id + +end + +end + +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/Stage3RecvTmoSelA.lean b/proofs/Fl4ProofsA64/P3/Stage3RecvTmoSelA.lean new file mode 100644 index 000000000..ac8b3f385 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3RecvTmoSelA.lean @@ -0,0 +1,82 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2c): **the timeout selector of a waiting sender is closed by the record**. + +`FaultOKA` of a timeout fault asks `tcbSchedContext = none ∧ mem (scA snd) = 0`. The record already relates the two +(`SchedRelA.tcb`: `tcbSchedContext = optScA m (mem (scA t))`, `optScA m w = none ↔ w = 0`), so a timeout fault of a thread +without an SC is `FaultOKA` and one with an SC is the `tcbSchedContext ≠ none` disjunct of `recvSendA` / `recvPushA`: + +* `JStage3A.faultOK_tmoNoScA`: a timeout fault of a thread of `Z.T` without an SC is `FaultOKA`. +* `JStage3A.tmoSelA`: any timeout fault of a thread of `Z.T` is `FaultOKA` or has an SC. +* `JStage3A.sndFaultSelA`: for a fault `f`, the `hsnd` disjunct of `recvSendA` from `FaultOKA` of the non-timeout faults only. + +Used by `recvSendA`, whose `hsnd` asks `FaultOKA` of the non-timeout faults only. +No new premise: the selector `timeout of a sender without SC needs mem scA = 0` of Executors 10/11 is gone. +[P] none. New module. +-/ +import Fl4ProofsA64.P3.Stage3RecvBlockA +import Fl4ProofsA64.P3.EpFaultXferA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4Spec Fl4SpecA64 + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s : S} {a : A} {σ : State Unit} {m : McState} + +/-- A thread of `Z.T` without an SC has the SC word `0`. -/ +theorem JStage3A.scWord_zeroA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {u : U64} (hu : u ∈ Z.T) {y : Tcb} (hk : σ.kheap (specPtr u) = some (.tcb y)) + (hn : y.tcbSchedContext = none) : L.mem s (scA u) = 0#u64 := by + obtain ⟨y', hy', hys⟩ := J.sr.sch.tcb u hu + rw [hk] at hy' + cases hy' + rw [hn] at hys + by_contra h + unfold optScA at hys + rw [if_neg h] at hys + cases hys + +/-- A timeout fault of a thread without an SC is `FaultOKA`. -/ +theorem JStage3A.faultOK_tmoNoScA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {u : U64} (hu : u ∈ Z.T) {y : Tcb} (hk : σ.kheap (specPtr u) = some (.tcb y)) + (hn : y.tcbSchedContext = none) (rbo : Option ObjRef) (b : Data) : + Endp.FaultOKA (L := L) s u rbo y (.timeout b) := + ⟨hn, J.scWord_zeroA hu hk hn⟩ + +/-- **Every timeout fault is `FaultOKA` or has an SC.** -/ +theorem JStage3A.tmoSelA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {u : U64} (hu : u ∈ Z.T) {y : Tcb} (hk : σ.kheap (specPtr u) = some (.tcb y)) + (rbo : Option ObjRef) {b : Data} (hf : y.tcbFault = some (.timeout b)) : + (∃ f, y.tcbFault = some f ∧ Endp.FaultOKA (L := L) s u rbo y f) ∨ + ∃ b', y.tcbFault = some (.timeout b') ∧ y.tcbSchedContext ≠ none := by + by_cases h : y.tcbSchedContext = none + · exact Or.inl ⟨_, hf, J.faultOK_tmoNoScA hu hk h rbo b⟩ + · exact Or.inr ⟨b, hf, h⟩ + +/-- The sender-fault disjunct of `recvSendA` / `recvPushA` from `FaultOKA` of the non-timeout faults only. -/ +theorem JStage3A.sndFaultSelA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {u : U64} (hu : u ∈ Z.T) {y : Tcb} (hk : σ.kheap (specPtr u) = some (.tcb y)) + (rbo : Option ObjRef) {f : Fault} (hf : y.tcbFault = some f) + (hok : (∀ b, f ≠ .timeout b) → Endp.FaultOKA (L := L) s u rbo y f) : + (∃ f, y.tcbFault = some f ∧ Endp.FaultOKA (L := L) s u rbo y f) ∨ + ∃ b', y.tcbFault = some (.timeout b') ∧ y.tcbSchedContext ≠ none := by + by_cases h : ∃ b, f = .timeout b + · obtain ⟨b, rfl⟩ := h + exact J.tmoSelA hu hk rbo hf + · exact Or.inl ⟨f, hf, hok fun b e => h ⟨b, e⟩⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3RecvUnbArmA.lean b/proofs/Fl4ProofsA64/P3/Stage3RecvUnbArmA.lean new file mode 100644 index 000000000..65c88e3d0 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3RecvUnbArmA.lean @@ -0,0 +1,593 @@ +/- +P3 (aarch64, A64-P3 Stage 3, line e2): **the reply-push arms after `maybe_return_sc`** (E2 items 2 and 3). + +A blocking receive whose bound notification holds the receiver's SC (`BndUnbA`) against a waiting call / faulted sender with a +reply object: `maybe_return_sched_context` runs first (`RecvPreA.maybeReturnScA`), the receiver is then passive, and +`receive_from_sender` runs on the post-state. This module reuses the existing `_core` arms at the post-state: + +* `maybeReturn_noopA`: `maybe_return_sched_context` is the identity when the notification's SC word is null or not the TCB's. +* `receive_ipc_unbA`: `receive_ipc` at the entry state equals `receive_ipc` at the post-state `s1` (the same trick as e5's + cancel: the second `take_reply` is plain, the second `maybe_return_sched_context` a no-op). +* **`RecvPreA.unbCoreA`** (arm-agnostic): given an arm's core at `(s1, UW)`, the core at `(s, σ)`; the arm receives the + post-state record, `BndOKA .. true` at `s1`, the passive receiver, the unchanged heap and the written words. +* **`RecvPreA.recvSendUnbA_core`** / **`recvSendUnbA`**: the send arm (no reply push, any reply argument) at the post-state + (`recvSendA_core`); `unbCoreA` is generic in the reply argument (`ReplyArgA`). +* **`RecvPreA.recvPushUnbA_core`**: the donating push (`recvPushDonA_core`, sender with an SC and no timeout fault, [S] `hV`) + or the timeout push (`recvPushA_core`, the receiver is passive; nothing is donated) after the SC return. + +[S] `BndUnbA`; the sender selectors of `push`; `hV` for a non-timeout sender (the E4 selector, an existing kind). +[P] `ArchTotal`, `ArchPlat`, `hCs`, `hmb`, `hmax`, `htu`. No new named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3RecvUnbRecA +import Fl4ProofsA64.P3.Stage3RecvFrontA +import Fl4ProofsA64.P3.Stage3RecvCancelA +import Fl4ProofsA64.P3.Stage3RecvPushDonA +import Fl4ProofsA64.P3.Stage3RecvSendA +import Fl4ProofsA64.P3.Stage3RecvPushCancelA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4Spec Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {s : S} {a : A} {σ : State Unit} {m : McState} + +/-- **`maybe_return_sched_context` is the identity** when the notification's SC word is null or not the TCB's. -/ +theorem maybeReturn_noopA {s : S} {nn t : U64} + (h : L.mem s (nScA nn) = 0#u64 ∨ L.mem s (nScA nn) ≠ L.mem s (scA t)) : + notification.maybe_return_sched_context st s nn t = ok s := by + have hn : obj.Ntfn.sc st nn s = ok (L.mem s (nScA nn)) := eq_of_spec_fd (Notif.obj.Ntfn.sc.spec s nn) + have ht : obj.Tcb.sc st t s = ok (L.mem s (scA t)) := eq_of_spec_fd (obj.Tcb.sc.spec s t) + unfold notification.maybe_return_sched_context + rw [hn, bind_ok] + rcases h with h0 | hne + · have hb : obj.Sc.is_null (L.mem s (nScA nn)) = ok true := by + have := eq_of_spec_fd (obj.Sc.is_null.spec (L.mem s (nScA nn))) + simpa [h0] using this + rw [hb, bind_ok] + rfl + · by_cases h0 : L.mem s (nScA nn) = 0#u64 + · have hb : obj.Sc.is_null (L.mem s (nScA nn)) = ok true := by + have := eq_of_spec_fd (obj.Sc.is_null.spec (L.mem s (nScA nn))) + simpa [h0] using this + rw [hb, bind_ok] + rfl + · have hb : obj.Sc.is_null (L.mem s (nScA nn)) = ok false := by + have := eq_of_spec_fd (obj.Sc.is_null.spec (L.mem s (nScA nn))) + simpa [h0] using this + rw [hb, bind_ok] + simp only [Bool.false_eq_true, ↓reduceIte] + rw [ht, bind_ok, if_neg hne] + +/-- A `ReplyArgA` reply argument's reply object is unlinked. -/ +theorem ReplyArgA.unlA {R : Set U64} {m : U64 → U64} {rc : capability.Cap} {rc' : Cap} {rv : U64} + (h : ReplyArgA R m rc rc' rv) : ∀ b, rc = .Reply rv b → m (Endp.rTcbA rv) = 0#u64 := fun b hb => by + rcases h with ⟨h0, -⟩ | ⟨b', r', h', -, -, h0⟩ + · rw [h0] at hb; cases hb + · rw [h'] at hb; exact h0 + +/-- The spec's reply argument of a `ReplyArgA` is plain (unlinked). -/ +theorem plainOfArgA {T E : Set U64} {Bf : Set (U64 × Word)} {R : Set U64} {s : S} {σ : State Unit} {t : ObjRef} + (hP : EPlaced T E R Bf) (hR : EpStateRel T E R Bf (L.mem s) σ) {rc : capability.Cap} {rc' : Cap} {rv : U64} + (h : ReplyArgA R (L.mem s) rc rc' rv) : SpecReplyPlainA σ t rc' (optRef rv) := by + obtain ⟨hsr, -⟩ := replyArg_specA hP hR h + rcases hsr with h | ⟨r1, rr, xr, h1, h2, h3, h4⟩ + · exact Or.inl h + · exact Or.inr ⟨r1, rr, xr, h1, h2, h3, Or.inl h4⟩ + +/-- **`receive_ipc` at the entry state is `receive_ipc` at the state after the SC return** (module docstring). -/ +theorem receive_ipc_unbA {s s1 : S} (a : A) {t e cb nn rr : U64} {c1 c2 c3 c4 : Bool} {rc : capability.Cap} {rc' : Cap} + (hrc : ReplyCapA Rp rc rc' rr) (hfree : ∀ b, rc = .Reply rr b → L.mem s (Endp.rTcbA rr) = 0#u64) + (hfree1 : ∀ b, rc = .Reply rr b → L.mem s1 (Endp.rTcbA rr) = 0#u64) (hnz : nn ≠ 0#u64) + (hb : L.mem s (tBoundA t) = nn) (hb1 : L.mem s1 (tBoundA t) = nn) + (hs : stOf (L.mem s) nn ≠ obj.NTFN_ACTIVE) (hs1 : stOf (L.mem s1) nn ≠ obj.NTFN_ACTIVE) + (ew : notification.maybe_return_sched_context st s nn t = ok s1) + (hno : notification.maybe_return_sched_context st s1 nn t = ok s1) : + endpoint.receive_ipc st ar s a t (.Endpoint e cb c1 c2 c3 c4) true rc = + endpoint.receive_ipc st ar s1 a t (.Endpoint e cb c1 c2 c3 c4) true rc := by + obtain ⟨q0, ht0, e0⟩ := spec_existsA (take_reply_plainA (st := st) (ar := ar) (s := s) (a := a) (t := t) (R := Rp) + hrc (fun b h => Or.inl (hfree b h))) + obtain ⟨q1, ht1, e1⟩ := spec_existsA (take_reply_plainA (st := st) (ar := ar) (s := s1) (a := a) (t := t) (R := Rp) + hrc (fun b h => Or.inl (hfree1 b h))) + subst e0 + subst e1 + rw [receive_ipc_eqA, receive_ipc_eqA, ht0, ht1, bind_ok, bind_ok] + show rcvBodyA st ar s a t e true rr = rcvBodyA st ar s1 a t e true rr + rw [rcvReturningA s a t e nn rr hnz hb hs, rcvReturningA s1 a t e nn rr hnz hb1 hs1, ew, hno] + +set_option maxHeartbeats 8000000 in +/-- **The receive core across `maybe_return_sc`** (arm-agnostic, module docstring): given the arm's core at the state after +the SC return, the core at the entry state. -/ +theorem RecvPreA.unbCoreA + (hPl : ArchPlat ar) + (J : RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hA : P0.ArchTotal ar) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {slot : U64} (hslot : slot ∈ OS) + {rv p badge : U64} {send grant grantReply : Bool} {rc : capability.Cap} + (hra : ReplyArgA Rp (L.mem s) rc (absCapA rc) rv) (hrcA : ReplyCapA Rp rc (absCapA rc) rv) + (hbn : ∀ y, σ.kheap σ.curThread = some (.tcb y) → BndUnbA N σ y) + {x : Endpoint} (hk : σ.kheap (specPtr p) = some (.endpoint x)) + {Out1 : State Unit → State Unit → Prop} + (arm : ∀ (s1 : S) (UW : State Unit) (Q : U64 → U64 → List U64) + (J1 : RecvPreA st AR { Z with Q := Q } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s1 a UW m), + L.curW s1 = L.curW s → OSKeep st OS s s1 → ReplyArgA Rp (L.mem s1) rc (absCapA rc) rv → + WritesIn st s s1 (IpcFpA st Z E Rp Bf m σ s) → + (∀ q ob, σ.kheap q = some ob → q ≠ specPtr (L.curW s) → (∀ xs ns, ob ≠ .schedContext xs ns) → + UW.kheap q = some ob) → + (∀ y, UW.kheap UW.curThread = some (.tcb y) → BndOKA N (L.mem s1) UW (L.curW s1) y true) → + (∀ y, UW.kheap UW.curThread = some (.tcb y) → y.tcbSchedContext = none) → + UW.curThread = σ.curThread → + (∀ xt', σ.kheap σ.curThread = some (.tcb xt') → + UW.kheap UW.curThread = some (.tcb { xt' with tcbSchedContext := none })) → + RecvCoreA st AR ar hPl.ipa n Z.Cs s1 a m (.Endpoint p badge send true grant grantReply) true rc + (absCapA (.Endpoint p badge send true grant grantReply)) (absCapA rc) UW + (Out1 UW)) : + RecvCoreA st AR ar hPl.ipa n Z.Cs s a m (.Endpoint p badge send true grant grantReply) true rc + (absCapA (.Endpoint p badge send true grant grantReply)) (absCapA rc) σ + (fun σ' => ∃ UW, (∃ nn, maybeReturnSc (Z := Unit) (specPtr nn) (specPtr (L.curW s)) σ = + ({((), UW)}, False)) ∧ Out1 UW σ') := by + obtain ⟨nn, xt, sc, xs, ns, s1, Q, hnn, hnw, hkc, hst0, hks0, hbd, ⟨xn, hkn, hna⟩, ew, eU, F, wI, hcw, J1⟩ := + J.maybeReturnScA hA hPl hCs hmb hmax htu hslot hbn + have htZ : L.curW s ∈ Z.T := J.sr.curT + set t := L.curW s with ht_def + have ct : σ.curThread = specPtr t := J.j.1.sch.curThread + have hkt : σ.kheap (specPtr t) = some (.tcb xt) := by rw [← ct]; exact hkc + have hNT : sc ≠ specPtr t := fun e => by rw [e, hkt] at hks0; cases hks0 + have P := preschedA_keepA σ + set UW : State Unit := unbWaitA (preschedA σ) (specPtr t) sc xs ns { xt with tcbSchedContext := none } with hUW + have hkt1 : UW.kheap (specPtr t) = some (.tcb { xt with tcbSchedContext := none }) := unbWait_atTA hNT + have hkeep : ∀ q ob, σ.kheap q = some ob → q ≠ specPtr t → (∀ xs' ns', ob ≠ .schedContext xs' ns') → + UW.kheap q = some ob := fun q ob h hq hne => by + have h2 : q ≠ sc := fun e => by + rw [e, hks0] at h + exact hne xs ns (Option.some.inj h.symm) + rw [unbWait_keepA hNT hq h2, congrFun P.kh q]; exact h + have hkn1 : UW.kheap (specPtr nn) = some (.notification xn) := + hkeep _ _ hkn (fun e => by rw [e, hkt] at hkn; cases hkn) (fun _ _ e => by cases e) + have hcm1 : m.curCore < fl4A64Platform.numNodes := by rw [J1.j.1.sch.core]; exact hCs _ J1.j.1.sch.cpuCs + have hbf : BndOKA N (L.mem s1) UW t { xt with tcbSchedContext := none } false := + Or.inr ⟨nn, hnn, hbd, fun xn' hk' => by + rw [hkn1] at hk' + obtain rfl : xn' = xn := (KernelObject.notification.inj (Option.some.inj hk')).symm + exact ⟨hna, fun h => by cases h⟩⟩ + have hb1 : BndOKA N (L.mem s1) UW t { xt with tcbSchedContext := none } true := by + rcases J1.toJStage3A.bndOK_blockA hcm1 htZ hkt1 hbf with h | ⟨n3, hn3, xn3, sc3, hb3, hk3, ha3, hs3, hst3⟩ + · exact h + · exact absurd hst3 (by simp) + have hword : L.mem s1 (nScA nn) = 0#u64 ∨ L.mem s1 (nScA nn) ≠ L.mem s1 (scA t) := by + rcases hb1 with h | ⟨n3, hn3, hb3, hx3⟩ + · exact absurd (hbd.symm.trans h) (by simp) + · obtain rfl : n3 = nn := specPtr_inj (Option.some.inj (hb3.symm.trans hbd)) + exact ((hx3 xn hkn1).2 rfl).2 + have hno := maybeReturn_noopA (st := st) (s := s1) (nn := nn) (t := t) hword + have bw0 := bound_wordA (J.ep.at_tcb htZ hkt) hbd + have bw1 := bound_wordA (J1.ep.at_tcb htZ hkt1) hbd + have hs0 : stOf (L.mem s) nn ≠ obj.NTFN_ACTIVE := + stOf_not_activeA (J.ntfn.at_ntfn hnn hkn).obj (fun b e => by simp [isActive, e] at hna) + have hs1 : stOf (L.mem s1) nn ≠ obj.NTFN_ACTIVE := + stOf_not_activeA (J1.ntfn.at_ntfn hnn hkn1).obj (fun b e => by simp [isActive, e] at hna) + -- the reply object stays unlinked + have hra1 : ReplyArgA Rp (L.mem s1) rc (absCapA rc) rv := by + rcases hra with h | ⟨b, r', hrc, hrc', hrr, hfree⟩ + · exact Or.inl h + · refine Or.inr ⟨b, r', hrc, hrc', hrr, ?_⟩ + obtain ⟨xr0, hkr0, hxr0⟩ := J.ep.reply rv hrr + have hnone : xr0.replyTcb = none := by rw [hxr0, hfree]; exact optRef_zero + have hkr1 : UW.kheap (specPtr rv) = some (.reply xr0) := hkeep _ _ hkr0 + (fun e => by rw [e, hkt] at hkr0; cases hkr0) (fun _ _ e => by cases e) + obtain ⟨xr1, hkr1', hxr1⟩ := J1.ep.reply rv hrr + rw [hkr1] at hkr1' + obtain rfl := KernelObject.reply.inj (Option.some.inj hkr1') + by_contra hne + rw [optRef_ne hne, hnone] at hxr1 + cases hxr1 + have hcur1 : UW.curThread = specPtr t := P.cur.trans ct + have osk : OSKeep st OS s s1 := J.toJStage3A.osKeep_stepNA (F.wr.mono fun _ h => Or.inl (Or.inl h)) + have hpas1 : ∀ y, UW.kheap UW.curThread = some (.tcb y) → y.tcbSchedContext = none := by + intro y hy + rw [hcur1, hkt1] at hy + obtain rfl := KernelObject.tcb.inj (Option.some.inj hy) + rfl + have hb1' : ∀ y, UW.kheap UW.curThread = some (.tcb y) → BndOKA N (L.mem s1) UW (L.curW s1) y true := by + intro y hy + rw [hcur1, hkt1] at hy + obtain rfl := KernelObject.tcb.inj (Option.some.inj hy) + rw [hcw]; exact hb1 + obtain ⟨s2, σ', hipc, hrecv, hout, hrel, hcur, hbody⟩ := + arm s1 UW Q J1 hcw osk hra1 wI hkeep hb1' hpas1 (hcur1.trans ct.symm) (fun xt' h => by + rw [hkc] at h + obtain rfl := KernelObject.tcb.inj (Option.some.inj h) + rw [hcur1]; exact hkt1) + have hFL : endpoint.receive_ipc st ar s a t (.Endpoint p badge send true grant grantReply) true rc = + endpoint.receive_ipc st ar s1 a t (.Endpoint p badge send true grant grantReply) true rc := + receive_ipc_unbA a hrcA (ReplyArgA.unlA hra) (ReplyArgA.unlA hra1) bw0.1 bw0.2 bw1.2 hs0 hs1 ew hno + rw [hcw] at hipc + have hk1 : UW.kheap (specPtr p) = some (.endpoint x) := hkeep _ _ hk + (fun e => by rw [e, hkt] at hk; cases hk) (fun _ _ e => by cases e) + have hs1' : receiveIpc (Z := Unit) (specPtr t) (absCapA (.Endpoint p badge send true grant grantReply)) true + (absCapA rc) σ = + receiveIpc (Z := Unit) (specPtr t) (absCapA (.Endpoint p badge send true grant grantReply)) true + (absCapA rc) UW := by + rw [show absCapA (.Endpoint p badge send true grant grantReply) = + .endpointCap (specPtr p) badge.bv (rightsOfA send true grant grantReply) from rfl, + receiveIpc_bodyA _ _ true (plainOfArgA J.iplaced.e J.ep hra), + receiveIpc_bodyA _ _ true (plainOfArgA J1.iplaced.e J1.ep hra1), + specBodyReturningA' hkt hk hkn hbd hna eU, bndOK_specA hkt1 hk1 J1.ntfn hb1 (optRef rv)] + refine ⟨s2, σ', ?_, ?_, ⟨UW, ⟨nn, eU⟩, hout⟩, hrel, hcur.trans hcw, hbody⟩ + · rw [hFL]; exact hipc + · rw [hs1']; rw [hcw] at hrecv; exact hrecv + +/-- `FaultOKA` of a non-timeout fault across the SC return: only the sender's flags word is read. -/ +theorem FaultOKA.unbA {s s1 : S} {sd : U64} {rbo : Option ObjRef} {y : Tcb} {f : Fault} + (hf : ∀ b, f ≠ .timeout b) (h1 : L.mem s1 (tTflA sd) = L.mem s (tTflA sd)) + (h : Endp.FaultOKA (L := L) s sd rbo y f) : Endp.FaultOKA (L := L) s1 sd rbo y f := by + cases f with + | timeout b => exact absurd rfl (hf b) + | unknownSyscallException n => unfold Endp.FaultOKA at h ⊢; rw [h1]; exact h + | archFault af => cases af <;> first | exact h | (rename_i o; cases o <;> exact h) + | _ => exact h + +set_option maxHeartbeats 8000000 in +/-- **The reply-push arms after `maybe_return_sc`** (module docstring): a waiting call / faulted sender with a reply +object against a `BndUnbA` receiver. A timeout sender is the timeout push (`recvPushA_core` at the post-state); any other +sender donates its SC (`recvPushDonA_core`, [S] `hV`). -/ +theorem RecvPreA.recvPushUnbA_core + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p badge slot rr rslot : U64} {send grant grantReply rg : Bool} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) + (hsc : capA st s slot = .Endpoint p badge send true grant grantReply) + (hrslot : rslot ∈ OS) (hrsc : capA st s rslot = .Reply rr rg) (hrr : rr ∈ Rp) + (hfree : L.mem s (Endp.rTcbA rr) = 0#u64) + (hp : p ∈ E) {sd : U64} {q : List ObjRef} + (hk : σ.kheap (specPtr p) = some (.endpoint (.sendEP (specPtr sd :: q)))) + (hbn : ∀ y, σ.kheap σ.curThread = some (.tcb y) → BndUnbA N σ y) + (hsnd : ∀ y xt, σ.kheap (specPtr sd) = some (.tcb y) → σ.kheap σ.curThread = some (.tcb xt) → + y.tcbSchedContext ≠ none ∧ ∀ e' dd, + y.tcbState = .blockedOnSend e' dd → (dd.senderIsCall = true ∨ y.tcbFault ≠ none) ∧ + (dd.senderCanGrant = true ∨ dd.senderCanGrantReply = true)) + (hsndF : ∀ y xt, σ.kheap (specPtr sd) = some (.tcb y) → σ.kheap σ.curThread = some (.tcb xt) → + (y.tcbFault = none ∧ ∀ e' dd, y.tcbState = .blockedOnSend e' dd → dd.senderCanGrant = false ∨ + ipcBufOfA false y = none ∨ + unat (dataToMessageInfo (y.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0) ∨ + (∃ f, y.tcbFault = some f ∧ Endp.FaultOKA (L := L) s sd (ipcBufOfA true xt) y f) ∨ + ∃ b, y.tcbFault = some (.timeout b)) + (hV : L.mem s (core.num.U64.wrapping_add sd 3664#u64) = 0#u64) : + RecvCoreA st AR ar hPl.ipa n Z.Cs s a m (.Endpoint p badge send true grant grantReply) true (.Reply rr rg) + (absCapA (.Endpoint p badge send true grant grantReply)) (.replyCap (specPtr rr) (replyRightsA rg)) σ + (fun σ' => ∃ UW, (∃ nn, maybeReturnSc (Z := Unit) (specPtr nn) (specPtr (L.curW s)) σ = + ({((), UW)}, False)) ∧ + ((), σ') ∈ (specRecvSendA UW.curThread (specPtr p) (some (specPtr rr)) (specPtr sd :: q) UW).1) := by + have htZ : L.curW s ∈ Z.T := J.sr.curT + set t := L.curW s with ht_def + have ct : σ.curThread = specPtr t := J.j.1.sch.curThread + obtain ⟨xt, hkt, r⟩ := J.ep.tcb t htZ + have hkc : σ.kheap σ.curThread = some (.tcb xt) := by rw [ct]; exact hkt + have hrunS : xt.tcbState = .running ∨ xt.tcbState = .restart := J.state hkt + obtain ⟨hst1, xs0, -, hq0, hQ, hB⟩ := J.ep.at_ep hp hk + have hsdx : sd ∈ xs0 := by + have : specPtr sd ∈ xs0.map specPtr := by rw [← hq0]; exact List.mem_cons_self + obtain ⟨y, hy, e⟩ := List.mem_map.1 this + rwa [specPtr_inj e] at hy + have hsdT : sd ∈ Z.T := hQ.mem sd hsdx + have hs4 : (L.mem s (stA sd)).val = 4 := (hB sd hsdx).1 + obtain ⟨xs, hks, rs⟩ := J.ep.tcb sd hsdT + obtain ⟨e', dd, hsst⟩ := tsCode_four (rs.st.symm.trans hs4) + have hts : specPtr t ≠ specPtr sd := fun h => by + rw [h, hks] at hkt + cases hkt + rcases hrunS with e | e <;> rw [e] at hsst <;> cases hsst + refine RecvPreA.unbCoreA (Out1 := fun UW σ' => + ((), σ') ∈ (specRecvSendA UW.curThread (specPtr p) (some (specPtr rr)) (specPtr sd :: q) UW).1) + hPl J hA hCs hmb hmax htu hslot (Or.inr ⟨rg, _, rfl, rfl, hrr, hfree⟩) (Or.inr ⟨rg, _, rfl, rfl, hrr⟩) hbn hk ?_ + intro s1 UW Q J1 hcw osk hra1 wI hkeep hb1' hpas1 hcurUW hkt1 + have hkt1' := hkt1 xt hkc + have hfree1 : L.mem s1 (Endp.rTcbA rr) = 0#u64 := ReplyArgA.unlA hra1 rg rfl + have hks1 : UW.kheap (specPtr sd) = some (.tcb xs) := hkeep _ _ hks (fun e => hts e.symm) (fun _ _ e => by cases e) + have hk1 : UW.kheap (specPtr p) = some (.endpoint (.sendEP (specPtr sd :: q))) := hkeep _ _ hk + (fun e => by rw [e, hkt] at hk; cases hk) (fun _ _ e => by cases e) + have hcov1 := SlotCov.keepA osk hcov + have hsc1 : capA st s1 slot = .Endpoint p badge send true grant grantReply := (osk.capA hslot).trans hsc + have hrsc1 : capA st s1 rslot = .Reply rr rg := (osk.capA hrslot).trans hrsc + have hfl : L.mem s1 (tTflA sd) = L.mem s (tTflA sd) := + wI.2.2 _ (field_aligned (J.iplaced.e.tcb sd hsdT) (by simp) (by simp)) + (J.toJStage3A.tcbWord_offIpcA hsdT (K := 3456#u64) (by simp) (by unfold TWOffN; simp) (by simp)) + have hxt : ∀ xt', UW.kheap UW.curThread = some (.tcb xt') → xt' = { xt with tcbSchedContext := none } := by + intro xt' h + rw [hkt1'] at h + exact (KernelObject.tcb.inj (Option.some.inj h)).symm + have hys : ∀ y, UW.kheap (specPtr sd) = some (.tcb y) → y = xs := fun y h => by + rw [hks1] at h; exact (KernelObject.tcb.inj (Option.some.inj h)).symm + have hbuf : ipcBufOfA true { xt with tcbSchedContext := none } = ipcBufOfA true xt := rfl + by_cases tmo : xs.tcbFault.elim false isTimeoutFault = true + · -- the timeout push, the receiver passive + obtain ⟨b, hb⟩ : ∃ b, xs.tcbFault = some (.timeout b) := by + rcases hf : xs.tcbFault with _ | f + · simp [hf] at tmo + · cases f <;> simp [isTimeoutFault, hf] at tmo ⊢ + have core := RecvPreA.recvPushA_core hA hPl J1 hCs h1 hmb hmax htu hslot hcov1 hsc1 hrslot hrsc1 hrr hfree1 hp hk1 + hb1' + (fun y xt' hy hx => by + rw [hys y hy] + exact hsnd xs xt hks hkc) + (fun y xt' hy hx => by + rw [hys y hy] + exact Or.inr (Or.inr ⟨b, hb⟩)) + (fun x y hx hy hn => by rw [hys y hy] at hn; exact absurd tmo hn) + exact core + · -- the donating push + have core := RecvPreA.recvPushDonA_core hA hPl J1 hCs h1 hmb hmax htu hslot hcov1 hsc1 hrslot hrsc1 hrr hfree1 hp hk1 + hb1' + (fun y xt' hy hx => by + rw [hys y hy] + exact hsnd xs xt hks hkc) + (fun y xt' hy hx => by + rw [hys y hy] + rw [hxt xt' hx] + rcases hsndF xs xt hks hkc with g | ⟨f, hf, hok⟩ | ⟨b, hb⟩ + · exact Or.inl g + · refine Or.inr ⟨f, hf, ?_⟩ + rw [hbuf] + exact FaultOKA.unbA (fun b e => tmo (show xs.tcbFault.elim false isTimeoutFault = true by rw [hf, e]; rfl)) hfl hok + · exact absurd (show xs.tcbFault.elim false isTimeoutFault = true by rw [hb]; rfl) tmo) + hpas1 + (by + have h2 : L.mem s1 (core.num.U64.wrapping_add sd 3664#u64) = L.mem s (core.num.U64.wrapping_add sd 3664#u64) := + wI.2.2 _ (field_aligned (J.iplaced.e.tcb sd hsdT) (by simp) (by simp)) + (J.toJStage3A.tcbWord_offIpcA hsdT (K := 3664#u64) (by simp) (by unfold TWOffN; simp) (by simp)) + rw [h2]; exact hV) + exact core + +set_option maxHeartbeats 8000000 in +/-- **`handle_recv` (blocking, with a reply object) by a `BndUnbA` receiver against a waiting call / faulted sender** +(module docstring): `recvPushUnbA_core` behind the two capability lookups; the reply object may be linked to any thread +(its cancel first: `RecvPreA.cancelCoreXA`). -/ +theorem RecvPreA.recvPushUnbA + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p badge slot rr rslot : U64} {send grant grantReply rg : Bool} + (hcap : cspace.lookup_cap st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.CAP)) = + ok (.Ok (.Endpoint p badge send true grant grantReply))) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) + (hsc : capA st s slot = .Endpoint p badge send true grant grantReply) + (hcapR : cspace.lookup_cap st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.REPLY)) = ok (.Ok (.Reply rr rg))) + (hrslot : rslot ∈ OS) (hrsc : capA st s rslot = .Reply rr rg) (hrr : rr ∈ Rp) + (hp : p ∈ E) {sd : U64} {q : List ObjRef} + (hk : σ.kheap (specPtr p) = some (.endpoint (.sendEP (specPtr sd :: q)))) + (hbn : ∀ y, σ.kheap σ.curThread = some (.tcb y) → BndUnbA N σ y) + (hsnd : ∀ y xt, σ.kheap (specPtr sd) = some (.tcb y) → σ.kheap σ.curThread = some (.tcb xt) → + y.tcbSchedContext ≠ none ∧ ∀ e' dd, + y.tcbState = .blockedOnSend e' dd → (dd.senderIsCall = true ∨ y.tcbFault ≠ none) ∧ + (dd.senderCanGrant = true ∨ dd.senderCanGrantReply = true)) + (hsndF : ∀ y xt, σ.kheap (specPtr sd) = some (.tcb y) → σ.kheap σ.curThread = some (.tcb xt) → + (y.tcbFault = none ∧ ∀ e' dd, y.tcbState = .blockedOnSend e' dd → dd.senderCanGrant = false ∨ + ipcBufOfA false y = none ∨ + unat (dataToMessageInfo (y.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0) ∨ + (∃ f, y.tcbFault = some f ∧ Endp.FaultOKA (L := L) s sd (ipcBufOfA true xt) y f) ∨ + ∃ b, y.tcbFault = some (.timeout b)) + (hV : L.mem s (core.num.U64.wrapping_add sd 3664#u64) = 0#u64) : + HRecvPostA st AR ar hPl.ipa n Z.Cs s a m true true σ + (fun σ' => (∃ UW, (∃ nn, maybeReturnSc (Z := Unit) (specPtr nn) (specPtr (L.curW s)) σ = + ({((), UW)}, False)) ∧ + ((), σ') ∈ (specRecvSendA UW.curThread (specPtr p) (some (specPtr rr)) (specPtr sd :: q) UW).1) ∨ + ∃ i σ1, cancelIpc (Z := Unit) (specPtr i) σ = ({((), σ1)}, False) ∧ + ∃ UW, (∃ nn, maybeReturnSc (Z := Unit) (specPtr nn) (specPtr (L.curW s)) σ1 = ({((), UW)}, False)) ∧ + ((), σ') ∈ (specRecvSendA UW.curThread (specPtr p) (some (specPtr rr)) (specPtr sd :: q) UW).1) := by + by_cases h0 : L.mem s (Endp.rTcbA rr) = 0#u64 + · exact (RecvPreA.recvFrontA J hcap hcapR + (RecvPreA.recvPushUnbA_core hA hPl J hCs h1 hmb hmax htu hslot hcov hsc hrslot hrsc hrr h0 hp hk hbn hsnd hsndF + hV)).monoA (fun _ h => Or.inl h) + · have ct : σ.curThread = specPtr (L.curW s) := J.j.1.sch.curThread + have hcT : L.curW s ∈ Z.T := J.sr.curT + obtain ⟨xc, hk0, -⟩ := J.ep.tcb (L.curW s) hcT + have hkc : σ.kheap σ.curThread = some (.tcb xc) := by rw [ct]; exact hk0 + have core := RecvPreA.cancelCoreXA J hA hPl rfl hCs hmb hmax htu (p := p) (badge := badge) (send := send) + (grant := grant) (grantReply := grantReply) (blk := true) (rg := rg) hrr h0 + (Out1 := fun σ1 σ' => ∃ UW, (∃ nn, maybeReturnSc (Z := Unit) (specPtr nn) (specPtr (L.curW s)) σ1 = + ({((), UW)}, False)) ∧ + ((), σ') ∈ (specRecvSendA UW.curThread (specPtr p) (some (specPtr rr)) (specPtr sd :: q) UW).1) + (fun i hi hi0 hit hiT hi4 s1 σ1 stk Q Rl J1 hcw hcan X => ?_) + · refine (RecvPreA.recvFrontA J hcap hcapR core).monoA (fun σ' h => Or.inr ?_) + obtain ⟨i, σ1, hcan, hh⟩ := h + exact ⟨i, σ1, hcan, hh⟩ + · obtain ⟨-, xs0, -, hq0, hQ, hB⟩ := J.ep.at_ep hp hk + have hsdx : sd ∈ xs0 := by + have : specPtr sd ∈ xs0.map specPtr := by rw [← hq0]; exact List.mem_cons_self + obtain ⟨y, hy, e⟩ := List.mem_map.1 this + rwa [specPtr_inj e] at hy + have hsdT : sd ∈ Z.T := hQ.mem sd hsdx + have hne_sd : specPtr sd ≠ specPtr i := fun e => hi4 (by rw [← specPtr_inj e]; exact (hB sd hsdx).1) + obtain ⟨xs, hks, -⟩ := J.ep.tcb sd hsdT + have hk1 : σ1.kheap (specPtr p) = σ.kheap (specPtr p) := X.epS p hp _ hk + have hsd1 : σ1.kheap (specPtr sd) = σ.kheap (specPtr sd) := by rw [X.tcb _ xs hne_sd hks, hks] + have hct1 : σ1.curThread = σ.curThread := by + have h := congrArg (fun τ : State Unit => τ.curThread) X.fr + exact h + have hcur1 : σ1.kheap σ.curThread = σ.kheap σ.curThread := by + rw [X.tcb _ xc (fun e => hit (specPtr_inj (ct.symm.trans e)).symm) hkc, hkc] + have hcov1 := SlotCov.keepA X.osk hcov + have hsc1 : capA st s1 slot = .Endpoint p badge send true grant grantReply := by + rw [X.osk.capA hslot]; exact hsc + have hrsc1 : capA st s1 rslot = .Reply rr rg := by rw [X.osk.capA hrslot]; exact hrsc + have hbn1 : ∀ y, σ1.kheap σ1.curThread = some (.tcb y) → BndUnbA N σ1 y := by + intro y hy + rw [hct1, hcur1, hkc] at hy + obtain rfl := KernelObject.tcb.inj (Option.some.inj hy.symm) + obtain ⟨nn, hn, xn, sc, hb, hkn, hia, hsc', hts⟩ := hbn y hkc + exact ⟨nn, hn, xn, sc, hb, by rw [X.ntfn nn hn]; exact hkn, hia, hsc', hts⟩ + have hsnd1 : ∀ y xt, σ1.kheap (specPtr sd) = some (.tcb y) → σ1.kheap σ1.curThread = some (.tcb xt) → + y.tcbSchedContext ≠ none ∧ ∀ e' dd, + y.tcbState = .blockedOnSend e' dd → (dd.senderIsCall = true ∨ y.tcbFault ≠ none) ∧ + (dd.senderCanGrant = true ∨ dd.senderCanGrantReply = true) := by + intro y xt hy hxt + rw [hsd1] at hy + rw [hct1, hcur1] at hxt + exact hsnd y xt hy hxt + have e1 : L.mem s1 (tTflA sd) = L.mem s (tTflA sd) := + X.tw sd hsdT 3456#u64 (by simp) (by simp) (by simp) (by simp) (by simp) (by simp) + have e2 : L.mem s1 (scA sd) = L.mem s (scA sd) := by + rw [scA_eq] + exact X.tw sd hsdT 3464#u64 (by simp) (by simp) (by simp) (by simp) (by simp) (by simp) + have hsndF1 : ∀ y xt, σ1.kheap (specPtr sd) = some (.tcb y) → σ1.kheap σ1.curThread = some (.tcb xt) → + (y.tcbFault = none ∧ ∀ e' dd, y.tcbState = .blockedOnSend e' dd → dd.senderCanGrant = false ∨ + ipcBufOfA false y = none ∨ + unat (dataToMessageInfo (y.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0) ∨ + (∃ f, y.tcbFault = some f ∧ Endp.FaultOKA (L := L) s1 sd (ipcBufOfA true xt) y f) ∨ + ∃ b, y.tcbFault = some (.timeout b) := by + intro y xt hy hxt + rw [hsd1] at hy + rw [hct1, hcur1] at hxt + rcases hsndF y xt hy hxt with g | ⟨f, hf, hok⟩ | b + · exact Or.inl g + · exact Or.inr (Or.inl ⟨f, hf, Endp.FaultOKA.xport e1 e2 hok⟩) + · exact Or.inr (Or.inr b) + have hV1 : L.mem s1 (core.num.U64.wrapping_add sd 3664#u64) = 0#u64 := by + rw [X.tw sd hsdT 3664#u64 (by simp) (by simp) (by simp) (by simp) (by simp) (by simp)] + exact hV + have core1 := RecvPreA.recvPushUnbA_core hA hPl J1 hCs h1 hmb hmax htu hslot hcov1 hsc1 hrslot hrsc1 hrr X.unl hp + (by rw [hk1]; exact hk) hbn1 hsnd1 hsndF1 hV1 + rw [hcw] at core1 + exact core1 + +set_option maxHeartbeats 8000000 in +/-- **The send arm after `maybe_return_sc`** (module docstring): a waiting sender, no reply push, against a `BndUnbA` +receiver, any reply argument: `recvSendA_core` at the post-state. -/ +theorem RecvPreA.recvSendUnbA_core + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p badge slot rv : U64} {send grant grantReply : Bool} {rc : capability.Cap} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) + (hsc : capA st s slot = .Endpoint p badge send true grant grantReply) + (hra : ReplyArgA Rp (L.mem s) rc (absCapA rc) rv) (hrcA : ReplyCapA Rp rc (absCapA rc) rv) + (hrv : (rc = .Null ∧ rv = 0#u64) ∨ ∃ g, rc = .Reply rv g) + (hrcap : ∀ r g, rc = .Reply r g → ∃ rslot ∈ OS, capA st s rslot = .Reply r g) + (hp : p ∈ E) {sd : U64} {q : List ObjRef} + (hk : σ.kheap (specPtr p) = some (.endpoint (.sendEP (specPtr sd :: q)))) + (hbn : ∀ y, σ.kheap σ.curThread = some (.tcb y) → BndUnbA N σ y) + (hsnd : ∀ y xt, σ.kheap (specPtr sd) = some (.tcb y) → σ.kheap σ.curThread = some (.tcb xt) → + (y.tcbFault = none ∧ ∀ e' dd, y.tcbState = .blockedOnSend e' dd → dd.senderCanGrant = false ∨ + ipcBufOfA false y = none ∨ + unat (dataToMessageInfo (y.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0) ∨ + ∃ f, y.tcbFault = some f ∧ ((∀ b, f ≠ .timeout b) → Endp.FaultOKA (L := L) s sd (ipcBufOfA true xt) y f)) + (hng : rv ≠ 0#u64 → ∀ y, σ.kheap (specPtr sd) = some (.tcb y) → ∀ e' dd, y.tcbState = .blockedOnSend e' dd → + (dd.senderIsCall = true ∨ y.tcbFault ≠ none) → dd.senderCanGrant = false ∧ dd.senderCanGrantReply = false) : + RecvCoreA st AR ar hPl.ipa n Z.Cs s a m (.Endpoint p badge send true grant grantReply) true rc + (absCapA (.Endpoint p badge send true grant grantReply)) (absCapA rc) σ + (fun σ' => ∃ UW, (∃ nn, maybeReturnSc (Z := Unit) (specPtr nn) (specPtr (L.curW s)) σ = + ({((), UW)}, False)) ∧ + ((), σ') ∈ (specRecvSendA UW.curThread (specPtr p) (optRef rv) (specPtr sd :: q) UW).1) := by + have htZ : L.curW s ∈ Z.T := J.sr.curT + set t := L.curW s with ht_def + have ct : σ.curThread = specPtr t := J.j.1.sch.curThread + obtain ⟨xt, hkt, r⟩ := J.ep.tcb t htZ + have hkc : σ.kheap σ.curThread = some (.tcb xt) := by rw [ct]; exact hkt + have hrunS : xt.tcbState = .running ∨ xt.tcbState = .restart := J.state hkt + obtain ⟨hst1, xs0, -, hq0, hQ, hB⟩ := J.ep.at_ep hp hk + have hsdx : sd ∈ xs0 := by + have : specPtr sd ∈ xs0.map specPtr := by rw [← hq0]; exact List.mem_cons_self + obtain ⟨y, hy, e⟩ := List.mem_map.1 this + rwa [specPtr_inj e] at hy + have hsdT : sd ∈ Z.T := hQ.mem sd hsdx + have hs4 : (L.mem s (stA sd)).val = 4 := (hB sd hsdx).1 + obtain ⟨xs, hks, rs⟩ := J.ep.tcb sd hsdT + obtain ⟨e', dd, hsst⟩ := tsCode_four (rs.st.symm.trans hs4) + have hts : specPtr t ≠ specPtr sd := fun h => by + rw [h, hks] at hkt + cases hkt + rcases hrunS with e | e <;> rw [e] at hsst <;> cases hsst + refine RecvPreA.unbCoreA (Out1 := fun UW σ' => + ((), σ') ∈ (specRecvSendA UW.curThread (specPtr p) (optRef rv) (specPtr sd :: q) UW).1) + hPl J hA hCs hmb hmax htu hslot hra hrcA hbn hk ?_ + intro s1 UW Q J1 hcw osk hra1 wI hkeep hb1' hpas1 hcurUW hkt1 + have hkt1' := hkt1 xt hkc + have hks1 : UW.kheap (specPtr sd) = some (.tcb xs) := hkeep _ _ hks (fun e => hts e.symm) (fun _ _ e => by cases e) + have hk1 : UW.kheap (specPtr p) = some (.endpoint (.sendEP (specPtr sd :: q))) := hkeep _ _ hk + (fun e => by rw [e, hkt] at hk; cases hk) (fun _ _ e => by cases e) + have hfl : L.mem s1 (tTflA sd) = L.mem s (tTflA sd) := + wI.2.2 _ (field_aligned (J.iplaced.e.tcb sd hsdT) (by simp) (by simp)) + (J.toJStage3A.tcbWord_offIpcA hsdT (K := 3456#u64) (by simp) (by unfold TWOffN; simp) (by simp)) + have hxt : ∀ xt', UW.kheap UW.curThread = some (.tcb xt') → xt' = { xt with tcbSchedContext := none } := by + intro xt' h + rw [hkt1'] at h + exact (KernelObject.tcb.inj (Option.some.inj h)).symm + have hys : ∀ y, UW.kheap (specPtr sd) = some (.tcb y) → y = xs := fun y h => by + rw [hks1] at h; exact (KernelObject.tcb.inj (Option.some.inj h)).symm + exact RecvPreA.recvSendA_core hA hPl J1 hCs h1 hmb hmax htu hslot (SlotCov.keepA osk hcov) + ((osk.capA hslot).trans hsc) hra1 hrcA hrv + (fun r' g' c => by + obtain ⟨rs', h1', h2'⟩ := hrcap r' g' c + exact ⟨rs', h1', (osk.capA h1').trans h2'⟩) + hp hk1 hb1' + (fun y xt' hy hx => by + rw [hys y hy, hxt xt' hx] + rcases hsnd xs xt hks hkc with g | ⟨f, hf, hok⟩ + · exact Or.inl g + · exact Or.inr ⟨f, hf, fun hn => FaultOKA.unbA hn hfl (hok hn)⟩) + (fun hne y hy => by rw [hys y hy]; exact hng hne xs hks) + +set_option maxHeartbeats 1000000 in +/-- **`handle_recv` (blocking) by a `BndUnbA` receiver against a waiting sender, no reply push** (module docstring): +`recvSendUnbA_core` behind the capability lookups. -/ +theorem RecvPreA.recvSendUnbA + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p badge slot rv : U64} {send grant grantReply canReply : Bool} {rc : capability.Cap} + (hcap : cspace.lookup_cap st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.CAP)) = + ok (.Ok (.Endpoint p badge send true grant grantReply))) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) + (hsc : capA st s slot = .Endpoint p badge send true grant grantReply) + (hrp : RecvReplyPinA (st := st) s (L.curW s) Rp canReply rc) + (hrv : (rc = .Null ∧ rv = 0#u64) ∨ ∃ g, rc = .Reply rv g) + (hrcap : ∀ r g, rc = .Reply r g → ∃ rslot ∈ OS, capA st s rslot = .Reply r g) + (hp : p ∈ E) {sd : U64} {q : List ObjRef} + (hk : σ.kheap (specPtr p) = some (.endpoint (.sendEP (specPtr sd :: q)))) + (hbn : ∀ y, σ.kheap σ.curThread = some (.tcb y) → BndUnbA N σ y) + (hsnd : ∀ y xt, σ.kheap (specPtr sd) = some (.tcb y) → σ.kheap σ.curThread = some (.tcb xt) → + (y.tcbFault = none ∧ ∀ e' dd, y.tcbState = .blockedOnSend e' dd → dd.senderCanGrant = false ∨ + ipcBufOfA false y = none ∨ + unat (dataToMessageInfo (y.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0) ∨ + ∃ f, y.tcbFault = some f ∧ ((∀ b, f ≠ .timeout b) → Endp.FaultOKA (L := L) s sd (ipcBufOfA true xt) y f)) + (hng : rv ≠ 0#u64 → ∀ y, σ.kheap (specPtr sd) = some (.tcb y) → ∀ e' dd, y.tcbState = .blockedOnSend e' dd → + (dd.senderIsCall = true ∨ y.tcbFault ≠ none) → dd.senderCanGrant = false ∧ dd.senderCanGrantReply = false) : + HRecvPostA st AR ar hPl.ipa n Z.Cs s a m true canReply σ + (fun σ' => ∃ UW, (∃ nn, maybeReturnSc (Z := Unit) (specPtr nn) (specPtr (L.curW s)) σ = + ({((), UW)}, False)) ∧ + ((), σ') ∈ (specRecvSendA UW.curThread (specPtr p) (optRef rv) (specPtr sd :: q) UW).1) := by + obtain ⟨hra, hrcA, -, -⟩ := RecvReplyPinA.data hrp hrv + exact RecvPreA.recvFrontRpA J hcap hrp + (RecvPreA.recvSendUnbA_core hA hPl J hCs h1 hmb hmax htu hslot hcov hsc hra hrcA hrv hrcap hp hk hbn hsnd hng) + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3RecvUnbRecA.lean b/proofs/Fl4ProofsA64/P3/Stage3RecvUnbRecA.lean new file mode 100644 index 000000000..8807d725d --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3RecvUnbRecA.lean @@ -0,0 +1,239 @@ +/- +P3 (aarch64, A64-P3 Stage 3, line e2): **`maybe_return_sc` at the record** (E2 item 1). + +`receive_ipc` of a blocking receiver whose bound notification holds the receiver's SC (`BndUnbA`) first runs +`notification.maybe_return_sched_context` (after the plain `take_reply`). The frozen contract of that step: + +* FL4: `t.sc := 0`, `sc.tcb := 0`, and `reschedule_required` (`schedulerAction := chooseNewThread`; a pending + `switchThread u` is enqueued first). Nothing else is written: no notification field, no ready / release queue. +* Spec: `maybeReturnSc n t σ = unbWaitA (preschedA σ) t sc xs ns xt` (`maybeReturnSc_unbSwA`). +* Post: the receiver is passive, `BndOKA .. true` holds again (not-equal clause and word clause), the endpoint, the reply + objects and every other TCB / SC field are unchanged. + +* **`RecvPreA.maybeReturnScA`**: from `RecvPreA` at `(s, σ)` and `BndUnbA`, the FL4 step `maybe_return_sched_context` + reaches `s1`, the spec reaches `UW`, and `RecvPreA` holds at `(s1, UW)` (fresh queue ghosts `Q`, same `stack`), with the + frame bundle `MFrA` and the written words (`IpcFpA`) for the selector transport. + +[S] `BndUnbA`; `hslot` is any `OS` slot (it feeds `decPreA`'s `CanBlock`). [P] `ArchTotal`, `ArchPlat`, `hCs`, `hmb`, +`hmax`, `htu`. No new named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3RecvBlockUnbA +import Fl4ProofsA64.P3.Stage3RecvPollA +import Fl4ProofsA64.P3.Stage3RecvPushA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4Spec Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {s : S} {a : A} {σ : State Unit} {m : McState} + +set_option maxHeartbeats 8000000 in +/-- **`maybe_return_sched_context` of a `BndUnbA` receiver, at the record** (module docstring). -/ +theorem RecvPreA.maybeReturnScA + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {slot : U64} (hslot : slot ∈ OS) + (hbn : ∀ y, σ.kheap σ.curThread = some (.tcb y) → BndUnbA N σ y) : + ∃ (nn : U64) (xt : Tcb) (sc : ObjRef) (xs : SchedContext) (ns : Nat) (s1 : S) (Q : U64 → U64 → List U64), + nn ∈ N ∧ L.mem s (tBoundA (L.curW s)) = nn ∧ σ.kheap σ.curThread = some (.tcb xt) ∧ + xt.tcbSchedContext = some sc ∧ σ.kheap sc = some (.schedContext xs ns) ∧ + xt.tcbBoundNotification = some (specPtr nn) ∧ + (∃ xn, σ.kheap (specPtr nn) = some (.notification xn) ∧ isActive xn = false) ∧ + notification.maybe_return_sched_context st s nn (L.curW s) = ok s1 ∧ + maybeReturnSc (Z := Unit) (specPtr nn) (specPtr (L.curW s)) σ = + ({((), unbWaitA (preschedA σ) (specPtr (L.curW s)) sc xs ns { xt with tcbSchedContext := none })}, False) ∧ + MFrA (st := st) Z m s s1 σ (unbWaitA (preschedA σ) (specPtr (L.curW s)) sc xs ns { xt with tcbSchedContext := none }) ∧ + WritesIn st s s1 (IpcFpA st Z E Rp Bf m σ s) ∧ L.curW s1 = L.curW s ∧ + RecvPreA st AR { Z with Q := Q } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s1 a + (unbWaitA (preschedA σ) (specPtr (L.curW s)) sc xs ns { xt with tcbSchedContext := none }) m := by + have htZ : L.curW s ∈ Z.T := J.sr.curT + set t := L.curW s with ht_def + have ct : σ.curThread = specPtr t := J.j.1.sch.curThread + obtain ⟨xt, hkt, r⟩ := J.ep.tcb t htZ + have hkc : σ.kheap σ.curThread = some (.tcb xt) := by rw [ct]; exact hkt + obtain ⟨nn, hnn, xn, sc, hbd, hkn, hna, hsn, hst0⟩ := hbn xt hkc + have hthT : Z.th t = some .tcb := (J.sr.thT t).2 htZ + have hnvm : L.mem s (stA t) ≠ sched.TS_RUNNING_VM := J.nvm + have hrunS : xt.tcbState = .running ∨ xt.tcbState = .restart := J.state hkt + have hidle : specPtr t ≠ σ.idleThread := J.notIdle + have hRun : SndRunA σ (specPtr t) xt := J.sndRun hkt + have hcm : m.curCore < fl4A64Platform.numNodes := by rw [J.j.1.sch.core]; exact hCs _ J.j.1.sch.cpuCs + have hte : True := trivial + obtain ⟨xs0, ns0, hks0⟩ : ∃ xs0 ns0, σ.kheap sc = some (.schedContext xs0 ns0) := by + obtain ⟨z, j, hz, -⟩ := J.sr.inv.back (specPtr t) xt sc hkt hst0 + exact ⟨z, j, hz⟩ + have hNT : sc ≠ specPtr t := fun e => by rw [e, hkt] at hks0; cases hks0 + have hsi : sc ≠ idleScPtr := fun e => J.sr.inv.ntfnIdle (specPtr nn) xn hkn (by rw [hsn, e]) + have hr' : ActReadyA σ := J.sr.actReady + have P := preschedA_keepA σ + have hktp : (preschedA σ).kheap (specPtr t) = some (.tcb xt) := by rw [P.kh]; exact hkt + have hksp : (preschedA σ).kheap sc = some (.schedContext xs0 ns0) := by rw [P.kh]; exact hks0 + set UW : State Unit := unbWaitA (preschedA σ) (specPtr t) sc xs0 ns0 xt with hUW + have eU : maybeReturnSc (Z := Unit) (specPtr nn) (specPtr t) σ = ({((), UW)}, False) := + maybeReturnSc_unbSwA hkn hkt hsn hst0 hks0 hNT ct.symm hr' (J.pendingSc hkt hst0) + have hkt1 : UW.kheap (specPtr t) = some (.tcb { xt with tcbSchedContext := none }) := unbWait_atTA hNT + have hks1 : UW.kheap sc = some (.schedContext { xs0 with scTcb := none } ns0) := unbWait_atSA hNT + have hRun1 : SndRunA UW (specPtr t) { xt with tcbSchedContext := none } := by + refine ⟨hRun.1, ⟨fun d pr => P.notin _ hRun.2.1.1 (fun h => J.noSelf (by rw [← ct] at h; exact h)) d pr, + fun h => hRun.2.1.2 (P.rls ▸ h)⟩, fun u hu => ?_, fun h => hRun.2.2.2 (h.trans P.idle)⟩ + have e : UW.schedulerAction = .chooseNewThread := rfl + rw [e] at hu; cases hu + have Qu := (P.qk.trans (unbWait_qkeepA hNT hktp hksp)).toSend + have hz : nn ≠ 0#u64 ∧ L.mem s (tBoundA t) = nn := bound_wordA (J.ep.at_tcb htZ hkt) hbd + -- the step at the walk and at the schedulers' relations + have hmr := maybeReturnIpcA J.sr J.ipcPlace J.iplaced.e J.iplaced.n hcm J.ntfn J.ep hnn htZ hRun.2.1.2 + obtain ⟨Pt, Ub, Pp7, Tb7, Hr, Te, Kt, Lv7, Re, bits, KLv, W⟩ := J7A.unp J.j7 + have I := J.delIn hA hPl W + have h : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s := by + obtain ⟨q, b, x, sg, sc⟩ := J.sys.ent.bxi + exact ⟨q, b, x, sc, sg⟩ + have hext : ∀ z ∈ (Aeneas.Std.Array.repeat 3#usize 0#u64).val, z ≠ 0#u64 → z ∈ OS := fun z hz hne => by + exact absurd (by simpa [Aeneas.Std.Array.repeat] using hz) hne + have D := J.decPreA (extra := Aeneas.Std.Array.repeat 3#usize 0#u64) hslot hext + have W0 : SW st H Z.Cs OS Z.V ia ie Ac s s := SW.start J.sys.sysl J.symL h + have hrun0 : L.mem s (stA t) ≠ sched.TS_INACTIVE := by + rcases J.runW with e | e | e <;> rw [e] <;> + simp [sched.TS_RUNNING, sched.TS_RESTART, sched.TS_RUNNING_VM, sched.TS_INACTIVE] + have walk : notification.maybe_return_sched_context st s nn t ⦃ s1 => RpPost st H Z.Cs OS Z.V ia ie Ac s s1 ⦄ := by + refine WP.spec_mono (maybe_return_sched_context.dmd 0#u64 nn t h W0.1.sys.ly.h0 W0.1.sym W0.1.sys.vr D.cb.mem + (LiveQ.tcb D.cb.mem (Or.inl hrun0)) ⟨D.cb.nq, D.cb.nr, D.cb.na⟩ D.cb.ni) ?_ + rintro s1 ⟨d, b⟩ + exact (W0.dmd0 I J.core0 J.ko d b).1.post + obtain ⟨s1, ew, regs, σU, Q, hU, H1, HN1, HE1, F⟩ := spec_existsA (WP.spec_and walk hmr) + have eσU : σU = UW := single_outA (hU.symm.trans eU) + subst eσU + have wI : WritesIn st s s1 (IpcFpA st Z E Rp Bf m σ s) := F.wr.mono fun b h => Or.inr h + have wN : WritesIn st s s1 (fun b => SchedStepNWA (st := st) N E Rp Z m σ s b ∨ BufW Bf b) := + wI.mono fun _ h => h.elim (ewrite_stepNA_buf J.sr.core.tcbs) fun h => Or.inl (Or.inl (Or.inl h)) + have wI' : WritesIn st s s1 (fun b => IpcFpA st Z E Rp Bf m σ s b ∨ RepWA Rp b) := wI.mono fun _ => Or.inl + set M : ObjRef → Prop := fun o => o = specPtr t with hMdef + have K : SIKA σ.idleThread M (fun _ => False) true σ UW := + (preschedA_sikaA hr').trans (unbWait_sikaA hNT hktp hksp hidle hsi P.noSw) + have notM : ∀ c < n, ¬ M (m.idleThreadOf c) := by + intro c hc' h + obtain ⟨y, hy, hs⟩ := J.sidle.f3 c hc' + rw [h, hkt] at hy + cases hy + rcases hrunS with e | e <;> rw [e] at hs <;> cases hs + have sinv : SInvXA UW m n := by + refine K.sinvSw J.sinv J.sidle J.body.sw J.noSelf (fun u hu h => J.swCur u hu h) notM + (fun h => hRun.2.2.2 h.symm) ?_ (fun _ h => h.elim) (fun _ h => h.elim) + (fun y hy => by rw [show y = specPtr t from hy]; exact hRun.2.1.2) (J.sidle.rdyA J.sinv) + (K.mfA J.sinv fun q hq x hx => by + rw [show q = specPtr t from hq, hkt] at hx; cases hx + exact fun hf => by have h0 := FaultStA.notRun hf; rw [hRun.1] at h0; cases h0) + intro y hy dm pr + rw [show y = specPtr t from hy] + exact hRun.2.1.1 dm pr + have hMv : ∀ u ∈ Z.T, M (specPtr u) → L.mem s (Fl4ProofsA64.P1.stA u) ≠ sched.TS_RUNNING_VM := fun u _ hu => by + rw [specPtr_inj (show specPtr u = specPtr t from hu)]; exact hnvm + have ssz : SSzA σ UW := fun q y k h => by + by_cases e : q = sc + · rw [e] at h ⊢ + rw [hks1] at h; cases h + exact ⟨xs0, hks0⟩ + · have e2 : q ≠ specPtr t := fun e' => by rw [e', hkt1] at h; cases h + exact ⟨y, by rw [← show UW.kheap q = σ.kheap q from (unbWait_keepA hNT e2 e).trans (congrFun P.kh q)]; exact h⟩ + have HS : SpecSchedA σ UW := P.spec.trans (unbWait_specSchedA hNT hktp hksp) + have Qk : QBlkA (specPtr '' E) (specPtr '' N) σ UW := QSendA.toBlk J.ipcSorted Qu + have sv : SchedValidA UW := unbWait_schedValidA hNT hktp hksp (preschedA_schedValidA J.schedValid) + (fun d pr => P.notin _ hRun.2.1.1 (fun h => J.noSelf (by rw [← ct] at h; exact h)) d pr) + (fun h => hRun.2.1.2 (P.rls ▸ h)) + have keepS : ∀ q y k, σ.kheap q = some (.schedContext y k) → ∃ y', UW.kheap q = some (.schedContext y' k) ∧ + y'.scReplies = y.scReplies := fun q y k h => by + by_cases e : q = sc + · rw [e] at h ⊢ + rw [hks0] at h; cases h + exact ⟨{ xs0 with scTcb := none }, hks1, rfl⟩ + · have e2 : q ≠ specPtr t := fun e' => by rw [e', hkt] at h; cases h + exact ⟨y, by rw [show UW.kheap q = σ.kheap q from (unbWait_keepA hNT e2 e).trans (congrFun P.kh q)]; exact h, rfl⟩ + have reply := J.toJStage3A.reply_ipcGA wI + (fun c _ y k h => by + obtain ⟨y', h2, e⟩ := keepS _ y k h + exact ⟨y', k, h2, e⟩) + (fun r' _ y h => by + rcases unbWait_casesA hNT h with ⟨-, c⟩ | ⟨-, c⟩ | ⟨-, -, h0⟩ + · cases c + · cases c + · exact ⟨y, by rw [P.kh] at h0; exact h0, rfl⟩) + have hm : ∀ u ∈ Z.T, L.mem s1 (Notif.tBlockA u) = L.mem s (Notif.tBlockA u) := fun u hu => + F.wr.mem_eq (Endp.a_blk (J.iplaced.e.tcb u hu)).1 + (J.tcbWord_offMwrA hu (K := 3336#u64) (by simp) (by simp)) + have blkQ1 : BlkQueuedA Z.T E N (L.mem s1) UW := J.blkQ.keepA hm + (fun u _ x' hx' => by + rcases unbWait_casesA hNT hx' with ⟨e, c⟩ | ⟨-, c⟩ | ⟨-, -, h0⟩ + · cases c + rw [e] + exact Or.inr ⟨xt, hkt, rfl⟩ + · cases c + · exact Or.inr ⟨x', by rw [P.kh] at h0; exact h0, rfl⟩) + (fun p y h => by + have h1 : p ≠ specPtr t := fun e => by rw [e, hkt] at h; cases h + have h2 : p ≠ sc := fun e => by rw [e, hks0] at h; cases h + rw [unbWait_keepA hNT h1 h2, congrFun P.kh p]; exact h) + (fun p y h => by + have h1 : p ≠ specPtr t := fun e => by rw [e, hkt] at h; cases h + have h2 : p ≠ sc := fun e => by rw [e, hks0] at h; cases h + rw [unbWait_keepA hNT h1 h2, congrFun P.kh p]; exact h) + obtain ⟨bxi, sysl, symL, userOK, j7⟩ := J.toJStage3A.finRpA hA hPl rfl regs UW + have J2 := J.toJStage3A.of_ipcStepUSA (Q := Q) (Rl := Z.R) (curOf := Z.curOf) (curScOf := Z.curScOf) wN + HS ssz Qk (fun _ _ => by + show UW.machineState.underlyingMemory _ = σ.machineState.underlyingMemory _ + exact congrFun (congrArg (fun z => z.underlyingMemory) P.mach) _) (fun _ _ => rfl) bxi sysl symL userOK j7 H1 HE1 HN1 + reply J.deadExt.reply K.ipcKeep (J.toJStage3A.tw_ipcA wI') sinv (K.sidle J.sidle notM (J.sidle.rdyA J.sinv)) + (J.toJStage3A.runVcpu_ipcA wI' K HE1 hMv) blkQ1 sv + have hcurUW : UW.curThread = specPtr t := P.cur.trans ct + have hcw : L.curW s1 = t := by + have := curW_keptA J.toJStage3A J2 (show UW.curThread = σ.curThread from hcurUW.trans ct.symm) + exact this + have act' : UW.schedulerAction = .chooseNewThread := rfl + have hbody : BodyOKA UW m := by + refine J.body.step (σ' := UW) (fun u e => by rw [act'] at e; cases e) P.time + (congrArg (fun z => z.lastMachineTime) P.mach) (show UW.curThread = σ.curThread from hcurUW.trans ct.symm) + P.csc P.con (fun _ _ _ _ _ h => P.rls ▸ h) (fun y' k' hz => ?_) + (fun h => absurd h (by rw [act']; intro c; cases c)) + rcases unbWait_casesA hNT hz with ⟨-, c⟩ | ⟨e, c⟩ | ⟨-, -, h00⟩ + · cases c + · cases c + exact ⟨xs0, by rw [e]; exact hks0, rfl, rfl, Or.inr rfl⟩ + · exact ⟨y', by rw [P.kh] at h00; exact h00, rfl, rfl, Or.inl rfl⟩ + have hst1 := (HE1.at_tcb htZ hkt1).st + refine ⟨nn, xt, sc, xs0, ns0, s1, Q, hnn, hz.2, hkc, hst0, hks0, hbd, ⟨xn, hkn, hna⟩, ew, eU, F, wI, hcw, ?_⟩ + refine ⟨J2, hbody, ?_, ?_, ?_, ?_, ?_⟩ + · rw [hcw] + rcases hrunS with e | e + · left; refine UScalar.eq_of_val_eq ?_; simpa [e, tsCode, sched.TS_RUNNING] using hst1 + · right; refine UScalar.eq_of_val_eq ?_; simpa [e, tsCode, sched.TS_RESTART] using hst1 + · intro d pr + show UW.curThread ∉ UW.readyQueues d pr + rw [hcurUW]; exact hRun1.2.1.1 d pr + · show UW.curThread ∉ UW.releaseQueue + rw [hcurUW]; exact hRun1.2.1.2 + · intro x sc' hx hs + change UW.kheap UW.curThread = some (.tcb x) at hx + rw [hcurUW, hkt1] at hx + cases hx + exact absurd hs (by simp) + · intro h + change UW.schedulerAction = .switchThread UW.curThread at h + rw [act'] at h; cases h + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3RecvWaitA.lean b/proofs/Fl4ProofsA64/P3/Stage3RecvWaitA.lean new file mode 100644 index 000000000..eb5fc8934 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3RecvWaitA.lean @@ -0,0 +1,737 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2c): **blocking `Wait` at the record**: `handle_recv` with `blocking = true` on an +idle or waiting notification without an SC. The caller is inserted by priority into the notification's queue +(`blkWaitA`; the other waiters' queue links change, so `NtfnStateRel` comes from FL4's body, not from keeps). P1's +facts come from `receive_signal.sw`. + +* **`JSyscallA.waitBlockA`**: `HRecvPostA`. + +Every spec datum is pinned by the lookup equation, the invoked slot's cap and the spec heap at the entry state. + +[S] the notification idle or waiting, without an SC; bound to no other thread; `numNodes = 1`. +[P] `ArchTotal`, `ArchPlat`, `hCs`, `hmb`, `hmax`, `htu`. New module. +-/ +import Fl4ProofsA64.P3.Stage3RecvBlockA +import Fl4ProofsA64.P3.Stage3RecvActiveA +import Fl4ProofsA64.P3.Stage3SignalIdleA +import Fl4ProofsA64.P3.Stage3RecvWaitBlkA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4Spec Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-- The objects of `blkWaitA`: the waiter, the notification, or kept. -/ +structure WaitStepA (σ σ' : State Unit) (t n : ObjRef) (x' : Notification) (xt : Tcb) : Prop where + atT : σ'.kheap t = some (.tcb (waitTcbA xt n)) + atN : σ'.kheap n = some (.notification x') + far : ∀ p, p ≠ t → p ≠ n → σ'.kheap p = σ.kheap p + frame : σ' = { σ with kheap := σ'.kheap, schedulerAction := σ'.schedulerAction } + act : σ'.schedulerAction = σ.schedulerAction ∨ σ'.schedulerAction = .chooseNewThread + cur : σ'.schedulerAction = .resumeCurThread → t ≠ σ.curThread + +/-- The facts of the state `set_thread_state` leaves. -/ +theorem waitSts_frameA (σ : State Unit) (t n : ObjRef) (xt : Tcb) : + waitStsA σ t n xt = { σ with kheap := (waitStsA σ t n xt).kheap, + schedulerAction := (waitStsA σ t n xt).schedulerAction } := by + unfold waitStsA stsAct updK + split_ifs <;> rfl + +theorem waitSts_actA (σ : State Unit) (t n : ObjRef) (xt : Tcb) : + (waitStsA σ t n xt).schedulerAction = σ.schedulerAction ∨ + (waitStsA σ t n xt).schedulerAction = .chooseNewThread := by + unfold waitStsA stsAct + split_ifs + · exact Or.inr rfl + · exact Or.inl rfl + +theorem waitSts_curA (σ : State Unit) (t n : ObjRef) (xt : Tcb) + (h : (waitStsA σ t n xt).schedulerAction = .resumeCurThread) : t ≠ σ.curThread := by + intro ht + unfold waitStsA stsAct at h + split_ifs at h with c + refine c ⟨ht, h, ?_⟩ + have k : (updK σ t (.tcb (waitTcbA xt n))).kheap t = some (.tcb (waitTcbA xt n)) := by + rw [updK_kheap, Function.update_self] + intro hs + unfold schedulable at hs + simp only [decide_eq_true_eq] at hs + obtain ⟨h1, -, -⟩ := hs + have e : Fl4Spec.optMapSome (tcbsOf (updK σ t (.tcb (waitTcbA xt n)))) Tcb.tcbState t = + some (ThreadState.blockedOnNotification n) := by + simp [Fl4Spec.optMapSome, Fl4Spec.optMap, tcbsOf, tcbsOfKh, tcbOf, updK_kheap] + unfold Fl4Spec.optPred at h1 + rw [e] at h1 + simp [runnable] at h1 + +/-- **`SIKA`** up to the state `set_thread_state` leaves: the waiter is the only mover. -/ +theorem waitSts_sikaA {σ : State Unit} {t n i : ObjRef} {xt : Tcb} (hkt : σ.kheap t = some (.tcb xt)) + {M N : ObjRef → Prop} {don : Bool} (hM : M t) : SIKA i M N don σ (waitStsA σ t n xt) := by + have hrq : (waitStsA σ t n xt).readyQueues = σ.readyQueues := by rw [waitSts_frameA] + have hrl : (waitStsA σ t n xt).releaseQueue = σ.releaseQueue := by rw [waitSts_frameA] + have htm : (waitStsA σ t n xt).curTime = σ.curTime := by rw [waitSts_frameA] + refine ⟨?_, fun p => ?_, fun d pr y h => Or.inl (by rwa [hrq] at h), + fun y h => by rwa [hrl] at h, (waitSts_actA σ t n xt).imp_right Or.inl, fun g => ⟨fun y k h => by + rw [htm] + rw [waitStsA_kheap] at h + by_cases e : idleScPtr = t + · rw [e, Function.update_self] at h; cases h + · rw [Function.update_of_ne e] at h; exact g.1 y k h, fun hne y k h a => by + have hc : (waitStsA σ t n xt).consumedTime = σ.consumedTime := by rw [waitSts_frameA] + have hs : (waitStsA σ t n xt).curSc = σ.curSc := by rw [waitSts_frameA] + rw [hc] at hne + rw [hs, waitStsA_kheap] at h + rw [htm] + by_cases e : σ.curSc = t + · rw [e, Function.update_self] at h; cases h + · rw [Function.update_of_ne e] at h; exact g.2 hne y k h a⟩⟩ + · exact (waitSts_frameA σ t n xt).trans (by rw [waitSts_frameA σ t n xt]) + · rw [waitStsA_kheap] + by_cases h1 : p = t + · subst h1 + rw [hkt, Function.update_self] + exact .tcb _ _ ⟨rfl, rfl, rfl, rfl, rfl, rfl, Or.inr ⟨hM, by simp⟩, Or.inl rfl, rfl, rfl⟩ + · rw [Function.update_of_ne h1] + exact .same _ + +theorem blkWait_stepA {σ : State Unit} {t n : ObjRef} {x : Notification} {xt : Tcb} (htn : t ≠ n) : + WaitStepA σ (blkWaitA σ t n x xt) t n (waitNtfnA σ t x xt) xt := by + have kh : (blkWaitA σ t n x xt).kheap = Function.update (Function.update σ.kheap t (some (.tcb (waitTcbA xt n)))) n + (some (.notification (waitNtfnA σ t x xt))) := by + unfold blkWaitA + rw [updK_kheap, waitStsA_kheap] + have sa : (blkWaitA σ t n x xt).schedulerAction = (waitStsA σ t n xt).schedulerAction := rfl + refine ⟨?_, ?_, ?_, ?_, ?_, ?_⟩ + · rw [kh, Function.update_of_ne htn, Function.update_self] + · rw [kh, Function.update_self] + · intro p h1 h2 + rw [kh, Function.update_of_ne h2, Function.update_of_ne h1] + · unfold blkWaitA waitStsA stsAct updK + split_ifs <;> rfl + · rw [sa]; exact waitSts_actA σ t n xt + · intro h + rw [sa] at h + exact waitSts_curA σ t n xt h + +section +variable {σ σ' : State Unit} {t n : ObjRef} {x x' : Notification} {xt : Tcb} + +theorem WaitStepA.curThread (S : WaitStepA σ σ' t n x' xt) : σ'.curThread = σ.curThread := by rw [S.frame] +theorem WaitStepA.idleThread (S : WaitStepA σ σ' t n x' xt) : σ'.idleThread = σ.idleThread := by rw [S.frame] +theorem WaitStepA.readyQueues (S : WaitStepA σ σ' t n x' xt) : σ'.readyQueues = σ.readyQueues := by rw [S.frame] +theorem WaitStepA.releaseQueue (S : WaitStepA σ σ' t n x' xt) : σ'.releaseQueue = σ.releaseQueue := by rw [S.frame] + +theorem WaitStepA.cases (S : WaitStepA σ σ' t n x' xt) {p : ObjRef} {o : KernelObject} (h : σ'.kheap p = some o) : + (p = t ∧ o = .tcb (waitTcbA xt n)) ∨ (p = n ∧ o = .notification x') ∨ + (p ≠ t ∧ p ≠ n ∧ σ.kheap p = some o) := by + by_cases h1 : p = t + · subst h1; rw [S.atT] at h; cases h; exact Or.inl ⟨rfl, rfl⟩ + · by_cases h2 : p = n + · subst h2; rw [S.atN] at h; cases h; exact Or.inr (Or.inl ⟨rfl, rfl⟩) + · exact Or.inr (Or.inr ⟨h1, h2, by rw [← S.far p h1 h2]; exact h⟩) + +/-- An object that is neither a TCB nor a notification is kept, both ways. -/ +theorem WaitStepA.keep (S : WaitStepA σ σ' t n x' xt) (hkt : σ.kheap t = some (.tcb xt)) + (hk : σ.kheap n = some (.notification x)) {p : ObjRef} {o : KernelObject} (h1 : ∀ y, o ≠ .tcb y) + (h2 : ∀ y, o ≠ .notification y) : (σ.kheap p = some o ↔ σ'.kheap p = some o) := by + constructor + · intro h + rw [S.far p (fun c => by rw [c, hkt] at h; exact h1 _ (Option.some.inj h).symm) + (fun c => by rw [c, hk] at h; exact h2 _ (Option.some.inj h).symm)] + exact h + · intro h + rcases S.cases h with ⟨-, c⟩ | ⟨-, c⟩ | ⟨-, -, c⟩ + · exact absurd c (h1 _) + · exact absurd c (h2 _) + · exact c + +theorem WaitStepA.specSched (S : WaitStepA σ σ' t n x' xt) (hkt : σ.kheap t = some (.tcb xt)) + (hk : σ.kheap n = some (.notification x)) : SpecSchedA σ σ' := by + refine ⟨fun p => ?_, by rw [S.frame], by rw [S.frame], by rw [S.frame], by rw [S.frame], by rw [S.frame], + by rw [S.frame]⟩ + by_cases h1 : p = t + · subst h1 + exact Or.inr (Or.inl ⟨_, _, hkt, S.atT, rfl, rfl, rfl⟩) + · by_cases h2 : p = n + · subst h2 + exact Or.inr (Or.inr ⟨_, _, hk, S.atN, Or.inr (Or.inl ⟨_, _, rfl, rfl⟩)⟩) + · exact Or.inl (S.far p h1 h2) + +theorem WaitStepA.schedValid (S : WaitStepA σ σ' t n x' xt) (V : SchedValidA σ) + (hkt : σ.kheap t = some (.tcb xt)) (hk : σ.kheap n = some (.notification x)) + (hsc : ∀ sc, xt.tcbSchedContext = some sc → ScActiveAtA σ sc) : SchedValidA σ' := by + have act : ∀ p, ScActiveAtA σ p → ScActiveAtA σ' p := fun p ⟨y, k, h, a⟩ => + ⟨y, k, (S.keep hkt hk (fun _ c => by cases c) (fun _ c => by cases c)).1 h, a⟩ + have tcb : ∀ u y, σ.kheap u = some (.tcb y) → ∃ y', σ'.kheap u = some (.tcb y') ∧ + y'.tcbSchedContext = y.tcbSchedContext := fun u y h => by + by_cases h1 : u = t + · subst h1; rw [hkt] at h; cases h; exact ⟨_, S.atT, rfl⟩ + · refine ⟨y, ?_, rfl⟩ + rw [S.far u h1 (fun c => by rw [c, hk] at h; cases h)] + exact h + have bound : ∀ u, (∃ y sc, σ.kheap u = some (.tcb y) ∧ y.tcbSchedContext = some sc ∧ ScActiveAtA σ sc) → + ∃ y sc, σ'.kheap u = some (.tcb y) ∧ y.tcbSchedContext = some sc ∧ ScActiveAtA σ' sc := + fun u ⟨y, sc, h, es, a⟩ => by + obtain ⟨y', h', e'⟩ := tcb u y h + exact ⟨y', sc, h', e'.trans es, act _ a⟩ + refine ⟨fun d p u hm => ?_, fun ha hi => ?_, fun u y sc hy hb es => ?_, fun u hu => ?_⟩ + · rw [S.readyQueues] at hm + obtain ⟨h, m⟩ := V.rq d p u hm + exact ⟨bound u h, by rw [S.releaseQueue]; exact m⟩ + · have ha0 : σ.schedulerAction = .resumeCurThread := by + rcases S.act with c | c + · rw [← c]; exact ha + · rw [c] at ha; cases ha + rw [S.curThread, S.idleThread] at hi + obtain ⟨h, m⟩ := V.act ha0 hi + rw [S.curThread, S.releaseQueue] + exact ⟨bound _ h, m⟩ + · rcases S.cases hy with ⟨-, ho⟩ | ⟨-, ho⟩ | ⟨-, -, hy0⟩ + · cases ho + exact act _ (hsc sc es) + · cases ho + · exact act _ (V.blk u y sc hy0 hb es) + · rw [S.releaseQueue] at hu + exact bound u (V.rls u hu) + +theorem WaitStepA.qblk (S : WaitStepA σ σ' t n x' xt) {E N : Set ObjRef} (h : IpcSortedA E N σ) + (hkt : σ.kheap t = some (.tcb xt)) (hk : σ.kheap n = some (.notification x)) + (hq' : x'.ntfnObj.ntfnQueue = specIns x.ntfnObj.ntfnQueue (prioOf σ) xt.tcbPriority t) + (hq : ∀ a ∈ x.ntfnObj.ntfnQueue, ∃ y, σ.kheap a = some (.tcb y)) : QBlkA E N σ σ' := by + have hp : ∀ a y, σ.kheap a = some (.tcb y) → ∃ y', σ'.kheap a = some (.tcb y') ∧ y'.tcbPriority = y.tcbPriority := + fun a y ha => by + by_cases h1 : a = t + · subst h1; rw [hkt] at ha; cases ha; exact ⟨_, S.atT, rfl⟩ + · exact ⟨y, by rw [S.far a h1 (fun c => by rw [c, hk] at ha; cases ha)]; exact ha, rfl⟩ + have back : ∀ a y, σ'.kheap a = some (.tcb y) → ∃ y0, σ.kheap a = some (.tcb y0) ∧ y.tcbPriority = y0.tcbPriority := + fun a y ha => by + rcases S.cases ha with ⟨rfl, ho⟩ | ⟨-, ho⟩ | ⟨-, -, h0⟩ + · cases ho; exact ⟨xt, hkt, rfl⟩ + · cases ho + · exact ⟨y, h0, rfl⟩ + refine ⟨h.of_ins hkt hp back (fun p _ z hz => ?_) (fun p _ z hz => ?_), fun A p y k hy => ?_, fun p y hy => ?_, + ⟨by rw [S.frame], by rw [S.frame], by rw [S.frame], by rw [S.frame]⟩⟩ + · rcases S.cases hz with ⟨-, ho⟩ | ⟨-, ho⟩ | ⟨-, -, h0⟩ + · cases ho + · cases ho + · exact ⟨z, h0, Or.inl (List.Sublist.refl _)⟩ + · rcases S.cases hz with ⟨-, ho⟩ | ⟨ep, ho⟩ | ⟨-, -, h0⟩ + · cases ho + · cases ho + exact ⟨x, by rw [ep]; exact hk, Or.inr ⟨hq', hq⟩⟩ + · exact ⟨z, h0, Or.inl (List.Sublist.refl _)⟩ + · exact A p y k ((S.keep hkt hk (fun _ c => by cases c) (fun _ c => by cases c)).2 hy) + · exact ⟨y, (S.keep hkt hk (fun _ c => by cases c) (fun _ c => by cases c)).2 hy, id⟩ + +end + +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {J5 : JSlotA S} {R : Set U64 → JSlotA S} {s s' : S} {a : A} {σ σ' : State Unit} + {m : McState} + +/-- A TCB word the blocking `Wait` does not write. -/ +theorem JStage3A.tcbKept_waitA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7A st R) s a σ m) + {t p : U64} (ht : t ∈ Z.T) (hp : p ∈ N) {xs : List U64} (hxs : ∀ y ∈ xs, y ∈ Z.T) + (w : WritesIn st s s' (WaitFpA (fun σ b => MWrA (st := st) Z m σ s b) σ t p xs)) + {u : U64} (hu : u ∈ Z.T) {K : U64} (hK : K.val < 4096) (h8 : K.val % 8 = 0) + (h : K.val ≠ 3464 ∧ K.val ≠ 3480 ∧ K.val ≠ 3488 ∧ K.val ≠ 3592 ∧ K.val ≠ 3496 ∧ K.val ≠ 3504) + (hut : u = t → K.val ≠ 3328 ∧ K.val ≠ 3336) : + L.mem s' (wa u K) = L.mem s (wa u K) := by + have hP := J.iplaced + have hua := J.sr.core.tcbs u hu + have hfit := hua.fits + have hv : (wa u K).val = u.val + K.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have apart : ∀ v ∈ Z.T, v ≠ u → v.val + 4096 ≤ u.val ∨ u.val + 4096 ≤ v.val := fun v hv' ne => by + have := J.sr.place.apart v u .tcb .tcb ((J.sr.thT v).2 hv') ((J.sr.thT u).2 hu) ne + have hsz : ObjKindA.size .tcb = 4096 := rfl + omega + have hta := hP.e.tcb t ht + refine w.mem_eq (field_aligned hua hK h8) ?_ + rintro ((c | c) | c | (c | c) | ⟨v, hv', c⟩) + · have e := congrArg (fun w : U64 => w.val) c + have := val_st hta + simp only [hv] at e + by_cases d : u = t + · subst d; have := (hut rfl).1; omega + · have := apart t ht (Ne.symm d); omega + · have e := congrArg (fun w : U64 => w.val) c + have := val_blk hta + simp only [hv] at e + by_cases d : u = t + · subst d; have := (hut rfl).2; omega + · have := apart t ht (Ne.symm d); omega + · exact J.tcbWord_offMwrA hu hK (by omega) c + · have e := congrArg (fun w : U64 => w.val) c + rw [Notif.epHeadA_eq] at e + simp only [hv] at e + rw [Notif.nfield_val (hP.n.ntfn p hp) (by simp)] at e + have := hP.n.disj p hp u hu + simp at e + omega + · have e := congrArg (fun w : U64 => w.val) c + rw [Notif.epTSA_eq] at e + simp only [hv] at e + rw [Notif.nfield_val (hP.n.ntfn p hp) (by simp)] at e + have := hP.n.disj p hp u hu + simp at e + omega + · have hvT : v ∈ Z.T := by + rcases List.mem_cons.1 hv' with rfl | h' + exacts [ht, hxs v h'] + have hva := hP.e.tcb v hvT + have := hva.fits + have e : (wa u K).val = v.val + 3496 ∨ (wa u K).val = v.val + 3504 := by + rcases c with c | c + · exact Or.inl (by rw [c, val_next hva]) + · exact Or.inr (by rw [c, val_prev hva]) + rw [hv] at e + by_cases d : v = u + · subst d; omega + · have := apart v hvT d; omega + +set_option maxHeartbeats 4000000 in +/-- **Blocking `Wait`** (module docstring). -/ +theorem RecvPreA.waitBlockA + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p badge slot : U64} {send canReply : Bool} + (hcap : cspace.lookup_cap st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.CAP)) = + ok (.Ok (.Notification p badge send true))) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hsc : capA st s slot = .Notification p badge send true) + (hp : p ∈ N) {x : Notification} (hk : σ.kheap (specPtr p) = some (.notification x)) + (hna : ∀ b, x.ntfnObj ≠ .activeNtfn b) (hnsc : x.ntfnSc = none) + (hbt : x.ntfnBoundTcb = some σ.curThread ∨ x.ntfnBoundTcb = none) : + HRecvPostA st AR ar hPl.ipa n Z.Cs s a m true canReply σ + (fun σ' => ∃ y, σ.kheap σ.curThread = some (.tcb y) ∧ + σ' = blkWaitA σ σ.curThread (specPtr p) x y) := by + have htZ : L.curW s ∈ Z.T := J.sr.curT + set t := L.curW s with ht_def + have ct : σ.curThread = specPtr t := J.j.1.sch.curThread + obtain ⟨xt, hkt, r⟩ := J.ep.tcb t htZ + have hkc : σ.kheap σ.curThread = some (.tcb xt) := by rw [ct]; exact hkt + have hnvm : L.mem s (stA t) ≠ sched.TS_RUNNING_VM := J.nvm + have hrunS : xt.tcbState = .running ∨ xt.tcbState = .restart := J.state hkt + have hr : runnable xt.tcbState = true := by rcases hrunS with e | e <;> rw [e] <;> rfl + have hthT : Z.th t = some .tcb := (J.sr.thT t).2 htZ + have hidle : specPtr t ≠ σ.idleThread := by + obtain ⟨y, hy, hs⟩ := J.sinv.idleTcb + intro h + rw [← h, hkt] at hy + cases hy + rcases hrunS with e | e <;> rw [e] at hs <;> cases hs + have hRun : SndRunA σ (specPtr t) xt := J.sndRun hkt + have hsca : ∀ sc, xt.tcbSchedContext = some sc → ScActiveAtA σ sc := fun sc hs => J.scAct xt sc hkc hs + have hcm : m.curCore < fl4A64Platform.numNodes := by rw [J.j.1.sch.core]; exact hCs _ J.j.1.sch.cpuCs + have hSOps := ipcSchedOpsA (st := st) (ar := ar) (κ := κ) J.iplaced J.ipcPlace hcm hmb hmax htu + have hF : SchedFootNA st ar N Z.T (IpcSchedRelA st Z m κ) (DonLocalA (st := st) Z.C) + (fun σ s => MWrA (st := st) Z m σ s) := schedFootNA_of_sched J.iplaced.n hcm hmb J.ipcPlace.notification + have hSR : IpcSchedRelA st Z m κ s σ := ⟨Z.Q, Z.R, J.sr⟩ + have hx := J.ntfn.at_ntfn hp hk + obtain ⟨xs, hQ, hxs, hbq⟩ := hx.obj.queue + have hxsT : ∀ y ∈ xs, y ∈ Z.T := fun y hy => hQ.mem y hy + have hsrt := J.ipcSorted.2 (specPtr p) ⟨p, hp, rfl⟩ x hk + have hsorted : PrioSorted (fun y => L.mem s (prioA y)) xs := by + unfold PrioSortedA at hsrt + rw [hxs, List.pairwise_map] at hsrt + refine hsrt.imp_of_mem fun {a b} ha hb hab => ?_ + obtain ⟨xa, hka, ra⟩ := J.ntfn.tcb a (hxsT a ha) + obtain ⟨xb, hkb, rb⟩ := J.ntfn.tcb b (hxsT b hb) + have h := hab xa xb hka hkb + rw [← ra.prio, ← rb.prio] at h + exact h + have hq : ∀ a ∈ x.ntfnObj.ntfnQueue, ∃ y, σ.kheap a = some (.tcb y) := by + intro a ha + rw [hxs] at ha + obtain ⟨u, hu, rfl⟩ := List.mem_map.1 ha + obtain ⟨y, hy, -⟩ := J.ntfn.tcb u (hxsT u hu) + exact ⟨y, hy⟩ + have hmq : ∀ y ∈ ntfnQueueOf x, ∃ z, σ.kheap y = some (.tcb z) ∧ z.tcbPriority = prioOf σ y := fun y hy => by + obtain ⟨z, hz⟩ := hq y hy + refine ⟨z, hz, ?_⟩ + simp [prioOf, Fl4Spec.optMapSome, Fl4Spec.optMap, tcbsOf, tcbsOfKh, tcbOf, hz] + have hnb : ∀ m, xt.tcbState ≠ .blockedOnNotification m := fun m c => by + rcases hrunS with e | e <;> rw [e] at c <;> cases c + have htq : specPtr t ∉ ntfnQueueOf x := not_member J.ntfn htZ hkt hnb p hp x hk + have htp : specPtr t ≠ specPtr p := fun h => by rw [h, hk] at hkt; cases hkt + -- FL4 and spec + have body := receive_signal_waitA (st := st) (ar := ar) (hSOps).1 hSOps.2.2.1 hSOps.2.1 hF J.iplaced s a σ J.ntfn J.ep + hSR htZ hkt hRun hp hk hna hnsc hQ hxs hsorted + obtain ⟨⟨s2, a2⟩, ew, ea, N2, E2, ⟨Q, Rl, sr2⟩, w⟩ := spec_existsA body + simp only at ea N2 E2 sr2 w + subst a2 + obtain ⟨σt, hσt⟩ : ∃ σt, waitStsA σ (specPtr t) (specPtr p) xt = σt := ⟨_, rfl⟩ + obtain ⟨y4, hy4⟩ : ∃ y4, waitNtfnA σ (specPtr t) x xt = y4 := ⟨_, rfl⟩ + have eσ0 : blkWaitA σ (specPtr t) (specPtr p) x xt = updK σt (specPtr p) (.notification y4) := by + unfold blkWaitA; rw [hσt, hy4] + obtain ⟨σ', hσ'⟩ : ∃ σ', blkWaitA σ (specPtr t) (specPtr p) x xt = σ' := ⟨_, rfl⟩ + rw [hσ'] at N2 E2 sr2 + have eσ : σ' = updK σt (specPtr p) (.notification y4) := hσ'.symm.trans eσ0 + have B : WaitStepA σ σ' (specPtr t) (specPtr p) y4 xt := by + rw [← hσ', ← hy4]; exact blkWait_stepA htp + have hyq : y4.ntfnObj = .waitingNtfn (specIns (ntfnQueueOf x) (prioOf σ) xt.tcbPriority (specPtr t)) := by + rw [← hy4]; unfold waitNtfnA; rfl + -- FL4: `handle_recv` + have hb : L.mem s (Notif.nBoundA p) = t ∨ L.mem s (Notif.nBoundA p) = 0#u64 := by + by_cases h0 : L.mem s (Notif.nBoundA p) = 0#u64 + · exact Or.inr h0 + · rcases hbt with h | h + · rw [hx.bound, optRef, if_neg h0, ct] at h + exact Or.inl (specPtr_inj (Option.some.inj h)) + · rw [hx.bound, optRef, if_neg h0] at h + cases h + have hfl : syscall.handle_recv st ar s a true canReply = ok (s2, a) := by + unfold syscall.handle_recv + rw [cur_thread_ok, bind_ok, tcb_reg_eqA, bind_ok] + rw [show L.curW s = t from rfl, hcap] + simp only [bind_ok, ↓reduceIte] + obtain ⟨v', hv', e⟩ := spec_existsA (Notif.obj.Ntfn.bound_tcb.spec (st := st) s p) + rw [hv', bind_ok, e] + rcases hb with h | h + · simp only [h, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte, ite_self] + exact ew + · simp only [h, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + exact ew + -- the spec + have hrecv : ((), σ') ∈ (receiveSignal (Z := Unit) (specPtr t) + (absCapA (.Notification p badge send true)) true σ).1 := by + rw [show absCapA (.Notification p badge send true) = + .notificationCap (specPtr p) badge.bv (rightsOfA send true false false) from rfl, + blkWait_specA hk hkt hna hnsc htq hmq, hσ'] + exact Set.mem_singleton _ + have hmem : ((), σ') ∈ (handleRecv (Z := Unit) true canReply σ).1 := by + have capReg : (L.mem s (regWA t layout.reg.CAP)).bv = xt.tcbArch.tcbContext.userRegs cap_register := by + rw [← r.regs cap_register, regIdxA_capA]; rfl + have capRead : NonDet.liftM dataToCptr (asUser (Z := Unit) (specPtr t) (getRegister cap_register)) σ = + ({(toBl (L.mem s (regWA t layout.reg.CAP)).bv, σ)}, False) := by + unfold NonDet.liftM + rw [nd_bind_singleA' (asUser_getRegister_eqA hkt _), ← capReg] + rfl + have hc0 : (lookupCap (Z := Unit) (specPtr t) (toBl (L.mem s (regWA t layout.reg.CAP)).bv)).run σ = + ({(.ok (absCapA (.Notification p badge send true)), σ)}, False) := by + have l := post_of_okA (lookup_cap_recv_refinesA (st := st) _ s σ J.j.1.st J.j.1.cv t hthT _) hcap + obtain ⟨e, he, hrel⟩ := l + cases e with + | error _ => exact hrel.elim + | ok ac => + obtain rfl : absCapA (.Notification p badge send true) = ac := hrel.2 + exact he + have hc1 : (capFaultOnFailure (ofBl (toBl (L.mem s (regWA t layout.reg.CAP)).bv)) true + (lookupCap (Z := Unit) (specPtr t) (toBl (L.mem s (regWA t layout.reg.CAP)).bv))).run σ = + ({(.ok (absCapA (.Notification p badge send true)), σ)}, False) := run_handleE_okA hc0 + rw [handleRecv, nd_gets_bindSc, ct, nd_bind_oneSc capRead] + apply mem_bindA.2 + refine ⟨(.ok (), σ'), ?_, rfl⟩ + rw [run_bind_okA hc1] + dsimp only [absCapA] + have boundRead : (NonDetE.liftE (ε := Fault) + (getNtfnObjRef (Z := Unit) Notification.ntfnBoundTcb (specPtr p))).run σ = + ({(.ok x.ntfnBoundTcb, σ)}, False) := run_liftEA (getNtfnObjRef_eqA hk _) + rw [if_pos (by simp [AllowRecv, rightsOfA]), run_bind_okA boundRead, if_pos (by rwa [ct] at hbt)] + unfold NonDetE.liftE + rw [ExceptT.run_lift] + exact mem_bindA.2 ⟨((), σ'), hrecv, rfl⟩ + -- P1's walk + obtain ⟨Pt, Ub, Pp7, Tb7, Hr, Te, Kt, Lv7, Re, bits, KLv, W⟩ := J7A.unp J.j7 + have I := J.delIn hA hPl W + have h : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s := by + obtain ⟨q, b, x, sg, sc⟩ := J.sys.ent.bxi + exact ⟨q, b, x, sc, sg⟩ + have D := J.decPreA (extra := Aeneas.Std.Array.repeat 3#usize 0#u64) hslot (fun z hz hne => by + exact absurd (by simpa [Aeneas.Std.Array.repeat] using hz) hne) + have hH : H p = some .ntfn := J.utIpc.ntfn p hp + have regs : RpPost st H Z.Cs OS Z.V ia ie Ac s s2 := by + have walk := Fl4ProofsA64.P1.W7.R.ST.receive_signal.sw (st := st) hA a t p true I J.core0 J.ko + (SW.start J.sys.sysl J.symL h) D.cb + (by rcases J.runW with e | e | e <;> rw [e] <;> + simp [sched.TS_RUNNING, sched.TS_RESTART, sched.TS_RUNNING_VM, sched.TS_INACTIVE]) + (show p ∈ H.ntfns from hH) hH + (by + rw [epState_eq] + have : stOf (L.mem s) p = obj.NTFN_IDLE ∨ stOf (L.mem s) p = obj.NTFN_WAITING := by + cases h : x.ntfnObj with + | idleNtfn => exact Or.inl (stOf_idle (T := Z.T) (h ▸ hx.obj) rfl) + | waitingNtfn q => exact Or.inr (stOf_waiting (T := Z.T) (h ▸ hx.obj) rfl) + | activeNtfn b => exact absurd h (hna b) + rcases this with e | e <;> rw [e] <;> simp [ntfn_consts.1, ntfn_consts.2.1]) + (J.runVcpu.zero htZ hnvm) + (fun k hk' _ => by + rw [hH] at hk' + cases hk' + exact ⟨slot, hslot, by rw [hsc]; exact names_ntfn⟩) + exact (post_of_okA (P := fun q => SW st H Z.Cs OS Z.V ia ie Ac s q.1) walk ew).1.post + obtain ⟨bxi, sysl, symL, userOK, j7⟩ := J.toJStage3A.finRpA hA hPl rfl regs σ' + -- the record + have hP := J.iplaced + have twk := fun (u : U64) (hu : u ∈ Z.T) (K : U64) => J.toJStage3A.tcbKept_waitA (s' := s2) htZ hp hxsT w hu (K := K) + have wN : WritesIn st s s2 (fun b => SchedStepNWA (st := st) N E Rp Z m σ s b ∨ BufW Bf b) := + w.mono fun b h => by + have hta := hP.e.tcb t htZ + have tw : ∀ (k : Nat), b.val = t.val + k → TWOffN k → IpcFpA st Z E Rp Bf m σ s b := fun k hv hk' => + Or.inl (Or.inr (Or.inr (Or.inl ⟨t, htZ, Or.inl (twf_ofA k hv hk')⟩))) + have lift : IpcFpA st Z E Rp Bf m σ s b → SchedStepNWA (st := st) N E Rp Z m σ s b ∨ BufW Bf b := + fun h => h.elim (ewrite_stepNA_buf J.sr.core.tcbs) fun h => Or.inl (Or.inl (Or.inl h)) + have nw : ∀ k : Nat, b.val = p.val + k → k < 40 → SchedStepNWA (st := st) N E Rp Z m σ s b := fun k hv hk' => + Or.inr (Or.inl ⟨p, hp, by omega, by omega⟩) + rcases h with (c | c) | c | (c | c) | ⟨v, hv, c⟩ + · exact lift (tw 3328 (by rw [c, val_st hta]) (by unfold TWOffN; omega)) + · exact lift (tw 3336 (by rw [c, val_blk hta]) (by unfold TWOffN; omega)) + · exact lift (Or.inr c) + · refine Or.inl (nw 0 ?_ (by omega)) + rw [c, Notif.epHeadA_eq, Notif.nfield_val (hP.n.ntfn p hp) (by simp)] + simp + · refine Or.inl (nw 8 ?_ (by omega)) + rw [c, Notif.epTSA_eq, Notif.nfield_val (hP.n.ntfn p hp) (by simp)] + simp + · have hvT : v ∈ Z.T := by + rcases List.mem_cons.1 hv with rfl | hv + exacts [htZ, hxsT v hv] + have hva := hP.e.tcb v hvT + refine lift (Or.inl (Or.inr (Or.inr (Or.inl ⟨v, hvT, Or.inr ?_⟩)))) + rcases c with c | c + · exact c ▸ (a_next hva).2 + · exact c ▸ (a_prev hva).2 + set M : ObjRef → Prop := fun o => o = specPtr t with hMdef + have K0 : SIKA σ.idleThread M (fun _ => False) false σ σt := by + rw [← hσt]; exact waitSts_sikaA hkt rfl + have notM : ∀ c < n, ¬ M (m.idleThreadOf c) := by + intro c hc' h + obtain ⟨y, hy, hs⟩ := J.sidle.f3 c hc' + rw [h, hkt] at hy + cases hy + rcases hrunS with e | e <;> rw [e] at hs <;> cases hs + have sinv0 : SInvXA σt m n := by + refine K0.sinvSw J.sinv J.sidle J.body.sw J.noSelf (fun u hu h => J.swCur u hu h) notM (fun h => hRun.2.2.2 h.symm) ?_ (fun _ h => h.elim) (fun _ h => h.elim) + (fun y hy => by rw [show y = specPtr t from hy]; exact hRun.2.1.2) (J.sidle.rdyA J.sinv) + (K0.mfA J.sinv fun q hq x hx => by + rw [show q = specPtr t from hq, hkt] at hx; cases hx + exact fun hf => by have h0 := FaultStA.notRun hf; rw [hRun.1] at h0; cases h0) + intro y hy dm pr + rw [show y = specPtr t from hy] + exact hRun.2.1.1 dm pr + have sidle0 := K0.sidle J.sidle notM (J.sidle.rdyA J.sinv) + have atp : σ'.kheap (specPtr p) = some (.notification y4) := B.atN + have eid : σ'.idleThread = σt.idleThread := by rw [eσ]; rfl + have etm : σ'.curTime = σt.curTime := by rw [eσ]; rfl + have ktp : σt.kheap (specPtr p) = some (.notification x) := by + rw [← hσt, waitStsA_kheap, Function.update_of_ne (Ne.symm htp), hk] + have same : ∀ q, q ≠ specPtr p → σ'.kheap q = σt.kheap q := fun q e => by + rw [eσ, updK_kheap, Function.update_of_ne e] + have keepK : ∀ q ob, σt.kheap q = some ob → (∀ y, ob ≠ .notification y) → σ'.kheap q = some ob := + fun q ob h ne => by + rw [same q fun e => by rw [e, ktp] at h; exact ne x (Option.some.inj h).symm] + exact h + have backK : ∀ q ob, σ'.kheap q = some ob → (∀ y, ob ≠ .notification y) → σt.kheap q = some ob := + fun q ob h ne => by + by_cases e : q = specPtr p + · rw [e, atp] at h + exact absurd (Option.some.inj h).symm (ne _) + · rwa [same q e] at h + have sinv : SInvXA σ' m n := by + have same' : SchedSameA σt σ' := + { kh := fun q ko hko => by + have ne : ∀ y, ko ≠ .notification y := fun y e => by + rcases hko with ⟨_, e'⟩ | ⟨_, _, e'⟩ <;> rw [e] at e' <;> cases e' + exact ⟨fun h => backK q ko h ne, fun h => keepK q ko h ne⟩ + cur := by rw [eσ]; rfl + idle := by rw [eσ]; rfl + csc := by rw [eσ]; rfl + rq := by rw [eσ]; rfl + rls := by rw [eσ]; rfl + time := by rw [eσ]; rfl + cons := by rw [eσ]; rfl } + refine sinv0.of_spec same' (by rw [eσ]; rfl) ?_ + have I := sinv0.irqN + have kk : ∀ q ob b a', (updK σt (specPtr p) (.notification y4)).kheap q = some ob → capOf ob b = some a' → + σt.kheap q = some ob := fun q ob b a' h hc' => by + refine backK q ob (by rw [eσ]; exact h) fun y e => ?_ + subst e + cases hc' + have ct' : ∀ k q, capTableAt k q (updK σt (specPtr p) (.notification y4)) ↔ capTableAt k q σt := fun k q => by + rw [← eσ] + unfold capTableAt + by_cases e : q = specPtr p + · rw [e, atp, ktp] + constructor <;> rintro ⟨cs, h, -⟩ <;> simp at h + · rw [same q e] + rw [eσ] + refine ⟨?_, fun irq hi ob b a' h hc' => I.slot irq hi ob b a' (kk _ _ _ _ h hc') hc', + fun q ob b a' h hc' => I.ref q ob b a' (kk _ _ _ _ h hc') hc'⟩ + obtain ⟨⟨inj, tab⟩, dom⟩ := I.node + exact ⟨⟨inj, fun irq h => (ct' _ _).2 (tab irq h)⟩, fun irq h k c => dom irq h k ((ct' _ _).1 c)⟩ + have idleVt : ∀ c < n, vtCapA σ' (m.idleThreadOf c) = some .nullCap ∧ vcpuOfA σ' (m.idleThreadOf c) = none := by + intro c hc' + obtain ⟨y, hy, -⟩ := sidle0.f3 c hc' + exact idleVtSameA (by rw [hy]; exact keepK _ _ hy (fun _ e => by cases e)) (sidle0.idleSlot c hc') + (sidle0.idleBound c hc') + have sidle : SIdleA σ' m n := + { f3 := fun c hc' => by + obtain ⟨y, hy, hs⟩ := sidle0.f3 c hc' + exact ⟨y, keepK _ _ hy (fun _ e => by cases e), hs⟩ + f4 := fun y k h => sidle0.f4 y k (backK _ _ h (fun _ e => by cases e)) + f5 := fun q y k h => by + rw [eid]; exact sidle0.f5 q y k (backK _ _ h (fun _ e => by cases e)) + f6 := sidle0.f6.of_eq fun sl => by + unfold specCapAtA + by_cases e : sl.1 = specPtr p + · rw [e, atp, ktp] + · rw [same _ e] + f7 := fun y k h => by + rw [etm]; exact sidle0.f7 y k (backK _ _ h (fun _ e => by cases e)) + idleSlot := fun c hc' => (idleVt c hc').1 + idleBound := fun c hc' => (idleVt c hc').2 } + have HS := B.specSched hkt hk + have keepS : ∀ q y k, σ.kheap q = some (.schedContext y k) ↔ σ'.kheap q = some (.schedContext y k) := + fun q y k => B.keep hkt hk (fun _ e => by cases e) (fun _ e => by cases e) + have keepR : ∀ q y, σ.kheap q = some (.reply y) ↔ σ'.kheap q = some (.reply y) := + fun q y => B.keep hkt hk (fun _ e => by cases e) (fun _ e => by cases e) + have ssz : SSzA σ σ' := fun q y k h => ⟨y, (keepS q y k).2 h⟩ + have reply : ReplyStackRelA (L.mem s2) Rp Z.C σ' (scNameA m) stack := by + refine J.reply.frame (fun r' hr' => ?_) (fun c _ y k h => ⟨y, k, (keepS _ _ _).1 h, rfl⟩) + (fun _ _ y h => ⟨y, (keepR _ _).2 h, rfl⟩) + have hrH : H r' = some .reply := J.utIpc.reply r' hr' + obtain ⟨r32, -, rfit⟩ := hP.e.rep r' hr' + have one : ∀ o : U64, 8 ≤ o.val → o.val < 32 → o.val % 8 = 0 → L.mem s2 (wa r' o) = L.mem s (wa r' o) := by + intro o h1 h2 h3 + have hv : (wa r' o).val = r'.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + refine w.mem_eq (by unfold Aligned; rw [hv]; omega) ?_ + have hw : IpcObjWA N E Rp (wa r' o) := Or.inr (Or.inr ⟨r', hr', by omega, by omega⟩) + have offT : ∀ v ∈ Z.T, ∀ k, k < 4096 → (wa r' o).val ≠ v.val + k := fun v hv' k hk' => by + have := hP.e.rt r' hr' v hv'; omega + have offN : (wa r' o).val ≠ p.val ∧ (wa r' o).val ≠ p.val + 8 := by + have := hP.nr p hp r' hr'; omega + have hta := hP.e.tcb t htZ + rintro ((c | c) | c | (c | c) | ⟨v, hv', c⟩) + · exact offT t htZ 3328 (by omega) (by rw [c, val_st hta]) + · exact offT t htZ 3336 (by omega) (by rw [c, val_blk hta]) + · exact J.ipc_offMwrA hw c + · refine offN.1 ?_ + rw [c, Notif.epHeadA_eq, Notif.nfield_val (hP.n.ntfn p hp) (by simp)] + simp + · refine offN.2 ?_ + rw [c, Notif.epTSA_eq, Notif.nfield_val (hP.n.ntfn p hp) (by simp)] + simp + · have hvT : v ∈ Z.T := by + rcases List.mem_cons.1 hv' with rfl | h' + exacts [htZ, hxsT v h'] + have hva := hP.e.tcb v hvT + rcases c with c | c + · exact offT v hvT 3496 (by omega) (by rw [c, val_next hva]) + · exact offT v hvT 3504 (by omega) (by rw [c, val_prev hva]) + exact ⟨one _ (by simp [layout.reply.NEXT]) (by simp [layout.reply.NEXT]) (by simp [layout.reply.NEXT]), + one _ (by simp [layout.reply.PREV]) (by simp [layout.reply.PREV]) (by simp [layout.reply.PREV])⟩ + have tcbF : ∀ q y, σ.kheap q = some (.tcb y) → ∃ y', σ'.kheap q = some (.tcb y') ∧ y'.tcbFault = y.tcbFault ∧ + y'.tcbIpcframe = y.tcbIpcframe ∧ y'.tcbIpcBuffer = y.tcbIpcBuffer ∧ y'.tcbMcpriority = y.tcbMcpriority ∧ + y'.tcbYieldTo = y.tcbYieldTo ∧ y'.tcbFlags = y.tcbFlags := fun q y hy => by + by_cases e : q = specPtr t + · subst e + rw [hkt] at hy; cases hy + exact ⟨_, B.atT, rfl, rfl, rfl, rfl, rfl, rfl⟩ + · exact ⟨y, by rw [B.far q e (fun c => by rw [c, hk] at hy; cases hy)]; exact hy, rfl, rfl, rfl, rfl, rfl, rfl⟩ + have Kk : IpcKeepA σ σ' := ⟨tcbF, fun q y k h => ⟨y, k, (keepS _ _ _).1 h, rfl⟩, + fun q y k h => ⟨y, k, (keepS _ _ _).2 h, rfl⟩⟩ + have twr : ∀ u ∈ Z.T, ∀ O : U64, O.val < 224 ∨ O.val = 3440 ∨ O.val = 3472 ∨ O.val = 3512 ∨ O.val = 3456 → O.val % 8 = 0 → + L.mem s2 (wa u O) = L.mem s (wa u O) := fun u hu O hO h8 => + twk u hu O (by omega) h8 (by omega) (fun _ => by omega) + have kst : ∀ u ∈ Z.T, u ≠ t → L.mem s2 (stA u) = L.mem s (stA u) := fun u hu ne => by + rw [stA_eq] + exact twk u hu 3328#u64 (by simp) (by simp) (by simp) (fun c => absurd c ne) + have kblk : ∀ u ∈ Z.T, u ≠ t → L.mem s2 (Notif.tBlockA u) = L.mem s (Notif.tBlockA u) := fun u hu ne => by + rw [show Notif.tBlockA u = wa u 3336#u64 from rfl] + exact twk u hu 3336#u64 (by simp) (by simp) (by simp) (fun c => absurd c ne) + have rv : RunVcpuInv st Z.T s2 := fun u hu h0 => by + have e : L.mem s2 (runVcpuA u) = L.mem s (runVcpuA u) := + twk u hu 3672#u64 (by simp) (by simp) (by simp) (fun _ => by simp) + rw [e] at h0 + by_cases c : u = t + · subst c + exact absurd (J.runVcpu.zero htZ hnvm) h0 + · rw [show L.mem s2 (Fl4ProofsA64.P1.stA u) = L.mem s (Fl4ProofsA64.P1.stA u) from kst u hu c] + exact J.runVcpu u hu h0 + have blkQ : BlkQueuedA Z.T E N (L.mem s2) σ' := by + have old : ∀ u ∈ Z.T, ∀ y, σ'.kheap (specPtr u) = some (.tcb y) → u ≠ t → σ.kheap (specPtr u) = some (.tcb y) := by + intro u _ y hy ne + rcases B.cases hy with ⟨c, -⟩ | ⟨-, c⟩ | ⟨-, -, c⟩ + · exact absurd (specPtr_inj c) ne + · cases c + · exact c + refine ⟨fun u hu y hy e hb' => ?_, fun u hu y hy q hq' => ?_⟩ + · have ne : u ≠ t := fun c => by + rw [c, B.atT] at hy + cases hy + cases hb' + obtain ⟨hE, z, hz, hm⟩ := J.blkQ.ep u hu y (old u hu y hy ne) e hb' + exact ⟨by rw [kblk u hu ne]; exact hE, z, (B.keep (x := x) hkt hk (fun _ c => by cases c) + (fun _ c => by cases c)).1 hz, hm⟩ + · by_cases c : u = t + · subst c + rw [B.atT] at hy + cases hy + cases hq' + obtain ⟨y1, hy1, r'⟩ := N2.tcb t htZ + rw [B.atT] at hy1; cases hy1 + have eb : L.mem s2 (Notif.tBlockA t) = p := specPtr_inj (r'.blk (specPtr p) rfl) + exact ⟨by rw [eb]; exact hp, _, _, atp, hyq, mem_specInsA.2 (Or.inl rfl)⟩ + · obtain ⟨hN, z, l, hz, ho, hm⟩ := J.blkQ.ntfn u hu y (old u hu y hy c) q hq' + refine ⟨by rw [kblk u hu c]; exact hN, ?_⟩ + by_cases e : q = specPtr p + · subst e + rw [hk] at hz + cases hz + refine ⟨_, _, atp, hyq, mem_specInsA.2 (Or.inr ?_)⟩ + show specPtr u ∈ ntfnQueueOf x + unfold ntfnQueueOf + rw [ho]; exact hm + · refine ⟨z, l, ?_, ho, hm⟩ + rw [B.far q (fun d => by rw [d, hkt] at hz; cases hz) e] + exact hz + have J2 := J.toJStage3A.of_ipcStepUSA (Q := Q) (Rl := Rl) (curOf := Z.curOf) (curScOf := Z.curScOf) wN + HS ssz (B.qblk J.ipcSorted hkt hk (by rw [hyq]; rfl) hq) (fun _ _ => by rw [B.frame]) (fun _ _ => rfl) bxi sysl + symL userOK j7 sr2 E2 N2 reply J.deadExt.reply Kk twr sinv sidle rv blkQ (B.schedValid J.schedValid hkt hk hsca) + refine ⟨s2, a, σ', hfl, hmem, ⟨xt, hkc, by rw [ct]; exact hσ'.symm⟩, A64RelS3.ofJ J2 ?bo, + curW_keptA J.toJStage3A J2 B.curThread, ?bo⟩ + refine J.body.stepEq (σ' := σ') (by + rw [eσ] + exact (K0.swA J.body.sw (fun u hu h => J.swCur u hu h) (fun c => by cases c) (fun _ h _ => h.elim)).updOther ktp + (fun _ c => by cases c) (fun _ _ c => by cases c)) (by rw [B.frame]) (by rw [B.frame]) B.curThread + (by rw [B.frame]) (by rw [B.frame]) B.releaseQueue (fun y' k' hz => ?_) (fun h => ?_) + (fun hh => by + have h' : σt.schedulerAction = .resumeCurThread := by rw [eσ] at hh; exact hh + rcases K0.act with e | e | ⟨y, -, e⟩ <;> first | (rw [e] at h'; exact h') | (rw [e] at h'; cases h')) + · exact ⟨y', (keepS _ _ _).2 hz, rfl, rfl, rfl⟩ + · exact absurd ct.symm (B.cur h) + +set_option maxHeartbeats 1000000 in +/-- **`RecvPreA.waitBlockA` at a syscall entry.** -/ +theorem JSyscallA.waitBlockA + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p badge slot : U64} {send canReply : Bool} + (hcap : cspace.lookup_cap st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.CAP)) = + ok (.Ok (.Notification p badge send true))) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hsc : capA st s slot = .Notification p badge send true) + (hp : p ∈ N) {x : Notification} (hk : σ.kheap (specPtr p) = some (.notification x)) + (hna : ∀ b, x.ntfnObj ≠ .activeNtfn b) (hnsc : x.ntfnSc = none) + (hbt : x.ntfnBoundTcb = some σ.curThread ∨ x.ntfnBoundTcb = none) : + HRecvPostA st AR ar hPl.ipa n Z.Cs s a m true canReply σ + (fun σ' => ∃ y, σ.kheap σ.curThread = some (.tcb y) ∧ + σ' = blkWaitA σ σ.curThread (specPtr p) x y) := + RecvPreA.waitBlockA hA hPl J.toRecvPreA hCs h1 hmb hmax htu hcap hslot hcov hsc hp hk hna hnsc hbt + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3RecvWaitBlkA.lean b/proofs/Fl4ProofsA64/P3/Stage3RecvWaitBlkA.lean new file mode 100644 index 000000000..45e8174e9 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3RecvWaitBlkA.lean @@ -0,0 +1,393 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2c): **a blocking `receive_signal` on a notification without an SC**: FL4's four +stores and `ntfn_append` (`maybe_return_sched_context` does nothing), the spec's one outcome (`blkWaitA`), with the +notification and endpoint relations, the scheduler relation and the write set at the new state. + +[S] the notification has no SC. No premise. New module. +-/ +import Fl4ProofsA64.P3.NtfnBlockA +import Fl4ProofsA64.P3.EpRecvBlkA +import Fl4ProofsA64.P3.EpRecvBndA +import Fl4ProofsA64.P3.Stage3SortInsA +import Fl4ProofsA64.P3.Stage3RecvSigKeepA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched + Fl4Spec Fl4SpecA64 + +noncomputable section + +/-- The waiter's TCB after a blocking receive on the notification at `n`. -/ +abbrev waitTcbA (xt : Tcb) (n : ObjRef) : Tcb := { xt with tcbState := .blockedOnNotification n } + +/-- The spec state `set_thread_state` leaves. -/ +def waitStsA (σ : State Unit) (t n : ObjRef) (xt : Tcb) : State Unit := + stsAct (updK σ t (.tcb (waitTcbA xt n))) t + +/-- The notification after the waiter is queued. -/ +def waitNtfnA (σ : State Unit) (t : ObjRef) (x : Notification) (xt : Tcb) : Notification := + { x with ntfnObj := .waitingNtfn (specIns (ntfnQueueOf x) (prioOf σ) xt.tcbPriority t) } + +/-- **The spec's state after a blocking `receive_signal`** (module docstring). -/ +def blkWaitA (σ : State Unit) (t n : ObjRef) (x : Notification) (xt : Tcb) : State Unit := + updK (waitStsA σ t n xt) n (.notification (waitNtfnA σ t x xt)) + +theorem waitStsA_kheap (σ : State Unit) (t n : ObjRef) (xt : Tcb) : + (waitStsA σ t n xt).kheap = Function.update σ.kheap t (some (.tcb (waitTcbA xt n))) := by + unfold waitStsA + rw [stsAct_kheap, updK_kheap] + +/-- The write set of a blocking `receive_signal` (as `NtfnBlkFpA`). -/ +def WaitFpA (Wr : State Unit → _root_.Aeneas.Std.U64 → Prop) (σ : State Unit) (t n : U64) (xs : List U64) + (b : U64) : Prop := + (b = stA t ∨ b = tBlockA t) ∨ Wr σ b ∨ (b = epHeadA n ∨ b = epTSA n) ∨ + ∃ w ∈ t :: xs, b = nextA .Ep w ∨ b = prevA .Ep w + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + {N T E R : Set U64} {Bf : Set (U64 × Word)} {SR : S → State Unit → Prop} + {Wr : State Unit → S → U64 → Prop} + +set_option maxHeartbeats 2000000 in +/-- **The FL4 body of a blocking `receive_signal`** (module docstring). -/ +theorem receive_signal_waitA {DL : S → U64 → Prop} (hH : SchedHypsA st N T SR) (hOE : SchedOpsEA st T E R Bf SR) + (hOn : SchedOpsA st ar N T SR DL) (hF : SchedFootNA st ar N T SR DL Wr) + (hP : IPlacedA N T E R Bf) + (s : S) (a : A) (σ : State Unit) (hN : NtfnStateRel N T (L.mem s) σ) (hE : EpStateRel T E R Bf (L.mem s) σ) + (hSR : SR s σ) + {t : U64} (ht : t ∈ T) {xt : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hsr : SndRunA σ (specPtr t) xt) + {n : U64} (hn : n ∈ N) {x : Notification} (hk : σ.kheap (specPtr n) = some (.notification x)) + (hna : ∀ b, x.ntfnObj ≠ .activeNtfn b) (hsc : x.ntfnSc = none) + {xs : List U64} (hQ : QueueRel (L.mem s) T .Ep (qOf (L.mem s) n) xs) + (hxs : x.ntfnObj.ntfnQueue = xs.map specPtr) + (hsorted : PrioSorted (fun y => L.mem s (prioA y)) xs) : + notification.receive_signal st ar s a t n true ⦃ r => + r.2 = a ∧ NtfnStateRel N T (L.mem r.1) (blkWaitA σ (specPtr t) (specPtr n) x xt) ∧ + EpStateRel T E R Bf (L.mem r.1) (blkWaitA σ (specPtr t) (specPtr n) x xt) ∧ + SR r.1 (blkWaitA σ (specPtr t) (specPtr n) x xt) ∧ + WritesIn st s r.1 (WaitFpA (fun σ b => Wr σ s b) σ t n xs) ⦄ := by + have hPn := hP.n + obtain ⟨σ2, hσ2⟩ : ∃ σ2, waitStsA σ (specPtr t) (specPtr n) xt = σ2 := ⟨_, rfl⟩ + obtain ⟨y4, hy4⟩ : ∃ y4, waitNtfnA σ (specPtr t) x xt = y4 := ⟨_, rfl⟩ + have hσ4 : blkWaitA σ (specPtr t) (specPtr n) x xt = updK σ2 (specPtr n) (.notification y4) := by + unfold blkWaitA; rw [hσ2, hy4] + rw [hσ4] + have hxt := hN.at_tcb ht hkt + have hx := hN.at_ntfn hn hk + obtain ⟨hrun, hNQ, hsane, hni⟩ := hsr + have hRB : RelIfBound σ (specPtr t) := hOE.rd _ _ _ _ hSR hkt (Or.inl hrun) + have hnb : ∀ m, xt.tcbState ≠ .blockedOnNotification m := fun m c => by + rw [c] at hrun; simp [runnable] at hrun + have hnm := not_member hN ht hkt hnb + have hnt : specPtr n ≠ specPtr t := fun e => by rw [e, hkt] at hk; cases hk + have htxs : t ∉ xs := fun h => hnm n hn x hk (by rw [hxs]; exact List.mem_map_of_mem h) + have htq := not_queued_codeA hE ht hkt (runnable_codeA hrun) + have hst : stOf (L.mem s) n = obj.NTFN_IDLE ∨ stOf (L.mem s) n = obj.NTFN_WAITING := by + cases h : x.ntfnObj with + | idleNtfn => exact Or.inl (stOf_idle (T := T) (h ▸ hx.obj) rfl) + | waitingNtfn q => exact Or.inr (stOf_waiting (T := T) (h ▸ hx.obj) rfl) + | activeNtfn b => exact absurd h (hna b) + have hBlk : ∀ y ∈ xs, L.mem s (tBlockA y) = n := by + obtain ⟨ys, hQ', hys, hB⟩ := hx.obj.queue + obtain rfl : ys = xs := by + rw [hxs] at hys + exact (List.map_injective_iff.2 (fun a b h => specPtr_inj h) hys).symm + exact fun y hy => (hB y hy).2 + set x1 : Tcb := waitTcbA xt (specPtr n) with hx1 + set σ1 := updK σ (specPtr t) (.tcb x1) with hσ1 + set s2 := L.wr (L.wr s (stA t) sched.TS_BLOCKED_ON_NTFN) (tBlockA t) n with hs2 + obtain ⟨hw1, hoth1⟩ := twr_wr (st := st) hPn ht (K := 3328#u64) (Or.inl rfl) s sched.TS_BLOCKED_ON_NTFN + obtain ⟨hw2, hoth2⟩ := twr_wr (st := st) hPn ht (K := 3336#u64) (Or.inr (Or.inl rfl)) + (L.wr s (stA t) sched.TS_BLOCKED_ON_NTFN) n + rw [← stA_eq] at hw1 hoth1 + rw [← tBlockA_eq] at hw2 hoth2 + have hw := hw1.trans hw2 + have W2 : WritesIn st s s2 (fun b => b = stA t ∨ b = tBlockA t) := + ⟨by simp only [s2, LawfulStore.ksb_wr], by simp only [s2, LawfulStore.cpu_wr], fun b hb hnb' => by + show L.mem (L.wr (L.wr s (stA t) sched.TS_BLOCKED_ON_NTFN) (tBlockA t) n) b = L.mem s b + rw [rd_wr_ne (foot_blk hPn ht).1 hb (Ne.symm fun h => hnb' (Or.inr h)), + rd_wr_ne (foot_st hPn ht).1 hb (Ne.symm fun h => hnb' (Or.inl h))]⟩ + have hN1 : NtfnStateRel N T (L.mem s2) σ1 := by + refine hN.upd_tcb ht _ hw.ntfn hw.tcb hw.links (Or.inl hnm) ⟨?_, ?_, ?_, ?_⟩ + · show (L.mem s2 (stA t)).val = 6 + rw [stA_eq, hoth2 _ (by simp) (by simp) (by simp), ← stA_eq, rd_wr_eq (foot_st hPn ht).1, six_val] + · intro m hm + cases hm + show specPtr (L.mem s2 (tBlockA t)) = specPtr n + rw [rd_wr_eq (foot_blk hPn ht).1] + · show (L.mem s2 (badgeRegA t)).bv = badgeOf xt + rw [badgeRegA_eq, hoth2 _ (by simp) (by simp) (by simp), hoth1 _ (by simp) (by simp) (by simp), + ← badgeRegA_eq]; exact hxt.badge + · show (L.mem s2 (prioA t)).val = xt.tcbPriority.toNat + rw [prioA_eq', hoth2 _ (by simp) (by simp) (by simp), hoth1 _ (by simp) (by simp) (by simp), + ← prioA_eq']; exact hxt.prio + have hSR1 := hH.block _ _ _ _ t hSR hNQ hRB hsane hni hw.foot (NtfnStep.tcb hkt (.blockedOnNotification (specPtr n)) xt.tcbArch) + (fun p x y h1 h2 hp => by + simp only [Function.update_apply, hp, ↓reduceIte] at h2; rw [h1] at h2; cases h2; exact ⟨rfl, id⟩) + -- the endpoint relation across the two words + have hta := hP.e.tcb t ht + have hE1 : EpStateRel T E R Bf (L.mem s2) σ1 := by + have v1 := val_st hta; have v2 := val_blk hta + have hxE := hE.at_tcb ht hkt + have hA : Agree (L.mem s) (L.mem s2) (fun b => EFoot T E R Bf b ∧ ¬ TF t b) := fun b hb hh => by + refine W2.2.2 b hb fun c => ?_ + rcases c with c | c + · exact hh.2 (TF.of_val (a_st hta).2 (by rw [c])) + · exact hh.2 (TF.of_val (a_blk hta).2 (by rw [c])) + refine hE.upd_tcb hP.e ht _ hA (Or.inl htq) ?_ + refine hxE.restateA hta (m' := L.mem s2) (fun b hb hh => ?_) rfl rfl rfl rfl ?_ ?_ ?_ ?_ ?_ + · refine W2.2.2 b hb fun c => ?_ + rcases c with c | c <;> have := congrArg (fun w : U64 => w.val) c <;> have := hh.2 <;> omega + · show (L.mem s2 (stA t)).val = tsCode (.blockedOnNotification (specPtr n)) + rw [stA_eq, hoth2 _ (by simp) (by simp) (by simp), ← stA_eq, rd_wr_eq (foot_st hPn ht).1, six_val] + rfl + · intro e' h; simp [epBlocked] at h + · intro e' d h; cases h + · intro e' r d h; cases h + · intro r h; cases h + have hSE : SR s2 σ1 := hSR1 + have hsts : stsAct σ1 (specPtr t) = σ2 := by rw [← hσ2]; rfl + have hkh2 : σ2.kheap = σ1.kheap := by rw [← hsts, stsAct_kheap] + have hkv : Notif.KView σ1 σ2 := by rw [← hsts]; exact Notif.KView.stsAct _ _ + have hkvE : Endp.KView σ1 σ2 := by rw [← hsts]; exact KView.stsAct' _ _ + have hky : σ2.kheap (specPtr n) = some (.notification x) := by + rw [hkh2, hσ1, updK_kheap, Function.update_of_ne hnt, hk] + have key : (do + let s1 ← obj.Tcb.set_state st t s sched.TS_BLOCKED_ON_NTFN + let s2 ← obj.Tcb.set_block_obj st t s1 n + let s3 ← sched.schedule_tcb st s2 t + let s4 ← notification.ntfn_append st s3 n t + let s5 ← notification.maybe_return_sched_context st s4 n t + ok (s5, a)) ⦃ r => + r.2 = a ∧ NtfnStateRel N T (L.mem r.1) (updK σ2 (specPtr n) (.notification y4)) ∧ + EpStateRel T E R Bf (L.mem r.1) (updK σ2 (specPtr n) (.notification y4)) ∧ + SR r.1 (updK σ2 (specPtr n) (.notification y4)) ∧ + WritesIn st s r.1 (WaitFpA (fun σ b => Wr σ s b) σ t n xs) ⦄ := by + step with obj.Tcb.set_state.spec as ⟨s1', h1'⟩ + step with obj.Tcb.set_block_obj.spec as ⟨s2', h2'⟩ + subst h1' h2' + refine WP.spec_bind (WP.spec_and (WP.spec_and (hOn.scheduleTcb _ σ1 t hN1 hSR1 ht) (hF.scheduleTcb _ σ1 t hN1 hSR1 ht)) + (hOE.scheduleTcb _ σ1 t hE1 hSE ht)) ?_ + rintro s3 ⟨⟨⟨hf3, hS3⟩, hw3⟩, hfE, -⟩ + rw [hsts] at hS3 + have hS3' : SR s3 σ2 := hS3 + have hN3 : NtfnStateRel N T (L.mem s3) σ2 := hN1.frame hPn hf3 hkv + have hE3 : EpStateRel T E R Bf (L.mem s3) σ2 := hE1.frame hP.e hfE hkvE + have hk3 : σ2.kheap (specPtr n) = some (.notification x) := hky + have hkt3 : σ2.kheap (specPtr t) = some (.tcb x1) := by rw [hkh2, hσ1, updK_kheap, Function.update_self] + have hwp : ∀ y ∈ T, y ≠ t → L.mem s2 (prioA y) = L.mem s (prioA y) := fun y hyT hyt => (hw.tcb y hyT hyt).prio + obtain ⟨xs3, hQ3, hxs3, -⟩ := (hN3.at_ntfn hn hk3).obj.queue + have e3 : xs3 = xs := by + rw [hxs] at hxs3 + exact (List.map_injective_iff.2 (fun a b h => specPtr_inj h) hxs3).symm + subst e3 + refine WP.spec_bind (WP.spec_and (block_append hPn s s2 s3 _ ht hn hk3 hkt3 hQ hxs htxs hsorted hwp hf3 hN3) + (ntfn_append_full hPn s3 hn t xs3 hQ3 ht htxs)) ?_ + rintro s4 ⟨⟨hN4, hW4⟩, -, -, hA3⟩ + have hprs : ∀ y ∈ xs3, L.mem s3 (prioA y) = L.mem s (prioA y) := fun y hy => + (hf3.tfields hPn (hQ.mem y hy)).prio.trans (hwp y (hQ.mem y hy) (fun e => htxs (e ▸ hy))) + have hsorted3 : PrioSorted (fun y => L.mem s3 (prioA y)) xs3 := prioSorted_congr hsorted hprs + have hlist := specIns_map (σ2 := σ2) hsorted3 (hN3.at_tcb ht hkt3).prio + (fun y hy => by + obtain ⟨z, hz, hzr⟩ := hN3.tcb y (hQ.mem y hy) + exact ⟨z, hz, hzr.prio⟩) + have hpo : ∀ y ∈ xs3, prioOf σ2 (specPtr y) = prioOf σ (specPtr y) := by + intro y hy + have hne : specPtr y ≠ specPtr t := fun e => htxs (specPtr_inj e ▸ hy) + have : σ2.kheap (specPtr y) = σ.kheap (specPtr y) := by + rw [hkh2, hσ1, updK_kheap, Function.update_of_ne hne] + simp only [prioOf, Fl4Spec.optMapSome, Fl4Spec.optMap, tcbsOf, tcbsOfKh, this] + have hspec : specIns (ntfnQueueOf x) (prioOf σ) xt.tcbPriority (specPtr t) = + specIns (ntfnQueueOf x) (prioOf σ2) xt.tcbPriority (specPtr t) := by + have hq' : ntfnQueueOf x = xs3.map specPtr := hxs + have hc : ∀ z ∈ ntfnQueueOf x, + (fun y => @decide (prioOf σ y ≥ xt.tcbPriority) (Classical.propDecidable _)) z = + (fun y => @decide (prioOf σ2 y ≥ xt.tcbPriority) + (Classical.propDecidable _)) z := by + intro z hz + rw [hq'] at hz + obtain ⟨y, hy, rfl⟩ := List.mem_map.1 hz + exact decide_eq_decide.2 (by rw [hpo y hy]) + unfold specIns + rw [takeWhile_congr' hc, dropWhile_congr' hc] + have hN4' : NtfnStateRel N T (L.mem s4) (updK σ2 (specPtr n) (.notification y4)) := by + obtain ⟨l4, hl4⟩ : ∃ l : List ObjRef, + l = (insPrio (fun y => L.mem s3 (prioA y)) (L.mem s3 (prioA t)) t xs3).map specPtr := ⟨_, rfl⟩ + rw [← hl4] at hN4 hlist + have e4 : y4 = { x with ntfnObj := .waitingNtfn l4 } := by + rw [← hy4] + unfold waitNtfnA + rw [hspec, show ntfnQueueOf x = xs3.map specPtr from hxs, hlist] + rw [e4] + exact hN4 + have hSR4 : SR s4 (updK σ2 (specPtr n) (.notification y4)) := + hH.stable _ _ _ _ hS3' hW4 (NtfnStep.ntfnK (y := y4) hk3 (by rw [← hy4]; rfl) (by rw [← hy4]; rfl)) + (Endp.NoBlockE.ntfnK _ _ _) + have Wa : WritesIn st s3 s4 (fun b => (b = epHeadA n ∨ b = epTSA n) ∨ + ∃ w ∈ t :: xs3, b = nextA .Ep w ∨ b = prevA .Ep w) := + ⟨hA3.foot.1, hA3.foot.2.1, fun b hb hnb' => hA3.agree b hb + (fun w hw => ⟨fun h => hnb' (Or.inr ⟨w, hw, Or.inl h⟩), fun h => hnb' (Or.inr ⟨w, hw, Or.inr h⟩)⟩) + (fun h => hnb' (Or.inl (Or.inl h))) (fun h => hnb' (Or.inl (Or.inr h)))⟩ + have hxT : ∀ w ∈ t :: xs3, w ∈ T := fun w hw => by + rcases List.mem_cons.1 hw with rfl | hw + exacts [ht, hQ.mem w hw] + -- the endpoint relation across `ntfn_append`: no written word is read + have hE4 : EpStateRel T E R Bf (L.mem s4) (updK σ2 (specPtr n) (.notification y4)) := by + have hxq : ∀ w ∈ t :: xs3, ∀ e ∈ E, ∀ z, σ2.kheap (specPtr e) = + some (.endpoint z) → specPtr w ∉ z.epQueue := by + intro w hw e he z hz hm + have hz0 : σ.kheap (specPtr e) = some (.endpoint z) := by + have hne : specPtr e ≠ specPtr t := fun c => by rw [c, hkt3] at hz; cases hz + rwa [hkh2, hσ1, updK_kheap, Function.update_of_ne hne] at hz + rcases List.mem_cons.1 hw with rfl | hw + · exact htq e he z hz0 hm + · obtain ⟨z', hz', hr⟩ := hE.ep e he + rw [hz0] at hz'; cases hz' + have h1 := (hr.blocked hm).2 + rw [hBlk w hw] at h1 + have := hP.ne n hn e he + rw [h1] at this + omega + have off : ∀ b, Aligned b → ¬ (n.val ≤ b.val ∧ b.val < n.val + 40) → + (∀ w ∈ t :: xs3, ¬ TL w b) → L.mem s4 b = L.mem s3 b := fun b hb h1 h2 => by + refine Wa.2.2 b hb ?_ + rintro ((c | c) | ⟨w, hw, c⟩) + · exact h1 (by rw [c, Notif.epHeadA_eq, Notif.nfield_val (hPn.ntfn n hn) (by simp)]; simp) + · exact h1 (by rw [c, Notif.epTSA_eq, Notif.nfield_val (hPn.ntfn n hn) (by simp)]; simp) + · have hwa := hP.e.tcb w (hxT w hw) + rcases c with c | c + · exact h2 w hw (c ▸ (a_next hwa).2) + · exact h2 w hw (c ▸ (a_prev hwa).2) + have offT : ∀ y ∈ T, ∀ b, Aligned b → y.val ≤ b.val → b.val < y.val + 4096 → + (y ∈ t :: xs3 → ¬ TL y b) → L.mem s4 b = L.mem s3 b := by + intro y hy b hb h1 h2 h3 + refine off b hb (fun c => ?_) (fun w hw c => ?_) + · have := hPn.disj n hn y hy; omega + · by_cases e : w = y + · subst e; exact h3 hw c + · have := tcb_disj hP.e (hxT w hw) hy (fun c => e c.symm) + unfold TL at c; omega + have offE : ∀ b, Aligned b → (∃ e ∈ E, b.val = e.val ∨ b.val = e.val + 8) → L.mem s4 b = L.mem s3 b := by + rintro b hb ⟨e, he, c⟩ + refine off b hb (fun d => ?_) (fun w hw d => ?_) + · have := hP.ne n hn e he; omega + · have := hP.e.et e he w (hxT w hw); unfold TL at d; omega + refine ⟨fun e he => ?_, fun r hr => ?_, fun y hy => ?_, fun bp hb k hk' => ?_⟩ + · obtain ⟨z, hz, hr⟩ := hE3.ep e he + have hne : specPtr e ≠ specPtr n := fun c => by rw [c, hk3] at hz; cases hz + refine ⟨z, by rw [updK_kheap, Function.update_of_ne hne]; exact hz, + hr.frame (offE _ (a_head hP.e he).1 ⟨e, he, Or.inl (val_head e)⟩) + (offE _ (a_ts hP.e he).1 ⟨e, he, Or.inr (val_ts hP.e he)⟩) fun y hy hq => ?_⟩ + have hyx : y ∉ t :: xs3 := fun c => hxq y c e he z hz hq + have hya := hP.e.tcb y hy + have k : ∀ b, Aligned b → y.val ≤ b.val → b.val < y.val + 4096 → L.mem s4 b = L.mem s3 b := + fun b hb h1 h2 => offT y hy b hb h1 h2 (fun c => absurd c hyx) + have r1 := val_st hya; have r2 := val_blk hya; have r3 := val_next hya; have r4 := val_prev hya + have := hya.fits + exact ⟨k _ (a_st hya).1 (by omega) (by omega), k _ (a_blk hya).1 (by omega) (by omega), + k _ (a_prev hya).1 (by omega) (by omega), k _ (a_next hya).1 (by omega) (by omega)⟩ + · obtain ⟨z, hz, hz'⟩ := hE3.reply r hr + have hne : specPtr r ≠ specPtr n := fun c => by rw [c, hk3] at hz; cases hz + refine ⟨z, by rw [updK_kheap, Function.update_of_ne hne]; exact hz, ?_⟩ + rw [hz', off _ (a_rtcb hP.e hr).1 (fun c => by + have := hP.nr n hn r hr; rw [Endp.rTcbA_val] at c; omega) (fun w hw c => by + have := hP.e.rt r hr w (hxT w hw); rw [TL, Endp.rTcbA_val] at c; omega)] + · obtain ⟨z, hz, hz'⟩ := hE3.tcb y hy + have hne : specPtr y ≠ specPtr n := fun c => by rw [c, hk3] at hz; cases hz + refine ⟨z, by rw [updK_kheap, Function.update_of_ne hne]; exact hz, + hz'.frame (hP.e.tcb y hy) fun b hb hf => ?_⟩ + have := TF.range hf + exact offT y hy b hb hf.1 (by omega) (fun _ c => TL.not_tf c hf) + · have hbw : BufW Bf (bufW bp.1 k) := ⟨bp, hb, k, hk', rfl⟩ + have e : L.mem s4 (bufW bp.1 k) = L.mem s3 (bufW bp.1 k) := by + refine off _ (hP.e.balign _ hbw) (fun c => ?_) (fun w hw c => ?_) + · have := hP.nb n hn _ hbw; omega + · have := hP.e.bt _ hbw w (hxT w hw) + unfold TL at c; omega + rw [e] + exact hE3.buf bp hb k hk' + -- `maybe_return_sched_context` does nothing + have hsc4 : L.mem s4 (nScA n) = 0#u64 := by + have hk4 : (updK σ2 (specPtr n) (.notification y4)).kheap (specPtr n) = some (.notification y4) := by + rw [updK_kheap, Function.update_self] + have h := (hN4'.at_ntfn hn hk4).sc + have hy4sc : y4.ntfnSc = none := by rw [← hy4]; exact hsc + exact optRef_none (h.symm.trans hy4sc) + have hret : notification.maybe_return_sched_context st s4 n t ⦃ r => r = s4 ⦄ := by + unfold notification.maybe_return_sched_context + step with obj.Ntfn.sc.spec as ⟨i, hi⟩ + rw [hsc4] at hi + subst hi + obtain ⟨b, hb, eb⟩ := spec_existsA (P1.obj.Sc.is_null.spec 0#u64) + rw [hb, bind_ok, eb] + simp + refine WP.spec_bind hret ?_ + rintro s5 rfl + simp only [WP.spec_ok] + refine ⟨trivial, hN4', hE4, hSR4, ?_⟩ + refine ((W2.trans hw3).trans Wa).mono fun b h => ?_ + rcases h with (h | h) | h + · exact Or.inl h + · exact Or.inr (Or.inl (hF.back (ssz_updK_tcb σ (specPtr t) _) W2.1 h)) + · exact Or.inr (Or.inr h) + unfold notification.receive_signal + step with obj.Ntfn.state.spec as ⟨v, hv⟩ + rw [hv, epState_eq] + rcases hst with hst | hst + · simp only [hst, ↓reduceIte] + exact key + · simp only [hst, ntfn_consts.1, ntfn_consts.2.1, show (1#u64 : U64) ≠ 0#u64 by decide, ↓reduceIte] + exact key + +/-- The spec's blocking `receive_signal` on a notification without an SC has this one outcome. -/ +theorem blkWait_specA {σ : State Unit} {t n : U64} {x : Notification} {xt : Tcb} + (hk : σ.kheap (specPtr n) = some (.notification x)) (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hna : ∀ b, x.ntfnObj ≠ .activeNtfn b) (hsc : x.ntfnSc = none) + (htq : specPtr t ∉ ntfnQueueOf x) + (hq : ∀ y ∈ ntfnQueueOf x, ∃ z, σ.kheap y = some (.tcb z) ∧ z.tcbPriority = prioOf σ y) + (cb : Badge) (cr : CapRights) : + receiveSignal (Z := Unit) (specPtr t) (.notificationCap (specPtr n) cb cr) true σ = + ({((), blkWaitA σ (specPtr t) (specPtr n) x xt)}, False) := by + have hnt : specPtr n ≠ specPtr t := fun e => by rw [e, hkt] at hk; cases hk + obtain ⟨σ2, hσ2⟩ : ∃ σ2, stsAct (updK σ (specPtr t) + (.tcb { xt with tcbState := .blockedOnNotification (specPtr n) })) (specPtr t) = σ2 := ⟨_, rfl⟩ + obtain ⟨y4, hy4⟩ : ∃ y4, waitNtfnA σ (specPtr t) x xt = y4 := ⟨_, rfl⟩ + have hσ4 : blkWaitA σ (specPtr t) (specPtr n) x xt = updK σ2 (specPtr n) (.notification y4) := by + unfold blkWaitA waitStsA waitTcbA; rw [hσ2, hy4] + have hkh : σ2.kheap = Function.update σ.kheap (specPtr t) + (some (.tcb { xt with tcbState := .blockedOnNotification (specPtr n) })) := by + rw [← hσ2, stsAct_kheap, updK_kheap] + have heq : ∀ y ∈ ntfnQueueOf x, σ2.kheap y = σ.kheap y := fun y hy => by + rw [hkh, Function.update_of_ne (show y ≠ specPtr t from fun e => htq (e ▸ hy))] + have hpo : ∀ y ∈ ntfnQueueOf x, prioOf σ2 y = prioOf σ y := fun y hy => by + simp only [prioOf, Fl4Spec.optMapSome, Fl4Spec.optMap, tcbsOf, tcbsOfKh, heq y hy] + have hq2 : ∀ y ∈ ntfnQueueOf x, ∃ z, σ2.kheap y = some (.tcb z) ∧ z.tcbPriority = prioOf σ2 y := fun y hy => by + obtain ⟨z, hz, e⟩ := hq y hy + exact ⟨z, (heq y hy).trans hz, by rw [hpo y hy]; exact e⟩ + have hl : specIns (ntfnQueueOf x) (prioOf σ2) xt.tcbPriority (specPtr t) = + specIns (ntfnQueueOf x) (prioOf σ) xt.tcbPriority (specPtr t) := by + have hc : ∀ z ∈ ntfnQueueOf x, + (fun y => @decide (prioOf σ2 y ≥ xt.tcbPriority) (Classical.propDecidable _)) z = + (fun y => @decide (prioOf σ y ≥ xt.tcbPriority) (Classical.propDecidable _)) z := fun z hz => by + exact decide_eq_decide.2 (by rw [hpo z hz]) + unfold specIns + rw [takeWhile_congr' hc, dropWhile_congr' hc] + obtain ⟨l, hl'⟩ : ∃ l, specIns (ntfnQueueOf x) (prioOf σ) xt.tcbPriority (specPtr t) = l := ⟨_, rfl⟩ + have ey : y4 = { x with ntfnObj := .waitingNtfn l } := by + rw [← hy4]; unfold waitNtfnA; rw [hl'] + rw [block_spec_eq hk hkt hna hσ2 hq2 (l := l) (hl.trans hl') cb cr, hσ4, ← ey] + have h1 : (updK σ2 (specPtr n) (.notification y4)).kheap (specPtr n) = some (.notification y4) := by + rw [updK_kheap, Function.update_self] + have h2 : (updK σ2 (specPtr n) (.notification y4)).kheap (specPtr t) = + some (.tcb { xt with tcbState := .blockedOnNotification (specPtr n) }) := by + rw [updK_kheap, Function.update_of_ne (Ne.symm hnt), hkh, Function.update_self] + have hysc : y4.ntfnSc = none := by rw [ey]; exact hsc + exact maybeReturnSc_noopA h1 h2 (fun c => c.2 hysc) + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3RecvWaitDonA.lean b/proofs/Fl4ProofsA64/P3/Stage3RecvWaitDonA.lean new file mode 100644 index 000000000..bb0d874b1 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3RecvWaitDonA.lean @@ -0,0 +1,460 @@ +/- +P3 (aarch64, A64-P3 Stage 3, RecvPreA line): **`Wait` / `NBWait` on an active notification by a passive caller**: the +badge and the notification write, then FL4's tail may donate the notification's SC to the caller +(`recv_tail_frameA`); the record by `JStage3A.of_recvDonA`. + +* **`donTail_curKeepA`**: across the spec's receive tail the budget scalars and the current SC's object are kept, when + the current SC is not the notification's. +* **`RecvPreA.waitActiveDonA`** (any `blocking`): the notification's SC is not the current SC (it may be absent, and + then nothing is donated). The post-state is the one of `specRecvTail`. + +Hypotheses beyond `waitActiveA`'s: `hpas` (the caller is passive), [S] `hne` (a scope selector: `ntfnSc = curSc` is +reachable and belongs to the scope table), and `hdl` (`DonLocalA`, the notification's +SC is on this core). [P] as `waitActiveA`. New module. +-/ +import Fl4ProofsA64.P3.Stage3RecvActiveA +import Fl4ProofsA64.P3.Stage3RecvDonA +import Fl4ProofsA64.P3.Stage3RecvDonSpecA +import Fl4ProofsA64.P3.Stage3RecvDonFrameA +import Fl4ProofsA64.P3.Stage3IpcWakeA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec Classical Fl4ProofsA64.P3 + +noncomputable section + +/-- The spec's sporadic check keeps the scalars and the current SC's object. -/ +theorem ifSporadicTest_curKeepA (o : Option ObjRef) {σ : State Unit} {r : Unit × State Unit} + (hr : r ∈ (ifCondRefillUnblockCheck (Z := Unit) o (some false) (some false) σ).1) : + BScalA σ r.2 ∧ r.2.kheap σ.curSc = σ.kheap σ.curSc := by + have refl : r = ((), σ) → BScalA σ r.2 ∧ r.2.kheap σ.curSc = σ.kheap σ.curSc := fun e => by + rw [e]; exact ⟨BScalA.refl σ, rfl⟩ + unfold ifCondRefillUnblockCheck at hr + cases o with + | none => exact refl hr + | some p => + simp only [NonDet.maybeM] at hr + obtain ⟨⟨sc, σ1⟩, h1, hr⟩ := mem_bindA.1 hr + have e1 : σ1 = σ := (getSchedContext_mem h1).1 + rw [e1] at hr + dsimp only at hr + rw [nd_gets_bindSc, nd_pure_bindSc] at hr + try dsimp only at hr + unfold NonDet.when at hr + split at hr + · rename_i c + have ne : p ≠ σ.curSc := c.2 rfl + have h : PresR (OnlyScA p) (NonDet.when (some false = some true → p ≠ σ.curSc) + (refillUnblockCheck (Z := Unit) p)) := PresR.when refillUnblockCheck_onlyA + unfold NonDet.when at h + have K := h σ r hr + exact ⟨K.toBScalA, K.other _ (Ne.symm ne)⟩ + · exact refl hr + +/-- **The receive tail keeps the scalars and the current SC's object** (module docstring). -/ +theorem donTail_curKeepA (t np : ObjRef) {σ σ' : State Unit} {y : Notification} + (hkn : σ.kheap np = some (.notification y)) {y0 : SchedContext} {n0 : Nat} + (hc : σ.kheap σ.curSc = some (.schedContext y0 n0)) (hne : ∀ q, y.ntfnSc = some q → q ≠ σ.curSc) + (hr : ((), σ') ∈ (specRecvTailA t np σ).1) : BScalA σ σ' ∧ σ'.kheap σ.curSc = σ.kheap σ.curSc := by + unfold specRecvTailA at hr + obtain ⟨u, hu, hr⟩ := mem_bindA.1 hr + have D := maybeDonateSc_keepScA t np σ.curSc hkn hne hc hu + obtain ⟨o, -, hr⟩ := eqPres_mem (threadGet_eqPres _ t) hr + have K := ifSporadicTest_curKeepA o hr + have e : u.2.curSc = σ.curSc := D.1.csc + rw [e] at K + exact ⟨D.1.trans K.1, K.2.trans D.2⟩ + +end + +end Fl4ProofsA64.P3.Sched + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4Spec Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-- The two ports of the receive tail are one program. Stated between the functions, so that the kernel never +compares them applied to a state. -/ +theorem specRecvTail_eqA : specRecvTail = specRecvTailA := rfl + +section + +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {s : S} {a : A} {σ : State Unit} {m : McState} + +set_option maxHeartbeats 4000000 in +/-- **`Wait` / `NBWait` on an active notification, the caller passive** (module docstring). -/ +theorem RecvPreA.waitActiveDonA + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p badge slot : U64} {send canReply blk : Bool} + (hcap : cspace.lookup_cap st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.CAP)) = + ok (.Ok (.Notification p badge send true))) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hsc : capA st s slot = .Notification p badge send true) + (hp : p ∈ N) {x : Notification} (hk : σ.kheap (specPtr p) = some (.notification x)) + {b0 : Badge} (hact : x.ntfnObj = .activeNtfn b0) + (hbt : x.ntfnBoundTcb = some σ.curThread ∨ x.ntfnBoundTcb = none) + (hpas : ∀ y, σ.kheap σ.curThread = some (.tcb y) → y.tcbSchedContext = none) + (hne : x.ntfnSc ≠ some σ.curSc) (hdl : DonLocalA (st := st) Z.C s p) : + HRecvPostA st AR ar hPl.ipa n Z.Cs s a m blk canReply σ + (fun σ' => ∃ y, σ.kheap σ.curThread = some (.tcb y) ∧ + ((), σ') ∈ (specRecvTail σ.curThread (specPtr p) (rsPreσA σ σ.curThread (specPtr p) y x b0)).1) := by + have htZ : L.curW s ∈ Z.T := J.sr.curT + set t := L.curW s with ht_def + have ct : σ.curThread = specPtr t := J.j.1.sch.curThread + obtain ⟨xt, hkt, r⟩ := J.ep.tcb t htZ + have hkc : σ.kheap σ.curThread = some (.tcb xt) := by rw [ct]; exact hkt + have hthT : Z.th t = some .tcb := (J.sr.thT t).2 htZ + have hP := J.iplaced + have hx := J.ntfn.at_ntfn hp hk + set v := L.mem s (nBadgeA p) with hv + have hb0 : v.bv = b0 := by + have := hx.obj; rw [hact] at this; exact this.2.2 + set s1 := L.wr s (Endp.regA t layout.reg.BADGE) v with hs1 + set s2 := rsPreA st s t p with hs2 + set σ1 := updK σ (specPtr t) (.tcb (setReg xt badge_register b0)) with hσ1 + set σ2 := rsPreσA σ (specPtr t) (specPtr p) xt x b0 with hσ2 + have hta := J.sr.core.tcbs t htZ + have hbn : badgeRegA t = Endp.regA t layout.reg.BADGE := by + apply UScalar.eq_of_val_eq + have := hta.fits + have h0 : (layout.reg.BADGE).val = 0 := by simp [layout.reg.BADGE, layout.reg.X0] + rw [badgeRegA_eq, Endp.regA_val (hP.e.tcb t htZ) (by rw [h0]; decide), h0, wa_valm, + Nat.mod_eq_of_lt (by simp; omega)] + simp + obtain ⟨e1, e2, hR2, hW2, -⟩ := recv_active_prefix (st := st) hP.n J.ntfn htZ hp hk hact hkt + -- the FL4 writes + have hs2' : s2 = L.wr s1 (epTSA p) ((L.mem s1 (epTSA p) &&& ~~~3#u64) ||| (obj.NTFN_IDLE &&& 3#u64)) := by + rw [hs2, rsPreA, hbn] + have hset : obj.Ntfn.set_state st p s1 obj.NTFN_IDLE = ok s2 := by + obtain ⟨q, hq, e⟩ := spec_existsA (obj.Ntfn.set_state.spec (st := st) s1 p obj.NTFN_IDLE) + rw [hq, e, ← hs2'] + have hbv : (Endp.regA t layout.reg.BADGE).val = t.val + 256 := by + have h0 : (layout.reg.BADGE).val = 0 := by simp [layout.reg.BADGE, layout.reg.X0] + rw [Endp.regA_val (hP.e.tcb t htZ) (by rw [h0]; decide), h0] + have hbal : Aligned (Endp.regA t layout.reg.BADGE) := by + have e : Endp.regA t layout.reg.BADGE = wa t 256#u64 := by + rw [← hbn, badgeRegA_eq] + rw [e]; exact field_aligned hta (by simp) (by simp) + have htsal : Aligned (epTSA p) := (foot_ts hP.n hp).1 + have htsv : p.val ≤ (epTSA p).val ∧ (epTSA p).val < p.val + 40 := by + rw [epTSA_eq, nfield_val (hP.n.ntfn p hp) (by simp)] + simp + have w1 : WritesIn st s s1 (fun b => b = Endp.regA t layout.reg.BADGE) := writesIn_wr hbal + have w2 : WritesIn st s1 s2 (fun b => b = epTSA p) := by + rw [hs2']; exact writesIn_wr htsal + have w : WritesIn st s s2 (fun b => b = Endp.regA t layout.reg.BADGE ∨ b = epTSA p) := + w1.trans w2 + have hfit := hta.fits + have htv : ∀ O : U64, O.val < 4096 → (wa t O).val = t.val + O.val := fun O hO => by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have tcbKeep : ∀ O : U64, O.val < 4096 → O.val % 8 = 0 → O.val ≠ 256 → L.mem s2 (wa t O) = L.mem s (wa t O) := by + intro O h1 h2 h3 + refine w.mem_eq (field_aligned hta h1 h2) fun c => ?_ + rcases c with c | c <;> have := congrArg (fun w : U64 => w.val) c <;> simp only [htv O h1] at this + · rw [hbv] at this; omega + · have := hP.n.disj p hp t htZ; omega + have hsc0 : L.mem s2 (scA t) = L.mem s (scA t) := by + rw [scA_eq]; exact tcbKeep _ (by simp) (by simp) (by simp) + have htH : t ∈ H.tcbs := by rw [J.tcbs]; exact htZ + have hH : H p = some .ntfn := J.utIpc.ntfn p hp + have tp : specPtr t ≠ specPtr p := fun e => by rw [e, hk] at hkt; cases hkt + obtain ⟨y, k, hy⟩ := J.sinv.curSc + have sct : σ.curSc ≠ specPtr t := fun e => by rw [e, hkt] at hy; cases hy + have scp : σ.curSc ≠ specPtr p := fun e => by rw [e, hk] at hy; cases hy + have hy2 : σ2.kheap σ.curSc = some (.schedContext y k) := by + show Function.update (Function.update σ.kheap _ _) _ _ σ.curSc = _ + rw [Function.update_of_ne scp, Function.update_of_ne sct]; exact hy + have ht2 : σ2.kheap (specPtr t) = some (.tcb (setReg xt badge_register b0)) := by + show Function.update (Function.update σ.kheap _ _) _ _ (specPtr t) = _ + rw [Function.update_of_ne tp, Function.update_self] + -- the record at the badge write, and across the notification write + have hc : m.curCore < fl4A64Platform.numNodes := by rw [J.j.1.sch.core]; exact hCs _ J.j.1.sch.cpuCs + obtain ⟨Q, Rl, J1⟩ := J.toJStage3A.of_badgeSwA hA hPl rfl hc hmb hmax htu J.body.sw J.noSelf htZ hkt v + rw [hb0, ← hs1] at J1 + have hk1 : σ1.kheap (specPtr p) = some (.notification x) := by + show Function.update σ.kheap _ _ (specPtr p) = _ + rw [Function.update_of_ne (Ne.symm tp)]; exact hk + have rv : RunVcpuInv st Z.T s2 := fun u hu hne => by + have keep : ∀ O : U64, O.val < 4096 → O.val % 8 = 0 → L.mem s2 (wa u O) = L.mem s1 (wa u O) := fun O h1 h2 => by + have hua := J.sr.core.tcbs u hu + have := hua.fits + refine w2.mem_eq (field_aligned hua h1 h2) fun c => ?_ + have e := congrArg (fun w : U64 => w.val) c + have ev : (wa u O).val = u.val + O.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have := hP.n.disj p hp u hu + simp only [ev] at e + omega + have e1' : L.mem s2 (runVcpuA u) = L.mem s1 (runVcpuA u) := keep _ (by simp) (by simp) + have e2' : L.mem s2 (stA u) = L.mem s1 (stA u) := by rw [stA_eq]; exact keep _ (by simp) (by simp) + rw [e2'] + exact J1.runVcpu u hu (e1' ▸ hne) + have R' : NtfnStateRel N Z.T (L.mem s2) σ2 := by + rw [hs2', hs1, ← hbn]; exact hR2 + have wp : WritesIn st s1 s2 (NObjW p) := + w2.mono fun b c => by subst c; exact ⟨htsv.1, by have := htsv.2; unfold NObjW at *; omega⟩ + obtain ⟨⟨Q1, Rl1, sr2u⟩, -, -, -, ep2, -, -, -, sinv2, sidle2, -, sv2⟩ := + J1.ntfnObj_factsA hc hmb hmax htu hp hk1 (fun q c => by rw [hact] at c; cases c) + (x' := ntfnObjUpdate (fun _ => .idleNtfn) x) rfl rfl rfl wp R' + -- FL4's tail + have sr2 : SchedSRA st { { Z with Q := Q, R := Rl } with Q := Q1, R := Rl1 } m κ s2 σ2 := sr2u + have c2 : σ2.curThread = specPtr t := ct + have hcw : L.curW s2 = t := specPtr_inj (sr2.sch.curThread.symm.trans c2) + have hon : L.mem s2 (affA t) = L.cpu s2 := by + have := sr2.curOn; rwa [hcw] at this + have hrun2 : ∀ z, σ2.kheap (specPtr t) = some (.tcb z) → runnable z.tcbState = true := fun z hz => by + cases KernelObject.tcb.inj (Option.some.inj (ht2.symm.trans hz)) + rw [setReg_state] + rcases J.state hkt with e | e <;> rw [e] <;> rfl + have vS : (nScSc p).val = p.val + 32 := by + show (Notif.nScA p).val = _ + rw [nScA_eq, nfield_val (hP.n.ntfn p hp) (by simp)]; simp + have hnw : L.mem s2 (nScSc p) = L.mem s (nScSc p) := by + refine w.mem_eq (foot_sc hP.n hp).1 fun c => ?_ + rcases c with c | c + · have e := congrArg (fun w : U64 => w.val) c + simp only [vS, hbv] at e + have := hP.n.disj p hp t htZ + omega + · have e := congrArg (fun w : U64 => w.val) c + simp only [vS, epTSA_eq, nfield_val (hP.n.ntfn p hp) (K := 8#u64) (by simp)] at e + simp at e + have T := recv_tail_frameA (st := st) (ar := ar) sr2 a (hmb a) (t := t) (n := p) htZ hon + (notifScWord hp R' sr2.inv hc) (fun u hu z hz => (ep2.at_tcb hu hz).st) + (fun d pr => by rw [← ct]; exact J.noQ d pr) (by rw [← ct]; exact J.notRel) hrun2 + (J.sr.donLocalKeepA sr2 rfl hnw hdl) + obtain ⟨⟨s3, a3⟩, hq3, ha3, σ3, R3, htail, sr3, F3⟩ := spec_existsA T + dsimp only at ha3 sr3 F3 + subst a3 + have hsig : notification.receive_signal st ar s a t p blk = ok (s3, a) := by + have h : notification.receive_signal st ar s a t p blk = rustRecvTailA st ar s2 a t p := by + have hsa : stOf (L.mem s) p = obj.NTFN_ACTIVE := stOf_active hx.obj hact + unfold notification.receive_signal + obtain ⟨st0, e0, hst0⟩ := spec_existsA (obj.Ntfn.state.spec (st := st) s p) + rw [epState_eq, hsa] at hst0 + rw [e0, bind_ok, hst0] + rw [if_neg (by rw [ntfn_consts.1, ntfn_consts.2.2]; decide), + if_neg (by rw [ntfn_consts.2.1, ntfn_consts.2.2]; decide)] + obtain ⟨b, eb, hb⟩ := spec_existsA (obj.Ntfn.badge.spec (st := st) s p) + obtain ⟨q, eq1, hq1⟩ := spec_existsA (obj.Tcb.set_reg.spec (st := st) s t layout.reg.BADGE b) + rw [eb, bind_ok, eq1, bind_ok, hq1, hb, ← hv, ← hs1, hset, bind_ok] + rfl + rw [h, hq3] + -- the bound-thread check + have hb : L.mem s (Notif.nBoundA p) = t ∨ L.mem s (Notif.nBoundA p) = 0#u64 := by + by_cases h0 : L.mem s (Notif.nBoundA p) = 0#u64 + · exact Or.inr h0 + · rcases hbt with h | h + · rw [hx.bound, optRef, if_neg h0, ct] at h + exact Or.inl (specPtr_inj (Option.some.inj h)) + · rw [hx.bound, optRef, if_neg h0] at h + cases h + have hfl : syscall.handle_recv st ar s a blk canReply = ok (s3, a) := by + unfold syscall.handle_recv + rw [cur_thread_ok, bind_ok, tcb_reg_eqA, bind_ok] + rw [show L.curW s = t from rfl, hcap] + simp only [bind_ok, ↓reduceIte] + obtain ⟨v', hv', e⟩ := spec_existsA (Notif.obj.Ntfn.bound_tcb.spec (st := st) s p) + rw [hv', bind_ok, e] + rcases hb with h | h + · simp only [h, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte, ite_self] + exact hsig + · simp only [h, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + exact hsig + -- the spec + have hm3 : ((), σ3) ∈ (specRecvTailA (specPtr t) (specPtr p) σ2).1 := by + rw [htail] + exact Set.mem_singleton _ + have hrecv : ((), σ3) ∈ (receiveSignal (Z := Unit) (specPtr t) + (absCapA (.Notification p badge send true)) blk σ).1 := by + have eq : receiveSignal (Z := Unit) (specPtr t) (.notificationCap (specPtr p) badge.bv + (rightsOfA send true false false)) blk σ = ({((), σ3)}, False) := by + rw [receiveSignal_cap hk] + simp only [hact] + rw [nd_bind_one e1, nd_bind_one e2] + rw [specRecvTail_eqA] + exact htail + rw [show absCapA (.Notification p badge send true) = + .notificationCap (specPtr p) badge.bv (rightsOfA send true false false) from rfl, eq] + exact Set.mem_singleton _ + have hmem : ((), σ3) ∈ (handleRecv (Z := Unit) blk canReply σ).1 := by + have capReg : (L.mem s (regWA t layout.reg.CAP)).bv = xt.tcbArch.tcbContext.userRegs cap_register := by + rw [← r.regs cap_register, regIdxA_capA]; rfl + have capRead : NonDet.liftM dataToCptr (asUser (Z := Unit) (specPtr t) (getRegister cap_register)) σ = + ({(toBl (L.mem s (regWA t layout.reg.CAP)).bv, σ)}, False) := by + unfold NonDet.liftM + rw [nd_bind_singleA' (asUser_getRegister_eqA hkt _), ← capReg] + rfl + have hc0 : (lookupCap (Z := Unit) (specPtr t) (toBl (L.mem s (regWA t layout.reg.CAP)).bv)).run σ = + ({(.ok (absCapA (.Notification p badge send true)), σ)}, False) := by + have l := post_of_okA (lookup_cap_recv_refinesA (st := st) _ s σ J.j.1.st J.j.1.cv t hthT _) hcap + obtain ⟨e, he, hrel⟩ := l + cases e with + | error _ => exact hrel.elim + | ok ac => + obtain rfl : absCapA (.Notification p badge send true) = ac := hrel.2 + exact he + have hc1 : (capFaultOnFailure (ofBl (toBl (L.mem s (regWA t layout.reg.CAP)).bv)) true + (lookupCap (Z := Unit) (specPtr t) (toBl (L.mem s (regWA t layout.reg.CAP)).bv))).run σ = + ({(.ok (absCapA (.Notification p badge send true)), σ)}, False) := run_handleE_okA hc0 + rw [handleRecv, nd_gets_bindSc, ct, nd_bind_oneSc capRead] + apply mem_bindA.2 + refine ⟨(.ok (), σ3), ?_, rfl⟩ + rw [run_bind_okA hc1] + dsimp only [absCapA] + have boundRead : (NonDetE.liftE (ε := Fault) + (getNtfnObjRef (Z := Unit) Notification.ntfnBoundTcb (specPtr p))).run σ = + ({(.ok x.ntfnBoundTcb, σ)}, False) := run_liftEA (getNtfnObjRef_eqA hk _) + rw [if_pos (by simp [AllowRecv, rightsOfA]), run_bind_okA boundRead, if_pos (by rwa [ct] at hbt)] + unfold NonDetE.liftE + rw [ExceptT.run_lift] + exact mem_bindA.2 ⟨((), σ3), hrecv, rfl⟩ + -- P1's walk + obtain ⟨Pt, Ub, Pp7, Tb7, Hr, Te, Kt, Lv7, Re, bits, KLv, W⟩ := J7A.unp J.j7 + have I := J.delIn hA hPl W + have h : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s := by + obtain ⟨q, b, x, sg, sc⟩ := J.sys.ent.bxi + exact ⟨q, b, x, sc, sg⟩ + have D := J.decPreA (extra := Aeneas.Std.Array.repeat 3#usize 0#u64) hslot (fun z hz hne => by + exact absurd (by simpa [Aeneas.Std.Array.repeat] using hz) hne) + have regs : RpPost st H Z.Cs OS Z.V ia ie Ac s s3 := by + have walk := Fl4ProofsA64.P1.W7.R.ST.receive_signal.sw (st := st) hA a t p blk I J.core0 J.ko + (SW.start J.sys.sysl J.symL h) D.cb + (by rcases J.runW with e | e | e <;> rw [e] <;> + simp [sched.TS_RUNNING, sched.TS_RESTART, sched.TS_RUNNING_VM, sched.TS_INACTIVE]) + (show p ∈ H.ntfns from hH) hH + (by rw [epState_eq, stOf_active hx.obj hact, ntfn_consts.2.2]; decide) + (J.runVcpu.zero htZ J.nvm) + (fun k hk' _ => by + rw [hH] at hk' + cases hk' + exact ⟨slot, hslot, by rw [hsc]; exact names_ntfn⟩) + exact (post_of_okA (P := fun q => SW st H Z.Cs OS Z.V ia ie Ac s q.1) walk hsig).1.post + obtain ⟨bxi, sysl, symL, userOK, j7⟩ := J.finRpA hA hPl rfl regs σ3 + -- the spec invariants and the record at the end + have eT : donTailA (specPtr t) (specPtr p) (fun o => ifSporadicCurScTestRefillUnblockCheck (Z := Unit) o) = + specRecvTailA (specPtr t) (specPtr p) := rfl + have hm3' : ((), σ3) ∈ (donTailA (specPtr t) (specPtr p) + (fun o => ifSporadicCurScTestRefillUnblockCheck o) σ2).1 := by rw [eT]; exact hm3 + have hnt : specPtr t ≠ σ.idleThread := J.notIdle + have hnr2 : specPtr t ∉ σ2.releaseQueue := by rw [← ct]; exact J.notRel + have hk2 : σ2.kheap (specPtr p) = some (.notification (ntfnObjUpdate (fun _ => .idleNtfn) x)) := by + show Function.update (Function.update σ.kheap _ _) _ _ (specPtr p) = _ + rw [Function.update_self] + have sact2 : SActA σ2 := by + refine ⟨fun u e => ⟨fun h => J.noSelf (h ▸ e), fun h => ?_⟩, + ⟨_, by rw [show σ2.curThread = specPtr t from c2]; exact ht2, ?_⟩⟩ + · have e' : σ.schedulerAction = .switchThread u := e + obtain ⟨-, -, x0, e0, hr0⟩ := schedulable_actA (J.body.sw u e') + obtain ⟨z, hz, hs⟩ := J.sinv.idleTcb + rw [show u = σ.idleThread from h, hz] at e0 + cases e0 + rw [hs] at hr0 + cases hr0 + · rcases J.state hkt with e | e + · exact Or.inl e + · exact Or.inr (Or.inl e) + have hkT : ∀ o, PresR RefK (ifSporadicCurScTestRefillUnblockCheck (Z := Unit) o) := fun o => + ifCondRefillUnblockCheck_refK o _ _ + have hkP : ∀ o, TPres (ifSporadicCurScTestRefillUnblockCheck (Z := Unit) o) := fun o => by + unfold ifSporadicCurScTestRefillUnblockCheck ifCondRefillUnblockCheck + cases o <;> simp only [NonDet.maybeM] <;> tpres + have L2 : LoopInvA σ2 m n := ⟨sinv2, sidle2, sact2⟩ + have hnt2 : specPtr t ≠ σ2.idleThread := hnt + have hn2 : ∀ sc, x.ntfnSc = some sc → sc ≠ idleScPtr := fun sc h e => by subst e; exact J.sr.inv.ntfnIdle (specPtr p) x hk h + have hs2 : x.ntfnSc = some σ2.curSc → ∀ y k, σ2.kheap σ2.curSc = some (.schedContext y k) → scActive y = true → refillSufficient 0 (refillHd y) = true := fun h => absurd h hne + have hL : LoopInvA σ3 m n := donTail_loopA (σ := σ2) (specPtr t) (specPtr p) hkT L2 hnt2 hk2 hn2 hnr2 hs2 hm3' + have rq2 : RqDomPrio σ2 := fun f d pr z hz hm => by + have h0 := J.toJStage3A.rqDomPrio + change Function.update (Function.update σ.kheap (specPtr t) _) (specPtr p) _ f = _ at hz + by_cases e1 : f = specPtr p + · rw [e1, Function.update_self] at hz; cases hz + rw [Function.update_of_ne e1] at hz + by_cases e2 : f = specPtr t + · rw [e2, Function.update_self] at hz + cases hz + exact h0 f d pr xt (by rw [e2]; exact hkt) hm + · rw [Function.update_of_ne e2] at hz + exact h0 f d pr z hz hm + have hts : BlockedTsA xt.tcbState = false := by + rcases J.state hkt with e | e <;> rw [e] <;> rfl + have F2 : FIA σ2 := SchedValidA.toFIA sv2 + have hst2 : StIs (specPtr t) xt.tcbState σ2 := + fun z hz => by cases KernelObject.tcb.inj (Option.some.inj (ht2.symm.trans hz)); rfl + have fia : FIA σ3 := donTail_fiaA (σ := σ2) (σ' := σ3) (specPtr t) (specPtr p) hkP F2 rq2 hts hst2 hm3' + have sv3 : SchedValidA σ3 := SchedValidB.toA fia.1 fia.2 + have SF : SchedFrameA σ2 σ3 := by + have P : PresR SchedFrameA (specRecvTailA (specPtr t) (specPtr p)) := by + unfold specRecvTailA ifSporadicCurScTestRefillUnblockCheck ifCondRefillUnblockCheck; spres + exact P σ2 _ hm3 + have Qk : QKeepA σ2 σ3 := by + have P : PresR QKeepA (specRecvTailA (specPtr t) (specPtr p)) := by + unfold specRecvTailA ifSporadicCurScTestRefillUnblockCheck ifCondRefillUnblockCheck; qpres + exact P σ2 _ hm3 + have Wk : WkKeepA σ2 σ3 := by + have P : PresR WkKeepA (specRecvTailA (specPtr t) (specPtr p)) := by + unfold specRecvTailA ifSporadicCurScTestRefillUnblockCheck ifCondRefillUnblockCheck; wkpres + exact P σ2 _ hm3 + have J3 := J1.of_recvDonA hc hmb hmax htu hp hk1 (fun q c => by rw [hact] at c; cases c) + (x' := ntfnObjUpdate (fun _ => .idleNtfn) x) rfl rfl rfl wp R' F3 sr3 SF Qk Wk hL sv3 rv bxi sysl symL + userOK j7 + -- the body end + have hpas2 := J.passive_noResumeA hpas + have B2 : BodyOKA σ2 m := by + refine J.body.stepEq (σ' := σ2) ((J.swBadgeA hkt b0).updOther (p := specPtr p) + (by rw [updK_kheap, Function.update_of_ne (Ne.symm tp)]; exact hk) (fun _ c => by cases c) + (fun _ _ c => by cases c)) rfl rfl rfl rfl rfl rfl (fun y' k' hz => ?_) (fun h => absurd h hpas2) + rw [hy2] at hz + cases hz + exact ⟨y, hy, rfl, rfl, rfl⟩ + have swn : SwN (specPtr t) σ2 := fun u e => by + refine ⟨B2.sw u e, fun h => J.noSelf ?_⟩ + rw [ct, ← h]; exact e + have keep := donTail_curKeepA (specPtr t) (specPtr p) hk2 (y0 := y) (n0 := k) hy2 + (fun q h e => hne (h.trans (congrArg some e))) hm3 + have hown : ∀ y0 k0, σ.kheap σ.curSc = some (.schedContext y0 k0) → ∀ o, y0.scTcb = some o → o ≠ specPtr t := by + intro y0 k0 h0 o ho e + have hcs : σ.curSc = scRenA m (L.mem s (L.nodeAt s (L.cpu s) layout.node.CUR_SC)) := by + rw [← J.sr.sch.csc, J.sr.sch.core]; exact J.sr.sch.curScOfW _ J.sr.sch.cpuCs + obtain ⟨z, hz, hzs⟩ := J.toJStage3A.scBoundA J.sr.cscC (y := y0) (k := k0) (o := o) (by rw [← hcs]; exact h0) ho + rw [hpas z (by rw [ct, ← e]; exact hz)] at hzs + cases hzs + have B3 : BodyOKA σ3 m := by + refine B2.step (σ' := σ3) (donTail_swA (specPtr t) (specPtr p) hkP swn hm3') keep.1.time + (by rw [keep.1.ms]) keep.1.ct keep.1.csc keep.1.used (fun y0 k0 h0 o ho hm => ?_) (fun y' k' hz => ?_) + (fun h => absurd (keep.1.act h) hpas2) (fun h => absurd (keep.1.act h) hpas2) + · have h0' : σ.kheap σ.curSc = some (.schedContext y0 k0) := by + rw [show σ2.curSc = σ.curSc from rfl, hy2] at h0 + rw [hy]; exact h0 + by_contra c + exact donTail_notRelA (c := o) (t := specPtr t) (Ne.symm (hown y0 k0 h0' o ho)) (specPtr p) hkP c hm3' hm + · have k2 : σ3.kheap σ.curSc = σ2.kheap σ.curSc := keep.2 + rw [show σ2.curSc = σ.curSc from rfl] at hz ⊢ + rw [k2] at hz + exact ⟨y', hz, rfl, rfl, Or.inl rfl⟩ + have hOut : ∃ y, σ.kheap σ.curThread = some (.tcb y) ∧ + ((), σ3) ∈ (specRecvTail σ.curThread (specPtr p) (rsPreσA σ σ.curThread (specPtr p) y x b0)).1 := by + have e : rsPreσA σ σ.curThread (specPtr p) xt x b0 = σ2 := by rw [ct] + refine ⟨xt, hkc, ?_⟩ + rw [e, ct, specRecvTail_eqA] + exact hm3 + have hRel := A64RelS3.ofJ J3 B3 + have hcw := curW_keptA J.toJStage3A J3 keep.1.ct + exact ⟨s3, a, σ3, hfl, hmem, hOut, hRel, hcw, B3⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3RecvWaitScA.lean b/proofs/Fl4ProofsA64/P3/Stage3RecvWaitScA.lean new file mode 100644 index 000000000..7bf723560 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3RecvWaitScA.lean @@ -0,0 +1,669 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2c): **blocking `Wait` on a notification whose SC is the waiter's, at the record** +(generated by `handoff/tools/gen_lift2c_waitsc_rec.py` from `JSyscallA.waitBlockA`): `handle_recv` with +`blocking = true` on an idle or waiting notification whose SC `sc` is the waiter's. The caller is queued as in +`waitBlockA`, then `maybe_return_sched_context` unbinds the caller and `sc` (the passive server returns its SC) and +`reschedule_required` leaves `choose_new_thread` (`blkWaitScA`). The transport across the unbinding is +`Stage3RecvWaitUnbA` (`SIKA` with donation, `SpecSchedA`, `QKeepA`, `SchedValidA`, `BodyOKA.ofEntryUnbA`). + +* **`RecvPreA.waitBlockScA`**: `HRecvPostA`, a switch to another thread possibly pending: `reschedule_required` + then enqueues the pending target (`preschedA`, `Stage3PreSchedA`), and the post is at that state. +* **`JSyscallA.waitBlockScA`**: its syscall-entry form (no switch pending, post `blkWaitScA`). + +[S] the notification idle or waiting, bound to the caller or to no thread, with SC `sc`, the caller's SC; +`numNodes = 1`. [P] as `waitBlockA`. New module. +-/ +import Fl4ProofsA64.P3.Stage3RecvWaitA +import Fl4ProofsA64.P3.Stage3RecvWaitScBodyA +import Fl4ProofsA64.P3.Stage3PreSchedA +import Fl4ProofsA64.P3.Stage3ReplyMidA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4Spec Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section + +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {J5 : JSlotA S} {R : Set U64 → JSlotA S} {s s' : S} {a : A} {σ σ' : State Unit} + {m : McState} + +set_option maxHeartbeats 4000000 in +/-- **Blocking `Wait` on a notification whose SC is the waiter's** (module docstring). -/ +theorem RecvPreA.waitBlockScA + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : RecvPreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p badge slot : U64} {send canReply : Bool} + (hcap : cspace.lookup_cap st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.CAP)) = + ok (.Ok (.Notification p badge send true))) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hsc : capA st s slot = .Notification p badge send true) + (hp : p ∈ N) {x : Notification} (hk : σ.kheap (specPtr p) = some (.notification x)) + (hna : ∀ b, x.ntfnObj ≠ .activeNtfn b) {sc : ObjRef} (hsn : x.ntfnSc = some sc) + (hsct : ∀ y, σ.kheap σ.curThread = some (.tcb y) → y.tcbSchedContext = some sc) + (hbt : x.ntfnBoundTcb = some σ.curThread ∨ x.ntfnBoundTcb = none) : + HRecvPostA st AR ar hPl.ipa n Z.Cs s a m true canReply σ + (fun σ' => ∃ y xs ns, σ.kheap σ.curThread = some (.tcb y) ∧ σ.kheap sc = some (.schedContext xs ns) ∧ + σ' = unbWaitA (preschedA (blkWaitA σ σ.curThread (specPtr p) x y)) σ.curThread sc xs ns + (waitTcbA y (specPtr p))) := by + have htZ : L.curW s ∈ Z.T := J.sr.curT + set t := L.curW s with ht_def + have ct : σ.curThread = specPtr t := J.j.1.sch.curThread + obtain ⟨xt, hkt, r⟩ := J.ep.tcb t htZ + have hkc : σ.kheap σ.curThread = some (.tcb xt) := by rw [ct]; exact hkt + have hnvm : L.mem s (stA t) ≠ sched.TS_RUNNING_VM := J.nvm + have hrunS : xt.tcbState = .running ∨ xt.tcbState = .restart := J.state hkt + have hr : runnable xt.tcbState = true := by rcases hrunS with e | e <;> rw [e] <;> rfl + have hthT : Z.th t = some .tcb := (J.sr.thT t).2 htZ + have hidle : specPtr t ≠ σ.idleThread := by + obtain ⟨y, hy, hs⟩ := J.sinv.idleTcb + intro h + rw [← h, hkt] at hy + cases hy + rcases hrunS with e | e <;> rw [e] at hs <;> cases hs + have hRun : SndRunA σ (specPtr t) xt := J.sndRun hkt + have hsca : ∀ sc, xt.tcbSchedContext = some sc → ScActiveAtA σ sc := fun sc hs => J.scAct xt sc hkc hs + have hcm : m.curCore < fl4A64Platform.numNodes := by rw [J.j.1.sch.core]; exact hCs _ J.j.1.sch.cpuCs + have hSOps := ipcSchedOpsA (st := st) (ar := ar) (κ := κ) J.iplaced J.ipcPlace hcm hmb hmax htu + have hF : SchedFootNA st ar N Z.T (IpcSchedRelA st Z m κ) (DonLocalA (st := st) Z.C) + (fun σ s => MWrA (st := st) Z m σ s) := schedFootNA_of_sched J.iplaced.n hcm hmb J.ipcPlace.notification + have hSR : IpcSchedRelA st Z m κ s σ := ⟨Z.Q, Z.R, J.sr⟩ + have hx := J.ntfn.at_ntfn hp hk + obtain ⟨xs, hQ, hxs, hbq⟩ := hx.obj.queue + have hxsT : ∀ y ∈ xs, y ∈ Z.T := fun y hy => hQ.mem y hy + have hsrt := J.ipcSorted.2 (specPtr p) ⟨p, hp, rfl⟩ x hk + have hsorted : PrioSorted (fun y => L.mem s (prioA y)) xs := by + unfold PrioSortedA at hsrt + rw [hxs, List.pairwise_map] at hsrt + refine hsrt.imp_of_mem fun {a b} ha hb hab => ?_ + obtain ⟨xa, hka, ra⟩ := J.ntfn.tcb a (hxsT a ha) + obtain ⟨xb, hkb, rb⟩ := J.ntfn.tcb b (hxsT b hb) + have h := hab xa xb hka hkb + rw [← ra.prio, ← rb.prio] at h + exact h + have hq : ∀ a ∈ x.ntfnObj.ntfnQueue, ∃ y, σ.kheap a = some (.tcb y) := by + intro a ha + rw [hxs] at ha + obtain ⟨u, hu, rfl⟩ := List.mem_map.1 ha + obtain ⟨y, hy, -⟩ := J.ntfn.tcb u (hxsT u hu) + exact ⟨y, hy⟩ + have hmq : ∀ y ∈ ntfnQueueOf x, ∃ z, σ.kheap y = some (.tcb z) ∧ z.tcbPriority = prioOf σ y := fun y hy => by + obtain ⟨z, hz⟩ := hq y hy + refine ⟨z, hz, ?_⟩ + simp [prioOf, Fl4Spec.optMapSome, Fl4Spec.optMap, tcbsOf, tcbsOfKh, tcbOf, hz] + have hnb : ∀ m, xt.tcbState ≠ .blockedOnNotification m := fun m c => by + rcases hrunS with e | e <;> rw [e] at c <;> cases c + have htq : specPtr t ∉ ntfnQueueOf x := not_member J.ntfn htZ hkt hnb p hp x hk + have htp : specPtr t ≠ specPtr p := fun h => by rw [h, hk] at hkt; cases hkt + -- FL4 and spec + have body := receive_signal_waitScA (st := st) (ar := ar) (hSOps).1 hSOps.2.2.1 hSOps.2.1 hF J.iplaced + (Z := Z) (m := m) (κ := κ) rfl (fun _ _ h => h) (fun _ _ Q R h => ⟨Q, R, h⟩) J.ipcPlace hcm + (fun _ _ _ h => h) s a σ J.ntfn J.ep hSR htZ hkt hRun hp hk hna hQ hxs hsorted + obtain ⟨⟨s2, a2⟩, ew, ea, σ5, hmr, N2, E2, ⟨Q, Rl, sr2⟩, Kp45, ssz45, w⟩ := spec_existsA body + simp only at ea N2 E2 sr2 w + subst a2 + obtain ⟨σt, hσt⟩ : ∃ σt, waitStsA σ (specPtr t) (specPtr p) xt = σt := ⟨_, rfl⟩ + obtain ⟨y4, hy4⟩ : ∃ y4, waitNtfnA σ (specPtr t) x xt = y4 := ⟨_, rfl⟩ + have eσ0 : blkWaitA σ (specPtr t) (specPtr p) x xt = updK σt (specPtr p) (.notification y4) := by + unfold blkWaitA; rw [hσt, hy4] + obtain ⟨σ', hσ'⟩ : ∃ σ', blkWaitA σ (specPtr t) (specPtr p) x xt = σ' := ⟨_, rfl⟩ + rw [hσ'] at hmr Kp45 ssz45 + obtain ⟨xs0, ns0, hks0⟩ : ∃ xs0 ns0, σ.kheap sc = some (.schedContext xs0 ns0) := by + obtain ⟨z, j, hz, -⟩ := J.sr.inv.back (specPtr t) xt sc hkt (hsct xt hkc) + exact ⟨z, j, hz⟩ + have hst0 : xt.tcbSchedContext = some sc := hsct xt hkc + have hNT : sc ≠ specPtr t := fun e => by rw [e, hkt] at hks0; cases hks0 + have eσ : σ' = updK σt (specPtr p) (.notification y4) := hσ'.symm.trans eσ0 + have B : WaitStepA σ σ' (specPtr t) (specPtr p) y4 xt := by + rw [← hσ', ← hy4]; exact blkWait_stepA htp + -- a pending switch target across the block + have hSn : sc ≠ specPtr p := fun e => by rw [e, hk] at hks0; cases hks0 + have pend : ∀ u, σ.schedulerAction = .switchThread u → u ≠ specPtr t ∧ u ≠ specPtr p ∧ σ'.kheap u = σ.kheap u ∧ + ∀ x1 sc1, σ.kheap u = some (.tcb x1) → x1.tcbSchedContext = some sc1 → + sc1 ≠ sc ∧ σ'.kheap sc1 = σ.kheap sc1 := fun u e => by + obtain ⟨u1, -, z, hz, -⟩ := J.pending e + have u2 : u ≠ specPtr p := fun h => by rw [h, hk] at hz; cases hz + refine ⟨u1, u2, B.far u u1 u2, fun x1 sc1 hx hs => ?_⟩ + obtain ⟨y1, j, hy1, hb1⟩ := J.sr.inv.back u x1 sc1 hx hs + have c1 : sc1 ≠ specPtr t := fun h => by rw [h, hkt] at hy1; cases hy1 + have c2 : sc1 ≠ specPtr p := fun h => by rw [h, hk] at hy1; cases hy1 + refine ⟨fun h => ?_, B.far sc1 c1 c2⟩ + obtain ⟨y2, j2, hy2, hb2⟩ := J.sr.inv.back _ xt sc hkt hst0 + rw [h, hy2] at hy1; cases hy1 + exact u1 (Option.some.inj (hb1.symm.trans hb2)) + have hns' : ∀ u, σ'.schedulerAction = .switchThread u → σ.schedulerAction = .switchThread u := fun u e => by + rcases B.act with h | h + · exact h.symm.trans e + · rw [h] at e; cases e + have hr' : ActReadyA σ' := by + rcases B.act with h | h + swap + · intro u e; rw [h] at e; cases e + refine ActReadyA.congr J.sr.actReady h (by show σ'.curTime = _; rw [B.frame]) B.releaseQueue (fun u e => ?_) + obtain ⟨-, -, k1, k2⟩ := pend u e + exact ⟨k1, fun x1 sc1 hx hs => (k2 x1 sc1 hx hs).2⟩ + have hct' : specPtr t = σ'.curThread := by rw [B.curThread]; exact ct.symm + have kk : σ'.kheap sc = some (.schedContext xs0 ns0) := + B.keep hkt hk (fun _ e => by cases e) (fun _ e => by cases e) |>.1 hks0 + have hsw' : SwA σ' := fun u e => by + have e0 := hns' u e + obtain ⟨u1, u2, k1, k2⟩ := pend u e0 + obtain ⟨⟨x1, c, hx, hxs, y1, k, hy, act⟩, hnr, z, hz, hrun⟩ := schedulable_actA (J.body.sw u e0) + rw [hx] at hz; cases hz + refine schedulable_ofA (by rw [k1]; exact hx) hrun hxs (by rw [(k2 x1 c hx hxs).2]; exact hy) ?_ + (by rw [B.releaseQueue]; exact hnr) + simpa [scActive, activeSc] using act + have hret : maybeReturnSc (Z := Unit) (specPtr p) (specPtr t) σ' = + ({((), unbWaitA (preschedA σ') (specPtr t) sc xs0 ns0 (waitTcbA xt (specPtr p)))}, False) := by + have kn : σ'.kheap (specPtr p) = some (.notification y4) := B.atN + have kt : σ'.kheap (specPtr t) = some (.tcb (waitTcbA xt (specPtr p))) := B.atT + refine maybeReturnSc_unbSwA kn kt (by rw [← hy4]; exact hsn) hst0 kk hNT hct' hr' (fun u e => ?_) + obtain ⟨u1, -, k1, k2⟩ := pend u (hns' u e) + exact ⟨u1, fun x1 hx h => (k2 x1 sc (k1 ▸ hx) h).1 rfl⟩ + have hσ5' : σ5 = unbWaitA (preschedA σ') (specPtr t) sc xs0 ns0 (waitTcbA xt (specPtr p)) := + single_outA (hmr.symm.trans hret) + have hσ5 := hσ5' + rw [← hσ'] at hσ5 + have hyq : y4.ntfnObj = .waitingNtfn (specIns (ntfnQueueOf x) (prioOf σ) xt.tcbPriority (specPtr t)) := by + rw [← hy4]; unfold waitNtfnA; rfl + -- FL4: `handle_recv` + have hb : L.mem s (Notif.nBoundA p) = t ∨ L.mem s (Notif.nBoundA p) = 0#u64 := by + by_cases h0 : L.mem s (Notif.nBoundA p) = 0#u64 + · exact Or.inr h0 + · rcases hbt with h | h + · rw [hx.bound, optRef, if_neg h0, ct] at h + exact Or.inl (specPtr_inj (Option.some.inj h)) + · rw [hx.bound, optRef, if_neg h0] at h + cases h + have hfl : syscall.handle_recv st ar s a true canReply = ok (s2, a) := by + unfold syscall.handle_recv + rw [cur_thread_ok, bind_ok, tcb_reg_eqA, bind_ok] + rw [show L.curW s = t from rfl, hcap] + simp only [bind_ok, ↓reduceIte] + obtain ⟨v', hv', e⟩ := spec_existsA (Notif.obj.Ntfn.bound_tcb.spec (st := st) s p) + rw [hv', bind_ok, e] + rcases hb with h | h + · simp only [h, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte, ite_self] + exact ew + · simp only [h, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + exact ew + -- the spec + have hrecv : ((), σ5) ∈ (receiveSignal (Z := Unit) (specPtr t) + (absCapA (.Notification p badge send true)) true σ).1 := by + rw [show absCapA (.Notification p badge send true) = + .notificationCap (specPtr p) badge.bv (rightsOfA send true false false) from rfl, + blkWait_specRetA hk hkt hna htq hmq, hσ', hret, hσ5'] + exact Set.mem_singleton _ + have hmem : ((), σ5) ∈ (handleRecv (Z := Unit) true canReply σ).1 := by + have capReg : (L.mem s (regWA t layout.reg.CAP)).bv = xt.tcbArch.tcbContext.userRegs cap_register := by + rw [← r.regs cap_register, regIdxA_capA]; rfl + have capRead : NonDet.liftM dataToCptr (asUser (Z := Unit) (specPtr t) (getRegister cap_register)) σ = + ({(toBl (L.mem s (regWA t layout.reg.CAP)).bv, σ)}, False) := by + unfold NonDet.liftM + rw [nd_bind_singleA' (asUser_getRegister_eqA hkt _), ← capReg] + rfl + have hc0 : (lookupCap (Z := Unit) (specPtr t) (toBl (L.mem s (regWA t layout.reg.CAP)).bv)).run σ = + ({(.ok (absCapA (.Notification p badge send true)), σ)}, False) := by + have l := post_of_okA (lookup_cap_recv_refinesA (st := st) _ s σ J.j.1.st J.j.1.cv t hthT _) hcap + obtain ⟨e, he, hrel⟩ := l + cases e with + | error _ => exact hrel.elim + | ok ac => + obtain rfl : absCapA (.Notification p badge send true) = ac := hrel.2 + exact he + have hc1 : (capFaultOnFailure (ofBl (toBl (L.mem s (regWA t layout.reg.CAP)).bv)) true + (lookupCap (Z := Unit) (specPtr t) (toBl (L.mem s (regWA t layout.reg.CAP)).bv))).run σ = + ({(.ok (absCapA (.Notification p badge send true)), σ)}, False) := run_handleE_okA hc0 + rw [handleRecv, nd_gets_bindSc, ct, nd_bind_oneSc capRead] + apply mem_bindA.2 + refine ⟨(.ok (), σ5), ?_, rfl⟩ + rw [run_bind_okA hc1] + dsimp only [absCapA] + have boundRead : (NonDetE.liftE (ε := Fault) + (getNtfnObjRef (Z := Unit) Notification.ntfnBoundTcb (specPtr p))).run σ = + ({(.ok x.ntfnBoundTcb, σ)}, False) := run_liftEA (getNtfnObjRef_eqA hk _) + rw [if_pos (by simp [AllowRecv, rightsOfA]), run_bind_okA boundRead, if_pos (by rwa [ct] at hbt)] + unfold NonDetE.liftE + rw [ExceptT.run_lift] + exact mem_bindA.2 ⟨((), σ5), hrecv, rfl⟩ + -- P1's walk + obtain ⟨Pt, Ub, Pp7, Tb7, Hr, Te, Kt, Lv7, Re, bits, KLv, W⟩ := J7A.unp J.j7 + have I := J.delIn hA hPl W + have h : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s := by + obtain ⟨q, b, x, sg, sc⟩ := J.sys.ent.bxi + exact ⟨q, b, x, sc, sg⟩ + have D := J.decPreA (extra := Aeneas.Std.Array.repeat 3#usize 0#u64) hslot (fun z hz hne => by + exact absurd (by simpa [Aeneas.Std.Array.repeat] using hz) hne) + have hH : H p = some .ntfn := J.utIpc.ntfn p hp + have regs : RpPost st H Z.Cs OS Z.V ia ie Ac s s2 := by + have walk := Fl4ProofsA64.P1.W7.R.ST.receive_signal.sw (st := st) hA a t p true I J.core0 J.ko + (SW.start J.sys.sysl J.symL h) D.cb + (by rcases J.runW with e | e | e <;> rw [e] <;> + simp [sched.TS_RUNNING, sched.TS_RESTART, sched.TS_RUNNING_VM, sched.TS_INACTIVE]) + (show p ∈ H.ntfns from hH) hH + (by + rw [epState_eq] + have : stOf (L.mem s) p = obj.NTFN_IDLE ∨ stOf (L.mem s) p = obj.NTFN_WAITING := by + cases h : x.ntfnObj with + | idleNtfn => exact Or.inl (stOf_idle (T := Z.T) (h ▸ hx.obj) rfl) + | waitingNtfn q => exact Or.inr (stOf_waiting (T := Z.T) (h ▸ hx.obj) rfl) + | activeNtfn b => exact absurd h (hna b) + rcases this with e | e <;> rw [e] <;> simp [ntfn_consts.1, ntfn_consts.2.1]) + (J.runVcpu.zero htZ hnvm) + (fun k hk' _ => by + rw [hH] at hk' + cases hk' + exact ⟨slot, hslot, by rw [hsc]; exact names_ntfn⟩) + exact (post_of_okA (P := fun q => SW st H Z.Cs OS Z.V ia ie Ac s q.1) walk ew).1.post + obtain ⟨bxi, sysl, symL, userOK, j7⟩ := J.toJStage3A.finRpA hA hPl rfl regs σ5 + -- the record + have hP := J.iplaced + have twk := fun (u : U64) (hu : u ∈ Z.T) (K : U64) => J.toJStage3A.tcbKept_waitA (s' := s2) htZ hp hxsT w hu (K := K) + have wN : WritesIn st s s2 (fun b => SchedStepNWA (st := st) N E Rp Z m σ s b ∨ BufW Bf b) := + w.mono fun b h => by + have hta := hP.e.tcb t htZ + have tw : ∀ (k : Nat), b.val = t.val + k → TWOffN k → IpcFpA st Z E Rp Bf m σ s b := fun k hv hk' => + Or.inl (Or.inr (Or.inr (Or.inl ⟨t, htZ, Or.inl (twf_ofA k hv hk')⟩))) + have lift : IpcFpA st Z E Rp Bf m σ s b → SchedStepNWA (st := st) N E Rp Z m σ s b ∨ BufW Bf b := + fun h => h.elim (ewrite_stepNA_buf J.sr.core.tcbs) fun h => Or.inl (Or.inl (Or.inl h)) + have nw : ∀ k : Nat, b.val = p.val + k → k < 40 → SchedStepNWA (st := st) N E Rp Z m σ s b := fun k hv hk' => + Or.inr (Or.inl ⟨p, hp, by omega, by omega⟩) + rcases h with (c | c) | c | (c | c) | ⟨v, hv, c⟩ + · exact lift (tw 3328 (by rw [c, val_st hta]) (by unfold TWOffN; omega)) + · exact lift (tw 3336 (by rw [c, val_blk hta]) (by unfold TWOffN; omega)) + · exact lift (Or.inr c) + · refine Or.inl (nw 0 ?_ (by omega)) + rw [c, Notif.epHeadA_eq, Notif.nfield_val (hP.n.ntfn p hp) (by simp)] + simp + · refine Or.inl (nw 8 ?_ (by omega)) + rw [c, Notif.epTSA_eq, Notif.nfield_val (hP.n.ntfn p hp) (by simp)] + simp + · have hvT : v ∈ Z.T := by + rcases List.mem_cons.1 hv with rfl | hv + exacts [htZ, hxsT v hv] + have hva := hP.e.tcb v hvT + refine lift (Or.inl (Or.inr (Or.inr (Or.inl ⟨v, hvT, Or.inr ?_⟩)))) + rcases c with c | c + · exact c ▸ (a_next hva).2 + · exact c ▸ (a_prev hva).2 + set M : ObjRef → Prop := fun o => o = specPtr t with hMdef + have K0 : SIKA σ.idleThread M (fun _ => False) false σ σt := by + rw [← hσt]; exact waitSts_sikaA hkt rfl + have notM : ∀ c < n, ¬ M (m.idleThreadOf c) := by + intro c hc' h + obtain ⟨y, hy, hs⟩ := J.sidle.f3 c hc' + rw [h, hkt] at hy + cases hy + rcases hrunS with e | e <;> rw [e] at hs <;> cases hs + have sinv0 : SInvXA σt m n := by + refine K0.sinvSw J.sinv J.sidle J.body.sw J.noSelf (fun u hu h => J.swCur u hu h) notM + (fun h => hRun.2.2.2 h.symm) ?_ (fun _ h => h.elim) (fun _ h => h.elim) + (fun y hy => by rw [show y = specPtr t from hy]; exact hRun.2.1.2) (J.sidle.rdyA J.sinv) + (K0.mfA J.sinv fun q hq x hx => by + rw [show q = specPtr t from hq, hkt] at hx; cases hx + exact fun hf => by have h0 := FaultStA.notRun hf; rw [hRun.1] at h0; cases h0) + intro y hy dm pr + rw [show y = specPtr t from hy] + exact hRun.2.1.1 dm pr + have sidle0 := K0.sidle J.sidle notM (J.sidle.rdyA J.sinv) + have atp : σ'.kheap (specPtr p) = some (.notification y4) := B.atN + have eid : σ'.idleThread = σt.idleThread := by rw [eσ]; rfl + have etm : σ'.curTime = σt.curTime := by rw [eσ]; rfl + have ktp : σt.kheap (specPtr p) = some (.notification x) := by + rw [← hσt, waitStsA_kheap, Function.update_of_ne (Ne.symm htp), hk] + have same : ∀ q, q ≠ specPtr p → σ'.kheap q = σt.kheap q := fun q e => by + rw [eσ, updK_kheap, Function.update_of_ne e] + have keepK : ∀ q ob, σt.kheap q = some ob → (∀ y, ob ≠ .notification y) → σ'.kheap q = some ob := + fun q ob h ne => by + rw [same q fun e => by rw [e, ktp] at h; exact ne x (Option.some.inj h).symm] + exact h + have backK : ∀ q ob, σ'.kheap q = some ob → (∀ y, ob ≠ .notification y) → σt.kheap q = some ob := + fun q ob h ne => by + by_cases e : q = specPtr p + · rw [e, atp] at h + exact absurd (Option.some.inj h).symm (ne _) + · rwa [same q e] at h + have sinv : SInvXA σ' m n := by + have same' : SchedSameA σt σ' := + { kh := fun q ko hko => by + have ne : ∀ y, ko ≠ .notification y := fun y e => by + rcases hko with ⟨_, e'⟩ | ⟨_, _, e'⟩ <;> rw [e] at e' <;> cases e' + exact ⟨fun h => backK q ko h ne, fun h => keepK q ko h ne⟩ + cur := by rw [eσ]; rfl + idle := by rw [eσ]; rfl + csc := by rw [eσ]; rfl + rq := by rw [eσ]; rfl + rls := by rw [eσ]; rfl + time := by rw [eσ]; rfl + cons := by rw [eσ]; rfl } + refine sinv0.of_spec same' (by rw [eσ]; rfl) ?_ + have I := sinv0.irqN + have kk : ∀ q ob b a', (updK σt (specPtr p) (.notification y4)).kheap q = some ob → capOf ob b = some a' → + σt.kheap q = some ob := fun q ob b a' h hc' => by + refine backK q ob (by rw [eσ]; exact h) fun y e => ?_ + subst e + cases hc' + have ct' : ∀ k q, capTableAt k q (updK σt (specPtr p) (.notification y4)) ↔ capTableAt k q σt := fun k q => by + rw [← eσ] + unfold capTableAt + by_cases e : q = specPtr p + · rw [e, atp, ktp] + constructor <;> rintro ⟨cs, h, -⟩ <;> simp at h + · rw [same q e] + rw [eσ] + refine ⟨?_, fun irq hi ob b a' h hc' => I.slot irq hi ob b a' (kk _ _ _ _ h hc') hc', + fun q ob b a' h hc' => I.ref q ob b a' (kk _ _ _ _ h hc') hc'⟩ + obtain ⟨⟨inj, tab⟩, dom⟩ := I.node + exact ⟨⟨inj, fun irq h => (ct' _ _).2 (tab irq h)⟩, fun irq h k c => dom irq h k ((ct' _ _).1 c)⟩ + have idleVt : ∀ c < n, vtCapA σ' (m.idleThreadOf c) = some .nullCap ∧ vcpuOfA σ' (m.idleThreadOf c) = none := by + intro c hc' + obtain ⟨y, hy, -⟩ := sidle0.f3 c hc' + exact idleVtSameA (by rw [hy]; exact keepK _ _ hy (fun _ e => by cases e)) (sidle0.idleSlot c hc') + (sidle0.idleBound c hc') + have sidle : SIdleA σ' m n := + { f3 := fun c hc' => by + obtain ⟨y, hy, hs⟩ := sidle0.f3 c hc' + exact ⟨y, keepK _ _ hy (fun _ e => by cases e), hs⟩ + f4 := fun y k h => sidle0.f4 y k (backK _ _ h (fun _ e => by cases e)) + f5 := fun q y k h => by + rw [eid]; exact sidle0.f5 q y k (backK _ _ h (fun _ e => by cases e)) + f6 := sidle0.f6.of_eq fun sl => by + unfold specCapAtA + by_cases e : sl.1 = specPtr p + · rw [e, atp, ktp] + · rw [same _ e] + f7 := fun y k h => by + rw [etm]; exact sidle0.f7 y k (backK _ _ h (fun _ e => by cases e)) + idleSlot := fun c hc' => (idleVt c hc').1 + idleBound := fun c hc' => (idleVt c hc').2 } + have HS := B.specSched hkt hk + have keepS : ∀ q y k, σ.kheap q = some (.schedContext y k) ↔ σ'.kheap q = some (.schedContext y k) := + fun q y k => B.keep hkt hk (fun _ e => by cases e) (fun _ e => by cases e) + have keepR : ∀ q y, σ.kheap q = some (.reply y) ↔ σ'.kheap q = some (.reply y) := + fun q y => B.keep hkt hk (fun _ e => by cases e) (fun _ e => by cases e) + have ssz : SSzA σ σ' := fun q y k h => ⟨y, (keepS q y k).2 h⟩ + have reply : ReplyStackRelA (L.mem s2) Rp Z.C σ' (scNameA m) stack := by + refine J.reply.frame (fun r' hr' => ?_) (fun c _ y k h => ⟨y, k, (keepS _ _ _).1 h, rfl⟩) + (fun _ _ y h => ⟨y, (keepR _ _).2 h, rfl⟩) + have hrH : H r' = some .reply := J.utIpc.reply r' hr' + obtain ⟨r32, -, rfit⟩ := hP.e.rep r' hr' + have one : ∀ o : U64, 8 ≤ o.val → o.val < 32 → o.val % 8 = 0 → L.mem s2 (wa r' o) = L.mem s (wa r' o) := by + intro o h1 h2 h3 + have hv : (wa r' o).val = r'.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + refine w.mem_eq (by unfold Aligned; rw [hv]; omega) ?_ + have hw : IpcObjWA N E Rp (wa r' o) := Or.inr (Or.inr ⟨r', hr', by omega, by omega⟩) + have offT : ∀ v ∈ Z.T, ∀ k, k < 4096 → (wa r' o).val ≠ v.val + k := fun v hv' k hk' => by + have := hP.e.rt r' hr' v hv'; omega + have offN : (wa r' o).val ≠ p.val ∧ (wa r' o).val ≠ p.val + 8 := by + have := hP.nr p hp r' hr'; omega + have hta := hP.e.tcb t htZ + rintro ((c | c) | c | (c | c) | ⟨v, hv', c⟩) + · exact offT t htZ 3328 (by omega) (by rw [c, val_st hta]) + · exact offT t htZ 3336 (by omega) (by rw [c, val_blk hta]) + · exact J.ipc_offMwrA hw c + · refine offN.1 ?_ + rw [c, Notif.epHeadA_eq, Notif.nfield_val (hP.n.ntfn p hp) (by simp)] + simp + · refine offN.2 ?_ + rw [c, Notif.epTSA_eq, Notif.nfield_val (hP.n.ntfn p hp) (by simp)] + simp + · have hvT : v ∈ Z.T := by + rcases List.mem_cons.1 hv' with rfl | h' + exacts [htZ, hxsT v h'] + have hva := hP.e.tcb v hvT + rcases c with c | c + · exact offT v hvT 3496 (by omega) (by rw [c, val_next hva]) + · exact offT v hvT 3504 (by omega) (by rw [c, val_prev hva]) + exact ⟨one _ (by simp [layout.reply.NEXT]) (by simp [layout.reply.NEXT]) (by simp [layout.reply.NEXT]), + one _ (by simp [layout.reply.PREV]) (by simp [layout.reply.PREV]) (by simp [layout.reply.PREV])⟩ + have tcbF : ∀ q y, σ.kheap q = some (.tcb y) → ∃ y', σ'.kheap q = some (.tcb y') ∧ y'.tcbFault = y.tcbFault ∧ + y'.tcbIpcframe = y.tcbIpcframe ∧ y'.tcbIpcBuffer = y.tcbIpcBuffer ∧ y'.tcbMcpriority = y.tcbMcpriority ∧ + y'.tcbYieldTo = y.tcbYieldTo ∧ y'.tcbFlags = y.tcbFlags := fun q y hy => by + by_cases e : q = specPtr t + · subst e + rw [hkt] at hy; cases hy + exact ⟨_, B.atT, rfl, rfl, rfl, rfl, rfl, rfl⟩ + · exact ⟨y, by rw [B.far q e (fun c => by rw [c, hk] at hy; cases hy)]; exact hy, rfl, rfl, rfl, rfl, rfl, rfl⟩ + have Kk : IpcKeepA σ σ' := ⟨tcbF, fun q y k h => ⟨y, k, (keepS _ _ _).1 h, rfl⟩, + fun q y k h => ⟨y, k, (keepS _ _ _).2 h, rfl⟩⟩ + have twr : ∀ u ∈ Z.T, ∀ O : U64, O.val < 224 ∨ O.val = 3440 ∨ O.val = 3472 ∨ O.val = 3512 ∨ O.val = 3456 → O.val % 8 = 0 → + L.mem s2 (wa u O) = L.mem s (wa u O) := fun u hu O hO h8 => + twk u hu O (by omega) h8 (by omega) (fun _ => by omega) + have kst : ∀ u ∈ Z.T, u ≠ t → L.mem s2 (stA u) = L.mem s (stA u) := fun u hu ne => by + rw [stA_eq] + exact twk u hu 3328#u64 (by simp) (by simp) (by simp) (fun c => absurd c ne) + have kblk : ∀ u ∈ Z.T, u ≠ t → L.mem s2 (Notif.tBlockA u) = L.mem s (Notif.tBlockA u) := fun u hu ne => by + rw [show Notif.tBlockA u = wa u 3336#u64 from rfl] + exact twk u hu 3336#u64 (by simp) (by simp) (by simp) (fun c => absurd c ne) + have rv : RunVcpuInv st Z.T s2 := fun u hu h0 => by + have e : L.mem s2 (runVcpuA u) = L.mem s (runVcpuA u) := + twk u hu 3672#u64 (by simp) (by simp) (by simp) (fun _ => by simp) + rw [e] at h0 + by_cases c : u = t + · subst c + exact absurd (J.runVcpu.zero htZ hnvm) h0 + · rw [show L.mem s2 (Fl4ProofsA64.P1.stA u) = L.mem s (Fl4ProofsA64.P1.stA u) from kst u hu c] + exact J.runVcpu u hu h0 + have blkQ : BlkQueuedA Z.T E N (L.mem s2) σ' := by + have old : ∀ u ∈ Z.T, ∀ y, σ'.kheap (specPtr u) = some (.tcb y) → u ≠ t → σ.kheap (specPtr u) = some (.tcb y) := by + intro u _ y hy ne + rcases B.cases hy with ⟨c, -⟩ | ⟨-, c⟩ | ⟨-, -, c⟩ + · exact absurd (specPtr_inj c) ne + · cases c + · exact c + refine ⟨fun u hu y hy e hb' => ?_, fun u hu y hy q hq' => ?_⟩ + · have ne : u ≠ t := fun c => by + rw [c, B.atT] at hy + cases hy + cases hb' + obtain ⟨hE, z, hz, hm⟩ := J.blkQ.ep u hu y (old u hu y hy ne) e hb' + exact ⟨by rw [kblk u hu ne]; exact hE, z, (B.keep (x := x) hkt hk (fun _ c => by cases c) + (fun _ c => by cases c)).1 hz, hm⟩ + · by_cases c : u = t + · subst c + rw [B.atT] at hy + cases hy + cases hq' + obtain ⟨y1, hy1, r'⟩ := N2.tcb t htZ + rw [hσ5', unbWait_atTA hNT] at hy1; cases hy1 + have eb : L.mem s2 (Notif.tBlockA t) = p := specPtr_inj (r'.blk (specPtr p) rfl) + exact ⟨by rw [eb]; exact hp, _, _, atp, hyq, mem_specInsA.2 (Or.inl rfl)⟩ + · obtain ⟨hN, z, l, hz, ho, hm⟩ := J.blkQ.ntfn u hu y (old u hu y hy c) q hq' + refine ⟨by rw [kblk u hu c]; exact hN, ?_⟩ + by_cases e : q = specPtr p + · subst e + rw [hk] at hz + cases hz + refine ⟨_, _, atp, hyq, mem_specInsA.2 (Or.inr ?_)⟩ + show specPtr u ∈ ntfnQueueOf x + unfold ntfnQueueOf + rw [ho]; exact hm + · refine ⟨z, l, ?_, ho, hm⟩ + rw [B.far q (fun d => by rw [d, hkt] at hz; cases hz) e] + exact hz + -- `reschedule_required`'s enqueue of a pending switch target + obtain ⟨qp, ap, epre, hqp, hnap, -⟩ := preschedA_casesA σ' + have kh_p : (preschedA σ').kheap = σ'.kheap := by rw [epre] + have Kp : SIKA σ'.idleThread (fun _ => False) (fun _ => False) true σ' (preschedA σ') := preschedA_sikaA hr' + have hcs' : σ'.schedulerAction ≠ .switchThread σ'.curThread := fun e => by + rw [← hct'] at e + exact J.noSelf (by rw [← ct] at e; exact hns' _ e) + have sinvp : SInvXA (preschedA σ') m n := + Kp.sinvSw sinv sidle hsw' hcs' (fun _ _ h => h) (fun _ _ h => h) (fun h => h) (fun _ h => h.elim) + (fun _ h => h.elim) (fun _ h => h.elim) (fun _ h => h.elim) (sidle.rdyA sinv) + (Kp.mfA sinv fun _ h => h.elim) + have sidlep := Kp.sidle sidle (fun _ _ h => h) (sidle.rdyA sinv) + -- across the unbinding of `maybe_return_sc` + have hkt4 : (preschedA σ').kheap (specPtr t) = some (.tcb (waitTcbA xt (specPtr p))) := by rw [kh_p]; exact B.atT + have hks4 : (preschedA σ').kheap sc = some (.schedContext xs0 ns0) := by rw [kh_p]; exact kk + have hsi : sc ≠ idleScPtr := fun e => J.sr.inv.ntfnIdle (specPtr p) x hk (by rw [hsn, e]) + have hns4 : ∀ u, (preschedA σ').schedulerAction ≠ .switchThread u := fun u e => by + rw [epre] at e; exact hnap u e + have hti : specPtr t ≠ (preschedA σ').idleThread := by + rw [epre]; show specPtr t ≠ σ'.idleThread; rw [B.idleThread]; exact hidle + have K45 : SIKA (preschedA σ').idleThread M (fun _ => False) true (preschedA σ') + (unbWaitA (preschedA σ') (specPtr t) sc xs0 ns0 (waitTcbA xt (specPtr p))) := + unbWait_sikaA hNT hkt4 hks4 hti hsi hns4 + subst hσ5' + have hMI : ¬ M (preschedA σ').idleThread := fun h => hti h.symm + have hrqt : ∀ d p', specPtr t ∉ (preschedA σ').readyQueues d p' := fun dm pr hm => by + rw [epre] at hm + rcases hqp dm pr _ hm with h | ⟨h, -⟩ + · rw [B.readyQueues] at h; exact hRun.2.1.1 dm pr h + · exact J.noSelf (by rw [← ct] at h; exact hns' _ h) + have hMq : ∀ y, M y → ∀ d p', y ∉ (preschedA σ').readyQueues d p' := fun y hy dm pr => by + rw [show y = specPtr t from hy]; exact hrqt dm pr + have sinv5 := K45.sinv sinvp (m := m) (n := n) (fun c hc h => by + have e : (preschedA σ').idleThread = σ'.idleThread := by rw [epre] + exact notM c hc h) hMI hMq (fun _ h => h.elim) (fun _ h => h.elim) hns4 + (fun y hy hr => by + obtain ⟨x, hx, hrn⟩ := sinvp.rlsRun y hr + rw [show y = specPtr t from hy, hkt4] at hx; cases hx; cases hrn) + (sidlep.rdyA sinvp) + (K45.mfA sinvp fun p hp x hx => by + rw [show p = specPtr t from hp, hkt4] at hx; cases hx + rintro (⟨_, _, h⟩ | ⟨_, h⟩ | h) <;> cases h) + have sidle5 := K45.sidle sidlep notM (sidlep.rdyA sinvp) + have HSp : SpecSchedA σ' (preschedA σ') := by + rw [epre]; exact ⟨fun _ => Or.inl rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ + have HS5 := (HS.trans HSp).trans (unbWait_specSchedA hNT hkt4 hks4) + have ssz5 : SSzA σ (unbWaitA (preschedA σ') (specPtr t) sc xs0 ns0 (waitTcbA xt (specPtr p))) := fun q y k h => by + obtain ⟨y1, h1⟩ := ssz45 q y k h + obtain ⟨y2, h2⟩ := ssz q y1 k h1 + exact ⟨y2, h2⟩ + have Qp : QKeepA σ' (preschedA σ') := QKeepA.of_kheap kh_p (by rw [epre]; exact ⟨rfl, rfl, rfl, rfl⟩) + have Q45 := Qp.trans (unbWait_qkeepA hNT hkt4 hks4) + have Qb04 := B.qblk J.ipcSorted hkt hk (by rw [hyq]; rfl) hq + have Qb45 := QSendA.toBlk Qb04.sorted Q45.toSend + have Qk5 : QBlkA (specPtr '' E) (specPtr '' N) σ (unbWaitA (preschedA σ') (specPtr t) sc xs0 ns0 (waitTcbA xt (specPtr p))) := + ⟨Qb45.sorted, fun h => Qb45.act (Qb04.act h), fun p' y hp' => by + obtain ⟨x4, h4, a4⟩ := Qb45.rt p' y hp' + obtain ⟨x0, h0, a0⟩ := Qb04.rt p' x4 h4 + exact ⟨x0, h0, fun h => a4 (a0 h)⟩, Qb04.dom.trans Qb45.dom⟩ + have K45k := IpcKeepA.transA Kp.ipcKeep K45.ipcKeep + have Kk5 : IpcKeepA σ (unbWaitA (preschedA σ') (specPtr t) sc xs0 ns0 (waitTcbA xt (specPtr p))) := + ⟨fun q y hy => by + obtain ⟨y1, h1, a1, b1, c1, d1, e1, f1⟩ := Kk.tcb q y hy + obtain ⟨y2, h2, a2, b2, c2, d2, e2, f2⟩ := K45k.tcb q y1 h1 + exact ⟨y2, h2, a2.trans a1, b2.trans b1, c2.trans c1, d2.trans d1, e2.trans e1, f2.trans f1⟩, + fun q y k hy => by + obtain ⟨y1, k1, h1, a1⟩ := Kk.scF q y k hy + obtain ⟨y2, k2, h2, a2⟩ := K45k.scF q y1 k1 h1 + exact ⟨y2, k2, h2, a2.trans a1⟩, + fun q y' k' hy => by + obtain ⟨y1, k1, h1, a1⟩ := K45k.scB q y' k' hy + obtain ⟨y0, k0, h0, a0⟩ := Kk.scB q y1 k1 h1 + exact ⟨y0, k0, h0, a1.trans a0⟩⟩ + have reply5 : ReplyStackRelA (L.mem s2) Rp Z.C + (unbWaitA (preschedA σ') (specPtr t) sc xs0 ns0 (waitTcbA xt (specPtr p))) (scNameA m) stack := + reply.frame (fun _ _ => ⟨rfl, rfl⟩) (fun c _ x' k h => by + rw [← kh_p] at h + by_cases e : scNameA m c = sc + · rw [e] at h ⊢ + rw [hks4] at h + cases h + exact ⟨{ xs0 with scTcb := none }, ns0, unbWait_atSA hNT, rfl⟩ + · have e2 : scNameA m c ≠ specPtr t := fun e' => by rw [e', kh_p, B.atT] at h; cases h + exact ⟨x', k, by rw [unbWait_keepA hNT e2 e]; exact h, rfl⟩) + (fun r _ x' hx' => by + rcases unbWait_casesA hNT hx' with ⟨-, h⟩ | ⟨-, h⟩ | ⟨-, -, h⟩ + · cases h + · cases h + · exact ⟨x', kh_p ▸ h, rfl⟩) + have blkQp : BlkQueuedA Z.T E N (L.mem s2) (preschedA σ') := + blkQ.keepA (fun _ _ => rfl) (fun u _ x' hx' => Or.inr ⟨x', by rw [kh_p] at hx'; exact hx', rfl⟩) + (fun q y h => by rw [kh_p]; exact h) (fun q y h => by rw [kh_p]; exact h) + have blkQ5 : BlkQueuedA Z.T E N (L.mem s2) (unbWaitA (preschedA σ') (specPtr t) sc xs0 ns0 (waitTcbA xt (specPtr p))) := + blkQp.keepA (fun _ _ => rfl) (fun u _ x' hx' => by + rcases unbWait_casesA hNT hx' with ⟨e, h⟩ | ⟨-, h⟩ | ⟨-, -, h0⟩ + · cases h + rw [e] + exact Or.inr ⟨waitTcbA xt (specPtr p), hkt4, rfl⟩ + · cases h + · exact Or.inr ⟨x', h0, rfl⟩) + (fun q y h => by + have h1 : q ≠ specPtr t := fun e => by rw [e, hkt4] at h; cases h + have h2 : q ≠ sc := fun e => by rw [e, hks4] at h; cases h + rw [unbWait_keepA hNT h1 h2]; exact h) + (fun q y h => by + have h1 : q ≠ specPtr t := fun e => by rw [e, hkt4] at h; cases h + have h2 : q ≠ sc := fun e => by rw [e, hks4] at h; cases h + rw [unbWait_keepA hNT h1 h2]; exact h) + have sv5 := unbWait_schedValidA hNT hkt4 hks4 (preschedA_schedValidA (B.schedValid J.schedValid hkt hk hsca)) + hrqt (by rw [epre]; show _ ∉ σ'.releaseQueue; rw [B.releaseQueue]; exact hRun.2.1.2) + have J2 := J.toJStage3A.of_ipcStepUSA (Q := Q) (Rl := Rl) (curOf := Z.curOf) (curScOf := Z.curScOf) wN + HS5 ssz5 Qk5 (fun _ _ => by + show (preschedA σ').machineState.underlyingMemory _ = σ.machineState.underlyingMemory _ + rw [epre]; show σ'.machineState.underlyingMemory _ = _; rw [B.frame]) (fun _ _ => rfl) bxi sysl symL userOK j7 sr2 E2 N2 reply5 J.deadExt.reply Kk5 twr sinv5 sidle5 rv + blkQ5 sv5 + have hcur5 : (unbWaitA (preschedA σ') (specPtr t) sc xs0 ns0 (waitTcbA xt (specPtr p))).curThread = σ.curThread := by + show (preschedA σ').curThread = _ + rw [epre]; exact B.curThread + refine ⟨s2, a, _, hfl, hmem, ⟨xt, xs0, ns0, hkc, hks0, by rw [ct]; exact hσ5⟩, A64RelS3.ofJ J2 ?bo, + curW_keptA J.toJStage3A J2 hcur5, ?bo⟩ + have ft : (preschedA σ').curTime = σ.curTime ∧ (preschedA σ').machineState = σ.machineState ∧ + (preschedA σ').curSc = σ.curSc ∧ (preschedA σ').consumedTime = σ.consumedTime ∧ + (preschedA σ').releaseQueue = σ.releaseQueue := by + have P := preschedA_keepA σ' + refine ⟨P.time.trans ?_, P.mach.trans ?_, P.csc.trans ?_, P.con.trans ?_, P.rls.trans B.releaseQueue⟩ + all_goals rw [B.frame] + refine J.body.step (fun u e => by cases e) ft.1 (congrArg (fun z => z.lastMachineTime) ft.2.1) hcur5 ft.2.2.1 + ft.2.2.2.1 (fun _ _ _ _ _ h => ft.2.2.2.2 ▸ h) (fun y' k' hz => ?_) (fun h => ?_) + (fun hh => by + have e : (unbWaitA (preschedA σ') (specPtr t) sc xs0 ns0 (waitTcbA xt (specPtr p))).schedulerAction = .chooseNewThread := rfl + rw [e] at hh; cases hh) + · rcases unbWait_casesA hNT hz with ⟨-, h⟩ | ⟨e, h⟩ | ⟨-, -, h0⟩ + · cases h + · cases h + exact ⟨xs0, by rw [e]; exact hks0, rfl, rfl, Or.inr rfl⟩ + · exact ⟨y', (keepS _ _ _).2 (by rw [kh_p] at h0; exact h0), rfl, rfl, Or.inl rfl⟩ + · have e : (unbWaitA (preschedA σ') (specPtr t) sc xs0 ns0 (waitTcbA xt (specPtr p))).schedulerAction = .chooseNewThread := rfl + rw [e] at h; cases h + +set_option maxHeartbeats 1000000 in +/-- **`RecvPreA.waitBlockScA` at a syscall entry.** -/ +theorem JSyscallA.waitBlockScA + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p badge slot : U64} {send canReply : Bool} + (hcap : cspace.lookup_cap st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.CAP)) = + ok (.Ok (.Notification p badge send true))) + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hsc : capA st s slot = .Notification p badge send true) + (hp : p ∈ N) {x : Notification} (hk : σ.kheap (specPtr p) = some (.notification x)) + (hna : ∀ b, x.ntfnObj ≠ .activeNtfn b) {sc : ObjRef} (hsn : x.ntfnSc = some sc) + (hsct : ∀ y, σ.kheap σ.curThread = some (.tcb y) → y.tcbSchedContext = some sc) + (hbt : x.ntfnBoundTcb = some σ.curThread ∨ x.ntfnBoundTcb = none) : + HRecvPostA st AR ar hPl.ipa n Z.Cs s a m true canReply σ + (fun σ' => ∃ y xs ns, σ.kheap σ.curThread = some (.tcb y) ∧ σ.kheap sc = some (.schedContext xs ns) ∧ + σ' = blkWaitScA σ σ.curThread (specPtr p) x y sc xs ns) := + by + obtain ⟨s1, a1, σ1, r1, r2, ⟨y, xs, ns, k1, k2, e⟩, r4, r5, r6⟩ := + RecvPreA.waitBlockScA (canReply := canReply) hA hPl J.toRecvPreA hCs h1 hmb hmax htu hcap hslot hcov hsc hp hk + hna hsn hsct hbt + refine ⟨s1, a1, σ1, r1, r2, ⟨y, xs, ns, k1, k2, ?_⟩, r4, r5, r6⟩ + have htp : σ.curThread ≠ specPtr p := fun h => by rw [h, hk] at k1; cases k1 + rw [e, preschedA_noswitchA (fun u c => ?_)] + · rfl + rcases (blkWait_stepA (σ := σ) (x := x) (xt := y) htp).act with h | h + · exact J.noSwitch u (h.symm.trans c) + · rw [h] at c; cases c + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3RecvWaitScBodyA.lean b/proofs/Fl4ProofsA64/P3/Stage3RecvWaitScBodyA.lean new file mode 100644 index 000000000..0e30bae3c --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3RecvWaitScBodyA.lean @@ -0,0 +1,341 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2c): **the FL4 body of a blocking `receive_signal` whose SC return may unbind** +(`receive_signal_waitA` without its `ntfnSc = none` hypothesis; generated by `handoff/tools/gen_lift2c_waitsc_body.py` +from that theorem): the four stores and `ntfn_append`, then `maybe_return_sched_context` against `maybe_return_sc` +(`maybeReturnIpcA`), with the relations at the spec's outcome, the spec frames `SKeepA` / `SSzA` and the write set. + +[S] none beyond the caller's. Hypotheses: the concrete scheduler relation behind `SR` (`hSRc` / `hSRi`), the IPC +placement, `m.curCore < numNodes`, `MWrA ⊆ Wr`. No premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3RecvWaitSpecScA +import Fl4ProofsA64.P3.IpcScGlueA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched + Fl4Spec Fl4SpecA64 + +noncomputable section + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + {N T E R : Set U64} {Bf : Set (U64 × Word)} {SR : S → State Unit → Prop} + {Wr : State Unit → S → U64 → Prop} + +set_option maxHeartbeats 2000000 in +/-- **The FL4 body of a blocking `receive_signal` whose `maybe_return_sched_context` may unbind** (module +docstring). -/ +theorem receive_signal_waitScA {DL : S → U64 → Prop} (hH : SchedHypsA st N T SR) (hOE : SchedOpsEA st T E R Bf SR) + (hOn : SchedOpsA st ar N T SR DL) (hF : SchedFootNA st ar N T SR DL Wr) + (hP : IPlacedA N T E R Bf) + {Z : XSetsA} {m : McState} {κ : U64 → U64} (hZT : Z.T = T) + (hSRc : ∀ s' σ', SR s' σ' → ∃ Q R, SchedSRA st { Z with Q := Q, R := R } m κ s' σ') + (hSRi : ∀ s' σ' Q R, SchedSRA st { Z with Q := Q, R := R } m κ s' σ' → SR s' σ') + (hPl : IpcSchedPlacementA Z.th N E R Bf) (hcm : m.curCore < fl4A64Platform.numNodes) + (hWr : ∀ σ' s' b, MWrA (st := st) Z m σ' s' b → Wr σ' s' b) + (s : S) (a : A) (σ : State Unit) (hN : NtfnStateRel N T (L.mem s) σ) (hE : EpStateRel T E R Bf (L.mem s) σ) + (hSR : SR s σ) + {t : U64} (ht : t ∈ T) {xt : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hsr : SndRunA σ (specPtr t) xt) + {n : U64} (hn : n ∈ N) {x : Notification} (hk : σ.kheap (specPtr n) = some (.notification x)) + (hna : ∀ b, x.ntfnObj ≠ .activeNtfn b) + {xs : List U64} (hQ : QueueRel (L.mem s) T .Ep (qOf (L.mem s) n) xs) + (hxs : x.ntfnObj.ntfnQueue = xs.map specPtr) + (hsorted : PrioSorted (fun y => L.mem s (prioA y)) xs) : + notification.receive_signal st ar s a t n true ⦃ r => + r.2 = a ∧ ∃ σ5, maybeReturnSc (Z := Unit) (specPtr n) (specPtr t) + (blkWaitA σ (specPtr t) (specPtr n) x xt) = ({((), σ5)}, False) ∧ + NtfnStateRel N T (L.mem r.1) σ5 ∧ EpStateRel T E R Bf (L.mem r.1) σ5 ∧ SR r.1 σ5 ∧ + SKeepA (blkWaitA σ (specPtr t) (specPtr n) x xt) σ5 ∧ SSzA (blkWaitA σ (specPtr t) (specPtr n) x xt) σ5 ∧ + WritesIn st s r.1 (WaitFpA (fun σ b => Wr σ s b) σ t n xs) ⦄ := by + have hPn := hP.n + obtain ⟨σ2, hσ2⟩ : ∃ σ2, waitStsA σ (specPtr t) (specPtr n) xt = σ2 := ⟨_, rfl⟩ + obtain ⟨y4, hy4⟩ : ∃ y4, waitNtfnA σ (specPtr t) x xt = y4 := ⟨_, rfl⟩ + have hσ4 : blkWaitA σ (specPtr t) (specPtr n) x xt = updK σ2 (specPtr n) (.notification y4) := by + unfold blkWaitA; rw [hσ2, hy4] + rw [hσ4] + have hxt := hN.at_tcb ht hkt + have hx := hN.at_ntfn hn hk + obtain ⟨hrun, hNQ, hsane, hni⟩ := hsr + have hRB : RelIfBound σ (specPtr t) := hOE.rd _ _ _ _ hSR hkt (Or.inl hrun) + have hnb : ∀ m, xt.tcbState ≠ .blockedOnNotification m := fun m c => by + rw [c] at hrun; simp [runnable] at hrun + have hnm := not_member hN ht hkt hnb + have hnt : specPtr n ≠ specPtr t := fun e => by rw [e, hkt] at hk; cases hk + have htxs : t ∉ xs := fun h => hnm n hn x hk (by rw [hxs]; exact List.mem_map_of_mem h) + have htq := not_queued_codeA hE ht hkt (runnable_codeA hrun) + have hst : stOf (L.mem s) n = obj.NTFN_IDLE ∨ stOf (L.mem s) n = obj.NTFN_WAITING := by + cases h : x.ntfnObj with + | idleNtfn => exact Or.inl (stOf_idle (T := T) (h ▸ hx.obj) rfl) + | waitingNtfn q => exact Or.inr (stOf_waiting (T := T) (h ▸ hx.obj) rfl) + | activeNtfn b => exact absurd h (hna b) + have hBlk : ∀ y ∈ xs, L.mem s (tBlockA y) = n := by + obtain ⟨ys, hQ', hys, hB⟩ := hx.obj.queue + obtain rfl : ys = xs := by + rw [hxs] at hys + exact (List.map_injective_iff.2 (fun a b h => specPtr_inj h) hys).symm + exact fun y hy => (hB y hy).2 + set x1 : Tcb := waitTcbA xt (specPtr n) with hx1 + set σ1 := updK σ (specPtr t) (.tcb x1) with hσ1 + set s2 := L.wr (L.wr s (stA t) sched.TS_BLOCKED_ON_NTFN) (tBlockA t) n with hs2 + obtain ⟨hw1, hoth1⟩ := twr_wr (st := st) hPn ht (K := 3328#u64) (Or.inl rfl) s sched.TS_BLOCKED_ON_NTFN + obtain ⟨hw2, hoth2⟩ := twr_wr (st := st) hPn ht (K := 3336#u64) (Or.inr (Or.inl rfl)) + (L.wr s (stA t) sched.TS_BLOCKED_ON_NTFN) n + rw [← stA_eq] at hw1 hoth1 + rw [← tBlockA_eq] at hw2 hoth2 + have hw := hw1.trans hw2 + have W2 : WritesIn st s s2 (fun b => b = stA t ∨ b = tBlockA t) := + ⟨by simp only [s2, LawfulStore.ksb_wr], by simp only [s2, LawfulStore.cpu_wr], fun b hb hnb' => by + show L.mem (L.wr (L.wr s (stA t) sched.TS_BLOCKED_ON_NTFN) (tBlockA t) n) b = L.mem s b + rw [rd_wr_ne (foot_blk hPn ht).1 hb (Ne.symm fun h => hnb' (Or.inr h)), + rd_wr_ne (foot_st hPn ht).1 hb (Ne.symm fun h => hnb' (Or.inl h))]⟩ + have hN1 : NtfnStateRel N T (L.mem s2) σ1 := by + refine hN.upd_tcb ht _ hw.ntfn hw.tcb hw.links (Or.inl hnm) ⟨?_, ?_, ?_, ?_⟩ + · show (L.mem s2 (stA t)).val = 6 + rw [stA_eq, hoth2 _ (by simp) (by simp) (by simp), ← stA_eq, rd_wr_eq (foot_st hPn ht).1, six_val] + · intro m hm + cases hm + show specPtr (L.mem s2 (tBlockA t)) = specPtr n + rw [rd_wr_eq (foot_blk hPn ht).1] + · show (L.mem s2 (badgeRegA t)).bv = badgeOf xt + rw [badgeRegA_eq, hoth2 _ (by simp) (by simp) (by simp), hoth1 _ (by simp) (by simp) (by simp), + ← badgeRegA_eq]; exact hxt.badge + · show (L.mem s2 (prioA t)).val = xt.tcbPriority.toNat + rw [prioA_eq', hoth2 _ (by simp) (by simp) (by simp), hoth1 _ (by simp) (by simp) (by simp), + ← prioA_eq']; exact hxt.prio + have hSR1 := hH.block _ _ _ _ t hSR hNQ hRB hsane hni hw.foot (NtfnStep.tcb hkt (.blockedOnNotification (specPtr n)) xt.tcbArch) + (fun p x y h1 h2 hp => by + simp only [Function.update_apply, hp, ↓reduceIte] at h2; rw [h1] at h2; cases h2; exact ⟨rfl, id⟩) + -- the endpoint relation across the two words + have hta := hP.e.tcb t ht + have hE1 : EpStateRel T E R Bf (L.mem s2) σ1 := by + have v1 := val_st hta; have v2 := val_blk hta + have hxE := hE.at_tcb ht hkt + have hA : Agree (L.mem s) (L.mem s2) (fun b => EFoot T E R Bf b ∧ ¬ TF t b) := fun b hb hh => by + refine W2.2.2 b hb fun c => ?_ + rcases c with c | c + · exact hh.2 (TF.of_val (a_st hta).2 (by rw [c])) + · exact hh.2 (TF.of_val (a_blk hta).2 (by rw [c])) + refine hE.upd_tcb hP.e ht _ hA (Or.inl htq) ?_ + refine hxE.restateA hta (m' := L.mem s2) (fun b hb hh => ?_) rfl rfl rfl rfl ?_ ?_ ?_ ?_ ?_ + · refine W2.2.2 b hb fun c => ?_ + rcases c with c | c <;> have := congrArg (fun w : U64 => w.val) c <;> have := hh.2 <;> omega + · show (L.mem s2 (stA t)).val = tsCode (.blockedOnNotification (specPtr n)) + rw [stA_eq, hoth2 _ (by simp) (by simp) (by simp), ← stA_eq, rd_wr_eq (foot_st hPn ht).1, six_val] + rfl + · intro e' h; simp [epBlocked] at h + · intro e' d h; cases h + · intro e' r d h; cases h + · intro r h; cases h + have hSE : SR s2 σ1 := hSR1 + have hsts : stsAct σ1 (specPtr t) = σ2 := by rw [← hσ2]; rfl + have hkh2 : σ2.kheap = σ1.kheap := by rw [← hsts, stsAct_kheap] + have hkv : Notif.KView σ1 σ2 := by rw [← hsts]; exact Notif.KView.stsAct _ _ + have hkvE : Endp.KView σ1 σ2 := by rw [← hsts]; exact KView.stsAct' _ _ + have hky : σ2.kheap (specPtr n) = some (.notification x) := by + rw [hkh2, hσ1, updK_kheap, Function.update_of_ne hnt, hk] + have key : (do + let s1 ← obj.Tcb.set_state st t s sched.TS_BLOCKED_ON_NTFN + let s2 ← obj.Tcb.set_block_obj st t s1 n + let s3 ← sched.schedule_tcb st s2 t + let s4 ← notification.ntfn_append st s3 n t + let s5 ← notification.maybe_return_sched_context st s4 n t + ok (s5, a)) ⦃ r => + r.2 = a ∧ ∃ σ5, maybeReturnSc (Z := Unit) (specPtr n) (specPtr t) + (updK σ2 (specPtr n) (.notification y4)) = ({((), σ5)}, False) ∧ + NtfnStateRel N T (L.mem r.1) σ5 ∧ EpStateRel T E R Bf (L.mem r.1) σ5 ∧ SR r.1 σ5 ∧ + SKeepA (updK σ2 (specPtr n) (.notification y4)) σ5 ∧ SSzA (updK σ2 (specPtr n) (.notification y4)) σ5 ∧ + WritesIn st s r.1 (WaitFpA (fun σ b => Wr σ s b) σ t n xs) ⦄ := by + step with obj.Tcb.set_state.spec as ⟨s1', h1'⟩ + step with obj.Tcb.set_block_obj.spec as ⟨s2', h2'⟩ + subst h1' h2' + refine WP.spec_bind (WP.spec_and (WP.spec_and (hOn.scheduleTcb _ σ1 t hN1 hSR1 ht) (hF.scheduleTcb _ σ1 t hN1 hSR1 ht)) + (hOE.scheduleTcb _ σ1 t hE1 hSE ht)) ?_ + rintro s3 ⟨⟨⟨hf3, hS3⟩, hw3⟩, hfE, -⟩ + rw [hsts] at hS3 + have hS3' : SR s3 σ2 := hS3 + have hN3 : NtfnStateRel N T (L.mem s3) σ2 := hN1.frame hPn hf3 hkv + have hE3 : EpStateRel T E R Bf (L.mem s3) σ2 := hE1.frame hP.e hfE hkvE + have hk3 : σ2.kheap (specPtr n) = some (.notification x) := hky + have hkt3 : σ2.kheap (specPtr t) = some (.tcb x1) := by rw [hkh2, hσ1, updK_kheap, Function.update_self] + have hwp : ∀ y ∈ T, y ≠ t → L.mem s2 (prioA y) = L.mem s (prioA y) := fun y hyT hyt => (hw.tcb y hyT hyt).prio + obtain ⟨xs3, hQ3, hxs3, -⟩ := (hN3.at_ntfn hn hk3).obj.queue + have e3 : xs3 = xs := by + rw [hxs] at hxs3 + exact (List.map_injective_iff.2 (fun a b h => specPtr_inj h) hxs3).symm + subst e3 + refine WP.spec_bind (WP.spec_and (block_append hPn s s2 s3 _ ht hn hk3 hkt3 hQ hxs htxs hsorted hwp hf3 hN3) + (ntfn_append_full hPn s3 hn t xs3 hQ3 ht htxs)) ?_ + rintro s4 ⟨⟨hN4, hW4⟩, -, -, hA3⟩ + have hprs : ∀ y ∈ xs3, L.mem s3 (prioA y) = L.mem s (prioA y) := fun y hy => + (hf3.tfields hPn (hQ.mem y hy)).prio.trans (hwp y (hQ.mem y hy) (fun e => htxs (e ▸ hy))) + have hsorted3 : PrioSorted (fun y => L.mem s3 (prioA y)) xs3 := prioSorted_congr hsorted hprs + have hlist := specIns_map (σ2 := σ2) hsorted3 (hN3.at_tcb ht hkt3).prio + (fun y hy => by + obtain ⟨z, hz, hzr⟩ := hN3.tcb y (hQ.mem y hy) + exact ⟨z, hz, hzr.prio⟩) + have hpo : ∀ y ∈ xs3, prioOf σ2 (specPtr y) = prioOf σ (specPtr y) := by + intro y hy + have hne : specPtr y ≠ specPtr t := fun e => htxs (specPtr_inj e ▸ hy) + have : σ2.kheap (specPtr y) = σ.kheap (specPtr y) := by + rw [hkh2, hσ1, updK_kheap, Function.update_of_ne hne] + simp only [prioOf, Fl4Spec.optMapSome, Fl4Spec.optMap, tcbsOf, tcbsOfKh, this] + have hspec : specIns (ntfnQueueOf x) (prioOf σ) xt.tcbPriority (specPtr t) = + specIns (ntfnQueueOf x) (prioOf σ2) xt.tcbPriority (specPtr t) := by + have hq' : ntfnQueueOf x = xs3.map specPtr := hxs + have hc : ∀ z ∈ ntfnQueueOf x, + (fun y => @decide (prioOf σ y ≥ xt.tcbPriority) (Classical.propDecidable _)) z = + (fun y => @decide (prioOf σ2 y ≥ xt.tcbPriority) + (Classical.propDecidable _)) z := by + intro z hz + rw [hq'] at hz + obtain ⟨y, hy, rfl⟩ := List.mem_map.1 hz + exact decide_eq_decide.2 (by rw [hpo y hy]) + unfold specIns + rw [takeWhile_congr' hc, dropWhile_congr' hc] + have hN4' : NtfnStateRel N T (L.mem s4) (updK σ2 (specPtr n) (.notification y4)) := by + obtain ⟨l4, hl4⟩ : ∃ l : List ObjRef, + l = (insPrio (fun y => L.mem s3 (prioA y)) (L.mem s3 (prioA t)) t xs3).map specPtr := ⟨_, rfl⟩ + rw [← hl4] at hN4 hlist + have e4 : y4 = { x with ntfnObj := .waitingNtfn l4 } := by + rw [← hy4] + unfold waitNtfnA + rw [hspec, show ntfnQueueOf x = xs3.map specPtr from hxs, hlist] + rw [e4] + exact hN4 + have hSR4 : SR s4 (updK σ2 (specPtr n) (.notification y4)) := + hH.stable _ _ _ _ hS3' hW4 (NtfnStep.ntfnK (y := y4) hk3 (by rw [← hy4]; rfl) (by rw [← hy4]; rfl)) + (Endp.NoBlockE.ntfnK _ _ _) + have Wa : WritesIn st s3 s4 (fun b => (b = epHeadA n ∨ b = epTSA n) ∨ + ∃ w ∈ t :: xs3, b = nextA .Ep w ∨ b = prevA .Ep w) := + ⟨hA3.foot.1, hA3.foot.2.1, fun b hb hnb' => hA3.agree b hb + (fun w hw => ⟨fun h => hnb' (Or.inr ⟨w, hw, Or.inl h⟩), fun h => hnb' (Or.inr ⟨w, hw, Or.inr h⟩)⟩) + (fun h => hnb' (Or.inl (Or.inl h))) (fun h => hnb' (Or.inl (Or.inr h)))⟩ + have hxT : ∀ w ∈ t :: xs3, w ∈ T := fun w hw => by + rcases List.mem_cons.1 hw with rfl | hw + exacts [ht, hQ.mem w hw] + -- the endpoint relation across `ntfn_append`: no written word is read + have hE4 : EpStateRel T E R Bf (L.mem s4) (updK σ2 (specPtr n) (.notification y4)) := by + have hxq : ∀ w ∈ t :: xs3, ∀ e ∈ E, ∀ z, σ2.kheap (specPtr e) = + some (.endpoint z) → specPtr w ∉ z.epQueue := by + intro w hw e he z hz hm + have hz0 : σ.kheap (specPtr e) = some (.endpoint z) := by + have hne : specPtr e ≠ specPtr t := fun c => by rw [c, hkt3] at hz; cases hz + rwa [hkh2, hσ1, updK_kheap, Function.update_of_ne hne] at hz + rcases List.mem_cons.1 hw with rfl | hw + · exact htq e he z hz0 hm + · obtain ⟨z', hz', hr⟩ := hE.ep e he + rw [hz0] at hz'; cases hz' + have h1 := (hr.blocked hm).2 + rw [hBlk w hw] at h1 + have := hP.ne n hn e he + rw [h1] at this + omega + have off : ∀ b, Aligned b → ¬ (n.val ≤ b.val ∧ b.val < n.val + 40) → + (∀ w ∈ t :: xs3, ¬ TL w b) → L.mem s4 b = L.mem s3 b := fun b hb h1 h2 => by + refine Wa.2.2 b hb ?_ + rintro ((c | c) | ⟨w, hw, c⟩) + · exact h1 (by rw [c, Notif.epHeadA_eq, Notif.nfield_val (hPn.ntfn n hn) (by simp)]; simp) + · exact h1 (by rw [c, Notif.epTSA_eq, Notif.nfield_val (hPn.ntfn n hn) (by simp)]; simp) + · have hwa := hP.e.tcb w (hxT w hw) + rcases c with c | c + · exact h2 w hw (c ▸ (a_next hwa).2) + · exact h2 w hw (c ▸ (a_prev hwa).2) + have offT : ∀ y ∈ T, ∀ b, Aligned b → y.val ≤ b.val → b.val < y.val + 4096 → + (y ∈ t :: xs3 → ¬ TL y b) → L.mem s4 b = L.mem s3 b := by + intro y hy b hb h1 h2 h3 + refine off b hb (fun c => ?_) (fun w hw c => ?_) + · have := hPn.disj n hn y hy; omega + · by_cases e : w = y + · subst e; exact h3 hw c + · have := tcb_disj hP.e (hxT w hw) hy (fun c => e c.symm) + unfold TL at c; omega + have offE : ∀ b, Aligned b → (∃ e ∈ E, b.val = e.val ∨ b.val = e.val + 8) → L.mem s4 b = L.mem s3 b := by + rintro b hb ⟨e, he, c⟩ + refine off b hb (fun d => ?_) (fun w hw d => ?_) + · have := hP.ne n hn e he; omega + · have := hP.e.et e he w (hxT w hw); unfold TL at d; omega + refine ⟨fun e he => ?_, fun r hr => ?_, fun y hy => ?_, fun bp hb k hk' => ?_⟩ + · obtain ⟨z, hz, hr⟩ := hE3.ep e he + have hne : specPtr e ≠ specPtr n := fun c => by rw [c, hk3] at hz; cases hz + refine ⟨z, by rw [updK_kheap, Function.update_of_ne hne]; exact hz, + hr.frame (offE _ (a_head hP.e he).1 ⟨e, he, Or.inl (val_head e)⟩) + (offE _ (a_ts hP.e he).1 ⟨e, he, Or.inr (val_ts hP.e he)⟩) fun y hy hq => ?_⟩ + have hyx : y ∉ t :: xs3 := fun c => hxq y c e he z hz hq + have hya := hP.e.tcb y hy + have k : ∀ b, Aligned b → y.val ≤ b.val → b.val < y.val + 4096 → L.mem s4 b = L.mem s3 b := + fun b hb h1 h2 => offT y hy b hb h1 h2 (fun c => absurd c hyx) + have r1 := val_st hya; have r2 := val_blk hya; have r3 := val_next hya; have r4 := val_prev hya + have := hya.fits + exact ⟨k _ (a_st hya).1 (by omega) (by omega), k _ (a_blk hya).1 (by omega) (by omega), + k _ (a_prev hya).1 (by omega) (by omega), k _ (a_next hya).1 (by omega) (by omega)⟩ + · obtain ⟨z, hz, hz'⟩ := hE3.reply r hr + have hne : specPtr r ≠ specPtr n := fun c => by rw [c, hk3] at hz; cases hz + refine ⟨z, by rw [updK_kheap, Function.update_of_ne hne]; exact hz, ?_⟩ + rw [hz', off _ (a_rtcb hP.e hr).1 (fun c => by + have := hP.nr n hn r hr; rw [Endp.rTcbA_val] at c; omega) (fun w hw c => by + have := hP.e.rt r hr w (hxT w hw); rw [TL, Endp.rTcbA_val] at c; omega)] + · obtain ⟨z, hz, hz'⟩ := hE3.tcb y hy + have hne : specPtr y ≠ specPtr n := fun c => by rw [c, hk3] at hz; cases hz + refine ⟨z, by rw [updK_kheap, Function.update_of_ne hne]; exact hz, + hz'.frame (hP.e.tcb y hy) fun b hb hf => ?_⟩ + have := TF.range hf + exact offT y hy b hb hf.1 (by omega) (fun _ c => TL.not_tf c hf) + · have hbw : BufW Bf (bufW bp.1 k) := ⟨bp, hb, k, hk', rfl⟩ + have e : L.mem s4 (bufW bp.1 k) = L.mem s3 (bufW bp.1 k) := by + refine off _ (hP.e.balign _ hbw) (fun c => ?_) (fun w hw c => ?_) + · have := hP.nb n hn _ hbw; omega + · have := hP.e.bt _ hbw w (hxT w hw) + unfold TL at c; omega + rw [e] + exact hE3.buf bp hb k hk' + -- `maybe_return_sched_context`: the SC return (a no-op or the unbinding) + obtain ⟨Q4, R4, hSc4⟩ := hSRc _ _ hSR4 + have hrq2 : σ2.releaseQueue = σ.releaseQueue := by + have e := waitSts_frameA σ (specPtr t) (specPtr n) xt + rw [hσ2] at e + have e2 := congrArg (fun s : State Unit => s.releaseQueue) e + exact e2 + have hq4 : specPtr t ∉ (updK σ2 (specPtr n) (.notification y4)).releaseQueue := by + show specPtr t ∉ σ2.releaseQueue + rw [hrq2]; exact hNQ.2 + have hssz4 : SSzA σ (updK σ2 (specPtr n) (.notification y4)) := fun q y k h => by + have hqn : q ≠ specPtr n := fun e => by rw [e, updK_kheap, Function.update_self] at h; cases h + have hqt : q ≠ specPtr t := fun e => by + rw [e, updK_kheap, Function.update_of_ne (Ne.symm hnt), hkh2, hσ1, updK_kheap, Function.update_self] at h + cases h + refine ⟨y, ?_⟩ + rw [updK_kheap, Function.update_of_ne hqn, hkh2, hσ1, updK_kheap, Function.update_of_ne hqt] at h + exact h + have hN4Z : NtfnStateRel N Z.T (L.mem s4) (updK σ2 (specPtr n) (.notification y4)) := by rw [hZT]; exact hN4' + have hE4Z : EpStateRel Z.T E R Bf (L.mem s4) (updK σ2 (specPtr n) (.notification y4)) := by rw [hZT]; exact hE4 + have hPnZ : Notif.NPlaced Z.T N := by rw [hZT]; exact hPn + have hPeZ : EPlaced Z.T E R Bf := by rw [hZT]; exact hP.e + have htZ : t ∈ Z.T := by rw [hZT]; exact ht + have hret := maybeReturnIpcA (st := st) (Z := { Z with Q := Q4, R := R4 }) hSc4 hPl hPeZ hPnZ hcm + hN4Z hE4Z hn htZ hq4 + refine WP.spec_bind hret ?_ + rintro s5 ⟨σ5, Q5, hmr, hS5, hN5, hE5, hF5⟩ + simp only [WP.spec_ok] + refine ⟨trivial, σ5, hmr, (by subst hZT; exact hN5), (by subst hZT; exact hE5), hSRi _ _ Q5 R4 hS5, hF5.keep, hF5.sz, ?_⟩ + have hW4 := (W2.trans hw3).trans Wa + refine (hW4.trans hF5.wr).mono fun b h => ?_ + rcases h with ((h | h) | h) | h + · exact Or.inl h + · exact Or.inr (Or.inl (hF.back (ssz_updK_tcb σ (specPtr t) _) W2.1 h)) + · exact Or.inr (Or.inr h) + · exact Or.inr (Or.inl (hF.back hssz4 hW4.1 (hWr _ _ b h))) + unfold notification.receive_signal + step with obj.Ntfn.state.spec as ⟨v, hv⟩ + rw [hv, epState_eq] + rcases hst with hst | hst + · simp only [hst, ↓reduceIte] + exact key + · simp only [hst, ntfn_consts.1, ntfn_consts.2.1, show (1#u64 : U64) ≠ 0#u64 by decide, ↓reduceIte] + exact key + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3RecvWaitSpecScA.lean b/proofs/Fl4ProofsA64/P3/Stage3RecvWaitSpecScA.lean new file mode 100644 index 000000000..ab127f63d --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3RecvWaitSpecScA.lean @@ -0,0 +1,156 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2c): **the spec of a blocking `receive_signal` on a notification whose SC is the +waiter's own**: `maybe_return_sc` really unbinds (passive server returning its SC to the notification). + +* `maybeReturnSc_unbA`: with the notification's SC equal to the thread's SC, `t` the current thread and no pending + switch, `maybe_return_sc` unbinds both sides (`unbStA`) and `reschedule_required` leaves `choose_new_thread`. +* `blkWait_specRetA`: the blocking `receive_signal` is the block (`blkWaitA`) followed by `maybe_return_sc` + (`blkWait_specA` without its SC hypothesis). +* **`blkWaitScA`** / **`blkWaitSc_specA`**: the spec's one outcome when the notification's SC is the waiter's. + +[S] the notification idle or waiting, its SC `sc` is the waiter's SC; the waiter is the current thread and no switch +is pending. No premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3RecvWaitA +import Fl4ProofsA64.P3.ScRetA +import Fl4ProofsA64.P3.ScSwitchA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched + Fl4Spec Fl4SpecA64 + +noncomputable section + +/-- **`maybe_return_sc` unbinds** (module docstring). -/ +theorem maybeReturnSc_unbA {σ : State Unit} {n t sc : ObjRef} {xn : Notification} {xt : Tcb} {xs : SchedContext} + {ns : Nat} (hkn : σ.kheap n = some (.notification xn)) (hkt : σ.kheap t = some (.tcb xt)) + (hsn : xn.ntfnSc = some sc) (hst : xt.tcbSchedContext = some sc) + (hks : σ.kheap sc = some (.schedContext xs ns)) (hNT : sc ≠ t) (hct : t = σ.curThread) + (hns : ∀ u, σ.schedulerAction ≠ .switchThread u) : + maybeReturnSc (Z := Unit) n t σ = + ({((), { unbStA σ sc t xs ns xt with schedulerAction := .chooseNewThread })}, False) := by + unfold maybeReturnSc getTcbObjRef + rw [nd_bind_oneSc (getNtfnSc_eqSc hkn), nd_bind_oneSc (threadGet_eqSc _ hkt), hsn, hst] + rw [Fl4Spec.NonDet.when, if_pos ⟨rfl, by simp⟩] + rw [nd_bind_oneSc (show Fl4Spec.NonDet.assertOpt (σ := State Unit) (some sc) σ = ({(sc, σ)}, False) from rfl)] + rw [nd_bind_oneSc (threadSet_eqSc _ hkt)] + have hks1 : (updKSc σ t (.tcb (tcbSchedContextUpdate (fun _ => none) xt))).kheap sc = + some (.schedContext xs ns) := by simp [updKSc, hNT, hks] + show (updateSchedContext sc _ >>= _) _ = _ + rw [nd_bind_oneSc (Fl4ProofsA64.P3.Sched.updateSchedContext_eqA hks1 _), nd_gets_bindSc] + show Fl4Spec.NonDet.when (t = (unbStA σ sc t xs ns xt).curThread) rescheduleRequired + (unbStA σ sc t xs ns xt) = _ + rw [Fl4Spec.NonDet.when, if_pos (show t = (unbStA σ sc t xs ns xt).curThread from hct)] + exact rescheduleRequired_noswitchA (σ := unbStA σ sc t xs ns xt) (fun u => hns u) + +/-- **A blocking `receive_signal` is the block followed by `maybe_return_sc`** (`blkWait_specA` without its SC +hypothesis). -/ +theorem blkWait_specRetA {σ : State Unit} {t n : U64} {x : Notification} {xt : Tcb} + (hk : σ.kheap (specPtr n) = some (.notification x)) (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hna : ∀ b, x.ntfnObj ≠ .activeNtfn b) + (htq : specPtr t ∉ ntfnQueueOf x) + (hq : ∀ y ∈ ntfnQueueOf x, ∃ z, σ.kheap y = some (.tcb z) ∧ z.tcbPriority = prioOf σ y) + (cb : Badge) (cr : CapRights) : + receiveSignal (Z := Unit) (specPtr t) (.notificationCap (specPtr n) cb cr) true σ = + maybeReturnSc (Z := Unit) (specPtr n) (specPtr t) (blkWaitA σ (specPtr t) (specPtr n) x xt) := by + have hnt : specPtr n ≠ specPtr t := fun e => by rw [e, hkt] at hk; cases hk + obtain ⟨σ2, hσ2⟩ : ∃ σ2, stsAct (updK σ (specPtr t) + (.tcb { xt with tcbState := .blockedOnNotification (specPtr n) })) (specPtr t) = σ2 := ⟨_, rfl⟩ + obtain ⟨y4, hy4⟩ : ∃ y4, waitNtfnA σ (specPtr t) x xt = y4 := ⟨_, rfl⟩ + have hσ4 : blkWaitA σ (specPtr t) (specPtr n) x xt = updK σ2 (specPtr n) (.notification y4) := by + unfold blkWaitA waitStsA waitTcbA; rw [hσ2, hy4] + have hkh : σ2.kheap = Function.update σ.kheap (specPtr t) + (some (.tcb { xt with tcbState := .blockedOnNotification (specPtr n) })) := by + rw [← hσ2, stsAct_kheap, updK_kheap] + have heq : ∀ y ∈ ntfnQueueOf x, σ2.kheap y = σ.kheap y := fun y hy => by + rw [hkh, Function.update_of_ne (show y ≠ specPtr t from fun e => htq (e ▸ hy))] + have hpo : ∀ y ∈ ntfnQueueOf x, prioOf σ2 y = prioOf σ y := fun y hy => by + simp only [prioOf, Fl4Spec.optMapSome, Fl4Spec.optMap, tcbsOf, tcbsOfKh, heq y hy] + have hq2 : ∀ y ∈ ntfnQueueOf x, ∃ z, σ2.kheap y = some (.tcb z) ∧ z.tcbPriority = prioOf σ2 y := fun y hy => by + obtain ⟨z, hz, e⟩ := hq y hy + exact ⟨z, (heq y hy).trans hz, by rw [hpo y hy]; exact e⟩ + have hl : specIns (ntfnQueueOf x) (prioOf σ2) xt.tcbPriority (specPtr t) = + specIns (ntfnQueueOf x) (prioOf σ) xt.tcbPriority (specPtr t) := by + have hc : ∀ z ∈ ntfnQueueOf x, + (fun y => @decide (prioOf σ2 y ≥ xt.tcbPriority) (Classical.propDecidable _)) z = + (fun y => @decide (prioOf σ y ≥ xt.tcbPriority) (Classical.propDecidable _)) z := fun z hz => by + exact decide_eq_decide.2 (by rw [hpo z hz]) + unfold specIns + rw [takeWhile_congr' hc, dropWhile_congr' hc] + obtain ⟨l, hl'⟩ : ∃ l, specIns (ntfnQueueOf x) (prioOf σ) xt.tcbPriority (specPtr t) = l := ⟨_, rfl⟩ + have ey : y4 = { x with ntfnObj := .waitingNtfn l } := by + rw [← hy4]; unfold waitNtfnA; rw [hl'] + rw [block_spec_eq hk hkt hna hσ2 hq2 (l := l) (hl.trans hl') cb cr, hσ4, ← ey] + +/-- **The spec's state after a blocking `receive_signal` whose notification's SC `sc` is the waiter's**: the block, +then both sides of the binding cleared and the scheduler told to choose a new thread. -/ +def unbWaitA (σ4 : State Unit) (t sc : ObjRef) (xs : SchedContext) (ns : Nat) (xt' : Tcb) : State Unit := + { unbStA σ4 sc t xs ns xt' with schedulerAction := .chooseNewThread } + +/-- `blkWaitScA` (module docstring). -/ +def blkWaitScA (σ : State Unit) (t n : ObjRef) (x : Notification) (xt : Tcb) (sc : ObjRef) (xs : SchedContext) + (ns : Nat) : State Unit := + unbWaitA (blkWaitA σ t n x xt) t sc xs ns (waitTcbA xt n) + +/-- **`maybe_return_sc` after the block unbinds** (the second half of `blkWaitSc_specA`). `xs` is the SC object at `sc` +in the spec state before the block. -/ +theorem blkWait_retA {σ : State Unit} {t n : U64} {x : Notification} {xt : Tcb} {sc : ObjRef} {xs : SchedContext} + {ns : Nat} (hk : σ.kheap (specPtr n) = some (.notification x)) (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hsn : x.ntfnSc = some sc) (hst : xt.tcbSchedContext = some sc) + (hks : σ.kheap sc = some (.schedContext xs ns)) (hct : specPtr t = σ.curThread) + (hns : ∀ u, σ.schedulerAction ≠ .switchThread u) : + maybeReturnSc (Z := Unit) (specPtr n) (specPtr t) (blkWaitA σ (specPtr t) (specPtr n) x xt) = + ({((), blkWaitScA σ (specPtr t) (specPtr n) x xt sc xs ns)}, False) := by + have hnt : specPtr n ≠ specPtr t := fun e => by rw [e, hkt] at hk; cases hk + have hNT : sc ≠ specPtr t := fun e => by rw [e, hkt] at hks; cases hks + have hSn : sc ≠ specPtr n := fun e => by rw [e, hk] at hks; cases hks + obtain ⟨σ4, hσ4⟩ : ∃ σ4, blkWaitA σ (specPtr t) (specPtr n) x xt = σ4 := ⟨_, rfl⟩ + have kh : σ4.kheap = Function.update (Function.update σ.kheap (specPtr t) (some (.tcb (waitTcbA xt (specPtr n))))) + (specPtr n) (some (.notification (waitNtfnA σ (specPtr t) x xt))) := by + rw [← hσ4]; unfold blkWaitA; rw [updK_kheap, waitStsA_kheap] + have k1 : σ4.kheap (specPtr n) = some (.notification (waitNtfnA σ (specPtr t) x xt)) := by + rw [kh, Function.update_self] + have k2 : σ4.kheap (specPtr t) = some (.tcb (waitTcbA xt (specPtr n))) := by + rw [kh, Function.update_of_ne (Ne.symm hnt), Function.update_self] + have k3 : σ4.kheap sc = some (.schedContext xs ns) := by + rw [kh, Function.update_of_ne hSn, Function.update_of_ne hNT, hks] + have B := blkWait_stepA (σ := σ) (x := x) (xt := xt) hnt.symm + rw [hσ4] at B + have hct4 : specPtr t = σ4.curThread := by rw [B.curThread]; exact hct + have hns4 : ∀ u, σ4.schedulerAction ≠ .switchThread u := fun u e => by + rcases B.act with h | h + · exact hns u (h.symm.trans e) + · rw [h] at e; cases e + have e5 := maybeReturnSc_unbA (n := specPtr n) (t := specPtr t) (xn := waitNtfnA σ (specPtr t) x xt) + (xt := waitTcbA xt (specPtr n)) k1 k2 hsn hst k3 hNT hct4 hns4 + rw [hσ4, e5] + show _ = ({((), unbWaitA (blkWaitA σ (specPtr t) (specPtr n) x xt) (specPtr t) sc xs ns (waitTcbA xt (specPtr n)))}, + False) + rw [hσ4] + rfl + +/-- **The spec's blocking `receive_signal` on a notification whose SC is the waiter's has this one outcome.** -/ +theorem blkWaitSc_specA {σ : State Unit} {t n : U64} {x : Notification} {xt : Tcb} {sc : ObjRef} {xs : SchedContext} + {ns : Nat} (hk : σ.kheap (specPtr n) = some (.notification x)) (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hna : ∀ b, x.ntfnObj ≠ .activeNtfn b) (hsn : x.ntfnSc = some sc) (hst : xt.tcbSchedContext = some sc) + (hks : σ.kheap sc = some (.schedContext xs ns)) (hct : specPtr t = σ.curThread) + (hns : ∀ u, σ.schedulerAction ≠ .switchThread u) + (htq : specPtr t ∉ ntfnQueueOf x) + (hq : ∀ y ∈ ntfnQueueOf x, ∃ z, σ.kheap y = some (.tcb z) ∧ z.tcbPriority = prioOf σ y) + (cb : Badge) (cr : CapRights) : + receiveSignal (Z := Unit) (specPtr t) (.notificationCap (specPtr n) cb cr) true σ = + ({((), blkWaitScA σ (specPtr t) (specPtr n) x xt sc xs ns)}, False) := by + rw [blkWait_specRetA hk hkt hna htq hq cb cr] + exact blkWait_retA hk hkt hsn hst hks hct hns + +/-- The unique outcome of a singleton step. -/ +theorem single_outA {σa σb : State Unit} (h : (({((), σa)} : Set (Unit × State Unit)), False) = + (({((), σb)} : Set (Unit × State Unit)), False)) : σa = σb := by + have h1 := (Prod.mk.inj h).1 + have h2 := Set.singleton_eq_singleton_iff.1 h1 + exact (Prod.mk.inj h2).2 + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3RecvWaitUnbA.lean b/proofs/Fl4ProofsA64/P3/Stage3RecvWaitUnbA.lean new file mode 100644 index 000000000..e2dd6b1a5 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3RecvWaitUnbA.lean @@ -0,0 +1,220 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2c): **the SC unbinding step of `maybe_return_sc` at the record** (generated by +`handoff/tools/gen_lift2c_waitunb.py`; `ofEntryUnbA` is `BodyOKA.ofEntry` verbatim but for `hobj`). + +`unbWaitA σ4 t sc xs ns xt'` unbinds the thread `t` and the SC `sc` and leaves `choose_new_thread` pending: +* `unbWait_sikaA`: it is `SIKA` with donation allowed (`don = true`); +* `unbWait_specSchedA`, `unbWait_qkeepA`, `unbWait_schedValidA`: `SpecSchedA`, `QKeepA` and `SchedValidA` across it; +* `BodyOKA.ofEntryUnbA`: `BodyOKA.ofEntry` where the current SC may lose its bound TCB. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3RecvWaitSpecScA +import Fl4ProofsA64.P3.Stage3SchedPresA +import Fl4ProofsA64.P3.Stage3IpcSchedValidA +import Fl4ProofsA64.P3.Stage3BodyOKA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched + Fl4Spec Fl4SpecA64 + +noncomputable section + +section +variable {σ4 : State Unit} {t sc : ObjRef} {xt' : Tcb} {xs : SchedContext} {ns : Nat} + +theorem unbWait_kheapA (hNT : sc ≠ t) (p : ObjRef) : + (unbWaitA σ4 t sc xs ns xt').kheap p = if p = t then some (.tcb { xt' with tcbSchedContext := none }) + else if p = sc then some (.schedContext { xs with scTcb := none } ns) else σ4.kheap p := + unbStA_kheap hNT p + +theorem unbWait_casesA (hNT : sc ≠ t) {p : ObjRef} {o : KernelObject} + (h : (unbWaitA σ4 t sc xs ns xt').kheap p = some o) : + (p = t ∧ o = .tcb { xt' with tcbSchedContext := none }) ∨ + (p = sc ∧ o = .schedContext { xs with scTcb := none } ns) ∨ (p ≠ t ∧ p ≠ sc ∧ σ4.kheap p = some o) := by + rw [unbWait_kheapA hNT] at h + by_cases h1 : p = t + · rw [if_pos h1] at h; exact Or.inl ⟨h1, (Option.some.inj h).symm⟩ + · by_cases h2 : p = sc + · rw [if_neg h1, if_pos h2] at h; exact Or.inr (Or.inl ⟨h2, (Option.some.inj h).symm⟩) + · rw [if_neg h1, if_neg h2] at h; exact Or.inr (Or.inr ⟨h1, h2, h⟩) + +theorem unbWait_keepA (hNT : sc ≠ t) {p : ObjRef} (h1 : p ≠ t) (h2 : p ≠ sc) : + (unbWaitA σ4 t sc xs ns xt').kheap p = σ4.kheap p := by + rw [unbWait_kheapA hNT, if_neg h1, if_neg h2] + +theorem unbWait_atTA (hNT : sc ≠ t) : + (unbWaitA σ4 t sc xs ns xt').kheap t = some (.tcb { xt' with tcbSchedContext := none }) := by + rw [unbWait_kheapA hNT, if_pos rfl] + +theorem unbWait_atSA (hNT : sc ≠ t) : + (unbWaitA σ4 t sc xs ns xt').kheap sc = some (.schedContext { xs with scTcb := none } ns) := by + rw [unbWait_kheapA hNT, if_neg hNT, if_pos rfl] + +/-- The unbinding is `SIKA` with donation allowed. -/ +theorem unbWait_sikaA {i : ObjRef} {M N : ObjRef → Prop} (hNT : sc ≠ t) (hkt : σ4.kheap t = some (.tcb xt')) + (hks : σ4.kheap sc = some (.schedContext xs ns)) (hti : t ≠ i) (hsi : sc ≠ idleScPtr) + (hns : ∀ u, σ4.schedulerAction ≠ .switchThread u) : + SIKA i M N true σ4 (unbWaitA σ4 t sc xs ns xt') := by + have K1 : SIKA i M N true σ4 (updKSc σ4 t (.tcb { xt' with tcbSchedContext := none })) := + SIKA.upd hkt (.tcb _ _ ⟨rfl, rfl, rfl, rfl, rfl, rfl, Or.inl rfl, Or.inr ⟨rfl, hti, Or.inr rfl⟩, rfl, rfl⟩) + (idleRdy_nscA (by intro _ _ c; cases c)) + have hks1 : (updKSc σ4 t (.tcb { xt' with tcbSchedContext := none })).kheap sc = some (.schedContext xs ns) := by + simp [updKSc, hNT, hks] + have K2 : SIKA i M N true (updKSc σ4 t (.tcb { xt' with tcbSchedContext := none })) + (updKSc (updKSc σ4 t (.tcb { xt' with tcbSchedContext := none })) sc + (.schedContext { xs with scTcb := none } ns)) := + SIKA.upd hks1 (.sc _ _ _ ⟨rfl, rfl, id, fun h => absurd h hsi, Or.inr ⟨rfl, by simp⟩, fun h => by cases h⟩) + (idleRdy_scA hks1 rfl rfl) + have hns2 : ∀ u, (unbStA σ4 sc t xs ns xt').schedulerAction ≠ .switchThread u := fun u => hns u + have K3 : SIKA i M N true (unbStA σ4 sc t xs ns xt') + { unbStA σ4 sc t xs ns xt' with schedulerAction := .chooseNewThread } := + rescheduleRequired_sikA (i := i) (M := M) (N := N) (don := true) (unbStA σ4 sc t xs ns xt') + ((), { unbStA σ4 sc t xs ns xt' with schedulerAction := .chooseNewThread }) + (by rw [rescheduleRequired_noswitchA hns2]; exact Set.mem_singleton _) + exact K1.trans (K2.trans K3) + +/-- `SpecSchedA` across the unbinding. -/ +theorem unbWait_specSchedA (hNT : sc ≠ t) (hkt : σ4.kheap t = some (.tcb xt')) + (hks : σ4.kheap sc = some (.schedContext xs ns)) : + SpecSchedA σ4 (unbWaitA σ4 t sc xs ns xt') := by + refine ⟨fun p => ?_, rfl, rfl, rfl, rfl, rfl, rfl⟩ + by_cases h1 : p = t + · subst h1 + exact Or.inr (Or.inl ⟨xt', _, hkt, unbWait_atTA hNT, rfl, rfl, rfl⟩) + · by_cases h2 : p = sc + · subst h2 + exact Or.inr (Or.inr ⟨_, _, hks, unbWait_atSA hNT, Or.inl ⟨xs, ns, _, ns, rfl, rfl⟩⟩) + · exact Or.inl (unbWait_keepA hNT h1 h2) + +/-- `QKeepA` across the unbinding. -/ +theorem unbWait_qkeepA (hNT : sc ≠ t) (hkt : σ4.kheap t = some (.tcb xt')) + (hks : σ4.kheap sc = some (.schedContext xs ns)) : + QKeepA σ4 (unbWaitA σ4 t sc xs ns xt') := by + refine ⟨fun u y hy => ?_, fun p x' hy => ?_, fun p x' hy => ?_, fun p y' k hy => ?_, fun p y hy => ?_, + ⟨rfl, rfl, rfl, rfl⟩⟩ + · rcases unbWait_casesA hNT hy with ⟨rfl, h⟩ | ⟨rfl, h⟩ | ⟨-, -, h⟩ + · cases h; exact ⟨xt', hkt, rfl⟩ + · cases h + · exact ⟨y, h, rfl⟩ + · rcases unbWait_casesA hNT hy with ⟨rfl, h⟩ | ⟨rfl, h⟩ | ⟨-, -, h⟩ + · cases h + · cases h + · exact ⟨x', h, List.Sublist.refl _⟩ + · rcases unbWait_casesA hNT hy with ⟨rfl, h⟩ | ⟨rfl, h⟩ | ⟨-, -, h⟩ + · cases h + · cases h + · exact ⟨x', h, List.Sublist.refl _⟩ + · rcases unbWait_casesA hNT hy with ⟨rfl, h⟩ | ⟨rfl, h⟩ | ⟨-, -, h⟩ + · cases h + · cases h; exact ⟨xs, ns, hks, fun h => ⟨h, fun a => a⟩⟩ + · exact ⟨y', k, h, fun h => ⟨h, fun a => a⟩⟩ + · rcases unbWait_casesA hNT hy with ⟨rfl, h⟩ | ⟨rfl, h⟩ | ⟨-, -, h⟩ + · cases h + · cases h + · exact ⟨y, h, id⟩ + +/-- Every SC active before is active after the unbinding. -/ +theorem unbWait_activeA (hNT : sc ≠ t) (hkt : σ4.kheap t = some (.tcb xt')) + (hks : σ4.kheap sc = some (.schedContext xs ns)) {p : ObjRef} (h : ScActiveAtA σ4 p) : + ScActiveAtA (unbWaitA σ4 t sc xs ns xt') p := by + obtain ⟨y, k, hy, a⟩ := h + by_cases h2 : p = sc + · subst h2 + rw [hks] at hy; cases hy + exact ⟨{ xs with scTcb := none }, ns, unbWait_atSA hNT, a⟩ + · by_cases h1 : p = t + · subst h1; rw [hkt] at hy; cases hy + · exact ⟨y, k, by rw [unbWait_keepA hNT h1 h2]; exact hy, a⟩ + +/-- `SchedValidA` across the unbinding of a thread in no queue. -/ +theorem unbWait_schedValidA (hNT : sc ≠ t) (hkt : σ4.kheap t = some (.tcb xt')) + (hks : σ4.kheap sc = some (.schedContext xs ns)) (V : SchedValidA σ4) + (hnq : ∀ d p, t ∉ σ4.readyQueues d p) (hnr : t ∉ σ4.releaseQueue) : + SchedValidA (unbWaitA σ4 t sc xs ns xt') := by + have hscne : ∀ u x, σ4.kheap u = some (.tcb x) → u ≠ sc := fun u x hx e => by rw [e, hks] at hx; cases hx + have tcb : ∀ u x sc', σ4.kheap u = some (.tcb x) → x.tcbSchedContext = some sc' → u ≠ t → + (unbWaitA σ4 t sc xs ns xt').kheap u = some (.tcb x) := fun u x sc' hx _ hut => by + rw [unbWait_keepA hNT hut (hscne u x hx)]; exact hx + refine ⟨fun d p u hm => ?_, fun h => ?_, fun u x sc' hu hb hsc => ?_, fun u hu => ?_⟩ + · obtain ⟨⟨x, sc', hx, es, a⟩, nr⟩ := V.rq d p u hm + have hut : u ≠ t := fun e => hnq d p (e ▸ hm) + exact ⟨⟨x, sc', tcb u x sc' hx es hut, es, unbWait_activeA hNT hkt hks a⟩, nr⟩ + · have e : (unbWaitA σ4 t sc xs ns xt').schedulerAction = .chooseNewThread := rfl + rw [e] at h; cases h + · rcases unbWait_casesA hNT hu with ⟨rfl, h⟩ | ⟨rfl, h⟩ | ⟨h1, h2, h0⟩ + · cases h; cases hsc + · cases h + · exact unbWait_activeA hNT hkt hks (V.blk u x sc' h0 hb hsc) + · obtain ⟨x, sc', hx, es, a⟩ := V.rls u hu + have hut : u ≠ t := fun e => hnr (e ▸ hu) + exact ⟨x, sc', tcb u x sc' hx es hut, es, unbWait_activeA hNT hkt hks a⟩ + +end + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {J5 J7 : JSlotA S} {s s' : S} {a : A} {σ σ' : State Unit} + {m : McState} + +/-- **`BodyOKA.ofEntry` where the current SC may lose its bound TCB** (module docstring). -/ +theorem BodyOKA.ofEntryUnbA (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (hact : σ'.schedulerAction = σ.schedulerAction ∨ σ'.schedulerAction = .chooseNewThread) (htime : σ'.curTime = σ.curTime) + (hlast : σ'.machineState.lastMachineTime = σ.machineState.lastMachineTime) + (hthr : σ'.curThread = σ.curThread) (hsc : σ'.curSc = σ.curSc) (hcon : σ'.consumedTime = σ.consumedTime) + (hrel : σ'.releaseQueue = σ.releaseQueue) + (hobj : ∀ y' k, σ'.kheap σ.curSc = some (.schedContext y' k) → ∃ y, σ.kheap σ.curSc = some (.schedContext y k) ∧ + y'.scRefills = y.scRefills ∧ y'.scRefillMax = y.scRefillMax ∧ (y'.scTcb = y.scTcb ∨ y'.scTcb = none)) + (hrun : σ'.schedulerAction = .resumeCurThread → ∀ x, σ'.kheap σ.curThread = some (.tcb x) → + x.tcbState = .running ∨ x.tcbState = .restart) : + BodyOKA σ' m := by + have hd : ∀ y' y : SchedContext, y'.scRefills = y.scRefills → refillHd y' = refillHd y := fun y' y e => by + unfold refillHd; rw [e] + have ha : ∀ y' y : SchedContext, y'.scRefillMax = y.scRefillMax → scActive y' = scActive y := fun y' y e => by + unfold scActive; rw [e] + refine ⟨⟨?_, fun y' k hk _ _ => ?_, fun h x hx => ?_⟩, fun t e => ?_, fun h => ?_, fun y' k hk _ _ _ o ho => ?_⟩ + · have := J.validTime + unfold ValidMachineTimeA at this ⊢ + rw [htime, hlast]; exact this + · rw [hsc] at hk + obtain ⟨y, hy, e1, e2, -⟩ := hobj y' k hk + constructor + · intro z j hz + rw [hsc, hk] at hz + obtain ⟨rfl, -⟩ := KernelObject.schedContext.inj (Option.some.inj hz) + rw [hd _ _ e1, hcon, htime] + exact J.offReady y k hy + · intro hpos z j hz hactive + rw [hsc, hk] at hz + obtain ⟨rfl, -⟩ := KernelObject.schedContext.inj (Option.some.inj hz) + rw [hd _ _ e1, hcon, htime] + rw [hcon] at hpos + refine J.offOK hpos y k hy fun h => ?_ + rw [← ha _ _ e2]; exact hactive (hsc ▸ h) + · rcases hrun h x (hthr ▸ hx) with h | h <;> rw [h] + · exact Or.inl rfl + · exact Or.inr (Or.inl rfl) + · rcases hact with h | h + · rw [h, J.act] at e; cases e + · rw [h] at e; cases e + · rw [hsc] at h ⊢; exact J.curScNotIdle h + · rw [hsc] at hk + obtain ⟨y, hy, -, -, e3⟩ := hobj y' k hk + obtain ⟨x, hx, hxs, -⟩ := J.curInv.tcb + obtain ⟨z, j, hz, hzt⟩ := J.sr.inv.back _ x _ hx hxs + rw [hy] at hz + obtain ⟨rfl, -⟩ := KernelObject.schedContext.inj (Option.some.inj hz) + rcases e3 with e3 | e3 + · rw [e3, hzt] at ho + rw [hrel, ← Option.some.inj ho] + exact J.curInv.notRel + · rw [e3] at ho; cases ho + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3ReplyArmA.lean b/proofs/Fl4ProofsA64/P3/Stage3ReplyArmA.lean new file mode 100644 index 000000000..4724af5a5 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ReplyArmA.lean @@ -0,0 +1,456 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2b): **the live Reply arm at the record**: Reply on a reply object whose caller is +`BlockedOnReply` on it. + +* **`JStage3A.ntfnWait_replyA`**: `NtfnWaitKeepMA` from the live Reply's narrow write set (`ReplyNwA`). +* **`JSyscallA.replyLiveA`**: `decode_invocation` on a Reply cap: the message is transferred, the reply object is + popped (the SC goes back to a caller without one) and the caller runs. +* **`JSyscallA.replyLiveDispA`**: the same arm in the dispatch top's shape (`DispPostA`). + +The spec data (reply object, grant, caller) come from the cap read at the entry (`hcap`) and the spec reply object +(`hk` / `hcl`); no free datum. + +[S] (scope selectors) `hsnd` (the replier has no fault and no extra caps), `hrcv` (the caller waits `BlockedOnReply` +and has no fault); `h1` (`numNodes = 1`: the SC holder is local). The caller's timeout handler may be an endpoint cap +(the SC tail's nested timeout send, `JStage3A.replyTmoA`): it names an endpoint of `E` (`JStage3A.tmoHandlerEpA`). +[P] `ArchTotal`, `MaxSpi`, `ArchPlat`, `A64FrameMasks`, `hCs`, `hmb` (`Fl4MinBudgetAgreeA`), `hmax`, `htu`, `hconv`. +New module. +-/ +import Fl4ProofsA64.P3.Stage3ReplyExitA +import Fl4ProofsA64.P3.Stage3ReplyLiveKeepA +import Fl4ProofsA64.P3.Stage3ReplyTmoDispA +import Fl4ProofsA64.P3.Stage3SendBlkArmA +import Fl4ProofsA64.P3.ReplyHookStallA +import Fl4ProofsA64.P3.ReplyCallerA +import Fl4ProofsA64.P3.ReplyNotIdleA +import Fl4ProofsA64.P3.Stage3ReplyDetachedA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {J5 : JSlotA S} {R : Set U64 → JSlotA S} {s s' : S} {a : A} {σ : State Unit} {m : McState} + +/-- **`NtfnWaitKeepMA`** from the live Reply's write set: the threads waiting on a notification are not the caller, so +no `Ep` link or block word of theirs is written. -/ +theorem JStage3A.ntfnWait_replyA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7A st R) s a σ m) + {r t : U64} (hr : r ∈ Rp) (ht : t ∈ Z.T) + (w : WritesIn st s s' (ReplyNwA (st := st) Z m σ s Rp r t Bf)) : + NtfnWaitKeepMA (fun o => o = specPtr t) Z.T N (L.mem s) (L.mem s') := by + intro y hy hM hN + have hP := J.iplaced + have hya := hP.e.tcb y hy + have hfy := hya.fits + have hyt : y ≠ t := fun c => hM (by rw [c]) + have apart : t.val + 4096 ≤ y.val ∨ y.val + 4096 ≤ t.val := by + have := J.sr.place.apart t y .tcb .tcb ((J.sr.thT t).2 ht) ((J.sr.thT y).2 hy) hyt.symm + have hsz : ObjKindA.size .tcb = 4096 := rfl + omega + have key : ∀ (K : U64) (b : U64), (K.val = 3336 ∨ K.val = 3496 ∨ K.val = 3504) → b = wa y K → Aligned b → + L.mem s' b = L.mem s b := by + intro K b hK hb hal + have hbv : b.val = y.val + K.val := by rw [hb, wa_valm]; exact Nat.mod_eq_of_lt (by omega) + refine w.mem_eq hal ?_ + rintro ((c | c | c | c) | c | c) + · obtain ⟨q, hq, c⟩ := c + have h1 := hP.e.rt q hq y hy + have h2 := replyNextPlaceA (Z := Z) hP.e hq + have h3 := replyPrevPlaceA (Z := Z) hP.e hq + rcases c with c | c <;> rw [c] at hbv <;> omega + · have h1 := hP.e.rt r hr y hy + have h2 : b.val = r.val := by rw [c, Endp.rTcbA_val] + omega + · have h1 : b.val = t.val + 3328 := by rw [c]; exact Endp.val_st (hP.e.tcb t ht) + omega + · rw [hb] at c + exact J.tcbWord_offMwrA hy (by omega) (by omega) c + · obtain ⟨i, c⟩ := c + have h1 := regIdxA_lt i + rw [Endp.regA_val (hP.e.tcb t ht) h1] at c + omega + · have := hP.e.bt _ c y hy; omega + refine ⟨?_, ?_, ?_⟩ + · exact key 3336#u64 _ (by simp) (by unfold Notif.tBlockA; rfl) (Endp.a_blk hya).1 + · exact key 3504#u64 _ (by simp) (by rw [prevEp_eq]) (Endp.a_prev hya).1 + · exact key 3496#u64 _ (by simp) (by rw [nextEp_eq]) (Endp.a_next hya).1 + +set_option maxHeartbeats 2000000 in +/-- **Reply to a caller blocked on the reply object, at a syscall entry** (module docstring). -/ +theorem JSyscallA.replyLiveXA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hag : fl4MinBudget = MIN_BUDGET) (hmb : ∀ a, Fl4MinBudgetAgreeA ar a) + {r slot lbl len buf : U64} {grant blk call don fp : Bool} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Reply r grant) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hr : r ∈ Rp) {xr : Reply} (hk : σ.kheap (specPtr r) = some (.reply xr)) {d : U64} + (hcl : xr.replyTcb = some (specPtr d)) + (hsnd : ∀ x, σ.kheap σ.curThread = some (.tcb x) → x.tcbFault = none ∧ (grant = false ∨ + ipcBufOfA false x = none ∨ + unat (dataToMessageInfo (x.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0)) + (hrcv : ∀ y, σ.kheap (specPtr d) = some (.tcb y) → + y.tcbState = .blockedOnReply (specPtr r) ∧ y.tcbFault = none) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) (hconv : Fl4TicksToUsLegacyAgree) : + ∃ xc σ1 σ2 s2, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ((), σ2) ∈ (fl4DoReplyTransfer (Z := Unit) σ.curThread (specPtr r) grant σ1).1 ∧ + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don fp buf extra = + ok (.Ok (), s2, a) ∧ + Sched.WakeEndA σ σ2 ∧ + ∃ Q Rl stack', JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack' κ n + (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s2 a σ2 m ∧ + ((∀ y, σ.kheap (specPtr d) = some (.tcb y) → isEpCap y.tcbTimeoutHandler = false) → + ∃ σ4, Sched.SIKA σ1.idleThread (· = specPtr d) (· = specPtr d) true σ1 σ4 ∧ σ2.kheap = σ4.kheap ∧ + (∀ x, σ4.kheap (specPtr d) = some (.tcb x) → x.tcbState = .running) ∧ + (∀ u, σ4.schedulerAction ≠ .switchThread u) ∧ Sched.ReplyExitA (specPtr d) (specPtr r) σ1 σ4 σ2) := by + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hslot hcov J.actW J.running J.cur + have hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM := J.toJStage3EntryA.curNotVmA + obtain ⟨xc, hkc, hst, hsp, J1, -, hOS1, -⟩ := J.toJStage3A.of_restartCurA + RS.pl.choose_spec.choose_spec.choose_spec.choose_spec RS.sym RS.run hnvm RS.cur RS.vs RS.vc + have hst1 : L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART) (stA (L.curW s)) = sched.TS_RESTART := + rd_wr_eq (by rw [stA_eq]; exact field_aligned (J.sr.core.tcbs _ J.sr.curT) (by simp) (by simp)) + generalize hs1 : L.wr s (stA (L.curW s)) sched.TS_RESTART = s1 at hst J1 hOS1 hst1 + set t := L.curW s with ht_def + set σ1 := stUpdA σ σ.curThread xc .restart with hσ1 + have kb1 : L.ksb s1 = L.ksb s := by rw [← hs1]; exact L.ksb_wr _ _ _ + have ct : σ.curThread = specPtr t := J.j.1.sch.curThread + have ct1 : σ1.curThread = specPtr t := ct + have htZ : t ∈ Z.T := J.sr.curT + set xs : Tcb := { xc with tcbState := .restart } with hxs + have hks : σ1.kheap (specPtr t) = some (.tcb xs) := by + rw [hσ1, ← ct]; simp [stUpdA, setObjA, hxs] + have ne1 : specPtr r ≠ σ.curThread := fun e => by rw [e, hkc] at hk; cases hk + have hk1 : σ1.kheap (specPtr r) = some (.reply xr) := by + rw [hσ1]; simp only [stUpdA, setObjA, Function.update_of_ne ne1]; exact hk + -- the caller + have hH : H r = some .reply := J.utIpc.reply r hr + have hw : L.mem s1 (Endp.rTcbA r) ≠ 0#u64 ∧ L.mem s1 (Endp.rTcbA r) = d := by + obtain ⟨y, hy, he⟩ := J1.ep.reply r hr + rw [hk1] at hy + obtain rfl := KernelObject.reply.inj (Option.some.inj hy) + obtain ⟨h0, e⟩ := optRef_some (he.symm.trans hcl) + exact ⟨h0, specPtr_inj e⟩ + have J1T := J1.toTA ⟨fun u h => absurd h (J.noSwitch u), ⟨xs, by rw [ct1]; exact hks, Or.inr (Or.inl rfl)⟩⟩ + J.noSwitch + have hd : d ∈ Z.T := hw.2 ▸ (J1T.replyCallerA hH hw.1).1 + obtain ⟨xd, hkdσ, -⟩ := J.ep.tcb d hd + obtain ⟨hstd, hfd⟩ := hrcv xd hkdσ + have htd : t ≠ d := by + intro e + have hky : σ.kheap (specPtr t) = some (.tcb xc) := ct ▸ hkc + have hr' : runnable xc.tcbState = true := (runnable_iffA ((J.ep.at_tcb htZ hky).st)).1 J.running + rw [e, hkdσ] at hky + cases hky + rw [hstd] at hr' + cases hr' + have hdt : specPtr d ≠ specPtr t := fun h => htd (specPtr_inj h).symm + have hkd : σ1.kheap (specPtr d) = some (.tcb xd) := by + rw [hσ1, ct]; simp only [stUpdA, setObjA, Function.update_of_ne hdt]; exact hkdσ + have hsndx : xs.tcbFault = none ∧ (grant = false ∨ ipcBufOfA false xs = none ∨ + unat (dataToMessageInfo (xs.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0) := hsnd xc hkc + have hq : ∀ e ∈ E, ∀ z, σ1.kheap (specPtr e) = some (.endpoint z) → specPtr d ∉ z.epQueue := by + intro e he z hz hm + obtain ⟨z0, hz0, rel⟩ := J1.ep.ep e he + rw [hz] at hz0 + cases hz0 + have h5 : (L.mem s1 (Fl4ProofsA64.P1.stA d)).val = 5 := by + rw [(J1.ep.at_tcb hd hkd).st, hstd]; rfl + cases z with + | idleEP => simp [Endpoint.epQueue] at hm + | sendEP q => + obtain ⟨-, xs', -, rfl, -, hq⟩ := rel + have := (hq d (mem_map_specPtrSc.1 hm)).1 + omega + | recvEP q => + obtain ⟨-, xs', -, rfl, -, hq⟩ := rel + have := (hq d (mem_map_specPtrSc.1 hm)).1 + omega + -- the removal's locality at one core + have hlocal : ∀ sc ∈ Z.C, (stack sc).head? = some r → xd.tcbSchedContext = none → + ReplyPopLocalQA (st := st) (ar := ar) (Z := Z) (m := m) a s1 σ1 sc d r := by + intro sc hsc hh _ + obtain ⟨rest, hl⟩ : ∃ rest, stack sc = r :: rest := by + cases ee : stack sc with + | nil => rw [ee] at hh; cases hh + | cons c l => + rw [ee] at hh + cases hh + exact ⟨l, rfl⟩ + obtain ⟨⟨y, k, hy, hL⟩, -⟩ := J1.reply.stackRel sc hsc + have cur : ∀ c < fl4A64Platform.numNodes, c = m.curCore := fun c h => + (Nat.lt_one_iff.1 (h1 ▸ h)).trans (Nat.lt_one_iff.1 (h1 ▸ J1T.curCore_lt hCs)).symm + have JL : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7LA st (J7R2A st ar hPl.ipa)) s1 a σ1 m := { J1T with j7 := J1T.j7.toLA } + refine ⟨notIdleSc_of_repliesA J1.sidle cur hy (by rw [hL, hl]; simp), ?_, ?_⟩ + · rw [JL.onCore_single hCs h1 hd, J1.sr.scCore sc hsc] + have a1 := J1.scCpu sc hsc + have a2 := hCs _ J1.j.1.sch.cpuCs + exact UScalar.eq_of_val_eq (by omega) + · by_cases hz : L.mem s1 (scTcbA sc) = 0#u64 + · exact Or.inl hz + have hscH : sc ∈ H.scs := J1.scs.symm ▸ hsc + have hX := JL.blkA.2.2.1.2.2 + have hf : L.mem s1 (scTcbA sc) ∈ Z.T := J1.tcbs ▸ hX.yt.1 sc hscH hz + exact Or.inr ⟨_, hf, hX.tsc sc hscH (J1.scNZ sc hsc) hz, JL.onCore_single hCs h1 hf, + fun s2 hrun => JL.replyStallA hCs h1 hsc hl hf hrun⟩ + have unb : ∀ s2 σ2 Q1 R1, SchedSRA st { Z with Q := Q1, R := R1 } m κ s2 σ2 → RemKeepA σ1 σ2 → + replyRemove (Z := Unit) (specPtr d) (specPtr r) σ1 = ({((), σ2)}, False) → + (L.mem s2 (scA d) ≠ 0#u64 → L.mem s2 (scCoreA (L.mem s2 (scA d))) = L.cpu s2) ∧ + (specPtr d ∈ σ2.releaseQueue → ∀ y n, + σ2.kheap (scNameA m (L.mem s2 (scA d))) = some (.schedContext y n) → + refillReady σ2.curTime (refillHd y) = false) := by + intro s2 σ2 Q1 R1 H2 _ hu + refine ⟨fun h0 => ?_, fun hmem => ?_⟩ + · have hsc : L.mem s2 (scA d) ∈ Z.C := H2.rest.scMem d hd h0 + rw [H2.scCore _ hsc] + have a1 := J1.scCpu _ hsc + have a2 := hCs _ H2.sch.cpuCs + exact UScalar.eq_of_val_eq (by omega) + · obtain ⟨y, hy, hys⟩ := replyRemove_inactiveA hu + exact absurd hmem (H2.notQueued hy (by rw [hys]; rfl)).2 + have hoff := J1.slotOffEwA + have hnq := J1.sr.notQueued hkd (by rw [hstd]; rfl) + -- the tail's locality at the reached state + have tailS : ∀ (s4 : S) (σ4 : State Unit) Q4 R4 (x : Tcb), + SchedSRB st { Z with Q := Q4, R := R4 } m κ s4 σ4 (specPtr d) → + σ4.kheap (specPtr d) = some (.tcb x) → BoundActive (specPtr d) σ4 → + SIKA σ1.idleThread (· = specPtr d) (· = specPtr d) true σ1 σ4 → + WritesIn st s1 s4 (ReplyFootW (st := st) Z m σ1 s1 E Rp Bf) → + L.mem s4 (affA d) = L.cpu s4 ∧ + (L.mem s4 (scA d) ≠ 0#u64 → L.mem s4 (scCoreA (L.mem s4 (scA d))) = L.cpu s4) ∧ + (∀ sc y n, L.mem s4 (scA d) = sc → sc ≠ 0#u64 → + σ4.kheap (scNameA m sc) = some (.schedContext y n) → scActive y = true) ∧ + faults.valid_timeout_handler st s4 d = ok (isEpCap x.tcbTimeoutHandler) := by + intro s4 σ4 Q4 R4 x H4 hx4 B4 K4 w4 + have a2 := hCs _ H4.sch.cpuCs + refine ⟨?_, fun h0 => ?_, fun sc y k he h0 hy => ?_, ?_⟩ + · have a1 := hCs _ (H4.core.aff d hd) + exact UScalar.eq_of_val_eq (by omega) + · have hsc : L.mem s4 (scA d) ∈ Z.C := H4.rest.scMem d hd h0 + rw [H4.scCore _ hsc] + have a1 := J1.scCpu _ hsc + exact UScalar.eq_of_val_eq (by omega) + · obtain ⟨z, hz, hzs⟩ := H4.sch.tcb d hd + have ez : z = x := KernelObject.tcb.inj (Option.some.inj (hz.symm.trans hx4)) + rw [ez] at hzs + have hb : x.tcbSchedContext = some (scNameA m sc) := by + rw [hzs, he, optScA, if_neg h0] + rfl + obtain ⟨z, c, hz, hzc, y', k', hy', act⟩ := B4 x _ hx4 hb + rw [hx4] at hz + cases hz + rw [hb] at hzc + cases hzc + rw [hy] at hy' + cases hy' + exact act + · have htox : x.tcbTimeoutHandler = xd.tcbTimeoutHandler := by + obtain ⟨z, hz, g⟩ := K4.tcbF hkd + rw [hx4] at hz + cases hz + have one : ∀ z : Tcb, tcbCnodeMap z (tcbCnodeIndex 4) = some z.tcbTimeoutHandler := fun z => by + simp [tcbCnodeMap, tcbIdx_eq, tcbStage2Slot, tcbHighVSpaceSlot] + have e : x.tcbTimeoutHandler = xd.tcbTimeoutHandler := + Option.some.inj ((one x).symm.trans ((congrFun g.caps (tcbCnodeIndex 4)).trans (one xd))) + exact e + have hdth : Z.th d = some .tcb := (J1.sr.thT d).2 hd + have hsl : IsSlotA Z.th (slotIdx d layout.tcb.SLOT_TIMEOUT_HANDLER) := + ⟨d, Or.inr ⟨hdth, 4, by unfold tcbSlotsA; omega, by + rw [slotIdx_addrA] + unfold layout.tcb.SLOT_TIMEOUT_HANDLER + rfl⟩⟩ + have keep := slotWordsKeepA J1.sr.toSchedSRN J1.iplaced.e J1.ipcPlace hoff w4 + (Fl4ProofsA64.P3.Sched.IrqSlotsKeepA.of_replyFoot J1.sr.toSchedSRN J1.iplaced.e J1.ipcPlace w4) _ hsl + have ecap : capA st s4 (slotIdx d layout.tcb.SLOT_TIMEOUT_HANDLER) = + capAtA (L.mem s1) (slotIdx d layout.tcb.SLOT_TIMEOUT_HANDLER) := by + show capAtA (L.mem s4) _ = _ + unfold capAtA + rw [keep.1, keep.2] + have rel : CapRelA (capAtA (L.mem s1) (slotIdx d layout.tcb.SLOT_TIMEOUT_HANDLER)) xd.tcbTimeoutHandler := by + obtain ⟨z, hz, h⟩ := J1.j.1.st.tcb d hdth + rw [hkd] at hz + cases hz + obtain ⟨ac, hac, h⟩ := h 4 (by unfold tcbSlotsA; omega) + have one : tcbCnodeMap xd (tcbCnodeIndex 4) = some xd.tcbTimeoutHandler := by + simp [tcbCnodeMap, tcbIdx_eq, tcbStage2Slot, tcbHighVSpaceSlot] + rw [one] at hac + cases hac + have es : slotIdx d layout.tcb.SLOT_TIMEOUT_HANDLER = slotAddrA (specPtr d) (BitVec.ofNat 64 4) := by + rw [slotIdx_addrA] + unfold layout.tcb.SLOT_TIMEOUT_HANDLER + rfl + rw [es] + exact h + rw [htox] + exact validTimeoutHandler_relA (by rw [ecap]; exact rel) + have hidle' : ∀ c < fl4A64Platform.numNodes, specPtr d ≠ m.idleThreadOf c := fun c hc e => by + have hcc : c = m.curCore := (Nat.lt_one_iff.1 (h1 ▸ hc)).trans (Nat.lt_one_iff.1 (h1 ▸ J1T.curCore_lt hCs)).symm + have ei : m.idleThreadOf c = σ1.idleThread := by + rw [hcc, J1.sr.sch.core, J1.sr.sch.idleOf _ J1.sr.sch.cpuCs, J1.sr.sch.idle] + rfl + obtain ⟨y, hy, hs⟩ := J1.sinv.idleTcb + rw [← ei, ← e, hkd] at hy + cases hy + rw [hstd] at hs + cases hs + have keepA := do_reply_transfer_keepTA (st := st) (ar := ar) (a := a) (grant := grant) (n := n) + (RT := fun s' a' σ' => ReplyTmoOutA st ar AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf κ n hPl.ipa m a s' a' σ' ∧ + Sched.RepEndA σ1 σ' ∧ + ∃ y, σ.kheap (specPtr d) = some (.tcb y) ∧ isEpCap y.tcbTimeoutHandler = true) J1.sr J1.ipcPlace + J1.iplaced.e J1.ep J1.reply hr htZ hd htd hkd hk1 hcl hstd hks hq (J1.scCov.reply _) + (fun sc hsc _ => J1.scNZ sc hsc) hlocal unb hoff J1.j.1.st J1.j.1.cv J1.bufCover J1.bufNative hsndx.1 hsndx.2 + (J1.bufFrameA hd hkd true) (J1.bufAlign0A hd hkd true) hag hmb + hidle' + hfd + (fun sc hsc y k hy => by + obtain ⟨y', k', hy', hb⟩ := J1.sr.inv.back (specPtr d) xd sc hkd hsc + rw [hy] at hy' + cases hy' + exact hb) + J1.sinv J1.sidle J1.schedValid J1.activeReplyScs J.noSwitch hnq.1 (by rw [ct1]; exact hdt) + ⟨fun u e => J.sact.f1 u e, ⟨xs, by rw [ct1]; exact hks, Or.inr (Or.inl rfl)⟩⟩ tailS + (fun s4 σ4 Q4 R4 stack4 x MD sc y k badge hsc h0 hy _ hep => by + have htm : ∃ y, σ.kheap (specPtr d) = some (.tcb y) ∧ isEpCap y.tcbTimeoutHandler = true := by + obtain ⟨z, hz, g⟩ := MD.keep.tcbF hkd + rw [MD.kx] at hz + cases hz + have one : ∀ z : Tcb, tcbCnodeMap z (tcbCnodeIndex 4) = some z.tcbTimeoutHandler := fun z => by + simp [tcbCnodeMap, tcbIdx_eq, tcbStage2Slot, tcbHighVSpaceSlot] + refine ⟨xd, hkdσ, ?_⟩ + rw [← Option.some.inj ((one x).symm.trans ((congrFun g.caps (tcbCnodeIndex 4)).trans (one xd)))] + exact hep + refine WP.spec_mono (J1.replyTmoA hmax htu hconv hCs h1 J.noSwitch (fun z hz => by rw [ct1, hks] at hz; cases hz; rfl) hd + (by rw [ct1]; exact hdt) hidle' hkd hstd (J.toJStage3A.tmoHandlerEpA hd hkdσ) MD (J1.ntfnWait_replyA hr hd MD.wn) + (J1.replyTmoRtA hr hd hw.2 (MD.wn.mono fun _ h => Or.inl h)) badge hsc h0 hy hep) + fun _ ⟨σ', hm, hr', hre⟩ => ⟨σ', hm, hr', hre, htm⟩) + -- P1's invariants after the transfer + obtain ⟨Pt, Ub, Pp7, Tb7, Hr, Te, Kt, Lv7, Re, bits, KLv, W⟩ := J1.j7.unp + have I := J1.delIn hA hPl W + have D := J.decPreA hslot hext + have htH : t ∈ H.tcbs := by rw [J.tcbs]; exact htZ + have kt := spec_at (sched.set_thread_state.restart_blk (Es := ∅) s t (BXI.blk J.sys.ent.bxi) + htH (runnable_ok J.running).1 (runnable_ok J.running).2) hst + have kt1 := kt.2.2.2 + have P := (J.toJStage3A.sysPlace hext hslot hcov).keepO hOS1 kb1 + have p1 := Fl4ProofsA64.P1.W7.R.ST.ipc.do_reply_transfer.rpw (st := st) (ar := ar) (Vc := Z.V) (s := s1) hA a t r grant P + J1.sys.sysl J1.symL kt.1 I.del.sca I.ts (by rw [kb1]; exact J.ko) I.del.vr hH htH + (D.rv.restart kt1 (D.rv.zero htH D.nvm)) + obtain ⟨⟨s2, a2⟩, eb, ⟨⟨σ2, hmem, hor⟩, p1post⟩⟩ := exists_of_specA (WP.spec_and keepA p1) + simp only [WP.uncurry'_pair] at eb hor p1post + have ha2 : a2 = a := by + rcases hor with ⟨_, h, _⟩ | ⟨⟨h, _⟩, _⟩ <;> exact h + subst a2 + have hdec : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don fp buf extra = + ok (.Ok (), s2, a) := by + rw [hcap, decodeReply_programA] + simp only [ipcInvokeProgramA, cur_thread_ok, bind_ok, ← ht_def, hst] + rw [show ipc.do_reply_transfer st ar s1 a t r grant = ok (s2, a) from eb] + simp only [bind_ok] + rfl + have F := J.decodeFinA hA hM hPl hfm hslot hcov (rfl : capA st s slot = capA st s slot) hext hSC (lbl := lbl) + (by rw [hcap]; unfold DecArmS1aEnt DecArmS1aC; simp) + (fun _ h => by rw [hcap] at h; cases h) (fun _ h => by rw [hcap] at h; cases h) hdec σ2 m + obtain ⟨-, -, -, -, -, userOK, j7⟩ := F + rcases hor with ⟨stack', -, ⟨Q, Rl, sr2⟩, wD, -, E2, rs2, sub, K, sinv, sidle, sv, Qs, + ⟨y2, hif, hib, U2⟩, wN, ⟨σ4, K4, hk4, E4, run4, ns4, X4⟩, hrepK⟩ | + ⟨⟨-, Q, Rl, stack', kT⟩, hrepT, yt, hyt, htm⟩ + swap + · exact ⟨xc, σ1, σ2, s2, hkc, rfl, hsp, by rw [ct]; exact hmem, hdec, Sched.WakeEndA.ofRep hkc hrepT, Q, Rl, stack', + kT (BlkOK.bxi p1post.2.2.1) p1post.1 p1post.2.1 userOK j7, + fun hnt => by rw [hnt yt hyt] at htm; cases htm⟩ + have wI : WritesIn st s1 s2 (fun b => IpcFpA st Z E Rp Bf m σ1 s1 b ∨ RepWA Rp b) := wD.mono fun b h => by + rcases h with h | h | ⟨q, hq, h⟩ + · exact Or.inl (Or.inl h) + · exact Or.inl (Or.inr h) + · have h2 := replyNextPlaceA (Z := Z) J1.iplaced.e hq + have h3 := replyPrevPlaceA (Z := Z) J1.iplaced.e hq + refine Or.inr ⟨q, hq, ?_⟩ + rcases h with h | h <;> rw [h] <;> omega + have wS : WritesIn st s1 s2 (fun b => SchedStepNWA (st := st) N E Rp Z m σ1 s1 b ∨ BufW Bf b) := + wD.mono fun b h => replyFootW_wA J1.iplaced.e h + have wk := J1.ntfnWait_replyA hr hd wN + have after : ∀ u ∈ Z.T, specPtr u = specPtr d → ∀ x, σ4.kheap (specPtr u) = some (.tcb x) → + epBlocked x.tcbState = none ∧ ∀ p, x.tcbState ≠ .blockedOnNotification p := by + intro u _ hu x hx + rw [run4 x (hu ▸ hx)] + exact ⟨rfl, fun _ h => by cases h⟩ + have hMv : ∀ u ∈ Z.T, specPtr u = specPtr d → L.mem s1 (stA u) ≠ sched.TS_RUNNING_VM := by + intro u _ hu h + rw [specPtr_inj hu] at h + have h5 : (L.mem s1 (stA d)).val = 5 := by rw [(J1.ep.at_tcb hd hkd).st, hstd]; rfl + rw [h] at h5 + revert h5 + unfold sched.TS_RUNNING_VM + simp + have ntfn4 := J1.ntfn_ipcMA wI K4 E4 wk (fun u _ hu x hx p h => by + rw [show specPtr u = specPtr d from hu, hkd] at hx + cases hx + rw [hstd] at h + cases h) (fun u hu hm x hx => (after u hu hm x hx).2) + have ntfn : NtfnStateRel N Z.T (L.mem s2) σ2 := + ⟨fun q hq => by rw [hk4]; exact ntfn4.ntfn q hq, fun u hu => by rw [hk4]; exact ntfn4.tcb u hu⟩ + have blk4 := J1.blkQ_ipcMA K4 E4 wk after + have blkQ : BlkQueuedA Z.T E N (L.mem s2) σ2 := + ⟨fun u hu x hx => by rw [hk4] at hx ⊢; exact blk4.ep u hu x hx, + fun u hu x hx => by rw [hk4] at hx ⊢; exact blk4.ntfn u hu x hx⟩ + have hbuf : ipcBufOfA true y2 = ipcBufOfA true xd := by unfold ipcBufOfA; rw [hif, hib] + have J2 := J1.of_ipcStepUA (Q := Q) (Rl := Rl) (curOf := Z.curOf) (curScOf := Z.curScOf) wS U2.1 U2.2.1 Qs + (J1.specZero_of_recvBufA hd hkd (fun x hx => U2.2.2 x (hbuf.symm ▸ hx))) (fun _ _ => rfl) (BlkOK.bxi p1post.2.2.1) + p1post.1 p1post.2.1 userOK j7 sr2 E2 ntfn rs2 + (fun q hq dead c hc hm => J1.deadExt.reply q hq dead c hc (sub c hc q hm)) K (J1.tw_ipcA wI) sinv sidle + (J1.runVcpu_ipcA wI K4 E4 hMv) blkQ sv + exact ⟨xc, σ1, σ2, s2, hkc, rfl, hsp, by rw [ct]; exact hmem, hdec, Sched.WakeEndA.ofRep hkc hrepK, Q, Rl, + stack', J2, fun _ => ⟨σ4, K4, hk4, run4, ns4, X4⟩⟩ + +/-- **`replyLiveXA` without the exit facts** (module docstring). -/ +theorem JSyscallA.replyLiveA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hag : fl4MinBudget = MIN_BUDGET) (hmb : ∀ a, Fl4MinBudgetAgreeA ar a) + {r slot lbl len buf : U64} {grant blk call don fp : Bool} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Reply r grant) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hr : r ∈ Rp) {xr : Reply} (hk : σ.kheap (specPtr r) = some (.reply xr)) {d : U64} + (hcl : xr.replyTcb = some (specPtr d)) + (hsnd : ∀ x, σ.kheap σ.curThread = some (.tcb x) → x.tcbFault = none ∧ (grant = false ∨ + ipcBufOfA false x = none ∨ + unat (dataToMessageInfo (x.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0)) + (hrcv : ∀ y, σ.kheap (specPtr d) = some (.tcb y) → + y.tcbState = .blockedOnReply (specPtr r) ∧ y.tcbFault = none) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) (hconv : Fl4TicksToUsLegacyAgree) : + ∃ xc σ1 σ2 s2, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ((), σ2) ∈ (fl4DoReplyTransfer (Z := Unit) σ.curThread (specPtr r) grant σ1).1 ∧ + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don fp buf extra = + ok (.Ok (), s2, a) ∧ + Sched.WakeEndA σ σ2 ∧ + ∃ Q Rl stack', JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack' κ n + (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s2 a σ2 m := by + obtain ⟨xc, σ1, σ2, s2, h1, h2, h3, h4, h5, hW, Q, Rl, stack', J2, -⟩ := + J.replyLiveXA (lbl := lbl) (len := len) (buf := buf) (blk := blk) (call := call) (don := don) (fp := fp) + hA hM hPl hfm hCs h1 hag hmb hslot hcov hcap hext hSC hr hk hcl hsnd hrcv hmax htu hconv + exact ⟨xc, σ1, σ2, s2, h1, h2, h3, h4, h5, hW, Q, Rl, stack', J2⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3ReplyDetachedA.lean b/proofs/Fl4ProofsA64/P3/Stage3ReplyDetachedA.lean new file mode 100644 index 000000000..633424524 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ReplyDetachedA.lean @@ -0,0 +1,72 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2b): **a Reply invocation on a detached reply object, at the record.** + +**`JSyscallA.replyDetachedA`**: `decode_invocation` on a Reply cap at a syscall entry whose reply object names no +thread. FL4's and the spec's reply transfer do nothing past the caller's Restart, so the record is the one +`of_restartCurA` gives. **`JSyscallA.replyDetachedDispA`** is the same arm in the dispatch top's shape (`DispPostA`), +for an IPC group. + +No named premise beyond those of the record. New module. +-/ +import Fl4ProofsA64.P3.Stage3SignalIdleA +import Fl4ProofsA64.P3.Stage3DispBaseA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s : S} {a : A} {σ : State Unit} {m : McState} + +set_option maxHeartbeats 1000000 in +/-- **Reply on a detached reply object, at a syscall entry** (module docstring). -/ +theorem JSyscallA.replyDetachedA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {r slot lbl len buf : U64} {grant blk call don fp : Bool} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Reply r grant) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) + (hr : r ∈ Rp) {x : Reply} (hk : σ.kheap (specPtr r) = some (.reply x)) (hdet : x.replyTcb = none) : + ∃ xc σ1 s1, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + doReplyTransfer (Z := Unit) σ.curThread (specPtr r) grant σ1 = ({((), σ1)}, False) ∧ + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don fp buf extra = + ok (.Ok (), s1, a) ∧ + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s1 a σ1 m := by + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hslot hcov J.actW J.running J.cur + have hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM := J.toJStage3EntryA.curNotVmA + obtain ⟨xc, hkc, hst, hsp, J1, -, -, -⟩ := J.toJStage3A.of_restartCurA + RS.pl.choose_spec.choose_spec.choose_spec.choose_spec RS.sym RS.run hnvm RS.cur RS.vs RS.vc + set s1 := L.wr s (stA (L.curW s)) sched.TS_RESTART with hs1 + set σ1 := stUpdA σ σ.curThread xc .restart with hσ1 + have ne1 : specPtr r ≠ σ.curThread := fun e => by rw [e, hkc] at hk; cases hk + have hk1 : σ1.kheap (specPtr r) = some (.reply x) := by + rw [hσ1]; simp only [stUpdA, setObjA, Function.update_of_ne ne1]; exact hk + -- the reply word is zero + have h0 : L.mem s1 (Endp.rTcbA r) = 0#u64 := by + obtain ⟨y, hy, he⟩ := J1.ep.reply r hr + rw [hk1] at hy + obtain rfl := KernelObject.reply.inj (Option.some.inj hy) + exact optRef_none (he ▸ hdet) + have htr : obj.Reply.tcb st r s1 = ok 0#u64 := + (eq_of_spec_fd (obj.Reply.tcb.spec (st := st) s1 r)).trans (by rw [h0]) + have hspec := replyTransfer_detached_specA (grant := grant) (t := σ.curThread) hk1 hdet + refine ⟨xc, σ1, s1, hkc, rfl, hsp, hspec, ?_, J1⟩ + rw [hcap, decodeReply_programA] + simp only [ipcInvokeProgramA, cur_thread_ok, bind_ok, hst] + rw [show ipc.do_reply_transfer st ar (L.wr s (stA (L.curW s)) sched.TS_RESTART) a (L.curW s) r grant = + ok (L.wr s (stA (L.curW s)) sched.TS_RESTART, a) from replyTransfer_detachedA htr] + simp only [bind_ok] + rfl + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3ReplyErrA.lean b/proofs/Fl4ProofsA64/P3/Stage3ReplyErrA.lean new file mode 100644 index 000000000..6a3482039 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ReplyErrA.lean @@ -0,0 +1,323 @@ +/- +P3 (aarch64, A64-P3 Lift 1b, 4b): **the error reply's record walk** (`syscall.reply_error`, x86 P3/ReplyFailX and +XTopJointReplyTail). + +* `error_codeA`, `error_mrs_genA`: FL4's label and message words of a `SysError` related to l4v's `SyscallError` + (`VsErrRelA`) are `msgFromSyscallError`'s. +* `tcbRegW_viewsA`, `bufWordW_viewsA`: a write of a register word or of a tracked IPC-buffer word keeps the merged + IPC views (`TcbIpcViewsA`), with the spec's register write / user-memory store. +* `replyErr_walkA`: `reply_error` of a record TCB (badge register, message registers and buffer words, the debug word, + the message info) is one outcome of `fl4ReplyFromKernelError true`, keeping the views; only the TCB's registers and + tracked buffer words are written, and user memory only inside the TCB's receive buffer. + +Named premises: none. New module. +-/ +import Fl4ProofsA64.P3.Stage3ReplyErrBufA +import Fl4ProofsA64.P3.Stage3ReplyOneA +import Fl4ProofsA64.P3.EpFaultCapA +import Fl4ProofsA64.P3.TcbRegViewA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.ReplyErr +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3 Fl4ProofsA64.P3.Endp + Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section words +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **FL4's error label is l4v's.** -/ +theorem error_codeA (e : error.SysError) (e' : SyscallError) (h : VsErrRelA (.Sys e) e') : + ∃ c : U64, error.SysError.code e = ok c ∧ c.bv = (msgFromSyscallError e').1 ∧ + c.bv &&& Fl4SpecA64.mask msgLabelBits = c.bv := by + have key : ∀ c : U64, c.val ≤ 10 → c.bv &&& Fl4SpecA64.mask msgLabelBits = c.bv := fun c hc => by + apply BitVec.eq_of_toNat_eq + rw [BitVec.toNat_and, show (Fl4SpecA64.mask msgLabelBits : BitVec 64).toNat = 2 ^ 52 - 1 from rfl, + Nat.and_two_pow_sub_one_eq_mod] + exact Nat.mod_eq_of_lt (show c.val < 2 ^ 52 by omega) + rcases h with h | ⟨n, he, rfl⟩ + · cases e <;> cases e' <;> simp only [SysErrRelA] at h <;> + refine ⟨_, rfl, ?_, key _ ?_⟩ <;> + (unfold_projs + simp [msgFromSyscallError, fl4_abi.consts.error.InvalidArgument, fl4_abi.consts.error.IllegalOperation, + fl4_abi.consts.error.RangeError, fl4_abi.consts.error.AlignmentError, fl4_abi.consts.error.FailedLookup, + fl4_abi.consts.error.TruncatedMessage, fl4_abi.consts.error.DeleteFirst, fl4_abi.consts.error.RevokeFirst, + fl4_abi.consts.error.NotEnoughMemory]) + · cases he + refine ⟨_, rfl, ?_, key _ ?_⟩ <;> simp [msgFromSyscallError, fl4_abi.consts.error.InvalidCapability] + +/-- A related lookup failure's words are l4v's `msg_from_lookup_failure`. -/ +theorem lfMsg_relA {lf : error.LookupFault} {lf' : LookupFailure} (h : LookupFaultRelA lf lf') : + ((fl4LfMsgA lf).map UScalar.bv : List Word) = msgFromLookupFailure lf' := by + cases lf <;> cases lf' <;> simp only [LookupFaultRelA] at h + · rfl + · subst h + simp only [fl4LfMsgA, List.map_cons, List.map_nil, msgFromLookupFailure, List.cons.injEq, and_true] + exact ⟨rfl, bv_ofNat_valA _ rfl⟩ + · obtain ⟨rfl, rfl⟩ := h + simp only [fl4LfMsgA, List.map_cons, List.map_nil, msgFromLookupFailure, List.cons.injEq, and_true] + exact ⟨rfl, bv_ofNat_valA _ rfl, bv_ofNat_valA _ rfl⟩ + · rename_i g left gs left' g' + obtain ⟨rfl, rfl, hg, hgs, -⟩ := h + simp only [fl4LfMsgA, List.map_cons, List.map_nil, msgFromLookupFailure, List.cons.injEq, and_true] + refine ⟨rfl, bv_ofNat_valA _ rfl, ?_, ?_⟩ + · rw [ofBl_bitsOfA _ _ (by omega), Nat.mod_eq_of_lt hg] + exact bv_ofNat_valA _ rfl + · rw [length_bitsOfA] + exact bv_ofNat_valA _ rfl + +/-- **`error_mrs` writes l4v's error message** under a message invariant `I`. -/ +theorem error_mrs_genA {I : S → List Word → Prop} {t buf : U64} {g : Nat → Nat} (hset : SetMrLawA st I t buf g) + (hg0 : g 0 = 0) {s : S} (hI : I s []) (e : error.SysError) (e' : SyscallError) (h : VsErrRelA (.Sys e) e') : + syscall.error_mrs st s t buf e ⦃ r => r.1.val = g (msgFromSyscallError e').2.length ∧ + I r.2 (msgFromSyscallError e').2 ⦄ := by + have one : ∀ v : U64, msg.set_mr st s t buf 0#u64 v ⦃ r => r.1.val = g 1 ∧ I r.2 [v.bv] ⦄ := fun v => + hset s [] hI 0#u64 rfl (by simp) v + have none0 : (ok (0#u64, s) : Result (U64 × S)) ⦃ r => r.1.val = g 0 ∧ I r.2 [] ⦄ := by + rw [WP.spec_ok]; exact ⟨by rw [hg0]; rfl, hI⟩ + unfold syscall.error_mrs + rcases h with h | ⟨n, he, rfl⟩ + swap + · cases he + refine WP.spec_mono (one n) fun r hr => ?_ + simp only [msgFromSyscallError] + rw [← bv_ofNat_valA n rfl] + exact hr + cases e <;> cases e' <;> simp only [SysErrRelA] at h + case InvalidArgument.invalidArgument a n => + subst h + refine WP.spec_mono (one a) fun r hr => ?_ + simp only [msgFromSyscallError] + rw [← bv_ofNat_valA a rfl] + exact hr + case IllegalOperation.illegalOperation => exact none0 + case AlignmentError.alignmentError => exact none0 + case TruncatedMessage.truncatedMessage => exact none0 + case DeleteFirst.deleteFirst => exact none0 + case RevokeFirst.revokeFirst => exact none0 + case NotEnoughMemory.notEnoughMemory b n => + subst h + exact one b + case RangeError.rangeError mn mx mn' mx' => + obtain ⟨rfl, rfl⟩ := h + dsimp only + refine WP.spec_bind (one mn) ?_ + rintro ⟨i1, s1⟩ ⟨-, h1⟩ + try simp only [WP.uncurry'_pair] + exact hset s1 _ h1 1#u64 rfl (by simp) mx + case FailedLookup.failedLookup source lf src' lf' => + obtain ⟨rfl, hlf⟩ := h + have hw := lfMsg_relA hlf + have go : ∀ (w : U64), w.bv = (if src' = true then (1 : Word) else 0) → + (do + let (_, s1) ← msg.set_mr st s t buf 0#u64 w + faults.set_mrs_lookup_failure_at st s1 t buf lf 1#u64) ⦃ r => + r.1.val = g (msgFromSyscallError (.failedLookup src' lf')).2.length ∧ + I r.2 (msgFromSyscallError (.failedLookup src' lf')).2 ⦄ := by + intro w hwv + refine WP.spec_bind (one w) ?_ + rintro ⟨i1, s1⟩ ⟨-, h1⟩ + try simp only [WP.uncurry'_pair] + unfold faults.set_mrs_lookup_failure_at + refine WP.spec_mono (set_mrs_lookup_failure_genA hset h1 1#u64 rfl (by simp) lf) fun r hr => ?_ + have e2 : (msgFromSyscallError (.failedLookup src' lf')).2 = + [w.bv] ++ ((fl4LfMsgA lf).map UScalar.bv : List Word) := by + rw [hw, hwv]; rfl + rw [e2] + refine ⟨?_, hr.2⟩ + rw [hr.1]; simp [Nat.add_comm] + dsimp only + cases src' with + | true => exact go 1#u64 rfl + | false => exact go 0#u64 rfl + +theorem msgErr_lenA (e' : SyscallError) : (msgFromSyscallError e').2.length ≤ 5 := by + cases e' with + | failedLookup b lf => cases lf <;> simp [msgFromSyscallError, msgFromLookupFailure] + | _ => simp [msgFromSyscallError] + +end words + +/-! ## The writes and the merged views -/ + +section views +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E Rp : Set U64} {Bf : Set (U64 × Word)} + {stack : U64 → List U64} + +/-- A register word of `t` or a tracked IPC-buffer word. -/ +def RepW (Bf : Set (U64 × Word)) (t b : U64) : Prop := (∃ r : Register, b = Endp.regA t (regIdxA r)) ∨ BufW Bf b + +/-- **A write to a tracked IPC-buffer word keeps the merged views**, with the spec's user-memory store. -/ +theorem bufWordW_viewsA (hP : IPlacedA N Z.T E Rp Bf) (P : IpcSchedPlacementA Z.th N E Rp Bf) {s : S} + {σ : State Unit} (V : TcbIpcViewsA st Z m κ N E Rp Bf stack s σ) {bp : U64 × Word} (hb : bp ∈ Bf) + {k : U64} (hk : k.val < 128) (v : U64) : + TcbIpcViewsA st Z m κ N E Rp Bf stack (L.wr s (bufW bp.1 k) v) + (setUm σ (storeMem σ.machineState.underlyingMemory (bufAddr bp.2 k.val) v.bv)) ∧ + WritesIn st s (L.wr s (bufW bp.1 k) v) (EWrite Z.T E Rp Bf) := by + have hw : BufW Bf (bufW bp.1 k) := ⟨bp, hb, k, hk, rfl⟩ + have hal := hP.e.balign _ hw + have hW : WritesIn st s (L.wr s (bufW bp.1 k) v) (EWrite Z.T E Rp Bf) := + (writesIn_wr hal).mono fun b e => by rw [e]; exact Or.inr (Or.inr (Or.inr hw)) + have hag := agree_wr (L := L) (s := s) (v := v) hal + have off : ∀ b : U64, Aligned b → ¬ BufW Bf b → L.mem (L.wr s (bufW bp.1 k) v) b = L.mem s b := + fun b hb' hn => hag b hb' (val_ne_of_ne fun h => hn (h ▸ hw)) + have tcbOff : ∀ u ∈ Z.T, ∀ b : U64, u.val ≤ b.val → b.val < u.val + 4096 → ¬ BufW Bf b := fun u hu b h1 h2 hb' => by + have := hP.e.bt b hb' u hu; omega + have fld : ∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → L.mem (L.wr s (bufW bp.1 k) v) (wa u K) = L.mem s (wa u K) := by + intro u hu K h1 h2 + have hua := hP.n.tcb u hu + have hv := field_val hua h1 + exact off _ (field_aligned hua h1 h2) (tcbOff u hu _ (by omega) (by omega)) + refine ⟨⟨?_, ?_, ?_, ?_, ?_, ?_⟩, hW⟩ + · have O := schedOpsEAOfPlacementA (st := st) (m := m) (κ := κ) P + exact O.stable _ _ _ _ V.rel hW (EpStep.setUmA _ _) (NoBlockE.setUmA _ _) + · refine V.ntfn.frame hP.n (fun a ha hf => off a ha fun hb' => ?_) ⟨fun _ _ h => h, fun _ x h => ⟨x, h, rfl, rfl, rfl⟩⟩ + rcases hf with ⟨q, hq, h1, h2⟩ | ⟨u, hu, hf⟩ + · have := hP.nb q hq a hb'; omega + · have hua := hP.n.tcb u hu + have : u.val ≤ a.val ∧ a.val < u.val + 4096 := by + rcases hf with rfl | rfl | rfl | rfl | rfl | rfl + · rw [val_st hua]; omega + · rw [val_blk hua]; omega + · rw [badgeRegA_eq, tw_val hua (by simp)]; simp + · rw [val_prio hua]; omega + · rw [val_next hua]; omega + · rw [val_prev hua]; omega + exact tcbOff u hu a this.1 this.2 hb' + · exact V.ep.upd_buf hP.e hb hk v (fun b hb' hne => hag b hb' (val_ne_of_ne hne)) (rd_wr_eq hal) + · refine V.stk.frame (fun r hr => ?_) (fun c _ y n h => ⟨y, n, h, rfl⟩) (fun r _ x h => ⟨x, h, rfl⟩) + have hr3 := hP.e.rep r hr + have key : ∀ o : U64, o.val = 8 ∨ o.val = 16 → L.mem (L.wr s (bufW bp.1 k) v) (wa r o) = L.mem s (wa r o) := by + intro o ho + have hv : (wa r o).val = r.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + refine off _ (by unfold Aligned; rw [hv]; omega) fun hb' => ?_ + have := hP.e.br _ hb' r hr; omega + exact ⟨key _ (Or.inr (by simp [layout.reply.NEXT])), key _ (Or.inl (by simp [layout.reply.PREV]))⟩ + · exact V.mcp.congrA (fun u hu => ⟨by unfold mcpA; exact fld u hu _ (by simp) (by simp), + by unfold flagsA; exact fld u hu _ (by simp) (by simp)⟩) (fun u hu x hx => ⟨x, hx, rfl, rfl⟩) + · have ye : ∀ u ∈ Z.T, L.mem (L.wr s (bufW bp.1 k) v) (yieldA u) = L.mem s (yieldA u) := fun u hu => by + unfold yieldA; exact fld u hu _ (by simp) (by simp) + refine ⟨fun u hu => ?_, fun u hu h => ?_, fun c hc y n h p hp => ?_⟩ + · obtain ⟨x, hx, e⟩ := V.yld.tcb u hu + exact ⟨x, hx, by rw [ye u hu]; exact e⟩ + · rw [ye u hu] at h ⊢ + exact V.yld.toSc u hu h + · obtain ⟨u, hu, e1, e2⟩ := V.yld.frm c hc y n h p hp + exact ⟨u, hu, e1, by rw [ye u hu]; exact e2⟩ + +/-- The message invariant: the views at the spec state `τ ms`, and the writes since `s0`. -/ +def ErrInvA (Z : XSetsA) (m : McState) (κ : U64 → U64) (N E Rp : Set U64) (Bf : Set (U64 × Word)) + (stack : U64 → List U64) (t : U64) (s0 : S) (τ : List Word → State Unit) (s : S) (ms : List Word) : Prop := + TcbIpcViewsA st Z m κ N E Rp Bf stack s (τ ms) ∧ WritesIn st s0 s (RepW Bf t) ∧ + WritesIn st s0 s (EWrite Z.T E Rp Bf) + +variable (hP : IPlacedA N Z.T E Rp Bf) (P : IpcSchedPlacementA Z.th N E Rp Bf) {t : U64} (ht : t ∈ Z.T) +include hP P ht + +/-- A register write under the views and the write sets. -/ +theorem regW_stepA {s0 s : S} {σ : State Unit} (V : TcbIpcViewsA st Z m κ N E Rp Bf stack s σ) + (w1 : WritesIn st s0 s (RepW Bf t)) (w2 : WritesIn st s0 s (EWrite Z.T E Rp Bf)) + {x : Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) (r : Register) (v : U64) : + TcbIpcViewsA st Z m κ N E Rp Bf stack (L.wr s (Endp.regA t (regIdxA r)) v) + (updK σ (specPtr t) (.tcb (setReg x r v.bv))) ∧ + WritesIn st s0 (L.wr s (Endp.regA t (regIdxA r)) v) (RepW Bf t) ∧ + WritesIn st s0 (L.wr s (Endp.regA t (regIdxA r)) v) (EWrite Z.T E Rp Bf) := by + have hal : Aligned (Endp.regA t (regIdxA r)) := (a_reg (hP.n.tcb t ht) r).1 + refine ⟨tcbRegWrite_viewsA hP P V ht hk r v, ?_, ?_⟩ + · exact (w1.trans (writesIn_wr hal (v := v))).mono fun b h => + h.elim id (fun e => Or.inl ⟨r, e⟩) + · exact (w2.trans (write_reg_rel hP.e s σ V.ep ht hk r v).2.1).mono fun _ h => h.elim id id + +/-- **`set_mr` into a receiver with an IPC buffer** keeps `ErrInvA`. -/ +theorem errSetMr_bufA {s0 : S} {σ0 : State Unit} {x0 : Tcb} {bp : U64 × Word} (hb : bp ∈ Bf) (hb0 : bp.1 ≠ 0#u64) : + SetMrLawA st (ErrInvA (st := st) Z m κ N E Rp Bf stack t s0 (mrState σ0 (specPtr t) x0 bp.2)) t bp.1 id := by + intro s ms ⟨V, w1, w2⟩ i hi hi' v + have hiv : (wa i 1#u64).val = i.val + 1 := by + simp [core.num.U64.wrapping_add_val_eq, U64.size_def, U64.numBits]; omega + by_cases h4 : i.val < 4 + · rw [set_mr_reg s t bp.1 i v h4, WP.spec_ok] + have h := regW_stepA hP P ht V w1 w2 (mrState_kheap σ0 (specPtr t) x0 bp.2 ms) (mrIdx i.val) v + rw [mrState_snoc_lt _ _ _ _ _ _ i.val hi (by omega)] at h + exact ⟨hiv, h⟩ + · rw [set_mr_buf s t bp.1 i v (by omega) hb0, WP.spec_ok] + have hk : (wa i 1#u64).val < 128 := by omega + obtain ⟨V', hW⟩ := bufWordW_viewsA hP P V hb hk v + rw [hiv, hi] at V' + rw [mrState_snoc_ge _ _ _ _ _ _ (by omega) (by omega)] at V' + have hw : BufW Bf (bufW bp.1 (wa i 1#u64)) := ⟨bp, hb, _, hk, rfl⟩ + refine ⟨hiv, V', ?_, (w2.trans hW).mono fun _ h => h.elim id id⟩ + exact (w1.trans (writesIn_wr (hP.e.balign _ hw) (v := v))).mono fun b h => + h.elim id (fun e => Or.inr (e ▸ hw)) + +/-- **`set_mr` into a receiver without an IPC buffer** keeps `ErrInvA`. -/ +theorem errSetMr_nobufA {s0 : S} {σ0 : State Unit} {x0 : Tcb} : + SetMrLawA st (ErrInvA (st := st) Z m κ N E Rp Bf stack t s0 (mrStateNA σ0 (specPtr t) x0)) t 0#u64 + (fun n => min n 4) := by + intro s ms ⟨V, w1, w2⟩ i hi _ v + by_cases h4 : i.val < 4 + · rw [set_mr_reg s t 0#u64 i v h4, WP.spec_ok] + have h := regW_stepA hP P ht V w1 w2 (mrStateNA_kheap σ0 (specPtr t) x0 ms) (mrIdx i.val) v + rw [mrStateNA_snoc_lt _ _ _ _ _ i.val hi (by omega)] at h + refine ⟨?_, h⟩ + show _ = min (i.val + 1) 4 + simp [core.num.U64.wrapping_add_val_eq, U64.size_def, U64.numBits]; omega + · rw [set_mr_nobuf s t i v (by omega), WP.spec_ok] + have hN : msg.N_MSG_REGISTERS.val = 4 := by unfold msg.N_MSG_REGISTERS; rfl + refine ⟨by show _ = min (i.val + 1) 4; rw [hN]; omega, ?_, w1, w2⟩ + rw [mrStateNA_append_ge _ _ _ _ _ (by omega)]; exact V + +end views + +/-! ## The spec state of the reply -/ + +section shape + +/-- `σ'` is `σ0` with another user context of the TCB at `p` and user memory changed only in `B`. -/ +def ErrShapeA (σ0 : State Unit) (p : ObjRef) (x : Tcb) (B : Word → Prop) (σ' : State Unit) : Prop := + ∃ (uc : UserContext) (um : Word → Word8), + σ' = setUm (updK σ0 p (.tcb { x with tcbArch := { x.tcbArch with tcbContext := uc } })) um ∧ + ∀ a, ¬ B a → um a = σ0.machineState.underlyingMemory a + +theorem storeMem_offA (um : Word → Word8) (p w a : Word) (h : ∀ y : Word, y.toNat < 8 → a ≠ p + y) : + storeMem um p w a = um a := by + have h0 := h 0 (by decide) + have h1 := h 1 (by decide) + have h2 := h 2 (by decide) + have h3 := h 3 (by decide) + have h4 := h 4 (by decide) + have h5 := h 5 (by decide) + have h6 := h 6 (by decide) + have h7 := h 7 (by decide) + unfold storeMem + rw [intUpto_07] + simp only [List.foldl_cons, List.foldl_nil] + repeat rw [Function.update_of_ne] + all_goals first | assumption | simpa + +theorem storeBuf_offA {sb : Word} {k : Nat} (hk : k < 128) (um : Word → Word8) (w a : Word) + (h : ¬ BufBytesA (some sb) a) : storeMem um (bufAddr sb k) w a = um a := + storeMem_offA um _ w a fun y hy e => h ⟨sb, rfl, k, hk, y, hy, e⟩ + +theorem mrMem_offA {sb a : Word} (h : ¬ BufBytesA (some sb) a) : + ∀ (ps : List (Nat × Word)) (um : Word → Word8), (∀ q ∈ ps, q.1 < 128) → mrMem um sb ps a = um a + | [], _, _ => rfl + | (k, v) :: ps, um, hq => by + unfold mrMem + rw [mrMem_offA h ps _ (fun q hq' => hq q (by simp [hq'])), + storeBuf_offA (show k < 128 from hq (k, v) (by simp)) um v a h] + +theorem mrPairs_ltA (ms : List Word) : ∀ q ∈ mrPairs ms, q.1 < 128 := by + intro q hq + have := List.of_mem_zip (show (q.1, q.2) ∈ (List.range' 5 116).zip (ms.drop 4) from hq) + have h := (List.mem_range'_1.1 this.1) + omega + +end shape + +end + +end Fl4ProofsA64.P3.ReplyErr diff --git a/proofs/Fl4ProofsA64/P3/Stage3ReplyErrBufA.lean b/proofs/Fl4ProofsA64/P3/Stage3ReplyErrBufA.lean new file mode 100644 index 000000000..d61c105f4 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ReplyErrBufA.lean @@ -0,0 +1,209 @@ +/- +P3 (aarch64, A64-P3 Lift 1b, 4b): **a record TCB's IPC buffer lies in a user frame** (J10 across writes into it, and +`inUserFrame` for the spec's stores). Text of lift 2b's `P3/Stage3RecvBufA` (branch `par/a64-p3-lift2b`), in the +namespace `ReplyErr` so that a later merge of that branch does not clash. New module. +-/ +import Fl4ProofsA64.P3.Stage3IpcGlueA +import Fl4ProofsA64.P3.Stage3BufWrA +import Fl4ProofsA64.P3.Stage3SysPlaceA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.ReplyErr +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3 Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +/-- The bytes of the 128 words of the buffer `rb`. -/ +def BufBytesA (rb : Option ObjRef) (a : Word) : Prop := + ∃ p, rb = some p ∧ ∃ k, k < 128 ∧ ∃ y : Word, y.toNat < 8 ∧ a = bufAddr p k + y + +theorem andNotMaskA_toNat (x : BitVec 64) {k : Nat} (hk : k ∈ [12, 21, 30]) : + (x &&& ~~~(Fl4SpecA64.mask k : BitVec 64)).toNat = x.toNat / 2 ^ k * 2 ^ k := by + have e : ~~~(Fl4SpecA64.mask k : BitVec 64) = BitVec.allOnes 64 <<< k := by + simp at hk; rcases hk with rfl | rfl | rfl <;> decide + rw [e, ← BitVec.shiftLeft_ushiftRight] + simp only [BitVec.toNat_shiftLeft, BitVec.toNat_ushiftRight, Nat.shiftLeft_eq, Nat.shiftRight_eq_div_pow] + rw [Nat.mod_eq_of_lt (lt_of_le_of_lt (Nat.div_mul_le_self _ _) x.isLt)] + +theorem maskAndA_toNat (x : BitVec 64) {k : Nat} (hk : k ∈ [12, 21, 30]) : + (x &&& (Fl4SpecA64.mask k : BitVec 64)).toNat = x.toNat % 2 ^ k := by + have h : (Fl4SpecA64.mask k : BitVec 64).toNat = 2 ^ k - 1 := by + simp at hk; rcases hk with rfl | rfl | rfl <;> decide + rw [BitVec.toNat_and, h, Nat.and_two_pow_sub_one_eq_mod] + +theorem pageBitsA_mem (vms : VMPageSize) : pageBitsForSize vms ∈ [12, 21, 30] := by + cases vms <;> simp [pageBitsForSize, pageBits, ptTranslationBits] + +/-- **The page of a buffer word** (module docstring). -/ +theorem bufAddr_pageA {q : U64} {n : Nat} (hn : n ∈ [12, 21, 30]) (hq : q.val % 2 ^ n = 0) (w : BitVec 64) + (hw : w.toNat % 1024 = 0) {k : Nat} (hk : k < 128) : + bufAddr (specPtr q + (w &&& Fl4SpecA64.mask n)) k &&& ~~~(Fl4SpecA64.mask n : BitVec 64) = specPtr q := by + have hn39 : n ≤ 39 := by simp at hn; omega + have hn10 : 10 ≤ n := by simp at hn; omega + apply BitVec.eq_of_toNat_eq + rw [andNotMaskA_toNat _ hn] + unfold bufAddr + rw [BitVec.toNat_add, BitVec.toNat_add, maskAndA_toNat w hn, BitVec.toNat_ofNat] + have hA : (specPtr q).toNat % 2 ^ n = 0 := (specPtr_mod q hn39).trans hq + have hAlt : (specPtr q).toNat < 2 ^ 64 := (specPtr q).isLt + have hwm : w.toNat % 2 ^ n % 1024 = 0 := by + rw [Nat.mod_mod_of_dvd _ (show 1024 ∣ 2 ^ n from Nat.pow_dvd_pow 2 hn10)]; exact hw + have hwl : w.toNat % 2 ^ n < 2 ^ n := Nat.mod_lt _ (by positivity) + obtain ⟨f, hf⟩ : 1024 ∣ 2 ^ n := Nat.pow_dvd_pow 2 hn10 + obtain ⟨e, he⟩ : 2 ^ n ∣ 2 ^ 64 := Nat.pow_dvd_pow 2 (by omega) + obtain ⟨c, hc⟩ : 2 ^ n ∣ (specPtr q).toNat := Nat.dvd_of_mod_eq_zero hA + have hPpos : 0 < 2 ^ n := by positivity + have hk8 : k * wordSize < 1024 := by unfold wordSize; omega + have h64 : (2 : Nat) ^ 64 = 18446744073709551616 := by norm_num + rw [h64] at hAlt he ⊢ + generalize (specPtr q).toNat = A at * + generalize w.toNat % 2 ^ n = o at * + generalize 2 ^ n = P at * + generalize k * wordSize = j at * + subst hc + have hce : c < e := Nat.lt_of_mul_lt_mul_left (a := P) (by omega) + have hle : P * c + P ≤ P * e := by rw [← Nat.mul_succ]; exact Nat.mul_le_mul_left _ hce + have hoj : o + j < P := by omega + rw [Nat.mod_eq_of_lt (show P * c + o < 18446744073709551616 by omega), + Nat.mod_eq_of_lt (show j < 18446744073709551616 by omega), + Nat.mod_eq_of_lt (show P * c + o + j < 18446744073709551616 by omega), Nat.add_assoc, + Nat.mul_add_div hPpos, Nat.div_eq_of_lt hoj, Nat.add_zero, Nat.mul_comm] + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {s : S} {a : A} {σ : State Unit} {m : McState} + +set_option maxHeartbeats 1000000 in +/-- **The frame of a record TCB's IPC buffer** (module docstring): a heap object `H q = .other n` aligned to `2^n`, a +spec data page of that size, and the buffer at `specPtr q + (tcbIpcBuffer &&& mask n)`. -/ +theorem _root_.Fl4ProofsA64.P3.JStage3A.replyBufObjA {R7 : Set U64 → JSlotA S} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st R7) s a σ m) + {d : U64} (hd : d ∈ Z.T) {xd : Tcb} (hkd : σ.kheap (specPtr d) = some (.tcb xd)) (b : Bool) {rp : ObjRef} + (hrp : ipcBufOfA b xd = some rp) : + ∃ q vms, H q = some (.other (pageBitsForSize vms)) ∧ q.val % 2 ^ pageBitsForSize vms = 0 ∧ + rp = specPtr q + (xd.tcbIpcBuffer &&& Fl4SpecA64.mask (pageBitsForSize vms)) ∧ + σ.kheap (specPtr q) = some (.archObj (.dataPage false vms)) := by + have hth : Z.th d = some .tcb := (J.sr.thT d).2 hd + obtain ⟨x', hk', hs⟩ := J.j.1.st.tcb d hth + rw [hkd] at hk' + cases hk' + obtain ⟨ac, hac, hca⟩ := hs 2 (by decide) + rw [tcbCnodeMap_twoA] at hac + cases hac + have hyOS : slotAddrA (specPtr d) (BitVec.ofNat 64 2) ∈ OS := + J.j5.2.1 _ ⟨d, Or.inr ⟨hth, 2, by decide, rfl⟩⟩ + rw [capAtA_eq] at hca + obtain ⟨-, hab⟩ := hca + generalize hc : capA st s (slotAddrA (specPtr d) (BitVec.ofNat 64 2)) = c at hab + cases c with + | Frame q sz r dev mp ept asid va => + replace hab : absCapA (.Frame q sz r dev mp ept asid va) = xd.tcbIpcframe := hab + cases dev + · cases ept + · have erp : rp = specPtr q + (xd.tcbIpcBuffer &&& Fl4SpecA64.mask (pageBitsForSize (frameSizeOfA sz))) := by + unfold ipcBufOfA at hrp + rw [← hab] at hrp + simp only [absCapA, absFrameE, Bool.false_eq_true, ↓reduceIte] at hrp + split_ifs at hrp + exact (Option.some.inj hrp).symm + have hpg := J.userFr _ hyOS q sz r mp false asid va hc + obtain ⟨Pt, Ub, -, -, -, cc, -⟩ := J.j7 + have hfo := cc.fo _ hyOS q sz r mp false asid va hc + have hH : H q = some (.other (pageBitsForSize (frameSizeOfA sz))) := by + cases sz + · exact hfo 12#u64 rfl + · exact hfo 21#u64 rfl + · exact hfo 30#u64 rfl + exact ⟨q, frameSizeOfA sz, hH, J.sys.ent.heap.align q _ hH, erp, hpg⟩ + · unfold ipcBufOfA at hrp; rw [← hab] at hrp; simp [absCapA, absFrameE] at hrp + · unfold ipcBufOfA at hrp; rw [← hab] at hrp; cases ept <;> simp [absCapA, absFrameE] at hrp + | _ => + unfold ipcBufOfA at hrp + rcases CapEqA.eq_or_pt hab with e | ⟨p', a', v', e⟩ <;> rw [e] at hrp <;> simp [absCapA] at hrp + +/-- **A record TCB's IPC buffer is in a user frame** (module docstring). -/ +theorem _root_.Fl4ProofsA64.P3.JStage3A.replyBufFrameA {R7 : Set U64 → JSlotA S} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st R7) s a σ m) + {d : U64} (hd : d ∈ Z.T) {xd : Tcb} (hkd : σ.kheap (specPtr d) = some (.tcb xd)) (b : Bool) : + ∀ rp, ipcBufOfA b xd = some rp → ∀ k, k < 128 → inUserFrame (bufAddr rp k) σ := by + intro rp hrp k hk + obtain ⟨q, vms, -, hal, erp, hpg⟩ := J.replyBufObjA hd hkd b hrp + refine ⟨vms, ?_⟩ + rw [erp, bufAddr_pageA (pageBitsA_mem _) hal _ (J.bufAl d hd xd hkd) hk] + exact hpg + +/-- The buffer's word 0 is aligned. -/ +theorem _root_.Fl4ProofsA64.P3.JStage3A.replyBufAlign0A {R7 : Set U64 → JSlotA S} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st R7) s a σ m) + {d : U64} (hd : d ∈ Z.T) {xd : Tcb} (hkd : σ.kheap (specPtr d) = some (.tcb xd)) (b : Bool) : + ∀ rp, ipcBufOfA b xd = some rp → (bufAddr rp 0).toNat % 8 = 0 := by + intro rp hrp + obtain ⟨q, vms, -, hal, erp, -⟩ := J.replyBufObjA hd hkd b hrp + have hn := pageBitsA_mem vms + have hn39 : pageBitsForSize vms ≤ 39 := by simp at hn; omega + have hn10 : 10 ≤ pageBitsForSize vms := by simp at hn; omega + have hA : (specPtr q).toNat % 2 ^ pageBitsForSize vms = 0 := (specPtr_mod q hn39).trans hal + have h8 : 8 ∣ 2 ^ pageBitsForSize vms := Nat.pow_dvd_pow 2 (by omega : 3 ≤ pageBitsForSize vms) + have hw : (xd.tcbIpcBuffer &&& Fl4SpecA64.mask (pageBitsForSize vms)).toNat % 1024 = 0 := by + rw [maskAndA_toNat _ hn, Nat.mod_mod_of_dvd _ (show 1024 ∣ 2 ^ pageBitsForSize vms from Nat.pow_dvd_pow 2 hn10)] + exact J.bufAl d hd xd hkd + have hA8 : (specPtr q).toNat % 8 = 0 := Nat.mod_eq_zero_of_dvd (h8.trans (Nat.dvd_of_mod_eq_zero hA)) + rw [erp] + unfold bufAddr + rw [BitVec.toNat_add, BitVec.toNat_add, BitVec.toNat_ofNat] + omega + +/-- **The bytes of a buffer word lie in its frame**, under the FL4 naming. -/ +theorem fl4Ptr_bufA {q : U64} {k0 : Nat} (hn : k0 ∈ [12, 21, 30]) (hfit : q.val + 2 ^ k0 ≤ 2 ^ 64) (w : BitVec 64) + (hw : w.toNat % 1024 = 0) {k : Nat} (hk : k < 128) {y : Word} (hy : y.toNat < 8) : + q.val ≤ (fl4Ptr (bufAddr (specPtr q + (w &&& Fl4SpecA64.mask k0)) k + y)).val ∧ + (fl4Ptr (bufAddr (specPtr q + (w &&& Fl4SpecA64.mask k0)) k + y)).val < q.val + 2 ^ k0 := by + have hn10 : 10 ≤ k0 := by simp at hn; omega + have hm : (w &&& Fl4SpecA64.mask k0).toNat = w.toNat % 2 ^ k0 := maskAndA_toNat w hn + have hwm : w.toNat % 2 ^ k0 % 1024 = 0 := by + rw [Nat.mod_mod_of_dvd _ (show 1024 ∣ 2 ^ k0 from Nat.pow_dvd_pow 2 hn10)]; exact hw + obtain ⟨f, hf⟩ : 1024 ∣ 2 ^ k0 := Nat.pow_dvd_pow 2 hn10 + have hwl : w.toNat % 2 ^ k0 < 2 ^ k0 := Nat.mod_lt _ (by positivity) + have hk8 : k * wordSize + 8 ≤ 1024 := by unfold wordSize; omega + have ev : ∀ z : Fl4SpecA64.Word, (fl4Ptr z).val = (z - pptrShiftA).toNat := fun z => by rw [fl4Ptr_def]; rfl + rw [ev] + unfold bufAddr + rw [BitVec.toNat_sub, BitVec.toNat_add, BitVec.toNat_add, BitVec.toNat_add, specPtr_toNat, hm, + BitVec.toNat_ofNat, pptrShiftA_toNat] + have hq : q.val < 2 ^ 64 := q.hBounds + have h64 : (2 : Nat) ^ 64 = 18446744073709551616 := by norm_num + rw [h64] at hfit hq ⊢ + generalize w.toNat % 2 ^ k0 = o at * + generalize 2 ^ k0 = P at * + generalize k * wordSize = j at * + omega + +/-- **(D12) J10 across a step changing user memory only in a record TCB's receive buffer**: the buffer's frame is a +heap object (`bufFrameObjA`), off every free untyped part (`ObjAllocA`). -/ +theorem _root_.Fl4ProofsA64.P3.JStage3A.replyBufZeroA {R7 : Set U64 → JSlotA S} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st R7) s a σ m) + {d : U64} (hd : d ∈ Z.T) {xd : Tcb} (hkd : σ.kheap (specPtr d) = some (.tcb xd)) {σ' : State Unit} + (hum : ∀ x, ¬ BufBytesA (ipcBufOfA true xd) x → + σ'.machineState.underlyingMemory x = σ.machineState.underlyingMemory x) : + ∀ x : Word, UtFreeA st OS s (fun d => d = false) (fl4Ptr x).val → + σ'.machineState.underlyingMemory x = σ.machineState.underlyingMemory x := by + intro x hfree + by_contra hne + have hB : BufBytesA (ipcBufOfA true xd) x := Classical.byContradiction fun h => hne (hum x h) + obtain ⟨rp, hrp, k, hk, y, hy, rfl⟩ := hB + obtain ⟨q, vms, hH, -, erp, -⟩ := J.replyBufObjA hd hkd true hrp + obtain ⟨u, hu, qu, b, fi, dd, hc, -, f1, f2⟩ := hfree + have hO := J.objAlloc q (2 ^ pageBitsForSize vms) (Or.inr (Or.inr (Or.inr ⟨_, hH, rfl⟩))) u hu qu b fi dd hc + have hfit : q.val + 2 ^ pageBitsForSize vms ≤ 2 ^ 64 := J.sys.ent.heap.fits q _ hH + rw [erp] at f1 f2 + have hb := fl4Ptr_bufA (pageBitsA_mem vms) hfit xd.tcbIpcBuffer (J.bufAl d hd xd hkd) hk hy + omega + +end + +end + +end Fl4ProofsA64.P3.ReplyErr diff --git a/proofs/Fl4ProofsA64/P3/Stage3ReplyErrGlueA.lean b/proofs/Fl4ProofsA64/P3/Stage3ReplyErrGlueA.lean new file mode 100644 index 000000000..3318c1726 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ReplyErrGlueA.lean @@ -0,0 +1,202 @@ +/- +P3 (aarch64, A64-P3 Lift 1b, 4b): **the record glue of the error reply** (`syscall.reply_error`): a step writing the +caller's registers, its IPC-buffer words and user memory, with no scheduler or queue change. + +`JStage3A.of_replyFinA` / `of_replyStepA`: `of_stepFinA` / `of_ipcStepA` with `BufW Bf` in the write set and user +memory changed only off the free RAM untyped parts (`hz`). Text of lift 2b's `of_stepFinUA` (branch +`par/a64-p3-lift2b`, `P3/Stage3IpcGlueUA`) without the queue-growth and reply-stack changes. New module. +-/ +import Fl4ProofsA64.P3.Stage3IpcGlueA +import Fl4ProofsA64.P3.Stage3BufWrA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {ar : machine.Arch A} {nA : U64} {s s' : S} {a : A} {σ σ' : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} {Q : U64 → U64 → List U64} {Rl : U64 → List U64} + {curOf curScOf : Fl4SpecA64.CoreId → Fl4SpecA64.ObjRef} + +set_option maxHeartbeats 1000000 in +/-- **`of_stepFinA` across a body that also writes buffer words and user memory** (module docstring). -/ +theorem JStage3A.of_replyFinA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (w : WritesIn st s s' (fun b => SchedStepNWA (st := st) N E Rp Z m σ s b ∨ BufW Bf b)) (HS : SpecSchedA σ σ') (SSz : SSzA σ σ') (Qk : QKeepA σ σ') + (hz : ∀ x : Fl4SpecA64.Word, UtFreeA st OS s (fun d => d = false) (fl4Ptr x).val → + σ'.machineState.underlyingMemory x = σ.machineState.underlyingMemory x) + (hcurOf : ∀ k, k ≠ m.curCore → curOf k = Z.curOf k) + (bxi : BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s') + (sysl : SysL st H Z.Cs OS ia ie s') (symL : SymL st H Z.Cs s') + (userOK : Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s') + (j7 : J7A st (J7R2A st ar nA) Z H OS s' σ' m) + (sr : SchedSRA st { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } m κ s' σ') + (ep : EpStateRel Z.T E Rp Bf (L.mem s') σ') + (ntfn : NtfnStateRel N Z.T (L.mem s') σ') + (reply : ReplyStackRelA (L.mem s') Rp Z.C σ' (scNameA m) stack) + (bufCover : BufCover st Z.T Bf s' σ') (bufAl : UtBufAlA Z.T σ') + (idleNoFault : IdleNoFaultA m σ') (mcp : McpRelA Z.T m (L.mem s') σ') + (yieldRel : YieldRelA Z.T Z.C m (L.mem s') σ') + (sinv : SInvXA σ' m n) (sidle : SIdleA σ' m n) + (runVcpu : RunVcpuInv st Z.T s') (blkQ : BlkQueuedA Z.T E N (L.mem s') σ') + (schedValid : SchedValidA σ') : + JStage3A st AR { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } H OS U Ac Pp Tb Lv ia ie N E Rp + Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s' a σ' m := by + have kb : L.ksb s' = L.ksb s := w.1 + have bks : ∀ b, (L.ksb s).val ≤ b.val → b.val < (L.ksb s).val + 788736 → ¬ BufW Bf b := fun b h1 h2 hb => by + have hksz : ksSizeA = 788736 := rfl + have := J.buf_ksA hb + omega + have osk : OSKeep st OS s s' := fun y hy => by + have hfit := J.sys.ent.os.fit y hy + have key : ∀ o : U64, o.val < 32 → o.val % 8 = 0 → L.mem s' (wa y o) = L.mem s (wa y o) := by + intro o o1 o2 + have hv : (wa y o).val = y.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have hal : Aligned (wa y o) := J.sys.ent.os.al hy o1 o2 + have hin : InOS OS (wa y o) := ⟨y, hy, by omega, by omega⟩ + exact w.mem_eq hal (fun h => h.elim (J.os_offStepNA hal hin) (fun hb => J.bufOff.os _ hb hin)) + have e0 : wa y 0#u64 = y := by apply UScalar.eq_of_val_eq; rw [wa_valm]; simp + exact ⟨by have := key 0#u64 (by simp) (by simp); rwa [e0] at this, key 8#u64 (by simp) (by simp), + key 16#u64 (by simp) (by simp), key 24#u64 (by simp) (by simp)⟩ + have vsr : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → L.mem s' b = L.mem s b := fun b hb => by + refine w.mem_eq (J.vsOk.al b hb) (fun h => h.elim ?_ (fun h => J.bufVs b h hb)) + by_cases h : AsW (L.ksb s) b + · obtain ⟨o, o1, o2, -, e⟩ := h + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.sys.ent.ksw + have ev : b.val = (L.ksb s).val + o.val := by + rw [e, wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact J.asid_offStepNA (by omega) (by omega) + · exact J.vsr_offStepNA J.vsOk hb h + have sys : SysInvA st H Z.Cs OS U Ac Pp Tb Lv ia ie s' := + ⟨⟨J.sys.ent.heap, bxi, J.sys.ent.os, userOK, (utVsPt_keepA J.sys.ent.vs kb vsr).1, + by rw [kb]; exact J.sys.ent.ksw⟩, sysl⟩ + have sq : SchedQueuesInv st H.tcbs Z.Cs H.omega H s' Q Rl := by + obtain ⟨Q0, R0, h0⟩ := bxi.1 + exact SchedQueuesInv.reCoreA h0 (by rw [J.tcbs]; exact sr.core) + have poolOff : PoolOff st H Z.Cs OS s' := fun i => by + have h0 := J.poolOff i + rw [vspace.asid_table.eq] at h0 ⊢ + have v := AtOff.v i + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.sys.ent.ksw + have ev : (wa (L.ksb s) (AtOff i)).val = (L.ksb s).val + (AtOff i).val := by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have e : L.mem s' (wa (L.ksb s') (AtOff i)) = L.mem s (wa (L.ksb s) (AtOff i)) := by + rw [kb] + refine w.mem_eq ?_ (fun h => h.elim (J.asid_offStepNA (by omega) (by omega)) (bks _ (by omega) (by omega))) + have h32 := J.j.1.ksbAl + show (wa (L.ksb s) (AtOff i)).val % 8 = 0 + omega + rw [e] + refine (WP.spec_ok _).2 fun hne asid => ?_ + have q := (WP.spec_ok _).1 h0 hne asid + exact ⟨q.al, q.om, q.sg, q.os, RegionOffNodes.ksb kb q.ks⟩ + have scCov : ScCovA Z.C m σ' := fun q x k h => by + obtain ⟨y, hy⟩ := SSz q x k h + exact J.scCov q y k hy + have hTr : TreeA st OS Z.G.chains Z.G.par s := + J.j.1.cdt.tree.grow J.j5.2.1 (fun x hx hn => J.j5.2.2 x hx hn) + have j5 : J5A st { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } H OS s' σ' m := + ⟨⟨MdbInvA.keepO hTr.lk hTr.tree J.j5.1.1 osk, IrqRunA.keepO hTr.lk J.j5.1.2.1 osk, + HandlersActiveA.keepO hTr.lk J.j5.1.2.2 osk kb (fun i hi => by + have eI : irqStAddrA (L.ksb s) i = irqWordA (L.ksb s) i := rfl + have hv := irqWordA_val J.sr.ksFit hi + have hal : Aligned (irqWordA (L.ksb s) i) := by + have := J.sr.toSchedSRN.ksb_al + show _ % 8 = 0 + omega + rw [eI] + have hksz : ksSizeA = 788736 := rfl + exact w.mem_eq hal (fun h => h.elim (J.irq_offStepNA hi) (bks _ (by omega) (by omega))))⟩, J.j5.2.1, + fun y hy hn => by rw [osk.capA hy]; exact J.j5.2.2 y hy hn⟩ + exact JStage3A.of_schedWrX (symL := symL) (schedValid := schedValid) (vcX := fun v hv o a b hb => J.bufOff.vcw _ hb v hv o a b rfl) (Q := Q) (Rl := Rl) + (curOf := curOf) (curScOf := curScOf) (a' := a) J J.bufXOffA (fun b hv hb => J.bufVs b hb hv) bufAl w HS SSz Qk hz J.j.1.ar hcurOf sys sq poolOff + sr scCov ep ntfn reply bufCover idleNoFault mcp yieldRel sinv sidle runVcpu blkQ j5 j7 + +set_option maxHeartbeats 1000000 in +/-- **`of_ipcStepA` across a body that also writes buffer words and user memory** (module docstring). -/ +theorem JStage3A.of_replyStepA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (w : WritesIn st s s' (fun b => SchedStepNWA (st := st) N E Rp Z m σ s b ∨ BufW Bf b)) (HS : SpecSchedA σ σ') (SSz : SSzA σ σ') (Qk : QKeepA σ σ') + (hz : ∀ x : Fl4SpecA64.Word, UtFreeA st OS s (fun d => d = false) (fl4Ptr x).val → + σ'.machineState.underlyingMemory x = σ.machineState.underlyingMemory x) + (hcurOf : ∀ k, k ≠ m.curCore → curOf k = Z.curOf k) + (bxi : BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s') + (sysl : SysL st H Z.Cs OS ia ie s') (symL : SymL st H Z.Cs s') + (userOK : Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s') + (j7 : J7A st (J7R2A st ar nA) Z H OS s' σ' m) + (sr : SchedSRA st { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } m κ s' σ') + (ep : EpStateRel Z.T E Rp Bf (L.mem s') σ') + (ntfn : NtfnStateRel N Z.T (L.mem s') σ') + (reply : ReplyStackRelA (L.mem s') Rp Z.C σ' (scNameA m) stack) + (K : IpcKeepA σ σ') + (tw : ∀ t ∈ Z.T, ∀ O : U64, O.val < 224 ∨ O.val = 3440 ∨ O.val = 3472 ∨ O.val = 3512 ∨ O.val = 3456 → O.val % 8 = 0 → + L.mem s' (wa t O) = L.mem s (wa t O)) + (sinv : SInvXA σ' m n) (sidle : SIdleA σ' m n) + (runVcpu : RunVcpuInv st Z.T s') (blkQ : BlkQueuedA Z.T E N (L.mem s') σ') + (schedValid : SchedValidA σ') : + JStage3A st AR { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } H OS U Ac Pp Tb Lv ia ie N E Rp + Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s' a σ' m := by + have at_ : ∀ t ∈ Z.T, ∃ x, σ.kheap (specPtr t) = some (.tcb x) := fun t ht => by + obtain ⟨x, hx, -⟩ := J.ntfn.tcb t ht + exact ⟨x, hx⟩ + have bufCover : BufCover st Z.T Bf s' σ' := by + intro u hu y b sb v hy hb hl + obtain ⟨x, hx⟩ := at_ u hu + obtain ⟨y0, hy0, -, hfr, hbuf, -, -⟩ := K.tcb _ _ hx + rw [hy] at hy0 + cases hy0 + have hb0 : ipcBufOfA b x = some sb := by + unfold ipcBufOfA at hb ⊢ + rwa [hfr, hbuf] at hb + have hib : IbSame (L := L) s s' u := by + refine ⟨?_, ?_, ?_⟩ + · rw [ibWordA_eq]; exact tw u hu _ (by simp) (by simp) + · rw [sbWordA_eq]; exact tw u hu _ (by simp) (by simp) + · rw [sbWordA8_eq]; exact tw u hu _ (by simp) (by simp) + rw [lookup_ipc_buffer_sameA hib] at hl + exact J.bufCover u hu x b sb v hx hb0 hl + have bufAl : UtBufAlA Z.T σ' := fun t ht y hy => by + obtain ⟨x, hx⟩ := at_ t ht + obtain ⟨y0, hy0, -, -, hbuf, -, -⟩ := K.tcb _ _ hx + rw [hy] at hy0 + cases hy0 + rw [hbuf]; exact J.bufAl t ht x hx + have idle : IdleNoFaultA m σ' := fun c hc => by + obtain ⟨x, hx, hf⟩ := J.idleNoFault c hc + obtain ⟨y, hy, e, -⟩ := K.tcb _ _ hx + exact ⟨y, hy, e.trans hf⟩ + have mcp : McpRelA Z.T m (L.mem s') σ' := + J.mcp.congrA (fun t ht => ⟨tw t ht _ (by simp) (by simp), tw t ht _ (by simp) (by simp)⟩) (fun t ht x hx => by + obtain ⟨y, hy, -, -, -, hm, hfl⟩ := K.tcb _ _ hx + exact ⟨y, hy, hm, hfl.2⟩) + have ye : ∀ t ∈ Z.T, L.mem s' (yieldA t) = L.mem s (yieldA t) := fun t ht => by + unfold yieldA; exact tw t ht _ (by simp) (by simp) + have yr : YieldRelA Z.T Z.C m (L.mem s') σ' := by + refine ⟨fun t ht => ?_, fun t ht h => ?_, fun c hc y k h p hp => ?_⟩ + · obtain ⟨x, hx, e⟩ := J.yieldRel.tcb t ht + obtain ⟨y, hy, -, -, -, -, hyt⟩ := K.tcb _ _ hx + exact ⟨y, hy, by rw [ye t ht, hyt.1]; exact e⟩ + · rw [ye t ht] at h ⊢ + obtain ⟨hC, y, k, hy, e⟩ := J.yieldRel.toSc t ht h + obtain ⟨y', k', hy', e'⟩ := K.scF _ _ _ hy + exact ⟨hC, y', k', hy', e'.trans e⟩ + · obtain ⟨y0, k0, h0, e⟩ := K.scB _ _ _ h + obtain ⟨t, ht, e1, e2⟩ := J.yieldRel.frm c hc y0 k0 h0 p (e ▸ hp) + exact ⟨t, ht, e1, by rw [ye t ht]; exact e2⟩ + exact J.of_replyFinA w HS SSz Qk hz hcurOf bxi sysl symL userOK j7 sr ep ntfn reply bufCover bufAl idle mcp yr sinv + sidle runVcpu blkQ schedValid + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3ReplyErrTopA.lean b/proofs/Fl4ProofsA64/P3/Stage3ReplyErrTopA.lean new file mode 100644 index 000000000..9257e70a8 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ReplyErrTopA.lean @@ -0,0 +1,708 @@ +/- +P3 (aarch64, A64-P3 Lift 1b, 4b): **`invocationA`'s `hErr`**, the code after a decode `Err`. + +* `replyErr_walkA`: `reply_error` of a record TCB against `fl4ReplyFromKernelError true` (one outcome). +* `JStage3A.replyErrA`: that walk at the unified record: `A64RelS3` at the spec's outcome (P1 from + `reply_error.rpw`, J7 by the fixed-witness step, the rest from the IPC views). +* `DecErrSpecA` [J]: **the spec decoder's error correspondence** (every cap kind): a non-preempted decode `Err` is a + syscall error the spec decode of the same invocation also returns. +* `DecErrKeepA` [J]: such an `Err` keeps the store and the arch relation. Proved for the non-arch caps and IRQ control + (`DecErrKeepA.of_arch`), so only the arch decoders remain. +* `JSyscallA.hErrA`: `invocationA`'s `hErr` from these two, with `rep = true` (FL4's + `KERNEL_INVOCATION_REPORT_ERROR_IPC`). + +Named premises: [J] `DecErrSpecA`, [J] the arch part of `DecErrKeepA`; [P] `ArchTotal`, `ArchPlat`, `MsiKeepA`; +[S] `hCs`, `numNodes = 1` (non-arch decoder stalls). New module. +-/ +import Fl4ProofsA64.P3.Stage3ReplyErrA +import Fl4ProofsA64.P3.Stage3ReplyErrGlueA +import Fl4ProofsA64.P3.Stage3WrapA +import Fl4ProofsA64.P3.Stage3DispErrA +import Fl4ProofsA64.P3.Stage3SiteA +import Fl4ProofsA64.P3.Stage3ScopeA +import Fl4ProofsA64.P3.IrqInvokeDispatchA +import Fl4ProofsA64.P3.Stage3J7StepA +import Fl4ProofsA64.P1.S1aCReplyErr +import Fl4ProofsA64.P3.Stage3HPreA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.ReplyErr +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3 Fl4ProofsA64.P3.Endp + Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {Clock : A → Fl4SpecA64.MachineState → Prop} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {ar : machine.Arch A} {nA : U64} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +private theorem dbg_idxA : syscall.DEBUG_MESSAGE_START + 1#u64 = ok 7#u64 := by + unfold syscall.DEBUG_MESSAGE_START + obtain ⟨z, hz, hv⟩ := spec_existsA (U64.add_spec (x := 6#u64) (y := 1#u64) (by scalar_tac)) + rw [hz] + exact congrArg ok (UScalar.eq_of_val_eq (by rw [hv]; rfl)) + +/-- **FL4's `reply_error` against the spec's error reply** (module docstring). -/ +theorem replyErr_walkA {R7 : Set U64 → JSlotA S} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st R7) s a σ m) + {t : U64} (ht : t ∈ Z.T) {x : Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) (e : error.SysError) + (e' : SyscallError) (hrel : VsErrRelA (.Sys e) e') : + syscall.reply_error st s t e ⦃ s' => ∃ σ', + fl4ReplyFromKernelError (Z := Unit) true (specPtr t) (msgFromSyscallError e') σ = ({((), σ')}, False) ∧ + ErrShapeA σ (specPtr t) x (BufBytesA (ipcBufOfA true x)) σ' ∧ + TcbIpcViewsA st Z m κ N E Rp Bf stack s' σ' ∧ WritesIn st s s' (RepW Bf t) ∧ + WritesIn st s s' (EWrite Z.T E Rp Bf) ⦄ := by + have hth : Z.th t = some .tcb := (J.sr.thT t).2 ht + have hP := J.iplaced + have P := J.ipcPlace + have V : TcbIpcViewsA st Z m κ N E Rp Bf stack s σ := + ⟨⟨Z.Q, Z.R, J.sr⟩, J.ntfn, J.ep, J.reply, J.mcp, J.yieldRel⟩ + obtain ⟨w, ew, hz, hbm⟩ := spec_existsA (lookup_buf_full s σ J.j.1.st hth ht hk J.bufCover J.bufNative true) + obtain ⟨c, ec, hcl, hcm⟩ := error_codeA e e' hrel + have hlen := msgErr_lenA e' + set lab := (msgFromSyscallError e').1 with hlab + set ms := (msgFromSyscallError e').2 with hms + have emsg : msgFromSyscallError e' = (lab, ms) := rfl + have eB : layout.reg.BADGE = regIdxA .x0 := by simp [layout.reg.BADGE, layout.reg.X0, regIdxA] + -- the badge register + set x1 := Notif.setReg x .x0 (0#u64 : U64).bv with hx1 + set σ1 := updK σ (specPtr t) (.tcb x1) with hσ1 + set s1 := L.wr s (Endp.regA t (regIdxA .x0)) 0#u64 with hs1 + obtain ⟨V1, w1, w2⟩ := regW_stepA (st := st) hP P ht (s0 := s) V (WritesIn.refl _ _) (WritesIn.refl _ _) hk .x0 0#u64 + have hk1 : σ1.kheap (specPtr t) = some (.tcb x1) := by simp [σ1, updK] + have elk : lookupIpcBuffer (Z := Unit) true (specPtr t) σ = ({(ipcBufOfA true x, σ)}, False) := + lookupIpcBuffer_oneA hk + have ebadge : asUser (Z := Unit) (specPtr t) (setRegister badge_register 0) σ = ({((), σ1)}, False) := + asUser_setReg_oneA hk _ _ + have notData : ∀ (y : Tcb) sz, (KernelObject.tcb y) ≠ .archObj (.dataPage false sz) := fun _ _ h => by cases h + unfold syscall.reply_error + rw [ew, bind_ok, eB, ut_set_regA, bind_ok] + change (syscall.error_mrs st s1 t w e >>= _) ⦃ _ ⦄ + cases ho : ipcBufOfA true x with + | some sb => + have hm : (w, sb) ∈ Bf := hbm sb ho + have h0 : w ≠ 0#u64 := fun h => by have := hz.1 h; rw [ho] at this; cases this + have I0 : ErrInvA (st := st) Z m κ N E Rp Bf stack t s (mrState σ1 (specPtr t) x1 sb) s1 [] := by + unfold ErrInvA + rw [mrState_nil hk1 sb] + exact ⟨V1, w1, w2⟩ + refine WP.spec_bind (error_mrs_genA (errSetMr_bufA (st := st) hP P ht (bp := (w, sb)) hm h0) rfl I0 e e' hrel) ?_ + rintro ⟨len, s2⟩ ⟨hl, V2, w1', w2'⟩ + try simp only [WP.uncurry'_pair] + rw [← hms] at hl V2 + simp only [id] at hl + set σ2 := mrState σ1 (specPtr t) x1 sb ms with hσ2 + set x2 := withRegs x1 (mrRegs x1.tcbArch.tcbContext.userRegs ms) with hx2 + have hk2 : σ2.kheap (specPtr t) = some (.tcb x2) := mrState_kheap _ _ _ _ _ + have h7 : (7#u64 : U64).val < 128 := by simp + have hbuf := V2.ep.buf (w, sb) hm 7#u64 h7 + set wd := L.mem s2 (bufW w 7#u64) with hwd + set v := wd &&& ~~~255#u64 with hv + obtain ⟨V3, hW3⟩ := bufWordW_viewsA hP P V2 hm h7 v + set s3 := L.wr s2 (bufW w 7#u64) v with hs3 + set σ3 := setUm σ2 (storeMem σ2.machineState.underlyingMemory (bufAddr sb 7) v.bv) with hσ3 + have hk3 : σ3.kheap (specPtr t) = some (.tcb x2) := hk2 + have hw7 : BufW Bf (bufW w 7#u64) := ⟨(w, sb), hm, _, h7, rfl⟩ + have w13 : WritesIn st s s3 (RepW Bf t) := + (w1'.trans (writesIn_wr (hP.e.balign _ hw7) (v := v))).mono fun b h => h.elim id (fun e => Or.inr (e ▸ hw7)) + have w23 : WritesIn st s s3 (EWrite Z.T E Rp Bf) := (w2'.trans hW3).mono fun _ h => h.elim id id + set len1 := wa len 1#u64 with hlen1 + have hl1 : len1.val = ms.length + 1 := by + simp [len1, core.num.U64.wrapping_add_val_eq, U64.size_def, U64.numBits, hl]; omega + obtain ⟨mi, emi, hmi⟩ := spec_existsA (to_word_specA c len1 (by omega)) + set x4 := Notif.setReg x2 msg_info_register mi.bv with hx4 + obtain ⟨V4, w14, w24⟩ := regW_stepA (st := st) hP P ht V3 w13 w23 hk3 msg_info_register mi + set σ4 := updK σ3 (specPtr t) (.tcb x4) with hσ4 + have hne : (w != 0#u64) = true := by simpa using h0 + have eget : msg.buf_get st s2 w 7#u64 = ok wd := by rw [buf_get_eqA]; rfl + have eprog : ∀ Q : S → Prop, Q (L.wr s3 (Endp.regA t (regIdxA msg_info_register)) mi) → + (do + let (s5, len2) ← + if fl4_abi.config.KERNEL_INVOCATION_REPORT_ERROR_IPC then + if w != 0#u64 then do + let i ← syscall.DEBUG_MESSAGE_START + 1#u64 + let wv ← msg.buf_get st s2 w i + let i1 ← lift (~~~ 255#u64) + let i2 ← lift (wv &&& i1) + let s4 ← msg.buf_set st s2 w i i2 + let len2 ← lift (core.num.U64.wrapping_add len 1#u64) + ok (s4, len2) + else ok (s2, len) + else ok (s2, len) + let i ← error.SysError.code e + let mi ← msginfo.MessageInfo.new i 0#u64 0#u64 len2 + msg.set_msg_info st s5 t mi) ⦃ Q ⦄ := by + intro Q hQ + simp only [fl4_abi.config.KERNEL_INVOCATION_REPORT_ERROR_IPC, ↓reduceIte, hne, dbg_idxA, bind_ok, eget, lift, + buf_set_eq] + rw [ec] + change (ok c >>= fun i => msginfo.MessageInfo.new i 0#u64 0#u64 len1 >>= fun mi' => + msg.set_msg_info st s3 t mi') ⦃ Q ⦄ + rw [bind_tc_ok] + unfold msginfo.MessageInfo.new msg.set_msg_info + rw [bind_tc_ok] + change (msginfo.MessageInfo.to_word { label := c, caps_unwrapped := 0#u64, extra_caps := 0#u64, length := len1 } + >>= fun i => obj.Tcb.set_reg st t s3 layout.reg.MSG_INFO i) ⦃ Q ⦄ + rw [emi, bind_tc_ok, ← regIdxA_msgInfo, ut_set_regA, WP.spec_ok] + exact hQ + refine eprog _ ⟨σ4, ?_, ?_, V4, w14, w24⟩ + · have hal : ∀ k, k < 128 → (bufAddr sb k).toNat % 8 = 0 := fun k hk' => by + have ev : (⟨BitVec.ofNat 64 k⟩ : U64).val = k := by + show (BitVec.ofNat 64 k).toNat = k; simp; omega + have := (V.ep.buf (w, sb) hm ⟨BitVec.ofNat 64 k⟩ (by rw [ev]; exact hk')).2 + rwa [ev] at this + have huf := J.replyBufFrameA ht hk true sb ho + have huf1 : ∀ k, k < 128 → inUserFrame (bufAddr sb k) σ1 := fun k hk' => + inUserFrame_updK hk (notData _) _ (huf k hk') + have huf2 : inUserFrame (bufAddr sb 7) σ2 := inUserFrame_updK hk1 (notData _) _ (huf1 7 (by omega)) + have emrs := setMrs_eq (σ := σ1) hk1 sb ms (fun q hq => + ⟨hal _ (mrPairs_ltA ms q hq), huf1 _ (mrPairs_ltA ms q hq)⟩) + have eload : loadWordOffs (Z := Unit) sb (debugMessageStart + 1) σ2 = ({(wd.bv, σ2)}, False) := by + have h := loadWordOffs_eqA σ2 sb 7 (hal 7 (by omega)) + refine h.trans ?_ + have e2 : wd.bv = loadWordAtA σ2.machineState.underlyingMemory (bufAddrA sb 7) := hbuf.1 + rw [e2] + have estore : storeWordOffs (Z := Unit) sb (debugMessageStart + 1) (wd.bv &&& ~~~255) σ2 = + ({((), σ3)}, False) := + storeWordOffs_eq σ2 sb 7 _ (hal 7 (by omega)) huf2 + have einfo : messageInfoToData ⟨natToLen (min ms.length msgMaxLength) + 1, 0, 0, lab⟩ = mi.bv := by + have el : natToLen (min ms.length msgMaxLength) + 1 = len1.bv := by + rw [bv_ofNat_valA len1 hl1, Nat.min_eq_left (by unfold msgMaxLength; omega)] + simp [natToLen, BitVec.ofNat_add] + rw [hmi, el, hcm, hcl] + try rfl + have eset : setMessageInfo (Z := Unit) (specPtr t) + ⟨natToLen (min ms.length msgMaxLength) + 1, 0, 0, lab⟩ σ3 = ({((), σ4)}, False) := by + unfold setMessageInfo + rw [einfo] + exact asUser_setReg_oneA hk3 _ _ + unfold fl4ReplyFromKernelError + rw [emsg, if_pos rfl] + dsimp only + rw [nd_bind_oneA (show (pure (lab, ms) : SMonad Unit (Data × List Data)) σ = ({((lab, ms), σ)}, False) from rfl)] + dsimp only + rw [nd_bind_oneA elk, ho, nd_bind_oneA ebadge, nd_bind_oneA emrs] + dsimp only + rw [nd_bind_oneA eload, nd_bind_oneA estore] + exact eset + · refine ⟨x4.tcbArch.tcbContext, σ4.machineState.underlyingMemory, ?_, fun b hb => ?_⟩ + · simp only [σ4, σ3, σ2, σ1, mrState, setUm, updK, Function.update_idem] + rfl + · show storeMem (mrMem σ.machineState.underlyingMemory sb (mrPairs ms)) (bufAddr sb 7) v.bv b = _ + rw [storeBuf_offA (by omega) _ _ _ hb, mrMem_offA hb _ _ (mrPairs_ltA ms)] + | none => + have hw0 : w = 0#u64 := hz.2 ho + subst hw0 + have I0 : ErrInvA (st := st) Z m κ N E Rp Bf stack t s (mrStateNA σ1 (specPtr t) x1) s1 [] := by + unfold ErrInvA + rw [mrStateNA_nil hk1] + exact ⟨V1, w1, w2⟩ + refine WP.spec_bind (error_mrs_genA (errSetMr_nobufA (st := st) hP P ht) rfl I0 e e' hrel) ?_ + rintro ⟨len, s2⟩ ⟨hl, V2, w1', w2'⟩ + try simp only [WP.uncurry'_pair] + rw [← hms] at hl V2 + set σ2 := mrStateNA σ1 (specPtr t) x1 ms with hσ2 + set x2 := withRegs x1 (mrRegs x1.tcbArch.tcbContext.userRegs ms) with hx2 + have hk2 : σ2.kheap (specPtr t) = some (.tcb x2) := mrStateNA_kheap _ _ _ _ + simp only at hl + obtain ⟨mi, emi, hmi⟩ := spec_existsA (to_word_specA c len (by omega)) + set x4 := Notif.setReg x2 msg_info_register mi.bv with hx4 + obtain ⟨V4, w14, w24⟩ := regW_stepA (st := st) hP P ht V2 w1' w2' hk2 msg_info_register mi + set σ4 := updK σ2 (specPtr t) (.tcb x4) with hσ4 + simp only [fl4_abi.config.KERNEL_INVOCATION_REPORT_ERROR_IPC, ↓reduceIte, bne_self_eq_false, Bool.false_eq_true, + bind_ok] + rw [ec] + change (ok c >>= fun i => msginfo.MessageInfo.new i 0#u64 0#u64 len >>= fun mi' => msg.set_msg_info st s2 t mi') ⦃ _ ⦄ + rw [bind_tc_ok] + unfold msginfo.MessageInfo.new msg.set_msg_info + rw [bind_tc_ok] + change (msginfo.MessageInfo.to_word { label := c, caps_unwrapped := 0#u64, extra_caps := 0#u64, length := len } + >>= fun i => obj.Tcb.set_reg st t s2 layout.reg.MSG_INFO i) ⦃ _ ⦄ + rw [emi, bind_tc_ok, ← regIdxA_msgInfo, ut_set_regA, WP.spec_ok] + refine ⟨σ4, ?_, ?_, V4, w14, w24⟩ + · have emrs := setMrs_eqNA (σ := σ1) hk1 ms + have einfo : messageInfoToData ⟨natToLen (min 4 ms.length), 0, 0, lab⟩ = mi.bv := by + have el : natToLen (min 4 ms.length) = len.bv := by + rw [bv_ofNat_valA len hl, Nat.min_comm]; rfl + rw [hmi, el, hcm, hcl] + try rfl + have eset : setMessageInfo (Z := Unit) (specPtr t) ⟨natToLen (min 4 ms.length), 0, 0, lab⟩ σ2 = + ({((), σ4)}, False) := by + unfold setMessageInfo + rw [einfo] + exact asUser_setReg_oneA hk2 _ _ + unfold fl4ReplyFromKernelError + rw [emsg, if_pos rfl] + dsimp only + rw [nd_bind_oneA (show (pure (lab, ms) : SMonad Unit (Data × List Data)) σ = ({((lab, ms), σ)}, False) from rfl)] + dsimp only + rw [nd_bind_oneA elk, ho, nd_bind_oneA ebadge, nd_bind_oneA emrs] + exact eset + · refine ⟨x4.tcbArch.tcbContext, σ.machineState.underlyingMemory, ?_, fun _ _ => rfl⟩ + simp only [σ4, σ2, σ1, mrStateNA, setUm, updK, Function.update_idem] + rfl + +/-- **The error reply at the unified record** (module docstring). -/ +theorem _root_.Fl4ProofsA64.P3.JSyscallA.replyErrA (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + {a' : A} (har : AR a' Z.hw) (e : error.SysError) (e' : SyscallError) (hrel : VsErrRelA (.Sys e) e') : + syscall.reply_error st s (L.curW s) e ⦃ s' => ∃ σ', + fl4ReplyFromKernelError (Z := Unit) true σ.curThread (msgFromSyscallError e') σ = ({((), σ')}, False) ∧ + A64RelS3 st AR ar hPl.ipa n Z.Cs s' a' σ' m ⦄ := by + have J' := J.toJStage3A.arStateA har + have ct : σ.curThread = specPtr (L.curW s) := J.j.1.sch.curThread + have htZ : L.curW s ∈ Z.T := J.sr.curT + obtain ⟨x, hk, -⟩ := J.ep.tcb (L.curW s) htZ + have hrun := J.curRunA (ct ▸ hk : σ.kheap σ.curThread = some (.tcb x)) + have ht : L.curW s ∈ H.tcbs := by rw [J'.tcbs]; exact htZ + generalize htdef : L.curW s = t at * + -- P1 + obtain ⟨Pt, Ub, Pp7, Tb7, Hr, Te, Kt, Lv7, Re, bits, KLv, W⟩ := J7A.unp J'.j7 + have I := J'.delIn hA hPl W + have hbk : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s := by + obtain ⟨q, b, x', sg, sc⟩ := J'.sys.ent.bxi + exact ⟨q, b, x', sc, sg⟩ + have p1 := syscall.reply_error.rpw (st := st) t e I J'.sys.sysl J'.symL hbk J'.core0 J'.ko ht + refine WP.spec_mono (WP.spec_and (replyErr_walkA J' htZ hk e e' hrel) p1) ?_ + rintro s' ⟨⟨σ', hspec, ⟨uc, um, hσ', hum⟩, V, w1, w2⟩, P3⟩ + refine ⟨σ', by rw [ct]; exact hspec, ?_⟩ + have h3 := P3.2.2.1 + have bx3 : BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s' := by + obtain ⟨q, b, x', sc, sg⟩ := h3 + exact ⟨q, b, x', sg, sc⟩ + have d3 := (fun q => ⟨q.1, q.2, P3.2.2.2.2.1.1, P3.2.2.2.2.2⟩ : + ArmQIAt st H Z.Cs OS Z.V ia ie s s' ∧ TabKeepOrZero st s s' → + ArmQIAt st H Z.Cs OS Z.V ia ie s s' ∧ TabKeepOrZero st s s' ∧ ArmVsK st H OS Z.V s s' ∧ + XferSlots2 st H OS Z.V s s') + (ArmQIAt.ofXC (Vc := Z.V) ⟨P3.1, P3.2.1⟩ ⟨Ac, P3.2.2.1⟩ P3.2.2.2.1 P3.2.2.2.2.1.1.toVs) + have Fx := S1aStepFix.ofXfer2 (n := hPl.ipa) d3 I.del.vr I.fu I.vcc W.col W.df W.r2 W.sep W.hiK + have hks : L.ksb s' = L.ksb s := w2.1 + have hPN : PoolNamed st OS s' := d3.1.2.2.2.2.2.1 W.pn + have hsp : ∀ p, SchedPageA st Z OS s' p → p ∈ Pt := by + have Fx' := Fx + obtain ⟨_, -, -, -, ⟨_, _, _, _, _, _, _, R'⟩, -⟩ := Fx' + exact schedPage_ptA R' W.sp + have j7 : J7A st (J7R2A st ar hPl.ipa) Z H OS s' σ' m := + J7A.of_stepFixedA Fx W.vr hks W.al W.vu hPN hsp (fun b hb o ho => by rw [hks]; exact W.ks b hb o ho) + (J7R2K.of_stepFixedA (σ' := σ') (m := m) Fx W.ktK) + -- the spec state + set x' : Tcb := { x with tcbArch := { x.tcbArch with tcbContext := uc } } with hx' + have atT : σ'.kheap (specPtr t) = some (.tcb x') := by rw [hσ']; simp [setUm, updK, x'] + have same : ∀ p, p ≠ specPtr t → σ'.kheap p = σ.kheap p := fun p e => by + rw [hσ']; simp [setUm, updK, Function.update_of_ne e] + have keepK : ∀ p ob, σ.kheap p = some ob → (∀ y, ob ≠ .tcb y) → σ'.kheap p = some ob := fun p ob h ho => by + have e : p ≠ specPtr t := fun e => by rw [e, hk] at h; exact ho x (Option.some.inj h).symm + rw [same p e]; exact h + have backK : ∀ p ob, σ'.kheap p = some ob → (∀ y, ob ≠ .tcb y) → σ.kheap p = some ob := fun p ob h ho => by + by_cases e : p = specPtr t + · rw [e, atT] at h; exact absurd (Option.some.inj h).symm (ho x') + · rw [← same p e]; exact h + have tcbF : ∀ p y, σ.kheap p = some (.tcb y) → ∃ y', σ'.kheap p = some (.tcb y') ∧ + y' = { y with tcbArch := y'.tcbArch } := fun p y hy => by + by_cases e : p = specPtr t + · rw [e, hk] at hy; cases hy + exact ⟨x', e ▸ atT, rfl⟩ + · exact ⟨y, by rw [same p e]; exact hy, rfl⟩ + have tcbB : ∀ p y', σ'.kheap p = some (.tcb y') → ∃ y, σ.kheap p = some (.tcb y) ∧ + y' = { y with tcbArch := y'.tcbArch } := fun p y' hy => by + by_cases e : p = specPtr t + · rw [e, atT] at hy; cases hy + exact ⟨x, e ▸ hk, rfl⟩ + · exact ⟨y', by rw [← same p e]; exact hy, rfl⟩ + have HS : SpecSchedA σ σ' := + { kh := fun p => by + by_cases e : p = specPtr t + · exact Or.inr (Or.inl ⟨x, x', e ▸ hk, e ▸ atT, ⟨rfl, rfl, rfl⟩⟩) + · exact Or.inl (same p e) + irqS := by rw [hσ']; rfl + irqN := by rw [hσ']; rfl + asid := by rw [hσ']; rfl + vcur := by rw [hσ']; rfl + cdt := by rw [hσ']; rfl + orig := by rw [hσ']; rfl } + have SSz : SSzA σ σ' := fun p y k h => ⟨y, backK p _ h (fun _ e => by cases e)⟩ + have Qk : QKeepA σ σ' := by + have h := QKeepA.upd (o' := .tcb x') hk (QObjA.ofTcb rfl) + rw [hσ']; exact ⟨h.prio, h.ep, h.ntfn, h.sc, h.rt, ⟨rfl, rfl, rfl, rfl⟩⟩ + have K : IpcKeepA σ σ' := + ⟨fun p y h => by + obtain ⟨y', h', e⟩ := tcbF p y h + exact ⟨y', h', by rw [e], by rw [e], by rw [e], by rw [e], by rw [e], by rw [e]⟩, + fun p y k h => ⟨y, k, keepK p _ h (fun _ e => by cases e), rfl⟩, + fun p y k h => ⟨y, k, backK p _ h (fun _ e => by cases e), rfl⟩⟩ + have hta : TcbAddr t := J'.sr.core.tcbs t htZ + -- the words kept + have tw : ∀ u ∈ Z.T, ∀ O : U64, O.val < 4096 → O.val % 8 = 0 → (O.val < 256 ∨ 552 ≤ O.val) → + L.mem s' (wa u O) = L.mem s (wa u O) := by + intro u hu O h1 h8 hO + have hua := J'.sr.core.tcbs u hu + have hv := field_val hua h1 + refine w1.mem_eq (field_aligned hua h1 h8) fun h => ?_ + rcases h with ⟨r, hr⟩ | hb + · have hrv := regA_val hta (regIdxA_lt r) + have hlt := regIdxA_lt r + have ev := congrArg UScalar.val hr + rw [hv, hrv] at ev + by_cases e : u = t + · subst e; omega + · have hf := hua.fits + have hf' := hta.fits + have h1' := hua.align + have h2' := hta.align + have hne : u.val ≠ t.val := fun h => e (UScalar.eq_of_val_eq h) + omega + · have := J'.iplaced.e.bt _ hb u hu; omega + have stW : ∀ u ∈ Z.T, L.mem s' (stA u) = L.mem s (stA u) := fun u hu => by + rw [stA_eq]; exact tw u hu _ (by simp) (by simp) (by simp) + have T : ∀ p y', σ'.kheap p = some (.tcb y') → ∃ y, σ.kheap p = some (.tcb y) ∧ y'.tcbState = y.tcbState ∧ + y'.tcbSchedContext = y.tcbSchedContext ∧ y'.tcbFault = y.tcbFault := fun p y' h => by + obtain ⟨y, hy, e⟩ := tcbB p y' h + exact ⟨y, hy, by rw [e], by rw [e], by rw [e]⟩ + have F : ∀ p y, σ.kheap p = some (.tcb y) → ∃ y', σ'.kheap p = some (.tcb y') ∧ y'.tcbState = y.tcbState ∧ + y'.tcbSchedContext = y.tcbSchedContext ∧ y'.tcbFault = y.tcbFault := fun p y h => by + obtain ⟨y', hy, e⟩ := tcbF p y h + exact ⟨y', hy, by rw [e], by rw [e], by rw [e]⟩ + have sinv : SInvXA σ' m n := by + refine J'.sinv.of_fr + { tcb := fun p y h => ?_, tcb' := fun p y h => ?_, sc := fun p y k => ?_, cur := by rw [hσ']; rfl, + idle := by rw [hσ']; rfl, csc := by rw [hσ']; rfl, rq := by rw [hσ']; rfl, rls := by rw [hσ']; rfl, + act := by rw [hσ']; rfl, time := by rw [hσ']; rfl, cons := by rw [hσ']; rfl } ?_ + · obtain ⟨y', hy, e1, e2⟩ := F p y h + exact ⟨y', hy, e1, e2⟩ + · obtain ⟨y0, hy, e1, e2⟩ := T p y h + exact ⟨y0, hy, e1, e2⟩ + · exact ⟨fun h => backK _ _ h (fun _ e => by cases e), fun h => keepK _ _ h (fun _ e => by cases e)⟩ + · have h : SpecIrqA (setObjA σ (specPtr t) (.tcb x')) := J'.sinv.irqN.updTcbA hk rfl + rw [hσ']; exact ⟨h.node, h.slot, h.ref⟩ + have sidle : SIdleA σ' m n := + { f3 := fun c hc => by + obtain ⟨y, hy, hs⟩ := J'.sidle.f3 c hc + obtain ⟨y', hy', e1, -⟩ := F _ _ hy + exact ⟨y', hy', e1.trans hs⟩ + f4 := fun y k h => J'.sidle.f4 y k (backK _ _ h (fun _ e => by cases e)) + f5 := fun p y k h he => J'.sidle.f5 p y k (backK _ _ h (fun _ e => by cases e)) (by rw [hσ'] at he; exact he) + f6 := J'.sidle.f6.of_eq fun sl => by + unfold specCapAtA + by_cases e : sl.1 = specPtr t + · rw [e, atT, hk] + try rfl + · rw [same _ e] + f7 := fun y k h => by + have h7 := J'.sidle.f7 y k (backK _ _ h (fun _ e => by cases e)) + have hT : σ'.curTime = σ.curTime := by rw [hσ']; rfl + rw [hT]; exact h7 + idleSlot := fun c hc => by + have h0 := J'.sidle.idleSlot c hc + unfold vtCapA at h0 ⊢ + by_cases e : m.idleThreadOf c = specPtr t + · rw [e, atT]; rw [e, hk] at h0; exact h0 + · rw [same _ e]; exact h0 + idleBound := fun c hc => by + have h0 := J'.sidle.idleBound c hc + unfold vcpuOfA at h0 ⊢ + by_cases e : m.idleThreadOf c = specPtr t + · rw [e, atT]; rw [e, hk] at h0; exact h0 + · rw [same _ e]; exact h0 } + have runVcpu : RunVcpuInv st Z.T s' := fun u hu h0 => by + have rvw : L.mem s' (runVcpuA u) = L.mem s (runVcpuA u) := + tw u hu 3672#u64 (by simp) (by simp) (by simp) + rw [rvw] at h0 + rw [stW u hu] + exact J'.runVcpu u hu h0 + have blkQ : BlkQueuedA Z.T E N (L.mem s') σ' := by + have tb : ∀ u ∈ Z.T, L.mem s' (tBlockA u) = L.mem s (tBlockA u) := fun u hu => + tw u hu 3336#u64 (by simp) (by simp) (by simp) + refine ⟨fun u hu y h e hb => ?_, fun u hu y h p hb => ?_⟩ + · obtain ⟨y0, hy0, e1, -⟩ := T _ _ h + obtain ⟨c, z, hz, hm⟩ := J'.blkQ.ep u hu y0 hy0 e (e1 ▸ hb) + exact ⟨by rw [tb u hu]; exact c, z, keepK _ _ hz (fun _ e => by cases e), hm⟩ + · obtain ⟨y0, hy0, e1, -⟩ := T _ _ h + obtain ⟨c, z, l, hz, hl, hm⟩ := J'.blkQ.ntfn u hu y0 hy0 p (e1 ▸ hb) + exact ⟨by rw [tb u hu]; exact c, z, l, keepK _ _ hz (fun _ e => by cases e), hl, hm⟩ + have sv : SchedValidA σ' := by + have sc : ∀ p, ScActiveAtA σ p → ScActiveAtA σ' p := fun p ⟨y, k, e, act⟩ => + ⟨y, k, keepK p _ e (fun _ h => by cases h), act⟩ + have tcbAct : ∀ u, (∃ y c, σ.kheap u = some (.tcb y) ∧ y.tcbSchedContext = some c ∧ ScActiveAtA σ c) → + ∃ y c, σ'.kheap u = some (.tcb y) ∧ y.tcbSchedContext = some c ∧ ScActiveAtA σ' c := + fun u ⟨y, c, h, es, act⟩ => by + obtain ⟨y', h', -, es'⟩ := F u y h + exact ⟨y', c, h', es'.1.trans es, sc _ act⟩ + have hq : σ'.readyQueues = σ.readyQueues := by rw [hσ']; rfl + have hrl : σ'.releaseQueue = σ.releaseQueue := by rw [hσ']; rfl + have hact : σ'.schedulerAction = σ.schedulerAction := by rw [hσ']; rfl + have hc : σ'.curThread = σ.curThread := by rw [hσ']; rfl + have hi : σ'.idleThread = σ.idleThread := by rw [hσ']; rfl + refine ⟨fun d p u hm => ?_, fun e1 e2 => ?_, fun u y c e1 e2 e3 => ?_, fun u hu => ?_⟩ + · rw [hq] at hm + obtain ⟨act, nr⟩ := J'.schedValid.rq d p u hm + exact ⟨tcbAct u act, by rw [hrl]; exact nr⟩ + · rw [hact] at e1 + rw [hc, hi] at e2 + obtain ⟨act, nr⟩ := J'.schedValid.act e1 e2 + rw [hc, hrl] + exact ⟨tcbAct _ act, nr⟩ + · obtain ⟨y0, hy0, es, ec, -⟩ := T _ _ e1 + exact sc _ (J'.schedValid.blk u y0 c hy0 (es ▸ e2) (ec ▸ e3)) + · rw [hrl] at hu + exact tcbAct u (J'.schedValid.rls u hu) + obtain ⟨Q2, R2, H2⟩ := id V.rel + have J2 := J'.of_replyStepA (Q := Q2) (Rl := R2) (curOf := Z.curOf) (curScOf := Z.curScOf) (s' := s') (σ' := σ') + (w2.mono fun b h => ewrite_stepNA_buf J'.sr.core.tcbs h) HS SSz Qk + (J'.replyBufZeroA htZ hk (fun b hb => by rw [hσ']; exact hum b hb)) + (fun _ _ => rfl) bx3 P3.1 P3.2.1 (J'.sys.ent.user.ksb hks) j7 H2 V.ep V.ntfn V.stk K + (fun u hu O hO h8 => tw u hu O (by omega) h8 (by omega)) sinv sidle runVcpu blkQ sv + refine A64RelS3.ofJ J2 ?_ + refine Sched.BodyOKA.ofEntry J (Or.inl (by rw [hσ']; rfl)) (by rw [hσ']; rfl) (by rw [hσ']; rfl) + (by rw [hσ']; rfl) (by rw [hσ']; rfl) (by rw [hσ']; rfl) (by rw [hσ']; rfl) + (fun y' k h => ⟨y', backK _ _ h (fun _ e => by cases e), rfl, rfl, rfl⟩) (fun _ z hz => ?_) + have ec : σ'.curThread = σ.curThread := by rw [hσ']; rfl + have hz' : σ'.kheap (specPtr t) = some (.tcb z) := by rw [← ct]; exact hz + rw [atT] at hz' + cases hz' + exact hrun + +/-! ## `hErr` -/ + +/-- `syscall` along a successful front end and a decode error. -/ +theorem syscall_errA {α β γ : Type} {mF : FMonad Unit α} {hF : Fl4SpecA64.Fault → SMonad Unit γ} + {mE : α → SEMonad Unit β} {hE : SyscallError → SMonad Unit γ} {mFin : β → PMonad Unit γ} + {σ τ : Fl4SpecA64.State Unit} {x : α} {e' : SyscallError} {v : γ} (h1 : mF.run σ = ({(.ok x, σ)}, False)) + (h2 : (.error e', σ) ∈ ((mE x).run σ).1) (h : (v, τ) ∈ (hE e' σ).1) : + (.ok v, τ) ∈ ((syscall mF hF mE hE mFin).run σ).1 := by + unfold syscall withoutPreemption + rw [run_bind_okA (run_liftEA (show (ExceptT.run mF : SMonad Unit _) σ = ({(.ok x, σ)}, False) from h1))] + dsimp only + rw [ExceptT.run_bind] + exact mem_bindA.2 ⟨(.ok (.error e'), σ), liftE_memA (m := ExceptT.run (mE x)) h2, liftE_memA h⟩ + +variable (st) in +/-- [J] **The spec decoder's error correspondence for the caps satisfying `P`**: a decode `Err` other than +`Preempted` of such a cap is a syscall error which the spec's decode of the same invocation (its front-end values) +also returns. Split per cap group in `P3/Stage3DecErrA`. -/ +def DecErrSpecPA (ar : machine.Arch A) (th : TypedHeapA) (s : S) (a : A) (σ : Fl4SpecA64.State Unit) + (calling blocking donate : Bool) (cptr : U64) (P : capability.Cap → Prop) : Prop := + ∀ (info : msginfo.MessageInfo) (buf : U64) (r1 : cspace.Resolved) (e : Aeneas.Std.Array obj.Slot 3#usize) + (x : error.Exc) (s' : S) (a' : A) (mi : Fl4SpecA64.MessageInfo) (sl : Fl4SpecA64.CslotPtr) + (c : Fl4SpecA64.Cap) (excaps : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) + (buffer : Option Fl4SpecA64.ObjRef) (args : List Fl4SpecA64.Data), + msg.msg_info st s (L.curW s) = ok info → msg.lookup_ipc_buffer st s false (L.curW s) = ok buf → + cspace.lookup_slot st s (L.curW s) cptr = ok (.Ok r1) → + ipc.lookup_extra_caps st s (L.curW s) buf info = ok (.Ok e) → + syscall.decode_invocation st ar s a info.label (msgLenA info buf) r1.slot (capA st s r1.slot) blocking + calling donate false buf e = ok (.Err x, s', a') → x ≠ error.Exc.Preempted → P (capA st s r1.slot) → + mi.miLabel = info.label.bv → r1.slot = cslotAddrA sl → CapRelA (capA st s r1.slot) c → + ExtraOKA th (L.mem s) e excaps → AbsSlotA th sl → + BufViewA (L.mem s) σ.machineState.underlyingMemory buf buffer → + getMessageInfo (Z := Unit) σ.curThread σ = ({(mi, σ)}, False) → + getCap (Z := Unit) sl σ = ({(c, σ)}, False) → + lookupIpcBuffer (Z := Unit) false σ.curThread σ = ({(buffer, σ)}, False) → + (lookupExtraCaps (Z := Unit) σ.curThread buffer mi).run σ = ({(.ok excaps, σ)}, False) → + getMrs (Z := Unit) σ.curThread buffer mi σ = ({(args, σ)}, False) → + ∃ (e2 : error.SysError) (e' : SyscallError), x = .Sys e2 ∧ VsErrRelA x e' ∧ + (.error e', σ) ∈ ((fl4DecodeInvocation (Z := Unit) calling false mi.miLabel args + (Fl4SpecA64.toBl cptr.bv) sl c excaps buffer).run σ).1 + +variable (st) in +/-- [J] **The spec decoder's error correspondence** (every cap kind): `DecErrSpecPA` for all caps. -/ +abbrev DecErrSpecA (ar : machine.Arch A) (th : TypedHeapA) (s : S) (a : A) (σ : Fl4SpecA64.State Unit) + (calling blocking donate : Bool) (cptr : U64) : Prop := + DecErrSpecPA st ar th s a σ calling blocking donate cptr (fun _ => True) + +variable (st) in +/-- [J] **A decode `Err` other than `Preempted` keeps the store and the arch relation.** -/ +def DecErrKeepA (ar : machine.Arch A) (AR : A → HwRootA → Prop) (Z : XSetsA) (s : S) (a : A) + (blocking calling donate : Bool) (cptr : U64) (P : capability.Cap → Prop) : Prop := + ∀ (info : msginfo.MessageInfo) (buf : U64) (r1 : cspace.Resolved) (e : Aeneas.Std.Array obj.Slot 3#usize) + (x : error.Exc) (s' : S) (a' : A), + msg.msg_info st s (L.curW s) = ok info → msg.lookup_ipc_buffer st s false (L.curW s) = ok buf → + cspace.lookup_slot st s (L.curW s) cptr = ok (.Ok r1) → + ipc.lookup_extra_caps st s (L.curW s) buf info = ok (.Ok e) → + syscall.decode_invocation st ar s a info.label (msgLenA info buf) r1.slot (capA st s r1.slot) blocking + calling donate false buf e = ok (.Err x, s', a') → x ≠ error.Exc.Preempted → + P (capA st s r1.slot) → s' = s ∧ AR a' Z.hw + +/-- **`DecErrKeepA` from its arch caps**: the non-arch caps and IRQ control keep the store and the arch relation. -/ +theorem DecErrKeepA.of_arch (hA : P0.ArchTotal ar) (hM : MsiKeepA ar AR) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < Fl4SpecA64.fl4A64Platform.numNodes) (h1 : Fl4SpecA64.fl4A64Platform.numNodes = 1) + {blocking calling donate : Bool} {cptr : U64} + (hArch : DecErrKeepA st ar AR Z s a blocking calling donate cptr (fun c => ¬ NonArchA c ∧ c ≠ .IrqControl)) : + DecErrKeepA st ar AR Z s a blocking calling donate cptr (fun _ => True) := by + intro info buf r1 e x s' a' einfo ebuf elr eex hd hne _ + by_cases hn : NonArchA (capA st s r1.slot) + · obtain ⟨_, _, _, _, _, _, Q⟩ := J.siteA einfo ebuf elr eex + have k := syscall.decode_invocation.errKA hA s a info.label (msgLenA info buf) r1.slot (capA st s r1.slot) + blocking calling donate false buf e hn (J.stallSitesA hCs h1 Q.hOS) + rw [hd, WP.spec_ok] at k + have e2 := Prod.mk.inj (k x rfl hne) + exact ⟨e2.1, by rw [e2.2]; exact J.j.1.ar⟩ + by_cases hi : capA st s r1.slot = .IrqControl + · have k := interrupt.decode_irq_control.errKA (st := st) (ar := ar) hA s a (fun a => AR a Z.hw) J.j.1.ar + (fun irq dev ev cpu => hM.msi_map a irq dev ev cpu _ J.j.1.ar) info.label (msgLenA info buf) r1.slot buf e + have hd' := hd + rw [hi, decodeIrqControl_programA] at hd' + rw [hd', WP.spec_ok] at k + exact k x rfl hne + exact hArch info buf r1 e x s' a' einfo ebuf elr eex hd hne ⟨hn, hi⟩ + +/-- **`invocationA`'s `hErr`** (module docstring). -/ +theorem _root_.Fl4ProofsA64.P3.JSyscallA.hErrA (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (calling blocking donate : Bool) (cptr : U64) + (hSpec : DecErrSpecA st ar Z.th s a σ calling blocking donate cptr) + (hKeep : DecErrKeepA st ar AR Z s a blocking calling donate cptr (fun _ => True)) : + ∀ (info : msginfo.MessageInfo) (buf : U64) (r1 : cspace.Resolved) + (e : Aeneas.Std.Array obj.Slot 3#usize) (x : error.Exc) (s' : S) (a' : A), + msg.msg_info st s (L.curW s) = ok info → msg.lookup_ipc_buffer st s false (L.curW s) = ok buf → + cspace.lookup_slot st s (L.curW s) cptr = ok (.Ok r1) → + ipc.lookup_extra_caps st s (L.curW s) buf info = ok (.Ok e) → + syscall.decode_invocation st ar s a info.label (msgLenA info buf) r1.slot (capA st s r1.slot) blocking + calling donate false buf e = ok (.Err x, s', a') → x ≠ error.Exc.Preempted → + (match x with + | .Sys e2 => if calling then (do let s2 ← syscall.reply_error st s' (L.curW s) e2 + ok (core.result.Result.Ok (), s2, a')) + else ok (core.result.Result.Ok (), s', a') + | _ => ok (core.result.Result.Ok (), s', a') : Result (core.result.Result Unit error.Exc × S × A)) + ⦃ r => r.1 = .Ok () ∧ ∃ τ, + (.ok (), τ) ∈ ((fl4HandleInvocationR (Z := Unit) true calling blocking donate false + (Fl4SpecA64.toBl cptr.bv)).run σ).1 ∧ A64RelS3 st AR ar hPl.ipa n Z.Cs r.2.1 r.2.2 τ m ⦄ := by + intro info buf r1 e x s' a' einfo ebuf elr eex hd hne + obtain ⟨es, har⟩ := hKeep info buf r1 e x s' a' einfo ebuf elr eex hd hne trivial + subst es + have ct : σ.curThread = specPtr (L.curW s') := J.j.1.sch.curThread + have htZ : L.curW s' ∈ Z.T := J.sr.curT + have hth : Z.th (L.curW s') = some .tcb := (J.j.1.thT _).2 htZ + obtain ⟨y, hk, -⟩ := J.ep.tcb (L.curW s') htZ + -- the spec's front end, as `invocationA`'s + obtain ⟨info', einfo', hlab, -, hext, -⟩ := spec_existsA (msg_info_specA (L := L) s' (L.curW s')) + obtain rfl : info' = info := by rw [einfo] at einfo'; exact (okinj einfo').symm + set mi := Fl4SpecA64.dataToMessageInfo (y.tcbArch.tcbContext.userRegs Fl4SpecA64.msg_info_register) with hmi + have hmw := (J.ep.at_tcb htZ hk).regView.1 + have gmi : getMessageInfo (Z := Unit) (specPtr (L.curW s')) σ = ({(mi, σ)}, False) := getMessageInfo_eqA hk + obtain ⟨buf', ebuf', hbuf0, hbufS⟩ := + spec_existsA (lookup_buf_full s' σ J.j.1.st hth htZ hk J.bufCover J.bufNative false) + obtain rfl : buf' = buf := by rw [ebuf] at ebuf'; exact (okinj ebuf').symm + have eIB : lookupIpcBuffer (Z := Unit) false (specPtr (L.curW s')) σ = ({(ipcBufOfA false y, σ)}, False) := + lookupIpcBuffer_eqA hk false + have hBV : BufViewA (L.mem s') σ.machineState.underlyingMemory buf' (ipcBufOfA false y) := by + cases ho : ipcBufOfA false y with + | none => rw [hbuf0.2 ho]; exact bufViewA_none _ _ + | some sb => + refine J.ep.bufView (hbufS sb ho) fun h => ?_ + rw [hbuf0.1 h] at ho; cases ho + obtain ⟨lr, elr', se, hse, hrel⟩ := spec_existsA + (lookup_slot_refinesA Z.th s' σ J.j.1.st J.j.1.cv (L.curW s') hth cptr) + obtain rfl : lr = .Ok r1 := by rw [elr] at elr'; exact (okinj elr').symm + cases se with + | error _ => exact hrel.1.elim + | ok v => + obtain ⟨sl, rest⟩ := v + obtain ⟨⟨hsl, -⟩, p, hp1, hp, -⟩ := hrel + obtain ⟨c, hcr, hgc⟩ := J.j.1.st.slot_capA hp1 hp + have lcs := lcs_okA (ofBl (Fl4SpecA64.toBl cptr.bv)) hse hgc + have hn : info'.extra_caps.val = unat mi.miExtraCaps := by + show info'.extra_caps.bv.toNat = _; rw [hext, hmw] + have hn3 : info'.extra_caps.val ≤ 3 := by + show info'.extra_caps.bv.toNat ≤ 3; rw [hext, hmw]; exact extra_le3A _ + obtain ⟨r2, er2, hx⟩ := spec_existsA + (lookup_extra_relA J.j.1.st J.j.1.cv hth buf' (ipcBufOfA false y) hBV info' mi hn hn3) + obtain rfl : r2 = .Ok e := by rw [eex] at er2; exact (okinj er2).symm + obtain ⟨excaps, hrun, hok⟩ := hx + obtain ⟨args, gargs⟩ : ∃ args, getMrs (Z := Unit) (specPtr (L.curW s')) (ipcBufOfA false y) mi σ = + ({(args, σ)}, False) := ⟨_, getMrs_eqA hk _ hBV.al mi⟩ + have hcr' : CapRelA (capA st s' r1.slot) c := by rw [hsl, ← capAtA_eq]; exact hcr + have hlab' : mi.miLabel = info'.label.bv := by + rw [hlab, hmw] + exact label_maskA _ + obtain ⟨e2, e', rfl, hrel, hdec⟩ := hSpec info' buf' r1 e x s' a' mi sl c excaps (ipcBufOfA false y) args einfo ebuf + elr eex hd hne trivial hlab' hsl hcr' hok ⟨p, hp1, Or.inl hp⟩ hBV (by rw [ct]; exact gmi) hgc (by rw [ct]; exact eIB) + (by rw [ct]; exact hrun) (by rw [ct]; exact gargs) + have front : (frontSpecA (specPtr (L.curW s')) (Fl4SpecA64.toBl cptr.bv) mi).run σ = + ({(.ok (sl, c, excaps, ipcBufOfA false y), σ)}, False) := by + unfold frontSpecA + rw [run_bind_okA lcs] + dsimp only + rw [run_bind_okA (run_liftEA eIB), run_bind_okA hrun] + rfl + have decRun : (.error e', σ) ∈ ((decSpecA calling (specPtr (L.curW s')) (Fl4SpecA64.toBl cptr.bv) mi + (sl, c, excaps, ipcBufOfA false y)).run σ).1 := by + unfold decSpecA + dsimp only + rw [run_bind_okA (run_liftEA gargs)]; exact hdec + have whole : ∀ τ, ((), τ) ∈ ((NonDet.when calling <| + fl4ReplyFromKernelError (Z := Unit) true (specPtr (L.curW s')) (msgFromSyscallError e')) σ).1 → + (.ok (), τ) ∈ ((fl4HandleInvocationR (Z := Unit) true calling blocking donate false + (Fl4SpecA64.toBl cptr.bv)).run σ).1 := fun τ h => by + have gc : (NonDet.gets fun s : Fl4SpecA64.State Unit => s.curThread) σ = + ({(specPtr (L.curW s'), σ)}, False) := by rw [← ct]; rfl + rw [fl4HandleInvocationR_piecesA, run_bind_okA (run_liftEA gc)] + unfold withoutPreemption + rw [run_bind_okA (run_liftEA gmi)] + exact syscall_errA front decRun h + dsimp only + cases calling with + | false => + rw [if_neg Bool.false_ne_true, WP.spec_ok] + refine ⟨rfl, σ, whole σ ?_, A64RelS3.ofJ (J.toJStage3A.arStateA har) J.bodyOKA⟩ + unfold Fl4Spec.NonDet.when + exact Set.mem_singleton _ + | true => + rw [if_pos rfl] + refine WP.spec_bind (J.replyErrA hA hPl har e2 e' hrel) fun s2 ⟨τ, hrun2, R⟩ => ?_ + rw [WP.spec_ok] + refine ⟨rfl, τ, whole τ ?_, R⟩ + unfold Fl4Spec.NonDet.when + rw [if_pos rfl, ← ct, hrun2] + exact Set.mem_singleton _ + +/-- **Send, NBSend and Call with the decode errors closed**: `sendBodyScopeA` with `hErr` from `hErrA`. -/ +theorem _root_.Fl4ProofsA64.P3.JSyscallA.sendBodyErrA [PlainStore st] (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (hCnt : Sched.CounterStepA σ) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) (hIP : IrqPlatA st ar) {pending : SMonad Unit Unit} (call : Fl4SpecA64.Syscall) + (hc : call = .sysSend ∨ call = .sysNBSend ∨ call = .sysCall) + (hFault : ∀ calling blocking donate, + DispatchInvokeAtA (L.mem s (regWA (L.curW s) layout.reg.CAP)) (L.mem s (regWA (L.curW s) layout.reg.REPLY)) + (L.mem s (regWA (L.curW s) layout.reg.NBSEND_RECV_DEST)) call calling blocking donate false + (L.mem s (regWA (L.curW s) layout.reg.CAP)) → + blocking = true → ∀ (f : error.Fault) (f' : Fl4SpecA64.Fault), CapFaultCorrA f f' → + faults.handle_fault st ar s a (L.curW s) f ⦃ r => ∃ τ, ((), τ) ∈ (handleFault (Z := Unit) σ.curThread f' σ).1 ∧ + A64RelS3 st AR ar hPl.ipa n Z.Cs r.1 r.2 τ m ⦄) + (hArm : ∀ calling blocking donate, + DispatchInvokeAtA (L.mem s (regWA (L.curW s) layout.reg.CAP)) (L.mem s (regWA (L.curW s) layout.reg.REPLY)) + (L.mem s (regWA (L.curW s) layout.reg.NBSEND_RECV_DEST)) call calling blocking donate false + (L.mem s (regWA (L.curW s) layout.reg.CAP)) → + ∀ (info : msginfo.MessageInfo) (buf : U64) (r1 : cspace.Resolved) (e : Aeneas.Std.Array obj.Slot 3#usize) + (mi : Fl4SpecA64.MessageInfo) (sl : Fl4SpecA64.CslotPtr) (c : Fl4SpecA64.Cap) + (excaps : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) (buffer : Option Fl4SpecA64.ObjRef) (argsU : List U64), + msg.msg_info st s (L.curW s) = ok info → msg.lookup_ipc_buffer st s false (L.curW s) = ok buf → + cspace.lookup_slot st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.CAP)) = ok (.Ok r1) → + ipc.lookup_extra_caps st s (L.curW s) buf info = ok (.Ok e) → + SiteA st Z OS s σ info buf r1.slot e mi sl c excaps buffer argsU → + ∀ (r0 : core.result.Result Unit error.Exc) (s0 : S) (a0 : A), + syscall.decode_invocation st ar s a info.label (msgLenA info buf) r1.slot (capA st s r1.slot) blocking + calling donate false buf e = ok (r0, s0, a0) → (∀ x, r0 = .Err x → x = error.Exc.Preempted) → + ∃ Out, DispScopeA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ m Clock hPl.ipa info.label + (msgLenA info buf) r1.slot blocking calling donate false buf e Out) + (hSpec : ∀ calling blocking donate, + DecErrSpecA st ar Z.th s a σ calling blocking donate (L.mem s (regWA (L.curW s) layout.reg.CAP))) + (hKeep : ∀ calling blocking donate, + DecErrKeepA st ar AR Z s a blocking calling donate (L.mem s (regWA (L.curW s) layout.reg.CAP)) (fun _ => True)) + (XP : ExitPlatA st ar Z) (HEnv : HPreEnvA st AR ar hPl n Z.Cs m Clock pending s a call) : + syscall.dispatch_syscall st ar s a (syscallNumberA call) ⦃ r => + StepPostA (fun s a τ => A64RelS3 st AR ar hPl.ipa n Z.Cs s a τ m) + (fl4SettleDispatch pending (fl4DispatchSyscall (fl4HandleInvocationR (Z := Unit) true) call)) σ r ⦄ := + J.sendBodyScopeA (hCnt := hCnt) hA hM hPl hfm hIP true call hc hFault hArm + (fun calling blocking donate _ => J.hErrA hA hPl calling blocking donate _ (hSpec calling blocking donate) + (hKeep calling blocking donate)) XP HEnv + +end + +end + +end Fl4ProofsA64.P3.ReplyErr diff --git a/proofs/Fl4ProofsA64/P3/Stage3ReplyExitA.lean b/proofs/Fl4ProofsA64/P3/Stage3ReplyExitA.lean new file mode 100644 index 000000000..3d37b2e25 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ReplyExitA.lean @@ -0,0 +1,273 @@ +/- +P3 (aarch64, A64-P3 line RecvPreA): **what the live Reply leaves for a following `handle_recv`** (spec only). + +`ReplyExitA t r σ σ4 σ'` relates the entry state `σ` of `do_reply_transfer`, its state `σ4` before the SC tail and its end +state `σ'`: the current SC's budget and the last machine time are kept from `σ`; the tail changes only the queues, the +reprogram flag and the scheduler action, queues at most the receiver `t`, and leaves `SwA`; a receiver with its own SC +takes none, so every thread keeps its SC. + +* `doIpcTransfer_normal_exA`: a fault-free message transfer keeps the current SC's budget and the last machine time. +* `replyRemove_keepScA`: `reply_remove` of a caller with an SC keeps every thread's SC (no donation). + +Named premises: none. New module. +-/ +import Fl4ProofsA64.P3.Stage3RecvSendCscA +import Fl4ProofsA64.P3.Stage3RecvSendSwA +import Fl4ProofsA64.P3.Stage3IpcSchedValidA +import Fl4ProofsA64.P3.IpcScReplyTransferA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4ProofsA64.P3.Endp + Fl4ProofsA64.P3.Notif Classical + +noncomputable section + +/-- **The SC tail's step** from `σ4` to `σ'` for the receiver `t`. -/ +structure ReplyTailXA (t : ObjRef) (σ4 σ' : State Unit) : Prop where + frame : σ' = { σ4 with readyQueues := σ'.readyQueues, releaseQueue := σ'.releaseQueue, + reprogramTimer := σ'.reprogramTimer, schedulerAction := σ'.schedulerAction } + rq : (∀ u, σ4.schedulerAction ≠ .switchThread u) → ∀ d p y, y ∈ σ'.readyQueues d p → + y ∈ σ4.readyQueues d p ∨ y = t + rls : ∀ y ∈ σ'.releaseQueue, y = t ∨ y ∈ σ4.releaseQueue + act : σ'.schedulerAction = σ4.schedulerAction ∨ σ'.schedulerAction = .chooseNewThread ∨ + σ'.schedulerAction = .switchThread t + sw : (∀ u, σ4.schedulerAction ≠ .switchThread u) → SwA σ' + +theorem ReplyTailXA.refl (t : ObjRef) (σ : State Unit) : ReplyTailXA t σ σ := + ⟨rfl, fun _ _ _ _ h => Or.inl h, fun _ h => Or.inr h, Or.inl rfl, fun h u e => absurd e (h u)⟩ + +/-- Every thread's SC is kept. -/ +def AllScA (a b : State Unit) : Prop := + ∀ p x, a.kheap p = some (.tcb x) → ∃ y, b.kheap p = some (.tcb y) ∧ y.tcbSchedContext = x.tcbSchedContext + +/-- **The live Reply's exit facts** (module docstring). -/ +structure ReplyExitA (t r : ObjRef) (σ σ4 σ' : State Unit) : Prop where + csc : CscKA σ σ' + lmt : σ'.machineState.lastMachineTime = σ.machineState.lastMachineTime + tail : ReplyTailXA t σ4 σ' + keepSc : (∀ x, σ.kheap t = some (.tcb x) → x.tcbSchedContext ≠ none) → ∀ p x, σ.kheap p = some (.tcb x) → + ∃ y, σ'.kheap p = some (.tcb y) ∧ y.tcbSchedContext = x.tcbSchedContext + /-- Every thread keeps the SC it has after `reply_remove` of the reply `r`. -/ + afterRm : ∃ σr, ((), σr) ∈ (replyRemove (Z := Unit) t r σ).1 ∧ AllScA σr σ' + +/-- The current SC's budget and the last machine time. -/ +def CsLmA (a b : State Unit) : Prop := CscKA a b ∧ b.machineState.lastMachineTime = a.machineState.lastMachineTime + +instance : PreO CsLmA where + refl σ := ⟨PreO.refl σ, rfl⟩ + trans h1 h2 := ⟨PreO.trans h1.1 h2.1, h2.2.trans h1.2⟩ + +theorem BKeepA.toCsLm {a b : State Unit} (h : BKeepA a b) : CsLmA a b := ⟨h.toCsc, by rw [h.ms]⟩ + +theorem csLm_of_kpA {α : Type} (c : SMonad Unit α) [h : KPresBInst c] {σ : State Unit} {r : α × State Unit} + (hr : r ∈ (c σ).1) : CsLmA σ r.2 := BKeepA.toCsLm (h.pres σ r hr) + +/-- The frame of the message transfer with the budget and the time kept. -/ +def UCsLmA (B : Word → Prop) (a b : State Unit) : Prop := UFrA B a b ∧ CsLmA a b + +instance (B : Word → Prop) : PreO (UCsLmA B) where + refl σ := ⟨PreO.refl σ, PreO.refl σ⟩ + trans h1 h2 := ⟨PreO.trans h1.1 h2.1, PreO.trans h1.2 h2.2⟩ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **A fault-free `do_ipc_transfer`** keeps the current SC's budget and the last machine time. -/ +theorem doIpcTransfer_normal_exA {th : TypedHeapA} {kb : U64} (hPl : PlaceA th kb) (sf : S) {σ : State Unit} + (hR : StateRelA th (L.mem sf) σ) (hV : SlotsValidA th (L.mem sf)) {snd rcv : U64} + (htr : th rcv = some .tcb) {xs xr : Tcb} (hks : σ.kheap (specPtr snd) = some (.tcb xs)) + (hkr : σ.kheap (specPtr rcv) = some (.tcb xr)) (hf : xs.tcbFault = none) + (hal : ∀ rb, ipcBufOfA true xr = some rb → (bufAddr rb 0).toNat % 8 = 0) + (ep : Option ObjRef) (badge : Badge) (grant : Bool) + (hcaps : grant = false ∨ ipcBufOfA false xs = none ∨ + unat (dataToMessageInfo (xs.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0) : + ∀ x ∈ (doIpcTransfer (Z := Unit) (specPtr snd) ep badge grant (specPtr rcv) σ).1, CsLmA σ x.2 := by + intro x hx + have hS : ∀ rb, ipcBufOfA true xr = some rb → ∀ k < 128, ∀ v, + PresR (UCsLmA (BufBytesA (ipcBufOfA true xr))) (storeWordOffs (Z := Unit) rb k v) := by + intro rb erb k hk v σ0 r hr + have a : UFrA (BufBytesA (ipcBufOfA true xr)) σ0 r.2 := by + rw [erb]; exact storeWordOffs_ufrA rb hk v σ0 r hr + exact ⟨a, storeWordOffs_cscA rb k v σ0 r hr, + ((show NdTmA (storeWordOffs (Z := Unit) rb k v) by unfold storeWordOffs; tm) σ0 r hr).2.1⟩ + exact (doIpcTransfer_genA (R := UCsLmA (BufBytesA (ipcBufOfA true xr))) (fun _ _ h => h.1) + (fun t f σ0 r hr => ⟨PresR.ufrA (asUser_presA t f) σ0 r hr, + ((KPresBInst.pres : KPresB (asUser (Z := Unit) t f)) σ0 r hr).toCsc, (asUser_tmA t f σ0 r hr).2.1⟩) + (fun a σ0 r hr => ⟨PresR.ufrA (doMachineOp_presA _ (loadWord_keepA a)) σ0 r hr, doMachineOp_cscA _ σ0 r hr, + (doMachineOp_mtmA (loadWord_mtmA a) σ0 r hr).2.1⟩) + (rbo := ipcBufOfA true xr) hS hPl sf hR hV htr hks hkr rfl hf hal _ _ _ hcaps σ rfl x hx).2 + +end + +theorem AllScA.trans {a b c : State Unit} (h1 : AllScA a b) (h2 : AllScA b c) : AllScA a c := fun p x h => by + obtain ⟨y, hy, e⟩ := h1 p x h + obtain ⟨z, hz, e'⟩ := h2 p y hy + exact ⟨z, hz, e'.trans e⟩ + +theorem AllScA.of_sik {i : ObjRef} {M N : ObjRef → Prop} {a b : State Unit} (R : SIKA i M N false a b) : + AllScA a b := fun _ _ h => R.tcbScA h + +/-- **`reply_remove` of a caller with an SC** donates nothing. -/ +theorem replyRemove_keepScA {i t r : ObjRef} {σ σ1 : State Unit} {x : Tcb} (hkt : σ.kheap t = some (.tcb x)) + (hx : x.tcbSchedContext ≠ none) (h : ((), σ1) ∈ (replyRemove (Z := Unit) t r σ).1) : AllScA σ σ1 := by + unfold replyRemove at h + obtain ⟨⟨rp, s1⟩, h1, h⟩ := mem_bindA.1 h + have e1 : σ = s1 := getReply_eqPres r σ _ h1 + subst e1 + simp only at h + obtain ⟨u, hu, h⟩ := mem_bindA.1 h + have eu : σ = u.2 := (PresR.assert (R := @Eq (State Unit)) _) σ u hu + rw [← eu] at h + obtain ⟨⟨o, s2⟩, h2, h⟩ := mem_bindA.1 h + have e2 : σ = s2 := (PresR.gets (R := @Eq (State Unit)) _) σ _ h2 + subst e2 + simp only at h + cases o with + | none => + dsimp only at h + have h' : ((), σ1) ∈ (replyUnlinkTcb (Z := Unit) t r σ).1 := h + exact AllScA.of_sik (replyUnlinkTcb_sikA (i := i) (M := (· = t)) (N := (· = t)) (don := false) t r rfl σ _ h') + | some p => + dsimp only at h + obtain ⟨⟨l, s3⟩, h3, h⟩ := mem_bindA.1 h + unfold NonDet.liftM at h3 + obtain ⟨⟨y, s4⟩, h3a, h3b⟩ := mem_bindA.1 h3 + have e4 : s4 = σ := (getSchedContext_eqPres p σ _ h3a).symm + obtain ⟨el, es3⟩ := Prod.mk.inj (show (l, s3) = (y.scReplies, s4) from h3b) + rw [e4] at es3 + rw [es3] at h + simp only at h + obtain ⟨⟨c, s5⟩, h5, h⟩ := mem_bindA.1 h + unfold getTcbObjRef at h5 + obtain ⟨ec, es5⟩ := Prod.mk.inj (threadGet_valA Tcb.tcbSchedContext hkt h5) + rw [es5] at h + simp only at h + obtain ⟨v, hv, h⟩ := mem_bindA.1 h + have T : TKeepA σ v.2 := replyUnlinkSc_tpres p r σ v hv + obtain ⟨w, hw, h⟩ := mem_bindA.1 h + unfold NonDet.when at hw + split_ifs at hw with hg + · exact absurd (ec.symm.trans hg.2) hx + have ew : w = ((), v.2) := hw + rw [ew] at h + have S : SIKA i (· = t) (· = t) false v.2 σ1 := + replyUnlinkTcb_sikA (i := i) (M := (· = t)) (N := (· = t)) (don := false) t r rfl v.2 _ h + refine AllScA.trans (fun q z hz => ?_) (AllScA.of_sik S) + obtain ⟨-, -, -, -, -, -, fwd, -⟩ := T + exact fwd q z hz + +/-- **`reply_remove` of a caller without an SC** whose reply heads the SC `p` donates `p` to the caller; every other +thread keeps its SC or loses it. -/ +theorem replyRemove_donateScA {i t r p : ObjRef} {σ σ1 : State Unit} {xt : Tcb} {y : SchedContext} {k : Nat} + (hkt : σ.kheap t = some (.tcb xt)) (hx : xt.tcbSchedContext = none) (hsw : scWithReply r σ = some p) + (hk : σ.kheap p = some (.schedContext y k)) (hh : y.scReplies.head? = some r) + (h : ((), σ1) ∈ (replyRemove (Z := Unit) t r σ).1) : + (∃ z, σ1.kheap t = some (.tcb z) ∧ z.tcbSchedContext = some p) ∧ + ∀ q x, q ≠ t → σ.kheap q = some (.tcb x) → + ∃ z, σ1.kheap q = some (.tcb z) ∧ (z.tcbSchedContext = x.tcbSchedContext ∨ z.tcbSchedContext = none) := by + unfold replyRemove at h + obtain ⟨⟨rp, s1⟩, h1, h⟩ := mem_bindA.1 h + have e1 : σ = s1 := getReply_eqPres r σ _ h1 + subst e1 + simp only at h + obtain ⟨u, hu, h⟩ := mem_bindA.1 h + have eu : σ = u.2 := (PresR.assert (R := @Eq (State Unit)) _) σ u hu + rw [← eu] at h + obtain ⟨⟨o, s2⟩, h2, h⟩ := mem_bindA.1 h + obtain ⟨eo, es2⟩ := Prod.mk.inj (show (o, s2) = (scWithReply r σ, σ) from h2) + rw [es2, eo, hsw] at h + simp only at h + obtain ⟨⟨l, s3⟩, h3, h⟩ := mem_bindA.1 h + unfold NonDet.liftM at h3 + obtain ⟨⟨y', s4⟩, h3a, h3b⟩ := mem_bindA.1 h3 + rw [getSchedContext_eqA hk] at h3a + obtain ⟨ey, e4⟩ := Prod.mk.inj (show (y', s4) = (y, σ) from h3a) + obtain ⟨el, es3⟩ := Prod.mk.inj (show (l, s3) = (y'.scReplies, s4) from h3b) + rw [e4] at es3 + rw [es3] at h + simp only at h + obtain ⟨⟨c, s5⟩, h5, h⟩ := mem_bindA.1 h + unfold getTcbObjRef at h5 + obtain ⟨ec, es5⟩ := Prod.mk.inj (threadGet_valA Tcb.tcbSchedContext hkt h5) + rw [es5] at h + simp only at h + obtain ⟨v, hv, h⟩ := mem_bindA.1 h + have T : TKeepA σ v.2 := replyUnlinkSc_tpres p r σ v hv + obtain ⟨-, -, -, -, -, -, fwd, -⟩ := T + obtain ⟨w, hw, h⟩ := mem_bindA.1 h + have hg : Fl4SpecA64.the l.head? = r ∧ c = none := by + refine ⟨?_, ec.trans hx⟩ + rw [el, ey, hh]; rfl + unfold NonDet.when at hw + rw [if_pos hg] at hw + have S : SIKA i (· = t) (· = t) false w.2 σ1 := + replyUnlinkTcb_sikA (i := i) (M := (· = t)) (N := (· = t)) (don := false) t r rfl w.2 _ h + -- the donation + unfold schedContextDonate at hw + have hg0 : PresR (@Eq (State Unit)) (getScObjRef (Z := Unit) SchedContext.scTcb p) := by + unfold getScObjRef + exact PresR.bind (getSchedContext_eqPres p) (fun _ => PresR.pure _) + obtain ⟨fo, hfo, hw⟩ := eqPres_mem hg0 hw + obtain ⟨d, hd, hw⟩ := mem_bindA.1 hw + let Q : State Unit → State Unit → Prop := fun a b => ∀ q x, a.kheap q = some (.tcb x) → + ∃ z, b.kheap q = some (.tcb z) ∧ (z.tcbSchedContext = x.tcbSchedContext ∨ z.tcbSchedContext = none) + have Qt : ∀ a b c : State Unit, Q a b → Q b c → Q a c := fun a b c h1 h2 q x hq => by + obtain ⟨z, hz, e1⟩ := h1 q x hq + obtain ⟨z', hz', e2⟩ := h2 q z hz + refine ⟨z', hz', ?_⟩ + rcases e2 with e2 | e2 + · rw [e2]; exact e1 + · exact Or.inr e2 + have Qa : ∀ a b : State Unit, AllScA a b → Q a b := fun a b h q x hq => by + obtain ⟨z, hz, e⟩ := h q x hq + exact ⟨z, hz, Or.inl e⟩ + have D : Q v.2 d.2 := by + unfold NonDet.when at hd + split_ifs at hd with hne + swap + · have : d = ((), v.2) := hd + rw [this] + exact Qa _ _ fun q x hq => ⟨x, hq, rfl⟩ + obtain ⟨f, -, hd⟩ := eqPres_mem (PresR.assertOpt _) hd + obtain ⟨a, ha, hd⟩ := mem_bindA.1 hd + obtain ⟨b, hb, hd⟩ := mem_bindA.1 hd + obtain ⟨c', hc, hd⟩ := mem_bindA.1 hd + refine Qt _ _ _ (Qa _ _ (AllScA.of_sik (tcbSchedDeq_sikA (i := i) (M := (· = t)) (N := (· = t)) (don := false) + f _ a ha))) (Qt _ _ _ (Qa _ _ (AllScA.of_sik (tcbReleaseRemove_sikA (i := i) (M := (· = t)) (N := (· = t)) + (don := false) f _ b hb))) (Qt _ _ _ ?_ (Qa _ _ (AllScA.of_sik (testReschedule_sikA (i := i) (M := (· = t)) + (N := (· = t)) (don := false) f _ d hd))))) + unfold setTcbObjRef at hc + obtain ⟨xf, hxf, ec'⟩ := threadSet_memA _ _ hc + intro q x hq + by_cases e : q = f + · have k : c'.2.kheap q = some (.tcb (tcbSchedContextUpdate (fun _ => none) xf)) := by + rw [ec', e]; exact Function.update_self .. + exact ⟨_, k, Or.inr rfl⟩ + · rw [ec'] + exact ⟨x, (Function.update_of_ne e _ _).trans hq, Or.inl rfl⟩ + obtain ⟨v2, hv2, hw⟩ := mem_bindA.1 hw + unfold setScObjRef at hv2 + obtain ⟨y2, k2, hk2, ev⟩ := updateSchedContext_memA _ _ hv2 + have E : Q d.2 v2.2 := fun q x hq => by + have ne : q ≠ p := fun e => by rw [e, hk2] at hq; cases hq + exact ⟨x, by rw [ev]; exact (Function.update_of_ne ne _ _).trans hq, Or.inl rfl⟩ + unfold setTcbObjRef at hw + obtain ⟨xl, hxl, el'⟩ := threadSet_memA _ _ hw + have F : Q σ v2.2 := Qt _ _ _ (fun q x hq => by + obtain ⟨z, hz, e⟩ := fwd q x hq + exact ⟨z, hz, Or.inl e⟩) (Qt _ _ _ D E) + have wt : w.2.kheap t = some (.tcb { xl with tcbSchedContext := some p }) := by + rw [el']; exact Function.update_self .. + refine ⟨?_, fun q x hq hqx => ?_⟩ + · obtain ⟨z, hz, e⟩ := AllScA.of_sik S t _ wt + exact ⟨z, hz, e⟩ + · obtain ⟨z, hz, e⟩ := F q x hqx + have : w.2.kheap q = some (.tcb z) := by + rw [el']; exact (Function.update_of_ne hq _ _).trans hz + obtain ⟨z', hz', e'⟩ := AllScA.of_sik S q z this + exact ⟨z', hz', by rw [e']; exact e⟩ + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3ReplyFaultArmA.lean b/proofs/Fl4ProofsA64/P3/Stage3ReplyFaultArmA.lean new file mode 100644 index 000000000..d5faa5d9e --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ReplyFaultArmA.lean @@ -0,0 +1,403 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2b): **the live Reply arm for a faulted caller at the record**: Reply on a reply +object whose caller is `BlockedOnReply` on it and has a pending fault `f`. + +* **`JSyscallA.replyFaultA`**: `decode_invocation` on a Reply cap: the replier's message is the fault reply + (`handle_fault_reply`), the caller's fault is cleared and it runs again (`Restart`) or is left `Inactive`, the SC + goes back to a caller without one. The record follows `JStage3A.of_ipcStepUXA` (the fault clear is not an + `IpcKeepA` step: `IpcKeepNA.ofClr`, `SIKA.clrX` of `P3/Stage3ReplyFaultGlueA`). + +The spec data (reply object, grant, caller) come from the cap read at the entry (`hcap`) and the spec reply object +(`hk` / `hcl`); no free datum. + +[S] (scope selectors, each a separate named hypothesis) `hrcv` (the caller waits `BlockedOnReply` with the pending +fault `f`), `hok` (`FaultReplyOKA`: an `UnknownSyscall` reply is +short or was not sent in the long form, DESIGN.md §9); `h1` (`numNodes = 1`). The caller's timeout handler may be an +endpoint cap (the SC tail's nested timeout send, `JStage3A.replyTmoA`): it names an endpoint of `E` (`JStage3A.tmoHandlerEpA`). A VCPU-bound caller and every `Timeout` +reply are covered (the spec is `fl4HandleFaultReply`). `hL` (`LoopInvA`) of `do_reply_transfer_keepFA` is discharged +here from `J` (`sinv`, `sidle`, `sact`), so it is not a premise of the arm. The +replier's own fault is not read (`hRplNoFault` is not needed). [P] `ArchTotal`, `MaxSpi`, `ArchPlat`, +`A64FrameMasks`, `hCs`, `hmb`, `hmax`, `htu`, `hconv`. New module. +-/ +import Fl4ProofsA64.P3.Stage3ReplyTmoDispA +import Fl4ProofsA64.P3.Stage3ReplyFaultGlueA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {J5 : JSlotA S} {R : Set U64 → JSlotA S} {s s' : S} {a : A} {σ : State Unit} {m : McState} + +set_option maxHeartbeats 2000000 in +/-- **Reply to a faulted caller blocked on the reply object, at a syscall entry** (module docstring). -/ +theorem JSyscallA.replyFaultA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hag : fl4MinBudget = MIN_BUDGET) (hmb : ∀ a, Fl4MinBudgetAgreeA ar a) + {r slot lbl len buf : U64} {grant blk call don fp : Bool} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Reply r grant) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hr : r ∈ Rp) {xr : Reply} (hk : σ.kheap (specPtr r) = some (.reply xr)) {d : U64} + (hcl : xr.replyTcb = some (specPtr d)) + {f : Fault} + (hrcv : ∀ y, σ.kheap (specPtr d) = some (.tcb y) → + y.tcbState = .blockedOnReply (specPtr r) ∧ y.tcbFault = some f) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) (hconv : Fl4TicksToUsLegacyAgree) + (hok : ∀ x, σ.kheap σ.curThread = some (.tcb x) → + FaultReplyOKA (L := L) s d + (unat (dataToMessageInfo (x.tcbArch.tcbContext.userRegs msg_info_register)).miLength) f) : + ∃ xc σ1 σ2 s2, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ((), σ2) ∈ (fl4DoReplyTransfer (Z := Unit) σ.curThread (specPtr r) grant σ1).1 ∧ + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don fp buf extra = + ok (.Ok (), s2, a) ∧ + Sched.WakeEndA σ σ2 ∧ + ∃ Q Rl stack', JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack' κ n + (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s2 a σ2 m := by + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hslot hcov J.actW J.running J.cur + have hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM := J.toJStage3EntryA.curNotVmA + obtain ⟨xc, hkc, hst, hsp, J1, -, hOS1, -⟩ := J.toJStage3A.of_restartCurA + RS.pl.choose_spec.choose_spec.choose_spec.choose_spec RS.sym RS.run hnvm RS.cur RS.vs RS.vc + have hst1 : L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART) (stA (L.curW s)) = sched.TS_RESTART := + rd_wr_eq (by rw [stA_eq]; exact field_aligned (J.sr.core.tcbs _ J.sr.curT) (by simp) (by simp)) + generalize hs1 : L.wr s (stA (L.curW s)) sched.TS_RESTART = s1 at hst J1 hOS1 hst1 + set t := L.curW s with ht_def + set σ1 := stUpdA σ σ.curThread xc .restart with hσ1 + have kb1 : L.ksb s1 = L.ksb s := by rw [← hs1]; exact L.ksb_wr _ _ _ + have ct : σ.curThread = specPtr t := J.j.1.sch.curThread + have ct1 : σ1.curThread = specPtr t := ct + have htZ : t ∈ Z.T := J.sr.curT + set xs : Tcb := { xc with tcbState := .restart } with hxs + have hks : σ1.kheap (specPtr t) = some (.tcb xs) := by + rw [hσ1, ← ct]; simp [stUpdA, setObjA, hxs] + have ne1 : specPtr r ≠ σ.curThread := fun e => by rw [e, hkc] at hk; cases hk + have hk1 : σ1.kheap (specPtr r) = some (.reply xr) := by + rw [hσ1]; simp only [stUpdA, setObjA, Function.update_of_ne ne1]; exact hk + -- the caller + have hH : H r = some .reply := J.utIpc.reply r hr + have hw : L.mem s1 (Endp.rTcbA r) ≠ 0#u64 ∧ L.mem s1 (Endp.rTcbA r) = d := by + obtain ⟨y, hy, he⟩ := J1.ep.reply r hr + rw [hk1] at hy + obtain rfl := KernelObject.reply.inj (Option.some.inj hy) + obtain ⟨h0, e⟩ := optRef_some (he.symm.trans hcl) + exact ⟨h0, specPtr_inj e⟩ + have J1T := J1.toTA ⟨fun u h => absurd h (J.noSwitch u), ⟨xs, by rw [ct1]; exact hks, Or.inr (Or.inl rfl)⟩⟩ + J.noSwitch + have hd : d ∈ Z.T := hw.2 ▸ (J1T.replyCallerA hH hw.1).1 + obtain ⟨xd, hkdσ, -⟩ := J.ep.tcb d hd + obtain ⟨hstd, hfd⟩ := hrcv xd hkdσ + have htd : t ≠ d := by + intro e + have hky : σ.kheap (specPtr t) = some (.tcb xc) := ct ▸ hkc + have hr' : runnable xc.tcbState = true := (runnable_iffA ((J.ep.at_tcb htZ hky).st)).1 J.running + rw [e, hkdσ] at hky + cases hky + rw [hstd] at hr' + cases hr' + have hdt : specPtr d ≠ specPtr t := fun h => htd (specPtr_inj h).symm + have hkd : σ1.kheap (specPtr d) = some (.tcb xd) := by + rw [hσ1, ct]; simp only [stUpdA, setObjA, Function.update_of_ne hdt]; exact hkdσ + have hq : ∀ e ∈ E, ∀ z, σ1.kheap (specPtr e) = some (.endpoint z) → specPtr d ∉ z.epQueue := by + intro e he z hz hm + obtain ⟨z0, hz0, rel⟩ := J1.ep.ep e he + rw [hz] at hz0 + cases hz0 + have h5 : (L.mem s1 (Fl4ProofsA64.P1.stA d)).val = 5 := by + rw [(J1.ep.at_tcb hd hkd).st, hstd]; rfl + cases z with + | idleEP => simp [Endpoint.epQueue] at hm + | sendEP q => + obtain ⟨-, xs', -, rfl, -, hq⟩ := rel + have := (hq d (mem_map_specPtrSc.1 hm)).1 + omega + | recvEP q => + obtain ⟨-, xs', -, rfl, -, hq⟩ := rel + have := (hq d (mem_map_specPtrSc.1 hm)).1 + omega + -- the removal's locality at one core + have hlocal : ∀ sc ∈ Z.C, (stack sc).head? = some r → xd.tcbSchedContext = none → + ReplyPopLocalQA (st := st) (ar := ar) (Z := Z) (m := m) a s1 σ1 sc d r := by + intro sc hsc hh _ + obtain ⟨rest, hl⟩ : ∃ rest, stack sc = r :: rest := by + cases ee : stack sc with + | nil => rw [ee] at hh; cases hh + | cons c l => + rw [ee] at hh + cases hh + exact ⟨l, rfl⟩ + obtain ⟨⟨y, k, hy, hL⟩, -⟩ := J1.reply.stackRel sc hsc + have cur : ∀ c < fl4A64Platform.numNodes, c = m.curCore := fun c h => + (Nat.lt_one_iff.1 (h1 ▸ h)).trans (Nat.lt_one_iff.1 (h1 ▸ J1T.curCore_lt hCs)).symm + have JL : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7LA st (J7R2A st ar hPl.ipa)) s1 a σ1 m := { J1T with j7 := J1T.j7.toLA } + refine ⟨notIdleSc_of_repliesA J1.sidle cur hy (by rw [hL, hl]; simp), ?_, ?_⟩ + · rw [JL.onCore_single hCs h1 hd, J1.sr.scCore sc hsc] + have a1 := J1.scCpu sc hsc + have a2 := hCs _ J1.j.1.sch.cpuCs + exact UScalar.eq_of_val_eq (by omega) + · by_cases hz : L.mem s1 (scTcbA sc) = 0#u64 + · exact Or.inl hz + have hscH : sc ∈ H.scs := J1.scs.symm ▸ hsc + have hX := JL.blkA.2.2.1.2.2 + have hf : L.mem s1 (scTcbA sc) ∈ Z.T := J1.tcbs ▸ hX.yt.1 sc hscH hz + exact Or.inr ⟨_, hf, hX.tsc sc hscH (J1.scNZ sc hsc) hz, JL.onCore_single hCs h1 hf, + fun s2 hrun => JL.replyStallA hCs h1 hsc hl hf hrun⟩ + have unb : ∀ s2 σ2 Q1 R1, SchedSRA st { Z with Q := Q1, R := R1 } m κ s2 σ2 → RemKeepA σ1 σ2 → + replyRemove (Z := Unit) (specPtr d) (specPtr r) σ1 = ({((), σ2)}, False) → + (L.mem s2 (scA d) ≠ 0#u64 → L.mem s2 (scCoreA (L.mem s2 (scA d))) = L.cpu s2) ∧ + (specPtr d ∈ σ2.releaseQueue → ∀ y n, + σ2.kheap (scNameA m (L.mem s2 (scA d))) = some (.schedContext y n) → + refillReady σ2.curTime (refillHd y) = false) := by + intro s2 σ2 Q1 R1 H2 _ hu + refine ⟨fun h0 => ?_, fun hmem => ?_⟩ + · have hsc : L.mem s2 (scA d) ∈ Z.C := H2.rest.scMem d hd h0 + rw [H2.scCore _ hsc] + have a1 := J1.scCpu _ hsc + have a2 := hCs _ H2.sch.cpuCs + exact UScalar.eq_of_val_eq (by omega) + · obtain ⟨y, hy, hys⟩ := replyRemove_inactiveA hu + exact absurd hmem (H2.notQueued hy (by rw [hys]; rfl)).2 + have hoff := J1.slotOffEwA + have hnq := J1.sr.notQueued hkd (by rw [hstd]; rfl) + -- the tail's locality at the reached state + have tailS : ∀ (s4 : S) (σ4 : State Unit) Q4 R4 (x : Tcb), + SchedSRB st { Z with Q := Q4, R := R4 } m κ s4 σ4 (specPtr d) → + σ4.kheap (specPtr d) = some (.tcb x) → BoundActive (specPtr d) σ4 → + SIKA σ1.idleThread (· = specPtr d) (· = specPtr d) true (faultClrA σ1 (specPtr d) xd) σ4 → + WritesIn st s1 s4 (ReplyFootW (st := st) Z m σ1 s1 E Rp Bf) → + L.mem s4 (affA d) = L.cpu s4 ∧ + (L.mem s4 (scA d) ≠ 0#u64 → L.mem s4 (scCoreA (L.mem s4 (scA d))) = L.cpu s4) ∧ + (∀ sc y n, L.mem s4 (scA d) = sc → sc ≠ 0#u64 → + σ4.kheap (scNameA m sc) = some (.schedContext y n) → scActive y = true) ∧ + faults.valid_timeout_handler st s4 d = ok (isEpCap x.tcbTimeoutHandler) := by + intro s4 σ4 Q4 R4 x H4 hx4 B4 K4 w4 + have a2 := hCs _ H4.sch.cpuCs + refine ⟨?_, fun h0 => ?_, fun sc y k he h0 hy => ?_, ?_⟩ + · have a1 := hCs _ (H4.core.aff d hd) + exact UScalar.eq_of_val_eq (by omega) + · have hsc : L.mem s4 (scA d) ∈ Z.C := H4.rest.scMem d hd h0 + rw [H4.scCore _ hsc] + have a1 := J1.scCpu _ hsc + exact UScalar.eq_of_val_eq (by omega) + · obtain ⟨z, hz, hzs⟩ := H4.sch.tcb d hd + have ez : z = x := KernelObject.tcb.inj (Option.some.inj (hz.symm.trans hx4)) + rw [ez] at hzs + have hb : x.tcbSchedContext = some (scNameA m sc) := by + rw [hzs, he, optScA, if_neg h0] + rfl + obtain ⟨z, c, hz, hzc, y', k', hy', act⟩ := B4 x _ hx4 hb + rw [hx4] at hz + cases hz + rw [hb] at hzc + cases hzc + rw [hy] at hy' + cases hy' + exact act + · have htox : x.tcbTimeoutHandler = xd.tcbTimeoutHandler := by + obtain ⟨z, hz, g⟩ := K4.tcbF (faultClrA_at (σ := σ1) (t := specPtr d) (x := xd)) + rw [hx4] at hz + cases hz + have one : ∀ z : Tcb, tcbCnodeMap z (tcbCnodeIndex 4) = some z.tcbTimeoutHandler := fun z => by + simp [tcbCnodeMap, tcbIdx_eq, tcbStage2Slot, tcbHighVSpaceSlot] + have e : x.tcbTimeoutHandler = xd.tcbTimeoutHandler := + Option.some.inj ((one x).symm.trans ((congrFun g.caps (tcbCnodeIndex 4)).trans (one { xd with tcbFault := none }))) + exact e + have hdth : Z.th d = some .tcb := (J1.sr.thT d).2 hd + have hsl : IsSlotA Z.th (slotIdx d layout.tcb.SLOT_TIMEOUT_HANDLER) := + ⟨d, Or.inr ⟨hdth, 4, by unfold tcbSlotsA; omega, by + rw [slotIdx_addrA] + unfold layout.tcb.SLOT_TIMEOUT_HANDLER + rfl⟩⟩ + have keep := slotWordsKeepA J1.sr.toSchedSRN J1.iplaced.e J1.ipcPlace hoff w4 + (Fl4ProofsA64.P3.Sched.IrqSlotsKeepA.of_replyFoot J1.sr.toSchedSRN J1.iplaced.e J1.ipcPlace w4) _ hsl + have ecap : capA st s4 (slotIdx d layout.tcb.SLOT_TIMEOUT_HANDLER) = + capAtA (L.mem s1) (slotIdx d layout.tcb.SLOT_TIMEOUT_HANDLER) := by + show capAtA (L.mem s4) _ = _ + unfold capAtA + rw [keep.1, keep.2] + have rel : CapRelA (capAtA (L.mem s1) (slotIdx d layout.tcb.SLOT_TIMEOUT_HANDLER)) xd.tcbTimeoutHandler := by + obtain ⟨z, hz, h⟩ := J1.j.1.st.tcb d hdth + rw [hkd] at hz + cases hz + obtain ⟨ac, hac, h⟩ := h 4 (by unfold tcbSlotsA; omega) + have one : tcbCnodeMap xd (tcbCnodeIndex 4) = some xd.tcbTimeoutHandler := by + simp [tcbCnodeMap, tcbIdx_eq, tcbStage2Slot, tcbHighVSpaceSlot] + rw [one] at hac + cases hac + have es : slotIdx d layout.tcb.SLOT_TIMEOUT_HANDLER = slotAddrA (specPtr d) (BitVec.ofNat 64 4) := by + rw [slotIdx_addrA] + unfold layout.tcb.SLOT_TIMEOUT_HANDLER + rfl + rw [es] + exact h + rw [htox] + exact validTimeoutHandler_relA (by rw [ecap]; exact rel) + have hidle' : ∀ c < fl4A64Platform.numNodes, specPtr d ≠ m.idleThreadOf c := fun c hc e => by + have hcc : c = m.curCore := (Nat.lt_one_iff.1 (h1 ▸ hc)).trans (Nat.lt_one_iff.1 (h1 ▸ J1T.curCore_lt hCs)).symm + have ei : m.idleThreadOf c = σ1.idleThread := by + rw [hcc, J1.sr.sch.core, J1.sr.sch.idleOf _ J1.sr.sch.cpuCs, J1.sr.sch.idle] + rfl + obtain ⟨y, hy, hs⟩ := J1.sinv.idleTcb + rw [← ei, ← e, hkd] at hy + cases hy + rw [hstd] at hs + cases hs + -- the caller's selectors at the state after the replier's restart + have hka : Aligned (stA t) := by + rw [stA_eq]; exact field_aligned (J.sr.core.tcbs t htZ) (by simp) (by simp) + have kw : ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → L.mem s1 (wa d K) = L.mem s (wa d K) := fun K hK h8 => by + rw [← hs1] + exact rd_wr_ne hka (field_aligned (J.sr.core.tcbs d hd) hK h8) + (by rw [stA_eq]; exact field_ne (J.sr.core.tcbs t htZ) (J.sr.core.tcbs d hd) (by simp) hK (fun e => absurd e htd)) + have hokS : FaultReplyOKA (L := L) s1 d + (unat (dataToMessageInfo (xs.tcbArch.tcbContext.userRegs msg_info_register)).miLength) f := by + have h := hok xc hkc + have hlen : xs.tcbArch = xc.tcbArch := by rw [hxs] + rw [hlen] + have e2 : L.mem s1 (tFw1A d) = L.mem s (tFw1A d) := kw 3392#u64 (by simp) (by simp) + refine ⟨fun hh => ?_⟩ + rw [e2] + exact h.trap hh + -- [J] the caller's VCPU link (`VcpuCoreA.tcb`) + have hvcS : xd.tcbArch.tcbVcpu ≠ none ↔ L.mem s1 (tVcpuA d) ≠ 0#u64 := by + obtain ⟨z, hz, h, -⟩ := J1.j.2.1.tcb d hd + rw [hkd] at hz + cases hz + have h' : xd.tcbArch.tcbVcpu = optRefA (L.mem s1 (tVcpuA d)) := h + rw [h'] + unfold optRefA + by_cases e : L.mem s1 (tVcpuA d) = 0#u64 <;> simp [e] + have hwords : ∀ s2, WritesIn st s1 s2 (RemWrA (st := st) Z m σ1 s1 Rp r d) → + L.mem s2 (tVcpuA d) = L.mem s1 (tVcpuA d) ∧ L.mem s2 (tFw1A d) = L.mem s1 (tFw1A d) := by + intro s2 w + have hP := J1.iplaced + have key : ∀ K : U64, (K.val = 3392 ∨ K.val = 3664) → L.mem s2 (wa d K) = L.mem s1 (wa d K) := by + intro K hK + have hbv : (wa d K).val = d.val + K.val := field_val (hP.e.tcb d hd) (by omega) + refine w.mem_eq (field_aligned (hP.e.tcb d hd) (by omega) (by omega)) ?_ + rintro (⟨q, hq, c⟩ | c | c | c) + · have h1 := hP.e.rt q hq d hd + have h2 := replyNextPlaceA (Z := Z) hP.e hq + have h3 := replyPrevPlaceA (Z := Z) hP.e hq + rcases c with c | c <;> rw [c] at hbv <;> omega + · have h1 := hP.e.rt r hr d hd + have h2 : (wa d K).val = r.val := by rw [c, Endp.rTcbA_val] + omega + · have h1 : (wa d K).val = d.val + 3328 := by rw [c]; exact Endp.val_st (hP.e.tcb d hd) + omega + · exact J1.tcbWord_offMwrA hd (by omega) (by omega) c + exact ⟨key 3664#u64 (by simp), key 3392#u64 (by simp)⟩ + have keepA := do_reply_transfer_keepFTA (st := st) + (RT := fun s' a' σ' => ReplyTmoOutA st ar AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf κ n hPl.ipa m a s' a' σ' ∧ + Sched.RepEndA σ1 σ') + (ar := ar) (a := a) (grant := grant) (n := n) J1.sr J1.ipcPlace + J1.iplaced.e J1.ep J1.reply hr htZ hd htd hkd hk1 hcl hstd hks hq (J1.scCov.reply _) + (fun sc hsc _ => J1.scNZ sc hsc) hlocal unb hoff J1.j.1.st J1.j.1.cv J1.bufCover J1.bufNative hag hmb + hidle' hfd hokS hvcS hwords + (fun sc hsc y k hy => by + obtain ⟨y', k', hy', hb⟩ := J1.sr.inv.back (specPtr d) xd sc hkd hsc + rw [hy] at hy' + cases hy' + exact hb) + ⟨J1.sinv, J1.sidle, ⟨fun u e => J.sact.f1 u e, ⟨xs, by rw [ct1]; exact hks, Or.inr (Or.inl rfl)⟩⟩⟩ + J1.schedValid J1.activeReplyScs J.noSwitch hnq.1 (by rw [ct1]; exact hdt) tailS + (fun s4 σ4 Q4 R4 stack4 x MD sc y k badge hsc h0 hy _ hep => + J1.replyTmoA hmax htu hconv hCs h1 J.noSwitch (fun z hz => by rw [ct1, hks] at hz; cases hz; rfl) hd + (by rw [ct1]; exact hdt) hidle' hkd hstd (J.toJStage3A.tmoHandlerEpA hd hkdσ) MD (J1.ntfnWait_replyFA hr hd MD.wn) + (J1.replyTmoRtA hr hd hw.2 MD.wn) badge hsc h0 hy hep) + -- P1's invariants after the transfer + obtain ⟨Pt, Ub, Pp7, Tb7, Hr, Te, Kt, Lv7, Re, bits, KLv, W⟩ := J1.j7.unp + have I := J1.delIn hA hPl W + have D := J.decPreA hslot hext + have htH : t ∈ H.tcbs := by rw [J.tcbs]; exact htZ + have kt := spec_at (sched.set_thread_state.restart_blk (Es := ∅) s t (BXI.blk J.sys.ent.bxi) + htH (runnable_ok J.running).1 (runnable_ok J.running).2) hst + have kt1 := kt.2.2.2 + have P := (J.toJStage3A.sysPlace hext hslot hcov).keepO hOS1 kb1 + have p1 := Fl4ProofsA64.P1.W7.R.ST.ipc.do_reply_transfer.rpw (st := st) (ar := ar) (Vc := Z.V) (s := s1) hA a t r grant P + J1.sys.sysl J1.symL kt.1 I.del.sca I.ts (by rw [kb1]; exact J.ko) I.del.vr hH htH + (D.rv.restart kt1 (D.rv.zero htH D.nvm)) + obtain ⟨⟨s2, a2⟩, eb, ⟨⟨σ2, hmem, hor⟩, p1post⟩⟩ := exists_of_specA (WP.spec_and keepA p1) + simp only [WP.uncurry'_pair] at eb hor p1post + have ha2 : a2 = a := by + rcases hor with ⟨_, h, _⟩ | ⟨⟨h, _⟩, _⟩ <;> exact h + subst a2 + have hdec : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don fp buf extra = + ok (.Ok (), s2, a) := by + rw [hcap, decodeReply_programA] + simp only [ipcInvokeProgramA, cur_thread_ok, bind_ok, ← ht_def, hst] + rw [show ipc.do_reply_transfer st ar s1 a t r grant = ok (s2, a) from eb] + simp only [bind_ok] + rfl + have F := J.decodeFinA hA hM hPl hfm hslot hcov (rfl : capA st s slot = capA st s slot) hext hSC (lbl := lbl) + (by rw [hcap]; unfold DecArmS1aEnt DecArmS1aC; simp) + (fun _ h => by rw [hcap] at h; cases h) (fun _ h => by rw [hcap] at h; cases h) hdec σ2 m + obtain ⟨-, -, -, -, -, userOK, j7⟩ := F + rcases hor with ⟨stack', -, ⟨Q, Rl, sr2⟩, wD, -, E2, rs2, sub, K, sinv, sidle, sv, Qs, + U2, wN, ⟨σ4, K4, hk4, E4, run4⟩, hrepK⟩ | + ⟨⟨-, Q, Rl, stack', kT⟩, hrepT⟩ + swap + · exact ⟨xc, σ1, σ2, s2, hkc, rfl, hsp, by rw [ct]; exact hmem, hdec, Sched.WakeEndA.ofRep hkc hrepT, Q, Rl, stack', + kT (BlkOK.bxi p1post.2.2.1) p1post.1 p1post.2.1 userOK j7⟩ + have wI : WritesIn st s1 s2 (fun b => IpcFpA st Z E Rp Bf m σ1 s1 b ∨ RepWA Rp b) := wD.mono fun b h => by + rcases h with h | h | ⟨q, hq, h⟩ + · exact Or.inl (Or.inl h) + · exact Or.inl (Or.inr h) + · have h2 := replyNextPlaceA (Z := Z) J1.iplaced.e hq + have h3 := replyPrevPlaceA (Z := Z) J1.iplaced.e hq + refine Or.inr ⟨q, hq, ?_⟩ + rcases h with h | h <;> rw [h] <;> omega + have wS : WritesIn st s1 s2 (fun b => SchedStepNWA (st := st) N E Rp Z m σ1 s1 b ∨ BufW Bf b) := + wD.mono fun b h => replyFootW_wA J1.iplaced.e h + have wk := J1.ntfnWait_replyFA hr hd wN + have after : ∀ u ∈ Z.T, specPtr u = specPtr d → ∀ x, σ4.kheap (specPtr u) = some (.tcb x) → + epBlocked x.tcbState = none ∧ ∀ p, x.tcbState ≠ .blockedOnNotification p := by + intro u _ hu x hx + rcases run4 x (hu ▸ hx) with h | h <;> rw [h] <;> exact ⟨rfl, fun _ h => by cases h⟩ + have hMv : ∀ u ∈ Z.T, specPtr u = specPtr d → L.mem s1 (stA u) ≠ sched.TS_RUNNING_VM := by + intro u _ hu h + rw [specPtr_inj hu] at h + have h5 : (L.mem s1 (stA d)).val = 5 := by rw [(J1.ep.at_tcb hd hkd).st, hstd]; rfl + rw [h] at h5 + revert h5 + unfold sched.TS_RUNNING_VM + simp + have K4X := K4.clrX hkd + have ntfn4 := J1.ntfn_ipcMRXA wI K4X E4 wk (fun u _ hu x hx p h => by + rw [show specPtr u = specPtr d from hu, hkd] at hx + cases hx + rw [hstd] at h + cases h) (fun u hu hm x hx => (after u hu hm x hx).2) + have ntfn : NtfnStateRel N Z.T (L.mem s2) σ2 := + ⟨fun q hq => by rw [hk4]; exact ntfn4.ntfn q hq, fun u hu => by rw [hk4]; exact ntfn4.tcb u hu⟩ + have blk4 := J1.blkQ_ipcMXA K4X E4 wk after + have blkQ : BlkQueuedA Z.T E N (L.mem s2) σ2 := + ⟨fun u hu x hx => by rw [hk4] at hx ⊢; exact blk4.ep u hu x hx, + fun u hu x hx => by rw [hk4] at hx ⊢; exact blk4.ntfn u hu x hx⟩ + have J2 := J1.of_ipcStepUXA (Q := Q) (Rl := Rl) (curOf := Z.curOf) (curScOf := Z.curScOf) wS U2.1 U2.2.1 Qs + (J1.specZero_of_recvBufA hd hkd (fun x _ => U2.2.2 x (fun h => h))) (fun _ _ => rfl) (BlkOK.bxi p1post.2.2.1) + p1post.1 p1post.2.1 userOK j7 sr2 E2 ntfn rs2 + (fun q hq dead c hc hm => J1.deadExt.reply q hq dead c hc (sub c hc q hm)) (IpcKeepNA.ofClr hkd K) + (IdleNoFaultA.ofClr hkd K J1.idleNoFault (fun c hc e => hidle' c hc e.symm)) (J1.tw_ipcA wI) sinv sidle + (J1.runVcpu_ipcRXA wI K4X E4 hMv) blkQ sv + exact ⟨xc, σ1, σ2, s2, hkc, rfl, hsp, by rw [ct]; exact hmem, hdec, Sched.WakeEndA.ofRep hkc hrepK, Q, Rl, + stack', J2⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3ReplyFaultClrA.lean b/proofs/Fl4ProofsA64/P3/Stage3ReplyFaultClrA.lean new file mode 100644 index 000000000..37b2eebb0 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ReplyFaultClrA.lean @@ -0,0 +1,134 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2b): **clearing the fault of an `Inactive` caller**, the program side of the +fault Reply's `clear_fault` (one word, `faultA t`) against the spec's `threadSet (tcbFault := none)`. + +* `SchedSRA.clearFaultA`: `SchedSRB … t` (and `StEqA`) at the state with the fault cleared (`faultClrA`). +* `EpStateRel.clearFaultA`: the endpoint relation moves to the cleared TCB. +* `fault_stateRunA`: the state write of the fault Reply (`Restart` or `Inactive`) from the cleared TCB. +* `faultTBneA`: the fault type word against the spec fault (the SC tail's `Timeout` test). + +The TCB is `Inactive`, so `FaultRel'` only asks for the null type word. No premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3ReplyFaultKeepSpecA +import Fl4ProofsA64.P3.Stage3TimeoutSendBA +import Fl4ProofsA64.P3.TcbInactiveExA +import Fl4ProofsA64.P3.ReplyRunExA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **`SchedSRB … t` across the clear of `t`'s fault** (module docstring). -/ +theorem SchedSRA.clearFaultA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRA st Z m κ s σ) {t : U64} (ht : t ∈ Z.T) {x : Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) : + SchedSRB st Z m κ (L.wr s (faultA t) 0#u64) (faultClrA σ (specPtr t) x) (specPtr t) := by + have HB := H.toB (specPtr t) + obtain ⟨hS, hrun, hrd⟩ := faultStepExA hk none HB.run HB.rdEx + exact HB.stableExBA (writesIn_wr (a_fault (H.core.tcbs t ht)).1) + (fun _ hb => faultOffReadA H.toSchedSRN ht (Or.inl hb)) hS hrun hrd + +/-- **`EpStateRel` across the clear of `t`'s fault** (module docstring). -/ +theorem _root_.Fl4ProofsA64.P3.Endp.EpStateRel.clearFaultA {T E R : Set U64} {Bf : Set (U64 × Word)} {s : S} {σ : State Unit} + (hR : EpStateRel T E R Bf (L.mem s) σ) (hP : EPlaced T E R Bf) {t : U64} (ht : t ∈ T) {x : Tcb} + (hk : σ.kheap (specPtr t) = some (.tcb x)) : + EpStateRel T E R Bf (L.mem (L.wr s (faultA t) 0#u64)) (faultClrA σ (specPtr t) x) := by + have hta := hP.tcb t ht + have w : WritesIn st s (L.wr s (faultA t) 0#u64) (· = faultA t) := writesIn_wr (a_fault hta).1 + have keep : ∀ {K : U64}, K.val < 4096 → K.val % 8 = 0 → K.val ≠ 3376 → + L.mem (L.wr s (faultA t) 0#u64) (wa t K) = L.mem s (wa t K) := fun {K} h1 h2 h3 => + w.mem_eq (field_aligned hta h1 h2) (fun h => field_ne hta hta h1 (K2 := 3376#u64) (by simp) + (fun _ e => h3 (by simpa using e)) h) + have hv : ∀ K : U64, K.val < 4096 → (wa t K).val = t.val + K.val := fun K hK => field_val hta hK + have v0 : L.mem (L.wr s (faultA t) 0#u64) (tFaultA t) = 0#u64 := L.mem_wr_same (a_fault hta).1 + have hxt := hR.at_tcb ht hk + have hx : TcbRelE R (L.mem (L.wr s (faultA t) 0#u64)) t { x with tcbFault := none } := by + refine ⟨?_, fun e h => ?_, fun e d h => ?_, fun e r d h => ?_, fun r h => ?_, fun r => ?_, ?_, ?_, ?_, ?_, ?_⟩ + · rw [stA_eq, keep (K := 3328#u64) (by simp) (by simp) (by simp), ← stA_eq]; exact hxt.st + · rw [tBlockA, keep (K := 3336#u64) (by simp) (by simp) (by simp)]; exact hxt.blk e h + · rw [tBadgeA, tFlagsA, keep (K := 3344#u64) (by simp) (by simp) (by simp), + keep (K := 3352#u64) (by simp) (by simp) (by simp)] + exact hxt.send e d h + · rw [tReplyA, keep (K := 3360#u64) (by simp) (by simp) (by simp)]; exact hxt.recv e r d h + · rw [tReplyA, keep (K := 3360#u64) (by simp) (by simp) (by simp)]; exact hxt.reply r h + · rw [w.mem_eq (a_reg hta r).1 fun hb => ?_] + · exact hxt.regs r + · have h1 := regA_val hta (regIdxA_lt r) + have h2 := regIdxA_lt r + have h3 : (faultA t).val = t.val + 3376 := hv 3376#u64 (by simp) + rw [hb, h3] at h1 + omega + · rw [prioA, keep (K := 3448#u64) (by simp) (by simp) (by simp)]; exact hxt.prio + · rw [v0]; exact ⟨fun _ => rfl, fun _ => rfl⟩ + · rw [v0]; exact ⟨fun h => absurd h (by decide), fun ⟨b, h⟩ => by cases h⟩ + · rw [tBoundA, keep (K := 3368#u64) (by simp) (by simp) (by simp)]; exact hxt.bound + · exact v0 + refine hR.upd_tcb hP ht _ (fun b hb hn => w.mem_eq hb fun h => hn.2 (h ▸ (a_fault hta).2)) (Or.inr ⟨?_, ?_⟩) hx + · rw [stA_eq]; exact keep (K := 3328#u64) (by simp) (by simp) (by simp) + · rw [tBlockA]; exact keep (K := 3336#u64) (by simp) (by simp) (by simp) + +/-- The ready queues, release queue and machine state across `setThreadState`. -/ +theorem setThreadState_keepQA {σ σ' : State Unit} {t : ObjRef} {x : Tcb} {ts : ThreadState} + (hk : σ.kheap t = some (.tcb x)) (he : setThreadState (Z := Unit) t ts σ = ({((), σ')}, False)) : + σ'.readyQueues = σ.readyQueues ∧ σ'.releaseQueue = σ.releaseQueue ∧ + σ'.kheap = (updKSc σ t (.tcb { x with tcbState := ts })).kheap ∧ σ'.machineState = σ.machineState := by + have e4 : σ' = stsActA (updKSc σ t (.tcb { x with tcbState := ts })) t := + det_outA he (setThreadState_stsActExA hk ts) + rw [e4] + unfold stsActA + split <;> exact ⟨rfl, rfl, rfl, rfl⟩ + +/-- **The state write of the fault Reply**: `Restart` or `Inactive` for the cleared, `Inactive` TCB (module docstring). -/ +theorem fault_stateRunA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + {t : U64} (HB : SchedSRB st Z m κ s σ (specPtr t)) (hst : StEqA Z.T (L.mem s) σ) (ht : t ∈ Z.T) + {x : Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) (hx : x.tcbState = .inactive) + (hnq : (∀ d p, specPtr t ∉ σ.readyQueues d p) ∧ specPtr t ∉ σ.releaseQueue) (restart : Bool) : + sched.set_thread_state st s t (if restart then sched.TS_RESTART else sched.TS_INACTIVE) ⦃ s' => ∃ σ', + setThreadState (Z := Unit) (specPtr t) (if restart then .restart else .inactive) σ = ({((), σ')}, False) ∧ + SchedSRB st Z m κ s' σ' (specPtr t) ∧ StEqA Z.T (L.mem s') σ' ∧ + σ'.kheap = Function.update σ.kheap (specPtr t) + (some (.tcb { x with tcbState := if restart then .restart else .inactive })) ∧ + σ'.machineState = σ.machineState ∧ σ'.readyQueues = σ.readyQueues ∧ σ'.releaseQueue = σ.releaseQueue ∧ + WritesIn st s s' (fun b => b = stA t ∨ b = L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) ⦄ := by + have hcls : ¬ inClassRSc x.tcbState x.tcbFault := by rw [hx]; simp [inClassRSc, runnable] + cases restart + · simp only [Bool.false_eq_true, ↓reduceIte] + refine WP.spec_mono (set_thread_state_inactiveExA HB hst ht hk hnq) ?_ + rintro s' ⟨σ', he, hk', HA', hst', ws⟩ + obtain ⟨q1, q2, -, q3⟩ := setThreadState_keepQA hk he + exact ⟨σ', he, HA'.toB _, hst', hk'.trans (updKSc_kheap ..), q3, q1, q2, ws⟩ + · simp only [↓reduceIte] + refine WP.spec_mono (set_thread_state_exAnyA (HB.toA_ncA hk hcls) hst ht hk (ts := .restart) rfl + (v := sched.TS_RESTART) (by unfold sched.TS_RESTART; rfl)) ?_ + rintro s' ⟨σ', he, hk', HB', hst', ws⟩ + obtain ⟨q1, q2, -, q3⟩ := setThreadState_keepQA hk he + exact ⟨σ', he, HB', hst', hk'.trans (updKSc_kheap ..), q3, q1, q2, ws⟩ + +end + +/-- **The fault type word against the spec fault**: the SC tail's `Timeout` test (module docstring). -/ +theorem faultTBneA {R : Set U64} {m : U64 → U64} {t : U64} {y : Tcb} (hrel : TcbRelE R m t y) {f : Fault} + (hf : y.tcbFault = some f) : + (m (tFaultA t) != fl4_abi.consts.fault.Timeout) = !isTimeoutFault f := by + have key := hrel.faultT + rw [hf] at key + have T : fl4_abi.consts.fault.Timeout = 5#u64 := by unfold fl4_abi.consts.fault.Timeout; rfl + rw [T] + by_cases h : m (tFaultA t) = 5#u64 + · obtain ⟨b, hb⟩ := key.1 h + have e := Option.some.inj hb + subst e + simp [h, isTimeoutFault] + · cases f with + | timeout b => exact absurd (key.2 ⟨b, rfl⟩) h + | _ => simp [isTimeoutFault, h] <;> exact h + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3ReplyFaultGlueA.lean b/proofs/Fl4ProofsA64/P3/Stage3ReplyFaultGlueA.lean new file mode 100644 index 000000000..89db7675d --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ReplyFaultGlueA.lean @@ -0,0 +1,237 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2b): **the record glue across the fault clear of a Reply**. + +`of_ipcStepUA` asks `IpcKeepA σ σ'` (every fault kept), but the fault Reply clears the caller's fault. The +kernel-entry variant `JStage3A.of_ipcStepUXA` (`P3/Stage3KEntrySendKeepA`) takes `IpcKeepNA` (no fault clause) and the +idle threads' faults (`IdleNoFaultA`) instead, with the `SIKXA` form of the step. This module takes the fault Reply's +keeps (`IpcKeepA (faultClrA σ t x) σ'`, `SIKA … (faultClrA σ t x) σ'`) to them: + +* `SIKA.clrX`, `IpcKeepNA.ofClr`, `IdleNoFaultA.ofClr`. +* `ntfn_ipcMRXA`, `blkQ_ipcMXA`, `runVcpu_ipcRXA`: `ntfn_ipcMA` / `blkQ_ipcMA` / `runVcpu_ipcA` over `SIKXA` (the + reply words stay in the write set; `ntfn_ipcXMA` has none). +* `JStage3A.ntfnWait_replyFA`: `NtfnWaitKeepMA` from the fault Reply's narrow write set (`ReplyNwFA`). + +No premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3SendBlkArmA +import Fl4ProofsA64.P3.ReplyHookStallA +import Fl4ProofsA64.P3.ReplyCallerA +import Fl4ProofsA64.P3.ReplyNotIdleA +import Fl4ProofsA64.P3.Stage3ReplyDetachedA +import Fl4ProofsA64.P3.Stage3ReplyLiveKeepFA +import Fl4ProofsA64.P3.Stage3KEntrySendKeepA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3 Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +theorem SIKA.clrX {i : ObjRef} {M N : ObjRef → Prop} {don : Bool} {σ σ' : State Unit} {t : ObjRef} {x : Tcb} + (R : SIKA i M N don (faultClrA σ t x) σ') (hk : σ.kheap t = some (.tcb x)) : SIKXA i M N don σ σ' := by + have hc : σ'.consumedTime = σ.consumedTime := by rw [R.frame]; rfl + refine ⟨?_, fun p => ?_, R.rq, R.rls, R.act, fun g => R.rdy (RdyA.updNs g t _ (fun _ _ e => by cases e))⟩ + · rw [hc]; exact R.frame + · by_cases h : p = t + · subst h + have o := R.obj p + rw [faultClrA_at] at o + right + generalize σ'.kheap p = oy at o ⊢ + cases o with + | same => exact ⟨x, _, hk, rfl, ⟨rfl, rfl, rfl, rfl, rfl, Or.inl rfl, Or.inl rfl, rfl, rfl⟩⟩ + | tcb _ y g => exact ⟨x, y, hk, rfl, ⟨g.frame, g.buf, g.mcp, g.yt, g.caps, g.st, g.sc, g.vcpu, g.flags⟩⟩ + · have o := R.obj p + rw [faultClrA_ne h] at o + exact Or.inl o + +theorem IpcKeepNA.ofClr {σ σ' : State Unit} {t : ObjRef} {x : Tcb} (hk : σ.kheap t = some (.tcb x)) + (K : IpcKeepA (faultClrA σ t x) σ') : IpcKeepNA σ σ' := by + refine ⟨fun p y h => ?_, fun p y k h => ?_, fun p y k h => ?_⟩ + · have e : ∃ y', (faultClrA σ t x).kheap p = some (.tcb y') ∧ y'.tcbIpcframe = y.tcbIpcframe ∧ + y'.tcbIpcBuffer = y.tcbIpcBuffer ∧ y'.tcbMcpriority = y.tcbMcpriority ∧ y'.tcbYieldTo = y.tcbYieldTo ∧ + y'.tcbFlags = y.tcbFlags := by + by_cases hp : p = t + · subst hp + rw [hk] at h + cases h + exact ⟨_, faultClrA_at, rfl, rfl, rfl, rfl, rfl⟩ + · exact ⟨y, by rw [faultClrA_ne hp]; exact h, rfl, rfl, rfl, rfl, rfl⟩ + obtain ⟨y', e', f1, f2, f3, f4, f5⟩ := e + obtain ⟨z, hz, -, a1, a2, a3, a4, a5⟩ := K.tcb p y' e' + exact ⟨z, hz, trivial, a1.trans f1, a2.trans f2, a3.trans f3, a4.trans f4, a5.trans f5⟩ + · obtain ⟨y', k', hy, e⟩ := K.scF p y k ((faultClrA_sc hk).2 h) + exact ⟨y', k', hy, e⟩ + · obtain ⟨y0, k0, hy, e⟩ := K.scB p y k h + exact ⟨y0, k0, (faultClrA_sc hk).1 hy, e⟩ + +theorem IdleNoFaultA.ofClr {m : McState} {σ σ' : State Unit} {t : ObjRef} {x : Tcb} + (hk : σ.kheap t = some (.tcb x)) (K : IpcKeepA (faultClrA σ t x) σ') (hI : IdleNoFaultA m σ) + (hne : ∀ c < fl4A64Platform.numNodes, m.idleThreadOf c ≠ t) : IdleNoFaultA m σ' := fun c hc => by + obtain ⟨y, hy, hf⟩ := hI c hc + obtain ⟨z, hz, e, -⟩ := K.tcb _ y (by rw [faultClrA_ne (hne c hc)]; exact hy) + exact ⟨z, hz, e.trans hf⟩ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {A : Type} {AR' : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} + {κ : U64 → U64} {n : Nat} {J5 : JSlotA S} {R : Set U64 → JSlotA S} {s s' : S} {a : A} + {σ σ' : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} {i : Fl4SpecA64.ObjRef} + {M Nq : Fl4SpecA64.ObjRef → Prop} {don : Bool} + +variable (J : JStage3A st AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7A st R) s a σ m) +include J + +/-- **`RunVcpuInv`** across the send: the movers run no VCPU. -/ +theorem _root_.Fl4ProofsA64.P3.JStage3A.runVcpu_ipcRXA (w : WritesIn st s s' (fun b => IpcFpA st Z E Rp Bf m σ s b ∨ RepWA Rp b)) (K : SIKXA i M Nq don σ σ') + (E' : EpStateRel Z.T E Rp Bf (L.mem s') σ') + (hMv : ∀ t ∈ Z.T, M (specPtr t) → L.mem s (Fl4ProofsA64.P1.stA t) ≠ sched.TS_RUNNING_VM) : + RunVcpuInv st Z.T s' := fun t ht h0 => by + have e : L.mem s' (runVcpuA t) = L.mem s (runVcpuA t) := + J.tcbKept_ipcA w (K := 3672#u64) ht (by simp) (by simp) (by unfold TWOffN; simp) (by simp) + rw [e] at h0 + by_cases hM : M (specPtr t) + · exact absurd (J.runVcpu.zero ht (hMv t ht hM)) h0 + · rw [J.stKept_ipcXA K E' ht hM]; exact J.runVcpu t ht h0 + +/-- **`NtfnStateRel` at the new state** (module docstring): `hMs` the movers wait on no notification before, `hMs'` +nor after. -/ +theorem _root_.Fl4ProofsA64.P3.JStage3A.ntfn_ipcMRXA (w : WritesIn st s s' (fun b => IpcFpA st Z E Rp Bf m σ s b ∨ RepWA Rp b)) (K : SIKXA i M Nq don σ σ') + (E' : EpStateRel Z.T E Rp Bf (L.mem s') σ') (wk : NtfnWaitKeepMA M Z.T N (L.mem s) (L.mem s')) + (hMs : ∀ t ∈ Z.T, M (specPtr t) → ∀ x, σ.kheap (specPtr t) = some (.tcb x) → + ∀ p, x.tcbState ≠ .blockedOnNotification p) + (hMs' : ∀ t ∈ Z.T, M (specPtr t) → ∀ x, σ'.kheap (specPtr t) = some (.tcb x) → + ∀ p, x.tcbState ≠ .blockedOnNotification p) : + NtfnStateRel N Z.T (L.mem s') σ' := by + have hP := J.iplaced + have nw : ∀ q ∈ N, ∀ O : U64, O.val < 40 → O.val % 8 = 0 → L.mem s' (wa q O) = L.mem s (wa q O) := by + intro q hq O h1 h2 + have hna := hP.n.ntfn q hq + have hv : (wa q O).val = q.val + O.val := Notif.nfield_val hna (by omega) + refine w.mem_eq (Notif.nfield_aligned hna (by omega) h2) ?_ + have hw : IpcObjWA N E Rp (wa q O) := Or.inl ⟨q, hq, by omega, by omega⟩ + rintro ((c | c) | ⟨r, hr, c⟩) + · exact nw_not_efoot hP hq (by omega) (by omega) c.foot + · exact J.ipc_offMwrA hw c + · have hqH : H q = some .ntfn := J.utIpc.ntfn q hq + have hrH : H r = some .reply := J.utIpc.reply r hr + have := J.sys.ent.heap.disj q _ r _ hqH hrH (fun e => by rw [e, hrH] at hqH; cases hqH) + have hv : (wa q O).val = q.val + O.val := Notif.nfield_val hna (by omega) + simp only [HKind.bits] at this + omega + refine ⟨fun q hq => ?_, fun t ht => ?_⟩ + · obtain ⟨x, hx, hr⟩ := J.ntfn.ntfn q hq + refine ⟨x, K.ntfnF hx, hr.frame ?_ fun y _ hm => ?_⟩ + · refine ⟨?_, ?_, ?_, ?_, ?_⟩ + · rw [Notif.epHeadA_eq]; exact nw q hq _ (by simp) (by simp) + · rw [Notif.epTSA_eq]; exact nw q hq _ (by simp) (by simp) + · rw [Notif.nBadgeA_eq]; exact nw q hq _ (by simp) (by simp) + · rw [Notif.nBoundA_eq]; exact nw q hq _ (by simp) (by simp) + · rw [Notif.nScA_eq]; exact nw q hq _ (by simp) (by simp) + · obtain ⟨hyT, hy6, hyb⟩ := hr.obj.blocked hm + have hM : ¬ M (specPtr y) := fun c => by + obtain ⟨z, hz, rz⟩ := J.ntfn.tcb y hyT + have h6 := rz.st + rw [hy6] at h6 + cases hs : z.tcbState with + | blockedOnNotification p => exact hMs y hyT c z hz p hs + | ext e => rw [hs] at h6; cases e <;> simp [tsCode] at h6 + | _ => rw [hs] at h6; simp [tsCode] at h6 + obtain ⟨k1, k2, k3⟩ := wk y hyT hM (by rw [hyb]; exact hq) + exact ⟨J.stKept_ipcXA K E' hyT hM, k1, k2, k3⟩ + · obtain ⟨x, hx, r⟩ := J.ntfn.tcb t ht + obtain ⟨y, hy, r'⟩ := E'.tcb t ht + refine ⟨y, hy, ⟨r'.st, fun p hp => ?_, ?_, r'.prio⟩⟩ + · have hM : ¬ M (specPtr t) := fun c => hMs' t ht c y hy p hp + obtain ⟨y', hy', g⟩ := K.tcbF hx + rw [hy] at hy' + cases hy' + have hs : x.tcbState = .blockedOnNotification p := by + rw [← g.st.resolve_right fun c => hM c.1]; exact hp + have hb := r.blk p hs + have hN : L.mem s (Notif.tBlockA t) ∈ N := (J.blkQ.ntfn t ht x hx p hs).1 + rw [(wk t ht hM hN).1]; exact hb + · rw [badgeRegA_eq_regA] + exact r'.regs _ + +/-- **`BlkQueuedA` at the new state**: the movers end blocked on neither an endpoint nor a notification. -/ +theorem _root_.Fl4ProofsA64.P3.JStage3A.blkQ_ipcMXA (K : SIKXA i M Nq don σ σ') (E' : EpStateRel Z.T E Rp Bf (L.mem s') σ') + (wk : NtfnWaitKeepMA M Z.T N (L.mem s) (L.mem s')) + (hMs' : ∀ t ∈ Z.T, M (specPtr t) → ∀ x, σ'.kheap (specPtr t) = some (.tcb x) → + Fl4SpecA64.epBlocked x.tcbState = none ∧ ∀ p, x.tcbState ≠ .blockedOnNotification p) : + BlkQueuedA Z.T E N (L.mem s') σ' := by + have kept : ∀ t ∈ Z.T, ∀ y, σ'.kheap (specPtr t) = some (.tcb y) → ¬ M (specPtr t) → + ∃ x, σ.kheap (specPtr t) = some (.tcb x) ∧ x.tcbState = y.tcbState := fun t _ y hy hM => by + obtain ⟨x, hx, g⟩ := K.tcbB hy + exact ⟨x, hx, (g.st.resolve_right fun c => hM c.1).symm⟩ + refine ⟨fun t ht y hy e hb => ?_, fun t ht y hy p hp => ?_⟩ + · have hM : ¬ M (specPtr t) := fun c => by rw [(hMs' t ht c y hy).1] at hb; cases hb + obtain ⟨x, hx, es⟩ := kept t ht y hy hM + obtain ⟨hE, z, hz, hm⟩ := J.blkQ.ep t ht x hx e (es ▸ hb) + obtain ⟨z', hz', hq⟩ := K.epF hz + obtain ⟨x1, hx1, r⟩ := J.ep.tcb t ht + obtain ⟨y1, hy1, r'⟩ := E'.tcb t ht + rw [hx] at hx1; cases hx1 + rw [hy] at hy1; cases hy1 + have eq : L.mem s' (Notif.tBlockA t) = L.mem s (Notif.tBlockA t) := + specPtr_inj ((r'.blk e hb).trans (r.blk e (es ▸ hb)).symm) + exact ⟨by rw [eq]; exact hE, z', hz', hq _ hm hM⟩ + · have hM : ¬ M (specPtr t) := fun c => (hMs' t ht c y hy).2 p hp + obtain ⟨x, hx, es⟩ := kept t ht y hy hM + obtain ⟨hN, z, q, hz, ho, hm⟩ := J.blkQ.ntfn t ht x hx p (es.trans hp) + exact ⟨by rw [(wk t ht hM hN).1]; exact hN, z, q, K.ntfnF hz, ho, hm⟩ + +/-- **`NtfnWaitKeepMA`** from the fault Reply's narrow write set (the caller's block word and `Ep` links are not +written for another thread; module docstring). -/ +theorem _root_.Fl4ProofsA64.P3.JStage3A.ntfnWait_replyFA {r t : U64} (hr : r ∈ Rp) (ht : t ∈ Z.T) + (w : WritesIn st s s' (ReplyNwFA (st := st) Z m σ s Rp r t Bf)) : + NtfnWaitKeepMA (fun o => o = specPtr t) Z.T N (L.mem s) (L.mem s') := by + intro y hy hM hN + have hP := J.iplaced + have hya := hP.e.tcb y hy + have hfy := hya.fits + have hyt : y ≠ t := fun c => hM (by rw [c]) + have apart : t.val + 4096 ≤ y.val ∨ y.val + 4096 ≤ t.val := by + have := J.sr.place.apart t y .tcb .tcb ((J.sr.thT t).2 ht) ((J.sr.thT y).2 hy) hyt.symm + have hsz : ObjKindA.size .tcb = 4096 := rfl + omega + have key : ∀ (K : U64) (b : U64), (K.val = 3336 ∨ K.val = 3496 ∨ K.val = 3504) → b = wa y K → Aligned b → + L.mem s' b = L.mem s b := by + intro K b hK hb hal + have hbv : b.val = y.val + K.val := by rw [hb, wa_valm]; exact Nat.mod_eq_of_lt (by omega) + refine w.mem_eq hal ?_ + rintro (((c | c | c | c) | c | c) | c) + · obtain ⟨q, hq, c⟩ := c + have h1 := hP.e.rt q hq y hy + have h2 := replyNextPlaceA (Z := Z) hP.e hq + have h3 := replyPrevPlaceA (Z := Z) hP.e hq + rcases c with c | c <;> rw [c] at hbv <;> omega + · have h1 := hP.e.rt r hr y hy + have h2 : b.val = r.val := by rw [c, Endp.rTcbA_val] + omega + · have h1 : b.val = t.val + 3328 := by rw [c]; exact Endp.val_st (hP.e.tcb t ht) + omega + · rw [hb] at c + exact J.tcbWord_offMwrA hy (by omega) (by omega) c + · obtain ⟨i, c⟩ := c + have h1 := regIdxA_lt i + rw [Endp.regA_val (hP.e.tcb t ht) h1] at c + omega + · have := hP.e.bt _ c y hy; omega + · have h1 : b.val = t.val + 3376 := by + rw [c]; exact field_val (hP.e.tcb t ht) (K := 3376#u64) (by simp) + omega + refine ⟨?_, ?_, ?_⟩ + · exact key 3336#u64 _ (by simp) (by unfold Notif.tBlockA; rfl) (Endp.a_blk hya).1 + · exact key 3504#u64 _ (by simp) (by rw [prevEp_eq]) (Endp.a_prev hya).1 + · exact key 3496#u64 _ (by simp) (by rw [nextEp_eq]) (Endp.a_next hya).1 + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3ReplyFaultKeepSpecA.lean b/proofs/Fl4ProofsA64/P3/Stage3ReplyFaultKeepSpecA.lean new file mode 100644 index 000000000..0a19f3ab0 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ReplyFaultKeepSpecA.lean @@ -0,0 +1,475 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2b): **the spec keeps of a Reply to a faulted caller**, the sibling of +`replyLive_specKeepA` (spec only). + +The fault tail of l4v's `do_reply_transfer` writes the caller's registers (`handle_fault_reply`), clears its fault, +sets it `Restart` or `Inactive` and runs the SC tail. Clearing the fault is not a keep (`SIKA`, `IpcKeepA` keep every +TCB's fault), so the keeps are stated from the pre-state with the caller's fault cleared (`faultClrA`): `SIKA.clrA` +carries the steps before the clear over to that state. + +* `faultClrA`, `SIKA.clrA`, `SInvXA.ofFaultClr`, `SIdleA.ofFaultClr`, `SIKA.sinvQA`. +* **`replyFault_specKeepA`**: `IpcKeepA (faultClrA σ t x) σ5`, `SInvXA`, `SIdleA`, `SchedValidA`, `QSendA`, `RepSubA`, + the user-memory frame (no user byte written), and the tail's `kheap` / memory. + +Case selector (no named premise): the caller's timeout handler is no endpoint cap. Facts the arm takes from its +scheduler relation: `hD3` (ready queues match the domains and priorities), `hidle4` (an `Inactive` caller is in no +ready queue and is not the switch target). New module. +-/ +import Fl4ProofsA64.P3.Stage3ReplyKeepSpecA +import Fl4ProofsA64.P3.EpFaultReplyA + +namespace Fl4ProofsA64.P3.Sched +open Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4SpecA64 + +noncomputable section + +/-- The state with the fault of the TCB `x` at `t` cleared. -/ +def faultClrA (σ : State Unit) (t : ObjRef) (x : Tcb) : State Unit := + { σ with kheap := Function.update σ.kheap t (some (.tcb { x with tcbFault := none })) } + +section Clr +variable {σ : State Unit} {t : ObjRef} {x : Tcb} + +theorem faultClrA_at : (faultClrA σ t x).kheap t = some (.tcb { x with tcbFault := none }) := + Function.update_self .. + +theorem faultClrA_ne {p : ObjRef} (h : p ≠ t) : (faultClrA σ t x).kheap p = σ.kheap p := + Function.update_of_ne h .. + +theorem faultClrA_tcb (hk : σ.kheap t = some (.tcb x)) {p : ObjRef} {y : Tcb} (e : σ.kheap p = some (.tcb y)) : + ∃ y', (faultClrA σ t x).kheap p = some (.tcb y') ∧ y'.tcbState = y.tcbState ∧ + y'.tcbSchedContext = y.tcbSchedContext ∧ y'.tcbArch = y.tcbArch ∧ y'.tcbVtable = y.tcbVtable := by + by_cases h : p = t + · subst h + rw [hk] at e + cases e + exact ⟨_, faultClrA_at, rfl, rfl, rfl, rfl⟩ + · exact ⟨y, by rw [faultClrA_ne h]; exact e, rfl, rfl, rfl, rfl⟩ + +theorem faultClrA_tcbB (hk : σ.kheap t = some (.tcb x)) {p : ObjRef} {y' : Tcb} + (e : (faultClrA σ t x).kheap p = some (.tcb y')) : + ∃ y, σ.kheap p = some (.tcb y) ∧ y'.tcbState = y.tcbState := by + by_cases h : p = t + · subst h + rw [faultClrA_at] at e + cases e + exact ⟨x, hk, rfl⟩ + · exact ⟨y', by rwa [faultClrA_ne h] at e, rfl⟩ + +theorem faultClrA_sc (hk : σ.kheap t = some (.tcb x)) {p : ObjRef} {y : SchedContext} {k : Nat} : + (faultClrA σ t x).kheap p = some (.schedContext y k) ↔ σ.kheap p = some (.schedContext y k) := by + by_cases h : p = t + · subst h + rw [faultClrA_at, hk] + simp + · rw [faultClrA_ne h] + +theorem SInvXA.ofFaultClr {m : McState} {n : Nat} (h : SInvXA σ m n) (hk : σ.kheap t = some (.tcb x)) : + SInvXA (faultClrA σ t x) m n := by + have tcbK := fun p y (e : σ.kheap p = some (.tcb y)) => faultClrA_tcb hk e + refine + { mc := h.mc + view := h.view + curTcb := by + obtain ⟨y, e⟩ := h.curTcb + obtain ⟨y', e', -⟩ := tcbK _ y e + exact ⟨y', e'⟩ + idleTcb := by + obtain ⟨y, e, e2⟩ := h.idleTcb + obtain ⟨y', e', f, -⟩ := tcbK _ y e + exact ⟨y', e', f.trans e2⟩ + onlyIdle := fun p y e e2 => by + obtain ⟨z, ez, f⟩ := faultClrA_tcbB hk e + exact h.onlyIdle p z ez (f.symm.trans e2) + idleSc := by + obtain ⟨y, z, k, e1, e2, e3, e4⟩ := h.idleSc + obtain ⟨y', e1', -, f, -⟩ := tcbK _ y e1 + exact ⟨y', z, k, e1', f.trans e2, (faultClrA_sc hk).2 e3, e4⟩ + curSc := by + obtain ⟨y, k, e⟩ := h.curSc + exact ⟨y, k, (faultClrA_sc hk).2 e⟩ + ctQ := h.ctQ + idleQ := h.idleQ + rq := fun d p y hy => by + obtain ⟨z, e, e2⟩ := h.rq d p y hy + obtain ⟨z', e', f, -⟩ := tcbK _ z e + exact ⟨z', e', by rw [f]; exact e2⟩ + rls := fun y hy => by + obtain ⟨z, e⟩ := h.rls y hy + obtain ⟨z', e', -⟩ := tcbK _ z e + exact ⟨z', e'⟩ + rpos := fun p y k e a => h.rpos p y k ((faultClrA_sc hk).1 e) a + irqN := h.irqN.updTcbA hk rfl + zc := fun u y e hs hr => by + by_cases hu : u = t + · subst hu; rw [faultClrA_at] at e; cases e; exact h.zc u x hk hs hr + · rw [faultClrA_ne hu] at e; exact h.zc u y e hs hr + rlsRun := fun u hr => by + obtain ⟨z, e, r⟩ := h.rlsRun u hr + obtain ⟨z', e', f, -⟩ := tcbK _ z e + exact ⟨z', e', by rw [f]; exact r⟩ + curScReady := fun hc y k e ha => h.curScReady hc y k ((faultClrA_sc hk).1 e) ha + faultSt := fun p y e hf => by + by_cases hp : p = t + · subst hp; rw [faultClrA_at] at e; cases e; exact absurd rfl hf + · rw [faultClrA_ne hp] at e; exact h.faultSt p y e hf } + +theorem SIdleA.ofFaultClr {m : McState} {n : Nat} (h : SIdleA σ m n) (hk : σ.kheap t = some (.tcb x)) : + SIdleA (faultClrA σ t x) m n := by + have vt : ∀ c < n, vtCapA (faultClrA σ t x) (m.idleThreadOf c) = some .nullCap ∧ + vcpuOfA (faultClrA σ t x) (m.idleThreadOf c) = none := by + intro c hc + obtain ⟨y, hy, -⟩ := h.f3 c hc + obtain ⟨y', hy', -, -, fa, fv⟩ := faultClrA_tcb hk hy + exact idleVtKeepA hy hy' fv (by rw [fa]) (h.idleSlot c hc) (h.idleBound c hc) + have cap : ∀ sl, specCapAtA (faultClrA σ t x) sl = specCapAtA σ sl := by + intro sl + unfold specCapAtA + by_cases e : sl.1 = t + · rw [e, faultClrA_at, hk] + rfl + · rw [faultClrA_ne e] + refine ⟨fun c hc => ?_, fun y k hy => h.f4 y k ((faultClrA_sc hk).1 hy), + fun p y k hy ht => h.f5 p y k ((faultClrA_sc hk).1 hy) ht, h.f6.of_eq cap, + fun y k hy => h.f7 y k ((faultClrA_sc hk).1 hy), fun c hc => (vt c hc).1, fun c hc => (vt c hc).2⟩ + obtain ⟨y, hy, hs⟩ := h.f3 c hc + obtain ⟨y', hy', f, -⟩ := faultClrA_tcb hk hy + exact ⟨y', hy', f.trans hs⟩ + +end Clr + +/-- A step that keeps what `SIKA` keeps still does with the same thread's fault cleared on both sides. -/ +theorem SIKA.clrA {i : ObjRef} {M N : ObjRef → Prop} {don : Bool} {σ σ' : State Unit} (R : SIKA i M N don σ σ') + {t : ObjRef} {x y : Tcb} (hx : σ.kheap t = some (.tcb x)) (hy : σ'.kheap t = some (.tcb y)) : + SIKA i M N don (faultClrA σ t x) (faultClrA σ' t y) := by + refine ⟨?_, fun p => ?_, R.rq, R.rls, R.act, fun g => ?_⟩ + · have e := R.frame + unfold faultClrA + conv_lhs => rw [e] + · by_cases h : p = t + · subst h + rw [faultClrA_at, faultClrA_at] + have o := R.obj p + rw [hx, hy] at o + have key : TcbIpcA i M don p x y → TcbIpcA i M don p { x with tcbFault := none } { y with tcbFault := none } := + fun g => ⟨rfl, g.frame, g.buf, g.mcp, g.yt, g.caps, g.st, g.sc, g.vcpu, g.flags⟩ + cases o with + | same => exact .same _ + | tcb _ _ g => exact .tcb _ _ (key g) + · rw [faultClrA_ne h, faultClrA_ne h] + exact R.obj p + · obtain ⟨g0, g0c⟩ := g + have g1 := R.rdy ⟨fun z k hz => g0 z k ((faultClrA_sc hx).2 hz), + fun hc z k hz ha => g0c hc z k ((faultClrA_sc hx).2 hz) ha⟩ + exact ⟨fun z k hz => g1.1 z k ((faultClrA_sc hy).1 hz), fun hc z k hz ha => g1.2 hc z k ((faultClrA_sc hy).1 hz) ha⟩ + +/-- `SInvXA` across an `SIKA` step whose new queue members or switch targets, if any, are runnable. -/ +theorem SIKA.sinvQA {σ σ' : State Unit} {M N : ObjRef → Prop} {don : Bool} {m : McState} {n : Nat} + (R : SIKA σ.idleThread M N don σ σ') + (h : SInvXA σ m n) (hMi : ∀ c < n, ¬ M (m.idleThreadOf c)) (hMI : ¬ M σ.idleThread) + (hMq : ∀ y, M y → ∀ d p, y ∉ σ.readyQueues d p) + (hN : ∀ y, N y → y ≠ σ.curThread ∧ y ≠ σ.idleThread ∧ (∀ c < n, m.idleThreadOf c ≠ y) ∧ + (((∃ d p, y ∈ σ'.readyQueues d p) ∨ σ'.schedulerAction = .switchThread y) → + ∀ z, σ'.kheap y = some (.tcb z) → runnable z.tcbState = true)) + (hN' : ∀ y, N y → ∃ z, σ.kheap y = some (.tcb z)) + (hsw : ∀ u, σ.schedulerAction ≠ .switchThread u) + (hMrls : ∀ y, M y → y ∉ σ.releaseQueue) + (hRdy : RdyA σ) + (hMf : ∀ p, M p → ∀ x y, σ.kheap p = some (.tcb x) → σ'.kheap p = some (.tcb y) → y.tcbFault ≠ none → + FaultStA y.tcbState) : SInvXA σ' m n := by + let N' : ObjRef → Prop := fun y => N y ∧ ((∃ d p, y ∈ σ'.readyQueues d p) ∨ σ'.schedulerAction = .switchThread y) + have R' : SIKA σ.idleThread M N' don σ σ' := by + refine ⟨R.frame, R.obj, fun d p y hy => ?_, R.rls, ?_, R.rdy⟩ + · rcases R.rq d p y hy with a | a | a + · exact Or.inl a + · exact Or.inr (Or.inl ⟨a, Or.inl ⟨d, p, hy⟩⟩) + · exact Or.inr (Or.inr a) + · rcases R.act with a | a | ⟨y, ny, a⟩ + · exact Or.inl a + · exact Or.inr (Or.inl a) + · exact Or.inr (Or.inr ⟨y, ⟨ny, Or.inr a⟩, a⟩) + exact R'.sinv h hMi hMI hMq (fun y e => ⟨(hN y e.1).1, (hN y e.1).2.1, (hN y e.1).2.2.1, (hN y e.1).2.2.2 e.2⟩) + (fun y e => hN' y e.1) hsw hMrls hRdy hMf + +/-- **The spec keeps of a Reply to a faulted caller** (module docstring). `σ1 .. σ4`: after `reply_remove`, the +refill test, the register writes, the fault clear (`σ3c`) and the state write; `σ5`: after the tail. -/ +theorem replyFault_specKeepA {σ σ1 σ2 σ3 σ3c σ4 σ5 : State Unit} {m : McState} {n : Nat} {t r : ObjRef} + {xt : Tcb} (hkt : σ.kheap t = some (.tcb xt)) (hst : xt.tcbState = .blockedOnReply r) + (hback : ∀ sc, xt.tcbSchedContext = some sc → ∀ y k, σ.kheap sc = some (.schedContext y k) → y.scTcb = some t) + (hinv : SInvXA σ m n) (hI : SIdleA σ m n) (hV : SchedValidA σ) (hD : RqDomPrio σ) (hA : ActRepA σ) + (hsw : ∀ u, σ.schedulerAction ≠ .switchThread u) (hnq : ∀ d p, t ∉ σ.readyQueues d p) + (hcur : t ≠ σ.curThread) + (h1 : ((), σ1) ∈ (replyRemove (Z := Unit) t r σ).1) + (h2 : ((), σ2) ∈ ((getTcbObjRef (Z := Unit) Tcb.tcbSchedContext t >>= + ifSporadicCurScTestRefillUnblockCheck) σ1).1) + {uc : UserContext} + (h3 : ((), σ3) ∈ (asUser (Z := Unit) t (fun _ => ({((), uc)}, False)) σ2).1) + (h3c : ((), σ3c) ∈ (threadSet (Z := Unit) (fun tcb => { tcb with tcbFault := none }) t σ3).1) + (hD3 : RqDomPrio σ3c) {ts : ThreadState} (hts : ts = .restart ∨ ts = .inactive) + (h4 : ((), σ4) ∈ (setThreadState (Z := Unit) t ts σ3c).1) + (hstate : ∀ z, σ4.kheap t = some (.tcb z) → z.tcbState = ts) + (hidle4 : ts = .inactive → (∀ d p, t ∉ σ4.readyQueues d p) ∧ σ4.schedulerAction ≠ .switchThread t) + (hsuf4 : ∀ x p y k, σ4.kheap t = some (.tcb x) → x.tcbSchedContext = some p → + σ4.kheap p = some (.schedContext y k) → scActive y = true → fl4RefillSufficient 0 (refillHd y) = true) + {fault : Option Fault} (h5 : ((), σ5) ∈ (replyTailSpecA t fault σ4).1) + (hto : ¬ ReplyTmoCondA σ4 t fault) : + IpcKeepA (faultClrA σ t xt) σ5 ∧ SInvXA σ5 m n ∧ SIdleA σ5 m n ∧ SchedValidA σ5 ∧ QSendA σ σ5 ∧ RepSubA σ σ5 ∧ + UFrA (fun _ => False) σ σ5 ∧ σ5.kheap = σ4.kheap ∧ + σ5.machineState.underlyingMemory = σ4.machineState.underlyingMemory := by + have htsB : BlockedTsA ts = false := by rcases hts with rfl | rfl <;> rfl + have htsI : ts ≠ .idleThreadState := by rcases hts with rfl | rfl <;> intro c <;> cases c + -- the idle thread + have hidl : ∀ c < n, m.idleThreadOf c ≠ t := fun c hc e => by + obtain ⟨x, hx, hs⟩ := hI.f3 c hc + rw [e, hkt] at hx + cases hx + rw [hst] at hs + cases hs + have hti : t ≠ σ.idleThread := fun e => by + obtain ⟨x, hx, hs⟩ := hinv.idleTcb + rw [← e, hkt] at hx + cases hx + rw [hst] at hs + cases hs + -- `reply_remove`, the refill test and the register writes + obtain ⟨S1, P1, hsc1⟩ := replyRemove_keepA (i := σ.idleThread) (t := t) rfl hI hti hkt h1 + have F1 : FIA σ1 := replyRemove_fia t r σ ⟨SchedValidA.toFIA hV, hD⟩ _ h1 + have B1 : BAs [t] σ1 := fun u hu x1 sc hx1 hs => by + obtain rfl := List.mem_singleton.1 hu + have act : ScActiveAtA σ sc := by + rcases hsc1 x1 sc hx1 hs with ⟨a, -⟩ | ⟨⟨y, k, hy, ne⟩, -⟩ + · exact (SchedValidA.toFIA hV).1.blk _ xt sc hkt (by rw [hst]; rfl) a + · exact ⟨y, k, hy, hA sc y k hy ne⟩ + obtain ⟨y, k, hy, a⟩ := act + obtain ⟨y', hy', g⟩ := S1.scF hy + exact ⟨x1, sc, hx1, hs, y', k, hy', by unfold scActive at a ⊢; rw [g.rm]; exact a⟩ + have S2 : SIKA σ.idleThread (· = t) (· = t) false σ1 σ2 := + PresR.bind (eqPres_sikA (by unfold getTcbObjRef; exact threadGet_eqPres _ _)) + (fun o => refillCheck_sikA o) σ1 _ h2 + have F2 : FIA σ2 ∧ BAs [t] σ2 := by + obtain ⟨⟨o, s⟩, hg, hr⟩ := mem_bindA.1 h2 + have es : σ1 = s := (show PresR (@Eq (State Unit)) (getTcbObjRef (Z := Unit) Tcb.tcbSchedContext t) by + unfold getTcbObjRef; exact threadGet_eqPres _ _) σ1 _ hg + subst es + exact refillCheckT_fia o (BAs [t]) (fun _ _ k h => h.ofTKeep k) σ1 ⟨F1, B1⟩ _ hr + have S3 : SIKA σ.idleThread (· = t) (· = t) false σ2 σ3 := asUser_sikA t _ σ2 _ h3 + obtain ⟨y3, uc3, hy3, e3a⟩ := asUser_memA t _ h3 + have e3a' : σ3 = { σ2 with kheap := Function.update σ2.kheap t (some (.tcb { y3 with tcbArch := archTcbContextSet uc3 y3.tcbArch })) } := e3a + have F3 : FIA σ3 := by + rw [e3a'] + exact F2.1.ofTcbUpd hy3 rfl (fun _ => rfl) + have B3 : BAs [t] σ3 := by + rw [e3a'] + exact F2.2.tcbUpd hy3 rfl + have S13 : SIKA σ.idleThread (· = t) (· = t) false σ1 σ3 := S2.trans S3 + have S03 : SIKA σ.idleThread (· = t) (· = t) true σ σ3 := S1.trans S13.up + -- the fault clear + obtain ⟨x1, hx1, -⟩ := S1.tcbF hkt + obtain ⟨x3, hx3, g3⟩ := S03.tcbF hkt + obtain ⟨x3', hx3', e3c⟩ := threadSet_memA _ t h3c + have ex : x3' = x3 := by + rw [hx3] at hx3' + exact (KernelObject.tcb.inj (Option.some.inj hx3')).symm + have e3 : σ3c = faultClrA σ3 t x3 := by + rw [ex] at e3c + exact e3c + have F3c : FIA σ3c := by + rw [e3] + exact F3.ofTcbUpd hx3 rfl (fun _ => rfl) + have B3c : BAs [t] σ3c := by + rw [e3] + exact B3.tcbUpd hx3 rfl + -- the state write + have F4 : FIA σ4 ∧ BAs [t] σ4 := + setThreadState_fia t ts htsB (BAs [t]) (fun σ p x x' hx h1 h => h.tcbUpd hx h1) + (fun σ a h => h.act a) σ3c ⟨F3c, B3c⟩ _ h4 + have S4 : SIKA σ.idleThread (· = t) (· = t) false σ3c σ4 := + setThreadState_sikA (i := σ.idleThread) (M := (· = t)) (N := (· = t)) (don := false) t rfl ts htsI σ3c _ h4 + have S14c : SIKA σ.idleThread (· = t) (· = t) false (faultClrA σ1 t x1) σ4 := + (S13.clrA hx1 hx3).trans (e3 ▸ S4) + have S04c : SIKA σ.idleThread (· = t) (· = t) true (faultClrA σ t xt) σ4 := + (S03.clrA hkt hx3).trans (e3 ▸ S4).up + have inv0 : SInvXA (faultClrA σ t xt) m n := Sched.SInvXA.ofFaultClr hinv hkt + have idl0 : SIdleA (faultClrA σ t xt) m n := Sched.SIdleA.ofFaultClr hI hkt + have hqs : ∀ y, y = t → ∀ d p, y ∉ σ.readyQueues d p := fun y e => e ▸ hnq + have hMrls : ∀ y, y = t → y ∉ (faultClrA σ t xt).releaseQueue := fun y e hr => by + obtain ⟨x, hx, hrn⟩ := hinv.rlsRun y hr + rw [e, hkt] at hx; cases hx + rw [hst] at hrn; cases hrn + have sinv : ∀ σ', SIKA σ.idleThread (· = t) (· = t) true (faultClrA σ t xt) σ' → σ'.kheap = σ4.kheap → + (ts = .inactive → (∀ d p, t ∉ σ'.readyQueues d p) ∧ σ'.schedulerAction ≠ .switchThread t) → + SInvXA σ' m n := fun σ' R hk hin => by + refine Sched.SIKA.sinvQA (σ := faultClrA σ t xt) R inv0 (fun c hc e => hidl c hc e) (fun e => hti e.symm) hqs + (fun y e => ⟨e ▸ hcur, e ▸ hti, fun c hc a => hidl c hc (a.trans e), fun q z hz => ?_⟩) + (fun y e => e ▸ ⟨_, faultClrA_at⟩) hsw hMrls + (idl0.rdyA inv0) + (fun p e x y hx hy hf => by + obtain ⟨x0, hx0, g⟩ := R.tcbB hy + rw [e, faultClrA_at] at hx0; cases hx0 + exact absurd g.fault hf) + rcases hts with h | h + · rw [e, hk] at hz + rw [hstate z hz, h] + rfl + · obtain ⟨a, b⟩ := hin h + rcases q with ⟨d, p, q⟩ | q + · exact absurd (e ▸ q) (a d p) + · exact absurd (e ▸ q) b + have sidle4 : SIdleA σ4 m n := S04c.sidle idl0 (fun c hc e => hidl c hc e) (idl0.rdyA inv0) + -- the caller's SC and handler at the tail + obtain ⟨x4, hx4, g4⟩ := S04c.tcbF (faultClrA_at (σ := σ) (t := t) (x := xt)) + have link : ∀ p y k, x4.tcbSchedContext = some p → σ4.kheap p = some (.schedContext y k) → + y.scTcb = some t := by + intro p y4 k4 hp hy4 + have e4 : x4.tcbSchedContext = x1.tcbSchedContext := by + obtain ⟨z, hz, e⟩ := S14c.tcbScA (faultClrA_at (σ := σ1) (t := t) (x := x1)) + rw [hx4] at hz + cases hz + exact e + obtain ⟨y1, hy1c, g⟩ := S14c.scB hy4 + have hy1 := (faultClrA_sc hx1).1 hy1c + have th : y4.scTcb = y1.scTcb := g.th.resolve_right fun a => by cases a.1 + rw [th] + rcases hsc1 x1 p hx1 (e4 ▸ hp) with ⟨a, b⟩ | ⟨-, b⟩ + · obtain ⟨y0, k0, hy0, e0⟩ := b y1 k4 hy1 + rw [e0] + exact hback p a y0 k0 hy0 + · exact b y1 k4 hy1 + have hcur4 : σ4.curThread = σ.curThread := by rw [S04c.frame]; rfl + -- the tail + have tail : σ5.kheap = σ4.kheap ∧ σ5.idleThread = σ4.idleThread ∧ σ5.curTime = σ4.curTime ∧ + SInvXA σ5 m n ∧ SchedValidA σ5 ∧ QKeepA σ4 σ5 ∧ SchedFrameA σ4 σ5 := by + have refl : σ5 = σ4 → _ := fun e => (show σ5.kheap = σ4.kheap ∧ σ5.idleThread = σ4.idleThread ∧ + σ5.curTime = σ4.curTime ∧ SInvXA σ5 m n ∧ SchedValidA σ5 ∧ QKeepA σ4 σ5 ∧ SchedFrameA σ4 σ5 by + rw [e] + exact ⟨rfl, rfl, rfl, sinv σ4 S04c rfl hidle4, F4.1.1.toA F4.1.2, QKeepA.refl _, PreO.refl _⟩) + rcases hts with hr | hin + swap + · rw [replyTailSpec_skipA hx4 (Or.inr (by rw [hstate x4 hx4, hin]; rfl))] at h5 + exact refl (Prod.mk.inj (show ((), σ5) = ((), σ4) from h5)).2 + have notIn : ∀ σ' : State Unit, ts = .inactive → + (∀ d p, t ∉ σ'.readyQueues d p) ∧ σ'.schedulerAction ≠ .switchThread t := + fun _ c => by rw [hr] at c; cases c + unfold replyTailSpecA at h5 + obtain ⟨⟨o, s1⟩, a1, h5⟩ := mem_bindA.1 h5 + unfold getTcbObjRef at a1 + obtain ⟨eo, es1⟩ := Prod.mk.inj (threadGet_valA Tcb.tcbSchedContext hx4 a1) + rw [es1] at h5 + simp only at h5 + obtain ⟨⟨tst, s2⟩, a2, h5⟩ := mem_bindA.1 h5 + unfold getThreadState at a2 + obtain ⟨etst, es2⟩ := Prod.mk.inj (threadGet_valA Tcb.tcbState hx4 a2) + rw [es2] at h5 + simp only at h5 + unfold Fl4Spec.NonDet.when at h5 + split_ifs at h5 with hg + swap + · exact refl (Prod.mk.inj (show ((), σ5) = ((), σ4) from h5)).2 + cases o with + | none => exact absurd rfl hg.1 + | some p0 => + obtain ⟨⟨p, s3⟩, a3, h6⟩ := mem_bindA.1 h5 + have e3' : (p, s3) = (p0, σ4) := a3 + obtain ⟨e3a, e3b⟩ := Prod.mk.inj e3' + rw [e3a, e3b] at h6 + simp only at h6 + obtain ⟨⟨y, s4⟩, a4, h7⟩ := mem_bindA.1 h6 + have e4 : s4 = σ4 := (getSchedContext_eqPres p0 σ4 _ a4).symm + obtain ⟨-, k, hk⟩ := getSchedContext_mem a4 + rw [e4] at h7 + simp only at h7 + obtain ⟨⟨now, s5⟩, a5, h8⟩ := mem_bindA.1 h7 + have e5 : s5 = σ4 := ((PresR.gets (R := @Eq (State Unit)) _) σ4 _ a5).symm + have enow : now = σ4.curTime := (Prod.mk.inj (show (now, s5) = (σ4.curTime, σ4) from a5)).1 + rw [e5] at h8 + simp only at h8 + split_ifs at h8 with hrf + · have S5 : SIKA σ.idleThread (· = t) (· = t) false σ4 σ5 := + possibleSwitchTo_sikA (i := σ.idleThread) (M := (· = t)) (N := (· = t)) (don := false) t rfl σ4 _ h8 + have Q5 : QKeepA σ4 σ5 := (inferInstance : QInst (possibleSwitchTo (Z := Unit) t)).pres σ4 _ h8 + have M5 : SchedFrameA σ4 σ5 := SPresInst.pres σ4 _ h8 + have hk5 : σ5.kheap = σ4.kheap := by + obtain ⟨q, -⟩ := possibleSwitchTo_memA t h8 + have e : σ5 = _ := q.1 + rw [e] + have fr := S5.frame + refine ⟨hk5, by rw [fr], by rw [fr], sinv σ5 (S04c.trans S5.up) hk5 (notIn _), ?_, Q5, M5⟩ + have F5 := possibleSwitchTo_fia' t (fun _ => True) (fun _ _ h => h) (fun _ _ h => h) σ4 + ⟨F4.1, F4.2 _ (List.mem_singleton_self _), trivial⟩ _ h8 + exact F5.1.1.toA F5.1.2 + · unfold replyLateSpecA at h8 + obtain ⟨⟨tc, s6⟩, a6, h9⟩ := mem_bindA.1 h8 + have e6 : (tc, s6) = (x4, σ4) := by + have hg4 : getTcb t σ4 = some x4 := by simp [getTcb, readObject, hx4] + have := a6 + unfold Fl4Spec.NonDet.getsThe at this + rw [hg4] at this + exact this + obtain ⟨e6a, e6b⟩ := Prod.mk.inj e6 + rw [e6a, e6b] at h9 + simp only at h9 + rw [if_neg (fun hc => hto ⟨x4, p0, y, k, hx4, eo.symm, etst ▸ hg.2, hk, enow ▸ hrf, hc.1, hc.2⟩)] at h9 + have hlink : y.scTcb = some t := link p0 y k eo.symm hk + have PQ : PostQA t σ4 σ5 := postpone_postqA hk hlink h9 + have fr := PQ.frame + have hk5 : σ5.kheap = σ4.kheap := by rw [fr] + refine ⟨hk5, by rw [fr], by rw [fr], (sinv σ4 S04c rfl (notIn _)).postq PQ ⟨x4, hx4, etst ▸ hg.2⟩ + (postq_zcA (sinv σ4 S04c rfl (notIn _)) hx4 eo.symm hk (F4.2 _ (List.mem_singleton_self _) x4 p0 hx4 eo.symm) + hsuf4 (by rw [← enow]; exact hrf)), ?_, + (inferInstance : QInst (postpone (Z := Unit) p0)).pres σ4 _ h9, SPresInst.pres σ4 _ h9⟩ + have hD4 : RqDomPrio σ4 := by + intro f d q z hz hm + rw [setThreadState_rqA h4] at hm + obtain ⟨z0, hz0, ed, ep⟩ := setThreadState_domA h4 hz + have := hD3 f d q z0 hz0 hm + exact ⟨this.1.trans ed, this.2.trans ep⟩ + have act : ActTcbA σ4 t := F4.2 _ (List.mem_singleton_self _) x4 p0 hx4 eo.symm + have P5 := postpone_fia p0 (fun _ => True) (fun _ _ _ _ _ => trivial) σ4 + ⟨F4.1, hD4, trivial, fun z kz u hz hu => by + rw [hk] at hz + cases hz + rw [hlink] at hu + cases hu + exact act, fun z kz u hz hu => by + rw [hk] at hz + cases hz + rw [hlink] at hu + cases hu + exact Or.inl (by rw [hcur4]; exact Ne.symm hcur)⟩ _ h9 + exact P5.1.1.toA P5.1.2 + obtain ⟨hk5, hi5, ht5, sinv5, valid5, Q5, M5⟩ := tail + have K : IpcKeepA (faultClrA σ t xt) σ5 := by + have K4 := S04c.ipcKeep + exact ⟨fun p x h => by rw [hk5]; exact K4.tcb p x h, fun p y k h => by rw [hk5]; exact K4.scF p y k h, + fun p y k h => K4.scB p y k (by rw [← hk5]; exact h)⟩ + refine ⟨K, sinv5, sidle4.congr hk5 hi5 (by rw [ht5]; exact BitVec.le_refl _), valid5, ?_, ?_, ?_, hk5, M5.2.2 ▸ rfl⟩ + · have Q1 : QKeepA σ σ1 := (inferInstance : QInst (replyRemove (Z := Unit) t r)).pres σ _ h1 + have Q2 : QKeepA σ1 σ2 := by + refine (show PresR QKeepA (getTcbObjRef (Z := Unit) Tcb.tcbSchedContext t >>= + ifSporadicCurScTestRefillUnblockCheck) from ?_) σ1 _ h2 + unfold ifSporadicCurScTestRefillUnblockCheck; qpres + have Q3 : QKeepA σ2 σ3 := asUser_qA t _ σ2 _ h3 + have Q3c : QKeepA σ3 σ3c := + (show PresR QKeepA (threadSet (Z := Unit) (fun tcb => { tcb with tcbFault := none }) t) by qpres) σ3 _ h3c + have Q4 : QKeepA σ3c σ4 := (inferInstance : QInst (setThreadState (Z := Unit) t ts)).pres σ3c _ h4 + exact (Q1.trans (Q2.trans (Q3.trans (Q3c.trans (Q4.trans Q5))))).toSend + · have R3c : RepSubA σ3 σ3c := by + rw [e3] + exact repSub_upd (fun _ _ c => by cases c) + exact PreO.trans P1 (PreO.trans (RepSubA.of_sik S13) (PreO.trans R3c (PreO.trans (RepSubA.of_sik S4) + (RepSubA.of_kheap hk5)))) + · have U1 : UFrA (fun _ => False) σ σ1 := UFrA.of_fr (SPresInst.pres σ _ h1) + have U2 : UFrA (fun _ => False) σ1 σ2 := by + refine UFrA.of_fr ((show SPresA (getTcbObjRef (Z := Unit) Tcb.tcbSchedContext t >>= + ifSporadicCurScTestRefillUnblockCheck) from ?_) σ1 _ h2) + exact SPresInst.pres + have U3 : UFrA (fun _ => False) σ2 σ3 := UFrA.of_fr (asUser_presA t _ σ2 _ h3) + have U3c : UFrA (fun _ => False) σ3 σ3c := + UFrA.of_fr ((show SPresA (threadSet (Z := Unit) (fun tcb => { tcb with tcbFault := none }) t) from + SPresInst.pres) σ3 _ h3c) + exact PreO.trans U1 (PreO.trans U2 (PreO.trans U3 (PreO.trans U3c (PreO.trans + (UFrA.of_fr (SPresInst.pres σ3c _ h4)) (UFrA.of_fr M5))))) + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3ReplyFaultMidA.lean b/proofs/Fl4ProofsA64/P3/Stage3ReplyFaultMidA.lean new file mode 100644 index 000000000..904bcc513 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ReplyFaultMidA.lean @@ -0,0 +1,128 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2b): **the fault Reply up to the SC tail** (spec only), the sibling of +`replyLive_midA`. + +* **`replyFault_midA`**: after `reply_remove`, the refill test, the register writes, the fault clear and the state + write: the donating `SIKA` from the pre-state with the caller's fault cleared (`faultClrA`), `FIA` and the caller's + active SC (`BoundActive`), the facts the SC tail's locality (`tailS`) is read from. +* **`replyFault_noSwA`**: the caller is not a pending switch target after those steps, given no pending switch at the + start (`SwN`). + +Premises: the record's `LoopInvA` (for `replyRemove_stepA`'s `NoSwA`). New module. +-/ +import Fl4ProofsA64.P3.Stage3ReplyFaultKeepSpecA +import Fl4ProofsA64.P3.Stage3ReplyKeepSpecA +import Fl4ProofsA64.P3.Stage3SigSwA +import Fl4ProofsA64.P3.ReplyRemoveInvA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4SpecA64 + +noncomputable section + +/-- **The fault Reply up to the SC tail** (module docstring). -/ +theorem replyFault_midA {σ σ1 σ2 σ3 σ3c σ4 : State Unit} {m : McState} {n : Nat} {t r : ObjRef} + {xt : Tcb} (hkt : σ.kheap t = some (.tcb xt)) (hst : xt.tcbState = .blockedOnReply r) + (hinv : SInvXA σ m n) (hI : SIdleA σ m n) (hV : SchedValidA σ) (hD : RqDomPrio σ) (hA : ActRepA σ) + (h1 : ((), σ1) ∈ (replyRemove (Z := Unit) t r σ).1) + (h2 : ((), σ2) ∈ ((getTcbObjRef (Z := Unit) Tcb.tcbSchedContext t >>= + ifSporadicCurScTestRefillUnblockCheck) σ1).1) + {uc : UserContext} + (h3 : ((), σ3) ∈ (asUser (Z := Unit) t (fun _ => ({((), uc)}, False)) σ2).1) + (h3c : ((), σ3c) ∈ (threadSet (Z := Unit) (fun tcb => { tcb with tcbFault := none }) t σ3).1) + {ts : ThreadState} (hts : ts = .restart ∨ ts = .inactive) + (h4 : ((), σ4) ∈ (setThreadState (Z := Unit) t ts σ3c).1) : + SIKA σ.idleThread (· = t) (· = t) true (faultClrA σ t xt) σ4 ∧ FIA σ4 ∧ BoundActive t σ4 := by + have htsB : BlockedTsA ts = false := by rcases hts with rfl | rfl <;> rfl + have htsI : ts ≠ .idleThreadState := by rcases hts with rfl | rfl <;> intro c <;> cases c + have hidl : ∀ c < n, m.idleThreadOf c ≠ t := fun c hc e => by + obtain ⟨x, hx, hs⟩ := hI.f3 c hc + rw [e, hkt] at hx + cases hx + rw [hst] at hs + cases hs + have hti : t ≠ σ.idleThread := fun e => by + obtain ⟨x, hx, hs⟩ := hinv.idleTcb + rw [← e, hkt] at hx + cases hx + rw [hst] at hs + cases hs + obtain ⟨S1, P1, hsc1⟩ := replyRemove_keepA (i := σ.idleThread) (t := t) rfl hI hti hkt h1 + have F1 : FIA σ1 := replyRemove_fia t r σ ⟨SchedValidA.toFIA hV, hD⟩ _ h1 + have B1 : BAs [t] σ1 := fun u hu x1 sc hx1 hs => by + obtain rfl := List.mem_singleton.1 hu + have act : ScActiveAtA σ sc := by + rcases hsc1 x1 sc hx1 hs with ⟨a, -⟩ | ⟨⟨y, k, hy, ne⟩, -⟩ + · exact (SchedValidA.toFIA hV).1.blk _ xt sc hkt (by rw [hst]; rfl) a + · exact ⟨y, k, hy, hA sc y k hy ne⟩ + obtain ⟨y, k, hy, a⟩ := act + obtain ⟨y', hy', g⟩ := S1.scF hy + exact ⟨x1, sc, hx1, hs, y', k, hy', by unfold scActive at a ⊢; rw [g.rm]; exact a⟩ + have S2 : SIKA σ.idleThread (· = t) (· = t) false σ1 σ2 := + PresR.bind (eqPres_sikA (by unfold getTcbObjRef; exact threadGet_eqPres _ _)) + (fun o => refillCheck_sikA o) σ1 _ h2 + have F2 : FIA σ2 ∧ BAs [t] σ2 := by + obtain ⟨⟨o, s⟩, hg, hr⟩ := mem_bindA.1 h2 + have es : σ1 = s := (show PresR (@Eq (State Unit)) (getTcbObjRef (Z := Unit) Tcb.tcbSchedContext t) by + unfold getTcbObjRef; exact threadGet_eqPres _ _) σ1 _ hg + subst es + exact refillCheckT_fia o (BAs [t]) (fun _ _ k h => h.ofTKeep k) σ1 ⟨F1, B1⟩ _ hr + have S3 : SIKA σ.idleThread (· = t) (· = t) false σ2 σ3 := asUser_sikA t _ σ2 _ h3 + obtain ⟨y3, uc3, hy3, e3a⟩ := asUser_memA t _ h3 + have e3a' : σ3 = { σ2 with kheap := Function.update σ2.kheap t (some (.tcb { y3 with tcbArch := archTcbContextSet uc3 y3.tcbArch })) } := e3a + have F3 : FIA σ3 := by + rw [e3a'] + exact F2.1.ofTcbUpd hy3 rfl (fun _ => rfl) + have B3 : BAs [t] σ3 := by + rw [e3a'] + exact F2.2.tcbUpd hy3 rfl + have S13 : SIKA σ.idleThread (· = t) (· = t) false σ1 σ3 := S2.trans S3 + have S03 : SIKA σ.idleThread (· = t) (· = t) true σ σ3 := S1.trans S13.up + obtain ⟨x1, hx1, -⟩ := S1.tcbF hkt + obtain ⟨x3, hx3, g3⟩ := S03.tcbF hkt + obtain ⟨x3', hx3', e3c⟩ := threadSet_memA _ t h3c + have ex : x3' = x3 := by + rw [hx3] at hx3' + exact (KernelObject.tcb.inj (Option.some.inj hx3')).symm + have e3 : σ3c = faultClrA σ3 t x3 := by + rw [ex] at e3c + exact e3c + have F3c : FIA σ3c := by + rw [e3] + exact F3.ofTcbUpd hx3 rfl (fun _ => rfl) + have B3c : BAs [t] σ3c := by + rw [e3] + exact B3.tcbUpd hx3 rfl + have F4 : FIA σ4 ∧ BAs [t] σ4 := + setThreadState_fia t ts htsB (BAs [t]) (fun σ p x x' hx h1 h => h.tcbUpd hx h1) + (fun σ a h => h.act a) σ3c ⟨F3c, B3c⟩ _ h4 + have S4 : SIKA σ.idleThread (· = t) (· = t) false σ3c σ4 := + setThreadState_sikA (i := σ.idleThread) (M := (· = t)) (N := (· = t)) (don := false) t rfl ts htsI σ3c _ h4 + have S04c : SIKA σ.idleThread (· = t) (· = t) true (faultClrA σ t xt) σ4 := + (S03.clrA hkt hx3).trans (e3 ▸ S4).up + exact ⟨S04c, F4.1, F4.2 _ (List.mem_singleton_self _)⟩ + +/-- **No pending switch target is the caller** across the steps of the fault Reply (module docstring). -/ +theorem replyFault_noSwA {σ σ1 σ2 σ3 σ3c σ4 : State Unit} {m : McState} {n : Nat} {t r : ObjRef} + {xt : Tcb} (hkt : σ.kheap t = some (.tcb xt)) (hst : xt.tcbState = .blockedOnReply r) + (hL : LoopInvA σ m n) (hsw : NoSwA σ) + (h1 : ((), σ1) ∈ (replyRemove (Z := Unit) t r σ).1) + (h2 : ((), σ2) ∈ ((getTcbObjRef (Z := Unit) Tcb.tcbSchedContext t >>= + ifSporadicCurScTestRefillUnblockCheck) σ1).1) + {uc : UserContext} + (h3 : ((), σ3) ∈ (asUser (Z := Unit) t (fun _ => ({((), uc)}, False)) σ2).1) + (h3c : ((), σ3c) ∈ (threadSet (Z := Unit) (fun tcb => { tcb with tcbFault := none }) t σ3).1) + {ts : ThreadState} (h4 : ((), σ4) ∈ (setThreadState (Z := Unit) t ts σ3c).1) : + SwN t σ4 := by + obtain ⟨-, -, -, -, -, ns1⟩ := replyRemove_stepA hL hkt (Or.inl hst) h1 + have s1 : SwN t σ1 := fun u e => absurd e (ns1 hsw u) + have s2 : SwN t σ2 := (HT.bind (Q := fun _ => SwN t) (HT.swn (by unfold getTcbObjRef; tpres) t) fun _ => + HT.swn (by unfold ifSporadicCurScTestRefillUnblockCheck ifCondRefillUnblockCheck; tpres) t) σ1 s1 _ h2 + have s3 : SwN t σ3 := HT.swn (asUser_tpres t _) t σ2 s2 _ h3 + have s3c : SwN t σ3c := threadSet_swn t _ σ3 s3 _ h3c + exact setThreadState_swn t ts σ3c s3c _ h4 + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3ReplyKeepSpecA.lean b/proofs/Fl4ProofsA64/P3/Stage3ReplyKeepSpecA.lean new file mode 100644 index 000000000..131eda0a0 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ReplyKeepSpecA.lean @@ -0,0 +1,896 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2b): **the spec keeps of the live Reply** to a fault-free receiver, from a +fault-free sender without an extra-cap insert, whose tail sends no timeout (the receiver's timeout handler is no +endpoint cap): `reply_remove`, the refill test, the transfer, `set_thread_state Running`, then the SC tail (nothing, +`possible_switch_to`, or `postpone`). + +* **`replyRemove_keepA`**: `SIKA` (donating), `RepSubA` (reply stacks only shrink) and the receiver's SC afterwards. +* **`replyLive_specKeepA`**: `IpcKeepA`, `SInvXA`, `SIdleA`, `SchedValidA`, `QSendA`, `RepSubA` and the user-memory + frame from the entry state to the final one. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3IpcSchedValidA +import Fl4ProofsA64.P3.Stage3SendQA +import Fl4ProofsA64.P3.Stage3DelXSchedValidA +import Fl4ProofsA64.P3.IpcScReplyTransferA +import Fl4ProofsA64.P3.Stage3ReplyExitA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P3 Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Classical + +noncomputable section + +/-! ## Reply stacks only shrink -/ + +/-- Every SC's reply list afterwards is contained in its reply list before. -/ +def RepSubA (σ σ' : State Unit) : Prop := + ∀ p y' k, σ'.kheap p = some (.schedContext y' k) → + ∃ y k0, σ.kheap p = some (.schedContext y k0) ∧ ∀ r ∈ y'.scReplies, r ∈ y.scReplies + +instance : PreO RepSubA := + ⟨fun _ _ y k h => ⟨y, k, h, fun _ a => a⟩, fun h1 h2 p y k h => by + obtain ⟨y2, k2, e2, a2⟩ := h2 p y k h + obtain ⟨y1, k1, e1, a1⟩ := h1 p y2 k2 e2 + exact ⟨y1, k1, e1, fun r hr => a1 r (a2 r hr)⟩⟩ + +theorem RepSubA.of_kheap {σ σ' : State Unit} (h : σ'.kheap = σ.kheap) : RepSubA σ σ' := + fun _ y k e => ⟨y, k, by rw [← h]; exact e, fun _ a => a⟩ + +theorem RepSubA.of_sik {i : ObjRef} {M N : ObjRef → Prop} {σ σ' : State Unit} (R : SIKA i M N false σ σ') : + RepSubA σ σ' := fun p y k h => by + obtain ⟨x, e, g⟩ := R.scB h + exact ⟨x, k, e, fun r a => by rwa [g.reps rfl] at a⟩ + +theorem repSub_upd {σ : State Unit} {p : ObjRef} {o : KernelObject} (h : ∀ y n, o ≠ .schedContext y n) : + RepSubA σ { σ with kheap := Function.update σ.kheap p (some o) } := fun q y k e => by + by_cases hq : q = p + · subst hq + simp only [Function.update_self] at e + exact absurd (Option.some.inj e) (h y k) + · simp only [Function.update_of_ne hq] at e + exact ⟨y, k, e, fun _ a => a⟩ + +theorem updSc_repSub (p : ObjRef) (f : SchedContext → SchedContext) + (hf : ∀ x, ∀ r ∈ (f x).scReplies, r ∈ x.scReplies) : PresR RepSubA (updateSchedContext (Z := Unit) p f) := by + intro σ r hr + obtain ⟨y, n, hk, e⟩ := updateSchedContext_memA p f hr + rw [e] + intro q z k hz + by_cases hq : q = p + · subst hq + simp only [Function.update_self] at hz + cases hz + exact ⟨y, n, hk, hf y⟩ + · simp only [Function.update_of_ne hq] at hz + exact ⟨z, k, hz, fun _ a => a⟩ + +/-! ## `reply_remove` -/ + +theorem scWithReply_memA {σ : State Unit} {r p : ObjRef} (h : scWithReply r σ = some p) : + ∃ y n, σ.kheap p = some (.schedContext y n) ∧ r ∈ y.scReplies := by + unfold scWithReply partialInv at h + split_ifs at h with hu + have e := Option.some.inj h + unfold The at e + rw [dif_pos hu] at e + have := (Classical.choose_spec hu).1 + rw [e] at this + exact of_decide_eq_true this + +variable {i t : ObjRef} + +private abbrev Mv (t : ObjRef) : ObjRef → Prop := (· = t) + +/-- A donating `SIKA` step that only shrinks the reply stacks. -/ +private def KR (i t : ObjRef) (σ σ' : State Unit) : Prop := SIKA i (Mv t) (Mv t) true σ σ' ∧ RepSubA σ σ' + +private instance : PreO (KR i t) := + ⟨fun s => ⟨SIKA.refl s, PreO.refl s⟩, fun h1 h2 => ⟨h1.1.trans h2.1, PreO.trans h1.2 h2.2⟩⟩ + +private theorem replyUnlinkTcb_krA (r : ObjRef) : PresR (KR i t) (replyUnlinkTcb (Z := Unit) t r) := fun s x hx => by + have a : SIKA i (Mv t) (Mv t) false s x.2 := replyUnlinkTcb_sikA (i := i) (M := Mv t) (N := Mv t) (don := false) t r rfl s x hx + exact ⟨a.up, RepSubA.of_sik a⟩ + +/-- The SC-thread donation: the old thread (not `idle_sc_ptr`'s) is unbound, `t` gets the SC. -/ +private theorem donate_krA {sc : ObjRef} (hsc : sc ≠ idleScPtr) (ht : t ≠ i) : + PresRC (KR i t) (fun s => ∀ y k, s.kheap sc = some (.schedContext y k) → y.scTcb ≠ some i) + (schedContextDonate (Z := Unit) sc t) := by + intro σ hσ r hr + unfold schedContextDonate at hr + obtain ⟨⟨f, s1⟩, h1, hr1⟩ := mem_bindA.1 hr + obtain ⟨hs, y, k, hk, hv⟩ := getScObjRef_mem h1 + simp only at hs hv hr1 + subst s1 + have tail : PresR (KR i t) (do + setScObjRef scTcbUpdate sc (some t) + setTcbObjRef (Z := Unit) tcbSchedContextUpdate t (some sc)) := by + refine PresR.bind (fun s x hx => ⟨?_, ?_⟩) (fun _ s x hx => ⟨?_, ?_⟩) + · exact updateSc_sikA sc (scTcbUpdate fun _ => some t) (fun _ => ⟨rfl, rfl, id, fun e => absurd e hsc, + Or.inr ⟨rfl, fun e => ht (Option.some.inj e)⟩, fun e => by cases e⟩) (fun _ => rfl) s x hx + · exact updSc_repSub sc (scTcbUpdate fun _ => some t) (fun _ _ a => a) s x hx + · exact threadSetSc_sikA (M := Mv t) (N := Mv t) t ht _ (Or.inl rfl) s x hx + · obtain ⟨z, -, e⟩ := threadSet_memA _ t hx + rw [e] + exact repSub_upd (by intro _ _ c; cases c) + obtain ⟨u, hu, hr2⟩ := mem_bindA.1 hr1 + refine PreO.trans ?_ (tail u.2 r hr2) + unfold NonDet.when at hu + split_ifs at hu with hf + swap + · have : u = ((), σ) := hu + rw [this]; exact PreO.refl _ + obtain ⟨o, ho, hu⟩ := mem_bindA.1 hu + cases f with + | none => exact absurd rfl hf + | some f => + have eo : o = (f, σ) := ho + subst eo + have hfi : f ≠ i := fun e => hσ y k hk (by rw [← hv, e]) + have body : PresR (KR i t) (do + tcbSchedAction (Z := Unit) tcbSchedDequeue f + tcbReleaseRemove f + setTcbObjRef tcbSchedContextUpdate f none + testReschedule f) := by + have of0 : ∀ {α : Type} {mm : SMonad Unit α}, PresR (SIKA i (Mv t) (Mv t) false) mm → PresR (KR i t) mm := + fun h s x hx => ⟨(h s x hx).up, RepSubA.of_sik (h s x hx)⟩ + refine PresR.bind (of0 (tcbSchedDeq_sikA _)) fun _ => PresR.bind (of0 (tcbReleaseRemove_sikA _)) fun _ => + PresR.bind (fun s x hx => ⟨threadSetSc_sikA (M := Mv t) (N := Mv t) _ hfi _ (Or.inr rfl) s x hx, ?_⟩) fun _ => of0 (testReschedule_sikA _) + obtain ⟨z, -, e⟩ := threadSet_memA _ f hx + rw [e] + exact repSub_upd (by intro _ _ c; cases c) + exact body _ _ hu + +private theorem replyUnlinkSc_thA (sc r : ObjRef) : PresR ScThA (replyUnlinkSc (Z := Unit) sc r) := by + have upd : ∀ f : SchedContext → SchedContext, (∀ z, (f z).scTcb = z.scTcb) → + PresR ScThA (updateSchedContext (Z := Unit) sc f) := fun f hf σ v hv => by + obtain ⟨z, n, hz, e⟩ := updateSchedContext_memA sc f hv + rw [e] + intro q y k hy + by_cases hq : q = sc + · subst hq + simp only [Function.update_self] at hy + cases hy + exact ⟨z, hz, hf z⟩ + · simp only [Function.update_of_ne hq] at hy + exact ⟨y, hy, rfl⟩ + have rd : ∀ {α : Type} {mm : SMonad Unit α}, PresR (@Eq (State Unit)) mm → PresR ScThA mm := + fun h s x hx => by rw [← h s x hx]; exact PreO.refl s + have up1 := upd (scRepliesUpdate List.tail) (fun _ => rfl) + have up2 := upd (scRepliesUpdate (List.takeWhile fun q => decide (q ≠ r))) (fun _ => rfl) + unfold replyUnlinkSc NonDet.liftM + refine PresR.bind (PresR.bind (rd (getSchedContext_eqPres sc)) fun _ => PresR.pure _) fun _ => + PresR.bind (rd (getReply_eqPres r)) fun _ => ?_ + split_ifs + all_goals + repeat' (first + | exact setReplyObjRef_scThA _ _ _ + | exact rd (PresR.assert _) + | exact up1 + | exact up2 + | exact PresR.pure _ + | refine PresR.bind ?_ (fun _ => ?_) + | split) + +private theorem donate_thA (sc t : ObjRef) : + HT (fun _ => True) (schedContextDonate (Z := Unit) sc t) + (fun _ σ => ∀ y k, σ.kheap sc = some (.schedContext y k) → y.scTcb = some t) := by + unfold schedContextDonate + refine HT.bind (Q := fun _ _ => True) (fun _ _ _ _ => trivial) fun _ => ?_ + refine HT.bind (Q := fun _ _ => True) (fun _ _ _ _ => trivial) fun _ => ?_ + refine HT.bind (Q := fun _ σ => ∀ y k, σ.kheap sc = some (.schedContext y k) → y.scTcb = some t) ?_ fun _ => ?_ + · intro σ _ v hv y k hy + unfold setScObjRef at hv + obtain ⟨z, n, -, e⟩ := updateSchedContext_memA sc _ hv + rw [e] at hy + simp only [Function.update_self] at hy + cases hy + rfl + · intro σ hσ v hv y k hy + obtain ⟨z, hz, e⟩ := threadSet_memA _ t hv + rw [e] at hy + by_cases hq : sc = t + · subst hq; simp only [Function.update_self] at hy; cases hy + · simp only [Function.update_of_ne hq] at hy + exact hσ y k hy + +private theorem replyUnlinkSc_krA {σ : State Unit} {sc r : ObjRef} + (hidle : ∀ y k, σ.kheap idleScPtr = some (.schedContext y k) → y.scReplies = []) : + PresRC (KR i t) (· = σ) (replyUnlinkSc (Z := Unit) sc r) := by + intro s hs x hx + subst hs + unfold replyUnlinkSc NonDet.liftM at hx + obtain ⟨⟨l, s1⟩, h1, hx1⟩ := mem_bindA.1 hx + obtain ⟨⟨y, s2⟩, h1a, h1b⟩ := mem_bindA.1 h1 + have e2 : s2 = s := (getSchedContext_eqPres sc s _ h1a).symm + obtain ⟨-, k, hk⟩ := getSchedContext_mem h1a + have e1 : (l, s1) = (y.scReplies, s2) := h1b + obtain ⟨el, es1⟩ := Prod.mk.inj e1 + rw [e2] at es1 + subst es1 + simp only at hx1 + obtain ⟨⟨rp, s3⟩, h3, hx2⟩ := mem_bindA.1 hx1 + have e3 : s1 = s3 := getReply_eqPres r s1 (rp, s3) h3 + subst e3 + simp only at hx2 + have upd : ∀ f : SchedContext → SchedContext, (∀ z, ∀ q ∈ (f z).scReplies, q ∈ z.scReplies) → + (sc = idleScPtr → (f y).scReplies = y.scReplies) → + (∀ z, (f z).scYieldFrom = z.scYieldFrom ∧ (f z).scRefillMax = z.scRefillMax ∧ (f z).scRefills = z.scRefills ∧ + (f z).scTcb = z.scTcb) → + ∀ v ∈ (updateSchedContext (Z := Unit) sc f s1).1, KR i t s1 v.2 := by + intro f hsub hid hz v hv + refine ⟨?_, updSc_repSub sc f hsub s1 v hv⟩ + obtain ⟨z, n, hz', e⟩ := updateSchedContext_memA sc f hv + have ez : z = y := by + rw [hk] at hz' + cases hz' + rfl + rw [e] + exact SIKA.upd hz' (.sc _ _ _ ⟨(hz z).1, (hz z).2.1, fun a => by rw [(hz z).2.2.1]; exact a, + fun a => ⟨(hz z).2.2.2, by rw [ez]; exact hid a⟩, Or.inl (hz z).2.2.2, fun a => by cases a⟩) + (idleRdy_scA hz' (hz z).2.2.1 (hz z).2.1) + have nil : sc = idleScPtr → y.scReplies = [] := fun e => hidle y k (e ▸ hk) + have sr : ∀ q v, PresR (KR i t) (setReplyObjRef (Z := Unit) replyScUpdate q v) := fun q v s z hz => by + refine ⟨setReplyObjRef_sikA _ _ _ (fun e => by cases e) s z hz, ?_⟩ + unfold setReplyObjRef updateSkObjRef at hz + obtain ⟨a, ha, hz⟩ := mem_bindA.1 hz + have ex : s = a.2 := getSimpleKo_eqPres _ q s a ha + rw [← ex] at hz + obtain ⟨-, -, hz2⟩ := setSimpleKo_memA _ _ _ hz + rw [hz2] + exact repSub_upd (by intro _ _ c; cases c) + split_ifs at hx2 with hh + · obtain ⟨u, hu, hx3⟩ := mem_bindA.1 hx2 + have eu : s1 = u.2 := (PresR.assert (R := @Eq (State Unit)) _) s1 u hu + rw [← eu] at hx3 + obtain ⟨v, hv, hx4⟩ := mem_bindA.1 hx3 + refine PreO.trans (upd (scRepliesUpdate List.tail) (fun z q a => List.mem_of_mem_tail a) (fun e => ?_) (fun _ => ⟨rfl, rfl, rfl, rfl⟩) v hv) ?_ + · show y.scReplies.tail = y.scReplies + rw [nil e]; rfl + refine (?_ : PresR (KR i t) _) v.2 x hx4 + repeat' (first + | exact sr _ _ + | exact PresR.pure _ + | refine PresR.bind ?_ (fun _ => ?_) + | split) + · obtain ⟨u, hu, hx3⟩ := mem_bindA.1 hx2 + have eu : s1 = u.2 := (PresR.assert (R := @Eq (State Unit)) _) s1 u hu + rw [← eu] at hx3 + refine upd (scRepliesUpdate (List.takeWhile fun q => decide (q ≠ r))) (fun z q a => (List.takeWhile_sublist _).subset a) (fun e => ?_) (fun _ => ⟨rfl, rfl, rfl, rfl⟩) x hx3 + show y.scReplies.takeWhile _ = y.scReplies + rw [nil e]; rfl + +/-- **`reply_remove`** of the caller `t` (not the idle thread): donating `SIKA`, shrinking reply stacks, and the +caller's SC afterwards is its own or an SC that had a reply. -/ +theorem replyRemove_keepA {σ σ1 : State Unit} {m : McState} {n : Nat} {r : ObjRef} (hi : σ.idleThread = i) + (hI : SIdleA σ m n) (ht : t ≠ i) {x : Tcb} (hkt : σ.kheap t = some (.tcb x)) + (h : ((), σ1) ∈ (replyRemove (Z := Unit) t r σ).1) : + SIKA i (· = t) (· = t) true σ σ1 ∧ RepSubA σ σ1 ∧ + ∀ x1 sc, σ1.kheap t = some (.tcb x1) → x1.tcbSchedContext = some sc → + (x.tcbSchedContext = some sc ∧ ∀ y1 k1, σ1.kheap sc = some (.schedContext y1 k1) → + ∃ y k, σ.kheap sc = some (.schedContext y k) ∧ y1.scTcb = y.scTcb) ∨ + ((∃ y k, σ.kheap sc = some (.schedContext y k) ∧ y.scReplies ≠ []) ∧ + ∀ y1 k1, σ1.kheap sc = some (.schedContext y1 k1) → y1.scTcb = some t) := by + have kept : ∀ {a b : State Unit} (K : TKeepA a b) {y : Tcb}, a.kheap t = some (.tcb y) → + ∀ z, b.kheap t = some (.tcb z) → z.tcbSchedContext = y.tcbSchedContext := by + intro a b K y hy z hz + obtain ⟨-, -, -, -, -, back, -⟩ := K + obtain ⟨y', hy', e, -⟩ := back t z hz + rw [hy] at hy' + cases hy' + exact e + have unl : ∀ {a b : State Unit}, SIKA i (Mv t) (Mv t) false a b → ∀ {y : Tcb}, a.kheap t = some (.tcb y) → + ∀ z, b.kheap t = some (.tcb z) → z.tcbSchedContext = y.tcbSchedContext := by + intro a b R y hy z hz + obtain ⟨z', hz', e⟩ := R.tcbScA hy + rw [hz] at hz' + cases hz' + exact e + have thS : ∀ {a b : State Unit}, SIKA i (Mv t) (Mv t) false a b → ScThA a b := by + intro a b R q y k hy + obtain ⟨z, hz, g⟩ := R.scB hy + exact ⟨z, hz, g.th.resolve_right fun c => by cases c.1⟩ + unfold replyRemove at h + obtain ⟨⟨rp, s1⟩, h1, h⟩ := mem_bindA.1 h + have e1 : σ = s1 := getReply_eqPres r σ _ h1 + subst e1 + simp only at h + obtain ⟨u, hu, h⟩ := mem_bindA.1 h + have eu : σ = u.2 := (PresR.assert (R := @Eq (State Unit)) _) σ u hu + rw [← eu] at h + obtain ⟨⟨o, s2⟩, h2, h⟩ := mem_bindA.1 h + have e2 : σ = s2 := (PresR.gets (R := @Eq (State Unit)) _) σ _ h2 + subst e2 + have eo : o = scWithReply r σ := congrArg Prod.fst (show (o, σ) = (scWithReply r σ, σ) from h2) + simp only at h + cases o with + | none => + dsimp only at h + have h' : ((), σ1) ∈ (replyUnlinkTcb (Z := Unit) t r σ).1 := h + have S : SIKA i (Mv t) (Mv t) false σ σ1 := replyUnlinkTcb_sikA (i := i) (M := Mv t) (N := Mv t) (don := false) t r rfl σ _ h' + refine ⟨S.up, RepSubA.of_sik S, fun x1 sc h1 h2 => Or.inl ⟨by rw [← unl S hkt x1 h1]; exact h2, fun y1 k1 hy => ?_⟩⟩ + obtain ⟨z, hz, e⟩ := thS S sc y1 k1 hy + exact ⟨z, k1, hz, e⟩ + | some p => + have hsw : scWithReply r σ = some p := eo.symm + dsimp only at h + obtain ⟨⟨l, s3⟩, h3, h⟩ := mem_bindA.1 h + unfold NonDet.liftM at h3 + obtain ⟨⟨y, s4⟩, h3a, h3b⟩ := mem_bindA.1 h3 + have e4 : s4 = σ := (getSchedContext_eqPres p σ _ h3a).symm + obtain ⟨-, k, hk⟩ := getSchedContext_mem h3a + obtain ⟨el, es3⟩ := Prod.mk.inj (show (l, s3) = (y.scReplies, s4) from h3b) + rw [e4] at es3 + rw [es3] at h + simp only at h + obtain ⟨⟨c, s5⟩, h5, h⟩ := mem_bindA.1 h + unfold getTcbObjRef at h5 + obtain ⟨ec, es5⟩ := Prod.mk.inj (threadGet_valA Tcb.tcbSchedContext hkt h5) + rw [es5] at h + simp only at h + obtain ⟨v, hv, h⟩ := mem_bindA.1 h + have V : KR i t σ v.2 := replyUnlinkSc_krA hI.f4 σ rfl v hv + have T : TKeepA σ v.2 := replyUnlinkSc_tpres p r σ v hv + have tv : ∃ z, v.2.kheap t = some (.tcb z) ∧ z.tcbSchedContext = x.tcbSchedContext := by + obtain ⟨-, -, -, -, -, -, fwd, -⟩ := T + exact fwd t x hkt + obtain ⟨z, hz, ez⟩ := tv + obtain ⟨w, hw, h⟩ := mem_bindA.1 h + unfold NonDet.when at hw + split_ifs at hw with hg + swap + · have ew : w = ((), v.2) := hw + rw [ew] at h + have S : SIKA i (Mv t) (Mv t) false v.2 σ1 := replyUnlinkTcb_sikA (i := i) (M := Mv t) (N := Mv t) (don := false) t r rfl v.2 _ h + refine ⟨V.1.trans S.up, PreO.trans V.2 (RepSubA.of_sik S), fun x1 sc h1 h2 => + Or.inl ⟨by rw [← ez, ← unl S hz x1 h1]; exact h2, fun y1 k1 hy => ?_⟩⟩ + obtain ⟨z1, hz1, e1⟩ := thS S sc y1 k1 hy + obtain ⟨z0, hz0, e0⟩ := replyUnlinkSc_thA p r σ v hv sc z1 k1 hz1 + exact ⟨z0, k1, hz0, e1.trans e0⟩ + have ne : y.scReplies ≠ [] := fun e => by + obtain ⟨y', n', hy', hm⟩ := scWithReply_memA hsw + rw [hk] at hy' + cases hy' + rw [e] at hm + cases hm + have hpi : p ≠ idleScPtr := fun e => ne (hI.f4 y k (e ▸ hk)) + have hth : y.scTcb ≠ some i := fun e => hpi (hI.f5 p y k hk (by rw [hi]; exact e)) + have pre : ∀ y' k', v.2.kheap p = some (.schedContext y' k') → y'.scTcb ≠ some i := by + intro y' k' e + obtain ⟨y0, e0, g⟩ := V.1.scB e + rw [hk] at e0 + cases e0 + rcases g.th with a | a + · rw [a]; exact hth + · exact a.2 + have D : KR i t v.2 w.2 := donate_krA (i := i) (t := t) hpi ht v.2 pre w hw + have B := donate_bindsA p t v.2 trivial w hw + have S : SIKA i (Mv t) (Mv t) false w.2 σ1 := replyUnlinkTcb_sikA (i := i) (M := Mv t) (N := Mv t) (don := false) t r rfl w.2 _ h + have TH := donate_thA p t v.2 trivial w hw + refine ⟨V.1.trans (D.1.trans S.up), PreO.trans V.2 (PreO.trans D.2 (RepSubA.of_sik S)), + fun x1 sc h1 h2 => Or.inr ?_⟩ + obtain ⟨zw, hzw, -⟩ := D.1.tcbF hz + have : x1.tcbSchedContext = some p := (unl S hzw x1 h1).trans (B zw hzw) + rw [this] at h2 + cases h2 + refine ⟨⟨y, k, hk, ne⟩, fun y1 k1 hy => ?_⟩ + obtain ⟨z1, hz1, e1⟩ := thS S p y1 k1 hy + exact e1.trans (TH z1 k1 hz1) + +/-! ## The tail's `postpone` -/ + +/-- What `postpone` of an SC bound to `t` changes: queues (the release queue gains at most `t`) and the reprogram +flag. -/ +structure PostQA (t : ObjRef) (σ σ' : State Unit) : Prop where + frame : σ' = { σ with readyQueues := σ'.readyQueues, releaseQueue := σ'.releaseQueue, + reprogramTimer := σ'.reprogramTimer } + rq : ∀ d p y, y ∈ σ'.readyQueues d p → y ∈ σ.readyQueues d p + rls : ∀ y ∈ σ'.releaseQueue, y = t ∨ y ∈ σ.releaseQueue + +theorem postpone_postqA {σ σ' : State Unit} {p t : ObjRef} {y : SchedContext} {k : Nat} + (hk : σ.kheap p = some (.schedContext y k)) (ht : y.scTcb = some t) + (h : ((), σ') ∈ (postpone (Z := Unit) p σ).1) : PostQA t σ σ' := by + unfold postpone at h + obtain ⟨⟨o, s1⟩, h1, h⟩ := mem_bindA.1 h + obtain ⟨hs, y', k', hk', hv⟩ := getScObjRef_mem h1 + simp only at hs hv h + rw [hs] at h + rw [hk] at hk' + cases hk' + rw [hv, ht] at h + obtain ⟨⟨tp, s2⟩, h2, h⟩ := mem_bindA.1 h + have e2 : (tp, s2) = (t, σ) := h2 + obtain ⟨e2a, e2b⟩ := Prod.mk.inj e2 + rw [e2a, e2b] at h + simp only at h + obtain ⟨a, ha, h⟩ := mem_bindA.1 h + have A : a.2.releaseQueue = σ.releaseQueue ∧ a.2.reprogramTimer = σ.reprogramTimer ∧ + a.2 = { σ with readyQueues := a.2.readyQueues } ∧ ∀ d p y, y ∈ a.2.readyQueues d p → y ∈ σ.readyQueues d p := by + unfold tcbSchedAction at ha + obtain ⟨d, -, ha⟩ := eqPres_mem (threadGet_eqPres _ t) ha + obtain ⟨pr, -, ha⟩ := eqPres_mem (threadGet_eqPres _ t) ha + have hq : PresR (@Eq (State Unit)) (getTcbQueue (Z := Unit) d pr) := by unfold getTcbQueue; eqpres + obtain ⟨q, hq', ha⟩ := eqPres_mem hq ha + have eq : q = σ.readyQueues d pr := by + unfold getTcbQueue at hq' + obtain ⟨z, hz, hq'⟩ := mem_bindA.1 hq' + have ez : z = (σ.readyQueues, σ) := hz + subst ez + exact congrArg Prod.fst (show (q, σ) = (σ.readyQueues d pr, σ) from hq') + obtain ⟨q', hq'', ha⟩ := mem_bindA.1 ha + have eq' : q' = (tcbSchedDequeue t q, σ) := hq'' + subst eq' + have er : a = ((), { σ with readyQueues := fun d' p => + if d' = d ∧ p = pr then tcbSchedDequeue t q else σ.readyQueues d' p }) := ha + rw [er] + refine ⟨rfl, rfl, rfl, fun d' p z hz => ?_⟩ + simp only at hz + split_ifs at hz with c + · rw [c.1, c.2, ← eq]; exact (List.mem_filter.1 hz).1 + · exact hz + obtain ⟨b, hb, h⟩ := mem_bindA.1 h + have B : ∃ q' bb, b.2 = { a.2 with releaseQueue := q', reprogramTimer := bb } ∧ + ∀ z ∈ q', z = t ∨ z ∈ a.2.releaseQueue := by + unfold tcbReleaseEnqueue at hb + obtain ⟨qs, hqs, hb⟩ := eqPres_mem (PresR.gets _) hb + have eqs : qs = a.2.releaseQueue := congrArg Prod.fst (show (qs, a.2) = (a.2.releaseQueue, a.2) from hqs) + obtain ⟨u1, h1, hb⟩ := mem_bindA.1 hb + unfold readyTimesAppend at h1 + obtain ⟨hσ1, hmem⟩ := orderedInsert_spec _ _ _ _ h1 + obtain ⟨u2, h2, hb⟩ := mem_bindA.1 hb + have e2 : u2 = ((), { u1.2 with releaseQueue := u1.1 }) := h2 + have e3 : ∃ bb, b.2 = { a.2 with releaseQueue := u1.1, reprogramTimer := bb } := by + unfold NonDet.when at hb + split_ifs at hb + · have : b = ((), { u2.2 with reprogramTimer := true }) := hb + refine ⟨true, ?_⟩ + rw [this, e2] + show ({ u1.2 with releaseQueue := u1.1, reprogramTimer := true } : State Unit) = _ + rw [hσ1] + · have : b = u2 := hb + refine ⟨a.2.reprogramTimer, ?_⟩ + rw [this, e2] + show ({ u1.2 with releaseQueue := u1.1 } : State Unit) = _ + rw [hσ1] + obtain ⟨bb, e3⟩ := e3 + exact ⟨u1.1, bb, e3, fun z hz => by rw [← eqs]; exact hmem z hz⟩ + obtain ⟨q', bb, eb, hm⟩ := B + have er : ((), σ') = ((), { b.2 with reprogramTimer := true }) := h + have es : σ' = { σ with readyQueues := a.2.readyQueues, releaseQueue := q', reprogramTimer := true } := by + rw [(Prod.mk.inj er).2, eb, A.2.2.1] + rw [es] + exact ⟨rfl, A.2.2.2, fun z hz => by rw [← A.1]; exact hm z hz⟩ + +theorem _root_.Fl4ProofsA64.P3.SInvXA.postq {σ σ' : State Unit} {m : McState} {n : Nat} {t : ObjRef} (h : SInvXA σ m n) + (Q : PostQA t σ σ') (ht : ∃ x, σ.kheap t = some (.tcb x) ∧ runnable x.tcbState = true) + (hz : ∀ x, σ.kheap t = some (.tcb x) → x.tcbSchedContext = some σ.curSc → σ.consumedTime = 0) : + SInvXA σ' m n := by + have e := Q.frame + have hcons : σ'.consumedTime = σ.consumedTime := by rw [e] + have htime : σ'.curTime = σ.curTime := by rw [e] + have hk : σ'.kheap = σ.kheap := by rw [e] + have hc : σ'.curThread = σ.curThread := by rw [e] + have hi : σ'.idleThread = σ.idleThread := by rw [e] + have hsc : σ'.curSc = σ.curSc := by rw [e] + have ha : σ'.schedulerAction = σ.schedulerAction := by rw [e] + refine + { mc := h.mc + view := by unfold McView; rw [hi]; exact h.view + curTcb := by rw [hk, hc]; exact h.curTcb + idleTcb := by rw [hk, hi]; exact h.idleTcb + onlyIdle := by rw [hk]; exact h.onlyIdle + idleSc := by rw [hk, hi]; exact h.idleSc + curSc := by rw [hk, hsc]; exact h.curSc + ctQ := fun a d p hm => by + rw [ha] at a + rw [hc] at hm + exact h.ctQ a d p (Q.rq _ _ _ hm) + idleQ := fun d p c hcn hm => h.idleQ d p c hcn (Q.rq _ _ _ hm) + rq := fun d p u hu => by rw [hk]; exact h.rq d p u (Q.rq _ _ _ hu) + rls := fun u hu => by + rw [hk] + rcases Q.rls u hu with a | a + · rw [a]; exact ht.imp fun _ h => h.1 + · exact h.rls u a + rpos := by rw [hk]; exact h.rpos + irqN := by rw [e]; exact ⟨h.irqN.node, h.irqN.slot, h.irqN.ref⟩ + zc := fun u x hx hs hu => by + rw [hk] at hx + rw [hsc] at hs + rw [hcons] + rcases Q.rls u hu with a | a + · subst a; exact hz x hx hs + · exact h.zc u x hx hs a + rlsRun := fun u hu => by + rw [hk] + rcases Q.rls u hu with a | a + · rw [a]; exact ht + · exact h.rlsRun u a + curScReady := by unfold CurRdyA; rw [hk, hsc, hcons, htime]; exact h.curScReady + faultSt := by unfold FaultStOkA; rw [hk]; exact h.faultSt } + +/-- (ruling 14:18Z item 2) **The postponed SC is not a charged current SC**: `postpone` runs only in the `else` branch +of `ready ∧ sufficient 0` (`ipc.rs:287-295`); were `t`'s SC the current one with time consumed, `CurRdyA` gives +`ready` and `hsuf` (`ScInvA.suff` at the arm) `sufficient 0`. -/ +theorem postq_zcA {σ : State Unit} {m : McState} {n : Nat} {t p0 : ObjRef} {x : Tcb} {y : SchedContext} {k : Nat} + (h : SInvXA σ m n) (hx : σ.kheap t = some (.tcb x)) (hp : x.tcbSchedContext = some p0) + (hk : σ.kheap p0 = some (.schedContext y k)) (act : ActTcbA σ t) + (hsuf : ∀ x p y k, σ.kheap t = some (.tcb x) → x.tcbSchedContext = some p → σ.kheap p = some (.schedContext y k) → + scActive y = true → fl4RefillSufficient 0 (refillHd y) = true) + (hr : ¬(refillReady σ.curTime (refillHd y) = true ∧ fl4RefillSufficient 0 (refillHd y) = true)) : + ∀ x', σ.kheap t = some (.tcb x') → x'.tcbSchedContext = some σ.curSc → σ.consumedTime = 0 := by + intro x' hx' hs + rw [hx] at hx'; cases hx' + obtain ⟨x1, p1, hx1, hp1, y1, k1, hy1, ha1⟩ := act + rw [hx] at hx1; cases hx1 + rw [hp] at hp1; cases hp1 + rw [hk] at hy1; cases hy1 + have e : p0 = σ.curSc := Option.some.inj (hp.symm.trans hs) + by_contra hc + exact hr ⟨h.curScReady hc y k (by rw [← e]; exact hk) ha1, hsuf x p0 y k hx hp hk ha1⟩ + +/-! ## The whole reply -/ + +/-- `set_thread_state` keeps the ready queues. -/ +theorem setThreadState_rqA {σ σ' : State Unit} {t : ObjRef} {ts : ThreadState} + (h : ((), σ') ∈ (setThreadState (Z := Unit) t ts σ).1) : σ'.readyQueues = σ.readyQueues := by + unfold Fl4SpecA64.setThreadState at h + obtain ⟨u, hu, h⟩ := mem_bindA.1 h + obtain ⟨x, -, e⟩ := threadSet_memA _ t hu + rw [setThreadStateAct_eq] at h + have : ((), σ') = ((), stsAct u.2 t) := h + rw [(Prod.mk.inj this).2] + unfold stsAct + split_ifs <;> rw [e] + +/-- `set_thread_state` keeps every TCB's domain and priority. -/ +theorem setThreadState_domA {σ σ' : State Unit} {t f : ObjRef} {ts : ThreadState} {z : Tcb} + (h : ((), σ') ∈ (setThreadState (Z := Unit) t ts σ).1) (hz : σ'.kheap f = some (.tcb z)) : + ∃ z0, σ.kheap f = some (.tcb z0) ∧ z0.tcbDomain = z.tcbDomain ∧ z0.tcbPriority = z.tcbPriority := by + unfold Fl4SpecA64.setThreadState at h + obtain ⟨u, hu, h⟩ := mem_bindA.1 h + obtain ⟨x, hx, e⟩ := threadSet_memA _ t hu + rw [setThreadStateAct_eq] at h + have : ((), σ') = ((), stsAct u.2 t) := h + have hk : σ'.kheap = u.2.kheap := by + rw [(Prod.mk.inj this).2] + unfold stsAct + split_ifs <;> rfl + rw [hk, e] at hz + by_cases hq : f = t + · subst hq + simp only [Function.update_self] at hz + cases hz + exact ⟨x, hx, rfl, rfl⟩ + · simp only [Function.update_of_ne hq] at hz + exact ⟨z, hz, rfl, rfl⟩ + +instance (t r : ObjRef) : QInst (replyRemove (Z := Unit) t r) := ⟨by unfold replyRemove replyUnlinkSc; qpres⟩ + +section +open fl4_core_a64 Fl4ProofsA64.P1 +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **The live Reply up to the SC tail**: donating `SIKA`, `SchedValidA` (as `FIA`) and the receiver's active SC. -/ +theorem replyLive_midA {th : TypedHeapA} {kb : U64} (hPl : PlaceA th kb) (sf : S) + {σ σ1 σ2 σ3 σ4 : State Unit} {m : McState} {n : Nat} {sender t r : U64} {grant : Bool} + {xt : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) (hst : xt.tcbState = .blockedOnReply (specPtr r)) + (hinv : SInvXA σ m n) (hI : SIdleA σ m n) (hV : SchedValidA σ) (hD : RqDomPrio σ) (hA : ActRepA σ) + (h1 : ((), σ1) ∈ (replyRemove (Z := Unit) (specPtr t) (specPtr r) σ).1) + (h2 : ((), σ2) ∈ ((getTcbObjRef (Z := Unit) Tcb.tcbSchedContext (specPtr t) >>= + ifSporadicCurScTestRefillUnblockCheck) σ1).1) + (hSt : StateRelA th (L.mem sf) σ2) (hSV : SlotsValidA th (L.mem sf)) (htr : th t = some .tcb) + {xs y2 : Tcb} (hks : σ2.kheap (specPtr sender) = some (.tcb xs)) + (hky : σ2.kheap (specPtr t) = some (.tcb y2)) (hfs : xs.tcbFault = none) + (hcaps : grant = false ∨ ipcBufOfA false xs = none ∨ + unat (dataToMessageInfo (xs.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0) + (hal : ∀ rb, ipcBufOfA true y2 = some rb → (bufAddr rb 0).toNat % 8 = 0) + (h3 : ((), σ3) ∈ (doIpcTransfer (Z := Unit) (specPtr sender) none (0 : Badge) grant (specPtr t) σ2).1) + (h4 : ((), σ4) ∈ (setThreadState (Z := Unit) (specPtr t) .running σ3).1) : + SIKA σ.idleThread (· = specPtr t) (· = specPtr t) true σ σ4 ∧ FIA σ4 ∧ BoundActive (specPtr t) σ4 := by + -- the idle thread + have hidl : ∀ c < n, m.idleThreadOf c ≠ specPtr t := fun c hc e => by + obtain ⟨x, hx, hs⟩ := hI.f3 c hc + rw [e, hkt] at hx + cases hx + rw [hst] at hs + cases hs + have hti : specPtr t ≠ σ.idleThread := fun e => by + obtain ⟨x, hx, hs⟩ := hinv.idleTcb + rw [← e, hkt] at hx + cases hx + rw [hst] at hs + cases hs + -- `reply_remove` + obtain ⟨S1, P1, hsc1⟩ := replyRemove_keepA (i := σ.idleThread) (t := specPtr t) rfl hI hti hkt h1 + have F1 : FIA σ1 := replyRemove_fia (specPtr t) (specPtr r) σ ⟨SchedValidA.toFIA hV, hD⟩ _ h1 + have B1 : BAs [specPtr t] σ1 := fun u hu x1 sc hx1 hs => by + obtain rfl := List.mem_singleton.1 hu + have act : ScActiveAtA σ sc := by + rcases hsc1 x1 sc hx1 hs with ⟨a, -⟩ | ⟨⟨y, k, hy, ne⟩, -⟩ + · exact (SchedValidA.toFIA hV).1.blk _ xt sc hkt (by rw [hst]; rfl) a + · exact ⟨y, k, hy, hA sc y k hy ne⟩ + obtain ⟨y, k, hy, a⟩ := act + obtain ⟨y', hy', g⟩ := S1.scF hy + exact ⟨x1, sc, hx1, hs, y', k, hy', by unfold scActive at a ⊢; rw [g.rm]; exact a⟩ + -- the refill test + have S2 : SIKA σ.idleThread (· = specPtr t) (· = specPtr t) false σ1 σ2 := + PresR.bind (eqPres_sikA (by unfold getTcbObjRef; exact threadGet_eqPres _ _)) + (fun o => refillCheck_sikA o) σ1 _ h2 + have F2 : FIA σ2 ∧ BAs [specPtr t] σ2 := by + obtain ⟨⟨o, s⟩, hg, hr⟩ := mem_bindA.1 h2 + have es : σ1 = s := (show PresR (@Eq (State Unit)) (getTcbObjRef (Z := Unit) Tcb.tcbSchedContext (specPtr t)) by + unfold getTcbObjRef; exact threadGet_eqPres _ _) σ1 _ hg + subst es + exact refillCheckT_fia o (BAs [specPtr t]) (fun _ _ k h => h.ofTKeep k) σ1 ⟨F1, B1⟩ _ hr + -- the transfer + have S3 : SIKA σ.idleThread (· = specPtr t) (· = specPtr t) false σ2 σ3 := + doIpcTransfer_sikA hPl sf hSt hSV htr hks hky hfs hal none 0 grant hcaps σ2 rfl _ h3 + have T3 : TKeepA σ2 σ3 := + (doIpcTransfer_tkA hPl sf hSt hSV htr hks hky hfs hal none 0 grant hcaps _ h3).1 + have F3 : FIA σ3 := F2.1.ofTKeep T3 + have B3 : BAs [specPtr t] σ3 := F2.2.ofTKeep T3 + -- `Running` + have S4 : SIKA σ.idleThread (· = specPtr t) (· = specPtr t) false σ3 σ4 := + setThreadState_sikA (i := σ.idleThread) (M := (· = specPtr t)) (N := (· = specPtr t)) (don := false) (specPtr t) rfl .running (fun c => by cases c) σ3 _ h4 + have F4 : FIA σ4 ∧ BAs [specPtr t] σ4 := + setThreadState_fia (specPtr t) .running rfl (BAs [specPtr t]) (fun σ p x x' hx h1 h => h.tcbUpd hx h1) + (fun σ a h => h.act a) σ3 ⟨F3, B3⟩ _ h4 + have S24 : SIKA σ.idleThread (· = specPtr t) (· = specPtr t) false σ1 σ4 := S2.trans (S3.trans S4) + have S14 : SIKA σ.idleThread (· = specPtr t) (· = specPtr t) true σ σ4 := S1.trans S24.up + exact ⟨S14, F4.1, F4.2 _ (List.mem_singleton_self _)⟩ + +/-- **The spec keeps of the live Reply** (module docstring). `σ1 .. σ4`: after `reply_remove`, the refill test, the +transfer and the `Running` write; `σ5`: after the tail. -/ +theorem replyLive_specKeepA {th : TypedHeapA} {kb : U64} (hPl : PlaceA th kb) (sf : S) + {σ σ1 σ2 σ3 σ4 σ5 : State Unit} {m : McState} {n : Nat} {sender t r : U64} {grant : Bool} + {xt : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) (hst : xt.tcbState = .blockedOnReply (specPtr r)) + (hto : ¬ ReplyTmoCondA σ4 (specPtr t) none) (hfr : xt.tcbFault = none) + (hback : ∀ sc, xt.tcbSchedContext = some sc → ∀ y k, σ.kheap sc = some (.schedContext y k) → + y.scTcb = some (specPtr t)) + (hinv : SInvXA σ m n) (hI : SIdleA σ m n) (hV : SchedValidA σ) (hD : RqDomPrio σ) (hA : ActRepA σ) + (hsw : ∀ u, σ.schedulerAction ≠ .switchThread u) (hnq : ∀ d p, specPtr t ∉ σ.readyQueues d p) + (hcur : specPtr t ≠ σ.curThread) + (h1 : ((), σ1) ∈ (replyRemove (Z := Unit) (specPtr t) (specPtr r) σ).1) + (h2 : ((), σ2) ∈ ((getTcbObjRef (Z := Unit) Tcb.tcbSchedContext (specPtr t) >>= + ifSporadicCurScTestRefillUnblockCheck) σ1).1) + (hSt : StateRelA th (L.mem sf) σ2) (hSV : SlotsValidA th (L.mem sf)) (htr : th t = some .tcb) + {xs y2 : Tcb} (hks : σ2.kheap (specPtr sender) = some (.tcb xs)) + (hky : σ2.kheap (specPtr t) = some (.tcb y2)) (hfs : xs.tcbFault = none) + (hcaps : grant = false ∨ ipcBufOfA false xs = none ∨ + unat (dataToMessageInfo (xs.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0) + (hal : ∀ rb, ipcBufOfA true y2 = some rb → (bufAddr rb 0).toNat % 8 = 0) + (h3 : ((), σ3) ∈ (doIpcTransfer (Z := Unit) (specPtr sender) none (0 : Badge) grant (specPtr t) σ2).1) + (hD3 : RqDomPrio σ3) + (h4 : ((), σ4) ∈ (setThreadState (Z := Unit) (specPtr t) .running σ3).1) + (hrun : ∀ z, σ4.kheap (specPtr t) = some (.tcb z) → z.tcbState = .running) + (hsuf4 : ∀ x p y k, σ4.kheap (specPtr t) = some (.tcb x) → x.tcbSchedContext = some p → + σ4.kheap p = some (.schedContext y k) → scActive y = true → fl4RefillSufficient 0 (refillHd y) = true) + (h5 : ((), σ5) ∈ (replyTailSpecA (specPtr t) none σ4).1) : + IpcKeepA σ σ5 ∧ SInvXA σ5 m n ∧ SIdleA σ5 m n ∧ SchedValidA σ5 ∧ QSendA σ σ5 ∧ RepSubA σ σ5 ∧ + UFrA (BufBytesA (ipcBufOfA true y2)) σ σ5 ∧ σ5.kheap = σ4.kheap ∧ + σ5.machineState.underlyingMemory = σ4.machineState.underlyingMemory ∧ ReplyExitA (specPtr t) (specPtr r) σ σ4 σ5 := by + -- the idle thread + have hidl : ∀ c < n, m.idleThreadOf c ≠ specPtr t := fun c hc e => by + obtain ⟨x, hx, hs⟩ := hI.f3 c hc + rw [e, hkt] at hx + cases hx + rw [hst] at hs + cases hs + have hti : specPtr t ≠ σ.idleThread := fun e => by + obtain ⟨x, hx, hs⟩ := hinv.idleTcb + rw [← e, hkt] at hx + cases hx + rw [hst] at hs + cases hs + have mid := replyLive_midA hPl sf hkt hst hinv hI hV hD hA h1 h2 hSt hSV htr hks hky hfs hcaps hal h3 h4 + obtain ⟨S14, F4a, B4⟩ := mid + obtain ⟨S1, P1, hsc1⟩ := replyRemove_keepA (i := σ.idleThread) (t := specPtr t) rfl hI hti hkt h1 + have S2 : SIKA σ.idleThread (· = specPtr t) (· = specPtr t) false σ1 σ2 := + PresR.bind (eqPres_sikA (by unfold getTcbObjRef; exact threadGet_eqPres _ _)) + (fun o => refillCheck_sikA o) σ1 _ h2 + have S3 : SIKA σ.idleThread (· = specPtr t) (· = specPtr t) false σ2 σ3 := + doIpcTransfer_sikA hPl sf hSt hSV htr hks hky hfs hal none 0 grant hcaps σ2 rfl _ h3 + have S4 : SIKA σ.idleThread (· = specPtr t) (· = specPtr t) false σ3 σ4 := + setThreadState_sikA (i := σ.idleThread) (M := (· = specPtr t)) (N := (· = specPtr t)) (don := false) (specPtr t) rfl .running (fun c => by cases c) σ3 _ h4 + have S24 : SIKA σ.idleThread (· = specPtr t) (· = specPtr t) false σ1 σ4 := S2.trans (S3.trans S4) + have hqs : ∀ y, y = specPtr t → ∀ d p, y ∉ σ.readyQueues d p := fun y e => e ▸ hnq + have hN' : ∀ y, y = specPtr t → ∃ z, σ.kheap y = some (.tcb z) := fun y e => e ▸ ⟨xt, hkt⟩ + have hMrls : ∀ y, y = specPtr t → y ∉ σ.releaseQueue := fun y e hr => by + obtain ⟨x, hx, hrn⟩ := hinv.rlsRun y hr + rw [e, hkt] at hx; cases hx + rw [hst] at hrn; cases hrn + have sinv : ∀ σ', SIKA σ.idleThread (· = specPtr t) (· = specPtr t) true σ σ' → σ'.kheap = σ4.kheap → + SInvXA σ' m n := fun σ' R hk => + R.sinv hinv (fun c hc e => hidl c hc e) (fun e => hti e.symm) hqs + (fun y e => ⟨e ▸ hcur, e ▸ hti, fun c hc a => hidl c hc (a.trans e), fun z hz => by + rw [e, hk] at hz + rw [hrun z hz]; rfl⟩) hN' hsw hMrls + (hI.rdyA hinv) + (fun p e x y hx hy hf => by + obtain ⟨x0, hx0, g⟩ := R.tcbB hy + rw [e, hkt] at hx0; cases hx0 + exact absurd (g.fault.trans hfr) hf) + have sidle4 : SIdleA σ4 m n := S14.sidle hI (fun c hc e => hidl c hc e) (hI.rdyA hinv) + -- the receiver's SC and handler at the tail + obtain ⟨x4, hx4, g4⟩ := S14.tcbF hkt + have link : ∀ p y k, x4.tcbSchedContext = some p → σ4.kheap p = some (.schedContext y k) → + y.scTcb = some (specPtr t) := by + intro p y4 k4 hp hy4 + obtain ⟨x1, hx1, -⟩ := S1.tcbF hkt + have e4 : x4.tcbSchedContext = x1.tcbSchedContext := by + obtain ⟨z, hz, e⟩ := S24.tcbScA hx1 + rw [hx4] at hz + cases hz + exact e + obtain ⟨y1, hy1, g⟩ := S24.scB hy4 + have th : y4.scTcb = y1.scTcb := g.th.resolve_right fun a => by cases a.1 + rw [th] + rcases hsc1 x1 p hx1 (e4 ▸ hp) with ⟨a, b⟩ | ⟨-, b⟩ + · obtain ⟨y0, k0, hy0, e0⟩ := b y1 k4 hy1 + rw [e0] + exact hback p a y0 k0 hy0 + · exact b y1 k4 hy1 + -- the tail + have tail : σ5.kheap = σ4.kheap ∧ σ5.idleThread = σ4.idleThread ∧ σ5.curTime = σ4.curTime ∧ + SInvXA σ5 m n ∧ SchedValidA σ5 ∧ QKeepA σ4 σ5 ∧ SchedFrameA σ4 σ5 ∧ CsLmA σ4 σ5 ∧ + ReplyTailXA (specPtr t) σ4 σ5 := by + have refl : σ5 = σ4 → _ := fun e => (show σ5.kheap = σ4.kheap ∧ σ5.idleThread = σ4.idleThread ∧ + σ5.curTime = σ4.curTime ∧ SInvXA σ5 m n ∧ SchedValidA σ5 ∧ QKeepA σ4 σ5 ∧ SchedFrameA σ4 σ5 ∧ + CsLmA σ4 σ5 ∧ ReplyTailXA (specPtr t) σ4 σ5 by + rw [e] + exact ⟨rfl, rfl, rfl, sinv σ4 S14 rfl, F4a.1.toA F4a.2, QKeepA.refl _, PreO.refl _, PreO.refl _, + ReplyTailXA.refl _ _⟩) + unfold replyTailSpecA at h5 + obtain ⟨⟨o, s1⟩, a1, h5⟩ := mem_bindA.1 h5 + unfold getTcbObjRef at a1 + obtain ⟨eo, es1⟩ := Prod.mk.inj (threadGet_valA Tcb.tcbSchedContext hx4 a1) + rw [es1] at h5 + simp only at h5 + obtain ⟨⟨ts, s2⟩, a2, h5⟩ := mem_bindA.1 h5 + unfold getThreadState at a2 + obtain ⟨etst, es2⟩ := Prod.mk.inj (threadGet_valA Tcb.tcbState hx4 a2) + rw [es2] at h5 + simp only at h5 + unfold NonDet.when at h5 + split_ifs at h5 with hg + swap + · exact refl (Prod.mk.inj (show ((), σ5) = ((), σ4) from h5)).2 + cases o with + | none => exact absurd rfl hg.1 + | some p0 => + obtain ⟨⟨p, s3⟩, a3, h6⟩ := mem_bindA.1 h5 + have e3 : (p, s3) = (p0, σ4) := a3 + obtain ⟨e3a, e3b⟩ := Prod.mk.inj e3 + rw [e3a, e3b] at h6 + simp only at h6 + obtain ⟨⟨y, s4⟩, a4, h7⟩ := mem_bindA.1 h6 + have e4 : s4 = σ4 := (getSchedContext_eqPres p0 σ4 _ a4).symm + obtain ⟨-, k, hk⟩ := getSchedContext_mem a4 + rw [e4] at h7 + simp only at h7 + obtain ⟨⟨now, s5⟩, a5, h8⟩ := mem_bindA.1 h7 + have e5 : s5 = σ4 := ((PresR.gets (R := @Eq (State Unit)) _) σ4 _ a5).symm + have enow : now = σ4.curTime := (Prod.mk.inj (show (now, s5) = (σ4.curTime, σ4) from a5)).1 + rw [e5] at h8 + simp only at h8 + split_ifs at h8 with hr + · have S5 : SIKA σ.idleThread (· = specPtr t) (· = specPtr t) false σ4 σ5 := + possibleSwitchTo_sikA (i := σ.idleThread) (M := (· = specPtr t)) (N := (· = specPtr t)) (don := false) (specPtr t) rfl σ4 _ h8 + have Q5 : QKeepA σ4 σ5 := (inferInstance : QInst (possibleSwitchTo (Z := Unit) (specPtr t))).pres σ4 _ h8 + have M5 : SchedFrameA σ4 σ5 := SPresInst.pres σ4 _ h8 + have hk5 : σ5.kheap = σ4.kheap := by + obtain ⟨q, -⟩ := possibleSwitchTo_memA (specPtr t) h8 + have e : σ5 = _ := q.1 + rw [e] + have fr := S5.frame + have X5 : ReplyTailXA (specPtr t) σ4 σ5 := by + obtain ⟨q, -⟩ := possibleSwitchTo_memA (specPtr t) h8 + have e : σ5 = { σ4 with readyQueues := σ5.readyQueues, schedulerAction := σ5.schedulerAction } := q.1 + refine ⟨?_, fun ns d p y hy => ?_, fun y hy => Or.inr (S5.rls y hy), + S5.act.imp_right (Or.imp_right fun ⟨_, c, a⟩ => by rw [← c]; exact a), fun ns => ?_⟩ + · refine e.trans ?_ + rw [show σ5.releaseQueue = σ4.releaseQueue by rw [e], show σ5.reprogramTimer = σ4.reprogramTimer by rw [e]] + · rcases S5.rq d p y hy with a | a | a + exacts [Or.inl a, Or.inr a, absurd a (ns y)] + · exact possibleSwitchTo_swRunA (specPtr t) σ4 + ⟨fun u c => absurd c (ns u), fun z hz => by rw [hrun z hz]; rfl, B4⟩ _ h8 + refine ⟨hk5, by rw [fr], by rw [fr], sinv σ5 (S14.trans S5.up) hk5, ?_, Q5, M5, + csLm_of_kpA (possibleSwitchTo (Z := Unit) (specPtr t)) h8, X5⟩ + have F5 := possibleSwitchTo_fia' (specPtr t) (fun _ => True) (fun _ _ h => h) (fun _ _ h => h) σ4 + ⟨F4a, B4, trivial⟩ _ h8 + exact F5.1.1.toA F5.1.2 + · unfold replyLateSpecA at h8 + obtain ⟨⟨tc, s6⟩, a6, h9⟩ := mem_bindA.1 h8 + have e6 : (tc, s6) = (x4, σ4) := by + have hg4 : getTcb (specPtr t) σ4 = some x4 := by simp [getTcb, readObject, hx4] + have := a6 + unfold NonDet.getsThe at this + rw [hg4] at this + exact this + obtain ⟨e6a, e6b⟩ := Prod.mk.inj e6 + rw [e6a, e6b] at h9 + simp only at h9 + rw [if_neg (fun hc => hto ⟨x4, p0, y, k, hx4, eo.symm, etst ▸ hg.2, hk, enow ▸ hr, hc.1, hc.2⟩)] at h9 + have hlink : y.scTcb = some (specPtr t) := link p0 y k eo.symm hk + have PQ : PostQA (specPtr t) σ4 σ5 := postpone_postqA hk hlink h9 + have fr := PQ.frame + have hk5 : σ5.kheap = σ4.kheap := by rw [fr] + refine ⟨hk5, by rw [fr], by rw [fr], (sinv σ4 S14 rfl).postq PQ ⟨x4, hx4, etst ▸ hg.2⟩ + (postq_zcA (sinv σ4 S14 rfl) hx4 eo.symm hk (B4 x4 p0 hx4 eo.symm) hsuf4 (by rw [← enow]; exact hr)), ?_, + (inferInstance : QInst (postpone (Z := Unit) p0)).pres σ4 _ h9, SPresInst.pres σ4 _ h9, + csLm_of_kpA (postpone (Z := Unit) p0) h9, ?xx⟩ + case xx => + have ea : σ5.schedulerAction = σ4.schedulerAction := by rw [PQ.frame] + refine ⟨?_, fun _ d p y hy => Or.inl (PQ.rq d p y hy), PQ.rls, Or.inl ea, + fun ns u c => absurd (ea ▸ c) (ns u)⟩ + refine PQ.frame.trans ?_ + rw [ea] + have hD4 : RqDomPrio σ4 := by + intro f d q z hz hm + rw [setThreadState_rqA h4] at hm + obtain ⟨z0, hz0, ed, ep⟩ := setThreadState_domA h4 hz + have := hD3 f d q z0 hz0 hm + exact ⟨this.1.trans ed, this.2.trans ep⟩ + have act : ActTcbA σ4 (specPtr t) := B4 x4 p0 hx4 eo.symm + have P5 := postpone_fia p0 (fun _ => True) (fun _ _ _ _ _ => trivial) σ4 + ⟨F4a, hD4, trivial, fun z kz u hz hu => by + rw [hk] at hz + cases hz + rw [hlink] at hu + cases hu + exact act, fun z kz u hz hu => by + rw [hk] at hz + cases hz + rw [hlink] at hu + cases hu + exact Or.inl (by + have : σ4.curThread = σ.curThread := by rw [S14.frame] + rw [this]; exact Ne.symm hcur)⟩ _ h9 + exact P5.1.1.toA P5.1.2 + obtain ⟨hk5, hi5, ht5, sinv5, valid5, Q5, M5, C5, X5⟩ := tail + have K : IpcKeepA σ σ5 := by + have K4 := S14.ipcKeep + exact ⟨fun p x h => by rw [hk5]; exact K4.tcb p x h, fun p y k h => by rw [hk5]; exact K4.scF p y k h, + fun p y k h => K4.scB p y k (by rw [← hk5]; exact h)⟩ + have exit : ReplyExitA (specPtr t) (specPtr r) σ σ4 σ5 := by + have c1 : CsLmA σ σ1 := csLm_of_kpA (replyRemove (Z := Unit) (specPtr t) (specPtr r)) h1 + have c2 : CsLmA σ1 σ2 := BKeepA.toCsLm ((show KPresB (getTcbObjRef (Z := Unit) Tcb.tcbSchedContext (specPtr t) >>= + ifSporadicCurScTestRefillUnblockCheck) by unfold ifSporadicCurScTestRefillUnblockCheck; kbres) σ1 _ h2) + have c3 : CsLmA σ2 σ3 := + doIpcTransfer_normal_exA hPl sf hSt hSV htr hks hky hfs hal none 0 grant hcaps _ h3 + have c4 : CsLmA σ3 σ4 := csLm_of_kpA (setThreadState (Z := Unit) (specPtr t) .running) h4 + have c : CsLmA σ σ5 := PreO.trans c1 (PreO.trans c2 (PreO.trans c3 (PreO.trans c4 C5))) + refine ⟨c.1, c.2, X5, fun hx p x h => ?_, ⟨σ1, h1, fun p x h => ?_⟩⟩ + have a1 : AllScA σ σ1 := replyRemove_keepScA (i := σ.idleThread) hkt (hx xt hkt) h1 + obtain ⟨y, hy, e⟩ := AllScA.trans a1 (AllScA.of_sik S24) p x h + exact ⟨y, by rw [hk5]; exact hy, e⟩ + obtain ⟨y, hy, e⟩ := AllScA.of_sik S24 p x h + exact ⟨y, by rw [hk5]; exact hy, e⟩ + refine ⟨K, sinv5, sidle4.congr hk5 hi5 (by rw [ht5]; exact BitVec.le_refl _), valid5, ?_, ?_, ?_, hk5, M5.2.2, exit⟩ + · have Q1 : QKeepA σ σ1 := (inferInstance : QInst (replyRemove (Z := Unit) (specPtr t) (specPtr r))).pres σ _ h1 + have Q2 : QKeepA σ1 σ2 := by + refine (show PresR QKeepA (getTcbObjRef (Z := Unit) Tcb.tcbSchedContext (specPtr t) >>= + ifSporadicCurScTestRefillUnblockCheck) from ?_) σ1 _ h2 + unfold ifSporadicCurScTestRefillUnblockCheck; qpres + have Q3 : QKeepA σ2 σ3 := + doIpcTransfer_any_qA hPl sf hSt hSV htr hks hky hal none 0 grant (Or.inl ⟨hfs, hcaps⟩) σ2 rfl _ h3 + have Q4 : QKeepA σ3 σ4 := (inferInstance : QInst (setThreadState (Z := Unit) (specPtr t) .running)).pres σ3 _ h4 + exact (Q1.trans (Q2.trans (Q3.trans (Q4.trans Q5)))).toSend + · exact PreO.trans P1 (PreO.trans (RepSubA.of_sik S24) (RepSubA.of_kheap hk5)) + · have U1 : UFrA (BufBytesA (ipcBufOfA true y2)) σ σ1 := UFrA.of_fr (SPresInst.pres σ _ h1) + have U2 : UFrA (BufBytesA (ipcBufOfA true y2)) σ1 σ2 := by + refine UFrA.of_fr ((show SPresA (getTcbObjRef (Z := Unit) Tcb.tcbSchedContext (specPtr t) >>= + ifSporadicCurScTestRefillUnblockCheck) from ?_) σ1 _ h2) + exact SPresInst.pres + have U3 : UFrA (BufBytesA (ipcBufOfA true y2)) σ2 σ3 := + doIpcTransfer_ufrA hPl sf hSt hSV htr hks hky hfs hal none 0 grant hcaps σ2 rfl _ h3 + exact PreO.trans U1 (PreO.trans U2 (PreO.trans U3 (PreO.trans (UFrA.of_fr (SPresInst.pres σ3 _ h4)) + (UFrA.of_fr M5)))) + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3ReplyLiveKeepA.lean b/proofs/Fl4ProofsA64/P3/Stage3ReplyLiveKeepA.lean new file mode 100644 index 000000000..6d4598561 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ReplyLiveKeepA.lean @@ -0,0 +1,599 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2b): **the live Reply joint with the record's exports**, the sibling of +`Endp.do_reply_transfer_fullA` for a fault-free receiver, a fault-free sender without an extra-cap insert, and a +receiver whose timeout handler is no endpoint cap. + +* **`do_reply_transfer_keepA`**: besides `IpcSchedRelA` and the (narrow, `ReplyFootW`) write set it exports + `EpStateRel`, `ReplyStackRelA` (over a stack contained in the old one) and the spec keeps `IpcKeepA`, `SInvXA`, + `SIdleA`, `SchedValidA`, `QSendA` and the user-memory frame. + +Premises: those of `do_reply_transfer_jointA` ([S] `hlocal`, `unb`; [P] `hmb`), the tail locality `tailS` (stated at +the reached state, with the spec keeps and the write set), the case selectors `hfs`, `hcaps`, `hfr`, `hto`, and record +facts at the entry (`hback`, `hinv`, `hI`, `hSV`, `hA`, `hsw`, `hnq`, `hcur`, `hal`). New module. +-/ +import Fl4ProofsA64.P3.Stage3ReplyKeepSpecA +import Fl4ProofsA64.P3.Stage3RqDomPrioA +import Fl4ProofsA64.P3.Stage3ReplyRemQA +import Fl4ProofsA64.P3.Stage3ReplyTmoMidA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +private theorem reachedRA {α : Type} {r : Result α} {P : α → Prop} (step : r ⦃ P ⦄) : + r ⦃ v => r = ok v ∧ P v ⦄ := by + obtain ⟨v, eq, post⟩ := spec_ex_fd step + rw [eq, WP.spec_ok] + exact ⟨rfl, post⟩ + +section Store +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + +/-- The `Running` write of an `Inactive` thread keeps the endpoint relation. -/ +theorem epRel_runningA {s s' : S} {σ σ' : State Unit} (H : SchedSRN st Z m κ s σ) + (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) (E0 : EpStateRel Z.T E R Bf (L.mem s) σ) + {t : U64} (ht : t ∈ Z.T) {x : Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) (hx : x.tcbState = .inactive) + (hkh : σ'.kheap = Function.update σ.kheap (specPtr t) (some (.tcb { x with tcbState := .running }))) + (hm : σ'.machineState = σ.machineState) (hst : (L.mem s' (stA t)).val = tsCode ThreadState.running) + (w : WritesIn st s s' (fun b => b = stA t ∨ b = L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION)) : + EpStateRel Z.T E R Bf (L.mem s') σ' := by + have hT : TcbRelE R (L.mem s) t x := by + obtain ⟨y, hy, h⟩ := E0.tcb t ht + rw [hk] at hy + cases hy + exact h + have h0 : (L.mem s (stA t)).val = 0 := by rw [hT.st, hx]; rfl + have nq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue := by + intro e he z hz hmem + obtain ⟨z', hz', hr⟩ := E0.ep e he + rw [hz] at hz' + cases hz' + have key : ∀ (xs : List U64) (k : Nat), k ≠ 0 → (∀ y ∈ xs, QueuedOn (L.mem s) e k y) → + specPtr t ∈ xs.map specPtr → False := by + intro xs k hk0 hq hmm + obtain ⟨y, hy, ey⟩ := List.mem_map.1 hmm + have := (hq y hy).1 + rw [specPtr_inj ey, h0] at this + exact hk0 this.symm + cases z with + | idleEP => cases hmem + | sendEP q => + obtain ⟨-, xs, -, eq, -, hq⟩ := hr + rw [Endpoint.epQueue, eq] at hmem + exact key xs 4 (by omega) hq hmem + | recvEP q => + obtain ⟨-, xs, -, eq, -, hq⟩ := hr + rw [Endpoint.epQueue, eq] at hmem + exact key xs 3 (by omega) hq hmem + have node : ∀ b, EFoot Z.T E R Bf b → b ≠ L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION := by + intro b hb e + refine endpFootOffWrite H P hb ?_ + rw [e] + exact Or.inr (Or.inl (H.node_sks H.sch.cpuCs (by simp [layout.node.SCHED_ACTION]))) + have hA : Agree (L.mem s) (L.mem s') (fun b => EFoot Z.T E R Bf b ∧ ¬ TF t b) := by + intro b ha hb + refine w.2.2 b ha fun h => ?_ + rcases h with h | h + · refine hb.2 ?_ + rw [h] + exact (a_st (PE.tcb t ht)).2 + · exact node b hb.1 h + have hT' : TcbRelE R (L.mem s') t { x with tcbState := .running } := by + refine TcbRelE.with_state (PE.tcb t ht) hT .running (by decide) hst fun b ha hb => w.2.2 b ha fun h => ?_ + rcases h with h | h + · exact hb.2 (by rw [h]) + · exact node b (Or.inr (Or.inr (Or.inl ⟨t, ht, Or.inl hb.1⟩))) h + have U := EpStateRel.upd_tcb E0 PE ht _ hA (Or.inl nq) hT' + refine U.frame PE (fun _ _ _ => rfl) ⟨fun p y h => ?_, fun p y h => ⟨y, ?_, rfl⟩, + fun p y h => ⟨y, ?_, rfl, rfl, rfl, rfl, rfl⟩, ?_⟩ + · rw [hkh]; exact h + · rw [hkh]; exact h + · rw [hkh]; exact h + · rw [hm]; rfl + +set_option maxHeartbeats 1000000 in +/-- **The live Reply with the record's exports, the timeout send handed to `tmo`** (module docstring). -/ +theorem do_reply_transfer_keepTA {s : S} {a : A} {σ : State Unit} {stack : U64 → List U64} {n : Nat} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {sender r t : U64} {grant : Bool} (hr : r ∈ R) (hs : sender ∈ Z.T) (ht : t ∈ Z.T) + (hne : sender ≠ t) + {xt xs : Tcb} {xr : Reply} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hkr : σ.kheap (specPtr r) = some (.reply xr)) (hb : xr.replyTcb = some (specPtr t)) + (hst : xt.tcbState = .blockedOnReply (specPtr r)) + (hks : σ.kheap (specPtr sender) = some (.tcb xs)) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) + (cov : ScReplyCovA Z.C m σ (specPtr r)) + (hsc0 : ∀ sc ∈ Z.C, (stack sc).head? = some r → sc ≠ 0#u64) + (hlocal : ∀ sc ∈ Z.C, (stack sc).head? = some r → xt.tcbSchedContext = none → + ReplyPopLocalQA (st := st) (ar := ar) (Z := Z) (m := m) a s σ sc t r) + (unb : ∀ s1 σ1 Q1 R1, SchedSRA st { Z with Q := Q1, R := R1 } m κ s1 σ1 → RemKeepA σ σ1 → + replyRemove (Z := Unit) (specPtr t) (specPtr r) σ = ({((), σ1)}, False) → + (L.mem s1 (scA t) ≠ 0#u64 → L.mem s1 (scCoreA (L.mem s1 (scA t))) = L.cpu s1) ∧ + (specPtr t ∈ σ1.releaseQueue → ∀ y n, + σ1.kheap (scNameA m (L.mem s1 (scA t))) = some (.schedContext y n) → + refillReady σ1.curTime (refillHd y) = false)) + (hoff : ∀ y, IsSlotA Z.th y → ¬ EWrite Z.T E R Bf y ∧ ¬ EWrite Z.T E R Bf (wa y 8#u64)) + (hSt : StateRelA Z.th (L.mem s) σ) (hV : SlotsValidA Z.th (L.mem s)) + (hC : BufCover st Z.T Bf s σ) (hNative : BufNativeA Z.T (L.mem s)) + (hfs : xs.tcbFault = none) + (hcaps : grant = false ∨ ipcBufOfA false xs = none ∨ + unat (dataToMessageInfo (xs.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0) + (hu : ∀ rp, ipcBufOfA true xt = some rp → ∀ k, k < 128 → inUserFrame (bufAddr rp k) σ) + (hal : ∀ rb, ipcBufOfA true xt = some rb → (bufAddr rb 0).toNat % 8 = 0) + (hag : fl4MinBudget = MIN_BUDGET) (hmb : ∀ a', Fl4MinBudgetAgreeA ar a') + (hidle : ∀ c < fl4A64Platform.numNodes, specPtr t ≠ m.idleThreadOf c) + (hfr : xt.tcbFault = none) + (hback : ∀ sc, xt.tcbSchedContext = some sc → ∀ y k, σ.kheap sc = some (.schedContext y k) → + y.scTcb = some (specPtr t)) + (hinv : SInvXA σ m n) (hI : SIdleA σ m n) (hSV : SchedValidA σ) (hA : ActRepA σ) + (hsw : ∀ u, σ.schedulerAction ≠ .switchThread u) (hnq : ∀ d p, specPtr t ∉ σ.readyQueues d p) + (hcur : specPtr t ≠ σ.curThread) (hsact : SActA σ) + (tailS : ∀ (s4 : S) (σ4 : State Unit) Q4 R4 (x : Tcb), + SchedSRB st { Z with Q := Q4, R := R4 } m κ s4 σ4 (specPtr t) → + σ4.kheap (specPtr t) = some (.tcb x) → BoundActive (specPtr t) σ4 → + SIKA σ.idleThread (· = specPtr t) (· = specPtr t) true σ σ4 → + WritesIn st s s4 (ReplyFootW (st := st) Z m σ s E R Bf) → + L.mem s4 (affA t) = L.cpu s4 ∧ + (L.mem s4 (scA t) ≠ 0#u64 → L.mem s4 (scCoreA (L.mem s4 (scA t))) = L.cpu s4) ∧ + (∀ sc y n, L.mem s4 (scA t) = sc → sc ≠ 0#u64 → + σ4.kheap (scNameA m sc) = some (.schedContext y n) → scActive y = true) ∧ + faults.valid_timeout_handler st s4 t = ok (isEpCap x.tcbTimeoutHandler)) + {RT : S → A → State Unit → Prop} + (tmo : ∀ (s4 : S) (σ4 : State Unit) Q4 R4 stack4 (x : Tcb), + ReplyTmoMidA st Z m κ E R Bf s σ σ t (ReplyNwA (st := st) Z m σ s R r t Bf) s4 σ4 Q4 R4 stack4 x → + ∀ sc y k (badge : U64), L.mem s4 (scA t) = sc → sc ≠ 0#u64 → + σ4.kheap (scNameA m sc) = some (.schedContext y k) → badge.bv = y.scBadge → + isEpCap x.tcbTimeoutHandler = true → + faults.handle_timeout st ar s4 a t (.Timeout badge) ⦃ o => + ∃ σ', ((), σ') ∈ (fl4HandleTimeout (Z := Unit) (specPtr t) (.timeout badge.bv) σ4).1 ∧ RT o.1 o.2 σ' ⦄) : + ipc.do_reply_transfer st ar s a sender r grant ⦃ out => ∃ σ5, + ((), σ5) ∈ (fl4DoReplyTransfer (Z := Unit) (specPtr sender) (specPtr r) grant σ).1 ∧ ((∃ stack', out.2 = a ∧ + IpcSchedRelA st Z m κ out.1 σ5 ∧ + WritesIn st s out.1 (ReplyFootW (st := st) Z m σ s E R Bf) ∧ SSzA σ σ5 ∧ + EpStateRel Z.T E R Bf (L.mem out.1) σ5 ∧ + ReplyStackRelA (L.mem out.1) R Z.C σ5 (scNameA m) stack' ∧ + (∀ c ∈ Z.C, ∀ q ∈ stack' c, q ∈ stack c) ∧ + IpcKeepA σ σ5 ∧ SInvXA σ5 m n ∧ SIdleA σ5 m n ∧ SchedValidA σ5 ∧ QSendA σ σ5 ∧ + (∃ y2 : Tcb, y2.tcbIpcframe = xt.tcbIpcframe ∧ y2.tcbIpcBuffer = xt.tcbIpcBuffer ∧ + UFrA (BufBytesA (ipcBufOfA true y2)) σ σ5) ∧ + WritesIn st s out.1 (ReplyNwA (st := st) Z m σ s R r t Bf) ∧ + (∃ σ4, SIKA σ.idleThread (· = specPtr t) (· = specPtr t) true σ σ4 ∧ σ5.kheap = σ4.kheap ∧ + EpStateRel Z.T E R Bf (L.mem out.1) σ4 ∧ + (∀ x, σ4.kheap (specPtr t) = some (.tcb x) → x.tcbState = .running) ∧ + (∀ u, σ4.schedulerAction ≠ .switchThread u) ∧ ReplyExitA (specPtr t) (specPtr r) σ σ4 σ5) ∧ RepEndA σ σ5) ∨ RT out.1 out.2 σ5) ⦄ := by + have hrt : xr.replyTcb = optRef (L.mem s (Endp.rTcbA r)) := by + obtain ⟨x, hx, h⟩ := HE.reply r hr + rw [hkr] at hx + cases hx + exact h + obtain ⟨hw0, hwt⟩ := optRef_some (hrt.symm.trans hb) + have hw : L.mem s (Endp.rTcbA r) = t := specPtr_inj hwt + have htr : obj.Reply.tcb st r s = ok t := + (eq_of_spec_fd (obj.Reply.tcb.spec (st := st) s r)).trans (by rw [hw]) + have hn : t ≠ 0#u64 := hw ▸ hw0 + have hrel : TcbRelE R (L.mem s) t xt := by + obtain ⟨x, hx, h⟩ := HE.tcb t ht + rw [hkt] at hx + cases hx + exact h + have hsr : obj.Tcb.state st t s = ok sched.TS_BLOCKED_ON_REPLY := by + have hsw' : L.mem s (stA t) = sched.TS_BLOCKED_ON_REPLY := by + apply UScalar.eq_of_val_eq + rw [hrel.st, hst] + unfold sched.TS_BLOCKED_ON_REPLY + rfl + exact (eq_of_spec_fd (P1.obj.Tcb.state.spec (st := st) s t)).trans (by rw [hsw']) + have hD : RqDomPrio σ := rqDomPrio_of_schedA H + refine do_reply_transfer_liveA + (M1 := fun s1 a1 σ1 => a1 = a ∧ replyRemove (Z := Unit) (specPtr t) (specPtr r) σ = ({((), σ1)}, False) ∧ + replyUnblockProgramA st s1 t ⦃ s2 => ∃ σ2, + (getTcbObjRef (Z := Unit) Tcb.tcbSchedContext (specPtr t) >>= + ifSporadicCurScTestRefillUnblockCheck) σ1 = ({((), σ2)}, False) ∧ + ReplyMidA st Z m κ E R Bf s σ r t s2 σ2 ∧ WritesIn st s s2 (RemWrA (st := st) Z m σ s R r t) ⦄) + (M2 := fun s2 a1 σ2 => a1 = a ∧ (∃ σ1, replyRemove (Z := Unit) (specPtr t) (specPtr r) σ = ({((), σ1)}, False) ∧ + (getTcbObjRef (Z := Unit) Tcb.tcbSchedContext (specPtr t) >>= + ifSporadicCurScTestRefillUnblockCheck) σ1 = ({((), σ2)}, False)) ∧ ReplyMidA st Z m κ E R Bf s σ r t s2 σ2 ∧ + WritesIn st s s2 (RemWrA (st := st) Z m σ s R r t)) + (M3 := fun s4 a4 σ4 ft fault => a4 = a ∧ ft = fl4_abi.consts.fault.NullFault ∧ fault = none ∧ + ∃ σ1 σ2 σ3 stack' Q R' x y2 ys, + replyRemove (Z := Unit) (specPtr t) (specPtr r) σ = ({((), σ1)}, False) ∧ + (getTcbObjRef (Z := Unit) Tcb.tcbSchedContext (specPtr t) >>= + ifSporadicCurScTestRefillUnblockCheck) σ1 = ({((), σ2)}, False) ∧ + (∃ s2 : S, StateRelA Z.th (L.mem s2) σ2 ∧ SlotsValidA Z.th (L.mem s2)) ∧ + σ2.kheap (specPtr sender) = some (.tcb ys) ∧ σ2.kheap (specPtr t) = some (.tcb y2) ∧ + ys.tcbFault = none ∧ (grant = false ∨ ipcBufOfA false ys = none ∨ + unat (dataToMessageInfo (ys.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0) ∧ + y2.tcbIpcframe = xt.tcbIpcframe ∧ y2.tcbIpcBuffer = xt.tcbIpcBuffer ∧ + (∀ rb, ipcBufOfA true y2 = some rb → (bufAddr rb 0).toNat % 8 = 0) ∧ + ((), σ3) ∈ (doIpcTransfer (Z := Unit) (specPtr sender) none (0 : Badge) grant (specPtr t) σ2).1 ∧ + RqDomPrio σ3 ∧ ((), σ4) ∈ (setThreadState (Z := Unit) (specPtr t) .running σ3).1 ∧ + SchedSRB st { Z with Q := Q, R := R' } m κ s4 σ4 (specPtr t) ∧ StEqA Z.T (L.mem s4) σ4 ∧ + σ4.kheap (specPtr t) = some (.tcb x) ∧ x.tcbState = .running ∧ + (∀ d p, specPtr t ∉ σ4.readyQueues d p) ∧ specPtr t ∉ σ4.releaseQueue ∧ + EpStateRel Z.T E R Bf (L.mem s4) σ4 ∧ ReplyStackRelA (L.mem s4) R Z.C σ4 (scNameA m) stack' ∧ + WritesIn st s s4 (ReplyFootW (st := st) Z m σ s E R Bf) ∧ SSzA σ σ4 ∧ + WritesIn st s s4 (ReplyNwA (st := st) Z m σ s R r t Bf)) + (R := fun s' a' σ' => (∃ stack', a' = a ∧ IpcSchedRelA st Z m κ s' σ' ∧ + WritesIn st s s' (ReplyFootW (st := st) Z m σ s E R Bf) ∧ SSzA σ σ' ∧ + EpStateRel Z.T E R Bf (L.mem s') σ' ∧ ReplyStackRelA (L.mem s') R Z.C σ' (scNameA m) stack' ∧ + (∀ c ∈ Z.C, ∀ q ∈ stack' c, q ∈ stack c) ∧ + IpcKeepA σ σ' ∧ SInvXA σ' m n ∧ SIdleA σ' m n ∧ SchedValidA σ' ∧ QSendA σ σ' ∧ + (∃ y2 : Tcb, y2.tcbIpcframe = xt.tcbIpcframe ∧ y2.tcbIpcBuffer = xt.tcbIpcBuffer ∧ + UFrA (BufBytesA (ipcBufOfA true y2)) σ σ') ∧ + WritesIn st s s' (ReplyNwA (st := st) Z m σ s R r t Bf) ∧ + (∃ σ4, SIKA σ.idleThread (· = specPtr t) (· = specPtr t) true σ σ4 ∧ σ'.kheap = σ4.kheap ∧ + EpStateRel Z.T E R Bf (L.mem s') σ4 ∧ + (∀ x, σ4.kheap (specPtr t) = some (.tcb x) → x.tcbState = .running) ∧ + (∀ u, σ4.schedulerAction ≠ .switchThread u) ∧ ReplyExitA (specPtr t) (specPtr r) σ σ4 σ') ∧ RepEndA σ σ') ∨ RT s' a' σ') + hkr hb hkt hst htr hn hsr ?_ ?_ ?_ ?_ + · refine WP.spec_mono (replyRemoveThenUnblockQA (ar := ar) a H P PE HE HS hr ht hkt hkr hb hst hq cov hsc0 hlocal + unb hoff hSt hV hC hNative) ?_ + rintro out ⟨σ1, h1, ha, hU⟩ + exact ⟨σ1, h1, ha, h1, hU⟩ + · rintro s1 a1 σ1 ⟨ha, h1, hU⟩ + refine WP.spec_mono hU ?_ + rintro s2 ⟨σ2, h2, hM⟩ + exact ⟨σ2, h2, ha, ⟨σ1, h1, h2⟩, hM⟩ + · rintro s2 a1 σ2 ft ⟨ha1, ⟨σ1, h1, h2⟩, hM⟩ hft + obtain ⟨⟨Q1, R1, stack', H2, E2, S2, -, keep, hSt2, hV2, hC2, hN2, ⟨y2, hy2, hy2s⟩, wm, szm⟩, wn⟩ := id hM + have hrel2 : TcbRelE R (L.mem s2) t y2 := by + obtain ⟨x, hx, h⟩ := E2.tcb t ht + rw [hy2] at hx + cases hx + exact h + obtain ⟨ys, st1, sc1, hys, eys⟩ := keep.2.2 _ xs hks + obtain ⟨yt, st2, sc2, hyt, eyt⟩ := keep.2.2 _ xt hkt + have e2 : yt = y2 := by + rw [hy2] at hyt + exact (KernelObject.tcb.inj (Option.some.inj hyt)).symm + subst e2 + have hf2 : yt.tcbFault = none := by rw [eyt]; exact hfr + have h0 : ft = fl4_abi.consts.fault.NullFault := by + rw [hft] + exact (hrel2.fault0.2 hf2).trans (by unfold fl4_abi.consts.fault.NullFault; rfl) + subst h0 + have hfys : ys.tcbFault = none := by rw [eys]; exact hfs + have hcys : grant = false ∨ ipcBufOfA false ys = none ∨ + unat (dataToMessageInfo (ys.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0 := by + rw [eys]; exact hcaps + have hif : yt.tcbIpcframe = xt.tcbIpcframe := by rw [eyt] + have hib : yt.tcbIpcBuffer = xt.tcbIpcBuffer := by rw [eyt] + have hbuf : ipcBufOfA true yt = ipcBufOfA true xt := by rw [eyt]; rfl + have hu2 : ∀ rp, ipcBufOfA true yt = some rp → ∀ k, k < 128 → inUserFrame (bufAddr rp k) σ2 := by + intro rp hrp k hk + rw [hbuf] at hrp + have hin := hu rp hrp k hk + unfold inUserFrame at hin ⊢ + obtain ⟨sz, hsz⟩ := hin + exact ⟨sz, keep.2.1 _ _ hsz (fun _ _ e => by cases e) (fun _ e => by cases e) (fun _ e => by cases e)⟩ + let Rb : S → A → State Unit → Prop := fun s4 a4 σ4 => a4 = a1 ∧ ∃ σ3 Q R' x, + ((), σ3) ∈ (doIpcTransfer (Z := Unit) (specPtr sender) none (0 : Badge) grant (specPtr t) σ2).1 ∧ + RqDomPrio σ3 ∧ ((), σ4) ∈ (setThreadState (Z := Unit) (specPtr t) .running σ3).1 ∧ + SchedSRB st { Z with Q := Q, R := R' } m κ s4 σ4 (specPtr t) ∧ StEqA Z.T (L.mem s4) σ4 ∧ + σ4.kheap (specPtr t) = some (.tcb x) ∧ x.tcbState = .running ∧ + (∀ d p, specPtr t ∉ σ4.readyQueues d p) ∧ specPtr t ∉ σ4.releaseQueue ∧ + EpStateRel Z.T E R Bf (L.mem s4) σ4 ∧ ReplyStackRelA (L.mem s4) R Z.C σ4 (scNameA m) stack' ∧ + WritesIn st s s4 (ReplyFootW (st := st) Z m σ s E R Bf) ∧ SSzA σ σ4 ∧ + WritesIn st s s4 (ReplyNwA (st := st) Z m σ s R r t Bf) + have body : replyBodyProgramA st ar s2 a1 sender t fl4_abi.consts.fault.NullFault grant ⦃ r => ∃ σ4, ((), σ4) ∈ ((fl4DoIpcTransfer (Z := Unit) + (specPtr sender) none 0 grant (specPtr t) >>= fun _ => setThreadState (specPtr t) .running) σ2).1 ∧ + Rb r.1 r.2 σ4 ⦄ := by + refine replyNormalBody_refinesA (st := st) (ar := ar) (s := s2) (a := a1) (σ := σ2) (sender := sender) + (t := t) (grant := grant) (M := fun s3 σ3 => + ((), σ3) ∈ (doIpcTransfer (Z := Unit) (specPtr sender) none (0 : Badge) grant (specPtr t) σ2).1 ∧ + XferPostA (st := st) (L := L) Z.T E R Bf (IpcSchedRelA st Z m κ) s2 σ2 t yt s3 σ3) (R := Rb) + hys hy2 hfys ?_ ?_ + · refine WP.spec_mono (do_ipc_transfer_anyA (ar := ar) (schedOpsEAOfPlacementA (st := st) (m := m) (κ := κ) P) + PE H2.toSchedSRN.place s2 a1 σ2 E2 ⟨_, _, H2⟩ hSt2 hV2 hC2 hN2 hs ht hne + ((H2.toSchedSRN.thT sender).2 hs) ((H2.toSchedSRN.thT t).2 ht) hys hy2 hu2 hoff 0#u64 0#u64 grant + (Or.inl ⟨hfys, hcys⟩) none) ?_ + rintro s3 ⟨σ3, he, hpost⟩ + have hm : ((), σ3) ∈ + (doIpcTransfer (Z := Unit) (specPtr sender) none (0#u64 : U64).bv grant (specPtr t) σ2).1 := by + rw [he] + exact Set.mem_singleton _ + exact ⟨σ3, hm, hm, hpost⟩ + · rintro s3 σ3 ⟨p3, E3, ⟨Q3, R3, H3⟩, hag, w3, es3, hoth, x3, hx3, ex3⟩ + have hst3 : StEqA Z.T (L.mem s3) σ3 := fun u hu x hx => (E3.at_tcb hu hx).st + have hx3s : x3.tcbState = .inactive := (congrArg Tcb.tcbState ex3).trans hy2s + have hnq3 := H3.notQueued hx3 (by rw [hx3s]; rfl) + have pD : RqDomPrio σ3 := rqDomPrio_of_schedA H3 + refine WP.spec_mono (set_thread_state_exAnyA H3 hst3 ht hx3 (ts := .running) rfl + (v := sched.TS_RUNNING) (by unfold sched.TS_RUNNING; rfl)) ?_ + rintro s4 ⟨σ4, he4, hk4, HB, hst4, ws⟩ + have p4 : ((), σ4) ∈ (setThreadState (Z := Unit) (specPtr t) .running σ3).1 := by + rw [he4]; exact Set.mem_singleton _ + have e4 : σ4 = stsActA (updKSc σ3 (specPtr t) (.tcb { x3 with tcbState := .running })) (specPtr t) := + det_outA he4 (setThreadState_stsActExA hx3 _) + have hq4 : σ4.readyQueues = σ3.readyQueues ∧ σ4.releaseQueue = σ3.releaseQueue ∧ + σ4.machineState = σ3.machineState := by + rw [e4] + unfold stsActA + split <;> exact ⟨rfl, rfl, rfl⟩ + have hkh : σ4.kheap = Function.update σ3.kheap (specPtr t) (some (.tcb { x3 with tcbState := .running })) := by + rw [hk4, updKSc_kheap] + have hx4 : σ4.kheap (specPtr t) = some (.tcb { x3 with tcbState := .running }) := by + rw [hkh, Function.update_self] + have hz23 : SSzA σ2 σ3 := ssz_of_epStep es3 + have hz34 : SSzA σ3 σ4 := (SPresInst.pres (m := setThreadState (Z := Unit) (specPtr t) .running) + σ3 _ p4).2.1 + have E4 : EpStateRel Z.T E R Bf (L.mem s4) σ4 := + epRel_runningA (Z := { Z with Q := Q3, R := R3 }) H3.toSchedSRN P PE E3 ht hx3 hx3s hkh hq4.2.2 (hst4 t ht _ hx4) ws + -- the reply stack + have wa3 : ∀ b, b = stA t ∨ b = L.nodeAt s3 (L.cpu s3) layout.node.SCHED_ACTION → + ReplyFootW (st := st) { Z with Q := Q3, R := R3 } m σ3 s3 E R Bf b := by + rintro b (h | h) + · refine Or.inl (Or.inr (Or.inr (Or.inl ⟨t, ht, Or.inl ?_⟩))) + have hfit := (PE.tcb t ht).fits + have v : (stA t).val = t.val + 3328 := by + rw [stA_eq, tw_val (PE.tcb t ht) (by simp)]; simp + rw [h] + exact ⟨by omega, by unfold TWOffN; omega⟩ + · refine Or.inr (Or.inl (Or.inr (Or.inl ?_))) + rw [h] + exact H3.toSchedSRN.node_sks H3.toSchedSRN.sch.cpuCs (by simp [layout.node.SCHED_ACTION]) + have w3' : WritesIn st s2 s3 (ReplyFootW (st := st) { Z with Q := Q1, R := R1 } m σ2 s2 E R Bf) := + w3.mono fun b h => Or.inl h + have w4' : WritesIn st s3 s4 (ReplyFootW (st := st) { Z with Q := Q3, R := R3 } m σ3 s3 E R Bf) := + ws.mono wa3 + have w24 : WritesIn st s2 s4 (ReplyFootW (st := st) { Z with Q := Q1, R := R1 } m σ2 s2 E R Bf) := + ReplyFootW.trans (Z := { Z with Q := Q1, R := R1 }) (Z' := { Z with Q := Q3, R := R3 }) rfl rfl + w3' hz23 w4' + have w04 : WritesIn st s s4 (ReplyFootW (st := st) Z m σ s E R Bf) := + ReplyFootW.trans (Z' := { Z with Q := Q1, R := R1 }) rfl rfl wm szm w24 + have S4 : ReplyStackRelA (L.mem s4) R Z.C σ4 (scNameA m) stack' := by + have link : ∀ q ∈ R, ∀ b, Aligned b → q.val < b.val → b.val < q.val + 32 → L.mem s4 b = L.mem s2 b := by + intro q hq b ha hl hh + have off := replyLinkOffEp H2.toSchedSRN P PE hq hl hh + have off3 := replyLinkOffEp H3.toSchedSRN P PE hq hl hh + have a3 : L.mem s3 b = L.mem s2 b := w3.2.2 b ha fun h => off h.foot + rw [← a3] + refine ws.2.2 b ha fun h => ?_ + rcases h with h | h + · refine off3 (Or.inr (Or.inr (Or.inl ⟨t, ht, Or.inl ?_⟩))) + rw [h]; exact (a_st (PE.tcb t ht)).2 + · refine (replyWordOffSchedA (b := b) H3.toSchedSRN P hq (by omega) (by omega)).2 ?_ + rw [h] + exact Or.inr (Or.inl (H3.toSchedSRN.node_sks H3.toSchedSRN.sch.cpuCs (by simp [layout.node.SCHED_ACTION]))) + have kh : ∀ p o, σ4.kheap p = some o → (∀ x, o ≠ .tcb x) → (∀ x, o ≠ .endpoint x) → (∀ x, o ≠ .reply x) → + σ2.kheap p = some o := by + intro p o h n1 n2 n3 + have npt : p ≠ specPtr t := fun e => by + rw [e, hx4] at h + exact n1 _ (Option.some.inj h).symm + rw [hkh, Function.update_of_ne npt] at h + rcases es3.2.2 p with e | ⟨_, y, _, e⟩ | ⟨_, y, _, e, _⟩ | ⟨_, y, _, e, _⟩ + · rwa [e] at h + · rw [e] at h; exact absurd (Option.some.inj h).symm (n2 y) + · rw [e] at h; exact absurd (Option.some.inj h).symm (n3 y) + · rw [e] at h; exact absurd (Option.some.inj h).symm (n1 y) + refine S2.frame (fun q hq => ?_) (fun c _ x k hx => ?_) (fun q hq x' hx' => ?_) + · have v1 := replyNextPlaceA (Z := Z) PE hq + have v2 := replyPrevPlaceA (Z := Z) PE hq + exact ⟨link q hq _ v1.1 (by omega) (by omega), link q hq _ v2.1 (by omega) (by omega)⟩ + · refine ⟨x, k, ?_, rfl⟩ + have npt : scNameA m c ≠ specPtr t := fun e => by rw [e, hy2] at hx; cases hx + rw [hkh, Function.update_of_ne npt] + rcases es3.2.2 (scNameA m c) with e | ⟨_, _, e, _⟩ | ⟨_, _, e, _⟩ | ⟨_, _, e, _⟩ + · rwa [e] + all_goals (rw [hx] at e; cases e) + · have npt : specPtr q ≠ specPtr t := fun e => by + rw [e, hx4] at hx'; cases hx' + rw [hkh, Function.update_of_ne npt] at hx' + rcases es3.2.2 (specPtr q) with e | ⟨_, y, _, e⟩ | ⟨z, y, ez, e, er⟩ | ⟨_, y, _, e, _⟩ + · exact ⟨x', by rw [← e]; exact hx', rfl⟩ + · rw [e] at hx'; cases hx' + · rw [e] at hx' + cases hx' + exact ⟨z, ez, er⟩ + · rw [e] at hx'; cases hx' + have w04n : WritesIn st s s4 (ReplyNwA (st := st) Z m σ s R r t Bf) := by + have w3n : WritesIn st s2 s3 (fun b => RcvRegA t b ∨ BufW Bf b) := + ⟨w3.1, w3.2.1, fun b hb hn => hag b hb ⟨fun h => hn (Or.inl h), fun h => hn (Or.inr h)⟩⟩ + refine ((wn.trans w3n).trans ws).mono fun b hb => ?_ + rcases hb with (h | h | h) | h | h + · exact Or.inl h + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr h) + · exact Or.inl (Or.inr (Or.inr (Or.inl h))) + · refine Or.inl (Or.inr (Or.inr (Or.inr (mwr_back (Z := Z) (Z' := { Z with Q := Q3, R := R3 }) rfl rfl + (szm.trans hz23) (w3.1.trans wm.1) (Or.inr (Or.inl ?_)))))) + rw [h] + exact H3.toSchedSRN.node_sks H3.toSchedSRN.sch.cpuCs (by simp [layout.node.SCHED_ACTION]) + refine ⟨σ4, p4, rfl, σ3, Q3, R3, { x3 with tcbState := .running }, p3, pD, p4, HB, hst4, hx4, rfl, + fun d p => by rw [hq4.1]; exact hnq3.1 d p, by rw [hq4.2.1]; exact hnq3.2, E4, S4, w04, + szm.trans (hz23.trans hz34), w04n⟩ + refine WP.spec_mono body ?_ + rintro out ⟨σ4, hm, ha4, σ3, Q, R', x, p3, pD, p4, rest⟩ + refine ⟨σ4, yt, hy2, Or.inl ⟨hf2, hm⟩, ha4.trans ha1, rfl, hf2, + σ1, σ2, σ3, stack', Q, R', x, yt, ys, h1, h2, ⟨s2, hSt2, hV2⟩, hys, hy2, hfys, hcys, hif, hib, + (fun rb h => hal rb (hbuf ▸ h)), p3, pD, p4, rest⟩ + · rintro s4 a4 σ4 ft fault ⟨ha4, hft0, hf0, σ1, σ2, σ3, stack', Q, R', x, y2, ys, h1, h2, ⟨s2, hSt2, hV2⟩, hys, + hy2, hfys, hcys, hif, hib, hal2, p3, pD, p4, HB, hst4, hx4, hxr, hnqs, hnrel, E4, S4, w04, sz04, w04n⟩ + subst hft0 hf0 + have m1 : ((), σ1) ∈ (replyRemove (Z := Unit) (specPtr t) (specPtr r) σ).1 := by + rw [h1]; exact Set.mem_singleton _ + have m2 : ((), σ2) ∈ ((getTcbObjRef (Z := Unit) Tcb.tcbSchedContext (specPtr t) >>= + ifSporadicCurScTestRefillUnblockCheck) σ1).1 := by + rw [h2]; exact Set.mem_singleton _ + have htr' : Z.th t = some .tcb := (H.toSchedSRN.thT t).2 ht + obtain ⟨S14, F4, B4⟩ := replyLive_midA H.toSchedSRN.place s2 hkt hst hinv hI hSV hD hA m1 m2 hSt2 hV2 htr' hys hy2 + hfys hcys hal2 p3 p4 + obtain ⟨RS4, QS4, D4, U4, SW4⟩ := replyLive_midXA H.toSchedSRN.place s2 hkt hst hI hinv ⟨hinv, hI, hsact⟩ hsw m1 m2 hSt2 hV2 htr' hys hy2 + hfys hcys hal2 p3 pD p4 + obtain ⟨hon, hcpu, hact, hv⟩ := tailS s4 σ4 Q R' x HB hx4 B4 S14 w04 + have hcls : runnable x.tcbState = true ∨ ¬ inClassRSc x.tcbState x.tcbFault := Or.inl (by rw [hxr]; rfl) + have hbuf : ipcBufOfA true y2 = ipcBufOfA true xt := by unfold ipcBufOfA; rw [hif, hib] + have MID : ReplyTmoMidA st Z m κ E R Bf s σ σ t (ReplyNwA (st := st) Z m σ s R r t Bf) s4 σ4 Q R' stack' x := + { sr := HB, stq := hst4, kx := hx4, run := by rw [hxr]; rfl, nq := hnqs, nrel := hnrel, ep := E4, rs := S4, + w := w04, wn := w04n, sz := sz04, clr := Or.inl rfl, keep := S14, fia := F4, bnd := B4, dom := D4, qs := QS4, + rsub := RS4, ufr := ⟨xt, hkt, hbuf ▸ U4⟩, nsw := SW4, + bl := replyLive_blA H.toSchedSRN.place s2 m1 m2 hSt2 hV2 htr' hys hy2 hfys hcys hal2 p3 p4 } + have tmo4 := tmo s4 σ4 Q R' stack' x MID + rw [← ha4] at tmo4 + have key := replyTailExTA (st := st) (ar := ar) (a := a4) (ft := fl4_abi.consts.fault.NullFault) + (R := fun s' a' σ' => (¬ ReplyTmoCondA σ4 (specPtr t) none ∧ a' = a4 ∧ + (∃ Q'' R'', SchedSRA st { Z with Q := Q'', R := R'' } m κ s' σ') ∧ + σ'.kheap = σ4.kheap ∧ WritesIn st s4 s' (MWrA (st := st) { Z with Q := Q, R := R' } m σ4 s4)) ∨ RT s' a' σ') + HB hst4 ht hon (hmb a4) none hx4 hcls hidle hact hv + (by unfold fl4_abi.consts.fault.NullFault fl4_abi.consts.fault.Timeout; decide) hcpu hnqs hnrel + (fun hnt H' => Or.inl ⟨hnt, rfl, ⟨_, _, H'⟩, rfl, WritesIn.refl _ _⟩) + (fun hnt _ _ Q' H' hk w => Or.inl ⟨hnt, rfl, ⟨Q', R', H'⟩, hk, w.mono fun _ hb => Or.inr hb⟩) + (fun hnt _ _ R'' H' hk _ w => Or.inl ⟨hnt, rfl, ⟨Q, R'', H'⟩, hk, w.mono fun b hb => Or.inr (by + rcases hb with hb | hb + · exact HB.toSchedSRN.qfoot_swr (fun v hv => (List.mem_cons.1 hv).elim (· ▸ ht) + (fun e => (HB.toSchedSRN.relAct v e).1)) HB.toSchedSRN.sch.cpuCs ht hb + · rw [hb]; exact HB.toSchedSRN.repr_swr)⟩) + (fun sc y k badge h1 h2 h3 h4 h5 _ _ => WP.spec_mono (tmo4 sc y k badge h1 h2 h3 h4 h5) + fun _ ⟨σ', hm, hR⟩ => ⟨σ', hm, Or.inr hR⟩) + refine WP.spec_mono key ?_ + rintro out ⟨σ5, hm, hR⟩ + refine ⟨σ5, hm, ?_⟩ + rcases hR with ⟨hnt, ha5, ⟨Q5, R5, H5⟩, hk5, w5⟩ | hRT + swap + · exact Or.inr hRT + refine Or.inl ?_ + have m5 : ((), σ5) ∈ (replyTailSpecA (specPtr t) none σ4).1 := hm + obtain ⟨K, I5, D5, V5, Qs, Rs, U5, -, mach, X5⟩ := replyLive_specKeepA H.toSchedSRN.place s2 hkt hst hnt hfr hback hinv hI + hSV hD hA hsw hnq hcur m1 m2 hSt2 hV2 htr' hys hy2 hfys hcys hal2 p3 pD p4 + (fun z hz => by rw [hx4] at hz; cases hz; exact hxr) + (fun x' p y k hx' hp hy ha => by + rw [fl4RefillSufficient_eq_l4v hag] + exact HB.inv.suff hy ha (HB.inv.tcbScNotIdle _ x' p hx' hp hidle)) m5 + have E5 : EpStateRel Z.T E R Bf (L.mem out.1) σ5 := by + refine E4.frame PE (fun b ha hb => w5.2.2 b ha (endpFootOffWrite HB.toSchedSRN P hb)) ⟨fun p y h => ?_, + fun p y h => ⟨y, ?_, rfl⟩, fun p y h => ⟨y, ?_, rfl, rfl, rfl, rfl, rfl⟩, mach⟩ + · rw [hk5]; exact h + · rw [hk5]; exact h + · rw [hk5]; exact h + have S5 : ReplyStackRelA (L.mem out.1) R Z.C σ5 (scNameA m) stack' := by + refine S4.frame (fun q hq => ?_) (fun c _ y k h => ⟨y, k, by rw [hk5]; exact h, rfl⟩) + (fun q _ y h => ⟨y, by rw [← hk5]; exact h, rfl⟩) + have link : ∀ b, Aligned b → q.val < b.val → b.val < q.val + 32 → L.mem out.1 b = L.mem s4 b := + fun b ha hl hh => w5.2.2 b ha (replyWordOffSchedA (b := b) HB.toSchedSRN P hq (by omega) (by omega)).2 + have v1 := replyNextPlaceA (Z := Z) PE hq + have v2 := replyPrevPlaceA (Z := Z) PE hq + exact ⟨link _ v1.1 (by omega) (by omega), link _ v2.1 (by omega) (by omega)⟩ + have sub : ∀ c ∈ Z.C, ∀ q ∈ stack' c, q ∈ stack c := by + intro c hc q hq + obtain ⟨⟨y5, n5, hy5, l5⟩, -⟩ := S5.stackRel c hc + obtain ⟨⟨y0, n0, hy0, l0⟩, -⟩ := HS.stackRel c hc + obtain ⟨y, k, hy, sb⟩ := Rs _ _ _ hy5 + rw [hy0] at hy + cases hy + have := sb (specPtr q) (by rw [l5]; exact List.mem_map_of_mem hq) + rw [l0] at this + obtain ⟨q', hq', e⟩ := List.mem_map.1 this + rwa [specPtr_inj e] at hq' + have w05 : WritesIn st s out.1 (ReplyFootW (st := st) Z m σ s E R Bf) := + ReplyFootW.trans (Z' := { Z with Q := Q, R := R' }) rfl rfl w04 sz04 (w5.mono fun _ h => Or.inr (Or.inl h)) + have sz45 : SSzA σ4 σ5 := SSzA.of_kheap hk5 + have w05n : WritesIn st s out.1 (ReplyNwA (st := st) Z m σ s R r t Bf) := + (w04n.trans w5).mono fun b hb => hb.elim id fun h => Or.inl (Or.inr (Or.inr (Or.inr + (mwr_back (Z := Z) (Z' := { Z with Q := Q, R := R' }) rfl rfl sz04 w04.1 h)))) + exact ⟨stack', ha5.trans ha4, ⟨Q5, R5, H5⟩, w05, sz04.trans sz45, E5, S5, sub, K, I5, D5, V5, Qs, + ⟨y2, hif, hib, U5⟩, w05n, ⟨σ4, S14, hk5, + E4.frame PE (fun b ha hb => w5.2.2 b ha (endpFootOffWrite HB.toSchedSRN P hb)) + ⟨fun _ _ h => h, fun _ y h => ⟨y, h, rfl⟩, fun _ y h => ⟨y, h, rfl, rfl, rfl, rfl, rfl⟩, rfl⟩, + fun z hz => by rw [hx4] at hz; cases hz; exact hxr, + fun u e => by + rcases S14.act with c | c | ⟨y, hy, c⟩ + · exact hsw u (c.symm.trans e) + · rw [c] at e; cases e + · rw [c] at e; cases e; exact (SW4 _ c).2 hy, + X5⟩, + RepEndA.ofTail S14 rfl rfl hcur hsw (fun u e => (SW4 u e).2) + (replyLive_blA H.toSchedSRN.place s2 m1 m2 hSt2 hV2 htr' hys hy2 hfys hcys hal2 p3 p4) hx4 B4 hnt m5 hk5⟩ + + +set_option maxHeartbeats 1000000 in +/-- **The live Reply with the record's exports** (module docstring). -/ +theorem do_reply_transfer_keepA {s : S} {a : A} {σ : State Unit} {stack : U64 → List U64} {n : Nat} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {sender r t : U64} {grant : Bool} (hr : r ∈ R) (hs : sender ∈ Z.T) (ht : t ∈ Z.T) + (hne : sender ≠ t) + {xt xs : Tcb} {xr : Reply} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hkr : σ.kheap (specPtr r) = some (.reply xr)) (hb : xr.replyTcb = some (specPtr t)) + (hst : xt.tcbState = .blockedOnReply (specPtr r)) + (hks : σ.kheap (specPtr sender) = some (.tcb xs)) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) + (cov : ScReplyCovA Z.C m σ (specPtr r)) + (hsc0 : ∀ sc ∈ Z.C, (stack sc).head? = some r → sc ≠ 0#u64) + (hlocal : ∀ sc ∈ Z.C, (stack sc).head? = some r → xt.tcbSchedContext = none → + ReplyPopLocalQA (st := st) (ar := ar) (Z := Z) (m := m) a s σ sc t r) + (unb : ∀ s1 σ1 Q1 R1, SchedSRA st { Z with Q := Q1, R := R1 } m κ s1 σ1 → RemKeepA σ σ1 → + replyRemove (Z := Unit) (specPtr t) (specPtr r) σ = ({((), σ1)}, False) → + (L.mem s1 (scA t) ≠ 0#u64 → L.mem s1 (scCoreA (L.mem s1 (scA t))) = L.cpu s1) ∧ + (specPtr t ∈ σ1.releaseQueue → ∀ y n, + σ1.kheap (scNameA m (L.mem s1 (scA t))) = some (.schedContext y n) → + refillReady σ1.curTime (refillHd y) = false)) + (hoff : ∀ y, IsSlotA Z.th y → ¬ EWrite Z.T E R Bf y ∧ ¬ EWrite Z.T E R Bf (wa y 8#u64)) + (hSt : StateRelA Z.th (L.mem s) σ) (hV : SlotsValidA Z.th (L.mem s)) + (hC : BufCover st Z.T Bf s σ) (hNative : BufNativeA Z.T (L.mem s)) + (hfs : xs.tcbFault = none) + (hcaps : grant = false ∨ ipcBufOfA false xs = none ∨ + unat (dataToMessageInfo (xs.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0) + (hu : ∀ rp, ipcBufOfA true xt = some rp → ∀ k, k < 128 → inUserFrame (bufAddr rp k) σ) + (hal : ∀ rb, ipcBufOfA true xt = some rb → (bufAddr rb 0).toNat % 8 = 0) + (hag : fl4MinBudget = MIN_BUDGET) (hmb : ∀ a', Fl4MinBudgetAgreeA ar a') + (hidle : ∀ c < fl4A64Platform.numNodes, specPtr t ≠ m.idleThreadOf c) + (hfr : xt.tcbFault = none) (hto : isEpCap xt.tcbTimeoutHandler = false) + (hback : ∀ sc, xt.tcbSchedContext = some sc → ∀ y k, σ.kheap sc = some (.schedContext y k) → + y.scTcb = some (specPtr t)) + (hinv : SInvXA σ m n) (hI : SIdleA σ m n) (hSV : SchedValidA σ) (hA : ActRepA σ) + (hsw : ∀ u, σ.schedulerAction ≠ .switchThread u) (hnq : ∀ d p, specPtr t ∉ σ.readyQueues d p) + (hcur : specPtr t ≠ σ.curThread) (hsact : SActA σ) + (tailS : ∀ (s4 : S) (σ4 : State Unit) Q4 R4 (x : Tcb), + SchedSRB st { Z with Q := Q4, R := R4 } m κ s4 σ4 (specPtr t) → + σ4.kheap (specPtr t) = some (.tcb x) → BoundActive (specPtr t) σ4 → + SIKA σ.idleThread (· = specPtr t) (· = specPtr t) true σ σ4 → + WritesIn st s s4 (ReplyFootW (st := st) Z m σ s E R Bf) → + L.mem s4 (affA t) = L.cpu s4 ∧ + (L.mem s4 (scA t) ≠ 0#u64 → L.mem s4 (scCoreA (L.mem s4 (scA t))) = L.cpu s4) ∧ + (∀ sc y n, L.mem s4 (scA t) = sc → sc ≠ 0#u64 → + σ4.kheap (scNameA m sc) = some (.schedContext y n) → scActive y = true) ∧ + faults.valid_timeout_handler st s4 t = ok (isEpCap x.tcbTimeoutHandler)) : + ipc.do_reply_transfer st ar s a sender r grant ⦃ out => ∃ σ5 stack', + ((), σ5) ∈ (fl4DoReplyTransfer (Z := Unit) (specPtr sender) (specPtr r) grant σ).1 ∧ out.2 = a ∧ + IpcSchedRelA st Z m κ out.1 σ5 ∧ + WritesIn st s out.1 (ReplyFootW (st := st) Z m σ s E R Bf) ∧ SSzA σ σ5 ∧ + EpStateRel Z.T E R Bf (L.mem out.1) σ5 ∧ + ReplyStackRelA (L.mem out.1) R Z.C σ5 (scNameA m) stack' ∧ + (∀ c ∈ Z.C, ∀ q ∈ stack' c, q ∈ stack c) ∧ + IpcKeepA σ σ5 ∧ SInvXA σ5 m n ∧ SIdleA σ5 m n ∧ SchedValidA σ5 ∧ QSendA σ σ5 ∧ + (∃ y2 : Tcb, y2.tcbIpcframe = xt.tcbIpcframe ∧ y2.tcbIpcBuffer = xt.tcbIpcBuffer ∧ + UFrA (BufBytesA (ipcBufOfA true y2)) σ σ5) ∧ + WritesIn st s out.1 (ReplyNwA (st := st) Z m σ s R r t Bf) ∧ + (∃ σ4, SIKA σ.idleThread (· = specPtr t) (· = specPtr t) true σ σ4 ∧ σ5.kheap = σ4.kheap ∧ + EpStateRel Z.T E R Bf (L.mem out.1) σ4 ∧ + (∀ x, σ4.kheap (specPtr t) = some (.tcb x) → x.tcbState = .running)) ∧ RepEndA σ σ5 ⦄ := by + refine WP.spec_mono (do_reply_transfer_keepTA (RT := fun _ _ _ => False) H P PE HE HS hr hs ht hne hkt hkr hb hst hks hq + cov hsc0 hlocal unb hoff hSt hV hC hNative hfs hcaps hu hal hag hmb hidle hfr hback hinv hI hSV hA hsw hnq hcur hsact tailS + ?_) ?_ + · intro s4 σ4 Q4 R4 stack4 x M sc y k badge _ _ _ _ he + exfalso + obtain ⟨z, hz, g⟩ := M.keep.tcbF hkt + rw [M.kx] at hz + cases hz + have one : ∀ z : Tcb, tcbCnodeMap z (tcbCnodeIndex 4) = some z.tcbTimeoutHandler := fun z => by + simp [tcbCnodeMap, tcbIdx_eq, tcbStage2Slot, tcbHighVSpaceSlot] + rw [Option.some.inj ((one x).symm.trans ((congrFun g.caps (tcbCnodeIndex 4)).trans (one xt))), hto] at he + cases he + · rintro out ⟨σ5, hm, ⟨stack', rest⟩ | h⟩ + · obtain ⟨h1, h2, h3, h4, h5, h6, h7, h8, h9, h10, h11, h12, h13, h14, ⟨σ4, k1, k2, k3, k4, -, -⟩, hre⟩ := rest + exact ⟨σ5, stack', hm, h1, h2, h3, h4, h5, h6, h7, h8, h9, h10, h11, h12, h13, h14, ⟨σ4, k1, k2, k3, k4⟩, hre⟩ + · exact h.elim +end Store + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3ReplyLiveKeepFA.lean b/proofs/Fl4ProofsA64/P3/Stage3ReplyLiveKeepFA.lean new file mode 100644 index 000000000..d86e58db5 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ReplyLiveKeepFA.lean @@ -0,0 +1,633 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2b): **the live Reply to a faulted caller with the record's exports**, the fault +sibling of `do_reply_transfer_keepA`. + +* **`do_reply_transfer_keepFA`**: `ipc.do_reply_transfer` for a caller blocked on the reply object that has a pending + fault `f`: the removal, the refill test, `handle_fault_reply`'s register writes, the fault clear and `Restart` / + `Inactive`, the SC tail. It exports `IpcSchedRelA`, the write set (`ReplyFootW`, narrow: `ReplyNwFA`), `SSzA`, + `EpStateRel`, `ReplyStackRelA` over a stack contained in the old one, and the spec keeps from the pre-state *with the + caller's fault cleared* (`IpcKeepA (faultClrA σ t xt) σ5`, `SInvXA`, `SIdleA`, `SchedValidA`, `QSendA`, no user byte + written). +* `ReplyNwFA`: `ReplyNwA` and the fault word. + +Premises: those of `do_reply_transfer_keepA` (the fault-free selectors `hfr` / `hfs` / `hcaps` are replaced by `hfr` the +caller's fault `f`, `hok` ([S] `FaultReplyOKA`) and `hvc` ([J] its VCPU link)); `hwords` (the caller's VCPU and fault-word-1 words are off the +removal's write set); `hL` (`LoopInvA`: no pending switch survives to the caller); `tailS` is stated at the reached +state from the cleared pre-state. New module. +-/ +import Fl4ProofsA64.P3.Stage3ReplyLiveKeepA +import Fl4ProofsA64.P3.Stage3ReplyFaultClrA +import Fl4ProofsA64.P3.Stage3ReplyFaultMidA +import Fl4ProofsA64.P3.Stage3ReplyFaultKeepSpecA +import Fl4ProofsA64.P3.Stage3ReplyTmoMidA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-- The narrow write set of the fault Reply: the removal's, the caller's registers and buffer words, and its fault +word. -/ +def ReplyNwFA {S : Type} (st : machine.Store S) [L : LawfulStore st] (Z : XSetsA) (m : McState) (σ : State Unit) + (s : S) (R : Set U64) (r t : U64) (Bf : Set (U64 × Word)) (b : U64) : Prop := + ReplyNwA (st := st) Z m σ s R r t Bf b ∨ b = faultA t + +section Store +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + +private theorem reachedRFA {α : Type} {r : Result α} {P : α → Prop} (step : r ⦃ P ⦄) : + r ⦃ v => r = ok v ∧ P v ⦄ := by + obtain ⟨v, eq, post⟩ := spec_ex_fd step + rw [eq, WP.spec_ok] + exact ⟨rfl, post⟩ + +/-- The `ts` write (`Restart` / `Inactive`) of an `Inactive` thread keeps the endpoint relation. -/ +theorem epRel_stateA {s s' : S} {σ σ' : State Unit} (H : SchedSRN st Z m κ s σ) + (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) (E0 : EpStateRel Z.T E R Bf (L.mem s) σ) + {t : U64} (ht : t ∈ Z.T) {x : Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) (hx : x.tcbState = .inactive) + {ts : ThreadState} (hts : tsCode ts < 3) + (hkh : σ'.kheap = Function.update σ.kheap (specPtr t) (some (.tcb { x with tcbState := ts }))) + (hm : σ'.machineState = σ.machineState) (hst : (L.mem s' (stA t)).val = tsCode ts) + (w : WritesIn st s s' (fun b => b = stA t ∨ b = L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION)) : + EpStateRel Z.T E R Bf (L.mem s') σ' := by + have hT : TcbRelE R (L.mem s) t x := by + obtain ⟨y, hy, h⟩ := E0.tcb t ht + rw [hk] at hy + cases hy + exact h + have h0 : (L.mem s (stA t)).val = 0 := by rw [hT.st, hx]; rfl + have nq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue := by + intro e he z hz hmem + obtain ⟨z', hz', hr⟩ := E0.ep e he + rw [hz] at hz' + cases hz' + have key : ∀ (xs : List U64) (k : Nat), k ≠ 0 → (∀ y ∈ xs, QueuedOn (L.mem s) e k y) → + specPtr t ∈ xs.map specPtr → False := by + intro xs k hk0 hq hmm + obtain ⟨y, hy, ey⟩ := List.mem_map.1 hmm + have := (hq y hy).1 + rw [specPtr_inj ey, h0] at this + exact hk0 this.symm + cases z with + | idleEP => cases hmem + | sendEP q => + obtain ⟨-, xs, -, eq, -, hq⟩ := hr + rw [Endpoint.epQueue, eq] at hmem + exact key xs 4 (by omega) hq hmem + | recvEP q => + obtain ⟨-, xs, -, eq, -, hq⟩ := hr + rw [Endpoint.epQueue, eq] at hmem + exact key xs 3 (by omega) hq hmem + have node : ∀ b, EFoot Z.T E R Bf b → b ≠ L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION := by + intro b hb e + refine endpFootOffWrite H P hb ?_ + rw [e] + exact Or.inr (Or.inl (H.node_sks H.sch.cpuCs (by simp [layout.node.SCHED_ACTION]))) + have hA : Agree (L.mem s) (L.mem s') (fun b => EFoot Z.T E R Bf b ∧ ¬ TF t b) := by + intro b ha hb + refine w.2.2 b ha fun h => ?_ + rcases h with h | h + · refine hb.2 ?_ + rw [h] + exact (a_st (PE.tcb t ht)).2 + · exact node b hb.1 h + have hT' : TcbRelE R (L.mem s') t { x with tcbState := ts } := by + refine TcbRelE.with_state (PE.tcb t ht) hT ts hts hst fun b ha hb => w.2.2 b ha fun h => ?_ + rcases h with h | h + · exact hb.2 (by rw [h]) + · exact node b (Or.inr (Or.inr (Or.inl ⟨t, ht, Or.inl hb.1⟩))) h + have U := EpStateRel.upd_tcb E0 PE ht _ hA (Or.inl nq) hT' + refine U.frame PE (fun _ _ _ => rfl) ⟨fun p y h => ?_, fun p y h => ⟨y, ?_, rfl⟩, + fun p y h => ⟨y, ?_, rfl, rfl, rfl, rfl, rfl⟩, ?_⟩ + · rw [hkh]; exact h + · rw [hkh]; exact h + · rw [hkh]; exact h + · rw [hm]; rfl + +set_option maxHeartbeats 2000000 in +/-- **The live Reply to a faulted caller with the record's exports** (module docstring). -/ +theorem do_reply_transfer_keepFTA {s : S} {a : A} {σ : State Unit} {stack : U64 → List U64} {n : Nat} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {sender r t : U64} {grant : Bool} (hr : r ∈ R) (hs : sender ∈ Z.T) (ht : t ∈ Z.T) + (hne : sender ≠ t) + {xt xs : Tcb} {xr : Reply} {f : Fault} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hkr : σ.kheap (specPtr r) = some (.reply xr)) (hb : xr.replyTcb = some (specPtr t)) + (hst : xt.tcbState = .blockedOnReply (specPtr r)) + (hks : σ.kheap (specPtr sender) = some (.tcb xs)) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) + (cov : ScReplyCovA Z.C m σ (specPtr r)) + (hsc0 : ∀ sc ∈ Z.C, (stack sc).head? = some r → sc ≠ 0#u64) + (hlocal : ∀ sc ∈ Z.C, (stack sc).head? = some r → xt.tcbSchedContext = none → + ReplyPopLocalQA (st := st) (ar := ar) (Z := Z) (m := m) a s σ sc t r) + (unb : ∀ s1 σ1 Q1 R1, SchedSRA st { Z with Q := Q1, R := R1 } m κ s1 σ1 → RemKeepA σ σ1 → + replyRemove (Z := Unit) (specPtr t) (specPtr r) σ = ({((), σ1)}, False) → + (L.mem s1 (scA t) ≠ 0#u64 → L.mem s1 (scCoreA (L.mem s1 (scA t))) = L.cpu s1) ∧ + (specPtr t ∈ σ1.releaseQueue → ∀ y n, + σ1.kheap (scNameA m (L.mem s1 (scA t))) = some (.schedContext y n) → + refillReady σ1.curTime (refillHd y) = false)) + (hoff : ∀ y, IsSlotA Z.th y → ¬ EWrite Z.T E R Bf y ∧ ¬ EWrite Z.T E R Bf (wa y 8#u64)) + (hSt : StateRelA Z.th (L.mem s) σ) (hV : SlotsValidA Z.th (L.mem s)) + (hC : BufCover st Z.T Bf s σ) (hNative : BufNativeA Z.T (L.mem s)) + (hag : fl4MinBudget = MIN_BUDGET) (hmb : ∀ a', Fl4MinBudgetAgreeA ar a') + (hidle : ∀ c < fl4A64Platform.numNodes, specPtr t ≠ m.idleThreadOf c) + (hfr : xt.tcbFault = some f) + (hok : FaultReplyOKA (L := L) s t + (unat (dataToMessageInfo (xs.tcbArch.tcbContext.userRegs msg_info_register)).miLength) f) + (hvc : xt.tcbArch.tcbVcpu ≠ none ↔ L.mem s (tVcpuA t) ≠ 0#u64) + (hwords : ∀ s2, WritesIn st s s2 (RemWrA (st := st) Z m σ s R r t) → + L.mem s2 (tVcpuA t) = L.mem s (tVcpuA t) ∧ L.mem s2 (tFw1A t) = L.mem s (tFw1A t)) + (hback : ∀ sc, xt.tcbSchedContext = some sc → ∀ y k, σ.kheap sc = some (.schedContext y k) → + y.scTcb = some (specPtr t)) + (hL : LoopInvA σ m n) (hSV : SchedValidA σ) (hA : ActRepA σ) + (hsw : ∀ u, σ.schedulerAction ≠ .switchThread u) (hnq : ∀ d p, specPtr t ∉ σ.readyQueues d p) + (hcur : specPtr t ≠ σ.curThread) + (tailS : ∀ (s4 : S) (σ4 : State Unit) Q4 R4 (x : Tcb), + SchedSRB st { Z with Q := Q4, R := R4 } m κ s4 σ4 (specPtr t) → + σ4.kheap (specPtr t) = some (.tcb x) → BoundActive (specPtr t) σ4 → + SIKA σ.idleThread (· = specPtr t) (· = specPtr t) true (faultClrA σ (specPtr t) xt) σ4 → + WritesIn st s s4 (ReplyFootW (st := st) Z m σ s E R Bf) → + L.mem s4 (affA t) = L.cpu s4 ∧ + (L.mem s4 (scA t) ≠ 0#u64 → L.mem s4 (scCoreA (L.mem s4 (scA t))) = L.cpu s4) ∧ + (∀ sc y n, L.mem s4 (scA t) = sc → sc ≠ 0#u64 → + σ4.kheap (scNameA m sc) = some (.schedContext y n) → scActive y = true) ∧ + faults.valid_timeout_handler st s4 t = ok (isEpCap x.tcbTimeoutHandler)) + {RT : S → A → State Unit → Prop} + (tmo : ∀ (s4 : S) (σ4 : State Unit) Q4 R4 stack4 (x : Tcb), + ReplyTmoMidA st Z m κ E R Bf s σ (faultClrA σ (specPtr t) xt) t (ReplyNwFA (st := st) Z m σ s R r t Bf) s4 σ4 + Q4 R4 stack4 x → + ∀ sc y k (badge : U64), L.mem s4 (scA t) = sc → sc ≠ 0#u64 → + σ4.kheap (scNameA m sc) = some (.schedContext y k) → badge.bv = y.scBadge → + isEpCap x.tcbTimeoutHandler = true → + faults.handle_timeout st ar s4 a t (.Timeout badge) ⦃ o => + ∃ σ', ((), σ') ∈ (fl4HandleTimeout (Z := Unit) (specPtr t) (.timeout badge.bv) σ4).1 ∧ RT o.1 o.2 σ' ⦄) : + ipc.do_reply_transfer st ar s a sender r grant ⦃ out => ∃ σ5, + ((), σ5) ∈ (fl4DoReplyTransfer (Z := Unit) (specPtr sender) (specPtr r) grant σ).1 ∧ ((∃ stack', out.2 = a ∧ + IpcSchedRelA st Z m κ out.1 σ5 ∧ + WritesIn st s out.1 (ReplyFootW (st := st) Z m σ s E R Bf) ∧ SSzA σ σ5 ∧ + EpStateRel Z.T E R Bf (L.mem out.1) σ5 ∧ + ReplyStackRelA (L.mem out.1) R Z.C σ5 (scNameA m) stack' ∧ + (∀ c ∈ Z.C, ∀ q ∈ stack' c, q ∈ stack c) ∧ + IpcKeepA (faultClrA σ (specPtr t) xt) σ5 ∧ SInvXA σ5 m n ∧ SIdleA σ5 m n ∧ SchedValidA σ5 ∧ + QSendA σ σ5 ∧ UFrA (fun _ => False) σ σ5 ∧ + WritesIn st s out.1 (ReplyNwFA (st := st) Z m σ s R r t Bf) ∧ + (∃ σ4, SIKA σ.idleThread (· = specPtr t) (· = specPtr t) true (faultClrA σ (specPtr t) xt) σ4 ∧ + σ5.kheap = σ4.kheap ∧ EpStateRel Z.T E R Bf (L.mem out.1) σ4 ∧ + (∀ x, σ4.kheap (specPtr t) = some (.tcb x) → x.tcbState = .restart ∨ x.tcbState = .inactive)) ∧ RepEndA σ σ5) ∨ + RT out.1 out.2 σ5) ⦄ := by + have hrt : xr.replyTcb = optRef (L.mem s (Endp.rTcbA r)) := by + obtain ⟨x, hx, h⟩ := HE.reply r hr + rw [hkr] at hx + cases hx + exact h + obtain ⟨hw0, hwt⟩ := optRef_some (hrt.symm.trans hb) + have hw : L.mem s (Endp.rTcbA r) = t := specPtr_inj hwt + have htr : obj.Reply.tcb st r s = ok t := + (eq_of_spec_fd (obj.Reply.tcb.spec (st := st) s r)).trans (by rw [hw]) + have hn : t ≠ 0#u64 := hw ▸ hw0 + have hrel : TcbRelE R (L.mem s) t xt := by + obtain ⟨x, hx, h⟩ := HE.tcb t ht + rw [hkt] at hx + cases hx + exact h + have hsr : obj.Tcb.state st t s = ok sched.TS_BLOCKED_ON_REPLY := by + have hsw' : L.mem s (stA t) = sched.TS_BLOCKED_ON_REPLY := by + apply UScalar.eq_of_val_eq + rw [hrel.st, hst] + unfold sched.TS_BLOCKED_ON_REPLY + rfl + exact (eq_of_spec_fd (P1.obj.Tcb.state.spec (st := st) s t)).trans (by rw [hsw']) + have hD : RqDomPrio σ := rqDomPrio_of_schedA H + have hLsw : NoSwA σ := hsw + refine do_reply_transfer_liveA + (M1 := fun s1 a1 σ1 => a1 = a ∧ replyRemove (Z := Unit) (specPtr t) (specPtr r) σ = ({((), σ1)}, False) ∧ + replyUnblockProgramA st s1 t ⦃ s2 => ∃ σ2, + (getTcbObjRef (Z := Unit) Tcb.tcbSchedContext (specPtr t) >>= + ifSporadicCurScTestRefillUnblockCheck) σ1 = ({((), σ2)}, False) ∧ + ReplyMidA st Z m κ E R Bf s σ r t s2 σ2 ∧ WritesIn st s s2 (RemWrA (st := st) Z m σ s R r t) ⦄) + (M2 := fun s2 a1 σ2 => a1 = a ∧ (∃ σ1, replyRemove (Z := Unit) (specPtr t) (specPtr r) σ = ({((), σ1)}, False) ∧ + (getTcbObjRef (Z := Unit) Tcb.tcbSchedContext (specPtr t) >>= + ifSporadicCurScTestRefillUnblockCheck) σ1 = ({((), σ2)}, False)) ∧ ReplyMidA st Z m κ E R Bf s σ r t s2 σ2 ∧ + WritesIn st s s2 (RemWrA (st := st) Z m σ s R r t)) + (M3 := fun s4 a4 σ4 ft fault => a4 = a ∧ fault = some f ∧ + (ft != fl4_abi.consts.fault.Timeout) = !isTimeoutFault f ∧ + ∃ σ1 σ2 σ3 σ3c stack' Q R' x y2 uc ts, + replyRemove (Z := Unit) (specPtr t) (specPtr r) σ = ({((), σ1)}, False) ∧ + (getTcbObjRef (Z := Unit) Tcb.tcbSchedContext (specPtr t) >>= + ifSporadicCurScTestRefillUnblockCheck) σ1 = ({((), σ2)}, False) ∧ + σ2.kheap (specPtr t) = some (.tcb y2) ∧ + ((), σ3) ∈ (asUser (Z := Unit) (specPtr t) (fun _ => ({((), uc)}, False)) σ2).1 ∧ + ((), σ3c) ∈ (threadSet (Z := Unit) (fun tcb => { tcb with tcbFault := none }) (specPtr t) σ3).1 ∧ + RqDomPrio σ3c ∧ (ts = .restart ∨ ts = .inactive) ∧ + ((), σ4) ∈ (setThreadState (Z := Unit) (specPtr t) ts σ3c).1 ∧ + (ts = .inactive → (∀ d p, specPtr t ∉ σ4.readyQueues d p) ∧ σ4.schedulerAction ≠ .switchThread (specPtr t)) ∧ + SchedSRB st { Z with Q := Q, R := R' } m κ s4 σ4 (specPtr t) ∧ StEqA Z.T (L.mem s4) σ4 ∧ + σ4.kheap (specPtr t) = some (.tcb x) ∧ x.tcbState = ts ∧ + (∀ d p, specPtr t ∉ σ4.readyQueues d p) ∧ specPtr t ∉ σ4.releaseQueue ∧ + EpStateRel Z.T E R Bf (L.mem s4) σ4 ∧ ReplyStackRelA (L.mem s4) R Z.C σ4 (scNameA m) stack' ∧ + WritesIn st s s4 (ReplyFootW (st := st) Z m σ s E R Bf) ∧ SSzA σ σ4 ∧ + WritesIn st s s4 (ReplyNwFA (st := st) Z m σ s R r t Bf)) + (R := fun s' a' σ' => (∃ stack', a' = a ∧ IpcSchedRelA st Z m κ s' σ' ∧ + WritesIn st s s' (ReplyFootW (st := st) Z m σ s E R Bf) ∧ SSzA σ σ' ∧ + EpStateRel Z.T E R Bf (L.mem s') σ' ∧ ReplyStackRelA (L.mem s') R Z.C σ' (scNameA m) stack' ∧ + (∀ c ∈ Z.C, ∀ q ∈ stack' c, q ∈ stack c) ∧ + IpcKeepA (faultClrA σ (specPtr t) xt) σ' ∧ SInvXA σ' m n ∧ SIdleA σ' m n ∧ SchedValidA σ' ∧ + QSendA σ σ' ∧ UFrA (fun _ => False) σ σ' ∧ + WritesIn st s s' (ReplyNwFA (st := st) Z m σ s R r t Bf) ∧ + (∃ σ4, SIKA σ.idleThread (· = specPtr t) (· = specPtr t) true (faultClrA σ (specPtr t) xt) σ4 ∧ + σ'.kheap = σ4.kheap ∧ EpStateRel Z.T E R Bf (L.mem s') σ4 ∧ + (∀ x, σ4.kheap (specPtr t) = some (.tcb x) → x.tcbState = .restart ∨ x.tcbState = .inactive)) ∧ RepEndA σ σ') ∨ + RT s' a' σ') + hkr hb hkt hst htr hn hsr ?_ ?_ ?_ ?_ + · refine WP.spec_mono (replyRemoveThenUnblockQA (ar := ar) a H P PE HE HS hr ht hkt hkr hb hst hq cov hsc0 hlocal + unb hoff hSt hV hC hNative) ?_ + rintro out ⟨σ1, h1, ha, hU⟩ + exact ⟨σ1, h1, ha, h1, hU⟩ + · rintro s1 a1 σ1 ⟨ha, h1, hU⟩ + refine WP.spec_mono hU ?_ + rintro s2 ⟨σ2, h2, hM⟩ + exact ⟨σ2, h2, ha, ⟨σ1, h1, h2⟩, hM⟩ + · -- the fault body + rintro s2 a1 σ2 ft ⟨ha1, ⟨σ1, h1, h2⟩, hM⟩ hft + obtain ⟨⟨Q1, R1, stack', H2, E2, S2, -, keep, hSt2, hV2, hC2, hN2, ⟨y2, hy2, hy2s⟩, wm, szm⟩, wn⟩ := id hM + have m1 : ((), σ1) ∈ (replyRemove (Z := Unit) (specPtr t) (specPtr r) σ).1 := by + rw [h1]; exact Set.mem_singleton _ + have m2 : ((), σ2) ∈ ((getTcbObjRef (Z := Unit) Tcb.tcbSchedContext (specPtr t) >>= + ifSporadicCurScTestRefillUnblockCheck) σ1).1 := by + rw [h2]; exact Set.mem_singleton _ + have hrel2 : TcbRelE R (L.mem s2) t y2 := by + obtain ⟨x, hx, h⟩ := E2.tcb t ht + rw [hy2] at hx + cases hx + exact h + obtain ⟨ys, st1, sc1, hys, eys⟩ := keep.2.2 _ xs hks + obtain ⟨yt, st2, sc2, hyt, eyt⟩ := keep.2.2 _ xt hkt + have e2 : yt = y2 := by + rw [hy2] at hyt + exact (KernelObject.tcb.inj (Option.some.inj hyt)).symm + subst e2 + have hf2 : yt.tcbFault = some f := by rw [eyt]; exact hfr + have hft0 : ft ≠ fl4_abi.consts.fault.NullFault := fun h0 => by + have h00 : L.mem s2 (tFaultA t) = 0#u64 := by rw [← hft, h0]; unfold fl4_abi.consts.fault.NullFault; rfl + have := hrel2.fault0.1 h00 + rw [hf2] at this + cases this + have hbne : (ft != fl4_abi.consts.fault.Timeout) = !isTimeoutFault f := by + rw [hft] + exact faultTBneA hrel2 hf2 + have hokS : FaultReplyOKA (L := L) s2 t + (unat (dataToMessageInfo (ys.tcbArch.tcbContext.userRegs msg_info_register)).miLength) f := by + obtain ⟨-, hv2⟩ := hwords s2 wn + have e1 : yt.tcbArch = xt.tcbArch := by rw [eyt] + have e3 : ys.tcbArch = xs.tcbArch := by rw [eys] + rw [e3] + exact ⟨fun h => by rw [hv2]; exact hok.trap h⟩ + have hvcS : yt.tcbArch.tcbVcpu ≠ none ↔ L.mem s2 (tVcpuA t) ≠ 0#u64 := by + have e1 : yt.tcbArch = xt.tcbArch := by rw [eyt] + rw [e1, (hwords s2 wn).1] + exact hvc + have hz2 : SSzA σ σ2 := szm + refine WP.spec_mono (replyFaultBody_refinesA (st := st) (ar := ar) (s := s2) (a := a1) (σ := σ2) (sender := sender) + (t := t) (grant := grant) (ft := ft) (fault := f) hft0 + (M := fun s3 a3 σ3 => a3 = a1 ∧ ∃ uc, σ3 = updK σ2 (specPtr t) + (.tcb { yt with tcbArch := archTcbContextSet uc yt.tcbArch }) ∧ + EpStateRel Z.T E R Bf (L.mem s3) σ3 ∧ IpcSchedRelA st Z m κ s3 σ3 ∧ + WritesIn st s2 s3 (fun b => ∃ r : Register, b = Endp.regA t (regIdxA r)) ∧ + WritesIn st s2 s3 (EWrite Z.T E R Bf)) + (R := fun s4 a4 σ4 => a4 = a1 ∧ ∃ σ3 σ3c Q R' x uc ts, + σ3 = updK σ2 (specPtr t) (.tcb { yt with tcbArch := archTcbContextSet uc yt.tcbArch }) ∧ + ((), σ3c) ∈ (threadSet (Z := Unit) (fun tcb => { tcb with tcbFault := none }) (specPtr t) σ3).1 ∧ + RqDomPrio σ3c ∧ (ts = .restart ∨ ts = .inactive) ∧ + ((), σ4) ∈ (setThreadState (Z := Unit) (specPtr t) ts σ3c).1 ∧ + (ts = .inactive → (∀ d p, specPtr t ∉ σ4.readyQueues d p) ∧ σ4.schedulerAction ≠ .switchThread (specPtr t)) ∧ + SchedSRB st { Z with Q := Q, R := R' } m κ s4 σ4 (specPtr t) ∧ StEqA Z.T (L.mem s4) σ4 ∧ + σ4.kheap (specPtr t) = some (.tcb x) ∧ x.tcbState = ts ∧ + (∀ d p, specPtr t ∉ σ4.readyQueues d p) ∧ specPtr t ∉ σ4.releaseQueue ∧ + EpStateRel Z.T E R Bf (L.mem s4) σ4 ∧ ReplyStackRelA (L.mem s4) R Z.C σ4 (scNameA m) stack' ∧ + WritesIn st s s4 (ReplyFootW (st := st) Z m σ s E R Bf) ∧ SSzA σ σ4 ∧ + WritesIn st s s4 (ReplyNwFA (st := st) Z m σ s R r t Bf)) + ?_ ?_) ?_ + · refine WP.spec_mono (handle_fault_reply_regsA (ar := ar) + (schedOpsEAOfPlacementA (st := st) (m := m) (κ := κ) P) PE s2 a1 σ2 E2 ⟨_, _, H2⟩ hSt2 hC2 hN2 ht hs hne + ((H2.toSchedSRN.thT sender).2 hs) hys hy2 hf2 hokS hvcS) ?_ + rintro ⟨restart, s3, a3⟩ ⟨ha3, x', ⟨uc, hx'⟩, hpay, E3, SR3, wreg, wE⟩ + refine ⟨updK σ2 (specPtr t) (.tcb x'), x', by rw [updK_kheap, Function.update_self], + ⟨ha3, uc, by rw [hx'], E3, SR3, wreg, wE⟩, ?_⟩ + show (restart, _) ∈ (faultPayloadA (specPtr sender) (specPtr t) f σ2).1 + rw [hpay] + exact Set.mem_singleton _ + · rintro s3 a3 σ3 x restart ⟨ha3, uc, e3, E3, ⟨Q3, R3, H3⟩, wreg, wE⟩ hk3 + rw [show fl4_abi.consts.fault.NullFault = 0#u64 by unfold fl4_abi.consts.fault.NullFault; rfl] + have hxx : x = { yt with tcbArch := archTcbContextSet uc yt.tcbArch } := by + rw [e3, updK_kheap, Function.update_self] at hk3 + exact (KernelObject.tcb.inj (Option.some.inj hk3)).symm + have hxin : x.tcbState = .inactive := by rw [hxx]; exact hy2s + have HB := H3.clearFaultA ht hk3 + have EC := E3.clearFaultA PE ht hk3 + have hstC : StEqA Z.T (L.mem (L.wr s3 (faultA t) 0#u64)) (faultClrA σ3 (specPtr t) x) := + fun u hu y hy => (EC.at_tcb hu hy).st + have hkc : (faultClrA σ3 (specPtr t) x).kheap (specPtr t) = some (.tcb { x with tcbFault := none }) := + faultClrA_at + have hcls : ¬ inClassRSc x.tcbState none := by rw [hxin]; simp [inClassRSc, runnable] + have HA3c := HB.toA_ncA hkc hcls + have hnq3 := HA3c.notQueued hkc (by show runnable x.tcbState = false; rw [hxin]; rfl) + have hD3 : RqDomPrio (faultClrA σ3 (specPtr t) x) := rqDomPrio_of_schedA HA3c + have h3c : ((), faultClrA σ3 (specPtr t) x) ∈ + (threadSet (Z := Unit) (fun tcb => { tcb with tcbFault := none }) (specPtr t) σ3).1 := by + rw [threadSet_eqSc _ hk3] + exact Set.mem_singleton _ + have h3m : ((), σ3) ∈ (asUser (Z := Unit) (specPtr t) (fun _ => ({((), uc)}, False)) σ2).1 := by + rw [e3, asUser_writeA hy2 (f := fun _ => ({((), uc)}, False)) (u := uc) rfl] + exact Set.mem_singleton _ + have wc : WritesIn st s3 (L.wr s3 (faultA t) 0#u64) (· = faultA t) := writesIn_wr (a_fault (PE.tcb t ht)).1 + refine WP.spec_mono (fault_stateRunA HB hstC ht hkc hxin hnq3 restart) ?_ + rintro s4 ⟨σ4, he, HB4, hst4, hk4, hm4, q1, q2, ws⟩ + have h4m : ((), σ4) ∈ (setThreadState (Z := Unit) (specPtr t) (if restart then .restart else .inactive) + (faultClrA σ3 (specPtr t) x)).1 := by + rw [he] + exact Set.mem_singleton _ + have hts : (if restart then ThreadState.restart else .inactive) = .restart ∨ + (if restart then ThreadState.restart else .inactive) = .inactive := by cases restart <;> simp + have hsw4 := replyFault_noSwA (m := m) (n := n) hkt hst hL hsw m1 m2 h3m h3c h4m + have hx4 : σ4.kheap (specPtr t) = some (.tcb { ({ x with tcbFault := none } : Tcb) with + tcbState := if restart then .restart else .inactive }) := by rw [hk4, Function.update_self] + have hker : ∀ p, p ≠ specPtr t → σ4.kheap p = σ2.kheap p := by + intro p hp + rw [hk4, Function.update_of_ne hp, faultClrA_ne hp, e3, updK_kheap, Function.update_of_ne hp] + have hz23 : SSzA σ2 σ3 := by rw [e3]; exact ssz_updK_tcb _ _ _ + have hz33 : SSzA σ3 (faultClrA σ3 (specPtr t) x) := ssz_updK_tcb _ _ _ + have hz34 : SSzA (faultClrA σ3 (specPtr t) x) σ4 := + (SPresInst.pres (m := setThreadState (Z := Unit) (specPtr t) (if restart then .restart else .inactive)) + _ _ h4m).2.1 + have E4 : EpStateRel Z.T E R Bf (L.mem s4) σ4 := by + cases restart + · exact epRel_stateA (Z := { Z with Q := Q3, R := R3 }) HB.toSchedSRN P PE EC ht hkc hxin + (ts := .inactive) (by decide) hk4 hm4 (hst4 t ht _ hx4) ws + · exact epRel_stateA (Z := { Z with Q := Q3, R := R3 }) HB.toSchedSRN P PE EC ht hkc hxin + (ts := .restart) (by decide) hk4 hm4 (hst4 t ht _ hx4) ws + have hid : (if restart then ThreadState.restart else .inactive) = .inactive → + (∀ d p, specPtr t ∉ σ4.readyQueues d p) ∧ σ4.schedulerAction ≠ .switchThread (specPtr t) := + fun _ => ⟨fun d p => by rw [q1]; exact hnq3.1 d p, fun e => (hsw4 _ e).2 rfl⟩ + have hnr : (∀ d p, specPtr t ∉ σ4.readyQueues d p) := fun d p => by rw [q1]; exact hnq3.1 d p + have hnl : specPtr t ∉ σ4.releaseQueue := by rw [q2]; exact hnq3.2 + rw [show updKSc σ3 (specPtr t) (.tcb { x with tcbFault := none }) = faultClrA σ3 (specPtr t) x from rfl] + generalize (if restart = true then ThreadState.restart else ThreadState.inactive) = ts at h4m hts hx4 hid + refine ⟨σ4, h4m, ha3, σ3, faultClrA σ3 (specPtr t) x, Q3, R3, + { ({ x with tcbFault := none } : Tcb) with tcbState := ts }, uc, ts, e3, h3c, hD3, hts, h4m, + hid, HB4, hst4, hx4, rfl, hnr, hnl, E4, ?_, ?_, ?_, ?_⟩ + · have link : ∀ q ∈ R, ∀ b, Aligned b → q.val < b.val → b.val < q.val + 32 → + L.mem s4 b = L.mem s2 b := by + intro q hq b ha hl hh + have off := replyLinkOffEp H2.toSchedSRN P PE hq hl hh + have off3 := replyLinkOffEp H3.toSchedSRN P PE hq hl hh + have offB := replyLinkOffEp HB.toSchedSRN P PE hq hl hh + have a3 : L.mem s3 b = L.mem s2 b := wE.2.2 b ha fun h => off h.foot + have a3c : L.mem (L.wr s3 (faultA t) 0#u64) b = L.mem s3 b := + wc.2.2 b ha fun h => off3 (Or.inr (Or.inr (Or.inl ⟨t, ht, Or.inl (by + rw [h]; exact (a_fault (PE.tcb t ht)).2)⟩))) + rw [← a3, ← a3c] + refine ws.2.2 b ha fun h => ?_ + rcases h with h | h + · refine offB (Or.inr (Or.inr (Or.inl ⟨t, ht, Or.inl ?_⟩))) + rw [h]; exact (a_st (PE.tcb t ht)).2 + · refine (replyWordOffSchedA (b := b) HB.toSchedSRN P hq (by omega) (by omega)).2 ?_ + rw [h] + exact Or.inr (Or.inl (HB.toSchedSRN.node_sks HB.toSchedSRN.sch.cpuCs (by simp [layout.node.SCHED_ACTION]))) + refine S2.frame (fun q hq => ?_) (fun c _ y k hy => ?_) (fun q hq y' hy' => ?_) + · have v1 := replyNextPlaceA (Z := Z) PE hq + have v2 := replyPrevPlaceA (Z := Z) PE hq + exact ⟨link q hq _ v1.1 (by omega) (by omega), link q hq _ v2.1 (by omega) (by omega)⟩ + · have npt : scNameA m c ≠ specPtr t := fun e => by rw [e, hy2] at hy; cases hy + exact ⟨y, k, by rw [hker _ npt]; exact hy, rfl⟩ + · have npt : specPtr q ≠ specPtr t := fun e => by rw [e, hx4] at hy'; cases hy' + exact ⟨y', by rw [← hker _ npt]; exact hy', rfl⟩ + · have wa3 : ∀ b, (b = stA t ∨ b = L.nodeAt (L.wr s3 (faultA t) 0#u64) (L.cpu (L.wr s3 (faultA t) 0#u64)) + layout.node.SCHED_ACTION) → ReplyFootW (st := st) { Z with Q := Q3, R := R3 } m + (faultClrA σ3 (specPtr t) x) (L.wr s3 (faultA t) 0#u64) E R Bf b := by + rintro b (h | h) + · refine Or.inl (Or.inr (Or.inr (Or.inl ⟨t, ht, Or.inl ?_⟩))) + have hfit := (PE.tcb t ht).fits + have v : (stA t).val = t.val + 3328 := by + rw [stA_eq, tw_val (PE.tcb t ht) (by simp)]; simp + rw [h] + exact ⟨by omega, by unfold TWOffN; omega⟩ + · refine Or.inr (Or.inl (Or.inr (Or.inl ?_))) + rw [h] + exact HB.toSchedSRN.node_sks HB.toSchedSRN.sch.cpuCs (by simp [layout.node.SCHED_ACTION]) + have wcF : ∀ b, b = faultA t → + ReplyFootW (st := st) { Z with Q := Q3, R := R3 } m σ3 s3 E R Bf b := by + intro b h + refine Or.inl (Or.inr (Or.inr (Or.inl ⟨t, ht, Or.inl ?_⟩))) + have hv : (faultA t).val = t.val + 3376 := field_val (PE.tcb t ht) (K := 3376#u64) (by simp) + rw [h] + unfold TWF + rw [hv] + exact ⟨by omega, by unfold TWOffN; omega⟩ + have w3' : WritesIn st s2 s3 (ReplyFootW (st := st) { Z with Q := Q1, R := R1 } m σ2 s2 E R Bf) := + wE.mono fun b h => Or.inl h + have w23c := ReplyFootW.trans (Z := { Z with Q := Q1, R := R1 }) (Z' := { Z with Q := Q3, R := R3 }) + rfl rfl w3' hz23 (wc.mono wcF) + have w24 := ReplyFootW.trans (Z := { Z with Q := Q1, R := R1 }) (Z' := { Z with Q := Q3, R := R3 }) + rfl rfl w23c (hz23.trans hz33) (ws.mono wa3) + exact ReplyFootW.trans (Z' := { Z with Q := Q1, R := R1 }) rfl rfl wm szm w24 + · exact szm.trans (hz23.trans (hz33.trans hz34)) + · have wcomp := (((wn.trans (wreg.mono fun b ⟨r, h⟩ => (⟨r, by rw [h]⟩ : RcvRegA t b))).trans wc).trans ws) + refine wcomp.mono fun b hb => ?_ + rcases hb with ((h | h) | h) | h | h + · exact Or.inl (Or.inl h) + · exact Or.inl (Or.inr (Or.inl h)) + · exact Or.inr h + · exact Or.inl (Or.inl (Or.inr (Or.inr (Or.inl h)))) + · refine Or.inl (Or.inl (Or.inr (Or.inr (Or.inr (mwr_back (Z := Z) + (Z' := { Z with Q := Q3, R := R3 }) rfl rfl (szm.trans (hz23.trans hz33)) + (wc.1.trans (wE.1.trans wm.1)) (Or.inr (Or.inl ?_))))))) + rw [h] + exact HB.toSchedSRN.node_sks HB.toSchedSRN.sch.cpuCs (by simp [layout.node.SCHED_ACTION]) + · rintro ⟨s4, a4⟩ ⟨σ4, hm, ha4, σ3, σ3c, Q, R', x, uc, ts, e3, h3c, hD3, hts, h4, hid, HB, hst4, hx4, hxs, + hnq4, hnr4, E4, S4, w04, sz04, w04n⟩ + have h3m : ((), σ3) ∈ (asUser (Z := Unit) (specPtr t) (fun _ => ({((), uc)}, False)) σ2).1 := by + rw [e3, asUser_writeA hy2 (f := fun _ => ({((), uc)}, False)) (u := uc) rfl] + exact Set.mem_singleton _ + exact ⟨σ4, yt, hy2, Or.inr ⟨f, hf2, hm⟩, ha4.trans ha1, hf2, hbne, σ1, σ2, σ3, σ3c, stack', Q, R', x, yt, uc, + ts, h1, h2, hy2, h3m, h3c, hD3, hts, h4, hid, HB, hst4, hx4, hxs, hnq4, hnr4, E4, S4, w04, sz04, w04n⟩ + · rintro s4 a4 σ4 ft fault ⟨ha4, hf0, hbne, σ1, σ2, σ3, σ3c, stack', Q, R', x, y2, uc, ts, h1, h2, hy2, h3, h3c, + hD3, hts, h4, hid, HB, hst4, hx4, hxs, hnqs, hnrel, E4, S4, w04, sz04, w04n⟩ + subst hf0 + have m1 : ((), σ1) ∈ (replyRemove (Z := Unit) (specPtr t) (specPtr r) σ).1 := by + rw [h1]; exact Set.mem_singleton _ + have m2 : ((), σ2) ∈ ((getTcbObjRef (Z := Unit) Tcb.tcbSchedContext (specPtr t) >>= + ifSporadicCurScTestRefillUnblockCheck) σ1).1 := by + rw [h2]; exact Set.mem_singleton _ + obtain ⟨S14, F4, B4⟩ := replyFault_midA hkt hst hL.sinv hL.sidle hSV hD hA m1 m2 h3 h3c hts h4 + obtain ⟨hon, hcpu, hact, hv⟩ := tailS s4 σ4 Q R' x HB hx4 B4 S14 w04 + have hcls : runnable x.tcbState = true ∨ ¬ inClassRSc x.tcbState x.tcbFault := by + rcases hts with h | h + · exact Or.inl (by rw [hxs, h]; rfl) + · exact Or.inr (by rw [hxs, h]; simp [inClassRSc, runnable]) + obtain ⟨RS4, QS4, D4, U4⟩ := replyFault_midXA hkt hst hL.sidle hL.sinv m1 m2 h3 h3c hD3 hts h4 + have tmo4 : runnable x.tcbState = true → _ := fun hrun => tmo s4 σ4 Q R' stack' x + { sr := HB, stq := hst4, kx := hx4, run := hrun, nq := hnqs, nrel := hnrel, ep := E4, rs := S4, + w := w04, wn := w04n, sz := sz04, clr := Or.inr ⟨xt, hkt, rfl⟩, keep := S14, fia := F4, bnd := B4, dom := D4, + qs := QS4, rsub := RS4, ufr := ⟨xt, hkt, ⟨U4.1, U4.2.1, fun a _ => U4.2.2 a id⟩⟩, + nsw := replyFault_noSwA hkt hst hL hLsw m1 m2 h3 h3c h4, bl := replyFault_blA m1 m2 h3 h3c h4 } + rw [← ha4] at tmo4 + have key := replyTailExTA (st := st) (ar := ar) (a := a4) (ft := ft) + (R := fun s' a' σ' => (¬ ReplyTmoCondA σ4 (specPtr t) (some f) ∧ a' = a4 ∧ + (∃ Q'' R'', SchedSRA st { Z with Q := Q'', R := R'' } m κ s' σ') ∧ + σ'.kheap = σ4.kheap ∧ WritesIn st s4 s' (MWrA (st := st) { Z with Q := Q, R := R' } m σ4 s4)) ∨ RT s' a' σ') + HB hst4 ht hon (hmb a4) (some f) hx4 hcls hidle hact hv hbne hcpu hnqs hnrel + (fun hnt H' => Or.inl ⟨hnt, rfl, ⟨_, _, H'⟩, rfl, WritesIn.refl _ _⟩) + (fun hnt _ _ Q' H' hk w => Or.inl ⟨hnt, rfl, ⟨Q', R', H'⟩, hk, w.mono fun _ hb => Or.inr hb⟩) + (fun hnt _ _ R'' H' hk _ w => Or.inl ⟨hnt, rfl, ⟨Q, R'', H'⟩, hk, w.mono fun b hb => Or.inr (by + rcases hb with hb | hb + · exact HB.toSchedSRN.qfoot_swr (fun v hv => (List.mem_cons.1 hv).elim (· ▸ ht) + (fun e => (HB.toSchedSRN.relAct v e).1)) HB.toSchedSRN.sch.cpuCs ht hb + · rw [hb]; exact HB.toSchedSRN.repr_swr)⟩) + (fun sc y k badge h1 h2 h3 h4 h5 _ hrun => WP.spec_mono (tmo4 hrun sc y k badge h1 h2 h3 h4 h5) + fun _ ⟨σ', hm, hR⟩ => ⟨σ', hm, Or.inr hR⟩) + refine WP.spec_mono key ?_ + rintro out ⟨σ5, hm, hR⟩ + refine ⟨σ5, hm, ?_⟩ + rcases hR with ⟨hnt, ha5, ⟨Q5, R5, H5⟩, hk5, w5⟩ | hRT + swap + · exact Or.inr hRT + refine Or.inl ?_ + have m5 : ((), σ5) ∈ (replyTailSpecA (specPtr t) (some f) σ4).1 := hm + obtain ⟨K, I5, D5, V5, Qs, Rs, U5, -, mach⟩ := replyFault_specKeepA hkt hst hback hL.sinv hL.sidle hSV hD hA + hsw hnq hcur m1 m2 h3 h3c hD3 hts h4 (fun z hz => by rw [hx4] at hz; cases hz; exact hxs) hid + (fun x' p y k hx' hp hy ha => by + rw [fl4RefillSufficient_eq_l4v hag] + exact HB.inv.suff hy ha (HB.inv.tcbScNotIdle _ x' p hx' hp hidle)) m5 hnt + have E5 : EpStateRel Z.T E R Bf (L.mem out.1) σ5 := by + refine E4.frame PE (fun b ha hb => w5.2.2 b ha (endpFootOffWrite HB.toSchedSRN P hb)) ⟨fun p y h => ?_, + fun p y h => ⟨y, ?_, rfl⟩, fun p y h => ⟨y, ?_, rfl, rfl, rfl, rfl, rfl⟩, mach⟩ + · rw [hk5]; exact h + · rw [hk5]; exact h + · rw [hk5]; exact h + have S5 : ReplyStackRelA (L.mem out.1) R Z.C σ5 (scNameA m) stack' := by + refine S4.frame (fun q hq => ?_) (fun c _ y k h => ⟨y, k, by rw [hk5]; exact h, rfl⟩) + (fun q _ y h => ⟨y, by rw [← hk5]; exact h, rfl⟩) + have link : ∀ b, Aligned b → q.val < b.val → b.val < q.val + 32 → L.mem out.1 b = L.mem s4 b := + fun b ha hl hh => w5.2.2 b ha (replyWordOffSchedA (b := b) HB.toSchedSRN P hq (by omega) (by omega)).2 + have v1 := replyNextPlaceA (Z := Z) PE hq + have v2 := replyPrevPlaceA (Z := Z) PE hq + exact ⟨link _ v1.1 (by omega) (by omega), link _ v2.1 (by omega) (by omega)⟩ + have sub : ∀ c ∈ Z.C, ∀ q ∈ stack' c, q ∈ stack c := by + intro c hc q hq + obtain ⟨⟨y5, n5, hy5, l5⟩, -⟩ := S5.stackRel c hc + obtain ⟨⟨y0, n0, hy0, l0⟩, -⟩ := HS.stackRel c hc + obtain ⟨y, k, hy, sb⟩ := Rs _ _ _ hy5 + rw [hy0] at hy + cases hy + have := sb (specPtr q) (by rw [l5]; exact List.mem_map_of_mem hq) + rw [l0] at this + obtain ⟨q', hq', e⟩ := List.mem_map.1 this + rwa [specPtr_inj e] at hq' + have w05 : WritesIn st s out.1 (ReplyFootW (st := st) Z m σ s E R Bf) := + ReplyFootW.trans (Z' := { Z with Q := Q, R := R' }) rfl rfl w04 sz04 (w5.mono fun _ h => Or.inr (Or.inl h)) + have sz45 : SSzA σ4 σ5 := SSzA.of_kheap hk5 + have w05n : WritesIn st s out.1 (ReplyNwFA (st := st) Z m σ s R r t Bf) := + (w04n.trans w5).mono fun b hb => hb.elim id fun h => Or.inl (Or.inl (Or.inr (Or.inr (Or.inr + (mwr_back (Z := Z) (Z' := { Z with Q := Q, R := R' }) rfl rfl sz04 w04.1 h))))) + exact ⟨stack', ha5.trans ha4, ⟨Q5, R5, H5⟩, w05, sz04.trans sz45, E5, S5, sub, K, I5, D5, V5, Qs, U5, w05n, ⟨σ4, + S14, hk5, E4.frame PE (fun b ha hb => w5.2.2 b ha (endpFootOffWrite HB.toSchedSRN P hb)) + ⟨fun _ _ h => h, fun _ y h => ⟨y, h, rfl⟩, fun _ y h => ⟨y, h, rfl, rfl, rfl, rfl, rfl⟩, rfl⟩, + fun z hz => by + rw [hx4] at hz + cases hz + rcases hts with h | h + · exact Or.inl (hxs.trans h) + · exact Or.inr (hxs.trans h)⟩, + RepEndA.ofTail S14 rfl (by simp [faultClrA, Function.update_of_ne (Ne.symm hcur)]) hcur hsw + (fun u e => (replyFault_noSwA (m := m) (n := n) hkt hst hL hLsw m1 m2 h3 h3c h4 u e).2) + (replyFault_blA m1 m2 h3 h3c h4) hx4 B4 hnt m5 hk5⟩ + + +set_option maxHeartbeats 1000000 in +/-- **The live Reply to a faulted caller with the record's exports** (module docstring). -/ +theorem do_reply_transfer_keepFA {s : S} {a : A} {σ : State Unit} {stack : U64 → List U64} {n : Nat} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {sender r t : U64} {grant : Bool} (hr : r ∈ R) (hs : sender ∈ Z.T) (ht : t ∈ Z.T) + (hne : sender ≠ t) + {xt xs : Tcb} {xr : Reply} {f : Fault} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hkr : σ.kheap (specPtr r) = some (.reply xr)) (hb : xr.replyTcb = some (specPtr t)) + (hst : xt.tcbState = .blockedOnReply (specPtr r)) + (hks : σ.kheap (specPtr sender) = some (.tcb xs)) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) + (cov : ScReplyCovA Z.C m σ (specPtr r)) + (hsc0 : ∀ sc ∈ Z.C, (stack sc).head? = some r → sc ≠ 0#u64) + (hlocal : ∀ sc ∈ Z.C, (stack sc).head? = some r → xt.tcbSchedContext = none → + ReplyPopLocalQA (st := st) (ar := ar) (Z := Z) (m := m) a s σ sc t r) + (unb : ∀ s1 σ1 Q1 R1, SchedSRA st { Z with Q := Q1, R := R1 } m κ s1 σ1 → RemKeepA σ σ1 → + replyRemove (Z := Unit) (specPtr t) (specPtr r) σ = ({((), σ1)}, False) → + (L.mem s1 (scA t) ≠ 0#u64 → L.mem s1 (scCoreA (L.mem s1 (scA t))) = L.cpu s1) ∧ + (specPtr t ∈ σ1.releaseQueue → ∀ y n, + σ1.kheap (scNameA m (L.mem s1 (scA t))) = some (.schedContext y n) → + refillReady σ1.curTime (refillHd y) = false)) + (hoff : ∀ y, IsSlotA Z.th y → ¬ EWrite Z.T E R Bf y ∧ ¬ EWrite Z.T E R Bf (wa y 8#u64)) + (hSt : StateRelA Z.th (L.mem s) σ) (hV : SlotsValidA Z.th (L.mem s)) + (hC : BufCover st Z.T Bf s σ) (hNative : BufNativeA Z.T (L.mem s)) + (hag : fl4MinBudget = MIN_BUDGET) (hmb : ∀ a', Fl4MinBudgetAgreeA ar a') + (hidle : ∀ c < fl4A64Platform.numNodes, specPtr t ≠ m.idleThreadOf c) + (hfr : xt.tcbFault = some f) (hto : isEpCap xt.tcbTimeoutHandler = false) + (hok : FaultReplyOKA (L := L) s t + (unat (dataToMessageInfo (xs.tcbArch.tcbContext.userRegs msg_info_register)).miLength) f) + (hvc : xt.tcbArch.tcbVcpu ≠ none ↔ L.mem s (tVcpuA t) ≠ 0#u64) + (hwords : ∀ s2, WritesIn st s s2 (RemWrA (st := st) Z m σ s R r t) → + L.mem s2 (tVcpuA t) = L.mem s (tVcpuA t) ∧ L.mem s2 (tFw1A t) = L.mem s (tFw1A t)) + (hback : ∀ sc, xt.tcbSchedContext = some sc → ∀ y k, σ.kheap sc = some (.schedContext y k) → + y.scTcb = some (specPtr t)) + (hL : LoopInvA σ m n) (hSV : SchedValidA σ) (hA : ActRepA σ) + (hsw : ∀ u, σ.schedulerAction ≠ .switchThread u) (hnq : ∀ d p, specPtr t ∉ σ.readyQueues d p) + (hcur : specPtr t ≠ σ.curThread) + (tailS : ∀ (s4 : S) (σ4 : State Unit) Q4 R4 (x : Tcb), + SchedSRB st { Z with Q := Q4, R := R4 } m κ s4 σ4 (specPtr t) → + σ4.kheap (specPtr t) = some (.tcb x) → BoundActive (specPtr t) σ4 → + SIKA σ.idleThread (· = specPtr t) (· = specPtr t) true (faultClrA σ (specPtr t) xt) σ4 → + WritesIn st s s4 (ReplyFootW (st := st) Z m σ s E R Bf) → + L.mem s4 (affA t) = L.cpu s4 ∧ + (L.mem s4 (scA t) ≠ 0#u64 → L.mem s4 (scCoreA (L.mem s4 (scA t))) = L.cpu s4) ∧ + (∀ sc y n, L.mem s4 (scA t) = sc → sc ≠ 0#u64 → + σ4.kheap (scNameA m sc) = some (.schedContext y n) → scActive y = true) ∧ + faults.valid_timeout_handler st s4 t = ok (isEpCap x.tcbTimeoutHandler)) : + ipc.do_reply_transfer st ar s a sender r grant ⦃ out => ∃ σ5 stack', + ((), σ5) ∈ (fl4DoReplyTransfer (Z := Unit) (specPtr sender) (specPtr r) grant σ).1 ∧ out.2 = a ∧ + IpcSchedRelA st Z m κ out.1 σ5 ∧ + WritesIn st s out.1 (ReplyFootW (st := st) Z m σ s E R Bf) ∧ SSzA σ σ5 ∧ + EpStateRel Z.T E R Bf (L.mem out.1) σ5 ∧ + ReplyStackRelA (L.mem out.1) R Z.C σ5 (scNameA m) stack' ∧ + (∀ c ∈ Z.C, ∀ q ∈ stack' c, q ∈ stack c) ∧ + IpcKeepA (faultClrA σ (specPtr t) xt) σ5 ∧ SInvXA σ5 m n ∧ SIdleA σ5 m n ∧ SchedValidA σ5 ∧ + QSendA σ σ5 ∧ UFrA (fun _ => False) σ σ5 ∧ + WritesIn st s out.1 (ReplyNwFA (st := st) Z m σ s R r t Bf) ∧ + (∃ σ4, SIKA σ.idleThread (· = specPtr t) (· = specPtr t) true (faultClrA σ (specPtr t) xt) σ4 ∧ + σ5.kheap = σ4.kheap ∧ EpStateRel Z.T E R Bf (L.mem out.1) σ4 ∧ + (∀ x, σ4.kheap (specPtr t) = some (.tcb x) → x.tcbState = .restart ∨ x.tcbState = .inactive)) ∧ RepEndA σ σ5 ⦄ := by + refine WP.spec_mono (do_reply_transfer_keepFTA (RT := fun _ _ _ => False) H P PE HE HS hr hs ht hne hkt hkr hb hst hks + hq cov hsc0 hlocal unb hoff hSt hV hC hNative hag hmb hidle hfr hok hvc hwords hback hL hSV hA hsw hnq hcur tailS + ?_) ?_ + · intro s4 σ4 Q4 R4 stack4 x M sc y k badge _ _ _ _ he + exfalso + obtain ⟨z, hz, g⟩ := M.keep.tcbF (faultClrA_at (σ := σ) (t := specPtr t) (x := xt)) + rw [M.kx] at hz + cases hz + have one : ∀ z : Tcb, tcbCnodeMap z (tcbCnodeIndex 4) = some z.tcbTimeoutHandler := fun z => by + simp [tcbCnodeMap, tcbIdx_eq, tcbStage2Slot, tcbHighVSpaceSlot] + rw [Option.some.inj ((one x).symm.trans ((congrFun g.caps (tcbCnodeIndex 4)).trans + (one { xt with tcbFault := none }))), hto] at he + cases he + · rintro out ⟨σ5, hm, ⟨stack', rest⟩ | h⟩ + · exact ⟨σ5, stack', hm, rest⟩ + · exact h.elim +end Store + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3ReplyMidA.lean b/proofs/Fl4ProofsA64/P3/Stage3ReplyMidA.lean new file mode 100644 index 000000000..dfbe486d2 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ReplyMidA.lean @@ -0,0 +1,454 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 3): **the one-word reply after a body step** (lead ruling 04:28Z (a)). + +**`JStage3A.replyOneMidA`**: `JStage3A.replyOneA` (`P3/Stage3ReplyOneA`, unchanged) started from the state between an +operation body and `vspace.reply1`, at which only the relations hold: the record `J` is at the base state (after the +caller's Restart); the body's writes are Stage-3 write-set words of the base (`wb`), its spec step a scheduler frame +(`Fb`) keeping the IPC fields (`Kb`) and the TCB words `of_ipcStepA` reads (`twb`); the relations, `BlkOK` and the +current thread's SC facts are given at the middle state. The record is rebuilt once, from `J`, at the final state +(`of_ipcStepA` with the composed footprint), so no record facts are needed at the middle state. Used by Consumed; +SetFlags and YieldTo's reply arm are its other intended users. + +No named premise (those of `fin`'s producer). New module. +-/ +import Fl4ProofsA64.P3.Stage3Lift3QA +import Fl4ProofsA64.P3.Stage3ReplyOneA +import Fl4ProofsA64.P3.Stage3PartialA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-- `IpcKeepA` composes. -/ +theorem IpcKeepA.transA {σ1 σ2 σ3 : Fl4SpecA64.State Unit} (h1 : IpcKeepA σ1 σ2) (h2 : IpcKeepA σ2 σ3) : + IpcKeepA σ1 σ3 := + ⟨fun p x h => by + obtain ⟨y, hy, a1, a2, a3, a4, a5⟩ := h1.tcb p x h + obtain ⟨z, hz, b1, b2, b3, b4, b5⟩ := h2.tcb p y hy + exact ⟨z, hz, b1.trans a1, b2.trans a2, b3.trans a3, b4.trans a4, ⟨b5.1.trans a5.1, b5.2.trans a5.2⟩⟩, + fun p y k h => by + obtain ⟨y', k', h', e⟩ := h1.scF p y k h + obtain ⟨y'', k'', h'', e'⟩ := h2.scF p y' k' h' + exact ⟨y'', k'', h'', e'.trans e⟩, + fun p y k h => by + obtain ⟨y', k', h', e⟩ := h2.scB p y k h + obtain ⟨y'', k'', h'', e'⟩ := h1.scB p y' k' h' + exact ⟨y'', k'', h'', e.trans e'⟩⟩ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {ar : machine.Arch A} {nA : U64} {s0 s : S} {a : A} {σ0 σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} + +set_option maxHeartbeats 2000000 in +/-- **The one-word reply after a body step** (module docstring). -/ +theorem JStage3A.replyOneMidA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s0 a σ0 m) + (wb : WritesIn st s0 s (SchedStepNWA (st := st) N E Rp Z m σ0 s0)) (Fb : SchedFrameA σ0 σ) + (Kb : IpcKeepFA σ0 σ) (Qb : QKeepA σ0 σ) + (twb : ∀ t ∈ Z.T, ∀ O : U64, O.val < 224 ∨ O.val = 3440 ∨ O.val = 3472 ∨ O.val = 3512 → O.val % 8 = 0 → + L.mem s (wa t O) = L.mem s0 (wa t O)) + {Q0 : U64 → U64 → List U64} {R0 : U64 → List U64} + (srM : SchedSRA st { Z with Q := Q0, R := R0 } m κ s σ) + (epM : EpStateRel Z.T E Rp Bf (L.mem s) σ) (ntM : NtfnStateRel N Z.T (L.mem s) σ) + (rpM : ReplyStackRelA (L.mem s) Rp Z.C σ (scNameA m) stack) + (mcpM : McpRelA Z.T m (L.mem s) σ) (yM : YieldRelA Z.T Z.C m (L.mem s) σ) + (sinvM : SInvXA σ m n) (sidleM : SIdleA σ m n) (rvM : RunVcpuInv st Z.T s) + (bqM : BlkQueuedA Z.T E N (L.mem s) σ) (svM : SchedValidA σ) + (blkM : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s) + (ct : σ.curThread = specPtr (L.curW s)) + {x : Fl4SpecA64.Tcb} (hx : σ.kheap σ.curThread = some (.tcb x)) (hr : x.tcbState = .restart) + (hsc : L.mem s (scA (L.curW s)) ≠ 0#u64) (hac : L.mem s (scA (L.curW s)) ∈ Ac) + (hiq : (L.mem s (inqA (L.curW s)) &&& sched.Q_RELEASE) = 0#u64) + (hrq : σ.curThread ∉ σ.releaseQueue) + (hwC0 : L.mem s (scA (L.curW s)) ∈ Z.C) + (hscx0 : x.tcbSchedContext = some (scRenA m (L.mem s (scA (L.curW s))))) + (hpos0 : ∃ y k, σ.kheap (scRenA m (L.mem s (scA (L.curW s)))) = some (.schedContext y k) ∧ 0 < y.scRefillMax) + (call : Bool) (v : U64) + (fin : ∀ s', vspace.reply1 st s (L.curW s) call v = ok s' → ∀ σX : Fl4SpecA64.State Unit, + SysL st H Z.Cs OS ia ie s' ∧ SymL st H Z.Cs s' ∧ Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s' ∧ + J7A st (J7R2A st ar nA) Z H OS s' σX m) : + vspace.reply1 st s (L.curW s) call v ⦃ s' => ∃ σ', + ((), σ') ∈ ((do + Fl4Spec.NonDet.when call (Fl4SpecA64.replyFromKernel (Z := Unit) σ.curThread (0, [v.bv])) + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .running) σ).1 ∧ ReplyShapeA σ σ' x ∧ + ∃ Q Rl, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s' a σ' m ⦄ := by + generalize ht : L.curW s = t at * + have htZ : t ∈ Z.T := ht ▸ srM.curT + have hta : TcbAddr t := srM.core.tcbs t htZ + have hk : σ.kheap (specPtr t) = some (.tcb x) := ct ▸ hx + have PE := J.iplaced + have hbx := BlkOK.bxi blkM + obtain ⟨-, -, hX, -, hS⟩ := hbx + obtain ⟨Q0', R0', q⟩ := blkM.1 + have hct : t ∈ H.tcbs := by rw [J.tcbs]; exact htZ + set w := L.mem s (scA t) with hw + have hwC : w ∈ Z.C := hwC0 + have hwH : w ∈ H.scs := by rw [J.scs]; exact hwC + have hrm : L.rmax s w ≠ 0#u64 := (hS.act w hwH).1 hac + have hmΩ : maxA w ∈ H.omega := hS.sub _ (hS.all _ hwH).2.1 + have hma : Aligned (maxA w) := (q.omega _ hmΩ).1 + -- the register writes + have V : TcbIpcViewsA st Z m κ N E Rp Bf stack s σ := + ⟨⟨Q0, R0, srM⟩, ntM, epM, rpM, mcpM, yM⟩ + obtain ⟨s2, e2, V2, w2⟩ := reply1_regs_viewsA J.iplaced J.ipcPlace V htZ hk call v + set uc := replyUcA call x v with huc + set x1 : Fl4SpecA64.Tcb := { x with tcbArch := { x.tcbArch with tcbContext := uc } } with hx1 + set σ1 := setObjA σ (specPtr t) (.tcb x1) with hσ1 + set x3 : Fl4SpecA64.Tcb := { x1 with tcbState := .running } with hx3 + set σ' := stUpdA σ1 (specPtr t) x1 .running with hσ' + have hk1 : σ1.kheap (specPtr t) = some (.tcb x1) := by simp [σ1, setObjA] + have e' : σ' = setObjA σ (specPtr t) (.tcb x3) := by + simp only [σ', σ1, stUpdA, setObjA, Function.update_idem, x3] + -- the thread's words after the register writes + have far : ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → K.val ≠ 256 → K.val ≠ 264 → K.val ≠ 272 → + L.mem s2 (wa t K) = L.mem s (wa t K) := by + intro K h1 h2 h3 h4 h5 + refine w2.mem_eq (field_aligned hta h1 h2) fun h => ?_ + rcases h with h | h | h + · exact field_ne hta hta h1 (by simp) (fun _ => by simpa using h3) h + · exact field_ne hta hta h1 (by simp) (fun _ => by simpa using h4) h + · exact field_ne hta hta h1 (by simp) (fun _ => by simpa using h5) h + have hsc2 : L.mem s2 (scA t) = w := far 3464#u64 (by simp) (by simp) (by simp) (by simp) (by simp) + have hiq2 : (L.mem s2 (inqA t) &&& sched.Q_RELEASE) = 0#u64 := by + show (L.mem s2 (wa t 3592#u64) &&& sched.Q_RELEASE) = 0#u64 + rw [far 3592#u64 (by simp) (by simp) (by simp) (by simp) (by simp)]; exact hiq + have hrm2 : L.rmax s2 w ≠ 0#u64 := by + show L.mem s2 (maxA w) ≠ 0#u64 + rw [w2.mem_eq hma fun h => ?_] + · exact hrm + rcases h with h | h | h <;> + exact out_ne_field (q.omega _ hmΩ) hct hta (by simp) (congrArg UScalar.val h.symm) + have hmx : stA t ≠ maxA w := fun h => + out_ne_field (q.omega _ hmΩ) hct hta (K := 3328#u64) (by simp) (by rw [← stA_eq]; exact congrArg UScalar.val h) + -- the state write + have eRun : sched.set_thread_state st s2 t sched.TS_RUNNING = ok (L.wr s2 (stA t) sched.TS_RUNNING) := + set_thread_state_run_wrA hta (by rw [hsc2]; exact hsc) (by rw [hsc2]; exact hrm2) hiq2 + (by rw [hsc2]; exact hma) (by rw [hsc2]; exact hmx) + set s' := L.wr s2 (stA t) sched.TS_RUNNING with hs' + have eProg : vspace.reply1 st s t call v = ok s' := by + unfold vspace.reply1 + rw [e2, bind_ok, eRun] + rw [eProg, WP.spec_ok] + -- the spec run + have hscx : x.tcbSchedContext = some (scRenA m w) := hscx0 + obtain ⟨ysc, nsc, hysc, hpos⟩ := hpos0 + have hne : scRenA m w ≠ specPtr t := fun e => by rw [e, hk] at hysc; cases hysc + have sched1 : schedulable (specPtr t) (setObjA σ1 (specPtr t) (.tcb x3)) = true := by + refine schedulable_ofA (x := x3) (n := nsc) (by simp [setObjA]) rfl hscx (y := ysc) ?_ hpos (ct ▸ hrq) + simpa [σ1, setObjA, Function.update_of_ne hne] using hysc + have hsch : ∀ uc', specPtr t = σ.curThread → σ.schedulerAction = .resumeCurThread → + schedulable (specPtr t) (setObjA σ (specPtr t) + (.tcb { x with tcbArch := { x.tcbArch with tcbContext := uc' }, tcbState := .running })) = true := fun uc' _ _ => by + refine schedulable_ofA (x := { x with tcbArch := { x.tcbArch with tcbContext := uc' }, tcbState := .running }) + (n := nsc) (by simp [setObjA]) rfl hscx (y := ysc) ?_ hpos (ct ▸ hrq) + simpa [setObjA, Function.update_of_ne hne] using hysc + have hrun := replyRun_oneA (σ := σ) hk call v.bv hsch + have hset : setThreadState (Z := Unit) (specPtr t) .running σ1 = ({((), σ')}, False) := + setThreadState_eqA σ1 (specPtr t) x1 hk1 .running (fun _ _ => sched1) + have hmem : ((), σ') ∈ ((do + Fl4Spec.NonDet.when call (Fl4SpecA64.replyFromKernel (Z := Unit) σ.curThread (0, [v.bv])) + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .running) σ).1 := by + rw [ct, hrun] + refine Set.mem_singleton_iff.2 (Prod.ext rfl ?_) + rw [e'] + cases call <;> + simp only [stUpdA, setObjA, ctxUpdA, Function.update_idem, x3, x1, uc, replyUcA, Bool.false_eq_true, + ↓reduceIte] + -- the record + have sA : Aligned (stA t) := by rw [stA_eq]; exact field_aligned hta (by simp) (by simp) + have wAll : WritesIn st s s' (fun b => ScConsumedRegsA t b ∨ b = stA t) := + (w2.trans (writesIn_wr sA (v := sched.TS_RUNNING))).mono fun _ h => h + have wN : WritesIn st s s' (SchedStepNWA (st := st) N E Rp Z m σ s) := by + refine wAll.mono fun b h => ?_ + rcases h with h | h + · exact consumedRegs_stepNA srM.core.tcbs htZ h + · rw [h]; exact stA_stepNA srM.core.tcbs htZ + have tcbW : ∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → K.val ≠ 256 → K.val ≠ 264 → K.val ≠ 272 → + K.val ≠ 3328 → L.mem s' (wa u K) = L.mem s (wa u K) := by + intro u hu K h1 h2 h3 h4 h5 h6 + have hua := srM.core.tcbs u hu + refine wAll.mem_eq (field_aligned hua h1 h2) fun h => ?_ + rcases h with (h | h | h) | h + · exact field_ne hua hta h1 (by simp) (fun _ => by simpa using h3) h + · exact field_ne hua hta h1 (by simp) (fun _ => by simpa using h4) h + · exact field_ne hua hta h1 (by simp) (fun _ => by simpa using h5) h + · rw [stA_eq] at h + exact field_ne hua hta h1 (by simp) (fun _ => by simpa using h6) h + have same : ∀ p, p ≠ specPtr t → σ'.kheap p = σ.kheap p := fun p e => by + rw [e']; simp [setObjA, Function.update_of_ne e] + have atT : σ'.kheap (specPtr t) = some (.tcb x3) := by rw [e']; simp [setObjA] + have keepK : ∀ p ob, σ.kheap p = some ob → (∀ y, ob ≠ .tcb y) → σ'.kheap p = some ob := fun p ob h ho => by + have e : p ≠ specPtr t := fun e => by rw [e, hk] at h; exact ho x (Option.some.inj h).symm + rw [same p e]; exact h + have backK : ∀ p ob, σ'.kheap p = some ob → (∀ y, ob ≠ .tcb y) → σ.kheap p = some ob := fun p ob h ho => by + by_cases e : p = specPtr t + · rw [e, atT] at h; exact absurd (Option.some.inj h).symm (ho x3) + · rw [← same p e]; exact h + have tcbK : ∀ p y, σ.kheap p = some (.tcb y) → ∃ y', σ'.kheap p = some (.tcb y') ∧ + y'.tcbSchedContext = y.tcbSchedContext ∧ y'.tcbFault = y.tcbFault ∧ y'.tcbIpcframe = y.tcbIpcframe ∧ + y'.tcbIpcBuffer = y.tcbIpcBuffer ∧ y'.tcbMcpriority = y.tcbMcpriority ∧ y'.tcbYieldTo = y.tcbYieldTo ∧ + y'.tcbFlags = y.tcbFlags ∧ (p ≠ specPtr t → y' = y) ∧ (p = specPtr t → y'.tcbState = .running) := by + intro p y hy + by_cases e : p = specPtr t + · rw [e, hk] at hy; cases hy + exact ⟨x3, e ▸ atT, rfl, rfl, rfl, rfl, rfl, rfl, rfl, fun h => absurd e h, fun _ => rfl⟩ + · exact ⟨y, by rw [same p e]; exact hy, rfl, rfl, rfl, rfl, rfl, rfl, rfl, fun _ => rfl, fun h => absurd h e⟩ + have HS : SpecSchedA σ σ' := + { kh := fun p => by + by_cases e : p = specPtr t + · refine Or.inr (Or.inl ⟨x, x3, e ▸ hk, e ▸ atT, ⟨rfl, rfl, rfl⟩⟩) + · exact Or.inl (same p e) + irqS := by rw [e']; rfl + irqN := by rw [e']; rfl + asid := by rw [e']; rfl + vcur := by rw [e']; rfl + cdt := by rw [e']; rfl + orig := by rw [e']; rfl } + have SSz : SSzA σ σ' := fun p y k h => ⟨y, backK p _ h (fun _ e => by cases e)⟩ + have K : IpcKeepA σ σ' := + ⟨fun p y h => by + obtain ⟨y', h', -, f, g1, g2, g3, g4, gf, -⟩ := tcbK p y h + exact ⟨y', h', f, g1, g2, g3, g4, gf⟩, + fun p y k h => ⟨y, k, keepK p _ h (fun _ e => by cases e), rfl⟩, + fun p y k h => ⟨y, k, backK p _ h (fun _ e => by cases e), rfl⟩⟩ + have hbx2 : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s' := by + have hnb : ¬ BlockedR (L.mem s (stA t)) := by + have code := (epM.at_tcb htZ hk).st + rw [hr] at code + have ev : L.mem s (stA t) = sched.TS_RESTART := UScalar.eq_of_val_eq (by rw [code, ts_vals.2.1]; rfl) + rw [ev]; exact nbr_restart + have h0 := Fl4ProofsA64.P1.W7.R.ST.vcpu.reply1.dfKn (Ko := ∅) blkM hct hnb call v + rw [← vspace.reply1_eq, eProg, WP.spec_ok] at h0 + exact h0.2 + obtain ⟨sysl, symL, userOK, j7⟩ := fin s' eProg σ' + have T2 : StEqA Z.T (L.mem s2) σ1 := fun u hu y hy => (V2.ep.at_tcb hu hy).st + obtain ⟨Q2, R2, H2⟩ := id V2.rel + have sr : SchedSRA st { Z with Q := Q2, R := R2 } m κ s' σ' := by + have hvn : sched.TS_RUNNING.val = tsCodeA .running := by rw [ts_vals.1]; rfl + have h := sc_set_runnable_stateA (Z := { Z with Q := Q2, R := R2 }) H2 T2 htZ hk1 + (show runnable x1.tcbState = true by simp [x1, hr, runnable]) (ts := .running) rfl hvn + rw [eRun, WP.spec_ok] at h + obtain ⟨τ, hτ, Hs, -, -, -⟩ := h + have e : τ = σ' := by + rw [hset] at hτ + exact ((Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst hτ))).2).symm + rw [e] at Hs + exact Hs + have V2ep := V2.ep + have views : EpStateRel Z.T E Rp Bf (L.mem s') σ' ∧ NtfnStateRel N Z.T (L.mem s') σ' := by + have notQ : ∀ e ∈ E, ∀ z, σ1.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue := by + intro e he z hz hq + obtain ⟨z', hz', rel⟩ := V2.ep.ep e he + rw [hz] at hz' + obtain rfl := KernelObject.endpoint.inj (Option.some.inj hz') + obtain ⟨qs, _, hqs, members⟩ := rel.queue + rw [hqs, List.mem_map] at hq + obtain ⟨u, hu, hEq⟩ := hq + have eu : u = t := specPtr_inj hEq + subst u + have state := (members t hu).2 + rw [(V2.ep.at_tcb htZ hk1).st] at state + simp [x1, hr] at state + exact absurd state (by decide) + have code : sched.TS_RUNNING.val = Notif.tsCode .running := by rw [ts_vals.1]; rfl + have eσ : Notif.updK σ1 (specPtr t) (.tcb { x1 with tcbState := .running }) = σ' := rfl + refine ⟨?_, ?_⟩ + · have h := (Endp.write_state_rel J.iplaced.e s2 σ1 V2.ep htZ hk1 notQ .running (by decide) _ code).1 + rw [eσ] at h; exact h + · obtain ⟨writes, kept⟩ := Notif.twr_wr (st := st) J.iplaced.n htZ (K := 3328#u64) (Or.inl rfl) s2 + sched.TS_RUNNING + rw [← stA_eq] at writes kept + have nt := V2.ntfn.at_tcb htZ hk1 + have h : NtfnStateRel N Z.T (L.mem s') (Notif.updK σ1 (specPtr t) (.tcb { x1 with tcbState := .running })) := by + refine V2.ntfn.upd_tcb htZ _ writes.ntfn writes.tcb writes.links (Or.inl ?_) ?_ + · intro q hq z hz hm + obtain ⟨z', hz', rel⟩ := V2.ntfn.ntfn q hq + rw [hz] at hz' + obtain rfl := KernelObject.notification.inj (Option.some.inj hz') + have blocked := (rel.obj.blocked hm).2.1 + rw [nt.st] at blocked + simp [x1, hr] at blocked + exact absurd blocked (by decide) + · refine ⟨?_, (fun _ h => by cases h), ?_, ?_⟩ + · rw [L.mem_wr_same (Notif.foot_st J.iplaced.n htZ).1]; exact code + · show (L.mem s' (Notif.badgeRegA t)).bv = Notif.badgeOf x1 + rw [Notif.badgeRegA_eq, kept _ (by simp) (by simp) (by simp), ← Notif.badgeRegA_eq] + exact nt.badge + · show (L.mem s' (prioA t)).val = x1.tcbPriority.toNat + rw [Notif.prioA_eq', kept _ (by simp) (by simp) (by simp), ← Notif.prioA_eq'] + exact nt.prio + exact h + have reply : ReplyStackRelA (L.mem s') Rp Z.C σ' (scNameA m) stack := by + refine V2.stk.frame (fun r hr' => ?_) (fun c _ y k h => ⟨y, k, ?_, rfl⟩) (fun r _ x' h => ⟨x', ?_, rfl⟩) + · have hr3 := J.iplaced.e.rep r hr' + have hdis := J.iplaced.e.rt r hr' t htZ + have key : ∀ o : U64, o.val = 8 ∨ o.val = 16 → L.mem s' (wa r o) = L.mem s2 (wa r o) := by + intro o ho + have hv : (wa r o).val = r.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + refine mem_wr_ne' sA (by unfold Aligned; rw [hv]; omega) ?_ + intro e + have ev := congrArg UScalar.val e + rw [stA_eq, Notif.tfield_val hta (by decide : (3328#u64 : U64).val < 4096), hv] at ev + simp at ev + omega + exact ⟨key _ (Or.inr (by simp [layout.reply.NEXT])), key _ (Or.inl (by simp [layout.reply.PREV]))⟩ + · have e : scNameA m c ≠ specPtr t := fun e => by rw [e, hk1] at h; cases h + rw [same _ e] + simpa [σ1, setObjA, Function.update_of_ne e] using h + · have e : specPtr r ≠ specPtr t := fun e => by + rw [show σ'.kheap (specPtr r) = some (.tcb x3) by rw [e]; exact atT] at h; cases h + rw [same _ e] at h + simpa [σ1, setObjA, Function.update_of_ne e] using h + have hni : specPtr t ≠ σ.idleThread := fun e => by + obtain ⟨y, hy, hs⟩ := sinvM.idleTcb + rw [← e, hk] at hy; cases hy + rw [hr] at hs; cases hs + have sinv : SInvXA σ' m n := by + have h1 : SInvXA σ1 m n := by + have same1 : ∀ p, p ≠ specPtr t → σ1.kheap p = σ.kheap p := fun p e => by + simp [σ1, setObjA, Function.update_of_ne e] + refine sinvM.of_fr + { tcb := fun p y h => ?_, tcb' := fun p y h => ?_, sc := fun p y k => ?_, cur := rfl, idle := rfl, csc := rfl, + rq := rfl, rls := rfl, act := rfl, time := rfl, cons := rfl } (sinvM.irqN.updTcbA hk rfl) + · by_cases e : p = specPtr t + · rw [e, hk] at h; cases h + exact ⟨x1, e ▸ hk1, rfl, rfl, rfl⟩ + · exact ⟨y, by rw [same1 p e]; exact h, rfl, rfl, rfl⟩ + · by_cases e : p = specPtr t + · rw [e, hk1] at h; cases h + exact ⟨x, e ▸ hk, rfl, rfl, rfl⟩ + · exact ⟨y, by rw [← same1 p e]; exact h, rfl, rfl, rfl⟩ + · by_cases e : p = specPtr t + · rw [e, hk1, hk] + constructor <;> intro h <;> cases h + · rw [same1 p e] + exact h1.restartA hk1 hni (ts := .running) rfl (fun h => by cases h) (by by_contra hf; have := sinvM.faultSt.faultNotRun _ x hk hf; rw [hr] at this; exact absurd this (by decide)) + have sidle : SIdleA σ' m n := by + have hidle : ∀ c < n, m.idleThreadOf c ≠ specPtr t := fun c hc e => by + obtain ⟨y, hy, hs⟩ := sidleM.f3 c hc + rw [e, hk] at hy; cases hy + rw [hr] at hs; cases hs + refine + { f3 := fun c hc => by + obtain ⟨y, hy, hs⟩ := sidleM.f3 c hc + exact ⟨y, by rw [same _ (hidle c hc)]; exact hy, hs⟩ + f4 := fun y k h => sidleM.f4 y k (backK _ _ h (fun _ e => by cases e)) + f5 := fun p y k h he => sidleM.f5 p y k (backK _ _ h (fun _ e => by cases e)) (by rw [e'] at he; exact he) + f6 := sidleM.f6.of_eq fun sl => by + unfold specCapAtA + by_cases e : sl.1 = specPtr t + · rw [e, atT, hk] + try rfl + · rw [same _ e] + f7 := fun y k h => sidleM.f7 y k (backK _ _ h (fun _ e => by cases e)) + idleSlot := fun c hc => by + have h0 := sidleM.idleSlot c hc + unfold vtCapA at h0 ⊢ + rw [same _ (hidle c hc)]; exact h0 + idleBound := fun c hc => by + have h0 := sidleM.idleBound c hc + unfold vcpuOfA at h0 ⊢ + rw [same _ (hidle c hc)]; exact h0 } + have runVcpu : RunVcpuInv st Z.T s' := fun u hu h0 => by + have rvw : L.mem s' (runVcpuA u) = L.mem s (runVcpuA u) := + tcbW u hu 3672#u64 (by simp) (by simp) (by simp) (by simp) (by simp) (by simp) + rw [rvw] at h0 + have ne : u ≠ t := fun e => by + have h1 := rvM u hu h0 + have code := (epM.at_tcb htZ hk).st + rw [hr] at code + rw [e] at h1 + rw [h1] at code + simp [sched.TS_RUNNING_VM] at code + exact absurd code (by decide) + have hs : L.mem s' (stA u) = L.mem s (stA u) := by + have hua := srM.core.tcbs u hu + rw [stA_eq] + refine wAll.mem_eq (field_aligned hua (by simp) (by simp)) fun h => ?_ + rcases h with (h | h | h) | h + · exact field_ne hua hta (by simp) (by simp) (fun e => absurd e ne) h + · exact field_ne hua hta (by simp) (by simp) (fun e => absurd e ne) h + · exact field_ne hua hta (by simp) (by simp) (fun e => absurd e ne) h + · rw [stA_eq] at h + exact field_ne hua hta (by simp) (by simp) (fun e => absurd e ne) h + rw [hs] + exact rvM u hu h0 + have blkQ : BlkQueuedA Z.T E N (L.mem s') σ' := by + have tb : ∀ u ∈ Z.T, L.mem s' (tBlockA u) = L.mem s (tBlockA u) := fun u hu => + tcbW u hu 3336#u64 (by simp) (by simp) (by simp) (by simp) (by simp) (by simp) + have old : ∀ u, ∀ y, σ'.kheap (specPtr u) = some (.tcb y) → y.tcbState ≠ .running → + σ.kheap (specPtr u) = some (.tcb y) := fun u y h hn => by + by_cases e : specPtr u = specPtr t + · rw [e, atT] at h; cases h; exact absurd rfl hn + · rw [← same _ e]; exact h + refine ⟨fun u hu y h e hb => ?_, fun u hu y h p hb => ?_⟩ + · have hn : y.tcbState ≠ .running := fun h' => by rw [h'] at hb; cases hb + obtain ⟨c, z, hz, hm⟩ := bqM.ep u hu y (old u y h hn) e hb + exact ⟨by rw [tb u hu]; exact c, z, keepK _ _ hz (fun _ e => by cases e), hm⟩ + · have hn : y.tcbState ≠ .running := fun h' => by rw [h'] at hb; cases hb + obtain ⟨c, z, l, hz, hl, hm⟩ := bqM.ntfn u hu y (old u y h hn) p hb + exact ⟨by rw [tb u hu]; exact c, z, l, keepK _ _ hz (fun _ e => by cases e), hl, hm⟩ + have sv : SchedValidA σ' := by + have sc : ∀ p, ScActiveAtA σ p → ScActiveAtA σ' p := fun p ⟨y, k, e, act⟩ => + ⟨y, k, keepK p _ e (fun _ h => by cases h), act⟩ + have tcbAct : ∀ u, (∃ y c, σ.kheap u = some (.tcb y) ∧ y.tcbSchedContext = some c ∧ ScActiveAtA σ c) → + ∃ y c, σ'.kheap u = some (.tcb y) ∧ y.tcbSchedContext = some c ∧ ScActiveAtA σ' c := + fun u ⟨y, c, h, es, act⟩ => by + obtain ⟨y', h', es', -⟩ := tcbK u y h + exact ⟨y', c, h', es'.trans es, sc _ act⟩ + have hq : σ'.readyQueues = σ.readyQueues := by rw [e']; rfl + have hrl : σ'.releaseQueue = σ.releaseQueue := by rw [e']; rfl + have hact : σ'.schedulerAction = σ.schedulerAction := by rw [e']; rfl + have hc : σ'.curThread = σ.curThread := by rw [e']; rfl + have hi : σ'.idleThread = σ.idleThread := by rw [e']; rfl + refine ⟨fun d p u hm => ?_, fun e1 e2 => ?_, fun u y c e1 e2 e3 => ?_, fun u hu => ?_⟩ + · rw [hq] at hm + obtain ⟨act, nr⟩ := svM.rq d p u hm + exact ⟨tcbAct u act, by rw [hrl]; exact nr⟩ + · rw [hact] at e1 + rw [hc, hi] at e2 + obtain ⟨act, nr⟩ := svM.act e1 e2 + rw [hc, hrl] + exact ⟨tcbAct _ act, nr⟩ + · by_cases h : u = specPtr t + · rw [h, atT] at e1; cases e1; simp [x3, BlockedTsA] at e2 + · rw [same u h] at e1 + exact sc _ (svM.blk u y c e1 e2 e3) + · rw [hrl] at hu + exact tcbAct u (svM.rls u hu) + have wN0 : WritesIn st s0 s' (SchedStepNWA (st := st) N E Rp Z m σ0 s0) := + (wb.trans wN).mono fun b h => h.elim id (schedStepN_backA Fb.2.1 wb.1) + have Qk : QKeepA σ σ' := QKeepA.ofBwd fun p o h => by + by_cases e : p = specPtr t + · rw [e, atT] at h + cases h + exact ⟨.tcb x, by rw [e]; exact hk, QObjA.ofTcb rfl⟩ + · rw [same p e] at h + exact ⟨o, h, QObjA.refl o⟩ + refine ⟨σ', hmem, ⟨uc, by rw [ct]; exact e'⟩, Q2, R2, ?_⟩ + exact J.of_ipcStepFlA (Q := Q2) (Rl := R2) (curOf := Z.curOf) (curScOf := Z.curScOf) wN0 (Fb.1.trans HS) + (SSzA.trans Fb.2.1 SSz) (Qb.trans Qk) ((show σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory by + rw [e']; rfl).trans Fb.2.2) (fun _ _ => rfl) (BlkOK.bxi hbx2) sysl symL userOK + j7 sr views.1 views.2 reply (Kb.trans K.toF) + (mcpM.congrA (fun u hu => ⟨tcbW u hu _ (by simp) (by simp) (by simp) (by simp) (by simp) (by simp), + tcbW u hu _ (by simp) (by simp) (by simp) (by simp) (by simp) (by simp)⟩) fun u hu x hx => by + obtain ⟨y, hy, -, -, -, hm, -, hfl⟩ := K.tcb _ _ hx + exact ⟨y, hy, hm, hfl⟩) + (fun u hu O h h8 => (tcbW u hu O (by omega) h8 (by omega) (by omega) (by omega) (by omega)).trans + (twb u hu O h h8)) + sinv sidle runVcpu blkQ sv + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3ReplyOneA.lean b/proofs/Fl4ProofsA64/P3/Stage3ReplyOneA.lean new file mode 100644 index 000000000..b53cb4491 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ReplyOneA.lean @@ -0,0 +1,541 @@ +/- +P3 (aarch64, A64-P3 Lift 1a): **the one-word reply at record level** (reusable: `ARMPageGetAddress`, +`ARMPageTestAndClearAccessed`, and every other one-result reply such as SC `Consumed` and the TCB reads). + +`vspace.reply1` on the current thread, runnable (restarted, or still running as in +`ARMPageTestAndClearAccessed`): for a call, the three register words (badge `x0 := 0`, +`MR0 := v`, the message info), then `set_thread_state Running`. **`JStage3A.replyOneA`**: the spec's +`when call (replyFromKernel ct (0, [v]))` then `set_thread_state ct Running` has that outcome and `JStage3A` holds at +it. The caller supplies the facts the heap-keeping decode arms get from `decodeFinA` at the final state (`SysL`, +`SymL`, `UserOK`, `J7A`); `BXI` comes from `vcpu.reply1.dfKn`. + +Named premises: none (those of `fin`'s producer). +-/ +import Fl4ProofsA64.P3.Stage3Lift3QA +import Fl4ProofsA64.P3.Stage3IpcGlueA +import Fl4ProofsA64.P3.FrReplyA +import Fl4ProofsA64.P3.TcbRegViewA +import Fl4ProofsA64.P3.RestartA +import Fl4ProofsA64.P3.EpXferA +import Fl4ProofsA64.P3.Stage3RestartA +import Fl4ProofsA64.P3.UtTcbCreateA +import Fl4ProofsA64.P1.MdbVsMapA +import Fl4ProofsA64.P1.MdbVsGaQ +import Fl4ProofsA64.P1.QueueDecDel +import Fl4ProofsA64.P3.Stage3McsFrA +import Fl4ProofsA64.P3.Stage3LiftBodyA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {ar : machine.Arch A} {nA : U64} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- The caller's user context after the (possible) one-word reply. -/ +def replyUcA (call : Bool) (x : Fl4SpecA64.Tcb) (v : U64) : Fl4SpecA64.UserContext := + if call then replyCtxA x v.bv else x.tcbArch.tcbContext + +/-- The register writes of `vspace.reply1`: a call writes `x0`, `x2` (MR0) and `x1` (message info) and keeps the +merged views, with the caller's context the spec reply's. -/ +theorem reply1_regs_viewsA {s : S} {σ : Fl4SpecA64.State Unit} + (hP : IPlacedA N Z.T E Rp Bf) (P : IpcSchedPlacementA Z.th N E Rp Bf) + (V : TcbIpcViewsA st Z m κ N E Rp Bf stack s σ) {t : U64} (ht : t ∈ Z.T) {x : Fl4SpecA64.Tcb} + (hk : σ.kheap (specPtr t) = some (.tcb x)) (call : Bool) (v : U64) : + ∃ s2, (if call then do + let buf ← msg.lookup_ipc_buffer st s true t + let s2 ← obj.Tcb.set_reg st t s layout.reg.BADGE 0#u64 + let (n, s3) ← msg.set_mr st s2 t buf 0#u64 v + let mi ← msginfo.MessageInfo.new 0#u64 0#u64 0#u64 n + msg.set_msg_info st s3 t mi + else ok s) = ok s2 ∧ + TcbIpcViewsA st Z m κ N E Rp Bf stack s2 + (setObjA σ (specPtr t) (.tcb { x with tcbArch := { x.tcbArch with tcbContext := replyUcA call x v } })) ∧ + WritesIn st s s2 (ScConsumedRegsA t) := by + have hta := hP.n.tcb t ht + cases call with + | false => + refine ⟨s, rfl, ?_, WritesIn.refl _ _⟩ + have e : setObjA σ (specPtr t) (.tcb { x with tcbArch := { x.tcbArch with tcbContext := replyUcA false x v } }) = + σ := by + show { σ with kheap := Function.update σ.kheap (specPtr t) (some (.tcb x)) } = σ + rw [← hk, Function.update_eq_self] + rw [e]; exact V + | true => + obtain ⟨buf, hbuf, -⟩ := spec_existsA (P0.msg.lookup_ipc_buffer.p0 st s true t L.total) + obtain ⟨mi, hmi, hmiv⟩ := spec_existsA (to_word_specA 0#u64 1#u64 (by simp)) + have eB : layout.reg.BADGE = regIdxA .x0 := by simp [layout.reg.BADGE, layout.reg.X0, regIdxA] + have eX2 : layout.reg.X2 = regIdxA .x2 := by simp [layout.reg.X2, regIdxA] + have V1 := tcbRegWrite_viewsA hP P V ht hk .x0 0#u64 + set x1 := Notif.setReg x .x0 (0#u64 : U64).bv with hx1 + have h1 : (Notif.updK σ (specPtr t) (.tcb x1)).kheap (specPtr t) = some (.tcb x1) := by + simp [Notif.updK] + have V2 := tcbRegWrite_viewsA hP P V1 ht h1 .x2 v + set x2 := Notif.setReg x1 .x2 v.bv with hx2 + have h2 : (Notif.updK (Notif.updK σ (specPtr t) (.tcb x1)) (specPtr t) (.tcb x2)).kheap (specPtr t) = + some (.tcb x2) := by simp [Notif.updK] + have V3 := tcbRegWrite_viewsA hP P V2 ht h2 Fl4SpecA64.msg_info_register mi + set x3 := Notif.setReg x2 Fl4SpecA64.msg_info_register mi.bv with hx3 + have ex : x3 = { x with tcbArch := { x.tcbArch with tcbContext := replyUcA true x v } } := by + have hmi' : mi.bv = messageInfoToData ⟨natToLen 1, 0, 0, 0⟩ := by + have z : (U64.bv 0#u64 &&& mask msgLabelBits) = 0 := by + have z0 : U64.bv 0#u64 = 0#64 := rfl + rw [z0]; simp + rw [hmiv, z]; rfl + have hl : natToLen (min msg_registers.length [v.bv].length) = natToLen 1 ∧ + natToLen (min [v.bv].length msgMaxLength) = natToLen 1 := by constructor <;> rfl + have htake : msg_registers.take [v.bv].length = [Register.x2] := by rfl + have regs : Function.update (Function.update (Function.update x.tcbArch.tcbContext.userRegs .x0 + (0#u64 : U64).bv) .x2 v.bv) Fl4SpecA64.msg_info_register mi.bv = + (replyCtxA x v.bv).userRegs := by + unfold replyCtxA + dsimp only + refine congrArg₂ (fun f b => Function.update f Fl4SpecA64.msg_info_register b) ?_ ?_ + · funext r + rw [htake] + by_cases e : r = .x2 + · subst e; simp [theIndex, msg_registers, msgRegisters, Register.x2ToX5]; rfl + · rw [Function.update_of_ne e, if_neg (by simpa using e)] + rfl + · rw [hmi'] + split <;> (try split) <;> first | rw [hl.1] | rw [hl.2] + have fpu : (replyCtxA x v.bv).userFpuState = x.tcbArch.tcbContext.userFpuState := rfl + have uc : replyCtxA x v.bv = ⟨x.tcbArch.tcbContext.userFpuState, (replyCtxA x v.bv).userRegs⟩ := by + rw [← fpu] + unfold replyUcA + rw [if_pos rfl, uc, ← regs] + rfl + have eσ : Notif.updK (Notif.updK (Notif.updK σ (specPtr t) (.tcb x1)) (specPtr t) (.tcb x2)) (specPtr t) + (.tcb x3) = setObjA σ (specPtr t) + (.tcb { x with tcbArch := { x.tcbArch with tcbContext := replyUcA true x v } }) := by + rw [← ex] + unfold Notif.updK setObjA + simp only [Function.update_idem] + rw [eσ] at V3 + refine ⟨_, ?_, V3, ?_⟩ + · simp only [↓reduceIte, hbuf, bind_ok, eB, ut_set_regA] + unfold msg.set_mr + rw [if_pos (show (0#u64 : U64) < msg.N_MSG_REGISTERS by simp [msg.N_MSG_REGISTERS])] + simp only [msg.msg_register, ↓reduceIte, bind_ok, eX2, ut_set_regA, lift] + unfold msginfo.MessageInfo.new msg.set_msg_info + have e1 : core.num.U64.wrapping_add 0#u64 1#u64 = 1#u64 := by + apply UScalar.eq_of_val_eq + simp [core.num.U64.wrapping_add_val_eq, U64.size_def, U64.numBits] + simp only [bind_ok, e1] + rw [← regIdxA_msgInfo] + change (msginfo.MessageInfo.to_word + { label := 0#u64, caps_unwrapped := 0#u64, extra_caps := 0#u64, length := 1#u64 } >>= fun i => + obj.Tcb.set_reg st t (L.wr (L.wr s (Endp.regA t (regIdxA .x0)) 0#u64) (Endp.regA t (regIdxA .x2)) v) + (regIdxA Fl4SpecA64.msg_info_register) i) = _ + rw [hmi, bind_tc_ok] + exact ut_set_regA _ _ _ _ + · have w : ∀ (s0 : S) (r : Fl4SpecA64.Register) (w : U64) (K : U64), (regIdxA r).val < 37 → + K.val = 256 + 8 * (regIdxA r).val → WritesIn st s0 (L.wr s0 (Endp.regA t (regIdxA r)) w) (· = wa t K) := by + intro s0 r w K hr hK + have e : Endp.regA t (regIdxA r) = wa t K := by + apply UScalar.eq_of_val_eq + rw [regA_val hta hr, field_val hta (by omega)] + omega + rw [e] + exact writesIn_wr (field_aligned hta (by omega) (by omega)) + have w0 := w s .x0 0#u64 256#u64 (regIdxA_lt _) (by simp [regIdxA]) + have w2 := w (L.wr s (Endp.regA t (regIdxA .x0)) 0#u64) .x2 v 272#u64 (regIdxA_lt _) (by simp [regIdxA]) + have w1 := w (L.wr (L.wr s (Endp.regA t (regIdxA .x0)) 0#u64) (Endp.regA t (regIdxA .x2)) v) + Fl4SpecA64.msg_info_register mi 264#u64 (regIdxA_lt _) + (by rw [regIdxA_msgInfo]; simp [layout.reg.MSG_INFO, layout.reg.X1]) + refine (w0.trans (w2.trans w1)).mono fun b h => ?_ + rcases h with e | e | e + · exact Or.inl e + · exact Or.inr (Or.inr e) + · exact Or.inr (Or.inl e) + +/-- **The one-word reply at record level** (module docstring). -/ +theorem JStage3A.replyOneA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {x : Fl4SpecA64.Tcb} (hx : σ.kheap σ.curThread = some (.tcb x)) (hxs : x.tcbState = .restart ∨ x.tcbState = .running) + (hsc : L.mem s (scA (L.curW s)) ≠ 0#u64) (hac : L.mem s (scA (L.curW s)) ∈ Ac) + (hiq : (L.mem s (inqA (L.curW s)) &&& sched.Q_RELEASE) = 0#u64) + (hrq : σ.curThread ∉ σ.releaseQueue) + (call : Bool) (v : U64) + (fin : ∀ s', vspace.reply1 st s (L.curW s) call v = ok s' → ∀ σX : Fl4SpecA64.State Unit, + SysL st H Z.Cs OS ia ie s' ∧ SymL st H Z.Cs s' ∧ Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s' ∧ + J7A st (J7R2A st ar nA) Z H OS s' σX m) : + vspace.reply1 st s (L.curW s) call v ⦃ s' => ∃ σ', + ((), σ') ∈ ((do + Fl4Spec.NonDet.when call (Fl4SpecA64.replyFromKernel (Z := Unit) σ.curThread (0, [v.bv])) + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .running) σ).1 ∧ ReplyShapeA σ σ' x ∧ + ∃ Q Rl, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s' a σ' m ⦄ := by + have ct : σ.curThread = specPtr (L.curW s) := J.j.1.sch.curThread + generalize ht : L.curW s = t at * + have htZ : t ∈ Z.T := ht ▸ J.sr.curT + have hta : TcbAddr t := J.sr.core.tcbs t htZ + have hk : σ.kheap (specPtr t) = some (.tcb x) := ct ▸ hx + have PE := J.iplaced + have hr : runnable x.tcbState = true := by rcases hxs with e0 | e0 <;> rw [e0] <;> rfl + have hrun0 : Runnable (L.mem s (stA t)) := + (runnable_iffA (show (L.mem s (stA t)).val = tsCodeA x.tcbState from (J.ep.at_tcb htZ hk).st)).2 hr + have hbx := J.sys.ent.bxi + obtain ⟨-, -, hX, -, hS⟩ := hbx + have q := J.sq + have hct : t ∈ H.tcbs := by rw [J.tcbs]; exact htZ + set w := L.mem s (scA t) with hw + have hwC : w ∈ Z.C := J.j.1.rest.scMem t htZ hsc + have hwH : w ∈ H.scs := by rw [J.scs]; exact hwC + have hrm : L.rmax s w ≠ 0#u64 := (hS.act w hwH).1 hac + have hmΩ : maxA w ∈ H.omega := hS.sub _ (hS.all _ hwH).2.1 + have hma : Aligned (maxA w) := (q.omega _ hmΩ).1 + -- the register writes + have V : TcbIpcViewsA st Z m κ N E Rp Bf stack s σ := + ⟨⟨Z.Q, Z.R, J.sr⟩, J.ntfn, J.ep, J.reply, J.mcp, J.yieldRel⟩ + obtain ⟨s2, e2, V2, w2⟩ := reply1_regs_viewsA J.iplaced J.ipcPlace V htZ hk call v + set uc := replyUcA call x v with huc + set x1 : Fl4SpecA64.Tcb := { x with tcbArch := { x.tcbArch with tcbContext := uc } } with hx1 + set σ1 := setObjA σ (specPtr t) (.tcb x1) with hσ1 + set x3 : Fl4SpecA64.Tcb := { x1 with tcbState := .running } with hx3 + set σ' := stUpdA σ1 (specPtr t) x1 .running with hσ' + have hk1 : σ1.kheap (specPtr t) = some (.tcb x1) := by simp [σ1, setObjA] + have e' : σ' = setObjA σ (specPtr t) (.tcb x3) := by + simp only [σ', σ1, stUpdA, setObjA, Function.update_idem, x3] + -- the thread's words after the register writes + have far : ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → K.val ≠ 256 → K.val ≠ 264 → K.val ≠ 272 → + L.mem s2 (wa t K) = L.mem s (wa t K) := by + intro K h1 h2 h3 h4 h5 + refine w2.mem_eq (field_aligned hta h1 h2) fun h => ?_ + rcases h with h | h | h + · exact field_ne hta hta h1 (by simp) (fun _ => by simpa using h3) h + · exact field_ne hta hta h1 (by simp) (fun _ => by simpa using h4) h + · exact field_ne hta hta h1 (by simp) (fun _ => by simpa using h5) h + have hsc2 : L.mem s2 (scA t) = w := far 3464#u64 (by simp) (by simp) (by simp) (by simp) (by simp) + have hiq2 : (L.mem s2 (inqA t) &&& sched.Q_RELEASE) = 0#u64 := by + show (L.mem s2 (wa t 3592#u64) &&& sched.Q_RELEASE) = 0#u64 + rw [far 3592#u64 (by simp) (by simp) (by simp) (by simp) (by simp)]; exact hiq + have hrm2 : L.rmax s2 w ≠ 0#u64 := by + show L.mem s2 (maxA w) ≠ 0#u64 + rw [w2.mem_eq hma fun h => ?_] + · exact hrm + rcases h with h | h | h <;> + exact out_ne_field (q.omega _ hmΩ) hct hta (by simp) (congrArg UScalar.val h.symm) + have hmx : stA t ≠ maxA w := fun h => + out_ne_field (q.omega _ hmΩ) hct hta (K := 3328#u64) (by simp) (by rw [← stA_eq]; exact congrArg UScalar.val h) + -- the state write + have eRun : sched.set_thread_state st s2 t sched.TS_RUNNING = ok (L.wr s2 (stA t) sched.TS_RUNNING) := + set_thread_state_run_wrA hta (by rw [hsc2]; exact hsc) (by rw [hsc2]; exact hrm2) hiq2 + (by rw [hsc2]; exact hma) (by rw [hsc2]; exact hmx) + set s' := L.wr s2 (stA t) sched.TS_RUNNING with hs' + have eProg : vspace.reply1 st s t call v = ok s' := by + unfold vspace.reply1 + rw [e2, bind_ok, eRun] + rw [eProg, WP.spec_ok] + -- the spec run + have hscx : x.tcbSchedContext = some (scRenA m w) := by + obtain ⟨y, hy, -, hsx⟩ := J.j.1.sch.tcb t htZ + rw [hk] at hy; cases hy + rw [hsx]; unfold optScA; rw [← hw, if_neg hsc] + obtain ⟨ysc, nsc, hysc, hpos⟩ : ∃ y k, σ.kheap (scRenA m w) = some (.schedContext y k) ∧ 0 < y.scRefillMax := by + obtain ⟨y, k, ey, eyr⟩ := J.j.1.rest.refillMax w hwC + refine ⟨y, k, ey, ?_⟩ + rw [eyr] + have e3 : L.rmax s w = L.mem s (wa w 72#u64) := by unfold LawfulStore.rmax; rw [maxA_eqA] + rw [e3] at hrm + exact Nat.pos_of_ne_zero (fun e => hrm (UScalar.eq_of_val_eq (by simpa using e))) + have hne : scRenA m w ≠ specPtr t := fun e => by rw [e, hk] at hysc; cases hysc + have sched1 : schedulable (specPtr t) (setObjA σ1 (specPtr t) (.tcb x3)) = true := by + refine schedulable_ofA (x := x3) (n := nsc) (by simp [setObjA]) rfl hscx (y := ysc) ?_ hpos (ct ▸ hrq) + simpa [σ1, setObjA, Function.update_of_ne hne] using hysc + have hsch : ∀ uc', specPtr t = σ.curThread → σ.schedulerAction = .resumeCurThread → + schedulable (specPtr t) (setObjA σ (specPtr t) + (.tcb { x with tcbArch := { x.tcbArch with tcbContext := uc' }, tcbState := .running })) = true := fun uc' _ _ => by + refine schedulable_ofA (x := { x with tcbArch := { x.tcbArch with tcbContext := uc' }, tcbState := .running }) + (n := nsc) (by simp [setObjA]) rfl hscx (y := ysc) ?_ hpos (ct ▸ hrq) + simpa [setObjA, Function.update_of_ne hne] using hysc + have hrun := replyRun_oneA (σ := σ) hk call v.bv hsch + have hset : setThreadState (Z := Unit) (specPtr t) .running σ1 = ({((), σ')}, False) := + setThreadState_eqA σ1 (specPtr t) x1 hk1 .running (fun _ _ => sched1) + have hmem : ((), σ') ∈ ((do + Fl4Spec.NonDet.when call (Fl4SpecA64.replyFromKernel (Z := Unit) σ.curThread (0, [v.bv])) + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .running) σ).1 := by + rw [ct, hrun] + refine Set.mem_singleton_iff.2 (Prod.ext rfl ?_) + rw [e'] + cases call <;> + simp only [stUpdA, setObjA, ctxUpdA, Function.update_idem, x3, x1, uc, replyUcA, Bool.false_eq_true, + ↓reduceIte] + -- the record + have sA : Aligned (stA t) := by rw [stA_eq]; exact field_aligned hta (by simp) (by simp) + have wAll : WritesIn st s s' (fun b => ScConsumedRegsA t b ∨ b = stA t) := + (w2.trans (writesIn_wr sA (v := sched.TS_RUNNING))).mono fun _ h => h + have wN : WritesIn st s s' (SchedStepNWA (st := st) N E Rp Z m σ s) := by + refine wAll.mono fun b h => ?_ + rcases h with h | h + · exact consumedRegs_stepNA J.sr.core.tcbs htZ h + · rw [h]; exact stA_stepNA J.sr.core.tcbs htZ + have tcbW : ∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → K.val ≠ 256 → K.val ≠ 264 → K.val ≠ 272 → + K.val ≠ 3328 → L.mem s' (wa u K) = L.mem s (wa u K) := by + intro u hu K h1 h2 h3 h4 h5 h6 + have hua := J.sr.core.tcbs u hu + refine wAll.mem_eq (field_aligned hua h1 h2) fun h => ?_ + rcases h with (h | h | h) | h + · exact field_ne hua hta h1 (by simp) (fun _ => by simpa using h3) h + · exact field_ne hua hta h1 (by simp) (fun _ => by simpa using h4) h + · exact field_ne hua hta h1 (by simp) (fun _ => by simpa using h5) h + · rw [stA_eq] at h + exact field_ne hua hta h1 (by simp) (fun _ => by simpa using h6) h + have same : ∀ p, p ≠ specPtr t → σ'.kheap p = σ.kheap p := fun p e => by + rw [e']; simp [setObjA, Function.update_of_ne e] + have atT : σ'.kheap (specPtr t) = some (.tcb x3) := by rw [e']; simp [setObjA] + have keepK : ∀ p ob, σ.kheap p = some ob → (∀ y, ob ≠ .tcb y) → σ'.kheap p = some ob := fun p ob h ho => by + have e : p ≠ specPtr t := fun e => by rw [e, hk] at h; exact ho x (Option.some.inj h).symm + rw [same p e]; exact h + have backK : ∀ p ob, σ'.kheap p = some ob → (∀ y, ob ≠ .tcb y) → σ.kheap p = some ob := fun p ob h ho => by + by_cases e : p = specPtr t + · rw [e, atT] at h; exact absurd (Option.some.inj h).symm (ho x3) + · rw [← same p e]; exact h + have tcbK : ∀ p y, σ.kheap p = some (.tcb y) → ∃ y', σ'.kheap p = some (.tcb y') ∧ + y'.tcbSchedContext = y.tcbSchedContext ∧ y'.tcbFault = y.tcbFault ∧ y'.tcbIpcframe = y.tcbIpcframe ∧ + y'.tcbIpcBuffer = y.tcbIpcBuffer ∧ y'.tcbMcpriority = y.tcbMcpriority ∧ y'.tcbYieldTo = y.tcbYieldTo ∧ y'.tcbFlags = y.tcbFlags ∧ + (p ≠ specPtr t → y' = y) ∧ (p = specPtr t → y'.tcbState = .running) := by + intro p y hy + by_cases e : p = specPtr t + · rw [e, hk] at hy; cases hy + exact ⟨x3, e ▸ atT, rfl, rfl, rfl, rfl, rfl, rfl, rfl, fun h => absurd e h, fun _ => rfl⟩ + · exact ⟨y, by rw [same p e]; exact hy, rfl, rfl, rfl, rfl, rfl, rfl, rfl, fun _ => rfl, fun h => absurd h e⟩ + have HS : SpecSchedA σ σ' := + { kh := fun p => by + by_cases e : p = specPtr t + · refine Or.inr (Or.inl ⟨x, x3, e ▸ hk, e ▸ atT, ⟨rfl, rfl, rfl⟩⟩) + · exact Or.inl (same p e) + irqS := by rw [e']; rfl + irqN := by rw [e']; rfl + asid := by rw [e']; rfl + vcur := by rw [e']; rfl + cdt := by rw [e']; rfl + orig := by rw [e']; rfl } + have SSz : SSzA σ σ' := fun p y k h => ⟨y, backK p _ h (fun _ e => by cases e)⟩ + have K : IpcKeepA σ σ' := + ⟨fun p y h => by + obtain ⟨y', h', -, f, g1, g2, g3, g4, g5, -⟩ := tcbK p y h + exact ⟨y', h', f, g1, g2, g3, ⟨g4, g5⟩⟩, + fun p y k h => ⟨y, k, keepK p _ h (fun _ e => by cases e), rfl⟩, + fun p y k h => ⟨y, k, backK p _ h (fun _ e => by cases e), rfl⟩⟩ + have hbx2 : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s' := by + have hnb : ¬ BlockedR (L.mem s (stA t)) := nbr_runnable hrun0 + have h0 := Fl4ProofsA64.P1.W7.R.ST.vcpu.reply1.dfKn (Ko := ∅) (BXI.blk J.sys.ent.bxi) hct hnb call v + rw [← vspace.reply1_eq, eProg, WP.spec_ok] at h0 + exact h0.2 + obtain ⟨sysl, symL, userOK, j7⟩ := fin s' eProg σ' + have T2 : StEqA Z.T (L.mem s2) σ1 := fun u hu y hy => (V2.ep.at_tcb hu hy).st + obtain ⟨Q2, R2, H2⟩ := id V2.rel + have sr : SchedSRA st { Z with Q := Q2, R := R2 } m κ s' σ' := by + have hvn : sched.TS_RUNNING.val = tsCodeA .running := by rw [ts_vals.1]; rfl + have h := sc_set_runnable_stateA (Z := { Z with Q := Q2, R := R2 }) H2 T2 htZ hk1 + (show runnable x1.tcbState = true from hr) (ts := .running) rfl hvn + rw [eRun, WP.spec_ok] at h + obtain ⟨τ, hτ, Hs, -, -, -⟩ := h + have e : τ = σ' := by + rw [hset] at hτ + exact ((Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst hτ))).2).symm + rw [e] at Hs + exact Hs + have V2ep := V2.ep + have views : EpStateRel Z.T E Rp Bf (L.mem s') σ' ∧ NtfnStateRel N Z.T (L.mem s') σ' := by + have notQ : ∀ e ∈ E, ∀ z, σ1.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue := by + intro e he z hz hq + obtain ⟨z', hz', rel⟩ := V2.ep.ep e he + rw [hz] at hz' + obtain rfl := KernelObject.endpoint.inj (Option.some.inj hz') + obtain ⟨qs, _, hqs, members⟩ := rel.queue + rw [hqs, List.mem_map] at hq + obtain ⟨u, hu, hEq⟩ := hq + have eu : u = t := specPtr_inj hEq + subst u + have state := (members t hu).2 + rw [(V2.ep.at_tcb htZ hk1).st] at state + rcases hxs with e0 | e0 <;> simp [x1, e0] at state <;> exact absurd state (by decide) + have code : sched.TS_RUNNING.val = Notif.tsCode .running := by rw [ts_vals.1]; rfl + have eσ : Notif.updK σ1 (specPtr t) (.tcb { x1 with tcbState := .running }) = σ' := rfl + refine ⟨?_, ?_⟩ + · have h := (Endp.write_state_rel J.iplaced.e s2 σ1 V2.ep htZ hk1 notQ .running (by decide) _ code).1 + rw [eσ] at h; exact h + · obtain ⟨writes, kept⟩ := Notif.twr_wr (st := st) J.iplaced.n htZ (K := 3328#u64) (Or.inl rfl) s2 + sched.TS_RUNNING + rw [← stA_eq] at writes kept + have nt := V2.ntfn.at_tcb htZ hk1 + have h : NtfnStateRel N Z.T (L.mem s') (Notif.updK σ1 (specPtr t) (.tcb { x1 with tcbState := .running })) := by + refine V2.ntfn.upd_tcb htZ _ writes.ntfn writes.tcb writes.links (Or.inl ?_) ?_ + · intro q hq z hz hm + obtain ⟨z', hz', rel⟩ := V2.ntfn.ntfn q hq + rw [hz] at hz' + obtain rfl := KernelObject.notification.inj (Option.some.inj hz') + have blocked := (rel.obj.blocked hm).2.1 + rw [nt.st] at blocked + rcases hxs with e0 | e0 <;> simp [x1, e0] at blocked <;> exact absurd blocked (by decide) + · refine ⟨?_, (fun _ h => by cases h), ?_, ?_⟩ + · rw [L.mem_wr_same (Notif.foot_st J.iplaced.n htZ).1]; exact code + · show (L.mem s' (Notif.badgeRegA t)).bv = Notif.badgeOf x1 + rw [Notif.badgeRegA_eq, kept _ (by simp) (by simp) (by simp), ← Notif.badgeRegA_eq] + exact nt.badge + · show (L.mem s' (prioA t)).val = x1.tcbPriority.toNat + rw [Notif.prioA_eq', kept _ (by simp) (by simp) (by simp), ← Notif.prioA_eq'] + exact nt.prio + exact h + have reply : ReplyStackRelA (L.mem s') Rp Z.C σ' (scNameA m) stack := by + refine V2.stk.frame (fun r hr' => ?_) (fun c _ y k h => ⟨y, k, ?_, rfl⟩) (fun r _ x' h => ⟨x', ?_, rfl⟩) + · have hr3 := J.iplaced.e.rep r hr' + have hdis := J.iplaced.e.rt r hr' t htZ + have key : ∀ o : U64, o.val = 8 ∨ o.val = 16 → L.mem s' (wa r o) = L.mem s2 (wa r o) := by + intro o ho + have hv : (wa r o).val = r.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + refine mem_wr_ne' sA (by unfold Aligned; rw [hv]; omega) ?_ + intro e + have ev := congrArg UScalar.val e + rw [stA_eq, Notif.tfield_val hta (by decide : (3328#u64 : U64).val < 4096), hv] at ev + simp at ev + omega + exact ⟨key _ (Or.inr (by simp [layout.reply.NEXT])), key _ (Or.inl (by simp [layout.reply.PREV]))⟩ + · have e : scNameA m c ≠ specPtr t := fun e => by rw [e, hk1] at h; cases h + rw [same _ e] + simpa [σ1, setObjA, Function.update_of_ne e] using h + · have e : specPtr r ≠ specPtr t := fun e => by + rw [show σ'.kheap (specPtr r) = some (.tcb x3) by rw [e]; exact atT] at h; cases h + rw [same _ e] at h + simpa [σ1, setObjA, Function.update_of_ne e] using h + have hni : specPtr t ≠ σ.idleThread := fun e => by + obtain ⟨y, hy, hs⟩ := J.sinv.idleTcb + rw [← e, hk] at hy; cases hy + rcases hxs with e0 | e0 <;> rw [e0] at hs <;> cases hs + have sinv : SInvXA σ' m n := by + have h1 : SInvXA σ1 m n := by + have same1 : ∀ p, p ≠ specPtr t → σ1.kheap p = σ.kheap p := fun p e => by + simp [σ1, setObjA, Function.update_of_ne e] + refine J.sinv.of_fr + { tcb := fun p y h => ?_, tcb' := fun p y h => ?_, sc := fun p y k => ?_, cur := rfl, idle := rfl, csc := rfl, + rq := rfl, rls := rfl, act := rfl, time := rfl, cons := rfl } (J.sinv.irqN.updTcbA hk rfl) + · by_cases e : p = specPtr t + · rw [e, hk] at h; cases h + exact ⟨x1, e ▸ hk1, rfl, rfl, rfl⟩ + · exact ⟨y, by rw [same1 p e]; exact h, rfl, rfl, rfl⟩ + · by_cases e : p = specPtr t + · rw [e, hk1] at h; cases h + exact ⟨x, e ▸ hk, rfl, rfl, rfl⟩ + · exact ⟨y, by rw [← same1 p e]; exact h, rfl, rfl, rfl⟩ + · by_cases e : p = specPtr t + · rw [e, hk1, hk] + constructor <;> intro h <;> cases h + · rw [same1 p e] + exact h1.restartA hk1 hni (ts := .running) rfl (fun h => by cases h) (by by_contra hf; have := J.sinv.faultSt.faultNotRun _ x hk hf; rw [this] at hr; cases hr) + have sidle : SIdleA σ' m n := by + have hidle : ∀ c < n, m.idleThreadOf c ≠ specPtr t := fun c hc e => by + obtain ⟨y, hy, hs⟩ := J.sidle.f3 c hc + rw [e, hk] at hy; cases hy + rcases hxs with e0 | e0 <;> rw [e0] at hs <;> cases hs + refine + { f3 := fun c hc => by + obtain ⟨y, hy, hs⟩ := J.sidle.f3 c hc + exact ⟨y, by rw [same _ (hidle c hc)]; exact hy, hs⟩ + f4 := fun y k h => J.sidle.f4 y k (backK _ _ h (fun _ e => by cases e)) + f5 := fun p y k h he => J.sidle.f5 p y k (backK _ _ h (fun _ e => by cases e)) (by rw [e'] at he; exact he) + f6 := J.sidle.f6.of_eq fun sl => by + unfold specCapAtA + by_cases e : sl.1 = specPtr t + · rw [e, atT, hk] + try rfl + · rw [same _ e] + f7 := fun y k h => J.sidle.f7 y k (backK _ _ h (fun _ e => by cases e)) + idleSlot := fun c hc => by + have h0 := J.sidle.idleSlot c hc + unfold vtCapA at h0 ⊢ + rw [same _ (hidle c hc)]; exact h0 + idleBound := fun c hc => by + have h0 := J.sidle.idleBound c hc + unfold vcpuOfA at h0 ⊢ + rw [same _ (hidle c hc)]; exact h0 } + have runVcpu : RunVcpuInv st Z.T s' := fun u hu h0 => by + have rvw : L.mem s' (runVcpuA u) = L.mem s (runVcpuA u) := + tcbW u hu 3672#u64 (by simp) (by simp) (by simp) (by simp) (by simp) (by simp) + rw [rvw] at h0 + have ne : u ≠ t := fun e => by + have h1 := J.runVcpu u hu h0 + have code := (J.ep.at_tcb htZ hk).st + rw [e] at h1 + rw [h1] at code + have h8 : (sched.TS_RUNNING_VM : U64).val = 8 := by unfold sched.TS_RUNNING_VM; rfl + rw [h8] at code + rcases hxs with e0 | e0 <;> rw [e0] at code <;> exact absurd code (by decide) + have hs : L.mem s' (stA u) = L.mem s (stA u) := by + have hua := J.sr.core.tcbs u hu + rw [stA_eq] + refine wAll.mem_eq (field_aligned hua (by simp) (by simp)) fun h => ?_ + rcases h with (h | h | h) | h + · exact field_ne hua hta (by simp) (by simp) (fun e => absurd e ne) h + · exact field_ne hua hta (by simp) (by simp) (fun e => absurd e ne) h + · exact field_ne hua hta (by simp) (by simp) (fun e => absurd e ne) h + · rw [stA_eq] at h + exact field_ne hua hta (by simp) (by simp) (fun e => absurd e ne) h + rw [hs] + exact J.runVcpu u hu h0 + have blkQ : BlkQueuedA Z.T E N (L.mem s') σ' := by + have tb : ∀ u ∈ Z.T, L.mem s' (tBlockA u) = L.mem s (tBlockA u) := fun u hu => + tcbW u hu 3336#u64 (by simp) (by simp) (by simp) (by simp) (by simp) (by simp) + have old : ∀ u, ∀ y, σ'.kheap (specPtr u) = some (.tcb y) → y.tcbState ≠ .running → + σ.kheap (specPtr u) = some (.tcb y) := fun u y h hn => by + by_cases e : specPtr u = specPtr t + · rw [e, atT] at h; cases h; exact absurd rfl hn + · rw [← same _ e]; exact h + refine ⟨fun u hu y h e hb => ?_, fun u hu y h p hb => ?_⟩ + · have hn : y.tcbState ≠ .running := fun h' => by rw [h'] at hb; cases hb + obtain ⟨c, z, hz, hm⟩ := J.blkQ.ep u hu y (old u y h hn) e hb + exact ⟨by rw [tb u hu]; exact c, z, keepK _ _ hz (fun _ e => by cases e), hm⟩ + · have hn : y.tcbState ≠ .running := fun h' => by rw [h'] at hb; cases hb + obtain ⟨c, z, l, hz, hl, hm⟩ := J.blkQ.ntfn u hu y (old u y h hn) p hb + exact ⟨by rw [tb u hu]; exact c, z, l, keepK _ _ hz (fun _ e => by cases e), hl, hm⟩ + have sv : SchedValidA σ' := by + have sc : ∀ p, ScActiveAtA σ p → ScActiveAtA σ' p := fun p ⟨y, k, e, act⟩ => + ⟨y, k, keepK p _ e (fun _ h => by cases h), act⟩ + have tcbAct : ∀ u, (∃ y c, σ.kheap u = some (.tcb y) ∧ y.tcbSchedContext = some c ∧ ScActiveAtA σ c) → + ∃ y c, σ'.kheap u = some (.tcb y) ∧ y.tcbSchedContext = some c ∧ ScActiveAtA σ' c := + fun u ⟨y, c, h, es, act⟩ => by + obtain ⟨y', h', es', -⟩ := tcbK u y h + exact ⟨y', c, h', es'.trans es, sc _ act⟩ + have hq : σ'.readyQueues = σ.readyQueues := by rw [e']; rfl + have hrl : σ'.releaseQueue = σ.releaseQueue := by rw [e']; rfl + have hact : σ'.schedulerAction = σ.schedulerAction := by rw [e']; rfl + have hc : σ'.curThread = σ.curThread := by rw [e']; rfl + have hi : σ'.idleThread = σ.idleThread := by rw [e']; rfl + refine ⟨fun d p u hm => ?_, fun e1 e2 => ?_, fun u y c e1 e2 e3 => ?_, fun u hu => ?_⟩ + · rw [hq] at hm + obtain ⟨act, nr⟩ := J.schedValid.rq d p u hm + exact ⟨tcbAct u act, by rw [hrl]; exact nr⟩ + · rw [hact] at e1 + rw [hc, hi] at e2 + obtain ⟨act, nr⟩ := J.schedValid.act e1 e2 + rw [hc, hrl] + exact ⟨tcbAct _ act, nr⟩ + · by_cases h : u = specPtr t + · rw [h, atT] at e1; cases e1; simp [x3, BlockedTsA] at e2 + · rw [same u h] at e1 + exact sc _ (J.schedValid.blk u y c e1 e2 e3) + · rw [hrl] at hu + exact tcbAct u (J.schedValid.rls u hu) + have Qk : QKeepA σ σ' := by + rw [e']; exact QKeepA.upd hk (QObjA.ofTcb rfl) + refine ⟨σ', hmem, ⟨replyUcA call x v, by rw [ct]; exact e'⟩, Q2, R2, ?_⟩ + exact J.of_ipcStepA (Q := Q2) (Rl := R2) (curOf := Z.curOf) (curScOf := Z.curScOf) wN HS SSz Qk + (by rw [e']; rfl) (fun _ _ => rfl) (BlkOK.bxi hbx2) sysl symL userOK + j7 sr views.1 views.2 reply K + (fun u hu O h h8 => tcbW u hu O (by omega) h8 (by omega) (by omega) (by omega) (by omega)) + sinv sidle runVcpu blkQ sv + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3ReplyRecvA.lean b/proofs/Fl4ProofsA64/P3/Stage3ReplyRecvA.lean new file mode 100644 index 000000000..692dadf6c --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ReplyRecvA.lean @@ -0,0 +1,592 @@ +/- +P3 (aarch64, A64-P3 line RecvPreA): **`ReplyRecv`: a live Reply followed by any `Recv` arm**. + +Both halves run inside one kernel-entry body (`dispatch_syscall`: `handle_invocation`, `after_invocation`, +`handle_recv`). The first half here is `decode_invocation` on the Reply cap, the success epilogue of +`handle_invocation` (the replier goes from `Restart` to `Running`) and `after_invocation` on `Ok`. + +* `JSyscallA.replyFirstA`: the first half ends in `RecvPreA` at the sets of the Reply arm, given what the replier's SC + is after the reply (`o`) and the owner of the current SC staying out of the release queue. +* **`JSyscallA.replyKeepRecvPreA`** / **`replyRecvA`**: the caller has its own SC; the replier keeps its SC. +* **`JSyscallA.replyPassiveRecvPreA`** / **`replyPassiveRecvA`**: the caller has no SC and the reply heads the current + SC's stack (the passive-server pattern); the replier ends with no SC. The arms that need no SC of the receiver + compose (`recvBlockA`, `recvBlockRA`, `nbRecvIdleA`, `nbWaitIdleA`, `waitBlockA`). + +[S] those of `JSyscallA.replyLiveA`; the woken caller has no endpoint timeout handler (so the SC tail sends no +timeout fault). The P1 invariants after the success epilogue are `epiP1A` (no premise). +Not here: `handle_invocation`'s front end (cap and buffer lookup), which the dispatch top owns. New module. +-/ +import Fl4ProofsA64.P3.Stage3ReplyArmA +import Fl4ProofsA64.P3.Stage3RecvPreA +import Fl4ProofsA64.P3.Stage3RecvPollA +import Fl4ProofsA64.P3.Stage3WrapA +import Fl4ProofsA64.P3.Stage3EpiP1A +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4Spec Fl4SpecA64 + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {s : S} {a : A} {σ : State Unit} {m : McState} + +/-- The caller named by a live reply object is a TCB. -/ +theorem JSyscallA.callerTcbA {nA : U64} + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {r d : U64} (hr : r ∈ Rp) {xr : Reply} (hk : σ.kheap (specPtr r) = some (.reply xr)) + (hcl : xr.replyTcb = some (specPtr d)) : ∃ xd, σ.kheap (specPtr d) = some (.tcb xd) := by + obtain ⟨yr, hyr, her⟩ := J.ep.reply r hr + rw [hk] at hyr + cases hyr + obtain ⟨h0, e⟩ := optRef_some (her.symm.trans hcl) + have ct : σ.curThread = specPtr (L.curW s) := J.j.1.sch.curThread + obtain ⟨xc, hkc, hrunc⟩ : ∃ xc, σ.kheap σ.curThread = some (.tcb xc) ∧ + (xc.tcbState = .running ∨ xc.tcbState = .restart) := by + obtain ⟨xc, hkc, -⟩ := J.ep.tcb (L.curW s) J.sr.curT + exact ⟨xc, by rw [ct]; exact hkc, J.toRecvPreA.state hkc⟩ + have J1T := J.toTA ⟨fun u h => absurd h (J.noSwitch u), ⟨xc, hkc, hrunc.imp id Or.inl⟩⟩ J.noSwitch + have hH : H r = some .reply := J.utIpc.reply r hr + have hd := (J1T.replyCallerA hH h0).1 + rw [specPtr_inj e] at hd + obtain ⟨xd, h, -⟩ := J.ep.tcb d hd + exact ⟨xd, h⟩ + +/-- The caller blocked on the reply is not the current thread. -/ +theorem JSyscallA.callerNeCurA {nA : U64} + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {r d : U64} (hrcv : ∀ y, σ.kheap (specPtr d) = some (.tcb y) → + y.tcbState = .blockedOnReply (specPtr r) ∧ y.tcbFault = none) + {xd : Tcb} (hkd : σ.kheap (specPtr d) = some (.tcb xd)) : specPtr d ≠ specPtr (L.curW s) := fun e => by + have h := (hrcv xd hkd).1 + rcases J.toRecvPreA.state (e ▸ hkd) with c | c <;> rw [c] at h <;> cases h + +set_option maxHeartbeats 2000000 in +/-- **The first half of `ReplyRecv` ends in `RecvPreA`** (module docstring). -/ +theorem JSyscallA.replyFirstA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hag : fl4MinBudget = MIN_BUDGET) (hmb : ∀ a, Fl4MinBudgetAgreeA ar a) + {r slot lbl len buf : U64} {grant blk call don fp : Bool} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Reply r grant) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hr : r ∈ Rp) {xr : Reply} (hk : σ.kheap (specPtr r) = some (.reply xr)) {d : U64} + (hcl : xr.replyTcb = some (specPtr d)) + (hsnd : ∀ x, σ.kheap σ.curThread = some (.tcb x) → x.tcbFault = none ∧ (grant = false ∨ + ipcBufOfA false x = none ∨ + unat (dataToMessageInfo (x.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0)) + (hrcv : ∀ y, σ.kheap (specPtr d) = some (.tcb y) → + y.tcbState = .blockedOnReply (specPtr r) ∧ y.tcbFault = none) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) (hconv : Fl4TicksToUsLegacyAgree) + (hnt : ∀ y, σ.kheap (specPtr d) = some (.tcb y) → isEpCap y.tcbTimeoutHandler = false) + {o : Option ObjRef} (ho : o = none ∨ o = some σ.curSc) + (hrep : ∀ (σ1 σ2 σ4 : State Unit) (s2 : S) (Q : U64 → U64 → List U64) (Rl : U64 → List U64) (stack' : U64 → List U64) (xc : Tcb), + σ.kheap σ.curThread = some (.tcb xc) → σ1 = stUpdA σ σ.curThread xc .restart → + JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack' κ n + (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s2 a σ2 m → + Sched.ReplyExitA (specPtr d) (specPtr r) σ1 σ4 σ2 → σ2.curSc = σ.curSc → σ2.consumedTime = σ.consumedTime → + (∀ y ∈ σ2.releaseQueue, y = specPtr d ∨ y ∈ σ.releaseQueue) → + (∀ x2, σ2.kheap (specPtr (L.curW s)) = some (.tcb x2) → x2.tcbSchedContext = o) ∧ + (∀ y k, σ2.kheap σ.curSc = some (.schedContext y k) → scActive y = true → σ.curSc ≠ idleScPtr → + σ.consumedTime ≠ 0 → ∀ ow, y.scTcb = some ow → ow ∉ σ2.releaseQueue)) : + ∃ xc σ1 σ2 σ3 s2 s3, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ((), σ2) ∈ (fl4DoReplyTransfer (Z := Unit) σ.curThread (specPtr r) grant σ1).1 ∧ + ((), σ3) ∈ (setThreadState (Z := Unit) σ.curThread .running σ2).1 ∧ + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don fp buf extra = + ok (.Ok (), s2, a) ∧ + obj.Tcb.state st (L.curW s) s2 = ok sched.TS_RESTART ∧ + sched.set_thread_state st s2 (L.curW s) sched.TS_RUNNING = ok s3 ∧ + syscall.after_invocation st ar s3 a (.Ok ()) = ok (true, s3, a) ∧ + L.curW s3 = L.curW s ∧ + σ3.curSc = σ.curSc ∧ (∀ x, σ3.kheap σ3.curThread = some (.tcb x) → x.tcbSchedContext = o) ∧ + getThreadState (Z := Unit) σ.curThread σ2 = ({(.restart, σ2)}, False) ∧ + ∃ Q Rl stack', RecvPreA st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack' κ n + (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s3 a σ3 m := by + obtain ⟨xc, σ1, σ2, s2, hkc, hσ1, hsp, hmem, hdec, _, Q, Rl, stack', J2, hex⟩ := + J.replyLiveXA (lbl := lbl) (len := len) (buf := buf) (blk := blk) (call := call) (don := don) (fp := fp) + hA hM hPl hfm hCs h1 hag hmb hslot hcov hcap hext hSC hr hk hcl hsnd hrcv hmax htu hconv + obtain ⟨σ4, K4, hk4, -, ns4, X⟩ := hex hnt + set t := L.curW s with ht_def + have htZ : t ∈ Z.T := J.sr.curT + have ct : σ.curThread = specPtr t := J.j.1.sch.curThread + have hkt : σ.kheap (specPtr t) = some (.tcb xc) := ct ▸ hkc + have P0' := J.toRecvPreA + have hrunc : xc.tcbState = .running ∨ xc.tcbState = .restart := P0'.state hkt + -- the caller `d` + obtain ⟨yr, hyr, her⟩ := J.ep.reply r hr + have hd : d ∈ Z.T := by + obtain ⟨xd, hxd⟩ : ∃ xd, σ.kheap (specPtr d) = some (.tcb xd) := by + rw [hk] at hyr + cases hyr + obtain ⟨h0, e⟩ := optRef_some (her.symm.trans hcl) + have J1T := J.toTA ⟨fun u h => absurd h (J.noSwitch u), ⟨xc, hkc, hrunc.imp id Or.inl⟩⟩ J.noSwitch + have hH : H r = some .reply := J.utIpc.reply r hr + have hd := (J1T.replyCallerA hH h0).1 + rw [specPtr_inj e] at hd + obtain ⟨xd, h, -⟩ := J.ep.tcb d hd + exact ⟨xd, h⟩ + obtain ⟨u, hu, e⟩ : ∃ u ∈ Z.T, specPtr d = specPtr u := by + rw [hk] at hyr + cases hyr + obtain ⟨h0, e⟩ := optRef_some (her.symm.trans hcl) + have J1T := J.toTA ⟨fun u h => absurd h (J.noSwitch u), ⟨xc, hkc, hrunc.imp id Or.inl⟩⟩ J.noSwitch + have hH : H r = some .reply := J.utIpc.reply r hr + exact ⟨_, (J1T.replyCallerA hH h0).1, e.symm⟩ + rw [specPtr_inj e]; exact hu + obtain ⟨xd, hkd, -⟩ := J.ep.tcb d hd + have hdt : specPtr d ≠ specPtr t := fun e => by + rw [e, hkt] at hkd + cases hkd + have h := (hrcv xc (e ▸ hkt)).1 + rcases hrunc with c | c <;> rw [c] at h <;> cases h + have htd : specPtr t ≠ specPtr d := Ne.symm hdt + -- the state after `Restart` + have k1 : ∀ p, p ≠ specPtr t → σ1.kheap p = σ.kheap p := fun p h => by + rw [hσ1, ct]; simp only [stUpdA, setObjA, Function.update_of_ne h] + have k1t : σ1.kheap (specPtr t) = some (.tcb { xc with tcbState := .restart }) := by + rw [hσ1, ct]; simp only [stUpdA, setObjA, Function.update_self] + have f1 : σ1.curThread = σ.curThread ∧ σ1.curSc = σ.curSc ∧ σ1.curTime = σ.curTime ∧ + σ1.consumedTime = σ.consumedTime ∧ σ1.readyQueues = σ.readyQueues ∧ σ1.releaseQueue = σ.releaseQueue ∧ + σ1.schedulerAction = σ.schedulerAction ∧ σ1.machineState = σ.machineState := by + rw [hσ1]; exact ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ + obtain ⟨f1t, f1c, f1tm, f1n, f1q, f1r, f1a, f1m⟩ := f1 + -- the Reply's end state + have f4 : σ4.curThread = σ1.curThread ∧ σ4.curTime = σ1.curTime ∧ σ4.consumedTime = σ1.consumedTime := by + rw [K4.frame]; exact ⟨rfl, rfl, rfl⟩ + have f2 : σ2.curThread = σ4.curThread ∧ σ2.curTime = σ4.curTime ∧ σ2.consumedTime = σ4.consumedTime := by + rw [X.tail.frame]; exact ⟨rfl, rfl, rfl⟩ + have thr2 : σ2.curThread = σ.curThread := f2.1.trans (f4.1.trans f1t) + have time2 : σ2.curTime = σ.curTime := f2.2.1.trans (f4.2.1.trans f1tm) + have cons2 : σ2.consumedTime = σ.consumedTime := f2.2.2.trans (f4.2.2.trans f1n) + have csc2 : σ2.curSc = σ.curSc := X.csc.csc.trans f1c + have lmt2 : σ2.machineState.lastMachineTime = σ.machineState.lastMachineTime := by rw [X.lmt, f1m] + have na1 : ∀ u, σ1.schedulerAction ≠ .switchThread u := fun u => by rw [f1a]; exact J.noSwitch u + have rq2 : ∀ dm p, specPtr t ∉ σ2.readyQueues dm p := fun dm p hm => by + rcases X.tail.rq ns4 dm p _ hm with hm | hm + · rcases K4.rq dm p _ hm with hm | hm | hm + · rw [f1q] at hm + exact P0'.noQ dm p (ct ▸ hm) + · exact htd hm + · exact na1 _ hm + · exact htd hm + have rl2 : ∀ y ∈ σ2.releaseQueue, y = specPtr d ∨ y ∈ σ.releaseQueue := fun y hy => by + rcases X.tail.rls y hy with h | h + · exact Or.inl h + · exact Or.inr (f1r ▸ K4.rls y h) + have nrl : specPtr t ∉ σ2.releaseQueue := fun h => by + rcases rl2 _ h with h | h + · exact htd h + · exact P0'.notRel (ct ▸ h) + have ns2 : σ2.schedulerAction ≠ .switchThread (specPtr t) := by + have a4 : σ4.schedulerAction ≠ .switchThread (specPtr t) := ns4 _ + rcases X.tail.act with h | h | h <;> rw [h] + · exact a4 + · intro c; cases c + · intro c; injection c with c; exact hdt c + have sw2 : SwA σ2 := X.tail.sw ns4 + -- the replier's SC after the reply + have hxsc : xc.tcbSchedContext = some σ.curSc := by + obtain ⟨y, hy, e, -⟩ := J.curInv.tcb + rw [hkc] at hy + cases hy + exact e + obtain ⟨x2, hk2, hx2st⟩ : ∃ x2, σ2.kheap (specPtr t) = some (.tcb x2) ∧ x2.tcbState = .restart := by + obtain ⟨y4, hy4, g⟩ := K4.tcbF k1t + rw [← hk4] at hy4 + exact ⟨y4, hy4, g.st.resolve_right fun c => htd c.1⟩ + have hkc2 : σ2.kheap σ2.curThread = some (.tcb x2) := by rw [thr2, ct]; exact hk2 + have rep := hrep σ1 σ2 σ4 s2 Q Rl stack' xc hkc hσ1 J2 X csc2 cons2 rl2 + have hx2sc : x2.tcbSchedContext = o := rep.1 x2 hk2 + have curne : σ.curSc ≠ specPtr t := fun e => by + obtain ⟨y, k, hy⟩ := J.curInv.sc + rw [e, hkt] at hy + cases hy + have obj2 : ∀ y' k, σ2.kheap σ.curSc = some (.schedContext y' k) → ∃ y, σ.kheap σ.curSc = some (.schedContext y k) ∧ + y'.scRefills = y.scRefills ∧ y'.scRefillMax = y.scRefillMax := fun y' k hy => by + obtain ⟨y, h, a, b⟩ := X.csc.sc y' k (by rw [f1c]; exact hy) + rw [f1c] at h + rw [k1 _ curne] at h + exact ⟨y, h, a, b⟩ + have B2 : BodyOKA σ2 m := + P0'.body.stepOwn sw2 time2 lmt2 thr2 csc2 cons2 obj2 rep.2 (fun _ x hx => by + rw [ct, hk2] at hx + cases hx + exact Or.inr hx2st) (fun hh => by + have h4 : σ4.schedulerAction = .resumeCurThread := by + rcases X.tail.act with e | e | e <;> first | (rw [e] at hh; exact hh) | (rw [e] at hh; cases hh) + rcases K4.act with e | e | ⟨y, -, e⟩ <;> first | (rw [e, f1a] at h4; exact h4) | (rw [e] at h4; cases h4)) + -- the success epilogue + have hcur2 : L.curW s2 = t := curW_keptA J.toJStage3A J2 thr2 + have htZ2 : t ∈ Z.T := htZ + have hw : L.mem s2 (stA (L.curW s2)) = sched.TS_RESTART := by + rw [hcur2] + have c := (J2.ep.at_tcb htZ2 hk2).st + rw [hx2st] at c + exact UScalar.eq_of_val_eq (by rw [c, ts_vals.2.1]; rfl) + have V : TcbIpcViewsA st { Z with Q := Q, R := Rl } m κ N E Rp Bf stack' s2 σ2 := + ⟨⟨_, _, J2.sr⟩, J2.ntfn, J2.ep, J2.reply, J2.mcp, J2.yieldRel⟩ + have e0 : setObjA σ2 (specPtr t) (.tcb { x2 with tcbArch := { x2.tcbArch with tcbContext := x2.tcbArch.tcbContext } }) = + σ2 := by + show { σ2 with kheap := Function.update σ2.kheap (specPtr t) (some (.tcb x2)) } = σ2 + rw [← hk2, Function.update_eq_self] + have hP1 : EpiP1A st AR ar hPl.ipa n m false := epiP1A hA hPl false + have fin := fun σX : State Unit => + hP1 _ H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack' κ s2 a σ2 J2 hw s2 rfl σX + rw [hcur2] at fin + obtain ⟨eRun, σ3, hset, e3, Q3, R3, J3⟩ := J2.runRestartA (t := t) hcur2 hk2 hx2st x2.tcbArch.tcbContext + (by rw [e0]; exact V) (WritesIn.refl _ _) fin + rw [e0] at hset + have e3' : σ3 = setObjA σ2 σ2.curThread + (.tcb { x2 with tcbArch := { x2.tcbArch with tcbContext := x2.tcbArch.tcbContext }, tcbState := .running }) := by + rw [thr2, ct]; exact e3 + have B3 : BodyOKA σ3 m := Sched.BodyOKA.runA (uc := x2.tcbArch.tcbContext) B2 hkc2 hx2st e3' + set x3 : Tcb := { x2 with tcbArch := { x2.tcbArch with tcbContext := x2.tcbArch.tcbContext }, tcbState := .running } + with hx3 + have hk3 : σ3.kheap (specPtr t) = some (.tcb x3) := by rw [e3]; simp [setObjA] + have g3 : σ3.curThread = σ2.curThread ∧ σ3.curSc = σ2.curSc ∧ σ3.readyQueues = σ2.readyQueues ∧ + σ3.releaseQueue = σ2.releaseQueue ∧ σ3.schedulerAction = σ2.schedulerAction := by + rw [e3]; exact ⟨rfl, rfl, rfl, rfl, rfl⟩ + obtain ⟨g3t, g3c, g3q, g3r, g3a⟩ := g3 + have ct3 : σ3.curThread = specPtr t := g3t.trans (thr2.trans ct) + have hcur3 : L.curW (L.wr s2 (stA t) sched.TS_RUNNING) = t := + curW_keptA J.toJStage3A J3 (g3t.trans thr2) + have far3 : ∀ p, p ≠ specPtr t → σ3.kheap p = σ2.kheap p := fun p h => by + rw [e3]; simp only [setObjA, Function.update_of_ne h] + have act3 : ScActiveAtA σ3 σ.curSc := by + obtain ⟨y2, k, hy2⟩ := J2.sinv.curSc + rw [csc2] at hy2 + obtain ⟨y, hy, -, em⟩ := obj2 y2 k hy2 + have a : ScActiveAtA σ σ.curSc := P0'.scAct xc _ hkc hxsc + obtain ⟨z, j, hz, ha⟩ := a + rw [hy] at hz + cases hz + refine ⟨y2, k, by rw [far3 _ curne]; exact hy2, ?_⟩ + unfold scActive at ha ⊢ + rw [em]; exact ha + have own3 : ∀ x, σ3.kheap σ3.curThread = some (.tcb x) → x.tcbSchedContext = o := fun x hx => by + rw [ct3, hk3] at hx + cases hx + exact hx2sc + have P3 : RecvPreA st AR { Z with Q := Q3, R := R3 } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack' κ n + (J5A st) (J7A st (J7R2A st ar hPl.ipa)) (L.wr s2 (stA t) sched.TS_RUNNING) a σ3 m := + { toJStage3A := J3 + body := B3 + running := by + rw [hcur3] + refine Or.inl ?_ + have c := (J3.ep.at_tcb htZ hk3).st + exact UScalar.eq_of_val_eq (by rw [c, ts_vals.1]; rfl) + noQ := fun dm p => by rw [g3q, ct3]; exact rq2 dm p + notRel := by rw [g3r, ct3]; exact nrl + scAct := fun x sc hx hs => by + have e := own3 x hx + rw [hs] at e + rcases ho with c | c + · rw [c] at e; cases e + · rw [c] at e + cases e + exact act3 + noSelf := by rw [g3a, ct3]; exact ns2 } + have mem3 : ((), σ3) ∈ (setThreadState (Z := Unit) σ.curThread .running σ2).1 := by + rw [ct, hset]; exact Set.mem_singleton _ + have hstate : obj.Tcb.state st t s2 = ok sched.TS_RESTART := by + have e : obj.Tcb.state st t s2 = ok (L.mem s2 (stA t)) := eq_of_spec_fd (P1.obj.Tcb.state.spec (st := st) s2 t) + rw [e, ← hcur2, hw] + have get2 : getThreadState (Z := Unit) σ.curThread σ2 = ({(.restart, σ2)}, False) := by + rw [ct, ← hx2st] + exact threadGet_eqA Tcb.tcbState hk2 + exact ⟨xc, σ1, σ2, σ3, s2, _, hkc, hσ1, hsp, hmem, mem3, hdec, hstate, eRun, rfl, hcur3, g3c.trans csc2, own3, + get2, Q3, R3, stack', P3⟩ + +set_option maxHeartbeats 1000000 in +/-- **The first half of `ReplyRecv` with a caller that has its own SC**: the replier keeps its SC. -/ +theorem JSyscallA.replyKeepRecvPreA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hag : fl4MinBudget = MIN_BUDGET) (hmb : ∀ a, Fl4MinBudgetAgreeA ar a) + {r slot lbl len buf : U64} {grant blk call don fp : Bool} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Reply r grant) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hr : r ∈ Rp) {xr : Reply} (hk : σ.kheap (specPtr r) = some (.reply xr)) {d : U64} + (hcl : xr.replyTcb = some (specPtr d)) + (hsnd : ∀ x, σ.kheap σ.curThread = some (.tcb x) → x.tcbFault = none ∧ (grant = false ∨ + ipcBufOfA false x = none ∨ + unat (dataToMessageInfo (x.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0)) + (hrcv : ∀ y, σ.kheap (specPtr d) = some (.tcb y) → + y.tcbState = .blockedOnReply (specPtr r) ∧ y.tcbFault = none) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) (hconv : Fl4TicksToUsLegacyAgree) + (hnt : ∀ y, σ.kheap (specPtr d) = some (.tcb y) → isEpCap y.tcbTimeoutHandler = false) + (hdsc : ∀ y, σ.kheap (specPtr d) = some (.tcb y) → y.tcbSchedContext ≠ none) : + ∃ xc σ1 σ2 σ3 s2 s3, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ((), σ2) ∈ (fl4DoReplyTransfer (Z := Unit) σ.curThread (specPtr r) grant σ1).1 ∧ + ((), σ3) ∈ (setThreadState (Z := Unit) σ.curThread .running σ2).1 ∧ + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don fp buf extra = + ok (.Ok (), s2, a) ∧ + obj.Tcb.state st (L.curW s) s2 = ok sched.TS_RESTART ∧ + sched.set_thread_state st s2 (L.curW s) sched.TS_RUNNING = ok s3 ∧ + syscall.after_invocation st ar s3 a (.Ok ()) = ok (true, s3, a) ∧ + L.curW s3 = L.curW s ∧ + (∀ x, σ3.kheap σ3.curThread = some (.tcb x) → x.tcbSchedContext = some σ3.curSc) ∧ + getThreadState (Z := Unit) σ.curThread σ2 = ({(.restart, σ2)}, False) ∧ + ∃ Q Rl stack', RecvPreA st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack' κ n + (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s3 a σ3 m := by + have htZ : L.curW s ∈ Z.T := J.sr.curT + have ct : σ.curThread = specPtr (L.curW s) := J.j.1.sch.curThread + obtain ⟨xd, hkd⟩ := J.callerTcbA hr hk hcl + have hdt : specPtr d ≠ specPtr (L.curW s) := J.callerNeCurA hrcv hkd + obtain ⟨xc, σ1, σ2, σ3, s2, s3, r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, csc3, own3, rest⟩ := + J.replyFirstA (lbl := lbl) (len := len) (buf := buf) (blk := blk) (call := call) (don := don) (fp := fp) + hA hM hPl hfm hCs h1 hag hmb hslot hcov hcap hext hSC hr hk hcl hsnd hrcv hmax htu hconv hnt (o := some σ.curSc) (Or.inr rfl) + (fun σ1 σ2 σ4 s2 Q Rl stack' xc hkc hσ1 J2 X csc2 cons2 rl2 => by + have k1 : ∀ p, p ≠ specPtr (L.curW s) → σ1.kheap p = σ.kheap p := fun p h => by + rw [hσ1, ct]; simp only [stUpdA, setObjA, Function.update_of_ne h] + have k1t : σ1.kheap (specPtr (L.curW s)) = some (.tcb { xc with tcbState := .restart }) := by + rw [hσ1, ct]; simp only [stUpdA, setObjA, Function.update_self] + have hkt : σ.kheap (specPtr (L.curW s)) = some (.tcb xc) := ct ▸ hkc + have hxsc : xc.tcbSchedContext = some σ.curSc := by + obtain ⟨y, hy, e, -⟩ := J.curInv.tcb + rw [hkc] at hy + cases hy + exact e + have keep := X.keepSc (fun x hx => by rw [k1 _ hdt, hkd] at hx; cases hx; exact hdsc xd hkd) + obtain ⟨x2, hk2, e2⟩ := keep _ _ k1t + have hx2sc : x2.tcbSchedContext = some σ.curSc := e2.trans hxsc + have back0 : ∀ y k, σ.kheap σ.curSc = some (.schedContext y k) → y.scTcb = some (specPtr (L.curW s)) := + fun y k hy => by + obtain ⟨z, j, hz, hzt⟩ := J.sr.inv.back _ xc _ hkt hxsc + rw [hy] at hz + cases hz + exact hzt + refine ⟨fun x hx => ?_, fun y k hy act hni hcon ow how hm => ?_⟩ + · rw [hk2] at hx; cases hx; exact hx2sc + · obtain ⟨z, j, hz, hzt⟩ := J2.sr.inv.back _ x2 _ hk2 hx2sc + rw [hy] at hz + cases hz + rw [hzt] at how + cases how + rcases rl2 _ hm with h | h + · exact hdt h.symm + · exact J.toRecvPreA.notRel (ct ▸ h)) + exact ⟨xc, σ1, σ2, σ3, s2, s3, r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, + fun x hx => by rw [csc3]; exact own3 x hx, rest⟩ + +set_option maxHeartbeats 1000000 in +/-- **The first half of a passive server's `ReplyRecv`**: the caller has no SC and the reply heads the current SC's +stack, so `reply_remove` returns the SC to the caller; the replier ends with no SC. -/ +theorem JSyscallA.replyPassiveRecvPreA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hag : fl4MinBudget = MIN_BUDGET) (hmb : ∀ a, Fl4MinBudgetAgreeA ar a) + {r slot lbl len buf : U64} {grant blk call don fp : Bool} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Reply r grant) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hr : r ∈ Rp) {xr : Reply} (hk : σ.kheap (specPtr r) = some (.reply xr)) {d : U64} + (hcl : xr.replyTcb = some (specPtr d)) + (hsnd : ∀ x, σ.kheap σ.curThread = some (.tcb x) → x.tcbFault = none ∧ (grant = false ∨ + ipcBufOfA false x = none ∨ + unat (dataToMessageInfo (x.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0)) + (hrcv : ∀ y, σ.kheap (specPtr d) = some (.tcb y) → + y.tcbState = .blockedOnReply (specPtr r) ∧ y.tcbFault = none) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) (hconv : Fl4TicksToUsLegacyAgree) + (hnt : ∀ y, σ.kheap (specPtr d) = some (.tcb y) → isEpCap y.tcbTimeoutHandler = false) + (hdsc : ∀ y, σ.kheap (specPtr d) = some (.tcb y) → y.tcbSchedContext = none) + (hsw : scWithReply (specPtr r) σ = some σ.curSc) + (hhd : ∀ y k, σ.kheap σ.curSc = some (.schedContext y k) → y.scReplies.head? = some (specPtr r)) : + ∃ xc σ1 σ2 σ3 s2 s3, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ((), σ2) ∈ (fl4DoReplyTransfer (Z := Unit) σ.curThread (specPtr r) grant σ1).1 ∧ + ((), σ3) ∈ (setThreadState (Z := Unit) σ.curThread .running σ2).1 ∧ + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don fp buf extra = + ok (.Ok (), s2, a) ∧ + obj.Tcb.state st (L.curW s) s2 = ok sched.TS_RESTART ∧ + sched.set_thread_state st s2 (L.curW s) sched.TS_RUNNING = ok s3 ∧ + syscall.after_invocation st ar s3 a (.Ok ()) = ok (true, s3, a) ∧ + L.curW s3 = L.curW s ∧ + (∀ x, σ3.kheap σ3.curThread = some (.tcb x) → x.tcbSchedContext = none) ∧ + getThreadState (Z := Unit) σ.curThread σ2 = ({(.restart, σ2)}, False) ∧ + ∃ Q Rl stack', RecvPreA st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack' κ n + (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s3 a σ3 m := by + have htZ : L.curW s ∈ Z.T := J.sr.curT + have ct : σ.curThread = specPtr (L.curW s) := J.j.1.sch.curThread + obtain ⟨xd, hkd⟩ := J.callerTcbA hr hk hcl + have hdt : specPtr d ≠ specPtr (L.curW s) := J.callerNeCurA hrcv hkd + obtain ⟨xc, σ1, σ2, σ3, s2, s3, r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, -, own3, rest⟩ := + J.replyFirstA (lbl := lbl) (len := len) (buf := buf) (blk := blk) (call := call) (don := don) (fp := fp) + hA hM hPl hfm hCs h1 hag hmb hslot hcov hcap hext hSC hr hk hcl hsnd hrcv hmax htu hconv hnt (o := none) (Or.inl rfl) + (fun σ1 σ2 σ4 s2 Q Rl stack' xc hkc hσ1 J2 X csc2 cons2 rl2 => by + have k1 : ∀ p, p ≠ specPtr (L.curW s) → σ1.kheap p = σ.kheap p := fun p h => by + rw [hσ1, ct]; simp only [stUpdA, setObjA, Function.update_of_ne h] + have k1t : σ1.kheap (specPtr (L.curW s)) = some (.tcb { xc with tcbState := .restart }) := by + rw [hσ1, ct]; simp only [stUpdA, setObjA, Function.update_self] + have hkt : σ.kheap (specPtr (L.curW s)) = some (.tcb xc) := ct ▸ hkc + have hxsc : xc.tcbSchedContext = some σ.curSc := by + obtain ⟨y, hy, e, -⟩ := J.curInv.tcb + rw [hkc] at hy + cases hy + exact e + obtain ⟨ys, ks, hys⟩ := J.curInv.sc + have curne : σ.curSc ≠ specPtr (L.curW s) := fun e => by rw [e, hkt] at hys; cases hys + have hys1 : σ1.kheap σ.curSc = some (.schedContext ys ks) := by rw [k1 _ curne]; exact hys + have hsw1 : scWithReply (specPtr r) σ1 = some σ.curSc := by + rw [← hsw] + unfold scWithReply + congr 1 + funext p + refine decide_eq_decide.2 ?_ + by_cases e : p = specPtr (L.curW s) + · rw [e, k1t, hkt] + constructor <;> rintro ⟨_, _, h, -⟩ <;> cases h + · rw [k1 p e] + have hkd1 : σ1.kheap (specPtr d) = some (.tcb xd) := by rw [k1 _ hdt]; exact hkd + obtain ⟨σr, hrm, hall⟩ := X.afterRm + obtain ⟨⟨zd, hzd, esd⟩, oth⟩ := Sched.replyRemove_donateScA (i := σ.idleThread) hkd1 (hdsc xd hkd) hsw1 hys1 + (hhd ys ks hys) hrm + obtain ⟨zt, hzt, est⟩ := oth _ _ hdt.symm k1t + obtain ⟨yd, hyd, eyd⟩ := hall _ _ hzd + obtain ⟨yt, hyt, eyt⟩ := hall _ _ hzt + have hdsc2 : yd.tcbSchedContext = some σ2.curSc := by rw [eyd, esd, csc2] + obtain ⟨z, j, hz, hzo⟩ := J2.sr.inv.back _ yd _ hyd hdsc2 + have hnone : yt.tcbSchedContext = none := by + rw [eyt] + rcases est with e | e + · exfalso + have e' : yt.tcbSchedContext = some σ2.curSc := by rw [eyt, e, csc2]; exact hxsc + obtain ⟨z', j', hz', hzo'⟩ := J2.sr.inv.back _ yt _ hyt e' + rw [hz] at hz' + cases hz' + exact hdt (Option.some.inj (hzo.symm.trans hzo')) + · exact e + refine ⟨fun x hx => ?_, fun y k hy _ _ hcon ow how hm => ?_⟩ + · rw [hyt] at hx; cases hx; exact hnone + · rw [csc2] at hz + rw [hy] at hz + cases hz + rw [hzo] at how + cases how + exact hcon (cons2 ▸ J2.sinv.zc _ yd hyd hdsc2 hm)) + exact ⟨xc, σ1, σ2, σ3, s2, s3, r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, own3, rest⟩ + +/-- **`ReplyRecv`: a live Reply in which the replier keeps its SC, followed by any `Recv` arm** (module docstring). +The arm is applied at the `RecvPreA` state; its `HRecvPostA` composes with the first half. -/ +theorem JSyscallA.replyRecvA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hag : fl4MinBudget = MIN_BUDGET) (hmb : ∀ a, Fl4MinBudgetAgreeA ar a) + {r slot lbl len buf : U64} {grant blk call don fp : Bool} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Reply r grant) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hr : r ∈ Rp) {xr : Reply} (hk : σ.kheap (specPtr r) = some (.reply xr)) {d : U64} + (hcl : xr.replyTcb = some (specPtr d)) + (hsnd : ∀ x, σ.kheap σ.curThread = some (.tcb x) → x.tcbFault = none ∧ (grant = false ∨ + ipcBufOfA false x = none ∨ + unat (dataToMessageInfo (x.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0)) + (hrcv : ∀ y, σ.kheap (specPtr d) = some (.tcb y) → + y.tcbState = .blockedOnReply (specPtr r) ∧ y.tcbFault = none) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) (hconv : Fl4TicksToUsLegacyAgree) + (hdsc : ∀ y, σ.kheap (specPtr d) = some (.tcb y) → y.tcbSchedContext ≠ none) + (hnt : ∀ y, σ.kheap (specPtr d) = some (.tcb y) → isEpCap y.tcbTimeoutHandler = false) : + ∃ xc σ1 σ2 σ3 s2 s3, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ((), σ2) ∈ (fl4DoReplyTransfer (Z := Unit) σ.curThread (specPtr r) grant σ1).1 ∧ + ((), σ3) ∈ (setThreadState (Z := Unit) σ.curThread .running σ2).1 ∧ + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don fp buf extra = + ok (.Ok (), s2, a) ∧ + obj.Tcb.state st (L.curW s) s2 = ok sched.TS_RESTART ∧ + sched.set_thread_state st s2 (L.curW s) sched.TS_RUNNING = ok s3 ∧ + syscall.after_invocation st ar s3 a (.Ok ()) = ok (true, s3, a) ∧ + (∀ x, σ3.kheap σ3.curThread = some (.tcb x) → x.tcbSchedContext = some σ3.curSc) ∧ + ∃ Q Rl stack', RecvPreA st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack' κ n + (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s3 a σ3 m ∧ + ∀ (blocking canReply : Bool) (Out : State Unit → Prop), + HRecvPostA st AR ar hPl.ipa n Z.Cs s3 a m blocking canReply σ3 Out → + ∃ s4 a4 σ4, syscall.handle_recv st ar s3 a blocking canReply = ok (s4, a4) ∧ + ((), σ4) ∈ (handleRecv (Z := Unit) blocking canReply σ3).1 ∧ Out σ4 ∧ + A64RelS3 st AR ar hPl.ipa n Z.Cs s4 a4 σ4 m ∧ L.curW s4 = L.curW s ∧ BodyOKA σ4 m := by + obtain ⟨xc, σ1, σ2, σ3, s2, s3, hkc, hσ1, hsp, hmem, mem3, hdec, hstate, eRun, haft, hcur3, own3, + -, Q, Rl, stack', P⟩ := + J.replyKeepRecvPreA (lbl := lbl) (len := len) (buf := buf) (blk := blk) (call := call) (don := don) (fp := fp) + hA hM hPl hfm hCs h1 hag hmb hslot hcov hcap hext hSC hr hk hcl hsnd hrcv hmax htu hconv hnt hdsc + refine ⟨xc, σ1, σ2, σ3, s2, s3, hkc, hσ1, hsp, hmem, mem3, hdec, hstate, eRun, haft, own3, Q, Rl, stack', P, + fun blocking canReply Out h => ?_⟩ + obtain ⟨s4, a4, σ4, h1, h2, h3, h4, h5, h6⟩ := h + exact ⟨s4, a4, σ4, h1, h2, h3, h4, h5.trans hcur3, h6⟩ + + +set_option maxHeartbeats 1000000 in +/-- **A passive server's `ReplyRecv`**: the SC goes back to the caller, then any `Recv` arm needing no SC. -/ +theorem JSyscallA.replyPassiveRecvA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hag : fl4MinBudget = MIN_BUDGET) (hmb : ∀ a, Fl4MinBudgetAgreeA ar a) + {r slot lbl len buf : U64} {grant blk call don fp : Bool} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Reply r grant) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hr : r ∈ Rp) {xr : Reply} (hk : σ.kheap (specPtr r) = some (.reply xr)) {d : U64} + (hcl : xr.replyTcb = some (specPtr d)) + (hsnd : ∀ x, σ.kheap σ.curThread = some (.tcb x) → x.tcbFault = none ∧ (grant = false ∨ + ipcBufOfA false x = none ∨ + unat (dataToMessageInfo (x.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0)) + (hrcv : ∀ y, σ.kheap (specPtr d) = some (.tcb y) → + y.tcbState = .blockedOnReply (specPtr r) ∧ y.tcbFault = none) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) (hconv : Fl4TicksToUsLegacyAgree) + (hdsc : ∀ y, σ.kheap (specPtr d) = some (.tcb y) → y.tcbSchedContext = none) + (hsw : scWithReply (specPtr r) σ = some σ.curSc) + (hhd : ∀ y k, σ.kheap σ.curSc = some (.schedContext y k) → y.scReplies.head? = some (specPtr r)) + (hnt : ∀ y, σ.kheap (specPtr d) = some (.tcb y) → isEpCap y.tcbTimeoutHandler = false) : + ∃ xc σ1 σ2 σ3 s2 s3, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ((), σ2) ∈ (fl4DoReplyTransfer (Z := Unit) σ.curThread (specPtr r) grant σ1).1 ∧ + ((), σ3) ∈ (setThreadState (Z := Unit) σ.curThread .running σ2).1 ∧ + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don fp buf extra = + ok (.Ok (), s2, a) ∧ + obj.Tcb.state st (L.curW s) s2 = ok sched.TS_RESTART ∧ + sched.set_thread_state st s2 (L.curW s) sched.TS_RUNNING = ok s3 ∧ + syscall.after_invocation st ar s3 a (.Ok ()) = ok (true, s3, a) ∧ + (∀ x, σ3.kheap σ3.curThread = some (.tcb x) → x.tcbSchedContext = none) ∧ + ∃ Q Rl stack', RecvPreA st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack' κ n + (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s3 a σ3 m ∧ + ∀ (blocking canReply : Bool) (Out : State Unit → Prop), + HRecvPostA st AR ar hPl.ipa n Z.Cs s3 a m blocking canReply σ3 Out → + ∃ s4 a4 σ4, syscall.handle_recv st ar s3 a blocking canReply = ok (s4, a4) ∧ + ((), σ4) ∈ (handleRecv (Z := Unit) blocking canReply σ3).1 ∧ Out σ4 ∧ + A64RelS3 st AR ar hPl.ipa n Z.Cs s4 a4 σ4 m ∧ L.curW s4 = L.curW s ∧ BodyOKA σ4 m := by + obtain ⟨xc, σ1, σ2, σ3, s2, s3, hkc, hσ1, hsp, hmem, mem3, hdec, hstate, eRun, haft, hcur3, own3, + -, Q, Rl, stack', P⟩ := + J.replyPassiveRecvPreA (lbl := lbl) (len := len) (buf := buf) (blk := blk) (call := call) (don := don) (fp := fp) + hA hM hPl hfm hCs h1 hag hmb hslot hcov hcap hext hSC hr hk hcl hsnd hrcv hmax htu hconv hnt hdsc hsw hhd + refine ⟨xc, σ1, σ2, σ3, s2, s3, hkc, hσ1, hsp, hmem, mem3, hdec, hstate, eRun, haft, own3, Q, Rl, stack', P, + fun blocking canReply Out h => ?_⟩ + obtain ⟨s4, a4, σ4, h1, h2, h3, h4, h5, h6⟩ := h + exact ⟨s4, a4, σ4, h1, h2, h3, h4, h5.trans hcur3, h6⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3ReplyRemQA.lean b/proofs/Fl4ProofsA64/P3/Stage3ReplyRemQA.lean new file mode 100644 index 000000000..a731c8c3b --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ReplyRemQA.lean @@ -0,0 +1,124 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2b): **the live Reply's removal with the SC holder queued or not**, the sibling of +`Endp.replyRemoveThenUnblockA` over `ReplyPopLocalQA` (the holder's locality is `aff = cpu` and a no-op stall: no VCPU +or queue condition), with the narrow write set `RemWrA`. + +* **`replyRemoveThenUnblockQA`**. +* `ReplyNwA`: the live Reply's narrow write set; `replyNwFootA` (it is inside `ReplyFootW` for a receiver of `Z.T`). + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.ReplyLiveJointA +import Fl4ProofsA64.P3.ReplyRemoveQA +import Fl4ProofsA64.P3.ReplyRemoveFootQA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4SpecA64 Sched + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +private theorem reachedQA {α : Type} {r : Result α} {P : α → Prop} (step : r ⦃ P ⦄) : + r ⦃ v => r = ok v ∧ P v ⦄ := by + obtain ⟨v, eq, post⟩ := spec_ex_fd step + rw [eq, WP.spec_ok] + exact ⟨rfl, post⟩ + +section Store +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + +/-- The live Reply's write set: the removal's words, the receiver's registers and the IPC buffers. -/ +def ReplyNwA (Z : XSetsA) (m : McState) (σ : State Unit) (s : S) (R : Set U64) (r t : U64) + (Bf : Set (U64 × Word)) (b : U64) : Prop := + RemWrA (st := st) Z m σ s R r t b ∨ RcvRegA t b ∨ BufW Bf b + +/-- **The removal, then the refill test from the reached state** (holder queued or not). -/ +theorem replyRemoveThenUnblockQA {s : S} {σ : State Unit} (a : A) {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {r t : U64} (hr : r ∈ R) (ht : t ∈ Z.T) + {xt : Tcb} {xr : Reply} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hkr : σ.kheap (specPtr r) = some (.reply xr)) (hb : xr.replyTcb = some (specPtr t)) + (hst : xt.tcbState = .blockedOnReply (specPtr r)) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue) + (cov : ScReplyCovA Z.C m σ (specPtr r)) + (hsc0 : ∀ sc ∈ Z.C, (stack sc).head? = some r → sc ≠ 0#u64) + (hlocal : ∀ sc ∈ Z.C, (stack sc).head? = some r → xt.tcbSchedContext = none → + ReplyPopLocalQA (st := st) (ar := ar) (Z := Z) (m := m) a s σ sc t r) + (unb : ∀ s1 σ1 Q1 R1, SchedSRA st { Z with Q := Q1, R := R1 } m κ s1 σ1 → RemKeepA σ σ1 → + replyRemove (Z := Unit) (specPtr t) (specPtr r) σ = ({((), σ1)}, False) → + (L.mem s1 (scA t) ≠ 0#u64 → L.mem s1 (scCoreA (L.mem s1 (scA t))) = L.cpu s1) ∧ + (specPtr t ∈ σ1.releaseQueue → ∀ y n, + σ1.kheap (scNameA m (L.mem s1 (scA t))) = some (.schedContext y n) → + refillReady σ1.curTime (refillHd y) = false)) + (hoff : ∀ y, IsSlotA Z.th y → ¬ EWrite Z.T E R Bf y ∧ ¬ EWrite Z.T E R Bf (wa y 8#u64)) + (hSt : StateRelA Z.th (L.mem s) σ) (hV : SlotsValidA Z.th (L.mem s)) + (hC : BufCover st Z.T Bf s σ) (hNative : BufNativeA Z.T (L.mem s)) : + ipc.reply_remove st ar s a r t ⦃ out => ∃ σ1, + replyRemove (Z := Unit) (specPtr t) (specPtr r) σ = ({((), σ1)}, False) ∧ out.2 = a ∧ + replyUnblockProgramA st out.1 t ⦃ s2 => ∃ σ2, + (getTcbObjRef (Z := Unit) Tcb.tcbSchedContext (specPtr t) >>= + ifSporadicCurScTestRefillUnblockCheck) σ1 = ({((), σ2)}, False) ∧ + ReplyMidA st Z m κ E R Bf s σ r t s2 σ2 ∧ + WritesIn st s s2 (RemWrA (st := st) Z m σ s R r t) ⦄ ⦄ := by + have hcls : ¬ inClassRSc xt.tcbState xt.tcbFault := by + rw [hst] + simp [inClassRSc, runnable] + have hTcb : ∀ u ∈ Z.T, ∃ x0, σ.kheap (specPtr u) = some (.tcb x0) := fun u hu => by + obtain ⟨x, hx, -⟩ := HE.tcb u hu + exact ⟨x, hx⟩ + refine WP.spec_mono (reachedQA (WP.spec_and + (reply_remove_relQA_sub (st := st) (ar := ar) a H P PE HE HS hr ht cov hkt hkr hb (Or.inl hst) hq hsc0 + hlocal hcls) + (reply_remove_footQA (st := st) (ar := ar) a H P PE HE HS hr ht hkt hkr hb (Or.inl hst) hq cov hsc0 + hlocal hcls))) ?_ + rintro ⟨s1, a1⟩ ⟨-, ⟨ha, σ1, stack', hu1, ⟨Q1, R1, H1⟩, E1, S1, iso1, -⟩, wn, σ1', hu1', keep1⟩ + dsimp only at ha H1 E1 S1 iso1 wn ⊢ + have e1 : σ1' = σ1 := by + have := hu1'.symm.trans hu1 + simpa using congrArg Prod.fst this + subst e1 + have w1 : WritesIn st s s1 (ReplyFootW (st := st) Z m σ s E R Bf) := wn.mono fun b h => by + rcases h with h | h | h | h + · exact Or.inr (Or.inr h) + · exact Or.inl (Or.inr (Or.inl ⟨r, hr, by rw [h, Endp.rTcbA_val]⟩)) + · refine Or.inl (Or.inr (Or.inr (Or.inl ⟨t, ht, Or.inl ?_⟩))) + have v := Endp.val_st (PE.tcb t ht) + rw [h] + exact ⟨by omega, by rw [v]; unfold TWOffN; omega⟩ + · exact Or.inr (Or.inl h) + have X1 := replyXferFactsA H.toSchedSRN PE P hoff w1 keep1 (Fl4ProofsA64.P3.Sched.IrqSlotsKeepA.of_replyFoot H.toSchedSRN PE P w1) hSt hV hC hNative hTcb + obtain ⟨hon, hrq⟩ := unb s1 σ1' Q1 R1 H1 keep1 hu1 + obtain ⟨x1, hk1, -⟩ := E1.tcb t ht + obtain ⟨yi, hyi, hyst⟩ := replyRemove_inactiveA hu1 + refine ⟨σ1', hu1, ha, ?_⟩ + refine WP.spec_mono (reachedQA (replyUnblockFrameA H1 P PE E1 S1 ht hr iso1 hk1 hon hrq)) ?_ + rintro s2 ⟨-, σ2, hu2, H2, E2, S2', iso2, F, -, hmem, -⟩ + have keep2 : RemKeepA σ1' σ2 := RemKeepA.of_skeep F.keep hmem + have w2 : WritesIn st s1 s2 (ReplyFootW (st := st) { Z with Q := Q1, R := R1 } m σ1' s1 E R Bf) := + F.wr.mono fun b hb => Or.inr (Or.inl hb) + have hTcb1 : ∀ u ∈ Z.T, ∃ x0, σ1'.kheap (specPtr u) = some (.tcb x0) := fun u hu => by + obtain ⟨x, hx, -⟩ := E1.tcb u hu + exact ⟨x, hx⟩ + have X2 := replyXferFactsA (Z := { Z with Q := Q1, R := R1 }) H1.toSchedSRN PE P hoff w2 keep2 + (Fl4ProofsA64.P3.Sched.IrqSlotsKeepA.of_replyFoot H1.toSchedSRN PE P w2) X1.1 X1.2.1 X1.2.2.1 X1.2.2.2 hTcb1 + obtain ⟨y2, hy2, o2, ey2⟩ := F.keep.tcb _ _ hyi + have hz1 : SSzA σ σ1' := (SPresInst.pres (m := replyRemove (Z := Unit) (specPtr t) (specPtr r))) σ ((), σ1') + (by rw [hu1]; exact Set.mem_singleton _) |>.2.1 + have wn2 : WritesIn st s s2 (RemWrA (st := st) Z m σ s R r t) := + (wn.trans F.wr).mono fun b hb => hb.elim id fun h => + Or.inr (Or.inr (Or.inr (mwr_back (Z := Z) (Z' := { Z with Q := Q1, R := R1 }) rfl rfl hz1 wn.1 h))) + refine ⟨σ2, hu2, ⟨Q1, R1, stack', H2, E2, S2', iso2, keep1.trans keep2, X2.1, X2.2.1, X2.2.2.1, + X2.2.2.2, ⟨y2, hy2, ?_⟩, ReplyFootW.trans rfl rfl w1 hz1 w2, hz1.trans F.sz⟩, wn2⟩ + rw [ey2] + exact hyst + +end Store + +end + +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/Stage3ReplyTmoBlkA.lean b/proofs/Fl4ProofsA64/P3/Stage3ReplyTmoBlkA.lean new file mode 100644 index 000000000..69e4a458a --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ReplyTmoBlkA.lean @@ -0,0 +1,377 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2b): **the nested timeout send of a Reply, no receiver waiting** on the handler +endpoint, at the record: the sibling of `Stage3ReplyTmoRecvA` (the woken caller `t` blocks on the endpoint). + +* `handle_timeout_blockXA`: `handle_timeout_blockBA` with the faulted TCB and state as opaque names. +* **`JStage3A.replyTmoBlkA`**: the record at the reached state, given its P1 parts (`ReplyTmoOutA`), by + `JStage3A.of_ipcStepUXSA`. + +Named premises: [P] `hmax`, `hconv`. Case selectors: the handler cap (`hslot`, `hcap`, `hgc`, `he`) and an idle or +sending endpoint (`hk`, `hc`). Inputs from the arm: `M` (the mid state), `wk`. New module. +-/ +import Fl4ProofsA64.P3.Stage3ReplyTmoRecvA +import Fl4ProofsA64.P3.Stage3KEntrySendBlkA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + +/-- `handle_timeout_blockBA` with the faulted TCB and state named. -/ +theorem handle_timeout_blockXA {s : S} {σ : State Unit} (a : A) + (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) (hconv : Fl4TicksToUsLegacyAgree) + {t : U64} (H : SchedSRB st Z m κ s σ (specPtr t)) (hM : McpRelA Z.T m (L.mem s) σ) + (hY : YieldRelA Z.T Z.C m (L.mem s) σ) + (ht : t ∈ Z.T) {xt : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hRun : SndRunA σ (specPtr t) xt) + {slot ep hb : U64} {sd rv g gr : Bool} + (hslot : obj.Tcb.slot t layout.tcb.SLOT_TIMEOUT_HANDLER = ok slot) + (hcap : obj.Slot.cap st slot s = ok (.Endpoint ep hb sd rv g gr)) + (hgc : getCap (Z := Unit) (getTcbTimeoutHandlerPtr (specPtr t)) σ = + ({(.endpointCap (specPtr ep) hb.bv (rightsOfA sd rv g gr), σ)}, False)) + (he : ep ∈ E) {x : Endpoint} (hk : σ.kheap (specPtr ep) = some (.endpoint x)) (hc : CanBlockA false x) + (hs : PrioSortedA σ x.epQueue) (badge : U64) : + faults.handle_timeout st ar s a t (.Timeout badge) ⦃ r => r.2 = a ∧ ∃ (x1 : Tcb) (σf τ : State Unit), + x1 = { xt with tcbFault := some (.timeout badge.bv) } ∧ σf = updK σ (specPtr t) (.tcb x1) ∧ + fl4HandleTimeout (Z := Unit) (specPtr t) (.timeout badge.bv) σ = ({((), τ)}, False) ∧ + BlkStepA σf τ (specPtr t) (specPtr ep) x x1 (sndPlA hb.bv g gr false) ∧ + (∃ Q' R', YieldQueueRelA (st := st) { Z with Q := Q', R := R' } m κ r.1 τ) ∧ + EpStateRel Z.T E R Bf (L.mem r.1) τ ∧ + WritesIn st s r.1 (fun b => FaultWA t b ∨ + BlkSendFpA st Z.T (fun σ s => MWrA (st := st) Z m σ s) σ s t ep b) ∧ + SSzA σ τ ⦄ := by + refine WP.spec_mono (handle_timeout_blockBA (st := st) (ar := ar) (N := N) a P PE HE hmax hconv H hM hY ht hkt + hRun hslot hcap hgc he hk hc hs badge) ?_ + rintro r ⟨ha, h⟩ + obtain ⟨h1, -, h3, h4, h5, h6, h7⟩ := h + exact ⟨ha, _, _, _, rfl, rfl, h1, h3, h4, h5, h6, h7⟩ + +end + +/-- `NtfnWaitKeepMA` composed. -/ +theorem ntfnWaitMA_compMA {M1 M2 M : ObjRef → Prop} {T N : Set U64} {m1 m2 m3 : U64 → U64} + (h1 : NtfnWaitKeepMA M1 T N m1 m2) (h2 : NtfnWaitKeepMA M2 T N m2 m3) (hM1 : ∀ p, M1 p → M p) + (hM2 : ∀ p, M2 p → M p) : NtfnWaitKeepMA M T N m1 m3 := by + intro y hy hn hN + obtain ⟨a1, b1, c1⟩ := h1 y hy (fun c => hn (hM1 _ c)) hN + obtain ⟨a2, b2, c2⟩ := h2 y hy (fun c => hn (hM2 _ c)) (by rw [a1]; exact hN) + exact ⟨a2.trans a1, b2.trans b1, c2.trans c1⟩ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s1 : S} {a : A} {σ1 : State Unit} {m : McState} + +set_option maxHeartbeats 4000000 in +/-- **The nested timeout send with no receiver waiting, at the record** (module docstring). -/ +theorem JStage3A.replyTmoBlkA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s1 a σ1 m) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) (hconv : Fl4TicksToUsLegacyAgree) + (hsw : ∀ u, σ1.schedulerAction ≠ .switchThread u) + {t : U64} (ht : t ∈ Z.T) (hcur : specPtr t ≠ σ1.curThread) + (hidle : ∀ c < fl4A64Platform.numNodes, specPtr t ≠ m.idleThreadOf c) + {xt0 : Tcb} {rr : ObjRef} (hkt0 : σ1.kheap (specPtr t) = some (.tcb xt0)) + (hst0 : xt0.tcbState = .blockedOnReply rr) + {σ0 : State Unit} {Wn : U64 → Prop} {s4 : S} {σ4 : State Unit} {Q : U64 → U64 → List U64} + {R' : U64 → List U64} {stack' : U64 → List U64} {x : Tcb} + (MD : ReplyTmoMidA st Z m κ E Rp Bf s1 σ1 σ0 t Wn s4 σ4 Q R' stack' x) + (wk : NtfnWaitKeepMA (· = specPtr t) Z.T N (L.mem s1) (L.mem s4)) + (badge : U64) {slot ep hbg : U64} {sd rv g gr : Bool} + (hslot : obj.Tcb.slot t layout.tcb.SLOT_TIMEOUT_HANDLER = ok slot) + (hcap : obj.Slot.cap st slot s4 = ok (.Endpoint ep hbg sd rv g gr)) + (hgc : tcbCnodeMap x (tcbCnodeIndex 4) = some (.endpointCap (specPtr ep) hbg.bv (rightsOfA sd rv g gr))) + (he : ep ∈ E) {xe : Endpoint} (hk : σ4.kheap (specPtr ep) = some (.endpoint xe)) (hc : CanBlockA false xe) : + faults.handle_timeout st ar s4 a t (.Timeout badge) ⦃ o => ∃ σ', + ((), σ') ∈ (fl4HandleTimeout (Z := Unit) (specPtr t) (.timeout badge.bv) σ4).1 ∧ + ReplyTmoOutA st ar AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf κ n nA m a o.1 o.2 σ' ∧ + Sched.RepEndA σ1 σ' ⦄ := by + have HB := MD.sr + have hx4 := MD.kx + have K04 : SIKXA σ1.idleThread (· = specPtr t) (· = specPtr t) true σ1 σ4 := SIKXA.ofClr MD.keep MD.clr + have fr4 := K04.frame + have hidle4 : σ4.idleThread = σ1.idleThread := by rw [fr4] + have hσ0f : ∀ p, p ≠ specPtr t → σ0.kheap p = σ1.kheap p := fun p hp => by + rcases MD.clr with e | ⟨z, -, e⟩ + · rw [e] + · rw [e]; exact faultClrA_ne hp + have hnI : specPtr t ≠ σ1.idleThread := fun e => by + obtain ⟨xi, hxi, hsi⟩ := J.sinv.idleTcb + rw [← e, hkt0] at hxi; cases hxi; rw [hst0] at hsi; cases hsi + have hRun : SndRunA σ4 (specPtr t) x := + ⟨MD.run, ⟨MD.nq, MD.nrel⟩, fun u e => (MD.nsw u e).2, by rw [hidle4]; exact hnI⟩ + -- the write set of the Reply part and the relations at the mid state + have wI : WritesIn st s1 s4 (fun b => IpcFpA st Z E Rp Bf m σ1 s1 b ∨ RepWA Rp b) := MD.w.mono fun b h => by + rcases h with h | h | ⟨q', hq, h⟩ + · exact Or.inl (Or.inl h) + · exact Or.inl (Or.inr h) + · have h2 := replyNextPlaceA (Z := Z) J.iplaced.e hq + have h3 := replyPrevPlaceA (Z := Z) J.iplaced.e hq + refine Or.inr ⟨q', hq, ?_⟩ + rcases h with h | h <;> rw [h] <;> omega + have tw4 := J.tw_ipcA wI + have KN4 : IpcKeepNA σ1 σ4 := Fl4ProofsA64.P3.SIKXA.ipcKeepN K04 + have kb4 : L.ksb s4 = L.ksb s1 := MD.w.1 + have slotK : ∀ y', IsSlotA Z.th y' → capAtA (L.mem s4) y' = capAtA (L.mem s1) y' := by + intro y' hy' + have keep := slotWordsKeepA J.sr.toSchedSRN J.iplaced.e J.ipcPlace J.slotOffEwA MD.w + (Fl4ProofsA64.P3.Sched.IrqSlotsKeepA.of_replyFoot J.sr.toSchedSRN J.iplaced.e J.ipcPlace MD.w) _ hy' + unfold capAtA + rw [keep.1, keep.2] + have at_ : ∀ u ∈ Z.T, ∃ x', σ1.kheap (specPtr u) = some (.tcb x') := fun u hu => by + obtain ⟨x', hx', -⟩ := J.ntfn.tcb u hu + exact ⟨x', hx'⟩ + have hM4 : McpRelA Z.T m (L.mem s4) σ4 := + J.mcp.congrA (fun u hu => ⟨tw4 u hu _ (by simp) (by simp), tw4 u hu _ (by simp) (by simp)⟩) fun u hu x' hx' => by + obtain ⟨y', hy', -, -, -, hm, -, hf⟩ := KN4.tcb _ _ hx' + exact ⟨y', hy', hm, hf⟩ + have ye : ∀ u ∈ Z.T, L.mem s4 (Fl4ProofsA64.P1.yieldA u) = L.mem s1 (Fl4ProofsA64.P1.yieldA u) := fun u hu => by + unfold Fl4ProofsA64.P1.yieldA; exact tw4 u hu _ (by simp) (by simp) + have hY4 : YieldRelA Z.T Z.C m (L.mem s4) σ4 := by + refine ⟨fun u hu => ?_, fun u hu h => ?_, fun c hc' y' k' h p hp => ?_⟩ + · obtain ⟨x', hx', e⟩ := J.yieldRel.tcb u hu + obtain ⟨y', hy', -, -, -, -, hyt, -⟩ := KN4.tcb _ _ hx' + exact ⟨y', hy', by rw [ye u hu, hyt]; exact e⟩ + · rw [ye u hu] at h ⊢ + obtain ⟨hC, y', k', hy', e⟩ := J.yieldRel.toSc u hu h + obtain ⟨y'', k'', hy'', e'⟩ := KN4.scF _ _ _ hy' + exact ⟨hC, y'', k'', hy'', e'.trans e⟩ + · obtain ⟨y0, k0, h0', e⟩ := KN4.scB _ _ _ h + obtain ⟨u, hu, e1, e2⟩ := J.yieldRel.frm c hc' y0 k0 h0' p (e ▸ hp) + exact ⟨u, hu, e1, by rw [ye u hu]; exact e2⟩ + have hgc' : getCap (Z := Unit) (getTcbTimeoutHandlerPtr (specPtr t)) σ4 = + ({(.endpointCap (specPtr ep) hbg.bv (rightsOfA sd rv g gr), σ4)}, False) := getCap_tcb_eqA hx4 hgc + have hsort : PrioSortedA σ4 xe.epQueue := + (J.ipcSorted.of_qs MD.qs).1 (specPtr ep) ⟨ep, he, rfl⟩ xe hk + have hqT : ∀ a ∈ xe.epQueue, ∃ y, σ4.kheap a = some (.tcb y) := by + intro a ha + obtain ⟨xs, hQ, hxs, -⟩ := (MD.ep.at_ep he hk).queue + rw [hxs] at ha + obtain ⟨u, hu, rfl⟩ := List.mem_map.1 ha + obtain ⟨y, hy, -⟩ := MD.ep.tcb u (hQ.mem u hu) + exact ⟨y, hy⟩ + refine WP.spec_mono (handle_timeout_blockXA (st := st) (ar := ar) (N := N) (Z := { Z with Q := Q, R := R' }) a + J.ipcPlace J.iplaced.e MD.ep hmax hconv HB hM4 hY4 ht hx4 hRun hslot hcap hgc' he hk hc hsort badge) ?_ + rintro ⟨s5, a5⟩ ⟨ha5, x1, σf, τ, ex1, eσf, hfl, B, ⟨Q5, R5, HQ⟩, HE5, w45, sz45⟩ + refine ⟨τ, by rw [hfl]; exact Set.mem_singleton _, ?_⟩ + have hx1sc : x1.tcbSchedContext = x.tcbSchedContext := by rw [ex1] + have hept : specPtr ep ≠ specPtr t := fun e => by rw [e, hx4] at hk; cases hk + have far1 : ∀ p, p ≠ specPtr t → σf.kheap p = σ4.kheap p := fun p hp => by rw [eσf]; simp [updK, hp] + have hkt1 : σf.kheap (specPtr t) = some (.tcb x1) := by rw [eσf]; simp [updK] + have hk1 : σf.kheap (specPtr ep) = some (.endpoint xe) := by rw [far1 _ hept]; exact hk + have K2 : SIKA σ1.idleThread (· = specPtr t) (fun _ => False) true σf τ := B.sika hkt1 hk1 rfl + have K2f : SIKA σ1.idleThread (· = specPtr t) (fun _ => False) false σf τ := B.sika hkt1 hk1 rfl + have KA : SIKXA σ1.idleThread (· = specPtr t) (fun _ => False) true σ1 σ4 := + K04.narrowN (N' := fun _ => False) (fun y' e d' p => e ▸ MD.nq d' p) (fun y' e h => (MD.nsw _ h).2 (e ▸ rfl)) + have KF : SIKXA σ1.idleThread (· = specPtr t) (fun _ => False) true σ4 σf := by + rw [eσf, ex1]; exact SIKXA.updFault hx4 _ + have K15 : SIKXA σ1.idleThread (· = specPtr t) (fun _ => False) true σ1 τ := (KA.transX KF).transS K2 + have Qf : QKeepA σ4 σf := by + rw [eσf, ex1] + exact (QKeepA.upd hx4 (QObjA.ofTcb (y := { x with tcbFault := some (.timeout badge.bv) }) rfl)).trans + (QKeepA.of_kheap rfl ⟨rfl, rfl, rfl, rfl⟩) + have Qs : QBlkA (specPtr '' E) (specPtr '' N) σ1 τ := by + have Q01 : QBlkA (specPtr '' E) (specPtr '' N) σ1 σf := QSendA.toBlk J.ipcSorted (MD.qs.trans Qf.toSend) + have hq1 : ∀ a ∈ xe.epQueue, ∃ y, σf.kheap a = some (.tcb y) := fun a ha => by + by_cases e : a = specPtr t + · exact ⟨x1, by rw [e]; exact hkt1⟩ + · obtain ⟨y, hy⟩ := hqT a ha + exact ⟨y, by rw [far1 a e]; exact hy⟩ + have Q13 := B.qblk Q01.sorted hkt1 hk1 hq1 + exact ⟨Q13.sorted, fun h => Q13.act (Q01.act h), fun p y h => by + obtain ⟨x2, hx2, f⟩ := Q13.rt p y h + obtain ⟨x0, hx0, f0⟩ := Q01.rt p x2 hx2 + exact ⟨x0, hx0, fun h => f (f0 h)⟩, Q01.dom.trans Q13.dom⟩ + have SV1 : SchedValidA σf := by + rw [eσf, ex1]; exact SchedValidA.faultA (MD.fia.1.toA MD.fia.2) hx4 _ + have SV5 : SchedValidA τ := B.schedValid SV1 hkt1 hk1 (fun sc' hs' => by + obtain ⟨z, c, hz, hzc, ha⟩ := MD.bnd x _ hx4 (hx1sc ▸ hs') + rw [hx4] at hz; cases hz + rw [← hx1sc, hs'] at hzc; cases hzc + obtain ⟨y', k', hy', act⟩ := ha + exact ⟨y', k', by rw [far1 _ (fun e => by rw [e, hx4] at hy'; cases hy')]; exact hy', act⟩) + have fault5 : ∀ p, p ≠ specPtr t → ∀ x', σ1.kheap p = some (.tcb x') → ∃ y', τ.kheap p = some (.tcb y') ∧ + y'.tcbFault = x'.tcbFault := fun p hp x' hx' => by + obtain ⟨y4, hy4, g4⟩ := MD.keep.tcbF (show σ0.kheap p = some (.tcb x') by rw [hσ0f p hp]; exact hx') + have hf : σf.kheap p = some (.tcb y4) := by rw [far1 p hp]; exact hy4 + have hpe : p ≠ specPtr ep := fun e => by rw [e, hk1] at hf; cases hf + exact ⟨y4, by rw [B.far p hp hpe]; exact hf, g4.fault⟩ + -- the write sets from the pre-state + have w45' : WritesIn st s4 s5 (fun b => IpcFpA st Z E Rp Bf m σ1 s1 b ∨ FaultWA t b) := by + refine w45.mono fun b h => ?_ + rcases h with h | h + · exact Or.inr h + refine Or.inl ?_ + have hta := J.iplaced.e.tcb t ht + have tw : ∀ (k : Nat), b.val = t.val + k → TWOffN k → IpcFpA st Z E Rp Bf m σ1 s1 b := fun k hv hk' => + Or.inl (Or.inr (Or.inr (Or.inl ⟨t, ht, Or.inl (twf_ofA k hv hk')⟩))) + rcases h with c | c | c | c | c | ⟨v, hv, c⟩ | c + · exact tw 3328 (by rw [c]; exact Endp.val_st hta) (by unfold TWOffN; omega) + · exact tw 3336 (by rw [c]; exact Endp.val_blk hta) (by unfold TWOffN; omega) + · exact tw 3344 (by rw [c]; exact Endp.val_badge hta) (by unfold TWOffN; omega) + · exact tw 3352 (by rw [c]; exact Endp.val_flags hta) (by unfold TWOffN; omega) + · exact Or.inl (Or.inl ⟨ep, he, c⟩) + · have hvT : v ∈ Z.T := by + rcases hv with rfl | hv + · exact ht + · exact hv.1 + exact Or.inl (Or.inr (Or.inr (Or.inl ⟨v, hvT, Or.inr c⟩))) + · exact Or.inr (mwr_back (Z := Z) (Z' := { Z with Q := Q, R := R' }) rfl rfl MD.sz kb4 c) + have wAll : WritesIn st s1 s5 (fun b => (IpcFpA st Z E Rp Bf m σ1 s1 b ∨ RepWA Rp b) ∨ FaultWA t b) := + (wI.trans w45').mono fun b h => h.elim Or.inl fun h => h.elim (fun h => Or.inl (Or.inl h)) Or.inr + have wN : WritesIn st s1 s5 (fun b => SchedStepNWA (st := st) N E Rp Z m σ1 s1 b ∨ BufW Bf b) := by + refine (MD.w.trans w45').mono fun b h => ?_ + rcases h with h | h | h + · exact replyFootW_wA J.iplaced.e h + · rcases h with h | h + · exact ewrite_stepNA_buf J.sr.core.tcbs h + · exact Or.inl (Or.inl (Or.inl h)) + · exact Or.inl (Or.inl (Or.inr (faultTcbStepA J.sr.core.tcbs ht h))) + -- user memory and the spec frame + obtain ⟨z0, hz0, U14⟩ := MD.ufr + have Uf : UFrA (fun _ => False) σ4 σf := by + rw [eσf, ex1] + exact UFrA.of_fr (SchedFrameA.upd_tcb (x' := { x with tcbFault := some (.timeout badge.bv) }) hx4 ⟨rfl, rfl, rfl⟩) + have HS : SpecSchedA σ1 τ := (U14.1.trans Uf.1).trans (B.specSched hkt1 hk1) + have SSz : SSzA σ1 τ := U14.2.1.trans sz45 + have hz : ∀ w : Word, UtFreeA st OS s1 (fun d => d = false) (fl4Ptr w).val → + τ.machineState.underlyingMemory w = σ1.machineState.underlyingMemory w := by + intro w hw + have a1 := J.specZero_of_recvBufA ht hz0 (σ' := σ4) (fun v hv => U14.2.2 v hv) w hw + rw [B.machine, Uf.2.2 w id, a1] + -- the reply stacks + have link45 : ∀ q' ∈ Rp, L.mem s5 (rNextA q') = L.mem s4 (rNextA q') ∧ + L.mem s5 (rPrevA q') = L.mem s4 (rPrevA q') := by + intro q' hq' + have f : ∀ o : U64, 8 ≤ o.val → o.val < 32 → o.val % 8 = 0 → L.mem s5 (wa q' o) = L.mem s4 (wa q' o) := + fun o h1 h2 h3 => by + obtain ⟨al, nw⟩ := J.replyLinks_offA ht hq' o h1 h2 h3 + exact w45'.mem_eq al nw + exact ⟨f _ (by simp [layout.reply.NEXT]) (by simp [layout.reply.NEXT]) (by simp [layout.reply.NEXT]), + f _ (by simp [layout.reply.PREV]) (by simp [layout.reply.PREV]) (by simp [layout.reply.PREV])⟩ + have RS5 : ReplyStackRelA (L.mem s5) Rp Z.C τ (scNameA m) stack' := by + refine MD.rs.frame link45 (fun c _ x' k' h => ?_) (fun r' _ y' h => ?_) + · have hne : scNameA m c ≠ specPtr t := fun e => by rw [e, hx4] at h; cases h + obtain ⟨y', hy', g'⟩ := K2f.scF (show σf.kheap (scNameA m c) = some (.schedContext x' k') by + rw [far1 _ hne]; exact h) + exact ⟨y', k', hy', g'.reps rfl⟩ + · obtain ⟨x', hx', e⟩ := K2f.replyB h + have hne : specPtr r' ≠ specPtr t := fun e' => by rw [e', hkt1] at hx'; cases hx' + exact ⟨x', by rw [far1 _ hne] at hx'; exact hx', e⟩ + have sub : ∀ c ∈ Z.C, ∀ q' ∈ stack' c, q' ∈ stack c := by + intro c hc' q' hq' + obtain ⟨⟨y4, n4, hy4, l4⟩, -⟩ := MD.rs.stackRel c hc' + obtain ⟨⟨y0, n0, hy0, l0⟩, -⟩ := J.reply.stackRel c hc' + obtain ⟨y', k', hy', sb⟩ := MD.rsub _ _ _ hy4 + rw [hy0] at hy' + cases hy' + have := sb (specPtr q') (by rw [l4]; exact List.mem_map_of_mem hq') + rw [l0] at this + obtain ⟨q'', hq'', e⟩ := List.mem_map.1 this + rwa [specPtr_inj e] at hq'' + -- the mover + have notM : ∀ c < n, ¬ (m.idleThreadOf c = specPtr t) := by + intro c hc' h + obtain ⟨y1, hy1, hs1⟩ := J.sidle.f3 c hc' + rw [h, hkt0] at hy1; cases hy1; rw [hs1] at hst0; cases hst0 + have hMq : ∀ y', y' = specPtr t → ∀ dm pr, y' ∉ σ1.readyQueues dm pr := by + intro y' hy' dm pr hm + obtain ⟨z, hz', hr⟩ := J.sinv.rq dm pr _ hm + rw [hy', hkt0] at hz'; cases hz'; rw [hst0] at hr; cases hr + have sinv : SInvXA τ m n := + K15.sinv J.sinv notM (fun h => hnI h.symm) hMq (fun _ h => h.elim) (fun _ h => h.elim) hsw + (fun y hy hr => by + obtain ⟨z, hz, hrn⟩ := J.sinv.rlsRun _ hr + rw [show y = specPtr t from hy, hkt0] at hz; cases hz; rw [hst0] at hrn; cases hrn) + (Or.inl (by + have e1 : τ.consumedTime = σf.consumedTime := by rw [K2.frame] + have e2 : σf.consumedTime = σ4.consumedTime := by rw [eσf]; rfl + have e3 : σ4.consumedTime = σ0.consumedTime := by rw [MD.keep.frame] + have e4 : σ0.consumedTime = σ1.consumedTime := by + rcases MD.clr with h | ⟨y, -, h⟩ + · rw [h] + · rw [h]; rfl + rw [e1, e2, e3, e4])) + (J.sidle.rdyA J.sinv) + (K15.faultStA J.sinv.faultSt + (fun p hp x y hx hy => by + obtain ⟨y', hy', e⟩ := fault5 p hp x hx + rw [hy] at hy'; cases hy'; exact e) + (fun p hp y hy hf => by + rw [show p = specPtr t from hp, B.atT] at hy; cases hy + exact Or.inl ⟨_, _, rfl⟩)) + have idle : IdleNoFaultA m τ := fun c hc' => by + obtain ⟨x', hx', hf⟩ := J.idleNoFault c hc' + obtain ⟨y', hy', e⟩ := fault5 _ (fun e => hidle c hc' e.symm) x' hx' + exact ⟨y', hy', e.trans hf⟩ + have hMs : ∀ u ∈ Z.T, specPtr u = specPtr t → ∀ x', σ1.kheap (specPtr u) = some (.tcb x') → + ∀ p, x'.tcbState ≠ .blockedOnNotification p := by + intro u _ h x' hx' p hp + rw [h, hkt0] at hx'; cases hx'; rw [hst0] at hp; cases hp + have hMs' : ∀ u ∈ Z.T, specPtr u = specPtr t → ∀ x', τ.kheap (specPtr u) = some (.tcb x') → + ∀ p, x'.tcbState ≠ .blockedOnNotification p := by + intro u _ h x' hx' p hp + rw [h, B.atT] at hx' + cases hx' + cases hp + have hMv : ∀ u ∈ Z.T, specPtr u = specPtr t → L.mem s1 (Fl4ProofsA64.P1.stA u) ≠ sched.TS_RUNNING_VM := by + intro u _ e h' + rw [specPtr_inj e] at h' + have h5 : (L.mem s1 (stA t)).val = 5 := by rw [(J.ep.at_tcb ht hkt0).st, hst0]; rfl + rw [h'] at h5; revert h5; unfold sched.TS_RUNNING_VM; simp + have wk45 : NtfnWaitKeepMA (fun o => o = specPtr t) Z.T N (L.mem s4) (L.mem s5) := + J.ntfnWait_blkGA (Z0 := { Z with Q := Q, R := R' }) (s0 := s4) (σ0 := σ4) ht he + (fun b h => mwr_back (Z := Z) (Z' := { Z with Q := Q, R := R' }) rfl rfl MD.sz kb4 h) + (w45.mono fun b h => h.symm) + have wk15 : NtfnWaitKeepMA (fun o => o = specPtr t) Z.T N (L.mem s1) (L.mem s5) := + ntfnWaitMA_compMA wk wk45 (fun _ h => h) (fun _ h => h) + have hrepE : Sched.RepEndA σ1 τ := by + have act0 : σ0.schedulerAction = σ1.schedulerAction := by + rcases MD.clr with h | ⟨z, -, h⟩ <;> rw [h] <;> rfl + have ns4 : NoSwA σ4 := Sched.noSw_midA MD.keep act0 hsw (fun u e => (MD.nsw u e).2) + have nsf : NoSwA σf := fun u e => ns4 u (by rw [eσf] at e; exact e) + have B1 : Sched.BLA σ4 σf := by rw [eσf]; exact Sched.BLA.tcbW hx4 + have B2 : Sched.BLA σf τ := by + refine ⟨by rw [B.frame], by rw [B.frame], by rw [B.frame], by rw [B.frame], by rw [B.frame], + by rw [B.machine], fun h => ?_, fun y' k h => ?_⟩ + · rcases B.act with e | e + · rw [e] at h; exact h + · rw [e] at h; cases h + · rcases B.cases h with ⟨-, c⟩ | ⟨-, c⟩ | ⟨-, -, c⟩ + · cases c + · cases c + · exact ⟨y', c, rfl, rfl⟩ + refine ⟨PreO.trans MD.bl (PreO.trans B1 B2), fun u c => ?_, fun y' hy' => ?_⟩ + · rcases B.act with e | e + · rw [e] at c; exact absurd c (nsf u) + · rw [e] at c; cases c + have ne : σ1.curThread ≠ specPtr t := fun h => hcur h.symm + rcases B.cases hy' with ⟨h, -⟩ | ⟨-, c⟩ | ⟨-, -, hz⟩ + · exact absurd h ne + · cases c + have hz4 : σ4.kheap σ1.curThread = some (.tcb y') := by rw [← far1 _ ne]; exact hz + exact Sched.curSt_midA MD.keep (hσ0f _ ne) ne y' hz4 + refine ⟨⟨ha5, Q5, R5, stack', fun bxi sysl symL userOK j7 => ?_⟩, hrepE⟩ + exact J.of_ipcStepUXSA (Q := Q5) (Rl := R5) (curOf := Z.curOf) (curScOf := Z.curScOf) wN HS SSz Qs hz + (fun _ _ => rfl) bxi sysl symL userOK j7 HQ.sched HE5 + (J.ntfn_ipcRFA wAll ht K15 HE5 wk15 hMs hMs') RS5 + (fun q' hq' dead c hc' hm => J.deadExt.reply q' hq' dead c hc' (sub c hc' q' hm)) + (Fl4ProofsA64.P3.SIKXA.ipcKeepN K15) idle + (J.tw_ipcRFA wAll ht) sinv (K15.sidle J.sidle notM (J.sidle.rdyA J.sinv)) (J.runVcpu_ipcRFA wAll ht K15 HE5 hMv) + (J.blkQ_blkXA ht he K15 ⟨x1, _, B.atT⟩ ⟨_, B.atE, mem_specInsA.2 (Or.inl rfl)⟩ HE5 wk15) SV5 + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3ReplyTmoDispA.lean b/proofs/Fl4ProofsA64/P3/Stage3ReplyTmoDispA.lean new file mode 100644 index 000000000..c521c16a0 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ReplyTmoDispA.lean @@ -0,0 +1,202 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2b): **the nested timeout send of a Reply, either endpoint case**, shared by the live +and the faulted Reply arm. + +* `validTimeoutHandler_relA`: `valid_timeout_handler` reads `isEpCap` of the related spec cap. +* `JStage3A.replyTmoRtA`: the reply objects' thread words are kept but the caller's (from the Reply's + narrow write set, live / faulted). +* **`JStage3A.replyTmoA`**: the caller's timeout handler cap is read at the mid state and the endpoint case picked + (`replyTmoRecvA`: a waiting receiver; `replyTmoBlkA`: idle or waiting senders). + +Named premises: `replyTmoA`'s `hte` (the caller's handler names an endpoint of `E`: `JStage3A.tmoHandlerEpA` at its callers); +[P] `hmax`, `htu`, `hconv`, `hCs`, `one`. New module. +-/ +import Fl4ProofsA64.P3.Stage3ReplyTmoBlkA +import Fl4ProofsA64.P3.Stage3ReplyLiveKeepFA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- `valid_timeout_handler` reads whether the related spec cap is an endpoint cap. -/ +theorem validTimeoutHandler_relA {s : S} {t : U64} {h : Fl4SpecA64.Cap} + (rel : CapRelA (capA st s (slotIdx t layout.tcb.SLOT_TIMEOUT_HANDLER)) h) : + faults.valid_timeout_handler st s t = ok (isEpCap h) := by + unfold faults.valid_timeout_handler + rw [tcb_slot_val, bind_ok, capA_eq, bind_ok] + have e := rel.2 + generalize capA st s (slotIdx t layout.tcb.SLOT_TIMEOUT_HANDLER) = c at e ⊢ + cases c with + | Endpoint => simp only [CapEqA, absCapA] at e; subst e; rfl + | PageTable q mm asid va => cases h <;> first | rfl | (cases mm <;> simp [CapEqA, absCapA] at e) + | _ => cases h <;> first | rfl | (simp [CapEqA, absCapA] at e) + +end + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {J5 : JSlotA S} {R : Set U64 → JSlotA S} {s1 s' : S} {a : A} {σ1 : State Unit} {m : McState} + +/-- **The other reply objects' thread words across the live Reply** (module docstring). -/ +theorem JStage3A.replyTmoRtA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7A st R) s1 a σ1 m) + {r t : U64} (hr : r ∈ Rp) (ht : t ∈ Z.T) (hrw : L.mem s1 (Endp.rTcbA r) = t) + (w : WritesIn st s1 s' (ReplyNwFA st Z m σ1 s1 Rp r t Bf)) : + ∀ q ∈ Rp, L.mem s' (Endp.rTcbA q) ≠ L.mem s1 (Endp.rTcbA q) → L.mem s1 (Endp.rTcbA q) = t := by + intro q hq hne + by_cases eq : q = r + · rw [eq]; exact hrw + refine absurd ?_ hne + have hP := J.iplaced + have hrH : ∀ q ∈ Rp, H q = some .reply := fun q hq => J.utIpc.reply q hq + obtain ⟨q32, -, qfit⟩ := hP.e.rep q hq + have hv : (Endp.rTcbA q).val = q.val := Endp.rTcbA_val q + have other : ∀ q' ∈ Rp, q' ≠ q → q'.val + 32 ≤ q.val ∨ q.val + 32 ≤ q'.val := fun q' hq' ne => by + have := J.sys.ent.heap.disj q' _ q _ (hrH q' hq') (hrH q hq) ne + simp only [HKind.bits] at this + omega + refine w.mem_eq (by unfold Aligned; rw [hv]; omega) ?_ + have hw : IpcObjWA N E Rp (Endp.rTcbA q) := Or.inr (Or.inr ⟨q, hq, by omega, by omega⟩) + rintro (((c | c | c | c) | c | c) | c) + · obtain ⟨q', hq', c⟩ := c + have h2 := replyNextPlaceA (Z := Z) hP.e hq' + have h3 := replyPrevPlaceA (Z := Z) hP.e hq' + by_cases e : q' = q + · subst e + rcases c with c | c <;> rw [c] at hv <;> omega + · have := other q' hq' e + rcases c with c | c <;> rw [c] at hv <;> omega + · have h2 : (Endp.rTcbA q).val = r.val := by rw [c, Endp.rTcbA_val] + exact eq (UScalar.eq_of_val_eq (by omega)) + · have h1 := hP.e.rt q hq t ht + have h2 : (Endp.rTcbA q).val = t.val + 3328 := by rw [c]; exact Endp.val_st (hP.e.tcb t ht) + omega + · exact J.ipc_offMwrA hw c + · obtain ⟨i, c⟩ := c + have h1 := regIdxA_lt i + have h2 := hP.e.rt q hq t ht + rw [c, Endp.regA_val (hP.e.tcb t ht) h1] at hv + omega + · exact hP.e.br _ c q hq |> fun h => by omega + · have h1 := hP.e.rt q hq t ht + have e : faultA t = wa t 3376#u64 := by unfold faultA; rfl + rw [c, e, field_val (hP.e.tcb t ht) (by simp)] at hv + have : (3376#u64 : U64).val = 3376 := by simp + omega + +set_option maxHeartbeats 1000000 in +/-- **The nested timeout send of a Reply** (module docstring). -/ +theorem JStage3A.replyTmoA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s1 a σ1 m) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) (hconv : Fl4TicksToUsLegacyAgree) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (one : fl4A64Platform.numNodes = 1) + (hsw : ∀ u, σ1.schedulerAction ≠ .switchThread u) + (hcurR : ∀ z, σ1.kheap σ1.curThread = some (.tcb z) → runnable z.tcbState = true) + {t : U64} (ht : t ∈ Z.T) (hcur : specPtr t ≠ σ1.curThread) + (hidle : ∀ c < fl4A64Platform.numNodes, specPtr t ≠ m.idleThreadOf c) + {xt0 : Tcb} {rr : ObjRef} (hkt0 : σ1.kheap (specPtr t) = some (.tcb xt0)) + (hst0 : xt0.tcbState = .blockedOnReply rr) + (hte : ∀ p b r, xt0.tcbTimeoutHandler = .endpointCap p b r → ∃ ep ∈ E, p = specPtr ep) + {σ0 : State Unit} {Wn : U64 → Prop} {s4 : S} {σ4 : State Unit} {Q : U64 → U64 → List U64} + {R' : U64 → List U64} {stack' : U64 → List U64} {x : Tcb} + (MD : ReplyTmoMidA st Z m κ E Rp Bf s1 σ1 σ0 t Wn s4 σ4 Q R' stack' x) + (wk : NtfnWaitKeepMA (· = specPtr t) Z.T N (L.mem s1) (L.mem s4)) + (hrt : ∀ q ∈ Rp, L.mem s4 (Endp.rTcbA q) ≠ L.mem s1 (Endp.rTcbA q) → L.mem s1 (Endp.rTcbA q) = t) + {sc : U64} {y : SchedContext} {k : Nat} (badge : U64) (hsc : L.mem s4 (scA t) = sc) (h0 : sc ≠ 0#u64) + (hy : σ4.kheap (scNameA m sc) = some (.schedContext y k)) + (hep : isEpCap x.tcbTimeoutHandler = true) : + faults.handle_timeout st ar s4 a t (.Timeout badge) ⦃ o => ∃ σ', + ((), σ') ∈ (fl4HandleTimeout (Z := Unit) (specPtr t) (.timeout badge.bv) σ4).1 ∧ + ReplyTmoOutA st ar AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf κ n nA m a o.1 o.2 σ' ∧ + Sched.RepEndA σ1 σ' ⦄ := by + have K04 : SIKXA σ1.idleThread (· = specPtr t) (· = specPtr t) true σ1 σ4 := SIKXA.ofClr MD.keep MD.clr + have one4 : ∀ z : Tcb, tcbCnodeMap z (tcbCnodeIndex 4) = some z.tcbTimeoutHandler := fun z => by + simp [tcbCnodeMap, tcbIdx_eq, tcbStage2Slot, tcbHighVSpaceSlot] + have eto : x.tcbTimeoutHandler = xt0.tcbTimeoutHandler := by + obtain ⟨z, hz, g⟩ := K04.tcbF hkt0 + rw [MD.kx] at hz + cases hz + exact Option.some.inj ((one4 x).symm.trans ((congrFun g.caps (tcbCnodeIndex 4)).trans (one4 xt0))) + -- the handler cap's slot, kept by the Reply + have hdth : Z.th t = some .tcb := (J.sr.thT t).2 ht + have es : slotIdx t layout.tcb.SLOT_TIMEOUT_HANDLER = slotAddrA (specPtr t) (BitVec.ofNat 64 4) := by + rw [slotIdx_addrA] + unfold layout.tcb.SLOT_TIMEOUT_HANDLER + rfl + have hsl : IsSlotA Z.th (slotIdx t layout.tcb.SLOT_TIMEOUT_HANDLER) := + ⟨t, Or.inr ⟨hdth, 4, by unfold tcbSlotsA; omega, es⟩⟩ + have keep := slotWordsKeepA J.sr.toSchedSRN J.iplaced.e J.ipcPlace J.slotOffEwA MD.w + (Fl4ProofsA64.P3.Sched.IrqSlotsKeepA.of_replyFoot J.sr.toSchedSRN J.iplaced.e J.ipcPlace MD.w) _ hsl + have ecap : capA st s4 (slotIdx t layout.tcb.SLOT_TIMEOUT_HANDLER) = + capAtA (L.mem s1) (slotIdx t layout.tcb.SLOT_TIMEOUT_HANDLER) := by + show capAtA (L.mem s4) _ = _ + unfold capAtA + rw [keep.1, keep.2] + have rel : CapRelA (capAtA (L.mem s1) (slotIdx t layout.tcb.SLOT_TIMEOUT_HANDLER)) xt0.tcbTimeoutHandler := by + obtain ⟨z, hz, h⟩ := J.j.1.st.tcb t hdth + rw [hkt0] at hz + cases hz + obtain ⟨ac, hac, h⟩ := h 4 (by unfold tcbSlotsA; omega) + rw [one4] at hac + cases hac + rw [es] + exact h + obtain ⟨p, b, rights, hh⟩ : ∃ p b rights, xt0.tcbTimeoutHandler = .endpointCap p b rights := by + rw [eto] at hep + cases h : xt0.tcbTimeoutHandler <;> rw [h] at hep <;> simp [isEpCap] at hep + exact ⟨_, _, _, rfl⟩ + obtain ⟨ep, he, hpe⟩ := hte _ _ _ hh + obtain ⟨ep', hbg, sd, rv, g, gr, hc, ha⟩ : ∃ ep' hbg sd rv g gr, + capAtA (L.mem s1) (slotIdx t layout.tcb.SLOT_TIMEOUT_HANDLER) = .Endpoint ep' hbg sd rv g gr ∧ + xt0.tcbTimeoutHandler = .endpointCap (specPtr ep') hbg.bv (rightsOfA sd rv g gr) := by + have e := rel.2 + rw [hh] at e ⊢ + generalize capAtA (L.mem s1) (slotIdx t layout.tcb.SLOT_TIMEOUT_HANDLER) = c at e ⊢ + cases c with + | Endpoint ep' hbg sd rv g gr => + refine ⟨ep', hbg, sd, rv, g, gr, rfl, ?_⟩ + simp only [CapEqA, absCapA] at e + exact e.symm + | PageTable q mm asid va => cases mm <;> simp [CapEqA, absCapA] at e + | _ => simp [CapEqA, absCapA] at e + have eep : ep' = ep := by + rw [hh] at ha + injection ha with h1 _ _ + exact specPtr_inj (h1.symm.trans hpe) + subst ep' + have hslot : obj.Tcb.slot t layout.tcb.SLOT_TIMEOUT_HANDLER = ok (slotIdx t layout.tcb.SLOT_TIMEOUT_HANDLER) := + tcb_slot_val _ _ + have hcap : obj.Slot.cap st (slotIdx t layout.tcb.SLOT_TIMEOUT_HANDLER) s4 = ok (.Endpoint ep hbg sd rv g gr) := by + rw [capA_eq, ecap, hc] + have hgc : tcbCnodeMap x (tcbCnodeIndex 4) = some (.endpointCap (specPtr ep) hbg.bv (rightsOfA sd rv g gr)) := by + rw [one4, eto, ha] + obtain ⟨xe, hk, rel_e⟩ := MD.ep.ep ep he + cases xe with + | idleEP => + exact J.replyTmoBlkA hmax hconv hsw ht hcur hidle hkt0 hst0 MD wk badge hslot hcap hgc he hk trivial + | sendEP q => + exact J.replyTmoBlkA hmax hconv hsw ht hcur hidle hkt0 hst0 MD wk badge hslot hcap hgc he hk rfl + | recvEP q => + obtain ⟨-, xs, hne, rfl, -, -⟩ := rel_e + cases xs with + | nil => exact absurd rfl hne + | cons d ds => + exact J.replyTmoRecvA hmax htu hconv hCs one hsw hcurR ht hcur hidle hkt0 hst0 MD wk hrt badge hsc h0 hy + hslot hcap hgc he hk + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3ReplyTmoFootA.lean b/proofs/Fl4ProofsA64/P3/Stage3ReplyTmoFootA.lean new file mode 100644 index 000000000..49e4a5be4 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ReplyTmoFootA.lean @@ -0,0 +1,244 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2b): **write-set facts for a Reply followed by the nested timeout send**: the +Reply's words (`IpcFpA ∨ RepWA`, at the Reply's pre-state) together with the timeout sender's fault words +(`FaultWA`). + +* `JStage3A.tcbKept_ipcRFA`, `tw_ipcRFA`, `runVcpu_ipcRFA`, `ntfn_ipcRFA`: `tcbKept_ipcA` / `tw_ipcA` / + `runVcpu_ipcRXA` / `ntfn_ipcMRXA` with the fault words. +* `JStage3A.replyLinks_offA`: the reply objects' link words are off `IpcFpA ∨ FaultWA`. +* `JStage3A.ntfnWait_sendGA`: `ntfnWait_sendXA` for a send from another state (`s0`), its scheduler words mapped to + the record's (`hmw`). + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3ReplyFaultGlueA +import Fl4ProofsA64.P3.Stage3KEntrySendA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3 Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {A : Type} {AR' : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} + {κ : U64 → U64} {n : Nat} {J5 : JSlotA S} {R : Set U64 → JSlotA S} {s s' : S} {a : A} + {σ σ' : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} {i : Fl4SpecA64.ObjRef} + {M Nq : Fl4SpecA64.ObjRef → Prop} {don : Bool} + +variable (J : JStage3A st AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7A st R) s a σ m) +include J + +theorem _root_.Fl4ProofsA64.P3.JStage3A.tcbKept_ipcRFA {tf : U64} + (w : WritesIn st s s' (fun b => (IpcFpA st Z E Rp Bf m σ s b ∨ RepWA Rp b) ∨ FaultWA tf b)) (htf : tf ∈ Z.T) + {t K : U64} (ht : t ∈ Z.T) + (hK : K.val < 4096) (h8 : K.val % 8 = 0) (hw : ¬ TWOffN K.val) + (h : K.val ≠ 3496 ∧ K.val ≠ 3504 ∧ K.val ≠ 3464 ∧ K.val ≠ 3480 ∧ K.val ≠ 3488 ∧ K.val ≠ 3592) + (hF : K.val ≠ 3376 ∧ K.val ≠ 3384 ∧ K.val ≠ 3392 ∧ K.val ≠ 3400) : + L.mem s' (wa t K) = L.mem s (wa t K) := + w.mem_eq (field_aligned (J.sr.core.tcbs t ht) hK h8) fun c => c.elim + (fun c => c.elim (J.tcbWord_offIpcA ht hK hw h) fun ⟨r, hr, c⟩ => by + have hfit := (J.sr.core.tcbs t ht).fits + have hv : (wa t K).val = t.val + K.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have := J.iplaced.e.rt r hr t ht + omega) + (not_faultWA (J.sr.core.tcbs tf htf) (J.sr.core.tcbs t ht) hK hF) + +theorem _root_.Fl4ProofsA64.P3.JStage3A.tw_ipcRFA {tf : U64} + (w : WritesIn st s s' (fun b => (IpcFpA st Z E Rp Bf m σ s b ∨ RepWA Rp b) ∨ FaultWA tf b)) (htf : tf ∈ Z.T) : + ∀ t ∈ Z.T, ∀ O : U64, O.val < 224 ∨ O.val = 3440 ∨ O.val = 3472 ∨ O.val = 3512 ∨ O.val = 3456 → O.val % 8 = 0 → + L.mem s' (wa t O) = L.mem s (wa t O) := fun t ht O h h8 => + J.tcbKept_ipcRFA w htf ht (by omega) h8 (by unfold TWOffN; omega) (by omega) (by omega) + +theorem _root_.Fl4ProofsA64.P3.JStage3A.runVcpu_ipcRFA {tf : U64} + (w : WritesIn st s s' (fun b => (IpcFpA st Z E Rp Bf m σ s b ∨ RepWA Rp b) ∨ FaultWA tf b)) (htf : tf ∈ Z.T) + (K : SIKXA i M Nq don σ σ') (E' : EpStateRel Z.T E Rp Bf (L.mem s') σ') + (hMv : ∀ t ∈ Z.T, M (specPtr t) → L.mem s (Fl4ProofsA64.P1.stA t) ≠ sched.TS_RUNNING_VM) : + RunVcpuInv st Z.T s' := fun t ht h0 => by + have e : L.mem s' (runVcpuA t) = L.mem s (runVcpuA t) := + J.tcbKept_ipcRFA w htf (K := 3672#u64) ht (by simp) (by simp) (by unfold TWOffN; simp) (by simp) (by simp) + rw [e] at h0 + by_cases hM : M (specPtr t) + · exact absurd (J.runVcpu.zero ht (hMv t ht hM)) h0 + · rw [J.stKept_ipcXA K E' ht hM]; exact J.runVcpu t ht h0 + +theorem _root_.Fl4ProofsA64.P3.JStage3A.ntfn_ipcRFA {tf : U64} (w : WritesIn st s s' (fun b => (IpcFpA st Z E Rp Bf m σ s b ∨ RepWA Rp b) ∨ FaultWA tf b)) + (htf : tf ∈ Z.T) (K : SIKXA i M Nq don σ σ') + (E' : EpStateRel Z.T E Rp Bf (L.mem s') σ') (wk : NtfnWaitKeepMA M Z.T N (L.mem s) (L.mem s')) + (hMs : ∀ t ∈ Z.T, M (specPtr t) → ∀ x, σ.kheap (specPtr t) = some (.tcb x) → + ∀ p, x.tcbState ≠ .blockedOnNotification p) + (hMs' : ∀ t ∈ Z.T, M (specPtr t) → ∀ x, σ'.kheap (specPtr t) = some (.tcb x) → + ∀ p, x.tcbState ≠ .blockedOnNotification p) : + NtfnStateRel N Z.T (L.mem s') σ' := by + have hP := J.iplaced + have nw : ∀ q ∈ N, ∀ O : U64, O.val < 40 → O.val % 8 = 0 → L.mem s' (wa q O) = L.mem s (wa q O) := by + intro q hq O h1 h2 + have hna := hP.n.ntfn q hq + have hv : (wa q O).val = q.val + O.val := Notif.nfield_val hna (by omega) + refine w.mem_eq (Notif.nfield_aligned hna (by omega) h2) ?_ + have hw : IpcObjWA N E Rp (wa q O) := Or.inl ⟨q, hq, by omega, by omega⟩ + rintro (((c | c) | ⟨r, hr, c⟩) | c) + rotate_right + · obtain ⟨K, hK, e⟩ := faultWA_eq c + have hKv : K.val < 4096 ∧ 3376 ≤ K.val := by rcases hK with rfl | rfl | rfl | rfl <;> simp + have hf := field_val (J.sr.core.tcbs tf htf) hKv.1 + have := hP.n.disj q hq tf htf + rw [← e] at hf + omega + · exact nw_not_efoot hP hq (by omega) (by omega) c.foot + · exact J.ipc_offMwrA hw c + · have hqH : H q = some .ntfn := J.utIpc.ntfn q hq + have hrH : H r = some .reply := J.utIpc.reply r hr + have := J.sys.ent.heap.disj q _ r _ hqH hrH (fun e => by rw [e, hrH] at hqH; cases hqH) + have hv : (wa q O).val = q.val + O.val := Notif.nfield_val hna (by omega) + simp only [HKind.bits] at this + omega + refine ⟨fun q hq => ?_, fun t ht => ?_⟩ + · obtain ⟨x, hx, hr⟩ := J.ntfn.ntfn q hq + refine ⟨x, K.ntfnF hx, hr.frame ?_ fun y _ hm => ?_⟩ + · refine ⟨?_, ?_, ?_, ?_, ?_⟩ + · rw [Notif.epHeadA_eq]; exact nw q hq _ (by simp) (by simp) + · rw [Notif.epTSA_eq]; exact nw q hq _ (by simp) (by simp) + · rw [Notif.nBadgeA_eq]; exact nw q hq _ (by simp) (by simp) + · rw [Notif.nBoundA_eq]; exact nw q hq _ (by simp) (by simp) + · rw [Notif.nScA_eq]; exact nw q hq _ (by simp) (by simp) + · obtain ⟨hyT, hy6, hyb⟩ := hr.obj.blocked hm + have hM : ¬ M (specPtr y) := fun c => by + obtain ⟨z, hz, rz⟩ := J.ntfn.tcb y hyT + have h6 := rz.st + rw [hy6] at h6 + cases hs : z.tcbState with + | blockedOnNotification p => exact hMs y hyT c z hz p hs + | ext e => rw [hs] at h6; cases e <;> simp [tsCode] at h6 + | _ => rw [hs] at h6; simp [tsCode] at h6 + obtain ⟨k1, k2, k3⟩ := wk y hyT hM (by rw [hyb]; exact hq) + exact ⟨J.stKept_ipcXA K E' hyT hM, k1, k2, k3⟩ + · obtain ⟨x, hx, r⟩ := J.ntfn.tcb t ht + obtain ⟨y, hy, r'⟩ := E'.tcb t ht + refine ⟨y, hy, ⟨r'.st, fun p hp => ?_, ?_, r'.prio⟩⟩ + · have hM : ¬ M (specPtr t) := fun c => hMs' t ht c y hy p hp + obtain ⟨y', hy', g⟩ := K.tcbF hx + rw [hy] at hy' + cases hy' + have hs : x.tcbState = .blockedOnNotification p := by + rw [← g.st.resolve_right fun c => hM c.1]; exact hp + have hb := r.blk p hs + have hN : L.mem s (Notif.tBlockA t) ∈ N := (J.blkQ.ntfn t ht x hx p hs).1 + rw [(wk t ht hM hN).1]; exact hb + · rw [badgeRegA_eq_regA] + exact r'.regs _ + + +/-- The reply objects' link words are off `IpcFpA ∨ FaultWA`. -/ +theorem _root_.Fl4ProofsA64.P3.JStage3A.replyLinks_offA {tf : U64} (htf : tf ∈ Z.T) {r : U64} (hr : r ∈ Rp) + (o : U64) (h1 : 8 ≤ o.val) (h2 : o.val < 32) (h3 : o.val % 8 = 0) : + Aligned (wa r o) ∧ ¬ (IpcFpA st Z E Rp Bf m σ s (wa r o) ∨ FaultWA tf (wa r o)) := by + have hrH : H r = some .reply := J.utIpc.reply r hr + obtain ⟨r32, -, rfit⟩ := J.iplaced.e.rep r hr + have hv : (wa r o).val = r.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + refine ⟨by unfold Aligned; rw [hv]; omega, ?_⟩ + have hw : IpcObjWA N E Rp (wa r o) := Or.inr (Or.inr ⟨r, hr, by omega, by omega⟩) + have other : ∀ r' ∈ Rp, r' ≠ r → r'.val + 32 ≤ r.val ∨ r.val + 32 ≤ r'.val := fun r' hr' ne => by + have := J.sys.ent.heap.disj r' _ r _ (J.utIpc.reply r' hr') hrH ne + simp only [HKind.bits] at this + omega + rintro ((c | c) | c) + rotate_left + · exact J.ipc_offMwrA hw c + · obtain ⟨K, hK, e⟩ := faultWA_eq c + have hKv : K.val < 4096 ∧ 3376 ≤ K.val := by rcases hK with rfl | rfl | rfl | rfl <;> simp + have hf := field_val (J.sr.core.tcbs tf htf) hKv.1 + have := J.iplaced.e.rt r hr tf htf + rw [← e] at hf + omega + · rcases c with ⟨e, he, c⟩ | ⟨r', hr', c⟩ | ⟨t, ht, c⟩ | c + · have := J.iplaced.e.er e he r hr; omega + · by_cases er : r' = r + · subst er; omega + · have := other r' hr' er; omega + · have := J.iplaced.e.rt r hr t ht + unfold TWF TL at c + rcases c with ⟨c1, c2⟩ | c + · unfold TWOffN at c2; omega + · omega + · have := J.iplaced.e.br _ c r hr; omega + + +theorem _root_.Fl4ProofsA64.P3.JStage3A.ntfnWait_sendGA {W : Prop} {Z0 : XSetsA} {s0 : S} {σ0 : State Unit} {t e d tf : U64} (ht : t ∈ Z.T) (htf : tf ∈ Z.T) (he : e ∈ E) (hd : d ∈ Z.T) + (hdb : L.mem s0 (Notif.tBlockA d) = e) + (hmw : ∀ b, MWrA (st := st) Z0 m σ0 s0 b → MWrA (st := st) Z m σ s b) + (w : WritesIn st s0 s' (fun b => (SendFpA st Z.T Rp Bf (fun σ s => MWrA (st := st) Z0 m σ s) σ0 s0 t e d W b ∨ + (∃ r ∈ Rp, r.val ≤ b.val ∧ b.val < r.val + 32) ∨ ∃ u ∈ Z.T, b = tReplyA u) ∨ FaultWA tf b)) : + NtfnWaitKeepA Z.T N (L.mem s0) (L.mem s') := by + intro y hy hN + have hP := J.iplaced + have hya := hP.e.tcb y hy + have hfy := hya.fits + have notE : ∀ u ∈ Z.T, L.mem s0 (Notif.tBlockA u) = e → u ≠ y := fun u _ hu c => by + subst c + rw [hu] at hN + have := hP.ne e hN e he + omega + have key : ∀ (K : U64) (b : U64), (K.val = 3336 ∨ K.val = 3496 ∨ K.val = 3504) → b = wa y K → Aligned b → + L.mem s' b = L.mem s0 b := by + intro K b hK hb hal + have hbv : b.val = y.val + K.val := by rw [hb, wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have apart : ∀ u ∈ Z.T, u ≠ y → u.val + 4096 ≤ y.val ∨ y.val + 4096 ≤ u.val := fun u hu ne => by + have := J.sr.place.apart u y .tcb .tcb ((J.sr.thT u).2 hu) ((J.sr.thT y).2 hy) ne + have hsz : ObjKindA.size .tcb = 4096 := rfl + omega + refine w.mem_eq hal ?_ + rintro (((c | ⟨r, hr, c⟩ | ⟨-, c⟩ | c | c) | ⟨r, hr, c⟩ | ⟨u, hu, c⟩) | c) + · rcases c with c | ⟨u, hu, hub, c⟩ | ⟨r, c⟩ | c + · have := hP.e.et e he y hy; unfold EW at c; omega + · have := apart u hu (notE u hu hub) + have := (hP.e.tcb u hu).fits + unfold TL at c; omega + · have hfd := (hP.e.tcb d hd).fits + have h1 := regIdxA_lt r + rw [Endp.regA_val (hP.e.tcb d hd) h1] at c + have := apart d hd (notE d hd hdb) + omega + · have := hP.e.bt _ c y hy; omega + · have h1 := hP.e.rt r hr y hy + have h2 : b.val = r.val := by rw [c, Endp.rTcbA_val] + omega + · have h1 : b.val = t.val + 3328 := by rw [c]; exact Endp.val_st (hP.e.tcb t ht) + by_cases ety : t = y + · subst ety; omega + · have := apart t ht ety; omega + · have h1 : b.val = d.val + 3328 := by rw [c]; exact Endp.val_st (hP.e.tcb d hd) + have := apart d hd (notE d hd hdb) + omega + · rw [hb] at c + exact J.tcbWord_offMwrA hy (by omega) (by omega) (hmw _ c) + · have := hP.e.rt r hr y hy + omega + · have h1 : b.val = u.val + 3360 := by rw [c]; exact Endp.val_reply (hP.e.tcb u hu) + by_cases euy : u = y + · subst euy; omega + · have := apart u hu euy; omega + · obtain ⟨K', hK', e'⟩ := faultWA_eq c + have hK'v : K'.val < 4096 ∧ 3376 ≤ K'.val ∧ K'.val ≤ 3400 := by + rcases hK' with rfl | rfl | rfl | rfl <;> simp + have hf := field_val (hP.e.tcb tf htf) hK'v.1 + rw [← e'] at hf + by_cases ety : tf = y + · subst ety; omega + · have := apart tf htf ety; omega + refine ⟨?_, ?_, ?_⟩ + · exact key 3336#u64 _ (by simp) (by unfold Notif.tBlockA; rfl) (Endp.a_blk hya).1 + · exact key 3504#u64 _ (by simp) (by rw [prevEp_eq]) (Endp.a_prev hya).1 + · exact key 3496#u64 _ (by simp) (by rw [nextEp_eq]) (Endp.a_next hya).1 + + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3ReplyTmoMidA.lean b/proofs/Fl4ProofsA64/P3/Stage3ReplyTmoMidA.lean new file mode 100644 index 000000000..747c3ee53 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ReplyTmoMidA.lean @@ -0,0 +1,197 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2b): **the mid-Reply state where the SC tail takes the timeout send**, shared by +the live and the faulted Reply. + +* **`ReplyTmoMidA`**: the facts at the state reached after the reply removal, the transfer (or the fault reply) and + the state write (`s4` / `σ4`), from the Reply's pre-state (`s1` / `σ1`): the caller `t` runnable and unqueued, its + SC tail's relation `SchedSRB … t`, the endpoint and reply-stack relations, the write sets, the donating `SIKA` + (from `σ0`: `σ1`, or `σ1` with `t`'s fault cleared), `FIA`, `BoundActive`, `RqDomPrio`, `QSendA`, `RepSubA` and the + user-memory frame (`t`'s IPC buffer). `do_reply_transfer_keepTA` / `_keepFTA` hand it to the timeout continuation. +* `replyLive_midXA`, `replyFault_midXA`: the spec facts (and, live, no pending switch to the caller) of the bundle not given by `replyLive_midA` / + `replyFault_midA`. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3ReplyFaultMidA +import Fl4ProofsA64.P3.Stage3ReplyRemQA +import Fl4ProofsA64.P3.Stage3ReplyWakeA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4SpecA64 + +noncomputable section + +section +variable {S : Type} (st : machine.Store S) [L : LawfulStore st] + +/-- **The mid-Reply state of the timeout send** (module docstring). -/ +structure ReplyTmoMidA (Z : XSetsA) (m : McState) (κ : U64 → U64) (E Rp : Set U64) (Bf : Set (U64 × Word)) + (s1 : S) (σ1 σ0 : State Unit) (t : U64) (Wn : U64 → Prop) (s4 : S) (σ4 : State Unit) + (Q : U64 → U64 → List U64) (R' : U64 → List U64) (stack' : U64 → List U64) (x : Tcb) : Prop where + sr : SchedSRB st { Z with Q := Q, R := R' } m κ s4 σ4 (specPtr t) + stq : StEqA Z.T (L.mem s4) σ4 + kx : σ4.kheap (specPtr t) = some (.tcb x) + run : runnable x.tcbState = true + nq : ∀ d p, specPtr t ∉ σ4.readyQueues d p + nrel : specPtr t ∉ σ4.releaseQueue + ep : EpStateRel Z.T E Rp Bf (L.mem s4) σ4 + rs : ReplyStackRelA (L.mem s4) Rp Z.C σ4 (scNameA m) stack' + w : WritesIn st s1 s4 (ReplyFootW (st := st) Z m σ1 s1 E Rp Bf) + wn : WritesIn st s1 s4 Wn + sz : SSzA σ1 σ4 + clr : σ0 = σ1 ∨ ∃ y, σ1.kheap (specPtr t) = some (.tcb y) ∧ σ0 = faultClrA σ1 (specPtr t) y + keep : SIKA σ1.idleThread (· = specPtr t) (· = specPtr t) true σ0 σ4 + fia : FIA σ4 + bnd : BoundActive (specPtr t) σ4 + dom : RqDomPrio σ4 + qs : QSendA σ1 σ4 + rsub : RepSubA σ1 σ4 + nsw : SwN (specPtr t) σ4 + /-- The budget frame from the Reply's pre-state. -/ + bl : BLA σ1 σ4 + ufr : ∃ z, σ1.kheap (specPtr t) = some (.tcb z) ∧ UFrA (BufBytesA (ipcBufOfA true z)) σ1 σ4 + +end + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- `RqDomPrio` across the state write. -/ +theorem rqDomPrio_stsA {σ3 σ4 : State Unit} {t : ObjRef} {ts : ThreadState} (hD3 : RqDomPrio σ3) + (h4 : ((), σ4) ∈ (setThreadState (Z := Unit) t ts σ3).1) : RqDomPrio σ4 := by + intro f d q z hz hm + rw [setThreadState_rqA h4] at hm + obtain ⟨z0, hz0, ed, ep⟩ := setThreadState_domA h4 hz + have := hD3 f d q z0 hz0 hm + exact ⟨this.1.trans ed, this.2.trans ep⟩ + +/-- **The live Reply's bundle facts at the mid state** (module docstring). -/ +theorem replyLive_midXA {th : TypedHeapA} {kb : U64} (hPl : PlaceA th kb) (sf : S) + {σ σ1 σ2 σ3 σ4 : State Unit} {m : McState} {n : Nat} {sender t r : U64} {grant : Bool} + {xt : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) (hst : xt.tcbState = .blockedOnReply (specPtr r)) + (hI : SIdleA σ m n) (hinv : SInvXA σ m n) (hL : LoopInvA σ m n) (hsw : NoSwA σ) + (h1 : ((), σ1) ∈ (replyRemove (Z := Unit) (specPtr t) (specPtr r) σ).1) + (h2 : ((), σ2) ∈ ((getTcbObjRef (Z := Unit) Tcb.tcbSchedContext (specPtr t) >>= + ifSporadicCurScTestRefillUnblockCheck) σ1).1) + (hSt : StateRelA th (L.mem sf) σ2) (hSV : SlotsValidA th (L.mem sf)) (htr : th t = some .tcb) + {xs y2 : Tcb} (hks : σ2.kheap (specPtr sender) = some (.tcb xs)) + (hky : σ2.kheap (specPtr t) = some (.tcb y2)) (hfs : xs.tcbFault = none) + (hcaps : grant = false ∨ ipcBufOfA false xs = none ∨ + unat (dataToMessageInfo (xs.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0) + (hal : ∀ rb, ipcBufOfA true y2 = some rb → (bufAddr rb 0).toNat % 8 = 0) + (h3 : ((), σ3) ∈ (doIpcTransfer (Z := Unit) (specPtr sender) none (0 : Badge) grant (specPtr t) σ2).1) + (hD3 : RqDomPrio σ3) + (h4 : ((), σ4) ∈ (setThreadState (Z := Unit) (specPtr t) .running σ3).1) : + RepSubA σ σ4 ∧ QSendA σ σ4 ∧ RqDomPrio σ4 ∧ UFrA (BufBytesA (ipcBufOfA true y2)) σ σ4 ∧ + SwN (specPtr t) σ4 := by + have hti : specPtr t ≠ σ.idleThread := fun e => by + obtain ⟨x, hx, hs⟩ := hinv.idleTcb + rw [← e, hkt] at hx + cases hx + rw [hst] at hs + cases hs + obtain ⟨-, P1, -⟩ := replyRemove_keepA (i := σ.idleThread) (t := specPtr t) rfl hI hti hkt h1 + have S2 : SIKA σ.idleThread (· = specPtr t) (· = specPtr t) false σ1 σ2 := + PresR.bind (eqPres_sikA (by unfold getTcbObjRef; exact threadGet_eqPres _ _)) + (fun o => refillCheck_sikA o) σ1 _ h2 + have S3 : SIKA σ.idleThread (· = specPtr t) (· = specPtr t) false σ2 σ3 := + doIpcTransfer_sikA hPl sf hSt hSV htr hks hky hfs hal none 0 grant hcaps σ2 rfl _ h3 + have S4 : SIKA σ.idleThread (· = specPtr t) (· = specPtr t) false σ3 σ4 := + setThreadState_sikA (i := σ.idleThread) (M := (· = specPtr t)) (N := (· = specPtr t)) (don := false) (specPtr t) rfl .running (fun c => by cases c) σ3 _ h4 + have S24 : SIKA σ.idleThread (· = specPtr t) (· = specPtr t) false σ1 σ4 := S2.trans (S3.trans S4) + have Q1 : QKeepA σ σ1 := (inferInstance : QInst (replyRemove (Z := Unit) (specPtr t) (specPtr r))).pres σ _ h1 + have Q2 : QKeepA σ1 σ2 := by + refine (show PresR QKeepA (getTcbObjRef (Z := Unit) Tcb.tcbSchedContext (specPtr t) >>= + ifSporadicCurScTestRefillUnblockCheck) from ?_) σ1 _ h2 + unfold ifSporadicCurScTestRefillUnblockCheck; qpres + have Q3 : QKeepA σ2 σ3 := + doIpcTransfer_any_qA hPl sf hSt hSV htr hks hky hal none 0 grant (Or.inl ⟨hfs, hcaps⟩) σ2 rfl _ h3 + have Q4 : QKeepA σ3 σ4 := (inferInstance : QInst (setThreadState (Z := Unit) (specPtr t) .running)).pres σ3 _ h4 + have SW : SwN (specPtr t) σ4 := by + obtain ⟨-, -, -, -, -, ns1⟩ := replyRemove_stepA hL hkt (Or.inl hst) h1 + have s1 : SwN (specPtr t) σ1 := fun u e => absurd e (ns1 hsw u) + have s2 : SwN (specPtr t) σ2 := (HT.bind (Q := fun _ => SwN (specPtr t)) + (HT.swn (by unfold getTcbObjRef; tpres) (specPtr t)) fun _ => + HT.swn (by unfold ifSporadicCurScTestRefillUnblockCheck ifCondRefillUnblockCheck; tpres) (specPtr t)) σ1 s1 _ h2 + have s3 : SwN (specPtr t) σ3 := + s2.tkeep (doIpcTransfer_tkA hPl sf hSt hSV htr hks hky hfs hal none 0 grant hcaps _ h3).1 + exact setThreadState_swn (specPtr t) .running σ3 s3 _ h4 + refine ⟨PreO.trans P1 (RepSubA.of_sik S24), (Q1.trans (Q2.trans (Q3.trans Q4))).toSend, rqDomPrio_stsA hD3 h4, ?_, + SW⟩ + have U1 : UFrA (BufBytesA (ipcBufOfA true y2)) σ σ1 := UFrA.of_fr (SPresInst.pres σ _ h1) + have U2 : UFrA (BufBytesA (ipcBufOfA true y2)) σ1 σ2 := by + refine UFrA.of_fr ((show SPresA (getTcbObjRef (Z := Unit) Tcb.tcbSchedContext (specPtr t) >>= + ifSporadicCurScTestRefillUnblockCheck) from ?_) σ1 _ h2) + exact SPresInst.pres + have U3 : UFrA (BufBytesA (ipcBufOfA true y2)) σ2 σ3 := + doIpcTransfer_ufrA hPl sf hSt hSV htr hks hky hfs hal none 0 grant hcaps σ2 rfl _ h3 + exact PreO.trans U1 (PreO.trans U2 (PreO.trans U3 (UFrA.of_fr (SPresInst.pres σ3 _ h4)))) + +/-- **The faulted Reply's bundle facts at the mid state** (module docstring). -/ +theorem replyFault_midXA {σ σ1 σ2 σ3 σ3c σ4 : State Unit} {m : McState} {n : Nat} {t r : ObjRef} + {xt : Tcb} (hkt : σ.kheap t = some (.tcb xt)) (hst : xt.tcbState = .blockedOnReply r) + (hI : SIdleA σ m n) (hinv : SInvXA σ m n) + (h1 : ((), σ1) ∈ (replyRemove (Z := Unit) t r σ).1) + (h2 : ((), σ2) ∈ ((getTcbObjRef (Z := Unit) Tcb.tcbSchedContext t >>= + ifSporadicCurScTestRefillUnblockCheck) σ1).1) + {uc : UserContext} + (h3 : ((), σ3) ∈ (asUser (Z := Unit) t (fun _ => ({((), uc)}, False)) σ2).1) + (h3c : ((), σ3c) ∈ (threadSet (Z := Unit) (fun tcb => { tcb with tcbFault := none }) t σ3).1) + (hD3 : RqDomPrio σ3c) {ts : ThreadState} (hts : ts = .restart ∨ ts = .inactive) + (h4 : ((), σ4) ∈ (setThreadState (Z := Unit) t ts σ3c).1) : + RepSubA σ σ4 ∧ QSendA σ σ4 ∧ RqDomPrio σ4 ∧ UFrA (fun _ => False) σ σ4 := by + have hti : t ≠ σ.idleThread := fun e => by + obtain ⟨x, hx, hs⟩ := hinv.idleTcb + rw [← e, hkt] at hx + cases hx + rw [hst] at hs + cases hs + obtain ⟨S1, P1, -⟩ := replyRemove_keepA (i := σ.idleThread) (t := t) rfl hI hti hkt h1 + have S2 : SIKA σ.idleThread (· = t) (· = t) false σ1 σ2 := + PresR.bind (eqPres_sikA (by unfold getTcbObjRef; exact threadGet_eqPres _ _)) + (fun o => refillCheck_sikA o) σ1 _ h2 + have S3 : SIKA σ.idleThread (· = t) (· = t) false σ2 σ3 := asUser_sikA t _ σ2 _ h3 + have S13 : SIKA σ.idleThread (· = t) (· = t) false σ1 σ3 := S2.trans S3 + have S03 : SIKA σ.idleThread (· = t) (· = t) true σ σ3 := S1.trans S13.up + obtain ⟨x3, hx3, -⟩ := S03.tcbF hkt + obtain ⟨x3', hx3', e3c⟩ := threadSet_memA _ t h3c + have ex : x3' = x3 := by + rw [hx3] at hx3' + exact (KernelObject.tcb.inj (Option.some.inj hx3')).symm + have e3 : σ3c = faultClrA σ3 t x3 := by + rw [ex] at e3c + exact e3c + have htsI : ts ≠ .idleThreadState := by rcases hts with rfl | rfl <;> intro c <;> cases c + have S4 : SIKA σ.idleThread (· = t) (· = t) false σ3c σ4 := + setThreadState_sikA (i := σ.idleThread) (M := (· = t)) (N := (· = t)) (don := false) t rfl ts htsI σ3c _ h4 + have R3c : RepSubA σ3 σ3c := by + rw [e3] + exact repSub_upd (fun _ _ c => by cases c) + have Q1 : QKeepA σ σ1 := (inferInstance : QInst (replyRemove (Z := Unit) t r)).pres σ _ h1 + have Q2 : QKeepA σ1 σ2 := by + refine (show PresR QKeepA (getTcbObjRef (Z := Unit) Tcb.tcbSchedContext t >>= + ifSporadicCurScTestRefillUnblockCheck) from ?_) σ1 _ h2 + unfold ifSporadicCurScTestRefillUnblockCheck; qpres + have Q3 : QKeepA σ2 σ3 := asUser_qA t _ σ2 _ h3 + have Q3c : QKeepA σ3 σ3c := + (show PresR QKeepA (threadSet (Z := Unit) (fun tcb => { tcb with tcbFault := none }) t) by qpres) σ3 _ h3c + have Q4 : QKeepA σ3c σ4 := (inferInstance : QInst (setThreadState (Z := Unit) t ts)).pres σ3c _ h4 + refine ⟨PreO.trans P1 (PreO.trans (RepSubA.of_sik S13) (PreO.trans R3c (RepSubA.of_sik S4))), + (Q1.trans (Q2.trans (Q3.trans (Q3c.trans Q4)))).toSend, rqDomPrio_stsA hD3 h4, ?_⟩ + have U1 : UFrA (fun _ => False) σ σ1 := UFrA.of_fr (SPresInst.pres σ _ h1) + have U2 : UFrA (fun _ => False) σ1 σ2 := by + refine UFrA.of_fr ((show SPresA (getTcbObjRef (Z := Unit) Tcb.tcbSchedContext t >>= + ifSporadicCurScTestRefillUnblockCheck) from ?_) σ1 _ h2) + exact SPresInst.pres + have U3 : UFrA (fun _ => False) σ2 σ3 := UFrA.of_fr (asUser_presA t _ σ2 _ h3) + have U3c : UFrA (fun _ => False) σ3 σ3c := + UFrA.of_fr ((show SPresA (threadSet (Z := Unit) (fun tcb => { tcb with tcbFault := none }) t) from + SPresInst.pres) σ3 _ h3c) + exact PreO.trans U1 (PreO.trans U2 (PreO.trans U3 (PreO.trans U3c (UFrA.of_fr (SPresInst.pres σ3c _ h4))))) + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3ReplyTmoRecvA.lean b/proofs/Fl4ProofsA64/P3/Stage3ReplyTmoRecvA.lean new file mode 100644 index 000000000..d842d42be --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ReplyTmoRecvA.lean @@ -0,0 +1,570 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2b): **the nested timeout send of a Reply, handler endpoint with a waiting +receiver**, at the record (shared by the live and the faulted Reply arm). + +The Reply's SC tail takes the timeout send of the woken caller `t` from the mid-Reply state (`ReplyTmoMidA`, the +facts `do_reply_transfer_keepTA` / `_keepFTA` hand over). **`JStage3A.replyTmoRecvA`** runs it by kernel entry 2's +`handle_timeout_recvBA` and rebuilds the record from the Reply's pre-state (`J`, at `s1` / `σ1`) by +`JStage3A.of_ipcStepUXA`: the spec step `σ1 → σ4 → σ4 with the timeout fault → τ` is one `SIKXA` (movers `t` and the +receiver `d`), the write set is the Reply's (`IpcFpA ∨ RepWA`) with the nested send's (mapped back to the pre-state) +and `t`'s fault words. The P1 parts of the record (`BXI`, `SysL`, `SymL`, `UserOK`, J7) are the caller's +(`ReplyTmoOutA`). + +* `ReplyTmoOutA`: the outcome (the record at the reached state, given the P1 parts). +* `inUserFrame_sikxA`, `ntfnWaitMA_compA`: the user frames across `SIKXA`; `NtfnWaitKeepMA` composed. + +Named premises: [P] `hmax`, `htu`, `hconv` (`handle_timeout_recvBA`'s), `hCs`, `one` (`numNodes = 1`). Case +selectors: the handler cap (`hslot`, `hcap`, `hgc`, `he`) and a receiver waiting on its endpoint (`hk`). Inputs from the +arm: `M` (the mid state), `wk` (the Reply's notification waiters kept), `hrt` (reply-object thread words kept but the +caller's). New module. +-/ +import Fl4ProofsA64.P3.Stage3ReplyTmoMidA +import Fl4ProofsA64.P3.Stage3ReplyTmoFootA +import Fl4ProofsA64.P3.Stage3SikxAlgA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} (st : machine.Store S) [L : LawfulStore st] {A : Type} (ar : machine.Arch A) + +/-- **The outcome of the nested timeout send**: the record at the reached state, given its P1 parts. -/ +def ReplyTmoOutA (AR : A → HwRootA → Prop) (Z : XSetsA) (H : Heap) (OS U Ac Pp Tb : Set U64) (Lv : U64 → Nat) + (ia ie : Nat) (N E Rp : Set U64) (Bf : Set (U64 × Word)) (κ : U64 → U64) (n : Nat) (nA : U64) (m : McState) + (a : A) (s' : S) (a' : A) (σ' : State Unit) : Prop := + a' = a ∧ ∃ Q Rl stack', ∀ (_ : BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s') + (_ : SysL st H Z.Cs OS ia ie s') (_ : SymL st H Z.Cs s') + (_ : Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s') (_ : J7A st (J7R2A st ar nA) Z H OS s' σ' m), + JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack' κ n (J5A st) + (J7A st (J7R2A st ar nA)) s' a σ' m + +end + +section +variable {i : ObjRef} {M N : ObjRef → Prop} {don : Bool} + +/-- The user frames across `SIKXA`. -/ +theorem inUserFrame_sikxA {σ σ' : State Unit} (K : SIKXA i M N don σ σ') {a : Word} (h : inUserFrame a σ) : + inUserFrame a σ' := by + obtain ⟨sz, hsz⟩ := h + refine ⟨sz, ?_⟩ + rcases K.obj (a &&& ~~~(mask (pageBitsForSize sz))) with o | ⟨x, y, e1, -, -⟩ + · rw [hsz] at o + generalize eb : σ'.kheap _ = b at o + cases o + rfl + · rw [hsz] at e1; cases e1 + +end + +theorem ntfnWaitMA_compA {M M' : ObjRef → Prop} {T N : Set U64} {m1 m2 m3 : U64 → U64} + (h1 : NtfnWaitKeepMA M T N m1 m2) (h2 : NtfnWaitKeepA T N m2 m3) (hM : ∀ p, M p → M' p) : + NtfnWaitKeepMA M' T N m1 m3 := by + intro y hy hn hN + obtain ⟨a1, b1, c1⟩ := h1 y hy (fun c => hn (hM _ c)) hN + obtain ⟨a2, b2, c2⟩ := h2 y hy (by rw [a1]; exact hN) + exact ⟨a2.trans a1, b2.trans b1, c2.trans c1⟩ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s1 : S} {a : A} {σ1 : State Unit} {m : McState} + +set_option maxHeartbeats 4000000 in +/-- **The nested timeout send to a waiting receiver, at the record** (module docstring). -/ +theorem JStage3A.replyTmoRecvA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s1 a σ1 m) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) (hconv : Fl4TicksToUsLegacyAgree) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (one : fl4A64Platform.numNodes = 1) + (hsw : ∀ u, σ1.schedulerAction ≠ .switchThread u) + (hcurR : ∀ z, σ1.kheap σ1.curThread = some (.tcb z) → runnable z.tcbState = true) + {t : U64} (ht : t ∈ Z.T) (hcur : specPtr t ≠ σ1.curThread) + (hidle : ∀ c < fl4A64Platform.numNodes, specPtr t ≠ m.idleThreadOf c) + {xt0 : Tcb} {rr : ObjRef} (hkt0 : σ1.kheap (specPtr t) = some (.tcb xt0)) + (hst0 : xt0.tcbState = .blockedOnReply rr) + {σ0 : State Unit} {Wn : U64 → Prop} {s4 : S} {σ4 : State Unit} {Q : U64 → U64 → List U64} + {R' : U64 → List U64} {stack' : U64 → List U64} {x : Tcb} + (MD : ReplyTmoMidA st Z m κ E Rp Bf s1 σ1 σ0 t Wn s4 σ4 Q R' stack' x) + (wk : NtfnWaitKeepMA (· = specPtr t) Z.T N (L.mem s1) (L.mem s4)) + (hrt : ∀ q ∈ Rp, L.mem s4 (Endp.rTcbA q) ≠ L.mem s1 (Endp.rTcbA q) → L.mem s1 (Endp.rTcbA q) = t) + {sc : U64} {y : SchedContext} {k : Nat} (badge : U64) (hsc : L.mem s4 (scA t) = sc) (h0 : sc ≠ 0#u64) + (hy : σ4.kheap (scNameA m sc) = some (.schedContext y k)) + {slot ep hbg : U64} {sd rv g gr : Bool} + (hslot : obj.Tcb.slot t layout.tcb.SLOT_TIMEOUT_HANDLER = ok slot) + (hcap : obj.Slot.cap st slot s4 = ok (.Endpoint ep hbg sd rv g gr)) + (hgc : tcbCnodeMap x (tcbCnodeIndex 4) = some (.endpointCap (specPtr ep) hbg.bv (rightsOfA sd rv g gr))) + (he : ep ∈ E) {d : U64} {q : List ObjRef} + (hk : σ4.kheap (specPtr ep) = some (.endpoint (.recvEP (specPtr d :: q)))) : + faults.handle_timeout st ar s4 a t (.Timeout badge) ⦃ o => ∃ σ', + ((), σ') ∈ (fl4HandleTimeout (Z := Unit) (specPtr t) (.timeout badge.bv) σ4).1 ∧ + ReplyTmoOutA st ar AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf κ n nA m a o.1 o.2 σ' ∧ + Sched.RepEndA σ1 σ' ⦄ := by + have HB := MD.sr + have hx4 := MD.kx + have K04 : SIKXA σ1.idleThread (· = specPtr t) (· = specPtr t) true σ1 σ4 := SIKXA.ofClr MD.keep MD.clr + have fr4 := K04.frame + have hcur4 : σ4.curThread = σ1.curThread := by rw [fr4] + have hidle4 : σ4.idleThread = σ1.idleThread := by rw [fr4] + have hσ0f : ∀ p, p ≠ specPtr t → σ0.kheap p = σ1.kheap p := fun p hp => by + rcases MD.clr with e | ⟨z, -, e⟩ + · rw [e] + · rw [e]; exact faultClrA_ne hp + -- the caller `t` + have hnI : specPtr t ≠ σ1.idleThread := fun e => by + obtain ⟨xi, hxi, hsi⟩ := J.sinv.idleTcb + rw [← e, hkt0] at hxi; cases hxi; rw [hst0] at hsi; cases hsi + have hidl : ∀ c < n, m.idleThreadOf c ≠ specPtr t := fun c hc e => by + obtain ⟨xi, hxi, hsi⟩ := J.sidle.f3 c hc + rw [e, hkt0] at hxi; cases hxi; rw [hst0] at hsi; cases hsi + have hsc0 : L.mem s4 (scA t) ≠ 0#u64 := hsc ▸ h0 + obtain ⟨x0, hx0, hxs0⟩ := HB.sch.tcb t ht + have ex0 : x0 = x := KernelObject.tcb.inj (Option.some.inj (hx0.symm.trans hx4)) + subst x0 + have hxs' : x.tcbSchedContext = some (scNameA m sc) := by + rw [hxs0, hsc, optScA, if_neg h0] + rfl + have hNI : NotIdleScA m (scNameA m sc) := HB.inv.tcbScNotIdle _ x _ hx4 hxs' hidle + have hname : scNameA m sc = specPtr sc := scNameA_eq_of_notIdle hNI + have hxsc : x.tcbSchedContext = some (specPtr (L.mem s4 (scA t))) := by rw [hsc, ← hname]; exact hxs' + have hky : σ4.kheap (specPtr (L.mem s4 (scA t))) = some (.schedContext y k) := by rw [hsc, ← hname]; exact hy + have hRun : SndRunA σ4 (specPtr t) x := + ⟨MD.run, ⟨MD.nq, MD.nrel⟩, fun u e => (MD.nsw u e).2, by rw [hidle4]; exact hnI⟩ + have hD : DonA σ4 (specPtr t) x := by + intro c hc + rw [hxs'] at hc + cases hc + refine ⟨hNI.1, fun y' k' hy' => ?_⟩ + obtain ⟨z, j, hz, hb⟩ := HB.inv.back _ x _ hx4 hxs' + rw [hy'] at hz; cases hz + exact hb + -- the write set of the Reply part and the relations at the mid state + have wI : WritesIn st s1 s4 (fun b => IpcFpA st Z E Rp Bf m σ1 s1 b ∨ RepWA Rp b) := MD.w.mono fun b h => by + rcases h with h | h | ⟨q', hq, h⟩ + · exact Or.inl (Or.inl h) + · exact Or.inl (Or.inr h) + · have h2 := replyNextPlaceA (Z := Z) J.iplaced.e hq + have h3 := replyPrevPlaceA (Z := Z) J.iplaced.e hq + refine Or.inr ⟨q', hq, ?_⟩ + rcases h with h | h <;> rw [h] <;> omega + have tw4 := J.tw_ipcA wI + have KN4 : IpcKeepNA σ1 σ4 := Fl4ProofsA64.P3.SIKXA.ipcKeepN K04 + have kb4 : L.ksb s4 = L.ksb s1 := MD.w.1 + have slotK : ∀ y', IsSlotA Z.th y' → capAtA (L.mem s4) y' = capAtA (L.mem s1) y' := by + intro y' hy' + have keep := slotWordsKeepA J.sr.toSchedSRN J.iplaced.e J.ipcPlace J.slotOffEwA MD.w + (Fl4ProofsA64.P3.Sched.IrqSlotsKeepA.of_replyFoot J.sr.toSchedSRN J.iplaced.e J.ipcPlace MD.w) _ hy' + unfold capAtA + rw [keep.1, keep.2] + have hSt4 : StateRelA Z.th (L.mem s4) σ4 := by + have h := J.j.1.st.congrA (mem' := L.mem s4) fun y' hy' => slotK y' (slotW_isSlot J.sr.place hy') + refine ⟨fun p r hp => ?_, fun u hu => ?_⟩ + · obtain ⟨cs, h1, h2', h3⟩ := h.cnode p r hp + refine ⟨cs, ?_, h2', h3⟩ + rcases K04.obj (specPtr p) with o | ⟨x', y', e1, -, -⟩ + · rw [h1] at o + generalize eb : σ4.kheap _ = b at o + cases o + rfl + · rw [h1] at e1; cases e1 + · obtain ⟨x1, h1, h2'⟩ := h.tcb u hu + obtain ⟨y1, hy1, g⟩ := K04.tcbF h1 + exact ⟨y1, hy1, by rw [g.caps]; exact h2'⟩ + have hV4 : SlotsValidA Z.th (L.mem s4) := fun y' hy' => by rw [slotK y' hy']; exact J.j.1.cv y' hy' + have at_ : ∀ u ∈ Z.T, ∃ x', σ1.kheap (specPtr u) = some (.tcb x') := fun u hu => by + obtain ⟨x', hx', -⟩ := J.ntfn.tcb u hu + exact ⟨x', hx'⟩ + have hC4 : BufCover st Z.T Bf s4 σ4 := by + intro u hu y' b sb v hy' hb hl + obtain ⟨x', hx'⟩ := at_ u hu + obtain ⟨y0, hy0, -, hfr, hbuf, -, -⟩ := KN4.tcb _ _ hx' + rw [hy'] at hy0 + cases hy0 + have hb0 : ipcBufOfA b x' = some sb := by + unfold ipcBufOfA at hb ⊢ + rwa [hfr, hbuf] at hb + have hib : IbSame (L := L) s1 s4 u := by + refine ⟨?_, ?_, ?_⟩ + · rw [ibWordA_eq]; exact tw4 u hu _ (by simp) (by simp) + · rw [sbWordA_eq]; exact tw4 u hu _ (by simp) (by simp) + · rw [sbWordA8_eq]; exact tw4 u hu _ (by simp) (by simp) + rw [lookup_ipc_buffer_sameA hib] at hl + exact J.bufCover u hu x' b sb v hx' hb0 hl + have hN4 : BufNativeA Z.T (L.mem s4) := by + apply J.bufNative.frame + intro u hu + rw [sbWordA8_eq, sbWordA_eq] + exact ⟨tw4 u hu _ (by simp) (by simp), tw4 u hu _ (by simp) (by simp)⟩ + have hM4 : McpRelA Z.T m (L.mem s4) σ4 := + J.mcp.congrA (fun u hu => ⟨tw4 u hu _ (by simp) (by simp), tw4 u hu _ (by simp) (by simp)⟩) fun u hu x' hx' => by + obtain ⟨y', hy', -, -, -, hm, -, hf⟩ := KN4.tcb _ _ hx' + exact ⟨y', hy', hm, hf⟩ + have ye : ∀ u ∈ Z.T, L.mem s4 (Fl4ProofsA64.P1.yieldA u) = L.mem s1 (Fl4ProofsA64.P1.yieldA u) := fun u hu => by + unfold Fl4ProofsA64.P1.yieldA; exact tw4 u hu _ (by simp) (by simp) + have hY4 : YieldRelA Z.T Z.C m (L.mem s4) σ4 := by + refine ⟨fun u hu => ?_, fun u hu h => ?_, fun c hc y' k' h p hp => ?_⟩ + · obtain ⟨x', hx', e⟩ := J.yieldRel.tcb u hu + obtain ⟨y', hy', -, -, -, -, hyt, -⟩ := KN4.tcb _ _ hx' + exact ⟨y', hy', by rw [ye u hu, hyt]; exact e⟩ + · rw [ye u hu] at h ⊢ + obtain ⟨hC, y', k', hy', e⟩ := J.yieldRel.toSc u hu h + obtain ⟨y'', k'', hy'', e'⟩ := KN4.scF _ _ _ hy' + exact ⟨hC, y'', k'', hy'', e'.trans e⟩ + · obtain ⟨y0, k0, h0', e⟩ := KN4.scB _ _ _ h + obtain ⟨u, hu, e1, e2⟩ := J.yieldRel.frm c hc y0 k0 h0' p (e ▸ hp) + exact ⟨u, hu, e1, by rw [ye u hu]; exact e2⟩ + -- the receiver `d` + obtain ⟨-, xs, -, hq', hQ, hB⟩ := MD.ep.at_ep he hk + have hdx : d ∈ xs := by + have : specPtr d ∈ xs.map specPtr := by rw [← hq']; exact List.mem_cons_self + obtain ⟨y', hy', e⟩ := List.mem_map.1 this + rwa [specPtr_inj e] at hy' + have hd : d ∈ Z.T := hQ.mem d hdx + have hdb : L.mem s4 (Notif.tBlockA d) = ep := (hB d hdx).2 + obtain ⟨xd, hkd, rd⟩ := MD.ep.tcb d hd + obtain ⟨e', rep, dd, hstd⟩ := tsCode_three (rd.st.symm.trans (hB d hdx).1) + have hdth : Z.th d = some .tcb := (J.sr.thT d).2 hd + have hdt : specPtr d ≠ specPtr t := fun e => by + rw [e, hx4] at hkd; cases hkd + have r := MD.run + rw [hstd] at r + cases r + have hdt' : d ≠ t := fun e => hdt (by rw [e]) + obtain ⟨xd1, hkd1, gd⟩ := K04.tcbB hkd + have hstd1 : xd1.tcbState = xd.tcbState := (gd.st.resolve_right fun c => hdt c.1).symm + have hbuf1 : ipcBufOfA true xd = ipcBufOfA true xd1 := by unfold ipcBufOfA; rw [gd.frame, gd.buf] + have hal : ∀ rb, ipcBufOfA true xd = some rb → (bufAddr rb 0).toNat % 8 = 0 := fun rb h => + J.bufAlign0A hd hkd1 true rb (hbuf1 ▸ h) + have hu : ∀ rp, ipcBufOfA true xd = some rp → ∀ k', k' < 128 → inUserFrame (bufAddr rp k') σ4 := fun rp h k' hk' => + inUserFrame_sikxA K04 (J.bufFrameA hd hkd1 true rp (hbuf1 ▸ h) k' hk') + have hrep : RepOKA σ4 (specPtr d) := by + intro z hz e2 r2 dd2 hs2 + rw [hkd] at hz; cases hz + have hs1 : xd1.tcbState = .blockedOnReceive e2 (some r2) dd2 := hstd1.trans hs2 + obtain ⟨z1, hz1, rel1⟩ := J.ep.tcb d hd + rw [hkd1] at hz1; cases hz1 + obtain ⟨hr1, hr2⟩ := rel1.recv e2 (some r2) dd2 hs1 + have hiR := hr2 (by simp) + have h0' : L.mem s1 (tReplyA d) ≠ 0#u64 := fun e => by rw [e, optRef_zero] at hr1; cases hr1 + have er : r2 = specPtr (L.mem s1 (tReplyA d)) := by + rw [optRef_ne h0'] at hr1; exact (Option.some.inj hr1).symm + obtain ⟨xr, hxr, hbk⟩ := J.repOKA hd xd1 hkd1 e2 r2 dd2 hs1 + obtain ⟨xr1, hkr1, hb1⟩ := J.ep.reply _ hiR + obtain ⟨xr4, hkr4, hb4⟩ := MD.ep.reply _ hiR + rw [← er] at hkr1 hkr4 + rw [hxr] at hkr1; cases hkr1 + refine ⟨xr4, hkr4, ?_⟩ + rw [hb4] + by_cases ew : L.mem s4 (Endp.rTcbA (L.mem s1 (tReplyA d))) = L.mem s1 (Endp.rTcbA (L.mem s1 (tReplyA d))) + · rw [ew, ← hb1, hbk] + · have et := hrt _ hiR ew + rw [hb1, et, optRef_ne (J.sr.place.nz t _ ((J.sr.thT t).2 ht))] at hbk + exact absurd (specPtr_inj (Option.some.inj hbk)) (fun e => hdt' e.symm) + have hon : L.mem s4 (affA d) = L.cpu s4 := by + have x1 := hCs _ (HB.core.aff d hd) + have y1 := hCs _ HB.sch.cpuCs + exact UScalar.eq_of_val_eq (by omega) + have hgc' : getCap (Z := Unit) (getTcbTimeoutHandlerPtr (specPtr t)) σ4 = + ({(.endpointCap (specPtr ep) hbg.bv (rightsOfA sd rv g gr), σ4)}, False) := getCap_tcb_eqA hx4 hgc + refine WP.spec_mono (handle_timeout_recvBA (st := st) (ar := ar) (N := N) (Z := { Z with Q := Q, R := R' }) a J.ipcPlace J.iplaced.e MD.ep + J.sr.place hSt4 hV4 hC4 hN4 J.slotOffEwA hmax htu hconv HB hM4 hY4 ht hx4 hRun hD hslot hcap hgc' he hk hd hkd + hu hal hrep hon hidle hsc0 hxsc hky badge) ?_ + rintro ⟨s5, a5⟩ ⟨ha5, τ, hfl, hsi, ⟨Q5, R5, HQ⟩, HE5, w45, sz45⟩ + refine ⟨τ, by rw [hfl]; exact Set.mem_singleton _, ?_⟩ + -- the spec steps from the faulted mid state + obtain ⟨x1, ex1⟩ : ∃ x1 : Tcb, x1 = { x with tcbFault := some (.timeout badge.bv) } := ⟨_, rfl⟩ + obtain ⟨σf, eσf⟩ : ∃ σf : State Unit, σf = updK σ4 (specPtr t) (.tcb x1) := ⟨_, rfl⟩ + have hx1f : x1.tcbFault ≠ none := by rw [ex1]; simp + have hx1f' : x1.tcbFault = some (.timeout badge.bv) := by subst ex1; rfl + have hx1sc : x1.tcbSchedContext = x.tcbSchedContext := by subst ex1; rfl + have hcm : tcbCnodeMap x1 = tcbCnodeMap x := by subst ex1; rfl + have hmem : ((), τ) ∈ (sendIpc (Z := Unit) true false hbg.bv g gr false (specPtr t) (specPtr ep) σf).1 := by + have h' := hsi + rw [← ex1, ← eσf] at h' + rw [h']; exact Set.mem_singleton _ + have hept : specPtr ep ≠ specPtr t := fun e => by rw [e, hx4] at hk; cases hk + have far1 : ∀ p, p ≠ specPtr t → σf.kheap p = σ4.kheap p := fun p hp => by rw [eσf]; simp [updK, hp] + have hkt1 : σf.kheap (specPtr t) = some (.tcb x1) := by rw [eσf]; simp [updK] + have hk1 : σf.kheap (specPtr ep) = some (.endpoint (.recvEP (specPtr d :: q))) := by + rw [far1 _ hept]; exact hk + have hkd1' : σf.kheap (specPtr d) = some (.tcb xd) := by rw [far1 _ hdt]; exact hkd + have hSt1 : StateRelA Z.th (L.mem s4) σf := by + refine ⟨fun p r hp => ?_, fun u hu => ?_⟩ + · obtain ⟨cs, h1, h2', h3⟩ := hSt4.cnode p r hp + have hne : specPtr p ≠ specPtr t := fun e => by rw [e, hx4] at h1; cases h1 + exact ⟨cs, by rw [far1 _ hne]; exact h1, h2', h3⟩ + · obtain ⟨x2, h1, h2'⟩ := hSt4.tcb u hu + by_cases e : specPtr u = specPtr t + · rw [e] at h1 + have e5 : x2 = x := KernelObject.tcb.inj (Option.some.inj (h1.symm.trans hx4)) + subst x2 + exact ⟨x1, by rw [e]; exact hkt1, hcm ▸ h2'⟩ + · exact ⟨x2, by rw [far1 _ e]; exact h1, h2'⟩ + have hD1 : DonA σf (specPtr t) x1 := fun sc' hs' => + ⟨(hD sc' (hx1sc ▸ hs')).1, fun y' k' hy' => (hD sc' (hx1sc ▸ hs')).2 y' k' (by + have hne : sc' ≠ specPtr t := fun e => by rw [e, hkt1] at hy'; cases hy' + rwa [far1 _ hne] at hy')⟩ + have hdI : specPtr d ≠ σ1.idleThread := fun e => by + obtain ⟨xi, hxi, hsi'⟩ := J.sinv.idleTcb + rw [← e, hkd1] at hxi; cases hxi; rw [hstd1, hstd] at hsi'; cases hsi' + let Mx : ObjRef → Prop := fun o => o = specPtr t ∨ o = specPtr d + let Nx : ObjRef → Prop := fun o => o = specPtr d + have K2 : SIKA σ1.idleThread Mx Nx true σf τ := + sendIpc_recv_sikFA (i := σ1.idleThread) (M := Mx) (N := Nx) J.sr.place s4 hSt1 hV4 hdth hk1 hkt1 hkd1' hal + true false hbg.bv g gr false (Or.inr ⟨_, hx1f'⟩) (Or.inl rfl) (Or.inr rfl) rfl hnI hdI hD1 + (fun h => by cases h) (fun _ => rfl) _ hmem + have Q2 : QSendA σf τ := + sendIpc_recv_qsA (i := σ1.idleThread) (M := Mx) (N := Nx) J.sr.place s4 hSt1 hV4 hdth hk1 hkt1 hkd1' hal + true false hbg.bv g gr false (Or.inr ⟨_, hx1f'⟩) (Or.inl rfl) (Or.inr rfl) rfl hnI hdI hD1 + (fun h => by cases h) (fun h => by cases h) + (fun e2 r2 dd2 h => by + obtain ⟨xr, k2, b2⟩ := hrep xd hkd e2 r2 dd2 h + exact ⟨xr, by rw [far1 _ (fun e => by rw [e, hx4] at k2; cases k2)]; exact k2, by rw [b2]; simp⟩) _ hmem + have U2 : UFrA (BufBytesA (ipcBufOfA true xd)) σf τ := + sendIpc_recv_ufrFA J.sr.place s4 hSt1 hV4 hdth hk1 hkt1 hkd1' hal true false hbg.bv g gr false + (Or.inr ⟨_, hx1f'⟩) _ hmem + have SV4 : SchedValidA σ4 := MD.fia.1.toA MD.fia.2 + have SV1 : SchedValidA σf := by rw [eσf, ex1]; exact SchedValidA.faultA SV4 hx4 _ + have RQ1 : RqDomPrio σf := by rw [eσf, ex1]; exact rqDomPrio_faultA MD.dom hx4 _ + have actT1 : ActTcbA σf (specPtr t) := by + obtain ⟨z, c, hz, hzc, ha⟩ := MD.bnd x _ hx4 hxs' + rw [hx4] at hz; cases hz + rw [hxs'] at hzc; cases hzc + obtain ⟨y', k', hy', act⟩ := ha + refine ⟨x1, scNameA m sc, hkt1, hx1sc.trans hxs', y', k', ?_, act⟩ + rw [far1 _ (fun e => by rw [e, hx4] at hy'; cases hy')]; exact hy' + have hbr : (false = false ∧ false = false ∧ x1.tcbFault = none) ∨ + ((false = true ∨ x1.tcbFault ≠ none) ∧ ((g = false ∧ gr = false) ∨ rep = none)) ∨ + ((false = true ∨ x1.tcbFault ≠ none) ∧ (g = true ∨ gr = true) ∧ rep ≠ none ∧ specPtr t ≠ specPtr d ∧ + ActTcbA σf (specPtr t) ∧ (false = true → xd.tcbSchedContext = none)) ∨ + ((false = true ∨ x1.tcbFault ≠ none) ∧ (g = true ∨ gr = true) ∧ rep ≠ none ∧ false = true ∧ + specPtr t ≠ specPtr d ∧ xd.tcbSchedContext ≠ none ∧ ActTcbA σf (specPtr t)) := by + by_cases hg' : g = true ∨ gr = true + · by_cases hr : rep = none + · exact Or.inr (Or.inl ⟨Or.inr hx1f, Or.inr hr⟩) + · exact Or.inr (Or.inr (Or.inl ⟨Or.inr hx1f, hg', hr, hdt.symm, actT1, fun h => by cases h⟩)) + · refine Or.inr (Or.inl ⟨Or.inr hx1f, Or.inl ?_⟩) + simp only [not_or, Bool.not_eq_true] at hg' + exact hg' + have SV5 : SchedValidA τ := + sendIpc_recv_schedValidFA J.sr.place s4 hSt1 hV4 hdth hk1 hkt1 hkd1' hal true false hbg.bv g gr false + (Or.inr ⟨_, hx1f'⟩) hstd (hbr.imp id (Or.imp id (Or.imp id Or.inl))) SV1 RQ1 _ hmem + have run5 : StIs (specPtr d) .running τ := sendIpc_recv_runningA hk1 true false hbg.bv g gr false _ hmem + have snd5 := sendIpc_fault_senderA J.sr.place s4 hSt1 hV4 hdth hk1 hkt1 hkd1' hal hstd hdt.symm + hx1f true hbg.bv g gr (Or.inr ⟨_, hx1f'⟩) _ hmem + -- the composed spec step + have KA : SIKXA σ1.idleThread (· = specPtr t) Nx true σ1 σ4 := + K04.narrowN (N' := Nx) (fun y' e d' p => e ▸ MD.nq d' p) (fun y' e h => (MD.nsw _ h).2 (e ▸ rfl)) + have KB : SIKXA σ1.idleThread Mx Nx true σ1 σ4 := KA.mono (M' := Mx) (fun _ h => Or.inl h) (fun _ h => h) id + have KF : SIKXA σ1.idleThread Mx Nx true σ4 σf := by + rw [eσf, ex1]; exact SIKXA.updFault hx4 _ + have K15 : SIKXA σ1.idleThread Mx Nx true σ1 τ := (KB.transX KF).transS K2 + have Qf : QKeepA σ4 σf := by + rw [eσf, ex1] + exact (QKeepA.upd hx4 (QObjA.ofTcb (y := { x with tcbFault := some (.timeout badge.bv) }) rfl)).trans + (QKeepA.of_kheap rfl ⟨rfl, rfl, rfl, rfl⟩) + have Qs : QSendA σ1 τ := MD.qs.trans (Qf.toSend.trans Q2) + have fault5 : ∀ p, p ≠ specPtr t → ∀ x', σ1.kheap p = some (.tcb x') → ∃ y', τ.kheap p = some (.tcb y') ∧ + y'.tcbFault = x'.tcbFault := fun p hp x' hx' => by + obtain ⟨y4, hy4, g4⟩ := MD.keep.tcbF (show σ0.kheap p = some (.tcb x') by rw [hσ0f p hp]; exact hx') + obtain ⟨y5, hy5, g5⟩ := K2.tcbF (show σf.kheap p = some (.tcb y4) by rw [far1 p hp]; exact hy4) + exact ⟨y5, hy5, g5.fault.trans g4.fault⟩ + -- the write sets from the pre-state + have mapN : ∀ b, (FaultWA t b ∨ TmoFpA st { Z with Q := Q, R := R' } m Rp Bf σ4 s4 t ep d b) → + IpcFpA st Z E Rp Bf m σ1 s1 b ∨ FaultWA t b := by + intro b h + rcases h with h | h + · exact Or.inr h + · rcases tmoFpA_splitA (Z := { Z with Q := Q, R := R' }) J.iplaced.e.tcb ht he hd h with h | h + · exact Or.inl (Or.inl h) + · exact Or.inl (Or.inr (mwr_back (Z := Z) (Z' := { Z with Q := Q, R := R' }) rfl rfl MD.sz kb4 h)) + have w45' : WritesIn st s4 s5 (fun b => IpcFpA st Z E Rp Bf m σ1 s1 b ∨ FaultWA t b) := w45.mono mapN + have wAll : WritesIn st s1 s5 (fun b => (IpcFpA st Z E Rp Bf m σ1 s1 b ∨ RepWA Rp b) ∨ FaultWA t b) := + (wI.trans w45').mono fun b h => h.elim Or.inl fun h => h.elim (fun h => Or.inl (Or.inl h)) Or.inr + have wN : WritesIn st s1 s5 (fun b => SchedStepNWA (st := st) N E Rp Z m σ1 s1 b ∨ BufW Bf b) := by + refine (MD.w.trans w45').mono fun b h => ?_ + rcases h with h | h | h + · exact replyFootW_wA J.iplaced.e h + · rcases h with h | h + · exact ewrite_stepNA_buf J.sr.core.tcbs h + · exact Or.inl (Or.inl (Or.inl h)) + · exact Or.inl (Or.inl (Or.inr (faultTcbStepA J.sr.core.tcbs ht h))) + -- user memory and the spec frame + obtain ⟨z0, hz0, U14⟩ := MD.ufr + have Uf : UFrA (fun _ => False) σ4 σf := by + rw [eσf, ex1] + exact UFrA.of_fr (SchedFrameA.upd_tcb (x' := { x with tcbFault := some (.timeout badge.bv) }) hx4 ⟨rfl, rfl, rfl⟩) + have HS : SpecSchedA σ1 τ := (U14.1.trans Uf.1).trans U2.1 + have SSz : SSzA σ1 τ := (U14.2.1.trans Uf.2.1).trans U2.2.1 + have hz : ∀ w : Word, UtFreeA st OS s1 (fun d => d = false) (fl4Ptr w).val → + τ.machineState.underlyingMemory w = σ1.machineState.underlyingMemory w := by + classical + intro w hw + have a1 := J.specZero_of_recvBufA ht hz0 (σ' := σ4) (fun v hv => U14.2.2 v hv) w hw + have a2 := J.specZero_of_recvBufA hd hkd1 (σ' := { σ1 with machineState := { σ1.machineState with + underlyingMemory := fun v => if BufBytesA (ipcBufOfA true xd) v then τ.machineState.underlyingMemory v + else σ1.machineState.underlyingMemory v } }) (fun v hv => by + show (if BufBytesA (ipcBufOfA true xd) v then _ else _) = _ + rw [if_neg (by rw [hbuf1]; exact hv)]) w hw + by_cases hb : BufBytesA (ipcBufOfA true xd) w + · have a3 : (if BufBytesA (ipcBufOfA true xd) w then τ.machineState.underlyingMemory w + else σ1.machineState.underlyingMemory w) = σ1.machineState.underlyingMemory w := a2 + rw [if_pos hb] at a3 + exact a3 + · rw [U2.2.2 w hb, Uf.2.2 w id, a1] + -- the reply stacks + have link45 : ∀ q' ∈ Rp, L.mem s5 (rNextA q') = L.mem s4 (rNextA q') ∧ + L.mem s5 (rPrevA q') = L.mem s4 (rPrevA q') := by + intro q' hq' + have f : ∀ o : U64, 8 ≤ o.val → o.val < 32 → o.val % 8 = 0 → L.mem s5 (wa q' o) = L.mem s4 (wa q' o) := + fun o h1 h2 h3 => by + obtain ⟨al, nw⟩ := J.replyLinks_offA ht hq' o h1 h2 h3 + exact w45'.mem_eq al nw + exact ⟨f _ (by simp [layout.reply.NEXT]) (by simp [layout.reply.NEXT]) (by simp [layout.reply.NEXT]), + f _ (by simp [layout.reply.PREV]) (by simp [layout.reply.PREV]) (by simp [layout.reply.PREV])⟩ + have K2f : SIKA σ1.idleThread Mx Nx false σf τ := + sendIpc_recv_sikFA (i := σ1.idleThread) (M := Mx) (N := Nx) J.sr.place s4 hSt1 hV4 hdth hk1 hkt1 hkd1' hal + true false hbg.bv g gr false (Or.inr ⟨_, hx1f'⟩) (Or.inl rfl) (Or.inr rfl) rfl hnI hdI hD1 + (fun h => by cases h) (fun _ => rfl) _ hmem + have RS5 : ReplyStackRelA (L.mem s5) Rp Z.C τ (scNameA m) stack' := by + refine MD.rs.frame link45 (fun c _ x' k' h => ?_) (fun r' _ y' h => ?_) + · have hne : scNameA m c ≠ specPtr t := fun e => by rw [e, hx4] at h; cases h + obtain ⟨y', hy', g'⟩ := K2f.scF (show σf.kheap (scNameA m c) = some (.schedContext x' k') by + rw [far1 _ hne]; exact h) + exact ⟨y', k', hy', g'.reps rfl⟩ + · obtain ⟨x', hx', e⟩ := K2f.replyB h + have hne : specPtr r' ≠ specPtr t := fun e' => by rw [e', hkt1] at hx'; cases hx' + exact ⟨x', by rw [far1 _ hne] at hx'; exact hx', e⟩ + have sub : ∀ c ∈ Z.C, ∀ q' ∈ stack' c, q' ∈ stack c := by + intro c hc q' hq' + obtain ⟨⟨y4, n4, hy4, l4⟩, -⟩ := MD.rs.stackRel c hc + obtain ⟨⟨y0, n0, hy0, l0⟩, -⟩ := J.reply.stackRel c hc + obtain ⟨y', k', hy', sb⟩ := MD.rsub _ _ _ hy4 + rw [hy0] at hy' + cases hy' + have := sb (specPtr q') (by rw [l4]; exact List.mem_map_of_mem hq') + rw [l0] at this + obtain ⟨q'', hq'', e⟩ := List.mem_map.1 this + rwa [specPtr_inj e] at hq'' + -- the movers + have hdst1 : xd1.tcbState = .blockedOnReceive e' rep dd := hstd1.trans hstd + have hkd1x : σ1.kheap (specPtr d) = some (.tcb xd1) := hkd1 + have notM : ∀ c < n, ¬ Mx (m.idleThreadOf c) := by + intro c hc h + obtain ⟨y1, hy1, hs1⟩ := J.sidle.f3 c hc + rcases h with h | h + · rw [h, hkt0] at hy1; cases hy1; rw [hs1] at hst0; cases hst0 + · rw [h, hkd1x] at hy1; cases hy1; rw [hs1] at hdst1; cases hdst1 + have hMq : ∀ y', Mx y' → ∀ dm pr, y' ∉ σ1.readyQueues dm pr := by + intro y' hy' dm pr hm + obtain ⟨z, hz, hr⟩ := J.sinv.rq dm pr _ hm + rcases hy' with rfl | rfl + · rw [hkt0] at hz; cases hz; rw [hst0] at hr; cases hr + · rw [hkd1x] at hz; cases hz; rw [hdst1] at hr; cases hr + have hdc : specPtr d ≠ σ1.curThread := fun e => by + have := hcurR xd1 (by rw [← e]; exact hkd1x) + rw [hdst1] at this; cases this + have sinv : SInvXA τ m n := + K15.sinv J.sinv notM (fun h => h.elim (fun e => hnI e.symm) (fun e => hdI e.symm)) hMq + (fun y' hy' => by + cases hy' + exact ⟨hdc, hdI, fun c hc e => notM c hc (Or.inr e), fun z hz => by rw [run5 z hz]; rfl⟩) + (fun y' hy' => by cases hy'; exact ⟨xd1, hkd1x⟩) hsw + (fun y hy hr => by + obtain ⟨z, hz, hrn⟩ := J.sinv.rlsRun _ hr + rcases hy with rfl | rfl + · rw [hkt0] at hz; cases hz; rw [hst0] at hrn; cases hrn + · rw [hkd1x] at hz; cases hz; rw [hdst1] at hrn; cases hrn) + (Or.inl (by + have e1 : τ.consumedTime = σf.consumedTime := by rw [K2.frame] + have e2 : σf.consumedTime = σ4.consumedTime := by rw [eσf]; rfl + have e3 : σ4.consumedTime = σ0.consumedTime := by rw [MD.keep.frame] + have e4 : σ0.consumedTime = σ1.consumedTime := by + rcases MD.clr with h | ⟨y, -, h⟩ + · rw [h] + · rw [h]; rfl + rw [e1, e2, e3, e4])) + (J.sidle.rdyA J.sinv) + (K15.faultStA J.sinv.faultSt + (fun p hp x y hx hy => by + obtain ⟨y', hy', e⟩ := fault5 p (fun e => hp (Or.inl e)) x hx + rw [hy] at hy'; cases hy'; exact e) + (fun p hp y hy hf => by + rcases hp with rfl | rfl + · rcases snd5 with h3 | ⟨r, h3⟩ + · rw [h3 y hy]; exact Or.inr (Or.inr rfl) + · rw [h3 y hy]; exact Or.inr (Or.inl ⟨_, rfl⟩) + · obtain ⟨y', hy', e⟩ := fault5 _ hdt xd1 hkd1x + rw [hy] at hy'; cases hy' + exact absurd (e.trans (FaultStOkA.none J.sinv.faultSt hkd1x (by + rw [hdst1]; rintro (⟨_, _, h⟩ | ⟨_, h⟩ | h) <;> cases h))) hf)) + have idle : IdleNoFaultA m τ := fun c hc => by + obtain ⟨x', hx', hf⟩ := J.idleNoFault c hc + obtain ⟨y', hy', e⟩ := fault5 _ (fun e => hidle c hc e.symm) x' hx' + exact ⟨y', hy', e.trans hf⟩ + have hMs : ∀ u ∈ Z.T, Mx (specPtr u) → ∀ x', σ1.kheap (specPtr u) = some (.tcb x') → + ∀ p, x'.tcbState ≠ .blockedOnNotification p := by + intro u _ hu x' hx' p hp + rcases hu with h | h + · rw [h, hkt0] at hx'; cases hx'; rw [hst0] at hp; cases hp + · rw [h, hkd1x] at hx'; cases hx'; rw [hdst1] at hp; cases hp + have afterM : ∀ u ∈ Z.T, Mx (specPtr u) → ∀ x', τ.kheap (specPtr u) = some (.tcb x') → + Fl4SpecA64.epBlocked x'.tcbState = none ∧ ∀ p, x'.tcbState ≠ .blockedOnNotification p := by + intro u _ hu x' hx' + rcases hu with h | h + · rw [h] at hx' + rcases snd5 with h3 | ⟨r, h3⟩ + · rw [h3 x' hx']; exact ⟨rfl, fun _ h => by cases h⟩ + · rw [h3 x' hx']; exact ⟨rfl, fun _ h => by cases h⟩ + · rw [h] at hx' + rw [run5 x' hx']; exact ⟨rfl, fun _ h => by cases h⟩ + have hMv : ∀ u ∈ Z.T, Mx (specPtr u) → L.mem s1 (Fl4ProofsA64.P1.stA u) ≠ sched.TS_RUNNING_VM := by + intro u hu hm h' + rcases hm with e | e <;> rw [specPtr_inj e] at h' + · have h5 : (L.mem s1 (stA t)).val = 5 := by rw [(J.ep.at_tcb ht hkt0).st, hst0]; rfl + rw [h'] at h5; revert h5; unfold sched.TS_RUNNING_VM; simp + · have h3 : (L.mem s1 (stA d)).val = 3 := by rw [(J.ep.at_tcb hd hkd1x).st, hdst1]; rfl + rw [h'] at h3; revert h3; unfold sched.TS_RUNNING_VM; simp + have wk45 : NtfnWaitKeepA Z.T N (L.mem s4) (L.mem s5) := + J.ntfnWait_sendGA (W := True) (Z0 := { Z with Q := Q, R := R' }) (s0 := s4) (σ0 := σ4) ht ht he hd hdb + (fun b h => mwr_back (Z := Z) (Z' := { Z with Q := Q, R := R' }) rfl rfl MD.sz kb4 h) + (w45.mono fun b h => h.elim Or.inr fun h => Or.inl (h.elim Or.inl fun e => Or.inr (Or.inr ⟨t, ht, e⟩))) + have wk15 : NtfnWaitKeepMA Mx Z.T N (L.mem s1) (L.mem s5) := ntfnWaitMA_compA wk wk45 (fun _ h => Or.inl h) + have hrepE : Sched.RepEndA σ1 τ := by + have act0 : σ0.schedulerAction = σ1.schedulerAction := by + rcases MD.clr with h | ⟨z, -, h⟩ <;> rw [h] <;> rfl + have ns4 : NoSwA σ4 := Sched.noSw_midA MD.keep act0 hsw (fun u e => (MD.nsw u e).2) + have nsf : NoSwA σf := fun u e => ns4 u (by rw [eσf] at e; exact e) + have E3 := Sched.sendIpc_recv_endA J.sr.place s4 hSt1 hV4 hdth hk1 hkt1 hkd1' hal true false hbg.bv g gr false + (Or.inr ⟨_, hx1f'⟩) hstd (hbr.imp id (Or.imp id (Or.imp id Or.inl))) SV1 RQ1 nsf hD1 + (show specPtr t ≠ σf.idleThread by rw [eσf]; exact fun h => hnI (h.trans hidle4)) + (show specPtr d ≠ σf.idleThread by rw [eσf]; exact fun h => hdI (h.trans hidle4)) _ hmem + have B2 : Sched.BLA σf τ := Sched.sendIpc_recv_blFA J.sr.place s4 hSt1 hV4 hdth hk1 hkt1 hkd1' hal true false + hbg.bv g gr false (Or.inr ⟨_, hx1f'⟩) _ hmem + have B1 : Sched.BLA σ4 σf := by rw [eσf]; exact Sched.BLA.tcbW hx4 + refine ⟨PreO.trans MD.bl (PreO.trans B1 B2), E3.1, fun y' hy' => ?_⟩ + have ne : σ1.curThread ≠ specPtr t := fun h => hcur h.symm + obtain ⟨z, hz, g'⟩ := K2.tcbB hy' + have hz4 : σ4.kheap σ1.curThread = some (.tcb z) := by rw [← far1 _ ne]; exact hz + obtain ⟨x0, hx0, e0⟩ := Sched.curSt_midA MD.keep (hσ0f _ ne) ne z hz4 + exact ⟨x0, hx0, (g'.st.resolve_right fun c => c.1.elim ne (fun h => hdc h.symm)).trans e0⟩ + refine ⟨⟨ha5, Q5, R5, stack', fun bxi sysl symL userOK j7 => ?_⟩, hrepE⟩ + exact J.of_ipcStepUXA (Q := Q5) (Rl := R5) (curOf := Z.curOf) (curScOf := Z.curScOf) wN HS SSz Qs hz + (fun _ _ => rfl) bxi sysl symL userOK j7 HQ.sched HE5 + (J.ntfn_ipcRFA wAll ht K15 HE5 wk15 hMs (fun u hu hm x' hx' => (afterM u hu hm x' hx').2)) RS5 + (fun q' hq' dead c hc hm => J.deadExt.reply q' hq' dead c hc (sub c hc q' hm)) (Fl4ProofsA64.P3.SIKXA.ipcKeepN K15) idle + (J.tw_ipcRFA wAll ht) sinv (K15.sidle J.sidle notM (J.sidle.rdyA J.sinv)) (J.runVcpu_ipcRFA wAll ht K15 HE5 hMv) + (J.blkQ_ipcMXA K15 HE5 wk15 afterM) SV5 + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3ReplyWA.lean b/proofs/Fl4ProofsA64/P3/Stage3ReplyWA.lean new file mode 100644 index 000000000..45cae9a2b --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ReplyWA.lean @@ -0,0 +1,134 @@ +/- +P3 (aarch64, A64-P3 Stage-3 integration, D8/T1 step 2): **the write set of the live Reply joint** and its +composition lemmas. + +`ReplyW N E R Bf Z m σ s` = `SchedStepNWA N E R Z m σ s ∨ BufW Bf`: the shape consumed by +`JStage3A.of_schedWrX` (`Stage3BufWrA`). The pieces of `do_reply_transfer` write + +* the removal / refill test: `ReplyFootW` (`replyFootW_wA`); +* the transfer and the receiver's `Running` write: `EWrite ∨ stA t ∨ action word` (`replyNormalW_wA`); +* the SC tail: `SWrA` (switch) or the release-queue footprint plus the reprogram word (`replyPostponeW_wA`). + +`ReplyW.trans` / `ReplyFootW.trans` move a later segment's footprint back to the entry state (the SC sizes are kept, +`SSzA`, and the kernel-state block is kept). + +No named premise. +-/ +import Fl4ProofsA64.P3.Stage3SignalFootA +import Fl4ProofsA64.P3.Stage3TimeoutFootA +import Fl4ProofsA64.P3.ReplyPopFootA +import Fl4ProofsA64.P3.IpcScPushA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4ProofsA64.P3.Sched + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + {N E R : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {Z : XSetsA} {m : Fl4SpecA64.McState} + +/-- **The write set of the live Reply joint** (module docstring). -/ +def ReplyW (N E R : Set U64) (Bf : Set (U64 × Fl4SpecA64.Word)) (Z : XSetsA) (m : Fl4SpecA64.McState) + (σ : Fl4SpecA64.State Unit) (s : S) (b : U64) : Prop := + SchedStepNWA (st := st) N E R Z m σ s b ∨ BufW Bf b + +theorem ReplyW.back {σ σ1 : Fl4SpecA64.State Unit} {s s1 : S} (hz : SSzA σ σ1) (hk : L.ksb s1 = L.ksb s) {b : U64} + (h : ReplyW (st := st) N E R Bf Z m σ1 s1 b) : ReplyW (st := st) N E R Bf Z m σ s b := + h.elim (fun h => Or.inl (schedStepN_backA hz hk h)) Or.inr + +theorem ReplyW.trans {σ σ1 : Fl4SpecA64.State Unit} {s s1 s2 : S} + (h1 : WritesIn st s s1 (ReplyW (st := st) N E R Bf Z m σ s)) (hz : SSzA σ σ1) + (h2 : WritesIn st s1 s2 (ReplyW (st := st) N E R Bf Z m σ1 s1)) : + WritesIn st s s2 (ReplyW (st := st) N E R Bf Z m σ s) := + (h1.trans h2).mono fun _ hb => hb.elim id (ReplyW.back hz h1.1) + +/-- A `ReplyFootW` word of a later state (SC sizes and `ksb` kept) is one of the earlier state. -/ +theorem ReplyFootW.back {Z' : XSetsA} (hC : Z'.C = Z.C) (hT : Z'.T = Z.T) {σ σ1 : Fl4SpecA64.State Unit} + {s s1 : S} (hz : SSzA σ σ1) (hk : L.ksb s1 = L.ksb s) {b : U64} + (h : ReplyFootW (st := st) Z' m σ1 s1 E R Bf b) : ReplyFootW (st := st) Z m σ s E R Bf b := by + rcases h with h | h | h + · rw [hT] at h; exact Or.inl h + · exact Or.inr (Or.inl (mwr_back hC hT hz hk h)) + · exact Or.inr (Or.inr h) + +theorem ReplyFootW.trans {Z' : XSetsA} (hC : Z'.C = Z.C) (hT : Z'.T = Z.T) {σ σ1 : Fl4SpecA64.State Unit} + {s s1 s2 : S} (h1 : WritesIn st s s1 (ReplyFootW (st := st) Z m σ s E R Bf)) (hz : SSzA σ σ1) + (h2 : WritesIn st s1 s2 (ReplyFootW (st := st) Z' m σ1 s1 E R Bf)) : + WritesIn st s s2 (ReplyFootW (st := st) Z m σ s E R Bf) := + (h1.trans h2).mono fun _ hb => hb.elim id (ReplyFootW.back hC hT hz h1.1) + +/-- A reply-removal word is a joint word. -/ +theorem replyFootW_wA {σ : Fl4SpecA64.State Unit} {s : S} (PE : EPlaced Z.T E R Bf) {b : U64} + (h : ReplyFootW (st := st) Z m σ s E R Bf b) : ReplyW (st := st) N E R Bf Z m σ s b := by + rcases h with h | h | ⟨q, hq, h | h⟩ + · exact ewrite_stepNA_buf PE.tcb h + · exact Or.inl (Or.inl (Or.inl h)) + · obtain ⟨-, v⟩ := replyNextPlaceA (Z := Z) PE hq + have ev := congrArg UScalar.val h + rw [v] at ev + exact Or.inl (Or.inr (Or.inr (Or.inr ⟨q, hq, by omega, by omega⟩))) + · obtain ⟨-, v⟩ := replyPrevPlaceA (Z := Z) PE hq + have ev := congrArg UScalar.val h + rw [v] at ev + exact Or.inl (Or.inr (Or.inr (Or.inr ⟨q, hq, by omega, by omega⟩))) + +/-- The TCB state word is a `TcbStepWA` word. -/ +theorem stA_tcbStepWA {T : Set U64} (hP : TcbPlaced T) {t : U64} (ht : t ∈ T) : TcbStepWA T (stA t) := by + have hfit := (hP t ht).fits + have hv : ∀ K : U64, K.val < 4096 → (wa t K).val = t.val + K.val := by + intro K hK; rw [wa_valm]; omega + have e : (stA t).val = t.val + 3328 := by + rw [stA_eq, hv 3328#u64 (by simp)] + simp + exact tcbStepWA_of_off hP ht 3328 (by omega) (by omega) (by omega) (by omega) e + +/-- The words of the transfer to the receiver and its `Running` write are joint words. -/ +theorem replyNormalW_wA {κ : U64 → U64} {σ : Fl4SpecA64.State Unit} {s : S} (PE : EPlaced Z.T E R Bf) + (H : SchedSRN st Z m κ s σ) {t : U64} (ht : t ∈ Z.T) {b : U64} + (h : EWrite Z.T E R Bf b ∨ b = stA t ∨ b = L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) : + ReplyW (st := st) N E R Bf Z m σ s b := by + rcases h with h | rfl | rfl + · exact ewrite_stepNA_buf PE.tcb h + · exact Or.inl (Or.inl (Or.inr (stA_tcbStepWA PE.tcb ht))) + · exact Or.inl (SchedSRN.act_stepNA H) + +/-- A write set over a structure-updated `Z` (same `C`, `T`) is one over `Z`. -/ +theorem ReplyW.zc {Z' : XSetsA} (hC : Z'.C = Z.C) (hT : Z'.T = Z.T) {σ : Fl4SpecA64.State Unit} {s : S} {b : U64} + (h : ReplyW (st := st) N E R Bf Z' m σ s b) : ReplyW (st := st) N E R Bf Z m σ s b := by + rcases h with ((h | h) | h) | h + · exact Or.inl (Or.inl (Or.inl (mwr_back hC hT (SSzA.refl σ) rfl h))) + · rw [hT] at h; exact Or.inl (Or.inl (Or.inr h)) + · exact Or.inl (Or.inr h) + · exact Or.inr h + +theorem ReplyW.transZ {Z' : XSetsA} (hC : Z'.C = Z.C) (hT : Z'.T = Z.T) {σ σ1 : Fl4SpecA64.State Unit} + {s s1 s2 : S} (h1 : WritesIn st s s1 (ReplyW (st := st) N E R Bf Z m σ s)) (hz : SSzA σ σ1) + (h2 : WritesIn st s1 s2 (ReplyW (st := st) N E R Bf Z' m σ1 s1)) : + WritesIn st s s2 (ReplyW (st := st) N E R Bf Z m σ s) := + ReplyW.trans h1 hz (h2.mono fun _ h => ReplyW.zc hC hT h) + +/-- `SWrA` words are joint words. -/ +theorem replySWrA_wA {σ : Fl4SpecA64.State Unit} {s : S} {b : U64} (h : SWrA (st := st) Z s b) : + ReplyW (st := st) N E R Bf Z m σ s b := + Or.inl (Or.inl (Or.inl (Or.inr h))) + +/-- The release-queue footprint of a postponed receiver (and the reprogram word) is in `SWrA`. -/ +theorem replyPostponeW_wA {κ : U64 → U64} {σ : Fl4SpecA64.State Unit} {s : S} (H : SchedSRN st Z m κ s σ) + {t : U64} (ht : t ∈ Z.T) {b : U64} + (h : L.QFootN s (t :: Z.R (L.cpu s)) (L.cpu s) RelOffs t b ∨ b = L.nodeAt s (L.cpu s) layout.node.REPROGRAM) : + ReplyW (st := st) N E R Bf Z m σ s b := by + refine replySWrA_wA ?_ + rcases h with h | rfl + · exact H.qfoot_swr (fun v hv => (List.mem_cons.1 hv).elim (· ▸ ht) (fun e => (H.relAct v e).1)) + H.sch.cpuCs ht h + · exact H.repr_swr + +end + +end + +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/Stage3ReplyWakeA.lean b/proofs/Fl4ProofsA64/P3/Stage3ReplyWakeA.lean new file mode 100644 index 000000000..7e76a97a3 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ReplyWakeA.lean @@ -0,0 +1,204 @@ +/- +P3 (aarch64, A64-P3 Stage 3, dispatch restore): **what a Reply leaves for the body end.** + +* **`RepEndA σ σ'`**: the budget frame (`BLA`), `SwA`, and the current thread's state kept. +* `WakeEndA.ofRep`: from the caller's `Restart` and a `RepEndA` step, `WakeEndA`. +* `replyLive_blA` / `replyFault_blA`: the budget frame across the reply removal, the refill test, the transfer (or the + fault reply) and the state write. +* **`replyTail_wakeA`**: the SC tail without the timeout send keeps the budget frame and leaves `SwA`, from a state + with no pending switch. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3IpcWakeA +import Fl4ProofsA64.P3.IpcScReplyTransferA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Classical + +noncomputable section + +/-- **What a Reply step leaves** (module docstring). -/ +structure RepEndA (σ σ' : State Unit) : Prop where + bl : BLA σ σ' + sw : SwA σ' + cur : ∀ y, σ'.kheap σ.curThread = some (.tcb y) → ∃ x, σ.kheap σ.curThread = some (.tcb x) ∧ y.tcbState = x.tcbState + +/-- **`WakeEndA` across the caller's `Restart` and a Reply** (module docstring). -/ +theorem WakeEndA.ofRep {σ σ' : State Unit} {x : Tcb} (hk : σ.kheap σ.curThread = some (.tcb x)) + (E : RepEndA { σ with kheap := Function.update σ.kheap σ.curThread (some (.tcb { x with tcbState := .restart })) } σ') : + WakeEndA σ σ' := by + refine ⟨PreO.trans (BLA.tcbW hk) E.bl, E.sw, fun _ y hy => Or.inr ?_⟩ + obtain ⟨z, hz, e⟩ := E.cur y hy + have hz' : Function.update σ.kheap σ.curThread (some (.tcb { x with tcbState := .restart })) σ.curThread = + some (.tcb z) := hz + rw [Function.update_self] at hz' + obtain rfl := KernelObject.tcb.inj (Option.some.inj hz') + exact e + +theorem postpone_noSwA (p : ObjRef) : HT NoSwA (postpone (Z := Unit) p) (fun _ => NoSwA) := by + have keep : ∀ σ σ' : State Unit, σ'.schedulerAction = σ.schedulerAction → NoSwA σ → NoSwA σ' := + fun σ σ' e h u c => h u (e ▸ c) + unfold postpone + refine HT.bind (Q := fun _ => NoSwA) (fun σ h r hr => + keep σ _ ((by unfold getScObjRef; tpres : TPres (getScObjRef (Z := Unit) SchedContext.scTcb p)) σ r hr).2.2.2.2.1 h) + fun o => ?_ + refine HT.bind (Q := fun _ => NoSwA) (HT.assertOpt _ fun a _ σ h => h) fun t => ?_ + refine HT.bind (Q := fun _ => NoSwA) (tcbSchedAction_keep _ t NoSwA fun σ g h => h) fun _ => ?_ + refine HT.bind (Q := fun _ => NoSwA) ?_ fun _ => HT.modify _ fun σ h => h + unfold tcbReleaseEnqueue + refine HT.bind (Q := fun _ => NoSwA) (HT.gets _ fun σ h => h) fun q => ?_ + refine HT.bind (Q := fun _ => NoSwA) ?_ fun q' => ?_ + · intro σ hσ r hr + unfold readyTimesAppend at hr + obtain ⟨hs, -⟩ := orderedInsert_spec _ _ _ _ hr + rw [hs]; exact hσ + refine HT.bind (Q := fun _ => NoSwA) ?_ fun _ => ?_ + · unfold setReleaseQueue + exact HT.modify _ fun σ h => h + · exact HT.when (fun _ => HT.modify _ fun σ h => h) (fun _ σ h => h) + +/-- **The SC tail without the timeout send** (module docstring). -/ +theorem replyTail_wakeA {σ σ' : State Unit} {t : ObjRef} {x : Tcb} {fault : Option Fault} + (hx : σ.kheap t = some (.tcb x)) (B : BoundActive t σ) (hns : NoSwA σ) (hto : ¬ ReplyTmoCondA σ t fault) + (h : ((), σ') ∈ (replyTailSpecA t fault σ).1) : BKeepA σ σ' ∧ SwA σ' := by + have same : σ' = σ → BKeepA σ σ' ∧ SwA σ' := fun e => by + rw [e]; exact ⟨PreO.refl σ, fun u c => absurd c (hns u)⟩ + unfold replyTailSpecA at h + obtain ⟨⟨o, s1⟩, a1, h⟩ := mem_bindA.1 h + unfold getTcbObjRef at a1 + obtain ⟨eo, es1⟩ := Prod.mk.inj (threadGet_valA Tcb.tcbSchedContext hx a1) + rw [es1] at h + simp only at h + obtain ⟨⟨ts, s2⟩, a2, h⟩ := mem_bindA.1 h + unfold getThreadState at a2 + obtain ⟨etst, es2⟩ := Prod.mk.inj (threadGet_valA Tcb.tcbState hx a2) + rw [es2] at h + simp only at h + unfold NonDet.when at h + split_ifs at h with hg + swap + · exact same (Prod.mk.inj (show ((), σ') = ((), σ) from h)).2 + cases o with + | none => exact absurd rfl hg.1 + | some p0 => + obtain ⟨⟨p, s3⟩, a3, h6⟩ := mem_bindA.1 h + have e3 : (p, s3) = (p0, σ) := a3 + obtain ⟨e3a, e3b⟩ := Prod.mk.inj e3 + rw [e3a, e3b] at h6 + simp only at h6 + obtain ⟨⟨y, s4⟩, a4, h7⟩ := mem_bindA.1 h6 + have e4 : s4 = σ := (getSchedContext_eqPres p0 σ _ a4).symm + obtain ⟨-, k, hk⟩ := getSchedContext_mem a4 + rw [e4] at h7 + simp only at h7 + obtain ⟨⟨now, s5⟩, a5, h8⟩ := mem_bindA.1 h7 + have e5 : s5 = σ := ((PresR.gets (R := @Eq (State Unit)) _) σ _ a5).symm + have enow : now = σ.curTime := (Prod.mk.inj (show (now, s5) = (σ.curTime, σ) from a5)).1 + rw [e5] at h8 + simp only at h8 + split_ifs at h8 with hr + · refine ⟨(show KPresB (possibleSwitchTo (Z := Unit) t) from KPresBInst.pres) σ _ h8, ?_⟩ + refine possibleSwitchTo_swRunA t σ ⟨fun u c => absurd c (hns u), fun z hz => ?_, B⟩ _ h8 + rw [hx] at hz + obtain rfl := KernelObject.tcb.inj (Option.some.inj hz) + rw [← etst]; exact hg.2 + · unfold replyLateSpecA at h8 + obtain ⟨⟨tc, s6⟩, a6, h9⟩ := mem_bindA.1 h8 + have e6 : (tc, s6) = (x, σ) := by + have hg4 : getTcb t σ = some x := by simp [getTcb, readObject, hx] + have := a6 + unfold NonDet.getsThe at this + rw [hg4] at this + exact this + obtain ⟨e6a, e6b⟩ := Prod.mk.inj e6 + rw [e6a, e6b] at h9 + simp only at h9 + rw [if_neg (fun hc => hto ⟨x, p0, y, k, hx, eo.symm, etst ▸ hg.2, hk, enow ▸ hr, hc.1, hc.2⟩)] at h9 + refine ⟨(show KPresB (postpone (Z := Unit) p0) from KPresBInst.pres) σ _ h9, fun u c => ?_⟩ + exact absurd c (postpone_noSwA p0 σ hns _ h9 u) + +/-- No pending switch at the state reached by moving only the caller `t`, which is not the switch target. -/ +theorem noSw_midA {σ σ0 σ4 : State Unit} {i t : ObjRef} {M : ObjRef → Prop} {don : Bool} + (S : SIKA i M (· = t) don σ0 σ4) (hact : σ0.schedulerAction = σ.schedulerAction) (hsw : NoSwA σ) + (hnt : ∀ u, σ4.schedulerAction = .switchThread u → u ≠ t) : NoSwA σ4 := fun u e => by + rcases S.act with a | a | ⟨y, hy, a⟩ + · rw [a, hact] at e; exact hsw u e + · rw [a] at e; cases e + · have e' := e + rw [a] at e' + injection e' with e' + exact hnt u e (e' ▸ hy) + +/-- A thread other than the movers keeps its state. -/ +theorem curSt_midA {σ σ0 σ4 : State Unit} {i c : ObjRef} {M N : ObjRef → Prop} {don : Bool} + (S : SIKA i M N don σ0 σ4) (hk : σ0.kheap c = σ.kheap c) (hM : ¬ M c) (y : Tcb) + (hy : σ4.kheap c = some (.tcb y)) : ∃ x, σ.kheap c = some (.tcb x) ∧ y.tcbState = x.tcbState := by + obtain ⟨z, hz, g⟩ := S.tcbB hy + rw [hk] at hz + exact ⟨z, hz, g.st.resolve_right fun h => hM h.1⟩ + +/-- **`RepEndA` of a Reply without the timeout send**: the prefix keeps the budget frame and moves only the caller +`t`, which is not the pending switch target; then the SC tail. -/ +theorem RepEndA.ofTail {σ σ0 σ4 σ5 : State Unit} {i t : ObjRef} {don : Bool} {x : Tcb} {fault : Option Fault} + (S : SIKA i (· = t) (· = t) don σ0 σ4) (hact : σ0.schedulerAction = σ.schedulerAction) + (hcurk : σ0.kheap σ.curThread = σ.kheap σ.curThread) (hcur : t ≠ σ.curThread) (hsw : NoSwA σ) + (hnt : ∀ u, σ4.schedulerAction = .switchThread u → u ≠ t) (bl : BLA σ σ4) + (hx : σ4.kheap t = some (.tcb x)) (B : BoundActive t σ4) (hto : ¬ ReplyTmoCondA σ4 t fault) + (h : ((), σ5) ∈ (replyTailSpecA t fault σ4).1) (hk : σ5.kheap = σ4.kheap) : RepEndA σ σ5 := by + have ns : NoSwA σ4 := noSw_midA S hact hsw hnt + obtain ⟨K, sw⟩ := replyTail_wakeA hx B ns hto h + refine ⟨PreO.trans bl K.toBLA, sw, fun y hy => ?_⟩ + rw [hk] at hy + exact curSt_midA S hcurk (fun c => hcur c.symm) y hy + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **The budget frame across the live Reply up to the SC tail** (module docstring). -/ +theorem replyLive_blA {th : TypedHeapA} {kb : U64} (hPl : PlaceA th kb) (sf : S) + {σ σ1 σ2 σ3 σ4 : State Unit} {sender t r : U64} {grant : Bool} + (h1 : ((), σ1) ∈ (replyRemove (Z := Unit) (specPtr t) (specPtr r) σ).1) + (h2 : ((), σ2) ∈ ((getTcbObjRef (Z := Unit) Tcb.tcbSchedContext (specPtr t) >>= + ifSporadicCurScTestRefillUnblockCheck) σ1).1) + (hSt : StateRelA th (L.mem sf) σ2) (hSV : SlotsValidA th (L.mem sf)) (htr : th t = some .tcb) + {xs y2 : Tcb} (hks : σ2.kheap (specPtr sender) = some (.tcb xs)) + (hky : σ2.kheap (specPtr t) = some (.tcb y2)) (hfs : xs.tcbFault = none) + (hcaps : grant = false ∨ ipcBufOfA false xs = none ∨ + unat (dataToMessageInfo (xs.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0) + (hal : ∀ rb, ipcBufOfA true y2 = some rb → (bufAddr rb 0).toNat % 8 = 0) + (h3 : ((), σ3) ∈ (doIpcTransfer (Z := Unit) (specPtr sender) none (0 : Badge) grant (specPtr t) σ2).1) + (h4 : ((), σ4) ∈ (setThreadState (Z := Unit) (specPtr t) .running σ3).1) : BLA σ σ4 := by + have B1 : BKeepA σ σ1 := + (show KPresB (replyRemove (Z := Unit) (specPtr t) (specPtr r)) from KPresBInst.pres) σ _ h1 + have B2 : BKeepA σ1 σ2 := (show KPresB (getTcbObjRef (Z := Unit) Tcb.tcbSchedContext (specPtr t) >>= + ifSporadicCurScTestRefillUnblockCheck) by kbres) σ1 _ h2 + have B3 : BLA σ2 σ3 := (doIpcTransfer_blA hPl sf hSt hSV htr hks hky hfs hal none 0 grant hcaps σ2 rfl _ h3).2 + have B4 : BKeepA σ3 σ4 := + (show KPresB (setThreadState (Z := Unit) (specPtr t) .running) from KPresBInst.pres) σ3 _ h4 + exact PreO.trans B1.toBLA (PreO.trans B2.toBLA (PreO.trans B3 B4.toBLA)) + +end + +/-- **The budget frame across the faulted Reply up to the SC tail** (module docstring). -/ +theorem replyFault_blA {σ σ1 σ2 σ3 σ3c σ4 : State Unit} {t r : ObjRef} + (h1 : ((), σ1) ∈ (replyRemove (Z := Unit) t r σ).1) + (h2 : ((), σ2) ∈ ((getTcbObjRef (Z := Unit) Tcb.tcbSchedContext t >>= + ifSporadicCurScTestRefillUnblockCheck) σ1).1) + {uc : UserContext} + (h3 : ((), σ3) ∈ (asUser (Z := Unit) t (fun _ => ({((), uc)}, False)) σ2).1) + (h3c : ((), σ3c) ∈ (threadSet (Z := Unit) (fun tcb => { tcb with tcbFault := none }) t σ3).1) + {ts : ThreadState} (h4 : ((), σ4) ∈ (setThreadState (Z := Unit) t ts σ3c).1) : BLA σ σ4 := by + have B1 : BKeepA σ σ1 := (show KPresB (replyRemove (Z := Unit) t r) from KPresBInst.pres) σ _ h1 + have B2 : BKeepA σ1 σ2 := (show KPresB (getTcbObjRef (Z := Unit) Tcb.tcbSchedContext t >>= + ifSporadicCurScTestRefillUnblockCheck) by kbres) σ1 _ h2 + have B3 : BKeepA σ2 σ3 := asUser_bkA t _ σ2 _ h3 + have B3c : BKeepA σ3 σ3c := threadSet_bkA _ t σ3 _ h3c + have B4 : BKeepA σ3c σ4 := (show KPresB (setThreadState (Z := Unit) t ts) from KPresBInst.pres) σ3c _ h4 + exact (PreO.trans B1 (PreO.trans B2 (PreO.trans B3 (PreO.trans B3c B4)))).toBLA + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3RestartA.lean b/proofs/Fl4ProofsA64/P3/Stage3RestartA.lean new file mode 100644 index 000000000..5055c0b6b --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3RestartA.lean @@ -0,0 +1,382 @@ +/- +P3 (aarch64, A64-P3 Stage-3 integration, D8 / Untyped `hready`): **`JStage3A` across the caller's `set_thread_state +Restart`**. + +`JEntA.restartA` (`P3/RestartA`) is the one concrete write `L.wr s (stA cur) TS_RESTART` (FL4) against `stUpdA σ cur x +.restart` (the spec). **`JStage3A.of_restartCurA`** carries the whole unified record across it, through +`JStage3A.of_schedWr` (the write is a `TcbStepWA` word): the scheduler relation by `sc_set_runnable_stateA`, the +endpoint / notification relations by `restart_ipc_viewsA` (a runnable caller is in no queue), the reply stacks, +buffer coverage, `mcp`, yield and idle-fault relations by the words and objects the write does not touch +(`StOffA.fld`), `J5A` by `MdbInvA.keepO` and the frame lemmas of `OSKeep`, `J7A` with `S1aR2.keep`. + +Hypotheses: those of `JEntA.restartA` (the caller's `SysPlace`, `SymL`, runnable current thread with its SC, `hvs` / `hvc`). +No named premise. +-/ +import Fl4ProofsA64.P3.InvokeRestartJointA +import Fl4ProofsA64.P3.Stage3SchedWrA +import Fl4ProofsA64.P3.Stage3ReplyWA +import Fl4ProofsA64.P3.Stage3J5A +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4SpecA64 Sched Fl4ProofsA64.P3.Endp + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-- (record edit #8) A thread-state write keeps the queue invariants' ingredients. -/ +theorem QKeepA.of_stUpdA {σ : Fl4SpecA64.State Unit} {t : ObjRef} {x : Tcb} {ts : ThreadState} + (hk : σ.kheap t = some (.tcb x)) : QKeepA σ (stUpdA σ t x ts) := by + refine ⟨fun p y hy => ?_, fun p y hp => ⟨y, stUpd_back hp (fun _ e => by cases e), List.Sublist.refl _⟩, + fun p y hp => ⟨y, stUpd_back hp (fun _ e => by cases e), List.Sublist.refl _⟩, + fun p y k hp => ⟨y, k, stUpd_back hp (fun _ e => by cases e), fun r => ⟨r, id⟩⟩, + fun p y hp => ⟨y, stUpd_back hp (fun _ e => by cases e), id⟩, ⟨rfl, rfl, rfl, rfl⟩⟩ + by_cases e : p = t + · subst e + have h2 : (stUpdA σ p x ts).kheap p = some (.tcb { x with tcbState := ts }) := by simp [setObjA] + rw [h2] at hy + cases hy + exact ⟨x, hk, rfl⟩ + · exact ⟨y, by simpa [setObjA, Function.update_of_ne e] using hy, rfl⟩ + +/-- **F3–F6 across one thread-state write** to a thread that is no core's idle thread. -/ +theorem SIdleA.stUpdA {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} {n : Nat} (h : SIdleA σ m n) + {t : ObjRef} {x : Tcb} {ts : ThreadState} (hk : σ.kheap t = some (.tcb x)) + (hne : ∀ c < n, m.idleThreadOf c ≠ t) : SIdleA (stUpdA σ t x ts) m n where + f3 := fun c hc => by + obtain ⟨y, hy, hs⟩ := h.f3 c hc + exact ⟨y, by simp [stUpdA, setObjA, Function.update_of_ne (hne c hc), hy], hs⟩ + f4 := fun y k hy => h.f4 y k (stUpd_back hy (fun _ e => by cases e)) + f5 := fun p y k hy he => h.f5 p y k (stUpd_back hy (fun _ e => by cases e)) he + f6 := h.f6.of_eq fun sl => by + unfold specCapAtA + by_cases e : sl.1 = t + · rw [e] + simp only [stUpdA, setObjA, Function.update_self] + rw [hk] + try rfl + · simp only [stUpdA, setObjA, Function.update_of_ne e] + f7 := fun y k hy => h.f7 y k (stUpd_back hy (fun _ e => by cases e)) + idleSlot := fun c hc => (idleVtSameA (by simp [stUpdA, setObjA, Function.update_of_ne (hne c hc)]) + (h.idleSlot c hc) (h.idleBound c hc)).1 + idleBound := fun c hc => (idleVtSameA (by simp [stUpdA, setObjA, Function.update_of_ne (hne c hc)]) + (h.idleSlot c hc) (h.idleBound c hc)).2 + +/-- **`of_restartCurA`'s `hnvm` at a kernel entry** (no [J]): `SActA.f2` (the current thread is running, restarting, +inactive or idle) and `EpStateRel`'s state code. -/ +theorem JStage3EntryA.curNotVmA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J5 J7 : JSlotA S} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} + (J : JStage3EntryA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) : + L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM := by + obtain ⟨x, hx, hs⟩ := J.sact.f2 + have ct : σ.curThread = specPtr (L.curW s) := J.j.1.sch.curThread + have code := (J.ep.at_tcb J.sr.curT (ct ▸ hx)).st + intro e + rw [e] at code + have h8 : (sched.TS_RUNNING_VM : U64).val = 8 := by unfold sched.TS_RUNNING_VM; rfl + rw [h8] at code + rcases hs with h | h | h | h <;> rw [h] at code <;> exact absurd code (by decide) + +set_option maxHeartbeats 2000000 in +/-- **`JStage3A` across a Restart write of the current thread, from `JEntA.restartCoreA`'s outcome**. -/ +theorem JStage3A.of_restartCoreA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {ar : machine.Arch A} {nA : U64} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (hrun : Runnable (L.mem s (stA (L.curW s)))) + (hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM) + (hvc : ∀ v ∈ Z.V, stA (L.curW s) ≠ v ∧ stA (L.curW s) ≠ wa v 24#u64) + (hres : ∃ x, σ.kheap σ.curThread = some (.tcb x) ∧ + sched.set_thread_state st s (L.curW s) sched.TS_RESTART = ok (L.wr s (stA (L.curW s)) sched.TS_RESTART) ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie (L.wr s (stA (L.curW s)) sched.TS_RESTART) a + (stUpdA σ σ.curThread x .restart) m ∧ + SymL st H Z.Cs (L.wr s (stA (L.curW s)) sched.TS_RESTART) ∧ + OSKeep st OS s (L.wr s (stA (L.curW s)) sched.TS_RESTART) ∧ + (∀ y, SlotWA Z.th y → capAtA (L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART)) y = capAtA (L.mem s) y) ∧ + (∀ n, SInvXA σ m n → SInvXA (stUpdA σ σ.curThread x .restart) m n)) : + ∃ x, σ.kheap σ.curThread = some (.tcb x) ∧ + sched.set_thread_state st s (L.curW s) sched.TS_RESTART = ok (L.wr s (stA (L.curW s)) sched.TS_RESTART) ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) + (L.wr s (stA (L.curW s)) sched.TS_RESTART) a (stUpdA σ σ.curThread x .restart) m ∧ + SymL st H Z.Cs (L.wr s (stA (L.curW s)) sched.TS_RESTART) ∧ + OSKeep st OS s (L.wr s (stA (L.curW s)) sched.TS_RESTART) ∧ + (∀ y, SlotWA Z.th y → capAtA (L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART)) y = capAtA (L.mem s) y) := by + have Je := J.toJEntA + obtain ⟨x, hk, heq, hset, E1, Y1, hOS1, hcap, hinv⟩ := hres + refine ⟨x, hk, heq, hset, ?_, Y1, hOS1, hcap⟩ + have ct : σ.curThread = specPtr (L.curW s) := J.j.1.sch.curThread + have htZ : L.curW s ∈ Z.T := J.sr.curT + have hta : TcbAddr (L.curW s) := J.sr.core.tcbs _ htZ + have O := Je.stOffA hvc + have sA : Aligned (stA (L.curW s)) := by rw [stA_eq]; exact field_aligned hta (by simp) (by simp) + have hx : σ.kheap (specPtr (L.curW s)) = some (.tcb x) := ct ▸ hk + have states : StEqA Z.T (L.mem s) σ := fun u hu y hy => (J.ep.at_tcb hu hy).st + have hr : runnable x.tcbState = true := (runnable_iffA (states _ htZ x hx)).1 hrun + have code : sched.TS_RESTART.val = tsCodeA .restart := by rw [ts_vals.2.1]; rfl + have result := sc_set_runnable_stateA J.sr states htZ hx hr (ts := .restart) rfl code + rw [heq, WP.spec_ok] at result + obtain ⟨τ, schedStep, SR1, states1, -, -⟩ := result + have eqτ : τ = stUpdA σ σ.curThread x .restart := by + rw [← ct, hset] at schedStep + exact (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst schedStep))).2.symm + subst eqτ + -- the spec states + have kT : ∀ p y, σ.kheap p = some (.tcb y) → ∃ q, (stUpdA σ σ.curThread x .restart).kheap p = + some (.tcb { y with tcbState := q }) := by + intro p y hy + by_cases e : p = σ.curThread + · subst e; rw [hk] at hy + obtain rfl := KernelObject.tcb.inj (Option.some.inj hy) + exact ⟨.restart, by simp [stUpdA, setObjA]⟩ + · exact ⟨y.tcbState, by simp [stUpdA, setObjA, Function.update_of_ne e, hy]⟩ + have kB : ∀ p y1, (stUpdA σ σ.curThread x .restart).kheap p = some (.tcb y1) → + ∃ y q, σ.kheap p = some (.tcb y) ∧ y1 = { y with tcbState := q } := by + intro p y1 hy + by_cases e : p = σ.curThread + · subst e + simp [stUpdA, setObjA] at hy + exact ⟨x, .restart, hk, hy.symm⟩ + · simp [stUpdA, setObjA, Function.update_of_ne e] at hy + exact ⟨y1, y1.tcbState, hy, by cases y1; rfl⟩ + have HS : SpecSchedA σ (stUpdA σ σ.curThread x .restart) := + { kh := fun p => by + by_cases e : p = σ.curThread + · right; left + subst e + exact ⟨x, { x with tcbState := .restart }, hk, by simp [stUpdA, setObjA], ⟨rfl, rfl, rfl⟩⟩ + · left; simp [stUpdA, setObjA, Function.update_of_ne e] + irqS := rfl + irqN := rfl + asid := rfl + vcur := rfl + cdt := rfl + orig := rfl } + have SSz : SSzA σ (stUpdA σ σ.curThread x .restart) := fun p y n h => + ⟨y, stUpd_back h (fun _ e => by cases e)⟩ + have scCov : ScCovA Z.C m (stUpdA σ σ.curThread x .restart) := fun p y n h => + J.scCov p y n (stUpd_back h (fun _ e => by cases e)) + have idleNoFault : IdleNoFaultA m (stUpdA σ σ.curThread x .restart) := fun c hc => by + obtain ⟨y, hy, hf⟩ := J.idleNoFault c hc + obtain ⟨q, hq⟩ := kT _ y hy + exact ⟨_, hq, hf⟩ + have views : EpStateRel Z.T E Rp Bf (L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART)) + (stUpdA σ σ.curThread x .restart) ∧ + Notif.NtfnStateRel N Z.T (L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART)) (stUpdA σ σ.curThread x .restart) := by + rw [ct] + exact restart_ipc_viewsA J.ep J.iplaced.e J.ntfn J.iplaced.n htZ hx hr + have tw : ∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → K.val ≠ 3328 → + L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART) (wa u K) = L.mem s (wa u K) := by + intro u hu K h1 h2 h3 + exact mem_wr_ne' sA (field_aligned (J.sr.core.tcbs u hu) h1 h2) (O.fld u hu K h1 (fun _ => h3)).symm + have mcp : McpRelA Z.T m (L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART)) (stUpdA σ σ.curThread x .restart) := + fun u hu => by + obtain ⟨y, hy, e, hf⟩ := J.mcp u hu + obtain ⟨q, hq⟩ := kT _ y hy + refine ⟨_, hq, ?_, fun g => (hf g).keep ?_ rfl⟩ + · unfold mcpA at e ⊢ + rw [tw u hu _ (by simp) (by simp) (by simp)] + exact e + · unfold flagsA + exact tw u hu _ (by simp) (by simp) (by simp) + have ye : ∀ u ∈ Z.T, L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART) (yieldA u) = L.mem s (yieldA u) := + fun u hu => by unfold yieldA; exact tw u hu _ (by simp) (by simp) (by simp) + have yieldRel : YieldRelA Z.T Z.C m (L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART)) + (stUpdA σ σ.curThread x .restart) := by + refine ⟨fun u hu => ?_, fun u hu h => ?_, fun c hcc y k h p hp => ?_⟩ + · obtain ⟨y, hy, e⟩ := J.yieldRel.tcb u hu + obtain ⟨q, hq⟩ := kT _ y hy + exact ⟨_, hq, by rw [ye u hu]; exact e⟩ + · rw [ye u hu] at h ⊢ + obtain ⟨hC, y, k, hy, e⟩ := J.yieldRel.toSc u hu h + exact ⟨hC, y, k, stUpd_keep hk hy (fun _ e => by cases e), e⟩ + · obtain ⟨u, hu, e1, e2⟩ := J.yieldRel.frm c hcc y k (stUpd_back h (fun _ e => by cases e)) p hp + exact ⟨u, hu, e1, by rw [ye u hu]; exact e2⟩ + have reply : ReplyStackRelA (L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART)) Rp Z.C + (stUpdA σ σ.curThread x .restart) (scNameA m) stack := by + refine J.reply.frame (fun r hr => ?_) (fun c _ y k h => ⟨y, k, stUpd_keep hk h (fun _ e => by cases e), rfl⟩) + (fun r _ x' h => ⟨x', stUpd_back h (fun _ e => by cases e), rfl⟩) + have hr3 := J.iplaced.e.rep r hr + have hdis := J.iplaced.e.rt r hr (L.curW s) htZ + have key : ∀ o : U64, o.val = 8 ∨ o.val = 16 → + L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART) (wa r o) = L.mem s (wa r o) := by + intro o ho + have hv : (wa r o).val = r.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + refine mem_wr_ne' sA (by unfold Aligned; rw [hv]; omega) ?_ + intro e + have ev := congrArg UScalar.val e + rw [stA_eq, Notif.tfield_val hta (by decide : (3328#u64 : U64).val < 4096), hv] at ev + simp at ev + omega + exact ⟨key _ (Or.inr (by simp [layout.reply.NEXT])), key _ (Or.inl (by simp [layout.reply.PREV]))⟩ + have bufCover : BufCover st Z.T Bf (L.wr s (stA (L.curW s)) sched.TS_RESTART) + (stUpdA σ σ.curThread x .restart) := by + intro u hu y b sb v hy hb hl + obtain ⟨y0, q, hy0, rfl⟩ := kB _ y hy + have hib : IbSame (L := L) s (L.wr s (stA (L.curW s)) sched.TS_RESTART) u := by + refine ⟨tw u hu _ (by simp) (by simp) (by simp), ?_, ?_⟩ + · rw [sbWordA_eq]; exact tw u hu _ (by simp) (by simp) (by simp) + · rw [sbWordA8_eq]; exact tw u hu _ (by simp) (by simp) (by simp) + rw [lookup_ipc_buffer_sameA hib] at hl + exact J.bufCover u hu y0 b sb v hy0 hb hl + have osk : OSKeep st OS s (L.wr s (stA (L.curW s)) sched.TS_RESTART) := hOS1 + have kb : L.ksb (L.wr s (stA (L.curW s)) sched.TS_RESTART) = L.ksb s := L.ksb_wr _ _ _ + have hTr : TreeA st OS Z.G.chains Z.G.par s := + J.j.1.cdt.tree.grow J.j5.2.1 (fun x hx hn => J.j5.2.2 x hx hn) + have j5 : J5A st Z H OS (L.wr s (stA (L.curW s)) sched.TS_RESTART) (stUpdA σ σ.curThread x .restart) m := + ⟨⟨MdbInvA.keepO hTr.lk hTr.tree J.j5.1.1 osk, IrqRunA.keepO hTr.lk J.j5.1.2.1 osk, + HandlersActiveA.keepO hTr.lk J.j5.1.2.2 osk kb (fun i hi => by + have eI : irqStAddrA (L.ksb s) i = irqWordA (L.ksb s) i := rfl + have hv := irqWordA_val J.sr.ksFit hi + have hal : Aligned (irqWordA (L.ksb s) i) := by + have := J.sr.toSchedSRN.ksb_al + show _ % 8 = 0 + omega + rw [eI] + exact mem_wr_ne' sA hal (O.om _ (Or.inr (Or.inr (Or.inl ⟨i, hi, rfl⟩)))).symm)⟩, J.j5.2.1, + fun y hy hn => by rw [osk.capA hy]; exact J.j5.2.2 y hy hn⟩ + have w : WritesIn st s (L.wr s (stA (L.curW s)) sched.TS_RESTART) (SchedStepWA (st := st) Z m σ s) := + (writesIn_wr sA (v := sched.TS_RESTART)).mono fun b hb => by + subst hb; exact Or.inr (stA_tcbStepWA (fun t ht => J.sr.core.tcbs t ht) htZ) + have j7 : J7A st (J7R2A st ar nA) Z H OS (L.wr s (stA (L.curW s)) sched.TS_RESTART) + (stUpdA σ σ.curThread x .restart) m := by + obtain ⟨Pt, Ub, vr, pn, arm, cc, vcc, dfa, hi, link, vcw, fu, ual, vcu, Pp', Tb', Hr, Te, Kt, Lv', Re, bits, R2, Kut, + KLv, ktK, hik⟩ := J.j7 + have vsr : VsKeep st (L.ksb s) Pt s (L.wr s (stA (L.curW s)) sched.TS_RESTART) := fun b hb => by + refine w.mem_eq (vr.al _ hb) ?_ + by_cases h : AsW (L.ksb s) b + · obtain ⟨o, o1, o2, -, e⟩ := h + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.sys.ent.ksw + have ev : b.val = (L.ksb s).val + o.val := by + rw [e, wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact J.asid_offStepA (by omega) (by omega) + · exact J.vsr_offStepA vr hb h + have tab : TabKeep st s (L.wr s (stA (L.curW s)) sched.TS_RESTART) := + ⟨kb, fun i => by rw [kb]; exact vsr _ (Or.inr (asW_at _ i))⟩ + refine ⟨Pt, Ub, by rw [kb]; exact vr, PoolNamed.ofTab kb (fun j => tab.2 j) osk pn, S1aArmCol.ofTab tab arm, + S1aCapCol.ofOSK osk cc, CapInv.keepO vcc osk, CapInv.keepO dfa osk, + HiCap511.keep hi (fun y hy => osk.capA hy) (fun y hy p m a e => ?_), fun p hp => link p ?_, + by rw [kb]; exact vcw, FrUb.keepO fu osk, ual, vcu, Pp', Tb', Hr, Te, Kt, Lv', Re, bits, + R2.ofImage kb vsr (S1aSlotImage.ofSame R2.capRefs.refs fun _ hy => Or.inl (osk.capA hy)), Kut.keep osk, KLv, ktK, + S1aHiK.keep hik ⟨EdgeKeepOn.ofVsKeep R2 vsr, fun y hy R m a c => ⟨y, hy, m, a, by rwa [osk.capA hy] at c⟩⟩⟩ + · have live : p ∈ Hr := by rw [R2.highRoots]; exact ⟨y, hy, m, a, e⟩ + exact vsr _ (Or.inl ⟨p, R2.place.tables (R2.place.high.sub live), 4088#u64, by decide, by decide, rfl⟩) + · unfold SchedPageA at hp ⊢ + rcases hp with hp | hp | hp | hp | hp | hp | hp | ⟨u, hu, hp⟩ + · exact Or.inl hp + · exact Or.inr (Or.inl hp) + · exact Or.inr (Or.inr (Or.inl hp)) + · exact Or.inr (Or.inr (Or.inr (Or.inl hp))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inl hp)))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl hp))))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl hp)))))) + · rw [osk.capA hu] at hp + exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ⟨u, hu, hp⟩)))))) + have sidle : SIdleA (stUpdA σ σ.curThread x .restart) m n := by + refine J.sidle.stUpdA hk (fun c hc e => ?_) + obtain ⟨y, hy, hs⟩ := J.sidle.f3 c hc + rw [e, hk] at hy + have hxy := KernelObject.tcb.inj (Option.some.inj hy) + subst hxy + rw [hs] at hr + exact absurd hr (by decide) + have bufAl : UtBufAlA Z.T (stUpdA σ σ.curThread x .restart) := fun t ht y hy => by + obtain ⟨y0, q, hy0, rfl⟩ := kB _ y hy + exact J.bufAl t ht y0 hy0 + have rv0 : L.mem s (runVcpuA (L.curW s)) = 0#u64 := J.runVcpu.zero htZ hnvm + have rvw : ∀ u ∈ Z.T, L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART) (runVcpuA u) = L.mem s (runVcpuA u) := + fun u hu => tw u hu 3672#u64 (by simp) (by simp) (by simp) + have runVcpu : RunVcpuInv st Z.T (L.wr s (stA (L.curW s)) sched.TS_RESTART) := fun u hu h0 => by + rw [rvw u hu] at h0 + have ne : u ≠ L.curW s := fun e => h0 (e ▸ rv0) + have hs : L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART) (stA u) = L.mem s (stA u) := by + refine mem_wr_ne' sA (by rw [stA_eq]; exact field_aligned (J.sr.core.tcbs u hu) (by simp) (by simp)) ?_ + intro e + have ev := congrArg UScalar.val e + rw [stA_eq, stA_eq, Notif.tfield_val hta (by decide : (3328#u64 : U64).val < 4096), + Notif.tfield_val (J.sr.core.tcbs u hu) (by decide : (3328#u64 : U64).val < 4096)] at ev + exact ne (UScalar.eq_of_val_eq (by omega)) + rw [hs] + exact J.runVcpu u hu h0 + have blkQ : BlkQueuedA Z.T E N (L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART)) + (stUpdA σ σ.curThread x .restart) := + J.blkQ.keepA (fun u hu => tw u hu 3336#u64 (by simp) (by simp) (by simp)) + (fun u _ x' h => by + by_cases e : specPtr u = σ.curThread + · left + rw [e] at h + simp [stUpdA, setObjA] at h + rw [← h] + · exact Or.inr ⟨x', by simpa [stUpdA, setObjA, Function.update_of_ne e] using h, rfl⟩) + (fun p y h => stUpd_keep hk h (fun _ e => by cases e)) (fun p y h => stUpd_keep hk h (fun _ e => by cases e)) + have sv' : SchedValidA (stUpdA σ σ.curThread x .restart) := by + have same : ∀ p, p ≠ σ.curThread → (stUpdA σ σ.curThread x .restart).kheap p = σ.kheap p := fun p e => by + simp [stUpdA, setObjA, Function.update_of_ne e] + have atct : (stUpdA σ σ.curThread x .restart).kheap σ.curThread = some (.tcb { x with tcbState := .restart }) := by + simp [stUpdA, setObjA] + have sc : ∀ p, ScActiveAtA σ p → ScActiveAtA (stUpdA σ σ.curThread x .restart) p := fun p ⟨y, k, e, act⟩ => + ⟨y, k, by rw [same p fun h => (by rw [h, hk] at e; cases e)]; exact e, act⟩ + have tcbAct : ∀ t, (∃ y c, σ.kheap t = some (.tcb y) ∧ y.tcbSchedContext = some c ∧ ScActiveAtA σ c) → + ∃ y c, (stUpdA σ σ.curThread x .restart).kheap t = some (.tcb y) ∧ y.tcbSchedContext = some c ∧ + ScActiveAtA (stUpdA σ σ.curThread x .restart) c := fun t ⟨y, c, e, es, act⟩ => by + by_cases h : t = σ.curThread + · subst h + rw [hk] at e + obtain rfl := KernelObject.tcb.inj (Option.some.inj e) + exact ⟨_, c, atct, es, sc _ act⟩ + · exact ⟨y, c, by rw [same t h]; exact e, es, sc _ act⟩ + refine ⟨fun d p t hm => ?_, fun e1 e2 => ?_, fun t x' c e1 e2 e3 => ?_, fun t ht => ?_⟩ + · obtain ⟨a, nr⟩ := J.schedValid.rq d p t hm + exact ⟨tcbAct t a, nr⟩ + · obtain ⟨act, nr⟩ := J.schedValid.act e1 e2 + exact ⟨tcbAct _ act, nr⟩ + rotate_left + · exact tcbAct t (J.schedValid.rls t ht) + · by_cases h : t = σ.curThread + · subst h + rw [atct] at e1 + obtain rfl := KernelObject.tcb.inj (Option.some.inj e1) + simp [BlockedTsA] at e2 + · rw [same t h] at e1 + exact sc _ (J.schedValid.blk t x' c e1 e2 e3) + exact JStage3A.of_schedWr (symL := Y1) (schedValid := sv') J (Q := Z.Q) (Rl := Z.R) (curOf := Z.curOf) (curScOf := Z.curScOf) w HS SSz (QKeepA.of_stUpdA hk) rfl + J.j.1.ar (fun _ _ => rfl) bufAl E1.sys E1.sq E1.poolOff SR1 scCov views.1 views.2 reply bufCover idleNoFault mcp + yieldRel (hinv n J.sinv) sidle runVcpu blkQ j5 j7 + +/-- **`JStage3A` across the caller's Restart write** (module docstring). -/ +theorem JStage3A.of_restartCurA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {ar : machine.Arch A} {nA : U64} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {Rq Nt : Set U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} {slot : U64} + (P : SysPlace st H Z.Cs H.frame Rq Nt OS s extra slot) (Y : SymL st H Z.Cs s) + (hrun : Runnable (L.mem s (stA (L.curW s)))) + (hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM) + (hcur : (L.mem s (inqA (L.curW s)) &&& sched.Q_RELEASE) = 0#u64 ∧ L.mem s (scA (L.curW s)) ≠ 0#u64 ∧ + L.mem s (scA (L.curW s)) ∈ Ac) + (hvs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → Aligned b ∧ b ≠ stA (L.curW s)) + (hvc : ∀ v ∈ Z.V, stA (L.curW s) ≠ v ∧ stA (L.curW s) ≠ wa v 24#u64) : + ∃ x, σ.kheap σ.curThread = some (.tcb x) ∧ + sched.set_thread_state st s (L.curW s) sched.TS_RESTART = ok (L.wr s (stA (L.curW s)) sched.TS_RESTART) ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) + (L.wr s (stA (L.curW s)) sched.TS_RESTART) a (stUpdA σ σ.curThread x .restart) m ∧ + SymL st H Z.Cs (L.wr s (stA (L.curW s)) sched.TS_RESTART) ∧ + OSKeep st OS s (L.wr s (stA (L.curW s)) sched.TS_RESTART) ∧ + (∀ y, SlotWA Z.th y → capAtA (L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART)) y = capAtA (L.mem s) y) := + J.of_restartCoreA hrun hnvm hvc (J.toJEntA.restartA P Y hrun hcur hvs hvc) + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3RqDomPrioA.lean b/proofs/Fl4ProofsA64/P3/Stage3RqDomPrioA.lean new file mode 100644 index 000000000..b8e4cbe6b --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3RqDomPrioA.lean @@ -0,0 +1,69 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2b): **ready-queue domain and priority from the record**: every ready-queue member +sits in the queue of its own domain and priority. `SchedRestA.rqDom` / `rq` / `prio` and `SchedCoreA.inQ` at the +current core. The statement is `RqDomPrio σ` of `par/a64-p3-g1`'s `Stage3DelReplyRemoveA` unfolded (that branch is not +merged here), the side hypothesis of its `replyRemove_schedValid`. + +* **`rqDomPrio_of_schedA`** (from `SchedSRA`), **`JStage3A.rqDomPrioA`**. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3A +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {Z : XSetsA} {m : McState} + {κ : U64 → U64} {s : S} {σ : State Unit} + +/-- **`RqDomPrio` (unfolded) from the scheduler relation** (module docstring). -/ +theorem rqDomPrio_of_schedA (H : SchedSRA st Z m κ s σ) : + ∀ f d p x, σ.kheap f = some (.tcb x) → f ∈ σ.readyQueues d p → d = x.tcbDomain ∧ p = x.tcbPriority := by + intro f d p x hx hm + by_cases hd : d = 0 + swap + · rw [H.rest.rqDom d p hd] at hm + simp at hm + subst hd + have hp8 : p.toNat < 256 := p.isLt + obtain ⟨pw, hpw⟩ : ∃ pw : U64, pw.val = p.toNat := + ⟨⟨BitVec.ofNat 64 p.toNat⟩, by + show (BitVec.ofNat 64 p.toNat).toNat = p.toNat + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega)⟩ + have hlt : pw.val < 256 := by omega + have e8 : BitVec.ofNat 8 pw.val = p := by + apply BitVec.eq_of_toNat_eq + rw [BitVec.toNat_ofNat, hpw] + exact Nat.mod_eq_of_lt hp8 + have hq := H.rest.rq pw hlt + rw [e8] at hq + rw [hq] at hm + obtain ⟨t, ht, rfl⟩ := List.mem_map.1 hm + have htT : t ∈ Z.T := ((H.core.ready (L.cpu s) H.sch.cpuCs).1.rel pw hlt).mem t ht + have hinq : L.mem s (prioA t) = pw := (H.core.inQ (L.cpu s) H.sch.cpuCs pw hlt t ht).2 + obtain ⟨y, hy, hd0, -, hpr⟩ := H.rest.prio t htT + rw [hy] at hx + obtain rfl := KernelObject.tcb.inj (Option.some.inj hx) + refine ⟨hd0.symm, ?_⟩ + apply BitVec.eq_of_toNat_eq + rw [← hpw, ← hinq] + exact hpr + +variable {AR : A → HwRootA → Prop} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} {ia ie : Nat} + {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {n : Nat} {J5 J7 : JSlotA S} {a : A} + +/-- **`RqDomPrio` (unfolded) at the record.** -/ +theorem JStage3A.rqDomPrioA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) : + ∀ f d p x, σ.kheap f = some (.tcb x) → f ∈ σ.readyQueues d p → d = x.tcbDomain ∧ p = x.tcbPriority := + rqDomPrio_of_schedA J.sr + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3RsSiteA.lean b/proofs/Fl4ProofsA64/P3/Stage3RsSiteA.lean new file mode 100644 index 000000000..1bf1f7035 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3RsSiteA.lean @@ -0,0 +1,76 @@ +/- +P3 (aarch64, A64-P3 Stage-3 T3 item G3, part 5): **the `RsSiteA` producer** (the restart-site bundle every +cap-invoking arm takes), from the record at a decode site. + +`RsSiteA.vs` (and `JEntA.restartA`'s `hvs`) are stated over the installed pools and reachable tables `utVsPtA` +(approved merged-statement narrowing, Architect 01:08Z: over P1's whole table set `Pp ∪ Tb` they were not derivable, +`Tb` being placed only on `Z.S1`); here they follow from the record's `vsOk` (`.al`, `.om`: an `Ω` word is off every +TCB). + +* `JStage3A.rsVsA` (`vs`), `JStage3A.rsVcA` (`vc`: the VCPU words are `Ω` words, off the current TCB); +* **`JStage3A.rsSiteA`**: `RsSiteA` from the record, the decode site's lookup results (`ext`, `src`, `SlotCov` of the + invoked slot) and the syscall-entry facts of G4 (`L.actionW s = 0`, `running`, `cur`). + +Named premises: none. New module; no existing statement changed. +-/ +import Fl4ProofsA64.P3.Stage3DecExtraA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section rows +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- An `Ω` word is not the current thread's `STATE` word. -/ +theorem JStage3A.omega_ne_stA (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {b : U64} (hb : b ∈ H.omega) : b ≠ stA (L.curW s) := by + have q := J.sq + intro e + exact out_ne_field (q.omega _ hb) q.curT (q.tcbs _ q.curT) (K := 3328#u64) (by simp) (by rw [e, stA_eq] <;> rfl) + +/-- **`RsSiteA.vs`** from `vsOk`. -/ +theorem JStage3A.rsVsA (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) : + ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → Aligned b ∧ b ≠ stA (L.curW s) := + fun b hb => ⟨J.vsOk.al b hb, J.omega_ne_stA (J.vsOk.om b hb)⟩ + +/-- **`RsSiteA.vc`**: a VCPU's `TCB` / `RUN_CALLER` word is not the current thread's `STATE` word. -/ +theorem JStage3A.rsVcA {R : Set U64 → JSlotA S} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7A st R) s a σ m) : + ∀ v ∈ Z.V, stA (L.curW s) ≠ v ∧ stA (L.curW s) ≠ wa v 24#u64 := by + obtain ⟨_, _, _, _, arm, -⟩ := J.j7 + intro v hv + have W := vcWords_ofHA J.sys.ent.heap (arm.vh v hv) + have w0 := (W 0#u64 (by simp) (by simp)).2.1 + rw [wa_zeroVcA] at w0 + exact ⟨fun e => J.omega_ne_stA w0 e.symm, + fun e => J.omega_ne_stA (W 24#u64 (by simp) (by simp)).2.1 e.symm⟩ + +/-- **`RsSiteA` at a decode site** (module docstring). -/ +theorem JStage3A.rsSiteA {R : Set U64 → JSlotA S} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st R) s a σ m) + {extra : Aeneas.Std.Array obj.Slot 3#usize} {slot : U64} + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hsrc : slot ∈ OS) (hcov : SlotCov st OS s slot) + (hact : L.actionW s = 0#u64) (hrun : Runnable (L.mem s (stA (L.curW s)))) + (hcur : (L.mem s (inqA (L.curW s)) &&& sched.Q_RELEASE) = 0#u64 ∧ L.mem s (scA (L.curW s)) ≠ 0#u64) : + RsSiteA (L := L) Z H OS Ac Pp Tb Lv s where + pl := ⟨H.replies, H.ntfns, extra, slot, J.sysPlace hext hsrc hcov⟩ + sym := J.symL + run := hrun + cur := ⟨hcur.1, hcur.2, J.curSc_act hact hcur.2⟩ + vs := J.rsVsA + vc := J.rsVcA + sub := J.j5.2.1 + +end rows + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3ScBindArmA.lean b/proofs/Fl4ProofsA64/P3/Stage3ScBindArmA.lean new file mode 100644 index 000000000..ec4793f86 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ScBindArmA.lean @@ -0,0 +1,328 @@ +/- +P3 (aarch64, A64-P3 Stage 3, scbind): **SchedContext Bind and UnbindObject of a TCB at a syscall entry**. + +* `refillCheckTest_bkA`: the active-SC test variant of the sporadic unblock (`if_sporadic_active_cur_sc_test_…`) keeps + the current SC's budget (`BKeepA`): it runs only off the current SC. +* `Sched.BodyEndA.ofScTcbA`: the caller's `Restart` then a `BKeepA` step that keeps the current thread's TCB ends in + `BodyEndA` (`ofCurBA`). +* `JSyscallA.scTcbEntryA`: the entry facts shared by the arms (the caller's Restart, the SC, the quiet stall, `CurBA`). +* **`JSyscallA.scBindTcbArmA`**: `decode_invocation` on a SchedContext cap with the Bind label and a thread cap + (`JStage3A.scBindTcbA`). +* **`JSyscallA.scUnbindTcbArmA`**: the same with the UnbindObject label and the SC's own thread + (`JStage3A.scUnbindTcbA`). + +Named premises: [P] `ArchTotal`, `MaxSpi`, `ArchPlat`, `A64FrameMasks`, `hCs`, Bind's `hmb`; [S] `h1` (`numNodes = 1`: +the remote stalls, same core). Decode-site case hypotheses: the decoder's checks (`hn`, `ht0`, `hbl`/`hrel` for Bind; +`htw`, `hne` for UnbindObject) and `hsl` (the spec cap at a slot naming the target thread, so it is no core's idle +thread, as `JSyscallA.resumeStoppedA`). The `Err` returns are not lifted. New module. +-/ +import Fl4ProofsA64.P3.Stage3ScBindTcbA +import Fl4ProofsA64.P3.Stage3ScNtfnA +import Fl4ProofsA64.P3.Stage3RecvSendCscA +import Fl4ProofsA64.P3.Stage3BodyCurA +import Fl4ProofsA64.P3.ScUnbComposeA +import Fl4ProofsA64.P3.DelSuspendA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 + +noncomputable section + +theorem refillCheckTest_bkA (o : Option ObjRef) : + KPresB (ifCondRefillUnblockCheck (Z := Unit) o (some true) (some false)) := by + unfold ifCondRefillUnblockCheck + cases o with + | none => exact PresR.pure _ + | some p => + intro σ r hr + simp only [NonDet.maybeM] at hr + obtain ⟨⟨sc, σ1⟩, h1, hr⟩ := mem_bindA.1 hr + have e1 : σ1 = σ := (getSchedContext_mem h1).1 + rw [e1] at hr + dsimp only at hr + rw [nd_gets_bindSc, nd_pure_bindSc] at hr + try dsimp only at hr + unfold NonDet.when at hr + split at hr + · rename_i c + have ne : p ≠ σ.curSc := c.2 rfl + split at hr + · exact (refillUnblockCheck_onlyA σ r hr).bkeep ne + · have : r = ((), σ) := hr + rw [this]; exact PreO.refl _ + · have : r = ((), σ) := hr + rw [this]; exact PreO.refl _ + +instance (o : Option ObjRef) : KPresBInst (ifCondRefillUnblockCheck (Z := Unit) o (some true) (some false)) := + ⟨refillCheckTest_bkA o⟩ +instance (p t : ObjRef) : KPresBInst (schedContextBindTcb (Z := Unit) p t) := + ⟨by unfold schedContextBindTcb setTcbObjRef setScObjRef; kbres⟩ +instance (p : ObjRef) : KPresBInst (schedContextUnbindTcb (Z := Unit) p) := + ⟨by unfold schedContextUnbindTcb setTcbObjRef setScObjRef; kbres⟩ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s : S} {a : A} {σ σ2 : State Unit} {m : McState} + +/-- **`BodyEndA` after the caller's `Restart` and an SC–TCB link step** (module docstring). -/ +theorem Sched.BodyEndA.ofScTcbA {J5 J7 : JSlotA S} + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {xc : Tcb} (hkc : σ.kheap σ.curThread = some (.tcb xc)) + (BK : BKeepA (stUpdA σ σ.curThread xc .restart) σ2) + (hoth : σ2.kheap σ.curThread = some (.tcb { xc with tcbState := .restart })) + (hsw : NoSwA σ2) (hinv : ScInvA m σ2) (hzc : ZeroConsA σ2) : BodyEndA σ σ2 m := by + have ne : σ.curSc ≠ σ.curThread := fun e => by + obtain ⟨y0, j, h0⟩ := J.curInv.sc + rw [e, hkc] at h0; cases h0 + have e1 : (stUpdA σ σ.curThread xc .restart).kheap σ.curSc = σ.kheap σ.curSc := by + simp [stUpdA, setObjA, Function.update_of_ne ne] + obtain ⟨t0, ht0, hts, -⟩ := J.curInv.tcb + rw [hkc] at ht0 + obtain rfl := KernelObject.tcb.inj (Option.some.inj ht0) + have CB : CurBA σ2 := ⟨{ xc with tcbState := .restart }, by rw [BK.ct]; exact hoth, by rw [BK.csc]; exact hts⟩ + refine BodyEndA.ofCurBA J hinv hzc (fun u e => absurd e (hsw u)) BK.time (by rw [BK.ms]; rfl) BK.ct BK.csc BK.used + (fun y' k h => ?_) CB (fun _ x hx => ?_) + · obtain ⟨y, hy, h1, h2⟩ := BK.sc y' k h + exact ⟨y, by rw [← e1]; exact hy, h1, h2⟩ + · rw [hoth] at hx + cases hx + exact Or.inr rfl + +set_option maxHeartbeats 2000000 in +/-- The entry facts shared by the SC–TCB arms (module docstring). -/ +theorem JSyscallA.scTcbEntryA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + {slot sc sz : U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .SchedContext sc sz) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) : + ∃ xc, σ.kheap σ.curThread = some (.tcb xc) ∧ + sched.set_thread_state st s (L.curW s) sched.TS_RESTART = ok (L.wr s (stA (L.curW s)) sched.TS_RESTART) ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), stUpdA σ σ.curThread xc .restart)}, False) ∧ + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) + (L.wr s (stA (L.curW s)) sched.TS_RESTART) a (stUpdA σ σ.curThread xc .restart) m ∧ + SActA (stUpdA σ σ.curThread xc .restart) ∧ NoSwA (stUpdA σ σ.curThread xc .restart) ∧ + CurBA (stUpdA σ σ.curThread xc .restart) ∧ + sc ∈ Z.C ∧ scNameA m sc = specPtr sc ∧ NotIdleScA m (scNameA m sc) ∧ + (∀ q, q ≠ σ.curThread → (stUpdA σ σ.curThread xc .restart).kheap q = σ.kheap q) := by + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hslot hcov J.actW J.running J.cur + have hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM := J.toJStage3EntryA.curNotVmA + obtain ⟨xc, hkc, hst, hsp, J1, -, -, -⟩ := J.toJStage3A.of_restartCurA RS.pl.choose_spec.choose_spec.choose_spec.choose_spec + RS.sym RS.run hnvm RS.cur RS.vs RS.vc + have T0 := J.toJStage3A.toTA J.sact J.noSwitch + have hH : H sc = some (.sc sz.val) := (T0.capTy slot hslot).2.2.2.1 sc sz hcap + have hcH : sc ∈ H.scs := ⟨_, hH⟩ + have hc : sc ∈ Z.C := by rw [← J.scs]; exact hcH + have hci : specPtr sc ≠ m.idleScOf m.curCore := T0.ciSc slot hslot sc sz hcap + have hname : scNameA m sc = specPtr sc := by unfold scNameA; rw [if_neg hci] + have hni : NotIdleScA m (scNameA m sc) := by + rw [hname] + refine ⟨J.sr.sch.scOff sc hc hci, fun c hcN => ?_⟩ + have hcc : m.curCore = 0 := Nat.lt_one_iff.mp (lt_of_lt_of_eq (T0.curCore_lt hCs) h1) + have hc1 : c = 0 := Nat.lt_one_iff.mp (lt_of_lt_of_eq hcN h1) + have e : c = m.curCore := hc1.trans hcc.symm + rw [e] + exact hci + have sact1 : SActA (stUpdA σ σ.curThread xc .restart) := + ⟨fun u h => J.sact.f1 u h, ⟨{ xc with tcbState := .restart }, by simp only [stUpdA, setObjA, Function.update_self], + Or.inr (Or.inl rfl)⟩⟩ + obtain ⟨t0, ht0, hts, -⟩ := J.curInv.tcb + rw [hkc] at ht0 + obtain rfl := KernelObject.tcb.inj (Option.some.inj ht0) + refine ⟨xc, hkc, hst, hsp, J1, sact1, fun u h => J.noSwitch u h, + ⟨{ xc with tcbState := .restart }, by simp only [stUpdA, setObjA, Function.update_self], hts⟩, hc, hname, hni, + fun q hq => ?_⟩ + simp [stUpdA, setObjA, Function.update_of_ne hq] + +set_option maxHeartbeats 4000000 in +/-- **SchedContext Bind of a thread at a syscall entry** (module docstring). -/ +theorem JSyscallA.scBindTcbArmA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + {slot lbl len buf sc sz t : U64} {blk call don b : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .SchedContext sc sz) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hlbl : lbl = fl4_abi.label.SchedContextBind) + (he : schedcontext.extra_cap st s extra 0#usize = ok (some (.Thread t))) + {sl : CslotPtr} (hsl : specCapAtA σ sl = some (.threadCap (specPtr t))) + (hn : obj.Sc.tcb st sc s = ok 0#u64) (ht0 : obj.Tcb.sc st t s = ok 0#u64) + (hbl : sched.is_blocked st s t = ok b) (hrel : b = true → sporadic.sc_released st s sc = ok true) : + ∃ xc σ1 σ2 s2, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + invokeSchedContext (Z := Unit) (.invokeSchedContextBind (specPtr sc) (.threadCap (specPtr t))) σ1 = + ({([], σ2)}, False) ∧ + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (.Ok (), s2, a) ∧ + ∃ Q Rl Ac', JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac' Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s2 a σ2 m ∧ Sched.BodyEndA σ σ2 m := by + obtain ⟨xc, hkc, hst, hsp, J1, sact1, nsw1, cb1, hc, hname, hni, far⟩ := J.scTcbEntryA hCs h1 hslot hcov hcap hext + have T0 := J.toJStage3A.toTA J.sact J.noSwitch + have ht : t ∈ Z.T := by + have q := schedcontext.extra_cap.slot (J.decPreA hslot hext) (WritesIn.refl s _) + rw [he, WP.spec_ok] at q + obtain ⟨x, hx, hx0, hxc, -⟩ := q _ rfl + exact J.tcbs ▸ (J.toJStage3A.slotCapTy (hext x hx hx0)).1.2.2.2.2.2 t hxc + have hstallT : smp.remote_tcb_stall st ar s a t = ok (s, a) := T0.stall_singleA hCs h1 ht + obtain ⟨hti, hidles⟩ := T0.notIdle_of_capA J.sinv.mc.curCore_lt hsl + have w0 : L.mem s (scTcbA sc) = 0#u64 := by + have h := eq_of_spec_fd (obj.Sc.tcb.spec (L := L) s sc) + rw [hn] at h + exact (ok_inj_ff h).symm + have wt0 : L.mem s (scA t) = 0#u64 := by + have h := eq_of_spec_fd (obj.Tcb.sc.spec (L := L) s t) + rw [ht0] at h + exact (ok_inj_ff h).symm + obtain ⟨xt, hkt, hxo⟩ := J.sr.sch.tcb t ht + have hxn : xt.tcbSchedContext = none := by rw [hxo, wt0, optScA, if_pos rfl] + have htc : specPtr t ≠ σ.curThread := fun e => by + obtain ⟨t0, ht0', hts, -⟩ := J.curInv.tcb + rw [← e, hkt] at ht0' + cases ht0' + rw [hxn] at hts; cases hts + obtain ⟨y, k, hky, hy⟩ := J.sr.scr.sc sc hc + have hscur : scNameA m sc ≠ σ.curThread := fun e => by + rw [e, hkc] at hky; cases hky + -- the decoder's release check in the spec + have hbx : b = ipcQueuedThreadState xt.tcbState := by + have e := bind_isBlockedA (st := st) (J.stEqA t ht xt hkt) + rw [hbl] at e + exact ok_inj_ff e + have hbS : ∀ x y k, (stUpdA σ σ.curThread xc .restart).kheap (specPtr t) = some (.tcb x) → + (stUpdA σ σ.curThread xc .restart).kheap (scNameA m sc) = some (.schedContext y k) → + ipcQueuedThreadState x.tcbState = true → scReleased (stUpdA σ σ.curThread xc .restart).curTime y = true := by + intro x y' k' hx hy' hq + rw [far _ htc, hkt] at hx + cases hx + rw [far _ hscur] at hy' + have hr := hrel (hbx.trans hq) + rw [bind_scReleasedA J.sr.toSchedSRN hc hy'] at hr + exact ok_inj_ff hr + -- the link words after the caller's Restart + have hkt1 : (stUpdA σ σ.curThread xc .restart).kheap (specPtr t) = some (.tcb xt) := (far _ htc).trans hkt + have hky1 : (stUpdA σ σ.curThread xc .restart).kheap (scNameA m sc) = some (.schedContext y k) := + (far _ hscur).trans hky + have ws1 : L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART) (scA t) = 0#u64 := by + obtain ⟨z, hz, hopt⟩ := J1.sr.sch.tcb t ht + rw [hkt1] at hz; cases hz + unfold optScA at hopt + by_contra hh + rw [if_neg hh, hxn] at hopt + cases hopt + have hs1 : L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART) (scTcbA sc) = 0#u64 := by + obtain ⟨z, j, hz, hzR⟩ := J1.sr.scr.sc sc hc + rw [hky1] at hz; cases hz + have e1 := hzR.tcb + rw [hy.tcb, ← scTcbA_eqSc, w0, optRefS_zero] at e1 + by_contra hnon + rw [optRefS_ne hnon] at e1 + cases e1 + have R := J1.scBindTcbA sact1 nsw1 hCs h1 (hmb a) hc ht hni hti hidles ws1 hs1 cb1 hbS + obtain ⟨⟨s2, a2⟩, prog, ea, σ2, Q, Rl, hspec, LI, NS, SV, RY, OT, K⟩ := exists_of_specA R + simp only at ea + subst a2 + have hdec : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (.Ok (), s2, a) := by + rw [hcap, decodeSc_programA, if_neg Bool.false_ne_true, hlbl, + bind_decode_bodyA hn (cur_thread_ok s) he ht0 hbl hrel hstallT, hst, bind_ok, prog, bind_ok] + simp only [uncurry_apply_pair] + obtain ⟨-, Ac', sysl, symL, bxi, userOK, j7⟩ := J.decodeFinA hA hM hPl hfm hslot hcov + (rfl : capA st s slot = capA st s slot) hext hSC (lbl := lbl) + (by rw [hcap, hlbl]; simp [DecArmS1aEnt, DecArmQH, DecArmQZ2, DecArmKQ2, DecArmKQ, DecArmKQ3, DecArmDelQ, DecArmS1aC, capFam]) + (fun _ h => by rw [hcap] at h; cases h) (fun _ h => by rw [hcap] at h; cases h) hdec σ2 m + have J2 := K Ac' bxi sysl symL userOK j7 + have hm : ((), σ2) ∈ (schedContextBindTcb (Z := Unit) (scNameA m sc) (specPtr t) + (stUpdA σ σ.curThread xc .restart)).1 := by rw [hspec]; exact rfl + have BK : BKeepA (stUpdA σ σ.curThread xc .restart) σ2 := + (inferInstance : KPresBInst (schedContextBindTcb (Z := Unit) (scNameA m sc) (specPtr t))).pres _ _ hm + have hoth : σ2.kheap σ.curThread = some (.tcb { xc with tcbState := .restart }) := + OT _ _ (Ne.symm htc) (by simp only [stUpdA, setObjA, Function.update_self]) + rw [hname] at hspec + refine ⟨xc, _, σ2, s2, hkc, rfl, hsp, ?_, hdec, Q, Rl, Ac', J2, + Sched.BodyEndA.ofScTcbA J hkc BK hoth NS J2.sr.inv J2.sinv.zc⟩ + change (do schedContextBindTcb (Z := Unit) (specPtr sc) (specPtr t); pure ([] : List Data)) _ = _ + rw [nd_bind_oneSc hspec] + rfl + +set_option maxHeartbeats 4000000 in +/-- **SchedContext UnbindObject of its thread at a syscall entry** (module docstring). -/ +theorem JSyscallA.scUnbindTcbArmA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + {slot lbl len buf sc sz t : U64} {blk call don : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .SchedContext sc sz) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hlbl : lbl = fl4_abi.label.SchedContextUnbindObject) + (he : schedcontext.extra_cap st s extra 0#usize = ok (some (.Thread t))) + (htw : obj.Sc.tcb st sc s = ok t) (hne : t ≠ L.curW s) : + ∃ xc σ1 σ2 s2, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + invokeSchedContext (Z := Unit) (.invokeSchedContextUnbindObject (specPtr sc) (.threadCap (specPtr t))) σ1 = + ({([], σ2)}, False) ∧ + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (.Ok (), s2, a) ∧ + ∃ Q Rl Ac', JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac' Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s2 a σ2 m ∧ Sched.BodyEndA σ σ2 m := by + obtain ⟨xc, hkc, hst, hsp, J1, sact1, nsw1, -, hc, hname, hni, far⟩ := J.scTcbEntryA hCs h1 hslot hcov hcap hext + have T0 := J.toJStage3A.toTA J.sact J.noSwitch + have ht : t ∈ Z.T := by + have q := schedcontext.extra_cap.slot (J.decPreA hslot hext) (WritesIn.refl s _) + rw [he, WP.spec_ok] at q + obtain ⟨x, hx, hx0, hxc, -⟩ := q _ rfl + exact J.tcbs ▸ (J.toJStage3A.slotCapTy (hext x hx hx0)).1.2.2.2.2.2 t hxc + have t0 : t ≠ 0#u64 := (tcb_ne01Sc (J.sr.core.tcbs t ht)).1 + have hstall : (if t != 0#u64 then smp.remote_tcb_stall st ar s a t else ok (s, a)) = ok (s, a) := by + rw [if_pos (bne_iff_ne.2 t0)] + exact T0.stall_singleA hCs h1 ht + have w0 : L.mem s (scTcbA sc) = t := by + have h := eq_of_spec_fd (obj.Sc.tcb.spec (L := L) s sc) + rw [htw] at h + exact (ok_inj_ff h).symm + have htc : specPtr t ≠ σ.curThread := fun e => hne (specPtr_inj (e.trans J.sr.sch.curThread)) + obtain ⟨y, k, hky, hy⟩ := J.sr.scr.sc sc hc + have hscur : scNameA m sc ≠ σ.curThread := fun e => by + rw [e, hkc] at hky; cases hky + have hs1 : L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART) (scTcbA sc) = t := by + obtain ⟨z, j, hz, hzR⟩ := J1.sr.scr.sc sc hc + rw [far _ hscur, hky] at hz + cases hz + have e1 := hzR.tcb + rw [hy.tcb, ← scTcbA_eqSc] at e1 + exact (scOpt_injA e1).symm.trans w0 + have R := J1.scUnbindTcbA sact1 nsw1 hCs h1 hc hni (by rw [hs1]; exact t0) + obtain ⟨s2, prog, σ2, Q, Rl, hspec, LI, NS, SV, hcur, OT, K⟩ := exists_of_specA R + have hdec : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (.Ok (), s2, a) := by + rw [hcap, decodeSc_programA, if_neg Bool.false_ne_true, hlbl, + sc_decode_unbind_object_thread_eqA htw hstall (cur_thread_ok s) he htw hne, hst, bind_ok, prog, bind_ok] + obtain ⟨-, Ac', sysl, symL, bxi, userOK, j7⟩ := J.decodeFinA hA hM hPl hfm hslot hcov + (rfl : capA st s slot = capA st s slot) hext hSC (lbl := lbl) + (by rw [hcap, hlbl]; simp [DecArmS1aEnt, DecArmQH, DecArmQZ2, DecArmKQ2, DecArmKQ, DecArmKQ3, DecArmDelQ, DecArmS1aC, capFam]) + (fun _ h => by rw [hcap] at h; cases h) (fun _ h => by rw [hcap] at h; cases h) hdec σ2 m + have J2 := K Ac' bxi sysl symL userOK j7 + have hm : ((), σ2) ∈ (schedContextUnbindTcb (Z := Unit) (scNameA m sc) (stUpdA σ σ.curThread xc .restart)).1 := by + rw [hspec]; exact rfl + have BK : BKeepA (stUpdA σ σ.curThread xc .restart) σ2 := + (inferInstance : KPresBInst (schedContextUnbindTcb (Z := Unit) (scNameA m sc))).pres _ _ hm + have hoth : σ2.kheap σ.curThread = some (.tcb { xc with tcbState := .restart }) := + OT _ _ (by rw [hs1]; exact Ne.symm htc) (by simp only [stUpdA, setObjA, Function.update_self]) + rw [hname] at hspec + refine ⟨xc, _, σ2, s2, hkc, rfl, hsp, ?_, hdec, Q, Rl, Ac', J2, + Sched.BodyEndA.ofScTcbA J hkc BK hoth NS J2.sr.inv J2.sinv.zc⟩ + change (do schedContextUnbindTcb (Z := Unit) (specPtr sc); pure ([] : List Data)) _ = _ + rw [nd_bind_oneSc hspec] + rfl + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3ScBindSpecA.lean b/proofs/Fl4ProofsA64/P3/Stage3ScBindSpecA.lean new file mode 100644 index 000000000..21b4ef883 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ScBindSpecA.lean @@ -0,0 +1,270 @@ +/- +P3 (aarch64, A64-P3 Stage 3, scbind): **the spec side of SchedContext Bind of a TCB** (`schedContextBindTcb`). + +* `schedContextResume_tailSA`: l4v's `sched_context_resume` keeps every non-SC object and every SC's replies (the twin + of `fl4SchedContextResume_tailSA`). +* `ScRYKeepA`: every SC kept with its replies and its yielder. +* **`bindTcb_specA`**: across `schedContextBindTcb p t` of an unbound thread `t` (not idle, not current) to an unbound + SC `p` (not `idle_sc_ptr`, not the current SC): `LoopInvA`, no pending switch, `SchedValidA` (a blocked `t` needs + `p` active: the decoder's `sc_released` check) and `ScRYKeepA`. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3TcbResumeSpecA +import Fl4ProofsA64.P3.Stage3WakeInvA +import Fl4ProofsA64.P3.ScBindUnblockA +import Fl4ProofsA64.P3.Stage3SchedValidStepA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open Fl4Spec Fl4SpecA64 Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Endp + +noncomputable section + +theorem schedContextResume_tailSA (p : ObjRef) : PresR TailSA (schedContextResume (Z := Unit) p) := by + unfold schedContextResume + refine PresR.bind (eq_tailSA (getSchedContext_eqPres p)) (fun _ => ?_) + refine PresR.bind (eq_tailSA (PresR.assertOpt _)) (fun t => ?_) + refine PresR.bind (PresR.gets _) (fun _ => PresR.when ?_) + refine PresR.bind (eq_tailSA (threadGet_eqPres _ t)) (fun _ => ?_) + refine PresR.bind (eq_tailSA (getScRefillReady_eqPres p)) (fun _ => ?_) + refine PresR.bind (eq_tailSA (getScRefillSufficient_eqPres p 0)) (fun _ => PresR.when ?_) + refine PresR.bind (eq_tailSA (threadGet_eqPres _ t)) (fun d => ?_) + refine PresR.bind (eq_tailSA (threadGet_eqPres _ t)) (fun q => ?_) + refine PresR.bind (eq_tailSA (by unfold getTcbQueue; eqpres)) (fun _ => ?_) + exact PresR.bind (eq_tailSA (PresR.assert _)) (fun _ => postpone_tailSA p) + +/-- Every SC is kept, with its replies and its yielder. -/ +def ScRYKeepA (σ σ' : State Unit) : Prop := + ∀ q y k, σ.kheap q = some (.schedContext y k) → ∃ y', σ'.kheap q = some (.schedContext y' k) ∧ + y'.scReplies = y.scReplies ∧ y'.scYieldFrom = y.scYieldFrom + +theorem ScRYKeepA.trans {σ1 σ2 σ3 : State Unit} (h1 : ScRYKeepA σ1 σ2) (h2 : ScRYKeepA σ2 σ3) : ScRYKeepA σ1 σ3 := + fun q y k h => by + obtain ⟨y1, e1, r1, f1⟩ := h1 q y k h + obtain ⟨y2, e2, r2, f2⟩ := h2 q y1 k e1 + exact ⟨y2, e2, r2.trans r1, f2.trans f1⟩ + +theorem ScRYKeepA.of_kheap {σ σ' : State Unit} (h : σ'.kheap = σ.kheap) : ScRYKeepA σ σ' := + fun _ y _ e => ⟨y, by rw [h]; exact e, rfl, rfl⟩ + +theorem bind_inClass_blocked'A {x : Tcb} + (hr : runnable x.tcbState = false) (hc : inClassRSc x.tcbState x.tcbFault) : + ipcQueuedThreadState x.tcbState = true := by + rcases hc with h | ⟨u, h⟩ | ⟨e, d, h, -⟩ | ⟨e, r, d, h⟩ + · rw [h] at hr; cases hr + · rw [h]; rfl + · rw [h]; rfl + · rw [h]; rfl + +theorem blockedTs_ipcQueuedA {ts : ThreadState} (h : BlockedTsA ts = true) : ipcQueuedThreadState ts = true := by + cases ts <;> simp_all [BlockedTsA, ipcQueuedThreadState] + +set_option maxHeartbeats 4000000 in +/-- **The spec invariants across `schedContextBindTcb`** (module docstring). -/ +theorem bindTcb_specA {σ σ' : State Unit} {m : McState} {n : Nat} {p t : ObjRef} {x : Tcb} {y : SchedContext} + {k : Nat} (h : LoopInvA σ m n) (ns : NoSwA σ) (V : SchedValidA σ) (D : RqDomPrio σ) + (hk : σ.kheap t = some (.tcb x)) (hp : σ.kheap p = some (.schedContext y k)) + (hx : x.tcbSchedContext = none) (hy : y.scTcb = none) (hpi : p ≠ idleScPtr) + (hti : t ≠ σ.idleThread) (htc : t ≠ σ.curThread) (hidles : ∀ c < n, m.idleThreadOf c ≠ t) + (hpc : p ≠ σ.curSc) (hact : BlockedTsA x.tcbState = true → scActive y = true) + (hr : ((), σ') ∈ (schedContextBindTcb (Z := Unit) p t σ).1) : + LoopInvA σ' m n ∧ NoSwA σ' ∧ SchedValidA σ' ∧ ScRYKeepA σ σ' ∧ + ∀ q z, q ≠ t → σ.kheap q = some (.tcb z) → σ'.kheap q = some (.tcb z) := by + have hpt : p ≠ t := fun e => by rw [e, hk] at hp; cases hp + have hnr : t ∉ σ.releaseQueue := fun hm => by + obtain ⟨x', sc, e, es, -⟩ := V.rls t hm + rw [hk] at e; cases e; rw [hx] at es; cases es + have hnq : ∀ d q, t ∉ σ.readyQueues d q := fun d q hm => by + obtain ⟨⟨x', sc, e, es, -⟩, -⟩ := V.rq d q t hm + rw [hk] at e; cases e; rw [hx] at es; cases es + unfold schedContextBindTcb at hr + obtain ⟨⟨⟨⟩, σ1⟩, h1, hr⟩ := mem_bindA.1 hr + obtain ⟨⟨⟨⟩, σ2⟩, h2, hr⟩ := mem_bindA.1 hr + obtain ⟨⟨⟨⟩, σ3⟩, h3, hr⟩ := mem_bindA.1 hr + obtain ⟨⟨⟨⟩, σ4⟩, h4, hr⟩ := mem_bindA.1 hr + -- the two link writes + unfold setTcbObjRef at h1 + obtain ⟨z, hz, e1⟩ := threadSet_memA _ _ h1 + simp only at e1 + rw [hk] at hz + cases hz + have R1 : RStepA m n σ σ1 := by + rw [e1]; exact RStepA.tcbSc h hk hti (some p) (fun _ hm => absurd hm hnr) + have hp1 : σ1.kheap p = some (.schedContext y k) := by + rw [e1] + show Function.update σ.kheap t _ p = _ + rw [Function.update_of_ne hpt]; exact hp + have ht1 : σ1.kheap t = some (.tcb { x with tcbSchedContext := some p }) := by + rw [e1]; exact Function.update_self _ _ _ + have k1 : ∀ q, q ≠ t → σ1.kheap q = σ.kheap q := fun q hq => by + rw [e1]; exact Function.update_of_ne hq _ _ + unfold setScObjRef at h2 + obtain ⟨y0, j, hy0, e2⟩ := updateSchedContext_memA _ _ h2 + simp only at e2 + have ey := hy0.symm.trans hp1 + simp only [Option.some.injEq, KernelObject.schedContext.injEq] at ey + obtain ⟨e0, ej⟩ := ey + subst e0 + subst ej + have R2 : RStepA m n σ1 σ2 := by + rw [e2] + exact RStepA.updSc R1.inv hy0 hpi (y := scTcbUpdate (fun _ => some t) y0) rfl rfl rfl + (Or.inr fun e => hti ((Option.some.inj e).trans R1.idle)) + have hp2 : σ2.kheap p = some (.schedContext (scTcbUpdate (fun _ => some t) y0) j) := by + rw [e2]; exact Function.update_self _ _ _ + have ht2 : σ2.kheap t = some (.tcb { x with tcbSchedContext := some p }) := by + rw [e2] + show Function.update σ1.kheap p _ t = _ + rw [Function.update_of_ne hpt.symm]; exact ht1 + have k2 : ∀ q, q ≠ p → q ≠ t → σ2.kheap q = σ.kheap q := fun q h1 h2 => by + rw [e2] + show Function.update σ1.kheap p _ q = _ + rw [Function.update_of_ne h1]; exact k1 q h2 + have fq : σ2.readyQueues = σ.readyQueues := by rw [e2, e1] + have fr : σ2.releaseQueue = σ.releaseQueue := by rw [e2, e1] + have fa : σ2.schedulerAction = σ.schedulerAction := by rw [e2, e1] + have fc : σ2.curThread = σ.curThread := by rw [e2, e1] + have fi : σ2.idleThread = σ.idleThread := by rw [e2, e1] + -- the sporadic unblock + have I2 := R2.inv + have R3 : RefK σ2 σ3 := ifCondRefillUnblockCheck_refK _ _ _ _ _ h3 + have T3 : TailSA σ2 σ3 := ifCondRefillUnblockCheck_tailSA _ _ _ _ _ h3 + have I3 := I2.refK R3 + have Y3 : ScRYKeepA σ2 σ3 := by + have h3' := h3 + rw [bind_unblock_guardA hp2] at h3' + split_ifs at h3' with hc + · rcases unblock_resultA hp2 (I2.sinv.rpos _ _ _ hp2 hc.2.1) h3' with e | ⟨w, e, -, -, hw, -, -⟩ + · rw [e]; exact ScRYKeepA.of_kheap rfl + · rw [e] + intro q v j hv + by_cases eq : q = p + · subst eq + rw [hp2] at hv + cases hv + refine ⟨w, by simp [updKSc], ?_, ?_⟩ <;> rw [hw] + · exact ⟨v, by simp only [updKSc]; rw [Function.update_of_ne eq]; exact hv, rfl, rfl⟩ + · have e : σ3 = σ2 := congrArg Prod.snd (show ((), σ3) = ((), σ2) from h3') + rw [e]; exact ScRYKeepA.of_kheap rfl + have cs3 : σ3.curSc = σ.curSc := by rw [R3.1]; exact R2.csc.trans R1.csc + have cur3 : σ3.curThread = σ.curThread := by rw [R3.1]; exact R2.cur.trans R1.cur + have link3 : ∀ w j t', σ3.kheap p = some (.schedContext w j) → w.scTcb = some t' → + t' = t ∧ ∀ x', σ3.kheap t' = some (.tcb x') → x'.tcbSchedContext = some p := by + intro w j t' hw ht' + obtain ⟨w', hw', E⟩ := (R3.2.1 p).scF hp2 + rw [hw] at hw' + cases hw' + rw [E.1] at ht' + have et : t' = t := (Option.some.inj ht').symm + subst et + refine ⟨rfl, fun x' hx' => ?_⟩ + obtain ⟨x2, hx2, E2⟩ := (R3.2.1 t').tcbF ht2 + rw [hx'] at hx2 + cases hx2 + exact E2.2.1 + -- resume + have R4 : RStepA m n σ3 σ4 := schedContextResume_stepZA p I3 (fun _ e => absurd (e.trans cs3) hpc) + (fun w j t' hw ht' x' hx' => (link3 w j t' hw ht').2 x' hx') h4 + have T4 : TailSA σ3 σ4 := schedContextResume_tailSA p σ3 _ h4 + have Y4 : ScRYKeepA σ3 σ4 := fun q w j hw => by + obtain ⟨w1, e1, r1, -, -⟩ := T4.sc q w j hw + obtain ⟨w2, e2, f2, -, -⟩ := R4.sc q w j hw + rw [e1] at e2 + cases e2 + exact ⟨w1, e1, r1, f2⟩ + -- `SchedValidA` before the resume + have act2 : ∀ q, ScActiveAtA σ q → ScActiveAtA σ2 q := fun q ⟨w, j, e, a⟩ => by + by_cases eq : q = p + · subst eq; rw [hp] at e; cases e; exact ⟨_, j, hp2, a⟩ + · have eq2 : q ≠ t := fun e' => by rw [e', hk] at e; cases e + exact ⟨w, j, by rw [k2 q eq eq2]; exact e, a⟩ + have actT : ∀ u, u ≠ t → ActTcbA σ u → ActTcbA σ2 u := fun u hu ⟨x', sc, e, es, a⟩ => by + have hup : u ≠ p := fun e' => by rw [e', hp] at e; cases e + exact ⟨x', sc, by rw [k2 u hup hu]; exact e, es, act2 sc a⟩ + have V2 : SchedValidA σ2 := by + refine ⟨fun d q u hm => ?_, fun a1 a2 => ?_, fun u x' sc e bl es => ?_, fun u hm => ?_⟩ + · rw [fq] at hm + obtain ⟨A, nr⟩ := V.rq d q u hm + rw [fr] + exact ⟨actT u (fun e => hnq d q (e ▸ hm)) A, nr⟩ + · rw [fa] at a1 + rw [fc, fi] at a2 + obtain ⟨A, nr⟩ := V.act a1 a2 + rw [fc, fr] + exact ⟨actT _ (fun e => htc e.symm) A, nr⟩ + · by_cases eu : u = t + · subst eu + rw [ht2] at e + cases e + cases es + exact ⟨_, j, hp2, hact bl⟩ + · have hup : u ≠ p := fun e' => by rw [e', hp2] at e; cases e + rw [k2 u hup eu] at e + exact act2 sc (V.blk u x' sc e bl es) + · rw [fr] at hm + exact actT u (fun e => hnr (e ▸ hm)) (V.rls u hm) + have D2 : RqDomPrio σ2 := fun f d q w hw hm => by + rw [fq] at hm + have hf : f ≠ t := fun e => hnq d q (e ▸ hm) + have hfp : f ≠ p := fun e => by rw [e, hp2] at hw; cases hw + rw [k2 f hfp hf] at hw + exact D f d q w hw hm + have V3 := V2.refKA R3 + have D3 : RqDomPrio σ3 := fun f d q w hw hm => by + rw [show σ3.readyQueues = σ2.readyQueues by rw [R3.1]] at hm + obtain ⟨v, hv, -⟩ := (R3.2.1 f).tcbB hw + have e := T3.obj f _ hv (fun _ _ c => by cases c) + rw [hw] at e; cases e + exact D2 f d q w hv hm + have F4 : FIA σ4 := (schedContextResume_fia p (fun _ => True) (fun _ _ _ _ h => h) σ3 + ⟨Sched.SchedValidA.toFIA V3, D3, trivial, fun w j t' hw ht' => Or.inl (by + rw [cur3, (link3 w j t' hw ht').1]; exact fun e => htc e.symm)⟩ ((), σ4) h4).1 + have N4 : NoSwA σ4 := R4.ns (R3.noSwA (R2.ns (R1.ns ns))) + have Y34 : ScRYKeepA σ σ4 := by + refine ScRYKeepA.trans ?_ (Y3.trans Y4) + intro q w j hw + by_cases eq : q = p + · subst eq; rw [hp] at hw; cases hw; exact ⟨_, hp2, rfl, rfl⟩ + · have eq2 : q ≠ t := fun e' => by rw [e', hk] at hw; cases hw + exact ⟨w, by rw [k2 q eq eq2]; exact hw, rfl, rfl⟩ + have O4 : ∀ q z, q ≠ t → σ.kheap q = some (.tcb z) → σ4.kheap q = some (.tcb z) := fun q z hq hz => by + have hqp : q ≠ p := fun e => by rw [e, hp] at hz; cases hz + have z2 : σ2.kheap q = some (.tcb z) := by rw [k2 q hqp hq]; exact hz + exact T4.obj q _ (T3.obj q _ z2 (fun _ _ c => by cases c)) (fun _ _ c => by cases c) + -- the ready-queue tail + obtain ⟨b, hb, hr⟩ := eqPres_mem (PresR.gets _) hr + have eb : b = schedulable t σ4 := congrArg Prod.fst (show (b, σ4) = (schedulable t σ4, σ4) from hb) + unfold NonDet.when at hr + split_ifs at hr with hsch + · obtain ⟨⟨⟨⟩, σ5⟩, h5, h6⟩ := mem_bindA.1 hr + obtain ⟨Q5, a5⟩ := tcbSchedAction_enq_memA t h5 + have hs4 : schedulable t σ4 = true := by rw [← eb]; exact hsch + obtain ⟨A4, N4r, x4, hx4, hrun4⟩ := schedulable_actA hs4 + have c4 : σ4.curThread = σ.curThread := R4.cur.trans cur3 + have I5 := R4.inv.queueE Q5 (fun u e => by + subst e + exact ⟨by rw [c4]; exact htc, hidles, x4, hx4, hrun4⟩) + (fun u e => Or.inl (by rw [a5] at e; exact e)) (fun e => by rw [a5] at e; exact e) + have I6 := I5.reschedule h6 + obtain ⟨Q6, a6⟩ := rescheduleRequired_memA h6 + have F5 := (Sched.HT.enq t (fun _ => True) (fun _ _ h => h)) σ4 ⟨F4, trivial, A4, N4r⟩ ((), σ5) h5 + have F6 := (resched_fia (fun _ => True) (fun _ _ h => h) (fun _ _ h => h)) σ5 ⟨F5.1, trivial⟩ ((), σ') h6 + have kh : σ'.kheap = σ4.kheap := by + have e6 := Q6.1 + have e5 := Q5.1 + simp only at e6 e5 + rw [e6, e5] + have N6 : NoSwA σ' := fun u e => by + simp only at a6 + rw [a6] at e + cases e + exact ⟨I6, N6, F6.1.toValid, Y34.trans (ScRYKeepA.of_kheap kh), + fun q z hq hz => (congrFun kh q).trans (O4 q z hq hz)⟩ + · have e : ((), σ') = ((), σ4) := hr + cases e + exact ⟨R4.inv, N4, F4.toValid, Y34, O4⟩ + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3ScBindTcbA.lean b/proofs/Fl4ProofsA64/P3/Stage3ScBindTcbA.lean new file mode 100644 index 000000000..385ed6ed6 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ScBindTcbA.lean @@ -0,0 +1,283 @@ +/- +P3 (aarch64, A64-P3 Stage 3, scbind): **binding and unbinding a scheduling context and a TCB at the record**. + +* `JStage3A.of_mfrA`: `JStage3A` after a scheduler step given by its `MFrA` frame (writes in `MWrA`, TCBs changed only + in their SC binding, other non-SC objects kept), its spec frame, the spec invariants at the end and every SC's + replies and yielder kept; the P1 facts at the end state are inputs (`of_ipcStepA`, as `JSyscallA.scCfgA`). +* **`JStage3A.scBindTcbA`**: `schedcontext.bind_tcb` of an unbound thread and an unbound SC refines + `schedContextBindTcb` and keeps the record (`sc_bind_tcb_localA`, `bindTcb_specA`). +* **`JStage3A.scUnbindTcbA`**: `schedcontext.unbind_tcb` of a bound SC refines `schedContextUnbindTcb` and keeps the + record (`sc_unbind_tcb_localA`, `unbindTcb_stepA`, `schedContextUnbindTcb_fia`). + +Both are stated at any `JStage3A` state with `SActA` and no pending switch (a caller in the middle of a TCB arm), with +pre-state facts as inputs: the thread and SC sets, the link words, the idle exclusions and, for Bind, the current +thread's binding (`CurBA`) and the decoder's check that a blocked target's SC is released (`hb`). The P1 facts at the +end state (`BXI`, `SysL`, `SymL`, `UserOK`, `J7A`) are inputs of the record, as in every Stage-3 step. +Named premises: [P] `hCs`, Bind's `hmb`; [S] `h1` (`numNodes = 1`: same core). New module. +-/ +import Fl4ProofsA64.P3.Stage3ScBindSpecA +import Fl4ProofsA64.P3.Stage3ScCfgRecA +import Fl4ProofsA64.P3.ScBindTopA +import Fl4ProofsA64.P3.DelThrScA +import Fl4ProofsA64.P3.Stage3DelSvSuspendA +import Fl4ProofsA64.P3.Stage3KEntryBudgetA +import Fl4ProofsA64.P3.Stage3BodyCurA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +instance (p t : ObjRef) : QInst (schedContextBindTcb (Z := Unit) p t) := ⟨by unfold schedContextBindTcb; qpres⟩ + +/-- `sched_context_unbind_tcb` keeps every TCB but its thread's. -/ +theorem unbindTcb_othA {σ σ' : State Unit} {p t : ObjRef} {x : SchedContext} {k : Nat} + (hk : σ.kheap p = some (.schedContext x k)) (ht : x.scTcb = some t) + (hr : ((), σ') ∈ (schedContextUnbindTcb (Z := Unit) p σ).1) : + ∀ q z, q ≠ t → σ.kheap q = some (.tcb z) → σ'.kheap q = some (.tcb z) := by + intro q z hq hz + unfold schedContextUnbindTcb at hr + rw [nd_bind_oneSc (Sched.getSchedContext_eqA hk), ht, + nd_bind_oneSc (show Fl4Spec.NonDet.assertOpt (σ := State Unit) (some t) σ = ({(t, σ)}, False) from rfl), + nd_gets_bindSc] at hr + obtain ⟨a, ha, hr⟩ := mem_bindA.1 hr + have A : a.2.kheap = σ.kheap := by + unfold Fl4Spec.NonDet.when at ha + split_ifs at ha + · have e := (rescheduleRequired_memA ha).1.1 + rw [e] + · have : a = ((), σ) := ha + rw [this] + obtain ⟨b, hb, hr⟩ := mem_bindA.1 hr + have B : b.2.kheap = σ.kheap := (tcbSchedDeq_kheapA t hb).trans A + obtain ⟨c, hc', hr⟩ := mem_bindA.1 hr + have C : c.2.kheap = σ.kheap := (tcbReleaseRemove_kheapA t hc').trans B + obtain ⟨d, hd, hr⟩ := mem_bindA.1 hr + unfold setTcbObjRef at hd + obtain ⟨zt, hzt, ez⟩ := threadSet_memA _ _ hd + unfold setScObjRef at hr + obtain ⟨y, j, hy, ey⟩ := updateSchedContext_memA _ _ hr + simp only at ey + have hqp : q ≠ p := fun e => by rw [e, hk] at hz; cases hz + rw [ey] + change Function.update d.2.kheap p _ q = _ + rw [Function.update_of_ne hqp, ez] + change Function.update c.2.kheap t _ q = _ + rw [Function.update_of_ne hq, C]; exact hz + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s : S} {a : A} {σ : State Unit} {m : McState} + +set_option maxHeartbeats 3200000 in +/-- **`JStage3A` after an `MFrA` scheduler step** (module docstring). -/ +theorem JStage3A.of_mfrA {Ac' : Set U64} {s' : S} {σ' : State Unit} {Q : U64 → U64 → List U64} {Rl : U64 → List U64} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (F : MFrA (st := st) Z m s s' σ σ') (Fr : SchedFrameA σ σ') (Qk : QKeepA σ σ') + (sr : SchedSRA st { Z with Q := Q, R := Rl } m κ s' σ') (hsc : ScRYKeepA σ σ') + (LI : LoopInvA σ' m n) (sv : SchedValidA σ') + (bxi : BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac' s') + (sysl : SysL st H Z.Cs OS ia ie s') (symL : SymL st H Z.Cs s') + (userOK : Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s') + (j7 : J7A st (J7R2A st ar nA) Z H OS s' σ' m) : + JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac' Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s' a σ' m := by + have wr := F.wr + have hP := J.iplaced + have um : σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory := Fr.2.2 + have keepO : ∀ q o, σ.kheap q = some o → (∀ y k, o ≠ .schedContext y k) → (∀ x, o ≠ .tcb x) → + σ'.kheap q = some o := F.keep.obj + have qk : ∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → K.val ≠ 3464 → K.val ≠ 3480 → K.val ≠ 3488 → + K.val ≠ 3592 → L.mem s' (wa u K) = L.mem s (wa u K) := fun u hu K k1 k2 n1 n2 n3 n4 => + wr.mem_eq (field_aligned (J.sr.core.tcbs u hu) k1 k2) (tcbWordNotMWrA J.sr.toSchedSRN hu k1 n1 n2 n3 n4) + have epq : EpStateRel Z.T E Rp Bf (L.mem s') σ' := + J.ep.frame hP.e (fun b hb hf => wr.mem_eq hb (endpFootOffWrite J.sr.toSchedSRN J.ipcPlace hf)) + ⟨fun q _ h => keepO q _ h (fun _ _ e => by cases e) (fun _ e => by cases e), + fun q x h => ⟨x, keepO q _ h (fun _ _ e => by cases e) (fun _ e => by cases e), rfl⟩, + fun q x h => by + obtain ⟨y, hy, o, rfl⟩ := F.keep.tcb q x h + exact ⟨_, hy, rfl, rfl, rfl, rfl, rfl⟩, um⟩ + have ntq : NtfnStateRel N Z.T (L.mem s') σ' := + J.ntfn.frame hP.n (fun b hb hf => wr.mem_eq hb (notifFootOffMWrA J.sr.toSchedSRN J.ipcPlace.notification hf)) + ⟨fun q _ h => keepO q _ h (fun _ _ e => by cases e) (fun _ e => by cases e), fun q x h => by + obtain ⟨y, hy, o, rfl⟩ := F.keep.tcb q x h + exact ⟨_, hy, rfl, rfl, rfl⟩⟩ + have rpq : ReplyStackRelA (L.mem s') Rp Z.C σ' (scNameA m) stack := by + refine J.reply.frame (fun r hr => ?_) (fun c _ y k h => ?_) (fun r hr x' h => ?_) + · have hr3 := hP.e.rep r hr + have one : ∀ O : U64, O.val = 8 ∨ O.val = 16 → L.mem s' (wa r O) = L.mem s (wa r O) := by + intro O hO + have hv : (wa r O).val = r.val + O.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact wr.mem_eq (by unfold Aligned; rw [hv]; omega) + (otherWordOffMWrA J.sr.toSchedSRN (J.ipcPlace.reply r hr) (by omega) (by omega)) + exact ⟨one _ (Or.inr (by simp [layout.reply.NEXT])), one _ (Or.inl (by simp [layout.reply.PREV]))⟩ + · obtain ⟨y', h', e, -⟩ := hsc _ _ _ h + exact ⟨y', k, h', e⟩ + · obtain ⟨z0, hz0, -⟩ := J.ep.reply r hr + have := keepO _ _ hz0 (fun _ _ e => by cases e) (fun _ e => by cases e) + rw [h] at this + cases this + exact ⟨_, hz0, rfl⟩ + have rvq : RunVcpuInv st Z.T s' := fun u hu h0 => by + have e1 : L.mem s' (runVcpuA u) = L.mem s (runVcpuA u) := by + unfold runVcpuA + exact qk u hu 3672#u64 (by simp) (by simp) (by simp) (by simp) (by simp) (by simp) + have e2 : L.mem s' (Fl4ProofsA64.P1.stA u) = L.mem s (Fl4ProofsA64.P1.stA u) := by + rw [Fl4ProofsA64.P1.stA_eq] + exact qk u hu 3328#u64 (by simp) (by simp) (by simp) (by simp) (by simp) (by simp) + rw [e2]; exact J.runVcpu u hu (e1 ▸ h0) + have bqq : BlkQueuedA Z.T E N (L.mem s') σ' := + J.blkQ.keepA (fun u hu => by + rw [Notif.tBlockA_eq] + exact qk u hu 3336#u64 (by simp) (by simp) (by simp) (by simp) (by simp) (by simp)) + (fun u hu y h => by + obtain ⟨x, hx, -⟩ := J.ep.tcb u hu + obtain ⟨y', hy', o, rfl⟩ := F.keep.tcb _ x hx + rw [h] at hy' + cases hy' + exact Or.inr ⟨x, hx, rfl⟩) + (fun q y h => keepO q _ h (fun _ _ e => by cases e) (fun _ e => by cases e)) + (fun q y h => keepO q _ h (fun _ _ e => by cases e) (fun _ e => by cases e)) + have K : IpcKeepA σ σ' := + ⟨fun q x h => by + obtain ⟨y, hy, o, rfl⟩ := F.keep.tcb q x h + exact ⟨_, hy, rfl, rfl, rfl, rfl, rfl, rfl⟩, + fun q y k h => by + obtain ⟨y', h', -, e⟩ := hsc q y k h + exact ⟨y', k, h', e⟩, + fun q y' k h => by + obtain ⟨y, hy⟩ := F.sz q y' k h + obtain ⟨y2, h2, -, e⟩ := hsc q y k hy + rw [h] at h2 + cases h2 + exact ⟨y, k, hy, e⟩⟩ + have tw : ∀ u ∈ Z.T, ∀ O : U64, O.val < 224 ∨ O.val = 3440 ∨ O.val = 3472 ∨ O.val = 3512 ∨ O.val = 3456 → + O.val % 8 = 0 → L.mem s' (wa u O) = L.mem s (wa u O) := + fun u hu O h h8 => qk u hu O (by omega) h8 (by omega) (by omega) (by omega) (by omega) + exact J.of_ipcStepA (Q := Q) (Rl := Rl) (curOf := Z.curOf) (curScOf := Z.curScOf) + (wr.mono fun _ h => Or.inl (Or.inl h)) Fr.1 Fr.2.1 Qk um (fun _ _ => rfl) bxi sysl symL userOK j7 sr epq ntq rpq K + tw LI.sinv LI.sidle rvq bqq sv + +set_option maxHeartbeats 3200000 in +/-- **`unbind_tcb` of a bound SC at the record** (module docstring). -/ +theorem JStage3A.scUnbindTcbA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (sact : SActA σ) (nsw : NoSwA σ) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + {sc : U64} (hc : sc ∈ Z.C) (hni : NotIdleScA m (scNameA m sc)) (ht0 : L.mem s (scTcbA sc) ≠ 0#u64) : + schedcontext.unbind_tcb st s sc ⦃ s' => ∃ σ' Q Rl, + schedContextUnbindTcb (Z := Unit) (scNameA m sc) σ = ({((), σ')}, False) ∧ + LoopInvA σ' m n ∧ NoSwA σ' ∧ SchedValidA σ' ∧ σ'.curThread = σ.curThread ∧ + (∀ q z, q ≠ specPtr (L.mem s (scTcbA sc)) → σ.kheap q = some (.tcb z) → σ'.kheap q = some (.tcb z)) ∧ + ∀ Ac', BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac' s' → + SysL st H Z.Cs OS ia ie s' → SymL st H Z.Cs s' → Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s' → + J7A st (J7R2A st ar nA) Z H OS s' σ' m → + JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac' Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s' a σ' m ⦄ := by + have hcH : sc ∈ H.scs := by rw [J.scs]; exact hc + have sc0 : sc ≠ 0#u64 := J.scNZ sc hc + have htT0 : L.mem s (scTcbA sc) ∈ Z.T := by rw [← J.tcbs]; exact J.sys.ent.bxi.2.2.1.2.2.yt.1 sc hcH ht0 + have hts0 : L.mem s (scA (L.mem s (scTcbA sc))) = sc := J.sys.ent.bxi.2.2.1.2.2.tsc sc hcH sc0 ht0 + generalize htdef : L.mem s (scTcbA sc) = t at ht0 htT0 hts0 ⊢ + have hon : L.mem s (affA t) = L.cpu s := by + have x := hCs _ (J.sr.core.aff t htT0) + have y := hCs _ J.sr.sch.cpuCs + exact UScalar.eq_of_val_eq (by omega) + have L0 : LoopInvA σ m n := ⟨J.sinv, J.sidle, sact⟩ + obtain ⟨xs, ns, hk, hxr⟩ := J.sr.scr.sc sc hc + have ht0' := (tcb_ne01Sc (J.sr.core.tcbs t htT0)).1 + have hxsT : xs.scTcb = some (specPtr t) := by + rw [hxr.tcb, ← scTcbA_eqSc, htdef, optRefS_ne ht0'] + refine WP.spec_mono (sc_unbind_tcb_localA J.sr J.stEqA hc sc0 htT0 htdef hts0 hon) ?_ + rintro s1 ⟨σ1, Q1, R1, sp1, H1, F1⟩ + have m1 : ((), σ1) ∈ (schedContextUnbindTcb (Z := Unit) (scNameA m sc) σ).1 := by rw [sp1]; exact rfl + have S1 := unbindTcb_stepA L0 hk hni.1 hxsT m1 + have Rp1 := unbindTcb_repA hk hxsT m1 + have Fr : SchedFrameA σ σ1 := + (inferInstance : SPresInst (schedContextUnbindTcb (Z := Unit) (scNameA m sc))).pres σ ((), σ1) m1 + have sv : SchedValidA σ1 := + (schedContextUnbindTcb_fia _ σ ⟨Sched.SchedValidA.toFIA J.schedValid, Sched.JStage3A.rqDomPrioA J⟩ ((), σ1) m1).toValid + have hsc : ScRYKeepA σ σ1 := fun q y k h => by + obtain ⟨y1, e1, r1⟩ := Rp1 q y k h + obtain ⟨y2, e2, f2, -, -⟩ := S1.sc q y k h + rw [e1] at e2 + cases e2 + exact ⟨y1, e1, r1, f2⟩ + exact ⟨σ1, Q1, R1, sp1, S1.inv, S1.ns nsw, sv, S1.cur, unbindTcb_othA hk hxsT m1, fun Ac' bxi sysl symL userOK j7 => + J.of_mfrA F1 Fr (Sched.qk_of m1) H1 hsc S1.inv sv bxi sysl symL userOK j7⟩ + +set_option maxHeartbeats 3200000 in +/-- **`bind_tcb` of an unbound thread and an unbound SC at the record** (module docstring). -/ +theorem JStage3A.scBindTcbA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (sact : SActA σ) (nsw : NoSwA σ) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + {sc t : U64} (hc : sc ∈ Z.C) (ht : t ∈ Z.T) (hni : NotIdleScA m (scNameA m sc)) + (hti : specPtr t ≠ σ.idleThread) (hidles : ∀ c < n, m.idleThreadOf c ≠ specPtr t) + (ht0 : L.mem s (scA t) = 0#u64) (hs0 : L.mem s (scTcbA sc) = 0#u64) (hcb : CurBA σ) + (hb : ∀ x y k, σ.kheap (specPtr t) = some (.tcb x) → σ.kheap (scNameA m sc) = some (.schedContext y k) → + ipcQueuedThreadState x.tcbState = true → scReleased σ.curTime y = true) : + schedcontext.bind_tcb st ar s a sc t ⦃ r => r.2 = a ∧ ∃ σ' Q Rl, + schedContextBindTcb (Z := Unit) (scNameA m sc) (specPtr t) σ = ({((), σ')}, False) ∧ + LoopInvA σ' m n ∧ NoSwA σ' ∧ SchedValidA σ' ∧ ScRYKeepA σ σ' ∧ + (∀ q z, q ≠ specPtr t → σ.kheap q = some (.tcb z) → σ'.kheap q = some (.tcb z)) ∧ + ∀ Ac', BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac' r.1 → + SysL st H Z.Cs OS ia ie r.1 → SymL st H Z.Cs r.1 → Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U r.1 → + J7A st (J7R2A st ar nA) Z H OS r.1 σ' m → + JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac' Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) r.1 a σ' m ⦄ := by + have sc0 : sc ≠ 0#u64 := J.scNZ sc hc + have hon : L.mem s (affA t) = L.cpu s := by + have x := hCs _ (J.sr.core.aff t ht) + have y := hCs _ J.sr.sch.cpuCs + exact UScalar.eq_of_val_eq (by omega) + have hcpu : L.mem s (scCoreA sc) = L.cpu s := by + rw [J.sr.scCore sc hc] + have x := J.scCpu _ hc + have y := hCs _ J.sr.sch.cpuCs + rw [h1] at x y + exact UScalar.eq_of_val_eq (by omega) + obtain ⟨xt, hkt, hxo⟩ := J.sr.sch.tcb t ht + have hxn : xt.tcbSchedContext = none := by rw [hxo, ht0, optScA, if_pos rfl] + obtain ⟨y, k, hky, hy⟩ := J.sr.scr.sc sc hc + have hyn : y.scTcb = none := by rw [hy.tcb, ← scTcbA_eqSc, hs0, optRefS_zero] + obtain ⟨xc, hxc, hcs⟩ := hcb + have htc : specPtr t ≠ σ.curThread := fun e => by + rw [e, hxc] at hkt; cases hkt; rw [hcs] at hxn; cases hxn + have hpc : scNameA m sc ≠ σ.curSc := fun e => by + obtain ⟨yc, jc, hyc, hyt⟩ := J.sr.inv.back _ _ _ hxc hcs + rw [← e, hky] at hyc; cases hyc; rw [hyn] at hyt; cases hyt + have hnq : runnable xt.tcbState = true → ∀ d p, specPtr t ∉ σ.readyQueues d p := fun _ d p hm => by + obtain ⟨⟨x', sc', e, es, -⟩, -⟩ := J.schedValid.rq d p _ hm + rw [hkt] at e; cases e; rw [hxn] at es; cases es + have hb1 : runnable xt.tcbState = false → inClassRSc xt.tcbState xt.tcbFault → + scActive y = true → refillReady σ.curTime (refillHd y) = true := fun hr c a' => by + have e := hb _ _ _ hkt hky (bind_inClass_blocked'A hr c) + rw [scReleased, a', Bool.true_and] at e + exact e + have hact : BlockedTsA xt.tcbState = true → scActive y = true := fun hbl => by + have e := hb _ _ _ hkt hky (blockedTs_ipcQueuedA hbl) + simp only [scReleased, Bool.and_eq_true] at e + exact e.1 + refine WP.spec_mono (sc_bind_tcb_localA J.sr J.stEqA a hmb ht hc sc0 ht0 hs0 hon hcpu hni hkt hky hnq hb1) ?_ + rintro ⟨s', a'⟩ ⟨ea, σ', Q', R', hspec, HF, F⟩ + have hm : ((), σ') ∈ (schedContextBindTcb (Z := Unit) (scNameA m sc) (specPtr t) σ).1 := by rw [hspec]; exact rfl + obtain ⟨LI, NS, SV, RY, OT⟩ := bindTcb_specA ⟨J.sinv, J.sidle, sact⟩ nsw J.schedValid (Sched.JStage3A.rqDomPrioA J) + hkt hky hxn hyn hni.1 hti htc hidles hpc hact hm + have Fr : SchedFrameA σ σ' := + (inferInstance : SPresInst (schedContextBindTcb (Z := Unit) (scNameA m sc) (specPtr t))).pres σ ((), σ') hm + exact ⟨ea, σ', Q', R', hspec, LI, NS, SV, RY, OT, fun Ac' bxi sysl symL userOK j7 => + J.of_mfrA F Fr (Sched.qk_of hm) HF RY LI SV bxi sysl symL userOK j7⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3ScCfgRecA.lean b/proofs/Fl4ProofsA64/P3/Stage3ScCfgRecA.lean new file mode 100644 index 000000000..ab4d002ad --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ScCfgRecA.lean @@ -0,0 +1,513 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 3d): **SchedControl ConfigureFlags at the record** (`JSyscallA.scCfgA`). + +`decode_invocation` on a SchedControl cap at a syscall entry: `decode_sched_control` writes the caller's `Restart` and +runs `configure_flags` on the extra cap's SC (`cfg_configure_flagsA`); the spec invariants are the walk of +`P3/Stage3ScCfgSpecA`. The record's active-SC set changes (to `Ac'`, from `decodeFinA`); `κ` does not (one core: +the SC's `CORE` word is `0` before and after). The spec effect pins `commit_time`'s zeroed `consumedTime`. + +Named premises: +* [P] `ArchTotal`, `MaxSpi`, `ArchPlat`, `A64FrameMasks`, `hCs`, `hT` (timer agreement), `hAgree` (minimum budget); +* [S] `h1` (`numNodes = 1`: same core, the remote stall), `hdec` (the spec decoder accepts; the `Err` returns are not + lifted). + +Derived from the record (no longer premises): the cap's core is the current one (`CapTyA`'s `SchedControl` clause, +`numNodes = 1`), the bound thread names the SC back (`ScExtra.tsc`, l4v `sym_refs`), the spec SC has the heap object's +size (`JStage3A.scSize`'s non-idle equality: the SC named by an OS cap is no idle SC). + +The arm ends in `Sched.BodyEndA` (the record's `BodyOKA` and the kept current thread): the current SC's head facts at +the end are `configure_flags`' own `OffReadyA` / `OffOKA` (`cfg_configure_flagsA`), the clock is kept (`cfg_tmA`). + +Decode-site case hypotheses (not record facts, as in `P3/CnodeCopyA` and the VSpace arms): `hex` (`ExtraRelA`, the +spec's extra caps against FL4's extra slots: the front end's `ExtraOKA.rel1`), `hlen` / `hargs` (the message words), +`hslbl` (the spec label). + +New module. +-/ +import Fl4ProofsA64.P3.Stage3ScCfgSpecA +import Fl4ProofsA64.P3.ScCfgTopA +import Fl4ProofsA64.P3.Stage3IpcGlueA +import Fl4ProofsA64.P3.Stage3McsFrA +import Fl4ProofsA64.P3.Stage3SchedExitFrA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {s : S} {a : A} {σ : State Unit} {m : McState} + +/-- **The record gives the walk's ready-queue placement** (`SchedRestA.rq` / `rqDom` / `prio`, `SchedCoreA.inQ`). -/ +theorem rqDomPrio_ofSRA (H' : SchedSRA st Z m κ s σ) : RqDomPrio σ := by + intro f d p x hx hm + have hd : d = 0 := by + by_contra h + rw [H'.rest.rqDom d p h] at hm + cases hm + subst hd + obtain ⟨q, hq⟩ : ∃ q : U64, q = ⟨p.setWidth 64⟩ := ⟨_, rfl⟩ + have vq : q.val = p.toNat := by + rw [hq] + show (p.setWidth 64).toNat = _ + rw [BitVec.toNat_setWidth] + exact Nat.mod_eq_of_lt (lt_trans p.isLt (by decide)) + have q256 : q.val < 256 := by rw [vq]; exact p.isLt + have ep : BitVec.ofNat 8 q.val = p := by rw [vq]; simp + have h := H'.rest.rq q q256 + rw [ep] at h + rw [h] at hm + obtain ⟨u, hu, e⟩ := List.mem_map.1 hm + have hc := H'.sch.cpuCs + have huT : u ∈ Z.T := H'.core.memT hc q256 hu + obtain ⟨y, hy, hdom, -, hpr⟩ := H'.rest.prio u huT + rw [e, hx] at hy + cases hy + have hin := (H'.core.inQ _ hc q q256 u hu).2 + refine ⟨hdom.symm, BitVec.eq_of_toNat_eq ?_⟩ + rw [← vq, ← hpr, hin] + +/-! ### The clock across ConfigureFlags (`TmKeepA`: current time, timer flag, last machine time) -/ + +instance (p : ObjRef) : TmInst (maybeAddEmptyTail (Z := Unit) p) := ⟨by unfold maybeAddEmptyTail; tmres⟩ +instance (p : ObjRef) (k : Nat) (b per : Ticks) : TmInst (refillNew (Z := Unit) p k b per) := + ⟨by unfold refillNew; tmres⟩ +instance (p : ObjRef) (per b : Ticks) (k : Nat) : TmInst (refillUpdate (Z := Unit) p per b k) := + ⟨by unfold refillUpdate; tmres⟩ +instance {α β : Type} [Nonempty β] (t : α) (ts : List α) (f : α → RMonad Unit β) (R : β → β → Prop) : + TmInst (orderedInsert (Z := Unit) t ts f R) := ⟨by unfold orderedInsert; tmres⟩ +instance (t : ObjRef) : TmInst (tcbReleaseEnqueue (Z := Unit) t) := + ⟨by unfold tcbReleaseEnqueue readyTimesAppend; tmres⟩ +instance (p : ObjRef) : TmInst (postpone (Z := Unit) p) := ⟨by unfold postpone; tmres⟩ +instance (p : ObjRef) : TmInst (fl4SchedContextResume (Z := Unit) p) := ⟨by unfold fl4SchedContextResume; tmres⟩ + +/-- ConfigureFlags keeps the current time and the machine's last time. -/ +theorem cfg_tmA (iv : SchedControlInvocation) : TmPresA (invokeFl4SchedControlConfigureFlags (Z := Unit) iv) := by + unfold invokeFl4SchedControlConfigureFlags + cases iv + tmres + +/-- **SchedControl ConfigureFlags at a syscall entry** (module docstring). -/ +theorem JSyscallA.scCfgA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hT : ScCfgTimeAgreeA ar a) (hAgree : fl4MinBudget = MIN_BUDGET) + {slot lbl len buf cpu : U64} {blk call don : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .SchedControl cpu) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hlbl : lbl = fl4_abi.label.SchedControlConfigureFlags) + {slbl : Data} (hslbl : genInvocationType slbl = .schedControlConfigureFlags) + {args : List U64} (hlen : len.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) + {x0 p sb : U64} (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) (hx0n : x0 ≠ 0#u64) + (hc0 : capA st s x0 = .SchedContext p sb) + {excaps : List Fl4SpecA64.Cap} (hex : ExtraRelA Z.th (L.mem s) x0 excaps) + {bS pS : Ticks} {mr : Nat} {bd fl : Word} + (hdec : (decodeFl4SchedControlInvocation (Z := Unit) slbl (args.map UScalar.bv) excaps).run σ = + ({(.ok (.invokeSchedControlConfigureFlags (specPtr p) bS pS mr bd fl), σ)}, False)) : + ∃ xc σ1 σ2 s2, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + invokeFl4SchedControlConfigureFlags (Z := Unit) + (.invokeSchedControlConfigureFlags (specPtr p) bS pS mr bd fl) σ1 = ({((), σ2)}, False) ∧ + (∃ y k, σ1.kheap (specPtr p) = some (.schedContext y k) ∧ + σ2.consumedTime = if y.scTcb ≠ none ∧ σ1.curSc = specPtr p then 0 else σ1.consumedTime) ∧ + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (.Ok (), s2, a) ∧ + ∃ Q Rl Ac', JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac' Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s2 a σ2 m ∧ Sched.BodyEndA σ σ2 m := by + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hslot hcov J.actW J.running J.cur + have hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM := J.toJStage3EntryA.curNotVmA + obtain ⟨xc, hkc, hst, hsp, J1, -, -, -⟩ := J.toJStage3A.of_restartCurA RS.pl.choose_spec.choose_spec.choose_spec.choose_spec + RS.sym RS.run hnvm RS.cur RS.vs RS.vc + have T0 := J.toJStage3A.toTA J.sact J.noSwitch + -- the SchedControl cap's core (`CapTyA`: below `numNodes = 1`, so the current core) + have hcs : cpu ∈ Z.Cs := by + have k := (T0.capTy slot hslot).2.2.2.2 cpu hcap + have c0 := hCs _ J.sr.sch.cpuCs + rw [h1] at k c0 + have e : cpu = L.cpu s := UScalar.eq_of_val_eq (by omega) + rw [e] + exact J.sr.sch.cpuCs + -- the extra cap's SC + have hxm : x0 ∈ extra.val := by + have hl : (0#usize : Usize).val < extra.val.length := by rw [extra.property]; simp + obtain ⟨y, hy, e⟩ := spec_existsA (Aeneas.Std.Array.index_usize_spec extra 0#usize hl) + rw [ok_inj_ff (hx0.symm.trans hy), e] + exact List.mem_of_getElem? (i := 0) (by simp [hl]) + have hxO : x0 ∈ OS := hext x0 hxm hx0n + have hH : H p = some (.sc sb.val) := (T0.capTy x0 hxO).2.2.2.1 p sb hc0 + have hpS : p ∈ H.scs := ⟨_, hH⟩ + have hc : p ∈ Z.C := by rw [← J.scs]; exact hpS + have hci : specPtr p ≠ m.idleScOf m.curCore := T0.ciSc x0 hxO p sb hc0 + have hname : scNameA m p = specPtr p := by unfold scNameA; rw [if_neg hci] + have hp0 : p ≠ 0#u64 := J.scNZ p hc + -- the SC's bound thread names it back (`TSc`, l4v `sym_refs` for `sc_tcb` / `tcb_sched_context`) + have hbound : L.mem s (scTcbA p) ≠ 0#u64 → L.mem s (scA (L.mem s (scTcbA p))) = p := + J.sys.ent.bxi.2.2.1.2.2.tsc p hpS hp0 + have hni : NotIdleScA m (scNameA m p) := by + rw [hname] + refine ⟨J.sr.sch.scOff p hc hci, fun c hcN => ?_⟩ + have hcc : m.curCore = 0 := Nat.lt_one_iff.mp (lt_of_lt_of_eq (T0.curCore_lt hCs) h1) + have hc1 : c = 0 := Nat.lt_one_iff.mp (lt_of_lt_of_eq hcN h1) + have e : c = m.curCore := hc1.trans hcc.symm + rw [e] + exact hci + have hsb64 : sb.val < 64 := by + obtain ⟨hP, -⟩ := J.sys.ent.bxi.2.2.1.1.sc p hpS + have h64 := hP.lt64 + have e : H.scBits p = sb.val := by simp [Heap.scBits, hH] + omega + -- the spec SC's size (record edit #8b-D (a): equality at a non-idle SC) + have hsize : ∀ y k, σ.kheap (scNameA m p) = some (.schedContext y k) → k + minSchedContextBits = sb.val := + fun y k h => ((J.toJStage3A.scSize p hc y k h).2 hni).trans (by simp [Heap.scBits, hH]) + have hscB : ∀ x ∈ extra.val, x ≠ 0#u64 → ∀ q sz, capA st s x = .SchedContext q sz → sz.val = H.scBits q := by + intro x hx hxn q sz h + have e := (T0.capTy x (hext x hx hxn)).2.2.2.1 q sz h + simp [Heap.scBits, e] + -- the caller's restart + obtain ⟨c0, hc0d⟩ : ∃ c, c = L.curW s := ⟨_, rfl⟩ + obtain ⟨s1, hs1⟩ : ∃ q, q = L.wr s (stA c0) sched.TS_RESTART := ⟨_, rfl⟩ + obtain ⟨σ1, hσ1⟩ : ∃ q, q = stUpdA σ σ.curThread xc .restart := ⟨_, rfl⟩ + rw [← hc0d] at hst J1 hargs + rw [← hs1] at hst J1 + rw [← hσ1] at hsp J1 + have hcZ : c0 ∈ Z.T := by rw [hc0d]; exact J.sr.curT + have hca : TcbAddr c0 := J.sr.core.tcbs _ hcZ + have sA : Aligned (stA c0) := by rw [stA_eq]; exact field_aligned hca (by simp) (by simp) + have farT : ∀ u : U64, TcbAddr u → ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → (u = c0 → K.val ≠ 3328) → + L.mem s1 (wa u K) = L.mem s (wa u K) := fun u hu K k1 k2 k3 => by + rw [hs1] + refine mem_wr_ne' sA (field_aligned hu k1 k2) ?_ + rw [stA_eq] + exact field_ne (K2 := K) hca hu (by simp) k1 (fun e => by simpa using Ne.symm (k3 e.symm)) + have sact1 : SActA σ1 := + ⟨fun u h => by rw [hσ1] at h ⊢; exact J.sact.f1 u h, + ⟨{ xc with tcbState := .restart }, by simp only [hσ1, stUpdA, setObjA, Function.update_self], + Or.inr (Or.inl rfl)⟩⟩ + have nsw1 : ∀ u, σ1.schedulerAction ≠ .switchThread u := fun u h => J.noSwitch u (by rw [hσ1] at h; exact h) + have act1 : σ1.schedulerAction = .resumeCurThread := by rw [hσ1]; exact J.act + have ccur : σ.curThread = specPtr c0 := by rw [hc0d]; exact J.j.1.sch.curThread + have hrun : runnable xc.tcbState = true := by + have hcode := J.yieldQueueA.states c0 hcZ xc (ccur ▸ hkc) + have hr := J.running + rw [← hc0d] at hr + have hv := hnvm + rw [← hc0d] at hv + rcases hr with h | h | h + · rw [h, ts_vals.1] at hcode + revert hcode + cases xc.tcbState with + | running => intro _; rfl + | restart => intro _; rfl + | ext e => cases e; simp [tsCodeA] + | _ => simp [tsCodeA] + · rw [h, ts_vals.2.1] at hcode + revert hcode + cases xc.tcbState with + | running => intro _; rfl + | restart => intro _; rfl + | ext e => cases e; simp [tsCodeA] + | _ => simp [tsCodeA] + · exact absurd h hv + obtain ⟨-, I1, O1, R1, Ac1⟩ := cfgRestartTimingA (st := st) J.sr J.curInv J.offOK J.offReady J.actIdle ccur + (ccur ▸ hkc) hrun (ccur ▸ hsp) + -- the SC and its bound thread + obtain ⟨y0, n0, hk0, hy0⟩ := J.sr.scr.sc p hc + obtain ⟨y1, n1, hk1, hy1⟩ := J1.sr.scr.sc p hc + have hkk : σ1.kheap (scNameA m p) = some (.schedContext y0 n0) := by + simp only [hσ1, stUpdA, setObjA] + rw [Function.update_of_ne (fun e => by rw [e, hkc] at hk0; cases hk0)]; exact hk0 + rw [hkk] at hk1 + cases hk1 + have optInj : ∀ u v : U64, optRefS u = optRefS v → u = v := fun u v h => by + by_cases hu : u = 0#u64 <;> by_cases hv : v = 0#u64 + · exact hu.trans hv.symm + · rw [hu, optRefS_zero, optRefS_ne hv] at h; cases h + · rw [hv, optRefS_zero, optRefS_ne hu] at h; cases h + · rw [optRefS_ne hu, optRefS_ne hv] at h + exact specPtr_inj (Option.some.inj h) + have hT1 : L.mem s1 (scTcbA p) = L.mem s (scTcbA p) := by + apply optInj + rw [scTcbA_eqSc, ← hy1.tcb, ← hy0.tcb] + have hbx1 := J1.sys.ent.bxi + have hcpu1 : L.cpu s1 = cpu := by + have c1 := hCs _ J1.sr.sch.cpuCs + have c2 := hCs _ hcs + exact UScalar.eq_of_val_eq (by omega) + have hcore : L.mem s1 (scCoreA p) = L.cpu s1 := by + rw [J1.sr.scCore p hc] + have c1 := J1.scCpu p hc + have c2 := hCs _ J1.sr.sch.cpuCs + exact UScalar.eq_of_val_eq (by omega) + have hκ : Function.update κ p cpu = κ := by + rw [← hcpu1, ← hcore, J1.sr.scCore p hc] + exact Function.update_eq_self _ _ + have bound : ∀ t, L.mem s1 (scTcbA p) = t → t ≠ 0#u64 → + t ∈ Z.T ∧ L.mem s1 (scA t) = p ∧ + ∀ z, σ1.kheap (specPtr t) = some (.tcb z) → z.tcbSchedContext = some (scNameA m p) := by + intro t ht h0 + have es : L.mem s (scTcbA p) = t := hT1.symm.trans ht + have htZ : t ∈ Z.T := by + rw [← es, ← J.tcbs]; exact T0.blkA.2.2.1.2.2.yt.1 p hpS (es ▸ h0) + have hta : TcbAddr t := J.sr.core.tcbs t htZ + have hb : L.mem s1 (scA t) = p := by + refine (farT t hta 3464#u64 (by simp) (by simp) (fun _ => by simp)).trans ?_ + have h := hbound (es ▸ h0) + rw [es] at h + exact h + refine ⟨htZ, hb, fun z hz => ?_⟩ + obtain ⟨z', h⟩ := J1.sr.sch.tcb t htZ + have hz' := h.1 + have hsc := h.2 + have ez : z' = z := KernelObject.tcb.inj (Option.some.inj (hz'.symm.trans hz)) + rw [← ez, hsc, hb] + unfold optScA + rw [if_neg hp0] + rfl + have hLoc : CfgOwnerLocalA (st := st) (ar := ar) Z m s1 a σ1 p := by + intro h0 + obtain ⟨htZ, hb, hz⟩ := bound (L.mem s1 (scTcbA p)) rfl h0 + have affS : ScAff st H.tcbs s1 := hbx1.2.2.1.2.2.aff + have eaff := affS.core_eq (by rw [J.tcbs]; exact htZ) (fun h => hp0 (hb.symm.trans h)) + rw [hb] at eaff + exact ⟨htZ, eaff.trans hcore, hcore, hz, + (J1.toTA sact1 nsw1).stall_singleA hCs h1 htZ⟩ + -- the spec decoder's invocation and `configure_flags` + have hct : sched.cur_thread st s = ok c0 := by rw [hc0d]; exact cur_thread_ok s + have D := cfg_decode_sched_controlA (st := st) (ar := ar) Z.th s a hT σ lbl slbl (iff_of_true hslbl hlbl) cpu len + buf c0 hct args hlen hargs extra x0 hx0 excaps hex + (fun _ q z h => by + obtain ⟨-, e⟩ := capability.Cap.SchedContext.inj (hc0.symm.trans h) + exact e ▸ hsb64) + (fun inv r => ∃ (budget period num bd' fl' : U64) (τ : State Unit) (Q : U64 → U64 → List U64) + (R : U64 → List U64), + inv = .invokeSchedControlConfigureFlags (specPtr p) budget.bv period.bv num.val bd'.bv fl'.bv ∧ + 0 < num.val ∧ r.1 = .Ok () ∧ r.2.2 = a ∧ + invokeFl4SchedControlConfigureFlags (Z := Unit) + (.invokeSchedControlConfigureFlags (scNameA m p) budget.bv period.bv num.val bd'.bv fl'.bv) σ1 = + ({((), τ)}, False) ∧ + SchedSRA st {Z with Q := Q, R := R} m (Function.update κ p cpu) r.2.1 τ ∧ + McpRelA Z.T m (L.mem r.2.1) τ ∧ YieldRelA Z.T Z.C m (L.mem r.2.1) τ ∧ + CurInvA τ ∧ OffOKA τ ∧ OffReadyA τ ∧ ActIdleA τ ∧ τ.curThread = σ1.curThread ∧ τ.curSc = σ1.curSc ∧ + MFrA (st := st) Z m s1 r.2.1 σ1 τ) + (by + intro q qb bu pu er bd' fl' budget period _ hC _ _ _ _ _ hBu hPe hMb hBp hRef + obtain ⟨eq, eb⟩ := capability.Cap.SchedContext.inj (hc0.symm.trans hC) + subst eq eb + obtain ⟨num, hnum⟩ : ∃ x, x = core.num.U64.wrapping_add er sporadic.MIN_REFILLS := ⟨_, rfl⟩ + have maxEq : Fl4SpecA64.maxNumRefills sb.val = absMax sb.val := by + unfold absMax Fl4SpecA64.maxNumRefills Fl4SpecA64.sizeofSchedContextT Fl4SpecA64.wordSize + Fl4SpecA64.refillSizeBytes + rw [if_neg (by omega)] + have maxBound : absMax sb.val < 2 ^ 64 := by + unfold absMax + rw [if_neg (by omega)] + have hp := Nat.pow_lt_pow_right (by decide : 1 < 2) hsb64 + omega + have vNum : num.val = er.val + 2 := by + have v2 : sporadic.MIN_REFILLS.val = 2 := by unfold sporadic.MIN_REFILLS; rfl + rw [maxEq] at hRef + rw [hnum, wadd_val (by rw [v2]; omega), v2] + have hm : num.val ≤ absMax (n0 + minSchedContextBits) := by + rw [hsize _ _ hk0, ← maxEq, vNum]; exact hRef + rw [hst, bind_ok, ← hnum] + refine WP.spec_bind (cfg_configure_flagsA J1.yieldQueueA.sched J1.yieldQueueA.states J1.yieldQueueA.mcp + J1.yieldQueueA.links I1 O1 R1 Ac1 a hAgree hT hc hp0 + hcpu1.symm hni hLoc hkk budget period num bd' fl' hMb hBp (by omega) hm) ?_ + rintro ⟨s2, a2⟩ ⟨ea, τ, Q, R, e, H2, -, M2, Y2, I2, O2, R2, A2, hCt2, hCs2, W2⟩ + simp only [uncurry_apply_pair, WP.spec_ok] + exact ⟨budget, period, num, bd', fl', τ, Q, R, by rw [hBu, hPe, vNum], by omega, trivial, ea, e, H2, M2, Y2, + I2, O2, R2, A2, hCt2, hCs2, W2⟩) + obtain ⟨⟨rr, s2, a2⟩, edec, hpost⟩ := exists_of_specA D + rcases hpost with ⟨e, e', -, herr, -⟩ | ⟨inv, hinv, budget, period, num, bd', fl', τ, Q, Rl, einv, hnum, hr1, hr2, + hrun2, H2, M2, Y2, I2, O2, R2, A2, hCt2, hCs2, W2⟩ + · rw [hdec] at herr + have := (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst herr))).1 + cases this + rw [hdec] at hinv + have ei := (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst hinv))).1 + cases ei + injection einv with _ eb ep em ebd efl + subst eb ep em ebd efl + change rr = .Ok () at hr1 + change a2 = a at hr2 + change MFrA (st := st) Z m s1 s2 σ1 τ at W2 + subst hr1 hr2 + rw [hname] at hrun2 + rw [hκ] at H2 + have hdecC : syscall.decode_invocation st ar s a2 lbl len slot (capA st s slot) blk call don false buf extra = + ok (.Ok (), s2, a2) := by + unfold syscall.decode_invocation + rw [hct, bind_ok, hcap] + exact edec + -- the spec walk + rw [hname] at hkk + have hbt : ∀ t, y0.scTcb = some t → ∃ z, σ1.kheap t = some (.tcb z) ∧ z.tcbSchedContext = some (specPtr p) := by + intro t ht + have e : y0.scTcb = optRefS (L.mem s1 (scTcbA p)) := by rw [hy1.tcb, ← scTcbA_eqSc] + rw [e] at ht + by_cases h0 : L.mem s1 (scTcbA p) = 0#u64 + · rw [h0, optRefS_zero] at ht; cases ht + rw [optRefS_ne h0] at ht + cases ht + obtain ⟨htZ, -, hz⟩ := bound _ rfl h0 + obtain ⟨z, h⟩ := J1.sr.sch.tcb _ htZ + have hk := h.1 + exact ⟨z, hk, (hz z hk).trans (congrArg some hname)⟩ + have hmem : ((), τ) ∈ (invokeFl4SchedControlConfigureFlags (Z := Unit) + (.invokeSchedControlConfigureFlags (specPtr p) budget.bv period.bv num.val bd'.bv fl'.bv) σ1).1 := by + rw [hrun2]; exact Set.mem_singleton _ + obtain ⟨Lτ, Vτ, P⟩ := cfg_spec_walkA hAgree ⟨J1.sinv, J1.sidle, sact1⟩ J1.schedValid (rqDomPrio_ofSRA J1.sr) act1 + hkk hnum hbt + (fun e => by + obtain ⟨z, hz, hb, -⟩ := I1.tcb + obtain ⟨y, j, hs, hy⟩ := J1.sr.inv.back _ _ _ hz hb + rw [e, hkk] at hs + cases hs + rw [hy] + exact Option.some_ne_none _) + (by rw [← hname]; exact hni.1) hmem + obtain ⟨yf, hyf, F, hact, -⟩ := P.sc + -- objects across the walk + have keepO : ∀ q o, σ1.kheap q = some o → (∀ z k, o ≠ .schedContext z k) → τ.kheap q = some o := fun q o h ho => by + rw [P.other q (fun e => by rw [e, hkk] at h; exact ho _ _ (Option.some.inj h).symm)]; exact h + have backO : ∀ q o, τ.kheap q = some o → (∀ z k, o ≠ .schedContext z k) → σ1.kheap q = some o := fun q o h ho => by + rw [← P.other q (fun e => by rw [e, hyf] at h; exact ho _ _ (Option.some.inj h).symm)]; exact h + have keepT : ∀ q z, σ1.kheap q = some (.tcb z) → τ.kheap q = some (.tcb z) := fun q z h => + keepO q _ h (fun _ _ e => by cases e) + have scF : ∀ q y k, σ1.kheap q = some (.schedContext y k) → ∃ y', τ.kheap q = some (.schedContext y' k) ∧ + y'.scReplies = y.scReplies ∧ y'.scYieldFrom = y.scYieldFrom := fun q y k h => by + by_cases e : q = specPtr p + · rw [e, hkk] at h + cases h + exact ⟨yf, by rw [e]; exact hyf, F.2.1, F.2.2.1⟩ + · exact ⟨y, by rw [P.other q e]; exact h, rfl, rfl⟩ + have scB : ∀ q y' k, τ.kheap q = some (.schedContext y' k) → ∃ y, σ1.kheap q = some (.schedContext y k) ∧ + y'.scReplies = y.scReplies ∧ y'.scYieldFrom = y.scYieldFrom ∧ (scActive y = true → scActive y' = true) := + fun q y' k h => by + by_cases e : q = specPtr p + · rw [e, hyf] at h + cases h + exact ⟨y0, by rw [e]; exact hkk, F.2.1, F.2.2.1, fun _ => hact⟩ + · exact ⟨y', by rw [← P.other q e]; exact h, rfl, rfl, id⟩ + have Fr := mcs_cfg_frameA hsp (σ := σ) hrun2 + have F1 : SchedFrameA σ1 τ := + frame_of_runA (invokeFl4SchedControlConfigureFlags_presA _) hrun2 + have Qk : QKeepA σ1 τ := QKeepA.ofBwd (fun q o h => by + by_cases e : q = specPtr p + · rw [e, hyf] at h + cases h + exact ⟨.schedContext y0 n0, by rw [e]; exact hkk, QObjA.ofSc fun hr => ⟨F.2.1 ▸ hr, fun _ => hact⟩⟩ + · exact ⟨o, by rw [← P.other q e]; exact h, QObjA.refl o⟩) + P.dom + -- the concrete words + have wr := W2.wr + have qk : ∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → K.val ≠ 3464 → K.val ≠ 3480 → K.val ≠ 3488 → + K.val ≠ 3592 → L.mem s2 (wa u K) = L.mem s1 (wa u K) := fun u hu K k1 k2 n1 n2 n3 n4 => + wr.mem_eq (field_aligned (J1.sr.core.tcbs u hu) k1 k2) (tcbWordNotMWrA J1.sr.toSchedSRN hu k1 n1 n2 n3 n4) + have hP := J1.iplaced + have um : τ.machineState.underlyingMemory = σ1.machineState.underlyingMemory := F1.2.2 + have epq : EpStateRel Z.T E Rp Bf (L.mem s2) τ := + J1.ep.frame hP.e (fun b hb hf => wr.mem_eq hb (endpFootOffWrite J1.sr.toSchedSRN J1.ipcPlace hf)) + ⟨fun q _ h => keepO q _ h (fun _ _ e => by cases e), + fun q x h => ⟨x, keepO q _ h (fun _ _ e => by cases e), rfl⟩, + fun q x h => ⟨x, keepT q x h, rfl, rfl, rfl, rfl, rfl⟩, um⟩ + have ntq : NtfnStateRel N Z.T (L.mem s2) τ := + J1.ntfn.frame hP.n (fun b hb hf => wr.mem_eq hb (notifFootOffMWrA J1.sr.toSchedSRN J1.ipcPlace.notification hf)) + ⟨fun q _ h => keepO q _ h (fun _ _ e => by cases e), fun q x h => ⟨x, keepT q x h, rfl, rfl, rfl⟩⟩ + have rpq : ReplyStackRelA (L.mem s2) Rp Z.C τ (scNameA m) stack := by + refine J1.reply.frame (fun r hr => ?_) (fun c _ y k h => ?_) + (fun r _ x' h => ⟨x', backO _ _ h (fun _ _ e => by cases e), rfl⟩) + · have hr3 := hP.e.rep r hr + have one : ∀ O : U64, O.val = 8 ∨ O.val = 16 → L.mem s2 (wa r O) = L.mem s1 (wa r O) := by + intro O hO + have hv : (wa r O).val = r.val + O.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact wr.mem_eq (by unfold Aligned; rw [hv]; omega) + (otherWordOffMWrA J1.sr.toSchedSRN (J1.ipcPlace.reply r hr) (by omega) (by omega)) + exact ⟨one _ (Or.inr (by simp [layout.reply.NEXT])), one _ (Or.inl (by simp [layout.reply.PREV]))⟩ + · obtain ⟨y', h', e, -⟩ := scF _ _ _ h + exact ⟨y', k, h', e⟩ + have rvq : RunVcpuInv st Z.T s2 := fun u hu h0 => by + have e1 : L.mem s2 (runVcpuA u) = L.mem s1 (runVcpuA u) := by + unfold runVcpuA + exact qk u hu 3672#u64 (by simp) (by simp) (by simp) (by simp) (by simp) (by simp) + have e2 : L.mem s2 (Fl4ProofsA64.P1.stA u) = L.mem s1 (Fl4ProofsA64.P1.stA u) := by + rw [Fl4ProofsA64.P1.stA_eq] + exact qk u hu 3328#u64 (by simp) (by simp) (by simp) (by simp) (by simp) (by simp) + rw [e2]; exact J1.runVcpu u hu (e1 ▸ h0) + have bqq : BlkQueuedA Z.T E N (L.mem s2) τ := + J1.blkQ.keepA (fun u hu => by + rw [Notif.tBlockA_eq] + exact qk u hu 3336#u64 (by simp) (by simp) (by simp) (by simp) (by simp) (by simp)) + (fun u _ y h => Or.inr ⟨y, backO _ _ h (fun _ _ e => by cases e), rfl⟩) + (fun q y h => keepO q _ h (fun _ _ e => by cases e)) + (fun q y h => keepO q _ h (fun _ _ e => by cases e)) + have K : IpcKeepA σ1 τ := + ⟨fun q x h => ⟨x, keepT q x h, rfl, rfl, rfl, rfl, rfl, rfl⟩, + fun q y k h => by + obtain ⟨y', h', -, e⟩ := scF q y k h + exact ⟨y', k, h', e⟩, + fun q y' k h => by + obtain ⟨y, h', -, e, -⟩ := scB q y' k h + exact ⟨y, k, h', e⟩⟩ + have tw : ∀ u ∈ Z.T, ∀ O : U64, O.val < 224 ∨ O.val = 3440 ∨ O.val = 3472 ∨ O.val = 3512 ∨ O.val = 3456 → + O.val % 8 = 0 → + L.mem s2 (wa u O) = L.mem s1 (wa u O) := + fun u hu O h h8 => qk u hu O (by omega) h8 (by omega) (by omega) (by omega) (by omega) + obtain ⟨-, Ac', sysl, symL, bxi, userOK, j7⟩ := J.decodeFinA hA hM hPl hfm hslot hcov + (rfl : capA st s slot = capA st s slot) hext hSC (lbl := lbl) + (by rw [hcap, hlbl]; simp [DecArmS1aEnt, DecArmQH, DecArmQZ2, DecArmKQ2, DecArmKQ, DecArmKQ3, DecArmDelQ, + DecArmS1aC, capFam]) + (fun _ h => by rw [hcap] at h; cases h) + (fun c h => by + have e : cpu = c := capability.Cap.SchedControl.inj (hcap.symm.trans h) + exact ⟨e ▸ hcs, hscB⟩) hdecC τ m + have J2 := J1.of_ipcStepA (Q := Q) (Rl := Rl) (curOf := Z.curOf) (curScOf := Z.curScOf) + (wr.mono fun _ h => Or.inl (Or.inl h)) F1.1 F1.2.1 Qk um (fun _ _ => rfl) bxi sysl symL userOK j7 H2 epq ntq rpq K + tw Lτ.sinv Lτ.sidle rvq bqq Vτ + -- the body-end facts + have Tm : TmKeepA σ1 τ := cfg_tmA _ σ1 ((), τ) hmem + have ct : τ.curThread = σ.curThread := hCt2.trans (by rw [hσ1]; rfl) + have cs : τ.curSc = σ.curSc := hCs2.trans (by rw [hσ1]; rfl) + have BE : Sched.BodyEndA σ τ m := by + refine ⟨⟨⟨?_, fun _ _ _ _ _ => ⟨R2, O2⟩, ?_⟩, A2.1, ?_, ?_⟩, ct⟩ + · have v := J.validTime + unfold ValidMachineTimeA at v ⊢ + rw [Tm.time, Tm.lmt, hσ1] + exact v + · intro _ x hx + rw [ct] at hx + have h1 : σ1.kheap σ.curThread = some (.tcb { xc with tcbState := .restart }) := by + rw [hσ1]; simp [stUpdA, setObjA] + have hne : σ.curThread ≠ specPtr p := fun e => by + rw [e, hkk] at h1; cases h1 + rw [P.other _ hne, h1] at hx + cases hx + exact Or.inr (Or.inl rfl) + · intro h + rw [cs] at h ⊢ + exact J.curScNotIdle h + · intro y' k hk _ _ _ o ho + obtain ⟨x, hx, hxs, -⟩ := I2.tcb + obtain ⟨z, j, hz, hzt⟩ := H2.inv.back _ x _ hx hxs + rw [hk] at hz + obtain ⟨rfl, -⟩ := KernelObject.schedContext.inj (Option.some.inj hz) + rw [hzt] at ho + rw [← Option.some.inj ho] + exact I2.notRel + refine ⟨xc, σ1, τ, s2, hkc, hσ1, hsp, hrun2, ⟨y0, n0, hkk, P.consumed⟩, hdecC, Q, Rl, Ac', J2, BE⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3ScCfgSpecA.lean b/proofs/Fl4ProofsA64/P3/Stage3ScCfgSpecA.lean new file mode 100644 index 000000000..d27741311 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ScCfgSpecA.lean @@ -0,0 +1,1049 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 3d): **the spec walk of FL4's ConfigureFlags** (`invokeFl4SchedControlConfigureFlags`). + +From an entry state (action `resume_cur_thread`) every outcome keeps the invariants `LoopInvA` (`SInvXA`, `SIdleA`, +`SActA`) and `SchedValidA`, and changes one heap object: the configured SC (`CfgSpecPostA`: same size, bound TCB, +replies, yield-from and notification; active with a non-empty refill list). + +`RefK` does not apply (`scRefillMax` and `consumedTime` change), so the SC-only steps have their own relation +`ScAtA`. FL4's `commit_time` runs when the configured SC is the current one and zeroes `consumedTime`; the post +records it (record edit #8b's zero-consumed fact). + +When the configured SC is the invoker's own, `postpone` may put the current thread in the release queue before +`reschedule_required`: `SchedValidA.act` fails in between, so the queue clauses (`SchedQA`) are carried alone and +`chooseNewThread` makes `act` vacuous at the end. + +Premises: [P] `hmin` (FL4's minimum budget is the spec's). New module. +-/ +import Fl4ProofsA64.P3.ScCfgRunA +import Fl4ProofsA64.P3.Stage3ScYieldSwA +import Fl4ProofsA64.P3.Stage3DelReplyRemoveA +import Fl4ProofsA64.P3.Stage3SigSchedValidA +import Fl4ProofsA64.P3.Stage3WakeInvA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 + +noncomputable section + +/-! ## The SC-only relation -/ + +/-- The SC fields ConfigureFlags' budget steps keep. -/ +def ScFixA (y y' : SchedContext) : Prop := + y'.scTcb = y.scTcb ∧ y'.scReplies = y.scReplies ∧ y'.scYieldFrom = y.scYieldFrom ∧ y'.scNtfn = y.scNtfn + +theorem ScFixA.refl (y : SchedContext) : ScFixA y y := ⟨rfl, rfl, rfl, rfl⟩ + +theorem ScFixA.trans {x y z : SchedContext} (a : ScFixA x y) (b : ScFixA y z) : ScFixA x z := + ⟨b.1.trans a.1, b.2.1.trans a.2.1, b.2.2.1.trans a.2.2.1, b.2.2.2.trans a.2.2.2⟩ + +/-- Only the SC at `p` changes (keeping `ScFixA` and its size), and the reprogram flag. -/ +structure ScAtA (p : ObjRef) (σ σ' : State Unit) : Prop where + rest : σ' = { σ with kheap := σ'.kheap, reprogramTimer := σ'.reprogramTimer } + kh : ∀ q, σ'.kheap q = σ.kheap q ∨ (q = p ∧ ∃ y y' n, σ.kheap q = some (.schedContext y n) ∧ + σ'.kheap q = some (.schedContext y' n) ∧ ScFixA y y') + +instance (p : ObjRef) : PreO (ScAtA p) where + refl σ := ⟨rfl, fun _ => Or.inl rfl⟩ + trans {a b c} h1 h2 := by + refine ⟨?_, fun q => ?_⟩ + · have x := h1.rest + have y := h2.rest + rw [x] at y + exact y + · rcases h1.kh q with e1 | ⟨eq, y, y', n, a1, b1, f1⟩ + · rcases h2.kh q with e2 | ⟨eq, z, z', k, a2, b2, f2⟩ + · exact Or.inl (e2.trans e1) + · exact Or.inr ⟨eq, z, z', k, e1 ▸ a2, b2, f2⟩ + · rcases h2.kh q with e2 | ⟨-, z, z', k, a2, b2, f2⟩ + · exact Or.inr ⟨eq, y, y', n, a1, e2.trans b1, f1⟩ + · rw [b1] at a2 + cases a2 + exact Or.inr ⟨eq, y, z', n, a1, b2, f1.trans f2⟩ + +theorem ScAtA.ofEq {α : Type} {p : ObjRef} {m : SMonad Unit α} (h : PresR (@Eq (State Unit)) m) : + PresR (ScAtA p) m := fun s x hx => by + rw [← h s x hx] + exact PreO.refl s + +theorem ScAtA.upd (p : ObjRef) (f : SchedContext → SchedContext) (h : ∀ y, ScFixA y (f y)) : + PresR (ScAtA p) (updateSchedContext (Z := Unit) p f) := fun s r hr => by + obtain ⟨y, n, hk, e⟩ := updateSchedContext_memA p f hr + rw [e] + refine ⟨rfl, fun q => ?_⟩ + by_cases eq : q = p + · subst eq + exact Or.inr ⟨rfl, y, f y, n, hk, Function.update_self _ _ _, h y⟩ + · exact Or.inl (Function.update_of_ne eq _ _) + +/-- The configured SC stays an SC of the same size with the same fixed fields. -/ +theorem ScAtA.sc {p : ObjRef} {σ σ' : State Unit} (h : ScAtA p σ σ') {y : SchedContext} {n : Nat} + (hk : σ.kheap p = some (.schedContext y n)) : + ∃ y', σ'.kheap p = some (.schedContext y' n) ∧ ScFixA y y' := by + rcases h.kh p with e | ⟨-, a, b, k, e1, e2, f⟩ + · exact ⟨y, e.trans hk, .refl y⟩ + · rw [hk] at e1 + cases e1 + exact ⟨b, e2, f⟩ + +theorem ScAtA.other {p q : ObjRef} {σ σ' : State Unit} (h : ScAtA p σ σ') (hq : q ≠ p) : σ'.kheap q = σ.kheap q := + (h.kh q).resolve_right fun a => hq a.1 + +theorem ScAtA.nonSc {p q : ObjRef} {σ σ' : State Unit} (h : ScAtA p σ σ') {o : KernelObject} + (hk : σ.kheap q = some o) (ho : ∀ y n, o ≠ .schedContext y n) : σ'.kheap q = some o := by + rcases h.kh q with e | ⟨-, a, b, k, e1, -⟩ + · exact e.trans hk + · rw [hk] at e1 + exact absurd (Option.some.inj e1) (ho _ _) + +/-! ## The reads -/ + +theorem getRefills_eqPresA (p : ObjRef) : PresR (@Eq (State Unit)) (getRefills (Z := Unit) p) := by + unfold getRefills + refine PresR.bind (getSchedContext_eqPres p) (fun _ => ?_) + eqpres + +theorem getRefillHead_eqPresA (p : ObjRef) : PresR (@Eq (State Unit)) (getRefillHead (Z := Unit) p) := by + unfold getRefillHead + eqpres + +theorem getRefillTail_eqPresA (p : ObjRef) : PresR (@Eq (State Unit)) (getRefillTail (Z := Unit) p) := by + unfold getRefillTail + eqpres + +theorem isRoundRobin_eqPresA (p : ObjRef) : PresR (@Eq (State Unit)) (isRoundRobin (Z := Unit) p) := by + unfold isRoundRobin + refine PresR.bind (getSchedContext_eqPres p) (fun _ => ?_) + eqpres + +theorem refillSize_eqPresA (p : ObjRef) : PresR (@Eq (State Unit)) (refillSize (Z := Unit) p) := by + unfold refillSize + refine PresR.bind (getRefills_eqPresA p) (fun _ => ?_) + eqpres + +theorem refillSingle_eqPresA (p : ObjRef) : PresR (@Eq (State Unit)) (refillSingle (Z := Unit) p) := by + unfold refillSingle + refine PresR.bind (refillSize_eqPresA p) (fun _ => ?_) + eqpres + +theorem refillFull_eqPresA (p : ObjRef) : PresR (@Eq (State Unit)) (refillFull (Z := Unit) p) := by + unfold refillFull + refine PresR.bind (getSchedContext_eqPres p) (fun _ => ?_) + eqpres + +theorem getScActive_eqPresA (p : ObjRef) : PresR (@Eq (State Unit)) (getScActive (Z := Unit) p) := by + unfold getScActive + exact PresR.getsThe _ + +/-- The structural tactic for `ScAtA p`. -/ +local macro "scat" : tactic => `(tactic| + repeat' (first + | exact PresR.pure _ + | exact PresR.gets _ + | exact PresR.getsThe _ + | exact PresR.assertOpt _ + | exact PresR.assert _ + | exact PresR.fail + | exact ScAtA.ofEq (getSchedContext_eqPres _) + | exact ScAtA.ofEq (getRefills_eqPresA _) + | exact ScAtA.ofEq (getRefillHead_eqPresA _) + | exact ScAtA.ofEq (getRefillTail_eqPresA _) + | exact ScAtA.ofEq (isRoundRobin_eqPresA _) + | exact ScAtA.ofEq (refillSingle_eqPresA _) + | exact ScAtA.ofEq (refillFull_eqPresA _) + | exact ScAtA.ofEq (getScActive_eqPresA _) + | exact ScAtA.ofEq (getScRefillReady_eqPres _) + | exact ScAtA.upd _ _ (fun _ => ⟨rfl, rfl, rfl, rfl⟩) + | refine PresR.bind_pure _ _ ?_ + | refine PresR.bind ?_ (fun _ => ?_) + | refine PresR.when ?_ + | refine PresR.ite' ?_ ?_ + | refine PresR.whileLoop (fun _ => ?_) _ + | dsimp only + | split)) + +/-! ## The refill steps -/ + +theorem maybeAddEmptyTail_scAtA (p : ObjRef) : PresR (ScAtA p) (maybeAddEmptyTail (Z := Unit) p) := by + unfold maybeAddEmptyTail refillAddTail + scat + +theorem refillNew_scAtA (p : ObjRef) (n : Nat) (b per : Ticks) : + PresR (ScAtA p) (refillNew (Z := Unit) p n b per) := by + unfold refillNew maybeAddEmptyTail refillAddTail setScObjRef + scat + +theorem refillUpdate_scAtA (p : ObjRef) (per b : Ticks) (n : Nat) : + PresR (ScAtA p) (refillUpdate (Z := Unit) p per b n) := by + unfold refillUpdate maybeAddEmptyTail refillAddTail updateRefillHd setScObjRef + scat + +theorem chargeEntire_scAtA (p : ObjRef) (u : Ticks) : PresR (ScAtA p) (chargeEntireHeadRefill (Z := Unit) p u) := by + unfold chargeEntireHeadRefill refillPopHead scheduleUsed updateRefillTl refillAddTail updateRefillHd + scat + +theorem budgetBody_scAtA (p : ObjRef) (limit minimum usage : Ticks) : + PresR (ScAtA p) (do + let robin ← isRoundRobin (Z := Unit) p + Fl4Spec.NonDet.assert (¬robin) + let usage' ← handleOverrunWith limit p usage + let head ← getRefillHead p + Fl4Spec.NonDet.when (usage' > 0 ∧ head.rTime < limit) <| do + let sc ← getSchedContext p + let used ← pure { rTime := head.rTime + sc.scPeriod, rAmount := usage' : Refill } + updateRefillHd p fun r => { r with rAmount := head.rAmount - usage' } + updateRefillHd p fun r => { r with rTime := head.rTime + usage' } + scheduleUsed p used + headInsufficientLoopWith minimum p) := by + unfold handleOverrunWith headInsufficientLoopWith mergeNonoverlappingHeadRefill chargeEntireHeadRefill + refillPopHead scheduleUsed updateRefillTl refillAddTail updateRefillHd + scat + +theorem roundRobinBody_scAtA (p : ObjRef) (usage : Ticks) : + PresR (ScAtA p) (do + updateRefillHd (Z := Unit) p fun refill => { refill with rAmount := refill.rAmount - usage } + updateRefillTl p fun refill => { refill with rAmount := refill.rAmount + usage }) := by + unfold updateRefillTl updateRefillHd + scat + +/-- A computation reading `curSc` first, from a state whose current SC is `p`. -/ +theorem atCurScA {α : Type} {p : ObjRef} {f : ObjRef → SMonad Unit α} {σ : State Unit} (hc : σ.curSc = p) + (h : PresR (ScAtA p) (f p)) {r : α × State Unit} + (hr : r ∈ ((Fl4Spec.NonDet.gets (fun s : State Unit => s.curSc) >>= f) σ).1) : ScAtA p σ r.2 := by + rw [nd_gets_bindSc, hc] at hr + exact h σ r hr + +/-- **FL4's `commit_time` on the configured current SC**: SC-only, then `consumedTime := 0`. -/ +theorem commitTime_cfgA {p : ObjRef} {σ : State Unit} (hc : σ.curSc = p) {r : Unit × State Unit} + (hr : r ∈ (fl4CommitTime (Z := Unit) σ).1) : + ∃ τ, ScAtA p σ τ ∧ r.2 = { τ with consumedTime := 0 } := by + unfold fl4CommitTime at hr + rw [nd_gets_bindSc, hc] at hr + obtain ⟨active, -, hr⟩ := eqPres_mem (getScActive_eqPresA p) hr + obtain ⟨u, hu, hr⟩ := mem_bindA.1 hr + have er : r = ((), { u.2 with consumedTime := 0 }) := hr + refine ⟨u.2, ?_, by rw [er]⟩ + unfold Fl4Spec.NonDet.when at hu + split_ifs at hu + swap + · have : u = ((), σ) := hu + rw [this] + exact PreO.refl _ + rw [nd_gets_bindSc] at hu + obtain ⟨v, hv, hu⟩ := mem_bindA.1 hu + have hv' : ScAtA p σ v.2 := by + split_ifs at hv + swap + · have : v = ((), σ) := hv + rw [this] + exact PreO.refl _ + obtain ⟨robin, -, hv⟩ := eqPres_mem (isRoundRobin_eqPresA p) hv + split_ifs at hv + · unfold refillBudgetCheckRoundRobin at hv + exact atCurScA hc (roundRobinBody_scAtA p _) hv + · unfold fl4RefillBudgetCheck refillBudgetCheckWith at hv + exact atCurScA hc (budgetBody_scAtA p _ _ _) hv + refine PreO.trans hv' (ScAtA.upd p (fun sc => { sc with scConsumed := sc.scConsumed + σ.consumedTime }) ?_ v.2 u hu) + exact fun _ => ⟨rfl, rfl, rfl, rfl⟩ + +/-! ## The refill list after `refill_new` / `refill_update` -/ + +/-- At `p`: `scRefillMax` kept, a non-empty refill list stays non-empty. -/ +def ScGrowA (p : ObjRef) (σ σ' : State Unit) : Prop := + ∀ y n, σ.kheap p = some (.schedContext y n) → ∃ y', σ'.kheap p = some (.schedContext y' n) ∧ + y'.scRefillMax = y.scRefillMax ∧ (y.scRefills ≠ [] → y'.scRefills ≠ []) + +instance (p : ObjRef) : PreO (ScGrowA p) where + refl _ y _ h := ⟨y, h, rfl, id⟩ + trans h1 h2 y n h := by + obtain ⟨y1, k1, e1, r1⟩ := h1 y n h + obtain ⟨y2, k2, e2, r2⟩ := h2 y1 n k1 + exact ⟨y2, k2, e2.trans e1, r2 ∘ r1⟩ + +theorem ScGrowA.upd (p : ObjRef) (f : SchedContext → SchedContext) + (h : ∀ y, (f y).scRefillMax = y.scRefillMax ∧ (y.scRefills ≠ [] → (f y).scRefills ≠ [])) : + PresR (ScGrowA p) (updateSchedContext (Z := Unit) p f) := fun s r hr y n hk => by + obtain ⟨z, k, hz, e⟩ := updateSchedContext_memA p f hr + rw [hk] at hz + cases hz + exact ⟨f y, by rw [e]; exact Function.update_self _ _ _, (h y).1, (h y).2⟩ + +theorem ScGrowA.ofEq {α : Type} {p : ObjRef} {m : SMonad Unit α} (h : PresR (@Eq (State Unit)) m) : + PresR (ScGrowA p) m := fun s x hx => by + rw [← h s x hx] + exact PreO.refl s + +theorem maybeAddEmptyTail_growA (p : ObjRef) : PresR (ScGrowA p) (maybeAddEmptyTail (Z := Unit) p) := by + unfold maybeAddEmptyTail refillAddTail + refine PresR.bind (ScGrowA.ofEq (isRoundRobin_eqPresA p)) fun _ => PresR.when ?_ + refine PresR.bind (ScGrowA.ofEq (getRefillHead_eqPresA p)) fun _ => ?_ + refine PresR.bind (PresR.pure _) fun _ => ?_ + exact ScGrowA.upd p _ fun y => ⟨rfl, fun _ => by simp [scRefillsUpdate]⟩ + +/-- At `p`: an SC with `scRefillMax = k` and a non-empty refill list. -/ +def ScSetA (p : ObjRef) (k : Nat) (σ : State Unit) : Prop := + ∃ y n, σ.kheap p = some (.schedContext y n) ∧ y.scRefillMax = k ∧ y.scRefills ≠ [] + +theorem ScSetA.grow {p : ObjRef} {k : Nat} {σ σ' : State Unit} (h : ScSetA p k σ) (g : ScGrowA p σ σ') : + ScSetA p k σ' := by + obtain ⟨y, n, hk, e, r⟩ := h + obtain ⟨y', hk', e', r'⟩ := g y n hk + exact ⟨y', n, hk', e'.trans e, r' r⟩ + +theorem refillNew_setA {p : ObjRef} {k : Nat} {b per : Ticks} {σ : State Unit} {r : Unit × State Unit} + (hr : r ∈ (refillNew (Z := Unit) p k b per σ).1) : ScSetA p k r.2 := by + unfold refillNew setScObjRef at hr + rw [nd_gets_bindSc] at hr + obtain ⟨a, ha, hr⟩ := mem_bindA.1 hr + obtain ⟨y1, n1, h1, e1⟩ := updateSchedContext_memA p _ ha + obtain ⟨b', hb, hr⟩ := mem_bindA.1 hr + obtain ⟨y2, n2, h2, e2⟩ := updateSchedContext_memA p _ hb + obtain ⟨c, hc, hr⟩ := mem_bindA.1 hr + obtain ⟨y3, n3, h3, e3⟩ := updateSchedContext_memA p _ hc + obtain ⟨d, hd, hr⟩ := mem_bindA.1 hr + have ed : d = (({ rTime := σ.curTime, rAmount := b } : Refill), c.2) := hd + subst ed + obtain ⟨f, hf, hr⟩ := mem_bindA.1 hr + obtain ⟨y4, n4, h4, e4⟩ := updateSchedContext_memA p _ hf + have m3 : y4.scRefillMax = k := by + rw [e3] at h4 + simp only [Function.update_self, Option.some.injEq, KernelObject.schedContext.injEq] at h4 + rw [← h4.1] + rfl + have S : ScSetA p k f.2 := by + exact ⟨scRefillsUpdate (fun _ => [({ rTime := σ.curTime, rAmount := b } : Refill)]) y4, n4, + by rw [e4]; exact Function.update_self _ _ _, m3, List.cons_ne_nil _ _⟩ + exact S.grow (maybeAddEmptyTail_growA p _ _ hr) + +theorem refillUpdate_setA {p : ObjRef} {k : Nat} {b per : Ticks} {σ : State Unit} {r : Unit × State Unit} + (hr : r ∈ (refillUpdate (Z := Unit) p per b k σ).1) : ScSetA p k r.2 := by + unfold refillUpdate setScObjRef at hr + obtain ⟨a, ha, hr⟩ := mem_bindA.1 hr + obtain ⟨y1, n1, h1, e1⟩ := updateSchedContext_memA p _ ha + obtain ⟨b', hb, hr⟩ := mem_bindA.1 hr + obtain ⟨y2, n2, h2, e2⟩ := updateSchedContext_memA p _ hb + have m2 : y2.scRefills ≠ [] := by + rw [e1] at h2 + simp only [Function.update_self, Option.some.injEq, KernelObject.schedContext.injEq] at h2 + rw [← h2.1] + simp [scRefillsUpdate] + have S : ScSetA p k b'.2 := by + exact ⟨scRefillMaxUpdate (fun _ => k) y2, n2, by rw [e2]; exact Function.update_self _ _ _, rfl, m2⟩ + refine S.grow ?_ + refine (show PresR (ScGrowA p) _ from ?_) _ _ hr + unfold updateRefillHd maybeAddEmptyTail refillAddTail + have upd : ∀ f : SchedContext → SchedContext, + (∀ y, (f y).scRefillMax = y.scRefillMax ∧ (y.scRefills ≠ [] → (f y).scRefills ≠ [])) → + PresR (ScGrowA p) (updateSchedContext (Z := Unit) p f) := ScGrowA.upd p + repeat' (first + | exact PresR.pure _ + | exact PresR.gets _ + | exact ScGrowA.ofEq (getScRefillReady_eqPres _) + | exact ScGrowA.ofEq (getRefillHead_eqPresA _) + | exact ScGrowA.ofEq (isRoundRobin_eqPresA _) + | exact upd _ (fun _ => ⟨rfl, id⟩) + | exact upd _ (fun _ => ⟨rfl, fun _ => List.cons_ne_nil _ _⟩) + | exact upd _ (fun _ => ⟨rfl, fun _ => List.append_ne_nil_of_right_ne_nil _ (List.cons_ne_nil _ _)⟩) + | refine PresR.bind_pure _ _ ?_ + | refine PresR.bind ?_ (fun _ => ?_) + | refine PresR.when ?_ + | refine PresR.ite' ?_ ?_ + | dsimp only + | split) + +/-! ## The invariants across the SC-only steps -/ + +/-- **`LoopInvA` across `ScAtA`** (`RefK` does not apply: `scRefillMax` changes), given the final SC's refill +list, an SC that is not the idle SC, and not the current SC while time is consumed (`curScReady`). -/ +theorem LoopInvA.scAtA {p : ObjRef} {σ σ' : State Unit} {m : McState} {n : Nat} (h : LoopInvA σ m n) + (R : ScAtA p σ σ') (hr : ∀ y k, σ'.kheap p = some (.schedContext y k) → scActive y = true → y.scRefills ≠ []) + (hcp : σ.consumedTime ≠ 0 → σ.curSc ≠ p) (hpi : p ≠ idleScPtr) : + LoopInvA σ' m n := by + have e := R.rest + have hcons : σ'.consumedTime = σ.consumedTime := by rw [e] + have htime : σ'.curTime = σ.curTime := by rw [e] + have hcur : σ'.curThread = σ.curThread := by rw [e] + have hidle : σ'.idleThread = σ.idleThread := by rw [e] + have hcsc : σ'.curSc = σ.curSc := by rw [e] + have hrq : σ'.readyQueues = σ.readyQueues := by rw [e] + have hrls : σ'.releaseQueue = σ.releaseQueue := by rw [e] + have hact : σ'.schedulerAction = σ.schedulerAction := by rw [e] + have hnode : σ'.interruptIrqNode = σ.interruptIrqNode := by rw [e] + have tcbF : ∀ q x, σ.kheap q = some (.tcb x) → σ'.kheap q = some (.tcb x) := fun q x hq => + R.nonSc hq (fun _ _ e => by cases e) + have tcbB : ∀ q x, σ'.kheap q = some (.tcb x) → σ.kheap q = some (.tcb x) := fun q x hq => by + rcases R.kh q with e | ⟨-, a, b, k, -, e2, -⟩ + · exact e ▸ hq + · rw [hq] at e2; cases e2 + have scF : ∀ q y k, σ.kheap q = some (.schedContext y k) → + ∃ y', σ'.kheap q = some (.schedContext y' k) ∧ ScFixA y y' := fun q y k hq => by + by_cases eq : q = p + · rw [eq] at hq ⊢; exact R.sc hq + · exact ⟨y, (R.other eq).trans hq, .refl y⟩ + have scB : ∀ q y' k, σ'.kheap q = some (.schedContext y' k) → + ∃ y, σ.kheap q = some (.schedContext y k) ∧ ScFixA y y' := fun q y' k hq => by + rcases R.kh q with e | ⟨-, a, b, j, e1, e2, f⟩ + · exact ⟨y', e ▸ hq, .refl _⟩ + · rw [hq] at e2; cases e2; exact ⟨a, e1, f⟩ + have other : ∀ q o, σ'.kheap q = some o → (∀ y n, o ≠ .schedContext y n) → σ.kheap q = some o := fun q o hq ho => by + rcases R.kh q with e | ⟨-, a, b, j, -, e2, -⟩ + · exact e ▸ hq + · rw [hq] at e2; exact absurd (Option.some.inj e2) (ho _ _) + have hct : ∀ k q, capTableAt k q σ' ↔ capTableAt k q σ := fun k q => by + unfold capTableAt + rcases R.kh q with e | ⟨-, a, b, j, e1, e2, -⟩ + · rw [e] + · rw [e1, e2]; simp + have capB : ∀ q o' b c, σ'.kheap q = some o' → capOf o' b = some c → + ∃ o, σ.kheap q = some o ∧ capOf o b = some c := fun q o' b c hq hc => + ⟨o', other q o' hq (fun _ _ e => by rw [e] at hc; cases hc), hc⟩ + have specCap : ∀ sl, specCapAtA σ' sl = specCapAtA σ sl := fun sl => by + unfold specCapAtA + rcases R.kh sl.1 with e | ⟨-, a, b, j, e1, e2, -⟩ + · rw [e] + · rw [e1, e2] + have S := h.sinv + refine + { sinv := + { mc := S.mc + view := by unfold McView; rw [hidle]; exact S.view + curTcb := by + obtain ⟨x, hx⟩ := S.curTcb + exact ⟨x, by rw [hcur]; exact tcbF _ _ hx⟩ + idleTcb := by + obtain ⟨x, hx, hs⟩ := S.idleTcb + exact ⟨x, by rw [hidle]; exact tcbF _ _ hx, hs⟩ + onlyIdle := fun t x hx hs => S.onlyIdle t x (tcbB _ _ hx) hs + idleSc := by + obtain ⟨x, y, k, h1, h2, h3, h4⟩ := S.idleSc + obtain ⟨y', h3', ey⟩ := scF _ _ _ h3 + exact ⟨x, y', k, by rw [hidle]; exact tcbF _ _ h1, h2, h3', by rw [hidle]; exact ey.1.trans h4⟩ + curSc := by + obtain ⟨y, k, hy⟩ := S.curSc + obtain ⟨y', h', -⟩ := scF _ _ _ hy + exact ⟨y', k, by rw [hcsc]; exact h'⟩ + ctQ := fun ha d q => by rw [hcur, hrq]; exact S.ctQ (hact.symm.trans ha) d q + idleQ := fun d q => by rw [hrq]; exact S.idleQ d q + rq := fun d q t ht => by + rw [hrq] at ht + obtain ⟨x, hx, hr⟩ := S.rq d q t ht + exact ⟨x, tcbF _ _ hx, hr⟩ + rls := fun t ht => by + rw [hrls] at ht + obtain ⟨x, hx⟩ := S.rls t ht + exact ⟨x, tcbF _ _ hx⟩ + rpos := fun q y' k hq ha => by + by_cases eq : q = p + · rw [eq] at hq; exact hr y' k hq ha + · exact S.rpos q y' k ((R.other eq).symm.trans hq) ha + irqN := by + obtain ⟨⟨⟨hinj, hcn⟩, hdom⟩, hsl, href⟩ := S.irqN + refine ⟨⟨⟨?_, fun irq hi => ?_⟩, fun irq hi k hk' => ?_⟩, fun irq hi o b c ho hc => ?_, + fun q o b c ho hc => ?_⟩ + · rw [hnode]; exact hinj + · rw [hnode]; exact (hct _ _).2 (hcn irq hi) + · rw [hnode] at hk'; exact hdom irq hi k ((hct _ _).1 hk') + · rw [hnode] at ho + obtain ⟨o0, h0, c0⟩ := capB _ _ _ _ ho hc + exact hsl irq hi o0 b c h0 c0 + · obtain ⟨o0, h0, c0⟩ := capB _ _ _ _ ho hc + rw [hnode]; exact href q o0 b c h0 c0 + zc := ZeroConsA.back S.zc (fun t ht => by rw [hrls] at ht; exact ht) hcons hcsc + (fun t x' h0 => ⟨x', tcbB _ _ h0, rfl⟩) + rlsRun := RlsRunA.back S.rlsRun (fun t ht => by rw [hrls] at ht; exact ht) + (fun t x hx _ => ⟨x, tcbF _ _ hx, id⟩) + curScReady := fun hc0 y k hy a => by + rw [hcons] at hc0 + rw [hcsc, R.other (hcp hc0)] at hy + rw [htime] + exact S.curScReady hc0 y k hy a + faultSt := fun t x h0 f => S.faultSt t x (tcbB _ _ h0) f } + sidle := + { f3 := fun c hc => by + obtain ⟨x, hx, hs⟩ := h.sidle.f3 c hc + exact ⟨x, tcbF _ _ hx, hs⟩ + f4 := fun y' k hy => by + obtain ⟨y, hy0, e⟩ := scB _ _ _ hy + exact e.2.1.trans (h.sidle.f4 y k hy0) + f5 := fun q y' k hy ht => by + obtain ⟨y, hy0, e⟩ := scB _ _ _ hy + exact h.sidle.f5 q y k hy0 (by rw [← hidle, ← e.1]; exact ht) + f6 := h.sidle.f6.of_eq specCap + f7 := fun y k hy => by + rw [R.other (Ne.symm hpi)] at hy + rw [htime] + exact h.sidle.f7 y k hy + idleSlot := fun c hc => by + obtain ⟨x, hx, -⟩ := h.sidle.f3 c hc + have h0 := h.sidle.idleSlot c hc + unfold vtCapA at h0 ⊢ + rw [tcbF _ _ hx] + rw [hx] at h0 + exact h0 + idleBound := fun c hc => by + obtain ⟨x, hx, -⟩ := h.sidle.f3 c hc + have h0 := h.sidle.idleBound c hc + unfold vcpuOfA at h0 ⊢ + rw [tcbF _ _ hx] + rw [hx] at h0 + exact h0 } + sact := + { f1 := fun u eu => by rw [hcur, hidle]; exact h.sact.f1 u (hact.symm.trans eu) + f2 := by + obtain ⟨x, hx, hs⟩ := h.sact.f2 + exact ⟨x, by rw [hcur]; exact tcbF _ _ hx, hs⟩ } } + +/-- **`SchedValidA` across `ScAtA`** ending with an active SC at `p`. -/ +theorem SchedValidA.scAtA {p : ObjRef} {σ σ' : State Unit} (h : SchedValidA σ) (R : ScAtA p σ σ') + (ha : ∀ y k, σ.kheap p = some (.schedContext y k) → ScActiveAtA σ' p) : SchedValidA σ' := by + have e := R.rest + have hcur : σ'.curThread = σ.curThread := by rw [e] + have hidle : σ'.idleThread = σ.idleThread := by rw [e] + have hrq : σ'.readyQueues = σ.readyQueues := by rw [e] + have hrls : σ'.releaseQueue = σ.releaseQueue := by rw [e] + have hact : σ'.schedulerAction = σ.schedulerAction := by rw [e] + have act : ∀ q, ScActiveAtA σ q → ScActiveAtA σ' q := fun q ⟨y, k, hy, a⟩ => by + by_cases eq : q = p + · rw [eq] at hy ⊢; exact ha y k hy + · exact ⟨y, k, (R.other eq).trans hy, a⟩ + have tcbF : ∀ q x, σ.kheap q = some (.tcb x) → σ'.kheap q = some (.tcb x) := fun q x hq => + R.nonSc hq (fun _ _ e => by cases e) + have T : ∀ t, ActTcbA σ t → ActTcbA σ' t := fun t ⟨x, sc, h1, h2, h3⟩ => ⟨x, sc, tcbF _ _ h1, h2, act _ h3⟩ + refine ⟨fun d q t ht => ?_, fun ha1 ha2 => ?_, fun t x sc hx hb hs => ?_, fun t ht => ?_⟩ + · rw [hrq] at ht + obtain ⟨a, b⟩ := h.rq d q t ht + exact ⟨T t a, by rw [hrls]; exact b⟩ + · rw [hact] at ha1 + rw [hcur, hidle] at ha2 + obtain ⟨a, b⟩ := h.act ha1 ha2 + rw [hcur, hrls] + exact ⟨T _ a, b⟩ + · have hx0 : σ.kheap t = some (.tcb x) := by + rcases R.kh t with e | ⟨-, a, b, j, -, e2, -⟩ + · exact e ▸ hx + · rw [hx] at e2; cases e2 + exact act _ (h.blk t x sc hx0 hb hs) + · rw [hrls] at ht + exact T t (h.rls t ht) + +/-! ## The queue steps -/ + +/-- Only the queues and the reprogram flag change; the ready queues shrink. -/ +structure QShrinkA (σ σ' : State Unit) : Prop where + rest : σ' = { σ with readyQueues := σ'.readyQueues, releaseQueue := σ'.releaseQueue, + reprogramTimer := σ'.reprogramTimer } + rq : ∀ d p y, y ∈ σ'.readyQueues d p → y ∈ σ.readyQueues d p + +theorem QShrinkA.trans {a b c : State Unit} (h1 : QShrinkA a b) (h2 : QShrinkA b c) : QShrinkA a c := by + refine ⟨?_, fun d p y h => h1.rq d p y (h2.rq d p y h)⟩ + have x := h1.rest + have y := h2.rest + rw [x] at y + exact y + +theorem releaseRemove_shrinkA (t : ObjRef) {σ : State Unit} {r : Unit × State Unit} + (hr : r ∈ (tcbReleaseRemove (Z := Unit) t σ).1) : QShrinkA σ r.2 := by + unfold tcbReleaseRemove at hr + rw [nd_gets_bindSc] at hr + obtain ⟨u, hu, hr⟩ := mem_bindA.1 hr + have h1 : u.2 = { σ with reprogramTimer := u.2.reprogramTimer } := by + unfold Fl4Spec.NonDet.when at hu + split_ifs at hu + · have : u = ((), { σ with reprogramTimer := true }) := hu + rw [this] + · have : u = ((), σ) := hu + rw [this] + obtain ⟨q', hq', hr⟩ := mem_bindA.1 hr + have eq' : q' = (tcbSchedDequeue t σ.releaseQueue, u.2) := hq' + subst eq' + have er : r = ((), { u.2 with releaseQueue := tcbSchedDequeue t σ.releaseQueue }) := hr + rw [er, h1] + exact ⟨rfl, fun _ _ _ h => h⟩ + +/-- `tcb_sched_action` on a TCB rewrites its own queue. -/ +theorem schedAct_memA (f : ObjRef → List ObjRef → List ObjRef) {t : ObjRef} {σ : State Unit} {x : Tcb} + (hx : σ.kheap t = some (.tcb x)) {r : Unit × State Unit} + (hr : r ∈ (tcbSchedAction (Z := Unit) f t σ).1) : + r.2 = { σ with readyQueues := (fun d q => if d = x.tcbDomain ∧ q = x.tcbPriority then (f t (σ.readyQueues x.tcbDomain x.tcbPriority)) else (σ.readyQueues d q)) } := by + unfold tcbSchedAction at hr + rw [nd_bind_oneSc (threadGet_eqSc _ hx), nd_bind_oneSc (threadGet_eqSc _ hx)] at hr + have hq : getTcbQueue (Z := Unit) x.tcbDomain x.tcbPriority σ = + ({(σ.readyQueues x.tcbDomain x.tcbPriority, σ)}, False) := by + unfold getTcbQueue + rw [nd_gets_bindSc] + rfl + rw [nd_bind_oneSc hq] at hr + obtain ⟨y, hy, hr⟩ := mem_bindA.1 hr + have ey : y = (f t (σ.readyQueues x.tcbDomain x.tcbPriority), σ) := hy + subst ey + unfold setTcbQueue at hr + have er : r = ((), { σ with readyQueues := (fun d q => if d = x.tcbDomain ∧ q = x.tcbPriority then (f t (σ.readyQueues x.tcbDomain x.tcbPriority)) else (σ.readyQueues d q)) }) := hr + rw [er] + +theorem dequeue_shrinkA {t : ObjRef} {σ : State Unit} {x : Tcb} (hx : σ.kheap t = some (.tcb x)) + {r : Unit × State Unit} (hr : r ∈ (tcbSchedAction (Z := Unit) tcbSchedDequeue t σ).1) : QShrinkA σ r.2 := by + rw [schedAct_memA _ hx hr] + refine ⟨rfl, fun d p y h => ?_⟩ + by_cases e : d = x.tcbDomain ∧ p = x.tcbPriority + · simp only [if_pos e] at h + rw [e.1, e.2] + exact (List.mem_filter.1 h).1 + · simpa only [if_neg e] using h + +theorem rqDomPrio_shrinkA {σ σ' : State Unit} (h : RqDomPrio σ) (q : QShrinkA σ σ') : RqDomPrio σ' := + fun f d p x hx hm => by + have hk : σ'.kheap = σ.kheap := by rw [q.rest] + exact h f d p x (hk ▸ hx) (q.rq d p f hm) + +/-- **`SchedValidA`'s queue clauses** (all but `act`). -/ +structure SchedQA (σ : State Unit) : Prop where + rq : ∀ d p t, t ∈ σ.readyQueues d p → ActTcbA σ t ∧ t ∉ σ.releaseQueue + blk : ∀ t x sc, σ.kheap t = some (.tcb x) → BlockedTsA x.tcbState = true → x.tcbSchedContext = some sc → + ScActiveAtA σ sc + rls : ∀ t ∈ σ.releaseQueue, ActTcbA σ t + +theorem SchedValidA.toQA {σ : State Unit} (h : SchedValidA σ) : SchedQA σ := ⟨h.rq, h.blk, h.rls⟩ + +/-- **`postpone`** of a thread-bound SC: the thread leaves its ready queue and may enter the release queue. -/ +theorem postpone_memA {p t : ObjRef} {σ : State Unit} {y : SchedContext} {n : Nat} {x : Tcb} + (hk : σ.kheap p = some (.schedContext y n)) (ht : y.scTcb = some t) (hx : σ.kheap t = some (.tcb x)) + (D : RqDomPrio σ) {r : Unit × State Unit} (hr : r ∈ (postpone (Z := Unit) p σ).1) : + r.2 = { σ with readyQueues := r.2.readyQueues, releaseQueue := r.2.releaseQueue, + reprogramTimer := r.2.reprogramTimer } ∧ + (∀ d q u, u ∈ r.2.readyQueues d q → u ∈ σ.readyQueues d q ∧ u ≠ t) ∧ + (∀ u ∈ r.2.releaseQueue, u = t ∨ u ∈ σ.releaseQueue) := by + unfold postpone at hr + have hgp : getScObjRef (Z := Unit) SchedContext.scTcb p σ = ({(y.scTcb, σ)}, False) := by + unfold getScObjRef + rw [nd_bind_oneSc (Sched.getSchedContext_eqA hk)] + rfl + rw [nd_bind_oneSc hgp, ht, + nd_bind_oneSc (show Fl4Spec.NonDet.assertOpt (σ := State Unit) (some t) σ = ({(t, σ)}, False) from rfl)] at hr + obtain ⟨a, ha, hr⟩ := mem_bindA.1 hr + have ea := schedAct_memA _ hx ha + obtain ⟨b, hb, hr⟩ := mem_bindA.1 hr + have er : r = ((), { b.2 with reprogramTimer := true }) := hr + unfold tcbReleaseEnqueue at hb + rw [nd_gets_bindSc] at hb + obtain ⟨c, hc, hb⟩ := mem_bindA.1 hb + unfold readyTimesAppend at hc + obtain ⟨hs, hm⟩ := orderedInsert_spec _ _ _ _ hc + obtain ⟨d, hd, hb⟩ := mem_bindA.1 hb + have ed : d = ((), { c.2 with releaseQueue := c.1 }) := hd + have eb : b.2 = { d.2 with reprogramTimer := b.2.reprogramTimer } := by + unfold Fl4Spec.NonDet.when at hb + split_ifs at hb + · have : b = ((), { d.2 with reprogramTimer := true }) := hb + rw [this] + · have : b = ((), d.2) := hb + rw [this] + have eall : r.2 = { a.2 with releaseQueue := c.1, reprogramTimer := true } := by + rw [er] + show ({ b.2 with reprogramTimer := true } : State Unit) = _ + rw [eb, ed, hs] + have erq : r.2.readyQueues = a.2.readyQueues := by rw [eall] + have erl : r.2.releaseQueue = c.1 := by rw [eall] + refine ⟨by rw [eall, ea], fun d' q u hu => ?_, fun u hu => ?_⟩ + · rw [erq, ea] at hu + simp only at hu + split at hu + · rename_i e + obtain ⟨m1, m2⟩ := List.mem_filter.1 hu + refine ⟨by rw [e.1, e.2]; exact m1, fun h => ?_⟩ + simp only [decide_eq_true_eq] at m2 + exact m2 h.symm + · rename_i e + refine ⟨hu, fun h => e ?_⟩ + exact D t d' q x hx (h ▸ hu) + · rw [erl] at hu + rcases hm u hu with h | h + · exact Or.inl h + · refine Or.inr ?_ + have : a.2.releaseQueue = σ.releaseQueue := by rw [ea] + exact this ▸ h + +/-- **`sched_context_resume`** on a thread-bound SC: nothing, or `postpone` of the schedulable thread. -/ +theorem resume_memA {p t : ObjRef} {σ : State Unit} {y : SchedContext} {n : Nat} + (hk : σ.kheap p = some (.schedContext y n)) (ht : y.scTcb = some t) + {r : Unit × State Unit} (hr : r ∈ (schedContextResume (Z := Unit) p σ).1) : + r.2 = σ ∨ (schedulable t σ = true ∧ r ∈ (postpone (Z := Unit) p σ).1) := by + unfold schedContextResume at hr + rw [nd_bind_oneSc (Sched.getSchedContext_eqA hk), ht, + nd_bind_oneSc (show Fl4Spec.NonDet.assertOpt (σ := State Unit) (some t) σ = ({(t, σ)}, False) from rfl), + nd_gets_bindSc] at hr + unfold Fl4Spec.NonDet.when at hr + split_ifs at hr with hs + swap + · have : r = ((), σ) := hr + exact Or.inl (by rw [this]) + obtain ⟨ts, -, hr⟩ := eqPres_mem (threadGet_eqPres _ t) hr + obtain ⟨ready, -, hr⟩ := eqPres_mem (getScRefillReady_eqPres p) hr + obtain ⟨suff, -, hr⟩ := eqPres_mem (getScRefillSufficient_eqPres p 0) hr + split_ifs at hr + swap + · have : r = ((), σ) := hr + exact Or.inl (by rw [this]) + obtain ⟨d, -, hr⟩ := eqPres_mem (threadGet_eqPres _ t) hr + obtain ⟨pr, -, hr⟩ := eqPres_mem (threadGet_eqPres _ t) hr + obtain ⟨q, -, hr⟩ := eqPres_mem (by unfold getTcbQueue; eqpres : PresR (@Eq (State Unit)) + (getTcbQueue (Z := Unit) d pr)) hr + obtain ⟨-, -, hr⟩ := eqPres_mem (PresR.assert _) hr + exact Or.inr ⟨hs, hr⟩ + +/-- **The resume tail of ConfigureFlags** (resume, then reschedule or switch) for a bound SC. -/ +theorem cfgResume_walkA {p t : ObjRef} {σ : State Unit} {m : McState} {n : Nat} {y : SchedContext} {k : Nat} + {x : Tcb} (L : LoopInvA σ m n) (V : SchedValidA σ) (D : RqDomPrio σ) (ha : σ.schedulerAction = .resumeCurThread) + (hk : σ.kheap p = some (.schedContext y k)) (hy : y.scTcb = some t) (hx : σ.kheap t = some (.tcb x)) + (hxp : x.tcbSchedContext = some p) (hact : scActive y = true) (hz : σ.consumedTime ≠ 0 → p ≠ σ.curSc) + {r : Unit × State Unit} + (hr : r ∈ ((do + schedContextResume (Z := Unit) p + let tcbPtr ← Fl4Spec.NonDet.assertOpt (some t) + let st ← getThreadState tcbPtr + let ct ← Fl4Spec.NonDet.gets fun s : State Unit => s.curThread + if tcbPtr = ct then rescheduleRequired + else Fl4Spec.NonDet.when (runnable st) <| possibleSwitchTo tcbPtr) σ).1) : + LoopInvA r.2 m n ∧ SchedValidA r.2 ∧ r.2.kheap = σ.kheap ∧ r.2.consumedTime = σ.consumedTime ∧ + r.2.curSc = σ.curSc ∧ DomKeepA σ r.2 := by + obtain ⟨a, haR, hr⟩ := mem_bindA.1 hr + have R := schedContextResume_stepZA p L (fun hne hp => absurd hp (hz hne)) + (fun y' k' t' hk' ht' x' hx' => by + rw [hk] at hk' + cases hk' + rw [hy] at ht' + cases ht' + rw [hx] at hx' + cases hx' + exact hxp) haR + have S : a.2.kheap = σ.kheap ∧ a.2.consumedTime = σ.consumedTime ∧ a.2.schedulerAction = .resumeCurThread ∧ + SchedQA a.2 ∧ (t ≠ σ.curThread → SchedValidA a.2) ∧ DomKeepA σ a.2 := by + rcases resume_memA hk hy haR with e | ⟨hs, hp⟩ + · rw [e]; exact ⟨rfl, rfl, ha, V.toQA, fun _ => V, DomKeepA.refl σ⟩ + obtain ⟨eq, hq, hl⟩ := postpone_memA hk hy hx D hp + have ek : a.2.kheap = σ.kheap := by rw [eq] + have ec : a.2.consumedTime = σ.consumedTime := by rw [eq] + have ea : a.2.schedulerAction = σ.schedulerAction := by rw [eq] + have ect : a.2.curThread = σ.curThread := by rw [eq] + have ei : a.2.idleThread = σ.idleThread := by rw [eq] + have A : ∀ q, ScActiveAtA σ q → ScActiveAtA a.2 q := fun q ⟨z, j, h, c⟩ => ⟨z, j, by rw [ek]; exact h, c⟩ + have Q : SchedQA a.2 := by + refine ⟨fun d q u hu => ?_, fun u z sc hz hb hs' => ?_, fun u hu => ?_⟩ + · obtain ⟨m1, ne⟩ := hq d q u hu + obtain ⟨c1, c2⟩ := V.rq d q u m1 + exact ⟨ActTcbA.ofKh ek c1, fun h => (hl u h).elim ne c2⟩ + · rw [ek] at hz + exact A _ (V.blk u z sc hz hb hs') + · rcases hl u hu with e | h + · rw [e]; exact ActTcbA.ofKh ek (schedulable_actA hs).1 + · exact ActTcbA.ofKh ek (V.rls u h) + refine ⟨ek, ec, ea.trans ha, Q, fun ne => ⟨Q.rq, fun h1 h2 => ?_, Q.blk, Q.rls⟩, + ⟨by rw [eq], by rw [eq], by rw [eq], by rw [eq]⟩⟩ + rw [ea] at h1 + rw [ect, ei] at h2 + obtain ⟨c1, c2⟩ := V.act h1 h2 + rw [ect] + exact ⟨ActTcbA.ofKh ek c1, fun h => (hl _ h).elim (fun e => ne e.symm) c2⟩ + obtain ⟨ek, ec, eact, Q, V', D1⟩ := S + rw [nd_bind_oneSc (show Fl4Spec.NonDet.assertOpt (σ := State Unit) (some t) a.2 = ({(t, a.2)}, False) + from rfl)] at hr + have hx' : a.2.kheap t = some (.tcb x) := by rw [ek]; exact hx + unfold getThreadState at hr + rw [nd_bind_oneSc (threadGet_eqSc _ hx'), nd_gets_bindSc, R.cur] at hr + split_ifs at hr with hc + · obtain ⟨q, e⟩ := rescheduleRequired_memA hr + have eq := q.1 + have ek2 : r.2.kheap = a.2.kheap := by rw [eq] + have el : r.2.releaseQueue = a.2.releaseQueue := by rw [eq] + have noP : ∀ d p' u, u ∈ r.2.readyQueues d p' → u ∈ a.2.readyQueues d p' := fun d p' u hu => + (q.2 d p' u hu).resolve_right fun h => by cases eact.symm.trans h.1 + have A : ∀ q', ScActiveAtA a.2 q' → ScActiveAtA r.2 q' := fun q' ⟨z, j, h, c⟩ => + ⟨z, j, by rw [ek2]; exact h, c⟩ + refine ⟨R.inv.reschedule hr, ⟨fun d p' u hu => ?_, fun h => ?_, fun u z sc hz hb hs => ?_, fun u hu => ?_⟩, + ek2.trans ek, (show r.2.consumedTime = a.2.consumedTime by rw [eq]).trans ec, + (show r.2.curSc = a.2.curSc by rw [eq]).trans R.csc, + D1.trans ⟨by rw [eq], by rw [eq], by rw [eq], by rw [eq]⟩⟩ + · obtain ⟨c1, c2⟩ := Q.rq d p' u (noP d p' u hu) + exact ⟨ActTcbA.ofKh ek2 c1, by rw [el]; exact c2⟩ + · rw [e] at h; cases h + · rw [ek2] at hz + exact A _ (Q.blk u z sc hz hb hs) + · rw [el] at hu + exact ActTcbA.ofKh ek2 (Q.rls u hu) + · have V2 := V' hc + unfold Fl4Spec.NonDet.when at hr + split_ifs at hr with hrun + swap + · have : r = ((), a.2) := hr + rw [this]; exact ⟨R.inv, V2, ek, ec, R.csc, D1⟩ + have tI : ∀ c < n, m.idleThreadOf c ≠ t := fun c hcn e => by + obtain ⟨z, hz, hs⟩ := R.inv.sidle.f3 c hcn + rw [e, hx'] at hz + cases hz + rw [hs] at hrun + cases hrun + have ti : t ≠ a.2.idleThread := fun e => by + obtain ⟨z, hz, hs⟩ := R.inv.sinv.idleTcb + rw [← e, hx'] at hz + cases hz + rw [hs] at hrun + cases hrun + have ho : EnqOkA a.2 m n t := ⟨by rw [R.cur]; exact hc, tI, x, hx', hrun⟩ + obtain ⟨L2, ek2⟩ := R.inv.possibleSwitch ho ti hr + obtain ⟨qq, -⟩ := possibleSwitchTo_memA t hr + have ec2 : r.2.consumedTime = a.2.consumedTime := by rw [qq.1] + have cs2 : r.2.curSc = a.2.curSc := by rw [qq.1] + have act : ActTcbA a.2 t := ⟨x, p, hx', hxp, y, k, by rw [ek]; exact hk, hact⟩ + have F := possibleSwitchTo_fia t a.2 ⟨Sched.SchedValidA.toFIA V2, act⟩ _ hr + exact ⟨L2, F.1.toA F.2, ek2.trans ek, ec2.trans ec, cs2.trans R.csc, + D1.trans ⟨by rw [qq.1], by rw [qq.1], by rw [qq.1], by rw [qq.1]⟩⟩ + +/-! ## The walk -/ + +/-- **What ConfigureFlags leaves**: one heap object changed, the configured SC; `consumedTime` zeroed when +`commit_time` ran (a bound current SC). -/ +structure CfgSpecPostA (p : ObjRef) (x : SchedContext) (n : Nat) (σ τ : State Unit) : Prop where + other : ∀ q, q ≠ p → τ.kheap q = σ.kheap q + sc : ∃ y, τ.kheap p = some (.schedContext y n) ∧ ScFixA x y ∧ scActive y = true ∧ y.scRefills ≠ [] + consumed : τ.consumedTime = if x.scTcb ≠ none ∧ σ.curSc = p then 0 else σ.consumedTime + /-- The domain schedule is kept. -/ + dom : DomKeepA σ τ + +/-- The parameter selection (`refill_new` / `refill_update`) of ConfigureFlags, with its continuation `K`. -/ +theorem cfgSelect_walkA {p : ObjRef} {owner : Option ObjRef} {mr : Nat} {budget period : Ticks} {σ : State Unit} + (ht : ∀ t, owner = some t → ∃ z, σ.kheap t = some (.tcb z)) (hmr : 0 < mr) {β : Type} (K : SMonad Unit β) + {r : β × State Unit} + (hr : r ∈ ((if period = budget + then refillNew (Z := Unit) p MIN_REFILLS budget 0 >>= fun _ => K + else getScActive p >>= fun active => + if active ∧ owner ≠ none + then Fl4Spec.NonDet.assertOpt owner >>= fun tcbPtr => getThreadState tcbPtr >>= fun st => + if runnable st + then refillUpdate p period budget mr >>= fun _ => K + else refillNew p mr budget period >>= fun _ => K + else refillNew p mr budget period >>= fun _ => K) σ).1) : + ∃ τ, (ScAtA p σ τ ∧ ∃ k, 0 < k ∧ ScSetA p k τ) ∧ r ∈ (K τ).1 := by + have new : ∀ (k : Nat) (b per : Ticks), 0 < k → r ∈ ((refillNew (Z := Unit) p k b per >>= fun _ => K) σ).1 → + ∃ τ, (ScAtA p σ τ ∧ ∃ k, 0 < k ∧ ScSetA p k τ) ∧ r ∈ (K τ).1 := fun k b per h0 h => by + obtain ⟨a, ha, h⟩ := mem_bindA.1 h + exact ⟨a.2, ⟨refillNew_scAtA p k b per σ a ha, k, h0, refillNew_setA ha⟩, h⟩ + split_ifs at hr with e + · exact new _ _ _ (by decide) hr + obtain ⟨active, -, hr⟩ := eqPres_mem (getScActive_eqPresA p) hr + split_ifs at hr with h + swap + · exact new _ _ _ hmr hr + obtain ⟨t, he⟩ := Option.ne_none_iff_exists'.1 h.2 + obtain ⟨z, hz⟩ := ht t he + rw [he, nd_bind_oneSc (show Fl4Spec.NonDet.assertOpt (σ := State Unit) (some t) σ = ({(t, σ)}, False) from rfl)] at hr + unfold getThreadState at hr + rw [nd_bind_oneSc (threadGet_eqSc _ hz)] at hr + split_ifs at hr + · obtain ⟨a, ha, h⟩ := mem_bindA.1 hr + exact ⟨a.2, ⟨refillUpdate_scAtA p _ _ _ σ a ha, mr, hmr, refillUpdate_setA ha⟩, h⟩ + · exact new _ _ _ hmr hr + +theorem LoopInvA.consumedA {σ : State Unit} {m : McState} {n : Nat} (h : LoopInvA σ m n) {c : Ticks} + (hc : c = 0 ∨ c = σ.consumedTime) : LoopInvA { σ with consumedTime := c } m n := by + rcases hc with rfl | rfl + · have S := h.sinv + exact + { sinv := + { mc := S.mc, view := S.view, curTcb := S.curTcb, idleTcb := S.idleTcb, onlyIdle := S.onlyIdle, + idleSc := S.idleSc, curSc := S.curSc, ctQ := S.ctQ, idleQ := S.idleQ, rq := S.rq, rls := S.rls, + rpos := S.rpos + irqN := by + obtain ⟨⟨⟨hinj, hcn⟩, hdom⟩, hsl, href⟩ := S.irqN + exact ⟨⟨⟨hinj, hcn⟩, hdom⟩, hsl, href⟩ + zc := fun _ _ _ _ _ => rfl + rlsRun := S.rlsRun + curScReady := fun h0 => absurd rfl h0 + faultSt := S.faultSt } + sidle := { f3 := h.sidle.f3, f4 := h.sidle.f4, f5 := h.sidle.f5, f6 := h.sidle.f6.of_eq fun _ => rfl, + f7 := h.sidle.f7, idleSlot := h.sidle.idleSlot, idleBound := h.sidle.idleBound } + sact := { f1 := h.sact.f1, f2 := h.sact.f2 } } + · exact h + +theorem SchedValidA.consumedA {σ : State Unit} (h : SchedValidA σ) (c : Ticks) : + SchedValidA { σ with consumedTime := c } := { h with } + +/-- **Phase 1** (unqueue the bound thread, bill the current SC): a queue-only step, then `consumedTime := c` and an +SC-only step at `p`. -/ +theorem cfgPrep_walkA {p : ObjRef} {x : SchedContext} {n0 : Nat} {σ : State Unit} {m : McState} {n : Nat} + (L : LoopInvA σ m n) (V : SchedValidA σ) + (hk : σ.kheap p = some (.schedContext x n0)) (ht : ∀ t, x.scTcb = some t → ∃ z, σ.kheap t = some (.tcb z)) + {r : Unit × State Unit} + (hr : r ∈ ((Fl4Spec.NonDet.when (x.scTcb ≠ none) <| do + let tcbPtr ← Fl4Spec.NonDet.assertOpt x.scTcb + tcbReleaseRemove (Z := Unit) tcbPtr + tcbSchedAction tcbSchedDequeue tcbPtr + let curSc ← Fl4Spec.NonDet.gets fun s : State Unit => s.curSc + Fl4Spec.NonDet.when (curSc = p) fl4CommitTime) σ).1) : + ∃ b, QShrinkA σ b ∧ LoopInvA b m n ∧ SchedValidA b ∧ + ScAtA p { b with consumedTime := if x.scTcb ≠ none ∧ σ.curSc = p then 0 else σ.consumedTime } r.2 := by + have keep : ∀ b : State Unit, QShrinkA σ b → + { b with consumedTime := σ.consumedTime } = b := fun b Q => by + have h : b.consumedTime = σ.consumedTime := by rw [Q.rest] + rw [← h] + have Q0 : QShrinkA σ σ := ⟨rfl, fun _ _ _ h => h⟩ + unfold Fl4Spec.NonDet.when at hr + split_ifs at hr with hb + swap + · have e : r = ((), σ) := hr + refine ⟨σ, Q0, L, V, ?_⟩ + rw [if_neg (fun h => hb h.1), e] + exact PreO.refl _ + obtain ⟨t, he⟩ := Option.ne_none_iff_exists'.1 hb + obtain ⟨z, hz⟩ := ht t he + rw [he, nd_bind_oneSc (show Fl4Spec.NonDet.assertOpt (σ := State Unit) (some t) σ = ({(t, σ)}, False) + from rfl)] at hr + obtain ⟨a, haR, hr⟩ := mem_bindA.1 hr + have Ra := tcbReleaseRemove_stepA t L haR + have Qa := releaseRemove_shrinkA t haR + have Va := HT.tcbReleaseRemove t (fun _ => True) (fun _ _ h => h) (fun _ _ h => h) σ + ⟨Sched.SchedValidA.toFIA V, trivial⟩ _ haR + have hza : a.2.kheap t = some (.tcb z) := by rw [Qa.rest]; exact hz + obtain ⟨b, hbR, hr⟩ := mem_bindA.1 hr + have Rb := tcbSchedDeq_stepA t Ra.inv hbR + have Qb := dequeue_shrinkA hza hbR + have Vb := HT.deq t (fun _ => True) (fun _ _ h => h) a.2 ⟨Va.1, trivial⟩ _ hbR + have Q := Qa.trans Qb + have ec : b.2.curSc = σ.curSc := by rw [Q.rest] + have Vb' : SchedValidA b.2 := Vb.1.1.toA Vb.1.2 + refine ⟨b.2, Q, Rb.inv, Vb', ?_⟩ + rw [nd_gets_bindSc, ec] at hr + split_ifs at hr with hc + swap + · have e : r = ((), b.2) := hr + rw [if_neg (fun h => hc h.2), keep _ Q, e] + exact PreO.refl _ + obtain ⟨τ, S, e⟩ := commitTime_cfgA (ec.trans hc) hr + rw [if_pos ⟨hb, hc⟩, e] + refine ⟨?_, S.kh⟩ + have h := S.rest + show ({ τ with consumedTime := 0 } : State Unit) = _ + rw [h] + +/-- **The spec walk of FL4's ConfigureFlags** from an entry state. -/ +theorem cfg_spec_walkA (hmin : fl4MinBudget = MIN_BUDGET) {p : ObjRef} {x : SchedContext} {n0 mr : Nat} + {budget period : Ticks} {badge flag : Word} {σ : State Unit} {m : McState} {n : Nat} (L : LoopInvA σ m n) + (V : SchedValidA σ) (D : RqDomPrio σ) (ha : σ.schedulerAction = .resumeCurThread) + (hk : σ.kheap p = some (.schedContext x n0)) (hmr : 0 < mr) + (ht : ∀ t, x.scTcb = some t → ∃ z, σ.kheap t = some (.tcb z) ∧ z.tcbSchedContext = some p) + (hcb : σ.curSc = p → x.scTcb ≠ none) (hpi : p ≠ idleScPtr) + {r : Unit × State Unit} + (hr : r ∈ (invokeFl4SchedControlConfigureFlags (Z := Unit) + (.invokeSchedControlConfigureFlags p budget period mr badge flag) σ).1) : + LoopInvA r.2 m n ∧ SchedValidA r.2 ∧ CfgSpecPostA p x n0 σ r.2 := by + have ht' : ∀ t, x.scTcb = some t → ∃ z, σ.kheap t = some (.tcb z) := fun t h => + let ⟨z, hz, _⟩ := ht t h + ⟨z, hz⟩ + unfold invokeFl4SchedControlConfigureFlags at hr + dsimp only at hr + rw [nd_bind_oneSc (Sched.getSchedContext_eqA hk)] at hr + obtain ⟨u1, h1, hr⟩ := mem_bindA.1 hr + obtain ⟨b, Q, Lb, Vb, S1⟩ := cfgPrep_walkA L V hk ht' h1 + obtain ⟨c, hcdef⟩ : ∃ c, c = if x.scTcb ≠ none ∧ σ.curSc = p then 0 else σ.consumedTime := ⟨_, rfl⟩ + rw [← hcdef] at S1 + have hbc : b.consumedTime = σ.consumedTime := by rw [Q.rest] + have hbs : b.curSc = σ.curSc := by rw [Q.rest] + have Lc := Lb.consumedA (c := c) (by + rw [hcdef, hbc] + split + · exact Or.inl rfl + · exact Or.inr rfl) + -- with time consumed, the configured SC is not the current one + have hzc : c ≠ 0 → σ.curSc ≠ p := fun h0 e => h0 (by rw [hcdef, if_pos ⟨hcb e, e⟩]) + have Vc := SchedValidA.consumedA Vb c + have hkb : b.kheap = σ.kheap := by rw [Q.rest] + have hkc : ({ b with consumedTime := c } : State Unit).kheap p = some (.schedContext x n0) := by + show b.kheap p = _ + rw [hkb]; exact hk + obtain ⟨y1, hy1, F1⟩ := S1.sc hkc + have htu : ∀ t, x.scTcb = some t → ∃ z, u1.2.kheap t = some (.tcb z) := fun t h => by + obtain ⟨z, hz⟩ := ht' t h + exact ⟨z, S1.nonSc (show b.kheap t = _ by rw [hkb]; exact hz) (fun _ _ e => by cases e)⟩ + obtain ⟨τ2, h2, hr⟩ := cfgSelect_walkA (owner := x.scTcb) (mr := mr) (budget := budget) (period := period) + htu hmr _ hr + obtain ⟨S2, k, hk0, y2, n2, hy2, hmax, hne⟩ := h2 + have S12 : ScAtA p { b with consumedTime := c } τ2 := PreO.trans S1 S2 + obtain ⟨y2', hy2', F2⟩ := S12.sc hkc + rw [hy2] at hy2' + cases hy2' + have act2 : scActive y2 = true := by + unfold scActive activeSc + rw [hmax] + exact decide_eq_true hk0 + have L2 : LoopInvA τ2 m n := Lc.scAtA S12 (fun y j hy _ => by + rw [hy2] at hy + cases hy + exact hne) (fun h0 => by show b.curSc ≠ p; rw [hbs]; exact hzc h0) hpi + have V2 : SchedValidA τ2 := SchedValidA.scAtA Vc S12 fun _ _ _ => ⟨y2, n0, hy2, act2⟩ + have e2 := S12.rest + have a2 : τ2.schedulerAction = .resumeCurThread := by + rw [e2]; show b.schedulerAction = _; rw [Q.rest]; exact ha + have c2 : τ2.consumedTime = c := by rw [e2] + have cs2 : τ2.curSc = σ.curSc := by rw [e2]; exact hbs + have d2 : DomKeepA σ τ2 := + ⟨by rw [e2]; show b.domainIndex = _; rw [Q.rest], by rw [e2]; show b.domainStartIndex = _; rw [Q.rest], + by rw [e2]; show b.domainList = _; rw [Q.rest], by rw [e2]; show b.archState.armUsGlobalVspace = _; rw [Q.rest]⟩ + have D2 : RqDomPrio τ2 := fun f d q z hz hm => by + have erq : τ2.readyQueues = b.readyQueues := by rw [e2] + have hz0 : b.kheap f = some (.tcb z) := by + rcases S12.kh f with e | ⟨-, a1, a2', j, -, e3, -⟩ + · exact (show τ2.kheap f = b.kheap f from e) ▸ hz + · rw [hz] at e3; cases e3 + exact (rqDomPrio_shrinkA D Q) f d q z hz0 (erq ▸ hm) + have o2 : ∀ q, q ≠ p → τ2.kheap q = σ.kheap q := fun q hq => by + rw [S12.other hq] + show b.kheap q = _ + rw [hkb] + -- phase 3: resume + obtain ⟨u3, h3, hr⟩ := mem_bindA.1 hr + have P3 : LoopInvA u3.2 m n ∧ SchedValidA u3.2 ∧ u3.2.kheap = τ2.kheap ∧ u3.2.consumedTime = c ∧ + u3.2.curSc = σ.curSc ∧ DomKeepA σ u3.2 := by + unfold Fl4Spec.NonDet.when at h3 + split_ifs at h3 with hb + swap + · have e : u3 = ((), τ2) := h3 + rw [e]; exact ⟨L2, V2, rfl, c2, cs2, d2⟩ + obtain ⟨t, he⟩ := Option.ne_none_iff_exists'.1 hb + obtain ⟨z, hz, hzp⟩ := ht t he + have hz2 : τ2.kheap t = some (.tcb z) := by + rw [o2 t (fun h => by rw [h, hk] at hz; cases hz)]; exact hz + rw [fl4SchedContextResume_eq_l4v hmin, he] at h3 + obtain ⟨l, v, ek, ec, ecs, ed⟩ := cfgResume_walkA (t := t) L2 V2 D2 a2 hy2 (F2.1.trans he) hz2 hzp act2 + (fun h0 e => hzc (by rw [← c2]; exact h0) (by rw [← cs2]; exact e.symm)) h3 + exact ⟨l, v, ek, ec.trans c2, ecs.trans cs2, d2.trans ed⟩ + obtain ⟨L3, V3, k3, c3, cs3, d3⟩ := P3 + -- phase 4: badge and sporadic flag + have hy3 : u3.2.kheap p = some (.schedContext y2 n0) := by rw [k3]; exact hy2 + have S4 : PresR (ScAtA p) (do + setScObjRef (Z := Unit) scBadgeUpdate p badge + setScObjRef scSporadicUpdate p (testBit flag scSporadicFlag)) := + PresR.bind (ScAtA.upd p _ fun _ => ⟨rfl, rfl, rfl, rfl⟩) fun _ => ScAtA.upd p _ fun _ => ⟨rfl, rfl, rfl, rfl⟩ + have G4 : PresR (ScGrowA p) (do + setScObjRef (Z := Unit) scBadgeUpdate p badge + setScObjRef scSporadicUpdate p (testBit flag scSporadicFlag)) := + PresR.bind (ScGrowA.upd p _ fun _ => ⟨rfl, id⟩) fun _ => ScGrowA.upd p _ fun _ => ⟨rfl, id⟩ + have s4 := S4 u3.2 r hr + obtain ⟨y4, hy4, m4, ne4⟩ := G4 u3.2 r hr y2 n0 hy3 + have act4 : scActive y4 = true := by unfold scActive at act2 ⊢; rw [m4]; exact act2 + refine ⟨L3.scAtA s4 (fun y j hy _ => ?_) (fun h0 => by rw [cs3]; exact hzc (by rw [← c3]; exact h0)) hpi, + SchedValidA.scAtA V3 s4 fun _ _ _ => ⟨y4, n0, hy4, act4⟩, ?_, ?_, ?_, ?_⟩ + · rw [hy4] at hy + cases hy + exact ne4 hne + · intro q hq + rw [s4.other hq, k3, o2 q hq] + · obtain ⟨y4', hy4', F4⟩ := s4.sc hy3 + rw [hy4] at hy4' + cases hy4' + exact ⟨y4, hy4, F2.trans F4, act4, ne4 hne⟩ + · have : r.2.consumedTime = u3.2.consumedTime := by rw [s4.rest] + rw [this, c3, hcdef] + · exact d3.trans ⟨by rw [s4.rest], by rw [s4.rest], by rw [s4.rest], by rw [s4.rest]⟩ + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3ScConsumedA.lean b/proofs/Fl4ProofsA64/P3/Stage3ScConsumedA.lean new file mode 100644 index 000000000..dccfe01e2 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ScConsumedA.lean @@ -0,0 +1,521 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 3): **SchedContext Consumed at the record**. + +`decode_invocation` on a SchedContext cap with the Consumed label at a syscall entry (`JSyscallA.scConsumedA`): the +caller's Restart (`of_restartCurA`, the base record), `update_consumed` (the SC's `CONSUMED` word; spec +`schedContextUpdateConsumed`, a `RefK` step: `scConsumed_refKA`), then the one-word reply of the consumed time and the +caller's Running (`replyOneMidA`, the relations at the state after `update_consumed`). + +The reply part is `JSyscallA.consumedReplyA`, shared with YieldTo's reply arm. + +Named premises: [P] `ArchTotal`, `MaxSpi`, `ArchPlat`, `A64FrameMasks`, `hCs`, `hmax`, `htu`; [S] `h1` +(`numNodes = 1`, the remote stall). New module. +-/ +import Fl4ProofsA64.P3.Stage3Lift3QA +import Fl4ProofsA64.P3.Stage3ReplyMidA +import Fl4ProofsA64.P3.Stage3ScNtfnA +import Fl4ProofsA64.P3.ScConsumeOpA +import Fl4ProofsA64.P3.ScFrameA +import Fl4ProofsA64.P3.ScYieldQueueA +import Fl4ProofsA64.P3.ScYieldFrameA +import Fl4ProofsA64.P3.Stage3EntryProjA +import Fl4ProofsA64.P3.DelSinvLoopA +import Fl4ProofsA64.P3.Stage3TcbTlsSelfA +import Fl4ProofsA64.P3.Stage3TcbBodyA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-- The SC consumed-time update is a `RefK` step. -/ +theorem scConsumed_refKA {σ : State Unit} {p : ObjRef} {x : SchedContext} {k : Nat} (v : Word) + (hs : σ.kheap p = some (.schedContext x k)) : + RefK σ (updKSc σ p (.schedContext { x with scConsumed := v } k)) := by + refine ⟨rfl, fun r => ?_, fun hr => RdyA.updSc hr hs rfl (fun h => h)⟩ + simp only [updKSc_kheap, Function.update_apply] + by_cases h1 : r = p + · subst h1 + rw [if_pos rfl, hs] + exact .sc _ _ _ ⟨rfl, rfl, rfl, id⟩ + · rw [if_neg h1] + exact .same _ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s : S} {a : A} {σ : State Unit} {m : McState} + +/-- The remote stall on an SC cap's bound thread is a no-op on one node. -/ +theorem JSyscallA.scStallA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + {slot sc sz : U64} (hslot : slot ∈ OS) (hcap : capA st s slot = .SchedContext sc sz) : + (if L.mem s (scTcbA sc) != 0#u64 then smp.remote_tcb_stall st ar s a (L.mem s (scTcbA sc)) else ok (s, a)) = + ok (s, a) := by + have T0 := J.toJStage3A.toTA J.sact J.noSwitch + have hH : H sc = some (.sc sz.val) := (T0.capTy slot hslot).2.2.2.1 sc sz hcap + have hcH : sc ∈ H.scs := ⟨_, hH⟩ + by_cases h0 : L.mem s (scTcbA sc) = 0#u64 + · rw [h0]; rfl + · have ht : L.mem s (scTcbA sc) ∈ Z.T := by rw [← J.tcbs]; exact T0.blkA.2.2.1.2.2.yt.1 sc hcH h0 + rw [if_pos (bne_iff_ne.2 h0)] + exact T0.stall_singleA hCs h1 ht + +/-- The caller's `Restart` at a syscall entry. -/ +theorem JSyscallA.restartCurEqA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {slot : U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) : + sched.set_thread_state st s (L.curW s) sched.TS_RESTART = ok (L.wr s (stA (L.curW s)) sched.TS_RESTART) := by + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hslot hcov J.actW J.running J.cur + have hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM := J.toJStage3EntryA.curNotVmA + obtain ⟨-, -, hst, -⟩ := J.toJStage3A.of_restartCurA RS.pl.choose_spec.choose_spec.choose_spec.choose_spec + RS.sym RS.run hnvm RS.cur RS.vs RS.vc + exact hst + +/-- A TCB word other than the SC link and the ready-queue words is outside the scheduler write set. -/ +theorem tcbWordNotMWrA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRN st Z m κ s σ) {t K : U64} (ht : t ∈ Z.T) (hK : K.val < 4096) (h1 : K.val ≠ 3464) + (h2 : K.val ≠ 3480) (h3 : K.val ≠ 3488) (h4 : K.val ≠ 3592) : ¬ MWrA (st := st) Z m σ s (wa t K) := by + obtain ⟨hks, hsc⟩ := yieldTcbOffA H ht hK + refine not_mwrA hsc (fun u hu => ?_) hks + have field (k : U64) (hk : k.val < 4096) (hne : K.val ≠ k.val) : wa t K ≠ wa u k := + field_ne (H.core.tcbs t ht) (H.core.tcbs u hu) hK hk (fun _ => hne) + exact ⟨field 3464#u64 (by simp) (by simpa using h1), field 3480#u64 (by simp) (by simpa using h2), + field 3488#u64 (by simp) (by simpa using h3), field 3592#u64 (by simp) (by simpa using h4)⟩ + +set_option maxHeartbeats 4000000 in +/-- **The consumed-time reply after a ready-queue change**: from the caller's `Restart` (`s1`, `σ1`), a step to +`(sq, σq)` that changes only the ready queues (spec) and scheduler words (concrete), then `update_consumed`, the +one-word reply and `Running`. Consumed takes the empty step; YieldTo's requeue arm the dequeue/enqueue of the target. -/ +theorem JSyscallA.consumedMidA (hA : P0.ArchTotal ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {slot sc sz : U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .SchedContext sc sz) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (call : Bool) + {xc : Tcb} (hkc : σ.kheap σ.curThread = some (.tcb xc)) + {sq : S} {σq : State Unit} {Q' : U64 → U64 → List U64} + (hQ : YieldQueueRelA (st := st) { Z with Q := Q' } m κ sq σq) + (wq : WritesIn st (L.wr s (stA (L.curW s)) sched.TS_RESTART) sq + (MWrA (st := st) Z m (stUpdA σ σ.curThread xc .restart) (L.wr s (stA (L.curW s)) sched.TS_RESTART))) + (eqσ : σq = { stUpdA σ σ.curThread xc .restart with readyQueues := σq.readyQueues }) + (hscq : ∀ u ∈ Z.T, L.mem sq (scA u) = L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART) (scA u)) + (hiqq : L.mem sq (inqA (L.curW s)) = L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART) (inqA (L.curW s))) + (LIq : LoopInvA σq m n) (svq : SchedValidA σq) + (blkq : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac sq) + (fin : ∀ c su, schedcontext.update_consumed st ar sq a sc = ok (c, su) → + ∀ s', vspace.reply1 st su (L.curW s) call c = ok s' → ∀ σX : State Unit, + SysL st H Z.Cs OS ia ie s' ∧ SymL st H Z.Cs s' ∧ Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s' ∧ + J7A st (J7R2A st ar nA) Z H OS s' σX m) : + ∃ c su σ2 σ3 s3, schedcontext.update_consumed st ar sq a sc = ok (c, su) ∧ + Fl4SpecA64.returnConsumed (Z := Unit) (scNameA m sc) σq = ({(wordsFromTime c.bv, σ2)}, False) ∧ + ((), σ3) ∈ ((do + Fl4Spec.NonDet.when call (Fl4SpecA64.replyFromKernel (Z := Unit) σ.curThread (0, wordsFromTime c.bv)) + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .running) σ2).1 ∧ + vspace.reply1 st su (L.curW s) call c = ok s3 ∧ + ∃ Q Rl, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s3 a σ3 m ∧ Sched.BodyEndA σ σ3 m := by + -- the entry: restart, the SC, the stall + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hslot hcov J.actW J.running J.cur + have hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM := J.toJStage3EntryA.curNotVmA + obtain ⟨xc', hkc', hst, hsp, J1, -, -, -⟩ := J.toJStage3A.of_restartCurA RS.pl.choose_spec.choose_spec.choose_spec.choose_spec + RS.sym RS.run hnvm RS.cur RS.vs RS.vc + have exc : xc' = xc := by rw [hkc] at hkc'; cases hkc'; rfl + subst xc' + have T0 := J.toJStage3A.toTA J.sact J.noSwitch + have hH : H sc = some (.sc sz.val) := (T0.capTy slot hslot).2.2.2.1 sc sz hcap + have hcH : sc ∈ H.scs := ⟨_, hH⟩ + have hc : sc ∈ Z.C := by rw [← J.scs]; exact hcH + have hci : specPtr sc ≠ m.idleScOf m.curCore := T0.ciSc slot hslot sc sz hcap + have hname : scNameA m sc = specPtr sc := by unfold scNameA; rw [if_neg hci] + set s1 := L.wr s (stA (L.curW s)) sched.TS_RESTART with hs1 + set σ1 := stUpdA σ σ.curThread xc .restart with hσ1 + have kq : σq.kheap = σ1.kheap := congrArg (fun z => z.kheap) eqσ + have ctq : σq.curThread = σ1.curThread := congrArg (fun z => z.curThread) eqσ + have rlq : σq.releaseQueue = σ1.releaseQueue := congrArg (fun z => z.releaseQueue) eqσ + -- the current thread's words across the restart + have ct : σ.curThread = specPtr (L.curW s) := J.j.1.sch.curThread + set t := L.curW s with htdef + have htZ : t ∈ Z.T := J.sr.curT + have hta : TcbAddr t := J.sr.core.tcbs _ htZ + have sA : Aligned (stA t) := by rw [stA_eq]; exact field_aligned hta (by simp) (by simp) + have far : ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → K.val ≠ 3328 → L.mem s1 (wa t K) = L.mem s (wa t K) := + fun K h1 h2 h3 => mem_wr_ne' sA (field_aligned hta h1 h2) + (by rw [stA_eq]; exact field_ne (K2 := K) hta hta (by simp) h1 (fun _ => by simpa using Ne.symm h3)) + have hcurq : L.curW sq = t := specPtr_inj (hQ.sched.sch.curThread.symm.trans (ctq.trans ct)) + have hP := J1.iplaced + -- the relations after the ready-queue step + have qk : ∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → K.val ≠ 3464 → K.val ≠ 3480 → K.val ≠ 3488 → + K.val ≠ 3592 → L.mem sq (wa u K) = L.mem s1 (wa u K) := fun u hu K k1 k2 n1 n2 n3 n4 => + wq.mem_eq (field_aligned (J1.sr.core.tcbs u hu) k1 k2) (tcbWordNotMWrA J1.sr.toSchedSRN hu k1 n1 n2 n3 n4) + have srq : SchedSRA st { Z with Q := Q' } m κ sq σq := hQ.sched + have epq : EpStateRel Z.T E Rp Bf (L.mem sq) σq := + J1.ep.frame hP.e (fun b hb hf => wq.mem_eq hb fun h => endpFootOffWrite J1.sr.toSchedSRN J1.ipcPlace hf h) + ⟨fun _ _ h => by rw [kq]; exact h, fun _ x h => ⟨x, by rw [kq]; exact h, rfl⟩, + fun _ y h => ⟨y, by rw [kq]; exact h, rfl, rfl, rfl, rfl, rfl⟩, + congrArg (fun z => z.machineState.underlyingMemory) eqσ⟩ + have ntq : NtfnStateRel N Z.T (L.mem sq) σq := + J1.ntfn.frame hP.n (fun b hb hf => wq.mem_eq hb fun h => + notifFootOffMWrA J1.sr.toSchedSRN J1.ipcPlace.notification hf h) + ⟨fun _ _ h => by rw [kq]; exact h, fun _ y h => ⟨y, by rw [kq]; exact h, rfl, rfl, rfl⟩⟩ + have rpq : ReplyStackRelA (L.mem sq) Rp Z.C σq (scNameA m) stack := by + refine J1.reply.frame (fun r hr => ?_) (fun _ _ y k h => ⟨y, k, by rw [kq]; exact h, rfl⟩) + (fun r _ x' h => ⟨x', by rw [kq] at h; exact h, rfl⟩) + have hr3 := hP.e.rep r hr + have one : ∀ O : U64, O.val = 8 ∨ O.val = 16 → L.mem sq (wa r O) = L.mem s1 (wa r O) := by + intro O hO + have hv : (wa r O).val = r.val + O.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + refine wq.mem_eq (by unfold Aligned; rw [hv]; omega) fun h => ?_ + exact otherWordOffMWrA J1.sr.toSchedSRN (J1.ipcPlace.reply r hr) (by omega) (by omega) h + exact ⟨one _ (Or.inr (by simp [layout.reply.NEXT])), one _ (Or.inl (by simp [layout.reply.PREV]))⟩ + have rvq : RunVcpuInv st Z.T sq := fun u hu h0 => by + have e1 : L.mem sq (runVcpuA u) = L.mem s1 (runVcpuA u) := by + unfold runVcpuA + exact qk u hu 3672#u64 (by simp) (by simp) (by simp) (by simp) (by simp) (by simp) + have e2 : L.mem sq (Fl4ProofsA64.P1.stA u) = L.mem s1 (Fl4ProofsA64.P1.stA u) := by + rw [Fl4ProofsA64.P1.stA_eq] + exact qk u hu 3328#u64 (by simp) (by simp) (by simp) (by simp) (by simp) (by simp) + rw [e2]; exact J1.runVcpu u hu (e1 ▸ h0) + have bqq : BlkQueuedA Z.T E N (L.mem sq) σq := + J1.blkQ.keepA (fun u hu => by + rw [Notif.tBlockA_eq] + exact qk u hu 3336#u64 (by simp) (by simp) (by simp) (by simp) (by simp) (by simp)) + (fun u _ y h => Or.inr ⟨y, by rw [kq] at h; exact h, rfl⟩) + (fun q y h => by rw [kq]; exact h) (fun q y h => by rw [kq]; exact h) + -- update_consumed at the base record + obtain ⟨⟨c, su⟩, hu, x, n0, v, hk, hsp2, esu, wsu, srU⟩ := + exists_of_specA (update_consumed_refinesA srq a hc hmax (htu a)) + simp only at hsp2 esu wsu srU + set σu := updKSc σq (scNameA m sc) (.schedContext { x with scConsumed := v.bv } n0) with hσu + have notSc : ∀ b, ScObjA sc n0 b → ScFootA Z.C m σq b := fun b hb => ⟨sc, hc, x, n0, hk, hb⟩ + have thOff : ∀ b, ScObjA sc n0 b → ∀ q k, Z.th q = some k → b.val < q.val ∨ q.val + k.size ≤ b.val := + fun b hb q k hq => srq.scp.th_out (notSc b hb) hq + have tcbW : ∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → L.mem su (wa u K) = L.mem sq (wa u K) := by + intro u hu K k1 k2 + have hua : TcbAddr u := J1.sr.core.tcbs u hu + refine wsu.mem_eq (field_aligned hua k1 k2) fun hb => ?_ + have h := thOff _ hb u .tcb ((J1.sr.thT u).2 hu) + rw [Notif.tfield_val hua k1] at h + have sz : ObjKindA.size .tcb = 4096 := rfl + omega + have hcurU : L.curW su = t := by + have e1 := srU.sch.curThread + have e0 : σu.curThread = specPtr t := (srq.sch.curThread).trans (congrArg specPtr hcurq) + exact specPtr_inj (e1.symm.trans e0) + have tw1 : ∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → K.val ≠ 3464 → K.val ≠ 3480 → K.val ≠ 3488 → + K.val ≠ 3592 → L.mem su (wa u K) = L.mem s1 (wa u K) := fun u hu K k1 k2 n1 n2 n3 n4 => + (tcbW u hu K k1 k2).trans (qk u hu K k1 k2 n1 n2 n3 n4) + -- the spec step and the mid relations + have Fb : SchedFrameA σ1 σu := PreO.trans + (SchedFrameA.of_eq (σ := σ1) (σ' := σq) kq (congrArg (fun z => z.interruptStates) eqσ) + (congrArg (fun z => z.interruptIrqNode) eqσ) (congrArg (fun z => z.archState.armAsidTable) eqσ) + (congrArg (fun z => z.cdt) eqσ) (congrArg (fun z => z.isOriginalCap) eqσ) + (congrArg (fun z => z.machineState.underlyingMemory) eqσ) + (congrArg (fun z => z.archState.armCurrentVcpu) eqσ)) + (SchedFrameA.upd_sc hk) + have RK : RefK σq σu := scConsumed_refKA v.bv hk + have keepO : ∀ q o, σq.kheap q = some o → (∀ y k, o ≠ .schedContext y k) → σu.kheap q = some o := + fun q o h n1 => by + rw [hσu, updKSc_kheap, Function.update_of_ne (fun e => by rw [e, hk] at h; exact n1 x n0 (Option.some.inj h).symm)] + exact h + have backO : ∀ q o, σu.kheap q = some o → (∀ y k, o ≠ .schedContext y k) → σq.kheap q = some o := + fun q o h n1 => by + by_cases e : q = scNameA m sc + · rw [e, hσu, updKSc_kheap, Function.update_self] at h + exact absurd (Option.some.inj h).symm (n1 _ _) + · rw [hσu, updKSc_kheap, Function.update_of_ne e] at h; exact h + have scK : ∀ q y k, σq.kheap q = some (.schedContext y k) → ∃ y', σu.kheap q = some (.schedContext y' k) ∧ + y'.scReplies = y.scReplies ∧ y'.scYieldFrom = y.scYieldFrom := fun q y k h => by + by_cases e : q = scNameA m sc + · rw [e, hk] at h; cases h + exact ⟨{ x with scConsumed := v.bv }, by rw [e, hσu, updKSc_kheap, Function.update_self], rfl, rfl⟩ + · exact ⟨y, by rw [hσu, updKSc_kheap, Function.update_of_ne e]; exact h, rfl, rfl⟩ + have scB : ∀ q y k, σu.kheap q = some (.schedContext y k) → ∃ y0, σq.kheap q = some (.schedContext y0 k) ∧ + y.scYieldFrom = y0.scYieldFrom := fun q y k h => by + by_cases e : q = scNameA m sc + · rw [e, hσu, updKSc_kheap, Function.update_self] at h; cases h + exact ⟨x, e ▸ hk, rfl⟩ + · rw [hσu, updKSc_kheap, Function.update_of_ne e] at h; exact ⟨y, h, rfl⟩ + have Kb : IpcKeepA σ1 σu := + ⟨fun q y h => ⟨y, keepO q _ (by rw [kq]; exact h) (fun _ _ e => by cases e), rfl, rfl, rfl, rfl, rfl, rfl⟩, + fun q y k h => by obtain ⟨y', h', -, e⟩ := scK q y k (by rw [kq]; exact h); exact ⟨y', k, h', e⟩, + fun q y k h => by obtain ⟨y0, h0, e⟩ := scB q y k h; exact ⟨y0, k, by rw [kq] at h0; exact h0, e⟩⟩ + have hk1 : σ1.kheap (scNameA m sc) = some (.schedContext x n0) := by rw [← kq]; exact hk + have wb : WritesIn st s1 su (SchedStepNWA (st := st) N E Rp Z m σ1 s1) := + (wq.trans (wsu.mono fun b hb => mwr_scObj (m := m) (s := sq) hc hk hb)).mono fun b hb => + Or.inl (Or.inl (hb.elim id fun h => mwr_back (Z' := { Z with Q := Q' }) rfl rfl + (SSzA.of_kheap kq) wq.1 h)) + have epM : EpStateRel Z.T E Rp Bf (L.mem su) σu := + epq.frame hP.e (fun b hb hf => wsu.mem_eq hb fun hso => + endpFootOffWrite srq.toSchedSRN J1.ipcPlace hf (mwr_scObj hc hk hso)) + (endpKView_updA hk (fun _ e => by cases e) (fun _ e => by cases e) (fun _ e => by cases e)) + have ntM : NtfnStateRel N Z.T (L.mem su) σu := by + refine ntq.frame hP.n (fun b hb hf => wsu.mem_eq hb fun hso => ?_) + (ntfnKView_updA hk (fun _ e => by cases e) (fun _ e => by cases e)) + rcases hf with ⟨q, hq, h1, h2⟩ | ⟨u, hu, h⟩ + · have := thOff b hso q _ (J1.ipcPlace.notification q hq) + simp only [ObjKindA.size] at this + omega + · have hua := hP.n.tcb u hu + have v4 : ∀ K : U64, K.val < 4096 → (wa u K).val = u.val + K.val := fun K hK => Notif.tfield_val hua hK + have rng : u.val ≤ b.val ∧ b.val < u.val + 4096 := by + rcases h with h | h | h | h | h | h <;> rw [h] + · rw [stA_eq, v4 _ (by simp)]; simp + · rw [Notif.tBlockA_eq, v4 _ (by simp)]; simp + · rw [Notif.badgeRegA_eq, v4 _ (by simp)]; simp + · rw [Notif.prioA_eq', v4 _ (by simp)]; simp + · rw [Notif.nextEp_eq, v4 _ (by simp)]; simp + · rw [Notif.prevEp_eq, v4 _ (by simp)]; simp + have := thOff b hso u .tcb ((J1.sr.thT u).2 hu) + have sz : ObjKindA.size .tcb = 4096 := rfl + omega + have rpM : ReplyStackRelA (L.mem su) Rp Z.C σu (scNameA m) stack := by + refine rpq.frame (fun r hr => ?_) (fun c' _ y k h => by + obtain ⟨y', h', e, -⟩ := scK _ y k h; exact ⟨y', k, h', e⟩) + (fun r _ x' h => ⟨x', backO _ _ h (fun _ _ e => by cases e), rfl⟩) + have hr3 := hP.e.rep r hr + have one : ∀ O : U64, O.val = 8 ∨ O.val = 16 → L.mem su (wa r O) = L.mem sq (wa r O) := by + intro O hO + have hv : (wa r O).val = r.val + O.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + refine wsu.mem_eq (by unfold Aligned; rw [hv]; omega) fun hso => ?_ + have := thOff _ hso r _ (J1.ipcPlace.reply r hr) + simp only [ObjKindA.size] at this + omega + exact ⟨one _ (Or.inr (by simp [layout.reply.NEXT])), one _ (Or.inl (by simp [layout.reply.PREV]))⟩ + have mcpM : McpRelA Z.T m (L.mem su) σu := + hQ.mcp.congrA (fun u hu => ⟨tcbW u hu _ (by simp) (by simp), tcbW u hu _ (by simp) (by simp)⟩) + fun u hu y hy => ⟨y, keepO _ _ hy (fun _ _ e => by cases e), rfl, rfl⟩ + have ye : ∀ u ∈ Z.T, L.mem su (yieldA u) = L.mem sq (yieldA u) := fun u hu => by + unfold yieldA; exact tcbW u hu _ (by simp) (by simp) + have yM : YieldRelA Z.T Z.C m (L.mem su) σu := by + refine ⟨fun u hu => ?_, fun u hu h => ?_, fun c' hc' y k h p hp => ?_⟩ + · obtain ⟨y, hy, e⟩ := hQ.links.tcb u hu + exact ⟨y, keepO _ _ hy (fun _ _ e => by cases e), by rw [ye u hu]; exact e⟩ + · rw [ye u hu] at h ⊢ + obtain ⟨hC, y, k, hy, e⟩ := hQ.links.toSc u hu h + obtain ⟨y', hy', -, e'⟩ := scK _ y k hy + exact ⟨hC, y', k, hy', e'.trans e⟩ + · obtain ⟨y0, h0, e⟩ := scB _ y k h + obtain ⟨u, hu, e1, e2⟩ := hQ.links.frm c' hc' y0 k h0 p (e ▸ hp) + exact ⟨u, hu, e1, by rw [ye u hu]; exact e2⟩ + have LI := LIq.refK RK + have rvM : RunVcpuInv st Z.T su := fun u hu h0 => by + have e1 : L.mem su (runVcpuA u) = L.mem sq (runVcpuA u) := by + unfold runVcpuA; exact tcbW u hu 3672#u64 (by simp) (by simp) + have e2 : L.mem su (Fl4ProofsA64.P1.stA u) = L.mem sq (Fl4ProofsA64.P1.stA u) := by + rw [Fl4ProofsA64.P1.stA_eq]; exact tcbW u hu 3328#u64 (by simp) (by simp) + rw [e2]; exact rvq u hu (e1 ▸ h0) + have bqM : BlkQueuedA Z.T E N (L.mem su) σu := + bqq.keepA (fun u hu => by rw [Notif.tBlockA_eq]; exact tcbW u hu 3336#u64 (by simp) (by simp)) + (fun u _ y h => Or.inr ⟨y, backO _ _ h (fun _ _ e => by cases e), rfl⟩) + (fun q y h => keepO q _ h (fun _ _ e => by cases e)) (fun q y h => keepO q _ h (fun _ _ e => by cases e)) + have blkM : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac su := by + have q := schedcontext.update_consumed.blk (st := st) hA sq a sc blkq hcH + rw [hu, WP.spec_ok] at q + exact q.1 + -- the current thread's facts at the mid state + have hx1 : σu.kheap σu.curThread = some (.tcb { xc with tcbState := .restart }) := by + have h0 : σq.kheap σq.curThread = some (.tcb { xc with tcbState := .restart }) := by + rw [kq, ctq] + simp only [hσ1, stUpdA, setObjA, Function.update_self] + exact keepO _ _ h0 (fun _ _ e => by cases e) + have hsc : L.mem s (scA t) ≠ 0#u64 := RS.cur.2.1 + set w := L.mem s (scA t) with hw + have hscU : L.mem su (scA t) = w := + (tcbW t htZ 3464#u64 (by simp) (by simp)).trans ((hscq t htZ).trans (far 3464#u64 (by simp) (by simp) (by simp))) + have hiqU : L.mem su (inqA t) = L.mem s (inqA t) := + (tcbW t htZ 3592#u64 (by simp) (by simp)).trans (hiqq.trans (far 3592#u64 (by simp) (by simp) (by simp))) + have hwC : w ∈ Z.C := J.j.1.rest.scMem t htZ hsc + have hscx : xc.tcbSchedContext = some (scRenA m w) := by + obtain ⟨y, hy, -, hsx⟩ := J.j.1.sch.tcb t htZ + rw [← ct, hkc] at hy; cases hy + rw [hsx]; unfold optScA; rw [← hw, if_neg hsc] + have hpos : ∃ y k, σu.kheap (scRenA m w) = some (.schedContext y k) ∧ 0 < y.scRefillMax := by + have hbx := J.sys.ent.bxi + obtain ⟨-, -, -, -, hS⟩ := hbx + have hwH : w ∈ H.scs := by rw [J.scs]; exact hwC + have hrm : L.rmax s w ≠ 0#u64 := (hS.act w hwH).1 RS.cur.2.2 + obtain ⟨y, k, ey, eyr⟩ := J.j.1.rest.refillMax w hwC + have e3 : L.rmax s w = L.mem s (wa w 72#u64) := by unfold LawfulStore.rmax; rw [maxA_eqA] + rw [e3] at hrm + have hp0 : 0 < y.scRefillMax := by + rw [eyr]; exact Nat.pos_of_ne_zero (fun e => hrm (UScalar.eq_of_val_eq (by simpa using e))) + have ey1 : σ1.kheap (scRenA m w) = some (.schedContext y k) := by + simp only [hσ1, stUpdA, setObjA] + rw [Function.update_of_ne (fun e => by rw [e, hkc] at ey; cases ey)]; exact ey + obtain ⟨y', ey', E⟩ := (RK.2.1 _).scF (show σq.kheap (scRenA m w) = some (.schedContext y k) by rw [kq]; exact ey1) + exact ⟨y', k, ey', by rw [E.2.2.1]; exact hp0⟩ + have hrq : σu.curThread ∉ σu.releaseQueue := by + show σq.curThread ∉ σq.releaseQueue + rw [ctq, rlq] + show σ.curThread ∉ σ.releaseQueue + rw [ct, J.j.1.rest.rel]; intro hm + obtain ⟨q, hq, eq⟩ := List.mem_map.1 hm + have e' : q = t := specPtr_inj eq + subst e' + have haf := J.sq.inR _ J.sq.cur _ hq + exact ((J.sq.released _ (by rw [J.tcbs]; exact htZ)).2 (by rw [haf]; exact hq)) RS.cur.1 + -- the reply + have Qb : QKeepA σ1 σu := QKeepA.ofBwd (D := ⟨congrArg (fun z => z.domainIndex) eqσ, + congrArg (fun z => z.domainStartIndex) eqσ, congrArg (fun z => z.domainList) eqσ, congrArg (fun z => z.archState.armUsGlobalVspace) eqσ⟩) fun q o h => by + rw [← kq] + simp only [hσu, updKSc_kheap, Function.update_apply] at h + split at h + · rename_i e + cases h + exact ⟨.schedContext x n0, by rw [e]; exact hk, QObjA.ofSc fun hr => ⟨hr, id⟩⟩ + · exact ⟨o, h, QObjA.refl o⟩ + have R := J1.replyOneMidA (Q0 := Q') (R0 := Z.R) wb Fb Kb.toF Qb + (fun u hu O h h8 => tw1 u hu O (by omega) h8 (by omega) (by omega) (by omega) (by omega)) srU epM ntM rpM mcpM yM LI.sinv LI.sidle rvM bqM (svq.refKA RK) blkM + srU.sch.curThread hx1 rfl (by rw [hcurU, hscU]; exact hsc) (by rw [hcurU, hscU]; exact RS.cur.2.2) + (by rw [hcurU, hiqU]; exact RS.cur.1) hrq (by rw [hcurU, hscU]; exact hwC) + (by rw [hcurU, hscU]; exact hscx) (by rw [hcurU, hscU]; exact hpos) call c + (fun s' h2 σX => fin c su hu s' (by rw [← hcurU]; exact h2) σX) + rw [hcurU] at R + obtain ⟨s3, e3, σ3, hrun, hsh, Q, Rl, J2⟩ := exists_of_specA R + have hwft : wordsFromTime c.bv = [c.bv] := by simp [wordsFromTime, ucast] + refine ⟨c, su, σu, σ3, s3, hu, ?_, by rw [hwft, ← (show σu.curThread = σ.curThread from ctq)]; exact hrun, e3, Q, Rl, J2, + Sched.BodyEndA.ofConsumedReply J hkc eqσ hk hσu hsh⟩ + change Fl4SpecA64.returnConsumed (Z := Unit) (scNameA m sc) σq = _ + unfold Fl4SpecA64.returnConsumed + rw [nd_bind_oneSc hsp2] + rfl + +set_option maxHeartbeats 4000000 in +/-- **The consumed-time reply at a syscall entry**: the caller's `Restart`, `update_consumed` on the SC of the slot's +cap, the one-word reply and `Running` (shared by Consumed and YieldTo's reply arm). `fin` is `decodeFinA`'s output for +the caller's decode at the final state. -/ +theorem JSyscallA.consumedReplyA (hA : P0.ArchTotal ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {slot sc sz : U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .SchedContext sc sz) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (call : Bool) + (fin : ∀ c su, schedcontext.update_consumed st ar (L.wr s (stA (L.curW s)) sched.TS_RESTART) a sc = ok (c, su) → + ∀ s', vspace.reply1 st su (L.curW s) call c = ok s' → ∀ σX : State Unit, + SysL st H Z.Cs OS ia ie s' ∧ SymL st H Z.Cs s' ∧ Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s' ∧ + J7A st (J7R2A st ar nA) Z H OS s' σX m) : + ∃ xc σ1 c su σ2 σ3 s3, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + schedcontext.update_consumed st ar (L.wr s (stA (L.curW s)) sched.TS_RESTART) a sc = ok (c, su) ∧ + Fl4SpecA64.returnConsumed (Z := Unit) (scNameA m sc) σ1 = ({(wordsFromTime c.bv, σ2)}, False) ∧ + ((), σ3) ∈ ((do + Fl4Spec.NonDet.when call (Fl4SpecA64.replyFromKernel (Z := Unit) σ.curThread (0, wordsFromTime c.bv)) + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .running) σ2).1 ∧ + vspace.reply1 st su (L.curW s) call c = ok s3 ∧ + ∃ Q Rl, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s3 a σ3 m ∧ Sched.BodyEndA σ σ3 m := by + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hslot hcov J.actW J.running J.cur + have hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM := J.toJStage3EntryA.curNotVmA + obtain ⟨xc, hkc, hst, hsp, J1, -, -, -⟩ := J.toJStage3A.of_restartCurA RS.pl.choose_spec.choose_spec.choose_spec.choose_spec + RS.sym RS.run hnvm RS.cur RS.vs RS.vc + have sact1 : SActA (stUpdA σ σ.curThread xc .restart) := + ⟨fun u h => J.sact.f1 u h, ⟨{ xc with tcbState := .restart }, by simp only [stUpdA, setObjA, + Function.update_self], Or.inr (Or.inl rfl)⟩⟩ + obtain ⟨c, su, σ2, σ3, s3, hu, hret, hrun, e3, hJ⟩ := + J.consumedMidA hA hmax htu hslot hcov hcap hext call hkc (Q' := Z.Q) J1.yieldQueueA (WritesIn.refl _ _) rfl + (fun _ _ => rfl) rfl ⟨J1.sinv, J1.sidle, sact1⟩ J1.schedValid (BXI.blk J1.sys.ent.bxi) fin + exact ⟨xc, _, c, su, σ2, σ3, s3, hkc, rfl, hsp, hu, hret, hrun, e3, hJ⟩ + +/-- `returnConsumed` writes only the scheduling context: a thread's state is kept. -/ +theorem returnConsumed_stateA {σ σ2 : State Unit} {p t : ObjRef} {y : SchedContext} {k : Nat} {x : Tcb} + {d : List Data} (hk : σ.kheap p = some (.schedContext y k)) (hx : σ.kheap t = some (.tcb x)) + (hr : x.tcbState = .restart) (hret : returnConsumed (Z := Unit) p σ = ({(d, σ2)}, False)) : + getThreadState (Z := Unit) t σ2 = ({(.restart, σ2)}, False) := by + obtain ⟨tm, y', -, he⟩ := yieldConsumed_viewA hk + have e1 : returnConsumed (Z := Unit) p σ = + ({(wordsFromTime tm, updKSc σ p (.schedContext y' k))}, False) := by + unfold returnConsumed + rw [nd_bind_oneSc he] + rfl + have e2 : updKSc σ p (.schedContext y' k) = σ2 := + (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst (e1.symm.trans hret)))).2 + have ne : t ≠ p := fun e => by rw [e, hk] at hx; cases hx + have hk2 : σ2.kheap t = some (.tcb x) := by + rw [← e2, updKSc_kheap, Function.update_of_ne ne]; exact hx + rw [← hr] + exact threadGet_eqSc Tcb.tcbState hk2 + +set_option maxHeartbeats 4000000 in +/-- **SchedContext Consumed at a syscall entry** (module docstring). -/ +theorem JSyscallA.scConsumedA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {slot lbl len buf sc sz : U64} {blk call don : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .SchedContext sc sz) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hlbl : lbl = fl4_abi.label.SchedContextConsumed) : + ∃ xc σ1 σ2 d σ3 s3, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + invokeSchedContext (Z := Unit) (.invokeSchedContextConsumed (specPtr sc)) σ1 = ({(d, σ2)}, False) ∧ + getThreadState (Z := Unit) σ.curThread σ2 = ({(.restart, σ2)}, False) ∧ + ((), σ3) ∈ ((do + Fl4Spec.NonDet.when call (Fl4SpecA64.replyFromKernel (Z := Unit) σ.curThread (0, d)) + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .running) σ2).1 ∧ + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (.Ok (), s3, a) ∧ + ∃ Q Rl, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s3 a σ3 m ∧ Sched.BodyEndA σ σ3 m := by + have hstall : (if L.mem s (scTcbA sc) != 0#u64 then smp.remote_tcb_stall st ar s a (L.mem s (scTcbA sc)) + else ok (s, a)) = ok (s, a) := J.scStallA hCs h1 hslot hcap + have htSc : obj.Sc.tcb st sc s = ok (L.mem s (scTcbA sc)) := eq_of_spec_fd (P1.obj.Sc.tcb.spec s sc) + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hslot hcov J.actW J.running J.cur + have hst : sched.set_thread_state st s (L.curW s) sched.TS_RESTART = + ok (L.wr s (stA (L.curW s)) sched.TS_RESTART) := J.restartCurEqA hext hslot hcov + have hprog : ∀ c su, schedcontext.update_consumed st ar (L.wr s (stA (L.curW s)) sched.TS_RESTART) a sc = ok (c, su) → + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + (do let s4 ← vspace.reply1 st su (L.curW s) call c; ok (core.result.Result.Ok (), s4, a)) := fun c su hu => by + rw [hcap, decodeSc_programA, if_neg Bool.false_ne_true, hlbl, sc_decode_consumed_eqA, htSc, bind_ok, hstall, + bind_ok] + simp only [uncurry_apply_pair] + rw [cur_thread_ok, bind_ok, hst, bind_ok] + unfold schedcontext.set_consumed vspace.reply1 + rw [hu, bind_ok] + cases call <;> simp [Std.bind_assoc] <;> rfl + obtain ⟨xc, σ1, c, su, σ2, σ3, s3, hkc, e1, hsp, hu, hret, hrun, e3, Q, Rl, J2⟩ := + J.consumedReplyA hA hmax htu hslot hcov hcap hext call (fun c su hu s' h2 σX => by + have hd : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (core.result.Result.Ok (), s', a) := by rw [hprog c su hu, h2, bind_ok] + obtain ⟨-, -, sysl, symL, -, userOK, j7⟩ := J.decodeFinA hA hM hPl hfm hslot hcov + (rfl : capA st s slot = capA st s slot) hext hSC (lbl := lbl) + (by rw [hcap, hlbl]; simp [DecArmS1aEnt, DecArmQH, DecArmQZ2, DecArmKQ2, DecArmKQ, DecArmKQ3, DecArmDelQ, + DecArmS1aC, capFam]) + (fun _ h => by rw [hcap] at h; cases h) (fun _ h => by rw [hcap] at h; cases h) hd σX m + exact ⟨sysl, symL, userOK, j7⟩) + refine ⟨xc, σ1, σ2, wordsFromTime c.bv, σ3, s3, hkc, e1, hsp, ?_, ?_, hrun, ?_, Q, Rl, J2⟩ + rotate_left + · obtain ⟨y0, k0, hk0, -⟩ := J.sr.scr.sc sc (by + have T0 := J.toJStage3A.toTA J.sact J.noSwitch + rw [← J.scs]; exact ⟨_, (T0.capTy slot hslot).2.2.2.1 sc sz hcap⟩) + have hk1 : σ1.kheap (scNameA m sc) = some (.schedContext y0 k0) := by + rw [e1] + simp only [stUpdA, setObjA] + rw [Function.update_of_ne (fun e => by rw [e, hkc] at hk0; cases hk0)]; exact hk0 + exact returnConsumed_stateA hk1 (x := { xc with tcbState := .restart }) + (by rw [e1]; simp only [stUpdA, setObjA, Function.update_self]) rfl hret + rotate_right + · have T0 := J.toJStage3A.toTA J.sact J.noSwitch + have hci : specPtr sc ≠ m.idleScOf m.curCore := T0.ciSc slot hslot sc sz hcap + have hname : scNameA m sc = specPtr sc := by unfold scNameA; rw [if_neg hci] + rw [← hname] + exact hret + · rw [hprog c su hu, e3, bind_ok] + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3ScNtfnA.lean b/proofs/Fl4ProofsA64/P3/Stage3ScNtfnA.lean new file mode 100644 index 000000000..13a9c0174 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ScNtfnA.lean @@ -0,0 +1,630 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 3): **SchedContext Bind / UnbindObject of a notification at the record**. + +* **`scNtfnLink_refKA`**, **`SchedValidA.refKA`**: the SC–notification link step (`scNtfnLinkStA`) is a `RefK` step, + and `SchedValidA` survives every `RefK` step. +* **`JStage3A.scNtfnLink_stepA`**: `bind_ntfn` of an unlinked SC and notification (`b = true`), or `unbind_ntfn` of a + linked pair (`b = false`), keeps the record (`of_ipcStepA`). +* **`JSyscallA.scBindNtfnA`** / **`JSyscallA.scUnbindNtfnA`**: `decode_invocation` on a SchedContext cap with the + Bind / UnbindObject label and a notification cap at a syscall entry. + +Named premises: [P] `ArchTotal`, `MaxSpi`, `ArchPlat`, `A64FrameMasks`, `hCs`; [S] `h1` (`numNodes = 1`, the remote +stall). The decode succeeds (`he`, `hn`, `hn'`); the `Err` returns are not lifted. +New module. +-/ +import Fl4ProofsA64.P3.Stage3Lift3QA +import Fl4ProofsA64.P3.Stage3IpcGlueA +import Fl4ProofsA64.P3.Stage3SignalIdleA +import Fl4ProofsA64.P3.Stage3RestartA +import Fl4ProofsA64.P3.Stage3RsSiteA +import Fl4ProofsA64.P3.Stage3FinA +import Fl4ProofsA64.P3.Stage3TcbF0A +import Fl4ProofsA64.P3.DelNtfnUnlinkA +import Fl4ProofsA64.P3.DelNtfnEntA +import Fl4ProofsA64.P3.DelScStallA +import Fl4ProofsA64.P3.DelSinvLoopA +import Fl4ProofsA64.P3.ScDecodeOpA +import Fl4ProofsA64.P3.ScNtfnLinkA +import Fl4ProofsA64.P3.IpcInvokeDispatchA +import Fl4ProofsA64.P3.ScIdleA +import Fl4ProofsA64.P3.Stage3TcbBodyA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-- The SC–notification link step is a `RefK` step. -/ +theorem scNtfnLink_refKA {σ : State Unit} {p q : ObjRef} {xs : SchedContext} {ns : Nat} {xn : Notification} + (hs : σ.kheap p = some (.schedContext xs ns)) (hn : σ.kheap q = some (.notification xn)) (b : Bool) : + RefK σ (scNtfnLinkStA σ p q xs ns xn b) := by + have hpq : p ≠ q := fun e => by rw [e] at hs; rw [hs] at hn; cases hn + refine ⟨rfl, fun r => ?_, fun hr => ?_⟩ + swap + · have h1 := RdyA.updNs hr q (.notification { xn with ntfnSc := if b then some p else none }) + (fun _ _ e => by cases e) + have hk' : (updKSc σ q (.notification { xn with ntfnSc := if b then some p else none })).kheap p = + some (.schedContext xs ns) := by + simp only [updKSc, Function.update_of_ne hpq]; exact hs + exact RdyA.updSc h1 hk' rfl (fun h => h) + unfold scNtfnLinkStA + simp only [updKSc_kheap, Function.update_apply] + by_cases h1 : r = p + · subst h1 + rw [if_pos rfl, hs] + exact .sc _ _ _ ⟨rfl, rfl, rfl, id⟩ + · rw [if_neg h1] + by_cases h2 : r = q + · subst h2 + rw [if_pos rfl, hn] + exact .ntfn _ _ + · rw [if_neg h2] + exact .same _ + +/-- `SchedValidA` across a `RefK` step (states, SCs of TCBs and SC budgets kept). -/ +theorem SchedValidA.refKA {σ σ' : State Unit} (h : SchedValidA σ) (R : RefK σ σ') : SchedValidA σ' := by + have e := R.1 + have hq : σ'.readyQueues = σ.readyQueues := by rw [e] + have hr : σ'.releaseQueue = σ.releaseQueue := by rw [e] + have ha : σ'.schedulerAction = σ.schedulerAction := by rw [e] + have hc : σ'.curThread = σ.curThread := by rw [e] + have hi : σ'.idleThread = σ.idleThread := by rw [e] + have act : ∀ p, ScActiveAtA σ p → ScActiveAtA σ' p := fun p ⟨y, k, hy, hact⟩ => by + obtain ⟨y', hy', E⟩ := (R.2.1 p).scF hy + refine ⟨y', k, hy', ?_⟩ + simp only [scActive] at hact ⊢ + rw [E.2.2.1] + exact hact + have hf : ∀ t x, σ.kheap t = some (.tcb x) → ∃ x', σ'.kheap t = some (.tcb x') ∧ + x'.tcbSchedContext = x.tcbSchedContext := fun t x hx => by + obtain ⟨y, hy, E⟩ := (R.2.1 t).tcbF hx + exact ⟨y, hy, E.2.1⟩ + refine ⟨fun d p t hm => ?_, fun e1 e2 => ?_, fun t x' sc e1 e2 e3 => ?_, fun t ht => ?_⟩ + · rw [hq] at hm + obtain ⟨⟨x, sc, e, es, a⟩, n⟩ := h.rq d p t hm + obtain ⟨x', e', es'⟩ := hf t x e + exact ⟨⟨x', sc, e', es'.trans es, act _ a⟩, by rw [hr]; exact n⟩ + · rw [ha] at e1 + rw [hc, hi] at e2 + obtain ⟨⟨x, sc, e, es, a⟩, n⟩ := h.act e1 e2 + obtain ⟨x', e', es'⟩ := hf _ x e + rw [hc, hr] + exact ⟨⟨x', sc, e', es'.trans es, act _ a⟩, n⟩ + · obtain ⟨x, e, E⟩ := (R.2.1 t).tcbB e1 + exact act _ (h.blk t x sc e (E.1 ▸ e2) (E.2.1 ▸ e3)) + · rw [hr] at ht + obtain ⟨x, sc, e, es, a⟩ := h.rls t ht + obtain ⟨x', e', es'⟩ := hf t x e + exact ⟨x', sc, e', es'.trans es, act _ a⟩ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s : S} {a : A} {σ : State Unit} {m : McState} + +/-- The FL4 program of the link step. -/ +def scLinkProgA (st : machine.Store S) (s : S) (sc p : U64) (b : Bool) : Result S := + if b then schedcontext.bind_ntfn st s sc p else schedcontext.unbind_ntfn st s sc + +/-- The spec program of the link step. -/ +def scLinkSpecA (sc p : ObjRef) (b : Bool) : SMonad Unit Unit := + if b then schedContextBindNtfn sc p else schedContextUnbindNtfn sc + +set_option maxHeartbeats 4000000 in +/-- **The SC–notification link step at the record** (module docstring). -/ +theorem JStage3A.scNtfnLink_stepA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (sact : SActA σ) (nsw : ∀ u, σ.schedulerAction ≠ .switchThread u) + {sc p : U64} (hc : sc ∈ Z.C) (hpN : p ∈ N) (b : Bool) (hni : b = true → NotIdleScA m (scNameA m sc)) + (hw : if b then L.mem s (nScSc p) = 0#u64 ∧ L.mem s (scWA sc 40#u64) = 0#u64 + else L.mem s (scWA sc 40#u64) = p) : + ∃ s' σ', scLinkProgA st s sc p b = ok s' ∧ + scLinkSpecA (scNameA m sc) (specPtr p) b σ = ({((), σ')}, False) ∧ + (∃ xs ns xn, σ.kheap (scNameA m sc) = some (.schedContext xs ns) ∧ σ.kheap (specPtr p) = some (.notification xn) ∧ + σ' = scNtfnLinkStA σ (scNameA m sc) (specPtr p) xs ns xn b) ∧ + BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s' ∧ + (SysL st H Z.Cs OS ia ie s' → SymL st H Z.Cs s' → Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s' → + J7A st (J7R2A st ar nA) Z H OS s' σ' m → + ∃ Q Rl, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s' a σ' m) := by + have T := J.toTA sact nsw + have hP := J.iplaced + have hcH : sc ∈ H.scs := by rw [J.scs]; exact hc + have hpH : p ∈ H.ntfns := J.utIpc.ntfn p hpN + have sc0 : sc ≠ 0#u64 := J.scNZ sc hc + have p0 : p ≠ 0#u64 := J.sr.place.nz p _ (J.ipcPlace.notification p hpN) + have hna := hP.n.ntfn p hpN + have eS : nScSc p = Notif.nScA p := rfl + have aN : Aligned (nScSc p) := by + rw [eS, Notif.nScA_eq]; exact Notif.nfield_aligned hna (by simp) (by simp) + have vN : (nScSc p).val = p.val + 32 := by + rw [eS, Notif.nScA_eq, Notif.nfield_val hna (by simp)]; simp + have hout : ¬ SReadA st Z m s σ (nScSc p) := T.nScSc_offReadA hpN + obtain ⟨xs, ns, hks, hxs⟩ := J.sr.scr.sc sc hc + obtain ⟨xn, hkn, hrel⟩ := J.ntfn.ntfn p hpN + have h40 := hxs.hdr (K := 40#u64) (by simp) (by simp) + have obj40 : ScObjA sc ns (scWA sc 40#u64) := hxs.in_obj h40.2 + have mw40 : MWrA (st := st) Z m σ s (scWA sc 40#u64) := mwr_scObj hc hks obj40 + have f40 : ScFootA Z.C m σ (scWA sc 40#u64) := ⟨sc, hc, xs, ns, hks, obj40⟩ + have e40 : scNtfnA sc = scWA sc 40#u64 := by unfold scNtfnA scWA layout.sc.NTFN; rfl + have hpq : specPtr p ≠ scNameA m sc := by intro e; rw [e, hks] at hkn; cases hkn + set nv : U64 := if b then sc else 0#u64 with hnv + set sv : U64 := if b then p else 0#u64 with hsv + set s' := L.wr (L.wr s (nScSc p) nv) (scWA sc 40#u64) sv with hs' + set σ' := scNtfnLinkStA σ (scNameA m sc) (specPtr p) xs ns xn b with hσ' + -- the program, the spec step, the scheduler relation and `BlkOK` + have hsc0 : L.mem s (nScSc p) ≠ 0#u64 → L.mem s (nScSc p) = sc := by + cases b + · intro h0 + have hw' : L.mem s (scNtfnA sc) = p := e40 ▸ hw + have := T.blkA.2.2.1.2.2.bs.sn sc hcH sc0 (by rw [hw']; exact p0) + rw [hw'] at this + exact this + · intro h0; exact absurd hw.1 h0 + have prog : scLinkProgA st s sc p b = ok s' := by + unfold scLinkProgA + cases b + · simp only [Bool.false_eq_true, ↓reduceIte] + unfold schedcontext.unbind_ntfn + rw [eq_of_spec_fd (P1.obj.Sc.is_null.spec sc), bind_ok, decide_eq_false sc0, if_neg Bool.false_ne_true, + eq_of_spec_fd (obj.Sc.ntfn.valA (L := L) s sc), bind_ok, hw, show (p != 0#u64) = true from bne_iff_ne.2 p0, + if_pos rfl, eq_of_spec_fd (obj.Ntfn.set_sc.wr (st := st) s p 0#u64), bind_ok, + eq_of_spec_fd (obj.Sc.set_ntfn.wr (st := st) _ sc 0#u64), e40] + rfl + · simp only [↓reduceIte] + unfold schedcontext.bind_ntfn + rw [eq_of_spec_fd (obj.Ntfn.set_sc.wr (st := st) s p sc), bind_ok, + eq_of_spec_fd (obj.Sc.set_ntfn.wr (st := st) _ sc p), e40] + rfl + have spec : scLinkSpecA (scNameA m sc) (specPtr p) b σ = ({((), σ')}, False) := by + unfold scLinkSpecA + cases b + · have hn : xs.scNtfn = some (specPtr p) := by rw [hxs.ntfn, hw, optRefS_ne p0] + exact schedContextUnbindNtfn_someA hks hkn hn + · exact schedContextBindNtfn_eqA hks hkn + have SR : SchedSRA st Z m κ s' σ' := by + cases b + · obtain ⟨s2, e2, σ2, hsp, sr, -, -⟩ := exists_of_specA (sc_unbind_ntfn_someA J.sr hc sc0 p0 aN hout hw hkn) + have e2' : s2 = s' := by + have := prog + unfold scLinkProgA at this + simp only [Bool.false_eq_true, ↓reduceIte] at this + rw [e2] at this + exact ok_inj_ff this + have eσ : σ2 = σ' := by + have := hsp.symm.trans spec + have h1 : ((), σ2) ∈ ({((), σ')} : Set (Unit × State Unit)) := by rw [← (Prod.mk.inj this).1]; exact rfl + exact (Prod.mk.inj h1).2 + rw [← e2', ← eσ] + exact sr + · obtain ⟨s2, e2, σ2, hsp, sr, -, -⟩ := exists_of_specA (sc_bind_ntfnA J.sr hc sc0 p0 aN hout (hni rfl) hkn) + have e2' : s2 = s' := by + have := prog + unfold scLinkProgA at this + simp only [↓reduceIte] at this + rw [e2] at this + exact ok_inj_ff this + have eσ : σ2 = σ' := by + have := hsp.symm.trans spec + have h1 : ((), σ2) ∈ ({((), σ')} : Set (Unit × State Unit)) := by rw [← (Prod.mk.inj this).1]; exact rfl + exact (Prod.mk.inj h1).2 + rw [← e2', ← eσ] + exact sr + have blk' : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s' := by + have hb := BXI.blk J.sys.ent.bxi + have h := prog + unfold scLinkProgA at h + cases b + · simp only [Bool.false_eq_true, ↓reduceIte] at h + have q := schedcontext.unbind_ntfn.blk (st := st) s sc hb (fun _ => hcH) + rw [h, WP.spec_ok] at q + exact q.1 + · simp only [↓reduceIte] at h + have q := schedcontext.bind_ntfn.blk (st := st) s sc p hb hcH sc0 hpH hw.1 (e40 ▸ hw.2) + rw [h, WP.spec_ok] at q + exact q + refine ⟨s', σ', prog, spec, ⟨xs, ns, xn, hks, hkn, rfl⟩, blk', fun sysl symL userOK j7 => ?_⟩ + -- the footprint + have w12 : WritesIn st s s' (fun c => c = nScSc p ∨ c = scWA sc 40#u64) := + ((writesIn_wr aN).trans (writesIn_wr h40.1)).mono fun _ h => h + have off : ∀ c : U64, Aligned c → c ≠ nScSc p → c ≠ scWA sc 40#u64 → L.mem s' c = L.mem s c := + fun c hc' n1 n2 => w12.mem_eq hc' fun h => h.elim n1 n2 + have thOff : ∀ q k, Z.th q = some k → (scWA sc 40#u64).val < q.val ∨ q.val + k.size ≤ (scWA sc 40#u64).val := + fun q k hq => J.sr.scp.th_out f40 hq + have tw : ∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → L.mem s' (wa u K) = L.mem s (wa u K) := by + intro u hu K k1 k2 + have hua : TcbAddr u := J.sr.core.tcbs u hu + have hv := Notif.tfield_val hua k1 + refine off _ (field_aligned hua k1 k2) (fun e => ?_) (fun e => ?_) + · have := congrArg UScalar.val e + rw [hv, vN] at this + have := hP.n.disj p hpN u hu + omega + · have h := thOff u .tcb ((J.sr.thT u).2 hu) + rw [← e, hv] at h + have sz : ObjKindA.size .tcb = 4096 := rfl + omega + have bnd : L.mem s' (Notif.nBoundA p) = L.mem s (Notif.nBoundA p) := by + have f := Notif.foot_bound hP.n hpN + refine off _ f.1 (fun e => ?_) (fun e => ?_) + · have := congrArg UScalar.val e + rw [vN, Notif.nBoundA_eq, Notif.nfield_val hna (by simp)] at this + simp at this + · have h := thOff p _ (J.ipcPlace.notification p hpN) + have v := Notif.nfield_val hna (K := 24#u64) (by simp) + rw [← e, Notif.nBoundA_eq, v] at h + simp only [ObjKindA.size] at h + simp at h + have offN : ∀ c, Notif.NFoot N Z.T c → c ≠ scWA sc 40#u64 := by + intro c hb e + rcases hb with ⟨q, hq, h1, h2⟩ | ⟨t, ht, h⟩ + · have := thOff q _ (J.ipcPlace.notification q hq) + rw [← e] at this + simp only [ObjKindA.size] at this + omega + · have hta := hP.n.tcb t ht + have v : ∀ K : U64, K.val < 4096 → (wa t K).val = t.val + K.val := fun K hK => Notif.tfield_val hta hK + have rng : t.val ≤ c.val ∧ c.val < t.val + 4096 := by + rcases h with h | h | h | h | h | h <;> rw [h] + · rw [stA_eq, v _ (by simp)]; simp + · rw [Notif.tBlockA_eq, v _ (by simp)]; simp + · rw [Notif.badgeRegA_eq, v _ (by simp)]; simp + · rw [Notif.prioA_eq', v _ (by simp)]; simp + · rw [Notif.nextEp_eq, v _ (by simp)]; simp + · rw [Notif.prevEp_eq, v _ (by simp)]; simp + have := thOff t .tcb ((J.sr.thT t).2 ht) + rw [← e] at this + have sz : ObjKindA.size .tcb = 4096 := rfl + omega + -- the spec state + set x' : Notification := { xn with ntfnSc := if b then some (scNameA m sc) else none } with hx' + set σm := updKSc σ (specPtr p) (.notification x') with hσm + have eσ2 : σ' = updKSc σm (scNameA m sc) (.schedContext { xs with scNtfn := if b then some (specPtr p) else none } ns) := + rfl + have ksm : σm.kheap (scNameA m sc) = some (.schedContext xs ns) := by + rw [hσm, updKSc_kheap, Function.update_of_ne hpq.symm]; exact hks + have key : ∀ q, σ'.kheap q = + if q = scNameA m sc then some (.schedContext { xs with scNtfn := if b then some (specPtr p) else none } ns) + else if q = specPtr p then some (.notification x') else σ.kheap q := by + intro q + simp only [hσ', scNtfnLinkStA, updKSc_kheap, Function.update_apply, hx'] + have keepO : ∀ q o, σ.kheap q = some o → (∀ y k, o ≠ .schedContext y k) → (∀ z, o ≠ .notification z) → + σ'.kheap q = some o := fun q o h n1 n2 => by + rw [key, if_neg (fun e => by rw [e, hks] at h; exact n1 xs ns (Option.some.inj h).symm), + if_neg (fun e => by rw [e, hkn] at h; exact n2 xn (Option.some.inj h).symm)] + exact h + have backO : ∀ q o, σ'.kheap q = some o → (∀ y k, o ≠ .schedContext y k) → (∀ z, o ≠ .notification z) → + σ.kheap q = some o := fun q o h n1 n2 => by + rw [key] at h + split at h + · exact absurd (Option.some.inj h).symm (n1 _ _) + · split at h + · exact absurd (Option.some.inj h).symm (n2 _) + · exact h + -- the relations + have R1 : NtfnStateRel N Z.T (L.mem s') σm := by + refine NtfnStateRel.relinkA J.ntfn hP.n hpN (x' := x') hkn rfl (fun c hb f _ ne => off c hb (fun e => ne (e.trans eS)) + (offN c f)) (by rw [bnd]; exact hrel.bound) (by rw [bnd]; exact hrel.bound_mem) ?_ + have hm : L.mem s' (Notif.nScA p) = nv := by + rw [← eS] + have : L.mem s' (nScSc p) = L.mem (L.wr s (nScSc p) nv) (nScSc p) := by + refine (writesIn_wr (s := L.wr s (nScSc p) nv) (v := sv) h40.1).mem_eq aN fun e => ?_ + exact hout (e ▸ Or.inr (Or.inr (Or.inr (Or.inr f40)))) + rw [this, L.mem_wr_same aN] + rw [hm, hx'] + cases b + · simp [hnv, Notif.optRef_zero] + · simp only [↓reduceIte, hnv] + rw [Notif.optRef_ne sc0, scNameA_eq_of_notIdle (hni rfl)] + have ntfn : NtfnStateRel N Z.T (L.mem s') σ' := by + rw [eσ2] + exact R1.frame hP.n (fun _ _ _ => rfl) (ntfnKView_updA ksm (fun _ e => by cases e) (fun _ e => by cases e)) + have ep : EpStateRel Z.T E Rp Bf (L.mem s') σ' := by + have k1 : Endp.KView σ σm := endpKView_updA hkn (fun _ e => by cases e) (fun _ e => by cases e) + (fun _ e => by cases e) + have k2 : Endp.KView σm σ' := by + rw [eσ2] + exact endpKView_updA ksm (fun _ e => by cases e) (fun _ e => by cases e) (fun _ e => by cases e) + have k : Endp.KView σ σ' := by + refine ⟨fun q x h => k2.1 q x (k1.1 q x h), fun q x h => ?_, fun q x h => ?_, k2.2.2.2.trans k1.2.2.2⟩ + · obtain ⟨y, hy, e⟩ := k1.2.1 q x h + obtain ⟨z, hz, f⟩ := k2.2.1 q y hy + exact ⟨z, hz, f.trans e⟩ + · obtain ⟨y, hy, e1, e2, e3, e4, e5⟩ := k1.2.2.1 q x h + obtain ⟨z, hz, f1, f2, f3, f4, f5⟩ := k2.2.2.1 q y hy + exact ⟨z, hz, f1.trans e1, f2.trans e2, f3.trans e3, f4.trans e4, f5.trans e5⟩ + refine J.ep.frameX hP.e (fun c hb f => off c hb (fun e => ?_) (fun e => ?_)) k + · exact nw_not_efoot hP hpN (a := c) (by rw [e, vN]; omega) (by rw [e, vN]; omega) f.1 + · exact endpFootOffWrite J.sr.toSchedSRN J.ipcPlace f.1 (e ▸ mw40) + have reply : ReplyStackRelA (L.mem s') Rp Z.C σ' (scNameA m) stack := by + refine J.reply.frame (fun r hr => ?_) (fun c _ z k h => ?_) + (fun r _ x h => ⟨x, backO _ _ h (fun _ _ e => by cases e) (fun _ e => by cases e), rfl⟩) + · have dis := hP.nr p hpN r hr + have hro := thOff r _ (J.ipcPlace.reply r hr) + simp only [ObjKindA.size] at hro + have one : ∀ O : U64, O.val = 8 ∨ O.val = 16 → L.mem s' (wa r O) = L.mem s (wa r O) := by + intro O hO + have hv : (wa r O).val = r.val + O.val := by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by have := hP.e.rep r hr; omega) + refine off _ (by have := hP.e.rep r hr; unfold Aligned; rw [hv]; omega) (fun e => ?_) (fun e => ?_) + · have := congrArg UScalar.val e; rw [hv, vN] at this; omega + · rw [e] at hv; omega + exact ⟨one _ (Or.inr (by simp [layout.reply.NEXT])), one _ (Or.inl (by simp [layout.reply.PREV]))⟩ + · rw [key] at * + split + · rename_i e + rw [e, hks] at h + cases h + exact ⟨_, _, rfl, rfl⟩ + · split + · rename_i _ e + rw [e, hkn] at h + cases h + · exact ⟨z, k, h, rfl⟩ + have K : IpcKeepA σ σ' := by + refine ⟨fun q z h => ⟨z, keepO q _ h (fun _ _ e => by cases e) (fun _ e => by cases e), rfl, rfl, rfl, rfl, + rfl, rfl⟩, fun q z k h => ?_, fun q z k h => ?_⟩ + · rw [key] + split + · rename_i e + rw [e, hks] at h + cases h + exact ⟨_, _, rfl, rfl⟩ + · split + · rename_i _ e + rw [e, hkn] at h + cases h + · exact ⟨z, k, h, rfl⟩ + · rw [key] at h + split at h + · rename_i e + cases h + exact ⟨xs, ns, e ▸ hks, rfl⟩ + · split at h + · cases h + · exact ⟨z, k, h, rfl⟩ + have RK : RefK σ σ' := scNtfnLink_refKA hks hkn b + have LI := LoopInvA.refK ⟨J.sinv, J.sidle, sact⟩ RK + have rv : RunVcpuInv st Z.T s' := fun u hu h0 => by + have e1 : L.mem s' (runVcpuA u) = L.mem s (runVcpuA u) := by + unfold runVcpuA + exact tw u hu 3672#u64 (by simp) (by simp) + have e2 : L.mem s' (Fl4ProofsA64.P1.stA u) = L.mem s (Fl4ProofsA64.P1.stA u) := by + rw [Fl4ProofsA64.P1.stA_eq] + exact tw u hu 3328#u64 (by simp) (by simp) + rw [e2]; exact J.runVcpu u hu (e1 ▸ h0) + have blkQ : BlkQueuedA Z.T E N (L.mem s') σ' := by + have tb : ∀ u ∈ Z.T, L.mem s' (tBlockA u) = L.mem s (tBlockA u) := fun u hu => by + rw [Notif.tBlockA_eq]; exact tw u hu 3336#u64 (by simp) (by simp) + refine ⟨fun u hu w h e hb => ?_, fun u hu w h q hb => ?_⟩ + · have hz := backO _ _ h (fun _ _ e => by cases e) (fun _ e => by cases e) + obtain ⟨c, o, ho, hm⟩ := J.blkQ.ep u hu w hz e hb + exact ⟨by rw [tb u hu]; exact c, o, keepO _ _ ho (fun _ _ e => by cases e) (fun _ e => by cases e), hm⟩ + · have hz := backO _ _ h (fun _ _ e => by cases e) (fun _ e => by cases e) + obtain ⟨c, o, l, ho, hl, hm⟩ := J.blkQ.ntfn u hu w hz q hb + refine ⟨by rw [tb u hu]; exact c, ?_⟩ + by_cases e : q = specPtr p + · subst e + rw [hkn] at ho + cases ho + exact ⟨x', l, by rw [key, if_neg hpq, if_pos rfl], hl, hm⟩ + · have e' : q ≠ scNameA m sc := fun e2 => by rw [e2, hks] at ho; cases ho + exact ⟨o, l, by rw [key, if_neg e', if_neg e]; exact ho, hl, hm⟩ + have F : SchedFrameA σ σ' := by + have f1 : SchedFrameA σ σm := SchedFrameA.upd_simple hkn (Or.inr (Or.inl ⟨_, _, rfl, rfl⟩)) + (fun _ _ e => by cases e) + have f2 : SchedFrameA σm σ' := by rw [eσ2]; exact SchedFrameA.upd_sc ksm + exact PreO.trans f1 f2 + have wr : WritesIn st s s' (SchedStepNWA (st := st) N E Rp Z m σ s) := w12.mono fun c h => by + rcases h with e | e + · rw [e]; exact nScSc_stepNA hpN hna.fits + · rw [e]; exact Or.inl (Or.inl mw40) + refine ⟨Z.Q, Z.R, ?_⟩ + have Qk : QKeepA σ σ' := QKeepA.ofBwd fun q o h => by + rw [key] at h + split at h + · rename_i e + cases h + exact ⟨.schedContext xs ns, by rw [e]; exact hks, QObjA.ofSc fun hr => ⟨hr, id⟩⟩ + · split at h + · rename_i e + cases h + exact ⟨.notification xn, by rw [e]; exact hkn, QObjA.ofNtfn (List.Sublist.refl _)⟩ + · exact ⟨o, h, QObjA.refl o⟩ + exact J.of_ipcStepA (Q := Z.Q) (Rl := Z.R) (curOf := Z.curOf) (curScOf := Z.curScOf) wr F.1 F.2.1 Qk F.2.2 + (fun _ _ => rfl) (BlkOK.bxi blk') sysl symL userOK j7 SR ep ntfn reply K + (fun u hu O h h8 => tw u hu O (by omega) h8) LI.sinv LI.sidle rv blkQ (J.schedValid.refKA RK) + +/-- The entry facts shared by the two SC arms: restart, the SC and notification, the quiet stall, the two link words +moved across the caller's Restart. -/ +theorem JSyscallA.scLinkEntryA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + {slot sc sz p badge : U64} {send recv : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .SchedContext sc sz) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) + (he : schedcontext.extra_cap st s extra 0#usize = ok (some (.Notification p badge send recv))) : + ∃ xc, σ.kheap σ.curThread = some (.tcb xc) ∧ + sched.set_thread_state st s (L.curW s) sched.TS_RESTART = ok (L.wr s (stA (L.curW s)) sched.TS_RESTART) ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), stUpdA σ σ.curThread xc .restart)}, False) ∧ + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) + (L.wr s (stA (L.curW s)) sched.TS_RESTART) a (stUpdA σ σ.curThread xc .restart) m ∧ + SActA (stUpdA σ σ.curThread xc .restart) ∧ + (∀ u, (stUpdA σ σ.curThread xc .restart).schedulerAction ≠ .switchThread u) ∧ + sc ∈ Z.C ∧ p ∈ N ∧ scNameA m sc = specPtr sc ∧ NotIdleScA m (scNameA m sc) ∧ + obj.Sc.tcb st sc s = ok (L.mem s (scTcbA sc)) ∧ + (if L.mem s (scTcbA sc) != 0#u64 then smp.remote_tcb_stall st ar s a (L.mem s (scTcbA sc)) else ok (s, a)) = + ok (s, a) ∧ + L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART) (nScSc p) = L.mem s (nScSc p) ∧ + L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART) (scWA sc 40#u64) = L.mem s (scWA sc 40#u64) := by + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hslot hcov J.actW J.running J.cur + have hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM := J.toJStage3EntryA.curNotVmA + obtain ⟨xc, hkc, hst, hsp, J1, -, -, -⟩ := J.toJStage3A.of_restartCurA RS.pl.choose_spec.choose_spec.choose_spec.choose_spec + RS.sym RS.run hnvm RS.cur RS.vs RS.vc + have T0 := J.toJStage3A.toTA J.sact J.noSwitch + have hH : H sc = some (.sc sz.val) := (T0.capTy slot hslot).2.2.2.1 sc sz hcap + have hcH : sc ∈ H.scs := ⟨_, hH⟩ + have hc : sc ∈ Z.C := by rw [← J.scs]; exact hcH + have hpN : p ∈ N := by + have q := schedcontext.extra_cap.slot (J.decPreA hslot hext) (WritesIn.refl s _) + rw [he, WP.spec_ok] at q + obtain ⟨x, hx, hx0, hxc, -⟩ := q _ rfl + exact J.ipcHeapCov.ntfn p ((T0.capTy x (hext x hx hx0)).2.1 p badge send recv hxc) + have hci : specPtr sc ≠ m.idleScOf m.curCore := T0.ciSc slot hslot sc sz hcap + have hname : scNameA m sc = specPtr sc := by unfold scNameA; rw [if_neg hci] + have hni : NotIdleScA m (scNameA m sc) := by + rw [hname] + refine ⟨J.sr.sch.scOff sc hc hci, fun c hcN => ?_⟩ + have hcc : m.curCore = 0 := Nat.lt_one_iff.mp (lt_of_lt_of_eq (T0.curCore_lt hCs) h1) + have hc1 : c = 0 := Nat.lt_one_iff.mp (lt_of_lt_of_eq hcN h1) + have e : c = m.curCore := hc1.trans hcc.symm + rw [e] + exact hci + have htSc : obj.Sc.tcb st sc s = ok (L.mem s (scTcbA sc)) := eq_of_spec_fd (P1.obj.Sc.tcb.spec s sc) + have hstall : (if L.mem s (scTcbA sc) != 0#u64 then smp.remote_tcb_stall st ar s a (L.mem s (scTcbA sc)) + else ok (s, a)) = ok (s, a) := by + by_cases h0 : L.mem s (scTcbA sc) = 0#u64 + · rw [h0]; rfl + · have ht : L.mem s (scTcbA sc) ∈ Z.T := by rw [← J.tcbs]; exact T0.blkA.2.2.1.2.2.yt.1 sc hcH h0 + rw [if_pos (bne_iff_ne.2 h0)] + exact T0.stall_singleA hCs h1 ht + have hct : TcbAddr (L.curW s) := J.sr.core.tcbs _ J.sr.curT + have aS : Aligned (stA (L.curW s)) := by rw [stA_eq]; exact field_aligned hct (by simp) (by simp) + have vS : (stA (L.curW s)).val = (L.curW s).val + 3328 := by + rw [stA_eq, Notif.tfield_val hct (by decide : (3328#u64 : U64).val < 4096)]; rfl + have hna := J.iplaced.n.ntfn p hpN + have aN : Aligned (nScSc p) := by + rw [show nScSc p = Notif.nScA p from rfl, Notif.nScA_eq]; exact Notif.nfield_aligned hna (by simp) (by simp) + have vN : (nScSc p).val = p.val + 32 := by + rw [show nScSc p = Notif.nScA p from rfl, Notif.nScA_eq, Notif.nfield_val hna (by simp)]; simp + obtain ⟨xs, ns, hks, hxs⟩ := J.sr.scr.sc sc hc + have h40 := hxs.hdr (K := 40#u64) (by simp) (by simp) + have f40 : ScFootA Z.C m σ (scWA sc 40#u64) := ⟨sc, hc, xs, ns, hks, hxs.in_obj h40.2⟩ + have sact1 : SActA (stUpdA σ σ.curThread xc .restart) := + ⟨fun u h => J.sact.f1 u h, ⟨{ xc with tcbState := .restart }, by simp only [stUpdA, setObjA, Function.update_self], + Or.inr (Or.inl rfl)⟩⟩ + refine ⟨xc, hkc, hst, hsp, J1, sact1, fun u h => J.noSwitch u h, hc, hpN, hname, hni, htSc, hstall, ?_, ?_⟩ + · refine mem_wr_ne' aS aN fun e => ?_ + have := congrArg UScalar.val e + rw [vS, vN] at this + have := J.iplaced.n.disj p hpN (L.curW s) J.sr.curT + have a1 : (L.curW s).val % 4096 = 0 := hct.align + omega + · refine mem_wr_ne' aS h40.1 fun e => ?_ + have h := J.sr.scp.th_out f40 ((J.sr.thT _).2 J.sr.curT) + rw [← e, vS] at h + have sz : ObjKindA.size .tcb = 4096 := rfl + omega + +set_option maxHeartbeats 2000000 in +/-- **SchedContext Bind of a notification at a syscall entry** (module docstring). -/ +theorem JSyscallA.scBindNtfnA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + {slot lbl len buf sc sz p badge : U64} {blk call don send recv : Bool} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .SchedContext sc sz) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hlbl : lbl = fl4_abi.label.SchedContextBind) + (he : schedcontext.extra_cap st s extra 0#usize = ok (some (.Notification p badge send recv))) + (hn : obj.Sc.ntfn st sc s = ok 0#u64) (hn' : obj.Ntfn.sc st p s = ok 0#u64) : + ∃ xc σ1 σ2 s2, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + invokeSchedContext (Z := Unit) (.invokeSchedContextBind (specPtr sc) + (.notificationCap (specPtr p) badge.bv (rightsOfA send recv false false))) σ1 = ({([], σ2)}, False) ∧ + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (.Ok (), s2, a) ∧ + ∃ Q Rl, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s2 a σ2 m ∧ Sched.BodyEndA σ σ2 m := by + obtain ⟨xc, hkc, hst, hsp, J1, sact1, nsw1, hc, hpN, hname, hni, htSc, hstall, eN, e40⟩ := + J.scLinkEntryA hCs h1 hslot hcov hcap hext he + have w1 : L.mem s (nScSc p) = 0#u64 := by + have h := eq_of_spec_fd (ntfn_sc_specSc (L := L) s p) + rw [hn'] at h + exact (ok_inj_ff h).symm + have w2 : L.mem s (scWA sc 40#u64) = 0#u64 := by + have h := eq_of_spec_fd (obj.Sc.ntfn.valA (L := L) s sc) + rw [hn] at h + exact (ok_inj_ff h).symm + obtain ⟨s', σ', prog, spec, EE, -, K⟩ := J1.scNtfnLink_stepA sact1 nsw1 hc hpN true (fun _ => hni) + (by simp only [↓reduceIte]; exact ⟨eN.trans w1, e40.trans w2⟩) + unfold scLinkProgA at prog + unfold scLinkSpecA at spec + simp only [↓reduceIte] at prog spec + rw [hname] at spec + have hdec : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (.Ok (), s', a) := by + rw [hcap, decodeSc_programA, if_neg Bool.false_ne_true, hlbl, + sc_decode_bind_ntfn_eqA htSc hstall (cur_thread_ok s) he hn hn', hst, bind_ok, prog, bind_ok] + obtain ⟨-, -, sysl, symL, -, userOK, j7⟩ := J.decodeFinA hA hM hPl hfm hslot hcov + (rfl : capA st s slot = capA st s slot) hext hSC (lbl := lbl) + (by rw [hcap, hlbl]; simp [DecArmS1aEnt, DecArmQH, DecArmQZ2, DecArmKQ2, DecArmKQ, DecArmKQ3, DecArmDelQ, DecArmS1aC, capFam]) + (fun _ h => by rw [hcap] at h; cases h) (fun _ h => by rw [hcap] at h; cases h) hdec σ' m + obtain ⟨Q, Rl, J2⟩ := K sysl symL userOK j7 + obtain ⟨xs0, ns0, xn0, hks0, hkn0, hσ'⟩ := EE + refine ⟨xc, _, σ', s', hkc, rfl, hsp, ?_, hdec, Q, Rl, J2, (by rw [hσ']; exact Sched.BodyEndA.ofScNtfn J hkc hks0 hkn0)⟩ + change (do schedContextBindNtfn (Z := Unit) (specPtr sc) (specPtr p); pure ([] : List Data)) _ = _ + rw [nd_bind_oneSc spec] + rfl + +set_option maxHeartbeats 2000000 in +/-- **SchedContext UnbindObject of its notification at a syscall entry** (module docstring). -/ +theorem JSyscallA.scUnbindNtfnA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + {slot lbl len buf sc sz p badge : U64} {blk call don send recv : Bool} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .SchedContext sc sz) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hlbl : lbl = fl4_abi.label.SchedContextUnbindObject) + (he : schedcontext.extra_cap st s extra 0#usize = ok (some (.Notification p badge send recv))) + (hn : obj.Sc.ntfn st sc s = ok p) : + ∃ xc σ1 σ2 s2, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + invokeSchedContext (Z := Unit) (.invokeSchedContextUnbindObject (specPtr sc) + (.notificationCap (specPtr p) badge.bv (rightsOfA send recv false false))) σ1 = ({([], σ2)}, False) ∧ + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (.Ok (), s2, a) ∧ + ∃ Q Rl, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s2 a σ2 m ∧ Sched.BodyEndA σ σ2 m := by + obtain ⟨xc, hkc, hst, hsp, J1, sact1, nsw1, hc, hpN, hname, hni, htSc, hstall, -, e40⟩ := + J.scLinkEntryA hCs h1 hslot hcov hcap hext he + have w2 : L.mem s (scWA sc 40#u64) = p := by + have h := eq_of_spec_fd (obj.Sc.ntfn.valA (L := L) s sc) + rw [hn] at h + exact (ok_inj_ff h).symm + obtain ⟨s', σ', prog, spec, EE, -, K⟩ := J1.scNtfnLink_stepA sact1 nsw1 hc hpN false (fun h => by cases h) + (by simp only [Bool.false_eq_true, ↓reduceIte]; exact e40.trans w2) + unfold scLinkProgA at prog + unfold scLinkSpecA at spec + simp only [Bool.false_eq_true, ↓reduceIte] at prog spec + rw [hname] at spec + have hdec : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (.Ok (), s', a) := by + rw [hcap, decodeSc_programA, if_neg Bool.false_ne_true, hlbl, + sc_decode_unbind_object_ntfn_eqA htSc hstall (cur_thread_ok s) he hn, hst, bind_ok, prog, bind_ok] + obtain ⟨-, -, sysl, symL, -, userOK, j7⟩ := J.decodeFinA hA hM hPl hfm hslot hcov + (rfl : capA st s slot = capA st s slot) hext hSC (lbl := lbl) + (by rw [hcap, hlbl]; simp [DecArmS1aEnt, DecArmQH, DecArmQZ2, DecArmKQ2, DecArmKQ, DecArmKQ3, DecArmDelQ, DecArmS1aC, capFam]) + (fun _ h => by rw [hcap] at h; cases h) (fun _ h => by rw [hcap] at h; cases h) hdec σ' m + obtain ⟨Q, Rl, J2⟩ := K sysl symL userOK j7 + obtain ⟨xs0, ns0, xn0, hks0, hkn0, hσ'⟩ := EE + refine ⟨xc, _, σ', s', hkc, rfl, hsp, ?_, hdec, Q, Rl, J2, (by rw [hσ']; exact Sched.BodyEndA.ofScNtfn J hkc hks0 hkn0)⟩ + change (do schedContextUnbindNtfn (Z := Unit) (specPtr sc); pure ([] : List Data)) _ = _ + rw [nd_bind_oneSc spec] + rfl + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3ScUnbA.lean b/proofs/Fl4ProofsA64/P3/Stage3ScUnbA.lean new file mode 100644 index 000000000..d07fbce8e --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ScUnbA.lean @@ -0,0 +1,402 @@ +/- +P3 (aarch64, A64-P3 Stage 3, scbind): **SchedContext Unbind** at the record and at a syscall entry. + +* `scUnbind_specA`: the spec's three steps keep `SchedValidA`, the current thread's TCB (it is not the SC's) and the + current SC's budget (`BKeepA`). +* **`JStage3A.scUnbindA`**: `unbind_all_tcbs; unbind_ntfn; unbind_reply` refines the three steps and keeps the record; + the SC's reply stack becomes empty (`JStage3TA.scUnb_schedA` then `JStage3TA.rec_scEndA`). +* **`JSyscallA.scUnbindArmA`**: `decode_invocation` on a SchedContext cap with the Unbind label. + +Named premises: [P] `ArchTotal`, `MaxSpi`, `ArchPlat`, `A64FrameMasks`, `hCs`; [S] `h1` (`numNodes = 1`). Decode-site +case hypotheses: the decoder's check that the SC's thread is not the current one (`htw`, `hne`). New module. +-/ +import Fl4ProofsA64.P3.Stage3ScBindArmA +import Fl4ProofsA64.P3.DelScRecA +import Fl4ProofsA64.P3.Stage3DelSvScA +import Fl4ProofsA64.P3.Stage3TcbResumeRqA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +instance (p : ObjRef) : KPresBInst (schedContextUnbindAllTcbs (Z := Unit) p) := + ⟨by unfold schedContextUnbindAllTcbs; kbres⟩ +instance (p : ObjRef) : KPresBInst (schedContextUnbindNtfn (Z := Unit) p) := + ⟨by unfold schedContextUnbindNtfn doUnbindNtfnSc NonDet.maybeM; kbres⟩ +instance (p : ObjRef) : KPresBInst (schedContextUnbindReply (Z := Unit) p) := + ⟨by unfold schedContextUnbindReply; kbres⟩ + +/-- `unbind_all_tcbs` keeps every TCB but the SC's thread's. -/ +theorem unbindAll_othA {σ σ' : State Unit} {p : ObjRef} {x : SchedContext} {k : Nat} + (hk : σ.kheap p = some (.schedContext x k)) + (hr : ((), σ') ∈ (schedContextUnbindAllTcbs (Z := Unit) p σ).1) : + ∀ q z, x.scTcb ≠ some q → σ.kheap q = some (.tcb z) → σ'.kheap q = some (.tcb z) := by + intro q z hq hz + unfold schedContextUnbindAllTcbs at hr + rw [nd_bind_oneSc (Sched.getSchedContext_eqA hk)] at hr + unfold Fl4Spec.NonDet.when at hr + split_ifs at hr with hc + · obtain ⟨t, ht⟩ := Option.ne_none_iff_exists'.1 hc.1 + exact unbindTcb_othA hk ht hr q z (fun e => hq (by rw [ht, e])) hz + · have e : ((), σ') = ((), σ) := hr + cases e + exact hz + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s : S} {a : A} {σ : State Unit} {m : McState} + +set_option maxHeartbeats 4000000 in +/-- **The Unbind steps at the record** (module docstring). -/ +theorem JStage3A.scUnbindA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (sact : SActA σ) (nsw : NoSwA σ) + (hA : P0.ArchTotal ar) (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + {p : U64} (hp : p ∈ Z.C) (hni : specPtr p ≠ m.idleScOf m.curCore) : + (do + let (s1, a1) ← schedcontext.unbind_all_tcbs st ar s a p + let s2 ← schedcontext.unbind_ntfn st s1 p + let s3 ← schedcontext.unbind_reply st s2 p + ok (s3, a1)) ⦃ r => r.2 = a ∧ ∃ σ' Q Rl, + invokeSchedContext (Z := Unit) (.invokeSchedContextUnbind (specPtr p)) σ = ({([], σ')}, False) ∧ + NoSwA σ' ∧ SchedValidA σ' ∧ BKeepA σ σ' ∧ + (∀ q z, L.mem s (scTcbA p) = 0#u64 ∨ q ≠ specPtr (L.mem s (scTcbA p)) → σ.kheap q = some (.tcb z) → + σ'.kheap q = some (.tcb z)) ∧ + ∀ Ac', BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac' r.1 → + SysL st H Z.Cs OS ia ie r.1 → SymL st H Z.Cs r.1 → Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U r.1 → + J7A st (J7R2A st ar nA) Z H OS r.1 σ' m → + JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac' Pp Tb Lv ia ie N E Rp Bf + (Function.update stack p []) κ n (J5A st) (J7A st (J7R2A st ar nA)) r.1 a σ' m ⦄ := by + have T := J.toTA sact nsw + have hnm : scNameA m p = specPtr p := if_neg hni + have hni' : scNameA m p ≠ idleScPtr := by rw [hnm]; exact J.sr.sch.scOff p hp hni + obtain ⟨xs, ns, hk, hx⟩ := J.sr.scr.sc p hp + refine WP.spec_mono (T.scUnb_schedA hp hni hA hCs h1) ?_ + rintro ⟨s3, a3⟩ ⟨ea, s1, s2, σ1, σ2, σ3, Q1, R1, x1, x2, n1, F1, sp1, S1, hk1, sp2, K2, hk2, desc2, w02, kb02, z02, + sp3, H3, w3, z3, L3, -, -, hrp, hhead, desc3, -, -, M3, R3⟩ + dsimp only at ea H3 w3 hrp M3 R3 ⊢ + have m1 : ((), σ1) ∈ (schedContextUnbindAllTcbs (Z := Unit) (scNameA m p) σ).1 := by rw [sp1]; exact rfl + have m2 : ((), σ2) ∈ (schedContextUnbindNtfn (Z := Unit) (scNameA m p) σ1).1 := by rw [sp2]; exact rfl + have m3 : ((), σ3) ∈ (schedContextUnbindReply (Z := Unit) (scNameA m p) σ2).1 := by rw [sp3]; exact rfl + have rep1 := unbindAllTcbs_repA hk m1 + have o2 : ∀ r o, σ1.kheap r = some o → (∀ y n, o ≠ .schedContext y n) → (∀ z, o ≠ .notification z) → + σ2.kheap r = some o := by + intro r o h hs hnn + rcases desc2 with ⟨-, e⟩ | ⟨-, -, -, xn, hxn, e⟩ + · rw [e]; exact h + · have a' : r ≠ scNameA m p := fun e => by rw [e, hk1] at h; cases h; exact hs _ _ rfl + have b : r ≠ specPtr (L.mem s1 (scWA p 40#u64)) := fun e => by rw [e, hxn] at h; cases h; exact hnn _ rfl + rw [e] + unfold scNtfnLinkStA + rw [updKSc_kheap, Function.update_of_ne a', updKSc_kheap, Function.update_of_ne b]; exact h + -- the spec side + have sv : SchedValidA σ3 := by + have A1 := unbindAll_unbound (scNameA m p) hni' σ ⟨J.schedValid, Sched.JStage3A.rqDomPrioA J, fun u xu hu hb => by + obtain ⟨y, k, hy, hyt⟩ := J.sr.inv.back _ _ _ hu hb + exact ⟨y, k, hy, hyt⟩⟩ ((), σ1) m1 + have A2 := HT.unbound_tp (scNameA m p) (m := schedContextUnbindNtfn (Z := Unit) (scNameA m p)) + (by unfold schedContextUnbindNtfn doUnbindNtfnSc NonDet.maybeM; tpres) σ1 A1 ((), σ2) m2 + exact (HT.unbound_tp (scNameA m p) (m := schedContextUnbindReply (Z := Unit) (scNameA m p)) + (by unfold schedContextUnbindReply; tpres) σ2 A2 ((), σ3) m3).1 + have nsw3 : NoSwA σ3 := fun u c => by + refine S1.ns nsw u ?_ + rw [← show σ2.schedulerAction = σ1.schedulerAction by rw [K2.1], ← L3.2]; exact c + have BK : BKeepA σ σ3 := + PreO.trans ((inferInstance : KPresBInst (schedContextUnbindAllTcbs (Z := Unit) (scNameA m p))).pres σ _ m1) + (PreO.trans ((inferInstance : KPresBInst (schedContextUnbindNtfn (Z := Unit) (scNameA m p))).pres σ1 _ m2) + ((inferInstance : KPresBInst (schedContextUnbindReply (Z := Unit) (scNameA m p))).pres σ2 _ m3)) + have oth : ∀ q z, L.mem s (scTcbA p) = 0#u64 ∨ q ≠ specPtr (L.mem s (scTcbA p)) → σ.kheap q = some (.tcb z) → + σ3.kheap q = some (.tcb z) := by + intro q z hq hz + have a1 := unbindAll_othA hk m1 q z (fun e => by + rw [hx.tcb, ← scTcbA_eqSc] at e + by_cases h0 : L.mem s (scTcbA p) = 0#u64 + · rw [h0, optRefS_zero] at e; cases e + · rw [optRefS_ne h0] at e + exact hq.elim h0 fun c => c (Option.some.inj e).symm) hz + exact desc3.1 q _ (o2 q _ a1 (fun _ _ e => by cases e) (fun _ e => by cases e)) (fun _ _ e => by cases e) + (fun _ e => by cases e) + have inv : invokeSchedContext (Z := Unit) (.invokeSchedContextUnbind (specPtr p)) σ = ({([], σ3)}, False) := by + rw [← hnm] + change (do schedContextUnbindAllTcbs (Z := Unit) (scNameA m p) + schedContextUnbindNtfn (scNameA m p) + schedContextUnbindReply (scNameA m p) + pure ([] : List Data)) σ = _ + rw [nd_bind_oneSc sp1, nd_bind_oneSc sp2, nd_bind_oneSc sp3] + rfl + refine ⟨ea, σ3, Q1, R1, inv, nsw3, sv, BK, oth, fun Ac' bxi sysl symL userOK j7 => ?_⟩ + -- the end state against the start (`ScEndGA`) + have hP := T.iplaced + have wr : WritesIn st s s3 (ScFinWA st Z m σ s (L.mem s1 (scWA p 40#u64)) (L.mem s2 (scReplyA p)) 0#u64) := by + refine (w02.trans w3).mono fun b hb => ?_ + rcases hb with hb | hb + · rcases hb with hb | hb + · exact Or.inl hb + · exact Or.inr (Or.inl hb) + · rcases hb with hb | hb + · exact Or.inl (mwr_back (Z := Z) (Z' := { Z with Q := Q1, R := R1 }) rfl rfl z02 kb02 hb) + · exact Or.inr (Or.inr (Or.inl hb)) + have ntf : L.mem s1 (scWA p 40#u64) ≠ 0#u64 → + L.mem s1 (scWA p 40#u64) ∈ N ∧ L.mem s3 (nScSc (L.mem s1 (scWA p 40#u64))) = 0#u64 := by + intro h0 + rcases desc2 with ⟨c, -⟩ | ⟨-, qN, z0, -⟩ + · exact absurd c h0 + refine ⟨qN, ?_⟩ + have v : (nScSc (L.mem s1 (scWA p 40#u64))).val = (L.mem s1 (scWA p 40#u64)).val + 32 := by + have e : ∀ q, nScSc q = Notif.nScA q := fun _ => rfl + rw [e, Notif.nScA_eq, Notif.nfield_val (hP.n.ntfn _ qN) (by simp)]; simp + have al : Aligned (nScSc (L.mem s1 (scWA p 40#u64))) := by + have e : ∀ q, nScSc q = Notif.nScA q := fun _ => rfl + rw [e, Notif.nScA_eq]; exact Notif.nfield_aligned (hP.n.ntfn _ qN) (by simp) (by simp) + have hw : IpcObjWA N E Rp (nScSc (L.mem s1 (scWA p 40#u64))) := Or.inl ⟨_, qN, by omega, by omega⟩ + rw [w3.mem_eq al ?_, z0] + rintro (h' | ⟨h0', h'⟩) + · exact T.ipc_offMA hw (mwr_back (Z := Z) (Z' := { Z with Q := Q1, R := R1 }) rfl rfl z02 kb02 h') + · have hr := (hrp h0').1 + have n16 : (layout.reply.NEXT).val = 16 := by unfold layout.reply.NEXT; rfl + obtain ⟨-, -, rfit⟩ := hP.e.rep _ hr + have vr : (rNextA (L.mem s2 (scReplyA p))).val = (L.mem s2 (scReplyA p)).val + 16 := by + have e : rNextA (L.mem s2 (scReplyA p)) = wa (L.mem s2 (scReplyA p)) layout.reply.NEXT := by + unfold rNextA; rfl + rw [e, wa_valm, n16]; exact Nat.mod_eq_of_lt (by omega) + have := hP.nr _ qN _ hr + have := congrArg (fun w : U64 => w.val) h' + simp only [v, vr] at this + omega + have tcbE : ∀ r z, σ.kheap r = some (.tcb z) → ∃ y o, σ3.kheap r = some (.tcb y) ∧ + ((0#u64 ≠ 0#u64 ∧ r = specPtr 0#u64 → y = { yieldDoneA z 0 with tcbSchedContext := o }) ∧ + (¬ (0#u64 ≠ 0#u64 ∧ r = specPtr 0#u64) → y = { z with tcbSchedContext := o })) := by + intro r z h + obtain ⟨y1, h1', o, e1⟩ := F1.keep.tcb r z h + have h2 := o2 r _ h1' (fun _ _ e => by cases e) (fun _ e => by cases e) + exact ⟨y1, o, desc3.1 r _ h2 (fun _ _ e => by cases e) (fun _ e => by cases e), fun c => absurd rfl c.1, + fun _ => e1⟩ + have epE : ∀ r z, σ.kheap r = some (.endpoint z) → σ3.kheap r = some (.endpoint z) := fun r z h => by + have h1' := F1.keep.obj r _ h (fun _ _ e => by cases e) (fun _ e => by cases e) + exact desc3.1 r _ (o2 r _ h1' (fun _ _ e => by cases e) (fun _ e => by cases e)) (fun _ _ e => by cases e) + (fun _ e => by cases e) + have ntE : ∀ r z, σ.kheap r = some (.notification z) → ∃ o, + σ3.kheap r = some (.notification { z with ntfnSc := o }) ∧ + (L.mem s1 (scWA p 40#u64) ≠ 0#u64 ∧ r = specPtr (L.mem s1 (scWA p 40#u64)) → o = none) ∧ + (¬ (L.mem s1 (scWA p 40#u64) ≠ 0#u64 ∧ r = specPtr (L.mem s1 (scWA p 40#u64))) → o = z.ntfnSc) := by + intro r z h + have h1' := F1.keep.obj r _ h (fun _ _ e => by cases e) (fun _ e => by cases e) + have a' : r ≠ scNameA m p := fun e => by rw [e, hk1] at h1'; cases h1' + have done2 : ∀ w : Notification, σ2.kheap r = some (.notification w) → σ3.kheap r = some (.notification w) := + fun w h2 => desc3.1 r _ h2 (fun _ _ e => by cases e) (fun _ e => by cases e) + by_cases c : L.mem s1 (scWA p 40#u64) ≠ 0#u64 ∧ r = specPtr (L.mem s1 (scWA p 40#u64)) + · rcases desc2 with ⟨c0, -⟩ | ⟨-, -, -, xn, hxn, e⟩ + · exact absurd c0 c.1 + have : xn = z := by + rw [← c.2, h1'] at hxn + cases hxn; rfl + subst this + refine ⟨none, done2 _ ?_, fun _ => rfl, fun n => absurd c n⟩ + rw [e] + unfold scNtfnLinkStA + rw [updKSc_kheap, Function.update_of_ne a', updKSc_kheap, ← c.2, Function.update_self] + rfl + · refine ⟨z.ntfnSc, done2 _ ?_, fun c' => absurd c' c, fun _ => rfl⟩ + rcases desc2 with ⟨-, e⟩ | ⟨h0, -, -, xn, -, e⟩ + · rw [e]; exact h1' + · rw [e] + unfold scNtfnLinkStA + rw [updKSc_kheap, Function.update_of_ne a', updKSc_kheap, Function.update_of_ne (fun e' => c ⟨h0, e'⟩)] + exact h1' + have reE : ∀ r z, σ.kheap r = some (.reply z) → ∃ o, σ3.kheap r = some (.reply { z with replySc := o }) ∧ + (L.mem s2 (scReplyA p) ≠ 0#u64 ∧ r = specPtr (L.mem s2 (scReplyA p)) → o = none) ∧ + (¬ (L.mem s2 (scReplyA p) ≠ 0#u64 ∧ r = specPtr (L.mem s2 (scReplyA p))) → o = z.replySc) := by + intro r z h + have h1' := F1.keep.obj r _ h (fun _ _ e => by cases e) (fun _ e => by cases e) + exact desc3.2.1 r z (o2 r _ h1' (fun _ _ e => by cases e) (fun _ e => by cases e)) + have scE : ∀ r y k, σ.kheap r = some (.schedContext y k) → ∃ y', σ3.kheap r = some (.schedContext y' k) ∧ + (r = scNameA m p → y'.scReplies = []) ∧ (r ≠ scNameA m p → y'.scReplies = y.scReplies) := by + intro r y k h + obtain ⟨y1, h1', e1⟩ := rep1 r y k h + obtain ⟨y2, h2, e2⟩ : ∃ y2, σ2.kheap r = some (.schedContext y2 k) ∧ y2.scReplies = y1.scReplies := by + rcases desc2 with ⟨-, e⟩ | ⟨-, -, -, xn, hxn, e⟩ + · exact ⟨y1, by rw [e]; exact h1', rfl⟩ + · have b : r ≠ specPtr (L.mem s1 (scWA p 40#u64)) := fun e' => by rw [e', hxn] at h1'; cases h1' + by_cases a' : r = scNameA m p + · subst a' + have exy : y1 = x1 ∧ k = n1 := by rw [h1'] at hk1; simpa using hk1 + rw [exy.1, exy.2] + refine ⟨{ x1 with scNtfn := none }, ?_, rfl⟩ + rw [e] + unfold scNtfnLinkStA + rw [updKSc_kheap, Function.update_self] + rfl + · refine ⟨y1, ?_, rfl⟩ + rw [e] + unfold scNtfnLinkStA + rw [updKSc_kheap, Function.update_of_ne a', updKSc_kheap, Function.update_of_ne b] + exact h1' + obtain ⟨y3, h3, a3', b3⟩ := desc3.2.2 r y2 k h2 + exact ⟨y3, h3, a3', fun c => (b3 c).trans (e2.trans e1)⟩ + have END : ScEndGA st Z N Rp m s s3 σ σ3 (L.mem s1 (scWA p 40#u64)) (L.mem s2 (scReplyA p)) 0#u64 0 := + ⟨wr, ntf, fun h0 => ⟨(hrp h0).1, (hrp h0).2.1⟩, fun h => absurd rfl h, tcbE, epE, ntE, reE, M3, R3⟩ + -- the reply stacks + have hRp : L.mem s2 (scReplyA p) ≠ 0#u64 → L.mem s2 (scReplyA p) ∈ Rp := fun c => (hrp c).1 + have vnext : ∀ r ∈ Rp, (rNextA r).val = r.val + 16 := fun r hr => (replyNextPlaceA (Z := Z) hP.e hr).2 + have vq : L.mem s1 (scWA p 40#u64) ≠ 0#u64 → + (nScSc (L.mem s1 (scWA p 40#u64))).val = (L.mem s1 (scWA p 40#u64)).val + 32 := fun c => by + have e : ∀ q, nScSc q = Notif.nScA q := fun _ => rfl + rw [e, Notif.nScA_eq, Notif.nfield_val (hP.n.ntfn _ (ntf c).1) (by simp)]; simp + have hsub : ∀ c, ∀ r ∈ Function.update stack p [] c, r ∈ stack c := by + intro c r hr + by_cases e : c = p + · subst e; rw [Function.update_self] at hr; cases hr + · rwa [Function.update_of_ne e] at hr + have reply : ReplyStackRelA (L.mem s3) Rp Z.C σ3 (scNameA m) (Function.update stack p []) := by + have rw8 : ∀ r ∈ Rp, ∀ c : U64, c.val = r.val + 8 ∨ (c.val = r.val + 16 ∧ + ¬ (L.mem s2 (scReplyA p) ≠ 0#u64 ∧ r = L.mem s2 (scReplyA p))) → Aligned c → L.mem s3 c = L.mem s c := by + intro r hr c hc al + obtain ⟨r32, -, -⟩ := hP.e.rep r hr + have hw : IpcObjWA N E Rp c := Or.inr (Or.inr ⟨r, hr, by omega, by omega⟩) + refine END.wr.mem_eq al ?_ + rintro (x | ⟨c0, x⟩ | ⟨c0, x⟩ | ⟨c0, -⟩) + · exact T.ipc_offMA hw x + · have v : c.val = (L.mem s1 (scWA p 40#u64)).val + 32 := by rw [x, vq c0] + have := hP.nr _ (ntf c0).1 r hr + omega + · have v : c.val = (L.mem s2 (scReplyA p)).val + 16 := by rw [x, vnext _ (hRp c0)] + obtain ⟨h32, -, -⟩ := hP.e.rep _ (hRp c0) + rcases hc with hc | ⟨hc, hn⟩ + · omega + · exact hn ⟨c0, UScalar.eq_of_val_eq (by omega)⟩ + · exact c0 rfl + have al : ∀ r ∈ Rp, ∀ c : U64, c.val = r.val + 8 ∨ c.val = r.val + 16 → Aligned c := by + intro r hr c hc + obtain ⟨r32, -, -⟩ := hP.e.rep r hr + show c.val % 8 = 0 + omega + have vprev : ∀ r ∈ Rp, (rPrevA r).val = r.val + 8 := fun r hr => (replyPrevPlaceA (Z := Z) hP.e hr).2 + have hh : (stack p).head?.map specPtr = optRefS (L.mem s2 (scReplyA p)) := by + obtain ⟨⟨y, k', hy, hl⟩, -⟩ := J.reply.stackRel p hp + rw [hk] at hy + cases hy + obtain ⟨y1, e1, r1⟩ := rep1 _ _ _ hk + have e2 : x2.scReplies = xs.scReplies := by + rcases desc2 with ⟨-, e⟩ | ⟨-, -, -, xn, -, e⟩ + · rw [e, e1] at hk2 + cases hk2; exact r1 + · have h2 := hk2 + rw [e] at h2 + unfold scNtfnLinkStA at h2 + rw [updKSc_kheap, Function.update_self] at h2 + rw [e1] at hk1 + cases h2 + cases hk1 + exact r1 + rw [← hhead, e2, hl, List.head?_map] + refine ReplyStackRelA.unbindHeadA J.reply (fun d hd => replyPointerNonzeroA (Z := Z) hP.e hd) + (fun d hd => by have := (hP.e.rep d hd).1; omega) hp hh + (fun d hd => rw8 d hd _ (Or.inl (vprev d hd)) (al d hd _ (Or.inl (vprev d hd)))) + (fun d hd c => rw8 d hd _ (Or.inr ⟨vnext d hd, c⟩) (al d hd _ (Or.inr (vnext d hd)))) + (fun c => (END.rep c).2) ?_ ?_ (fun d hd => ?_) (fun d hd x' hx' => ?_) + · obtain ⟨y, hy, e, -⟩ := scE _ _ _ hk + exact ⟨y, ns, hy, e rfl⟩ + · intro c hc ne x' k' hx' + obtain ⟨y, hy, -, e⟩ := scE _ _ _ hx' + exact ⟨y, k', hy, e fun x => ne (J.sr.scp.inj c hc p hp x)⟩ + · obtain ⟨x', hx', -⟩ := J.ep.reply d hd + exact ⟨x', hx'⟩ + · exact END.re _ _ hx' + have F : SchedFrameA σ σ3 := + PreO.trans ((inferInstance : SPresInst (schedContextUnbindAllTcbs (Z := Unit) (scNameA m p))).pres σ ((), σ1) m1) + (PreO.trans ((inferInstance : SPresInst (schedContextUnbindNtfn (Z := Unit) (scNameA m p))).pres σ1 ((), σ2) m2) + ((inferInstance : SPresInst (schedContextUnbindReply (Z := Unit) (scNameA m p))).pres σ2 ((), σ3) m3)) + have Qk : QKeepA σ σ3 := ((Sched.qk_of m1).trans (Sched.qk_of m2)).trans (Sched.qk_of m3) + have bk : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac' s3 := + ⟨bxi.1, bxi.2.1, bxi.2.2.1, bxi.2.2.2.2, bxi.2.2.2.1⟩ + obtain ⟨J', w, -, -⟩ := T.rec_scEndA bk H3 L3.1 nsw3 F Qk END hsub reply ⟨xs, ns, hk⟩ (fun _ _ => rfl) + exact J.of_stepStkA (Q := Q1) (Rl := R1) (curOf := Z.curOf) (curScOf := Z.curScOf) w F.1 F.2.1 Qk F.2.2 + (fun _ _ => rfl) bxi sysl symL userOK j7 J'.sr J'.ep J'.ntfn J'.reply + (fun r hr hd c hc h => J.deadExt.reply r hr hd c hc (hsub c r h)) J'.bufCover J'.bufAl J'.idleNoFault J'.mcp + J'.yieldRel J'.sinv J'.sidle J'.runVcpu J'.blkQ sv + +set_option maxHeartbeats 4000000 in +/-- **SchedContext Unbind at a syscall entry** (module docstring). -/ +theorem JSyscallA.scUnbindArmA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + {slot lbl len buf sc sz t : U64} {blk call don : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .SchedContext sc sz) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hlbl : lbl = fl4_abi.label.SchedContextUnbind) + (htw : obj.Sc.tcb st sc s = ok t) (hne : t ≠ L.curW s) : + ∃ xc σ1 σ2 s2, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + invokeSchedContext (Z := Unit) (.invokeSchedContextUnbind (specPtr sc)) σ1 = ({([], σ2)}, False) ∧ + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (.Ok (), s2, a) ∧ + ∃ Q Rl Ac' stack', JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac' Pp Tb Lv ia ie N E Rp Bf stack' κ n + (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s2 a σ2 m ∧ Sched.BodyEndA σ σ2 m := by + obtain ⟨xc, hkc, hst, hsp, J1, sact1, nsw1, -, hc, hname, hni, far⟩ := J.scTcbEntryA hCs h1 hslot hcov hcap hext + have T0 := J.toJStage3A.toTA J.sact J.noSwitch + have hni0 : specPtr sc ≠ m.idleScOf m.curCore := by + have hH : H sc = some (.sc sz.val) := (T0.capTy slot hslot).2.2.2.1 sc sz hcap + exact T0.ciSc slot hslot sc sz hcap + have w0 : L.mem s (scTcbA sc) = t := by + have h := eq_of_spec_fd (obj.Sc.tcb.spec (L := L) s sc) + rw [htw] at h + exact (ok_inj_ff h).symm + have hstall : (if t != 0#u64 then smp.remote_tcb_stall st ar s a t else ok (s, a)) = ok (s, a) := by + by_cases t0 : t = 0#u64 + · rw [if_neg (by simp [t0])] + · rw [if_pos (bne_iff_ne.2 t0)] + have hcH : sc ∈ H.scs := by rw [J.scs]; exact hc + have ht : t ∈ Z.T := by + rw [← J.tcbs, ← w0]; exact J.sys.ent.bxi.2.2.1.2.2.yt.1 sc hcH (by rw [w0]; exact t0) + exact T0.stall_singleA hCs h1 ht + obtain ⟨y, k, hky, hy⟩ := J.sr.scr.sc sc hc + have hscur : scNameA m sc ≠ σ.curThread := fun e => by + rw [e, hkc] at hky; cases hky + have hs1 : L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART) (scTcbA sc) = t := by + obtain ⟨z, j, hz, hzR⟩ := J1.sr.scr.sc sc hc + rw [far _ hscur, hky] at hz + cases hz + have e1 := hzR.tcb + rw [hy.tcb, ← scTcbA_eqSc] at e1 + exact (scOpt_injA e1).symm.trans w0 + have R := J1.scUnbindA sact1 nsw1 hA hCs h1 hc hni0 + obtain ⟨⟨s2, a2⟩, prog, ea, σ2, Q, Rl, hspec, NS, SV, BK, OT, K⟩ := exists_of_specA R + simp only at ea + subst a2 + have hdec : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (.Ok (), s2, a) := by + rw [hcap, decodeSc_programA, if_neg Bool.false_ne_true, hlbl, + sc_decode_unbind_eqA htw hstall (cur_thread_ok s) htw hne, hst, bind_ok] + refine (scUnb3_assocA (schedcontext.unbind_all_tcbs st ar (LawfulStore.wr st s (stA (LawfulStore.curW s)) sched.TS_RESTART) a sc) + (fun s3 => schedcontext.unbind_ntfn st s3 sc) (fun s4 => schedcontext.unbind_reply st s4 sc) + (fun s3 a2 => ok (core.result.Result.Ok (T := Unit) (E := _) (), s3, a2))).trans ?_ + exact (congrArg (fun x => Std.bind x fun r => ok (core.result.Result.Ok (), r.1, r.2)) prog).trans (bind_ok _ _) + obtain ⟨-, Ac', sysl, symL, bxi, userOK, j7⟩ := J.decodeFinA hA hM hPl hfm hslot hcov + (rfl : capA st s slot = capA st s slot) hext hSC (lbl := lbl) + (by rw [hcap, hlbl]; simp [DecArmS1aEnt, DecArmQH, DecArmQZ2, DecArmKQ2, DecArmKQ, DecArmKQ3, DecArmDelQ, DecArmS1aC, capFam]) + (fun _ h => by rw [hcap] at h; cases h) (fun _ h => by rw [hcap] at h; cases h) hdec σ2 m + have J2 := K Ac' bxi sysl symL userOK j7 + have htc : t ≠ 0#u64 → specPtr t ≠ σ.curThread := fun _ e => hne (specPtr_inj (e.trans J.sr.sch.curThread)) + have hoth : σ2.kheap σ.curThread = some (.tcb { xc with tcbState := .restart }) := by + refine OT _ _ ?_ (by simp only [stUpdA, setObjA, Function.update_self]) + rw [hs1] + by_cases t0 : t = 0#u64 + · exact Or.inl t0 + · exact Or.inr (Ne.symm (htc t0)) + exact ⟨xc, _, σ2, s2, hkc, rfl, hsp, hspec, hdec, Q, Rl, Ac', _, J2, + Sched.BodyEndA.ofScTcbA J hkc BK hoth NS J2.sr.inv J2.sinv.zc⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3ScYieldA.lean b/proofs/Fl4ProofsA64/P3/Stage3ScYieldA.lean new file mode 100644 index 000000000..f07f1d771 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ScYieldA.lean @@ -0,0 +1,280 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 3): **SchedContext YieldTo, the reply arm, at the record**. + +`decode_invocation` on a SchedContext cap with the YieldTo label at a syscall entry (`JSyscallA.scYieldReplyA`), for +a bound thread that is not schedulable: `yield_to` finds no previous yielder, `resume` and `yield_switches` write +nothing, and the caller gets the consumed time at once (`consumedReplyA`). + +Named premises: +* [P] `ArchTotal`, `MaxSpi`, `ArchPlat`, `A64FrameMasks`, `hCs`, `hmb`, `hmax`, `htu`; +* [J] `hbound` (the bound thread's SC is this SC, as in `sc_decode_yield_to_localA`); +* [S] `h1` (`numNodes = 1`), `hyf` (no previous yielder: `complete_yield_to` is not lifted), `hon` (the target is on + this core), `hns` (the target is not schedulable: the lower-priority requeue and the switching arm are not lifted). + +The decode succeeds; the `Err` returns are not lifted. New module. +-/ +import Fl4ProofsA64.P3.Stage3ScConsumedA +import Fl4ProofsA64.P3.ScYieldResumeA +import Fl4ProofsA64.P3.ScYieldSwitchA +import Fl4ProofsA64.P3.ScYieldCompleteA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + +/-- The YieldTo decode of a bound, other thread whose priority the caller controls. -/ +theorem scDecodeYield_eqA {s s1 : S} {a : A} {sc t ct : U64} (ht : obj.Sc.tcb st sc s = ok t) + (he : (if t != 0#u64 then smp.remote_tcb_stall st ar s a t else ok (s, a)) = ok (s, a)) + (hcur : sched.cur_thread st s = ok ct) (ht0 : t ≠ 0#u64) (hne : t ≠ ct) + (hp : ¬ L.mem s (prioA t) > L.mem s (mcpA ct)) (hy : L.mem s (yieldA ct) = 0#u64) + (hst : sched.set_thread_state st s ct sched.TS_RESTART = ok s1) + (call : Bool) (extra : Aeneas.Std.Array obj.Slot 3#usize) : + schedcontext.decode st ar s a fl4_abi.label.SchedContextYieldTo sc call extra = (do + let (s3, a2) ← schedcontext.yield_to st ar s1 a sc call + ok (core.result.Result.Ok (), s3, a2)) := by + unfold schedcontext.decode + rw [ht, bind_ok, he, bind_ok] + simp only [uncurry_apply_pair, hcur, bind_ok] + have hConsumed : fl4_abi.label.SchedContextYieldTo ≠ fl4_abi.label.SchedContextConsumed := by + unfold fl4_abi.label.SchedContextYieldTo fl4_abi.label.SchedContextConsumed + decide + have hBind : fl4_abi.label.SchedContextYieldTo ≠ fl4_abi.label.SchedContextBind := by + unfold fl4_abi.label.SchedContextYieldTo fl4_abi.label.SchedContextBind + decide + have hObj : fl4_abi.label.SchedContextYieldTo ≠ fl4_abi.label.SchedContextUnbindObject := by + unfold fl4_abi.label.SchedContextYieldTo fl4_abi.label.SchedContextUnbindObject + decide + have hUnb : fl4_abi.label.SchedContextYieldTo ≠ fl4_abi.label.SchedContextUnbind := by + unfold fl4_abi.label.SchedContextYieldTo fl4_abi.label.SchedContextUnbind + decide + simp only [hConsumed, hBind, hObj, hUnb, ↓reduceIte] + rw [ht, bind_ok, eq_of_spec_fd (obj.Tcb.is_null.spec t), bind_ok, decide_eq_false ht0] + simp only [Bool.false_eq_true, ↓reduceIte] + have htne : obj.Tcb.Insts.CoreCmpPartialEqTcb.eq t ct = ok false := by + unfold obj.Tcb.Insts.CoreCmpPartialEqTcb.eq + simp [hne] + rw [htne, bind_ok] + simp only [Bool.false_eq_true, ↓reduceIte] + rw [eq_of_spec_fd (obj.Tcb.prio.spec s t), bind_ok, obj.Tcb.mcp.specA, bind_ok, if_neg hp, + eq_of_spec_fd (obj.Tcb.yield_to.spec s ct), bind_ok, hy, hst, bind_ok] + rfl + +/-- `yield_to` with no previous yielder, a no-op `resume` and an unschedulable bound thread. -/ +theorem yieldTo_notReady_concA {s1 : S} {a : A} {sc t ct : U64} (call : Bool) + (hF : L.mem s1 (scYieldFromA sc) = 0#u64) (eres : schedcontext.resume st ar s1 a sc = ok (s1, a)) + (hcur : sched.cur_thread st s1 = ok ct) (hT : L.mem s1 (scTcbA sc) = t) + (hs : sched.is_schedulable st s1 t = ok false) : + schedcontext.yield_to st ar s1 a sc call = (do + let s3 ← schedcontext.set_consumed st ar s1 a sc ct call + let s4 ← sched.set_thread_state st s3 ct sched.TS_RUNNING + ok (s4, a)) := by + have hsw : schedcontext.yield_switches st s1 sc ct = ok (false, s1) := by + unfold schedcontext.yield_switches + rw [eq_of_spec_fd (obj.Sc.tcb.spec s1 sc), bind_ok, hT, hs, bind_ok] + rfl + have hfrom0 : obj.Sc.yield_from st sc s1 = ok 0#u64 := by + unfold obj.Sc.yield_from layout.sc.YIELD_FROM + simp only [lift, bind_ok] + rw [show wa sc 56#u64 = scYieldFromA sc by unfold scYieldFromA layout.sc.YIELD_FROM; rfl, L.read_eq, hF] + unfold schedcontext.yield_to + rw [hfrom0, bind_ok, eq_of_spec_fd (obj.Tcb.is_null.spec _), bind_ok] + simp only [decide_true, ↓reduceIte, bind_ok, uncurry_apply_pair] + rw [eres, bind_ok] + simp only [uncurry_apply_pair] + rw [hcur, bind_ok, hsw, bind_ok] + simp only [uncurry_apply_pair, Bool.false_eq_true, ↓reduceIte] + +/-- An unschedulable thread, as `is_schedulable` reads it. -/ +theorem notReady_isSchedulableA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRN st Z m κ s σ) {t : U64} (ht : t ∈ Z.T) + (hrel : (L.mem s (inqA t) &&& sched.Q_RELEASE) = 0#u64 ↔ specPtr t ∉ σ.releaseQueue) + (hst : StEqA Z.T (L.mem s) σ) (hns : schedulable (specPtr t) σ = false) : + sched.is_schedulable st s t = ok false := by + rw [is_schedulable_eqA, schedulable_eqA H ht hrel hst, hns] + +/-- The spec's `yield_to` in the same case: the consumed time at once. -/ +theorem yieldTo_notReady_specA {σ σ2 : State Unit} {p t : ObjRef} {y : SchedContext} {n : Nat} {d : List Data} + (hk : σ.kheap p = some (.schedContext y n)) (hf0 : y.scYieldFrom = none) (htb : y.scTcb = some t) + (hsched : schedulable t σ = false) (hres : schedContextResume (Z := Unit) p σ = ({((), σ)}, False)) + (hret : returnConsumed (Z := Unit) p σ = ({(d, σ2)}, False)) : + invokeSchedContext (Z := Unit) (.invokeSchedContextYieldTo p) σ = ({(d, σ2)}, False) := by + change Fl4SpecA64.schedContextYieldTo (Z := Unit) p σ = _ + have assoc {α β γ : Type} (f : SMonad Unit α) (g : α → SMonad Unit β) (k : β → SMonad Unit γ) : + (f >>= g) >>= k = (f >>= fun x => g x >>= k) := funext (nd_bind_assocSc f g k) + have eq : schedContextYieldTo (Z := Unit) p = (do + completeScYielderA p + schedContextResume p + let b ← yieldSwitchA p + if b then returnConsumed p else pure []) := by + unfold schedContextYieldTo yieldSwitchA completeScYielderA + simp only [assoc] + have e0 : completeScYielderA p σ = ({((), σ)}, False) := by + unfold completeScYielderA + rw [nd_bind_oneSc (getScYieldFrom_eqA hk), hf0, Fl4Spec.NonDet.when, if_neg (by simp)] + rfl + have esw : yieldSwitchA p σ = ({(true, σ)}, False) := by + unfold yieldSwitchA + rw [nd_bind_oneSc (getScTcb_eqSc hk), htb, + nd_bind_oneSc (show Fl4Spec.NonDet.assertOpt (σ := State Unit) (some t) σ = ({(t, σ)}, False) from rfl), + nd_gets_bindSc, hsched] + rfl + rw [eq, nd_bind_oneSc e0, nd_bind_oneSc hres, nd_bind_oneSc esw] + exact hret + +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {s : S} {a : A} {σ : State Unit} {m : McState} + +set_option maxHeartbeats 4000000 in +/-- **SchedContext YieldTo's reply arm at a syscall entry** (module docstring). -/ +theorem JSyscallA.scYieldReplyA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {slot lbl len buf sc sz : U64} {blk call don : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .SchedContext sc sz) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hlbl : lbl = fl4_abi.label.SchedContextYieldTo) + (ht0 : L.mem s (scTcbA sc) ≠ 0#u64) (hne : L.mem s (scTcbA sc) ≠ L.curW s) + (hprio : ¬ L.mem s (prioA (L.mem s (scTcbA sc))) > L.mem s (mcpA (L.curW s))) + (hy : L.mem s (yieldA (L.curW s)) = 0#u64) (hyf : L.mem s (scYieldFromA sc) = 0#u64) + (hon : L.mem s (affA (L.mem s (scTcbA sc))) = L.cpu s) (hbound : L.mem s (scA (L.mem s (scTcbA sc))) = sc) + (hns : ∀ xc, σ.kheap σ.curThread = some (.tcb xc) → + schedulable (specPtr (L.mem s (scTcbA sc))) (stUpdA σ σ.curThread xc .restart) = false) : + ∃ xc σ1 σ2 d σ3 s3, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + invokeSchedContext (Z := Unit) (.invokeSchedContextYieldTo (specPtr sc)) σ1 = ({(d, σ2)}, False) ∧ + getThreadState (Z := Unit) σ.curThread σ2 = ({(.restart, σ2)}, False) ∧ + ((), σ3) ∈ ((do + Fl4Spec.NonDet.when call (Fl4SpecA64.replyFromKernel (Z := Unit) σ.curThread (0, d)) + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .running) σ2).1 ∧ + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (.Ok (), s3, a) ∧ + ∃ Q Rl, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s3 a σ3 m ∧ Sched.BodyEndA σ σ3 m := by + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hslot hcov J.actW J.running J.cur + have hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM := J.toJStage3EntryA.curNotVmA + obtain ⟨xc, hkc, hst, hsp, J1, -, -, -⟩ := J.toJStage3A.of_restartCurA RS.pl.choose_spec.choose_spec.choose_spec.choose_spec + RS.sym RS.run hnvm RS.cur RS.vs RS.vc + have T0 := J.toJStage3A.toTA J.sact J.noSwitch + have hH : H sc = some (.sc sz.val) := (T0.capTy slot hslot).2.2.2.1 sc sz hcap + have hcH : sc ∈ H.scs := ⟨_, hH⟩ + have hc : sc ∈ Z.C := by rw [← J.scs]; exact hcH + have hci : specPtr sc ≠ m.idleScOf m.curCore := T0.ciSc slot hslot sc sz hcap + have hname : scNameA m sc = specPtr sc := by unfold scNameA; rw [if_neg hci] + have hC0 : sc ≠ 0#u64 := by + obtain ⟨-, -, hX, -, -⟩ := BXI.blk J.sys.ent.bxi + exact hX.1.sc0 sc hcH + obtain ⟨t, htd⟩ : ∃ t, t = L.mem s (scTcbA sc) := ⟨_, rfl⟩ + obtain ⟨c0, hc0d⟩ : ∃ c, c = L.curW s := ⟨_, rfl⟩ + rw [← htd] at ht0 hne hprio hon hbound hns + rw [← hc0d] at hne hprio hy hst J1 + have ht : t ∈ Z.T := by + rw [htd] at ht0 ⊢; rw [← J.tcbs]; exact T0.blkA.2.2.1.2.2.yt.1 sc hcH ht0 + have hcZ : c0 ∈ Z.T := by rw [hc0d]; exact J.sr.curT + have hca : TcbAddr c0 := J.sr.core.tcbs _ hcZ + have hta : TcbAddr t := J.sr.core.tcbs t ht + have hstall : (if t != 0#u64 then smp.remote_tcb_stall st ar s a t else ok (s, a)) = ok (s, a) := by + rw [htd]; exact J.scStallA hCs h1 hslot hcap + obtain ⟨s1, hs1⟩ : ∃ q, q = L.wr s (stA c0) sched.TS_RESTART := ⟨_, rfl⟩ + obtain ⟨σ1, hσ1⟩ : ∃ q, q = stUpdA σ σ.curThread xc .restart := ⟨_, rfl⟩ + rw [← hs1] at hst J1 + rw [← hσ1] at hsp J1 + have sA : Aligned (stA c0) := by rw [stA_eq]; exact field_aligned hca (by simp) (by simp) + have farT : ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → L.mem s1 (wa t K) = L.mem s (wa t K) := + fun K k1 k2 => by + rw [hs1] + exact mem_wr_ne' sA (field_aligned hta k1 k2) + (by rw [stA_eq]; exact field_ne (K2 := K) hca hta (by simp) k1 (fun e => absurd e.symm hne)) + -- the SC's words across the restart + obtain ⟨y0, n0, hk0, hy0⟩ := J.sr.scr.sc sc hc + obtain ⟨y1, n1, hk1, hy1⟩ := J1.sr.scr.sc sc hc + have hkk : σ1.kheap (scNameA m sc) = some (.schedContext y0 n0) := by + simp only [hσ1, stUpdA, setObjA] + rw [Function.update_of_ne (fun e => by rw [e, hkc] at hk0; cases hk0)]; exact hk0 + rw [hkk] at hk1 + cases hk1 + have optInj : ∀ u v : U64, optRefS u = optRefS v → u = v := fun u v h => by + by_cases hu : u = 0#u64 <;> by_cases hv : v = 0#u64 + · exact hu.trans hv.symm + · rw [hu, optRefS_zero, optRefS_ne hv] at h; cases h + · rw [hv, optRefS_zero, optRefS_ne hu] at h; cases h + · rw [optRefS_ne hu, optRefS_ne hv] at h + exact specPtr_inj (Option.some.inj h) + have hT1 : L.mem s1 (scTcbA sc) = t := by + apply optInj + rw [scTcbA_eqSc, ← hy1.tcb, hy0.tcb, ← scTcbA_eqSc, htd] + have a56 : scYieldFromA sc = scWA sc 56#u64 := by unfold scYieldFromA scWA layout.sc.YIELD_FROM; rfl + have hF1 : L.mem s1 (scYieldFromA sc) = 0#u64 := by + apply optInj + rw [a56, ← hy1.yieldFrom, hy0.yieldFrom, ← a56, hyf] + have hbound1 : L.mem s1 (scA t) = sc := (farT 3464#u64 (by simp) (by simp)).trans hbound + have hon1 : L.mem s1 (affA t) = L.cpu s1 := yieldAffKeepA J.sr J1.sr ht ht hon + have states1 : StEqA Z.T (L.mem s1) σ1 := fun u hu y hy => (J1.ep.at_tcb hu hy).st + have hsched : schedulable (specPtr t) σ1 = false := by rw [hσ1]; exact hns xc hkc + -- resume, yield_switches + have R1 := yield_resume_noopA (ar := ar) J1.sr states1 a hc hC0 ht hon1 hT1 hbound1 (hmb a) + obtain ⟨⟨s1', a'⟩, eres, ⟨er, hres⟩⟩ := exists_of_specA R1 + cases er + have hs : sched.is_schedulable st s1 t = ok false := + notReady_isSchedulableA J1.sr.toSchedSRN ht (J1.sr.relOn ht hon1) states1 hsched + have hcur1 : sched.cur_thread st s1 = ok c0 := + (eq_of_spec_fd (sched.cur_thread.spec s1)).trans + (congrArg ok (specPtr_inj (J1.j.1.sch.curThread.symm.trans + (show σ1.curThread = specPtr c0 by rw [hσ1, hc0d]; exact J.j.1.sch.curThread)))) + have hyt := yieldTo_notReady_concA (ar := ar) call hF1 eres hcur1 hT1 hs + have hprog : ∀ c su, schedcontext.update_consumed st ar s1 a sc = ok (c, su) → + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + (do let s4 ← vspace.reply1 st su c0 call c; ok (core.result.Result.Ok (), s4, a)) := fun c su hu => by + have htSc : obj.Sc.tcb st sc s = ok t := by rw [htd]; exact eq_of_spec_fd (P1.obj.Sc.tcb.spec s sc) + have hct : sched.cur_thread st s = ok c0 := by rw [hc0d]; exact cur_thread_ok s + rw [hcap, decodeSc_programA, if_neg Bool.false_ne_true, hlbl, + scDecodeYield_eqA htSc hstall hct ht0 hne hprio hy hst, hyt] + unfold schedcontext.set_consumed vspace.reply1 + rw [hu, bind_ok] + cases call <;> simp [Std.bind_assoc] <;> rfl + obtain ⟨xc', σ1', c, su, σ2, σ3, s3, hkc', e1, -, hu, hret, hrun, e3, Q, Rl, J2⟩ := + J.consumedReplyA hA hmax htu hslot hcov hcap hext call (fun c su hu s' h2 σX => by + rw [← hc0d, ← hs1] at hu + rw [← hc0d] at h2 + have hd : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (core.result.Result.Ok (), s', a) := by rw [hprog c su hu, h2, bind_ok] + obtain ⟨-, -, sysl, symL, -, userOK, j7⟩ := J.decodeFinA hA hM hPl hfm hslot hcov + (rfl : capA st s slot = capA st s slot) hext hSC (lbl := lbl) + (by rw [hcap, hlbl]; simp [DecArmS1aEnt, DecArmQH, DecArmQZ2, DecArmKQ2, DecArmKQ, DecArmKQ3, DecArmDelQ, + DecArmS1aC, capFam]) + (fun _ h => by rw [hcap] at h; cases h) (fun _ h => by rw [hcap] at h; cases h) hd σX m + exact ⟨sysl, symL, userOK, j7⟩) + rw [hkc] at hkc'; cases hkc' + have e1' : σ1' = σ1 := e1.trans hσ1.symm + subst σ1' + rw [← hc0d, ← hs1] at hu + rw [← hc0d] at e3 + rw [← hσ1] at hret + have hf0 : y0.scYieldFrom = none := by rw [hy0.yieldFrom, ← a56, hyf, optRefS_zero] + have htb : y0.scTcb = some (specPtr t) := by rw [hy0.tcb, ← scTcbA_eqSc, ← htd, optRefS_ne ht0] + have hst2 : getThreadState (Z := Unit) σ.curThread σ2 = ({(.restart, σ2)}, False) := + returnConsumed_stateA hkk (x := { xc with tcbState := .restart }) + (by rw [hσ1]; simp only [stUpdA, setObjA, Function.update_self]) rfl hret + refine ⟨xc, σ1, σ2, wordsFromTime c.bv, σ3, s3, hkc, hσ1, hsp, ?_, hst2, hrun, ?_, Q, Rl, J2⟩ + · rw [← hname] + exact yieldTo_notReady_specA hkk hf0 htb hsched hres hret + · rw [hprog c su hu, e3, bind_ok] + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3ScYieldRqA.lean b/proofs/Fl4ProofsA64/P3/Stage3ScYieldRqA.lean new file mode 100644 index 000000000..9d81d8289 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ScYieldRqA.lean @@ -0,0 +1,418 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 3): **SchedContext YieldTo, the requeue arm, at the record**. + +`decode_invocation` on a SchedContext cap with the YieldTo label at a syscall entry (`JSyscallA.scYieldRequeueA`), for +a bound schedulable thread of lower priority than the caller: no previous yielder, `resume` writes nothing, +`yield_switches` dequeues and re-enqueues the target (spec `tcbSchedDequeue`; `tcbSchedEnqueue`), and the caller gets +the consumed time at once (`consumedMidA`). The spec invariants after the requeue go through `LoopInvA.queueE` +(`SInvXA` / `SIdleA` / `SActA`) and `HT.deq` / `HT.enq` (`SchedValidA`). + +Named premises: +* [P] `ArchTotal`, `MaxSpi`, `ArchPlat`, `A64FrameMasks`, `hCs`, `hmb`, `hmax`, `htu`; +* [J] `hbound` (the bound thread's SC is this SC); +* [S] `h1` (`numNodes = 1`), `hyf` (no previous yielder), `hon` (the target is on this core), `hsch` (the target is + schedulable), `hlt` (its priority is below the caller's). + +The decode succeeds; the `Err` returns are not lifted. New module. +-/ +import Fl4ProofsA64.P3.Stage3ScYieldA +import Fl4ProofsA64.P3.Stage3DelSchedValidA +import Fl4ProofsA64.P1.QueueYield +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-- A ready-queue write by an action that adds at most `t`. -/ +theorem tcbSchedAction_shapeA (action : ObjRef → List ObjRef → List ObjRef) + (hsub : ∀ t q y, y ∈ action t q → y = t ∨ y ∈ q) (t : ObjRef) {σ : State Unit} {r : Unit × State Unit} + (hr : r ∈ (tcbSchedAction (Z := Unit) action t σ).1) : + QueueE (fun y => y = t) σ r.2 ∧ r.2.schedulerAction = σ.schedulerAction := by + unfold tcbSchedAction at hr + obtain ⟨d, -, hr⟩ := eqPres_mem (threadGet_eqPres _ t) hr + obtain ⟨pr, -, hr⟩ := eqPres_mem (threadGet_eqPres _ t) hr + have hq : PresR (@Eq (State Unit)) (getTcbQueue (Z := Unit) d pr) := by unfold getTcbQueue; eqpres + obtain ⟨q, hq', hr⟩ := eqPres_mem hq hr + have eq : q = σ.readyQueues d pr := by + unfold getTcbQueue at hq' + obtain ⟨z, hz, hq'⟩ := mem_bindA.1 hq' + have ez : z = (σ.readyQueues, σ) := hz + subst ez + exact congrArg Prod.fst (show (q, σ) = (σ.readyQueues d pr, σ) from hq') + obtain ⟨q', hq'', hr⟩ := mem_bindA.1 hr + have eq' : q' = (action t q, σ) := hq'' + subst eq' + have er : r = ((), { σ with readyQueues := fun d' p => + if d' = d ∧ p = pr then action t q else σ.readyQueues d' p }) := hr + rw [er] + refine ⟨⟨rfl, fun d' p y hy => ?_⟩, rfl⟩ + simp only at hy + split_ifs at hy with h + · rcases hsub t q y hy with e | e + · exact Or.inr e + · rw [eq] at e; rw [h.1, h.2]; exact Or.inl e + · exact Or.inl hy + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + +/-- `yield_switches` on a schedulable, local, lower-priority bound thread: dequeue, enqueue, return now. -/ +theorem yieldSwitches_requeue_concA {s sd se : S} {sc t ct : U64} + (hT : L.mem s (scTcbA sc) = t) (hs : sched.is_schedulable st s t = ok true) + (hcore : L.mem s (scCoreA sc) = L.cpu s) (hlt : L.mem s (prioA t) < L.mem s (prioA ct)) + (ed : sched.dequeue st s t = ok sd) (ee : sched.enqueue st sd t = ok se) : + schedcontext.yield_switches st s sc ct = ok (false, se) := by + unfold schedcontext.yield_switches + rw [eq_of_spec_fd (obj.Sc.tcb.spec s sc), bind_ok, hT, hs, bind_ok] + simp only [↓reduceIte] + rw [eq_of_spec_fd (obj.Sc.cpu.spec s sc), bind_ok, hcore, L.cur_cpu_eq s, bind_ok] + simp only [bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + rw [eq_of_spec_fd (obj.Tcb.prio.spec s t), bind_ok, eq_of_spec_fd (obj.Tcb.prio.spec s ct), bind_ok] + simp only [hlt, ↓reduceIte] + rw [ed, bind_ok, ee, bind_ok] + +/-- `yield_to` with no previous yielder, a no-op `resume` and a `yield_switches` that returns now. -/ +theorem yieldTo_returnNow_concA {s1 sq : S} {a : A} {sc ct : U64} (call : Bool) + (hF : L.mem s1 (scYieldFromA sc) = 0#u64) (eres : schedcontext.resume st ar s1 a sc = ok (s1, a)) + (hcur : sched.cur_thread st s1 = ok ct) (hsw : schedcontext.yield_switches st s1 sc ct = ok (false, sq)) : + schedcontext.yield_to st ar s1 a sc call = (do + let s3 ← schedcontext.set_consumed st ar sq a sc ct call + let s4 ← sched.set_thread_state st s3 ct sched.TS_RUNNING + ok (s4, a)) := by + have hfrom0 : obj.Sc.yield_from st sc s1 = ok 0#u64 := by + unfold obj.Sc.yield_from layout.sc.YIELD_FROM + simp only [lift, bind_ok] + rw [show wa sc 56#u64 = scYieldFromA sc by unfold scYieldFromA layout.sc.YIELD_FROM; rfl, L.read_eq, hF] + unfold schedcontext.yield_to + rw [hfrom0, bind_ok, eq_of_spec_fd (obj.Tcb.is_null.spec _), bind_ok] + simp only [decide_true, ↓reduceIte, bind_ok, uncurry_apply_pair] + rw [eres, bind_ok] + simp only [uncurry_apply_pair] + rw [hcur, bind_ok, hsw, bind_ok] + simp only [uncurry_apply_pair, Bool.false_eq_true, ↓reduceIte] + +/-- The spec's `yield_to` in the same case: the requeue, then the consumed time at once. -/ +theorem yieldTo_returnNow_specA {σ σq σ2 : State Unit} {p : ObjRef} {y : SchedContext} {n : Nat} {d : List Data} + (hk : σ.kheap p = some (.schedContext y n)) (hf0 : y.scYieldFrom = none) + (hres : schedContextResume (Z := Unit) p σ = ({((), σ)}, False)) + (esw : yieldSwitchA p σ = ({(true, σq)}, False)) + (hret : returnConsumed (Z := Unit) p σq = ({(d, σ2)}, False)) : + invokeSchedContext (Z := Unit) (.invokeSchedContextYieldTo p) σ = ({(d, σ2)}, False) := by + change Fl4SpecA64.schedContextYieldTo (Z := Unit) p σ = _ + have assoc {α β γ : Type} (f : SMonad Unit α) (g : α → SMonad Unit β) (k : β → SMonad Unit γ) : + (f >>= g) >>= k = (f >>= fun x => g x >>= k) := funext (nd_bind_assocSc f g k) + have eq : schedContextYieldTo (Z := Unit) p = (do + completeScYielderA p + schedContextResume p + let b ← yieldSwitchA p + if b then returnConsumed p else pure []) := by + unfold schedContextYieldTo yieldSwitchA completeScYielderA + simp only [assoc] + have e0 : completeScYielderA p σ = ({((), σ)}, False) := by + unfold completeScYielderA + rw [nd_bind_oneSc (getScYieldFrom_eqA hk), hf0, Fl4Spec.NonDet.when, if_neg (by simp)] + rfl + rw [eq, nd_bind_oneSc e0, nd_bind_oneSc hres, nd_bind_oneSc esw] + exact hret + +variable {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + +/-- The spec's `yieldSwitchA` in the requeue case. -/ +theorem yieldSwitch_requeue_specA {σd σq : State Unit} {p t : ObjRef} {y : SchedContext} {n : Nat} {xt xc : Tcb} + (hk : σ.kheap p = some (.schedContext y n)) (htb : y.scTcb = some t) (hsched : schedulable t σ = true) + (htK : σ.kheap t = some (.tcb xt)) (hcK : σ.kheap σ.curThread = some (.tcb xc)) + (hlt : xt.tcbPriority < xc.tcbPriority) + (d : tcbSchedAction (Z := Unit) tcbSchedDequeue t σ = ({((), σd)}, False)) + (e : tcbSchedAction (Z := Unit) tcbSchedEnqueue t σd = ({((), σq)}, False)) : + yieldSwitchA p σ = ({(true, σq)}, False) := by + unfold yieldSwitchA + rw [nd_bind_oneSc (getScTcb_eqSc hk), htb, + nd_bind_oneSc (show Fl4Spec.NonDet.assertOpt (σ := State Unit) (some t) σ = ({(t, σ)}, False) from rfl), + nd_gets_bindSc, hsched] + simp only [↓reduceIte] + rw [nd_gets_bindSc, nd_bind_oneSc (threadGet_eqSc _ htK), nd_bind_oneSc (threadGet_eqSc _ hcK), if_pos hlt, + nd_bind_oneSc d, nd_bind_oneSc e] + rfl + +end + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {s : S} {a : A} {σ : State Unit} {m : McState} + +set_option maxHeartbeats 4000000 in +/-- **SchedContext YieldTo's requeue arm at a syscall entry** (module docstring). -/ +theorem JSyscallA.scYieldRequeueA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {slot lbl len buf sc sz : U64} {blk call don : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .SchedContext sc sz) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hlbl : lbl = fl4_abi.label.SchedContextYieldTo) + (ht0 : L.mem s (scTcbA sc) ≠ 0#u64) (hne : L.mem s (scTcbA sc) ≠ L.curW s) + (hy : L.mem s (yieldA (L.curW s)) = 0#u64) (hyf : L.mem s (scYieldFromA sc) = 0#u64) + (hon : L.mem s (affA (L.mem s (scTcbA sc))) = L.cpu s) (hbound : L.mem s (scA (L.mem s (scTcbA sc))) = sc) + (hprio : ¬ L.mem s (prioA (L.mem s (scTcbA sc))) > L.mem s (mcpA (L.curW s))) + (hlt : L.mem s (prioA (L.mem s (scTcbA sc))) < L.mem s (prioA (L.curW s))) + (hsch : ∀ xc, σ.kheap σ.curThread = some (.tcb xc) → + schedulable (specPtr (L.mem s (scTcbA sc))) (stUpdA σ σ.curThread xc .restart) = true) : + ∃ xc σ1 σ2 d σ3 s3, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + invokeSchedContext (Z := Unit) (.invokeSchedContextYieldTo (specPtr sc)) σ1 = ({(d, σ2)}, False) ∧ + getThreadState (Z := Unit) σ.curThread σ2 = ({(.restart, σ2)}, False) ∧ + ((), σ3) ∈ ((do + Fl4Spec.NonDet.when call (Fl4SpecA64.replyFromKernel (Z := Unit) σ.curThread (0, d)) + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .running) σ2).1 ∧ + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (.Ok (), s3, a) ∧ + ∃ Q Rl, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s3 a σ3 m ∧ Sched.BodyEndA σ σ3 m := by + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hslot hcov J.actW J.running J.cur + have hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM := J.toJStage3EntryA.curNotVmA + obtain ⟨xc, hkc, hst, hsp, J1, -, -, -⟩ := J.toJStage3A.of_restartCurA RS.pl.choose_spec.choose_spec.choose_spec.choose_spec + RS.sym RS.run hnvm RS.cur RS.vs RS.vc + have T0 := J.toJStage3A.toTA J.sact J.noSwitch + have hH : H sc = some (.sc sz.val) := (T0.capTy slot hslot).2.2.2.1 sc sz hcap + have hcH : sc ∈ H.scs := ⟨_, hH⟩ + have hc : sc ∈ Z.C := by rw [← J.scs]; exact hcH + have hci : specPtr sc ≠ m.idleScOf m.curCore := T0.ciSc slot hslot sc sz hcap + have hname : scNameA m sc = specPtr sc := by unfold scNameA; rw [if_neg hci] + have hC0 : sc ≠ 0#u64 := by + obtain ⟨-, -, hX, -, -⟩ := BXI.blk J.sys.ent.bxi + exact hX.1.sc0 sc hcH + obtain ⟨t, htd⟩ : ∃ t, t = L.mem s (scTcbA sc) := ⟨_, rfl⟩ + obtain ⟨c0, hc0d⟩ : ∃ c, c = L.curW s := ⟨_, rfl⟩ + rw [← htd] at ht0 hne hprio hon hbound hlt hsch + rw [← hc0d] at hne hprio hy hst J1 hlt + have ht : t ∈ Z.T := by + rw [htd] at ht0 ⊢; rw [← J.tcbs]; exact T0.blkA.2.2.1.2.2.yt.1 sc hcH ht0 + have htH : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + have hcZ : c0 ∈ Z.T := by rw [hc0d]; exact J.sr.curT + have hcH0 : c0 ∈ H.tcbs := by rw [J.tcbs]; exact hcZ + have hca : TcbAddr c0 := J.sr.core.tcbs _ hcZ + have hta : TcbAddr t := J.sr.core.tcbs t ht + have hstall : (if t != 0#u64 then smp.remote_tcb_stall st ar s a t else ok (s, a)) = ok (s, a) := by + rw [htd]; exact J.scStallA hCs h1 hslot hcap + obtain ⟨s1, hs1⟩ : ∃ q, q = L.wr s (stA c0) sched.TS_RESTART := ⟨_, rfl⟩ + obtain ⟨σ1, hσ1⟩ : ∃ q, q = stUpdA σ σ.curThread xc .restart := ⟨_, rfl⟩ + rw [← hs1] at hst J1 + rw [← hσ1] at hsp J1 + have sA : Aligned (stA c0) := by rw [stA_eq]; exact field_aligned hca (by simp) (by simp) + have farT : ∀ u : U64, TcbAddr u → ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → (u = c0 → K.val ≠ 3328) → + L.mem s1 (wa u K) = L.mem s (wa u K) := fun u hu K k1 k2 k3 => by + rw [hs1] + refine mem_wr_ne' sA (field_aligned hu k1 k2) ?_ + rw [stA_eq] + exact field_ne (K2 := K) hca hu (by simp) k1 (fun e => by simpa using Ne.symm (k3 e.symm)) + have farS : ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → L.mem s1 (wa t K) = L.mem s (wa t K) := + fun K k1 k2 => farT t hta K k1 k2 (fun e => absurd e hne) + -- the SC's words across the restart + obtain ⟨y0, n0, hk0, hy0⟩ := J.sr.scr.sc sc hc + obtain ⟨y1, n1, hk1, hy1⟩ := J1.sr.scr.sc sc hc + have hkk : σ1.kheap (scNameA m sc) = some (.schedContext y0 n0) := by + simp only [hσ1, stUpdA, setObjA] + rw [Function.update_of_ne (fun e => by rw [e, hkc] at hk0; cases hk0)]; exact hk0 + rw [hkk] at hk1 + cases hk1 + have optInj : ∀ u v : U64, optRefS u = optRefS v → u = v := fun u v h => by + by_cases hu : u = 0#u64 <;> by_cases hv : v = 0#u64 + · exact hu.trans hv.symm + · rw [hu, optRefS_zero, optRefS_ne hv] at h; cases h + · rw [hv, optRefS_zero, optRefS_ne hu] at h; cases h + · rw [optRefS_ne hu, optRefS_ne hv] at h + exact specPtr_inj (Option.some.inj h) + have hT1 : L.mem s1 (scTcbA sc) = t := by + apply optInj + rw [scTcbA_eqSc, ← hy1.tcb, hy0.tcb, ← scTcbA_eqSc, htd] + have a56 : scYieldFromA sc = scWA sc 56#u64 := by unfold scYieldFromA scWA layout.sc.YIELD_FROM; rfl + have hF1 : L.mem s1 (scYieldFromA sc) = 0#u64 := by + apply optInj + rw [a56, ← hy1.yieldFrom, hy0.yieldFrom, ← a56, hyf] + have hbound1 : L.mem s1 (scA t) = sc := (farS 3464#u64 (by simp) (by simp)).trans hbound + have hon1 : L.mem s1 (affA t) = L.cpu s1 := yieldAffKeepA J.sr J1.sr ht ht hon + have states1 : StEqA Z.T (L.mem s1) σ1 := fun u hu y hy => (J1.ep.at_tcb hu hy).st + have hsched : schedulable (specPtr t) σ1 = true := by rw [hσ1]; exact hsch xc hkc + have R1 := yield_resume_noopA (ar := ar) J1.sr states1 a hc hC0 ht hon1 hT1 hbound1 (hmb a) + obtain ⟨⟨s1', a'⟩, eres, ⟨er, hres⟩⟩ := exists_of_specA R1 + cases er + -- the target is schedulable at the words + have sdec := schedulable_eqA J1.sr.toSchedSRN ht (J1.sr.relOn ht hon1) states1 + have sw := of_decide_eq_true (sdec.trans hsched) + have hs : sched.is_schedulable st s1 t = ok true := by rw [is_schedulable_eqA, sdec, hsched] + have hrunt : Fl4ProofsA64.P1.Runnable (L.mem s1 (stA t)) := sw.1 + have hrel1 : (L.mem s1 (inqA t) &&& sched.Q_RELEASE) = 0#u64 := sw.2.2.2 + obtain ⟨hrunS, hnrS⟩ := schedulable_partsA hsched + have hcur1 : sched.cur_thread st s1 = ok c0 := + (eq_of_spec_fd (sched.cur_thread.spec s1)).trans + (congrArg ok (specPtr_inj (J1.j.1.sch.curThread.symm.trans + (show σ1.curThread = specPtr c0 by rw [hσ1, hc0d]; exact J.j.1.sch.curThread)))) + have hcurW1 : L.curW s1 = c0 := specPtr_inj (J1.j.1.sch.curThread.symm.trans + (show σ1.curThread = specPtr c0 by rw [hσ1, hc0d]; exact J.j.1.sch.curThread)) + -- core and priorities + have hbx1 := J1.sys.ent.bxi + have hcore : L.mem s1 (scCoreA sc) = L.cpu s1 := by + have affS : ScAff st H.tcbs s1 := hbx1.2.2.1.2.2.aff + have e := affS.core_eq htH (fun h => hC0 (hbound1.symm.trans h)) + rw [hbound1] at e + exact e.symm.trans hon1 + have ccur : σ1.curThread = specPtr c0 := by rw [hσ1, hc0d]; exact J.j.1.sch.curThread + obtain ⟨xt, htK, -, -, htp⟩ := J1.sr.rest.prio t ht + obtain ⟨xc1, hcK, -, -, hcp⟩ := J1.sr.rest.prio c0 hcZ + have ptS : L.mem s1 (prioA t) = L.mem s (prioA t) := farS 3448#u64 (by simp) (by simp) + have pcS : L.mem s1 (prioA c0) = L.mem s (prioA c0) := farT c0 hca 3448#u64 (by simp) (by simp) (fun _ => by simp) + have hlt1 : L.mem s1 (prioA t) < L.mem s1 (prioA c0) := by rw [ptS, pcS]; exact hlt + have pri : L.mem s1 (prioA t) < L.mem s1 (prioA c0) ↔ xt.tcbPriority < xc1.tcbPriority := by + rw [UScalar.lt_equiv, BitVec.lt_def, ← htp, ← hcp] + have hltS : xt.tcbPriority < xc1.tcbPriority := pri.1 hlt1 + -- dequeue and enqueue + have Q1 := J1.yieldQueueA + obtain ⟨sd, ed, σd, Qd, d, kd, rd, Hd, Fd⟩ := exists_of_specA (yieldDequeueA Q1 ht hon1) + have hond : L.mem sd (affA t) = L.cpu sd := yieldAffKeepA Q1.sched Hd.sched ht ht hon1 + have hrund : ∀ x, σd.kheap (specPtr t) = some (.tcb x) → runnable x.tcbState = true := fun x hx => + hrunS x (kd ▸ hx) + have hnrd : specPtr t ∉ σd.releaseQueue := rd.symm ▸ hnrS + obtain ⟨se, ee, σq, Qq, en, kq, rq, He, Fe⟩ := exists_of_specA (yieldEnqueueA Hd ht hond hrund hnrd) + have F := Fd.trans Fe rfl rfl + have hsw := yieldSwitches_requeue_concA (ct := c0) hT1 hs hcore hlt1 ed ee + have dm : ((), σd) ∈ (tcbSchedAction (Z := Unit) tcbSchedDequeue (specPtr t) σ1).1 := by + rw [d]; exact Set.mem_singleton _ + have em : ((), σq) ∈ (tcbSchedAction (Z := Unit) tcbSchedEnqueue (specPtr t) σd).1 := by + rw [en]; exact Set.mem_singleton _ + have Sd := tcbSchedAction_shapeA tcbSchedDequeue + (fun _ _ _ h => Or.inr (List.mem_filter.1 h).1) (specPtr t) dm + have Se := tcbSchedAction_shapeA tcbSchedEnqueue (fun _ _ _ h => tcbSchedEnqueue_memA h) (specPtr t) em + have eqσ : σq = { σ1 with readyQueues := σq.readyQueues } := by + have e1 : σd = { σ1 with readyQueues := σd.readyQueues } := by + have h := Sd.1.1 + have ha : σd.schedulerAction = σ1.schedulerAction := Sd.2 + rw [ha] at h; exact h + have e2 : σq = { σd with readyQueues := σq.readyQueues } := by + have h := Se.1.1 + have ha : σq.schedulerAction = σd.schedulerAction := Se.2 + rw [ha] at h; exact h + exact e2.trans (by rw [e1]) + -- the spec invariants after the requeue + have sact1 : SActA σ1 := + ⟨fun u h => by rw [hσ1] at h ⊢; exact J.sact.f1 u h, + ⟨{ xc with tcbState := .restart }, by simp only [hσ1, stUpdA, setObjA, Function.update_self], + Or.inr (Or.inl rfl)⟩⟩ + have L1 : LoopInvA σ1 m n := ⟨J1.sinv, J1.sidle, sact1⟩ + have tne : specPtr t ≠ σ1.curThread := by rw [ccur]; exact fun e => hne (specPtr_inj e) + have ok1 : EnqOkA σ1 m n (specPtr t) := by + refine ⟨tne, fun c hc e => ?_, xt, htK, hrunS xt htK⟩ + obtain ⟨z, hz, hz'⟩ := L1.sidle.f3 c hc + rw [e, htK] at hz + have := KernelObject.tcb.inj (Option.some.inj hz) + subst this + have := hrunS xt htK + rw [hz'] at this + cases this + have Ld : LoopInvA σd m n := L1.queueE Sd.1 (fun y p => p ▸ ok1) + (fun u h => Or.inl (Sd.2.symm.trans h)) (fun h => Sd.2.symm.trans h) + have hcd : σd.curThread = σ1.curThread := congrArg (fun z => z.curThread) Sd.1.1 + have okd : EnqOkA σd m n (specPtr t) := ⟨hcd.symm ▸ ok1.1, ok1.2.1, by rw [kd]; exact ok1.2.2⟩ + have Lq : LoopInvA σq m n := Ld.queueE Se.1 (fun y p => p ▸ okd) + (fun u h => Or.inl (Se.2.symm.trans h)) (fun h => Se.2.symm.trans h) + have tgt : TgtA (specPtr t) σ1 := by + obtain ⟨a, b, -⟩ := schedulable_actA hsched + exact ⟨a, b⟩ + have svq : SchedValidA σq := by + have h2 := HT.deq (specPtr t) (TgtA (specPtr t)) (fun _ f h => h.rq f) σ1 ⟨Sched.SchedValidA.toFIA J1.schedValid, tgt⟩ _ dm + have h3 := HT.enq (specPtr t) (TgtA (specPtr t)) (fun _ f h => h.rq f) σd + ⟨h2.1, h2.2, h2.2.1, h2.2.2⟩ _ em + exact h3.1.1.toA h3.1.2 + -- the concrete invariant and the kept words + have hs1b := J1.sys.ent.bxi + have RSc := RS.cur + have hrmt : L.rmax s1 sc ≠ 0#u64 := by + have h := sw.2.2.1 + rw [hbound1] at h + intro e + rw [e] at h + exact absurd h (by simp) + have hAct : sc ∈ Ac := by + obtain ⟨-, -, -, -, hS⟩ := J1.sys.ent.bxi + exact (hS.act sc hcH).2 hrmt + have hcRun : Fl4ProofsA64.P1.Runnable (L.mem s1 (stA c0)) := by + rw [hs1, L.mem_wr _ _ _ _ sA sA, if_pos rfl] + exact Or.inr (Or.inl rfl) + have hsc0' : L.mem s1 (scA c0) = L.mem s (scA c0) := farT c0 hca 3464#u64 (by simp) (by simp) (fun _ => by simp) + have hiq0' : L.mem s1 (inqA c0) = L.mem s (inqA c0) := farT c0 hca 3592#u64 (by simp) (by simp) (fun _ => by simp) + have hcsc : L.mem s1 (scA c0) ≠ 0#u64 := by rw [hsc0', hc0d]; exact RSc.2.1 + have hcac : L.mem s1 (scA c0) ∈ Ac := by rw [hsc0', hc0d]; exact RSc.2.2 + have hciq : (L.mem s1 (inqA c0) &&& sched.Q_RELEASE) = 0#u64 := by rw [hiq0', hc0d]; exact RSc.1 + have blkq : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac se := by + have q := schedcontext.yield_switches.blkY (st := st) s1 sc c0 (BXI.blk J1.sys.ent.bxi) hcH hcH0 + (by rw [hT1]; exact ht0) (by rw [hT1]; exact hne) hcRun hcsc hcac hciq + rw [hsw, WP.spec_ok] at q + exact q.1 + have Bd := sched.dequeue.blkY (st := st) s1 t (BXI.blk J1.sys.ent.bxi) htH + rw [ed, WP.spec_ok] at Bd + obtain ⟨bkd, -, relK, kw1⟩ := Bd + have hsct : L.mem sd (scA t) = sc := (kw1 t htH).2.1.trans hbound1 + have Be := sched.enqueue.blkY (st := st) sd t bkd htH ((kw1 t htH).1.symm ▸ hrunt) + (by rw [hsct]; exact hC0) (by rw [hsct]; exact hAct) (relK hrel1) + rw [ee, WP.spec_ok] at Be + obtain ⟨-, kw2⟩ := Be + have hscq : ∀ u ∈ Z.T, L.mem se (scA u) = L.mem s1 (scA u) := fun u hu => by + have huH : u ∈ H.tcbs := by rw [J.tcbs]; exact hu + exact (kw2 u huH).2.1.trans (kw1 u huH).2.1 + have hiqq : L.mem se (inqA c0) = L.mem s1 (inqA c0) := + (kw2 c0 hcH0).2.2 (Ne.symm hne) |>.trans ((kw1 c0 hcH0).2.2 (Ne.symm hne)) + -- the reply + have htSc : obj.Sc.tcb st sc s = ok t := by rw [htd]; exact eq_of_spec_fd (P1.obj.Sc.tcb.spec s sc) + have hct : sched.cur_thread st s = ok c0 := by rw [hc0d]; exact cur_thread_ok s + have hyt := yieldTo_returnNow_concA (ar := ar) call hF1 eres hcur1 hsw + have hprog : ∀ c su, schedcontext.update_consumed st ar se a sc = ok (c, su) → + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + (do let s4 ← vspace.reply1 st su c0 call c; ok (core.result.Result.Ok (), s4, a)) := fun c su hu => by + rw [hcap, decodeSc_programA, if_neg Bool.false_ne_true, hlbl, + scDecodeYield_eqA htSc hstall hct ht0 hne hprio hy hst, hyt] + unfold schedcontext.set_consumed vspace.reply1 + rw [hu, bind_ok] + cases call <;> simp [Std.bind_assoc] <;> rfl + have FW : WritesIn st s1 se (MWrA (st := st) Z m σ1 s1) := F.wr + obtain ⟨c, su, σ2, σ3, s3, hu, hret, hrun, e3, Q, Rl, J2⟩ := + J.consumedMidA hA hmax htu hslot hcov hcap hext call hkc (sq := se) (σq := σq) (Q' := Qq) + He (by rw [← hc0d, ← hs1, ← hσ1]; exact FW) (by rw [← hσ1]; exact eqσ) + (by rw [← hc0d, ← hs1]; exact hscq) (by rw [← hc0d, ← hs1]; exact hiqq) Lq svq blkq + (fun c su hu s' h2 σX => by + rw [← hc0d] at h2 + have hd : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (core.result.Result.Ok (), s', a) := by rw [hprog c su hu, h2, bind_ok] + obtain ⟨-, -, sysl, symL, -, userOK, j7⟩ := J.decodeFinA hA hM hPl hfm hslot hcov + (rfl : capA st s slot = capA st s slot) hext hSC (lbl := lbl) + (by rw [hcap, hlbl]; simp [DecArmS1aEnt, DecArmQH, DecArmQZ2, DecArmKQ2, DecArmKQ, DecArmKQ3, DecArmDelQ, + DecArmS1aC, capFam]) + (fun _ h => by rw [hcap] at h; cases h) (fun _ h => by rw [hcap] at h; cases h) hd σX m + exact ⟨sysl, symL, userOK, j7⟩) + rw [← hc0d] at e3 + have hf0 : y0.scYieldFrom = none := by rw [hy0.yieldFrom, ← a56, hyf, optRefS_zero] + have htb : y0.scTcb = some (specPtr t) := by rw [hy0.tcb, ← scTcbA_eqSc, ← htd, optRefS_ne ht0] + have esw := yieldSwitch_requeue_specA (p := scNameA m sc) hkk htb hsched htK (ccur ▸ hcK) hltS d en + have hst2 : getThreadState (Z := Unit) σ.curThread σ2 = ({(.restart, σ2)}, False) := by + have hkq : σq.kheap = σ1.kheap := by rw [eqσ] + exact returnConsumed_stateA (σ := σq) (p := scNameA m sc) (hkq ▸ hkk) (x := { xc with tcbState := .restart }) + (by rw [hkq, hσ1]; simp only [stUpdA, setObjA, Function.update_self]) rfl hret + refine ⟨xc, σ1, σ2, wordsFromTime c.bv, σ3, s3, hkc, hσ1, hsp, ?_, hst2, hrun, ?_, Q, Rl, J2⟩ + · rw [← hname] + exact yieldTo_returnNow_specA hkk hf0 hres esw hret + · rw [hprog c su hu, e3, bind_ok] + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3ScYieldSwA.lean b/proofs/Fl4ProofsA64/P3/Stage3ScYieldSwA.lean new file mode 100644 index 000000000..5eb2b14d4 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ScYieldSwA.lean @@ -0,0 +1,700 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 3): **SchedContext YieldTo, the switch arm, at the record**. + +`decode_invocation` on a SchedContext cap with the YieldTo label at a syscall entry (`JSyscallA.scYieldSwitchRecA`), +for a bound schedulable thread of priority not below the caller's: no previous yielder, `resume` writes nothing, +`yield_switches` installs the yield links, dequeues the target, enqueues the caller and the target and sets +`chooseNewThread`; the caller stays `Restart` (its reply is `complete_yield_to`'s, not lifted). The spec invariants: +`RefK` for the links, `LoopInvA.queueC` for the queue steps (the caller is enqueued; the final action is +`chooseNewThread`) and `HT.enq` / `resched_fia` for `SchedValidA`. + +Named premises: +* [P] `ArchTotal`, `MaxSpi`, `ArchPlat`, `A64FrameMasks`, `hCs`, `hmb`; +* [J] `hbound` (the bound thread's SC is this SC); +* [S] `h1` (`numNodes = 1`), `hyf` (no previous yielder), `hon` (the target is on this core), `hsch` (the target is + schedulable), `hnlt` (its priority is not below the caller's). + +The decode succeeds; the `Err` returns are not lifted. New module. +-/ +import Fl4ProofsA64.P3.Stage3ScYieldRqA +import Fl4ProofsA64.P3.Stage3TcbBodyA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-- `LoopInvA` across a ready-queue change ending in `chooseNewThread`: the queued threads may include the current one +(`ctQ` is guarded by `resume_cur_thread`). -/ +theorem LoopInvA.queueC {P : ObjRef → Prop} {σ σ' : State Unit} {m : McState} {n : Nat} (h : LoopInvA σ m n) + (Q : QueueE P σ σ') (hA : σ'.schedulerAction = .chooseNewThread) + (hP : ∀ y, P y → (∀ c < n, m.idleThreadOf c ≠ y) ∧ ∃ x, σ.kheap y = some (.tcb x) ∧ runnable x.tcbState = true) : + LoopInvA σ' m n := by + have e := Q.1 + have hk : σ'.kheap = σ.kheap := by rw [e] + have hcur : σ'.curThread = σ.curThread := by rw [e] + have hidle : σ'.idleThread = σ.idleThread := by rw [e] + have hcsc : σ'.curSc = σ.curSc := by rw [e] + have hrls : σ'.releaseQueue = σ.releaseQueue := by rw [e] + have hnode : σ'.interruptIrqNode = σ.interruptIrqNode := by rw [e] + have hct : ∀ k p, capTableAt k p σ' ↔ capTableAt k p σ := fun k p => by + unfold capTableAt; rw [hk] + have S := h.sinv + refine + { sinv := + { mc := S.mc + view := by unfold McView; rw [hidle]; exact S.view + curTcb := by rw [hk, hcur]; exact S.curTcb + idleTcb := by rw [hk, hidle]; exact S.idleTcb + onlyIdle := by rw [hk]; exact S.onlyIdle + idleSc := by rw [hk, hidle]; exact S.idleSc + curSc := by rw [hk, hcsc]; exact S.curSc + ctQ := fun hres => by rw [hA] at hres; cases hres + idleQ := fun d p c hc hm => by + rcases Q.2 d p _ hm with a | a + · exact S.idleQ d p c hc a + · exact (hP _ a).1 c hc rfl + rq := fun d p t hm => by + rw [hk] + rcases Q.2 d p t hm with a | a + · exact S.rq d p t a + · exact (hP _ a).2 + rls := fun t ht => by rw [hrls] at ht; rw [hk]; exact S.rls t ht + rpos := by rw [hk]; exact S.rpos + irqN := by + obtain ⟨⟨⟨hinj, hcn⟩, hdom⟩, hsl, href⟩ := S.irqN + refine ⟨⟨⟨?_, fun irq hi => ?_⟩, fun irq hi k hk' => ?_⟩, fun irq hi o b c ho hc => ?_, + fun p o b c ho hc => ?_⟩ + · rw [hnode]; exact hinj + · rw [hnode]; exact (hct _ _).2 (hcn irq hi) + · rw [hnode] at hk'; exact hdom irq hi k ((hct _ _).1 hk') + · rw [hk, hnode] at ho; exact hsl irq hi o b c ho hc + · rw [hnode]; rw [hk] at ho; exact href p o b c ho hc + zc := by + have hcons : σ'.consumedTime = σ.consumedTime := by rw [e] + unfold ZeroConsA; rw [hk, hcsc, hrls, hcons]; exact S.zc + rlsRun := by unfold RlsRunA; rw [hrls, hk]; exact S.rlsRun + curScReady := by + have hcons : σ'.consumedTime = σ.consumedTime := by rw [e] + have htime : σ'.curTime = σ.curTime := by rw [e] + unfold CurRdyA; rw [hk, hcsc, hcons, htime]; exact S.curScReady + faultSt := by unfold FaultStOkA; rw [hk]; exact S.faultSt } + sidle := h.sidle.congr hk hidle (by rw [show σ'.curTime = σ.curTime by rw [e]]; exact Nat.le_refl _) + sact := + { f1 := fun u eu => by rw [hA] at eu; cases eu + f2 := by rw [hk, hcur]; exact h.sact.f2 } } + +private theorem yieldInstallSw_specA {σ : State Unit} {p q : ObjRef} {x : Tcb} {y : SchedContext} {n : Nat} + (hk : σ.kheap q = some (.tcb x)) (hs : σ.kheap p = some (.schedContext y n)) : + (do + setTcbObjRef (Z := Unit) tcbYieldToUpdate q (some p) + setScObjRef scYieldFromUpdate p (some q)) σ = ({((), yieldPairStA σ p q x y n true)}, False) := by + have hpq : p ≠ q := fun e => by rw [e, hk] at hs; cases hs + let x' := {x with tcbYieldTo := some p} + let y' := {y with scYieldFrom := some q} + have hs' : (updKSc σ q (.tcb x')).kheap p = some (.schedContext y n) := by + rw [updKSc_kheap, Function.update_of_ne hpq] + exact hs + change (threadSet _ q >>= fun _ => updateSchedContext p _) σ = _ + rw [nd_bind_oneSc (threadSet_eqSc _ hk)] + change updateSchedContext p (fun z => {z with scYieldFrom := some q}) (updKSc σ q (.tcb x')) = _ + rw [Sched.updateSchedContext_eqA hs'] + have eq : updKSc (updKSc σ q (.tcb x')) p (.schedContext y' n) = + updKSc (updKSc σ p (.schedContext y' n)) q (.tcb x') := by + unfold updKSc + rw [Function.update_comm hpq.symm] + rw [eq] + rfl + +/-- The steps of the spec's switch arm, as states. -/ +theorem yieldSwitch_stepsA {σ σq : State Unit} {p t ct : ObjRef} {y : SchedContext} {n : Nat} {xt xc : Tcb} {b : Bool} + (hk : σ.kheap p = some (.schedContext y n)) (htb : y.scTcb = some t) (hsched : schedulable t σ = true) + (hct : σ.curThread = ct) (htK : σ.kheap t = some (.tcb xt)) (hcK : σ.kheap ct = some (.tcb xc)) + (hnlt : ¬ xt.tcbPriority < xc.tcbPriority) (h : yieldSwitchA p σ = ({(b, σq)}, False)) : + ∃ σd σe σf, ((), σd) ∈ (tcbSchedAction (Z := Unit) tcbSchedDequeue t (yieldPairStA σ p ct xc y n true)).1 ∧ + ((), σe) ∈ (tcbSchedAction (Z := Unit) tcbSchedEnqueue ct σd).1 ∧ + ((), σf) ∈ (tcbSchedAction (Z := Unit) tcbSchedEnqueue t σe).1 ∧ + ((), σq) ∈ (rescheduleRequired (Z := Unit) σf).1 ∧ b = false := by + have e : yieldSwitchA p σ = (tcbSchedAction (Z := Unit) tcbSchedDequeue t >>= fun _ => + tcbSchedAction tcbSchedEnqueue ct >>= fun _ => tcbSchedAction tcbSchedEnqueue t >>= fun _ => + rescheduleRequired >>= fun _ => pure false) (yieldPairStA σ p ct xc y n true) := by + have assoc {α β γ : Type} (f : SMonad Unit α) (g : α → SMonad Unit β) (k : β → SMonad Unit γ) : + (f >>= g) >>= k = (f >>= fun x => g x >>= k) := funext (nd_bind_assocSc f g k) + unfold yieldSwitchA + rw [nd_bind_oneSc (getScTcb_eqSc hk), htb, + nd_bind_oneSc (show Fl4Spec.NonDet.assertOpt (σ := State Unit) (some t) σ = ({(t, σ)}, False) from rfl), + nd_gets_bindSc, hsched] + simp only [↓reduceIte] + rw [nd_gets_bindSc, hct, nd_bind_oneSc (threadGet_eqSc _ htK), nd_bind_oneSc (threadGet_eqSc _ hcK), + if_neg hnlt, ← assoc, nd_bind_oneSc (yieldInstallSw_specA hcK hk)] + have hm : (b, σq) ∈ (yieldSwitchA p σ).1 := by rw [h]; exact Set.mem_singleton _ + rw [e] at hm + obtain ⟨⟨u1, σd⟩, h1, hm⟩ := mem_bindA.1 hm + obtain ⟨⟨u2, σe⟩, h2, hm⟩ := mem_bindA.1 hm + obtain ⟨⟨u3, σf⟩, h3, hm⟩ := mem_bindA.1 hm + obtain ⟨⟨u4, σg⟩, h4, hm⟩ := mem_bindA.1 hm + have em : (b, σq) = (false, σg) := hm + obtain ⟨eb, es⟩ := Prod.mk.inj em + subst es + exact ⟨σd, σe, σf, h1, h2, h3, h4, eb⟩ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + +/-- `yield_to` with no previous yielder, a no-op `resume` and a switching `yield_switches`. -/ +theorem yieldTo_switch_concA {s1 sq : S} {a : A} {sc ct : U64} (call : Bool) + (hF : L.mem s1 (scYieldFromA sc) = 0#u64) (eres : schedcontext.resume st ar s1 a sc = ok (s1, a)) + (hcur : sched.cur_thread st s1 = ok ct) (hsw : schedcontext.yield_switches st s1 sc ct = ok (true, sq)) : + schedcontext.yield_to st ar s1 a sc call = ok (sq, a) := by + have hfrom0 : obj.Sc.yield_from st sc s1 = ok 0#u64 := by + unfold obj.Sc.yield_from layout.sc.YIELD_FROM + simp only [lift, bind_ok] + rw [show wa sc 56#u64 = scYieldFromA sc by unfold scYieldFromA layout.sc.YIELD_FROM; rfl, L.read_eq, hF] + unfold schedcontext.yield_to + rw [hfrom0, bind_ok, eq_of_spec_fd (obj.Tcb.is_null.spec _), bind_ok] + simp only [decide_true, ↓reduceIte, bind_ok, uncurry_apply_pair] + rw [eres, bind_ok] + simp only [uncurry_apply_pair] + rw [hcur, bind_ok, hsw, bind_ok] + simp only [uncurry_apply_pair, ↓reduceIte] + +end + +/-- The spec's `yield_to` when `yieldSwitchA` switches: no reply. -/ +theorem yieldTo_noReturn_specA {σ σq : State Unit} {p : ObjRef} {y : SchedContext} {n : Nat} + (hk : σ.kheap p = some (.schedContext y n)) (hf0 : y.scYieldFrom = none) + (hres : schedContextResume (Z := Unit) p σ = ({((), σ)}, False)) + (esw : yieldSwitchA p σ = ({(!true, σq)}, False)) : + invokeSchedContext (Z := Unit) (.invokeSchedContextYieldTo p) σ = ({([], σq)}, False) := by + change Fl4SpecA64.schedContextYieldTo (Z := Unit) p σ = _ + have assoc {α β γ : Type} (f : SMonad Unit α) (g : α → SMonad Unit β) (k : β → SMonad Unit γ) : + (f >>= g) >>= k = (f >>= fun x => g x >>= k) := funext (nd_bind_assocSc f g k) + have eq : schedContextYieldTo (Z := Unit) p = (do + completeScYielderA p + schedContextResume p + let b ← yieldSwitchA p + if b then returnConsumed p else pure []) := by + unfold schedContextYieldTo yieldSwitchA completeScYielderA + simp only [assoc] + have e0 : completeScYielderA p σ = ({((), σ)}, False) := by + unfold completeScYielderA + rw [nd_bind_oneSc (getScYieldFrom_eqA hk), hf0, Fl4Spec.NonDet.when, if_neg (by simp)] + rfl + rw [eq, nd_bind_oneSc e0, nd_bind_oneSc hres, nd_bind_oneSc esw] + rfl + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {s : S} {a : A} {σ : State Unit} {m : McState} + +set_option maxHeartbeats 4000000 in +/-- **SchedContext YieldTo's switch arm at a syscall entry** (module docstring). -/ +theorem JSyscallA.scYieldSwitchRecA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + {slot lbl len buf sc sz : U64} {blk call don : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .SchedContext sc sz) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hlbl : lbl = fl4_abi.label.SchedContextYieldTo) + (ht0 : L.mem s (scTcbA sc) ≠ 0#u64) (hne : L.mem s (scTcbA sc) ≠ L.curW s) + (hy : L.mem s (yieldA (L.curW s)) = 0#u64) (hyf : L.mem s (scYieldFromA sc) = 0#u64) + (hon : L.mem s (affA (L.mem s (scTcbA sc))) = L.cpu s) (hbound : L.mem s (scA (L.mem s (scTcbA sc))) = sc) + (hprio : ¬ L.mem s (prioA (L.mem s (scTcbA sc))) > L.mem s (mcpA (L.curW s))) + (hnlt : ¬ L.mem s (prioA (L.mem s (scTcbA sc))) < L.mem s (prioA (L.curW s))) + (hsch : ∀ xc, σ.kheap σ.curThread = some (.tcb xc) → + schedulable (specPtr (L.mem s (scTcbA sc))) (stUpdA σ σ.curThread xc .restart) = true) : + ∃ xc σ1 σ2 s2, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + invokeSchedContext (Z := Unit) (.invokeSchedContextYieldTo (specPtr sc)) σ1 = ({([], σ2)}, False) ∧ + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (.Ok (), s2, a) ∧ + ∃ Q Rl, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s2 a σ2 m ∧ Sched.BodyEndA σ σ2 m := by + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hslot hcov J.actW J.running J.cur + have hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM := J.toJStage3EntryA.curNotVmA + obtain ⟨xc, hkc, hst, hsp, J1, -, -, -⟩ := J.toJStage3A.of_restartCurA RS.pl.choose_spec.choose_spec.choose_spec.choose_spec + RS.sym RS.run hnvm RS.cur RS.vs RS.vc + have T0 := J.toJStage3A.toTA J.sact J.noSwitch + have hH : H sc = some (.sc sz.val) := (T0.capTy slot hslot).2.2.2.1 sc sz hcap + have hcH : sc ∈ H.scs := ⟨_, hH⟩ + have hc : sc ∈ Z.C := by rw [← J.scs]; exact hcH + have hci : specPtr sc ≠ m.idleScOf m.curCore := T0.ciSc slot hslot sc sz hcap + have hname : scNameA m sc = specPtr sc := by unfold scNameA; rw [if_neg hci] + have hC0 : sc ≠ 0#u64 := by + obtain ⟨-, -, hX, -, -⟩ := BXI.blk J.sys.ent.bxi + exact hX.1.sc0 sc hcH + have hCz : ∀ c ∈ Z.C, c ≠ 0#u64 := J.scNZ + obtain ⟨t, htd⟩ : ∃ t, t = L.mem s (scTcbA sc) := ⟨_, rfl⟩ + obtain ⟨c0, hc0d⟩ : ∃ c, c = L.curW s := ⟨_, rfl⟩ + rw [← htd] at ht0 hne hprio hon hbound hnlt hsch + rw [← hc0d] at hne hprio hy hst J1 hnlt + have ht : t ∈ Z.T := by + rw [htd] at ht0 ⊢; rw [← J.tcbs]; exact T0.blkA.2.2.1.2.2.yt.1 sc hcH ht0 + have htH : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + have hcZ : c0 ∈ Z.T := by rw [hc0d]; exact J.sr.curT + have hcH0 : c0 ∈ H.tcbs := by rw [J.tcbs]; exact hcZ + have hca : TcbAddr c0 := J.sr.core.tcbs _ hcZ + have hta : TcbAddr t := J.sr.core.tcbs t ht + have hstall : (if t != 0#u64 then smp.remote_tcb_stall st ar s a t else ok (s, a)) = ok (s, a) := by + rw [htd]; exact J.scStallA hCs h1 hslot hcap + obtain ⟨s1, hs1⟩ : ∃ q, q = L.wr s (stA c0) sched.TS_RESTART := ⟨_, rfl⟩ + obtain ⟨σ1, hσ1⟩ : ∃ q, q = stUpdA σ σ.curThread xc .restart := ⟨_, rfl⟩ + rw [← hs1] at hst J1 + rw [← hσ1] at hsp J1 + have sA : Aligned (stA c0) := by rw [stA_eq]; exact field_aligned hca (by simp) (by simp) + have farT : ∀ u : U64, TcbAddr u → ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → (u = c0 → K.val ≠ 3328) → + L.mem s1 (wa u K) = L.mem s (wa u K) := fun u hu K k1 k2 k3 => by + rw [hs1] + refine mem_wr_ne' sA (field_aligned hu k1 k2) ?_ + rw [stA_eq] + exact field_ne (K2 := K) hca hu (by simp) k1 (fun e => by simpa using Ne.symm (k3 e.symm)) + have farS : ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → L.mem s1 (wa t K) = L.mem s (wa t K) := + fun K k1 k2 => farT t hta K k1 k2 (fun e => absurd e hne) + obtain ⟨y0, n0, hk0, hy0⟩ := J.sr.scr.sc sc hc + obtain ⟨y1, n1, hk1, hy1⟩ := J1.sr.scr.sc sc hc + have hkk : σ1.kheap (scNameA m sc) = some (.schedContext y0 n0) := by + simp only [hσ1, stUpdA, setObjA] + rw [Function.update_of_ne (fun e => by rw [e, hkc] at hk0; cases hk0)]; exact hk0 + rw [hkk] at hk1 + cases hk1 + have optInj : ∀ u v : U64, optRefS u = optRefS v → u = v := fun u v h => by + by_cases hu : u = 0#u64 <;> by_cases hv : v = 0#u64 + · exact hu.trans hv.symm + · rw [hu, optRefS_zero, optRefS_ne hv] at h; cases h + · rw [hv, optRefS_zero, optRefS_ne hu] at h; cases h + · rw [optRefS_ne hu, optRefS_ne hv] at h + exact specPtr_inj (Option.some.inj h) + have hT1 : L.mem s1 (scTcbA sc) = t := by + apply optInj + rw [scTcbA_eqSc, ← hy1.tcb, hy0.tcb, ← scTcbA_eqSc, htd] + have a56 : scYieldFromA sc = scWA sc 56#u64 := by unfold scYieldFromA scWA layout.sc.YIELD_FROM; rfl + have hF1 : L.mem s1 (scYieldFromA sc) = 0#u64 := by + apply optInj + rw [a56, ← hy1.yieldFrom, hy0.yieldFrom, ← a56, hyf] + have hbound1 : L.mem s1 (scA t) = sc := (farS 3464#u64 (by simp) (by simp)).trans hbound + have hy1' : L.mem s1 (yieldA c0) = 0#u64 := + (farT c0 hca 3472#u64 (by simp) (by simp) (fun _ => by simp)).trans hy + have hon1 : L.mem s1 (affA t) = L.cpu s1 := yieldAffKeepA J.sr J1.sr ht ht hon + have states1 : StEqA Z.T (L.mem s1) σ1 := fun u hu y hy => (J1.ep.at_tcb hu hy).st + have hsched : schedulable (specPtr t) σ1 = true := by rw [hσ1]; exact hsch xc hkc + have R1 := yield_resume_noopA (ar := ar) J1.sr states1 a hc hC0 ht hon1 hT1 hbound1 (hmb a) + obtain ⟨⟨s1', a'⟩, eres, ⟨er, hres⟩⟩ := exists_of_specA R1 + cases er + obtain ⟨hrunS, hnrS⟩ := schedulable_partsA hsched + have ccur : σ1.curThread = specPtr c0 := by rw [hσ1, hc0d]; exact J.j.1.sch.curThread + have hcurW1 : L.curW s1 = c0 := specPtr_inj (J1.j.1.sch.curThread.symm.trans ccur) + have hcur1 : sched.cur_thread st s1 = ok c0 := + (eq_of_spec_fd (sched.cur_thread.spec s1)).trans (congrArg ok hcurW1) + have hbx1 := J1.sys.ent.bxi + have hcore : L.mem s1 (scCoreA sc) = L.cpu s1 := by + have affS : ScAff st H.tcbs s1 := hbx1.2.2.1.2.2.aff + have e := affS.core_eq htH (fun h => hC0 (hbound1.symm.trans h)) + rw [hbound1] at e + exact e.symm.trans hon1 + -- the caller's facts + have sact1 : SActA σ1 := + ⟨fun u h => by rw [hσ1] at h ⊢; exact J.sact.f1 u h, + ⟨{ xc with tcbState := .restart }, by simp only [hσ1, stUpdA, setObjA, Function.update_self], + Or.inr (Or.inl rfl)⟩⟩ + have hcK : σ1.kheap (specPtr c0) = some (.tcb { xc with tcbState := .restart }) := by + rw [← ccur]; simp only [hσ1, stUpdA, setObjA, Function.update_self] + obtain ⟨xt, htK, -, -, htp⟩ := J1.sr.rest.prio t ht + obtain ⟨xc1, hcK', -, -, hcp⟩ := J1.sr.rest.prio c0 hcZ + rw [hcK] at hcK' + cases hcK' + have ptS : L.mem s1 (prioA t) = L.mem s (prioA t) := farS 3448#u64 (by simp) (by simp) + have pcS : L.mem s1 (prioA c0) = L.mem s (prioA c0) := farT c0 hca 3448#u64 (by simp) (by simp) (fun _ => by simp) + have pri : L.mem s1 (prioA t) < L.mem s1 (prioA c0) ↔ xt.tcbPriority < xc.tcbPriority := by + rw [UScalar.lt_equiv, BitVec.lt_def, ← htp, ← hcp] + have hnltS : ¬ xt.tcbPriority < xc.tcbPriority := fun h => hnlt (by rw [← ptS, ← pcS]; exact pri.2 h) + have hrc : ∀ x, σ1.kheap (specPtr c0) = some (.tcb x) → runnable x.tcbState = true := fun x hx => by + rw [hcK] at hx; cases hx; rfl + have hncr : specPtr c0 ∉ σ1.releaseQueue := by + have RSc := RS.cur + have hiq0 : (L.mem s1 (inqA c0) &&& sched.Q_RELEASE) = 0#u64 := by + rw [show inqA c0 = wa c0 3592#u64 from rfl, farT c0 hca 3592#u64 (by simp) (by simp) (fun _ => by simp), hc0d] + exact RSc.1 + have hon0 : L.mem s1 (affA c0) = L.cpu s1 := by rw [← hcurW1]; exact J1.sr.curOn + exact (J1.sr.relOn hcZ hon0).1 hiq0 + -- the switch + have Q1 := J1.yieldQueueA + obtain ⟨⟨rb, sq⟩, esw, σq, Q', hspecSw, Hq, szq, -, wq⟩ := exists_of_specA + (yield_switches_localA (st := st) (sc := sc) (t := t) (ct := c0) Q1 hc ht hcZ hne hCz hcore hon1 hcurW1 hT1 + hy1' hF1 hrc hncr) + simp only at hspecSw Hq wq + have htb : y0.scTcb = some (specPtr t) := by rw [hy0.tcb, ← scTcbA_eqSc, ← htd, optRefS_ne ht0] + have hf0 : y0.scYieldFrom = none := by rw [hy0.yieldFrom, ← a56, hyf, optRefS_zero] + obtain ⟨σd, σe, σf, md, me, mf, mr, eb⟩ := + yieldSwitch_stepsA hkk htb hsched ccur htK hcK hnltS hspecSw + have erb : rb = true := by cases rb <;> simp at eb ⊢ + subst erb + -- the spec states + set xr : Tcb := { xc with tcbState := .restart } with hxr + set σi := yieldPairStA σ1 (scNameA m sc) (specPtr c0) xr y0 n0 true with hσi + have hpc : scNameA m sc ≠ specPtr c0 := fun e => by rw [e, hcK] at hkk; cases hkk + set xi : Tcb := { xr with tcbYieldTo := some (scNameA m sc) } with hxi + set yi : SchedContext := { y0 with scYieldFrom := some (specPtr c0) } with hyi + have kiK : ∀ q, σi.kheap q = if q = specPtr c0 then some (.tcb xi) + else if q = scNameA m sc then some (.schedContext yi n0) else σ1.kheap q := fun q => by + simp only [hσi, yieldPairStA, updKSc_kheap, Function.update_apply, ↓reduceIte, hxi, hyi] + -- the spec invariants + have Ri : RefK σ1 σi := by + refine ⟨rfl, fun q => ?_, fun hr => ?_⟩ + swap + · refine ⟨fun y k h => ?_, fun hc y k h a => ?_⟩ + · rw [kiK] at h + split_ifs at h with e1 e2 + · cases h + · cases h; exact hr.1 y0 n0 (by rw [e2]; exact hkk) + · exact hr.1 y k h + · rw [kiK] at h + split_ifs at h with e1 e2 + · cases h + · cases h; exact hr.2 hc y0 n0 (by show σ1.kheap σi.curSc = _; rw [e2]; exact hkk) a + · exact hr.2 hc y k h a + rw [kiK] + by_cases e1 : q = specPtr c0 + · rw [if_pos e1, e1, hcK]; exact .tcb _ _ ⟨rfl, rfl, rfl, rfl, Or.inl rfl⟩ + rw [if_neg e1] + by_cases e2 : q = scNameA m sc + · rw [if_pos e2, e2, hkk]; exact .sc _ _ _ ⟨rfl, rfl, rfl, id⟩ + rw [if_neg e2]; exact .same _ + have L1 : LoopInvA σ1 m n := ⟨J1.sinv, J1.sidle, sact1⟩ + have Li := L1.refK Ri + have Sd := tcbSchedAction_shapeA tcbSchedDequeue (fun _ _ _ h => Or.inr (List.mem_filter.1 h).1) (specPtr t) md + have Se := tcbSchedAction_shapeA tcbSchedEnqueue (fun _ _ _ h => tcbSchedEnqueue_memA h) (specPtr c0) me + have Sf := tcbSchedAction_shapeA tcbSchedEnqueue (fun _ _ _ h => tcbSchedEnqueue_memA h) (specPtr t) mf + have Sr := rescheduleRequired_memA mr + have af : σf.schedulerAction = σ.schedulerAction := by + rw [show σf.schedulerAction = σe.schedulerAction from Sf.2, show σe.schedulerAction = σd.schedulerAction from Se.2, + show σd.schedulerAction = σi.schedulerAction from Sd.2, hσi, hσ1] + rfl + have Qall : QueueE (fun y => y = specPtr t ∨ y = specPtr c0) σi σq := + ((Sd.1.mono fun _ h => Or.inl h).trans (Se.1.mono fun _ h => Or.inr h)).trans + ((Sf.1.mono fun _ h => Or.inl h).trans (Sr.1.mono fun y p => absurd (af ▸ p.1) (J.noSwitch y))) + have tc : specPtr t ≠ specPtr c0 := fun e => hne (specPtr_inj e) + have tp : specPtr t ≠ scNameA m sc := fun e => by rw [e, hkk] at htK; cases htK + have kit : σi.kheap (specPtr t) = some (.tcb xt) := by rw [kiK, if_neg tc, if_neg tp]; exact htK + have kic : σi.kheap (specPtr c0) = some (.tcb xi) := by rw [kiK, if_pos rfl] + have rxt : runnable xt.tcbState = true := hrunS xt htK + have Lq : LoopInvA σq m n := by + refine Li.queueC Qall Sr.2 fun y hy => ?_ + have fin : ∀ z, σi.kheap y = some (.tcb z) → runnable z.tcbState = true → + (∀ c < n, m.idleThreadOf c ≠ y) ∧ ∃ x, σi.kheap y = some (.tcb x) ∧ runnable x.tcbState = true := by + refine fun z hz rz => ⟨fun c hc e => ?_, z, hz, rz⟩ + obtain ⟨w, hw, hw'⟩ := Li.sidle.f3 c hc + rw [e, hz] at hw + cases hw + rw [hw'] at rz + cases rz + rcases hy with e | e + · subst e; exact fin xt kit rxt + · subst e; exact fin xi kic rfl + have actK : ∀ q, ScActiveAtA σ1 q → ScActiveAtA σi q := fun q ⟨y, k, e, a⟩ => by + have q1 : q ≠ specPtr c0 := fun h => by rw [h, hcK] at e; cases e + by_cases q2 : q = scNameA m sc + · rw [q2, hkk] at e + cases e + exact ⟨yi, n0, by rw [kiK, if_neg q1, if_pos q2], a⟩ + · exact ⟨y, k, by rw [kiK, if_neg q1, if_neg q2]; exact e, a⟩ + have tgt : TgtA (specPtr t) σi := by + obtain ⟨⟨x, q, e1, e2, a1⟩, b, -⟩ := schedulable_actA hsched + exact ⟨⟨x, q, by rw [kiK, if_neg tc, if_neg (fun h => by rw [← h, e1] at hkk; cases hkk)]; exact e1, e2, + actK q a1⟩, b⟩ + have tgc : TgtA (specPtr c0) σi := by + have hci' : σ1.curThread ≠ σ1.idleThread := fun e => by + obtain ⟨z, hz, hs⟩ := J1.sinv.idleTcb + rw [← e, ccur, hcK] at hz + cases hz + cases hs + have hres' : σ1.schedulerAction = .resumeCurThread := by rw [hσ1]; exact J.act + obtain ⟨⟨x, q, e1, e2, a1⟩, b⟩ := J1.schedValid.act hres' hci' + rw [ccur, hcK] at e1 + cases e1 + rw [ccur] at b + exact ⟨⟨xi, q, kic, e2, actK q a1⟩, b⟩ + have svq : SchedValidA σq := by + let R := fun τ : State Unit => TgtA (specPtr t) τ ∧ TgtA (specPtr c0) τ + have hR : ∀ τ f, R τ → R { τ with readyQueues := f } := fun _ f h => ⟨h.1.rq f, h.2.rq f⟩ + have hRa : ∀ τ a, R τ → R { τ with schedulerAction := a } := fun _ a h => ⟨h.1.act a, h.2.act a⟩ + have svi : SchedValidA σi := J1.schedValid.refKA Ri + have h2 := HT.deq (specPtr t) R hR σi ⟨Sched.SchedValidA.toFIA svi, tgt, tgc⟩ _ md + have h3 := HT.enq (specPtr c0) R hR σd ⟨h2.1, h2.2, h2.2.2.1, h2.2.2.2⟩ _ me + have h4 := HT.enq (specPtr t) R hR σe ⟨h3.1, h3.2, h3.2.1.1, h3.2.1.2⟩ _ mf + have h5 := resched_fia R hR hRa σf ⟨h4.1, h4.2⟩ _ mr + exact h5.1.1.toA h5.1.2 + -- the spec state + have eqq : σq = { σi with readyQueues := σq.readyQueues, schedulerAction := .chooseNewThread } := by + have h := Qall.1 + rw [show σq.schedulerAction = .chooseNewThread from Sr.2] at h + exact h + have kq : σq.kheap = σi.kheap := congrArg (fun z => z.kheap) eqq + have kqK : ∀ q, σq.kheap q = if q = specPtr c0 then some (.tcb xi) + else if q = scNameA m sc then some (.schedContext yi n0) else σ1.kheap q := fun q => by + rw [kq]; exact kiK q + have fwdT : ∀ q z, σ1.kheap q = some (.tcb z) → ∃ w, σq.kheap q = some (.tcb w) ∧ + w = { z with tcbYieldTo := w.tcbYieldTo } := fun q z h => by + by_cases e : q = specPtr c0 + · rw [e, hcK] at h + cases h + exact ⟨xi, by rw [kqK, if_pos e], rfl⟩ + · exact ⟨z, by rw [kqK, if_neg e, if_neg (fun h' => by rw [← h', h] at hkk; cases hkk)]; exact h, rfl⟩ + have bwdT : ∀ q w, σq.kheap q = some (.tcb w) → ∃ z, σ1.kheap q = some (.tcb z) ∧ + w = { z with tcbYieldTo := w.tcbYieldTo } := fun q w h => by + rw [kqK] at h + split at h + · rename_i e + cases h + exact ⟨xr, e ▸ hcK, rfl⟩ + split at h + · cases h + · exact ⟨w, h, rfl⟩ + have keepO : ∀ q o, σ1.kheap q = some o → (∀ z, o ≠ .tcb z) → (∀ z k, o ≠ .schedContext z k) → + σq.kheap q = some o := fun q o h n1 n2 => by + rw [kqK, if_neg (fun e => by rw [e, hcK] at h; exact n1 _ (Option.some.inj h).symm), + if_neg (fun e => by rw [e, hkk] at h; exact n2 _ _ (Option.some.inj h).symm)] + exact h + have backO : ∀ q o, σq.kheap q = some o → (∀ z, o ≠ .tcb z) → (∀ z k, o ≠ .schedContext z k) → + σ1.kheap q = some o := fun q o h n1 n2 => by + rw [kqK] at h + split at h + · exact absurd (Option.some.inj h).symm (n1 _) + split at h + · exact absurd (Option.some.inj h).symm (n2 _ _) + · exact h + have scF : ∀ q y k, σ1.kheap q = some (.schedContext y k) → ∃ y', σq.kheap q = some (.schedContext y' k) ∧ + y'.scReplies = y.scReplies := fun q y k h => by + have q1 : q ≠ specPtr c0 := fun e => by rw [e, hcK] at h; cases h + by_cases q2 : q = scNameA m sc + · rw [q2, hkk] at h + cases h + exact ⟨yi, by rw [kqK, if_neg q1, if_pos q2], rfl⟩ + · exact ⟨y, by rw [kqK, if_neg q1, if_neg q2]; exact h, rfl⟩ + have F : SchedFrameA σ1 σq := by + have e1 := SchedFrameA.upd_sc (y' := yi) hkk + have hcA : (Function.update σ1.kheap (scNameA m sc) (some (.schedContext yi n0))) (specPtr c0) = + some (.tcb xr) := by rw [Function.update_of_ne hpc.symm]; exact hcK + have e2 := SchedFrameA.upd_tcb (σ := { σ1 with + kheap := Function.update σ1.kheap (scNameA m sc) (some (.schedContext yi n0)) }) (x' := xi) hcA ⟨rfl, rfl, rfl⟩ + have e3 : SchedFrameA σi σq := SchedFrameA.of_eq kq (congrArg (fun z => z.interruptStates) eqq) + (congrArg (fun z => z.interruptIrqNode) eqq) (congrArg (fun z => z.archState.armAsidTable) eqq) + (congrArg (fun z => z.cdt) eqq) (congrArg (fun z => z.isOriginalCap) eqq) + (congrArg (fun z => z.machineState.underlyingMemory) eqq) + (congrArg (fun z => z.archState.armCurrentVcpu) eqq) + exact PreO.trans e1 (PreO.trans e2 e3) + have Qk : QKeepA σ1 σq := QKeepA.ofBwd (D := ⟨congrArg (fun z => z.domainIndex) eqq, + congrArg (fun z => z.domainStartIndex) eqq, congrArg (fun z => z.domainList) eqq, congrArg (fun z => z.archState.armUsGlobalVspace) eqq⟩) fun q o h => by + rw [kqK] at h + split at h + · rename_i e + cases h + exact ⟨.tcb xr, by rw [e]; exact hcK, QObjA.ofTcb rfl⟩ + split at h + · rename_i e + cases h + exact ⟨.schedContext y0 n0, by rw [e]; exact hkk, QObjA.ofSc fun hr => ⟨hr, id⟩⟩ + · exact ⟨o, h, QObjA.refl o⟩ + -- the concrete words + have hP := J1.iplaced + have yv : (yieldA c0).val = c0.val + 3472 := field_val hca (K := 3472#u64) (by simp) + have wb : WritesIn st s1 sq (SchedStepNWA (st := st) N E Rp Z m σ1 s1) := wq.mono fun b hb => by + rcases hb with hb | hb + · exact Or.inl (Or.inl hb) + refine Or.inl (Or.inr ⟨c0, hcZ, by rw [hb, yv]; omega, by rw [hb, yv]; omega, ?_, ?_⟩) + · rw [hb]; exact field_ne (K1 := 3472#u64) (K2 := 3664#u64) hca hca (by simp) (by simp) (fun _ => by simp) + · rw [hb]; exact field_ne (K1 := 3472#u64) (K2 := 3672#u64) hca hca (by simp) (by simp) (fun _ => by simp) + have qk : ∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → K.val ≠ 3464 → K.val ≠ 3472 → K.val ≠ 3480 → + K.val ≠ 3488 → K.val ≠ 3592 → L.mem sq (wa u K) = L.mem s1 (wa u K) := fun u hu K k1 k2 n1 n0' n2 n3 n4 => by + have hua := J1.sr.core.tcbs u hu + refine wq.mem_eq (field_aligned hua k1 k2) fun h => ?_ + rcases h with h | h + · exact tcbWordNotMWrA J1.sr.toSchedSRN hu k1 n1 n2 n3 n4 h + · exact field_ne (K2 := 3472#u64) hua hca k1 (by simp) (fun _ => by simpa using n0') h + -- a word of a placed object other than a TCB is not the caller's yield word + have yobj : ∀ p k, Z.th p = some k → k ≠ .tcb → ∀ b : U64, p.val ≤ b.val → b.val < p.val + k.size → + b ≠ yieldA c0 := fun p k hp hk b h1' h2' e => by + have hth : Z.th c0 = some .tcb := (J1.sr.thT c0).2 hcZ + have hpc' : p ≠ c0 := fun h => by rw [h, hth] at hp; exact hk (Option.some.inj hp).symm + have := J1.sr.place.apart p c0 k .tcb hp hth hpc' + have hsz : ObjKindA.size .tcb = 4096 := rfl + have ev := congrArg UScalar.val e + rw [yv] at ev + omega + have yE : ∀ b, EFoot Z.T E Rp Bf b → b ≠ yieldA c0 := by + rintro b (⟨q, hq, h⟩ | ⟨r, hr, h⟩ | ⟨u, hu, h⟩ | h) e + · refine yobj q _ (J1.ipcPlace.endpoint q hq) (by intro h'; cases h') b (by omega) ?_ e + simp only [ObjKindA.size]; omega + · refine yobj r _ (J1.ipcPlace.reply r hr) (by intro h'; cases h') b (by omega) ?_ e + simp only [ObjKindA.size]; omega + · have hua := hP.e.tcb u hu + have a1 : u.val % 4096 = 0 := hua.align + have a2 : c0.val % 4096 = 0 := hca.align + have ev := congrArg UScalar.val e + rw [yv] at ev + rcases h with ⟨h1', h2'⟩ | h + · unfold TOffN at h2'; omega + · unfold TL at h; omega + · obtain ⟨q, len, hq, h1', h2'⟩ := J1.ipcPlace.buffer b h + exact yobj q _ hq (by intro h'; cases h') b h1' h2' e + have yN : ∀ b, Notif.NFoot N Z.T b → b ≠ yieldA c0 := by + rintro b (⟨q, hq, h1', h2'⟩ | ⟨u, hu, h⟩) e + · refine yobj q _ (J1.ipcPlace.notification q hq) (by intro h'; cases h') b h1' ?_ e + simp only [ObjKindA.size]; omega + · have hua := hP.n.tcb u hu + have v4 : ∀ K : U64, K.val < 4096 → (wa u K).val = u.val + K.val := fun K hK => Notif.tfield_val hua hK + have a1 : u.val % 4096 = 0 := hua.align + have a2 : c0.val % 4096 = 0 := hca.align + have ev := congrArg UScalar.val e + rw [yv] at ev + rcases h with h | h | h | h | h | h <;> rw [h] at ev + · rw [stA_eq, v4 _ (by simp)] at ev; simp at ev; omega + · rw [Notif.tBlockA_eq, v4 _ (by simp)] at ev; simp at ev; omega + · rw [Notif.badgeRegA_eq, v4 _ (by simp)] at ev; simp at ev; omega + · rw [Notif.prioA_eq', v4 _ (by simp)] at ev; simp at ev; omega + · rw [Notif.nextEp_eq, v4 _ (by simp)] at ev; simp at ev; omega + · rw [Notif.prevEp_eq, v4 _ (by simp)] at ev; simp at ev; omega + have um : σq.machineState.underlyingMemory = σ1.machineState.underlyingMemory := F.2.2 + have epq : EpStateRel Z.T E Rp Bf (L.mem sq) σq := + J1.ep.frame hP.e (fun b hb hf => wq.mem_eq hb fun h => + h.elim (endpFootOffWrite J1.sr.toSchedSRN J1.ipcPlace hf) (yE b hf)) + ⟨fun q _ h => keepO q _ h (fun _ e => by cases e) (fun _ _ e => by cases e), + fun q x h => ⟨x, keepO q _ h (fun _ e => by cases e) (fun _ _ e => by cases e), rfl⟩, + fun q x h => by + obtain ⟨w, hw, e⟩ := fwdT q x h + exact ⟨w, hw, by rw [e], by rw [e], by rw [e], by rw [e], by rw [e]⟩, + um⟩ + have ntq : NtfnStateRel N Z.T (L.mem sq) σq := + J1.ntfn.frame hP.n (fun b hb hf => wq.mem_eq hb fun h => + h.elim (notifFootOffMWrA J1.sr.toSchedSRN J1.ipcPlace.notification hf) (yN b hf)) + ⟨fun q _ h => keepO q _ h (fun _ e => by cases e) (fun _ _ e => by cases e), + fun q x h => by + obtain ⟨w, hw, e⟩ := fwdT q x h + exact ⟨w, hw, by rw [e], by rw [e]; rfl, by rw [e]⟩⟩ + have rpq : ReplyStackRelA (L.mem sq) Rp Z.C σq (scNameA m) stack := by + refine J1.reply.frame (fun r hr => ?_) (fun c _ y k h => ?_) + (fun r _ x' h => ⟨x', backO _ _ h (fun _ e => by cases e) (fun _ _ e => by cases e), rfl⟩) + · have hr3 := hP.e.rep r hr + have one : ∀ O : U64, O.val = 8 ∨ O.val = 16 → L.mem sq (wa r O) = L.mem s1 (wa r O) := by + intro O hO + have hv : (wa r O).val = r.val + O.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + refine wq.mem_eq (by unfold Aligned; rw [hv]; omega) fun h => ?_ + rcases h with h | h + · exact otherWordOffMWrA J1.sr.toSchedSRN (J1.ipcPlace.reply r hr) (by omega) (by omega) h + · refine yobj r _ (J1.ipcPlace.reply r hr) (by intro h'; cases h') _ (by omega) ?_ h + simp only [ObjKindA.size]; omega + exact ⟨one _ (Or.inr (by simp [layout.reply.NEXT])), one _ (Or.inl (by simp [layout.reply.PREV]))⟩ + · obtain ⟨y', h', e⟩ := scF _ _ _ h + exact ⟨y', k, h', e⟩ + have rvq : RunVcpuInv st Z.T sq := fun u hu h0 => by + have e1 : L.mem sq (runVcpuA u) = L.mem s1 (runVcpuA u) := by + unfold runVcpuA + exact qk u hu 3672#u64 (by simp) (by simp) (by simp) (by simp) (by simp) (by simp) (by simp) + have e2 : L.mem sq (Fl4ProofsA64.P1.stA u) = L.mem s1 (Fl4ProofsA64.P1.stA u) := by + rw [Fl4ProofsA64.P1.stA_eq] + exact qk u hu 3328#u64 (by simp) (by simp) (by simp) (by simp) (by simp) (by simp) (by simp) + rw [e2]; exact J1.runVcpu u hu (e1 ▸ h0) + have bqq : BlkQueuedA Z.T E N (L.mem sq) σq := + J1.blkQ.keepA (fun u hu => by + rw [Notif.tBlockA_eq] + exact qk u hu 3336#u64 (by simp) (by simp) (by simp) (by simp) (by simp) (by simp) (by simp)) + (fun u _ y h => by + obtain ⟨z, hz, e⟩ := bwdT _ y h + exact Or.inr ⟨z, hz, by rw [e]⟩) + (fun q y h => keepO q _ h (fun _ e => by cases e) (fun _ _ e => by cases e)) + (fun q y h => keepO q _ h (fun _ e => by cases e) (fun _ _ e => by cases e)) + have tw : ∀ u ∈ Z.T, ∀ O : U64, O.val < 224 ∨ O.val = 3512 → O.val % 8 = 0 → + L.mem sq (wa u O) = L.mem s1 (wa u O) := + fun u hu O h h8 => qk u hu O (by omega) h8 (by omega) (by omega) (by omega) (by omega) (by omega) + have at_ : ∀ u ∈ Z.T, ∃ x, σ1.kheap (specPtr u) = some (.tcb x) := fun u hu => by + obtain ⟨x, hx, -⟩ := J1.ntfn.tcb u hu + exact ⟨x, hx⟩ + have bufCover : BufCover st Z.T Bf sq σq := by + intro u hu y b sb v hy hb hl + obtain ⟨x, hx⟩ := at_ u hu + obtain ⟨y2, hy2, e⟩ := fwdT _ x hx + rw [hy] at hy2 + cases hy2 + have hb0 : ipcBufOfA b x = some sb := by + rw [e] at hb + exact hb + have hib : IbSame (L := L) s1 sq u := by + refine ⟨?_, ?_, ?_⟩ + · rw [ibWordA_eq]; exact tw u hu _ (by simp) (by simp) + · rw [sbWordA_eq]; exact tw u hu _ (by simp) (by simp) + · rw [sbWordA8_eq]; exact tw u hu _ (by simp) (by simp) + rw [lookup_ipc_buffer_sameA hib] at hl + exact J1.bufCover u hu x b sb v hx hb0 hl + have bufAl : UtBufAlA Z.T σq := fun u hu y hy => by + obtain ⟨x, hx⟩ := at_ u hu + obtain ⟨y2, hy2, e⟩ := fwdT _ x hx + rw [hy] at hy2 + cases hy2 + rw [e]; exact J1.bufAl u hu x hx + have idle : IdleNoFaultA m σq := fun c hc => by + obtain ⟨x, hx, hf⟩ := J1.idleNoFault c hc + obtain ⟨y, hy, e⟩ := fwdT _ x hx + exact ⟨y, hy, by rw [e]; exact hf⟩ + -- the concrete program + have htSc : obj.Sc.tcb st sc s = ok t := by rw [htd]; exact eq_of_spec_fd (P1.obj.Sc.tcb.spec s sc) + have hct : sched.cur_thread st s = ok c0 := by rw [hc0d]; exact cur_thread_ok s + have hyt := yieldTo_switch_concA (ar := ar) call hF1 eres hcur1 esw + have hdec : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (.Ok (), sq, a) := by + rw [hcap, decodeSc_programA, if_neg Bool.false_ne_true, hlbl, + scDecodeYield_eqA htSc hstall hct ht0 hne hprio hy hst, hyt] + simp only [bind_ok, uncurry_apply_pair] + have blkq : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac sq := by + have RSc := RS.cur + have hsc0' : L.mem s1 (scA c0) = L.mem s (scA c0) := farT c0 hca 3464#u64 (by simp) (by simp) (fun _ => by simp) + have hiq0' : L.mem s1 (inqA c0) = L.mem s (inqA c0) := farT c0 hca 3592#u64 (by simp) (by simp) (fun _ => by simp) + have hcRun : Fl4ProofsA64.P1.Runnable (L.mem s1 (stA c0)) := by + rw [hs1, L.mem_wr _ _ _ _ sA sA, if_pos rfl] + exact Or.inr (Or.inl rfl) + have q := schedcontext.yield_switches.blkY (st := st) s1 sc c0 (BXI.blk J1.sys.ent.bxi) hcH hcH0 + (by rw [hT1]; exact ht0) (by rw [hT1]; exact hne) hcRun (by rw [hsc0', hc0d]; exact RSc.2.1) + (by rw [hsc0', hc0d]; exact RSc.2.2) (by rw [hiq0', hc0d]; exact RSc.1) + rw [esw, WP.spec_ok] at q + exact q.1 + obtain ⟨-, -, sysl, symL, -, userOK, j7⟩ := J.decodeFinA hA hM hPl hfm hslot hcov + (rfl : capA st s slot = capA st s slot) hext hSC (lbl := lbl) + (by rw [hcap, hlbl]; simp [DecArmS1aEnt, DecArmQH, DecArmQZ2, DecArmKQ2, DecArmKQ, DecArmKQ3, DecArmDelQ, + DecArmS1aC, capFam]) + (fun _ h => by rw [hcap] at h; cases h) (fun _ h => by rw [hcap] at h; cases h) hdec σq m + have J2 := J1.of_stepFinA (Q := Q') (Rl := Z.R) (curOf := Z.curOf) (curScOf := Z.curScOf) wb F.1 F.2.1 Qk um + (fun _ _ => rfl) (BlkOK.bxi blkq) sysl symL userOK j7 Hq.sched epq ntq rpq bufCover bufAl idle Hq.mcp Hq.links + Lq.sinv Lq.sidle rvq bqq svq + have B : Sched.BodyEndA σ σq m := by + have ne : σ.curSc ≠ σ.curThread := fun e => by + obtain ⟨y2, j2, h2⟩ := J.curInv.sc + rw [e, hkc] at h2; cases h2 + have e1 : σ1.kheap σ.curSc = σ.kheap σ.curSc := by + rw [hσ1]; simp [stUpdA, setObjA, Function.update_of_ne ne] + have ne0 : σ.curSc ≠ specPtr c0 := by rw [← ccur, hσ1]; exact ne + refine ⟨Sched.BodyOKA.ofEntry J (Or.inr (by rw [eqq])) (by rw [eqq, hσi, hσ1]; rfl) (by rw [eqq, hσi, hσ1]; rfl) + (by rw [eqq, hσi, hσ1]; rfl) (by rw [eqq, hσi, hσ1]; rfl) (by rw [eqq, hσi, hσ1]; rfl) + (by rw [eqq, hσi, hσ1]; rfl) (fun y' k h => ?_) (fun e => by rw [eqq] at e; cases e), + by rw [eqq, hσi, hσ1]; rfl⟩ + rw [kqK, if_neg ne0] at h + split at h + · rename_i e + cases h + exact ⟨y0, by rw [← e1, e]; exact hkk, rfl, rfl, rfl⟩ + · exact ⟨y', by rw [← e1]; exact h, rfl, rfl, rfl⟩ + refine ⟨xc, σ1, σq, sq, hkc, hσ1, hsp, ?_, hdec, Q', Z.R, J2, B⟩ + rw [← hname] + exact yieldTo_noReturn_specA hkk hf0 hres hspecSw + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3SchedActA.lean b/proofs/Fl4ProofsA64/P3/Stage3SchedActA.lean new file mode 100644 index 000000000..e49968f2f --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SchedActA.lean @@ -0,0 +1,210 @@ +/- +P3 (aarch64, A64-P3 Stage-3 integration; queue item 1): **the activation footprint**. A sibling of +`activate_thread_refinesA` (`P3/ScActivateA`, statement unchanged) that also exports what `JStage3A.of_schedWr` needs: + +* `WritesIn s s' (SchedStepWA Z m σ s)`: `activate_yield` writes only the SC words of `MWrA`, the three consumed + registers `+256/264/272` and the YIELD word `+3472` of the yielder; `activate_state` writes the NEXT_IP register word + `+512`, the state word `+3328` and scheduler words. Every one is a TCB word from offset 224 (no IPC-buffer word). +* `SchedFrameA σ τ` (`P3/SpecFrameSchedA`): the spec step keeps the cap slots / VCPU links of TCBs, SC sizes, the + IRQ / ASID / CDT / original-cap state and the user memory. + +No premise; new module. Edit to `P3/ScActivateA`: `activationYieldA` and `activate_yield_refinesA` lose `private`. +-/ +import Fl4ProofsA64.P3.ScActivateA +import Fl4ProofsA64.P3.SpecFrameSchedA +import Fl4ProofsA64.P3.Stage3SchedWrA +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3.Sched + +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4ProofsA64.P1.W7.R.ST + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +instance (t : ObjRef) : SPresInst (activationYieldA t) := ⟨by unfold activationYieldA; spres⟩ + +/-- A TCB word from offset 224 (not the two VCPU links) is a per-step TCB word. -/ +theorem tcbStepW_ofA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRN st Z m κ s σ) {t K : U64} (ht : t ∈ Z.T) (h1 : 224 ≤ K.val) (h2 : K.val < 4096) + (h3 : K.val ≠ 3664) (h4 : K.val ≠ 3672) : TcbStepWA Z.T (wa t K) := by + have hv : ∀ K : U64, K.val < 4096 → (wa t K).val = t.val + K.val := fun K hK => field_val (H.core.tcbs t ht) hK + have h3' : (3664#u64 : U64).val = 3664 := by decide + have h4' : (3672#u64 : U64).val = 3672 := by decide + refine ⟨t, ht, by rw [hv K h2]; omega, by rw [hv K h2]; omega, fun c => ?_, fun c => ?_⟩ + · have := congrArg UScalar.val c + rw [hv K h2, hv _ (by rw [h3']; omega), h3'] at this + omega + · unfold runVcpuA at c + have := congrArg UScalar.val c + rw [hv K h2, hv _ (by rw [h4']; omega), h4'] at this + omega + +theorem schedStepW_mwr {Z : XSetsA} {m : McState} {σ : State Unit} {s : S} {b : U64} + (h : MWrA (st := st) Z m σ s b) : SchedStepWA (st := st) Z m σ s b := Or.inl h + +theorem schedStepW_swr {Z : XSetsA} {m : McState} {σ : State Unit} {s : S} {b : U64} + (h : SWrA (st := st) Z s b) : SchedStepWA (st := st) Z m σ s b := Or.inl (Or.inr h) + +/-- `activate_yield` footprint: the yielder's consumed registers and YIELD word, plus the `MWrA` words. -/ +theorem activate_yield_footA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {σ : State Unit} (H : YieldQueueRelA (st := st) Z m κ s σ) (a : A) {t : U64} + (ht : t ∈ Z.T) (hC0 : ∀ c ∈ Z.C, c ≠ 0#u64) + {x : Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + (htu : Fl4TicksToUsAgreeA ar a) : + (do + let i ← obj.Tcb.yield_to st t s + if i != 0#u64 then schedcontext.complete_yield_to st ar s a t else ok (s, a)) ⦃ r => + WritesIn st s r.1 (fun b => MWrA (st := st) Z m σ s b ∨ ScConsumedRegsA t b ∨ b = yieldA t) ⦄ := by + obtain ⟨x0, h0, hv⟩ := H.links.tcb t ht + rw [hk] at h0; cases h0 + let sc := L.mem s (yieldA t) + rw [eq_of_spec_fd (obj.Tcb.yield_to.spec s t), bind_ok] + change (if sc != 0#u64 then _ else _) ⦃ _ ⦄ + by_cases hsc : sc = 0#u64 + · rw [if_neg (by simp [hsc]), WP.spec_ok] + exact WritesIn.refl _ _ + · rw [if_pos (bne_iff_ne.2 hsc)] + obtain ⟨hc, y, n, hy, hFrom⟩ := H.links.toSc t ht hsc + have eq : L.mem s (scYieldFromA sc) = t := by + obtain ⟨y0, n0, e, R⟩ := H.sched.scr.sc sc hc + rw [hy] at e; cases e + have e := hFrom + have haddr : scYieldFromA sc = scWA sc 56#u64 := by unfold scYieldFromA scWA layout.sc.YIELD_FROM; rfl + rw [R.yieldFrom, ← haddr] at e + have en : L.mem s (scYieldFromA sc) ≠ 0#u64 := by + intro h; rw [h, optRefS_zero] at e; cases e + rw [optRefS_ne en] at e + exact specPtr_inj (Option.some.inj e) + have t0 : t ≠ 0#u64 := fun h => (H.sched.core.tcbs t ht).ne0 (by rw [h]; rfl) + refine WP.spec_mono (yield_complete_liveWithA H.sched H.states H.mcp H.links a hc hC0 t0 eq + fl4MaxConsumedTicks fl4TicksToUs rfl htu) ?_ + rintro r ⟨-, τ, -, -, -, -, -, -, -, -, -, -, w, -⟩ + exact w + +/-- `activate_state` footprint: the NEXT_IP register word, the state word and the scheduler words. -/ +theorem activate_state_footA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {σ : State Unit} (H : YieldQueueRelA (st := st) Z m κ s σ) (a : A) + {t : U64} (ht : t ∈ Z.T) {x : Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + (legal : ActivateStateOKA x.tcbState) + (pc : (L.mem s (regWA t layout.reg.FAULT_IP)).bv = x.tcbArch.tcbContext.userRegs .faultIP) : + (do + let ts ← obj.Tcb.state st t s + if ts = sched.TS_RESTART then + let saved ← obj.Tcb.reg st t s layout.reg.FAULT_IP + let s' ← obj.Tcb.set_reg st t s layout.reg.NEXT_IP saved + let s'' ← sched.set_thread_state st s' t sched.TS_RUNNING + ok (s'', a) + else ok (s, a)) ⦃ r => + WritesIn st s r.1 (fun b => b = wa t 512#u64 ∨ SWrA (st := st) Z s b ∨ b = stA t) ⦄ := by + have code := H.states t ht x hk + have stRead : L.mem s (stA t) = sched.TS_RESTART ↔ x.tcbState = .restart := by + have e : (u v : U64) → (u = v ↔ u.val = v.val) := + fun _ _ => ⟨congrArg UScalar.val, UScalar.eq_of_val_eq⟩ + rw [e, code] + unfold sched.TS_RESTART + rcases legal with h | h | h | h <;> rw [h] <;> decide + refine WP.spec_bind (obj.Tcb.state.spec s t) ?_ + intro ts hts + rw [hts] + by_cases hR : x.tcbState = .restart + · rw [if_pos (stRead.2 hR)] + rw [tcb_reg_eqA, bind_ok] + have small : (512#u64 : U64).val < 4096 := by decide + have aligned : (512#u64 : U64).val % 8 = 0 := by decide + have al : Aligned (wa t 512#u64) := field_aligned (H.sched.core.tcbs t ht) small aligned + refine WP.spec_bind (spec_andA + (activateWritePcA H ht hk _ .nextIP (Or.inl rfl) + (by simp [layout.reg.NEXT_IP, layout.reg.ELR_EL1]) (by simp [layout.reg.NEXT_IP, layout.reg.ELR_EL1])) + (setRegFrameA s (H.sched.core.tcbs t ht) (K := 512#u64) + (by simp [layout.reg.NEXT_IP, layout.reg.ELR_EL1]) (by simp [layout.reg.NEXT_IP, layout.reg.ELR_EL1]) al)) ?_ + rintro s' ⟨H', w1⟩ + have codeR : sched.TS_RUNNING.val = tsCodeA .running := by unfold sched.TS_RUNNING tsCodeA; rfl + refine WP.spec_bind (yieldRunnableA H' ht (updKSc_same _ _ _) + (by change runnable x.tcbState = _; rw [hR]; rfl) + (by rfl : runnable ThreadState.running = true) codeR) ?_ + rintro s'' ⟨τ, -, -, -, -, -, -, w2⟩ + rw [WP.spec_ok] + exact (w1.trans w2).mono fun b hb => by + rcases hb with h | h | h + · exact Or.inl h + · exact Or.inr (Or.inl (swr_ksb rfl w1.1 h)) + · exact Or.inr (Or.inr h) + · rw [if_neg (fun h => hR (stRead.1 h)), WP.spec_ok] + exact WritesIn.refl _ _ + + +set_option maxRecDepth 4096 in +/-- **`activate_thread_refinesA` with its footprint and spec frame** (module docstring). -/ +theorem activate_thread_stepA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {σ : State Unit} (H : YieldQueueRelA (st := st) Z m κ s σ) (a : A) + (hC0 : ∀ c ∈ Z.C, c ≠ 0#u64) (htu : Fl4TicksToUsAgreeA ar a) + {x : Tcb} (hk : σ.kheap σ.curThread = some (.tcb x)) (legal : ActivateStateOKA x.tcbState) + (pc : (L.mem s (regWA (L.curW s) layout.reg.FAULT_IP)).bv = x.tcbArch.tcbContext.userRegs .faultIP) : + sched.activate_thread st ar s a ⦃ r => r.2 = a ∧ ∃ τ, + fl4ActivateThread (Z := Unit) σ = ({((), τ)}, False) ∧ + YieldQueueRelA (st := st) Z m κ r.1 τ ∧ τ.curThread = σ.curThread ∧ + (∃ y, τ.kheap τ.curThread = some (.tcb y) ∧ y.tcbState = activatedStateA x.tcbState) ∧ + SchedFrameA σ τ ∧ WritesIn st s r.1 (SchedStepWA (st := st) Z m σ s) ⦄ := by + let t := L.curW s + have ht := H.sched.curT + have htσ := H.sched.sch.curThread + rw [htσ] at hk + unfold sched.activate_thread + refine WP.spec_bind (sched.cur_thread.spec s) ?_ + intro ct hct + rw [show ct = t from hct, ← cfgResultAssocA] + refine WP.spec_bind (spec_andA (activate_yield_refinesA H a ht hC0 hk htu) + (activate_yield_footA H a ht hC0 hk htu)) ?_ + rintro ⟨s1, a1⟩ ⟨⟨ea, σ1, x1, he, H1, hC1, h1, hS, hP, hWord⟩, W1⟩ + simp only [uncurry_apply_pair] at W1 ⊢ + change a1 = a at ea + subst a1 + have legal1 : ActivateStateOKA x1.tcbState := hS.symm ▸ legal + have pc1 : (L.mem s1 (regWA t layout.reg.FAULT_IP)).bv = x1.tcbArch.tcbContext.userRegs .faultIP := by + rw [hWord, hP]; exact pc + refine WP.spec_mono (spec_andA (activate_state_refinesA H1 a ht h1 legal1 pc1) + (activate_state_footA H1 a ht h1 legal1 pc1)) ?_ + rintro r ⟨⟨arch, τ, hτ, H2, hC2, y, hy, hState⟩, W2⟩ + have hCur := hC2.trans hC1 + have hTcb := H.sched.toSchedSRN + have F1 : SchedFrameA σ σ1 := by + have hm : ((), σ1) ∈ (activationYieldA (specPtr t) σ).1 := by rw [he]; exact Set.mem_singleton _ + exact (inferInstance : SPresInst (activationYieldA (specPtr t))).pres σ _ hm + have F2 : SchedFrameA σ1 τ := by + have hm : ((), τ) ∈ (fl4ActivateState (Z := Unit) (specPtr t) σ1).1 := by rw [hτ]; exact Set.mem_singleton _ + exact (inferInstance : SPresInst (fl4ActivateState (specPtr t))).pres σ1 _ hm + refine ⟨arch, τ, ?_, H2, hCur, ⟨y, ?_, ?_⟩, PreO.trans F1 F2, ?_⟩ + · unfold fl4ActivateThread + rw [nd_gets_bindSc, htσ] + rw [← nd_bind_assocSc] + change (activationYieldA (specPtr t) >>= fun _ => fl4ActivateState (specPtr t)) σ = _ + rw [nd_bind_oneSc he] + exact hτ + · rw [hCur, htσ]; exact hy + · rw [hState, hS] + · have hk1 : L.ksb s1 = L.ksb s := W1.1 + refine (W1.trans W2).mono fun b hb => ?_ + have K (K : U64) (h1 : 224 ≤ K.val) (h2 : K.val < 4096) (h3 : K.val ≠ 3664) (h4 : K.val ≠ 3672) : + TcbStepWA Z.T (wa t K) := tcbStepW_ofA hTcb ht h1 h2 h3 h4 + rcases hb with ((h | h | h) | h | h | h) + · exact Or.inl h + · rcases h with rfl | rfl | rfl + · exact Or.inr (K 256#u64 (by decide) (by decide) (by decide) (by decide)) + · exact Or.inr (K 264#u64 (by decide) (by decide) (by decide) (by decide)) + · exact Or.inr (K 272#u64 (by decide) (by decide) (by decide) (by decide)) + · rw [h]; exact Or.inr (K 3472#u64 (by decide) (by decide) (by decide) (by decide)) + · rw [h]; exact Or.inr (K 512#u64 (by decide) (by decide) (by decide) (by decide)) + · exact Or.inl (Or.inr (swr_ksb rfl hk1 h)) + · rw [h, stA_eq]; exact Or.inr (K 3328#u64 (by decide) (by decide) (by decide) (by decide)) + +end + +end + + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3SchedActResA.lean b/proofs/Fl4ProofsA64/P3/Stage3SchedActResA.lean new file mode 100644 index 000000000..23ccfd57d --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SchedActResA.lean @@ -0,0 +1,272 @@ +/- +P3 (aarch64, A64-P3 Stage 3, T3 G4b slice 1): **the scheduler spec functions keep the scheduler action**. + +`APres m` (`PresR ActKeepA m`): every result state of the spec computation `m` has the start state's +`schedulerAction`, `curThread`, `releaseQueue` and TCB objects. A parallel of `SPresA` (`P3/SpecFrameCalcA`) over the leaves of `fl4ScAndTimer`: the class +`APresInst`, the writing primitives (`updateSchedContext`, `doMachineOp`), the refill functions and the +timer tail. The footprint argument ("those leaves never write the action word") is exactly this: they write the heap, +the refills, the machine state, `reprogramTimer` and `curSc` only. + +* **`fl4ScAndTimer_apres`**. +* **`actOfA_resume`**, **`YieldQueueRelA.actionW_zero`**: a related state at `resumeCurThread` has action word `0`. +* **`fl4Schedule_actA`**: every outcome of `fl4Schedule` has `schedulerAction = .resumeCurThread` (its + `fl4SelectThread` ends with `setSchedulerAction .resumeCurThread`, then `fl4ScAndTimer` keeps it). + +No premise; new module. +-/ +import Fl4ProofsA64.P3.SpecFrameSchedA +import Fl4ProofsA64.P3.SpecFrA +import Fl4ProofsA64.P3.ScYieldQueueA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec fl4_core_a64 Fl4ProofsA64.P1 + +noncomputable section + +/-- The scheduler action, the current thread, the release and ready queues, the idle thread and every TCB object are +kept, and every SC object stays an SC object of the same size and activity. -/ +def ActKeepA (σ σ' : Fl4SpecA64.State Unit) : Prop := + σ'.schedulerAction = σ.schedulerAction ∧ σ'.curThread = σ.curThread ∧ σ'.releaseQueue = σ.releaseQueue ∧ + (∀ p x, σ'.kheap p = some (.tcb x) ↔ σ.kheap p = some (.tcb x)) ∧ σ'.readyQueues = σ.readyQueues ∧ + σ'.idleThread = σ.idleThread ∧ + ∀ p y k, σ.kheap p = some (.schedContext y k) → + ∃ y', σ'.kheap p = some (.schedContext y' k) ∧ scActive y' = scActive y + +instance : PreO ActKeepA where + refl _ := ⟨rfl, rfl, rfl, fun _ _ => Iff.rfl, rfl, rfl, fun _ y _ h => ⟨y, h, rfl⟩⟩ + trans h1 h2 := ⟨h2.1.trans h1.1, h2.2.1.trans h1.2.1, h2.2.2.1.trans h1.2.2.1, + fun p x => (h2.2.2.2.1 p x).trans (h1.2.2.2.1 p x), h2.2.2.2.2.1.trans h1.2.2.2.2.1, + h2.2.2.2.2.2.1.trans h1.2.2.2.2.2.1, fun p y k hp => by + obtain ⟨y1, e1, a1⟩ := h1.2.2.2.2.2.2 p y k hp + obtain ⟨y2, e2, a2⟩ := h2.2.2.2.2.2.2 p y1 k e1 + exact ⟨y2, e2, a2.trans a1⟩⟩ + +/-- A scheduling-context function keeping `scActive`. -/ +class ScActFn (f : Fl4SpecA64.SchedContext → Fl4SpecA64.SchedContext) : Prop where + act : ∀ y, scActive (f y) = scActive y + +instance (g : List Refill → List Refill) : ScActFn (scRefillsUpdate g) := ⟨fun _ => rfl⟩ +instance (g : Ticks → Ticks) : ScActFn (scBudgetUpdate g) := ⟨fun _ => rfl⟩ +instance (g : Ticks → Ticks) : ScActFn (scPeriodUpdate g) := ⟨fun _ => rfl⟩ +instance (g : Ticks → Ticks) : ScActFn (scConsumedUpdate g) := ⟨fun _ => rfl⟩ +instance (g : Option ObjRef → Option ObjRef) : ScActFn (scTcbUpdate g) := ⟨fun _ => rfl⟩ +instance (g : Option ObjRef → Option ObjRef) : ScActFn (scNtfnUpdate g) := ⟨fun _ => rfl⟩ +instance (g : Option ObjRef → Option ObjRef) : ScActFn (scYieldFromUpdate g) := ⟨fun _ => rfl⟩ +instance (g : List ObjRef → List ObjRef) : ScActFn (scRepliesUpdate g) := ⟨fun _ => rfl⟩ +instance (g : Badge → Badge) : ScActFn (scBadgeUpdate g) := ⟨fun _ => rfl⟩ +instance (g : Bool → Bool) : ScActFn (scSporadicUpdate g) := ⟨fun _ => rfl⟩ +instance (c : Ticks) : ScActFn (fun sc : SchedContext => { sc with scConsumed := sc.scConsumed + c }) := + ⟨fun _ => rfl⟩ + +/-- Scheduler-action preservation of a spec computation. -/ +abbrev APres {α : Type} (m : SMonad Unit α) : Prop := PresR ActKeepA m + +class APresInst {α : Type} (m : SMonad Unit α) : Prop where + pres : APres m + +instance {α β : Type} {m : SMonad Unit α} {f : α → SMonad Unit β} [hm : APresInst m] + [hf : ∀ a, APresInst (f a)] : APresInst (m >>= f) := + ⟨PresR.bind hm.pres fun a => (hf a).pres⟩ +instance {α : Type} (a : α) : APresInst (pure a : SMonad Unit α) := ⟨PresR.pure a⟩ +instance {α : Type} (f : State Unit → α) : APresInst (NonDet.gets f : SMonad Unit α) := ⟨PresR.gets f⟩ +instance : APresInst (NonDet.get : SMonad Unit (State Unit)) := ⟨PresR.get⟩ +instance {α : Type} (f : State Unit → Option α) : APresInst (NonDet.getsThe f : SMonad Unit α) := + ⟨PresR.getsThe f⟩ +instance {α : Type} (o : Option α) : APresInst (NonDet.assertOpt o : SMonad Unit α) := ⟨PresR.assertOpt o⟩ +instance (P : Prop) : APresInst (NonDet.assert P : SMonad Unit Unit) := ⟨PresR.assert P⟩ +instance {α : Type} : APresInst (NonDet.fail : SMonad Unit α) := ⟨PresR.fail⟩ +instance {P : Prop} {d : Decidable P} {m : SMonad Unit Unit} [h : APresInst m] : + APresInst (@NonDet.when (State Unit) P d m) := ⟨PresR.when h.pres⟩ +instance {P : Prop} {d : Decidable P} {m : SMonad Unit Unit} [h : APresInst m] : + APresInst (@NonDet.unless (State Unit) P d m) := ⟨PresR.unless' h.pres⟩ +instance {α : Type} {P : Prop} {d : Decidable P} {a b : SMonad Unit α} [ha : APresInst a] [hb : APresInst b] : + APresInst (@ite _ P d a b) := ⟨PresR.ite' ha.pres hb.pres⟩ +instance {β γ : Type} {f : γ → SMonad Unit β} [hf : ∀ a, APresInst (f a)] (l : List γ) : + APresInst (NonDet.mapM f l) := ⟨PresR.mapM (fun a => (hf a).pres) l⟩ +instance {α : Type} {C : α → State Unit → Prop} {B : α → SMonad Unit α} [hB : ∀ a, APresInst (B a)] (r : α) : + APresInst (NonDet.whileLoop C B r) := ⟨PresR.whileLoop (fun a => (hB a).pres) r⟩ +instance {α : Type} (S : Set α × Prop) : APresInst (NonDet.selectF S : SMonad Unit α) := ⟨PresR.selectF S⟩ +instance {α : Type} {f : α → SMonad Unit Unit} [hf : ∀ a, APresInst (f a)] (o : Option α) : + APresInst (NonDet.maybeM f o) := by + cases o + · exact ⟨PresR.pure _⟩ + · exact hf _ + +/-- A state modification off the scheduler action. -/ +theorem APres.modify_fix (f : State Unit → State Unit) (h : ∀ σ, ActKeepA σ (f σ)) : + APres (NonDet.modify f : SMonad Unit Unit) := PresR.modify f h + +macro "apres" : tactic => `(tactic| + repeat' (first + | exact APresInst.pres + | refine PresR.bind_pure _ _ ?_ + | refine PresR.bind ?_ (fun _ => ?_) + | refine PresR.when ?_ + | refine PresR.unless' ?_ + | refine PresR.ite' ?_ ?_ + | (refine APres.modify_fix _ (fun _ => ⟨rfl, rfl, rfl, fun _ _ => Iff.rfl, rfl, rfl, fun _ y _ h => ⟨y, h, rfl⟩⟩)) + | dsimp only + | split)) + +/-! ## The writing primitives -/ + +theorem updateSchedContext_apres (f : Fl4SpecA64.SchedContext → Fl4SpecA64.SchedContext) [ScActFn f] + (p : Fl4SpecA64.ObjRef) : + APres (updateSchedContext (Z := Unit) p f) := by + intro σ r hr + obtain ⟨y1, hy1, h⟩ := hr + have hy1' : y1 ∈ (NonDet.assertOpt (σ.kheap p) σ).1 := hy1 + cases hk : σ.kheap p with + | none => rw [hk] at hy1'; exact absurd hy1' (Set.notMem_empty _) + | some ko => + rw [hk] at hy1' + have e1 : y1 = (ko, σ) := hy1' + subst e1 + cases ko with + | schedContext sc n => + obtain ⟨ko', hko, hr2⟩ := setObject_memA h + rw [hr2] + refine ⟨rfl, rfl, rfl, fun q x => ?_, rfl, rfl, fun q y' k' hq => ?_⟩ + · by_cases e : q = p + · subst e; simp [hk] + · simp [Function.update_of_ne e] + · by_cases e : q = p + · subst e + rw [hk] at hq + have h1 := Option.some.inj hq + injection h1 with e1 e2 + subst e1; subst e2 + exact ⟨f sc, by simp, ScActFn.act sc⟩ + · exact ⟨y', by simp [Function.update_of_ne e]; exact hq, rfl⟩ + | _ => exact absurd h (Set.notMem_empty _) + +instance (p : Fl4SpecA64.ObjRef) (f : Fl4SpecA64.SchedContext → Fl4SpecA64.SchedContext) [ScActFn f] : + APresInst (updateSchedContext (Z := Unit) p f) := ⟨updateSchedContext_apres f p⟩ + +theorem doMachineOp_apres {α : Type} (mop : NonDet MachineState α) : APres (doMachineOp (Z := Unit) mop) := by + intro σ r hr + obtain ⟨y1, hy1, hr⟩ := hr + have e1 : y1 = (σ.machineState, σ) := hy1 + subst e1 + obtain ⟨⟨⟨a, ms'⟩, s2⟩, hy2, hr⟩ := hr + have e2 : s2 = σ := hy2.2 + subst e2 + obtain ⟨y3, hy3, hr⟩ := hr + have e3 : y3 = ((), { s2 with machineState := ms' }) := hy3 + subst e3 + have e4 : r = (a, { s2 with machineState := ms' }) := hr + rw [e4] + exact ⟨rfl, rfl, rfl, fun _ _ => Iff.rfl, rfl, rfl, fun _ y _ h => ⟨y, h, rfl⟩⟩ + +instance {α : Type} (mop : NonDet MachineState α) : APresInst (doMachineOp (Z := Unit) mop) := + ⟨doMachineOp_apres mop⟩ + +/-! ## Readers and small writers -/ + +instance {α : Type} (f : Tcb → α) (t : ObjRef) : APresInst (threadGet (Z := Unit) f t) := + ⟨by unfold threadGet; apres⟩ +instance (f : Tcb → Option ObjRef) (r : ObjRef) : APresInst (getTcbObjRef (Z := Unit) f r) := + ⟨by unfold getTcbObjRef; apres⟩ +instance (r : ObjRef) : APresInst (getThreadState (Z := Unit) r) := ⟨by unfold getThreadState; apres⟩ +instance (d : Domain) (p : Priority) : APresInst (getTcbQueue (Z := Unit) d p) := ⟨by unfold getTcbQueue; apres⟩ + +instance (p : ObjRef) : APresInst (getObject (Z := Unit) p) := ⟨by unfold getObject; apres⟩ +instance (p : ObjRef) : APresInst (getSchedContext (Z := Unit) p) := ⟨by unfold getSchedContext; apres⟩ +instance (f : SchedContext → Option ObjRef) (p : ObjRef) : APresInst (getScObjRef (Z := Unit) f p) := + ⟨by unfold getScObjRef; apres⟩ +instance (p : ObjRef) : APresInst (getRefills (Z := Unit) p) := ⟨by unfold getRefills; apres⟩ +instance (p : ObjRef) : APresInst (getRefillHead (Z := Unit) p) := ⟨by unfold getRefillHead; apres⟩ +instance (p : ObjRef) : APresInst (getRefillTail (Z := Unit) p) := ⟨by unfold getRefillTail; apres⟩ +instance (p : ObjRef) : APresInst (getScRefillReady (Z := Unit) p) := ⟨by unfold getScRefillReady; apres⟩ +instance (p : ObjRef) (u : Ticks) : APresInst (getScRefillSufficient (Z := Unit) p u) := + ⟨by unfold getScRefillSufficient; apres⟩ +instance (p : ObjRef) (u : Ticks) : APresInst (fl4GetScRefillSufficient (Z := Unit) p u) := + ⟨by unfold fl4GetScRefillSufficient; apres⟩ +instance (p : ObjRef) : APresInst (getScActive (Z := Unit) p) := ⟨by unfold getScActive; apres⟩ +instance (p : ObjRef) : APresInst (isRoundRobin (Z := Unit) p) := ⟨by unfold isRoundRobin; apres⟩ + +/-! ### Refills -/ + +instance (p : ObjRef) (f : Refill → Refill) : APresInst (updateRefillHd (Z := Unit) p f) := + ⟨by unfold updateRefillHd; apres⟩ +instance (p : ObjRef) (f : Refill → Refill) : APresInst (updateRefillTl (Z := Unit) p f) := + ⟨by unfold updateRefillTl; apres⟩ +instance (p : ObjRef) (r : Refill) : APresInst (refillAddTail (Z := Unit) p r) := ⟨by unfold refillAddTail; apres⟩ +instance (p : ObjRef) : APresInst (refillPopHead (Z := Unit) p) := ⟨by unfold refillPopHead; apres⟩ +instance (p : ObjRef) : APresInst (refillSize (Z := Unit) p) := ⟨by unfold refillSize; apres⟩ +instance (p : ObjRef) : APresInst (refillSingle (Z := Unit) p) := ⟨by unfold refillSingle; apres⟩ +instance (p : ObjRef) : APresInst (refillFull (Z := Unit) p) := ⟨by unfold refillFull; apres⟩ +instance (p : ObjRef) (r : Refill) : APresInst (scheduleUsed (Z := Unit) p r) := ⟨by unfold scheduleUsed; apres⟩ +instance (p : ObjRef) : APresInst (mergeOverlappingRefills (Z := Unit) p) := ⟨by unfold mergeOverlappingRefills; apres⟩ +instance (p : ObjRef) : APresInst (refillHeadOverlappingLoop (Z := Unit) p) := + ⟨by unfold refillHeadOverlappingLoop; apres⟩ +instance (p : ObjRef) : APresInst (refillUnblockCheck (Z := Unit) p) := ⟨by unfold refillUnblockCheck; apres⟩ +instance (o : Option ObjRef) (a b : Option Bool) : APresInst (ifCondRefillUnblockCheck (Z := Unit) o a b) := + ⟨by unfold ifCondRefillUnblockCheck; apres⟩ +instance (p : ObjRef) : APresInst (mergeNonoverlappingHeadRefill (Z := Unit) p) := + ⟨by unfold mergeNonoverlappingHeadRefill; apres⟩ +instance (m : Ticks) (p : ObjRef) : APresInst (headInsufficientLoopWith (Z := Unit) m p) := + ⟨by unfold headInsufficientLoopWith; apres⟩ +instance (p : ObjRef) (u : Ticks) : APresInst (chargeEntireHeadRefill (Z := Unit) p u) := + ⟨by unfold chargeEntireHeadRefill; apres⟩ +instance (l : Ticks) (p : ObjRef) (u : Ticks) : APresInst (handleOverrunWith (Z := Unit) l p u) := + ⟨by unfold handleOverrunWith; apres⟩ +instance (l m u : Ticks) : APresInst (refillBudgetCheckWith (Z := Unit) l m u) := + ⟨by unfold refillBudgetCheckWith; apres⟩ +instance (u : Ticks) : APresInst (fl4RefillBudgetCheck (Z := Unit) u) := ⟨by unfold fl4RefillBudgetCheck; apres⟩ +instance (u : Ticks) : APresInst (refillBudgetCheckRoundRobin (Z := Unit) u) := + ⟨by unfold refillBudgetCheckRoundRobin; apres⟩ +instance : APresInst (fl4CommitTime (Z := Unit)) := ⟨by unfold fl4CommitTime; apres⟩ + +/-! ### Switch, timer, awaken, domain -/ + +instance : APresInst (fl4SwitchSchedContext (Z := Unit)) := ⟨by unfold fl4SwitchSchedContext; apres⟩ + +instance (q : List ObjRef) : APresInst (fl4ReleaseDeadline (Z := Unit) q) := ⟨by unfold fl4ReleaseDeadline; apres⟩ +instance (timer : State Unit → Fl4TimerView) : APresInst (fl4SetNextInterrupt (Z := Unit) timer) := + ⟨by unfold fl4SetNextInterrupt; apres⟩ +instance (timer : State Unit → Fl4TimerView) : APresInst (fl4ScAndTimer (Z := Unit) timer) := + ⟨by unfold fl4ScAndTimer; apres⟩ + + +theorem fl4ScAndTimer_apres (timer : State Unit → Fl4TimerView) : APres (fl4ScAndTimer (Z := Unit) timer) := + (inferInstance : APresInst (fl4ScAndTimer (Z := Unit) timer)).pres + +/-- **Every outcome of `fl4Schedule` has `schedulerAction = .resumeCurThread`.** -/ +theorem fl4Schedule_actA (limit : Nat) (timer : State Unit → Fl4TimerView) {σ τ : State Unit} + (h : ((), τ) ∈ (fl4Schedule (Z := Unit) limit timer σ).1) : τ.schedulerAction = .resumeCurThread := by + unfold fl4Schedule at h + obtain ⟨y1, -, h⟩ := mem_bindA.1 h + obtain ⟨y2, -, h⟩ := mem_bindA.1 h + obtain ⟨y3, -, h⟩ := mem_bindA.1 h + obtain ⟨y4, h4, h⟩ := mem_bindA.1 h + have hk : y4.2.schedulerAction = .resumeCurThread := by + unfold fl4SelectThread at h4 + obtain ⟨y5, -, h5⟩ := mem_bindA.1 h4 + have e : y4 = ((), { y5.2 with schedulerAction := .resumeCurThread }) := h5 + rw [e] + have := fl4ScAndTimer_apres timer y4.2 ((), τ) h + exact this.1.trans hk + +theorem actOfA_resume {w : U64} (h : actOfA w = .resumeCurThread) : w = 0#u64 := by + unfold actOfA at h + by_cases h0 : w = 0#u64 + · exact h0 + · rw [if_neg h0] at h + by_cases h1 : w = 1#u64 + · rw [if_pos h1] at h; cases h + · rw [if_neg h1] at h; cases h + +/-- **A related state at `resumeCurThread` has action word `0`** (`SchedRelA.act`). -/ +theorem YieldQueueRelA.actionW_zero {S : Type} {st : machine.Store S} [L : LawfulStore st] {Z : XSetsA} + {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : YieldQueueRelA (st := st) Z m κ s σ) (h : σ.schedulerAction = .resumeCurThread) : + L.actionW s = 0#u64 := by + have e := H.sched.sch.act + rw [h] at e + exact actOfA_resume e.symm + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3SchedActivateA.lean b/proofs/Fl4ProofsA64/P3/Stage3SchedActivateA.lean new file mode 100644 index 000000000..687f6df16 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SchedActivateA.lean @@ -0,0 +1,517 @@ +/- +P3 (aarch64, A64-P3 Stage 3, T3 G4b / F8): **F8 across `activate_thread`**. + +`TKeepA σ σ'`: the current thread, queues and idle thread are kept; the scheduler action is kept or becomes +`chooseNewThread` (`setThreadStateAct`); every TCB keeps its bound SC and a blocked state is not new; every SC object +stays one of the same activity. `fl4ActivateThread` (`fl4CompleteYieldTo`: SC consumed time, the yield link, message +registers; `fl4ActivateState`: `restart → running`) satisfies it, so it keeps `FIA` (`FIA.ofTKeep`, +`activate_fia`): the activation never needs `schedulable` for F8's sake (if `setThreadStateAct` flips the action, the +`act` clause is vacuous). A class `TPresInst` (leaves of the activation) parallels `P3/Stage3SchedActResA`. + +No premise; new module. +-/ +import Fl4ProofsA64.P3.Stage3SchedExitA +import Fl4ProofsA64.P3.SpecFrameSchedA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Classical + +noncomputable section + +def TKeepA (σ σ' : State Unit) : Prop := + σ'.curThread = σ.curThread ∧ σ'.releaseQueue = σ.releaseQueue ∧ σ'.readyQueues = σ.readyQueues ∧ + σ'.idleThread = σ.idleThread ∧ + σ'.schedulerAction = σ.schedulerAction ∧ + (∀ t x', σ'.kheap t = some (.tcb x') → ∃ x, σ.kheap t = some (.tcb x) ∧ + x'.tcbSchedContext = x.tcbSchedContext ∧ x'.tcbState = x.tcbState) ∧ + (∀ t x, σ.kheap t = some (.tcb x) → ∃ x', σ'.kheap t = some (.tcb x') ∧ + x'.tcbSchedContext = x.tcbSchedContext) ∧ + (∀ p y k, σ.kheap p = some (.schedContext y k) → + ∃ y', σ'.kheap p = some (.schedContext y' k) ∧ scActive y' = scActive y) ∧ σ'.curSc = σ.curSc + +instance : PreO TKeepA where + refl _ := ⟨rfl, rfl, rfl, rfl, rfl, fun _ x h => ⟨x, h, rfl, rfl⟩, fun _ x h => ⟨x, h, rfl⟩, + ⟨fun _ y _ h => ⟨y, h, rfl⟩, rfl⟩⟩ + trans {a b c} h1 h2 := by + obtain ⟨c1, r1, q1, i1, a1, tb1, tf1, s1, cs1⟩ := h1 + obtain ⟨c2, r2, q2, i2, a2, tb2, tf2, s2, cs2⟩ := h2 + refine ⟨c2.trans c1, r2.trans r1, q2.trans q1, i2.trans i1, ?_, fun t x'' hx'' => ?_, fun t x hx => ?_, + ⟨fun p y k hp => ?_, cs2.trans cs1⟩⟩ + · exact a2.trans a1 + · obtain ⟨x', e', sc2, st2⟩ := tb2 t x'' hx'' + obtain ⟨x, e, sc1, st1⟩ := tb1 t x' e' + exact ⟨x, e, sc2.trans sc1, st2.trans st1⟩ + · obtain ⟨x', e', sc1⟩ := tf1 t x hx + obtain ⟨x'', e'', sc2⟩ := tf2 t x' e' + exact ⟨x'', e'', sc2.trans sc1⟩ + · obtain ⟨y1, e1, a1⟩ := s1 p y k hp + obtain ⟨y2, e2, a2⟩ := s2 p y1 k e1 + exact ⟨y2, e2, a2.trans a1⟩ + +/-- A TCB function keeping the bound SC and the thread state. -/ +class TcbScFn (f : Tcb → Tcb) : Prop where + keep : ∀ x, (f x).tcbSchedContext = x.tcbSchedContext ∧ (f x).tcbState = x.tcbState + +instance (g : Option ObjRef → Option ObjRef) : TcbScFn (tcbYieldToUpdate g) := ⟨fun _ => ⟨rfl, rfl⟩⟩ + +theorem setObjTcb_tkeep {σ : State Unit} {p : ObjRef} {x x' : Tcb} (hx : σ.kheap p = some (.tcb x)) + (h1 : x'.tcbSchedContext = x.tcbSchedContext) (h2 : x'.tcbState = x.tcbState) : + TKeepA σ { σ with kheap := Function.update σ.kheap p (some (.tcb x')) } := by + refine ⟨rfl, rfl, rfl, rfl, rfl, fun q y' hq => ?_, fun q y hq => ?_, ⟨fun q y k hq => ?_, rfl⟩⟩ + · by_cases e : q = p + · subst e + have hq' : x' = y' := by simpa using hq + subst hq' + exact ⟨x, hx, h1, h2⟩ + · exact ⟨y', by simpa [Function.update_of_ne e] using hq, rfl, rfl⟩ + · by_cases e : q = p + · subst e + rw [hx] at hq + have hh := Option.some.inj hq + injection hh with hh + subst hh + exact ⟨x', by simp, h1⟩ + · exact ⟨y, by simpa [Function.update_of_ne e] using hq, rfl⟩ + · by_cases e : q = p + · subst e + rw [hx] at hq + cases hq + · exact ⟨y, by simpa [Function.update_of_ne e] using hq, rfl⟩ + +theorem setObjSc_tkeep {σ : State Unit} {p : ObjRef} {y y' : SchedContext} {n : Nat} + (hy : σ.kheap p = some (.schedContext y n)) (h : scActive y' = scActive y) : + TKeepA σ { σ with kheap := Function.update σ.kheap p (some (.schedContext y' n)) } := by + refine ⟨rfl, rfl, rfl, rfl, rfl, fun q x' hq => ?_, fun q x hq => ?_, ⟨fun q z k hq => ?_, rfl⟩⟩ + · by_cases e : q = p + · subst e; simp at hq + · exact ⟨x', by simpa [Function.update_of_ne e] using hq, rfl, rfl⟩ + · by_cases e : q = p + · subst e; rw [hy] at hq; cases hq + · exact ⟨x, by simpa [Function.update_of_ne e] using hq, rfl⟩ + · by_cases e : q = p + · subst e + rw [hy] at hq + have hh := Option.some.inj hq + injection hh with e1 e2 + subst e1; subst e2 + exact ⟨y', by simp, h⟩ + · exact ⟨z, by simpa [Function.update_of_ne e] using hq, rfl⟩ + +/-- Preservation of `TKeepA` by a spec computation. -/ +abbrev TPres {α : Type} (m : SMonad Unit α) : Prop := PresR TKeepA m + +class TPresInst {α : Type} (m : SMonad Unit α) : Prop where + pres : TPres m + +instance {α β : Type} {m : SMonad Unit α} {f : α → SMonad Unit β} [hm : TPresInst m] + [hf : ∀ a, TPresInst (f a)] : TPresInst (m >>= f) := + ⟨PresR.bind hm.pres fun a => (hf a).pres⟩ +instance {α : Type} (a : α) : TPresInst (pure a : SMonad Unit α) := ⟨PresR.pure a⟩ +instance {α : Type} (f : State Unit → α) : TPresInst (NonDet.gets f : SMonad Unit α) := ⟨PresR.gets f⟩ +instance : TPresInst (NonDet.get : SMonad Unit (State Unit)) := ⟨PresR.get⟩ +instance {α : Type} (f : State Unit → Option α) : TPresInst (NonDet.getsThe f : SMonad Unit α) := + ⟨PresR.getsThe f⟩ +instance {α : Type} (o : Option α) : TPresInst (NonDet.assertOpt o : SMonad Unit α) := ⟨PresR.assertOpt o⟩ +instance (P : Prop) : TPresInst (NonDet.assert P : SMonad Unit Unit) := ⟨PresR.assert P⟩ +instance {α : Type} : TPresInst (NonDet.fail : SMonad Unit α) := ⟨PresR.fail⟩ +instance {P : Prop} {d : Decidable P} {m : SMonad Unit Unit} [h : TPresInst m] : + TPresInst (@NonDet.when (State Unit) P d m) := ⟨PresR.when h.pres⟩ +instance {P : Prop} {d : Decidable P} {m : SMonad Unit Unit} [h : TPresInst m] : + TPresInst (@NonDet.unless (State Unit) P d m) := ⟨PresR.unless' h.pres⟩ +instance {α : Type} {P : Prop} {d : Decidable P} {a b : SMonad Unit α} [ha : TPresInst a] [hb : TPresInst b] : + TPresInst (@ite _ P d a b) := ⟨PresR.ite' ha.pres hb.pres⟩ +instance {β γ : Type} {f : γ → SMonad Unit β} [hf : ∀ a, TPresInst (f a)] (l : List γ) : + TPresInst (NonDet.mapM f l) := ⟨PresR.mapM (fun a => (hf a).pres) l⟩ +instance {α : Type} {C : α → State Unit → Prop} {B : α → SMonad Unit α} [hB : ∀ a, TPresInst (B a)] (r : α) : + TPresInst (NonDet.whileLoop C B r) := ⟨PresR.whileLoop (fun a => (hB a).pres) r⟩ +instance {α : Type} (S : Set α × Prop) : TPresInst (NonDet.selectF S : SMonad Unit α) := ⟨PresR.selectF S⟩ +instance {α : Type} {f : α → SMonad Unit Unit} [hf : ∀ a, TPresInst (f a)] (o : Option α) : + TPresInst (NonDet.maybeM f o) := by + cases o + · exact ⟨PresR.pure _⟩ + · exact hf _ + +/-- A state modification off the scheduler action. -/ +theorem TPres.modify_fix (f : State Unit → State Unit) (h : ∀ σ, TKeepA σ (f σ)) : + TPres (NonDet.modify f : SMonad Unit Unit) := PresR.modify f h + +macro "tpres" : tactic => `(tactic| + repeat' (first + | exact TPresInst.pres + | refine PresR.bind_pure _ _ ?_ + | refine PresR.bind ?_ (fun _ => ?_) + | refine PresR.when ?_ + | refine PresR.unless' ?_ + | refine PresR.ite' ?_ ?_ + | (refine TPres.modify_fix _ (fun _ => ⟨rfl, rfl, rfl, rfl, rfl, fun _ x h => ⟨x, h, rfl, rfl⟩, fun _ x h => ⟨x, h, rfl⟩, ⟨fun _ y _ h => ⟨y, h, rfl⟩, rfl⟩⟩)) + | dsimp only + | split)) + +/-! ## The writing primitives -/ + +theorem threadSet_tpres (f : Tcb → Tcb) [TcbScFn f] (t : ObjRef) : TPres (threadSet (Z := Unit) f t) := by + intro σ r hr + obtain ⟨y1, hy1, h⟩ := hr + have hy1' : y1 ∈ (NonDet.assertOpt (getTcb (Z := Unit) t σ) σ).1 := hy1 + cases hg : getTcb (Z := Unit) t σ with + | none => rw [hg] at hy1'; exact absurd hy1' (Set.notMem_empty _) + | some x => + rw [hg] at hy1' + have e1 : y1 = (x, σ) := hy1' + subst e1 + obtain ⟨ko, hko, hr2⟩ := setObject_memA h + rw [hr2] + exact setObjTcb_tkeep (getTcb_kheapA hg) (TcbScFn.keep x).1 (TcbScFn.keep x).2 + +instance (f : Tcb → Tcb) [TcbScFn f] (t : ObjRef) : TPresInst (threadSet (Z := Unit) f t) := ⟨threadSet_tpres f t⟩ + +theorem updateSchedContext_tpres (f : SchedContext → SchedContext) [ScActFn f] (p : ObjRef) : + TPres (updateSchedContext (Z := Unit) p f) := by + intro σ r hr + obtain ⟨y1, hy1, h⟩ := hr + have hy1' : y1 ∈ (NonDet.assertOpt (σ.kheap p) σ).1 := hy1 + cases hk : σ.kheap p with + | none => rw [hk] at hy1'; exact absurd hy1' (Set.notMem_empty _) + | some ko => + rw [hk] at hy1' + have e1 : y1 = (ko, σ) := hy1' + subst e1 + cases ko with + | schedContext sc n => + obtain ⟨ko', hko, hr2⟩ := setObject_memA h + rw [hr2] + exact setObjSc_tkeep hk (ScActFn.act sc) + | _ => exact absurd h (Set.notMem_empty _) + +instance (p : ObjRef) (f : SchedContext → SchedContext) [ScActFn f] : + TPresInst (updateSchedContext (Z := Unit) p f) := ⟨updateSchedContext_tpres f p⟩ + +theorem asUser_tpres {α : Type} (t : ObjRef) (f : UserMonad α) : TPres (asUser (Z := Unit) t f) := by + intro σ r hr + obtain ⟨y1, hy1, hr⟩ := hr + have hy1' : y1 ∈ (NonDet.assertOpt (getTcb (Z := Unit) t σ) σ).1 := hy1 + cases hg : getTcb (Z := Unit) t σ with + | none => rw [hg] at hy1'; exact absurd hy1' (Set.notMem_empty _) + | some x => + rw [hg] at hy1' + have e1 : y1 = (x, σ) := hy1' + subst e1 + obtain ⟨y2, hy2, hr⟩ := hr + have e2 : y2 = (archTcbContextGet x.tcbArch, σ) := hy2 + subst e2 + obtain ⟨⟨⟨a, uc'⟩, s3⟩, hy3, hr⟩ := hr + have e3 : s3 = σ := hy3.2 + subst e3 + obtain ⟨y4, hy4, hr⟩ := hr + have e4 : y4 = ({ x with tcbArch := archTcbContextSet uc' x.tcbArch }, s3) := hy4 + subst e4 + obtain ⟨y5, hy5, hr⟩ := hr + obtain ⟨ko, hko, hr5⟩ := setObject_memA hy5 + have hx := getTcb_kheapA hg + have e6 : r = (a, y5.2) := hr + rw [e6, hr5] + exact setObjTcb_tkeep hx rfl rfl + +instance {α : Type} (t : ObjRef) (f : UserMonad α) : TPresInst (asUser (Z := Unit) t f) := ⟨asUser_tpres t f⟩ + +theorem setMrs_tpres (thread : ObjRef) (buf : Option ObjRef) (msgs : List Message) + (h : msgs.length ≤ msg_registers.length) : TPres (setMrs (Z := Unit) thread buf msgs) := by + intro σ r hr + obtain ⟨y1, hy1, hr⟩ := hr + have hy1' : y1 ∈ (NonDet.assertOpt (getTcb (Z := Unit) thread σ) σ).1 := hy1 + cases hg : getTcb (Z := Unit) thread σ with + | none => rw [hg] at hy1'; exact absurd hy1' (Set.notMem_empty _) + | some x => + rw [hg] at hy1' + have e1 : y1 = (x, σ) := hy1' + subst e1 + obtain ⟨y2, hy2, hr⟩ := hr + obtain rfl := mem_pureA hy2 + obtain ⟨y3, hy3, hr⟩ := hr + obtain rfl := mem_pureA hy3 + obtain ⟨y4, hy4, hr⟩ := hr + obtain ⟨ko, hko, hr4⟩ := setObject_memA hy4 + have hx := getTcb_kheapA hg + have F : TKeepA σ y4.2 := by + rw [hr4]; exact setObjTcb_tkeep hx rfl rfl + obtain ⟨y5, hy5, hr⟩ := hr + obtain rfl := mem_pureA hy5 + have hd : msgs.drop msg_registers.length = [] := List.drop_eq_nil_of_le h + have hr2 : r.2 = y4.2 := by + cases buf with + | none => rw [mem_pureA hr] + | some pptr => + obtain ⟨y6, hy6, hr⟩ := hr + dsimp only at hy6 + rw [hd, zipWithMx_nil_rightA] at hy6 + obtain rfl := mem_pureA hy6 + rw [mem_pureA hr] + rw [hr2]; exact F + +theorem doMachineOp_tpres {α : Type} (mop : NonDet MachineState α) : TPres (doMachineOp (Z := Unit) mop) := by + intro σ r hr + obtain ⟨y1, hy1, hr⟩ := hr + have e1 : y1 = (σ.machineState, σ) := hy1 + subst e1 + obtain ⟨⟨⟨a, ms'⟩, s2⟩, hy2, hr⟩ := hr + have e2 : s2 = σ := hy2.2 + subst e2 + obtain ⟨y3, hy3, hr⟩ := hr + have e3 : y3 = ((), { s2 with machineState := ms' }) := hy3 + subst e3 + have e4 : r = (a, { s2 with machineState := ms' }) := hr + rw [e4] + exact ⟨rfl, rfl, rfl, rfl, rfl, fun _ x h => ⟨x, h, rfl, rfl⟩, fun _ x h => ⟨x, h, rfl⟩, + ⟨fun _ y _ h => ⟨y, h, rfl⟩, rfl⟩⟩ + +instance {α : Type} (mop : NonDet MachineState α) : TPresInst (doMachineOp (Z := Unit) mop) := ⟨doMachineOp_tpres mop⟩ + +/-! ## Readers and small writers -/ + +instance {α : Type} (f : Tcb → α) (t : ObjRef) : TPresInst (threadGet (Z := Unit) f t) := + ⟨by unfold threadGet; tpres⟩ +instance (f : Tcb → Option ObjRef) (r : ObjRef) : TPresInst (getTcbObjRef (Z := Unit) f r) := + ⟨by unfold getTcbObjRef; tpres⟩ +instance (r : ObjRef) : TPresInst (getThreadState (Z := Unit) r) := ⟨by unfold getThreadState; tpres⟩ +instance (d : Domain) (p : Priority) : TPresInst (getTcbQueue (Z := Unit) d p) := ⟨by unfold getTcbQueue; tpres⟩ + +instance (p : ObjRef) : TPresInst (getObject (Z := Unit) p) := ⟨by unfold getObject; tpres⟩ +instance (p : ObjRef) : TPresInst (getSchedContext (Z := Unit) p) := ⟨by unfold getSchedContext; tpres⟩ +instance (f : SchedContext → Option ObjRef) (p : ObjRef) : TPresInst (getScObjRef (Z := Unit) f p) := + ⟨by unfold getScObjRef; tpres⟩ +instance (p : ObjRef) : TPresInst (getRefills (Z := Unit) p) := ⟨by unfold getRefills; tpres⟩ +instance (p : ObjRef) : TPresInst (getRefillHead (Z := Unit) p) := ⟨by unfold getRefillHead; tpres⟩ +instance (p : ObjRef) : TPresInst (getRefillTail (Z := Unit) p) := ⟨by unfold getRefillTail; tpres⟩ +instance (p : ObjRef) : TPresInst (getScRefillReady (Z := Unit) p) := ⟨by unfold getScRefillReady; tpres⟩ +instance (p : ObjRef) (u : Ticks) : TPresInst (getScRefillSufficient (Z := Unit) p u) := + ⟨by unfold getScRefillSufficient; tpres⟩ +instance (p : ObjRef) (u : Ticks) : TPresInst (fl4GetScRefillSufficient (Z := Unit) p u) := + ⟨by unfold fl4GetScRefillSufficient; tpres⟩ +instance (p : ObjRef) : TPresInst (getScActive (Z := Unit) p) := ⟨by unfold getScActive; tpres⟩ +instance (p : ObjRef) : TPresInst (isRoundRobin (Z := Unit) p) := ⟨by unfold isRoundRobin; tpres⟩ + +/-! ## The activation leaves -/ + +instance (thread : ObjRef) (buf : Option ObjRef) (c : Time) : + TPresInst (setMrs (Z := Unit) thread buf (wordsFromTime c)) := + ⟨setMrs_tpres _ _ _ (by simp [wordsFromTime, msg_registers, msgRegisters, Register.x2ToX5])⟩ +instance (thread : ObjRef) (l : Data) (c : Time) : TPresInst (replyFromKernel (Z := Unit) thread (l, wordsFromTime c)) := + ⟨by unfold replyFromKernel setMessageInfo; tpres⟩ +instance (lookup : Bool) (t : ObjRef) : TPresInst (lookupIpcBuffer (Z := Unit) lookup t) := + ⟨by unfold lookupIpcBuffer; tpres⟩ +instance (scPtr : ObjRef) (cap : Ticks) (toUs : Ticks → Word64) : + TPresInst (schedContextUpdateConsumedWith (Z := Unit) cap toUs scPtr) := + ⟨by unfold schedContextUpdateConsumedWith; tpres⟩ +instance (t : ObjRef) : TPresInst (schedContextCancelYieldTo (Z := Unit) t) := + ⟨by unfold schedContextCancelYieldTo; tpres⟩ +instance (cap : Ticks) (toUs : Ticks → Word64) (t : ObjRef) : + TPresInst (completeYieldToWith (Z := Unit) cap toUs t) := ⟨by unfold completeYieldToWith; tpres⟩ +instance (t : ObjRef) : TPresInst (fl4CompleteYieldTo (Z := Unit) t) := ⟨by unfold fl4CompleteYieldTo; tpres⟩ + +instance (t : ObjRef) : TPresInst (archActivateIdleThread (Z := Unit) t) := + ⟨by unfold archActivateIdleThread; tpres⟩ +theorem FIA.ofTKeep {σ σ' : State Unit} (h : FIA σ) (k : TKeepA σ σ') : FIA σ' := by + obtain ⟨kc, kr, kq, ki, ka, kb, kf, ks, kcs⟩ := k + have act : ∀ p, ScActiveAtA σ p → ScActiveAtA σ' p := fun p ⟨y, k, e, a⟩ => by + obtain ⟨y', e', a'⟩ := ks p y k e + exact ⟨y', k, e', a'.trans a⟩ + have actT : ∀ t, ActTcbA σ t → ActTcbA σ' t := fun t ⟨x, sc, e, es, a⟩ => by + obtain ⟨x', e', sc'⟩ := kf t x e + exact ⟨x', sc, e', sc'.trans es, act sc a⟩ + refine ⟨⟨fun d p t hm => ?_, fun e1 e2 => ?_, fun t x sc e1 e2 e3 => ?_⟩, fun t ht => ?_⟩ + · rw [kq] at hm + obtain ⟨a, n⟩ := h.1.rq d p t hm + exact ⟨actT t a, by rw [kr]; exact n⟩ + · rw [ka] at e1 + rw [kc, ki] at e2 + obtain ⟨a, n⟩ := h.1.act e1 e2 + rw [kc] + exact ⟨actT _ a, by rw [kr]; exact n⟩ + · obtain ⟨x0, e0, sc0, st0⟩ := kb t x e1 + exact act sc (h.1.blk t x0 sc e0 (by rw [← st0]; exact e2) (by rw [← sc0]; exact e3)) + · rw [kr] at ht + exact actT t (h.2 t ht) + +/-! ## The restart arm by hand: `setThreadState ct .running` and the activation's action -/ + +theorem ActTcbA.ofTKeep {σ σ' : State Unit} {t : ObjRef} (h : ActTcbA σ t) (k : TKeepA σ σ') : ActTcbA σ' t := by + obtain ⟨kc, kr, kq, ki, ka, kb, kf, ks, kcs⟩ := k + obtain ⟨x, sc, e, es, y, n, ey, ea⟩ := h + obtain ⟨x', e', sc'⟩ := kf t x e + obtain ⟨y', ey', ea'⟩ := ks sc y n ey + exact ⟨x', sc, e', sc'.trans es, y', n, ey', ea'.trans ea⟩ + +theorem TgtA.ofTKeep {σ σ' : State Unit} {t : ObjRef} (h : TgtA t σ) (k : TKeepA σ σ') : TgtA t σ' := + ⟨h.1.ofTKeep k, by rw [k.2.1]; exact h.2⟩ + +theorem schedulable_of_act {σ : State Unit} {t : ObjRef} (a : ActTcbA σ t) (n : t ∉ σ.releaseQueue) + (hr : ∃ x, σ.kheap t = some (.tcb x) ∧ runnable x.tcbState = true) : schedulable t σ = true := by + obtain ⟨x, sc, e, es, y, k, ey, ea⟩ := a + obtain ⟨x', e', hrun⟩ := hr + rw [e] at e' + have hx : x = x' := by injection e' with h; injection h + subst hx + unfold schedulable + apply decide_eq_true + refine ⟨?_, ?_, n⟩ + · simp [optPred, optMapSome, optMap, tcbsOf, tcbsOfKh, tcbOf, e, hrun] + · have hs : scsOf σ sc = some y := by unfold scsOf scsOfKh optMap; rw [ey]; rfl + simp [optPred, optMap, tcbsOf, tcbsOfKh, tcbOf, e, es, hs, ea] + +/-- `FIA` across the replacement of one TCB by one with the same SC (a blocked state only if it was already there). -/ +theorem FIA.ofTcbUpd {σ : State Unit} {p : ObjRef} {x x' : Tcb} (h : FIA σ) (hx : σ.kheap p = some (.tcb x)) + (h1 : x'.tcbSchedContext = x.tcbSchedContext) (h2 : BlockedTsA x'.tcbState = true → x'.tcbState = x.tcbState) : + FIA { σ with kheap := Function.update σ.kheap p (some (.tcb x')) } := by + have scK : ∀ q y k, σ.kheap q = some (.schedContext y k) → + Function.update σ.kheap p (some (.tcb x')) q = some (.schedContext y k) := fun q y k e => by + by_cases hq : q = p + · subst hq; rw [hx] at e; cases e + · simpa [Function.update_of_ne hq] using e + have act : ∀ q, ScActiveAtA σ q → ScActiveAtA { σ with kheap := Function.update σ.kheap p (some (.tcb x')) } q := + fun q ⟨y, k, e, a⟩ => ⟨y, k, scK q y k e, a⟩ + have actT : ∀ t, ActTcbA σ t → ActTcbA { σ with kheap := Function.update σ.kheap p (some (.tcb x')) } t := + fun t ⟨z, sc, e, es, a⟩ => by + by_cases hq : t = p + · subst hq + rw [hx] at e + have hz := Option.some.inj e + injection hz with hz + subst hz + exact ⟨x', sc, by simp, h1.trans es, act sc a⟩ + · exact ⟨z, sc, by simpa [Function.update_of_ne hq] using e, es, act sc a⟩ + refine ⟨⟨fun d p' t hm => ?_, fun e1 e2 => ?_, fun t z sc e1 e2 e3 => ?_⟩, fun t ht => ?_⟩ + · obtain ⟨a, n⟩ := h.1.rq d p' t hm + exact ⟨actT t a, n⟩ + · obtain ⟨a, n⟩ := h.1.act e1 e2 + exact ⟨actT _ a, n⟩ + · by_cases hq : t = p + · subst hq + have hz : x' = z := by simpa using e1 + subst hz + exact act sc (h.1.blk t x sc hx (by rw [← h2 e2]; exact e2) (by rw [← h1]; exact e3)) + · exact act sc (h.1.blk t z sc (by simpa [Function.update_of_ne hq] using e1) e2 e3) + · exact actT t (h.2 t ht) + +/-- The pre-state facts about the thread `ct` the restart arm uses. -/ +def XA (ct : ObjRef) (σ : State Unit) : Prop := + ct = σ.curThread ∧ TgtA ct σ ∧ ∃ x, σ.kheap ct = some (.tcb x) + +theorem XA.ofTKeep {ct : ObjRef} {σ σ' : State Unit} (h : XA ct σ) (k : TKeepA σ σ') : XA ct σ' := by + obtain ⟨hc, ht, x, e⟩ := h + obtain ⟨x', e', -⟩ := k.2.2.2.2.2.2.1 ct x e + exact ⟨hc.trans k.1.symm, ht.ofTKeep k, x', e'⟩ + +theorem HT.fail {α : Type} {P : State Unit → Prop} {Q : α → State Unit → Prop} : + HT P (NonDet.fail : SMonad Unit α) Q := fun σ _ r hr => absurd hr (Set.notMem_empty _) + +/-- **`setThreadState ct .running` keeps `FIA` and, when `ct` is the current schedulable thread, the action.** -/ +theorem setThreadStateRun_ht (ct : ObjRef) : + HT (fun σ => FIA σ ∧ σ.schedulerAction = .resumeCurThread ∧ XA ct σ) + (setThreadState (Z := Unit) ct .running) (fun _ τ => FIA τ ∧ τ.schedulerAction = .resumeCurThread) := by + unfold Fl4SpecA64.setThreadState Fl4SpecA64.setThreadStateAct + refine HT.bind (Q := fun _ τ => FIA τ ∧ τ.schedulerAction = .resumeCurThread ∧ XA ct τ ∧ + ∃ x, τ.kheap ct = some (.tcb x) ∧ x.tcbState = .running) ?_ fun _ => ?_ + · intro σ hσ r hr + obtain ⟨hI, hA, hct, hT, x0, e0⟩ := hσ + unfold Fl4SpecA64.threadSet at hr + obtain ⟨y1, hy1, h⟩ := mem_bindA.1 hr + change y1 ∈ (NonDet.assertOpt (getTcb (Z := Unit) ct σ) σ).1 at hy1 + cases hg : getTcb (Z := Unit) ct σ with + | none => rw [hg] at hy1; exact absurd hy1 (Set.notMem_empty _) + | some x => + rw [hg] at hy1 + have e1 : y1 = (x, σ) := hy1 + subst e1 + obtain ⟨ko, hko, hr2⟩ := setObject_memA h + rw [hr2] + have hx := getTcb_kheapA hg + have hF := hI.ofTcbUpd hx (x' := { x with tcbState := .running }) rfl (fun hb => by cases hb) + refine ⟨hF, hA, ⟨hct, ?_, ⟨{ x with tcbState := .running }, by simp⟩⟩, { x with tcbState := .running }, by simp, rfl⟩ + obtain ⟨⟨z, sc, ez, esz, a⟩, n⟩ := hT + rw [hx] at ez + have hz := Option.some.inj ez + injection hz with hz + subst hz + obtain ⟨y, k, ey, ea⟩ := a + refine ⟨⟨{ x with tcbState := .running }, sc, by simp, esz, y, k, ?_, ea⟩, n⟩ + by_cases hq : sc = ct + · subst hq; rw [hx] at ey; cases ey + · simpa [Function.update_of_ne hq] using ey + · refine HT.bind (Q := fun _ τ => FIA τ ∧ τ.schedulerAction = .resumeCurThread ∧ XA ct τ ∧ + ∃ x, τ.kheap ct = some (.tcb x) ∧ x.tcbState = .running) (HT.gets _ fun σ hσ => hσ) fun cur => ?_ + refine HT.bind (Q := fun _ τ => FIA τ ∧ τ.schedulerAction = .resumeCurThread ∧ XA ct τ ∧ + ∃ x, τ.kheap ct = some (.tcb x) ∧ x.tcbState = .running) (HT.gets _ fun σ hσ => hσ) fun act => ?_ + refine HT.bind (Q := fun b τ => (FIA τ ∧ τ.schedulerAction = .resumeCurThread ∧ XA ct τ ∧ + ∃ x, τ.kheap ct = some (.tcb x) ∧ x.tcbState = .running) ∧ b = schedulable ct τ) + (HT.gets _ fun σ hσ => ⟨hσ, rfl⟩) fun sched => ?_ + refine HT.when (fun hc σ hσ r hr => ?_) (fun hn σ hσ => ⟨hσ.1.1, hσ.1.2.1⟩) + exfalso + obtain ⟨⟨hI, hA, hX, x, ex, hrun⟩, hb⟩ := hσ + apply hc.2.2 + rw [hb] + exact schedulable_of_act hX.2.1.1 hX.2.1.2 ⟨x, ex, by rw [hrun]; rfl⟩ + +theorem liftX {α : Type} {m : SMonad Unit α} (σ0 : State Unit) (ct : ObjRef) (h : TPres m) : + HT (fun σ => (TKeepA σ0 σ ∧ FIA σ) ∧ XA ct σ) m (fun _ τ => (TKeepA σ0 τ ∧ FIA τ) ∧ XA ct τ) := + fun σ hσ r hr => + let k := h σ r hr + ⟨⟨PreO.trans hσ.1.1 k, FIA.ofTKeep hσ.1.2 k⟩, XA.ofTKeep hσ.2 k⟩ + +/-- **`activate_thread` keeps `FIA` and the action `resumeCurThread`** from a state at `resumeCurThread` whose current +thread, if it is restarting, has an active SC and is outside the release queue (`hrs`: F8's `act` clause gives it). -/ +theorem activate_act (σ0 : State Unit) (hres : σ0.schedulerAction = .resumeCurThread) + (hrs : ∀ x, σ0.kheap σ0.curThread = some (.tcb x) → x.tcbState = .restart → TgtA σ0.curThread σ0) : + HT (fun σ => σ = σ0 ∧ FIA σ) (fl4ActivateThread (Z := Unit)) + (fun _ τ => FIA τ ∧ τ.schedulerAction = .resumeCurThread) := by + unfold Fl4SpecA64.fl4ActivateThread Fl4SpecA64.getTcbObjRef + have fin : ∀ σ, (TKeepA σ0 σ ∧ FIA σ) → FIA σ ∧ σ.schedulerAction = .resumeCurThread := + fun σ h => ⟨h.2, h.1.2.2.2.2.1.trans hres⟩ + refine HT.bind (Q := fun ct σ => (TKeepA σ0 σ ∧ FIA σ) ∧ ct = σ.curThread) + (HT.gets _ fun σ hσ => ⟨by obtain ⟨rfl, h⟩ := hσ; exact ⟨PreO.refl _, h⟩, rfl⟩) fun ct => ?_ + refine HT.bind (Q := fun _ σ => (TKeepA σ0 σ ∧ FIA σ) ∧ ct = σ.curThread) + (HT.threadGet _ _ fun σ hσ x hx => hσ) fun yt => ?_ + refine HT.bind (Q := fun _ σ => (TKeepA σ0 σ ∧ FIA σ) ∧ ct = σ.curThread) + (HT.when (fun _ σ hσ r hr => ?_) (fun _ σ hσ => hσ)) fun _ => ?_ + · have k := (inferInstance : TPresInst (fl4CompleteYieldTo (Z := Unit) ct)).pres σ r hr + exact ⟨⟨PreO.trans hσ.1.1 k, FIA.ofTKeep hσ.1.2 k⟩, hσ.2.trans k.1.symm⟩ + unfold Fl4SpecA64.fl4ActivateState + refine HT.bind (Q := fun st σ => ((TKeepA σ0 σ ∧ FIA σ) ∧ ct = σ.curThread) ∧ + ∃ x, σ.kheap ct = some (.tcb x) ∧ x.tcbState = st) ?_ fun st => ?_ + · unfold Fl4SpecA64.getThreadState + exact HT.threadGet _ ct fun σ hσ x hx => ⟨hσ, x, hx, rfl⟩ + have restartCase : HT (fun σ => ((TKeepA σ0 σ ∧ FIA σ) ∧ ct = σ.curThread) ∧ + ∃ x, σ.kheap ct = some (.tcb x) ∧ x.tcbState = .restart) + (show SMonad Unit Unit from do + let pc ← asUser (Z := Unit) ct getRestartPC + asUser (Z := Unit) ct (setNextPC pc) + setThreadState (Z := Unit) ct .running) + (fun _ τ => FIA τ ∧ τ.schedulerAction = .resumeCurThread) := by + have pre : ∀ σ, (((TKeepA σ0 σ ∧ FIA σ) ∧ ct = σ.curThread) ∧ + ∃ x, σ.kheap ct = some (.tcb x) ∧ x.tcbState = .restart) → (TKeepA σ0 σ ∧ FIA σ) ∧ XA ct σ := by + intro σ hσ + obtain ⟨⟨hI, hct⟩, x, ex, hst⟩ := hσ + obtain ⟨x0, e0, sc0, st0⟩ := hI.1.2.2.2.2.2.1 ct x ex + have hc0 : ct = σ0.curThread := hct.trans hI.1.1 + have hT0 : TgtA σ0.curThread σ0 := hrs x0 (by rw [← hc0]; exact e0) (st0.symm.trans hst) + have hT : TgtA ct σ := by rw [hc0]; exact hT0.ofTKeep hI.1 + exact ⟨hI, hct, hT, x, ex⟩ + refine HT.bind (Q := fun _ σ => (TKeepA σ0 σ ∧ FIA σ) ∧ XA ct σ) + (HT.mono (liftX σ0 ct (inferInstance : TPresInst (asUser (Z := Unit) ct getRestartPC)).pres) pre + (fun _ _ h => h)) fun pc => ?_ + refine HT.bind (Q := fun _ σ => (TKeepA σ0 σ ∧ FIA σ) ∧ XA ct σ) + (liftX σ0 ct (inferInstance : TPresInst (asUser (Z := Unit) ct (setNextPC pc))).pres) fun _ => ?_ + exact HT.mono (setThreadStateRun_ht ct) (fun σ hσ => ⟨hσ.1.2, hσ.1.1.2.2.2.2.1.trans hres, hσ.2⟩) + (fun _ _ h => h) + cases st + all_goals dsimp only + all_goals first + | exact HT.fail + | exact restartCase + | exact HT.pure () (fun σ hσ => fin σ hσ.1.1) + | (unfold Fl4SpecA64.archActivateIdleThread; exact HT.pure () fun σ hσ => fin σ hσ.1.1) + | (rename_i e; cases e; exact HT.pure () fun σ hσ => fin σ hσ.1.1) + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3SchedAwakenA.lean b/proofs/Fl4ProofsA64/P3/Stage3SchedAwakenA.lean new file mode 100644 index 000000000..692e60162 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SchedAwakenA.lean @@ -0,0 +1,179 @@ +/- +P3 (aarch64, A64-P3 Stage-3 integration; queue item 1): **the release-walk footprint**. `awaken_loop_stepA` is a sibling of +`awaken_loop_refinesA` (`P3/ScAwakenA`, statement unchanged) that also exports `MFrA Z m s s' σ τ` (the walk writes only the +MCS words: `release_remove` and `possible_switch_to` are `MFrA` / `SWrA` steps, chained by `MFrA.trans`) and the spec frame +`awaken_presA` (`P3/SpecFrameSchedA`). + +No premise; new module. +-/ +import Fl4ProofsA64.P3.ScAwakenA +import Fl4ProofsA64.P3.SpecFrameSchedA +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3.Sched + +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4ProofsA64.P1.W7.R.ST + +noncomputable section + +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +set_option maxHeartbeats 600000 in +/-- The concrete release walk drains exactly the ready prefix. Its +strict fuel margin follows from placement and the queue's distinct +non-sentinel TCB addresses. -/ +theorem awaken_loop_stepA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {σ : State Unit} (H : YieldQueueRelA (st := st) Z m κ s σ) (idle : IdleThreadsA m σ) : + sched.awaken_loop st s layout.WALK_FUEL ⦃ s' => ∃ τ Q' R', + awaken (Z := Unit) σ = ({((), τ)}, False) ∧ + YieldQueueRelA (st := st) {Z with Q := Q', R := R'} m κ s' τ ∧ + τ.kheap = σ.kheap ∧ (∀ t ∈ τ.releaseQueue, t ∈ σ.releaseQueue) ∧ + MFrA (st := st) Z m s s' σ τ ⦄ := by + unfold sched.awaken_loop + apply loop.spec_decr_nat (measure := fun x => x.2.val) (inv := fun x => ∃ ρ Q R, + YieldQueueRelA (st := st) {Z with Q := Q, R := R} m κ x.1 ρ ∧ + ρ.releaseQueue.length < x.2.val ∧ awaken (Z := Unit) σ = awaken (Z := Unit) ρ ∧ + ρ.kheap = σ.kheap ∧ (∀ t ∈ ρ.releaseQueue, t ∈ σ.releaseQueue) ∧ + MFrA (st := st) Z m s x.1 σ ρ) + · rintro ⟨sk, fuel⟩ ⟨ρ, Q, R, Hk, len, loopEq, heap, sub, F⟩ + dsimp only at * + unfold sched.awaken_loop.body + have pos : fuel > 0#u64 := by rw [gt_iff_val]; simp; omega + rw [if_pos pos] + obtain ⟨prev, dec, val⟩ := spec_ex_fd (U64.sub_spec (x := fuel) (y := 1#u64) (by scalar_tac)) + rw [dec, bind_ok, L.cur_cpu_eq, bind_ok, eq_of_spec_fd (obj.node_get.spec sk _ _), bind_ok] + have rel := Hk.sched.core.release _ Hk.sched.sch.cpuCs + have words : L.mem sk (L.nodeAt sk (L.cpu sk) layout.node.RELEASE_HEAD) = (R (L.cpu sk)).headD 0#u64 := rel.head + rw [words] + cases list : R (L.cpu sk) with + | nil => + rw [eq_of_spec_fd (obj.Tcb.is_null.spec _)] + simp only [List.headD_nil, decide_true, bind_ok, ↓reduceIte, WP.spec_ok] + have empty : ρ.releaseQueue = [] := by + have e : ρ.releaseQueue = (R (L.cpu sk)).map specPtr := Hk.sched.rest.rel + rw [e, list] + rfl + refine ⟨ρ, Q, R, ?_, Hk, heap, sub, F⟩ + rw [loopEq] + unfold awaken + exact wl_doneSc (by rw [readReleaseEmptyA empty]; simp [the_someA]) + | cons t rest => + have tr : t ∈ R (L.cpu sk) := by rw [list]; exact List.mem_cons_self + have ht : t ∈ Z.T := rel.mem _ tr + have hon : L.mem sk (affA t) = L.cpu sk := Hk.sched.core.inR _ Hk.sched.sch.cpuCs _ tr + have t0 := (tcb_ne01Sc (Hk.sched.core.tcbs t ht)).1 + have null : obj.Tcb.is_null t = ok false := by + rw [eq_of_spec_fd (obj.Tcb.is_null.spec t)] + simp [t0] + simp only [List.headD_cons, null, bind_ok, Bool.false_eq_true, ↓reduceIte] + have q : ρ.releaseQueue = specPtr t :: rest.map specPtr := by + have e : ρ.releaseQueue = (R (L.cpu sk)).map specPtr := Hk.sched.rest.rel + rw [e, list] + rfl + have hm : specPtr t ∈ ρ.releaseQueue := by rw [q]; exact List.mem_cons_self + obtain ⟨x, p, y, n, hx, hsc, hy, active⟩ := Hk.sched.inv.relSc _ hm + obtain ⟨x', hx', hword⟩ := Hk.sched.sch.tcb t ht + rw [hx] at hx'; cases hx' + have c0 : L.mem sk (scA t) ≠ 0#u64 := by + intro e + rw [hword, optScA, if_pos e] at hsc + cases hsc + let sc := L.mem sk (scA t) + have scEq : p = scNameA m sc := by + rw [hword, optScA, if_neg c0] at hsc + exact (Option.some.inj hsc).symm + subst p + rw [eq_of_spec_fd (obj.Tcb.sc.spec sk t), bind_ok] + refine WP.spec_bind (ready_refinesA Hk.sched.scr Hk.sched.sch.time (Hk.sched.rest.scMem t ht c0) hy active) ?_ + intro b hb + rw [getScRefillReady_eqA hy (Hk.sched.inv.ne hy active)] at hb + have ready : refillReady ρ.curTime (refillHd y) = b := + (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst hb))).1 + have read := readReleaseHeadA q hx hsc hy (Hk.sched.inv.ne hy active) + cases b with + | false => + simp only [Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + refine ⟨ρ, Q, R, ?_, Hk, heap, sub, F⟩ + rw [loopEq] + unfold awaken + exact wl_doneSc (by rw [read, ready]; simp [the_someA]) + | true => + simp only [↓reduceIte] + let mid := releaseRemoveStA ρ (specPtr t) + have run : runnable x.tcbState = true := by + obtain ⟨w, hw, hr⟩ := Hk.sched.run.2 _ hm + rw [hx] at hw; cases hw; exact hr + have ni : NotIdleScA m (scNameA m sc) := by + refine Hk.sched.inv.tcbScNotIdle _ x _ hx hsc ?_ + intro c hc e + have no := idle c hc x (by rw [heap] at hx; rwa [← e]) + rw [no] at run + cases run + refine WP.spec_bind (release_remove_refines_localA Hk.sched ht hon) ?_ + rintro s1 ⟨R1, _, H1, W1⟩ + have F1 : MFrA (st := st) Z m s s1 σ mid := F.trans W1 rfl rfl + have a1 : SchedSRA st {Z with Q := Q, R := R1} m κ s1 mid := + H1.toA (fun x1 p1 y1 n1 hx1 hsc1 hy1 _ _ => by + change ρ.kheap (specPtr t) = some (.tcb x1) at hx1 + rw [hx] at hx1; cases hx1 + rw [hsc] at hsc1; cases hsc1 + change ρ.kheap (scNameA m sc) = some (.schedContext y1 n1) at hy1 + rw [hy] at hy1; cases hy1 + exact ⟨ready, Hk.sched.inv.suff hy active ni⟩) + have local1 : L.mem s1 (affA t) = L.cpu s1 := yieldAffKeepA Hk.sched a1 ht ht hon + obtain ⟨S1, M1, Y1⟩ := yieldQueueFrameA Hk.sched Hk.states Hk.mcp Hk.links (σ' := mid) rfl W1.wr + refine WP.spec_bind (possible_switch_to_refines_releaseA a1 t ht local1 + (fun w hw => by change ρ.kheap (specPtr t) = _ at hw; rw [hx] at hw; cases hw; exact run) S1) ?_ + rintro s2 ⟨τ, Q2, sw, heap2, rel2, W2, H2⟩ + have F2 : MFrA (st := st) Z m s s2 σ τ := F1.trans (MFrA.of_sw heap2 W2) rfl rfl + rw [WP.spec_ok] + obtain ⟨S2, M2, Y2⟩ := yieldQueueFrameA a1 S1 M1 Y1 heap2 (W2.mono fun _ w => Or.inr w) + have tail : mid.releaseQueue = rest.map specPtr := by + have nodup : (R (L.cpu sk)).Nodup := rel.nodup + rw [list, List.nodup_cons] at nodup + change tcbSchedDequeue _ ρ.releaseQueue = _ + rw [q] + unfold tcbSchedDequeue + simp only [List.filter_cons, ne_eq, not_true_eq_false, decide_false] + exact List.filter_eq_self.2 fun u hu => decide_eq_true (fun e => + nodup.1 (mem_map_specPtrSc.1 (e ▸ hu))) + refine ⟨⟨τ, Q2, R1, ⟨H2, S2, M2, Y2⟩, ?_, ?_, heap2.trans heap, ?_, F2⟩, ?_⟩ + · rw [rel2, tail, List.length_map] + rw [q, List.length_cons, List.length_map] at len + scalar_tac + · rw [loopEq] + unfold awaken + exact wl_stepSc (by rw [read, ready, the_someA]) (wakeReleaseStepA q sw) + · intro u hu + rw [rel2, tail] at hu + exact sub u (by rw [q]; exact List.mem_cons_of_mem _ hu) + · scalar_tac + · exact ⟨σ, Z.Q, Z.R, H, releaseLenA H.sched.toSchedSRN, rfl, rfl, fun _ h => h, MFrA.refl Z m s σ⟩ + + +/-- **The wake phase with its footprints.** `FF` is the frame the [J] `timer.fire` premise gives (a relation between the +states before and after the timer pass: the ledger's G2 / "JOIN ntfn signal"); the release walk then adds `MFrA` and +`SchedFrameA`. -/ +theorem awaken_stepA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {a : A} {σ : State Unit} + {FF : S → S → State Unit → State Unit → Prop} + (fireF : timer.fire st ar s a ⦃ r => ∃ σ1 Q R, + fl4TimerFires σ σ1 ∧ YieldQueueRelA (st := st) {Z with Q := Q, R := R} m κ r.1 σ1 ∧ IdleThreadsA m σ1 ∧ + FF s r.1 σ σ1 ⦄) : + sched.awaken st ar s a ⦃ r => ∃ s1 σ1 τ Q' R', + FF s s1 σ σ1 ∧ fl4TimerFires σ σ1 ∧ awaken (Z := Unit) σ1 = ({((), τ)}, False) ∧ + YieldQueueRelA (st := st) {Z with Q := Q', R := R'} m κ r.1 τ ∧ + MFrA (st := st) Z m s1 r.1 σ1 τ ∧ SchedFrameA σ1 τ ⦄ := by + unfold sched.awaken + refine WP.spec_bind fireF ?_ + rintro ⟨s1, a1⟩ ⟨σ1, Q, R, fires, H1, idle, F⟩ + refine WP.spec_bind (awaken_loop_stepA H1 idle) ?_ + rintro s2 ⟨τ, Q', R', waking, H2, -, -, Fr⟩ + rw [WP.spec_ok] + refine ⟨s1, σ1, τ, Q', R', F, fires, waking, H2, (MFrA.refl Z m s1 σ1).trans Fr rfl rfl, ?_⟩ + have hm : ((), τ) ∈ (awaken (Z := Unit) σ1).1 := by rw [waking]; exact Set.mem_singleton _ + exact awaken_presA σ1 _ hm + + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3SchedBindA.lean b/proofs/Fl4ProofsA64/P3/Stage3SchedBindA.lean new file mode 100644 index 000000000..f564cbac5 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SchedBindA.lean @@ -0,0 +1,361 @@ +/- +P3 (aarch64, A64-P3 Stage 3, kernel-entry composition, Plan A): **the scheduler spec steps keep the bindings**. + +`SBindA σ σ'`: the idle thread is kept, every TCB of `σ'` was a TCB of `σ` with the same SC, and every SC of `σ'` was +an SC of `σ` of the same size with the same replies and bound TCB. `SchedFrameA` does not give these; `SInvXA.idleSc`, +`SIdleA.f4`/`f5` and `ReplyStackRelA` across `schedule` need them. + +Generated by `handoff/tools/gen_kentry_sbind.py` (a twin of the instance lists of `P3/SpecFrameCalcA` and +`P3/SpecFrameSchedA`). No premise; new module. +-/ +import Fl4ProofsA64.P3.SpecFrameSchedA +import Fl4ProofsA64.P3.Stage3ExitAtA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec + +noncomputable section + +/-- The binding frame (module docstring). -/ +structure SBindA (σ σ' : State Unit) : Prop where + idle : σ'.idleThread = σ.idleThread + tcb : ∀ p x', σ'.kheap p = some (.tcb x') → ∃ x, σ.kheap p = some (.tcb x) ∧ x'.tcbSchedContext = x.tcbSchedContext + sc : ∀ p y' k, σ'.kheap p = some (.schedContext y' k) → ∃ y, σ.kheap p = some (.schedContext y k) ∧ + y'.scReplies = y.scReplies ∧ y'.scTcb = y.scTcb + +instance : PreO SBindA where + refl _ := ⟨rfl, fun _ x h => ⟨x, h, rfl⟩, fun _ y _ h => ⟨y, h, rfl, rfl⟩⟩ + trans h1 h2 := ⟨h2.idle.trans h1.idle, fun p x'' h => by + obtain ⟨x', h', e'⟩ := h2.tcb p x'' h + obtain ⟨x, h0, e⟩ := h1.tcb p x' h' + exact ⟨x, h0, e'.trans e⟩, fun p y'' k h => by + obtain ⟨y', h', a', b'⟩ := h2.sc p y'' k h + obtain ⟨y, h0, a, b⟩ := h1.sc p y' k h' + exact ⟨y, h0, a'.trans a, b'.trans b⟩⟩ + +theorem SBindA.of_eq {σ σ' : State Unit} (hk : σ'.kheap = σ.kheap) (hi : σ'.idleThread = σ.idleThread) : + SBindA σ σ' := + ⟨hi, fun _ x h => ⟨x, by rw [← hk]; exact h, rfl⟩, fun _ y _ h => ⟨y, by rw [← hk]; exact h, rfl, rfl⟩⟩ + +theorem SBindA.upd_tcb {σ : State Unit} {p : ObjRef} {x x' : Tcb} (h : σ.kheap p = some (.tcb x)) + (e : x'.tcbSchedContext = x.tcbSchedContext) : + SBindA σ { σ with kheap := Function.update σ.kheap p (some (.tcb x')) } := by + refine ⟨rfl, fun q z hz => ?_, fun q z k hz => ?_⟩ + · by_cases c : q = p + · subst c + have : x' = z := by simpa using hz + subst this + exact ⟨x, h, e⟩ + · exact ⟨z, by simpa [Function.update_of_ne c] using hz, rfl⟩ + · by_cases c : q = p + · subst c; simp at hz + · exact ⟨z, by simpa [Function.update_of_ne c] using hz, rfl, rfl⟩ + +theorem SBindA.upd_sc {σ : State Unit} {p : ObjRef} {y y' : SchedContext} {n : Nat} + (h : σ.kheap p = some (.schedContext y n)) (e1 : y'.scReplies = y.scReplies) (e2 : y'.scTcb = y.scTcb) : + SBindA σ { σ with kheap := Function.update σ.kheap p (some (.schedContext y' n)) } := by + refine ⟨rfl, fun q z hz => ?_, fun q z k hz => ?_⟩ + · by_cases c : q = p + · subst c; simp at hz + · exact ⟨z, by simpa [Function.update_of_ne c] using hz, rfl⟩ + · by_cases c : q = p + · subst c + simp at hz + obtain ⟨rfl, rfl⟩ := hz + exact ⟨y, h, e1, e2⟩ + · exact ⟨z, by simpa [Function.update_of_ne c] using hz, rfl, rfl⟩ + +abbrev BPresA {α : Type} (m : SMonad Unit α) : Prop := PresR SBindA m + +class BPresInst {α : Type} (m : SMonad Unit α) : Prop where + pres : BPresA m + +class TcbBFn (f : Tcb → Tcb) : Prop where + keep : ∀ x, (f x).tcbSchedContext = x.tcbSchedContext + +class ScBFn (f : SchedContext → SchedContext) : Prop where + keep : ∀ y, (f y).scReplies = y.scReplies ∧ (f y).scTcb = y.scTcb + +instance (ts : ThreadState) : TcbBFn (fun tcb : Tcb => { tcb with tcbState := ts }) := ⟨fun _ => rfl⟩ +instance (g : Option ObjRef → Option ObjRef) : TcbBFn (tcbYieldToUpdate g) := ⟨fun _ => rfl⟩ + +instance (g : List Refill → List Refill) : ScBFn (scRefillsUpdate g) := ⟨fun _ => ⟨rfl, rfl⟩⟩ +instance (g : Ticks → Ticks) : ScBFn (scBudgetUpdate g) := ⟨fun _ => ⟨rfl, rfl⟩⟩ +instance (g : Ticks → Ticks) : ScBFn (scPeriodUpdate g) := ⟨fun _ => ⟨rfl, rfl⟩⟩ +instance (g : Ticks → Ticks) : ScBFn (scConsumedUpdate g) := ⟨fun _ => ⟨rfl, rfl⟩⟩ +instance (g : Option ObjRef → Option ObjRef) : ScBFn (scNtfnUpdate g) := ⟨fun _ => ⟨rfl, rfl⟩⟩ +instance (g : Option ObjRef → Option ObjRef) : ScBFn (scYieldFromUpdate g) := ⟨fun _ => ⟨rfl, rfl⟩⟩ +instance (g : Badge → Badge) : ScBFn (scBadgeUpdate g) := ⟨fun _ => ⟨rfl, rfl⟩⟩ +instance (g : Bool → Bool) : ScBFn (scSporadicUpdate g) := ⟨fun _ => ⟨rfl, rfl⟩⟩ +instance (c : Ticks) : ScBFn (fun sc : SchedContext => { sc with scConsumed := sc.scConsumed + c }) := + ⟨fun _ => ⟨rfl, rfl⟩⟩ + +instance {α β : Type} {m : SMonad Unit α} {f : α → SMonad Unit β} [hm : BPresInst m] + [hf : ∀ a, BPresInst (f a)] : BPresInst (m >>= f) := + ⟨PresR.bind hm.pres fun a => (hf a).pres⟩ + +instance {α : Type} (a : α) : BPresInst (pure a : SMonad Unit α) := ⟨PresR.pure a⟩ + +instance {α : Type} (f : State Unit → α) : BPresInst (NonDet.gets f : SMonad Unit α) := ⟨PresR.gets f⟩ + +instance : BPresInst (NonDet.get : SMonad Unit (State Unit)) := ⟨PresR.get⟩ + +instance {α : Type} (f : State Unit → Option α) : BPresInst (NonDet.getsThe f : SMonad Unit α) := + ⟨PresR.getsThe f⟩ + +instance {α : Type} (o : Option α) : BPresInst (NonDet.assertOpt o : SMonad Unit α) := ⟨PresR.assertOpt o⟩ + +instance (P : Prop) : BPresInst (NonDet.assert P : SMonad Unit Unit) := ⟨PresR.assert P⟩ + +instance {α : Type} : BPresInst (NonDet.fail : SMonad Unit α) := ⟨PresR.fail⟩ + +instance {P : Prop} {d : Decidable P} {m : SMonad Unit Unit} [h : BPresInst m] : + BPresInst (@NonDet.when (State Unit) P d m) := ⟨PresR.when h.pres⟩ + +instance {P : Prop} {d : Decidable P} {m : SMonad Unit Unit} [h : BPresInst m] : + BPresInst (@NonDet.unless (State Unit) P d m) := ⟨PresR.unless' h.pres⟩ + +instance {α : Type} {P : Prop} {d : Decidable P} {a b : SMonad Unit α} [ha : BPresInst a] [hb : BPresInst b] : + BPresInst (@ite _ P d a b) := ⟨PresR.ite' ha.pres hb.pres⟩ + +instance {β γ : Type} {f : γ → SMonad Unit β} [hf : ∀ a, BPresInst (f a)] (l : List γ) : + BPresInst (NonDet.mapM f l) := ⟨PresR.mapM (fun a => (hf a).pres) l⟩ + +instance {α : Type} {C : α → State Unit → Prop} {B : α → SMonad Unit α} [hB : ∀ a, BPresInst (B a)] (r : α) : + BPresInst (NonDet.whileLoop C B r) := ⟨PresR.whileLoop (fun a => (hB a).pres) r⟩ + +instance {α : Type} (S : Set α × Prop) : BPresInst (NonDet.selectF S : SMonad Unit α) := ⟨PresR.selectF S⟩ + +instance {α : Type} {f : α → SMonad Unit Unit} [hf : ∀ a, BPresInst (f a)] (o : Option α) : + BPresInst (NonDet.maybeM f o) := by + cases o + · exact ⟨PresR.pure _⟩ + · exact hf _ + + +/-- A state modification that keeps the heap and the idle thread. -/ +theorem BPresA.modify_fix (f : State Unit → State Unit) (hk : ∀ σ, (f σ).kheap = σ.kheap) + (hi : ∀ σ, (f σ).idleThread = σ.idleThread) : BPresA (NonDet.modify f : SMonad Unit Unit) := + PresR.modify f fun σ => SBindA.of_eq (hk σ) (hi σ) + +macro "bpres" : tactic => `(tactic| + repeat' (first + | exact BPresInst.pres + | refine PresR.bind_pure _ _ ?_ + | refine PresR.bind ?_ (fun _ => ?_) + | refine PresR.when ?_ + | refine PresR.unless' ?_ + | refine PresR.ite' ?_ ?_ + | (refine BPresA.modify_fix _ (fun _ => rfl) (fun _ => rfl)) + | dsimp only + | split)) + +section +variable {σ : Fl4SpecA64.State Unit} + +theorem threadSet_bindA (f : Fl4SpecA64.Tcb → Fl4SpecA64.Tcb) [TcbBFn f] (t : Fl4SpecA64.ObjRef) : + BPresA (threadSet (Z := Unit) f t) := by + intro σ r hr + obtain ⟨y1, hy1, h⟩ := hr + have hy1' : y1 ∈ (NonDet.assertOpt (getTcb (Z := Unit) t σ) σ).1 := hy1 + cases hg : getTcb (Z := Unit) t σ with + | none => rw [hg] at hy1'; exact absurd hy1' (Set.notMem_empty _) + | some x => + rw [hg] at hy1' + have e1 : y1 = (x, σ) := hy1' + subst e1 + obtain ⟨ko, hko, hr2⟩ := setObject_memA h + have hx := getTcb_kheapA hg + rw [hr2] + exact SBindA.upd_tcb hx (TcbBFn.keep x) + +theorem updateSchedContext_bindA (f : Fl4SpecA64.SchedContext → Fl4SpecA64.SchedContext) [ScBFn f] (p : Fl4SpecA64.ObjRef) : + BPresA (updateSchedContext (Z := Unit) p f) := by + intro σ r hr + obtain ⟨y1, hy1, h⟩ := hr + have hy1' : y1 ∈ (NonDet.assertOpt (σ.kheap p) σ).1 := hy1 + cases hk : σ.kheap p with + | none => rw [hk] at hy1'; exact absurd hy1' (Set.notMem_empty _) + | some ko => + rw [hk] at hy1' + have e1 : y1 = (ko, σ) := hy1' + subst e1 + cases ko with + | schedContext sc n => + obtain ⟨ko', hko, hr2⟩ := setObject_memA h + rw [hr2] + exact SBindA.upd_sc hk (ScBFn.keep sc).1 (ScBFn.keep sc).2 + | _ => exact absurd h (Set.notMem_empty _) + +theorem asUser_bindA {α : Type} (t : Fl4SpecA64.ObjRef) (f : UserMonad α) : BPresA (asUser (Z := Unit) t f) := by + intro σ r hr + obtain ⟨y1, hy1, hr⟩ := hr + have hy1' : y1 ∈ (NonDet.assertOpt (getTcb (Z := Unit) t σ) σ).1 := hy1 + cases hg : getTcb (Z := Unit) t σ with + | none => rw [hg] at hy1'; exact absurd hy1' (Set.notMem_empty _) + | some x => + rw [hg] at hy1' + have e1 : y1 = (x, σ) := hy1' + subst e1 + obtain ⟨y2, hy2, hr⟩ := hr + have e2 : y2 = (archTcbContextGet x.tcbArch, σ) := hy2 + subst e2 + obtain ⟨⟨⟨a, uc'⟩, s3⟩, hy3, hr⟩ := hr + have e3 : s3 = σ := hy3.2 + subst e3 + obtain ⟨y4, hy4, hr⟩ := hr + have e4 : y4 = ({ x with tcbArch := archTcbContextSet uc' x.tcbArch }, s3) := hy4 + subst e4 + obtain ⟨y5, hy5, hr⟩ := hr + obtain ⟨ko, hko, hr5⟩ := setObject_memA hy5 + have hx := getTcb_kheapA hg + have e6 : r = (a, y5.2) := hr + rw [e6, hr5] + exact SBindA.upd_tcb hx rfl + +theorem doMachineOp_bindA {α : Type} (mop : NonDet MachineState α) : + BPresA (doMachineOp (Z := Unit) mop) := by + intro σ r hr + obtain ⟨y1, hy1, hr⟩ := hr + have e1 : y1 = (σ.machineState, σ) := hy1 + subst e1 + obtain ⟨⟨⟨a, ms'⟩, s2⟩, hy2, hr⟩ := hr + have e2 : s2 = σ := hy2.2 + subst e2 + obtain ⟨y3, hy3, hr⟩ := hr + have e3 : y3 = ((), { s2 with machineState := ms' }) := hy3 + subst e3 + have e4 : r = (a, { s2 with machineState := ms' }) := hr + rw [e4] + exact SBindA.of_eq rfl rfl + +end + +instance (f : Tcb → Tcb) [TcbBFn f] (t : ObjRef) : BPresInst (threadSet (Z := Unit) f t) := + ⟨threadSet_bindA f t⟩ +instance (p : ObjRef) (f : SchedContext → SchedContext) [ScBFn f] : BPresInst (updateSchedContext (Z := Unit) p f) := + ⟨updateSchedContext_bindA f p⟩ +instance {α : Type} (t : ObjRef) (f : UserMonad α) : BPresInst (asUser (Z := Unit) t f) := ⟨asUser_bindA t f⟩ +instance {α : Type} (mop : NonDet MachineState α) : BPresInst (doMachineOp (Z := Unit) mop) := ⟨doMachineOp_bindA mop⟩ + +/-! ## Readers and small writers -/ + +instance {α : Type} (f : Tcb → α) (t : ObjRef) : BPresInst (threadGet (Z := Unit) f t) := + ⟨by unfold threadGet; bpres⟩ +instance (f : Tcb → Option ObjRef) (r : ObjRef) : BPresInst (getTcbObjRef (Z := Unit) f r) := + ⟨by unfold getTcbObjRef; bpres⟩ +instance (r : ObjRef) : BPresInst (getThreadState (Z := Unit) r) := ⟨by unfold getThreadState; bpres⟩ +instance (d : Domain) (p : Priority) : BPresInst (getTcbQueue (Z := Unit) d p) := ⟨by unfold getTcbQueue; bpres⟩ +instance (d : Domain) (p : Priority) (q : ReadyQueue) : BPresInst (setTcbQueue (Z := Unit) d p q) := + ⟨by unfold setTcbQueue; bpres⟩ +instance (a : ObjRef → List ObjRef → List ObjRef) (t : ObjRef) : BPresInst (tcbSchedAction (Z := Unit) a t) := + ⟨by unfold tcbSchedAction; bpres⟩ +instance (a : SchedulerAction) : BPresInst (setSchedulerAction (Z := Unit) a) := + ⟨by unfold setSchedulerAction; bpres⟩ +instance (t : ObjRef) : BPresInst (tcbReleaseRemove (Z := Unit) t) := ⟨by unfold tcbReleaseRemove; bpres⟩ + +instance (p : ObjRef) : BPresInst (getObject (Z := Unit) p) := ⟨by unfold getObject; bpres⟩ +instance (p : ObjRef) : BPresInst (getSchedContext (Z := Unit) p) := ⟨by unfold getSchedContext; bpres⟩ +instance (f : SchedContext → Option ObjRef) (p : ObjRef) : BPresInst (getScObjRef (Z := Unit) f p) := + ⟨by unfold getScObjRef; bpres⟩ +instance (p : ObjRef) : BPresInst (getRefills (Z := Unit) p) := ⟨by unfold getRefills; bpres⟩ +instance (p : ObjRef) : BPresInst (getRefillHead (Z := Unit) p) := ⟨by unfold getRefillHead; bpres⟩ +instance (p : ObjRef) : BPresInst (getRefillTail (Z := Unit) p) := ⟨by unfold getRefillTail; bpres⟩ +instance (p : ObjRef) : BPresInst (getScRefillReady (Z := Unit) p) := ⟨by unfold getScRefillReady; bpres⟩ +instance (p : ObjRef) (u : Ticks) : BPresInst (getScRefillSufficient (Z := Unit) p u) := + ⟨by unfold getScRefillSufficient; bpres⟩ +instance (p : ObjRef) (u : Ticks) : BPresInst (fl4GetScRefillSufficient (Z := Unit) p u) := + ⟨by unfold fl4GetScRefillSufficient; bpres⟩ +instance (p : ObjRef) : BPresInst (getScActive (Z := Unit) p) := ⟨by unfold getScActive; bpres⟩ +instance (p : ObjRef) : BPresInst (isRoundRobin (Z := Unit) p) := ⟨by unfold isRoundRobin; bpres⟩ + +/-! ## Scheduler queues -/ + +instance : BPresInst (rescheduleRequired (Z := Unit)) := ⟨by unfold rescheduleRequired; bpres⟩ +instance (t : ObjRef) : BPresInst (possibleSwitchTo (Z := Unit) t) := ⟨by unfold possibleSwitchTo; bpres⟩ + + +/-! ## Tops -/ + +instance : BPresInst (fl4ChooseThread (Z := Unit)) := ⟨by unfold fl4ChooseThread; bpres⟩ +instance (t : ObjRef) : BPresInst (fl4SwitchToThread (Z := Unit) t) := ⟨by unfold fl4SwitchToThread; bpres⟩ +instance (eop : DetExtMonad Unit) : BPresInst (doExtendedOp (Z := Unit) eop) := ⟨by unfold doExtendedOp; bpres⟩ +instance (limit : Nat) : BPresInst (fl4NextDomain (Z := Unit) limit) := ⟨by unfold fl4NextDomain; bpres⟩ +instance (limit : Nat) : BPresInst (fl4ChooseNewThread (Z := Unit) limit) := ⟨by unfold fl4ChooseNewThread; bpres⟩ +instance (limit : Nat) (c cu i : ObjRef) (d : Domain) (w : Bool) : + BPresInst (fl4SelectCandidate (Z := Unit) limit c cu i d w) := ⟨by unfold fl4SelectCandidate; bpres⟩ + +instance (p : CslotPtr) : BPresInst (getCap (Z := Unit) p) := ⟨by unfold getCap; bpres⟩ + +/-! ### Refills -/ + +instance (p : ObjRef) (f : Refill → Refill) : BPresInst (updateRefillHd (Z := Unit) p f) := + ⟨by unfold updateRefillHd; bpres⟩ +instance (p : ObjRef) (f : Refill → Refill) : BPresInst (updateRefillTl (Z := Unit) p f) := + ⟨by unfold updateRefillTl; bpres⟩ +instance (p : ObjRef) (r : Refill) : BPresInst (refillAddTail (Z := Unit) p r) := ⟨by unfold refillAddTail; bpres⟩ +instance (p : ObjRef) : BPresInst (refillPopHead (Z := Unit) p) := ⟨by unfold refillPopHead; bpres⟩ +instance (p : ObjRef) : BPresInst (refillSize (Z := Unit) p) := ⟨by unfold refillSize; bpres⟩ +instance (p : ObjRef) : BPresInst (refillSingle (Z := Unit) p) := ⟨by unfold refillSingle; bpres⟩ +instance (p : ObjRef) : BPresInst (refillFull (Z := Unit) p) := ⟨by unfold refillFull; bpres⟩ +instance (p : ObjRef) (r : Refill) : BPresInst (scheduleUsed (Z := Unit) p r) := ⟨by unfold scheduleUsed; bpres⟩ +instance (p : ObjRef) : BPresInst (mergeOverlappingRefills (Z := Unit) p) := ⟨by unfold mergeOverlappingRefills; bpres⟩ +instance (p : ObjRef) : BPresInst (refillHeadOverlappingLoop (Z := Unit) p) := + ⟨by unfold refillHeadOverlappingLoop; bpres⟩ +instance (p : ObjRef) : BPresInst (refillUnblockCheck (Z := Unit) p) := ⟨by unfold refillUnblockCheck; bpres⟩ +instance (o : Option ObjRef) (a b : Option Bool) : BPresInst (ifCondRefillUnblockCheck (Z := Unit) o a b) := + ⟨by unfold ifCondRefillUnblockCheck; bpres⟩ +instance (p : ObjRef) : BPresInst (mergeNonoverlappingHeadRefill (Z := Unit) p) := + ⟨by unfold mergeNonoverlappingHeadRefill; bpres⟩ +instance (m : Ticks) (p : ObjRef) : BPresInst (headInsufficientLoopWith (Z := Unit) m p) := + ⟨by unfold headInsufficientLoopWith; bpres⟩ +instance (p : ObjRef) (u : Ticks) : BPresInst (chargeEntireHeadRefill (Z := Unit) p u) := + ⟨by unfold chargeEntireHeadRefill; bpres⟩ +instance (l : Ticks) (p : ObjRef) (u : Ticks) : BPresInst (handleOverrunWith (Z := Unit) l p u) := + ⟨by unfold handleOverrunWith; bpres⟩ +instance (l m u : Ticks) : BPresInst (refillBudgetCheckWith (Z := Unit) l m u) := + ⟨by unfold refillBudgetCheckWith; bpres⟩ +instance (u : Ticks) : BPresInst (fl4RefillBudgetCheck (Z := Unit) u) := ⟨by unfold fl4RefillBudgetCheck; bpres⟩ +instance (u : Ticks) : BPresInst (refillBudgetCheckRoundRobin (Z := Unit) u) := + ⟨by unfold refillBudgetCheckRoundRobin; bpres⟩ +instance : BPresInst (fl4CommitTime (Z := Unit)) := ⟨by unfold fl4CommitTime; bpres⟩ + +/-! ### Switch, timer, awaken, domain -/ + +instance : BPresInst (fl4SwitchSchedContext (Z := Unit)) := ⟨by unfold fl4SwitchSchedContext; bpres⟩ + +instance (q : List ObjRef) : BPresInst (fl4ReleaseDeadline (Z := Unit) q) := ⟨by unfold fl4ReleaseDeadline; bpres⟩ +instance (timer : State Unit → Fl4TimerView) : BPresInst (fl4SetNextInterrupt (Z := Unit) timer) := + ⟨by unfold fl4SetNextInterrupt; bpres⟩ +instance (timer : State Unit → Fl4TimerView) : BPresInst (fl4ScAndTimer (Z := Unit) timer) := + ⟨by unfold fl4ScAndTimer; bpres⟩ +instance : BPresInst (tcbReleaseDequeue (Z := Unit)) := ⟨by unfold tcbReleaseDequeue; bpres⟩ +instance : BPresInst (awaken (Z := Unit)) := ⟨by unfold awaken; bpres⟩ +instance : BPresInst (checkDomainTime (Z := Unit)) := ⟨by unfold checkDomainTime; bpres⟩ + +theorem fl4SelectAction_bindA (limit : Nat) : BPresA (fl4SelectAction (Z := Unit) limit) := by + unfold fl4SelectAction; bpres + +theorem fl4SelectThread_bindA (limit : Nat) : BPresA (fl4SelectThread (Z := Unit) limit) := by + unfold fl4SelectThread; bpres + + +theorem awaken_bindA : BPresA (awaken (Z := Unit)) := (inferInstance : BPresInst (awaken (Z := Unit))).pres +theorem checkDomainTime_bindA : BPresA (checkDomainTime (Z := Unit)) := + (inferInstance : BPresInst (checkDomainTime (Z := Unit))).pres +theorem fl4ScAndTimer_bindA (timer : State Unit → Fl4TimerView) : BPresA (fl4ScAndTimer (Z := Unit) timer) := + (inferInstance : BPresInst (fl4ScAndTimer (Z := Unit) timer)).pres + +/-- **The whole schedule tail keeps the bindings.** -/ +theorem schedTail_bindA (limit : Nat) (timer : State Unit → Fl4TimerView) {σ1 τ : State Unit} + (h : ((), τ) ∈ (SchedTail limit timer σ1).1) : SBindA σ1 τ := by + have P : BPresA (SchedTail limit timer) := by + unfold SchedTail + exact PresR.bind awaken_bindA fun _ => PresR.bind checkDomainTime_bindA fun _ => + PresR.bind (fl4SelectThread_bindA limit) fun _ => fl4ScAndTimer_bindA timer + exact P σ1 _ h + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3SchedCtQA.lean b/proofs/Fl4ProofsA64/P3/Stage3SchedCtQA.lean new file mode 100644 index 000000000..955a2fe9c --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SchedCtQA.lean @@ -0,0 +1,261 @@ +/- +P3 (aarch64, A64-P3 Stage 3, kernel-entry composition, Plan A): **`ct_not_in_q` across the schedule tail**. + +* `CtW σ`: when resuming, the current thread is in no ready queue and not in the release queue (the record's weakened + `ct_not_in_q`, with the release-queue part `FIA` / `RelRun` give). +* `OwnQ σ`: the current thread is the idle thread, or is not in the ready queue of its own domain and priority. After + `fl4SelectThread` and `fl4ScAndTimer` this holds (`schedTail_ownQ`); with the concrete queue relation at the exit + state it gives `ct_not_in_q` (`P3/Stage3KEntrySchedA`). + +No premise; new module. +-/ +import Fl4ProofsA64.P3.Stage3ExitAtA +import Fl4ProofsA64.P3.Stage3SchedSelectA +import Fl4ProofsA64.P3.Stage3SchedActResA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec Classical + +noncomputable section + +/-- Resuming: the current thread is in no ready queue and not in the release queue. -/ +def CtW (σ : State Unit) : Prop := + σ.schedulerAction = .resumeCurThread → (∀ d p, σ.curThread ∉ σ.readyQueues d p) ∧ σ.curThread ∉ σ.releaseQueue + +/-- The current thread is idle, or not queued at its own domain and priority. -/ +def OwnQ (σ : State Unit) : Prop := + σ.curThread = σ.idleThread ∨ + ∀ x, σ.kheap σ.curThread = some (.tcb x) → σ.curThread ∉ σ.readyQueues x.tcbDomain x.tcbPriority + +theorem CtW.ofNe {σ : State Unit} (h : σ.schedulerAction ≠ .resumeCurThread) : CtW σ := fun e => absurd e h + +theorem CtW.ownQ {σ : State Unit} (h : CtW σ) (e : σ.schedulerAction = .resumeCurThread) : OwnQ σ := + Or.inr fun _ _ => (h e).1 _ _ + +/-- `ct` is kept out of the queues by an enqueue / append / dequeue of another thread. -/ +theorem tcbSchedAction_other (f : ObjRef → List ObjRef → List ObjRef) (t : ObjRef) + (hf : ∀ q c, c ≠ t → c ∉ q → c ∉ f t q) : + HT (fun σ => σ.schedulerAction = .resumeCurThread → + ((∀ d p, σ.curThread ∉ σ.readyQueues d p) ∧ σ.curThread ∉ σ.releaseQueue) ∧ σ.curThread ≠ t) + (tcbSchedAction (Z := Unit) f t) (fun _ σ => CtW σ) := by + intro σ hσ r hr + unfold Fl4SpecA64.tcbSchedAction Fl4SpecA64.getTcbQueue Fl4SpecA64.setTcbQueue at hr + obtain ⟨⟨d, σ1⟩, h1, hr⟩ := mem_bindA.1 hr + have e1 : σ1 = σ := (HT.threadGet (P := fun s => s = σ) (Q := fun _ s => s = σ) Tcb.tcbDomain t + (fun _ h _ _ => h)) σ rfl _ h1 + rw [e1] at hr + obtain ⟨⟨p, σ2⟩, h2, hr⟩ := mem_bindA.1 hr + have e2 : σ2 = σ := (HT.threadGet (P := fun s => s = σ) (Q := fun _ s => s = σ) Tcb.tcbPriority t + (fun _ h _ _ => h)) σ rfl _ h2 + rw [e2] at hr + obtain ⟨⟨q, σ3⟩, h3, hr⟩ := mem_bindA.1 hr + obtain ⟨⟨qs, σ4⟩, h4, h5⟩ := mem_bindA.1 h3 + have e4 : (qs, σ4) = (σ.readyQueues, σ) := h4 + cases e4 + have e5 : (q, σ3) = (σ.readyQueues d p, σ) := h5 + cases e5 + obtain ⟨⟨q', σ5⟩, h6, hr⟩ := mem_bindA.1 hr + have e6 : (q', σ5) = (f t (σ.readyQueues d p), σ) := h6 + cases e6 + have e7 : r = ((), _) := hr + rw [e7] + intro ha + obtain ⟨⟨hq, hn⟩, hne⟩ := hσ ha + refine ⟨fun d' p' => ?_, hn⟩ + dsimp only + split + · exact hf _ _ hne (hq _ _) + · exact hq _ _ + +/-- `possibleSwitchTo` keeps `CtW` for a target that, when resuming, is not the current thread. -/ +theorem possibleSwitchTo_ctw (t : ObjRef) : + HT (fun σ => CtW σ ∧ (σ.schedulerAction = .resumeCurThread → t ≠ σ.curThread)) + (possibleSwitchTo (Z := Unit) t) (fun _ σ => CtW σ) := by + let P : State Unit → Prop := fun σ => CtW σ ∧ (σ.schedulerAction = .resumeCurThread → t ≠ σ.curThread) + unfold Fl4SpecA64.possibleSwitchTo Fl4SpecA64.getTcbObjRef + refine HT.bind (Q := fun _ σ => P σ) (HT.threadGet _ t fun σ hσ x hx => hσ) fun scOpt => ?_ + refine HT.bind (Q := fun _ σ => P σ) (HT.gets _ fun σ hσ => hσ) fun inq => ?_ + refine HT.when (fun hc => ?_) (fun _ σ hσ => hσ.1) + refine HT.bind (Q := fun _ σ => P σ) (HT.gets _ fun σ hσ => hσ) fun curDom => ?_ + refine HT.bind (Q := fun _ σ => P σ) (HT.threadGet _ t fun σ hσ x hx => hσ) fun targetDom => ?_ + refine HT.bind (Q := fun a σ => P σ ∧ σ.schedulerAction = a) (HT.gets _ fun σ hσ => ⟨hσ, rfl⟩) fun action => ?_ + refine HT.ite (fun _ => ?_) (fun _ => HT.ite (fun hne => ?_) (fun _ => ?_)) + · refine HT.mono (tcbSchedAction_other tcbSchedEnqueue t (fun q c hc hn => ?_)) (fun σ h e => ?_) (fun _ _ h => h) + · unfold Fl4SpecA64.tcbSchedEnqueue + split + · simp [hc, hn] + · exact hn + · exact ⟨h.1.1 e, (h.1.2 e).symm⟩ + · refine HT.bind (Q := fun _ σ => σ.schedulerAction = .chooseNewThread) + (HT.mono resched_choose (fun _ _ => trivial) (fun _ _ h => h)) fun _ => ?_ + refine HT.mono (tcbSchedAction_keep tcbSchedEnqueue t (fun σ => σ.schedulerAction = .chooseNewThread) + (fun _ _ h => h)) (fun _ h => h) (fun _ σ h => CtW.ofNe (by rw [h]; intro e; cases e)) + · unfold Fl4SpecA64.setSchedulerAction + exact HT.modify _ fun σ _ => CtW.ofNe (by intro e; cases e) + +theorem tcbReleaseDequeue_ctw : + HT CtW (tcbReleaseDequeue (Z := Unit)) (fun _ σ => CtW σ) := by + unfold Fl4SpecA64.tcbReleaseDequeue + refine HT.bind (Q := fun rq σ => CtW σ ∧ rq = σ.releaseQueue) (HT.gets _ fun σ hσ => ⟨hσ, rfl⟩) fun rq => ?_ + refine HT.bind (Q := fun _ σ => (CtW σ ∧ rq = σ.releaseQueue) ∧ rq ≠ []) + (HT.assert fun hne σ hσ => ⟨hσ, hne⟩) fun _ => ?_ + refine HT.bind (Q := fun _ σ => CtW σ ∧ (σ.schedulerAction = .resumeCurThread → the rq.head? ≠ σ.curThread)) + ?_ fun _ => possibleSwitchTo_ctw _ + refine HT.mono (P := fun σ => CtW σ ∧ (σ.schedulerAction = .resumeCurThread → the rq.head? ≠ σ.curThread)) + ?_ (fun σ hσ => ?_) (fun _ _ h => h) + · unfold Fl4SpecA64.tcbReleaseRemove + let t := the rq.head? + let P : State Unit → Prop := fun σ => CtW σ ∧ (σ.schedulerAction = .resumeCurThread → t ≠ σ.curThread) + refine HT.bind (Q := fun qs σ => P σ ∧ qs = σ.releaseQueue) (HT.gets _ fun σ hσ => ⟨hσ, rfl⟩) fun qs => ?_ + refine HT.bind (Q := fun _ σ => P σ ∧ qs = σ.releaseQueue) + (HT.when (fun _ => HT.modify _ fun σ hσ => hσ) (fun _ σ hσ => hσ)) fun _ => ?_ + refine HT.bind (Q := fun qs' σ => (P σ ∧ qs = σ.releaseQueue) ∧ qs' = tcbSchedDequeue t qs) + (HT.pure _ fun σ hσ => ⟨hσ, rfl⟩) fun qs' => ?_ + refine HT.modify _ fun σ hσ => ⟨fun e => ⟨(hσ.1.1.1 e).1, fun hm => ?_⟩, hσ.1.1.2⟩ + have hm' : σ.curThread ∈ qs' := hm + rw [hσ.2] at hm' + exact (hσ.1.1.1 e).2 (hσ.1.2 ▸ (List.mem_filter.1 hm').1) + · refine ⟨hσ.1.1, fun e he => (hσ.1.1 e).2 ?_⟩ + rw [← hσ.1.2, ← he] + cases h : rq with + | nil => exact absurd h hσ.2 + | cons a l => exact List.mem_cons_self + +theorem awaken_ctw : HT CtW (awaken (Z := Unit)) (fun _ σ => CtW σ) := by + unfold Fl4SpecA64.awaken + exact HT.whileLoop CtW () fun _ => tcbReleaseDequeue_ctw + +theorem checkDomainTime_ctw : HT CtW (checkDomainTime (Z := Unit)) (fun _ σ => CtW σ) := by + unfold Fl4SpecA64.checkDomainTime + refine HT.bind (Q := fun _ σ => CtW σ) (HT.gets _ fun σ hσ => hσ) fun exp => ?_ + refine HT.when (fun _ => ?_) (fun _ σ hσ => hσ) + refine HT.bind (Q := fun _ σ => CtW σ) (HT.modify _ fun σ hσ => hσ) fun _ => ?_ + exact HT.mono resched_choose (fun _ _ => trivial) (fun _ σ h => CtW.ofNe (by rw [h]; intro e; cases e)) + +theorem switchTo_ownQ (t : ObjRef) : + HT (fun _ => True) (fl4SwitchToThread (Z := Unit) t) (fun _ σ => OwnQ σ) := by + unfold Fl4SpecA64.fl4SwitchToThread + refine HT.bind (Q := fun _ σ => ∀ x, σ.kheap t = some (.tcb x) → t ∉ σ.readyQueues x.tcbDomain x.tcbPriority) + ?_ fun _ => HT.modify _ fun σ hσ => Or.inr hσ + intro σ _ r hr + unfold Fl4SpecA64.tcbSchedAction Fl4SpecA64.getTcbQueue Fl4SpecA64.setTcbQueue at hr + obtain ⟨⟨d, σ1⟩, h1, hr⟩ := mem_bindA.1 hr + have g1 : σ1 = σ ∧ ∀ x, σ.kheap t = some (.tcb x) → x.tcbDomain = d := + (HT.threadGet (P := fun s => s = σ) (Q := fun d s => s = σ ∧ ∀ x, σ.kheap t = some (.tcb x) → x.tcbDomain = d) + Tcb.tcbDomain t (fun s h x hx => ⟨h, fun y hy => by + rw [h, hy] at hx; have := KernelObject.tcb.inj (Option.some.inj hx); rw [this]⟩)) σ rfl _ h1 + rw [g1.1] at hr + obtain ⟨⟨p, σ2⟩, h2, hr⟩ := mem_bindA.1 hr + have g2 : σ2 = σ ∧ ∀ x, σ.kheap t = some (.tcb x) → x.tcbPriority = p := + (HT.threadGet (P := fun s => s = σ) (Q := fun p s => s = σ ∧ ∀ x, σ.kheap t = some (.tcb x) → x.tcbPriority = p) + Tcb.tcbPriority t (fun s h x hx => ⟨h, fun y hy => by + rw [h, hy] at hx; have := KernelObject.tcb.inj (Option.some.inj hx); rw [this]⟩)) σ rfl _ h2 + rw [g2.1] at hr + obtain ⟨⟨q, σ3⟩, h3, hr⟩ := mem_bindA.1 hr + obtain ⟨⟨qs, σ4⟩, h4, h5⟩ := mem_bindA.1 h3 + have e4 : (qs, σ4) = (σ.readyQueues, σ) := h4 + cases e4 + have e5 : (q, σ3) = (σ.readyQueues d p, σ) := h5 + cases e5 + obtain ⟨⟨q', σ5⟩, h6, hr⟩ := mem_bindA.1 hr + have e6 : (q', σ5) = (tcbSchedDequeue t (σ.readyQueues d p), σ) := h6 + cases e6 + have e7 : r = ((), _) := hr + rw [e7] + intro x hx + dsimp only at hx ⊢ + rw [g1.2 x hx, g2.2 x hx, if_pos ⟨rfl, rfl⟩] + unfold Fl4SpecA64.tcbSchedDequeue + intro hm + exact (by simpa using (List.mem_filter.1 hm).2 : False) + +theorem chooseThread_ownQ : HT (fun _ => True) (fl4ChooseThread (Z := Unit)) (fun _ σ => OwnQ σ) := by + unfold Fl4SpecA64.fl4ChooseThread + refine HT.bind (Q := fun s σ => s = σ) (HT.get fun σ _ => rfl) fun s => ?_ + refine HT.ite (fun _ => ?_) (fun _ => HT.mono (switchTo_ownQ _) (fun _ _ => trivial) (fun _ _ h => h)) + exact HT.modify _ fun σ hσ => Or.inl (by rw [hσ]) + +theorem chooseNewThread_ownQ (limit : Nat) : + HT (fun _ => True) (fl4ChooseNewThread (Z := Unit) limit) (fun _ σ => OwnQ σ) := by + unfold Fl4SpecA64.fl4ChooseNewThread + refine HT.bind (Q := fun _ _ => True) (fun _ _ _ _ => trivial) fun _ => ?_ + refine HT.bind (Q := fun _ _ => True) (fun _ _ _ _ => trivial) fun _ => ?_ + exact chooseThread_ownQ + +theorem selectCandidate_ownQ (limit : Nat) (cand cur idle : ObjRef) (dom : Domain) (wr : Bool) : + HT (fun _ => True) (fl4SelectCandidate (Z := Unit) limit cand cur idle dom wr) (fun _ σ => OwnQ σ) := by + unfold Fl4SpecA64.fl4SelectCandidate + refine HT.bind (Q := fun _ _ => True) (fun _ _ _ _ => trivial) fun prio => ?_ + have any : ∀ {α : Type} (m : SMonad Unit α), HT (fun _ => True) m (fun _ _ => True) := + fun _ _ _ _ _ => trivial + have chain : ∀ f : ObjRef → List ObjRef → List ObjRef, HT (fun _ => True) (do + tcbSchedAction (Z := Unit) f cand + setSchedulerAction .chooseNewThread + fl4ChooseNewThread limit) (fun _ σ => OwnQ σ) := fun f => + HT.bind (any _) fun _ => HT.bind (any _) fun _ => chooseNewThread_ownQ limit + have tail : ∀ cp : Priority, HT (fun _ => True) (show SMonad Unit Unit from do + let highest ← NonDet.gets (isHighestPrio dom prio) + if (cur = idle ∨ prio < cp) ∧ ¬highest then + (do tcbSchedAction (Z := Unit) tcbSchedEnqueue cand + setSchedulerAction .chooseNewThread + fl4ChooseNewThread limit) + else if wr = true ∧ prio = cp then + (do tcbSchedAction (Z := Unit) tcbSchedAppend cand + setSchedulerAction .chooseNewThread + fl4ChooseNewThread limit) + else fl4SwitchToThread cand) (fun _ σ => OwnQ σ) := fun cp => + HT.bind (any _) fun _ => HT.ite (fun _ => chain _) (fun _ => HT.ite (fun _ => chain _) (fun _ => switchTo_ownQ cand)) + dsimp only + exact HT.ite (fun _ => HT.bind (any _) tail) (fun _ => HT.bind (any _) tail) + +theorem selectThread_ownQ (limit : Nat) : + HT CtW (fl4SelectThread (Z := Unit) limit) (fun _ σ => OwnQ σ) := by + unfold Fl4SpecA64.fl4SelectThread Fl4SpecA64.setSchedulerAction + refine HT.bind (Q := fun _ σ => OwnQ σ) ?_ fun _ => HT.modify _ fun σ hσ => hσ + unfold Fl4SpecA64.fl4SelectAction + refine HT.bind (Q := fun s σ => CtW σ ∧ s = σ) (HT.get fun σ hσ => ⟨hσ, rfl⟩) fun s => ?_ + refine HT.when (fun hne => ?_) (fun hn σ hσ => hσ.1.ownQ (by rw [← hσ.2]; exact not_not.1 hn)) + dsimp only + refine HT.bind (Q := fun _ _ => True) (fun _ _ _ _ => trivial) fun _ => ?_ + cases hA : s.schedulerAction with + | resumeCurThread => exact absurd hA hne + | chooseNewThread => exact chooseNewThread_ownQ limit + | switchThread c => exact selectCandidate_ownQ limit c _ _ _ _ + +theorem scAndTimer_ownQ (timer : State Unit → Fl4TimerView) : + HT OwnQ (fl4ScAndTimer (Z := Unit) timer) (fun _ σ => OwnQ σ) := by + have P : APres (fl4ScAndTimer (Z := Unit) timer) := (inferInstance : APresInst (fl4ScAndTimer (Z := Unit) timer)).pres + refine HT.ofAP P OwnQ fun σ σ' k h => ?_ + obtain ⟨-, c, -, tcb, rq, i, -⟩ := k + unfold OwnQ at h ⊢ + rw [c, i, rq] + exact h.imp_right fun h x hx => h x ((tcb _ x).1 hx) + +/-- **The schedule tail**: from the record's facts at the post-fire state, the FIA of the exit and `OwnQ`. -/ +theorem schedTail_ctQ (limit : Nat) (timer : State Unit → Fl4TimerView) {σ1 τ : State Unit} + (hR : FireRecA σ1) (hC : σ1.schedulerAction = .resumeCurThread → ∀ d p, σ1.curThread ∉ σ1.readyQueues d p) + (h : ((), τ) ∈ (SchedTail limit timer σ1).1) : FIA τ ∧ OwnQ τ := by + obtain ⟨hF, hI, hS, hRR⟩ := hR + have c1 : CtW σ1 := fun e => ⟨hC e, fun hm => by + by_cases ci : σ1.curThread = σ1.idleThread + · obtain ⟨x, ex, hr⟩ := hRR _ hm + rw [ci] at ex + rw [hI x ex] at hr + cases hr + · exact ((SchedValidB.toA hF.1 hF.2).act e ci).2 hm⟩ + obtain ⟨⟨⟨⟩, σ2⟩, h21, h⟩ := mem_bindA.1 h + have f2 := awaken_fia σ1 hF _ h21 + have c2 := awaken_ctw σ1 c1 _ h21 + obtain ⟨⟨⟨⟩, σ3⟩, h31, h⟩ := mem_bindA.1 h + have f3 := checkDomainTime_fia σ2 f2 _ h31 + have c3 := checkDomainTime_ctw σ2 c2 _ h31 + have sw := hSw_of hF hS hRR h21 h31 + obtain ⟨⟨⟨⟩, σ4⟩, h41, h⟩ := mem_bindA.1 h + have f4 := selectThread_fia limit σ3 ⟨f3, sw⟩ _ h41 + have o4 := selectThread_ownQ limit σ3 c3 _ h41 + exact ⟨scAndTimer_fia timer σ4 f4 _ h, scAndTimer_ownQ timer σ4 o4 _ h⟩ + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3SchedCurA.lean b/proofs/Fl4ProofsA64/P3/Stage3SchedCurA.lean new file mode 100644 index 000000000..6c78c954a --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SchedCurA.lean @@ -0,0 +1,72 @@ +/- +P3 (aarch64, A64-P3 Stage 3, T3 G4b slice 3): **the current thread's SC words after the schedule tail**. + +`JSyscallA.curSc` and the SC part of `JSyscallA.cur` at the post of `schedule`'s tail (`schedule_tail_refinesA`), from +two related states and the spec facts `fl4ScAndTimer` establishes (`P3/Stage3SchedActResA`: the current thread and every +TCB object are kept; `curSc` becomes the incoming SC): + +* **`YieldQueueRelA.cur_sc_tail`**: with `H0` the relation before the tail (at `s0`, spec state `σ0`), `H1` after (at + `s'`, `τ`), the current thread and TCB objects kept, `τ.curSc = scNameA m w0` (`w0` the current thread's SC word at + `s0`, a related SC) and every related SC non-null: the current thread, its SC word and `cscW` at `s'` are those of `s0`, + and `cscW s' = L.mem s' (scA (L.curW s'))`, `≠ 0`. + +The release-queue bit of `JSyscallA.cur` needs F8(b) (pending record edit #5) and is not done here. + +No premise; new module. +-/ +import Fl4ProofsA64.P3.Stage3SchedActResA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec fl4_core_a64 Fl4ProofsA64.P1 + +noncomputable section + +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **The current thread's SC words across the schedule tail** (module docstring). -/ +theorem YieldQueueRelA.cur_sc_tail {Z Z' : XSetsA} {m : McState} {κ : U64 → U64} {s0 s' : S} {σ0 τ : State Unit} + (H0 : YieldQueueRelA (st := st) Z m κ s0 σ0) (H1 : YieldQueueRelA (st := st) Z' m κ s' τ) + (hT : Z'.T = Z.T) (hC : Z'.C = Z.C) (hC0 : ∀ c ∈ Z.C, c ≠ 0#u64) + (hcur : τ.curThread = σ0.curThread) (htcb : ∀ p x, τ.kheap p = some (.tcb x) ↔ σ0.kheap p = some (.tcb x)) + (hw0 : L.mem s0 (scA (L.curW s0)) ∈ Z.C) (hcsc : τ.curSc = scNameA m (L.mem s0 (scA (L.curW s0)))) : + L.curW s' = L.curW s0 ∧ L.mem s' (scA (L.curW s')) = L.mem s0 (scA (L.curW s0)) ∧ + L.cscW s' = L.mem s' (scA (L.curW s')) ∧ L.mem s' (scA (L.curW s')) ≠ 0#u64 := by + have hc0 := H0.sched.sch.curThread + have hc1 := H1.sched.sch.curThread + have hw : L.curW s' = L.curW s0 := specPtr_inj (hc1.symm.trans (hcur.trans hc0)) + have ht0 := H0.sched.curT + have ht1 := H1.sched.curT + obtain ⟨x0, k0, e0⟩ := H0.sched.sch.tcb (L.curW s0) ht0 + obtain ⟨x1, k1, e1⟩ := H1.sched.sch.tcb (L.curW s') ht1 + rw [hw] at k1 e1 + have hx : x1 = x0 := by + have k1' := (htcb _ _).1 k1 + rw [k0] at k1' + injection k1' with k1'; injection k1' with k1' + exact k1'.symm + rw [hx, e0] at e1 + have nz0 : L.mem s0 (scA (L.curW s0)) ≠ 0#u64 := hC0 _ hw0 + have nz1 : L.mem s' (scA (L.curW s0)) ≠ 0#u64 := by + intro e + rw [optScA, if_neg nz0, optScA, if_pos e] at e1 + cases e1 + have mem1 : L.mem s' (scA (L.curW s0)) ∈ Z.C := by + rw [← hC]; exact H1.sched.rest.scMem (L.curW s0) (by rw [hT]; exact ht0) nz1 + have names : scNameA m (L.mem s' (scA (L.curW s0))) = scNameA m (L.mem s0 (scA (L.curW s0))) := by + rw [optScA, if_neg nz0, optScA, if_neg nz1] at e1 + exact (Option.some.inj e1).symm + have word : L.mem s' (scA (L.curW s0)) = L.mem s0 (scA (L.curW s0)) := + H0.sched.scp.inj _ mem1 _ hw0 names + have hcs : τ.curSc = scRenA m (L.cscW s') := by + have := H1.sched.sch + rw [← this.csc, this.core]; exact this.curScOfW _ this.cpuCs + have cs1 : L.cscW s' ∈ Z.C := by rw [← hC]; exact H1.sched.cscC + have hn : scNameA m (L.cscW s') = scNameA m (L.mem s0 (scA (L.curW s0))) := hcs.symm.trans hcsc + have hcw : L.cscW s' = L.mem s0 (scA (L.curW s0)) := H0.sched.scp.inj _ cs1 _ hw0 hn + rw [hw] + exact ⟨rfl, word, hcw.trans word.symm, word ▸ nz0⟩ + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3SchedExitA.lean b/proofs/Fl4ProofsA64/P3/Stage3SchedExitA.lean new file mode 100644 index 000000000..e7120a202 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SchedExitA.lean @@ -0,0 +1,68 @@ +/- +P3 (aarch64, A64-P3 Stage 3, T3 G4b / F8): **F8 across the schedule exit, template step 3: the whole `fl4Schedule`**. + +* `FIA.ofActKeep`: the spec computations `P3/Stage3SchedActResA` covers (`fl4ScAndTimer`: it keeps the action, current + thread, queues, idle thread, TCB objects and SC activity) keep `FIA`; `scAndTimer_fia`. +* **`fl4Schedule_fia`**: every outcome of `fl4Schedule` from an `FIA` state is an `FIA` state, given + - the timer step (`fl4Awaken`: `hAwk`, F8 across the TCB-timer signals: the notification-signal line's obligation), + - the pending-switch fact at the state selection starts from (`hSw`, the record's `ActIdleA.1`, the input + `ScheduleSelectionInputsA.action` of `schedule_refinesA`). + The `awaken` loop, `checkDomainTime`, selection and `fl4ScAndTimer` are proved + (`P3/Stage3SchedValidStepA`, `P3/Stage3SchedSelectA`). + +Named premises: F8(d) inside `FIA`; `hAwk`; `hSw`. +-/ +import Fl4ProofsA64.P3.Stage3SchedSelectA +import Fl4ProofsA64.P3.Stage3SchedActResA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P3 Classical + +noncomputable section + +theorem FIA.ofActKeep {σ σ' : State Unit} (h : FIA σ) (k : ActKeepA σ σ') : FIA σ' := by + obtain ⟨ka, kc, kr, kt, kq, ki, ks⟩ := k + have act : ∀ p, ScActiveAtA σ p → ScActiveAtA σ' p := fun p ⟨y, k, e, a⟩ => by + obtain ⟨y', e', a'⟩ := ks p y k e + exact ⟨y', k, e', a'.trans a⟩ + have actT : ∀ t, ActTcbA σ t → ActTcbA σ' t := fun t ⟨x, sc, e, es, a⟩ => + ⟨x, sc, (kt t x).2 e, es, act sc a⟩ + refine ⟨⟨fun d p t hm => ?_, fun e1 e2 => ?_, fun t x sc e1 e2 e3 => ?_⟩, fun t ht => ?_⟩ + · rw [kq] at hm + obtain ⟨a, n⟩ := h.1.rq d p t hm + exact ⟨actT t a, by rw [kr]; exact n⟩ + · rw [ka] at e1 + rw [kc, ki] at e2 + obtain ⟨a, n⟩ := h.1.act e1 e2 + rw [kc] + exact ⟨actT _ a, by rw [kr]; exact n⟩ + · exact act sc (h.1.blk t x sc ((kt t x).1 e1) e2 e3) + · rw [kr] at ht + exact actT t (h.2 t ht) + +theorem scAndTimer_fia (timer : State Unit → Fl4TimerView) : + HT FIA (fl4ScAndTimer (Z := Unit) timer) (fun _ σ => FIA σ) := + fun σ hσ r hr => FIA.ofActKeep hσ (fl4ScAndTimer_apres timer σ r hr) + +/-- **`fl4Schedule` keeps `FIA`** (module docstring). -/ +theorem fl4Schedule_fia (limit : Nat) (timer : State Unit → Fl4TimerView) {σ τ : State Unit} (hσ : FIA σ) + (hAwk : ∀ σ1, fl4TimerFires σ σ1 → FIA σ1) + (hSw : ∀ σ1 σ2 σ3, fl4TimerFires σ σ1 → ((), σ2) ∈ (awaken (Z := Unit) σ1).1 → + ((), σ3) ∈ (checkDomainTime (Z := Unit) σ2).1 → SwA σ3) + (h : ((), τ) ∈ (fl4Schedule (Z := Unit) limit timer σ).1) : FIA τ := by + unfold Fl4SpecA64.fl4Schedule at h + obtain ⟨⟨⟨⟩, σ1⟩, h1, h⟩ := mem_bindA.1 h + have f1 := hAwk σ1 (mem_fl4Awaken.1 h1) + obtain ⟨⟨⟨⟩, σ2⟩, h2, h⟩ := mem_bindA.1 h + have f2 := awaken_fia σ1 f1 _ h2 + obtain ⟨⟨⟨⟩, σ3⟩, h3, h⟩ := mem_bindA.1 h + have f3 := checkDomainTime_fia σ2 f2 _ h3 + have sw := hSw σ1 σ2 σ3 (mem_fl4Awaken.1 h1) h2 h3 + obtain ⟨⟨⟨⟩, σ4⟩, h4, h⟩ := mem_bindA.1 h + have f4 := selectThread_fia limit σ3 ⟨f3, sw⟩ _ h4 + exact scAndTimer_fia timer σ4 f4 _ h + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3SchedExitFieldsA.lean b/proofs/Fl4ProofsA64/P3/Stage3SchedExitFieldsA.lean new file mode 100644 index 000000000..8566ef5e5 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SchedExitFieldsA.lean @@ -0,0 +1,112 @@ +/- +P3 (aarch64, A64-P3 Stage 3, T3 G4b): **`JSyscallA`'s exit fields from the schedule exit**. + +At the final state `(s, σ)` of a kernel exit (after `schedule` and `activate_thread`), related by `YieldQueueRelA`: + +* **`hc_of_words`**: when the current thread's SC word is non-null and is the current SC (`cscW`), the spec's current + thread's TCB has `σ.curSc` as its SC (`SchedRelA.tcb`, `curScW`). It carries `P3/Stage3SchedCurA`'s word-level facts + (`cur_sc_tail`) to the spec level, where `TKeepA` (the activation) keeps them. +* **`exit_cur_fields`**: `curSc` (`cscW` is the current thread's SC word) and `cur` (the SC word is non-null and the + release-queue bit of the current thread is clear), from the spec-level `hc` (the current thread's SC is `σ.curSc`) and + the F8 `act` clause (the current thread is outside the release queue; `SchedRelA`'s `rel` list and the queue bits). +* **`JSyscallA.ofExit`**: the record from a `JStage3EntryA` and the four fields. + +Named premises: `hc` (discharged by `hc_of_words` at the schedule tail and `TKeepA` through the activation), the +non-idle current thread (the syscall entry's), F8. +-/ +import Fl4ProofsA64.P3.Stage3SchedActivateA +import Fl4ProofsA64.P3.Stage3SchedCurA +import Fl4ProofsA64.P3.Stage3SchedRunA +import Fl4ProofsA64.P3.Stage3SyscallA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **The word-level current-SC facts give the spec-level `hc`.** -/ +theorem hc_of_words {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : YieldQueueRelA (st := st) Z m κ s σ) (hnz : L.mem s (scA (L.curW s)) ≠ 0#u64) + (hcw : L.cscW s = L.mem s (scA (L.curW s))) : + ∀ x, σ.kheap σ.curThread = some (.tcb x) → x.tcbSchedContext = some σ.curSc := by + intro x hx + have ht := H.sched.curT + have hcur := H.sched.sch.curThread + obtain ⟨x', kx, ex⟩ := H.sched.sch.tcb (L.curW s) ht + rw [hcur] at hx + rw [hx] at kx + have hxx : x = x' := by injection kx with h; injection h + subst hxx + have hcs : σ.curSc = scRenA m (L.cscW s) := by + have := H.sched.sch + rw [← this.csc, this.core]; exact this.curScOfW _ this.cpuCs + rw [ex, optScA, if_neg hnz, hcs, hcw] + +/-- **`JSyscallA`'s `curSc` and `cur`** (module docstring). -/ +theorem exit_cur_fields {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : YieldQueueRelA (st := st) Z m κ s σ) (hC0 : ∀ c ∈ Z.C, c ≠ 0#u64) + (hc : ∀ x, σ.kheap σ.curThread = some (.tcb x) → x.tcbSchedContext = some σ.curSc) + (hA : σ.schedulerAction = .resumeCurThread) (hnid : σ.curThread ≠ σ.idleThread) (hF : SchedValidA σ) : + (L.mem s (scA (L.curW s)) ≠ 0#u64 → L.cscW s = L.mem s (scA (L.curW s))) ∧ + (L.mem s (inqA (L.curW s)) &&& sched.Q_RELEASE) = 0#u64 ∧ L.mem s (scA (L.curW s)) ≠ 0#u64 := by + have ht := H.sched.curT + have hcur := H.sched.sch.curThread + obtain ⟨x, kx, ex⟩ := H.sched.sch.tcb (L.curW s) ht + have hx := hc x (by rw [hcur]; exact kx) + rw [ex] at hx + have nz : L.mem s (scA (L.curW s)) ≠ 0#u64 := by + intro e; rw [optScA, if_pos e] at hx; cases hx + have hs : σ.curSc = scRenA m (L.mem s (scA (L.curW s))) := by + rw [optScA, if_neg nz] at hx; exact (Option.some.inj hx).symm + have wC : L.mem s (scA (L.curW s)) ∈ Z.C := H.sched.rest.scMem _ ht nz + have hcs : σ.curSc = scRenA m (L.cscW s) := by + have := H.sched.sch + rw [← this.csc, this.core]; exact this.curScOfW _ this.cpuCs + have cC : L.cscW s ∈ Z.C := H.sched.cscC + have hcw : L.cscW s = L.mem s (scA (L.curW s)) := H.sched.scp.inj _ cC _ wC (hcs.symm.trans hs) + refine ⟨fun _ => hcw, ?_, nz⟩ + by_contra hb + have := (H.sched.core.released (L.curW s) ht).1 hb + rw [H.sched.curOn] at this + have hm : σ.curThread ∈ σ.releaseQueue := by + rw [H.sched.rest.rel, hcur] + exact List.mem_map.2 ⟨_, this, rfl⟩ + exact (hF.act hA hnid).2 hm + +/-- **`JSyscallA` from a kernel-entry record and the four exit facts.** -/ +theorem JSyscallA.ofExit {A : Type} {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} + {κ : U64 → U64} {n : Nat} {J5 J7 : JSlotA S} {s : S} {a : A} {σ : State Unit} {m : McState} + (J : JStage3EntryA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (act : σ.schedulerAction = .resumeCurThread) (running : Runnable (L.mem s (stA (L.curW s)))) + (curSc : L.mem s (scA (L.curW s)) ≠ 0#u64 → L.cscW s = L.mem s (scA (L.curW s))) + (cur : (L.mem s (inqA (L.curW s)) &&& sched.Q_RELEASE) = 0#u64 ∧ L.mem s (scA (L.curW s)) ≠ 0#u64) : + JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m := + { J with act := act, running := running, curSc := curSc, cur := cur } + +/-- **The spec side of the kernel exit**: `schedule` then `activate_thread` from an `FIA` state leave an `FIA` state at +`resumeCurThread`. `hidle`: the idle thread's TCB is in `idleThreadState` (`SInvXA.idleTcb`), so a restarting current +thread is not the idle thread and F8's `act` clause gives it an active SC outside the release queue. -/ +theorem exit_spec (limit : Nat) (timer : State Unit → Fl4TimerView) {σ0 τ1 τ2 : State Unit} (hσ : FIA σ0) + (hAwk : ∀ σ1, fl4TimerFires σ0 σ1 → FIA σ1) + (hSw : ∀ σ1 σ2 σ3, fl4TimerFires σ0 σ1 → ((), σ2) ∈ (awaken (Z := Unit) σ1).1 → + ((), σ3) ∈ (checkDomainTime (Z := Unit) σ2).1 → SwA σ3) + (hidle : ∀ x, τ1.kheap τ1.idleThread = some (.tcb x) → x.tcbState = .idleThreadState) + (h1 : ((), τ1) ∈ (fl4Schedule (Z := Unit) limit timer σ0).1) + (h2 : ((), τ2) ∈ (fl4ActivateThread (Z := Unit) τ1).1) : + FIA τ2 ∧ τ2.schedulerAction = .resumeCurThread := by + have F1 := fl4Schedule_fia limit timer hσ hAwk hSw h1 + have A1 := fl4Schedule_actA limit timer h1 + refine activate_act τ1 A1 (fun x hx hrs => ?_) τ1 ⟨rfl, F1⟩ _ h2 + have hne : τ1.curThread ≠ τ1.idleThread := fun e => by + rw [e] at hx + rw [hidle x hx] at hrs + cases hrs + exact F1.1.act A1 hne + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3SchedExitFrA.lean b/proofs/Fl4ProofsA64/P3/Stage3SchedExitFrA.lean new file mode 100644 index 000000000..db3471087 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SchedExitFrA.lean @@ -0,0 +1,423 @@ +/- +P3 (aarch64, A64-P3 Stage 3, kernel-entry composition, Plan A, item 2): **two frames of the schedule tail**. + +* `SfKeepA`: the heap, current SC, clocks, consumed time and idle thread are kept: `awaken`, `checkDomainTime`, + `fl4SelectThread` (queues, action, current thread, domain only) and `fl4SetNextInterrupt`. +* `TmKeepA`: the clock `cur_time` and the last machine time are kept: `fl4SwitchSchedContext` (refills included). + +Generated by `handoff/tools/gen_kentry_exitfr.py` (twins of the instance lists of `P3/SpecFrameCalcA` and +`P3/SpecFrameSchedA`; the leaves are written out). No premise; new module. +-/ +import Fl4ProofsA64.P3.SpecFrameSchedA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec Classical + +noncomputable section + +structure SfKeepA (σ σ' : State Unit) : Prop where + kh : σ'.kheap = σ.kheap + csc : σ'.curSc = σ.curSc + time : σ'.curTime = σ.curTime + used : σ'.consumedTime = σ.consumedTime + idle : σ'.idleThread = σ.idleThread + lmt : σ'.machineState.lastMachineTime = σ.machineState.lastMachineTime + /-- (integration round 2, item 11) The release queue only shrinks. -/ + rls : ∀ t ∈ σ'.releaseQueue, t ∈ σ.releaseQueue + +instance : PreO SfKeepA := + ⟨fun _ => ⟨rfl, rfl, rfl, rfl, rfl, rfl, fun _ h => h⟩, fun a b => ⟨b.kh.trans a.kh, b.csc.trans a.csc, b.time.trans a.time, + b.used.trans a.used, b.idle.trans a.idle, b.lmt.trans a.lmt, fun t h => a.rls t (b.rls t h)⟩⟩ + +structure TmKeepA (σ σ' : State Unit) : Prop where + time : σ'.curTime = σ.curTime + rep : σ.reprogramTimer = true → σ'.reprogramTimer = true + lmt : σ'.machineState.lastMachineTime = σ.machineState.lastMachineTime + +instance : PreO TmKeepA := + ⟨fun _ => ⟨rfl, fun h => h, rfl⟩, fun a b => ⟨b.time.trans a.time, fun h => b.rep (a.rep h), b.lmt.trans a.lmt⟩⟩ + +abbrev SfPresA {α : Type} (m : SMonad Unit α) : Prop := PresR SfKeepA m + +class SfKInst {α : Type} (m : SMonad Unit α) : Prop where + pres : SfPresA m + +section +instance {α β : Type} {m : SMonad Unit α} {f : α → SMonad Unit β} [hm : SfKInst m] + [hf : ∀ a, SfKInst (f a)] : SfKInst (m >>= f) := + ⟨PresR.bind hm.pres fun a => (hf a).pres⟩ + +instance {α : Type} (a : α) : SfKInst (pure a : SMonad Unit α) := ⟨PresR.pure a⟩ + +instance {α : Type} (f : State Unit → α) : SfKInst (NonDet.gets f : SMonad Unit α) := ⟨PresR.gets f⟩ + +instance : SfKInst (NonDet.get : SMonad Unit (State Unit)) := ⟨PresR.get⟩ + +instance {α : Type} (f : State Unit → Option α) : SfKInst (NonDet.getsThe f : SMonad Unit α) := + ⟨PresR.getsThe f⟩ + +instance {α : Type} (o : Option α) : SfKInst (NonDet.assertOpt o : SMonad Unit α) := ⟨PresR.assertOpt o⟩ + +instance (P : Prop) : SfKInst (NonDet.assert P : SMonad Unit Unit) := ⟨PresR.assert P⟩ + +instance {α : Type} : SfKInst (NonDet.fail : SMonad Unit α) := ⟨PresR.fail⟩ + +instance {P : Prop} {d : Decidable P} {m : SMonad Unit Unit} [h : SfKInst m] : + SfKInst (@NonDet.when (State Unit) P d m) := ⟨PresR.when h.pres⟩ + +instance {P : Prop} {d : Decidable P} {m : SMonad Unit Unit} [h : SfKInst m] : + SfKInst (@NonDet.unless (State Unit) P d m) := ⟨PresR.unless' h.pres⟩ + +instance {α : Type} {P : Prop} {d : Decidable P} {a b : SMonad Unit α} [ha : SfKInst a] [hb : SfKInst b] : + SfKInst (@ite _ P d a b) := ⟨PresR.ite' ha.pres hb.pres⟩ + +instance {β γ : Type} {f : γ → SMonad Unit β} [hf : ∀ a, SfKInst (f a)] (l : List γ) : + SfKInst (NonDet.mapM f l) := ⟨PresR.mapM (fun a => (hf a).pres) l⟩ + +instance {α : Type} {C : α → State Unit → Prop} {B : α → SMonad Unit α} [hB : ∀ a, SfKInst (B a)] (r : α) : + SfKInst (NonDet.whileLoop C B r) := ⟨PresR.whileLoop (fun a => (hB a).pres) r⟩ + +instance {α : Type} (S : Set α × Prop) : SfKInst (NonDet.selectF S : SMonad Unit α) := ⟨PresR.selectF S⟩ + +instance {α : Type} {f : α → SMonad Unit Unit} [hf : ∀ a, SfKInst (f a)] (o : Option α) : + SfKInst (NonDet.maybeM f o) := by + cases o + · exact ⟨PresR.pure _⟩ + · exact hf _ + + +macro "sfkres" : tactic => `(tactic| + repeat' (first + | exact SfKInst.pres + | refine PresR.bind_pure _ _ ?_ + | refine PresR.bind ?_ (fun _ => ?_) + | refine PresR.when ?_ + | refine PresR.unless' ?_ + | refine PresR.ite' ?_ ?_ + | (refine PresR.modify _ (fun _ => ⟨rfl, rfl, rfl, rfl, rfl, rfl, fun _ h => h⟩)) + | dsimp only + | split)) + +theorem machineRestLift_lmtA {α : Type} (f : MachineRestMonad α) (ms : MachineState) (x : α × MachineState) + (hx : x ∈ (machineRestLift f ms).1) : x.2.lastMachineTime = ms.lastMachineTime := by + obtain ⟨y1, hy1, hx⟩ := hx + have e1 : y1 = (ms.machineStateRest, ms) := hy1 + subst e1 + obtain ⟨⟨⟨a, mr'⟩, s2⟩, hy2, hx⟩ := hx + have e2 : s2 = ms := hy2.2 + subst e2 + obtain ⟨y3, hy3, hx⟩ := hx + have e3 : y3 = ((), { s2 with machineStateRest := mr' }) := hy3 + subst e3 + have e4 : x = (a, { s2 with machineStateRest := mr' }) := hx + rw [e4] + +theorem doExtendedOp_qkA (eop : DetExtMonad Unit) : SfPresA (doExtendedOp (Z := Unit) eop) := by + unfold doExtendedOp + refine PresR.bind PresR.get fun _ => PresR.bind (PresR.selectF _) fun _ => ?_ + exact PresR.modify _ fun _ => ⟨rfl, rfl, rfl, rfl, rfl, rfl, fun _ h => h⟩ + +instance (eop : DetExtMonad Unit) : SfKInst (doExtendedOp (Z := Unit) eop) := ⟨doExtendedOp_qkA eop⟩ + +theorem setDeadline_qkA (w : Word64) : SfPresA (doMachineOp (Z := Unit) (setDeadline w)) := by + intro σ r hr + obtain ⟨y1, hy1, hr⟩ := hr + have e1 : y1 = (σ.machineState, σ) := hy1 + subst e1 + obtain ⟨⟨⟨a, ms'⟩, s2⟩, hy2, hr⟩ := hr + have hm : (a, ms') ∈ (setDeadline w σ.machineState).1 := hy2.1 + have e2 : s2 = σ := hy2.2 + subst e2 + obtain ⟨y3, hy3, hr⟩ := hr + have e3 : y3 = ((), { s2 with machineState := ms' }) := hy3 + subst e3 + have e4 : r = (a, { s2 with machineState := ms' }) := hr + rw [e4] + refine ⟨rfl, rfl, rfl, rfl, rfl, ?_, fun _ h => h⟩ + unfold setDeadline machineOpLift at hm + exact machineRestLift_lmtA _ _ _ hm + +instance (w : Word64) : SfKInst (doMachineOp (Z := Unit) (setDeadline w)) := ⟨setDeadline_qkA w⟩ + +/-! ## Readers and small writers -/ + +instance {α : Type} (f : Tcb → α) (t : ObjRef) : SfKInst (threadGet (Z := Unit) f t) := + ⟨by unfold threadGet; sfkres⟩ +instance (f : Tcb → Option ObjRef) (r : ObjRef) : SfKInst (getTcbObjRef (Z := Unit) f r) := + ⟨by unfold getTcbObjRef; sfkres⟩ +instance (r : ObjRef) : SfKInst (getThreadState (Z := Unit) r) := ⟨by unfold getThreadState; sfkres⟩ +instance (d : Domain) (p : Priority) : SfKInst (getTcbQueue (Z := Unit) d p) := ⟨by unfold getTcbQueue; sfkres⟩ +instance (d : Domain) (p : Priority) (q : ReadyQueue) : SfKInst (setTcbQueue (Z := Unit) d p q) := + ⟨by unfold setTcbQueue; sfkres⟩ +instance (a : ObjRef → List ObjRef → List ObjRef) (t : ObjRef) : SfKInst (tcbSchedAction (Z := Unit) a t) := + ⟨by unfold tcbSchedAction; sfkres⟩ +instance (a : SchedulerAction) : SfKInst (setSchedulerAction (Z := Unit) a) := + ⟨by unfold setSchedulerAction; sfkres⟩ +/-- (integration round 2, item 11) `tcb_release_remove` keeps the heap and only shrinks the release queue (the queue +it writes is the old queue, dequeued). -/ +instance (t : ObjRef) : SfKInst (tcbReleaseRemove (Z := Unit) t) := ⟨fun σ r hr => by + unfold tcbReleaseRemove at hr + obtain ⟨y1, hy1, hr⟩ := mem_bindA.1 hr + rw [Set.mem_singleton_iff.1 hy1] at hr + obtain ⟨y2, hy2, hr⟩ := mem_bindA.1 hr + have h2 : ∃ b, y2.2 = { σ with reprogramTimer := b } := by + unfold NonDet.when at hy2 + split at hy2 + · exact ⟨true, by rw [Set.mem_singleton_iff.1 hy2]⟩ + · exact ⟨σ.reprogramTimer, by rw [Set.mem_singleton_iff.1 hy2]⟩ + obtain ⟨y3, hy3, hr⟩ := mem_bindA.1 hr + rw [Set.mem_singleton_iff.1 hy3] at hr + rw [Set.mem_singleton_iff.1 hr] + obtain ⟨b, hb⟩ := h2 + rw [hb] + exact ⟨rfl, rfl, rfl, rfl, rfl, rfl, fun u hu => (List.mem_filter.1 hu).1⟩⟩ + +instance (p : ObjRef) : SfKInst (getObject (Z := Unit) p) := ⟨by unfold getObject; sfkres⟩ +instance (p : ObjRef) : SfKInst (getSchedContext (Z := Unit) p) := ⟨by unfold getSchedContext; sfkres⟩ +instance (f : SchedContext → Option ObjRef) (p : ObjRef) : SfKInst (getScObjRef (Z := Unit) f p) := + ⟨by unfold getScObjRef; sfkres⟩ +instance (p : ObjRef) : SfKInst (getRefills (Z := Unit) p) := ⟨by unfold getRefills; sfkres⟩ +instance (p : ObjRef) : SfKInst (getRefillHead (Z := Unit) p) := ⟨by unfold getRefillHead; sfkres⟩ +instance (p : ObjRef) : SfKInst (getRefillTail (Z := Unit) p) := ⟨by unfold getRefillTail; sfkres⟩ +instance (p : ObjRef) : SfKInst (getScRefillReady (Z := Unit) p) := ⟨by unfold getScRefillReady; sfkres⟩ +instance (p : ObjRef) (u : Ticks) : SfKInst (getScRefillSufficient (Z := Unit) p u) := + ⟨by unfold getScRefillSufficient; sfkres⟩ +instance (p : ObjRef) (u : Ticks) : SfKInst (fl4GetScRefillSufficient (Z := Unit) p u) := + ⟨by unfold fl4GetScRefillSufficient; sfkres⟩ +instance (p : ObjRef) : SfKInst (getScActive (Z := Unit) p) := ⟨by unfold getScActive; sfkres⟩ +instance (p : ObjRef) : SfKInst (isRoundRobin (Z := Unit) p) := ⟨by unfold isRoundRobin; sfkres⟩ + +/-! ## Scheduler queues -/ + +instance : SfKInst (rescheduleRequired (Z := Unit)) := ⟨by unfold rescheduleRequired; sfkres⟩ +instance (t : ObjRef) : SfKInst (possibleSwitchTo (Z := Unit) t) := ⟨by unfold possibleSwitchTo; sfkres⟩ + + +/-! ## Tops -/ + +instance : SfKInst (fl4ChooseThread (Z := Unit)) := ⟨by unfold fl4ChooseThread; sfkres⟩ +instance (t : ObjRef) : SfKInst (fl4SwitchToThread (Z := Unit) t) := ⟨by unfold fl4SwitchToThread; sfkres⟩ +instance (limit : Nat) : SfKInst (fl4NextDomain (Z := Unit) limit) := ⟨by unfold fl4NextDomain; sfkres⟩ +instance (limit : Nat) : SfKInst (fl4ChooseNewThread (Z := Unit) limit) := ⟨by unfold fl4ChooseNewThread; sfkres⟩ +instance (limit : Nat) (c cu i : ObjRef) (d : Domain) (w : Bool) : + SfKInst (fl4SelectCandidate (Z := Unit) limit c cu i d w) := ⟨by unfold fl4SelectCandidate; sfkres⟩ + +instance (p : CslotPtr) : SfKInst (getCap (Z := Unit) p) := ⟨by unfold getCap; sfkres⟩ + + +instance (q : List ObjRef) : SfKInst (fl4ReleaseDeadline (Z := Unit) q) := ⟨by unfold fl4ReleaseDeadline; sfkres⟩ +instance (timer : State Unit → Fl4TimerView) : SfKInst (fl4SetNextInterrupt (Z := Unit) timer) := + ⟨by unfold fl4SetNextInterrupt; sfkres⟩ +instance : SfKInst (tcbReleaseDequeue (Z := Unit)) := ⟨by unfold tcbReleaseDequeue; sfkres⟩ +instance : SfKInst (awaken (Z := Unit)) := ⟨by unfold awaken; sfkres⟩ +instance : SfKInst (checkDomainTime (Z := Unit)) := ⟨by unfold checkDomainTime; sfkres⟩ + +theorem awaken_qkA : SfPresA (awaken (Z := Unit)) := (inferInstance : SfKInst (awaken (Z := Unit))).pres +theorem checkDomainTime_qkA : SfPresA (checkDomainTime (Z := Unit)) := + (inferInstance : SfKInst (checkDomainTime (Z := Unit))).pres +theorem fl4SelectThread_qkA (limit : Nat) : SfPresA (fl4SelectThread (Z := Unit) limit) := by + unfold fl4SelectThread fl4SelectAction; sfkres +theorem fl4SetNextInterrupt_qkA (timer : State Unit → Fl4TimerView) : + SfPresA (fl4SetNextInterrupt (Z := Unit) timer) := + (inferInstance : SfKInst (fl4SetNextInterrupt (Z := Unit) timer)).pres +end + +abbrev TmPresA {α : Type} (m : SMonad Unit α) : Prop := PresR TmKeepA m + +class TmInst {α : Type} (m : SMonad Unit α) : Prop where + pres : TmPresA m + +section +instance {α β : Type} {m : SMonad Unit α} {f : α → SMonad Unit β} [hm : TmInst m] + [hf : ∀ a, TmInst (f a)] : TmInst (m >>= f) := + ⟨PresR.bind hm.pres fun a => (hf a).pres⟩ + +instance {α : Type} (a : α) : TmInst (pure a : SMonad Unit α) := ⟨PresR.pure a⟩ + +instance {α : Type} (f : State Unit → α) : TmInst (NonDet.gets f : SMonad Unit α) := ⟨PresR.gets f⟩ + +instance : TmInst (NonDet.get : SMonad Unit (State Unit)) := ⟨PresR.get⟩ + +instance {α : Type} (f : State Unit → Option α) : TmInst (NonDet.getsThe f : SMonad Unit α) := + ⟨PresR.getsThe f⟩ + +instance {α : Type} (o : Option α) : TmInst (NonDet.assertOpt o : SMonad Unit α) := ⟨PresR.assertOpt o⟩ + +instance (P : Prop) : TmInst (NonDet.assert P : SMonad Unit Unit) := ⟨PresR.assert P⟩ + +instance {α : Type} : TmInst (NonDet.fail : SMonad Unit α) := ⟨PresR.fail⟩ + +instance {P : Prop} {d : Decidable P} {m : SMonad Unit Unit} [h : TmInst m] : + TmInst (@NonDet.when (State Unit) P d m) := ⟨PresR.when h.pres⟩ + +instance {P : Prop} {d : Decidable P} {m : SMonad Unit Unit} [h : TmInst m] : + TmInst (@NonDet.unless (State Unit) P d m) := ⟨PresR.unless' h.pres⟩ + +instance {α : Type} {P : Prop} {d : Decidable P} {a b : SMonad Unit α} [ha : TmInst a] [hb : TmInst b] : + TmInst (@ite _ P d a b) := ⟨PresR.ite' ha.pres hb.pres⟩ + +instance {β γ : Type} {f : γ → SMonad Unit β} [hf : ∀ a, TmInst (f a)] (l : List γ) : + TmInst (NonDet.mapM f l) := ⟨PresR.mapM (fun a => (hf a).pres) l⟩ + +instance {α : Type} {C : α → State Unit → Prop} {B : α → SMonad Unit α} [hB : ∀ a, TmInst (B a)] (r : α) : + TmInst (NonDet.whileLoop C B r) := ⟨PresR.whileLoop (fun a => (hB a).pres) r⟩ + +instance {α : Type} (S : Set α × Prop) : TmInst (NonDet.selectF S : SMonad Unit α) := ⟨PresR.selectF S⟩ + +instance {α : Type} {f : α → SMonad Unit Unit} [hf : ∀ a, TmInst (f a)] (o : Option α) : + TmInst (NonDet.maybeM f o) := by + cases o + · exact ⟨PresR.pure _⟩ + · exact hf _ + + +macro "tmres" : tactic => `(tactic| + repeat' (first + | exact TmInst.pres + | refine PresR.bind_pure _ _ ?_ + | refine PresR.bind ?_ (fun _ => ?_) + | refine PresR.when ?_ + | refine PresR.unless' ?_ + | refine PresR.ite' ?_ ?_ + | (refine PresR.modify _ (fun _ => ⟨rfl, by first | exact fun h => h | exact fun _ => rfl, rfl⟩)) + | dsimp only + | split)) + +theorem setObject_tmA {σ : State Unit} {p : ObjRef} {o : KernelObject} {r : Unit × State Unit} + (h : r ∈ (setObject (Z := Unit) p o σ).1) : TmKeepA σ r.2 := by + obtain ⟨-, -, e⟩ := setObject_memA h + rw [e]; exact ⟨rfl, fun h => h, rfl⟩ + +theorem threadSet_tmA (f : Tcb → Tcb) (t : ObjRef) : TmPresA (threadSet (Z := Unit) f t) := by + intro σ r hr + obtain ⟨y1, hy1, h⟩ := hr + have hy1' : y1 ∈ (NonDet.assertOpt (getTcb (Z := Unit) t σ) σ).1 := hy1 + cases hg : getTcb (Z := Unit) t σ with + | none => rw [hg] at hy1'; exact absurd hy1' (Set.notMem_empty _) + | some x => + rw [hg] at hy1' + have e1 : y1 = (x, σ) := hy1' + subst e1 + exact setObject_tmA h + +theorem updateSchedContext_tmA (f : SchedContext → SchedContext) (p : ObjRef) : + TmPresA (updateSchedContext (Z := Unit) p f) := by + intro σ r hr + obtain ⟨y1, hy1, h⟩ := hr + have hy1' : y1 ∈ (NonDet.assertOpt (σ.kheap p) σ).1 := hy1 + cases hk : σ.kheap p with + | none => rw [hk] at hy1'; exact absurd hy1' (Set.notMem_empty _) + | some ko => + rw [hk] at hy1' + have e1 : y1 = (ko, σ) := hy1' + subst e1 + cases ko with + | schedContext sc n => exact setObject_tmA h + | _ => exact absurd h (Set.notMem_empty _) + +instance (f : Tcb → Tcb) (t : ObjRef) : TmInst (threadSet (Z := Unit) f t) := ⟨threadSet_tmA f t⟩ +instance (p : ObjRef) (f : SchedContext → SchedContext) : TmInst (updateSchedContext (Z := Unit) p f) := + ⟨updateSchedContext_tmA f p⟩ + +theorem doExtendedOp_tmA (eop : DetExtMonad Unit) : TmPresA (doExtendedOp (Z := Unit) eop) := by + unfold doExtendedOp + refine PresR.bind PresR.get fun _ => PresR.bind (PresR.selectF _) fun _ => ?_ + exact PresR.modify _ fun _ => ⟨rfl, fun h => h, rfl⟩ + +instance (eop : DetExtMonad Unit) : TmInst (doExtendedOp (Z := Unit) eop) := ⟨doExtendedOp_tmA eop⟩ + +/-! ## Readers and small writers -/ + +instance {α : Type} (f : Tcb → α) (t : ObjRef) : TmInst (threadGet (Z := Unit) f t) := + ⟨by unfold threadGet; tmres⟩ +instance (f : Tcb → Option ObjRef) (r : ObjRef) : TmInst (getTcbObjRef (Z := Unit) f r) := + ⟨by unfold getTcbObjRef; tmres⟩ +instance (r : ObjRef) : TmInst (getThreadState (Z := Unit) r) := ⟨by unfold getThreadState; tmres⟩ +instance (d : Domain) (p : Priority) : TmInst (getTcbQueue (Z := Unit) d p) := ⟨by unfold getTcbQueue; tmres⟩ +instance (d : Domain) (p : Priority) (q : ReadyQueue) : TmInst (setTcbQueue (Z := Unit) d p q) := + ⟨by unfold setTcbQueue; tmres⟩ +instance (a : ObjRef → List ObjRef → List ObjRef) (t : ObjRef) : TmInst (tcbSchedAction (Z := Unit) a t) := + ⟨by unfold tcbSchedAction; tmres⟩ +instance (a : SchedulerAction) : TmInst (setSchedulerAction (Z := Unit) a) := + ⟨by unfold setSchedulerAction; tmres⟩ +instance (t : ObjRef) : TmInst (tcbReleaseRemove (Z := Unit) t) := ⟨by unfold tcbReleaseRemove; tmres⟩ + +instance (p : ObjRef) : TmInst (getObject (Z := Unit) p) := ⟨by unfold getObject; tmres⟩ +instance (p : ObjRef) : TmInst (getSchedContext (Z := Unit) p) := ⟨by unfold getSchedContext; tmres⟩ +instance (f : SchedContext → Option ObjRef) (p : ObjRef) : TmInst (getScObjRef (Z := Unit) f p) := + ⟨by unfold getScObjRef; tmres⟩ +instance (p : ObjRef) : TmInst (getRefills (Z := Unit) p) := ⟨by unfold getRefills; tmres⟩ +instance (p : ObjRef) : TmInst (getRefillHead (Z := Unit) p) := ⟨by unfold getRefillHead; tmres⟩ +instance (p : ObjRef) : TmInst (getRefillTail (Z := Unit) p) := ⟨by unfold getRefillTail; tmres⟩ +instance (p : ObjRef) : TmInst (getScRefillReady (Z := Unit) p) := ⟨by unfold getScRefillReady; tmres⟩ +instance (p : ObjRef) (u : Ticks) : TmInst (getScRefillSufficient (Z := Unit) p u) := + ⟨by unfold getScRefillSufficient; tmres⟩ +instance (p : ObjRef) (u : Ticks) : TmInst (fl4GetScRefillSufficient (Z := Unit) p u) := + ⟨by unfold fl4GetScRefillSufficient; tmres⟩ +instance (p : ObjRef) : TmInst (getScActive (Z := Unit) p) := ⟨by unfold getScActive; tmres⟩ +instance (p : ObjRef) : TmInst (isRoundRobin (Z := Unit) p) := ⟨by unfold isRoundRobin; tmres⟩ + +/-! ## Scheduler queues -/ + +instance : TmInst (rescheduleRequired (Z := Unit)) := ⟨by unfold rescheduleRequired; tmres⟩ +instance (t : ObjRef) : TmInst (possibleSwitchTo (Z := Unit) t) := ⟨by unfold possibleSwitchTo; tmres⟩ + + +/-! ## Tops -/ + +instance : TmInst (fl4ChooseThread (Z := Unit)) := ⟨by unfold fl4ChooseThread; tmres⟩ +instance (t : ObjRef) : TmInst (fl4SwitchToThread (Z := Unit) t) := ⟨by unfold fl4SwitchToThread; tmres⟩ +instance (limit : Nat) : TmInst (fl4NextDomain (Z := Unit) limit) := ⟨by unfold fl4NextDomain; tmres⟩ +instance (limit : Nat) : TmInst (fl4ChooseNewThread (Z := Unit) limit) := ⟨by unfold fl4ChooseNewThread; tmres⟩ +instance (limit : Nat) (c cu i : ObjRef) (d : Domain) (w : Bool) : + TmInst (fl4SelectCandidate (Z := Unit) limit c cu i d w) := ⟨by unfold fl4SelectCandidate; tmres⟩ + +instance (p : CslotPtr) : TmInst (getCap (Z := Unit) p) := ⟨by unfold getCap; tmres⟩ + +/-! ### Refills -/ + +instance (p : ObjRef) (f : Refill → Refill) : TmInst (updateRefillHd (Z := Unit) p f) := + ⟨by unfold updateRefillHd; tmres⟩ +instance (p : ObjRef) (f : Refill → Refill) : TmInst (updateRefillTl (Z := Unit) p f) := + ⟨by unfold updateRefillTl; tmres⟩ +instance (p : ObjRef) (r : Refill) : TmInst (refillAddTail (Z := Unit) p r) := ⟨by unfold refillAddTail; tmres⟩ +instance (p : ObjRef) : TmInst (refillPopHead (Z := Unit) p) := ⟨by unfold refillPopHead; tmres⟩ +instance (p : ObjRef) : TmInst (refillSize (Z := Unit) p) := ⟨by unfold refillSize; tmres⟩ +instance (p : ObjRef) : TmInst (refillSingle (Z := Unit) p) := ⟨by unfold refillSingle; tmres⟩ +instance (p : ObjRef) : TmInst (refillFull (Z := Unit) p) := ⟨by unfold refillFull; tmres⟩ +instance (p : ObjRef) (r : Refill) : TmInst (scheduleUsed (Z := Unit) p r) := ⟨by unfold scheduleUsed; tmres⟩ +instance (p : ObjRef) : TmInst (mergeOverlappingRefills (Z := Unit) p) := ⟨by unfold mergeOverlappingRefills; tmres⟩ +instance (p : ObjRef) : TmInst (refillHeadOverlappingLoop (Z := Unit) p) := + ⟨by unfold refillHeadOverlappingLoop; tmres⟩ +instance (p : ObjRef) : TmInst (refillUnblockCheck (Z := Unit) p) := ⟨by unfold refillUnblockCheck; tmres⟩ +instance (o : Option ObjRef) (a b : Option Bool) : TmInst (ifCondRefillUnblockCheck (Z := Unit) o a b) := + ⟨by unfold ifCondRefillUnblockCheck; tmres⟩ +instance (p : ObjRef) : TmInst (mergeNonoverlappingHeadRefill (Z := Unit) p) := + ⟨by unfold mergeNonoverlappingHeadRefill; tmres⟩ +instance (m : Ticks) (p : ObjRef) : TmInst (headInsufficientLoopWith (Z := Unit) m p) := + ⟨by unfold headInsufficientLoopWith; tmres⟩ +instance (p : ObjRef) (u : Ticks) : TmInst (chargeEntireHeadRefill (Z := Unit) p u) := + ⟨by unfold chargeEntireHeadRefill; tmres⟩ +instance (l : Ticks) (p : ObjRef) (u : Ticks) : TmInst (handleOverrunWith (Z := Unit) l p u) := + ⟨by unfold handleOverrunWith; tmres⟩ +instance (l m u : Ticks) : TmInst (refillBudgetCheckWith (Z := Unit) l m u) := + ⟨by unfold refillBudgetCheckWith; tmres⟩ +instance (u : Ticks) : TmInst (fl4RefillBudgetCheck (Z := Unit) u) := ⟨by unfold fl4RefillBudgetCheck; tmres⟩ +instance (u : Ticks) : TmInst (refillBudgetCheckRoundRobin (Z := Unit) u) := + ⟨by unfold refillBudgetCheckRoundRobin; tmres⟩ +instance : TmInst (fl4CommitTime (Z := Unit)) := ⟨by unfold fl4CommitTime; tmres⟩ + +/-! ### Switch, timer, awaken, domain -/ + +instance : TmInst (fl4SwitchSchedContext (Z := Unit)) := ⟨by unfold fl4SwitchSchedContext; tmres⟩ + +instance : TmInst (fl4SwitchSchedContext (Z := Unit)) := ⟨by unfold fl4SwitchSchedContext; tmres⟩ + +theorem fl4SwitchSchedContext_tmA : TmPresA (fl4SwitchSchedContext (Z := Unit)) := + (inferInstance : TmInst (fl4SwitchSchedContext (Z := Unit))).pres +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3SchedHoareA.lean b/proofs/Fl4ProofsA64/P3/Stage3SchedHoareA.lean new file mode 100644 index 000000000..7cbb296fb --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SchedHoareA.lean @@ -0,0 +1,145 @@ +/- +P3 (aarch64, A64-P3 Stage 3, T3 G4b / F8): **a Hoare-style layer over the spec scheduler computations** and the +facts of `schedulable` that F8 (`P3/Stage3SchedValidA`) uses. + +* **`HT P m Q`**: from every state satisfying `P`, every outcome `(a, σ')` of `m` satisfies `Q a σ'`. Closed under + `bind`, consequence, `pure`, `gets`, `modify`, `when`, `unless'`, `ite`, `assert`, `assertOpt`, `threadGet` + (`HT.bind`, ...). Unlike `PresR` the pre-condition is a state predicate, which F8's enqueue steps need (the enqueued + thread is `schedulable` at that point). +* **`ActTcbA σ t`**: `t`'s TCB has a bound SC that is active (`ScActiveAtA`). +* **`schedulable_actA`**: `schedulable t σ = true` gives `ActTcbA σ t`, `t ∉ releaseQueue` and a runnable state. + +No premise; new module. +-/ +import Fl4ProofsA64.P3.Stage3SchedValidA +import Fl4ProofsA64.P3.SpecFrA +import Fl4ProofsA64.P3.SpecFrameCalcA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P3 Classical + +noncomputable section + +/-- **Hoare triple over a spec computation** (module docstring). -/ +def HT {α : Type} (P : State Unit → Prop) (m : SMonad Unit α) (Q : α → State Unit → Prop) : Prop := + ∀ σ, P σ → ∀ r ∈ (m σ).1, Q r.1 r.2 + +namespace HT +variable {α β : Type} {P P' : State Unit → Prop} {Q Q' : α → State Unit → Prop} + +theorem mono {m : SMonad Unit α} (h : HT P m Q) (hp : ∀ σ, P' σ → P σ) (hq : ∀ a σ, Q a σ → Q' a σ) : + HT P' m Q' := fun σ hσ r hr => hq _ _ (h σ (hp σ hσ) r hr) + +theorem bind {m : SMonad Unit α} {f : α → SMonad Unit β} {R : β → State Unit → Prop} + (h1 : HT P m Q) (h2 : ∀ a, HT (Q a) (f a) R) : HT P (m >>= f) R := fun σ hσ r hr => by + obtain ⟨y, hy, hr'⟩ := mem_bindA.1 hr + exact h2 y.1 y.2 (h1 σ hσ y hy) r hr' + +theorem pure (a : α) (h : ∀ σ, P σ → Q a σ) : HT P (Pure.pure a : SMonad Unit α) Q := fun σ hσ r hr => by + have e : r = (a, σ) := hr + rw [e]; exact h σ hσ + +theorem pure' (a : α) (h : ∀ σ, P σ → Q a σ) : HT P (NonDet.pure a : SMonad Unit α) Q := fun σ hσ r hr => by + have e : r = (a, σ) := hr + rw [e]; exact h σ hσ + +theorem gets (f : State Unit → α) (h : ∀ σ, P σ → Q (f σ) σ) : HT P (NonDet.gets f : SMonad Unit α) Q := + fun σ hσ r hr => by + have e : r = (f σ, σ) := hr + rw [e]; exact h σ hσ + +theorem modify (f : State Unit → State Unit) {Q : Unit → State Unit → Prop} (h : ∀ σ, P σ → Q () (f σ)) : + HT P (NonDet.modify f : SMonad Unit Unit) Q := fun σ hσ r hr => by + have e : r = ((), f σ) := hr + rw [e]; exact h σ hσ + +theorem assert {c : Prop} {Q : Unit → State Unit → Prop} (h : c → ∀ σ, P σ → Q () σ) : + HT P (NonDet.assert c : SMonad Unit Unit) Q := fun σ hσ r hr => by + obtain ⟨hc, e⟩ := hr + rw [e]; exact h hc σ hσ + +theorem assertOpt (o : Option α) (h : ∀ a, o = some a → ∀ σ, P σ → Q a σ) : + HT P (NonDet.assertOpt o : SMonad Unit α) Q := fun σ hσ r hr => by + cases o with + | none => exact absurd hr (Set.notMem_empty _) + | some a => + have e : r = (a, σ) := hr + rw [e]; exact h a rfl σ hσ + +theorem getsThe (f : State Unit → Option α) (h : ∀ σ, P σ → ∀ a, f σ = some a → Q a σ) : + HT P (NonDet.getsThe f : SMonad Unit α) Q := fun σ hσ r hr => by + change r ∈ (NonDet.assertOpt (f σ) σ).1 at hr + cases hf : f σ with + | none => rw [hf] at hr; exact absurd hr (Set.notMem_empty _) + | some a => + rw [hf] at hr + have e : r = (a, σ) := hr + rw [e]; exact h σ hσ a hf + +theorem when {c : Prop} {d : Decidable c} {m : SMonad Unit Unit} {Q : Unit → State Unit → Prop} + (hm : c → HT P m Q) (hn : ¬ c → ∀ σ, P σ → Q () σ) : HT P (@NonDet.when (State Unit) c d m) Q := by + unfold NonDet.when + by_cases hc : c + · simp only [hc, if_true]; exact hm hc + · simp only [hc, if_false]; exact pure' _ (hn hc) + +theorem unless' {c : Prop} {d : Decidable c} {m : SMonad Unit Unit} {Q : Unit → State Unit → Prop} + (hm : ¬ c → HT P m Q) (hn : c → ∀ σ, P σ → Q () σ) : HT P (@NonDet.unless (State Unit) c d m) Q := by + unfold NonDet.unless + by_cases hc : c + · simp only [hc, if_true]; exact pure' _ (hn hc) + · simp only [hc, if_false]; exact hm hc + +theorem ite {c : Prop} {d : Decidable c} {a b : SMonad Unit α} (ha : c → HT P a Q) (hb : ¬ c → HT P b Q) : + HT P (@ite _ c d a b) Q := by + by_cases hc : c + · simp only [hc, if_true]; exact ha hc + · simp only [hc, if_false]; exact hb hc + +theorem threadGet (f : Tcb → α) (t : ObjRef) + (h : ∀ σ, P σ → ∀ x, σ.kheap t = some (.tcb x) → Q (f x) σ) : HT P (threadGet (Z := Unit) f t) Q := + fun σ hσ r hr => by + unfold Fl4SpecA64.threadGet at hr + obtain ⟨y, hy, hr'⟩ := mem_bindA.1 hr + change y ∈ (NonDet.assertOpt (getTcb (Z := Unit) t σ) σ).1 at hy + cases hg : getTcb (Z := Unit) t σ with + | none => rw [hg] at hy; exact absurd hy (Set.notMem_empty _) + | some x => + rw [hg] at hy + have ey : y = (x, σ) := hy + subst ey + have e : r = (f x, σ) := hr' + rw [e]; exact h σ hσ x (getTcb_kheapA hg) + +end HT + +/-- `t`'s TCB has a bound, active SC. -/ +def ActTcbA (σ : State Unit) (t : ObjRef) : Prop := + ∃ x sc, σ.kheap t = some (.tcb x) ∧ x.tcbSchedContext = some sc ∧ ScActiveAtA σ sc + +/-- **`schedulable` gives an active SC, release-queue absence and a runnable state.** -/ +theorem schedulable_actA {σ : State Unit} {t : ObjRef} (h : schedulable t σ = true) : + ActTcbA σ t ∧ t ∉ σ.releaseQueue ∧ ∃ x, σ.kheap t = some (.tcb x) ∧ runnable x.tcbState = true := by + unfold schedulable at h + obtain ⟨h1, h2, h3⟩ := of_decide_eq_true h + rcases hk : σ.kheap t with _ | ko + · simp [optPred, optMapSome, optMap, tcbsOf, tcbsOfKh, hk] at h1 + · cases ko with + | tcb x => + simp only [optPred, optMapSome, optMap, tcbsOf, tcbsOfKh, tcbOf, hk] at h1 h2 + rcases hs : x.tcbSchedContext with _ | sc + · simp [optPred, optMap, optMapSome, tcbsOf, tcbsOfKh, tcbOf, scsOf, scsOfKh, scOf, hk, hs] at h2 + · rcases hk2 : σ.kheap sc with _ | ko2 + · simp [optPred, optMap, optMapSome, tcbsOf, tcbsOfKh, tcbOf, scsOf, scsOfKh, scOf, hk, hs, hk2] at h2 + · cases ko2 with + | schedContext y n => + have e : scsOf σ sc = some y := by unfold scsOf scsOfKh optMap; rw [hk2]; rfl + simp only [hs, e] at h2 + exact ⟨⟨x, sc, hk, hs, y, n, hk2, h2⟩, h3, x, rfl, h1⟩ + | _ => simp [optPred, optMap, optMapSome, tcbsOf, tcbsOfKh, tcbOf, scsOf, scsOfKh, scOf, hk, hs, hk2] at h2 + | _ => simp [optPred, optMapSome, optMap, tcbsOf, tcbsOfKh, tcbOf, hk] at h1 + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3SchedIdleA.lean b/proofs/Fl4ProofsA64/P3/Stage3SchedIdleA.lean new file mode 100644 index 000000000..535b95d12 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SchedIdleA.lean @@ -0,0 +1,110 @@ +/- +P3 (aarch64, A64-P3 Stage 3, T3 G4b / F8, queue item 3): **the idle thread's TCB keeps `idleThreadState` across the +tail of `schedule`** (`awaken`, `checkDomainTime`, `fl4SelectThread`, `fl4ScAndTimer`), for `exit_spec`'s `hidle`. + +`TcbCellsEq σ σ'`: the idle thread and every TCB object are kept (the queues, the action and the current thread are not +constrained). `KPresInst m`: every result state of `m` is `TcbCellsEq`-related to its start. The leaves already proved for +`ActKeepA` (`APresInst`: reads, SC writes, `reprogramTimer` / `curSc` writes, `doMachineOp`, `doExtendedOp`) carry over by a +generic instance (`ActKeepA` keeps every TCB cell and the idle thread); the new leaves write only queues, the scheduler +action, the current thread and the domain (`modify`), so they keep every TCB cell by `rfl`. + +* `schedTail_cells`: the four tail steps are `TcbCellsEq`. +* `fl4Schedule_idleSt`: from `IdleSt` at every post-fire state, `IdleSt` after `fl4Schedule`. The fire part + (`sendSignal` never wakes the idle thread) is queue item 1's induction. + +No premise; new module. +-/ +import Fl4ProofsA64.P3.Stage3SchedExitFieldsA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P3 Classical + +noncomputable section + +/-- The idle thread and every TCB object are kept. -/ +def TcbCellsEq (σ σ' : State Unit) : Prop := + σ'.idleThread = σ.idleThread ∧ ∀ p x, σ.kheap p = some (.tcb x) ↔ σ'.kheap p = some (.tcb x) + +instance : PreO TcbCellsEq where + refl _ := ⟨rfl, fun _ _ => Iff.rfl⟩ + trans h1 h2 := ⟨h2.1.trans h1.1, fun p x => (h1.2 p x).trans (h2.2 p x)⟩ + +theorem TcbCellsEq.ofAct {σ σ' : State Unit} (h : ActKeepA σ σ') : TcbCellsEq σ σ' := + ⟨h.2.2.2.2.2.1, fun p x => (h.2.2.2.1 p x).symm⟩ + +/-- The idle thread's TCB, if any, is in `idleThreadState`. -/ +def IdleSt (σ : State Unit) : Prop := + ∀ x, σ.kheap σ.idleThread = some (.tcb x) → x.tcbState = .idleThreadState + +theorem IdleSt.ofCells {σ σ' : State Unit} (h : IdleSt σ) (c : TcbCellsEq σ σ') : IdleSt σ' := fun x hx => by + rw [c.1] at hx + exact h x ((c.2 _ x).2 hx) + +class KPresInst {α : Type} (m : SMonad Unit α) : Prop where + pres : PresR TcbCellsEq m + +instance (priority := 10) {α : Type} {m : SMonad Unit α} [h : APresInst m] : KPresInst m := + ⟨fun s x hx => TcbCellsEq.ofAct (h.pres s x hx)⟩ + +instance (priority := 900) {α β : Type} {m : SMonad Unit α} {f : α → SMonad Unit β} [hm : KPresInst m] + [hf : ∀ a, KPresInst (f a)] : KPresInst (m >>= f) := + ⟨PresR.bind hm.pres fun a => (hf a).pres⟩ +instance (priority := 900) {P : Prop} {d : Decidable P} {m : SMonad Unit Unit} [h : KPresInst m] : + KPresInst (@NonDet.when (State Unit) P d m) := ⟨PresR.when h.pres⟩ +instance (priority := 900) {α : Type} {P : Prop} {d : Decidable P} {a b : SMonad Unit α} [ha : KPresInst a] + [hb : KPresInst b] : KPresInst (@ite _ P d a b) := ⟨PresR.ite' ha.pres hb.pres⟩ +instance (priority := 900) {α : Type} {C : α → State Unit → Prop} {B : α → SMonad Unit α} + [hB : ∀ a, KPresInst (B a)] (r : α) : KPresInst (NonDet.whileLoop C B r) := + ⟨PresR.whileLoop (fun a => (hB a).pres) r⟩ + +macro "kpres" : tactic => `(tactic| + repeat' (first + | exact KPresInst.pres + | refine PresR.bind_pure _ _ ?_ + | refine PresR.bind ?_ (fun _ => ?_) + | refine PresR.when ?_ + | refine PresR.ite' ?_ ?_ + | (refine PresR.modify _ (fun _ => ⟨rfl, fun _ _ => Iff.rfl⟩)) + | dsimp only + | split)) + +instance (f : ObjRef → List ObjRef → List ObjRef) (t : ObjRef) : KPresInst (tcbSchedAction (Z := Unit) f t) := + ⟨by unfold tcbSchedAction setTcbQueue; kpres⟩ +instance (a : SchedulerAction) : KPresInst (setSchedulerAction (Z := Unit) a) := + ⟨by unfold setSchedulerAction; kpres⟩ +instance : KPresInst (rescheduleRequired (Z := Unit)) := ⟨by unfold rescheduleRequired; kpres⟩ +instance (t : ObjRef) : KPresInst (tcbReleaseRemove (Z := Unit) t) := ⟨by unfold tcbReleaseRemove; kpres⟩ +instance (t : ObjRef) : KPresInst (possibleSwitchTo (Z := Unit) t) := ⟨by unfold possibleSwitchTo; kpres⟩ +instance : KPresInst (tcbReleaseDequeue (Z := Unit)) := ⟨by unfold tcbReleaseDequeue; kpres⟩ +instance : KPresInst (awaken (Z := Unit)) := ⟨by unfold awaken; kpres⟩ +instance : KPresInst (checkDomainTime (Z := Unit)) := ⟨by unfold checkDomainTime; kpres⟩ +instance (limit : Nat) : KPresInst (fl4NextDomain (Z := Unit) limit) := ⟨by unfold fl4NextDomain; kpres⟩ +instance (t : ObjRef) : KPresInst (fl4SwitchToThread (Z := Unit) t) := ⟨by unfold fl4SwitchToThread; kpres⟩ +instance : KPresInst (fl4ChooseThread (Z := Unit)) := ⟨by unfold fl4ChooseThread; kpres⟩ +instance (limit : Nat) : KPresInst (fl4ChooseNewThread (Z := Unit) limit) := + ⟨by unfold fl4ChooseNewThread; kpres⟩ +instance (limit : Nat) (c cur idle : ObjRef) (d : Domain) (w : Bool) : + KPresInst (fl4SelectCandidate (Z := Unit) limit c cur idle d w) := ⟨by unfold fl4SelectCandidate; kpres⟩ +instance (limit : Nat) : KPresInst (fl4SelectAction (Z := Unit) limit) := ⟨by unfold fl4SelectAction; kpres⟩ +instance (limit : Nat) : KPresInst (fl4SelectThread (Z := Unit) limit) := ⟨by unfold fl4SelectThread; kpres⟩ + +/-- **The tail of `schedule` keeps the idle thread and every TCB object.** -/ +theorem schedTail_cells (limit : Nat) (timer : State Unit → Fl4TimerView) : + PresR TcbCellsEq (do awaken (Z := Unit) + checkDomainTime + fl4SelectThread limit + fl4ScAndTimer timer) := by + kpres + +/-- **`IdleSt` across `fl4Schedule`**, from `IdleSt` at every post-fire state. -/ +theorem fl4Schedule_idleSt (limit : Nat) (timer : State Unit → Fl4TimerView) {σ0 τ1 : State Unit} + (hf : ∀ σ1, fl4TimerFires σ0 σ1 → IdleSt σ1) (h : ((), τ1) ∈ (fl4Schedule (Z := Unit) limit timer σ0).1) : + IdleSt τ1 := by + unfold fl4Schedule at h + obtain ⟨⟨u, σ1⟩, h1, h2⟩ := mem_bindA.1 h + exact (hf σ1 (mem_fl4Awaken.1 h1)).ofCells (schedTail_cells limit timer σ1 _ h2) + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3SchedOpsTA.lean b/proofs/Fl4ProofsA64/P3/Stage3SchedOpsTA.lean new file mode 100644 index 000000000..fd169afe5 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SchedOpsTA.lean @@ -0,0 +1,146 @@ +/- +P3 (AArch64, shared standalone module, delete-hooks line, contract §40): **the IPC scheduler interfaces at the per-step +record `JStage3TA`**, over `IpcSchedRelA` (the MCS line's scheduler relation with existential ready / release queues). + +`SchedOpsEA` / `SchedOpsA` / `SchedFootEA` / `SchedFootNA` need no new scheduler lemma: the MCS producers +(`ipcSchedOpsA`, `schedFootEA_of_sched`, `schedFootNA_of_sched`, with the footprint `MWrA`) take exactly what the record +carries (`sr`, `iplaced`, `ipcPlace`), so this module only reads them off the record: + +* **`JStage3TA.ipcSchedA`**: `IpcSchedRelA` at the record's `SchedSRA`. +* **`JStage3TA.schedOpsA`**: the five IPC interfaces (`SchedHypsA`, `SchedOpsA`, `SchedOpsEA`, `TimeoutScOpA`, + `CompleteTailFrameA`) at the record. +* **`JStage3TA.schedOpsEA`** / **`.schedFootEA`** / **`.schedFootNA`**: the endpoint interface, its write footprint + (`MWrA`) and the notification footprint. + +The platform hypotheses are those of `ipcSchedOpsA` (the shared [P] set, as `TcbSuspendA` / `TcbRestartA` take them): +`hc` (`m.curCore < numNodes`), `hmb` (the minimum budget), `hmax` / `htu` (`ticks_to_us`); `schedFootNA` also takes +`hN` is now `J.ipcTh.1` (the entry line's `ipcTh` record field). No named premise of its own. New module. +-/ +import Fl4ProofsA64.P3.Stage3BA +import Fl4ProofsA64.P3.Stage3TA +import Fl4ProofsA64.P3.IpcScFootA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4SpecA64 Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Endp + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- **`IpcSchedRelA` at the record**: its queues are the record's own. -/ +theorem JStage3BA.ipcSchedA + (J : JStage3BA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) : + IpcSchedRelA st Z m κ s σ := ⟨Z.Q, Z.R, J.sr⟩ + +/-- `JStage3BA.ipcSchedA` at `JStage3TA`. -/ +theorem JStage3TA.ipcSchedA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) : + IpcSchedRelA st Z m κ s σ := + J.toBA.ipcSchedA + +/-- **`hc` from a platform [P]**: the record's current core is one of `Z.Cs` (`sch.core`, `sch.cpuCs`), so it is +below `numNodes` once every core of `Z.Cs` is. `csN` alone gives only `≤ 128`, so `hCs` is a shared platform [P] +(`Z.Cs ⊆ {c | c < numNodes}`; the boot cores are `numNodes` many). -/ +theorem JStage3BA.curCore_lt + (J : JStage3BA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) : m.curCore < fl4A64Platform.numNodes := by + rw [J.j.1.sch.core]; exact hCs _ J.j.1.sch.cpuCs + +/-- `JStage3BA.curCore_lt` at `JStage3TA`. -/ +theorem JStage3TA.curCore_lt + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) : m.curCore < fl4A64Platform.numNodes := + J.toBA.curCore_lt hCs + +/-- **The five IPC scheduler interfaces at the record** (module docstring). -/ +theorem JStage3BA.schedOpsA {ar : machine.Arch A} + (J : JStage3BA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (hc : m.curCore < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) : + Notif.SchedHypsA st N Z.T (IpcSchedRelA st Z m κ) ∧ + Notif.SchedOpsA st ar N Z.T (IpcSchedRelA st Z m κ) (DonLocalA (st := st) Z.C) ∧ + SchedOpsEA st Z.T E Rp Bf (IpcSchedRelA st Z m κ) ∧ + TimeoutScOpA st ar Z.T E Rp Bf (IpcSchedRelA st Z m κ) + (fun t => ∀ c < fl4A64Platform.numNodes, specPtr t ≠ m.idleThreadOf c) ∧ + Notif.CompleteTailFrameA st ar N Z.T (IpcSchedRelA st Z m κ) (DonLocalA (st := st) Z.C) + (fun s σ => EpStateRel Z.T E Rp Bf (L.mem s) σ) := + ipcSchedOpsA J.iplaced J.ipcPlace hc hmb hmax htu + +/-- `JStage3BA.schedOpsA` at `JStage3TA`. -/ +theorem JStage3TA.schedOpsA {ar : machine.Arch A} + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (hc : m.curCore < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) : + Notif.SchedHypsA st N Z.T (IpcSchedRelA st Z m κ) ∧ + Notif.SchedOpsA st ar N Z.T (IpcSchedRelA st Z m κ) (DonLocalA (st := st) Z.C) ∧ + SchedOpsEA st Z.T E Rp Bf (IpcSchedRelA st Z m κ) ∧ + TimeoutScOpA st ar Z.T E Rp Bf (IpcSchedRelA st Z m κ) + (fun t => ∀ c < fl4A64Platform.numNodes, specPtr t ≠ m.idleThreadOf c) ∧ + Notif.CompleteTailFrameA st ar N Z.T (IpcSchedRelA st Z m κ) (DonLocalA (st := st) Z.C) + (fun s σ => EpStateRel Z.T E Rp Bf (L.mem s) σ) := + J.toBA.schedOpsA hc hmb hmax htu + +/-- **`SchedOpsEA` at the record.** -/ +theorem JStage3BA.schedOpsEA {ar : machine.Arch A} + (J : JStage3BA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (hc : m.curCore < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) : + SchedOpsEA st Z.T E Rp Bf (IpcSchedRelA st Z m κ) := + (J.schedOpsA hc hmb hmax htu).2.2.1 + +/-- `JStage3BA.schedOpsEA` at `JStage3TA`. -/ +theorem JStage3TA.schedOpsEA {ar : machine.Arch A} + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (hc : m.curCore < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) : + SchedOpsEA st Z.T E Rp Bf (IpcSchedRelA st Z m κ) := + J.toBA.schedOpsEA hc hmb hmax htu + +/-- **The endpoint scheduler write footprint** (`MWrA`): independent of the record. -/ +theorem JStage3BA.schedFootEA + (J : JStage3BA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) : + SchedFootEA st Z.T E Rp Bf (IpcSchedRelA st Z m κ) (fun σ s => MWrA (st := st) Z m σ s) := + schedFootEA_of_sched + +/-- `JStage3BA.schedFootEA` at `JStage3TA`. -/ +theorem JStage3TA.schedFootEA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) : + SchedFootEA st Z.T E Rp Bf (IpcSchedRelA st Z m κ) (fun σ s => MWrA (st := st) Z m σ s) := + J.toBA.schedFootEA + +/-- **The notification scheduler write footprint** (`MWrA`). -/ +theorem JStage3BA.schedFootNA {ar : machine.Arch A} + (J : JStage3BA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (hc : m.curCore < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) : + Notif.SchedFootNA st ar N Z.T (IpcSchedRelA st Z m κ) (DonLocalA (st := st) Z.C) + (fun σ s => MWrA (st := st) Z m σ s) := + schedFootNA_of_sched J.iplaced.n hc hmb J.ipcTh.1 + +/-- `JStage3BA.schedFootNA` at `JStage3TA`. -/ +theorem JStage3TA.schedFootNA {ar : machine.Arch A} + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (hc : m.curCore < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) : + Notif.SchedFootNA st ar N Z.T (IpcSchedRelA st Z m κ) (DonLocalA (st := st) Z.C) + (fun σ s => MWrA (st := st) Z m σ s) := + J.toBA.schedFootNA hc hmb + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3SchedPresA.lean b/proofs/Fl4ProofsA64/P3/Stage3SchedPresA.lean new file mode 100644 index 000000000..c903ef375 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SchedPresA.lean @@ -0,0 +1,267 @@ +/- +P3 (aarch64, A64-P3 Stage-3 integration; Architect 23:50Z `stage3-schedpres-contract.md` §5): **`JStage3A` across the +scheduler steps**. + +* **G1, narrowed write set.** `SWrA` (hence `MWrA` and `MFrA`; `P3/ScSWrA`) now covers the kernel-state *global* + words `[0, 224)` and the 128 *node blocks* `[223488, 772352)`, not the whole kernel-state block. It is strictly off + the IRQ-node slots `[43232, 215264)`, `OS`, VSpace and frames. `SchedWrA := MWrA` below is the contract's name for + that set; the ratified MCS-19 statements keep their text and now export the narrower footprint. +* **`IrqSlotsKeepA.of_swr`**: a step writing only in `SWrA` keeps the IRQ-node slot region. This discharges the + temporary [J] `IrqSlotsKeepA` (`hirqR` / `hirqU`) at every scheduler-word step. + +No named premise. +-/ +import Fl4ProofsA64.P3.ReplyXferFactsA +import Fl4ProofsA64.P3.ReplyPopFootA +import Fl4ProofsA64.P3.Stage3A +import Fl4ProofsA64.P3.IrqSlotsFootA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + + +/-! ## `JAtZAX` across a scheduler step -/ + +/-- The TCB fields the CSpace and VCPU parts of `JAtZAX` read: the seven cap slots and the two VCPU links. -/ +def TcbSlotKeepA (x x' : Fl4SpecA64.Tcb) : Prop := + Fl4SpecA64.tcbCnodeMap x' = Fl4SpecA64.tcbCnodeMap x ∧ x'.tcbArch.tcbVcpu = x.tcbArch.tcbVcpu ∧ + x'.tcbExt.tcbRunVcpu = x.tcbExt.tcbRunVcpu + +/-- **What a scheduler or IPC step may do to one non-TCB object** (Architect 01:27Z widening): replace an SC, or a +notification, an endpoint or a reply by one of the same kind. -/ +def FrameStepA (o o' : Fl4SpecA64.KernelObject) : Prop := + (∃ y n y' n', o = .schedContext y n ∧ o' = .schedContext y' n') ∨ + (∃ a b, o = .notification a ∧ o' = .notification b) ∨ (∃ a b, o = .endpoint a ∧ o' = .endpoint b) ∨ + ∃ a b, o = .reply a ∧ o' = .reply b + +/-- The kinds `FrameStepA` may change: SC, notification, endpoint, reply. -/ +def FrameKindA (o : Fl4SpecA64.KernelObject) : Prop := + (∃ y n, o = .schedContext y n) ∨ (∃ a, o = .notification a) ∨ (∃ a, o = .endpoint a) ∨ ∃ a, o = .reply a + +theorem FrameStepA.kind {o o' : Fl4SpecA64.KernelObject} (h : FrameStepA o o') : FrameKindA o := by + rcases h with ⟨y, n, _, _, rfl, _⟩ | ⟨a, _, rfl, _⟩ | ⟨a, _, rfl, _⟩ | ⟨a, _, rfl, _⟩ + · exact Or.inl ⟨y, n, rfl⟩ + · exact Or.inr (Or.inl ⟨a, rfl⟩) + · exact Or.inr (Or.inr (Or.inl ⟨a, rfl⟩)) + · exact Or.inr (Or.inr (Or.inr ⟨a, rfl⟩)) + +theorem FrameStepA.kind' {o o' : Fl4SpecA64.KernelObject} (h : FrameStepA o o') : FrameKindA o' := by + rcases h with ⟨_, _, y, n, _, rfl⟩ | ⟨_, a, _, rfl⟩ | ⟨_, a, _, rfl⟩ | ⟨_, a, _, rfl⟩ + · exact Or.inl ⟨y, n, rfl⟩ + · exact Or.inr (Or.inl ⟨a, rfl⟩) + · exact Or.inr (Or.inr (Or.inl ⟨a, rfl⟩)) + · exact Or.inr (Or.inr (Or.inr ⟨a, rfl⟩)) + +theorem FrameKindA.tcb_not {x : Fl4SpecA64.Tcb} : ¬ FrameKindA (.tcb x) := by + rintro (⟨_, _, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ | ⟨_, e⟩) <;> cases e + +theorem FrameStepA.trans {o1 o2 o3 : Fl4SpecA64.KernelObject} (h1 : FrameStepA o1 o2) (h2 : FrameStepA o2 o3) : + FrameStepA o1 o3 := by + rcases h1 with ⟨y, n, y', n', rfl, rfl⟩ | ⟨a, b, rfl, rfl⟩ | ⟨a, b, rfl, rfl⟩ | ⟨a, b, rfl, rfl⟩ <;> + rcases h2 with ⟨y2, n2, y3, n3, e2, rfl⟩ | ⟨a2, b2, e2, rfl⟩ | ⟨a2, b2, e2, rfl⟩ | ⟨a2, b2, e2, rfl⟩ <;> + cases e2 <;> + first + | exact Or.inl ⟨_, _, _, _, rfl, rfl⟩ + | exact Or.inr (Or.inl ⟨_, _, rfl, rfl⟩) + | exact Or.inr (Or.inr (Or.inl ⟨_, _, rfl, rfl⟩)) + | exact Or.inr (Or.inr (Or.inr ⟨_, _, rfl, rfl⟩)) + +/-- **A scheduler spec step**: objects are kept, or are TCBs with the same cap slots and VCPU links, or are SCs, +notifications, endpoints or replies replaced by one of their kind; the IRQ state, ASID table, CDT and original-cap map +are kept. -/ +structure SpecSchedA (σ σ' : Fl4SpecA64.State Unit) : Prop where + kh : ∀ p, σ'.kheap p = σ.kheap p ∨ + (∃ x x', σ.kheap p = some (.tcb x) ∧ σ'.kheap p = some (.tcb x') ∧ TcbSlotKeepA x x') ∨ + ∃ o o', σ.kheap p = some o ∧ σ'.kheap p = some o' ∧ FrameStepA o o' + irqS : σ'.interruptStates = σ.interruptStates + irqN : σ'.interruptIrqNode = σ.interruptIrqNode + asid : σ'.archState.armAsidTable = σ.archState.armAsidTable + /-- (record edit #7) The current VCPU is kept. -/ + vcur : σ'.archState.armCurrentVcpu = σ.archState.armCurrentVcpu + cdt : σ'.cdt = σ.cdt + orig : σ'.isOriginalCap = σ.isOriginalCap + +namespace SpecSchedA +variable {σ σ' : Fl4SpecA64.State Unit} (H : SpecSchedA σ σ') +include H + +theorem keep {p : Fl4SpecA64.ObjRef} {ko : Fl4SpecA64.KernelObject} (h : σ.kheap p = some ko) + (ht : ∀ x, ko ≠ .tcb x) (hs : ¬ FrameKindA ko) : σ'.kheap p = some ko := by + rcases H.kh p with e | ⟨x, x', e1, -⟩ | ⟨o, o', e1, -, f⟩ + · rw [e]; exact h + · rw [h] at e1; exact absurd (Option.some.inj e1) (ht x) + · rw [h] at e1; cases e1; exact absurd f.kind hs + +theorem back {p : Fl4SpecA64.ObjRef} {ko : Fl4SpecA64.KernelObject} (h : σ'.kheap p = some ko) + (ht : ∀ x, ko ≠ .tcb x) (hs : ¬ FrameKindA ko) : σ.kheap p = some ko := by + rcases H.kh p with e | ⟨x, x', -, e2, -⟩ | ⟨o, o', -, e2, f⟩ + · rw [← e]; exact h + · rw [h] at e2; exact absurd (Option.some.inj e2) (ht x') + · rw [h] at e2; cases e2; exact absurd f.kind' hs + +theorem tcb {p : Fl4SpecA64.ObjRef} {x : Fl4SpecA64.Tcb} (h : σ.kheap p = some (.tcb x)) : + ∃ x', σ'.kheap p = some (.tcb x') ∧ TcbSlotKeepA x x' := by + rcases H.kh p with e | ⟨y, y', e1, e2, hy⟩ | ⟨o, o', e1, -, f⟩ + · exact ⟨x, by rw [e]; exact h, rfl, rfl, rfl⟩ + · rw [h] at e1 + obtain rfl : x = y := by injection e1 with e1; injection e1 + exact ⟨y', e2, hy⟩ + · rw [h] at e1; cases e1; exact absurd f.kind FrameKindA.tcb_not + +theorem tcbBack {p : Fl4SpecA64.ObjRef} {x : Fl4SpecA64.Tcb} (h : σ'.kheap p = some (.tcb x)) : + ∃ y, σ.kheap p = some (.tcb y) := by + rcases H.kh p with e | ⟨y, _, e1, -⟩ | ⟨o, o', -, e2, f⟩ + · exact ⟨x, by rw [← e]; exact h⟩ + · exact ⟨y, e1⟩ + · rw [h] at e2; cases e2; exact absurd f.kind' FrameKindA.tcb_not + +/-- An object that is neither a TCB nor of a changeable kind is untouched. -/ +theorem other {p : Fl4SpecA64.ObjRef} (ht : ∀ x, σ.kheap p ≠ some (.tcb x)) + (hs : ∀ o, FrameKindA o → σ.kheap p ≠ some o) : σ'.kheap p = σ.kheap p := by + rcases H.kh p with e | ⟨x, x', e1, -⟩ | ⟨o, o', e1, -, f⟩ + · exact e + · exact absurd e1 (ht x) + · exact absurd e1 (hs o f.kind) + +/-- A scheduler or IPC step keeps the cap every spec slot holds (CNodes are untouched; TCBs keep their slots). -/ +theorem specCapAt (sl : Fl4SpecA64.CslotPtr) : specCapAtA σ' sl = specCapAtA σ sl := by + unfold specCapAtA + rcases H.kh sl.1 with e | ⟨x, x', e1, e2, k⟩ | ⟨o, o', e1, e2, f⟩ + · rw [e] + · rw [e1, e2] + show Fl4SpecA64.tcbCnodeMap x' sl.2 = Fl4SpecA64.tcbCnodeMap x sl.2 + rw [k.1] + · rw [e1, e2] + rcases f with ⟨_, _, _, _, rfl, rfl⟩ | ⟨_, _, rfl, rfl⟩ | ⟨_, _, rfl, rfl⟩ | ⟨_, _, rfl, rfl⟩ <;> rfl + +end SpecSchedA + +/-- **The memory a scheduler step keeps** among the words `JAtZAX` reads: the CSpace / TCB slots, the TCB VCPU +links, the VCPUs, the IRQ states and the VSpace and Stage-2 words. -/ +structure SchedKeepMemA (Z : XSetsA) (s s' : S) : Prop where + ksb : L.ksb s' = L.ksb s + cpu : L.cpu s' = L.cpu s + slot : ∀ y, SlotWA Z.th y → ∀ w, (w = y ∨ w = wa y 8#u64 ∨ w = wa y 16#u64 ∨ w = wa y 24#u64) → + L.mem s' w = L.mem s w + tcbW : ∀ t ∈ Z.T, L.mem s' (wa t 3664#u64) = L.mem s (wa t 3664#u64) ∧ L.mem s' (runVcpuA t) = L.mem s (runVcpuA t) + vcW : ∀ v ∈ Z.V, L.mem s' v = L.mem s v ∧ L.mem s' (wa v 24#u64) = L.mem s (wa v 24#u64) + irqW : ∀ i : U64, i.val < 5376 → L.mem s' (irqWordA (L.ksb s) i) = L.mem s (irqWordA (L.ksb s) i) + vs : ∀ w, VsFootA Z.Pp Z.S1 Z.Y (L.ksb s) w → L.mem s' w = L.mem s w + ext : ∀ w, ExtFootE Z.X Z.Y Z.T2 w → L.mem s' w = L.mem s w + +variable {A : Type} in +/-- **`JAtZAX` across a scheduler step**: the scheduler relations and the nodes' cursor typing come from the step's +post, the rest from `SchedKeepMemA` and `SpecSchedA`. The sibling of `JAtZAX.of_slot` (which needs agreement on all of +`FootNA`, including the words a scheduler step writes). -/ +theorem JAtZAX.of_sched {AR : A → HwRootA → Prop} {Z : XSetsA} {s s' : S} {a a' : A} + {σ σ' : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} (J : JAtZAX (L := L) AR Z s a σ m) + {Q : U64 → U64 → List U64} {R : U64 → List U64} {curOf curScOf : Fl4SpecA64.CoreId → Fl4SpecA64.ObjRef} + (H : SchedKeepMemA (st := st) Z s s') (HS : SpecSchedA σ σ') (har : AR a' Z.hw) + (hsch : SchedRelA (L := L) Z.T Z.C Z.Cs curOf curScOf s' σ' m) + (hrest : SchedRestA (L := L) { Z with Q := Q, R := R, curOf := curOf, curScOf := curScOf } s' σ' m) + (hidleT : ∀ c ∈ Z.Cs, L.mem s' (L.nodeAt s' c layout.node.IDLE_THREAD) ∈ Z.T) + (hidleC : ∀ c ∈ Z.Cs, L.mem s' (L.nodeAt s' c layout.node.IDLE_SC) ∈ Z.C) + (hcurT : ∀ c ∈ Z.Cs, L.mem s' (L.nodeAt s' c layout.node.CUR_THREAD) ∈ Z.T) + (hcscC : ∀ c ∈ Z.Cs, L.mem s' (L.nodeAt s' c layout.node.CUR_SC) ∈ Z.C) : + JAtZAX (L := L) AR { Z with Q := Q, R := R, curOf := curOf, curScOf := curScOf } s' a' σ' m := by + obtain ⟨J, V, X, Y⟩ := J + have hcap : ∀ y, SlotWA Z.th y → capAtA (L.mem s') y = capAtA (L.mem s) y := fun y hy => by + unfold capAtA + rw [H.slot y hy _ (Or.inl rfl), H.slot y hy _ (Or.inr (Or.inl rfl))] + have hst : StateRelA Z.th (L.mem s') σ' := by + constructor + · intro p r hp + obtain ⟨cs, h1, h2, h3⟩ := J.st.cnode p r hp + refine ⟨cs, HS.keep h1 (fun _ h => by cases h) (fun h => by rcases h with ⟨_, _, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ <;> cases e), h2, fun idx hl => ?_⟩ + obtain ⟨ac, e, hr⟩ := h3 idx hl + exact ⟨ac, e, by rw [hcap _ (Or.inr ⟨p, r, hp, idx, hl, rfl⟩)]; exact hr⟩ + · intro t ht + obtain ⟨x, h1, h2⟩ := J.st.tcb t ht + obtain ⟨x', e', hx⟩ := HS.tcb h1 + refine ⟨x', e', fun i hi => ?_⟩ + obtain ⟨ac, e, hr⟩ := h2 i hi + refine ⟨ac, by rw [hx.1]; exact e, ?_⟩ + rw [hcap _ (Or.inl ⟨t, Or.inr ⟨ht, i, hi, rfl⟩⟩)]; exact hr + have hcdt : CdtRelA st Z.th s' σ' Z.G := + CdtRelA.mkA (treeA_of_mem J.cdt.tree fun y hy => + ⟨H.slot y (Or.inl hy) _ (Or.inl rfl), H.slot y (Or.inl hy) _ (Or.inr (Or.inl rfl)), + H.slot y (Or.inl hy) _ (Or.inr (Or.inr (Or.inl rfl))), H.slot y (Or.inl hy) _ (Or.inr (Or.inr (Or.inr rfl)))⟩) + (by rw [HS.cdt]; exact J.cdt.cdt) (by rw [HS.cdt]; exact J.cdt.cdt_slot) + (fun sl hs => by + have e : revA (L.mem s') (cslotAddrA sl) = revA (L.mem s) (cslotAddrA sl) := by + unfold revA + rw [H.slot _ (absSlot_w hs) _ (Or.inr (Or.inr (Or.inr rfl)))] + rw [HS.orig, e]; exact J.cdt.orig sl hs) + J.cdt.par_slot (by unfold SpecPOA; rw [HS.cdt, HS.orig]; exact J.cdt.po) + have J' : JAtZA (L := L) AR { Z with Q := Q, R := R, curOf := curOf, curScOf := curScOf } s' a' σ' m := + { st := hst + cdt := hcdt + sch := hsch + ar := har + place := by rw [H.ksb]; exact J.place + irq := + { st := fun i hi => by rw [H.ksb, H.irqW i hi, HS.irqS]; exact J.irq.st i hi + node := fun i hi => by rw [H.ksb, HS.irqN]; exact J.irq.node i hi + nodeHi := fun i hi => by rw [H.ksb, HS.irqN]; exact J.irq.nodeHi i hi } + rest := hrest + thT := J.thT + csN := J.csN + idleT := hidleT + idleC := hidleC + curT := hcurT + cscC := hcscC + ksFit := by rw [H.ksb]; exact J.ksFit + scOffD := J.scOffD + cv := fun y hy => by rw [hcap y (Or.inl hy)]; exact J.cv y hy + pml := fun y hy => by rw [hcap y (Or.inl hy)]; exact J.pml y hy + vs := by + refine (show VsRelA Z.Pp Z.S1 Z.Lv Z.X Z.Y (L.mem s') (L.ksb s') σ from ?_).of_spec HS.asid fun p hp => + HS.other (fun x h => J.vs.not_tcb hp h) (fun o ho h => ?_) + · rw [H.ksb]; exact J.vs.of_mem fun b hb => H.vs b hb + · rcases hp with ⟨q, hq, rfl⟩ | ⟨q, hq, rfl⟩ | ⟨q, hq, rfl⟩ + · obtain ⟨f, hk, -⟩ := J.vs.pool q hq; rw [hk] at h; cases h; rcases ho with ⟨_, _, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ <;> cases e + · obtain ⟨t, hk, -⟩ := J.vs.pt q hq; rw [hk] at h; cases h; rcases ho with ⟨_, _, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ <;> cases e + · obtain ⟨l1, hk, -⟩ := J.vs.high q hq; rw [hk] at h; cases h; rcases ho with ⟨_, _, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ <;> cases e + ext := by + refine (J.ext.of_mem fun b hb => H.ext b hb).of_spec HS.asid fun q hq => + HS.other (fun x h => ?_) (fun o ho h => ?_) + · rcases hq with hq | hq + · obtain ⟨f, hk, -⟩ := J.vs.pool q hq; rw [hk] at h; cases h + · exact J.ext.not_tcb hq x h + rcases hq with hq | hq | hq + · obtain ⟨f, hk, -⟩ := J.vs.pool q hq; rw [hk] at h; cases h; rcases ho with ⟨_, _, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ <;> cases e + · obtain ⟨es, hk, -⟩ := J.ext.s2root q hq; rw [hk] at h; cases h; rcases ho with ⟨_, _, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ <;> cases e + · obtain ⟨pt, hk, -⟩ := J.ext.s2pt q hq; rw [hk] at h; cases h; rcases ho with ⟨_, _, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ <;> cases e } + have hsl : ∀ y, IsSlotA Z.th y → capAtA (L.mem s') y = capAtA (L.mem s) y := fun y hy => hcap y (Or.inl hy) + refine ⟨J', ?_, ?_, ?_⟩ + · refine ⟨fun v hv => ?_, fun p x e => V.back p x (HS.back e (fun _ h => by cases h) (fun h => by rcases h with ⟨_, _, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ <;> cases e)), + fun t ht => ?_, fun y hy p e => V.cap y hy p (by rw [← hsl y hy]; exact e)⟩ + · obtain ⟨x, e, h2⟩ := V.obj v hv + rw [(H.vcW v hv).1, (H.vcW v hv).2] + exact ⟨x, HS.keep e (fun _ h => by cases h) (fun h => by rcases h with ⟨_, _, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ <;> cases e), h2⟩ + · obtain ⟨x, e, h1, h2⟩ := V.tcb t ht + obtain ⟨x', e', hx⟩ := HS.tcb e + rw [(H.tcbW t ht).1, (H.tcbW t ht).2] + exact ⟨x', e', by rw [hx.2.1]; exact h1, by rw [hx.2.2]; exact h2⟩ + · refine ⟨fun r hr => ?_, fun p es e => X.back p es (HS.back e (fun _ h => by cases h) (fun h => by rcases h with ⟨_, _, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ <;> cases e)), + fun y hy p l mp asid va e => X.cap y hy p l mp asid va (by rw [← hsl y hy]; exact e)⟩ + obtain ⟨es, e⟩ := X.obj r hr + exact ⟨es, HS.keep e (fun _ h => by cases h) (fun h => by rcases h with ⟨_, _, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ <;> cases e)⟩ + · refine ⟨fun r hr => ?_, fun p l1 e => Y.back p l1 (HS.back e (fun _ h => by cases h) (fun h => by rcases h with ⟨_, _, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ <;> cases e)), + fun y hy p mp asid e => Y.cap y hy p mp asid (by rw [← hsl y hy]; exact e)⟩ + obtain ⟨l1, e⟩ := Y.obj r hr + exact ⟨l1, HS.keep e (fun _ h => by cases h) (fun h => by rcases h with ⟨_, _, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ <;> cases e)⟩ + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3SchedRunA.lean b/proofs/Fl4ProofsA64/P3/Stage3SchedRunA.lean new file mode 100644 index 000000000..1dec253e1 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SchedRunA.lean @@ -0,0 +1,57 @@ +/- +P3 (aarch64, A64-P3 Stage 3, T3 G4b slice 2): **`running` at the exit of `activate_thread`**. + +* **`YieldQueueRelA.runnable_cur`**: in a related state whose current TCB is `running` or `runningVM`, the current + thread's state word is `Runnable` (`StEqA` with the current thread on its core). +* **`YieldQueueRelA.runnable_activated`**: after `activate_thread` (`P3/Stage3SchedActA`'s `activate_thread_stepA` + exports the TCB state `activatedStateA ts`), the current thread's state word is `Runnable` unless the thread was the + idle thread (`ts ≠ idleThreadState`). + +The non-idle premise is the entry's: a syscall is made by a user thread, never by the idle thread. It is supplied at +the syscall entry (not by the schedule), so the producer of `JSyscallA.running` takes it as a hypothesis. + +No premise; new module. +-/ +import Fl4ProofsA64.P3.Stage3SchedActA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec fl4_core_a64 Fl4ProofsA64.P1 + +noncomputable section + +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **A running current TCB gives a `Runnable` state word.** -/ +theorem YieldQueueRelA.runnable_cur {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : YieldQueueRelA (st := st) Z m κ s σ) {y : Tcb} (hk : σ.kheap σ.curThread = some (.tcb y)) + (hr : y.tcbState = .running ∨ y.tcbState = .ext .runningVM) : + Runnable (L.mem s (stA (L.curW s))) := by + have ht := H.sched.curT + have hc := H.sched.sch.curThread + rw [hc] at hk + have e := H.states (L.curW s) ht y hk + rcases hr with hr | hr + · rw [hr] at e + left + exact UScalar.eq_of_val_eq (by rw [e]; simp [tsCodeA]) + · rw [hr] at e + right; right + exact UScalar.eq_of_val_eq (by rw [e]; simp [tsCodeA]) + +/-- **After activation the current thread is `Runnable`** unless it was the idle thread. -/ +theorem YieldQueueRelA.runnable_activated {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : YieldQueueRelA (st := st) Z m κ s σ) {y : Tcb} {ts : ThreadState} + (hk : σ.kheap σ.curThread = some (.tcb y)) (hy : y.tcbState = activatedStateA ts) + (legal : ActivateStateOKA ts) (hn : ts ≠ .idleThreadState) : + Runnable (L.mem s (stA (L.curW s))) := by + refine H.runnable_cur hk ?_ + rcases legal with rfl | rfl | rfl | rfl + · exact Or.inl hy + · exact Or.inl (by rw [hy]; rfl) + · exact absurd rfl hn + · exact Or.inr hy + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3SchedSchedA.lean b/proofs/Fl4ProofsA64/P3/Stage3SchedSchedA.lean new file mode 100644 index 000000000..2c0687b11 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SchedSchedA.lean @@ -0,0 +1,69 @@ +/- +P3 (aarch64, A64-P3 Stage-3 integration; queue item 1): **the schedule footprint**. `schedule_stepA` is a sibling of +`schedule_refinesA` (`P3/ScScheduleA`, statement unchanged). After the timer pass (whose frame `FF` the [J] `timer.fire` +premise supplies: ledger G2, producer "JOIN ntfn signal"), the rest of the local scheduler exports +`MFrDA Z m s1 s' σ1 τ` (release walk, select, switch-context / timer tail chained by `MFrA.trans`) and `SchedFrameA σ1 τ` +(`P3/SpecFrameSchedA`: `awaken_presA`, `fl4SelectAction_presA`, `fl4ScAndTimer_presA`). + +Edit to `P3/ScScheduleA`: `selectResultA`, `select_action_refinesA`, `scheduleTailA`, `schedule_tail_refinesA` and +`reachedResultA` lose `private`. No new premise. +-/ +import Fl4ProofsA64.P3.Stage3SchedAwakenA +import Fl4ProofsA64.P3.ScScheduleA +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3.Sched + +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4ProofsA64.P1.W7.R.ST + +noncomputable section + +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +/-- **`schedule_refinesA` with its footprint and spec frame after the timer pass** (module docstring). -/ +theorem schedule_stepA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {σ : State Unit} {a : A} {tm : A → Nat → DeadlineViewA} + {AR : A → HwRootA → Prop} (P : ArchSwFA st ar) (view : State Unit → Fl4TimerView) + (inputs : ScheduleInputsA (st := st) Z m κ s a σ ar tm view AR) + {FF : S → S → State Unit → State Unit → Prop} + (fireF : timer.fire st ar s a ⦃ r => ∃ σ1 Q R, + fl4TimerFires σ σ1 ∧ YieldQueueRelA (st := st) {Z with Q := Q, R := R} m κ r.1 σ1 ∧ IdleThreadsA m σ1 ∧ + FF s r.1 σ σ1 ⦄) : + sched.schedule st ar s a ⦃ r => ∃ s1 σ1 τ Q R Tcur Ccur, + FF s s1 σ σ1 ∧ fl4TimerFires σ σ1 ∧ + ((), τ) ∈ (fl4Schedule layout.NUM_DOMAIN_SCHEDULES.val view σ).1 ∧ + YieldQueueRelA (st := st) {Z with Q := Q, R := R, curOf := Tcur, curScOf := Ccur} m κ r.1 τ ∧ + MFrDA (st := st) Z m s1 r.1 σ1 τ ∧ SchedFrameA σ1 τ ⦄ := by + rw [schedule_phasesA] + refine WP.spec_bind (reachedResultA (awaken_stepA fireF)) ?_ + rintro ⟨sA, aA⟩ ⟨wake, sF, σ1, σ2, Q, R, FFh, fires, waking, wakeRel, Fa, Sa⟩ + simp only [uncurry_apply_pair] + have steps := inputs.selection (sA, aA) σ1 σ2 Q R wake fires waking wakeRel + refine WP.spec_bind (reachedResultA (select_action_refinesA wakeRel P steps.action steps.domain steps.roots)) ?_ + rintro ⟨s2, a2⟩ ⟨selected, σ3, Q', _, selection, _, H2, frame, _⟩ + simp only [uncurry_apply_pair] + have ready := inputs.tail (sA, aA) (s2, a2) σ1 σ2 σ3 Q R Q' wake selected fires waking selection frame H2 + refine WP.spec_mono (schedule_tail_refinesA H2 view inputs.oneDomain inputs.minAgree ready) ?_ + rintro r ⟨τ, C, tail, H3, Ftail⟩ + refine ⟨sF, σ1, τ, Q', R, _, C, FFh, fires, ?_, H3, ?_, ?_⟩ + · unfold fl4Schedule + apply mem_bindA.2 + refine ⟨((), σ1), mem_fl4Awaken.2 fires, ?_⟩ + rw [nd_bind_oneSc waking, nd_bind_oneSc (checkDomainTime_single_specA σ2 inputs.oneDomain)] + unfold fl4SelectThread + rw [nd_bind_assocSc, nd_bind_oneSc selection] + exact tail + · exact (Fa.transD frame rfl rfl).transA Ftail rfl rfl + · have Sb : SchedFrameA σ2 σ3 := by + have hm : ((), σ3) ∈ (fl4SelectAction (Z := Unit) layout.NUM_DOMAIN_SCHEDULES.val σ2).1 := by + rw [selection]; exact Set.mem_singleton _ + exact fl4SelectAction_presA _ σ2 _ hm + have Sc : SchedFrameA σ3 τ := + (inferInstance : SPresInst (setSchedulerAction (Z := Unit) .resumeCurThread >>= fun _ => fl4ScAndTimer view)).pres + σ3 _ tail + exact PreO.trans Sa (PreO.trans Sb Sc) + + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3SchedSelectA.lean b/proofs/Fl4ProofsA64/P3/Stage3SchedSelectA.lean new file mode 100644 index 000000000..2353b1e6d --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SchedSelectA.lean @@ -0,0 +1,200 @@ +/- +P3 (aarch64, A64-P3 Stage 3, T3 G4b / F8): **F8 across the schedule exit, template step 2: thread selection** +(`fl4SelectThread`). + +On `FIA` (`P3/Stage3SchedValidStepA`: F8 stated on the current thread's own SC, with F8(d)): + +* `maxNonEmptyQueue_mem`: with a non-empty ready queue, `maxNonEmptyQueue` is one of them (`setMax` on the finite + priority set); +* `switchTo_fia`, `chooseThread_fia`, `chooseNewThread_fia`: choosing a thread keeps `FIA` and leaves a current + thread with an active SC outside the release queue (`CurOKA`): the new thread is a ready-queue member (F8(a)) or + the idle thread; +* `selectCandidate_fia`, `selectAction_fia`: the pending switch target is `schedulable` (`SwA`, `ActIdleA.1`), the + outgoing thread is enqueued under `schedulable`; +* `selectThread_fia`: after the final `setSchedulerAction .resumeCurThread` the current thread's `act` clause holds + (`CurOKA`). + +Named premises: F8(d) inside `FIA`; `SwA` (the record's `ActIdleA.1` at the start). +-/ +import Fl4ProofsA64.P3.Stage3SchedValidStepA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P3 Classical + +noncomputable section + +/-- The current thread, if not idle, has an active SC and is outside the release queue. -/ +def CurOKA (σ : State Unit) : Prop := σ.curThread ≠ σ.idleThread → TgtA σ.curThread σ + +/-- A pending switch target is `schedulable` (`ActIdleA.1`). -/ +def SwA (σ : State Unit) : Prop := ∀ t, σ.schedulerAction = .switchThread t → schedulable t σ = true + +theorem maxNonEmptyQueue_mem (q : Priority → ReadyQueue) (h : ¬ ∀ p, q p = []) : + ∃ p, q p ≠ [] ∧ maxNonEmptyQueue q = q p := by + have hex : ∃! m : Priority, m ∈ {p | q p ≠ []} ∧ ∀ x ∈ {p | q p ≠ []}, x ≤ m := by + obtain ⟨p0, hp0⟩ := not_forall.1 h + let T : Finset Nat := (Finset.range 256).filter (fun n => ∃ p : Priority, p.toNat = n ∧ q p ≠ []) + have memT : ∀ x : Priority, q x ≠ [] → x.toNat ∈ T := fun x hx => + Finset.mem_filter.2 ⟨Finset.mem_range.2 x.isLt, x, rfl, hx⟩ + have hT : T.Nonempty := ⟨p0.toNat, memT p0 hp0⟩ + obtain ⟨pm, hpm, hqm⟩ := (Finset.mem_filter.1 (Finset.max'_mem T hT)).2 + refine ⟨pm, ⟨hqm, fun x hx => ?_⟩, fun y ⟨hy, hy2⟩ => ?_⟩ + · rw [BitVec.le_def, hpm]; exact Finset.le_max' T x.toNat (memT x hx) + · have h1 := hy2 pm hqm + rw [BitVec.le_def] at h1 + have h2 : y.toNat ≤ T.max' hT := Finset.le_max' T y.toNat (memT y hy) + rw [← hpm] at h2 + exact BitVec.eq_of_toNat_eq (le_antisymm h2 h1) + refine ⟨Classical.choose hex, (Classical.choose_spec hex).1.1, ?_⟩ + unfold maxNonEmptyQueue setMax The + rw [dif_pos hex] + +namespace HT +variable {P : State Unit → Prop} + +theorem get {Q : State Unit → State Unit → Prop} (h : ∀ σ, P σ → Q σ σ) : + HT P (NonDet.get : SMonad Unit (State Unit)) Q := fun σ hσ r hr => by + have e : r = (σ, σ) := hr + rw [e]; exact h σ hσ + +theorem selectF {α : Type} (S : Set α × Prop) {Q : α → State Unit → Prop} (h : ∀ σ, P σ → ∀ a, Q a σ) : + HT P (NonDet.selectF S : SMonad Unit α) Q := fun σ hσ r hr => by + have e : r.2 = σ := hr.2 + rw [e]; exact h σ hσ r.1 + +end HT + +theorem doExtendedOp_fia (eop : DetExtMonad Unit) : HT FIA (doExtendedOp (Z := Unit) eop) (fun _ σ => FIA σ) := by + unfold Fl4SpecA64.doExtendedOp + refine HT.bind (Q := fun _ σ => FIA σ) (HT.get fun σ hσ => hσ) fun ex => ?_ + refine HT.bind (Q := fun _ σ => FIA σ) (HT.selectF _ fun σ hσ a => hσ) fun r => ?_ + exact HT.modify _ fun σ hσ => hσ.ofSame rfl rfl rfl rfl rfl rfl rfl + +theorem nextDomain_fia (limit : Nat) : HT FIA (fl4NextDomain (Z := Unit) limit) (fun _ σ => FIA σ) := by + unfold Fl4SpecA64.fl4NextDomain + refine HT.bind (Q := fun _ σ => FIA σ) (HT.modify _ fun σ hσ => hσ.ofSame rfl rfl rfl rfl rfl rfl rfl) fun _ => ?_ + exact doExtendedOp_fia _ + +theorem switchTo_fia (t : ObjRef) : + HT (fun σ => FIA σ ∧ TgtA t σ) (fl4SwitchToThread (Z := Unit) t) (fun _ σ => FIA σ ∧ CurOKA σ) := by + unfold Fl4SpecA64.fl4SwitchToThread + refine HT.bind (Q := fun _ σ => FIA σ ∧ TgtA t σ) + (HT.mono (HT.deq t (TgtA t) (fun σ f h => h)) (fun σ hσ => hσ) (fun _ σ h => h)) fun _ => ?_ + exact HT.modify _ fun σ hσ => ⟨⟨⟨hσ.1.1.rq, fun _ _ => hσ.2, hσ.1.1.blk⟩, hσ.1.2⟩, fun _ => hσ.2⟩ + +theorem chooseThread_fia : HT FIA (fl4ChooseThread (Z := Unit)) (fun _ σ => FIA σ ∧ CurOKA σ) := by + unfold Fl4SpecA64.fl4ChooseThread + refine HT.bind (Q := fun s σ => FIA σ ∧ s = σ) (HT.get fun σ hσ => ⟨hσ, rfl⟩) fun s => ?_ + refine HT.ite (fun hq => ?_) (fun hq => ?_) + · exact HT.modify _ fun σ hσ => by + obtain ⟨hI, hs⟩ := hσ + subst hs + exact ⟨⟨⟨hI.1.rq, fun _ h => absurd rfl h, hI.1.blk⟩, hI.2⟩, fun h => absurd rfl h⟩ + · refine HT.mono (P := fun σ => FIA σ ∧ TgtA (the (maxNonEmptyQueue (s.readyQueues s.curDomain)).head?) σ ∧ s = σ) + (HT.mono (switchTo_fia _) (fun σ hσ => ⟨hσ.1, hσ.2.1⟩) (fun _ σ h => h)) ?_ (fun _ _ h => h) + intro σ hσ + obtain ⟨hI, hs⟩ := hσ + subst hs + obtain ⟨p, hp, hm⟩ := maxNonEmptyQueue_mem (s.readyQueues s.curDomain) hq + have hmem : the (maxNonEmptyQueue (s.readyQueues s.curDomain)).head? ∈ s.readyQueues s.curDomain p := by + rw [hm] + cases hl : s.readyQueues s.curDomain p with + | nil => exact absurd hl hp + | cons a l => exact List.mem_cons_self + exact ⟨hI, hI.1.rq _ _ _ hmem, rfl⟩ + +theorem chooseNewThread_fia (limit : Nat) : + HT FIA (fl4ChooseNewThread (Z := Unit) limit) (fun _ σ => FIA σ ∧ CurOKA σ) := by + unfold Fl4SpecA64.fl4ChooseNewThread + refine HT.bind (Q := fun _ σ => FIA σ) (HT.gets _ fun σ hσ => hσ) fun domTime => ?_ + refine HT.bind (Q := fun _ σ => FIA σ) (HT.when (fun _ => nextDomain_fia limit) (fun _ σ hσ => hσ)) fun _ => ?_ + exact chooseThread_fia + +theorem selectCandidate_fia (limit : Nat) (cand cur idle : ObjRef) (dom : Domain) (wr : Bool) : + HT (fun σ => FIA σ ∧ TgtA cand σ) (fl4SelectCandidate (Z := Unit) limit cand cur idle dom wr) + (fun _ σ => FIA σ ∧ CurOKA σ) := by + unfold Fl4SpecA64.fl4SelectCandidate + refine HT.bind (Q := fun _ σ => FIA σ ∧ TgtA cand σ) (HT.threadGet _ cand fun σ hσ x hx => hσ) fun prio => ?_ + have chain : HT (fun σ => FIA σ ∧ TgtA cand σ) (do + tcbSchedAction (Z := Unit) tcbSchedEnqueue cand + setSchedulerAction .chooseNewThread + fl4ChooseNewThread limit) (fun _ σ => FIA σ ∧ CurOKA σ) := by + refine HT.bind (Q := fun _ σ => FIA σ ∧ TgtA cand σ) + (HT.mono (HT.enq cand (TgtA cand) (fun σ f h => h)) (fun σ hσ => ⟨hσ.1, hσ.2, hσ.2.1, hσ.2.2⟩) + (fun _ _ h => h)) fun _ => ?_ + refine HT.bind (Q := fun _ σ => FIA σ ∧ TgtA cand σ) + (HT.setAct .chooseNewThread (by intro e; cases e) (TgtA cand) (fun σ a h => h)) fun _ => ?_ + exact HT.mono (chooseNewThread_fia limit) (fun σ hσ => hσ.1) (fun _ _ h => h) + have chain2 : HT (fun σ => FIA σ ∧ TgtA cand σ) (do + tcbSchedAction (Z := Unit) tcbSchedAppend cand + setSchedulerAction .chooseNewThread + fl4ChooseNewThread limit) (fun _ σ => FIA σ ∧ CurOKA σ) := by + refine HT.bind (Q := fun _ σ => FIA σ ∧ TgtA cand σ) + (HT.mono (HT.app cand (TgtA cand) (fun σ f h => h)) (fun σ hσ => ⟨hσ.1, hσ.2, hσ.2.1, hσ.2.2⟩) + (fun _ _ h => h)) fun _ => ?_ + refine HT.bind (Q := fun _ σ => FIA σ ∧ TgtA cand σ) + (HT.setAct .chooseNewThread (by intro e; cases e) (TgtA cand) (fun σ a h => h)) fun _ => ?_ + exact HT.mono (chooseNewThread_fia limit) (fun σ hσ => hσ.1) (fun _ _ h => h) + have tail : ∀ cp : Priority, HT (fun σ => FIA σ ∧ TgtA cand σ) (show SMonad Unit Unit from do + let highest ← NonDet.gets (isHighestPrio dom prio) + if (cur = idle ∨ prio < cp) ∧ ¬highest then + (do tcbSchedAction (Z := Unit) tcbSchedEnqueue cand + setSchedulerAction .chooseNewThread + fl4ChooseNewThread limit) + else if wr = true ∧ prio = cp then + (do tcbSchedAction (Z := Unit) tcbSchedAppend cand + setSchedulerAction .chooseNewThread + fl4ChooseNewThread limit) + else fl4SwitchToThread cand) (fun _ σ => FIA σ ∧ CurOKA σ) := fun cp => by + refine HT.bind (Q := fun _ σ => FIA σ ∧ TgtA cand σ) (HT.gets _ fun σ hσ => hσ) fun highest => ?_ + refine HT.ite (fun _ => chain) (fun _ => ?_) + exact HT.ite (fun _ => chain2) (fun _ => switchTo_fia cand) + dsimp only + exact HT.ite (fun _ => HT.bind (Q := fun _ σ => FIA σ ∧ TgtA cand σ) (HT.pure _ fun σ hσ => hσ) tail) + (fun _ => HT.bind (Q := fun _ σ => FIA σ ∧ TgtA cand σ) (HT.threadGet _ cur fun σ hσ x hx => hσ) tail) + +/-- The scalars `fl4SelectAction` reads from its start state `s`. -/ +def SameS (s σ : State Unit) : Prop := + σ.schedulerAction = s.schedulerAction ∧ σ.curThread = s.curThread ∧ σ.idleThread = s.idleThread ∧ + σ.curDomain = s.curDomain + +theorem selectAction_fia (limit : Nat) : + HT (fun σ => FIA σ ∧ SwA σ) (fl4SelectAction (Z := Unit) limit) (fun _ σ => FIA σ ∧ CurOKA σ) := by + unfold Fl4SpecA64.fl4SelectAction + refine HT.bind (Q := fun s σ => (FIA σ ∧ SwA σ) ∧ s = σ) (HT.get fun σ hσ => ⟨hσ, rfl⟩) fun s => ?_ + refine HT.when (fun hne => ?_) (fun hn σ hσ => ?_) + · dsimp only + refine HT.bind (Q := fun _ σ => FIA σ ∧ (SwA σ ∧ SameS s σ)) + (HT.when (fun hw => ?_) (fun hn σ hσ => ?_)) fun _ => ?_ + · refine HT.mono (HT.enq s.curThread (fun σ => SwA σ ∧ SameS s σ) (fun σ f h => h)) (fun σ hσ => ?_) + (fun _ _ h => h) + obtain ⟨⟨hI, hS⟩, hs⟩ := hσ + subst hs + obtain ⟨a, n, -⟩ := schedulable_actA hw + exact ⟨hI, ⟨hS, rfl, rfl, rfl, rfl⟩, a, n⟩ + · obtain ⟨⟨hI, hS⟩, hs⟩ := hσ + subst hs + exact ⟨hI, hS, rfl, rfl, rfl, rfl⟩ + · cases hA : s.schedulerAction with + | resumeCurThread => exact absurd hA hne + | chooseNewThread => exact HT.mono (chooseNewThread_fia limit) (fun σ hσ => hσ.1) (fun _ _ h => h) + | switchThread c => + refine HT.mono (selectCandidate_fia limit c s.curThread s.idleThread s.curDomain (schedulable s.curThread s)) + (fun σ hσ => ?_) (fun _ _ h => h) + have hsch := hσ.2.1 c (by rw [hσ.2.2.1, hA]) + obtain ⟨a, n, -⟩ := schedulable_actA hsch + exact ⟨hσ.1, a, n⟩ + · obtain ⟨⟨hI, hS⟩, hs⟩ := hσ + subst hs + exact ⟨hI, fun hc => hI.1.act (not_not.1 hn) hc⟩ + +theorem selectThread_fia (limit : Nat) : + HT (fun σ => FIA σ ∧ SwA σ) (fl4SelectThread (Z := Unit) limit) (fun _ σ => FIA σ) := by + unfold Fl4SpecA64.fl4SelectThread Fl4SpecA64.setSchedulerAction + refine HT.bind (Q := fun _ σ => FIA σ ∧ CurOKA σ) (selectAction_fia limit) fun _ => ?_ + exact HT.modify _ fun σ hσ => ⟨⟨hσ.1.1.rq, fun _ hc => hσ.2 hc, hσ.1.1.blk⟩, hσ.1.2⟩ + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3SchedTailQA.lean b/proofs/Fl4ProofsA64/P3/Stage3SchedTailQA.lean new file mode 100644 index 000000000..512049471 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SchedTailQA.lean @@ -0,0 +1,72 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2b, merge of record edit #8c): **the schedule tail keeps the IPC queues** +(`QKeepA`, `P3/Stage3QKeepPresA`): `awaken`, `check_domain_time`, the thread selection and the SC switch with the +timer, for the record across `schedule` (`P3/Stage3KEntrySchedA`). + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.ScDomKeepA +import Fl4ProofsA64.P3.Stage3SchedBindA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec Classical Fl4ProofsA64.P3 + +noncomputable section + +/-! ## The schedule tail advances the domain: the queue relation without the `dom` field -/ + +instance : PreO QKeepN := ⟨QKeepN.refl, QKeepN.trans⟩ + +class QInstN {α : Type} (m : SMonad Unit α) : Prop where + pres : PresR QKeepN m + +instance (priority := low) {α : Type} {m : SMonad Unit α} [h : QInst m] : QInstN m := + ⟨fun s x hx => (h.pres s x hx).toN⟩ + +instance {α β : Type} {m : SMonad Unit α} {f : α → SMonad Unit β} [hm : QInstN m] + [hf : ∀ a, QInstN (f a)] : QInstN (m >>= f) := + ⟨PresR.bind hm.pres fun a => (hf a).pres⟩ + +instance {P : Prop} {d : Decidable P} {m : SMonad Unit Unit} [h : QInstN m] : + QInstN (@NonDet.when (State Unit) P d m) := ⟨PresR.when h.pres⟩ + +instance {α : Type} {P : Prop} {d : Decidable P} {a b : SMonad Unit α} [ha : QInstN a] [hb : QInstN b] : + QInstN (@ite _ P d a b) := ⟨PresR.ite' ha.pres hb.pres⟩ + +macro "qpresN" : tactic => `(tactic| + repeat' (first + | exact QInstN.pres + | refine PresR.bind_pure _ _ ?_ + | refine PresR.bind ?_ (fun _ => ?_) + | exact PresR.pure _ + | refine PresR.when ?_ + | refine PresR.unless' ?_ + | refine PresR.ite' ?_ ?_ + | dsimp only + | split)) + +instance (limit : Nat) : QInstN (fl4NextDomain (Z := Unit) limit) := ⟨by + unfold fl4NextDomain + exact PresR.bind (PresR.modify _ fun σ => QKeepN.of_kheap rfl) fun _ => QInstN.pres⟩ + +instance (limit : Nat) : QInstN (fl4ChooseNewThread (Z := Unit) limit) := ⟨by unfold fl4ChooseNewThread; qpresN⟩ +instance (limit : Nat) (c cu i : ObjRef) (d : Domain) (w : Bool) : + QInstN (fl4SelectCandidate (Z := Unit) limit c cu i d w) := ⟨by unfold fl4SelectCandidate; qpresN⟩ +instance (limit : Nat) : QInstN (fl4SelectThread (Z := Unit) limit) := + ⟨by unfold fl4SelectThread fl4SelectAction; qpresN⟩ + +/-- **The schedule tail keeps the queues** (all but the domain schedule, which `next_domain` may advance). -/ +theorem schedTail_qnA (limit : Nat) (timer : State Unit → Fl4TimerView) {σ1 τ : State Unit} + (h : ((), τ) ∈ (SchedTail limit timer σ1).1) : QKeepN σ1 τ := by + have P : PresR QKeepN (SchedTail limit timer) := by + unfold SchedTail + exact PresR.bind (inferInstance : QInstN (awaken (Z := Unit))).pres fun _ => + PresR.bind (inferInstance : QInstN (checkDomainTime (Z := Unit))).pres fun _ => + PresR.bind (inferInstance : QInstN (fl4SelectThread (Z := Unit) limit)).pres fun _ => + (inferInstance : QInstN (fl4ScAndTimer (Z := Unit) timer)).pres + exact P σ1 _ h + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3SchedValidA.lean b/proofs/Fl4ProofsA64/P3/Stage3SchedValidA.lean new file mode 100644 index 000000000..13469d70e --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SchedValidA.lean @@ -0,0 +1,85 @@ +/- +P3 (aarch64, record edit #5, F8): **`SchedValidA`**, l4v's `valid_ready_qs` / `ct_not_in_release_q` and the active SC of +blocked threads, on the current core's view; its transport across spec steps that keep the scheduler's data. +-/ +import Fl4ProofsA64.P3.FrameA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4SpecA64 + +/-- Blocked on a notification, a send, a receive or a reply. -/ +def BlockedTsA : Fl4SpecA64.ThreadState → Bool + | .blockedOnReceive .. => true + | .blockedOnSend .. => true + | .blockedOnReply _ => true + | .blockedOnNotification _ => true + | _ => false + +/-- The SC at `p` exists and is active. -/ +def ScActiveAtA (σ : Fl4SpecA64.State Unit) (p : Fl4SpecA64.ObjRef) : Prop := + ∃ y k, σ.kheap p = some (.schedContext y k) ∧ Fl4SpecA64.scActive y = true + +/-- [J] (record edit #5, F8) The scheduler's validity facts. -/ +structure SchedValidA (σ : Fl4SpecA64.State Unit) : Prop where + /-- Ready-queue threads have an active SC and are not in the release queue (`valid_ready_qs`). -/ + rq : ∀ d p t, t ∈ σ.readyQueues d p → + (∃ x sc, σ.kheap t = some (.tcb x) ∧ x.tcbSchedContext = some sc ∧ ScActiveAtA σ sc) ∧ t ∉ σ.releaseQueue + /-- When resuming a non-idle current thread, the current SC is active and the thread is not in the release queue + (`ct_not_in_release_q`). -/ + act : σ.schedulerAction = .resumeCurThread → σ.curThread ≠ σ.idleThread → + (∃ x sc, σ.kheap σ.curThread = some (.tcb x) ∧ x.tcbSchedContext = some sc ∧ ScActiveAtA σ sc) ∧ + σ.curThread ∉ σ.releaseQueue + /-- A blocked thread's bound SC is active. -/ + blk : ∀ t x sc, σ.kheap t = some (.tcb x) → BlockedTsA x.tcbState = true → x.tcbSchedContext = some sc → + ScActiveAtA σ sc + /-- (record edit #6, F8(d)) Release-queue threads have an active SC (`valid_release_q`). -/ + rls : ∀ t ∈ σ.releaseQueue, ∃ x sc, σ.kheap t = some (.tcb x) ∧ x.tcbSchedContext = some sc ∧ ScActiveAtA σ sc + +/-- `SchedValidA` across a step keeping the scheduler globals, the SC objects, and every TCB's state and SC. -/ +theorem SchedValidA.congr {σ σ' : Fl4SpecA64.State Unit} (h : SchedValidA σ) + (hq : σ'.readyQueues = σ.readyQueues) (hr : σ'.releaseQueue = σ.releaseQueue) + (ha : σ'.schedulerAction = σ.schedulerAction) (hc : σ'.curThread = σ.curThread) + (hi : σ'.idleThread = σ.idleThread) (hs : σ'.curSc = σ.curSc) + (hsc : ∀ p y k, σ.kheap p = some (.schedContext y k) → σ'.kheap p = some (.schedContext y k)) + (hf : ∀ t x, σ.kheap t = some (.tcb x) → ∃ x', σ'.kheap t = some (.tcb x') ∧ + x'.tcbSchedContext = x.tcbSchedContext) + (ht : ∀ t x', σ'.kheap t = some (.tcb x') → ∃ x, σ.kheap t = some (.tcb x) ∧ x'.tcbState = x.tcbState ∧ + x'.tcbSchedContext = x.tcbSchedContext) : + SchedValidA σ' := by + have act : ∀ p, ScActiveAtA σ p → ScActiveAtA σ' p := fun p ⟨y, k, e, a⟩ => ⟨y, k, hsc p y k e, a⟩ + refine ⟨fun d p t hm => ?_, fun e1 e2 => ?_, fun t x' sc e1 e2 e3 => ?_, fun t ht => ?_⟩ + · rw [hq] at hm + obtain ⟨⟨x, sc, e, es, a⟩, n⟩ := h.rq d p t hm + obtain ⟨x', e', es'⟩ := hf t x e + exact ⟨⟨x', sc, e', es'.trans es, act _ a⟩, by rw [hr]; exact n⟩ + · rw [ha] at e1 + rw [hc, hi] at e2 + obtain ⟨⟨x, sc, e, es, a⟩, n⟩ := h.act e1 e2 + obtain ⟨x', e', es'⟩ := hf _ x e + rw [hc, hr] + exact ⟨⟨x', sc, e', es'.trans es, act _ a⟩, n⟩ + · obtain ⟨x, e, es, ec⟩ := ht t x' e1 + exact act _ (h.blk t x sc e (es ▸ e2) (ec ▸ e3)) + · rw [hr] at ht + obtain ⟨x, sc, e, es, a⟩ := h.rls t ht + obtain ⟨x', e', es'⟩ := hf t x e + exact ⟨x', sc, e', es'.trans es, act _ a⟩ + +theorem SchedValidA.ofSlot {σ σ' : Fl4SpecA64.State Unit} (h : SchedValidA σ) (HS : SpecSlotA σ σ') : + SchedValidA σ' := by + refine h.congr HS.rq HS.rls HS.act HS.cur HS.idle HS.csc (fun p y k e => ?_) (fun t x e => ?_) (fun t x' e => ?_) + · rcases HS.kh p with e1 | ⟨r, cs, cs', e1, -⟩ | ⟨x, x', e1, -⟩ + · rw [e1]; exact e + · rw [e] at e1; cases e1 + · rw [e] at e1; cases e1 + · rcases HS.kh t with e1 | ⟨r, cs, cs', e1, -⟩ | ⟨y, y', e1, e2, g⟩ + · exact ⟨x, by rw [e1]; exact e, rfl⟩ + · rw [e] at e1; cases e1 + · rw [e] at e1; cases e1; exact ⟨y', e2, (tcbNcA_eq g).2.1⟩ + · rcases HS.kh t with e1 | ⟨r, cs, cs', -, e2⟩ | ⟨y, y', e1, e2, g⟩ + · exact ⟨x', by rw [← e1]; exact e, rfl, rfl⟩ + · rw [e] at e2; cases e2 + · rw [e] at e2; cases e2; exact ⟨y, e1, (tcbNcA_eq g).1, (tcbNcA_eq g).2.1⟩ + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3SchedValidStepA.lean b/proofs/Fl4ProofsA64/P3/Stage3SchedValidStepA.lean new file mode 100644 index 000000000..125c31516 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SchedValidStepA.lean @@ -0,0 +1,292 @@ +/- +P3 (aarch64, A64-P3 Stage 3, T3 G4b / F8): **F8 across the schedule exit, template, step 1: the leaves, `awaken`, +`checkDomainTime`**. + +`FIA σ` is `SchedValidA σ` split as `SchedValidB σ` and F8(d) (every release-queue thread has +an active SC; `SchedValidA.rls` since record edit #6). Hoare triples (`P3/Stage3SchedHoareA`) show that `FIA` is kept by the queue primitives and by the first +steps of `fl4Schedule`: + +* `HT.tcbSchedAction_ready`: a ready-queue write adds only a thread with an active SC outside the release queue + (`tcbSchedEnqueue`: the enqueued thread, from a pre-condition; `tcbSchedDequeue`: nothing); +* `tcbReleaseRemove_fia`; `rescheduleRequired_fia` (its enqueue is under `schedulable`); `possibleSwitchTo_fia` (its + guard leaves the release queue and the SC is `schedulable`-free: the caller supplies `ActTcbA`); +* `tcbReleaseDequeue_fia`, `awaken_fia` (the loop), `checkDomainTime_fia`. + +Named premises: the active SC of a `possibleSwitchTo` target is a hypothesis of the +caller (F8(c) captured before the state write for the IPC lines; F8(d) for the release queue). +-/ +import Fl4ProofsA64.P3.Stage3SchedHoareA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P3 Classical + +noncomputable section + +/-- **F8 stated on the current thread's own SC** (an invariant across the schedule's intermediate states, where +`σ.curSc` lags the current thread): `SchedValidA` with `act` about `cur`'s bound SC. At the end of the schedule +`curSc` is that SC. `SchedValidB.toA` gives the record's form. -/ +structure SchedValidB (σ : State Unit) : Prop where + rq : ∀ d p t, t ∈ σ.readyQueues d p → ActTcbA σ t ∧ t ∉ σ.releaseQueue + act : σ.schedulerAction = .resumeCurThread → σ.curThread ≠ σ.idleThread → + ActTcbA σ σ.curThread ∧ σ.curThread ∉ σ.releaseQueue + blk : ∀ t x sc, σ.kheap t = some (.tcb x) → BlockedTsA x.tcbState = true → x.tcbSchedContext = some sc → + ScActiveAtA σ sc + +/-- **F8 with F8(d)**. -/ +def FIA (σ : State Unit) : Prop := SchedValidB σ ∧ ∀ t ∈ σ.releaseQueue, ActTcbA σ t + +/-- `SchedValidB` with the release-queue clause (F8(d), `FIA`'s second part) is the record's F8 (`SchedValidA`, +record edit #6: `act` is on the current thread's own SC). -/ +theorem SchedValidB.toA {σ : State Unit} (h : SchedValidB σ) (hr : ∀ t ∈ σ.releaseQueue, ActTcbA σ t) : + SchedValidA σ := + ⟨h.rq, h.act, h.blk, hr⟩ + +/-- **The record's F8 gives `FIA`** (the converse of `SchedValidB.toA`): `exit_spec`'s start premise from +`JStage3A.schedValid`. -/ +theorem SchedValidA.toFIA {σ : State Unit} (h : SchedValidA σ) : FIA σ := + ⟨⟨h.rq, h.act, h.blk⟩, h.rls⟩ + +theorem ActTcbA.ofKh {σ σ' : State Unit} {t : ObjRef} (hk : σ'.kheap = σ.kheap) (h : ActTcbA σ t) : ActTcbA σ' t := + let ⟨x, sc, e1, e2, y, k, e3, e4⟩ := h + ⟨x, sc, by rw [hk]; exact e1, e2, y, k, by rw [hk]; exact e3, e4⟩ + +/-- `SchedValidA` across a state update that keeps the heap, the ready queues, the action, the current thread / SC and +shrinks the release queue. -/ +theorem SchedValidB.ofRelSub {σ σ' : State Unit} (h : SchedValidB σ) (hk : σ'.kheap = σ.kheap) + (hq : σ'.readyQueues = σ.readyQueues) (hr : ∀ t ∈ σ'.releaseQueue, t ∈ σ.releaseQueue) + (ha : σ'.schedulerAction = σ.schedulerAction) (hc : σ'.curThread = σ.curThread) + (hi : σ'.idleThread = σ.idleThread) (hs : σ'.curSc = σ.curSc) : SchedValidB σ' := by + have act : ∀ p, ScActiveAtA σ p → ScActiveAtA σ' p := fun p ⟨y, k, e, a⟩ => ⟨y, k, by rw [hk]; exact e, a⟩ + refine ⟨fun d p t hm => ?_, fun e1 e2 => ?_, fun t x sc e1 e2 e3 => ?_⟩ + · rw [hq] at hm + obtain ⟨⟨x, sc, e, es, a⟩, n⟩ := h.rq d p t hm + exact ⟨⟨x, sc, by rw [hk]; exact e, es, act _ a⟩, fun hm' => n (hr t hm')⟩ + · rw [ha] at e1 + rw [hc, hi] at e2 + obtain ⟨a, n⟩ := h.act e1 e2 + rw [hc] + exact ⟨a.ofKh hk, fun hm' => n (hr _ hm')⟩ + · rw [hk] at e1 + exact act _ (h.blk t x sc e1 e2 e3) + +theorem FIA.ofRelSub {σ σ' : State Unit} (h : FIA σ) (hk : σ'.kheap = σ.kheap) + (hq : σ'.readyQueues = σ.readyQueues) (hr : ∀ t ∈ σ'.releaseQueue, t ∈ σ.releaseQueue) + (ha : σ'.schedulerAction = σ.schedulerAction) (hc : σ'.curThread = σ.curThread) + (hi : σ'.idleThread = σ.idleThread) (hs : σ'.curSc = σ.curSc) : FIA σ' := + ⟨h.1.ofRelSub hk hq hr ha hc hi hs, fun t ht => (h.2 t (hr t ht)).ofKh hk⟩ + +/-- A scalar-field update (`reprogramTimer`, `curSc`-free ones): `FIA` is kept. -/ +theorem FIA.ofSame {σ σ' : State Unit} (h : FIA σ) (hk : σ'.kheap = σ.kheap) + (hq : σ'.readyQueues = σ.readyQueues) (hr : σ'.releaseQueue = σ.releaseQueue) + (ha : σ'.schedulerAction = σ.schedulerAction) (hc : σ'.curThread = σ.curThread) + (hi : σ'.idleThread = σ.idleThread) (hs : σ'.curSc = σ.curSc) : FIA σ' := + h.ofRelSub hk hq (fun t ht => by rw [hr] at ht; exact ht) ha hc hi hs + +/-- The ready queues replaced by a family whose threads are old members or have an active SC outside the release +queue. -/ +theorem FIA.updRq {σ : State Unit} (h : FIA σ) (f : Domain → Priority → List ObjRef) + (hmem : ∀ d p t, t ∈ f d p → t ∈ σ.readyQueues d p ∨ (ActTcbA σ t ∧ t ∉ σ.releaseQueue)) : + FIA { σ with readyQueues := f } := by + refine ⟨⟨fun d p t hm => ?_, h.1.act, h.1.blk⟩, h.2⟩ + rcases hmem d p t hm with m | ⟨⟨x, sc, e, es, a⟩, n⟩ + · exact h.1.rq d p t m + · exact ⟨⟨x, sc, e, es, a⟩, n⟩ + +namespace HT + +theorem setTcbQueue {P : State Unit → Prop} (R : State Unit → Prop) + (hR : ∀ σ f, R σ → R { σ with readyQueues := f }) (d : Domain) (p : Priority) (q : ReadyQueue) + (h : ∀ σ, P σ → FIA σ ∧ R σ ∧ ∀ t ∈ q, t ∈ σ.readyQueues d p ∨ (ActTcbA σ t ∧ t ∉ σ.releaseQueue)) : + HT P (setTcbQueue (Z := Unit) d p q) (fun _ σ => FIA σ ∧ R σ) := by + unfold Fl4SpecA64.setTcbQueue + refine HT.modify _ fun σ hσ => ?_ + obtain ⟨hI, hr, hq⟩ := h σ hσ + refine ⟨hI.updRq _ fun d' p' t hm => ?_, hR σ _ hr⟩ + by_cases e : d' = d ∧ p' = p + · rw [if_pos e] at hm + rw [e.1, e.2]; exact hq t hm + · rw [if_neg e] at hm; exact Or.inl hm + +theorem schedAction {P : State Unit → Prop} (R : State Unit → Prop) + (hR : ∀ σ f, R σ → R { σ with readyQueues := f }) (action : ObjRef → List ObjRef → List ObjRef) (t : ObjRef) + (h : ∀ σ, P σ → FIA σ ∧ R σ ∧ ∀ q, ∀ t' ∈ action t q, t' ∈ q ∨ (ActTcbA σ t' ∧ t' ∉ σ.releaseQueue)) : + HT P (tcbSchedAction (Z := Unit) action t) (fun _ σ => FIA σ ∧ R σ) := by + unfold Fl4SpecA64.tcbSchedAction + refine HT.bind (Q := fun _ σ => P σ) (HT.threadGet _ t fun σ hσ x hx => hσ) fun d => ?_ + refine HT.bind (Q := fun _ σ => P σ) (HT.threadGet _ t fun σ hσ x hx => hσ) fun p => ?_ + unfold Fl4SpecA64.getTcbQueue + refine HT.bind (Q := fun q σ => P σ ∧ q = σ.readyQueues d p) + (HT.bind (Q := fun qs σ => P σ ∧ qs = σ.readyQueues) (HT.gets _ fun σ hσ => ⟨hσ, rfl⟩) fun qs => + HT.pure _ fun σ hσ => ⟨hσ.1, by rw [hσ.2]⟩) fun queue => ?_ + refine HT.bind (Q := fun q' σ => P σ ∧ queue = σ.readyQueues d p ∧ q' = action t queue) + (HT.pure _ fun σ hσ => ⟨hσ.1, hσ.2, rfl⟩) fun queue' => ?_ + refine HT.setTcbQueue (P := fun σ => P σ ∧ queue = σ.readyQueues d p ∧ queue' = action t queue) R hR d p queue' + fun σ hσ => ?_ + obtain ⟨hP, hq, hq'⟩ := hσ + obtain ⟨hI, hr, ha⟩ := h σ hP + refine ⟨hI, hr, fun t' ht' => ?_⟩ + rw [hq'] at ht' + rcases ha queue t' ht' with m | m + · rw [hq] at m; exact Or.inl m + · exact Or.inr m + +theorem enq (t : ObjRef) (R : State Unit → Prop) (hR : ∀ σ f, R σ → R { σ with readyQueues := f }) : + HT (fun σ => FIA σ ∧ R σ ∧ ActTcbA σ t ∧ t ∉ σ.releaseQueue) (tcbSchedAction (Z := Unit) tcbSchedEnqueue t) + (fun _ σ => FIA σ ∧ R σ) := + HT.schedAction R hR _ t fun σ hσ => ⟨hσ.1, hσ.2.1, fun q t' ht' => by + unfold tcbSchedEnqueue at ht' + by_cases hm : t ∉ q + · rw [if_pos hm] at ht' + rcases List.mem_cons.1 ht' with e | m + · rw [e]; exact Or.inr ⟨hσ.2.2.1, hσ.2.2.2⟩ + · exact Or.inl m + · rw [if_neg hm] at ht'; exact Or.inl ht'⟩ + +theorem app (t : ObjRef) (R : State Unit → Prop) (hR : ∀ σ f, R σ → R { σ with readyQueues := f }) : + HT (fun σ => FIA σ ∧ R σ ∧ ActTcbA σ t ∧ t ∉ σ.releaseQueue) (tcbSchedAction (Z := Unit) tcbSchedAppend t) + (fun _ σ => FIA σ ∧ R σ) := + HT.schedAction R hR _ t fun σ hσ => ⟨hσ.1, hσ.2.1, fun q t' ht' => by + unfold tcbSchedAppend at ht' + by_cases hm : t ∉ q + · rw [if_pos hm] at ht' + rcases List.mem_append.1 ht' with m | e + · exact Or.inl m + · rw [List.mem_singleton.1 e]; exact Or.inr ⟨hσ.2.2.1, hσ.2.2.2⟩ + · rw [if_neg hm] at ht'; exact Or.inl ht'⟩ + +theorem deq (t : ObjRef) (R : State Unit → Prop) (hR : ∀ σ f, R σ → R { σ with readyQueues := f }) : + HT (fun σ => FIA σ ∧ R σ) (tcbSchedAction (Z := Unit) tcbSchedDequeue t) (fun _ σ => FIA σ ∧ R σ) := + HT.schedAction R hR _ t fun σ hσ => ⟨hσ.1, hσ.2, fun q t' ht' => Or.inl (List.mem_filter.1 ht').1⟩ + +theorem tcbReleaseRemove (t : ObjRef) (R : State Unit → Prop) (hr : ∀ σ f, R σ → R { σ with releaseQueue := f }) + (hp : ∀ σ b, R σ → R { σ with reprogramTimer := b }) : + HT (fun σ => FIA σ ∧ R σ) (tcbReleaseRemove (Z := Unit) t) (fun _ σ => FIA σ ∧ R σ) := by + unfold Fl4SpecA64.tcbReleaseRemove + refine HT.bind (Q := fun qs σ => (FIA σ ∧ R σ) ∧ qs = σ.releaseQueue) (HT.gets _ fun σ hσ => ⟨hσ, rfl⟩) fun qs => ?_ + refine HT.bind (Q := fun _ σ => (FIA σ ∧ R σ) ∧ qs = σ.releaseQueue) + (HT.when (fun _ => HT.modify _ fun σ hσ => + ⟨⟨hσ.1.1.ofSame rfl rfl rfl rfl rfl rfl rfl, hp σ true hσ.1.2⟩, hσ.2⟩) (fun _ σ hσ => hσ)) fun _ => ?_ + refine HT.bind (Q := fun qs' σ => (FIA σ ∧ R σ) ∧ qs = σ.releaseQueue ∧ qs' = tcbSchedDequeue t qs) + (HT.pure _ fun σ hσ => ⟨hσ.1, hσ.2, rfl⟩) fun qs' => ?_ + refine HT.modify _ fun σ hσ => ?_ + obtain ⟨⟨hI, hR⟩, hq, hq'⟩ := hσ + refine ⟨hI.ofRelSub rfl rfl (fun t' ht' => ?_) rfl rfl rfl rfl, hr σ _ hR⟩ + have ht'' : t' ∈ qs' := ht' + rw [hq'] at ht'' + rw [← hq] + exact (List.mem_filter.1 ht'').1 + +theorem setAct (a : SchedulerAction) (ha : a ≠ .resumeCurThread) (R : State Unit → Prop) + (hR : ∀ σ a, R σ → R { σ with schedulerAction := a }) : + HT (fun σ => FIA σ ∧ R σ) (setSchedulerAction (Z := Unit) a) (fun _ σ => FIA σ ∧ R σ) := by + unfold Fl4SpecA64.setSchedulerAction + exact HT.modify _ fun σ hσ => ⟨⟨⟨hσ.1.1.rq, fun e1 => absurd e1 ha, hσ.1.1.blk⟩, hσ.1.2⟩, hR σ a hσ.2⟩ + +theorem whileLoop {α : Type} {C : α → State Unit → Prop} {B : α → SMonad Unit α} (I : State Unit → Prop) (r : α) + (hB : ∀ a, HT I (B a) (fun _ σ => I σ)) : HT I (NonDet.whileLoop C B r) (fun _ σ => I σ) := by + intro s hs x hx + have key : ∀ r s x, NonDet.WhileResults C B r s x → I s → I x.2 := by + intro r s x h + induction h with + | done _ => exact id + | step hC hb _ ih => exact fun hI => ih (hB _ _ hI _ hb) + exact key r s x hx hs + +end HT + +/-- The target's active SC and absence from the release queue (preserved by ready-queue and action writes). -/ +def TgtA (t : ObjRef) (σ : State Unit) : Prop := ActTcbA σ t ∧ t ∉ σ.releaseQueue + +theorem TgtA.rq {t : ObjRef} {σ : State Unit} (h : TgtA t σ) (f : Domain → Priority → List ObjRef) : + TgtA t { σ with readyQueues := f } := h + +theorem TgtA.act {t : ObjRef} {σ : State Unit} (h : TgtA t σ) (a : SchedulerAction) : + TgtA t { σ with schedulerAction := a } := h + +/-- **`rescheduleRequired` keeps `FIA`** (its enqueue is under `schedulable`); and any predicate preserved by ready-queue +and action writes. -/ +theorem resched_fia (R : State Unit → Prop) (hq : ∀ σ f, R σ → R { σ with readyQueues := f }) + (ha : ∀ σ a, R σ → R { σ with schedulerAction := a }) : + HT (fun σ => FIA σ ∧ R σ) (rescheduleRequired (Z := Unit)) (fun _ σ => FIA σ ∧ R σ) := by + unfold Fl4SpecA64.rescheduleRequired + refine HT.bind (Q := fun a σ => (FIA σ ∧ R σ) ∧ a = σ.schedulerAction) (HT.gets _ fun σ hσ => ⟨hσ, rfl⟩) fun action => ?_ + have fin : HT (fun σ => FIA σ ∧ R σ) (setSchedulerAction (Z := Unit) .chooseNewThread) (fun _ σ => FIA σ ∧ R σ) := + HT.setAct .chooseNewThread (by intro e; cases e) R ha + cases action with + | switchThread t => + dsimp only + refine HT.bind (Q := fun b σ => (FIA σ ∧ R σ) ∧ b = schedulable t σ) (HT.gets _ fun σ hσ => ⟨hσ.1, rfl⟩) fun sched => ?_ + refine HT.bind (Q := fun _ σ => FIA σ ∧ R σ) ?_ fun _ => fin + refine HT.when (fun hs => ?_) (fun _ σ hσ => hσ.1) + refine HT.mono (P := fun σ => (FIA σ ∧ R σ) ∧ schedulable t σ = true) ?_ + (fun σ hσ => ⟨hσ.1, by rw [← hσ.2]; exact hs⟩) (fun _ _ h => h) + refine HT.bind (Q := fun _ σ => (FIA σ ∧ R σ) ∧ schedulable t σ = true) (HT.threadGet _ t fun σ hσ x hx => hσ) fun scOpt => ?_ + refine HT.bind (Q := fun _ σ => (FIA σ ∧ R σ) ∧ schedulable t σ = true) (HT.assertOpt _ fun a _ σ hσ => hσ) fun scp => ?_ + refine HT.bind (Q := fun _ σ => (FIA σ ∧ R σ) ∧ schedulable t σ = true) (HT.getsThe _ fun σ hσ a _ => hσ) fun suf => ?_ + refine HT.bind (Q := fun _ σ => (FIA σ ∧ R σ) ∧ schedulable t σ = true) (HT.getsThe _ fun σ hσ a _ => hσ) fun rdy => ?_ + refine HT.bind (Q := fun _ σ => (FIA σ ∧ R σ) ∧ schedulable t σ = true) (HT.assert fun _ σ hσ => hσ) fun _ => ?_ + refine HT.mono (HT.enq t R hq) (fun σ hσ => ?_) (fun _ _ h => h) + obtain ⟨a, n, -⟩ := schedulable_actA hσ.2 + exact ⟨hσ.1.1, hσ.1.2, a, n⟩ + | resumeCurThread => + dsimp only + exact HT.mono fin (fun σ hσ => hσ.1) (fun _ _ h => h) + | chooseNewThread => + dsimp only + exact HT.mono fin (fun σ hσ => hσ.1) (fun _ _ h => h) + +/-- **`possibleSwitchTo` keeps `FIA`**, given the target's active SC (the caller's obligation: F8(c) captured before the +state write, F8(d), or a `schedulable` guard). Its guard leaves the release queue. -/ +theorem possibleSwitchTo_fia (target : ObjRef) : + HT (fun σ => FIA σ ∧ ActTcbA σ target) (possibleSwitchTo (Z := Unit) target) (fun _ σ => FIA σ) := by + unfold Fl4SpecA64.possibleSwitchTo Fl4SpecA64.getTcbObjRef + refine HT.bind (Q := fun _ σ => FIA σ ∧ ActTcbA σ target) (HT.threadGet _ target fun σ hσ x hx => hσ) fun scOpt => ?_ + refine HT.bind (Q := fun inq σ => (FIA σ ∧ ActTcbA σ target) ∧ inq = inReleaseQueue target σ) + (HT.gets _ fun σ hσ => ⟨hσ, rfl⟩) fun inq => ?_ + refine HT.when (fun hc => ?_) (fun _ σ hσ => hσ.1.1) + refine HT.mono (P := fun σ => FIA σ ∧ TgtA target σ) ?_ + (fun σ hσ => ⟨hσ.1.1, hσ.1.2, fun m => hc.2 (by rw [hσ.2]; simp [inReleaseQueue, m])⟩) (fun _ _ h => h) + refine HT.bind (Q := fun _ σ => FIA σ ∧ TgtA target σ) (HT.gets _ fun σ hσ => hσ) fun curDom => ?_ + refine HT.bind (Q := fun _ σ => FIA σ ∧ TgtA target σ) (HT.threadGet _ target fun σ hσ x hx => hσ) fun targetDom => ?_ + refine HT.bind (Q := fun _ σ => FIA σ ∧ TgtA target σ) (HT.gets _ fun σ hσ => hσ) fun action => ?_ + have enqT : HT (fun σ => FIA σ ∧ TgtA target σ) (tcbSchedAction (Z := Unit) tcbSchedEnqueue target) + (fun _ σ => FIA σ ∧ TgtA target σ) := + HT.mono (HT.enq target (TgtA target) (fun σ f h => h)) (fun σ hσ => ⟨hσ.1, hσ.2, hσ.2.1, hσ.2.2⟩) (fun _ _ h => h) + refine HT.mono (Q := fun _ σ => FIA σ ∧ TgtA target σ) ?_ (fun σ hσ => hσ) (fun _ σ h => h.1) + refine HT.ite (fun _ => enqT) (fun _ => ?_) + refine HT.ite (fun _ => ?_) (fun _ => ?_) + · exact HT.bind (Q := fun _ σ => FIA σ ∧ TgtA target σ) + (resched_fia (TgtA target) (fun σ f h => h) (fun σ a h => h)) fun _ => enqT + · exact HT.setAct (.switchThread target) (by intro e; cases e) (TgtA target) (fun σ a h => h) + +theorem tcbReleaseDequeue_fia : HT FIA (tcbReleaseDequeue (Z := Unit)) (fun _ σ => FIA σ) := by + unfold Fl4SpecA64.tcbReleaseDequeue + refine HT.bind (Q := fun rq σ => FIA σ ∧ rq = σ.releaseQueue) (HT.gets _ fun σ hσ => ⟨hσ, rfl⟩) fun rq => ?_ + refine HT.bind (Q := fun _ σ => (FIA σ ∧ rq = σ.releaseQueue) ∧ rq ≠ []) + (HT.assert fun hne σ hσ => ⟨hσ, hne⟩) fun _ => ?_ + refine HT.bind (Q := fun _ σ => FIA σ ∧ ActTcbA σ (the rq.head?)) ?_ fun _ => possibleSwitchTo_fia _ + refine HT.mono (HT.tcbReleaseRemove (the rq.head?) (fun σ => ActTcbA σ (the rq.head?)) + (fun σ f h => h) (fun σ b h => h)) (fun σ hσ => ?_) (fun _ σ h => h) + obtain ⟨⟨hI, hq⟩, hne⟩ := hσ + refine ⟨hI, hI.2 _ ?_⟩ + rw [← hq] + cases rq with + | nil => exact absurd rfl hne + | cons a l => exact List.mem_cons_self + +theorem awaken_fia : HT FIA (awaken (Z := Unit)) (fun _ σ => FIA σ) := by + unfold Fl4SpecA64.awaken + exact HT.whileLoop FIA () fun _ => tcbReleaseDequeue_fia + +theorem checkDomainTime_fia : HT FIA (checkDomainTime (Z := Unit)) (fun _ σ => FIA σ) := by + unfold Fl4SpecA64.checkDomainTime + refine HT.bind (Q := fun _ σ => FIA σ) (HT.gets _ fun σ hσ => hσ) fun exp => ?_ + refine HT.when (fun _ => ?_) (fun _ σ hσ => hσ) + refine HT.bind (Q := fun _ σ => FIA σ) (HT.modify _ fun σ hσ => hσ.ofSame rfl rfl rfl rfl rfl rfl rfl) fun _ => ?_ + exact HT.mono (resched_fia (fun _ => True) (fun _ _ h => h) (fun _ _ h => h)) (fun σ hσ => ⟨hσ, trivial⟩) + (fun _ σ h => h.1) + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3SchedWrA.lean b/proofs/Fl4ProofsA64/P3/Stage3SchedWrA.lean new file mode 100644 index 000000000..a6646d107 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SchedWrA.lean @@ -0,0 +1,920 @@ +/- +P3 (aarch64, A64-P3 Stage-3 integration; `stage3-schedpres-contract.md` §5, ruling of 2026-12-03 00:33Z C): +**`JStage3A` across a scheduler step**. + +* **`SchedStepWA`**: the write set of a scheduler step, `MWrA` together with the per-step TCB words `TcbStepWA` + (every word of a TCB of `Z.T` from offset 224, so the state word 3328 and the registers, except the two VCPU links). +* **`stepW_off…A`**: the written words miss the `OS` slots, the typed heap's slots, the IRQ-state words and every + VSpace / Stage-2 / unmapped-table / VCPU word the relation reads. +* **`JStage3A.of_schedWr`** (J7 = `J7A`): the record at the post state. The caller supplies what the step changes + (P1's `SysInvA` / queues / pool placement, the scheduler, IPC, MCS and `SInvXA` relations, J5, J7 and the spec + step `SpecSchedA`); every other field is carried over. + +No new premise. +-/ +import Fl4ProofsA64.P3.Stage3J7A +import Fl4ProofsA64.P3.Stage3SchedPresA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-- The per-step TCB words: everything from offset 224 of a TCB of `T` but the two VCPU links. -/ +def TcbStepWA (T : Set U64) (b : U64) : Prop := + ∃ t ∈ T, t.val + 224 ≤ b.val ∧ b.val < t.val + 4096 ∧ b ≠ wa t 3664#u64 ∧ b ≠ runVcpuA t + +/-- **The write set of a scheduler step** (module docstring). -/ +def SchedStepWA (Z : XSetsA) (m : Fl4SpecA64.McState) (σ : Fl4SpecA64.State Unit) (s : S) (b : U64) : Prop := + MWrA (st := st) Z m σ s b ∨ TcbStepWA Z.T b + +/-- A written word is an SC word, a kernel-state scheduler word or a per-step TCB word. -/ +theorem stepW_casesA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} {σ : Fl4SpecA64.State Unit} + (H : SchedSRN st Z m κ s σ) {b : U64} (h : SchedStepWA (st := st) Z m σ s b) : + ScFootA Z.C m σ b ∨ SKsA (L.ksb s) b ∨ TcbStepWA Z.T b := by + rcases h with (h | h | ⟨u, hu, e⟩) | h + · exact Or.inl h + · exact Or.inr (Or.inl h) + · refine Or.inr (Or.inr ⟨u, hu, ?_⟩) + have hfit := (H.core.tcbs u hu).fits + have hv : ∀ K : U64, K.val < 4096 → (wa u K).val = u.val + K.val := by + intro K hK; rw [wa_valm]; omega + have key : ∀ K : U64, 224 ≤ K.val → K.val < 4096 → K.val ≠ 3664 → K.val ≠ 3672 → b = wa u K → + u.val + 224 ≤ b.val ∧ b.val < u.val + 4096 ∧ b ≠ wa u 3664#u64 ∧ b ≠ runVcpuA u := by + intro K h1 h2 h3 h4 e + subst e + refine ⟨by rw [hv K h2]; omega, by rw [hv K h2]; omega, fun c => ?_, fun c => ?_⟩ + · have := congrArg UScalar.val c + rw [hv K h2, hv _ (by simp)] at this + simp at this + omega + · unfold runVcpuA at c + have := congrArg UScalar.val c + rw [hv K h2, hv _ (by simp)] at this + simp at this + omega + rcases e with e | e | e | e + · exact key 3464#u64 (by simp) (by simp) (by simp) (by simp) e + · exact key 3480#u64 (by simp) (by simp) (by simp) (by simp) e + · exact key 3488#u64 (by simp) (by simp) (by simp) (by simp) e + · exact key 3592#u64 (by simp) (by simp) (by simp) (by simp) e + · exact Or.inr (Or.inr h) + +section off +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 : JSlotA S} {R : Set U64 → JSlotA S} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7A st R) s a σ m) +include J + +/-- An SC word lies inside its heap SC object (`scSize`). -/ +theorem JStage3A.scFoot_heapA {b : U64} (h : ScFootA Z.C m σ b) : + ∃ c ∈ Z.C, ∃ z, H c = some (.sc z) ∧ InSC c (H.scBits c) b := by + obtain ⟨c, hc, x, k, hk, h1, h2⟩ := h + have hcH : c ∈ H.scs := by rw [J.scs]; exact hc + obtain ⟨z, hz⟩ := hcH + refine ⟨c, hc, z, hz, h1, lt_of_lt_of_le h2 ?_⟩ + have := (J.scSize c hc x k hk).1 + exact Nat.add_le_add_left (Nat.pow_le_pow_right (by decide) this) _ + +/-- A word of a heap object that is no state-block `other` region is off the 128 nodes. -/ +theorem JStage3A.heap_offNodesA {p b : U64} {k : HKind} (hp : H p = some k) (h1 : p.val ≤ b.val) + (h2 : b.val < p.val + 2 ^ k.bits) : + ¬ ((L.ksb s).val + 223488 ≤ b.val ∧ b.val < (L.ksb s).val + 772352) := by + rintro ⟨n1, n2⟩ + by_cases ho : ∃ z, k = .other z + · obtain ⟨z, rfl⟩ := ho + simp only [HKind.bits] at h2 + rcases J.ko.2 p z hp with h | h | h | h <;> omega + · have := J.ko.1 p k hp (fun z e => ho ⟨z, e⟩) + omega + +/-- **A heap word outside the SCs and the kernel globals is not written.** -/ +theorem JStage3A.omega_offStepA {b : U64} (hΩ : b ∈ H.omega) + (hsc : ∀ c ∈ H.scs, ¬ InSC c (H.scBits c) b) + (hg : ∀ o : U64, o.val < 223488 → wa (L.ksb s) o ≠ b) : ¬ SchedStepWA (st := st) Z m σ s b := by + intro hw + obtain ⟨Q, R', q⟩ := J.sys.ent.bxi.1 + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.sys.ent.ksw + rcases stepW_casesA J.sr.toSchedSRN hw with h | h | ⟨t, ht, h1, h2, -⟩ + · obtain ⟨c, hc, z, hz, hin⟩ := J.scFoot_heapA h + exact hsc c ⟨z, hz⟩ hin + · have h' : ((L.ksb s).val ≤ b.val ∧ b.val < (L.ksb s).val + 224) ∨ + ((L.ksb s).val + 223488 ≤ b.val ∧ b.val < (L.ksb s).val + 772352) := by + unfold SKsA at h; omega + rcases h' with ⟨h1, h2⟩ | h + · refine hg ⟨BitVec.ofNat 64 (b.val - (L.ksb s).val)⟩ ?_ ?_ + all_goals + have e : (⟨BitVec.ofNat 64 (b.val - (L.ksb s).val)⟩ : U64).val = b.val - (L.ksb s).val := by + show (BitVec.ofNat 64 _).toNat = _ + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + · omega + · exact UScalar.eq_of_val_eq (by rw [wa_valm, e]; rw [Nat.mod_eq_of_lt (by omega)]; omega) + · obtain ⟨-, p, k, hp, -, hin⟩ := hΩ + exact J.heap_offNodesA hp hin.1 hin.2 h + · have htH : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + have := (q.omega b hΩ).2.1 t htH + omega + +/-- **The VSpace, Stage-2 and unmapped-table words are not written** (`VrOK` at `J7A`'s link). -/ +theorem JStage3A.vsr_offStepA {Pt Ub : Set U64} + (vr : VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt) {b : U64} (h : VsR (L.ksb s) Pt b) + (hna : ¬ AsW (L.ksb s) b) : ¬ SchedStepWA (st := st) Z m σ s b := + J.omega_offStepA (vr.om b h) (vr.sc b h) (vr.ks b h) + +/-- (record edit #8b-B1) An `OS` slot word is off the domain-schedule words. -/ +theorem JStage3A.os_domA {y b : U64} (hy : y ∈ OS) (h1 : y.val ≤ b.val) (h2 : b.val < y.val + 32) + (hd : DomW (L.ksb s) b) : False := by + obtain ⟨o, ho, eo⟩ := hd.wa_exists J.sr.ksFit + exact J.sys.sysl.ly.kslo o ho y hy (by rw [eo]; exact ⟨h1, h2⟩) + +/-- (record edit #8b-B1) **A cap-slot word is off the domain-schedule words**. -/ +theorem JStage3A.capw_domA {b : U64} (hc : W7.CapW H.tcbs {b | InOS OS b} ∅ b) (hd : DomW (L.ksb s) b) : False := by + obtain ⟨Q, R', q⟩ := J.sys.ent.bxi.1 + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.sys.ent.ksw + rcases hc with ⟨t, ht, K, hK, e⟩ | ⟨⟨y, hy, y1, y2⟩, -⟩ + · have ev : b.val = t.val + K.val := by rw [e]; exact field_val (q.tcbs t ht) (K := K) (by omega) + have := J.sr.tcb_ks (show t ∈ Z.T by rw [← J.tcbs]; exact ht) + unfold DomW at hd + unfold ksSizeA at this + omega + · exact J.os_domA hy y1 y2 hd + +/-- **An `OS` slot word is not written.** -/ +theorem JStage3A.os_offStepA {b : U64} (hb : Aligned b) (h : InOS OS b) : ¬ SchedStepWA (st := st) Z m σ s b := by + intro hw + obtain ⟨Q, R', q⟩ := J.sys.ent.bxi.1 + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.sys.ent.ksw + have cap : W7.CapW H.tcbs H.omega H.frame b := inOS_capw J.sys.ent.os hb h + obtain ⟨y, hy, y1, y2⟩ := h + rcases stepW_casesA J.sr.toSchedSRN hw with h | h | ⟨t, ht, h1, h2, -⟩ + · obtain ⟨c, hc, z, hz, c1, c2⟩ := J.scFoot_heapA h + have := J.ap.sca y hy c ⟨z, hz⟩ + omega + · have h' : ((L.ksb s).val ≤ b.val ∧ b.val < (L.ksb s).val + 224) ∨ + ((L.ksb s).val + 223488 ≤ b.val ∧ b.val < (L.ksb s).val + 772352) := by + unfold SKsA at h; omega + rcases h' with ⟨h1, h2⟩ | h + · have e : (⟨BitVec.ofNat 64 (b.val - (L.ksb s).val)⟩ : U64).val = b.val - (L.ksb s).val := by + show (BitVec.ofNat 64 _).toNat = _ + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + have eb : wa (L.ksb s) ⟨BitVec.ofNat 64 (b.val - (L.ksb s).val)⟩ = b := + UScalar.eq_of_val_eq (by rw [wa_valm, e]; rw [Nat.mod_eq_of_lt (by omega)]; omega) + exact J.sys.sysl.ly.kslo _ (by rw [e]; omega) y hy (by rw [eb]; exact ⟨y1, y2⟩) + · rcases cap with ⟨t, ht, K, hK, e⟩ | ⟨hΩ, -⟩ + · have ev : b.val = t.val + K.val := by rw [e]; exact field_val (q.tcbs t ht) (K := K) (by omega) + have := J.heap_offNodesA (p := t) (b := b) (k := .tcb) ht (by omega) + (by simp only [HKind.bits]; omega) + exact this h + · obtain ⟨-, p, k, hp, -, hin⟩ := hΩ + exact J.heap_offNodesA hp hin.1 hin.2 h + · have htH : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + have hf := tcb4096 (q.tcbs t htH) + have eK : (⟨BitVec.ofNat 64 (b.val - t.val)⟩ : U64).val = b.val - t.val := by + show (BitVec.ofNat 64 _).toNat = _ + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + refine capw_ne_hi q cap htH (K := ⟨BitVec.ofNat 64 (b.val - t.val)⟩) (by omega) (by omega) ?_ + exact UScalar.eq_of_val_eq (by rw [field_val (q.tcbs t htH) (by omega), eK]; omega) + +/-- `OSKeep` across a scheduler step. -/ +theorem JStage3A.osKeep_stepA {s' : S} (w : WritesIn st s s' (SchedStepWA (st := st) Z m σ s)) : + OSKeep st OS s s' := by + intro y hy + have hfit := J.sys.ent.os.fit y hy + have al0 := J.sys.ent.os.al0 hy + have key : ∀ o : U64, o.val < 32 → o.val % 8 = 0 → L.mem s' (wa y o) = L.mem s (wa y o) := by + intro o o1 o2 + have hv : (wa y o).val = y.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have hal : Aligned (wa y o) := J.sys.ent.os.al hy o1 o2 + exact w.mem_eq hal (J.os_offStepA hal ⟨y, hy, by omega, by omega⟩) + have e0 : wa y 0#u64 = y := by apply UScalar.eq_of_val_eq; rw [wa_valm]; simp + exact ⟨by have := key 0#u64 (by simp) (by simp); rwa [e0] at this, key 8#u64 (by simp) (by simp), + key 16#u64 (by simp) (by simp), key 24#u64 (by simp) (by simp)⟩ + +/-- **A word inside a slot of the typed heap is not written.** -/ +theorem JStage3A.slot_offStepA {y b : U64} (hy : IsSlotA Z.th y) (y1 : y.val ≤ b.val) (y2 : b.val < y.val + 32) : + ¬ SchedStepWA (st := st) Z m σ s b := by + intro hw + have P := J.sr.place + have obj : ∃ p k, Z.th p = some k ∧ p.val ≤ y.val ∧ y.val + 32 ≤ p.val + k.size ∧ + (k = .tcb → y.val + 32 ≤ p.val + 224) := by + obtain ⟨p, ⟨r, hr, i, hi, rfl⟩ | ⟨ht, i, hi, rfl⟩⟩ := hy + · obtain ⟨k, hk, hs, hv⟩ := slotVA_of P hr (j := i) (by rw [cnode_size]; omega) + exact ⟨p, k, hk, by omega, by omega, fun e => by + rw [e, hr] at hk; cases hk⟩ + · obtain ⟨k, hk, hs, hv⟩ := tcb_slotV P ht hi + have : i < 7 := hi + exact ⟨p, k, hk, by omega, by omega, fun _ => by omega⟩ + obtain ⟨p, k, hk, p1, p2, pt⟩ := obj + have hksz : ksSizeA = 788736 := rfl + rcases stepW_casesA J.sr.toSchedSRN hw with h | h | ⟨t, ht, t1, t2, -⟩ + · obtain ⟨c, hc, x, n', hx, ha⟩ := h + have := (J.sr.scp.out c hc x n' hx b ha).1 p k hk + omega + · have hin : IrqNodeA (L.ksb s) p → + (L.ksb s).val + 43232 ≤ b.val ∧ b.val < (L.ksb s).val + 215264 := by + intro hn + have ek := P.irqK hk hn + subst ek + obtain ⟨i, hi, rfl⟩ := hn + have ev := irqSlotA_val J.sr.ksFit hi + have : ObjKindA.size (.cnode 0) = 32 := rfl + omega + by_cases hn : IrqNodeA (L.ksb s) p + · have := hin hn + unfold SKsA at h; omega + · have := P.ks p k hk hn + unfold SKsA at h; omega + · have hth := (J.sr.thT t).2 ht + by_cases e : p = t + · subst e + rw [hth] at hk + cases hk + have := pt rfl + omega + · have := P.apart p t k .tcb hk hth e + have : ObjKindA.size .tcb = 4096 := rfl + omega + +/-- A word of a TCB of `Z.T` at a VCPU link is not written. -/ +theorem JStage3A.link_offStepA {t K : U64} (ht : t ∈ Z.T) (hK : K = 3664#u64 ∨ K = 3672#u64) : + ¬ SchedStepWA (st := st) Z m σ s (wa t K) := by + intro hw + have hfit := (J.sr.core.tcbs t ht).fits + have hKv : K.val = 3664 ∨ K.val = 3672 := by rcases hK with rfl | rfl <;> simp + have hv : (wa t K).val = t.val + K.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have hth := (J.sr.thT t).2 ht + rcases stepW_casesA J.sr.toSchedSRN hw with h | h | ⟨u, hu, u1, u2, n1, n2⟩ + · obtain ⟨c, hc, x, n', hx, ha⟩ := h + have := (J.sr.scp.out c hc x n' hx _ ha).1 t _ hth + have : ObjKindA.size .tcb = 4096 := rfl + omega + · have := J.sr.tcb_ks ht + unfold SKsA at h + unfold ksSizeA at this; omega + · by_cases e : u = t + · subst e + rcases hK with rfl | rfl + · exact n1 rfl + · exact n2 rfl + · have := J.sr.place.apart u t .tcb .tcb ((J.sr.thT u).2 hu) hth e + have : ObjKindA.size .tcb = 4096 := rfl + omega + +/-- An IRQ-state word is not written. -/ +theorem JStage3A.irq_offStepA {i : U64} (hi : i.val < 5376) : + ¬ SchedStepWA (st := st) Z m σ s (irqWordA (L.ksb s) i) := by + intro hw + have hv := irqWordA_val J.sr.ksFit hi + have hksz : ksSizeA = 788736 := rfl + rcases stepW_casesA J.sr.toSchedSRN hw with h | h | ⟨t, ht, t1, t2, -⟩ + · obtain ⟨c, hc, x, n', hx, ha⟩ := h + have := (J.sr.scp.out c hc x n' hx _ ha).2 + omega + · unfold SKsA at h; omega + · have := J.sr.tcb_ks ht + omega + +/-- An ASID-table word is not written. -/ +theorem JStage3A.asid_offStepA {b : U64} (h1 : (L.ksb s).val + 772352 ≤ b.val) + (h2 : b.val < (L.ksb s).val + 773376) : ¬ SchedStepWA (st := st) Z m σ s b := by + intro hw + have hksz : ksSizeA = 788736 := rfl + rcases stepW_casesA J.sr.toSchedSRN hw with h | h | ⟨t, ht, t1, t2, -⟩ + · obtain ⟨c, hc, x, n', hx, ha⟩ := h + have := (J.sr.scp.out c hc x n' hx _ ha).2 + omega + · unfold SKsA at h; omega + · have := J.sr.tcb_ks ht + omega + +/-- (record edit #8b-B1) A domain-schedule word is not written. -/ +theorem JStage3A.dom_offStepA {b : U64} (hd : DomW (L.ksb s) b) : ¬ SchedStepWA (st := st) Z m σ s b := by + intro hw + have hksz : ksSizeA = 788736 := rfl + unfold DomW at hd + rcases stepW_casesA J.sr.toSchedSRN hw with h | h | ⟨t, ht, t1, t2, -⟩ + · obtain ⟨c, hc, x, n', hx, ha⟩ := h + have := (J.sr.scp.out c hc x n' hx _ ha).2 + omega + · unfold SKsA at h; omega + · have := J.sr.tcb_ks ht + omega + +/-! ### Words off the domain-schedule words `DomW` (the advancing `schedule`'s wider write set) -/ + +/-- A word off the first `223488` bytes of the kernel-state block is off `DomW`. -/ +theorem JStage3A.omega_domA {b : U64} (hg : ∀ o : U64, o.val < 223488 → wa (L.ksb s) o ≠ b) : ¬ DomW (L.ksb s) b := by + intro hd + obtain ⟨o, ho, eo⟩ := hd.wa_exists J.sr.ksFit + exact hg o (by omega) eo + +/-- A word inside a typed slot is off `DomW`. -/ +theorem JStage3A.slot_domA {y b : U64} (hy : IsSlotA Z.th y) (y1 : y.val ≤ b.val) (y2 : b.val < y.val + 32) : + ¬ DomW (L.ksb s) b := by + intro hd + have P := J.sr.place + obtain ⟨p, k, hk, p1, p2⟩ : ∃ p k, Z.th p = some k ∧ p.val ≤ y.val ∧ y.val + 32 ≤ p.val + k.size := by + obtain ⟨p, ⟨r, hr, i, hi, rfl⟩ | ⟨ht, i, hi, rfl⟩⟩ := hy + · obtain ⟨k, hk, hs, hv⟩ := slotVA_of P hr (j := i) (by rw [cnode_size]; omega) + exact ⟨p, k, hk, by omega, by omega⟩ + · obtain ⟨k, hk, hs, hv⟩ := tcb_slotV P ht hi + have : i < 7 := hi + exact ⟨p, k, hk, by omega, by omega⟩ + have hksz : ksSizeA = 788736 := rfl + unfold DomW at hd + by_cases hn : IrqNodeA (L.ksb s) p + · have ek := P.irqK hk hn + subst ek + obtain ⟨i, hi, rfl⟩ := hn + have ev := irqSlotA_val J.sr.ksFit hi + have : ObjKindA.size (.cnode 0) = 32 := rfl + omega + · have := P.ks p k hk hn + omega + +/-- A word of a TCB of `T` is off `DomW`. -/ +theorem JStage3A.tcbK_domA {t K : U64} (ht : t ∈ Z.T) (hK : K.val < 4096) : ¬ DomW (L.ksb s) (wa t K) := by + intro hd + have hfit := (J.sr.core.tcbs t ht).fits + have hv : (wa t K).val = t.val + K.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have := J.sr.tcb_ks ht + have hksz : ksSizeA = 788736 := rfl + unfold DomW at hd + omega + +/-- An IRQ-state word is off `DomW`. -/ +theorem JStage3A.irq_domA {i : U64} (hi : i.val < 5376) : ¬ DomW (L.ksb s) (irqWordA (L.ksb s) i) := by + intro hd + have hv := irqWordA_val J.sr.ksFit hi + unfold DomW at hd + omega + +/-- `OSKeep` across a scheduler step that may advance the domain schedule. -/ +theorem JStage3A.osKeep_stepDA {s' : S} + (w : WritesIn st s s' (fun b => SchedStepWA (st := st) Z m σ s b ∨ DomW (L.ksb s) b)) : + OSKeep st OS s s' := by + intro y hy + have hfit := J.sys.ent.os.fit y hy + have al0 := J.sys.ent.os.al0 hy + have key : ∀ o : U64, o.val < 32 → o.val % 8 = 0 → L.mem s' (wa y o) = L.mem s (wa y o) := by + intro o o1 o2 + have hv : (wa y o).val = y.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have hal : Aligned (wa y o) := J.sys.ent.os.al hy o1 o2 + exact w.mem_eq hal fun h => h.elim (J.os_offStepA hal ⟨y, hy, by omega, by omega⟩) + (J.os_domA hy (by omega) (by omega)) + have e0 : wa y 0#u64 = y := by apply UScalar.eq_of_val_eq; rw [wa_valm]; simp + exact ⟨by have := key 0#u64 (by simp) (by simp); rwa [e0] at this, key 8#u64 (by simp) (by simp), + key 16#u64 (by simp) (by simp), key 24#u64 (by simp) (by simp)⟩ + +/-- (record edit B2) The current core's IPI-pair words are not written. -/ +theorem JStage3A.ipi_offStepA {o : U64} (ho : o.val = 120 ∨ o.val = 128) : + ¬ SchedStepWA (st := st) Z m σ s (L.nodeAt s (L.cpu s) o) := by + intro hw + have hksz : ksSizeA = 788736 := rfl + obtain ⟨hv, hcl⟩ := J.sr.toSchedSRN.ipi_val ho + rcases stepW_casesA J.sr.toSchedSRN hw with h | h | ⟨t, ht, t1, t2, -⟩ + · obtain ⟨c, hc, x, n', hx, ha⟩ := h + have := (J.sr.scp.out c hc x n' hx _ ha).2 + omega + · unfold SKsA at h; omega + · have := J.sr.tcb_ks ht + omega + +/-- (record edit B2) An IPI-pair word is no cap-slot word. -/ +theorem JStage3A.ipi_capwA {o : U64} (ho : o.val = 120 ∨ o.val = 128) + (h : W7.CapW H.tcbs {b | InOS OS b} ∅ (L.nodeAt s (L.cpu s) o)) : False := by + obtain ⟨Q, R', q⟩ := J.sys.ent.bxi.1 + have hal : Aligned (L.nodeAt s (L.cpu s) o) := nodeAt_aligned (J.sr.core.nodes _ J.sr.sch.cpuCs) (by omega) (by omega) + exact capw_ne_node q (os_capw J.sys.ent.os hal h) J.sr.sch.cpuCs (show o.val < 4288 by omega) rfl + +/-- (record edit B2) An IPI-pair word is no word of a heap object. -/ +theorem JStage3A.ipi_omegaA {o : U64} (ho : o.val = 120 ∨ o.val = 128) + (hΩ : L.nodeAt s (L.cpu s) o ∈ H.omega) : False := by + obtain ⟨hv, hcl⟩ := J.sr.toSchedSRN.ipi_val ho + obtain ⟨-, p, k, hp, -, hin⟩ := hΩ + exact J.heap_offNodesA hp hin.1 hin.2 (by omega) + +/-- (record edit B2) **`ipiZero` across a write off the IPI-pair words.** -/ +theorem JStage3A.ipiKeepOf {s' : S} {W : U64 → Prop} (w : WritesIn st s s' W) + (hW : ∀ o : U64, (o.val = 120 ∨ o.val = 128) → ¬ W (L.nodeAt s (L.cpu s) o)) : + Sched.SilentScheduleIpiA (st := st) s' := + J.ipiZero.keepW w.1 w.2.1 fun o ho => + w.mem_eq (nodeAt_aligned (J.sr.core.nodes _ J.sr.sch.cpuCs) (by omega) (by omega)) (hW o ho) + +/-- (record edit F4) **The `KERNEL_ROOT` word is the spec's global user VSpace** (`NdOKA.kroot`): the tie `execIdle` / +`execThread` need (`vspace::set_kernel_vm_root` reads that word, `kernelRootWritesA σ` names the spec's). -/ +theorem JStage3A.kroot : + specPtr (L.mem s (Fl4ProofsA64.P1.krA (L := L) s)) = σ.archState.armUsGlobalVspace := + J.domOK.kroot + +end off + +/-- A word at or above the ASID table is off `DomW`. -/ +theorem asid_domA {k b : U64} (h1 : k.val + 772352 ≤ b.val) : ¬ DomW k b := by + intro hd + unfold DomW at hd + omega + +/-- The footprint reads `Z`'s scheduler ghosts and the state's block base and core only. -/ +theorem footA_schedA {Z : XSetsA} {Q : U64 → U64 → List U64} {Rl : U64 → List U64} + {curOf curScOf : Fl4SpecA64.CoreId → Fl4SpecA64.ObjRef} {s s' : S} (hk : L.ksb s' = L.ksb s) + (hc : L.cpu s' = L.cpu s) {w : U64} + (h : FootA (L := L) { { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } with S1 := ∅ } s' w) : + FootA (L := L) { Z with S1 := ∅ } s w := by + have hn := fun c o => nodeAt_ksb (L := L) hk c o + cases h with + | slot y w hy hw => exact .slot y w hy hw + | nslot w h => + refine .nslot w ?_ + cases h with + | node c _ hcm ho => simp only [hn] at ho; exact .node c _ hcm ho + | cur _ ho => simp only [hc, hn] at ho; exact .cur _ ho + | tcb t _ ht ho => exact .tcb t _ ht ho + | sc c hcm => exact .sc c hcm + | vcpu v _ hv hw => exact .vcpu v _ hv hw + | irq i hi => rw [hk]; exact .irq i hi + | cur2 _ ho => simp only [hc, hn] at ho; exact .cur2 _ ho + | glob _ ho => rw [hk] at ho; exact .glob _ ho + | tcb2 t _ ht ho => exact .tcb2 t _ ht ho + | sc2 c hcm => exact .sc2 c hcm + | vs _ hb => rw [hk] at hb; exact .vs _ hb + | ext _ hb => exact .ext _ hb + +set_option maxHeartbeats 1000000 in +/-- **`JStage3A` across a scheduler step that may advance the domain schedule**: `of_schedWr` with the queue relation +without its `dom` field, `domOK` at the new state as a hypothesis, and the write set widened by the +domain-schedule words `DomW` (`next_domain`). -/ +theorem JStage3A.of_schedWrD {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J5 : JSlotA S} {Rj : Set U64 → JSlotA S} {s s' : S} + {a a' : A} {σ σ' : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7A st Rj) s a σ m) + {Q : U64 → U64 → List U64} {Rl : U64 → List U64} {curOf curScOf : Fl4SpecA64.CoreId → Fl4SpecA64.ObjRef} + (w : WritesIn st s s' (fun b => SchedStepWA (st := st) Z m σ s b ∨ DomW (L.ksb s) b)) (HS : SpecSchedA σ σ') (SSz : SSzA σ σ') (Qk : QKeepN σ σ') + (hum : σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory) (har : AR a' Z.hw) + (hcurOf : ∀ k, k ≠ m.curCore → curOf k = Z.curOf k) (bufAl : UtBufAlA Z.T σ') + (sys : SysInvA st H Z.Cs OS U Ac Pp Tb Lv ia ie s') + (sq : SchedQueuesInv st H.tcbs Z.Cs H.omega H s' Q Rl) + (poolOff : PoolOff st H Z.Cs OS s') + (sr : SchedSRA st { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } m κ s' σ') + (scCov : ScCovA Z.C m σ') + (ep : EpStateRel Z.T E Rp Bf (L.mem s') σ') + (ntfn : NtfnStateRel N Z.T (L.mem s') σ') + (reply : ReplyStackRelA (L.mem s') Rp Z.C σ' (scNameA m) stack) + (bufCover : BufCover st Z.T Bf s' σ') + (idleNoFault : IdleNoFaultA m σ') + (mcp : McpRelA Z.T m (L.mem s') σ') + (yieldRel : YieldRelA Z.T Z.C m (L.mem s') σ') + (sinv : SInvXA σ' m n) + (sidle : SIdleA σ' m n) + (runVcpu : RunVcpuInv st Z.T s') (blkQ : BlkQueuedA Z.T E N (L.mem s') σ') + (symL : SymL st H Z.Cs s') + (schedValid : SchedValidA σ') + (j5 : J5 { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } H OS s' σ' m) + (j7 : J7A st Rj { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } H OS s' σ' m) + (hdom : Sched.NdOKA (st := st) s' σ') : + JStage3A st AR { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } H OS U Ac Pp Tb Lv ia ie N E Rp + Bf stack κ n J5 (J7A st Rj) s' a' σ' m := by + have hk := w.1 + have hc := w.2.1 + have osk := J.osKeep_stepDA w + have capE : ∀ u ∈ OS, capA st s' u = capA st s u := fun u hu => by + unfold capA; rw [(osk u hu).1, (osk u hu).2.1] + have keep : ∀ b, Aligned b → ¬ SchedStepWA (st := st) Z m σ s b → ¬ DomW (L.ksb s) b → L.mem s' b = L.mem s b := + fun b hb hn hd => w.mem_eq hb fun h => h.elim hn hd + obtain ⟨Pt, Ub, vr, -, arm, -, -, -, -, link, apart, -⟩ := J.j7 + have P := J.sr.place + have hT : ∀ t ∈ Z.T, t ∈ H.tcbs := fun t ht => by rw [J.tcbs]; exact ht + -- the page words + have page : ∀ p, SchedPageA st Z OS s p → ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → ¬ AsW (L.ksb s) (wa p o) → + L.mem s' (wa p o) = L.mem s (wa p o) := fun p hp o o1 o2 na => by + have hv : VsR (L.ksb s) Pt (wa p o) := Or.inl ⟨p, link p hp, o, o1, o2, rfl⟩ + exact keep _ (vr.al _ hv) (J.vsr_offStepA vr hv na) (J.omega_domA (vr.ks _ hv)) + have vsr : ∀ b, VsR (L.ksb s) Pt b → L.mem s' b = L.mem s b := fun b hb => by + refine keep _ (vr.al _ hb) ?_ (J.omega_domA (vr.ks b hb)) + by_cases h : AsW (L.ksb s) b + · obtain ⟨o, o1, o2, -, e⟩ := h + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.sys.ent.ksw + have ev : b.val = (L.ksb s).val + o.val := by + rw [e, wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact J.asid_offStepA (by omega) (by omega) + · exact J.vsr_offStepA vr hb h + have vsfoot : ∀ b, VsFootA Z.Pp Z.S1 Z.Y (L.ksb s) b → L.mem s' b = L.mem s b := fun b hb => by + refine vsr b ?_ + rcases hb.vsR with ⟨p, hp, h⟩ | h + · refine Or.inl ⟨p, link p ?_, h⟩ + rcases hp with hp | hp | hp + · exact Or.inl hp + · exact Or.inr (Or.inl hp) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl hp)))))) + · exact Or.inr h + have word : ∀ p, SchedPageA st Z OS s p → ∀ i : U64, i.val < 512 → L.mem s' (peAF p i) = L.mem s (peAF p i) := + fun p hp i hi => by + refine vsr _ (Or.inl ⟨p, link p hp, core.num.U64.wrapping_mul i 8#u64, ?_, ?_, rfl⟩) + · rw [wm8_valA hi]; omega + · rw [wm8_valA hi]; omega + have extfoot : ∀ b, ExtFootE Z.X Z.Y Z.T2 b → L.mem s' b = L.mem s b := fun b hb => by + cases hb with + | s2lo r i hr hi => exact word r (Or.inr (Or.inr (Or.inr (Or.inl hr)))) i hi + | s2hi r i hr hi => exact word _ (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨r, hr, rfl⟩))))) i hi + | s2pt t i ht hi => exact word t (Or.inr (Or.inr (Or.inl ht))) i hi + | wl0 y hy => exact word y (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl hy)))))) _ (by simp) + | wlo y i hy hi => exact word y (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl hy)))))) i (by omega) + -- the VCPU words + have vcw : ∀ v ∈ Z.V, ∀ o : U64, o.val = 0 ∨ o.val = 24 → L.mem s' (wa v o) = L.mem s (wa v o) := by + intro v hv o ho + have hH := arm.vh v hv + have hfit := J.sys.ent.heap.fits v _ hH + have hal := J.sys.ent.heap.align v _ hH + simp only [HKind.bits] at hfit hal + have ev : (wa v o).val = v.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have al : Aligned (wa v o) := by show _ % 8 = 0; rw [ev]; omega + have inObj : InObj v (.other 12) (wa v o) := by + unfold InObj; simp only [HKind.bits]; omega + have hg : ∀ g : U64, g.val < 223488 → wa (L.ksb s) g ≠ wa v o := fun g hg e => + apart _ ⟨v, hv, o, by omega, by omega, rfl⟩ g hg e + refine keep _ al (J.omega_offStepA ⟨al, v, _, hH, Or.inr (Or.inr (Or.inl ⟨12, rfl⟩)), inObj⟩ ?_ hg) + (J.omega_domA hg) + rintro c ⟨z, hz⟩ ⟨c1, c2⟩ + have e : H.scBits c = z := by unfold Heap.scBits; rw [hz] + have ne : v ≠ c := fun h => by rw [h, hz] at hH; cases hH + have := J.sys.ent.heap.disj v _ c _ hH hz ne + simp only [HKind.bits] at this + rw [e] at c2 + omega + have vcwAll : ∀ v ∈ Z.V, ∀ o : U64, o.val < 4096 ∧ o.val % 8 = 0 → L.mem s' (wa v o) = L.mem s (wa v o) := by + intro v hv o ⟨ho1, ho2⟩ + have hH := arm.vh v hv + have hfit := J.sys.ent.heap.fits v _ hH + have hal := J.sys.ent.heap.align v _ hH + simp only [HKind.bits] at hfit hal + have ev : (wa v o).val = v.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have al : Aligned (wa v o) := by show _ % 8 = 0; rw [ev]; omega + have inObj : InObj v (.other 12) (wa v o) := by + unfold InObj; simp only [HKind.bits]; omega + have hg : ∀ g : U64, g.val < 223488 → wa (L.ksb s) g ≠ wa v o := fun g hg e => + apart _ ⟨v, hv, o, by omega, by omega, rfl⟩ g hg e + refine keep _ al (J.omega_offStepA ⟨al, v, _, hH, Or.inr (Or.inr (Or.inl ⟨12, rfl⟩)), inObj⟩ ?_ hg) + (J.omega_domA hg) + rintro c ⟨z, hz⟩ ⟨c1, c2⟩ + have e : H.scBits c = z := by unfold Heap.scBits; rw [hz] + have ne : v ≠ c := fun h => by rw [h, hz] at hH; cases hH + have := J.sys.ent.heap.disj v _ c _ hH hz ne + simp only [HKind.bits] at this + rw [e] at c2 + omega + have slotw : ∀ y, IsSlotA Z.th y → ∀ x, (x = y ∨ x = wa y 8#u64 ∨ x = wa y 16#u64 ∨ x = wa y 24#u64) → + L.mem s' x = L.mem s x := by + intro y hy x hx + obtain ⟨a32, f32⟩ := hy.al32 P + have hv : ∀ o : U64, o.val < 32 → (wa y o).val = y.val + o.val := fun o ho => by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have key : x.val % 8 = 0 ∧ y.val ≤ x.val ∧ x.val < y.val + 32 := by + rcases hx with rfl | rfl | rfl | rfl + · omega + all_goals + rw [hv _ (by simp)]; simp; omega + exact keep _ key.1 (J.slot_offStepA hy key.2.1 key.2.2) (J.slot_domA hy key.2.1 key.2.2) + have cnw : ∀ y, SlotWA Z.th y → IsSlotA Z.th y := fun y hy => by + rcases hy with h | h + · exact h + · obtain ⟨p, r, hp, idx, hl, rfl⟩ := h + obtain ⟨e, hb, -⟩ := cn_slotV P hp hl + exact ⟨p, Or.inl ⟨r, hp, blvA idx, hb, e⟩⟩ + have tcbl : ∀ t ∈ Z.T, ∀ K : U64, K = 3664#u64 ∨ K = 3672#u64 → L.mem s' (wa t K) = L.mem s (wa t K) := by + intro t ht K hK + have hta := J.sr.core.tcbs t ht + refine keep _ (field_aligned (K := K) hta ?_ ?_) (J.link_offStepA ht hK) + (J.tcbK_domA ht (by rcases hK with rfl | rfl <;> simp)) + all_goals rcases hK with rfl | rfl <;> simp + have mem : SchedKeepMemA (st := st) Z s s' := + { ksb := hk + cpu := hc + slot := fun y hy x hx => slotw y (cnw y hy) x hx + tcbW := fun t ht => ⟨tcbl t ht _ (Or.inl rfl), tcbl t ht _ (Or.inr rfl)⟩ + vcW := fun v hv => by + have e0 : wa v 0#u64 = v := by apply UScalar.eq_of_val_eq; rw [wa_valm]; simp + have h0 := vcw v hv 0#u64 (Or.inl (by simp)) + rw [e0] at h0 + exact ⟨h0, vcw v hv _ (Or.inr (by simp))⟩ + irqW := fun i hi => by + refine keep _ ?_ (J.irq_offStepA hi) (J.irq_domA hi) + have := irqWordA_val J.sr.ksFit hi + have := J.sr.toSchedSRN.ksb_al + show _ % 8 = 0 + omega + vs := vsfoot + ext := extfoot } + -- the node cursors + have nodes : ∀ c ∈ Z.Cs, + L.mem s' (L.nodeAt s' c layout.node.IDLE_THREAD) ∈ Z.T ∧ L.mem s' (L.nodeAt s' c layout.node.IDLE_SC) ∈ Z.C ∧ + L.mem s' (L.nodeAt s' c layout.node.CUR_THREAD) ∈ Z.T := by + intro c hcs + refine ⟨?_, ?_, ?_⟩ + · have e : L.mem s' (L.nodeAt s' c layout.node.IDLE_THREAD) = L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) := + specPtr_inj ((sr.sch.idleOf c hcs).symm.trans (J.sr.sch.idleOf c hcs)) + rw [e]; exact J.j.1.idleT c hcs + · have e : L.mem s' (L.nodeAt s' c layout.node.IDLE_SC) = L.mem s (L.nodeAt s c layout.node.IDLE_SC) := + specPtr_inj ((sr.sch.idleScOf c hcs).symm.trans (J.sr.sch.idleScOf c hcs)) + rw [e]; exact J.j.1.idleC c hcs + · by_cases h : c = L.cpu s + · have := sr.curT + unfold LawfulStore.curW at this + rw [hc, ← h] at this + exact this + · have hne : c.val ≠ m.curCore := fun e => h (UScalar.eq_of_val_eq (e.trans J.sr.sch.core)) + have h1 := sr.sch.curOfW c hcs + rw [show ({ Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } : XSetsA).curOf = curOf from rfl, + hcurOf _ hne] at h1 + have e : L.mem s' (L.nodeAt s' c layout.node.CUR_THREAD) = L.mem s (L.nodeAt s c layout.node.CUR_THREAD) := + specPtr_inj (h1.symm.trans (J.sr.sch.curOfW c hcs)) + rw [e]; exact J.j.1.curT c hcs + have st' : StEqA Z.T (L.mem s') σ' := fun _ ht _ hx => (ep.at_tcb ht hx).st + have jx := JAtZAX.of_sched J.j (a' := a') (Q := Q) (R := Rl) (curOf := curOf) (curScOf := curScOf) mem HS har + (sr.sch.withSt st') sr.rest (fun c h => (nodes c h).1) (fun c h => (nodes c h).2.1) + (fun c h => (nodes c h).2.2) sr.cscAll + have free : ∀ dev x, UtFreeA st OS s' dev x → UtFreeA st OS s dev x := by + rintro dev x ⟨u, hu, q, b, fi, d, hcap, h⟩ + exact ⟨u, hu, q, b, fi, d, (capE u hu).symm.trans hcap, h⟩ + have dead : ∀ k p, DeadA st OS k s' p → DeadA st OS k s p := fun k p h u hu hp => by + have := h u hu + rw [capE u hu] at this + exact this hp + have nsc : ∀ {p : Fl4SpecA64.ObjRef}, σ.kheap p = none → σ'.kheap p = none := fun {p} h => by + rcases HS.kh p with e | ⟨x, _, e1, -⟩ | ⟨o, o', e1, -, -⟩ + · rw [e]; exact h + · rw [h] at e1; cases e1 + · rw [h] at e1; cases e1 + have keepE : ∀ {q : Fl4SpecA64.ObjRef} {e : Fl4SpecA64.ExtArchObj Fl4SpecA64.PTE}, + σ.kheap q = some (.archObj (.ext e)) → σ'.kheap q = some (.archObj (.ext e)) := fun h1 => + HS.keep h1 (fun _ e => by cases e) (fun e => by rcases e with ⟨_, _, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ <;> cases e) + have unmS2 : ∀ u ∈ OS, ∀ p a v, capA st s u = .Ept p 0#u64 false a v → UnmS2RootA st s σ p → + UnmS2RootA st s' σ' p := by + intro u hu p a v hc U + obtain ⟨es, h1, h2⟩ := U.spec + refine { inv := fun i hi => ?_, spec := ⟨es, keepE h1, h2⟩ } + by_cases hlo : i.val < 512 + · rw [word p (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inr (Or.inl ⟨a, v, hc⟩))⟩))))))) i hlo] + exact U.inv i hi + · obtain ⟨j, hj, e⟩ := peAF_hiA p i (by omega) hi + rw [e, word _ (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inr (Or.inr (Or.inl ⟨p, a, v, hc, rfl⟩)))⟩))))))) j hj, ← e] + exact U.inv i hi + have unmHv : ∀ u ∈ OS, ∀ y a, capA st s u = .HighVSpace y false a → UnmHiRootA st s σ y → + UnmHiRootA st s' σ' y := by + intro u hu y a hc U + obtain ⟨l1, h1, h2⟩ := U.spec + exact { inv := fun i hi => by + rw [word _ (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inr (Or.inr (Or.inr ⟨y, a, hc, rfl⟩)))⟩))))))) i hi] + exact U.inv i hi + spec := ⟨l1, keepE h1, h2⟩ } + have unm : ∀ ty p, SchedPageA st Z OS s p → UnmTabA st Z m s σ ty p → + UnmTabA st { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } m s' σ' ty p := by + intro ty p hp U1 + obtain ⟨tb, h1, h2, h3⟩ := U1.spec + exact + { inv := fun i hi => by rw [word p hp i hi]; exact U1.inv i hi + spec := ⟨tb, HS.keep h1 (fun _ e => by cases e) (fun e => by rcases e with ⟨_, _, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ <;> cases e), h2, h3⟩ + off := ⟨fun i hi h => U1.off.mem i hi (footA_schedA hk hc h), U1.off.obj⟩ } + exact + { sys := sys + j := jx + tcbs := J.tcbs + scs := J.scs + sq := sq + ap := by + have e : L.ksb s' = L.ksb s := hk + rw [e]; exact { J.ap with } + pp := J.pp + s1 := J.s1 + ko := by rw [hk]; exact J.ko + ptH := J.ptH + tabInj := by rw [HS.asid]; exact J.tabInj + poolOff := poolOff + objAlloc := ObjAllocA.keep (Z := Z) J.objAlloc osk + idleAnchor := IdleAnchorOffA.keep J.idleAnchor osk + vsVc := by rw [hk]; exact J.vsVc + sr := sr + scCov := scCov + scSize := fun c hcm x k h => by + obtain ⟨y, hy⟩ := SSz _ x k h + exact J.scSize c hcm y k hy + ep := ep + ntfn := ntfn + reply := reply + bufCover := bufCover + bufNative := fun t ht => by + have hsl : IsSlotA Z.th (slotAddrA (specPtr t) (BitVec.ofNat 64 2)) := + ⟨t, Or.inr ⟨(J.sr.thT t).2 ht, 2, by unfold tcbSlotsA; omega, rfl⟩⟩ + have e : capAtA (L.mem s') (slotAddrA (specPtr t) (BitVec.ofNat 64 2)) = + capAtA (L.mem s) (slotAddrA (specPtr t) (BitVec.ofNat 64 2)) := by + unfold capAtA + rw [slotw _ hsl _ (Or.inl rfl), slotw _ hsl _ (Or.inr (Or.inl rfl))] + rw [e]; exact J.bufNative t ht + iplaced := J.iplaced + ipcPlace := J.ipcPlace + idleNoFault := idleNoFault + mcp := mcp + yieldRel := yieldRel + sinv := sinv + setsInH := { J.setsInH with } + utIpc := J.utIpc + ipcTh := J.ipcTh + deadExt := + { vcpu := fun v hv hd x hx => J.deadExt.vcpu v hv (dead _ v hd) x + (HS.back hx (fun _ e => by cases e) (fun e => by rcases e with ⟨_, _, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ <;> cases e)) + reply := fun r hr hd => J.deadExt.reply r hr (dead _ r hd) + asid := fun i p h hd => J.deadExt.asid i p (by rw [HS.asid] at h; exact h) (dead _ p hd) + tvcpu := fun t ht hd => by + rw [tcbl t ht 3664#u64 (Or.inl rfl)] + exact J.deadExt.tvcpu t ht (dead _ t hd) + vcDeadRun := fun v hv hd => by + rw [vcw v hv 24#u64 (Or.inr (by simp))] + exact J.deadExt.vcDeadRun v hv (dead _ v hd) + replyTcb := fun r hr hd x hx => by + obtain ⟨y, hy, k⟩ := Qk.rt _ x hx + exact k (J.deadExt.replyTcb r hr (dead _ r hd) y hy) } + specFree := + { heap := fun p h => nsc (J.specFree.heap p (free _ _ h)) + cdt := fun x y h p hf => J.specFree.cdt x y (by rw [HS.cdt] at h; exact h) p (free _ _ hf) + orig := fun x h p hf => J.specFree.orig x (by rw [HS.orig] at h; exact h) p (free _ _ hf) } + utBlk := fun u hu q b fi d h => J.utBlk u hu q b fi d ((capE u hu).symm.trans h) + unmPt := + { pt := fun u hu p asid va h => by + have h0 := (capE u hu).symm.trans h + exact unm _ p (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inl ⟨_, _, _, h0⟩⟩))))))) (J.unmPt.pt u hu p asid va h0) + vs := fun u hu p asid h => by + have h0 := (capE u hu).symm.trans h + exact unm _ p (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inl ⟨_, _, h0⟩)⟩))))))) (J.unmPt.vs u hu p asid h0) + s2 := fun u hu p asid va h => by + have h0 := (capE u hu).symm.trans h + exact unmS2 u hu p asid va h0 (J.unmPt.s2 u hu p asid va h0) + hv := fun u hu p asid h => by + have h0 := (capE u hu).symm.trans h + exact unmHv u hu p asid h0 (J.unmPt.hv u hu p asid h0) } + tabS1 := fun q hq => + ⟨fun i hi h => (J.tabS1 q hq).mem i hi (footA_schedA hk hc h), (J.tabS1 q hq).obj⟩ + tabExt := + ⟨fun q hq i hi h => J.tabExt.mem q hq i hi (by + exact footA_schedA (Z := extZA Z) hk hc h), J.tabExt.obj⟩ + specZero := fun x h => by rw [hum]; exact J.specZero x (free _ _ h) + origLive := fun x h => J.origLive x (by rw [HS.orig] at h; exact h) + nullOrig := fun sl h => by + rw [HS.orig] + exact J.nullOrig sl (by rw [← SpecSchedA.specCapAt HS sl]; exact h) + thPos := fun p z h => J.thPos p z h + frBase := J.frBase.keepA capE + devFr := J.devFr.keepA capE fun p dev pg h => HS.keep h (fun _ e => by cases e) + (fun e => by rcases e with ⟨_, _, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ <;> cases e) + finCov := J.finCov.keepA + (fun q r cs h => HS.back h (fun _ e => by cases e) + (fun e => by rcases e with ⟨_, _, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ <;> cases e)) + (fun q x h => HS.tcbBack h) + idleAll := J.idleAll.keepA + (fun c hcs => specPtr_inj ((sr.sch.idleOf c hcs).symm.trans (J.sr.sch.idleOf c hcs))) + (fun c hcs => specPtr_inj ((sr.sch.idleScOf c hcs).symm.trans (J.sr.sch.idleScOf c hcs))) + ipcHeapCov := J.ipcHeapCov + zombieNoVcpu := J.zombieNoVcpu.keepA capE J.zombieTcb + fun t ht => tcbl t (J.tcbs ▸ ht) 3664#u64 (Or.inl rfl) + zombieTcb := J.zombieTcb.keepA capE + bufOff := J.bufOff.keepA (Z := Z) (Z' := { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf }) + hk rfl rfl rfl rfl rfl rfl rfl capE + vsOk := by + have e := (utVsPt_keepA J.sys.ent.vs hk (fun b hb => by + refine keep b (J.vsOk.al b hb) ?_ (J.omega_domA (J.vsOk.ks b hb)) + by_cases h : AsW (L.ksb s) b + · obtain ⟨o, o1, o2, -, e⟩ := h + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.sys.ent.ksw + have ev : b.val = (L.ksb s).val + o.val := by + rw [e, wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact J.asid_offStepA (by omega) (by omega) + · exact J.vsr_offStepA J.vsOk hb h)).2 + unfold UtVsOkA + rw [show L.ksb s' = L.ksb s from hk, e] + exact J.vsOk + domOK := hdom + ipiZero := J.ipiKeepOf w fun o ho h => h.elim (J.ipi_offStepA ho) fun hd => by + obtain ⟨hv, -⟩ := J.sr.toSchedSRN.ipi_val ho + unfold DomW at hd + omega + bufVs := by + have e := (utVsPt_keepA J.sys.ent.vs hk (fun b hb => by + refine keep b (J.vsOk.al b hb) ?_ (J.omega_domA (J.vsOk.ks b hb)) + by_cases h : AsW (L.ksb s) b + · obtain ⟨o, o1, o2, -, e⟩ := h + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.sys.ent.ksw + have ev : b.val = (L.ksb s).val + o.val := by + rw [e, wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact J.asid_offStepA (by omega) (by omega) + · exact J.vsr_offStepA J.vsOk hb h)).2 + rw [show L.ksb s' = L.ksb s from hk, e] + exact J.bufVs + bufAl := bufAl + zombieBnd := J.zombieBnd.keepA capE + devUtOff := J.devUtOff.keepA (Z := Z) capE hk (fun _ _ h => h) rfl + sidle := sidle + capTy := J.capTy.keepA capE + scCpu := J.scCpu + scNZ := J.scNZ + vcRun := J.vcRun.keepA (fun t ht => tcbl t ht 3664#u64 (Or.inl rfl)) (fun v hv => vcw v hv 24#u64 (Or.inr (by simp))) + (fun v hv => by + have h0 := vcw v hv 0#u64 (Or.inl (by simp)) + rwa [wa_zeroVcA] at h0) + (fun t ht => tcbl t ht 3672#u64 (Or.inr rfl)) + vcSym1 := J.vcSym1.keepA J.vcRun (fun t ht => tcbl t ht 3664#u64 (Or.inl rfl)) (fun v hv => by + have h0 := vcw v hv 0#u64 (Or.inl (by simp)) + rwa [wa_zeroVcA] at h0) + runVcpu := runVcpu + vcSym2 := J.vcSym2.keepA J.vcRun (fun v hv => vcw v hv 24#u64 (Or.inr (by simp))) + (fun t ht => tcbl t ht 3672#u64 (Or.inr rfl)) + blkQ := blkQ + heapWin := J.heapWin + coresAll := J.coresAll + ptCapIn := PtCapInA.of_capsA J.ptCapIn rfl (fun _ h => h) (fun _ h => h) + (fun y hy => Or.inl (by + unfold capAtA; rw [slotw y hy _ (Or.inl rfl), slotw y hy _ (Or.inr (Or.inl rfl))])) + symL := symL + fhOK := fun t ht => by + have hy := isSlot_fhA ((J.sr.thT t).2 ht) + have e : capA st s' (slotIdx t layout.tcb.SLOT_FAULT_HANDLER) = + capA st s (slotIdx t layout.tcb.SLOT_FAULT_HANDLER) := by + unfold capA; rw [slotw _ hy _ (Or.inl rfl), slotw _ hy _ (Or.inr (Or.inl rfl))] + rw [e]; exact J.fhOK t ht + zwf := DelInvA.keepW J.zwf fun y hy => by + unfold capA; rw [slotw y hy _ (Or.inl rfl), slotw y hy _ (Or.inr (Or.inl rfl))] + schedValid := schedValid + utWin := J.utWin.keepA capE + devWin := J.devWin.keepA capE + vcBack := vcBack_keepA J.vcRun.vtcb (fun v hv => by + have h0 := vcw v hv 0#u64 (Or.inl (by simp)) + rwa [wa_zeroVcA] at h0) (fun t ht => tcbl t ht 3664#u64 (Or.inl rfl)) J.vcBack + runBack := runBack_keepA J.vcRun.run (fun t ht => tcbl t ht 3672#u64 (Or.inr rfl)) + (fun v hv => vcw v hv 24#u64 (Or.inr (by simp))) J.runBack + vcSnap := J.vcSnap.keepA (fun v hv o h1 h2 => vcwAll v hv o ⟨h1, h2⟩) + (fun v _ x hx => HS.back hx (fun _ e => by cases e) + (fun e => by rcases e with ⟨_, _, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ <;> cases e)) + vcOS := J.vcOS + userFr := J.userFr.keepA capE fun p dev pg h => HS.keep h (fun _ e => by cases e) + (fun e => by rcases e with ⟨_, _, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ <;> cases e) + frTh := J.frTh.keepA capE fun _ _ e => e + bfSpec := J.bfSpec + curVcpuNone := HS.vcur.trans J.curVcpuNone + ipcSorted := J.ipcSorted.of_sub Qk.prio Qk.ep Qk.ntfn + activeReplyScs := J.activeReplyScs.of_keep Qk.sc + j5 := j5 + j7 := j7 } + +set_option maxHeartbeats 1000000 in +/-- **`JStage3A` across a scheduler step** (module docstring). -/ +theorem JStage3A.of_schedWr {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J5 : JSlotA S} {Rj : Set U64 → JSlotA S} {s s' : S} + {a a' : A} {σ σ' : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7A st Rj) s a σ m) + {Q : U64 → U64 → List U64} {Rl : U64 → List U64} {curOf curScOf : Fl4SpecA64.CoreId → Fl4SpecA64.ObjRef} + (w : WritesIn st s s' (SchedStepWA (st := st) Z m σ s)) (HS : SpecSchedA σ σ') (SSz : SSzA σ σ') (Qk : QKeepA σ σ') + (hum : σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory) (har : AR a' Z.hw) + (hcurOf : ∀ k, k ≠ m.curCore → curOf k = Z.curOf k) (bufAl : UtBufAlA Z.T σ') + (sys : SysInvA st H Z.Cs OS U Ac Pp Tb Lv ia ie s') + (sq : SchedQueuesInv st H.tcbs Z.Cs H.omega H s' Q Rl) + (poolOff : PoolOff st H Z.Cs OS s') + (sr : SchedSRA st { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } m κ s' σ') + (scCov : ScCovA Z.C m σ') + (ep : EpStateRel Z.T E Rp Bf (L.mem s') σ') + (ntfn : NtfnStateRel N Z.T (L.mem s') σ') + (reply : ReplyStackRelA (L.mem s') Rp Z.C σ' (scNameA m) stack) + (bufCover : BufCover st Z.T Bf s' σ') + (idleNoFault : IdleNoFaultA m σ') + (mcp : McpRelA Z.T m (L.mem s') σ') + (yieldRel : YieldRelA Z.T Z.C m (L.mem s') σ') + (sinv : SInvXA σ' m n) + (sidle : SIdleA σ' m n) + (runVcpu : RunVcpuInv st Z.T s') (blkQ : BlkQueuedA Z.T E N (L.mem s') σ') + (symL : SymL st H Z.Cs s') + (schedValid : SchedValidA σ') + (j5 : J5 { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } H OS s' σ' m) + (j7 : J7A st Rj { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } H OS s' σ' m) : + JStage3A st AR { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } H OS U Ac Pp Tb Lv ia ie N E Rp + Bf stack κ n J5 (J7A st Rj) s' a' σ' m := + JStage3A.of_schedWrD J (w.mono fun _ h => Or.inl h) HS SSz Qk.toN hum har hcurOf bufAl sys sq poolOff sr scCov ep ntfn reply bufCover idleNoFault + mcp yieldRel sinv sidle runVcpu blkQ symL schedValid j5 j7 + (J.domOK.keepS J.sr.toSchedSRN Qk.dom w.1 w.2.1 (fun b hb hd => w.mem_eq hb (J.dom_offStepA hd))) + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3SchedWrNA.lean b/proofs/Fl4ProofsA64/P3/Stage3SchedWrNA.lean new file mode 100644 index 000000000..953a01837 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SchedWrNA.lean @@ -0,0 +1,822 @@ +/- +P3 (aarch64, A64-P3 Stage-3 integration; Architect ruling 01:27Z, widening): **`JStage3A` across a scheduler step that also +writes IPC objects** (the timer-fire / IRQ-arm / timeout-send frames). + +* **`IpcObjWA N E Rp`**: the words of the notifications, endpoints and replies of the IPC ghost sets (64 / 16 / 32 bytes). + **`SchedStepNWA`** = `SchedStepWA` ∪ `IpcObjWA`. +* `JStage3A.ipc_*A`: an IPC object word misses the kernel-state block (`PlaceA.ks`), TCB and typed-slot words + (`PlaceA.apart`), every non-IPC heap object (`HeapOK.disj`, with `UtIpcHeapA`) and `H.omega ∖ H.frame` (the CSpace / + VSpace / table / VCPU words). +* **`XOffA`** / **`JStage3A.of_schedWrX`** (D8/T1, F0u): the same step with extra written words off the record's + footprint and user memory kept on the free untyped only; `of_schedWrN` is its instance. +* **`JStage3A.of_schedWrN`**: the sibling of `of_schedWr` (statement unchanged) whose write set is `SchedStepNWA`, the + spec step `SpecSchedA` (notification / endpoint / reply objects may change, by kind) and the IPC relations supplied by + the caller as before. + +No new premise. +-/ +import Fl4ProofsA64.P3.Stage3SchedWrA +import Fl4ProofsA64.P3.Stage3SortInsA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-- The words of the IPC objects (notifications 64, endpoints 16, replies 32 bytes). -/ +def IpcObjWA (N E Rp : Set U64) (b : U64) : Prop := + (∃ n ∈ N, n.val ≤ b.val ∧ b.val < n.val + 64) ∨ (∃ e ∈ E, e.val ≤ b.val ∧ b.val < e.val + 16) ∨ + ∃ r ∈ Rp, r.val ≤ b.val ∧ b.val < r.val + 32 + +/-- **The write set of a scheduler step that may write IPC objects.** -/ +def SchedStepNWA (N E Rp : Set U64) (Z : XSetsA) (m : Fl4SpecA64.McState) (σ : Fl4SpecA64.State Unit) (s : S) + (b : U64) : Prop := + SchedStepWA (st := st) Z m σ s b ∨ IpcObjWA N E Rp b + +section off +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 : JSlotA S} {R : Set U64 → JSlotA S} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7A st R) s a σ m) +include J + +/-- An IPC word lies in an endpoint, notification or reply heap object. -/ +theorem JStage3A.ipc_hobjA {b : U64} (h : IpcObjWA N E Rp b) : + ∃ p k, H p = some k ∧ (k = .ep ∨ k = .ntfn ∨ k = .reply) ∧ InObj p k b := by + rcases h with ⟨n, hn, h1, h2⟩ | ⟨e, he, h1, h2⟩ | ⟨r, hr, h1, h2⟩ + · exact ⟨n, .ntfn, J.utIpc.ntfn n hn, Or.inr (Or.inl rfl), h1, by simpa [HKind.bits] using h2⟩ + · exact ⟨e, .ep, J.utIpc.ep e he, Or.inl rfl, h1, by simpa [HKind.bits] using h2⟩ + · exact ⟨r, .reply, J.utIpc.reply r hr, Or.inr (Or.inr rfl), h1, by simpa [HKind.bits] using h2⟩ + +/-- An IPC word lies in a typed-heap `other` object of the IPC placement. -/ +theorem JStage3A.ipc_thA {b : U64} (h : IpcObjWA N E Rp b) : + ∃ p len, Z.th p = some (.other len) ∧ p.val ≤ b.val ∧ b.val < p.val + len := by + rcases h with ⟨n, hn, h1, h2⟩ | ⟨e, he, h1, h2⟩ | ⟨r, hr, h1, h2⟩ + · exact ⟨n, 64, J.ipcPlace.notification n hn, h1, h2⟩ + · exact ⟨e, 16, J.ipcPlace.endpoint e he, h1, h2⟩ + · exact ⟨r, 32, J.ipcPlace.reply r hr, h1, h2⟩ + +/-- An IPC word is off the kernel-state block. -/ +theorem JStage3A.ipc_ksA {b : U64} (h : IpcObjWA N E Rp b) : + b.val < (L.ksb s).val ∨ (L.ksb s).val + ksSizeA ≤ b.val := by + obtain ⟨e, len, he, e1, e2⟩ := J.ipc_thA h + have P := J.sr.place + have := P.ks e (.other len) he (fun hn => by have := P.irqK he hn; cases this) + have hsz : ObjKindA.size (.other len) = len := rfl + omega + +/-- An IPC word is in no heap object of a non-IPC kind. -/ +theorem JStage3A.ipc_off_hobjA {v : U64} {kv : HKind} (hv : H v = some kv) + (hkv : ¬ (kv = .ep ∨ kv = .ntfn ∨ kv = .reply)) {b : U64} (hin : InObj v kv b) : + ¬ IpcObjWA N E Rp b := by + intro h + obtain ⟨p, k, hp, hk, hin2⟩ := J.ipc_hobjA h + by_cases e : p = v + · subst e; rw [hv] at hp; cases hp; exact hkv hk + · have := J.sys.ent.heap.disj p k v kv hp hv e + unfold InObj at hin hin2 + omega + +/-- An IPC word is not in `H.omega ∖ H.frame` (the CSpace, VSpace, table and VCPU words). -/ +theorem JStage3A.ipc_omegaA {b : U64} (hΩ : b ∈ H.omega) (hF : b ∉ H.frame) : ¬ IpcObjWA N E Rp b := by + intro h + obtain ⟨p, k, hp, hk, hin⟩ := J.ipc_hobjA h + obtain ⟨hal, q, kq, hq, hkq, hinq⟩ := hΩ + by_cases e : q = p + · subst e; rw [hp] at hq; cases hq + rcases hk with rfl | rfl | rfl + · rcases hkq with hh | ⟨z, hh⟩ | ⟨z, hh⟩ | ⟨hh, -⟩ <;> cases hh + · rcases hkq with hh | ⟨z, hh⟩ | ⟨z, hh⟩ | ⟨-, hw⟩ <;> try cases hh + exact hF ⟨hal, q, .ntfn, hp, Or.inr (Or.inr ⟨rfl, hw⟩), hin⟩ + · exact hF ⟨hal, q, .reply, hp, Or.inl rfl, hin⟩ + · have := J.sys.ent.heap.disj p k q kq hp hq (Ne.symm e) + unfold InObj at hin hinq + omega + +/-- An IPC word is not a word of a typed slot. -/ +theorem JStage3A.ipc_slot_offA {y b : U64} (hy : IsSlotA Z.th y) (y1 : y.val ≤ b.val) (y2 : b.val < y.val + 32) : + ¬ IpcObjWA N E Rp b := by + intro hw + have P := J.sr.place + obtain ⟨e, len, he, e1, e2⟩ := J.ipc_thA hw + obtain ⟨p, k, hk, p1, p2, hkind⟩ : ∃ p k, Z.th p = some k ∧ p.val ≤ y.val ∧ y.val + 32 ≤ p.val + k.size ∧ + k ≠ .other len := by + obtain ⟨p, ⟨r, hr, i, hi, rfl⟩ | ⟨ht, i, hi, rfl⟩⟩ := hy + · obtain ⟨k, hk, hs, hv⟩ := slotVA_of P hr (j := i) (by rw [cnode_size]; omega) + exact ⟨p, k, hk, by omega, by omega, fun e' => by rw [e', hr] at hk; cases hk⟩ + · obtain ⟨k, hk, hs, hv⟩ := tcb_slotV P ht hi + have : i < 7 := hi + exact ⟨p, k, hk, by omega, by omega, fun e' => by rw [e', ht] at hk; cases hk⟩ + have hne : p ≠ e := fun h => hkind (by subst h; exact Option.some.inj (hk.symm.trans he)) + have := P.apart p e k (.other len) hk he hne + have hsz : ObjKindA.size (.other len) = len := rfl + omega + +/-- **The VSpace, Stage-2 and unmapped-table words are not written** (widened write set). -/ +theorem JStage3A.vsr_offStepNA {Pt Ub : Set U64} + (vr : VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt) {b : U64} (h : VsR (L.ksb s) Pt b) + (hna : ¬ AsW (L.ksb s) b) : ¬ SchedStepNWA (st := st) N E Rp Z m σ s b := + fun hw => hw.elim (J.vsr_offStepA vr h hna) (J.ipc_omegaA (vr.om b h) (vr.sg b h)) + +/-- **An `OS` slot word is not written** (widened write set). -/ +theorem JStage3A.os_offStepNA {b : U64} (hb : Aligned b) (h : InOS OS b) : + ¬ SchedStepNWA (st := st) N E Rp Z m σ s b := by + intro hw + rcases hw with hw | hw + · exact J.os_offStepA hb h hw + · obtain ⟨Q, R', q⟩ := J.sys.ent.bxi.1 + have cap : W7.CapW H.tcbs H.omega H.frame b := inOS_capw J.sys.ent.os hb h + rcases cap with ⟨t, ht, K, hK, e⟩ | ⟨hΩ, hF⟩ + · have ev : b.val = t.val + K.val := by rw [e]; exact field_val (q.tcbs t ht) (K := K) (by omega) + exact J.ipc_off_hobjA (v := t) (kv := .tcb) ht (by simp) + ⟨by omega, by simp only [HKind.bits]; omega⟩ hw + · exact J.ipc_omegaA hΩ hF hw + +/-- **A word inside a typed slot is not written** (widened write set). -/ +theorem JStage3A.slot_offStepNA {y b : U64} (hy : IsSlotA Z.th y) (y1 : y.val ≤ b.val) (y2 : b.val < y.val + 32) : + ¬ SchedStepNWA (st := st) N E Rp Z m σ s b := + fun hw => hw.elim (J.slot_offStepA hy y1 y2) (J.ipc_slot_offA hy y1 y2) + +/-- **A word of a TCB at a VCPU link is not written** (widened write set). -/ +theorem JStage3A.link_offStepNA {t K : U64} (ht : t ∈ Z.T) (hK : K = 3664#u64 ∨ K = 3672#u64) : + ¬ SchedStepNWA (st := st) N E Rp Z m σ s (wa t K) := by + intro hw + rcases hw with hw | hw + · exact J.link_offStepA ht hK hw + · have hfit := (J.sr.core.tcbs t ht).fits + have hKv : K.val = 3664 ∨ K.val = 3672 := by rcases hK with rfl | rfl <;> simp + have hv : (wa t K).val = t.val + K.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have htH : H t = some .tcb := by + have : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + exact this + exact J.ipc_off_hobjA (v := t) (kv := .tcb) htH (by simp) ⟨by omega, by simp only [HKind.bits]; omega⟩ hw + +/-- **An IRQ-state word is not written** (widened write set). -/ +theorem JStage3A.irq_offStepNA {i : U64} (hi : i.val < 5376) : + ¬ SchedStepNWA (st := st) N E Rp Z m σ s (irqWordA (L.ksb s) i) := by + intro hw + rcases hw with hw | hw + · exact J.irq_offStepA hi hw + · have hv := irqWordA_val J.sr.ksFit hi + have hksz : ksSizeA = 788736 := rfl + have := J.ipc_ksA hw + omega + +/-- **An ASID-table word is not written** (widened write set). -/ +theorem JStage3A.asid_offStepNA {b : U64} (h1 : (L.ksb s).val + 772352 ≤ b.val) + (h2 : b.val < (L.ksb s).val + 773376) : ¬ SchedStepNWA (st := st) N E Rp Z m σ s b := by + intro hw + rcases hw with hw | hw + · exact J.asid_offStepA h1 h2 hw + · have hksz : ksSizeA = 788736 := rfl + have := J.ipc_ksA hw + omega + +/-- (record edit #8b-B1) A domain-schedule word is not written (widened write set). -/ +theorem JStage3A.dom_offStepNA {b : U64} (hd : DomW (L.ksb s) b) : + ¬ SchedStepNWA (st := st) N E Rp Z m σ s b := by + intro hw + rcases hw with hw | hw + · exact J.dom_offStepA hd hw + · have hksz : ksSizeA = 788736 := rfl + have := J.ipc_ksA hw + unfold DomW at hd + omega + +/-- (record edit B2) The current core's IPI-pair words are not written (widened write set). -/ +theorem JStage3A.ipi_offStepNA {o : U64} (ho : o.val = 120 ∨ o.val = 128) : + ¬ SchedStepNWA (st := st) N E Rp Z m σ s (L.nodeAt s (L.cpu s) o) := by + intro hw + rcases hw with hw | hw + · exact J.ipi_offStepA ho hw + · have hksz : ksSizeA = 788736 := rfl + obtain ⟨hv, hcl⟩ := J.sr.toSchedSRN.ipi_val ho + have := J.ipc_ksA hw + omega + +/-- `OSKeep` across a scheduler step that may write IPC objects. -/ +theorem JStage3A.osKeep_stepNA {s' : S} (w : WritesIn st s s' (SchedStepNWA (st := st) N E Rp Z m σ s)) : + OSKeep st OS s s' := by + intro y hy + have hfit := J.sys.ent.os.fit y hy + have al0 := J.sys.ent.os.al0 hy + have key : ∀ o : U64, o.val < 32 → o.val % 8 = 0 → L.mem s' (wa y o) = L.mem s (wa y o) := by + intro o o1 o2 + have hv : (wa y o).val = y.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have hal : Aligned (wa y o) := J.sys.ent.os.al hy o1 o2 + exact w.mem_eq hal (J.os_offStepNA hal ⟨y, hy, by omega, by omega⟩) + have e0 : wa y 0#u64 = y := by apply UScalar.eq_of_val_eq; rw [wa_valm]; simp + exact ⟨by have := key 0#u64 (by simp) (by simp); rwa [e0] at this, key 8#u64 (by simp) (by simp), + key 16#u64 (by simp) (by simp), key 24#u64 (by simp) (by simp)⟩ + +end off + +/-- **Extra words a step may write**: off the footprint the record keeps (the `OS` slots, the typed slots, the TCBs' +VCPU links, the IRQ-state words, the VSpace relation's words, the table pages and the VCPU words). -/ +structure XOffA (Z : XSetsA) (H : Heap) (OS : Set U64) (s : S) (X : U64 → Prop) : Prop where + os : ∀ b, X b → ¬ InOS OS b + slot : ∀ y b, IsSlotA Z.th y → y.val ≤ b.val → b.val < y.val + 32 → ¬ X b + link : ∀ t ∈ Z.T, ∀ K : U64, K = 3664#u64 ∨ K = 3672#u64 → ¬ X (wa t K) + irq : ∀ i : U64, i.val < 5376 → ¬ X (irqWordA (L.ksb s) i) + vs : ∀ b, VsFootA Z.Pp Z.S1 Z.Y (L.ksb s) b → ¬ X b + page : ∀ p, SchedPageA st Z OS s p → ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → ¬ X (wa p o) + vcw : ∀ v ∈ Z.V, ∀ o : U64, o.val = 0 ∨ o.val = 24 → ¬ X (wa v o) + /-- (record edit #8b-B1) The domain-schedule words. -/ + dom : ∀ b, DomW (L.ksb s) b → ¬ X b + /-- (record edit B2) The current core's IPI-pair words. -/ + ipi : ∀ o : U64, (o.val = 120 ∨ o.val = 128) → ¬ X (L.nodeAt s (L.cpu s) o) + +set_option maxHeartbeats 1000000 in +/-- The dead reply objects stay unlinked across a step with `QBlkA`'s `rt`. -/ +theorem JStage3A.hrt_qblkA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J5 : JSlotA S} {Rj : Set U64 → JSlotA S} {s : S} + {a : A} {σ σ' : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} {E' N' : Set Fl4SpecA64.ObjRef} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7A st Rj) s a σ m) + (Qk : QBlkA E' N' σ σ') : + ∀ r ∈ Rp, DeadA st OS .reply s r → ∀ x, σ'.kheap (specPtr r) = some (.reply x) → x.replyTcb = none := + fun r hr hd x hx => by + obtain ⟨y, hy, k⟩ := Qk.rt _ x hx + exact k (J.deadExt.replyTcb r hr hd y hy) + +/-- **`JStage3A` across a scheduler or IPC-object step that may also write words `X`** off the footprint (`XOffA`), +with user memory kept on the free untyped (J10): `of_schedWrN`'s body. The reply stacks may change (`stack'`), the +dead reply objects staying off them (`hdr`). -/ +theorem JStage3A.of_schedWrXSS {Ac' : Set U64} {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J5 : JSlotA S} {Rj : Set U64 → JSlotA S} {s s' : S} + {a a' : A} {σ σ' : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7A st Rj) s a σ m) + {Q : U64 → U64 → List U64} {Rl : U64 → List U64} {curOf curScOf : Fl4SpecA64.CoreId → Fl4SpecA64.ObjRef} + {X : U64 → Prop} (XO : XOffA (st := st) Z H OS s X) + (hXvs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → ¬ X b) (bufAl : UtBufAlA Z.T σ') + (w : WritesIn st s s' (fun b => SchedStepNWA (st := st) N E Rp Z m σ s b ∨ X b)) (HS : SpecSchedA σ σ') + (SSz : SSzA σ σ') (Qk : QBlkRA (specPtr '' E) (specPtr '' N) σ σ') + (hrt : ∀ r ∈ Rp, DeadA st OS .reply s r → ∀ x, σ'.kheap (specPtr r) = some (.reply x) → x.replyTcb = none) + (hz : ∀ x : Fl4SpecA64.Word, UtFreeA st OS s (fun d => d = false) (fl4Ptr x).val → + σ'.machineState.underlyingMemory x = σ.machineState.underlyingMemory x) (har : AR a' Z.hw) + (hcurOf : ∀ k, k ≠ m.curCore → curOf k = Z.curOf k) + (sys : SysInvA st H Z.Cs OS U Ac' Pp Tb Lv ia ie s') + (sq : SchedQueuesInv st H.tcbs Z.Cs H.omega H s' Q Rl) + (poolOff : PoolOff st H Z.Cs OS s') + (sr : SchedSRA st { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } m κ s' σ') + (scCov : ScCovA Z.C m σ') + (ep : EpStateRel Z.T E Rp Bf (L.mem s') σ') + (ntfn : NtfnStateRel N Z.T (L.mem s') σ') + {stack' : U64 → List U64} (reply : ReplyStackRelA (L.mem s') Rp Z.C σ' (scNameA m) stack') + (hdr : ∀ r ∈ Rp, DeadA st OS .reply s r → ∀ c ∈ Z.C, r ∉ stack' c) + (bufCover : BufCover st Z.T Bf s' σ') + (idleNoFault : IdleNoFaultA m σ') + (mcp : McpRelA Z.T m (L.mem s') σ') + (yieldRel : YieldRelA Z.T Z.C m (L.mem s') σ') + (sinv : SInvXA σ' m n) + (sidle : SIdleA σ' m n) + (runVcpu : RunVcpuInv st Z.T s') (blkQ : BlkQueuedA Z.T E N (L.mem s') σ') + (symL : SymL st H Z.Cs s') + (schedValid : SchedValidA σ') + (vcX : ∀ v ∈ Z.V, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → ¬ X (wa v o)) + (j5 : J5 { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } H OS s' σ' m) + (j7 : J7A st Rj { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } H OS s' σ' m) : + JStage3A st AR { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } H OS U Ac' Pp Tb Lv ia ie N E Rp + Bf stack' κ n J5 (J7A st Rj) s' a' σ' m := by + have hk := w.1 + have hc := w.2.1 + have osk : OSKeep st OS s s' := OSKeep.of_off J.sys.ent.os w fun y hy o o1 o2 h => by + have hv : (wa y o).val = y.val + o.val := by + have := J.sys.ent.os.fit y hy + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have hin : InOS OS (wa y o) := ⟨y, hy, by omega, by omega⟩ + exact h.elim (J.os_offStepNA (J.sys.ent.os.al hy o1 o2) hin) fun hx => XO.os _ hx hin + have capE : ∀ u ∈ OS, capA st s' u = capA st s u := fun u hu => by + unfold capA; rw [(osk u hu).1, (osk u hu).2.1] + have keep : ∀ b, Aligned b → ¬ SchedStepNWA (st := st) N E Rp Z m σ s b → ¬ X b → L.mem s' b = L.mem s b := + fun b hb hn hx => w.mem_eq hb fun h => h.elim hn hx + obtain ⟨Pt, Ub, vr, -, arm, -, -, -, -, link, apart, -⟩ := J.j7 + have P := J.sr.place + have hT : ∀ t ∈ Z.T, t ∈ H.tcbs := fun t ht => by rw [J.tcbs]; exact ht + -- the page words + have vsr : ∀ b, VsR (L.ksb s) Pt b → ¬ X b → L.mem s' b = L.mem s b := fun b hb hx => by + refine keep _ (vr.al _ hb) ?_ hx + by_cases h : AsW (L.ksb s) b + · obtain ⟨o, o1, o2, -, e⟩ := h + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.sys.ent.ksw + have ev : b.val = (L.ksb s).val + o.val := by + rw [e, wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact J.asid_offStepNA (by omega) (by omega) + · exact J.vsr_offStepNA vr hb h + have vsfoot : ∀ b, VsFootA Z.Pp Z.S1 Z.Y (L.ksb s) b → L.mem s' b = L.mem s b := fun b hb => by + refine vsr b ?_ (XO.vs b hb) + rcases hb.vsR with ⟨p, hp, h⟩ | h + · refine Or.inl ⟨p, link p ?_, h⟩ + rcases hp with hp | hp | hp + · exact Or.inl hp + · exact Or.inr (Or.inl hp) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl hp)))))) + · exact Or.inr h + have word : ∀ p, SchedPageA st Z OS s p → ∀ i : U64, i.val < 512 → L.mem s' (peAF p i) = L.mem s (peAF p i) := + fun p hp i hi => by + refine vsr _ (Or.inl ⟨p, link p hp, core.num.U64.wrapping_mul i 8#u64, ?_, ?_, rfl⟩) + (XO.page p hp (core.num.U64.wrapping_mul i 8#u64) (by rw [wm8_valA hi]; omega) (by rw [wm8_valA hi]; omega)) + · rw [wm8_valA hi]; omega + · rw [wm8_valA hi]; omega + have extfoot : ∀ b, ExtFootE Z.X Z.Y Z.T2 b → L.mem s' b = L.mem s b := fun b hb => by + cases hb with + | s2lo r i hr hi => exact word r (Or.inr (Or.inr (Or.inr (Or.inl hr)))) i hi + | s2hi r i hr hi => exact word _ (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨r, hr, rfl⟩))))) i hi + | s2pt t i ht hi => exact word t (Or.inr (Or.inr (Or.inl ht))) i hi + | wl0 y hy => exact word y (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl hy)))))) _ (by simp) + | wlo y i hy hi => exact word y (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl hy)))))) i (by omega) + -- the VCPU words + have vcw : ∀ v ∈ Z.V, ∀ o : U64, o.val = 0 ∨ o.val = 24 → L.mem s' (wa v o) = L.mem s (wa v o) := by + intro v hv o ho + have hH := arm.vh v hv + have hfit := J.sys.ent.heap.fits v _ hH + have hal := J.sys.ent.heap.align v _ hH + simp only [HKind.bits] at hfit hal + have ev : (wa v o).val = v.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have al : Aligned (wa v o) := by show _ % 8 = 0; rw [ev]; omega + have inObj : InObj v (.other 12) (wa v o) := by + unfold InObj; simp only [HKind.bits]; omega + have hsc : ∀ c ∈ H.scs, ¬ InSC c (H.scBits c) (wa v o) := by + rintro c ⟨z, hz⟩ ⟨c1, c2⟩ + have e : H.scBits c = z := by unfold Heap.scBits; rw [hz] + have ne : v ≠ c := fun h => by rw [h, hz] at hH; cases hH + have := J.sys.ent.heap.disj v _ c _ hH hz ne + simp only [HKind.bits] at this + rw [e] at c2 + omega + have hg : ∀ g : U64, g.val < 223488 → wa (L.ksb s) g ≠ wa v o := by + intro g hg e + exact apart _ ⟨v, hv, o, by omega, by omega, rfl⟩ g hg e + exact keep _ al (fun hw => hw.elim + (J.omega_offStepA ⟨al, v, _, hH, Or.inr (Or.inr (Or.inl ⟨12, rfl⟩)), inObj⟩ hsc hg) + (J.ipc_off_hobjA hH (by simp) inObj)) (XO.vcw v hv o ho) + have vcwAll : ∀ v ∈ Z.V, ∀ o : U64, o.val < 4096 ∧ o.val % 8 = 0 → L.mem s' (wa v o) = L.mem s (wa v o) := by + intro v hv o ⟨ho1, ho2⟩ + have hH := arm.vh v hv + have hfit := J.sys.ent.heap.fits v _ hH + have hal := J.sys.ent.heap.align v _ hH + simp only [HKind.bits] at hfit hal + have ev : (wa v o).val = v.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have al : Aligned (wa v o) := by show _ % 8 = 0; rw [ev]; omega + have inObj : InObj v (.other 12) (wa v o) := by + unfold InObj; simp only [HKind.bits]; omega + have hsc : ∀ c ∈ H.scs, ¬ InSC c (H.scBits c) (wa v o) := by + rintro c ⟨z, hz⟩ ⟨c1, c2⟩ + have e : H.scBits c = z := by unfold Heap.scBits; rw [hz] + have ne : v ≠ c := fun h => by rw [h, hz] at hH; cases hH + have := J.sys.ent.heap.disj v _ c _ hH hz ne + simp only [HKind.bits] at this + rw [e] at c2 + omega + have hg : ∀ g : U64, g.val < 223488 → wa (L.ksb s) g ≠ wa v o := by + intro g hg e + exact apart _ ⟨v, hv, o, by omega, by omega, rfl⟩ g hg e + exact keep _ al (fun hw => hw.elim + (J.omega_offStepA ⟨al, v, _, hH, Or.inr (Or.inr (Or.inl ⟨12, rfl⟩)), inObj⟩ hsc hg) + (J.ipc_off_hobjA hH (by simp) inObj)) (vcX v hv o ho1 ho2) + have slotw : ∀ y, IsSlotA Z.th y → ∀ x, (x = y ∨ x = wa y 8#u64 ∨ x = wa y 16#u64 ∨ x = wa y 24#u64) → + L.mem s' x = L.mem s x := by + intro y hy x hx + obtain ⟨a32, f32⟩ := hy.al32 P + have hv : ∀ o : U64, o.val < 32 → (wa y o).val = y.val + o.val := fun o ho => by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have key : x.val % 8 = 0 ∧ y.val ≤ x.val ∧ x.val < y.val + 32 := by + rcases hx with rfl | rfl | rfl | rfl + · omega + all_goals + rw [hv _ (by simp)]; simp; omega + exact keep _ key.1 (J.slot_offStepNA hy key.2.1 key.2.2) (XO.slot y _ hy key.2.1 key.2.2) + have cnw : ∀ y, SlotWA Z.th y → IsSlotA Z.th y := fun y hy => by + rcases hy with h | h + · exact h + · obtain ⟨p, r, hp, idx, hl, rfl⟩ := h + obtain ⟨e, hb, -⟩ := cn_slotV P hp hl + exact ⟨p, Or.inl ⟨r, hp, blvA idx, hb, e⟩⟩ + have tcbl : ∀ t ∈ Z.T, ∀ K : U64, K = 3664#u64 ∨ K = 3672#u64 → L.mem s' (wa t K) = L.mem s (wa t K) := by + intro t ht K hK + have hta := J.sr.core.tcbs t ht + refine keep _ (field_aligned (K := K) hta ?_ ?_) (J.link_offStepNA ht hK) (XO.link t ht K hK) + all_goals rcases hK with rfl | rfl <;> simp + have mem : SchedKeepMemA (st := st) Z s s' := + { ksb := hk + cpu := hc + slot := fun y hy x hx => slotw y (cnw y hy) x hx + tcbW := fun t ht => ⟨tcbl t ht _ (Or.inl rfl), tcbl t ht _ (Or.inr rfl)⟩ + vcW := fun v hv => by + have e0 : wa v 0#u64 = v := by apply UScalar.eq_of_val_eq; rw [wa_valm]; simp + have h0 := vcw v hv 0#u64 (Or.inl (by simp)) + rw [e0] at h0 + exact ⟨h0, vcw v hv _ (Or.inr (by simp))⟩ + irqW := fun i hi => by + refine keep _ ?_ (J.irq_offStepNA hi) (XO.irq i hi) + have := irqWordA_val J.sr.ksFit hi + have := J.sr.toSchedSRN.ksb_al + show _ % 8 = 0 + omega + vs := vsfoot + ext := extfoot } + -- the node cursors + have nodes : ∀ c ∈ Z.Cs, + L.mem s' (L.nodeAt s' c layout.node.IDLE_THREAD) ∈ Z.T ∧ L.mem s' (L.nodeAt s' c layout.node.IDLE_SC) ∈ Z.C ∧ + L.mem s' (L.nodeAt s' c layout.node.CUR_THREAD) ∈ Z.T := by + intro c hcs + refine ⟨?_, ?_, ?_⟩ + · have e : L.mem s' (L.nodeAt s' c layout.node.IDLE_THREAD) = L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) := + specPtr_inj ((sr.sch.idleOf c hcs).symm.trans (J.sr.sch.idleOf c hcs)) + rw [e]; exact J.j.1.idleT c hcs + · have e : L.mem s' (L.nodeAt s' c layout.node.IDLE_SC) = L.mem s (L.nodeAt s c layout.node.IDLE_SC) := + specPtr_inj ((sr.sch.idleScOf c hcs).symm.trans (J.sr.sch.idleScOf c hcs)) + rw [e]; exact J.j.1.idleC c hcs + · by_cases h : c = L.cpu s + · have := sr.curT + unfold LawfulStore.curW at this + rw [hc, ← h] at this + exact this + · have hne : c.val ≠ m.curCore := fun e => h (UScalar.eq_of_val_eq (e.trans J.sr.sch.core)) + have h1 := sr.sch.curOfW c hcs + rw [show ({ Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } : XSetsA).curOf = curOf from rfl, + hcurOf _ hne] at h1 + have e : L.mem s' (L.nodeAt s' c layout.node.CUR_THREAD) = L.mem s (L.nodeAt s c layout.node.CUR_THREAD) := + specPtr_inj (h1.symm.trans (J.sr.sch.curOfW c hcs)) + rw [e]; exact J.j.1.curT c hcs + have st' : StEqA Z.T (L.mem s') σ' := fun _ ht _ hx => (ep.at_tcb ht hx).st + have jx := JAtZAX.of_sched J.j (a' := a') (Q := Q) (R := Rl) (curOf := curOf) (curScOf := curScOf) mem HS har + (sr.sch.withSt st') sr.rest (fun c h => (nodes c h).1) (fun c h => (nodes c h).2.1) + (fun c h => (nodes c h).2.2) sr.cscAll + have free : ∀ dev x, UtFreeA st OS s' dev x → UtFreeA st OS s dev x := by + rintro dev x ⟨u, hu, q, b, fi, d, hcap, h⟩ + exact ⟨u, hu, q, b, fi, d, (capE u hu).symm.trans hcap, h⟩ + have dead : ∀ k p, DeadA st OS k s' p → DeadA st OS k s p := fun k p h u hu hp => by + have := h u hu + rw [capE u hu] at this + exact this hp + have nsc : ∀ {p : Fl4SpecA64.ObjRef}, σ.kheap p = none → σ'.kheap p = none := fun {p} h => by + rcases HS.kh p with e | ⟨x, _, e1, -⟩ | ⟨o, o', e1, -, -⟩ + · rw [e]; exact h + · rw [h] at e1; cases e1 + · rw [h] at e1; cases e1 + have keepE : ∀ {q : Fl4SpecA64.ObjRef} {e : Fl4SpecA64.ExtArchObj Fl4SpecA64.PTE}, + σ.kheap q = some (.archObj (.ext e)) → σ'.kheap q = some (.archObj (.ext e)) := fun h1 => + HS.keep h1 (fun _ e => by cases e) (fun e => by rcases e with ⟨_, _, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ <;> cases e) + have unmS2 : ∀ u ∈ OS, ∀ p a v, capA st s u = .Ept p 0#u64 false a v → UnmS2RootA st s σ p → + UnmS2RootA st s' σ' p := by + intro u hu p a v hc U + obtain ⟨es, h1, h2⟩ := U.spec + refine { inv := fun i hi => ?_, spec := ⟨es, keepE h1, h2⟩ } + by_cases hlo : i.val < 512 + · rw [word p (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inr (Or.inl ⟨a, v, hc⟩))⟩))))))) i hlo] + exact U.inv i hi + · obtain ⟨j, hj, e⟩ := peAF_hiA p i (by omega) hi + rw [e, word _ (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inr (Or.inr (Or.inl ⟨p, a, v, hc, rfl⟩)))⟩))))))) j hj, ← e] + exact U.inv i hi + have unmHv : ∀ u ∈ OS, ∀ y a, capA st s u = .HighVSpace y false a → UnmHiRootA st s σ y → + UnmHiRootA st s' σ' y := by + intro u hu y a hc U + obtain ⟨l1, h1, h2⟩ := U.spec + exact { inv := fun i hi => by + rw [word _ (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inr (Or.inr (Or.inr ⟨y, a, hc, rfl⟩)))⟩))))))) i hi] + exact U.inv i hi + spec := ⟨l1, keepE h1, h2⟩ } + have unm : ∀ ty p, SchedPageA st Z OS s p → UnmTabA st Z m s σ ty p → + UnmTabA st { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } m s' σ' ty p := by + intro ty p hp U1 + obtain ⟨tb, h1, h2, h3⟩ := U1.spec + exact + { inv := fun i hi => by rw [word p hp i hi]; exact U1.inv i hi + spec := ⟨tb, HS.keep h1 (fun _ e => by cases e) (fun e => by rcases e with ⟨_, _, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ <;> cases e), h2, h3⟩ + off := ⟨fun i hi h => U1.off.mem i hi (footA_schedA hk hc h), U1.off.obj⟩ } + exact + { sys := sys + j := jx + tcbs := J.tcbs + scs := J.scs + sq := sq + ap := by + have e : L.ksb s' = L.ksb s := hk + rw [e]; exact { J.ap with } + pp := J.pp + s1 := J.s1 + ko := by rw [hk]; exact J.ko + ptH := J.ptH + tabInj := by rw [HS.asid]; exact J.tabInj + poolOff := poolOff + objAlloc := ObjAllocA.keep (Z := Z) J.objAlloc osk + idleAnchor := IdleAnchorOffA.keep J.idleAnchor osk + vsVc := by rw [hk]; exact J.vsVc + sr := sr + scCov := scCov + scSize := fun c hcm x k h => by + obtain ⟨y, hy⟩ := SSz _ x k h + exact J.scSize c hcm y k hy + ep := ep + ntfn := ntfn + reply := reply + bufCover := bufCover + bufNative := fun t ht => by + have hsl : IsSlotA Z.th (slotAddrA (specPtr t) (BitVec.ofNat 64 2)) := + ⟨t, Or.inr ⟨(J.sr.thT t).2 ht, 2, by unfold tcbSlotsA; omega, rfl⟩⟩ + have e : capAtA (L.mem s') (slotAddrA (specPtr t) (BitVec.ofNat 64 2)) = + capAtA (L.mem s) (slotAddrA (specPtr t) (BitVec.ofNat 64 2)) := by + unfold capAtA + rw [slotw _ hsl _ (Or.inl rfl), slotw _ hsl _ (Or.inr (Or.inl rfl))] + rw [e]; exact J.bufNative t ht + iplaced := J.iplaced + ipcPlace := J.ipcPlace + idleNoFault := idleNoFault + mcp := mcp + yieldRel := yieldRel + sinv := sinv + setsInH := { J.setsInH with } + utIpc := J.utIpc + ipcTh := J.ipcTh + deadExt := + { vcpu := fun v hv hd x hx => J.deadExt.vcpu v hv (dead _ v hd) x + (HS.back hx (fun _ e => by cases e) (fun e => by rcases e with ⟨_, _, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ <;> cases e)) + reply := fun r hr hd => hdr r hr (dead _ r hd) + asid := fun i p h hd => J.deadExt.asid i p (by rw [HS.asid] at h; exact h) (dead _ p hd) + tvcpu := fun t ht hd => by + rw [tcbl t ht 3664#u64 (Or.inl rfl)] + exact J.deadExt.tvcpu t ht (dead _ t hd) + vcDeadRun := fun v hv hd => by + rw [vcw v hv 24#u64 (Or.inr (by simp))] + exact J.deadExt.vcDeadRun v hv (dead _ v hd) + replyTcb := fun r hr hd x hx => hrt r hr (dead _ r hd) x hx } + specFree := + { heap := fun p h => nsc (J.specFree.heap p (free _ _ h)) + cdt := fun x y h p hf => J.specFree.cdt x y (by rw [HS.cdt] at h; exact h) p (free _ _ hf) + orig := fun x h p hf => J.specFree.orig x (by rw [HS.orig] at h; exact h) p (free _ _ hf) } + utBlk := fun u hu q b fi d h => J.utBlk u hu q b fi d ((capE u hu).symm.trans h) + unmPt := + { pt := fun u hu p asid va h => by + have h0 := (capE u hu).symm.trans h + exact unm _ p (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inl ⟨_, _, _, h0⟩⟩))))))) (J.unmPt.pt u hu p asid va h0) + vs := fun u hu p asid h => by + have h0 := (capE u hu).symm.trans h + exact unm _ p (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inl ⟨_, _, h0⟩)⟩))))))) (J.unmPt.vs u hu p asid h0) + s2 := fun u hu p asid va h => by + have h0 := (capE u hu).symm.trans h + exact unmS2 u hu p asid va h0 (J.unmPt.s2 u hu p asid va h0) + hv := fun u hu p asid h => by + have h0 := (capE u hu).symm.trans h + exact unmHv u hu p asid h0 (J.unmPt.hv u hu p asid h0) } + tabS1 := fun q hq => + ⟨fun i hi h => (J.tabS1 q hq).mem i hi (footA_schedA hk hc h), (J.tabS1 q hq).obj⟩ + tabExt := + ⟨fun q hq i hi h => J.tabExt.mem q hq i hi (by + exact footA_schedA (Z := extZA Z) hk hc h), J.tabExt.obj⟩ + specZero := fun x h => (hz x (free _ _ h)).trans (J.specZero x (free _ _ h)) + origLive := fun x h => J.origLive x (by rw [HS.orig] at h; exact h) + nullOrig := fun sl h => by + rw [HS.orig] + exact J.nullOrig sl (by rw [← SpecSchedA.specCapAt HS sl]; exact h) + thPos := fun p z h => J.thPos p z h + frBase := J.frBase.keepA capE + devFr := J.devFr.keepA capE fun p dev pg h => HS.keep h (fun _ e => by cases e) + (fun e => by rcases e with ⟨_, _, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ <;> cases e) + finCov := J.finCov.keepA + (fun q r cs h => HS.back h (fun _ e => by cases e) + (fun e => by rcases e with ⟨_, _, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ <;> cases e)) + (fun q x h => HS.tcbBack h) + idleAll := J.idleAll.keepA + (fun c hcs => specPtr_inj ((sr.sch.idleOf c hcs).symm.trans (J.sr.sch.idleOf c hcs))) + (fun c hcs => specPtr_inj ((sr.sch.idleScOf c hcs).symm.trans (J.sr.sch.idleScOf c hcs))) + ipcHeapCov := J.ipcHeapCov + zombieNoVcpu := J.zombieNoVcpu.keepA capE J.zombieTcb + fun t ht => tcbl t (J.tcbs ▸ ht) 3664#u64 (Or.inl rfl) + zombieTcb := J.zombieTcb.keepA capE + bufOff := J.bufOff.keepA (Z := Z) (Z' := { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf }) + hk rfl rfl rfl rfl rfl rfl rfl capE + vsOk := by + have e := (utVsPt_keepA J.sys.ent.vs hk (fun b hb => by + refine keep b (J.vsOk.al b hb) ?_ (hXvs b hb) + by_cases h : AsW (L.ksb s) b + · obtain ⟨o, o1, o2, -, e⟩ := h + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.sys.ent.ksw + have ev : b.val = (L.ksb s).val + o.val := by + rw [e, wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact J.asid_offStepNA (by omega) (by omega) + · exact J.vsr_offStepNA J.vsOk hb h)).2 + unfold UtVsOkA + rw [show L.ksb s' = L.ksb s from hk, e] + exact J.vsOk + domOK := J.domOK.keepS J.sr.toSchedSRN Qk.dom hk hc (fun b hb hd => w.mem_eq hb fun h => h.elim (J.dom_offStepNA hd) (XO.dom b hd)) + ipiZero := J.ipiKeepOf w fun o ho h => h.elim (J.ipi_offStepNA ho) (XO.ipi o ho) + bufVs := by + have e := (utVsPt_keepA J.sys.ent.vs hk (fun b hb => by + refine keep b (J.vsOk.al b hb) ?_ (hXvs b hb) + by_cases h : AsW (L.ksb s) b + · obtain ⟨o, o1, o2, -, e⟩ := h + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.sys.ent.ksw + have ev : b.val = (L.ksb s).val + o.val := by + rw [e, wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact J.asid_offStepNA (by omega) (by omega) + · exact J.vsr_offStepNA J.vsOk hb h)).2 + rw [show L.ksb s' = L.ksb s from hk, e] + exact J.bufVs + bufAl := bufAl + zombieBnd := J.zombieBnd.keepA capE + devUtOff := J.devUtOff.keepA (Z := Z) capE hk (fun _ _ h => h) rfl + sidle := sidle + capTy := J.capTy.keepA capE + scCpu := J.scCpu + scNZ := J.scNZ + vcRun := J.vcRun.keepA (fun t ht => tcbl t ht 3664#u64 (Or.inl rfl)) (fun v hv => vcw v hv 24#u64 (Or.inr (by simp))) + (fun v hv => by + have h0 := vcw v hv 0#u64 (Or.inl (by simp)) + rwa [wa_zeroVcA] at h0) + (fun t ht => tcbl t ht 3672#u64 (Or.inr rfl)) + vcSym1 := J.vcSym1.keepA J.vcRun (fun t ht => tcbl t ht 3664#u64 (Or.inl rfl)) (fun v hv => by + have h0 := vcw v hv 0#u64 (Or.inl (by simp)) + rwa [wa_zeroVcA] at h0) + runVcpu := runVcpu + vcSym2 := J.vcSym2.keepA J.vcRun (fun v hv => vcw v hv 24#u64 (Or.inr (by simp))) + (fun t ht => tcbl t ht 3672#u64 (Or.inr rfl)) + blkQ := blkQ + heapWin := J.heapWin + coresAll := J.coresAll + ptCapIn := PtCapInA.of_capsA J.ptCapIn rfl (fun _ h => h) (fun _ h => h) + (fun y hy => Or.inl (by + unfold capAtA; rw [slotw y hy _ (Or.inl rfl), slotw y hy _ (Or.inr (Or.inl rfl))])) + symL := symL + fhOK := fun t ht => by + have hy := isSlot_fhA ((J.sr.thT t).2 ht) + have e : capA st s' (slotIdx t layout.tcb.SLOT_FAULT_HANDLER) = + capA st s (slotIdx t layout.tcb.SLOT_FAULT_HANDLER) := by + unfold capA; rw [slotw _ hy _ (Or.inl rfl), slotw _ hy _ (Or.inr (Or.inl rfl))] + rw [e]; exact J.fhOK t ht + zwf := DelInvA.keepW J.zwf fun y hy => by + unfold capA; rw [slotw y hy _ (Or.inl rfl), slotw y hy _ (Or.inr (Or.inl rfl))] + schedValid := schedValid + utWin := J.utWin.keepA capE + devWin := J.devWin.keepA capE + vcBack := vcBack_keepA J.vcRun.vtcb (fun v hv => by + have h0 := vcw v hv 0#u64 (Or.inl (by simp)) + rwa [wa_zeroVcA] at h0) (fun t ht => tcbl t ht 3664#u64 (Or.inl rfl)) J.vcBack + runBack := runBack_keepA J.vcRun.run (fun t ht => tcbl t ht 3672#u64 (Or.inr rfl)) + (fun v hv => vcw v hv 24#u64 (Or.inr (by simp))) J.runBack + vcSnap := J.vcSnap.keepA (fun v hv o h1 h2 => vcwAll v hv o ⟨h1, h2⟩) + (fun v _ x hx => HS.back hx (fun _ e => by cases e) + (fun e => by rcases e with ⟨_, _, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ <;> cases e)) + vcOS := J.vcOS + userFr := J.userFr.keepA capE fun p dev pg h => HS.keep h (fun _ e => by cases e) + (fun e => by rcases e with ⟨_, _, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ <;> cases e) + frTh := J.frTh.keepA capE fun _ _ e => e + bfSpec := J.bfSpec + curVcpuNone := HS.vcur.trans J.curVcpuNone + ipcSorted := Qk.sorted + activeReplyScs := Qk.act J.activeReplyScs + j5 := j5 + j7 := j7 } + + +set_option maxHeartbeats 1000000 in +/-- `of_schedWrXSS` for a step shrinking the endpoint and notification queues (`QSendA`). -/ +theorem JStage3A.of_schedWrXS {Ac' : Set U64} {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J5 : JSlotA S} {Rj : Set U64 → JSlotA S} {s s' : S} + {a a' : A} {σ σ' : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7A st Rj) s a σ m) + {Q : U64 → U64 → List U64} {Rl : U64 → List U64} {curOf curScOf : Fl4SpecA64.CoreId → Fl4SpecA64.ObjRef} + {X : U64 → Prop} (XO : XOffA (st := st) Z H OS s X) + (hXvs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → ¬ X b) (bufAl : UtBufAlA Z.T σ') + (w : WritesIn st s s' (fun b => SchedStepNWA (st := st) N E Rp Z m σ s b ∨ X b)) (HS : SpecSchedA σ σ') + (SSz : SSzA σ σ') (Qk : QSendA σ σ') + (hz : ∀ x : Fl4SpecA64.Word, UtFreeA st OS s (fun d => d = false) (fl4Ptr x).val → + σ'.machineState.underlyingMemory x = σ.machineState.underlyingMemory x) (har : AR a' Z.hw) + (hcurOf : ∀ k, k ≠ m.curCore → curOf k = Z.curOf k) + (sys : SysInvA st H Z.Cs OS U Ac' Pp Tb Lv ia ie s') + (sq : SchedQueuesInv st H.tcbs Z.Cs H.omega H s' Q Rl) + (poolOff : PoolOff st H Z.Cs OS s') + (sr : SchedSRA st { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } m κ s' σ') + (scCov : ScCovA Z.C m σ') + (ep : EpStateRel Z.T E Rp Bf (L.mem s') σ') + (ntfn : NtfnStateRel N Z.T (L.mem s') σ') + {stack' : U64 → List U64} (reply : ReplyStackRelA (L.mem s') Rp Z.C σ' (scNameA m) stack') + (hdr : ∀ r ∈ Rp, DeadA st OS .reply s r → ∀ c ∈ Z.C, r ∉ stack' c) + (bufCover : BufCover st Z.T Bf s' σ') + (idleNoFault : IdleNoFaultA m σ') + (mcp : McpRelA Z.T m (L.mem s') σ') + (yieldRel : YieldRelA Z.T Z.C m (L.mem s') σ') + (sinv : SInvXA σ' m n) + (sidle : SIdleA σ' m n) + (runVcpu : RunVcpuInv st Z.T s') (blkQ : BlkQueuedA Z.T E N (L.mem s') σ') + (symL : SymL st H Z.Cs s') + (schedValid : SchedValidA σ') + (vcX : ∀ v ∈ Z.V, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → ¬ X (wa v o)) + (j5 : J5 { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } H OS s' σ' m) + (j7 : J7A st Rj { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } H OS s' σ' m) : + JStage3A st AR { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } H OS U Ac' Pp Tb Lv ia ie N E Rp + Bf stack' κ n J5 (J7A st Rj) s' a' σ' m := + JStage3A.of_schedWrXSS (J := J) (XO := XO) (hXvs := hXvs) (bufAl := bufAl) (w := w) (HS := HS) (SSz := SSz) + (hz := hz) (har := har) (hcurOf := hcurOf) (sys := sys) (sq := sq) (poolOff := poolOff) (sr := sr) + (scCov := scCov) (ep := ep) (ntfn := ntfn) (reply := reply) (hdr := hdr) (bufCover := bufCover) + (idleNoFault := idleNoFault) (mcp := mcp) (yieldRel := yieldRel) (sinv := sinv) (sidle := sidle) + (runVcpu := runVcpu) (blkQ := blkQ) (symL := symL) (schedValid := schedValid) (vcX := vcX) (j5 := j5) + (j7 := j7) + (Qk := (QSendA.toBlk J.ipcSorted Qk).toR) (hrt := J.hrt_qblkA (QSendA.toBlk J.ipcSorted Qk)) + +/-- `of_schedWrXS` with the reply stacks kept. -/ +theorem JStage3A.of_schedWrX {Ac' : Set U64} {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J5 : JSlotA S} {Rj : Set U64 → JSlotA S} {s s' : S} + {a a' : A} {σ σ' : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7A st Rj) s a σ m) + {stack' : U64 → List U64} + {Q : U64 → U64 → List U64} {Rl : U64 → List U64} {curOf curScOf : Fl4SpecA64.CoreId → Fl4SpecA64.ObjRef} + {X : U64 → Prop} (XO : XOffA (st := st) Z H OS s X) + (hXvs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → ¬ X b) (bufAl : UtBufAlA Z.T σ') + (w : WritesIn st s s' (fun b => SchedStepNWA (st := st) N E Rp Z m σ s b ∨ X b)) (HS : SpecSchedA σ σ') + (SSz : SSzA σ σ') (Qk : QKeepA σ σ') + (hz : ∀ x : Fl4SpecA64.Word, UtFreeA st OS s (fun d => d = false) (fl4Ptr x).val → + σ'.machineState.underlyingMemory x = σ.machineState.underlyingMemory x) (har : AR a' Z.hw) + (hcurOf : ∀ k, k ≠ m.curCore → curOf k = Z.curOf k) + (sys : SysInvA st H Z.Cs OS U Ac' Pp Tb Lv ia ie s') + (sq : SchedQueuesInv st H.tcbs Z.Cs H.omega H s' Q Rl) + (poolOff : PoolOff st H Z.Cs OS s') + (sr : SchedSRA st { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } m κ s' σ') + (scCov : ScCovA Z.C m σ') + (ep : EpStateRel Z.T E Rp Bf (L.mem s') σ') + (ntfn : NtfnStateRel N Z.T (L.mem s') σ') + (reply : ReplyStackRelA (L.mem s') Rp Z.C σ' (scNameA m) stack') + (bufCover : BufCover st Z.T Bf s' σ') + (idleNoFault : IdleNoFaultA m σ') + (mcp : McpRelA Z.T m (L.mem s') σ') + (yieldRel : YieldRelA Z.T Z.C m (L.mem s') σ') + (sinv : SInvXA σ' m n) + (sidle : SIdleA σ' m n) + (runVcpu : RunVcpuInv st Z.T s') (blkQ : BlkQueuedA Z.T E N (L.mem s') σ') + (symL : SymL st H Z.Cs s') + (schedValid : SchedValidA σ') + (vcX : ∀ v ∈ Z.V, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → ¬ X (wa v o)) + (j5 : J5 { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } H OS s' σ' m) + (j7 : J7A st Rj { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } H OS s' σ' m) + (hsub : ∀ c, ∀ r ∈ stack' c, r ∈ stack c := by exact fun _ _ h => h) : + JStage3A st AR { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } H OS U Ac' Pp Tb Lv ia ie N E Rp + Bf stack' κ n J5 (J7A st Rj) s' a' σ' m := + JStage3A.of_schedWrXS (J := J) (XO := XO) (reply := reply) (hdr := fun r hr hd c hc hm => J.deadExt.reply r hr hd c hc (hsub c r hm)) (hXvs := hXvs) (bufAl := bufAl) + (w := w) (HS := HS) (SSz := SSz) (Qk := Qk.toSend) (hz := hz) (har := har) (hcurOf := hcurOf) (sys := sys) (sq := sq) + (poolOff := poolOff) (sr := sr) (scCov := scCov) (ep := ep) (ntfn := ntfn) (bufCover := bufCover) + (idleNoFault := idleNoFault) (mcp := mcp) (yieldRel := yieldRel) (sinv := sinv) (sidle := sidle) + (runVcpu := runVcpu) (blkQ := blkQ) (symL := symL) (schedValid := schedValid) (vcX := vcX) (j5 := j5) (j7 := j7) + +set_option maxHeartbeats 1000000 in +/-- **`JStage3A` across a scheduler or IPC-object step** (module docstring): the write set is `SchedStepNWA`. -/ +theorem JStage3A.of_schedWrN {Ac' : Set U64} {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J5 : JSlotA S} {Rj : Set U64 → JSlotA S} {s s' : S} + {a a' : A} {σ σ' : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7A st Rj) s a σ m) + {stack' : U64 → List U64} + {Q : U64 → U64 → List U64} {Rl : U64 → List U64} {curOf curScOf : Fl4SpecA64.CoreId → Fl4SpecA64.ObjRef} + (w : WritesIn st s s' (SchedStepNWA (st := st) N E Rp Z m σ s)) (HS : SpecSchedA σ σ') (SSz : SSzA σ σ') (Qk : QKeepA σ σ') + (hum : σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory) (har : AR a' Z.hw) + (hcurOf : ∀ k, k ≠ m.curCore → curOf k = Z.curOf k) (bufAl : UtBufAlA Z.T σ') + (sys : SysInvA st H Z.Cs OS U Ac' Pp Tb Lv ia ie s') + (sq : SchedQueuesInv st H.tcbs Z.Cs H.omega H s' Q Rl) + (poolOff : PoolOff st H Z.Cs OS s') + (sr : SchedSRA st { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } m κ s' σ') + (scCov : ScCovA Z.C m σ') + (ep : EpStateRel Z.T E Rp Bf (L.mem s') σ') + (ntfn : NtfnStateRel N Z.T (L.mem s') σ') + (reply : ReplyStackRelA (L.mem s') Rp Z.C σ' (scNameA m) stack') + (bufCover : BufCover st Z.T Bf s' σ') + (idleNoFault : IdleNoFaultA m σ') + (mcp : McpRelA Z.T m (L.mem s') σ') + (yieldRel : YieldRelA Z.T Z.C m (L.mem s') σ') + (sinv : SInvXA σ' m n) + (sidle : SIdleA σ' m n) + (runVcpu : RunVcpuInv st Z.T s') (blkQ : BlkQueuedA Z.T E N (L.mem s') σ') + (symL : SymL st H Z.Cs s') + (schedValid : SchedValidA σ') + (j5 : J5 { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } H OS s' σ' m) + (j7 : J7A st Rj { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } H OS s' σ' m) + (hsub : ∀ c, ∀ r ∈ stack' c, r ∈ stack c := by exact fun _ _ h => h) : + JStage3A st AR { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } H OS U Ac' Pp Tb Lv ia ie N E Rp + Bf stack' κ n J5 (J7A st Rj) s' a' σ' m := + JStage3A.of_schedWrX (hsub := hsub) (symL := symL) (schedValid := schedValid) (vcX := fun _ _ _ _ _ h => h) J + (X := fun _ => False) + ⟨fun _ h => h.elim, fun _ _ _ _ _ h => h, fun _ _ _ _ h => h, fun _ _ h => h, fun _ _ h => h, + fun _ _ _ _ _ h => h, fun _ _ _ _ h => h, fun _ _ h => h, fun _ _ h => h⟩ + (fun _ _ h => h) bufAl + (w.mono fun _ h => Or.inl h) HS SSz Qk (fun x _ => by rw [hum]) har hcurOf sys sq poolOff sr scCov ep ntfn reply + bufCover idleNoFault mcp yieldRel sinv sidle runVcpu blkQ j5 j7 + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3SchedWrNTA.lean b/proofs/Fl4ProofsA64/P3/Stage3SchedWrNTA.lean new file mode 100644 index 000000000..3c038835d --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SchedWrNTA.lean @@ -0,0 +1,793 @@ +/- +P3 (AArch64, shared standalone module, contract §41): **`JStage3TA` across a scheduler step that also writes IPC +objects**: `P3/Stage3SchedWrNA`'s `ipc_*A` / `*_offStepNA` lemmas, `of_schedWrX` and `of_schedWrN` over the per-step +record, with any J7 slot of the class `J7PpA`. Generated by `handoff/tools/gen_schedwrnta.py` (`copy_ta.py`). No named +premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3BA +import Fl4ProofsA64.P3.Stage3SchedWrTA +import Fl4ProofsA64.P3.Stage3SchedWrNA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +section offBA +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} [J7PpA st J7] {s : S} {a : A} {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} + (J : JStage3BA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) +include J + +/-- An IPC word lies in an endpoint, notification or reply heap object. -/ +theorem JStage3BA.ipc_hobjPA {b : U64} (h : IpcObjWA N E Rp b) : + ∃ p k, H p = some k ∧ (k = .ep ∨ k = .ntfn ∨ k = .reply) ∧ InObj p k b := by + rcases h with ⟨n, hn, h1, h2⟩ | ⟨e, he, h1, h2⟩ | ⟨r, hr, h1, h2⟩ + · exact ⟨n, .ntfn, J.utIpc.ntfn n hn, Or.inr (Or.inl rfl), h1, by simpa [HKind.bits] using h2⟩ + · exact ⟨e, .ep, J.utIpc.ep e he, Or.inl rfl, h1, by simpa [HKind.bits] using h2⟩ + · exact ⟨r, .reply, J.utIpc.reply r hr, Or.inr (Or.inr rfl), h1, by simpa [HKind.bits] using h2⟩ + +/-- An IPC word is in no heap object of a non-IPC kind. -/ +theorem JStage3BA.ipc_off_hobjA {v : U64} {kv : HKind} (hv : H v = some kv) + (hkv : ¬ (kv = .ep ∨ kv = .ntfn ∨ kv = .reply)) {b : U64} (hin : InObj v kv b) : + ¬ IpcObjWA N E Rp b := by + intro h + obtain ⟨p, k, hp, hk, hin2⟩ := J.ipc_hobjPA h + by_cases e : p = v + · subst e; rw [hv] at hp; cases hp; exact hkv hk + · have := J.ent.heap.disj p k v kv hp hv e + unfold InObj at hin hin2 + omega + +end offBA + +section offBA +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} [J7PpA st J7] {s : S} {a : A} {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} + (J : JStage3BA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) +include J + +/-- An IPC word is not in `H.omega ∖ H.frame` (the CSpace, VSpace, table and VCPU words). -/ +theorem JStage3BA.ipc_omegaPA {b : U64} (hΩ : b ∈ H.omega) (hF : b ∉ H.frame) : ¬ IpcObjWA N E Rp b := by + intro h + obtain ⟨p, k, hp, hk, hin⟩ := J.ipc_hobjPA h + obtain ⟨hal, q, kq, hq, hkq, hinq⟩ := hΩ + by_cases e : q = p + · subst e; rw [hp] at hq; cases hq + rcases hk with rfl | rfl | rfl + · rcases hkq with hh | ⟨z, hh⟩ | ⟨z, hh⟩ | ⟨hh, -⟩ <;> cases hh + · rcases hkq with hh | ⟨z, hh⟩ | ⟨z, hh⟩ | ⟨-, hw⟩ <;> try cases hh + exact hF ⟨hal, q, .ntfn, hp, Or.inr (Or.inr ⟨rfl, hw⟩), hin⟩ + · exact hF ⟨hal, q, .reply, hp, Or.inl rfl, hin⟩ + · have := J.ent.heap.disj p k q kq hp hq (Ne.symm e) + unfold InObj at hin hinq + omega + +/-- An IPC word lies in a typed-heap `other` object of the IPC placement. -/ +theorem JStage3BA.ipc_thA {b : U64} (h : IpcObjWA N E Rp b) : + ∃ p len, Z.th p = some (.other len) ∧ p.val ≤ b.val ∧ b.val < p.val + len := by + rcases h with ⟨n, hn, h1, h2⟩ | ⟨e, he, h1, h2⟩ | ⟨r, hr, h1, h2⟩ + · exact ⟨n, 64, J.ipcPlace.notification n hn, h1, h2⟩ + · exact ⟨e, 16, J.ipcPlace.endpoint e he, h1, h2⟩ + · exact ⟨r, 32, J.ipcPlace.reply r hr, h1, h2⟩ + +/-- An IPC word is off the kernel-state block. -/ +theorem JStage3BA.ipc_ksA {b : U64} (h : IpcObjWA N E Rp b) : + b.val < (L.ksb s).val ∨ (L.ksb s).val + ksSizeA ≤ b.val := by + obtain ⟨e, len, he, e1, e2⟩ := J.ipc_thA h + have P := J.sr.place + have := P.ks e (.other len) he (fun hn => by have := P.irqK he hn; cases this) + have hsz : ObjKindA.size (.other len) = len := rfl + omega + +/-- An IPC word is not a word of a typed slot. -/ +theorem JStage3BA.ipc_slot_offA {y b : U64} (hy : IsSlotA Z.th y) (y1 : y.val ≤ b.val) (y2 : b.val < y.val + 32) : + ¬ IpcObjWA N E Rp b := by + intro hw + have P := J.sr.place + obtain ⟨e, len, he, e1, e2⟩ := J.ipc_thA hw + obtain ⟨p, k, hk, p1, p2, hkind⟩ : ∃ p k, Z.th p = some k ∧ p.val ≤ y.val ∧ y.val + 32 ≤ p.val + k.size ∧ + k ≠ .other len := by + obtain ⟨p, ⟨r, hr, i, hi, rfl⟩ | ⟨ht, i, hi, rfl⟩⟩ := hy + · obtain ⟨k, hk, hs, hv⟩ := slotVA_of P hr (j := i) (by rw [cnode_size]; omega) + exact ⟨p, k, hk, by omega, by omega, fun e' => by rw [e', hr] at hk; cases hk⟩ + · obtain ⟨k, hk, hs, hv⟩ := tcb_slotV P ht hi + have : i < 7 := hi + exact ⟨p, k, hk, by omega, by omega, fun e' => by rw [e', ht] at hk; cases hk⟩ + have hne : p ≠ e := fun h => hkind (by subst h; exact Option.some.inj (hk.symm.trans he)) + have := P.apart p e k (.other len) hk he hne + have hsz : ObjKindA.size (.other len) = len := rfl + omega + +/-- **The VSpace, Stage-2 and unmapped-table words are not written** (widened write set). -/ +theorem JStage3BA.vsr_offStepNA {Pt Ub : Set U64} + (vr : VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt) {b : U64} (h : VsR (L.ksb s) Pt b) + (hna : ¬ AsW (L.ksb s) b) : ¬ SchedStepNWA (st := st) N E Rp Z m σ s b := + fun hw => hw.elim (J.vsr_offStepA vr h hna) (J.ipc_omegaPA (vr.om b h) (vr.sg b h)) + +/-- **An `OS` slot word is not written** (widened write set). -/ +theorem JStage3BA.os_offStepNA {b : U64} (hb : Aligned b) (h : InOS OS b) : + ¬ SchedStepNWA (st := st) N E Rp Z m σ s b := by + intro hw + rcases hw with hw | hw + · exact J.os_offStepA hb h hw + · obtain ⟨Q, R', q⟩ := J.ent.bxi.1 + have cap : W7.CapW H.tcbs H.omega H.frame b := inOS_capw J.ent.os hb h + rcases cap with ⟨t, ht, K, hK, e⟩ | ⟨hΩ, hF⟩ + · have ev : b.val = t.val + K.val := by rw [e]; exact field_val (q.tcbs t ht) (K := K) (by omega) + exact J.ipc_off_hobjA (v := t) (kv := .tcb) ht (by simp) + ⟨by omega, by simp only [HKind.bits]; omega⟩ hw + · exact J.ipc_omegaPA hΩ hF hw + +/-- **A word inside a typed slot is not written** (widened write set). -/ +theorem JStage3BA.slot_offStepNA {y b : U64} (hy : IsSlotA Z.th y) (y1 : y.val ≤ b.val) (y2 : b.val < y.val + 32) : + ¬ SchedStepNWA (st := st) N E Rp Z m σ s b := + fun hw => hw.elim (J.slot_offStepA hy y1 y2) (J.ipc_slot_offA hy y1 y2) + +/-- **A word of a TCB at a VCPU link is not written** (widened write set). -/ +theorem JStage3BA.link_offStepNA {t K : U64} (ht : t ∈ Z.T) (hK : K = 3664#u64 ∨ K = 3672#u64) : + ¬ SchedStepNWA (st := st) N E Rp Z m σ s (wa t K) := by + intro hw + rcases hw with hw | hw + · exact J.link_offStepA ht hK hw + · have hfit := (J.sr.core.tcbs t ht).fits + have hKv : K.val = 3664 ∨ K.val = 3672 := by rcases hK with rfl | rfl <;> simp + have hv : (wa t K).val = t.val + K.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have htH : H t = some .tcb := by + have : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + exact this + exact J.ipc_off_hobjA (v := t) (kv := .tcb) htH (by simp) ⟨by omega, by simp only [HKind.bits]; omega⟩ hw + +/-- **An IRQ-state word is not written** (widened write set). -/ +theorem JStage3BA.irq_offStepNA {i : U64} (hi : i.val < 5376) : + ¬ SchedStepNWA (st := st) N E Rp Z m σ s (irqWordA (L.ksb s) i) := by + intro hw + rcases hw with hw | hw + · exact J.irq_offStepA hi hw + · have hv := irqWordA_val J.sr.ksFit hi + have hksz : ksSizeA = 788736 := rfl + have := J.ipc_ksA hw + omega + +/-- **An ASID-table word is not written** (widened write set). -/ +theorem JStage3BA.asid_offStepNA {b : U64} (h1 : (L.ksb s).val + 772352 ≤ b.val) + (h2 : b.val < (L.ksb s).val + 773376) : ¬ SchedStepNWA (st := st) N E Rp Z m σ s b := by + intro hw + rcases hw with hw | hw + · exact J.asid_offStepA h1 h2 hw + · have hksz : ksSizeA = 788736 := rfl + have := J.ipc_ksA hw + omega + +/-- (record edit #8b-B1) A domain-schedule word is not written (widened write set). -/ +theorem JStage3BA.dom_offStepNA {b : U64} (hd : DomW (L.ksb s) b) : + ¬ SchedStepNWA (st := st) N E Rp Z m σ s b := by + intro hw + rcases hw with hw | hw + · exact J.dom_offStepA hd hw + · have hksz : ksSizeA = 788736 := rfl + have := J.ipc_ksA hw + unfold DomW at hd + omega + +/-- (record edit B2) The current core's IPI-pair words are not written (widened write set). -/ +theorem JStage3BA.ipi_offStepNA {o : U64} (ho : o.val = 120 ∨ o.val = 128) : + ¬ SchedStepNWA (st := st) N E Rp Z m σ s (L.nodeAt s (L.cpu s) o) := by + intro hw + rcases hw with hw | hw + · exact J.ipi_offStepA ho hw + · have hksz : ksSizeA = 788736 := rfl + obtain ⟨hv, hcl⟩ := J.sr.toSchedSRN.ipi_val ho + have := J.ipc_ksA hw + omega + +/-- `OSKeep` across a scheduler step that may write IPC objects. -/ +theorem JStage3BA.osKeep_stepNA {s' : S} (w : WritesIn st s s' (SchedStepNWA (st := st) N E Rp Z m σ s)) : + OSKeep st OS s s' := by + intro y hy + have hfit := J.ent.os.fit y hy + have al0 := J.ent.os.al0 hy + have key : ∀ o : U64, o.val < 32 → o.val % 8 = 0 → L.mem s' (wa y o) = L.mem s (wa y o) := by + intro o o1 o2 + have hv : (wa y o).val = y.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have hal : Aligned (wa y o) := J.ent.os.al hy o1 o2 + exact w.mem_eq hal (J.os_offStepNA hal ⟨y, hy, by omega, by omega⟩) + have e0 : wa y 0#u64 = y := by apply UScalar.eq_of_val_eq; rw [wa_valm]; simp + exact ⟨by have := key 0#u64 (by simp) (by simp); rwa [e0] at this, key 8#u64 (by simp) (by simp), + key 16#u64 (by simp) (by simp), key 24#u64 (by simp) (by simp)⟩ + +end offBA + +section offTA +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} [J7PpA st J7] {s : S} {a : A} {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) +include J + + +/-- `JStage3BA.ipc_hobjPA` at `JStage3TA`. -/ +theorem JStage3TA.ipc_hobjPA {b : U64} (h : IpcObjWA N E Rp b) : + ∃ p k, H p = some k ∧ (k = .ep ∨ k = .ntfn ∨ k = .reply) ∧ InObj p k b := + J.toBA.ipc_hobjPA h + +/-- `JStage3BA.ipc_omegaPA` at `JStage3TA`. -/ +theorem JStage3TA.ipc_omegaPA {b : U64} (hΩ : b ∈ H.omega) (hF : b ∉ H.frame) : ¬ IpcObjWA N E Rp b := + J.toBA.ipc_omegaPA hΩ hF + +/-- `JStage3BA.ipc_thA` at `JStage3TA`. -/ +theorem JStage3TA.ipc_thA {b : U64} (h : IpcObjWA N E Rp b) : + ∃ p len, Z.th p = some (.other len) ∧ p.val ≤ b.val ∧ b.val < p.val + len := + J.toBA.ipc_thA h + +/-- `JStage3BA.ipc_ksA` at `JStage3TA`. -/ +theorem JStage3TA.ipc_ksA {b : U64} (h : IpcObjWA N E Rp b) : + b.val < (L.ksb s).val ∨ (L.ksb s).val + ksSizeA ≤ b.val := + J.toBA.ipc_ksA h + +/-- `JStage3BA.ipc_off_hobjA` at `JStage3TA`. -/ +theorem JStage3TA.ipc_off_hobjA {v : U64} {kv : HKind} (hv : H v = some kv) + (hkv : ¬ (kv = .ep ∨ kv = .ntfn ∨ kv = .reply)) {b : U64} (hin : InObj v kv b) : + ¬ IpcObjWA N E Rp b := + J.toBA.ipc_off_hobjA hv hkv hin + +/-- `JStage3BA.ipc_slot_offA` at `JStage3TA`. -/ +theorem JStage3TA.ipc_slot_offA {y b : U64} (hy : IsSlotA Z.th y) (y1 : y.val ≤ b.val) (y2 : b.val < y.val + 32) : + ¬ IpcObjWA N E Rp b := + J.toBA.ipc_slot_offA hy y1 y2 + +/-- `JStage3BA.vsr_offStepNA` at `JStage3TA`. -/ +theorem JStage3TA.vsr_offStepNA {Pt Ub : Set U64} + (vr : VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt) {b : U64} (h : VsR (L.ksb s) Pt b) + (hna : ¬ AsW (L.ksb s) b) : ¬ SchedStepNWA (st := st) N E Rp Z m σ s b := + J.toBA.vsr_offStepNA vr h hna + +/-- `JStage3BA.os_offStepNA` at `JStage3TA`. -/ +theorem JStage3TA.os_offStepNA {b : U64} (hb : Aligned b) (h : InOS OS b) : + ¬ SchedStepNWA (st := st) N E Rp Z m σ s b := + J.toBA.os_offStepNA hb h + +/-- `JStage3BA.slot_offStepNA` at `JStage3TA`. -/ +theorem JStage3TA.slot_offStepNA {y b : U64} (hy : IsSlotA Z.th y) (y1 : y.val ≤ b.val) (y2 : b.val < y.val + 32) : + ¬ SchedStepNWA (st := st) N E Rp Z m σ s b := + J.toBA.slot_offStepNA hy y1 y2 + +/-- `JStage3BA.link_offStepNA` at `JStage3TA`. -/ +theorem JStage3TA.link_offStepNA {t K : U64} (ht : t ∈ Z.T) (hK : K = 3664#u64 ∨ K = 3672#u64) : + ¬ SchedStepNWA (st := st) N E Rp Z m σ s (wa t K) := + J.toBA.link_offStepNA ht hK + +/-- `JStage3BA.irq_offStepNA` at `JStage3TA`. -/ +theorem JStage3TA.irq_offStepNA {i : U64} (hi : i.val < 5376) : + ¬ SchedStepNWA (st := st) N E Rp Z m σ s (irqWordA (L.ksb s) i) := + J.toBA.irq_offStepNA hi + +/-- `JStage3BA.asid_offStepNA` at `JStage3TA`. -/ +theorem JStage3TA.asid_offStepNA {b : U64} (h1 : (L.ksb s).val + 772352 ≤ b.val) + (h2 : b.val < (L.ksb s).val + 773376) : ¬ SchedStepNWA (st := st) N E Rp Z m σ s b := + J.toBA.asid_offStepNA h1 h2 + +/-- `JStage3BA.dom_offStepNA` at `JStage3TA`. -/ +theorem JStage3TA.dom_offStepNA {b : U64} (hd : DomW (L.ksb s) b) : + ¬ SchedStepNWA (st := st) N E Rp Z m σ s b := + J.toBA.dom_offStepNA hd + +/-- `JStage3BA.ipi_offStepNA` at `JStage3TA`. -/ +theorem JStage3TA.ipi_offStepNA {o : U64} (ho : o.val = 120 ∨ o.val = 128) : + ¬ SchedStepNWA (st := st) N E Rp Z m σ s (L.nodeAt s (L.cpu s) o) := + J.toBA.ipi_offStepNA ho + +/-- `JStage3BA.osKeep_stepNA` at `JStage3TA`. -/ +theorem JStage3TA.osKeep_stepNA {s' : S} (w : WritesIn st s s' (SchedStepNWA (st := st) N E Rp Z m σ s)) : + OSKeep st OS s s' := + J.toBA.osKeep_stepNA w + +end offTA + +set_option maxHeartbeats 1000000 in +/-- **`JStage3A` across a scheduler or IPC-object step that may also write words `X`** off the footprint (`XOffA`), +with user memory kept on the free untyped (J10): `of_schedWrN`'s body. -/ +theorem JStage3BA.of_schedWrX {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J5 J7 : JSlotA S} [J7PpA st J7] {s s' : S} + {a a' : A} {σ σ' : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3BA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {stack' : U64 → List U64} + {Q : U64 → U64 → List U64} {Rl : U64 → List U64} {curOf curScOf : Fl4SpecA64.CoreId → Fl4SpecA64.ObjRef} + {X : U64 → Prop} (XO : XOffA (st := st) Z H OS s X) + (hXvs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → ¬ X b) (bufAl : UtBufAlA Z.T σ') + (w : WritesIn st s s' (fun b => SchedStepNWA (st := st) N E Rp Z m σ s b ∨ X b)) (HS : SpecSchedA σ σ') + (SSz : SSzA σ σ') (Qk : QKeepA σ σ') + (hz : ∀ x : Fl4SpecA64.Word, UtFreeA st OS s (fun d => d = false) (fl4Ptr x).val → + σ'.machineState.underlyingMemory x = σ.machineState.underlyingMemory x) (har : AR a' Z.hw) + (hcurOf : ∀ k, k ≠ m.curCore → curOf k = Z.curOf k) + (ent : EntryInvA3 st H Z.Cs OS U Ac Pp Tb Lv s') + (sq : SchedQueuesInv st H.tcbs Z.Cs H.omega H s' Q Rl) + (poolOff : PoolOff st H Z.Cs OS s') + (sr : SchedSRA st { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } m κ s' σ') + (scCov : ScCovA Z.C m σ') + (ep : EpStateRel Z.T E Rp Bf (L.mem s') σ') + (ntfn : NtfnStateRel N Z.T (L.mem s') σ') + (reply : ReplyStackRelA (L.mem s') Rp Z.C σ' (scNameA m) stack') + (bufCover : BufCover st Z.T Bf s' σ') + (idleNoFault : IdleNoFaultA m σ') + (mcp : McpRelA Z.T m (L.mem s') σ') + (yieldRel : YieldRelA Z.T Z.C m (L.mem s') σ') + (sinv : SInvXA σ' m n) + (sidle : SIdleA σ' m n) + (runVcpu : RunVcpuInv st Z.T s') (hvr : ∀ sc ∈ Z.C, ∀ r ∈ stack' sc, RBlk st s' r) + (blkQ : BlkQueuedA Z.T E N (L.mem s') σ') + (j5 : J5 { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } H OS s' σ' m) + (j7 : J7 { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } H OS s' σ' m) + (hsub : ∀ c, ∀ r ∈ stack' c, r ∈ stack c := by exact fun _ _ h => h) : + JStage3BA st AR { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } H OS U Ac Pp Tb Lv ia ie N E Rp + Bf stack' κ n J5 J7 s' a' σ' m := by + have hk := w.1 + have hc := w.2.1 + have osk : OSKeep st OS s s' := OSKeep.of_off J.ent.os w fun y hy o o1 o2 h => by + have hv : (wa y o).val = y.val + o.val := by + have := J.ent.os.fit y hy + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have hin : InOS OS (wa y o) := ⟨y, hy, by omega, by omega⟩ + exact h.elim (J.os_offStepNA (J.ent.os.al hy o1 o2) hin) fun hx => XO.os _ hx hin + have capE : ∀ u ∈ OS, capA st s' u = capA st s u := fun u hu => by + unfold capA; rw [(osk u hu).1, (osk u hu).2.1] + have keep : ∀ b, Aligned b → ¬ SchedStepNWA (st := st) N E Rp Z m σ s b → ¬ X b → L.mem s' b = L.mem s b := + fun b hb hn hx => w.mem_eq hb fun h => h.elim hn hx + obtain ⟨Pt, Ub, vr, arm, link, apart⟩ := J7PpA.core (st := st) _ _ _ _ _ _ J.j7 + have P := J.sr.place + have hT : ∀ t ∈ Z.T, t ∈ H.tcbs := fun t ht => by rw [J.tcbs]; exact ht + -- the page words + have vsr : ∀ b, VsR (L.ksb s) Pt b → ¬ X b → L.mem s' b = L.mem s b := fun b hb hx => by + refine keep _ (vr.al _ hb) ?_ hx + by_cases h : AsW (L.ksb s) b + · obtain ⟨o, o1, o2, -, e⟩ := h + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.ent.ksw + have ev : b.val = (L.ksb s).val + o.val := by + rw [e, wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact J.asid_offStepNA (by omega) (by omega) + · exact J.vsr_offStepNA vr hb h + have vsfoot : ∀ b, VsFootA Z.Pp Z.S1 Z.Y (L.ksb s) b → L.mem s' b = L.mem s b := fun b hb => by + refine vsr b ?_ (XO.vs b hb) + rcases hb.vsR with ⟨p, hp, h⟩ | h + · refine Or.inl ⟨p, link p ?_, h⟩ + rcases hp with hp | hp | hp + · exact Or.inl hp + · exact Or.inr (Or.inl hp) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl hp)))))) + · exact Or.inr h + have word : ∀ p, SchedPageA st Z OS s p → ∀ i : U64, i.val < 512 → L.mem s' (peAF p i) = L.mem s (peAF p i) := + fun p hp i hi => by + refine vsr _ (Or.inl ⟨p, link p hp, core.num.U64.wrapping_mul i 8#u64, ?_, ?_, rfl⟩) + (XO.page p hp (core.num.U64.wrapping_mul i 8#u64) (by rw [wm8_valA hi]; omega) (by rw [wm8_valA hi]; omega)) + · rw [wm8_valA hi]; omega + · rw [wm8_valA hi]; omega + have extfoot : ∀ b, ExtFootE Z.X Z.Y Z.T2 b → L.mem s' b = L.mem s b := fun b hb => by + cases hb with + | s2lo r i hr hi => exact word r (Or.inr (Or.inr (Or.inr (Or.inl hr)))) i hi + | s2hi r i hr hi => exact word _ (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨r, hr, rfl⟩))))) i hi + | s2pt t i ht hi => exact word t (Or.inr (Or.inr (Or.inl ht))) i hi + | wl0 y hy => exact word y (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl hy)))))) _ (by simp) + | wlo y i hy hi => exact word y (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl hy)))))) i (by omega) + -- the VCPU words + have vcw : ∀ v ∈ Z.V, ∀ o : U64, o.val = 0 ∨ o.val = 24 → L.mem s' (wa v o) = L.mem s (wa v o) := by + intro v hv o ho + have hH := arm.vh v hv + have hfit := J.ent.heap.fits v _ hH + have hal := J.ent.heap.align v _ hH + simp only [HKind.bits] at hfit hal + have ev : (wa v o).val = v.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have al : Aligned (wa v o) := by show _ % 8 = 0; rw [ev]; omega + have inObj : InObj v (.other 12) (wa v o) := by + unfold InObj; simp only [HKind.bits]; omega + have hsc : ∀ c ∈ H.scs, ¬ InSC c (H.scBits c) (wa v o) := by + rintro c ⟨z, hz⟩ ⟨c1, c2⟩ + have e : H.scBits c = z := by unfold Heap.scBits; rw [hz] + have ne : v ≠ c := fun h => by rw [h, hz] at hH; cases hH + have := J.ent.heap.disj v _ c _ hH hz ne + simp only [HKind.bits] at this + rw [e] at c2 + omega + have hg : ∀ g : U64, g.val < 223488 → wa (L.ksb s) g ≠ wa v o := by + intro g hg e + exact apart _ ⟨v, hv, o, by omega, by omega, rfl⟩ g hg e + exact keep _ al (fun hw => hw.elim + (J.omega_offStepA ⟨al, v, _, hH, Or.inr (Or.inr (Or.inl ⟨12, rfl⟩)), inObj⟩ hsc hg) + (J.ipc_off_hobjA hH (by simp) inObj)) (XO.vcw v hv o ho) + have slotw : ∀ y, IsSlotA Z.th y → ∀ x, (x = y ∨ x = wa y 8#u64 ∨ x = wa y 16#u64 ∨ x = wa y 24#u64) → + L.mem s' x = L.mem s x := by + intro y hy x hx + obtain ⟨a32, f32⟩ := hy.al32 P + have hv : ∀ o : U64, o.val < 32 → (wa y o).val = y.val + o.val := fun o ho => by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have key : x.val % 8 = 0 ∧ y.val ≤ x.val ∧ x.val < y.val + 32 := by + rcases hx with rfl | rfl | rfl | rfl + · omega + all_goals + rw [hv _ (by simp)]; simp; omega + exact keep _ key.1 (J.slot_offStepNA hy key.2.1 key.2.2) (XO.slot y _ hy key.2.1 key.2.2) + have cnw : ∀ y, SlotWA Z.th y → IsSlotA Z.th y := fun y hy => by + rcases hy with h | h + · exact h + · obtain ⟨p, r, hp, idx, hl, rfl⟩ := h + obtain ⟨e, hb, -⟩ := cn_slotV P hp hl + exact ⟨p, Or.inl ⟨r, hp, blvA idx, hb, e⟩⟩ + have tcbl : ∀ t ∈ Z.T, ∀ K : U64, K = 3664#u64 ∨ K = 3672#u64 → L.mem s' (wa t K) = L.mem s (wa t K) := by + intro t ht K hK + have hta := J.sr.core.tcbs t ht + refine keep _ (field_aligned (K := K) hta ?_ ?_) (J.link_offStepNA ht hK) (XO.link t ht K hK) + all_goals rcases hK with rfl | rfl <;> simp + have mem : SchedKeepMemA (st := st) Z s s' := + { ksb := hk + cpu := hc + slot := fun y hy x hx => slotw y (cnw y hy) x hx + tcbW := fun t ht => ⟨tcbl t ht _ (Or.inl rfl), tcbl t ht _ (Or.inr rfl)⟩ + vcW := fun v hv => by + have e0 : wa v 0#u64 = v := by apply UScalar.eq_of_val_eq; rw [wa_valm]; simp + have h0 := vcw v hv 0#u64 (Or.inl (by simp)) + rw [e0] at h0 + exact ⟨h0, vcw v hv _ (Or.inr (by simp))⟩ + irqW := fun i hi => by + refine keep _ ?_ (J.irq_offStepNA hi) (XO.irq i hi) + have := irqWordA_val J.sr.ksFit hi + have := J.sr.toSchedSRN.ksb_al + show _ % 8 = 0 + omega + vs := vsfoot + ext := extfoot } + -- the node cursors + have nodes : ∀ c ∈ Z.Cs, + L.mem s' (L.nodeAt s' c layout.node.IDLE_THREAD) ∈ Z.T ∧ L.mem s' (L.nodeAt s' c layout.node.IDLE_SC) ∈ Z.C ∧ + L.mem s' (L.nodeAt s' c layout.node.CUR_THREAD) ∈ Z.T := by + intro c hcs + refine ⟨?_, ?_, ?_⟩ + · have e : L.mem s' (L.nodeAt s' c layout.node.IDLE_THREAD) = L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) := + specPtr_inj ((sr.sch.idleOf c hcs).symm.trans (J.sr.sch.idleOf c hcs)) + rw [e]; exact J.j.1.idleT c hcs + · have e : L.mem s' (L.nodeAt s' c layout.node.IDLE_SC) = L.mem s (L.nodeAt s c layout.node.IDLE_SC) := + specPtr_inj ((sr.sch.idleScOf c hcs).symm.trans (J.sr.sch.idleScOf c hcs)) + rw [e]; exact J.j.1.idleC c hcs + · by_cases h : c = L.cpu s + · have := sr.curT + unfold LawfulStore.curW at this + rw [hc, ← h] at this + exact this + · have hne : c.val ≠ m.curCore := fun e => h (UScalar.eq_of_val_eq (e.trans J.sr.sch.core)) + have h1 := sr.sch.curOfW c hcs + rw [show ({ Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } : XSetsA).curOf = curOf from rfl, + hcurOf _ hne] at h1 + have e : L.mem s' (L.nodeAt s' c layout.node.CUR_THREAD) = L.mem s (L.nodeAt s c layout.node.CUR_THREAD) := + specPtr_inj (h1.symm.trans (J.sr.sch.curOfW c hcs)) + rw [e]; exact J.j.1.curT c hcs + have st' : StEqA Z.T (L.mem s') σ' := fun _ ht _ hx => (ep.at_tcb ht hx).st + have jx := JAtZAX.of_sched J.j (a' := a') (Q := Q) (R := Rl) (curOf := curOf) (curScOf := curScOf) mem HS har + (sr.sch.withSt st') sr.rest (fun c h => (nodes c h).1) (fun c h => (nodes c h).2.1) + (fun c h => (nodes c h).2.2) sr.cscAll + have free : ∀ dev x, UtFreeA st OS s' dev x → UtFreeA st OS s dev x := by + rintro dev x ⟨u, hu, q, b, fi, d, hcap, h⟩ + exact ⟨u, hu, q, b, fi, d, (capE u hu).symm.trans hcap, h⟩ + have dead : ∀ k p, DeadA st OS k s' p → DeadA st OS k s p := fun k p h u hu hp => by + have := h u hu + rw [capE u hu] at this + exact this hp + have nsc : ∀ {p : Fl4SpecA64.ObjRef}, σ.kheap p = none → σ'.kheap p = none := fun {p} h => by + rcases HS.kh p with e | ⟨x, _, e1, -⟩ | ⟨o, o', e1, -, -⟩ + · rw [e]; exact h + · rw [h] at e1; cases e1 + · rw [h] at e1; cases e1 + have keepE : ∀ {q : Fl4SpecA64.ObjRef} {e : Fl4SpecA64.ExtArchObj Fl4SpecA64.PTE}, + σ.kheap q = some (.archObj (.ext e)) → σ'.kheap q = some (.archObj (.ext e)) := fun h1 => + HS.keep h1 (fun _ e => by cases e) (fun e => by rcases e with ⟨_, _, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ <;> cases e) + have unmS2 : ∀ u ∈ OS, ∀ p a v, capA st s u = .Ept p 0#u64 false a v → UnmS2RootA st s σ p → + UnmS2RootA st s' σ' p := by + intro u hu p a v hc U + obtain ⟨es, h1, h2⟩ := U.spec + refine { inv := fun i hi => ?_, spec := ⟨es, keepE h1, h2⟩ } + by_cases hlo : i.val < 512 + · rw [word p (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inr (Or.inl ⟨a, v, hc⟩))⟩))))))) i hlo] + exact U.inv i hi + · obtain ⟨j, hj, e⟩ := peAF_hiA p i (by omega) hi + rw [e, word _ (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inr (Or.inr (Or.inl ⟨p, a, v, hc, rfl⟩)))⟩))))))) j hj, ← e] + exact U.inv i hi + have unmHv : ∀ u ∈ OS, ∀ y a, capA st s u = .HighVSpace y false a → UnmHiRootA st s σ y → + UnmHiRootA st s' σ' y := by + intro u hu y a hc U + obtain ⟨l1, h1, h2⟩ := U.spec + exact { inv := fun i hi => by + rw [word _ (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inr (Or.inr (Or.inr ⟨y, a, hc, rfl⟩)))⟩))))))) i hi] + exact U.inv i hi + spec := ⟨l1, keepE h1, h2⟩ } + have unm : ∀ ty p, SchedPageA st Z OS s p → UnmTabA st Z m s σ ty p → + UnmTabA st { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } m s' σ' ty p := by + intro ty p hp U1 + obtain ⟨tb, h1, h2, h3⟩ := U1.spec + exact + { inv := fun i hi => by rw [word p hp i hi]; exact U1.inv i hi + spec := ⟨tb, HS.keep h1 (fun _ e => by cases e) (fun e => by rcases e with ⟨_, _, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ <;> cases e), h2, h3⟩ + off := ⟨fun i hi h => U1.off.mem i hi (footA_schedA hk hc h), U1.off.obj⟩ } + exact + { ent := ent + j := jx + tcbs := J.tcbs + scs := J.scs + sq := sq + ap := by + have e : L.ksb s' = L.ksb s := hk + rw [e]; exact { J.ap with } + pp := J.pp + s1 := J.s1 + ko := by rw [hk]; exact J.ko + ptH := J.ptH + tabInj := by rw [HS.asid]; exact J.tabInj + poolOff := poolOff + objAlloc := ObjAllocA.keep (Z := Z) J.objAlloc osk + idleAnchor := IdleAnchorOffA.keep J.idleAnchor osk + vsVc := by rw [hk]; exact J.vsVc + sr := sr + scCov := scCov + scSize := fun c hcm x k h => by + obtain ⟨y, hy⟩ := SSz _ x k h + exact J.scSize c hcm y k hy + ep := ep + ntfn := ntfn + reply := reply + bufCover := bufCover + bufNative := fun t ht => by + have hsl : IsSlotA Z.th (slotAddrA (specPtr t) (BitVec.ofNat 64 2)) := + ⟨t, Or.inr ⟨(J.sr.thT t).2 ht, 2, by unfold tcbSlotsA; omega, rfl⟩⟩ + have e : capAtA (L.mem s') (slotAddrA (specPtr t) (BitVec.ofNat 64 2)) = + capAtA (L.mem s) (slotAddrA (specPtr t) (BitVec.ofNat 64 2)) := by + unfold capAtA + rw [slotw _ hsl _ (Or.inl rfl), slotw _ hsl _ (Or.inr (Or.inl rfl))] + rw [e]; exact J.bufNative t ht + iplaced := J.iplaced + ipcPlace := J.ipcPlace + idleNoFault := idleNoFault + mcp := mcp + yieldRel := yieldRel + sinv := sinv + sidle := sidle + setsInH := { J.setsInH with } + utIpc := J.utIpc + ipcTh := J.ipcTh + deadExt := + { vcpu := fun v hv hd x hx => J.deadExt.vcpu v hv (dead _ v hd) x + (HS.back hx (fun _ e => by cases e) (fun e => by rcases e with ⟨_, _, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ <;> cases e)) + reply := fun r hr hd c hc hm => J.deadExt.reply r hr (dead _ r hd) c hc (hsub c r hm) + asid := fun i p h hd => J.deadExt.asid i p (by rw [HS.asid] at h; exact h) (dead _ p hd) + tvcpu := fun t ht hd => by + rw [tcbl t ht 3664#u64 (Or.inl rfl)] + exact J.deadExt.tvcpu t ht (dead _ t hd) + vcDeadRun := fun v hv hd => by + rw [vcw v hv 24#u64 (Or.inr (by simp))] + exact J.deadExt.vcDeadRun v hv (dead _ v hd) + replyTcb := fun r hr hd x hx => by + obtain ⟨y, hy, k⟩ := Qk.rt _ x hx + exact k (J.deadExt.replyTcb r hr (dead _ r hd) y hy) } + specFree := + { heap := fun p h => nsc (J.specFree.heap p (free _ _ h)) + cdt := fun x y h p hf => J.specFree.cdt x y (by rw [HS.cdt] at h; exact h) p (free _ _ hf) + orig := fun x h p hf => J.specFree.orig x (by rw [HS.orig] at h; exact h) p (free _ _ hf) } + utBlk := fun u hu q b fi d h => J.utBlk u hu q b fi d ((capE u hu).symm.trans h) + unmPt := + { pt := fun u hu p asid va h => by + have h0 := (capE u hu).symm.trans h + exact unm _ p (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inl ⟨_, _, _, h0⟩⟩))))))) (J.unmPt.pt u hu p asid va h0) + vs := fun u hu p asid h => by + have h0 := (capE u hu).symm.trans h + exact unm _ p (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inl ⟨_, _, h0⟩)⟩))))))) (J.unmPt.vs u hu p asid h0) + s2 := fun u hu p asid va h => by + have h0 := (capE u hu).symm.trans h + exact unmS2 u hu p asid va h0 (J.unmPt.s2 u hu p asid va h0) + hv := fun u hu p asid h => by + have h0 := (capE u hu).symm.trans h + exact unmHv u hu p asid h0 (J.unmPt.hv u hu p asid h0) } + tabS1 := fun q hq => + ⟨fun i hi h => (J.tabS1 q hq).mem i hi (footA_schedA hk hc h), (J.tabS1 q hq).obj⟩ + tabExt := + ⟨fun q hq i hi h => J.tabExt.mem q hq i hi (by + exact footA_schedA (Z := extZA Z) hk hc h), J.tabExt.obj⟩ + specZero := fun x h => (hz x (free _ _ h)).trans (J.specZero x (free _ _ h)) + origLive := fun x h => J.origLive x (by rw [HS.orig] at h; exact h) + nullOrig := fun sl h => by + rw [HS.orig] + exact J.nullOrig sl (by rw [← SpecSchedA.specCapAt HS sl]; exact h) + thPos := fun p z h => J.thPos p z h + frBase := J.frBase.keepA capE + devFr := J.devFr.keepA capE fun p dev pg h => HS.keep h (fun _ e => by cases e) + (fun e => by rcases e with ⟨_, _, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ <;> cases e) + finCov := J.finCov.keepA + (fun q r cs h => HS.back h (fun _ e => by cases e) + (fun e => by rcases e with ⟨_, _, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ <;> cases e)) + (fun q x h => HS.tcbBack h) + idleAll := J.idleAll.keepA + (fun c hcs => specPtr_inj ((sr.sch.idleOf c hcs).symm.trans (J.sr.sch.idleOf c hcs))) + (fun c hcs => specPtr_inj ((sr.sch.idleScOf c hcs).symm.trans (J.sr.sch.idleScOf c hcs))) + ipcHeapCov := J.ipcHeapCov + zombieNoVcpu := J.zombieNoVcpu.keepA capE J.zombieTcb + fun t ht => tcbl t (J.tcbs ▸ ht) 3664#u64 (Or.inl rfl) + zombieTcb := J.zombieTcb.keepA capE + bufOff := J.bufOff.keepA (Z := Z) (Z' := { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf }) + hk rfl rfl rfl rfl rfl rfl rfl capE + vsOk := by + have e := (utVsPt_keepA J.ent.vs hk (fun b hb => by + refine keep b (J.vsOk.al b hb) ?_ (hXvs b hb) + by_cases h : AsW (L.ksb s) b + · obtain ⟨o, o1, o2, -, e⟩ := h + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.ent.ksw + have ev : b.val = (L.ksb s).val + o.val := by + rw [e, wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact J.asid_offStepNA (by omega) (by omega) + · exact J.vsr_offStepNA J.vsOk hb h)).2 + unfold UtVsOkA + rw [show L.ksb s' = L.ksb s from hk, e] + exact J.vsOk + domOK := J.domOK.keepS J.sr.toSchedSRN Qk.dom hk hc (fun b hb hd => w.mem_eq hb fun h => h.elim (J.dom_offStepNA hd) (XO.dom b hd)) + ipiZero := J.ipiKeepOf w fun o ho h => h.elim (J.ipi_offStepNA ho) (XO.ipi o ho) + bufVs := by + have e := (utVsPt_keepA J.ent.vs hk (fun b hb => by + refine keep b (J.vsOk.al b hb) ?_ (hXvs b hb) + by_cases h : AsW (L.ksb s) b + · obtain ⟨o, o1, o2, -, e⟩ := h + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.ent.ksw + have ev : b.val = (L.ksb s).val + o.val := by + rw [e, wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact J.asid_offStepNA (by omega) (by omega) + · exact J.vsr_offStepNA J.vsOk hb h)).2 + rw [show L.ksb s' = L.ksb s from hk, e] + exact J.bufVs + bufAl := bufAl + zombieBnd := J.zombieBnd.keepA capE + devUtOff := J.devUtOff.keepA (Z := Z) capE hk (fun _ _ h => h) rfl + capTy := J.capTy.keepA capE + scCpu := J.scCpu + scNZ := J.scNZ + vcRun := J.vcRun.keepA (fun t ht => tcbl t ht 3664#u64 (Or.inl rfl)) (fun v hv => vcw v hv 24#u64 (Or.inr (by simp))) + (fun v hv => by + have h := vcw v hv 0#u64 (Or.inl (by simp)) + rwa [wa_zeroVcA] at h) + (fun t ht => tcbl t ht 3672#u64 (Or.inr rfl)) + runVcpu := runVcpu + vr := hvr + blkQ := blkQ + ciSc := fun u hu p b h => J.ciSc u hu p b ((capE u hu).symm.trans h) + curVcpuNone := HS.vcur.trans J.curVcpuNone + ipcSorted := J.ipcSorted.of_q Qk + activeReplyScs := J.activeReplyScs.of_q Qk + j5 := j5 + j7 := j7 } + +/-- `JStage3BA.of_schedWrX` at `JStage3TA`. -/ +theorem JStage3TA.of_schedWrX {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J5 J7 : JSlotA S} [J7PpA st J7] {s s' : S} + {a a' : A} {σ σ' : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {Q : U64 → U64 → List U64} {Rl : U64 → List U64} {curOf curScOf : Fl4SpecA64.CoreId → Fl4SpecA64.ObjRef} + {X : U64 → Prop} (XO : XOffA (st := st) Z H OS s X) + (hXvs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → ¬ X b) (bufAl : UtBufAlA Z.T σ') + (w : WritesIn st s s' (fun b => SchedStepNWA (st := st) N E Rp Z m σ s b ∨ X b)) (HS : SpecSchedA σ σ') + (SSz : SSzA σ σ') (Qk : QKeepA σ σ') + (hz : ∀ x : Fl4SpecA64.Word, UtFreeA st OS s (fun d => d = false) (fl4Ptr x).val → + σ'.machineState.underlyingMemory x = σ.machineState.underlyingMemory x) (har : AR a' Z.hw) + (hcurOf : ∀ k, k ≠ m.curCore → curOf k = Z.curOf k) + (ent : EntryInvA3 st H Z.Cs OS U Ac Pp Tb Lv s') + (sq : SchedQueuesInv st H.tcbs Z.Cs H.omega H s' Q Rl) + (poolOff : PoolOff st H Z.Cs OS s') + (sr : SchedSRA st { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } m κ s' σ') + (scCov : ScCovA Z.C m σ') + (ep : EpStateRel Z.T E Rp Bf (L.mem s') σ') + (ntfn : NtfnStateRel N Z.T (L.mem s') σ') + (reply : ReplyStackRelA (L.mem s') Rp Z.C σ' (scNameA m) stack) + (bufCover : BufCover st Z.T Bf s' σ') + (idleNoFault : IdleNoFaultA m σ') + (mcp : McpRelA Z.T m (L.mem s') σ') + (yieldRel : YieldRelA Z.T Z.C m (L.mem s') σ') + (sinv : SInvXA σ' m n) + (sidle : SIdleA σ' m n) + (runVcpu : RunVcpuInv st Z.T s') (hvr : ∀ sc ∈ Z.C, ∀ r ∈ stack sc, RBlk st s' r) + (noSwitch : ∀ u, σ'.schedulerAction ≠ .switchThread u) + (sact : SActA σ') + (blkQ : BlkQueuedA Z.T E N (L.mem s') σ') + (j5 : J5 { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } H OS s' σ' m) + (j7 : J7 { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } H OS s' σ' m) : + JStage3TA st AR { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } H OS U Ac Pp Tb Lv ia ie N E Rp + Bf stack κ n J5 J7 s' a' σ' m := + (JStage3BA.of_schedWrX J.toBA (curOf := curOf) (curScOf := curScOf) XO hXvs bufAl w HS SSz Qk hz har hcurOf ent sq poolOff sr scCov ep ntfn reply bufCover idleNoFault mcp yieldRel sinv sidle runVcpu hvr (blkQ := blkQ) j5 j7).toTA noSwitch sact + +set_option maxHeartbeats 1000000 in +/-- **`JStage3A` across a scheduler or IPC-object step** (module docstring): the write set is `SchedStepNWA`. -/ +theorem JStage3BA.of_schedWrN {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J5 J7 : JSlotA S} [J7PpA st J7] {s s' : S} + {a a' : A} {σ σ' : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3BA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {stack' : U64 → List U64} + {Q : U64 → U64 → List U64} {Rl : U64 → List U64} {curOf curScOf : Fl4SpecA64.CoreId → Fl4SpecA64.ObjRef} + (w : WritesIn st s s' (SchedStepNWA (st := st) N E Rp Z m σ s)) (HS : SpecSchedA σ σ') (SSz : SSzA σ σ') (Qk : QKeepA σ σ') + (hum : σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory) (har : AR a' Z.hw) + (hcurOf : ∀ k, k ≠ m.curCore → curOf k = Z.curOf k) (bufAl : UtBufAlA Z.T σ') + (ent : EntryInvA3 st H Z.Cs OS U Ac Pp Tb Lv s') + (sq : SchedQueuesInv st H.tcbs Z.Cs H.omega H s' Q Rl) + (poolOff : PoolOff st H Z.Cs OS s') + (sr : SchedSRA st { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } m κ s' σ') + (scCov : ScCovA Z.C m σ') + (ep : EpStateRel Z.T E Rp Bf (L.mem s') σ') + (ntfn : NtfnStateRel N Z.T (L.mem s') σ') + (reply : ReplyStackRelA (L.mem s') Rp Z.C σ' (scNameA m) stack') + (bufCover : BufCover st Z.T Bf s' σ') + (idleNoFault : IdleNoFaultA m σ') + (mcp : McpRelA Z.T m (L.mem s') σ') + (yieldRel : YieldRelA Z.T Z.C m (L.mem s') σ') + (sinv : SInvXA σ' m n) + (sidle : SIdleA σ' m n) + (runVcpu : RunVcpuInv st Z.T s') (hvr : ∀ sc ∈ Z.C, ∀ r ∈ stack' sc, RBlk st s' r) + (blkQ : BlkQueuedA Z.T E N (L.mem s') σ') + (j5 : J5 { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } H OS s' σ' m) + (j7 : J7 { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } H OS s' σ' m) + (hsub : ∀ c, ∀ r ∈ stack' c, r ∈ stack c := by exact fun _ _ h => h) : + JStage3BA st AR { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } H OS U Ac Pp Tb Lv ia ie N E Rp + Bf stack' κ n J5 J7 s' a' σ' m := + JStage3BA.of_schedWrX J (hsub := hsub) (X := fun _ => False) + ⟨fun _ h => h.elim, fun _ _ _ _ _ h => h, fun _ _ _ _ h => h, fun _ _ h => h, fun _ _ h => h, + fun _ _ _ _ _ h => h, fun _ _ _ _ h => h, fun _ _ h => h, fun _ _ h => h⟩ + (fun _ _ h => h) bufAl + (w.mono fun _ h => Or.inl h) HS SSz Qk (fun x _ => by rw [hum]) har hcurOf ent sq poolOff sr scCov ep ntfn reply + bufCover idleNoFault mcp yieldRel sinv sidle runVcpu hvr (blkQ := blkQ) j5 j7 + +/-- `JStage3BA.of_schedWrN` at `JStage3TA`. -/ +theorem JStage3TA.of_schedWrN {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J5 J7 : JSlotA S} [J7PpA st J7] {s s' : S} + {a a' : A} {σ σ' : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {Q : U64 → U64 → List U64} {Rl : U64 → List U64} {curOf curScOf : Fl4SpecA64.CoreId → Fl4SpecA64.ObjRef} + (w : WritesIn st s s' (SchedStepNWA (st := st) N E Rp Z m σ s)) (HS : SpecSchedA σ σ') (SSz : SSzA σ σ') (Qk : QKeepA σ σ') + (hum : σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory) (har : AR a' Z.hw) + (hcurOf : ∀ k, k ≠ m.curCore → curOf k = Z.curOf k) (bufAl : UtBufAlA Z.T σ') + (ent : EntryInvA3 st H Z.Cs OS U Ac Pp Tb Lv s') + (sq : SchedQueuesInv st H.tcbs Z.Cs H.omega H s' Q Rl) + (poolOff : PoolOff st H Z.Cs OS s') + (sr : SchedSRA st { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } m κ s' σ') + (scCov : ScCovA Z.C m σ') + (ep : EpStateRel Z.T E Rp Bf (L.mem s') σ') + (ntfn : NtfnStateRel N Z.T (L.mem s') σ') + (reply : ReplyStackRelA (L.mem s') Rp Z.C σ' (scNameA m) stack) + (bufCover : BufCover st Z.T Bf s' σ') + (idleNoFault : IdleNoFaultA m σ') + (mcp : McpRelA Z.T m (L.mem s') σ') + (yieldRel : YieldRelA Z.T Z.C m (L.mem s') σ') + (sinv : SInvXA σ' m n) + (sidle : SIdleA σ' m n) + (runVcpu : RunVcpuInv st Z.T s') (hvr : ∀ sc ∈ Z.C, ∀ r ∈ stack sc, RBlk st s' r) + (noSwitch : ∀ u, σ'.schedulerAction ≠ .switchThread u) + (sact : SActA σ') + (blkQ : BlkQueuedA Z.T E N (L.mem s') σ') + (j5 : J5 { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } H OS s' σ' m) + (j7 : J7 { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } H OS s' σ' m) : + JStage3TA st AR { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } H OS U Ac Pp Tb Lv ia ie N E Rp + Bf stack κ n J5 J7 s' a' σ' m := + (JStage3BA.of_schedWrN J.toBA (curOf := curOf) (curScOf := curScOf) w HS SSz Qk hum har hcurOf bufAl ent sq poolOff sr scCov ep ntfn reply bufCover idleNoFault mcp yieldRel sinv sidle runVcpu hvr (blkQ := blkQ) j5 j7).toTA noSwitch sact + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3SchedWrSTA.lean b/proofs/Fl4ProofsA64/P3/Stage3SchedWrSTA.lean new file mode 100644 index 000000000..11c357bd1 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SchedWrSTA.lean @@ -0,0 +1,447 @@ +/- +P3 (AArch64, delete hooks W3 Reply): **`of_schedWrX` with a shrunk reply stack** (`of_schedWrSX` / `of_schedWrSN`): +`P3/Stage3SchedWrNTA`'s body with a new `stack'` whose every list is contained in the old one (`reply_remove`). Only +`reply` and `deadExt.reply` mention the stack. The active SC set is the new `ent`'s (`Ac'`; the SC finaliser +deactivates its SC). No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3SchedWrNTA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +set_option maxHeartbeats 1000000 in +theorem JStage3TA.of_schedWrSX {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J5 J7 : JSlotA S} [J7PpA st J7] {s s' : S} + {a a' : A} {σ σ' : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + {Ac' : Set U64} + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {stack' : U64 → List U64} (hsub : ∀ c, ∀ r ∈ stack' c, r ∈ stack c) + {Q : U64 → U64 → List U64} {Rl : U64 → List U64} {curOf curScOf : Fl4SpecA64.CoreId → Fl4SpecA64.ObjRef} + {X : U64 → Prop} (XO : XOffA (st := st) Z H OS s X) + (hXvs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → ¬ X b) (bufAl : UtBufAlA Z.T σ') + (w : WritesIn st s s' (fun b => SchedStepNWA (st := st) N E Rp Z m σ s b ∨ X b)) (HS : SpecSchedA σ σ') + (SSz : SSzA σ σ') (Qk : QKeepA σ σ') + (hz : ∀ x : Fl4SpecA64.Word, UtFreeA st OS s (fun d => d = false) (fl4Ptr x).val → + σ'.machineState.underlyingMemory x = σ.machineState.underlyingMemory x) (har : AR a' Z.hw) + (hcurOf : ∀ k, k ≠ m.curCore → curOf k = Z.curOf k) + (ent : EntryInvA3 st H Z.Cs OS U Ac' Pp Tb Lv s') + (sq : SchedQueuesInv st H.tcbs Z.Cs H.omega H s' Q Rl) + (poolOff : PoolOff st H Z.Cs OS s') + (sr : SchedSRA st { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } m κ s' σ') + (scCov : ScCovA Z.C m σ') + (ep : EpStateRel Z.T E Rp Bf (L.mem s') σ') + (ntfn : NtfnStateRel N Z.T (L.mem s') σ') + (reply : ReplyStackRelA (L.mem s') Rp Z.C σ' (scNameA m) stack') + (bufCover : BufCover st Z.T Bf s' σ') + (idleNoFault : IdleNoFaultA m σ') + (mcp : McpRelA Z.T m (L.mem s') σ') + (yieldRel : YieldRelA Z.T Z.C m (L.mem s') σ') + (sinv : SInvXA σ' m n) + (sidle : SIdleA σ' m n) + (runVcpu : RunVcpuInv st Z.T s') (hvr : ∀ sc ∈ Z.C, ∀ r ∈ stack' sc, RBlk st s' r) + (noSwitch : ∀ u, σ'.schedulerAction ≠ .switchThread u) + (sact : SActA σ') + (blkQ : BlkQueuedA Z.T E N (L.mem s') σ') + (j5 : J5 { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } H OS s' σ' m) + (j7 : J7 { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } H OS s' σ' m) : + JStage3TA st AR { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } H OS U Ac' Pp Tb Lv ia ie N E Rp + Bf stack' κ n J5 J7 s' a' σ' m := by + have hk := w.1 + have hc := w.2.1 + have osk : OSKeep st OS s s' := OSKeep.of_off J.ent.os w fun y hy o o1 o2 h => by + have hv : (wa y o).val = y.val + o.val := by + have := J.ent.os.fit y hy + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have hin : InOS OS (wa y o) := ⟨y, hy, by omega, by omega⟩ + exact h.elim (J.os_offStepNA (J.ent.os.al hy o1 o2) hin) fun hx => XO.os _ hx hin + have capE : ∀ u ∈ OS, capA st s' u = capA st s u := fun u hu => by + unfold capA; rw [(osk u hu).1, (osk u hu).2.1] + have keep : ∀ b, Aligned b → ¬ SchedStepNWA (st := st) N E Rp Z m σ s b → ¬ X b → L.mem s' b = L.mem s b := + fun b hb hn hx => w.mem_eq hb fun h => h.elim hn hx + obtain ⟨Pt, Ub, vr, arm, link, apart⟩ := J7PpA.core (st := st) _ _ _ _ _ _ J.j7 + have P := J.sr.place + have hT : ∀ t ∈ Z.T, t ∈ H.tcbs := fun t ht => by rw [J.tcbs]; exact ht + -- the page words + have vsr : ∀ b, VsR (L.ksb s) Pt b → ¬ X b → L.mem s' b = L.mem s b := fun b hb hx => by + refine keep _ (vr.al _ hb) ?_ hx + by_cases h : AsW (L.ksb s) b + · obtain ⟨o, o1, o2, -, e⟩ := h + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.ent.ksw + have ev : b.val = (L.ksb s).val + o.val := by + rw [e, wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact J.asid_offStepNA (by omega) (by omega) + · exact J.vsr_offStepNA vr hb h + have vsfoot : ∀ b, VsFootA Z.Pp Z.S1 Z.Y (L.ksb s) b → L.mem s' b = L.mem s b := fun b hb => by + refine vsr b ?_ (XO.vs b hb) + rcases hb.vsR with ⟨p, hp, h⟩ | h + · refine Or.inl ⟨p, link p ?_, h⟩ + rcases hp with hp | hp | hp + · exact Or.inl hp + · exact Or.inr (Or.inl hp) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl hp)))))) + · exact Or.inr h + have word : ∀ p, SchedPageA st Z OS s p → ∀ i : U64, i.val < 512 → L.mem s' (peAF p i) = L.mem s (peAF p i) := + fun p hp i hi => by + refine vsr _ (Or.inl ⟨p, link p hp, core.num.U64.wrapping_mul i 8#u64, ?_, ?_, rfl⟩) + (XO.page p hp (core.num.U64.wrapping_mul i 8#u64) (by rw [wm8_valA hi]; omega) (by rw [wm8_valA hi]; omega)) + · rw [wm8_valA hi]; omega + · rw [wm8_valA hi]; omega + have extfoot : ∀ b, ExtFootE Z.X Z.Y Z.T2 b → L.mem s' b = L.mem s b := fun b hb => by + cases hb with + | s2lo r i hr hi => exact word r (Or.inr (Or.inr (Or.inr (Or.inl hr)))) i hi + | s2hi r i hr hi => exact word _ (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨r, hr, rfl⟩))))) i hi + | s2pt t i ht hi => exact word t (Or.inr (Or.inr (Or.inl ht))) i hi + | wl0 y hy => exact word y (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl hy)))))) _ (by simp) + | wlo y i hy hi => exact word y (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl hy)))))) i (by omega) + -- the VCPU words + have vcw : ∀ v ∈ Z.V, ∀ o : U64, o.val = 0 ∨ o.val = 24 → L.mem s' (wa v o) = L.mem s (wa v o) := by + intro v hv o ho + have hH := arm.vh v hv + have hfit := J.ent.heap.fits v _ hH + have hal := J.ent.heap.align v _ hH + simp only [HKind.bits] at hfit hal + have ev : (wa v o).val = v.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have al : Aligned (wa v o) := by show _ % 8 = 0; rw [ev]; omega + have inObj : InObj v (.other 12) (wa v o) := by + unfold InObj; simp only [HKind.bits]; omega + have hsc : ∀ c ∈ H.scs, ¬ InSC c (H.scBits c) (wa v o) := by + rintro c ⟨z, hz⟩ ⟨c1, c2⟩ + have e : H.scBits c = z := by unfold Heap.scBits; rw [hz] + have ne : v ≠ c := fun h => by rw [h, hz] at hH; cases hH + have := J.ent.heap.disj v _ c _ hH hz ne + simp only [HKind.bits] at this + rw [e] at c2 + omega + have hg : ∀ g : U64, g.val < 223488 → wa (L.ksb s) g ≠ wa v o := by + intro g hg e + exact apart _ ⟨v, hv, o, by omega, by omega, rfl⟩ g hg e + exact keep _ al (fun hw => hw.elim + (J.omega_offStepA ⟨al, v, _, hH, Or.inr (Or.inr (Or.inl ⟨12, rfl⟩)), inObj⟩ hsc hg) + (J.ipc_off_hobjA hH (by simp) inObj)) (XO.vcw v hv o ho) + have slotw : ∀ y, IsSlotA Z.th y → ∀ x, (x = y ∨ x = wa y 8#u64 ∨ x = wa y 16#u64 ∨ x = wa y 24#u64) → + L.mem s' x = L.mem s x := by + intro y hy x hx + obtain ⟨a32, f32⟩ := hy.al32 P + have hv : ∀ o : U64, o.val < 32 → (wa y o).val = y.val + o.val := fun o ho => by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have key : x.val % 8 = 0 ∧ y.val ≤ x.val ∧ x.val < y.val + 32 := by + rcases hx with rfl | rfl | rfl | rfl + · omega + all_goals + rw [hv _ (by simp)]; simp; omega + exact keep _ key.1 (J.slot_offStepNA hy key.2.1 key.2.2) (XO.slot y _ hy key.2.1 key.2.2) + have cnw : ∀ y, SlotWA Z.th y → IsSlotA Z.th y := fun y hy => by + rcases hy with h | h + · exact h + · obtain ⟨p, r, hp, idx, hl, rfl⟩ := h + obtain ⟨e, hb, -⟩ := cn_slotV P hp hl + exact ⟨p, Or.inl ⟨r, hp, blvA idx, hb, e⟩⟩ + have tcbl : ∀ t ∈ Z.T, ∀ K : U64, K = 3664#u64 ∨ K = 3672#u64 → L.mem s' (wa t K) = L.mem s (wa t K) := by + intro t ht K hK + have hta := J.sr.core.tcbs t ht + refine keep _ (field_aligned (K := K) hta ?_ ?_) (J.link_offStepNA ht hK) (XO.link t ht K hK) + all_goals rcases hK with rfl | rfl <;> simp + have mem : SchedKeepMemA (st := st) Z s s' := + { ksb := hk + cpu := hc + slot := fun y hy x hx => slotw y (cnw y hy) x hx + tcbW := fun t ht => ⟨tcbl t ht _ (Or.inl rfl), tcbl t ht _ (Or.inr rfl)⟩ + vcW := fun v hv => by + have e0 : wa v 0#u64 = v := by apply UScalar.eq_of_val_eq; rw [wa_valm]; simp + have h0 := vcw v hv 0#u64 (Or.inl (by simp)) + rw [e0] at h0 + exact ⟨h0, vcw v hv _ (Or.inr (by simp))⟩ + irqW := fun i hi => by + refine keep _ ?_ (J.irq_offStepNA hi) (XO.irq i hi) + have := irqWordA_val J.sr.ksFit hi + have := J.sr.toSchedSRN.ksb_al + show _ % 8 = 0 + omega + vs := vsfoot + ext := extfoot } + -- the node cursors + have nodes : ∀ c ∈ Z.Cs, + L.mem s' (L.nodeAt s' c layout.node.IDLE_THREAD) ∈ Z.T ∧ L.mem s' (L.nodeAt s' c layout.node.IDLE_SC) ∈ Z.C ∧ + L.mem s' (L.nodeAt s' c layout.node.CUR_THREAD) ∈ Z.T := by + intro c hcs + refine ⟨?_, ?_, ?_⟩ + · have e : L.mem s' (L.nodeAt s' c layout.node.IDLE_THREAD) = L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) := + specPtr_inj ((sr.sch.idleOf c hcs).symm.trans (J.sr.sch.idleOf c hcs)) + rw [e]; exact J.j.1.idleT c hcs + · have e : L.mem s' (L.nodeAt s' c layout.node.IDLE_SC) = L.mem s (L.nodeAt s c layout.node.IDLE_SC) := + specPtr_inj ((sr.sch.idleScOf c hcs).symm.trans (J.sr.sch.idleScOf c hcs)) + rw [e]; exact J.j.1.idleC c hcs + · by_cases h : c = L.cpu s + · have := sr.curT + unfold LawfulStore.curW at this + rw [hc, ← h] at this + exact this + · have hne : c.val ≠ m.curCore := fun e => h (UScalar.eq_of_val_eq (e.trans J.sr.sch.core)) + have h1 := sr.sch.curOfW c hcs + rw [show ({ Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } : XSetsA).curOf = curOf from rfl, + hcurOf _ hne] at h1 + have e : L.mem s' (L.nodeAt s' c layout.node.CUR_THREAD) = L.mem s (L.nodeAt s c layout.node.CUR_THREAD) := + specPtr_inj (h1.symm.trans (J.sr.sch.curOfW c hcs)) + rw [e]; exact J.j.1.curT c hcs + have st' : StEqA Z.T (L.mem s') σ' := fun _ ht _ hx => (ep.at_tcb ht hx).st + have jx := JAtZAX.of_sched J.j (a' := a') (Q := Q) (R := Rl) (curOf := curOf) (curScOf := curScOf) mem HS har + (sr.sch.withSt st') sr.rest (fun c h => (nodes c h).1) (fun c h => (nodes c h).2.1) + (fun c h => (nodes c h).2.2) sr.cscAll + have free : ∀ dev x, UtFreeA st OS s' dev x → UtFreeA st OS s dev x := by + rintro dev x ⟨u, hu, q, b, fi, d, hcap, h⟩ + exact ⟨u, hu, q, b, fi, d, (capE u hu).symm.trans hcap, h⟩ + have dead : ∀ k p, DeadA st OS k s' p → DeadA st OS k s p := fun k p h u hu hp => by + have := h u hu + rw [capE u hu] at this + exact this hp + have nsc : ∀ {p : Fl4SpecA64.ObjRef}, σ.kheap p = none → σ'.kheap p = none := fun {p} h => by + rcases HS.kh p with e | ⟨x, _, e1, -⟩ | ⟨o, o', e1, -, -⟩ + · rw [e]; exact h + · rw [h] at e1; cases e1 + · rw [h] at e1; cases e1 + have keepE : ∀ {q : Fl4SpecA64.ObjRef} {e : Fl4SpecA64.ExtArchObj Fl4SpecA64.PTE}, + σ.kheap q = some (.archObj (.ext e)) → σ'.kheap q = some (.archObj (.ext e)) := fun h1 => + HS.keep h1 (fun _ e => by cases e) (fun e => by rcases e with ⟨_, _, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ <;> cases e) + have unmS2 : ∀ u ∈ OS, ∀ p a v, capA st s u = .Ept p 0#u64 false a v → UnmS2RootA st s σ p → + UnmS2RootA st s' σ' p := by + intro u hu p a v hc U + obtain ⟨es, h1, h2⟩ := U.spec + refine { inv := fun i hi => ?_, spec := ⟨es, keepE h1, h2⟩ } + by_cases hlo : i.val < 512 + · rw [word p (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inr (Or.inl ⟨a, v, hc⟩))⟩))))))) i hlo] + exact U.inv i hi + · obtain ⟨j, hj, e⟩ := peAF_hiA p i (by omega) hi + rw [e, word _ (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inr (Or.inr (Or.inl ⟨p, a, v, hc, rfl⟩)))⟩))))))) j hj, ← e] + exact U.inv i hi + have unmHv : ∀ u ∈ OS, ∀ y a, capA st s u = .HighVSpace y false a → UnmHiRootA st s σ y → + UnmHiRootA st s' σ' y := by + intro u hu y a hc U + obtain ⟨l1, h1, h2⟩ := U.spec + exact { inv := fun i hi => by + rw [word _ (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inr (Or.inr (Or.inr ⟨y, a, hc, rfl⟩)))⟩))))))) i hi] + exact U.inv i hi + spec := ⟨l1, keepE h1, h2⟩ } + have unm : ∀ ty p, SchedPageA st Z OS s p → UnmTabA st Z m s σ ty p → + UnmTabA st { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } m s' σ' ty p := by + intro ty p hp U1 + obtain ⟨tb, h1, h2, h3⟩ := U1.spec + exact + { inv := fun i hi => by rw [word p hp i hi]; exact U1.inv i hi + spec := ⟨tb, HS.keep h1 (fun _ e => by cases e) (fun e => by rcases e with ⟨_, _, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ <;> cases e), h2, h3⟩ + off := ⟨fun i hi h => U1.off.mem i hi (footA_schedA hk hc h), U1.off.obj⟩ } + exact + { ent := ent + j := jx + tcbs := J.tcbs + scs := J.scs + sq := sq + ap := by + have e : L.ksb s' = L.ksb s := hk + rw [e]; exact { J.ap with } + pp := J.pp + s1 := J.s1 + ko := by rw [hk]; exact J.ko + ptH := J.ptH + tabInj := by rw [HS.asid]; exact J.tabInj + poolOff := poolOff + objAlloc := ObjAllocA.keep (Z := Z) J.objAlloc osk + idleAnchor := IdleAnchorOffA.keep J.idleAnchor osk + vsVc := by rw [hk]; exact J.vsVc + sr := sr + scCov := scCov + scSize := fun c hcm x k h => by + obtain ⟨y, hy⟩ := SSz _ x k h + exact J.scSize c hcm y k hy + ep := ep + ntfn := ntfn + reply := reply + bufCover := bufCover + bufNative := fun t ht => by + have hsl : IsSlotA Z.th (slotAddrA (specPtr t) (BitVec.ofNat 64 2)) := + ⟨t, Or.inr ⟨(J.sr.thT t).2 ht, 2, by unfold tcbSlotsA; omega, rfl⟩⟩ + have e : capAtA (L.mem s') (slotAddrA (specPtr t) (BitVec.ofNat 64 2)) = + capAtA (L.mem s) (slotAddrA (specPtr t) (BitVec.ofNat 64 2)) := by + unfold capAtA + rw [slotw _ hsl _ (Or.inl rfl), slotw _ hsl _ (Or.inr (Or.inl rfl))] + rw [e]; exact J.bufNative t ht + iplaced := J.iplaced + ipcPlace := J.ipcPlace + idleNoFault := idleNoFault + mcp := mcp + yieldRel := yieldRel + sinv := sinv + sidle := sidle + sact := sact + setsInH := { J.setsInH with } + utIpc := J.utIpc + ipcTh := J.ipcTh + deadExt := + { vcpu := fun v hv hd x hx => J.deadExt.vcpu v hv (dead _ v hd) x + (HS.back hx (fun _ e => by cases e) (fun e => by rcases e with ⟨_, _, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ <;> cases e)) + reply := fun r hr hd c hc hm => J.deadExt.reply r hr (dead _ r hd) c hc (hsub c r hm) + asid := fun i p h hd => J.deadExt.asid i p (by rw [HS.asid] at h; exact h) (dead _ p hd) + tvcpu := fun t ht hd => by + rw [tcbl t ht 3664#u64 (Or.inl rfl)] + exact J.deadExt.tvcpu t ht (dead _ t hd) + vcDeadRun := fun v hv hd => by + rw [vcw v hv 24#u64 (Or.inr (by simp))] + exact J.deadExt.vcDeadRun v hv (dead _ v hd) + replyTcb := fun r hr hd x hx => by + obtain ⟨y, hy, k⟩ := Qk.rt _ x hx + exact k (J.deadExt.replyTcb r hr (dead _ r hd) y hy) } + specFree := + { heap := fun p h => nsc (J.specFree.heap p (free _ _ h)) + cdt := fun x y h p hf => J.specFree.cdt x y (by rw [HS.cdt] at h; exact h) p (free _ _ hf) + orig := fun x h p hf => J.specFree.orig x (by rw [HS.orig] at h; exact h) p (free _ _ hf) } + utBlk := fun u hu q b fi d h => J.utBlk u hu q b fi d ((capE u hu).symm.trans h) + unmPt := + { pt := fun u hu p asid va h => by + have h0 := (capE u hu).symm.trans h + exact unm _ p (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inl ⟨_, _, _, h0⟩⟩))))))) (J.unmPt.pt u hu p asid va h0) + vs := fun u hu p asid h => by + have h0 := (capE u hu).symm.trans h + exact unm _ p (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inl ⟨_, _, h0⟩)⟩))))))) (J.unmPt.vs u hu p asid h0) + s2 := fun u hu p asid va h => by + have h0 := (capE u hu).symm.trans h + exact unmS2 u hu p asid va h0 (J.unmPt.s2 u hu p asid va h0) + hv := fun u hu p asid h => by + have h0 := (capE u hu).symm.trans h + exact unmHv u hu p asid h0 (J.unmPt.hv u hu p asid h0) } + tabS1 := fun q hq => + ⟨fun i hi h => (J.tabS1 q hq).mem i hi (footA_schedA hk hc h), (J.tabS1 q hq).obj⟩ + tabExt := + ⟨fun q hq i hi h => J.tabExt.mem q hq i hi (by + exact footA_schedA (Z := extZA Z) hk hc h), J.tabExt.obj⟩ + specZero := fun x h => (hz x (free _ _ h)).trans (J.specZero x (free _ _ h)) + origLive := fun x h => J.origLive x (by rw [HS.orig] at h; exact h) + nullOrig := fun sl h => by + rw [HS.orig] + exact J.nullOrig sl (by rw [← SpecSchedA.specCapAt HS sl]; exact h) + thPos := fun p z h => J.thPos p z h + frBase := J.frBase.keepA capE + devFr := J.devFr.keepA capE fun p dev pg h => HS.keep h (fun _ e => by cases e) + (fun e => by rcases e with ⟨_, _, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ <;> cases e) + finCov := J.finCov.keepA + (fun q r cs h => HS.back h (fun _ e => by cases e) + (fun e => by rcases e with ⟨_, _, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ <;> cases e)) + (fun q x h => HS.tcbBack h) + idleAll := J.idleAll.keepA + (fun c hcs => specPtr_inj ((sr.sch.idleOf c hcs).symm.trans (J.sr.sch.idleOf c hcs))) + (fun c hcs => specPtr_inj ((sr.sch.idleScOf c hcs).symm.trans (J.sr.sch.idleScOf c hcs))) + ipcHeapCov := J.ipcHeapCov + zombieNoVcpu := J.zombieNoVcpu.keepA capE J.zombieTcb + fun t ht => tcbl t (J.tcbs ▸ ht) 3664#u64 (Or.inl rfl) + zombieTcb := J.zombieTcb.keepA capE + bufOff := J.bufOff.keepA (Z := Z) (Z' := { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf }) + hk rfl rfl rfl rfl rfl rfl rfl capE + vsOk := by + have e := (utVsPt_keepA J.ent.vs hk (fun b hb => by + refine keep b (J.vsOk.al b hb) ?_ (hXvs b hb) + by_cases h : AsW (L.ksb s) b + · obtain ⟨o, o1, o2, -, e⟩ := h + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.ent.ksw + have ev : b.val = (L.ksb s).val + o.val := by + rw [e, wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact J.asid_offStepNA (by omega) (by omega) + · exact J.vsr_offStepNA J.vsOk hb h)).2 + unfold UtVsOkA + rw [show L.ksb s' = L.ksb s from hk, e] + exact J.vsOk + domOK := J.domOK.keepS J.sr.toSchedSRN Qk.dom hk hc (fun b hb hd => w.mem_eq hb fun h => h.elim (J.dom_offStepNA hd) (XO.dom b hd)) + ipiZero := J.ipiKeepOf w fun o ho h => h.elim (J.ipi_offStepNA ho) (XO.ipi o ho) + bufVs := by + have e := (utVsPt_keepA J.ent.vs hk (fun b hb => by + refine keep b (J.vsOk.al b hb) ?_ (hXvs b hb) + by_cases h : AsW (L.ksb s) b + · obtain ⟨o, o1, o2, -, e⟩ := h + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.ent.ksw + have ev : b.val = (L.ksb s).val + o.val := by + rw [e, wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact J.asid_offStepNA (by omega) (by omega) + · exact J.vsr_offStepNA J.vsOk hb h)).2 + rw [show L.ksb s' = L.ksb s from hk, e] + exact J.bufVs + bufAl := bufAl + zombieBnd := J.zombieBnd.keepA capE + devUtOff := J.devUtOff.keepA (Z := Z) capE hk (fun _ _ h => h) rfl + capTy := J.capTy.keepA capE + scCpu := J.scCpu + scNZ := J.scNZ + vcRun := J.vcRun.keepA (fun t ht => tcbl t ht 3664#u64 (Or.inl rfl)) (fun v hv => vcw v hv 24#u64 (Or.inr (by simp))) + (fun v hv => by + have h := vcw v hv 0#u64 (Or.inl (by simp)) + rwa [wa_zeroVcA] at h) + (fun t ht => tcbl t ht 3672#u64 (Or.inr rfl)) + runVcpu := runVcpu + vr := hvr + blkQ := blkQ + ciSc := fun u hu p b h => J.ciSc u hu p b ((capE u hu).symm.trans h) + noSwitch := noSwitch + curVcpuNone := HS.vcur.trans J.curVcpuNone + ipcSorted := J.ipcSorted.of_q Qk + activeReplyScs := J.activeReplyScs.of_q Qk + j5 := j5 + j7 := j7 } + +set_option maxHeartbeats 1000000 in +/-- **`JStage3A` across a scheduler or IPC-object step** (module docstring): the write set is `SchedStepNWA`. -/ +theorem JStage3TA.of_schedWrSN {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J5 J7 : JSlotA S} [J7PpA st J7] {s s' : S} + {a a' : A} {σ σ' : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + {Ac' : Set U64} + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {stack' : U64 → List U64} (hsub : ∀ c, ∀ r ∈ stack' c, r ∈ stack c) + {Q : U64 → U64 → List U64} {Rl : U64 → List U64} {curOf curScOf : Fl4SpecA64.CoreId → Fl4SpecA64.ObjRef} + (w : WritesIn st s s' (SchedStepNWA (st := st) N E Rp Z m σ s)) (HS : SpecSchedA σ σ') (SSz : SSzA σ σ') (Qk : QKeepA σ σ') + (hum : σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory) (har : AR a' Z.hw) + (hcurOf : ∀ k, k ≠ m.curCore → curOf k = Z.curOf k) (bufAl : UtBufAlA Z.T σ') + (ent : EntryInvA3 st H Z.Cs OS U Ac' Pp Tb Lv s') + (sq : SchedQueuesInv st H.tcbs Z.Cs H.omega H s' Q Rl) + (poolOff : PoolOff st H Z.Cs OS s') + (sr : SchedSRA st { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } m κ s' σ') + (scCov : ScCovA Z.C m σ') + (ep : EpStateRel Z.T E Rp Bf (L.mem s') σ') + (ntfn : NtfnStateRel N Z.T (L.mem s') σ') + (reply : ReplyStackRelA (L.mem s') Rp Z.C σ' (scNameA m) stack') + (bufCover : BufCover st Z.T Bf s' σ') + (idleNoFault : IdleNoFaultA m σ') + (mcp : McpRelA Z.T m (L.mem s') σ') + (yieldRel : YieldRelA Z.T Z.C m (L.mem s') σ') + (sinv : SInvXA σ' m n) + (sidle : SIdleA σ' m n) + (runVcpu : RunVcpuInv st Z.T s') (hvr : ∀ sc ∈ Z.C, ∀ r ∈ stack' sc, RBlk st s' r) + (noSwitch : ∀ u, σ'.schedulerAction ≠ .switchThread u) + (sact : SActA σ') + (blkQ : BlkQueuedA Z.T E N (L.mem s') σ') + (j5 : J5 { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } H OS s' σ' m) + (j7 : J7 { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } H OS s' σ' m) : + JStage3TA st AR { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } H OS U Ac' Pp Tb Lv ia ie N E Rp + Bf stack' κ n J5 J7 s' a' σ' m := + JStage3TA.of_schedWrSX J hsub (X := fun _ => False) + ⟨fun _ h => h.elim, fun _ _ _ _ _ h => h, fun _ _ _ _ h => h, fun _ _ h => h, fun _ _ h => h, + fun _ _ _ _ _ h => h, fun _ _ _ _ h => h, fun _ _ h => h, fun _ _ h => h⟩ + (fun _ _ h => h) bufAl + (w.mono fun _ h => Or.inl h) HS SSz Qk (fun x _ => by rw [hum]) har hcurOf ent sq poolOff sr scCov ep ntfn reply + bufCover idleNoFault mcp yieldRel sinv sidle runVcpu hvr noSwitch sact (blkQ := blkQ) j5 j7 + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3SchedWrTA.lean b/proofs/Fl4ProofsA64/P3/Stage3SchedWrTA.lean new file mode 100644 index 000000000..67fd7fc59 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SchedWrTA.lean @@ -0,0 +1,822 @@ +/- +P3 (AArch64, contract §36: shared preemption-point closure): **`JStage3TA` across a scheduler step**: +`P3/Stage3SchedWrA`'s `of_schedWr` and the record lemmas it uses, over the per-step record `JStage3TA` with any J7 +slot of the class **`J7PpA`** (instances `J7A (J7R2A)` and `J7LA (J7R2A)`). A `JStage3A` reaches it through +`toTA`. Generated by `handoff/tools/gen_schedwrta.py` (`copy_ta.py`; `SysL`'s one use, `ly.kslo`, is J7's +`KsBridge.slotKs`). No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3BA +import Fl4ProofsA64.P3.Stage3TA +import Fl4ProofsA64.P3.Stage3J7A +import Fl4ProofsA64.P3.DelJ7LA +import Fl4ProofsA64.P3.Stage3SchedWrA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +variable (st) in +/-- **What the preemption point's writes need of a J7 slot**: the VSpace word set with its placement (`core`), and +that the slot survives a step keeping the `OS` caps, the kernel-state base and the VSpace words (`keep`). -/ +class J7PpA (J7 : JSlotA S) : Prop where + core : ∀ Z H OS s σ m, J7 Z H OS s σ m → ∃ Pt Ub : Set U64, + VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt ∧ S1aArmCol st H Z.Cs OS Z.V Ub s ∧ + (∀ p, SchedPageA st Z OS s p → p ∈ Pt) ∧ (∀ b, VcW Z.V b → ∀ o : U64, o.val < 223488 → wa (L.ksb s) o ≠ b) + +/-- `SysL`'s `kslo` from J7's `KsBridge`: the `OS` slots are off the first 224 bytes of the kernel-state block. -/ +theorem JStage3BA.ksloA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} + {κ : U64 → U64} {n : Nat} {J5 J7 : JSlotA S} [J7PpA st J7] {s : S} {a : A} {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} + (J : JStage3BA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) : + ∀ o : U64, o.val < 224 → ∀ y ∈ OS, ¬ (y.val ≤ (wa (L.ksb s) o).val ∧ (wa (L.ksb s) o).val < y.val + 32) := by + intro o ho y hy h + obtain ⟨_, _, -, arm, -, -⟩ := J7PpA.core (st := st) _ _ _ _ _ _ J.j7 + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.ent.ksw + have ev : (wa (L.ksb s) o).val = (L.ksb s).val + o.val := by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + rw [ev] at h + rcases (arm.kb.slotKs y hy).1 with g | g <;> omega + +/-- `JStage3BA.ksloA` at `JStage3TA`. -/ +theorem JStage3TA.ksloA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} + {κ : U64 → U64} {n : Nat} {J5 J7 : JSlotA S} [J7PpA st J7] {s : S} {a : A} {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) : + ∀ o : U64, o.val < 224 → ∀ y ∈ OS, ¬ (y.val ≤ (wa (L.ksb s) o).val ∧ (wa (L.ksb s) o).val < y.val + 32) := + J.toBA.ksloA + +section offBA +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} [J7PpA st J7] {s : S} {a : A} {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} + (J : JStage3BA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) +include J + + +/-- An SC word lies inside its heap SC object (`scSize`). -/ +theorem JStage3BA.scFoot_heapPA {b : U64} (h : ScFootA Z.C m σ b) : + ∃ c ∈ Z.C, ∃ z, H c = some (.sc z) ∧ InSC c (H.scBits c) b := by + obtain ⟨c, hc, x, k, hk, h1, h2⟩ := h + have hcH : c ∈ H.scs := by rw [J.scs]; exact hc + obtain ⟨z, hz⟩ := hcH + refine ⟨c, hc, z, hz, h1, lt_of_lt_of_le h2 ?_⟩ + have := (J.scSize c hc x k hk).1 + exact Nat.add_le_add_left (Nat.pow_le_pow_right (by decide) this) _ + +omit [J7PpA st J7] in +/-- A word of a heap object that is no state-block `other` region is off the 128 nodes. -/ +theorem JStage3BA.heap_offNodesPA {p b : U64} {k : HKind} (hp : H p = some k) (h1 : p.val ≤ b.val) + (h2 : b.val < p.val + 2 ^ k.bits) : + ¬ ((L.ksb s).val + 223488 ≤ b.val ∧ b.val < (L.ksb s).val + 772352) := by + rintro ⟨n1, n2⟩ + by_cases ho : ∃ z, k = .other z + · obtain ⟨z, rfl⟩ := ho + simp only [HKind.bits] at h2 + rcases J.ko.2 p z hp with h | h | h | h <;> omega + · have := J.ko.1 p k hp (fun z e => ho ⟨z, e⟩) + omega + +/-- **A heap word outside the SCs and the kernel globals is not written.** -/ +theorem JStage3BA.omega_offStepA {b : U64} (hΩ : b ∈ H.omega) + (hsc : ∀ c ∈ H.scs, ¬ InSC c (H.scBits c) b) + (hg : ∀ o : U64, o.val < 223488 → wa (L.ksb s) o ≠ b) : ¬ SchedStepWA (st := st) Z m σ s b := by + intro hw + obtain ⟨Q, R', q⟩ := J.ent.bxi.1 + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.ent.ksw + rcases stepW_casesA J.sr.toSchedSRN hw with h | h | ⟨t, ht, h1, h2, -⟩ + · obtain ⟨c, hc, z, hz, hin⟩ := J.scFoot_heapPA h + exact hsc c ⟨z, hz⟩ hin + · have h' : ((L.ksb s).val ≤ b.val ∧ b.val < (L.ksb s).val + 224) ∨ + ((L.ksb s).val + 223488 ≤ b.val ∧ b.val < (L.ksb s).val + 772352) := by + unfold SKsA at h; omega + rcases h' with ⟨h1, h2⟩ | h + · refine hg ⟨BitVec.ofNat 64 (b.val - (L.ksb s).val)⟩ ?_ ?_ + all_goals + have e : (⟨BitVec.ofNat 64 (b.val - (L.ksb s).val)⟩ : U64).val = b.val - (L.ksb s).val := by + show (BitVec.ofNat 64 _).toNat = _ + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + · omega + · exact UScalar.eq_of_val_eq (by rw [wa_valm, e]; rw [Nat.mod_eq_of_lt (by omega)]; omega) + · obtain ⟨-, p, k, hp, -, hin⟩ := hΩ + exact J.heap_offNodesPA hp hin.1 hin.2 h + · have htH : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + have := (q.omega b hΩ).2.1 t htH + omega + +/-- **The VSpace, Stage-2 and unmapped-table words are not written** (`VrOK` at `J7A`'s link). -/ +theorem JStage3BA.vsr_offStepA {Pt Ub : Set U64} + (vr : VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt) {b : U64} (h : VsR (L.ksb s) Pt b) + (hna : ¬ AsW (L.ksb s) b) : ¬ SchedStepWA (st := st) Z m σ s b := + J.omega_offStepA (vr.om b h) (vr.sc b h) (vr.ks b h) + +/-- (record edit #8b-B1) An `OS` slot word is off the domain-schedule words. -/ +theorem JStage3BA.os_domA {y b : U64} (hy : y ∈ OS) (h1 : y.val ≤ b.val) (h2 : b.val < y.val + 32) + (hd : DomW (L.ksb s) b) : False := by + obtain ⟨o, ho, eo⟩ := hd.wa_exists J.sr.ksFit + exact J.ksloA o ho y hy (by rw [eo]; exact ⟨h1, h2⟩) + +/-- (record edit #8b-B1) **A cap-slot word is off the domain-schedule words**. -/ +theorem JStage3BA.capw_domA {b : U64} (hc : W7.CapW H.tcbs {b | InOS OS b} ∅ b) (hd : DomW (L.ksb s) b) : False := by + obtain ⟨Q, R', q⟩ := J.ent.bxi.1 + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.ent.ksw + rcases hc with ⟨t, ht, K, hK, e⟩ | ⟨⟨y, hy, y1, y2⟩, -⟩ + · have ev : b.val = t.val + K.val := by rw [e]; exact field_val (q.tcbs t ht) (K := K) (by omega) + have := J.sr.tcb_ks (show t ∈ Z.T by rw [← J.tcbs]; exact ht) + unfold DomW at hd + unfold ksSizeA at this + omega + · exact J.os_domA hy y1 y2 hd + +/-- **An `OS` slot word is not written.** -/ +theorem JStage3BA.os_offStepA {b : U64} (hb : Aligned b) (h : InOS OS b) : ¬ SchedStepWA (st := st) Z m σ s b := by + intro hw + obtain ⟨Q, R', q⟩ := J.ent.bxi.1 + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.ent.ksw + have cap : W7.CapW H.tcbs H.omega H.frame b := inOS_capw J.ent.os hb h + obtain ⟨y, hy, y1, y2⟩ := h + rcases stepW_casesA J.sr.toSchedSRN hw with h | h | ⟨t, ht, h1, h2, -⟩ + · obtain ⟨c, hc, z, hz, c1, c2⟩ := J.scFoot_heapPA h + have := J.ap.sca y hy c ⟨z, hz⟩ + omega + · have h' : ((L.ksb s).val ≤ b.val ∧ b.val < (L.ksb s).val + 224) ∨ + ((L.ksb s).val + 223488 ≤ b.val ∧ b.val < (L.ksb s).val + 772352) := by + unfold SKsA at h; omega + rcases h' with ⟨h1, h2⟩ | h + · have e : (⟨BitVec.ofNat 64 (b.val - (L.ksb s).val)⟩ : U64).val = b.val - (L.ksb s).val := by + show (BitVec.ofNat 64 _).toNat = _ + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + have eb : wa (L.ksb s) ⟨BitVec.ofNat 64 (b.val - (L.ksb s).val)⟩ = b := + UScalar.eq_of_val_eq (by rw [wa_valm, e]; rw [Nat.mod_eq_of_lt (by omega)]; omega) + exact J.ksloA _ (by rw [e]; omega) y hy (by rw [eb]; exact ⟨y1, y2⟩) + · rcases cap with ⟨t, ht, K, hK, e⟩ | ⟨hΩ, -⟩ + · have ev : b.val = t.val + K.val := by rw [e]; exact field_val (q.tcbs t ht) (K := K) (by omega) + have := J.heap_offNodesPA (p := t) (b := b) (k := .tcb) ht (by omega) + (by simp only [HKind.bits]; omega) + exact this h + · obtain ⟨-, p, k, hp, -, hin⟩ := hΩ + exact J.heap_offNodesPA hp hin.1 hin.2 h + · have htH : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + have hf := tcb4096 (q.tcbs t htH) + have eK : (⟨BitVec.ofNat 64 (b.val - t.val)⟩ : U64).val = b.val - t.val := by + show (BitVec.ofNat 64 _).toNat = _ + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + refine capw_ne_hi q cap htH (K := ⟨BitVec.ofNat 64 (b.val - t.val)⟩) (by omega) (by omega) ?_ + exact UScalar.eq_of_val_eq (by rw [field_val (q.tcbs t htH) (by omega), eK]; omega) + +/-- `OSKeep` across a scheduler step. -/ +theorem JStage3BA.osKeep_stepA {s' : S} (w : WritesIn st s s' (SchedStepWA (st := st) Z m σ s)) : + OSKeep st OS s s' := by + intro y hy + have hfit := J.ent.os.fit y hy + have al0 := J.ent.os.al0 hy + have key : ∀ o : U64, o.val < 32 → o.val % 8 = 0 → L.mem s' (wa y o) = L.mem s (wa y o) := by + intro o o1 o2 + have hv : (wa y o).val = y.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have hal : Aligned (wa y o) := J.ent.os.al hy o1 o2 + exact w.mem_eq hal (J.os_offStepA hal ⟨y, hy, by omega, by omega⟩) + have e0 : wa y 0#u64 = y := by apply UScalar.eq_of_val_eq; rw [wa_valm]; simp + exact ⟨by have := key 0#u64 (by simp) (by simp); rwa [e0] at this, key 8#u64 (by simp) (by simp), + key 16#u64 (by simp) (by simp), key 24#u64 (by simp) (by simp)⟩ + +/-- **A word inside a slot of the typed heap is not written.** -/ +theorem JStage3BA.slot_offStepA {y b : U64} (hy : IsSlotA Z.th y) (y1 : y.val ≤ b.val) (y2 : b.val < y.val + 32) : + ¬ SchedStepWA (st := st) Z m σ s b := by + intro hw + have P := J.sr.place + have obj : ∃ p k, Z.th p = some k ∧ p.val ≤ y.val ∧ y.val + 32 ≤ p.val + k.size ∧ + (k = .tcb → y.val + 32 ≤ p.val + 224) := by + obtain ⟨p, ⟨r, hr, i, hi, rfl⟩ | ⟨ht, i, hi, rfl⟩⟩ := hy + · obtain ⟨k, hk, hs, hv⟩ := slotVA_of P hr (j := i) (by rw [cnode_size]; omega) + exact ⟨p, k, hk, by omega, by omega, fun e => by + rw [e, hr] at hk; cases hk⟩ + · obtain ⟨k, hk, hs, hv⟩ := tcb_slotV P ht hi + have : i < 7 := hi + exact ⟨p, k, hk, by omega, by omega, fun _ => by omega⟩ + obtain ⟨p, k, hk, p1, p2, pt⟩ := obj + have hksz : ksSizeA = 788736 := rfl + rcases stepW_casesA J.sr.toSchedSRN hw with h | h | ⟨t, ht, t1, t2, -⟩ + · obtain ⟨c, hc, x, n', hx, ha⟩ := h + have := (J.sr.scp.out c hc x n' hx b ha).1 p k hk + omega + · have hin : IrqNodeA (L.ksb s) p → + (L.ksb s).val + 43232 ≤ b.val ∧ b.val < (L.ksb s).val + 215264 := by + intro hn + have ek := P.irqK hk hn + subst ek + obtain ⟨i, hi, rfl⟩ := hn + have ev := irqSlotA_val J.sr.ksFit hi + have : ObjKindA.size (.cnode 0) = 32 := rfl + omega + by_cases hn : IrqNodeA (L.ksb s) p + · have := hin hn + unfold SKsA at h; omega + · have := P.ks p k hk hn + unfold SKsA at h; omega + · have hth := (J.sr.thT t).2 ht + by_cases e : p = t + · subst e + rw [hth] at hk + cases hk + have := pt rfl + omega + · have := P.apart p t k .tcb hk hth e + have : ObjKindA.size .tcb = 4096 := rfl + omega + +/-- A word of a TCB of `Z.T` at a VCPU link is not written. -/ +theorem JStage3BA.link_offStepA {t K : U64} (ht : t ∈ Z.T) (hK : K = 3664#u64 ∨ K = 3672#u64) : + ¬ SchedStepWA (st := st) Z m σ s (wa t K) := by + intro hw + have hfit := (J.sr.core.tcbs t ht).fits + have hKv : K.val = 3664 ∨ K.val = 3672 := by rcases hK with rfl | rfl <;> simp + have hv : (wa t K).val = t.val + K.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have hth := (J.sr.thT t).2 ht + rcases stepW_casesA J.sr.toSchedSRN hw with h | h | ⟨u, hu, u1, u2, n1, n2⟩ + · obtain ⟨c, hc, x, n', hx, ha⟩ := h + have := (J.sr.scp.out c hc x n' hx _ ha).1 t _ hth + have : ObjKindA.size .tcb = 4096 := rfl + omega + · have := J.sr.tcb_ks ht + unfold SKsA at h + unfold ksSizeA at this; omega + · by_cases e : u = t + · subst e + rcases hK with rfl | rfl + · exact n1 rfl + · exact n2 rfl + · have := J.sr.place.apart u t .tcb .tcb ((J.sr.thT u).2 hu) hth e + have : ObjKindA.size .tcb = 4096 := rfl + omega + +/-- An IRQ-state word is not written. -/ +theorem JStage3BA.irq_offStepA {i : U64} (hi : i.val < 5376) : + ¬ SchedStepWA (st := st) Z m σ s (irqWordA (L.ksb s) i) := by + intro hw + have hv := irqWordA_val J.sr.ksFit hi + have hksz : ksSizeA = 788736 := rfl + rcases stepW_casesA J.sr.toSchedSRN hw with h | h | ⟨t, ht, t1, t2, -⟩ + · obtain ⟨c, hc, x, n', hx, ha⟩ := h + have := (J.sr.scp.out c hc x n' hx _ ha).2 + omega + · unfold SKsA at h; omega + · have := J.sr.tcb_ks ht + omega + +/-- An ASID-table word is not written. -/ +theorem JStage3BA.asid_offStepA {b : U64} (h1 : (L.ksb s).val + 772352 ≤ b.val) + (h2 : b.val < (L.ksb s).val + 773376) : ¬ SchedStepWA (st := st) Z m σ s b := by + intro hw + have hksz : ksSizeA = 788736 := rfl + rcases stepW_casesA J.sr.toSchedSRN hw with h | h | ⟨t, ht, t1, t2, -⟩ + · obtain ⟨c, hc, x, n', hx, ha⟩ := h + have := (J.sr.scp.out c hc x n' hx _ ha).2 + omega + · unfold SKsA at h; omega + · have := J.sr.tcb_ks ht + omega + +/-- (record edit #8b-B1) A domain-schedule word is not written. -/ +theorem JStage3BA.dom_offStepA {b : U64} (hd : DomW (L.ksb s) b) : ¬ SchedStepWA (st := st) Z m σ s b := by + intro hw + have hksz : ksSizeA = 788736 := rfl + unfold DomW at hd + rcases stepW_casesA J.sr.toSchedSRN hw with h | h | ⟨t, ht, t1, t2, -⟩ + · obtain ⟨c, hc, x, n', hx, ha⟩ := h + have := (J.sr.scp.out c hc x n' hx _ ha).2 + omega + · unfold SKsA at h; omega + · have := J.sr.tcb_ks ht + omega + +/-- (record edit B2) The current core's IPI-pair words are not written. -/ +theorem JStage3BA.ipi_offStepA {o : U64} (ho : o.val = 120 ∨ o.val = 128) : + ¬ SchedStepWA (st := st) Z m σ s (L.nodeAt s (L.cpu s) o) := by + intro hw + have hksz : ksSizeA = 788736 := rfl + obtain ⟨hv, hcl⟩ := J.sr.toSchedSRN.ipi_val ho + rcases stepW_casesA J.sr.toSchedSRN hw with h | h | ⟨t, ht, t1, t2, -⟩ + · obtain ⟨c, hc, x, n', hx, ha⟩ := h + have := (J.sr.scp.out c hc x n' hx _ ha).2 + omega + · unfold SKsA at h; omega + · have := J.sr.tcb_ks ht + omega + +/-- (record edit B2) An IPI-pair word is no cap-slot word. -/ +theorem JStage3BA.ipi_capwA {o : U64} (ho : o.val = 120 ∨ o.val = 128) + (h : W7.CapW H.tcbs {b | InOS OS b} ∅ (L.nodeAt s (L.cpu s) o)) : False := by + obtain ⟨Q, R', q⟩ := J.ent.bxi.1 + have hal : Aligned (L.nodeAt s (L.cpu s) o) := nodeAt_aligned (J.sr.core.nodes _ J.sr.sch.cpuCs) (by omega) (by omega) + exact capw_ne_node q (os_capw J.ent.os hal h) J.sr.sch.cpuCs (show o.val < 4288 by omega) rfl + +omit [J7PpA st J7] in +/-- (record edit B2) An IPI-pair word is no word of a heap object. -/ +theorem JStage3BA.ipi_omegaA {o : U64} (ho : o.val = 120 ∨ o.val = 128) + (hΩ : L.nodeAt s (L.cpu s) o ∈ H.omega) : False := by + obtain ⟨hv, hcl⟩ := J.sr.toSchedSRN.ipi_val ho + obtain ⟨-, p, k, hp, -, hin⟩ := hΩ + exact J.heap_offNodesPA hp hin.1 hin.2 (by omega) + +omit [J7PpA st J7] in +/-- (record edit B2) **`ipiZero` across a write off the IPI-pair words.** -/ +theorem JStage3BA.ipiKeepOf {s' : S} {W : U64 → Prop} (w : WritesIn st s s' W) + (hW : ∀ o : U64, (o.val = 120 ∨ o.val = 128) → ¬ W (L.nodeAt s (L.cpu s) o)) : + Sched.SilentScheduleIpiA (st := st) s' := + J.ipiZero.keepW w.1 w.2.1 fun o ho => + w.mem_eq (nodeAt_aligned (J.sr.core.nodes _ J.sr.sch.cpuCs) (by omega) (by omega)) (hW o ho) + +omit [J7PpA st J7] in +/-- (record edit F4) The `KERNEL_ROOT` word is the spec's global user VSpace. -/ +theorem JStage3BA.kroot : + specPtr (L.mem s (Fl4ProofsA64.P1.krA (L := L) s)) = σ.archState.armUsGlobalVspace := + J.domOK.kroot + +end offBA + +section offTA +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} [J7PpA st J7] {s : S} {a : A} {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) +include J + +/-- `JStage3BA.scFoot_heapPA` at `JStage3TA`. -/ +theorem JStage3TA.scFoot_heapPA {b : U64} (h : ScFootA Z.C m σ b) : + ∃ c ∈ Z.C, ∃ z, H c = some (.sc z) ∧ InSC c (H.scBits c) b := + J.toBA.scFoot_heapPA h + +omit [J7PpA st J7] in +theorem JStage3TA.heap_offNodesPA {p b : U64} {k : HKind} (hp : H p = some k) (h1 : p.val ≤ b.val) + (h2 : b.val < p.val + 2 ^ k.bits) : + ¬ ((L.ksb s).val + 223488 ≤ b.val ∧ b.val < (L.ksb s).val + 772352) := + J.toBA.heap_offNodesPA hp h1 h2 + +/-- `JStage3BA.omega_offStepA` at `JStage3TA`. -/ +theorem JStage3TA.omega_offStepA {b : U64} (hΩ : b ∈ H.omega) + (hsc : ∀ c ∈ H.scs, ¬ InSC c (H.scBits c) b) + (hg : ∀ o : U64, o.val < 223488 → wa (L.ksb s) o ≠ b) : ¬ SchedStepWA (st := st) Z m σ s b := + J.toBA.omega_offStepA hΩ hsc hg + +/-- `JStage3BA.vsr_offStepA` at `JStage3TA`. -/ +theorem JStage3TA.vsr_offStepA {Pt Ub : Set U64} + (vr : VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt) {b : U64} (h : VsR (L.ksb s) Pt b) + (hna : ¬ AsW (L.ksb s) b) : ¬ SchedStepWA (st := st) Z m σ s b := + J.toBA.vsr_offStepA vr h hna + +/-- `JStage3BA.os_domA` at `JStage3TA`. -/ +theorem JStage3TA.os_domA {y b : U64} (hy : y ∈ OS) (h1 : y.val ≤ b.val) (h2 : b.val < y.val + 32) + (hd : DomW (L.ksb s) b) : False := + J.toBA.os_domA hy h1 h2 hd + +/-- `JStage3BA.capw_domA` at `JStage3TA`. -/ +theorem JStage3TA.capw_domA {b : U64} (hc : W7.CapW H.tcbs {b | InOS OS b} ∅ b) (hd : DomW (L.ksb s) b) : False := + J.toBA.capw_domA hc hd + +/-- `JStage3BA.os_offStepA` at `JStage3TA`. -/ +theorem JStage3TA.os_offStepA {b : U64} (hb : Aligned b) (h : InOS OS b) : ¬ SchedStepWA (st := st) Z m σ s b := + J.toBA.os_offStepA hb h + +/-- `JStage3BA.osKeep_stepA` at `JStage3TA`. -/ +theorem JStage3TA.osKeep_stepA {s' : S} (w : WritesIn st s s' (SchedStepWA (st := st) Z m σ s)) : + OSKeep st OS s s' := + J.toBA.osKeep_stepA w + +/-- `JStage3BA.slot_offStepA` at `JStage3TA`. -/ +theorem JStage3TA.slot_offStepA {y b : U64} (hy : IsSlotA Z.th y) (y1 : y.val ≤ b.val) (y2 : b.val < y.val + 32) : + ¬ SchedStepWA (st := st) Z m σ s b := + J.toBA.slot_offStepA hy y1 y2 + +/-- `JStage3BA.link_offStepA` at `JStage3TA`. -/ +theorem JStage3TA.link_offStepA {t K : U64} (ht : t ∈ Z.T) (hK : K = 3664#u64 ∨ K = 3672#u64) : + ¬ SchedStepWA (st := st) Z m σ s (wa t K) := + J.toBA.link_offStepA ht hK + +/-- `JStage3BA.irq_offStepA` at `JStage3TA`. -/ +theorem JStage3TA.irq_offStepA {i : U64} (hi : i.val < 5376) : + ¬ SchedStepWA (st := st) Z m σ s (irqWordA (L.ksb s) i) := + J.toBA.irq_offStepA hi + +/-- `JStage3BA.asid_offStepA` at `JStage3TA`. -/ +theorem JStage3TA.asid_offStepA {b : U64} (h1 : (L.ksb s).val + 772352 ≤ b.val) + (h2 : b.val < (L.ksb s).val + 773376) : ¬ SchedStepWA (st := st) Z m σ s b := + J.toBA.asid_offStepA h1 h2 + +/-- `JStage3BA.dom_offStepA` at `JStage3TA`. -/ +theorem JStage3TA.dom_offStepA {b : U64} (hd : DomW (L.ksb s) b) : ¬ SchedStepWA (st := st) Z m σ s b := + J.toBA.dom_offStepA hd + +/-- `JStage3BA.ipi_offStepA` at `JStage3TA`. -/ +theorem JStage3TA.ipi_offStepA {o : U64} (ho : o.val = 120 ∨ o.val = 128) : + ¬ SchedStepWA (st := st) Z m σ s (L.nodeAt s (L.cpu s) o) := + J.toBA.ipi_offStepA ho + +/-- `JStage3BA.ipi_capwA` at `JStage3TA`. -/ +theorem JStage3TA.ipi_capwA {o : U64} (ho : o.val = 120 ∨ o.val = 128) + (h : W7.CapW H.tcbs {b | InOS OS b} ∅ (L.nodeAt s (L.cpu s) o)) : False := + J.toBA.ipi_capwA ho h + +omit [J7PpA st J7] in +theorem JStage3TA.ipi_omegaA {o : U64} (ho : o.val = 120 ∨ o.val = 128) + (hΩ : L.nodeAt s (L.cpu s) o ∈ H.omega) : False := + J.toBA.ipi_omegaA ho hΩ + +omit [J7PpA st J7] in +theorem JStage3TA.ipiKeepOf {s' : S} {W : U64 → Prop} (w : WritesIn st s s' W) + (hW : ∀ o : U64, (o.val = 120 ∨ o.val = 128) → ¬ W (L.nodeAt s (L.cpu s) o)) : + Sched.SilentScheduleIpiA (st := st) s' := + J.toBA.ipiKeepOf w hW + +omit [J7PpA st J7] in +/-- (record edit F4) The `KERNEL_ROOT` word is the spec's global user VSpace. -/ +theorem JStage3TA.kroot : + specPtr (L.mem s (Fl4ProofsA64.P1.krA (L := L) s)) = σ.archState.armUsGlobalVspace := + J.domOK.kroot + +end offTA + +set_option maxHeartbeats 1000000 in +/-- **`JStage3A` across a scheduler step** (module docstring). -/ +theorem JStage3TA.of_schedWr {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J5 J7 : JSlotA S} [J7PpA st J7] {s s' : S} + {a a' : A} {σ σ' : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {Q : U64 → U64 → List U64} {Rl : U64 → List U64} {curOf curScOf : Fl4SpecA64.CoreId → Fl4SpecA64.ObjRef} + (w : WritesIn st s s' (SchedStepWA (st := st) Z m σ s)) (HS : SpecSchedA σ σ') (SSz : SSzA σ σ') (Qk : QKeepA σ σ') + (hum : σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory) (har : AR a' Z.hw) + (hcurOf : ∀ k, k ≠ m.curCore → curOf k = Z.curOf k) (bufAl : UtBufAlA Z.T σ') + (ent : EntryInvA3 st H Z.Cs OS U Ac Pp Tb Lv s') + (sq : SchedQueuesInv st H.tcbs Z.Cs H.omega H s' Q Rl) + (poolOff : PoolOff st H Z.Cs OS s') + (sr : SchedSRA st { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } m κ s' σ') + (scCov : ScCovA Z.C m σ') + (ep : EpStateRel Z.T E Rp Bf (L.mem s') σ') + (ntfn : NtfnStateRel N Z.T (L.mem s') σ') + (reply : ReplyStackRelA (L.mem s') Rp Z.C σ' (scNameA m) stack) + (bufCover : BufCover st Z.T Bf s' σ') + (idleNoFault : IdleNoFaultA m σ') + (mcp : McpRelA Z.T m (L.mem s') σ') + (yieldRel : YieldRelA Z.T Z.C m (L.mem s') σ') + (sinv : SInvXA σ' m n) + (sidle : SIdleA σ' m n) + (runVcpu : RunVcpuInv st Z.T s') (hvr : ∀ sc ∈ Z.C, ∀ r ∈ stack sc, RBlk st s' r) + (blkQ : BlkQueuedA Z.T E N (L.mem s') σ') + (noSwitch : ∀ u, σ'.schedulerAction ≠ .switchThread u) + (sact : SActA σ') + (j5 : J5 { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } H OS s' σ' m) + (j7 : J7 { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } H OS s' σ' m) : + JStage3TA st AR { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } H OS U Ac Pp Tb Lv ia ie N E Rp + Bf stack κ n J5 J7 s' a' σ' m := by + have hk := w.1 + have hc := w.2.1 + have osk := J.osKeep_stepA w + have capE : ∀ u ∈ OS, capA st s' u = capA st s u := fun u hu => by + unfold capA; rw [(osk u hu).1, (osk u hu).2.1] + have keep : ∀ b, Aligned b → ¬ SchedStepWA (st := st) Z m σ s b → L.mem s' b = L.mem s b := fun b hb hn => + w.mem_eq hb hn + obtain ⟨Pt, Ub, vr, arm, link, apart⟩ := J7PpA.core (st := st) _ _ _ _ _ _ J.j7 + have P := J.sr.place + have hT : ∀ t ∈ Z.T, t ∈ H.tcbs := fun t ht => by rw [J.tcbs]; exact ht + -- the page words + have page : ∀ p, SchedPageA st Z OS s p → ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → ¬ AsW (L.ksb s) (wa p o) → + L.mem s' (wa p o) = L.mem s (wa p o) := fun p hp o o1 o2 na => by + have hv : VsR (L.ksb s) Pt (wa p o) := Or.inl ⟨p, link p hp, o, o1, o2, rfl⟩ + exact keep _ (vr.al _ hv) (J.vsr_offStepA vr hv na) + have vsr : ∀ b, VsR (L.ksb s) Pt b → L.mem s' b = L.mem s b := fun b hb => by + refine keep _ (vr.al _ hb) ?_ + by_cases h : AsW (L.ksb s) b + · obtain ⟨o, o1, o2, -, e⟩ := h + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.ent.ksw + have ev : b.val = (L.ksb s).val + o.val := by + rw [e, wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact J.asid_offStepA (by omega) (by omega) + · exact J.vsr_offStepA vr hb h + have vsfoot : ∀ b, VsFootA Z.Pp Z.S1 Z.Y (L.ksb s) b → L.mem s' b = L.mem s b := fun b hb => by + refine vsr b ?_ + rcases hb.vsR with ⟨p, hp, h⟩ | h + · refine Or.inl ⟨p, link p ?_, h⟩ + rcases hp with hp | hp | hp + · exact Or.inl hp + · exact Or.inr (Or.inl hp) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl hp)))))) + · exact Or.inr h + have word : ∀ p, SchedPageA st Z OS s p → ∀ i : U64, i.val < 512 → L.mem s' (peAF p i) = L.mem s (peAF p i) := + fun p hp i hi => by + refine vsr _ (Or.inl ⟨p, link p hp, core.num.U64.wrapping_mul i 8#u64, ?_, ?_, rfl⟩) + · rw [wm8_valA hi]; omega + · rw [wm8_valA hi]; omega + have extfoot : ∀ b, ExtFootE Z.X Z.Y Z.T2 b → L.mem s' b = L.mem s b := fun b hb => by + cases hb with + | s2lo r i hr hi => exact word r (Or.inr (Or.inr (Or.inr (Or.inl hr)))) i hi + | s2hi r i hr hi => exact word _ (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨r, hr, rfl⟩))))) i hi + | s2pt t i ht hi => exact word t (Or.inr (Or.inr (Or.inl ht))) i hi + | wl0 y hy => exact word y (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl hy)))))) _ (by simp) + | wlo y i hy hi => exact word y (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl hy)))))) i (by omega) + -- the VCPU words + have vcw : ∀ v ∈ Z.V, ∀ o : U64, o.val = 0 ∨ o.val = 24 → L.mem s' (wa v o) = L.mem s (wa v o) := by + intro v hv o ho + have hH := arm.vh v hv + have hfit := J.ent.heap.fits v _ hH + have hal := J.ent.heap.align v _ hH + simp only [HKind.bits] at hfit hal + have ev : (wa v o).val = v.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have al : Aligned (wa v o) := by show _ % 8 = 0; rw [ev]; omega + have inObj : InObj v (.other 12) (wa v o) := by + unfold InObj; simp only [HKind.bits]; omega + refine keep _ al (J.omega_offStepA ⟨al, v, _, hH, Or.inr (Or.inr (Or.inl ⟨12, rfl⟩)), inObj⟩ ?_ ?_) + · rintro c ⟨z, hz⟩ ⟨c1, c2⟩ + have e : H.scBits c = z := by unfold Heap.scBits; rw [hz] + have ne : v ≠ c := fun h => by rw [h, hz] at hH; cases hH + have := J.ent.heap.disj v _ c _ hH hz ne + simp only [HKind.bits] at this + rw [e] at c2 + omega + · intro g hg e + exact apart _ ⟨v, hv, o, by omega, by omega, rfl⟩ g hg e + have slotw : ∀ y, IsSlotA Z.th y → ∀ x, (x = y ∨ x = wa y 8#u64 ∨ x = wa y 16#u64 ∨ x = wa y 24#u64) → + L.mem s' x = L.mem s x := by + intro y hy x hx + obtain ⟨a32, f32⟩ := hy.al32 P + have hv : ∀ o : U64, o.val < 32 → (wa y o).val = y.val + o.val := fun o ho => by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have key : x.val % 8 = 0 ∧ y.val ≤ x.val ∧ x.val < y.val + 32 := by + rcases hx with rfl | rfl | rfl | rfl + · omega + all_goals + rw [hv _ (by simp)]; simp; omega + exact keep _ key.1 (J.slot_offStepA hy key.2.1 key.2.2) + have cnw : ∀ y, SlotWA Z.th y → IsSlotA Z.th y := fun y hy => by + rcases hy with h | h + · exact h + · obtain ⟨p, r, hp, idx, hl, rfl⟩ := h + obtain ⟨e, hb, -⟩ := cn_slotV P hp hl + exact ⟨p, Or.inl ⟨r, hp, blvA idx, hb, e⟩⟩ + have tcbl : ∀ t ∈ Z.T, ∀ K : U64, K = 3664#u64 ∨ K = 3672#u64 → L.mem s' (wa t K) = L.mem s (wa t K) := by + intro t ht K hK + have hta := J.sr.core.tcbs t ht + refine keep _ (field_aligned (K := K) hta ?_ ?_) (J.link_offStepA ht hK) + all_goals rcases hK with rfl | rfl <;> simp + have mem : SchedKeepMemA (st := st) Z s s' := + { ksb := hk + cpu := hc + slot := fun y hy x hx => slotw y (cnw y hy) x hx + tcbW := fun t ht => ⟨tcbl t ht _ (Or.inl rfl), tcbl t ht _ (Or.inr rfl)⟩ + vcW := fun v hv => by + have e0 : wa v 0#u64 = v := by apply UScalar.eq_of_val_eq; rw [wa_valm]; simp + have h0 := vcw v hv 0#u64 (Or.inl (by simp)) + rw [e0] at h0 + exact ⟨h0, vcw v hv _ (Or.inr (by simp))⟩ + irqW := fun i hi => by + refine keep _ ?_ (J.irq_offStepA hi) + have := irqWordA_val J.sr.ksFit hi + have := J.sr.toSchedSRN.ksb_al + show _ % 8 = 0 + omega + vs := vsfoot + ext := extfoot } + -- the node cursors + have nodes : ∀ c ∈ Z.Cs, + L.mem s' (L.nodeAt s' c layout.node.IDLE_THREAD) ∈ Z.T ∧ L.mem s' (L.nodeAt s' c layout.node.IDLE_SC) ∈ Z.C ∧ + L.mem s' (L.nodeAt s' c layout.node.CUR_THREAD) ∈ Z.T := by + intro c hcs + refine ⟨?_, ?_, ?_⟩ + · have e : L.mem s' (L.nodeAt s' c layout.node.IDLE_THREAD) = L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) := + specPtr_inj ((sr.sch.idleOf c hcs).symm.trans (J.sr.sch.idleOf c hcs)) + rw [e]; exact J.j.1.idleT c hcs + · have e : L.mem s' (L.nodeAt s' c layout.node.IDLE_SC) = L.mem s (L.nodeAt s c layout.node.IDLE_SC) := + specPtr_inj ((sr.sch.idleScOf c hcs).symm.trans (J.sr.sch.idleScOf c hcs)) + rw [e]; exact J.j.1.idleC c hcs + · by_cases h : c = L.cpu s + · have := sr.curT + unfold LawfulStore.curW at this + rw [hc, ← h] at this + exact this + · have hne : c.val ≠ m.curCore := fun e => h (UScalar.eq_of_val_eq (e.trans J.sr.sch.core)) + have h1 := sr.sch.curOfW c hcs + rw [show ({ Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } : XSetsA).curOf = curOf from rfl, + hcurOf _ hne] at h1 + have e : L.mem s' (L.nodeAt s' c layout.node.CUR_THREAD) = L.mem s (L.nodeAt s c layout.node.CUR_THREAD) := + specPtr_inj (h1.symm.trans (J.sr.sch.curOfW c hcs)) + rw [e]; exact J.j.1.curT c hcs + have st' : StEqA Z.T (L.mem s') σ' := fun _ ht _ hx => (ep.at_tcb ht hx).st + have jx := JAtZAX.of_sched J.j (a' := a') (Q := Q) (R := Rl) (curOf := curOf) (curScOf := curScOf) mem HS har + (sr.sch.withSt st') sr.rest (fun c h => (nodes c h).1) (fun c h => (nodes c h).2.1) + (fun c h => (nodes c h).2.2) sr.cscAll + have free : ∀ dev x, UtFreeA st OS s' dev x → UtFreeA st OS s dev x := by + rintro dev x ⟨u, hu, q, b, fi, d, hcap, h⟩ + exact ⟨u, hu, q, b, fi, d, (capE u hu).symm.trans hcap, h⟩ + have dead : ∀ k p, DeadA st OS k s' p → DeadA st OS k s p := fun k p h u hu hp => by + have := h u hu + rw [capE u hu] at this + exact this hp + have nsc : ∀ {p : Fl4SpecA64.ObjRef}, σ.kheap p = none → σ'.kheap p = none := fun {p} h => by + rcases HS.kh p with e | ⟨x, _, e1, -⟩ | ⟨o, o', e1, -, -⟩ + · rw [e]; exact h + · rw [h] at e1; cases e1 + · rw [h] at e1; cases e1 + have keepE : ∀ {q : Fl4SpecA64.ObjRef} {e : Fl4SpecA64.ExtArchObj Fl4SpecA64.PTE}, + σ.kheap q = some (.archObj (.ext e)) → σ'.kheap q = some (.archObj (.ext e)) := fun h1 => + HS.keep h1 (fun _ e => by cases e) (fun e => by rcases e with ⟨_, _, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ <;> cases e) + have unmS2 : ∀ u ∈ OS, ∀ p a v, capA st s u = .Ept p 0#u64 false a v → UnmS2RootA st s σ p → + UnmS2RootA st s' σ' p := by + intro u hu p a v hc U + obtain ⟨es, h1, h2⟩ := U.spec + refine { inv := fun i hi => ?_, spec := ⟨es, keepE h1, h2⟩ } + by_cases hlo : i.val < 512 + · rw [word p (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inr (Or.inl ⟨a, v, hc⟩))⟩))))))) i hlo] + exact U.inv i hi + · obtain ⟨j, hj, e⟩ := peAF_hiA p i (by omega) hi + rw [e, word _ (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inr (Or.inr (Or.inl ⟨p, a, v, hc, rfl⟩)))⟩))))))) j hj, ← e] + exact U.inv i hi + have unmHv : ∀ u ∈ OS, ∀ y a, capA st s u = .HighVSpace y false a → UnmHiRootA st s σ y → + UnmHiRootA st s' σ' y := by + intro u hu y a hc U + obtain ⟨l1, h1, h2⟩ := U.spec + exact { inv := fun i hi => by + rw [word _ (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inr (Or.inr (Or.inr ⟨y, a, hc, rfl⟩)))⟩))))))) i hi] + exact U.inv i hi + spec := ⟨l1, keepE h1, h2⟩ } + have unm : ∀ ty p, SchedPageA st Z OS s p → UnmTabA st Z m s σ ty p → + UnmTabA st { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } m s' σ' ty p := by + intro ty p hp U1 + obtain ⟨tb, h1, h2, h3⟩ := U1.spec + exact + { inv := fun i hi => by rw [word p hp i hi]; exact U1.inv i hi + spec := ⟨tb, HS.keep h1 (fun _ e => by cases e) (fun e => by rcases e with ⟨_, _, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ <;> cases e), h2, h3⟩ + off := ⟨fun i hi h => U1.off.mem i hi (footA_schedA hk hc h), U1.off.obj⟩ } + exact + { ent := ent + j := jx + tcbs := J.tcbs + scs := J.scs + sq := sq + ap := by + have e : L.ksb s' = L.ksb s := hk + rw [e]; exact { J.ap with } + pp := J.pp + s1 := J.s1 + ko := by rw [hk]; exact J.ko + ptH := J.ptH + tabInj := by rw [HS.asid]; exact J.tabInj + poolOff := poolOff + objAlloc := ObjAllocA.keep (Z := Z) J.objAlloc osk + idleAnchor := IdleAnchorOffA.keep J.idleAnchor osk + vsVc := by rw [hk]; exact J.vsVc + sr := sr + scCov := scCov + scSize := fun c hcm x k h => by + obtain ⟨y, hy⟩ := SSz _ x k h + exact J.scSize c hcm y k hy + ep := ep + ntfn := ntfn + reply := reply + bufCover := bufCover + bufNative := fun t ht => by + have hsl : IsSlotA Z.th (slotAddrA (specPtr t) (BitVec.ofNat 64 2)) := + ⟨t, Or.inr ⟨(J.sr.thT t).2 ht, 2, by unfold tcbSlotsA; omega, rfl⟩⟩ + have e : capAtA (L.mem s') (slotAddrA (specPtr t) (BitVec.ofNat 64 2)) = + capAtA (L.mem s) (slotAddrA (specPtr t) (BitVec.ofNat 64 2)) := by + unfold capAtA + rw [slotw _ hsl _ (Or.inl rfl), slotw _ hsl _ (Or.inr (Or.inl rfl))] + rw [e]; exact J.bufNative t ht + iplaced := J.iplaced + ipcPlace := J.ipcPlace + idleNoFault := idleNoFault + mcp := mcp + yieldRel := yieldRel + sinv := sinv + setsInH := { J.setsInH with } + utIpc := J.utIpc + ipcTh := J.ipcTh + deadExt := + { vcpu := fun v hv hd x hx => J.deadExt.vcpu v hv (dead _ v hd) x + (HS.back hx (fun _ e => by cases e) (fun e => by rcases e with ⟨_, _, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ <;> cases e)) + reply := fun r hr hd => J.deadExt.reply r hr (dead _ r hd) + asid := fun i p h hd => J.deadExt.asid i p (by rw [HS.asid] at h; exact h) (dead _ p hd) + tvcpu := fun t ht hd => by + rw [tcbl t ht 3664#u64 (Or.inl rfl)] + exact J.deadExt.tvcpu t ht (dead _ t hd) + vcDeadRun := fun v hv hd => by + rw [vcw v hv 24#u64 (Or.inr (by simp))] + exact J.deadExt.vcDeadRun v hv (dead _ v hd) + replyTcb := fun r hr hd x hx => by + obtain ⟨y, hy, k⟩ := Qk.rt _ x hx + exact k (J.deadExt.replyTcb r hr (dead _ r hd) y hy) } + specFree := + { heap := fun p h => nsc (J.specFree.heap p (free _ _ h)) + cdt := fun x y h p hf => J.specFree.cdt x y (by rw [HS.cdt] at h; exact h) p (free _ _ hf) + orig := fun x h p hf => J.specFree.orig x (by rw [HS.orig] at h; exact h) p (free _ _ hf) } + utBlk := fun u hu q b fi d h => J.utBlk u hu q b fi d ((capE u hu).symm.trans h) + unmPt := + { pt := fun u hu p asid va h => by + have h0 := (capE u hu).symm.trans h + exact unm _ p (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inl ⟨_, _, _, h0⟩⟩))))))) (J.unmPt.pt u hu p asid va h0) + vs := fun u hu p asid h => by + have h0 := (capE u hu).symm.trans h + exact unm _ p (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inl ⟨_, _, h0⟩)⟩))))))) (J.unmPt.vs u hu p asid h0) + s2 := fun u hu p asid va h => by + have h0 := (capE u hu).symm.trans h + exact unmS2 u hu p asid va h0 (J.unmPt.s2 u hu p asid va h0) + hv := fun u hu p asid h => by + have h0 := (capE u hu).symm.trans h + exact unmHv u hu p asid h0 (J.unmPt.hv u hu p asid h0) } + tabS1 := fun q hq => + ⟨fun i hi h => (J.tabS1 q hq).mem i hi (footA_schedA hk hc h), (J.tabS1 q hq).obj⟩ + tabExt := + ⟨fun q hq i hi h => J.tabExt.mem q hq i hi (by + exact footA_schedA (Z := extZA Z) hk hc h), J.tabExt.obj⟩ + specZero := fun x h => by rw [hum]; exact J.specZero x (free _ _ h) + origLive := fun x h => J.origLive x (by rw [HS.orig] at h; exact h) + nullOrig := fun sl h => by + rw [HS.orig] + exact J.nullOrig sl (by rw [← SpecSchedA.specCapAt HS sl]; exact h) + thPos := fun p z h => J.thPos p z h + frBase := J.frBase.keepA capE + devFr := J.devFr.keepA capE fun p dev pg h => HS.keep h (fun _ e => by cases e) + (fun e => by rcases e with ⟨_, _, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ <;> cases e) + finCov := J.finCov.keepA + (fun q r cs h => HS.back h (fun _ e => by cases e) + (fun e => by rcases e with ⟨_, _, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ <;> cases e)) + (fun q x h => HS.tcbBack h) + idleAll := J.idleAll.keepA + (fun c hcs => specPtr_inj ((sr.sch.idleOf c hcs).symm.trans (J.sr.sch.idleOf c hcs))) + (fun c hcs => specPtr_inj ((sr.sch.idleScOf c hcs).symm.trans (J.sr.sch.idleScOf c hcs))) + ipcHeapCov := J.ipcHeapCov + zombieNoVcpu := J.zombieNoVcpu.keepA capE J.zombieTcb + fun t ht => tcbl t (J.tcbs ▸ ht) 3664#u64 (Or.inl rfl) + zombieTcb := J.zombieTcb.keepA capE + bufOff := J.bufOff.keepA (Z := Z) (Z' := { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf }) + hk rfl rfl rfl rfl rfl rfl rfl capE + vsOk := by + have e := (utVsPt_keepA J.ent.vs hk (fun b hb => by + refine keep b (J.vsOk.al b hb) ?_ + by_cases h : AsW (L.ksb s) b + · obtain ⟨o, o1, o2, -, e⟩ := h + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.ent.ksw + have ev : b.val = (L.ksb s).val + o.val := by + rw [e, wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact J.asid_offStepA (by omega) (by omega) + · exact J.vsr_offStepA J.vsOk hb h)).2 + unfold UtVsOkA + rw [show L.ksb s' = L.ksb s from hk, e] + exact J.vsOk + domOK := J.domOK.keepS J.sr.toSchedSRN Qk.dom hk hc (fun b hb hd => w.mem_eq hb (J.dom_offStepA hd)) + ipiZero := J.ipiKeepOf w fun o ho => J.ipi_offStepA ho + bufVs := by + have e := (utVsPt_keepA J.ent.vs hk (fun b hb => by + refine keep b (J.vsOk.al b hb) ?_ + by_cases h : AsW (L.ksb s) b + · obtain ⟨o, o1, o2, -, e⟩ := h + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.ent.ksw + have ev : b.val = (L.ksb s).val + o.val := by + rw [e, wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact J.asid_offStepA (by omega) (by omega) + · exact J.vsr_offStepA J.vsOk hb h)).2 + rw [show L.ksb s' = L.ksb s from hk, e] + exact J.bufVs + bufAl := bufAl + zombieBnd := J.zombieBnd.keepA capE + devUtOff := J.devUtOff.keepA (Z := Z) capE hk (fun _ _ h => h) rfl + sidle := sidle + capTy := J.capTy.keepA capE + scCpu := J.scCpu + scNZ := J.scNZ + vcRun := J.vcRun.keepA (fun t ht => tcbl t ht 3664#u64 (Or.inl rfl)) (fun v hv => vcw v hv 24#u64 (Or.inr (by simp))) + (fun v hv => by + have h0 := vcw v hv 0#u64 (Or.inl (by simp)) + rwa [wa_zeroVcA] at h0) + (fun t ht => tcbl t ht 3672#u64 (Or.inr rfl)) + sact := sact + runVcpu := runVcpu + vr := hvr + ciSc := fun u hu p b h => J.ciSc u hu p b ((capE u hu).symm.trans h) + noSwitch := noSwitch + blkQ := blkQ + curVcpuNone := HS.vcur.trans J.curVcpuNone + ipcSorted := J.ipcSorted.of_q Qk + activeReplyScs := J.activeReplyScs.of_q Qk + j5 := j5 + j7 := j7 } + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3ScopeA.lean b/proofs/Fl4ProofsA64/P3/Stage3ScopeA.lean new file mode 100644 index 000000000..2e780aa9d --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ScopeA.lean @@ -0,0 +1,272 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Plan B): **`hScope`'s provider**, and the first closed Send / NBSend / Call bodies. + +* `JSyscallA.hScopeA`: `JSyscallA.invocationA`'s `hScope` from a scope member at every invocation site (`SiteA`): + the `InvSpecA` clause is `DispScope4A.invSpecA`. The members come from `P3/Stage3ScopeArmsA`, one theorem per + scope constructor, whose hypotheses past `hlab` are the arm's inputs. +* `JSyscallA.invocationScopeA`: `invocationA` from such members, with `hP1` discharged (`epiP1A`). +* `JSyscallA.sendBodyScopeA`: `dispatch_syscall` for Send, NBSend and Call, from `JSyscallA` to the record at a + body's end (`A64RelS3`). +* `JSyscallA.cnodeCopyBodyA`: that body for CNode Copy / Mint / Move / Mutate / Rotate; the arm's inputs come from + the run and the record (`JSyscallA.cnCopy_armA`, `JSyscallA.cnodeMove_runA`, `P3/Stage3CnArmFactsA`; + `JSyscallA.cnodeRotate_runA`, `P3/Stage3CnRotFactsA`). + +Named plug-ins: `hFault`, `hErr`, `HEnv` (`HPreEnvA`) [J] (`P3/Stage3WrapA`), and for the generic bodies a scope member at each +successful or preempted decode. New module. +-/ +import Fl4ProofsA64.P3.Stage3CnRotFactsA +import Fl4ProofsA64.P3.Stage3EpiP1A +import Fl4ProofsA64.P3.Stage3InvSpecL1TopA +import Fl4ProofsA64.P3.Stage3HPreA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {Clock : A → Fl4SpecA64.MachineState → Prop} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {ar : machine.Arch A} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- **`invocationA`'s `hScope`** from a scope member at every invocation site (module docstring). -/ +theorem JSyscallA.hScopeA [PlainStore st] (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (hCnt : Sched.CounterStepA σ) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) (hIP : IrqPlatA st ar) (calling blocking donate : Bool) (cptr : U64) + (hArm : ∀ (info : msginfo.MessageInfo) (buf : U64) (r1 : cspace.Resolved) (e : Aeneas.Std.Array obj.Slot 3#usize) + (mi : Fl4SpecA64.MessageInfo) (sl : Fl4SpecA64.CslotPtr) (c : Fl4SpecA64.Cap) + (excaps : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) (buffer : Option Fl4SpecA64.ObjRef) (argsU : List U64), + msg.msg_info st s (L.curW s) = ok info → msg.lookup_ipc_buffer st s false (L.curW s) = ok buf → + cspace.lookup_slot st s (L.curW s) cptr = ok (.Ok r1) → + ipc.lookup_extra_caps st s (L.curW s) buf info = ok (.Ok e) → + SiteA st Z OS s σ info buf r1.slot e mi sl c excaps buffer argsU → + ∀ (r0 : core.result.Result Unit error.Exc) (s0 : S) (a0 : A), + syscall.decode_invocation st ar s a info.label (msgLenA info buf) r1.slot (capA st s r1.slot) blocking + calling donate false buf e = ok (r0, s0, a0) → (∀ x, r0 = .Err x → x = error.Exc.Preempted) → + ∃ Out, DispScopeA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ m Clock hPl.ipa info.label + (msgLenA info buf) r1.slot blocking calling donate false buf e Out) : + ∀ (info : msginfo.MessageInfo) (buf : U64) (r1 : cspace.Resolved) + (e : Aeneas.Std.Array obj.Slot 3#usize), + msg.msg_info st s (L.curW s) = ok info → msg.lookup_ipc_buffer st s false (L.curW s) = ok buf → + cspace.lookup_slot st s (L.curW s) cptr = ok (.Ok r1) → + ipc.lookup_extra_caps st s (L.curW s) buf info = ok (.Ok e) → + ∀ (r0 : core.result.Result Unit error.Exc) (s0 : S) (a0 : A), + syscall.decode_invocation st ar s a info.label (msgLenA info buf) r1.slot (capA st s r1.slot) blocking + calling donate false buf e = ok (r0, s0, a0) → (∀ x, r0 = .Err x → x = error.Exc.Preempted) → + ∃ Out, DispScopeA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ m Clock hPl.ipa info.label + (msgLenA info buf) r1.slot blocking calling donate false buf e Out ∧ + ∀ (r : core.result.Result Unit error.Exc) (s' : S) (a' : A) (σ' : Fl4SpecA64.State Unit), + syscall.decode_invocation st ar s a info.label (msgLenA info buf) r1.slot (capA st s r1.slot) blocking + calling donate false buf e = ok (r, s', a') → Out σ' → + ∀ (done : Bool), (done = true ∧ r = .Ok ()) ∨ (done = false ∧ r = .Err error.Exc.Preempted) → + ∀ (mi : Fl4SpecA64.MessageInfo) (sl : Fl4SpecA64.CslotPtr) (c : Fl4SpecA64.Cap) + (excaps : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) (buffer : Option Fl4SpecA64.ObjRef) + (args : List Fl4SpecA64.Data), + getMessageInfo (Z := Unit) σ.curThread σ = ({(mi, σ)}, False) → + (frontSpecA σ.curThread (Fl4SpecA64.toBl cptr.bv) mi).run σ = ({(.ok (sl, c, excaps, buffer), σ)}, False) → + r1.slot = cslotAddrA sl → CapRelA (capA st s r1.slot) c → ExtraOKA Z.th (L.mem s) e excaps → + AbsSlotA Z.th sl → BufViewA (L.mem s) σ.machineState.underlyingMemory buf buffer → + getCap (Z := Unit) sl σ = ({(c, σ)}, False) → + lookupIpcBuffer (Z := Unit) false σ.curThread σ = ({(buffer, σ)}, False) → + (lookupExtraCaps (Z := Unit) σ.curThread buffer mi).run σ = ({(.ok excaps, σ)}, False) → + getMrs (Z := Unit) σ.curThread buffer mi σ = ({(args, σ)}, False) → + InvSpecA calling blocking donate done mi.miLabel args (Fl4SpecA64.toBl cptr.bv) sl c excaps buffer σ σ' := by + intro info buf r1 e einfo ebuf elr eex r0 s0 a0 hdec0 hgood + obtain ⟨mi, sl, c, excaps, buffer, argsU, Q⟩ := J.siteA einfo ebuf elr eex + obtain ⟨Out, h⟩ := hArm info buf r1 e mi sl c excaps buffer argsU einfo ebuf elr eex Q r0 s0 a0 hdec0 hgood + refine ⟨Out, h, ?_⟩ + intro r s' a' σ' hd hOut done hdone mi' sl' c' excaps' buffer' args hmi front hsl hc hok hab hBV gc eib hex gargs + exact DispScopeA.invSpecA (hCnt := hCnt) hA hM hPl hIP J hfm h einfo r s' a' σ' hd hOut done hdone mi' sl' c' excaps' buffer' args hmi + front hsl hc hok hab hBV gc eib hex gargs + +/-- **`handle_invocation` from scope members** (module docstring). -/ +theorem JSyscallA.invocationScopeA [PlainStore st] (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (hCnt : Sched.CounterStepA σ) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) (hIP : IrqPlatA st ar) (rep calling blocking donate : Bool) (cptr : U64) + (hFault : blocking = true → ∀ (f : error.Fault) (f' : Fl4SpecA64.Fault), CapFaultCorrA f f' → + faults.handle_fault st ar s a (L.curW s) f ⦃ r => ∃ τ, ((), τ) ∈ (handleFault (Z := Unit) σ.curThread f' σ).1 ∧ + A64RelS3 st AR ar hPl.ipa n Z.Cs r.1 r.2 τ m ⦄) + (hArm : ∀ (info : msginfo.MessageInfo) (buf : U64) (r1 : cspace.Resolved) (e : Aeneas.Std.Array obj.Slot 3#usize) + (mi : Fl4SpecA64.MessageInfo) (sl : Fl4SpecA64.CslotPtr) (c : Fl4SpecA64.Cap) + (excaps : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) (buffer : Option Fl4SpecA64.ObjRef) (argsU : List U64), + msg.msg_info st s (L.curW s) = ok info → msg.lookup_ipc_buffer st s false (L.curW s) = ok buf → + cspace.lookup_slot st s (L.curW s) cptr = ok (.Ok r1) → + ipc.lookup_extra_caps st s (L.curW s) buf info = ok (.Ok e) → + SiteA st Z OS s σ info buf r1.slot e mi sl c excaps buffer argsU → + ∀ (r0 : core.result.Result Unit error.Exc) (s0 : S) (a0 : A), + syscall.decode_invocation st ar s a info.label (msgLenA info buf) r1.slot (capA st s r1.slot) blocking + calling donate false buf e = ok (r0, s0, a0) → (∀ x, r0 = .Err x → x = error.Exc.Preempted) → + ∃ Out, DispScopeA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ m Clock hPl.ipa info.label + (msgLenA info buf) r1.slot blocking calling donate false buf e Out) + (hErr : ∀ (info : msginfo.MessageInfo) (buf : U64) (r1 : cspace.Resolved) + (e : Aeneas.Std.Array obj.Slot 3#usize) (x : error.Exc) (s' : S) (a' : A), + msg.msg_info st s (L.curW s) = ok info → msg.lookup_ipc_buffer st s false (L.curW s) = ok buf → + cspace.lookup_slot st s (L.curW s) cptr = ok (.Ok r1) → + ipc.lookup_extra_caps st s (L.curW s) buf info = ok (.Ok e) → + syscall.decode_invocation st ar s a info.label (msgLenA info buf) r1.slot (capA st s r1.slot) blocking + calling donate false buf e = ok (.Err x, s', a') → x ≠ error.Exc.Preempted → + (match x with + | .Sys e2 => if calling then (do let s2 ← syscall.reply_error st s' (L.curW s) e2 + ok (core.result.Result.Ok (), s2, a')) + else ok (core.result.Result.Ok (), s', a') + | _ => ok (core.result.Result.Ok (), s', a') : Result (core.result.Result Unit error.Exc × S × A)) + ⦃ r => r.1 = .Ok () ∧ ∃ τ, + (.ok (), τ) ∈ ((fl4HandleInvocationR (Z := Unit) rep calling blocking donate false + (Fl4SpecA64.toBl cptr.bv)).run σ).1 ∧ A64RelS3 st AR ar hPl.ipa n Z.Cs r.2.1 r.2.2 τ m ⦄) : + syscall.handle_invocation st ar s a calling blocking donate false cptr ⦃ r => + ∃ e τ, (e, τ) ∈ ((fl4HandleInvocationR (Z := Unit) rep calling blocking donate false + (Fl4SpecA64.toBl cptr.bv)).run σ).1 ∧ + r.1 = (match e with | .ok _ => .Ok () | .error _ => .Err error.Exc.Preempted) ∧ + A64RelES3 st AR ar hPl.ipa n Z.Cs e r.2.1 r.2.2 τ m Clock ⦄ := + J.invocationA (hCnt := hCnt) hA hM hPl hfm rep calling blocking donate cptr hFault + (J.hScopeA (hCnt := hCnt) hA hM hPl hfm hIP calling blocking donate cptr hArm) hErr (epiP1A hA hPl calling) + +/-- **Send, NBSend and Call from the syscall-entry record** (module docstring). -/ +theorem JSyscallA.sendBodyScopeA [PlainStore st] (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (hCnt : Sched.CounterStepA σ) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) (hIP : IrqPlatA st ar) (rep : Bool) {pending : SMonad Unit Unit} (call : Fl4SpecA64.Syscall) + (hc : call = .sysSend ∨ call = .sysNBSend ∨ call = .sysCall) + (hFault : ∀ calling blocking donate, + DispatchInvokeAtA (L.mem s (regWA (L.curW s) layout.reg.CAP)) (L.mem s (regWA (L.curW s) layout.reg.REPLY)) + (L.mem s (regWA (L.curW s) layout.reg.NBSEND_RECV_DEST)) call calling blocking donate false + (L.mem s (regWA (L.curW s) layout.reg.CAP)) → + blocking = true → ∀ (f : error.Fault) (f' : Fl4SpecA64.Fault), CapFaultCorrA f f' → + faults.handle_fault st ar s a (L.curW s) f ⦃ r => ∃ τ, ((), τ) ∈ (handleFault (Z := Unit) σ.curThread f' σ).1 ∧ + A64RelS3 st AR ar hPl.ipa n Z.Cs r.1 r.2 τ m ⦄) + (hArm : ∀ calling blocking donate, + DispatchInvokeAtA (L.mem s (regWA (L.curW s) layout.reg.CAP)) (L.mem s (regWA (L.curW s) layout.reg.REPLY)) + (L.mem s (regWA (L.curW s) layout.reg.NBSEND_RECV_DEST)) call calling blocking donate false + (L.mem s (regWA (L.curW s) layout.reg.CAP)) → + ∀ (info : msginfo.MessageInfo) (buf : U64) (r1 : cspace.Resolved) (e : Aeneas.Std.Array obj.Slot 3#usize) + (mi : Fl4SpecA64.MessageInfo) (sl : Fl4SpecA64.CslotPtr) (c : Fl4SpecA64.Cap) + (excaps : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) (buffer : Option Fl4SpecA64.ObjRef) (argsU : List U64), + msg.msg_info st s (L.curW s) = ok info → msg.lookup_ipc_buffer st s false (L.curW s) = ok buf → + cspace.lookup_slot st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.CAP)) = ok (.Ok r1) → + ipc.lookup_extra_caps st s (L.curW s) buf info = ok (.Ok e) → + SiteA st Z OS s σ info buf r1.slot e mi sl c excaps buffer argsU → + ∀ (r0 : core.result.Result Unit error.Exc) (s0 : S) (a0 : A), + syscall.decode_invocation st ar s a info.label (msgLenA info buf) r1.slot (capA st s r1.slot) blocking + calling donate false buf e = ok (r0, s0, a0) → (∀ x, r0 = .Err x → x = error.Exc.Preempted) → + ∃ Out, DispScopeA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ m Clock hPl.ipa info.label + (msgLenA info buf) r1.slot blocking calling donate false buf e Out) + (hErr : ∀ calling blocking donate, + DispatchInvokeAtA (L.mem s (regWA (L.curW s) layout.reg.CAP)) (L.mem s (regWA (L.curW s) layout.reg.REPLY)) + (L.mem s (regWA (L.curW s) layout.reg.NBSEND_RECV_DEST)) call calling blocking donate false + (L.mem s (regWA (L.curW s) layout.reg.CAP)) → + ∀ (info : msginfo.MessageInfo) (buf : U64) (r1 : cspace.Resolved) + (e : Aeneas.Std.Array obj.Slot 3#usize) (x : error.Exc) (s' : S) (a' : A), + msg.msg_info st s (L.curW s) = ok info → msg.lookup_ipc_buffer st s false (L.curW s) = ok buf → + cspace.lookup_slot st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.CAP)) = ok (.Ok r1) → + ipc.lookup_extra_caps st s (L.curW s) buf info = ok (.Ok e) → + syscall.decode_invocation st ar s a info.label (msgLenA info buf) r1.slot (capA st s r1.slot) blocking + calling donate false buf e = ok (.Err x, s', a') → x ≠ error.Exc.Preempted → + (match x with + | .Sys e2 => if calling then (do let s2 ← syscall.reply_error st s' (L.curW s) e2 + ok (core.result.Result.Ok (), s2, a')) + else ok (core.result.Result.Ok (), s', a') + | _ => ok (core.result.Result.Ok (), s', a') : Result (core.result.Result Unit error.Exc × S × A)) + ⦃ r => r.1 = .Ok () ∧ ∃ τ, + (.ok (), τ) ∈ ((fl4HandleInvocationR (Z := Unit) rep calling blocking donate false + (Fl4SpecA64.toBl (L.mem s (regWA (L.curW s) layout.reg.CAP)).bv)).run σ).1 ∧ A64RelS3 st AR ar hPl.ipa n Z.Cs r.2.1 r.2.2 τ m ⦄) + (XP : ExitPlatA st ar Z) (HEnv : HPreEnvA st AR ar hPl n Z.Cs m Clock pending s a call) : + syscall.dispatch_syscall st ar s a (syscallNumberA call) ⦃ r => + StepPostA (fun s a τ => A64RelS3 st AR ar hPl.ipa n Z.Cs s a τ m) + (fl4SettleDispatch pending (fl4DispatchSyscall (fl4HandleInvocationR (Z := Unit) rep) call)) σ r ⦄ := by + have htZ : L.curW s ∈ Z.T := J.sr.curT + obtain ⟨x, hk, -⟩ := J.ep.tcb (L.curW s) htZ + have regs := (J.ep.at_tcb htZ hk).regs + have reg : ∀ (r : Fl4SpecA64.Register) (o : U64), Endp.regA (L.curW s) (regIdxA r) = regWA (L.curW s) o → + x.tcbArch.tcbContext.userRegs r = (L.mem s (regWA (L.curW s) o)).bv := fun r o e => by + rw [← e]; exact (regs r).symm + have hta := J.iplaced.n.tcb _ htZ + have off : ∀ (r : Fl4SpecA64.Register) (o : U64), (regIdxA r).val = o.val → + Endp.regA (L.curW s) (regIdxA r) = regWA (L.curW s) o := fun r o e => by + unfold Endp.regA regWA + rw [UScalar.eq_of_val_eq e] + refine sendBody_of_invocationA (st := st) (ar := ar) (a := a) + (InvRel := fun e s' a' τ => A64RelES3 st AR ar hPl.ipa n Z.Cs e s' a' τ m Clock) call hc (cur_thread_ok s) J.j.1.sch.curThread hk + (reg cap_register layout.reg.CAP (off _ _ (by simp [cap_register, capRegister, regIdxA, layout.reg.CAP, layout.reg.X0]))) + (reg replyRegister layout.reg.REPLY (off _ _ (by simp [replyRegister, regIdxA, layout.reg.REPLY, layout.reg.X6]))) + (reg nbsendRecvDest layout.reg.NBSEND_RECV_DEST (off _ _ (by simp [nbsendRecvDest, regIdxA, layout.reg.NBSEND_RECV_DEST, layout.reg.X8]))) + (fun calling blocking donate h => ?_) (HPreA.ofEnv hA hPl XP HEnv) + exact J.invocationScopeA (hCnt := hCnt) hA hM hPl hfm hIP rep calling blocking donate _ (hFault calling blocking donate h) + (hArm calling blocking donate h) (hErr calling blocking donate h) + +/-- **CNode Copy / Mint / Move / Mutate / Rotate end to end** (module docstring). -/ +theorem JSyscallA.cnodeCopyBodyA [PlainStore st] (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (hCnt : Sched.CounterStepA σ) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) (hIP : IrqPlatA st ar) (rep : Bool) {pending : SMonad Unit Unit} (call : Fl4SpecA64.Syscall) + (hc : call = .sysSend ∨ call = .sysNBSend ∨ call = .sysCall) + (hFault : ∀ calling blocking donate, + DispatchInvokeAtA (L.mem s (regWA (L.curW s) layout.reg.CAP)) (L.mem s (regWA (L.curW s) layout.reg.REPLY)) + (L.mem s (regWA (L.curW s) layout.reg.NBSEND_RECV_DEST)) call calling blocking donate false + (L.mem s (regWA (L.curW s) layout.reg.CAP)) → + blocking = true → ∀ (f : error.Fault) (f' : Fl4SpecA64.Fault), CapFaultCorrA f f' → + faults.handle_fault st ar s a (L.curW s) f ⦃ r => ∃ τ, ((), τ) ∈ (handleFault (Z := Unit) σ.curThread f' σ).1 ∧ + A64RelS3 st AR ar hPl.ipa n Z.Cs r.1 r.2 τ m ⦄) + -- the invoked cap is a CNode and the label is Copy, Mint, Move, Mutate or Rotate (24..28; this body's case) + (hSel : ∀ (info : msginfo.MessageInfo) (buf : U64) (r1 : cspace.Resolved), + msg.msg_info st s (L.curW s) = ok info → msg.lookup_ipc_buffer st s false (L.curW s) = ok buf → + cspace.lookup_slot st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.CAP)) = ok (.Ok r1) → + (24 ≤ info.label.val ∧ info.label.val ≤ 28) ∧ ∃ p r gs g, capA st s r1.slot = .CNode p r gs g) + (hErr : ∀ calling blocking donate, + DispatchInvokeAtA (L.mem s (regWA (L.curW s) layout.reg.CAP)) (L.mem s (regWA (L.curW s) layout.reg.REPLY)) + (L.mem s (regWA (L.curW s) layout.reg.NBSEND_RECV_DEST)) call calling blocking donate false + (L.mem s (regWA (L.curW s) layout.reg.CAP)) → + ∀ (info : msginfo.MessageInfo) (buf : U64) (r1 : cspace.Resolved) + (e : Aeneas.Std.Array obj.Slot 3#usize) (x : error.Exc) (s' : S) (a' : A), + msg.msg_info st s (L.curW s) = ok info → msg.lookup_ipc_buffer st s false (L.curW s) = ok buf → + cspace.lookup_slot st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.CAP)) = ok (.Ok r1) → + ipc.lookup_extra_caps st s (L.curW s) buf info = ok (.Ok e) → + syscall.decode_invocation st ar s a info.label (msgLenA info buf) r1.slot (capA st s r1.slot) blocking + calling donate false buf e = ok (.Err x, s', a') → x ≠ error.Exc.Preempted → + (match x with + | .Sys e2 => if calling then (do let s2 ← syscall.reply_error st s' (L.curW s) e2 + ok (core.result.Result.Ok (), s2, a')) + else ok (core.result.Result.Ok (), s', a') + | _ => ok (core.result.Result.Ok (), s', a') : Result (core.result.Result Unit error.Exc × S × A)) + ⦃ r => r.1 = .Ok () ∧ ∃ τ, + (.ok (), τ) ∈ ((fl4HandleInvocationR (Z := Unit) rep calling blocking donate false + (Fl4SpecA64.toBl (L.mem s (regWA (L.curW s) layout.reg.CAP)).bv)).run σ).1 ∧ A64RelS3 st AR ar hPl.ipa n Z.Cs r.2.1 r.2.2 τ m ⦄) + (XP : ExitPlatA st ar Z) (HEnv : HPreEnvA st AR ar hPl n Z.Cs m Clock pending s a call) : + syscall.dispatch_syscall st ar s a (syscallNumberA call) ⦃ r => + StepPostA (fun s a τ => A64RelS3 st AR ar hPl.ipa n Z.Cs s a τ m) + (fl4SettleDispatch pending (fl4DispatchSyscall (fl4HandleInvocationR (Z := Unit) rep) call)) σ r ⦄ := + J.sendBodyScopeA (hCnt := hCnt) hA hM hPl hfm hIP rep call hc hFault + (fun calling blocking donate _ info buf r1 e mi sl c excaps buffer argsU einfo ebuf elr _ Q r0 s0 a0 hdec0 hgood => by + obtain ⟨hlb, p, r, gs, g, hslc⟩ := hSel info buf r1 einfo ebuf elr + have hr0 : r0 = .Ok () := by + cases r0 with + | Ok u => cases u; rfl + | Err x => + exact absurd (hgood x rfl) (J.cnCopy_notPreemptedA Q hslc hlb hdec0) + subst hr0 + have h21 := lblA_copy; have h25 := lblA_mint; have h26 := lblA_move; have h27 := lblA_mutate + have h28 := lblA_rotate + by_cases hrt : info.label = fl4_abi.label.CNodeRotate + · exact DispScopeA.of4 (J.cnodeRotate_runA hA hM hPl hfm Q hslc hrt hdec0) + by_cases hcm : info.label = fl4_abi.label.CNodeCopy ∨ info.label = fl4_abi.label.CNodeMint + · obtain ⟨X⟩ := J.cnCopy_armA Q hslc hcm hdec0 + exact DispScopeA.of4 (J.cnodeCopyArm_scopeA hA hM hPl hfm Q X blocking calling donate) + · have hmm : info.label = fl4_abi.label.CNodeMove ∨ info.label = fl4_abi.label.CNodeMutate := by + rw [u64_eq_iffA] at hrt + rw [u64_eq_iffA, u64_eq_iffA] at hcm ⊢ + omega + exact DispScopeA.of4 (J.cnodeMove_runA hA hM hPl hfm Q hslc hmm hdec0)) + hErr XP HEnv + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3ScopeArmsA.lean b/proofs/Fl4ProofsA64/P3/Stage3ScopeArmsA.lean new file mode 100644 index 000000000..933a06e2b --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3ScopeArmsA.lean @@ -0,0 +1,960 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Plan B): **the dispatch scope at the invocation site**, one theorem per scope +constructor (`hScope`'s provider, `P3/Stage3ScopeA`). From the site (`SiteA`: the front end's values and the slot +facts) and the arm's decode-site facts, a `DispScope4A` member at `info.label`, `msgLenA info buf`, the looked-up +slot and the extra slots, with the constructor's pin discharged (`P3/Stage3Pin{Vs,Cn}A`). The hypotheses of each +theorem past `hlab` are the arm's inputs. + +Generated (WS/handoff/tools/gen_planb_scope.py). Named premises: [J] `JSyscallA`, `hcan` (`ptUnmapS1`, `ptUnmapX`), +the `∀ anc` facts of the CNode arms; those of each constructor. New module. +-/ +import Fl4ProofsA64.P3.Stage3SiteA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {Clock : A → Fl4SpecA64.MachineState → Prop} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {ar : machine.Arch A} {s : S} {a : A} {σ : State Unit} {m : McState} + {info : msginfo.MessageInfo} {buf slot : U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} {mi : MessageInfo} + {sl : CslotPtr} {c : Cap} {excaps : List (Cap × CslotPtr)} {buffer : Option ObjRef} {argsU : List U64} +variable (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) (hfm : A64FrameMasks st s) +include hA hM hPl hfm + +set_option maxHeartbeats 800000 + +/-- `VsScopeA.frameMapS1` at the invocation site. -/ +theorem JSyscallA.frameMapS1_scopeA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (Q : SiteA st Z OS s σ info buf slot extra mi sl c excaps buffer argsU) + (hlab : info.label = fl4_abi.label.ARMPageMap) + {s3 : S} + {a3 : A} + {fp asid0 va0 rt : U64} + {sz : capability.FrameSize} + {dev mp : Bool} + {bB bD bF : Bool} + (K : ArchKeepsA ar AR) + (hcap : capAtA (L.mem s) slot = .Frame fp sz rt dev mp false asid0 va0) + (FP : FrPlA sz fp mp asid0) + (call : Bool) + (x0 : U64) + (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) + (hS1 : ∀ p l mp' asid va, capAtA (L.mem s) x0 ≠ .Ept p l mp' asid va) + (hHi : ∀ p mp' asid, capAtA (L.mem s) x0 ≠ .HighVSpace p mp' asid) + (hdec : vspace.decode_frame st ar s a fl4_abi.label.ARMPageMap (msgLenA info buf) slot + (.Frame fp sz rt dev mp false asid0 va0) call buf extra = ok (core.result.Result.Ok (), s3, a3)) : + ∃ Out, DispScope4A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ m Clock hPl.ipa info.label + (msgLenA info buf) slot bB call bD bF buf extra Out := by + rw [hlab] + have hsl := Q.F.pin.hslot + subst hsl + exact ⟨_, Or.inl (VsScopeA.frameMapS1 (s3 := s3) (a3 := a3) (fp := fp) (asid0 := asid0) (va0 := va0) (rt := rt) (sz := sz) (dev := dev) (mp := mp) (bB := bB) (bD := bD) (bF := bF) (K := K) (hnvm := Q.nvm) (cte := sl) (hab := Q.F.pin.habs) (hcap := hcap) (FP := FP) (slbl := (specLabelWord (archL .armPageMap))) (hl := (by rw [invType_specA]; rfl)) (length := (msgLenA info buf)) (buf := buf) (args := argsU) (hlen := Q.F.len) (hargs := Q.F.args) (call := call) (extra := extra) (x0 := x0) (hx0 := hx0) (exs := excaps) (hex := (by rw [show x0 = exAtA extra 0 from okinj (hx0.symm.trans (exAtA_idx0 extra))]; exact Q.F.extra.rel1 Q.place)) (hS1 := hS1) (hHi := hHi) (hdec := hdec) (hcov := Q.cov) (hext := Q.hext) (hSC := Q.hSC) (hpin := (frameMapS1_pinA Q.F hlab ((capAtA_eq _ _).symm.trans hcap) hx0 hS1 hHi)))⟩ + +/-- `VsScopeA.frameUnmapS1` at the invocation site. -/ +theorem JSyscallA.frameUnmapS1_scopeA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (Q : SiteA st Z OS s σ info buf slot extra mi sl c excaps buffer argsU) + (hlab : info.label = fl4_abi.label.ARMPageUnmap) + {bB bD bF : Bool} + (K : ArchKeepsA ar AR) + (hTO : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s q) + (call : Bool) + (fp rt asid va : U64) + (sz : capability.FrameSize) + (dev : Bool) + (h0 : asid ≠ 0#u64) + (hcap : capA st s slot = .Frame fp sz rt dev true false asid va) + (hS1 : Fl4SpecA64.asidRootKind σ (BitVec.ofNat 16 asid.val) = none ∨ + Fl4SpecA64.asidRootKind σ (BitVec.ofNat 16 asid.val) = some .stage1) : + ∃ Out, DispScope4A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ m Clock hPl.ipa info.label + (msgLenA info buf) slot bB call bD bF buf extra Out := by + rw [hlab] + have hsl := Q.F.pin.hslot + subst hsl + exact ⟨_, Or.inl (VsScopeA.frameUnmapS1 (bB := bB) (bD := bD) (bF := bF) (K := K) (hnvm := Q.nvm) (hTO := hTO) (length := (msgLenA info buf)) (buf := buf) (call := call) (ex := extra) (cte := sl) (hab := Q.F.pin.habs) (fp := fp) (rt := rt) (asid := asid) (va := va) (sz := sz) (dev := dev) (h0 := h0) (hcap := hcap) (hS1 := hS1) (hcov := Q.cov) (hext := Q.hext) (hSC := Q.hSC) (hpin := (frameUnmapS1_pinA Q.F hlab hcap hS1)))⟩ + +/-- `VsScopeA.frameUnmapX` at the invocation site. -/ +theorem JSyscallA.frameUnmapX_scopeA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (Q : SiteA st Z OS s σ info buf slot extra mi sl c excaps buffer argsU) + (hlab : info.label = fl4_abi.label.ARMPageUnmap) + {bB bD bF : Bool} + (K : ArchKeepsA ar AR) + (hTO : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s q) + (hEO : ExtTabOffA (L := L) Z m s) + (hIpa : S2IpaE ar a) + (call : Bool) + (fp rt asid va : U64) + (sz : capability.FrameSize) + (dev isEpt : Bool) + (h0 : asid ≠ 0#u64) + (hcap : capA st s slot = .Frame fp sz rt dev true isEpt asid va) + (k : Fl4SpecA64.ExtRootKind) + (hkA : isEpt = false → Fl4SpecA64.asidRootKind σ (BitVec.ofNat 16 asid.val) = some k ∧ k ≠ .stage1) + (hk2 : isEpt = true → k = .stage2) : + ∃ Out, DispScope4A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ m Clock hPl.ipa info.label + (msgLenA info buf) slot bB call bD bF buf extra Out := by + rw [hlab] + have hsl := Q.F.pin.hslot + subst hsl + exact ⟨_, Or.inl (VsScopeA.frameUnmapX (bB := bB) (bD := bD) (bF := bF) (K := K) (hnvm := Q.nvm) (hTO := hTO) (hEO := hEO) (hIpa := hIpa) (length := (msgLenA info buf)) (buf := buf) (call := call) (ex := extra) (cte := sl) (hab := Q.F.pin.habs) (fp := fp) (rt := rt) (asid := asid) (va := va) (sz := sz) (dev := dev) (isEpt := isEpt) (h0 := h0) (hcap := hcap) (k := k) (hkA := hkA) (hcov := Q.cov) (hext := Q.hext) (hSC := Q.hSC) (hpin := (frameUnmapX_pinA Q.F hlab hcap hkA hk2)))⟩ + +/-- `VsScopeA.frameUnmapZ` at the invocation site. -/ +theorem JSyscallA.frameUnmapZ_scopeA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (Q : SiteA st Z OS s σ info buf slot extra mi sl c excaps buffer argsU) + (hlab : info.label = fl4_abi.label.ARMPageUnmap) + {bB bD bF : Bool} + (call : Bool) + (fp rt va : U64) + (sz : capability.FrameSize) + (dev : Bool) + (hcap : capA st s slot = .Frame fp sz rt dev true false 0#u64 va) : + ∃ Out, DispScope4A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ m Clock hPl.ipa info.label + (msgLenA info buf) slot bB call bD bF buf extra Out := by + rw [hlab] + have hsl := Q.F.pin.hslot + subst hsl + exact ⟨_, Or.inl (VsScopeA.frameUnmapZ (bB := bB) (bD := bD) (bF := bF) (hnvm := Q.nvm) (length := (msgLenA info buf)) (buf := buf) (call := call) (ex := extra) (cte := sl) (hab := Q.F.pin.habs) (fp := fp) (rt := rt) (va := va) (sz := sz) (dev := dev) (hcap := hcap) (hcov := Q.cov) (hext := Q.hext) (hSC := Q.hSC) (hpin := (frameUnmapZ_pinA Q.F hlab hcap)))⟩ + +/-- `VsScopeA.frameUnmapS2Z` at the invocation site. -/ +theorem JSyscallA.frameUnmapS2Z_scopeA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (Q : SiteA st Z OS s σ info buf slot extra mi sl c excaps buffer argsU) + (hlab : info.label = fl4_abi.label.ARMPageUnmap) + {bB bD bF : Bool} + (call : Bool) + (fp rt va : U64) + (sz : capability.FrameSize) + (dev : Bool) + (hcap : capA st s slot = .Frame fp sz rt dev true true 0#u64 va) : + ∃ Out, DispScope4A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ m Clock hPl.ipa info.label + (msgLenA info buf) slot bB call bD bF buf extra Out := by + rw [hlab] + have hsl := Q.F.pin.hslot + subst hsl + exact ⟨_, Or.inl (VsScopeA.frameUnmapS2Z (bB := bB) (bD := bD) (bF := bF) (hnvm := Q.nvm) (length := (msgLenA info buf)) (buf := buf) (call := call) (ex := extra) (cte := sl) (hab := Q.F.pin.habs) (fp := fp) (rt := rt) (va := va) (sz := sz) (dev := dev) (hcap := hcap) (hcov := Q.cov) (hext := Q.hext) (hSC := Q.hSC) (hpin := (frameUnmapS2Z_pinA Q.F hlab hcap)))⟩ + +/-- `VsScopeA.frameUnmapU` at the invocation site. -/ +theorem JSyscallA.frameUnmapU_scopeA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (Q : SiteA st Z OS s σ info buf slot extra mi sl c excaps buffer argsU) + (hlab : info.label = fl4_abi.label.ARMPageUnmap) + {bB bD bF : Bool} + (call : Bool) + (fp rt asid va : U64) + (sz : capability.FrameSize) + (dev isEpt : Bool) + (hcap : capA st s slot = .Frame fp sz rt dev false isEpt asid va) : + ∃ Out, DispScope4A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ m Clock hPl.ipa info.label + (msgLenA info buf) slot bB call bD bF buf extra Out := by + rw [hlab] + have hsl := Q.F.pin.hslot + subst hsl + exact ⟨_, Or.inl (VsScopeA.frameUnmapU (bB := bB) (bD := bD) (bF := bF) (hnvm := Q.nvm) (length := (msgLenA info buf)) (buf := buf) (call := call) (ex := extra) (cte := sl) (hab := Q.F.pin.habs) (fp := fp) (rt := rt) (asid := asid) (va := va) (sz := sz) (dev := dev) (isEpt := isEpt) (hcap := hcap) (hcov := Q.cov) (hext := Q.hext) (hSC := Q.hSC) (hpin := (frameUnmapU_pinA Q.F hlab hcap)) (hcs := (frameUnmapU_hcsA Q.F J.j.1.st hcap)))⟩ + +/-- `VsScopeA.ptMapS1` at the invocation site. -/ +theorem JSyscallA.ptMapS1_scopeA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (Q : SiteA st Z OS s σ info buf slot extra mi sl c excaps buffer argsU) + (hlab : info.label = fl4_abi.label.ARMPageTableMap) + {s3 : S} + {a3 : A} + {ptr asid0 va0 : U64} + {bB bC bD bF : Bool} + (hcap : capAtA (L.mem s) slot = .PageTable ptr false asid0 va0) + (UP : UnmPtA Z.S1 Z.Y OS H.tcbs (L.mem s) σ ptr) + (hS2 : ptr ∉ S2PgE Z.X Z.T2) + (hY : ptr ∉ Z.Y) + (x0 : U64) + (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) + (hS1 : ∀ p l mp' asid va, capAtA (L.mem s) x0 ≠ .Ept p l mp' asid va) + (hHi : ∀ p mp' asid, capAtA (L.mem s) x0 ≠ .HighVSpace p mp' asid) + (hdec : vspace.decode_page_table st ar s a fl4_abi.label.ARMPageTableMap (msgLenA info buf) slot + (.PageTable ptr false asid0 va0) buf extra = ok (core.result.Result.Ok (), s3, a3)) : + ∃ Out, DispScope4A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ m Clock hPl.ipa info.label + (msgLenA info buf) slot bB bC bD bF buf extra Out := by + rw [hlab] + have hsl := Q.F.pin.hslot + subst hsl + exact ⟨_, Or.inl (VsScopeA.ptMapS1 (s3 := s3) (a3 := a3) (ptr := ptr) (asid0 := asid0) (va0 := va0) (bB := bB) (bC := bC) (bD := bD) (bF := bF) (hnvm := Q.nvm) (cte := sl) (hab := Q.F.pin.habs) (hcap := hcap) (UP := UP) (hS2 := hS2) (hY := hY) (slbl := (specLabelWord (archL .armPageTableMap))) (hl := (by rw [invType_specA]; rfl)) (length := (msgLenA info buf)) (buf := buf) (args := argsU) (hlen := Q.F.len) (hargs := Q.F.args) (extra := extra) (x0 := x0) (hx0 := hx0) (exs := excaps) (hex := (by rw [show x0 = exAtA extra 0 from okinj (hx0.symm.trans (exAtA_idx0 extra))]; exact Q.F.extra.rel1 Q.place)) (hS1 := hS1) (hHi := hHi) (hdec := hdec) (hcov := Q.cov) (hext := Q.hext) (hSC := Q.hSC) (hpin := (ptMapS1_pinA Q.F hlab ((capAtA_eq _ _).symm.trans hcap) hx0 hS1 hHi)))⟩ + +/-- `VsScopeA.ptUnmapS1` at the invocation site. -/ +theorem JSyscallA.ptUnmapS1_scopeA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (Q : SiteA st Z OS s σ info buf slot extra mi sl c excaps buffer argsU) + (hlab : info.label = fl4_abi.label.ARMPageTableUnmap) + {bB bC bD bF : Bool} + (K : ArchKeepsA ar AR) + (hTO : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s q) + (hEO : ExtTabOffA (L := L) Z m s) + (ptr asid va : U64) + (hpS : ptr ∈ Z.S1 ∧ 1 ≤ Z.Lv ptr) + (hcap : capA st s slot = .PageTable ptr true asid va) + (hf : cdt.is_final_capability st s slot = ok true) + (hS1 : Fl4SpecA64.asidRootKind σ (BitVec.ofNat 16 asid.val) = none ∨ + Fl4SpecA64.asidRootKind σ (BitVec.ofNat 16 asid.val) = some .stage1) + -- [J] the spec's mapped page-table cap at the slot is the canonical one (record edit #8b part C) + (hcan : c = absCapA (capA st s slot)) : + ∃ Out, DispScope4A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ m Clock hPl.ipa info.label + (msgLenA info buf) slot bB bC bD bF buf extra Out := by + rw [hlab] + have hsl := Q.F.pin.hslot + subst hsl + exact ⟨_, Or.inl (VsScopeA.ptUnmapS1 (bB := bB) (bC := bC) (bD := bD) (bF := bF) (K := K) (hnvm := Q.nvm) (hTO := hTO) (hEO := hEO) (length := (msgLenA info buf)) (buf := buf) (ex := extra) (cte := sl) (hab := Q.F.pin.habs) (ptr := ptr) (asid := asid) (va := va) (hpS := hpS) (hcap := hcap) (hf := hf) (hS1 := hS1) (hcov := Q.cov) (hext := Q.hext) (hSC := Q.hSC) (hpin := (ptUnmapS1_pinA Q.F J hlab hcap hcan hf hS1)))⟩ + +/-- `VsScopeA.ptUnmapX` at the invocation site. -/ +theorem JSyscallA.ptUnmapX_scopeA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (Q : SiteA st Z OS s σ info buf slot extra mi sl c excaps buffer argsU) + (hlab : info.label = fl4_abi.label.ARMPageTableUnmap) + {bB bC bD bF : Bool} + (K : ArchKeepsA ar AR) + (hTO : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s q) + (hEO : ExtTabOffA (L := L) Z m s) + (hIpa : S2IpaE ar a) + (ptr asid va : U64) + (hpS : ptr ∈ Z.S1 ∧ 1 ≤ Z.Lv ptr) + (hcap : capA st s slot = .PageTable ptr true asid va) + (hf : cdt.is_final_capability st s slot = ok true) + (k : Fl4SpecA64.ExtRootKind) + (hkR : Fl4SpecA64.asidRootKind σ (BitVec.ofNat 16 asid.val) = some k) + (hkR1 : k ≠ .stage1) + -- [J] as `ptUnmapS1` + (hcan : c = absCapA (capA st s slot)) : + ∃ Out, DispScope4A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ m Clock hPl.ipa info.label + (msgLenA info buf) slot bB bC bD bF buf extra Out := by + rw [hlab] + have hsl := Q.F.pin.hslot + subst hsl + exact ⟨_, Or.inl (VsScopeA.ptUnmapX (bB := bB) (bC := bC) (bD := bD) (bF := bF) (K := K) (hnvm := Q.nvm) (hTO := hTO) (hEO := hEO) (hIpa := hIpa) (length := (msgLenA info buf)) (buf := buf) (ex := extra) (cte := sl) (hab := Q.F.pin.habs) (ptr := ptr) (asid := asid) (va := va) (hpS := hpS) (hcap := hcap) (hf := hf) (k := k) (hkR := hkR) (hkR1 := hkR1) (hcov := Q.cov) (hext := Q.hext) (hSC := Q.hSC) (hpin := (ptUnmapX_pinA Q.F J hlab hcap hcan hf hkR hkR1)))⟩ + +/-- `VsScopeA.ptUnmapU` at the invocation site. -/ +theorem JSyscallA.ptUnmapU_scopeA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (Q : SiteA st Z OS s σ info buf slot extra mi sl c excaps buffer argsU) + (hlab : info.label = fl4_abi.label.ARMPageTableUnmap) + {bB bC bD bF : Bool} + (ptr asid va : U64) + (hcap : capA st s slot = .PageTable ptr false asid va) + (hf : cdt.is_final_capability st s slot = ok true) : + ∃ Out, DispScope4A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ m Clock hPl.ipa info.label + (msgLenA info buf) slot bB bC bD bF buf extra Out := by + rw [hlab] + have hsl := Q.F.pin.hslot + subst hsl + exact ⟨_, Or.inl (VsScopeA.ptUnmapU (bB := bB) (bC := bC) (bD := bD) (bF := bF) (hnvm := Q.nvm) (length := (msgLenA info buf)) (buf := buf) (ex := extra) (cte := sl) (hab := Q.F.pin.habs) (ptr := ptr) (asid := asid) (va := va) (hcap := hcap) (hf := hf) (hcov := Q.cov) (hext := Q.hext) (hSC := Q.hSC) (hpin := (ptUnmapU_pinA Q.F J hlab hcap hf)))⟩ + +/-- `VsScopeA.asidAssign` at the invocation site. -/ +theorem JSyscallA.asidAssign_scopeA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (Q : SiteA st Z OS s σ info buf slot extra mi sl c excaps buffer argsU) + (hlab : info.label = fl4_abi.label.ARMASIDPoolAssign) + (p base : U64) + (hpool : capA st s slot = .AsidPool p base) + (block call cd fp : Bool) + (dst : Fl4SpecA64.CslotPtr) + (hd0 : AbsSlotA Z.th dst) + (hex : vspace.extra extra 0#usize = ok (some (cslotAddrA dst))) + (k : Fl4SpecA64.ExtRootKind) + (root oldAsid oldVa : U64) + (hcap : capA st s (cslotAddrA dst) = assignUnmappedA k root oldAsid oldVa) + (htab : ∀ hi : U64, hi.val = base.val / 512 → L.mem s (atAF (L.ksb s) hi) = p) + (hp0 : p ≠ 0#u64) + (i : U64) + (hsearch : vspace.decode_loop1 st s base p 0#u64 = ok i) + (hfull : i ≠ 512#u64) : + ∃ Out, DispScope4A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ m Clock hPl.ipa info.label + (msgLenA info buf) slot block call cd fp buf extra Out := by + rw [hlab] + have hsl := Q.F.pin.hslot + subst hsl + exact ⟨_, Or.inl (VsScopeA.asidAssign (hnvm := Q.nvm) (cte := sl) (hab := Q.F.pin.habs) (p := p) (base := base) (hpool := hpool) (length := (msgLenA info buf)) (buf := buf) (block := block) (call := call) (cd := cd) (fp := fp) (ex := extra) (dst := dst) (hd0 := hd0) (hex := hex) (k := k) (root := root) (oldAsid := oldAsid) (oldVa := oldVa) (hcap := hcap) (htab := htab) (hp0 := hp0) (i := i) (hsearch := hsearch) (hfull := hfull) (hcov := Q.cov) (hext := Q.hext) (hSC := Q.hSC) (hpin := (asidAssign_pinA (block := block) (cd := cd) (fp := fp) hA hM hPl J hfm Q.F hlab Q.nvm hpool hd0 hex hcap htab hp0 hsearch hfull Q.cov Q.hext Q.hSC)))⟩ + +/-- `VsScopeA.frameMapHi` at the invocation site. -/ +theorem JSyscallA.frameMapHi_scopeA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (Q : SiteA st Z OS s σ info buf slot extra mi sl c excaps buffer argsU) + (hlab : info.label = fl4_abi.label.ARMPageMap) + {x0 : U64} + {D : Set (Except SyscallError Fl4ArchInvocation × State Unit) × Prop} + {dc : capability.Cap} + {s3 : S} + {a3 : A} + {callD : Bool} + {rr : capability.Rights} + {bB bD bF : Bool} + (K : ArchKeepsA ar AR) + (hTO : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s q) + (hEO : ExtTabOffA (L := L) Z m s) + (fp rt asid va : U64) + (sz : capability.FrameSize) + (dev mp : Bool) + (hcap : capAtA (L.mem s) slot = .Frame fp sz rt dev mp false asid va) + (FP : FrPlA sz fp mp asid) + (p1 asid1 : U64) + (mp1 : Bool) + (h16 : asid1.val < 2 ^ 16) + (hx0 : x0 ∈ OS) + (hx0c : capA st s x0 = .HighVSpace p1 mp1 asid1) + (vaddr rightsMask vm attr : U64) + (hvmEq : vmRightsOfA vm = maskVmRights (vmRightsOfA rt) (dataToRights rightsMask.bv)) + (hD : D = (NonDetE.liftME Fl4ArchInvocation.extVSpace + (decodeHighPageMap (Z := Unit) sl (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev + (mapDataA mp asid va)) (extRootCapA .high (specPtr p1) (asidDataA mp1 asid1)) + vaddr.bv rightsMask.bv attr.bv)).run σ) + (d : DecPre st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits U Ac s dc extra) + (hres : vspace.decode_high_page_map st ar s a slot + { ptr := fp, size := sz, rights := rt, is_device := dev, mapped := mp, is_ept := false, asid := asid, vaddr := va } + (.HighVSpace p1 mp1 asid1) vaddr vm attr = ok (core.result.Result.Ok (), s3, a3)) + (hx : vspace.extra extra 0#usize = ok (some x0)) + (hlen : (msgLenA info buf) ≥ 3#u64) + (h0 : msg.syscall_arg st s (L.curW s) buf 0#u64 = ok vaddr) + (h1 : msg.syscall_arg st s (L.curW s) buf 1#u64 = ok rightsMask) + (h2 : msg.syscall_arg st s (L.curW s) buf 2#u64 = ok attr) + (hr : capability.Rights.from_word rightsMask = ok rr) + (hvm : capability.mask_vm_rights rt rr = ok vm) : + ∃ Out, DispScope4A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ m Clock hPl.ipa info.label + (msgLenA info buf) slot bB callD bD bF buf extra Out := by + rw [hlab] + have hsl := Q.F.pin.hslot + subst hsl + exact ⟨_, Or.inl (VsScopeA.frameMapHi (x0 := x0) (D := D) (dc := dc) (ex := extra) (s3 := s3) (a3 := a3) (callD := callD) (lenD := (msgLenA info buf)) (bufD := buf) (rr := rr) (bB := bB) (bD := bD) (bF := bF) (K := K) (hnvm := Q.nvm) (hTO := hTO) (hEO := hEO) (cte := sl) (hab := Q.F.pin.habs) (fp := fp) (rt := rt) (asid := asid) (va := va) (sz := sz) (dev := dev) (mp := mp) (hcap := hcap) (FP := FP) (p1 := p1) (asid1 := asid1) (mp1 := mp1) (h16 := h16) (hx0 := hx0) (hx0c := hx0c) (vaddr := vaddr) (rightsMask := rightsMask) (vm := vm) (attr := attr) (hvmEq := hvmEq) (hD := hD) (d := d) (hres := hres) (hcov := Q.cov) (hext := Q.hext) (hSC := Q.hSC) (hx := hx) (hlen := hlen) (h0 := h0) (h1 := h1) (h2 := h2) (hr := hr) (hvm := hvm) (hpin := (frameMapHi_pinA Q.F hlab ((capAtA_eq _ _).symm.trans hcap) hx hx0c hlen h0 h1 h2 hD)))⟩ + +/-- `CnScopeA.cnodeCopy` at the invocation site. -/ +theorem JSyscallA.cnodeCopy_scopeA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (Q : SiteA st Z OS s σ info buf slot extra mi sl c excaps buffer argsU) + {ps pd : U64} + {nc : capability.Cap} + {p r gs g : U64} + {lbl index depth x0 : U64} + {si sdp : U64} + {root : capability.Cap} + {bB bC bD : Bool} + (ss sd : Fl4SpecA64.CslotPtr) + (hCs : ss.1 = specPtr ps ∧ Z.th ps = some (.cnode ss.2.length)) + (hCd : sd.1 = specPtr pd ∧ Z.th pd = some (.cnode sd.2.length)) + (hsn : capA st s (cslotAddrA ss) ≠ .Null) + (hdn : capA st s (cslotAddrA sd) = .Null) + (hdc : obj.sext48 (cslotAddrA sd) = ok (cslotAddrA sd)) + (hw : CapRt.EncWf nc) + (hn : nc ≠ .Null) + (hD : Fl4ProofsA64.P3.DerivA nc (capA st s (cslotAddrA ss))) + (hal : UA nc (capA st s (cslotAddrA ss))) + (hk : SameBadgeKindA nc (capA st s (cslotAddrA ss))) + (hsh : Fl4ProofsA64.P3.shapeA nc = Fl4ProofsA64.P3.shapeA (capA st s (cslotAddrA ss))) + (hsh1 : Fl4ProofsA64.P1.W7.R.ST.shapeA nc = Fl4ProofsA64.P1.W7.R.ST.shapeA (capA st s (cslotAddrA ss))) + (hbd1 : badgeA nc = badgeA (capA st s (cslotAddrA ss)) ∨ badgeA (capA st s (cslotAddrA ss)) = 0#u64) + (hbd : ∀ r b rs, (absCapA (capA st s (cslotAddrA ss)) = .endpointCap r b rs ∨ + absCapA (capA st s (cslotAddrA ss)) = .notificationCap r b rs) → b ≠ 0 → + Fl4SpecA64.capEpBadge (absCapA nc) = b) + (hirq : ∀ i, nc = .IrqHandler i ↔ capA st s (cslotAddrA ss) = .IrqHandler i) + (hz : ∀ p t k, capA st s (cslotAddrA ss) ≠ .Zombie p t k) + (hut : isUtA (capA st s (cslotAddrA ss)) = true → ∀ y, ¬ Fl4Proofs.P3.Mdb.Desc Z.G.par y (cslotAddrA ss)) + (hdo : ¬ IrqNodeA (L.ksb s) (fl4Ptr sd.1) ∨ IrqCapA (absCapA nc)) + (hcv : CapValidA Z.th (capA st s (cslotAddrA ss)) → CapValidA Z.th nc) + (hslc : capA st s slot = .CNode p r gs g) + (hlb : lbl = fl4_abi.label.CNodeCopy ∨ lbl = fl4_abi.label.CNodeMint) + (hl4 : ¬ (msgLenA info buf) < 4#u64) + (hl2 : ¬ (msgLenA info buf) < 2#u64) + (hi : msg.syscall_arg st s (L.curW s) buf 0#u64 = ok index) + (hd0 : msg.syscall_arg st s (L.curW s) buf 1#u64 = ok depth) + (hres : cspace.lookup_target_slot st s (.CNode p r gs g) index depth = ok (.Ok (cslotAddrA sd))) + (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) + (h0 : x0 ≠ 0#u64) + (h2 : msg.syscall_arg st s (L.curW s) buf 2#u64 = ok si) + (h3 : msg.syscall_arg st s (L.curW s) buf 3#u64 = ok sdp) + (hroot : obj.Slot.cap st x0 s = ok root) + (hemp : cdt.ensure_empty_slot st s (cslotAddrA sd) = ok (.Ok ())) + (hsrc : cspace.lookup_source_slot st s root si sdp = ok (.Ok (cslotAddrA ss))) + (hcop : cnode.copied_cap st s lbl (msgLenA info buf) buf (cslotAddrA ss) (capA st s (cslotAddrA ss)) = ok (.Ok (nc, false))) + -- [J] the arm's facts about the spec representative of the derived cap (the pin returns it) + (hsci : ∀ anc, CapRelA nc anc → NoHiPtV anc → NoIdleCapV m n anc) + (hlab : info.label = lbl) : + ∃ Out, DispScope4A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ m Clock hPl.ipa info.label + (msgLenA info buf) slot bB bC bD false buf extra Out := by + obtain ⟨anc, hrel, hnh, hpin⟩ := cnCopy_pinA (mv := false) (call := bC) J Q.F hlab + (by have := lblA_copy; have := lblA_mint; have := lblA_move; have := lblA_mutate; rcases hlb with h | h <;> subst h <;> refine ⟨?_, ?_⟩ <;> scalar_tac) ⟨_, hCs.1, Or.inl hCs.2⟩ ⟨_, hCd.1, Or.inl hCd.2⟩ + hsn hdn hn hslc hl4 hi hd0 hres hx0 h0 h2 h3 hroot hsrc hcop + rw [hlab] + exact ⟨_, Or.inr (Or.inl (CnScopeA.cnodeCopy (ps := ps) (pd := pd) (nc := nc) (anc := anc) (slot := slot) (p := p) (r := r) (gs := gs) (g := g) (extra := extra) (length := (msgLenA info buf)) (buf := buf) (lbl := lbl) (index := index) (depth := depth) (x0 := x0) (si := si) (sdp := sdp) (root := root) (bB := bB) (bC := bC) (bD := bD) (hnvm := Q.nvm) (ss := ss) (sd := sd) (hCs := hCs) (hCd := hCd) (hsn := hsn) (hdn := hdn) (hdc := hdc) (hwf := hrel.1) (hanc := hrel.2) (hw := hw) (hn := hn) (hD := hD) (hal := hal) (hk := hk) (hsh := hsh) (hsh1 := hsh1) (hbd1 := hbd1) (hbd := hbd) (hirq := hirq) (hz := hz) (hut := hut) (hdo := hdo) (hcv := hcv) (hsci := (hsci anc hrel hnh)) (hslot := Q.hOS) (hslc := hslc) (hlb := hlb) (hl4 := hl4) (hl2 := hl2) (hi := hi) (hd0 := hd0) (hres := hres) (hx0 := hx0) (h0 := h0) (h2 := h2) (h3 := h3) (hroot := hroot) (hemp := hemp) (hsrc := hsrc) (hcop := hcop) (hcov := Q.cov) (hext := Q.hext) (hSC := Q.hSC) (hpin := hpin)))⟩ + +/-- `CnScopeA.cnodeMove` at the invocation site. -/ +theorem JSyscallA.cnodeMove_scopeA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (Q : SiteA st Z OS s σ info buf slot extra mi sl c excaps buffer argsU) + {ps pd : U64} + {nc : capability.Cap} + {p r gs g : U64} + {lbl index depth x0 : U64} + {si sdp : U64} + {root : capability.Cap} + {bB bC bD : Bool} + (ss sd : Fl4SpecA64.CslotPtr) + (hCs : ss.1 = specPtr ps ∧ Z.th ps = some (.cnode ss.2.length)) + (hCd : sd.1 = specPtr pd ∧ Z.th pd = some (.cnode sd.2.length)) + (hsn : capA st s (cslotAddrA ss) ≠ .Null) + (hdn : capA st s (cslotAddrA sd) = .Null) + (hdc : obj.sext48 (cslotAddrA sd) = ok (cslotAddrA sd)) + (hw : CapRt.EncWf nc) + (hn : nc ≠ .Null) + (hD : DerivA nc (capA st s (cslotAddrA ss))) + (hal : UA nc (capA st s (cslotAddrA ss))) + (hsh : Fl4ProofsA64.P1.W7.R.ST.shapeA nc = Fl4ProofsA64.P1.W7.R.ST.shapeA (capA st s (cslotAddrA ss))) + (hbd : badgeA nc = badgeA (capA st s (cslotAddrA ss))) + (hirq : ∀ i, nc = .IrqHandler i ↔ capA st s (cslotAddrA ss) = .IrqHandler i) + (hno : CapObjOkA Z nc) + (hslc : capA st s slot = .CNode p r gs g) + (hlb : lbl = fl4_abi.label.CNodeMove ∨ lbl = fl4_abi.label.CNodeMutate) + (hl4 : ¬ (msgLenA info buf) < 4#u64) + (hl2 : ¬ (msgLenA info buf) < 2#u64) + (hi : msg.syscall_arg st s (L.curW s) buf 0#u64 = ok index) + (hd0 : msg.syscall_arg st s (L.curW s) buf 1#u64 = ok depth) + (hres : cspace.lookup_target_slot st s (.CNode p r gs g) index depth = ok (.Ok (cslotAddrA sd))) + (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) + (h0 : x0 ≠ 0#u64) + (h2 : msg.syscall_arg st s (L.curW s) buf 2#u64 = ok si) + (h3 : msg.syscall_arg st s (L.curW s) buf 3#u64 = ok sdp) + (hroot : obj.Slot.cap st x0 s = ok root) + (hemp : cdt.ensure_empty_slot st s (cslotAddrA sd) = ok (.Ok ())) + (hsrc : cspace.lookup_source_slot st s root si sdp = ok (.Ok (cslotAddrA ss))) + (hcop : cnode.copied_cap st s lbl (msgLenA info buf) buf (cslotAddrA ss) (capA st s (cslotAddrA ss)) = ok (.Ok (nc, true))) + -- [J] the arm's facts about the spec representative of the derived cap (the pin returns it) + (hok : ∀ ac, CapRelA nc ac → CapOkA σ.interruptIrqNode sd ac) + (hsci : ∀ ac, CapRelA nc ac → NoHiPtV ac → NoIdleCapV m n ac) + (hlab : info.label = lbl) : + ∃ Out, DispScope4A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ m Clock hPl.ipa info.label + (msgLenA info buf) slot bB bC bD false buf extra Out := by + obtain ⟨ac, hrel, hnh, hpin⟩ := cnCopy_pinA (mv := true) (call := bC) J Q.F hlab + (by have := lblA_copy; have := lblA_mint; have := lblA_move; have := lblA_mutate; rcases hlb with h | h <;> subst h <;> refine ⟨?_, ?_⟩ <;> scalar_tac) ⟨_, hCs.1, Or.inl hCs.2⟩ ⟨_, hCd.1, Or.inl hCd.2⟩ + hsn hdn hn hslc hl4 hi hd0 hres hx0 h0 h2 h3 hroot hsrc hcop + rw [hlab] + exact ⟨_, Or.inr (Or.inl (CnScopeA.cnodeMove (ps := ps) (pd := pd) (nc := nc) (ac := ac) (slot := slot) (p := p) (r := r) (gs := gs) (g := g) (extra := extra) (length := (msgLenA info buf)) (buf := buf) (lbl := lbl) (index := index) (depth := depth) (x0 := x0) (si := si) (sdp := sdp) (root := root) (bB := bB) (bC := bC) (bD := bD) (hnvm := Q.nvm) (ss := ss) (sd := sd) (hCs := hCs) (hCd := hCd) (hsn := hsn) (hdn := hdn) (hdc := hdc) (hca := hrel) (hw := hw) (hn := hn) (hD := hD) (hal := hal) (hsh := hsh) (hbd := hbd) (hirq := hirq) (hno := hno) (hok := (hok ac hrel)) (hsci := (hsci ac hrel hnh)) (hslot := Q.hOS) (hslc := hslc) (hlb := hlb) (hl4 := hl4) (hl2 := hl2) (hi := hi) (hd0 := hd0) (hres := hres) (hx0 := hx0) (h0 := h0) (h2 := h2) (h3 := h3) (hroot := hroot) (hemp := hemp) (hsrc := hsrc) (hcop := hcop) (hcov := Q.cov) (hext := Q.hext) (hSC := Q.hSC) (hpin := hpin)))⟩ + +/-- `CnScopeA.cnodeRotate` at the invocation site. -/ +theorem JSyscallA.cnodeRotate_scopeA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (Q : SiteA st Z OS s σ info buf slot extra mi sl c excaps buffer argsU) + {ps pp pd : U64} + {np : capability.Cap} + {ns : capability.Cap} + {p r gs g : U64} + {lbl index depth x0 : U64} + {x1' : U64} + {pdata sdata : U64} + {bB bC bD : Bool} + (ss sp sd : Fl4SpecA64.CslotPtr) + (hCs : ss.1 = specPtr ps ∧ Z.th ps = some (.cnode ss.2.length)) + (hCp : sp.1 = specPtr pp ∧ Z.th pp = some (.cnode sp.2.length)) + (hCd : sd.1 = specPtr pd ∧ Z.th pd = some (.cnode sd.2.length)) + (hsp : cslotAddrA ss ≠ cslotAddrA sp) + (hsd : cslotAddrA ss ≠ cslotAddrA sd) + (hsn : capA st s (cslotAddrA ss) ≠ .Null) + (hpn : capA st s (cslotAddrA sp) ≠ .Null) + (hdn : capA st s (cslotAddrA sd) = .Null) + (hpc : obj.sext48 (cslotAddrA sp) = ok (cslotAddrA sp)) + (hdc : obj.sext48 (cslotAddrA sd) = ok (cslotAddrA sd)) + (hwp : CapRt.EncWf np) + (hnp : np ≠ .Null) + (hDp : Fl4ProofsA64.P3.DerivA np (capA st s (cslotAddrA sp))) + (halp : UA np (capA st s (cslotAddrA sp))) + (hshp : Fl4ProofsA64.P1.W7.R.ST.shapeA np = Fl4ProofsA64.P1.W7.R.ST.shapeA (capA st s (cslotAddrA sp))) + (hbdp : badgeA np = badgeA (capA st s (cslotAddrA sp))) + (hirqp : ∀ i, np = .IrqHandler i ↔ capA st s (cslotAddrA sp) = .IrqHandler i) + (hop : CapObjOkA Z np) + (hws : CapRt.EncWf ns) + (hns : ns ≠ .Null) + (hDs : Fl4ProofsA64.P3.DerivA ns (capA st s (cslotAddrA ss))) + (hals : UA ns (capA st s (cslotAddrA ss))) + (hshs : Fl4ProofsA64.P1.W7.R.ST.shapeA ns = Fl4ProofsA64.P1.W7.R.ST.shapeA (capA st s (cslotAddrA ss))) + (hbds : badgeA ns = badgeA (capA st s (cslotAddrA ss))) + (hirqs : ∀ i, ns = .IrqHandler i ↔ capA st s (cslotAddrA ss) = .IrqHandler i) + (hos : CapObjOkA Z ns) + (hslc : capA st s slot = .CNode p r gs g) + (hlb : lbl = fl4_abi.label.CNodeRotate) + (hl8 : ¬ (msgLenA info buf) < 8#u64) + (hl2 : ¬ (msgLenA info buf) < 2#u64) + (hi : msg.syscall_arg st s (L.curW s) buf 0#u64 = ok index) + (hd0 : msg.syscall_arg st s (L.curW s) buf 1#u64 = ok depth) + (hres : cspace.lookup_target_slot st s (.CNode p r gs g) index depth = ok (.Ok (cslotAddrA sd))) + (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) + (h0 : x0 ≠ 0#u64) + (hx1 : Aeneas.Std.Array.index_usize extra 1#usize = ok x1') + (h1 : x1' ≠ 0#u64) + (h2 : msg.syscall_arg st s (L.curW s) buf 2#u64 = ok pdata) + (h5 : msg.syscall_arg st s (L.curW s) buf 5#u64 = ok sdata) + (hrot : cnode.rotate_slots st s buf extra (cslotAddrA sd) = ok (.Ok (cslotAddrA ss, cslotAddrA sp))) + (hus : objects.update_cap_data true sdata (capA st s (cslotAddrA ss)) = ok ns) + (hup : objects.update_cap_data true pdata (capA st s (cslotAddrA sp)) = ok np) + -- [J] the arm's facts about the spec representatives of the rotated caps (the pin returns them) + (hokp : ∀ ap, CapRelA np ap → CapOkA σ.interruptIrqNode sd ap) + (hscp : ∀ ap, CapRelA np ap → NoHiPtV ap → NoIdleCapV m n ap) + (hoks : ∀ an, CapRelA ns an → CapOkA σ.interruptIrqNode sp an) + (hscs : ∀ an, CapRelA ns an → NoHiPtV an → NoIdleCapV m n an) + (hlab : info.label = lbl) : + ∃ Out, DispScope4A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ m Clock hPl.ipa info.label + (msgLenA info buf) slot bB bC bD false buf extra Out := by + obtain ⟨an, ap, hcs, hcp, hnhs, hnhp, hpin⟩ := cnRotate_pinA (call := bC) (ss := ss) (sp := sp) (dsl := sd) J Q.F (hlab.trans hlb) + ⟨_, hCs.1, Or.inl hCs.2⟩ ⟨_, hCp.1, Or.inl hCp.2⟩ ⟨_, hCd.1, Or.inl hCd.2⟩ ⟨_, hCd⟩ hns hnp hslc hl8 hi hd0 hres hx0 h0 + hx1 h1 h2 h5 hrot hus hup + rw [hlab] + exact ⟨_, Or.inr (Or.inl (CnScopeA.cnodeRotate (ps := ps) (pp := pp) (pd := pd) (np := np) (ap := ap) (ns := ns) (an := an) (slot := slot) (p := p) (r := r) (gs := gs) (g := g) (extra := extra) (length := (msgLenA info buf)) (buf := buf) (lbl := lbl) (index := index) (depth := depth) (x0 := x0) (x1' := x1') (pdata := pdata) (sdata := sdata) (bB := bB) (bC := bC) (bD := bD) (hnvm := Q.nvm) (ss := ss) (sp := sp) (sd := sd) (hCs := hCs) (hCp := hCp) (hCd := hCd) (hsp := hsp) (hsd := hsd) (hsn := hsn) (hpn := hpn) (hdn := hdn) (hpc := hpc) (hdc := hdc) (hcp := hcp) (hwp := hwp) (hnp := hnp) (hDp := hDp) (halp := halp) (hshp := hshp) (hbdp := hbdp) (hirqp := hirqp) (hop := hop) (hokp := (hokp ap hcp)) (hscp := (hscp ap hcp hnhp)) (hcs := hcs) (hws := hws) (hns := hns) (hDs := hDs) (hals := hals) (hshs := hshs) (hbds := hbds) (hirqs := hirqs) (hos := hos) (hoks := (hoks an hcs)) (hscs := (hscs an hcs hnhs)) (hslot := Q.hOS) (hslc := hslc) (hlb := hlb) (hl8 := hl8) (hl2 := hl2) (hi := hi) (hd0 := hd0) (hres := hres) (hx0 := hx0) (h0 := h0) (hx1 := hx1) (h1 := h1) (h2 := h2) (h5 := h5) (hrot := hrot) (hus := hus) (hup := hup) (hcov := Q.cov) (hext := Q.hext) (hSC := Q.hSC) (hpin := hpin)))⟩ + +/-- `CnScopeA.cnodeRotateSw` at the invocation site. -/ +theorem JSyscallA.cnodeRotateSw_scopeA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (Q : SiteA st Z OS s σ info buf slot extra mi sl c excaps buffer argsU) + {ps pp : U64} + {np : capability.Cap} + {ns : capability.Cap} + {p r gs g : U64} + {lbl index depth x0 : U64} + {x1' : U64} + {pdata sdata : U64} + {bB bC bD : Bool} + (ss sp : Fl4SpecA64.CslotPtr) + (hCs : ss.1 = specPtr ps ∧ Z.th ps = some (.cnode ss.2.length)) + (hCp : sp.1 = specPtr pp ∧ Z.th pp = some (.cnode sp.2.length)) + (hsp : cslotAddrA ss ≠ cslotAddrA sp) + (hsn : capA st s (cslotAddrA ss) ≠ .Null) + (hpn : capA st s (cslotAddrA sp) ≠ .Null) + (hsx : obj.sext48 (cslotAddrA ss) = ok (cslotAddrA ss)) + (hpx : obj.sext48 (cslotAddrA sp) = ok (cslotAddrA sp)) + (hwp : CapRt.EncWf np) + (hnp : np ≠ .Null) + (hDp : Fl4ProofsA64.P3.DerivA np (capA st s (cslotAddrA sp))) + (halp : UA np (capA st s (cslotAddrA sp))) + (hshp : Fl4ProofsA64.P1.W7.R.ST.shapeA np = Fl4ProofsA64.P1.W7.R.ST.shapeA (capA st s (cslotAddrA sp))) + (hbdp : badgeA np = badgeA (capA st s (cslotAddrA sp))) + (hirqp : ∀ i, np = .IrqHandler i ↔ capA st s (cslotAddrA sp) = .IrqHandler i) + (hop : CapObjOkA Z np) + (hws : CapRt.EncWf ns) + (hns : ns ≠ .Null) + (hDs : Fl4ProofsA64.P3.DerivA ns (capA st s (cslotAddrA ss))) + (hals : UA ns (capA st s (cslotAddrA ss))) + (hshs : Fl4ProofsA64.P1.W7.R.ST.shapeA ns = Fl4ProofsA64.P1.W7.R.ST.shapeA (capA st s (cslotAddrA ss))) + (hbds : badgeA ns = badgeA (capA st s (cslotAddrA ss))) + (hirqs : ∀ i, ns = .IrqHandler i ↔ capA st s (cslotAddrA ss) = .IrqHandler i) + (hos : CapObjOkA Z ns) + (hslc : capA st s slot = .CNode p r gs g) + (hlb : lbl = fl4_abi.label.CNodeRotate) + (hl8 : ¬ (msgLenA info buf) < 8#u64) + (hl2 : ¬ (msgLenA info buf) < 2#u64) + (hi : msg.syscall_arg st s (L.curW s) buf 0#u64 = ok index) + (hd0 : msg.syscall_arg st s (L.curW s) buf 1#u64 = ok depth) + (hres : cspace.lookup_target_slot st s (.CNode p r gs g) index depth = ok (.Ok (cslotAddrA ss))) + (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) + (h0 : x0 ≠ 0#u64) + (hx1 : Aeneas.Std.Array.index_usize extra 1#usize = ok x1') + (h1 : x1' ≠ 0#u64) + (h2 : msg.syscall_arg st s (L.curW s) buf 2#u64 = ok pdata) + (h5 : msg.syscall_arg st s (L.curW s) buf 5#u64 = ok sdata) + (hrot : cnode.rotate_slots st s buf extra (cslotAddrA ss) = ok (.Ok (cslotAddrA ss, cslotAddrA sp))) + (hus : objects.update_cap_data true sdata (capA st s (cslotAddrA ss)) = ok ns) + (hup : objects.update_cap_data true pdata (capA st s (cslotAddrA sp)) = ok np) + -- [J] the arm's facts about the spec representatives of the rotated caps (the pin returns them) + (hokp : ∀ ap, CapRelA np ap → CapOkA σ.interruptIrqNode ss ap) + (hscp : ∀ ap, CapRelA np ap → NoHiPtV ap → NoIdleCapV m n ap) + (hoks : ∀ an, CapRelA ns an → CapOkA σ.interruptIrqNode sp an) + (hscs : ∀ an, CapRelA ns an → NoHiPtV an → NoIdleCapV m n an) + (hlab : info.label = lbl) : + ∃ Out, DispScope4A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ m Clock hPl.ipa info.label + (msgLenA info buf) slot bB bC bD false buf extra Out := by + obtain ⟨an, ap, hcs, hcp, hnhs, hnhp, hpin⟩ := cnRotate_pinA (call := bC) (ss := ss) (sp := sp) (dsl := ss) J Q.F (hlab.trans hlb) + ⟨_, hCs.1, Or.inl hCs.2⟩ ⟨_, hCp.1, Or.inl hCp.2⟩ ⟨_, hCs.1, Or.inl hCs.2⟩ ⟨_, hCs⟩ hns hnp hslc hl8 hi hd0 hres hx0 h0 + hx1 h1 h2 h5 hrot hus hup + rw [hlab] + exact ⟨_, Or.inr (Or.inl (CnScopeA.cnodeRotateSw (ps := ps) (pp := pp) (np := np) (ap := ap) (ns := ns) (an := an) (slot := slot) (p := p) (r := r) (gs := gs) (g := g) (extra := extra) (length := (msgLenA info buf)) (buf := buf) (lbl := lbl) (index := index) (depth := depth) (x0 := x0) (x1' := x1') (pdata := pdata) (sdata := sdata) (bB := bB) (bC := bC) (bD := bD) (hnvm := Q.nvm) (ss := ss) (sp := sp) (hCs := hCs) (hCp := hCp) (hsp := hsp) (hsn := hsn) (hpn := hpn) (hsx := hsx) (hpx := hpx) (hcp := hcp) (hwp := hwp) (hnp := hnp) (hDp := hDp) (halp := halp) (hshp := hshp) (hbdp := hbdp) (hirqp := hirqp) (hop := hop) (hokp := (hokp ap hcp)) (hscp := (hscp ap hcp hnhp)) (hcs := hcs) (hws := hws) (hns := hns) (hDs := hDs) (hals := hals) (hshs := hshs) (hbds := hbds) (hirqs := hirqs) (hos := hos) (hoks := (hoks an hcs)) (hscs := (hscs an hcs hnhs)) (hslot := Q.hOS) (hslc := hslc) (hlb := hlb) (hl8 := hl8) (hl2 := hl2) (hi := hi) (hd0 := hd0) (hres := hres) (hx0 := hx0) (h0 := h0) (hx1 := hx1) (h1 := h1) (h2 := h2) (h5 := h5) (hrot := hrot) (hus := hus) (hup := hup) (hcov := Q.cov) (hext := Q.hext) (hSC := Q.hSC) (hpin := hpin)))⟩ + +/-- `IpcScopeA.signalQuiet` at the invocation site. -/ +theorem JSyscallA.signalQuiet_scopeA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (Q : SiteA st Z OS s σ info buf slot extra mi sl c excaps buffer argsU) + {p badge lbl : U64} + {recv blk call don fp : Bool} + {x : Notification} + (hc : m.curCore < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (hcap : capA st s slot = .Notification p badge true recv) + (hp : p ∈ N) + (hk : σ.kheap (specPtr p) = some (.notification x)) + (hq : QuietSigA σ x) + (hlab : info.label = lbl) : + ∃ Out, DispScope4A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ m Clock hPl.ipa info.label + (msgLenA info buf) slot blk call don fp buf extra Out := by + rw [hlab] + exact ⟨_, Or.inr (Or.inr (Or.inr (Or.inl (IpcScopeA.signalQuiet (slot := slot) (len := (msgLenA info buf)) (buf := buf) (p := p) (badge := badge) (lbl := lbl) (recv := recv) (blk := blk) (call := call) (don := don) (fp := fp) (extra := extra) (x := x) (hc := hc) (hmb := hmb) (hmax := hmax) (htu := htu) (hslot := Q.hOS) (hcov := Q.cov) (hcap := hcap) (hext := Q.hext) (hSC := Q.hSC) (hp := hp) (hk := hk) (hq := hq)))))⟩ + +/-- `IpcScopeA.cancelBadged` at the invocation site. -/ +theorem JSyscallA.cancelBadged_scopeA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (Q : SiteA st Z OS s σ info buf slot extra mi sl c excaps buffer argsU) + {lbl index depth dest p b c0 cr cgs cg : U64} + {blk call don : Bool} + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (hcap : capA st s slot = .CNode c0 cr cgs cg) + (hlbl : lbl = fl4_abi.label.CNodeCancelBadgedSends) + (hl2 : ¬ (msgLenA info buf) < 2#u64) + (hi : msg.syscall_arg st s (L.curW s) buf 0#u64 = ok index) + (hd : msg.syscall_arg st s (L.curW s) buf 1#u64 = ok depth) + (hres : cspace.lookup_target_slot st s (.CNode c0 cr cgs cg) index depth = ok (.Ok dest)) + (hdc : capA st s dest = .Endpoint p b true true true true) + (hdOS : dest ∈ OS) + (hlab : info.label = lbl) : + ∃ Out, DispScope4A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ m Clock hPl.ipa info.label + (msgLenA info buf) slot blk call don false buf extra Out := by + rw [hlab] + exact ⟨_, Or.inr (Or.inr (Or.inr (Or.inl (IpcScopeA.cancelBadged (slot := slot) (len := (msgLenA info buf)) (buf := buf) (lbl := lbl) (index := index) (depth := depth) (dest := dest) (p := p) (b := b) (c0 := c0) (cr := cr) (cgs := cgs) (cg := cg) (blk := blk) (call := call) (don := don) (extra := extra) (hCs := hCs) (h1 := h1) (hmb := hmb) (hmax := hmax) (htu := htu) (hslot := Q.hOS) (hcov := Q.cov) (hcap := hcap) (hext := Q.hext) (hSC := Q.hSC) (hlbl := hlbl) (hl2 := hl2) (hi := hi) (hd := hd) (hres := hres) (hdc := hdc) (hdOS := hdOS)))))⟩ + +/-! ## The IPC send, Reply and Notification arms (A64 dispatch restore step 5) -/ + +omit hA hM hPl hfm in +/-- **The Endpoint a slot cap names is in `E`** (cap typing `CapKindA` and N1 `IpcHeapCovA`). -/ +theorem JSyscallA.epOfCapA {J5 J7 : JSlotA S} + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {sl p b : U64} {x y z w : Bool} (hsl : sl ∈ OS) (hcap : capA st s sl = .Endpoint p b x y z w) : p ∈ E := + J.ipcHeapCov.ep p ((J.capTy _ hsl).1 p b x y z w hcap) + +omit hA hM hPl hfm in +/-- **The Notification a slot cap names is in `N`**. -/ +theorem JSyscallA.ntfnOfCapA {J5 J7 : JSlotA S} + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {sl p b : U64} {x y : Bool} (hsl : sl ∈ OS) (hcap : capA st s sl = .Notification p b x y) : p ∈ N := + J.ipcHeapCov.ntfn p ((J.capTy _ hsl).2.1 p b x y hcap) + +omit hA hM hPl hfm in +/-- **The Reply object a slot cap names is in `Rp`**. -/ +theorem JSyscallA.replyOfCapA {J5 J7 : JSlotA S} + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {sl p : U64} {g : Bool} (hsl : sl ∈ OS) (hcap : capA st s sl = .Reply p g) : p ∈ Rp := + J.ipcHeapCov.reply p ((J.capTy _ hsl).2.2.1 p g hcap) + +/-- `IpcScopeA.sendNoblock` at the invocation site. -/ +theorem JSyscallA.sendNoblock_scopeA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (Q : SiteA st Z OS s σ info buf slot extra mi sl c excaps buffer argsU) + {p badge lbl : U64} + {recv grant grantReply call don fp : Bool} + {x : Endpoint} + (hcap : capA st s slot = .Endpoint p badge true recv grant grantReply) + (hk : σ.kheap (specPtr p) = some (.endpoint x)) + (hc : CanBlockA false x) + (hlab : info.label = lbl) : + ∃ Out, DispScope4A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ m Clock hPl.ipa info.label + (msgLenA info buf) slot false call don fp buf extra Out := by + rw [hlab] + exact ⟨_, Or.inr (Or.inr (Or.inr (Or.inl (IpcScopeA.sendNoblock (p := p) (badge := badge) (slot := slot) (lbl := lbl) (len := (msgLenA info buf)) (buf := buf) (recv := recv) (grant := grant) (grantReply := grantReply) (call := call) (don := don) (fp := fp) (extra := extra) (x := x) (hslot := Q.hOS) (hcov := Q.cov) (hcap := hcap) (hext := Q.hext) (hp := (J.epOfCapA Q.hOS hcap)) (hk := hk) (hc := hc)))))⟩ + +/-- `IpcScopeA.sendBlock` at the invocation site. -/ +theorem JSyscallA.sendBlock_scopeA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (Q : SiteA st Z OS s σ info buf slot extra mi sl c excaps buffer argsU) + {p badge lbl : U64} + {recv grant grantReply call don fp : Bool} + {x : Endpoint} + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (hcap : capA st s slot = .Endpoint p badge true recv grant grantReply) + (hk : σ.kheap (specPtr p) = some (.endpoint x)) + (hc : CanBlockA false x) + (hlab : info.label = lbl) : + ∃ Out, DispScope4A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ m Clock hPl.ipa info.label + (msgLenA info buf) slot true call don fp buf extra Out := by + rw [hlab] + exact ⟨_, Or.inr (Or.inr (Or.inr (Or.inl (IpcScopeA.sendBlock (p := p) (badge := badge) (slot := slot) (lbl := lbl) (len := (msgLenA info buf)) (buf := buf) (recv := recv) (grant := grant) (grantReply := grantReply) (call := call) (don := don) (fp := fp) (extra := extra) (x := x) (hCs := hCs) (h1 := h1) (hmb := hmb) (hmax := hmax) (htu := htu) (hslot := Q.hOS) (hcov := Q.cov) (hcap := hcap) (hext := Q.hext) (hSC := Q.hSC) (hp := (J.epOfCapA Q.hOS hcap)) (hk := hk) (hc := hc)))))⟩ + +/-- `IpcScopeA.sendRecv` at the invocation site. -/ +theorem JSyscallA.sendRecv_scopeA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (Q : SiteA st Z OS s σ info buf slot extra mi sl c excaps buffer argsU) + {p badge lbl : U64} + {recv grant grantReply blk call don fp : Bool} + {d : U64} + {q : List ObjRef} + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (hcap : capA st s slot = .Endpoint p badge true recv grant grantReply) + (hk : σ.kheap (specPtr p) = some (.endpoint (.recvEP (specPtr d :: q)))) + (hsnd : ∀ x, σ.kheap σ.curThread = some (.tcb x) → x.tcbFault = none ∧ (grant = false ∨ + ipcBufOfA false x = none ∨ + unat (dataToMessageInfo (x.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0)) + (hbr0 : (call = false ∧ don = false) ∨ (call = true ∧ grant = false ∧ grantReply = false)) + (hlab : info.label = lbl) : + ∃ Out, DispScope4A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ m Clock hPl.ipa info.label + (msgLenA info buf) slot blk call don fp buf extra Out := by + rw [hlab] + exact ⟨_, Or.inr (Or.inr (Or.inr (Or.inl (IpcScopeA.sendRecv (p := p) (badge := badge) (slot := slot) (lbl := lbl) (len := (msgLenA info buf)) (buf := buf) (recv := recv) (grant := grant) (grantReply := grantReply) (blk := blk) (call := call) (don := don) (fp := fp) (extra := extra) (d := d) (q := q) (hCs := hCs) (h1 := h1) (hmb := hmb) (hmax := hmax) (htu := htu) (hslot := Q.hOS) (hcov := Q.cov) (hcap := hcap) (hext := Q.hext) (hSC := Q.hSC) (hp := (J.epOfCapA Q.hOS hcap)) (hk := hk) (hsnd := hsnd) (hbr0 := hbr0)))))⟩ + +/-- `IpcScopeA.sendDonate` at the invocation site. -/ +theorem JSyscallA.sendDonate_scopeA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (Q : SiteA st Z OS s σ info buf slot extra mi sl c excaps buffer argsU) + {p badge lbl : U64} + {recv grant grantReply blk fp : Bool} + {d : U64} + {q : List ObjRef} + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (hcap : capA st s slot = .Endpoint p badge true recv grant grantReply) + (hk : σ.kheap (specPtr p) = some (.endpoint (.recvEP (specPtr d :: q)))) + (hsnd : ∀ x, σ.kheap σ.curThread = some (.tcb x) → x.tcbFault = none ∧ (grant = false ∨ + ipcBufOfA false x = none ∨ + unat (dataToMessageInfo (x.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0)) + (hg : grant = true ∨ grantReply = true) + (hrcv : ∀ y, σ.kheap (specPtr d) = some (.tcb y) → + y.tcbSchedContext = none ∧ ∃ e r dd, y.tcbState = .blockedOnReceive e (some r) dd) + (hV : L.mem s (core.num.U64.wrapping_add (L.curW s) 3664#u64) = 0#u64) + (hlab : info.label = lbl) : + ∃ Out, DispScope4A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ m Clock hPl.ipa info.label + (msgLenA info buf) slot blk true true fp buf extra Out := by + rw [hlab] + exact ⟨_, Or.inr (Or.inr (Or.inr (Or.inl (IpcScopeA.sendDonate (p := p) (badge := badge) (slot := slot) (lbl := lbl) (len := (msgLenA info buf)) (buf := buf) (recv := recv) (grant := grant) (grantReply := grantReply) (blk := blk) (fp := fp) (extra := extra) (d := d) (q := q) (hCs := hCs) (h1 := h1) (hmb := hmb) (hmax := hmax) (htu := htu) (hslot := Q.hOS) (hcov := Q.cov) (hcap := hcap) (hext := Q.hext) (hSC := Q.hSC) (hp := (J.epOfCapA Q.hOS hcap)) (hk := hk) (hsnd := hsnd) (hg := hg) (hrcv := hrcv) (hV := hV)))))⟩ + +/-- `IpcScopeA.sendNoDon` at the invocation site. -/ +theorem JSyscallA.sendNoDon_scopeA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (Q : SiteA st Z OS s σ info buf slot extra mi sl c excaps buffer argsU) + {p badge lbl : U64} + {recv grant grantReply blk fp : Bool} + {d : U64} + {q : List ObjRef} + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (hcap : capA st s slot = .Endpoint p badge true recv grant grantReply) + (hk : σ.kheap (specPtr p) = some (.endpoint (.recvEP (specPtr d :: q)))) + (hsnd : ∀ x, σ.kheap σ.curThread = some (.tcb x) → x.tcbFault = none ∧ (grant = false ∨ + ipcBufOfA false x = none ∨ + unat (dataToMessageInfo (x.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0)) + (hg : grant = true ∨ grantReply = true) + (hrcv : ∀ y, σ.kheap (specPtr d) = some (.tcb y) → + y.tcbSchedContext ≠ none ∧ ∃ e r dd, y.tcbState = .blockedOnReceive e (some r) dd) + (hlab : info.label = lbl) : + ∃ Out, DispScope4A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ m Clock hPl.ipa info.label + (msgLenA info buf) slot blk true true fp buf extra Out := by + rw [hlab] + exact ⟨_, Or.inr (Or.inr (Or.inr (Or.inl (IpcScopeA.sendNoDon (p := p) (badge := badge) (slot := slot) (lbl := lbl) (len := (msgLenA info buf)) (buf := buf) (recv := recv) (grant := grant) (grantReply := grantReply) (blk := blk) (fp := fp) (extra := extra) (d := d) (q := q) (hCs := hCs) (h1 := h1) (hmb := hmb) (hmax := hmax) (htu := htu) (hslot := Q.hOS) (hcov := Q.cov) (hcap := hcap) (hext := Q.hext) (hSC := Q.hSC) (hp := (J.epOfCapA Q.hOS hcap)) (hk := hk) (hsnd := hsnd) (hg := hg) (hrcv := hrcv)))))⟩ + +/-- `IpcScopeA.replyDetached` at the invocation site. -/ +theorem JSyscallA.replyDetached_scopeA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (Q : SiteA st Z OS s σ info buf slot extra mi sl c excaps buffer argsU) + {r lbl : U64} + {grant blk call don fp : Bool} + {x : Reply} + (hcap : capA st s slot = .Reply r grant) + (hk : σ.kheap (specPtr r) = some (.reply x)) + (hdet : x.replyTcb = none) + (hlab : info.label = lbl) : + ∃ Out, DispScope4A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ m Clock hPl.ipa info.label + (msgLenA info buf) slot blk call don fp buf extra Out := by + rw [hlab] + exact ⟨_, Or.inr (Or.inr (Or.inr (Or.inl (IpcScopeA.replyDetached (r := r) (slot := slot) (lbl := lbl) (len := (msgLenA info buf)) (buf := buf) (grant := grant) (blk := blk) (call := call) (don := don) (fp := fp) (extra := extra) (x := x) (hslot := Q.hOS) (hcov := Q.cov) (hcap := hcap) (hext := Q.hext) (hr := (J.replyOfCapA Q.hOS hcap)) (hk := hk) (hdet := hdet)))))⟩ + +/-- `IpcScopeA.replyLive` at the invocation site. -/ +theorem JSyscallA.replyLive_scopeA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (Q : SiteA st Z OS s σ info buf slot extra mi sl c excaps buffer argsU) + {r lbl d : U64} + {grant blk call don fp : Bool} + {xr : Reply} + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (h1 : fl4A64Platform.numNodes = 1) + (hag : fl4MinBudget = MIN_BUDGET) + (hmb : ∀ a, Fl4MinBudgetAgreeA ar a) + (hcap : capA st s slot = .Reply r grant) + (hk : σ.kheap (specPtr r) = some (.reply xr)) + (hcl : xr.replyTcb = some (specPtr d)) + (hsnd : ∀ x, σ.kheap σ.curThread = some (.tcb x) → x.tcbFault = none ∧ (grant = false ∨ + ipcBufOfA false x = none ∨ + unat (dataToMessageInfo (x.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0)) + (hrcv : ∀ y, σ.kheap (specPtr d) = some (.tcb y) → + y.tcbState = .blockedOnReply (specPtr r) ∧ y.tcbFault = none) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (hconv : Fl4TicksToUsLegacyAgree) + (hlab : info.label = lbl) : + ∃ Out, DispScope4A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ m Clock hPl.ipa info.label + (msgLenA info buf) slot blk call don fp buf extra Out := by + rw [hlab] + exact ⟨_, Or.inr (Or.inr (Or.inr (Or.inl (IpcScopeA.replyLive (r := r) (slot := slot) (lbl := lbl) (len := (msgLenA info buf)) (buf := buf) (d := d) (grant := grant) (blk := blk) (call := call) (don := don) (fp := fp) (extra := extra) (xr := xr) (hCs := hCs) (h1 := h1) (hag := hag) (hmb := hmb) (hslot := Q.hOS) (hcov := Q.cov) (hcap := hcap) (hext := Q.hext) (hSC := Q.hSC) (hr := (J.replyOfCapA Q.hOS hcap)) (hk := hk) (hcl := hcl) (hsnd := hsnd) (hrcv := hrcv) (hmax := hmax) (htu := htu) (hconv := hconv)))))⟩ + +/-- `IpcScopeA.replyFault` at the invocation site. -/ +theorem JSyscallA.replyFault_scopeA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (Q : SiteA st Z OS s σ info buf slot extra mi sl c excaps buffer argsU) + {r lbl d : U64} + {grant blk call don fp : Bool} + {xr : Reply} + {f : Fl4SpecA64.Fault} + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (h1 : fl4A64Platform.numNodes = 1) + (hag : fl4MinBudget = MIN_BUDGET) + (hmb : ∀ a, Fl4MinBudgetAgreeA ar a) + (hcap : capA st s slot = .Reply r grant) + (hk : σ.kheap (specPtr r) = some (.reply xr)) + (hcl : xr.replyTcb = some (specPtr d)) + (hrcv : ∀ y, σ.kheap (specPtr d) = some (.tcb y) → + y.tcbState = .blockedOnReply (specPtr r) ∧ y.tcbFault = some f) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (hconv : Fl4TicksToUsLegacyAgree) + (hok : ∀ x, σ.kheap σ.curThread = some (.tcb x) → + FaultReplyOKA (L := L) s d + (unat (dataToMessageInfo (x.tcbArch.tcbContext.userRegs msg_info_register)).miLength) f) + (hlab : info.label = lbl) : + ∃ Out, DispScope4A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ m Clock hPl.ipa info.label + (msgLenA info buf) slot blk call don fp buf extra Out := by + rw [hlab] + exact ⟨_, Or.inr (Or.inr (Or.inr (Or.inl (IpcScopeA.replyFault (r := r) (slot := slot) (lbl := lbl) (len := (msgLenA info buf)) (buf := buf) (d := d) (grant := grant) (blk := blk) (call := call) (don := don) (fp := fp) (extra := extra) (xr := xr) (f := f) (hCs := hCs) (h1 := h1) (hag := hag) (hmb := hmb) (hslot := Q.hOS) (hcov := Q.cov) (hcap := hcap) (hext := Q.hext) (hSC := Q.hSC) (hr := (J.replyOfCapA Q.hOS hcap)) (hk := hk) (hcl := hcl) (hrcv := hrcv) (hmax := hmax) (htu := htu) (hconv := hconv) (hok := hok)))))⟩ + +/-- `IpcScopeA.signalWaiting` at the invocation site. -/ +theorem JSyscallA.signalWaiting_scopeA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (Q : SiteA st Z OS s σ info buf slot extra mi sl c excaps buffer argsU) + {p badge lbl : U64} + {recv blk call don fp : Bool} + {x : Notification} + {q : List ObjRef} + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (hcap : capA st s slot = .Notification p badge true recv) + (hk : σ.kheap (specPtr p) = some (.notification x)) + (hw : x.ntfnObj = .waitingNtfn q) + (hlab : info.label = lbl) : + ∃ Out, DispScope4A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ m Clock hPl.ipa info.label + (msgLenA info buf) slot blk call don fp buf extra Out := by + rw [hlab] + exact ⟨_, Or.inr (Or.inr (Or.inr (Or.inl (IpcScopeA.signalWaiting (p := p) (badge := badge) (slot := slot) (lbl := lbl) (len := (msgLenA info buf)) (buf := buf) (recv := recv) (blk := blk) (call := call) (don := don) (fp := fp) (extra := extra) (x := x) (q := q) (hCs := hCs) (h1 := h1) (hmb := hmb) (hmax := hmax) (htu := htu) (hslot := Q.hOS) (hcov := Q.cov) (hcap := hcap) (hext := Q.hext) (hSC := Q.hSC) (hp := (J.ntfnOfCapA Q.hOS hcap)) (hk := hk) (hw := hw)))))⟩ + +/-- `IpcScopeA.signalBoundRecv` at the invocation site. -/ +theorem JSyscallA.signalBoundRecv_scopeA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (Q : SiteA st Z OS s σ info buf slot extra mi sl c excaps buffer argsU) + {p badge lbl : U64} + {recv blk call don fp : Bool} + {x : Notification} + {t e' : ObjRef} + {xt : Tcb} + {ro : Option ObjRef} + {d : ReceiverPayload} + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (hcap : capA st s slot = .Notification p badge true recv) + (hk : σ.kheap (specPtr p) = some (.notification x)) + (hidle : x.ntfnObj = .idleNtfn) + (hb : x.ntfnBoundTcb = some t) + (hkt : σ.kheap t = some (.tcb xt)) + (hs : xt.tcbState = .blockedOnReceive e' ro d) + (hlab : info.label = lbl) : + ∃ Out, DispScope4A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ m Clock hPl.ipa info.label + (msgLenA info buf) slot blk call don fp buf extra Out := by + rw [hlab] + exact ⟨_, Or.inr (Or.inr (Or.inr (Or.inl (IpcScopeA.signalBoundRecv (p := p) (badge := badge) (slot := slot) (lbl := lbl) (len := (msgLenA info buf)) (buf := buf) (recv := recv) (blk := blk) (call := call) (don := don) (fp := fp) (extra := extra) (x := x) (t := t) (e' := e') (xt := xt) (ro := ro) (d := d) (hCs := hCs) (h1 := h1) (hmb := hmb) (hmax := hmax) (htu := htu) (hslot := Q.hOS) (hcov := Q.cov) (hcap := hcap) (hext := Q.hext) (hSC := Q.hSC) (hp := (J.ntfnOfCapA Q.hOS hcap)) (hk := hk) (hidle := hidle) (hb := hb) (hkt := hkt) (hs := hs)))))⟩ + +/-- **CNode Copy's arm inputs** at one invocation site: `JSyscallA.cnodeCopy_scopeA`'s hypotheses past `hlab`. -/ +structure CnCopyArmA (st : machine.Store S) [L : LawfulStore st] (Z : XSetsA) (OS : Set U64) (n : Nat) (s : S) + (σ : State Unit) (m : McState) (buf slot : U64) (info : msginfo.MessageInfo) + (extra : Aeneas.Std.Array obj.Slot 3#usize) where + ps : U64 + pd : U64 + nc : capability.Cap + p : U64 + r : U64 + gs : U64 + g : U64 + index : U64 + depth : U64 + x0 : U64 + si : U64 + sdp : U64 + root : capability.Cap + ss : Fl4SpecA64.CslotPtr + sd : Fl4SpecA64.CslotPtr + hCs : ss.1 = specPtr ps ∧ Z.th ps = some (.cnode ss.2.length) + hCd : sd.1 = specPtr pd ∧ Z.th pd = some (.cnode sd.2.length) + hsn : capA st s (cslotAddrA ss) ≠ .Null + hdn : capA st s (cslotAddrA sd) = .Null + hdc : obj.sext48 (cslotAddrA sd) = ok (cslotAddrA sd) + hw : CapRt.EncWf nc + hn : nc ≠ .Null + hD : Fl4ProofsA64.P3.DerivA nc (capA st s (cslotAddrA ss)) + hal : UA nc (capA st s (cslotAddrA ss)) + hk : SameBadgeKindA nc (capA st s (cslotAddrA ss)) + hsh : Fl4ProofsA64.P3.shapeA nc = Fl4ProofsA64.P3.shapeA (capA st s (cslotAddrA ss)) + hsh1 : Fl4ProofsA64.P1.W7.R.ST.shapeA nc = Fl4ProofsA64.P1.W7.R.ST.shapeA (capA st s (cslotAddrA ss)) + hbd1 : badgeA nc = badgeA (capA st s (cslotAddrA ss)) ∨ badgeA (capA st s (cslotAddrA ss)) = 0#u64 + hbd : ∀ r b rs, (absCapA (capA st s (cslotAddrA ss)) = .endpointCap r b rs ∨ + absCapA (capA st s (cslotAddrA ss)) = .notificationCap r b rs) → b ≠ 0 → + Fl4SpecA64.capEpBadge (absCapA nc) = b + hirq : ∀ i, nc = .IrqHandler i ↔ capA st s (cslotAddrA ss) = .IrqHandler i + hz : ∀ p t k, capA st s (cslotAddrA ss) ≠ .Zombie p t k + hut : isUtA (capA st s (cslotAddrA ss)) = true → ∀ y, ¬ Fl4Proofs.P3.Mdb.Desc Z.G.par y (cslotAddrA ss) + hdo : ¬ IrqNodeA (L.ksb s) (fl4Ptr sd.1) ∨ IrqCapA (absCapA nc) + hcv : CapValidA Z.th (capA st s (cslotAddrA ss)) → CapValidA Z.th nc + hslc : capA st s slot = .CNode p r gs g + hlb : info.label = fl4_abi.label.CNodeCopy ∨ info.label = fl4_abi.label.CNodeMint + hl4 : ¬ (msgLenA info buf) < 4#u64 + hl2 : ¬ (msgLenA info buf) < 2#u64 + hi : msg.syscall_arg st s (L.curW s) buf 0#u64 = ok index + hd0 : msg.syscall_arg st s (L.curW s) buf 1#u64 = ok depth + hres : cspace.lookup_target_slot st s (.CNode p r gs g) index depth = ok (.Ok (cslotAddrA sd)) + hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0 + h0 : x0 ≠ 0#u64 + h2 : msg.syscall_arg st s (L.curW s) buf 2#u64 = ok si + h3 : msg.syscall_arg st s (L.curW s) buf 3#u64 = ok sdp + hroot : obj.Slot.cap st x0 s = ok root + hemp : cdt.ensure_empty_slot st s (cslotAddrA sd) = ok (.Ok ()) + hsrc : cspace.lookup_source_slot st s root si sdp = ok (.Ok (cslotAddrA ss)) + hcop : cnode.copied_cap st s info.label (msgLenA info buf) buf (cslotAddrA ss) (capA st s (cslotAddrA ss)) = ok (.Ok (nc, false)) + hsci : ∀ anc, CapRelA nc anc → NoHiPtV anc → NoIdleCapV m n anc + +/-- `CnScopeA.cnodeCopy` from the bundled arm inputs. -/ +theorem JSyscallA.cnodeCopyArm_scopeA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (Q : SiteA st Z OS s σ info buf slot extra mi sl c excaps buffer argsU) + (X : CnCopyArmA st Z OS n s σ m buf slot info extra) (bB bC bD : Bool) : + ∃ Out, DispScope4A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ m Clock hPl.ipa info.label + (msgLenA info buf) slot bB bC bD false buf extra Out := + J.cnodeCopy_scopeA hA hM hPl hfm Q (lbl := info.label) (bB := bB) (bC := bC) (bD := bD) + X.ss X.sd X.hCs X.hCd X.hsn X.hdn X.hdc X.hw X.hn X.hD X.hal X.hk X.hsh X.hsh1 X.hbd1 X.hbd X.hirq X.hz X.hut X.hdo X.hcv X.hslc X.hlb X.hl4 X.hl2 X.hi X.hd0 X.hres X.hx0 X.h0 X.h2 X.h3 X.hroot X.hemp X.hsrc X.hcop X.hsci rfl + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3SendBlkA.lean b/proofs/Fl4ProofsA64/P3/Stage3SendBlkA.lean new file mode 100644 index 000000000..64717e0ec --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SendBlkA.lean @@ -0,0 +1,233 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2b-1): **the blocking send of the current thread at the record**: `send_ipc` with +`blocking = true` on an endpoint that is idle or holds waiting senders. The sender is inserted by priority into the +endpoint's queue. + +* `JStage3A.blkQ_sendA`, `JStage3A.ntfnWait_blkA`: `BlkQueuedA` and the notification waiters across it. +* **`JStage3A.sendBlkA`**: the program, the spec outcome (`blkSendA`) and the record, given P1's invariants at the + new state (`BXI`, `SysL`, `SymL`, `UserOK`, `J7A`; each caller has its own source for them). The sender is any + runnable current thread, call or not, faulted or not. + +[S] the endpoint's state (idle or `SendEP`); `h1` (`numNodes = 1`). +[P] `hCs`, `hmb`, `hmax`, `htu`. New module. +-/ +import Fl4ProofsA64.P3.Stage3SendBlkSpecA +import Fl4ProofsA64.P3.Stage3SendRecvArmA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {J5 : JSlotA S} {R : Set U64 → JSlotA S} {s s' : S} {a : A} {σ σ' : State Unit} + {m : McState} + +/-- **`NtfnWaitKeepMA`** from the blocking send's write set: the threads waiting on a notification are not queued on +the endpoint, so no `Ep` link or block word of theirs is written. -/ +theorem JStage3A.ntfnWait_blkA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7A st R) s a σ m) + {t e : U64} (ht : t ∈ Z.T) (he : e ∈ E) + (w : WritesIn st s s' (BlkSendFpA st Z.T (fun σ s => MWrA (st := st) Z m σ s) σ s t e)) : + NtfnWaitKeepMA (fun o => o = specPtr t) Z.T N (L.mem s) (L.mem s') := by + intro y hy hM hN + have hP := J.iplaced + have hya := hP.e.tcb y hy + have hyt : y ≠ t := fun c => hM (by rw [c]) + have apart : ∀ u ∈ Z.T, u ≠ y → u.val + 4096 ≤ y.val ∨ y.val + 4096 ≤ u.val := fun u hu ne => by + have := J.sr.place.apart u y .tcb .tcb ((J.sr.thT u).2 hu) ((J.sr.thT y).2 hy) ne + have hsz : ObjKindA.size .tcb = 4096 := rfl + omega + have key : ∀ (K : U64) (b : U64), (K.val = 3336 ∨ K.val = 3496 ∨ K.val = 3504) → b = wa y K → Aligned b → + L.mem s' b = L.mem s b := by + intro K b hK hb hal + have hfy := hya.fits + have hbv : b.val = y.val + K.val := by rw [hb, wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have sw : ∀ (k : U64), b.val = t.val + k.val → k.val < 4096 → False := fun k h hk => by + have := apart t ht hyt.symm + omega + refine w.mem_eq hal ?_ + rintro (c | c | c | c | c | ⟨v, hv, c⟩ | c) + · exact sw 3328#u64 (by rw [c]; exact Endp.val_st (hP.e.tcb t ht)) (by simp) + · exact sw 3336#u64 (by rw [c]; exact Endp.val_blk (hP.e.tcb t ht)) (by simp) + · exact sw 3344#u64 (by rw [c]; exact Endp.val_badge (hP.e.tcb t ht)) (by simp) + · exact sw 3352#u64 (by rw [c]; exact Endp.val_flags (hP.e.tcb t ht)) (by simp) + · have := hP.e.et e he y hy; unfold Endp.EW at c; omega + · have hvT : v ∈ Z.T := by + rcases hv with rfl | hv + · exact ht + · exact hv.1 + have hvy : v ≠ y := by + rcases hv with rfl | hv + · exact hyt.symm + · intro h + subst h + rw [hv.2] at hN + have := hP.ne e hN e he + omega + have := apart v hvT hvy + have := (hP.e.tcb v hvT).fits + unfold TL at c + omega + · rw [hb] at c + exact J.tcbWord_offMwrA hy (by omega) (by omega) c + refine ⟨?_, ?_, ?_⟩ + · exact key 3336#u64 _ (by simp) (by unfold Notif.tBlockA; rfl) (Endp.a_blk hya).1 + · exact key 3504#u64 _ (by simp) (by rw [prevEp_eq]) (Endp.a_prev hya).1 + · exact key 3496#u64 _ (by simp) (by rw [nextEp_eq]) (Endp.a_next hya).1 + +/-- **`BlkQueuedA`** across the blocking send: the sender is queued on the endpoint it blocks on. -/ +theorem JStage3A.blkQ_sendA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7A st R) s a σ m) + {t e : U64} (ht : t ∈ Z.T) (he : e ∈ E) {x : Endpoint} {xt : Tcb} {pl : SenderPayload} + (B : BlkStepA σ σ' (specPtr t) (specPtr e) x xt pl) {i : ObjRef} {Nq : ObjRef → Prop} + (K : SIKA i (fun o => o = specPtr t) Nq false σ σ') (E' : EpStateRel Z.T E Rp Bf (L.mem s') σ') + (wk : NtfnWaitKeepMA (fun o => o = specPtr t) Z.T N (L.mem s) (L.mem s')) : + BlkQueuedA Z.T E N (L.mem s') σ' := by + have kept : ∀ u ∈ Z.T, ∀ y, σ'.kheap (specPtr u) = some (.tcb y) → specPtr u ≠ specPtr t → + ∃ z, σ.kheap (specPtr u) = some (.tcb z) ∧ z.tcbState = y.tcbState := fun u _ y hy hM => by + obtain ⟨z, hz, g⟩ := K.tcbB hy + exact ⟨z, hz, (g.st.resolve_right fun c => hM c.1).symm⟩ + refine ⟨fun u hu y hy p hb => ?_, fun u hu y hy p hp => ?_⟩ + · by_cases hM : specPtr u = specPtr t + · have eu : u = t := specPtr_inj hM + subst eu + obtain ⟨y1, hy1, r'⟩ := E'.tcb u hu + rw [hy] at hy1; cases hy1 + have hb' := r'.blk p hb + rw [B.atT] at hy; cases hy + simp only [blkTcbA, epBlocked, Option.some.injEq] at hb + subst hb + have eq : L.mem s' (Notif.tBlockA u) = e := specPtr_inj hb' + exact ⟨by rw [eq]; exact he, _, B.atE, mem_specInsA.2 (Or.inl rfl)⟩ + · obtain ⟨z, hz, es⟩ := kept u hu y hy hM + obtain ⟨hE, q, hq, hm⟩ := J.blkQ.ep u hu z hz p (es ▸ hb) + obtain ⟨q', hq', hmem⟩ := K.epF hq + obtain ⟨x1, hx1, r⟩ := J.ep.tcb u hu + obtain ⟨y1, hy1, r'⟩ := E'.tcb u hu + rw [hz] at hx1; cases hx1 + rw [hy] at hy1; cases hy1 + have eq : L.mem s' (Notif.tBlockA u) = L.mem s (Notif.tBlockA u) := + specPtr_inj ((r'.blk p hb).trans (r.blk p (es ▸ hb)).symm) + exact ⟨by rw [eq]; exact hE, q', hq', hmem _ hm hM⟩ + · have hM : specPtr u ≠ specPtr t := fun c => by + rw [c, B.atT] at hy + cases hy + cases hp + obtain ⟨z, hz, es⟩ := kept u hu y hy hM + obtain ⟨hN, z', q, hq, ho, hm⟩ := J.blkQ.ntfn u hu z hz p (es.trans hp) + exact ⟨by rw [(wk u hu hM hN).1]; exact hN, z', q, K.ntfnF hq, ho, hm⟩ + +set_option maxHeartbeats 2000000 in +/-- **The blocking send at the record** (module docstring). -/ +theorem JStage3A.sendBlkA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {t : U64} (ht : t ∈ Z.T) {xt : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hsr : SndRunA σ (specPtr t) xt) (hsc : ∀ sc, xt.tcbSchedContext = some sc → ScActiveAtA σ sc) + (hnvm : L.mem s (stA t) ≠ sched.TS_RUNNING_VM) (hns : ∀ u, σ.schedulerAction ≠ .switchThread u) + {p : U64} (hp : p ∈ E) {x : Endpoint} (hk : σ.kheap (specPtr p) = some (.endpoint x)) (hc : CanBlockA false x) + (call : Bool) (badge : U64) (g gr don : Bool) : + ∃ s2, endpoint.send_ipc st ar s a true call badge g gr don t p = ok (s2, a) ∧ + sendIpc (Z := Unit) true call badge.bv g gr don (specPtr t) (specPtr p) σ = + ({((), blkSendA σ (specPtr t) (specPtr p) x xt (sndPlA badge.bv g gr call))}, False) ∧ + WritesIn st s s2 (fun b => SchedStepNWA (st := st) N E Rp Z m σ s b ∨ BufW Bf b) ∧ + ∃ Q Rl, + (BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s2 → + SysL st H Z.Cs OS ia ie s2 → SymL st H Z.Cs s2 → Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s2 → + J7A st (J7R2A st ar nA) Z H OS s2 (blkSendA σ (specPtr t) (specPtr p) x xt (sndPlA badge.bv g gr call)) m → + JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s2 a (blkSendA σ (specPtr t) (specPtr p) x xt (sndPlA badge.bv g gr call)) m) := by + have hcm : m.curCore < fl4A64Platform.numNodes := by rw [J.j.1.sch.core]; exact hCs _ J.j.1.sch.cpuCs + have hO : SchedOpsEA st Z.T E Rp Bf (IpcSchedRelA st Z m κ) := + (ipcSchedOpsA (ar := ar) J.iplaced J.ipcPlace hcm hmb hmax htu).2.2.1 + have hF : SchedFootEA st Z.T E Rp Bf (IpcSchedRelA st Z m κ) (fun σ s => MWrA (st := st) Z m σ s) := + schedFootEA_of_sched + have hSR : IpcSchedRelA st Z m κ s σ := ⟨Z.Q, Z.R, J.sr⟩ + have hx := J.ep.at_ep hp hk + have hsort := prioSorted_of_specA J.iplaced.e.tcb J.ep hx (J.ipcSorted.1 (specPtr p) ⟨p, hp, rfl⟩ x hk) + have body := send_ipc_block_fpA (st := st) (ar := ar) hO hF J.iplaced.e s a σ J.ep hSR ht hkt hsr hp hk hc hsort + call badge g gr don + obtain ⟨⟨s2, a2⟩, eb, ha2, hspec, E2, ⟨Q, Rl, sr2⟩, w, hz⟩ := exists_of_specA body + simp only at ha2 eb E2 sr2 w + subst ha2 + have hte : specPtr t ≠ specPtr p := fun h => by rw [h, hk] at hkt; cases hkt + generalize hσ' : blkSendA σ (specPtr t) (specPtr p) x xt (sndPlA badge.bv g gr call) = σ' at hspec E2 sr2 hz ⊢ + have B : BlkStepA σ σ' (specPtr t) (specPtr p) x xt (sndPlA badge.bv g gr call) := hσ' ▸ blkSendA_step hte + have wI : WritesIn st s s2 (IpcFpA st Z E Rp Bf m σ s) := w.mono fun b h => by + have hta := J.iplaced.e.tcb t ht + have tw : ∀ (k : Nat), b.val = t.val + k → TWOffN k → IpcFpA st Z E Rp Bf m σ s b := fun k hv hk => + Or.inl (Or.inr (Or.inr (Or.inl ⟨t, ht, Or.inl (twf_ofA k hv hk)⟩))) + rcases h with c | c | c | c | c | ⟨v, hv, c⟩ | c + · exact tw 3328 (by rw [c]; exact Endp.val_st hta) (by unfold TWOffN; omega) + · exact tw 3336 (by rw [c]; exact Endp.val_blk hta) (by unfold TWOffN; omega) + · exact tw 3344 (by rw [c]; exact Endp.val_badge hta) (by unfold TWOffN; omega) + · exact tw 3352 (by rw [c]; exact Endp.val_flags hta) (by unfold TWOffN; omega) + · exact Or.inl (Or.inl ⟨p, hp, c⟩) + · have hvT : v ∈ Z.T := by + rcases hv with rfl | hv + · exact ht + · exact hv.1 + exact Or.inl (Or.inr (Or.inr (Or.inl ⟨v, hvT, Or.inr c⟩))) + · exact Or.inr c + have wN : WritesIn st s s2 (fun b => SchedStepNWA (st := st) N E Rp Z m σ s b ∨ BufW Bf b) := + wI.mono fun _ h => h.elim (ewrite_stepNA_buf J.sr.core.tcbs) fun h => Or.inl (Or.inl (Or.inl h)) + have wI' : WritesIn st s s2 (fun b => IpcFpA st Z E Rp Bf m σ s b ∨ RepWA Rp b) := wI.mono fun _ => Or.inl + refine ⟨s2, eb, hspec, wN, Q, Rl, fun bxi sysl symL userOK j7 => ?_⟩ + set M : ObjRef → Prop := fun o => o = specPtr t with hMdef + have K : SIKA σ.idleThread M (fun _ => False) false σ σ' := B.sika hkt hk rfl + have notM : ∀ c < n, ¬ M (m.idleThreadOf c) := by + intro c hc' h + obtain ⟨y, hy, hs⟩ := J.sidle.f3 c hc' + rw [h, hkt] at hy + cases hy + have := hsr.1 + rw [hs] at this + exact absurd this (by decide) + have sinv : SInvXA σ' m n := by + refine K.sinv J.sinv notM (fun h => hsr.2.2.2 h.symm) ?_ (fun _ h => h.elim) (fun _ h => h.elim) hns + (fun y hy => by rw [show y = specPtr t from hy]; exact hsr.2.1.2) (J.sidle.rdyA J.sinv) + (fun q hq x y hx hy hf => by + rw [show q = specPtr t from hq, B.atT] at hy; cases hy; exact Or.inl ⟨_, _, rfl⟩) + intro y hy dm pr + rw [show y = specPtr t from hy] + exact hsr.2.1.1 dm pr + have wk := J.ntfnWait_blkA (s' := s2) ht hp w + have ntfn := J.ntfn_ipcMA wI' K E2 wk (fun u hu hm y hy q h => by + rw [show specPtr u = specPtr t from hm, hkt] at hy + cases hy + have := hsr.1 + rw [h] at this + exact absurd this (by simp [runnable])) (fun u hu hm y hy q h => by + rw [show specPtr u = specPtr t from hm, B.atT] at hy + cases hy + cases h) + have hMv : ∀ u ∈ Z.T, M (specPtr u) → L.mem s (Fl4ProofsA64.P1.stA u) ≠ sched.TS_RUNNING_VM := fun u _ hu => by + rw [specPtr_inj (show specPtr u = specPtr t from hu)]; exact hnvm + have hq : ∀ a ∈ x.epQueue, ∃ y, σ.kheap a = some (.tcb y) := by + intro a ha + obtain ⟨xs, hQ, hxs, -⟩ := hx.queue + rw [hxs] at ha + obtain ⟨u, hu, rfl⟩ := List.mem_map.1 ha + obtain ⟨y, hy, -⟩ := J.ep.tcb u (hQ.mem u hu) + exact ⟨y, hy⟩ + exact J.of_ipcStepUSA (Q := Q) (Rl := Rl) (curOf := Z.curOf) (curScOf := Z.curScOf) wN (B.specSched hkt hk) hz + (B.qblk J.ipcSorted hkt hk hq) (fun _ _ => by rw [B.machine]) (fun _ _ => rfl) bxi sysl symL userOK j7 + sr2 E2 ntfn (J.reply_ipcA wI K) J.deadExt.reply K.ipcKeep (J.tw_ipcA wI') sinv (K.sidle J.sidle notM (J.sidle.rdyA J.sinv)) + (J.runVcpu_ipcA wI' K E2 hMv) (J.blkQ_sendA ht hp B K E2 wk) (B.schedValid J.schedValid hkt hk hsc) + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3SendBlkArmA.lean b/proofs/Fl4ProofsA64/P3/Stage3SendBlkArmA.lean new file mode 100644 index 000000000..e6c1f8d6a --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SendBlkArmA.lean @@ -0,0 +1,384 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2b-1): **blocking send / call with no receiver waiting, and `handle_fault` with no +receiver waiting, at the record.** The handler endpoint is idle or holds waiting senders; the current thread blocks +on it, inserted by priority (`Stage3SendBlkA.sendBlkA`). + +* **`JSyscallA.sendBlockA`** / **`sendBlockDispA`**: `decode_invocation` on an Endpoint send cap with + `blocking = true` (any call / donate / grant flags, any sender: faulted or with extra caps too). +* **`BodyEndA.ofBlkSendA`** (the blocked-caller shape): the caller's `Restart` and its blocking on an endpoint end in + `BodyEndA` (only the caller and the endpoint change; the action is kept or `chooseNewThread`, never resuming a + blocked caller). +* **`JSyscallA.faultBlockA`**: `handle_fault` of the current thread (user or VM fault, `UsrFaultA`) whose fault + handler is an endpoint with no waiting receiver. + +The spec data of the arms (endpoint, badge, rights) are those of the FL4 cap read at the entry (`hcap`; for the fault +arm also `hgc`): no free datum. + +[S] the endpoint's state; `h1` (`numNodes = 1`). +[P] `ArchTotal`, `MaxSpi`, `ArchPlat`, `A64FrameMasks`, `hCs`, `hmb`, `hmax`, `htu`. New module. +-/ +import Fl4ProofsA64.P3.Stage3SendBlkA +import Fl4ProofsA64.P3.Stage3FaultArmA +import Fl4ProofsA64.P3.Stage3KEntrySendBlkA +import Fl4ProofsA64.P1.MdbSendE +import Fl4ProofsA64.P3.Stage3BodyCurA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {s : S} {a : A} {σ : State Unit} {m : McState} + +/-- The endpoint's state word of an idle or sending endpoint. -/ +theorem EpObjRel.canBlock_stA {m : U64 → U64} {T : Set U64} {p : U64} {x : Endpoint} (h : EpObjRel m T p x) + (hc : CanBlockA false x) : stOf m p = 0#u64 ∨ stOf m p = 1#u64 := by + cases x with + | idleEP => exact Or.inl h.1 + | sendEP q => exact Or.inr h.1 + | recvEP q => simp [CanBlockA] at hc + +/-- **The blocked-caller shape** (module docstring). -/ +theorem Sched.BodyEndA.ofBlkSendA {J5 J7 : JSlotA S} + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {xc : Tcb} (hkc : σ.kheap σ.curThread = some (.tcb xc)) {t e : ObjRef} (ht : σ.curThread = t) {x : Endpoint} + (hk : σ.kheap e = some (.endpoint x)) (pl : SenderPayload) : + BodyEndA σ (blkSendA (stUpdA σ σ.curThread xc .restart) t e x { xc with tcbState := .restart } pl) m := by + subst ht + have ne : σ.curSc ≠ σ.curThread := fun h => by + obtain ⟨y0, j, h0⟩ := J.curInv.sc + rw [h, hkc] at h0; cases h0 + have nee : σ.curSc ≠ e := fun h => by + obtain ⟨y0, j, h0⟩ := J.curInv.sc + rw [h, hk] at h0; cases h0 + unfold blkSendA + refine ⟨BodyOKA.ofEntry J ?_ ?_ ?_ ?_ ?_ ?_ ?_ (fun y' k h => ?_) (fun h => ?_), ?_⟩ + all_goals first + | (unfold stsAct; split_ifs <;> first | exact Or.inl rfl | exact Or.inr rfl) + | (unfold stsAct; split_ifs <;> rfl) + | skip + · simp only [updK_kheap, stsAct_kheap, Function.update_of_ne nee, Function.update_of_ne ne, stUpdA, setObjA] at h + exact ⟨y', h, rfl, rfl, rfl⟩ + · unfold stsAct at h + split_ifs at h with hc + · cases h + · exfalso + refine hc ⟨rfl, h, fun hs => ?_⟩ + have := (schedulable_partsA hs).1 (blkTcbA { xc with tcbState := .restart } e pl) (by simp [updK_kheap]) + simp [runnable, blkTcbA] at this + +/-- **The blocked-caller shape from a faulted caller**: the caller's TCB write (its fault) and its blocking on an +endpoint end in `BodyEndA`. -/ +theorem Sched.BodyEndA.ofBlkFaultA {J5 J7 : JSlotA S} + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {x1 : Tcb} {t e : ObjRef} (ht : σ.curThread = t) {x : Endpoint} (hk : σ.kheap e = some (.endpoint x)) + (pl : SenderPayload) : BodyEndA σ (blkSendA (updK σ t (.tcb x1)) t e x x1 pl) m := by + subst ht + have ne : σ.curSc ≠ σ.curThread := fun h => by + obtain ⟨y0, j, h0⟩ := J.curInv.sc + obtain ⟨x, hx, -⟩ := J.curInv.tcb + rw [h, hx] at h0; cases h0 + have nee : σ.curSc ≠ e := fun h => by + obtain ⟨y0, j, h0⟩ := J.curInv.sc + rw [h, hk] at h0; cases h0 + unfold blkSendA + refine ⟨BodyOKA.ofEntry J ?_ ?_ ?_ ?_ ?_ ?_ ?_ (fun y' k h => ?_) (fun h => ?_), ?_⟩ + all_goals first + | (unfold stsAct; split_ifs <;> first | exact Or.inl rfl | exact Or.inr rfl) + | (unfold stsAct; split_ifs <;> rfl) + | skip + · simp only [updK_kheap, stsAct_kheap, Function.update_of_ne nee, Function.update_of_ne ne] at h + exact ⟨y', h, rfl, rfl, rfl⟩ + · unfold stsAct at h + split_ifs at h with hc + · cases h + · exfalso + refine hc ⟨rfl, h, fun hs => ?_⟩ + have := (schedulable_partsA hs).1 (blkTcbA x1 e pl) (by simp [updK_kheap]) + simp [runnable, blkTcbA] at this + +set_option maxHeartbeats 2000000 in +/-- **Blocking send or call with no receiver waiting, at a syscall entry** (module docstring). -/ +theorem JSyscallA.sendBlockA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p badge slot lbl len buf : U64} {recv grant grantReply call don fp : Bool} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) + (hcap : capA st s slot = .Endpoint p badge true recv grant grantReply) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hp : p ∈ E) {x : Endpoint} (hk : σ.kheap (specPtr p) = some (.endpoint x)) (hc : CanBlockA false x) : + ∃ xc σ1 σ2 s2, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + sendIpc (Z := Unit) true call badge.bv grant grantReply don σ.curThread (specPtr p) σ1 = + ({((), σ2)}, False) ∧ + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) true call don fp buf extra = + ok (.Ok (), s2, a) ∧ + ∃ Q Rl, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s2 a σ2 m ∧ Sched.BodyEndA σ σ2 m := by + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hslot hcov J.actW J.running J.cur + have hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM := J.toJStage3EntryA.curNotVmA + obtain ⟨xc, hkc, hst, hsp, J1, -, hOS1, -⟩ := J.toJStage3A.of_restartCurA + RS.pl.choose_spec.choose_spec.choose_spec.choose_spec RS.sym RS.run hnvm RS.cur RS.vs RS.vc + have hst1 : L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART) (stA (L.curW s)) = sched.TS_RESTART := + rd_wr_eq (by rw [stA_eq]; exact field_aligned (J.sr.core.tcbs _ J.sr.curT) (by simp) (by simp)) + generalize hs1 : L.wr s (stA (L.curW s)) sched.TS_RESTART = s1 at hst J1 hOS1 hst1 + set t := L.curW s with ht_def + set σ1 := stUpdA σ σ.curThread xc .restart with hσ1 + have kb1 : L.ksb s1 = L.ksb s := by rw [← hs1]; exact L.ksb_wr _ _ _ + have ct : σ.curThread = specPtr t := J.j.1.sch.curThread + have htZ : t ∈ Z.T := J.sr.curT + set xt : Tcb := { xc with tcbState := .restart } with hxt + have hkt : σ1.kheap (specPtr t) = some (.tcb xt) := by + rw [hσ1, ← ct]; simp [stUpdA, setObjA, hxt] + have ne1 : specPtr p ≠ σ.curThread := fun e => by rw [e, hkc] at hk; cases hk + have hk1 : σ1.kheap (specPtr p) = some (.endpoint x) := by + rw [hσ1]; simp only [stUpdA, setObjA, Function.update_of_ne ne1]; exact hk + have hidle : specPtr t ≠ σ1.idleThread := by + obtain ⟨y, hy, hs⟩ := J1.sinv.idleTcb + intro h + rw [← h, hkt] at hy + cases hy + cases hs + have hRun : SndRunA σ1 (specPtr t) xt := + J.sndRunA hCs h1 (by rw [hxt]; rfl) (fun dm pr hm => by + refine J1.sinv.ctQ (by show σ.schedulerAction = .resumeCurThread; rw [J.sr.sch.act]; unfold actOfA; rw [if_pos J.actW]) dm pr ?_ + rw [show σ1.curThread = specPtr t from ct] + exact hm) rfl rfl hidle + have hsc : ∀ sc, xt.tcbSchedContext = some sc → ScActiveAtA σ1 sc := by + intro sc hs + obtain ⟨y, sc', ex, es, a'⟩ := (J1.schedValid.act J.act (fun c => hidle (ct ▸ c))).1 + rw [show σ1.curThread = specPtr t from ct, hkt] at ex + cases ex + rw [hs] at es + cases es + exact a' + have hnvm1 : L.mem s1 (stA t) ≠ sched.TS_RUNNING_VM := by + rw [hst1]; unfold sched.TS_RESTART sched.TS_RUNNING_VM; decide + obtain ⟨s2, eb, hspec, -, Q, Rl, K⟩ := J1.sendBlkA (ar := ar) hCs hmb hmax htu htZ hkt hRun hsc hnvm1 J.noSwitch hp hk1 + hc call badge grant grantReply don + have hdec : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) true call don fp buf extra = + ok (.Ok (), s2, a) := by + rw [hcap, decodeEndpoint_programA] + simp only [↓reduceIte, ipcInvokeProgramA, cur_thread_ok, bind_ok, ← ht_def, hst] + rw [show endpoint.send_ipc st ar s1 a true call badge grant grantReply don t p = ok (s2, a) from eb] + simp only [bind_ok] + rfl + -- P1's invariants after the send + obtain ⟨Pt, Ub, Pp7, Tb7, Hr, Te, Kt, Lv7, Re, bits, KLv, W⟩ := J1.j7.unp + have I := J1.delIn hA hPl W + have htH : t ∈ H.tcbs := by rw [J.tcbs]; exact htZ + have kt := spec_at (sched.set_thread_state.restart_blk (Es := ∅) s t (BXI.blk J.sys.ent.bxi) + htH (runnable_ok J.running).1 (runnable_ok J.running).2) hst + have D := J.decPreA hslot hext + have kt1 := kt.2.2.2 + have hc1 := D.cb.restart (BXI.blk J.sys.ent.bxi).1.choose_spec.choose_spec.tcbs kt1 kt.2.2.1 + have hH : H p = some .ep := J.utIpc.ep p hp + have P := (J.toJStage3A.sysPlace hext hslot hcov).keepO hOS1 kb1 + have hnames : ∀ k, H p = some k → ∃ x ∈ OS, NamesA (capA st s1 x) p k := fun k hk' => by + rw [hH] at hk' + cases hk' + exact ⟨slot, hslot, by rw [hOS1.capA hslot, hcap]; exact names_ep⟩ + have hep : L.epState s1 p = 0#u64 ∨ L.epState s1 p = 1#u64 := by + rw [epState_eq]; exact EpObjRel.canBlock_stA (J1.ep.at_ep hp hk1) hc + have p1 := Fl4ProofsA64.P1.W7.R.ST.endpoint.send_ipc.blockL (st := st) (ar := ar) a true call badge grant grantReply don t p + P (BXI.blk J1.sys.ent.bxi) I.del.sca J1.sys.sysl J1.symL hH hep hnames (fun _ => hc1) + (by rw [hst1]; unfold sched.TS_RESTART sched.TS_INACTIVE; decide) + have p1' := post_of_okA p1 eb + simp only [WP.uncurry'_pair] at p1' + have F := J.decodeFinA hA hM hPl hfm hslot hcov (rfl : capA st s slot = capA st s slot) hext hSC (lbl := lbl) + (by rw [hcap]; simp [DecArmS1aEnt, DecArmQH, DecArmQZ2, DecArmKQ2, DecArmKQ, capFam]) + (fun _ h => by rw [hcap] at h; cases h) (fun _ h => by rw [hcap] at h; cases h) hdec + (blkSendA σ1 (specPtr t) (specPtr p) x xt (sndPlA badge.bv grant grantReply call)) m + obtain ⟨-, -, -, -, -, userOK, j7⟩ := F + exact ⟨xc, σ1, _, s2, hkc, rfl, hsp, by rw [ct]; exact hspec, hdec, Q, Rl, + K (BlkOK.bxi p1'.2.2) p1'.1 p1'.2.1 userOK j7, Sched.BodyEndA.ofBlkSendA J hkc ct hk _⟩ + +set_option maxHeartbeats 2000000 in +/-- **A user or VM fault with no receiver waiting on the handler endpoint** (module docstring): the faulted thread +blocks on it. -/ +theorem JSyscallA.faultBlockA + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {fe : error.Fault} {ty w0 w1 w2 : U64} {f : Fl4SpecA64.Fault} (F : UsrFaultA st (L.curW s) fe ty w0 w1 w2 f) + {p badge : U64} {sd rv g gr : Bool} + (hcap : capA st s (slotIdx (L.curW s) layout.tcb.SLOT_FAULT_HANDLER) = .Endpoint p badge sd rv g gr) + (hgc : getCap (Z := Unit) (getTcbFaultHandlerPtr σ.curThread) σ = + ({(.endpointCap (specPtr p) badge.bv (rightsOfA sd rv g gr), σ)}, False)) + (hp : p ∈ E) {x : Endpoint} (hk : σ.kheap (specPtr p) = some (.endpoint x)) (hc : CanBlockA false x) : + ∃ σ2 s2, ((), σ2) ∈ (handleFault (Z := Unit) σ.curThread f σ).1 ∧ + faults.handle_fault st ar s a (L.curW s) fe = ok (s2, a) ∧ + Sched.BodyEndA σ σ2 m ∧ + ∃ Q Rl, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s2 a σ2 m := by + have htZ : L.curW s ∈ Z.T := J.sr.curT + have ct : σ.curThread = specPtr (L.curW s) := J.j.1.sch.curThread + obtain ⟨y, hky, -⟩ := J.ep.tcb _ htZ + have P := J.faultPcA F hky + have hscW : L.mem s (scA (L.curW s)) ≠ 0#u64 := J.cur.2 + have hprog := handle_fault_sendProgA (ar := ar) s a (L.curW s) F hcap hscW + have hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM := J.toJStage3EntryA.curNotVmA + have hr : runnable y.tcbState = true := + (runnable_iffA ((J.ep.at_tcb htZ hky).st)).1 J.running + set t := L.curW s with ht_def + set s1 := faultWordsA st s t ty w0 w1 w2 with hs1 + set x1 : Tcb := { y with tcbFault := some f } with hx1 + set σ1 := updK σ (specPtr t) (.tcb x1) with hσ1 + have w1' : WritesIn st s s1 (FaultWA t) := faultWords_wrG (st := st) J.sr.core.tcbs htZ s ty w0 w1 w2 + have hta : TcbAddr t := J.sr.core.tcbs _ htZ + have SSz1 : SSzA σ σ1 := ssz_updK_tcb σ _ _ + have hkt : σ1.kheap (specPtr t) = some (.tcb x1) := by simp [hσ1] + have far : ∀ p, p ≠ specPtr t → σ1.kheap p = σ.kheap p := fun p hp => by simp [hσ1, hp] + have ne1 : specPtr p ≠ specPtr t := fun e => by rw [e, hky] at hk; cases hk + have hk1 : σ1.kheap (specPtr p) = some (.endpoint x) := by rw [far _ ne1]; exact hk + have hidle : specPtr t ≠ σ.idleThread := by + obtain ⟨z, hz, hs⟩ := J.sinv.idleTcb + intro h + rw [← h, hky] at hz + cases hz + rw [show y.tcbState = .idleThreadState from hs] at hr + exact absurd hr (by decide) + have hRun : SndRunA σ1 (specPtr t) x1 := + J.sndRunA hCs h1 (show runnable x1.tcbState = true from hr) (fun dm pr hm => by + refine J.sinv.ctQ J.act dm pr ?_ + rw [ct] + exact hm) rfl rfl hidle + have hsc : ∀ sc, x1.tcbSchedContext = some sc → ScActiveAtA σ1 sc := by + intro sc hs + obtain ⟨z, sc', ex, es, a'⟩ := (P.sv.act J.act (fun c => hidle (ct ▸ c))).1 + rw [show σ1.curThread = specPtr t from ct, hkt] at ex + cases ex + rw [hs] at es + cases es + exact a' + -- the blocking send at the faulted state + have hcm : m.curCore < fl4A64Platform.numNodes := by rw [J.j.1.sch.core]; exact hCs _ J.j.1.sch.cpuCs + have hO : SchedOpsEA st Z.T E Rp Bf (IpcSchedRelA st Z m κ) := + (ipcSchedOpsA (ar := ar) J.iplaced J.ipcPlace hcm hmb hmax htu).2.2.1 + have hF : SchedFootEA st Z.T E Rp Bf (IpcSchedRelA st Z m κ) (fun σ s => MWrA (st := st) Z m σ s) := + schedFootEA_of_sched + have hx := P.ep.at_ep hp hk1 + have bk : ∀ c z, σ1.kheap c = some (.tcb z) → ∃ z0, σ.kheap c = some (.tcb z0) ∧ z0.tcbPriority = z.tcbPriority := + fun c z hz => by + by_cases e : c = specPtr t + · rw [e, hkt] at hz; cases hz; exact ⟨y, by rw [e]; exact hky, rfl⟩ + · exact ⟨z, by rw [← far _ e]; exact hz, rfl⟩ + have hsort1 : PrioSortedA σ1 x.epQueue := + (J.ipcSorted.1 (specPtr p) ⟨p, hp, rfl⟩ x hk).imp fun {c d} h yc yd ec ed => by + obtain ⟨c0, hc0, pc⟩ := bk c yc ec + obtain ⟨d0, hd0, pd⟩ := bk d yd ed + rw [← pc, ← pd] + exact h c0 d0 hc0 hd0 + have body := send_ipc_block_fpA (st := st) (ar := ar) hO hF J.iplaced.e s1 a σ1 P.ep ⟨Z.Q, Z.R, P.sr⟩ htZ hkt hRun + hp hk1 hc (prioSorted_of_specA J.iplaced.e.tcb P.ep hx hsort1) false badge g gr true + obtain ⟨⟨s2, a2⟩, eb, ha2, hspec, E2, ⟨Q, Rl, sr2⟩, w, hz⟩ := exists_of_specA body + simp only at ha2 eb E2 sr2 w + subst ha2 + have hte : specPtr t ≠ specPtr p := fun h => ne1 h.symm + set σ2 := blkSendA σ1 (specPtr t) (specPtr p) x x1 (sndPlA badge.bv g gr false) with hσ2 + have B : BlkStepA σ1 σ2 (specPtr t) (specPtr p) x x1 (sndPlA badge.bv g gr false) := blkSendA_step hte + have hp2 := hprog eb + have hxsc : y.tcbSchedContext ≠ none := by + rw [Endp.schedTcbScA J.sr htZ hky, optScA, if_neg hscW] + simp + have hmem : ((), σ2) ∈ (sendIpc (Z := Unit) true false badge.bv g gr true (specPtr t) (specPtr p) σ1).1 := by + rw [hspec]; exact Set.mem_singleton _ + have hsp : ((), σ2) ∈ (handleFault (Z := Unit) σ.curThread f σ).1 := by + rw [ct] + exact handleFault_sendA (σ1 := σ1) hky hxsc (by rw [← ct]; exact hgc) (by rw [hσ1, hx1]) hmem + have wI : WritesIn st s1 s2 (IpcFpA st Z E Rp Bf m σ1 s1) := w.mono fun b h => by + have tw : ∀ (k : Nat), b.val = t.val + k → TWOffN k → IpcFpA st Z E Rp Bf m σ1 s1 b := fun k hv hk => + Or.inl (Or.inr (Or.inr (Or.inl ⟨t, htZ, Or.inl (twf_ofA k hv hk)⟩))) + rcases h with c | c | c | c | c | ⟨v, hv, c⟩ | c + · exact tw 3328 (by rw [c]; exact Endp.val_st hta) (by unfold TWOffN; omega) + · exact tw 3336 (by rw [c]; exact Endp.val_blk hta) (by unfold TWOffN; omega) + · exact tw 3344 (by rw [c]; exact Endp.val_badge hta) (by unfold TWOffN; omega) + · exact tw 3352 (by rw [c]; exact Endp.val_flags hta) (by unfold TWOffN; omega) + · exact Or.inl (Or.inl ⟨p, hp, c⟩) + · have hvT : v ∈ Z.T := by + rcases hv with rfl | hv + · exact htZ + · exact hv.1 + exact Or.inl (Or.inr (Or.inr (Or.inl ⟨v, hvT, Or.inr c⟩))) + · exact Or.inr c + have wF : WritesIn st s s2 (fun b => IpcFpA st Z E Rp Bf m σ s b ∨ FaultWA t b) := + (w1'.trans wI).mono fun _ h => h.elim Or.inr fun h => Or.inl (h.imp id (mwr_back rfl rfl SSz1 w1'.1)) + have blkBack : ∀ b, BlkSendFpA st Z.T (fun σ s => MWrA (st := st) Z m σ s) σ1 s1 t p b → + BlkSendFpA st Z.T (fun σ s => MWrA (st := st) Z m σ s) σ s t p b := by + intro b h + rcases h with h | h | h | h | h | ⟨v, hv, h⟩ | h + · exact Or.inl h + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr (Or.inl h)) + · exact Or.inr (Or.inr (Or.inr (Or.inl h))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inl h)))) + · refine Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨v, ?_, h⟩))))) + exact hv.imp id fun hv => ⟨hv.1, (w1'.mem_eq (field_aligned (J.sr.core.tcbs v hv.1) (K := 3336#u64) (by simp) + (by simp)) (not_faultWA hta (J.sr.core.tcbs v hv.1) (by simp) (by simp))).symm.trans hv.2⟩ + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (mwr_back rfl rfl SSz1 w1'.1 h)))))) + have wk := J.toJStage3A.ntfnWait_blkFA htZ hp ((w1'.trans w).mono fun b h => h.elim Or.inr fun h => Or.inl (blkBack b h)) + have hq1 : ∀ c ∈ x.epQueue, ∃ z, σ1.kheap c = some (.tcb z) := by + intro c hc + obtain ⟨xs, hQ, hxs, -⟩ := hx.queue + rw [hxs] at hc + obtain ⟨u, hu, rfl⟩ := List.mem_map.1 hc + obtain ⟨z, hz, -⟩ := P.ep.tcb u (hQ.mem u hu) + exact ⟨z, hz⟩ + set M : ObjRef → Prop := fun o => o = specPtr t with hMdef + have K : SIKA σ.idleThread M (fun _ => False) false σ1 σ2 := B.sika hkt hk1 rfl + have notM : ∀ c < n, ¬ M (m.idleThreadOf c) := by + intro c hc' h + obtain ⟨z, hz, hs⟩ := J.sidle.f3 c hc' + rw [show m.idleThreadOf c = specPtr t from h, hky] at hz + cases hz + rw [hs] at hr + exact absurd hr (by decide) + have hend : Sched.BodyEndA σ σ2 m := by + rw [hσ2, hσ1] + exact Sched.BodyEndA.ofBlkFaultA J ct hk _ + refine ⟨σ2, s2, hsp, hp2, hend, Q, Rl, ?_⟩ + exact J.faultTailA hA hPl hCs h1 hky hp2 (wI.mono fun _ => Or.inl) K rfl (B.specSched hkt hk1) hz + (fun h => B.qblk h hkt hk1 hq1) (fun _ _ => by rw [B.machine] <;> rfl) sr2 E2 + (fun K13 => J.toJStage3A.reply_ipcXA wF htZ K13) J.deadExt.reply wk + (fun u _ hu z hz q h => by + rw [show specPtr u = specPtr t from hu, hky] at hz + cases hz + rw [h] at hr + exact absurd hr (by simp [runnable])) + (fun u _ hu z hz q h => by + rw [show specPtr u = specPtr t from hu, B.atT] at hz + cases hz + cases h) + (fun K13 => J.toJStage3A.blkQ_blkXA htZ hp K13 ⟨_, _, B.atT⟩ ⟨_, B.atE, mem_specInsA.2 (Or.inl rfl)⟩ E2 wk) + (B.schedValid P.sv hkt hk1 hsc) notM (fun h => hidle h.symm) + (fun z hz dm pr => by rw [show z = specPtr t from hz]; exact hRun.2.1.1 dm pr) + (fun _ h => h.elim) (fun _ h => h.elim) + (fun z hz => by rw [show z = specPtr t from hz]; exact hRun.2.1.2) + (fun q hq z hz _ => by + rw [show q = specPtr t from hq, B.atT] at hz + cases hz + exact Or.inl ⟨_, _, rfl⟩) + (fun u _ hu h => by rw [specPtr_inj (show specPtr u = specPtr t from hu)] at h; exact hnvm h) + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3SendBlkSpecA.lean b/proofs/Fl4ProofsA64/P3/Stage3SendBlkSpecA.lean new file mode 100644 index 000000000..0cf65ae40 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SendBlkSpecA.lean @@ -0,0 +1,211 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2b-1): **the spec state after a blocking send** (`EpSendBlkFpA.blkSendA`): its +objects and scheduler data, and the record's spec-side facts across it: `SIKA` (movers: the sender), `SpecSchedA`, +`SchedValidA`, and `QBlkA` (the endpoint's queue stays priority sorted). + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.EpSendBlkFpA +import Fl4ProofsA64.P3.Stage3SortInsA +import Fl4ProofsA64.P3.Stage3IpcKeepSA +import Fl4ProofsA64.P3.Stage3SchedValidA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +/-- The facts of `blkSendA` (module docstring): `t` blocked, `t` inserted into the queue of `e`, the other objects +and the scheduler queues kept. -/ +structure BlkStepA (σ σ' : State Unit) (t e : ObjRef) (x : Endpoint) (xt : Tcb) (pl : SenderPayload) : Prop where + atT : σ'.kheap t = some (.tcb (blkTcbA xt e pl)) + atE : σ'.kheap e = some (.endpoint (.sendEP (specIns x.epQueue (prioOf σ) xt.tcbPriority t))) + far : ∀ p, p ≠ t → p ≠ e → σ'.kheap p = σ.kheap p + frame : σ' = { σ with kheap := σ'.kheap, schedulerAction := σ'.schedulerAction } + act : σ'.schedulerAction = σ.schedulerAction ∨ σ'.schedulerAction = .chooseNewThread + cur : σ'.schedulerAction = .resumeCurThread → t ≠ σ.curThread + +theorem blkSendA_step {σ : State Unit} {t e : ObjRef} {x : Endpoint} {xt : Tcb} {pl : SenderPayload} + (hte : t ≠ e) : BlkStepA σ (blkSendA σ t e x xt pl) t e x xt pl := by + have kh : (blkSendA σ t e x xt pl).kheap = Function.update (Function.update σ.kheap t (some (.tcb (blkTcbA xt e pl)))) e + (some (.endpoint (.sendEP (specIns x.epQueue (prioOf σ) xt.tcbPriority t)))) := by + unfold blkSendA + rw [updK_kheap, stsAct_kheap, updK_kheap] + have sa : (blkSendA σ t e x xt pl).schedulerAction = + (stsAct (updK σ t (.tcb (blkTcbA xt e pl))) t).schedulerAction := rfl + refine ⟨?_, ?_, ?_, ?_, ?_, ?_⟩ + · rw [kh, Function.update_of_ne hte, Function.update_self] + · rw [kh, Function.update_self] + · intro p h1 h2 + rw [kh, Function.update_of_ne h2, Function.update_of_ne h1] + · unfold blkSendA stsAct updK + split_ifs <;> rfl + · rw [sa] + unfold stsAct + split_ifs + · exact Or.inr rfl + · exact Or.inl rfl + · intro h ht + rw [sa] at h + unfold stsAct at h + split_ifs at h with c + refine c ⟨ht, h, ?_⟩ + have k : (updK σ t (.tcb (blkTcbA xt e pl))).kheap t = some (.tcb (blkTcbA xt e pl)) := by + rw [updK_kheap, Function.update_self] + intro hs + unfold schedulable at hs + simp only [decide_eq_true_eq] at hs + obtain ⟨h1, -, -⟩ := hs + have e : Fl4Spec.optMapSome (tcbsOf (updK σ t (.tcb (blkTcbA xt e pl)))) Tcb.tcbState t = + some (ThreadState.blockedOnSend e pl) := by simp [Fl4Spec.optMapSome, Fl4Spec.optMap, tcbsOf, tcbsOfKh, tcbOf, updK_kheap] + unfold Fl4Spec.optPred at h1 + rw [e] at h1 + simp [runnable] at h1 + +section +variable {σ σ' : State Unit} {t e : ObjRef} {x : Endpoint} {xt : Tcb} {pl : SenderPayload} + +theorem BlkStepA.curThread (S : BlkStepA σ σ' t e x xt pl) : σ'.curThread = σ.curThread := by rw [S.frame] +theorem BlkStepA.idleThread (S : BlkStepA σ σ' t e x xt pl) : σ'.idleThread = σ.idleThread := by rw [S.frame] +theorem BlkStepA.readyQueues (S : BlkStepA σ σ' t e x xt pl) : σ'.readyQueues = σ.readyQueues := by rw [S.frame] +theorem BlkStepA.releaseQueue (S : BlkStepA σ σ' t e x xt pl) : σ'.releaseQueue = σ.releaseQueue := by rw [S.frame] +theorem BlkStepA.machine (S : BlkStepA σ σ' t e x xt pl) : σ'.machineState = σ.machineState := by rw [S.frame] + +/-- Every object of the new state: the sender, the endpoint, or kept. -/ +theorem BlkStepA.cases (S : BlkStepA σ σ' t e x xt pl) {p : ObjRef} {o : KernelObject} (h : σ'.kheap p = some o) : + (p = t ∧ o = .tcb (blkTcbA xt e pl)) ∨ + (p = e ∧ o = .endpoint (.sendEP (specIns x.epQueue (prioOf σ) xt.tcbPriority t))) ∨ + (p ≠ t ∧ p ≠ e ∧ σ.kheap p = some o) := by + by_cases h1 : p = t + · subst h1; rw [S.atT] at h; cases h; exact Or.inl ⟨rfl, rfl⟩ + · by_cases h2 : p = e + · subst h2; rw [S.atE] at h; cases h; exact Or.inr (Or.inl ⟨rfl, rfl⟩) + · exact Or.inr (Or.inr ⟨h1, h2, by rw [← S.far p h1 h2]; exact h⟩) + +/-- **`SIKA`** across the blocking send: the sender is the only mover. -/ +theorem BlkStepA.sika (S : BlkStepA σ σ' t e x xt pl) (hkt : σ.kheap t = some (.tcb xt)) + (hk : σ.kheap e = some (.endpoint x)) {i : ObjRef} {M N : ObjRef → Prop} {don : Bool} (hM : M t) : + SIKA i M N don σ σ' := by + refine ⟨?_, fun p => ?_, fun d pr y h => Or.inl (by rwa [S.readyQueues] at h), + fun y h => by rwa [S.releaseQueue] at h, S.act.imp_right Or.inl, fun g => ?_⟩ + rotate_left 2 + · have ht : σ'.curTime = σ.curTime := by rw [S.frame] + have hc : σ'.consumedTime = σ.consumedTime := by rw [S.frame] + have hs : σ'.curSc = σ.curSc := by rw [S.frame] + refine ⟨fun y n h => ?_, fun hne y k hy a => ?_⟩ + · rcases S.cases h with ⟨-, c⟩ | ⟨-, c⟩ | ⟨-, -, c⟩ + · cases c + · cases c + · rw [ht]; exact g.1 y n c + · rw [hc] at hne + rw [hs] at hy + rcases S.cases hy with ⟨-, c⟩ | ⟨-, c⟩ | ⟨-, -, c⟩ + · cases c + · cases c + · show refillReady σ'.curTime _ = true + rw [ht]; exact g.2 hne y k c a + · exact S.frame.trans (by rw [S.frame]) + · by_cases h1 : p = t + · subst h1 + rw [hkt, S.atT] + exact .tcb _ _ ⟨rfl, rfl, rfl, rfl, rfl, rfl, Or.inr ⟨hM, by simp [blkTcbA]⟩, Or.inl rfl, rfl, rfl⟩ + · by_cases h2 : p = e + · subst h2 + rw [hk, S.atE] + exact .ep _ _ fun u hu _ => mem_specInsA.2 (Or.inr hu) + · rw [S.far p h1 h2] + exact .same _ + +theorem BlkStepA.specSched (S : BlkStepA σ σ' t e x xt pl) (hkt : σ.kheap t = some (.tcb xt)) + (hk : σ.kheap e = some (.endpoint x)) : SpecSchedA σ σ' := by + refine ⟨fun p => ?_, by rw [S.frame], by rw [S.frame], by rw [S.frame], by rw [S.frame], by rw [S.frame], + by rw [S.frame]⟩ + by_cases h1 : p = t + · subst h1 + exact Or.inr (Or.inl ⟨_, _, hkt, S.atT, rfl, rfl, rfl⟩) + · by_cases h2 : p = e + · subst h2 + exact Or.inr (Or.inr ⟨_, _, hk, S.atE, Or.inr (Or.inr (Or.inl ⟨_, _, rfl, rfl⟩))⟩) + · exact Or.inl (S.far p h1 h2) + +/-- **`SchedValidA`** across the blocking send: the sender's SC (if any) is active. -/ +theorem BlkStepA.schedValid (S : BlkStepA σ σ' t e x xt pl) (V : SchedValidA σ) + (hkt : σ.kheap t = some (.tcb xt)) (hk : σ.kheap e = some (.endpoint x)) + (hsc : ∀ sc, xt.tcbSchedContext = some sc → ScActiveAtA σ sc) : SchedValidA σ' := by + have act : ∀ p, ScActiveAtA σ p → ScActiveAtA σ' p := fun p ⟨y, k, h, a⟩ => by + refine ⟨y, k, ?_, a⟩ + rw [S.far p (fun c => by rw [c, hkt] at h; cases h) (fun c => by rw [c, hk] at h; cases h)] + exact h + have tcb : ∀ u y, σ.kheap u = some (.tcb y) → ∃ y', σ'.kheap u = some (.tcb y') ∧ + y'.tcbSchedContext = y.tcbSchedContext := fun u y h => by + by_cases h1 : u = t + · subst h1; rw [hkt] at h; cases h; exact ⟨_, S.atT, rfl⟩ + · refine ⟨y, ?_, rfl⟩ + rw [S.far u h1 (fun c => by rw [c, hk] at h; cases h)] + exact h + have bound : ∀ u, (∃ y sc, σ.kheap u = some (.tcb y) ∧ y.tcbSchedContext = some sc ∧ ScActiveAtA σ sc) → + ∃ y sc, σ'.kheap u = some (.tcb y) ∧ y.tcbSchedContext = some sc ∧ ScActiveAtA σ' sc := + fun u ⟨y, sc, h, es, a⟩ => by + obtain ⟨y', h', e'⟩ := tcb u y h + exact ⟨y', sc, h', e'.trans es, act _ a⟩ + refine ⟨fun d p u hm => ?_, fun ha hi => ?_, fun u y sc hy hb es => ?_, fun u hu => ?_⟩ + · rw [S.readyQueues] at hm + obtain ⟨h, n⟩ := V.rq d p u hm + exact ⟨bound u h, by rw [S.releaseQueue]; exact n⟩ + · have ha0 : σ.schedulerAction = .resumeCurThread := by + rcases S.act with c | c + · rw [← c]; exact ha + · rw [c] at ha; cases ha + rw [S.curThread, S.idleThread] at hi + obtain ⟨h, n⟩ := V.act ha0 hi + rw [S.curThread, S.releaseQueue] + exact ⟨bound _ h, n⟩ + · rcases S.cases hy with ⟨-, ho⟩ | ⟨-, ho⟩ | ⟨-, -, hy0⟩ + · cases ho + exact act _ (hsc sc es) + · cases ho + · exact act _ (V.blk u y sc hy0 hb es) + · rw [S.releaseQueue] at hu + exact bound u (V.rls u hu) + +/-- **`QBlkA`** across the blocking send (module docstring): `ipcSorted` at the new state. -/ +theorem BlkStepA.qblk (S : BlkStepA σ σ' t e x xt pl) {E N : Set ObjRef} (h : IpcSortedA E N σ) + (hkt : σ.kheap t = some (.tcb xt)) (hk : σ.kheap e = some (.endpoint x)) + (hq : ∀ a ∈ x.epQueue, ∃ y, σ.kheap a = some (.tcb y)) : QBlkA E N σ σ' := by + have hp : ∀ a y, σ.kheap a = some (.tcb y) → ∃ y', σ'.kheap a = some (.tcb y') ∧ y'.tcbPriority = y.tcbPriority := + fun a y ha => by + by_cases h1 : a = t + · subst h1; rw [hkt] at ha; cases ha; exact ⟨_, S.atT, rfl⟩ + · exact ⟨y, by rw [S.far a h1 (fun c => by rw [c, hk] at ha; cases ha)]; exact ha, rfl⟩ + have back : ∀ a y, σ'.kheap a = some (.tcb y) → ∃ y0, σ.kheap a = some (.tcb y0) ∧ y.tcbPriority = y0.tcbPriority := + fun a y ha => by + rcases S.cases ha with ⟨rfl, ho⟩ | ⟨-, ho⟩ | ⟨-, -, h0⟩ + · cases ho; exact ⟨xt, hkt, rfl⟩ + · cases ho + · exact ⟨y, h0, rfl⟩ + refine ⟨h.of_ins hkt hp back (fun p _ z hz => ?_) (fun p _ z hz => ?_), fun A p y k hy => ?_, fun p y hy => ?_, + ⟨congrArg (·.domainIndex) S.frame, congrArg (·.domainStartIndex) S.frame, congrArg (·.domainList) S.frame, congrArg (·.archState.armUsGlobalVspace) S.frame⟩⟩ + · rcases S.cases hz with ⟨-, ho⟩ | ⟨ep, ho⟩ | ⟨-, -, h0⟩ + · cases ho + · cases ho + exact ⟨x, by rw [ep]; exact hk, Or.inr ⟨rfl, hq⟩⟩ + · exact ⟨z, h0, Or.inl (List.Sublist.refl _)⟩ + · rcases S.cases hz with ⟨-, ho⟩ | ⟨-, ho⟩ | ⟨-, -, h0⟩ + · cases ho + · cases ho + · exact ⟨z, h0, Or.inl (List.Sublist.refl _)⟩ + · rcases S.cases hy with ⟨-, ho⟩ | ⟨-, ho⟩ | ⟨-, -, h0⟩ + · cases ho + · cases ho + · exact A p y k h0 + · rcases S.cases hy with ⟨-, ho⟩ | ⟨-, ho⟩ | ⟨-, -, h0⟩ + · cases ho + · cases ho + · exact ⟨y, h0, id⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3SendBudgetA.lean b/proofs/Fl4ProofsA64/P3/Stage3SendBudgetA.lean new file mode 100644 index 000000000..0c67ea872 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SendBudgetA.lean @@ -0,0 +1,165 @@ +/- +P3 (aarch64, A64-P3 Stage 3, dispatch restore): **the budget frame across a message transfer** (shared by the +woken-receiver IPC arms). + +`BKeepA` keeps the whole machine state, which the transfer's buffer stores change. `BLA σ σ'` is `BKeepA` with the +last machine time in place of the machine state: the current thread and SC, the clock, the consumed time, the idle +thread and the last machine time are kept, a resuming state came from a resuming state, and the current SC keeps its +refills and refill maximum. + +* `BKeepA.toBLA`, `PresR.blA` (every `KPresB` step), `doMachineOp_blA`, `storeWordOffs_blA`. +* `doIpcTransfer_blA`: `doIpcTransfer_genA` along `UFrA ∧ BLA` (a sender without fault and extra caps). +* **`sendIpc_recv_blA`**: every outcome of the spec's `send_ipc` to a waiting receiver is `BLA`. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3SendSpecFrA +import Fl4ProofsA64.P3.Stage3SigBudgetA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P3 Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Classical + +noncomputable section + +/-- **The budget frame up to the user memory** (module docstring). -/ +structure BLA (σ σ' : State Unit) : Prop where + ct : σ'.curThread = σ.curThread + csc : σ'.curSc = σ.curSc + time : σ'.curTime = σ.curTime + used : σ'.consumedTime = σ.consumedTime + idle : σ'.idleThread = σ.idleThread + lmt : σ'.machineState.lastMachineTime = σ.machineState.lastMachineTime + act : σ'.schedulerAction = .resumeCurThread → σ.schedulerAction = .resumeCurThread + sc : ∀ y' n, σ'.kheap σ.curSc = some (.schedContext y' n) → ∃ y, σ.kheap σ.curSc = some (.schedContext y n) ∧ + y'.scRefills = y.scRefills ∧ y'.scRefillMax = y.scRefillMax + +theorem BKeepA.toBLA {σ σ' : State Unit} (h : BKeepA σ σ') : BLA σ σ' := + ⟨h.ct, h.csc, h.time, h.used, h.idle, by rw [h.ms], h.act, h.sc⟩ + +instance : PreO BLA where + refl σ := ⟨rfl, rfl, rfl, rfl, rfl, rfl, id, fun y _ h => ⟨y, h, rfl, rfl⟩⟩ + trans h1 h2 := ⟨h2.ct.trans h1.ct, h2.csc.trans h1.csc, h2.time.trans h1.time, h2.used.trans h1.used, + h2.idle.trans h1.idle, h2.lmt.trans h1.lmt, fun h => h1.act (h2.act h), fun y'' n h => by + obtain ⟨y', h', a', b'⟩ := h2.sc y'' n (by rw [h1.csc]; exact h) + rw [h1.csc] at h' + obtain ⟨y, h0, a, b⟩ := h1.sc y' n h' + exact ⟨y, h0, a'.trans a, b'.trans b⟩⟩ + +theorem PresR.blA {α : Type} {m : SMonad Unit α} (h : KPresB m) : PresR BLA m := fun σ r hr => (h σ r hr).toBLA + +theorem PresR.andA {α : Type} {R1 R2 : State Unit → State Unit → Prop} {m : SMonad Unit α} (h1 : PresR R1 m) + (h2 : PresR R2 m) : PresR (fun a b => R1 a b ∧ R2 a b) m := fun σ r hr => ⟨h1 σ r hr, h2 σ r hr⟩ + +theorem doMachineOp_blA {α : Type} (mop : NonDet MachineState α) + (h : ∀ ms, ∀ x ∈ (mop ms).1, x.2.lastMachineTime = ms.lastMachineTime) : + PresR BLA (doMachineOp (Z := Unit) mop) := by + intro σ r hr + obtain ⟨y1, hy1, hr⟩ := hr + have e1 : y1 = (σ.machineState, σ) := hy1 + subst e1 + obtain ⟨⟨⟨a, ms'⟩, s2⟩, hy2, hr⟩ := hr + have hm : (a, ms') ∈ (mop σ.machineState).1 := hy2.1 + have e2 : s2 = σ := hy2.2 + subst e2 + obtain ⟨y3, hy3, hr⟩ := hr + have e3 : y3 = ((), { s2 with machineState := ms' }) := hy3 + subst e3 + have e4 : r = (a, { s2 with machineState := ms' }) := hr + rw [e4] + exact ⟨rfl, rfl, rfl, rfl, rfl, h _ _ hm, id, fun y _ h => ⟨y, h, rfl, rfl⟩⟩ + +theorem loadWord_lmtA (a : Word) : ∀ ms, ∀ x ∈ (loadWord a ms).1, x.2.lastMachineTime = ms.lastMachineTime := by + intro ms x hx + obtain ⟨y1, hy1, hx⟩ := hx + have e1 : y1 = (ms.underlyingMemory, ms) := hy1 + subst e1 + obtain ⟨y2, hy2, hx⟩ := hx + have e2 : y2 = ((), ms) := hy2.2 + subst e2 + rw [mem_pureA hx] + +theorem storeWord_lmtA (p w : Word) : ∀ ms, ∀ x ∈ (storeWord p w ms).1, x.2.lastMachineTime = ms.lastMachineTime := by + intro ms x hx + unfold storeWord at hx + obtain ⟨y1, hy1, hx⟩ := hx + have e1 : y1 = ((), ms) := hy1.2 + subst e1 + have e2 : x = ((), { ms with underlyingMemory := storeMem ms.underlyingMemory p w }) := hx + rw [e2] + +theorem storeWordOffs_blA (p : ObjRef) (x : Nat) (v : Word) : PresR BLA (storeWordOffs (Z := Unit) p x v) := by + unfold storeWordOffs + exact PresR.bind PresR.get fun _ => PresR.bind (PresR.assert _) fun _ => doMachineOp_blA _ (storeWord_lmtA _ _) + +section +open fl4_core_a64 Fl4ProofsA64.P1 +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- `UFrA` and `BLA`. -/ +def UBA (B : Word → Prop) (a b : State Unit) : Prop := UFrA B a b ∧ BLA a b + +instance (B : Word → Prop) : PreO (UBA B) where + refl σ := ⟨PreO.refl σ, PreO.refl σ⟩ + trans h1 h2 := ⟨PreO.trans h1.1 h2.1, PreO.trans h1.2 h2.2⟩ + +/-- **The transfer keeps the budget frame** (module docstring). -/ +theorem doIpcTransfer_blA {th : TypedHeapA} {kb : U64} (hPl : PlaceA th kb) (sf : S) {σ : State Unit} + (hR : StateRelA th (L.mem sf) σ) (hV : SlotsValidA th (L.mem sf)) {snd rcv : U64} + (htr : th rcv = some .tcb) {xs xr : Tcb} (hks : σ.kheap (specPtr snd) = some (.tcb xs)) + (hkr : σ.kheap (specPtr rcv) = some (.tcb xr)) (hf : xs.tcbFault = none) + (hal : ∀ rb, ipcBufOfA true xr = some rb → (bufAddr rb 0).toNat % 8 = 0) + (ep : Option ObjRef) (badge : Badge) (grant : Bool) + (hcaps : grant = false ∨ ipcBufOfA false xs = none ∨ + unat (dataToMessageInfo (xs.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0) : + PresRC (UBA (BufBytesA (ipcBufOfA true xr))) (· = σ) + (doIpcTransfer (Z := Unit) (specPtr snd) ep badge grant (specPtr rcv)) := + doIpcTransfer_genA (R := UBA (BufBytesA (ipcBufOfA true xr))) (fun _ _ h => h.1) + (fun t f => PresR.andA (PresR.ufrA (asUser_presA t f)) (PresR.blA (asUser_bkA t f))) + (fun a => PresR.andA (PresR.ufrA (doMachineOp_presA _ (loadWord_keepA a))) (doMachineOp_blA _ (loadWord_lmtA a))) + (rbo := ipcBufOfA true xr) (fun rb e x hx v => PresR.andA (e ▸ storeWordOffs_ufrA rb hx v) (storeWordOffs_blA rb x v)) + hPl sf hR hV htr hks hkr rfl hf hal ep badge grant hcaps + +/-- One step keeping the scheduler frame, every TCB and the budget frame, along the reachability predicate. -/ +theorem stepKBA {B : Word → Prop} {σ0 : State Unit} {α : Type} (m : SMonad Unit α) [hs : SPresInst m] + [hk : TKInst m] [hb : KPresBInst m] : + ∀ s, (TcbKeepA σ0 s ∧ UBA B σ0 s) → ∀ x ∈ (m s).1, UBA B s x.2 ∧ (TcbKeepA σ0 x.2 ∧ UBA B σ0 x.2) := + fun s h x hx => + have h1 : UBA B s x.2 := ⟨UFrA.of_fr (hs.pres s x hx), (hb.pres s x hx).toBLA⟩ + ⟨h1, PreO.trans h.1 (hk.pres s x hx), PreO.trans h.2 h1⟩ + +set_option maxHeartbeats 1000000 in +/-- **`send_ipc` to a waiting receiver keeps the budget frame** (module docstring). -/ +theorem sendIpc_recv_blA {th : TypedHeapA} {kb : U64} (hPl : PlaceA th kb) (sf : S) {σ : State Unit} + (hR : StateRelA th (L.mem sf) σ) (hV : SlotsValidA th (L.mem sf)) {t d : U64} {e : ObjRef} {q : List ObjRef} + (htr : th d = some .tcb) (hk : σ.kheap e = some (.endpoint (.recvEP (specPtr d :: q)))) + {xt xd : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) (hkd : σ.kheap (specPtr d) = some (.tcb xd)) + (hf : xt.tcbFault = none) (hal : ∀ rb, ipcBufOfA true xd = some rb → (bufAddr rb 0).toNat % 8 = 0) + (blk call : Bool) (badge : Badge) (g gr cd : Bool) + (hcaps : g = false ∨ ipcBufOfA false xt = none ∨ + unat (dataToMessageInfo (xt.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0) : + ∀ x ∈ (sendIpc (Z := Unit) blk call badge g gr cd (specPtr t) e σ).1, BLA σ x.2 := by + suffices H : PresRC (UBA (BufBytesA (ipcBufOfA true xd))) (· = σ) + (sendIpc (Z := Unit) blk call badge g gr cd (specPtr t) e) from fun x hx => (H σ rfl x hx).2 + unfold sendIpc + refine PresRC.bind_oneA (getEndpoint_eq hk) ?_ + refine PresRC.monoPA (P' := fun s => TcbKeepA σ s ∧ UBA (BufBytesA (ipcBufOfA true xd)) σ s) + (fun s h => h ▸ ⟨fun _ _ h => h, PreO.refl s⟩) ?_ + cases blk <;> dsimp only + all_goals + refine PresRC.bind_inv (stepKBA _) fun _ => ?_ + refine PresRC.bind_inv (stepKBA _) fun recvState => ?_ + cases recvState + case blockedOnReceive a r dd => + dsimp only + refine PresRC.bind_pureA r ?_ + refine PresRC.bind_last (fun s hs x hx => ?_) (fun _ => PresR.andA (PresR.ufrA (by spres)) (PresR.blA (by kbres))) + exact doIpcTransfer_blA hPl sf (hR.specSchedA hs.2.1.1) hV htr (hs.1 _ _ hkt) (hs.1 _ _ hkd) hf hal _ _ _ hcaps + s rfl x hx + all_goals (dsimp only; exact PresRC.bind_fail) + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3SendDonIrrelA.lean b/proofs/Fl4ProofsA64/P3/Stage3SendDonIrrelA.lean new file mode 100644 index 000000000..2e0aa7d0e --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SendDonIrrelA.lean @@ -0,0 +1,263 @@ +/- +P3 (aarch64, A64-P3 line RecvPreA): **`can_donate` is irrelevant for a plain send to a receiver with its own SC.** +The first phase of `NBSendRecv` / `NBSendWait` sends with `can_donate = true`; these are the `can_donate = true` +siblings of `EpSendRecvA.send_ipc_recvA` / `EpSendFpA.send_ipc_recv_fpA` for a non-call, fault-free sender. + +* `sendIpc_recv_runDonA`: the spec's outcome of a plain send, any `canDonate`, receiver with an SC. +* **`send_ipc_recvDonA`**: FL4 refines it; also the spec programs with and without donation are equal. +* `send_ipc_recv_fpDonA`: the narrow write set. + +Case hypotheses: the receiver has an SC (`hdsc`) and the state relation ties it to the SC word (`hscw`). New module. +-/ +import Fl4ProofsA64.P3.EpSendFpA +import Fl4ProofsA64.P3.Stage3IpcKeepSA +import Fl4ProofsA64.P3.Stage3IpcSchedValidA +open Aeneas Aeneas.Std Result +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched + +noncomputable section + +section Spec +open Fl4SpecA64 Fl4Spec + +theorem sendIpc_recv_runDonA {σ : State Unit} {t e d : ObjRef} {q : List ObjRef} + (hk : σ.kheap e = some (.endpoint (.recvEP (d :: q)))) {xd : Tcb} (hkd : σ.kheap d = some (.tcb xd)) + (hde : d ≠ e) {e' : ObjRef} {rep : Option ObjRef} {dd : ReceiverPayload} + (hst : xd.tcbState = .blockedOnReceive e' rep dd) (blk : Bool) (badge : Badge) (g gr cd : Bool) + {σ2 σ3 : State Unit} + (hT : doIpcTransfer (Z := Unit) t (some e) badge g d (updK σ e (.endpoint (epDeq (.recvEP (d :: q)) d))) = + ({((), σ2)}, False)) + (hU : NonDet.maybeM (replyUnlinkTcb (Z := Unit) d) rep σ2 = ({((), σ3)}, False)) + {xt3 x3 : Tcb} (hkt3 : σ3.kheap t = some (.tcb xt3)) (hf3 : xt3.tcbFault = none) + (hkd3 : σ3.kheap d = some (.tcb x3)) (hsc : x3.tcbSchedContext ≠ none) : + sendIpc (Z := Unit) blk false badge g gr cd t e σ = + specSendTailA d (stsAct (updK σ3 d (.tcb { x3 with tcbState := .running })) d) := by + have hkd1 : (updK σ e (.endpoint (epDeq (.recvEP (d :: q)) d))).kheap d = some (.tcb xd) := by + rw [updK_kheap, Function.update_of_ne hde, hkd] + unfold sendIpc + rw [nd_bind_one (getEndpoint_eq hk)] + cases blk <;> dsimp only <;> + · rw [nd_bind_one (tcbEpDequeue_eq hk (by simp [Endpoint.epQueue])), nd_bind_one (getThreadState_eq hkd1), hst] + dsimp only + rw [nd_pure_bind', nd_bind_one hT, nd_bind_one hU, nd_bind_one (getTcbObjRef_eq _ hkd3), + nd_bind_one (threadGet_eq _ hkt3), hf3] + simp only [Bool.false_eq_true, ne_eq, not_true_eq_false, or_self, ↓reduceIte, false_and] + rw [nd_when_false_bindA (by simp [hsc]), nd_bind_one (setThreadState_eq _ hkd3)] + rfl + +end Spec + +section Keep +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +open Fl4SpecA64 Fl4Spec in +/-- The receiver keeps its SC across the dequeue, the transfer and the reply unlink. -/ +theorem recvScKeepA {th : TypedHeapA} {kb : U64} (hPl : PlaceA th kb) (sf : S) {σ : State Unit} + (hR : StateRelA th (L.mem sf) σ) (hV : SlotsValidA th (L.mem sf)) {t d : U64} (hdh : th d = some .tcb) + {e : ObjRef} {q : List ObjRef} (hk : σ.kheap e = some (.endpoint (.recvEP (specPtr d :: q)))) + {xt xd : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) (hkd : σ.kheap (specPtr d) = some (.tcb xd)) + (hft : xt.tcbFault = none) (hal : ∀ rb, ipcBufOfA true xd = some rb → (bufAddr rb 0).toNat % 8 = 0) + (badge : Badge) (g : Bool) + (hcaps : g = false ∨ ipcBufOfA false xt = none ∨ + unat (dataToMessageInfo (xt.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0) + {σ2 σ3 : State Unit} {rep : Option ObjRef} + (hT : doIpcTransfer (Z := Unit) (specPtr t) (some e) badge g (specPtr d) + (updK σ e (.endpoint (epDeq (.recvEP (specPtr d :: q)) (specPtr d)))) = ({((), σ2)}, False)) + (hU : NonDet.maybeM (replyUnlinkTcb (Z := Unit) (specPtr d)) rep σ2 = ({((), σ3)}, False)) + {x3 : Tcb} (hkd3 : σ3.kheap (specPtr d) = some (.tcb x3)) (hsc : xd.tcbSchedContext ≠ none) : + x3.tcbSchedContext ≠ none := by + have hde : specPtr d ≠ e := fun h => by rw [h, hk] at hkd; cases hkd + have hte : specPtr t ≠ e := fun h => by rw [h, hk] at hkt; cases hkt + have hdq := tcbEpDequeue_eq hk (t := specPtr d) (by simp [Endpoint.epQueue]) + have hSF : SchedFrameA σ (updK σ e (.endpoint (epDeq (.recvEP (specPtr d :: q)) (specPtr d)))) := + SPresInst.pres (m := tcbEpDequeue (Z := Unit) (specPtr d) e) σ ((), _) (by rw [hdq]; exact Set.mem_singleton _) + have hks1 : (updK σ e (.endpoint (epDeq (.recvEP (specPtr d :: q)) (specPtr d)))).kheap (specPtr t) = + some (.tcb xt) := by rw [updK_kheap, Function.update_of_ne hte, hkt] + have hkd1 : (updK σ e (.endpoint (epDeq (.recvEP (specPtr d :: q)) (specPtr d)))).kheap (specPtr d) = + some (.tcb xd) := by rw [updK_kheap, Function.update_of_ne hde, hkd] + have k12 := (doIpcTransfer_tkA hPl sf (hR.specSchedA hSF.1) hV hdh hks1 hkd1 hft hal (some e) badge g hcaps + ((), σ2) (by rw [hT]; exact Set.mem_singleton _)).1 + obtain ⟨-, -, -, -, -, -, f12, -⟩ := k12 + obtain ⟨y2, hy2, e2⟩ := f12 _ xd hkd1 + change σ2.kheap (specPtr d) = some (.tcb y2) at hy2 + cases rep with + | none => + have h : (((), σ3) : Unit × State Unit) = ((), σ2) := by + have : ((), σ3) ∈ (NonDet.maybeM (replyUnlinkTcb (Z := Unit) (specPtr d)) none σ2).1 := by + rw [hU]; exact Set.mem_singleton _ + exact this + rw [(Prod.mk.inj h).2, hy2] at hkd3 + obtain rfl := KernelObject.tcb.inj (Option.some.inj hkd3) + rw [e2]; exact hsc + | some r => + have K : SIKA σ2.idleThread (· = specPtr d) (· = specPtr d) false σ2 σ3 := + replyUnlinkTcb_sikA (i := σ2.idleThread) (M := (· = specPtr d)) (N := (· = specPtr d)) (don := false) + (specPtr d) r rfl σ2 ((), σ3) (by + have : ((), σ3) ∈ (NonDet.maybeM (replyUnlinkTcb (Z := Unit) (specPtr d)) (some r) σ2).1 := by + rw [hU]; exact Set.mem_singleton _ + exact this) + have o := K.obj (specPtr d) + rw [hy2, hkd3] at o + cases o with + | same => rw [e2]; exact hsc + | tcb _ _ g' => + rcases g'.sc with c | c + · rw [c, e2]; exact hsc + · exact absurd c.1 (by simp) + +end Keep + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] {T E R : Set U64} + {Bf : Set (U64 × Fl4SpecA64.Word)} {SR : S → Fl4SpecA64.State Unit → Prop} + {Wr : Fl4SpecA64.State Unit → S → U64 → Prop} + + +open Fl4SpecA64 Fl4Spec in +/-- **A plain send with donation allowed to a receiver with its own SC** (module docstring). -/ +theorem send_ipc_recvDonA (hO : SchedOpsEA st T E R Bf SR) (hF : SchedFootEA st T E R Bf SR Wr) + (hP : EPlaced T E R Bf) {th : TypedHeapA} {kb : U64} (hPl : PlaceA th kb) + (s : S) (a : A) (σ : State Unit) (hR : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) + (hSt : StateRelA th (L.mem s) σ) (hV : SlotsValidA th (L.mem s)) (hC : BufCover st T Bf s σ) + (hNative : BufNativeA T (L.mem s)) + (hoff : ∀ y, IsSlotA th y → ¬ EWrite T E R Bf y ∧ ¬ EWrite T E R Bf (core.num.U64.wrapping_add y 8#u64)) + {t : U64} (ht : t ∈ T) (hth : th t = some .tcb) {xt : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hnb : tsCode xt.tcbState ≠ 3) + {e d : U64} (he : e ∈ E) {q : List ObjRef} (hk : σ.kheap (specPtr e) = some (.endpoint (.recvEP (specPtr d :: q)))) + (hdh : th d = some .tcb) {xd : Tcb} (hkd : σ.kheap (specPtr d) = some (.tcb xd)) + (hu : ∀ rp, ipcBufOfA true xd = some rp → ∀ k, k < 128 → inUserFrame (bufAddr rp k) σ) + (hrep : RepOKA σ (specPtr d)) (hon : L.mem s (affA d) = L.cpu s) + (badge : U64) (g gr : Bool) (blk : Bool) + (hcase : (xt.tcbFault = none ∧ (g = false ∨ ipcBufOfA false xt = none ∨ + unat (dataToMessageInfo (xt.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0)) ∨ + ∃ f, xt.tcbFault = some f ∧ FaultOKA (L := L) s t (ipcBufOfA true xd) xt f) + (hft : xt.tcbFault = none) (hdsc : xd.tcbSchedContext ≠ none) + (hal : ∀ rb, ipcBufOfA true xd = some rb → (bufAddr rb 0).toNat % 8 = 0) + (hscw : ∀ s' σ', SR s' σ' → ∀ y : Tcb, σ'.kheap (specPtr d) = some (.tcb y) → y.tcbSchedContext ≠ none → + L.mem s' (scA d) ≠ 0#u64) : + endpoint.send_ipc st ar s a blk false badge g gr true t e ⦃ r => r.2 = a ∧ + ¬ (sendIpc (Z := Unit) blk false badge.bv g gr true (specPtr t) (specPtr e) σ).2 ∧ + sendIpc (Z := Unit) blk false badge.bv g gr true (specPtr t) (specPtr e) σ = + sendIpc (Z := Unit) blk false badge.bv g gr false (specPtr t) (specPtr e) σ ∧ + ∃ σ', ((), σ') ∈ (sendIpc (Z := Unit) blk false badge.bv g gr true (specPtr t) (specPtr e) σ).1 ∧ + EpStateRel T E R Bf (L.mem r.1) σ' ∧ SR r.1 σ' ∧ FpA st T E R Bf Wr σ s r.1 ⦄ := by + refine send_ipc_recvContA hO hP hPl s a σ hR hSR hSt hV hC hNative hoff ht hth hkt hnb + he hk hdh hkd hu hrep badge g gr blk false true hcase ?_ + intro s1 s2 σ2 x2 rep dd σ1 hdT htd hde hS1 hR2 hS2 hWf hE hW2 hE2 hfr2 hkt2 hkd1 hkd2 hstd hst2' hrr hq2 hT _ + have htd' : specPtr t ≠ specPtr d := fun h => htd (specPtr_inj h) + have hK2 := SchedKeepA.step hE + unfold sendRecvAfterTransferA sendRecvAfterUnlinkA + -- the reply word and the unlink + step with obj.Tcb.reply_obj.spec as ⟨i, hi⟩ + have hi0 : rep = none → i = 0#u64 := fun h => by + rw [hi]; exact optRef_none (((hR2.at_tcb hdT hkd2).recv _ _ _ hst2').1.trans h) + refine WP.spec_bind (send_recv_replyA hO.to0 hF.to0 hP hS1 hR2 hS2 hW2 hE2 hdT hfr2 htd' hkt2 hkd1 hkd2 hstd hst2' hrr hq2 i hi) + ?_ + rintro s3 ⟨⟨σ3, x3, hR3, hS3, hU, hkt3, hkd3, hnr3, hni3, hcls3, hq3, hK3, hz3⟩, hfp3⟩ + have hzσ3 : SSzA σ σ3 := (ssz_of_epStep hE).trans hz3 + have hpre3 := FpA.trans hF (FpA.of_ew hWf) (ssz_of_epStep hE) hfp3 + have hxt3 := hR3.at_tcb ht hkt3 + -- the receiver keeps its SC through the transfer and the reply unlink + have hsc3 : x3.tcbSchedContext ≠ none := + recvScKeepA hPl s hSt hV hdh hk hkt hkd hft hal badge.bv g + (hcase.resolve_right (by rintro ⟨f, hf, -⟩; rw [hft] at hf; cases hf)).2 hT hU hkd3 hdsc + simp only [Bool.false_eq_true, ↓reduceIte] + step with obj.Tcb.fault_type.spec as ⟨i1, hi1⟩ + have hf0 : i1 = fl4_abi.consts.fault.NullFault := by + rw [hi1, hxt3.fault0.2 hft]; unfold fl4_abi.consts.fault.NullFault; rfl + simp only [hf0, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + step with obj.Tcb.sc.spec as ⟨i2, hi2⟩ + have hi20 : i2 ≠ 0#u64 := by rw [hi2]; exact hscw s3 σ3 hS3 x3 hkd3 hsc3 + rw [if_neg hi20] + apply ok_bind_specA + -- the receiver runs + refine WP.spec_bind (set_thread_state_relF hO hF hP s3 σ3 hR3 hS3 hdT hkd3 hq3 .running (by decide) + sched.TS_RUNNING (by unfold sched.TS_RUNNING; rfl) (NoBlockE.updT hkd3 _ (Or.inr (Or.inl ⟨rfl, hni3, hcls3⟩)))) + ?_ + rintro s5 ⟨hR5, hS5, hfp5⟩ + have eqD := sendIpc_recv_runDonA hk hkd hde hstd blk badge.bv g gr true hT hU hkt3 hft hkd3 hsc3 + have eqF := sendIpc_recv_runDonA hk hkd hde hstd blk badge.bv g gr false hT hU hkt3 hft hkd3 hsc3 + rw [eqF, eqD] + refine WP.spec_mono (send_tail_fromA hO hF hP a hSR hdT hon hS3 hkd3 hnr3 hq3 hR5 hS5 (FpA.trans hF hpre3 hzσ3 hfp5) + (hzσ3.trans ((ssz_updK_tcb _ _ _).trans (ssz_stsAct _ _)))) fun r h => ⟨h.1, h.2.1, rfl, h.2.2⟩ + +open Fl4SpecA64 Fl4Spec in +/-- **Its narrow write set** (module docstring). -/ +theorem send_ipc_recv_fpDonA (hO : SchedOpsEA st T E R Bf SR) (hF : SchedFootEA st T E R Bf SR Wr) + (hP : EPlaced T E R Bf) {th : TypedHeapA} {kb : U64} (hPl : PlaceA th kb) + (s : S) (a : A) (σ : State Unit) (hR : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) + (hSt : StateRelA th (L.mem s) σ) (hV : SlotsValidA th (L.mem s)) (hC : BufCover st T Bf s σ) + (hNative : BufNativeA T (L.mem s)) + (hoff : ∀ y, IsSlotA th y → ¬ EWrite T E R Bf y ∧ ¬ EWrite T E R Bf (core.num.U64.wrapping_add y 8#u64)) + {t : U64} (ht : t ∈ T) (hth : th t = some .tcb) {xt : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hnb : tsCode xt.tcbState ≠ 3) + {e d : U64} (he : e ∈ E) {q : List ObjRef} (hk : σ.kheap (specPtr e) = some (.endpoint (.recvEP (specPtr d :: q)))) + (hdh : th d = some .tcb) {xd : Tcb} (hkd : σ.kheap (specPtr d) = some (.tcb xd)) + (hu : ∀ rp, ipcBufOfA true xd = some rp → ∀ k, k < 128 → inUserFrame (bufAddr rp k) σ) + (hrep : RepOKA σ (specPtr d)) (hon : L.mem s (affA d) = L.cpu s) + (badge : U64) (g gr : Bool) (blk : Bool) + (hcase : (xt.tcbFault = none ∧ (g = false ∨ ipcBufOfA false xt = none ∨ + unat (dataToMessageInfo (xt.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0)) ∨ + ∃ f, xt.tcbFault = some f ∧ FaultOKA (L := L) s t (ipcBufOfA true xd) xt f) + (hft : xt.tcbFault = none) (hdsc : xd.tcbSchedContext ≠ none) + (hal : ∀ rb, ipcBufOfA true xd = some rb → (bufAddr rb 0).toNat % 8 = 0) + (hscw : ∀ s' σ', SR s' σ' → ∀ y : Tcb, σ'.kheap (specPtr d) = some (.tcb y) → y.tcbSchedContext ≠ none → + L.mem s' (scA d) ≠ 0#u64) : + endpoint.send_ipc st ar s a blk false badge g gr true t e + ⦃ r => WritesIn st s r.1 (SendFpA st T R Bf Wr σ s t e d False) ⦄ := by + refine send_ipc_recvContA hO hP hPl s a σ hR hSR hSt hV hC hNative hoff ht hth hkt hnb + he hk hdh hkd hu hrep badge g gr blk false true hcase ?_ + intro s1 s2 σ2 x2 rep dd σ1 hdT htd hde hS1 hR2 hS2 hWf hE hW2 hE2 hfr2 hkt2 hkd1 hkd2 hstd hst2' hrr hq2 hT hfp + have htd' : specPtr t ≠ specPtr d := fun h => htd (specPtr_inj h) + have hK2 := SchedKeepA.step hE + have h2 : WritesIn st s s2 (SendFpA st T R Bf Wr σ s t e d False) := hfp.mono fun _ h => Or.inl h + have hz2 : SSzA σ σ2 := ssz_of_epStep hE + unfold sendRecvAfterTransferA sendRecvAfterUnlinkA + step with obj.Tcb.reply_obj.spec as ⟨i, hi⟩ + have hi0 : rep = none → i = 0#u64 := fun h => by + rw [hi]; exact optRef_none (((hR2.at_tcb hdT hkd2).recv _ _ _ hst2').1.trans h) + refine WP.spec_bind (WP.spec_and + (send_recv_replyA hO.to0 hF.to0 hP hS1 hR2 hS2 hW2 hE2 hdT hfr2 htd' hkt2 hkd1 hkd2 hstd hst2' hrr hq2 i hi) + (send_recv_reply_fpA hO.to0 hF.to0 hP hS1 hR2 hW2 hE2 hdT hfr2 htd' hkd1 hkd2 hstd hst2' hrr hq2 i hi)) ?_ + rintro s3 ⟨⟨⟨σ3, x3, hR3, hS3, hU, hkt3, hkd3, hnr3, hni3, hcls3, hq3, hK3, hz3⟩, -⟩, hw3⟩ + have hzσ3 : SSzA σ σ3 := (ssz_of_epStep hE).trans hz3 + have h3 : WritesIn st s s3 (SendFpA st T R Bf Wr σ s t e d False) := + WritesIn.schedA hF h2 hz2 (B := fun b => (∃ r ∈ R, b = rTcbA r) ∨ b = stA d) + (hw3.mono fun _ h => or_assoc.mpr h) + (fun _ h => h.elim (fun h => Or.inr (Or.inl h)) fun h => Or.inr (Or.inr (Or.inr (Or.inl h)))) + (fun _ h => Or.inr (Or.inr (Or.inr (Or.inr h)))) + have hxt3 := hR3.at_tcb ht hkt3 + -- the receiver keeps its SC through the transfer and the reply unlink + have hsc3 : x3.tcbSchedContext ≠ none := + recvScKeepA hPl s hSt hV hdh hk hkt hkd hft hal badge.bv g + (hcase.resolve_right (by rintro ⟨f, hf, -⟩; rw [hft] at hf; cases hf)).2 hT hU hkd3 hdsc + simp only [Bool.false_eq_true, ↓reduceIte] + step with obj.Tcb.fault_type.spec as ⟨i1, hi1⟩ + have hf0 : i1 = fl4_abi.consts.fault.NullFault := by + rw [hi1, hxt3.fault0.2 hft]; unfold fl4_abi.consts.fault.NullFault; rfl + simp only [hf0, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + step with obj.Tcb.sc.spec as ⟨i2, hi2⟩ + have hi20 : i2 ≠ 0#u64 := by rw [hi2]; exact hscw s3 σ3 hS3 x3 hkd3 hsc3 + rw [if_neg hi20] + apply ok_bind_specA + have hNB := NoBlockE.updT (σ := σ3) hkd3 { x3 with tcbState := .running } (Or.inr (Or.inl ⟨rfl, hni3, hcls3⟩)) + refine WP.spec_bind (WP.spec_and + (set_thread_state_relF hO hF hP s3 σ3 hR3 hS3 hdT hkd3 hq3 .running (by decide) + sched.TS_RUNNING (by unfold sched.TS_RUNNING; rfl) hNB) + (set_thread_state_fpA hO.to0 hF.to0 hP hS3 s3 σ3 hR3 (WritesIn.refl _ _) (EpStep.refl _) hdT hkd3 hq3 .running + (by decide) sched.TS_RUNNING (by unfold sched.TS_RUNNING; rfl) hNB)) ?_ + rintro s5 ⟨⟨hR5, hS5, -⟩, hw5⟩ + have h5 : WritesIn st s s5 (SendFpA st T R Bf Wr σ s t e d False) := + WritesIn.schedA hF h3 hzσ3 hw5 (fun _ h => Or.inr (Or.inr (Or.inr (Or.inl h)))) + (fun _ h => Or.inr (Or.inr (Or.inr (Or.inr h)))) + refine WP.spec_mono (send_tail_fpA hO hF a hSR hdT hon hS3 hkd3 hnr3 hq3 hR5 hS5) fun r hr => ?_ + exact WritesIn.schedA hF h5 (hzσ3.trans ((ssz_updK_tcb _ _ _).trans (ssz_stsAct _ _))) + (B := fun _ => False) (hr.mono fun _ h => Or.inr h) (fun _ h => h.elim) + (fun _ h => Or.inr (Or.inr (Or.inr (Or.inr h)))) + +end + +end + +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/Stage3SendDonPlainA.lean b/proofs/Fl4ProofsA64/P3/Stage3SendDonPlainA.lean new file mode 100644 index 000000000..675abb449 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SendDonPlainA.lean @@ -0,0 +1,491 @@ +/- +P3 (aarch64, A64-P3 line RecvPreA): **a plain send that donates its SC to a passive receiver**, the program and spec +facts (the donating sibling of `send_ipc_recv_pushDonate_fpA` for a non-call send, `can_donate = true`, a +fault-free sender). + +* `sendIpc_recv_donSpecA`: the spec's outcome after the transfer and the reply unlink: the donation, the receiver + runs, the tail. +* `donTailFpA`: FL4's `schedcontext::donate` of the sender's SC (`sc_donate_fromA`) with the endpoint frames, the + reply stacks (unchanged) and `MWrA`. +* **`send_ipc_recv_donPlain_fpA`**: FL4's `send_ipc` refines the spec send; write set `SendFpA … False`. +* `relIfBoundSc_donPlainA`: the receiver's `RelIfBoundSc` after the donation (the body's `hReleased`). + +[S] The receiver has no SC (a reply object is optional); the sender is on the current core with no bound VCPU and the +receiver has the SC's affinity. No named premise. New module. +-/ +import Fl4ProofsA64.P3.IpcScSendPushA +import Fl4ProofsA64.P3.Stage3SendDonateKeepA +import Fl4ProofsA64.P3.Stage3SendDonIrrelA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3 Fl4SpecA64 Sched + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section Spec +open Fl4Spec + +/-- The nondeterministic monad's bind is associative. -/ +theorem nd_bind_assocA {σ α β γ : Type} (m : NonDet σ α) (g : α → NonDet σ β) (f : β → NonDet σ γ) (s : σ) : + ((m >>= g) >>= f) s = (m >>= fun x => g x >>= f) s := by + refine Prod.ext (Set.ext fun r => ?_) (propext ?_) + · constructor + · rintro ⟨y, ⟨x, hx, hy⟩, hr⟩ + exact ⟨x, hx, y, hy, hr⟩ + · rintro ⟨x, hx, y, hy, hr⟩ + exact ⟨y, ⟨x, hx, hy⟩, hr⟩ + · constructor + · rintro ((h | ⟨x, hx, h⟩) | ⟨y, ⟨x, hx, hy⟩, h⟩) + · exact Or.inl h + · exact Or.inr ⟨x, hx, Or.inl h⟩ + · exact Or.inr ⟨x, hx, Or.inr ⟨y, hy, h⟩⟩ + · rintro (h | ⟨x, hx, h | ⟨y, hy, h⟩⟩) + · exact Or.inl (Or.inl h) + · exact Or.inl (Or.inr ⟨x, hx, h⟩) + · exact Or.inr ⟨y, ⟨x, hx, hy⟩, h⟩ + +/-- **The spec's plain send that donates** from the transfer's and the unlink's single outcomes. -/ +theorem sendIpc_recv_donSpecA {σ : State Unit} {t e d sc : ObjRef} {q : List ObjRef} + (hk : σ.kheap e = some (.endpoint (.recvEP (d :: q)))) {xd : Tcb} (hkd : σ.kheap d = some (.tcb xd)) + (hde : d ≠ e) {e' : ObjRef} {dd : ReceiverPayload} + {rep : Option ObjRef} (hst : xd.tcbState = .blockedOnReceive e' rep dd) + (blk : Bool) (badge : Badge) (g gr : Bool) {σ2 σ3 : State Unit} + (hT : doIpcTransfer (Z := Unit) t (some e) badge g d (updK σ e (.endpoint (epDeq (.recvEP (d :: q)) d))) = + ({((), σ2)}, False)) + (hU : NonDet.maybeM (replyUnlinkTcb (Z := Unit) d) rep σ2 = ({((), σ3)}, False)) + {xt3 x3 : Tcb} (hkt3 : σ3.kheap t = some (.tcb xt3)) (hkd3 : σ3.kheap d = some (.tcb x3)) + (hf3 : xt3.tcbFault = none) (hsct : xt3.tcbSchedContext = some sc) (hsc : x3.tcbSchedContext = none) : + sendIpc (Z := Unit) blk false badge g gr true t e σ = + (do + schedContextDonate sc d + setThreadState d .running + specSendTailA d) σ3 := by + have hkd1 : (updK σ e (.endpoint (epDeq (.recvEP (d :: q)) d))).kheap d = some (.tcb xd) := by + rw [updK_kheap, Function.update_of_ne hde, hkd] + unfold sendIpc + rw [nd_bind_one (getEndpoint_eq hk)] + cases blk <;> dsimp only <;> + · rw [nd_bind_one (tcbEpDequeue_eq hk (by simp [Endpoint.epQueue])), nd_bind_one (getThreadState_eq hkd1), hst] + dsimp only + rw [nd_pure_bind'] + change _ = _ + rw [nd_bind_one hT] + change (NonDet.maybeM (replyUnlinkTcb d) rep >>= _) σ2 = _ + rw [nd_bind_one hU, nd_bind_one (getTcbObjRef_eq _ hkd3), nd_bind_one (threadGet_eq _ hkt3), hf3] + simp only [Bool.false_eq_true, ne_eq, not_true_eq_false, or_self, ↓reduceIte, false_and] + rw [nd_when_true_bindA (by simp [hsc])] + rw [nd_bind_assocA, nd_bind_one (getTcbObjRef_eq _ hkt3), hsct] + rfl + + +theorem donationFromNone {σ σ' : State Unit} {p f t : ObjRef} {y : SchedContext} {k : Nat} {xf xt : Tcb} + (hp : σ.kheap p = some (.schedContext y k)) (hy : y.scTcb = some f) + (hf : σ.kheap f = some (.tcb xf)) (ht : σ.kheap t = some (.tcb xt)) (hft : f ≠ t) + (hdom : xf.tcbDomain = 0) (hnq : NotQueued σ f) + (h : ((), σ') ∈ (schedContextDonate (Z := Unit) p t σ).1) : + σ'.kheap f = some (.tcb { xf with tcbSchedContext := none }) := by + have hget : getScObjRef (Z := Unit) SchedContext.scTcb p σ = ({(some f, σ)}, False) := by + unfold getScObjRef + rw [nd_bind_oneSc (getSchedContext_eqA hp), hy] + rfl + have hs : tcbSchedAction (Z := Unit) tcbSchedDequeue f σ = ({((), σ)}, False) := by + rw [tcbSchedAction_eqA hf hdom] + have hfilt : tcbSchedDequeue f (σ.readyQueues 0 xf.tcbPriority) = σ.readyQueues 0 xf.tcbPriority := by + unfold tcbSchedDequeue + exact List.filter_eq_self.mpr fun a ha => by + have hne : f ≠ a := fun e => hnq.1 0 xf.tcbPriority (e ▸ ha) + simpa using hne + rw [hfilt, setRqA_selfA] + unfold schedContextDonate at h + rw [nd_bind_oneSc hget] at h + simp only [NonDet.when] at h + rw [if_pos (show (some f : Option ObjRef) ≠ none by simp)] at h + have hpre : (do + let f' ← NonDet.assertOpt (σ := State Unit) (some f) + tcbSchedAction tcbSchedDequeue f' + tcbReleaseRemove f' + setTcbObjRef tcbSchedContextUpdate f' none + testReschedule f' : SMonad Unit Unit) σ = + testReschedule (Z := Unit) f (unbTStA σ f xf) := by + rw [nd_bind_oneSc (show (NonDet.assertOpt (σ := State Unit) (some f)) σ = ({(f, σ)}, False) from rfl)] + rw [nd_bind_oneSc hs, nd_bind_oneSc (tcbReleaseRemove_notA hnq.2)] + exact nd_bind_oneSc (threadSet_eqSc _ hf) _ + change ∃ z ∈ ((do + let f' ← NonDet.assertOpt (σ := State Unit) (some f) + tcbSchedAction tcbSchedDequeue f' + tcbReleaseRemove f' + setTcbObjRef tcbSchedContextUpdate f' none + testReschedule f' : SMonad Unit Unit) σ).1, + ((), σ') ∈ ((do + setScObjRef scTcbUpdate p (some t) + setTcbObjRef tcbSchedContextUpdate t (some p) : SMonad Unit Unit) z.2).1 at h + rw [hpre] at h + obtain ⟨⟨_, s⟩, hres, hs'⟩ := h + have hsk := testRescheduleKeepsHeap _ _ _ _ hres + have old (q : ObjRef) (hq : q ≠ f) : s.kheap q = σ.kheap q := by + rw [hsk, unbTStA_kheap, if_neg hq] + have poff : p ≠ f := by intro e; rw [e, hf] at hp; cases hp + have ptoff : p ≠ t := by intro e; rw [e, ht] at hp; cases hp + have hps := (old p poff).trans hp + have hts : (updKSc s p (.schedContext { y with scTcb := some t } k)).kheap t = some (.tcb xt) := by + rw [updKSc_kheap, Function.update_of_ne ptoff.symm, old t hft.symm, ht] + change _ ∈ ((updateSchedContext p _ >>= _) s).1 at hs' + rw [nd_bind_oneSc (updateSchedContext_eqA hps _)] at hs' + change _ ∈ (threadSet _ t (updKSc s p (.schedContext { y with scTcb := some t } k))).1 at hs' + rw [threadSet_eqSc _ hts] at hs' + have heq : σ' = updKSc (updKSc s p (.schedContext { y with scTcb := some t } k)) + t (.tcb { xt with tcbSchedContext := some p }) := + congrArg Prod.snd (Set.mem_singleton_iff.mp hs') + rw [heq, updKSc_kheap, Function.update_of_ne hft, updKSc_kheap, Function.update_of_ne (Ne.symm poff), hsk, + unbTStA_kheap, if_pos rfl] + + + +/-- The receiver's wakeup after a donation keeps another thread's SC binding. -/ +theorem donTail_scIs {t d : ObjRef} (htd : t ≠ d) (v : Option ObjRef) : + HT (fun σ => ScIs t v σ) (do setThreadState (Z := Unit) d .running + specSendTailA d) (fun _ σ => ScIs t v σ) := by + unfold specSendTailA + refine HT.bind (Q := fun _ σ => ScIs t v σ) ?_ fun _ => ?_ + · unfold Fl4SpecA64.setThreadState + refine HT.bind (Q := fun _ σ => ScIs t v σ) (fun σ h r hr y hy => ?_) fun _ σ h r hr => ?_ + · obtain ⟨x, -, e⟩ := threadSet_memA _ d hr + rw [e] at hy + exact h y (by simpa only [Function.update_of_ne htd] using hy) + · rw [setThreadStateAct_eq] at hr + have e : r = ((), stsAct σ d) := hr + rw [e] + exact fun y hy => h y (by rwa [stsAct_kheap] at hy) + refine HT.bind (Q := fun _ σ => ScIs t v σ) (fun σ h r hr => by + have e : r.2 = σ := ((threadGet_eqPres _ _) σ r hr).symm + rw [e]; exact h) fun sc => ?_ + refine HT.bind (Q := fun _ σ => ScIs t v σ) (fun σ h r hr => + h.ofTKeep ((by unfold ifSporadicCurScTestRefillUnblockCheck ifCondRefillUnblockCheck; tpres : + TPres (ifSporadicCurScTestRefillUnblockCheck (Z := Unit) sc)) σ r hr)) fun _ => ?_ + intro σ h r hr y hy + exact h y (((KPresInst.pres : PresR TcbCellsEq (possibleSwitchTo (Z := Unit) d)) σ r hr).2 _ _ |>.2 hy) + +end Spec + +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + +set_option maxHeartbeats 400000 in +/-- **FL4's `donate` of the sender's SC to a passive receiver** (module docstring), the donation suffix of a plain +send: `replyPushDonateTailFpA` without the reply bind. -/ +theorem donTailFpA {s : S} {σ : State Unit} (a : A) {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {sc f d : U64} (hc : sc ∈ Z.C) (hsc0 : sc ≠ 0#u64) + (hf : f ∈ Z.T) (hd : d ∈ Z.T) (hfd : f ≠ d) (hfs : L.mem s (scA f) = sc) + (hd0 : L.mem s (scA d) = 0#u64) (hfon : L.mem s (affA f) = L.cpu s) + (hmig : L.mem s (affA d) = L.mem s (scCoreA sc)) (hV : L.mem s (wa f 3664#u64) = 0#u64) + (hni : NotIdleScA m (scNameA m sc)) (hQ : NotQueued σ (specPtr f)) + (hpost : ∀ σ', schedContextDonate (Z := Unit) (scNameA m sc) (specPtr d) σ = ({((), σ')}, False) → + RelIfBoundSc σ' (specPtr d)) : + schedcontext.donate st ar s a sc d ⦃ out => out.2 = a ∧ ∃ σ', + schedContextDonate (Z := Unit) (scNameA m sc) (specPtr d) σ = ({((), σ')}, False) ∧ + IpcSchedRelA st Z m κ out.1 σ' ∧ EpStateRel Z.T E R Bf (L.mem out.1) σ' ∧ + ReplyStackRelA (L.mem out.1) R Z.C σ' (scNameA m) stack ∧ KView σ σ' ∧ + MFrA (st := st) Z m s out.1 σ σ' ∧ + ∃ xf, σ.kheap (specPtr f) = some (.tcb xf) ∧ + σ'.kheap (specPtr f) = some (.tcb { xf with tcbSchedContext := none }) ⦄ := by + obtain ⟨xf, hxf, ef⟩ := H.sch.tcb f hf + obtain ⟨xd, hxd, ed⟩ := H.sch.tcb d hd + have hstall : smp.remote_tcb_stall st ar s a f = ok (s, a) := + stall_keepA a (PE.tcb f hf) hfon hV (WritesIn.refl (st := st) s (fun _ => False)) (fun h => h) (fun h => h) + have hst1 : StEqA Z.T (L.mem s) σ := by + intro t ht xt hkt + exact (HE.at_tcb ht hkt).st + have hxfSc : xf.tcbSchedContext = some (scNameA m sc) := by + rw [ef, hfs, optScA, if_neg hsc0] + rfl + obtain ⟨xs, ns, hs, ha⟩ := H.scr.sc sc hc + have scTcb : xs.scTcb = some (specPtr f) := by + obtain ⟨y, k, hy, hb⟩ := H.inv.back (specPtr f) xf _ hxf hxfSc + rw [hs] at hy + cases hy + exact hb + have dom : xf.tcbDomain = 0 := by + obtain ⟨y, hy, hdom, _⟩ := H.rest.prio f hf + rw [hxf] at hy + cases hy + exact hdom + refine WP.spec_mono (sc_donate_fromA H hst1 a hc hsc0 hf hfs hd hd0 hfd hfon hQ.1 hQ.2 hstall hmig hni) ?_ + rintro out ⟨hArch, σ', Q', hD, HB, F⟩ + have hRes : ((), σ') ∈ (schedContextDonate (Z := Unit) (scNameA m sc) (specPtr d) σ).1 := by + rw [hD]; exact Set.mem_singleton _ + obtain ⟨FW, KW⟩ := epFrameOfMcsA H.toSchedSRN P F (schedContextDonateKeepsUserMemory _ _ _ _ hRes) + refine ⟨hArch, σ', hD, ⟨Q', Z.R, HB.toA (hpost σ' hD)⟩, HE.frame PE FW KW, ?_, KW, F, + xf, hxf, donationFromNone hs scTcb hxf hxd (fun eq => hfd (specPtr_inj eq)) dom hQ hRes⟩ + refine HS.frame ?_ (fun q _ y k hq => ?_) ?_ + · intro q hq + have hn := replyNextPlaceA (Z := Z) PE hq + have hp := replyPrevPlaceA (Z := Z) PE hq + exact ⟨F.wr.mem_eq hn.1 (replyWordOffSchedA H.toSchedSRN P hq (by omega) (by omega)).2, + F.wr.mem_eq hp.1 (replyWordOffSchedA H.toSchedSRN P hq (by omega) (by omega)).2⟩ + · obtain ⟨y', hq', ey⟩ := donationKeepsReplyLists hs scTcb hxf hxd (fun eq => hfd (specPtr_inj eq)) + dom hQ hRes hq + exact ⟨y', k, hq', ey⟩ + · intro q hq y hky + obtain ⟨xq, hkq, _⟩ := HE.reply q hq + have hkq' := F.keep.obj (specPtr q) (.reply xq) hkq (by intro _ _ e; cases e) + (by intro _ e; cases e) + rw [hkq'] at hky + cases hky + exact ⟨_, hkq, rfl⟩ + +set_option maxHeartbeats 400000 in +/-- The program after the reply unlink of a plain donating send: the donation and the wakeup. -/ +theorem sendRecvDonPlainAfterUnlinkA {s : S} {σ : State Unit} (a : A) {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {sc r t d : U64} (hc : sc ∈ Z.C) (hsc0 : sc ≠ 0#u64) (ht : t ∈ Z.T) (hd : d ∈ Z.T) (htd : t ≠ d) + {xt xd : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) (hkd : σ.kheap (specPtr d) = some (.tcb xd)) + (hft : xt.tcbFault = none) (hsf : L.mem s (scA t) = sc) (hsd : L.mem s (scA d) = 0#u64) + (hfon : L.mem s (affA t) = L.cpu s) (hon : L.mem s (affA d) = L.cpu s) + (hmig : L.mem s (affA d) = L.mem s (scCoreA sc)) (hV : L.mem s (wa t 3664#u64) = 0#u64) + (hni : NotIdleScA m (scNameA m sc)) (hRun : SndRunA σ (specPtr t) xt) + (hnr : runnable xd.tcbState = false ∧ xd.tcbState ≠ .idleThreadState) + (hq : ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr d ∉ z.epQueue) + (g gr : Bool) + (hpost : ∀ σ', schedContextDonate (Z := Unit) (scNameA m sc) (specPtr d) σ = ({((), σ')}, False) → + RelIfBoundSc σ' (specPtr d)) : + sendRecvAfterUnlinkA (st := st) (ar := ar) s a t d r false g gr true ⦃ out => out.2 = a ∧ + ∃ σ', (do + schedContextDonate (Z := Unit) (scNameA m sc) (specPtr d) + setThreadState (specPtr d) .running + specSendTailA (specPtr d)) σ = ({((), σ')}, False) ∧ + EpStateRel Z.T E R Bf (L.mem out.1) σ' ∧ IpcSchedRelA st Z m κ out.1 σ' ∧ + ReplyStackRelA (L.mem out.1) R Z.C σ' (scNameA m) stack ∧ + WritesIn st s out.1 (fun b => b = stA d ∨ MWrA (st := st) Z m σ s b) ∧ + ScIs (specPtr t) none σ' ⦄ := by + have hf0 : L.mem s (tFaultA t) = fl4_abi.consts.fault.NullFault := by + rw [(HE.at_tcb ht hkt).fault0.2 hft]; unfold fl4_abi.consts.fault.NullFault; rfl + unfold sendRecvAfterUnlinkA + simp only [Bool.false_eq_true, ↓reduceIte] + step with obj.Tcb.fault_type.spec as ⟨i1, hi1⟩ + have hf0' : i1 = fl4_abi.consts.fault.NullFault := by rw [hi1]; exact hf0 + simp only [hf0', bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + step with obj.Tcb.sc.spec as ⟨i2, hi2⟩ + have hi20 : i2 = 0#u64 := by rw [hi2]; exact hsd + rw [if_pos hi20] + step with obj.Tcb.sc.spec as ⟨i3, hi3⟩ + have hi3' : i3 = sc := by rw [hi3]; exact hsf + subst hi3' + refine WP.spec_bind (donTailFpA a H P PE HE HS hc hsc0 ht hd htd hsf hsd hfon hmig hV hni hRun.2.1 hpost) ?_ + rintro ⟨s4, a4⟩ ⟨rfl, σ4, hD, ⟨Q4, R4, H4⟩, E4, S4, KW, F4, xf4, hxf4, hn4⟩ + obtain ⟨x4, hk4, hs4, -⟩ := KW.2.2.1 (specPtr d) xd hkd + have hq4 : ∀ e ∈ E, ∀ z, σ4.kheap (specPtr e) = some (.endpoint z) → specPtr d ∉ z.epQueue := by + intro e he z hz + obtain ⟨y, hy, _⟩ := HE.ep e he + have eq := KernelObject.endpoint.inj (Option.some.inj (hz.symm.trans (KW.1 _ _ hy))) + exact hq e he z (eq.symm ▸ hy) + have hon4 : L.mem s4 (affA d) = L.cpu s4 := by rw [H.aff_eq H4 hd hd, H.cpu_eq H4, hon] + refine WP.spec_mono (WP.spec_and + (sendWakeAfterPushA a4 H4 P PE E4 S4 hd hon4 hk4 (by rw [hs4]; exact hnr) hq4 (hpost _ hD)) + (sendWakeFpA a4 H4 P PE E4 hd hon4 hk4 (by rw [hs4]; exact hnr) hq4 (hpost _ hD))) ?_ + rintro out ⟨⟨ha, σ', hW, E', H', S'⟩, w5⟩ + have hm : ((), σ') ∈ ((do setThreadState (Z := Unit) (specPtr d) .running + specSendTailA (specPtr d)) σ4).1 := by + rw [hW]; exact Set.mem_singleton _ + have scn : ScIs (specPtr t) none σ' := + donTail_scIs (fun e => htd (specPtr_inj e)) none σ4 (fun x hx => by + obtain rfl := KernelObject.tcb.inj (Option.some.inj (hn4.symm.trans hx)) + rfl) ((), σ') hm + refine ⟨ha, σ', (nd_bind_one hD _).trans hW, E', H', S', (WritesIn.trans (st := st) F4.wr w5).mono fun b h => ?_, + scn⟩ + rcases h with h | (h | h) + · exact Or.inr h + · exact Or.inl h + · exact Or.inr (mwr_back rfl rfl F4.sz F4.wr.1 h) + +set_option maxHeartbeats 400000 in +/-- **A plain send that donates, to a waiting receiver without SC, with or without a reply object** (module +docstring). -/ +theorem send_ipc_recv_donPlain_fpA {s : S} {σ : State Unit} (a : A) {stack : U64 → List U64} + (H : SchedSRA st Z m κ s σ) (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {kb : U64} (hPl : PlaceA Z.th kb) (hSt : StateRelA Z.th (L.mem s) σ) (hVld : SlotsValidA Z.th (L.mem s)) + (hC : BufCover st Z.T Bf s σ) (hNative : BufNativeA Z.T (L.mem s)) + (hoff : ∀ y, IsSlotA Z.th y → ¬ EWrite Z.T E R Bf y ∧ + ¬ EWrite Z.T E R Bf (core.num.U64.wrapping_add y 8#u64)) + {t : U64} (ht : t ∈ Z.T) {xt : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hRun : SndRunA σ (specPtr t) xt) + {e d : U64} (he : e ∈ E) {q : List ObjRef} + (hk : σ.kheap (specPtr e) = some (.endpoint (.recvEP (specPtr d :: q)))) + (hd : d ∈ Z.T) {xd : Tcb} (hkd : σ.kheap (specPtr d) = some (.tcb xd)) + (hu : ∀ rp, ipcBufOfA true xd = some rp → ∀ k, k < 128 → inUserFrame (bufAddr rp k) σ) + (hrep : RepOKA σ (specPtr d)) (badge : U64) (g gr blk : Bool) + (hft : xt.tcbFault = none) + (hcaps : g = false ∨ ipcBufOfA false xt = none ∨ + unat (dataToMessageInfo (xt.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0) + {sc : U64} (hc : sc ∈ Z.C) (hsc0 : sc ≠ 0#u64) + (hfs : L.mem s (scA t) = sc) (hd0 : L.mem s (scA d) = 0#u64) + (hfon : L.mem s (affA t) = L.cpu s) (hon : L.mem s (affA d) = L.cpu s) + (hmig : L.mem s (affA d) = L.mem s (scCoreA sc)) + (hV : L.mem s (wa t 3664#u64) = 0#u64) + (hni : NotIdleScA m (scNameA m sc)) {dd : ReceiverPayload} {rep : Option ObjRef} + (hRecv : xd.tcbState = .blockedOnReceive (specPtr e) rep dd) + (hrp : ∀ r', rep = some r' → ∃ r : U64, r' = specPtr r ∧ r ∈ R ∧ ReplyIsolatedA (L.mem s) Z.C stack r) + (hReleased : ∀ σ2 σ3 σ4, + doIpcTransfer (Z := Unit) (specPtr t) (some (specPtr e)) badge.bv g (specPtr d) + (updK σ (specPtr e) (.endpoint (epDeq (.recvEP (specPtr d :: q)) (specPtr d)))) = ({((), σ2)}, False) → + RfKA σ2 σ3 → + schedContextDonate (Z := Unit) (scNameA m sc) (specPtr d) σ3 = ({((), σ4)}, False) → + RelIfBoundSc σ4 (specPtr d)) : + endpoint.send_ipc st ar s a blk false badge g gr true t e ⦃ out => out.2 = a ∧ + ∃ σ', sendIpc (Z := Unit) blk false badge.bv g gr true (specPtr t) (specPtr e) σ = ({((), σ')}, False) ∧ + EpStateRel Z.T E R Bf (L.mem out.1) σ' ∧ IpcSchedRelA st Z m κ out.1 σ' ∧ + ReplyStackRelA (L.mem out.1) R Z.C σ' (scNameA m) stack ∧ + WritesIn st s out.1 (SendFpA st Z.T R Bf (fun σ s => MWrA (st := st) Z m σ s) σ s t e d False) ∧ + ScIs (specPtr t) none σ' ⦄ := by + let O := schedOpsEAOfPlacementA (st := st) (m := m) (κ := κ) P + have F : SchedFootEA st Z.T E R Bf (IpcSchedRelA st Z m κ) (fun σ s => MWrA (st := st) Z m σ s) := + schedFootEA_of_sched + have htSpec : xt.tcbSchedContext = some (scNameA m sc) := by + rw [schedTcbScA H ht hkt, hfs, optScA, if_neg hsc0] + rfl + have hdSpec : xd.tcbSchedContext = none := by + rw [schedTcbScA H hd hkd, hd0, optScA, if_pos (show (0#u64 : U64) = 0#u64 from rfl)] + refine send_ipc_recvContA O PE hPl s a σ HE ⟨Z.Q, Z.R, H⟩ hSt hVld hC hNative hoff ht + ((H.thT t).2 ht) hkt (runnable_codeA hRun.1).1 he hk ((H.thT d).2 hd) hkd hu hrep + badge g gr blk false true (Or.inl ⟨hft, hcaps⟩) ?_ + intro s1' s2 σ2 x2 rep' dd' σ1' _ htd hde hS1 E2 hS2 hW hE hW2 hE2 hfr2 hkt2 hkd1 hkd2 hstd hst2 hrr hq2 hT hfp + have hrep' : rep' = rep := by + have eq := hstd.symm.trans hRecv + injection eq + subst rep' + obtain ⟨Q2, R2, H2⟩ := hS2 + have HS2 := endpointStackFrameA H.toSchedSRN P PE HE HS hW hE + have hv2 := (endpointKeepsVcpuA (Z := Z) PE ht hW).trans hV + have hdSpec2 := (epStepTcbScA hE hkd hkd2).trans hdSpec + have h2 : WritesIn st s s2 (SendFpA st Z.T R Bf (fun σ s => MWrA (st := st) Z m σ s) σ s t e d False) := + hfp.mono fun _ h => Or.inl h + have hz2 : SSzA σ σ2 := ssz_of_epStep hE + cases rep with + | none => + have hword : L.mem s2 (tReplyA d) = 0#u64 := by + have h := ((E2.at_tcb hd hkd2).recv _ _ _ hst2).1 + unfold optRef at h + split_ifs at h with c + exact c + unfold sendRecvAfterTransferA + rw [eq_of_spec_fd (obj.Tcb.reply_obj.spec (L := L) s2 d), bind_ok, hword, if_neg (by simp), bind_ok] + obtain ⟨hsf3, hsd3⟩ := pushAfterBlockScWordsA H2 ht hd hsc0 hni hkt2 hkd2 htSpec hdSpec2 + have hfon3 : L.mem s2 (affA t) = L.cpu s2 := by rw [H.aff_eq H2 ht ht, H.cpu_eq H2, hfon] + have hon3 : L.mem s2 (affA d) = L.cpu s2 := by rw [H.aff_eq H2 hd hd, H.cpu_eq H2, hon] + have hmig3 : L.mem s2 (affA d) = L.mem s2 (scCoreA sc) := by + rw [H.aff_eq H2 hd hd, H2.scCore sc hc, ← H.scCore sc hc, hmig] + have hRun3 := hRun.keep (SchedKeepA.step hE) + have spec := sendIpc_recv_donSpecA hk hkd hde hstd blk badge.bv g gr hT (σ3 := σ2) rfl hkt2 hkd2 hft htSpec + hdSpec2 + refine WP.spec_mono (sendRecvDonPlainAfterUnlinkA a H2 P PE E2 HS2 hc hsc0 ht hd htd hkt2 hkd2 hft hsf3 hsd3 + hfon3 hon3 hmig3 hv2 hni hRun3 (by rw [hst2]; exact ⟨rfl, fun h => by cases h⟩) hq2 g gr + (fun σ4 hp => hReleased σ2 σ2 σ4 hT (PreO.refl _) hp)) ?_ + rintro out ⟨ha, σ', hP, E', H', S', w4, scn⟩ + exact ⟨ha, σ', spec.trans hP, E', H', S', + WritesIn.schedA F h2 hz2 (B := fun b => b = stA d) w4 + (fun _ h => Or.inr (Or.inr (Or.inr (Or.inl h)))) (fun _ h => Or.inr (Or.inr (Or.inr (Or.inr h)))), scn⟩ + | some r' => + obtain ⟨r, rfl, hr, hi⟩ := hrp r' rfl + obtain ⟨xr, hkr, hback⟩ := hrr _ rfl + have hi2 := endpointKeepsIsolatedA H.toSchedSRN P PE hr hi hW + have hr0 : r ≠ 0#u64 := fun eq => (PE.rep r hr).2.1 (by rw [eq]; rfl) + have hword : L.mem s2 (tReplyA d) = r := by + obtain ⟨_, eq⟩ := optRef_some ((E2.at_tcb hd hkd2).recv _ _ _ hst2).1 + exact specPtr_inj eq + unfold sendRecvAfterTransferA + rw [eq_of_spec_fd (obj.Tcb.reply_obj.spec (L := L) s2 d), bind_ok, hword, if_pos (bne_iff_ne.mpr hr0)] + have wu : ipc.reply_unlink st s2 r d ⦃ s3 => WritesIn st s2 s3 + (fun b => (∃ q ∈ R, b = rTcbA q) ∨ b = stA d ∨ MWrA (st := st) Z m σ2 s2 b) ⦄ := by + have h := send_recv_reply_fpA O.to0 F.to0 PE hS1 E2 hW2 hE2 hd hfr2 (fun h => htd (specPtr_inj h)) hkd1 hkd2 hstd + hst2 hrr hq2 r hword.symm + rwa [if_pos (bne_iff_ne.mpr hr0)] at h + refine WP.spec_bind (WP.spec_and (unlinkReplyRelFrameA H2 P PE E2 HS2 hd hr hi2 hkd2 hkr hback + (Or.inr ⟨_, _, hst2⟩) hq2) wu) ?_ + rintro s3 ⟨⟨σ3, hU, H3, E3, S3, I3, V3⟩, w3⟩ + obtain ⟨hkt3, hkd3, hq3, keep3⟩ := sendUnlinkFactsA htd hkt2 hkd2 hkr hback hst2 hq2 hU + obtain ⟨hsf3, hsd3⟩ := pushAfterBlockScWordsA H3 ht hd hsc0 hni hkt3 hkd3 htSpec hdSpec2 + have hfon3 : L.mem s3 (affA t) = L.cpu s3 := by rw [H.aff_eq H3 ht ht, H.cpu_eq H3, hfon] + have hon3 : L.mem s3 (affA d) = L.cpu s3 := by rw [H.aff_eq H3 hd hd, H.cpu_eq H3, hon] + have hmig3 : L.mem s3 (affA d) = L.mem s3 (scCoreA sc) := by + rw [H.aff_eq H3 hd hd, H3.scCore sc hc, ← H.scCore sc hc, hmig] + have hRun3 := hRun.keep ((SchedKeepA.step hE).trans keep3) + have spec := sendIpc_recv_donSpecA hk hkd hde hstd blk badge.bv g gr hT hU hkt3 hkd3 hft htSpec hdSpec2 + have mU : ((), σ3) ∈ (replyUnlinkTcb (Z := Unit) (specPtr d) (specPtr r) σ2).1 := by + rw [hU]; exact Set.mem_singleton _ + refine WP.spec_mono (sendRecvDonPlainAfterUnlinkA a H3 P PE E3 S3 hc hsc0 ht hd htd hkt3 hkd3 hft hsf3 hsd3 + hfon3 hon3 hmig3 ((V3 t ht).trans hv2) hni hRun3 ⟨rfl, fun h => by cases h⟩ hq3 g gr + (fun σ4 hp => hReleased σ2 σ3 σ4 hT (replyUnlinkTcb_rfA _ _ _ _ mU) hp)) ?_ + rintro out ⟨ha, σ', hP, E', H', S', w4, scn⟩ + have hz3 : SSzA σ σ3 := hz2.trans (sszRunA hU) + have h3 : WritesIn st s s3 (SendFpA st Z.T R Bf (fun σ s => MWrA (st := st) Z m σ s) σ s t e d False) := + WritesIn.schedA F h2 hz2 (B := fun b => (∃ r ∈ R, b = rTcbA r) ∨ b = stA d) + (w3.mono fun _ h => or_assoc.mpr h) + (fun _ h => h.elim (fun h => Or.inr (Or.inl h)) fun h => Or.inr (Or.inr (Or.inr (Or.inl h)))) + (fun _ h => Or.inr (Or.inr (Or.inr (Or.inr h)))) + exact ⟨ha, σ', spec.trans hP, E', H', S', + WritesIn.schedA F h3 hz3 (B := fun b => b = stA d) w4 + (fun _ h => Or.inr (Or.inr (Or.inr (Or.inl h)))) (fun _ h => Or.inr (Or.inr (Or.inr (Or.inr h)))), scn⟩ + +/-- **The receiver's `RelIfBoundSc` after the donation of a plain send** (the body's `hReleased`): the SC it +receives is the sender's, released at the send (`RelIfBound` of the runnable sender) with the same refills and +clock. -/ +theorem relIfBoundSc_donPlainA {th : TypedHeapA} {kb : U64} (hPl : PlaceA th kb) (sf : S) {σ : State Unit} + (hR : StateRelA th (L.mem sf) σ) (hV : SlotsValidA th (L.mem sf)) {t d : U64} {e : ObjRef} {z : Endpoint} + (htr : th d = some .tcb) (hk : σ.kheap e = some (.endpoint z)) (z' : Endpoint) + {xt xd : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) (hkd : σ.kheap (specPtr d) = some (.tcb xd)) + (hf : xt.tcbFault = none) (hal : ∀ rb, ipcBufOfA true xd = some rb → (bufAddr rb 0).toNat % 8 = 0) + {sc : ObjRef} (hsc : xt.tcbSchedContext = some sc) + (hrl : specPtr t ∉ σ.releaseQueue) (hRB : RelIfBound σ (specPtr t)) + (badge : Badge) (g : Bool) + (hcaps : g = false ∨ ipcBufOfA false xt = none ∨ + unat (dataToMessageInfo (xt.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0) : + ∀ σ2 σ3 σ4, + doIpcTransfer (Z := Unit) (specPtr t) (some e) badge g (specPtr d) (updK σ e (.endpoint z')) = + ({((), σ2)}, False) → + RfKA σ2 σ3 → + schedContextDonate (Z := Unit) sc (specPtr d) σ3 = ({((), σ4)}, False) → + RelIfBoundSc σ4 (specPtr d) := by + intro σ2 σ3 σ4 hT R3 hP + set σ0 := updK σ e (.endpoint z') with hσ0 + have hte : specPtr t ≠ e := fun h => by rw [← h, hkt] at hk; cases hk + have hde : specPtr d ≠ e := fun h => by rw [← h, hkd] at hk; cases hk + have hkt0 : σ0.kheap (specPtr t) = some (.tcb xt) := by rw [hσ0, updK_kheap, Function.update_of_ne hte, hkt] + have hkd0 : σ0.kheap (specPtr d) = some (.tcb xd) := by rw [hσ0, updK_kheap, Function.update_of_ne hde, hkd] + have HS : SpecSchedA σ σ0 := by + refine ⟨fun p => ?_, rfl, rfl, rfl, rfl, rfl, rfl⟩ + by_cases h : p = e + · subst h + exact Or.inr (Or.inr ⟨_, _, hk, by rw [hσ0, updK_kheap, Function.update_self], + Or.inr (Or.inr (Or.inl ⟨_, _, rfl, rfl⟩))⟩) + · exact Or.inl (by rw [hσ0, updK_kheap, Function.update_of_ne h]) + have R0 : RfKA σ σ0 := RfKA.upd fun _ _ h => by cases h + have m2 : ((), σ2) ∈ (doIpcTransfer (Z := Unit) (specPtr t) (some e) badge g (specPtr d) σ0).1 := by + rw [hT]; exact Set.mem_singleton _ + have m4 : ((), σ4) ∈ (schedContextDonate (Z := Unit) sc (specPtr d) σ3).1 := by + rw [hP]; exact Set.mem_singleton _ + have R2 : RfKA σ0 σ2 := doIpcTransfer_rfA hPl sf (hR.specSchedA HS) hV htr hkt0 hkd0 hf hal _ _ _ hcaps _ m2 + have R4 : RfKA σ3 σ4 := schedContextDonate_rfA _ _ _ _ m4 + have RF : RfKA σ σ4 := PreO.trans R0 (PreO.trans R2 (PreO.trans R3 R4)) + have bound : ∀ y, σ4.kheap (specPtr d) = some (.tcb y) → y.tcbSchedContext = some sc := + donate_bindsA sc (specPtr d) σ3 trivial ((), σ4) m4 + intro x scp y n hx hscp hy ha _ + have e1 : scp = sc := Option.some.inj (hscp.symm.trans (bound x hx)) + subst e1 + obtain ⟨y0, hy0, er, em⟩ := RF.2.2 _ _ _ hy + have a0 : scActive y0 = true := by unfold scActive at ha ⊢; rw [← em]; exact ha + have rel := hRB xt scp y0 n hkt hsc hy0 a0 hrl + unfold releasedSc + unfold released at rel + have eh : refillHd y = refillHd y0 := by unfold refillHd; rw [er] + rw [RF.1, eh] + exact rel + +end + +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/Stage3SendDonPlainArmA.lean b/proofs/Fl4ProofsA64/P3/Stage3SendDonPlainArmA.lean new file mode 100644 index 000000000..03d6a5763 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SendDonPlainArmA.lean @@ -0,0 +1,533 @@ +/- +P3 (aarch64, A64-P3 line RecvPreA): **the donating plain send (`can_donate = true`, not a call) to a passive +receiver**, at the record, and `NBSendRecv` / `NBSendWait` through it. + +* **`JSyscallA.sendDonPlainA`**: `decode_invocation` (`blocking`, `call = false`, `can_donate = true`) on an Endpoint + send cap whose endpoint holds a blocked receiver without an SC, with or without a reply object, from a sender without + a fault and without extra caps to transfer: the message is transferred, a reply object is unlinked, the sender's SC goes + to the receiver (`send_ipc_recv_donPlain_fpA`) and the receiver runs; the sender stays `Restart` without an SC; the + reply stacks are unchanged. +* **`JSyscallA.nbWakeEpiDonA`**: the success epilogue of a waking operation after which the caller may have lost its + SC (`nbWakeEpiA` with `x.tcbSchedContext ∈ {some curSc, none}`). +* **`JSyscallA.nbSendDonFirstA`**: `NBSendRecv` / `NBSendWait` whose send half donates to a passive receiver ends in + `RecvPreA`. + +[S] `hsnd` (the sender has no fault and no extra caps), `hrcv` (the receiver has no SC), +`hV` (the sender's VCPU word is 0). [P] `ArchTotal`, `MaxSpi`, `ArchPlat`, `A64FrameMasks`, `hCs`, `h1` +(`numNodes = 1`), `hmb`, `hmax`, `htu`. No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3SendDonPlainA +import Fl4ProofsA64.P3.Stage3SendDonateArmA +import Fl4ProofsA64.P3.Stage3NBSendIpcA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {J5 J7 : JSlotA S} {s : S} {a : A} {σ : State Unit} {m : McState} + +set_option maxHeartbeats 2000000 in +/-- **The donating plain send to a passive receiver, at a syscall entry** (module docstring). -/ +theorem JSyscallA.sendDonPlainA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p badge slot lbl len buf : U64} {recv grant grantReply blk fp : Bool} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) + (hcap : capA st s slot = .Endpoint p badge true recv grant grantReply) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hp : p ∈ E) {d : U64} {q : List ObjRef} + (hk : σ.kheap (specPtr p) = some (.endpoint (.recvEP (specPtr d :: q)))) + (hsnd : ∀ x, σ.kheap σ.curThread = some (.tcb x) → x.tcbFault = none ∧ (grant = false ∨ + ipcBufOfA false x = none ∨ + unat (dataToMessageInfo (x.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0)) + (hrcv : ∀ y, σ.kheap (specPtr d) = some (.tcb y) → y.tcbSchedContext = none) + (hV : L.mem s (wa (L.curW s) 3664#u64) = 0#u64) : + ∃ xc σ1 σ2 s2, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ((), σ2) ∈ (sendIpc (Z := Unit) blk false badge.bv grant grantReply true σ.curThread (specPtr p) σ1).1 ∧ + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk false true fp buf extra = + ok (.Ok (), s2, a) ∧ + Sched.WakeEndA σ σ2 ∧ specPtr d ≠ σ.curThread ∧ + SIKA σ.idleThread (fun o => o = σ.curThread ∨ o = specPtr d) (fun o => o = specPtr d) true σ1 σ2 ∧ + (∀ x, σ2.kheap σ.curThread = some (.tcb x) → x.tcbState = .restart ∧ x.tcbSchedContext = none) ∧ + ∃ Q Rl, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s2 a σ2 m := by + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hslot hcov J.actW J.running J.cur + have hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM := J.toJStage3EntryA.curNotVmA + obtain ⟨xc, hkc, hst, hsp, J1, -, hOS1, -⟩ := J.toJStage3A.of_restartCurA + RS.pl.choose_spec.choose_spec.choose_spec.choose_spec RS.sym RS.run hnvm RS.cur RS.vs RS.vc + have hws : WritesIn st s (L.wr s (stA (L.curW s)) sched.TS_RESTART) (fun b => b = stA (L.curW s)) := by + refine writesIn_wr ?_ + rw [stA_eq] + exact field_aligned (J.sr.core.tcbs _ J.sr.curT) (by simp) (by simp) + have hst1 : L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART) (stA (L.curW s)) = sched.TS_RESTART := + rd_wr_eq (by rw [stA_eq]; exact field_aligned (J.sr.core.tcbs _ J.sr.curT) (by simp) (by simp)) + generalize hs1 : L.wr s (stA (L.curW s)) sched.TS_RESTART = s1 at hst J1 hOS1 hws hst1 + set t := L.curW s with ht_def + set σ1 := stUpdA σ σ.curThread xc .restart with hσ1 + have kb1 : L.ksb s1 = L.ksb s := hws.1 + have cpu1 : L.cpu s1 = L.cpu s := hws.2.1 + have ct : σ.curThread = specPtr t := J.j.1.sch.curThread + have htZ : t ∈ Z.T := J.sr.curT + set xt : Tcb := { xc with tcbState := .restart } with hxt + have hkt : σ1.kheap (specPtr t) = some (.tcb xt) := by + rw [hσ1, ← ct]; simp [stUpdA, setObjA, hxt] + have ne1 : specPtr p ≠ σ.curThread := fun e => by rw [e, hkc] at hk; cases hk + have hk1 : σ1.kheap (specPtr p) = some (.endpoint (.recvEP (specPtr d :: q))) := by + rw [hσ1]; simp only [stUpdA, setObjA, Function.update_of_ne ne1]; exact hk + -- the receiver + obtain ⟨hep2, xs, -, hq, hQ, hB⟩ := J1.ep.at_ep hp hk1 + have hdx : d ∈ xs := by + have : specPtr d ∈ xs.map specPtr := by rw [← hq]; exact List.mem_cons_self + obtain ⟨y, hy, e⟩ := List.mem_map.1 this + rwa [specPtr_inj e] at hy + have hd : d ∈ Z.T := hQ.mem d hdx + have hdst : (L.mem s1 (stA d)).val = 3 := (hB d hdx).1 + have hdb : L.mem s1 (Notif.tBlockA d) = p := (hB d hdx).2 + obtain ⟨xd, hkd, rd⟩ := J1.ep.tcb d hd + obtain ⟨e', rep, dd, hstd⟩ := tsCode_three (rd.st.symm.trans hdst) + have htd : t ≠ d := fun h => by + have : (L.mem s1 (stA t)).val = 3 := h ▸ hdst + rw [hst1] at this + unfold sched.TS_RESTART at this + simp at this + have hdt : specPtr d ≠ specPtr t := fun h => htd (specPtr_inj h).symm + have hkdσ : σ.kheap (specPtr d) = some (.tcb xd) := by + rw [← hkd, hσ1, ct]; simp only [stUpdA, setObjA, Function.update_of_ne hdt] + -- the sender and the receiver in the spec + obtain ⟨hft, hcaps⟩ : xt.tcbFault = none ∧ (grant = false ∨ ipcBufOfA false xt = none ∨ + unat (dataToMessageInfo (xt.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0) := hsnd xc hkc + have hc : m.curCore < fl4A64Platform.numNodes := by rw [J1.j.1.sch.core]; exact hCs _ J1.j.1.sch.cpuCs + have hO : SchedOpsEA st Z.T E Rp Bf (IpcSchedRelA st Z m κ) := + (ipcSchedOpsA (ar := ar) J1.iplaced J1.ipcPlace hc hmb hmax htu).2.2.1 + have hF : SchedFootEA st Z.T E Rp Bf (IpcSchedRelA st Z m κ) (fun σ s => MWrA (st := st) Z m σ s) := + schedFootEA_of_sched + have hSR : IpcSchedRelA st Z m κ s1 σ1 := ⟨Z.Q, Z.R, J1.sr⟩ + have hdth : Z.th d = some .tcb := (J1.sr.thT d).2 hd + have hon : L.mem s1 (affA d) = L.cpu s1 := by + have x := hCs _ (J1.sr.core.aff d hd) + have y := hCs _ J1.j.1.sch.cpuCs + exact UScalar.eq_of_val_eq (by omega) + have hnb : tsCode xt.tcbState ≠ 3 := by simp [hxt, tsCode] + have hu := J1.bufFrameA hd hkd true + have hrep := J1.repOKA hd + have hoff := J1.slotOffEwA + have hidle : specPtr t ≠ σ1.idleThread ∧ specPtr d ≠ σ1.idleThread := by + obtain ⟨x, hx, hs⟩ := J1.sinv.idleTcb + refine ⟨fun h => ?_, fun h => ?_⟩ + · rw [← h, hkt] at hx; cases hx; cases hs + · rw [← h, hkd] at hx; cases hx; rw [hstd] at hs; cases hs + -- the receiver's SC, its reply object if any, the sender's SC, the donation's side conditions + have hd0 := hrcv xd hkdσ + have he' : e' = specPtr p := by + have h := rd.blk e' (by rw [hstd]; rfl) + rw [← h, hdb] + subst he' + have hrepR : ∀ r', rep = some r' → ∃ r : U64, r' = specPtr r ∧ r ∈ Rp ∧ + ReplyIsolatedA (L.mem s1) Z.C stack r := by + intro r' hr' + subst rep + obtain ⟨hw, hrr⟩ := optRef_some (rd.recv _ _ _ hstd).1 + have hRrp : L.mem s1 (tReplyA d) ∈ Rp := (rd.recv _ _ _ hstd).2 (by simp) + set r : U64 := L.mem s1 (tReplyA d) with hr_def + subst hrr + obtain ⟨xr, hkr, hback⟩ := J1.repOKA hd xd hkd _ _ _ hstd + have hnbr : L.mem s1 (stA (L.mem s1 (Endp.rTcbA r))) ≠ sched.TS_BLOCKED_ON_REPLY := by + obtain ⟨yr, hyr, er⟩ := J1.ep.reply r hRrp + rw [hkr] at hyr + cases hyr + rw [hback] at er + rw [specPtr_inj (optRef_some er.symm).2] + intro h + rw [h] at hdst + revert hdst + unfold sched.TS_BLOCKED_ON_REPLY + simp + exact ⟨r, rfl, hRrp, J1.replyIsolatedA hRrp hnbr⟩ + set sc : U64 := L.mem s1 (scA t) with hsc_def + have htH : t ∈ H.tcbs := by rw [J.tcbs]; exact htZ + have kt := spec_at (sched.set_thread_state.restart_blk (Es := ∅) s t (BXI.blk J.sys.ent.bxi) + htH (runnable_ok J.running).1 (runnable_ok J.running).2) hst + have kt1 := kt.2.2.2 + have hscs : sc = L.mem s (scA t) := by rw [hsc_def]; exact kt1.scf htH + have hsc1 : L.mem s1 (scA t) ≠ 0#u64 := by rw [kt1.scf htH]; exact J.cur.2 + have hsc0 : sc ≠ 0#u64 := hsc1 + have hscC : sc ∈ Z.C := J1.sr.rest.scMem t htZ hsc1 + have htSc : xt.tcbSchedContext = some (scNameA m sc) := by + rw [Endp.schedTcbScA J1.sr htZ hkt, ← hsc_def, optScA, if_neg hsc0] + rfl + have hDon : DonA σ1 (specPtr t) xt := by + intro sc' hsc' + obtain ⟨y, k, hy, hb⟩ := J1.sr.inv.back (specPtr t) xt sc' hkt hsc' + refine ⟨fun h => ?_, fun y' k' hy' => by rw [hy] at hy'; cases hy'; exact hb⟩ + obtain ⟨x, yi, ki, -, -, hyi, hti⟩ := J1.sinv.idleSc + rw [h, hyi] at hy + cases hy + exact hidle.1 (Option.some.inj (hb.symm.trans hti)) + have hni : NotIdleScA m (scNameA m sc) := by + have h1' : scNameA m sc ≠ idleScPtr := (hDon _ htSc).1 + refine ⟨h1', fun c hc' e => ?_⟩ + have hcc : c = m.curCore := by + have a : c < 1 := h1 ▸ hc' + have b : m.curCore < 1 := h1 ▸ hc + exact (Nat.lt_one_iff.1 a).trans (Nat.lt_one_iff.1 b).symm + rw [hcc] at e + unfold scNameA at e h1' + split_ifs at e h1' with g + · exact h1' rfl + · exact g e + have affE : ∀ u ∈ Z.T, L.mem s1 (affA u) = L.cpu s1 := fun u hu => by + have x := hCs _ (J1.sr.core.aff u hu) + have y := hCs _ J1.j.1.sch.cpuCs + exact UScalar.eq_of_val_eq (by omega) + have hmig : L.mem s1 (affA d) = L.mem s1 (scCoreA sc) := by + have x := hCs _ (J1.sr.core.aff d hd) + have y := J1.scCpu sc hscC + refine UScalar.eq_of_val_eq ?_ + rw [J1.sr.scCore sc hscC] + omega + have hV1 : L.mem s1 (wa t 3664#u64) = 0#u64 := by + have hta := J.sr.core.tcbs t htZ + have hv : (wa t 3664#u64).val = t.val + 3664 := by + have := hta.fits + rw [wa_valm]; exact Nat.mod_eq_of_lt (by simp; omega) + rw [← hV] + refine hws.mem_eq (field_aligned hta (by simp) (by simp)) fun c => ?_ + have h' := congrArg (fun w : U64 => w.val) c + simp only [hv, Endp.val_st (J.iplaced.e.tcb t htZ)] at h' + omega + have hRun : SndRunA σ1 (specPtr t) xt := + J.sndRunA hCs h1 (by rw [hxt]; rfl) (fun dm pr hm => by + refine J1.sinv.ctQ (by show σ.schedulerAction = .resumeCurThread; rw [J.sr.sch.act]; unfold actOfA; rw [if_pos J.actW]) dm pr ?_ + rw [show σ1.curThread = specPtr t from ct] + exact hm) rfl rfl hidle.1 + have hal := J1.bufAlign0A hd hkd true + have hRB : RelIfBound σ1 (specPtr t) := J1.sr.rd _ _ hkt (Or.inl hRun.1) + have hRel := relIfBoundSc_donPlainA (st := st) J1.sr.place s1 J1.j.1.st J1.j.1.cv hdth hk1 + (epDeq (.recvEP (specPtr d :: q)) (specPtr d)) hkt hkd hft hal htSc hRun.2.1.2 hRB badge.bv grant hcaps + have pd := send_ipc_recv_donPlain_fpA (st := st) (ar := ar) a J1.sr J1.ipcPlace J1.iplaced.e J1.ep J1.reply + J1.sr.place J1.j.1.st J1.j.1.cv J1.bufCover J1.bufNative hoff htZ hkt hRun hp hk1 hd hkd hu hrep badge grant + grantReply blk hft hcaps (sc := sc) hscC hsc0 rfl + (by + refine UScalar.eq_of_val_eq ?_ + have h := Endp.schedTcbScA J1.sr hd hkd + rw [hd0] at h + unfold optScA at h + split_ifs at h with c + rw [c]) + (affE t htZ) (affE d hd) hmig hV1 hni hstd hrepR hRel + -- P1's invariants after the send + obtain ⟨Pt, Ub, Pp7, Tb7, Hr, Te, Kt, Lv7, Re, bits, KLv, W⟩ := J1.j7.unp + have I := J1.delIn hA hPl W + have D := J.decPreA hslot hext + have hc1 := D.cb.restart (BXI.blk J.sys.ent.bxi).1.choose_spec.choose_spec.tcbs kt1 kt.2.2.1 + have hH : H p = some .ep := J.utIpc.ep p hp + have P := (J.toJStage3A.sysPlace hext hslot hcov).keepO hOS1 kb1 + have hnames : ∀ k, H p = some k → ∃ x ∈ OS, NamesA (capA st s1 x) p k := fun k hk' => by + rw [hH] at hk' + cases hk' + exact ⟨slot, hslot, by rw [hOS1.capA hslot, hcap]; exact names_ep⟩ + have p1 := Fl4ProofsA64.P1.W7.R.ST.endpoint.send_ipc.recvS (st := st) (ar := ar) (Vc' := Z.V) (Vc := Z.V) + (Ub := Ub) (Pt := Pt) hA a blk false badge grant grantReply true t p P (BXI.blk J1.sys.ent.bxi) I.del.sca I.ts + J1.sys.sysl J1.symL hH (by rw [epState_eq]; exact hep2) hnames hc1 + (by rw [hst1]; unfold sched.TS_RESTART sched.TS_INACTIVE; decide) (fun _ => hsc1) + (D.rv.restart kt1 (D.rv.zero htH D.nvm)) + obtain ⟨⟨s2, a2⟩, eb, ⟨⟨ha2, σ2, hspecI, E2, ⟨Q, Rl, sr2⟩, rs2, wD, scn⟩, p1post⟩⟩ := + exists_of_specA (WP.spec_and pd p1) + simp only at ha2 eb E2 sr2 rs2 wD scn p1post + subst ha2 + have hmem : ((), σ2) ∈ (sendIpc (Z := Unit) blk false badge.bv grant grantReply true (specPtr t) (specPtr p) σ1).1 := by + rw [hspecI]; exact Set.mem_singleton _ + have blk2 := p1post.2.2.1 + have hdec : syscall.decode_invocation st ar s a2 lbl len slot (capA st s slot) blk false true fp buf extra = + ok (.Ok (), s2, a2) := by + rw [hcap, decodeEndpoint_programA] + simp only [↓reduceIte, ipcInvokeProgramA, cur_thread_ok, bind_ok, ← ht_def, hst] + rw [show endpoint.send_ipc st ar s1 a2 blk false badge grant grantReply true t p = ok (s2, a2) from eb] + simp only [bind_ok] + rfl + have F := J.decodeFinA hA hM hPl hfm hslot hcov (rfl : capA st s slot = capA st s slot) hext hSC (lbl := lbl) + (by rw [hcap]; simp [DecArmS1aEnt, DecArmQH, DecArmQZ2, DecArmKQ2, DecArmKQ, capFam]) + (fun _ h => by rw [hcap] at h; cases h) (fun _ h => by rw [hcap] at h; cases h) hdec σ2 m + obtain ⟨-, -, sysl, symL, -, userOK, j7⟩ := F + -- the spec outcome's record facts + set M : ObjRef → Prop := fun o => o = specPtr t ∨ o = specPtr d with hMdef + set Nq : ObjRef → Prop := fun o => o = specPtr d with hNdef + have K : SIKA σ1.idleThread M Nq true σ1 σ2 := + sendIpc_recv_sikA (M := M) (N := Nq) J1.sr.place s1 J1.j.1.st J1.j.1.cv hdth hk1 hkt hkd hft hal blk false + badge.bv grant grantReply true hcaps (Or.inl rfl) (Or.inr rfl) rfl hidle.1 hidle.2 hDon + (fun _ => by rw [htSc]; simp) (fun c => by cases c) _ hmem + have U2 : UFrA (BufBytesA (ipcBufOfA true xd)) σ1 σ2 := + sendIpc_recv_ufrA J1.sr.place s1 J1.j.1.st J1.j.1.cv hdth hk1 hkt hkd hft hal blk false badge.bv grant + grantReply true hcaps _ hmem + have hact : ActTcbA σ1 (specPtr t) := by + have h := (J1.schedValid.act J.act (fun c => hidle.1 (ct ▸ c))).1 + obtain ⟨x, sc', ex, es, a'⟩ := h + exact ⟨x, sc', ct ▸ ex, es, a'⟩ + have Qs : QSendA σ1 σ2 := + sendIpc_recv_qsA (i := σ1.idleThread) (M := M) (N := Nq) J1.sr.place s1 J1.j.1.st J1.j.1.cv hdth hk1 hkt hkd hal + blk false badge.bv grant grantReply true (Or.inl ⟨hft, hcaps⟩) (Or.inl rfl) (Or.inr rfl) rfl hidle.1 hidle.2 + hDon (fun _ => by rw [htSc]; simp) (fun _ => scActive_of_actTcbA hact hkt) + (fun e' r dd h => by + obtain ⟨xr, k, b⟩ := J1.repOKA hd xd hkd e' r dd h + exact ⟨xr, k, by rw [b]; simp⟩) _ hmem + have sv : SchedValidA σ2 := + sendIpc_recv_schedValidFA J1.sr.place s1 J1.j.1.st J1.j.1.cv hdth hk1 hkt hkd hal blk false + badge.bv grant grantReply true (Or.inl ⟨hft, hcaps⟩) hstd + (Or.inr (Or.inr (Or.inr (Or.inr ⟨rfl, rfl, hft, hd0, hact⟩)))) J1.schedValid J1.rqDomPrioA _ hmem + have run : StIs (specPtr d) .running σ2 := sendIpc_recv_runningA hk1 blk false badge.bv grant grantReply true _ hmem + -- the senders and receivers before and after + have hMs : ∀ u ∈ Z.T, M (specPtr u) → ∀ x, σ1.kheap (specPtr u) = some (.tcb x) → + (x = xt ∨ x = xd) := by + intro u _ hu x hx + rcases hu with h | h + · rw [h, hkt] at hx; cases hx; exact Or.inl rfl + · rw [h, hkd] at hx; cases hx; exact Or.inr rfl + have hst2 : L.mem s2 (stA t) = L.mem s1 (stA t) := by + have hta := J1.sr.core.tcbs t htZ + refine (wD.mono fun _ h => h).mem_eq (a_st hta).1 ?_ + have hv := Endp.val_st hta + rintro (c | ⟨r, hr, c⟩ | ⟨c, -⟩ | c | c) + · have apart : ∀ u ∈ Z.T, u ≠ t → u.val + 4096 ≤ t.val ∨ t.val + 4096 ≤ u.val := fun u hu ne => by + have := J1.sr.place.apart u t .tcb .tcb ((J1.sr.thT u).2 hu) ((J1.sr.thT t).2 htZ) ne + have hsz : ObjKindA.size .tcb = 4096 := rfl + omega + rcases c with c | ⟨u, hu, -, c⟩ | ⟨r, c⟩ | c + · have := J1.iplaced.e.et p hp t htZ; unfold Endp.EW at c; omega + · have := (J1.iplaced.e.tcb u hu).fits + unfold TL at c + by_cases e : u = t + · subst e; omega + · have := apart u hu e; omega + · rw [Endp.regA_val (J1.iplaced.e.tcb d hd) (regIdxA_lt r)] at c + have := regIdxA_lt r + have := apart d hd htd.symm + omega + · have := J1.iplaced.e.bt _ c t htZ; omega + · have := J1.iplaced.e.rt r hr t htZ + rw [c, Endp.rTcbA_val] at hv + omega + · exact c.elim + · have h' := congrArg (fun w : U64 => w.val) c + simp only [hv, Endp.val_st (J1.iplaced.e.tcb d hd)] at h' + exact htd (UScalar.eq_of_val_eq (by omega)) + · rw [stA_eq] at c + exact J1.tcbWord_offMwrA htZ (by simp) (by simp) c + have hres : ∀ x, σ2.kheap σ.curThread = some (.tcb x) → x.tcbState = .restart := by + intro x hx + rw [ct] at hx + obtain ⟨y, hy, r⟩ := E2.tcb t htZ + rw [hx] at hy + cases hy + have h2 : tsCode x.tcbState = 2 := by rw [← r.st, hst2, hst1]; unfold sched.TS_RESTART; rfl + revert h2 + cases x.tcbState <;> intro h2 <;> first | rfl | (simp [tsCode] at h2) | (rename_i e; cases e; simp [tsCode] at h2) + have after : ∀ u ∈ Z.T, M (specPtr u) → ∀ x, σ2.kheap (specPtr u) = some (.tcb x) → + epBlocked x.tcbState = none ∧ ∀ p, x.tcbState ≠ .blockedOnNotification p := by + intro u _ hu x hx + rcases hu with h | h + · rw [specPtr_inj h] at hx + have h2 := hres x (by rw [ct]; exact hx) + rw [h2] + exact ⟨rfl, fun _ h => by cases h⟩ + · rw [h] at hx + rw [run x hx] + exact ⟨rfl, fun _ h => by cases h⟩ + have wk : NtfnWaitKeepA Z.T N (L.mem s1) (L.mem s2) := + J1.ntfnWait_sendA (W := False) htZ hp hd hdb (wD.mono fun _ h => Or.inl h) + have wI : WritesIn st s1 s2 (fun b => IpcFpA st Z E Rp Bf m σ1 s1 b ∨ RepWA Rp b) := + wD.mono fun _ h => donFpA_splitA J1.sr.core.tcbs htZ hp hd + (Or.inl (h.imp id (Or.imp id (Or.imp (fun c => c.1.elim) id)))) + have wN : WritesIn st s1 s2 (fun b => SchedStepNWA (st := st) N E Rp Z m σ1 s1 b ∨ BufW Bf b) := + wI.mono fun _ h => h.elim + (fun h => h.elim (ewrite_stepNA_buf J1.sr.core.tcbs) fun h => Or.inl (Or.inl (Or.inl h))) + fun h => Or.inl (Or.inr (Or.inr (Or.inr h))) + have notM : ∀ c < n, ¬ M (m.idleThreadOf c) := by + intro c hc' h + obtain ⟨y, hy, hs⟩ := J1.sidle.f3 c hc' + rcases h with h | h + · rw [h, hkt] at hy; cases hy; cases hs + · rw [h, hkd] at hy; cases hy; rw [hstd] at hs; cases hs + have sinv : SInvXA σ2 m n := by + refine K.sinv J1.sinv notM (fun h => h.elim (fun h => hidle.1 h.symm) fun h => hidle.2 h.symm) ?_ ?_ ?_ ?_ + (fun y hy hr => by + rcases hy with rfl | rfl + · exact hRun.2.1.2 hr + · obtain ⟨x, hx, hrn⟩ := J1.sinv.rlsRun _ hr; rw [hkd] at hx; cases hx; rw [hstd] at hrn; cases hrn) + (J1.sidle.rdyA J1.sinv) + (fun q hq x y hx hy hf => by + rcases hq with rfl | rfl + · obtain ⟨x0, hx0, g⟩ := K.tcbB hy + rw [hkt] at hx0; cases hx0 + exact absurd (g.fault.trans hft) hf + · obtain ⟨x0, hx0, g⟩ := K.tcbB hy + rw [hkd] at hx0; cases hx0 + exact absurd (g.fault.trans (FaultStOkA.none J1.sinv.faultSt hkd (by + rw [hstd]; rintro (⟨_, _, h⟩ | ⟨_, h⟩ | h) <;> cases h))) hf) + · intro y hy dm pr hm + rcases hy with rfl | rfl + · refine J1.sinv.ctQ (by show σ.schedulerAction = .resumeCurThread; rw [J.sr.sch.act]; unfold actOfA; rw [if_pos J.actW]) dm pr ?_ + rw [show σ1.curThread = specPtr t from ct] + exact hm + · obtain ⟨x, hx, hr⟩ := J1.sinv.rq dm pr _ hm + rw [hkd] at hx; cases hx; rw [hstd] at hr; cases hr + · intro y hy + cases hy + refine ⟨fun h => hdt (h.trans (show σ1.curThread = specPtr t from ct)), hidle.2, fun c hc' h => + notM c hc' (Or.inr h), fun z hz => by rw [run z hz]; rfl⟩ + · intro y hy + cases hy + exact ⟨xd, hkd⟩ + · exact J.noSwitch + have hMv : ∀ u ∈ Z.T, M (specPtr u) → L.mem s1 (stA u) ≠ sched.TS_RUNNING_VM := by + intro u _ hu h + rcases hu with e | e <;> rw [specPtr_inj e] at h + · rw [hst1] at h; revert h; unfold sched.TS_RESTART sched.TS_RUNNING_VM; decide + · rw [h] at hdst; revert hdst; unfold sched.TS_RUNNING_VM; decide + have ntfn := J1.ntfn_ipcA wI K E2 wk (fun u hu hm x hx p h => by + rcases hMs u hu hm x hx with rfl | rfl + · cases h + · rw [hstd] at h; cases h) (fun u hu hm x hx => (after u hu hm x hx).2) + have J2 := J1.of_ipcStepUA (Q := Q) (Rl := Rl) (curOf := Z.curOf) (curScOf := Z.curScOf) wN U2.1 U2.2.1 Qs + (J1.specZero_of_recvBufA hd hkd U2.2.2) (fun _ _ => rfl) (BlkOK.bxi blk2) sysl symL + userOK j7 sr2 E2 ntfn rs2 J1.deadExt.reply K.ipcKeep (J1.tw_ipcA wI) sinv + (K.sidle J1.sidle notM (J1.sidle.rdyA J1.sinv)) + (J1.runVcpu_ipcA wI K E2 hMv) (J1.blkQ_ipcA K E2 wk after) sv + have hwake : Sched.WakeEndA σ σ2 := by + have E3 := Sched.sendIpc_recv_endA J1.sr.place s1 J1.j.1.st J1.j.1.cv hdth hk1 hkt hkd hal blk false badge.bv + grant grantReply true (Or.inl ⟨hft, hcaps⟩) hstd + (Or.inr (Or.inr (Or.inr (Or.inr ⟨rfl, rfl, hft, hd0, hact⟩)))) + J1.schedValid J1.rqDomPrioA J.noSwitch hDon hidle.1 hidle.2 _ hmem + have B : Sched.BLA σ1 σ2 := Sched.sendIpc_recv_blA J1.sr.place s1 J1.j.1.st J1.j.1.cv hdth hk1 hkt hkd hft hal + blk false badge.bv grant grantReply true hcaps _ hmem + have B0 : Sched.BLA σ σ1 := Sched.BLA.tcbW hkc + refine ⟨PreO.trans B0 B, E3.1, fun _ x hx => Or.inr (hres x hx)⟩ + have hK : SIKA σ.idleThread (fun o => o = σ.curThread ∨ o = specPtr d) (fun o => o = specPtr d) true σ1 σ2 := by + rw [ct]; exact K + refine ⟨xc, σ1, σ2, s2, hkc, rfl, hsp, by rw [ct]; exact hmem, hdec, hwake, fun e => hdt (e.trans ct), hK, + fun x hx => ⟨hres x hx, scn x (by rw [← ct]; exact hx)⟩, Q, Rl, J2⟩ + +set_option maxHeartbeats 2000000 in +/-- **From a donating operation's end record to `RecvPreA`**: the caller is still `Restart` and has lost its SC +(module docstring). -/ +theorem JSyscallA.nbWakeEpiDonA + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + {σ2 : State Unit} {s2 : S} {Q1 : U64 → U64 → List U64} {R1 : U64 → List U64} + (J2 : JStage3A st AR { Z with Q := Q1, R := R1 } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s2 a σ2 m) + (W : Sched.WakeEndA σ σ2) (off : OffQA σ.curThread σ2) + (hx : ∀ x, σ2.kheap σ.curThread = some (.tcb x) → x.tcbState = .restart ∧ x.tcbSchedContext = none) : + getThreadState (Z := Unit) σ.curThread σ2 = ({(.restart, σ2)}, False) ∧ + ∃ σ3 s3, ((), σ3) ∈ (setThreadState (Z := Unit) σ.curThread .running σ2).1 ∧ + obj.Tcb.state st (L.curW s) s2 = ok sched.TS_RESTART ∧ + sched.set_thread_state st s2 (L.curW s) sched.TS_RUNNING = ok s3 ∧ + syscall.after_invocation st ar s3 a (.Ok ()) = ok (true, s3, a) ∧ + L.curW s3 = L.curW s ∧ + (∀ x, σ3.kheap σ3.curThread = some (.tcb x) → x.tcbSchedContext = none) ∧ + ∃ Q Rl, RecvPreA st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n + (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s3 a σ3 m := by + set t := L.curW s with ht_def + have ct : σ.curThread = specPtr t := J.j.1.sch.curThread + have P0' := J.toRecvPreA + have B := Sched.BodyEndA.ofWakeA (nA := hPl.ipa) J J2 W + have thr : σ2.curThread = σ.curThread := B.2 + have htZ : t ∈ Z.T := J.sr.curT + obtain ⟨x2, hx2, -⟩ := J2.ep.tcb t htZ + have hk2c : σ2.kheap σ.curThread = some (.tcb x2) := by rw [ct]; exact hx2 + have hk2 : σ2.kheap σ2.curThread = some (.tcb x2) := by rw [thr]; exact hk2c + obtain ⟨hst2, hsc2⟩ := hx x2 hk2c + have act2 : ∀ sc, x2.tcbSchedContext = some sc → ScActiveAtA σ2 sc := fun sc hs => + absurd (hsc2.symm.trans hs) (by simp) + have get2 : getThreadState (Z := Unit) σ.curThread σ2 = ({(.restart, σ2)}, False) := by + rw [← hst2]; exact threadGet_eqA Tcb.tcbState hk2c + obtain ⟨σ3, s3, mem3, hstate, eRun, haft, hcur3, c3, own3, Q3, R3, P3⟩ := + J.epiBridgeA hA hPl J2 B.1 thr hk2 hst2 (fun d p => by rw [thr]; exact off.1 d p) (by rw [thr]; exact off.2.1) + act2 (fun e => off.2.2 _ e thr) + refine ⟨get2, σ3, s3, mem3, hstate, eRun, haft, hcur3, fun x hxx => ?_, Q3, R3, P3⟩ + rw [own3 x hxx] + exact hsc2 + + +set_option maxHeartbeats 2000000 in +/-- **`NBSendRecv` / `NBSendWait` donating to a passive receiver ends in `RecvPreA`** (module docstring). -/ +theorem JSyscallA.nbSendDonFirstA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p badge slot lbl len buf : U64} {recv grant grantReply fp : Bool} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) + (hcap : capA st s slot = .Endpoint p badge true recv grant grantReply) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hp : p ∈ E) {d : U64} {q : List ObjRef} + (hk : σ.kheap (specPtr p) = some (.endpoint (.recvEP (specPtr d :: q)))) + (hsnd : ∀ x, σ.kheap σ.curThread = some (.tcb x) → x.tcbFault = none ∧ (grant = false ∨ + ipcBufOfA false x = none ∨ + unat (dataToMessageInfo (x.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0)) + (hrcv : ∀ y, σ.kheap (specPtr d) = some (.tcb y) → y.tcbSchedContext = none) + (hV : L.mem s (wa (L.curW s) 3664#u64) = 0#u64) : + ∃ xc σ1 σ2 σ3 s2 s3, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ((), σ2) ∈ (sendIpc (Z := Unit) false false badge.bv grant grantReply true σ.curThread (specPtr p) σ1).1 ∧ + getThreadState (Z := Unit) σ.curThread σ2 = ({(.restart, σ2)}, False) ∧ + ((), σ3) ∈ (setThreadState (Z := Unit) σ.curThread .running σ2).1 ∧ + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) false false true fp buf extra = + ok (.Ok (), s2, a) ∧ + obj.Tcb.state st (L.curW s) s2 = ok sched.TS_RESTART ∧ + sched.set_thread_state st s2 (L.curW s) sched.TS_RUNNING = ok s3 ∧ + syscall.after_invocation st ar s3 a (.Ok ()) = ok (true, s3, a) ∧ + L.curW s3 = L.curW s ∧ + (∀ x, σ3.kheap σ3.curThread = some (.tcb x) → x.tcbSchedContext = none) ∧ + ∃ Q Rl, RecvPreA st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n + (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s3 a σ3 m := by + obtain ⟨xc, σ1, σ2, s2, hkc, hσ1, hsp, hmem, hdec, W, hdne, K, hres, Q1, R1, J2⟩ := + J.sendDonPlainA (lbl := lbl) (len := len) (buf := buf) (blk := false) (fp := fp) + hA hM hPl hfm hCs h1 hmb hmax htu hslot hcov hcap hext hSC hp hk hsnd hrcv hV + have P0' := J.toRecvPreA + have f1 : σ1.curThread = σ.curThread ∧ σ1.readyQueues = σ.readyQueues ∧ σ1.releaseQueue = σ.releaseQueue ∧ + σ1.schedulerAction = σ.schedulerAction := by rw [hσ1]; exact ⟨rfl, rfl, rfl, rfl⟩ + obtain ⟨-, f1q, f1r, f1a⟩ := f1 + have off : OffQA σ.curThread σ2 := by + refine ⟨fun dm pr h => ?_, fun h => ?_, fun u e eu => ?_⟩ + · rcases K.rq dm pr _ h with h | h | h + · rw [f1q] at h; exact P0'.noQ dm pr h + · exact hdne h.symm + · rw [f1a] at h; exact J.noSwitch _ h + · have := K.rls _ h + rw [f1r] at this + exact P0'.notRel this + · rcases K.act with c | c | ⟨y, hy, c⟩ + · rw [c, f1a] at e; exact J.noSwitch u e + · rw [c] at e; cases e + · rw [c] at e + cases e + exact hdne (hy.symm.trans eu) + obtain ⟨get2, σ3, s3, mem3, hstate, eRun, haft, hcur3, own3, Q3, R3, P3⟩ := + J.nbWakeEpiDonA hA hPl J2 W off hres + exact ⟨xc, σ1, σ2, σ3, s2, s3, hkc, hσ1, hsp, hmem, get2, mem3, hdec, hstate, eRun, haft, hcur3, own3, Q3, R3, P3⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3SendDonateArmA.lean b/proofs/Fl4ProofsA64/P3/Stage3SendDonateArmA.lean new file mode 100644 index 000000000..5a827601c --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SendDonateArmA.lean @@ -0,0 +1,399 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2b-2): **the donating Endpoint call to a waiting receiver, at the record**: a call +with grant or grant-reply of a sender without a fault and without extra caps, to a receiver blocked with a reply +object and without an SC. + +* **`JSyscallA.sendDonateA`**: `decode_invocation` (`blocking`, `call`, `can_donate`) on an Endpoint send cap whose + endpoint holds waiting receivers: the message is transferred, the reply object is pushed on the sender's SC, the SC + is donated and the receiver runs; the reply stacks gain the reply at the sender's SC. +* **`JSyscallA.sendDonateDispA`**: the same arm in the dispatch top's shape (`DispPostA`). + +[S] (scope selectors) `hg` (grant or grant-reply), `hsnd` (the sender has no fault and no extra caps), `hrcv` (the +receiver waits with a reply object and has no SC), `hV` (the sender's VCPU word is 0). +[P] `ArchTotal`, `MaxSpi`, `ArchPlat`, `A64FrameMasks`, `hCs`, `h1` (`numNodes = 1`), `hmb`, `hmax`, `htu`. New module. +-/ +import Fl4ProofsA64.P3.Stage3SendQA +import Fl4ProofsA64.P3.Stage3SendRecvArmA +import Fl4ProofsA64.P3.Stage3SendDonateKeepA +import Fl4ProofsA64.P3.Stage3IpcWakeA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {J5 J7 : JSlotA S} {s : S} {a : A} {σ : State Unit} {m : McState} + +set_option maxHeartbeats 2000000 in +/-- **The donating call to a waiting receiver, at a syscall entry** (module docstring). -/ +theorem JSyscallA.sendDonateA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p badge slot lbl len buf : U64} {recv grant grantReply blk fp : Bool} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) + (hcap : capA st s slot = .Endpoint p badge true recv grant grantReply) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hp : p ∈ E) {d : U64} {q : List ObjRef} + (hk : σ.kheap (specPtr p) = some (.endpoint (.recvEP (specPtr d :: q)))) + (hsnd : ∀ x, σ.kheap σ.curThread = some (.tcb x) → x.tcbFault = none ∧ (grant = false ∨ + ipcBufOfA false x = none ∨ + unat (dataToMessageInfo (x.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0)) + (hg : grant = true ∨ grantReply = true) + (hrcv : ∀ y, σ.kheap (specPtr d) = some (.tcb y) → + y.tcbSchedContext = none ∧ ∃ e r dd, y.tcbState = .blockedOnReceive e (some r) dd) + (hV : L.mem s (wa (L.curW s) 3664#u64) = 0#u64) : + ∃ xc σ1 σ2 s2, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ((), σ2) ∈ (sendIpc (Z := Unit) blk true badge.bv grant grantReply true σ.curThread (specPtr p) σ1).1 ∧ + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk true true fp buf extra = + ok (.Ok (), s2, a) ∧ + Sched.WakeEndA σ σ2 ∧ + ∃ Q Rl, ∃ r ∈ Rp, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf + (Function.update stack (L.mem s (scA (L.curW s))) (r :: stack (L.mem s (scA (L.curW s))))) κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s2 a σ2 m := by + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hslot hcov J.actW J.running J.cur + have hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM := J.toJStage3EntryA.curNotVmA + obtain ⟨xc, hkc, hst, hsp, J1, -, hOS1, -⟩ := J.toJStage3A.of_restartCurA + RS.pl.choose_spec.choose_spec.choose_spec.choose_spec RS.sym RS.run hnvm RS.cur RS.vs RS.vc + have hws : WritesIn st s (L.wr s (stA (L.curW s)) sched.TS_RESTART) (fun b => b = stA (L.curW s)) := by + refine writesIn_wr ?_ + rw [stA_eq] + exact field_aligned (J.sr.core.tcbs _ J.sr.curT) (by simp) (by simp) + have hst1 : L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART) (stA (L.curW s)) = sched.TS_RESTART := + rd_wr_eq (by rw [stA_eq]; exact field_aligned (J.sr.core.tcbs _ J.sr.curT) (by simp) (by simp)) + generalize hs1 : L.wr s (stA (L.curW s)) sched.TS_RESTART = s1 at hst J1 hOS1 hws hst1 + set t := L.curW s with ht_def + set σ1 := stUpdA σ σ.curThread xc .restart with hσ1 + have kb1 : L.ksb s1 = L.ksb s := hws.1 + have cpu1 : L.cpu s1 = L.cpu s := hws.2.1 + have ct : σ.curThread = specPtr t := J.j.1.sch.curThread + have htZ : t ∈ Z.T := J.sr.curT + set xt : Tcb := { xc with tcbState := .restart } with hxt + have hkt : σ1.kheap (specPtr t) = some (.tcb xt) := by + rw [hσ1, ← ct]; simp [stUpdA, setObjA, hxt] + have ne1 : specPtr p ≠ σ.curThread := fun e => by rw [e, hkc] at hk; cases hk + have hk1 : σ1.kheap (specPtr p) = some (.endpoint (.recvEP (specPtr d :: q))) := by + rw [hσ1]; simp only [stUpdA, setObjA, Function.update_of_ne ne1]; exact hk + -- the receiver + obtain ⟨hep2, xs, -, hq, hQ, hB⟩ := J1.ep.at_ep hp hk1 + have hdx : d ∈ xs := by + have : specPtr d ∈ xs.map specPtr := by rw [← hq]; exact List.mem_cons_self + obtain ⟨y, hy, e⟩ := List.mem_map.1 this + rwa [specPtr_inj e] at hy + have hd : d ∈ Z.T := hQ.mem d hdx + have hdst : (L.mem s1 (stA d)).val = 3 := (hB d hdx).1 + have hdb : L.mem s1 (Notif.tBlockA d) = p := (hB d hdx).2 + obtain ⟨xd, hkd, rd⟩ := J1.ep.tcb d hd + obtain ⟨e', rep, dd, hstd⟩ := tsCode_three (rd.st.symm.trans hdst) + have htd : t ≠ d := fun h => by + have : (L.mem s1 (stA t)).val = 3 := h ▸ hdst + rw [hst1] at this + unfold sched.TS_RESTART at this + simp at this + have hdt : specPtr d ≠ specPtr t := fun h => htd (specPtr_inj h).symm + have hkdσ : σ.kheap (specPtr d) = some (.tcb xd) := by + rw [← hkd, hσ1, ct]; simp only [stUpdA, setObjA, Function.update_of_ne hdt] + -- the sender and the receiver in the spec + obtain ⟨hft, hcaps⟩ : xt.tcbFault = none ∧ (grant = false ∨ ipcBufOfA false xt = none ∨ + unat (dataToMessageInfo (xt.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0) := hsnd xc hkc + have hc : m.curCore < fl4A64Platform.numNodes := by rw [J1.j.1.sch.core]; exact hCs _ J1.j.1.sch.cpuCs + have hO : SchedOpsEA st Z.T E Rp Bf (IpcSchedRelA st Z m κ) := + (ipcSchedOpsA (ar := ar) J1.iplaced J1.ipcPlace hc hmb hmax htu).2.2.1 + have hF : SchedFootEA st Z.T E Rp Bf (IpcSchedRelA st Z m κ) (fun σ s => MWrA (st := st) Z m σ s) := + schedFootEA_of_sched + have hSR : IpcSchedRelA st Z m κ s1 σ1 := ⟨Z.Q, Z.R, J1.sr⟩ + have hdth : Z.th d = some .tcb := (J1.sr.thT d).2 hd + have hon : L.mem s1 (affA d) = L.cpu s1 := by + have x := hCs _ (J1.sr.core.aff d hd) + have y := hCs _ J1.j.1.sch.cpuCs + exact UScalar.eq_of_val_eq (by omega) + have hnb : tsCode xt.tcbState ≠ 3 := by simp [hxt, tsCode] + have hu := J1.bufFrameA hd hkd true + have hrep := J1.repOKA hd + have hoff := J1.slotOffEwA + have hidle : specPtr t ≠ σ1.idleThread ∧ specPtr d ≠ σ1.idleThread := by + obtain ⟨x, hx, hs⟩ := J1.sinv.idleTcb + refine ⟨fun h => ?_, fun h => ?_⟩ + · rw [← h, hkt] at hx; cases hx; cases hs + · rw [← h, hkd] at hx; cases hx; rw [hstd] at hs; cases hs + -- the receiver's reply object, the sender's SC, the donation's side conditions + obtain ⟨hd0, er, rr, edd, hsr⟩ := hrcv xd hkdσ + have hrep0 : rep = some rr := by + have h := hstd.symm.trans hsr + injection h + subst hrep0 + have hrcvw := rd.recv _ _ _ hstd + obtain ⟨-, hrr⟩ := optRef_some hrcvw.1 + have hRrp : L.mem s1 (tReplyA d) ∈ Rp := hrcvw.2 (by simp) + set r : U64 := L.mem s1 (tReplyA d) with hr_def + subst hrr + have he' : e' = specPtr p := by + have h := rd.blk e' (by rw [hstd]; rfl) + rw [← h, hdb] + subst he' + have hrep := J1.repOKA hd + obtain ⟨xr, hkr, hback⟩ := hrep xd hkd _ _ _ hstd + have hnbr : L.mem s1 (stA (L.mem s1 (Endp.rTcbA r))) ≠ sched.TS_BLOCKED_ON_REPLY := by + obtain ⟨yr, hyr, er⟩ := J1.ep.reply r hRrp + rw [hkr] at hyr + cases hyr + rw [hback] at er + rw [specPtr_inj (optRef_some er.symm).2] + intro h + rw [h] at hdst + revert hdst + unfold sched.TS_BLOCKED_ON_REPLY + simp + have hri := J1.replyIsolatedA hRrp hnbr + set sc : U64 := L.mem s1 (scA t) with hsc_def + have htH : t ∈ H.tcbs := by rw [J.tcbs]; exact htZ + have kt := spec_at (sched.set_thread_state.restart_blk (Es := ∅) s t (BXI.blk J.sys.ent.bxi) + htH (runnable_ok J.running).1 (runnable_ok J.running).2) hst + have kt1 := kt.2.2.2 + have hscs : sc = L.mem s (scA t) := by rw [hsc_def]; exact kt1.scf htH + have hsc1 : L.mem s1 (scA t) ≠ 0#u64 := by rw [kt1.scf htH]; exact J.cur.2 + have hsc0 : sc ≠ 0#u64 := hsc1 + have hscC : sc ∈ Z.C := J1.sr.rest.scMem t htZ hsc1 + have htSc : xt.tcbSchedContext = some (scNameA m sc) := by + rw [Endp.schedTcbScA J1.sr htZ hkt, ← hsc_def, optScA, if_neg hsc0] + rfl + have hDon : DonA σ1 (specPtr t) xt := by + intro sc' hsc' + obtain ⟨y, k, hy, hb⟩ := J1.sr.inv.back (specPtr t) xt sc' hkt hsc' + refine ⟨fun h => ?_, fun y' k' hy' => by rw [hy] at hy'; cases hy'; exact hb⟩ + obtain ⟨x, yi, ki, -, -, hyi, hti⟩ := J1.sinv.idleSc + rw [h, hyi] at hy + cases hy + exact hidle.1 (Option.some.inj (hb.symm.trans hti)) + have hni : NotIdleScA m (scNameA m sc) := by + have h1' : scNameA m sc ≠ idleScPtr := (hDon _ htSc).1 + refine ⟨h1', fun c hc' e => ?_⟩ + have hcc : c = m.curCore := by + have a : c < 1 := h1 ▸ hc' + have b : m.curCore < 1 := h1 ▸ hc + exact (Nat.lt_one_iff.1 a).trans (Nat.lt_one_iff.1 b).symm + rw [hcc] at e + unfold scNameA at e h1' + split_ifs at e h1' with g + · exact h1' rfl + · exact g e + have affE : ∀ u ∈ Z.T, L.mem s1 (affA u) = L.cpu s1 := fun u hu => by + have x := hCs _ (J1.sr.core.aff u hu) + have y := hCs _ J1.j.1.sch.cpuCs + exact UScalar.eq_of_val_eq (by omega) + have hmig : L.mem s1 (affA d) = L.mem s1 (scCoreA sc) := by + have x := hCs _ (J1.sr.core.aff d hd) + have y := J1.scCpu sc hscC + refine UScalar.eq_of_val_eq ?_ + rw [J1.sr.scCore sc hscC] + omega + have hV1 : L.mem s1 (wa t 3664#u64) = 0#u64 := by + have hta := J.sr.core.tcbs t htZ + have hv : (wa t 3664#u64).val = t.val + 3664 := by + have := hta.fits + rw [wa_valm]; exact Nat.mod_eq_of_lt (by simp; omega) + rw [← hV] + refine hws.mem_eq (field_aligned hta (by simp) (by simp)) fun c => ?_ + have h' := congrArg (fun w : U64 => w.val) c + simp only [hv, Endp.val_st (J.iplaced.e.tcb t htZ)] at h' + omega + have hRun : SndRunA σ1 (specPtr t) xt := + J.sndRunA hCs h1 (by rw [hxt]; rfl) (fun dm pr hm => by + refine J1.sinv.ctQ (by show σ.schedulerAction = .resumeCurThread; rw [J.sr.sch.act]; unfold actOfA; rw [if_pos J.actW]) dm pr ?_ + rw [show σ1.curThread = specPtr t from ct] + exact hm) rfl rfl hidle.1 + have hal := J1.bufAlign0A hd hkd true + have hRB : RelIfBound σ1 (specPtr t) := J1.sr.rd _ _ hkt (Or.inl hRun.1) + have hRel := relIfBoundSc_donPushA (st := st) J1.sr.place s1 J1.j.1.st J1.j.1.cv hdth hk1 + (epDeq (.recvEP (specPtr d :: q)) (specPtr d)) hkt hkd hft hal hdt.symm hd0 htSc hDon hidle.1 hidle.2 + hRun.2.1.2 hRB badge.bv grant hcaps (specPtr r) + have pd := send_ipc_recv_pushDonate_fpA (st := st) (ar := ar) a J1.sr J1.ipcPlace J1.iplaced.e J1.ep J1.reply + J1.sr.place J1.j.1.st J1.j.1.cv J1.bufCover J1.bufNative hoff htZ hkt hRun hp hk1 hd hkd hu hrep badge grant + grantReply blk true (Or.inl ⟨hft, hcaps⟩) (sc := sc) (r := r) hscC hsc0 hRrp hri rfl + (by + refine UScalar.eq_of_val_eq ?_ + have h := Endp.schedTcbScA J1.sr hd hkd + rw [hd0] at h + unfold optScA at h + split_ifs at h with c + rw [c]) + (affE t htZ) (affE d hd) hmig hV1 hni hstd (Or.inl rfl) hg hRel + -- P1's invariants after the send + obtain ⟨Pt, Ub, Pp7, Tb7, Hr, Te, Kt, Lv7, Re, bits, KLv, W⟩ := J1.j7.unp + have I := J1.delIn hA hPl W + have D := J.decPreA hslot hext + have hc1 := D.cb.restart (BXI.blk J.sys.ent.bxi).1.choose_spec.choose_spec.tcbs kt1 kt.2.2.1 + have hH : H p = some .ep := J.utIpc.ep p hp + have P := (J.toJStage3A.sysPlace hext hslot hcov).keepO hOS1 kb1 + have hnames : ∀ k, H p = some k → ∃ x ∈ OS, NamesA (capA st s1 x) p k := fun k hk' => by + rw [hH] at hk' + cases hk' + exact ⟨slot, hslot, by rw [hOS1.capA hslot, hcap]; exact names_ep⟩ + have p1 := Fl4ProofsA64.P1.W7.R.ST.endpoint.send_ipc.recvS (st := st) (ar := ar) (Vc' := Z.V) (Vc := Z.V) + (Ub := Ub) (Pt := Pt) hA a blk true badge grant grantReply true t p P (BXI.blk J1.sys.ent.bxi) I.del.sca I.ts + J1.sys.sysl J1.symL hH (by rw [epState_eq]; exact hep2) hnames hc1 + (by rw [hst1]; unfold sched.TS_RESTART sched.TS_INACTIVE; decide) (fun _ => hsc1) + (D.rv.restart kt1 (D.rv.zero htH D.nvm)) + obtain ⟨⟨s2, a2⟩, eb, ⟨⟨ha2, σ2, hspecI, E2, ⟨Q, Rl, sr2⟩, rs2, wD⟩, p1post⟩⟩ := + exists_of_specA (WP.spec_and pd p1) + simp only at ha2 eb E2 sr2 rs2 wD p1post + subst ha2 + have hmem : ((), σ2) ∈ (sendIpc (Z := Unit) blk true badge.bv grant grantReply true (specPtr t) (specPtr p) σ1).1 := by + rw [hspecI]; exact Set.mem_singleton _ + have blk2 := p1post.2.2.1 + have hdec : syscall.decode_invocation st ar s a2 lbl len slot (capA st s slot) blk true true fp buf extra = + ok (.Ok (), s2, a2) := by + rw [hcap, decodeEndpoint_programA] + simp only [↓reduceIte, ipcInvokeProgramA, cur_thread_ok, bind_ok, ← ht_def, hst] + rw [show endpoint.send_ipc st ar s1 a2 blk true badge grant grantReply true t p = ok (s2, a2) from eb] + simp only [bind_ok] + rfl + have F := J.decodeFinA hA hM hPl hfm hslot hcov (rfl : capA st s slot = capA st s slot) hext hSC (lbl := lbl) + (by rw [hcap]; simp [DecArmS1aEnt, DecArmQH, DecArmQZ2, DecArmKQ2, DecArmKQ, capFam]) + (fun _ h => by rw [hcap] at h; cases h) (fun _ h => by rw [hcap] at h; cases h) hdec σ2 m + obtain ⟨-, -, sysl, symL, -, userOK, j7⟩ := F + -- the spec outcome's record facts + set M : ObjRef → Prop := fun o => o = specPtr t ∨ o = specPtr d with hMdef + set Nq : ObjRef → Prop := fun o => o = specPtr d with hNdef + have K : SIKA σ1.idleThread M Nq true σ1 σ2 := + sendIpc_recv_sikA (M := M) (N := Nq) J1.sr.place s1 J1.j.1.st J1.j.1.cv hdth hk1 hkt hkd hft hal blk true + badge.bv grant grantReply true hcaps (Or.inl rfl) (Or.inr rfl) rfl hidle.1 hidle.2 hDon + (fun _ => by rw [htSc]; simp) (fun c => by cases c) _ hmem + have U2 : UFrA (BufBytesA (ipcBufOfA true xd)) σ1 σ2 := + sendIpc_recv_ufrA J1.sr.place s1 J1.j.1.st J1.j.1.cv hdth hk1 hkt hkd hft hal blk true badge.bv grant grantReply + true hcaps _ hmem + have hact : ActTcbA σ1 (specPtr t) := by + have h := (J1.schedValid.act J.act (fun c => hidle.1 (ct ▸ c))).1 + obtain ⟨x, sc', ex, es, a'⟩ := h + exact ⟨x, sc', ct ▸ ex, es, a'⟩ + have Qs : QSendA σ1 σ2 := + sendIpc_recv_qsA (i := σ1.idleThread) (M := M) (N := Nq) J1.sr.place s1 J1.j.1.st J1.j.1.cv hdth hk1 hkt hkd hal + blk true badge.bv grant grantReply true (Or.inl ⟨hft, hcaps⟩) (Or.inl rfl) (Or.inr rfl) rfl hidle.1 hidle.2 hDon + (fun _ => by rw [htSc]; simp) (fun _ => scActive_of_actTcbA hact hkt) + (fun e' r dd h => by + obtain ⟨xr, k, b⟩ := J1.repOKA hd xd hkd e' r dd h + exact ⟨xr, k, by rw [b]; simp⟩) _ hmem + have sv : SchedValidA σ2 := + sendIpc_recv_schedValidA J1.sr.place s1 J1.j.1.st J1.j.1.cv hdth hk1 hkt hkd hft hal blk true badge.bv grant + grantReply true hcaps hstd + (Or.inr (Or.inr (Or.inl ⟨rfl, hg, by simp, rfl, hdt.symm, hd0, hact⟩))) J1.schedValid J1.rqDomPrioA _ hmem + have run : StIs (specPtr d) .running σ2 := sendIpc_recv_runningA hk1 blk true badge.bv grant grantReply true _ hmem + have snt : StIs (specPtr t) (.blockedOnReply (specPtr r)) σ2 := + sendIpc_push_senderA hk1 hkd hstd hdt.symm blk badge.bv grant grantReply true hg _ hmem + -- the senders and receivers before and after + have hMs : ∀ u ∈ Z.T, M (specPtr u) → ∀ x, σ1.kheap (specPtr u) = some (.tcb x) → + (x = xt ∨ x = xd) := by + intro u _ hu x hx + rcases hu with h | h + · rw [h, hkt] at hx; cases hx; exact Or.inl rfl + · rw [h, hkd] at hx; cases hx; exact Or.inr rfl + have after : ∀ u ∈ Z.T, M (specPtr u) → ∀ x, σ2.kheap (specPtr u) = some (.tcb x) → + epBlocked x.tcbState = none ∧ ∀ p, x.tcbState ≠ .blockedOnNotification p := by + intro u _ hu x hx + rcases hu with h | h + · rw [h] at hx + rw [snt x hx] + exact ⟨rfl, fun _ h => by cases h⟩ + · rw [h] at hx + rw [run x hx] + exact ⟨rfl, fun _ h => by cases h⟩ + have wk : NtfnWaitKeepA Z.T N (L.mem s1) (L.mem s2) := + J1.ntfnWait_sendA (W := True) htZ hp hd hdb (wD.mono fun _ h => h.imp id (Or.imp id fun c => ⟨t, htZ, c⟩)) + have wI : WritesIn st s1 s2 (fun b => IpcFpA st Z E Rp Bf m σ1 s1 b ∨ RepWA Rp b) := + wD.mono fun _ h => donFpA_splitA J1.sr.core.tcbs htZ hp hd h + have wN : WritesIn st s1 s2 (fun b => SchedStepNWA (st := st) N E Rp Z m σ1 s1 b ∨ BufW Bf b) := + wI.mono fun _ h => h.elim + (fun h => h.elim (ewrite_stepNA_buf J1.sr.core.tcbs) fun h => Or.inl (Or.inl (Or.inl h))) + fun h => Or.inl (Or.inr (Or.inr (Or.inr h))) + have notM : ∀ c < n, ¬ M (m.idleThreadOf c) := by + intro c hc' h + obtain ⟨y, hy, hs⟩ := J1.sidle.f3 c hc' + rcases h with h | h + · rw [h, hkt] at hy; cases hy; cases hs + · rw [h, hkd] at hy; cases hy; rw [hstd] at hs; cases hs + have sinv : SInvXA σ2 m n := by + refine K.sinv J1.sinv notM (fun h => h.elim (fun h => hidle.1 h.symm) fun h => hidle.2 h.symm) ?_ ?_ ?_ ?_ + (fun y hy hr => by + rcases hy with rfl | rfl + · exact hRun.2.1.2 hr + · obtain ⟨x, hx, hrn⟩ := J1.sinv.rlsRun _ hr; rw [hkd] at hx; cases hx; rw [hstd] at hrn; cases hrn) + (J1.sidle.rdyA J1.sinv) + (fun q hq x y hx hy hf => by + rcases hq with rfl | rfl + · rw [snt y hy]; exact Or.inr (Or.inl ⟨_, rfl⟩) + · obtain ⟨x0, hx0, g⟩ := K.tcbB hy + rw [hkd] at hx0; cases hx0 + exact absurd (g.fault.trans (FaultStOkA.none J1.sinv.faultSt hkd (by + rw [hstd]; rintro (⟨_, _, h⟩ | ⟨_, h⟩ | h) <;> cases h))) hf) + · intro y hy dm pr hm + rcases hy with rfl | rfl + · refine J1.sinv.ctQ (by show σ.schedulerAction = .resumeCurThread; rw [J.sr.sch.act]; unfold actOfA; rw [if_pos J.actW]) dm pr ?_ + rw [show σ1.curThread = specPtr t from ct] + exact hm + · obtain ⟨x, hx, hr⟩ := J1.sinv.rq dm pr _ hm + rw [hkd] at hx; cases hx; rw [hstd] at hr; cases hr + · intro y hy + cases hy + refine ⟨fun h => hdt (h.trans (show σ1.curThread = specPtr t from ct)), hidle.2, fun c hc' h => + notM c hc' (Or.inr h), fun z hz => by rw [run z hz]; rfl⟩ + · intro y hy + cases hy + exact ⟨xd, hkd⟩ + · exact J.noSwitch + have hMv : ∀ u ∈ Z.T, M (specPtr u) → L.mem s1 (stA u) ≠ sched.TS_RUNNING_VM := by + intro u _ hu h + rcases hu with e | e <;> rw [specPtr_inj e] at h + · rw [hst1] at h; revert h; unfold sched.TS_RESTART sched.TS_RUNNING_VM; decide + · rw [h] at hdst; revert hdst; unfold sched.TS_RUNNING_VM; decide + have ntfn := J1.ntfn_ipcA wI K E2 wk (fun u hu hm x hx p h => by + rcases hMs u hu hm x hx with rfl | rfl + · cases h + · rw [hstd] at h; cases h) (fun u hu hm x hx => (after u hu hm x hx).2) + have hdr : ∀ q' ∈ Rp, DeadA st OS .reply s1 q' → ∀ c ∈ Z.C, + q' ∉ Function.update stack sc (r :: stack sc) c := by + intro q' hq' hdead c hc' hm + have old : q' ∈ stack c → False := fun h => J1.deadExt.reply q' hq' hdead c hc' h + by_cases ec : c = sc + · subst ec + rw [Function.update_self] at hm + rcases List.mem_cons.1 hm with e | h + · subst e + exact absurd (J1.deadExt.replyTcb r hRrp hdead xr hkr) (by rw [hback]; simp) + · exact old h + · rw [Function.update_of_ne ec] at hm + exact old hm + have J2 := J1.of_ipcStepUA (Q := Q) (Rl := Rl) (curOf := Z.curOf) (curScOf := Z.curScOf) wN U2.1 U2.2.1 Qs + (J1.specZero_of_recvBufA hd hkd U2.2.2) (fun _ _ => rfl) (BlkOK.bxi blk2) sysl symL + userOK j7 sr2 E2 ntfn rs2 hdr K.ipcKeep (J1.tw_ipcA wI) sinv (K.sidle J1.sidle notM (J1.sidle.rdyA J1.sinv)) + (J1.runVcpu_ipcA wI K E2 hMv) (J1.blkQ_ipcA K E2 wk after) sv + have hwake : Sched.WakeEndA σ σ2 := by + have E3 := Sched.sendIpc_recv_endA J1.sr.place s1 J1.j.1.st J1.j.1.cv hdth hk1 hkt hkd hal blk true badge.bv + grant grantReply true (Or.inl ⟨hft, hcaps⟩) hstd + (Or.inr (Or.inr (Or.inl ⟨Or.inl rfl, hg, by simp, hdt.symm, hact, fun _ => hd0⟩))) + J1.schedValid J1.rqDomPrioA J.noSwitch hDon hidle.1 hidle.2 _ hmem + have B : Sched.BLA σ1 σ2 := Sched.sendIpc_recv_blA J1.sr.place s1 J1.j.1.st J1.j.1.cv hdth hk1 hkt hkd hft hal + blk true badge.bv grant grantReply true hcaps _ hmem + have B0 : Sched.BLA σ σ1 := Sched.BLA.tcbW hkc + refine ⟨PreO.trans B0 B, E3.1, fun hres => absurd hres (E3.2 ⟨show σ1.curThread = specPtr t from ct, Or.inl rfl⟩)⟩ + rw [hscs] at J2 + exact ⟨xc, σ1, σ2, s2, hkc, rfl, hsp, by rw [ct]; exact hmem, hdec, hwake, Q, Rl, r, hRrp, J2⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3SendDonateKeepA.lean b/proofs/Fl4ProofsA64/P3/Stage3SendDonateKeepA.lean new file mode 100644 index 000000000..0f2c31995 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SendDonateKeepA.lean @@ -0,0 +1,555 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2b-2): **spec facts and write sets of the donating call to a waiting receiver** +(`send_ipc_recv_pushDonate_fpA`). + +* **`RfKA`**: the clock kept, the release queue only shrinking, every SC's refills and refill maximum kept; across + `do_ipc_transfer` (no fault, no extra caps), `reply_unlink_tcb` and `reply_push`. +* **`relIfBoundSc_donPushA`**: the receiver's `RelIfBoundSc` after the donating push (the donating body's + `hReleased`): the SC it receives is the sender's, released at the send (`RelIfBound` of the runnable sender) with + the same refills and clock. +* **`sendIpc_push_senderA`**: after a call with a reply push the sender is `BlockedOnReply`. +* **`donFpA_splitA`**: the donating write set `DonFpA` is `IpcFpA` or a reply object's word. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.IpcScSendPushA +import Fl4ProofsA64.P3.Stage3IpcFl4KeepA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P3 Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Classical + +noncomputable section + +/-! ## The refills are kept -/ + +/-- The clock kept, the release queue only shrinking, every SC's refills and refill maximum kept. -/ +def RfKA (σ σ' : State Unit) : Prop := + σ'.curTime = σ.curTime ∧ (∀ y ∈ σ'.releaseQueue, y ∈ σ.releaseQueue) ∧ + ∀ p y' k, σ'.kheap p = some (.schedContext y' k) → + ∃ y, σ.kheap p = some (.schedContext y k) ∧ y'.scRefills = y.scRefills ∧ y'.scRefillMax = y.scRefillMax + +instance rfPreOA : PreO RfKA := + ⟨fun _ => ⟨rfl, fun _ h => h, fun _ y _ h => ⟨y, h, rfl, rfl⟩⟩, fun h1 h2 => + ⟨h2.1.trans h1.1, fun y h => h1.2.1 y (h2.2.1 y h), fun p y'' k h => by + obtain ⟨y', e', a, b⟩ := h2.2.2 p y'' k h + obtain ⟨y, e, a', b'⟩ := h1.2.2 p y' k e' + exact ⟨y, e, a.trans a', b.trans b'⟩⟩⟩ + +/-- A step that keeps the heap and the clock and only shrinks the release queue. -/ +theorem RfKA.ofKh {σ σ' : State Unit} (hk : σ'.kheap = σ.kheap) (ht : σ'.curTime = σ.curTime) + (hr : ∀ y ∈ σ'.releaseQueue, y ∈ σ.releaseQueue) : RfKA σ σ' := + ⟨ht, hr, fun _ y _ h => ⟨y, by rw [← hk]; exact h, rfl, rfl⟩⟩ + +/-- One object replaced by one that is no SC, or an SC with the same refills. -/ +theorem RfKA.upd {σ : State Unit} {p : ObjRef} {o' : KernelObject} + (h : ∀ y' k, o' = .schedContext y' k → + ∃ y, σ.kheap p = some (.schedContext y k) ∧ y'.scRefills = y.scRefills ∧ y'.scRefillMax = y.scRefillMax) : + RfKA σ { σ with kheap := Function.update σ.kheap p (some o') } := by + refine ⟨rfl, fun _ h => h, fun q y' k hq => ?_⟩ + change Function.update σ.kheap p (some o') q = _ at hq + by_cases e : q = p + · subst e + rw [Function.update_self] at hq + exact h y' k (Option.some.inj hq) + · rw [Function.update_of_ne e] at hq + exact ⟨y', hq, rfl, rfl⟩ + +theorem eqPres_rfA {α : Type} {m : SMonad Unit α} (h : PresR (@Eq (State Unit)) m) : PresR RfKA m := + fun s x hx => by rw [← h s x hx]; exact PreO.refl s + +theorem asUser_rfA {α : Type} (t : ObjRef) (f : UserMonad α) : PresR RfKA (asUser (Z := Unit) t f) := by + intro σ r hr + obtain ⟨x, uc, hk, e⟩ := asUser_memA t f hr + rw [e] + exact RfKA.upd fun _ _ h => by cases h + +theorem doMachineOp_rfA {α : Type} (mop : NonDet MachineState α) : PresR RfKA (doMachineOp (Z := Unit) mop) := by + intro σ r hr + obtain ⟨y1, hy1, hr⟩ := hr + have e1 : y1 = (σ.machineState, σ) := hy1 + subst e1 + obtain ⟨⟨⟨a, ms'⟩, s2⟩, hy2, hr⟩ := hr + have e2 : s2 = σ := hy2.2 + subst e2 + obtain ⟨y3, hy3, hr⟩ := hr + have e3 : y3 = ((), { s2 with machineState := ms' }) := hy3 + subst e3 + have e4 : r = (a, { s2 with machineState := ms' }) := hr + rw [e4] + exact RfKA.ofKh rfl rfl fun _ h => h + +theorem threadSet_rfA (f : Tcb → Tcb) (t : ObjRef) : PresR RfKA (threadSet (Z := Unit) f t) := by + intro σ r hr + obtain ⟨x, hk, e⟩ := threadSet_memA _ t hr + rw [e] + exact RfKA.upd fun _ _ h => by cases h + +theorem setThreadState_rfA (t : ObjRef) (ts : ThreadState) : PresR RfKA (setThreadState (Z := Unit) t ts) := by + unfold Fl4SpecA64.setThreadState + refine PresR.bind (threadSet_rfA _ t) (fun _ => fun σ r hr => ?_) + rw [setThreadStateAct_eq] at hr + have : r = ((), stsAct σ t) := hr + rw [this] + unfold stsAct + split_ifs + · exact RfKA.ofKh rfl rfl fun _ h => h + · exact PreO.refl σ + +theorem setReplyObjRef_rfA (f : (Option ObjRef → Option ObjRef) → Reply → Reply) (p : ObjRef) (v : Option ObjRef) : + PresR RfKA (setReplyObjRef (Z := Unit) f p v) := by + intro σ r hr + unfold setReplyObjRef updateSkObjRef at hr + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + have ex : σ = x.2 := getSimpleKo_eqPres _ p σ x hx + rw [← ex] at hr + obtain ⟨-, -, hr2⟩ := setSimpleKo_memA _ _ _ hr + rw [hr2] + exact RfKA.upd fun _ _ h => by cases h + +theorem updateSc_rfA (p : ObjRef) (f : SchedContext → SchedContext) + (hf : ∀ x, (f x).scRefills = x.scRefills ∧ (f x).scRefillMax = x.scRefillMax) : + PresR RfKA (updateSchedContext (Z := Unit) p f) := by + intro σ r hr + obtain ⟨y, n, hk, e⟩ := updateSchedContext_memA p f hr + rw [e] + refine RfKA.upd fun y' k h => ?_ + cases h + exact ⟨y, hk, (hf y).1, (hf y).2⟩ + +theorem tcbSchedDeq_khA (t : ObjRef) : ∀ σ : State Unit, ∀ r ∈ (tcbSchedAction (Z := Unit) tcbSchedDequeue t σ).1, + r.2.kheap = σ.kheap := by + intro σ r hr + unfold tcbSchedAction at hr + obtain ⟨d, -, hr⟩ := eqPres_mem (threadGet_eqPres _ t) hr + obtain ⟨pr, -, hr⟩ := eqPres_mem (threadGet_eqPres _ t) hr + have hq : PresR (@Eq (State Unit)) (getTcbQueue (Z := Unit) d pr) := by unfold getTcbQueue; eqpres + obtain ⟨q, -, hr⟩ := eqPres_mem hq hr + obtain ⟨q', hq'', hr⟩ := mem_bindA.1 hr + have eq' : q' = (tcbSchedDequeue t q, σ) := hq'' + subst eq' + have er : r = ((), { σ with readyQueues := fun d' p => + if d' = d ∧ p = pr then tcbSchedDequeue t q else σ.readyQueues d' p }) := hr + rw [er] + +theorem tcbSchedDeq_rfA (t : ObjRef) : PresR RfKA (tcbSchedAction (Z := Unit) tcbSchedDequeue t) := fun σ r hr => by + have K := tcbSchedDeq_sikA (i := σ.idleThread) (M := fun _ => False) (N := fun _ => False) (don := false) t σ r hr + exact RfKA.ofKh (tcbSchedDeq_khA t σ r hr) (by rw [K.frame]) K.rls + +theorem tcbReleaseRemove_khA (t : ObjRef) : ∀ σ : State Unit, ∀ r ∈ (tcbReleaseRemove (Z := Unit) t σ).1, + r.2.kheap = σ.kheap := by + intro σ r hr + unfold tcbReleaseRemove at hr + obtain ⟨qs, hqs, hr⟩ := mem_bindA.1 hr + have e : qs = (σ.releaseQueue, σ) := hqs + subst e + obtain ⟨u, hu, hr⟩ := mem_bindA.1 hr + have hu' : u.2.kheap = σ.kheap := by + unfold NonDet.when at hu + split_ifs at hu + · have : u = ((), { σ with reprogramTimer := true }) := hu + rw [this] + · have : u = ((), σ) := hu + rw [this] + obtain ⟨q', hq', hr⟩ := mem_bindA.1 hr + have eq : q' = (tcbSchedDequeue t σ.releaseQueue, u.2) := hq' + subst eq + have er : r = ((), { u.2 with releaseQueue := tcbSchedDequeue t σ.releaseQueue }) := hr + rw [er] + exact hu' + +theorem tcbReleaseRemove_rfA (t : ObjRef) : PresR RfKA (tcbReleaseRemove (Z := Unit) t) := fun σ r hr => by + have K := tcbReleaseRemove_sikA (i := σ.idleThread) (M := fun _ => False) (N := fun _ => False) (don := false) + t σ r hr + exact RfKA.ofKh (tcbReleaseRemove_khA t σ r hr) (by rw [K.frame]) K.rls + +theorem rescheduleRequired_rfA : PresR RfKA (rescheduleRequired (Z := Unit)) := fun σ r hr => by + have K := rescheduleRequired_sikA (i := σ.idleThread) (M := fun _ => False) (N := fun _ => False) (don := false) + σ r hr + obtain ⟨q, -⟩ := rescheduleRequired_memA hr + exact RfKA.ofKh (by rw [q.1]) (by rw [K.frame]) K.rls + +/-- `sched_context_donate` along any frame of its steps. -/ +theorem schedContextDonate_genA {R : State Unit → State Unit → Prop} [PreO R] + (hE : ∀ {α : Type} {m : SMonad Unit α}, PresR (@Eq (State Unit)) m → PresR R m) + (h1 : ∀ t, PresR R (tcbSchedAction (Z := Unit) tcbSchedDequeue t)) (h2 : ∀ t, PresR R (tcbReleaseRemove (Z := Unit) t)) + (h3 : ∀ t v, PresR R (setTcbObjRef (Z := Unit) tcbSchedContextUpdate t v)) + (h4 : PresR R (rescheduleRequired (Z := Unit))) + (h5 : ∀ p v, PresR R (setScObjRef (Z := Unit) scTcbUpdate p v)) (sc d : ObjRef) : + PresR R (schedContextDonate (Z := Unit) sc d) := by + unfold schedContextDonate + refine PresR.bind (hE (by unfold getScObjRef; eqpres)) (fun f => ?_) + have tail : PresR R (do + setScObjRef scTcbUpdate sc (some d) + setTcbObjRef (Z := Unit) tcbSchedContextUpdate d (some sc)) := PresR.bind (h5 _ _) fun _ => h3 _ _ + refine PresR.bind (PresR.when ?_) (fun _ => tail) + refine PresR.bind (hE (by eqpres)) (fun t => ?_) + refine PresR.bind (h1 _) fun _ => PresR.bind (h2 _) fun _ => PresR.bind (h3 _ _) fun _ => ?_ + unfold testReschedule + refine PresR.bind (PresR.gets _) (fun _ => PresR.bind (PresR.gets _) (fun _ => ?_)) + exact PresR.bind (PresR.pure _) (fun _ => PresR.when h4) + +theorem schedContextDonate_rfA (sc d : ObjRef) : PresR RfKA (schedContextDonate (Z := Unit) sc d) := + schedContextDonate_genA eqPres_rfA tcbSchedDeq_rfA tcbReleaseRemove_rfA (fun t _ => threadSet_rfA _ t) + rescheduleRequired_rfA (fun p _ => updateSc_rfA p _ fun _ => ⟨rfl, rfl⟩) sc d + +/-! ## The TCB states are kept -/ + +/-- Every TCB's state is kept (backwards). -/ +def StKA (σ σ' : State Unit) : Prop := + ∀ q y, σ'.kheap q = some (.tcb y) → ∃ x, σ.kheap q = some (.tcb x) ∧ y.tcbState = x.tcbState + +instance stkPreOA : PreO StKA := + ⟨fun _ _ y h => ⟨y, h, rfl⟩, fun h1 h2 q z h => by + obtain ⟨y, hy, e⟩ := h2 q z h + obtain ⟨x, hx, e'⟩ := h1 q y hy + exact ⟨x, hx, e.trans e'⟩⟩ + +theorem StKA.ofKh {σ σ' : State Unit} (hk : σ'.kheap = σ.kheap) : StKA σ σ' := + fun _ y h => ⟨y, by rw [← hk]; exact h, rfl⟩ + +theorem StKA.upd {σ : State Unit} {p : ObjRef} {o' : KernelObject} + (h : ∀ y, o' = .tcb y → ∃ x, σ.kheap p = some (.tcb x) ∧ y.tcbState = x.tcbState) : + StKA σ { σ with kheap := Function.update σ.kheap p (some o') } := by + intro q y hq + change Function.update σ.kheap p (some o') q = _ at hq + by_cases e : q = p + · subst e + rw [Function.update_self] at hq + exact h y (Option.some.inj hq) + · rw [Function.update_of_ne e] at hq + exact ⟨y, hq, rfl⟩ + +theorem eqPres_stkA {α : Type} {m : SMonad Unit α} (h : PresR (@Eq (State Unit)) m) : PresR StKA m := + fun s x hx => by rw [← h s x hx]; exact PreO.refl s + +theorem updateSc_stkA (p : ObjRef) (f : SchedContext → SchedContext) : + PresR StKA (updateSchedContext (Z := Unit) p f) := by + intro σ r hr + obtain ⟨y, n, hk, e⟩ := updateSchedContext_memA p f hr + rw [e] + exact StKA.upd fun _ h => by cases h + +theorem schedContextDonate_stkA (sc d : ObjRef) : PresR StKA (schedContextDonate (Z := Unit) sc d) := by + refine schedContextDonate_genA eqPres_stkA (fun t s x hx => ?_) (fun t s x hx => ?_) (fun t v s x hx => ?_) + (fun s x hx => ?_) (fun p _ => updateSc_stkA p _) sc d + · exact StKA.ofKh (tcbSchedDeq_khA t s x hx) + · exact StKA.ofKh (tcbReleaseRemove_khA t s x hx) + · obtain ⟨y, hk, e⟩ := threadSet_memA _ t hx + rw [e] + exact StKA.upd fun z h => by cases h; exact ⟨y, hk, rfl⟩ + · obtain ⟨q, -⟩ := rescheduleRequired_memA hx + exact StKA.ofKh (by rw [q.1]) + +theorem bindScReply_stkA (sc r : ObjRef) : PresR StKA (bindScReply (Z := Unit) sc r) := by + have ro : ∀ (f : (Option ObjRef → Option ObjRef) → Reply → Reply) p v, + PresR StKA (setReplyObjRef (Z := Unit) f p v) := fun f p v σ x hx => by + unfold setReplyObjRef updateSkObjRef at hx + obtain ⟨y, hy, hx⟩ := mem_bindA.1 hx + have ey : σ = y.2 := getSimpleKo_eqPres _ p σ y hy + rw [← ey] at hx + obtain ⟨-, -, e⟩ := setSimpleKo_memA _ _ _ hx + rw [e] + exact StKA.upd fun _ h => by cases h + unfold bindScReply + refine PresR.bind (eqPres_stkA ?_) (fun l => ?_) + · show PresR (@Eq (State Unit)) (NonDet.liftM SchedContext.scReplies (getSchedContext (Z := Unit) sc)) + unfold NonDet.liftM + exact PresR.bind (getSchedContext_eqPres sc) (fun _ => PresR.pure _) + have rest : PresR StKA (do + setScObjRef scRepliesUpdate sc (r :: l) + setReplyObjRef (Z := Unit) replyScUpdate r (some sc)) := + PresR.bind (updateSc_stkA sc _) fun _ => ro _ _ _ + cases l with + | nil => exact rest + | cons a l => exact PresR.bind (ro _ _ _) (fun _ => rest) + +theorem StIs.stk {t : ObjRef} {ts : ThreadState} {σ σ' : State Unit} (h : StIs t ts σ) (k : StKA σ σ') : + StIs t ts σ' := fun y hy => by + obtain ⟨x, hx, e⟩ := k t y hy + rw [e]; exact h x hx + +theorem bindScReply_rfA (sc r : ObjRef) : PresR RfKA (bindScReply (Z := Unit) sc r) := by + unfold bindScReply + refine PresR.bind (eqPres_rfA ?_) (fun l => ?_) + · show PresR (@Eq (State Unit)) (NonDet.liftM SchedContext.scReplies (getSchedContext (Z := Unit) sc)) + unfold NonDet.liftM + exact PresR.bind (getSchedContext_eqPres sc) (fun _ => PresR.pure _) + have rest : PresR RfKA (do + setScObjRef scRepliesUpdate sc (r :: l) + setReplyObjRef (Z := Unit) replyScUpdate r (some sc)) := + PresR.bind (updateSc_rfA sc _ fun _ => ⟨rfl, rfl⟩) fun _ => setReplyObjRef_rfA _ _ _ + cases l with + | nil => exact rest + | cons a l => exact PresR.bind (setReplyObjRef_rfA _ _ _) (fun _ => rest) + +theorem replyPush_rfA (t d r : ObjRef) (cd : Bool) : PresR RfKA (replyPush (Z := Unit) t d r cd) := by + unfold replyPush + refine PresR.bind (eqPres_rfA (by unfold getTcbObjRef; exact threadGet_eqPres _ _)) (fun a => ?_) + refine PresR.bind (eqPres_rfA (by unfold getTcbObjRef; exact threadGet_eqPres _ _)) (fun b => ?_) + refine PresR.bind (setReplyObjRef_rfA _ _ _) (fun _ => ?_) + refine PresR.bind (setThreadState_rfA _ _) (fun _ => ?_) + exact PresR.when (PresR.bind (bindScReply_rfA _ _) fun _ => schedContextDonate_rfA _ _) + +theorem replyUnlinkTcb_rfA (t r : ObjRef) : PresR RfKA (replyUnlinkTcb (Z := Unit) t r) := by + unfold replyUnlinkTcb + refine PresR.bind (eqPres_rfA (getReply_eqPres r)) (fun _ => PresR.bind (PresR.assert _) (fun _ => ?_)) + refine PresR.bind (eqPres_rfA ?_) (fun _ => PresR.bind (PresR.assert _) (fun _ => ?_)) + · unfold getThreadState; exact threadGet_eqPres _ _ + exact PresR.bind (setReplyObjRef_rfA _ _ _) (fun _ => setThreadState_rfA t _) + +section +open fl4_core_a64 Fl4ProofsA64.P1 +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- `do_ipc_transfer` (no fault, no extra caps) keeps the refills. -/ +theorem doIpcTransfer_rfA {th : TypedHeapA} {kb : U64} (hPl : PlaceA th kb) (sf : S) {σ : State Unit} + (hR : StateRelA th (L.mem sf) σ) (hV : SlotsValidA th (L.mem sf)) {snd rcv : U64} + (htr : th rcv = some .tcb) {xs xr : Tcb} (hks : σ.kheap (specPtr snd) = some (.tcb xs)) + (hkr : σ.kheap (specPtr rcv) = some (.tcb xr)) (hf : xs.tcbFault = none) + (hal : ∀ rb, ipcBufOfA true xr = some rb → (bufAddr rb 0).toNat % 8 = 0) + (ep : Option ObjRef) (badge : Badge) (grant : Bool) + (hcaps : grant = false ∨ ipcBufOfA false xs = none ∨ + unat (dataToMessageInfo (xs.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0) : + ∀ x ∈ (doIpcTransfer (Z := Unit) (specPtr snd) ep badge grant (specPtr rcv) σ).1, RfKA σ x.2 := by + let B := BufBytesA (ipcBufOfA true xr) + let R : State Unit → State Unit → Prop := fun a b => UFrA B a b ∧ RfKA a b + haveI : PreO R := ⟨fun s => ⟨PreO.refl s, PreO.refl s⟩, + fun h1 h2 => ⟨PreO.trans h1.1 h2.1, PreO.trans h1.2 h2.2⟩⟩ + have key := doIpcTransfer_genA (R := R) (B := B) (fun _ _ h => h.1) + (fun t f s y hy => ⟨UFrA.of_fr (asUser_presA t f s y hy), asUser_rfA t f s y hy⟩) + (fun a s y hy => ⟨UFrA.of_fr (doMachineOp_presA _ (loadWord_keepA a) s y hy), doMachineOp_rfA _ s y hy⟩) + (rbo := ipcBufOfA true xr) (fun rb e k hk v s y hy => by + refine ⟨by show UFrA (BufBytesA (ipcBufOfA true xr)) _ _; rw [e]; exact storeWordOffs_ufrA rb hk v s y hy, ?_⟩ + unfold storeWordOffs at hy + exact (PresR.bind (R := RfKA) PresR.get fun _ => PresR.bind (PresR.assert _) fun _ => + doMachineOp_rfA _) s y hy) + hPl sf hR hV htr hks hkr rfl hf hal ep badge grant hcaps + exact fun x hx => (key σ rfl x hx).2 + +/-- **The receiver's `RelIfBoundSc` after the donating push** (module docstring). -/ +theorem relIfBoundSc_donPushA {th : TypedHeapA} {kb : U64} (hPl : PlaceA th kb) (sf : S) {σ : State Unit} + (hR : StateRelA th (L.mem sf) σ) (hV : SlotsValidA th (L.mem sf)) {t d : U64} {e : ObjRef} {z : Endpoint} + (htr : th d = some .tcb) (hk : σ.kheap e = some (.endpoint z)) (z' : Endpoint) + {xt xd : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) (hkd : σ.kheap (specPtr d) = some (.tcb xd)) + (hf : xt.tcbFault = none) (hal : ∀ rb, ipcBufOfA true xd = some rb → (bufAddr rb 0).toNat % 8 = 0) + (htd : specPtr t ≠ specPtr d) (hd0 : xd.tcbSchedContext = none) {sc : ObjRef} + (hsc : xt.tcbSchedContext = some sc) (hD : DonA σ (specPtr t) xt) + (hti : specPtr t ≠ σ.idleThread) (hdi : specPtr d ≠ σ.idleThread) + (hrl : specPtr t ∉ σ.releaseQueue) (hRB : RelIfBound σ (specPtr t)) + (badge : Badge) (g : Bool) + (hcaps : g = false ∨ ipcBufOfA false xt = none ∨ + unat (dataToMessageInfo (xt.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0) (r : ObjRef) : + ∀ σ2 σ3 σ4, + doIpcTransfer (Z := Unit) (specPtr t) (some e) badge g (specPtr d) + (updK σ e (.endpoint z')) = ({((), σ2)}, False) → + replyUnlinkTcb (Z := Unit) (specPtr d) r σ2 = ({((), σ3)}, False) → + replyPush (Z := Unit) (specPtr t) (specPtr d) r true σ3 = ({((), σ4)}, False) → + RelIfBoundSc σ4 (specPtr d) := by + intro σ2 σ3 σ4 hT hU hP + set σ0 := updK σ e (.endpoint z') with hσ0 + have hte : specPtr t ≠ e := fun h => by rw [← h, hkt] at hk; cases hk + have hde : specPtr d ≠ e := fun h => by rw [← h, hkd] at hk; cases hk + have hkt0 : σ0.kheap (specPtr t) = some (.tcb xt) := by rw [hσ0, updK_kheap, Function.update_of_ne hte, hkt] + have hkd0 : σ0.kheap (specPtr d) = some (.tcb xd) := by rw [hσ0, updK_kheap, Function.update_of_ne hde, hkd] + have HS : SpecSchedA σ σ0 := by + refine ⟨fun p => ?_, rfl, rfl, rfl, rfl, rfl, rfl⟩ + by_cases h : p = e + · subst h + exact Or.inr (Or.inr ⟨_, _, hk, by rw [hσ0, updK_kheap, Function.update_self], + Or.inr (Or.inr (Or.inl ⟨_, _, rfl, rfl⟩))⟩) + · exact Or.inl (by rw [hσ0, updK_kheap, Function.update_of_ne h]) + have R0 : RfKA σ σ0 := RfKA.upd fun _ _ h => by cases h + have m2 : ((), σ2) ∈ (doIpcTransfer (Z := Unit) (specPtr t) (some e) badge g (specPtr d) σ0).1 := by + rw [hT]; exact Set.mem_singleton _ + have m3 : ((), σ3) ∈ (replyUnlinkTcb (Z := Unit) (specPtr d) r σ2).1 := by rw [hU]; exact Set.mem_singleton _ + have m4 : ((), σ4) ∈ (replyPush (Z := Unit) (specPtr t) (specPtr d) r true σ3).1 := by + rw [hP]; exact Set.mem_singleton _ + have R2 : RfKA σ0 σ2 := doIpcTransfer_rfA hPl sf (hR.specSchedA HS) hV htr hkt0 hkd0 hf hal _ _ _ hcaps _ m2 + have R3 : RfKA σ2 σ3 := replyUnlinkTcb_rfA _ _ _ _ m3 + have R4 : RfKA σ3 σ4 := replyPush_rfA _ _ _ _ _ _ m4 + have RF : RfKA σ σ4 := PreO.trans R0 (PreO.trans R2 (PreO.trans R3 R4)) + -- the SC bindings before the push + have K2 : SIKA σ.idleThread (fun p => p = specPtr d) (fun _ => False) false σ0 σ2 := + doIpcTransfer_sikA hPl sf (hR.specSchedA HS) hV htr hkt0 hkd0 hf hal _ _ _ hcaps σ0 rfl _ m2 + have K3 : SIKA σ.idleThread (fun p => p = specPtr d) (fun _ => False) false σ2 σ3 := + replyUnlinkTcb_sikA (i := σ.idleThread) (M := fun p => p = specPtr d) (N := fun _ => False) (don := false) + (specPtr d) r rfl _ _ m3 + have K := K2.trans K3 + obtain ⟨xt3, hkt3, gt⟩ := K.tcbF hkt0 + obtain ⟨xd3, hkd3, gd⟩ := K.tcbF hkd0 + have et : xt3.tcbSchedContext = some sc := (gt.sc.resolve_right fun c => by cases c.1).trans hsc + have ed : xd3.tcbSchedContext = none := (gd.sc.resolve_right fun c => by cases c.1).trans hd0 + -- the push binds the sender's SC to the receiver + have bound : ∀ y, σ4.kheap (specPtr d) = some (.tcb y) → y.tcbSchedContext = some sc := by + have H : HT (fun s => s = σ3) (replyPush (Z := Unit) (specPtr t) (specPtr d) r true) + (fun _ s => ∀ y, s.kheap (specPtr d) = some (.tcb y) → y.tcbSchedContext = some sc) := by + unfold replyPush + refine HT.bind (Q := fun a s => s = σ3 ∧ a = some sc) (fun s h x hx => ?_) fun a => ?_ + · subst h + unfold getTcbObjRef at hx + rw [threadGet_valA _ hkt3 hx] + exact ⟨rfl, et⟩ + refine HT.bind (Q := fun b s => a = some sc ∧ b = none) (fun s h x hx => ?_) fun b => ?_ + · obtain ⟨rfl, ha⟩ := h + unfold getTcbObjRef at hx + rw [threadGet_valA _ hkd3 hx] + exact ⟨ha, ed⟩ + refine HT.bind (Q := fun _ _ => a = some sc ∧ b = none) (fun _ h _ _ => h) fun _ => ?_ + refine HT.bind (Q := fun _ _ => a = some sc ∧ b = none) (fun _ h _ _ => h) fun _ => ?_ + intro s ⟨ha, hb⟩ x hx + subst ha hb + unfold NonDet.when at hx + rw [if_pos (by simp)] at hx + obtain ⟨u, -, hx⟩ := mem_bindA.1 hx + exact donate_bindsA sc (specPtr d) u.2 trivial x hx + exact H σ3 rfl _ m4 + intro x scp y n hx hscp hy ha _ + have e1 : scp = sc := Option.some.inj (hscp.symm.trans (bound x hx)) + subst e1 + obtain ⟨y0, hy0, er, em⟩ := RF.2.2 _ _ _ hy + have a0 : scActive y0 = true := by unfold scActive at ha ⊢; rw [← em]; exact ha + have rel := hRB xt scp y0 n hkt hsc hy0 a0 hrl + unfold releasedSc + unfold released at rel + have eh : refillHd y = refillHd y0 := by unfold refillHd; rw [er] + rw [RF.1, eh] + exact rel + +end + +/-! ## The sender after the push -/ + +/-- **After a call with a reply push the sender is `BlockedOnReply`** (every outcome). -/ +theorem sendIpc_push_senderA {σ : State Unit} {t d e r : ObjRef} {q : List ObjRef} + (hk : σ.kheap e = some (.endpoint (.recvEP (d :: q)))) {xd : Tcb} (hkd : σ.kheap d = some (.tcb xd)) + {e' : ObjRef} {dd : ReceiverPayload} (hst : xd.tcbState = .blockedOnReceive e' (some r) dd) + (htd : t ≠ d) (blk : Bool) (badge : Badge) (g gr cd : Bool) (hg : g = true ∨ gr = true) : + ∀ x ∈ (sendIpc (Z := Unit) blk true badge g gr cd t e σ).1, StIs t (.blockedOnReply r) x.2 := by + have skip : ∀ {α β : Type} {m : SMonad Unit α} {f : α → SMonad Unit β} {Q : β → State Unit → Prop}, + (∀ a, HT (fun _ => True) (f a) Q) → HT (fun _ => True) (m >>= f) Q := + fun h => HT.bind (Q := fun _ _ => True) (fun _ _ _ _ => trivial) h + let B : State Unit → Prop := StIs t (.blockedOnReply r) + have tail : HT B (do + setThreadState (Z := Unit) d .running + let destSc ← getTcbObjRef Tcb.tcbSchedContext d + ifSporadicCurScTestRefillUnblockCheck destSc + possibleSwitchTo d) (fun _ σ => B σ) := by + refine HT.bind (Q := fun _ σ => B σ) ?_ fun _ => ?_ + · unfold Fl4SpecA64.setThreadState + refine HT.bind (Q := fun _ σ => B σ) (fun σ h r hr y hy => ?_) fun _ σ h r hr => ?_ + · obtain ⟨x, -, e⟩ := threadSet_memA _ d hr + rw [e] at hy + exact h y (by simpa only [Function.update_of_ne htd] using hy) + · rw [setThreadStateAct_eq] at hr + have e : r = ((), stsAct σ d) := hr + rw [e] + exact fun y hy => h y (by rwa [stsAct_kheap] at hy) + refine HT.bind (Q := fun _ σ => B σ) (fun σ h r hr => by + have e : r.2 = σ := ((threadGet_eqPres _ _) σ r hr).symm + rw [e]; exact h) fun sc => ?_ + refine HT.bind (Q := fun _ σ => B σ) (fun σ h r hr => + (StIs.frame t _).keep σ _ ((by unfold ifSporadicCurScTestRefillUnblockCheck ifCondRefillUnblockCheck; tpres : + TPres (ifSporadicCurScTestRefillUnblockCheck (Z := Unit) sc)) σ r hr) h) fun _ => ?_ + intro σ h r hr y hy + exact h y (((KPresInst.pres : PresR TcbCellsEq (possibleSwitchTo (Z := Unit) d)) σ r hr).2 _ _ |>.2 hy) + have push : ∀ r', r' = r → HT (fun _ => True) (replyPush (Z := Unit) t d r' cd) (fun _ σ => B σ) := by + intro r' er + subst er + unfold replyPush + refine skip fun _ => skip fun _ => skip fun _ => ?_ + refine HT.bind (Q := fun _ σ => B σ) ?_ fun _ => ?_ + · unfold Fl4SpecA64.setThreadState + refine HT.bind (Q := fun _ σ => B σ) (fun σ _ z hz y hy => ?_) fun _ σ h z hz => ?_ + · obtain ⟨x, -, e⟩ := threadSet_memA _ t hz + rw [e] at hy + have : ({ x with tcbState := .blockedOnReply r' } : Tcb) = y := by simpa using hy + rw [← this] + · rw [setThreadStateAct_eq] at hz + have e : z = ((), stsAct σ t) := hz + rw [e] + exact fun y hy => h y (by rwa [stsAct_kheap] at hy) + refine HT.when (fun _ => ?_) (fun _ _ h => h) + refine HT.bind (Q := fun _ σ => B σ) (fun σ h z hz => h.stk (bindScReply_stkA _ _ σ z hz)) fun _ => ?_ + intro σ h z hz + exact h.stk (schedContextDonate_stkA _ _ σ z hz) + suffices H : HT (fun s => s = σ) (sendIpc (Z := Unit) blk true badge g gr cd t e) (fun _ s => B s) from + fun x hx => H σ rfl x hx + unfold sendIpc + intro s hs x hx + subst hs + obtain ⟨y, hy, hx'⟩ := mem_bindA.1 hx + clear hx + rw [getEndpoint_eq hk] at hy + have ey : y = (.recvEP (d :: q), s) := hy + subst ey + revert x hx' + cases blk <;> dsimp only + all_goals + intro x hx + refine (?_ : HT (fun σ' => σ' = s) _ (fun _ s => B s)) s rfl x hx + refine HT.bind (Q := fun _ s1 => TcbKeepA s s1) (fun σ' h z hz => by subst h; exact TKInst.pres σ' z hz) fun _ => ?_ + refine HT.bind (Q := fun rs _ => rs = .blockedOnReceive e' (some r) dd) (fun σ' h z hz => ?_) fun rs => ?_ + · have hz' : z = (xd.tcbState, σ') := by + unfold getThreadState at hz + exact threadGet_valA _ (h _ _ hkd) hz + subst hz' + exact hst + intro σ' ers z hz + subst ers + refine (?_ : HT (fun _ => True) _ (fun _ s => B s)) σ' trivial z hz + dsimp only + refine HT.bind (Q := fun a _ => a = some r) (fun _ _ z hz => by + have e : z.1 = some r := congrArg Prod.fst (show z = (some r, _) from hz) + exact e) fun reply => ?_ + intro σ' er z hz + subst er + refine (?_ : HT (fun _ => True) _ (fun _ s => B s)) σ' trivial z hz + refine skip fun _ => skip fun _ => skip fun _ => skip fun fault => ?_ + rw [if_pos (Or.inl rfl), if_pos ⟨hg, by simp⟩] + exact HT.bind (push _ rfl) fun _ => tail + +end + +end Fl4ProofsA64.P3.Sched + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {Z : XSetsA} {E Rp : Set U64} + {Bf : Set (U64 × Word)} {m : McState} {σ : State Unit} {s : S} + +/-- **The donating write set is `IpcFpA` or a reply object's word** (module docstring). -/ +theorem donFpA_splitA (hP : TcbPlaced Z.T) {t e d b : U64} (ht : t ∈ Z.T) (he : e ∈ E) (hd : d ∈ Z.T) + (h : DonFpA st Z m Rp Bf σ s t e d b) : IpcFpA st Z E Rp Bf m σ s b ∨ RepWA Rp b := by + have tw : ∀ u ∈ Z.T, ∀ c : U64, Aligned c ∧ TF u c → TWOffN (c.val - u.val) → IpcFpA st Z E Rp Bf m σ s c := + fun u hu c h w => Or.inl (Or.inr (Or.inr (Or.inl ⟨u, hu, Or.inl ⟨h.2.1, w⟩⟩))) + have stw : ∀ u ∈ Z.T, IpcFpA st Z E Rp Bf m σ s (stA u) := fun u hu => by + refine tw u hu _ (a_st (hP u hu)) ?_ + rw [val_st (hP u hu), Nat.add_sub_cancel_left]; unfold TWOffN; simp + rcases h with (h | ⟨r, hr, h⟩ | ⟨-, h⟩ | h | h) | h | h + · rcases h with h | ⟨w, hw, -, h⟩ | ⟨r, h⟩ | h + · exact Or.inl (Or.inl (Or.inl ⟨e, he, h⟩)) + · exact Or.inl (Or.inl (Or.inr (Or.inr (Or.inl ⟨w, hw, Or.inr h⟩)))) + · have hv := regA_val (hP d hd) (regIdxA_lt r) + have := regIdxA_lt r + refine Or.inl (Or.inl (Or.inr (Or.inr (Or.inl ⟨d, hd, Or.inl ⟨by omega, ?_⟩⟩)))) + unfold TWOffN; omega + · exact Or.inl (Or.inl (Or.inr (Or.inr (Or.inr h)))) + · exact Or.inl (Or.inl (Or.inr (Or.inl ⟨r, hr, by rw [h, Endp.rTcbA_val]⟩))) + · rw [h]; exact Or.inl (stw t ht) + · rw [h]; exact Or.inl (stw d hd) + · exact Or.inl (Or.inr h) + · exact Or.inr h + · rw [h] + refine Or.inl (tw t ht _ (a_reply (hP t ht)) ?_) + rw [val_reply (hP t ht), Nat.add_sub_cancel_left]; unfold TWOffN; simp + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3SendErrAllA.lean b/proofs/Fl4ProofsA64/P3/Stage3SendErrAllA.lean new file mode 100644 index 000000000..f65746eb8 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SendErrAllA.lean @@ -0,0 +1,114 @@ +/- +P3 (aarch64, A64-P3 Stage 3 assembly): **`sendBodyErrA` with `hSpec` discharged by `decErrSpecA`**. + +`JSyscallA.sendBodyErrAllA`: `sendBodyErrA` where the [J] premise `hSpec` (`DecErrSpecA` at every `calling` / +`blocking` / `donate`) is replaced by `JSyscallA.decErrSpecA`'s own premises (listed in that module's docstring; +none new, all taken at the reached state `s`, none `∀ state`). `hKeep` is unchanged. New module. +-/ +import Fl4ProofsA64.P3.Stage3DecErrAllA +import Fl4ProofsA64.P3.Stage3ReplyErrTopA +import Fl4ProofsA64.P3.Stage3HPreA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.ReplyErr +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3 Fl4ProofsA64.P3.Endp + Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4ProofsA64.P3.Vcpu + Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {Clock : A → Fl4SpecA64.MachineState → Prop} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {ar : machine.Arch A} {nA : U64} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- **Send, NBSend and Call with the decode errors closed from `decErrSpecA`** (module docstring). -/ +theorem _root_.Fl4ProofsA64.P3.JSyscallA.sendBodyErrAllA [PlainStore st] (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (hCnt : Sched.CounterStepA σ) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) (hIP : IrqPlatA st ar) {pending : SMonad Unit Unit} (call : Fl4SpecA64.Syscall) + (hc : call = .sysSend ∨ call = .sysNBSend ∨ call = .sysCall) + (hFault : ∀ calling blocking donate, + DispatchInvokeAtA (L.mem s (regWA (L.curW s) layout.reg.CAP)) (L.mem s (regWA (L.curW s) layout.reg.REPLY)) + (L.mem s (regWA (L.curW s) layout.reg.NBSEND_RECV_DEST)) call calling blocking donate false + (L.mem s (regWA (L.curW s) layout.reg.CAP)) → + blocking = true → ∀ (f : error.Fault) (f' : Fl4SpecA64.Fault), CapFaultCorrA f f' → + faults.handle_fault st ar s a (L.curW s) f ⦃ r => ∃ τ, ((), τ) ∈ (handleFault (Z := Unit) σ.curThread f' σ).1 ∧ + A64RelS3 st AR ar hPl.ipa n Z.Cs r.1 r.2 τ m ⦄) + (hArm : ∀ calling blocking donate, + DispatchInvokeAtA (L.mem s (regWA (L.curW s) layout.reg.CAP)) (L.mem s (regWA (L.curW s) layout.reg.REPLY)) + (L.mem s (regWA (L.curW s) layout.reg.NBSEND_RECV_DEST)) call calling blocking donate false + (L.mem s (regWA (L.curW s) layout.reg.CAP)) → + ∀ (info : msginfo.MessageInfo) (buf : U64) (r1 : cspace.Resolved) (e : Aeneas.Std.Array obj.Slot 3#usize) + (mi : Fl4SpecA64.MessageInfo) (sl : Fl4SpecA64.CslotPtr) (c : Fl4SpecA64.Cap) + (excaps : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) (buffer : Option Fl4SpecA64.ObjRef) (argsU : List U64), + msg.msg_info st s (L.curW s) = ok info → msg.lookup_ipc_buffer st s false (L.curW s) = ok buf → + cspace.lookup_slot st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.CAP)) = ok (.Ok r1) → + ipc.lookup_extra_caps st s (L.curW s) buf info = ok (.Ok e) → + SiteA st Z OS s σ info buf r1.slot e mi sl c excaps buffer argsU → + ∀ (r0 : core.result.Result Unit error.Exc) (s0 : S) (a0 : A), + syscall.decode_invocation st ar s a info.label (msgLenA info buf) r1.slot (capA st s r1.slot) blocking + calling donate false buf e = ok (r0, s0, a0) → (∀ x, r0 = .Err x → x = error.Exc.Preempted) → + ∃ Out, DispScopeA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ m Clock hPl.ipa info.label + (msgLenA info buf) r1.slot blocking calling donate false buf e Out) + (K : ArchKeepsA ar AR) + (hT : ScCfgTimeAgreeA ar a) (hP : VcpuPlatAgreeA ar) (hAK : VcpuArchKeepsA ar AR) + (hfan : Fl4SpecA64.retypeFanOutLimit = 256#64) + (hv : ∃ vi, ar.virtualization a = ok vi ∧ (vi = 0#u64 ↔ fl4A64Virt = false)) + (hIpa : S2IpaE ar a) (hnum : numDomains = 1) + (hCs : ∀ c ∈ Z.Cs, c.val < Fl4SpecA64.fl4A64Platform.numNodes) (hCsN : ∀ c ∈ Z.Cs, c.val < n) + (h1 : Fl4SpecA64.fl4A64Platform.numNodes = 1) + (hsb : ∀ x0 : U64, x0 ≠ 0#u64 → ∀ p sb, capAtA (L.mem s) x0 = .SchedContext p sb → sb.val < 64) + (hYAs : ∀ y ∈ Z.Y, ∀ j : U64, j.val < 512 → ¬ AsW (L.ksb s) (peAF y j)) + (hFI : FinInvA Z.th (L.mem s) σ Z.G) (hcO : ∀ y ∈ OS, obj.sext48 y = ok y) + (hD : ∀ u frame fi, capA st s u = .Untyped frame 12#u64 fi false → + cdt.ensure_no_children st s u = ok (core.result.Result.Ok ()) → DeadDrop4KA Z H frame fi) + (hPtMap : ∀ (cap : capability.Cap) (lbl ptr : U64) (mp : Bool) (asid va : U64), cap = .PageTable ptr mp asid va → + lbl = fl4_abi.label.ARMPageTableMap → + UnmPtA Z.S1 Z.Y OS H.tcbs (L.mem s) σ ptr ∧ ptr ∉ S2PgE Z.X Z.T2 ∧ ptr ∉ Z.Y) + (hFrMap : ∀ (cap : capability.Cap) (lbl fp : U64) (sz : capability.FrameSize) (rt : U64) (dev mp e : Bool) + (asid va : U64), cap = .Frame fp sz rt dev mp e asid va → lbl = fl4_abi.label.ARMPageMap → FrPlA sz fp mp asid) + (hFrSub : ∀ (cap : capability.Cap) (lbl : U64) (cte : CslotPtr) (fp : U64) (sz : capability.FrameSize) (rt : U64) + (dev mp e : Bool) (asid va : U64), cap = .Frame fp sz rt dev mp e asid va → + lbl = fl4_abi.label.ARMPageDeriveSubframe → sz = capability.FrameSize.Large → dev = false → + (fp.val % 2 ^ 21 = 0 ∧ CapRt.Canonical fp) ∧ + ∀ w ∈ Z.G.chains.flatten, ∀ (q vm' : U64), fp.val ≤ q.val → q.val < fp.val + 2 ^ 21 → + OvlA (absCapA (.Frame q .Small vm' false false false 0#u64 0#u64)) (absCapA (capAtA (L.mem s) w)) → + isFrameA (capAtA (L.mem s) w) = true ∧ ∃ ch ∈ Z.G.chains, cslotAddrA cte ∈ ch ∧ w ∈ ch) + (hAsidAssign : ∀ (cap : capability.Cap) (lbl p base : U64), cap = .AsidPool p base → + lbl = fl4_abi.label.ARMASIDPoolAssign → + ∀ u ∈ OS, ∀ k root oldAsid oldVa, capA st s u = assignUnmappedA k root oldAsid oldVa → + UnmRootAssignA k (L.mem s) σ root) + (hNoS2 : ∀ info buf (e : Array obj.Slot 3#usize), msg.msg_info st s (L.curW s) = ok info → + msg.lookup_ipc_buffer st s false (L.curW s) = ok buf → + ipc.lookup_extra_caps st s (L.curW s) buf info = ok (.Ok e) → + ∀ p sz r d mp ept asid va, capA st s (exAtA e 0) = .Frame p sz r d mp ept asid va → ept = false) + (hAgreeWS : ∀ info buf (e : Array obj.Slot 3#usize), msg.msg_info st s (L.curW s) = ok info → + msg.lookup_ipc_buffer st s false (L.curW s) = ok buf → + ipc.lookup_extra_caps st s (L.curW s) buf info = ok (.Ok e) → + ∀ p sz r mp ept asid va off mask, capA st s (exAtA e 0) = .Frame p sz r false mp ept asid va → + msg.syscall_arg st s (L.curW s) buf 0#u64 = ok off → msg.syscall_arg st s (L.curW s) buf 1#u64 = ok mask → + mask ≠ 0#u64 → FrameInputAgreeA st s σ (wa p off)) + (hAgreeRun : ∀ buf, msg.lookup_ipc_buffer st s false (L.curW s) = ok buf → + ∀ p sz r mp ept asid va off mask, capA st s (sbWordA (L.curW s)) = .Frame p sz r false mp ept asid va → + msg.syscall_arg st s (L.curW s) buf 0#u64 = ok off → msg.syscall_arg st s (L.curW s) buf 1#u64 = ok mask → + mask ≠ 0#u64 → FrameInputAgreeA st s σ (wa p off)) + (hKeep : ∀ calling blocking donate, + DecErrKeepA st ar AR Z s a blocking calling donate (L.mem s (regWA (L.curW s) layout.reg.CAP)) (fun _ => True)) + (XP : ExitPlatA st ar Z) (HEnv : HPreEnvA st AR ar hPl n Z.Cs m Clock pending s a call) : + syscall.dispatch_syscall st ar s a (syscallNumberA call) ⦃ r => + StepPostA (fun s a τ => A64RelS3 st AR ar hPl.ipa n Z.Cs s a τ m) + (fl4SettleDispatch pending (fl4DispatchSyscall (fl4HandleInvocationR (Z := Unit) true) call)) σ r ⦄ := + J.sendBodyErrA (hCnt := hCnt) hA hM hPl hfm hIP call hc hFault hArm + (fun _ _ _ => J.decErrSpecA hA hM hPl K hIP hT hP hAK hfm hfan hv hIpa hnum hCs hCsN h1 hsb hYAs hFI hcO hD hPtMap hFrMap + hFrSub hAsidAssign hNoS2 hAgreeWS hAgreeRun) hKeep XP HEnv + +end + +end + +end Fl4ProofsA64.P3.ReplyErr diff --git a/proofs/Fl4ProofsA64/P3/Stage3SendFaultSpecA.lean b/proofs/Fl4ProofsA64/P3/Stage3SendFaultSpecA.lean new file mode 100644 index 000000000..982c73320 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SendFaultSpecA.lean @@ -0,0 +1,336 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2b-2): **the spec side of `send_ipc` from a faulted sender to a waiting +receiver** (the fault message replaces the message registers), the siblings of `sendIpc_recv_sikA` / +`sendIpc_recv_ufrA` that kernel entry needs for `handle_fault` and `handle_timeout`. + +* `setMrs_genA`, `makeFaultMsg_genA`, **`doFaultTransfer_genA`**: `do_fault_transfer` of every fault kind along any + frame `R` that contains the receiver's register-context write, the buffer's word stores (words below 128) and + `sched_context_update_consumed` (the timeout message of a sender with an SC). +* **`XferCaseA`**: the sender has no fault and no extra caps (`do_ipc_transfer`'s old case), or has a fault. + **`doIpcTransfer_any_ufrA`** / **`_sikA`**: both cases. +* **`sendIpc_recv_ufrFA`** / **`sendIpc_recv_sikFA`**: every outcome of `send_ipc` to a waiting receiver is `UFrA` of + the receiver's buffer, and `SIKA`; without donation (`don = false`) only needs `cd = false`, a reply push then + changing no SC. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3IpcKeepSA +import Fl4ProofsA64.P3.Stage3McsFrA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P3 Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Classical + +noncomputable section + +theorem PresR.zipWithMx_memA {σ α β γ : Type} {R : σ → σ → Prop} [PreO R] {f : α → β → NonDet σ γ} : + ∀ (l : List α) (l' : List β), (∀ a ∈ l, ∀ b, PresR R (f a b)) → PresR R (NonDet.zipWithMx f l l') + | [], _, _ => by unfold NonDet.zipWithMx; exact PresR.pure _ + | _ :: _, [], _ => by unfold NonDet.zipWithMx; exact PresR.pure _ + | a :: l, b :: l', h => by + show PresR R (NonDet.bind (f a b) fun _ => NonDet.zipWithMx f l l') + exact PresR.bind (h a (by simp) b) fun _ => PresR.zipWithMx_memA l l' fun x hx => h x (by simp [hx]) + +/-- **`set_mrs`** along any frame of the register-context write and the buffer's word stores. -/ +theorem setMrs_genA {R : State Unit → State Unit → Prop} [PreO R] (t : ObjRef) (buf : Option ObjRef) + (msgs : List Message) + (hT : ∀ s (x : Tcb) regs, s.kheap t = some (.tcb x) → + R s { s with kheap := Function.update s.kheap t (some (.tcb { x with tcbArch := archTcbSetRegisters regs x.tcbArch })) }) + (hS : ∀ rb, buf = some rb → ∀ k < 128, ∀ v, PresR R (storeWordOffs (Z := Unit) rb k v)) : + PresR R (setMrs (Z := Unit) t buf msgs) := by + intro σ r hr + unfold setMrs at hr + obtain ⟨y1, hy1, hr⟩ := hr + have hy1' : y1 ∈ (NonDet.assertOpt (getTcb (Z := Unit) t σ) σ).1 := hy1 + cases hg : getTcb (Z := Unit) t σ with + | none => rw [hg] at hy1'; exact absurd hy1' (Set.notMem_empty _) + | some x => + rw [hg] at hy1' + have e1 : y1 = (x, σ) := hy1' + subst e1 + obtain ⟨y2, hy2, hr⟩ := hr + obtain rfl := mem_pureA hy2 + obtain ⟨y3, hy3, hr⟩ := hr + obtain rfl := mem_pureA hy3 + obtain ⟨y4, hy4, hr⟩ := hr + obtain ⟨ko, hko, hr4⟩ := setObject_memA hy4 + have F : R σ y4.2 := by + rw [hr4]; exact hT σ x _ (getTcb_kheapA hg) + refine PreO.trans F ?_ + obtain ⟨y5, hy5, hr⟩ := hr + obtain rfl := mem_pureA hy5 + refine (?_ : PresR R _) y4.2 r hr + cases buf with + | none => exact PresR.pure _ + | some pptr => + refine PresR.bind (PresR.zipWithMx_memA _ _ fun k hk v => hS pptr rfl k ?_ v) fun _ => PresR.pure _ + simp only [List.mem_range'_1, msg_registers_length_A, msgMaxLength] at hk + omega + +section +variable {R : State Unit → State Unit → Prop} [PreO R] + (hE : ∀ a b : State Unit, a = b → R a b) + (hU : ∀ {α : Type} (t : ObjRef) (f : UserMonad α), PresR R (asUser (Z := Unit) t f)) + (hC : ∀ p, PresR R (schedContextUpdateConsumed (Z := Unit) p)) +include hE hU hC + +/-- `make_fault_msg` of every fault kind. -/ +theorem makeFaultMsg_genA (f : Fault) (t : ObjRef) : PresR R (makeFaultMsg (Z := Unit) f t) := by + have ro : ∀ {α : Type} {m : SMonad Unit α}, PresR (@Eq (State Unit)) m → PresR R m := + fun h s x hx => hE _ _ (h s x hx) + cases f with + | capFault c rp lf => rw [makeFaultMsg]; exact PresR.bind (hU _ _) fun _ => PresR.pure _ + | unknownSyscallException n => rw [makeFaultMsg]; exact PresR.bind (hU _ _) fun _ => PresR.pure _ + | userException e c => rw [makeFaultMsg]; exact PresR.bind (hU _ _) fun _ => PresR.pure _ + | timeout b => + rw [makeFaultMsg] + refine PresR.bind (ro (PresR.getsThe _)) fun x => ?_ + cases x.tcbSchedContext with + | none => exact PresR.pure _ + | some sc => exact PresR.bind (hC _) fun _ => PresR.pure _ + | archFault af => + rw [makeFaultMsg] + cases af with + | vmFault va d => unfold makeArchFaultMsg; exact PresR.bind (hU _ _) fun _ => PresR.pure _ + | vcpuFault h => unfold makeArchFaultMsg; exact PresR.pure _ + | vppiEvent irq => unfold makeArchFaultMsg; exact PresR.pure _ + | vgicMaintenance o => unfold makeArchFaultMsg; exact PresR.bind (PresR.pure _) fun _ => PresR.pure _ + +/-- **`do_fault_transfer`** (module docstring). -/ +theorem doFaultTransfer_genA (badge : Data) (snd rcv : ObjRef) (buf : Option ObjRef) + (hT : ∀ s (x : Tcb) regs, s.kheap rcv = some (.tcb x) → + R s { s with kheap := Function.update s.kheap rcv (some (.tcb { x with tcbArch := archTcbSetRegisters regs x.tcbArch })) }) + (hS : ∀ rb, buf = some rb → ∀ k < 128, ∀ v, PresR R (storeWordOffs (Z := Unit) rb k v)) : + PresR R (doFaultTransfer (Z := Unit) badge snd rcv buf) := by + have ro : ∀ {α : Type} {m : SMonad Unit α}, PresR (@Eq (State Unit)) m → PresR R m := + fun h s x hx => hE _ _ (h s x hx) + unfold doFaultTransfer + refine PresR.bind (ro (threadGet_eqPres _ _)) fun fault => ?_ + have tail : ∀ f : Fault, PresR R (do + let (label, msg) ← makeFaultMsg (Z := Unit) f snd + let sent ← setMrs rcv buf msg + setMessageInfo rcv ⟨sent, 0, 0, label⟩ + asUser rcv <| setRegister badge_register badge) := fun f => by + refine PresR.bind (makeFaultMsg_genA hE hU hC f snd) fun lm => ?_ + refine PresR.bind (setMrs_genA rcv buf _ hT hS) fun _ => PresR.bind ?_ fun _ => hU _ _ + unfold setMessageInfo + exact hU _ _ + cases fault with + | none => + intro s x hx + obtain ⟨y, hy, -⟩ := mem_bindA.1 hx + exact absurd hy (Set.notMem_empty _) + | some f => exact PresR.bind (PresR.pure _) tail + +end + +/-- `sched_context_update_consumed` is `SIKA`. -/ +theorem updateConsumed_sikA {i : ObjRef} {M N : ObjRef → Prop} {don : Bool} (p : ObjRef) : + PresR (SIKA i M N don) (schedContextUpdateConsumed (Z := Unit) p) := by + have w : ∀ c, PresR (SIKA i M N don) (setScObjRef scConsumedUpdate p c) := fun c => + updateSc_sikA p _ (fun _ => ⟨rfl, rfl, id, fun _ => ⟨rfl, rfl⟩, Or.inl rfl, fun _ => rfl⟩) (fun _ => rfl) + unfold schedContextUpdateConsumed + refine PresR.bind (eqPres_sikA (getSchedContext_eqPres p)) fun _ => PresR.bind (PresR.pure _) fun _ => ?_ + exact PresR.ite' (PresR.bind (w _) fun _ => PresR.pure _) (PresR.bind (w _) fun _ => PresR.pure _) + +/-- The sender has no fault and no extra caps, or has a fault (module docstring). -/ +def XferCaseA (xs : Tcb) (grant : Bool) : Prop := + (xs.tcbFault = none ∧ (grant = false ∨ ipcBufOfA false xs = none ∨ + unat (dataToMessageInfo (xs.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0)) ∨ + ∃ f, xs.tcbFault = some f + +section +open fl4_core_a64 Fl4ProofsA64.P1 +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- The fault branch of `do_ipc_transfer` is `do_fault_transfer` into the receiver's buffer. -/ +theorem doIpcTransfer_fault_genA {R : State Unit → State Unit → Prop} [PreO R] {σ : State Unit} {snd rcv : ObjRef} + {xs xr : Tcb} (hks : σ.kheap snd = some (.tcb xs)) (hkr : σ.kheap rcv = some (.tcb xr)) {f : Fault} + (hf : xs.tcbFault = some f) (ep : Option ObjRef) (badge : Badge) (grant : Bool) + (h : PresR R (doFaultTransfer (Z := Unit) badge snd rcv (ipcBufOfA true xr))) : + PresRC R (· = σ) (doIpcTransfer (Z := Unit) snd ep badge grant rcv) := by + unfold doIpcTransfer + refine PresRC.bind_oneA (lookupIpcBuffer_eqA hkr true) ?_ + refine PresRC.bind_oneA (threadGet_eq _ hks) ?_ + simp only [hf] + exact PresRC.of_pres h + +/-- **`do_ipc_transfer`** (both cases) writes only the receiver's buffer (module docstring). -/ +theorem doIpcTransfer_any_ufrA {th : TypedHeapA} {kb : U64} (hPl : PlaceA th kb) (sf : S) {σ : State Unit} + (hR : StateRelA th (L.mem sf) σ) (hV : SlotsValidA th (L.mem sf)) {snd rcv : U64} + (htr : th rcv = some .tcb) {xs xr : Tcb} (hks : σ.kheap (specPtr snd) = some (.tcb xs)) + (hkr : σ.kheap (specPtr rcv) = some (.tcb xr)) + (hal : ∀ rb, ipcBufOfA true xr = some rb → (bufAddr rb 0).toNat % 8 = 0) + (ep : Option ObjRef) (badge : Badge) (grant : Bool) (hcase : XferCaseA xs grant) : + PresRC (UFrA (BufBytesA (ipcBufOfA true xr))) (· = σ) + (doIpcTransfer (Z := Unit) (specPtr snd) ep badge grant (specPtr rcv)) := by + rcases hcase with ⟨hf, hcaps⟩ | ⟨f, hf⟩ + · exact doIpcTransfer_ufrA hPl sf hR hV htr hks hkr hf hal ep badge grant hcaps + refine doIpcTransfer_fault_genA hks hkr hf ep badge grant ?_ + refine doFaultTransfer_genA (fun a b e => e ▸ PreO.refl a) (fun t g => PresR.ufrA (asUser_presA t g)) + (fun p => PresR.ufrA (inferInstance : SPresInst (schedContextUpdateConsumed (Z := Unit) p)).pres) _ _ _ _ + (fun s x regs hx => UFrA.of_fr (SchedFrameA.upd_tcb hx ⟨rfl, rfl, rfl⟩)) + (fun rb e k hk v => e ▸ storeWordOffs_ufrA rb hk v) + +/-- **`do_ipc_transfer`** (both cases) is `SIKA`. -/ +theorem doIpcTransfer_any_sikA {i : ObjRef} {M N : ObjRef → Prop} + {th : TypedHeapA} {kb : U64} (hPl : PlaceA th kb) (sf : S) {σ : State Unit} + (hR : StateRelA th (L.mem sf) σ) (hV : SlotsValidA th (L.mem sf)) {snd rcv : U64} + (htr : th rcv = some .tcb) {xs xr : Tcb} (hks : σ.kheap (specPtr snd) = some (.tcb xs)) + (hkr : σ.kheap (specPtr rcv) = some (.tcb xr)) + (hal : ∀ rb, ipcBufOfA true xr = some rb → (bufAddr rb 0).toNat % 8 = 0) + (ep : Option ObjRef) (badge : Badge) (grant : Bool) (hcase : XferCaseA xs grant) : + PresRC (SIKA i M N false) (· = σ) (doIpcTransfer (Z := Unit) (specPtr snd) ep badge grant (specPtr rcv)) := by + rcases hcase with ⟨hf, hcaps⟩ | ⟨f, hf⟩ + · exact doIpcTransfer_sikA hPl sf hR hV htr hks hkr hf hal ep badge grant hcaps + refine doIpcTransfer_fault_genA hks hkr hf ep badge grant ?_ + refine doFaultTransfer_genA (fun a b e => e ▸ SIKA.refl a) (fun t g => asUser_sikA t g) + (fun p => updateConsumed_sikA p) _ _ _ _ + (fun s x regs hx => SIKA.upd hx (.tcb _ _ ⟨rfl, rfl, rfl, rfl, rfl, rfl, Or.inl rfl, Or.inl rfl, rfl, rfl⟩) + (idleRdy_nscA (by intro _ _ c; cases c))) + (fun rb _ k _ v => ?_) + · unfold storeWordOffs + exact PresR.bind PresR.get fun _ => PresR.bind (PresR.assert _) fun _ => doMachineOp_sikA _ + +/-- A reply push without donation changes no SC. -/ +theorem replyPush_noDon_sikA {i : ObjRef} {M N : ObjRef → Prop} (t d r : ObjRef) (hM : M t) : + PresR (SIKA i M N false) (replyPush (Z := Unit) t d r false) := by + unfold replyPush + refine PresR.bind (eqPres_sikA (by unfold getTcbObjRef; exact threadGet_eqPres _ _)) (fun a => ?_) + refine PresR.bind (eqPres_sikA (by unfold getTcbObjRef; exact threadGet_eqPres _ _)) (fun b => ?_) + refine PresR.bind (setReplyObjRef_sikA _ _ _ fun _ _ _ => rfl) (fun _ => ?_) + refine PresR.bind (setThreadState_sikA t hM _ (by intro h; cases h)) (fun _ => ?_) + intro s x hx + unfold NonDet.when at hx + rw [if_neg (by simp)] at hx + have : x = ((), s) := hx + rw [this]; exact SIKA.refl _ + +/-- **Every outcome of `send_ipc` to a waiting receiver writes only the receiver's buffer**, faulted senders +included. -/ +theorem sendIpc_recv_ufrFA {th : TypedHeapA} {kb : U64} (hPl : PlaceA th kb) (sf : S) {σ : State Unit} + (hR : StateRelA th (L.mem sf) σ) (hV : SlotsValidA th (L.mem sf)) {t d : U64} {e : ObjRef} {q : List ObjRef} + (htr : th d = some .tcb) (hk : σ.kheap e = some (.endpoint (.recvEP (specPtr d :: q)))) + {xt xd : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) (hkd : σ.kheap (specPtr d) = some (.tcb xd)) + (hal : ∀ rb, ipcBufOfA true xd = some rb → (bufAddr rb 0).toNat % 8 = 0) + (blk call : Bool) (badge : Badge) (g gr cd : Bool) (hcase : XferCaseA xt g) : + ∀ x ∈ (sendIpc (Z := Unit) blk call badge g gr cd (specPtr t) e σ).1, + UFrA (BufBytesA (ipcBufOfA true xd)) σ x.2 := by + suffices H : PresRC (UFrA (BufBytesA (ipcBufOfA true xd))) (· = σ) + (sendIpc (Z := Unit) blk call badge g gr cd (specPtr t) e) from fun x hx => H σ rfl x hx + unfold sendIpc + refine PresRC.bind_oneA (getEndpoint_eq hk) ?_ + refine PresRC.monoPA (P' := fun s => TcbKeepA σ s ∧ UFrA (BufBytesA (ipcBufOfA true xd)) σ s) + (fun s h => h ▸ ⟨fun _ _ h => h, PreO.refl s⟩) ?_ + cases blk <;> dsimp only + all_goals + refine PresRC.bind_inv (stepKA _) fun _ => ?_ + refine PresRC.bind_inv (stepKA _) fun recvState => ?_ + cases recvState + case blockedOnReceive a r dd => + dsimp only + refine PresRC.bind_pureA r ?_ + refine PresRC.bind_last (fun s hs x hx => ?_) (fun _ => PresR.ufrA (by spres)) + exact doIpcTransfer_any_ufrA hPl sf (hR.specSchedA hs.2.1) hV htr (hs.1 _ _ hkt) (hs.1 _ _ hkd) hal _ _ _ hcase + s rfl x hx + all_goals (dsimp only; exact PresRC.bind_fail) + +/-- **Every outcome of `send_ipc` to a waiting receiver `d` is `SIKA`**, faulted senders included. Without donation +(`don = false`) the send must not donate (`cd = false`). -/ +theorem sendIpc_recv_sikFA {i : ObjRef} {M N : ObjRef → Prop} {th : TypedHeapA} {kb : U64} (hPl : PlaceA th kb) + (sf : S) {σ : State Unit} (hR : StateRelA th (L.mem sf) σ) (hV : SlotsValidA th (L.mem sf)) {t d : U64} + {e : ObjRef} {q : List ObjRef} + (htr : th d = some .tcb) (hk : σ.kheap e = some (.endpoint (.recvEP (specPtr d :: q)))) + {xt xd : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) (hkd : σ.kheap (specPtr d) = some (.tcb xd)) + (hal : ∀ rb, ipcBufOfA true xd = some rb → (bufAddr rb 0).toNat % 8 = 0) + (blk call : Bool) (badge : Badge) (g gr cd : Bool) (hcase : XferCaseA xt g) + (hMt : M (specPtr t)) (hMd : M (specPtr d)) (hNd : N (specPtr d)) + (hti : specPtr t ≠ i) (hdi : specPtr d ≠ i) (hD : DonA σ (specPtr t) xt) + (hcd : cd = true → xt.tcbSchedContext ≠ none) {don : Bool} (hdon : don = false → cd = false) : + ∀ x ∈ (sendIpc (Z := Unit) blk call badge g gr cd (specPtr t) e σ).1, SIKA i M N don σ x.2 := by + suffices H : PresRC (SIKA i M N don) (· = σ) + (sendIpc (Z := Unit) blk call badge g gr cd (specPtr t) e) from fun x hx => H σ rfl x hx + unfold sendIpc + refine PresRC.bind_oneA (getEndpoint_eq hk) ?_ + refine PresRC.monoPA (P' := fun s => TcbKeepA σ s ∧ SchedFrameA σ s ∧ SIKA i M N false σ s) + (fun s h => h ▸ ⟨fun _ _ h => h, PreO.refl s, SIKA.refl s⟩) ?_ + cases blk <;> dsimp only + all_goals + refine PresRC.bind_inv (fun s hs x hx => ?_) (fun _ => ?_) + · have h1 := tcbEpDequeue_sikA (i := i) (M := M) (N := N) (don := false) _ e hMd s x hx + have h2 : SchedFrameA s x.2 := SPresInst.pres s x hx + exact ⟨h1.upTo, PreO.trans hs.1 (TKInst.pres s x hx), PreO.trans hs.2.1 h2, hs.2.2.trans h1⟩ + refine PresRC.bind_valA (P := fun s => TcbKeepA σ s ∧ SchedFrameA σ s ∧ SIKA i M N false σ s) + (Q := fun rs => rs = xd.tcbState) (fun s hs x hx => ?_) (fun recvState _hrs => ?_) + · have hz : x = (xd.tcbState, s) := by + unfold getThreadState at hx + exact threadGet_valA _ (hs.1 _ _ hkd) hx + subst hz; exact ⟨SIKA.refl s, hs, rfl⟩ + cases recvState + case blockedOnReceive a r dd => + dsimp only + refine PresRC.bind_pureA r ?_ + refine PresRC.monoPA (P' := fun s => TcbKeepA σ s ∧ SchedFrameA σ s ∧ SIKA i M N false σ s) (fun _ h => h) ?_ + intro s hs x hx + obtain ⟨y, hy, hx⟩ := hx + have hT : SIKA i M N false s y.2 := + doIpcTransfer_any_sikA hPl sf (hR.specSchedA hs.2.1.1) hV htr (hs.1 _ _ hkt) (hs.1 _ _ hkd) hal _ _ _ hcase + s rfl y hy + refine SIKA.trans hT.upTo ?_ + refine (?_ : PresRC (SIKA i M N don) (SIKA i M N false σ) _) y.2 (hs.2.2.trans hT) x hx + refine PresRC.sik_stepA ?_ (fun _ => ?_) + · cases r with + | none => exact PresR.pure _ + | some r => exact replyUnlinkTcb_sikA _ _ hMd + refine PresRC.sik_stepA (eqPres_sikA (threadGet_eqPres _ _)) (fun scOpt => ?_) + refine PresRC.sik_stepA (eqPres_sikA (threadGet_eqPres _ _)) (fun fault => ?_) + have tail : PresR (SIKA i M N don) (do + setThreadState (specPtr d) .running + let destSc ← getTcbObjRef Tcb.tcbSchedContext (specPtr d) + ifSporadicCurScTestRefillUnblockCheck destSc + possibleSwitchTo (Z := Unit) (specPtr d)) := + PresR.bind (setThreadState_sikA _ hMd _ (by intro h; cases h)) fun _ => + PresR.bind (eqPres_sikA (threadGet_eqPres _ _)) fun _ => + PresR.bind (refillCheck_sikA _) fun _ => possibleSwitchTo_sikA _ hNd + have inact : PresRC (SIKA i M N don) (SIKA i M N false σ) (setThreadState (Z := Unit) (specPtr t) .inactive) := + PresRC.of_pres fun s x hx => (setThreadState_sikA (don := false) _ hMt _ (by intro h; cases h) s x hx).upTo + split_ifs with c1 c2 + · refine PresRC.bind_last ?_ (fun _ => tail) + cases r with + | none => exact absurd rfl c2.2 + | some r => + cases don with + | true => exact replyPush_sikA hkt hD hMt hMd hti hdi _ + | false => + rw [hdon rfl] + exact PresRC.of_pres (replyPush_noDon_sikA _ _ _ hMt) + · exact PresRC.bind_last inact (fun _ => tail) + · refine PresRC.bind_last ?_ (fun _ => tail) + intro s hs x hx + unfold NonDet.when at hx + split_ifs at hx with hg + swap + · have : x = ((), s) := hx + rw [this]; exact SIKA.refl _ + cases don with + | false => rw [hdon rfl] at hg; exact absurd hg.1 (by simp) + | true => + obtain ⟨⟨c, s1⟩, h1, hx⟩ := mem_bindA.1 hx + unfold getTcbObjRef at h1 + obtain ⟨y, hy, ey⟩ := SIKA.tcbScA hs hkt + obtain ⟨ec, es⟩ := Prod.mk.inj (threadGet_valA Tcb.tcbSchedContext hy h1) + subst es + rw [ey] at ec + subst ec + cases hsc : xt.tcbSchedContext with + | none => exact absurd hsc (hcd hg.1) + | some sc => + rw [hsc] at hx + exact schedContextDonate_sikA sc (specPtr d) (specPtr t) (hD sc hsc).1 hdi hti hMd s1 (hD.keep hsc hs) x hx + all_goals (dsimp only; exact PresRC.bind_fail) + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3SendFaultValidA.lean b/proofs/Fl4ProofsA64/P3/Stage3SendFaultValidA.lean new file mode 100644 index 000000000..0505b132a --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SendFaultValidA.lean @@ -0,0 +1,424 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2b-2): **`SchedValidA` across `send_ipc` from a faulted sender to a waiting +receiver**, the sibling of `sendIpc_recv_schedValidA`. + +* `doIpcTransfer_any_tkA`: `do_ipc_transfer` with or without a fault keeps the scheduler's data and `RqDomPrio`. +* `replyPushNoDon_fia`: a reply push without donation keeps `FIA` (the caller, with an active SC, blocks). +* **`sendIpc_recv_schedValidFA`**: every outcome keeps `SchedValidA` (no donation, an inactive sender, a reply push to a + receiver without SC, or a reply push to a receiver that has its own SC). + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3IpcSchedValidA +import Fl4ProofsA64.P3.Stage3SendFaultSpecA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P3 Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Classical + +noncomputable section + +/-- The thread's TCB (if any) has the fault `f`. -/ +def FaultIsA (t : ObjRef) (f : Option Fault) (σ : State Unit) : Prop := + ∀ x, σ.kheap t = some (.tcb x) → x.tcbFault = f + +theorem FaultIsA.sik {t i : ObjRef} {M N : ObjRef → Prop} {don : Bool} {f : Option Fault} {σ σ' : State Unit} + (h : FaultIsA t f σ) (R : SIKA i M N don σ σ') : FaultIsA t f σ' := fun y hy => by + obtain ⟨x, hx, g⟩ := R.tcbB hy + rw [g.fault]; exact h x hx + +/-- Two triples with one precondition (`HT.and` lives downstream). -/ +theorem htAnd {α : Type} {m : SMonad Unit α} {P : State Unit → Prop} {Q1 Q2 : α → State Unit → Prop} + (h1 : HT P m Q1) (h2 : HT P m Q2) : HT P m (fun a σ => Q1 a σ ∧ Q2 a σ) := + fun σ hσ r hr => ⟨h1 σ hσ r hr, h2 σ hσ r hr⟩ + +/-- **A reply push without donation** keeps `FIA`. -/ +theorem replyPushNoDon_fia {t d r : ObjRef} (htd : d ≠ t) : + HT (fun σ => FIA σ ∧ RqDomPrio σ ∧ ActTcbA σ t ∧ BAs [d] σ) + (replyPush (Z := Unit) t d r false) (fun _ σ => FIA σ ∧ BAs [d] σ) := by + unfold replyPush + let P : State Unit → Prop := fun σ => FIA σ ∧ RqDomPrio σ ∧ ActTcbA σ t ∧ BAs [d] σ + have keep : ∀ σ σ', TKeepA σ σ' → CellsEq σ σ' → P σ → P σ' := fun σ σ' k c h => by + obtain ⟨h1, h2, ⟨x, sc, e, es, y, n, ey, ea⟩, h4⟩ := h + obtain ⟨y', ey', ea'⟩ := k.2.2.2.2.2.2.2.1 sc y n ey + exact ⟨h1.ofTKeep k, h2.ofCells c, ⟨x, sc, (c.2 _ _).1 e, es, y', n, ey', ea'.trans ea⟩, h4.ofTKeep k⟩ + have T : ∀ {β : Type} {m : SMonad Unit β}, TPres m → CPres m → HT P m (fun _ σ => P σ) := + fun h1 h2 σ h z hz => keep σ _ (h1 σ z hz) (h2 σ z hz) h + refine HT.bind (T (by unfold getTcbObjRef; tpres) (by unfold getTcbObjRef; cpres)) fun _ => ?_ + refine HT.bind (T (by unfold getTcbObjRef; tpres) (by unfold getTcbObjRef; cpres)) fun _ => ?_ + refine HT.bind (T (by tpres) (by cpres)) fun _ => ?_ + refine HT.bind (Q := fun _ σ => FIA σ ∧ BAs [d] σ) (fun σ h z hz => ?_) fun _ => ?_ + · have := setThreadStateB t (.blockedOnReply r) (BAs [d]) (fun σ x hx h => h.tcbUpd hx rfl) + (fun σ a h => h.act a) σ ⟨h.1, h.2.1, h.2.2.1, h.2.2.2⟩ z hz + exact ⟨this.1, this.2.2⟩ + intro σ h z hz + unfold NonDet.when at hz + rw [if_neg (by simp)] at hz + have e : z = ((), σ) := hz + rw [e]; exact h + +/-- The donating `when` branch of a plain send: the sender's SC goes to the receiver. -/ +theorem scActive_onlyFrameA (p : ObjRef) : TFrame (fun σ => ScActiveAtA σ p) := + ⟨fun σ σ' k h => by + obtain ⟨y, n, e, a⟩ := h + obtain ⟨y', e', a'⟩ := k.2.2.2.2.2.2.2.1 p y n e + exact ⟨y', n, e', a'.trans a⟩, + fun σ σ' hk _ h => by + obtain ⟨y, n, e, a⟩ := h + exact ⟨y, n, by rw [hk]; exact e, a⟩, + fun σ q x x' hx _ h => by + obtain ⟨y, n, e, a⟩ := h + have ne : p ≠ q := fun h => by rw [h, hx] at e; cases e + exact ⟨y, n, by simpa [Function.update_of_ne ne] using e, a⟩⟩ + +def donPlainM (t d : ObjRef) : SMonad Unit Unit := do + let c ← getTcbObjRef Tcb.tcbSchedContext t + schedContextDonate (Z := Unit) (the c) d + +/-- **A plain send's donation** (`can_donate`, a receiver without SC, a sender with an active SC) keeps `FIA` and +leaves the receiver's bound SC active. -/ +theorem donPlain_fia {t d sc : ObjRef} : + HT (fun σ => FIA σ ∧ RqDomPrio σ ∧ ActTcbA σ t ∧ ScIs t (some sc) σ ∧ ScIs d none σ) + (donPlainM t d) (fun _ σ => FIA σ ∧ BAs [d] σ) := by + unfold donPlainM + refine HT.bind (Q := fun a σ => (FIA σ ∧ RqDomPrio σ ∧ (ScIs d none σ ∧ ScActiveAtA σ sc)) ∧ + a = some sc) (fun σ h z hz => ?_) fun a => ?_ + · unfold getTcbObjRef at hz + obtain ⟨x, sc', e, es, act⟩ := h.2.2.1 + have ez := threadGet_valA Tcb.tcbSchedContext e hz + have h3 := h.2.2.2.1 x e + obtain rfl : sc' = sc := Option.some.inj (es.symm.trans h3) + rw [ez] + exact ⟨⟨h.1, h.2.1, h.2.2.2.2, act⟩, h3⟩ + intro σ ⟨h, ha⟩ z hz + subst ha + have a1 := donateR_fia sc d _ (scActive_onlyFrameA sc) σ ⟨h.1, h.2.2.1, Or.inl h.2.1, h.2.2.2⟩ z hz + have a2 := donate_bindsA sc d σ trivial z hz + have fia : FIA z.2 := by + refine ⟨⟨a1.1.1.rq, fun e1 e2 => a1.1.1.act e1 e2 id, fun u x sc' e1 e2 e3 => ?_⟩, a1.1.2⟩ + by_cases hu : u = d + · subst u + rw [a2 x e1] at e3 + cases e3 + exact a1.2 + · exact a1.1.1.blk u x sc' e1 e2 e3 hu + refine ⟨fia, fun u hu x sc' e es => ?_⟩ + obtain rfl := List.mem_singleton.1 hu + rw [a2 x e] at es; cases es + exact ⟨x, sc, e, a2 x e, a1.2⟩ + +/-- **A plain send's donation is `SIKA`** (the donating `when` branch of `send_ipc`). -/ +theorem donPlain_sikA {i : ObjRef} {M N : ObjRef → Prop} {σ : State Unit} {t d : ObjRef} {x : Tcb} + (hk : σ.kheap t = some (.tcb x)) (hD : DonA σ t x) (hMd : M d) (hti : t ≠ i) (hdi : d ≠ i) + (hsc : x.tcbSchedContext ≠ none) : + PresRC (SIKA i M N true) (SIKA i M N false σ) (donPlainM t d) := by + unfold donPlainM + intro s hs z hz + obtain ⟨⟨c, s1⟩, h1, hx⟩ := mem_bindA.1 hz + unfold getTcbObjRef at h1 + obtain ⟨y, hy, ey⟩ := SIKA.tcbScA hs hk + obtain ⟨ec, es⟩ := Prod.mk.inj (threadGet_valA Tcb.tcbSchedContext hy h1) + subst es + rw [ey] at ec + subst ec + cases hsc' : x.tcbSchedContext with + | none => exact absurd hsc' hsc + | some sc => + rw [hsc'] at hx + exact schedContextDonate_sikA sc d t (hD sc hsc').1 hdi hti hMd s1 (hD.keep hsc' hs) z hx + +section +open fl4_core_a64 Fl4ProofsA64.P1 +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- `do_ipc_transfer` (both cases) keeps the scheduler's data and `RqDomPrio`. -/ +theorem doIpcTransfer_any_tkA {th : TypedHeapA} {kb : U64} (hPl : PlaceA th kb) (sf : S) {σ : State Unit} + (hR : StateRelA th (L.mem sf) σ) (hV : SlotsValidA th (L.mem sf)) {snd rcv : U64} + (htr : th rcv = some .tcb) {xs xr : Tcb} (hks : σ.kheap (specPtr snd) = some (.tcb xs)) + (hkr : σ.kheap (specPtr rcv) = some (.tcb xr)) + (hal : ∀ rb, ipcBufOfA true xr = some rb → (bufAddr rb 0).toNat % 8 = 0) + (ep : Option ObjRef) (badge : Badge) (grant : Bool) (hcase : XferCaseA xs grant) : + ∀ x ∈ (doIpcTransfer (Z := Unit) (specPtr snd) ep badge grant (specPtr rcv) σ).1, + TKeepA σ x.2 ∧ (RqDomPrio σ → RqDomPrio x.2) := by + rcases hcase with ⟨hf, hcaps⟩ | ⟨f, hf⟩ + · exact doIpcTransfer_tkA hPl sf hR hV htr hks hkr hf hal ep badge grant hcaps + let R : State Unit → State Unit → Prop := fun a b => TKeepA a b ∧ (RqDomPrio a → RqDomPrio b) + haveI : PreO R := ⟨fun s => ⟨PreO.refl s, id⟩, fun h1 h2 => ⟨PreO.trans h1.1 h2.1, fun h => h2.2 (h1.2 h)⟩⟩ + have hM : ∀ {α : Type} (m : NonDet MachineState α), PresR R (doMachineOp (Z := Unit) m) := fun m s y hy => by + refine ⟨doMachineOp_tpres _ s y hy, fun h => ?_⟩ + have q : y.2.readyQueues = s.readyQueues ∧ y.2.kheap = s.kheap := by + obtain ⟨y1, hy1, hy⟩ := hy + have e1 : y1 = (s.machineState, s) := hy1 + subst e1 + obtain ⟨⟨⟨a, ms'⟩, s2⟩, hy2, hy⟩ := hy + have e2 : s2 = s := hy2.2 + subst e2 + obtain ⟨y3, hy3, hy⟩ := hy + have e3 : y3 = ((), { s2 with machineState := ms' }) := hy3 + subst e3 + have e4 : y = (a, { s2 with machineState := ms' }) := hy + rw [e4]; exact ⟨rfl, rfl⟩ + intro g d p x hx hm + rw [q.2] at hx; rw [q.1] at hm + exact h g d p x hx hm + have key : PresRC R (· = σ) (doIpcTransfer (Z := Unit) (specPtr snd) ep badge grant (specPtr rcv)) := by + refine doIpcTransfer_fault_genA hks hkr hf ep badge grant ?_ + refine doFaultTransfer_genA (fun a b e => e ▸ PreO.refl a) (fun t g s y hy => ⟨asUser_tpres t g s y hy, fun h => by + obtain ⟨x, uc, hx, e⟩ := asUser_spec hy + rw [e]; exact h.ofTcbRepl hx rfl rfl⟩) (fun p s y hy => ?_) _ _ _ _ + (fun s x regs hx => ⟨setObjTcb_tkeep hx rfl rfl, fun h => h.ofTcbRepl hx rfl rfl⟩) (fun rb _ k _ v => ?_) + · refine ⟨?_, fun h => h.ofCells ((by unfold schedContextUpdateConsumed; cpres : + CPres (schedContextUpdateConsumed (Z := Unit) p)) s y hy)⟩ + have w : ∀ c, PresR TKeepA (setScObjRef scConsumedUpdate p c) := fun c s r hr => by + obtain ⟨z, n, hk, e⟩ := updateSchedContext_memA p _ hr + rw [e]; exact setObjSc_tkeep hk rfl + have : PresR TKeepA (schedContextUpdateConsumed (Z := Unit) p) := by + unfold schedContextUpdateConsumed + refine PresR.bind (fun s x hx => by rw [← getSchedContext_eqPres p s x hx]; exact PreO.refl s) fun _ => + PresR.bind (PresR.pure _) fun _ => ?_ + exact PresR.ite' (PresR.bind (w _) fun _ => PresR.pure _) (PresR.bind (w _) fun _ => PresR.pure _) + exact this s y hy + · unfold storeWordOffs + exact PresR.bind (R := R) PresR.get fun _ => PresR.bind (PresR.assert _) fun _ => hM _ + exact fun x hx => key σ rfl x hx + +/-- **`send_ipc` to a waiting receiver from a possibly faulted sender, generically** (as `sendIpc_recv_gA`). -/ +theorem sendIpc_recv_gFA {th : TypedHeapA} {kb : U64} (hPl : PlaceA th kb) (sf : S) {σ : State Unit} + (hR : StateRelA th (L.mem sf) σ) (hV : SlotsValidA th (L.mem sf)) {t d : U64} {e : ObjRef} {q : List ObjRef} + (htr : th d = some .tcb) (hk : σ.kheap e = some (.endpoint (.recvEP (specPtr d :: q)))) + {xt xd : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) (hkd : σ.kheap (specPtr d) = some (.tcb xd)) + (hal : ∀ rb, ipcBufOfA true xd = some rb → (bufAddr rb 0).toNat % 8 = 0) + (blk call : Bool) (badge : Badge) (g gr cd : Bool) + (hcase : XferCaseA xt g) + {e' : ObjRef} {rep : Option ObjRef} {dd : ReceiverPayload} (hst : xd.tcbState = .blockedOnReceive e' rep dd) + (hbr : (call = false ∧ cd = false ∧ xt.tcbFault = none) ∨ + ((call = true ∨ xt.tcbFault ≠ none) ∧ ((g = false ∧ gr = false) ∨ rep = none)) ∨ + ((call = true ∨ xt.tcbFault ≠ none) ∧ (g = true ∨ gr = true) ∧ rep ≠ none ∧ specPtr t ≠ specPtr d ∧ + ActTcbA σ (specPtr t) ∧ (cd = true → xd.tcbSchedContext = none)) ∨ + ((call = true ∨ xt.tcbFault ≠ none) ∧ (g = true ∨ gr = true) ∧ rep ≠ none ∧ cd = true ∧ + specPtr t ≠ specPtr d ∧ xd.tcbSchedContext ≠ none ∧ ActTcbA σ (specPtr t)) ∨ + (call = false ∧ cd = true ∧ xt.tcbFault = none ∧ xd.tcbSchedContext = none ∧ + ActTcbA σ (specPtr t))) + (hI : SchedValidA σ) (hD : RqDomPrio σ) + {Mid Post : State Unit → Prop} + (htail : HT (fun σ => (FIA σ ∧ BAs [specPtr d] σ) ∧ Mid σ) + (do setThreadState (Z := Unit) (specPtr d) .running + let destSc ← getTcbObjRef Tcb.tcbSchedContext (specPtr d) + ifSporadicCurScTestRefillUnblockCheck destSc + possibleSwitchTo (specPtr d)) + (fun _ σ => Post σ)) + (hmid1 : ∀ s1, SIKA σ.idleThread (fun _ => True) (fun _ => False) false σ s1 → + (call = true ∨ xt.tcbFault ≠ none) → ¬ ((g = true ∨ gr = true) ∧ rep ≠ none) → + ∀ x ∈ (setThreadState (Z := Unit) (specPtr t) .inactive s1).1, Mid x.2) + (hmid2 : ∀ s1, SIKA σ.idleThread (fun _ => True) (fun _ => False) false σ s1 → + (call = true ∨ xt.tcbFault ≠ none) → (g = true ∨ gr = true) → rep ≠ none → + ∀ x ∈ (replyPush (Z := Unit) (specPtr t) (specPtr d) (the rep) cd s1).1, Mid x.2) + (hmid3 : ∀ s1, SIKA σ.idleThread (fun _ => True) (fun _ => False) false σ s1 → + ¬ (call = true ∨ xt.tcbFault ≠ none) → Mid s1) + (hmid4 : ∀ s1, SIKA σ.idleThread (fun _ => True) (fun _ => False) false σ s1 → + (call = false ∧ cd = true ∧ xt.tcbFault = none ∧ xd.tcbSchedContext = none ∧ + ActTcbA σ (specPtr t)) → + ∀ x ∈ (donPlainM (specPtr t) (specPtr d) s1).1, Mid x.2) : + ∀ x ∈ (sendIpc (Z := Unit) blk call badge g gr cd (specPtr t) e σ).1, Post x.2 := by + suffices H : HT (fun s => s = σ) (sendIpc (Z := Unit) blk call badge g gr cd (specPtr t) e) (fun _ s => Post s) from + fun x hx => H σ rfl x hx + have hFI : FIA σ := SchedValidA.toFIA hI + have hBA : BAs [specPtr d] σ := fun u hu x sc ex hs => by + obtain rfl := List.mem_singleton.1 hu + have : x = xd := KernelObject.tcb.inj (Option.some.inj (ex.symm.trans hkd)) + subst this + exact ⟨x, sc, ex, hs, hFI.1.blk _ x sc ex (by rw [hst]; rfl) hs⟩ + unfold sendIpc + intro s hs x hx + subst hs + obtain ⟨y, hy, hx'⟩ := mem_bindA.1 hx + clear hx + rw [getEndpoint_eq hk] at hy + have ey : y = (.recvEP (specPtr d :: q), s) := hy + subst ey + revert x hx' + cases blk <;> dsimp only + all_goals + refine HT.atA (P := fun σ' => σ' = s) ?_ rfl + refine HT.bind (Q := fun _ s1 => FIA s1 ∧ RqDomPrio s1 ∧ BAs [specPtr d] s1 ∧ TcbKeepA s s1 ∧ SchedFrameA s s1 ∧ + SIKA s.idleThread (fun _ => True) (fun _ => False) false s s1) (fun σ' h z hz => ?_) fun _ => ?_ + · subst h + have h1 := (HT.ofTC' (by unfold tcbEpDequeue; tpres) (by unfold tcbEpDequeue; cpres) (BAs [specPtr d]) + (fun _ _ k h => h.ofTKeep k) : + HT _ (tcbEpDequeue (Z := Unit) (specPtr d) e) _) σ' ⟨hFI, hD, hBA⟩ z hz + exact ⟨h1.1, h1.2.1, h1.2.2, TKInst.pres σ' z hz, SPresInst.pres σ' z hz, tcbEpDequeue_sikA _ _ trivial σ' z hz⟩ + refine HT.bind (Q := fun rs s1 => (FIA s1 ∧ RqDomPrio s1 ∧ BAs [specPtr d] s1 ∧ TcbKeepA s s1 ∧ + SchedFrameA s s1 ∧ SIKA s.idleThread (fun _ => True) (fun _ => False) false s s1) ∧ + rs = .blockedOnReceive e' rep dd) (fun σ' h z hz => ?_) fun rs => ?_ + · have hz' : z = (xd.tcbState, σ') := by + unfold getThreadState at hz + exact threadGet_valA _ (h.2.2.2.1 _ _ hkd) hz + subst hz' + exact ⟨h, hst⟩ + intro σ' ⟨h, ers⟩ z hz + subst ers + dsimp only at hz + obtain ⟨w, hw, hz1⟩ := mem_bindA.1 hz + clear hz + have ew : w = (rep, σ') := hw + subst ew + obtain ⟨u, hu, hz2⟩ := mem_bindA.1 hz1 + clear hz1 + have hT := doIpcTransfer_any_tkA hPl sf (hR.specSchedA h.2.2.2.2.1.1) hV htr (h.2.2.2.1 _ _ hkt) + (h.2.2.2.1 _ _ hkd) hal _ _ _ hcase u hu + have hS : SIKA s.idleThread (fun _ => True) (fun _ => False) false σ' u.2 := + doIpcTransfer_any_sikA hPl sf (hR.specSchedA h.2.2.2.2.1.1) hV htr (h.2.2.2.1 _ _ hkt) + (h.2.2.2.1 _ _ hkd) hal _ _ _ hcase σ' rfl u hu + have hNF : FaultIsA (specPtr t) xt.tcbFault u.2 := FaultIsA.sik (fun x hx => by + rw [h.2.2.2.1 _ _ hkt] at hx + have : xt = x := KernelObject.tcb.inj (Option.some.inj hx) + rw [← this]) hS + revert z hz2 + let K : State Unit → Prop := fun s1 => (FIA s1 ∧ RqDomPrio s1 ∧ BAs [specPtr d] s1) ∧ + SIKA s.idleThread (fun _ => True) (fun _ => False) false s s1 + refine HT.atA (P := fun s1 => K s1 ∧ FaultIsA (specPtr t) xt.tcbFault s1) ?_ + ⟨⟨⟨h.1.ofTKeep hT.1, hT.2 h.2.1, h.2.2.1.ofTKeep hT.1⟩, h.2.2.2.2.2.trans hS⟩, hNF⟩ + refine HT.bind (Q := fun _ s1 => (K s1 ∧ FaultIsA (specPtr t) xt.tcbFault s1) ∧ + (rep ≠ none → StIs (specPtr d) .inactive s1)) ?_ fun _ => ?_ + · cases rep with + | none => exact HT.pure () (fun _ h => ⟨h, fun c => absurd rfl c⟩) + | some r => + intro s1 h z hz + have a := replyUnlinkTcbD (specPtr d) r s1 s1 ⟨h.1.1.1, h.1.1.2.1, KI.refl _ s1, h.1.1.2.2⟩ z hz + have b := replyUnlinkTcb_sikA (i := s.idleThread) (M := fun _ => True) (N := fun _ => False) (don := false) + (specPtr d) r trivial s1 z hz + exact ⟨⟨⟨⟨a.1, a.2.1, a.2.2.2⟩, h.1.2.trans b⟩, h.2.sik b⟩, + fun _ => replyUnlinkTcb_stA (specPtr d) r s1 trivial z hz⟩ + refine HT.bind (Q := fun _ s1 => (K s1 ∧ FaultIsA (specPtr t) xt.tcbFault s1) ∧ + (rep ≠ none → StIs (specPtr d) .inactive s1)) + (fun s1 h z hz => by + have e : z.2 = s1 := ((threadGet_eqPres _ _) s1 z hz).symm + rw [e]; exact h) fun scOpt => ?_ + refine HT.bind (Q := fun fault s1 => (K s1 ∧ (rep ≠ none → StIs (specPtr d) .inactive s1)) ∧ fault = xt.tcbFault) + (HT.threadGet _ (specPtr t) fun s1 h x hx => ⟨⟨h.1.1, h.2⟩, h.1.2 x hx⟩) fun fault => ?_ + intro s1 ⟨⟨⟨h1, hk⟩, hin⟩, ef⟩ z hz + subst ef + revert z hz + refine HT.atA (P := fun s2 => s2 = s1) ?_ rfl + refine HT.mono (P := fun s2 => (FIA s2 ∧ RqDomPrio s2 ∧ BAs [specPtr d] s2) ∧ s2 = s1) ?_ + (fun s2 e => ⟨e ▸ h1, e⟩) (fun _ _ h => h) + have inact : HT (fun s2 => (FIA s2 ∧ RqDomPrio s2 ∧ BAs [specPtr d] s2) ∧ s2 = s1) + (setThreadState (Z := Unit) (specPtr t) .inactive) (fun _ s2 => FIA s2 ∧ BAs [specPtr d] s2) := + HT.mono (setThreadStateD (specPtr t) .inactive rfl (BAs [specPtr d]) (fun σ x hx h => h.tcbUpd hx rfl) + (fun σ a h => h.act a)) (fun _ h => h.1) (fun _ _ h => ⟨h.1, h.2.2.1⟩) + have mid : ∀ {m : SMonad Unit Unit}, (∀ x ∈ (m s1).1, Mid x.2) → + HT (fun s2 => (FIA s2 ∧ RqDomPrio s2 ∧ BAs [specPtr d] s2) ∧ s2 = s1) m (fun _ s2 => Mid s2) := + fun h s2 e x hx => h x (e.2 ▸ hx) + split_ifs with c1 c2 + · refine HT.bind (Q := fun _ s2 => (FIA s2 ∧ BAs [specPtr d] s2) ∧ Mid s2) + (htAnd ?_ (mid (hmid2 s1 hk c1 c2.1 c2.2))) fun _ => htail + rcases hbr with ⟨rfl, -, hf⟩ | ⟨-, hn⟩ | ⟨-, -, hr, htd, hact, hd0⟩ | ⟨-, -, -, rfl, htd, hdc, hact⟩ | + ⟨rfl, -, hf, -⟩ + · simp [hf] at c1 + · refine absurd c2 (fun c => ?_) + rcases hn with ⟨rfl, rfl⟩ | rfl + · simp at c + · exact c.2 rfl + · obtain ⟨x, sc, ex, es, y, n, ey, ea⟩ := hact + have hx : x = xt := KernelObject.tcb.inj (Option.some.inj (ex.symm.trans hkt)) + subst hx + obtain ⟨xt', hxt', exs⟩ : ∃ y, s1.kheap (specPtr t) = some (.tcb y) ∧ (cd = true → y.tcbSchedContext = some sc) := by + obtain ⟨y, hy, e⟩ := hk.tcbScA ex + exact ⟨y, hy, fun _ => e.trans es⟩ + have act : ActTcbA s1 (specPtr t) := by + obtain ⟨z, hz, ez⟩ := hk.tcbScA ex + obtain ⟨y', ey', gy⟩ := hk.scF ey + exact ⟨z, sc, hz, ez.trans es, y', n, ey', by unfold scActive at ea ⊢; rw [gy.rm]; exact ea⟩ + cases cd with + | false => + refine HT.mono (replyPushNoDon_fia (Ne.symm htd)) (fun s2 e => ?_) (fun _ _ h => h) + rw [e.2] + exact ⟨h1.1, h1.2.1, act, h1.2.2⟩ + | true => + refine HT.mono (replyPushDon_fia (sc := sc) (Ne.symm htd)) (fun s2 h => ?_) (fun _ _ h => h) + obtain ⟨⟨f, rq, -⟩, e⟩ := h + subst e + obtain ⟨xd', hxd', exd⟩ := hk.tcbScA hkd + refine ⟨f, rq, act, fun w hw => ?_, fun w hw => ?_, hin hr⟩ + · rw [hxt'] at hw + obtain rfl : xt' = w := KernelObject.tcb.inj (Option.some.inj hw) + exact exs rfl + · rw [hxd'] at hw + obtain rfl : xd' = w := KernelObject.tcb.inj (Option.some.inj hw) + exact exd.trans (hd0 rfl) + · obtain ⟨c, hc⟩ := Option.ne_none_iff_exists'.1 hdc + obtain ⟨x, sc, ex, es, y, n, ey, ea⟩ := hact + have hx : x = xt := KernelObject.tcb.inj (Option.some.inj (ex.symm.trans hkt)) + subst hx + refine HT.mono (replyPushCalleeSc_fia (c := c) (Ne.symm htd)) (fun s2 h => ?_) (fun _ _ h => h) + obtain ⟨⟨f, rq, hba⟩, e⟩ := h + subst e + obtain ⟨xt', hxt', exs⟩ := hk.tcbScA ex + obtain ⟨xd', hxd', exd⟩ := hk.tcbScA hkd + obtain ⟨y', ey', gy⟩ := hk.scF ey + refine ⟨f, rq, ⟨xt', sc, hxt', exs.trans es, y', n, ey', by unfold scActive at ea ⊢; rw [gy.rm]; exact ea⟩, + fun w hw => ?_, hba⟩ + rw [hxd'] at hw + obtain rfl : xd' = w := KernelObject.tcb.inj (Option.some.inj hw) + exact exd.trans hc + · simp [hf] at c1 + · exact HT.bind (htAnd inact (mid (hmid1 s1 hk c1 c2))) fun _ => htail + · refine HT.bind (Q := fun _ s2 => (FIA s2 ∧ BAs [specPtr d] s2) ∧ Mid s2) + (HT.when (fun hg => ?_) (fun _ _ h => ⟨⟨h.1.1, h.1.2.2⟩, h.2 ▸ hmid3 s1 hk c1⟩)) fun _ => htail + rcases hbr with ⟨-, rfl, -⟩ | ⟨c, -⟩ | ⟨c, -⟩ | ⟨c, -⟩ | ⟨hcl, hcdt, hf, hd0, hact⟩ + · exact absurd hg.1 (by simp) + · exact absurd c c1 + · exact absurd c c1 + · exact absurd c c1 + · obtain ⟨x, sc, ex, es, y, n, ey, ea⟩ := hact + have hx : x = xt := KernelObject.tcb.inj (Option.some.inj (ex.symm.trans hkt)) + subst hx + obtain ⟨xt', hxt', exs⟩ := hk.tcbScA ex + obtain ⟨xd', hxd', exd⟩ := hk.tcbScA hkd + obtain ⟨y', ey', gy⟩ := hk.scF ey + have act : ActTcbA s1 (specPtr t) := + ⟨xt', sc, hxt', exs.trans es, y', n, ey', by unfold scActive at ea ⊢; rw [gy.rm]; exact ea⟩ + refine htAnd (HT.mono (donPlain_fia (t := specPtr t) (d := specPtr d) (sc := sc)) (fun s2 h => ?_) + (fun _ _ h => h)) + (mid (hmid4 s1 hk ⟨hcl, hcdt, hf, hd0, ⟨x, sc, ex, es, y, n, ey, ea⟩⟩)) + obtain ⟨⟨f, rq, -⟩, e⟩ := h + subst e + refine ⟨f, rq, act, fun w hw => ?_, fun w hw => ?_⟩ + · rw [hxt'] at hw + obtain rfl : xt' = w := KernelObject.tcb.inj (Option.some.inj hw) + exact exs.trans es + · rw [hxd'] at hw + obtain rfl : xd' = w := KernelObject.tcb.inj (Option.some.inj hw) + exact exd.trans hd0 + +/-- **`SchedValidA` across `send_ipc` to a waiting receiver**, faulted senders included (module docstring): no +donation, an inactive sender, or a reply push (`cd = true`: donating, to a receiver without SC). -/ +theorem sendIpc_recv_schedValidFA {th : TypedHeapA} {kb : U64} (hPl : PlaceA th kb) (sf : S) {σ : State Unit} + (hR : StateRelA th (L.mem sf) σ) (hV : SlotsValidA th (L.mem sf)) {t d : U64} {e : ObjRef} {q : List ObjRef} + (htr : th d = some .tcb) (hk : σ.kheap e = some (.endpoint (.recvEP (specPtr d :: q)))) + {xt xd : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) (hkd : σ.kheap (specPtr d) = some (.tcb xd)) + (hal : ∀ rb, ipcBufOfA true xd = some rb → (bufAddr rb 0).toNat % 8 = 0) + (blk call : Bool) (badge : Badge) (g gr cd : Bool) + (hcase : XferCaseA xt g) + {e' : ObjRef} {rep : Option ObjRef} {dd : ReceiverPayload} (hst : xd.tcbState = .blockedOnReceive e' rep dd) + (hbr : (call = false ∧ cd = false ∧ xt.tcbFault = none) ∨ + ((call = true ∨ xt.tcbFault ≠ none) ∧ ((g = false ∧ gr = false) ∨ rep = none)) ∨ + ((call = true ∨ xt.tcbFault ≠ none) ∧ (g = true ∨ gr = true) ∧ rep ≠ none ∧ specPtr t ≠ specPtr d ∧ + ActTcbA σ (specPtr t) ∧ (cd = true → xd.tcbSchedContext = none)) ∨ + ((call = true ∨ xt.tcbFault ≠ none) ∧ (g = true ∨ gr = true) ∧ rep ≠ none ∧ cd = true ∧ + specPtr t ≠ specPtr d ∧ xd.tcbSchedContext ≠ none ∧ ActTcbA σ (specPtr t)) ∨ + (call = false ∧ cd = true ∧ xt.tcbFault = none ∧ xd.tcbSchedContext = none ∧ + ActTcbA σ (specPtr t))) + (hI : SchedValidA σ) (hD : RqDomPrio σ) : + ∀ x ∈ (sendIpc (Z := Unit) blk call badge g gr cd (specPtr t) e σ).1, SchedValidA x.2 := by + have H := sendIpc_recv_gFA hPl sf hR hV htr hk hkt hkd hal blk call badge g gr cd hcase hst hbr hI hD + (Mid := fun _ => True) (Post := FIA) (HT.mono (sendTail_fia (specPtr d)) (fun _ h => h.1) (fun _ _ h => h)) + (fun _ _ _ _ _ _ => trivial) (fun _ _ _ _ _ _ _ => trivial) (fun _ _ _ => trivial) + (fun _ _ _ _ _ => trivial) + exact fun x hx => SchedValidB.toA (H x hx).1 (H x hx).2 + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3SendNarrowA.lean b/proofs/Fl4ProofsA64/P3/Stage3SendNarrowA.lean new file mode 100644 index 000000000..532c2bdc2 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SendNarrowA.lean @@ -0,0 +1,127 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2b-2b): **`NtfnWaitKeepA`** (`Stage3IpcFl4KeepA`'s hypothesis) from the narrow +write sets of a send to a waiting receiver (`EpSendFpA.send_ipc_recv_fpA`, +`IpcScSendPushA.send_ipc_recv_pushDonate_fpA`). + +* **`JStage3A.tcbWord_offMwrA`**: a TCB's links and block word are not scheduler or SC words. +* **`JStage3A.ntfnWait_sendA`**. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3IpcFl4KeepA +import Fl4ProofsA64.P3.EpSendFpA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Sched + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {A : Type} {AR' : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} + {κ : U64 → U64} {n : Nat} {J5 : JSlotA S} {R : Set U64 → JSlotA S} {s s' : S} {a : A} + {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} +variable (J : JStage3A st AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7A st R) s a σ m) +include J + +/-- A TCB word at an offset the scheduler and SC code do not write (links and block word included). -/ +theorem JStage3A.tcbWord_offMwrA {t K : U64} (ht : t ∈ Z.T) (hK : K.val < 4096) + (h : K.val ≠ 3464 ∧ K.val ≠ 3480 ∧ K.val ≠ 3488 ∧ K.val ≠ 3592) : + ¬ MWrA (st := st) Z m σ s (wa t K) := by + have hfit := (J.sr.core.tcbs t ht).fits + have hv : (wa t K).val = t.val + K.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have hth := (J.sr.thT t).2 ht + have hsz : ObjKindA.size .tcb = 4096 := rfl + have other : ∀ u ∈ Z.T, ∀ K' : U64, K'.val < 4096 → K.val ≠ K'.val → wa t K ≠ wa u K' := by + intro u hu K' k1 k2 h + have hfu := (J.sr.core.tcbs u hu).fits + have hv' : (wa u K').val = u.val + K'.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have h' := congrArg (fun w : U64 => w.val) h + simp only [hv, hv'] at h' + by_cases e : u = t + · subst e; omega + · have := J.sr.place.apart u t .tcb .tcb ((J.sr.thT u).2 hu) hth e + omega + rintro (c | c | ⟨u, hu, e⟩) + · obtain ⟨c0, hc, x, k, hx, ha⟩ := c + have := (J.sr.scp.out c0 hc x k hx _ ha).1 t _ hth + omega + · have := J.sr.tcb_ks ht + unfold SKsA at c + unfold ksSizeA at this; omega + · rcases e with e | e | e | e + · rw [scA_eq] at e; exact other u hu _ (by simp) (by simpa using h.1) e + · exact other u hu 3480#u64 (by simp) (by simpa using h.2.1) e + · exact other u hu 3488#u64 (by simp) (by simpa using h.2.2.1) e + · rw [inqA_eq] at e; exact other u hu _ (by simp) (by simpa using h.2.2.2) e + +/-- **`NtfnWaitKeepA`** from `send_ipc_recv_fpA`'s write set: the threads waiting on a notification are not on the +endpoint (`hdb`: the receiver is), so no `Ep` link or block word of theirs is written. The write set may further +have reply objects' words and TCB reply words (the reply push of a donating call). -/ +theorem JStage3A.ntfnWait_sendA {W : Prop} {t e d : U64} (ht : t ∈ Z.T) (he : e ∈ E) (hd : d ∈ Z.T) + (hdb : L.mem s (Notif.tBlockA d) = e) + (w : WritesIn st s s' (fun b => SendFpA st Z.T Rp Bf (fun σ s => MWrA (st := st) Z m σ s) σ s t e d W b ∨ + (∃ r ∈ Rp, r.val ≤ b.val ∧ b.val < r.val + 32) ∨ ∃ u ∈ Z.T, b = tReplyA u)) : + NtfnWaitKeepA Z.T N (L.mem s) (L.mem s') := by + intro y hy hN + have hP := J.iplaced + have hya := hP.e.tcb y hy + have hfy := hya.fits + have notE : ∀ u ∈ Z.T, L.mem s (Notif.tBlockA u) = e → u ≠ y := fun u _ hu c => by + subst c + rw [hu] at hN + have := hP.ne e hN e he + omega + have key : ∀ (K : U64) (b : U64), (K.val = 3336 ∨ K.val = 3496 ∨ K.val = 3504) → b = wa y K → Aligned b → + L.mem s' b = L.mem s b := by + intro K b hK hb hal + have hbv : b.val = y.val + K.val := by rw [hb, wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have apart : ∀ u ∈ Z.T, u ≠ y → u.val + 4096 ≤ y.val ∨ y.val + 4096 ≤ u.val := fun u hu ne => by + have := J.sr.place.apart u y .tcb .tcb ((J.sr.thT u).2 hu) ((J.sr.thT y).2 hy) ne + have hsz : ObjKindA.size .tcb = 4096 := rfl + omega + refine w.mem_eq hal ?_ + rintro ((c | ⟨r, hr, c⟩ | ⟨-, c⟩ | c | c) | ⟨r, hr, c⟩ | ⟨u, hu, c⟩) + · rcases c with c | ⟨u, hu, hub, c⟩ | ⟨r, c⟩ | c + · have := hP.e.et e he y hy; unfold EW at c; omega + · have := apart u hu (notE u hu hub) + have := (hP.e.tcb u hu).fits + unfold TL at c; omega + · have hfd := (hP.e.tcb d hd).fits + have h1 := regIdxA_lt r + rw [Endp.regA_val (hP.e.tcb d hd) h1] at c + have := apart d hd (notE d hd hdb) + omega + · have := hP.e.bt _ c y hy; omega + · have h1 := hP.e.rt r hr y hy + have h2 : b.val = r.val := by rw [c, Endp.rTcbA_val] + omega + · have h1 : b.val = t.val + 3328 := by rw [c]; exact Endp.val_st (hP.e.tcb t ht) + by_cases ety : t = y + · subst ety; omega + · have := apart t ht ety; omega + · have h1 : b.val = d.val + 3328 := by rw [c]; exact Endp.val_st (hP.e.tcb d hd) + have := apart d hd (notE d hd hdb) + omega + · rw [hb] at c + exact J.tcbWord_offMwrA hy (by omega) (by omega) c + · have := hP.e.rt r hr y hy + omega + · have h1 : b.val = u.val + 3360 := by rw [c]; exact Endp.val_reply (hP.e.tcb u hu) + by_cases euy : u = y + · subst euy; omega + · have := apart u hu euy; omega + refine ⟨?_, ?_, ?_⟩ + · exact key 3336#u64 _ (by simp) (by unfold Notif.tBlockA; rfl) (Endp.a_blk hya).1 + · exact key 3504#u64 _ (by simp) (by rw [prevEp_eq]) (Endp.a_prev hya).1 + · exact key 3496#u64 _ (by simp) (by rw [nextEp_eq]) (Endp.a_next hya).1 + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3SendNoDonArmA.lean b/proofs/Fl4ProofsA64/P3/Stage3SendNoDonArmA.lean new file mode 100644 index 000000000..223e20459 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SendNoDonArmA.lean @@ -0,0 +1,368 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2b-2): **the non-donating Endpoint call to a waiting receiver, at the record**: a +call with grant or grant-reply of a sender without a fault and without extra caps, to a receiver blocked with a reply +object that **has its own SC**. The reply is pushed (the sender blocks on it) but nothing is donated; the receiver +keeps its SC and runs; the reply stacks are unchanged. + +* **`JSyscallA.sendNoDonA`**: `decode_invocation` (`blocking`, `call`, `can_donate`) on an Endpoint send cap whose + endpoint holds waiting receivers. +* **`JSyscallA.sendNoDonDispA`**: the same arm in the dispatch top's shape (`DispPostA`). + +[S] (scope selectors) `hg` (grant or grant-reply), `hsnd` (the sender has no fault and no extra caps), `hrcv` (the +receiver waits with a reply object and has an SC). +[P] `ArchTotal`, `MaxSpi`, `ArchPlat`, `A64FrameMasks`, `hCs`, `h1` (`numNodes = 1`), `hmb`, `hmax`, `htu`. New module. +-/ +import Fl4ProofsA64.P3.Stage3SendQA +import Fl4ProofsA64.P3.Stage3SendRecvArmA +import Fl4ProofsA64.P3.Stage3SendDonateKeepA +import Fl4ProofsA64.P3.Stage3SendNoDonKeepA +import Fl4ProofsA64.P3.Stage3IpcWakeA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {J5 J7 : JSlotA S} {s : S} {a : A} {σ : State Unit} {m : McState} + +set_option maxHeartbeats 2000000 in +/-- **The non-donating call to a waiting receiver, at a syscall entry** (module docstring). -/ +theorem JSyscallA.sendNoDonA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p badge slot lbl len buf : U64} {recv grant grantReply blk fp : Bool} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) + (hcap : capA st s slot = .Endpoint p badge true recv grant grantReply) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hp : p ∈ E) {d : U64} {q : List ObjRef} + (hk : σ.kheap (specPtr p) = some (.endpoint (.recvEP (specPtr d :: q)))) + (hsnd : ∀ x, σ.kheap σ.curThread = some (.tcb x) → x.tcbFault = none ∧ (grant = false ∨ + ipcBufOfA false x = none ∨ + unat (dataToMessageInfo (x.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0)) + (hg : grant = true ∨ grantReply = true) + (hrcv : ∀ y, σ.kheap (specPtr d) = some (.tcb y) → + y.tcbSchedContext ≠ none ∧ ∃ e r dd, y.tcbState = .blockedOnReceive e (some r) dd) : + ∃ xc σ1 σ2 s2, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ((), σ2) ∈ (sendIpc (Z := Unit) blk true badge.bv grant grantReply true σ.curThread (specPtr p) σ1).1 ∧ + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk true true fp buf extra = + ok (.Ok (), s2, a) ∧ + Sched.WakeEndA σ σ2 ∧ + ∃ Q Rl, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf + stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s2 a σ2 m := by + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hslot hcov J.actW J.running J.cur + have hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM := J.toJStage3EntryA.curNotVmA + obtain ⟨xc, hkc, hst, hsp, J1, -, hOS1, -⟩ := J.toJStage3A.of_restartCurA + RS.pl.choose_spec.choose_spec.choose_spec.choose_spec RS.sym RS.run hnvm RS.cur RS.vs RS.vc + have hws : WritesIn st s (L.wr s (stA (L.curW s)) sched.TS_RESTART) (fun b => b = stA (L.curW s)) := by + refine writesIn_wr ?_ + rw [stA_eq] + exact field_aligned (J.sr.core.tcbs _ J.sr.curT) (by simp) (by simp) + have hst1 : L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART) (stA (L.curW s)) = sched.TS_RESTART := + rd_wr_eq (by rw [stA_eq]; exact field_aligned (J.sr.core.tcbs _ J.sr.curT) (by simp) (by simp)) + generalize hs1 : L.wr s (stA (L.curW s)) sched.TS_RESTART = s1 at hst J1 hOS1 hws hst1 + set t := L.curW s with ht_def + set σ1 := stUpdA σ σ.curThread xc .restart with hσ1 + have kb1 : L.ksb s1 = L.ksb s := hws.1 + have cpu1 : L.cpu s1 = L.cpu s := hws.2.1 + have ct : σ.curThread = specPtr t := J.j.1.sch.curThread + have htZ : t ∈ Z.T := J.sr.curT + set xt : Tcb := { xc with tcbState := .restart } with hxt + have hkt : σ1.kheap (specPtr t) = some (.tcb xt) := by + rw [hσ1, ← ct]; simp [stUpdA, setObjA, hxt] + have ne1 : specPtr p ≠ σ.curThread := fun e => by rw [e, hkc] at hk; cases hk + have hk1 : σ1.kheap (specPtr p) = some (.endpoint (.recvEP (specPtr d :: q))) := by + rw [hσ1]; simp only [stUpdA, setObjA, Function.update_of_ne ne1]; exact hk + -- the receiver + obtain ⟨hep2, xs, -, hq, hQ, hB⟩ := J1.ep.at_ep hp hk1 + have hdx : d ∈ xs := by + have : specPtr d ∈ xs.map specPtr := by rw [← hq]; exact List.mem_cons_self + obtain ⟨y, hy, e⟩ := List.mem_map.1 this + rwa [specPtr_inj e] at hy + have hd : d ∈ Z.T := hQ.mem d hdx + have hdst : (L.mem s1 (stA d)).val = 3 := (hB d hdx).1 + have hdb : L.mem s1 (Notif.tBlockA d) = p := (hB d hdx).2 + obtain ⟨xd, hkd, rd⟩ := J1.ep.tcb d hd + obtain ⟨e', rep, dd, hstd⟩ := tsCode_three (rd.st.symm.trans hdst) + have htd : t ≠ d := fun h => by + have : (L.mem s1 (stA t)).val = 3 := h ▸ hdst + rw [hst1] at this + unfold sched.TS_RESTART at this + simp at this + have hdt : specPtr d ≠ specPtr t := fun h => htd (specPtr_inj h).symm + have hkdσ : σ.kheap (specPtr d) = some (.tcb xd) := by + rw [← hkd, hσ1, ct]; simp only [stUpdA, setObjA, Function.update_of_ne hdt] + -- the sender and the receiver in the spec + obtain ⟨hft, hcaps⟩ : xt.tcbFault = none ∧ (grant = false ∨ ipcBufOfA false xt = none ∨ + unat (dataToMessageInfo (xt.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0) := hsnd xc hkc + have hc : m.curCore < fl4A64Platform.numNodes := by rw [J1.j.1.sch.core]; exact hCs _ J1.j.1.sch.cpuCs + have hO : SchedOpsEA st Z.T E Rp Bf (IpcSchedRelA st Z m κ) := + (ipcSchedOpsA (ar := ar) J1.iplaced J1.ipcPlace hc hmb hmax htu).2.2.1 + have hF : SchedFootEA st Z.T E Rp Bf (IpcSchedRelA st Z m κ) (fun σ s => MWrA (st := st) Z m σ s) := + schedFootEA_of_sched + have hSR : IpcSchedRelA st Z m κ s1 σ1 := ⟨Z.Q, Z.R, J1.sr⟩ + have hdth : Z.th d = some .tcb := (J1.sr.thT d).2 hd + have hon : L.mem s1 (affA d) = L.cpu s1 := by + have x := hCs _ (J1.sr.core.aff d hd) + have y := hCs _ J1.j.1.sch.cpuCs + exact UScalar.eq_of_val_eq (by omega) + have hnb : tsCode xt.tcbState ≠ 3 := by simp [hxt, tsCode] + have hu := J1.bufFrameA hd hkd true + have hrep := J1.repOKA hd + have hoff := J1.slotOffEwA + have hidle : specPtr t ≠ σ1.idleThread ∧ specPtr d ≠ σ1.idleThread := by + obtain ⟨x, hx, hs⟩ := J1.sinv.idleTcb + refine ⟨fun h => ?_, fun h => ?_⟩ + · rw [← h, hkt] at hx; cases hx; cases hs + · rw [← h, hkd] at hx; cases hx; rw [hstd] at hs; cases hs + -- the receiver's reply object, the sender's SC, the donation's side conditions + obtain ⟨hdsc, er, rr, edd, hsr⟩ := hrcv xd hkdσ + have hrep0 : rep = some rr := by + have h := hstd.symm.trans hsr + injection h + subst hrep0 + have hrcvw := rd.recv _ _ _ hstd + obtain ⟨-, hrr⟩ := optRef_some hrcvw.1 + have hRrp : L.mem s1 (tReplyA d) ∈ Rp := hrcvw.2 (by simp) + set r : U64 := L.mem s1 (tReplyA d) with hr_def + subst hrr + have he' : e' = specPtr p := by + have h := rd.blk e' (by rw [hstd]; rfl) + rw [← h, hdb] + subst he' + have hrep := J1.repOKA hd + obtain ⟨xr, hkr, hback⟩ := hrep xd hkd _ _ _ hstd + have hnbr : L.mem s1 (stA (L.mem s1 (Endp.rTcbA r))) ≠ sched.TS_BLOCKED_ON_REPLY := by + obtain ⟨yr, hyr, er⟩ := J1.ep.reply r hRrp + rw [hkr] at hyr + cases hyr + rw [hback] at er + rw [specPtr_inj (optRef_some er.symm).2] + intro h + rw [h] at hdst + revert hdst + unfold sched.TS_BLOCKED_ON_REPLY + simp + have hri := J1.replyIsolatedA hRrp hnbr + set sc : U64 := L.mem s1 (scA t) with hsc_def + have htH : t ∈ H.tcbs := by rw [J.tcbs]; exact htZ + have kt := spec_at (sched.set_thread_state.restart_blk (Es := ∅) s t (BXI.blk J.sys.ent.bxi) + htH (runnable_ok J.running).1 (runnable_ok J.running).2) hst + have kt1 := kt.2.2.2 + have hsc1 : L.mem s1 (scA t) ≠ 0#u64 := by rw [kt1.scf htH]; exact J.cur.2 + have hsc0 : sc ≠ 0#u64 := hsc1 + have htSc : xt.tcbSchedContext = some (scNameA m sc) := by + rw [Endp.schedTcbScA J1.sr htZ hkt, ← hsc_def, optScA, if_neg hsc0] + rfl + have hDon : DonA σ1 (specPtr t) xt := by + intro sc' hsc' + obtain ⟨y, k, hy, hb⟩ := J1.sr.inv.back (specPtr t) xt sc' hkt hsc' + refine ⟨fun h => ?_, fun y' k' hy' => by rw [hy] at hy'; cases hy'; exact hb⟩ + obtain ⟨x, yi, ki, -, -, hyi, hti⟩ := J1.sinv.idleSc + rw [h, hyi] at hy + cases hy + exact hidle.1 (Option.some.inj (hb.symm.trans hti)) + have hni : NotIdleScA m (scNameA m sc) := by + have h1' : scNameA m sc ≠ idleScPtr := (hDon _ htSc).1 + refine ⟨h1', fun c hc' e => ?_⟩ + have hcc : c = m.curCore := by + have a : c < 1 := h1 ▸ hc' + have b : m.curCore < 1 := h1 ▸ hc + exact (Nat.lt_one_iff.1 a).trans (Nat.lt_one_iff.1 b).symm + rw [hcc] at e + unfold scNameA at e h1' + split_ifs at e h1' with g + · exact h1' rfl + · exact g e + have affE : ∀ u ∈ Z.T, L.mem s1 (affA u) = L.cpu s1 := fun u hu => by + have x := hCs _ (J1.sr.core.aff u hu) + have y := hCs _ J1.j.1.sch.cpuCs + exact UScalar.eq_of_val_eq (by omega) + have hRun : SndRunA σ1 (specPtr t) xt := + J.sndRunA hCs h1 (by rw [hxt]; rfl) (fun dm pr hm => by + refine J1.sinv.ctQ (by show σ.schedulerAction = .resumeCurThread; rw [J.sr.sch.act]; unfold actOfA; rw [if_pos J.actW]) dm pr ?_ + rw [show σ1.curThread = specPtr t from ct] + exact hm) rfl rfl hidle.1 + have hal := J1.bufAlign0A hd hkd true + have hRB : RelIfBound σ1 (specPtr t) := J1.sr.rd _ _ hkt (Or.inl hRun.1) + have hrl : specPtr d ∉ σ1.releaseQueue := fun h => by + obtain ⟨x, hx, hr⟩ := J1.sr.run.2 _ h + rw [hkd] at hx + cases hx + rw [hstd] at hr + cases hr + have hRBd : RelIfBoundSc σ1 (specPtr d) := J1.sr.rd _ _ hkd (Or.inr (Or.inr (Or.inr ⟨_, _, _, hstd⟩))) + have hRel := relIfBoundSc_noDonPushA (st := st) J1.sr.place s1 J1.j.1.st J1.j.1.cv hdth hk1 + (epDeq (.recvEP (specPtr d :: q)) (specPtr d)) hkt hkd hft hal hdsc hrl hRBd badge.bv grant hcaps (specPtr r) + have pd := send_ipc_recv_pushNoDon_fpA (st := st) (ar := ar) a J1.sr J1.ipcPlace J1.iplaced.e J1.ep J1.reply + J1.sr.place J1.j.1.st J1.j.1.cv J1.bufCover J1.bufNative hoff htZ hkt hRun hp hk1 hd hkd hu hrep badge grant + grantReply blk true (Or.inl ⟨hft, hcaps⟩) (sc := sc) (r := r) hsc0 hRrp hri rfl hdsc (affE d hd) hni hstd + (Or.inl rfl) hg hRel + -- P1's invariants after the send + obtain ⟨Pt, Ub, Pp7, Tb7, Hr, Te, Kt, Lv7, Re, bits, KLv, W⟩ := J1.j7.unp + have I := J1.delIn hA hPl W + have D := J.decPreA hslot hext + have hc1 := D.cb.restart (BXI.blk J.sys.ent.bxi).1.choose_spec.choose_spec.tcbs kt1 kt.2.2.1 + have hH : H p = some .ep := J.utIpc.ep p hp + have P := (J.toJStage3A.sysPlace hext hslot hcov).keepO hOS1 kb1 + have hnames : ∀ k, H p = some k → ∃ x ∈ OS, NamesA (capA st s1 x) p k := fun k hk' => by + rw [hH] at hk' + cases hk' + exact ⟨slot, hslot, by rw [hOS1.capA hslot, hcap]; exact names_ep⟩ + have p1 := Fl4ProofsA64.P1.W7.R.ST.endpoint.send_ipc.recvS (st := st) (ar := ar) (Vc' := Z.V) (Vc := Z.V) + (Ub := Ub) (Pt := Pt) hA a blk true badge grant grantReply true t p P (BXI.blk J1.sys.ent.bxi) I.del.sca I.ts + J1.sys.sysl J1.symL hH (by rw [epState_eq]; exact hep2) hnames hc1 + (by rw [hst1]; unfold sched.TS_RESTART sched.TS_INACTIVE; decide) (fun _ => hsc1) + (D.rv.restart kt1 (D.rv.zero htH D.nvm)) + obtain ⟨⟨s2, a2⟩, eb, ⟨⟨ha2, σ2, hspecI, E2, ⟨Q, Rl, sr2⟩, rs2, wD⟩, p1post⟩⟩ := + exists_of_specA (WP.spec_and pd p1) + simp only at ha2 eb E2 sr2 rs2 wD p1post + subst ha2 + have hmem : ((), σ2) ∈ (sendIpc (Z := Unit) blk true badge.bv grant grantReply true (specPtr t) (specPtr p) σ1).1 := by + rw [hspecI]; exact Set.mem_singleton _ + have blk2 := p1post.2.2.1 + have hdec : syscall.decode_invocation st ar s a2 lbl len slot (capA st s slot) blk true true fp buf extra = + ok (.Ok (), s2, a2) := by + rw [hcap, decodeEndpoint_programA] + simp only [↓reduceIte, ipcInvokeProgramA, cur_thread_ok, bind_ok, ← ht_def, hst] + rw [show endpoint.send_ipc st ar s1 a2 blk true badge grant grantReply true t p = ok (s2, a2) from eb] + simp only [bind_ok] + rfl + have F := J.decodeFinA hA hM hPl hfm hslot hcov (rfl : capA st s slot = capA st s slot) hext hSC (lbl := lbl) + (by rw [hcap]; simp [DecArmS1aEnt, DecArmQH, DecArmQZ2, DecArmKQ2, DecArmKQ, capFam]) + (fun _ h => by rw [hcap] at h; cases h) (fun _ h => by rw [hcap] at h; cases h) hdec σ2 m + obtain ⟨-, -, sysl, symL, -, userOK, j7⟩ := F + -- the spec outcome's record facts + set M : ObjRef → Prop := fun o => o = specPtr t ∨ o = specPtr d with hMdef + set Nq : ObjRef → Prop := fun o => o = specPtr d with hNdef + have K : SIKA σ1.idleThread M Nq true σ1 σ2 := + sendIpc_recv_sikA (M := M) (N := Nq) J1.sr.place s1 J1.j.1.st J1.j.1.cv hdth hk1 hkt hkd hft hal blk true + badge.bv grant grantReply true hcaps (Or.inl rfl) (Or.inr rfl) rfl hidle.1 hidle.2 hDon + (fun _ => by rw [htSc]; simp) (fun c => by cases c) _ hmem + have U2 : UFrA (BufBytesA (ipcBufOfA true xd)) σ1 σ2 := + sendIpc_recv_ufrA J1.sr.place s1 J1.j.1.st J1.j.1.cv hdth hk1 hkt hkd hft hal blk true badge.bv grant grantReply + true hcaps _ hmem + have hact : ActTcbA σ1 (specPtr t) := by + have h := (J1.schedValid.act J.act (fun c => hidle.1 (ct ▸ c))).1 + obtain ⟨x, sc', ex, es, a'⟩ := h + exact ⟨x, sc', ct ▸ ex, es, a'⟩ + have Qs : QSendA σ1 σ2 := + sendIpc_recv_qsA (i := σ1.idleThread) (M := M) (N := Nq) J1.sr.place s1 J1.j.1.st J1.j.1.cv hdth hk1 hkt hkd hal + blk true badge.bv grant grantReply true (Or.inl ⟨hft, hcaps⟩) (Or.inl rfl) (Or.inr rfl) rfl hidle.1 hidle.2 hDon + (fun _ => by rw [htSc]; simp) (fun _ => scActive_of_actTcbA hact hkt) + (fun e' r dd h => by + obtain ⟨xr, k, b⟩ := J1.repOKA hd xd hkd e' r dd h + exact ⟨xr, k, by rw [b]; simp⟩) _ hmem + have sv : SchedValidA σ2 := + sendIpc_recv_schedValidA J1.sr.place s1 J1.j.1.st J1.j.1.cv hdth hk1 hkt hkd hft hal blk true badge.bv grant + grantReply true hcaps hstd + (Or.inr (Or.inr (Or.inr ⟨rfl, hg, by simp, rfl, hdt.symm, hdsc, hact⟩))) J1.schedValid J1.rqDomPrioA _ hmem + have run : StIs (specPtr d) .running σ2 := sendIpc_recv_runningA hk1 blk true badge.bv grant grantReply true _ hmem + have snt : StIs (specPtr t) (.blockedOnReply (specPtr r)) σ2 := + sendIpc_push_senderA hk1 hkd hstd hdt.symm blk badge.bv grant grantReply true hg _ hmem + -- the senders and receivers before and after + have hMs : ∀ u ∈ Z.T, M (specPtr u) → ∀ x, σ1.kheap (specPtr u) = some (.tcb x) → + (x = xt ∨ x = xd) := by + intro u _ hu x hx + rcases hu with h | h + · rw [h, hkt] at hx; cases hx; exact Or.inl rfl + · rw [h, hkd] at hx; cases hx; exact Or.inr rfl + have after : ∀ u ∈ Z.T, M (specPtr u) → ∀ x, σ2.kheap (specPtr u) = some (.tcb x) → + epBlocked x.tcbState = none ∧ ∀ p, x.tcbState ≠ .blockedOnNotification p := by + intro u _ hu x hx + rcases hu with h | h + · rw [h] at hx + rw [snt x hx] + exact ⟨rfl, fun _ h => by cases h⟩ + · rw [h] at hx + rw [run x hx] + exact ⟨rfl, fun _ h => by cases h⟩ + have wk : NtfnWaitKeepA Z.T N (L.mem s1) (L.mem s2) := + J1.ntfnWait_sendA (W := True) htZ hp hd hdb (wD.mono fun _ h => h.imp id (Or.imp id fun c => ⟨t, htZ, c⟩)) + have wI : WritesIn st s1 s2 (fun b => IpcFpA st Z E Rp Bf m σ1 s1 b ∨ RepWA Rp b) := + wD.mono fun _ h => donFpA_splitA J1.sr.core.tcbs htZ hp hd h + have wN : WritesIn st s1 s2 (fun b => SchedStepNWA (st := st) N E Rp Z m σ1 s1 b ∨ BufW Bf b) := + wI.mono fun _ h => h.elim + (fun h => h.elim (ewrite_stepNA_buf J1.sr.core.tcbs) fun h => Or.inl (Or.inl (Or.inl h))) + fun h => Or.inl (Or.inr (Or.inr (Or.inr h))) + have notM : ∀ c < n, ¬ M (m.idleThreadOf c) := by + intro c hc' h + obtain ⟨y, hy, hs⟩ := J1.sidle.f3 c hc' + rcases h with h | h + · rw [h, hkt] at hy; cases hy; cases hs + · rw [h, hkd] at hy; cases hy; rw [hstd] at hs; cases hs + have sinv : SInvXA σ2 m n := by + refine K.sinv J1.sinv notM (fun h => h.elim (fun h => hidle.1 h.symm) fun h => hidle.2 h.symm) ?_ ?_ ?_ ?_ + (fun y hy hr => by + rcases hy with rfl | rfl + · exact hRun.2.1.2 hr + · obtain ⟨x, hx, hrn⟩ := J1.sinv.rlsRun _ hr; rw [hkd] at hx; cases hx; rw [hstd] at hrn; cases hrn) + (J1.sidle.rdyA J1.sinv) + (fun q hq x y hx hy hf => by + rcases hq with rfl | rfl + · rw [snt y hy]; exact Or.inr (Or.inl ⟨_, rfl⟩) + · obtain ⟨x0, hx0, g⟩ := K.tcbB hy + rw [hkd] at hx0; cases hx0 + exact absurd (g.fault.trans (FaultStOkA.none J1.sinv.faultSt hkd (by + rw [hstd]; rintro (⟨_, _, h⟩ | ⟨_, h⟩ | h) <;> cases h))) hf) + · intro y hy dm pr hm + rcases hy with rfl | rfl + · refine J1.sinv.ctQ (by show σ.schedulerAction = .resumeCurThread; rw [J.sr.sch.act]; unfold actOfA; rw [if_pos J.actW]) dm pr ?_ + rw [show σ1.curThread = specPtr t from ct] + exact hm + · obtain ⟨x, hx, hr⟩ := J1.sinv.rq dm pr _ hm + rw [hkd] at hx; cases hx; rw [hstd] at hr; cases hr + · intro y hy + cases hy + refine ⟨fun h => hdt (h.trans (show σ1.curThread = specPtr t from ct)), hidle.2, fun c hc' h => + notM c hc' (Or.inr h), fun z hz => by rw [run z hz]; rfl⟩ + · intro y hy + cases hy + exact ⟨xd, hkd⟩ + · exact J.noSwitch + have hMv : ∀ u ∈ Z.T, M (specPtr u) → L.mem s1 (stA u) ≠ sched.TS_RUNNING_VM := by + intro u _ hu h + rcases hu with e | e <;> rw [specPtr_inj e] at h + · rw [hst1] at h; revert h; unfold sched.TS_RESTART sched.TS_RUNNING_VM; decide + · rw [h] at hdst; revert hdst; unfold sched.TS_RUNNING_VM; decide + have ntfn := J1.ntfn_ipcA wI K E2 wk (fun u hu hm x hx p h => by + rcases hMs u hu hm x hx with rfl | rfl + · cases h + · rw [hstd] at h; cases h) (fun u hu hm x hx => (after u hu hm x hx).2) + have hdr : ∀ q' ∈ Rp, DeadA st OS .reply s1 q' → ∀ c ∈ Z.C, q' ∉ stack c := + fun q' hq' hdead c hc' hm => J1.deadExt.reply q' hq' hdead c hc' hm + have J2 := J1.of_ipcStepUA (Q := Q) (Rl := Rl) (curOf := Z.curOf) (curScOf := Z.curScOf) wN U2.1 U2.2.1 Qs + (J1.specZero_of_recvBufA hd hkd U2.2.2) (fun _ _ => rfl) (BlkOK.bxi blk2) sysl symL + userOK j7 sr2 E2 ntfn rs2 hdr K.ipcKeep (J1.tw_ipcA wI) sinv (K.sidle J1.sidle notM (J1.sidle.rdyA J1.sinv)) + (J1.runVcpu_ipcA wI K E2 hMv) (J1.blkQ_ipcA K E2 wk after) sv + have hwake : Sched.WakeEndA σ σ2 := by + have E3 := Sched.sendIpc_recv_endA J1.sr.place s1 J1.j.1.st J1.j.1.cv hdth hk1 hkt hkd hal blk true badge.bv + grant grantReply true (Or.inl ⟨hft, hcaps⟩) hstd + (Or.inr (Or.inr (Or.inr (Or.inl ⟨Or.inl rfl, hg, by simp, rfl, hdt.symm, hdsc, hact⟩)))) + J1.schedValid J1.rqDomPrioA J.noSwitch hDon hidle.1 hidle.2 _ hmem + have B : Sched.BLA σ1 σ2 := Sched.sendIpc_recv_blA J1.sr.place s1 J1.j.1.st J1.j.1.cv hdth hk1 hkt hkd hft hal + blk true badge.bv grant grantReply true hcaps _ hmem + have B0 : Sched.BLA σ σ1 := Sched.BLA.tcbW hkc + refine ⟨PreO.trans B0 B, E3.1, fun hres => absurd hres (E3.2 ⟨show σ1.curThread = specPtr t from ct, Or.inl rfl⟩)⟩ + exact ⟨xc, σ1, σ2, s2, hkc, rfl, hsp, by rw [ct]; exact hmem, hdec, hwake, Q, Rl, J2⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3SendNoDonKeepA.lean b/proofs/Fl4ProofsA64/P3/Stage3SendNoDonKeepA.lean new file mode 100644 index 000000000..278af2b44 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SendNoDonKeepA.lean @@ -0,0 +1,108 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2b-2): **spec facts of the non-donating reply-push call to a waiting receiver** +(`send_ipc_recv_pushNoDon_fpA`): a call with grant or grant-reply to a receiver blocked with a reply object that has +its own SC. + +* **`replyPush_calleeScA`**: the spec `reply_push` with donation allowed to a callee with an SC is the one without. +* **`relIfBoundSc_noDonPushA`**: the receiver's `RelIfBoundSc` after the push (the body's `hReleased`): its own SC, + released before the send (`RdA` of a waiting receiver), with the same refills and clock. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3SendDonateKeepA +import Fl4ProofsA64.P3.Stage3SendFaultSpecA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P3 Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Classical + +noncomputable section + +/-- **The spec `reply_push` with donation allowed, to a callee that already has an SC, donates nothing.** -/ +theorem replyPush_calleeScA {σ : State Unit} {t d r : ObjRef} {x y : Tcb} + (ht : σ.kheap t = some (.tcb x)) (hd : σ.kheap d = some (.tcb y)) (hsc : y.tcbSchedContext ≠ none) : + replyPush (Z := Unit) t d r true σ = replyPush (Z := Unit) t d r false σ := by + unfold replyPush + rw [nd_bind_one (getTcbObjRef_eq _ ht), nd_bind_one (getTcbObjRef_eq _ ht)] + rw [nd_bind_one (getTcbObjRef_eq _ hd), nd_bind_one (getTcbObjRef_eq _ hd)] + simp [hsc] + +section +open fl4_core_a64 Fl4ProofsA64.P1 +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **The receiver's `RelIfBoundSc` after the non-donating push** (module docstring). -/ +theorem relIfBoundSc_noDonPushA {th : TypedHeapA} {kb : U64} (hPl : PlaceA th kb) (sf : S) {σ : State Unit} + (hR : StateRelA th (L.mem sf) σ) (hV : SlotsValidA th (L.mem sf)) {t d : U64} {e : ObjRef} {z : Endpoint} + (htr : th d = some .tcb) (hk : σ.kheap e = some (.endpoint z)) (z' : Endpoint) + {xt xd : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) (hkd : σ.kheap (specPtr d) = some (.tcb xd)) + (hf : xt.tcbFault = none) (hal : ∀ rb, ipcBufOfA true xd = some rb → (bufAddr rb 0).toNat % 8 = 0) + (hdsc : xd.tcbSchedContext ≠ none) (hrl : specPtr d ∉ σ.releaseQueue) + (hRBd : RelIfBoundSc σ (specPtr d)) (badge : Badge) (g : Bool) + (hcaps : g = false ∨ ipcBufOfA false xt = none ∨ + unat (dataToMessageInfo (xt.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0) (r : ObjRef) : + ∀ σ2 σ3 σ4, + doIpcTransfer (Z := Unit) (specPtr t) (some e) badge g (specPtr d) + (updK σ e (.endpoint z')) = ({((), σ2)}, False) → + replyUnlinkTcb (Z := Unit) (specPtr d) r σ2 = ({((), σ3)}, False) → + replyPush (Z := Unit) (specPtr t) (specPtr d) r true σ3 = ({((), σ4)}, False) → + RelIfBoundSc σ4 (specPtr d) := by + intro σ2 σ3 σ4 hT hU hP + set σ0 := updK σ e (.endpoint z') with hσ0 + have hte : specPtr t ≠ e := fun h => by rw [← h, hkt] at hk; cases hk + have hde : specPtr d ≠ e := fun h => by rw [← h, hkd] at hk; cases hk + have hkt0 : σ0.kheap (specPtr t) = some (.tcb xt) := by rw [hσ0, updK_kheap, Function.update_of_ne hte, hkt] + have hkd0 : σ0.kheap (specPtr d) = some (.tcb xd) := by rw [hσ0, updK_kheap, Function.update_of_ne hde, hkd] + have HS : SpecSchedA σ σ0 := by + refine ⟨fun p => ?_, rfl, rfl, rfl, rfl, rfl, rfl⟩ + by_cases h : p = e + · subst h + exact Or.inr (Or.inr ⟨_, _, hk, by rw [hσ0, updK_kheap, Function.update_self], + Or.inr (Or.inr (Or.inl ⟨_, _, rfl, rfl⟩))⟩) + · exact Or.inl (by rw [hσ0, updK_kheap, Function.update_of_ne h]) + have R0 : RfKA σ σ0 := RfKA.upd fun _ _ h => by cases h + have m2 : ((), σ2) ∈ (doIpcTransfer (Z := Unit) (specPtr t) (some e) badge g (specPtr d) σ0).1 := by + rw [hT]; exact Set.mem_singleton _ + have m3 : ((), σ3) ∈ (replyUnlinkTcb (Z := Unit) (specPtr d) r σ2).1 := by rw [hU]; exact Set.mem_singleton _ + have m4 : ((), σ4) ∈ (replyPush (Z := Unit) (specPtr t) (specPtr d) r true σ3).1 := by + rw [hP]; exact Set.mem_singleton _ + have R2 : RfKA σ0 σ2 := doIpcTransfer_rfA hPl sf (hR.specSchedA HS) hV htr hkt0 hkd0 hf hal _ _ _ hcaps _ m2 + have R3 : RfKA σ2 σ3 := replyUnlinkTcb_rfA _ _ _ _ m3 + have R4 : RfKA σ3 σ4 := replyPush_rfA _ _ _ _ _ _ m4 + have RF : RfKA σ σ4 := PreO.trans R0 (PreO.trans R2 (PreO.trans R3 R4)) + -- the receiver's TCB keeps its SC along the transfer, the unlink and the (non-donating) push + have K2 : SIKA σ.idleThread (fun p => p = specPtr d ∨ p = specPtr t) (fun _ => False) false σ0 σ2 := + doIpcTransfer_sikA hPl sf (hR.specSchedA HS) hV htr hkt0 hkd0 hf hal _ _ _ hcaps σ0 rfl _ m2 + have K3 : SIKA σ.idleThread (fun p => p = specPtr d ∨ p = specPtr t) (fun _ => False) false σ2 σ3 := + replyUnlinkTcb_sikA (i := σ.idleThread) (M := fun p => p = specPtr d ∨ p = specPtr t) (N := fun _ => False) + (don := false) (specPtr d) r (Or.inl rfl) _ _ m3 + have K23 := K2.trans K3 + obtain ⟨xt3, hkt3, -⟩ := K23.tcbF hkt0 + obtain ⟨xd3, hkd3, gd⟩ := K23.tcbF hkd0 + have ed : xd3.tcbSchedContext = xd.tcbSchedContext := gd.sc.resolve_right fun c => by cases c.1 + have hP' : replyPush (Z := Unit) (specPtr t) (specPtr d) r false σ3 = ({((), σ4)}, False) := + (replyPush_calleeScA (r := r) hkt3 hkd3 (by rw [ed]; exact hdsc)).symm.trans hP + have m4' : ((), σ4) ∈ (replyPush (Z := Unit) (specPtr t) (specPtr d) r false σ3).1 := by + rw [hP']; exact Set.mem_singleton _ + have K4 : SIKA σ.idleThread (fun p => p = specPtr d ∨ p = specPtr t) (fun _ => False) false σ3 σ4 := + replyPush_noDon_sikA (specPtr t) (specPtr d) r (Or.inr rfl) σ3 _ m4' + have K := K23.trans K4 + obtain ⟨xd4, hkd4, g4⟩ := K.tcbF hkd0 + have e4 : xd4.tcbSchedContext = xd.tcbSchedContext := g4.sc.resolve_right fun c => by cases c.1 + intro x scp y n hx hscp hy ha hnr + have hxd : xd4 = x := KernelObject.tcb.inj (Option.some.inj (hkd4.symm.trans hx)) + subst hxd + have hscp0 : xd.tcbSchedContext = some scp := e4.symm.trans hscp + obtain ⟨y0, hy0, er, em⟩ := RF.2.2 _ _ _ hy + have a0 : scActive y0 = true := by unfold scActive at ha ⊢; rw [← em]; exact ha + have rel := hRBd xd scp y0 n hkd hscp0 hy0 a0 hrl + unfold releasedSc at rel ⊢ + have eh : refillHd y = refillHd y0 := by unfold refillHd; rw [er] + rw [RF.1, eh] + exact rel + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3SendNoblockA.lean b/proofs/Fl4ProofsA64/P3/Stage3SendNoblockA.lean new file mode 100644 index 000000000..6ec5917d5 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SendNoblockA.lean @@ -0,0 +1,72 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2b): **non-blocking Endpoint send / call with no receiver, at the record.** + +**`JSyscallA.sendNoblockA`**: `decode_invocation` on an Endpoint send cap at a syscall entry with `block = false` and an +endpoint that is idle or holds waiting senders. FL4 and the spec's `send_ipc` do nothing past the caller's Restart, so +the record is the one `of_restartCurA` gives. Any `call` / `donate` / grant flags. + +**`JSyscallA.sendNoblockDispA`** is the same arm in the dispatch top's shape (`DispPostA`), for an IPC group. + +No named premise beyond those of the record. New module. +-/ +import Fl4ProofsA64.P3.Stage3SignalIdleA +import Fl4ProofsA64.P3.EpSendBlkA +import Fl4ProofsA64.P3.Stage3DispBaseA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s : S} {a : A} {σ : State Unit} {m : McState} + +set_option maxHeartbeats 1000000 in +/-- **Non-blocking send with no receiver, at a syscall entry** (module docstring). -/ +theorem JSyscallA.sendNoblockA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {p badge slot lbl len buf : U64} {recv grant grantReply call don fp : Bool} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) + (hcap : capA st s slot = .Endpoint p badge true recv grant grantReply) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) + (hp : p ∈ E) {x : Endpoint} (hk : σ.kheap (specPtr p) = some (.endpoint x)) (hc : CanBlockA false x) : + ∃ xc σ1 s1, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + sendIpc (Z := Unit) false call badge.bv grant grantReply don σ.curThread (specPtr p) σ1 = + ({((), σ1)}, False) ∧ + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) false call don fp buf extra = + ok (.Ok (), s1, a) ∧ + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s1 a σ1 m := by + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hslot hcov J.actW J.running J.cur + have hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM := J.toJStage3EntryA.curNotVmA + obtain ⟨xc, hkc, hst, hsp, J1, -, -, -⟩ := J.toJStage3A.of_restartCurA + RS.pl.choose_spec.choose_spec.choose_spec.choose_spec RS.sym RS.run hnvm RS.cur RS.vs RS.vc + set s1 := L.wr s (stA (L.curW s)) sched.TS_RESTART with hs1 + set σ1 := stUpdA σ σ.curThread xc .restart with hσ1 + have ct : σ.curThread = specPtr (L.curW s) := J.j.1.sch.curThread + have ne1 : specPtr p ≠ σ.curThread := fun e => by rw [e, hkc] at hk; cases hk + have hk1 : σ1.kheap (specPtr p) = some (.endpoint x) := by + rw [hσ1]; simp only [stUpdA, setObjA, Function.update_of_ne ne1]; exact hk + have body := send_ipc_noblockA (st := st) (ar := ar) s1 a σ1 J1.ep hp hk1 hc call badge grant grantReply don + (L.curW s) + obtain ⟨r, eb, hr, hspec⟩ := exists_of_specA body + subst hr + refine ⟨xc, σ1, s1, hkc, rfl, hsp, ct ▸ hspec, ?_, J1⟩ + rw [hcap, decodeEndpoint_programA] + simp only [↓reduceIte, ipcInvokeProgramA, cur_thread_ok, bind_ok, hst] + rw [show endpoint.send_ipc st ar (L.wr s (stA (L.curW s)) sched.TS_RESTART) a false call badge grant grantReply don + (L.curW s) p = ok (L.wr s (stA (L.curW s)) sched.TS_RESTART, a) from eb] + simp only [bind_ok] + rfl + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3SendQA.lean b/proofs/Fl4ProofsA64/P3/Stage3SendQA.lean new file mode 100644 index 000000000..c75bddaa9 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SendQA.lean @@ -0,0 +1,387 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2b): **the queue invariants' spec side across `send_ipc` to a waiting receiver.** + +* **`sendIpc_recv_qsA`**: every outcome of `send_ipc` to a waiting receiver is `QSendA` (every transfer case, with or + without reply push and donation). + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3SendFaultSpecA +import Fl4ProofsA64.P3.Stage3QKeepPresA +import Fl4ProofsA64.P3.Stage3SchedHoareA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P3 Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Classical + +noncomputable section + +/-- The step relation of the walk: `QKeepA` except that the replies of `ex` may gain a caller and the SC of `sc0` +may gain replies. -/ +structure QStepA (ex sc0 : Option ObjRef) (σ σ' : State Unit) : Prop where + prio : ∀ t y, σ'.kheap t = some (.tcb y) → ∃ x, σ.kheap t = some (.tcb x) ∧ y.tcbPriority = x.tcbPriority + ep : ∀ p x', σ'.kheap p = some (.endpoint x') → + ∃ x, σ.kheap p = some (.endpoint x) ∧ x'.epQueue.Sublist x.epQueue + ntfn : ∀ p x', σ'.kheap p = some (.notification x') → + ∃ x, σ.kheap p = some (.notification x) ∧ x'.ntfnObj.ntfnQueue.Sublist x.ntfnObj.ntfnQueue + sc : ∀ p y' k, σ'.kheap p = some (.schedContext y' k) → + ∃ y k0, σ.kheap p = some (.schedContext y k0) ∧ (y'.scReplies ≠ [] → y.scReplies ≠ [] ∨ some p = sc0) + rt : ∀ p y, σ'.kheap p = some (.reply y) → + ∃ x, σ.kheap p = some (.reply x) ∧ (some p ≠ ex → x.replyTcb = none → y.replyTcb = none) + +variable {ex sc0 : Option ObjRef} + +theorem _root_.Fl4ProofsA64.P3.QKeepA.toStep {σ σ' : State Unit} (Q : QKeepA σ σ') : QStepA ex sc0 σ σ' := + ⟨Q.prio, Q.ep, Q.ntfn, fun p y k h => by + obtain ⟨y0, k0, h0, a⟩ := Q.sc p y k h + exact ⟨y0, k0, h0, fun r => Or.inl (a r).1⟩, + fun p y h => by + obtain ⟨x, hx, a⟩ := Q.rt p y h + exact ⟨x, hx, fun _ => a⟩⟩ + +theorem QStepA.trans {σ1 σ2 σ3 : State Unit} (h1 : QStepA ex sc0 σ1 σ2) (h2 : QStepA ex sc0 σ2 σ3) : + QStepA ex sc0 σ1 σ3 := by + refine ⟨fun t x hx => ?_, fun p x hx => ?_, fun p x hx => ?_, fun p y k hy => ?_, fun p y hy => ?_⟩ + · obtain ⟨y, hy, ey⟩ := h2.prio t x hx + obtain ⟨z, hz, ez⟩ := h1.prio t y hy + exact ⟨z, hz, ey.trans ez⟩ + · obtain ⟨y, hy, sy⟩ := h2.ep p x hx + obtain ⟨z, hz, sz⟩ := h1.ep p y hy + exact ⟨z, hz, sy.trans sz⟩ + · obtain ⟨y, hy, sy⟩ := h2.ntfn p x hx + obtain ⟨z, hz, sz⟩ := h1.ntfn p y hy + exact ⟨z, hz, sy.trans sz⟩ + · obtain ⟨y2, k2, hy2, a2⟩ := h2.sc p y k hy + obtain ⟨y1, k1, hy1, a1⟩ := h1.sc p y2 k2 hy2 + exact ⟨y1, k1, hy1, fun h => (a2 h).elim a1 Or.inr⟩ + · obtain ⟨y2, hy2, a2⟩ := h2.rt p y hy + obtain ⟨y1, hy1, a1⟩ := h1.rt p y2 hy2 + exact ⟨y1, hy1, fun n h => a2 n (a1 n h)⟩ + +instance : PreO (QStepA ex sc0) := ⟨fun s => (QKeepA.refl s).toStep, QStepA.trans⟩ + +theorem qk_stepA {α : Type} {m : SMonad Unit α} (h : PresR QKeepA m) : PresR (QStepA ex sc0) m := + fun s x hx => (h s x hx).toStep + +/-- The user-memory frame together with `QKeepA`. -/ +def UQA (B : Word → Prop) (σ σ' : State Unit) : Prop := UFrA B σ σ' ∧ QKeepA σ σ' + +instance (B : Word → Prop) : PreO (UQA B) := + ⟨fun s => ⟨PreO.refl s, QKeepA.refl s⟩, fun h1 h2 => ⟨PreO.trans h1.1 h2.1, h1.2.trans h2.2⟩⟩ + +section +open fl4_core_a64 Fl4ProofsA64.P1 +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +theorem storeWordOffs_qA (rb : ObjRef) (k : Nat) : + ∀ v, PresR QKeepA (storeWordOffs (Z := Unit) rb k v) := by + intro v + unfold storeWordOffs + exact PresR.bind PresR.get fun _ => PresR.bind (PresR.assert _) fun _ => doMachineOp_qA _ + +/-- `do_ipc_transfer` (no extra caps, or a fault) keeps the queues. -/ +theorem doIpcTransfer_any_qA {th : TypedHeapA} {kb : U64} (hPl : PlaceA th kb) (sf : S) {σ : State Unit} + (hR : StateRelA th (L.mem sf) σ) (hV : SlotsValidA th (L.mem sf)) {snd rcv : U64} + (htr : th rcv = some .tcb) {xs xr : Tcb} (hks : σ.kheap (specPtr snd) = some (.tcb xs)) + (hkr : σ.kheap (specPtr rcv) = some (.tcb xr)) + (hal : ∀ rb, ipcBufOfA true xr = some rb → (bufAddr rb 0).toNat % 8 = 0) + (ep : Option ObjRef) (badge : Badge) (grant : Bool) (hcase : XferCaseA xs grant) : + PresRC QKeepA (· = σ) (doIpcTransfer (Z := Unit) (specPtr snd) ep badge grant (specPtr rcv)) := by + rcases hcase with ⟨hf, hcaps⟩ | ⟨f, hf⟩ + · intro s hs x hx + exact (doIpcTransfer_genA (R := UQA (BufBytesA (ipcBufOfA true xr))) (B := BufBytesA (ipcBufOfA true xr)) + (fun _ _ h => h.1) (fun t f s y hy => ⟨UFrA.of_fr (asUser_presA t f s y hy), asUser_qA t f s y hy⟩) + (fun a s y hy => ⟨UFrA.of_fr (doMachineOp_presA _ (loadWord_keepA a) s y hy), doMachineOp_qA _ s y hy⟩) + (rbo := ipcBufOfA true xr) + (fun rb e k hk v s y hy => ⟨e ▸ storeWordOffs_ufrA rb hk v s y hy, storeWordOffs_qA rb k v s y hy⟩) + hPl sf hR hV htr hks hkr rfl hf hal ep badge grant hcaps s hs x hx).2 + refine doIpcTransfer_fault_genA hks hkr hf ep badge grant ?_ + refine doFaultTransfer_genA (fun a b e => e ▸ QKeepA.refl a) (fun t g => asUser_qA t g) + (fun p => (inferInstance : QInst (schedContextUpdateConsumed (Z := Unit) p)).pres) _ _ _ _ + (fun s x regs hx => QKeepA.upd hx (QObjA.ofTcb rfl)) + (fun rb _ k _ v => storeWordOffs_qA rb k v) + +theorem bindScReply_stepA (sc r : ObjRef) : PresR (QStepA ex (some sc)) (bindScReply (Z := Unit) sc r) := by + unfold bindScReply + refine PresR.bind (qk_stepA ?_) (fun l => ?_) + · show PresR QKeepA (NonDet.liftM SchedContext.scReplies (getSchedContext (Z := Unit) sc)) + unfold NonDet.liftM + exact PresR.bind (inferInstance : QInst (getSchedContext (Z := Unit) sc)).pres (fun _ => PresR.pure _) + have push : PresR (QStepA ex (some sc)) (setScObjRef (Z := Unit) scRepliesUpdate sc (r :: l)) := by + intro s x hx + obtain ⟨y, n, hk, e⟩ := updateSchedContext_memA sc _ hx + rw [e] + have key : ∀ q w, Function.update s.kheap sc (some (.schedContext (scRepliesUpdate (fun _ => r :: l) y) n)) q + = some w → (q = sc ∧ w = .schedContext (scRepliesUpdate (fun _ => r :: l) y) n) ∨ + (q ≠ sc ∧ s.kheap q = some w) := fun q w h => by + by_cases hq : q = sc + · subst hq; rw [Function.update_self] at h; exact Or.inl ⟨rfl, (Option.some.inj h).symm⟩ + · rw [Function.update_of_ne hq] at h; exact Or.inr ⟨hq, h⟩ + refine ⟨fun q z h => ?_, fun q z h => ?_, fun q z h => ?_, fun q z k h => ?_, fun q z h => ?_⟩ + · rcases key q _ h with ⟨-, c⟩ | ⟨-, h'⟩ + · cases c + · exact ⟨z, h', rfl⟩ + · rcases key q _ h with ⟨-, c⟩ | ⟨-, h'⟩ + · cases c + · exact ⟨z, h', List.Sublist.refl _⟩ + · rcases key q _ h with ⟨-, c⟩ | ⟨-, h'⟩ + · cases c + · exact ⟨z, h', List.Sublist.refl _⟩ + · rcases key q _ h with ⟨rfl, c⟩ | ⟨-, h'⟩ + · exact ⟨y, n, hk, fun _ => Or.inr rfl⟩ + · exact ⟨z, k, h', Or.inl⟩ + · rcases key q _ h with ⟨-, c⟩ | ⟨-, h'⟩ + · cases c + · exact ⟨z, h', fun _ => id⟩ + have rest : PresR (QStepA ex (some sc)) (do + setScObjRef scRepliesUpdate sc (r :: l) + setReplyObjRef (Z := Unit) replyScUpdate r (some sc)) := + PresR.bind push fun _ => qk_stepA (inferInstance : QInst (setReplyObjRef (Z := Unit) replyScUpdate r (some sc))).pres + cases l with + | nil => exact rest + | cons a l => + exact PresR.bind (qk_stepA (inferInstance : QInst (setReplyObjRef (Z := Unit) replyScUpdate a none)).pres) + (fun _ => rest) + +/-- The caller link of a reply push. -/ +theorem setReplyTcb_stepA (r c : ObjRef) : + PresR (QStepA (some r) sc0) (setReplyObjRef (Z := Unit) replyTcbUpdate r (some c)) := by + intro σ z hz + unfold setReplyObjRef updateSkObjRef at hz + obtain ⟨x, hx, hz⟩ := mem_bindA.1 hz + have ex : σ = x.2 := getSimpleKo_eqPres _ r σ x hx + rw [← ex] at hz + have hk := getReplyKo_memA hx + obtain ⟨-, -, hr2⟩ := setSimpleKo_memA _ _ _ hz + rw [hr2] + have key : ∀ (o : Reply) q w, Function.update σ.kheap r (some (.reply o)) q = some w → + (q = r ∧ w = .reply o) ∨ (q ≠ r ∧ σ.kheap q = some w) := fun o q w h => by + by_cases hq : q = r + · subst hq; rw [Function.update_self] at h; exact Or.inl ⟨rfl, (Option.some.inj h).symm⟩ + · rw [Function.update_of_ne hq] at h; exact Or.inr ⟨hq, h⟩ + refine ⟨fun q z h => ?_, fun q z h => ?_, fun q z h => ?_, fun q z k h => ?_, fun q z h => ?_⟩ + · rcases key _ q _ h with ⟨-, c⟩ | ⟨-, h'⟩ + · cases c + · exact ⟨z, h', rfl⟩ + · rcases key _ q _ h with ⟨-, c⟩ | ⟨-, h'⟩ + · cases c + · exact ⟨z, h', List.Sublist.refl _⟩ + · rcases key _ q _ h with ⟨-, c⟩ | ⟨-, h'⟩ + · cases c + · exact ⟨z, h', List.Sublist.refl _⟩ + · rcases key _ q _ h with ⟨-, c⟩ | ⟨-, h'⟩ + · cases c + · exact ⟨z, k, h', Or.inl⟩ + · rcases key _ q _ h with ⟨rfl, c⟩ | ⟨-, h'⟩ + · exact ⟨_, hk, fun n => absurd rfl n⟩ + · exact ⟨z, h', fun _ => id⟩ + +/-- `reply_push` of a caller whose SC is `sc0`. -/ +theorem replyPush_stepA {i : ObjRef} {M N : ObjRef → Prop} {σ : State Unit} {t d r : ObjRef} {x : Tcb} + (hk : σ.kheap t = some (.tcb x)) (cd : Bool) : + PresRC (QStepA (some r) (if cd then x.tcbSchedContext else none)) (SIKA i M N false σ) (replyPush (Z := Unit) t d r cd) := by + unfold replyPush + intro s hs z hz + obtain ⟨⟨c, s1⟩, h1, hz⟩ := mem_bindA.1 hz + unfold getTcbObjRef at h1 + obtain ⟨y, hy, ey⟩ := SIKA.tcbScA hs hk + obtain ⟨ec, es⟩ := Prod.mk.inj (threadGet_valA Tcb.tcbSchedContext hy h1) + subst es + rw [ey] at ec + subst ec + dsimp only at hz + refine (?_ : PresR (QStepA (some r) (if cd then x.tcbSchedContext else none)) _) s1 z hz + refine PresR.bind (qk_stepA (inferInstance : QInst (getTcbObjRef (Z := Unit) Tcb.tcbSchedContext d)).pres) + fun cc => ?_ + refine PresR.bind (setReplyTcb_stepA r t) fun _ => ?_ + refine PresR.bind (qk_stepA (inferInstance : QInst (setThreadState (Z := Unit) t (.blockedOnReply r))).pres) + fun _ => ?_ + intro s2 w hw + unfold NonDet.when at hw + split_ifs at hw with hg + swap + · have : w = ((), s2) := hw + rw [this]; exact PreO.refl _ + cases hsc : x.tcbSchedContext with + | none => exact absurd hsc hg.1 + | some sc => + have hcd : cd = true := hg.2.2 + rw [hsc] at hw + rw [if_pos hcd] + exact (PresR.bind (bindScReply_stepA sc r) fun _ => + qk_stepA (inferInstance : QInst (schedContextDonate (Z := Unit) sc d)).pres) s2 w hw + +/-- **Every outcome of `send_ipc` to a waiting receiver `d` is `QStepA`**: only the receiver's reply may gain a +caller, only the sender's SC may gain replies. -/ +theorem sendIpc_recv_stepA {i : ObjRef} {M N : ObjRef → Prop} {th : TypedHeapA} {kb : U64} (hPl : PlaceA th kb) + (sf : S) {σ : State Unit} (hR : StateRelA th (L.mem sf) σ) (hV : SlotsValidA th (L.mem sf)) {t d : U64} + {e : ObjRef} {q : List ObjRef} + (htr : th d = some .tcb) (hk : σ.kheap e = some (.endpoint (.recvEP (specPtr d :: q)))) + {xt xd : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) (hkd : σ.kheap (specPtr d) = some (.tcb xd)) + (hal : ∀ rb, ipcBufOfA true xd = some rb → (bufAddr rb 0).toNat % 8 = 0) + (blk call : Bool) (badge : Badge) (g gr cd : Bool) (hcase : XferCaseA xt g) + (hMt : M (specPtr t)) (hMd : M (specPtr d)) + {ex : Option ObjRef} (hex : ∀ e' r dd, xd.tcbState = .blockedOnReceive e' (some r) dd → ex = some r) : + ∀ x ∈ (sendIpc (Z := Unit) blk call badge g gr cd (specPtr t) e σ).1, + QStepA ex (if cd then xt.tcbSchedContext else none) σ x.2 := by + suffices H : PresRC (QStepA ex (if cd then xt.tcbSchedContext else none)) (· = σ) + (sendIpc (Z := Unit) blk call badge g gr cd (specPtr t) e) from fun x hx => H σ rfl x hx + unfold sendIpc + refine PresRC.bind_oneA (getEndpoint_eq hk) ?_ + refine PresRC.monoPA (P' := fun s => TcbKeepA σ s ∧ SchedFrameA σ s ∧ SIKA i M N false σ s) + (fun s h => h ▸ ⟨fun _ _ h => h, PreO.refl s, SIKA.refl s⟩) ?_ + cases blk <;> dsimp only + all_goals + refine PresRC.bind_inv (fun s hs x hx => ?_) (fun _ => ?_) + · have h1 := tcbEpDequeue_sikA (i := i) (M := M) (N := N) (don := false) _ e hMd s x hx + have h2 : SchedFrameA s x.2 := SPresInst.pres s x hx + exact ⟨(tcbEpDequeue_qA _ e s x hx).toStep, PreO.trans hs.1 (TKInst.pres s x hx), PreO.trans hs.2.1 h2, + hs.2.2.trans h1⟩ + refine PresRC.bind_valA (P := fun s => TcbKeepA σ s ∧ SchedFrameA σ s ∧ SIKA i M N false σ s) + (Q := fun rs => rs = xd.tcbState) (fun s hs x hx => ?_) (fun recvState hrs => ?_) + · have hz : x = (xd.tcbState, s) := by + unfold getThreadState at hx + exact threadGet_valA _ (hs.1 _ _ hkd) hx + subst hz; exact ⟨PreO.refl s, hs, rfl⟩ + cases recvState + case blockedOnReceive a r dd => + dsimp only + refine PresRC.bind_pureA r ?_ + refine PresRC.monoPA (P' := fun s => TcbKeepA σ s ∧ SchedFrameA σ s ∧ SIKA i M N false σ s) (fun _ h => h) ?_ + intro s hs x hx + obtain ⟨y, hy, hx⟩ := hx + have hT : SIKA i M N false s y.2 := + doIpcTransfer_any_sikA hPl sf (hR.specSchedA hs.2.1.1) hV htr (hs.1 _ _ hkt) (hs.1 _ _ hkd) hal _ _ _ hcase + s rfl y hy + have hQ : QKeepA s y.2 := + doIpcTransfer_any_qA hPl sf (hR.specSchedA hs.2.1.1) hV htr (hs.1 _ _ hkt) (hs.1 _ _ hkd) hal _ _ _ hcase + s rfl y hy + refine PreO.trans hQ.toStep ?_ + refine (?_ : PresRC (QStepA ex (if cd then xt.tcbSchedContext else none)) (SIKA i M N false σ) _) y.2 (hs.2.2.trans hT) x hx + have step : ∀ {α β : Type} {m : SMonad Unit α} {f : α → SMonad Unit β}, PresR (SIKA i M N false) m → + PresR QKeepA m → (∀ a, PresRC (QStepA ex (if cd then xt.tcbSchedContext else none)) (SIKA i M N false σ) (f a)) → + PresRC (QStepA ex (if cd then xt.tcbSchedContext else none)) (SIKA i M N false σ) (NonDet.bind m f) := + fun h1 h2 hf => PresRC.bind_inv (fun s hs x hx => ⟨(h2 s x hx).toStep, hs.trans (h1 s x hx)⟩) hf + refine step ?_ ?_ (fun _ => ?_) + · cases r with + | none => exact PresR.pure _ + | some r => exact replyUnlinkTcb_sikA _ _ hMd + · cases r with + | none => exact PresR.pure _ + | some r => exact (inferInstance : QInst (replyUnlinkTcb (Z := Unit) (specPtr d) r)).pres + refine step (eqPres_sikA (threadGet_eqPres _ _)) (fun s x hx => ?_) (fun scOpt => ?_) + · rw [← threadGet_eqPres _ _ s x hx]; exact QKeepA.refl s + refine step (eqPres_sikA (threadGet_eqPres _ _)) (fun s x hx => ?_) (fun fault => ?_) + · rw [← threadGet_eqPres _ _ s x hx]; exact QKeepA.refl s + have tail : PresR (QStepA ex (if cd then xt.tcbSchedContext else none)) (do + setThreadState (specPtr d) .running + let destSc ← getTcbObjRef Tcb.tcbSchedContext (specPtr d) + ifSporadicCurScTestRefillUnblockCheck destSc + possibleSwitchTo (Z := Unit) (specPtr d)) := + qk_stepA (by unfold ifSporadicCurScTestRefillUnblockCheck; qpres) + have inact : PresRC (QStepA ex (if cd then xt.tcbSchedContext else none)) (SIKA i M N false σ) + (setThreadState (Z := Unit) (specPtr t) .inactive) := + PresRC.of_pres (qk_stepA (inferInstance : QInst (setThreadState (Z := Unit) (specPtr t) .inactive)).pres) + generalize hS : (if cd = true then xt.tcbSchedContext else none) = S0 at tail inact ⊢ + split_ifs with c1 c2 + · refine PresRC.bind_last ?_ (fun _ => tail) + cases r with + | none => exact absurd rfl c2.2 + | some r => + have e : ex = some r := hex _ _ _ hrs.symm + rw [e, ← hS] + exact replyPush_stepA hkt _ + · exact PresRC.bind_last inact (fun _ => tail) + · refine PresRC.bind_last ?_ (fun _ => tail) + refine PresRC.of_pres (qk_stepA ?_) + intro s x hx + unfold NonDet.when at hx + split_ifs at hx with hg + swap + · have : x = ((), s) := hx + rw [this]; exact QKeepA.refl _ + obtain ⟨⟨c, s1⟩, h1, hx⟩ := mem_bindA.1 hx + have es : s = s1 := by + unfold getTcbObjRef at h1 + exact threadGet_eqPres _ _ s _ h1 + subst es + exact (inferInstance : QInst (schedContextDonate (Z := Unit) _ _)).pres s x hx + all_goals (dsimp only; exact PresRC.bind_fail) + +/-- The sender's SC is active (the form `sendIpc_recv_qsA`'s `hact` takes). -/ +theorem scActive_of_actTcbA {σ : State Unit} {t : ObjRef} (h : ActTcbA σ t) {x : Tcb} + (hk : σ.kheap t = some (.tcb x)) : + ∀ sc, x.tcbSchedContext = some sc → ∀ y k, σ.kheap sc = some (.schedContext y k) → scActive y = true := by + intro sc hs y k hy + obtain ⟨x', sc', ex, es, y0, k0, e0, a⟩ := h + rw [hk] at ex + cases ex + rw [hs] at es + cases es + rw [hy] at e0 + cases e0 + exact a + +theorem SIKA.domk {i : ObjRef} {M N : ObjRef → Prop} {don : Bool} {σ σ' : State Unit} (K : SIKA i M N don σ σ') : + DomKeepA σ σ' := + ⟨congrArg (·.domainIndex) K.frame, congrArg (·.domainStartIndex) K.frame, congrArg (·.domainList) K.frame, + congrArg (·.archState.armUsGlobalVspace) K.frame⟩ + +/-- **Every outcome of `send_ipc` to a waiting receiver is `QSendA`** (module docstring). The receiver's reply object +has a caller; a donating send's SC is active. -/ +theorem sendIpc_recv_qsA {i : ObjRef} {M N : ObjRef → Prop} {th : TypedHeapA} {kb : U64} (hPl : PlaceA th kb) + (sf : S) {σ : State Unit} (hR : StateRelA th (L.mem sf) σ) (hV : SlotsValidA th (L.mem sf)) {t d : U64} + {e : ObjRef} {q : List ObjRef} + (htr : th d = some .tcb) (hk : σ.kheap e = some (.endpoint (.recvEP (specPtr d :: q)))) + {xt xd : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) (hkd : σ.kheap (specPtr d) = some (.tcb xd)) + (hal : ∀ rb, ipcBufOfA true xd = some rb → (bufAddr rb 0).toNat % 8 = 0) + (blk call : Bool) (badge : Badge) (g gr cd : Bool) (hcase : XferCaseA xt g) + (hMt : M (specPtr t)) (hMd : M (specPtr d)) (hNd : N (specPtr d)) + (hti : specPtr t ≠ i) (hdi : specPtr d ≠ i) (hD : DonA σ (specPtr t) xt) + (hcd : cd = true → xt.tcbSchedContext ≠ none) + (hact : cd = true → ∀ sc, xt.tcbSchedContext = some sc → ∀ y k, σ.kheap sc = some (.schedContext y k) → + scActive y = true) + (hrc : ∀ e' r dd, xd.tcbState = .blockedOnReceive e' (some r) dd → + ∃ x, σ.kheap r = some (.reply x) ∧ x.replyTcb ≠ none) : + ∀ x ∈ (sendIpc (Z := Unit) blk call badge g gr cd (specPtr t) e σ).1, QSendA σ x.2 := by + intro x hx + have K : SIKA i M N true σ x.2 := + sendIpc_recv_sikFA hPl sf hR hV htr hk hkt hkd hal blk call badge g gr cd hcase hMt hMd hNd hti hdi hD hcd + (don := true) (fun h => by cases h) x hx + have hex : ∃ ex : Option ObjRef, (∀ e' r dd, xd.tcbState = .blockedOnReceive e' (some r) dd → ex = some r) ∧ + ∀ r, ex = some r → ∃ y, σ.kheap r = some (.reply y) ∧ y.replyTcb ≠ none := by + by_cases h : ∃ e' r dd, xd.tcbState = .blockedOnReceive e' (some r) dd + · obtain ⟨e', r, dd, h⟩ := h + refine ⟨some r, fun e'' r' dd' h' => ?_, fun r' er => ?_⟩ + · rw [h] at h'; injection h' + · cases er; exact hrc _ _ _ h + · exact ⟨none, fun e' r dd h' => absurd ⟨e', r, dd, h'⟩ h, fun _ er => by cases er⟩ + obtain ⟨ex, h1, h2⟩ := hex + have Q : QStepA ex (if cd then xt.tcbSchedContext else none) σ x.2 := + sendIpc_recv_stepA (i := i) (M := M) (N := N) hPl sf hR hV htr hk hkt hkd hal blk call badge g gr cd hcase hMt hMd + h1 x hx + refine ⟨Q.prio, Q.ep, Q.ntfn, fun A p y' k hp hr => ?_, fun p y hp => ?_, K.domk⟩ + · obtain ⟨y, k0, hy, a⟩ := Q.sc p y' k hp + obtain ⟨y1, hy1, s⟩ := SIKA.scF K hy + have rm : y'.scRefillMax = y.scRefillMax := by + rw [hp] at hy1 + cases hy1 + exact s.rm + have act : scActive y = true := (a hr).elim (A p y k0 hy) fun h => by + cases hc : cd + · rw [hc] at h; cases h + · rw [hc] at h; exact hact hc p h.symm y k0 hy + unfold scActive at act ⊢ + rw [rm]; exact act + · obtain ⟨z, hz, a⟩ := Q.rt p y hp + refine ⟨z, hz, fun n => a (fun h => ?_) n⟩ + obtain ⟨w, hw, c⟩ := h2 p h.symm + rw [hz] at hw + cases hw + exact c n + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3SendRecvArmA.lean b/proofs/Fl4ProofsA64/P3/Stage3SendRecvArmA.lean new file mode 100644 index 000000000..6b858841d --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SendRecvArmA.lean @@ -0,0 +1,637 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2b-2b): **Endpoint send to a waiting receiver, at the record**: the plain send +(`call = false`, no donation) and the call without grant (`call = true`, no grant and no grant-reply) of a sender +without a fault and without extra caps. + +* `sendIpc_noGrant_cdA`, `sendIpc_noGrant_senderA`: a call without grant ignores `can_donate`, and leaves the sender + `Inactive`. +* **`JStage3A.slotOffEwA`**: the cap slots of the typed heap are off the endpoint code's write set (the transfer's + `hoff`). +* **`JSyscallA.sndRunA`**: the sender's scheduler facts after its Restart. +* **`JSyscallA.sendRecvA`**: `decode_invocation` on an Endpoint send cap whose endpoint holds waiting receivers: the + receiver gets the message (registers and IPC buffer) and runs. +* **`JSyscallA.sendRecvDispA`**: the same arm in the dispatch top's shape (`DispPostA`). + +[S] (scope selectors, `hbr`) `call = don = false`, or `call` with no grant and no grant-reply (any `don`); the sender +has no fault and no extra caps. +[P] `ArchTotal`, `MaxSpi`, `ArchPlat`, `A64FrameMasks`, `hCs`, `h1` (`numNodes = 1`: the receiver is on the current +core), `hmb`, `hmax`, `htu`. New module. +-/ +import Fl4ProofsA64.P3.Stage3SendQA +import Fl4ProofsA64.P3.Stage3SendDonIrrelA +import Fl4ProofsA64.P3.Stage3SendNarrowA +import Fl4ProofsA64.P3.Stage3SendRecvFactsA +import Fl4ProofsA64.P3.Stage3RecvBufA +import Fl4ProofsA64.P3.Stage3RqDomPrioA +import Fl4ProofsA64.P3.Stage3SchedOpsTA +import Fl4ProofsA64.P3.Stage3SignalFootA +import Fl4ProofsA64.P3.Stage3DispBaseA +import Fl4ProofsA64.P3.Stage3SignalIdleA +import Fl4ProofsA64.P3.Stage3IpcWakeA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {J5 J7 : JSlotA S} {s : S} {a : A} {σ : State Unit} {m : McState} + +/-- A call without grant does not read `can_donate`. -/ +theorem sendIpc_noGrant_cdA (blk : Bool) (badge : Badge) (cd : Bool) (t e : ObjRef) : + sendIpc (Z := Unit) blk true badge false false cd t e = sendIpc blk true badge false false false t e := by + unfold sendIpc + simp + +/-- **A call without grant leaves the sender `Inactive`** (every outcome). -/ +theorem sendIpc_noGrant_senderA {σ : State Unit} {t d e : ObjRef} {q : List ObjRef} + (hk : σ.kheap e = some (.endpoint (.recvEP (d :: q)))) (htd : t ≠ d) (blk : Bool) (badge : Badge) (cd : Bool) : + ∀ x ∈ (sendIpc (Z := Unit) blk true badge false false cd t e σ).1, StIs t .inactive x.2 := by + have skip : ∀ {α β : Type} {m : SMonad Unit α} {f : α → SMonad Unit β} {Q : β → State Unit → Prop}, + (∀ a, HT (fun _ => True) (f a) Q) → HT (fun _ => True) (m >>= f) Q := + fun h => HT.bind (Q := fun _ _ => True) (fun _ _ _ _ => trivial) h + have tail : HT (StIs t .inactive) (do + setThreadState (Z := Unit) d .running + let destSc ← getTcbObjRef Tcb.tcbSchedContext d + ifSporadicCurScTestRefillUnblockCheck destSc + possibleSwitchTo d) (fun _ σ => StIs t .inactive σ) := by + refine HT.bind (Q := fun _ σ => StIs t .inactive σ) ?_ fun _ => ?_ + · unfold Fl4SpecA64.setThreadState + refine HT.bind (Q := fun _ σ => StIs t .inactive σ) (fun σ h r hr y hy => ?_) fun _ σ h r hr => ?_ + · obtain ⟨x, -, e⟩ := threadSet_memA _ d hr + rw [e] at hy + exact h y (by simpa only [Function.update_of_ne htd] using hy) + · rw [setThreadStateAct_eq] at hr + have e : r = ((), stsAct σ d) := hr + rw [e] + exact fun y hy => h y (by rwa [stsAct_kheap] at hy) + refine HT.bind (Q := fun _ σ => StIs t .inactive σ) (fun σ h r hr => by + have e : r.2 = σ := ((threadGet_eqPres _ _) σ r hr).symm + rw [e]; exact h) fun sc => ?_ + refine HT.bind (Q := fun _ σ => StIs t .inactive σ) (fun σ h r hr => + (StIs.frame t .inactive).keep σ _ ((by unfold ifSporadicCurScTestRefillUnblockCheck ifCondRefillUnblockCheck; tpres : + TPres (ifSporadicCurScTestRefillUnblockCheck (Z := Unit) sc)) σ r hr) h) fun _ => ?_ + intro σ h r hr y hy + exact h y (((KPresInst.pres : PresR TcbCellsEq (possibleSwitchTo (Z := Unit) d)) σ r hr).2 _ _ |>.2 hy) + have block : HT (fun _ => True) (setThreadState (Z := Unit) t .inactive) (fun _ σ => StIs t .inactive σ) := by + unfold Fl4SpecA64.setThreadState + refine HT.bind (Q := fun _ σ => StIs t .inactive σ) (fun σ _ r hr y hy => ?_) fun _ σ h r hr => ?_ + · obtain ⟨x, -, e⟩ := threadSet_memA _ t hr + rw [e] at hy + have : ({ x with tcbState := .inactive } : Tcb) = y := by simpa using hy + rw [← this] + · rw [setThreadStateAct_eq] at hr + have e : r = ((), stsAct σ t) := hr + rw [e] + exact fun y hy => h y (by rwa [stsAct_kheap] at hy) + rw [sendIpc_noGrant_cdA] + suffices H : HT (fun s => s = σ) (sendIpc (Z := Unit) blk true badge false false false t e) + (fun _ s => StIs t .inactive s) from fun x hx => H σ rfl x hx + unfold sendIpc + intro s hs x hx + subst hs + obtain ⟨y, hy, hx'⟩ := mem_bindA.1 hx + clear hx + rw [getEndpoint_eq hk] at hy + have ey : y = (.recvEP (d :: q), s) := hy + subst ey + revert x hx' + cases blk <;> dsimp only + all_goals + intro x hx + refine (?_ : HT (fun _ => True) _ (fun _ s => StIs t .inactive s)) s trivial x hx + refine skip fun _ => skip fun rs => ?_ + cases rs + case blockedOnReceive a r dd => + dsimp only + refine skip fun _ => skip fun _ => skip fun _ => skip fun _ => skip fun fault => ?_ + simp only [Bool.false_eq_true, or_self, false_and, true_or, ↓reduceIte] + exact HT.bind block fun _ => tail + all_goals + dsimp only + intro s _ r hr + obtain ⟨z, hz, -⟩ := mem_bindA.1 hr + exact absurd hz (Set.notMem_empty _) + +/-- **The slots are off the endpoint code's write set** (module docstring). -/ +theorem JStage3A.slotOffEwA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) : + ∀ y, IsSlotA Z.th y → ¬ EWrite Z.T E Rp Bf y ∧ ¬ EWrite Z.T E Rp Bf (wa y 8#u64) := by + intro y hy + have hP := J.sr.place + obtain ⟨q, kq, off, hq, hsz, hyv, hk⟩ : ∃ q kq off, Z.th q = some kq ∧ off + 32 ≤ kq.size ∧ + y.val = q.val + off ∧ ((∃ r, kq = .cnode r) ∨ (kq = .tcb ∧ off + 32 ≤ 224)) := by + obtain ⟨p, ⟨r, hr, i, hi, rfl⟩ | ⟨ht, i, hi, rfl⟩⟩ := hy + · obtain ⟨k, hk, h1, h2⟩ := slotVA_of hP hr (j := i) (by rw [cnode_size]; omega) + rw [hr] at hk + cases hk + exact ⟨p, _, 32 * i, hr, by omega, h2, Or.inl ⟨r, rfl⟩⟩ + · obtain ⟨k, hk, h1, h2⟩ := tcb_slotV hP ht hi + rw [ht] at hk + cases hk + unfold tcbSlotsA at hi + exact ⟨p, _, 32 * i, ht, by omega, h2, Or.inr ⟨rfl, by omega⟩⟩ + have hfit := hP.fit q kq hq + have key : ∀ b : U64, y.val ≤ b.val → b.val < y.val + 16 → ¬ EWrite Z.T E Rp Bf b := by + intro b b1 b2 c + have oth : ∀ o z, Z.th o = some (.other z) → o.val ≤ b.val → b.val < o.val + z → False := by + intro o z ho o1 o2 + by_cases e : q = o + · subst e + rw [hq] at ho + cases ho + rcases hk with ⟨r, h⟩ | ⟨h, -⟩ <;> cases h + · have := hP.apart q o kq (.other z) hq ho e + have hz : ObjKindA.size (.other z) = z := rfl + omega + have tcbk : ∀ u ∈ Z.T, u.val + 256 ≤ b.val → b.val < u.val + 4096 → False := by + intro u hu u1 u2 + have hu' := (J.sr.thT u).2 hu + have hz : ObjKindA.size .tcb = 4096 := rfl + by_cases e : q = u + · subst e + rw [hq] at hu' + cases hu' + rcases hk with ⟨r, h⟩ | ⟨-, h224⟩ + · cases h + · omega + · have := hP.apart q u kq .tcb hq hu' e + omega + rcases c with ⟨e, he, c⟩ | ⟨r, hr, c⟩ | ⟨u, hu, c⟩ | c + · exact oth e 16 (J.ipcPlace.endpoint e he) (by omega) (by omega) + · exact oth r 32 (J.ipcPlace.reply r hr) (by omega) (by omega) + · have hfu := (J.sr.core.tcbs u hu).fits + rcases c with ⟨c1, c2⟩ | c + · unfold TWOffN at c2 + exact tcbk u hu (by omega) (by omega) + · unfold TL at c + exact tcbk u hu (by omega) (by omega) + · obtain ⟨p, len, hp, h1, h2⟩ := J.ipcPlace.buffer b c + exact oth p len hp h1 h2 + have h8 : (wa y 8#u64).val = y.val + 8 := by + rw [wa_valm] + have : (8#u64 : U64).val = 8 := rfl + rw [this] + exact Nat.mod_eq_of_lt (by omega) + exact ⟨key y (le_refl _) (by omega), key _ (by omega) (by omega)⟩ + +/-- **The sender's scheduler facts after its Restart** (the blocking branch's `SndRunA`). -/ +theorem JSyscallA.sndRunA (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + {σ1 : State Unit} {x : Tcb} (hr : runnable x.tcbState = true) + (hq : ∀ d p, specPtr (L.curW s) ∉ σ1.readyQueues d p) (hrel : σ1.releaseQueue = σ.releaseQueue) + (hact : σ1.schedulerAction = σ.schedulerAction) (hni : specPtr (L.curW s) ≠ σ1.idleThread) : + SndRunA σ1 (specPtr (L.curW s)) x := by + refine ⟨hr, ⟨hq, fun h => ?_⟩, fun u hu => absurd (hact ▸ hu) (J.noSwitch u), hni⟩ + have htZ : L.curW s ∈ Z.T := J.sr.curT + rw [hrel, J.sr.rest.rel] at h + obtain ⟨u, hu, e⟩ := List.mem_map.1 h + rw [specPtr_inj e] at hu + have on : L.mem s (affA (L.curW s)) = L.cpu s := by + have x := hCs _ (J.sr.core.aff _ htZ) + have y := hCs _ J.j.1.sch.cpuCs + exact UScalar.eq_of_val_eq (by omega) + exact (J.sr.core.released _ htZ).2 (on ▸ hu) J.cur.1 + +set_option maxHeartbeats 2000000 in +/-- **Send to a waiting receiver, at a syscall entry** (module docstring). -/ +theorem JSyscallA.sendRecvGA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p badge slot lbl len buf : U64} {recv grant grantReply blk call don fp : Bool} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) + (hcap : capA st s slot = .Endpoint p badge true recv grant grantReply) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hp : p ∈ E) {d : U64} {q : List ObjRef} + (hk : σ.kheap (specPtr p) = some (.endpoint (.recvEP (specPtr d :: q)))) + (hsnd : ∀ x, σ.kheap σ.curThread = some (.tcb x) → x.tcbFault = none ∧ (grant = false ∨ + ipcBufOfA false x = none ∨ + unat (dataToMessageInfo (x.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0)) + (hbrG : (call = false ∧ don = false) ∨ (call = true ∧ grant = false ∧ grantReply = false) ∨ + (call = false ∧ don = true ∧ ∀ y, σ.kheap (specPtr d) = some (.tcb y) → y.tcbSchedContext ≠ none)) : + ∃ xc σ1 σ2 s2, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ((), σ2) ∈ (sendIpc (Z := Unit) blk call badge.bv grant grantReply don σ.curThread (specPtr p) σ1).1 ∧ + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don fp buf extra = + ok (.Ok (), s2, a) ∧ + Sched.WakeEndA σ σ2 ∧ + SIKA σ.idleThread (fun o => o = σ.curThread ∨ o = specPtr d) (fun o => o = specPtr d) false σ1 σ2 ∧ + (call = false → ∀ x, σ2.kheap σ.curThread = some (.tcb x) → x.tcbState = .restart) ∧ + ∃ Q Rl, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s2 a σ2 m := by + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hslot hcov J.actW J.running J.cur + have hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM := J.toJStage3EntryA.curNotVmA + obtain ⟨xc, hkc, hst, hsp, J1, -, hOS1, -⟩ := J.toJStage3A.of_restartCurA + RS.pl.choose_spec.choose_spec.choose_spec.choose_spec RS.sym RS.run hnvm RS.cur RS.vs RS.vc + have hws : WritesIn st s (L.wr s (stA (L.curW s)) sched.TS_RESTART) (fun b => b = stA (L.curW s)) := by + refine writesIn_wr ?_ + rw [stA_eq] + exact field_aligned (J.sr.core.tcbs _ J.sr.curT) (by simp) (by simp) + have hst1 : L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART) (stA (L.curW s)) = sched.TS_RESTART := + rd_wr_eq (by rw [stA_eq]; exact field_aligned (J.sr.core.tcbs _ J.sr.curT) (by simp) (by simp)) + generalize hs1 : L.wr s (stA (L.curW s)) sched.TS_RESTART = s1 at hst J1 hOS1 hws hst1 + set t := L.curW s with ht_def + set σ1 := stUpdA σ σ.curThread xc .restart with hσ1 + have kb1 : L.ksb s1 = L.ksb s := hws.1 + have cpu1 : L.cpu s1 = L.cpu s := hws.2.1 + have ct : σ.curThread = specPtr t := J.j.1.sch.curThread + have htZ : t ∈ Z.T := J.sr.curT + set xt : Tcb := { xc with tcbState := .restart } with hxt + have hkt : σ1.kheap (specPtr t) = some (.tcb xt) := by + rw [hσ1, ← ct]; simp [stUpdA, setObjA, hxt] + have ne1 : specPtr p ≠ σ.curThread := fun e => by rw [e, hkc] at hk; cases hk + have hk1 : σ1.kheap (specPtr p) = some (.endpoint (.recvEP (specPtr d :: q))) := by + rw [hσ1]; simp only [stUpdA, setObjA, Function.update_of_ne ne1]; exact hk + -- the receiver + obtain ⟨hep2, xs, -, hq, hQ, hB⟩ := J1.ep.at_ep hp hk1 + have hdx : d ∈ xs := by + have : specPtr d ∈ xs.map specPtr := by rw [← hq]; exact List.mem_cons_self + obtain ⟨y, hy, e⟩ := List.mem_map.1 this + rwa [specPtr_inj e] at hy + have hd : d ∈ Z.T := hQ.mem d hdx + have hdst : (L.mem s1 (stA d)).val = 3 := (hB d hdx).1 + have hdb : L.mem s1 (Notif.tBlockA d) = p := (hB d hdx).2 + obtain ⟨xd, hkd, rd⟩ := J1.ep.tcb d hd + obtain ⟨e', rep, dd, hstd⟩ := tsCode_three (rd.st.symm.trans hdst) + have htd : t ≠ d := fun h => by + have : (L.mem s1 (stA t)).val = 3 := h ▸ hdst + rw [hst1] at this + unfold sched.TS_RESTART at this + simp at this + have hdt : specPtr d ≠ specPtr t := fun h => htd (specPtr_inj h).symm + have hkdσ : σ.kheap (specPtr d) = some (.tcb xd) := by + rw [← hkd, hσ1, ct]; simp only [stUpdA, setObjA, Function.update_of_ne hdt] + -- the sender and the receiver in the spec + obtain ⟨hft, hcaps⟩ : xt.tcbFault = none ∧ (grant = false ∨ ipcBufOfA false xt = none ∨ + unat (dataToMessageInfo (xt.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0) := hsnd xc hkc + have hc : m.curCore < fl4A64Platform.numNodes := by rw [J1.j.1.sch.core]; exact hCs _ J1.j.1.sch.cpuCs + have hO : SchedOpsEA st Z.T E Rp Bf (IpcSchedRelA st Z m κ) := + (ipcSchedOpsA (ar := ar) J1.iplaced J1.ipcPlace hc hmb hmax htu).2.2.1 + have hF : SchedFootEA st Z.T E Rp Bf (IpcSchedRelA st Z m κ) (fun σ s => MWrA (st := st) Z m σ s) := + schedFootEA_of_sched + have hSR : IpcSchedRelA st Z m κ s1 σ1 := ⟨Z.Q, Z.R, J1.sr⟩ + have hdth : Z.th d = some .tcb := (J1.sr.thT d).2 hd + have hon : L.mem s1 (affA d) = L.cpu s1 := by + have x := hCs _ (J1.sr.core.aff d hd) + have y := hCs _ J1.j.1.sch.cpuCs + exact UScalar.eq_of_val_eq (by omega) + have hnb : tsCode xt.tcbState ≠ 3 := by simp [hxt, tsCode] + have hu := J1.bufFrameA hd hkd true + have hrep := J1.repOKA hd + have hoff := J1.slotOffEwA + have hidle : specPtr t ≠ σ1.idleThread ∧ specPtr d ≠ σ1.idleThread := by + obtain ⟨x, hx, hs⟩ := J1.sinv.idleTcb + refine ⟨fun h => ?_, fun h => ?_⟩ + · rw [← h, hkt] at hx; cases hx; cases hs + · rw [← h, hkd] at hx; cases hx; rw [hstd] at hs; cases hs + obtain ⟨don0, hbr0, hdon⟩ : ∃ don0 : Bool, + ((call = false ∧ don0 = false) ∨ (call = true ∧ grant = false ∧ grantReply = false)) ∧ + (don0 = don ∨ (call = false ∧ don = true ∧ don0 = false ∧ xd.tcbSchedContext ≠ none)) := by + rcases hbrG with h | h | ⟨hc, hd, hs⟩ + · exact ⟨don, Or.inl h, Or.inl rfl⟩ + · exact ⟨don, Or.inr h, Or.inl rfl⟩ + · exact ⟨false, Or.inl ⟨hc, rfl⟩, Or.inr ⟨hc, hd, rfl, hs xd hkdσ⟩⟩ + have hbr : (call = false ∧ xt.tcbFault = none ∧ don0 = false) ∨ + ((call = true ∨ xt.tcbFault ≠ none) ∧ + ((grant = false ∧ grantReply = false) ∨ ∀ e' r dd, xd.tcbState ≠ .blockedOnReceive e' (some r) dd) ∧ + SndRunA σ1 (specPtr t) xt) := by + rcases hbr0 with ⟨hc0, hd0⟩ | ⟨hc1, hg, hgr⟩ + · exact Or.inl ⟨hc0, hft, hd0⟩ + · refine Or.inr ⟨Or.inl hc1, Or.inl ⟨hg, hgr⟩, J.sndRunA hCs h1 (by rw [hxt]; rfl) (fun dm pr hm => ?_) rfl rfl + hidle.1⟩ + refine J1.sinv.ctQ (by show σ.schedulerAction = .resumeCurThread; rw [J.sr.sch.act]; unfold actOfA; rw [if_pos J.actW]) dm pr ?_ + rw [show σ1.curThread = specPtr t from ct] + exact hm + have body0 := send_ipc_recvA (st := st) (ar := ar) hO hF J1.iplaced.e J1.sr.place s1 a σ1 J1.ep hSR J1.j.1.st + J1.j.1.cv J1.bufCover J1.bufNative hoff htZ ((J1.sr.thT t).2 htZ) hkt hnb hp hk1 hdth hkd hu hrep hon badge grant + grantReply blk call don0 (Or.inl ⟨hft, hcaps⟩) hbr + have fpw0 := send_ipc_recv_fpA (st := st) (ar := ar) hO hF J1.iplaced.e J1.sr.place s1 a σ1 J1.ep hSR J1.j.1.st + J1.j.1.cv J1.bufCover J1.bufNative hoff htZ ((J1.sr.thT t).2 htZ) hkt hnb hp hk1 hdth hkd hu hrep hon badge grant + grantReply blk call don0 (Or.inl ⟨hft, hcaps⟩) hbr + obtain ⟨body, fpw, hflip⟩ : + (endpoint.send_ipc st ar s1 a blk call badge grant grantReply don t p ⦃ r => r.2 = a ∧ + ¬ (sendIpc (Z := Unit) blk call badge.bv grant grantReply don (specPtr t) (specPtr p) σ1).2 ∧ + ∃ σ', ((), σ') ∈ (sendIpc (Z := Unit) blk call badge.bv grant grantReply don (specPtr t) (specPtr p) σ1).1 ∧ + EpStateRel Z.T E Rp Bf (L.mem r.1) σ' ∧ IpcSchedRelA st Z m κ r.1 σ' ∧ + FpA st Z.T E Rp Bf (fun σ s => MWrA (st := st) Z m σ s) σ1 s1 r.1 ⦄) ∧ + (endpoint.send_ipc st ar s1 a blk call badge grant grantReply don t p ⦃ r => WritesIn st s1 r.1 + (SendFpA st Z.T Rp Bf (fun σ s => MWrA (st := st) Z m σ s) σ1 s1 t p d (call = true ∨ xt.tcbFault ≠ none)) ⦄) ∧ + sendIpc (Z := Unit) blk call badge.bv grant grantReply don (specPtr t) (specPtr p) σ1 = + sendIpc (Z := Unit) blk call badge.bv grant grantReply don0 (specPtr t) (specPtr p) σ1 := by + rcases hdon with e | ⟨hc, hdt', hd0, hdsc⟩ + · subst e + exact ⟨body0, fpw0, rfl⟩ + · have hscw : ∀ s' σ', IpcSchedRelA st Z m κ s' σ' → ∀ y : Tcb, σ'.kheap (specPtr d) = some (.tcb y) → + y.tcbSchedContext ≠ none → L.mem s' (scA d) ≠ 0#u64 := by + rintro s' σ' ⟨Q', R', sr'⟩ y hy hne h0 + obtain ⟨z, hz, hzs⟩ := sr'.sch.tcb _ hd + rw [hy] at hz + obtain rfl := KernelObject.tcb.inj (Option.some.inj hz) + rw [hzs, optScA, if_pos h0] at hne + exact hne rfl + have bD := send_ipc_recvDonA (st := st) (ar := ar) hO hF J1.iplaced.e J1.sr.place s1 a σ1 J1.ep hSR J1.j.1.st + J1.j.1.cv J1.bufCover J1.bufNative hoff htZ ((J1.sr.thT t).2 htZ) hkt hnb hp hk1 hdth hkd hu hrep hon badge + grant grantReply blk (Or.inl ⟨hft, hcaps⟩) hft hdsc (J1.bufAlign0A hd hkd true) hscw + have fD := send_ipc_recv_fpDonA (st := st) (ar := ar) hO hF J1.iplaced.e J1.sr.place s1 a σ1 J1.ep hSR J1.j.1.st + J1.j.1.cv J1.bufCover J1.bufNative hoff htZ ((J1.sr.thT t).2 htZ) hkt hnb hp hk1 hdth hkd hu hrep hon badge + grant grantReply blk (Or.inl ⟨hft, hcaps⟩) hft hdsc (J1.bufAlign0A hd hkd true) hscw + obtain ⟨-, eb, -, -, heq, -⟩ := exists_of_specA bD + subst hc hdt' hd0 + refine ⟨WP.spec_mono bD fun r h => ⟨h.1, h.2.1, h.2.2.2⟩, + WP.spec_mono fD fun r h => h.mono fun b hb => ?_, heq⟩ + rcases hb with hb | hb | hb | hb | hb + · exact Or.inl hb + · exact Or.inr (Or.inl hb) + · exact hb.1.elim + · exact Or.inr (Or.inr (Or.inr (Or.inl hb))) + · exact Or.inr (Or.inr (Or.inr (Or.inr hb))) + -- P1's invariants after the send + obtain ⟨Pt, Ub, Pp7, Tb7, Hr, Te, Kt, Lv7, Re, bits, KLv, W⟩ := J1.j7.unp + have I := J1.delIn hA hPl W + have htH : t ∈ H.tcbs := by rw [J.tcbs]; exact htZ + have kt := spec_at (sched.set_thread_state.restart_blk (Es := ∅) s t (BXI.blk J.sys.ent.bxi) + htH (runnable_ok J.running).1 (runnable_ok J.running).2) hst + have D := J.decPreA hslot hext + have kt1 := kt.2.2.2 + have hc1 := D.cb.restart (BXI.blk J.sys.ent.bxi).1.choose_spec.choose_spec.tcbs kt1 kt.2.2.1 + have hH : H p = some .ep := J.utIpc.ep p hp + have P := (J.toJStage3A.sysPlace hext hslot hcov).keepO hOS1 kb1 + have hnames : ∀ k, H p = some k → ∃ x ∈ OS, NamesA (capA st s1 x) p k := fun k hk' => by + rw [hH] at hk' + cases hk' + exact ⟨slot, hslot, by rw [hOS1.capA hslot, hcap]; exact names_ep⟩ + have hsc1 : L.mem s1 (scA t) ≠ 0#u64 := by rw [kt1.scf htH]; exact J.cur.2 + have p1 := Fl4ProofsA64.P1.W7.R.ST.endpoint.send_ipc.recvS (st := st) (ar := ar) (Vc' := Z.V) (Vc := Z.V) + (Ub := Ub) (Pt := Pt) hA a blk call badge grant grantReply don t p P (BXI.blk J1.sys.ent.bxi) I.del.sca I.ts + J1.sys.sysl J1.symL hH (by rw [epState_eq]; exact hep2) hnames hc1 + (by rw [hst1]; unfold sched.TS_RESTART sched.TS_INACTIVE; decide) (fun _ => hsc1) + (D.rv.restart kt1 (D.rv.zero htH D.nvm)) + obtain ⟨⟨s2, a2⟩, eb, ⟨⟨ha2, -, σ2, hmem, E2, ⟨Q, Rl, sr2⟩, w⟩, wn⟩, p1post⟩ := + exists_of_specA (WP.spec_and (WP.spec_and body fpw) p1) + simp only at ha2 eb E2 sr2 w wn p1post + subst ha2 + have blk2 := p1post.2.2.1 + have hdec : syscall.decode_invocation st ar s a2 lbl len slot (capA st s slot) blk call don fp buf extra = + ok (.Ok (), s2, a2) := by + rw [hcap, decodeEndpoint_programA] + simp only [↓reduceIte, ipcInvokeProgramA, cur_thread_ok, bind_ok, ← ht_def, hst] + rw [show endpoint.send_ipc st ar s1 a2 blk call badge grant grantReply don t p = ok (s2, a2) from eb] + simp only [bind_ok] + rfl + have F := J.decodeFinA hA hM hPl hfm hslot hcov (rfl : capA st s slot = capA st s slot) hext hSC (lbl := lbl) + (by rw [hcap]; simp [DecArmS1aEnt, DecArmQH, DecArmQZ2, DecArmKQ2, DecArmKQ, capFam]) + (fun _ h => by rw [hcap] at h; cases h) (fun _ h => by rw [hcap] at h; cases h) hdec σ2 m + obtain ⟨-, -, sysl, symL, -, userOK, j7⟩ := F + -- the spec outcome's record facts + set M : ObjRef → Prop := fun o => o = specPtr t ∨ o = specPtr d with hMdef + set Nq : ObjRef → Prop := fun o => o = specPtr d with hNdef + have hmem0 : ((), σ2) ∈ (sendIpc (Z := Unit) blk call badge.bv grant grantReply don0 (specPtr t) (specPtr p) σ1).1 := + hflip ▸ hmem + have hDon : DonA σ1 (specPtr t) xt := by + intro sc hsc + obtain ⟨y, k, hy, hb⟩ := J1.sr.inv.back (specPtr t) xt sc hkt hsc + refine ⟨fun h => ?_, fun y' k' hy' => by rw [hy] at hy'; cases hy'; exact hb⟩ + obtain ⟨x, yi, ki, -, -, hyi, hti⟩ := J1.sinv.idleSc + rw [h, hyi] at hy + cases hy + exact hidle.1 (Option.some.inj (hb.symm.trans hti)) + have hal := J1.bufAlign0A hd hkd true + have K : SIKA σ1.idleThread M Nq false σ1 σ2 := by + have key : ∀ cd : Bool, cd = false → + ((), σ2) ∈ (sendIpc (Z := Unit) blk call badge.bv grant grantReply cd (specPtr t) (specPtr p) σ1).1 → + SIKA σ1.idleThread M Nq false σ1 σ2 := fun cd hcd h => + sendIpc_recv_sikA (M := M) (N := Nq) J1.sr.place s1 J1.j.1.st J1.j.1.cv hdth hk1 hkt hkd hft hal blk call + badge.bv grant grantReply cd hcaps (Or.inl rfl) (Or.inr rfl) rfl hidle.1 hidle.2 hDon + (fun c => by rw [hcd] at c; cases c) + (fun _ => ⟨hcd, hbr0.imp And.left (fun h => Or.inl h.2) |>.elim Or.inl Or.inr⟩) _ h + rcases hbr0 with ⟨-, hd0⟩ | ⟨hc1, hg, hgr⟩ + · exact key don0 hd0 hmem0 + · refine key false rfl ?_ + rw [hc1, hg, hgr] at hmem0 ⊢ + rwa [sendIpc_noGrant_cdA] at hmem0 + have hact : ActTcbA σ1 (specPtr t) := by + have h := (J1.schedValid.act J.act (fun c => hidle.1 (ct ▸ c))).1 + obtain ⟨x, sc', ex, es, a'⟩ := h + exact ⟨x, sc', ct ▸ ex, es, a'⟩ + have Qs : QSendA σ1 σ2 := by + have key : ∀ cd : Bool, (cd = true → xt.tcbSchedContext ≠ none) → + ((), σ2) ∈ (sendIpc (Z := Unit) blk call badge.bv grant grantReply cd (specPtr t) (specPtr p) σ1).1 → + QSendA σ1 σ2 := fun cd hcd h => + sendIpc_recv_qsA (i := σ1.idleThread) (M := M) (N := Nq) J1.sr.place s1 J1.j.1.st J1.j.1.cv hdth hk1 hkt hkd + hal blk call badge.bv grant grantReply cd (Or.inl ⟨hft, hcaps⟩) (Or.inl rfl) (Or.inr rfl) rfl hidle.1 + hidle.2 hDon hcd (fun _ => scActive_of_actTcbA hact hkt) + (fun e' r dd h => by + obtain ⟨xr, k, b⟩ := J1.repOKA hd xd hkd e' r dd h + exact ⟨xr, k, by rw [b]; simp⟩) _ h + rcases hbr0 with ⟨-, hd0⟩ | ⟨hc1, hg, hgr⟩ + · exact key don0 (fun c => by rw [hd0] at c; cases c) hmem0 + · refine key false (fun c => by cases c) ?_ + rw [hc1, hg, hgr] at hmem0 ⊢ + rwa [sendIpc_noGrant_cdA] at hmem0 + have U2 : UFrA (BufBytesA (ipcBufOfA true xd)) σ1 σ2 := + sendIpc_recv_ufrA J1.sr.place s1 J1.j.1.st J1.j.1.cv hdth hk1 hkt hkd hft hal blk call badge.bv grant grantReply + don0 hcaps _ hmem0 + have sv : SchedValidA σ2 := + sendIpc_recv_schedValidA J1.sr.place s1 J1.j.1.st J1.j.1.cv hdth hk1 hkt hkd hft hal blk call badge.bv grant + grantReply don0 hcaps hstd + (hbr0.elim Or.inl fun h => Or.inr (Or.inl ⟨h.1, Or.inl h.2⟩)) J1.schedValid J1.rqDomPrioA _ hmem0 + have run : StIs (specPtr d) .running σ2 := sendIpc_recv_runningA hk1 blk call badge.bv grant grantReply don0 _ hmem0 + -- the senders and receivers before and after + have hMs : ∀ u ∈ Z.T, M (specPtr u) → ∀ x, σ1.kheap (specPtr u) = some (.tcb x) → + (x = xt ∨ x = xd) := by + intro u _ hu x hx + rcases hu with h | h + · rw [h, hkt] at hx; cases hx; exact Or.inl rfl + · rw [h, hkd] at hx; cases hx; exact Or.inr rfl + have hst2 : call = false → L.mem s2 (stA t) = L.mem s1 (stA t) := fun hc0 => by + have hta := J1.sr.core.tcbs t htZ + refine wn.mem_eq (a_st hta).1 ?_ + have hv := Endp.val_st hta + rintro (c | ⟨r, hr, c⟩ | ⟨c, -⟩ | c | c) + · have apart : ∀ u ∈ Z.T, u ≠ t → u.val + 4096 ≤ t.val ∨ t.val + 4096 ≤ u.val := fun u hu ne => by + have := J1.sr.place.apart u t .tcb .tcb ((J1.sr.thT u).2 hu) ((J1.sr.thT t).2 htZ) ne + have hsz : ObjKindA.size .tcb = 4096 := rfl + omega + rcases c with c | ⟨u, hu, -, c⟩ | ⟨r, c⟩ | c + · have := J1.iplaced.e.et p hp t htZ; unfold Endp.EW at c; omega + · have := (J1.iplaced.e.tcb u hu).fits + unfold TL at c + by_cases e : u = t + · subst e; omega + · have := apart u hu e; omega + · rw [Endp.regA_val (J1.iplaced.e.tcb d hd) (regIdxA_lt r)] at c + have := regIdxA_lt r + have := apart d hd htd.symm + omega + · have := J1.iplaced.e.bt _ c t htZ; omega + · have := J1.iplaced.e.rt r hr t htZ + rw [c, Endp.rTcbA_val] at hv + omega + · rcases c with c | c + · rw [hc0] at c; cases c + · exact c hft + · have h' := congrArg (fun w : U64 => w.val) c + simp only [hv, Endp.val_st (J1.iplaced.e.tcb d hd)] at h' + exact htd (UScalar.eq_of_val_eq (by omega)) + · rw [stA_eq] at c + exact J1.tcbWord_offMwrA htZ (by simp) (by simp) c + have after : ∀ u ∈ Z.T, M (specPtr u) → ∀ x, σ2.kheap (specPtr u) = some (.tcb x) → + epBlocked x.tcbState = none ∧ ∀ p, x.tcbState ≠ .blockedOnNotification p := by + intro u _ hu x hx + rcases hu with h | h + · rw [specPtr_inj h] at hx + rcases hbr0 with ⟨hc0, -⟩ | ⟨hc1, hg, hgr⟩ + swap + · rw [hc1, hg, hgr] at hmem0 + rw [sendIpc_noGrant_senderA hk1 (fun e => hdt e.symm) blk badge.bv don0 _ hmem0 x hx] + exact ⟨rfl, fun _ h => by cases h⟩ + obtain ⟨y, hy, r⟩ := E2.tcb t htZ + rw [hx] at hy + cases hy + have h2 : tsCode x.tcbState = 2 := by rw [← r.st, hst2 hc0, hst1]; unfold sched.TS_RESTART; rfl + cases hs : x.tcbState with + | blockedOnNotification p => rw [hs] at h2; simp [tsCode] at h2 + | ext e => exact ⟨rfl, fun _ h => by cases h⟩ + | blockedOnReceive _ _ _ => rw [hs] at h2; simp [tsCode] at h2 + | blockedOnSend _ _ => rw [hs] at h2; simp [tsCode] at h2 + | blockedOnReply _ => rw [hs] at h2; simp [tsCode] at h2 + | _ => exact ⟨rfl, fun _ h => by cases h⟩ + · rw [h] at hx + rw [run x hx] + exact ⟨rfl, fun _ h => by cases h⟩ + have wk : NtfnWaitKeepA Z.T N (L.mem s1) (L.mem s2) := + J1.ntfnWait_sendA htZ hp hd hdb (wn.mono fun _ h => Or.inl h) + have wI : WritesIn st s1 s2 (fun b => IpcFpA st Z E Rp Bf m σ1 s1 b ∨ RepWA Rp b) := WritesIn.mono w fun _ => Or.inl + have wN : WritesIn st s1 s2 (fun b => SchedStepNWA (st := st) N E Rp Z m σ1 s1 b ∨ BufW Bf b) := + w.mono fun _ h => h.elim (ewrite_stepNA_buf J1.sr.core.tcbs) fun h => Or.inl (Or.inl (Or.inl h)) + have notM : ∀ c < n, ¬ M (m.idleThreadOf c) := by + intro c hc' h + obtain ⟨y, hy, hs⟩ := J1.sidle.f3 c hc' + rcases h with h | h + · rw [h, hkt] at hy; cases hy; cases hs + · rw [h, hkd] at hy; cases hy; rw [hstd] at hs; cases hs + have sinv : SInvXA σ2 m n := by + refine K.sinv J1.sinv notM (fun h => h.elim (fun h => hidle.1 h.symm) fun h => hidle.2 h.symm) ?_ ?_ ?_ ?_ ?_ ?_ ?_ + · intro y hy dm pr hm + rcases hy with rfl | rfl + · refine J1.sinv.ctQ (by show σ.schedulerAction = .resumeCurThread; rw [J.sr.sch.act]; unfold actOfA; rw [if_pos J.actW]) dm pr ?_ + rw [show σ1.curThread = specPtr t from ct] + exact hm + · obtain ⟨x, hx, hr⟩ := J1.sinv.rq dm pr _ hm + rw [hkd] at hx; cases hx; rw [hstd] at hr; cases hr + · intro y hy + cases hy + refine ⟨fun h => hdt (h.trans (show σ1.curThread = specPtr t from ct)), hidle.2, fun c hc' h => + notM c hc' (Or.inr h), fun z hz => by rw [run z hz]; rfl⟩ + · intro y hy + cases hy + exact ⟨xd, hkd⟩ + · exact J.noSwitch + · intro y hy hr + rcases hy with rfl | rfl + · have hnr : σ1.curThread ∉ σ1.releaseQueue := J.toJStage3EntryA.curInv.notRel + exact hnr (by rw [show σ1.curThread = specPtr t from ct]; exact hr) + · obtain ⟨x, hx, hrn⟩ := J1.sinv.rlsRun _ hr + rw [hkd] at hx; cases hx; rw [hstd] at hrn; cases hrn + · exact J1.sidle.rdyA J1.sinv + · refine K.mfA J1.sinv fun p hp x hx => ?_ + rcases hp with rfl | rfl + · rw [hkt] at hx; cases hx; rintro (⟨_, _, h⟩ | ⟨_, h⟩ | h) <;> simp only [hxt] at h <;> cases h + · rw [hkd] at hx; cases hx; rw [hstd]; rintro (⟨_, _, h⟩ | ⟨_, h⟩ | h) <;> cases h + have hMv : ∀ u ∈ Z.T, M (specPtr u) → L.mem s1 (stA u) ≠ sched.TS_RUNNING_VM := by + intro u _ hu h + rcases hu with e | e <;> rw [specPtr_inj e] at h + · rw [hst1] at h; revert h; unfold sched.TS_RESTART sched.TS_RUNNING_VM; decide + · rw [h] at hdst; revert hdst; unfold sched.TS_RUNNING_VM; decide + have ntfn := J1.ntfn_ipcA wI K E2 wk (fun u hu hm x hx p h => by + rcases hMs u hu hm x hx with rfl | rfl + · cases h + · rw [hstd] at h; cases h) (fun u hu hm x hx => (after u hu hm x hx).2) + have hwake : Sched.WakeEndA σ σ2 := by + have E3 := Sched.sendIpc_recv_endA J1.sr.place s1 J1.j.1.st J1.j.1.cv hdth hk1 hkt hkd hal blk call badge.bv + grant grantReply don0 (Or.inl ⟨hft, hcaps⟩) hstd + (hbr0.elim (fun h => Or.inl ⟨h.1, h.2, hft⟩) fun h => Or.inr (Or.inl ⟨Or.inl h.1, Or.inl h.2⟩)) + J1.schedValid J1.rqDomPrioA J.noSwitch hDon hidle.1 hidle.2 _ hmem0 + have B : Sched.BLA σ1 σ2 := Sched.sendIpc_recv_blA J1.sr.place s1 J1.j.1.st J1.j.1.cv hdth hk1 hkt hkd hft hal + blk call badge.bv grant grantReply don0 hcaps _ hmem0 + have B0 : Sched.BLA σ σ1 := Sched.BLA.tcbW hkc + refine ⟨PreO.trans B0 B, E3.1, fun hres x hx => ?_⟩ + cases call with + | true => exact absurd hres (E3.2 ⟨show σ1.curThread = specPtr t from ct, Or.inl rfl⟩) + | false => + rw [ct] at hx + obtain ⟨y, hy, r⟩ := E2.tcb t htZ + rw [hx] at hy + cases hy + have h2 : tsCode x.tcbState = 2 := by rw [← r.st, hst2 rfl, hst1]; unfold sched.TS_RESTART; rfl + right + revert h2 + cases x.tcbState <;> intro h2 <;> first | rfl | (simp [tsCode] at h2) | (rename_i e; cases e; simp [tsCode] at h2) + have hK : SIKA σ.idleThread (fun o => o = σ.curThread ∨ o = specPtr d) (fun o => o = specPtr d) false σ1 σ2 := by + rw [ct]; exact K + have hres : call = false → ∀ x, σ2.kheap σ.curThread = some (.tcb x) → x.tcbState = .restart := by + intro hc0 x hx + rw [ct] at hx + obtain ⟨y, hy, r⟩ := E2.tcb t htZ + rw [hx] at hy + cases hy + have h2 : tsCode x.tcbState = 2 := by rw [← r.st, hst2 hc0, hst1]; unfold sched.TS_RESTART; rfl + revert h2 + cases x.tcbState <;> intro h2 <;> first | rfl | (simp [tsCode] at h2) | (rename_i e; cases e; simp [tsCode] at h2) + refine ⟨xc, σ1, σ2, s2, hkc, rfl, hsp, by rw [ct]; exact hmem, hdec, hwake, hK, hres, Q, Rl, ?_⟩ + exact J1.of_ipcStepUA (Q := Q) (Rl := Rl) (curOf := Z.curOf) (curScOf := Z.curScOf) wN U2.1 U2.2.1 Qs + (J1.specZero_of_recvBufA hd hkd U2.2.2) (fun _ _ => rfl) (BlkOK.bxi blk2) sysl symL + userOK j7 sr2 E2 ntfn (J1.reply_ipcA (WritesIn.mono w fun _ h => h) K) J1.deadExt.reply K.ipcKeep (J1.tw_ipcA wI) sinv (K.sidle J1.sidle notM (J1.sidle.rdyA J1.sinv)) + (J1.runVcpu_ipcA wI K E2 hMv) (J1.blkQ_ipcA K E2 wk after) sv + +/-- **`sendRecvGA` without the donating plain send.** -/ +theorem JSyscallA.sendRecvA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p badge slot lbl len buf : U64} {recv grant grantReply blk call don fp : Bool} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) + (hcap : capA st s slot = .Endpoint p badge true recv grant grantReply) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hp : p ∈ E) {d : U64} {q : List ObjRef} + (hk : σ.kheap (specPtr p) = some (.endpoint (.recvEP (specPtr d :: q)))) + (hsnd : ∀ x, σ.kheap σ.curThread = some (.tcb x) → x.tcbFault = none ∧ (grant = false ∨ + ipcBufOfA false x = none ∨ + unat (dataToMessageInfo (x.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0)) + (hbr0 : (call = false ∧ don = false) ∨ (call = true ∧ grant = false ∧ grantReply = false)) : + ∃ xc σ1 σ2 s2, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ((), σ2) ∈ (sendIpc (Z := Unit) blk call badge.bv grant grantReply don σ.curThread (specPtr p) σ1).1 ∧ + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don fp buf extra = + ok (.Ok (), s2, a) ∧ + Sched.WakeEndA σ σ2 ∧ + ∃ Q Rl, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s2 a σ2 m := by + obtain ⟨xc, σ1, σ2, s2, h1', h2', h3, h4, h5, h6, -, -, h7⟩ := + J.sendRecvGA (lbl := lbl) (len := len) (buf := buf) (fp := fp) hA hM hPl hfm hCs h1 hmb hmax htu hslot hcov hcap + hext hSC hp hk hsnd (hbr0.imp id Or.inl) + exact ⟨xc, σ1, σ2, s2, h1', h2', h3, h4, h5, h6, h7⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3SendRecvFactsA.lean b/proofs/Fl4ProofsA64/P3/Stage3SendRecvFactsA.lean new file mode 100644 index 000000000..90728e612 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SendRecvFactsA.lean @@ -0,0 +1,116 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2b): **record facts the receiver-present IPC arms share.** + +* **`JStage3A.repOKA`**: a receive-blocked thread's reply object names it back (`RepOKA`), from P1's + `ReplyInv0.recv` and the endpoint relation. +* **`JStage3A.replyIsolatedA`**: a reply whose thread is not blocked on reply is on no reply stack and has null + stack links (`ReplyIsolatedA`), from P1's `ValidReplies` (`rnext` / `rprev` name blocked replies, `screp` the + stack head) and the reply-stack relation. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3A +import Fl4ProofsA64.P3.EpCancelIpcA +import Fl4ProofsA64.P3.IpcScPopReadA +import Fl4ProofsA64.P3.UtClosA +import Fl4ProofsA64.P3.Stage3SysPlaceA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- **A receive-blocked thread's reply object names it** (module docstring). -/ +theorem JStage3A.repOKA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) {d : U64} (hd : d ∈ Z.T) : + RepOKA σ (specPtr d) := by + intro x hx ep r dd hs + obtain ⟨y, hy, rel⟩ := J.ep.tcb d hd + rw [hx] at hy + cases hy + have e1 := (rel.recv ep (some r) dd hs).1 + obtain ⟨hw0, hwt⟩ := optRef_some e1 + have hst : L.mem s (stA d) = sched.TS_BLOCKED_ON_RECV := by + apply UScalar.eq_of_val_eq + rw [rel.st, hs] + unfold sched.TS_BLOCKED_ON_RECV + rfl + have R : ReplyInv st H.tcbs H.replies H.scs s := J.sys.ent.bxi.2.2.1.2.1 + have hdT : d ∈ H.tcbs := by rw [J.tcbs]; exact hd + have eqa : tReplyA d = replyA d := rfl + rw [eqa] at hw0 hwt + obtain ⟨hrR, hrt⟩ := R.recv d hdT hst hw0 + have hRp : L.mem s (replyA d) ∈ Rp := J.ipcHeapCov.reply _ hrR + obtain ⟨xr, hk, h⟩ := J.ep.reply _ hRp + have hd0 : d ≠ 0#u64 := ne0_of_heap J.sys.sysl.ly.h0 (show H d = some .tcb from hdT) + refine ⟨xr, by rw [← hwt]; exact hk, ?_⟩ + have h' : xr.replyTcb = optRef d := h.trans (congrArg optRef hrt) + rw [h', optRef_ne hd0] + +/-- **A reply whose thread is not blocked on reply is isolated** (module docstring). -/ +theorem JStage3A.replyIsolatedA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) {r : U64} (hr : r ∈ Rp) + (hnb : L.mem s (stA (L.mem s (Endp.rTcbA r))) ≠ sched.TS_BLOCKED_ON_REPLY) : + ReplyIsolatedA (L.mem s) Z.C stack r := by + have VR : ValidReplies st H s := J.sys.sysl.vr + have R : ReplyInv st H.tcbs H.replies H.scs s := J.sys.ent.bxi.2.2.1.2.1 + have nR : ¬ RBlk st s r := fun h => hnb h.blk + have inH : ∀ q ∈ Rp, q ∈ H.replies := fun q hq => J.utIpc.reply q hq + have ev : ∀ q ∈ Rp, q &&& ~~~1#u64 = q ∧ headBit q = false := fun q hq => + ut_even_maskA (by have := (J.iplaced.e.rep q hq).1; omega) + have r0 : r ≠ 0#u64 := fun e => (J.iplaced.e.rep r hr).2.1 (by rw [e]; rfl) + have notin : ∀ c ∈ Z.C, r ∉ stack c := by + intro c hc hm + obtain ⟨-, hmem, -, hw⟩ := J.reply.stackRel c hc + obtain ⟨i, hi, e⟩ := List.getElem_of_mem hm + cases i with + | zero => + have w := (hw 0 hi).2 + rw [e] at w + simp only at w + have cal : c.val % 8 = 0 := J.toJEntA.scAl c hc + have ce : c.val % 2 = 0 := by omega + have hb : headBit (L.mem s (rNextA r)) = true := by + rw [w, ← mask1_of_even ce]; exact Fl4ProofsA64.P1.nx_head1 c + have hp : L.mem s (rNextA r) &&& ~~~1#u64 = c := by rw [w]; exact replyHeadMaskA ce + have c0 : c ≠ 0#u64 := J.scNZ c hc + have hh := R.head r (inH r hr) hb (by rw [hp]; exact c0) + rw [hp] at hh + have cS : c ∈ H.scs := by rw [J.scs]; exact hc + have := VR.screp c cS (by rw [hh]; exact r0) + rw [hh] at this + exact nR this + | succ j => + have hj : j < (stack c).length := by omega + have w := (hw j hj).1 + rw [List.getD_eq_getElem _ _ hi, e] at w + have qR : (stack c)[j] ∈ Rp := hmem _ (List.getElem_mem hj) + have := VR.rprev _ (inH _ qR) (by rw [w, (ev r hr).1]; exact r0) + rw [w, (ev r hr).1] at this + exact nR this + obtain ⟨-, hn, hp⟩ := J.reply.detached r hr notin + refine ⟨notin, ?_, ?_⟩ + · by_contra h + obtain ⟨qR, -, hq⟩ := hn h + have := VR.rprev _ (inH _ qR) (by rw [hq, (ev r hr).1]; exact r0) + rw [hq, (ev r hr).1] at this + exact nR this + · by_contra h + obtain ⟨qR, -, hq⟩ := hp h + have := VR.rnext _ (inH _ qR) (by rw [hq, (ev r hr).1]; exact r0) (by rw [hq]; exact (ev r hr).2) + rw [hq, (ev r hr).1] at this + exact nR this + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3SendSpecFrA.lean b/proofs/Fl4ProofsA64/P3/Stage3SendSpecFrA.lean new file mode 100644 index 000000000..2f48b1698 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SendSpecFrA.lean @@ -0,0 +1,285 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2b-2): **the spec frame of `send_ipc` to a waiting receiver** (the message transfer +writes the receiver's IPC buffer, so `SchedFrameA`'s "user memory kept" does not hold). + +* `BufBytesA rb`: the bytes of the 128 words of the buffer `rb`. **`UFrA B`**: `SpecSchedA`, SC sizes kept, user memory + kept outside `B`; a preorder, implied by `SchedFrameA` (`PresR.ufrA`). +* `storeWordOffs_ufrA`, `copyMrs_ufrA` (any state; the message length at most 127). `copyMrs_genA`, + `doIpcTransfer_genA`: the same along any frame `R` under `UFrA`. +* `StateRelA.specSchedA`: the CSpace relation across a `SpecSchedA` step (the CNodes and the TCBs' cap slots kept). +* **`doIpcTransfer_ufrA`** (a sender without fault, no extra caps: `hcaps` as `do_ipc_transfer_relA`'s) and + **`sendIpc_recv_ufrA`**: every outcome of the spec's `send_ipc` to a waiting receiver `d` is `UFrA` of the receiver's + buffer bytes. The receive-slot lookup (`getReceiveSlots_oneA`) keeps the state at every state `UFrA`-reachable from a + `StateRelA` one; the rest of `send_ipc` is `SPresA` (`P3/SpecFrameIpcA`). + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.SpecFrameIpcA +import Fl4ProofsA64.P3.EpSendRecvA +import Fl4ProofsA64.P3.FrontEndExtraA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P3 Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + +noncomputable section + +/-- The bytes of the 128 words of the buffer `rb` (none without a buffer). -/ +def BufBytesA (rb : Option ObjRef) (a : Word) : Prop := + ∃ p, rb = some p ∧ ∃ k, k < 128 ∧ ∃ y : Word, y.toNat < 8 ∧ a = bufAddr p k + y + +/-- **The scheduler frame with user memory changed inside `B` only.** -/ +def UFrA (B : Word → Prop) (σ σ' : State Unit) : Prop := + SpecSchedA σ σ' ∧ SSzA σ σ' ∧ + ∀ a, ¬ B a → σ'.machineState.underlyingMemory a = σ.machineState.underlyingMemory a + +instance ufrPreOA (B : Word → Prop) : PreO (UFrA B) where + refl σ := ⟨SpecSchedA.refl σ, SSzA.refl σ, fun _ _ => rfl⟩ + trans h1 h2 := ⟨h1.1.trans h2.1, SSzA.trans h1.2.1 h2.2.1, fun a ha => (h2.2.2 a ha).trans (h1.2.2 a ha)⟩ + +theorem UFrA.of_fr {B : Word → Prop} {σ σ' : State Unit} (h : SchedFrameA σ σ') : UFrA B σ σ' := + ⟨h.1, h.2.1, fun _ _ => by rw [h.2.2]⟩ + +theorem PresR.ufrA {B : Word → Prop} {α : Type} {m : SMonad Unit α} (h : SPresA m) : PresR (UFrA B) m := + fun s x hx => UFrA.of_fr (h s x hx) + +theorem PresR.mapM_memA {σ β γ : Type} {R : σ → σ → Prop} [PreO R] {f : γ → NonDet σ β} : + ∀ (l : List γ), (∀ a ∈ l, PresR R (f a)) → PresR R (NonDet.mapM f l) + | [], _ => PresR.pure _ + | a :: l, h => by + show PresR R (NonDet.bind (f a) fun b => NonDet.bind (NonDet.mapM f l) fun bs => NonDet.pure (b :: bs)) + exact PresR.bind (h a (by simp)) fun _ => + PresR.bind (PresR.mapM_memA l fun x hx => h x (by simp [hx])) fun _ => PresR.pure _ + +theorem PresRC.monoPA {σ α : Type} {R : σ → σ → Prop} {P P' : σ → Prop} {m : NonDet σ α} + (hP : ∀ s, P s → P' s) (h : PresRC R P' m) : PresRC R P m := fun s hs x hx => h s (hP s hs) x hx + +/-- Binding after a step with one outcome keeping the (fixed) state. -/ +theorem PresRC.bind_oneA {σ α β : Type} {R : σ → σ → Prop} {m : NonDet σ α} {f : α → NonDet σ β} + {s0 : σ} {a : α} (hm : m s0 = ({(a, s0)}, False)) (hf : PresRC R (· = s0) (f a)) : + PresRC R (· = s0) (NonDet.bind m f) := by + rintro s rfl x ⟨y, hy, hx⟩ + rw [hm] at hy + have e : y = (a, s) := hy + subst e + exact hf s rfl x hx + +theorem PresRC.bind_pureA {σ α β : Type} {R : σ → σ → Prop} {P : σ → Prop} {f : α → NonDet σ β} (a : α) + (hf : PresRC R P (f a)) : PresRC R P (NonDet.bind (NonDet.pure a) f) := by + rintro s hs x ⟨y, hy, hx⟩ + have e : y = (a, s) := hy + subst e + exact hf s hs x hx + +/-! ## User-memory stores -/ + +theorem storeWord_memA {p w : Word} {ms : MachineState} {x : Unit × MachineState} (hx : x ∈ (storeWord p w ms).1) : + x.2.underlyingMemory = storeMem ms.underlyingMemory p w := by + unfold storeWord at hx + obtain ⟨y1, hy1, hx⟩ := hx + have e1 : y1 = ((), ms) := hy1.2 + subst e1 + have e2 : x = ((), { ms with underlyingMemory := storeMem ms.underlyingMemory p w }) := hx + rw [e2] + +theorem loadWord_keepA (a : Word) : ∀ ms, ∀ x ∈ (loadWord a ms).1, x.2.underlyingMemory = ms.underlyingMemory := by + intro ms x hx + obtain ⟨y1, hy1, hx⟩ := hx + have e1 : y1 = (ms.underlyingMemory, ms) := hy1 + subst e1 + obtain ⟨y2, hy2, hx⟩ := hx + have e2 : y2 = ((), ms) := hy2.2 + subst e2 + rw [mem_pureA hx] + +/-- **A store into word `x < 128` of the buffer `p`.** -/ +theorem storeWordOffs_ufrA (p : ObjRef) {x : Nat} (hx : x < 128) (v : Word) : + PresR (UFrA (BufBytesA (some p))) (storeWordOffs (Z := Unit) p x v) := by + intro σ r hr + unfold storeWordOffs at hr + obtain ⟨y1, hy1, hr⟩ := hr + have e1 : y1 = (σ, σ) := hy1 + subst e1 + obtain ⟨y2, hy2, hr⟩ := hr + have e2 : y2 = ((), σ) := hy2.2 + subst e2 + obtain ⟨y3, hy3, hr⟩ := hr + have e3 : y3 = (σ.machineState, σ) := hy3 + subst e3 + obtain ⟨⟨⟨a, ms'⟩, s2⟩, hy4, hr⟩ := hr + have hm : (a, ms') ∈ (storeWord (p + BitVec.ofNat 64 (x * wordSize)) v σ.machineState).1 := hy4.1 + have e4 : s2 = σ := hy4.2 + subst e4 + obtain ⟨y5, hy5, hr⟩ := hr + have e5 : y5 = ((), { s2 with machineState := ms' }) := hy5 + subst e5 + have e6 : r = (a, { s2 with machineState := ms' }) := hr + rw [e6] + have hmem := storeWord_memA hm + refine ⟨⟨fun _ => Or.inl rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩, SSzA.of_kheap rfl, fun b hb => ?_⟩ + show ms'.underlyingMemory b = _ + rw [hmem] + exact storeMem_other _ _ _ _ fun y hy e => hb ⟨p, rfl, x, hx, y, hy, e⟩ + +/-- **`copy_mrs`** along any frame of the user registers, the word loads and the receive buffer's word stores. -/ +theorem copyMrs_genA {R : State Unit → State Unit → Prop} [PreO R] + (hU : ∀ {α : Type} (t : ObjRef) (f : UserMonad α), PresR R (asUser (Z := Unit) t f)) + (hL : ∀ a, PresR R (doMachineOp (Z := Unit) (loadWord a))) + (snd : ObjRef) (sbuf : Option ObjRef) (rcv : ObjRef) (rbuf : Option ObjRef) + (hS : ∀ rb, rbuf = some rb → ∀ x < 128, ∀ v, PresR R (storeWordOffs (Z := Unit) rb x v)) + (n : LengthType) (hn : n.toNat ≤ 127) : PresR R (copyMrs (Z := Unit) snd sbuf rcv rbuf n) := by + unfold copyMrs + refine PresR.bind_pure _ _ ?_ + refine PresR.bind (PresR.mapM_memA _ fun x _ => PresR.bind (hU _ _) fun _ => hU _ _) (fun _ => ?_) + rcases sbuf with _ | sb <;> rcases rbuf with _ | rb <;> dsimp only + · exact PresR.bind (PresR.pure _) fun _ => PresR.pure _ + · exact PresR.bind (PresR.pure _) fun _ => PresR.pure _ + · exact PresR.bind (PresR.pure _) fun _ => PresR.pure _ + · refine PresR.bind (PresR.mapM_memA _ fun x hx => ?_) fun _ => PresR.pure _ + have hx' : x < 128 := by + simp only [unat, List.mem_range'_1] at hx + omega + refine PresR.bind ?_ fun v => hS rb rfl x hx' v + unfold loadWordOffs + exact hL _ + +/-- **`copy_mrs`** writes only the receive buffer's words. -/ +theorem copyMrs_ufrA (snd : ObjRef) (sbuf : Option ObjRef) (rcv : ObjRef) (rbuf : Option ObjRef) (n : LengthType) + (hn : n.toNat ≤ 127) : PresR (UFrA (BufBytesA rbuf)) (copyMrs (Z := Unit) snd sbuf rcv rbuf n) := + copyMrs_genA (fun t f => PresR.ufrA (asUser_presA t f)) + (fun a => PresR.ufrA (doMachineOp_presA _ (loadWord_keepA a))) snd sbuf rcv rbuf + (fun rb e x hx v => e ▸ storeWordOffs_ufrA rb hx v) n hn + +theorem miLength_leA (w : Data) : (dataToMessageInfo w).miLength.toNat ≤ 120 := by + unfold dataToMessageInfo + dsimp only + split + · decide + · rename_i h + simp only [gt_iff_lt, BitVec.not_lt, BitVec.le_def] at h + simpa using h + +/-- **`StateRelA` across a `SpecSchedA` step** (the CNodes and the TCBs' cap slots are kept). -/ +theorem _root_.Fl4ProofsA64.P3.StateRelA.specSchedA {th : TypedHeapA} {mem : U64 → U64} {σ σ' : State Unit} + (J : StateRelA th mem σ) (HS : SpecSchedA σ σ') : StateRelA th mem σ' := by + constructor + · intro p r hp + obtain ⟨cs, h1, h2, h3⟩ := J.cnode p r hp + exact ⟨cs, HS.keep h1 (fun _ h => by cases h) + (fun h => by rcases h with ⟨_, _, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ <;> cases e), h2, h3⟩ + · intro t ht + obtain ⟨x, h1, h2⟩ := J.tcb t ht + obtain ⟨x', e', hx⟩ := HS.tcb h1 + exact ⟨x', e', fun i hi => by rw [hx.1]; exact h2 i hi⟩ + +section +open fl4_core_a64 Fl4ProofsA64.P1 +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +set_option maxHeartbeats 1000000 in +/-- **`do_ipc_transfer` without fault and extra caps** along any frame `R` under `UFrA` of the receiver's buffer. -/ +theorem doIpcTransfer_genA {R : State Unit → State Unit → Prop} [PreO R] {B : Word → Prop} + (hUF : ∀ a b, R a b → UFrA B a b) + (hU : ∀ {α : Type} (t : ObjRef) (f : UserMonad α), PresR R (asUser (Z := Unit) t f)) + (hL : ∀ a, PresR R (doMachineOp (Z := Unit) (loadWord a))) {rbo : Option ObjRef} + (hS : ∀ rb, rbo = some rb → ∀ x < 128, ∀ v, PresR R (storeWordOffs (Z := Unit) rb x v)) + {th : TypedHeapA} {kb : U64} (hPl : PlaceA th kb) (sf : S) {σ : State Unit} + (hR : StateRelA th (L.mem sf) σ) (hV : SlotsValidA th (L.mem sf)) {snd rcv : U64} + (htr : th rcv = some .tcb) {xs xr : Tcb} (hks : σ.kheap (specPtr snd) = some (.tcb xs)) + (hkr : σ.kheap (specPtr rcv) = some (.tcb xr)) (hrbo : ipcBufOfA true xr = rbo) (hf : xs.tcbFault = none) + (hal : ∀ rb, ipcBufOfA true xr = some rb → (bufAddr rb 0).toNat % 8 = 0) + (ep : Option ObjRef) (badge : Badge) (grant : Bool) + (hcaps : grant = false ∨ ipcBufOfA false xs = none ∨ + unat (dataToMessageInfo (xs.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0) : + PresRC R (· = σ) + (doIpcTransfer (Z := Unit) (specPtr snd) ep badge grant (specPtr rcv)) := by + have reach : ∀ s, R σ s → StateRelA th (L.mem sf) s := fun s h => hR.specSchedA (hUF _ _ h).1 + unfold doIpcTransfer + refine PresRC.bind_oneA (lookupIpcBuffer_eqA hkr true) ?_ + refine PresRC.bind_oneA (threadGet_eq _ hks) ?_ + simp only [hf] + refine PresRC.bind_oneA (lookupIpcBuffer_eqA hks false) ?_ + unfold doNormalTransfer + refine PresRC.bind_oneA (getMessageInfo_eqA hks) ?_ + cases grant <;> simp only [Bool.false_eq_true, ↓reduceIte] <;> + first + | refine PresRC.bind_pureA (R := R) [] ?_ + | refine PresRC.bind_oneA (catch_lookupExtraCaps_nilA + (by rcases hcaps with h | h | h <;> first | exact Or.inl h | exact Or.inr h | cases h)) ?_ + all_goals + refine PresRC.monoPA (P' := fun s => R σ s) + (fun s h => h ▸ PreO.refl s) ?_ + refine PresRC.bind_inv (fun s hs x hx => ?_) (fun _ => ?_) + · have h := copyMrs_genA (R := R) hU hL (specPtr snd) (ipcBufOfA false xs) (specPtr rcv) (ipcBufOfA true xr) + (fun rb e => hS rb (hrbo ▸ e)) _ + (by have := miLength_leA (xs.tcbArch.tcbContext.userRegs msg_info_register); omega) s x hx + exact ⟨h, PreO.trans hs h⟩ + refine PresRC.bind_inv (fun s hs x hx => ?_) + (fun _ => PresRC.of_pres (by unfold setMessageInfo; exact PresR.bind (hU _ _) fun _ => hU _ _)) + have hsl : ∀ rb, ipcBufOfA true xr = some rb → + ∃ sl, getReceiveSlots (Z := Unit) (specPtr rcv) (some rb) s = ({(sl, s)}, False) := + fun rb hrb => getReceiveSlots_oneA hPl sf s (reach s hs) hV htr (hal rb hrb) + rw [transferCaps_nilA _ ep _ _ hsl] at hx + have e : x.2 = s := by rw [Set.mem_singleton_iff.mp hx] + exact ⟨by rw [e]; exact PreO.refl s, by rw [e]; exact hs⟩ + +/-- **`do_ipc_transfer` without fault and extra caps** writes only the receiver's buffer (module docstring). -/ +theorem doIpcTransfer_ufrA {th : TypedHeapA} {kb : U64} (hPl : PlaceA th kb) (sf : S) {σ : State Unit} + (hR : StateRelA th (L.mem sf) σ) (hV : SlotsValidA th (L.mem sf)) {snd rcv : U64} + (htr : th rcv = some .tcb) {xs xr : Tcb} (hks : σ.kheap (specPtr snd) = some (.tcb xs)) + (hkr : σ.kheap (specPtr rcv) = some (.tcb xr)) (hf : xs.tcbFault = none) + (hal : ∀ rb, ipcBufOfA true xr = some rb → (bufAddr rb 0).toNat % 8 = 0) + (ep : Option ObjRef) (badge : Badge) (grant : Bool) + (hcaps : grant = false ∨ ipcBufOfA false xs = none ∨ + unat (dataToMessageInfo (xs.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0) : + PresRC (UFrA (BufBytesA (ipcBufOfA true xr))) (· = σ) + (doIpcTransfer (Z := Unit) (specPtr snd) ep badge grant (specPtr rcv)) := + doIpcTransfer_genA (R := UFrA (BufBytesA (ipcBufOfA true xr))) (fun _ _ h => h) + (fun t f => PresR.ufrA (asUser_presA t f)) (fun a => PresR.ufrA (doMachineOp_presA _ (loadWord_keepA a))) + (rbo := ipcBufOfA true xr) (fun rb e x hx v => e ▸ storeWordOffs_ufrA rb hx v) + hPl sf hR hV htr hks hkr rfl hf hal ep badge grant hcaps + +/-- One step keeping the scheduler frame and every TCB, along the reachability predicate. -/ +theorem stepKA {B : Word → Prop} {σ0 : State Unit} {α : Type} (m : SMonad Unit α) [hs : SPresInst m] + [hk : TKInst m] : + ∀ s, (TcbKeepA σ0 s ∧ UFrA B σ0 s) → ∀ x ∈ (m s).1, UFrA B s x.2 ∧ (TcbKeepA σ0 x.2 ∧ UFrA B σ0 x.2) := + fun s h x hx => + have h1 : UFrA B s x.2 := UFrA.of_fr (hs.pres s x hx) + ⟨h1, PreO.trans h.1 (hk.pres s x hx), PreO.trans h.2 h1⟩ + +set_option maxHeartbeats 1000000 in +/-- **`send_ipc` to a waiting receiver** (module docstring). -/ +theorem sendIpc_recv_ufrA {th : TypedHeapA} {kb : U64} (hPl : PlaceA th kb) (sf : S) {σ : State Unit} + (hR : StateRelA th (L.mem sf) σ) (hV : SlotsValidA th (L.mem sf)) {t d : U64} {e : ObjRef} {q : List ObjRef} + (htr : th d = some .tcb) (hk : σ.kheap e = some (.endpoint (.recvEP (specPtr d :: q)))) + {xt xd : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) (hkd : σ.kheap (specPtr d) = some (.tcb xd)) + (hf : xt.tcbFault = none) (hal : ∀ rb, ipcBufOfA true xd = some rb → (bufAddr rb 0).toNat % 8 = 0) + (blk call : Bool) (badge : Badge) (g gr cd : Bool) + (hcaps : g = false ∨ ipcBufOfA false xt = none ∨ + unat (dataToMessageInfo (xt.tcbArch.tcbContext.userRegs msg_info_register)).miExtraCaps = 0) : + ∀ x ∈ (sendIpc (Z := Unit) blk call badge g gr cd (specPtr t) e σ).1, + UFrA (BufBytesA (ipcBufOfA true xd)) σ x.2 := by + suffices H : PresRC (UFrA (BufBytesA (ipcBufOfA true xd))) (· = σ) + (sendIpc (Z := Unit) blk call badge g gr cd (specPtr t) e) from fun x hx => H σ rfl x hx + unfold sendIpc + refine PresRC.bind_oneA (getEndpoint_eq hk) ?_ + refine PresRC.monoPA (P' := fun s => TcbKeepA σ s ∧ UFrA (BufBytesA (ipcBufOfA true xd)) σ s) + (fun s h => h ▸ ⟨fun _ _ h => h, PreO.refl s⟩) ?_ + cases blk <;> dsimp only + all_goals + refine PresRC.bind_inv (stepKA _) fun _ => ?_ + refine PresRC.bind_inv (stepKA _) fun recvState => ?_ + cases recvState + case blockedOnReceive a r dd => + dsimp only + refine PresRC.bind_pureA r ?_ + refine PresRC.bind_last (fun s hs x hx => ?_) (fun _ => PresR.ufrA (by spres)) + exact doIpcTransfer_ufrA hPl sf (hR.specSchedA hs.2.1) hV htr (hs.1 _ _ hkt) (hs.1 _ _ hkd) hf hal _ _ _ hcaps + s rfl x hx + all_goals (dsimp only; exact PresRC.bind_fail) + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3SetPrioFrA.lean b/proofs/Fl4ProofsA64/P3/Stage3SetPrioFrA.lean new file mode 100644 index 000000000..96ebc13f6 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SetPrioFrA.lean @@ -0,0 +1,84 @@ +/- +P3 (aarch64, A64-P3 labels merge): **the current thread's deletion clauses across `set_priority`** of another thread: +`CKeepA` (so `DelStepA.off` / `ctSc`) for `setPriority t` when `t` is not the current thread. New module. +-/ +import Fl4ProofsA64.P3.Stage3CancelBodyA +import Fl4ProofsA64.P3.Stage3DelBodyA +import Fl4ProofsA64.P3.Stage3SigSchedValidA +import Fl4ProofsA64.P3.Stage3ScYieldRqA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4ProofsA64.P3 Fl4Spec Fl4SpecA64 Classical + +theorem tcbSchedDequeue_neA (t : ObjRef) : PresR (CNeA t) (tcbSchedAction (Z := Unit) tcbSchedDequeue t) := by + intro σ r hr + obtain ⟨⟨e, m⟩, ha⟩ := tcbSchedAction_shapeA tcbSchedDequeue (fun _ _ _ h => Or.inr (List.mem_filter.1 h).1) t hr + rw [ha] at e + have b : CBaseA σ r.2 := by + rw [e]; exact ⟨⟨rfl, rfl, rfl, rfl, rfl, rfl⟩, fun _ _ h => h⟩ + refine ⟨b, fun n => ⟨b, fun h => ⟨fun d p hm => ?_, fun u hu => h.2 u (ha ▸ hu)⟩, + CtScKA.ofEq (by rw [e])⟩⟩ + rcases m d p _ hm with c | c + · exact h.1 d p c + · exact n c.symm + +theorem tcbAppend_ckA (t : ObjRef) (qs : List ObjRef) : CPresA (tcbAppend (Z := Unit) t qs) := by + intro σ r hr + unfold tcbAppend at hr + rw [(orderedInsert_spec t qs _ _ hr).1] + exact PreO.refl (R := CKeepA) σ + +theorem reorderEp_ckA (e t : ObjRef) : CPresA (reorderEp (Z := Unit) e t) := by + unfold reorderEp + refine PresR.bind (getEndpoint_ckA _) (fun _ => ?_) + repeat' (first + | exact tcbAppend_ckA _ _ + | exact setEndpoint_ckA _ _ + | exact PresR.pure _ + | exact PresR.assert _ + | refine PresR.bind ?_ (fun _ => ?_) + | dsimp only) + +theorem reorderNtfn_ckA (n t : ObjRef) : CPresA (reorderNtfn (Z := Unit) n t) := by + unfold reorderNtfn + refine PresR.bind (getNotification_ckA _) (fun _ => ?_) + repeat' (first + | exact tcbAppend_ckA _ _ + | exact setNotification_ckA _ _ + | exact PresR.pure _ + | exact PresR.assert _ + | refine PresR.bind ?_ (fun _ => ?_) + | dsimp only) + +/-- **`set_priority`** keeps the current thread's clauses when its target is another thread. -/ +theorem setPriority_neA (t : ObjRef) (p : Priority) : PresR (CNeA t) (setPriority (Z := Unit) t p) := by + have hp : PresR (CNeA t) (threadSetPriority (Z := Unit) t p) := by + unfold threadSetPriority + exact cne_of (by apply threadSet_ckA; intro _; rfl) + unfold setPriority + refine PresR.bind (cne_of (getThreadState_ckA t)) (fun ts => ?_) + refine PresR.ite' ?_ ?_ + · refine PresR.bind (cne_of (threadGet_ckA _ _)) (fun d => ?_) + refine PresR.bind (cne_of (threadGet_ckA _ _)) (fun q => ?_) + refine PresR.bind (cne_of (by unfold getTcbQueue; ckres)) (fun queue => ?_) + refine PresR.bind (cne_of (PresR.gets _)) (fun cur => ?_) + refine PresR.ite' ?_ hp + refine PresR.bind (tcbSchedDequeue_neA t) (fun _ => ?_) + refine PresR.bind hp (fun _ => ?_) + exact PresR.bind (tcbSchedEnqueue_neA t) (fun _ => cne_of rescheduleRequired_ckA) + · refine PresR.bind hp (fun _ => ?_) + refine PresR.bind ?_ (fun _ => ?_) + · cases epBlocked ts + · exact PresR.pure _ + · exact cne_of (reorderEp_ckA _ _) + · cases ntfnBlocked ts + · exact PresR.pure _ + · exact cne_of (reorderNtfn_ckA _ _) + +/-- `setPriority_neA` at a computed `set_priority` of a non-current thread. -/ +theorem setPriority_ckA {σ σ' : State Unit} {t : ObjRef} {p : Priority} + (h : setPriority (Z := Unit) t p σ = ({((), σ')}, False)) (ht : t ≠ σ.curThread) : CKeepA σ σ' := + (setPriority_neA t p σ ((), σ') (by rw [h]; exact Set.mem_singleton _)).2 ht + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3SigBoundCancelA.lean b/proofs/Fl4ProofsA64/P3/Stage3SigBoundCancelA.lean new file mode 100644 index 000000000..b733d2ad4 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SigBoundCancelA.lean @@ -0,0 +1,199 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2a): **`cancel_ipc` of a receiver without a reply object**, at the footprint, spec and +record levels (the first half of `send_signal` on an idle notification whose bound thread is receive-blocked). + +* `cancel_ipc_recvNRF`: the twin of `Endp.cancel_ipc_recvF` for `reply = none`. +* `cancelRecvNR_specA`, `cancelIpc_recvNR_blkA`: the spec side. +* `JStage3TA.cancelRecvNR_recA`: the twin of `JStage3TA.replyRecv_recA`. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.ReplyHookRecvA +import Fl4ProofsA64.P3.DelSuspendSpecA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +section Fl4 +variable {S A : Type} {st : machine.Store S} [L : LawfulStore st] {ar : machine.Arch A} +variable {Z : XSetsA} {m : McState} {T E R : Set U64} {Bf : Set (U64 × Word)} {SR : S → State Unit → Prop} + +/-- **The footprint of `cancel_ipc` of a receiver without a reply object**. -/ +theorem cancel_ipc_recvNRF {Wr : State Unit → S → U64 → Prop} + (hO : SchedOpsEA st T E R Bf SR) (hF : SchedFootE0A st T E R Bf SR Wr) + (hP : EPlaced T E R Bf) (s : S) (a : A) (σ : State Unit) + (hRE : EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) {t : U64} (ht : t ∈ T) {x : Tcb} + (hk : σ.kheap (specPtr t) = some (.tcb x)) {e : U64} (he : e ∈ E) {z : Endpoint} + (hkz : σ.kheap (specPtr e) = some (.endpoint z)) (hq : specPtr t ∈ z.epQueue) (hbe : L.mem s (tBlockA t) = e) + {d : ReceiverPayload} (hs : x.tcbState = .blockedOnReceive (specPtr e) none d) + {Q : Set U64} (htQ : t ∈ Q) (hQs : ∀ w ∈ T, L.mem s (tBlockA w) = e → w ∈ Q) : + ipc.cancel_ipc st ar s a t ⦃ out => WritesIn st s out.1 + (fun b => b = tFaultA t ∨ EpSigWA R Q e b ∨ Wr σ s b) ⦄ := by + have hta := hP.tcb t ht + have hx := hRE.at_tcb ht hk + obtain ⟨hR1, hW1, hS1, hag⟩ := clear_fault_rel hP s σ hRE ht hk + have vf := val_fault hta + have hb1 : L.mem (L.wr s (tFaultA t) 0#u64) (tBlockA t) = e := + (hag _ (a_blk hta).1 (by dsimp only; rw [val_blk hta, vf]; omega)).trans hbe + have wF : WritesIn st s (L.wr s (tFaultA t) 0#u64) (fun b => b = tFaultA t) := writesIn_wr (a_fault hta).1 + unfold ipc.cancel_ipc faults.clear_fault + step with obj.Tcb.set_fault_type.spec as ⟨s1, hs1⟩ + have e1 : s1 = L.wr s (tFaultA t) 0#u64 := by rw [hs1]; unfold fl4_abi.consts.fault.NullFault; rfl + subst e1 + step with obj.Tcb.state.spec as ⟨w, hw⟩ + have hw' : w.val = tsCode x.tcbState := by + rw [hw, hag _ (a_st hta).1 (by dsimp only; rw [val_st hta, vf]; omega)]; exact hx.st + have hwR : w = sched.TS_BLOCKED_ON_RECV := + UScalar.eq_of_val_eq (by rw [hw', hs, tsVals.2.2.1]; rfl) + have hSR4 : sched.TS_BLOCKED_ON_RECV ≠ sched.TS_BLOCKED_ON_SEND := by rw [tsVals.2.2.1, tsVals.2.2.2.1]; decide + rw [if_neg (by rw [hwR]; exact hSR4), if_pos hwR] + -- the state after the fault clear + have hte : specPtr e ≠ specPtr t := by intro h; rw [h, hk] at hkz; cases hkz + have hk1 : (updK σ (specPtr t) (.tcb { x with tcbFault := none })).kheap (specPtr t) = + some (.tcb { x with tcbFault := none }) := by rw [updK_kheap, Function.update_self] + have hkz1 : (updK σ (specPtr t) (.tcb { x with tcbFault := none })).kheap (specPtr e) = some (.endpoint z) := by + rw [updK_kheap, Function.update_of_ne hte, hkz] + have hSR1 : SR (L.wr s (tFaultA t) 0#u64) (updK σ (specPtr t) (.tcb { x with tcbFault := none })) := + hO.stable _ _ _ _ hSR hW1 hS1 (NoBlockE.updT hk _ (Or.inl ⟨rfl, fun _ => + Or.inr (Or.inr (Or.inr ⟨_, _, _, hs⟩))⟩)) + have z1 : SSzA σ (updK σ (specPtr t) (.tcb { x with tcbFault := none })) := ssz_updK_tcb _ _ _ + unfold epArmProg at * + have hep : obj.Tcb.block_obj st t (L.wr s (tFaultA t) 0#u64) ⦃ i => i = e ⦄ := by + step with obj.Tcb.block_obj.spec as ⟨i, hi⟩ + exact hi.trans hb1 + refine WP.spec_bind hep ?_ + rintro i rfl + obtain ⟨xs, hQr, hxs, hB⟩ := (hR1.at_ep he hkz1).queue + have htx : t ∈ xs := by + have hq' := hq + rw [hxs, List.mem_map] at hq' + obtain ⟨y, hy, ey⟩ := hq' + rw [← specPtr_inj ey]; exact hy + have hxsQ : ∀ v ∈ xs, v ∈ Q := fun v hv => by + have vT := hQr.mem v hv + have va := hP.tcb v vT + refine hQs v vT ?_ + rw [← (hB v hv).1] + by_cases ev : v = t + · rw [ev]; exact (hag _ (a_blk hta).1 (by dsimp only; rw [val_blk hta, vf]; omega)).symm + · refine (hag _ (a_blk va).1 ?_).symm + have := tcb_disj hP ht vT ev + dsimp only; rw [val_blk va, vf]; omega + refine WP.spec_bind (WP.spec_and (ep_dequeue_rel hO hP _ _ hR1 hSR1 he hkz1 hq) + (ep_dequeue_sigE hP _ he t xs hQr htx (Q := Q) hxsQ)) ?_ + rintro s2 ⟨⟨-, hR2, hSR2, hA2⟩, w2⟩ + have z2 : SSzA σ (updK (updK σ (specPtr t) (.tcb { x with tcbFault := none })) (specPtr i) + (.endpoint (epDeq z (specPtr t)))) := z1.trans (ssz_of_epStep (EpStep.ep hkz1)) + have hkd : (updK (updK σ (specPtr t) (.tcb { x with tcbFault := none })) (specPtr i) + (.endpoint (epDeq z (specPtr t)))).kheap (specPtr t) = some (.tcb { x with tcbFault := none }) := by + rw [updK_kheap, Function.update_of_ne hte.symm, hk1] + have hkyd : (updK (updK σ (specPtr t) (.tcb { x with tcbFault := none })) (specPtr i) + (.endpoint (epDeq z (specPtr t)))).kheap (specPtr i) = some (.endpoint (epDeq z (specPtr t))) := by + rw [updK_kheap, Function.update_self] + have hb2 : L.mem s2 (tBlockA t) = i := (hA2 _ (a_blk hta).1 ⟨t, ht, (a_blk hta).2⟩).trans hb1 + have hq2 := dequeued_not_queued hR2 he hb2 hkyd (epDeq_not_mem hxs hQr.nodup t) + have hrec := (hR2.at_tcb ht hkd).recv (specPtr i) none d hs + have hrep : obj.Tcb.reply_obj st t s2 ⦃ v => v = 0#u64 ⦄ := by + step with obj.Tcb.reply_obj.spec as ⟨v, hv⟩ + have hv' : v = L.mem s2 (tReplyA t) := hv + have h1 := hrec.1 + rw [← hv'] at h1 + by_contra v0 + rw [optRef_ne v0] at h1 + cases h1 + simp only [hwR, ↓reduceIte, Std.bind_assoc] + refine WP.spec_bind hrep ?_ + rintro v rfl + simp only [bne_self_eq_false, Bool.false_eq_true, ↓reduceIte, bind_ok] + obtain ⟨hk4, hq4, -, -⟩ := sts_facts hkd hq2 .inactive + have hblk : runnable x.tcbState = false := by rw [hs]; rfl + refine WP.spec_bind (set_thread_state_sigE hO hF hP hSR2 _ _ hR2 (WritesIn.refl _ _) (EpStep.refl _) ht hkd hq2 + .inactive (by decide) sched.TS_INACTIVE (by unfold sched.TS_INACTIVE; rfl) + (NoBlockE.updT hkd _ (Or.inr (Or.inr ⟨rfl, hblk, fun h => by rw [hs] at h; cases h⟩))) (Q := Q) (e := i) htQ) ?_ + rintro s3 ⟨-, -, w3⟩ + rw [WP.spec_ok] + have k2 : L.ksb s2 = L.ksb s := w2.1.trans wF.1 + refine (WritesIn.trans (st := st) wF (WritesIn.trans (st := st) w2 w3)).mono fun b h => ?_ + rcases h with h | h | (h | h) + · exact Or.inl h + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr (hF.back z2 k2 h)) + +end Fl4 + +/-- **The spec side of `cancel_ipc` of a receiver without a reply object**. -/ +theorem cancelRecvNR_specA {σ : State Unit} {t e : ObjRef} {x : Tcb} (hk : σ.kheap t = some (.tcb x)) + {z : Endpoint} (hkz : σ.kheap e = some (.endpoint z)) (hq : t ∈ z.epQueue) {d : ReceiverPayload} + (hs : x.tcbState = .blockedOnReceive e none d) : + ∃ σ', cancelIpc (Z := Unit) t σ = ({((), σ')}, False) ∧ + EpSpecKeepA (updK σ t (.tcb { x with tcbFault := none })) σ' ∧ + (∀ q, q ≠ t → q ≠ e → σ'.kheap q = σ.kheap q) ∧ + σ'.curThread = σ.curThread ∧ σ'.idleThread = σ.idleThread ∧ σ'.curSc = σ.curSc := by + have het : e ≠ t := by intro h; rw [h, hk] at hkz; cases hkz + let σ1 := updK σ t (.tcb { x with tcbFault := none }) + have hkz1 : σ1.kheap e = some (.endpoint z) := by + simp only [σ1, updK_kheap, Function.update_of_ne het]; exact hkz + have hdq := tcbEpDequeue_eq (Z := Unit) hkz1 hq + let σ2 := updK σ1 e (.endpoint (epDeq z t)) + have hk2 : σ2.kheap t = some (.tcb { x with tcbFault := none }) := by + simp only [σ2, σ1, updK_kheap, Function.update_of_ne het.symm, Function.update_self] + have hnn : (∀ n, x.tcbState ≠ .blockedOnNotification n) ∧ ∀ r, x.tcbState ≠ .blockedOnReply r := + ⟨(fun n h => by rw [hs] at h; cases h), (fun n h => by rw [hs] at h; cases h)⟩ + refine ⟨_, cancelIpc_blocked hk none (Or.inr ⟨e, d, hs⟩) (bci_none (by rw [hs]; rfl) hdq hk2), ?_, + fun q q1 q2 => by + simp only [σ2, σ1, stsAct_kheap, updK_kheap, Function.update_of_ne q1, Function.update_of_ne q2], + by unfold stsAct; split_ifs <;> rfl, by unfold stsAct; split_ifs <;> rfl, by unfold stsAct; split_ifs <;> rfl⟩ + have k1 : EpSpecKeepA σ1 σ2 := EpSpecKeepA.of_upd hkz1 (o' := .endpoint (epDeq z t)) ⟨_, rfl⟩ + exact k1.trans (epk_sts hk2 hnn (Or.inr rfl)) + +/-- The blocked-queue monotonicity of that cancel. -/ +theorem cancelIpc_recvNR_blkA {σ σ' : State Unit} {t e : ObjRef} {x : Tcb} (hk : σ.kheap t = some (.tcb x)) + {z : Endpoint} (hkz : σ.kheap e = some (.endpoint z)) (hq : t ∈ z.epQueue) {d : ReceiverPayload} + (hs : x.tcbState = .blockedOnReceive e none d) + (h : ((), σ') ∈ (cancelIpc (Z := Unit) t σ).1) : + BlkMonoA e σ σ' ∧ ∀ u ∈ z.epQueue, + NoBA σ' u ∨ ∃ z', σ'.kheap e = some (.endpoint z') ∧ u ∈ z'.epQueue := by + have het : e ≠ t := fun c => by rw [c, hk] at hkz; cases hkz + have hkz1 : (updK σ t (.tcb { x with tcbFault := none })).kheap e = some (.endpoint z) := by + rw [updK_kheap, Function.update_of_ne het]; exact hkz + have hf : BlkMonoA e σ (updK σ t (.tcb { x with tcbFault := none })) := + BlkMonoA.updA hk (fun y h => by cases h; exact ⟨_, rfl, Or.inl rfl⟩) (fun _ h => by cases h) + (fun _ h => by cases h) + have hdq := tcbEpDequeue_eq (Z := Unit) hkz1 hq + have hmono := tcbEpDequeue_blkA t e (updK σ t (.tcb { x with tcbFault := none })) + ((), updK (updK σ t (.tcb { x with tcbFault := none })) e (.endpoint (epDeq z t))) + (by rw [hdq]; exact rfl) + unfold cancelIpc at h + rw [nd_bind_one (getThreadState_eq hk), nd_bind_one (threadSet_eq _ hk)] at h + generalize hσ1 : updK σ t (.tcb { x with tcbFault := none }) = σ1 at h hdq hf hmono + rw [hs] at h + dsimp only at h + unfold blockedCancelIpc at h + obtain ⟨y1, hy1, h⟩ := mem_bindA.1 h + have e1 : y1 = (e, σ1) := hy1 + subst e1 + obtain ⟨y2, hy2, h⟩ := mem_bindA.1 h + rw [hdq] at hy2 + have e2 : y2 = ((), updK σ1 e (.endpoint (epDeq z t))) := hy2 + subst e2 + dsimp only at h + try rw [nd_pure_bind'] at h + have k2 := setThreadState_blkA e t noBlk_inactiveA _ _ h + have kt := setThreadState_blkA t t noBlk_inactiveA _ _ h + refine ⟨(hf.trans hmono).trans k2, fun u hu => ?_⟩ + by_cases c : u = t + · subst c + obtain ⟨y, hy, ey⟩ := setThreadState_memA h + exact Or.inl ⟨y, hy, ey ▸ noBlk_inactiveA⟩ + · refine Or.inr ⟨epDeq z t, kt.ep e _ het (by rw [updK_kheap, Function.update_self]), ?_⟩ + rw [epDeq_queue] + unfold removeAll + exact List.mem_filter.2 ⟨hu, decide_eq_true (Ne.symm c)⟩ + +end + +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/Stage3SigBoundRecA.lean b/proofs/Fl4ProofsA64/P3/Stage3SigBoundRecA.lean new file mode 100644 index 000000000..d51628d82 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SigBoundRecA.lean @@ -0,0 +1,808 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2a): **Notification send to an idle notification whose bound TCB is blocked on a +receive, with or without a reply object, at the record**. + +* `JStage3TA.cancelRecvNR_recA`: the record across `cancel_ipc` of that receiver (twin of + `JStage3TA.replyRecv_recA`), with the write set and the frames the wake needs. +* `JStage3TA.cancelRecvAny_recA`: both receivers (the reply case is `JStage3TA.replyRecv_recFA`). +* `JStage3A.sigBoundRecv_stepA`: the whole `send_signal` arm (cancel, then the wake chain of + `P3/Stage3SigWakeRecA`). +* `JSyscallA.signalBoundRecvA`: the arm at a syscall entry. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3SigWakeRecA +import Fl4ProofsA64.P3.Stage3SigBoundCancelA +import Fl4ProofsA64.P3.Stage3TcbResumeSpecA +import Fl4ProofsA64.P3.SendSignalDetA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 Fl4ProofsA64.P3.Endp +open Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Notif + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} + +/-- **The record across `cancel_ipc` of a receiver without a reply object** (module docstring). -/ +theorem JStage3BA.cancelRecvNR_recA {ar : machine.Arch A} + (J : JStage3BA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {t : U64} (ht : t ∈ Z.T) {x : Fl4SpecA64.Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + {e' : ObjRef} {d : ReceiverPayload} (hs : x.tcbState = .blockedOnReceive e' none d) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) : + ipc.cancel_ipc st ar s a t ⦃ r => + r.2 = a ∧ ∃ σ' x' Q R, + cancelIpc (Z := Unit) (specPtr t) σ = ({((), σ')}, False) ∧ + JStage3BA st AR { Z with Q := Q, R := R } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) r.1 a σ' m ∧ + IpcSchedRelA st Z m κ r.1 σ' ∧ + WritesIn st s r.1 (SchedStepNWA (st := st) N E Rp Z m σ s) ∧ + (∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → + (K.val ≠ 3328 ∧ K.val ≠ 3496 ∧ K.val ≠ 3504 ∧ K.val ≠ 3464 ∧ K.val ≠ 3480 ∧ K.val ≠ 3488 ∧ + K.val ≠ 3592) → K.val ≠ 3376 → L.mem r.1 (wa u K) = L.mem s (wa u K)) ∧ + SchedFrameA σ σ' ∧ QKeepA σ σ' ∧ σ'.kheap (specPtr t) = some (.tcb x') ∧ x'.tcbState = .inactive ∧ + RelIfBound σ' (specPtr t) ∧ + EpSpecKeepA (updK σ (specPtr t) (.tcb { x with tcbFault := none })) σ' ∧ + (∀ q, q ≠ specPtr t → (∀ z, σ.kheap q ≠ some (.endpoint z)) → σ'.kheap q = σ.kheap q) ∧ + σ'.curThread = σ.curThread ∧ σ'.idleThread = σ.idleThread ∧ σ'.curSc = σ.curSc ∧ + WritesIn st s r.1 (CancelFootA st Rp Z.T s t) ⦄ := by + have htH : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + have hc := J.curCore_lt hCs + have hP := J.iplaced + have hta := hP.e.tcb t ht + have hx := J.ep.at_tcb ht hk + have hrecv : L.mem s (Fl4ProofsA64.P1.stA t) = sched.TS_BLOCKED_ON_RECV := by + refine UScalar.eq_of_val_eq ?_ + have := hx.st + rw [hs] at this + exact this.trans (by unfold sched.TS_BLOCKED_ON_RECV; rfl) + have h3 : tsCode x.tcbState = 3 := by rw [hs]; rfl + have hb : epBlocked x.tcbState = some e' := by rw [hs]; rfl + obtain ⟨heE, z, hkz, hq⟩ := J.blkQ.ep t ht x hk e' hb + obtain ⟨e, ee⟩ : ∃ e, L.mem s (tBlockA t) = e := ⟨_, rfl⟩ + have es : specPtr e = e' := by rw [← ee]; exact hx.blk e' hb + subst es + rw [ee] at heE + have hrep : RepOKA σ (specPtr t) := J.repOKA ht + let Q : Set U64 := {q | q ∈ Z.T ∧ L.mem s (tBlockA q) = e} + have hQ : Q ⊆ Z.T := fun _ h => h.1 + have htQ : t ∈ Q := ⟨ht, ee⟩ + have ops := J.schedOpsA (ar := ar) hc hmb hmax htu + have hSched : CancelScheduleA st N Z.T (IpcSchedRelA st Z m κ) := cancelScheduleA_of_placement J.ipcPlace + have rel := cancel_ipc_rel (ar := ar) ops.1 hSched ops.2.2.1 hP s a σ J.ntfn J.ep J.ipcSchedA ht hk + (fun r h => by rw [hs] at h; cases h) + (fun e1 h => by + rw [hb] at h + cases h + exact ⟨by rw [ee]; exact heE, z, hkz, hq⟩) + (fun n' h => by rw [hs] at h; cases h) hrep + have fp := cancel_ipc_recvNRF (ar := ar) (Wr := fun _ s b => SKsA (L.ksb s) b) (J.schedOpsEA (ar := ar) hc hmb hmax htu) + (schedFootE0A_ks (st := st) (Z := Z) (m := m) (κ := κ) (E := E) (R := Rp) (Bf := Bf)) + hP.e s a σ J.ep J.ipcSchedA ht hk heE hkz hq ee hs (Q := Q) htQ (fun w hw h => ⟨hw, h⟩) + have of := cancelIpc_of_rel (ar := ar) ops.1 hSched ops.2.2.1 hP s a σ J.ntfn J.ep J.ipcSchedA ht hk + (by rw [hs]; rfl) hrep (by rw [ee]; exact heE) (fun e1 h => by + rw [hb] at h + cases h + exact ⟨z, hkz, hq⟩) + obtain ⟨σ', hsp, K, hoth, hct, hit, hcs⟩ := cancelRecvNR_specA hk hkz hq hs + have blk := J.cancelRecv_blkA (ar := ar) htH + refine WP.spec_mono (WP.spec_and (WP.spec_and blk of) (WP.spec_and rel fp)) ?_ + rintro ⟨s', a'⟩ ⟨⟨blk', σ3, x3, hsp3, -, -, -, hk3, hin3, hrb3⟩, ⟨ha, σ2, hsp2, ntfn, E', SR'⟩, wF⟩ + dsimp only at blk' ha E' SR' ntfn wF hk3 ⊢ + have e2 : σ2 = σ' := by + have := hsp2.symm.trans hsp + simpa using congrArg Prod.fst this + have e3 : σ3 = σ' := by + have := hsp3.symm.trans hsp + simpa using congrArg Prod.fst this + subst e2 e3 + have F : SchedFrameA σ σ3 := + cancelIpc_presA (specPtr t) σ ((), σ3) (by rw [hsp]; exact Set.mem_singleton _) + -- the spec state after the fault clear + have hk1 : (updK σ (specPtr t) (.tcb { x with tcbFault := none })).kheap (specPtr t) = + some (.tcb { x with tcbFault := none }) := by rw [updK_kheap, Function.update_self] + have k1 : ∀ q o, (∀ y, o ≠ .tcb y) → σ.kheap q = some o → + (updK σ (specPtr t) (.tcb { x with tcbFault := none })).kheap q = some o := fun q o ho h => by + have ne : q ≠ specPtr t := fun e0 => by rw [e0, hk] at h; cases h; exact ho x rfl + rw [updK_kheap, Function.update_of_ne ne]; exact h + have k1b : ∀ q o, (∀ y, o ≠ .tcb y) → + (updK σ (specPtr t) (.tcb { x with tcbFault := none })).kheap q = some o → σ.kheap q = some o := + fun q o ho h => by + have ne : q ≠ specPtr t := fun e0 => by rw [e0, hk1] at h; cases h; exact ho _ rfl + rw [updK_kheap, Function.update_of_ne ne] at h; exact h + have k1t : ∀ q y, σ.kheap q = some (.tcb y) → ∃ y1, + (updK σ (specPtr t) (.tcb { x with tcbFault := none })).kheap q = some (.tcb y1) ∧ + y1.tcbMcpriority = y.tcbMcpriority ∧ y1.tcbYieldTo = y.tcbYieldTo ∧ y1.tcbIpcframe = y.tcbIpcframe ∧ + y1.tcbIpcBuffer = y.tcbIpcBuffer ∧ (y.tcbFault = none → y1.tcbFault = none) ∧ + y1.tcbFlags = y.tcbFlags := fun q y h => by + by_cases e0 : q = specPtr t + · have ey : y = x := by + rw [e0, hk] at h + exact (KernelObject.tcb.inj (Option.some.inj h)).symm + rw [ey] + exact ⟨{ x with tcbFault := none }, by rw [e0]; exact hk1, rfl, rfl, rfl, rfl, fun _ => rfl, rfl⟩ + · exact ⟨y, by rw [updK_kheap, Function.update_of_ne e0]; exact h, rfl, rfl, rfl, rfl, id, rfl⟩ + have TK : ∀ q y, σ.kheap q = some (.tcb y) → ∃ y2, σ3.kheap q = some (.tcb y2) ∧ + y2.tcbIpcframe = y.tcbIpcframe ∧ y2.tcbIpcBuffer = y.tcbIpcBuffer ∧ + (y.tcbFault = none → y2.tcbFault = none) := fun q y h => by + obtain ⟨y1, h1, -, -, f, b, g, -⟩ := k1t q y h + obtain ⟨y2, h2, k⟩ := K.tcb h1 + exact ⟨y2, h2, k.frame.trans f, k.buf.trans b, fun e0 => k.fault.trans (g e0)⟩ + have wC : WritesIn st s s' (CancelFootA st Rp Z.T s t) := by + refine wF.mono fun b h => ?_ + rcases h with h | h | h + · exact Or.inl h + · refine Or.inr (Or.inl ?_) + rw [ee] + exact h + · exact Or.inr (Or.inr h) + have wE : WritesIn st s s' (fun b => b = tFaultA t ∨ EpSigWA Rp Q e b ∨ MWrA (st := st) Z m σ s b) := + wF.mono fun b h => by + rcases h with h | h | h + · exact Or.inl h + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr (Or.inr (Or.inl h))) + have w : WritesIn st s s' (SchedStepNWA (st := st) N E Rp Z m σ s) := + wE.mono fun _ h => J.recvW_stepNA heE hQ ht h + have tw : ∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → + (K.val ≠ 3328 ∧ K.val ≠ 3496 ∧ K.val ≠ 3504 ∧ K.val ≠ 3464 ∧ K.val ≠ 3480 ∧ K.val ≠ 3488 ∧ + K.val ≠ 3592) → K.val ≠ 3376 → L.mem s' (wa u K) = L.mem s (wa u K) := fun u hu K h1 h2 h3 h4 => + wE.mem_eq (field_aligned (J.sr.core.tcbs u hu) h1 h2) (J.tcbWord_offRecvA heE hQ ht hu h1 h3 h4) + obtain ⟨Q', R', H3⟩ := SR' + have rv : RunVcpuInv st H.tcbs s' := by + intro y hy h0 + have hyT : y ∈ Z.T := by rw [← J.tcbs]; exact hy + have ev : L.mem s' (runVcpuA y) = L.mem s (runVcpuA y) := + tw y hyT 3672#u64 (by simp) (by simp) (by simp) (by simp) + rw [ev] at h0 + have RV : RunVcpuInv st H.tcbs s := J.tcbs ▸ J.runVcpu + have hR := RV y hy h0 + have ne : specPtr y ≠ specPtr t := fun c => by + have := specPtr_inj c + subst this + rw [hrecv] at hR + exact absurd hR (by unfold sched.TS_BLOCKED_ON_RECV sched.TS_RUNNING_VM; decide) + obtain ⟨x0, hx0, -⟩ := J.ntfn.tcb y hyT + have ne2 : specPtr y ≠ specPtr e := fun c => by rw [c, hkz] at hx0; cases hx0 + have c0 := (J.ep.at_tcb hyT hx0).st + have c1 := (E'.at_tcb hyT ((hoth _ ne ne2).trans hx0)).st + exact (UScalar.eq_of_val_eq (c1.trans c0.symm) : L.mem s' (stA y) = L.mem s (stA y)).trans hR + have sq : SchedQueuesInv st H.tcbs Z.Cs H.omega H s' Q' R' := by + obtain ⟨Q0, R0, h0'⟩ := blk'.1 + exact SchedQueuesInv.reQA h0' (by rw [J.tcbs]; exact H3.core) + have kb : L.ksb s' = L.ksb s := w.1 + have poolOff : PoolOff st H Z.Cs OS s' := fun i => by + have h0' := J.poolOff i + rw [vspace.asid_table.eq] at h0' ⊢ + have v := AtOff.v i + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.ent.ksw + have ev : (wa (L.ksb s) (AtOff i)).val = (L.ksb s).val + (AtOff i).val := by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have e0 : L.mem s' (wa (L.ksb s') (AtOff i)) = L.mem s (wa (L.ksb s) (AtOff i)) := by + rw [kb] + refine w.mem_eq ?_ (J.asid_offStepNA (by omega) (by omega)) + have h32 := J.j.1.ksbAl + show (wa (L.ksb s) (AtOff i)).val % 8 = 0 + omega + rw [e0] + refine (WP.spec_ok _).2 fun hne asid => ?_ + have q := (WP.spec_ok _).1 h0' hne asid + exact ⟨q.al, q.om, q.sg, q.os, RegionOffNodes.ksb kb q.ks⟩ + have reply : ReplyStackRelA (L.mem s') Rp Z.C σ3 (scNameA m) stack := by + have R1 : ReplyStackRelA (L.mem s) Rp Z.C (updK σ (specPtr t) (.tcb { x with tcbFault := none })) + (scNameA m) stack := + J.reply.frame (fun _ _ => ⟨rfl, rfl⟩) + (fun _ _ y k h => ⟨y, k, k1 _ _ (fun _ e0 => by cases e0) h, rfl⟩) + (fun _ _ y h => ⟨y, k1b _ _ (fun _ e0 => by cases e0) h, rfl⟩) + refine R1.epFr K fun r hr => ?_ + obtain ⟨r32, -, rfit⟩ := hP.e.rep r hr + have one : ∀ o : U64, 8 ≤ o.val → o.val < 32 → o.val % 8 = 0 → L.mem s' (wa r o) = L.mem s (wa r o) := by + intro o o0 o1 o2 + have v : (wa r o).val = r.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + refine wE.mem_eq ?_ ?_ + · show (wa r o).val % 8 = 0 + omega + rintro (h | h) + · have h' := congrArg (fun w : U64 => w.val) h + simp only [v, val_fault hta] at h' + have := hP.e.rt r hr t ht + omega + · exact J.reply_offEpA heE hQ hr (by omega) (by omega) h + have n16 : (layout.reply.NEXT).val = 16 := by unfold layout.reply.NEXT; rfl + have p8 : (layout.reply.PREV).val = 8 := by unfold layout.reply.PREV; rfl + exact ⟨one _ (by omega) (by omega) (by omega), one _ (by omega) (by omega) (by omega)⟩ + have mcp : McpRelA Z.T m (L.mem s') σ3 := by + have M1 : McpRelA Z.T m (L.mem s) (updK σ (specPtr t) (.tcb { x with tcbFault := none })) := fun u hu => by + obtain ⟨y, hy, e0, hf⟩ := J.mcp u hu + obtain ⟨y1, h1, m1, -, -, -, -, fl⟩ := k1t _ _ hy + exact ⟨y1, h1, by rw [m1]; exact e0, fun g => (hf g).keep rfl fl⟩ + exact M1.epFr K fun u hu => ⟨tw u hu 3440#u64 (by simp) (by simp) (by simp) (by simp), + tw u hu 3456#u64 (by simp) (by simp) (by simp) (by simp)⟩ + have yr : YieldRelA Z.T Z.C m (L.mem s') σ3 := by + have Y1 : YieldRelA Z.T Z.C m (L.mem s) (updK σ (specPtr t) (.tcb { x with tcbFault := none })) := by + refine ⟨fun u hu => ?_, fun u hu h => ?_, fun c hcc y k hy q hq' => ?_⟩ + · obtain ⟨y, hy, e0⟩ := J.yieldRel.tcb u hu + obtain ⟨y1, h1, -, m1, -⟩ := k1t _ _ hy + exact ⟨y1, h1, by rw [m1]; exact e0⟩ + · obtain ⟨hcc, y, k, hy, e0⟩ := J.yieldRel.toSc u hu h + exact ⟨hcc, y, k, k1 _ _ (fun _ e1 => by cases e1) hy, e0⟩ + · exact J.yieldRel.frm c hcc y k (k1b _ _ (fun _ e1 => by cases e1) hy) q hq' + exact Y1.epFr K fun u hu => tw u hu 3472#u64 (by simp) (by simp) (by simp) (by simp) + have buf : BufCover st Z.T Bf s' σ3 := + J.bufCover_wA wE (fun u hu K hK => J.tcbWord_offRecvA heE hQ ht hu (by omega) (by omega) (by omega)) + (fun q y h => by + obtain ⟨y2, hy, b, c, -⟩ := TK q y h + exact ⟨y2, hy, b, c⟩) + have bufAl : UtBufAlA Z.T σ3 := fun u hu y hy => by + obtain ⟨x0, hx0, -⟩ := J.ntfn.tcb u hu + obtain ⟨y', hy', -, e0, -⟩ := TK _ _ hx0 + rw [hy] at hy'; cases hy' + rw [e0]; exact J.bufAl u hu x0 hx0 + have idle : IdleNoFaultA m σ3 := fun c hc' => by + obtain ⟨x0, hx0, hf⟩ := J.idleNoFault c hc' + obtain ⟨y, hy, -, -, g⟩ := TK _ _ hx0 + exact ⟨y, hy, g hf⟩ + have scCov : ScCovA Z.C m σ3 := fun q y k h => by + obtain ⟨y', hy⟩ := F.2.1 q y k h + exact J.scCov q y' k hy + have inv : SPairA σ3 m n := SPairA.cancelRecv ⟨J.sinv, J.sidle⟩ hk hs hkz hq hrep + (by rw [hsp]; exact Set.mem_singleton _) + have hvr : ∀ sc ∈ Z.C, ∀ q ∈ stack sc, RBlk st s' q := by + refine J.vr_specA blk' (fun _ _ h => h) (fun sc _ q _ c _ _ x2 o hx2 hs2 => ?_) E' + (fun c hc => tw c hc 3360#u64 (by simp) (by simp) (by simp) (by simp)) + have ne : specPtr c ≠ specPtr t := fun e0 => by + rw [e0, hk] at hx2 + rw [← KernelObject.tcb.inj (Option.some.inj hx2), hs] at hs2 + cases hs2 + have h1 : (updK σ (specPtr t) (.tcb { x with tcbFault := none })).kheap (specPtr c) = some (.tcb x2) := by + rw [updK_kheap, Function.update_of_ne ne]; exact hx2 + obtain ⟨y, hy, k⟩ := K.tcb h1 + refine ⟨y, hy, ?_⟩ + rcases k.st with e | ⟨⟨-, nr⟩, -⟩ + · exact e + · exact absurd hs2 (nr o) + have j := J.j57_stepNWA w σ3 + have bq : BlkQueuedA Z.T E N (L.mem s') σ3 := by + obtain ⟨M, dq⟩ := cancelIpc_recvNR_blkA hk hkz hq hs (σ' := σ3) (by rw [hsp]; exact Set.mem_singleton _) + refine J.blkQ.finA M (fun u hu => ?_) (fun u hu => ?_) (fun y hy r hr => ?_) (fun y q hn => ?_) + · obtain ⟨x0, hx0, -⟩ := J.ntfn.tcb u hu + exact ⟨x0, hx0⟩ + · rw [Notif.tBlockA_eq]; exact tw u hu 3336#u64 (by simp) (by simp) (by simp) (by simp) + · rw [hkz] at hy; cases hy; exact dq r hr + · rw [hkz] at hn; cases hn + have J' := JStage3BA.of_schedWrN (blkQ := bq) J (Q := Q') (Rl := R') (curOf := Z.curOf) (curScOf := Z.curScOf) (s' := s') + (a' := a) (σ' := σ3) w F.1 F.2.1 + (Sched.qk_of (m := cancelIpc (Z := Unit) (specPtr t)) (by rw [hsp]; exact Set.mem_singleton _)) F.2.2 J.j.1.ar (fun _ _ => rfl) bufAl (J.ent_stepNWA w blk') sq poolOff H3 + scCov E' ntfn reply buf idle mcp yr inv.sinv inv.sidle (J.tcbs ▸ rv) hvr j.1 j.2 + exact ⟨ha, σ3, x3, Q', R', hsp, ha ▸ J', ⟨Q', R', H3⟩, w, tw, F, + Sched.qk_of (m := cancelIpc (Z := Unit) (specPtr t)) (by rw [hsp]; exact Set.mem_singleton _), hk3, hin3, hrb3, K, + fun q q1 q2 => hoth q q1 (fun c => q2 z (c ▸ hkz)), hct, hit, hcs, wC⟩ + +/-- `JStage3BA.cancelRecvNR_recA` at `JStage3TA` (no switch is started). -/ +theorem JStage3TA.cancelRecvNR_recA {ar : machine.Arch A} + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {t : U64} (ht : t ∈ Z.T) {x : Fl4SpecA64.Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + {e' : ObjRef} {d : ReceiverPayload} (hs : x.tcbState = .blockedOnReceive e' none d) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) : + ipc.cancel_ipc st ar s a t ⦃ r => + r.2 = a ∧ ∃ σ' x' Q R, + cancelIpc (Z := Unit) (specPtr t) σ = ({((), σ')}, False) ∧ + JStage3TA st AR { Z with Q := Q, R := R } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) r.1 a σ' m ∧ + IpcSchedRelA st Z m κ r.1 σ' ∧ + WritesIn st s r.1 (SchedStepNWA (st := st) N E Rp Z m σ s) ∧ + (∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → + (K.val ≠ 3328 ∧ K.val ≠ 3496 ∧ K.val ≠ 3504 ∧ K.val ≠ 3464 ∧ K.val ≠ 3480 ∧ K.val ≠ 3488 ∧ + K.val ≠ 3592) → K.val ≠ 3376 → L.mem r.1 (wa u K) = L.mem s (wa u K)) ∧ + SchedFrameA σ σ' ∧ QKeepA σ σ' ∧ σ'.kheap (specPtr t) = some (.tcb x') ∧ x'.tcbState = .inactive ∧ + RelIfBound σ' (specPtr t) ∧ + EpSpecKeepA (updK σ (specPtr t) (.tcb { x with tcbFault := none })) σ' ∧ + (∀ q, q ≠ specPtr t → (∀ z, σ.kheap q ≠ some (.endpoint z)) → σ'.kheap q = σ.kheap q) ∧ + σ'.curThread = σ.curThread ∧ σ'.idleThread = σ.idleThread ∧ σ'.curSc = σ.curSc ∧ + WritesIn st s r.1 (CancelFootA st Rp Z.T s t) ⦄ := by + refine WP.spec_mono (J.toBA.cancelRecvNR_recA (ar := ar) ht hk hs hCs hmb hmax htu) ?_ + rintro r ⟨ha, σ', x', Q, R, h1, h2, rest⟩ + have N := J.cancelRecv_noSwA ht hk hs (σ' := σ') (by rw [h1]; exact Set.mem_singleton _) + exact ⟨ha, σ', x', Q, R, h1, h2.toTA N.1 N.2, rest⟩ + +/-- **The record across `cancel_ipc` of a receive-blocked thread**, with or without a reply object +(`cancelRecvNR_recA` / `replyRecv_recFA`). -/ +theorem JStage3BA.cancelRecvAny_recA {ar : machine.Arch A} (hA : P0.ArchTotal ar) + (J : JStage3BA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {t : U64} (ht : t ∈ Z.T) {x : Fl4SpecA64.Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + {e' : ObjRef} {ro : Option ObjRef} {d : ReceiverPayload} (hs : x.tcbState = .blockedOnReceive e' ro d) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) : + ipc.cancel_ipc st ar s a t ⦃ r => + r.2 = a ∧ ∃ σ' x' Q R, + cancelIpc (Z := Unit) (specPtr t) σ = ({((), σ')}, False) ∧ + JStage3BA st AR { Z with Q := Q, R := R } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) r.1 a σ' m ∧ + IpcSchedRelA st Z m κ r.1 σ' ∧ + WritesIn st s r.1 (SchedStepNWA (st := st) N E Rp Z m σ s) ∧ + (∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → + (K.val ≠ 3328 ∧ K.val ≠ 3496 ∧ K.val ≠ 3504 ∧ K.val ≠ 3464 ∧ K.val ≠ 3480 ∧ K.val ≠ 3488 ∧ + K.val ≠ 3592) → K.val ≠ 3376 → L.mem r.1 (wa u K) = L.mem s (wa u K)) ∧ + SchedFrameA σ σ' ∧ QKeepA σ σ' ∧ σ'.kheap (specPtr t) = some (.tcb x') ∧ x'.tcbState = .inactive ∧ + RelIfBound σ' (specPtr t) ∧ + (∀ q, q ≠ specPtr t → (∀ z, σ.kheap q ≠ some (.endpoint z)) → (∀ y, σ.kheap q ≠ some (.reply y)) → + σ'.kheap q = σ.kheap q) ∧ + σ'.curThread = σ.curThread ∧ σ'.idleThread = σ.idleThread ∧ σ'.curSc = σ.curSc ∧ + WritesIn st s r.1 (CancelFootA st Rp Z.T s t) ⦄ := by + cases ro with + | none => + refine WP.spec_mono (J.cancelRecvNR_recA (ar := ar) ht hk hs hCs hmb hmax htu) ?_ + rintro r ⟨ha, σ', x', Q, R, h1, h2, h3, h4, h5, h6, h7, h8, h9, h10, -, h12, h13⟩ + exact ⟨ha, σ', x', Q, R, h1, h2, h3, h4, h5, h6, h7, h8, h9, h10, fun q q1 q2 _ => h12 q q1 q2, h13⟩ + | some rr => + have htH : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + have hx := J.ep.at_tcb ht hk + have hrecv : L.mem s (Fl4ProofsA64.P1.stA t) = sched.TS_BLOCKED_ON_RECV := by + refine UScalar.eq_of_val_eq ?_ + have := hx.st + rw [hs] at this + exact this.trans (by unfold sched.TS_BLOCKED_ON_RECV; rfl) + have h0 : L.mem s (tReplyA t) ≠ 0#u64 := fun c => by + have := (hx.recv e' (some rr) d hs).1 + rw [c, optRef_zero] at this + cases this + obtain ⟨p, ep⟩ : ∃ p, L.mem s (tReplyA t) = p := ⟨_, rfl⟩ + have back : p ∈ H.replies ∧ L.mem s (Endp.rTcbA p) = t := ep ▸ J.blkA.2.2.1.2.1.recv t htH hrecv h0 + obtain ⟨pH, et⟩ := back + have p0 : L.mem s (Endp.rTcbA p) ≠ 0#u64 := by + rw [et]; exact fun c => (J.iplaced.e.tcb t ht).ne0 (by rw [c]; rfl) + have w := J.replyRecv_recFA (ar := ar) hA (fun _ _ _ _ h => h) (p := p) pH p0 (by rw [et]; exact hrecv) hCs hmb hmax htu + rw [et] at w + refine WP.spec_mono w ?_ + rintro r ⟨ha, σ', Q, R, h1, h2, -, -, -, h6, -, -, h9, h10, h11, h12, ⟨x', h13, h14⟩, h15, h16, h17⟩ + exact ⟨ha, σ', x', Q, R, h1, h2, h9, h10, h11, h6, h12, h13, h14, h15, h16, h17⟩ + +/-- `JStage3BA.cancelRecvAny_recA` at `JStage3TA` (no switch is started). -/ +theorem JStage3TA.cancelRecvAny_recA {ar : machine.Arch A} (hA : P0.ArchTotal ar) + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {t : U64} (ht : t ∈ Z.T) {x : Fl4SpecA64.Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + {e' : ObjRef} {ro : Option ObjRef} {d : ReceiverPayload} (hs : x.tcbState = .blockedOnReceive e' ro d) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) : + ipc.cancel_ipc st ar s a t ⦃ r => + r.2 = a ∧ ∃ σ' x' Q R, + cancelIpc (Z := Unit) (specPtr t) σ = ({((), σ')}, False) ∧ + JStage3TA st AR { Z with Q := Q, R := R } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) r.1 a σ' m ∧ + IpcSchedRelA st Z m κ r.1 σ' ∧ + WritesIn st s r.1 (SchedStepNWA (st := st) N E Rp Z m σ s) ∧ + (∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → + (K.val ≠ 3328 ∧ K.val ≠ 3496 ∧ K.val ≠ 3504 ∧ K.val ≠ 3464 ∧ K.val ≠ 3480 ∧ K.val ≠ 3488 ∧ + K.val ≠ 3592) → K.val ≠ 3376 → L.mem r.1 (wa u K) = L.mem s (wa u K)) ∧ + SchedFrameA σ σ' ∧ QKeepA σ σ' ∧ σ'.kheap (specPtr t) = some (.tcb x') ∧ x'.tcbState = .inactive ∧ + RelIfBound σ' (specPtr t) ∧ + (∀ q, q ≠ specPtr t → (∀ z, σ.kheap q ≠ some (.endpoint z)) → (∀ y, σ.kheap q ≠ some (.reply y)) → + σ'.kheap q = σ.kheap q) ∧ + σ'.curThread = σ.curThread ∧ σ'.idleThread = σ.idleThread ∧ σ'.curSc = σ.curSc ∧ + WritesIn st s r.1 (CancelFootA st Rp Z.T s t) ⦄ := by + refine WP.spec_mono (J.toBA.cancelRecvAny_recA (ar := ar) hA ht hk hs hCs hmb hmax htu) ?_ + rintro r ⟨ha, σ', x', Q, R, h1, h2, rest⟩ + have N := J.cancelRecv_noSwA ht hk hs (σ' := σ') (by rw [h1]; exact Set.mem_singleton _) + exact ⟨ha, σ', x', Q, R, h1, h2.toTA N.1 N.2, rest⟩ + +end + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +section recB +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {ar : machine.Arch A} {nA : U64} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +set_option maxHeartbeats 4000000 in +/-- **`send_signal` on an idle notification whose bound thread is receive-blocked without a reply object, at the +record** (with or without a reply object; module docstring). -/ +theorem JStage3A.sigBoundRecv_stepA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (hA : P0.ArchTotal ar) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p : U64} (hp : p ∈ N) {x : Notification} (hk : σ.kheap (specPtr p) = some (.notification x)) + (hidle : x.ntfnObj = .idleNtfn) {t : ObjRef} (hb : x.ntfnBoundTcb = some t) {xt : Tcb} + (hkt : σ.kheap t = some (.tcb xt)) {e' : ObjRef} {ro : Option ObjRef} {d : ReceiverPayload} + (hs : xt.tcbState = .blockedOnReceive e' ro d) (badge : U64) : + notification.send_signal st ar s a p badge ⦃ r => r.2 = a ∧ + ∃ σ', sendSignal (Z := Unit) (specPtr p) badge.bv σ = ({((), σ')}, False) ∧ + BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac r.1 ∧ RunVcpuInv st Z.T r.1 ∧ + WritesIn st s r.1 (SchedStepNWA (st := st) N E Rp Z m σ s) ∧ σ'.machineState = σ.machineState ∧ + (SysL st H Z.Cs OS ia ie r.1 → SymL st H Z.Cs r.1 → + Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U r.1 → + J7A st (J7R2A st ar nA) Z H OS r.1 σ' m → + ∃ Q Rl, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) r.1 a σ' m) ⦄ := by + have T := J.toBA + have hP := J.iplaced + have hc := T.curCore_lt hCs + obtain ⟨hH, hO, -⟩ := T.schedOpsA (ar := ar) hc hmb hmax htu + have hF := T.schedFootNA (ar := ar) hc hmb + have hdl := T.donLocal_single hCs h1 hp + have hAr := wakeTailArchA (st := st) (ar := ar) (Z := Z) (m := m) (κ := κ) (N := N) hc hmb + have pH : H p = some .ntfn := J.utIpc.ntfn p hp + have pNt : p ∈ H.ntfns := pH + have rv1 : RunVcpuInv st H.tcbs s := J.tcbs ▸ J.runVcpu + have blk := Fl4ProofsA64.P1.notification.send_signal.tinv0 (ar := ar) hA s a p badge (BXI.blk J.sys.ent.bxi) pNt pH rv1 + have rvi := Fl4ProofsA64.P1.notification.send_signal.rvi (ar := ar) hA s a p badge (BXI.blk J.sys.ent.bxi) pNt pH rv1 + have hx := J.ntfn.at_ntfn hp hk + obtain ⟨hb0, hbt⟩ := optRef_some (hx.bound ▸ hb) + subst hbt + have hiT := hx.bound_mem hb0 + obtain ⟨u, hu⟩ : ∃ u, L.mem s (nBoundA p) = u := ⟨_, rfl⟩ + rw [hu] at hkt hb0 hiT hb + have hrb : Fl4SpecA64.receiveBlocked xt.tcbState = true := by rw [hs]; rfl + have hs3 : L.mem s (Fl4ProofsA64.P1.stA u) = sched.TS_BLOCKED_ON_RECV := by + have h3 := (J.ntfn.at_tcb hiT hkt).st + rw [hs] at h3 + rw [tsVals.2.2.1] + exact UScalar.eq_of_val_eq h3 + have body : notification.send_signal st ar s a p badge ⦃ r => r.2 = a ∧ + ∃ s1 σ1 x1 σ', cancelIpc (Z := Unit) (specPtr u) σ = ({((), σ1)}, False) ∧ + (∃ Q R, JStage3BA st AR { Z with Q := Q, R := R } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s1 a σ1 m) ∧ + WritesIn st s s1 (SchedStepNWA (st := st) N E Rp Z m σ s) ∧ + SchedFrameA σ σ1 ∧ QKeepA σ σ1 ∧ σ1.kheap (specPtr u) = some (.tcb x1) ∧ x1.tcbState = .inactive ∧ + (∀ q, q ≠ specPtr u → (∀ z, σ.kheap q ≠ some (.endpoint z)) → (∀ y, σ.kheap q ≠ some (.reply y)) → + σ1.kheap q = σ.kheap q) ∧ + σ1.curThread = σ.curThread ∧ σ1.idleThread = σ.idleThread ∧ σ1.curSc = σ.curSc ∧ + sendSignal (Z := Unit) (specPtr p) badge.bv σ = specWake (specPtr u) (specPtr p) badge.bv σ1 ∧ + ¬ (specWake (specPtr u) (specPtr p) badge.bv σ1).2 ∧ + ((), σ') ∈ (specWake (specPtr u) (specPtr p) badge.bv σ1).1 ∧ NtfnStateRel N Z.T (L.mem r.1) σ' ∧ + IpcSchedRelA st Z m κ r.1 σ' ∧ + FpW st (fun σ s => MWrA (st := st) Z m σ s) (fun b => b = Fl4ProofsA64.P1.stA u ∨ b = badgeRegA u) σ1 s1 r.1 ⦄ := by + unfold notification.send_signal + step with obj.Ntfn.state.spec as ⟨ns, hns⟩ + have hIdle : ns = obj.NTFN_IDLE := by rw [hns]; exact stOf_idle hx.obj hidle + simp only [hIdle, ↓reduceIte] + step with obj.Ntfn.bound_tcb.spec as ⟨i, hi⟩ + step with obj.Tcb.is_null.spec as ⟨bn, hNull⟩ + have hi' : i = u := hi.trans hu + subst hi' + simp only [hNull, hb0, decide_false, Bool.false_eq_true, ↓reduceIte] + step with obj.Tcb.state.spec as ⟨ts, hts⟩ + simp only [hts, hs3, ↓reduceIte] + refine WP.spec_bind (T.cancelRecvAny_recA (ar := ar) hA hiT hkt hs hCs hmb hmax htu) ?_ + rintro ⟨s1, a1⟩ ⟨ha1, σ1, x1, Q, R, hcan, J1, SR1, w1, tw1, F1, Qk1, hk1, hin, hrb1, hoth, hct, hit, hcs, -⟩ + dsimp only at ha1 J1 SR1 w1 tw1 ⊢ + subst a1 + have hspec : sendSignal (Z := Unit) (specPtr p) badge.bv σ = + specWake (specPtr i) (specPtr p) badge.bv σ1 := by + unfold Fl4SpecA64.sendSignal + rw [nd_bind_one (getNotification_eq hk)] + simp only [hidle, hb] + rw [nd_bind_one (getThreadState_eq hkt)] + simp only [hrb, ↓reduceIte] + rw [nd_bind_one hcan] + rfl + have hq1 : ∀ n' ∈ N, ∀ z, σ1.kheap (specPtr n') = some (.notification z) → + specPtr i ∉ z.ntfnObj.ntfnQueue := by + intro n' hn' z hz hm + obtain ⟨-, h6, -⟩ := (J1.ntfn.at_ntfn hn' hz).obj.blocked hm + have := (J1.ntfn.at_tcb hiT hk1).st + rw [hin, h6] at this + cases this + have hon : L.mem s1 (affA i) = L.cpu s1 := J1.onCore_single hCs h1 hiT + refine WP.spec_mono (wake_refinesW hH hO hF hP.n hAr + (W := fun b => b = Fl4ProofsA64.P1.stA i ∨ b = badgeRegA i) s1 a σ1 J1.ntfn SR1 hiT hp hk1 hq1 badge (Or.inr hrb1) + (by rw [hin]; rfl) (by rw [hin]; exact fun h => by cases h) hon J.ntfn T.ipcSchedA hdl + (cancelIpc_recv_nscK hkt hrb hcan) (Or.inl rfl) (Or.inr rfl)) ?_ + rintro r ⟨har, hnf, σ', hm, Rn, SR, fp⟩ + exact ⟨har, s1, σ1, x1, σ', hcan, ⟨Q, R, J1⟩, w1, F1, Qk1, hk1, hin, hoth, hct, hit, hcs, hspec, hnf, hm, Rn, + SR, fp⟩ + refine WP.spec_mono (WP.spec_and (WP.spec_and body blk) rvi) ?_ + rintro r ⟨⟨⟨har, s1, σ1, x1, σ', hcan, ⟨Q1, R1, J1⟩, w1, F1, Qk1, hk1, hin, hoth, hct, hit, hcs, hspec, hnf, hmw, + Rn, SR', fp⟩, blk'⟩, rv'⟩ + have hm0 : ((), σ') ∈ (sendSignal (Z := Unit) (specPtr p) badge.bv σ).1 := by rw [hspec]; exact hmw + have hne : specPtr p ≠ specPtr u := fun c => by rw [c, hkt] at hk; cases hk + have hk1p : σ1.kheap (specPtr p) = some (.notification x) := + (hoth _ hne (fun z c => by rw [hk] at c; cases c) (fun y c => by rw [hk] at c; cases c)).trans hk + have eupd : updK σ1 (specPtr p) (.notification x) = σ1 := by + unfold updK + rw [← hk1p, Function.update_eq_self] + obtain ⟨σ3, hW, h3, hms⟩ := wake_chainA (x' := x) hk1 hne (by rw [eupd]; exact hmw) + have WK := wake_wakeKeepA hk1p hk1 h3 hms hW + have NK := wake_ntfnSpecKeepA hk1p hk1 hne h3 hW + have QK : QKeepA σ1 σ' := Sched.qk_of hmw + have F : SchedFrameA σ σ' := + (inferInstance : SPresInst (sendSignal (Z := Unit) (specPtr p) badge.bv)).pres σ ((), σ') hm0 + have hQ : ({u} : Set U64) ⊆ Z.T := fun v hv => by rw [Set.mem_singleton_iff] at hv; rw [hv]; exact hiT + have kb1 : L.ksb s1 = L.ksb s := w1.1 + have ssz1 : SSzA σ σ1 := F1.2.1 + have wk : WritesIn st s1 r.1 + (fun b => (SigWrA {u} p b ∨ b = badgeRegA u) ∨ MWrA (st := st) Z m σ s b) := by + refine fp.mono fun b h => ?_ + rcases h with (h | h) | h + · exact Or.inl (Or.inl (Or.inr ⟨u, rfl, Or.inl h⟩)) + · exact Or.inl (Or.inr h) + · exact Or.inr (hF.back ssz1 kb1 h) + have w : WritesIn st s r.1 (SchedStepNWA (st := st) N E Rp Z m σ s) := + (WritesIn.trans (st := st) w1 (wk.mono fun _ h => T.sigWk_stepNWA hp hQ hiT h)).mono + fun _ h => h.elim id id + have hms : σ'.machineState = σ.machineState := by + obtain ⟨-, z, hkz, hq⟩ := J.blkQ.ep u hiT xt hkt e' (by rw [hs]; rfl) + exact (wake_msA hk1 hne (by rw [eupd]; exact hmw)).trans + (cancelIpc_recv_msA hkt hs hkz hq (J.toBA.repOKA hiT) (by rw [hcan]; exact Set.mem_singleton _)) + refine ⟨har, σ', Det.sendSignal_singletonA (by rw [hspec]; exact hnf) hm0, blk', J.tcbs ▸ rv', w, hms, + fun sysl symL userOK j7 => ?_⟩ + have tww : ∀ v ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → + (K.val ≠ 3328 ∧ K.val ≠ 3496 ∧ K.val ≠ 3504 ∧ K.val ≠ 256 ∧ K.val ≠ 3464 ∧ K.val ≠ 3480 ∧ + K.val ≠ 3488 ∧ K.val ≠ 3592) → L.mem r.1 (wa v K) = L.mem s1 (wa v K) := + fun v hv K k1 k2 k3 => wk.mem_eq (field_aligned (J.sr.core.tcbs v hv) k1 k2) (T.tcbWord_offWkA hp hQ hiT hv k1 k3) + obtain ⟨Q', R', H3⟩ := SR' + have notq : ∀ e ∈ E, ∀ z, σ1.kheap (specPtr e) = some (.endpoint z) → specPtr u ∉ z.epQueue := by + have c := (J1.ep.at_tcb hiT hk1).st + rw [hin] at c + have c0 : (L.mem s1 (Fl4ProofsA64.P1.stA u)).val = 0 := c + exact notqE_of_code J1.ep (t := u) (by rw [c0]; decide) (by rw [c0]; decide) + have ep : EpStateRel Z.T E Rp Bf (L.mem r.1) σ' := + EpStateRel.frameWG J1.ep hP hp (Q := {u}) (fun v hv => hQ hv) rfl + (fun e he z hz y _ hq hy => by + rw [Set.mem_singleton_iff] at hy + rw [hy] at hq + exact notq e he z hz hq) + (fun b hb hf hn hbd => wk.mem_eq hb fun h => + h.elim (fun h2 => h2.elim hn hbd) (endpFootOffWrite J.sr.toSchedSRN J.ipcPlace hf)) + (fun v hv z hz => (Rn.at_tcb hv hz).st) (fun z hz => (Rn.at_tcb hiT hz).badge) WK + have reply : ReplyStackRelA (L.mem r.1) Rp Z.C σ' (scNameA m) stack := by + refine J1.reply.ntfnFr NK fun r' hr' => ?_ + obtain ⟨r32, -, rfit⟩ := hP.e.rep r' hr' + have one : ∀ o : U64, o.val < 32 → o.val % 8 = 0 → L.mem r.1 (wa r' o) = L.mem s1 (wa r' o) := by + intro o o1 o2 + have v : (wa r' o).val = r'.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + refine wk.mem_eq ?_ (T.reply_offWkA hp hQ hiT hr' (by omega) (by omega)) + show (wa r' o).val % 8 = 0 + omega + have n16 : (layout.reply.NEXT).val = 16 := by unfold layout.reply.NEXT; rfl + have p8 : (layout.reply.PREV).val = 8 := by unfold layout.reply.PREV; rfl + exact ⟨one _ (by omega) (by omega), one _ (by omega) (by omega)⟩ + have K : IpcKeepA σ1 σ' := by + refine ⟨fun v xq h => ?_, fun p0 y k h => ?_, fun p0 y' k' h => ?_⟩ + · obtain ⟨y, hy, f1, -, -, f4, f5, -, -⟩ := WK.tcb v xq h + obtain ⟨y2, hy2, m1, m2⟩ := NK.tcb h + rw [hy] at hy2 + cases hy2 + exact ⟨_, hy, f1, f4, f5, m1, m2.1, m2.2⟩ + · obtain ⟨z, hz, -, k2⟩ := NK.sc h + exact ⟨z, k, hz, k2⟩ + · obtain ⟨z, hz, -, k2⟩ := NK.sc_back h + exact ⟨z, k', hz, k2⟩ + have at_ : ∀ t ∈ Z.T, ∃ x, σ1.kheap (specPtr t) = some (.tcb x) := fun t ht => by + obtain ⟨x, hx, -⟩ := J1.ntfn.tcb t ht + exact ⟨x, hx⟩ + have bufCover : BufCover st Z.T Bf r.1 σ' := by + intro u hu y b sb v hy hb hl + obtain ⟨x, hx⟩ := at_ u hu + obtain ⟨y0, hy0, -, hfr, hbuf, -, -, -⟩ := K.tcb _ _ hx + rw [hy] at hy0 + cases hy0 + have hb0 : ipcBufOfA b x = some sb := by + unfold ipcBufOfA at hb ⊢ + rwa [hfr, hbuf] at hb + have hib : IbSame (L := L) s1 r.1 u := by + refine ⟨?_, ?_, ?_⟩ + · rw [ibWordA_eq]; exact tww u hu _ (by simp) (by simp) (by simp) + · rw [sbWordA_eq]; exact tww u hu _ (by simp) (by simp) (by simp) + · rw [sbWordA8_eq]; exact tww u hu _ (by simp) (by simp) (by simp) + rw [lookup_ipc_buffer_sameA hib] at hl + exact J1.bufCover u hu x b sb v hx hb0 hl + have bufAl : UtBufAlA Z.T σ' := fun t ht y hy => by + obtain ⟨x, hx⟩ := at_ t ht + obtain ⟨y0, hy0, -, -, hbuf, -, -, -⟩ := K.tcb _ _ hx + rw [hy] at hy0 + cases hy0 + rw [hbuf]; exact J1.bufAl t ht x hx + have idle : IdleNoFaultA m σ' := fun c hc => by + obtain ⟨x, hx, hf⟩ := J1.idleNoFault c hc + obtain ⟨y, hy, e, -⟩ := K.tcb _ _ hx + exact ⟨y, hy, e.trans hf⟩ + have mcp : McpRelA Z.T m (L.mem r.1) σ' := fun t ht => by + obtain ⟨x, hx, e, hf⟩ := J1.mcp t ht + obtain ⟨y, hy, -, -, -, hm, -, hfl⟩ := K.tcb _ _ hx + refine ⟨y, hy, ?_, fun g => (hf g).keep ?_ hfl⟩ + · unfold mcpA at e ⊢ + rw [tww t ht _ (by simp) (by simp) (by simp), hm] + exact e + · unfold flagsA + exact tww t ht _ (by simp) (by simp) (by simp) + have ye : ∀ t ∈ Z.T, L.mem r.1 (yieldA t) = L.mem s1 (yieldA t) := fun t ht => by + unfold yieldA; exact tww t ht _ (by simp) (by simp) (by simp) + have yr : YieldRelA Z.T Z.C m (L.mem r.1) σ' := by + refine ⟨fun t ht => ?_, fun t ht h => ?_, fun c hc y k h p hp => ?_⟩ + · obtain ⟨x, hx, e⟩ := J1.yieldRel.tcb t ht + obtain ⟨y, hy, -, -, -, -, hyt, -⟩ := K.tcb _ _ hx + exact ⟨y, hy, by rw [ye t ht, hyt]; exact e⟩ + · rw [ye t ht] at h ⊢ + obtain ⟨hC, y, k, hy, e⟩ := J1.yieldRel.toSc t ht h + obtain ⟨y', k', hy', e'⟩ := K.scF _ _ _ hy + exact ⟨hC, y', k', hy', e'.trans e⟩ + · obtain ⟨y0, k0, h0, e⟩ := K.scB _ _ _ h + obtain ⟨t, ht, e1, e2⟩ := J1.yieldRel.frm c hc y0 k0 h0 p (e ▸ hp) + exact ⟨t, ht, e1, by rw [ye t ht]; exact e2⟩ + -- the spec invariants + have tidle : specPtr u ≠ σ1.idleThread := fun c => by + obtain ⟨z, hz, hz'⟩ := J.sinv.idleTcb + rw [hit] at c + rw [← c, hkt] at hz + have := KernelObject.tcb.inj (Option.some.inj hz) + subst this + rw [hs] at hz' + cases hz' + have tidles : ∀ c < n, m.idleThreadOf c ≠ specPtr u := fun c hcn e0 => by + obtain ⟨z, hz, hz'⟩ := J.sidle.f3 c hcn + rw [e0, hkt] at hz + have := KernelObject.tcb.inj (Option.some.inj hz) + subst this + rw [hs] at hz' + cases hz' + have hsc : ∀ sc, x.ntfnSc = some sc → sc ≠ idleScPtr := fun sc h c => by + subst c + exact J1.sr.inv.ntfnIdle (specPtr p) x hk1p h + have hsuf1 : x.ntfnSc = some σ1.curSc → ∀ z k, σ1.kheap σ1.curSc = some (.schedContext z k) → z.scTcb = none → + scActive z = true → refillSufficient 0 (refillHd z) = true := fun e y k hy _ ha => by + obtain ⟨y0, n0, hy0⟩ := J.sinv.curSc + rw [hcs] at e hy + rw [hoth σ.curSc (fun c => by rw [c, hkt] at hy0; cases hy0) + (fun z c => by rw [c] at hy0; cases hy0) (fun z c => by rw [c] at hy0; cases hy0)] at hy + exact J.ntfnScSufA hk e hy ha + have hdom : x1.tcbDomain = σ1.curDomain := by + obtain ⟨x', hx', e0, -⟩ := J1.sr.rest.prio u hiT + rw [hk1] at hx' + have := KernelObject.tcb.inj (Option.some.inj hx') + subst this + rw [e0, J1.sr.rest.curDom.1] + have hLoop := SPairA.wake ⟨J1.sinv, J1.sidle⟩ hk1p hsc hk1 (by rw [hin]; rfl) + (cancelIpc_faultNoneA _ (by rw [hcan]; exact Set.mem_singleton _) x1 hk1) tidle tidles hdom + badge.bv hsuf1 hmw + have bq : BlkQueuedA Z.T E N (L.mem r.1) σ' := by + refine ⟨fun v hv y h e hbk => ?_, fun v hv y h q' hbk => ?_⟩ + · obtain ⟨x0, hx0⟩ := at_ v hv + obtain ⟨y0, hy0, -, -, -, -, -, hne0, heq⟩ := WK.tcb _ x0 hx0 + rw [h] at hy0 + cases hy0 + by_cases ed : specPtr v = specPtr u + · rw [(heq ed).2] at hbk + simp [Fl4SpecA64.epBlocked] at hbk + · have hst := (hne0 ed).2 + obtain ⟨c, z, hz, hmem⟩ := J1.blkQ.ep v hv x0 hx0 e (by rw [← hst]; exact hbk) + refine ⟨by rw [Notif.tBlockA_eq, tww v hv 3336#u64 (by simp) (by simp) (by simp), + ← Notif.tBlockA_eq]; exact c, z, WK.ep _ _ hz, hmem⟩ + · obtain ⟨x0, hx0⟩ := at_ v hv + obtain ⟨y0, hy0, -, -, -, -, -, hne0, heq⟩ := WK.tcb _ x0 hx0 + rw [h] at hy0 + cases hy0 + by_cases ed : specPtr v = specPtr u + · rw [(heq ed).2] at hbk + cases hbk + · have hst := (hne0 ed).2 + obtain ⟨c, z, l, hz, hl, hmem⟩ := J1.blkQ.ntfn v hv x0 hx0 q' (by rw [← hst]; exact hbk) + have tb : L.mem r.1 (Notif.tBlockA v) = L.mem s1 (Notif.tBlockA v) := by + rw [Notif.tBlockA_eq]; exact tww v hv 3336#u64 (by simp) (by simp) (by simp) + have eqp : q' ≠ specPtr p := fun c0 => by + rw [c0, hk1p] at hz + cases hz + rw [hidle] at hl + cases hl + have hnt : q' ≠ specPtr u := fun c0 => by rw [c0, hk1] at hz; cases hz + exact ⟨by rw [tb]; exact c, z, l, + wake_kept hk1p hne h3 hW eqp hnt hz (fun _ e => by cases e) (fun _ _ e => by cases e), hl, hmem⟩ + have sv : SchedValidA σ' := by + have hI := SchedValidA.toFIA J.schedValid + have hid : IdleSt σ := fun x0 hx0 => by + obtain ⟨x2, hx2, hs2⟩ := J.sinv.idleTcb + rw [hx2] at hx0 + cases hx0 + exact hs2 + have := sendSignal_fia (specPtr p) badge.bv σ σ ⟨rfl, hI, J.rqDomPrio, hid, J.sigQ hp⟩ ((), σ') hm0 + exact SchedValidB.toA this.1.1 this.1.2 + refine ⟨Q', R', ?_⟩ + exact J.of_stepFinA (Q := Q') (Rl := R') (curOf := Z.curOf) (curScOf := Z.curScOf) w F.1 F.2.1 (Qk1.trans QK) F.2.2 + (fun _ _ => rfl) (BlkOK.bxi blk') sysl symL userOK j7 H3 ep Rn reply bufCover bufAl idle mcp yr hLoop.sinv + hLoop.sidle (J.tcbs ▸ rv') bq sv + +end recB + +section armA +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {ar : machine.Arch A} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +set_option maxHeartbeats 4000000 in +/-- **Notification send to an idle notification with a receive-blocked bound thread (with or without a reply object), +at a syscall entry** (module docstring). -/ +theorem JSyscallA.signalBoundRecvA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hn : fl4A64Platform.numNodes ≤ n) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p badge slot lbl len buf : U64} {recv blk call don fp : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Notification p badge true recv) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hp : p ∈ N) {x : Notification} (hk : σ.kheap (specPtr p) = some (.notification x)) + (hidle : x.ntfnObj = .idleNtfn) {t : ObjRef} (hb : x.ntfnBoundTcb = some t) {xt : Tcb} + (hkt : σ.kheap t = some (.tcb xt)) {e' : ObjRef} {ro : Option ObjRef} {d : ReceiverPayload} + (hs : xt.tcbState = .blockedOnReceive e' ro d) : + ∃ xc σ1 σ2 s2, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ((), σ2) ∈ (sendSignal (Z := Unit) (specPtr p) badge.bv σ1).1 ∧ + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don fp buf extra = + ok (.Ok (), s2, a) ∧ + (FIA σ1 ∧ RqDomPrio σ1 ∧ IdleSt σ1 ∧ SigQ (specPtr p) σ1) ∧ + ∃ Q Rl, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s2 a σ2 m := by + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hslot hcov J.actW J.running J.cur + have hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM := J.toJStage3EntryA.curNotVmA + obtain ⟨xc, hkc, hst, hsp, J1, -, -, -⟩ := J.toJStage3A.of_restartCurA RS.pl.choose_spec.choose_spec.choose_spec.choose_spec + RS.sym RS.run hnvm RS.cur RS.vs RS.vc + set s1 := L.wr s (stA (L.curW s)) sched.TS_RESTART with hs1 + set σ1 := stUpdA σ σ.curThread xc .restart with hσ1 + have ne1 : specPtr p ≠ σ.curThread := fun e => by rw [e, hkc] at hk; cases hk + have hk1 : σ1.kheap (specPtr p) = some (.notification x) := by + rw [hσ1]; simp only [stUpdA, setObjA, Function.update_of_ne ne1]; exact hk + have hrun : runnable xc.tcbState = true := by + have ct : σ.curThread = specPtr (L.curW s) := J.j.1.sch.curThread + have states : StEqA Z.T (L.mem s) σ := fun u hu y hy => (J.ep.at_tcb hu hy).st + exact (runnable_iffA (states _ J.sr.curT xc (ct ▸ hkc))).1 J.running + have ne2 : t ≠ σ.curThread := fun e => by + rw [e, hkc] at hkt + cases hkt + rw [hs] at hrun + cases hrun + have hkt1 : σ1.kheap t = some (.tcb xt) := by + rw [hσ1]; simp only [stUpdA, setObjA, Function.update_of_ne ne2]; exact hkt + have body := J1.sigBoundRecv_stepA hA hCs h1 hmb hmax htu hp hk1 hidle hb hkt1 hs badge + obtain ⟨⟨s2, a2⟩, eb, ha2, σ2, hs2, blk', rv', -, -, K⟩ := exists_of_specA body + have hm2 : ((), σ2) ∈ (sendSignal (Z := Unit) (specPtr p) badge.bv σ1).1 := by + rw [hs2]; exact Set.mem_singleton _ + simp only at ha2 eb + have ha2' : a = a2 := ha2.symm + subst ha2' + have hdec : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don fp buf extra = + ok (.Ok (), s2, a) := by + rw [hcap, decodeNotification_programA] + simp only [↓reduceIte, ipcInvokeProgramA, cur_thread_ok, bind_ok, hst] + rw [show notification.send_signal st ar (L.wr s (stA (L.curW s)) sched.TS_RESTART) a p badge = ok (s2, a) + from eb] + simp only [bind_ok] + rfl + have F := J.decodeFinA hA hM hPl hfm hslot hcov (rfl : capA st s slot = capA st s slot) hext hSC (lbl := lbl) + (by rw [hcap]; simp [DecArmS1aEnt, DecArmQH, DecArmQZ2, DecArmKQ2, DecArmKQ, capFam]) + (fun _ h => by rw [hcap] at h; cases h) (fun _ h => by rw [hcap] at h; cases h) hdec σ2 m + obtain ⟨-, -, sysl, symL, -, userOK, j7⟩ := F + obtain ⟨Q, Rl, J2⟩ := K sysl symL userOK j7 + have hid1 : IdleSt σ1 := fun x0 hx0 => by + obtain ⟨x1, hx1, hs1⟩ := J1.sinv.idleTcb + rw [hx1] at hx0; cases hx0; exact hs1 + exact ⟨xc, σ1, σ2, s2, hkc, rfl, hsp, hm2, hdec, + ⟨Sched.SchedValidA.toFIA J1.schedValid, J1.rqDomPrio, hid1, J1.sigQ hp⟩, Q, Rl, J2⟩ + +end armA + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3SigBudgetA.lean b/proofs/Fl4ProofsA64/P3/Stage3SigBudgetA.lean new file mode 100644 index 000000000..e6e2f0ba7 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SigBudgetA.lean @@ -0,0 +1,453 @@ +/- +P3 (aarch64, A64-P3 Stage 3, kernel-entry composition, Plan A): **`send_signal` keeps the current budget**. + +`BKeepA σ σ'`: the current thread and SC, the clock, the consumed time and the machine state are kept, a resuming +state came from a resuming state, and the current SC keeps its refills and refill maximum. Every `send_signal` has it: +its only refill write on another path is `refill_unblock_check` guarded to an SC that is not the current one +(`ifSporadicTest_bkA`). + +Generated by `handoff/tools/gen_kentry_bkeep.py` (a twin of the instance lists of `P3/SpecFrameCalcA`, +`P3/SpecFrameSchedA`, `P3/SpecFrameIpcA`; the leaves are written out). No premise; new module. +-/ +import Fl4ProofsA64.P3.SpecFrameIpcA +import Fl4ProofsA64.P3.Stage3SigSchedValidA +import Fl4ProofsA64.P3.ScScheduleA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec Classical + +noncomputable section + +/-- The scalars of the budget frame. -/ +structure BScalA (σ σ' : State Unit) : Prop where + ct : σ'.curThread = σ.curThread + csc : σ'.curSc = σ.curSc + time : σ'.curTime = σ.curTime + used : σ'.consumedTime = σ.consumedTime + ms : σ'.machineState = σ.machineState + idle : σ'.idleThread = σ.idleThread + act : σ'.schedulerAction = .resumeCurThread → σ.schedulerAction = .resumeCurThread + +theorem BScalA.refl (σ : State Unit) : BScalA σ σ := ⟨rfl, rfl, rfl, rfl, rfl, rfl, fun h => h⟩ + +theorem BScalA.trans {a b c : State Unit} (h1 : BScalA a b) (h2 : BScalA b c) : BScalA a c := + ⟨h2.ct.trans h1.ct, h2.csc.trans h1.csc, h2.time.trans h1.time, h2.used.trans h1.used, h2.ms.trans h1.ms, + h2.idle.trans h1.idle, + fun h => h1.act (h2.act h)⟩ + +/-- The budget frame (module docstring). -/ +structure BKeepA (σ σ' : State Unit) : Prop extends BScalA σ σ' where + sc : ∀ y' n, σ'.kheap σ.curSc = some (.schedContext y' n) → ∃ y, σ.kheap σ.curSc = some (.schedContext y n) ∧ + y'.scRefills = y.scRefills ∧ y'.scRefillMax = y.scRefillMax + +instance : PreO BKeepA where + refl σ := ⟨BScalA.refl σ, fun y _ h => ⟨y, h, rfl, rfl⟩⟩ + trans h1 h2 := ⟨h1.toBScalA.trans h2.toBScalA, fun y'' n h => by + obtain ⟨y', h', a', b'⟩ := h2.sc y'' n (by rw [h1.csc]; exact h) + rw [h1.csc] at h' + obtain ⟨y, h0, a, b⟩ := h1.sc y' n h' + exact ⟨y, h0, a'.trans a, b'.trans b⟩⟩ + +theorem BKeepA.of_kheap {σ σ' : State Unit} (s : BScalA σ σ') (hk : σ'.kheap = σ.kheap) : BKeepA σ σ' := + ⟨s, fun y _ h => ⟨y, by rw [← hk]; exact h, rfl, rfl⟩⟩ + +/-- One object replaced: not by an SC, or an SC by one of the same size, refills and refill maximum. -/ +theorem BKeepA.upd {σ : State Unit} {p : ObjRef} {o : KernelObject} + (h : ∀ y' n, o = .schedContext y' n → ∃ y, σ.kheap p = some (.schedContext y n) ∧ + y'.scRefills = y.scRefills ∧ y'.scRefillMax = y.scRefillMax) : + BKeepA σ { σ with kheap := Function.update σ.kheap p (some o) } := by + refine ⟨⟨rfl, rfl, rfl, rfl, rfl, rfl, fun h => h⟩, fun y' n hq => ?_⟩ + by_cases c : σ.curSc = p + · simp only [c, Function.update_self, Option.some.injEq] at hq + rw [c] + exact h y' n hq + · simp only [Function.update_of_ne c] at hq + exact ⟨y', hq, rfl, rfl⟩ + +abbrev KPresB {α : Type} (m : SMonad Unit α) : Prop := PresR BKeepA m + +class KPresBInst {α : Type} (m : SMonad Unit α) : Prop where + pres : KPresB m + +class ScBudFn (f : SchedContext → SchedContext) : Prop where + keep : ∀ y, (f y).scRefills = y.scRefills ∧ (f y).scRefillMax = y.scRefillMax + +instance (g : Option ObjRef → Option ObjRef) : ScBudFn (scTcbUpdate g) := ⟨fun _ => ⟨rfl, rfl⟩⟩ +instance (g : Option ObjRef → Option ObjRef) : ScBudFn (scNtfnUpdate g) := ⟨fun _ => ⟨rfl, rfl⟩⟩ +instance (g : Option ObjRef → Option ObjRef) : ScBudFn (scYieldFromUpdate g) := ⟨fun _ => ⟨rfl, rfl⟩⟩ +instance (g : List ObjRef → List ObjRef) : ScBudFn (scRepliesUpdate g) := ⟨fun _ => ⟨rfl, rfl⟩⟩ + +instance {α β : Type} {m : SMonad Unit α} {f : α → SMonad Unit β} [hm : KPresBInst m] + [hf : ∀ a, KPresBInst (f a)] : KPresBInst (m >>= f) := + ⟨PresR.bind hm.pres fun a => (hf a).pres⟩ + +instance {α : Type} (a : α) : KPresBInst (pure a : SMonad Unit α) := ⟨PresR.pure a⟩ + +instance {α : Type} (f : State Unit → α) : KPresBInst (NonDet.gets f : SMonad Unit α) := ⟨PresR.gets f⟩ + +instance : KPresBInst (NonDet.get : SMonad Unit (State Unit)) := ⟨PresR.get⟩ + +instance {α : Type} (f : State Unit → Option α) : KPresBInst (NonDet.getsThe f : SMonad Unit α) := + ⟨PresR.getsThe f⟩ + +instance {α : Type} (o : Option α) : KPresBInst (NonDet.assertOpt o : SMonad Unit α) := ⟨PresR.assertOpt o⟩ + +instance (P : Prop) : KPresBInst (NonDet.assert P : SMonad Unit Unit) := ⟨PresR.assert P⟩ + +instance {α : Type} : KPresBInst (NonDet.fail : SMonad Unit α) := ⟨PresR.fail⟩ + +instance {P : Prop} {d : Decidable P} {m : SMonad Unit Unit} [h : KPresBInst m] : + KPresBInst (@NonDet.when (State Unit) P d m) := ⟨PresR.when h.pres⟩ + +instance {P : Prop} {d : Decidable P} {m : SMonad Unit Unit} [h : KPresBInst m] : + KPresBInst (@NonDet.unless (State Unit) P d m) := ⟨PresR.unless' h.pres⟩ + +instance {α : Type} {P : Prop} {d : Decidable P} {a b : SMonad Unit α} [ha : KPresBInst a] [hb : KPresBInst b] : + KPresBInst (@ite _ P d a b) := ⟨PresR.ite' ha.pres hb.pres⟩ + +instance {β γ : Type} {f : γ → SMonad Unit β} [hf : ∀ a, KPresBInst (f a)] (l : List γ) : + KPresBInst (NonDet.mapM f l) := ⟨PresR.mapM (fun a => (hf a).pres) l⟩ + +instance {α : Type} {C : α → State Unit → Prop} {B : α → SMonad Unit α} [hB : ∀ a, KPresBInst (B a)] (r : α) : + KPresBInst (NonDet.whileLoop C B r) := ⟨PresR.whileLoop (fun a => (hB a).pres) r⟩ + +instance {α : Type} (S : Set α × Prop) : KPresBInst (NonDet.selectF S : SMonad Unit α) := ⟨PresR.selectF S⟩ + +instance {α : Type} {f : α → SMonad Unit Unit} [hf : ∀ a, KPresBInst (f a)] (o : Option α) : + KPresBInst (NonDet.maybeM f o) := by + cases o + · exact ⟨PresR.pure _⟩ + · exact hf _ + + +/-- A state modification that keeps the heap and the scalars. -/ +theorem KPresB.modify_fix (f : State Unit → State Unit) (s : ∀ σ, BScalA σ (f σ)) + (hk : ∀ σ, (f σ).kheap = σ.kheap) : KPresB (NonDet.modify f : SMonad Unit Unit) := + PresR.modify f fun σ => BKeepA.of_kheap (s σ) (hk σ) + +macro "kbres" : tactic => `(tactic| + repeat' (first + | exact KPresBInst.pres + | refine PresR.bind_pure _ _ ?_ + | refine PresR.bind ?_ (fun _ => ?_) + | refine PresR.when ?_ + | refine PresR.unless' ?_ + | refine PresR.ite' ?_ ?_ + | (refine KPresB.modify_fix _ (fun _ => ⟨rfl, rfl, rfl, rfl, rfl, rfl, fun h => h⟩) (fun _ => rfl)) + | dsimp only + | split)) + +/-! ## The writing primitives -/ + +theorem threadSet_bkA (f : Tcb → Tcb) (t : ObjRef) : KPresB (threadSet (Z := Unit) f t) := by + intro σ r hr + obtain ⟨y1, hy1, h⟩ := hr + have hy1' : y1 ∈ (NonDet.assertOpt (getTcb (Z := Unit) t σ) σ).1 := hy1 + cases hg : getTcb (Z := Unit) t σ with + | none => rw [hg] at hy1'; exact absurd hy1' (Set.notMem_empty _) + | some x => + rw [hg] at hy1' + have e1 : y1 = (x, σ) := hy1' + subst e1 + obtain ⟨ko, hko, hr2⟩ := setObject_memA h + rw [hr2] + exact BKeepA.upd (fun _ _ e => by cases e) + +theorem asUser_bkA {α : Type} (t : ObjRef) (f : UserMonad α) : KPresB (asUser (Z := Unit) t f) := by + intro σ r hr + obtain ⟨y1, hy1, hr⟩ := hr + have hy1' : y1 ∈ (NonDet.assertOpt (getTcb (Z := Unit) t σ) σ).1 := hy1 + cases hg : getTcb (Z := Unit) t σ with + | none => rw [hg] at hy1'; exact absurd hy1' (Set.notMem_empty _) + | some x => + rw [hg] at hy1' + have e1 : y1 = (x, σ) := hy1' + subst e1 + obtain ⟨y2, hy2, hr⟩ := hr + have e2 : y2 = (archTcbContextGet x.tcbArch, σ) := hy2 + subst e2 + obtain ⟨⟨⟨a, uc'⟩, s3⟩, hy3, hr⟩ := hr + have e3 : s3 = σ := hy3.2 + subst e3 + obtain ⟨y4, hy4, hr⟩ := hr + have e4 : y4 = ({ x with tcbArch := archTcbContextSet uc' x.tcbArch }, s3) := hy4 + subst e4 + obtain ⟨y5, hy5, hr⟩ := hr + obtain ⟨ko, hko, hr5⟩ := setObject_memA hy5 + have e6 : r = (a, y5.2) := hr + rw [e6, hr5] + exact BKeepA.upd (fun _ _ e => by cases e) + +theorem updateSchedContext_bkA (f : SchedContext → SchedContext) [ScBudFn f] (p : ObjRef) : + KPresB (updateSchedContext (Z := Unit) p f) := by + intro σ r hr + obtain ⟨y1, hy1, h⟩ := hr + have hy1' : y1 ∈ (NonDet.assertOpt (σ.kheap p) σ).1 := hy1 + cases hk : σ.kheap p with + | none => rw [hk] at hy1'; exact absurd hy1' (Set.notMem_empty _) + | some ko => + rw [hk] at hy1' + have e1 : y1 = (ko, σ) := hy1' + subst e1 + cases ko with + | schedContext sc n => + obtain ⟨ko', hko, hr2⟩ := setObject_memA h + rw [hr2] + refine BKeepA.upd (fun y' k e => ?_) + injection e with e1 e2 + subst e1 e2 + exact ⟨sc, hk, ScBudFn.keep sc⟩ + | _ => exact absurd h (Set.notMem_empty _) + +theorem setSimpleKo_bkA {α : Type} [Nonempty α] (C : α → KernelObject) [SimpleCtorA C] (ptr : ObjRef) (a : α) : + KPresB (setSimpleKo (Z := Unit) C ptr a) := by + intro σ r hr + obtain ⟨y, hk, hr2⟩ := setSimpleKo_memA C ptr a hr + rw [hr2] + exact BKeepA.upd (fun y' n e => absurd e (SimpleCtorA.nosc a y' n)) + +theorem setSchedulerAction_bkA (a : SchedulerAction) (h : a ≠ .resumeCurThread) : + KPresB (setSchedulerAction (Z := Unit) a) := by + unfold setSchedulerAction + exact PresR.modify _ fun σ => BKeepA.of_kheap ⟨rfl, rfl, rfl, rfl, rfl, rfl, fun e => absurd e h⟩ rfl + +instance : KPresBInst (setSchedulerAction (Z := Unit) .chooseNewThread) := + ⟨setSchedulerAction_bkA _ (by intro e; cases e)⟩ +instance (t : ObjRef) : KPresBInst (setSchedulerAction (Z := Unit) (.switchThread t)) := + ⟨setSchedulerAction_bkA _ (by intro e; cases e)⟩ +instance (f : Tcb → Tcb) (t : ObjRef) : KPresBInst (threadSet (Z := Unit) f t) := ⟨threadSet_bkA f t⟩ +instance (p : ObjRef) (f : SchedContext → SchedContext) [ScBudFn f] : KPresBInst (updateSchedContext (Z := Unit) p f) := + ⟨updateSchedContext_bkA f p⟩ +instance {α : Type} (t : ObjRef) (f : UserMonad α) : KPresBInst (asUser (Z := Unit) t f) := ⟨asUser_bkA t f⟩ +instance {α : Type} (a : α) : KPresBInst (NonDet.pure a : SMonad Unit α) := ⟨PresR.pure a⟩ +instance (q : ObjRef) : KPresBInst (getObject (Z := Unit) q) := ⟨by unfold getObject; kbres⟩ +instance {α : Type} [Nonempty α] (C : α → KernelObject) [SimpleCtorA C] (ptr : ObjRef) (a : α) : + KPresBInst (setSimpleKo (Z := Unit) C ptr a) := ⟨setSimpleKo_bkA C ptr a⟩ +instance {α : Type} [Nonempty α] (C : α → KernelObject) (ptr : ObjRef) : + KPresBInst (getSimpleKo (Z := Unit) C ptr) := ⟨by unfold getSimpleKo; kbres⟩ +instance {α : Type} [Nonempty α] (C : α → KernelObject) (f : α → Option ObjRef) (ptr : ObjRef) : + KPresBInst (getSkObjRef (Z := Unit) C f ptr) := ⟨by unfold getSkObjRef; kbres⟩ +instance {α : Type} [Nonempty α] (C : α → KernelObject) [SimpleCtorA C] + (f : (Option ObjRef → Option ObjRef) → α → α) (ref : ObjRef) (new : Option ObjRef) : + KPresBInst (updateSkObjRef (Z := Unit) C f ref new) := ⟨by unfold updateSkObjRef; kbres⟩ + +/-! ## The guarded refill check -/ + +/-- Only the SC at `p` and the reprogram flag change. -/ +structure OnlyScA (p : ObjRef) (σ σ' : State Unit) : Prop extends BScalA σ σ' where + other : ∀ q, q ≠ p → σ'.kheap q = σ.kheap q + +instance (p : ObjRef) : PreO (OnlyScA p) where + refl σ := ⟨BScalA.refl σ, fun _ _ => rfl⟩ + trans h1 h2 := ⟨h1.toBScalA.trans h2.toBScalA, fun q h => (h2.other q h).trans (h1.other q h)⟩ + +class OPresInst {α : Type} (p : ObjRef) (m : SMonad Unit α) : Prop where + pres : PresR (OnlyScA p) m + +theorem OnlyScA.bkeep {p : ObjRef} {σ σ' : State Unit} (h : OnlyScA p σ σ') (ne : p ≠ σ.curSc) : BKeepA σ σ' := + ⟨h.toBScalA, fun y n hq => ⟨y, by rw [← h.other _ (Ne.symm ne)]; exact hq, rfl, rfl⟩⟩ + +section onlyS +variable {p : ObjRef} + +instance (priority := 900) {α β : Type} {m : SMonad Unit α} {f : α → SMonad Unit β} [hm : OPresInst p m] + [hf : ∀ a, OPresInst p (f a)] : OPresInst p (m >>= f) := + ⟨PresR.bind hm.pres fun a => (hf a).pres⟩ + +instance (priority := 900) {α : Type} (a : α) : OPresInst p (pure a : SMonad Unit α) := ⟨PresR.pure a⟩ + +instance (priority := 900) {α : Type} (f : State Unit → α) : OPresInst p (NonDet.gets f : SMonad Unit α) := ⟨PresR.gets f⟩ + +instance : OPresInst p (NonDet.get : SMonad Unit (State Unit)) := ⟨PresR.get⟩ + +instance (priority := 900) {α : Type} (f : State Unit → Option α) : OPresInst p (NonDet.getsThe f : SMonad Unit α) := + ⟨PresR.getsThe f⟩ + +instance (priority := 900) {α : Type} (o : Option α) : OPresInst p (NonDet.assertOpt o : SMonad Unit α) := ⟨PresR.assertOpt o⟩ + +instance (priority := 900) (P : Prop) : OPresInst p (NonDet.assert P : SMonad Unit Unit) := ⟨PresR.assert P⟩ + +instance (priority := 900) {α : Type} : OPresInst p (NonDet.fail : SMonad Unit α) := ⟨PresR.fail⟩ + +instance (priority := 900) {P : Prop} {d : Decidable P} {m : SMonad Unit Unit} [h : OPresInst p m] : + OPresInst p (@NonDet.when (State Unit) P d m) := ⟨PresR.when h.pres⟩ + +instance (priority := 900) {P : Prop} {d : Decidable P} {m : SMonad Unit Unit} [h : OPresInst p m] : + OPresInst p (@NonDet.unless (State Unit) P d m) := ⟨PresR.unless' h.pres⟩ + +instance (priority := 900) {α : Type} {P : Prop} {d : Decidable P} {a b : SMonad Unit α} [ha : OPresInst p a] [hb : OPresInst p b] : + OPresInst p (@ite _ P d a b) := ⟨PresR.ite' ha.pres hb.pres⟩ + +instance (priority := 900) {β γ : Type} {f : γ → SMonad Unit β} [hf : ∀ a, OPresInst p (f a)] (l : List γ) : + OPresInst p (NonDet.mapM f l) := ⟨PresR.mapM (fun a => (hf a).pres) l⟩ + +instance (priority := 900) {α : Type} {C : α → State Unit → Prop} {B : α → SMonad Unit α} [hB : ∀ a, OPresInst p (B a)] (r : α) : + OPresInst p (NonDet.whileLoop C B r) := ⟨PresR.whileLoop (fun a => (hB a).pres) r⟩ + +instance (priority := 900) {α : Type} (S : Set α × Prop) : OPresInst p (NonDet.selectF S : SMonad Unit α) := ⟨PresR.selectF S⟩ + +instance (priority := 900) {α : Type} {f : α → SMonad Unit Unit} [hf : ∀ a, OPresInst p (f a)] (o : Option α) : + OPresInst p (NonDet.maybeM f o) := by + cases o + · exact ⟨PresR.pure _⟩ + · exact hf _ + + +macro "opres" : tactic => `(tactic| + repeat' (first + | exact OPresInst.pres + | refine PresR.bind_pure _ _ ?_ + | refine PresR.bind ?_ (fun _ => ?_) + | refine PresR.when ?_ + | refine PresR.unless' ?_ + | refine PresR.ite' ?_ ?_ + | (refine PresR.modify _ (fun _ => ⟨⟨rfl, rfl, rfl, rfl, rfl, rfl, fun h => h⟩, fun _ _ => rfl⟩)) + | dsimp only + | split)) + +theorem updateSchedContext_onlyA (f : SchedContext → SchedContext) : PresR (OnlyScA p) (updateSchedContext (Z := Unit) p f) := by + intro σ r hr + obtain ⟨y1, hy1, h⟩ := hr + have hy1' : y1 ∈ (NonDet.assertOpt (σ.kheap p) σ).1 := hy1 + cases hk : σ.kheap p with + | none => rw [hk] at hy1'; exact absurd hy1' (Set.notMem_empty _) + | some ko => + rw [hk] at hy1' + have e1 : y1 = (ko, σ) := hy1' + subst e1 + cases ko with + | schedContext sc n => + obtain ⟨ko', hko, hr2⟩ := setObject_memA h + rw [hr2] + exact ⟨⟨rfl, rfl, rfl, rfl, rfl, rfl, fun h => h⟩, fun q e => Function.update_of_ne e _ _⟩ + | _ => exact absurd h (Set.notMem_empty _) + +instance (f : SchedContext → SchedContext) : OPresInst p (updateSchedContext (Z := Unit) p f) := + ⟨updateSchedContext_onlyA f⟩ +instance (q : ObjRef) : OPresInst p (getObject (Z := Unit) q) := ⟨by unfold getObject; opres⟩ +instance (q : ObjRef) : OPresInst p (getSchedContext (Z := Unit) q) := ⟨by unfold getSchedContext; opres⟩ +instance (q : ObjRef) : OPresInst p (getRefills (Z := Unit) q) := ⟨by unfold getRefills; opres⟩ +instance (q : ObjRef) : OPresInst p (getRefillHead (Z := Unit) q) := ⟨by unfold getRefillHead; opres⟩ +instance (q : ObjRef) : OPresInst p (getScRefillReady (Z := Unit) q) := ⟨by unfold getScRefillReady; opres⟩ +instance (q : ObjRef) : OPresInst p (isRoundRobin (Z := Unit) q) := ⟨by unfold isRoundRobin; opres⟩ +instance (f : Refill → Refill) : OPresInst p (updateRefillHd (Z := Unit) p f) := ⟨by unfold updateRefillHd; opres⟩ +instance : OPresInst p (refillPopHead (Z := Unit) p) := ⟨by unfold refillPopHead; opres⟩ +instance : OPresInst p (mergeOverlappingRefills (Z := Unit) p) := ⟨by unfold mergeOverlappingRefills; opres⟩ +instance : OPresInst p (refillHeadOverlappingLoop (Z := Unit) p) := ⟨by unfold refillHeadOverlappingLoop; opres⟩ + +theorem refillUnblockCheck_onlyA : PresR (OnlyScA p) (refillUnblockCheck (Z := Unit) p) := by + unfold refillUnblockCheck; opres + +end onlyS + +/-- The sporadic check of a signal keeps the budget frame: it is guarded to an SC that is not the current one. -/ +theorem ifSporadicTest_bkA (o : Option ObjRef) : + KPresB (ifCondRefillUnblockCheck (Z := Unit) o (some false) (some false)) := by + unfold ifCondRefillUnblockCheck + cases o with + | none => exact PresR.pure _ + | some p => + intro σ r hr + simp only [NonDet.maybeM] at hr + obtain ⟨⟨sc, σ1⟩, h1, hr⟩ := mem_bindA.1 hr + have e1 : σ1 = σ := (getSchedContext_mem h1).1 + rw [e1] at hr + dsimp only at hr + rw [nd_gets_bindSc, nd_pure_bindSc] at hr + try dsimp only at hr + unfold NonDet.when at hr + split at hr + · rename_i c + have ne : p ≠ σ.curSc := c.2 rfl + have h : PresR (OnlyScA p) (NonDet.when (some false = some true → p ≠ σ.curSc) + (refillUnblockCheck (Z := Unit) p)) := PresR.when refillUnblockCheck_onlyA + unfold NonDet.when at h + exact (h σ r hr).bkeep ne + · have : r = ((), σ) := hr + rw [this]; exact PreO.refl _ + +instance (o : Option ObjRef) : KPresBInst (ifCondRefillUnblockCheck (Z := Unit) o (some false) (some false)) := + ⟨ifSporadicTest_bkA o⟩ + +/-! ## Readers and small writers -/ + +instance {α : Type} (f : Tcb → α) (t : ObjRef) : KPresBInst (threadGet (Z := Unit) f t) := + ⟨by unfold threadGet; kbres⟩ +instance (f : Tcb → Option ObjRef) (r : ObjRef) : KPresBInst (getTcbObjRef (Z := Unit) f r) := + ⟨by unfold getTcbObjRef; kbres⟩ +instance (r : ObjRef) : KPresBInst (getThreadState (Z := Unit) r) := ⟨by unfold getThreadState; kbres⟩ +instance (d : Domain) (p : Priority) : KPresBInst (getTcbQueue (Z := Unit) d p) := ⟨by unfold getTcbQueue; kbres⟩ +instance (d : Domain) (p : Priority) (q : ReadyQueue) : KPresBInst (setTcbQueue (Z := Unit) d p q) := + ⟨by unfold setTcbQueue; kbres⟩ +instance (a : ObjRef → List ObjRef → List ObjRef) (t : ObjRef) : KPresBInst (tcbSchedAction (Z := Unit) a t) := + ⟨by unfold tcbSchedAction; kbres⟩ +instance (t : ObjRef) : KPresBInst (tcbReleaseRemove (Z := Unit) t) := ⟨by unfold tcbReleaseRemove; kbres⟩ + +instance (p : ObjRef) : KPresBInst (getObject (Z := Unit) p) := ⟨by unfold getObject; kbres⟩ +instance (p : ObjRef) : KPresBInst (getSchedContext (Z := Unit) p) := ⟨by unfold getSchedContext; kbres⟩ +instance (f : SchedContext → Option ObjRef) (p : ObjRef) : KPresBInst (getScObjRef (Z := Unit) f p) := + ⟨by unfold getScObjRef; kbres⟩ +instance (p : ObjRef) : KPresBInst (getRefills (Z := Unit) p) := ⟨by unfold getRefills; kbres⟩ +instance (p : ObjRef) : KPresBInst (getRefillHead (Z := Unit) p) := ⟨by unfold getRefillHead; kbres⟩ +instance (p : ObjRef) : KPresBInst (getRefillTail (Z := Unit) p) := ⟨by unfold getRefillTail; kbres⟩ +instance (p : ObjRef) : KPresBInst (getScRefillReady (Z := Unit) p) := ⟨by unfold getScRefillReady; kbres⟩ +instance (p : ObjRef) (u : Ticks) : KPresBInst (getScRefillSufficient (Z := Unit) p u) := + ⟨by unfold getScRefillSufficient; kbres⟩ +instance (p : ObjRef) (u : Ticks) : KPresBInst (fl4GetScRefillSufficient (Z := Unit) p u) := + ⟨by unfold fl4GetScRefillSufficient; kbres⟩ +instance (p : ObjRef) : KPresBInst (getScActive (Z := Unit) p) := ⟨by unfold getScActive; kbres⟩ +instance (p : ObjRef) : KPresBInst (isRoundRobin (Z := Unit) p) := ⟨by unfold isRoundRobin; kbres⟩ + +/-! ## Scheduler queues -/ + +instance : KPresBInst (rescheduleRequired (Z := Unit)) := ⟨by unfold rescheduleRequired; kbres⟩ +instance (t : ObjRef) : KPresBInst (possibleSwitchTo (Z := Unit) t) := ⟨by unfold possibleSwitchTo; kbres⟩ + + + +/-! ## Queue operations -/ + +instance {α β : Type} [Nonempty β] (t : α) (ts : List α) (f : α → RMonad Unit β) (R : β → β → Prop) : + KPresBInst (orderedInsert (Z := Unit) t ts f R) := ⟨by unfold orderedInsert; kbres⟩ +instance (t : ObjRef) (qs : List ObjRef) : KPresBInst (tcbAppend (Z := Unit) t qs) := ⟨by unfold tcbAppend; kbres⟩ +instance (t e : ObjRef) : KPresBInst (tcbEpDequeue (Z := Unit) t e) := ⟨by unfold tcbEpDequeue; kbres⟩ +instance (t e : ObjRef) : KPresBInst (tcbNtfnDequeue (Z := Unit) t e) := ⟨by unfold tcbNtfnDequeue; kbres⟩ +instance (t e : ObjRef) (b : Bool) : KPresBInst (tcbEpAppend (Z := Unit) t e b) := ⟨by unfold tcbEpAppend; kbres⟩ +instance (t e : ObjRef) : KPresBInst (tcbNtfnAppend (Z := Unit) t e) := ⟨by unfold tcbNtfnAppend; kbres⟩ + +/-! ## Scheduling-context donation and replies -/ + +instance (t : ObjRef) : KPresBInst (tcbReleaseEnqueue (Z := Unit) t) := ⟨by unfold tcbReleaseEnqueue; kbres⟩ +instance (p : ObjRef) : KPresBInst (postpone (Z := Unit) p) := ⟨by unfold postpone; kbres⟩ +instance (p : ObjRef) : KPresBInst (schedContextResume (Z := Unit) p) := ⟨by unfold schedContextResume; kbres⟩ +instance (t : ObjRef) : KPresBInst (testReschedule (Z := Unit) t) := ⟨by unfold testReschedule; kbres⟩ +instance (sc t : ObjRef) : KPresBInst (schedContextDonate (Z := Unit) sc t) := + ⟨by unfold schedContextDonate; kbres⟩ +instance (t n : ObjRef) : KPresBInst (maybeDonateSc (Z := Unit) t n) := ⟨by unfold maybeDonateSc; kbres⟩ +instance (n t : ObjRef) : KPresBInst (maybeReturnSc (Z := Unit) n t) := ⟨by unfold maybeReturnSc; kbres⟩ +instance (sc r : ObjRef) : KPresBInst (replyUnlinkSc (Z := Unit) sc r) := ⟨by unfold replyUnlinkSc; kbres⟩ +instance (t r : ObjRef) : KPresBInst (replyUnlinkTcb (Z := Unit) t r) := ⟨by unfold replyUnlinkTcb; kbres⟩ +instance (c r : ObjRef) : KPresBInst (replyRemove (Z := Unit) c r) := ⟨by unfold replyRemove; kbres⟩ +instance (t r : ObjRef) : KPresBInst (replyRemoveTcb (Z := Unit) t r) := ⟨by unfold replyRemoveTcb; kbres⟩ +instance (sc r : ObjRef) : KPresBInst (bindScReply (Z := Unit) sc r) := ⟨by unfold bindScReply; kbres⟩ +instance (c e r : ObjRef) (d : Bool) : KPresBInst (replyPush (Z := Unit) c e r d) := + ⟨by unfold replyPush; kbres⟩ + +instance (s : ThreadState) : KPresBInst (getBlockingObject (Z := Unit) s) := ⟨by unfold getBlockingObject; kbres⟩ +instance (s : ThreadState) (t : ObjRef) (r : Option ObjRef) : KPresBInst (blockedCancelIpc (Z := Unit) s t r) := + ⟨by unfold blockedCancelIpc; kbres⟩ +instance (t n : ObjRef) : KPresBInst (cancelSignal (Z := Unit) t n) := ⟨by unfold cancelSignal; kbres⟩ +instance (t : ObjRef) : KPresBInst (cancelIpc (Z := Unit) t) := ⟨by unfold cancelIpc; kbres⟩ + +instance (n : ObjRef) (q : List ObjRef) (b : Badge) : KPresBInst (updateWaitingNtfn (Z := Unit) n q b) := + ⟨by unfold updateWaitingNtfn; kbres⟩ +instance (n : ObjRef) (b : Badge) : KPresBInst (sendSignal (Z := Unit) n b) := ⟨by unfold sendSignal; kbres⟩ + +/-- **`send_signal` keeps the budget frame.** -/ +theorem sendSignal_bkA (n : ObjRef) (b : Badge) : KPresB (sendSignal (Z := Unit) n b) := + (inferInstance : KPresBInst (sendSignal (Z := Unit) n b)).pres + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3SigSchedValidA.lean b/proofs/Fl4ProofsA64/P3/Stage3SigSchedValidA.lean new file mode 100644 index 000000000..7f3cf633d --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SigSchedValidA.lean @@ -0,0 +1,832 @@ +/- +P3 (aarch64, A64-P3 Stage 3, T3 G4b / F8, queue item 1): **F8 across `send_signal`** (the TCB-timer signals of +`fl4TimerFires`), slice (a): the release-queue side, `postpone` and `sched_context_resume`. + +`tcbReleaseEnqueue t` keeps `FIA` when `t` has an active SC (`ActTcbA`), is in no ready queue (`NoQ`) and is not the +current thread of a resuming action (`cur ≠ t ∨ ¬(resume ∧ cur ≠ idle)`). `schedContextResume` guards its `postpone` by +`schedulable` of the SC's thread, which gives the active SC; `NoQ` comes from the guard's "not in its own queue" plus +`RqDomPrio` (`P3/Stage3DelReplyRemoveA`), and the current-thread side condition is the lemma's hypothesis `CurNeSc`. + +No premise; new module. +-/ +import Fl4ProofsA64.P3.Stage3DelReplyRemoveA +import Fl4ProofsA64.P3.Stage3SchedIdleA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P3 Classical + +noncomputable section + +instance eqPreO {α : Type} : PreO (@Eq α) := ⟨fun _ => rfl, fun h1 h2 => h1.trans h2⟩ + +/-- `orderedInsert` reads only: the state is kept, and the new list is `t` and members of the old one. -/ +theorem orderedInsert_spec {α β : Type} [Nonempty β] (t : α) (ts : List α) (f : α → RMonad Unit β) + (R : β → β → Prop) {σ : State Unit} {r : List α × State Unit} + (h : r ∈ (orderedInsert (Z := Unit) t ts f R σ).1) : r.2 = σ ∧ ∀ y ∈ r.1, y = t ∨ y ∈ ts := by + unfold orderedInsert at h + obtain ⟨⟨val, σ1⟩, h1, h2⟩ := mem_bindA.1 h + have e1 : σ1 = σ := ((PresR.getsThe (R := (@Eq (State Unit))) (f t)) σ _ h1).symm + obtain ⟨⟨vals, σ2⟩, h3, h4⟩ := mem_bindA.1 h2 + have e2 : σ2 = σ1 := ((PresR.mapM (R := (@Eq (State Unit))) + (fun a => PresR.getsThe (R := (@Eq (State Unit))) (f a)) ts) σ1 _ h3).symm + obtain ⟨⟨z, σ3⟩, h5, h6⟩ := mem_bindA.1 h4 + have e3 : ((z, σ3) : _ × State Unit) = (ts.zip vals, σ2) := h5 + obtain ⟨⟨z', σ4⟩, h7, h8⟩ := mem_bindA.1 h6 + have e7 : ((z', σ4) : _ × State Unit) = (z.takeWhile (fun p => decide (R p.2 val)) ++ [(t, val)] ++ + z.dropWhile (fun p => decide (R p.2 val)), σ3) := h7 + have e4 : r = (z'.map Prod.fst, σ4) := h8 + obtain ⟨hz, hs3⟩ := Prod.mk.inj e3 + obtain ⟨hz', hs4⟩ := Prod.mk.inj e7 + subst hz + refine ⟨by rw [e4]; show σ4 = σ; rw [hs4, hs3, e2, e1], fun y hy => ?_⟩ + rw [e4] at hy + simp only [List.mem_map] at hy + obtain ⟨p, hp, rfl⟩ := hy + rw [hz'] at hp + simp only [List.mem_append, List.mem_singleton] at hp + have hsplit := List.takeWhile_append_dropWhile (p := fun q : α × β => decide (R q.2 val)) (l := ts.zip vals) + rcases hp with (hp | hp) | hp + · right + exact (List.of_mem_zip (hsplit ▸ List.mem_append_left _ hp)).1 + · left; rw [hp] + · right + exact (List.of_mem_zip (hsplit ▸ List.mem_append_right _ hp)).1 + +/-- `FIA` across a release queue replaced by the old one plus one thread `t` with an active SC, in no ready queue and not +the current thread of a resuming action. -/ +theorem FIA.relIns {σ : State Unit} (h : FIA σ) {t : ObjRef} (hn : NoQ t σ) (ha : ActTcbA σ t) + (hc : σ.curThread ≠ t ∨ ¬(σ.schedulerAction = .resumeCurThread ∧ σ.curThread ≠ σ.idleThread)) + {q' : List ObjRef} (hq : ∀ y ∈ q', y = t ∨ y ∈ σ.releaseQueue) : + FIA { σ with releaseQueue := q' } := by + refine ⟨⟨fun d p u hm => ?_, fun e1 e2 => ?_, h.1.blk⟩, fun u hu => ?_⟩ + · obtain ⟨a, n⟩ := h.1.rq d p u hm + refine ⟨a, fun hu => ?_⟩ + rcases hq u hu with e | e + · subst e; exact hn d p hm + · exact n e + · obtain ⟨a, n⟩ := h.1.act e1 e2 + refine ⟨a, fun hu => ?_⟩ + rcases hq _ hu with e | e + · rcases hc with hc | hc + · exact hc e + · exact hc ⟨e1, e2⟩ + · exact n e + · rcases hq u hu with e | e + · subst e; exact ha + · exact h.2 u e + +theorem tcbReleaseEnqueue_fia (t : ObjRef) (R : State Unit → Prop) + (hr : ∀ σ f, R σ → R { σ with releaseQueue := f }) (hp : ∀ σ b, R σ → R { σ with reprogramTimer := b }) : + HT (fun σ => FIA σ ∧ R σ ∧ NoQ t σ ∧ ActTcbA σ t ∧ + (σ.curThread ≠ t ∨ ¬(σ.schedulerAction = .resumeCurThread ∧ σ.curThread ≠ σ.idleThread))) + (tcbReleaseEnqueue (Z := Unit) t) (fun _ σ => FIA σ ∧ R σ) := by + unfold tcbReleaseEnqueue + refine HT.bind (Q := fun q σ => (FIA σ ∧ R σ ∧ NoQ t σ ∧ ActTcbA σ t ∧ + (σ.curThread ≠ t ∨ ¬(σ.schedulerAction = .resumeCurThread ∧ σ.curThread ≠ σ.idleThread))) ∧ + q = σ.releaseQueue) (HT.gets _ fun σ hσ => ⟨hσ, rfl⟩) fun q => ?_ + refine HT.bind (Q := fun q' σ => ((FIA σ ∧ R σ ∧ NoQ t σ ∧ ActTcbA σ t ∧ + (σ.curThread ≠ t ∨ ¬(σ.schedulerAction = .resumeCurThread ∧ σ.curThread ≠ σ.idleThread))) ∧ + q = σ.releaseQueue) ∧ ∀ y ∈ q', y = t ∨ y ∈ q) ?_ fun q' => ?_ + · intro σ hσ r hr + unfold readyTimesAppend at hr + obtain ⟨hs, hm⟩ := orderedInsert_spec _ _ _ _ hr + obtain ⟨a, s'⟩ := r + simp only at hs hm ⊢ + subst hs + exact ⟨hσ, hm⟩ + refine HT.bind (Q := fun _ σ => FIA σ ∧ R σ) ?_ fun _ => ?_ + · unfold setReleaseQueue + refine HT.modify _ fun σ hσ => ?_ + obtain ⟨⟨⟨hI, hR, hn, ha, hc⟩, hq⟩, hm⟩ := hσ + refine ⟨hI.relIns hn ha hc ?_, hr σ _ hR⟩ + intro y hy + rcases hm y hy with e | e + · exact Or.inl e + · exact Or.inr (by rw [← hq]; exact e) + · exact HT.when (fun _ => HT.modify _ fun σ hσ => ⟨hσ.1.ofSame rfl rfl rfl rfl rfl rfl rfl, hp σ true hσ.2⟩) + (fun _ σ hσ => hσ) + + +/-- The SC's thread (if any) has an active SC. -/ +def ScThActive (scPtr : ObjRef) (σ : State Unit) : Prop := + ∀ y k t', σ.kheap scPtr = some (.schedContext y k) → y.scTcb = some t' → ActTcbA σ t' + +/-- The SC's thread (if any) is not the current thread of a resuming action. -/ +def ScThCurNe (scPtr : ObjRef) (σ : State Unit) : Prop := + ∀ y k t', σ.kheap scPtr = some (.schedContext y k) → y.scTcb = some t' → + (σ.curThread ≠ t' ∨ ¬(σ.schedulerAction = .resumeCurThread ∧ σ.curThread ≠ σ.idleThread)) + +/-- **`postpone` keeps `FIA`.** -/ +theorem postpone_fia (scPtr : ObjRef) (R : State Unit → Prop) + (hK : ∀ σ σ', σ'.kheap = σ.kheap → σ'.idleThread = σ.idleThread → R σ → R σ') : + HT (fun σ => FIA σ ∧ RqDomPrio σ ∧ R σ ∧ ScThActive scPtr σ ∧ ScThCurNe scPtr σ) (postpone (Z := Unit) scPtr) + (fun _ σ => FIA σ ∧ R σ) := by + unfold postpone + refine HT.bind (Q := fun tcbOpt σ => FIA σ ∧ RqDomPrio σ ∧ R σ ∧ ∀ t', tcbOpt = some t' → + (ActTcbA σ t' ∧ (σ.curThread ≠ t' ∨ ¬(σ.schedulerAction = .resumeCurThread ∧ σ.curThread ≠ σ.idleThread)))) ?_ + fun tcbOpt => ?_ + · intro σ hσ r hr + obtain ⟨hs, y, k, hk, hv⟩ := getScObjRef_mem hr + obtain ⟨a, s'⟩ := r + simp only at hs hv ⊢ + subst hs + exact ⟨hσ.1, hσ.2.1, hσ.2.2.1, fun t' ht' => ⟨hσ.2.2.2.1 y k t' hk (by rw [← hv]; exact ht'), + hσ.2.2.2.2 y k t' hk (by rw [← hv]; exact ht')⟩⟩ + refine HT.bind (Q := fun tcbPtr σ => FIA σ ∧ RqDomPrio σ ∧ R σ ∧ ActTcbA σ tcbPtr ∧ + (σ.curThread ≠ tcbPtr ∨ ¬(σ.schedulerAction = .resumeCurThread ∧ σ.curThread ≠ σ.idleThread))) + (HT.assertOpt _ fun a ha σ hσ => ⟨hσ.1, hσ.2.1, hσ.2.2.1, hσ.2.2.2 a ha⟩) fun tcbPtr => ?_ + refine HT.bind (Q := fun _ σ => FIA σ ∧ ((R σ ∧ ActTcbA σ tcbPtr ∧ + (σ.curThread ≠ tcbPtr ∨ ¬(σ.schedulerAction = .resumeCurThread ∧ σ.curThread ≠ σ.idleThread))) ∧ NoQ tcbPtr σ)) + (HT.mono (deqD tcbPtr (fun σ => R σ ∧ ActTcbA σ tcbPtr ∧ + (σ.curThread ≠ tcbPtr ∨ ¬(σ.schedulerAction = .resumeCurThread ∧ σ.curThread ≠ σ.idleThread))) + (fun σ f h => ⟨hK σ _ rfl rfl h.1, h.2⟩)) + (fun σ h => ⟨h.1, h.2.1, h.2.2.1, h.2.2.2⟩) (fun _ σ h => ⟨h.1, h.2.1, h.2.2⟩)) fun _ => ?_ + refine HT.bind (Q := fun _ σ => FIA σ ∧ R σ) + (HT.mono (tcbReleaseEnqueue_fia tcbPtr R (fun σ f h => hK σ _ rfl rfl h) (fun σ b h => hK σ _ rfl rfl h)) + (fun σ h => ⟨h.1, h.2.1.1, h.2.2, h.2.1.2.1, h.2.1.2.2⟩) (fun _ _ h => h)) fun _ => ?_ + exact HT.modify _ fun σ hσ => ⟨hσ.1.ofSame rfl rfl rfl rfl rfl rfl rfl, hK σ _ rfl rfl hσ.2⟩ + +theorem getSchedContext_mem {σ : State Unit} {p : ObjRef} {r : SchedContext × State Unit} + (h : r ∈ (getSchedContext (Z := Unit) p σ).1) : r.2 = σ ∧ ∃ n, σ.kheap p = some (.schedContext r.1 n) := by + unfold getSchedContext at h + obtain ⟨y1, hy1, h2⟩ := mem_bindA.1 h + have hy1' : y1 ∈ (NonDet.assertOpt (σ.kheap p) σ).1 := hy1 + cases hk : σ.kheap p with + | none => rw [hk] at hy1'; exact absurd hy1' (Set.notMem_empty _) + | some ko => + rw [hk] at hy1' + have e1 : y1 = (ko, σ) := hy1' + subst e1 + cases ko with + | schedContext sc n => + have e2 : r = (sc, σ) := h2 + subst e2 + exact ⟨rfl, n, rfl⟩ + | _ => exact absurd h2 (Set.notMem_empty _) + +/-- **`sched_context_resume` keeps `FIA`.** -/ +theorem schedContextResume_fia (scPtr : ObjRef) (R : State Unit → Prop) + (hK : ∀ σ σ', σ'.kheap = σ.kheap → σ'.idleThread = σ.idleThread → R σ → R σ') : + HT (fun σ => FIA σ ∧ RqDomPrio σ ∧ R σ ∧ ScThCurNe scPtr σ) (schedContextResume (Z := Unit) scPtr) + (fun _ σ => FIA σ ∧ R σ) := by + unfold schedContextResume + refine HT.bind (Q := fun sc σ => (FIA σ ∧ RqDomPrio σ ∧ R σ ∧ ScThCurNe scPtr σ) ∧ + ∃ n, σ.kheap scPtr = some (.schedContext sc n)) ?_ fun sc => ?_ + · intro σ hσ r hr + obtain ⟨hs, n, hk⟩ := getSchedContext_mem hr + obtain ⟨a, s'⟩ := r + simp only at hs hk ⊢ + subst hs + exact ⟨hσ, n, hk⟩ + refine HT.bind (Q := fun tptr σ => ((FIA σ ∧ RqDomPrio σ ∧ R σ ∧ ScThCurNe scPtr σ) ∧ + ∃ n, σ.kheap scPtr = some (.schedContext sc n)) ∧ sc.scTcb = some tptr) + (HT.assertOpt _ fun a ha σ hσ => ⟨hσ, ha⟩) fun tptr => ?_ + refine HT.bind (Q := fun sched σ => (((FIA σ ∧ RqDomPrio σ ∧ R σ ∧ ScThCurNe scPtr σ) ∧ + ∃ n, σ.kheap scPtr = some (.schedContext sc n)) ∧ sc.scTcb = some tptr) ∧ sched = schedulable tptr σ) + (HT.gets _ fun σ hσ => ⟨hσ, rfl⟩) fun sched => ?_ + refine HT.when (fun hs => ?_) (fun _ σ h => ⟨h.1.1.1.1, h.1.1.1.2.2.1⟩) + have hsch : ∀ σ, ((((FIA σ ∧ RqDomPrio σ ∧ R σ ∧ ScThCurNe scPtr σ) ∧ + ∃ n, σ.kheap scPtr = some (.schedContext sc n)) ∧ sc.scTcb = some tptr) ∧ sched = schedulable tptr σ) → + schedulable tptr σ = true := fun σ h => by rw [← h.2]; exact hs + let Pc : State Unit → Prop := fun σ => ((((FIA σ ∧ RqDomPrio σ ∧ R σ ∧ ScThCurNe scPtr σ) ∧ + ∃ n, σ.kheap scPtr = some (.schedContext sc n)) ∧ sc.scTcb = some tptr) ∧ sched = schedulable tptr σ) + refine HT.mono (P := Pc) ?_ (fun σ h => h) (fun _ _ h => h) + refine HT.bind (Q := fun _ σ => Pc σ) (HT.threadGet _ tptr fun σ hσ x hx => hσ) fun ts => ?_ + refine HT.bind (Q := fun _ σ => Pc σ) (HT.getsThe _ fun σ hσ a _ => hσ) fun ready => ?_ + refine HT.bind (Q := fun _ σ => Pc σ) (HT.getsThe _ fun σ hσ a _ => hσ) fun suf => ?_ + refine HT.when (fun _ => ?_) (fun _ σ h => ⟨h.1.1.1.1, h.1.1.1.2.2.1⟩) + refine HT.bind (Q := fun _ σ => Pc σ) (HT.threadGet _ tptr fun σ hσ x hx => hσ) fun d => ?_ + refine HT.bind (Q := fun _ σ => Pc σ) (HT.threadGet _ tptr fun σ hσ x hx => hσ) fun p => ?_ + refine HT.bind (Q := fun _ σ => Pc σ) ?_ fun q => ?_ + · unfold getTcbQueue + exact HT.bind (Q := fun _ σ => Pc σ) (HT.gets _ fun σ hσ => hσ) fun qs => HT.pure _ fun σ hσ => hσ + refine HT.bind (Q := fun _ σ => Pc σ) (HT.assert fun _ σ hσ => hσ) fun _ => ?_ + refine HT.mono (postpone_fia scPtr R hK) (fun σ h => ?_) (fun _ _ h => h) + obtain ⟨⟨⟨⟨hI, hD, hR, hC⟩, n, hk⟩, hsc⟩, hsd⟩ := h + have hact : ActTcbA σ tptr := (schedulable_actA (hsch σ ⟨⟨⟨⟨hI, hD, hR, hC⟩, n, hk⟩, hsc⟩, hsd⟩)).1 + refine ⟨hI, hD, hR, fun y k t' hk' hv => ?_, hC⟩ + rw [hk] at hk' + have e := Option.some.inj hk' + injection e with e1 e2 + subst e1 + rw [hsc] at hv + have := Option.some.inj hv + subst this + exact hact + + +theorem setScTcb_spec {σ : State Unit} {p c : ObjRef} {r : Unit × State Unit} + (h : r ∈ (setScObjRef (Z := Unit) scTcbUpdate p (some c) σ).1) : + ∃ y n, σ.kheap p = some (.schedContext y n) ∧ + r.2 = { σ with kheap := Function.update σ.kheap p (some (.schedContext (scTcbUpdate (fun _ => some c) y) n)) } := by + obtain ⟨y1, hy1, h2⟩ := h + have hy1' : y1 ∈ (NonDet.assertOpt (σ.kheap p) σ).1 := hy1 + cases hk : σ.kheap p with + | none => rw [hk] at hy1'; exact absurd hy1' (Set.notMem_empty _) + | some ko => + rw [hk] at hy1' + have e1 : y1 = (ko, σ) := hy1' + subst e1 + cases ko with + | schedContext sc n => + obtain ⟨ko', hko, hr2⟩ := setObject_memA h2 + exact ⟨sc, n, rfl, hr2⟩ + | _ => exact absurd h2 (Set.notMem_empty _) + +theorem threadSet_spec {σ : State Unit} {c : ObjRef} {f : Tcb → Tcb} {r : Unit × State Unit} + (h : r ∈ (threadSet (Z := Unit) f c σ).1) : + ∃ x, σ.kheap c = some (.tcb x) ∧ + r.2 = { σ with kheap := Function.update σ.kheap c (some (.tcb (f x))) } := by + unfold threadSet at h + obtain ⟨y1, hy1, h2⟩ := mem_bindA.1 h + change y1 ∈ (NonDet.assertOpt (getTcb (Z := Unit) c σ) σ).1 at hy1 + cases hg : getTcb (Z := Unit) c σ with + | none => rw [hg] at hy1; exact absurd hy1 (Set.notMem_empty _) + | some x => + rw [hg] at hy1 + have e1 : y1 = (x, σ) := hy1 + subst e1 + obtain ⟨ko, hko, hr2⟩ := setObject_memA h2 + exact ⟨x, getTcb_kheapA hg, hr2⟩ + +theorem RqDomPrio.ofTcbRepl {σ : State Unit} {p : ObjRef} {x x' : Tcb} (h : RqDomPrio σ) + (hx : σ.kheap p = some (.tcb x)) (hd : x'.tcbDomain = x.tcbDomain) (hp : x'.tcbPriority = x.tcbPriority) : + RqDomPrio { σ with kheap := Function.update σ.kheap p (some (.tcb x')) } := by + intro f d p' y hy hm + by_cases e : f = p + · subst e + have : x' = y := by simpa using hy + subst this + obtain ⟨a, b⟩ := h f d p' x hx hm + exact ⟨a.trans hd.symm, b.trans hp.symm⟩ + · exact h f d p' y (by simpa [Function.update_of_ne e] using hy) hm + +/-- **The owner-less donate, with its post-state**: the SC's thread becomes `caller`; current thread, action and idle +thread are kept. -/ +theorem donateNone_fia (scPtr caller : ObjRef) (R : State Unit → Prop) (F : TFrame R) (σ0 : State Unit) : + HT (fun σ => σ = σ0 ∧ FIA σ ∧ ScIs caller none σ ∧ + (∀ y k, σ.kheap scPtr = some (.schedContext y k) → y.scTcb = none) ∧ R σ) + (schedContextDonate (Z := Unit) scPtr caller) + (fun _ σ => FIX (fun _ => False) (fun u => u = caller) σ ∧ R σ ∧ + (∃ y k, σ.kheap scPtr = some (.schedContext y k) ∧ y.scTcb = some caller) ∧ + σ.curThread = σ0.curThread ∧ σ.schedulerAction = σ0.schedulerAction ∧ σ.idleThread = σ0.idleThread ∧ + (RqDomPrio σ0 → RqDomPrio σ)) := by + intro σ hσ r hr + obtain ⟨hσ0, hI, hS, hO, hR⟩ := hσ + subst hσ0 + unfold schedContextDonate at hr + obtain ⟨⟨fromOpt, σ1⟩, h1, h2⟩ := mem_bindA.1 hr + obtain ⟨hs1, y, k, hk, hv⟩ := getScObjRef_mem h1 + have hs1' : σ1 = σ := hs1 + have hnone : fromOpt = none := by + have : fromOpt = y.scTcb := hv + rw [this]; exact hO y k hk + rw [hs1'] at h2 + obtain ⟨⟨u, σ2⟩, h3, h4⟩ := mem_bindA.1 h2 + have e3 : ((u, σ2) : Unit × State Unit) = ((), σ) := by + unfold NonDet.when at h3 + rw [if_neg (by simp [hnone])] at h3 + exact h3 + have hσ2 : σ2 = σ := (Prod.mk.inj e3).2 + obtain ⟨⟨u2, σ3⟩, h5, h6⟩ := mem_bindA.1 h4 + have h5' : (u2, σ3) ∈ (setScObjRef (Z := Unit) scTcbUpdate scPtr (some caller) σ2).1 := h5 + obtain ⟨y', n, hk', hσ3⟩ := setScTcb_spec h5' + have hσ3' : σ3 = { σ2 with kheap := Function.update σ2.kheap scPtr (some (.schedContext (scTcbUpdate (fun _ => some caller) y') n)) } := hσ3 + have hkeep : TKeepA σ2 σ3 := + (by tpres : TPres (setScObjRef (Z := Unit) scTcbUpdate scPtr (some caller))) σ2 _ h5' + rw [hσ2] at hkeep hσ3' hk' + have hcells : CellsEq σ σ3 := by + rw [hσ3']; exact cells_upd hk' (fun x h => by cases h) (fun x h => by cases h) + have pre3 : FIA σ3 ∧ ScIs caller none σ3 ∧ R σ3 := ⟨hI.ofTKeep hkeep, hS.ofTKeep hkeep, F.keep σ σ3 hkeep hR⟩ + obtain ⟨hF, hRr⟩ := rebindR caller scPtr R (fun σ x x' hx hst h => F.repl σ caller x x' hx hst h) σ3 pre3 r h6 + obtain ⟨x, hx, hr2⟩ := threadSet_spec (r := r) (f := tcbSchedContextUpdate fun _ => some scPtr) (c := caller) h6 + have hne : scPtr ≠ caller := by + intro e + have h3k : σ3.kheap scPtr = some (.schedContext (scTcbUpdate (fun _ => some caller) y') n) := by + rw [hσ3']; simp + rw [e] at h3k + rw [hx] at h3k + cases h3k + refine ⟨hF, hRr, ⟨scTcbUpdate (fun _ => some caller) y', n, ?_, rfl⟩, ?_, ?_, ?_, fun hrq => ?_⟩ + · rw [hr2] + simp only + rw [Function.update_of_ne hne, hσ3'] + simp + · rw [hr2, hσ3'] + · rw [hr2, hσ3'] + · rw [hr2, hσ3'] + · have h3 : RqDomPrio σ3 := hrq.ofCells hcells + rw [hr2] + exact h3.ofTcbRepl hx rfl rfl + + +theorem TFrame.and {R S : State Unit → Prop} (a : TFrame R) (b : TFrame S) : TFrame (fun σ => R σ ∧ S σ) := + ⟨fun σ σ' k h => ⟨a.keep σ σ' k h.1, b.keep σ σ' k h.2⟩, fun σ σ' hk hid h => ⟨a.kh σ σ' hk hid h.1, b.kh σ σ' hk hid h.2⟩, + fun σ p x x' hx hst h => ⟨a.repl σ p x x' hx hst h.1, b.repl σ p x x' hx hst h.2⟩⟩ + +theorem curNe_of_noSc {σ : State Unit} {d : ObjRef} (h : FIA σ) (hS : ScIs d none σ) : + σ.curThread ≠ d ∨ ¬(σ.schedulerAction = .resumeCurThread ∧ σ.curThread ≠ σ.idleThread) := by + by_cases hc : σ.schedulerAction = .resumeCurThread ∧ σ.curThread ≠ σ.idleThread + · left + obtain ⟨⟨z, sc, e, es, -⟩, -⟩ := h.1.act hc.1 hc.2 + intro hcd + rw [hcd] at e + rw [hS z e] at es + cases es + · exact Or.inr hc + +theorem FIX.ofSt {σ : State Unit} {t : ObjRef} {ts : ThreadState} (h : FIX (fun _ => False) (fun u => u = t) σ) + (hs : StIs t ts σ) (hb : BlockedTsA ts = false) : FIA σ := + ⟨⟨h.1.rq, fun e1 e2 => h.1.act e1 e2 id, fun u x sc e1 e2 e3 => h.1.blk u x sc e1 e2 e3 (fun hu => by + subst hu; rw [hs x e1, hb] at e2; cases e2)⟩, h.2⟩ + +instance (p : ObjRef) : CPresInst (getNotification (Z := Unit) p) := ⟨getSimpleKo_cells _ p⟩ + +/-- The donate-and-resume of `maybeDonateSc` keeps `FIA`. -/ +theorem donateResume_fia (scPtr dest : ObjRef) {ts : ThreadState} (hts : BlockedTsA ts = false) + (R : State Unit → Prop) (F : TFrame R) : + HT (fun σ => FIA σ ∧ RqDomPrio σ ∧ R σ ∧ StIs dest ts σ ∧ ScIs dest none σ ∧ + (∀ y k, σ.kheap scPtr = some (.schedContext y k) → y.scTcb = none)) + (do schedContextDonate (Z := Unit) scPtr dest + schedContextResume scPtr) + (fun _ σ => FIA σ ∧ R σ) := by + intro σ hσ r hr + obtain ⟨hI, hD, hR, hSt, hS, hO⟩ := hσ + obtain ⟨⟨u, σ1⟩, h1, h2⟩ := mem_bindA.1 hr + have F' : TFrame (fun σ => R σ ∧ StIs dest ts σ) := F.and (StIs.frame dest ts) + obtain ⟨hF, ⟨hR1, hSt1⟩, ⟨y0, k0, hk0, hv0⟩, hc1, ha1, hi1, hD1⟩ := + donateNone_fia scPtr dest (fun σ => R σ ∧ StIs dest ts σ) F' σ σ ⟨rfl, hI, hS, hO, hR, hSt⟩ ⟨u, σ1⟩ h1 + have hI1 : FIA σ1 := FIX.ofSt hF hSt1 hts + have hne := curNe_of_noSc hI hS + refine schedContextResume_fia scPtr R (fun σ σ' hk hid h => F.kh σ σ' hk hid h) σ1 + ⟨hI1, hD1 hD, hR1, fun y k t' hk' hv => ?_⟩ r h2 + rw [hk0] at hk' + have e := Option.some.inj hk' + injection e with e1 e2 + subst e1 + rw [hv0] at hv + have hd : t' = dest := (Option.some.inj hv).symm + show σ1.curThread ≠ t' ∨ ¬(σ1.schedulerAction = .resumeCurThread ∧ σ1.curThread ≠ σ1.idleThread) + rw [hd, hc1, ha1, hi1] + exact hne + +/-- **`maybe_donate_sc` keeps `FIA`** (the thread is already in a non-blocked state). -/ +theorem maybeDonateSc_fia (dest n : ObjRef) {ts : ThreadState} (hts : BlockedTsA ts = false) (R : State Unit → Prop) (F : TFrame R) : + HT (fun σ => FIA σ ∧ RqDomPrio σ ∧ R σ ∧ StIs dest ts σ) (maybeDonateSc (Z := Unit) dest n) + (fun _ σ => FIA σ ∧ R σ) := by + unfold maybeDonateSc + refine HT.bind (Q := fun scOpt σ => FIA σ ∧ RqDomPrio σ ∧ R σ ∧ StIs dest ts σ ∧ ScIs dest scOpt σ) + ?_ fun scOpt => ?_ + · unfold getTcbObjRef + exact HT.threadGet _ dest fun σ hσ x hx => ⟨hσ.1, hσ.2.1, hσ.2.2.1, hσ.2.2.2, fun y hy => by + rw [hx] at hy + have : x = y := KernelObject.tcb.inj (Option.some.inj hy) + subst this; rfl⟩ + refine HT.when (fun hc => ?_) (fun _ σ h => ⟨h.1, h.2.2.1⟩) + subst hc + have T0 : ∀ {β : Type} {m : SMonad Unit β}, TPres m → CPres m → + HT (fun σ => FIA σ ∧ RqDomPrio σ ∧ R σ ∧ StIs dest ts σ ∧ ScIs dest none σ) m + (fun _ σ => FIA σ ∧ RqDomPrio σ ∧ R σ ∧ StIs dest ts σ ∧ ScIs dest none σ) := fun h1 h2 => + HT.mono (HT.ofTC h1 h2 (fun σ => R σ ∧ StIs dest ts σ ∧ ScIs dest none σ) + (fun σ σ' k h => ⟨F.keep σ σ' k h.1, (StIs.frame dest ts).keep σ σ' k h.2.1, h.2.2.ofTKeep k⟩)) + (fun σ h => ⟨h.1, ⟨h.2.2.1, h.2.2.2.1, h.2.2.2.2⟩, h.2.1⟩) (fun _ σ h => ⟨h.1, h.2.2, h.2.1.1, h.2.1.2.1, h.2.1.2.2⟩) + refine HT.bind (Q := fun _ σ => FIA σ ∧ RqDomPrio σ ∧ R σ ∧ StIs dest ts σ ∧ ScIs dest none σ) + (T0 (by unfold getNtfnObjRef getSkObjRef; tpres) (by unfold getNtfnObjRef getSkObjRef; cpres)) fun nsc => ?_ + cases nsc with + | none => exact HT.pure () (fun σ h => ⟨h.1, h.2.2.1⟩) + | some scPtr => + show HT _ ((do let scTcb ← getScObjRef SchedContext.scTcb scPtr + NonDet.when (scTcb = none) (do schedContextDonate scPtr dest; schedContextResume scPtr))) _ + refine HT.bind (Q := fun scTcb σ => (FIA σ ∧ RqDomPrio σ ∧ R σ ∧ StIs dest ts σ ∧ ScIs dest none σ) ∧ + (scTcb = none → ∀ y k, σ.kheap scPtr = some (.schedContext y k) → y.scTcb = none)) ?_ fun scTcb => ?_ + · intro σ hσ r hr + obtain ⟨hs, y, k, hk, hv⟩ := getScObjRef_mem hr + obtain ⟨a, s'⟩ := r + simp only at hs hv ⊢ + subst hs + refine ⟨hσ, fun hn y' k' hk' => ?_⟩ + rw [hk] at hk' + have e := Option.some.inj hk' + injection e with e1 e2 + subst e1 + rw [← hv]; exact hn + · refine HT.when (fun hn => ?_) (fun _ σ h => ⟨h.1.1, h.1.2.2.1⟩) + exact HT.mono (donateResume_fia scPtr dest hts R F) + (fun σ h => ⟨h.1.1, h.1.2.1, h.1.2.2.1, h.1.2.2.2.1, h.1.2.2.2.2, h.2 hn⟩) (fun _ _ h => h) + + +/-! ## `updateWaitingNtfn` -/ + +/-- Every TCB's state is as in `σ0`, but for the thread `t0`. -/ +def KE0 (t0 : ObjRef) (σ0 σ : State Unit) : Prop := + ∀ p x', σ.kheap p = some (.tcb x') → p ≠ t0 → ∃ x, σ0.kheap p = some (.tcb x) ∧ x'.tcbState = x.tcbState + +theorem KE0.refl (t0 : ObjRef) (σ0 : State Unit) : KE0 t0 σ0 σ0 := fun _ x' h _ => ⟨x', h, rfl⟩ + +/-- `KE0` together with the idle thread of `σ0`. -/ +def KI (t0 : ObjRef) (σ0 σ : State Unit) : Prop := KE0 t0 σ0 σ ∧ σ.idleThread = σ0.idleThread + +theorem KI.refl (t0 : ObjRef) (σ0 : State Unit) : KI t0 σ0 σ0 := ⟨KE0.refl t0 σ0, rfl⟩ + +theorem KI.frame (t0 : ObjRef) (σ0 : State Unit) : TFrame (KI t0 σ0) := + ⟨fun σ σ' k h => ⟨fun p x' hx hp => by + obtain ⟨x, e, -, st⟩ := k.2.2.2.2.2.1 p x' hx + obtain ⟨x0, e0, st0⟩ := h.1 p x e hp + exact ⟨x0, e0, st.trans st0⟩, k.2.2.2.1.trans h.2⟩, + fun σ σ' hk hid h => ⟨fun p x' hx hp => h.1 p x' (by rw [hk] at hx; exact hx) hp, hid.trans h.2⟩, + fun σ p x x' hx hst h => ⟨fun q y hy hq => by + by_cases e : q = p + · subst e + have : x' = y := by simpa using hy + subst this + obtain ⟨x0, e0, st0⟩ := h.1 q x hx hq + exact ⟨x0, e0, hst.trans st0⟩ + · exact h.1 q y (by simpa [Function.update_of_ne e] using hy) hq, h.2⟩⟩ + +theorem IdleSt.ofKI {σ0 σ : State Unit} {t0 : ObjRef} (hI : IdleSt σ0) (hk : KI t0 σ0 σ) + (hne : t0 ≠ σ0.idleThread) : IdleSt σ := fun x hx => by + rw [hk.2] at hx + obtain ⟨x0, e0, st0⟩ := hk.1 _ x hx hne.symm + rw [st0]; exact hI x0 e0 + +theorem asUser_spec {α : Type} {t : ObjRef} {f : UserMonad α} {σ : State Unit} {r : α × State Unit} + (h : r ∈ (asUser (Z := Unit) t f σ).1) : + ∃ x uc', σ.kheap t = some (.tcb x) ∧ + r.2 = { σ with kheap := Function.update σ.kheap t (some (.tcb { x with tcbArch := archTcbContextSet uc' x.tcbArch })) } := by + obtain ⟨y1, hy1, hr⟩ := h + have hy1' : y1 ∈ (NonDet.assertOpt (getTcb (Z := Unit) t σ) σ).1 := hy1 + cases hg : getTcb (Z := Unit) t σ with + | none => rw [hg] at hy1'; exact absurd hy1' (Set.notMem_empty _) + | some x => + rw [hg] at hy1' + have e1 : y1 = (x, σ) := hy1' + subst e1 + obtain ⟨y2, hy2, hr⟩ := hr + have e2 : y2 = (archTcbContextGet x.tcbArch, σ) := hy2 + subst e2 + obtain ⟨⟨⟨a, uc'⟩, s3⟩, hy3, hr⟩ := hr + have e3 : s3 = σ := hy3.2 + subst e3 + obtain ⟨y4, hy4, hr⟩ := hr + have e4 : y4 = ({ x with tcbArch := archTcbContextSet uc' x.tcbArch }, s3) := hy4 + subst e4 + obtain ⟨y5, hy5, hr⟩ := hr + obtain ⟨ko, hko, hr5⟩ := setObject_memA hy5 + have e6 : r = (a, y5.2) := hr + exact ⟨x, uc', getTcb_kheapA hg, by rw [e6]; exact hr5⟩ + +theorem asUserD {α : Type} (t : ObjRef) (f : UserMonad α) (R : State Unit → Prop) + (hR : ∀ σ σ', TKeepA σ σ' → R σ → R σ') : + HT (fun σ => FIA σ ∧ RqDomPrio σ ∧ R σ) (asUser (Z := Unit) t f) + (fun _ σ => FIA σ ∧ RqDomPrio σ ∧ R σ) := by + intro σ hσ r hr + obtain ⟨x, uc', hx, hr2⟩ := asUser_spec hr + have k := asUser_tpres t f σ r hr + refine ⟨hσ.1.ofTKeep k, ?_, hR σ _ k hσ.2.2⟩ + rw [hr2] + exact hσ.2.1.ofTcbRepl hx rfl rfl + +theorem setThreadStateD (r : ObjRef) (ts : ThreadState) (hts : BlockedTsA ts = false) (R : State Unit → Prop) + (hT : ∀ σ x, σ.kheap r = some (.tcb x) → R σ → + R { σ with kheap := Function.update σ.kheap r (some (.tcb { x with tcbState := ts })) }) + (hA : ∀ σ a, R σ → R { σ with schedulerAction := a }) : + HT (fun σ => FIA σ ∧ RqDomPrio σ ∧ R σ) (setThreadState (Z := Unit) r ts) + (fun _ σ => FIA σ ∧ RqDomPrio σ ∧ R σ ∧ StIs r ts σ) := by + unfold Fl4SpecA64.setThreadState Fl4SpecA64.setThreadStateAct + refine HT.bind (Q := fun _ σ => FIA σ ∧ RqDomPrio σ ∧ R σ ∧ StIs r ts σ) ?_ fun _ => ?_ + · intro σ hσ r' hr + obtain ⟨x, hx, hr2⟩ := threadSet_spec hr + rw [hr2] + refine ⟨hσ.1.ofTcbUpd hx (x' := { x with tcbState := ts }) rfl (fun hb => by rw [hts] at hb; cases hb), + hσ.2.1.ofTcbRepl hx rfl rfl, hT σ x hx hσ.2.2, fun y hy => ?_⟩ + have : ({ x with tcbState := ts } : Tcb) = y := by simpa using hy + rw [← this] + · refine HT.bind (Q := fun _ σ => FIA σ ∧ RqDomPrio σ ∧ R σ ∧ StIs r ts σ) (HT.gets _ fun σ hσ => hσ) fun cur => ?_ + refine HT.bind (Q := fun _ σ => FIA σ ∧ RqDomPrio σ ∧ R σ ∧ StIs r ts σ) (HT.gets _ fun σ hσ => hσ) fun act => ?_ + refine HT.bind (Q := fun _ σ => FIA σ ∧ RqDomPrio σ ∧ R σ ∧ StIs r ts σ) (HT.gets _ fun σ hσ => hσ) fun sched => ?_ + exact HT.when (fun _ => HT.mono (HT.setAct .chooseNewThread (by intro e; cases e) + (fun σ => RqDomPrio σ ∧ R σ ∧ StIs r ts σ) (fun σ a h => ⟨h.1, hA σ a h.2.1, h.2.2⟩)) + (fun σ h => ⟨h.1, h.2.1, h.2.2.1, h.2.2.2⟩) (fun _ _ h => h)) (fun _ σ hσ => hσ) + +theorem refillCheckT_fia (scOpt : Option ObjRef) (R : State Unit → Prop) (hR : ∀ σ σ', TKeepA σ σ' → R σ → R σ') : + HT (fun σ => FIA σ ∧ R σ) (ifSporadicCurScTestRefillUnblockCheck (Z := Unit) scOpt) + (fun _ σ => FIA σ ∧ R σ) := + HT.ofTPres (by unfold ifSporadicCurScTestRefillUnblockCheck ifCondRefillUnblockCheck; tpres) R hR + +instance (p : ObjRef) (e : Notification) : CPresInst (setNotification (Z := Unit) p e) := + ⟨setSimpleKo_cells _ nsko_ntfn p e⟩ + +/-- **The wake-up tail** shared by `update_waiting_ntfn` and the bound-thread arm of `send_signal`: the woken thread +`dest` (blocked before, with an active bound SC) becomes `running`, may be donated the notification's SC, and may be +switched to. -/ +theorem wakeTail_fia (dest n : ObjRef) (badge : Badge) (σ0 : State Unit) : + HT (fun σ => FIA σ ∧ RqDomPrio σ ∧ (KI dest σ0 σ ∧ BAs [dest] σ)) + (do setThreadState (Z := Unit) dest .running + asUser dest (setRegister badge_register badge) + maybeDonateSc dest n + let sched ← NonDet.gets (schedulable dest) + NonDet.when sched <| possibleSwitchTo dest + let scopt ← getTcbObjRef Tcb.tcbSchedContext dest + ifSporadicCurScTestRefillUnblockCheck scopt) + (fun _ σ => FIA σ ∧ KI dest σ0 σ) := by + have FK : TFrame (KI dest σ0) := KI.frame dest σ0 + have FR_keep : ∀ σ σ', TKeepA σ σ' → (KI dest σ0 σ ∧ BAs [dest] σ) → (KI dest σ0 σ' ∧ BAs [dest] σ') := + fun σ σ' k h => ⟨FK.keep σ σ' k h.1, h.2.ofTKeep k⟩ + refine HT.bind (Q := fun _ σ => FIA σ ∧ RqDomPrio σ ∧ (KI dest σ0 σ ∧ BAs [dest] σ) ∧ StIs dest .running σ) + (setThreadStateD dest .running rfl (fun σ => KI dest σ0 σ ∧ BAs [dest] σ) ?_ ?_) fun _ => ?_ + · intro σ x hx h + refine ⟨⟨fun p y hy hp => ?_, h.1.2⟩, ?_⟩ + · by_cases e : p = dest + · exact absurd e hp + · exact h.1.1 p y (by simpa [Function.update_of_ne e] using hy) hp + · exact h.2.tcbUpd hx rfl + · intro σ a h + exact ⟨h.1, h.2.act a⟩ + refine HT.bind (Q := fun _ σ => FIA σ ∧ RqDomPrio σ ∧ (KI dest σ0 σ ∧ BAs [dest] σ) ∧ StIs dest .running σ) + (HT.mono (asUserD dest _ (fun σ => (KI dest σ0 σ ∧ BAs [dest] σ) ∧ StIs dest .running σ) + (fun σ σ' k h => ⟨FR_keep σ σ' k h.1, (StIs.frame dest .running).keep σ σ' k h.2⟩)) + (fun σ h => ⟨h.1, h.2.1, h.2.2⟩) (fun _ σ h => ⟨h.1, h.2.1, h.2.2⟩)) fun _ => ?_ + refine HT.bind (Q := fun _ σ => FIA σ ∧ KI dest σ0 σ) + (HT.mono (maybeDonateSc_fia dest n (ts := .running) rfl (KI dest σ0) FK) (fun σ h => ⟨h.1, h.2.1, h.2.2.1.1, h.2.2.2⟩) + (fun _ _ h => h)) fun _ => ?_ + refine HT.bind (Q := fun sched σ => (FIA σ ∧ KI dest σ0 σ) ∧ sched = schedulable dest σ) + (HT.gets _ fun σ hσ => ⟨hσ, rfl⟩) fun sched => ?_ + refine HT.bind (Q := fun _ σ => FIA σ ∧ KI dest σ0 σ) ?_ fun _ => ?_ + · refine HT.when (fun hs => ?_) (fun _ σ h => h.1) + exact HT.mono (possibleSwitchTo_fia' dest (KI dest σ0) (fun σ f h => h) (fun σ a h => h)) + (fun σ h => ⟨h.1.1, BoundActive.ofAct (schedulable_actA (by rw [← h.2]; exact hs)).1, h.1.2⟩) + (fun _ _ h => h) + refine HT.bind (Q := fun _ σ => FIA σ ∧ KI dest σ0 σ) + (HT.ofTPres (by unfold getTcbObjRef; tpres) (KI dest σ0) FK.keep) fun scopt => ?_ + exact refillCheckT_fia scopt (KI dest σ0) FK.keep + + +theorem HT.pure_bind {α β : Type} {a : α} {f : α → SMonad Unit β} {P : State Unit → Prop} + {Q : β → State Unit → Prop} (h : HT P (f a) Q) : HT P ((Pure.pure a : SMonad Unit α) >>= f) Q := + fun σ hσ r hr => by + obtain ⟨x, hx, hr2⟩ := mem_bindA.1 hr + have : x = (a, σ) := hx + subst this + exact h σ hσ r hr2 + +/-- **`update_waiting_ntfn` keeps `FIA`**, the woken head `the queue.head?` blocked and its state alone changed. -/ +theorem updateWaitingNtfn_fia (n : ObjRef) (queue : List ObjRef) (badge : Badge) (σ0 : State Unit) : + HT (fun σ => σ = σ0 ∧ FIA σ ∧ RqDomPrio σ ∧ queue ≠ [] ∧ + (∀ x, σ.kheap (the queue.head?) = some (.tcb x) → BlockedTsA x.tcbState = true)) + (updateWaitingNtfn (Z := Unit) n queue badge) + (fun _ σ => FIA σ ∧ KI (the queue.head?) σ0 σ) := by + unfold updateWaitingNtfn + refine HT.bind (Q := fun _ σ => σ = σ0 ∧ FIA σ ∧ RqDomPrio σ ∧ + (∀ x, σ.kheap (the queue.head?) = some (.tcb x) → BlockedTsA x.tcbState = true)) + (HT.assert fun _ σ h => ⟨h.1, h.2.1, h.2.2.1, h.2.2.2.2⟩) fun _ => ?_ + refine HT.pure_bind ?_ + generalize (the queue.head?) = dest + have FK : TFrame (KI dest σ0) := KI.frame dest σ0 + have FR_keep : ∀ σ σ', TKeepA σ σ' → (KI dest σ0 σ ∧ BAs [dest] σ) → (KI dest σ0 σ' ∧ BAs [dest] σ') := + fun σ σ' k h => ⟨FK.keep σ σ' k h.1, h.2.ofTKeep k⟩ + have pre : ∀ σ, (σ = σ0 ∧ FIA σ ∧ RqDomPrio σ ∧ + (∀ x, σ.kheap dest = some (.tcb x) → BlockedTsA x.tcbState = true)) → + FIA σ ∧ RqDomPrio σ ∧ (KI dest σ0 σ ∧ BAs [dest] σ) := by + intro σ ⟨hs, hI, hD, hb⟩ + subst hs + refine ⟨hI, hD, KI.refl dest σ, fun u hu x sc e hs => ?_⟩ + have hu' : u = dest := List.mem_singleton.1 hu + subst hu' + obtain ⟨y, k, ey, ea⟩ := hI.1.blk _ x sc e (hb x e) hs + exact ⟨x, sc, e, hs, y, k, ey, ea⟩ + refine HT.bind (Q := fun _ σ => FIA σ ∧ RqDomPrio σ ∧ (KI dest σ0 σ ∧ BAs [dest] σ)) + (HT.mono (HT.ofTC (R := fun σ => KI dest σ0 σ ∧ BAs [dest] σ) (by unfold tcbNtfnDequeue; tpres) + (by unfold tcbNtfnDequeue; cpres) FR_keep) + (fun σ h => by have := pre σ h; exact ⟨this.1, this.2.2, this.2.1⟩) + (fun _ σ h => ⟨h.1, h.2.2, h.2.1⟩)) fun _ => ?_ + exact wakeTail_fia dest n badge σ0 + + +/-! ## `cancelIpc` of a receive-blocked thread, and `sendSignal` -/ + +theorem HT.ofTC' {α : Type} {m : SMonad Unit α} (h1 : TPres m) (h2 : CPres m) (R : State Unit → Prop) + (hR : ∀ σ σ', TKeepA σ σ' → R σ → R σ') : + HT (fun σ => FIA σ ∧ RqDomPrio σ ∧ R σ) m (fun _ σ => FIA σ ∧ RqDomPrio σ ∧ R σ) := + HT.mono (HT.ofTC h1 h2 R hR) (fun σ h => ⟨h.1, h.2.2, h.2.1⟩) (fun _ σ h => ⟨h.1, h.2.2, h.2.1⟩) + +instance (p : ObjRef) : CPresInst (getEndpoint (Z := Unit) p) := ⟨getSimpleKo_cells _ p⟩ +instance (p : ObjRef) (e : Endpoint) : CPresInst (setEndpoint (Z := Unit) p e) := + ⟨setSimpleKo_cells _ nsko_ep p e⟩ + +theorem IdleSt.ofTKeep {σ σ' : State Unit} (h : IdleSt σ) (k : TKeepA σ σ') : IdleSt σ' := fun x hx => by + rw [k.2.2.2.1] at hx + obtain ⟨x0, e0, -, st0⟩ := k.2.2.2.2.2.1 _ x hx + rw [st0]; exact h x0 e0 + +/-- `replyUnlinkTcb t r` keeps `FIA`, `RqDomPrio` and the frame `KI t σ0 ∧ BAs [t]`. -/ +theorem replyUnlinkTcbD (t r : ObjRef) (σ0 : State Unit) : + HT (fun σ => FIA σ ∧ RqDomPrio σ ∧ (KI t σ0 σ ∧ BAs [t] σ)) (replyUnlinkTcb (Z := Unit) t r) + (fun _ σ => FIA σ ∧ RqDomPrio σ ∧ (KI t σ0 σ ∧ BAs [t] σ)) := by + unfold replyUnlinkTcb + have FK : TFrame (KI t σ0) := KI.frame t σ0 + have keep : ∀ σ σ', TKeepA σ σ' → (KI t σ0 σ ∧ BAs [t] σ) → (KI t σ0 σ' ∧ BAs [t] σ') := + fun σ σ' k h => ⟨FK.keep σ σ' k h.1, h.2.ofTKeep k⟩ + have T : ∀ {β : Type} {m : SMonad Unit β}, TPres m → CPres m → + HT (fun σ => FIA σ ∧ RqDomPrio σ ∧ (KI t σ0 σ ∧ BAs [t] σ)) m + (fun _ σ => FIA σ ∧ RqDomPrio σ ∧ (KI t σ0 σ ∧ BAs [t] σ)) := fun h1 h2 => HT.ofTC' h1 h2 _ keep + refine HT.bind (Q := fun _ σ => FIA σ ∧ RqDomPrio σ ∧ (KI t σ0 σ ∧ BAs [t] σ)) (T (by tpres) (by cpres)) fun _ => ?_ + refine HT.bind (Q := fun _ σ => FIA σ ∧ RqDomPrio σ ∧ (KI t σ0 σ ∧ BAs [t] σ)) (T (by tpres) (by cpres)) fun _ => ?_ + refine HT.bind (Q := fun _ σ => FIA σ ∧ RqDomPrio σ ∧ (KI t σ0 σ ∧ BAs [t] σ)) (T (by tpres) (by cpres)) fun _ => ?_ + refine HT.bind (Q := fun _ σ => FIA σ ∧ RqDomPrio σ ∧ (KI t σ0 σ ∧ BAs [t] σ)) (T (by tpres) (by cpres)) fun _ => ?_ + refine HT.bind (Q := fun _ σ => FIA σ ∧ RqDomPrio σ ∧ (KI t σ0 σ ∧ BAs [t] σ)) (T (by tpres) (by cpres)) fun _ => ?_ + exact HT.mono (setThreadStateD t .inactive rfl (fun σ => KI t σ0 σ ∧ BAs [t] σ) (fun σ x hx h => by + refine ⟨⟨fun p y hy hp => ?_, h.1.2⟩, h.2.tcbUpd hx rfl⟩ + by_cases e : p = t + · exact absurd e hp + · exact h.1.1 p y (by simpa [Function.update_of_ne e] using hy) hp) + (fun σ a h => ⟨h.1, h.2.act a⟩)) + (fun σ h => h) (fun _ σ h => ⟨h.1, h.2.1, h.2.2.1⟩) + +/-- The thread's TCB (if any) is receive-blocked. -/ +def RecvSt (c : ObjRef) (σ : State Unit) : Prop := + ∀ x, σ.kheap c = some (.tcb x) → ∃ ep rp d, x.tcbState = .blockedOnReceive ep rp d + +/-- **`cancel_ipc` of a receive-blocked thread keeps `FIA`.** -/ +theorem cancelIpcRecv_fia (t : ObjRef) (σ0 : State Unit) : + HT (fun σ => FIA σ ∧ RqDomPrio σ ∧ (KI t σ0 σ ∧ BAs [t] σ) ∧ RecvSt t σ) (cancelIpc (Z := Unit) t) + (fun _ σ => FIA σ ∧ RqDomPrio σ ∧ (KI t σ0 σ ∧ BAs [t] σ)) := by + unfold cancelIpc + have FK : TFrame (KI t σ0) := KI.frame t σ0 + have keep : ∀ σ σ', TKeepA σ σ' → (KI t σ0 σ ∧ BAs [t] σ) → (KI t σ0 σ' ∧ BAs [t] σ') := + fun σ σ' k h => ⟨FK.keep σ σ' k h.1, h.2.ofTKeep k⟩ + refine HT.bind (Q := fun st σ => (FIA σ ∧ RqDomPrio σ ∧ (KI t σ0 σ ∧ BAs [t] σ)) ∧ + ∃ ep rp d, st = .blockedOnReceive ep rp d) ?_ fun st => ?_ + · show HT _ (threadGet (Z := Unit) Tcb.tcbState t) _ + exact HT.threadGet _ t fun σ hσ x hx => ⟨⟨hσ.1, hσ.2.1, hσ.2.2.1⟩, hσ.2.2.2 x hx⟩ + refine HT.bind (Q := fun _ σ => (FIA σ ∧ RqDomPrio σ ∧ (KI t σ0 σ ∧ BAs [t] σ)) ∧ + ∃ ep rp d, st = .blockedOnReceive ep rp d) ?_ fun _ => ?_ + · intro σ hσ r hr + obtain ⟨x, hx, hr2⟩ := threadSet_spec hr + rw [hr2] + exact ⟨⟨hσ.1.1.ofTcbUpd hx (x' := { x with tcbFault := none }) rfl (fun _ => rfl), + hσ.1.2.1.ofTcbRepl hx rfl rfl, + ⟨FK.repl σ t x { x with tcbFault := none } hx rfl hσ.1.2.2.1, hσ.1.2.2.2.tcbUpd hx rfl⟩⟩, hσ.2⟩ + cases st with + | blockedOnReceive ep rp d => + dsimp only + unfold blockedCancelIpc getBlockingObject + refine HT.bind (Q := fun _ σ => FIA σ ∧ RqDomPrio σ ∧ (KI t σ0 σ ∧ BAs [t] σ)) + (HT.assertOpt _ fun a _ σ hσ => hσ.1) fun epptr => ?_ + refine HT.bind (Q := fun _ σ => FIA σ ∧ RqDomPrio σ ∧ (KI t σ0 σ ∧ BAs [t] σ)) + (HT.ofTC' (by unfold tcbEpDequeue; tpres) (by unfold tcbEpDequeue; cpres) _ keep) fun _ => ?_ + cases rp with + | none => + dsimp only + exact HT.mono (setThreadStateD t .inactive rfl (fun σ => KI t σ0 σ ∧ BAs [t] σ) (fun σ x hx h => by + refine ⟨⟨fun p y hy hp => ?_, h.1.2⟩, h.2.tcbUpd hx rfl⟩ + by_cases e : p = t + · exact absurd e hp + · exact h.1.1 p y (by simpa [Function.update_of_ne e] using hy) hp) + (fun σ a h => ⟨h.1, h.2.act a⟩)) (fun σ h => h) (fun _ σ h => ⟨h.1, h.2.1, h.2.2.1⟩) + | some r => + dsimp only + refine HT.bind (Q := fun _ σ => FIA σ ∧ RqDomPrio σ ∧ (KI t σ0 σ ∧ BAs [t] σ)) + (replyUnlinkTcbD t r σ0) fun _ => ?_ + exact HT.mono (setThreadStateD t .inactive rfl (fun σ => KI t σ0 σ ∧ BAs [t] σ) (fun σ x hx h => by + refine ⟨⟨fun p y hy hp => ?_, h.1.2⟩, h.2.tcbUpd hx rfl⟩ + by_cases e : p = t + · exact absurd e hp + · exact h.1.1 p y (by simpa [Function.update_of_ne e] using hy) hp) + (fun σ a h => ⟨h.1, h.2.act a⟩)) (fun σ h => h) (fun _ σ h => ⟨h.1, h.2.1, h.2.2.1⟩) + | _ => exact fun σ hσ _ _ => absurd hσ.2 (by rintro ⟨ep, rp, d, h⟩; cases h) + +/-- A notification's waiting-queue members are TCBs blocked on it. -/ +def SigQ (n : ObjRef) (σ : State Unit) : Prop := + ∀ x q t, σ.kheap n = some (.notification x) → x.ntfnObj = .waitingNtfn q → t ∈ q → + ∃ y, σ.kheap t = some (.tcb y) ∧ y.tcbState = .blockedOnNotification n + +theorem HT.ntfnSet {m : SMonad Unit Unit} (h : TPres m) (P : State Unit → Prop) + (hP : ∀ σ, P σ → FIA σ ∧ IdleSt σ) : HT P m (fun _ σ => FIA σ ∧ IdleSt σ) := + fun σ hσ r hr => ⟨(hP σ hσ).1.ofTKeep (h σ r hr), (hP σ hσ).2.ofTKeep (h σ r hr)⟩ + +/-- **`send_signal` keeps `FIA` and `IdleSt`**, given the waiting queue's members are blocked on the notification. -/ +theorem sendSignal_fia (n : ObjRef) (badge : Badge) (σ0 : State Unit) : + HT (fun σ => σ = σ0 ∧ FIA σ ∧ RqDomPrio σ ∧ IdleSt σ ∧ SigQ n σ) (sendSignal (Z := Unit) n badge) + (fun _ σ => FIA σ ∧ IdleSt σ) := by + unfold sendSignal + refine HT.bind (Q := fun ntfn σ => (σ = σ0 ∧ FIA σ ∧ RqDomPrio σ ∧ IdleSt σ ∧ SigQ n σ) ∧ + σ.kheap n = some (.notification ntfn)) ?_ fun ntfn => ?_ + · intro σ hσ r hr + obtain ⟨hk, hs⟩ := getNotification_mem hr + obtain ⟨a, s'⟩ := r + simp only at hk hs ⊢ + subst hs + exact ⟨hσ, hk⟩ + have nsArm : ∀ (o : Notification), HT (fun σ => (σ = σ0 ∧ FIA σ ∧ RqDomPrio σ ∧ IdleSt σ ∧ SigQ n σ) ∧ + σ.kheap n = some (.notification ntfn)) + (setNotification (Z := Unit) n o) (fun _ σ => FIA σ ∧ IdleSt σ) := fun o => + HT.ntfnSet (by tpres) _ fun σ h => ⟨h.1.2.1, h.1.2.2.2.1⟩ + rcases ntfn with ⟨o, bt, nsc⟩ + cases o with + | idleNtfn => + cases bt with + | none => exact nsArm _ + | some tcb => + dsimp only + refine HT.bind (Q := fun st σ => ((σ = σ0 ∧ FIA σ ∧ RqDomPrio σ ∧ IdleSt σ ∧ SigQ n σ) ∧ + σ.kheap n = some (.notification { ntfnObj := .idleNtfn, ntfnBoundTcb := some tcb, ntfnSc := nsc })) ∧ + ∃ x, σ.kheap tcb = some (.tcb x) ∧ x.tcbState = st) ?_ fun st => ?_ + · show HT _ (threadGet (Z := Unit) Tcb.tcbState tcb) _ + exact HT.threadGet _ tcb fun σ hσ x hx => ⟨hσ, x, hx, rfl⟩ + refine HT.ite (fun hc => ?_) (fun hc => HT.mono (nsArm _) (fun σ h => h.1) (fun _ _ h => h)) + cases st with + | blockedOnReceive ep rp d => + intro σ hσ r hr + obtain ⟨⟨⟨hs, hI, hD, hid, hq⟩, hk⟩, x, hx, hst⟩ := hσ + subst hs + have hBA : BAs [tcb] σ := fun u hu y sc e hsc => by + have hu' : u = tcb := List.mem_singleton.1 hu + subst hu' + rw [hx] at e + have := KernelObject.tcb.inj (Option.some.inj e) + subst this + obtain ⟨y', k, ey, ea⟩ := hI.1.blk u x sc hx (by rw [hst]; rfl) hsc + exact ⟨x, sc, hx, hsc, y', k, ey, ea⟩ + have hrecv : RecvSt tcb σ := fun y hy => by + rw [hx] at hy + have := KernelObject.tcb.inj (Option.some.inj hy) + subst this + exact ⟨ep, rp, d, hst⟩ + obtain ⟨⟨u, σ1⟩, h1, h2⟩ := mem_bindA.1 hr + have hC := cancelIpcRecv_fia tcb σ σ ⟨hI, hD, ⟨KI.refl tcb σ, hBA⟩, hrecv⟩ ⟨u, σ1⟩ h1 + obtain ⟨hF, hKI⟩ := wakeTail_fia tcb n badge σ σ1 hC r h2 + refine ⟨hF, hid.ofKI hKI ?_⟩ + intro he + rw [he] at hx + have := hid x hx + rw [hst] at this + cases this + | _ => simp [receiveBlocked] at hc + | waitingNtfn q => + dsimp only + rcases q with _ | ⟨h, tl⟩ + · intro σ hσ r hr + unfold updateWaitingNtfn at hr + obtain ⟨x, hx, -⟩ := mem_bindA.1 hr + exact absurd rfl hx.1 + · intro σ hσ r hr + obtain ⟨⟨hs, hI, hD, hid, hq⟩, hk⟩ := hσ + subst hs + obtain ⟨y, hy, hst⟩ := hq _ (h :: tl) h hk rfl (List.mem_cons_self ..) + have hb : ∀ x, σ.kheap (the (h :: tl).head?) = some (.tcb x) → BlockedTsA x.tcbState = true := fun x hx => by + change σ.kheap h = some (.tcb x) at hx + rw [hy] at hx + have := KernelObject.tcb.inj (Option.some.inj hx) + subst this + rw [hst]; rfl + obtain ⟨hF, hKI⟩ := updateWaitingNtfn_fia n (h :: tl) badge σ σ ⟨rfl, hI, hD, List.cons_ne_nil _ _, hb⟩ r hr + refine ⟨hF, hid.ofKI hKI ?_⟩ + intro he + change h = σ.idleThread at he + rw [he] at hy + have := hid y hy + rw [hst] at this + cases this + | activeNtfn b' => exact nsArm _ + + +/-! ## The timer fires: `hAwk` and `IdleSt` at every post-fire state -/ + +/-- **F8 and the idle thread across `fl4TimerFires`** (queue items 1 and 3): from `FIA` and `IdleSt` at `σ0`, given at +every post-fire state `RqDomPrio` and that notification wait queues hold blocked threads (`SigQ`), `FIA` and `IdleSt` +at every post-fire state. -/ +theorem fires_fia {σ0 : State Unit} (hI : FIA σ0) (hid : IdleSt σ0) + (hD : ∀ σ1, fl4TimerFires σ0 σ1 → RqDomPrio σ1) + (hQ : ∀ σ1 n, fl4TimerFires σ0 σ1 → SigQ n σ1) : + ∀ σ1, fl4TimerFires σ0 σ1 → FIA σ1 ∧ IdleSt σ1 := by + intro σ1 hf + induction hf with + | refl => exact ⟨hI, hid⟩ + | @tail b c hab hbc ih => + obtain ⟨σm, hm, hc⟩ := hbc + have hm' : FIA σm ∧ IdleSt σm := by + rcases hm with e | ⟨n, bd, hs⟩ + · subst e; exact ih + · have hmem : ((), σm) ∈ (sendSignal (Z := Unit) n bd b).1 := by rw [hs]; rfl + exact sendSignal_fia n bd b b ⟨rfl, ih.1, hD b hab, ih.2, hQ b n hab⟩ ((), σm) hmem + subst hc + exact ⟨hm'.1.ofSame rfl rfl rfl rfl rfl rfl rfl, fun x hx => hm'.2 x hx⟩ + +/-- **`hAwk`** for `exit_spec`/`exit_noSwitch`: `FIA` after every `fl4TimerFires` outcome. -/ +theorem hAwk_of {σ0 : State Unit} (hI : FIA σ0) (hid : IdleSt σ0) + (hD : ∀ σ1, fl4TimerFires σ0 σ1 → RqDomPrio σ1) + (hQ : ∀ σ1 n, fl4TimerFires σ0 σ1 → SigQ n σ1) : ∀ σ1, fl4TimerFires σ0 σ1 → FIA σ1 := + fun σ1 h => (fires_fia hI hid hD hQ σ1 h).1 + +/-- **`hidle`** for `exit_spec`: the idle thread's TCB is in `idleThreadState` after `fl4Schedule`. -/ +theorem hidle_of (limit : Nat) (timer : State Unit → Fl4TimerView) {σ0 τ1 : State Unit} (hI : FIA σ0) + (hid : IdleSt σ0) (hD : ∀ σ1, fl4TimerFires σ0 σ1 → RqDomPrio σ1) + (hQ : ∀ σ1 n, fl4TimerFires σ0 σ1 → SigQ n σ1) + (h : ((), τ1) ∈ (fl4Schedule (Z := Unit) limit timer σ0).1) : + ∀ x, τ1.kheap τ1.idleThread = some (.tcb x) → x.tcbState = .idleThreadState := + fl4Schedule_idleSt limit timer (fun σ1 hf => (fires_fia hI hid hD hQ σ1 hf).2) h + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3SigSwA.lean b/proofs/Fl4ProofsA64/P3/Stage3SigSwA.lean new file mode 100644 index 000000000..09e49526d --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SigSwA.lean @@ -0,0 +1,351 @@ +/- +P3 (aarch64, A64-P3 Stage 3, kernel-entry composition, step 1): **`SwA` kept by one `send_signal`**. + +`sendSignal_swa`: `SwA` (a pending switch target is `schedulable`) survives `send_signal`, given that the +notification's waiting threads are blocked (`SigQ`). Until the final `possible_switch_to`, the pending target is +`schedulable` and is not the woken thread (`SwN`): the woken thread was blocked, so it was not the target. +Spec only; no premise beyond `SigQ`. +-/ +import Fl4ProofsA64.P3.Stage3ExitAtA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Classical + +noncomputable section + +/-- A pending switch target is `schedulable` and is not `d`. -/ +def SwN (d : ObjRef) (σ : State Unit) : Prop := + ∀ t, σ.schedulerAction = .switchThread t → schedulable t σ = true ∧ t ≠ d + +theorem SwN.swa {d : ObjRef} {σ : State Unit} (h : SwN d σ) : SwA σ := fun t e => (h t e).1 + +theorem schedulable_tkeep {σ σ' : State Unit} (k : TKeepA σ σ') {t : ObjRef} (h : schedulable t σ = true) : + schedulable t σ' = true := by + obtain ⟨⟨x, sc, e, es, y, n, ey, ea⟩, nr, -⟩ := schedulable_actA h + obtain ⟨-, -, x0, e0, hrun⟩ := schedulable_actA h + obtain ⟨-, kr, -, -, -, kb, kf, ks, -⟩ := k + obtain ⟨x', e', es'⟩ := kf t x e + obtain ⟨y', ey', ea'⟩ := ks sc y n ey + obtain ⟨x1, e1, -, st1⟩ := kb t x' e' + refine schedulable_of_act ⟨x', sc, e', es'.trans es, y', n, ey', ea'.trans ea⟩ (by rw [kr]; exact nr) + ⟨x', e', ?_⟩ + rw [e0] at e1 + have := KernelObject.tcb.inj (Option.some.inj e1) + subst this + rw [st1]; exact hrun + +theorem SwN.tkeep {d : ObjRef} {σ σ' : State Unit} (k : TKeepA σ σ') (h : SwN d σ) : SwN d σ' := fun t e => by + rw [k.2.2.2.2.1] at e + exact ⟨schedulable_tkeep k (h t e).1, (h t e).2⟩ + +theorem SwA.tkeep {σ σ' : State Unit} (k : TKeepA σ σ') (h : SwA σ) : SwA σ' := fun t e => by + rw [k.2.2.2.2.1] at e + exact schedulable_tkeep k (h t e) + +theorem HT.swn {α : Type} {m : SMonad Unit α} (h : TPres m) (d : ObjRef) : HT (SwN d) m (fun _ => SwN d) := + fun σ hσ r hr => hσ.tkeep (h σ r hr) + +/-- Replacing the TCB of `d` keeps `SwN d`. -/ +theorem SwN.updTcb {d : ObjRef} {σ : State Unit} {x x' : Tcb} (h : SwN d σ) (hx : σ.kheap d = some (.tcb x)) : + SwN d { σ with kheap := Function.update σ.kheap d (some (.tcb x')) } := fun t e => by + obtain ⟨hs, ne⟩ := h t e + refine ⟨?_, ne⟩ + obtain ⟨⟨z, sc, ez, es, y, n, ey, ea⟩, nr, w, ew, hrun⟩ := schedulable_actA hs + have nsc : sc ≠ d := fun c => by rw [c, hx] at ey; cases ey + refine schedulable_of_act ⟨z, sc, ?_, es, y, n, ?_, ea⟩ nr ⟨w, ?_, hrun⟩ + · simpa [Function.update_of_ne ne] using ez + · simpa [Function.update_of_ne nsc] using ey + · simpa [Function.update_of_ne ne] using ew + +theorem threadSet_swn (d : ObjRef) (f : Tcb → Tcb) : HT (SwN d) (threadSet (Z := Unit) f d) (fun _ => SwN d) := + fun σ hσ r hr => by + obtain ⟨x, hx, e⟩ := threadSet_spec hr + rw [e]; exact hσ.updTcb hx + +theorem setChoose_swn (d : ObjRef) : HT (SwN d) (setSchedulerAction (Z := Unit) .chooseNewThread) (fun _ => SwN d) := by + unfold setSchedulerAction + exact HT.modify _ fun σ _ t e => by cases e + +theorem setThreadState_swn (d : ObjRef) (ts : ThreadState) : + HT (SwN d) (setThreadState (Z := Unit) d ts) (fun _ => SwN d) := by + unfold setThreadState setThreadStateAct + refine HT.bind (Q := fun _ => SwN d) (threadSet_swn d _) fun _ => ?_ + refine HT.bind (Q := fun _ => SwN d) (HT.gets _ fun σ h => h) fun cur => ?_ + refine HT.bind (Q := fun _ => SwN d) (HT.gets _ fun σ h => h) fun act => ?_ + refine HT.bind (Q := fun _ => SwN d) (HT.gets _ fun σ h => h) fun sch => ?_ + exact HT.when (fun _ => setChoose_swn d) (fun _ σ h => h) + +theorem replyUnlinkTcb_swn (d r : ObjRef) : HT (SwN d) (replyUnlinkTcb (Z := Unit) d r) (fun _ => SwN d) := by + unfold replyUnlinkTcb + refine HT.bind (Q := fun _ => SwN d) (HT.swn (by tpres) d) fun _ => ?_ + refine HT.bind (Q := fun _ => SwN d) (HT.swn (by tpres) d) fun _ => ?_ + refine HT.bind (Q := fun _ => SwN d) (HT.swn (by tpres) d) fun _ => ?_ + refine HT.bind (Q := fun _ => SwN d) (HT.swn (by tpres) d) fun _ => ?_ + refine HT.bind (Q := fun _ => SwN d) (HT.swn (by tpres) d) fun _ => ?_ + exact setThreadState_swn d _ + +theorem cancelIpcRecv_swn (d : ObjRef) : + HT (fun σ => SwN d σ ∧ RecvSt d σ) (cancelIpc (Z := Unit) d) (fun _ => SwN d) := by + unfold cancelIpc + refine HT.bind (Q := fun st σ => SwN d σ ∧ ∃ ep rp q, st = .blockedOnReceive ep rp q) ?_ fun st => ?_ + · show HT _ (threadGet (Z := Unit) Tcb.tcbState d) _ + exact HT.threadGet _ d fun σ hσ x hx => ⟨hσ.1, hσ.2 x hx⟩ + refine HT.bind (Q := fun _ σ => SwN d σ ∧ ∃ ep rp q, st = .blockedOnReceive ep rp q) + (fun σ hσ r hr => ⟨threadSet_swn d _ σ hσ.1 r hr, hσ.2⟩) fun _ => ?_ + cases st with + | blockedOnReceive ep rp q => + dsimp only + refine HT.mono (P := SwN d) (Q := fun _ => SwN d) ?_ (fun σ h => h.1) (fun _ _ h => h) + unfold blockedCancelIpc getBlockingObject + refine HT.bind (Q := fun _ => SwN d) (HT.assertOpt _ fun a _ σ h => h) fun p => ?_ + refine HT.bind (Q := fun _ => SwN d) (HT.swn (by unfold tcbEpDequeue; tpres) d) fun _ => ?_ + cases rp with + | none => exact setThreadState_swn d _ + | some r => + dsimp only + exact HT.bind (Q := fun _ => SwN d) (replyUnlinkTcb_swn d r) fun _ => setThreadState_swn d _ + | _ => exact fun σ hσ _ _ => absurd hσ.2 (by rintro ⟨ep, rp, q, h⟩; cases h) + +/-- `postpone` of an SC bound to `d` keeps `SwN d`: only `d` enters the release queue. -/ +theorem postpone_swn (d p : ObjRef) : + HT (fun σ => SwN d σ ∧ ∃ y k, σ.kheap p = some (.schedContext y k) ∧ y.scTcb = some d) + (postpone (Z := Unit) p) (fun _ => SwN d) := by + unfold postpone + refine HT.bind (Q := fun o σ => SwN d σ ∧ o = some d) ?_ fun o => ?_ + · intro σ ⟨hs, y, k, hk, hv⟩ r hr + obtain ⟨e, y', k', hk', hv'⟩ := getScObjRef_mem hr + obtain ⟨a, s'⟩ := r + simp only at e hv' ⊢ + subst e + rw [hk] at hk' + have := Option.some.inj hk' + injection this with e1 e2 + subst e1 + exact ⟨hs, by rw [hv']; exact hv⟩ + refine HT.bind (Q := fun t σ => SwN d σ ∧ t = d) + (HT.assertOpt _ fun a ha σ h => ⟨h.1, by rw [h.2] at ha; exact (Option.some.inj ha).symm⟩) fun t => ?_ + have key : HT (SwN d) (do tcbSchedAction (Z := Unit) tcbSchedDequeue d + tcbReleaseEnqueue d + NonDet.modify fun s : State Unit => { s with reprogramTimer := true }) + (fun _ => SwN d) := by + refine HT.bind (Q := fun _ => SwN d) (tcbSchedAction_keep _ d (SwN d) fun σ g h => h) fun _ => ?_ + refine HT.bind (Q := fun _ => SwN d) ?_ fun _ => HT.modify _ fun σ h => h + unfold tcbReleaseEnqueue + refine HT.bind (Q := fun q σ => SwN d σ ∧ q = σ.releaseQueue) (HT.gets _ fun σ h => ⟨h, rfl⟩) fun q => ?_ + refine HT.bind (Q := fun q' σ => (SwN d σ ∧ q = σ.releaseQueue) ∧ ∀ y ∈ q', y = d ∨ y ∈ q) ?_ fun q' => ?_ + · intro σ hσ r hr + unfold readyTimesAppend at hr + obtain ⟨hs, hm⟩ := orderedInsert_spec _ _ _ _ hr + obtain ⟨a, s'⟩ := r + simp only at hs hm ⊢ + subst hs + exact ⟨hσ, hm⟩ + refine HT.bind (Q := fun _ => SwN d) ?_ fun _ => ?_ + · unfold setReleaseQueue + refine HT.modify _ fun σ ⟨⟨hs, hq⟩, hm⟩ t e => ?_ + obtain ⟨h1, ne⟩ := hs t e + obtain ⟨a, nr, w⟩ := schedulable_actA h1 + refine ⟨schedulable_of_act (a.ofKh rfl) (fun m => ?_) w, ne⟩ + rcases hm t m with e' | e' + · exact ne e' + · exact nr (hq ▸ e') + · exact HT.when (fun _ => HT.modify _ fun σ h => h) (fun _ σ h => h) + intro σ h r hr + rw [h.2] at hr + exact key σ h.1 r hr + +/-- `maybe_donate_sc` to `d` keeps `SwN d`. -/ +theorem maybeDonateSc_swn (d n : ObjRef) : HT (SwN d) (maybeDonateSc (Z := Unit) d n) (fun _ => SwN d) := by + unfold maybeDonateSc + refine HT.bind (Q := fun _ => SwN d) (HT.swn (by unfold getTcbObjRef; tpres) d) fun scOpt => ?_ + refine HT.when (fun _ => ?_) (fun _ σ h => h) + refine HT.bind (Q := fun _ => SwN d) (HT.swn (by unfold getNtfnObjRef getSkObjRef; tpres) d) fun nsc => ?_ + cases nsc with + | none => exact HT.pure () (fun σ h => h) + | some p => + show HT _ ((do let scTcb ← getScObjRef SchedContext.scTcb p + NonDet.when (scTcb = none) (do schedContextDonate p d; schedContextResume p))) _ + refine HT.bind (Q := fun o σ => SwN d σ ∧ (o = none → ∀ y k, σ.kheap p = some (.schedContext y k) → y.scTcb = none)) + ?_ fun o => ?_ + · intro σ hσ r hr + obtain ⟨e, y, k, hk, hv⟩ := getScObjRef_mem hr + obtain ⟨a, s'⟩ := r + simp only at e hv ⊢ + subst e + refine ⟨hσ, fun hn y' k' hk' => ?_⟩ + rw [hk] at hk' + have := Option.some.inj hk' + injection this with e1 e2 + subst e1 + rw [← hv]; exact hn + refine HT.when (fun hn => ?_) (fun _ σ h => h.1) + refine HT.bind (Q := fun _ σ => SwN d σ ∧ ∃ y k, σ.kheap p = some (.schedContext y k) ∧ y.scTcb = some d) + ?_ fun _ => ?_ + · intro σ hσ r hr + unfold schedContextDonate at hr + obtain ⟨⟨fromOpt, σ1⟩, h1, h2⟩ := mem_bindA.1 hr + obtain ⟨e1, y, k, hk, hv⟩ := getScObjRef_mem h1 + have e1' : σ1 = σ := e1 + have hnone : fromOpt = none := by + have : fromOpt = y.scTcb := hv + rw [this]; exact hσ.2 hn y k hk + rw [e1'] at h2 + obtain ⟨⟨u, σ2⟩, h3, h4⟩ := mem_bindA.1 h2 + have e3 : ((u, σ2) : Unit × State Unit) = ((), σ) := by + unfold NonDet.when at h3 + rw [if_neg (by simp [hnone])] at h3 + exact h3 + have hσ2 : σ2 = σ := (Prod.mk.inj e3).2 + rw [hσ2] at h4 + obtain ⟨⟨u2, σ3⟩, h5, h6⟩ := mem_bindA.1 h4 + have h5' : (u2, σ3) ∈ (setScObjRef (Z := Unit) scTcbUpdate p (some d) σ).1 := h5 + obtain ⟨y', m, hk', hσ3⟩ := setScTcb_spec h5' + have k3 : TKeepA σ σ3 := (by tpres : TPres (setScObjRef (Z := Unit) scTcbUpdate p (some d))) σ _ h5' + have s3 : SwN d σ3 := hσ.1.tkeep k3 + obtain ⟨x, hx, hr2⟩ := threadSet_spec (r := r) (f := tcbSchedContextUpdate fun _ => some p) (c := d) h6 + have hne : p ≠ d := by + intro c + have : σ3.kheap p = some (.schedContext (scTcbUpdate (fun _ => some d) y') m) := by + have e := hσ3 + dsimp only at e + rw [e]; simp + rw [c, hx] at this + cases this + rw [hr2] + refine ⟨s3.updTcb hx, scTcbUpdate (fun _ => some d) y', m, ?_, rfl⟩ + simp only + rw [Function.update_of_ne hne] + have e := hσ3 + dsimp only at e + rw [e]; simp + unfold schedContextResume + have T : ∀ {β : Type} {m : SMonad Unit β} {P : State Unit → Prop}, (∀ σ, P σ → ∀ r ∈ (m σ).1, r.2 = σ) → + HT P m (fun _ => P) := fun h σ hσ r hr => by rw [h σ hσ r hr]; exact hσ + let Pc : State Unit → Prop := fun σ => SwN d σ ∧ ∃ y k, σ.kheap p = some (.schedContext y k) ∧ y.scTcb = some d + refine HT.bind (Q := fun sc σ => Pc σ ∧ sc.scTcb = some d) ?_ fun sc => ?_ + · intro σ hσ r hr + obtain ⟨e, m, hk⟩ := getSchedContext_mem hr + obtain ⟨a, s'⟩ := r + simp only at e hk ⊢ + subst e + refine ⟨hσ, ?_⟩ + obtain ⟨-, y, k, hk', hv⟩ := hσ + rw [hk] at hk' + have := Option.some.inj hk' + injection this with e1 e2 + subst e1 + exact hv + refine HT.bind (Q := fun _ => Pc) (HT.assertOpt _ fun a _ σ h => h.1) fun t => ?_ + refine HT.bind (Q := fun _ => Pc) (HT.gets _ fun σ h => h) fun sched => ?_ + refine HT.when (fun _ => ?_) (fun _ σ h => h.1) + refine HT.bind (Q := fun _ => Pc) (HT.threadGet _ t fun σ h x hx => h) fun ts => ?_ + refine HT.bind (Q := fun _ => Pc) (HT.getsThe _ fun σ h a _ => h) fun ready => ?_ + refine HT.bind (Q := fun _ => Pc) (HT.getsThe _ fun σ h a _ => h) fun suf => ?_ + refine HT.when (fun _ => ?_) (fun _ σ h => h.1) + refine HT.bind (Q := fun _ => Pc) (HT.threadGet _ t fun σ h x hx => h) fun dom => ?_ + refine HT.bind (Q := fun _ => Pc) (HT.threadGet _ t fun σ h x hx => h) fun prio => ?_ + refine HT.bind (Q := fun _ => Pc) ?_ fun q => ?_ + · unfold getTcbQueue + exact HT.bind (Q := fun _ => Pc) (HT.gets _ fun σ h => h) fun qs => HT.pure _ fun σ h => h + refine HT.bind (Q := fun _ => Pc) (HT.assert fun _ σ h => h) fun _ => ?_ + exact postpone_swn d p + +/-- The wake-up tail of `update_waiting_ntfn` and the bound-thread arm. -/ +theorem wakeTail_swa (d n : ObjRef) (badge : Badge) : + HT (SwN d) + (do setThreadState (Z := Unit) d .running + asUser d (setRegister badge_register badge) + maybeDonateSc d n + let sched ← NonDet.gets (schedulable d) + NonDet.when sched <| possibleSwitchTo d + let scopt ← getTcbObjRef Tcb.tcbSchedContext d + ifSporadicCurScTestRefillUnblockCheck scopt) + (fun _ => SwA) := by + refine HT.bind (Q := fun _ => SwN d) (setThreadState_swn d _) fun _ => ?_ + refine HT.bind (Q := fun _ => SwN d) (HT.swn (by tpres) d) fun _ => ?_ + refine HT.bind (Q := fun _ => SwN d) (maybeDonateSc_swn d n) fun _ => ?_ + refine HT.bind (Q := fun sched σ => SwA σ ∧ sched = schedulable d σ) + (HT.gets _ fun σ h => ⟨h.swa, rfl⟩) fun sched => ?_ + refine HT.bind (Q := fun _ => SwA) ?_ fun _ => ?_ + · refine HT.when (fun hs => ?_) (fun _ σ h => h.1) + exact HT.mono (possibleSwitchTo_swa d) (fun σ h => ⟨h.1, by rw [← h.2]; exact hs⟩) (fun _ _ h => h) + have T : ∀ {β : Type} {m : SMonad Unit β}, TPres m → HT SwA m (fun _ => SwA) := fun h σ hσ r hr => + SwA.tkeep (h σ r hr) hσ + refine HT.bind (Q := fun _ => SwA) (T (by unfold getTcbObjRef; tpres)) fun _ => ?_ + exact T (by unfold ifSporadicCurScTestRefillUnblockCheck ifCondRefillUnblockCheck; tpres) + +theorem SwN.of_blocked {d : ObjRef} {σ : State Unit} (h : SwA σ) + (hb : ∀ x, σ.kheap d = some (.tcb x) → runnable x.tcbState ≠ true) : SwN d σ := fun t e => by + refine ⟨h t e, fun c => ?_⟩ + obtain ⟨-, -, x, ex, hr⟩ := schedulable_actA (h t e) + rw [c] at ex + exact hb x ex hr + +/-- **`SwA` kept by `send_signal`** (module docstring). -/ +theorem sendSignal_swa (n : ObjRef) (badge : Badge) : + HT (fun σ => SwA σ ∧ SigQ n σ) (sendSignal (Z := Unit) n badge) (fun _ => SwA) := by + unfold sendSignal + refine HT.bind (Q := fun ntfn σ => (SwA σ ∧ SigQ n σ) ∧ σ.kheap n = some (.notification ntfn)) ?_ + fun ntfn => ?_ + · intro σ hσ r hr + obtain ⟨hk, hs⟩ := getNotification_mem hr + obtain ⟨a, s'⟩ := r + simp only at hk hs ⊢ + subst hs + exact ⟨hσ, hk⟩ + have ns : ∀ (o : Notification), HT (fun σ => (SwA σ ∧ SigQ n σ) ∧ σ.kheap n = some (.notification ntfn)) + (setNotification (Z := Unit) n o) (fun _ => SwA) := fun o σ hσ r hr => + SwA.tkeep ((by tpres : TPres (setNotification (Z := Unit) n o)) σ r hr) hσ.1.1 + rcases ntfn with ⟨o, bt, nsc⟩ + cases o with + | idleNtfn => + cases bt with + | none => exact ns _ + | some tcb => + dsimp only + refine HT.bind (Q := fun st σ => ((SwA σ ∧ SigQ n σ) ∧ + σ.kheap n = some (.notification { ntfnObj := .idleNtfn, ntfnBoundTcb := some tcb, ntfnSc := nsc })) ∧ + ∃ x, σ.kheap tcb = some (.tcb x) ∧ x.tcbState = st) ?_ fun st => ?_ + · show HT _ (threadGet (Z := Unit) Tcb.tcbState tcb) _ + exact HT.threadGet _ tcb fun σ hσ x hx => ⟨hσ, x, hx, rfl⟩ + refine HT.ite (fun hc => ?_) (fun hc => HT.mono (ns _) (fun σ h => h.1) (fun _ _ h => h)) + cases st with + | blockedOnReceive ep rp q => + refine HT.bind (Q := fun _ => SwN tcb) (HT.mono (cancelIpcRecv_swn tcb) (fun σ h => ?_) (fun _ _ h => h)) + fun _ => wakeTail_swa tcb n badge + obtain ⟨⟨⟨hS, -⟩, -⟩, x, hx, hst⟩ := h + refine ⟨SwN.of_blocked hS fun y hy => ?_, fun y hy => ?_⟩ + all_goals + rw [hx] at hy + have := KernelObject.tcb.inj (Option.some.inj hy) + subst this + · rw [hst]; simp [runnable] + · exact ⟨ep, rp, q, hst⟩ + | _ => simp [receiveBlocked] at hc + | waitingNtfn q => + dsimp only + unfold updateWaitingNtfn + refine HT.bind (Q := fun _ σ => ((SwA σ ∧ SigQ n σ) ∧ + σ.kheap n = some (.notification { ntfnObj := .waitingNtfn q, ntfnBoundTcb := bt, ntfnSc := nsc })) ∧ q ≠ []) + (HT.assert fun h σ hσ => ⟨hσ, h⟩) fun _ => ?_ + refine HT.pure_bind ?_ + refine HT.bind (Q := fun _ => SwN (the q.head?)) + (HT.mono (HT.swn (by unfold tcbNtfnDequeue; tpres) (the q.head?)) (fun σ h => ?_) (fun _ _ h => h)) + fun _ => wakeTail_swa _ n badge + obtain ⟨⟨⟨hS, hq⟩, hk⟩, hne⟩ := h + have hm : the q.head? ∈ q := by + cases q with + | nil => exact absurd rfl hne + | cons a l => exact List.mem_cons_self + obtain ⟨y, hy, hst⟩ := hq _ q _ hk rfl hm + refine SwN.of_blocked hS fun x hx => ?_ + rw [hy] at hx + have := KernelObject.tcb.inj (Option.some.inj hx) + subst this + rw [hst]; simp [runnable] + | activeNtfn b => exact ns _ + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3SigVmArmA.lean b/proofs/Fl4ProofsA64/P3/Stage3SigVmArmA.lean new file mode 100644 index 000000000..82c84e800 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SigVmArmA.lean @@ -0,0 +1,68 @@ +/- +P3 (aarch64, A64-P3 asmsig): **`send_signal` on an idle notification whose bound thread runs a guest, at the record** +(`JStage3A.sigVmA`), in the shape of `JStage3A.irqSignalQuietA`: the fires loop's and the interrupt entry's +guest-running arm. From `send_signal_vmA`, the stall being a no-op on one core. + +Named premises: [P] `ArchTotal`, `hmb`, `hmax`, `htu`; `hCs`; [S] `numNodes = 1`. + +New module. +-/ +import Fl4ProofsA64.P3.Stage3SignalVmA +import Fl4ProofsA64.P3.Stage3SigWakeAnyA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s : S} {a : A} {σ : State Unit} {m : McState} + +/-- **The guest-running signal arm at the record** (module docstring). -/ +theorem JStage3A.sigVmA (hA : P0.ArchTotal ar) + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p : U64} (hp : p ∈ N) {x : Notification} (hk : σ.kheap (specPtr p) = some (.notification x)) + (hvm : VmSigA σ x) (badge : U64) : + notification.send_signal st ar s a p badge ⦃ r => r.2 = a ∧ ∃ x', + sendSignal (Z := Unit) (specPtr p) badge.bv σ = ({((), updK σ (specPtr p) (.notification x'))}, False) ∧ + (SysL st H Z.Cs OS ia ie r.1 → SymL st H Z.Cs r.1 → + BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac r.1 → + ∃ Q Rl, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) r.1 a (updK σ (specPtr p) (.notification x')) m) ⦄ := by + obtain ⟨hidle, t, y, hbound, hy, hrun⟩ := hvm + have hcore : m.curCore < fl4A64Platform.numNodes := by + rw [J.sr.sch.core]; exact hCs _ J.sr.sch.cpuCs + have hx := J.ntfn.at_ntfn hp hk + obtain ⟨hb0, -⟩ := optRef_some (hx.bound ▸ hbound) + have hbT := hx.bound_mem hb0 + have pH : H p = some .ntfn := J.utIpc.ntfn p hp + have rv1 : RunVcpuInv st H.tcbs s := J.tcbs ▸ J.runVcpu + have rvi := Fl4ProofsA64.P1.notification.send_signal.rvi (ar := ar) hA s a p badge (BXI.blk J.sys.ent.bxi) + (show p ∈ H.ntfns from pH) pH rv1 + have body := send_signal_vmA (st := st) (ar := ar) J.iplaced.n s a σ J.ntfn hp hk hidle hbound hy hrun badge + (fun s' w => J.stall_after_ntfnWrA hCs h1 hp hbT w) + refine WP.spec_mono (WP.spec_and body rvi) ?_ + rintro ⟨s', a'⟩ ⟨⟨ha, hs, R', w⟩, rv'⟩ + simp only [WP.uncurry'_pair] at ha R' w rv' ⊢ + subst ha + refine ⟨rfl, quietNtfnA x badge.bv, hs, fun sysl symL bxi => ?_⟩ + have wN : WritesIn st s s' (SchedStepNWA (st := st) N E Rp Z m σ s) := + w.mono fun c h => Or.inr (Or.inl ⟨p, hp, h.1, by unfold NObjW at h; omega⟩) + have nq : ∀ q, x.ntfnObj ≠ .waitingNtfn q := fun q e => by rw [hidle] at e; cases e + exact J.of_ntfnQuietA hcore hmb hmax htu hp hk nq badge.bv w R' bxi (J.tcbs ▸ rv') sysl symL + (J.sys.ent.user.ksb w.1) (J.j57_stepNWA wN _).2 + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3SigWakeAnyA.lean b/proofs/Fl4ProofsA64/P3/Stage3SigWakeAnyA.lean new file mode 100644 index 000000000..381a115d2 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SigWakeAnyA.lean @@ -0,0 +1,81 @@ +/- +P3 (aarch64, A64-P3 sigwake2): **`send_signal` waking a thread, at the record** (`JStage3A.sigWakeA`): every signal +neither quiet (`QuietSigA`) nor on an idle notification whose bound thread runs a guest (`VmSigA`), from the waiting +(`sigWaiting_stepA`) and bound-receive (`sigBoundRecv_stepA`) arms. No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3SigBoundRecA +import Fl4ProofsA64.P3.Stage3SignalIdleA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 Fl4ProofsA64.P3.Endp +open Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Notif + +noncomputable section + +/-- An idle notification whose bound thread runs a guest (the VCPU arm of `send_signal`). -/ +def VmSigA (σ : State Unit) (x : Notification) : Prop := + x.ntfnObj = .idleNtfn ∧ ∃ t y, x.ntfnBoundTcb = some t ∧ σ.kheap t = some (.tcb y) ∧ y.tcbState = .ext .runningVM + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- **A waking `send_signal` at the record** (module docstring). -/ +theorem JStage3A.sigWakeA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (hA : P0.ArchTotal ar) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p : U64} (hp : p ∈ N) {x : Notification} (hk : σ.kheap (specPtr p) = some (.notification x)) + (hq : ¬ QuietSigA σ x) (hvm : ¬ VmSigA σ x) (badge : U64) : + notification.send_signal st ar s a p badge ⦃ r => r.2 = a ∧ + ∃ σ', sendSignal (Z := Unit) (specPtr p) badge.bv σ = ({((), σ')}, False) ∧ + σ'.machineState = σ.machineState ∧ + (SysL st H Z.Cs OS ia ie r.1 → SymL st H Z.Cs r.1 → + ∃ Q Rl, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) r.1 a σ' m) ⦄ := by + have fin : notification.send_signal st ar s a p badge ⦃ r => r.2 = a ∧ + ∃ σ', sendSignal (Z := Unit) (specPtr p) badge.bv σ = ({((), σ')}, False) ∧ + BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac r.1 ∧ RunVcpuInv st Z.T r.1 ∧ + WritesIn st s r.1 (SchedStepNWA (st := st) N E Rp Z m σ s) ∧ σ'.machineState = σ.machineState ∧ + (SysL st H Z.Cs OS ia ie r.1 → SymL st H Z.Cs r.1 → + Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U r.1 → + J7A st (J7R2A st ar nA) Z H OS r.1 σ' m → + ∃ Q Rl, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) r.1 a σ' m) ⦄ := by + cases ho : x.ntfnObj with + | activeNtfn b0 => exact absurd (Or.inr ⟨b0, ho⟩) hq + | waitingNtfn q => exact J.sigWaiting_stepA hA hCs h1 hmb hmax htu hp hk ho badge + | idleNtfn => + have hx := J.ntfn.at_ntfn hp hk + cases hb : x.ntfnBoundTcb with + | none => exact absurd (Or.inl ⟨ho, fun t h => by rw [hb] at h; cases h⟩) hq + | some t => + obtain ⟨hb0, hbt⟩ := optRef_some (hx.bound ▸ hb) + obtain ⟨y, hy, -⟩ := J.ntfn.tcb _ (hx.bound_mem hb0) + rw [hbt] at hy + have nvm : y.tcbState ≠ .ext .runningVM := fun h => hvm ⟨ho, t, y, hb, hy, h⟩ + have hrb : Fl4SpecA64.receiveBlocked y.tcbState = true := by + refine Bool.of_not_eq_false fun h => hq (Or.inl ⟨ho, fun t' h' => ?_⟩) + rw [hb] at h'; cases h' + exact ⟨y, hy, h, nvm⟩ + have hs : ∃ e ro d, y.tcbState = .blockedOnReceive e ro d := by + cases hts : y.tcbState <;> rw [hts] at hrb <;> first + | exact ⟨_, _, _, rfl⟩ + | cases hrb + obtain ⟨e, ro, d, hs⟩ := hs + exact J.sigBoundRecv_stepA hA hCs h1 hmb hmax htu hp hk ho hb hy hs badge + refine WP.spec_mono fin ?_ + rintro r ⟨ha, σ', hsp, -, -, w, hms, K⟩ + exact ⟨ha, σ', hsp, hms, fun sysl symL => K sysl symL (J.sys.ent.user.ksb w.1) (J.j57_stepNWA w _).2⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3SigWakeFoot2A.lean b/proofs/Fl4ProofsA64/P3/Stage3SigWakeFoot2A.lean new file mode 100644 index 000000000..8ddd1b398 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SigWakeFoot2A.lean @@ -0,0 +1,103 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2a): continuation of `P3/Stage3SigWakeFootA` (split for memory): `wake_from_stateW`, +`wake_spineW`, `wake_refinesW`. No named premise; new module. +-/ +import Fl4ProofsA64.P3.Stage3SigWakeFootA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Notif +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4ProofsA64.P3.Sched + +noncomputable section + + +section Wake +open Fl4SpecA64 +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] {N T : Set U64} + {SR : S → Fl4SpecA64.State Unit → Prop} {DL : S → U64 → Prop} + {Wr : Fl4SpecA64.State Unit → S → U64 → Prop} + +theorem wake_from_stateW (hH : SchedHypsA st N T SR) (hO : SchedOpsA st ar N T SR DL) (hF : SchedFootNA st ar N T SR DL Wr) (hP : NPlaced T N) {W : U64 → Prop} + (hAr : ∀ s (a : A) σ (t n : U64) (x : Fl4SpecA64.Tcb), NtfnStateRel N T (L.mem s) σ → SR s σ → t ∈ T → + n ∈ N → σ.kheap (specPtr t) = some (.tcb x) → x.tcbState = .running → NotQueued σ (specPtr t) → + L.mem s (affA t) = L.cpu s → DL s n → rustWakeTail st ar s a t n ⦃ r => r.2 = a ⦄) (s : S) (a : A) + (σ : Fl4SpecA64.State Unit) (hSR : SR s σ) {t n : U64} (ht : t ∈ T) + (hn : n ∈ N) {x : Fl4SpecA64.Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) (badge : U64) + (hnr : Fl4SpecA64.runnable x.tcbState = false) (hon : L.mem s (affA t) = L.cpu s) {s0 : S} + {σ0 : Fl4SpecA64.State Unit} (hR0 : NtfnStateRel N T (L.mem s0) σ0) (hSR0 : SR s0 σ0) (hdl : DL s0 n) + (hK : NScK σ0 σ) (hWb : W (badgeRegA t)) (s2 : S) + (hR2 : NtfnStateRel N T (L.mem s2) (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := .running })) + (specPtr t))) + (hSR2 : SR s2 (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := .running })) (specPtr t))) : + (do + let s3 ← obj.Tcb.set_reg st t s2 layout.reg.BADGE badge + rustWakeTail st ar s3 a t n) ⦃ r => + r.2 = a ∧ ¬ (specWakeTail (specPtr t) (specPtr n) (updK (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := .running })) + (specPtr t)) (specPtr t) (.tcb (setReg { x with tcbState := .running } Fl4SpecA64.badge_register badge.bv)))).2 ∧ + ∃ σ', ((), σ') ∈ (specWakeTail (specPtr t) (specPtr n) (updK (stsAct (updK σ (specPtr t) + (.tcb { x with tcbState := .running })) (specPtr t)) (specPtr t) (.tcb (setReg { x with tcbState := .running } + Fl4SpecA64.badge_register badge.bv)))).1 ∧ NtfnStateRel N T (L.mem r.1) σ' ∧ SR r.1 σ' ∧ + FpW st Wr W (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := .running })) (specPtr t)) s2 r.1 ⦄ := by + have h0 := hH.notQueued s σ t x hSR hk hnr + have hNQ : NotQueued (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := .running })) (specPtr t)) (specPtr t) := + ⟨fun d p => by rw [stsAct_readyQueues]; exact h0.1 d p, by rw [stsAct_releaseQueue]; exact h0.2⟩ + have hk2 : (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := .running })) (specPtr t)).kheap (specPtr t) = + some (.tcb { x with tcbState := .running }) := by rw [stsAct_kheap, updK_kheap, Function.update_self] + exact wake_after_stateW hH hO hF hP hAr s2 a _ hR2 hSR2 ht hn hk2 rfl hNQ badge + ((hO.coreKeep _ _ _ _ hSR hSR2).onCore ht hon) hR0 hSR0 hdl + (hK.trans ((NScK.upd (fun z h => by rw [hk] at h; cases h) _).trans (NScK.of_kheap (stsAct_kheap _ _)))) hWb + +theorem wake_spineW (hH : SchedHypsA st N T SR) (hO : SchedOpsA st ar N T SR DL) (hF : SchedFootNA st ar N T SR DL Wr) (hP : NPlaced T N) {W : U64 → Prop} + (hAr : ∀ s (a : A) σ (t n : U64) (x : Fl4SpecA64.Tcb), NtfnStateRel N T (L.mem s) σ → SR s σ → t ∈ T → + n ∈ N → σ.kheap (specPtr t) = some (.tcb x) → x.tcbState = .running → NotQueued σ (specPtr t) → + L.mem s (affA t) = L.cpu s → DL s n → rustWakeTail st ar s a t n ⦃ r => r.2 = a ⦄) (s : S) (a : A) + (σ : Fl4SpecA64.State Unit) (hR : NtfnStateRel N T (L.mem s) σ) (hSR : SR s σ) {t n : U64} (ht : t ∈ T) + {x : Fl4SpecA64.Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hq : ∀ n ∈ N, ∀ z, σ.kheap (specPtr n) = some (.notification z) → (specPtr t) ∉ z.ntfnObj.ntfnQueue) (badge : U64) + (hcls : inClassR x.tcbState x.tcbFault ∨ RelIfBound σ (specPtr t)) + (hni : x.tcbState ≠ .idleThreadState) (hW : W (stA t)) + (hcut : ∀ s2, NtfnStateRel N T (L.mem s2) (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := .running })) + (specPtr t)) → SR s2 (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := .running })) (specPtr t)) → + (do + let s3 ← obj.Tcb.set_reg st t s2 layout.reg.BADGE badge + rustWakeTail st ar s3 a t n) ⦃ r => + r.2 = a ∧ ¬ (specWakeTail (specPtr t) (specPtr n) (updK (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := .running })) + (specPtr t)) (specPtr t) (.tcb (setReg { x with tcbState := .running } Fl4SpecA64.badge_register badge.bv)))).2 ∧ + ∃ σ', ((), σ') ∈ (specWakeTail (specPtr t) (specPtr n) (updK (stsAct (updK σ (specPtr t) + (.tcb { x with tcbState := .running })) (specPtr t)) (specPtr t) (.tcb (setReg { x with tcbState := .running } + Fl4SpecA64.badge_register badge.bv)))).1 ∧ NtfnStateRel N T (L.mem r.1) σ' ∧ SR r.1 σ' ∧ + FpW st Wr W (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := .running })) (specPtr t)) s2 r.1 ⦄) : + rustWake st ar s a t n badge ⦃ r => r.2 = a ∧ ¬ (specWake (specPtr t) (specPtr n) badge.bv σ).2 ∧ + ∃ σ', ((), σ') ∈ (specWake (specPtr t) (specPtr n) badge.bv σ).1 ∧ NtfnStateRel N T (L.mem r.1) σ' ∧ SR r.1 σ' ∧ + FpW st Wr W σ s r.1 ⦄ := by + rw [specWake_eq hk badge.bv] + unfold rustWake + refine WP.spec_bind (set_thread_state_runningW hH hO hF hP s σ hR hSR ht hk hq hcls hni hW) ?_ + rintro s2 ⟨hR2, hSR2, hfp2⟩ + refine WP.spec_mono (hcut s2 hR2 hSR2) ?_ + rintro r ⟨har, hnf, σ', hm, h1, h2, h3⟩ + exact ⟨har, hnf, σ', hm, h1, h2, + FpW.trans hF.back hfp2 ((Endp.ssz_updK_tcb σ _ _).trans (Endp.ssz_stsAct _ _)) h3⟩ + +theorem wake_refinesW (hH : SchedHypsA st N T SR) (hO : SchedOpsA st ar N T SR DL) (hF : SchedFootNA st ar N T SR DL Wr) (hP : NPlaced T N) {W : U64 → Prop} + (hAr : ∀ s (a : A) σ (t n : U64) (x : Fl4SpecA64.Tcb), NtfnStateRel N T (L.mem s) σ → SR s σ → t ∈ T → + n ∈ N → σ.kheap (specPtr t) = some (.tcb x) → x.tcbState = .running → NotQueued σ (specPtr t) → + L.mem s (affA t) = L.cpu s → DL s n → rustWakeTail st ar s a t n ⦃ r => r.2 = a ⦄) (s : S) (a : A) + (σ : Fl4SpecA64.State Unit) (hR : NtfnStateRel N T (L.mem s) σ) (hSR : SR s σ) {t n : U64} (ht : t ∈ T) + (hn : n ∈ N) {x : Fl4SpecA64.Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hq : ∀ n ∈ N, ∀ z, σ.kheap (specPtr n) = some (.notification z) → (specPtr t) ∉ z.ntfnObj.ntfnQueue) (badge : U64) + (hcls : inClassR x.tcbState x.tcbFault ∨ RelIfBound σ (specPtr t)) (hnr : Fl4SpecA64.runnable x.tcbState = false) + (hni : x.tcbState ≠ .idleThreadState) (hon : L.mem s (affA t) = L.cpu s) {s0 : S} + {σ0 : Fl4SpecA64.State Unit} (hR0 : NtfnStateRel N T (L.mem s0) σ0) (hSR0 : SR s0 σ0) (hdl : DL s0 n) + (hK : NScK σ0 σ) (hW : W (stA t)) (hWb : W (badgeRegA t)) : + rustWake st ar s a t n badge ⦃ r => r.2 = a ∧ ¬ (specWake (specPtr t) (specPtr n) badge.bv σ).2 ∧ + ∃ σ', ((), σ') ∈ (specWake (specPtr t) (specPtr n) badge.bv σ).1 ∧ NtfnStateRel N T (L.mem r.1) σ' ∧ SR r.1 σ' ∧ + FpW st Wr W σ s r.1 ⦄ := + wake_spineW hH hO hF hP hAr s a σ hR hSR ht hk hq badge hcls hni hW + (fun s2 hR2 hSR2 => wake_from_stateW hH hO hF hP hAr s a σ hSR ht hn hk badge hnr hon hR0 hSR0 hdl hK hWb s2 hR2 hSR2) + +end Wake + +end + +end Fl4ProofsA64.P3.Notif diff --git a/proofs/Fl4ProofsA64/P3/Stage3SigWakeFoot3A.lean b/proofs/Fl4ProofsA64/P3/Stage3SigWakeFoot3A.lean new file mode 100644 index 000000000..d0fd9ba0e --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SigWakeFoot3A.lean @@ -0,0 +1,145 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2a): **the fine write set of Notification send waking a waiter** +(`send_signal_waitingW`), the last piece of `P3/Stage3SigWakeFootA` / `P3/Stage3SigWakeFoot2A` (split for memory). +No named premise; new module. +-/ +import Fl4ProofsA64.P3.Stage3SigWakeFoot2A +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Notif +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4ProofsA64.P3.Sched + +noncomputable section + + +section +open Fl4SpecA64 +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] {N T : Set U64} + {SR : S → Fl4SpecA64.State Unit → Prop} {DL : S → U64 → Prop} + {Wr : Fl4SpecA64.State Unit → S → U64 → Prop} + +set_option linter.all false in +theorem waitingW_rest (hH : SchedHypsA st N T SR) (hO : SchedOpsA st ar N T SR DL) (hF : SchedFootNA st ar N T SR DL Wr) (hP : NPlaced T N) + (hAr : ∀ s (a : A) σ (t n : U64) (x : Fl4SpecA64.Tcb), NtfnStateRel N T (L.mem s) σ → SR s σ → t ∈ T → + n ∈ N → σ.kheap (specPtr t) = some (.tcb x) → x.tcbState = .running → NotQueued σ (specPtr t) → + L.mem s (affA t) = L.cpu s → DL s n → rustWakeTail st ar s a t n ⦃ r => r.2 = a ⦄) (s : S) (a : A) + (σ : Fl4SpecA64.State Unit) (hR : NtfnStateRel N T (L.mem s) σ) (hSR : SR s σ) {n : U64} (hn : n ∈ N) + {x : Fl4SpecA64.Notification} (hk : σ.kheap (specPtr n) = some (.notification x)) {d : U64} {rest' : List U64} + (hw : x.ntfnObj = .waitingNtfn ((d :: rest').map specPtr)) (badge : U64) + (hon : ∀ e ∈ T, specPtr e ∈ (d :: rest').map specPtr → L.mem s (affA e) = L.cpu s) (hdl : DL s n) + {Q : Set U64} (hQs : ∀ w ∈ T, specPtr w ∈ (d :: rest').map specPtr → w ∈ Q) + (hst : stOf (L.mem s) n = 1#u64) (hQ : QueueRel (L.mem s) T .Ep (qOf (L.mem s) n) (d :: rest')) + (hB : ∀ y ∈ d :: rest', BlockedOn (L.mem s) n y) (hd : d ∈ d :: rest') (hdT : d ∈ T) + {xd : Fl4SpecA64.Tcb} (hkd : σ.kheap (specPtr d) = some (.tcb xd)) {s1 : S} + (h1 : QueueRel (L.mem s1) T .Ep (qOf (L.mem s1) n) ((d :: rest').erase d)) + (h2 : stOf (L.mem s1) n = (if (d :: rest').erase d = [] then 0#u64 else stOf (L.mem s) n)) + (h3 : QKeeps st N T n (d :: rest') s s1) : + rustWake st ar s1 a d n badge ⦃ r => + r.2 = a ∧ ¬ (specWake (specPtr d) (specPtr n) badge.bv (updK σ (specPtr n) (.notification (afterDequeue x d)))).2 ∧ + ∃ σ', ((), σ') ∈ (specWake (specPtr d) (specPtr n) badge.bv (updK σ (specPtr n) (.notification (afterDequeue x d)))).1 ∧ + NtfnStateRel N T (L.mem r.1) σ' ∧ SR r.1 σ' ∧ + FpW st Wr (fun b => SigWrA Q n b ∨ b = badgeRegA d) σ s r.1 ∧ + ((), σ') ∈ (specWake (specPtr d) (specPtr n) badge.bv + (updK σ (specPtr n) (.notification (afterDequeue x d)))).1 ⦄ := by + set σ1 := updK σ (specPtr n) (.notification (afterDequeue x d)) with hσ1 + have hQd : d ∈ Q := hQs d hdT (by simp) + obtain ⟨hR1, hq1⟩ := rel_after_dequeue hR hn hk hw hQ hB hd h1 h2 h3 hst + have hSR1 : SR s1 σ1 := hH.stable _ _ _ _ hSR h3.foot (NtfnStep.ntfnK (y := afterDequeue x d) hk rfl rfl) + (Endp.NoBlockE.ntfnK _ _ _) + have hk1 : σ1.kheap (specPtr d) = some (.tcb xd) := by + have : (specPtr d) ≠ (specPtr n) := by intro e; rw [e, hk] at hkd; cases hkd + simp [σ1, this, hkd] + have hcls : inClassR xd.tcbState xd.tcbFault ∨ RelIfBound σ1 (specPtr d) := by + left; right; left + have h6 := (hB d hd).1 + have := (hR.at_tcb hdT hkd).st + rw [h6] at this + exact tsCode_six' this.symm + have hnr : Fl4SpecA64.runnable xd.tcbState = false := by + obtain ⟨m, hm⟩ : ∃ m, xd.tcbState = .blockedOnNotification m := by + have h6 := (hB d hd).1 + have := (hR.at_tcb hdT hkd).st + rw [h6] at this + exact tsCode_six' this.symm + rw [hm]; rfl + have hni : xd.tcbState ≠ .idleThreadState := by + obtain ⟨m, hm⟩ : ∃ m, xd.tcbState = .blockedOnNotification m := by + have h6 := (hB d hd).1 + have := (hR.at_tcb hdT hkd).st + rw [h6] at this + exact tsCode_six' this.symm + rw [hm]; exact fun h => by cases h + refine WP.spec_mono (wake_refinesW hH hO hF hP hAr (W := fun b => SigWrA Q n b ∨ b = badgeRegA d) s1 a σ1 hR1 hSR1 hdT hn hk1 hq1 badge hcls hnr hni + ((hO.coreKeep _ _ _ _ hSR hSR1).onCore hdT (hon d hdT (by simp))) hR hSR hdl + (NScK.updN hk (afterDequeue x d) rfl) (Or.inl (Or.inr ⟨d, hQd, Or.inl rfl⟩)) (Or.inr rfl)) ?_ + rintro r ⟨har, hnf, σ', hm, h1', h2', h3'⟩ + refine ⟨har, hnf, σ', hm, h1', h2', FpW.trans hF.back (FpW.of_w ?_) + (ssz_updK_ntfn σ (specPtr n) (afterDequeue x d)) h3', hm⟩ + refine ⟨h3.foot.1, h3.foot.2.1, fun b hb hn' => h3.agree b hb (fun w hw => ?_) (fun e => ?_) (fun e => ?_)⟩ + · have hwQ : w ∈ Q := hQs w (hQ.mem w hw) (List.mem_map_of_mem hw) + exact ⟨fun e => hn' (Or.inl (Or.inr ⟨w, hwQ, Or.inr (Or.inl e)⟩)), + fun e => hn' (Or.inl (Or.inr ⟨w, hwQ, Or.inr (Or.inr e)⟩))⟩ + · refine hn' (Or.inl (Or.inl ?_)) + rw [e, epHeadA_eq, nfield_val (hP.ntfn n hn) (by simp)]; simp + · refine hn' (Or.inl (Or.inl ?_)) + rw [e, epTSA_eq, nfield_val (hP.ntfn n hn) (by simp)]; simp + + + +set_option linter.all false in +theorem send_signal_waitingW (hH : SchedHypsA st N T SR) (hO : SchedOpsA st ar N T SR DL) (hF : SchedFootNA st ar N T SR DL Wr) (hP : NPlaced T N) + (hAr : ∀ s (a : A) σ (t n : U64) (x : Fl4SpecA64.Tcb), NtfnStateRel N T (L.mem s) σ → SR s σ → t ∈ T → + n ∈ N → σ.kheap (specPtr t) = some (.tcb x) → x.tcbState = .running → NotQueued σ (specPtr t) → + L.mem s (affA t) = L.cpu s → DL s n → rustWakeTail st ar s a t n ⦃ r => r.2 = a ⦄) (s : S) (a : A) + (σ : Fl4SpecA64.State Unit) (hR : NtfnStateRel N T (L.mem s) σ) (hSR : SR s σ) {n : U64} (hn : n ∈ N) + {x : Fl4SpecA64.Notification} (hk : σ.kheap (specPtr n) = some (.notification x)) {q : List Fl4SpecA64.ObjRef} + (hw : x.ntfnObj = .waitingNtfn q) (badge : U64) + (hon : ∀ d ∈ T, specPtr d ∈ q → L.mem s (affA d) = L.cpu s) (hdl : DL s n) + {Q : Set U64} (hQs : ∀ w ∈ T, specPtr w ∈ q → w ∈ Q) : + notification.send_signal st ar s a n badge ⦃ r => r.2 = a ∧ ¬ (Fl4SpecA64.sendSignal (specPtr n) badge.bv σ).2 ∧ + ∃ σ', ((), σ') ∈ (Fl4SpecA64.sendSignal (specPtr n) badge.bv σ).1 ∧ NtfnStateRel N T (L.mem r.1) σ' ∧ SR r.1 σ' ∧ + ∃ d ∈ T, ∃ rest : List Fl4SpecA64.ObjRef, q = specPtr d :: rest ∧ + FpW st Wr (fun b => SigWrA Q n b ∨ b = badgeRegA d) σ s r.1 ∧ + ((), σ') ∈ (specWake (specPtr d) (specPtr n) badge.bv + (updK σ (specPtr n) (.notification (afterDequeue x d)))).1 ⦄ := by + have hx := hR.at_ntfn hn hk + have hobj := hx.obj + rw [hw] at hobj + obtain ⟨hst, xs, hne, rfl, hQ, hB⟩ := hobj + obtain ⟨d, rest, rfl⟩ := List.exists_cons_of_ne_nil hne + have hd : d ∈ d :: rest := List.mem_cons_self + have hdT : d ∈ T := hQ.mem d hd + obtain ⟨xd, hkd, -⟩ := hR.tcb d hdT + -- the spec + have hmem : (specPtr d) ∈ Fl4SpecA64.ntfnQueueOf x := by + show (specPtr d) ∈ x.ntfnObj.ntfnQueue + rw [hw]; simp [Fl4SpecA64.Ntfn.ntfnQueue] + have hspec : Fl4SpecA64.sendSignal (specPtr n) badge.bv σ = specWake (specPtr d) (specPtr n) badge.bv (updK σ (specPtr n) (.notification (afterDequeue x d))) := by + unfold Fl4SpecA64.sendSignal + rw [nd_bind_one (getNotification_eq hk)] + simp only [hw] + unfold Fl4SpecA64.updateWaitingNtfn + rw [nd_bind_one (assert_true (by simp) σ), nd_pure_bind', List.map_cons, the_head, + nd_bind_one (tcbNtfnDequeue_eq hk hmem)] + rfl + rw [hspec] + have hQd : d ∈ Q := hQs d hdT (by simp) + -- FL4 + unfold notification.send_signal + step with obj.Ntfn.state.spec as ⟨nst, nst_post⟩ + have hsw : nst = obj.NTFN_WAITING := by rw [nst_post]; exact stOf_waiting (q := _) hx.obj hw + simp only [hsw, ntfn_consts.1, ntfn_consts.2.1, show (1#u64 : U64) ≠ 0#u64 by decide, ↓reduceIte] + step with notification.get_queue.spec as ⟨q, q_post⟩ + have hqh : q.head = d := by rw [q_post, epQ_eq, hQ.head]; rfl + rw [hqh] + refine WP.spec_bind (ntfn_dequeue_full hP s hn d _ hQ hd) ?_ + rintro s1 ⟨h1, h2, h3⟩ + refine WP.spec_mono (waitingW_rest hH hO hF hP hAr s a σ hR hSR hn hk hw badge hon hdl hQs hst hQ hB hd hdT hkd h1 h2 h3) ?_ + rintro r ⟨har, hnf, σ', hm, h1', h2', hfp, hm'⟩ + exact ⟨har, hnf, σ', hm, h1', h2', d, hdT, rest.map specPtr, by simp, hfp, hm'⟩ + +end + +end + +end Fl4ProofsA64.P3.Notif diff --git a/proofs/Fl4ProofsA64/P3/Stage3SigWakeFootA.lean b/proofs/Fl4ProofsA64/P3/Stage3SigWakeFootA.lean new file mode 100644 index 000000000..7256a9675 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SigWakeFootA.lean @@ -0,0 +1,136 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2): **the fine write set of Notification send waking a waiter**: twins of +`P3/NtfnWakeFootA`'s wake chain, generated by `WS/handoff/tools/gen_sigwakefoot.py`, at a write set `W ∨ Wr` in +place of `NWrite ∨ Wr` (`NWrite` covers every TCB's block word and links, which `EpStateRel` reads). +`send_signal_waitingW`: FL4 writes the notification's words, the state word and links of the queued threads (`Q`), +the woken head's badge register, and the scheduler words. + +No named premise; new module. +-/ +import Fl4ProofsA64.P3.NtfnWakeFootA +import Fl4ProofsA64.P3.SigFootA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Notif +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4ProofsA64.P3.Sched + +noncomputable section + +section Defs +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + {Wr : Fl4SpecA64.State Unit → S → U64 → Prop} {W : U64 → Prop} + +variable (st Wr W) in +/-- The footprint shape of the twins. -/ +def FpW (σ : Fl4SpecA64.State Unit) (s s' : S) : Prop := WritesIn st s s' (fun b => W b ∨ Wr σ s b) + +theorem FpW.of_w {σ : Fl4SpecA64.State Unit} {s s' : S} (h : WritesIn st s s' W) : FpW st Wr W σ s s' := + h.mono fun _ hb => Or.inl hb + +theorem FpW.of_wr {σ : Fl4SpecA64.State Unit} {s s' : S} (h : WritesIn st s s' (Wr σ s)) : FpW st Wr W σ s s' := + h.mono fun _ hb => Or.inr hb + +theorem FpW.trans {σ σ1 : Fl4SpecA64.State Unit} {s s1 s2 : S} + (hb : ∀ {σ σ1 : Fl4SpecA64.State Unit} {s s1 : S} {b : U64}, SSzA σ σ1 → L.ksb s1 = L.ksb s → + Wr σ1 s1 b → Wr σ s b) + (h1 : FpW st Wr W σ s s1) (hz : SSzA σ σ1) (h2 : FpW st Wr W σ1 s1 s2) : FpW st Wr W σ s s2 := + (WritesIn.trans (st := st) h1 h2).mono fun _ h => by + rcases h with (h | h) | (h | h) + · exact Or.inl h + · exact Or.inr h + · exact Or.inl h + · exact Or.inr (hb hz h1.1 h) + +theorem aligned_badgeRegA {N T : Set U64} (hP : NPlaced T N) {t : U64} (ht : t ∈ T) : Aligned (badgeRegA t) := by + rw [badgeRegA_eq]; exact tword hP ht (K := 256#u64) (by simp) (by simp) + +end Defs + +section Wake +open Fl4SpecA64 +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] {N T : Set U64} + {SR : S → Fl4SpecA64.State Unit → Prop} {DL : S → U64 → Prop} + {Wr : Fl4SpecA64.State Unit → S → U64 → Prop} + +theorem set_thread_state_ntfnW (hH : SchedHypsA st N T SR) (hO : SchedOpsA st ar N T SR DL) (hF : SchedFootNA st ar N T SR DL Wr) (hP : NPlaced T N) {W : U64 → Prop} (s : S) + (σ : Fl4SpecA64.State Unit) (hR : NtfnStateRel N T (L.mem s) σ) (hSR : SR s σ) {t : U64} (ht : t ∈ T) + {x : Fl4SpecA64.Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hq : ∀ n ∈ N, ∀ z, σ.kheap (specPtr n) = some (.notification z) → (specPtr t) ∉ z.ntfnObj.ntfnQueue) + (ts : Fl4SpecA64.ThreadState) (hts : ∀ n, ts ≠ .blockedOnNotification n) (code : U64) + (hW : W (stA t)) (hcode : code.val = tsCode ts) (hNB : Endp.NoBlockE σ (updK σ (specPtr t) (.tcb { x with tcbState := ts }))) : + sched.set_thread_state st s t code ⦃ s' => + NtfnStateRel N T (L.mem s') (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := ts })) (specPtr t)) ∧ + SR s' (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := ts })) (specPtr t)) ∧ + FpW st Wr W σ s s' ⦄ := by + unfold sched.set_thread_state + step with obj.Tcb.set_state.spec as ⟨s1, s1_post⟩ + subst s1_post + have hx := hR.at_tcb ht hk + obtain ⟨hw, hoth⟩ := twr_wr (st := st) hP ht (K := 3328#u64) (Or.inl rfl) s code + rw [← stA_eq] at hw hoth + have hR1 : NtfnStateRel N T (L.mem (L.wr s (stA t) code)) + (updK σ (specPtr t) (.tcb { x with tcbState := ts })) := by + refine hR.upd_tcb ht _ hw.ntfn hw.tcb hw.links (Or.inl hq) ⟨?_, ?_, ?_, ?_⟩ + · rw [rd_wr_eq (foot_st hP ht).1]; exact hcode + · intro n hn; exact absurd hn (hts n) + · show (L.mem _ (badgeRegA t)).bv = badgeOf x + rw [badgeRegA_eq, hoth _ (by simp) (by simp) (by simp), ← badgeRegA_eq]; exact hx.badge + · show (L.mem _ (prioA t)).val = x.tcbPriority.toNat + rw [prioA_eq', hoth _ (by simp) (by simp) (by simp), ← prioA_eq']; exact hx.prio + have hSR1 := hH.stable _ _ _ _ hSR hw.foot (NtfnStep.tcb hk ts x.tcbArch) hNB + refine WP.spec_mono (WP.spec_and (hO.scheduleTcb _ _ t hR1 hSR1 ht) (hF.scheduleTcb _ _ t hR1 hSR1 ht)) ?_ + rintro s2 ⟨⟨hf, hS⟩, hw2⟩ + exact ⟨hR1.frame hP hf (KView.stsAct _ _), hS, + FpW.trans hF.back (FpW.of_w ((writesIn_wr (foot_st hP ht).1).mono fun _ h => h ▸ hW)) (Endp.ssz_updK_tcb σ _ _) (FpW.of_wr hw2)⟩ + +theorem set_thread_state_runningW (hH : SchedHypsA st N T SR) (hO : SchedOpsA st ar N T SR DL) (hF : SchedFootNA st ar N T SR DL Wr) (hP : NPlaced T N) {W : U64 → Prop} (s : S) + (σ : Fl4SpecA64.State Unit) (hR : NtfnStateRel N T (L.mem s) σ) (hSR : SR s σ) {t : U64} (ht : t ∈ T) + {x : Fl4SpecA64.Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hq : ∀ n ∈ N, ∀ z, σ.kheap (specPtr n) = some (.notification z) → (specPtr t) ∉ z.ntfnObj.ntfnQueue) + (hcls : inClassR x.tcbState x.tcbFault ∨ RelIfBound σ (specPtr t)) (hni : x.tcbState ≠ .idleThreadState) (hW : W (stA t)) : + sched.set_thread_state st s t sched.TS_RUNNING ⦃ s' => + NtfnStateRel N T (L.mem s') (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := .running })) (specPtr t)) ∧ + SR s' (stsAct (updK σ (specPtr t) (.tcb { x with tcbState := .running })) (specPtr t)) ∧ + FpW st Wr W σ s s' ⦄ := + set_thread_state_ntfnW hH hO hF hP s σ hR hSR ht hk hq .running (fun _ h => by cases h) sched.TS_RUNNING hW + (by rw [tsVals.1]; rfl) (Endp.NoBlockE.updT hk _ (Or.inr (Or.inl ⟨rfl, hni, hcls⟩))) + +theorem wake_after_stateW (hH : SchedHypsA st N T SR) (hO : SchedOpsA st ar N T SR DL) (hF : SchedFootNA st ar N T SR DL Wr) (hP : NPlaced T N) {W : U64 → Prop} + (hAr : ∀ s (a : A) σ (t n : U64) (x : Fl4SpecA64.Tcb), NtfnStateRel N T (L.mem s) σ → SR s σ → t ∈ T → + n ∈ N → σ.kheap (specPtr t) = some (.tcb x) → x.tcbState = .running → NotQueued σ (specPtr t) → + L.mem s (affA t) = L.cpu s → DL s n → rustWakeTail st ar s a t n ⦃ r => r.2 = a ⦄) (s2 : S) + (a : A) (σ2 : Fl4SpecA64.State Unit) (hR2 : NtfnStateRel N T (L.mem s2) σ2) (hSR2 : SR s2 σ2) {t n : U64} + (ht : t ∈ T) (hn : n ∈ N) {x : Fl4SpecA64.Tcb} (hk2 : σ2.kheap (specPtr t) = some (.tcb x)) + (hrun : x.tcbState = .running) (hNQ : NotQueued σ2 (specPtr t)) (badge : U64) + (hon : L.mem s2 (affA t) = L.cpu s2) {s0 : S} {σ0 : Fl4SpecA64.State Unit} + (hR0 : NtfnStateRel N T (L.mem s0) σ0) (hSR0 : SR s0 σ0) (hdl : DL s0 n) (hK : NScK σ0 σ2) (hWb : W (badgeRegA t)) : + (do + let s3 ← obj.Tcb.set_reg st t s2 layout.reg.BADGE badge + rustWakeTail st ar s3 a t n) ⦃ r => + r.2 = a ∧ ¬ (specWakeTail (specPtr t) (specPtr n) (updK σ2 (specPtr t) (.tcb (setReg x Fl4SpecA64.badge_register + badge.bv)))).2 ∧ + ∃ σ', ((), σ') ∈ (specWakeTail (specPtr t) (specPtr n) (updK σ2 (specPtr t) (.tcb (setReg x + Fl4SpecA64.badge_register badge.bv)))).1 ∧ NtfnStateRel N T (L.mem r.1) σ' ∧ SR r.1 σ' ∧ FpW st Wr W σ2 s2 r.1 ⦄ := by + step with Endp.obj.Tcb.set_reg.spec as ⟨s3, s3_post⟩ + subst s3_post + obtain ⟨hR3, hW3, hS3⟩ := set_badge_reg_rel hP hR2 ht hk2 badge + have hSR3 := hH.stable _ _ _ _ hSR2 hW3 hS3 (Endp.NoBlockE.tcbK hk2 (setReg x Fl4SpecA64.badge_register badge.bv) rfl rfl) + have hk3 : (updK σ2 (specPtr t) (.tcb (setReg x Fl4SpecA64.badge_register badge.bv))).kheap (specPtr t) = + some (.tcb (setReg x Fl4SpecA64.badge_register badge.bv)) := by rw [updK_kheap, Function.update_self] + have hNQ3 : NotQueued (updK σ2 (specPtr t) (.tcb (setReg x Fl4SpecA64.badge_register badge.bv))) (specPtr t) := + hNQ + have hK3 : NScK σ0 (updK σ2 (specPtr t) (.tcb (setReg x Fl4SpecA64.badge_register badge.bv))) := + hK.trans (NScK.upd (fun z h => by rw [hk2] at h; cases h) _) + have hcore := (hO.coreKeep _ _ _ _ hSR2 hSR3).onCore ht hon + have hdl3 := hO.donLocalKeep _ _ _ _ n hn hR0 hR3 hSR0 hSR3 (nsc_word hR0 hR3 hn hK3) hdl + refine WP.spec_mono (WP.spec_and (WP.spec_and (hO.wakeTail _ a _ t n _ hR3 hSR3 ht hn hk3 hrun hNQ3 hcore hdl3) + (hF.wakeTail _ a _ t n _ hR3 hSR3 ht hn hk3 hrun hNQ3 hcore hdl3)) (hAr _ a _ t n _ hR3 hSR3 ht hn hk3 hrun hNQ3 hcore hdl3)) ?_ + rintro r ⟨⟨⟨hnf, σ', hmem, hf, hv, hS⟩, hw⟩, har⟩ + exact ⟨har, hnf, σ', hmem, hR3.frame hP hf hv, hS, + FpW.trans hF.back (FpW.of_w ((writesIn_wr (aligned_badgeRegA hP ht)).mono fun _ h => h ▸ hWb)) (Endp.ssz_updK_tcb σ2 _ _) (FpW.of_wr hw)⟩ + +end Wake + +end + +end Fl4ProofsA64.P3.Notif diff --git a/proofs/Fl4ProofsA64/P3/Stage3SigWakeRecA.lean b/proofs/Fl4ProofsA64/P3/Stage3SigWakeRecA.lean new file mode 100644 index 000000000..6a53130aa --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SigWakeRecA.lean @@ -0,0 +1,711 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2a): **Notification send waking a waiter, at the record** (the footprint part). + +The wake writes the notification's words, the state word and `Ep` links of the queued threads `Q` (`SigWrA`), the woken +head's badge register, and the scheduler words (`MWrA`). `JStage3TA.sigWk_stepNWA`: that write set is a +`SchedStepNWA` set; `JStage3TA.tcbWord_offWkA`: a TCB word at an offset none of these writes is off it. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3BA +import Fl4ProofsA64.P3.Stage3WakePInvA +import Fl4ProofsA64.P3.SendSignalDetA +import Fl4ProofsA64.P3.Stage3SigWakeFoot3A +import Fl4ProofsA64.P3.Stage3SigSchedValidA +import Fl4ProofsA64.P3.DelNtfnLoopFrA +import Fl4ProofsA64.P3.DelNtfnRecA +import Fl4ProofsA64.P3.Stage3WkKeepA +import Fl4ProofsA64.P3.Stage3WakeMsA +import Fl4ProofsA64.P3.Stage3EpWakeFrA +import Fl4ProofsA64.P3.Stage3WakeInvA +import Fl4ProofsA64.P3.Stage3IpcGlueA +import Fl4ProofsA64.P3.Stage3SignalIdleA +import Fl4ProofsA64.P3.IpcScGlueA +import Fl4ProofsA64.P3.Stage3SchedOpsTA +import Fl4ProofsA64.P3.Stage3QKeepPresA +import Fl4ProofsA64.P3.Stage3SortA +import Fl4ProofsA64.P3.Stage3BodyCurA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s s' : S} {a : A} {σ σ' : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + {Q : Set U64} {q d : U64} [J7PpA st J7] + +/-- The wake's write set is a scheduler / IPC-object write set. -/ +theorem JStage3BA.sigWk_stepNWA + (J : JStage3BA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (hq : q ∈ N) (hQ : Q ⊆ Z.T) (hd : d ∈ Z.T) {b : U64} + (h : (SigWrA Q q b ∨ b = badgeRegA d) ∨ MWrA (st := st) Z m σ s b) : + SchedStepNWA (st := st) N E Rp Z m σ s b := by + rcases h with (h | rfl) | h + · exact J.sigW_stepNWA hq hQ (Or.inl h) + · have hfit := (J.sr.core.tcbs d hd).fits + have hv : ∀ K : U64, K.val < 4096 → (wa d K).val = d.val + K.val := fun K hK => by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have key : ∀ K : U64, 224 ≤ K.val → K.val < 4096 → K.val ≠ 3664 → K.val ≠ 3672 → + TcbStepWA Z.T (wa d K) := by + intro K k1 k2 k3 k4 + have e1 := hv K k2 + refine ⟨d, hd, by omega, by omega, fun h => k3 ?_, fun h => k4 ?_⟩ + · have := congrArg (fun w : U64 => w.val) h + simp only [e1, hv 3664#u64 (by simp)] at this + simpa using this + · unfold runVcpuA at h + have := congrArg (fun w : U64 => w.val) h + simp only [e1, hv 3672#u64 (by simp)] at this + simpa using this + refine Or.inl (Or.inr ?_) + rw [badgeRegA_eq] + exact key 256#u64 (by simp) (by simp) (by simp) (by simp) + · exact J.sigW_stepNWA hq hQ (Or.inr h) + +/-- `JStage3BA.sigWk_stepNWA` at `JStage3TA`. -/ +theorem JStage3TA.sigWk_stepNWA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (hq : q ∈ N) (hQ : Q ⊆ Z.T) (hd : d ∈ Z.T) {b : U64} + (h : (SigWrA Q q b ∨ b = badgeRegA d) ∨ MWrA (st := st) Z m σ s b) : + SchedStepNWA (st := st) N E Rp Z m σ s b := + J.toBA.sigWk_stepNWA hq hQ hd h + +/-- A TCB word at an offset the wake does not write. -/ +theorem JStage3BA.tcbWord_offWkA + (J : JStage3BA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (hq : q ∈ N) (hQ : Q ⊆ Z.T) (hd : d ∈ Z.T) {t K : U64} (ht : t ∈ Z.T) (hK : K.val < 4096) + (h8 : K.val ≠ 3328 ∧ K.val ≠ 3496 ∧ K.val ≠ 3504 ∧ K.val ≠ 256 ∧ K.val ≠ 3464 ∧ K.val ≠ 3480 ∧ + K.val ≠ 3488 ∧ K.val ≠ 3592) : + ¬ ((SigWrA Q q (wa t K) ∨ wa t K = badgeRegA d) ∨ MWrA (st := st) Z m σ s (wa t K)) := by + have hfit := (J.sr.core.tcbs t ht).fits + have hv : (wa t K).val = t.val + K.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have htH : H t = some .tcb := by + have : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + exact this + have other : ∀ u ∈ Z.T, ∀ K' : U64, K'.val < 4096 → K.val ≠ K'.val → wa t K ≠ wa u K' := by + intro u hu K' k1 k2 h + have hfu := (J.sr.core.tcbs u hu).fits + have hv' : (wa u K').val = u.val + K'.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have h' := congrArg (fun w : U64 => w.val) h + simp only [hv, hv'] at h' + by_cases e : u = t + · subst e; omega + · have := J.sr.place.apart u t .tcb .tcb ((J.sr.thT u).2 hu) ((J.sr.thT t).2 ht) e + have hsz : ObjKindA.size .tcb = 4096 := rfl + omega + rintro (((⟨h1, h2⟩ | ⟨u, hu, e⟩) | h) | h) + · exact J.ipc_off_hobjA (v := t) (kv := .tcb) htH (by simp) ⟨by omega, by simp only [HKind.bits]; omega⟩ + (Or.inl ⟨q, hq, h1, by omega⟩) + · have huT := hQ hu + rcases e with e | e | e + · rw [stA_eq] at e; exact other u huT _ (by simp) (by simpa using h8.1) e + · exact other u huT 3496#u64 (by simp) (by simpa using h8.2.1) e + · exact other u huT 3504#u64 (by simp) (by simpa using h8.2.2.1) e + · rw [badgeRegA_eq] at h + exact other d hd 256#u64 (by simp) (by simpa using h8.2.2.2.1) h + · exact J.tcbWord_offMA ht hK ⟨h8.2.2.2.2.1, h8.2.2.2.2.2.1, h8.2.2.2.2.2.2.1, h8.2.2.2.2.2.2.2⟩ h + +/-- `JStage3BA.tcbWord_offWkA` at `JStage3TA`. -/ +theorem JStage3TA.tcbWord_offWkA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (hq : q ∈ N) (hQ : Q ⊆ Z.T) (hd : d ∈ Z.T) {t K : U64} (ht : t ∈ Z.T) (hK : K.val < 4096) + (h8 : K.val ≠ 3328 ∧ K.val ≠ 3496 ∧ K.val ≠ 3504 ∧ K.val ≠ 256 ∧ K.val ≠ 3464 ∧ K.val ≠ 3480 ∧ + K.val ≠ 3488 ∧ K.val ≠ 3592) : + ¬ ((SigWrA Q q (wa t K) ∨ wa t K = badgeRegA d) ∨ MWrA (st := st) Z m σ s (wa t K)) := + J.toBA.tcbWord_offWkA hq hQ hd ht hK h8 + +/-- A reply object's words are not written by the wake. -/ +theorem JStage3BA.reply_offWkA + (J : JStage3BA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (hq : q ∈ N) (hQ : Q ⊆ Z.T) (hd : d ∈ Z.T) {r b : U64} (hr : r ∈ Rp) + (lo : r.val ≤ b.val) (hi : b.val < r.val + 32) : + ¬ ((SigWrA Q q b ∨ b = badgeRegA d) ∨ MWrA (st := st) Z m σ s b) := by + rintro ((h | h) | h) + · exact J.reply_offFinA hq hQ hr lo hi (Or.inr (Or.inr (Or.inr (Or.inl h)))) + · have hw : IpcObjWA N E Rp b := Or.inr (Or.inr ⟨r, hr, lo, hi⟩) + have hfit := (J.sr.core.tcbs d hd).fits + have dH : H d = some .tcb := by + have : d ∈ H.tcbs := by rw [J.tcbs]; exact hd + exact this + have g : (256#u64).val = 256 := by simp + have hv : (wa d 256#u64).val = d.val + 256 := by rw [wa_valm, g]; exact Nat.mod_eq_of_lt (by omega) + rw [h, badgeRegA_eq] at hw + exact J.ipc_off_hobjA (v := d) (kv := .tcb) dH (by simp) + ⟨by rw [hv]; omega, by rw [hv]; simp only [HKind.bits]; omega⟩ hw + · exact (replyWordOffSchedA J.sr.toSchedSRN J.ipcPlace hr lo hi).2 h + +/-- `JStage3BA.reply_offWkA` at `JStage3TA`. -/ +theorem JStage3TA.reply_offWkA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (hq : q ∈ N) (hQ : Q ⊆ Z.T) (hd : d ∈ Z.T) {r b : U64} (hr : r ∈ Rp) + (lo : r.val ≤ b.val) (hi : b.val < r.val + 32) : + ¬ ((SigWrA Q q b ∨ b = badgeRegA d) ∨ MWrA (st := st) Z m σ s b) := + J.toBA.reply_offWkA hq hQ hd hr lo hi + +/-- **The donation clause at the record** under `numNodes = 1`: the notification's SC (if any) is a related SC, and +its core and the current core are both `0`. -/ +theorem JStage3BA.donLocal_single + (T : JStage3BA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) {p : U64} (hp : p ∈ N) : + DonLocalA (st := st) Z.C s p := by + intro sc e h0 + have hnH : p ∈ H.ntfns := T.utIpc.ntfn p hp + have nsc := T.blkA.2.2.1.2.2.nsc p hnH + have e' : L.mem s (ntfnScA p) = sc := e + have hs := nsc (by rw [e']; exact h0) + rw [e', T.scs] at hs + refine ⟨hs.1, ?_⟩ + have c1 := T.sr.scCore sc hs.1 + have c2 := T.scCpu sc hs.1 + have c3 := hCs _ T.j.1.sch.cpuCs + apply UScalar.eq_of_val_eq + rw [c1] + omega + +/-- `JStage3BA.donLocal_single` at `JStage3TA`. -/ +theorem JStage3TA.donLocal_single + (T : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) {p : U64} (hp : p ∈ N) : + DonLocalA (st := st) Z.C s p := + T.toBA.donLocal_single hCs h1 hp + +/-- **`r.2 = a` of the wake tail** at the MCS scheduler relation (`wake_tail_frameA`). -/ +theorem wakeTailArchA {ar : machine.Arch A} {Z : XSetsA} {m : McState} {κ : U64 → U64} {N : Set U64} + (hc : m.curCore < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) : + ∀ s (a : A) (σ : State Unit) (t n : U64) (x : Tcb), NtfnStateRel N Z.T (L.mem s) σ → + IpcSchedRelA st Z m κ s σ → t ∈ Z.T → n ∈ N → σ.kheap (specPtr t) = some (.tcb x) → + x.tcbState = .running → NotQueued σ (specPtr t) → L.mem s (affA t) = L.cpu s → + DonLocalA (st := st) Z.C s n → rustWakeTail st ar s a t n ⦃ r => r.2 = a ⦄ := by + rintro s a σ t n x hRel ⟨Q, R, H⟩ ht hn hk hrun hq hon hd + have hr : ∀ y, σ.kheap (specPtr t) = some (.tcb y) → runnable y.tcbState = true := by + intro y hy + rw [hk] at hy + cases hy + rw [hrun] + rfl + refine WP.spec_mono (wake_tail_frameA H a (hmb a) ht hon (notifScWord hn hRel H.inv hc) + (fun _ ht' _ hx => (hRel.at_tcb ht' hx).st) hq.1 hq.2 hr hd) ?_ + rintro r ⟨har, -⟩ + exact har + +/-! ## The spec chain of a wake -/ + +section spec +variable {np dsp : ObjRef} {x' : Notification} {xd : Tcb} {b : Word} {σ σ' σ3 : State Unit} + +/-- **The wake's spec chain**: the dequeue, the state write and the badge write, then the tail, which keeps `WkKeepA`. -/ +theorem wake_chainA (hkd : σ.kheap dsp = some (.tcb xd)) (hnd : np ≠ dsp) + (hm : ((), σ') ∈ (specWake dsp np b (updK σ np (.notification x'))).1) : + ∃ σ3 : State Unit, WkKeepA σ3 σ' ∧ + σ3.kheap = Function.update (Function.update σ.kheap np (some (.notification x'))) dsp + (some (.tcb (setReg { xd with tcbState := .running } badge_register b))) ∧ + σ3.machineState = σ.machineState := by + have hk1 : (updK σ np (.notification x')).kheap dsp = some (.tcb xd) := by + rw [updK_kheap, Function.update_of_ne hnd.symm]; exact hkd + rw [specWake_eq hk1] at hm + exact ⟨_, specWakeTail_wkA dsp np _ ((), σ') hm, by simp only [updK_kheap, stsAct_kheap, Function.update_idem], + by unfold stsAct; split_ifs <;> rfl⟩ + +end spec + +section derived +variable {np dsp : ObjRef} {x x' : Notification} {xd : Tcb} {b : Word} {σ σ' σ3 : State Unit} + +/-- **What the wake keeps of the TCBs, endpoints and replies** (`WakeKeepA`), from the chain. -/ +theorem wake_wakeKeepA (hkn : σ.kheap np = some (.notification x)) (hkd : σ.kheap dsp = some (.tcb xd)) + (h3 : σ3.kheap = Function.update (Function.update σ.kheap np (some (.notification x'))) dsp + (some (.tcb (setReg { xd with tcbState := .running } badge_register b)))) + (hms : σ3.machineState = σ.machineState) (hW : WkKeepA σ3 σ') : WakeKeepA σ σ' dsp := by + have k3 : ∀ q, q ≠ np → q ≠ dsp → σ3.kheap q = σ.kheap q := fun q h1 h2 => by + rw [h3, Function.update_of_ne h2, Function.update_of_ne h1] + have kd : σ3.kheap dsp = some (.tcb (setReg { xd with tcbState := .running } badge_register b)) := by + rw [h3, Function.update_self] + have ne_np : ∀ q ko, σ.kheap q = some ko → (∀ y, ko ≠ .notification y) → q ≠ np := fun q ko h hn e => by + rw [e, hkn] at h; exact hn x (Option.some.inj h).symm + have ne_d : ∀ q ko, σ.kheap q = some ko → (∀ y, ko ≠ .tcb y) → q ≠ dsp := fun q ko h hn e => by + rw [e, hkd] at h; exact hn xd (Option.some.inj h).symm + refine ⟨fun q y h => ?_, fun q y h => ?_, fun q xq h => ?_, ?_⟩ + · exact hW.2.1 q _ (by rw [k3 q (ne_np q _ h fun _ e => by cases e) (ne_d q _ h fun _ e => by cases e)]; exact h) + (fun _ _ e => by cases e) (fun _ e => by cases e) + · exact hW.2.1 q _ (by rw [k3 q (ne_np q _ h fun _ e => by cases e) (ne_d q _ h fun _ e => by cases e)]; exact h) + (fun _ _ e => by cases e) (fun _ e => by cases e) + · have a := ne_np q _ h (fun _ e => by cases e) + by_cases e : q = dsp + · subst e + have hxq : xq = xd := by rw [hkd] at h; exact (KernelObject.tcb.inj (Option.some.inj h)).symm + subst hxq + obtain ⟨y, hy, ey⟩ := hW.2.2.2.1 q _ kd + refine ⟨y, hy, (congrArg Tcb.tcbFault ey).trans rfl, (congrArg Tcb.tcbBoundNotification ey).trans rfl, + (congrArg Tcb.tcbPriority ey).trans rfl, (congrArg Tcb.tcbIpcframe ey).trans rfl, + (congrArg Tcb.tcbIpcBuffer ey).trans rfl, fun hne => absurd rfl hne, fun _ => ⟨fun r hr => ?_, ?_⟩⟩ + · exact (congrArg (fun t : Tcb => t.tcbArch.tcbContext.userRegs r) ey).trans + (by simp [setReg, setRegUc, archTcbContextSet, archTcbContextGet, Function.update_of_ne hr]) + · exact (congrArg Tcb.tcbState ey).trans rfl + · obtain ⟨y, hy, ey⟩ := hW.2.2.2.1 q xq (by rw [k3 q a e]; exact h) + exact ⟨y, hy, by rw [ey] <;> rfl, by rw [ey] <;> rfl, by rw [ey] <;> rfl, by rw [ey] <;> rfl, by rw [ey] <;> rfl, + fun _ => ⟨by rw [ey] <;> rfl, by rw [ey] <;> rfl⟩, fun h' => absurd h' e⟩ + · exact hW.2.2.2.2.2.2.trans (congrArg (fun ms : MachineState => ms.underlyingMemory) hms) + +/-- The notification after the dequeue keeps a queue's other members as a sublist. -/ +theorem afterDequeue_sub (x : Notification) (d : U64) : + (afterDequeue x d).ntfnObj.ntfnQueue.Sublist x.ntfnObj.ntfnQueue := by + have hs : (Fl4SpecA64.removeAll (specPtr d) (Fl4SpecA64.ntfnQueueOf x)).Sublist x.ntfnObj.ntfnQueue := + by unfold Fl4SpecA64.removeAll; exact List.filter_sublist + unfold afterDequeue Fl4SpecA64.ntfnObjUpdate + cases hrm : Fl4SpecA64.removeAll (specPtr d) (Fl4SpecA64.ntfnQueueOf x) with + | nil => simp [hrm, Fl4SpecA64.Ntfn.ntfnQueue] + | cons a l => + rw [hrm] at hs + simpa [hrm, Fl4SpecA64.Ntfn.ntfnQueue] using hs + +/-- A queued member other than the head is still queued after the dequeue. -/ +theorem afterDequeue_mem {x : Notification} {d t : U64} {q : List ObjRef} (hw : x.ntfnObj = .waitingNtfn q) + (hm : specPtr t ∈ q) (hne : specPtr t ≠ specPtr d) : + ∃ q', (afterDequeue x d).ntfnObj = .waitingNtfn q' ∧ specPtr t ∈ q' := by + have hmem : specPtr t ∈ Fl4SpecA64.removeAll (specPtr d) (Fl4SpecA64.ntfnQueueOf x) := by + unfold Fl4SpecA64.removeAll + refine List.mem_filter.2 ⟨?_, by simpa using fun e => hne e.symm⟩ + show specPtr t ∈ x.ntfnObj.ntfnQueue + rw [hw]; exact hm + unfold afterDequeue Fl4SpecA64.ntfnObjUpdate + cases hrm : Fl4SpecA64.removeAll (specPtr d) (Fl4SpecA64.ntfnQueueOf x) with + | nil => rw [hrm] at hmem; cases hmem + | cons a l => rw [hrm] at hmem; exact ⟨a :: l, by simp [hrm], hmem⟩ + +/-- **The wake keeps `NtfnSpecKeepA`** (`mcp`, yield-to, SC reply stacks and yield-from). -/ +theorem wake_ntfnSpecKeepA (hkn : σ.kheap np = some (.notification x)) (hkd : σ.kheap dsp = some (.tcb xd)) + (hnd : np ≠ dsp) + (h3 : σ3.kheap = Function.update (Function.update σ.kheap np (some (.notification x'))) dsp + (some (.tcb (setReg { xd with tcbState := .running } badge_register b)))) (hW : WkKeepA σ3 σ') : + NtfnSpecKeepA σ σ' := by + have k1 : NtfnSpecKeepA σ { σ with kheap := Function.update σ.kheap np (some (.notification x')) } := + NtfnSpecKeepA.of_upd hkn rfl ⟨_, rfl⟩ + have hq : ({ σ with kheap := Function.update σ.kheap np (some (.notification x')) } : State Unit).kheap dsp = + some (.tcb xd) := by + show Function.update σ.kheap np _ dsp = _ + rw [Function.update_of_ne hnd.symm]; exact hkd + have k2 : NtfnSpecKeepA { σ with kheap := Function.update σ.kheap np (some (.notification x')) } σ3 := + NtfnSpecKeepA.of_upd (q := dsp) hq h3 ⟨_, rfl, rfl, rfl, rfl⟩ + exact (k1.trans k2).trans hW.1 + +instance (t n : ObjRef) (b : Badge) : Sched.QInst (specWake t n b) := + ⟨by unfold specWake specWakeTail ifSporadicCurScTestRefillUnblockCheck; qpres⟩ + +/-- **The wake's queue ingredients** (`QKeepA`): the dequeue shrinks the notification's queue to a sublist, and the +wake itself (`specWake`) keeps the queue ingredients (`QInst`). -/ +theorem wake_qKeepA {np dsp : ObjRef} {x x' : Notification} {b : Word} {σ σ' : State Unit} + (hkn : σ.kheap np = some (.notification x)) (hsub : x'.ntfnObj.ntfnQueue.Sublist x.ntfnObj.ntfnQueue) + (hm : ((), σ') ∈ (specWake dsp np b (updK σ np (.notification x'))).1) : QKeepA σ σ' := + (QKeepA.upd hkn (QObjA.ofNtfn hsub)).trans (Sched.qk_of hm) + +/-- An object that is neither a TCB, an SC nor the notification survives the wake. -/ +theorem wake_kept (hkn : σ.kheap np = some (.notification x)) (hnd : np ≠ dsp) + (h3 : σ3.kheap = Function.update (Function.update σ.kheap np (some (.notification x'))) dsp + (some (.tcb (setReg { xd with tcbState := .running } badge_register b)))) (hW : WkKeepA σ3 σ') + {q : ObjRef} (hq : q ≠ np) (hqd : q ≠ dsp) {ko : KernelObject} (h : σ.kheap q = some ko) + (ht : ∀ y, ko ≠ .tcb y) (hs : ∀ sc k, ko ≠ .schedContext sc k) : σ'.kheap q = some ko := by + have : σ3.kheap q = σ.kheap q := by rw [h3, Function.update_of_ne hqd, Function.update_of_ne hq] + exact hW.2.1 q ko (by rw [this]; exact h) hs ht + +/-- The notification after the wake. -/ +theorem wake_np (hnd : np ≠ dsp) + (h3 : σ3.kheap = Function.update (Function.update σ.kheap np (some (.notification x'))) dsp + (some (.tcb (setReg { xd with tcbState := .running } badge_register b)))) (hW : WkKeepA σ3 σ') : + σ'.kheap np = some (.notification x') := by + have : σ3.kheap np = some (.notification x') := by + rw [h3, Function.update_of_ne hnd, Function.update_self] + exact hW.2.1 np _ this (fun _ _ e => by cases e) (fun _ e => by cases e) + +end derived + +theorem sufficient_of_rrokA {y : SchedContext} (h : RRokA y) : refillSufficient 0 (refillHd y) = true := by + unfold refillSufficient refillCapacity + have h0 : ¬ (refillHd y).rAmount < 0 := by rw [BitVec.lt_def]; simp + simp only [h0, ↓reduceIte, decide_eq_true_eq] + have e : (refillHd y).rAmount - 0 = (refillHd y).rAmount := by bv_omega + rw [e]; exact h.2.2 + +section recA +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {ar : machine.Arch A} {nA : U64} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- **`RqDomPrio` at the record**: a ready-queue member sits in the queue of its own domain and priority. -/ +theorem JStage3A.rqDomPrio + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) : + RqDomPrio σ := by + intro f d p x hx hm + by_cases hd : d = 0 + · subst hd + have hlt : p.toNat < 256 := p.isLt + let pp : U64 := ⟨BitVec.ofNat 64 p.toNat⟩ + have hpp : pp.val = p.toNat := by + show (BitVec.ofNat 64 p.toNat).toNat = p.toNat + rw [BitVec.toNat_ofNat] + exact Nat.mod_eq_of_lt (by omega) + have hrq := J.sr.rest.rq pp (by rw [hpp]; exact hlt) + have e8 : BitVec.ofNat 8 pp.val = p := by + rw [hpp] + apply BitVec.eq_of_toNat_eq + rw [BitVec.toNat_ofNat] + exact Nat.mod_eq_of_lt hlt + rw [e8] at hrq + rw [hrq] at hm + obtain ⟨u, hu, rfl⟩ := List.mem_map.1 hm + have hcpu := J.sr.sch.cpuCs + have hq := J.sr.core.inQ _ hcpu pp (by rw [hpp]; exact hlt) u hu + have huT := J.sr.core.memT hcpu (by rw [hpp]; exact hlt) hu + obtain ⟨x', hx', hdom, -, hpr⟩ := J.sr.rest.prio u huT + rw [hx] at hx' + have := KernelObject.tcb.inj (Option.some.inj hx') + subst this + refine ⟨hdom.symm, ?_⟩ + apply BitVec.eq_of_toNat_eq + rw [← hpr, hq.2, hpp] + · have := J.sr.rest.rqDom d p hd + rw [this] at hm + cases hm + +/-- **A notification's wait queue is blocked on it** (`SigQ`). -/ +theorem JStage3A.sigQ + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {p : U64} (hp : p ∈ N) : SigQ (specPtr p) σ := by + intro x q t hk hw ht + have hx := J.ntfn.at_ntfn hp hk + have hobj := hx.obj + rw [hw] at hobj + obtain ⟨-, xs, -, rfl, hQ, hB⟩ := hobj + obtain ⟨u, hu, rfl⟩ := List.mem_map.1 ht + have huT := hQ.mem u hu + obtain ⟨xd, hkd, -⟩ := J.ntfn.tcb u huT + refine ⟨xd, hkd, ?_⟩ + have h6 := (hB u hu).1 + have hst := (J.ntfn.at_tcb huT hkd).st + rw [h6] at hst + obtain ⟨m0, hm0⟩ := tsCode_six' hst.symm + have hb := (J.ntfn.at_tcb huT hkd).blk m0 hm0 + have hbu := (hB u hu).2 + rw [hm0] + congr 1 + rw [← hb, hbu] + +end recA + +section recB +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {ar : machine.Arch A} {nA : U64} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +set_option maxHeartbeats 4000000 in +/-- A notification's SC has a sufficient head refill (`ntfnIdle` / `ntfnIdleOf`, then `refills` / `rr`). -/ +theorem JStage3A.ntfnScSufA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {q : ObjRef} {x : Notification} (hk : σ.kheap q = some (.notification x)) {sc : ObjRef} (hsc : x.ntfnSc = some sc) + {y : SchedContext} {k : Nat} (hy : σ.kheap sc = some (.schedContext y k)) (ha : scActive y = true) : + refillSufficient 0 (refillHd y) = true := by + by_cases per : y.scPeriod = 0 + · have NI : NotIdleScA m sc := ⟨fun e => J.sr.inv.ntfnIdle q x hk (e ▸ hsc), + fun c hc e => J.sr.inv.ntfnIdleOf q x hk c hc (e ▸ hsc)⟩ + exact sufficient_of_rrokA ((J.sr.inv.rr _ y k hy ha per).2 NI) + · exact sufficient_of_refOKA (J.sr.inv.refills _ y k hy ha per) + +/-- **`send_signal` waking the head of a waiting notification, at the record** (module docstring of +`P3/Stage3SigWakeFootA`, `P3/Stage3SigWakeRecA`). -/ +theorem JStage3A.sigWaiting_stepA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (hA : P0.ArchTotal ar) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p : U64} (hp : p ∈ N) {x : Notification} (hk : σ.kheap (specPtr p) = some (.notification x)) + {q : List ObjRef} (hw : x.ntfnObj = .waitingNtfn q) (badge : U64) : + notification.send_signal st ar s a p badge ⦃ r => r.2 = a ∧ + ∃ σ', sendSignal (Z := Unit) (specPtr p) badge.bv σ = ({((), σ')}, False) ∧ + BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac r.1 ∧ RunVcpuInv st Z.T r.1 ∧ + WritesIn st s r.1 (SchedStepNWA (st := st) N E Rp Z m σ s) ∧ σ'.machineState = σ.machineState ∧ + (SysL st H Z.Cs OS ia ie r.1 → SymL st H Z.Cs r.1 → + Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U r.1 → + J7A st (J7R2A st ar nA) Z H OS r.1 σ' m → + ∃ Q Rl, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) r.1 a σ' m) ⦄ := by + have T := J.toBA + have hP := J.iplaced + have hc := T.curCore_lt hCs + obtain ⟨hH, hO, -⟩ := T.schedOpsA (ar := ar) hc hmb hmax htu + have hF := T.schedFootNA (ar := ar) hc hmb + have hdl := T.donLocal_single hCs h1 hp + have hon : ∀ d ∈ Z.T, specPtr d ∈ q → L.mem s (affA d) = L.cpu s := fun d hd _ => T.onCore_single hCs h1 hd + have hAr := wakeTailArchA (st := st) (ar := ar) (Z := Z) (m := m) (κ := κ) (N := N) hc hmb + have pH : H p = some .ntfn := J.utIpc.ntfn p hp + have pNt : p ∈ H.ntfns := pH + have rv1 : RunVcpuInv st H.tcbs s := J.tcbs ▸ J.runVcpu + have blk := Fl4ProofsA64.P1.notification.send_signal.tinv0 (ar := ar) hA s a p badge (BXI.blk J.sys.ent.bxi) pNt pH rv1 + have rvi := Fl4ProofsA64.P1.notification.send_signal.rvi (ar := ar) hA s a p badge (BXI.blk J.sys.ent.bxi) pNt pH rv1 + let Q : Set U64 := {w | w ∈ Z.T ∧ specPtr w ∈ q} + have hQ : Q ⊆ Z.T := fun _ h => h.1 + have body := send_signal_waitingW hH hO hF hP.n hAr s a σ J.ntfn T.ipcSchedA hp hk hw badge hon hdl (Q := Q) + (fun w hw hq => ⟨hw, hq⟩) + refine WP.spec_mono (WP.spec_and (WP.spec_and body blk) rvi) ?_ + rintro r ⟨⟨⟨har, hnf, σ', hm0, Rn, SR', d, hdT, rest, hq, fp, hmw⟩, blk'⟩, rv'⟩ + have hdQ : d ∈ Q := ⟨hdT, by rw [hq]; simp⟩ + obtain ⟨xd, hkd, -⟩ := J.ntfn.tcb d hdT + have hnd : specPtr p ≠ specPtr d := fun e => by rw [e, hkd] at hk; cases hk + obtain ⟨σ3, hW, h3, hms⟩ := wake_chainA hkd hnd hmw + have WK := wake_wakeKeepA hk hkd h3 hms hW + have NK := wake_ntfnSpecKeepA hk hkd hnd h3 hW + have QK := wake_qKeepA hk (afterDequeue_sub x d) hmw + have F : SchedFrameA σ σ' := + (inferInstance : SPresInst (sendSignal (Z := Unit) (specPtr p) badge.bv)).pres σ ((), σ') hm0 + have wk : WritesIn st s r.1 (fun b => (SigWrA Q p b ∨ b = badgeRegA d) ∨ MWrA (st := st) Z m σ s b) := fp + have w : WritesIn st s r.1 (SchedStepNWA (st := st) N E Rp Z m σ s) := + wk.mono fun _ h => T.sigWk_stepNWA hp hQ hdT h + refine ⟨har, σ', Det.sendSignal_singletonA hnf hm0, blk', J.tcbs ▸ rv', w, wake_msA hkd hnd hmw, + fun sysl symL userOK j7 => ?_⟩ + have tw : ∀ t ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → + (K.val ≠ 3328 ∧ K.val ≠ 3496 ∧ K.val ≠ 3504 ∧ K.val ≠ 256 ∧ K.val ≠ 3464 ∧ K.val ≠ 3480 ∧ + K.val ≠ 3488 ∧ K.val ≠ 3592) → L.mem r.1 (wa t K) = L.mem s (wa t K) := + fun t ht K k1 k2 k3 => wk.mem_eq (field_aligned (J.sr.core.tcbs t ht) k1 k2) (T.tcbWord_offWkA hp hQ hdT ht k1 k3) + obtain ⟨Q', R', H3⟩ := SR' + -- the endpoint relation + have hQb : ∀ w ∈ Q, w ∈ Z.T ∧ L.mem s (tBlockA w) = p := by + intro w hwQ + obtain ⟨hwT, hwq⟩ := hwQ + have hobj := (J.ntfn.at_ntfn hp hk).obj + rw [hw] at hobj + obtain ⟨-, xs, -, hxs, -, hB⟩ := hobj + rw [hxs] at hwq + obtain ⟨y, hy, ey⟩ := List.mem_map.1 hwq + have e : y = w := specPtr_inj ey + subst e + exact ⟨hwT, (hB y hy).2⟩ + have ep : EpStateRel Z.T E Rp Bf (L.mem r.1) σ' := + EpStateRel.frameW J.ep hP hp hQb hdQ + (fun b hb hf hn hbd => wk.mem_eq hb fun h => + h.elim (fun h1 => h1.elim hn hbd) (endpFootOffWrite J.sr.toSchedSRN J.ipcPlace hf)) + (fun t ht z hz => (Rn.at_tcb ht hz).st) (fun z hz => (Rn.at_tcb hdT hz).badge) WK + have reply : ReplyStackRelA (L.mem r.1) Rp Z.C σ' (scNameA m) stack := by + refine J.reply.ntfnFr NK fun r' hr' => ?_ + obtain ⟨r32, -, rfit⟩ := hP.e.rep r' hr' + have one : ∀ o : U64, o.val < 32 → o.val % 8 = 0 → L.mem r.1 (wa r' o) = L.mem s (wa r' o) := by + intro o o1 o2 + have v : (wa r' o).val = r'.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + refine wk.mem_eq ?_ (T.reply_offWkA hp hQ hdT hr' (by omega) (by omega)) + show (wa r' o).val % 8 = 0 + omega + have n16 : (layout.reply.NEXT).val = 16 := by unfold layout.reply.NEXT; rfl + have p8 : (layout.reply.PREV).val = 8 := by unfold layout.reply.PREV; rfl + exact ⟨one _ (by omega) (by omega), one _ (by omega) (by omega)⟩ + have K : IpcKeepA σ σ' := by + refine ⟨fun t xq h => ?_, fun p0 y k h => ?_, fun p0 y' k' h => ?_⟩ + · obtain ⟨y, hy, f1, -, -, f4, f5, -, -⟩ := WK.tcb t xq h + obtain ⟨y2, hy2, m1, m2⟩ := NK.tcb h + rw [hy] at hy2 + cases hy2 + exact ⟨_, hy, f1, f4, f5, m1, m2.1, m2.2⟩ + · obtain ⟨z, hz, -, k2⟩ := NK.sc h + exact ⟨z, k, hz, k2⟩ + · obtain ⟨z, hz, -, k2⟩ := NK.sc_back h + exact ⟨z, k', hz, k2⟩ + -- the spec invariants + have hL : SPairA σ m n := ⟨J.sinv, J.sidle⟩ + have R1 : RefK σ (updK σ (specPtr p) (.notification (afterDequeue x d))) := by + refine ⟨rfl, fun q' => ?_, fun g => g.updNs _ _ (fun _ _ e => by cases e)⟩ + show RefObjA (σ.kheap q') (Function.update σ.kheap (specPtr p) _ q') + by_cases e : q' = specPtr p + · subst e; rw [Function.update_self, hk]; exact .ntfn _ _ + · rw [Function.update_of_ne e]; exact .same _ + have hL1 := hL.refK R1 + have hkn1 : (updK σ (specPtr p) (.notification (afterDequeue x d))).kheap (specPtr p) = + some (.notification (afterDequeue x d)) := by simp + have hkd1 : (updK σ (specPtr p) (.notification (afterDequeue x d))).kheap (specPtr d) = some (.tcb xd) := by + rw [updK_kheap, Function.update_of_ne hnd.symm]; exact hkd + have hsc : ∀ sc, (afterDequeue x d).ntfnSc = some sc → sc ≠ idleScPtr := fun sc h e => by + subst e + exact J.sr.inv.ntfnIdle (specPtr p) x hk h + have hxd : ∃ o, xd.tcbState = .blockedOnNotification o := by + have hobj := (J.ntfn.at_ntfn hp hk).obj + rw [hw] at hobj + obtain ⟨-, xs, -, hxs, -, hB⟩ := hobj + have hdq : specPtr d ∈ q := by rw [hq]; simp + rw [hxs] at hdq + obtain ⟨y, hy, ey⟩ := List.mem_map.1 hdq + have e : y = d := specPtr_inj ey + subst e + have h6 := (hB y hy).1 + have hst := (J.ntfn.at_tcb hdT hkd).st + rw [h6] at hst + exact tsCode_six' hst.symm + have hsuf1 : (afterDequeue x d).ntfnSc = + some (updK σ (specPtr p) (.notification (afterDequeue x d))).curSc → ∀ z k, + (updK σ (specPtr p) (.notification (afterDequeue x d))).kheap + (updK σ (specPtr p) (.notification (afterDequeue x d))).curSc = some (.schedContext z k) → + z.scTcb = none → scActive z = true → refillSufficient 0 (refillHd z) = true := fun e y k hy _ ha => by + change Function.update σ.kheap (specPtr p) _ σ.curSc = _ at hy + by_cases ec : σ.curSc = specPtr p + · rw [ec, Function.update_self] at hy; cases hy + · rw [Function.update_of_ne ec] at hy + exact J.ntfnScSufA hk e hy ha + have tidle : specPtr d ≠ (updK σ (specPtr p) (.notification (afterDequeue x d))).idleThread := fun e => by + obtain ⟨z, hz, hz'⟩ := hL1.sinv.idleTcb + rw [← e, hkd1] at hz + have := KernelObject.tcb.inj (Option.some.inj hz) + subst this + obtain ⟨o, ho⟩ := hxd + rw [ho] at hz' + cases hz' + have tidles : ∀ c < n, m.idleThreadOf c ≠ specPtr d := fun c hc e => by + obtain ⟨z, hz, hz'⟩ := hL1.sidle.f3 c hc + rw [e, hkd1] at hz + have := KernelObject.tcb.inj (Option.some.inj hz) + subst this + obtain ⟨o, ho⟩ := hxd + rw [ho] at hz' + cases hz' + have hrun : runnable xd.tcbState = false := by + obtain ⟨o, ho⟩ := hxd + rw [ho]; rfl + have hfn : xd.tcbFault = none := FaultStOkA.none hL1.sinv.faultSt hkd1 (by + obtain ⟨o, ho⟩ := hxd + rw [ho]; rintro (⟨_, _, h⟩ | ⟨_, h⟩ | h) <;> cases h) + have hdom : xd.tcbDomain = σ.curDomain := by + obtain ⟨x', hx', e0, -⟩ := J.sr.rest.prio d hdT + rw [hkd] at hx' + have := KernelObject.tcb.inj (Option.some.inj hx') + subst this + rw [e0, J.sr.rest.curDom.1] + have hLoop := hL1.wake hkn1 hsc hkd1 hrun hfn tidle tidles hdom badge.bv hsuf1 hmw + -- blocked queues + have bq : BlkQueuedA Z.T E N (L.mem r.1) σ' := by + refine ⟨fun t ht y h e hb => ?_, fun t ht y h q' hb => ?_⟩ + · obtain ⟨x0, hx0, -⟩ := J.ntfn.tcb t ht + obtain ⟨y0, hy0, -, -, -, -, -, hne, heq⟩ := WK.tcb _ x0 hx0 + rw [h] at hy0 + cases hy0 + by_cases ed : specPtr t = specPtr d + · rw [(heq ed).2] at hb + simp [Fl4SpecA64.epBlocked] at hb + · have hs := (hne ed).2 + obtain ⟨c, z, hz, hm⟩ := J.blkQ.ep t ht x0 hx0 e (by rw [← hs]; exact hb) + refine ⟨by rw [Notif.tBlockA_eq, tw t ht 3336#u64 (by simp) (by simp) (by simp)]; exact c, + z, WK.ep _ _ hz, hm⟩ + · obtain ⟨x0, hx0, -⟩ := J.ntfn.tcb t ht + obtain ⟨y0, hy0, -, -, -, -, -, hne, heq⟩ := WK.tcb _ x0 hx0 + rw [h] at hy0 + cases hy0 + by_cases ed : specPtr t = specPtr d + · rw [(heq ed).2] at hb + cases hb + · have hs := (hne ed).2 + obtain ⟨c, z, l, hz, hl, hm⟩ := J.blkQ.ntfn t ht x0 hx0 q' (by rw [← hs]; exact hb) + have tb : L.mem r.1 (tBlockA t) = L.mem s (tBlockA t) := by + rw [Notif.tBlockA_eq]; exact tw t ht 3336#u64 (by simp) (by simp) (by simp) + by_cases eq : q' = specPtr p + · subst eq + rw [hk] at hz + cases hz + rw [hw] at hl + cases hl + obtain ⟨q2, hq2, hm2⟩ := afterDequeue_mem hw hm ed + exact ⟨by rw [tb]; exact c, afterDequeue x d, q2, wake_np hnd h3 hW, hq2, hm2⟩ + · have hnt : q' ≠ specPtr d := fun e' => by rw [e', hkd] at hz; cases hz + exact ⟨by rw [tb]; exact c, z, l, + wake_kept hk hnd h3 hW eq hnt hz (fun _ e => by cases e) (fun _ _ e => by cases e), hl, hm⟩ + -- F8 + have sv : SchedValidA σ' := by + have hI := SchedValidA.toFIA J.schedValid + have hid : IdleSt σ := fun x0 hx0 => by + obtain ⟨x1, hx1, hs⟩ := J.sinv.idleTcb + rw [hx1] at hx0 + cases hx0 + exact hs + have := sendSignal_fia (specPtr p) badge.bv σ σ ⟨rfl, hI, J.rqDomPrio, hid, J.sigQ hp⟩ ((), σ') hm0 + exact SchedValidB.toA this.1.1 this.1.2 + refine ⟨Q', R', ?_⟩ + exact J.of_ipcStepA (Q := Q') (Rl := R') (curOf := Z.curOf) (curScOf := Z.curScOf) w F.1 F.2.1 QK F.2.2 + (fun _ _ => rfl) (BlkOK.bxi blk') sysl symL userOK j7 H3 ep Rn reply K + (fun t ht O h h8 => tw t ht O (by omega) h8 (by omega)) hLoop.sinv hLoop.sidle (J.tcbs ▸ rv') bq sv + +end recB + +section armA +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {ar : machine.Arch A} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +set_option maxHeartbeats 4000000 in +/-- **Notification send waking a waiter, at a syscall entry**: `decode_invocation` on a Notification send cap naming a +notification with a waiting queue (module docstring). -/ +theorem JSyscallA.signalWaitingA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hn : fl4A64Platform.numNodes ≤ n) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p badge slot lbl len buf : U64} {recv blk call don fp : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Notification p badge true recv) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hp : p ∈ N) {x : Notification} (hk : σ.kheap (specPtr p) = some (.notification x)) + {q : List ObjRef} (hw : x.ntfnObj = .waitingNtfn q) : + ∃ xc σ1 σ2 s2, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ((), σ2) ∈ (sendSignal (Z := Unit) (specPtr p) badge.bv σ1).1 ∧ + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don fp buf extra = + ok (.Ok (), s2, a) ∧ + (FIA σ1 ∧ RqDomPrio σ1 ∧ IdleSt σ1 ∧ SigQ (specPtr p) σ1) ∧ + ∃ Q Rl, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s2 a σ2 m := by + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hslot hcov J.actW J.running J.cur + have hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM := J.toJStage3EntryA.curNotVmA + obtain ⟨xc, hkc, hst, hsp, J1, -, -, -⟩ := J.toJStage3A.of_restartCurA RS.pl.choose_spec.choose_spec.choose_spec.choose_spec + RS.sym RS.run hnvm RS.cur RS.vs RS.vc + set s1 := L.wr s (stA (L.curW s)) sched.TS_RESTART with hs1 + set σ1 := stUpdA σ σ.curThread xc .restart with hσ1 + have ne1 : specPtr p ≠ σ.curThread := fun e => by rw [e, hkc] at hk; cases hk + have hk1 : σ1.kheap (specPtr p) = some (.notification x) := by + rw [hσ1]; simp only [stUpdA, setObjA, Function.update_of_ne ne1]; exact hk + have body := J1.sigWaiting_stepA hA hCs h1 hmb hmax htu hp hk1 hw badge + obtain ⟨⟨s2, a2⟩, eb, ha2, σ2, hs2, blk', rv', -, -, K⟩ := exists_of_specA body + have hm2 : ((), σ2) ∈ (sendSignal (Z := Unit) (specPtr p) badge.bv σ1).1 := by + rw [hs2]; exact Set.mem_singleton _ + simp only at ha2 eb + have ha2' : a = a2 := ha2.symm + subst ha2' + have hdec : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don fp buf extra = + ok (.Ok (), s2, a) := by + rw [hcap, decodeNotification_programA] + simp only [↓reduceIte, ipcInvokeProgramA, cur_thread_ok, bind_ok, hst] + rw [show notification.send_signal st ar (L.wr s (stA (L.curW s)) sched.TS_RESTART) a p badge = ok (s2, a) + from eb] + simp only [bind_ok] + rfl + have F := J.decodeFinA hA hM hPl hfm hslot hcov (rfl : capA st s slot = capA st s slot) hext hSC (lbl := lbl) + (by rw [hcap]; simp [DecArmS1aEnt, DecArmQH, DecArmQZ2, DecArmKQ2, DecArmKQ, capFam]) + (fun _ h => by rw [hcap] at h; cases h) (fun _ h => by rw [hcap] at h; cases h) hdec σ2 m + obtain ⟨-, -, sysl, symL, -, userOK, j7⟩ := F + obtain ⟨Q, Rl, J2⟩ := K sysl symL userOK j7 + have hid1 : IdleSt σ1 := fun x0 hx0 => by + obtain ⟨x1, hx1, hs1⟩ := J1.sinv.idleTcb + rw [hx1] at hx0; cases hx0; exact hs1 + exact ⟨xc, σ1, σ2, s2, hkc, rfl, hsp, hm2, hdec, + ⟨Sched.SchedValidA.toFIA J1.schedValid, J1.rqDomPrio, hid1, J1.sigQ hp⟩, Q, Rl, J2⟩ + +end armA + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3SignalFootA.lean b/proofs/Fl4ProofsA64/P3/Stage3SignalFootA.lean new file mode 100644 index 000000000..82afed741 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SignalFootA.lean @@ -0,0 +1,147 @@ +/- +P3 (aarch64, A64-P3 Stage-3 integration, D8/T1 step 2): **from the cancel / receive footprints to the Stage-3 +write set**. + +* `ewrite_stepNA_buf`: an endpoint-code word is a `SchedStepNWA` word or a buffer word `BufW Bf` (the buffer disjunct + of `EWrite` is the only one `ewrite_stepNA` needs `Bf = ∅` for). +* `cancelWA_stepNA`: the `cancel_ipc` write set (`CancelWA`) is `SchedStepNWA ∨ BufW Bf`, given that the action word is a + `SchedStepNWA` word (`SchedSRN.act_stepNA`). +* `send_signal_idle_bound_recv_stepNA`: **the idle-bound-receive arm of `send_signal` writes + `SchedStepNWA ∨ BufW Bf`** (the IRQ arm / `timer.fire` frame of that arm; `Bf` is the caller's buffer set, which the + cancellation does not touch in fact but whose `EWrite` footprint the proofs keep generic: the consumer + `JStage3A.of_schedWrX` takes exactly this shape). +* `take_reply`'s cancellation: `take_reply_cancelAF`'s `CancelWA` footprint goes through `cancelWA_stepNA`. + +No named premise. +-/ +import Fl4ProofsA64.P3.Stage3NtfnIdleFootA +import Fl4ProofsA64.P3.Stage3RecvFootA +import Fl4ProofsA64.P3.Stage3NtfnFootA +import Fl4ProofsA64.P3.Stage3IpcFootA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Notif + +noncomputable section + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + {N E R Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {Z : XSetsA} {m : Fl4SpecA64.McState} + {σ : Fl4SpecA64.State Unit} {s : S} + +/-- An endpoint-code word is a Stage-3 scheduler-step word or a buffer word. -/ +theorem ewrite_stepNA_buf (hP : TcbPlaced Z.T) {b : U64} (h : EWrite Z.T E R Bf b) : + SchedStepNWA (st := st) N E R Z m σ s b ∨ BufW Bf b := by + by_cases hb : BufW Bf b + · exact Or.inr hb + · refine Or.inl (ewrite_stepNA hP (b := b) ?_) + rcases h with h | h | h | h + · exact Or.inl h + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr (Or.inl h)) + · exact absurd h hb + +/-- The action word of the current core is a Stage-3 scheduler-step word. -/ +theorem SchedSRN.act_stepNA {κ : U64 → U64} (H : SchedSRN (st := st) Z m κ s σ) : + SchedStepNWA (st := st) N E Rp Z m σ s (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) := + Or.inl (Or.inl (Or.inr (Or.inl (H.node_sks H.sch.cpuCs (by simp [layout.node.SCHED_ACTION]))))) + +/-- **The `cancel_ipc` write set is a Stage-3 write set or a buffer word.** -/ +theorem cancelWA_stepNA (hP : TcbPlaced Z.T) {ab b : U64} (hab : SchedStepNWA (st := st) N E R Z m σ s ab) + (h : CancelWA N Z.T E R Bf ab b) : SchedStepNWA (st := st) N E R Z m σ s b ∨ BufW Bf b := by + rcases h with h | h | h + · exact ewrite_stepNA_buf hP h + · exact Or.inl (nwrite_stepNA hP h) + · subst h; exact Or.inl hab + +/-- **The idle-bound-receive arm of `send_signal` writes `SchedStepNWA ∨ BufW Bf`** (module docstring). -/ +theorem send_signal_idle_bound_recv_stepNA {SR : S → Fl4SpecA64.State Unit → Prop} {DL : S → U64 → Prop} + (hP0 : TcbPlaced Z.T) (hH : SchedHypsA st N Z.T SR) (hO : SchedOpsA st ar N Z.T SR DL) + (hF : SchedFootNA st ar N Z.T SR DL (fun σ s => MWrA (st := st) Z m σ s)) + (hOE : SchedOpsEA st Z.T E R Bf SR) (hP : IPlacedA N Z.T E R Bf) (a : A) + (hR : NtfnStateRel N Z.T (L.mem s) σ) + (hRE : EpStateRel Z.T E R Bf (L.mem s) σ) (hSR : SR s σ) {n : U64} (hn : n ∈ N) + {x : Fl4SpecA64.Notification} (hk : σ.kheap (specPtr n) = some (.notification x)) (hidle : x.ntfnObj = .idleNtfn) + {t : Fl4SpecA64.ObjRef} {tcb : Fl4SpecA64.Tcb} (hb : x.ntfnBoundTcb = some t) (hkt : σ.kheap t = some (.tcb tcb)) + (hrb : Fl4SpecA64.receiveBlocked tcb.tcbState = true) + (hrep : RepOKA σ t) (heE : L.mem s (tBlockA (L.mem s (nBoundA n))) ∈ E) + (hz : ∀ e', Fl4SpecA64.epBlocked tcb.tcbState = some e' → + ∃ z, σ.kheap e' = some (.endpoint z) ∧ t ∈ z.epQueue) (badge : U64) + (hon : L.mem s (affA (L.mem s (nBoundA n))) = L.cpu s) (hdl : DL s n) + (hAct : Aligned (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION)) + (hab : SchedStepNWA (st := st) N E R Z m σ s (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION)) : + notification.send_signal st ar s a n badge ⦃ r => ¬ (Fl4SpecA64.sendSignal (specPtr n) badge.bv σ).2 ∧ + ∃ σ', ((), σ') ∈ (Fl4SpecA64.sendSignal (specPtr n) badge.bv σ).1 ∧ NtfnStateRel N Z.T (L.mem r.1) σ' ∧ + SR r.1 σ' ∧ WritesIn st s r.1 (fun b => SchedStepNWA (st := st) N E R Z m σ s b ∨ BufW Bf b) ⦄ := by + refine WP.spec_mono (send_signal_idle_bound_recvF hH hO hF hOE hP s a σ hR hRE hSR hn hk hidle hb hkt hrb hrep heE + hz badge hon hdl hAct) ?_ + rintro r ⟨h1, σ', h2, h3, h4, hw⟩ + refine ⟨h1, σ', h2, h3, h4, hw.mono fun b hb => ?_⟩ + rcases hb with hb | hb | hb | hb + · exact ewrite_stepNA_buf hP0 hb + · exact Or.inl (nwrite_stepNA hP0 hb) + · exact Or.inl (Or.inl (Or.inl hb)) + · subst hb; exact Or.inl hab + + +/-- A `SingleRel` outcome writes `SchedStepNWA`. -/ +theorem SingleRel.stepN (hP : TcbPlaced Z.T) {spec : Set (Unit × Fl4SpecA64.State Unit) × Prop} {s' : S} + (h : SingleRel (st := st) N Z.T s σ spec s') : + WritesIn st s s' (SchedStepNWA (st := st) N E Rp Z m σ s) := by + obtain ⟨-, -, -, -, w⟩ := h + exact w.mono fun _ hb => nwrite_stepNA hP hb + +/-- **Idle, no bound TCB, with its write set.** -/ +theorem send_signal_idle_unbound_stepNA (hP0 : TcbPlaced Z.T) (hP : NPlaced Z.T N) (a : A) + (hR : NtfnStateRel N Z.T (L.mem s) σ) {n : U64} (hn : n ∈ N) {x : Fl4SpecA64.Notification} + (hk : σ.kheap (specPtr n) = some (.notification x)) (hidle : x.ntfnObj = .idleNtfn) + (hb : x.ntfnBoundTcb = none) (badge : U64) : + notification.send_signal st ar s a n badge ⦃ r => + r.2 = a ∧ SingleRel (st := st) N Z.T s σ (Fl4SpecA64.sendSignal (specPtr n) badge.bv σ) r.1 ∧ + WritesIn st s r.1 (SchedStepNWA (st := st) N E Rp Z m σ s) ⦄ := + WP.spec_mono (send_signal_idle_unbound hP s a σ hR hn hk hidle hb badge) fun r ⟨h1, h2⟩ => + ⟨h1, h2, SingleRel.stepN hP0 h2⟩ + +/-- **Idle, bound TCB not receiving, with its write set.** -/ +theorem send_signal_idle_bound_idle_stepNA (hP0 : TcbPlaced Z.T) (hP : NPlaced Z.T N) (a : A) + (hR : NtfnStateRel N Z.T (L.mem s) σ) {n : U64} (hn : n ∈ N) {x : Fl4SpecA64.Notification} + (hk : σ.kheap (specPtr n) = some (.notification x)) (hidle : x.ntfnObj = .idleNtfn) + {t : Fl4SpecA64.ObjRef} {tcb : Fl4SpecA64.Tcb} (hb : x.ntfnBoundTcb = some t) (hkt : σ.kheap t = some (.tcb tcb)) + (hrb : Fl4SpecA64.receiveBlocked tcb.tcbState = false) (hvm : tcb.tcbState ≠ .ext .runningVM) (badge : U64) : + notification.send_signal st ar s a n badge ⦃ r => + r.2 = a ∧ SingleRel (st := st) N Z.T s σ (Fl4SpecA64.sendSignal (specPtr n) badge.bv σ) r.1 ∧ + WritesIn st s r.1 (SchedStepNWA (st := st) N E Rp Z m σ s) ⦄ := + WP.spec_mono (send_signal_idle_bound_idle hP s a σ hR hn hk hidle hb hkt hrb hvm badge) fun r ⟨h1, h2⟩ => + ⟨h1, h2, SingleRel.stepN hP0 h2⟩ + +/-- **Active, with its write set.** -/ +theorem send_signal_active_stepNA (hP0 : TcbPlaced Z.T) (hP : NPlaced Z.T N) (a : A) + (hR : NtfnStateRel N Z.T (L.mem s) σ) {n : U64} (hn : n ∈ N) {x : Fl4SpecA64.Notification} + (hk : σ.kheap (specPtr n) = some (.notification x)) {b0 : Fl4SpecA64.Badge} (hact : x.ntfnObj = .activeNtfn b0) + (badge : U64) : + notification.send_signal st ar s a n badge ⦃ r => + r.2 = a ∧ SingleRel (st := st) N Z.T s σ (Fl4SpecA64.sendSignal (specPtr n) badge.bv σ) r.1 ∧ + WritesIn st s r.1 (SchedStepNWA (st := st) N E Rp Z m σ s) ⦄ := + WP.spec_mono (send_signal_active hP s a σ hR hn hk hact badge) fun r ⟨h1, h2⟩ => + ⟨h1, h2, SingleRel.stepN hP0 h2⟩ + +/-- **Waiting, with its write set** (`send_signal_waitingF`, `FpNA.stepN`). -/ +theorem send_signal_waiting_stepNA {SR : S → Fl4SpecA64.State Unit → Prop} {DL : S → U64 → Prop} + (hP0 : TcbPlaced Z.T) (hH : SchedHypsA st N Z.T SR) (hO : SchedOpsA st ar N Z.T SR DL) + (hF : SchedFootNA st ar N Z.T SR DL (fun σ s => MWrA (st := st) Z m σ s)) (hP : NPlaced Z.T N) (a : A) + (hR : NtfnStateRel N Z.T (L.mem s) σ) (hSR : SR s σ) {n : U64} (hn : n ∈ N) + {x : Fl4SpecA64.Notification} (hk : σ.kheap (specPtr n) = some (.notification x)) {q : List Fl4SpecA64.ObjRef} + (hw : x.ntfnObj = .waitingNtfn q) (badge : U64) + (hon : ∀ d ∈ Z.T, specPtr d ∈ q → L.mem s (affA d) = L.cpu s) (hdl : DL s n) : + notification.send_signal st ar s a n badge ⦃ r => ¬ (Fl4SpecA64.sendSignal (specPtr n) badge.bv σ).2 ∧ + ∃ σ', ((), σ') ∈ (Fl4SpecA64.sendSignal (specPtr n) badge.bv σ).1 ∧ NtfnStateRel N Z.T (L.mem r.1) σ' ∧ SR r.1 σ' ∧ + WritesIn st s r.1 (SchedStepNWA (st := st) N E Rp Z m σ s) ⦄ := + WP.spec_mono (send_signal_waitingF hH hO hF hP s a σ hR hSR hn hk hw badge hon hdl) + fun r ⟨h1, σ', h2, h3, h4, h5⟩ => ⟨h1, σ', h2, h3, h4, FpNA.stepN hP0 h5⟩ + +end + +end + +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/Stage3SignalIdleA.lean b/proofs/Fl4ProofsA64/P3/Stage3SignalIdleA.lean new file mode 100644 index 000000000..601647c8d --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SignalIdleA.lean @@ -0,0 +1,470 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2): **`send_signal` on the arms that wake no thread, at the record**: an idle +notification whose bound TCB (if any) is not receiving and runs no VCPU, or an active one. + +* `NObjW`: the words of one notification object; **`send_signal_quietA`**: in these arms FL4 writes only them, and the + spec only makes `n` active. +* **`JStage3A.of_ntfnQuietA`**: the record across that step (`Stage3IpcGlueA`'s `of_ipcStepA`). +* **`JSyscallA.signalQuietA`**: `decode_invocation` on a Notification send cap at a syscall entry. + +Named premises: [P] `ArchTotal`, `MaxSpi`, `ArchPlat`, `A64FrameMasks`, `hc`, `hmb`, `hmax`, `htu`. +New module. +-/ +import Fl4ProofsA64.P3.Stage3BodyOKA +import Fl4ProofsA64.P3.Stage3IpcGlueA +import Fl4ProofsA64.P3.Stage3RestartA +import Fl4ProofsA64.P3.Stage3RsSiteA +import Fl4ProofsA64.P3.Stage3SignalFootA +import Fl4ProofsA64.P3.IpcInvokeDispatchA +import Fl4ProofsA64.P3.IpcScGlueA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-- A terminating triple names its result. -/ +theorem exists_of_specA {α : Type} {m : Result α} {P : α → Prop} (h : m ⦃ r => P r ⦄) : ∃ x, m = ok x ∧ P x := by + revert h + cases m <;> simp [WP.spec_vis, WP.spec_div, WP.spec_ok] + +/-- The words of the notification object at `n` (its five fields). -/ +def NObjW (n b : U64) : Prop := n.val ≤ b.val ∧ b.val < n.val + 40 + +/-- The arms of `send_signal` that wake no thread. -/ +def QuietSigA (σ : State Unit) (x : Notification) : Prop := + (x.ntfnObj = .idleNtfn ∧ ∀ t, x.ntfnBoundTcb = some t → + ∃ y, σ.kheap t = some (.tcb y) ∧ receiveBlocked y.tcbState = false ∧ y.tcbState ≠ .ext .runningVM) ∨ + ∃ b0, x.ntfnObj = .activeNtfn b0 + +/-- The notification after a quiet signal. -/ +def quietNtfnA (x : Notification) (b : Badge) : Notification := + ntfnObjUpdate (fun _ => .activeNtfn b) x + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} {N T : Set U64} + +/-- **A quiet `send_signal`** (module docstring). -/ +theorem send_signal_quietA (hP : NPlaced T N) (s : S) (a : A) (σ : State Unit) + (hR : NtfnStateRel N T (L.mem s) σ) {n : U64} (hn : n ∈ N) {x : Notification} + (hk : σ.kheap (specPtr n) = some (.notification x)) (hq : QuietSigA σ x) (badge : U64) : + notification.send_signal st ar s a n badge ⦃ r => + r.2 = a ∧ ∃ b : Badge, + sendSignal (Z := Unit) (specPtr n) badge.bv σ = + ({((), updK σ (specPtr n) (.notification (quietNtfnA x b)))}, False) ∧ + NtfnStateRel N T (L.mem r.1) (updK σ (specPtr n) (.notification (quietNtfnA x b))) ∧ + WritesIn st s r.1 (NObjW n) ⦄ := by + have hx := hR.at_ntfn hn hk + have hna := hP.ntfn n hn + have eAl : Aligned (epTSA n) ∧ NObjW n (epTSA n) := by + rw [epTSA_eq] + refine ⟨nfield_aligned hna (by simp) (by simp), ?_⟩ + unfold NObjW + rw [nfield_val hna (by simp)]; simp + have bAl : Aligned (nBadgeA n) ∧ NObjW n (nBadgeA n) := by + rw [nBadgeA_eq] + refine ⟨nfield_aligned hna (by simp) (by simp), ?_⟩ + unfold NObjW + rw [nfield_val hna (by simp)]; simp + have w1 : ∀ (s : S) (v : U64), WritesIn st s (L.wr s (nBadgeA n) v) (NObjW n) := fun s v => + (writesIn_wr bAl.1).mono fun _ h => h ▸ bAl.2 + have w2 : ∀ (s : S) (v : U64), WritesIn st s (L.wr s (epTSA n) v) (NObjW n) := fun s v => + (writesIn_wr eAl.1).mono fun _ h => h ▸ eAl.2 + have w12 : ∀ (v v' : U64), WritesIn st s (L.wr (L.wr s (epTSA n) v) (nBadgeA n) v') (NObjW n) := fun v v' => + ((w2 s v).trans (w1 _ v')).mono fun _ h => by rcases h with h | h <;> exact h + rcases hq with ⟨hidle, hb⟩ | ⟨b0, hact⟩ + · cases hbt0 : x.ntfnBoundTcb with + | none => + have h0 : L.mem s (nBoundA n) = 0#u64 := optRef_none (hx.bound ▸ hbt0) + unfold notification.send_signal + step with obj.Ntfn.state.spec as ⟨v, hv⟩ + have : v = obj.NTFN_IDLE := by rw [hv]; exact stOf_idle hx.obj hidle + simp only [this, ↓reduceIte] + step with obj.Ntfn.bound_tcb.val as ⟨i, ei⟩ + step with obj.Tcb.is_null.spec as ⟨b, eb⟩ + simp only [eb, ei, show ntfnBtA n = nBoundA n from rfl, h0, decide_true, ↓reduceIte] + step + subst s1_post + obtain ⟨hR', -⟩ := set_active_rel hP hR hn hk hidle badge + exact ⟨badge.bv, sendSignal_activate hk hidle (Or.inl hbt0) badge.bv, hR', w12 _ _⟩ + | some t => + obtain ⟨tcb, hkt, hrb, hvm⟩ := hb t hbt0 + obtain ⟨hb0, hbt⟩ := optRef_some (hx.bound ▸ hbt0) + have hbT := hx.bound_mem hb0 + have hy := hR.at_tcb hbT (hbt ▸ hkt) + have hns : L.mem s (stA (L.mem s (nBoundA n))) ≠ sched.TS_BLOCKED_ON_RECV := by + intro e + have h3 := hy.st + rw [e, tsVals.2.2.1] at h3 + revert hrb h3 + cases tcb.tcbState with + | ext e => cases e <;> simp [tsCode] + | _ => simp [tsCode, receiveBlocked] + have hnvm : L.mem s (stA (L.mem s (nBoundA n))) ≠ sched.TS_RUNNING_VM := by + intro e + have h3 := hy.st + rw [e] at h3 + simp only [sched.TS_RUNNING_VM] at h3 + cases hst : tcb.tcbState with + | ext e => cases e; exact hvm hst + | _ => simp_all [tsCode] + unfold notification.send_signal + step with obj.Ntfn.state.spec as ⟨v, hv⟩ + have : v = obj.NTFN_IDLE := by rw [hv]; exact stOf_idle hx.obj hidle + simp only [this, ↓reduceIte] + step with obj.Ntfn.bound_tcb.val as ⟨i, ei⟩ + step with obj.Tcb.is_null.spec as ⟨b, eb⟩ + simp only [eb, ei, show ntfnBtA n = nBoundA n from rfl, hb0, decide_false, Bool.false_eq_true, ↓reduceIte] + step with obj.Tcb.state.spec as ⟨i1, ei1⟩ + simp only [ei1, ei, show ntfnBtA n = nBoundA n from rfl, hns, hnvm, ↓reduceIte] + step + subst s1_post + obtain ⟨hR', -⟩ := set_active_rel hP hR hn hk hidle badge + have hst' : L.mem (L.wr (L.wr s (epTSA n) + ((L.mem s (epTSA n) &&& ~~~3#u64) ||| (obj.NTFN_ACTIVE &&& 3#u64))) (nBadgeA n) badge) + (stA (L.mem s (nBoundA n))) = L.mem s (stA (L.mem s (nBoundA n))) := by + have h1 := nwr_wr (L := L) hP (K := 8#u64) hn (by simp) (by simp) s + ((L.mem s (epTSA n) &&& ~~~3#u64) ||| (obj.NTFN_ACTIVE &&& 3#u64)) + rw [← epTSA_eq] at h1 + have h2 := nwr_wr (L := L) hP (K := 16#u64) hn (by simp) (by simp) + (L.wr s (epTSA n) ((L.mem s (epTSA n) &&& ~~~3#u64) ||| (obj.NTFN_ACTIVE &&& 3#u64))) badge + rw [← nBadgeA_eq] at h2 + exact ((h1.trans h2).tcb _ hbT).1.st + step with obj.Tcb.state.spec as ⟨vm_state, hvs⟩ + simp only [hvs, hst', hnvm, decide_false, Bool.false_eq_true, ↓reduceIte, bind_ok, WP.spec_ok] + exact ⟨trivial, badge.bv, sendSignal_activate hk hidle (Or.inr ⟨t, tcb, hbt0, hkt, hrb⟩) badge.bv, hR', w12 _ _⟩ + · have hb0 : (L.mem s (nBadgeA n)).bv = b0 := by + have := hx.obj; rw [hact] at this; exact this.2.2 + unfold notification.send_signal + step with obj.Ntfn.state.spec as ⟨v, hv⟩ + have : v = obj.NTFN_ACTIVE := by rw [hv]; exact stOf_active hx.obj hact + simp only [this, ntfn_consts.1, ntfn_consts.2.1, ntfn_consts.2.2] + simp only [show (2#u64 : U64) ≠ 0#u64 by decide, show (2#u64 : U64) ≠ 1#u64 by decide, ↓reduceIte] + step + step + step + subst s1_post + obtain ⟨hR', -⟩ := set_badge_rel hP hR hn hk hact b + have e : b.bv = combineNtfnBadges badge.bv b0 := by + have h := b_post1 + rw [i_post] at h + exact h.trans ((BitVec.or_comm _ _).trans (congrArg (badge.bv ||| ·) hb0)) + rw [e] at hR' + exact ⟨_, sendSignal_active_eq hk hact badge.bv, hR', w1 _ _⟩ + +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s s' : S} {a : A} {σ : State Unit} {m : McState} + +set_option maxHeartbeats 1000000 in +/-- **What a write of a notification object keeps**, before the P1 facts at the end state. -/ +theorem JStage3A.ntfnObj_factsA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (hc : m.curCore < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p : U64} (hp : p ∈ N) {x : Notification} (hk : σ.kheap (specPtr p) = some (.notification x)) + (hq : ∀ q, x.ntfnObj ≠ .waitingNtfn q) {x' : Notification} (hsub : x'.ntfnObj.ntfnQueue = []) + (hbt : x'.ntfnBoundTcb = x.ntfnBoundTcb) (hsc : x'.ntfnSc = x.ntfnSc) + (w : WritesIn st s s' (NObjW p)) + (R' : NtfnStateRel N Z.T (L.mem s') (updK σ (specPtr p) (.notification x'))) : + (∃ Q Rl, SchedSRA st { Z with Q := Q, R := Rl } m κ s' (updK σ (specPtr p) (.notification x'))) ∧ + SpecSchedA σ (updK σ (specPtr p) (.notification x')) ∧ SSzA σ (updK σ (specPtr p) (.notification x')) ∧ + QKeepA σ (updK σ (specPtr p) (.notification x')) ∧ + EpStateRel Z.T E Rp Bf (L.mem s') (updK σ (specPtr p) (.notification x')) ∧ + ReplyStackRelA (L.mem s') Rp Z.C (updK σ (specPtr p) (.notification x')) (scNameA m) stack ∧ + IpcKeepA σ (updK σ (specPtr p) (.notification x')) ∧ + (∀ t ∈ Z.T, ∀ O : U64, O.val < 4096 → O.val % 8 = 0 → L.mem s' (wa t O) = L.mem s (wa t O)) ∧ + SInvXA (updK σ (specPtr p) (.notification x')) m n ∧ SIdleA (updK σ (specPtr p) (.notification x')) m n ∧ + BlkQueuedA Z.T E N (L.mem s') (updK σ (specPtr p) (.notification x')) ∧ + SchedValidA (updK σ (specPtr p) (.notification x')) := by + set o : KernelObject := .notification x' with ho + set σ' := updK σ (specPtr p) o with hσ' + have P := J.iplaced + have pH : H p = some .ntfn := J.utIpc.ntfn p hp + have same : ∀ q, q ≠ specPtr p → σ'.kheap q = σ.kheap q := fun q e => by + rw [hσ', updK_kheap, Function.update_of_ne e] + have atp : σ'.kheap (specPtr p) = some o := by rw [hσ', updK_kheap, Function.update_self] + have keepK : ∀ q ob, σ.kheap q = some ob → (∀ y, ob ≠ .notification y) → σ'.kheap q = some ob := + fun q ob h ne => by + rw [same q fun e => by rw [e, hk] at h; exact ne x (Option.some.inj h).symm] + exact h + have backK : ∀ q ob, σ'.kheap q = some ob → (∀ y, ob ≠ .notification y) → σ.kheap q = some ob := + fun q ob h ne => by + by_cases e : q = specPtr p + · rw [e, atp] at h + exact absurd (Option.some.inj h).symm (ne _) + · rwa [same q e] at h + have inP : ∀ b : U64, NObjW p b → p.val ≤ b.val ∧ b.val < p.val + 64 := fun b h => ⟨h.1, by unfold NObjW at h; omega⟩ + have HS : SpecSchedA σ σ' := + { kh := fun q => by + by_cases e : q = specPtr p + · exact Or.inr (Or.inr ⟨_, _, e ▸ hk, e ▸ atp, Or.inr (Or.inl ⟨_, _, rfl, rfl⟩)⟩) + · exact Or.inl (same q e) + irqS := rfl + irqN := rfl + asid := rfl + vcur := rfl + cdt := rfl + orig := rfl } + have SSz : SSzA σ σ' := fun q y k h => ⟨y, backK q _ h (fun _ e => by cases e)⟩ + have step : NtfnStep σ σ' := NtfnStep.ntfn hk hbt hsc + have noE : NoBlockE σ σ' := fun q y z hy hz => by + have e := backK q _ hz (fun _ e => by cases e) + rw [hy] at e + cases e + exact Or.inl ⟨rfl, id⟩ + have ops := (ipcSchedOpsA (st := st) (ar := ar) (κ := κ) P J.ipcPlace hc hmb hmax htu).1 + have wW : WritesIn st s s' (NWrite N Z.T) := w.mono fun b h => Or.inl ⟨p, hp, h⟩ + obtain ⟨Q, Rl, sr⟩ := ops.stable s s' σ σ' ⟨Z.Q, Z.R, J.sr⟩ wW step noE + -- words off the notification object + have off : ∀ b : U64, Aligned b → ¬ (p.val ≤ b.val ∧ b.val < p.val + 64) → L.mem s' b = L.mem s b := + fun b hb h => w.mem_eq hb fun c => h (inP b c) + have tcbW : ∀ t ∈ Z.T, ∀ O : U64, O.val < 4096 → O.val % 8 = 0 → L.mem s' (wa t O) = L.mem s (wa t O) := + fun t ht O h1 h2 => by + have hta := J.sr.core.tcbs t ht + have hfit := hta.fits + have hv : (wa t O).val = t.val + O.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have := P.n.disj p hp t ht + exact off _ (field_aligned hta h1 h2) (by omega) + have fe : FootEq Z.T E Rp Bf (L.mem s) (L.mem s') := by + intro b hb hf + refine off b hb fun h => ?_ + rcases hf with ⟨e, he, h1⟩ | ⟨r, hr, h1⟩ | ⟨t, ht, h1⟩ | h1 + · have := P.ne p hp e he; omega + · have := P.nr p hp r hr; omega + · have := P.n.disj p hp t ht + have hfit := (J.sr.core.tcbs t ht).fits + have : t.val ≤ b.val ∧ b.val < t.val + 4096 := by + rcases h1 with h1 | h1 + · unfold Endp.TF TOffN at h1 + omega + · unfold TL at h1 + omega + omega + · have := P.nb p hp b h1; omega + have kv : Endp.KView σ σ' := by + refine ⟨fun q y h => keepK q _ h (fun _ e => by cases e), fun q y h => ⟨y, keepK q _ h (fun _ e => by cases e), rfl⟩, + fun q y h => ⟨y, keepK q _ h (fun _ e => by cases e), rfl, rfl, rfl, rfl, rfl⟩, rfl⟩ + have ep : EpStateRel Z.T E Rp Bf (L.mem s') σ' := J.ep.frame P.e fe kv + have reply : ReplyStackRelA (L.mem s') Rp Z.C σ' (scNameA m) stack := by + refine J.reply.frame (fun r hr => ?_) (fun c _ y k h => ⟨y, k, keepK _ _ h (fun _ e => by cases e), rfl⟩) + (fun r _ x' h => ⟨x', backK _ _ h (fun _ e => by cases e), rfl⟩) + have hr3 := P.e.rep r hr + have dis := P.nr p hp r hr + have key : ∀ O : U64, O.val = 8 ∨ O.val = 16 → L.mem s' (wa r O) = L.mem s (wa r O) := by + intro O hO + have hv : (wa r O).val = r.val + O.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact off _ (by unfold Aligned; rw [hv]; omega) (by omega) + exact ⟨key _ (Or.inr (by simp [layout.reply.NEXT])), key _ (Or.inl (by simp [layout.reply.PREV]))⟩ + have K : IpcKeepA σ σ' := IpcKeepA.of_objs (fun q y h => keepK q _ h (fun _ e => by cases e)) + (fun q y k h => keepK q _ h (fun _ e => by cases e)) (fun q y k h => backK q _ h (fun _ e => by cases e)) + have sinv : SInvXA σ' m n := by + have same' : SchedSameA σ σ' := + { kh := fun q ko hko => by + have ne : ∀ y, ko ≠ .notification y := fun y e => by + rcases hko with ⟨_, e'⟩ | ⟨_, _, e'⟩ <;> rw [e] at e' <;> cases e' + exact ⟨fun h => backK q ko h ne, fun h => keepK q ko h ne⟩ + cur := rfl + idle := rfl + csc := rfl + rq := rfl + rls := rfl + time := rfl + cons := rfl } + refine J.sinv.of_spec same' rfl ?_ + have I := J.sinv.irqN + have capK : ∀ q ob b a', σ'.kheap q = some ob → capOf ob b = some a' → + σ.kheap q = some ob := fun q ob b a' h hc' => by + refine backK q ob h fun y e => ?_ + subst e + cases hc' + refine ⟨?_, fun irq hi ob b a' h hc' => ?_, fun q ob b a' h hc' => ?_⟩ + · have ct : ∀ k q, capTableAt k q σ' ↔ capTableAt k q σ := fun k q => by + unfold capTableAt + by_cases e : q = specPtr p + · rw [e, atp, hk] + constructor <;> rintro ⟨cs, h, -⟩ <;> simp [ho] at h + · rw [same q e] + obtain ⟨⟨inj, tab⟩, dom⟩ := I.node + exact ⟨⟨inj, fun irq h => (ct _ _).2 (tab irq h)⟩, fun irq h k c => dom irq h k ((ct _ _).1 c)⟩ + · exact I.slot irq hi ob b a' (capK _ _ _ _ h hc') hc' + · exact I.ref q ob b a' (capK _ _ _ _ h hc') hc' + have idleVt : ∀ c < n, vtCapA σ' (m.idleThreadOf c) = some .nullCap ∧ vcpuOfA σ' (m.idleThreadOf c) = none := by + intro c hc + obtain ⟨y, hy, -⟩ := J.sidle.f3 c hc + exact idleVtSameA (by rw [hy]; exact keepK _ _ hy (fun _ e => by cases e)) (J.sidle.idleSlot c hc) + (J.sidle.idleBound c hc) + have sidle : SIdleA σ' m n := + { f3 := fun c hc' => by + obtain ⟨y, hy, hs⟩ := J.sidle.f3 c hc' + exact ⟨y, keepK _ _ hy (fun _ e => by cases e), hs⟩ + f4 := fun y k h => J.sidle.f4 y k (backK _ _ h (fun _ e => by cases e)) + f5 := fun q y k h => J.sidle.f5 q y k (backK _ _ h (fun _ e => by cases e)) + f6 := J.sidle.f6.of_eq fun sl => by + unfold specCapAtA + by_cases e : sl.1 = specPtr p + · rw [e, atp, hk] + · rw [same _ e] + f7 := fun y k h => J.sidle.f7 y k (backK _ _ h (fun _ e => by cases e)) + idleSlot := fun c hc => (idleVt c hc).1 + idleBound := fun c hc => (idleVt c hc).2 } + have blkQ : BlkQueuedA Z.T E N (L.mem s') σ' := by + have tb : ∀ t ∈ Z.T, L.mem s' (tBlockA t) = L.mem s (tBlockA t) := fun t ht => by + rw [show tBlockA t = wa t 3336#u64 from rfl] + exact tcbW t ht _ (by simp) (by simp) + refine ⟨fun t ht y h e hb => ?_, fun t ht y h q hb => ?_⟩ + · obtain ⟨c, z, hz, hm⟩ := J.blkQ.ep t ht y (backK _ _ h (fun _ e => by cases e)) e hb + exact ⟨by rw [tb t ht]; exact c, z, keepK _ _ hz (fun _ e => by cases e), hm⟩ + · obtain ⟨c, z, l, hz, hl, hm⟩ := J.blkQ.ntfn t ht y (backK _ _ h (fun _ e => by cases e)) q hb + have ne : q ≠ specPtr p := fun e => by + rw [e, hk] at hz + cases hz + exact hq l hl + exact ⟨by rw [tb t ht]; exact c, z, l, by rw [same q ne]; exact hz, hl, hm⟩ + have sv : SchedValidA σ' := J.schedValid.congr rfl rfl rfl rfl rfl rfl + (fun q y k h => keepK q _ h (fun _ e => by cases e)) + (fun t y h => ⟨y, keepK t _ h (fun _ e => by cases e), rfl⟩) + (fun t y h => ⟨y, backK t _ h (fun _ e => by cases e), rfl, rfl⟩) + exact ⟨⟨Q, Rl, sr⟩, HS, SSz, QKeepA.upd hk (QObjA.ofNtfn (hsub ▸ List.nil_sublist _)), ep, reply, K, tcbW, sinv, + sidle, blkQ, sv⟩ + +set_option maxHeartbeats 1000000 in +/-- `of_ntfnQuietA` for any new notification object with an empty queue. -/ +theorem JStage3A.of_ntfnObjA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (hc : m.curCore < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p : U64} (hp : p ∈ N) {x : Notification} (hk : σ.kheap (specPtr p) = some (.notification x)) + (hq : ∀ q, x.ntfnObj ≠ .waitingNtfn q) {x' : Notification} (hsub : x'.ntfnObj.ntfnQueue = []) + (hbt : x'.ntfnBoundTcb = x.ntfnBoundTcb) (hsc : x'.ntfnSc = x.ntfnSc) + (w : WritesIn st s s' (NObjW p)) + (R' : NtfnStateRel N Z.T (L.mem s') (updK σ (specPtr p) (.notification x'))) + (bxi : BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s') + (rv : RunVcpuInv st Z.T s') + (sysl : SysL st H Z.Cs OS ia ie s') (symL : SymL st H Z.Cs s') + (userOK : Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s') + (j7 : J7A st (J7R2A st ar nA) Z H OS s' (updK σ (specPtr p) (.notification x')) m) : + ∃ Q Rl, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s' a (updK σ (specPtr p) (.notification x')) m := by + obtain ⟨⟨Q, Rl, sr⟩, HS, SSz, Qk, ep, reply, K, tcbW, sinv, sidle, blkQ, sv⟩ := + J.ntfnObj_factsA hc hmb hmax htu hp hk hq hsub hbt hsc w R' + have wN : WritesIn st s s' (SchedStepNWA (st := st) N E Rp Z m σ s) := + w.mono fun b h => Or.inr (Or.inl ⟨p, hp, h.1, by unfold NObjW at h; omega⟩) + refine ⟨Q, Rl, ?_⟩ + exact J.of_ipcStepA (Q := Q) (Rl := Rl) (curOf := Z.curOf) (curScOf := Z.curScOf) wN HS SSz Qk rfl (fun _ _ => rfl) + bxi sysl symL userOK j7 sr ep R' reply K + (fun t ht O h h8 => tcbW t ht O (by omega) h8) + sinv sidle rv blkQ sv + +set_option maxHeartbeats 1000000 in +/-- **The record across a quiet signal** (module docstring). -/ +theorem JStage3A.of_ntfnQuietA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (hc : m.curCore < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p : U64} (hp : p ∈ N) {x : Notification} (hk : σ.kheap (specPtr p) = some (.notification x)) + (hq : ∀ q, x.ntfnObj ≠ .waitingNtfn q) (b : Badge) + (w : WritesIn st s s' (NObjW p)) + (R' : NtfnStateRel N Z.T (L.mem s') (updK σ (specPtr p) (.notification (quietNtfnA x b)))) + (bxi : BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s') + (rv : RunVcpuInv st Z.T s') + (sysl : SysL st H Z.Cs OS ia ie s') (symL : SymL st H Z.Cs s') + (userOK : Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s') + (j7 : J7A st (J7R2A st ar nA) Z H OS s' (updK σ (specPtr p) (.notification (quietNtfnA x b))) m) : + ∃ Q Rl, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s' a (updK σ (specPtr p) (.notification (quietNtfnA x b))) m := + JStage3A.of_ntfnObjA J hc hmb hmax htu hp hk hq (x' := quietNtfnA x b) rfl rfl rfl w R' bxi rv sysl symL userOK j7 + +set_option maxHeartbeats 2000000 in +/-- **Notification send, quiet arms, at a syscall entry** (module docstring). -/ +theorem JSyscallA.signalQuietA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hc : m.curCore < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {p badge slot lbl len buf : U64} {recv blk call don fp : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Notification p badge true recv) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hp : p ∈ N) {x : Notification} (hk : σ.kheap (specPtr p) = some (.notification x)) + (hq : QuietSigA σ x) : + ∃ xc σ1 σ2 s2, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + sendSignal (Z := Unit) (specPtr p) badge.bv σ1 = ({((), σ2)}, False) ∧ + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don fp buf extra = + ok (.Ok (), s2, a) ∧ + (Sched.BodyOKA σ2 m ∧ σ2.curThread = σ.curThread) ∧ + ∃ Q Rl, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s2 a σ2 m := by + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hslot hcov J.actW J.running J.cur + have hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM := J.toJStage3EntryA.curNotVmA + obtain ⟨xc, hkc, hst, hsp, J1, -, -, -⟩ := J.toJStage3A.of_restartCurA RS.pl.choose_spec.choose_spec.choose_spec.choose_spec + RS.sym RS.run hnvm RS.cur RS.vs RS.vc + set s1 := L.wr s (stA (L.curW s)) sched.TS_RESTART with hs1 + set σ1 := stUpdA σ σ.curThread xc .restart with hσ1 + have ne1 : specPtr p ≠ σ.curThread := fun e => by rw [e, hkc] at hk; cases hk + have hk1 : σ1.kheap (specPtr p) = some (.notification x) := by + rw [hσ1]; simp only [stUpdA, setObjA, Function.update_of_ne ne1]; exact hk + have hq1 : QuietSigA σ1 x := by + rcases hq with ⟨hi, hb⟩ | h + · refine Or.inl ⟨hi, fun t ht => ?_⟩ + obtain ⟨y, hy, h1, h2⟩ := hb t ht + by_cases e : t = σ.curThread + · exact ⟨{ xc with tcbState := .restart }, by rw [hσ1, e]; simp [stUpdA, setObjA], rfl, by simp⟩ + · exact ⟨y, by rw [hσ1]; simp only [stUpdA, setObjA, Function.update_of_ne e]; exact hy, h1, h2⟩ + · exact Or.inr h + have hnw : ∀ q, x.ntfnObj ≠ .waitingNtfn q := fun q e => by + rcases hq with ⟨hi, -⟩ | ⟨b0, hb⟩ + · rw [hi] at e; cases e + · rw [hb] at e; cases e + have body := send_signal_quietA (st := st) (ar := ar) J1.iplaced.n s1 a σ1 J1.ntfn hp hk1 hq1 badge + obtain ⟨⟨s2, a2⟩, eb, ha2, b, hspec, R2, w⟩ := exists_of_specA body + simp only at ha2 eb R2 w + subst ha2 + have hdec : syscall.decode_invocation st ar s a2 lbl len slot (capA st s slot) blk call don fp buf extra = + ok (.Ok (), s2, a2) := by + rw [hcap, decodeNotification_programA] + simp only [↓reduceIte, ipcInvokeProgramA, cur_thread_ok, bind_ok, hst] + rw [show notification.send_signal st ar (L.wr s (stA (L.curW s)) sched.TS_RESTART) a2 p badge = ok (s2, a2) + from eb] + simp only [bind_ok] + rfl + have F := J.decodeFinA hA hM hPl hfm hslot hcov (rfl : capA st s slot = capA st s slot) hext hSC (lbl := lbl) + (by rw [hcap]; simp [DecArmS1aEnt, DecArmQH, DecArmQZ2, DecArmKQ2, DecArmKQ, capFam]) + (fun _ h => by rw [hcap] at h; cases h) (fun _ h => by rw [hcap] at h; cases h) hdec + (updK σ1 (specPtr p) (.notification (quietNtfnA x b))) m + obtain ⟨-, -, sysl, symL, -, userOK, j7⟩ := F + have pH : H p = some .ntfn := J1.utIpc.ntfn p hp + have pNt : p ∈ H.ntfns := pH + have rv1 : RunVcpuInv st H.tcbs s1 := J1.tcbs ▸ J1.runVcpu + have blk := Fl4ProofsA64.P1.notification.send_signal.tinv0 (ar := ar) hA s1 a2 p badge (BXI.blk J1.sys.ent.bxi) pNt + pH rv1 + have rvi := Fl4ProofsA64.P1.notification.send_signal.rvi (ar := ar) hA s1 a2 p badge (BXI.blk J1.sys.ent.bxi) pNt + pH rv1 + rw [eb] at blk rvi + have blk' : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s2 := by + simpa using (WP.spec_ok _).1 blk + have rvi' : RunVcpuInv st H.tcbs s2 := by simpa using (WP.spec_ok _).1 rvi + have bx := BlkOK.bxi blk' + have rv2 : RunVcpuInv st Z.T s2 := J1.tcbs ▸ rvi' + obtain ⟨Q, Rl, J2⟩ := J1.of_ntfnQuietA hc hmb hmax htu hp hk1 hnw b w R2 bx rv2 sysl symL userOK j7 + refine ⟨xc, σ1, _, s2, hkc, rfl, hsp, hspec, hdec, ?_, Q, Rl, J2⟩ + exact ⟨Sched.BodyOKA.ofKeep J hkc (Sched.BodyKeepA.ofOther _ (specPtr p) (fun _ _ e => by cases e) (fun _ e => by cases e)), rfl⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3SignalVmA.lean b/proofs/Fl4ProofsA64/P3/Stage3SignalVmA.lean new file mode 100644 index 000000000..4afc01399 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SignalVmA.lean @@ -0,0 +1,315 @@ +/- +P3 (aarch64, A64-P3 Stage 3, S4 VCPU): **`send_signal` on an idle notification whose bound TCB is in +`TS_RUNNING_VM` (`.ext .runningVM`), at the record**, completing the non-receiving arms of `notification.send_signal`. + +On AArch64 (`crates/fl4-core/src/arch/aarch64/vcpu.rs`), `vcpu.signal_completes` always returns `ok false` +(pending notifications are checked by `vcpu.check` on VM-entry/return rather than completing the VCPU run +directly from `send_signal`). Therefore `notification.send_signal` sets the notification active with `badge` +and calls `notification.kick_guest` = `smp.remote_tcb_stall`, which under single-core (`[S] numNodes = 1`, +`[P] hCs`) stalls nothing and leaves memory/arch unchanged after `set_active`. + +* **`send_signal_vmA`**: `notification.send_signal` on an idle notification bound to a `.ext .runningVM` TCB, + given the `smp.remote_tcb_stall` no-op hypothesis. +* **`JStage3A.stall_after_ntfnWrA`**: under `[S] numNodes = 1` + `[P] hCs`, `smp.remote_tcb_stall` on any + `t ∈ Z.T` stalls nothing after a write inside `NObjW p` (`p ∈ N`). +* **`JSyscallA.signalRunningVmA`**: `decode_invocation` on a Notification send cap at a syscall entry for the + `TS_RUNNING_VM` bound-TCB arm (`JSyscallA → JStage3A`). +* **`JSyscallA.signalNonRecvA`**: unified wrapper combining `JSyscallA.signalQuietA` and + `JSyscallA.signalRunningVmA` for all non-receiving idle and active notifications. + +Named premises: [P] `ArchTotal`, `MaxSpi`, `ArchPlat`, `A64FrameMasks`, `hc`, `hmb`, `hmax`, `htu`, `hCs`; +[S] `h1 : fl4A64Platform.numNodes = 1`. +New module. +-/ +import Fl4ProofsA64.P3.Stage3SignalIdleA +import Fl4ProofsA64.P3.DelScStallA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} {N T : Set U64} + +/-- **`send_signal` on an idle notification bound to a `TS_RUNNING_VM` TCB** (module docstring). -/ +theorem send_signal_vmA (hP : NPlaced T N) (s : S) (a : A) (σ : State Unit) + (hR : NtfnStateRel N T (L.mem s) σ) {n : U64} (hn : n ∈ N) {x : Notification} + (hk : σ.kheap (specPtr n) = some (.notification x)) + (hidle : x.ntfnObj = .idleNtfn) + {t : ObjRef} (hbt0 : x.ntfnBoundTcb = some t) + {tcb : Tcb} (hkt : σ.kheap t = some (.tcb tcb)) + (hvm : tcb.tcbState = .ext .runningVM) + (badge : U64) + (hstall : ∀ s1, WritesIn st s s1 (NObjW n) → + smp.remote_tcb_stall st ar s1 a (L.mem s (nBoundA n)) = ok (s1, a)) : + notification.send_signal st ar s a n badge ⦃ r => + r.2 = a ∧ + sendSignal (Z := Unit) (specPtr n) badge.bv σ = + ({((), updK σ (specPtr n) (.notification (quietNtfnA x badge.bv)))}, False) ∧ + NtfnStateRel N T (L.mem r.1) (updK σ (specPtr n) (.notification (quietNtfnA x badge.bv))) ∧ + WritesIn st s r.1 (NObjW n) ⦄ := by + have hx := hR.at_ntfn hn hk + have hna := hP.ntfn n hn + have eAl : Aligned (epTSA n) ∧ NObjW n (epTSA n) := by + rw [epTSA_eq] + refine ⟨nfield_aligned hna (by simp) (by simp), ?_⟩ + unfold NObjW + rw [nfield_val hna (by simp)]; simp + have bAl : Aligned (nBadgeA n) ∧ NObjW n (nBadgeA n) := by + rw [nBadgeA_eq] + refine ⟨nfield_aligned hna (by simp) (by simp), ?_⟩ + unfold NObjW + rw [nfield_val hna (by simp)]; simp + have w1 : ∀ (s : S) (v : U64), WritesIn st s (L.wr s (nBadgeA n) v) (NObjW n) := fun s v => + (writesIn_wr bAl.1).mono fun _ h => h ▸ bAl.2 + have w2 : ∀ (s : S) (v : U64), WritesIn st s (L.wr s (epTSA n) v) (NObjW n) := fun s v => + (writesIn_wr eAl.1).mono fun _ h => h ▸ eAl.2 + have w12 : ∀ (v v' : U64), WritesIn st s (L.wr (L.wr s (epTSA n) v) (nBadgeA n) v') (NObjW n) := fun v v' => + ((w2 s v).trans (w1 _ v')).mono fun _ h => by rcases h with h | h <;> exact h + obtain ⟨hb0, hbt⟩ := optRef_some (hx.bound ▸ hbt0) + have hbT := hx.bound_mem hb0 + have hy := hR.at_tcb hbT (hbt ▸ hkt) + have hst_vm : L.mem s (stA (L.mem s (nBoundA n))) = sched.TS_RUNNING_VM := by + have h3 := hy.st + rw [hvm] at h3 + apply UScalar.eq_of_val_eq + simpa [tsCode, sched.TS_RUNNING_VM] using h3 + have hns : sched.TS_RUNNING_VM ≠ sched.TS_BLOCKED_ON_RECV := by + simp [sched.TS_RUNNING_VM, sched.TS_BLOCKED_ON_RECV] + have hrb : receiveBlocked tcb.tcbState = false := by rw [hvm]; rfl + unfold notification.send_signal + step with obj.Ntfn.state.spec as ⟨v, hv⟩ + have : v = obj.NTFN_IDLE := by rw [hv]; exact stOf_idle hx.obj hidle + simp only [this, ↓reduceIte] + step with obj.Ntfn.bound_tcb.val as ⟨i, ei⟩ + step with obj.Tcb.is_null.spec as ⟨b, eb⟩ + simp only [eb, ei, show ntfnBtA n = nBoundA n from rfl, hb0, decide_false, Bool.false_eq_true, ↓reduceIte] + step with obj.Tcb.state.spec as ⟨i1, ei1⟩ + simp only [ei1, hst_vm, hns, eq_self, ↓reduceIte] + simp only [vcpu.signal_completes, bind_ok, Bool.false_eq_true, ↓reduceIte] + step + subst s1_post + set s1 := L.wr (L.wr s (epTSA n) + ((L.mem s (epTSA n) &&& ~~~3#u64) ||| (obj.NTFN_ACTIVE &&& 3#u64))) (nBadgeA n) badge with hs1 + have hw12 : WritesIn st s s1 (NObjW n) := w12 _ _ + obtain ⟨hR', -⟩ := set_active_rel hP hR hn hk hidle badge + have hst' : L.mem s1 (stA (L.mem s (nBoundA n))) = sched.TS_RUNNING_VM := by + have h1 := nwr_wr (L := L) hP (K := 8#u64) hn (by simp) (by simp) s + ((L.mem s (epTSA n) &&& ~~~3#u64) ||| (obj.NTFN_ACTIVE &&& 3#u64)) + rw [← epTSA_eq] at h1 + have h2 := nwr_wr (L := L) hP (K := 16#u64) hn (by simp) (by simp) + (L.wr s (epTSA n) ((L.mem s (epTSA n) &&& ~~~3#u64) ||| (obj.NTFN_ACTIVE &&& 3#u64))) badge + rw [← nBadgeA_eq] at h2 + exact (((h1.trans h2).tcb _ hbT).1.st).trans hst_vm + step with obj.Tcb.state.spec as ⟨vm_state, hvs⟩ + simp only [hvs, hst', eq_self, ↓reduceIte, notification.kick_guest, hstall s1 hw12, WP.spec_ok] + exact ⟨trivial, sendSignal_activate hk hidle (Or.inr ⟨t, tcb, hbt0, hkt, hrb⟩) badge.bv, hR', hw12⟩ + +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {s s1 : S} {a : A} {σ : State Unit} {m : McState} + +/-- **Single-core `remote_tcb_stall` after a write inside a notification object** (module docstring). -/ +theorem JStage3A.stall_after_ntfnWrA + {J5 : JSlotA S} {R7 : Set U64 → JSlotA S} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7A st R7) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (h1 : fl4A64Platform.numNodes = 1) + {p : U64} (hp : p ∈ N) {t : U64} (ht : t ∈ Z.T) + (w : WritesIn st s s1 (NObjW p)) : + smp.remote_tcb_stall st ar s1 a t = ok (s1, a) := by + have hcpu : L.cpu s1 = L.cpu s := w.2.1 + have hon : ∀ u ∈ Z.T, L.mem s (affA u) = L.cpu s := fun u hu => by + have x := hCs _ (J.sr.core.aff u hu) + have y := hCs _ J.j.1.sch.cpuCs + exact UScalar.eq_of_val_eq (by omega) + have htcb : ∀ u ∈ Z.T, ∀ O : U64, O.val < 4096 → O.val % 8 = 0 → + L.mem s1 (wa u O) = L.mem s (wa u O) := fun u hu O hO h8 => by + have hua := J.sr.core.tcbs u hu + have hfit := hua.fits + have hv : (wa u O).val = u.val + O.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have hd := J.iplaced.n.disj p hp u hu + exact w.mem_eq (field_aligned hua hO h8) fun h => by unfold NObjW at h; omega + have haff : ∀ u ∈ Z.T, L.mem s1 (affA u) = L.cpu s1 := fun u hu => by + rw [hcpu, show affA u = wa u 3520#u64 from rfl, + htcb u hu 3520#u64 (by decide) (by decide)] + exact hon u hu + obtain ⟨_, _, _, _, hcol, -⟩ := J.j7 + have hvcpu : ∀ v ∈ Z.V, L.mem s1 (wa v 24#u64) = L.mem s (wa v 24#u64) := fun v hv => by + have hvH : H v = some (.other 12) := hcol.vh v hv + have hpH : H p = some .ntfn := J.utIpc.ntfn p hp + have hne : v ≠ p := fun e => by rw [e, hpH] at hvH; cases hvH + have hd := J.sys.ent.heap.disj v (.other 12) p .ntfn hvH hpH hne + have hvf := J.sys.ent.heap.fits v (.other 12) hvH + have hva := J.sys.ent.heap.align v (.other 12) hvH + simp only [show (HKind.other 12).bits = 12 from rfl, show HKind.ntfn.bits = 6 from rfl, + Nat.reducePow] at hd hvf hva + have h24 : (24#u64 : U64).val = 24 := rfl + have hval : (wa v 24#u64).val = v.val + 24 := by + rw [wa_valm, h24]; exact Nat.mod_eq_of_lt (by omega) + have hal : Aligned (wa v 24#u64) := by + unfold Aligned; rw [hval]; omega + exact w.mem_eq hal fun h => by + unfold NObjW at h + omega + refine stall_localA a (J.sr.core.tcbs t ht) (haff t ht) fun v ev hv => ?_ + rw [htcb t ht 3664#u64 (by decide) (by decide)] at ev + have hvV : v ∈ Z.V := by + rw [← ev] + exact (J.vcRun.tcb t ht).resolve_left (ev.symm ▸ hv) + rw [hvcpu v hvV] + rcases J.vcRun.vcpu v hvV with h0 | hcT + · exact Or.inl h0 + · exact Or.inr (haff _ hcT) + +set_option maxHeartbeats 2000000 in +/-- **Notification send, bound TCB in `TS_RUNNING_VM`, at a syscall entry** (module docstring). -/ +theorem JSyscallA.signalRunningVmA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hc : m.curCore < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (h1 : fl4A64Platform.numNodes = 1) + {p badge slot lbl len buf : U64} {recv blk call don fp : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Notification p badge true recv) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hp : p ∈ N) {x : Notification} (hk : σ.kheap (specPtr p) = some (.notification x)) + (hidle : x.ntfnObj = .idleNtfn) + {t : ObjRef} (hbt0 : x.ntfnBoundTcb = some t) + {tcb : Tcb} (hkt : σ.kheap t = some (.tcb tcb)) + (hvm : tcb.tcbState = .ext .runningVM) : + ∃ xc σ1 σ2 s2, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + sendSignal (Z := Unit) (specPtr p) badge.bv σ1 = ({((), σ2)}, False) ∧ + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don fp buf extra = + ok (.Ok (), s2, a) ∧ + ∃ Q Rl, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s2 a σ2 m := by + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hslot hcov J.actW J.running J.cur + have hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM := J.toJStage3EntryA.curNotVmA + obtain ⟨xc, hkc, hst, hsp, J1, -, -, -⟩ := J.toJStage3A.of_restartCurA RS.pl.choose_spec.choose_spec.choose_spec.choose_spec + RS.sym RS.run hnvm RS.cur RS.vs RS.vc + set s1 := L.wr s (stA (L.curW s)) sched.TS_RESTART with hs1 + set σ1 := stUpdA σ σ.curThread xc .restart with hσ1 + have ne1 : specPtr p ≠ σ.curThread := fun e => by rw [e, hkc] at hk; cases hk + have hk1 : σ1.kheap (specPtr p) = some (.notification x) := by + rw [hσ1]; simp only [stUpdA, setObjA, Function.update_of_ne ne1]; exact hk + have hx0 := J.toJStage3A.ntfn.at_ntfn hp hk + obtain ⟨hb0, hbt⟩ := optRef_some (hx0.bound ▸ hbt0) + have hbT : L.mem s (nBoundA p) ∈ Z.T := hx0.bound_mem hb0 + have hnet : t ≠ σ.curThread := by + intro e + have hy0 := J.toJStage3A.ntfn.at_tcb hbT (hbt ▸ hkt) + have hst_t : L.mem s (stA (L.mem s (nBoundA p))) = sched.TS_RUNNING_VM := by + have h3 := hy0.st + rw [hvm] at h3 + apply UScalar.eq_of_val_eq + simpa [tsCode, sched.TS_RUNNING_VM] using h3 + have hcur_eq : σ.curThread = specPtr (L.curW s) := J.toJStage3A.j.1.sch.curThread + have hptr_eq : L.mem s (nBoundA p) = L.curW s := + specPtr_inj (by rw [hbt, e, hcur_eq]) + rw [hptr_eq] at hst_t + exact hnvm hst_t + have hkt1 : σ1.kheap t = some (.tcb tcb) := by + rw [hσ1]; simp only [stUpdA, setObjA, Function.update_of_ne hnet]; exact hkt + have hx1 := J1.ntfn.at_ntfn hp hk1 + obtain ⟨hb0', -⟩ := optRef_some (hx1.bound ▸ hbt0) + have hbT1 : L.mem s1 (nBoundA p) ∈ Z.T := hx1.bound_mem hb0' + have hnw : ∀ q, x.ntfnObj ≠ .waitingNtfn q := fun q e => by rw [hidle] at e; cases e + have body := send_signal_vmA (st := st) (ar := ar) J1.iplaced.n s1 a σ1 J1.ntfn hp hk1 hidle hbt0 hkt1 hvm badge + (fun s' hw => J1.stall_after_ntfnWrA hCs h1 hp hbT1 hw) + obtain ⟨⟨s2, a2⟩, eb, ha2, hspec, R2, w⟩ := exists_of_specA body + simp only at ha2 eb R2 w + subst ha2 + have hdec : syscall.decode_invocation st ar s a2 lbl len slot (capA st s slot) blk call don fp buf extra = + ok (.Ok (), s2, a2) := by + rw [hcap, decodeNotification_programA] + simp only [↓reduceIte, ipcInvokeProgramA, cur_thread_ok, bind_ok, hst] + rw [show notification.send_signal st ar (L.wr s (stA (L.curW s)) sched.TS_RESTART) a2 p badge = ok (s2, a2) + from eb] + simp only [bind_ok] + rfl + have F := J.decodeFinA hA hM hPl hfm hslot hcov (rfl : capA st s slot = capA st s slot) hext hSC (lbl := lbl) + (by rw [hcap]; simp [DecArmS1aEnt, DecArmQH, DecArmQZ2, DecArmKQ2, DecArmKQ, capFam]) + (fun _ h => by rw [hcap] at h; cases h) (fun _ h => by rw [hcap] at h; cases h) hdec + (updK σ1 (specPtr p) (.notification (quietNtfnA x badge.bv))) m + obtain ⟨-, -, sysl, symL, -, userOK, j7⟩ := F + have pH : H p = some .ntfn := J1.utIpc.ntfn p hp + have pNt : p ∈ H.ntfns := pH + have rv1 : RunVcpuInv st H.tcbs s1 := J1.tcbs ▸ J1.runVcpu + have blk := Fl4ProofsA64.P1.notification.send_signal.tinv0 (ar := ar) hA s1 a2 p badge (BXI.blk J1.sys.ent.bxi) pNt + pH rv1 + have rvi := Fl4ProofsA64.P1.notification.send_signal.rvi (ar := ar) hA s1 a2 p badge (BXI.blk J1.sys.ent.bxi) pNt + pH rv1 + rw [eb] at blk rvi + have blk' : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s2 := by + simpa using (WP.spec_ok _).1 blk + have rvi' : RunVcpuInv st H.tcbs s2 := by simpa using (WP.spec_ok _).1 rvi + have bx := BlkOK.bxi blk' + have rv2 : RunVcpuInv st Z.T s2 := J1.tcbs ▸ rvi' + obtain ⟨Q, Rl, J2⟩ := J1.of_ntfnQuietA hc hmb hmax htu hp hk1 hnw badge.bv w R2 bx rv2 sysl symL userOK j7 + refine ⟨xc, σ1, _, s2, hkc, rfl, hsp, hspec, hdec, Q, Rl, J2⟩ + +/-- **Notification send, all non-receiving arms (idle + active, including `TS_RUNNING_VM`), at a syscall entry.** -/ +theorem JSyscallA.signalNonRecvA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hc : m.curCore < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (h1 : fl4A64Platform.numNodes = 1) + {p badge slot lbl len buf : U64} {recv blk call don fp : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Notification p badge true recv) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hp : p ∈ N) {x : Notification} (hk : σ.kheap (specPtr p) = some (.notification x)) + (hnonrecv : (x.ntfnObj = .idleNtfn ∧ ∀ t, x.ntfnBoundTcb = some t → + ∃ y, σ.kheap t = some (.tcb y) ∧ receiveBlocked y.tcbState = false) ∨ + ∃ b0, x.ntfnObj = .activeNtfn b0) : + ∃ xc σ1 σ2 s2, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + sendSignal (Z := Unit) (specPtr p) badge.bv σ1 = ({((), σ2)}, False) ∧ + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don fp buf extra = + ok (.Ok (), s2, a) ∧ + ∃ Q Rl, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s2 a σ2 m := by + rcases hnonrecv with ⟨hidle, hb⟩ | hact + · cases hbt0 : x.ntfnBoundTcb with + | none => + obtain ⟨xc, σ1, σ2, s2, h1', h2', h3', h4', h5', -, h6'⟩ := + J.signalQuietA hA hM hPl hfm hc hmb hmax htu (lbl := lbl) (len := len) (buf := buf) (blk := blk) + (call := call) (don := don) (fp := fp) hslot hcov hcap hext hSC hp hk + (Or.inl ⟨hidle, fun t ht => by rw [hbt0] at ht; cases ht⟩) + exact ⟨xc, σ1, σ2, s2, h1', h2', h3', h4', h5', h6'⟩ + | some t => + obtain ⟨y, hy, hrb⟩ := hb t hbt0 + by_cases hvm : y.tcbState = .ext .runningVM + · exact J.signalRunningVmA hA hM hPl hfm hc hmb hmax htu hCs h1 hslot hcov hcap hext hSC hp hk + hidle hbt0 hy hvm + · obtain ⟨xc, σ1, σ2, s2, h1', h2', h3', h4', h5', -, h6'⟩ := + J.signalQuietA hA hM hPl hfm hc hmb hmax htu (lbl := lbl) (len := len) (buf := buf) (blk := blk) + (call := call) (don := don) (fp := fp) hslot hcov hcap hext hSC hp hk + (Or.inl ⟨hidle, fun t' ht' => by + rw [hbt0] at ht'; cases ht'; exact ⟨y, hy, hrb, hvm⟩⟩) + exact ⟨xc, σ1, σ2, s2, h1', h2', h3', h4', h5', h6'⟩ + · obtain ⟨xc, σ1, σ2, s2, h1', h2', h3', h4', h5', -, h6'⟩ := + J.signalQuietA hA hM hPl hfm hc hmb hmax htu (lbl := lbl) (len := len) (buf := buf) (blk := blk) + (call := call) (don := don) (fp := fp) hslot hcov hcap hext hSC hp hk (Or.inr hact) + exact ⟨xc, σ1, σ2, s2, h1', h2', h3', h4', h5', h6'⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3SikxAlgA.lean b/proofs/Fl4ProofsA64/P3/Stage3SikxAlgA.lean new file mode 100644 index 000000000..2d8737307 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SikxAlgA.lean @@ -0,0 +1,132 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2b): **`SIKXA` algebra** for composing a Reply with the nested timeout send. + +* `TcbIpcNA.trans`, `TcbIpcNA.mono`, `ScIpcA.mono`, `ObjIpcA.mono`, **`SIKXA.mono`** (the mover sets `M`, `N` and the + donation flag widened), **`SIKXA.transX`** (two `SIKXA` steps), **`SIKXA.narrowN`** (a pending-queue set `N` whose + members end neither queued nor switch targets is dropped), `SIKXA.ofClr` (`SIKA` from `σ` or from `σ` with a fault cleared). + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3KEntrySendKeepA +import Fl4ProofsA64.P3.Stage3ReplyFaultGlueA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4ProofsA64.P3 Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Endp Fl4SpecA64 + +noncomputable section + +section +variable {i : ObjRef} {M N M' N' : ObjRef → Prop} {don don' : Bool} + +theorem TcbIpcNA.trans {p : ObjRef} {x y z : Tcb} (h : TcbIpcNA i M don p x y) (h' : TcbIpcNA i M don p y z) : + TcbIpcNA i M don p x z := by + refine ⟨h'.frame.trans h.frame, h'.buf.trans h.buf, h'.mcp.trans h.mcp, h'.yt.trans h.yt, h'.caps.trans h.caps, + ?_, ?_, h'.vcpu.trans h.vcpu, h'.flags.trans h.flags⟩ + · rcases h'.st with e | e + · rw [e]; exact h.st + · exact Or.inr e + · rcases h'.sc with e | e + · rw [e]; exact h.sc + · exact Or.inr e + +theorem TcbIpcNA.mono {p : ObjRef} {x y : Tcb} (h : TcbIpcNA i M don p x y) (hM : ∀ q, M q → M' q) + (hd : don = true → don' = true) : TcbIpcNA i M' don' p x y := + ⟨h.frame, h.buf, h.mcp, h.yt, h.caps, h.st.imp id fun e => ⟨hM _ e.1, e.2⟩, + h.sc.imp id fun e => ⟨hd e.1, e.2.1, e.2.2.imp_left (hM _)⟩, h.vcpu, h.flags⟩ + +theorem TcbIpcA.mono {p : ObjRef} {x y : Tcb} (h : TcbIpcA i M don p x y) (hM : ∀ q, M q → M' q) + (hd : don = true → don' = true) : TcbIpcA i M' don' p x y := + ⟨h.fault, h.frame, h.buf, h.mcp, h.yt, h.caps, h.st.imp id fun e => ⟨hM _ e.1, e.2⟩, + h.sc.imp id fun e => ⟨hd e.1, e.2.1, e.2.2.imp_left (hM _)⟩, h.vcpu, h.flags⟩ + +theorem ScIpcA.mono {p : ObjRef} {x y : SchedContext} (h : ScIpcA i don p x y) (hd : don = true → don' = true) : + ScIpcA i don' p x y := + ⟨h.yf, h.rm, h.ne, h.idle, h.th.imp id fun e => ⟨hd e.1, e.2⟩, fun e => h.reps (by + cases don + · rfl + · rw [hd rfl] at e; cases e)⟩ + +theorem ObjIpcA.mono {p : ObjRef} {a b : Option KernelObject} (h : ObjIpcA i M don p a b) (hM : ∀ q, M q → M' q) + (hd : don = true → don' = true) : ObjIpcA i M' don' p a b := by + cases h with + | same => exact .same _ + | tcb x y h => exact .tcb _ _ (h.mono hM hd) + | sc x y k h => exact .sc _ _ _ (h.mono hd) + | ep a b h => exact .ep _ _ fun u hu n => h u hu fun m => n (hM u m) + | reply a b h => exact .reply _ _ fun e => h (by + cases don + · rfl + · rw [hd rfl] at e; cases e) + +theorem SIKXA.mono {σ σ' : State Unit} (h : SIKXA i M N don σ σ') (hM : ∀ q, M q → M' q) (hN : ∀ q, N q → N' q) + (hd : don = true → don' = true) : SIKXA i M' N' don' σ σ' := by + refine ⟨h.frame, fun p => ?_, fun d p y hy => ?_, h.rls, ?_, h.rdy⟩ + · rcases h.obj p with o | ⟨x, y, e1, e2, g⟩ + · exact Or.inl (o.mono hM hd) + · exact Or.inr ⟨x, y, e1, e2, g.mono hM hd⟩ + · rcases h.rq d p y hy with a | a | a + · exact Or.inl a + · exact Or.inr (Or.inl (hN _ a)) + · exact Or.inr (Or.inr a) + · rcases h.act with a | a | ⟨y, ny, a⟩ + · exact Or.inl a + · exact Or.inr (Or.inl a) + · exact Or.inr (Or.inr ⟨y, hN _ ny, a⟩) + +/-- Two `SIKXA` steps. -/ +theorem SIKXA.transX {σ1 σ2 σ3 : State Unit} (h1 : SIKXA i M N don σ1 σ2) (h2 : SIKXA i M N don σ2 σ3) : + SIKXA i M N don σ1 σ3 := by + refine ⟨?_, fun p => ?_, fun d p y hy => ?_, fun y hy => h1.rls y (h2.rls y hy), ?_, fun g => h2.rdy (h1.rdy g)⟩ + · have a := h1.frame + have b := h2.frame + rw [a] at b + exact b + · rcases h2.obj p with o | ⟨y, z, e2, e3, g2⟩ + · rcases h1.obj p with o1 | ⟨x, y, e1, e2, g⟩ + · exact Or.inl (o1.trans o) + · rw [e2] at o + generalize eb : σ3.kheap p = b at o + cases o with + | same => exact Or.inr ⟨x, y, e1, rfl, g⟩ + | tcb _ z h' => exact Or.inr ⟨x, z, e1, rfl, g.trans h'.toN⟩ + · obtain ⟨x, e1, g1⟩ := h1.tcbB e2 + exact Or.inr ⟨x, z, e1, e3, g1.trans g2⟩ + · rcases h2.rq d p y hy with h | h | h + · exact h1.rq d p y h + · exact Or.inr (Or.inl h) + · rcases h1.act with e | e | ⟨z, hz, e⟩ + · exact Or.inr (Or.inr (e ▸ h)) + · rw [e] at h; cases h + · rw [e] at h; cases h; exact Or.inr (Or.inl hz) + · rcases h2.act with e | e | e + · rw [e]; exact h1.act + · exact Or.inr (Or.inl e) + · exact Or.inr (Or.inr e) + +/-- Dropping `N` when no member of it ends queued or a switch target. -/ +theorem SIKXA.narrowN {σ σ' : State Unit} (h : SIKXA i M N don σ σ') + (hq : ∀ y, N y → ∀ d p, y ∉ σ'.readyQueues d p) (hs : ∀ y, N y → σ'.schedulerAction ≠ .switchThread y) : + SIKXA i M N' don σ σ' := by + refine ⟨h.frame, h.obj, fun d p y hy => ?_, h.rls, ?_, h.rdy⟩ + · rcases h.rq d p y hy with a | a | a + · exact Or.inl a + · exact absurd hy (hq y a d p) + · exact Or.inr (Or.inr a) + · rcases h.act with a | a | ⟨y, ny, a⟩ + · exact Or.inl a + · exact Or.inr (Or.inl a) + · exact absurd a (hs y ny) + +/-- `SIKA` from `σ` or from `σ` with `t`'s fault cleared, as `SIKXA` from `σ`. -/ +theorem SIKXA.ofClr {σ σ0 σ' : State Unit} {t : ObjRef} (h : SIKA i M N don σ0 σ') + (hc : σ0 = σ ∨ ∃ y, σ.kheap t = some (.tcb y) ∧ σ0 = faultClrA σ t y) : SIKXA i M N don σ σ' := by + rcases hc with e | ⟨y, hy, e⟩ + · subst e; exact h.toX + · subst e; exact h.clrX hy + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3SiteA.lean b/proofs/Fl4ProofsA64/P3/Stage3SiteA.lean new file mode 100644 index 000000000..7d21c04be --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SiteA.lean @@ -0,0 +1,70 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Plan B): **the invocation site** of `JSyscallA.invocationA`'s `hScope`. + +* `SiteA`: the front end's values (`FrontJA`) and what every scope constructor asks of the slots: the invoked slot + and the nonzero extra slots are covered slots of `OS`, the caller is not `RunningVM`, the typed heap is placed. +* `JSyscallA.siteA`: the site from FL4's four prefix successes. + +Named premises: [J] `JSyscallA`. New module. +-/ +import Fl4ProofsA64.P3.Stage3InvSpecTopA +import Fl4ProofsA64.P3.Stage3PinFrontA +import Fl4ProofsA64.P3.Stage3PinVsA +import Fl4ProofsA64.P3.Stage3PinCnA +import Fl4ProofsA64.P3.Stage3SysPlaceA +import Fl4ProofsA64.P1.MdbIrqH +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-- **The invocation site** (module docstring). -/ +structure SiteA (st : machine.Store S) [L : LawfulStore st] (Z : XSetsA) (OS : Set U64) (s : S) (σ : State Unit) + (info : msginfo.MessageInfo) (buf slot : U64) (extra : Aeneas.Std.Array obj.Slot 3#usize) (mi : MessageInfo) + (sl : CslotPtr) (c : Cap) (excaps : List (Cap × CslotPtr)) (buffer : Option ObjRef) (argsU : List U64) : + Prop where + F : FrontJA st Z.th s σ info buf slot extra mi sl c excaps buffer argsU + nvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM + place : PlaceA Z.th (L.ksb s) + hOS : slot ∈ OS + cov : SlotCov st OS s slot + hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS + hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x + +/-- **The site from FL4's prefix successes** (module docstring). -/ +theorem JSyscallA.siteA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} + {κ : U64 → U64} {n : Nat} {J7 : JSlotA S} {s : S} {a : A} {σ : State Unit} {m : McState} + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + {cptr : U64} {info : msginfo.MessageInfo} {buf : U64} {r1 : cspace.Resolved} + {e : Aeneas.Std.Array obj.Slot 3#usize} + (einfo : msg.msg_info st s (L.curW s) = ok info) + (ebuf : msg.lookup_ipc_buffer st s false (L.curW s) = ok buf) + (elr : cspace.lookup_slot st s (L.curW s) cptr = ok (.Ok r1)) + (eex : ipc.lookup_extra_caps st s (L.curW s) buf info = ok (.Ok e)) : + ∃ mi sl c excaps buffer argsU, SiteA st Z OS s σ info buf r1.slot e mi sl c excaps buffer argsU := by + obtain ⟨mi, sl, c, excaps, buffer, argsU, F⟩ := J.frontJA einfo ebuf elr eex + have htZ : L.curW s ∈ Z.T := J.sr.curT + have hth : Z.th (L.curW s) = some .tcb := (J.j.1.thT _).2 htZ + have htOS : L.curW s ∈ OS := by + have h := J.j5.2.1 _ (isSlot_tcbA (i := 0#u64) hth (by simp)) + rwa [slotIdx_zeroA] at h + have hT := J.toJStage3A.slotTy + obtain ⟨r, er, hr⟩ := spec_existsA (cspace.lookup_slot.cov hT htOS cptr) + have hr1 := hr r1 (okinj (er.symm.trans elr)) + obtain ⟨re, ere, hre⟩ := spec_existsA (ipc.lookup_extra_caps.cov hT htOS buf info) + have hx := hre e (okinj (ere.symm.trans eex)) + exact ⟨mi, sl, c, excaps, buffer, argsU, F, J.toJStage3EntryA.curNotVmA, J.j.1.place, hr1.1, hr1.2, + fun x h h0 => (hx x h h0).1, fun x h h0 => (hx x h h0).2⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3SortA.lean b/proofs/Fl4ProofsA64/P3/Stage3SortA.lean new file mode 100644 index 000000000..2f4c55d47 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SortA.lean @@ -0,0 +1,273 @@ +/- +P3 (aarch64, A64-P3 Stage 3, record edit #8): **the spec invariants of the IPC queues and of the reply stacks**. + +* `IpcSortedA E N σ` (l4v `sorted_ipc_queues`, over the endpoints in `E` and the notifications in `N`): the queue is + sorted by descending TCB priority (the order `tcb_ep_append` / `tcb_ntfn_append` maintain). The record takes `E` / + `N` to be its tracked IPC objects (it knows nothing of the queues of other spec objects, so Retype could not keep an + unrestricted form). +* `ActRepA σ` (l4v `active_reply_scs`): an SC with a non-empty reply stack is active. +* `IpcSortedA.of_sub`: a step that keeps every TCB's priority and shrinks every queue to a sublist keeps sortedness. +* `ActRepA.of_keep`: a step that keeps every SC's refill maximum and does not grow its reply stack keeps `ActRepA`. + +No named premise. New module. +-/ +import Fl4SpecA64 + +namespace Fl4ProofsA64.P3 +open Fl4SpecA64 + +/-- A queue sorted by descending priority of its (TCB) members. -/ +def PrioSortedA (σ : State Unit) (q : List ObjRef) : Prop := + q.Pairwise fun a b => ∀ x y, σ.kheap a = some (.tcb x) → σ.kheap b = some (.tcb y) → + y.tcbPriority.toNat ≤ x.tcbPriority.toNat + +/-- **l4v `sorted_ipc_queues`**: the queues of the endpoints in `E` and the wait queues of the notifications in `N` +are priority sorted. -/ +def IpcSortedA (E N : Set ObjRef) (σ : State Unit) : Prop := + (∀ p ∈ E, ∀ x, σ.kheap p = some (.endpoint x) → PrioSortedA σ x.epQueue) ∧ + (∀ p ∈ N, ∀ x, σ.kheap p = some (.notification x) → PrioSortedA σ x.ntfnObj.ntfnQueue) + +/-- **l4v `active_reply_scs`**: an SC with replies is active. -/ +def ActRepA (σ : State Unit) : Prop := + ∀ p y k, σ.kheap p = some (.schedContext y k) → y.scReplies ≠ [] → scActive y = true + +/-- A step keeping the priorities and shrinking the queues to sublists keeps `IpcSortedA`. -/ +theorem IpcSortedA.of_sub {E N : Set ObjRef} {σ σ' : State Unit} (h : IpcSortedA E N σ) + (ht : ∀ t y, σ'.kheap t = some (.tcb y) → ∃ x, σ.kheap t = some (.tcb x) ∧ y.tcbPriority = x.tcbPriority) + (he : ∀ p x', σ'.kheap p = some (.endpoint x') → + ∃ x, σ.kheap p = some (.endpoint x) ∧ x'.epQueue.Sublist x.epQueue) + (hn : ∀ p x', σ'.kheap p = some (.notification x') → + ∃ x, σ.kheap p = some (.notification x) ∧ x'.ntfnObj.ntfnQueue.Sublist x.ntfnObj.ntfnQueue) : + IpcSortedA E N σ' := by + have mono : ∀ q : List ObjRef, PrioSortedA σ q → PrioSortedA σ' q := fun q hq => + hq.imp fun {a b} hab x' y' hx' hy' => by + obtain ⟨x, hx, ex⟩ := ht a x' hx' + obtain ⟨y, hy, ey⟩ := ht b y' hy' + rw [ex, ey] + exact hab x y hx hy + refine ⟨fun p hE x' hp => ?_, fun p hN x' hp => ?_⟩ + · obtain ⟨x, hx, hs⟩ := he p x' hp + exact mono _ ((List.Pairwise.sublist hs (h.1 p hE x hx))) + · obtain ⟨x, hx, hs⟩ := hn p x' hp + exact mono _ ((List.Pairwise.sublist hs (h.2 p hN x hx))) + +/-- A step keeping every SC's refill maximum and not growing its reply stack keeps `ActRepA`. -/ +theorem ActRepA.of_keep {σ σ' : State Unit} (h : ActRepA σ) + (hs : ∀ p y' k, σ'.kheap p = some (.schedContext y' k) → + ∃ y k0, σ.kheap p = some (.schedContext y k0) ∧ + (y'.scReplies ≠ [] → y.scReplies ≠ [] ∧ (scActive y = true → scActive y' = true))) : + ActRepA σ' := fun p y' k hp hr => by + obtain ⟨y, k0, hy, h1⟩ := hs p y' k hp + exact (h1 hr).2 (h p y k0 hy (h1 hr).1) + +/-- The spec's domain-schedule position and table are kept (with `SchedFrameA`, what the `domain` premises of the +selector may assume of the state they are asked at). (Record edit #8b-B1: moved here from `P3/ScDomainA` so that +`QKeepA` can carry it.) -/ +structure DomKeepA (σ σ' : State Unit) : Prop where + index : σ'.domainIndex = σ.domainIndex + start : σ'.domainStartIndex = σ.domainStartIndex + table : σ'.domainList = σ.domainList + /-- (record edit F4, `NdOKA.kroot`) The global user VSpace root is kept: no spec step assigns `armUsGlobalVspace`. -/ + gvs : σ'.archState.armUsGlobalVspace = σ.archState.armUsGlobalVspace + +theorem DomKeepA.refl (σ : State Unit) : DomKeepA σ σ := ⟨rfl, rfl, rfl, rfl⟩ + +theorem DomKeepA.trans {σ1 σ2 σ3 : State Unit} (h1 : DomKeepA σ1 σ2) (h2 : DomKeepA σ2 σ3) : DomKeepA σ1 σ3 := + ⟨h2.index.trans h1.index, h2.start.trans h1.start, h2.table.trans h1.table, h2.gvs.trans h1.gvs⟩ + +/-- **What a step must show for the record's two queue invariants** (`IpcSortedA`, `ActRepA`): TCB priorities kept, +endpoint and notification queues shrunk to sublists, SC reply stacks not grown and SC activity kept; for +`DeadExtA.replyTcb`, a reply object with no caller gets none; and (record edit #8b-B1, `domOK`) the domain-schedule +position and table kept. -/ +structure QKeepA (σ σ' : State Unit) : Prop where + prio : ∀ t y, σ'.kheap t = some (.tcb y) → ∃ x, σ.kheap t = some (.tcb x) ∧ y.tcbPriority = x.tcbPriority + ep : ∀ p x', σ'.kheap p = some (.endpoint x') → + ∃ x, σ.kheap p = some (.endpoint x) ∧ x'.epQueue.Sublist x.epQueue + ntfn : ∀ p x', σ'.kheap p = some (.notification x') → + ∃ x, σ.kheap p = some (.notification x) ∧ x'.ntfnObj.ntfnQueue.Sublist x.ntfnObj.ntfnQueue + sc : ∀ p y' k, σ'.kheap p = some (.schedContext y' k) → + ∃ y k0, σ.kheap p = some (.schedContext y k0) ∧ + (y'.scReplies ≠ [] → y.scReplies ≠ [] ∧ (scActive y = true → scActive y' = true)) + rt : ∀ p y, σ'.kheap p = some (.reply y) → ∃ x, σ.kheap p = some (.reply x) ∧ (x.replyTcb = none → y.replyTcb = none) + dom : DomKeepA σ σ' + +theorem QKeepA.refl (σ : State Unit) : QKeepA σ σ := + ⟨fun _ x h => ⟨x, h, rfl⟩, fun _ x h => ⟨x, h, List.Sublist.refl _⟩, fun _ x h => ⟨x, h, List.Sublist.refl _⟩, + fun _ y k h => ⟨y, k, h, fun r => ⟨r, id⟩⟩, fun _ y h => ⟨y, h, id⟩, DomKeepA.refl σ⟩ + +theorem QKeepA.of_kheap {σ σ' : State Unit} (h : σ'.kheap = σ.kheap) (D : DomKeepA σ σ') : QKeepA σ σ' := by + refine ⟨fun t x hx => ⟨x, by rw [← h]; exact hx, rfl⟩, fun p x hx => ⟨x, by rw [← h]; exact hx, List.Sublist.refl _⟩, + fun p x hx => ⟨x, by rw [← h]; exact hx, List.Sublist.refl _⟩, fun p y k hy => ⟨y, k, by rw [← h]; exact hy, fun r => ⟨r, id⟩⟩, + fun p y hy => ⟨y, by rw [← h]; exact hy, id⟩, D⟩ + +theorem QKeepA.trans {σ1 σ2 σ3 : State Unit} (h1 : QKeepA σ1 σ2) (h2 : QKeepA σ2 σ3) : QKeepA σ1 σ3 := by + refine ⟨fun t x hx => ?_, fun p x hx => ?_, fun p x hx => ?_, fun p y k hy => ?_, fun p y hy => ?_, h1.dom.trans h2.dom⟩ + · obtain ⟨y, hy, ey⟩ := h2.prio t x hx + obtain ⟨z, hz, ez⟩ := h1.prio t y hy + exact ⟨z, hz, ey.trans ez⟩ + · obtain ⟨y, hy, sy⟩ := h2.ep p x hx + obtain ⟨z, hz, sz⟩ := h1.ep p y hy + exact ⟨z, hz, sy.trans sz⟩ + · obtain ⟨y, hy, sy⟩ := h2.ntfn p x hx + obtain ⟨z, hz, sz⟩ := h1.ntfn p y hy + exact ⟨z, hz, sy.trans sz⟩ + · obtain ⟨y2, k2, hy2, a2⟩ := h2.sc p y k hy + obtain ⟨y1, k1, hy1, a1⟩ := h1.sc p y2 k2 hy2 + exact ⟨y1, k1, hy1, fun h => ⟨(a1 (a2 h).1).1, fun e => (a2 h).2 ((a1 (a2 h).1).2 e)⟩⟩ + · obtain ⟨y2, hy2, a2⟩ := h2.rt p y hy + obtain ⟨y1, hy1, a1⟩ := h1.rt p y2 hy2 + exact ⟨y1, hy1, fun h => a2 (a1 h)⟩ + +/-- `QKeepA` without the domain-schedule field: what the schedule tail (`next_domain` included) keeps. -/ +structure QKeepN (σ σ' : State Unit) : Prop where + prio : ∀ t y, σ'.kheap t = some (.tcb y) → ∃ x, σ.kheap t = some (.tcb x) ∧ y.tcbPriority = x.tcbPriority + ep : ∀ p x', σ'.kheap p = some (.endpoint x') → + ∃ x, σ.kheap p = some (.endpoint x) ∧ x'.epQueue.Sublist x.epQueue + ntfn : ∀ p x', σ'.kheap p = some (.notification x') → + ∃ x, σ.kheap p = some (.notification x) ∧ x'.ntfnObj.ntfnQueue.Sublist x.ntfnObj.ntfnQueue + sc : ∀ p y' k, σ'.kheap p = some (.schedContext y' k) → + ∃ y k0, σ.kheap p = some (.schedContext y k0) ∧ + (y'.scReplies ≠ [] → y.scReplies ≠ [] ∧ (scActive y = true → scActive y' = true)) + rt : ∀ p y, σ'.kheap p = some (.reply y) → ∃ x, σ.kheap p = some (.reply x) ∧ (x.replyTcb = none → y.replyTcb = none) + +theorem QKeepA.toN {σ σ' : State Unit} (Q : QKeepA σ σ') : QKeepN σ σ' := ⟨Q.prio, Q.ep, Q.ntfn, Q.sc, Q.rt⟩ + +theorem QKeepN.toA {σ σ' : State Unit} (Q : QKeepN σ σ') (D : DomKeepA σ σ') : QKeepA σ σ' := + ⟨Q.prio, Q.ep, Q.ntfn, Q.sc, Q.rt, D⟩ + +theorem QKeepN.of_kheap {σ σ' : State Unit} (h : σ'.kheap = σ.kheap) : QKeepN σ σ' := + ⟨fun t x hx => ⟨x, by rw [← h]; exact hx, rfl⟩, fun p x hx => ⟨x, by rw [← h]; exact hx, List.Sublist.refl _⟩, + fun p x hx => ⟨x, by rw [← h]; exact hx, List.Sublist.refl _⟩, + fun p y k hy => ⟨y, k, by rw [← h]; exact hy, fun r => ⟨r, id⟩⟩, + fun p y hy => ⟨y, by rw [← h]; exact hy, id⟩⟩ + +theorem QKeepN.refl (σ : State Unit) : QKeepN σ σ := QKeepN.of_kheap rfl + +theorem QKeepN.trans {σ1 σ2 σ3 : State Unit} (h1 : QKeepN σ1 σ2) (h2 : QKeepN σ2 σ3) : QKeepN σ1 σ3 := by + refine ⟨fun t x hx => ?_, fun p x hx => ?_, fun p x hx => ?_, fun p y k hy => ?_, fun p y hy => ?_⟩ + · obtain ⟨y, hy, ey⟩ := h2.prio t x hx + obtain ⟨z, hz, ez⟩ := h1.prio t y hy + exact ⟨z, hz, ey.trans ez⟩ + · obtain ⟨y, hy, sy⟩ := h2.ep p x hx + obtain ⟨z, hz, sz⟩ := h1.ep p y hy + exact ⟨z, hz, sy.trans sz⟩ + · obtain ⟨y, hy, sy⟩ := h2.ntfn p x hx + obtain ⟨z, hz, sz⟩ := h1.ntfn p y hy + exact ⟨z, hz, sy.trans sz⟩ + · obtain ⟨y2, k2, hy2, a2⟩ := h2.sc p y k hy + obtain ⟨y1, k1, hy1, a1⟩ := h1.sc p y2 k2 hy2 + exact ⟨y1, k1, hy1, fun h => ⟨(a1 (a2 h).1).1, fun e => (a2 h).2 ((a1 (a2 h).1).2 e)⟩⟩ + · obtain ⟨y2, hy2, a2⟩ := h2.rt p y hy + obtain ⟨y1, hy1, a1⟩ := h1.rt p y2 hy2 + exact ⟨y1, hy1, fun h => a2 (a1 h)⟩ + +/-- A step all of whose objects were there (a detype). -/ +theorem QKeepA.of_back {σ σ' : State Unit} (h : ∀ p o, σ'.kheap p = some o → σ.kheap p = some o) + (D : DomKeepA σ σ') : QKeepA σ σ' := + ⟨fun _ y e => ⟨y, h _ _ e, rfl⟩, fun _ x e => ⟨x, h _ _ e, List.Sublist.refl _⟩, + fun _ x e => ⟨x, h _ _ e, List.Sublist.refl _⟩, fun _ y k e => ⟨y, k, h _ _ e, fun r => ⟨r, id⟩⟩, + fun _ y e => ⟨y, h _ _ e, id⟩, D⟩ + +theorem IpcSortedA.mono {E E' N N' : Set ObjRef} {σ : State Unit} (h : IpcSortedA E N σ) (e : E' ⊆ E) (n : N' ⊆ N) : + IpcSortedA E' N' σ := + ⟨fun p hp => h.1 p (e hp), fun p hp => h.2 p (n hp)⟩ + +theorem IpcSortedA.of_q {E N : Set ObjRef} {σ σ' : State Unit} (h : IpcSortedA E N σ) (Q : QKeepA σ σ') : + IpcSortedA E N σ' := + h.of_sub Q.prio Q.ep Q.ntfn + +theorem ActRepA.of_q {σ σ' : State Unit} (h : ActRepA σ) (Q : QKeepA σ σ') : ActRepA σ' := + h.of_keep Q.sc + +/-- What replacing the object `o` by `o'` must show for `QKeepA`. -/ +structure QObjA (o o' : KernelObject) : Prop where + prio : ∀ y, o' = .tcb y → ∃ x, o = .tcb x ∧ y.tcbPriority = x.tcbPriority + ep : ∀ x', o' = .endpoint x' → ∃ x, o = .endpoint x ∧ x'.epQueue.Sublist x.epQueue + ntfn : ∀ x', o' = .notification x' → ∃ x, o = .notification x ∧ x'.ntfnObj.ntfnQueue.Sublist x.ntfnObj.ntfnQueue + sc : ∀ y' k, o' = .schedContext y' k → ∃ y k0, o = .schedContext y k0 ∧ + (y'.scReplies ≠ [] → y.scReplies ≠ [] ∧ (scActive y = true → scActive y' = true)) + rt : ∀ y, o' = .reply y → ∃ x, o = .reply x ∧ (x.replyTcb = none → y.replyTcb = none) + +theorem QObjA.ofTcb {x y : Tcb} (h : y.tcbPriority = x.tcbPriority) : QObjA (.tcb x) (.tcb y) := + ⟨fun _ e => by cases e; exact ⟨x, rfl, h⟩, (fun _ e => by cases e), (fun _ e => by cases e), + (fun _ _ e => by cases e), (fun _ e => by cases e)⟩ + +theorem QObjA.ofEp {x y : Endpoint} (h : y.epQueue.Sublist x.epQueue) : QObjA (.endpoint x) (.endpoint y) := + ⟨(fun _ e => by cases e), fun _ e => by cases e; exact ⟨x, rfl, h⟩, (fun _ e => by cases e), + (fun _ _ e => by cases e), (fun _ e => by cases e)⟩ + +theorem QObjA.ofNtfn {x y : Notification} (h : y.ntfnObj.ntfnQueue.Sublist x.ntfnObj.ntfnQueue) : + QObjA (.notification x) (.notification y) := + ⟨(fun _ e => by cases e), (fun _ e => by cases e), fun _ e => by cases e; exact ⟨x, rfl, h⟩, + (fun _ _ e => by cases e), (fun _ e => by cases e)⟩ + +theorem QObjA.ofSc {x y : SchedContext} {k : Nat} + (h : y.scReplies ≠ [] → x.scReplies ≠ [] ∧ (scActive x = true → scActive y = true)) : + QObjA (.schedContext x k) (.schedContext y k) := + ⟨(fun _ e => by cases e), (fun _ e => by cases e), (fun _ e => by cases e), + fun _ _ e => by cases e; exact ⟨x, k, rfl, h⟩, (fun _ e => by cases e)⟩ + +theorem QObjA.ofReply {x y : Reply} (h : x.replyTcb = none → y.replyTcb = none) : QObjA (.reply x) (.reply y) := + ⟨(fun _ e => by cases e), (fun _ e => by cases e), (fun _ e => by cases e), (fun _ _ e => by cases e), + fun _ e => by cases e; exact ⟨x, rfl, h⟩⟩ + +/-- Replacing one heap object. -/ +theorem QKeepA.upd {σ : State Unit} {p : ObjRef} {o o' : KernelObject} (h : σ.kheap p = some o) (R : QObjA o o') : + QKeepA σ { σ with kheap := Function.update σ.kheap p (some o') } := by + have key : ∀ q w, Function.update σ.kheap p (some o') q = some w → + (q = p ∧ w = o') ∨ (q ≠ p ∧ σ.kheap q = some w) := fun q w e => by + by_cases hq : q = p + · subst hq; rw [Function.update_self] at e; exact Or.inl ⟨rfl, (Option.some.inj e).symm⟩ + · rw [Function.update_of_ne hq] at e; exact Or.inr ⟨hq, e⟩ + refine ⟨fun q y e => ?_, fun q y e => ?_, fun q y e => ?_, fun q y k e => ?_, fun q y e => ?_, ⟨rfl, rfl, rfl, rfl⟩⟩ + · rcases key q _ e with ⟨rfl, ho⟩ | ⟨-, e'⟩ + · obtain ⟨x, hx, k⟩ := R.prio y ho.symm; exact ⟨x, by rw [h, hx], k⟩ + · exact ⟨y, e', rfl⟩ + · rcases key q _ e with ⟨rfl, ho⟩ | ⟨-, e'⟩ + · obtain ⟨x, hx, k⟩ := R.ep y ho.symm; exact ⟨x, by rw [h, hx], k⟩ + · exact ⟨y, e', List.Sublist.refl _⟩ + · rcases key q _ e with ⟨rfl, ho⟩ | ⟨-, e'⟩ + · obtain ⟨x, hx, k⟩ := R.ntfn y ho.symm; exact ⟨x, by rw [h, hx], k⟩ + · exact ⟨y, e', List.Sublist.refl _⟩ + · rcases key q _ e with ⟨rfl, ho⟩ | ⟨-, e'⟩ + · obtain ⟨x, k0, hx, k1⟩ := R.sc y k ho.symm; exact ⟨x, k0, by rw [h, hx], k1⟩ + · exact ⟨y, k, e', fun r => ⟨r, id⟩⟩ + · rcases key q _ e with ⟨rfl, ho⟩ | ⟨-, e'⟩ + · obtain ⟨x, hx, k⟩ := R.rt y ho.symm; exact ⟨x, by rw [h, hx], k⟩ + · exact ⟨y, e', id⟩ + +/-- What the IPC glue needs of a step for `ipcSorted`, `activeReplyScs` and `DeadExtA.replyTcb`: `QKeepA` with the +SC clause replaced by "`ActRepA` is kept" (a donating reply push grows the stack of an active SC). -/ +structure QSendA (σ σ' : State Unit) : Prop where + prio : ∀ t y, σ'.kheap t = some (.tcb y) → ∃ x, σ.kheap t = some (.tcb x) ∧ y.tcbPriority = x.tcbPriority + ep : ∀ p x', σ'.kheap p = some (.endpoint x') → + ∃ x, σ.kheap p = some (.endpoint x) ∧ x'.epQueue.Sublist x.epQueue + ntfn : ∀ p x', σ'.kheap p = some (.notification x') → + ∃ x, σ.kheap p = some (.notification x) ∧ x'.ntfnObj.ntfnQueue.Sublist x.ntfnObj.ntfnQueue + act : ActRepA σ → ActRepA σ' + rt : ∀ p y, σ'.kheap p = some (.reply y) → ∃ x, σ.kheap p = some (.reply x) ∧ (x.replyTcb = none → y.replyTcb = none) + dom : DomKeepA σ σ' + +theorem QKeepA.toSend {σ σ' : State Unit} (Q : QKeepA σ σ') : QSendA σ σ' := + ⟨Q.prio, Q.ep, Q.ntfn, fun h => h.of_q Q, Q.rt, Q.dom⟩ + +theorem IpcSortedA.of_qs {E N : Set ObjRef} {σ σ' : State Unit} (h : IpcSortedA E N σ) (Q : QSendA σ σ') : + IpcSortedA E N σ' := + h.of_sub Q.prio Q.ep Q.ntfn + +theorem QSendA.trans {σ1 σ2 σ3 : State Unit} (h1 : QSendA σ1 σ2) (h2 : QSendA σ2 σ3) : QSendA σ1 σ3 := by + refine ⟨fun t x hx => ?_, fun p x hx => ?_, fun p x hx => ?_, fun a => h2.act (h1.act a), fun p y hy => ?_, h1.dom.trans h2.dom⟩ + · obtain ⟨y, hy, ey⟩ := h2.prio t x hx + obtain ⟨z, hz, ez⟩ := h1.prio t y hy + exact ⟨z, hz, ey.trans ez⟩ + · obtain ⟨y, hy, sy⟩ := h2.ep p x hx + obtain ⟨z, hz, sz⟩ := h1.ep p y hy + exact ⟨z, hz, sy.trans sz⟩ + · obtain ⟨y, hy, sy⟩ := h2.ntfn p x hx + obtain ⟨z, hz, sz⟩ := h1.ntfn p y hy + exact ⟨z, hz, sy.trans sz⟩ + · obtain ⟨y2, hy2, a2⟩ := h2.rt p y hy + obtain ⟨y1, hy1, a1⟩ := h1.rt p y2 hy2 + exact ⟨y1, hy1, fun h => a2 (a1 h)⟩ + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3SortInsA.lean b/proofs/Fl4ProofsA64/P3/Stage3SortInsA.lean new file mode 100644 index 000000000..953b8d261 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SortInsA.lean @@ -0,0 +1,235 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2b-1): **the endpoint queues stay priority sorted across a blocking send**, and the +facts the record glue takes when a queue grows. + +* `pairwise_specInsA` / **`prioSorted_specInsA`**: ordered insertion (`tcb_append`, P3/NtfnBlockA's `specIns`) into a + sorted queue of TCBs gives a sorted queue. +* **`IpcSortedA.of_reorder`**: a queue reorder (remove, then ordered insert at the new priority) keeps `IpcSortedA`. +* **`prioSorted_of_specA`**: the FL4 queue of a related endpoint whose spec queue is sorted is `PrioSorted` over memory + (`send_ipc_blockA`'s `hsort`). +* `QBlkA`: what the IPC glue needs for `ipcSorted`, `activeReplyScs` and `DeadExtA.replyTcb`, with `IpcSortedA σ'` + given directly (`QSendA` asks for sublist queues, so it does not cover a queue that grows). + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3SortA +import Fl4ProofsA64.P3.NtfnBlockA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Endp Fl4SpecA64 + +noncomputable section + +theorem mem_specInsA {qs : List ObjRef} {g : ObjRef → Priority} {v : Priority} {t y : ObjRef} : + y ∈ specIns qs g v t ↔ y = t ∨ y ∈ qs := by + unfold specIns + conv_rhs => rw [← List.takeWhile_append_dropWhile + (p := fun y => @decide (g y ≥ v) (Classical.propDecidable _)) (l := qs)] + simp only [List.mem_append, List.mem_cons] + constructor + · rintro (h | h | h) + · exact Or.inr (Or.inl h) + · exact Or.inl h + · exact Or.inr (Or.inr h) + · rintro (h | h | h) + · exact Or.inr (Or.inl h) + · exact Or.inl h + · exact Or.inr (Or.inr h) + +/-- Ordered insertion keeps a list sorted by descending `g` sorted. -/ +theorem pairwise_specInsA {g : ObjRef → Priority} {v : Priority} {t : ObjRef} (hv : (g t).toNat = v.toNat) : + ∀ {qs : List ObjRef}, qs.Pairwise (fun a b => (g b).toNat ≤ (g a).toNat) → + (specIns qs g v t).Pairwise (fun a b => (g b).toNat ≤ (g a).toNat) + | [], _ => by simp [specIns] + | a :: qs, h => by + obtain ⟨ha, hq⟩ := List.pairwise_cons.1 h + by_cases c : g a ≥ v + · have e : specIns (a :: qs) g v t = a :: specIns qs g v t := by + unfold specIns + rw [List.takeWhile_cons_of_pos (by simpa using c), List.dropWhile_cons_of_pos (by simpa using c)] + rfl + rw [e] + refine List.pairwise_cons.2 ⟨fun y hy => ?_, pairwise_specInsA hv hq⟩ + rcases mem_specInsA.1 hy with rfl | hy + · rw [hv]; exact BitVec.le_def.1 c + · exact ha y hy + · have e : specIns (a :: qs) g v t = t :: a :: qs := by + unfold specIns + rw [List.takeWhile_cons_of_neg (by simpa using c), List.dropWhile_cons_of_neg (by simpa using c)] + rfl + rw [e] + refine List.pairwise_cons.2 ⟨fun y hy => ?_, h⟩ + have hav : (g a).toNat < v.toNat := by + have : ¬ v.toNat ≤ (g a).toNat := fun h => c (BitVec.le_def.2 h) + omega + rcases List.mem_cons.1 hy with rfl | hy + · omega + · have := ha y hy + omega + +/-- A queue of TCBs is `PrioSortedA` iff sorted by `prioOf`. -/ +theorem prioSortedA_iff {σ : State Unit} {q : List ObjRef} (h : ∀ a ∈ q, ∃ x, σ.kheap a = some (.tcb x)) : + PrioSortedA σ q ↔ q.Pairwise (fun a b => (prioOf σ b).toNat ≤ (prioOf σ a).toNat) := by + have e : ∀ a x, σ.kheap a = some (.tcb x) → prioOf σ a = x.tcbPriority := fun a x hx => by simp [prioOf, hx] + unfold PrioSortedA + constructor + · intro hs + refine hs.imp_of_mem fun {a b} ha hb hab => ?_ + obtain ⟨x, hx⟩ := h a ha + obtain ⟨y, hy⟩ := h b hb + rw [e a x hx, e b y hy] + exact hab x y hx hy + · intro hs + refine hs.imp fun {a b} hab x y hx hy => ?_ + rw [e a x hx, e b y hy] at hab + exact hab + +/-- **Ordered insertion keeps an endpoint queue priority sorted** (module docstring): in any state `σ'` keeping every +TCB's priority. -/ +theorem prioSorted_specInsA {σ σ' : State Unit} {q : List ObjRef} {t : ObjRef} {xt : Tcb} + (hkt : σ.kheap t = some (.tcb xt)) (hq : ∀ a ∈ q, ∃ x, σ.kheap a = some (.tcb x)) (hs : PrioSortedA σ q) + (hp : ∀ a x, σ.kheap a = some (.tcb x) → ∃ y, σ'.kheap a = some (.tcb y) ∧ y.tcbPriority = x.tcbPriority) : + PrioSortedA σ' (specIns q (prioOf σ) xt.tcbPriority t) := by + have hm : ∀ a ∈ specIns q (prioOf σ) xt.tcbPriority t, ∃ x, σ.kheap a = some (.tcb x) := fun a ha => by + rcases mem_specInsA.1 ha with rfl | ha + · exact ⟨xt, hkt⟩ + · exact hq a ha + have hv : (prioOf σ t).toNat = xt.tcbPriority.toNat := by simp [prioOf, hkt] + have h := pairwise_specInsA hv ((prioSortedA_iff hq).1 hs) + have same : ∀ a x, σ.kheap a = some (.tcb x) → prioOf σ' a = prioOf σ a := fun a x hx => by + obtain ⟨y, hy, e⟩ := hp a x hx + simp [prioOf, hx, hy, e] + refine (prioSortedA_iff fun a ha => ?_).2 (h.imp_of_mem fun {a b} ha hb hab => ?_) + · obtain ⟨x, hx⟩ := hm a ha + obtain ⟨y, hy, -⟩ := hp a x hx + exact ⟨y, hy⟩ + · obtain ⟨x, hx⟩ := hm a ha + obtain ⟨y, hy⟩ := hm b hb + rw [same a x hx, same b y hy] + exact hab + +/-- **An ordered insert keeps `IpcSortedA`**, for endpoint queues and notification wait queues: a step keeping every +TCB's priority, whose queues are each a sublist of the old one or the old one (of TCBs) with `t` inserted by priority +(`tcb_append`: blocking send, blocking Recv, Wait). -/ +theorem IpcSortedA.of_ins {E N : Set ObjRef} {σ σ' : State Unit} (h : IpcSortedA E N σ) {t : ObjRef} {xt : Tcb} + (hkt : σ.kheap t = some (.tcb xt)) + (hp : ∀ a x, σ.kheap a = some (.tcb x) → ∃ y, σ'.kheap a = some (.tcb y) ∧ y.tcbPriority = x.tcbPriority) + (hb : ∀ a y, σ'.kheap a = some (.tcb y) → ∃ x, σ.kheap a = some (.tcb x) ∧ y.tcbPriority = x.tcbPriority) + (he : ∀ p ∈ E, ∀ x', σ'.kheap p = some (.endpoint x') → ∃ x, σ.kheap p = some (.endpoint x) ∧ + (x'.epQueue.Sublist x.epQueue ∨ (x'.epQueue = specIns x.epQueue (prioOf σ) xt.tcbPriority t ∧ + ∀ a ∈ x.epQueue, ∃ y, σ.kheap a = some (.tcb y)))) + (hn : ∀ p ∈ N, ∀ x', σ'.kheap p = some (.notification x') → ∃ x, σ.kheap p = some (.notification x) ∧ + (x'.ntfnObj.ntfnQueue.Sublist x.ntfnObj.ntfnQueue ∨ + (x'.ntfnObj.ntfnQueue = specIns x.ntfnObj.ntfnQueue (prioOf σ) xt.tcbPriority t ∧ + ∀ a ∈ x.ntfnObj.ntfnQueue, ∃ y, σ.kheap a = some (.tcb y)))) : + IpcSortedA E N σ' := by + have mono : ∀ q : List ObjRef, PrioSortedA σ q → PrioSortedA σ' q := fun q hs => + hs.imp fun {a b} hab ya yb ha hb' => by + obtain ⟨xa, hxa, ea⟩ := hb a ya ha + obtain ⟨xb, hxb, eb⟩ := hb b yb hb' + rw [ea, eb] + exact hab xa xb hxa hxb + have key : ∀ q q' : List ObjRef, PrioSortedA σ q → + (q'.Sublist q ∨ (q' = specIns q (prioOf σ) xt.tcbPriority t ∧ ∀ a ∈ q, ∃ y, σ.kheap a = some (.tcb y))) → + PrioSortedA σ' q' := fun q q' hs c => by + rcases c with c | ⟨rfl, hq⟩ + · exact mono _ (List.Pairwise.sublist c hs) + · exact prioSorted_specInsA hkt hq hs hp + refine ⟨fun p hE x' hx' => ?_, fun p hN x' hx' => ?_⟩ + · obtain ⟨x, hx, c⟩ := he p hE x' hx' + exact key _ _ (h.1 p hE x hx) c + · obtain ⟨x, hx, c⟩ := hn p hN x' hx' + exact key _ _ (h.2 p hN x hx) c + +/-- **A reorder keeps `IpcSortedA`** (`set_priority` of a blocked thread: `reorder_ep` / `reorder_ntfn`, a remove and +an insert at the new priority): a step keeping every other TCB's priority, whose queues either shrink to sublists +without `t`, or are the old queue (of TCBs) with `t` removed and inserted again at its new priority. -/ +theorem IpcSortedA.of_reorder {E N : Set ObjRef} {σ σ' : State Unit} (h : IpcSortedA E N σ) {t : ObjRef} {yt : Tcb} + (hkt : σ'.kheap t = some (.tcb yt)) + (hp : ∀ a x, a ≠ t → σ.kheap a = some (.tcb x) → ∃ y, σ'.kheap a = some (.tcb y) ∧ y.tcbPriority = x.tcbPriority) + (hb : ∀ a y, a ≠ t → σ'.kheap a = some (.tcb y) → ∃ x, σ.kheap a = some (.tcb x) ∧ y.tcbPriority = x.tcbPriority) + (he : ∀ p ∈ E, ∀ x', σ'.kheap p = some (.endpoint x') → ∃ x, σ.kheap p = some (.endpoint x) ∧ + ((x'.epQueue.Sublist x.epQueue ∧ t ∉ x'.epQueue) ∨ + (x'.epQueue = specIns (removeAll t x.epQueue) (prioOf σ') yt.tcbPriority t ∧ + ∀ a ∈ x.epQueue, ∃ y, σ.kheap a = some (.tcb y)))) + (hn : ∀ p ∈ N, ∀ x', σ'.kheap p = some (.notification x') → ∃ x, σ.kheap p = some (.notification x) ∧ + ((x'.ntfnObj.ntfnQueue.Sublist x.ntfnObj.ntfnQueue ∧ t ∉ x'.ntfnObj.ntfnQueue) ∨ + (x'.ntfnObj.ntfnQueue = specIns (removeAll t x.ntfnObj.ntfnQueue) (prioOf σ') yt.tcbPriority t ∧ + ∀ a ∈ x.ntfnObj.ntfnQueue, ∃ y, σ.kheap a = some (.tcb y)))) : + IpcSortedA E N σ' := by + have mono : ∀ q q' : List ObjRef, PrioSortedA σ q → q'.Sublist q → t ∉ q' → PrioSortedA σ' q' := + fun q q' hs c nt => (List.Pairwise.sublist c hs).imp_of_mem fun {a b} ha hb' hab ya yb ea eb => by + obtain ⟨xa, hxa, ka⟩ := hb a ya (fun e => nt (e ▸ ha)) ea + obtain ⟨xb, hxb, kb⟩ := hb b yb (fun e => nt (e ▸ hb')) eb + rw [ka, kb] + exact hab xa xb hxa hxb + have key : ∀ q q' : List ObjRef, PrioSortedA σ q → + ((q'.Sublist q ∧ t ∉ q') ∨ (q' = specIns (removeAll t q) (prioOf σ') yt.tcbPriority t ∧ + ∀ a ∈ q, ∃ y, σ.kheap a = some (.tcb y))) → PrioSortedA σ' q' := fun q q' hs c => by + rcases c with ⟨c, nt⟩ | ⟨rfl, hq⟩ + · exact mono q q' hs c nt + · have nt : t ∉ removeAll t q := fun m => by + have := (List.mem_filter.1 m).2 + simp at this + have base := mono q _ hs List.filter_sublist nt + refine prioSorted_specInsA hkt (fun a ha => ?_) base (fun a x hx => ⟨x, hx, rfl⟩) + obtain ⟨x, hx⟩ := hq a (List.mem_filter.1 ha).1 + obtain ⟨y, hy, -⟩ := hp a x (fun e => nt (e ▸ ha)) hx + exact ⟨y, hy⟩ + refine ⟨fun p hE x' hx' => ?_, fun p hN x' hx' => ?_⟩ + · obtain ⟨x, hx, c⟩ := he p hE x' hx' + exact key _ _ (h.1 p hE x hx) c + · obtain ⟨x, hx, c⟩ := hn p hN x' hx' + exact key _ _ (h.2 p hN x hx) c + +/-- **What the IPC glue needs of a step** for `ipcSorted`, `activeReplyScs` and `DeadExtA.replyTcb`, the queue +invariant given at the new state. -/ +structure QBlkA (E N : Set ObjRef) (σ σ' : State Unit) : Prop where + sorted : IpcSortedA E N σ' + act : ActRepA σ → ActRepA σ' + rt : ∀ p y, σ'.kheap p = some (.reply y) → ∃ x, σ.kheap p = some (.reply x) ∧ (x.replyTcb = none → y.replyTcb = none) + dom : DomKeepA σ σ' + +theorem QSendA.toBlk {E N : Set ObjRef} {σ σ' : State Unit} (h : IpcSortedA E N σ) (Q : QSendA σ σ') : + QBlkA E N σ σ' := + ⟨h.of_qs Q, Q.act, Q.rt, Q.dom⟩ + +/-- **`QBlkA` without `rt`** (Lift 2c): the step may link a reply object that no thread was blocked on (a `Recv` +pushing the caller onto the receiver's unlinked reply); the glue then takes the dead replies' `replyTcb` as a separate +hypothesis (`hrt`). -/ +structure QBlkRA (E N : Set ObjRef) (σ σ' : State Unit) : Prop where + sorted : IpcSortedA E N σ' + act : ActRepA σ → ActRepA σ' + dom : DomKeepA σ σ' + +theorem QBlkA.toR {E N : Set ObjRef} {σ σ' : State Unit} (Q : QBlkA E N σ σ') : QBlkRA E N σ σ' := + ⟨Q.sorted, Q.act, Q.dom⟩ + +section +variable {T : Set U64} + +/-- **The FL4 queue of a related endpoint with a sorted spec queue is sorted over memory** (`send_ipc_blockA`'s +`hsort`). -/ +theorem prioSorted_of_specA {E R : Set U64} {Bf : Set (U64 × Word)} {m : U64 → U64} {σ : State Unit} (hT : TcbPlaced T) + (hR : EpStateRel T E R Bf m σ) {e : U64} {x : Endpoint} (hx : EpObjRel m T e x) (hs : PrioSortedA σ x.epQueue) : + ∀ xs, QueueRel m T .Ep (qOf m e) xs → P1.PrioSorted (fun y => m (prioA y)) xs := by + intro xs hq + obtain ⟨ys, h1, h2, -⟩ := hx.queue + have e1 : xs = ys := hq.unique hT h1 + subst e1 + rw [h2] at hs + unfold PrioSortedA at hs + rw [List.pairwise_map] at hs + refine hs.imp_of_mem fun {a b} ha hb hab => ?_ + obtain ⟨xa, hka, ra⟩ := hR.tcb a (hq.mem a ha) + obtain ⟨xb, hkb, rb⟩ := hR.tcb b (hq.mem b hb) + have h := hab xa xb hka hkb + rw [← ra.prio, ← rb.prio] at h + exact h + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3StallA.lean b/proofs/Fl4ProofsA64/P3/Stage3StallA.lean new file mode 100644 index 000000000..c6c888d5c --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3StallA.lean @@ -0,0 +1,64 @@ +/- +P3 (aarch64, A64-P3 Lift 1b): **the remote stalls of a syscall entry on one core stall nothing** +(`P3/DelScStallA`'s `stall_singleA` at `JSyscallA`). + +* `JSyscallA.stallTA`: the stall of a TCB of the record. +* `StallSitesA`: the stalls a non-arch decoder runs before it can return an `Err`: the invoked thread + (`tcb.decode`), the thread bound to the invoked SchedContext (`schedcontext.decode`). +* `JSyscallA.stallSitesA`: they hold for the cap of an `OS` slot. + +Premises: [P] `hCs`; [S] `h1` (`numNodes = 1`). New module. +-/ +import Fl4ProofsA64.P3.DelScStallA +import Fl4ProofsA64.P3.DelNtfnEntA +import Fl4ProofsA64.P3.Stage3SyscallA +import Fl4ProofsA64.P3.Stage3SysPlaceA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST + +noncomputable section + +section +variable {S A : Type} + +/-- **The stalls a non-arch decoder runs before an `Err`** (module docstring). -/ +def StallSitesA (st : machine.Store S) (ar : machine.Arch A) (s : S) (a : A) (cap : capability.Cap) : Prop := + (∀ t, cap = .Thread t → smp.remote_tcb_stall st ar s a t = ok (s, a)) ∧ + (∀ sc b, cap = .SchedContext sc b → ∃ i, obj.Sc.tcb st sc s = ok i ∧ + (if i != 0#u64 then smp.remote_tcb_stall st ar s a i else ok (s, a)) = ok (s, a)) + +variable {st : machine.Store S} [L : LawfulStore st] {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- **The stall of a TCB of the record** (module docstring). -/ +theorem JSyscallA.stallTA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < Fl4SpecA64.fl4A64Platform.numNodes) + (h1 : Fl4SpecA64.fl4A64Platform.numNodes = 1) {t : U64} (ht : t ∈ Z.T) : + smp.remote_tcb_stall st ar s a t = ok (s, a) := + (J.toJStage3A.toTA J.sact J.noSwitch).stall_singleA hCs h1 ht + +/-- **The decoder stalls of an `OS` slot's cap** (module docstring). -/ +theorem JSyscallA.stallSitesA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < Fl4SpecA64.fl4A64Platform.numNodes) + (h1 : Fl4SpecA64.fl4A64Platform.numNodes = 1) {slot : U64} (hsl : slot ∈ OS) : + StallSitesA st ar s a (capA st s slot) := by + refine ⟨fun t hc => J.stallTA hCs h1 (J.tcbs ▸ (J.toJStage3A.slotCapTy hsl).1.2.2.2.2.2 t hc), fun sc b hc => ?_⟩ + refine ⟨L.mem s (scTcbA sc), eq_of_spec_fd (Fl4ProofsA64.P1.obj.Sc.tcb.spec s sc), ?_⟩ + by_cases h0 : L.mem s (scTcbA sc) = 0#u64 + · rw [if_neg (by simp [h0])] + rw [if_pos (by simpa using h0)] + have hH : sc ∈ H.scs := ⟨_, (J.capTy slot hsl).2.2.2.1 sc b hc⟩ + have T := J.toJStage3A.toTA J.sact J.noSwitch + exact J.stallTA hCs h1 (J.tcbs ▸ T.blkA.2.2.1.2.2.yt.1 sc hH h0) + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3SysCombA.lean b/proofs/Fl4ProofsA64/P3/Stage3SysCombA.lean new file mode 100644 index 000000000..882dae965 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SysCombA.lean @@ -0,0 +1,143 @@ +/- +P3 (aarch64, A64-P3 Stage 3, line recvcomb): **ReplyRecv, NBSendRecv and NBSendWait at the kernel-entry top** (the +combined instance of `P3/Stage3SysFrontA`, mirror of `P3/Stage3SysRecvA`). + +* `CombScopeA`: the two scope premises of the combined body, **pinned to the reached state**: `first` is a + `FirstScopeA` at the post-budget state `(sb, τ)`; `second` is the `RecvScopeA` of the receive half at the state the + first half reached `(s3, σ3)`, guarded by the first half's concrete equations (FL4's `handle_invocation = ok` and the + spec outcome), never by "every `RecvPreA` state". +* `syscallComb_refinesA`: `kBodyA (.syscall _ rep call)` for `call` ∈ {ReplyRecv, NBSendRecv, NBSendWait} ends in + `A64RelS3` (`syscallFront_refinesA`, dispatch hole filled by `JSyscallA.combinedBodyScopeA`). +* `kernelEntryComb_recGA`: the end-to-end top, `kernelEntry_recGA` with that `hbody`. + +Declared [S] limits: F2 (a first half that performs a real invocation: Untyped / IRQ / Arch / Reply to a faulted +caller) and, with it, first-half `Err Preempted` (F3); every +limit of `RecvScopeA` for the second half. No new premise kind: those of `syscallSend_refinesA` and +`syscallRecv_refinesA` ([P] `ArchTotal`, `MaxSpi`, `ArchPlat`, `FaultEnvA`, `FinPlatA`, `ArchSwFA`, the four +preemption-path agreements, `PlainStore`; [J] `SysChargeA`; guarded `hGap` [P]/[S], `hNat` [S4]; [S] `hScope`). +New module. +-/ +import Fl4ProofsA64.P3.Stage3FirstScopeA +import Fl4ProofsA64.P3.Stage3SysRecvA +import Fl4ProofsA64.P3.Stage3SysSendA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +section +variable {S : Type} (st : machine.Store S) [L : LawfulStore st] {A : Type} (ar : machine.Arch A) + +/-- The register value a combined call names: the NBSend destination for `NBSendRecv`, else REPLY. -/ +def CombCptrA (s : S) (call : Syscall) : U64 := + L.mem s (regWA (L.curW s) (if call = .sysNBSendRecv then layout.reg.NBSEND_RECV_DEST else layout.reg.REPLY)) + +/-- [S] **The combined scope at the reached state** (module docstring). -/ +structure CombScopeA (OS N E Rp T : Set U64) (rep : Bool) (call : Syscall) (s : S) (a : A) (σ : State Unit) : Prop where + first : FirstScopeA st OS N E Rp s σ (CombCptrA st s call) + second : ∀ receiveReply, DispatchCombinedAtA call receiveReply → ∀ (s3 : S) (σ3 : State Unit), + syscall.handle_invocation st ar s a false false true true (CombCptrA st s call) = ok (.Ok (), s3, a) → + (.ok (), σ3) ∈ ((fl4HandleInvocationR (Z := Unit) rep false false true true + (Fl4SpecA64.toBl (CombCptrA st s call).bv)).run σ).1 → + RecvScopeA st OS N E Rp T s3 σ3 true receiveReply + +end + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR AR' : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {s : S} {a : A} {σ : State Unit} {m : McState} {Clock : A → MachineState → Prop} + +set_option maxHeartbeats 4000000 in +/-- **ReplyRecv, NBSendRecv and NBSendWait at the entry** (module docstring). -/ +theorem syscallComb_refinesA [PlainStore st] (hM : MaxSpi ar) (hPl : ArchPlat ar) (Env : FaultEnvA st ar Z) + (hlimit : fl4MaxReleaseTime = MAX_RELEASE_TIME) + (hag : fl4MinBudget = MIN_BUDGET) (hconv : Fl4TicksToUsLegacyAgree) (v2 : config_ARM_GIC_V3 = false) + (number : I64) (rep : Bool) (call : Syscall) (hnum : number = syscallNumberA call) + (hc : call = .sysReplyRecv ∨ call = .sysNBSendRecv ∨ call = .sysNBSendWait) + (I : KEntryFaultInA st ar AR AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ hPl.ipa m Clock s a σ) + (hCharge : SysChargeA st ar AR AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ hPl.ipa m Clock s a σ) + (hScope : ∀ (Q : U64 → U64 → List U64) (Rl : U64 → List U64) sb ab τ (τPC : State Unit), + fl4SyscallFaultIP (Z := Unit) σ.curThread σ = ({((), τPC)}, False) → + syscall.budget_ok st ar (L.wr s (Endp.regA (L.curW s) layout.reg.FAULT_IP) + (core.num.U64.wrapping_sub (L.mem s (regWA (L.curW s) layout.reg.NEXT_IP)) 4#u64)) a = ok (true, sb, ab) → + fl4BudgetOk (Z := Unit) τPC = ({(true, τ)}, False) → + JSyscallA st AR' { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes + (J5A st) (J7A st (J7R2A st ar hPl.ipa)) sb ab τ m → CounterStepA τ → + CombScopeA st ar OS N E Rp Z.T rep call sb ab τ) : + kBodyA st ar s a (.syscall number rep call) ⦃ r => + StepPostA (fun s' a' τ => A64RelS3 st AR' ar hPl.ipa fl4A64Platform.numNodes Z.Cs s' a' τ m) + (kOpA (.syscall number rep call)) σ r ⦄ := by + refine syscallFront_refinesA (Fin := fun s' a' τ => A64RelS3 st AR' ar hPl.ipa fl4A64Platform.numNodes Z.Cs s' a' τ m) + hPl Env number rep call hnum I hCharge ?_ (fun _ _ _ R => R) + intro Q Rl sb ab τ τPC hpc eb e1 Jr Cn + have Sc := hScope Q Rl sb ab τ τPC hpc eb e1 Jr Cn + have key : ∀ rr, DispatchCombinedAtA call rr → + syscall.dispatch_syscall st ar sb ab (syscallNumberA call) ⦃ r => + StepPostA (fun s' a' τ' => A64RelS3 st AR' ar hPl.ipa fl4A64Platform.numNodes Z.Cs s' a' τ' m ∧ BodyOKA τ' m) + (fl4SettleDispatch (maybeHandleInterrupt true) + (fl4DispatchSyscall (fl4HandleInvocationR (Z := Unit) rep) call)) τ r ⦄ := fun rr hcc => + Jr.combinedBodyScopeA Env.hA hM hPl (A64FrameMasks.of_plain sb) Env.hCs Env.h1 le_rfl hag Env.hmb Env.hmax Env.htu + hconv rep (maybeHandleInterrupt true) hcc rfl Sc.first (Sc.second rr hcc) + have h : syscall.dispatch_syscall st ar sb ab (syscallNumberA call) ⦃ r => + StepPostA (fun s' a' τ' => A64RelS3 st AR' ar hPl.ipa fl4A64Platform.numNodes Z.Cs s' a' τ' m ∧ BodyOKA τ' m) + (fl4SettleDispatch (maybeHandleInterrupt true) + (fl4DispatchSyscall (fl4HandleInvocationR (Z := Unit) rep) call)) τ r ⦄ := by + rcases hc with rfl | rfl | rfl + exacts [key _ .replyRecv, key _ .nbsendRecv, key _ .nbsendWait] + have e : fl4SettleDispatch (maybeHandleInterrupt true) + (fl4DispatchSyscall (fl4HandleInvocationR (Z := Unit) rep) call) = + NonDetE.catch (fl4HandleSyscall (Z := Unit) rep call) (fun _ => fl4PreemptionPathIrq) := by + rw [← preemptionPath_agreeA hlimit hag Env.hmax hconv v2] + unfold fl4SettleDispatch + rcases hc with rfl | rfl | rfl <;> + simp only [fl4DispatchSyscall, fl4HandleSyscall, fl4HandleSyscallWith] + rw [e] at h + refine WP.spec_mono h ?_ + rintro r ⟨τ2, mem, R, -⟩ + exact ⟨τ2, mem, R⟩ + +set_option maxHeartbeats 4000000 in +/-- **ReplyRecv, NBSendRecv and NBSendWait end to end** (module docstring): `kernelEntry_recGA` with `hbody` from +`syscallComb_refinesA`. -/ +theorem kernelEntryComb_recGA [PlainStore st] (hM : MaxSpi ar) (hPl : ArchPlat ar) (Env : FaultEnvA st ar Z) + (hlimit : fl4MaxReleaseTime = MAX_RELEASE_TIME) + (hag : fl4MinBudget = MIN_BUDGET) (hconv : Fl4TicksToUsLegacyAgree) (v2 : config_ARM_GIC_V3 = false) + (arch : ArchSwFA st ar) (P : FinPlatA ar AR') + {tm : A → Nat → DeadlineViewA} {view : State Unit → Fl4TimerView} + (number : I64) (rep : Bool) (call : Syscall) (hnum : number = syscallNumberA call) + (hc : call = .sysReplyRecv ∨ call = .sysNBSendRecv ∨ call = .sysNBSendWait) + (run : kRuns st s (.syscall number rep call)) + (I : KEntryFaultInA st ar AR AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ hPl.ipa m Clock s a σ) + (hCharge : SysChargeA st ar AR AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ hPl.ipa m Clock s a σ) + (hScope : ∀ (Q : U64 → U64 → List U64) (Rl : U64 → List U64) sb ab τ (τPC : State Unit), + fl4SyscallFaultIP (Z := Unit) σ.curThread σ = ({((), τPC)}, False) → + syscall.budget_ok st ar (L.wr s (Endp.regA (L.curW s) layout.reg.FAULT_IP) + (core.num.U64.wrapping_sub (L.mem s (regWA (L.curW s) layout.reg.NEXT_IP)) 4#u64)) a = ok (true, sb, ab) → + fl4BudgetOk (Z := Unit) τPC = ({(true, τ)}, False) → + JSyscallA st AR' { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes + (J5A st) (J7A st (J7R2A st ar hPl.ipa)) sb ab τ m → CounterStepA τ → + CombScopeA st ar OS N E Rp Z.T rep call sb ab τ) + (hGap : ∀ s' a' τ, kBodyA st ar s a (.syscall number rep call) = ok (s', a') → + ((), τ) ∈ (kOpA (.syscall number rep call) σ).1 → + A64RelS3 st AR' ar hPl.ipa fl4A64Platform.numNodes Z.Cs s' a' τ m → SchedGapFreeA st m s' a' τ ar tm view) + (hNat : ∀ s' a' τ, kBodyA st ar s a (.syscall number rep call) = ok (s', a') → + ((), τ) ∈ (kOpA (.syscall number rep call) σ).1 → + A64RelS3 st AR' ar hPl.ipa fl4A64Platform.numNodes Z.Cs s' a' τ m → ∀ τ1 τ2, + ((), τ1) ∈ (fl4Schedule kLimitA view τ).1 → ((), τ2) ∈ (fl4ActivateThread (Z := Unit) τ1).1 → + ∀ y, τ2.kheap τ2.curThread = some (.tcb y) → y.tcbState ≠ .ext .runningVM) : + kCodeA st ar s a (.syscall number rep call) ⦃ out => + StepPostA (KExitA st ar AR' hPl.ipa m) + (kOpA (.syscall number rep call) >>= fun _ => fl4Finish kLimitA view) σ out ⦄ := + kernelEntry_recGA Env.hA hPl arch Env.hCs P s a σ _ run + (syscallComb_refinesA hM hPl Env hlimit hag hconv v2 number rep call hnum hc I hCharge hScope) hGap hNat + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3SysFaultIpA.lean b/proofs/Fl4ProofsA64/P3/Stage3SysFaultIpA.lean new file mode 100644 index 000000000..c032649a7 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SysFaultIpA.lean @@ -0,0 +1,314 @@ +/- +P3 (aarch64, A64-P3 sysfront): **the syscall entry's `FAULT_IP` write** (`kBodyA (.syscall _)` / `fl4SyscallFaultIP`). + +* `JStage3A.of_faultIpWrA`: the record across a write into the current thread's `FAULT_IP` register (the clone of + `JStage3A.of_badgeSwA` for register offset 528; the notification relation by `NtfnStateRel.frame`). +* `JSyscallA.faultIpA`: the syscall-entry record `JSyscallA` at the post-write state (σ-level fields kept). + +No named premise beyond those of `of_badgeSwA` ([P] `ArchTotal`, `ArchPlat`, `hmb`, `hmax`, `htu`). New module. +-/ +import Fl4ProofsA64.P3.Stage3RecvPollA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4Spec Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s : S} {a : A} {σ : State Unit} {m : McState} {ar : machine.Arch A} + +theorem badgeOf_setReg_neA (x : Tcb) (r : Register) (v : Word) (h : r ≠ badge_register) : + badgeOf (setReg x r v) = badgeOf x := by + simp [badgeOf, setReg, setRegUc, archTcbContextSet, archTcbContextGet, Function.update_of_ne (Ne.symm h)] + +/-- The notification relation across a write of the `FAULT_IP` register (word `t + 528`) of a TCB. -/ +theorem set_fip_ntfnRel {N T : Set U64} (hP : NPlaced T N) {s : S} {σ : State Unit} + (hR : NtfnStateRel N T (L.mem s) σ) {t : U64} (ht : t ∈ T) {x : Tcb} + (hk : σ.kheap (specPtr t) = some (.tcb x)) (v : U64) : + NtfnStateRel N T (L.mem (L.wr s (wa t 528#u64) v)) + (updK σ (specPtr t) (.tcb (setReg x faultRegister v.bv))) := by + refine hR.frame hP ?_ ?_ + · intro b hb hf + refine rd_wr_ne (tword hP ht (by simp) (by simp)) hb ?_ + intro e + rcases hf with ⟨n', hn, h1, h2⟩ | ⟨y, hy, h | h | h | h | h | h⟩ + · have := congrArg UScalar.val e + rw [tfield_val (hP.tcb t ht) (by simp)] at this + have d := hP.disj n' hn t ht + simp at this + omega + · rw [h, stA_eq] at e + exact absurd ((tt_eq_iff (hP.tcb t ht) (hP.tcb y hy) (by simp) (by simp)).1 e).2 (by decide) + · rw [h, tBlockA_eq] at e + exact absurd ((tt_eq_iff (hP.tcb t ht) (hP.tcb y hy) (by simp) (by simp)).1 e).2 (by decide) + · rw [h, badgeRegA_eq] at e + exact absurd ((tt_eq_iff (hP.tcb t ht) (hP.tcb y hy) (by simp) (by simp)).1 e).2 (by decide) + · rw [h, prioA_eq'] at e + exact absurd ((tt_eq_iff (hP.tcb t ht) (hP.tcb y hy) (by simp) (by simp)).1 e).2 (by decide) + · rw [h, nextEp_eq] at e + exact absurd ((tt_eq_iff (hP.tcb t ht) (hP.tcb y hy) (by simp) (by simp)).1 e).2 (by decide) + · rw [h, prevEp_eq] at e + exact absurd ((tt_eq_iff (hP.tcb t ht) (hP.tcb y hy) (by simp) (by simp)).1 e).2 (by decide) + · constructor + · intro p y' h + have hne : p ≠ specPtr t := fun e => by rw [e, hk] at h; cases h + rw [updK_kheap, Function.update_of_ne hne]; exact h + · intro p y' h + by_cases e : p = specPtr t + · subst e; rw [hk] at h; cases h + exact ⟨setReg x faultRegister v.bv, by rw [updK_kheap, Function.update_self], rfl, + badgeOf_setReg_neA x faultRegister v.bv (by decide), rfl⟩ + · exact ⟨y', by rw [updK_kheap, Function.update_of_ne e]; exact h, rfl, rfl, rfl⟩ + +set_option maxHeartbeats 2000000 in +/-- **The record across a write into thread `t`'s `FAULT_IP` register (the clone of `of_badgeSwA`)** (module docstring). -/ +theorem JStage3A.of_faultIpWrA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) (hnA : nA = hPl.ipa) + (hc : m.curCore < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (hsw : SwA σ) (hns : σ.schedulerAction ≠ .switchThread σ.curThread) + {t : U64} (ht : t ∈ Z.T) {xt : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) (v : U64) : + (∀ u ∈ Z.T, ∀ O : U64, O.val < 4096 → O.val % 8 = 0 → O.val ≠ 528 → + L.mem (L.wr s (Endp.regA t layout.reg.FAULT_IP) v) (wa u O) = L.mem s (wa u O)) ∧ + ∃ Q Rl, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) (L.wr s (Endp.regA t layout.reg.FAULT_IP) v) a + (updK σ (specPtr t) (.tcb (setReg xt faultRegister v.bv))) m := by + subst hnA + set s' := L.wr s (Endp.regA t layout.reg.FAULT_IP) v with hs' + set x' := setReg xt faultRegister v.bv with hx' + set σ' := updK σ (specPtr t) (.tcb x') with hσ' + have hP := J.iplaced + have htH : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + have hta := J.sr.core.tcbs t ht + -- the addresses + have hbv : (Endp.regA t layout.reg.FAULT_IP).val = t.val + 528 := by + have h0 : (layout.reg.FAULT_IP).val = 34 := by unfold layout.reg.FAULT_IP; rfl + have hv := Endp.regA_val (hP.e.tcb t ht) (i := layout.reg.FAULT_IP) (by rw [h0]; decide) + rw [h0] at hv; omega + have hbal : Aligned (Endp.regA t layout.reg.FAULT_IP) := by + have e : Endp.regA t layout.reg.FAULT_IP = wa t 528#u64 := by + apply UScalar.eq_of_val_eq + have := hta.fits + rw [hbv, wa_valm, Nat.mod_eq_of_lt (by simp; omega)]; simp + rw [e]; exact field_aligned hta (by simp) (by simp) + have w1 : WritesIn st s s' (fun b => b = Endp.regA t layout.reg.FAULT_IP) := writesIn_wr hbal + have off : ∀ b : U64, Aligned b → b.val ≠ t.val + 528 → L.mem s' b = L.mem s b := fun b hb h => + w1.mem_eq hb fun c => h (by rw [c, hbv]) + have tcbW : ∀ u ∈ Z.T, ∀ O : U64, O.val < 4096 → O.val % 8 = 0 → O.val ≠ 528 → L.mem s' (wa u O) = L.mem s (wa u O) := by + intro u hu O h1 h2 h3 + have hua := J.sr.core.tcbs u hu + have hfit := hua.fits + have hv : (wa u O).val = u.val + O.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + refine off _ (field_aligned hua h1 h2) fun c => ?_ + by_cases e : u = t + · subst e; omega + · have := J.sr.place.apart u t .tcb .tcb ((J.sr.thT u).2 hu) ((J.sr.thT t).2 ht) e + have hsz : ObjKindA.size .tcb = 4096 := rfl + omega + -- the spec step + have mem : ((), σ') ∈ (asUser (Z := Unit) (specPtr t) (setRegister faultRegister v.bv) σ).1 := by + rw [asUser_setRegister_eq _ _ hkt]; rfl + have K : SIKA σ.idleThread (fun _ => False) (fun _ => False) false σ σ' := asUser_sikA _ _ σ _ mem + have Qk : QKeepA σ σ' := asUser_qA _ _ σ _ mem + have same : ∀ q, q ≠ specPtr t → σ'.kheap q = σ.kheap q := fun q e => by + rw [hσ', updK_kheap, Function.update_of_ne e] + have atp : σ'.kheap (specPtr t) = some (.tcb x') := by rw [hσ', updK_kheap, Function.update_self] + have keepK : ∀ q ob, σ.kheap q = some ob → (∀ y, ob ≠ .tcb y) → σ'.kheap q = some ob := fun q ob h ne => by + rw [same q fun e => by rw [e, hkt] at h; exact ne xt (Option.some.inj h).symm] + exact h + have backK : ∀ q ob, σ'.kheap q = some ob → (∀ y, ob ≠ .tcb y) → σ.kheap q = some ob := fun q ob h ne => by + by_cases e : q = specPtr t + · rw [e, atp] at h; exact absurd (Option.some.inj h).symm (ne _) + · rwa [same q e] at h + have tcbF : ∀ q y, σ.kheap q = some (.tcb y) → ∃ y', σ'.kheap q = some (.tcb y') ∧ + y'.tcbState = y.tcbState ∧ y'.tcbSchedContext = y.tcbSchedContext := fun q y h => by + by_cases e : q = specPtr t + · rw [e, hkt] at h; cases h + exact ⟨x', e ▸ atp, rfl, rfl⟩ + · exact ⟨y, by rw [same q e]; exact h, rfl, rfl⟩ + have tcbB : ∀ q y', σ'.kheap q = some (.tcb y') → ∃ y, σ.kheap q = some (.tcb y) ∧ + y'.tcbState = y.tcbState ∧ y'.tcbSchedContext = y.tcbSchedContext := fun q y' h => by + by_cases e : q = specPtr t + · rw [e, atp] at h; cases h + exact ⟨xt, e ▸ hkt, rfl, rfl⟩ + · exact ⟨y', by rwa [same q e] at h, rfl, rfl⟩ + have HS : SpecSchedA σ σ' := + { kh := fun q => by + by_cases e : q = specPtr t + · exact Or.inr (Or.inl ⟨xt, x', e ▸ hkt, e ▸ atp, rfl, rfl, rfl⟩) + · exact Or.inl (same q e) + irqS := rfl + irqN := rfl + asid := rfl + vcur := rfl + cdt := rfl + orig := rfl } + have SSz : SSzA σ σ' := ssz_updK_tcb σ _ _ + -- FL4 / spec relations + obtain ⟨ep, wE, hE⟩ := write_reg_rel hP.e s σ J.ep ht hkt faultRegister v + rw [show regIdxA faultRegister = layout.reg.FAULT_IP from regIdxA_faultIP] at ep wE + have hfe : Endp.regA t layout.reg.FAULT_IP = wa t 528#u64 := by + apply UScalar.eq_of_val_eq + have := hta.fits + rw [hbv, wa_valm, Nat.mod_eq_of_lt (by simp; omega)]; simp + have ntfn := set_fip_ntfnRel hP.n J.ntfn ht hkt v + rw [← hfe] at ntfn + have ops := (ipcSchedOpsA (st := st) (ar := ar) (κ := κ) hP J.ipcPlace hc hmb hmax htu).2.2.1 + obtain ⟨Q, Rl, sr⟩ := ops.stable s s' σ σ' ⟨Z.Q, Z.R, J.sr⟩ wE hE (NoBlockE.tcbK hkt x' rfl rfl) + have wN : WritesIn st s s' (SchedStepNWA (st := st) N E Rp Z m σ s) := w1.mono fun b h => by + have hfit := hta.fits + have hv : ∀ O : U64, O.val < 4096 → (wa t O).val = t.val + O.val := fun O hO => by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + refine Or.inl (Or.inr ⟨t, ht, by rw [h, hbv]; omega, by rw [h, hbv]; omega, fun c => ?_, fun c => ?_⟩) + · have := congrArg (fun w : U64 => w.val) (h.symm.trans c) + simp only [hbv, hv 3664#u64 (by simp)] at this + simp at this + · have := congrArg (fun w : U64 => w.val) (h.symm.trans c) + rw [show runVcpuA t = wa t 3672#u64 from rfl] at this + simp only [hbv, hv 3672#u64 (by simp)] at this + simp at this + have reply : ReplyStackRelA (L.mem s') Rp Z.C σ' (scNameA m) stack := by + refine J.reply.frame (fun r hr => ?_) (fun c _ y k h => ⟨y, k, keepK _ _ h (fun _ e => by cases e), rfl⟩) + (fun r _ x1 h => ⟨x1, backK _ _ h (fun _ e => by cases e), rfl⟩) + have hr3 := hP.e.rep r hr + have dis := hP.e.rt r hr t ht + have key : ∀ O : U64, O.val = 8 ∨ O.val = 16 → L.mem s' (wa r O) = L.mem s (wa r O) := by + intro O hO + have hv : (wa r O).val = r.val + O.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact off _ (by unfold Aligned; rw [hv]; omega) (by omega) + exact ⟨key _ (Or.inr (by simp [layout.reply.NEXT])), key _ (Or.inl (by simp [layout.reply.PREV]))⟩ + have sinv : SInvXA σ' m n := + K.sinvSw J.sinv J.sidle hsw hns (fun _ _ h => h) (fun _ _ h => h) id (fun _ h => h.elim) (fun _ h => h.elim) + (fun _ h => h.elim) (fun _ h => h.elim) (J.sidle.rdyA J.sinv) (fun _ h => h.elim) + have sidle : SIdleA σ' m n := K.sidle J.sidle (fun _ _ h => h) (J.sidle.rdyA J.sinv) + have rv : RunVcpuInv st Z.T s' := fun u hu h => by + have e1 : L.mem s' (runVcpuA u) = L.mem s (runVcpuA u) := tcbW u hu _ (by simp) (by simp) (by simp) + have e2 : L.mem s' (stA u) = L.mem s (stA u) := by + rw [stA_eq]; exact tcbW u hu _ (by simp) (by simp) (by simp) + rw [e2] + exact J.runVcpu u hu (e1 ▸ h) + have blkQ : BlkQueuedA Z.T E N (L.mem s') σ' := + J.blkQ.keepA (fun u hu => tcbW u hu 3336#u64 (by simp) (by simp) (by simp)) + (fun u _ y h => Or.inr (by + obtain ⟨z, hz, e, -⟩ := tcbB _ _ h + exact ⟨z, hz, e.symm⟩)) + (fun q y h => keepK q _ h (fun _ e => by cases e)) (fun q y h => keepK q _ h (fun _ e => by cases e)) + have sv : SchedValidA σ' := J.schedValid.congr rfl rfl rfl rfl rfl rfl + (fun q y k h => keepK q _ h (fun _ e => by cases e)) + (fun u y h => by obtain ⟨z, hz, -, e⟩ := tcbF u y h; exact ⟨z, hz, e⟩) + (fun u y h => tcbB u y h) + -- P1's invariants after the write + obtain ⟨Pt, Ub, Pp7, Tb7, Hr, Te, Kt, Lv7, Re, bits, KLv, W⟩ := J7A.unp J.j7 + have I := J.delIn hA hPl W + have hH := I.del.heap + have hO := I.del.os + have hc0 := J.core0 + have B := J.sys.sysl + have Y := J.symL + have h : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s := by + obtain ⟨q, b, x, sg, sc⟩ := J.sys.ent.bxi + exact ⟨q, b, x, sc, sg⟩ + have regs : RpPost st H Z.Cs OS Z.V ia ie Ac s s' := by + have i0 : XKoQ st H Z.Cs OS ia ie Ac ∅ s s ∧ XV st H OS Z.V s s := + ⟨⟨⟨XJX.start B Y h, OSKeep.rfl' _⟩, TabKeep.refl s⟩, XV.refl s⟩ + have walk : obj.Tcb.set_reg st t s layout.reg.FAULT_IP v ⦃ q => RpPost st H Z.Cs OS Z.V ia ie Ac s q ⦄ := by + have hvr1 : VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt := I.del.vr + refine WP.spec_mono (XJX.regT (Vc := Z.V) hH hO hc0 i0.1.1.1 hvr1 htH + (by simp [layout.reg.FAULT_IP, layout.reg.X0])) fun q ⟨h3, k3, t3, a3⟩ => ?_ + exact RpW.post ⟨h3.sys, h3.sym, h3.blk, XC.keep (OSKeep.trans' i0.1.1.2 k3) (i0.1.2.trans t3), + i0.2.trans (XV.keep a3 k3), (XferSlots.refl s).keep fun z hz => (OSKeep.trans' i0.1.1.2 k3).capA hz⟩ + have e : obj.Tcb.set_reg st t s layout.reg.FAULT_IP v = ok s' := by + obtain ⟨q, hq, e⟩ := spec_existsA (obj.Tcb.set_reg.spec (st := st) s t layout.reg.FAULT_IP v) + rw [hq, e] + exact post_of_okA (P := fun q => RpPost st H Z.Cs OS Z.V ia ie Ac s q) walk e + obtain ⟨bxi, sysl, symL, userOK, j7⟩ := J.finRpA hA hPl rfl regs σ' + refine ⟨tcbW, Q, Rl, ?_⟩ + exact J.of_ipcStepA (Q := Q) (Rl := Rl) (curOf := Z.curOf) (curScOf := Z.curScOf) wN HS SSz Qk rfl (fun _ _ => rfl) + bxi sysl symL userOK j7 sr ep ntfn reply K.ipcKeep + (fun u hu O h1 h2 => tcbW u hu O (by omega) h2 (by omega)) + sinv sidle rv blkQ sv + +theorem JSyscallA.faultIpA + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) (hnA : nA = hPl.ipa) + (hc : m.curCore < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {xt : Tcb} (hkt : σ.kheap (specPtr (L.curW s)) = some (.tcb xt)) (v : U64) : + ∃ Q Rl, JSyscallA st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) (L.wr s (Endp.regA (L.curW s) layout.reg.FAULT_IP) v) a + (updK σ (specPtr (L.curW s)) (.tcb (setReg xt faultRegister v.bv))) m := by + have R := J.toRecvPreA + have ct : σ.curThread = specPtr (L.curW s) := J.sr.sch.curThread + have htZ : L.curW s ∈ Z.T := J.sr.curT + obtain ⟨tw, Q, Rl, J2⟩ := J.toJStage3A.of_faultIpWrA hA hPl hnA hc hmb hmax htu R.body.sw R.noSelf htZ hkt v + refine ⟨Q, Rl, ?_⟩ + set t := L.curW s with ht + set s2 := L.wr s (Endp.regA t layout.reg.FAULT_IP) v with hs2 + set x' := setReg xt faultRegister v.bv with hx' + set σ2 := updK σ (specPtr t) (.tcb x') with hσ2 + have hcw : L.curW s2 = t := specPtr_inj (J2.sr.sch.curThread.symm.trans ct) + have hσ2k : ∀ p, p ≠ specPtr t → σ2.kheap p = σ.kheap p := fun p e => by + rw [hσ2, updK_kheap, Function.update_of_ne e] + have atp : σ2.kheap (specPtr t) = some (.tcb x') := by rw [hσ2, updK_kheap, Function.update_self] + have keepK : ∀ q ob, σ.kheap q = some ob → (∀ y, ob ≠ .tcb y) → σ2.kheap q = some ob := fun q ob h ne => by + rw [hσ2k q fun e => by rw [e, hkt] at h; exact ne xt (Option.some.inj h).symm]; exact h + have backK : ∀ q ob, σ2.kheap q = some ob → (∀ y, ob ≠ .tcb y) → σ.kheap q = some ob := fun q ob h ne => by + by_cases e : q = specPtr t + · rw [e, atp] at h; exact absurd (Option.some.inj h).symm (ne _) + · rwa [hσ2k q e] at h + have e1 : L.mem s2 (stA t) = L.mem s (stA t) := by + rw [stA_eq]; exact tw t htZ _ (by simp) (by simp) (by simp) + have e2 : L.mem s2 (scA t) = L.mem s (scA t) := by + rw [scA_eq]; exact tw t htZ _ (by simp) (by simp) (by simp) + have e3 : L.mem s2 (inqA t) = L.mem s (inqA t) := by + rw [inqA_eq]; exact tw t htZ _ (by simp) (by simp) (by simp) + have hcsc : L.cscW s2 = L.cscW s := by + apply scRenA_inj m (J2.sr.sch.scOff _ J2.sr.cscC) (J.sr.sch.scOff _ J.sr.cscC) + exact J2.sr.currentScA.symm.trans ((show σ2.curSc = σ.curSc from rfl).trans J.sr.currentScA) + have hcur : ∀ y, σ.kheap σ.curThread = some (.tcb y) → y = xt := fun y h => by + rw [ct, hkt] at h; exact (KernelObject.tcb.inj (Option.some.inj h)).symm + have ct2 : σ2.curThread = specPtr t := ct + have nt : ∀ y n, ∀ (h : σ2.kheap σ2.curSc = some (.schedContext y n)), + σ.kheap σ.curSc = some (.schedContext y n) := fun y n h => backK _ _ h (fun _ e => by cases e) + refine ⟨⟨J2, ?_, ?_, ?_, ?_, ?_, ?_, ?_⟩, ?_, ?_, ?_, ?_⟩ + · obtain ⟨⟨tx, htx, hsc', hq⟩, ⟨y, k, hy⟩, hact, hnr⟩ := J.curInv + obtain rfl := hcur tx htx + refine ⟨⟨x', ct2 ▸ atp, hsc', hq⟩, ⟨y, k, keepK _ _ hy (fun _ e => by cases e)⟩, ?_, hnr⟩ + intro hn y' k' h + exact hact hn y' k' (nt y' k' h) + · exact fun hpos y k hy hact => J.offOK hpos y k (nt y k hy) hact + · exact fun y k hy => J.offReady y k (nt y k hy) + · refine ⟨fun u h => ?_, ?_⟩ + · have := J.act.symm.trans h; cases this + · obtain ⟨ti, hti, hsci, hsti, hrq⟩ := J.actIdle.2 + by_cases e : σ.idleThread = specPtr t + · rw [e, hkt] at hti + obtain rfl := (KernelObject.tcb.inj (Option.some.inj hti)).symm + exact ⟨x', by rw [show σ2.idleThread = specPtr t from e]; exact atp, hsci, hsti, hrq⟩ + · exact ⟨ti, by rw [show σ2.idleThread = σ.idleThread from rfl, hσ2k _ e]; exact hti, hsci, hsti, hrq⟩ + · exact J.curScNotIdle + · exact J.validTime + · refine ⟨fun u h => J.sact.f1 u h, ?_⟩ + obtain ⟨x0, hx0, hs0⟩ := J.sact.f2 + obtain rfl := hcur x0 hx0 + exact ⟨x', ct2 ▸ atp, hs0⟩ + · exact J.act + · rw [hcw, e1]; exact J.running + · rw [hcw, e2, hcsc]; exact J.curSc + · rw [hcw, e3, e2]; exact J.cur + + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3SysFrontA.lean b/proofs/Fl4ProofsA64/P3/Stage3SysFrontA.lean new file mode 100644 index 000000000..34edb9db6 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SysFrontA.lean @@ -0,0 +1,309 @@ +/- +P3 (aarch64, A64-P3 sysfront): **the syscall front**, the shared prefix of `kBodyA (.syscall _)` (module docstring of +`P3/Stage3SysFaultIpA` for the `FAULT_IP` write). + +* `update_timestamp_archA`, `update_timestamp_arXferA`: the architecture result of `sched.update_timestamp` is the one of + `ar.now a`, whatever the store: the entry's `hAr` is carried across the `FAULT_IP` write. +* `KEntryFaultInA.faultIpA`: the entry record's premises (`KEntryFaultInA`) at the post-write state. + +No new named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3SysFaultIpA +import Fl4ProofsA64.P3.Stage3KEntryFaultFinA +import Fl4ProofsA64.P3.EpFaultReplyA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + +theorem clockNodeWordsA2 {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRN st Z m κ s σ) : + Aligned (L.nodeA s layout.node.CUR_TIME) ∧ Aligned (L.nodeA s layout.node.CONSUMED) ∧ + L.nodeA s layout.node.CUR_TIME ≠ L.nodeA s layout.node.CONSUMED := by + obtain ⟨nn, -, hkn, pcs⟩ := H.csN + have hCpu : (L.cpu s).val < 128 := by have := (pcs _).1 H.sch.cpuCs; omega + have e40 : layout.node.CONSUMED.val = 40 := by unfold layout.node.CONSUMED; rfl + have e48 : layout.node.CUR_TIME.val = 48 := by unfold layout.node.CUR_TIME; rfl + have c := nodeAt_valSc H.ksFit hCpu (by rw [e40]; decide : layout.node.CONSUMED.val < 4288) + have t := nodeAt_valSc H.ksFit hCpu (by rw [e48]; decide : layout.node.CUR_TIME.val < 4288) + change Aligned (L.nodeAt s (L.cpu s) layout.node.CUR_TIME) ∧ + Aligned (L.nodeAt s (L.cpu s) layout.node.CONSUMED) ∧ + L.nodeAt s (L.cpu s) layout.node.CUR_TIME ≠ L.nodeAt s (L.cpu s) layout.node.CONSUMED + have ha := H.ksb_al + unfold Aligned at ha ⊢ + rw [c, t, e40, e48] + refine ⟨by omega, by omega, ?_⟩ + intro e + have ev := congrArg (fun k : U64 => k.val) e + rw [c, t, e40, e48] at ev + omega + + +set_option maxHeartbeats 2000000 in +/-- The architecture result of `update_timestamp` is `ar.now`'s, whatever the store. -/ +theorem update_timestamp_archA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRN st Z m κ s σ) (a : A) + {Clock : A → MachineState → Prop} (P : NowAgreeA ar Clock) (hClock : Clock a σ.machineState) : + ∃ now a1, ar.now a = ok (now, a1) ∧ sched.update_timestamp st ar s a ⦃ r => r.2 = a1 ⦄ := by + obtain ⟨⟨now, a1⟩, e, hn, hcl⟩ := spec_existsA (P a σ.machineState hClock) + refine ⟨now, a1, e, ?_⟩ + obtain ⟨alT, alC, ne⟩ := clockNodeWordsA2 H + have ep : sched.cur_time st s = ok (L.curTime s) := by + unfold sched.cur_time LawfulStore.curTime LawfulStore.nodeA + rw [L.cur_cpu_eq, bind_ok, eq_of_spec_fd (obj.node_get.spec s _ _)] + unfold sched.update_timestamp + rw [ep, bind_ok, e, bind_ok] + simp only [uncurry_apply_pair] + rw [L.cur_cpu_eq, bind_ok, eq_of_spec_fd (obj.node_set.spec s _ _ _)] + simp only [bind_ok, lift] + let s1 := L.wr s (L.nodeA s layout.node.CUR_TIME) now + have addr : L.nodeA s1 layout.node.CONSUMED = L.nodeA s layout.node.CONSUMED := by + unfold LawfulStore.nodeA s1 + rw [L.cpu_wr, nodeAt_of_ksb (L.ksb_wr _ _ _)] + have c : sched.consumed st s1 = ok (L.mem s (L.nodeA s layout.node.CONSUMED)) := by + rw [eq_of_spec_fd (sched.consumed.specA s1), addr] + rw [L.mem_wr_ne alT alC (fun h => ne (UScalar.eq_of_val_eq h))] + change sched.consumed st (L.wr s (L.nodeAt s (L.cpu s) layout.node.CUR_TIME) now) = _ at c + rw [c, bind_ok] + change (sched.set_consumed st s1 _ >>= _) ⦃ _ ⦄ + refine WP.spec_bind (sched.set_consumed.spec s1 _) ?_ + rintro s2 rfl + simp only [layout.NUM_DOMAINS, fl4_abi.config.NUM_DOMAINS, + show ¬ ((1#u64 : U64) > 1#u64) from by decide, ↓reduceIte, bind_ok, WP.spec_ok] + try rfl + +set_option maxHeartbeats 4000000 in +/-- `update_timestamp`'s architecture postcondition at one store is its postcondition at any other. -/ +theorem update_timestamp_arXferA {Z Z' : XSetsA} {m : McState} {κ κ' : U64 → U64} {s s' : S} {σ σ' : State Unit} + (H : SchedSRN st Z m κ s σ) (H' : SchedSRN st Z' m κ' s' σ') (a : A) + {Clock : A → MachineState → Prop} (P : NowAgreeA ar Clock) (hClock : Clock a σ.machineState) + (hClock' : Clock a σ'.machineState) {Q : A → Prop} + (hAr : sched.update_timestamp st ar s a ⦃ r => Q r.2 ⦄) : + sched.update_timestamp st ar s' a ⦃ r => Q r.2 ⦄ := by + obtain ⟨now, a1, e, hr⟩ := update_timestamp_archA H a P hClock + obtain ⟨now', a1', e', hr'⟩ := update_timestamp_archA H' a P hClock' + obtain ⟨r, er, hq⟩ := spec_existsA hAr + have h1 : r.2 = a1 := post_of_okA (P := fun q => q.2 = a1) hr er + have h2 : (now', a1') = (now, a1) := + post_of_okA (P := fun q => q = (now, a1)) (by rw [e]; exact (WP.spec_ok _).2 rfl) e' + have h3 : a1' = a1 := congrArg Prod.snd h2 + exact WP.spec_mono hr' fun r' h => by rw [h, h3, ← h1]; exact hq + +/-! ## The spec's entry body, at the budget -/ + +theorem liftedSingletonSysA {α : Type} {m : SMonad Unit α} {σ τ : State Unit} {v : α} + (h : m σ = ({(v, τ)}, False)) : + (NonDetE.liftE (ε := Unit) m).run σ = ({(.ok v, τ)}, False) := by + unfold NonDetE.liftE + rw [ExceptT.run_lift] + change (m >>= fun v => NonDet.pure (Except.ok v : Except Unit α)) σ = _ + rw [nd_bind_oneSc h] + rfl + +/-- `fl4HandleEvent` of a system call: the budget check, then the dispatcher, under one preemption handler. -/ +theorem settleBudgetSysA (rep : Bool) (call : Syscall) (Hs : SMonad Unit Unit) {σ τ : State Unit} {b : Bool} + (h : fl4BudgetOk (Z := Unit) σ = ({(b, τ)}, False)) : + NonDetE.catch (fl4HandleEvent (Z := Unit) rep (.syscallEvent call)) (fun _ => Hs) σ = + (if b then NonDetE.catch (fl4HandleSyscall (Z := Unit) rep call) (fun _ => Hs) τ else ({((), τ)}, False)) := by + have runs : (fl4HandleEvent (Z := Unit) rep (.syscallEvent call)).run σ = + (NonDetE.whenE (b = true) (fl4HandleSyscall (Z := Unit) rep call)).run τ := by + unfold fl4HandleEvent + rw [ExceptT.run_bind] + exact nd_bind_oneSc (liftedSingletonSysA h) _ + calc + _ = NonDetE.catch (NonDetE.whenE (b = true) (fl4HandleSyscall (Z := Unit) rep call)) (fun _ => Hs) τ := by + simp only [NonDetE.catch, NonDet.bind, runs] + _ = _ := by + cases b + · unfold NonDetE.catch + change (NonDet.pure (Except.ok () : Except Unit Unit) >>= _) τ = _ + have one : NonDet.pure (Except.ok () : Except Unit Unit) τ = ({(.ok (), τ)}, False) := rfl + rw [nd_bind_oneSc one] + rfl + · rfl + +/-- `kOpA`'s `handleE` run, result ignored. -/ +def kopRunA (B : PMonad Unit Unit) (H : SMonad Unit Unit) : SMonad Unit Unit := do + let _ ← ExceptT.run (NonDetE.handleE B (fun _ => withoutPreemption H)) + pure () + +/-- The handler of `kOpA`'s `handleE` is `NonDetE.catch`'s. -/ +theorem catch_to_handleE_memA (B : PMonad Unit Unit) (H : SMonad Unit Unit) {τ1 τ2 : State Unit} + (h : ((), τ2) ∈ (NonDetE.catch B (fun _ => H) τ1).1) : + ((), τ2) ∈ (kopRunA B H τ1).1 := by + unfold kopRunA + unfold NonDetE.catch at h + obtain ⟨⟨e, τm⟩, hm, hr⟩ := mem_bindA.1 h + refine mem_bindA.2 ⟨(Except.ok (), τ2), ?_, rfl⟩ + unfold NonDetE.handleE + refine mem_bindA.2 ⟨(e, τm), hm, ?_⟩ + cases e with + | ok a => + have : τ2 = τm := (Prod.mk.inj (Set.mem_singleton_iff.1 hr)).2 + subst this + cases a + exact Set.mem_singleton _ + | error ex => + exact liftE_memA hr + +/-- The spec entry body, from the `FAULT_IP` write and the budget answer. -/ +theorem kopMemSysA (number : I64) (rep : Bool) (call : Syscall) {σ τPC τ τ2 : State Unit} {b : Bool} + (pc : fl4SyscallFaultIP (Z := Unit) σ.curThread σ = ({((), τPC)}, False)) + (hb : fl4BudgetOk (Z := Unit) τPC = ({(b, τ)}, False)) + (hm : if b then ((), τ2) ∈ (NonDetE.catch (fl4HandleSyscall (Z := Unit) rep call) + (fun _ => fl4PreemptionPathIrq) τ).1 else τ2 = τ) : + ((), τ2) ∈ (kOpA (.syscall number rep call) σ).1 := by + simp only [kOpA] + rw [nd_gets_bindSc, nd_bind_oneSc pc] + refine catch_to_handleE_memA (fl4HandleEvent (Z := Unit) rep (.syscallEvent call)) fl4PreemptionPathIrq ?_ + rw [settleBudgetSysA rep call fl4PreemptionPathIrq hb] + cases b + · have : τ2 = τ := hm + subst this + exact Set.mem_singleton _ + · exact hm + +section +variable {AR AR' : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {s : S} {a : A} {σ : State Unit} {m : McState} {Clock : A → MachineState → Prop} + +set_option maxHeartbeats 2000000 in +/-- **The entry record's premises after the `FAULT_IP` write** (module docstring). -/ +theorem KEntryFaultInA.faultIpA (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (I : KEntryFaultInA st ar AR AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ hPl.ipa m Clock s a σ) + (hc : m.curCore < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {xt : Tcb} (hkt : σ.kheap (specPtr (L.curW s)) = some (.tcb xt)) (v : U64) : + ∃ Q Rl, KEntryFaultInA st ar AR AR' { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ hPl.ipa m + Clock (L.wr s (Endp.regA (L.curW s) layout.reg.FAULT_IP) v) a + (updK σ (specPtr (L.curW s)) (.tcb (setReg xt faultRegister v.bv))) := by + obtain ⟨Q, Rl, J2⟩ := I.J.faultIpA hA hPl rfl hc hmb hmax htu hkt v + refine ⟨Q, Rl, J2, ?_, I.P, I.hClock, I.hnd, I.C, I.hC, I.hNow, ?_, I.hmb⟩ + · intro b hb + have hk : L.ksb (L.wr s (Endp.regA (L.curW s) layout.reg.FAULT_IP) v) = L.ksb s := L.ksb_wr _ _ _ + rw [hk] at hb + obtain ⟨h1, h2⟩ := I.hvp b hb + exact ⟨h1, fun h => h2 (PPW.of hk (L.cpu_wr _ _ _) h)⟩ + · exact update_timestamp_arXferA I.J.sr.toSchedSRN J2.sr.toSchedSRN a I.P I.hClock I.hClock (Q := fun q => AR' q Z.hw) I.hAr + +end + +/-- The spec's `FAULT_IP` write of the current thread (`fl4SyscallFaultIP`): the next-instruction register less 4. -/ +theorem faultIP_eqA {σ : State Unit} {t : ObjRef} {x : Tcb} (hk : σ.kheap t = some (.tcb x)) : + fl4SyscallFaultIP (Z := Unit) t σ = + ({((), updK σ t (.tcb (setReg x faultRegister (x.tcbArch.tcbContext.userRegs nextInstructionRegister - 4))))}, + False) := by + unfold fl4SyscallFaultIP + have hf : (NonDet.bind (getRegister nextInstructionRegister) (fun n => setRegister faultRegister (n - 4)) : + UserMonad Unit) (archTcbContextGet x.tcbArch) = + ({((), ⟨(archTcbContextGet x.tcbArch).userFpuState, Function.update (archTcbContextGet x.tcbArch).userRegs + faultRegister (x.tcbArch.tcbContext.userRegs nextInstructionRegister - 4)⟩)}, False) := by + simp only [NonDet.bind, getRegister, setRegister, NonDet.gets, NonDet.modify] + ext ⟨a', b'⟩ <;> simp [archTcbContextGet] + rw [asUser_writeA hk hf] + rfl + +/-- [J] **The fault entries' `hCharge` after the `FAULT_IP` write** (the exhausted-budget branch of +`check_budget_restart`): at the concrete post-write FL4 state and the spec state `τPC` that `fl4SyscallFaultIP` fixes, +guarded by the syscall-entry record there (the ghost lists `Q`, `R` the write produces float). -/ +def SysChargeA (st : machine.Store S) [L : LawfulStore st] (ar : machine.Arch A) (AR AR' : A → HwRootA → Prop) + (Z : XSetsA) (H : Heap) (OS U Ac Pp Tb : Set U64) (Lv : U64 → Nat) (ia ie : Nat) (N E Rp : Set U64) + (Bf : Set (U64 × Fl4SpecA64.Word)) (stack : U64 → List U64) (κ : U64 → U64) (nA : U64) (m : McState) + (Clock : A → MachineState → Prop) (s : S) (a : A) (σ : State Unit) : Prop := + ∀ (Q : U64 → U64 → List U64) (Rl : U64 → List U64) (τPC : State Unit), + fl4SyscallFaultIP (Z := Unit) σ.curThread σ = ({((), τPC)}, False) → + JSyscallA st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes + (J5A st) (J7A st (J7R2A st ar nA)) + (L.wr s (Endp.regA (L.curW s) layout.reg.FAULT_IP) + (core.num.U64.wrapping_sub (L.mem s (regWA (L.curW s) layout.reg.NEXT_IP)) 4#u64)) a τPC m → + KEntryChargeA st ar AR' { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ nA m Clock + (L.wr s (Endp.regA (L.curW s) layout.reg.FAULT_IP) + (core.num.U64.wrapping_sub (L.mem s (regWA (L.curW s) layout.reg.NEXT_IP)) 4#u64)) a τPC + (sched.check_budget_restart st ar) fl4CheckBudgetRestart + +section front +variable {AR AR' : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {s : S} {a : A} {σ : State Unit} {m : McState} {Clock : A → MachineState → Prop} + +set_option maxHeartbeats 4000000 in +/-- **The syscall front** (module docstring): FL4's `kBodyA (.syscall …)` refines the spec's `kOpA`, parametric in the +dispatch body (`dispatch`, a hole at the post-budget record) and the final relation `Fin`. [J] `hCharge` is the fault +entries' (the exhausted-budget branch), at the post-`FAULT_IP` ghost sets. -/ +theorem syscallFront_refinesA {Fin : S → A → State Unit → Prop} + (hPl : ArchPlat ar) (Env : FaultEnvA st ar Z) (number : I64) (rep : Bool) (call : Syscall) + (hnum : number = syscallNumberA call) + (I : KEntryFaultInA st ar AR AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ hPl.ipa m Clock s a σ) + (hCharge : SysChargeA st ar AR AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ hPl.ipa m Clock s a σ) + (dispatch : ∀ (Q : U64 → U64 → List U64) (Rl : U64 → List U64) sb ab τ (τPC : State Unit), + fl4SyscallFaultIP (Z := Unit) σ.curThread σ = ({((), τPC)}, False) → + syscall.budget_ok st ar (L.wr s (Endp.regA (L.curW s) layout.reg.FAULT_IP) + (core.num.U64.wrapping_sub (L.mem s (regWA (L.curW s) layout.reg.NEXT_IP)) 4#u64)) a = ok (true, sb, ab) → + fl4BudgetOk (Z := Unit) τPC = ({(true, τ)}, False) → + JSyscallA st AR' { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes + (J5A st) (J7A st (J7R2A st ar hPl.ipa)) sb ab τ m → CounterStepA τ → + syscall.dispatch_syscall st ar sb ab (syscallNumberA call) ⦃ r => + StepPostA Fin (NonDetE.catch (fl4HandleSyscall (Z := Unit) rep call) (fun _ => fl4PreemptionPathIrq)) τ r ⦄) + (hfalse : ∀ s' a' τ, A64RelS3 st AR' ar hPl.ipa fl4A64Platform.numNodes Z.Cs s' a' τ m → Fin s' a' τ) : + kBodyA st ar s a (.syscall number rep call) ⦃ r => + StepPostA Fin (kOpA (.syscall number rep call)) σ r ⦄ := by + subst hnum + have J0 := I.J + have ct : σ.curThread = specPtr (L.curW s) := J0.sr.sch.curThread + have htZ : L.curW s ∈ Z.T := J0.sr.curT + obtain ⟨xt, hkt, -⟩ := J0.ep.tcb _ htZ + have hk' : σ.kheap σ.curThread = some (.tcb xt) := by rw [ct]; exact hkt + have hc : m.curCore < fl4A64Platform.numNodes := by + rw [J0.j.1.sch.core]; exact Env.hCs _ J0.j.1.sch.cpuCs + set v : U64 := core.num.U64.wrapping_sub (L.mem s (regWA (L.curW s) layout.reg.NEXT_IP)) 4#u64 with hv + have nreg : v.bv = xt.tcbArch.tcbContext.userRegs nextInstructionRegister - 4 := by + have h1 : (L.mem s (regWA (L.curW s) layout.reg.NEXT_IP)).bv = + xt.tcbArch.tcbContext.userRegs nextInstructionRegister := by + rw [← (J0.ep.at_tcb htZ hkt).regs nextInstructionRegister, + show regIdxA nextInstructionRegister = layout.reg.NEXT_IP from regIdxA_nextIP] + rfl + rw [hv, core.num.U64.wrapping_sub_bv_eq, h1] + simp + obtain ⟨Q1, R1, I2⟩ := KEntryFaultInA.faultIpA Env.hA hPl I hc Env.hmb Env.hmax Env.htu hkt v + have pc : fl4SyscallFaultIP (Z := Unit) σ.curThread σ = + ({((), updK σ (specPtr (L.curW s)) (.tcb (setReg xt faultRegister v.bv)))}, False) := by + rw [faultIP_eqA hk', ct, nreg] + simp only [kBodyA] + rw [eq_of_spec_fd (sched.cur_thread.spec (L := L) s), bind_ok, tcb_reg_eqA, bind_ok] + simp only [lift, bind_ok] + rw [show L.mem s (L.nodeAt s (L.cpu s) layout.node.CUR_THREAD) = L.curW s from rfl] + rw [← hv] + have hset : obj.Tcb.set_reg st (L.curW s) s layout.reg.FAULT_IP v = + ok (L.wr s (Endp.regA (L.curW s) layout.reg.FAULT_IP) v) := by + obtain ⟨q, hq, e⟩ := spec_existsA (obj.Tcb.set_reg.spec (st := st) s (L.curW s) layout.reg.FAULT_IP v) + rw [hq, e] + rw [hset, bind_ok] + have budget := JSyscallA.budgetOkSysA I2.J I2.hvp I2.P I2.hClock I2.hnd I2.C I2.hC I2.hNow I2.hAr I2.hmb + (fun s1 a1 σ1 e J1 En V CI R Act Cl suf => hCharge Q1 R1 _ pc I2.J s1 a1 σ1 e J1 En V CI R Act Cl suf) + obtain ⟨⟨b, sb, ab⟩, eb, τ, e1, Jq, tr⟩ := spec_existsA budget + rw [eb, bind_ok] + simp only [uncurry_apply_pair] + cases b + · rw [if_neg (by simp)] + obtain ⟨Q', R', cO, cS, J3⟩ := Jq + have bo := I2.J.bodyOKA_budgetFalse I2.hvp I2.P I2.hClock I2.hnd I2.C I2.hNow I2.hAr I2.hmb e1 + exact (WP.spec_ok _).2 ⟨τ, kopMemSysA _ rep call pc e1 (by simp), hfalse _ _ _ (A64RelS3.ofJ J3 bo)⟩ + · rw [if_pos rfl] + have Jt := tr rfl + refine WP.spec_mono (dispatch Q1 R1 sb ab τ _ pc eb e1 Jt.1 Jt.2) ?_ + rintro r ⟨τ2, mem2, hF⟩ + exact ⟨τ2, kopMemSysA _ rep call pc e1 (by simpa using mem2), hF⟩ + +end front + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3SysPlaceA.lean b/proofs/Fl4ProofsA64/P3/Stage3SysPlaceA.lean new file mode 100644 index 000000000..fb985c002 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SysPlaceA.lean @@ -0,0 +1,341 @@ +/- +P3 (aarch64, A64-P3 Stage-3 T3 item G3, part 2): **the `JStage3A → SysPlace` adapter and the decode-site cap +typing** (G3 freeze table, D rows `SlotTy`, `slotw`, `WinOK`, `WuApart`, `CapOK`, `CanBlock`). + +* Typed slots: `slotIdx_addrA` (FL4's `p + 32 i` is the spec-named slot address), `isSlot_tcbA`, `isSlot_cnA`; + `JStage3A.capWfA` (a typed slot's cap is `CapWfA`, from `StateRelA` through `IsSlotA.absA` / `getAbsA`). +* **`SlotTy`** (`JStage3A.slotTy`), per `OS` slot: the cap's `DelCapOK` / `CapIn` (`CapKindA` for `CapTyped`; + `CapWfA` for the IRQ bound and `IoPort`; `CapValidA` + J5's `IsSlotA → OS` + `OSOK.slot` for CNode caps' slots and + thread caps; F7 `zwf` (`ZOKA`) for both zombie kinds; a slot that is no typed slot is null by J5), the `PREV` link + (`prevOK_ofLkA`, `MdbLk` + `NullLk` of `SysL`) and the `NEXT` link (`nextOK_ofLkA`). +* **`SysPlace`** (`JStage3A.sysPlace`): from the record and the decode site's lookup results (`ext`, `src`, and `SlotCov` of the invoked slot for + `slotw`: a TCB slot is no `CapSlotW` slot, so `slotw` is a decode-site fact, not a record one). +* **`CapOK`** of every `OS` cap at the record's state (`JStage3A.capOK`): `CapTyped` (`CapKindA`), thread caps + (`CapValidA`), SC caps (`CapKindA`; non-null by `SysL.sc`; not bound to the idle thread by `TSc` + `SysL.ci`). +* **`CanBlock`** of the current thread and `DecPre` (`JStage3EntryA.canBlock`, `JStage3EntryA.decPre`) from the + record and the syscall-entry facts of G4 (`JSyscallA`'s `act` as `L.actionW s = 0`, `running`, `curSc`, `cur`, + taken as hypotheses until `JSyscallA` lands on this branch). `CanBlock.nq` is PTU's `precut_nq`. + `DecPre.extra` is a hypothesis here; with record edit #6's narrowed `NodeW` (no `IDLE_THREAD`) it is discharged by + `P3/Stage3DecExtraA` (`JStage3A.decExtra`, `JStage3EntryA.decPreA`). + +Named premises: none new (the G4 facts and `DecPre.extra` are hypotheses of the assembly lemma, listed above). New +module; no existing statement changed. +-/ +import Fl4ProofsA64.P3.Stage3DelInA +import Fl4ProofsA64.P3.Stage3RestartA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-! ## Typed slots -/ + +/-- FL4's slot `p + 32 i` is the spec-named slot address. -/ +theorem slotIdx_addrA (p i : U64) : slotIdx p i = slotAddrA (specPtr p) (BitVec.ofNat 64 i.val) := by + rw [slotAddrA_sp] + apply UScalar.eq_of_val_eq + have h1 : (slotIdx p i).val = (p.val + i.val * 32 % 2 ^ 64) % 2 ^ 64 := by + unfold slotIdx + rw [wa_val] + simp [core.num.U64.wrapping_mul_val_eq, U64.size_def, U64.numBits] + rw [h1] + show _ = (p.bv + BitVec.ofNat 64 i.val * 32#64).toNat + simp only [BitVec.toNat_add, BitVec.toNat_mul, BitVec.toNat_ofNat, Nat.reducePow, Nat.reduceMod] + have e1 : p.bv.toNat = p.val := rfl + have e2 : i.val % 18446744073709551616 = i.val := Nat.mod_eq_of_lt i.bv.isLt + rw [e1, e2] + +/-- A TCB's slot `i < 7` is a typed slot. -/ +theorem isSlot_tcbA {th : TypedHeapA} {t i : U64} (h : th t = some .tcb) (hi : i.val < 7) : + IsSlotA th (slotIdx t i) := + ⟨t, Or.inr ⟨h, i.val, by unfold tcbSlotsA; omega, slotIdx_addrA t i⟩⟩ + +/-- A CNode's slot `i < 2^r` is a typed slot. -/ +theorem isSlot_cnA {th : TypedHeapA} {p i : U64} {r : Nat} (h : th p = some (.cnode r)) (hi : i.val < 2 ^ r) : + IsSlotA th (slotIdx p i) := + ⟨p, Or.inl ⟨r, h, i.val, hi, slotIdx_addrA p i⟩⟩ + +theorem slotIdx_zeroA (p : U64) : slotIdx p 0#u64 = p := by + apply UScalar.eq_of_val_eq + unfold slotIdx + simp [core.num.U64.wrapping_add_val_eq, core.num.U64.wrapping_mul_val_eq, U64.size_def, U64.numBits] + all_goals exact Nat.mod_eq_of_lt p.bv.isLt + +/-! ## The MDB links -/ + +/-- **`LinkOK` of every `OS` slot's `PREV` word** (PTU's `precut_prev`). -/ +theorem prevOK_ofLkA {G : List (List U64)} {t : S} {O : Set U64} + (lk : MdbLk st O G t) (nl : NullLk st O t) {x : U64} (hx : x ∈ O) : + LinkOK O (L.mem t (wa x 16#u64)) := by + by_cases h : capA st t x = .Null + · exact Or.inl (nl x hx h).1 + have hm : x ∈ G.flatten := (lk.members x).2 ⟨hx, h⟩ + obtain ⟨ch, hch, hxc⟩ := List.mem_flatten.1 hm + obtain ⟨i, hi, e⟩ := List.getElem_of_mem hxc + cases i with + | zero => + exact Or.inl (lk.head ch hch x (by rw [List.head?_eq_getElem?, List.getElem?_eq_getElem hi, e])) + | succ j => + have hj : j < ch.length := by omega + have c := lk.chain ch hch j ch[j] x (List.getElem?_eq_getElem hj) (by rw [List.getElem?_eq_getElem hi, e]) + refine Or.inr ?_ + rw [c.2] + exact ((lk.members _).1 (List.mem_flatten.2 ⟨ch, hch, List.getElem_mem hj⟩)).1 + +/-- **`NextOK` of every `OS` slot's `NEXT` word.** -/ +theorem nextOK_ofLkA {G : List (List U64)} {t : S} {O : Set U64} + (lk : MdbLk st O G t) (nl : NullLk st O t) {x : U64} (hx : x ∈ O) : + NextOK O (L.mem t (wa x 24#u64)) := by + intro n hn + have hN : NxA st t x = n := okinj ((sext48_eq _).symm.trans hn) + subst hN + by_cases h : capA st t x = .Null + · exact Or.inl (nl x hx h).2 + have hm : x ∈ G.flatten := (lk.members x).2 ⟨hx, h⟩ + obtain ⟨ch, hch, hxc⟩ := List.mem_flatten.1 hm + obtain ⟨i, hi, e⟩ := List.getElem_of_mem hxc + by_cases hl : i + 1 < ch.length + · have c := lk.chain ch hch i x ch[i + 1] (by rw [List.getElem?_eq_getElem hi, e]) (List.getElem?_eq_getElem hl) + refine Or.inr ?_ + rw [c.1] + exact ((lk.members _).1 (List.mem_flatten.2 ⟨ch, hch, List.getElem_mem hl⟩)).1 + · refine Or.inl (lk.last ch hch x ?_) + rw [List.getLast?_eq_getElem?] + have e2 : ch.length - 1 = i := by omega + rw [e2, List.getElem?_eq_getElem hi, e] + +end + +/-! ## The rows from the record -/ + +section rows +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- A typed slot's cap is well formed (`CapWfA`, from the CSpace relation). -/ +theorem JStage3A.capWfA (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {y : U64} (hy : IsSlotA Z.th y) : CapWfA (capA st s y) := by + obtain ⟨sl, hs, e⟩ := hy.absA J.j.1.place + obtain ⟨c, -, hr⟩ := J.j.1.st.getAbsA hs + rw [e, capAtA_eq] at hr + exact hr.1 + +/-- A typed slot's cap is valid (`CapValidA`: CNode / thread caps name typed objects). -/ +theorem JStage3A.capValidA (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {y : U64} (hy : IsSlotA Z.th y) : CapValidA Z.th (capA st s y) := by + have h := J.j.1.cv y hy + rwa [capAtA_eq] at h + +/-- A typed object's slot is an `OS` slot with `SlotW` placement. -/ +theorem JStage3A.slotW_of (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + {y : U64} (hy : IsSlotA Z.th y) : y ∈ OS ∧ W7.SlotW H.tcbs H.omega H.frame y := + ⟨J.j5.2.1 y hy, J.sys.ent.os.slot y (J.j5.2.1 y hy)⟩ + +/-- A TCB of the record is a typed TCB. -/ +theorem JStage3A.thTcb (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {t : U64} (ht : t ∈ H.tcbs) : Z.th t = some .tcb := by + rw [J.tcbs] at ht + exact (J.j.1.thT t).2 ht + +set_option maxHeartbeats 1000000 in +/-- **A slot cap's typing** (`CapTy`, module docstring). -/ +theorem JStage3A.slotCapTy (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + {x : U64} (hx : x ∈ OS) : CapTy H.tcbs H.omega H.frame H H.replies H.ntfns OS (capA st s x) := by + by_cases hy : IsSlotA Z.th x + swap + · rw [J.j5.2.2 x hx hy] + exact ⟨Fl4ProofsA64.P1.W7.R.A5.I.DelCapOK.null, + CapIn.ctrl (fun _ _ _ _ h => by cases h) (fun _ _ _ h => by cases h)⟩ + have wf := J.capWfA hy + have cv := J.capValidA hy + have kd := J.capTy x hx + have zw := J.zwf x hy + obtain ⟨c, hc⟩ : ∃ c, capA st s x = c := ⟨_, rfl⟩ + rw [hc] at wf cv kd zw ⊢ + have slotsCn : ∀ p r g b, c = .CNode p r g b → ∀ i : U64, i.val < 2 ^ (r.val % 64) → + slotIdx p i ∈ OS ∧ W7.SlotW H.tcbs H.omega H.frame (slotIdx p i) := by + intro p r g b e i hi + subst e + have h64 : r.val < 64 := wf.1 + rw [Nat.mod_eq_of_lt h64] at hi + exact J.slotW_of (isSlot_cnA cv.1 hi) + have slotsZ : ∀ p t k, c = .Zombie p t k → ∀ i : U64, i.val < k.val → + slotIdx p i ∈ OS ∧ W7.SlotW H.tcbs H.omega H.frame (slotIdx p i) := by + intro p t k e i hi + rcases zw p t k e with ⟨-, ht, hk⟩ | ⟨-, hcn, hk⟩ + · exact J.slotW_of (isSlot_tcbA ht (by unfold tcbSlotsA at hk; omega)) + · exact J.slotW_of (isSlot_cnA hcn (by omega)) + have baseZ : ∀ p t k, c = .Zombie p t k → W7.SlotW H.tcbs H.omega H.frame p := by + intro p t k e + rcases zw p t k e with ⟨-, ht, -⟩ | ⟨-, hcn, -⟩ + · have := (J.slotW_of (isSlot_tcbA (i := 0#u64) ht (by simp))).2 + rwa [slotIdx_zeroA] at this + · have := (J.slotW_of (isSlot_cnA (i := 0#u64) hcn (by simp))).2 + rwa [slotIdx_zeroA] at this + refine ⟨⟨⟨kd.1, fun p b x y e => ⟨kd.2.1 p b x y e, kd.2.1 p b x y e⟩, fun p g e => kd.2.2.1 p g e⟩, + fun i e => ?_, fun f l o e => ?_, fun p r g b e => ⟨?_, fun i hi => (slotsCn p r g b e i hi).2⟩, + fun p t k e => ⟨baseZ p t k e, fun i hi => (slotsZ p t k e i hi).2⟩, fun p e => ?_⟩, + fun p r g b e i hi => (slotsCn p r g b e i hi).1, fun p t k e i hi => (slotsZ p t k e i hi).1⟩ + · subst e + have : i.val < Fl4SpecA64.fl4IrqCount := wf + unfold Fl4SpecA64.fl4IrqCount at this + exact this + · subst e + exact wf.elim + · subst e + have := (J.slotW_of (isSlot_cnA (i := 0#u64) cv.1 (by simp))).2 + rwa [slotIdx_zeroA] at this + · subst e + have h := (J.j.1.thT p).1 cv + rw [← J.tcbs] at h + exact h + +/-- **`SlotTy`** (module docstring). -/ +theorem JStage3A.slotTy (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) : + SlotTy st H.tcbs H.omega H.frame H H.replies H.ntfns OS s := by + intro x hx + obtain ⟨G, par, lk, -, -, nl, -⟩ := J.sys.sysl.sys.stz.1.1 + refine ⟨fun c hc => ?_, prevOK_ofLkA lk nl hx, nextOK_ofLkA lk nl hx⟩ + have e : capA st s x = c := okinj ((capA_dec s x).symm.trans hc) + rw [← e] + exact J.slotCapTy hx + +/-- Core 0 is a core of the record. -/ +theorem JStage3A.core0 (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) : + 0#u64 ∈ Z.Cs := by + obtain ⟨k, hk, -, hc⟩ := J.j.1.csN + exact (hc 0#u64).2 (by simpa using hk) + +/-- **`SysPlace`** at the decode site: the record and the lookup results (module docstring). -/ +theorem JStage3A.sysPlace (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + {extra : Aeneas.Std.Array obj.Slot 3#usize} {slot : U64} + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hsrc : slot ∈ OS) (hcov : SlotCov st OS s slot) : + SysPlace st H Z.Cs H.frame H.replies H.ntfns OS s extra slot := + ⟨J.sys.ent.heap, J.sys.ent.os, J.winOK, J.slotTy, J.core0, hext, hsrc, + SlotCov.slotW J.sys.ent.heap J.sys.ent.os J.sys.sysl hsrc hcov, J.wuApart⟩ + +/-- A TCB of the heap is not at address `0`. -/ +theorem JStage3A.tcb_ne0 (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {t : U64} (ht : t ∈ H.tcbs) : t ≠ 0#u64 := + ne0_of_heap J.sys.sysl.ly.h0 ht + +/-- **`CapOK` of every `OS` cap at the record's state** (module docstring). -/ +theorem JStage3A.capOK (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + {x : U64} (hx : x ∈ OS) : CapOK st H.tcbs H.replies H.scs H.ntfns H s (capA st s x) := by + have T := J.slotCapTy hx + have kd := J.capTy x hx + refine ⟨T.1.1, T.1.2.2.2.2.2, fun p b e => ?_⟩ + have hsc : p ∈ H.scs := ⟨b.val, kd.2.2.2.1 p b e⟩ + have h0 : p ≠ 0#u64 := ne0_of_heap J.sys.sysl.ly.h0 (kd.2.2.2.1 p b e) + refine ⟨hsc, h0, fun hi => ?_⟩ + have hB := J.sys.ent.bxi + have hI : L.idleW s ∈ H.tcbs := hB.1.choose_spec.choose_spec.idleT + have hT := hB.2.2.1.2.2.tsc p hsc h0 (by rw [hi]; exact J.tcb_ne0 hI) + rw [hi] at hT + have ci := J.sys.sysl.ci x hx + rw [hT] at ci + exact ci.2.1 b e + +/-- The current thread is not the idle thread when it is runnable (the idle thread's state is `idleThreadState`). -/ +theorem JStage3A.cur_ne_idle (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (hr : Runnable (L.mem s (stA (L.curW s)))) : L.idleW s ≠ L.curW s := by + intro e + have hI : L.idleW s ∈ Z.T := by + rw [← J.tcbs]; exact J.sys.ent.bxi.1.choose_spec.choose_spec.idleT + obtain ⟨x, hk, hst, -⟩ := J.j.1.sch.tcb _ hI + obtain ⟨y, hy, hys⟩ := J.sinv.idleTcb + rw [J.j.1.sch.idle, hk] at hy + have hxy : x = y := by + have h1 := Option.some.inj hy + injection h1 + subst hxy + rw [e, hys] at hst + have c1 : (sched.TS_RUNNING : U64).val = 1 := by unfold sched.TS_RUNNING; rfl + have c2 : (sched.TS_RESTART : U64).val = 2 := by unfold sched.TS_RESTART; rfl + have c8 : (sched.TS_RUNNING_VM : U64).val = 8 := by unfold sched.TS_RUNNING_VM; rfl + rcases hr with h | h | h <;> rw [h] at hst <;> simp only [c1, c2, c8, tsCodeA] at hst <;> exact absurd hst (by decide) + +/-- The bound SC of the current thread is active when the action is `resume` (`BoundInv.sact`). -/ +theorem JStage3A.curSc_act (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (hact : L.actionW s = 0#u64) (h0 : L.mem s (scA (L.curW s)) ≠ 0#u64) : L.mem s (scA (L.curW s)) ∈ Ac := by + have hB := J.sys.ent.bxi + exact hB.2.1.sact _ J.sq.curT (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨hact, rfl⟩))))) h0 + +/-- PTU's `precut_nq`: the current thread is not queued. -/ +theorem JStage3A.cur_nq (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (hact : L.actionW s = 0#u64) : + (L.mem s (inqA (L.curW s)) &&& sched.Q_QUEUED) = 0#u64 := by + by_contra hq + have hc : L.curW s ∈ H.tcbs := J.sq.curT + have mem := (J.sq.queued (L.curW s) hc).1 hq + rw [J.sr.curOn] at mem + have hp : (L.mem s (prioA (L.curW s))).val < 256 := J.sq.prio _ hc + have hr := J.sr.rest.rq _ hp + have ct : σ.curThread = specPtr (L.curW s) := J.j.1.sch.curThread + have hres : σ.schedulerAction = .resumeCurThread := by simp [J.j.1.sch.act, hact, actOfA] + refine J.sinv.ctQ hres 0 (BitVec.ofNat 8 (L.mem s (prioA (L.curW s))).val) ?_ + rw [hr, ct] + exact List.mem_map_of_mem mem + +/-- **`CanBlock` of the current thread** from the record and the syscall-entry facts (module docstring). -/ +theorem JStage3A.canBlock (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (hact : L.actionW s = 0#u64) (hrun : Runnable (L.mem s (stA (L.curW s)))) + (hcur : (L.mem s (inqA (L.curW s)) &&& sched.Q_RELEASE) = 0#u64) : + CanBlock st H.tcbs Ac s (L.curW s) where + mem := J.sq.curT + nb := fun hb => by + rcases hrun with h | h | h <;> rcases hb with h' | h' | h' <;> rw [h] at h' <;> + exact absurd (congrArg UScalar.val h') (by + first + | (simp [sched.TS_RUNNING, sched.TS_RESTART, sched.TS_RUNNING_VM, sched.TS_BLOCKED_ON_SEND, + sched.TS_BLOCKED_ON_RECV, sched.TS_BLOCKED_ON_NTFN]; done) + | decide) + nrp := fun h' => by + rcases hrun with h | h | h <;> rw [h] at h' <;> + exact absurd (congrArg UScalar.val h') (by + first + | (simp [sched.TS_RUNNING, sched.TS_RESTART, sched.TS_RUNNING_VM, sched.TS_BLOCKED_ON_REPLY]; done) + | decide) + nq := J.cur_nq hact + nr := hcur + na := by rw [hact]; exact fun e => J.tcb_ne0 J.sq.curT e.symm + ni := J.cur_ne_idle hrun + ac := J.curSc_act hact + +/-- **`DecPre` at the decode site** (module docstring): the record, the syscall-entry facts and the invoked cap's +`OS` slot; `extra` is the open hypothesis `hext`. -/ +theorem JStage3EntryA.decPre + (J : JStage3EntryA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + (hact : L.actionW s = 0#u64) (hrun : Runnable (L.mem s (stA (L.curW s)))) + (hcsc : L.mem s (scA (L.curW s)) ≠ 0#u64 → L.cscW s = L.mem s (scA (L.curW s))) + (hcur : (L.mem s (inqA (L.curW s)) &&& sched.Q_RELEASE) = 0#u64 ∧ L.mem s (scA (L.curW s)) ≠ 0#u64) + {x : U64} (hx : x ∈ OS) {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hext : ∀ s', WritesIn st s s' (NodeW st s) → ∀ sl ∈ extra.val, sl ≠ 0#u64 → + obj.Slot.cap st sl s' ⦃ c => CapOK st H.tcbs H.replies H.scs H.ntfns H s' c ⦄) : + DecPre st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits U Ac s (capA st s x) extra where + bxi := J.sys.ent.bxi + csc := by rw [hcsc hcur.2]; exact J.toJStage3A.curSc_act hact hcur.2 + cap := J.toJStage3A.capOK hx + extra := hext + run := hrun + act := hact + hcsc := hcsc + cb := J.toJStage3A.canBlock hact hrun hcur.1 + user := J.sys.ent.user + rv := by have h := J.runVcpu; rwa [← J.tcbs] at h + nvm := J.curNotVmA + +end rows + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3SysRecvA.lean b/proofs/Fl4ProofsA64/P3/Stage3SysRecvA.lean new file mode 100644 index 000000000..c2c2124f5 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SysRecvA.lean @@ -0,0 +1,109 @@ +/- +P3 (aarch64, A64-P3 recvglue): **Recv, NBRecv, Wait and NBWait at the kernel-entry top** (the receive instance of +`P3/Stage3SysFrontA`, mirror of `P3/Stage3SysSendA`). + +* `syscallRecv_refinesA`: `kBodyA (.syscall _ rep call)` for `call` ∈ {Recv, NBRecv, Wait, NBWait} ends in `A64RelS3` + (`syscallFront_refinesA` with `Fin := A64RelS3`, the dispatch hole filled by `JSyscallA.recvBodyScopeA`, whose + `BodyOKA` is projected away). +* `kernelEntryRecv_recGA`: the end-to-end top, `kernelEntry_recGA` with that `hbody`. + +The only scope argument is `hScope`: a `RecvScopeA` (`P3/Stage3RecvScopeA`) at the entry pre-state `(sb, τ)` of the +dispatch, for each `(blocking, canReply)` of the call. It is **pinned to the reached state**, as `SysChargeA` is: guarded +by the equations the front establishes (`fl4SyscallFaultIP` gives `τPC`, FL4's `budget_ok` from the post-write store +returns `true` with `(sb, ab)`, `fl4BudgetOk τPC` returns `(true, τ)`) and by the `JSyscallA` record there; only the ghost +lists `Q`, `Rl` float. (A scope over every related state would be false: E6/N2, E2/E5, handler endpoints, ...) Its declared scope +limits (E1–E6, N1, N2, cap-fault handler endpoint, `ntfnSc = curSc`) are listed in that module. + +Named premises: those of `syscallFront_refinesA` / `kernelEntry_recGA` (`SysChargeA` [J], guarded `hGap` [P]/[S], +`hNat` [S4]; [P] `ArchTotal`, `ArchPlat`, `FaultEnvA`, `FinPlatA`, `ArchSwFA`, the four preemption-path agreements) and +the [S] `hScope`. No new kind. New module. +-/ +import Fl4ProofsA64.P3.Stage3SysSendA +import Fl4ProofsA64.P3.Stage3RecvScopeA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR AR' : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {s : S} {a : A} {σ : State Unit} {m : McState} {Clock : A → MachineState → Prop} + +set_option maxHeartbeats 4000000 in +/-- **Recv, NBRecv, Wait and NBWait at the entry** (module docstring). -/ +theorem syscallRecv_refinesA (hPl : ArchPlat ar) (Env : FaultEnvA st ar Z) + (hlimit : fl4MaxReleaseTime = MAX_RELEASE_TIME) + (hag : fl4MinBudget = MIN_BUDGET) (hconv : Fl4TicksToUsLegacyAgree) (v2 : config_ARM_GIC_V3 = false) + (number : I64) (rep : Bool) (call : Syscall) (hnum : number = syscallNumberA call) + (hc : call = .sysRecv ∨ call = .sysNBRecv ∨ call = .sysWait ∨ call = .sysNBWait) + (I : KEntryFaultInA st ar AR AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ hPl.ipa m Clock s a σ) + (hCharge : SysChargeA st ar AR AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ hPl.ipa m Clock s a σ) + (hScope : ∀ (Q : U64 → U64 → List U64) (Rl : U64 → List U64) sb ab τ (τPC : State Unit), + fl4SyscallFaultIP (Z := Unit) σ.curThread σ = ({((), τPC)}, False) → + syscall.budget_ok st ar (L.wr s (Endp.regA (L.curW s) layout.reg.FAULT_IP) + (core.num.U64.wrapping_sub (L.mem s (regWA (L.curW s) layout.reg.NEXT_IP)) 4#u64)) a = ok (true, sb, ab) → + fl4BudgetOk (Z := Unit) τPC = ({(true, τ)}, False) → + JSyscallA st AR' { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes + (J5A st) (J7A st (J7R2A st ar hPl.ipa)) sb ab τ m → CounterStepA τ → + ∀ blocking canReply, DispatchRecvAtA call blocking canReply → + RecvScopeA st OS N E Rp Z.T sb τ blocking canReply) : + kBodyA st ar s a (.syscall number rep call) ⦃ r => + StepPostA (fun s' a' τ => A64RelS3 st AR' ar hPl.ipa fl4A64Platform.numNodes Z.Cs s' a' τ m) + (kOpA (.syscall number rep call)) σ r ⦄ := by + refine syscallFront_refinesA (Fin := fun s' a' τ => A64RelS3 st AR' ar hPl.ipa fl4A64Platform.numNodes Z.Cs s' a' τ m) + hPl Env number rep call hnum I hCharge ?_ (fun _ _ _ R => R) + intro Q Rl sb ab τ τPC hpc eb e1 Jr Cn + have h := Jr.recvBodyScopeA Env.hA hPl Env.hCs Env.h1 Env.hmb Env.hmax Env.htu + (invoke := fl4HandleInvocationR (Z := Unit) rep) (pending := maybeHandleInterrupt true) call hc + (hScope Q Rl sb ab τ τPC hpc eb e1 Jr Cn) + rw [recvSettle_eqA (preemptionPath_agreeA hlimit hag Env.hmax hconv v2) rep call hc] at h + refine WP.spec_mono h ?_ + rintro r ⟨τ2, mem, R, -⟩ + exact ⟨τ2, mem, R⟩ + +set_option maxHeartbeats 4000000 in +/-- **Recv, NBRecv, Wait and NBWait end to end** (module docstring): `kernelEntry_recGA` with `hbody` from +`syscallRecv_refinesA`. -/ +theorem kernelEntryRecv_recGA (hPl : ArchPlat ar) (Env : FaultEnvA st ar Z) + (hlimit : fl4MaxReleaseTime = MAX_RELEASE_TIME) + (hag : fl4MinBudget = MIN_BUDGET) (hconv : Fl4TicksToUsLegacyAgree) (v2 : config_ARM_GIC_V3 = false) + (arch : ArchSwFA st ar) (P : FinPlatA ar AR') + {tm : A → Nat → DeadlineViewA} {view : State Unit → Fl4TimerView} + (number : I64) (rep : Bool) (call : Syscall) (hnum : number = syscallNumberA call) + (hc : call = .sysRecv ∨ call = .sysNBRecv ∨ call = .sysWait ∨ call = .sysNBWait) + (run : kRuns st s (.syscall number rep call)) + (I : KEntryFaultInA st ar AR AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ hPl.ipa m Clock s a σ) + (hCharge : SysChargeA st ar AR AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ hPl.ipa m Clock s a σ) + (hScope : ∀ (Q : U64 → U64 → List U64) (Rl : U64 → List U64) sb ab τ (τPC : State Unit), + fl4SyscallFaultIP (Z := Unit) σ.curThread σ = ({((), τPC)}, False) → + syscall.budget_ok st ar (L.wr s (Endp.regA (L.curW s) layout.reg.FAULT_IP) + (core.num.U64.wrapping_sub (L.mem s (regWA (L.curW s) layout.reg.NEXT_IP)) 4#u64)) a = ok (true, sb, ab) → + fl4BudgetOk (Z := Unit) τPC = ({(true, τ)}, False) → + JSyscallA st AR' { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes + (J5A st) (J7A st (J7R2A st ar hPl.ipa)) sb ab τ m → CounterStepA τ → + ∀ blocking canReply, DispatchRecvAtA call blocking canReply → + RecvScopeA st OS N E Rp Z.T sb τ blocking canReply) + (hGap : ∀ s' a' τ, kBodyA st ar s a (.syscall number rep call) = ok (s', a') → + ((), τ) ∈ (kOpA (.syscall number rep call) σ).1 → + A64RelS3 st AR' ar hPl.ipa fl4A64Platform.numNodes Z.Cs s' a' τ m → SchedGapFreeA st m s' a' τ ar tm view) + (hNat : ∀ s' a' τ, kBodyA st ar s a (.syscall number rep call) = ok (s', a') → + ((), τ) ∈ (kOpA (.syscall number rep call) σ).1 → + A64RelS3 st AR' ar hPl.ipa fl4A64Platform.numNodes Z.Cs s' a' τ m → ∀ τ1 τ2, + ((), τ1) ∈ (fl4Schedule kLimitA view τ).1 → ((), τ2) ∈ (fl4ActivateThread (Z := Unit) τ1).1 → + ∀ y, τ2.kheap τ2.curThread = some (.tcb y) → y.tcbState ≠ .ext .runningVM) : + kCodeA st ar s a (.syscall number rep call) ⦃ out => + StepPostA (KExitA st ar AR' hPl.ipa m) + (kOpA (.syscall number rep call) >>= fun _ => fl4Finish kLimitA view) σ out ⦄ := + kernelEntry_recGA Env.hA hPl arch Env.hCs P s a σ _ run + (syscallRecv_refinesA hPl Env hlimit hag hconv v2 number rep call hnum hc I hCharge hScope) hGap hNat + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3SysSendA.lean b/proofs/Fl4ProofsA64/P3/Stage3SysSendA.lean new file mode 100644 index 000000000..cd6d77db6 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SysSendA.lean @@ -0,0 +1,170 @@ +/- +P3 (aarch64, A64-P3 sysfront): **Send, NBSend and Call at the kernel-entry top** (the instance of `P3/Stage3SysFrontA`). + +* `SendScopeA`: the four scope premises of `JSyscallA.sendBodyScopeA` ([J] `hFault`, `hArm`, `hErr`, and `HEnv`: the premises of `hPre`), bundled. +* `preemptionPath_agreeA`: `fl4PreemptionPath (maybeHandleInterrupt true) = fl4PreemptionPathIrq` from the five existing + [P] agreements (`fl4MaxReleaseTime`, `fl4MinBudget`, `maxTicksToUs`, `Fl4TicksToUsLegacyAgree`, the GICv2 `v2`): + no opaque `hpend`. +* `syscallSend_refinesA`: `kBodyA (.syscall _ rep call)` for `call` = Send / NBSend / Call ends in `A64RelS3`, the + relation of `kernelEntry_recGA` (`P3/Stage3SysTopA`). + +Named premises: those of `sendBodyScopeA` ([P] `ArchTotal`, `MaxSpi`, `ArchPlat`, `IrqPlatA`, `PlainStore`; [J] the +scope, `hCharge` as `SysChargeA`), and the [P] agreements above. New module. +-/ +import Fl4ProofsA64.P3.Stage3SysFrontA +import Fl4ProofsA64.P3.Stage3SysTopA +import Fl4ProofsA64.P3.Stage3ScopeA +import Fl4ProofsA64.P3.Stage3HPreA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-- **The scope premises of `JSyscallA.sendBodyScopeA`** (module docstring). -/ +structure SendScopeA {S : Type} (st : machine.Store S) [L : LawfulStore st] {A : Type} (ar : machine.Arch A) + (AR : A → HwRootA → Prop) (Z : XSetsA) (H : Heap) (OS U Ac Pp Tb : Set U64) (Lv : U64 → Nat) (ia ie : Nat) + (N E Rp : Set U64) (Bf : Set (U64 × Fl4SpecA64.Word)) (stack : U64 → List U64) (κ : U64 → U64) (n : Nat) + (s : S) (a : A) (σ : Fl4SpecA64.State Unit) (m : Fl4SpecA64.McState) (Clock : A → Fl4SpecA64.MachineState → Prop) + (hPl : ArchPlat ar) (rep : Bool) (pending : SMonad Unit Unit) (call : Fl4SpecA64.Syscall) : Prop where + hFault : ∀ calling blocking donate, + DispatchInvokeAtA (L.mem s (regWA (L.curW s) layout.reg.CAP)) (L.mem s (regWA (L.curW s) layout.reg.REPLY)) + (L.mem s (regWA (L.curW s) layout.reg.NBSEND_RECV_DEST)) call calling blocking donate false + (L.mem s (regWA (L.curW s) layout.reg.CAP)) → + blocking = true → ∀ (f : error.Fault) (f' : Fl4SpecA64.Fault), CapFaultCorrA f f' → + faults.handle_fault st ar s a (L.curW s) f ⦃ r => ∃ τ, ((), τ) ∈ (handleFault (Z := Unit) σ.curThread f' σ).1 ∧ + A64RelS3 st AR ar hPl.ipa n Z.Cs r.1 r.2 τ m ⦄ + hArm : ∀ calling blocking donate, + DispatchInvokeAtA (L.mem s (regWA (L.curW s) layout.reg.CAP)) (L.mem s (regWA (L.curW s) layout.reg.REPLY)) + (L.mem s (regWA (L.curW s) layout.reg.NBSEND_RECV_DEST)) call calling blocking donate false + (L.mem s (regWA (L.curW s) layout.reg.CAP)) → + ∀ (info : msginfo.MessageInfo) (buf : U64) (r1 : cspace.Resolved) (e : Aeneas.Std.Array obj.Slot 3#usize) + (mi : Fl4SpecA64.MessageInfo) (sl : Fl4SpecA64.CslotPtr) (c : Fl4SpecA64.Cap) + (excaps : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) (buffer : Option Fl4SpecA64.ObjRef) (argsU : List U64), + msg.msg_info st s (L.curW s) = ok info → msg.lookup_ipc_buffer st s false (L.curW s) = ok buf → + cspace.lookup_slot st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.CAP)) = ok (.Ok r1) → + ipc.lookup_extra_caps st s (L.curW s) buf info = ok (.Ok e) → + SiteA st Z OS s σ info buf r1.slot e mi sl c excaps buffer argsU → + ∀ (r0 : core.result.Result Unit error.Exc) (s0 : S) (a0 : A), + syscall.decode_invocation st ar s a info.label (msgLenA info buf) r1.slot (capA st s r1.slot) blocking + calling donate false buf e = ok (r0, s0, a0) → (∀ x, r0 = .Err x → x = error.Exc.Preempted) → + ∃ Out, DispScopeA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ m Clock hPl.ipa info.label + (msgLenA info buf) r1.slot blocking calling donate false buf e Out + hErr : ∀ calling blocking donate, + DispatchInvokeAtA (L.mem s (regWA (L.curW s) layout.reg.CAP)) (L.mem s (regWA (L.curW s) layout.reg.REPLY)) + (L.mem s (regWA (L.curW s) layout.reg.NBSEND_RECV_DEST)) call calling blocking donate false + (L.mem s (regWA (L.curW s) layout.reg.CAP)) → + ∀ (info : msginfo.MessageInfo) (buf : U64) (r1 : cspace.Resolved) + (e : Aeneas.Std.Array obj.Slot 3#usize) (x : error.Exc) (s' : S) (a' : A), + msg.msg_info st s (L.curW s) = ok info → msg.lookup_ipc_buffer st s false (L.curW s) = ok buf → + cspace.lookup_slot st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.CAP)) = ok (.Ok r1) → + ipc.lookup_extra_caps st s (L.curW s) buf info = ok (.Ok e) → + syscall.decode_invocation st ar s a info.label (msgLenA info buf) r1.slot (capA st s r1.slot) blocking + calling donate false buf e = ok (.Err x, s', a') → x ≠ error.Exc.Preempted → + (match x with + | .Sys e2 => if calling then (do let s2 ← syscall.reply_error st s' (L.curW s) e2 + ok (core.result.Result.Ok (), s2, a')) + else ok (core.result.Result.Ok (), s', a') + | _ => ok (core.result.Result.Ok (), s', a') : Result (core.result.Result Unit error.Exc × S × A)) + ⦃ r => r.1 = .Ok () ∧ ∃ τ, + (.ok (), τ) ∈ ((fl4HandleInvocationR (Z := Unit) rep calling blocking donate false + (Fl4SpecA64.toBl (L.mem s (regWA (L.curW s) layout.reg.CAP)).bv)).run σ).1 ∧ A64RelS3 st AR ar hPl.ipa n Z.Cs r.2.1 r.2.2 τ m ⦄ + HEnv : HPreEnvA st AR ar hPl n Z.Cs m Clock pending s a call + +/-- The preemption paths of FL4 and of the spec's entry agree under the platform agreements. -/ +theorem preemptionPath_agreeA (hlimit : fl4MaxReleaseTime = MAX_RELEASE_TIME) (hmin : fl4MinBudget = MIN_BUDGET) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) (hconv : Fl4TicksToUsLegacyAgree) + (v2 : config_ARM_GIC_V3 = false) : + fl4PreemptionPath (Z := Unit) (maybeHandleInterrupt true) = fl4PreemptionPathIrq := by + have cap : fl4MaxConsumedTicks = maxTicksToUs := by rw [hmax]; rfl + exact (fl4PreemptionPath_eq_l4v hlimit hmin cap hconv).trans (fl4PreemptionPathIrq_eq_l4v v2).symm + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {Clock : A → Fl4SpecA64.MachineState → Prop} +variable {AR' : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {s : S} {a : A} {σ : State Unit} {m : McState} {Clock : A → MachineState → Prop} + +set_option maxHeartbeats 4000000 in +/-- **Send, NBSend and Call at the entry** (module docstring). -/ +theorem syscallSend_refinesA [PlainStore st] (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (hIP : IrqPlatA st ar) (Env : FaultEnvA st ar Z) (hlimit : fl4MaxReleaseTime = MAX_RELEASE_TIME) + (hag : fl4MinBudget = MIN_BUDGET) (hconv : Fl4TicksToUsLegacyAgree) (v2 : config_ARM_GIC_V3 = false) + (number : I64) (rep : Bool) (call : Syscall) (hnum : number = syscallNumberA call) + (hc : call = .sysSend ∨ call = .sysNBSend ∨ call = .sysCall) + (I : KEntryFaultInA st ar AR AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ hPl.ipa m Clock s a σ) + (hCharge : SysChargeA st ar AR AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ hPl.ipa m Clock s a σ) + (hScope : ∀ (Q : U64 → U64 → List U64) (Rl : U64 → List U64) sb ab τ (τPC : State Unit), + fl4SyscallFaultIP (Z := Unit) σ.curThread σ = ({((), τPC)}, False) → + syscall.budget_ok st ar (L.wr s (Endp.regA (L.curW s) layout.reg.FAULT_IP) + (core.num.U64.wrapping_sub (L.mem s (regWA (L.curW s) layout.reg.NEXT_IP)) 4#u64)) a = ok (true, sb, ab) → + fl4BudgetOk (Z := Unit) τPC = ({(true, τ)}, False) → + JSyscallA st AR' { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes + (J5A st) (J7A st (J7R2A st ar hPl.ipa)) sb ab τ m → CounterStepA τ → + SendScopeA st ar AR' { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ + fl4A64Platform.numNodes sb ab τ m Clock hPl rep (maybeHandleInterrupt true) call) : + kBodyA st ar s a (.syscall number rep call) ⦃ r => + StepPostA (fun s' a' τ => A64RelS3 st AR' ar hPl.ipa fl4A64Platform.numNodes Z.Cs s' a' τ m) + (kOpA (.syscall number rep call)) σ r ⦄ := by + refine syscallFront_refinesA (Fin := fun s' a' τ => A64RelS3 st AR' ar hPl.ipa fl4A64Platform.numNodes Z.Cs s' a' τ m) + hPl Env number rep call hnum I hCharge ?_ (fun _ _ _ R => R) + intro Q Rl sb ab τ τPC hpc eb e1 Jr Cn + have Sc := hScope Q Rl sb ab τ τPC hpc eb e1 Jr Cn + have h := Jr.sendBodyScopeA (hCnt := Cn) hA hM hPl (A64FrameMasks.of_plain sb) hIP rep + (pending := maybeHandleInterrupt true) call hc Sc.hFault Sc.hArm Sc.hErr ⟨⟨Env.hA, Env.hCs, Env.h1, Env.hmb, Env.hmax, Env.htu⟩, hag, hconv, I.hnd⟩ Sc.HEnv + have e : fl4SettleDispatch (maybeHandleInterrupt true) + (fl4DispatchSyscall (fl4HandleInvocationR (Z := Unit) rep) call) = + NonDetE.catch (fl4HandleSyscall (Z := Unit) rep call) (fun _ => fl4PreemptionPathIrq) := by + rw [← preemptionPath_agreeA hlimit hag Env.hmax hconv v2] + unfold fl4SettleDispatch + rcases hc with rfl | rfl | rfl <;> + simp only [fl4DispatchSyscall, fl4HandleSyscall, fl4HandleSyscallWith] + rw [e] at h + exact h + +set_option maxHeartbeats 4000000 in +/-- **Send, NBSend and Call end to end** (module docstring): `kernelEntry_recGA` with `hbody` from +`syscallSend_refinesA`. -/ +theorem kernelEntrySend_recGA [PlainStore st] (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (hIP : IrqPlatA st ar) (Env : FaultEnvA st ar Z) (hlimit : fl4MaxReleaseTime = MAX_RELEASE_TIME) + (hag : fl4MinBudget = MIN_BUDGET) (hconv : Fl4TicksToUsLegacyAgree) (v2 : config_ARM_GIC_V3 = false) + (arch : ArchSwFA st ar) (P : FinPlatA ar AR') + {tm : A → Nat → DeadlineViewA} {view : State Unit → Fl4TimerView} + (number : I64) (rep : Bool) (call : Syscall) (hnum : number = syscallNumberA call) + (hc : call = .sysSend ∨ call = .sysNBSend ∨ call = .sysCall) + (run : kRuns st s (.syscall number rep call)) + (I : KEntryFaultInA st ar AR AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ hPl.ipa m Clock s a σ) + (hCharge : SysChargeA st ar AR AR' Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ hPl.ipa m Clock s a σ) + (hScope : ∀ (Q : U64 → U64 → List U64) (Rl : U64 → List U64) sb ab τ (τPC : State Unit), + fl4SyscallFaultIP (Z := Unit) σ.curThread σ = ({((), τPC)}, False) → + syscall.budget_ok st ar (L.wr s (Endp.regA (L.curW s) layout.reg.FAULT_IP) + (core.num.U64.wrapping_sub (L.mem s (regWA (L.curW s) layout.reg.NEXT_IP)) 4#u64)) a = ok (true, sb, ab) → + fl4BudgetOk (Z := Unit) τPC = ({(true, τ)}, False) → + JSyscallA st AR' { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes + (J5A st) (J7A st (J7R2A st ar hPl.ipa)) sb ab τ m → CounterStepA τ → + SendScopeA st ar AR' { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ + fl4A64Platform.numNodes sb ab τ m Clock hPl rep (maybeHandleInterrupt true) call) + (hGap : ∀ s' a' τ, kBodyA st ar s a (.syscall number rep call) = ok (s', a') → + ((), τ) ∈ (kOpA (.syscall number rep call) σ).1 → + A64RelS3 st AR' ar hPl.ipa fl4A64Platform.numNodes Z.Cs s' a' τ m → SchedGapFreeA st m s' a' τ ar tm view) + (hNat : ∀ s' a' τ, kBodyA st ar s a (.syscall number rep call) = ok (s', a') → + ((), τ) ∈ (kOpA (.syscall number rep call) σ).1 → + A64RelS3 st AR' ar hPl.ipa fl4A64Platform.numNodes Z.Cs s' a' τ m → ∀ τ1 τ2, + ((), τ1) ∈ (fl4Schedule kLimitA view τ).1 → ((), τ2) ∈ (fl4ActivateThread (Z := Unit) τ1).1 → + ∀ y, τ2.kheap τ2.curThread = some (.tcb y) → y.tcbState ≠ .ext .runningVM) : + kCodeA st ar s a (.syscall number rep call) ⦃ out => + StepPostA (KExitA st ar AR' hPl.ipa m) + (kOpA (.syscall number rep call) >>= fun _ => fl4Finish kLimitA view) σ out ⦄ := + kernelEntry_recGA hA hPl arch Env.hCs P s a σ _ run + (syscallSend_refinesA hA hM hPl hIP Env hlimit hag hconv v2 number rep call hnum hc I hCharge hScope) hGap hNat + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3SysTopA.lean b/proofs/Fl4ProofsA64/P3/Stage3SysTopA.lean new file mode 100644 index 000000000..bdda615d9 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SysTopA.lean @@ -0,0 +1,106 @@ +/- +P3 (aarch64, A64-P3 sysfront): **the unified top for entries whose body changes the ghost sets** (system calls). + +`kernelEntry_recA` fixes the ghost sets (`H`, `OS`, `U`, …) of the record at the body's end, which is right for the fault +entries and false for an invocation (Retype / delete / insert change them). Architect ruling (2026-12-05): fixed are +`st`, `ar`, `AR'`, `hPl.ipa`, `m` and `Z.Cs`; the ghost sets float existentially, 1:1 with `A64RelS3` / `KExitA`. + +* `SchedGapFreeA`: `SchedGapA` without the ghost sets (`Z` unused, `κ` only in a hypothesis). +* `A64RelS3.finishA`: `syscall.finish` from a record at some ghost sets, with `BodyOKA`. +* `kernelEntry_recGA`: the unified top with `hbody` ending in `A64RelS3`. The three fault entries stay as they are. + +Named premises: those of `kernelEntry_recA` ([P] `ArchTotal`, `ArchPlat`, `ArchSwFA`, `FinPlatA`, [S] `hCs`); +`hGap` (`SchedGapFreeA` at the body's reached record `A64RelS3`: [S] `oneDomain`, [P] `minAgree`, [P] `ticks`, [J] `timer`, guarded by the run awaken → select → `switch_sched_context`); `hNat` ([S4] +`hNative`, ghost-free). +New module. +-/ +import Fl4ProofsA64.P3.Stage3KEntryFaultFinA +import Fl4ProofsA64.P3.Stage3DispBaseA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + +/-- **`SchedGapA` without the ghost sets** (module docstring): the same premises on the scheduler's inputs, `κ` only in +the hypothesis `YieldQueueRelA`. Tags: [S] `oneDomain`, [P] `minAgree`, [P] `ticks`, [J] `timer` (guarded by the run: +awaken → select the action → `switch_sched_context`). -/ +structure SchedGapFreeA (st : machine.Store S) [L : LawfulStore st] (m : McState) (s : S) (a : A) (σ : State Unit) + (ar : machine.Arch A) (tm : A → Nat → DeadlineViewA) (view : State Unit → Fl4TimerView) : Prop where + /-- [S] one scheduling domain -/ + oneDomain : numDomains = 1 + /-- [P] FL4's minimum budget is the spec's -/ + minAgree : fl4MinBudget = MIN_BUDGET + /-- [P] the tick conversions of a scheduling-context configuration agree -/ + ticks : ∀ a', ScCfgTimeAgreeA ar a' + /-- [J] the timer inputs and `SniHypA` after `switch_sched_context`, guarded by the run that reaches it -/ + timer : ∀ (start selected : S × A) (Z3 : XSetsA) σ3, sched.awaken st ar s a = ok start → + scheduleSelectActionA (st := st) (ar := ar) start.1 start.2 = ok selected → + ∀ C σ0 s0 s4 σ4, sched.set_action st selected.1 sched.ACTION_RESUME_CURRENT = ok s0 → + sched.switch_sched_context st ar s0 selected.2 = ok (s4, selected.2) → + fl4SwitchSchedContext (Z := Unit) σ0 = ({((), σ4)}, False) → + σ0 = { σ3 with schedulerAction := .resumeCurThread } → MFrA (st := st) Z3 m selected.1 s4 σ3 σ4 → + (∃ κ : U64 → U64, YieldQueueRelA (st := st) { Z3 with curScOf := C } m κ s4 σ4) → σ4.reprogramTimer = true → + ScheduleTimerInputsA (st := st) s4 σ4 view ∧ SniHypA ar tm selected.2 (L.cpu s4).val + +theorem SchedGapFreeA.toGap {m : McState} {s : S} {a : A} {σ : State Unit} {tm : A → Nat → DeadlineViewA} + {view : State Unit → Fl4TimerView} (G : SchedGapFreeA st m s a σ ar tm view) (Z : XSetsA) (κ : U64 → U64) : + SchedGapA (st := st) Z m κ s a σ ar tm view := + ⟨G.oneDomain, G.minAgree, G.ticks, by + intro start selected Z3 σ3 h1 h2 C σ0 s0 s4 σ4 h3 h4 h5 h6 h7 hY h8 + exact G.timer start selected Z3 σ3 h1 h2 C σ0 s0 s4 σ4 h3 h4 h5 h6 h7 ⟨κ, hY⟩ h8⟩ + +section +variable {AR' : A → HwRootA → Prop} {m : McState} {Cs : Set U64} {s : S} {a : A} {σ : State Unit} + {tm : A → Nat → DeadlineViewA} {view : State Unit → Fl4TimerView} + +set_option maxHeartbeats 2000000 in +/-- **`syscall.finish` from a record at some ghost sets** (module docstring): `FaultRelA.finishA` for `A64RelS3`. -/ +theorem A64RelS3.finishA (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) (arch : ArchSwFA st ar) + (hCs : ∀ c ∈ Cs, c.val < fl4A64Platform.numNodes) (P : FinPlatA ar AR') + (G : SchedGapFreeA st m s a σ ar tm view) + (hNat : ∀ τ1 τ2, ((), τ1) ∈ (fl4Schedule kLimitA view σ).1 → + ((), τ2) ∈ (fl4ActivateThread (Z := Unit) τ1).1 → + ∀ y, τ2.kheap τ2.curThread = some (.tcb y) → y.tcbState ≠ .ext .runningVM) + (R : A64RelS3 st AR' ar hPl.ipa fl4A64Platform.numNodes Cs s a σ m) : + syscall.finish st ar s a ⦃ r => StepPostA (KExitA st ar AR' hPl.ipa m) (fl4Finish kLimitA view) σ r ⦄ := by + obtain ⟨Z, H, OS, U, Ac, Pp, Tb, Lv, ia, ie, N, E, Rp, Bf, stack, κ, hZ, J, B⟩ := R + have hCs' : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes := fun c hc => hCs c (hZ ▸ hc) + have hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄ := fun a => + WP.spec_mono (G.ticks a).minBudget fun _ h => h.trans G.minAgree + exact JStage3A.finishEA hA hPl arch hCs' P.h1 J B.sw B.toBodyExitA + (JStage3A.fireSigA hA hPl hCs' P.h1 hmb P.hmax P.htu) + (G.toGap Z κ) P.arK P.toUs hNat + +/-- **The unified kernel-entry top for entries whose body changes the ghost sets** (module docstring). + +Coverage (s4w): the VCPU labels 60-69, including `ARMVCPU_Run`, refine at the body / dispatch level (`VcpuScopeA`); +the VM-exit entries `runExit` and `runVppi` refine end to end (`P3/Stage3VcpuExitWireA`, which supplies `hbody`, +`hGap` and `hNat` for them). The `finish` carries [S4] `hNat` (`hNative`), so a Run that resumes the guest (the caller +stays current in `runningVM`) is outside this top. `vgic`, `vppi`, `vcpuExit` and `vcpuFault` are deferred to the S4 +guest-lifecycle follow-up. -/ +theorem kernelEntry_recGA (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) (arch : ArchSwFA st ar) + (hCs : ∀ c ∈ Cs, c.val < fl4A64Platform.numNodes) (P : FinPlatA ar AR') + (s : S) (a : A) (σ : State Unit) (e : KEventA) (run : kRuns st s e) + (hbody : kBodyA st ar s a e ⦃ r => StepPostA (fun s' a' τ => + A64RelS3 st AR' ar hPl.ipa fl4A64Platform.numNodes Cs s' a' τ m) (kOpA e) σ r ⦄) + (hGap : ∀ s' a' τ, kBodyA st ar s a e = ok (s', a') → ((), τ) ∈ (kOpA e σ).1 → + A64RelS3 st AR' ar hPl.ipa fl4A64Platform.numNodes Cs s' a' τ m → SchedGapFreeA st m s' a' τ ar tm view) + (hNat : ∀ s' a' τ, kBodyA st ar s a e = ok (s', a') → ((), τ) ∈ (kOpA e σ).1 → + A64RelS3 st AR' ar hPl.ipa fl4A64Platform.numNodes Cs s' a' τ m → ∀ τ1 τ2, + ((), τ1) ∈ (fl4Schedule kLimitA view τ).1 → ((), τ2) ∈ (fl4ActivateThread (Z := Unit) τ1).1 → + ∀ y, τ2.kheap τ2.curThread = some (.tcb y) → y.tcbState ≠ .ext .runningVM) : + kCodeA st ar s a e ⦃ out => + StepPostA (KExitA st ar AR' hPl.ipa m) (kOpA e >>= fun _ => fl4Finish kLimitA view) σ out ⦄ := + kernelEntry_stage3A s a σ e run hbody fun r τ eb op RB => + A64RelS3.finishA hA hPl arch hCs P (hGap r.1 r.2 τ eb op RB) (hNat r.1 r.2 τ eb op RB) RB + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3SyscallA.lean b/proofs/Fl4ProofsA64/P3/Stage3SyscallA.lean new file mode 100644 index 000000000..8478b730d --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3SyscallA.lean @@ -0,0 +1,69 @@ +/- +P3 (aarch64, A64-P3 Stage 3, T3 G4): **the syscall-entry record** `JSyscallA` (Architect ruling G4, F5). + +`JSyscallA` extends `JStage3EntryA` with the four facts of the previous kernel exit's `schedule` (+ `activate_thread`): + +* `act`: `σ.schedulerAction = .resumeCurThread`; +* `running`: the current thread's state word is runnable; +* `curSc`: a non-zero current-thread SC word is the core's current SC (`cscW`); +* `cur`: the current thread's release-queue bit is clear and its SC word is non-zero. + +Not true at `w1` (the boot state is `switchThread root`): the first syscall comes after the first `schedule`. + +* `JSyscallA.noSwitch`: `JStage3TA`'s `noSwitch` (one line). +* `JSyscallA.actW`: `L.actionW s = 0#u64`, i.e. `DecPre.act`. + +Named premises: none (the fields are the record's [J] facts; their producer, the schedule post, is a separate item). +-/ +import Fl4ProofsA64.P3.Stage3TA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + +noncomputable section + +section +variable {S : Type} (st : machine.Store S) [L : LawfulStore st] {A : Type} + +/-- **The syscall-entry record** (module docstring). -/ +structure JSyscallA (AR : A → HwRootA → Prop) (Z : XSetsA) (H : Heap) (OS U Ac Pp Tb : Set U64) + (Lv : U64 → Nat) (ia ie : Nat) (N E Rp : Set U64) (Bf : Set (U64 × Fl4SpecA64.Word)) (stack : U64 → List U64) + (κ : U64 → U64) (n : Nat) (J5 J7 : JSlotA S) (s : S) (a : A) (σ : Fl4SpecA64.State Unit) + (m : Fl4SpecA64.McState) : Prop + extends JStage3EntryA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m where + /-- [J] (G4) The previous exit's `schedule` left the scheduler action at `resumeCurThread`. -/ + act : σ.schedulerAction = .resumeCurThread + /-- [J] (G4) The current thread is runnable (`activate_thread`'s post). -/ + running : Runnable (L.mem s (stA (L.curW s))) + /-- [J] (G4) A non-zero current-thread SC word is the core's current SC. -/ + curSc : L.mem s (scA (L.curW s)) ≠ 0#u64 → L.cscW s = L.mem s (scA (L.curW s)) + /-- [J] (G4) The current thread is not in the release queue and has an SC. -/ + cur : (L.mem s (inqA (L.curW s)) &&& sched.Q_RELEASE) = 0#u64 ∧ L.mem s (scA (L.curW s)) ≠ 0#u64 + +variable {st} {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- **`JStage3TA`'s `noSwitch` from the syscall-entry record.** -/ +theorem JSyscallA.noSwitch (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) : + ∀ u, σ.schedulerAction ≠ .switchThread u := fun u => by rw [J.act]; intro h; cases h + +/-- **`DecPre.act`**: the scheduler-action word is `0` (`actOfA` of the word is `resumeCurThread`). -/ +theorem JSyscallA.actW (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) : + L.actionW s = 0#u64 := by + have h := J.sr.sch.act + rw [J.act] at h + unfold actOfA at h + by_cases h0 : L.actionW s = 0#u64 + · exact h0 + · rw [if_neg h0] at h + by_cases h1 : L.actionW s = 1#u64 + · rw [if_pos h1] at h; cases h + · rw [if_neg h1] at h; cases h + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3T3ArmCoreA.lean b/proofs/Fl4ProofsA64/P3/Stage3T3ArmCoreA.lean new file mode 100644 index 000000000..e6894ffa0 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3T3ArmCoreA.lean @@ -0,0 +1,127 @@ +/- +P3 (aarch64, A64-P3 Stage 3, T3 step 2): **the extended frame-map core at a syscall entry** (`Stage3T3ArmsA`). + +Named premises: [P] `ArchTotal`, `MaxSpi`, `ArchPlat`, `A64FrameMasks`; the arm's; `hinv` (decode site). +-/ +import Fl4ProofsA64.P3.Stage3BodyKeepA +import Fl4ProofsA64.P3.Stage3MapExtCoreA +import Fl4ProofsA64.P3.Stage3FinA +import Fl4ProofsA64.P3.Stage3RsSiteA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +section +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +set_option maxHeartbeats 4000000 in +/-- **Extended frame map (core)**, at a syscall entry; `hinv`: the decode-site equation of the invocation. -/ +theorem JSyscallA.frameMapExtCoreA + {AR : A → HwRootA → Prop} + {Z : XSetsA} + {H : Heap} + {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} + {ia ie : Nat} + {N E Rp : Set U64} + {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} + {κ : U64 → U64} + {n : Nat} + + {s : S} + {a : A} + {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} + {fp rt asid0 va0 : U64} + {sz : capability.FrameSize} + {dev mp e0 : Bool} + {c : capability.Cap} + {newSC : Fl4SpecA64.ArchCap} + {q j w : U64} + {x : Fl4SpecA64.Tcb} + {s2 s3 : S} + {a3 : A} + {σ2 : Fl4SpecA64.State Unit} + {ar : machine.Arch A} + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM) + (hK : Aligned (L.ksb s)) + (hT : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s)) + (cte : Fl4SpecA64.CslotPtr) + (hab : AbsSlotA Z.th cte) + (hcap : capA st s (cslotAddrA cte) = .Frame fp sz rt dev mp e0 asid0 va0) + (hc : ∃ mp' e' a' v', c = .Frame fp sz rt dev mp' e' a' v' ∧ + (e' = false ∨ ∀ t ∈ Z.T, sbWordA t ≠ cslotAddrA cte)) + (hEnc : CapRt.EncWf c) + (hca : CapRelA c (.archObjectCap newSC)) + (hok : CapObjOkA Z c) + (hq : XqA Z q) + (hj : j.val < 512) + (hal : Aligned (peAF q j)) + (sl : Fl4SpecA64.ExtSlot) + (pte : Fl4SpecA64.PTE) + (hk : σ.kheap σ.curThread = some (.tcb x)) + (hsc : obj.Slot.set_cap st (cslotAddrA cte) (L.wr s (stA (L.curW s)) sched.TS_RESTART) c = ok s2) + (hσ2 : ((), σ2) ∈ (Fl4SpecA64.setCap (Z := Unit) (.archObjectCap newSC) cte + (stUpdA σ σ.curThread x .restart)).1) + (hwP : ExtWordWriteA (st := st) s2 s3 (peAF q j) w) + (J3 : JAtZAX (L := L) AR Z s3 a3 (extSlotWriteX σ2 sl pte) m) + (hsl : ExtSlotOkX σ2 sl) + (ev : ∀ (Pt Pp' Tb' Hr Te Kt : Set U64) (Lv' Re bits : U64 → Nat), + S1aR2 st H OS Pt Pp' Tb' Hr Te Kt Lv' Re bits hPl.ipa s → + (Ub : Set U64) → VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt → + s3 = s ∨ ∃ tag f, S1aFrameMap st OS Pt Pp' hPl.ipa tag s s3 (cslotAddrA cte) f) + (hunm : ∀ s1 : S, OSKeep st OS s s1 → L.ksb s1 = L.ksb s → L.cpu s1 = L.cpu s → + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s1 a + (stUpdA σ σ.curThread x .restart) m → + ∀ Pt : Set U64, (∀ p, SchedPageA st Z OS s1 p → p ∈ Pt) → + (∀ u ∈ OS, ∀ c, capA st s3 u = c → (∀ p sz r d mp e a v, c ≠ .Frame p sz r d mp e a v) → capA st s1 u = c) → + (∀ p ∈ Pt, ∀ i : U64, i.val < 512 → peAF p i ≠ peAF q j → L.mem s3 (peAF p i) = L.mem s1 (peAF p i)) → + L.ksb s3 = L.ksb s1 → L.cpu s3 = L.cpu s1 → + (∀ p ao, p ≠ extSlotObjX sl → (stUpdA σ σ.curThread x .restart).kheap p = some (.archObj ao) → + (extSlotWriteX σ2 sl pte).kheap p = some (.archObj ao)) → + UnmPtSpecA st Z m OS s3 (extSlotWriteX σ2 sl pte)) + {ex : Aeneas.Std.Array obj.Slot 3#usize} {callD : Bool} {lenD bufD : U64} + (hinv : syscall.decode_invocation st ar s a fl4_abi.label.ARMPageMap lenD (cslotAddrA cte) + (.Frame fp sz rt dev mp e0 asid0 va0) false callD false false bufD ex = ok (core.result.Result.Ok (), s3, a3)) + (hcov : SlotCov st OS s (cslotAddrA cte)) + (hext : ∀ x ∈ ex.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ ex.val, x ≠ 0#u64 → SlotCov st OS s x) : + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + (([] : List Fl4SpecA64.Data), extSlotWriteX σ2 sl pte) ∈ (Fl4SpecA64.performExtVSpace (Z := Unit) + (.map newSC cte pte sl) (stUpdA σ σ.curThread x .restart)).1 ∧ + ∃ Ac' : Set U64, JStage3A st AR Z H OS U Ac' Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s3 a3 (extSlotWriteX σ2 sl pte) m ∧ + Sched.BodyKeepA (stUpdA σ σ.curThread x .restart) (extSlotWriteX σ2 sl pte) := by + have hslot : (cslotAddrA cte) ∈ OS := J.j5.2.1 _ (hab.isSlot J.j.1.place) + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hslot hcov J.actW J.running J.cur + have D : ∀ {bB bC bD bF : Bool} {len buf' : U64} {rX : core.result.Result Unit error.Exc} {sX : S} {aX : A}, + syscall.decode_invocation st ar s a fl4_abi.label.ARMPageMap len (cslotAddrA cte) (.Frame fp sz rt dev mp e0 asid0 va0) bB bC bD bF buf' ex = ok (rX, sX, aX) → + ∀ σX : Fl4SpecA64.State Unit, ∃ Ac' : Set U64, SysL st H Z.Cs OS ia ie sX ∧ SymL st H Z.Cs sX ∧ + BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac' sX ∧ + Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U sX ∧ + J7A st (J7R2A st ar hPl.ipa) Z H OS sX σX m := fun hd σX => + (J.decodeFinA hA hM hPl hfm hslot hcov (hcap) hext hSC (lbl := fl4_abi.label.ARMPageMap) (by simp [DecArmS1aEnt, DecArmQH]) + (fun _ h => by cases h) (fun _ h => by cases h) hd σX m).2 + refine JStage3A.frameMapExtCoreA (ar := ar) (nA := hPl.ipa) (J := J.toJStage3A) (RS := RS) (hnvm := hnvm) (hK := hK) (hT := hT) (cte := cte) (hab := hab) (hcap := hcap) (hc := hc) (hEnc := hEnc) (hca := hca) (hok := hok) (hq := hq) (hj := hj) (hal := hal) (sl := sl) (pte := pte) (hk := hk) (hsc := hsc) (hσ2 := hσ2) (hwP := hwP) (J3 := J3) (hsl := hsl) (ev := ev) (hunm := hunm) + (fin := D hinv _) + +end + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3T3ArmHiA.lean b/proofs/Fl4ProofsA64/P3/Stage3T3ArmHiA.lean new file mode 100644 index 000000000..2f9b215a9 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3T3ArmHiA.lean @@ -0,0 +1,125 @@ +/- +P3 (aarch64, A64-P3 Stage 3, T3 step 2): **the high-window frame map at a syscall entry** (`Stage3T3ArmsA`). + +Named premises: [P] `ArchTotal`, `MaxSpi`, `ArchPlat`, `A64FrameMasks`; the arm's; `hinv` (decode site). +-/ +import Fl4ProofsA64.P3.Stage3BodyKeepA +import Fl4ProofsA64.P3.Stage3ArmFrMapHiA +import Fl4ProofsA64.P3.Stage3T3DecEqA +import Fl4ProofsA64.P3.Stage3FinA +import Fl4ProofsA64.P3.Stage3RsSiteA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +section +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 Classical + +set_option maxHeartbeats 4000000 in +/-- **Frame map into the high window**, at a syscall entry (the invocation equation from the decode-site results). -/ +theorem JSyscallA.frameMapHiA + {AR : A → HwRootA → Prop} + {Z : XSetsA} + {H : Heap} + {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} + {ia ie : Nat} + {N E Rp : Set U64} + {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} + {κ : U64 → U64} + {n : Nat} + + {s : S} + {a : A} + {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} + {x0 : U64} + {D : Set (Except SyscallError Fl4ArchInvocation × State Unit) × Prop} + {dc : capability.Cap} + {ex : Aeneas.Std.Array obj.Slot 3#usize} + {s3 : S} + {a3 : A} + {ar : machine.Arch A} + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (K : ArchKeepsA ar AR) + (hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM) + (hTO : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s q) + (hEO : ExtTabOffA (L := L) Z m s) + (hK : Aligned (L.ksb s)) + (hT : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s)) + (cte : Fl4SpecA64.CslotPtr) + (hab : AbsSlotA Z.th cte) + (fp rt asid va : U64) + (sz : capability.FrameSize) + (dev mp : Bool) + (hcap : capAtA (L.mem s) (cslotAddrA cte) = .Frame fp sz rt dev mp false asid va) + (FP : FrPlA sz fp mp asid) + (p1 asid1 : U64) + (mp1 : Bool) + (h16 : asid1.val < 2 ^ 16) + (hx0 : x0 ∈ OS) + (hx0c : capA st s x0 = .HighVSpace p1 mp1 asid1) + (vaddr rightsMask vm attr : U64) + (hvmEq : vmRightsOfA vm = maskVmRights (vmRightsOfA rt) (dataToRights rightsMask.bv)) + (hD : D = (NonDetE.liftME Fl4ArchInvocation.extVSpace + (decodeHighPageMap (Z := Unit) cte (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev + (mapDataA mp asid va)) (extRootCapA .high (specPtr p1) (asidDataA mp1 asid1)) + vaddr.bv rightsMask.bv attr.bv)).run σ) + (d : DecPre st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits U Ac s dc ex) + (hres : vspace.decode_high_page_map st ar s a (cslotAddrA cte) + { ptr := fp, size := sz, rights := rt, is_device := dev, mapped := mp, is_ept := false, asid := asid, vaddr := va } + (.HighVSpace p1 mp1 asid1) vaddr vm attr = ok (core.result.Result.Ok (), s3, a3)) + (hcov : SlotCov st OS s (cslotAddrA cte)) + (hext : ∀ x ∈ ex.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ ex.val, x ≠ 0#u64 → SlotCov st OS s x) + {callD : Bool} {lenD bufD : U64} + (hx : vspace.extra ex 0#usize = ok (some x0)) (hlen : lenD ≥ 3#u64) + (h0 : msg.syscall_arg st s (L.curW s) bufD 0#u64 = ok vaddr) + (h1 : msg.syscall_arg st s (L.curW s) bufD 1#u64 = ok rightsMask) + (h2 : msg.syscall_arg st s (L.curW s) bufD 2#u64 = ok attr) + {rr : capability.Rights} (hr : capability.Rights.from_word rightsMask = ok rr) + (hvm : capability.mask_vm_rights rt rr = ok vm) : + ∃ (x : Fl4SpecA64.Tcb) (iv : ExtVSpaceInvocation) (σ3 : Fl4SpecA64.State Unit) (Ac' : Set U64), + σ.kheap σ.curThread = some (.tcb x) ∧ + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + (.ok (.extVSpace iv), σ) ∈ D.1 ∧ + (([] : List Fl4SpecA64.Data), σ3) ∈ + (Fl4SpecA64.performExtVSpace (Z := Unit) iv (stUpdA σ σ.curThread x .restart)).1 ∧ + JStage3A st AR Z H OS U Ac' Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s3 a3 σ3 m ∧ + Sched.BodyKeepA (stUpdA σ σ.curThread x .restart) σ3 := by + have hslot : (cslotAddrA cte) ∈ OS := J.j5.2.1 _ (hab.isSlot J.j.1.place) + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hslot hcov J.actW J.running J.cur + have D : ∀ {bB bC bD bF : Bool} {len buf' : U64} {rX : core.result.Result Unit error.Exc} {sX : S} {aX : A}, + syscall.decode_invocation st ar s a fl4_abi.label.ARMPageMap len (cslotAddrA cte) (.Frame fp sz rt dev mp false asid va) bB bC bD bF buf' ex = ok (rX, sX, aX) → + ∀ σX : Fl4SpecA64.State Unit, ∃ Ac' : Set U64, SysL st H Z.Cs OS ia ie sX ∧ SymL st H Z.Cs sX ∧ + BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac' sX ∧ + Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U sX ∧ + J7A st (J7R2A st ar hPl.ipa) Z H OS sX σX m := fun hd σX => + (J.decodeFinA hA hM hPl hfm hslot hcov ((capAtA_eq (st := st) _ _).symm.trans hcap) hext hSC (lbl := fl4_abi.label.ARMPageMap) (by simp [DecArmS1aEnt, DecArmQH]) + (fun _ h => by cases h) (fun _ h => by cases h) hd σX m).2 + have hinv : syscall.decode_invocation st ar s a fl4_abi.label.ARMPageMap lenD (cslotAddrA cte) + (.Frame fp sz rt dev mp false asid va) false callD false false bufD ex = ok (core.result.Result.Ok (), s3, a3) := by + rw [decInv_frameA ar s a _ lenD _ fp sz rt dev mp false asid va false callD false false bufD ex, + decFrame_hiA hx hlen h0 h1 h2 hr hvm (by rw [capA_eq, hx0c])] + exact hres + refine JStage3A.frameMapHiA (ar := ar) (nA := hPl.ipa) (J := J.toJStage3A) (RS := RS) (hA := hA) (K := K) (hnvm := hnvm) (hTO := hTO) (hEO := hEO) (hK := hK) (hT := hT) (cte := cte) (hab := hab) (fp := fp) (rt := rt) (asid := asid) (va := va) (sz := sz) (dev := dev) (mp := mp) (hcap := hcap) (FP := FP) (p1 := p1) (asid1 := asid1) (mp1 := mp1) (h16 := h16) (hx0 := hx0) (hx0c := hx0c) (vaddr := vaddr) (rightsMask := rightsMask) (vm := vm) (attr := attr) (hvmEq := hvmEq) (hD := hD) (d := d) (hres := hres) + (fin := fun _ _ σ3 _ _ _ => D hinv σ3) + +end + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3T3ArmsA.lean b/proofs/Fl4ProofsA64/P3/Stage3T3ArmsA.lean new file mode 100644 index 000000000..73fda6d7b --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3T3ArmsA.lean @@ -0,0 +1,857 @@ +/- +P3 (aarch64, A64-P3 Stage 3, T3 step 2): **the cap-writing VSpace arms at a syscall entry**, their `fin` (or +`sysl` / `symL`) discharged by `JSyscallA.decodeFinA` and their `RsSiteA` built from the record +(`JStage3A.rsSiteA`). Each `JSyscallA.` is `JStage3A.` with the same conclusion, `J7R2A` at `hPl.ipa`. +What remains per arm is the decode site: the invoked slot's `SlotCov`, the extra slots (`hext` / `hSC`) and the arm's +own lookup facts. + +Generated from the arm signatures (WS/handoff/tools/gen_t3arms.py). + +Named premises: [P] `ArchTotal`, `MaxSpi`, `ArchPlat`, `A64FrameMasks`; those of each arm. +-/ +import Fl4ProofsA64.P3.Stage3ArmFrMapA +import Fl4ProofsA64.P3.Stage3ArmFrUnmMA +import Fl4ProofsA64.P3.Stage3ArmFrUnmXA +import Fl4ProofsA64.P3.Stage3ArmFrUnmZA +import Fl4ProofsA64.P3.Stage3ArmFrUnmA +import Fl4ProofsA64.P3.Stage3ArmPtMapA +import Fl4ProofsA64.P3.Stage3ArmPtUnmMA +import Fl4ProofsA64.P3.Stage3ArmPtUnmUA +import Fl4ProofsA64.P3.Stage3ArmPtUnmXA +import Fl4ProofsA64.P3.Stage3ArmAssignA +import Fl4ProofsA64.P3.Stage3FinA +import Fl4ProofsA64.P3.Stage3RsSiteA +import Fl4ProofsA64.P3.VsArmA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +section +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +set_option maxHeartbeats 4000000 in +/-- **Frame map, Stage 1**, at a syscall entry. -/ +theorem JSyscallA.frameMapS1A + {AR : A → HwRootA → Prop} + {Z : XSetsA} + {H : Heap} + {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} + {ia ie : Nat} + {N E Rp : Set U64} + {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} + {κ : U64 → U64} + {n : Nat} + + {s s3 : S} + {a a3 : A} + {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} + {fp asid0 va0 rt : U64} + {sz : capability.FrameSize} + {dev mp : Bool} + {ar : machine.Arch A} + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (K : ArchKeepsA ar AR) + (hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM) + (cte : Fl4SpecA64.CslotPtr) + (hab : AbsSlotA Z.th cte) + (hcap : capAtA (L.mem s) (cslotAddrA cte) = .Frame fp sz rt dev mp false asid0 va0) + (FP : FrPlA sz fp mp asid0) + (slbl : Fl4SpecA64.Data) + (hl : Fl4SpecA64.invocationType slbl = .archInvocationLabel .armPageMap) + (length buf : U64) + (args : List U64) + (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) + (call : Bool) + (extra : Aeneas.Std.Array obj.Slot 3#usize) + (x0 : U64) + (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) + (exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) + (hex : ExtraRelA Z.th (L.mem s) x0 (exs.map Prod.fst)) + (hS1 : ∀ p l mp' asid va, capAtA (L.mem s) x0 ≠ .Ept p l mp' asid va) + (hHi : ∀ p mp' asid, capAtA (L.mem s) x0 ≠ .HighVSpace p mp' asid) + (hdec : vspace.decode_frame st ar s a fl4_abi.label.ARMPageMap length (cslotAddrA cte) + (.Frame fp sz rt dev mp false asid0 va0) call buf extra = ok (core.result.Result.Ok (), s3, a3)) + (hcov : SlotCov st OS s (cslotAddrA cte)) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) : + ∃ (x : Fl4SpecA64.Tcb) (ivp : _) (σ3 : Fl4SpecA64.State Unit) (Ac' : Set U64), + σ.kheap σ.curThread = some (.tcb x) ∧ + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + (Fl4SpecA64.decodeFrameInvocation (Z := Unit) slbl (args.map UScalar.bv) cte + (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev (mapDataA mp asid0 va0)) exs).run σ = + ({(.ok (.invokePage ivp), σ)}, False) ∧ + (([] : List Fl4SpecA64.Data), σ3) ∈ (Fl4SpecA64.performPageInvocation (Z := Unit) ivp + (stUpdA σ σ.curThread x .restart)).1 ∧ + JStage3A st AR Z H OS U Ac' Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s3 a3 σ3 m ∧ + Sched.BodyKeepA (stUpdA σ σ.curThread x .restart) σ3 := by + have hslot : (cslotAddrA cte) ∈ OS := J.j5.2.1 _ (hab.isSlot J.j.1.place) + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hslot hcov J.actW J.running J.cur + have D : ∀ {bB bC bD bF : Bool} {len buf' : U64} {rX : core.result.Result Unit error.Exc} {sX : S} {aX : A}, + syscall.decode_invocation st ar s a fl4_abi.label.ARMPageMap len (cslotAddrA cte) (.Frame fp sz rt dev mp false asid0 va0) bB bC bD bF buf' extra = ok (rX, sX, aX) → + ∀ σX : Fl4SpecA64.State Unit, ∃ Ac' : Set U64, SysL st H Z.Cs OS ia ie sX ∧ SymL st H Z.Cs sX ∧ + BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac' sX ∧ + Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U sX ∧ + J7A st (J7R2A st ar hPl.ipa) Z H OS sX σX m := fun hd σX => + (J.decodeFinA hA hM hPl hfm hslot hcov ((capAtA_eq (st := st) _ _).symm.trans hcap) hext hSC (lbl := fl4_abi.label.ARMPageMap) (by simp [DecArmS1aEnt, DecArmQH]) + (fun _ h => by cases h) (fun _ h => by cases h) hd σX m).2 + refine JStage3A.frameMapS1A (ar := ar) (nA := hPl.ipa) (J := J.toJStage3A) (RS := RS) (K := K) (hnvm := hnvm) (cte := cte) (hab := hab) (hcap := hcap) (FP := FP) (slbl := slbl) (hl := hl) (length := length) (buf := buf) (args := args) (hlen := hlen) (hargs := hargs) (call := call) (extra := extra) (x0 := x0) (hx0 := hx0) (exs := exs) (hex := hex) (hS1 := hS1) (hHi := hHi) (hdec := hdec) + (fin := fun _ σ3 _ _ _ _ _ _ => D (bB := false) (bD := false) (bF := false) + ((decInv_frameA ar s a _ length _ fp sz rt dev mp false asid0 va0 false call false false buf extra).trans hdec) σ3) + +end + +section +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +set_option maxHeartbeats 4000000 in +/-- **Frame unmap** (nonzero ASID), at a syscall entry. -/ +theorem JSyscallA.frameUnmapS1A + {AR : A → HwRootA → Prop} + {Z : XSetsA} + {H : Heap} + {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} + {ia ie : Nat} + {N E Rp : Set U64} + {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} + {κ : U64 → U64} + {n : Nat} + + {s : S} + {a : A} + {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} + {ar : machine.Arch A} + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (K : ArchKeepsA ar AR) + (hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM) + (hTO : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s q) + (hK : Aligned (L.ksb s)) + (hT : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s)) + (length buf : U64) + (call : Bool) + (ex : Aeneas.Std.Array obj.Slot 3#usize) + (cte : Fl4SpecA64.CslotPtr) + (hab : AbsSlotA Z.th cte) + (fp rt asid va : U64) + (sz : capability.FrameSize) + (dev : Bool) + (h0 : asid ≠ 0#u64) + (hcap : capA st s (cslotAddrA cte) = .Frame fp sz rt dev true false asid va) + (hS1 : Fl4SpecA64.asidRootKind σ (BitVec.ofNat 16 asid.val) = none ∨ + Fl4SpecA64.asidRootKind σ (BitVec.ofNat 16 asid.val) = some .stage1) + (hcov : SlotCov st OS s (cslotAddrA cte)) + (hext : ∀ x ∈ ex.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ ex.val, x ≠ 0#u64 → SlotCov st OS s x) : + ∃ (x : Fl4SpecA64.Tcb) (s3 : S) (a' : A) (σ3 : Fl4SpecA64.State Unit) (Ac' : Set U64), + σ.kheap σ.curThread = some (.tcb x) ∧ + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + vspace.decode_frame st ar s a fl4_abi.label.ARMPageUnmap length (cslotAddrA cte) + (.Frame fp sz rt dev true false asid va) call buf ex = ok (core.result.Result.Ok (), s3, a') ∧ + (([] : List Fl4SpecA64.Data), σ3) ∈ (Fl4SpecA64.performPageInvocation (Z := Unit) + (.pageUnmap (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev + (some (BitVec.ofNat 16 asid.val, va.bv))) cte) (stUpdA σ σ.curThread x .restart)).1 ∧ + JStage3A st AR Z H OS U Ac' Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s3 a' σ3 m ∧ + Sched.BodyKeepA (stUpdA σ σ.curThread x .restart) σ3 := by + have hslot : (cslotAddrA cte) ∈ OS := J.j5.2.1 _ (hab.isSlot J.j.1.place) + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hslot hcov J.actW J.running J.cur + have D : ∀ {bB bC bD bF : Bool} {len buf' : U64} {rX : core.result.Result Unit error.Exc} {sX : S} {aX : A}, + syscall.decode_invocation st ar s a fl4_abi.label.ARMPageUnmap len (cslotAddrA cte) (.Frame fp sz rt dev true false asid va) bB bC bD bF buf' ex = ok (rX, sX, aX) → + ∀ σX : Fl4SpecA64.State Unit, ∃ Ac' : Set U64, SysL st H Z.Cs OS ia ie sX ∧ SymL st H Z.Cs sX ∧ + BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac' sX ∧ + Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U sX ∧ + J7A st (J7R2A st ar hPl.ipa) Z H OS sX σX m := fun hd σX => + (J.decodeFinA hA hM hPl hfm hslot hcov (hcap) hext hSC (lbl := fl4_abi.label.ARMPageUnmap) (by simp [DecArmS1aEnt, DecArmQH]) + (fun _ h => by cases h) (fun _ h => by cases h) hd σX m).2 + refine JStage3A.frameUnmapS1A (ar := ar) (nA := hPl.ipa) (J := J.toJStage3A) (RS := RS) (K := K) (hnvm := hnvm) (hTO := hTO) (hK := hK) (hT := hT) (length := length) (buf := buf) (call := call) (ex := ex) (cte := cte) (hab := hab) (fp := fp) (rt := rt) (asid := asid) (va := va) (sz := sz) (dev := dev) (h0 := h0) (hcap := hcap) (hS1 := hS1) + (fin := fun _ _ σ3 _ _ hd _ => D (bB := false) (bD := false) (bF := false) + ((decInv_frameA ar s a _ length _ fp sz rt dev true false asid va false call false false buf ex).trans hd) σ3) + +end + +section +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +set_option maxHeartbeats 4000000 in +/-- **Frame unmap** (nonzero ASID), at a syscall entry. -/ +theorem JSyscallA.frameUnmapXA + {AR : A → HwRootA → Prop} + {Z : XSetsA} + {H : Heap} + {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} + {ia ie : Nat} + {N E Rp : Set U64} + {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} + {κ : U64 → U64} + {n : Nat} + + {s : S} + {a : A} + {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} + {ar : machine.Arch A} + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (K : ArchKeepsA ar AR) + (hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM) + (hTO : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s q) + (hEO : ExtTabOffA (L := L) Z m s) + (hK : Aligned (L.ksb s)) + (hT : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s)) + (hIpa : S2IpaE ar a) + (length buf : U64) + (call : Bool) + (ex : Aeneas.Std.Array obj.Slot 3#usize) + (cte : Fl4SpecA64.CslotPtr) + (hab : AbsSlotA Z.th cte) + (fp rt asid va : U64) + (sz : capability.FrameSize) + (dev isEpt : Bool) + (h0 : asid ≠ 0#u64) + (hcap : capA st s (cslotAddrA cte) = .Frame fp sz rt dev true isEpt asid va) + (k : Fl4SpecA64.ExtRootKind) + (hkA : isEpt = false → Fl4SpecA64.asidRootKind σ (BitVec.ofNat 16 asid.val) = some k ∧ k ≠ .stage1) + (hcov : SlotCov st OS s (cslotAddrA cte)) + (hext : ∀ x ∈ ex.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ ex.val, x ≠ 0#u64 → SlotCov st OS s x) : + ∃ (x : Fl4SpecA64.Tcb) (s3 : S) (a' : A) (σ3 : Fl4SpecA64.State Unit) (Ac' : Set U64), + σ.kheap σ.curThread = some (.tcb x) ∧ + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + vspace.decode_frame st ar s a fl4_abi.label.ARMPageUnmap length (cslotAddrA cte) + (.Frame fp sz rt dev true isEpt asid va) call buf ex = ok (core.result.Result.Ok (), s3, a') ∧ + (([] : List Fl4SpecA64.Data), σ3) ∈ (Fl4SpecA64.performExtVSpace (Z := Unit) (.pageUnmap k + (absFrameE isEpt (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev + (some (BitVec.ofNat 16 asid.val, va.bv))) cte) (stUpdA σ σ.curThread x .restart)).1 ∧ + JStage3A st AR Z H OS U Ac' Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s3 a' σ3 m ∧ + Sched.BodyKeepA (stUpdA σ σ.curThread x .restart) σ3 := by + have hslot : (cslotAddrA cte) ∈ OS := J.j5.2.1 _ (hab.isSlot J.j.1.place) + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hslot hcov J.actW J.running J.cur + have D : ∀ {bB bC bD bF : Bool} {len buf' : U64} {rX : core.result.Result Unit error.Exc} {sX : S} {aX : A}, + syscall.decode_invocation st ar s a fl4_abi.label.ARMPageUnmap len (cslotAddrA cte) (.Frame fp sz rt dev true isEpt asid va) bB bC bD bF buf' ex = ok (rX, sX, aX) → + ∀ σX : Fl4SpecA64.State Unit, ∃ Ac' : Set U64, SysL st H Z.Cs OS ia ie sX ∧ SymL st H Z.Cs sX ∧ + BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac' sX ∧ + Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U sX ∧ + J7A st (J7R2A st ar hPl.ipa) Z H OS sX σX m := fun hd σX => + (J.decodeFinA hA hM hPl hfm hslot hcov (hcap) hext hSC (lbl := fl4_abi.label.ARMPageUnmap) (by simp [DecArmS1aEnt, DecArmQH]) + (fun _ h => by cases h) (fun _ h => by cases h) hd σX m).2 + refine JStage3A.frameUnmapXA (ar := ar) (nA := hPl.ipa) (J := J.toJStage3A) (RS := RS) (K := K) (hnvm := hnvm) (hTO := hTO) (hEO := hEO) (hK := hK) (hT := hT) (hIpa := hIpa) (length := length) (buf := buf) (call := call) (ex := ex) (cte := cte) (hab := hab) (fp := fp) (rt := rt) (asid := asid) (va := va) (sz := sz) (dev := dev) (isEpt := isEpt) (h0 := h0) (hcap := hcap) (k := k) (hkA := hkA) + (fin := fun _ _ σ3 _ _ hd _ => D (bB := false) (bD := false) (bF := false) + ((decInv_frameA ar s a _ length _ fp sz rt dev true isEpt asid va false call false false buf ex).trans hd) σ3) + +end + +section +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +set_option maxHeartbeats 4000000 in +/-- **Frame unmap with ASID 0**, at a syscall entry. -/ +theorem JSyscallA.frameUnmapZA + {AR : A → HwRootA → Prop} + {Z : XSetsA} + {H : Heap} + {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} + {ia ie : Nat} + {N E Rp : Set U64} + {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} + {κ : U64 → U64} + {n : Nat} + + {s : S} + {a : A} + {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} + {ar : machine.Arch A} + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM) + (length buf : U64) + (call : Bool) + (ex : Aeneas.Std.Array obj.Slot 3#usize) + (cte : Fl4SpecA64.CslotPtr) + (hab : AbsSlotA Z.th cte) + (fp rt va : U64) + (sz : capability.FrameSize) + (dev : Bool) + (hcap : capA st s (cslotAddrA cte) = .Frame fp sz rt dev true false 0#u64 va) + (hcov : SlotCov st OS s (cslotAddrA cte)) + (hext : ∀ x ∈ ex.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ ex.val, x ≠ 0#u64 → SlotCov st OS s x) : + ∃ (x : Fl4SpecA64.Tcb) (s3 : S) (σ3 : Fl4SpecA64.State Unit), σ.kheap σ.curThread = some (.tcb x) ∧ + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + vspace.decode_frame st ar s a fl4_abi.label.ARMPageUnmap length (cslotAddrA cte) + (.Frame fp sz rt dev true false 0#u64 va) call buf ex = ok (core.result.Result.Ok (), s3, a) ∧ + (([] : List Fl4SpecA64.Data), σ3) ∈ (Fl4SpecA64.performPageInvocation (Z := Unit) + (.pageUnmap (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev + (some (BitVec.ofNat 16 (0#u64 : U64).val, va.bv))) cte) (stUpdA σ σ.curThread x .restart)).1 ∧ + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s3 a σ3 m ∧ + Sched.BodyKeepA (stUpdA σ σ.curThread x .restart) σ3 := by + have hslot : (cslotAddrA cte) ∈ OS := J.j5.2.1 _ (hab.isSlot J.j.1.place) + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hslot hcov J.actW J.running J.cur + have D : ∀ {bB bC bD bF : Bool} {len buf' : U64} {rX : core.result.Result Unit error.Exc} {sX : S} {aX : A}, + syscall.decode_invocation st ar s a fl4_abi.label.ARMPageUnmap len (cslotAddrA cte) (.Frame fp sz rt dev true false 0#u64 va) bB bC bD bF buf' ex = ok (rX, sX, aX) → + ∀ σX : Fl4SpecA64.State Unit, ∃ Ac' : Set U64, SysL st H Z.Cs OS ia ie sX ∧ SymL st H Z.Cs sX ∧ + BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac' sX ∧ + Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U sX ∧ + J7A st (J7R2A st ar hPl.ipa) Z H OS sX σX m := fun hd σX => + (J.decodeFinA hA hM hPl hfm hslot hcov (hcap) hext hSC (lbl := fl4_abi.label.ARMPageUnmap) (by simp [DecArmS1aEnt, DecArmQH]) + (fun _ h => by cases h) (fun _ h => by cases h) hd σX m).2 + refine JStage3A.frameUnmapZA (ar := ar) (nA := hPl.ipa) (J := J.toJStage3A) (RS := RS) (hnvm := hnvm) (length := length) (buf := buf) (call := call) (ex := ex) (cte := cte) (hab := hab) (fp := fp) (rt := rt) (va := va) (sz := sz) (dev := dev) (hcap := hcap) + (fin := fun _ s3 σ3 _ hset _ => ?_) + obtain ⟨x0, -, heq, -⟩ := J.toJStage3A.of_restartCurA RS.pl.choose_spec.choose_spec.choose_spec.choose_spec + RS.sym RS.run hnvm RS.cur RS.vs RS.vc + have hd : vspace.decode_frame st ar s a fl4_abi.label.ARMPageUnmap length (cslotAddrA cte) (.Frame fp sz rt dev true false 0#u64 va) call buf ex = + ok (core.result.Result.Ok (), s3, a) := by + rw [page_unmap0_eqA ar s a length (cslotAddrA cte) fp rt va sz dev call buf ex, heq, bind_ok, hset, bind_ok] + obtain ⟨_, S1, Y, -, -, C⟩ := D (bB := false) (bD := false) (bF := false) + ((decInv_frameA ar s a _ length _ fp sz rt dev true false 0#u64 va false call false false buf ex).trans hd) σ3 + exact ⟨S1, Y, C⟩ + +end + +section +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +set_option maxHeartbeats 4000000 in +/-- **Frame unmap with ASID 0**, at a syscall entry. -/ +theorem JSyscallA.frameUnmapS2ZA + {AR : A → HwRootA → Prop} + {Z : XSetsA} + {H : Heap} + {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} + {ia ie : Nat} + {N E Rp : Set U64} + {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} + {κ : U64 → U64} + {n : Nat} + + {s : S} + {a : A} + {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} + {ar : machine.Arch A} + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM) + (length buf : U64) + (call : Bool) + (ex : Aeneas.Std.Array obj.Slot 3#usize) + (cte : Fl4SpecA64.CslotPtr) + (hab : AbsSlotA Z.th cte) + (fp rt va : U64) + (sz : capability.FrameSize) + (dev : Bool) + (hcap : capA st s (cslotAddrA cte) = .Frame fp sz rt dev true true 0#u64 va) + (hcov : SlotCov st OS s (cslotAddrA cte)) + (hext : ∀ x ∈ ex.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ ex.val, x ≠ 0#u64 → SlotCov st OS s x) : + ∃ (x : Fl4SpecA64.Tcb) (s3 : S) (σ3 : Fl4SpecA64.State Unit), σ.kheap σ.curThread = some (.tcb x) ∧ + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + vspace.decode_frame st ar s a fl4_abi.label.ARMPageUnmap length (cslotAddrA cte) + (.Frame fp sz rt dev true true 0#u64 va) call buf ex = ok (core.result.Result.Ok (), s3, a) ∧ + (([] : List Fl4SpecA64.Data), σ3) ∈ (Fl4SpecA64.performExtVSpace (Z := Unit) + (.pageUnmap .stage2 (.ext (.stage2FrameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev + (some (BitVec.ofNat 16 (0#u64 : U64).val, va.bv)))) cte) (stUpdA σ σ.curThread x .restart)).1 ∧ + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s3 a σ3 m ∧ + Sched.BodyKeepA (stUpdA σ σ.curThread x .restart) σ3 := by + have hslot : (cslotAddrA cte) ∈ OS := J.j5.2.1 _ (hab.isSlot J.j.1.place) + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hslot hcov J.actW J.running J.cur + have D : ∀ {bB bC bD bF : Bool} {len buf' : U64} {rX : core.result.Result Unit error.Exc} {sX : S} {aX : A}, + syscall.decode_invocation st ar s a fl4_abi.label.ARMPageUnmap len (cslotAddrA cte) (.Frame fp sz rt dev true true 0#u64 va) bB bC bD bF buf' ex = ok (rX, sX, aX) → + ∀ σX : Fl4SpecA64.State Unit, ∃ Ac' : Set U64, SysL st H Z.Cs OS ia ie sX ∧ SymL st H Z.Cs sX ∧ + BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac' sX ∧ + Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U sX ∧ + J7A st (J7R2A st ar hPl.ipa) Z H OS sX σX m := fun hd σX => + (J.decodeFinA hA hM hPl hfm hslot hcov (hcap) hext hSC (lbl := fl4_abi.label.ARMPageUnmap) (by simp [DecArmS1aEnt, DecArmQH]) + (fun _ h => by cases h) (fun _ h => by cases h) hd σX m).2 + refine JStage3A.frameUnmapS2ZA (ar := ar) (nA := hPl.ipa) (J := J.toJStage3A) (RS := RS) (hnvm := hnvm) (length := length) (buf := buf) (call := call) (ex := ex) (cte := cte) (hab := hab) (fp := fp) (rt := rt) (va := va) (sz := sz) (dev := dev) (hcap := hcap) + (fin := fun _ s3 σ3 _ hset _ => ?_) + obtain ⟨x0, -, heq, -⟩ := J.toJStage3A.of_restartCurA RS.pl.choose_spec.choose_spec.choose_spec.choose_spec + RS.sym RS.run hnvm RS.cur RS.vs RS.vc + have hd : vspace.decode_frame st ar s a fl4_abi.label.ARMPageUnmap length (cslotAddrA cte) (.Frame fp sz rt dev true true 0#u64 va) call buf ex = + ok (core.result.Result.Ok (), s3, a) := by + rw [page_unmapS20_eqA ar s a length (cslotAddrA cte) fp rt va sz dev call buf ex, heq, bind_ok, hset, bind_ok] + obtain ⟨_, S1, Y, -, -, C⟩ := D (bB := false) (bD := false) (bF := false) + ((decInv_frameA ar s a _ length _ fp sz rt dev true true 0#u64 va false call false false buf ex).trans hd) σ3 + exact ⟨S1, Y, C⟩ + +end + +section +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +set_option maxHeartbeats 4000000 in +/-- **Unmap of an unmapped frame**, at a syscall entry. -/ +theorem JSyscallA.frameUnmapUA + {AR : A → HwRootA → Prop} + {Z : XSetsA} + {H : Heap} + {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} + {ia ie : Nat} + {N E Rp : Set U64} + {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} + {κ : U64 → U64} + {n : Nat} + + {s : S} + {a : A} + {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} + {ar : machine.Arch A} + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM) + (length buf : U64) + (call : Bool) + (ex : Aeneas.Std.Array obj.Slot 3#usize) + (cte : Fl4SpecA64.CslotPtr) + (hab : AbsSlotA Z.th cte) + (fp rt asid va : U64) + (sz : capability.FrameSize) + (dev isEpt : Bool) + (hcap : capA st s (cslotAddrA cte) = .Frame fp sz rt dev false isEpt asid va) + (hcov : SlotCov st OS s (cslotAddrA cte)) + (hext : ∀ x ∈ ex.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ ex.val, x ≠ 0#u64 → SlotCov st OS s x) : + ∃ x s2 σ2, σ.kheap σ.curThread = some (.tcb x) ∧ + vspace.decode_frame st ar s a fl4_abi.label.ARMPageUnmap length (cslotAddrA cte) + (.Frame fp sz rt dev false isEpt asid va) call buf ex = ok (core.result.Result.Ok (), s2, a) ∧ + ((), σ2) ∈ (setCap (Z := Unit) (.archObjectCap (.frameCap (specPtr fp) (vmRightsOfA rt) + (frameSizeOfA sz) dev none)) cte (stUpdA σ σ.curThread x .restart)).1 ∧ + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s2 a σ2 m ∧ + Sched.BodyKeepA (stUpdA σ σ.curThread x .restart) σ2 := by + have hslot : (cslotAddrA cte) ∈ OS := J.j5.2.1 _ (hab.isSlot J.j.1.place) + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hslot hcov J.actW J.running J.cur + have D : ∀ {bB bC bD bF : Bool} {len buf' : U64} {rX : core.result.Result Unit error.Exc} {sX : S} {aX : A}, + syscall.decode_invocation st ar s a fl4_abi.label.ARMPageUnmap len (cslotAddrA cte) (.Frame fp sz rt dev false isEpt asid va) bB bC bD bF buf' ex = ok (rX, sX, aX) → + ∀ σX : Fl4SpecA64.State Unit, ∃ Ac' : Set U64, SysL st H Z.Cs OS ia ie sX ∧ SymL st H Z.Cs sX ∧ + BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac' sX ∧ + Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U sX ∧ + J7A st (J7R2A st ar hPl.ipa) Z H OS sX σX m := fun hd σX => + (J.decodeFinA hA hM hPl hfm hslot hcov (hcap) hext hSC (lbl := fl4_abi.label.ARMPageUnmap) (by simp [DecArmS1aEnt, DecArmQH]) + (fun _ h => by cases h) (fun _ h => by cases h) hd σX m).2 + refine JStage3A.frameUnmapUA (ar := ar) (nA := hPl.ipa) (J := J.toJStage3A) (RS := RS) (hnvm := hnvm) (length := length) (buf := buf) (call := call) (ex := ex) (cte := cte) (hab := hab) (fp := fp) (rt := rt) (asid := asid) (va := va) (sz := sz) (dev := dev) (isEpt := isEpt) (hcap := hcap) + (symL := fun s2 hs2 => ?_) (sysl := fun s2 hs2 => ?_) + all_goals + obtain ⟨x0, -, heq, -⟩ := J.toJStage3A.of_restartCurA RS.pl.choose_spec.choose_spec.choose_spec.choose_spec + RS.sym RS.run hnvm RS.cur RS.vs RS.vc + have hd : vspace.decode_frame st ar s a fl4_abi.label.ARMPageUnmap length (cslotAddrA cte) (.Frame fp sz rt dev false isEpt asid va) call + buf ex = ok (core.result.Result.Ok (), s2, a) := by + rw [page_unmapU_eqA ar s a length (cslotAddrA cte) fp rt asid va sz dev isEpt call buf ex, heq, bind_ok, hs2, + bind_ok] + obtain ⟨_, S1, Y, -⟩ := D (bB := false) (bD := false) (bF := false) + ((decInv_frameA ar s a _ length _ fp sz rt dev false isEpt asid va false call false false buf ex).trans hd) σ + · exact Y + · exact S1 + +end + +section +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +set_option maxHeartbeats 4000000 in +/-- **Page-table map, Stage 1**, at a syscall entry. -/ +theorem JSyscallA.ptMapS1A + {AR : A → HwRootA → Prop} + {Z : XSetsA} + {H : Heap} + {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} + {ia ie : Nat} + {N E Rp : Set U64} + {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} + {κ : U64 → U64} + {n : Nat} + + {s s3 : S} + {a a3 : A} + {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} + {ptr asid0 va0 : U64} + {ar : machine.Arch A} + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM) + (cte : Fl4SpecA64.CslotPtr) + (hab : AbsSlotA Z.th cte) + (hcap : capAtA (L.mem s) (cslotAddrA cte) = .PageTable ptr false asid0 va0) + (UP : UnmPtA Z.S1 Z.Y OS H.tcbs (L.mem s) σ ptr) + (hS2 : ptr ∉ S2PgE Z.X Z.T2) + (hY : ptr ∉ Z.Y) + (slbl : Fl4SpecA64.Data) + (hl : Fl4SpecA64.invocationType slbl = .archInvocationLabel .armPageTableMap) + (length buf : U64) + (args : List U64) + (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) + (extra : Aeneas.Std.Array obj.Slot 3#usize) + (x0 : U64) + (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) + (exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) + (hex : ExtraRelA Z.th (L.mem s) x0 (exs.map Prod.fst)) + (hS1 : ∀ p l mp' asid va, capAtA (L.mem s) x0 ≠ .Ept p l mp' asid va) + (hHi : ∀ p mp' asid, capAtA (L.mem s) x0 ≠ .HighVSpace p mp' asid) + (hdec : vspace.decode_page_table st ar s a fl4_abi.label.ARMPageTableMap length (cslotAddrA cte) + (.PageTable ptr false asid0 va0) buf extra = ok (core.result.Result.Ok (), s3, a3)) + (hcov : SlotCov st OS s (cslotAddrA cte)) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) : + ∃ (x : Fl4SpecA64.Tcb) (ivp : Fl4SpecA64.PageTableInvocation) (σ3 : Fl4SpecA64.State Unit) + (Ac' Tb' : Set U64) (Lv' : U64 → Nat) (nL : Nat), + σ.kheap σ.curThread = some (.tcb x) ∧ + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + (Fl4SpecA64.decodePageTableInvocation (Z := Unit) slbl (args.map UScalar.bv) cte + (.pageTableCap (specPtr ptr) .normalPtT (mapDataA false asid0 va0)) exs).run σ = + ({(.ok (.invokePageTable ivp), σ)}, False) ∧ + ((), σ3) ∈ (Fl4SpecA64.performPageTableInvocation (Z := Unit) ivp (stUpdA σ σ.curThread x .restart)).1 ∧ + JStage3A st AR { Z with S1 := insert ptr Z.S1, Lv := Function.update Z.Lv ptr nL } H OS U Ac' Pp Tb' Lv' ia ie N E + Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s3 a3 σ3 m ∧ + Sched.BodyKeepA (stUpdA σ σ.curThread x .restart) σ3 := by + have hslot : (cslotAddrA cte) ∈ OS := J.j5.2.1 _ (hab.isSlot J.j.1.place) + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hslot hcov J.actW J.running J.cur + have D : ∀ {bB bC bD bF : Bool} {len buf' : U64} {rX : core.result.Result Unit error.Exc} {sX : S} {aX : A}, + syscall.decode_invocation st ar s a fl4_abi.label.ARMPageTableMap len (cslotAddrA cte) (.PageTable ptr false asid0 va0) bB bC bD bF buf' extra = ok (rX, sX, aX) → + ∀ σX : Fl4SpecA64.State Unit, ∃ Ac' : Set U64, SysL st H Z.Cs OS ia ie sX ∧ SymL st H Z.Cs sX ∧ + BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac' sX ∧ + Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U sX ∧ + J7A st (J7R2A st ar hPl.ipa) Z H OS sX σX m := fun hd σX => + (J.decodeFinA hA hM hPl hfm hslot hcov ((capAtA_eq (st := st) _ _).symm.trans hcap) hext hSC (lbl := fl4_abi.label.ARMPageTableMap) (by simp [DecArmS1aEnt, DecArmQH]) + (fun _ h => by cases h) (fun _ h => by cases h) hd σX m).2 + refine JStage3A.ptMapS1A (ar := ar) (nA := hPl.ipa) (J := J.toJStage3A) (RS := RS) (hnvm := hnvm) (cte := cte) (hab := hab) (hcap := hcap) (UP := UP) (hS2 := hS2) (hY := hY) (slbl := slbl) (hl := hl) (length := length) (buf := buf) (args := args) (hlen := hlen) (hargs := hargs) (extra := extra) (x0 := x0) (hx0 := hx0) (exs := exs) (hex := hex) (hS1 := hS1) (hHi := hHi) (hdec := hdec) + (fin := fun _ σ3 _ _ _ _ => D (bB := false) (bC := false) (bD := false) (bF := false) + ((decInv_ptA ar s a _ length _ ptr false asid0 va0 false false false false buf extra).trans hdec) σ3) + +end + +section +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +set_option maxHeartbeats 4000000 in +/-- **Page-table unmap**, at a syscall entry. -/ +theorem JSyscallA.ptUnmapS1A + {AR : A → HwRootA → Prop} + {Z : XSetsA} + {H : Heap} + {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} + {ia ie : Nat} + {N E Rp : Set U64} + {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} + {κ : U64 → U64} + {n : Nat} + + {s : S} + {a : A} + {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} + {ar : machine.Arch A} + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (K : ArchKeepsA ar AR) + (hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM) + (hTO : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s q) + (hEO : ExtTabOffA (L := L) Z m s) + (hK : Aligned (L.ksb s)) + (hT : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s)) + (length buf : U64) + (ex : Aeneas.Std.Array obj.Slot 3#usize) + (cte : Fl4SpecA64.CslotPtr) + (hab : AbsSlotA Z.th cte) + (ptr asid va : U64) + (hpS : ptr ∈ Z.S1 ∧ 1 ≤ Z.Lv ptr) + (hcap : capA st s (cslotAddrA cte) = .PageTable ptr true asid va) + (hf : cdt.is_final_capability st s (cslotAddrA cte) = ok true) + (hS1 : Fl4SpecA64.asidRootKind σ (BitVec.ofNat 16 asid.val) = none ∨ + Fl4SpecA64.asidRootKind σ (BitVec.ofNat 16 asid.val) = some .stage1) + (hcov : SlotCov st OS s (cslotAddrA cte)) + (hext : ∀ x ∈ ex.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ ex.val, x ≠ 0#u64 → SlotCov st OS s x) : + ∃ (x : Fl4SpecA64.Tcb) (s3 : S) (a' : A) (σ3 : Fl4SpecA64.State Unit) (Ac' : Set U64), + σ.kheap σ.curThread = some (.tcb x) ∧ + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + vspace.decode_page_table st ar s a fl4_abi.label.ARMPageTableUnmap length (cslotAddrA cte) + (.PageTable ptr true asid va) buf ex = ok (core.result.Result.Ok (), s3, a') ∧ + ((), σ3) ∈ (Fl4SpecA64.performPageTableInvocation (Z := Unit) (.pageTableUnmap (.pageTableCap (specPtr ptr) + .normalPtT (some (BitVec.ofNat 16 asid.val, va.bv))) cte) (stUpdA σ σ.curThread x .restart)).1 ∧ + JStage3A st AR Z H OS U Ac' Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s3 a' σ3 m ∧ + Sched.BodyKeepA (stUpdA σ σ.curThread x .restart) σ3 := by + have hslot : (cslotAddrA cte) ∈ OS := J.j5.2.1 _ (hab.isSlot J.j.1.place) + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hslot hcov J.actW J.running J.cur + have D : ∀ {bB bC bD bF : Bool} {len buf' : U64} {rX : core.result.Result Unit error.Exc} {sX : S} {aX : A}, + syscall.decode_invocation st ar s a fl4_abi.label.ARMPageTableUnmap len (cslotAddrA cte) (.PageTable ptr true asid va) bB bC bD bF buf' ex = ok (rX, sX, aX) → + ∀ σX : Fl4SpecA64.State Unit, ∃ Ac' : Set U64, SysL st H Z.Cs OS ia ie sX ∧ SymL st H Z.Cs sX ∧ + BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac' sX ∧ + Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U sX ∧ + J7A st (J7R2A st ar hPl.ipa) Z H OS sX σX m := fun hd σX => + (J.decodeFinA hA hM hPl hfm hslot hcov (hcap) hext hSC (lbl := fl4_abi.label.ARMPageTableUnmap) (by simp [DecArmS1aEnt, DecArmQH]) + (fun _ h => by cases h) (fun _ h => by cases h) hd σX m).2 + refine JStage3A.ptUnmapS1A (ar := ar) (nA := hPl.ipa) (J := J.toJStage3A) (RS := RS) (K := K) (hnvm := hnvm) (hTO := hTO) (hEO := hEO) (hK := hK) (hT := hT) (length := length) (buf := buf) (ex := ex) (cte := cte) (hab := hab) (ptr := ptr) (asid := asid) (va := va) (hpS := hpS) (hcap := hcap) (hf := hf) (hS1 := hS1) + (fin := fun _ _ σ3 _ _ hd _ => D (bB := false) (bC := false) (bD := false) (bF := false) + ((decInv_ptA ar s a _ length _ ptr true asid va false false false false buf ex).trans hd) σ3) + +end + +section +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +set_option maxHeartbeats 4000000 in +/-- **Page-table unmap**, at a syscall entry. -/ +theorem JSyscallA.ptUnmapXA + {AR : A → HwRootA → Prop} + {Z : XSetsA} + {H : Heap} + {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} + {ia ie : Nat} + {N E Rp : Set U64} + {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} + {κ : U64 → U64} + {n : Nat} + + {s : S} + {a : A} + {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} + {ar : machine.Arch A} + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (K : ArchKeepsA ar AR) + (hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM) + (hTO : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s q) + (hEO : ExtTabOffA (L := L) Z m s) + (hK : Aligned (L.ksb s)) + (hT : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s)) + (hIpa : S2IpaE ar a) + (length buf : U64) + (ex : Aeneas.Std.Array obj.Slot 3#usize) + (cte : Fl4SpecA64.CslotPtr) + (hab : AbsSlotA Z.th cte) + (ptr asid va : U64) + (hpS : ptr ∈ Z.S1 ∧ 1 ≤ Z.Lv ptr) + (hcap : capA st s (cslotAddrA cte) = .PageTable ptr true asid va) + (hf : cdt.is_final_capability st s (cslotAddrA cte) = ok true) + (k : Fl4SpecA64.ExtRootKind) + (hkR : Fl4SpecA64.asidRootKind σ (BitVec.ofNat 16 asid.val) = some k) + (hkR1 : k ≠ .stage1) + (hcov : SlotCov st OS s (cslotAddrA cte)) + (hext : ∀ x ∈ ex.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ ex.val, x ≠ 0#u64 → SlotCov st OS s x) : + ∃ (x : Fl4SpecA64.Tcb) (s3 : S) (a' : A) (σ3 : Fl4SpecA64.State Unit) (Ac' : Set U64), + σ.kheap σ.curThread = some (.tcb x) ∧ + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + vspace.decode_page_table st ar s a fl4_abi.label.ARMPageTableUnmap length (cslotAddrA cte) + (.PageTable ptr true asid va) buf ex = ok (core.result.Result.Ok (), s3, a') ∧ + (([] : List Fl4SpecA64.Data), σ3) ∈ (Fl4SpecA64.performExtVSpace (Z := Unit) (.tableUnmap k (.pageTableCap + (specPtr ptr) .normalPtT (some (BitVec.ofNat 16 asid.val, va.bv))) cte) (stUpdA σ σ.curThread x .restart)).1 ∧ + JStage3A st AR Z H OS U Ac' Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s3 a' σ3 m ∧ + Sched.BodyKeepA (stUpdA σ σ.curThread x .restart) σ3 := by + have hslot : (cslotAddrA cte) ∈ OS := J.j5.2.1 _ (hab.isSlot J.j.1.place) + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hslot hcov J.actW J.running J.cur + have D : ∀ {bB bC bD bF : Bool} {len buf' : U64} {rX : core.result.Result Unit error.Exc} {sX : S} {aX : A}, + syscall.decode_invocation st ar s a fl4_abi.label.ARMPageTableUnmap len (cslotAddrA cte) (.PageTable ptr true asid va) bB bC bD bF buf' ex = ok (rX, sX, aX) → + ∀ σX : Fl4SpecA64.State Unit, ∃ Ac' : Set U64, SysL st H Z.Cs OS ia ie sX ∧ SymL st H Z.Cs sX ∧ + BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac' sX ∧ + Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U sX ∧ + J7A st (J7R2A st ar hPl.ipa) Z H OS sX σX m := fun hd σX => + (J.decodeFinA hA hM hPl hfm hslot hcov (hcap) hext hSC (lbl := fl4_abi.label.ARMPageTableUnmap) (by simp [DecArmS1aEnt, DecArmQH]) + (fun _ h => by cases h) (fun _ h => by cases h) hd σX m).2 + refine JStage3A.ptUnmapXA (ar := ar) (nA := hPl.ipa) (J := J.toJStage3A) (RS := RS) (K := K) (hnvm := hnvm) (hTO := hTO) (hEO := hEO) (hK := hK) (hT := hT) (hIpa := hIpa) (length := length) (buf := buf) (ex := ex) (cte := cte) (hab := hab) (ptr := ptr) (asid := asid) (va := va) (hpS := hpS) (hcap := hcap) (hf := hf) (k := k) (hkR := hkR) (hkR1 := hkR1) + (fin := fun _ _ σ3 _ _ hd _ => D (bB := false) (bC := false) (bD := false) (bF := false) + ((decInv_ptA ar s a _ length _ ptr true asid va false false false false buf ex).trans hd) σ3) + +end + +section +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +set_option maxHeartbeats 4000000 in +/-- **Unmap of an unmapped page table**, at a syscall entry. -/ +theorem JSyscallA.ptUnmapUA + {AR : A → HwRootA → Prop} + {Z : XSetsA} + {H : Heap} + {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} + {ia ie : Nat} + {N E Rp : Set U64} + {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} + {κ : U64 → U64} + {n : Nat} + + {s : S} + {a : A} + {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} + {ar : machine.Arch A} + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM) + (length buf : U64) + (ex : Aeneas.Std.Array obj.Slot 3#usize) + (cte : Fl4SpecA64.CslotPtr) + (hab : AbsSlotA Z.th cte) + (ptr asid va : U64) + (hcap : capA st s (cslotAddrA cte) = .PageTable ptr false asid va) + (hf : cdt.is_final_capability st s (cslotAddrA cte) = ok true) + (hcov : SlotCov st OS s (cslotAddrA cte)) + (hext : ∀ x ∈ ex.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ ex.val, x ≠ 0#u64 → SlotCov st OS s x) : + ∃ x s2 σ2, σ.kheap σ.curThread = some (.tcb x) ∧ + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + vspace.decode_page_table st ar s a fl4_abi.label.ARMPageTableUnmap length (cslotAddrA cte) + (.PageTable ptr false asid va) buf ex = ok (core.result.Result.Ok (), s2, a) ∧ + ((), σ2) ∈ (Fl4SpecA64.performPageTableInvocation (Z := Unit) + (.pageTableUnmap (.pageTableCap (specPtr ptr) .normalPtT none) cte) (stUpdA σ σ.curThread x .restart)).1 ∧ + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s2 a σ2 m ∧ + Sched.BodyKeepA (stUpdA σ σ.curThread x .restart) σ2 := by + have hslot : (cslotAddrA cte) ∈ OS := J.j5.2.1 _ (hab.isSlot J.j.1.place) + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hslot hcov J.actW J.running J.cur + have D : ∀ {bB bC bD bF : Bool} {len buf' : U64} {rX : core.result.Result Unit error.Exc} {sX : S} {aX : A}, + syscall.decode_invocation st ar s a fl4_abi.label.ARMPageTableUnmap len (cslotAddrA cte) (.PageTable ptr false asid va) bB bC bD bF buf' ex = ok (rX, sX, aX) → + ∀ σX : Fl4SpecA64.State Unit, ∃ Ac' : Set U64, SysL st H Z.Cs OS ia ie sX ∧ SymL st H Z.Cs sX ∧ + BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac' sX ∧ + Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U sX ∧ + J7A st (J7R2A st ar hPl.ipa) Z H OS sX σX m := fun hd σX => + (J.decodeFinA hA hM hPl hfm hslot hcov (hcap) hext hSC (lbl := fl4_abi.label.ARMPageTableUnmap) (by simp [DecArmS1aEnt, DecArmQH]) + (fun _ h => by cases h) (fun _ h => by cases h) hd σX m).2 + refine JStage3A.ptUnmapUA (ar := ar) (nA := hPl.ipa) (J := J.toJStage3A) (RS := RS) (hnvm := hnvm) (length := length) (buf := buf) (ex := ex) (cte := cte) (hab := hab) (ptr := ptr) (asid := asid) (va := va) (hcap := hcap) (hf := hf) + (fin := fun s2 σ2 hs2 => ?_) + obtain ⟨x0, -, heq, -⟩ := J.toJStage3A.of_restartCurA RS.pl.choose_spec.choose_spec.choose_spec.choose_spec + RS.sym RS.run hnvm RS.cur RS.vs RS.vc + have hd : vspace.decode_page_table st ar s a fl4_abi.label.ARMPageTableUnmap length (cslotAddrA cte) (.PageTable ptr false asid va) buf ex = + ok (core.result.Result.Ok (), s2, a) := by + rw [pt_unmapU_eqA ar s a length (cslotAddrA cte) ptr asid va buf ex hf, heq, bind_ok, hs2, bind_ok] + obtain ⟨_, S1, Y, -, -, C⟩ := D (bB := false) (bC := false) (bD := false) (bF := false) + ((decInv_ptA ar s a _ length _ ptr false asid va false false false false buf ex).trans hd) σ2 + exact ⟨S1, Y, C⟩ + +end + +section +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 Classical + +set_option maxHeartbeats 4000000 in +/-- **ASIDPool Assign**, at a syscall entry. -/ +theorem JSyscallA.asidAssignA + {AR : A → HwRootA → Prop} + {Z : XSetsA} + {H : Heap} + {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} + {ia ie : Nat} + {N E Rp : Set U64} + {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} + {κ : U64 → U64} + {n : Nat} + + {s : S} + {a : A} + {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} + {ar : machine.Arch A} + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM) + (cte : Fl4SpecA64.CslotPtr) + (hab : AbsSlotA Z.th cte) + (p base : U64) + (hpool : capA st s (cslotAddrA cte) = .AsidPool p base) + (length buf : U64) + (block call cd fp : Bool) + (ex : Aeneas.Std.Array obj.Slot 3#usize) + (dst : Fl4SpecA64.CslotPtr) + (hd : AbsSlotA Z.th dst) + (hex : vspace.extra ex 0#usize = ok (some (cslotAddrA dst))) + (k : Fl4SpecA64.ExtRootKind) + (root oldAsid oldVa : U64) + (hcap : capA st s (cslotAddrA dst) = assignUnmappedA k root oldAsid oldVa) + (htab : ∀ hi : U64, hi.val = base.val / 512 → L.mem s (atAF (L.ksb s) hi) = p) + (hp0 : p ≠ 0#u64) + (i : U64) + (hsearch : vspace.decode_loop1 st s base p 0#u64 = ok i) + (hfull : i ≠ 512#u64) + (hcov : SlotCov st OS s (cslotAddrA cte)) + (hext : ∀ x ∈ ex.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ ex.val, x ≠ 0#u64 → SlotCov st OS s x) : + ∃ (x : Fl4SpecA64.Tcb) (s3 : S) (σ3 : Fl4SpecA64.State Unit) (Ac' Tb' : Set U64) (Lv' : U64 → Nat), + σ.kheap σ.curThread = some (.tcb x) ∧ + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + syscall.decode_invocation st ar s a fl4_abi.label.ARMASIDPoolAssign length (cslotAddrA cte) + (.AsidPool p base) block call cd fp buf ex = ok (core.result.Result.Ok (), s3, a) ∧ + (∀ (calling : Bool) (args : List Fl4SpecA64.Data) (xSlot : Fl4SpecA64.CapRef) (c0 : Fl4SpecA64.CslotPtr) + (rest : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)), + (.ok (assignInvA k (specPtr p) dst (asidOfA (wa base i))), σ) ∈ ((fl4ArchDecodeInvocation (Z := Unit) + calling (archL .armASIDPoolAssign) args xSlot c0 (.asidPoolCap (specPtr p) (asidOfA base)) + ((.archObjectCap (assignRootCapA k (specPtr root) none), dst) :: rest)).run σ).1) ∧ + (.ok ([] : List Fl4SpecA64.Data), σ3) ∈ ((fl4ArchPerformInvocation (Z := Unit) + (assignInvA k (specPtr p) dst (asidOfA (wa base i)))).run (stUpdA σ σ.curThread x .restart)).1 ∧ + JStage3A st AR Z H OS U Ac' Pp Tb' Lv' ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s3 a σ3 m ∧ + Sched.BodyKeepA (stUpdA σ σ.curThread x .restart) σ3 := by + have hslot : (cslotAddrA cte) ∈ OS := J.j5.2.1 _ (hab.isSlot J.j.1.place) + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hslot hcov J.actW J.running J.cur + have D : ∀ {bB bC bD bF : Bool} {len buf' : U64} {rX : core.result.Result Unit error.Exc} {sX : S} {aX : A}, + syscall.decode_invocation st ar s a fl4_abi.label.ARMASIDPoolAssign len (cslotAddrA cte) (.AsidPool p base) bB bC bD bF buf' ex = ok (rX, sX, aX) → + ∀ σX : Fl4SpecA64.State Unit, ∃ Ac' : Set U64, SysL st H Z.Cs OS ia ie sX ∧ SymL st H Z.Cs sX ∧ + BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac' sX ∧ + Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U sX ∧ + J7A st (J7R2A st ar hPl.ipa) Z H OS sX σX m := fun hd σX => + (J.decodeFinA hA hM hPl hfm hslot hcov (hpool) hext hSC (lbl := fl4_abi.label.ARMASIDPoolAssign) (Or.inr (Or.inr (Or.inl ⟨p, base, rfl, rfl⟩))) + (fun _ h => by cases h) (fun _ h => by cases h) hd σX m).2 + refine JStage3A.asidAssignA (ar := ar) (nA := hPl.ipa) (J := J.toJStage3A) (RS := RS) (hnvm := hnvm) (cte := cte) (hab := hab) (p := p) (base := base) (hpool := hpool) (length := length) (buf := buf) (block := block) (call := call) (cd := cd) (fp := fp) (ex := ex) (dst := dst) (hd := hd) (hex := hex) (k := k) (root := root) (oldAsid := oldAsid) (oldVa := oldVa) (hcap := hcap) (htab := htab) (hp0 := hp0) (i := i) (hsearch := hsearch) (hfull := hfull) + (fin := fun _ _ σ3 _ hd _ => D hd σ3) + +end + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3T3ArmsCnA.lean b/proofs/Fl4ProofsA64/P3/Stage3T3ArmsCnA.lean new file mode 100644 index 000000000..60fd47732 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3T3ArmsCnA.lean @@ -0,0 +1,468 @@ +/- +P3 (aarch64, A64-P3 Stage 3, T3 step 2): **the CNode Copy / Move / Rotate arms at a syscall entry**, `symL` / +`sysl` / `j7` discharged by `JSyscallA.decodeFinA` through the decode equations (`Stage3T3DecEqA`). The binders are +the results of FL4's decode-site checks. + +Named premises: [P] `ArchTotal`, `MaxSpi`, `ArchPlat`, `A64FrameMasks`; those of each arm. +-/ +import Fl4ProofsA64.P3.Stage3ArmCnCopyA +import Fl4ProofsA64.P3.Stage3ArmCnMoveA +import Fl4ProofsA64.P3.Stage3ArmCnRotA +import Fl4ProofsA64.P3.Stage3ArmCnSwapA +import Fl4ProofsA64.P3.Stage3FinA +import Fl4ProofsA64.P3.Stage3RsSiteA +import Fl4ProofsA64.P3.Stage3T3DecEqA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +section +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +set_option maxHeartbeats 4000000 in +/-- **CNode Copy / Mint**, at a syscall entry. -/ +theorem JSyscallA.cnodeCopyA + {AR : A → HwRootA → Prop} + {Z : XSetsA} + {H : Heap} + {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} + {ia ie : Nat} + {N E Rp : Set U64} + {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} + {κ : U64 → U64} + {n : Nat} + + {s : S} + {a : A} + {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} + {ps pd : U64} + {nc : capability.Cap} + {anc : Fl4SpecA64.Cap} + {ar : machine.Arch A} + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM) + (ss sd : Fl4SpecA64.CslotPtr) + (hCs : ss.1 = specPtr ps ∧ Z.th ps = some (.cnode ss.2.length)) + (hCd : sd.1 = specPtr pd ∧ Z.th pd = some (.cnode sd.2.length)) + (hsn : capA st s (cslotAddrA ss) ≠ .Null) + (hdn : capA st s (cslotAddrA sd) = .Null) + (hdc : obj.sext48 (cslotAddrA sd) = ok (cslotAddrA sd)) + (hwf : CapWfA nc) + (hanc : CapEqA nc anc) + (hw : CapRt.EncWf nc) + (hn : nc ≠ .Null) + (hD : Fl4ProofsA64.P3.DerivA nc (capA st s (cslotAddrA ss))) + (hal : UA nc (capA st s (cslotAddrA ss))) + (hk : SameBadgeKindA nc (capA st s (cslotAddrA ss))) + (hsh : Fl4ProofsA64.P3.shapeA nc = Fl4ProofsA64.P3.shapeA (capA st s (cslotAddrA ss))) + (hsh1 : Fl4ProofsA64.P1.W7.R.ST.shapeA nc = Fl4ProofsA64.P1.W7.R.ST.shapeA (capA st s (cslotAddrA ss))) + (hbd1 : badgeA nc = badgeA (capA st s (cslotAddrA ss)) ∨ badgeA (capA st s (cslotAddrA ss)) = 0#u64) + (hbd : ∀ r b rs, (absCapA (capA st s (cslotAddrA ss)) = .endpointCap r b rs ∨ + absCapA (capA st s (cslotAddrA ss)) = .notificationCap r b rs) → b ≠ 0 → + Fl4SpecA64.capEpBadge (absCapA nc) = b) + (hirq : ∀ i, nc = .IrqHandler i ↔ capA st s (cslotAddrA ss) = .IrqHandler i) + (hz : ∀ p t k, capA st s (cslotAddrA ss) ≠ .Zombie p t k) + (hut : isUtA (capA st s (cslotAddrA ss)) = true → ∀ y, ¬ Fl4Proofs.P3.Mdb.Desc Z.G.par y (cslotAddrA ss)) + (hdo : ¬ IrqNodeA (L.ksb s) (fl4Ptr sd.1) ∨ IrqCapA (absCapA nc)) + (hcv : CapValidA Z.th (capA st s (cslotAddrA ss)) → CapValidA Z.th nc) + (hsci : NoIdleCapV m n anc) + {slot : U64} (hslot : slot ∈ OS) {p r gs g : U64} (hslc : capA st s slot = .CNode p r gs g) + {extra : Aeneas.Std.Array obj.Slot 3#usize} {lbl length buf index depth x0 : U64} + (hlb : lbl = fl4_abi.label.CNodeCopy ∨ lbl = fl4_abi.label.CNodeMint) (hl4 : ¬ length < 4#u64) + (hl2 : ¬ length < 2#u64) (hi : msg.syscall_arg st s (L.curW s) buf 0#u64 = ok index) + (hd : msg.syscall_arg st s (L.curW s) buf 1#u64 = ok depth) + (hres : cspace.lookup_target_slot st s (.CNode p r gs g) index depth = ok (.Ok (cslotAddrA sd))) + (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) (h0 : x0 ≠ 0#u64) + {si sdp : U64} (h2 : msg.syscall_arg st s (L.curW s) buf 2#u64 = ok si) + (h3 : msg.syscall_arg st s (L.curW s) buf 3#u64 = ok sdp) + {root : capability.Cap} (hroot : obj.Slot.cap st x0 s = ok root) + (hemp : cdt.ensure_empty_slot st s (cslotAddrA sd) = ok (.Ok ())) + (hsrc : cspace.lookup_source_slot st s root si sdp = ok (.Ok (cslotAddrA ss))) + (hcop : cnode.copied_cap st s lbl length buf (cslotAddrA ss) (capA st s (cslotAddrA ss)) = ok (.Ok (nc, false))) + (hcov : SlotCov st OS s slot) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) : + ∃ x s2 σ2, σ.kheap σ.curThread = some (.tcb x) ∧ + sched.set_thread_state st s (L.curW s) sched.TS_RESTART = ok (L.wr s (stA (L.curW s)) sched.TS_RESTART) ∧ + cdt.insert st (L.wr s (stA (L.curW s)) sched.TS_RESTART) nc (cslotAddrA ss) (cslotAddrA sd) = ok s2 ∧ + ((), σ2) ∈ (Fl4SpecA64.capInsert (Z := Unit) anc ss sd (stUpdA σ σ.curThread x .restart)).1 ∧ + JStage3A st AR { Z with G := (insGA Z.G (cslotAddrA ss) (cslotAddrA sd) + (insBA (stUpdA σ σ.curThread x .restart) ss (absCapA nc) (absCapA (capA st s (cslotAddrA ss))))) } + H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s2 a σ2 m ∧ + Sched.BodyKeepA (stUpdA σ σ.curThread x .restart) σ2 := by + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hslot hcov J.actW J.running J.cur + have D : ∀ {bB bC bD bF : Bool} {len buf' : U64} {rX : core.result.Result Unit error.Exc} {sX : S} {aX : A}, + syscall.decode_invocation st ar s a lbl len slot (.CNode p r gs g) bB bC bD bF buf' extra = ok (rX, sX, aX) → + ∀ σX : Fl4SpecA64.State Unit, ∃ Ac' : Set U64, SysL st H Z.Cs OS ia ie sX ∧ SymL st H Z.Cs sX ∧ + BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac' sX ∧ + Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U sX ∧ + J7A st (J7R2A st ar hPl.ipa) Z H OS sX σX m := fun hd σX => + (J.decodeFinA hA hM hPl hfm hslot hcov (hslc) hext hSC (lbl := lbl) (by rcases hlb with h | h <;> subst h <;> simp [DecArmS1aEnt, DecArmQZ2, DecArmKQ2, DecArmKQ, capFam]) + (fun _ h => by cases h) (fun _ h => by cases h) hd σX m).2 + obtain ⟨x1, -, heq, -, -, -, hOS1, -⟩ := J.toJStage3A.of_restartCurA RS.pl.choose_spec.choose_spec.choose_spec.choose_spec + RS.sym RS.run hnvm RS.cur RS.vs RS.vc + have hlbl : ¬ lbl < fl4_abi.label.CNodeRevoke ∧ ¬ lbl > fl4_abi.label.CNodeRotate ∧ + lbl ≥ fl4_abi.label.CNodeCopy ∧ lbl ≤ fl4_abi.label.CNodeMutate := by + have := lblA_revoke; have := lblA_rotate; have := lblA_copy; have := lblA_mutate + have := lblA_mint; have := lblA_move + rcases hlb with h | h <;> subst h <;> refine ⟨?_, ?_, ?_, ?_⟩ <;> scalar_tac + have xs : cslotAddrA ss ∈ OS := J.j5.2.1 _ ((show AbsSlotA Z.th ss from ⟨ps, hCs.1, Or.inl hCs.2⟩).isSlot J.j.1.place) + have e : cdt.insert st (L.wr s (stA (L.curW s)) sched.TS_RESTART) nc (cslotAddrA ss) (cslotAddrA sd) = + cdt.insert_with_cap st (L.wr s (stA (L.curW s)) sched.TS_RESTART) nc (capA st s (cslotAddrA ss)) (cslotAddrA ss) + (cslotAddrA sd) := by unfold cdt.insert; rw [capA_eq, bind_ok, hOS1.capA xs] + have hdec : ∀ s2, cdt.insert st (L.wr s (stA (L.curW s)) sched.TS_RESTART) nc (cslotAddrA ss) (cslotAddrA sd) = ok s2 → + syscall.decode_invocation st ar s a lbl length slot (.CNode p r gs g) false false false false buf extra = + ok (core.result.Result.Ok (), s2, a) := fun s2 hs2 => by + rw [(decCnode_copyA (ar := ar) (a := a) (block := false) (call := false) (donate := false) hlbl.1 hlbl.2.1 hlbl.2.2.1 hlbl.2.2.2 + hl2 hl4 hi hd hres hx0 h0 h2 h3 hroot hemp hsrc (capA_eq _ _) hsn hcop hn), heq, bind_ok, if_neg Bool.false_ne_true, ← e, hs2, bind_ok] + refine JStage3A.cnodeCopyA (ar := ar) (nA := hPl.ipa) (J := J.toJStage3A) (RS := RS) (hnvm := hnvm) (ss := ss) (sd := sd) (hCs := hCs) (hCd := hCd) (hsn := hsn) (hdn := hdn) (hdc := hdc) (hwf := hwf) (hanc := hanc) (hw := hw) (hn := hn) (hD := hD) (hal := hal) (hk := hk) (hsh := hsh) (hsh1 := hsh1) (hbd1 := hbd1) (hbd := hbd) (hirq := hirq) (hz := hz) (hut := hut) (hdo := hdo) (hcv := hcv) (hsci := hsci) + (symL := fun s2 hs2 => (D (hdec s2 hs2) σ).choose_spec.2.1) + (sysl := fun s2 hs2 => (D (hdec s2 hs2) σ).choose_spec.1) + (j7 := fun _ s2 σ2 _ hs2 _ => (D (hdec s2 hs2) σ2).choose_spec.2.2.2.2) + +end + +section +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +set_option maxHeartbeats 4000000 in +/-- **CNode Move / Mutate**, at a syscall entry. -/ +theorem JSyscallA.cnodeMoveA + {AR : A → HwRootA → Prop} + {Z : XSetsA} + {H : Heap} + {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} + {ia ie : Nat} + {N E Rp : Set U64} + {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} + {κ : U64 → U64} + {n : Nat} + + {s : S} + {a : A} + {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} + {ps pd : U64} + {nc : capability.Cap} + {ac : Fl4SpecA64.Cap} + {ar : machine.Arch A} + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM) + (ss sd : Fl4SpecA64.CslotPtr) + (hCs : ss.1 = specPtr ps ∧ Z.th ps = some (.cnode ss.2.length)) + (hCd : sd.1 = specPtr pd ∧ Z.th pd = some (.cnode sd.2.length)) + (hsn : capA st s (cslotAddrA ss) ≠ .Null) + (hdn : capA st s (cslotAddrA sd) = .Null) + (hdc : obj.sext48 (cslotAddrA sd) = ok (cslotAddrA sd)) + (hca : CapRelA nc ac) + (hw : CapRt.EncWf nc) + (hn : nc ≠ .Null) + (hD : DerivA nc (capA st s (cslotAddrA ss))) + (hal : UA nc (capA st s (cslotAddrA ss))) + (hsh : Fl4ProofsA64.P1.W7.R.ST.shapeA nc = Fl4ProofsA64.P1.W7.R.ST.shapeA (capA st s (cslotAddrA ss))) + (hbd : badgeA nc = badgeA (capA st s (cslotAddrA ss))) + (hirq : ∀ i, nc = .IrqHandler i ↔ capA st s (cslotAddrA ss) = .IrqHandler i) + (hno : CapObjOkA Z nc) + (hok : CapOkA σ.interruptIrqNode sd ac) + (hsci : NoIdleCapV m n ac) + {slot : U64} (hslot : slot ∈ OS) {p r gs g : U64} (hslc : capA st s slot = .CNode p r gs g) + {extra : Aeneas.Std.Array obj.Slot 3#usize} {lbl length buf index depth x0 : U64} + (hlb : lbl = fl4_abi.label.CNodeMove ∨ lbl = fl4_abi.label.CNodeMutate) (hl4 : ¬ length < 4#u64) + (hl2 : ¬ length < 2#u64) (hi : msg.syscall_arg st s (L.curW s) buf 0#u64 = ok index) + (hd : msg.syscall_arg st s (L.curW s) buf 1#u64 = ok depth) + (hres : cspace.lookup_target_slot st s (.CNode p r gs g) index depth = ok (.Ok (cslotAddrA sd))) + (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) (h0 : x0 ≠ 0#u64) + {si sdp : U64} (h2 : msg.syscall_arg st s (L.curW s) buf 2#u64 = ok si) + (h3 : msg.syscall_arg st s (L.curW s) buf 3#u64 = ok sdp) + {root : capability.Cap} (hroot : obj.Slot.cap st x0 s = ok root) + (hemp : cdt.ensure_empty_slot st s (cslotAddrA sd) = ok (.Ok ())) + (hsrc : cspace.lookup_source_slot st s root si sdp = ok (.Ok (cslotAddrA ss))) + (hcop : cnode.copied_cap st s lbl length buf (cslotAddrA ss) (capA st s (cslotAddrA ss)) = ok (.Ok (nc, true))) + (hcov : SlotCov st OS s slot) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) : + ∃ x s2 σ2, σ.kheap σ.curThread = some (.tcb x) ∧ + sched.set_thread_state st s (L.curW s) sched.TS_RESTART = ok (L.wr s (stA (L.curW s)) sched.TS_RESTART) ∧ + cdt.move_cap st (L.wr s (stA (L.curW s)) sched.TS_RESTART) nc (cslotAddrA ss) (cslotAddrA sd) = ok s2 ∧ + ((), σ2) ∈ (capMove (Z := Unit) ac ss sd (stUpdA σ σ.curThread x .restart)).1 ∧ + JStage3A st AR { Z with G := mvGA Z.G (cslotAddrA ss) (cslotAddrA sd) } H OS U Ac Pp Tb Lv ia ie N E Rp Bf + stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s2 a σ2 m ∧ + Sched.BodyKeepA (stUpdA σ σ.curThread x .restart) σ2 := by + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hslot hcov J.actW J.running J.cur + have D : ∀ {bB bC bD bF : Bool} {len buf' : U64} {rX : core.result.Result Unit error.Exc} {sX : S} {aX : A}, + syscall.decode_invocation st ar s a lbl len slot (.CNode p r gs g) bB bC bD bF buf' extra = ok (rX, sX, aX) → + ∀ σX : Fl4SpecA64.State Unit, ∃ Ac' : Set U64, SysL st H Z.Cs OS ia ie sX ∧ SymL st H Z.Cs sX ∧ + BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac' sX ∧ + Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U sX ∧ + J7A st (J7R2A st ar hPl.ipa) Z H OS sX σX m := fun hd σX => + (J.decodeFinA hA hM hPl hfm hslot hcov (hslc) hext hSC (lbl := lbl) (by rcases hlb with h | h <;> subst h <;> simp [DecArmS1aEnt, DecArmQZ2, DecArmKQ2, DecArmKQ, capFam]) + (fun _ h => by cases h) (fun _ h => by cases h) hd σX m).2 + obtain ⟨x1, -, heq, -, -, -, hOS1, -⟩ := J.toJStage3A.of_restartCurA RS.pl.choose_spec.choose_spec.choose_spec.choose_spec + RS.sym RS.run hnvm RS.cur RS.vs RS.vc + have hlbl : ¬ lbl < fl4_abi.label.CNodeRevoke ∧ ¬ lbl > fl4_abi.label.CNodeRotate ∧ + lbl ≥ fl4_abi.label.CNodeCopy ∧ lbl ≤ fl4_abi.label.CNodeMutate := by + have := lblA_revoke; have := lblA_rotate; have := lblA_copy; have := lblA_mutate + have := lblA_mint; have := lblA_move + rcases hlb with h | h <;> subst h <;> refine ⟨?_, ?_, ?_, ?_⟩ <;> scalar_tac + have hdec : ∀ s2, cdt.move_cap st (L.wr s (stA (L.curW s)) sched.TS_RESTART) nc (cslotAddrA ss) (cslotAddrA sd) = ok s2 → + syscall.decode_invocation st ar s a lbl length slot (.CNode p r gs g) false false false false buf extra = + ok (core.result.Result.Ok (), s2, a) := fun s2 hs2 => by + rw [(decCnode_copyA (ar := ar) (a := a) (block := false) (call := false) (donate := false) hlbl.1 hlbl.2.1 hlbl.2.2.1 hlbl.2.2.2 + hl2 hl4 hi hd hres hx0 h0 h2 h3 hroot hemp hsrc (capA_eq _ _) hsn hcop hn), heq, bind_ok, if_pos rfl, hs2, bind_ok] + refine JStage3A.cnodeMoveA (ar := ar) (nA := hPl.ipa) (J := J.toJStage3A) (RS := RS) (hnvm := hnvm) (ss := ss) (sd := sd) (hCs := hCs) (hCd := hCd) (hsn := hsn) (hdn := hdn) (hdc := hdc) (hca := hca) (hw := hw) (hn := hn) (hD := hD) (hal := hal) (hsh := hsh) (hbd := hbd) (hirq := hirq) (hno := hno) (hok := hok) (hsci := hsci) + (symL := fun s2 hs2 => (D (hdec s2 hs2) σ).choose_spec.2.1) + (sysl := fun s2 hs2 => (D (hdec s2 hs2) σ).choose_spec.1) + (j7 := fun _ s2 σ2 _ hs2 _ => (D (hdec s2 hs2) σ2).choose_spec.2.2.2.2) + +end + +section +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +set_option maxHeartbeats 4000000 in +/-- **CNode Rotate** (three slots), at a syscall entry. -/ +theorem JSyscallA.cnodeRotateA + {AR : A → HwRootA → Prop} + {Z : XSetsA} + {H : Heap} + {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} + {ia ie : Nat} + {N E Rp : Set U64} + {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} + {κ : U64 → U64} + {n : Nat} + + {s : S} + {a : A} + {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} + {ps pp pd : U64} + {np : capability.Cap} + {ap : Fl4SpecA64.Cap} + {ns : capability.Cap} + {an : Fl4SpecA64.Cap} + {ar : machine.Arch A} + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM) + (ss sp sd : Fl4SpecA64.CslotPtr) + (hCs : ss.1 = specPtr ps ∧ Z.th ps = some (.cnode ss.2.length)) + (hCp : sp.1 = specPtr pp ∧ Z.th pp = some (.cnode sp.2.length)) + (hCd : sd.1 = specPtr pd ∧ Z.th pd = some (.cnode sd.2.length)) + (hsp : cslotAddrA ss ≠ cslotAddrA sp) + (hsd : cslotAddrA ss ≠ cslotAddrA sd) + (hsn : capA st s (cslotAddrA ss) ≠ .Null) + (hpn : capA st s (cslotAddrA sp) ≠ .Null) + (hdn : capA st s (cslotAddrA sd) = .Null) + (hpc : obj.sext48 (cslotAddrA sp) = ok (cslotAddrA sp)) + (hdc : obj.sext48 (cslotAddrA sd) = ok (cslotAddrA sd)) + (hcp : CapRelA np ap) + (hwp : CapRt.EncWf np) + (hnp : np ≠ .Null) + (hDp : Fl4ProofsA64.P3.DerivA np (capA st s (cslotAddrA sp))) + (halp : UA np (capA st s (cslotAddrA sp))) + (hshp : Fl4ProofsA64.P1.W7.R.ST.shapeA np = Fl4ProofsA64.P1.W7.R.ST.shapeA (capA st s (cslotAddrA sp))) + (hbdp : badgeA np = badgeA (capA st s (cslotAddrA sp))) + (hirqp : ∀ i, np = .IrqHandler i ↔ capA st s (cslotAddrA sp) = .IrqHandler i) + (hop : CapObjOkA Z np) + (hokp : CapOkA σ.interruptIrqNode sd ap) + (hscp : NoIdleCapV m n ap) + (hcs : CapRelA ns an) + (hws : CapRt.EncWf ns) + (hns : ns ≠ .Null) + (hDs : Fl4ProofsA64.P3.DerivA ns (capA st s (cslotAddrA ss))) + (hals : UA ns (capA st s (cslotAddrA ss))) + (hshs : Fl4ProofsA64.P1.W7.R.ST.shapeA ns = Fl4ProofsA64.P1.W7.R.ST.shapeA (capA st s (cslotAddrA ss))) + (hbds : badgeA ns = badgeA (capA st s (cslotAddrA ss))) + (hirqs : ∀ i, ns = .IrqHandler i ↔ capA st s (cslotAddrA ss) = .IrqHandler i) + (hos : CapObjOkA Z ns) + (hoks : CapOkA σ.interruptIrqNode sp an) + (hscs : NoIdleCapV m n an) + {slot : U64} (hslot : slot ∈ OS) {p r gs g : U64} (hslc : capA st s slot = .CNode p r gs g) + {extra : Aeneas.Std.Array obj.Slot 3#usize} {lbl length buf index depth x0 : U64} + (hlb : lbl = fl4_abi.label.CNodeRotate) (hl8 : ¬ length < 8#u64) + (hl2 : ¬ length < 2#u64) (hi : msg.syscall_arg st s (L.curW s) buf 0#u64 = ok index) + (hd : msg.syscall_arg st s (L.curW s) buf 1#u64 = ok depth) + (hres : cspace.lookup_target_slot st s (.CNode p r gs g) index depth = ok (.Ok (cslotAddrA sd))) + (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) (h0 : x0 ≠ 0#u64) + {x1' : U64} (hx1 : Aeneas.Std.Array.index_usize extra 1#usize = ok x1') (h1 : x1' ≠ 0#u64) + {pdata sdata : U64} (h2 : msg.syscall_arg st s (L.curW s) buf 2#u64 = ok pdata) + (h5 : msg.syscall_arg st s (L.curW s) buf 5#u64 = ok sdata) + (hrot : cnode.rotate_slots st s buf extra (cslotAddrA sd) = ok (.Ok (cslotAddrA ss, cslotAddrA sp))) + (hus : objects.update_cap_data true sdata (capA st s (cslotAddrA ss)) = ok ns) + (hup : objects.update_cap_data true pdata (capA st s (cslotAddrA sp)) = ok np) + (hcov : SlotCov st OS s slot) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) : + ∃ x s1 s3 σ1 σ3, σ.kheap σ.curThread = some (.tcb x) ∧ + sched.set_thread_state st s (L.curW s) sched.TS_RESTART = ok (L.wr s (stA (L.curW s)) sched.TS_RESTART) ∧ + cdt.move_cap st (L.wr s (stA (L.curW s)) sched.TS_RESTART) np (cslotAddrA sp) (cslotAddrA sd) = ok s1 ∧ + cdt.move_cap st s1 ns (cslotAddrA ss) (cslotAddrA sp) = ok s3 ∧ + ((), σ1) ∈ (capMove (Z := Unit) ap sp sd (stUpdA σ σ.curThread x .restart)).1 ∧ + ((), σ3) ∈ (capMove (Z := Unit) an ss sp σ1).1 ∧ + JStage3A st AR { Z with G := rotGA Z.G (cslotAddrA ss) (cslotAddrA sp) (cslotAddrA sd) } H OS U Ac Pp Tb + Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s3 a σ3 m ∧ + Sched.BodyKeepA (stUpdA σ σ.curThread x .restart) σ3 := by + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hslot hcov J.actW J.running J.cur + have D : ∀ {bB bC bD bF : Bool} {len buf' : U64} {rX : core.result.Result Unit error.Exc} {sX : S} {aX : A}, + syscall.decode_invocation st ar s a lbl len slot (.CNode p r gs g) bB bC bD bF buf' extra = ok (rX, sX, aX) → + ∀ σX : Fl4SpecA64.State Unit, ∃ Ac' : Set U64, SysL st H Z.Cs OS ia ie sX ∧ SymL st H Z.Cs sX ∧ + BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac' sX ∧ + Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U sX ∧ + J7A st (J7R2A st ar hPl.ipa) Z H OS sX σX m := fun hd σX => + (J.decodeFinA hA hM hPl hfm hslot hcov (hslc) hext hSC (lbl := lbl) (by subst hlb; simp [DecArmS1aEnt, DecArmQZ2, DecArmKQ2, DecArmKQ, capFam]) + (fun _ h => by cases h) (fun _ h => by cases h) hd σX m).2 + obtain ⟨y1, -, heq, -⟩ := J.toJStage3A.of_restartCurA RS.pl.choose_spec.choose_spec.choose_spec.choose_spec + RS.sym RS.run hnvm RS.cur RS.vs RS.vc + have hlbl : ¬ lbl < fl4_abi.label.CNodeRevoke ∧ ¬ lbl > fl4_abi.label.CNodeRotate := by + have := lblA_revoke; have := lblA_rotate + subst hlb; refine ⟨?_, ?_⟩ <;> scalar_tac + have hdec : ∀ s1 s3, cdt.move_cap st (L.wr s (stA (L.curW s)) sched.TS_RESTART) np (cslotAddrA sp) (cslotAddrA sd) = ok s1 → + cdt.move_cap st s1 ns (cslotAddrA ss) (cslotAddrA sp) = ok s3 → + syscall.decode_invocation st ar s a lbl length slot (.CNode p r gs g) false false false false buf extra = + ok (core.result.Result.Ok (), s3, a) := fun s1 s3 hs1 hs3 => by + rw [decCnode_rotateA (ar := ar) (a := a) (block := false) (call := false) (donate := false) hlb hlbl.1 hlbl.2 hl2 hl8 hi hd hres + hx0 h0 hx1 h1 h2 h5 hrot (capA_eq _ _) (capA_eq _ _) hus hup hns hnp, heq, bind_ok, if_neg hsd, hs1, bind_ok, hs3, bind_ok] + refine JStage3A.cnodeRotateA (ar := ar) (nA := hPl.ipa) (J := J.toJStage3A) (RS := RS) (hnvm := hnvm) (ss := ss) (sp := sp) (sd := sd) (hCs := hCs) (hCp := hCp) (hCd := hCd) (hsp := hsp) (hsd := hsd) (hsn := hsn) (hpn := hpn) (hdn := hdn) (hpc := hpc) (hdc := hdc) (hcp := hcp) (hwp := hwp) (hnp := hnp) (hDp := hDp) (halp := halp) (hshp := hshp) (hbdp := hbdp) (hirqp := hirqp) (hop := hop) (hokp := hokp) (hscp := hscp) (hcs := hcs) (hws := hws) (hns := hns) (hDs := hDs) (hals := hals) (hshs := hshs) (hbds := hbds) (hirqs := hirqs) (hos := hos) (hoks := hoks) (hscs := hscs) + (symL := fun s1 s3 hs1 hs3 => (D (hdec s1 s3 hs1 hs3) σ).choose_spec.2.1) + (sysl := fun s1 s3 hs1 hs3 => (D (hdec s1 s3 hs1 hs3) σ).choose_spec.1) + (j7 := fun _ s1 s3 _ σ3 _ hs1 hs3 _ _ => (D (hdec s1 s3 hs1 hs3) σ3).choose_spec.2.2.2.2) + +end + +section +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +set_option maxHeartbeats 4000000 in +/-- **CNode Rotate** as a swap, at a syscall entry. -/ +theorem JSyscallA.cnodeRotateSwA + {AR : A → HwRootA → Prop} + {Z : XSetsA} + {H : Heap} + {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} + {ia ie : Nat} + {N E Rp : Set U64} + {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} + {κ : U64 → U64} + {n : Nat} + + {s : S} + {a : A} + {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} + {ps pp : U64} + {np : capability.Cap} + {ap : Fl4SpecA64.Cap} + {ns : capability.Cap} + {an : Fl4SpecA64.Cap} + {ar : machine.Arch A} + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM) + (ss sp : Fl4SpecA64.CslotPtr) + (hCs : ss.1 = specPtr ps ∧ Z.th ps = some (.cnode ss.2.length)) + (hCp : sp.1 = specPtr pp ∧ Z.th pp = some (.cnode sp.2.length)) + (hsp : cslotAddrA ss ≠ cslotAddrA sp) + (hsn : capA st s (cslotAddrA ss) ≠ .Null) + (hpn : capA st s (cslotAddrA sp) ≠ .Null) + (hsx : obj.sext48 (cslotAddrA ss) = ok (cslotAddrA ss)) + (hpx : obj.sext48 (cslotAddrA sp) = ok (cslotAddrA sp)) + (hcp : CapRelA np ap) + (hwp : CapRt.EncWf np) + (hnp : np ≠ .Null) + (hDp : Fl4ProofsA64.P3.DerivA np (capA st s (cslotAddrA sp))) + (halp : UA np (capA st s (cslotAddrA sp))) + (hshp : Fl4ProofsA64.P1.W7.R.ST.shapeA np = Fl4ProofsA64.P1.W7.R.ST.shapeA (capA st s (cslotAddrA sp))) + (hbdp : badgeA np = badgeA (capA st s (cslotAddrA sp))) + (hirqp : ∀ i, np = .IrqHandler i ↔ capA st s (cslotAddrA sp) = .IrqHandler i) + (hop : CapObjOkA Z np) + (hokp : CapOkA σ.interruptIrqNode ss ap) + (hscp : NoIdleCapV m n ap) + (hcs : CapRelA ns an) + (hws : CapRt.EncWf ns) + (hns : ns ≠ .Null) + (hDs : Fl4ProofsA64.P3.DerivA ns (capA st s (cslotAddrA ss))) + (hals : UA ns (capA st s (cslotAddrA ss))) + (hshs : Fl4ProofsA64.P1.W7.R.ST.shapeA ns = Fl4ProofsA64.P1.W7.R.ST.shapeA (capA st s (cslotAddrA ss))) + (hbds : badgeA ns = badgeA (capA st s (cslotAddrA ss))) + (hirqs : ∀ i, ns = .IrqHandler i ↔ capA st s (cslotAddrA ss) = .IrqHandler i) + (hos : CapObjOkA Z ns) + (hoks : CapOkA σ.interruptIrqNode sp an) + (hscs : NoIdleCapV m n an) + {slot : U64} (hslot : slot ∈ OS) {p r gs g : U64} (hslc : capA st s slot = .CNode p r gs g) + {extra : Aeneas.Std.Array obj.Slot 3#usize} {lbl length buf index depth x0 : U64} + (hlb : lbl = fl4_abi.label.CNodeRotate) (hl8 : ¬ length < 8#u64) + (hl2 : ¬ length < 2#u64) (hi : msg.syscall_arg st s (L.curW s) buf 0#u64 = ok index) + (hd : msg.syscall_arg st s (L.curW s) buf 1#u64 = ok depth) + (hres : cspace.lookup_target_slot st s (.CNode p r gs g) index depth = ok (.Ok (cslotAddrA ss))) + (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) (h0 : x0 ≠ 0#u64) + {x1' : U64} (hx1 : Aeneas.Std.Array.index_usize extra 1#usize = ok x1') (h1 : x1' ≠ 0#u64) + {pdata sdata : U64} (h2 : msg.syscall_arg st s (L.curW s) buf 2#u64 = ok pdata) + (h5 : msg.syscall_arg st s (L.curW s) buf 5#u64 = ok sdata) + (hrot : cnode.rotate_slots st s buf extra (cslotAddrA ss) = ok (.Ok (cslotAddrA ss, cslotAddrA sp))) + (hus : objects.update_cap_data true sdata (capA st s (cslotAddrA ss)) = ok ns) + (hup : objects.update_cap_data true pdata (capA st s (cslotAddrA sp)) = ok np) + (hcov : SlotCov st OS s slot) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) : + ∃ x s2 σ2, σ.kheap σ.curThread = some (.tcb x) ∧ + sched.set_thread_state st s (L.curW s) sched.TS_RESTART = ok (L.wr s (stA (L.curW s)) sched.TS_RESTART) ∧ + cdt.swap st (L.wr s (stA (L.curW s)) sched.TS_RESTART) ns (cslotAddrA ss) np (cslotAddrA sp) = ok s2 ∧ + ((), σ2) ∈ (Fl4SpecA64.capSwap (Z := Unit) an ss ap sp (stUpdA σ σ.curThread x .restart)).1 ∧ + JStage3A st AR { Z with G := swGA Z.G (cslotAddrA ss) (cslotAddrA sp) } H OS U Ac Pp Tb Lv ia ie N E Rp Bf + stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s2 a σ2 m ∧ + Sched.BodyKeepA (stUpdA σ σ.curThread x .restart) σ2 := by + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hslot hcov J.actW J.running J.cur + have D : ∀ {bB bC bD bF : Bool} {len buf' : U64} {rX : core.result.Result Unit error.Exc} {sX : S} {aX : A}, + syscall.decode_invocation st ar s a lbl len slot (.CNode p r gs g) bB bC bD bF buf' extra = ok (rX, sX, aX) → + ∀ σX : Fl4SpecA64.State Unit, ∃ Ac' : Set U64, SysL st H Z.Cs OS ia ie sX ∧ SymL st H Z.Cs sX ∧ + BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac' sX ∧ + Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U sX ∧ + J7A st (J7R2A st ar hPl.ipa) Z H OS sX σX m := fun hd σX => + (J.decodeFinA hA hM hPl hfm hslot hcov (hslc) hext hSC (lbl := lbl) (by subst hlb; simp [DecArmS1aEnt, DecArmQZ2, DecArmKQ2, DecArmKQ, capFam]) + (fun _ h => by cases h) (fun _ h => by cases h) hd σX m).2 + obtain ⟨y1, -, heq, -⟩ := J.toJStage3A.of_restartCurA RS.pl.choose_spec.choose_spec.choose_spec.choose_spec + RS.sym RS.run hnvm RS.cur RS.vs RS.vc + have hlbl : ¬ lbl < fl4_abi.label.CNodeRevoke ∧ ¬ lbl > fl4_abi.label.CNodeRotate := by + have := lblA_revoke; have := lblA_rotate + subst hlb; refine ⟨?_, ?_⟩ <;> scalar_tac + have hdec : ∀ s2, cdt.swap st (L.wr s (stA (L.curW s)) sched.TS_RESTART) ns (cslotAddrA ss) np (cslotAddrA sp) = ok s2 → + syscall.decode_invocation st ar s a lbl length slot (.CNode p r gs g) false false false false buf extra = + ok (core.result.Result.Ok (), s2, a) := fun s2 hs2 => by + rw [decCnode_rotateA (ar := ar) (a := a) (block := false) (call := false) (donate := false) hlb hlbl.1 hlbl.2 hl2 hl8 hi hd hres + hx0 h0 hx1 h1 h2 h5 hrot (capA_eq _ _) (capA_eq _ _) hus hup hns hnp, heq, bind_ok, if_pos rfl, hs2, bind_ok] + refine JStage3A.cnodeRotateSwA (ar := ar) (nA := hPl.ipa) (J := J.toJStage3A) (RS := RS) (hnvm := hnvm) (ss := ss) (sp := sp) (hCs := hCs) (hCp := hCp) (hsp := hsp) (hsn := hsn) (hpn := hpn) (hsx := hsx) (hpx := hpx) (hcp := hcp) (hwp := hwp) (hnp := hnp) (hDp := hDp) (halp := halp) (hshp := hshp) (hbdp := hbdp) (hirqp := hirqp) (hop := hop) (hokp := hokp) (hscp := hscp) (hcs := hcs) (hws := hws) (hns := hns) (hDs := hDs) (hals := hals) (hshs := hshs) (hbds := hbds) (hirqs := hirqs) (hos := hos) (hoks := hoks) (hscs := hscs) + (symL := fun s2 hs2 => (D (hdec s2 hs2) σ).choose_spec.2.1) + (sysl := fun s2 hs2 => (D (hdec s2 hs2) σ).choose_spec.1) + (j7 := fun _ s2 σ2 _ hs2 _ => (D (hdec s2 hs2) σ2).choose_spec.2.2.2.2) + +end + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3T3ArmsVcpu2A.lean b/proofs/Fl4ProofsA64/P3/Stage3T3ArmsVcpu2A.lean new file mode 100644 index 000000000..b1647fff3 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3T3ArmsVcpu2A.lean @@ -0,0 +1,3434 @@ +/- +P3 (aarch64, A64-P3 S4 VCPU Phase 2): **Syscall-level VCPU arms (`ReadState` 67, `SetTCB` 60, +`WriteState` 68, `Run` 69) and unified `JSyscallA.vcpuDecodeA`** (`JSyscallA → JStage3A`, +Plan B `hpin`-compatible). + +Architecture (zero global `FrameMemRelA`, zero hand-rebuilt records): +* `vcpuReadStateA` (67): reuses `VcpuDelReadA.vcpu_read_state_to_frame_stage3TA` + + `JStage3TA.toJA_vcpuA` + `JSyscallA.decodeFinA` (zero `FrameMemRelA`). +* `vcpuSetTcbA` (60): reuses `VcpuDelHookA.vcpu_unbind_stage3TA` + `vcpu_complete_run_stage3TA` + + `VcpuDelStepsA.vcpu_cancel_run_stage3TA` + `JStage3TA.of_vcpuStepA` + + `VcpuSetTcbJA.fl4SetTcb_rust_order_eqA` + `JStage3TA.toJA_vcpuA` (zero `FrameMemRelA`). +* `vcpuWriteStateA` (68) / `vcpuRunA` (69): slice-local input-agreement `FrameInputAgreeA` + (`[scope limit]`, only when `mask ≠ 0`) + `JStage3TA.of_vcpuStepA` + `JStage3TA.toJA_vcpuA`. +-/ +import Fl4ProofsA64.P3.Stage3VcpuBridgeA +import Fl4ProofsA64.P3.VcpuDelReadA +import Fl4ProofsA64.P3.VcpuDelHookA +import Fl4ProofsA64.P3.VcpuSetTcbJA +import Fl4ProofsA64.P3.VcpuXferWriteJA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp + Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Vcpu Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section arms2 +variable {S A : Type} {st : machine.Store S} [L : LawfulStore st] +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} +variable {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} +variable {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} +variable {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +private theorem lift_ok2 {α : Type} (x : α) : lift x = ok x := rfl +private theorem okinj2 {α : Type} {x y : α} (h : ok x = ok y) : x = y := by + have hx : ok x ⦃ fun z => z = x ⦄ := (WP.spec_ok x).2 rfl + rw [h, WP.spec_ok] at hx; exact hx.symm + +private theorem bind_ok_pair2 {α β γ : Type} {x : α} {y : β} {f : α × β → Result γ} : + (do let (a, b) ← ok (x, y); f (a, b)) = f (x, y) := by rw [bind_ok]; rfl + +private theorem u64_eq_of_bv_eq2 {x y : U64} (h : x.bv = y.bv) : x = y := by + apply UScalar.eq_of_val_eq; exact congrArg BitVec.toNat h + +private theorem wa_zero2 (x : U64) : wa x 0#u64 = x := + UScalar.eq_of_val_eq (by rw [wa_valm]; change (x.val + 0) % 2 ^ 64 = x.val; rw [Nat.add_zero]; exact Nat.mod_eq_of_lt x.hBounds) + +private theorem frameSize_bits_spec2 {sz : capability.FrameSize} {bits : U64} + (hb : capability.FrameSize.bits sz = ok bits) : + bits.val = Fl4SpecA64.pageBitsForSize (frameSizeOfA sz) ∧ bits.val < 64 ∧ + (bits &&& 63#u64) = bits ∧ + (1#u64 <<< (bits &&& 63#u64)) = ok ⟨BitVec.ofNat 64 (2 ^ bits.val)⟩ := by + cases sz <;> simp [capability.FrameSize.bits, frameSizeOfA, Fl4SpecA64.pageBitsForSize] at hb ⊢ <;> + subst hb <;> refine ⟨rfl, by decide, by decide, ?_⟩ <;> rfl + +/-- `[scope limit]` Slice-local input-agreement on the 128-word transfer window `[frame, frame + 1024)`. +Replaces global `FrameMemRelA` for `WriteState` (68) and `Run` (69, when `inMask ≠ 0`). -/ +def FrameInputAgreeA (st : machine.Store S) [L : LawfulStore st] + (s : S) (σ : Fl4SpecA64.State Unit) (frame : U64) : Prop := + ∀ k < 128, (L.mem s (wa frame ⟨BitVec.ofNat 64 (8 * k)⟩)).bv = + loadWordAt σ.machineState.underlyingMemory (bufAddr (specPtr frame) k) + +set_option maxHeartbeats 600000 in +/-- **Label 67 (`ARMVCPUReadState`)** at a syscall entry (`JSyscallA → JStage3A`, Plan B `hpin`, +zero `FrameMemRelA`). -/ +theorem JSyscallA.vcpuReadStateA {ar : machine.Arch A} + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) (hP : VcpuPlatAgreeA ar) (hAK : VcpuArchKeepsA ar AR) + (hCs : ∀ c ∈ Z.Cs, c.val < Fl4SpecA64.fl4A64Platform.numNodes) (hCsN : ∀ c ∈ Z.Cs, c.val < n) + (h1 : Fl4SpecA64.fl4A64Platform.numNodes = 1) + (hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM) + {slot vc length buf : U64} {blk call don fp : Bool} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) + (hcap : capA st s slot = .Vcpu vc) (hvc : vc ∈ Z.V) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + {mi : MessageInfo} {sl : CslotPtr} {excaps : List (Fl4SpecA64.Cap × CslotPtr)} + {buffer : Option ObjRef} {args : List U64} + (hfp : VcpuFrontPinA Z.th σ slot mi sl (.archObjectCap (.vcpuCap (specPtr vc))) excaps buffer (args.map UScalar.bv)) + (hlbl : fl4LabelOf mi.miLabel = .ext .armVCPUReadState) + (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) + (hex0 : ∀ s1, Array.index_usize extra 0#usize = ok s1 → s1 ≠ 0#u64 → + ∃ c0 sl0, excaps[0]? = some (c0, sl0) ∧ CapRelA (capA st s s1) c0) + (hNoS2 : ∀ s1 p sz r d mp ept asid va, Array.index_usize extra 0#usize = ok s1 → + capA st s s1 = .Frame p sz r d mp ept asid va → ept = false) + {s3 : S} {a3 : A} + (hdec : syscall.decode_invocation st ar s a fl4_abi.label.ARMVCPUReadState length slot (.Vcpu vc) + blk call don fp buf extra = ok (core.result.Result.Ok (), s3, a3)) : + ∃ (x : Fl4SpecA64.Tcb) (frame mask : U64) (σ3 : Fl4SpecA64.State Unit), + σ.kheap σ.curThread = some (.tcb x) ∧ + VcpuDecPinA call Z.th σ slot (.invokeArchObject (.vcpu (.stateTransfer (specPtr vc) (specPtr frame) mask.bv true (0, [])))) ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + (.ok [], σ3) ∈ ((fl4ArchPerformInvocation (Z := Unit) + (.vcpu (.stateTransfer (specPtr vc) (specPtr frame) mask.bv true (0, [])))).run + (stUpdA σ σ.curThread x .restart)).1 ∧ + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s3 a3 σ3 m := by + have J3 := J.toJStage3A + obtain ⟨Pt, Ub, Pp7, Tb7, Hr, Te, Kt, Lv7, Re, bits7, KLv, W⟩ := J7A.unp J3.j7 + have hpl := J3.vcPlacedA W + have C := J.vcpuDecCtxA W hCs hCsN h1 hvc + have RS := J3.rsSiteA hext hslot hcov J.actW J.running J.cur + have hdec0 := hdec + unfold syscall.decode_invocation at hdec + rw [show sched.cur_thread st s = ok (L.curW s) from eq_of_spec_fd (sched.cur_thread.spec s), bind_ok] at hdec + dsimp only at hdec; unfold vcpu.decode at hdec + simp only [fl4_abi.label.ARMVCPUReadState, fl4_abi.label.ARMVCPUSetTCB, fl4_abi.label.ARMVCPUInjectIRQ, + fl4_abi.label.ARMVCPUReadReg, fl4_abi.label.ARMVCPUWriteReg, fl4_abi.label.ARMVCPUAckVPPI] at hdec + rw [if_neg (by decide), if_neg (by decide), if_neg (by decide), if_neg (by decide), if_neg (by decide), + if_pos trivial] at hdec + unfold vcpu.decode_state_transfer at hdec + by_cases hlen2 : length < 2#u64 + · rw [if_pos hlen2] at hdec; simp [error.truncated, bind_ok] at hdec + rw [if_neg hlen2] at hdec + have hge2 : 2 ≤ args.length := by have : ¬ length.val < 2 := hlen2; omega + cases hargs_eq : args with + | nil => simp [hargs_eq] at hge2 + | cons offset rest1 => + cases hrest_eq : rest1 with + | nil => simp [hargs_eq, hrest_eq] at hge2 + | cons mask rest2 => + have hlt0 : 0 < extra.val.length := by rw [extra.property]; decide + have hs1_idx : Array.index_usize extra 0#usize = ok (extra.val[0]'hlt0) := + eq_of_spec_fd (Array.index_usize_spec extra 0#usize hlt0) + set s1 := extra.val[0]'hlt0 + rw [hs1_idx, bind_ok] at hdec + have hisn : obj.Slot.is_null s1 = ok (decide (s1 = 0#u64)) := by unfold obj.Slot.is_null; rfl + rw [hisn, bind_ok] at hdec + by_cases hs1_0 : s1 = 0#u64 + · simp [hs1_0, error.truncated, bind_ok] at hdec + simp only [hs1_0, decide_false, Bool.false_eq_true, ↓reduceIte] at hdec + have hs1_mem : s1 ∈ extra.val := List.getElem_mem hlt0 + have hs1_OS : s1 ∈ OS := hext s1 hs1_mem hs1_0 + obtain ⟨c0, sl0, hex0_eq, hcapRel0⟩ := hex0 s1 hs1_idx hs1_0 + rw [show sched.cur_thread st s = ok (L.curW s) from eq_of_spec_fd (sched.cur_thread.spec s), bind_ok, + hargs 0#u64 offset (by simp [hargs_eq]), bind_ok, + hargs 1#u64 mask (by simp [hargs_eq, hrest_eq]), bind_ok] at hdec + have hall : fl4_abi.consts.vcpu_state_mask.ALL = ok 63#u64 := + eq_of_spec_fd Fl4ProofsA64.P0.Consts.fl4_abi.consts.vcpu_state_mask.ALL.spec + rw [hall, bind_ok, lift_ok2, bind_ok, lift_ok2, bind_ok] at hdec + by_cases hmask_bad : (mask &&& ~~~63#u64 != 0#u64) = true + · rw [if_pos hmask_bad] at hdec; simp [error.invalid_arg, bind_ok] at hdec + rw [if_neg hmask_bad] at hdec + have hmask_ok : mask.bv &&& ~~~smAll = 0 := by + have h0 : mask &&& ~~~63#u64 = 0#u64 := by + by_contra hne; exact hmask_bad (bne_iff_ne.2 hne) + exact congrArg UScalar.bv h0 + have hcap_s1 : obj.Slot.cap st s1 s = ok (capA st s s1) := Fl4ProofsA64.P1.W7.R.ST.capA_eq s s1 + rw [hcap_s1, bind_ok] at hdec + have hfr_ex : ∃ ptr sz rights is_dev mp ept asid va, + capA st s s1 = .Frame ptr sz rights is_dev mp ept asid va := by + cases hc : capA st s s1 <;> rw [hc] at hdec <;> + first | (simp [error.invalid_cap, bind_ok] at hdec; done) | exact ⟨_, _, _, _, _, _, _, _, rfl⟩ + obtain ⟨ptr, sz, rights, is_dev, mp, ept, asid, va, hc_s1⟩ := hfr_ex + rw [hc_s1] at hdec hcapRel0; dsimp only at hdec + by_cases hdev : is_dev = true + · rw [if_pos hdev] at hdec; simp [error.invalid_cap, bind_ok] at hdec + have hdevF : is_dev = false := by cases is_dev <;> simp_all + subst hdevF + rw [if_neg (by decide)] at hdec + by_cases hko : rights = capability.VM_KERNEL_ONLY + · rw [if_pos hko] at hdec; simp [error.invalid_cap, bind_ok] at hdec + rw [if_neg hko] at hdec + by_cases hrw : (rights != capability.VM_READ_WRITE) = true + · rw [if_pos hrw] at hdec; simp [error.invalid_cap, bind_ok] at hdec + rw [if_neg hrw] at hdec + have hrw_eq : rights = capability.VM_READ_WRITE := by + by_contra hne; exact hrw (bne_iff_ne.2 hne) + subst hrw_eq + have hbits_ex : ∃ bits, capability.FrameSize.bits sz = ok bits := by + cases sz <;> exact ⟨_, rfl⟩ + obtain ⟨bits, hbits⟩ := hbits_ex + obtain ⟨hbits_sp, hbits_lt, hbits_and, hshl⟩ := frameSize_bits_spec2 hbits + rw [hbits, bind_ok, lift_ok2, bind_ok, hshl, bind_ok, lift_ok2, bind_ok] at hdec + by_cases hoff64 : (offset &&& 63#u64 != 0#u64) = true + · rw [if_pos hoff64] at hdec; simp [error.invalid_arg, bind_ok] at hdec + rw [if_neg hoff64] at hdec + have hoff64_eq : offset &&& 63#u64 = 0#u64 := by + by_contra hne; exact hoff64 (bne_iff_ne.2 hne) + set size1 : U64 := ⟨BitVec.ofNat 64 (2 ^ bits.val)⟩ + have hsize1_val : size1.val = 2 ^ bits.val := by + show (BitVec.ofNat 64 (2 ^ bits.val)).toNat = 2 ^ bits.val + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (Nat.pow_lt_pow_right (by omega) hbits_lt) + by_cases hoff_ge : size1 ≤ offset + · rw [if_pos hoff_ge] at hdec; simp [error.invalid_arg, bind_ok] at hdec + rw [if_neg hoff_ge] at hdec + have hoff_lt : offset.val < 2 ^ bits.val := by + have : ¬ size1.val ≤ offset.val := hoff_ge; omega + have hsub : size1 - offset = ok ⟨BitVec.ofNat 64 (2 ^ bits.val - offset.val)⟩ := by + change UScalar.sub size1 offset = _; unfold UScalar.sub + rw [if_neg (Nat.not_lt.mpr (by omega : offset.val ≤ size1.val))] + congr 1; apply UScalar.eq_of_val_eq; rw [hsize1_val]; rfl + rw [hsub, bind_ok] at hdec + have hsz_c : fl4_abi.consts.vcpu_state_mask.SIZE = 1024#u64 := by + unfold fl4_abi.consts.vcpu_state_mask.SIZE; rfl + rw [hsz_c] at hdec + by_cases hrem_lt : (⟨BitVec.ofNat 64 (2 ^ bits.val - offset.val)⟩ : U64) < 1024#u64 + · rw [if_pos hrem_lt] at hdec; simp [error.invalid_arg, bind_ok] at hdec + rw [if_neg hrem_lt] at hdec + have hoff_sz : 2 ^ bits.val - offset.val ≥ 1024 := by + have : ¬ (⟨BitVec.ofNat 64 (2 ^ bits.val - offset.val)⟩ : U64).val < 1024 := hrem_lt + have hv : (⟨BitVec.ofNat 64 (2 ^ bits.val - offset.val)⟩ : U64).val = 2 ^ bits.val - offset.val := by + show (BitVec.ofNat 64 _).toNat = _; rw [BitVec.toNat_ofNat] + exact Nat.mod_eq_of_lt (by have := Nat.pow_lt_pow_right (a := 2) (by omega) hbits_lt; omega) + omega + rw [lift_ok2, bind_ok, vcpu_num_lrs_eqA hP a, bind_ok] at hdec + obtain ⟨g, hga, -⟩ := hP.gic a + rw [hga, bind_ok] at hdec + obtain ⟨⟨s1', a1⟩, hst_eq, hs1'_eq, hAR1⟩ := spec_ex_fd (stall_vcpu_localA hAK J3.j.1.ar vc C.hStallT C.hStallC) + dsimp only at hs1'_eq hAR1; rw [hs1'_eq] at hst_eq + rw [hst_eq, bind_ok] at hdec + change (do let s3 ← sched.set_thread_state st s (L.curW s) sched.TS_RESTART + let s4 ← vcpu.read_state_to_frame st ar s3 a1 vc (wa ptr offset) mask + ok (core.result.Result.Ok (), s4, a1)) = ok (core.result.Result.Ok (), s3, a3) at hdec + have hvc_st : ∀ v ∈ Z.V, stA (L.curW s) ≠ v ∧ stA (L.curW s) ≠ wa v 24#u64 := fun v hv => + ⟨by rw [stA_eq_wa]; have := hpl.disj_tcb v hv _ J3.sr.curT 0#u64 3328#u64 (by simp) (by simp) + rw [wa_zero2] at this; exact this.symm, + by rw [stA_eq_wa]; exact (hpl.disj_tcb v hv _ J3.sr.curT 24#u64 3328#u64 (by simp) (by simp)).symm⟩ + obtain ⟨_, _, _, _, hpl_rs⟩ := RS.pl + obtain ⟨x, hkx, hst_ok, hsts_spec, J1, -, osk1, -⟩ := + J3.of_restartCurA hpl_rs J3.symL RS.run hnvm RS.cur RS.vs hvc_st + rw [hst_ok, bind_ok] at hdec + set s2 := L.wr s (stA (L.curW s)) sched.TS_RESTART + set σ1 := stUpdA σ σ.curThread x .restart + have J1a : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s2 a1 σ1 m := + { J1 with j := ⟨{ J1.j.1 with ar := hAR1 }, J1.j.2⟩ } + have hSAct1 : SActA σ1 := by + refine ⟨fun u hu => J.sact.f1 u hu, ?_⟩ + refine ⟨{ x with tcbState := .restart }, ?_, Or.inr (Or.inl rfl)⟩ + unfold σ1 stUpdA setObjA; simp [Function.update] + have hNoSw1 : ∀ u, σ1.schedulerAction ≠ .switchThread u := J.noSwitch + have hcap_s2 : capA st s2 s1 = .Frame ptr sz capability.VM_READ_WRITE false mp ept asid va := by + rw [osk1.capA hs1_OS, hc_s1] + have hread := vcpu_read_state_to_frame_stage3TA (Clock := fun _ _ => True) + (J1a.toTA hSAct1 hNoSw1) J1a.schedValid J1a.vcpuRec6A trivial (fun _ _ _ _ _ _ => trivial) + hP hvc hs1_OS hcap_s2 hbits hoff64_eq hoff_lt hoff_sz mask + obtain ⟨s4, hread_eq, σ3, hsp3, J3T, hSV3, R6_3, -, -, -, -, -, -, osk3, -, -, -, -⟩ := spec_ex_fd hread + rw [hread_eq, bind_ok] at hdec + obtain ⟨-, rfl, rfl⟩ := Prod.mk.inj (okinj2 hdec) + obtain ⟨-, _, sysl3, symL3, -, -, -⟩ := + J.decodeFinA hA hM hPl hfm hslot hcov hcap hext hSC + (by simp [DecArmS1aEnt, DecArmQH]) (fun _ h => by cases h) (fun _ h => by cases h) + hdec0 σ3 m + have J3_fin : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s3 a3 σ3 m := by + have hZ_eq : ({ Z with hw := Z.hw } : XSetsA) = Z := by cases Z; rfl + have h := J3T.toJA_vcpuA J1a hSV3 R6_3 osk3 sysl3 symL3 + rwa [hZ_eq] at h + -- Spec decode pin for `ARMVCPUReadState` + have heptF : ept = false := hNoS2 s1 ptr sz capability.VM_READ_WRITE false mp ept asid va hs1_idx hc_s1 + subst heptF + have hc0_eq : c0 = .archObjectCap (.frameCap (specPtr ptr) vmReadWrite (frameSizeOfA sz) false (mapDataA mp asid va)) := by + have h1 := hcapRel0.2.symm + have hvm : vmRightsOfA capability.VM_READ_WRITE = vmReadWrite := by + unfold vmRightsOfA capability.VM_READ_WRITE; simp + simpa [absCapA, absFrameE, hvm] using h1 + have hframe_eq : stateWindow c0 offset.bv = some (some (specPtr (wa ptr offset))) := by + rw [hc0_eq]; unfold stateWindow; dsimp only + have h63 : (offset.bv &&& 63) = 0 := congrArg UScalar.bv hoff64_eq + have hsp_add : specPtr (wa ptr offset) = specPtr ptr + offset.bv := by + unfold specPtr; change (ptr.bv + offset.bv) + pptrShiftA = (ptr.bv + pptrShiftA) + offset.bv + rw [BitVec.add_assoc, BitVec.add_comm offset.bv pptrShiftA, ← BitVec.add_assoc] + have hsz_bv : ((1 : Word) <<< pageBitsForSize (frameSizeOfA sz)) = size1.bv := by + rw [← hbits_sp] + apply BitVec.eq_of_toNat_eq + rw [BitVec.toNat_shiftLeft, show (1 : Word).toNat = 1 from rfl, Nat.one_shiftLeft, + Nat.mod_eq_of_lt (Nat.pow_lt_pow_right (by omega) hbits_lt), ← hsize1_val]; rfl + rw [hsz_bv] + have hcond : ¬ (offset.bv &&& 63 ≠ 0 ∨ offset.bv ≥ size1.bv ∨ size1.bv - offset.bv < smSize) := by + rintro (h | h | h) + · exact h h63 + · exact hoff_ge h + · have hsub_toNat : (size1.bv - offset.bv).toNat = 2 ^ bits.val - offset.val := by + have hle : offset.bv.toNat ≤ size1.bv.toNat := by show offset.val ≤ size1.val; omega + rw [BitVec.toNat_sub_of_le hle]; show size1.val - offset.val = _; omega + have hlt : (size1.bv - offset.bv).toNat < (smSize : Word).toNat := h + have hsm : (smSize : Word).toNat = 1024 := by unfold smSize; decide + omega + rw [if_neg hcond, ← hsp_add] + have hfrd_eq : frameRightsDev c0 = some (vmReadWrite, false) := by + rw [hc0_eq]; rfl + have hargs2 : args = offset :: mask :: rest2 := by rw [hargs_eq, hrest_eq] + have hfp2 : VcpuFrontPinA Z.th σ slot mi sl (.archObjectCap (.vcpuCap (specPtr vc))) excaps buffer + ((offset :: mask :: rest2).map UScalar.bv) := hargs2 ▸ hfp + have hpin : VcpuDecPinA call Z.th σ slot + (.invokeArchObject (.vcpu (.stateTransfer (specPtr vc) (specPtr (wa ptr offset)) mask.bv true (0, [])))) := by + refine ⟨mi, sl, .archObjectCap (.vcpuCap (specPtr vc)), excaps, buffer, + (offset :: mask :: rest2).map UScalar.bv, hfp2, fun cptr => fl4DecodeInvocation_vcpu_of_archA ?_⟩ + rw [hlbl]; unfold fl4ArchDecodeInvocation + simp only [↓reduceIte, decodeVcpuStateTransfer, NonDetE.whenE, the] + have hex_ne : excaps ≠ [] := fun h => by rw [h] at hex0_eq; cases hex0_eq + simp only [List.map_cons, hex_ne, or_false, + List.getElem?_cons_zero, List.getElem?_cons_succ, hmask_ok, ne_eq, not_true_eq_false] + rw [if_neg (by simp), if_neg (by simp), hex0_eq] + dsimp only; rw [hfrd_eq, hframe_eq] + have hne_ko : vmReadWrite ≠ vmKernelOnly := fun h => by + have hr : Fl4SpecA64.Rights.allowRead ∈ vmReadWrite := by simp [vmReadWrite] + rw [h] at hr; simp [vmKernelOnly] at hr + simp [NonDetE.liftME, NonDetE.returnOk, hne_ko] + have hperf : (.ok [], σ3) ∈ ((fl4ArchPerformInvocation (Z := Unit) + (.vcpu (.stateTransfer (specPtr vc) (specPtr (wa ptr offset)) mask.bv true (0, [])))).run σ1).1 := by + unfold fl4ArchPerformInvocation fl4PerformVcpuInv + simp only [↓reduceIte, NonDetE.liftE] + have hfl : fl4VcpuFlush (Z := Unit) (specPtr vc) σ1 = ({((), σ1)}, False) := by + unfold fl4VcpuFlush NonDet.when + rw [nd_bind_oneSc (m := NonDet.gets _) rfl, J1a.curVcpuNone]; simp; rfl + change (Except.ok [], σ3) ∈ (((fl4VcpuFlush (specPtr vc) >>= + fun _ => fl4ReadStateToFrame (specPtr vc) (specPtr (wa ptr offset)) mask.bv >>= + fun _ => pure ([] : List Data)) >>= fun r => pure (Except.ok r)) σ1).1 + rw [nd_bind_oneSc (by rw [nd_bind_oneSc hfl, nd_bind_oneSc hsp3]; rfl)] + exact Set.mem_singleton _ + exact ⟨x, wa ptr offset, mask, σ3, hkx, hpin, hsts_spec, hperf, J3_fin⟩ + +/-! ### Label 60 (`ARMVCPUSetTCB`) on `JSyscallA → JStage3A` -/ + +/-- Upgrade `VcpuDelHookA.vcpu_unbind_stage3TA` from `fl4VcpuUnbind` to `fl4VcpuUnbindExt` +(zeroing `ownerCpu := 0` in spec when bound; concrete state is unchanged). -/ +private theorem vcpu_unbind_ext_stage3TA + {J7 : JSlotA S} [J7PpA st J7] [J7PpKA st J7] [J7FrA st J7] + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + (hSV : SchedValidA σ) (R6 : VcpuRec6A st Z OS Bf s σ) + (hP : VcpuPlatAgreeA ar) (hAK : VcpuArchKeepsA ar AR) (hVR : VcpuVmRootA st ar AR) + (hCs : ∀ c ∈ Z.Cs, c.val < Fl4SpecA64.fl4A64Platform.numNodes) + (h1 : Fl4SpecA64.fl4A64Platform.numNodes = 1) + {vc : U64} (hvc : vc ∈ Z.V) (hvc_nz : vc ≠ 0#u64) : + vcpu.unbind st ar s a vc ⦃ res => + ∃ (hw' : HwRootA) (σ' : Fl4SpecA64.State Unit), + fl4VcpuUnbindExt (specPtr vc) σ = ({((), σ')}, False) ∧ + JStage3TA st AR { Z with hw := hw' } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 res.1 res.2 σ' m ∧ + SchedValidA σ' ∧ + VcpuRec6A st { Z with hw := hw' } OS Bf res.1 σ' ∧ + L.mem res.1 vc = 0#u64 ∧ + L.mem res.1 (wa vc 24#u64) = 0#u64 ∧ + (L.mem s vc ≠ 0#u64 → L.mem res.1 (wa (L.mem s vc) 3664#u64) = 0#u64) ∧ + (∀ v ∈ Z.V, v ≠ vc → L.mem s v = 0#u64 → L.mem res.1 v = 0#u64) ∧ + L.ksb res.1 = L.ksb s ∧ L.cpu res.1 = L.cpu s ∧ + OSKeep st OS s res.1 ⦄ := by + have hunb := vcpu_unbind_stage3TA (Clock := fun a _ => ∃ h, AR a h) J hSV R6 ⟨Z.hw, J.j.1.ar⟩ + (fun s' a' _ u ⟨h, har⟩ => WP.spec_mono (hVR.set_vm_root s' a' h u har) (fun _ ⟨_, h', har'⟩ => ⟨h', har'⟩)) + (fun a' _ v ⟨h, har⟩ => WP.spec_mono (hAK.vcpu_flush a' h v har) (fun _ har' => ⟨h, har'⟩)) + (fun a' _ v om ⟨h, har⟩ => WP.spec_mono (hAK.vcpu_flush_state a' h v om har) (fun _ har' => ⟨h, har'⟩)) + (fun _ _ _ hc _ _ => hc) hP hAK hVR hCs h1 hvc hvc_nz + -- Evaluate `fl4VcpuEndRun (specPtr vc) σ` deterministically and show `ve.vcpuTcb = v0.vcpuTcb`. + obtain ⟨v0, hkv0, hvt0, hrc0⟩ := J.j.2.1.obj vc hvc + have hend : ∃ σe ve, fl4VcpuEndRun (Z := Unit) (specPtr vc) σ = ({((), σe)}, False) ∧ + σe.archState.armCurrentVcpu = none ∧ + σe.kheap (specPtr vc) = some (.archObj (.vcpu ve)) ∧ ve.vcpuTcb = v0.vcpuTcb ∧ + (∀ v ∈ Z.V, L.mem s v = 0#u64 → ∃ ve', σe.kheap (specPtr v) = some (.archObj (.vcpu ve')) ∧ ve'.vcpuTcb = none) ∧ + (∀ t ∈ Z.T, ∀ xt, σ.kheap (specPtr t) = some (.tcb xt) → + ∃ xt', σe.kheap (specPtr t) = some (.tcb xt') ∧ xt'.tcbArch.tcbVcpu = xt.tcbArch.tcbVcpu) := by + by_cases hrc_z : L.mem s (wa vc 24#u64) = 0#u64 + · have hrc_n : v0.vcpuExt.runCaller = none := by rw [hrc0, hrc_z]; rfl + refine ⟨σ, v0, fl4VcpuEndRun_noneA hkv0 hrc_n, J.curVcpuNone, hkv0, rfl, + fun v hv hvz => by + obtain ⟨xv, hkv', hvt', -⟩ := J.j.2.1.obj v hv + exact ⟨xv, hkv', by rw [hvt', hvz]; rfl⟩, + fun _ _ xt hkt => ⟨xt, hkt, rfl⟩⟩ + · set rc := L.mem s (wa vc 24#u64) + have hrcT : rc ∈ Z.T := (J.vcRun.vcpu vc hvc).resolve_left hrc_z + obtain ⟨xrc, hkrc, -, -⟩ := J.j.2.1.tcb rc hrcT + have hrc_s : v0.vcpuExt.runCaller = some (specPtr rc) := by + rw [hrc0]; unfold optRefA; rw [if_neg hrc_z] + by_cases hvm : xrc.tcbState = .ext .runningVM + · have hcr := vcpu_complete_run_stage3TA (Clock := fun a _ => ∃ h, AR a h) J hSV R6 ⟨Z.hw, J.j.1.ar⟩ + (fun s' a' _ u ⟨h, har⟩ => WP.spec_mono (hVR.set_vm_root s' a' h u har) (fun _ ⟨_, h', har'⟩ => ⟨h', har'⟩)) + (fun a' _ v om ⟨h, har⟩ => WP.spec_mono (hAK.vcpu_flush_state a' h v om har) (fun _ har' => ⟨h, har'⟩)) + (fun _ _ _ hc _ _ => hc) hP hAK hVR hrcT + (show fl4_abi.consts.vcpu_run_stop.INTERRUPT.bv = RunStop.interrupt.word by + unfold fl4_abi.consts.vcpu_run_stop.INTERRUPT RunStop.word; rfl) 0#u64 0#u64 0#u64 + obtain ⟨res_c, -, hw_c, σe, hcr_eq, Je, -, -, -, -, -, -, -, hvc_eq, -, -, -, -, -, -, ht_pres, -⟩ := spec_ex_fd hcr + obtain ⟨ve, hkve, hve_t, -⟩ := Je.j.2.1.obj vc hvc + refine ⟨σe, ve, by rw [fl4VcpuEndRun_runningVmA hkv0 hkrc hrc_s hvm]; exact hcr_eq, + Je.curVcpuNone, hkve, by rw [hve_t, hvc_eq vc hvc, ← hvt0], + fun v hv hvz => by + obtain ⟨ve', hkve', hve'_t, -⟩ := Je.j.2.1.obj v hv + exact ⟨ve', hkve', by rw [hve'_t, hvc_eq v hv, hvz]; rfl⟩, + fun t ht xt hkt => by + obtain ⟨xt', hkt', htv, -⟩ := ht_pres t ht xt hkt + exact ⟨xt', hkt', htv⟩⟩ + · have hne : specPtr rc ≠ specPtr vc := fun e => by rw [e, hkv0] at hkrc; cases hkrc + refine ⟨_, { v0 with vcpuExt := { v0.vcpuExt with runCaller := none } }, + fl4VcpuEndRun_staleA hkv0 hkrc hrc_s hvm, J.curVcpuNone, + by rw [updKSc_other _ hne.symm, updKSc_same], rfl, + fun v hv hvz => ?_, fun t ht xt hkt => ?_⟩ + · obtain ⟨xv, hkv', hvt', -⟩ := J.j.2.1.obj v hv + have hv0n : xv.vcpuTcb = none := by rw [hvt', hvz]; rfl + have hne_rc : specPtr v ≠ specPtr rc := fun e => by rw [e, hkrc] at hkv'; cases hkv' + by_cases hvv : v = vc + · subst hvv; rw [hkv0] at hkv'; cases hkv' + exact ⟨{ v0 with vcpuExt := { v0.vcpuExt with runCaller := none } }, + by rw [updKSc_other _ hne_rc, updKSc_same], hv0n⟩ + · exact ⟨xv, by rw [updKSc_other _ hne_rc, updKSc_other _ (fun e => hvv (specPtr_inj e))]; exact hkv', hv0n⟩ + · have hne_vc : specPtr t ≠ specPtr vc := fun e => by rw [e, hkv0] at hkt; cases hkt + by_cases htt : t = rc + · subst htt; rw [hkrc] at hkt; cases hkt; exact ⟨_, updKSc_same _ _ _, rfl⟩ + · exact ⟨xt, by rw [updKSc_other _ (fun e => htt (specPtr_inj e)), updKSc_other _ hne_vc]; exact hkt, rfl⟩ + obtain ⟨σe, ve, hend_eq, hcur_e, hkve, hve_tcb, hve_z, hte_pres⟩ := hend + refine WP.spec_mono hunb ?_ + rintro ⟨s', a'⟩ ⟨hw', σ', hsp, J', hSV', R6', -, -, -, hvc0, hrc0', hclr, hksb, hcpu, osk, -, -, -⟩ + have hkeep0 : ∀ v ∈ Z.V, v ≠ vc → L.mem s v = 0#u64 → L.mem s' v = 0#u64 := fun v hv hne hvz => by + by_contra hnz + obtain ⟨xv', hkv', hvt', -⟩ := J'.j.2.1.obj v hv + obtain ⟨ve', hkve', hve'_n⟩ := hve_z v hv hvz + unfold fl4VcpuUnbind at hsp + rw [nd_bind_oneSc hend_eq, nd_bind_oneSc (getVcpu_eqA hkve), hve_tcb] at hsp + by_cases hbd0 : L.mem s vc = 0#u64 + · have : v0.vcpuTcb = none := by rw [hvt0, hbd0]; rfl + rw [this] at hsp; dsimp only at hsp + cases (Prod.mk.inj (Set.singleton_eq_singleton_iff.mp (Prod.mk.inj hsp).1)).2 + rw [hkve'] at hkv'; cases hkv'; rw [hvt'] at hve'_n; unfold optRefA at hve'_n; rw [if_neg hnz] at hve'_n; cases hve'_n + · have hv0s : v0.vcpuTcb = some (specPtr (L.mem s vc)) := by rw [hvt0]; unfold optRefA; rw [if_neg hbd0] + rw [hv0s] at hsp; dsimp only at hsp + have htT := (J.vcRun.vtcb vc hvc).resolve_left hbd0 + obtain ⟨xt0, hkt0, htv0, -⟩ := J.j.2.1.tcb _ htT + obtain ⟨xte, hkte, htve⟩ := hte_pres _ htT xt0 hkt0 + have htv_eq : xte.tcbArch.tcbVcpu = some (specPtr vc) := by + rw [htve, htv0, R6.vcBack vc hvc hbd0]; unfold optRefA; rw [if_neg hvc_nz] + have hdiss := dissociateVcpuTcb_eqA hcur_e hkve hkte (hve_tcb.trans hv0s) htv_eq + rw [hdiss] at hsp + cases (Prod.mk.inj (Set.singleton_eq_singleton_iff.mp (Prod.mk.inj hsp).1)).2 + have hne_vc : specPtr v ≠ specPtr vc := fun e => hne (specPtr_inj e) + have hne_t : specPtr v ≠ specPtr (L.mem s vc) := fun e => by rw [e, hkte] at hkve'; cases hkve' + rw [updKSc_other _ hne_vc, updKSc_other _ hne_t, hkve'] at hkv'; cases hkv' + rw [hvt'] at hve'_n; unfold optRefA at hve'_n; rw [if_neg hnz] at hve'_n; cases hve'_n + by_cases hbound : L.mem s vc = 0#u64 + · have hv0_none : v0.vcpuTcb = none := by rw [hvt0, hbound]; rfl + have hsp_ext : fl4VcpuUnbindExt (specPtr vc) σ = ({((), σ')}, False) := by + unfold fl4VcpuUnbind at hsp; unfold fl4VcpuUnbindExt + rw [nd_bind_oneSc hend_eq, nd_bind_oneSc (getVcpu_eqA hkve), hve_tcb, hv0_none] at hsp ⊢ + exact hsp + exact ⟨hw', σ', hsp_ext, J', hSV', R6', hvc0, hrc0', hclr, hkeep0, hksb, hcpu, osk⟩ + · have hv0_some : v0.vcpuTcb = some (specPtr (L.mem s vc)) := by + rw [hvt0]; unfold optRefA; rw [if_neg hbound] + obtain ⟨v', hkv', hvt', hrc'⟩ := J'.j.2.1.obj vc hvc + set v'' : Fl4SpecA64.Vcpu := { v' with vcpuExt := { v'.vcpuExt with ownerCpu := 0 } } + set σ'' := updKSc σ' (specPtr vc) (.archObj (.vcpu v'')) + have hsp_ext : fl4VcpuUnbindExt (specPtr vc) σ = ({((), σ'')}, False) := by + unfold fl4VcpuUnbind at hsp; unfold fl4VcpuUnbindExt + rw [nd_bind_oneSc hend_eq, nd_bind_oneSc (getVcpu_eqA hkve), hve_tcb, hv0_some] at hsp ⊢ + dsimp only at hsp ⊢ + unfold fl4DissociateVcpuTcb + rw [nd_bind_oneSc hsp] + unfold fl4VcpuExtUpdate + rw [nd_bind_oneSc (getVcpu_eqA hkv')] + exact setVcpu_eqA hkv' + have hHS : VcpuSpecStepA H σ' σ'' := + ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, + fun p => by + by_cases hp : p = specPtr vc + · subst hp; exact Or.inr (Or.inr ⟨v', v'', hkv', updKSc_same _ _ _, rfl, rfl, rfl, rfl, rfl, rfl⟩) + · exact Or.inl (updKSc_other _ hp), + fun _ hne => absurd rfl hne⟩ + have hw_refl : WritesIn st s' s' (VcpuWrNoStA st { Z with hw := hw' } OS s') := + ⟨rfl, rfl, fun _ _ _ => rfl⟩ + have hVcCore'' : VcpuCoreA (L := L) { Z with hw := hw' } s' σ'' := by + refine ⟨fun v hv => ?_, fun p x hkp => ?_, fun t ht => ?_, J'.j.2.1.cap⟩ + · by_cases hvp : v = vc + · subst hvp; exact ⟨v'', updKSc_same _ _ _, hvt', hrc'⟩ + · obtain ⟨x, hkx, h1, h2⟩ := J'.j.2.1.obj v hv + exact ⟨x, by rw [updKSc_other _ (fun e => hvp (specPtr_inj e))]; exact hkx, h1, h2⟩ + · by_cases hp : p = specPtr vc + · subst hp; exact ⟨vc, hvc, rfl⟩ + · exact J'.j.2.1.back p x (by rwa [updKSc_other _ hp] at hkp) + · obtain ⟨x, hkx, h1, h2⟩ := J'.j.2.1.tcb t ht + have hne : specPtr t ≠ specPtr vc := fun e => by rw [e, hkv'] at hkx; cases hkx + exact ⟨x, by rw [updKSc_other _ hne]; exact hkx, h1, h2⟩ + have hEpTcb'' : ∀ t ∈ Z.T, ∃ x, σ''.kheap (specPtr t) = some (.tcb x) ∧ TcbRelE Rp (L.mem s') t x := fun t ht => by + obtain ⟨x, hkx, hrel⟩ := J'.ep.tcb t ht + have hne : specPtr t ≠ specPtr vc := fun e => by rw [e, hkv'] at hkx; cases hkx + exact ⟨x, by rw [updKSc_other _ hne]; exact hkx, hrel⟩ + have hDeadVcpu'' : ∀ v ∈ Z.V, DeadA st OS .vcpu s' v → ∀ x, σ''.kheap (specPtr v) = some (.archObj (.vcpu x)) → + x.vcpuTcb = none := fun v hv hd x hx => by + by_cases hvp : v = vc + · subst hvp; rw [updKSc_same] at hx; cases hx; exact J'.deadExt.vcpu v hv hd v' hkv' + · exact J'.deadExt.vcpu v hv hd x (by rwa [updKSc_other _ (fun e => hvp (specPtr_inj e))] at hx) + have J'' := JStage3TA.of_vcpuStepA J' R6' hw_refl hHS J'.j.1.ar (fun _ _ => rfl) + hVcCore'' J'.vcRun J'.runVcpu hEpTcb'' J'.ep.buf hDeadVcpu'' J'.deadExt.tvcpu J'.deadExt.vcDeadRun J'.zombieNoVcpu + have hSV'' := SchedValidA.of_vcpuStepA hSV' hHS + have hSnap'' := VcpuStateSnapA.of_vcpuStepA J' R6'.snap hw_refl hHS fun v hv => by + obtain ⟨x, hkx, -⟩ := J'.j.2.1.obj v hv + exact (R6'.snap.snap v hv x hkx).2.2.2.2 + have hFrTh'' : FrThA st Z.th OS s' := R6'.frTh.keepA (fun _ _ => rfl) fun _ _ e => e + have hUserFr'' : UserFrSpecA st OS s' σ'' := + R6'.userFr.keepA (fun _ _ => rfl) fun _ _ _ h => hHS.keepObj h (fun _ e => by cases e) (fun _ e => by cases e) + have hR6'' : VcpuRec6A st { Z with hw := hw' } OS Bf s' σ'' := + ⟨hSnap'', R6'.vcSym1, R6'.vcSym2, R6'.vcBack, R6'.runBack, R6'.vcOS, R6'.frOff, hUserFr'', hFrTh'', R6'.bfSpec⟩ + exact ⟨hw', σ'', hsp_ext, J'', hSV'', hR6'', hvc0, hrc0', hclr, hkeep0, hksb, hcpu, osk⟩ + +/-- Two-write association step (`vc + 0 := ptr`, `ptr + 3664 := vc`) lifted across `JStage3TA` +via `JStage3TA.of_vcpuStepA`. -/ +private theorem vcpu_assoc_stage3TA + {J7 : JSlotA S} [J7PpA st J7] [J7PpKA st J7] [J7FrA st J7] + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + (hSV : SchedValidA σ) (R6 : VcpuRec6A st Z OS Bf s σ) + {vc ptr : U64} (hvc : vc ∈ Z.V) (hptr : ptr ∈ Z.T) + (hvc_nz_all : ∀ v ∈ Z.V, v ≠ 0#u64) (ht_nz_all : ∀ t ∈ Z.T, t ≠ 0#u64) + (hvc_live : ¬ DeadA st OS .vcpu s vc) (hptr_live : ¬ DeadA st OS .tcb s ptr) + (hptr_nzomb : ∀ u ∈ OS, ∀ num, capA st s u ≠ .Zombie ptr capability.ZOMBIE_TCB num) + (hptr_not_idle : ∀ c < n, specPtr ptr ≠ m.idleThreadOf c) + (hvc_unb : L.mem s vc = 0#u64) (hptr_unb : L.mem s (wa ptr 3664#u64) = 0#u64) + (hptr_norun : L.mem s (runVcpuA ptr) = 0#u64) : + ∃ σ' : Fl4SpecA64.State Unit, + (do + archThreadSet (Z := Unit) (fun atcb => { atcb with tcbVcpu := some (specPtr vc) }) (specPtr ptr) + vcpuUpdate (Z := Unit) (specPtr vc) fun v => { v with vcpuTcb := some (specPtr ptr) } + pure ([] : List Data)) σ = ({([], σ')}, False) ∧ + let s' := L.wr (L.wr s vc ptr) (wa ptr 3664#u64) vc + JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s' a σ' m ∧ + SchedValidA σ' ∧ + VcpuRec6A st Z OS Bf s' σ' ∧ + OSKeep st OS s s' := by + have hvc_nz : vc ≠ 0#u64 := hvc_nz_all vc hvc + have hptr_nz : ptr ≠ 0#u64 := ht_nz_all ptr hptr + obtain ⟨v0, hkv0, hvt0, hrc0⟩ := J.j.2.1.obj vc hvc + obtain ⟨xt0, hkt0, htv0, htr0⟩ := J.j.2.1.tcb ptr hptr + have hne_pt_vc : specPtr ptr ≠ specPtr vc := fun e => by rw [e, hkv0] at hkt0; cases hkt0 + set xt1 : Fl4SpecA64.Tcb := { xt0 with tcbArch := { xt0.tcbArch with tcbVcpu := some (specPtr vc) } } + set v1 : Fl4SpecA64.Vcpu := { v0 with vcpuTcb := some (specPtr ptr) } + set σ1 := updKSc σ (specPtr ptr) (.tcb xt1) + set σ' := updKSc σ1 (specPtr vc) (.archObj (.vcpu v1)) + have hsp : (do + archThreadSet (Z := Unit) (fun atcb => { atcb with tcbVcpu := some (specPtr vc) }) (specPtr ptr) + vcpuUpdate (Z := Unit) (specPtr vc) fun v => { v with vcpuTcb := some (specPtr ptr) } + pure ([] : List Data)) σ = ({([], σ')}, False) := by + have h1 : archThreadSet (Z := Unit) (fun atcb => { atcb with tcbVcpu := some (specPtr vc) }) (specPtr ptr) σ = + ({((), σ1)}, False) := archThreadSet_eqA _ hkt0 + have hkv1 : σ1.kheap (specPtr vc) = some (.archObj (.vcpu v0)) := by + rw [updKSc_other _ hne_pt_vc.symm]; exact hkv0 + have h2 : vcpuUpdate (Z := Unit) (specPtr vc) (fun v => { v with vcpuTcb := some (specPtr ptr) }) σ1 = + ({((), σ')}, False) := by + unfold vcpuUpdate; rw [nd_bind_oneSc (getVcpu_eqA hkv1)]; exact setVcpu_eqA hkv1 + rw [nd_bind_oneSc h1, nd_bind_oneSc h2]; rfl + set s1 := L.wr s vc ptr + set s' := L.wr s1 (wa ptr 3664#u64) vc + obtain ⟨_, _, -, arm, -, -, -, -, -, -⟩ := J7FrA.fr (st := st) _ _ _ _ _ _ J.j7 + have hvc_al : Aligned vc := by + have hH := arm.vh vc hvc; have hal := J.ent.heap.align vc _ hH + simp only [HKind.bits] at hal; show vc.val % 8 = 0; omega + have hptr_al3664 : Aligned (wa ptr 3664#u64) := + field_aligned (J.sq.tcbs ptr (J.tcbs ▸ hptr)) (by decide) (by decide) + have hdisj_vt : ∀ v ∈ Z.V, ∀ o : U64, o.val < 4096 → ∀ t ∈ Z.T, ∀ k : U64, k.val < 4096 → + wa v o ≠ wa t k := fun v hv o ho t ht k hk => by + have hH1 := arm.vh v hv; have hH2 : H t = some .tcb := show t ∈ H.tcbs by rw [J.tcbs]; exact ht + have hfit1 := J.ent.heap.fits v _ hH1; have hfit2 := J.ent.heap.fits t _ hH2 + simp only [HKind.bits] at hfit1 hfit2 + have hne : v ≠ t := fun e => by subst e; rw [hH1] at hH2; cases hH2 + have hdisj := J.ent.heap.disj v _ t _ hH1 hH2 hne + simp only [HKind.bits] at hdisj + intro heq + have e1 : (wa v o).val = v.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have e2 : (wa t k).val = t.val + k.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have hval := congrArg UScalar.val heq; rw [e1, e2] at hval; omega + have hne_addr : vc ≠ wa ptr 3664#u64 := by + have := hdisj_vt vc hvc 0#u64 (by decide) ptr hptr 3664#u64 (by decide) + rwa [wa_zero2] at this + have hw : WritesIn st s s' (VcpuWrNoStA st Z OS s) := by + refine ⟨by simp [s', s1], by simp [s', s1], fun b hbal hne => ?_⟩ + have hnb1 : b ≠ wa ptr 3664#u64 := fun e => hne (Or.inr (Or.inl ⟨ptr, hptr, 3664#u64, by decide, Or.inr (Or.inl rfl), e⟩)) + have hnb2 : b ≠ vc := fun e => hne (Or.inl ⟨vc, hvc, Or.inl e⟩) + simp only [s', s1] + rw [L.mem_wr_ne hptr_al3664 hbal (fun e => hnb1 (UScalar.eq_of_val_eq e.symm)), + L.mem_wr_ne hvc_al hbal (fun e => hnb2 (UScalar.eq_of_val_eq e.symm))] + have hxt_not_idle : xt0.tcbState ≠ .idleThreadState := fun hi => by + obtain ⟨c, hc, heq⟩ := J.sinv.onlyIdle (specPtr ptr) xt0 hkt0 hi + exact hptr_not_idle c hc heq + have hHS : VcpuSpecStepA H σ σ' := by + refine ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, + fun p => ?_, fun _ h => absurd rfl h⟩ + by_cases hp1 : p = specPtr vc + · subst hp1; exact Or.inr (Or.inr ⟨v0, v1, hkv0, updKSc_same _ _ _, rfl, rfl, rfl, rfl, rfl, rfl⟩) + · by_cases hp2 : p = specPtr ptr + · subst hp2 + exact Or.inr (Or.inl ⟨xt0, xt1, hkt0, by rw [updKSc_other _ hp1, updKSc_same], + ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, + fun hi => absurd hi hxt_not_idle⟩⟩) + · exact Or.inl (by rw [updKSc_other _ hp1, updKSc_other _ hp2]) + have osk := osKeep_of_vcpuWrA J R6.vcOS R6.frOff hw + have capE : ∀ u ∈ OS, capA st s' u = capA st s u := fun u hu => osk.capA hu + -- Helper reads on `s'` + have hmem_vc_same : L.mem s' vc = ptr := by + simp only [s', s1] + rw [L.mem_wr_ne hptr_al3664 hvc_al (fun e => hne_addr (UScalar.eq_of_val_eq e.symm)), L.mem_wr_same hvc_al] + have hmem_ptr3664 : L.mem s' (wa ptr 3664#u64) = vc := by + simp only [s']; exact L.mem_wr_same hptr_al3664 + have hmem_vc_other : ∀ v ∈ Z.V, v ≠ vc → L.mem s' v = L.mem s v := fun v hv hne => by + have hval : Aligned v := by + have hH := arm.vh v hv; have hal := J.ent.heap.align v _ hH + simp only [HKind.bits] at hal; show v.val % 8 = 0; omega + have hne1 : v ≠ wa ptr 3664#u64 := by + have := hdisj_vt v hv 0#u64 (by decide) ptr hptr 3664#u64 (by decide); rwa [wa_zero2] at this + simp only [s', s1] + rw [L.mem_wr_ne hptr_al3664 hval (fun e => hne1 (UScalar.eq_of_val_eq e.symm)), + L.mem_wr_ne hvc_al hval (fun e => hne (UScalar.eq_of_val_eq e.symm))] + have hmem_vc_off : ∀ v ∈ Z.V, ∀ o : U64, 0 < o.val → o.val < 4096 → o.val % 8 = 0 → + L.mem s' (wa v o) = L.mem s (wa v o) := fun v hv o ho1 ho2 ho8 => by + have hH := arm.vh v hv; have hfit := J.ent.heap.fits v _ hH; have hal := J.ent.heap.align v _ hH + simp only [HKind.bits] at hfit hal + have ev : (wa v o).val = v.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have hval : Aligned (wa v o) := by show (wa v o).val % 8 = 0; rw [ev]; omega + have hne1 : wa v o ≠ wa ptr 3664#u64 := hdisj_vt v hv o ho2 ptr hptr 3664#u64 (by decide) + have hne2 : wa v o ≠ vc := by + by_cases hvv : v = vc + · subst hvv; intro e; have := congrArg UScalar.val e; rw [ev] at this; omega + · have hH2 := arm.vh vc hvc; have hdisj := J.ent.heap.disj v _ vc _ hH hH2 hvv + simp only [HKind.bits] at hdisj + intro e; have := congrArg UScalar.val e; rw [ev] at this; omega + simp only [s', s1] + rw [L.mem_wr_ne hptr_al3664 hval (fun e => hne1 (UScalar.eq_of_val_eq e.symm)), + L.mem_wr_ne hvc_al hval (fun e => hne2 (UScalar.eq_of_val_eq e.symm))] + have hmem_tcb_other : ∀ t ∈ Z.T, ∀ k : U64, k.val < 4096 → k.val % 8 = 0 → + (t ≠ ptr ∨ k ≠ 3664#u64) → L.mem s' (wa t k) = L.mem s (wa t k) := fun t ht k hk hk8 hne => by + have htal : Aligned (wa t k) := field_aligned (J.sq.tcbs t (J.tcbs ▸ ht)) hk hk8 + have hne2 : wa t k ≠ vc := by + have := hdisj_vt vc hvc 0#u64 (by decide) t ht k hk; rw [wa_zero2] at this; exact this.symm + have hne1 : wa t k ≠ wa ptr 3664#u64 := by + have hH1 : H t = some .tcb := show t ∈ H.tcbs by rw [J.tcbs]; exact ht + have hH2 : H ptr = some .tcb := show ptr ∈ H.tcbs by rw [J.tcbs]; exact hptr + have hf1 := J.ent.heap.fits t _ hH1; have hf2 := J.ent.heap.fits ptr _ hH2 + simp only [HKind.bits] at hf1 hf2 + have e1 : (wa t k).val = t.val + k.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have h3664 : (3664#u64).val = 3664 := rfl + have e2 : (wa ptr 3664#u64).val = ptr.val + 3664 := by rw [wa_valm, h3664]; exact Nat.mod_eq_of_lt (by omega) + by_cases htt : t = ptr + · subst htt; have hk_ne : k ≠ 3664#u64 := hne.resolve_left (not_not.mpr rfl) + intro e; apply hk_ne; apply UScalar.eq_of_val_eq + have := congrArg UScalar.val e; rw [e1, e2] at this; omega + · have hdisj := J.ent.heap.disj t _ ptr _ hH1 hH2 htt + simp only [HKind.bits] at hdisj + intro e; have := congrArg UScalar.val e; rw [e1, e2] at this; omega + simp only [s', s1] + rw [L.mem_wr_ne hptr_al3664 htal (fun e => hne1 (UScalar.eq_of_val_eq e.symm)), + L.mem_wr_ne hvc_al htal (fun e => hne2 (UScalar.eq_of_val_eq e.symm))] + have hVcCore' : VcpuCoreA (L := L) Z s' σ' := by + refine ⟨fun v hv => ?_, fun p x hkp => ?_, fun t ht => ?_, fun y hy p hc => ?_⟩ + · by_cases hvv : v = vc + · subst v + refine ⟨v1, updKSc_same _ _ _, ?_, ?_⟩ + · rw [hmem_vc_same]; unfold optRefA; rw [if_neg hptr_nz] + · rw [hmem_vc_off vc hvc 24#u64 (by decide) (by decide) (by decide)]; exact hrc0 + · obtain ⟨x, hkx, h1, h2⟩ := J.j.2.1.obj v hv + have hne1 : specPtr v ≠ specPtr vc := fun e => hvv (specPtr_inj e) + have hne2 : specPtr v ≠ specPtr ptr := fun e => by rw [e, hkt0] at hkx; cases hkx + refine ⟨x, by rw [updKSc_other _ hne1, updKSc_other _ hne2]; exact hkx, ?_, ?_⟩ + · rw [hmem_vc_other v hv hvv]; exact h1 + · rw [hmem_vc_off v hv 24#u64 (by decide) (by decide) (by decide)]; exact h2 + · by_cases hp1 : p = specPtr vc + · subst hp1; exact ⟨vc, hvc, rfl⟩ + · rw [updKSc_other _ hp1] at hkp + by_cases hp2 : p = specPtr ptr + · subst hp2; rw [updKSc_same] at hkp; cases hkp + · exact J.j.2.1.back p x (by rwa [updKSc_other _ hp2] at hkp) + · by_cases htt : t = ptr + · subst t + refine ⟨xt1, by rw [updKSc_other _ hne_pt_vc, updKSc_same], ?_, ?_⟩ + · rw [hmem_ptr3664]; unfold optRefA; rw [if_neg hvc_nz] + · unfold runVcpuA; rw [hmem_tcb_other ptr hptr 3672#u64 (by decide) (by decide) (Or.inr (by decide))]; exact htr0 + · obtain ⟨x, hkx, h1, h2⟩ := J.j.2.1.tcb t ht + have hne1 : specPtr t ≠ specPtr vc := fun e => by rw [e, hkv0] at hkx; cases hkx + have hne2 : specPtr t ≠ specPtr ptr := fun e => htt (specPtr_inj e) + refine ⟨x, by rw [updKSc_other _ hne1, updKSc_other _ hne2]; exact hkx, ?_, ?_⟩ + · rw [hmem_tcb_other t ht 3664#u64 (by decide) (by decide) (Or.inl htt)]; exact h1 + · unfold runVcpuA; rw [hmem_tcb_other t ht 3672#u64 (by decide) (by decide) (Or.inl htt)]; exact h2 + · exact J.j.2.1.cap y hy p (by rw [capAtA_eq, ← capE y (J.j5.2.1 y hy), ← capAtA_eq]; exact hc) + have hVcRun' : VcRunA st Z.T Z.V s' := by + refine ⟨fun t ht => ?_, fun v hv => ?_, fun v hv => ?_, fun t ht => ?_, fun t ht hne => ?_⟩ + · by_cases htt : t = ptr + · subst t; rw [hmem_ptr3664]; exact Or.inr hvc + · rw [hmem_tcb_other t ht 3664#u64 (by decide) (by decide) (Or.inl htt)]; exact J.vcRun.tcb t ht + · rw [hmem_vc_off v hv 24#u64 (by decide) (by decide) (by decide)]; exact J.vcRun.vcpu v hv + · by_cases hvv : v = vc + · subst v; rw [hmem_vc_same]; exact Or.inr hptr + · rw [hmem_vc_other v hv hvv]; exact J.vcRun.vtcb v hv + · unfold runVcpuA; rw [hmem_tcb_other t ht 3672#u64 (by decide) (by decide) (Or.inr (by decide))]; exact J.vcRun.run t ht + · by_cases htt : t = ptr + · subst t; unfold runVcpuA + rw [hmem_tcb_other ptr hptr 3672#u64 (by decide) (by decide) (Or.inr (by decide))]; exact hptr_norun + · rw [hmem_tcb_other t ht 3664#u64 (by decide) (by decide) (Or.inl htt)] at hne + unfold runVcpuA; rw [hmem_tcb_other t ht 3672#u64 (by decide) (by decide) (Or.inl htt)] + exact J.vcRun.disj t ht hne + have hRunVcpu' : RunVcpuInv st Z.T s' := fun t ht h => by + have hst := hmem_tcb_other t ht 3328#u64 (by decide) (by decide) (Or.inr (by decide)) + have hrv := hmem_tcb_other t ht 3672#u64 (by decide) (by decide) (Or.inr (by decide)) + unfold runVcpuA at h; rw [hrv] at h + have h0 := J.runVcpu t ht h + rw [stA_eq] at h0 ⊢; rwa [hst] + have hEpTcb' : ∀ t ∈ Z.T, ∃ x, σ'.kheap (specPtr t) = some (.tcb x) ∧ TcbRelE Rp (L.mem s') t x := fun t ht => by + obtain ⟨x, hkx, hrel⟩ := J.ep.tcb t ht + have htH : H t = some .tcb := show t ∈ H.tcbs by rw [J.tcbs]; exact ht + have hrel' : TcbRelE Rp (L.mem s') t x := + hrel.frame (J.sq.tcbs t htH) fun a ha8 ⟨hlo, hhi⟩ => by + unfold TOffN at hhi + have htfit := J.ent.heap.fits t .tcb htH; change t.val + 4096 ≤ 2 ^ 64 at htfit + have hne1 : a ≠ wa ptr 3664#u64 := by + have hpH : H ptr = some .tcb := show ptr ∈ H.tcbs by rw [J.tcbs]; exact hptr + have hpfit := J.ent.heap.fits ptr .tcb hpH; change ptr.val + 4096 ≤ 2 ^ 64 at hpfit + have h3664 : (3664#u64).val = 3664 := rfl + have ep : (wa ptr 3664#u64).val = ptr.val + 3664 := by rw [wa_valm, h3664]; exact Nat.mod_eq_of_lt (by omega) + by_cases htt : t = ptr + · subst t; intro e; rw [e, ep] at hhi; omega + · have hd := J.ent.heap.disj t .tcb ptr .tcb htH hpH htt; change t.val + 4096 ≤ ptr.val ∨ ptr.val + 4096 ≤ t.val at hd + intro e; rw [e, ep] at hlo hhi; omega + have hne2 : a ≠ vc := by + have hvH := arm.vh vc hvc; have hvfit := J.ent.heap.fits vc _ hvH; simp only [HKind.bits] at hvfit + have hne_tv : t ≠ vc := fun e => by subst e; rw [htH] at hvH; cases hvH + have hd := J.ent.heap.disj t .tcb vc _ htH hvH hne_tv; simp only [HKind.bits] at hd + intro e; rw [e] at hlo hhi; omega + simp only [s', s1] + rw [L.mem_wr_ne hptr_al3664 ha8 (fun e => hne1 (UScalar.eq_of_val_eq e.symm)), + L.mem_wr_ne hvc_al ha8 (fun e => hne2 (UScalar.eq_of_val_eq e.symm))] + by_cases htt : t = ptr + · subst t; rw [hkt0] at hkx; cases hkx + exact ⟨xt1, by rw [updKSc_other _ hne_pt_vc, updKSc_same], { hrel' with }⟩ + · have hne1 : specPtr t ≠ specPtr vc := fun e => by rw [e, hkv0] at hkx; cases hkx + have hne2 : specPtr t ≠ specPtr ptr := fun e => htt (specPtr_inj e) + exact ⟨x, by rw [updKSc_other _ hne1, updKSc_other _ hne2]; exact hkx, hrel'⟩ + have hEpBuf' : ∀ bp ∈ Bf, ∀ k : U64, k.val < 128 → + (L.mem s' (bufW bp.1 k)).bv = loadWordAt σ'.machineState.underlyingMemory (bufAddr bp.2 k.val) ∧ + (bufAddr bp.2 k.val).toNat % 8 = 0 := fun bp hbp k hk => by + have ⟨h1, h2⟩ := J.ep.buf bp hbp k hk + have hbufW : BufW Bf (bufW bp.1 k) := ⟨bp, hbp, k, hk, rfl⟩ + have hbal := J.iplaced.e.balign _ hbufW + have hne1 : bufW bp.1 k ≠ wa ptr 3664#u64 := by + intro e + have hpH : H ptr = some .tcb := show ptr ∈ H.tcbs by rw [J.tcbs]; exact hptr + have hfit_p := J.ent.heap.fits ptr .tcb hpH; change ptr.val + 4096 ≤ 2 ^ 64 at hfit_p + have h3664 : (3664#u64).val = 3664 := rfl + have ev : (wa ptr 3664#u64).val = ptr.val + 3664 := by rw [wa_valm, h3664]; exact Nat.mod_eq_of_lt (by omega) + have hbt := J.iplaced.e.bt _ hbufW ptr hptr + rw [e, ev] at hbt; omega + have hne2 : bufW bp.1 k ≠ vc := by + have := J.bufOff.vcw _ hbufW vc hvc 0#u64 (by decide) (by decide) + rwa [wa_zero2] at this + simp only [s', s1] + rw [L.mem_wr_ne hptr_al3664 hbal (fun e => hne1 (UScalar.eq_of_val_eq e.symm)), + L.mem_wr_ne hvc_al hbal (fun e => hne2 (UScalar.eq_of_val_eq e.symm))] + exact ⟨h1, h2⟩ + have hDeadVcpu' : ∀ v ∈ Z.V, DeadA st OS .vcpu s' v → ∀ x, σ'.kheap (specPtr v) = some (.archObj (.vcpu x)) → + x.vcpuTcb = none := fun v hv hd x hx => by + have hd0 : DeadA st OS .vcpu s v := fun u hu => by rw [← capE u hu]; exact hd u hu + by_cases hvv : v = vc + · subst hvv; exact absurd hd0 hvc_live + · have hne1 : specPtr v ≠ specPtr vc := fun e => hvv (specPtr_inj e) + have hne2 : specPtr v ≠ specPtr ptr := fun e => by + rw [updKSc_other _ hne1, e, updKSc_same] at hx; cases hx + rw [updKSc_other _ hne1, updKSc_other _ hne2] at hx + exact J.deadExt.vcpu v hv hd0 x hx + have hDeadTvcpu' : ∀ t ∈ Z.T, DeadA st OS .tcb s' t → L.mem s' (wa t 3664#u64) = 0#u64 := fun t ht hd => by + have hd0 : DeadA st OS .tcb s t := fun u hu => by rw [← capE u hu]; exact hd u hu + by_cases htt : t = ptr + · subst htt; exact absurd hd0 hptr_live + · rw [hmem_tcb_other t ht 3664#u64 (by decide) (by decide) (Or.inl htt)] + exact J.deadExt.tvcpu t ht hd0 + have hDeadVcRun' : ∀ v ∈ Z.V, DeadA st OS .vcpu s' v → L.mem s' (wa v 24#u64) = 0#u64 := fun v hv hd => by + have hd0 : DeadA st OS .vcpu s v := fun u hu => by rw [← capE u hu]; exact hd u hu + rw [hmem_vc_off v hv 24#u64 (by decide) (by decide) (by decide)] + exact J.deadExt.vcDeadRun v hv hd0 + have hZombNoVc' : ZombieTcbNoVcpuA st OS s' := fun z hz u num hzcap => by + have hz0 : capA st s z = .Zombie u capability.ZOMBIE_TCB num := by rw [← capE z hz]; exact hzcap + have huT : u ∈ Z.T := by rw [← J.tcbs]; exact J.zombieTcb z hz u num hz0 + have hne : u ≠ ptr := fun e => by subst e; exact hptr_nzomb z hz num hz0 + rw [hmem_tcb_other u huT 3664#u64 (by decide) (by decide) (Or.inl hne)] + exact J.zombieNoVcpu z hz u num hz0 + have J' := JStage3TA.of_vcpuStepA J R6 hw hHS J.j.1.ar + (fun b hb => hw.mem_eq (J.vsOk.al b hb) (utVs_off_vcpuWrA J hb)) + hVcCore' hVcRun' hRunVcpu' hEpTcb' hEpBuf' hDeadVcpu' hDeadTvcpu' hDeadVcRun' hZombNoVc' + have hSV' := SchedValidA.of_vcpuStepA hSV hHS + have hSnap' := VcpuStateSnapA.of_vcpuStepA J R6.snap hw hHS fun v hv => by + rw [hmem_vc_off v hv 8#u64 (by decide) (by decide) (by decide)] + obtain ⟨x, hkx, -⟩ := J.j.2.1.obj v hv + exact (R6.snap.snap v hv x hkx).2.2.2.2 + have hFrOff' : FrOff st Z.Cs OS (L.ksb s') s' := by + rw [hw.1] + intro u hu p0 sz0 r0 mp0 ept0 a0 va0 hc0 b0 hb0 + exact R6.frOff u hu p0 sz0 r0 mp0 ept0 a0 va0 ((capE u hu).symm.trans hc0) b0 hb0 + have hFrTh' : FrThA st Z.th OS s' := R6.frTh.keepA capE fun _ _ e => e + have hUserFr' : UserFrSpecA st OS s' σ' := + R6.userFr.keepA capE fun _ _ _ h => hHS.keepObj h (fun _ e => by cases e) (fun _ e => by cases e) + have hR6' : VcpuRec6A st Z OS Bf s' σ' := by + refine ⟨hSnap', fun u hu hne => ?_, fun v hv hne => ?_, fun v hv hne => ?_, fun u hu hne => ?_, + R6.vcOS, hFrOff', hUserFr', hFrTh', R6.bfSpec⟩ + · by_cases huu : u = ptr + · subst huu; rw [hmem_ptr3664, hmem_vc_same] + · rw [hmem_tcb_other u hu 3664#u64 (by decide) (by decide) (Or.inl huu)] at hne ⊢ + have hvV := (J.vcRun.tcb u hu).resolve_left hne + have hvv : L.mem s (wa u 3664#u64) ≠ vc := fun e => by + have := R6.vcSym1 u hu hne; rw [e, hvc_unb] at this; exact absurd this.symm (ht_nz_all u hu) + rw [hmem_vc_other _ hvV hvv]; exact R6.vcSym1 u hu hne + · rw [hmem_vc_off v hv 24#u64 (by decide) (by decide) (by decide)] at hne ⊢ + have huT := (J.vcRun.vcpu v hv).resolve_left hne + unfold runVcpuA + rw [hmem_tcb_other _ huT 3672#u64 (by decide) (by decide) (Or.inr (by decide))] + exact R6.vcSym2 v hv hne + · by_cases hvv : v = vc + · subst hvv; rw [hmem_vc_same, hmem_ptr3664] + · rw [hmem_vc_other v hv hvv] at hne ⊢ + have huT := (J.vcRun.vtcb v hv).resolve_left hne + have huu : L.mem s v ≠ ptr := fun e => by + have := R6.vcBack v hv hne; rw [e, hptr_unb] at this + exact absurd this.symm (hvc_nz_all v hv) + rw [hmem_tcb_other _ huT 3664#u64 (by decide) (by decide) (Or.inl huu)] + exact R6.vcBack v hv hne + · unfold runVcpuA at hne ⊢ + rw [hmem_tcb_other u hu 3672#u64 (by decide) (by decide) (Or.inr (by decide))] at hne ⊢ + have hvV := (J.vcRun.run u hu).resolve_left hne + change L.mem s' (wa (L.mem s (runVcpuA u)) 24#u64) = u + rw [hmem_vc_off _ hvV 24#u64 (by decide) (by decide) (by decide)] + exact R6.runBack u hu hne + exact ⟨σ', hsp, J', hSV', hR6', osk⟩ + +/-- **Label 60 (`ARMVCPUSetTCB`)** on `JSyscallA → JStage3A` (zero global `FrameMemRelA`). +Reuses `VcpuDelHookA.vcpu_unbind_stage3TA`, `vcpu_complete_run_stage3TA`, +`VcpuDelStepsA.vcpu_cancel_run_stage3TA`, `VcpuDelStepsA.remote_tcb_stall_singleA`, +`VcpuSetTcbJA.fl4SetTcb_rust_order_eqA`, `JStage3TA.toJA_vcpuA`, and `JSyscallA.decodeFinA`. -/ +theorem JSyscallA.vcpuSetTcbA {ar : machine.Arch A} + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hP : VcpuPlatAgreeA ar) (hAK : VcpuArchKeepsA ar AR) (hVR : VcpuVmRootA st ar AR) + (hCs : ∀ c ∈ Z.Cs, c.val < Fl4SpecA64.fl4A64Platform.numNodes) + (h1 : Fl4SpecA64.fl4A64Platform.numNodes = 1) + (hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM) + {slot vc length buf : U64} {blk call don fp : Bool} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) + (hcap : capA st s slot = .Vcpu vc) (hvc : vc ∈ Z.V) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + {mi : MessageInfo} {sl : CslotPtr} {excaps : List (Fl4SpecA64.Cap × CslotPtr)} + {buffer : Option ObjRef} {args : List Fl4SpecA64.Data} + (hfp : VcpuFrontPinA Z.th σ slot mi sl (.archObjectCap (.vcpuCap (specPtr vc))) excaps buffer args) + (hlbl : fl4LabelOf mi.miLabel = archL .armVCPUSetTCB) + (hex0 : ∀ s1, Array.index_usize extra 0#usize = ok s1 → s1 ≠ 0#u64 → + ∃ c0 sl0, excaps[0]? = some (c0, sl0) ∧ CapRelA (capA st s s1) c0) + (hNoZomb : ∀ s1 ptr, Array.index_usize extra 0#usize = ok s1 → capA st s s1 = .Thread ptr → + ∀ u ∈ OS, ∀ num, capA st s u ≠ .Zombie ptr capability.ZOMBIE_TCB num) + {s3 : S} {a3 : A} + (hdec : syscall.decode_invocation st ar s a fl4_abi.label.ARMVCPUSetTCB length slot (.Vcpu vc) + blk call don fp buf extra = ok (core.result.Result.Ok (), s3, a3)) : + ∃ (x : Fl4SpecA64.Tcb) (ptr : U64) (hw' : HwRootA) (σ3 : Fl4SpecA64.State Unit), + σ.kheap σ.curThread = some (.tcb x) ∧ + VcpuDecPinA call Z.th σ slot (.invokeArchObject (.vcpu (.setTcb (specPtr vc) (specPtr ptr)))) ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + (.ok [], σ3) ∈ ((fl4ArchPerformInvocation (Z := Unit) + (.vcpu (.setTcb (specPtr vc) (specPtr ptr)))).run + (stUpdA σ σ.curThread x .restart)).1 ∧ + JStage3A st AR { Z with hw := hw' } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s3 a3 σ3 m := by + have J3 := J.toJStage3A + obtain ⟨Pt, Ub, Pp7, Tb7, Hr, Te, Kt, Lv7, Re, bits7, KLv, W⟩ := J7A.unp J3.j7 + have hpl := J3.vcPlacedA W + have RS := J3.rsSiteA hext hslot hcov J.actW J.running J.cur + have hdec0 := hdec + unfold syscall.decode_invocation at hdec + rw [show sched.cur_thread st s = ok (L.curW s) from eq_of_spec_fd (sched.cur_thread.spec s), bind_ok] at hdec + dsimp only at hdec; unfold vcpu.decode at hdec + simp only [fl4_abi.label.ARMVCPUSetTCB] at hdec + rw [if_pos trivial] at hdec + unfold vcpu.decode_set_tcb at hdec + have hlt0 : 0 < extra.val.length := by rw [extra.property]; decide + have hs1_idx : Array.index_usize extra 0#usize = ok (extra.val[0]'hlt0) := + eq_of_spec_fd (Array.index_usize_spec extra 0#usize hlt0) + set s1 := extra.val[0]'hlt0 + rw [hs1_idx, bind_ok] at hdec + have hisn : obj.Slot.is_null s1 = ok (decide (s1 = 0#u64)) := by unfold obj.Slot.is_null; rfl + rw [hisn, bind_ok] at hdec + by_cases hs1_0 : s1 = 0#u64 + · rw [if_pos (by simp [hs1_0])] at hdec; simp [error.truncated, bind_ok] at hdec + rw [if_neg (by simp [hs1_0])] at hdec + have hs1_OS : s1 ∈ OS := hext s1 (Array.getElem_mem_toList hlt0) hs1_0 + obtain ⟨c0, sl0, hex0_eq, hcapRel0⟩ := hex0 s1 hs1_idx hs1_0 + have hcap_s1 : obj.Slot.cap st s1 s = ok (capA st s s1) := Fl4ProofsA64.P1.W7.R.ST.capA_eq s s1 + rw [hcap_s1, bind_ok] at hdec + have hthr_ex : ∃ ptr, capA st s s1 = .Thread ptr := by + cases hc : capA st s s1 <;> rw [hc] at hdec <;> + first | (simp [error.illegal, bind_ok] at hdec; done) | exact ⟨_, rfl⟩ + obtain ⟨ptr, hc_s1⟩ := hthr_ex + rw [hc_s1] at hdec hcapRel0; dsimp only at hdec + have hslot1 : IsSlotA Z.th s1 := by + by_contra hns + have hnull := J3.j5.2.2 s1 hs1_OS hns + rw [hc_s1] at hnull; cases hnull + have hptrT : ptr ∈ Z.T := by + have ho : CapObjOkA Z (capA st s s1) := J3.j.capObj hslot1 + rw [hc_s1] at ho + exact (J3.j.1.thT ptr).1 ho.2.2.2.2 + obtain ⟨_, _, -, arm3, -, -, -, -, -, -⟩ := J7FrA.fr (st := st) _ _ _ _ _ _ J3.j7 + have hvc_nz_all : ∀ v ∈ Z.V, v ≠ 0#u64 := fun v hv h0 => by + have hwin := (J3.heapWin v _ (arm3.vh v hv)).1 + rw [h0] at hwin + have h0v : (0#u64 : U64).val = 0 := rfl + have hpv := Fl4ProofsA64.P0.Consts.layout.PPTR_BASE.val_eq + rw [h0v, hpv] at hwin; omega + have ht_nz_all : ∀ t ∈ Z.T, t ≠ 0#u64 := fun t ht h0 => by + have htH : H t = some .tcb := show t ∈ H.tcbs by rw [J3.tcbs]; exact ht + have hwin := (J3.heapWin t _ htH).1 + rw [h0] at hwin + have h0v : (0#u64 : U64).val = 0 := rfl + have hpv := Fl4ProofsA64.P0.Consts.layout.PPTR_BASE.val_eq + rw [h0v, hpv] at hwin; omega + have hvc_nz : vc ≠ 0#u64 := hvc_nz_all vc hvc + have hptr_nz : ptr ≠ 0#u64 := ht_nz_all ptr hptrT + have hptr_not_idle : ∀ c < n, specPtr ptr ≠ m.idleThreadOf c := fun c hc heq => by + have hsl1_ex : ∃ sl1, specCapAtA σ sl1 = some (.threadCap (specPtr ptr)) := by + rcases hslot1 with ⟨p, ⟨r, hp, i, hi, heq1⟩ | ⟨hp, i, hi, heq1⟩⟩ + · obtain ⟨cs, hkp, hwf, hsl⟩ := J3.j.1.st.cnode p r hp + have hr64 : r ≤ 64 := by + have hfit := J3.j.1.place.fit p (.cnode r) hp + change p.val + 2 ^ (r + 5) ≤ 2 ^ 64 at hfit + by_contra h + have h1 : 2 ^ 65 ≤ 2 ^ (r + 5) := Nat.pow_le_pow_right (by omega) (by omega) + omega + have hsr := hsl (bitsOfA i r) (length_bitsOfA i r) + rw [ofBl_bitsOfA i r hr64, Nat.mod_eq_of_lt hi, ← heq1] at hsr + unfold SlotRelA at hsr; rw [capAtA_eq, hc_s1] at hsr + obtain ⟨cap1, hcap1_eq, hcr1⟩ := hsr + have hc1 : cap1 = .threadCap (specPtr ptr) := by + have := hcr1.2.symm; simpa [absCapA] using this + refine ⟨(specPtr p, bitsOfA i r), ?_⟩ + simp [specCapAtA, hkp, hwf, hcap1_eq, hc1] + · obtain ⟨xt, hkt, hsl⟩ := J3.j.1.st.tcb p hp + have hsr := hsl i hi + rw [← heq1] at hsr + unfold SlotRelA at hsr; rw [capAtA_eq, hc_s1] at hsr + obtain ⟨cap1, hcap1_eq, hcr1⟩ := hsr + have hc1 : cap1 = .threadCap (specPtr ptr) := by + have := hcr1.2.symm; simpa [absCapA] using this + refine ⟨(specPtr p, tcbCnodeIndex i), ?_⟩ + simp [specCapAtA, hkt, hcap1_eq, hc1] + obtain ⟨sl1, hc1_eq⟩ := hsl1_ex + rw [heq] at hc1_eq + exact (J3.sidle.f6 sl1).1 c hc hc1_eq + -- Step 1: `sched.set_thread_state cur TS_RESTART` + rw [show sched.cur_thread st s = ok (L.curW s) from eq_of_spec_fd (sched.cur_thread.spec s), bind_ok] at hdec + have hvc_st : ∀ v ∈ Z.V, stA (L.curW s) ≠ v ∧ stA (L.curW s) ≠ wa v 24#u64 := fun v hv => + ⟨by rw [stA_eq_wa]; have := hpl.disj_tcb v hv _ J3.sr.curT 0#u64 3328#u64 (by simp) (by simp) + rw [wa_zero2] at this; exact this.symm, + by rw [stA_eq_wa]; exact (hpl.disj_tcb v hv _ J3.sr.curT 24#u64 3328#u64 (by simp) (by simp)).symm⟩ + obtain ⟨_, _, _, _, hpl_rs⟩ := RS.pl + obtain ⟨xct, hkx, hst_ok, hsts_spec, J1, -, osk1, -⟩ := + J3.of_restartCurA hpl_rs J3.symL RS.run hnvm RS.cur RS.vs hvc_st + rw [hst_ok, bind_ok] at hdec + set s2 := L.wr s (stA (L.curW s)) sched.TS_RESTART + set σ1 := stUpdA σ σ.curThread xct .restart + have hSAct1 : SActA σ1 := by + refine ⟨fun u hu => J.sact.f1 u hu, ?_⟩ + refine ⟨{ xct with tcbState := .restart }, ?_, Or.inr (Or.inl rfl)⟩ + unfold σ1 stUpdA setObjA; simp [Function.update] + have hNoSw1 : ∀ u, σ1.schedulerAction ≠ .switchThread u := J.noSwitch + have J1T := J1.toTA hSAct1 hNoSw1 + -- Step 2: `vcpu.unbind vc` + obtain ⟨⟨s_u1, a_u1⟩, hu1_eq, hw_u1, σ_u1, hsp_u1, J_u1, hSV_u1, R6_u1, hvc0_u1, -, -, -, hksb_u1, hcpu_u1, osk_u1⟩ := + spec_ex_fd (vcpu_unbind_ext_stage3TA J1T J1.schedValid J1.vcpuRec6A hP hAK hVR hCs h1 hvc hvc_nz) + dsimp only at J_u1 R6_u1 hvc0_u1 hksb_u1 hcpu_u1 osk_u1 + rw [hu1_eq, bind_ok] at hdec + -- Step 3: `old_v ← vcpu.bound ptr`; optional `vcpu.unbind old_v` + have hbd_eq : vcpu.bound st s_u1 ptr = ok (L.mem s_u1 (wa ptr 3664#u64)) := by + unfold vcpu.bound + have hv_off : layout.tcb.VCPU = ok 3664#u64 := eq_of_spec_fd Fl4ProofsA64.P0.Consts.layout.tcb.VCPU.spec + rw [hv_off, bind_ok, lift_ok2, bind_ok, L.read_eq] + change (do let old_v ← vcpu.bound st s_u1 ptr + let (s4, a2) ← if (old_v != 0#u64) = true then vcpu.unbind st ar s_u1 a_u1 old_v else ok (s_u1, a_u1) + let (s5, a3) ← smp.remote_tcb_stall st ar s4 a2 ptr + let i ← layout.tcb.RUN_VCPU + let i1 ← lift (wa ptr i) + let i2 ← st.read s5 i1 + let (s6, a4) ← if (i2 != 0#u64) = true then do + let st_p ← obj.Tcb.state st ptr s5 + if st_p = sched.TS_RUNNING_VM then + vcpu.complete_run st ar s5 a3 ptr fl4_abi.consts.vcpu_run_stop.INTERRUPT 0#u64 0#u64 0#u64 + else vcpu.cancel_run st ar s5 a3 ptr + else ok (s5, a3) + let i3 ← lift (wa vc vcpu.TCB) + let s7 ← st.write s6 i3 ptr + let s8 ← vcpu.set_bound st s7 ptr vc + let t ← sched.cur_thread st s8 + let b1 ← obj.Tcb.Insts.CoreCmpPartialEqTcb.eq ptr t + if b1 = true then do + let (s9, a5) ← vspace.set_vm_root st ar s8 a4 ptr + ok (core.result.Result.Ok (), s9, a5) + else ok (core.result.Result.Ok (), s8, a4)) = ok (core.result.Result.Ok (), s3, a3) at hdec + rw [hbd_eq, bind_ok] at hdec + set old_v := L.mem s_u1 (wa ptr 3664#u64) + have hstep3 : ∃ (s_u2 : S) (a_u2 : A) (hw_u2 : HwRootA) (σ_u2 : Fl4SpecA64.State Unit), + (if (old_v != 0#u64) = true then vcpu.unbind st ar s_u1 a_u1 old_v else ok (s_u1, a_u1)) = ok (s_u2, a_u2) ∧ + (do let ov ← archThreadGet ArchTcb.tcbVcpu (specPtr ptr) + match ov with | some v => fl4VcpuUnbindExt v | none => pure ()) σ_u1 = ({((), σ_u2)}, False) ∧ + JStage3TA st AR { Z with hw := hw_u2 } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s_u2 a_u2 σ_u2 m ∧ + SchedValidA σ_u2 ∧ + VcpuRec6A st { Z with hw := hw_u2 } OS Bf s_u2 σ_u2 ∧ + L.mem s_u2 vc = 0#u64 ∧ + L.mem s_u2 (wa ptr 3664#u64) = 0#u64 ∧ + OSKeep st OS s_u1 s_u2 := by + obtain ⟨xp1, hkp1, htv1, -⟩ := J_u1.j.2.1.tcb ptr hptrT + by_cases hov : L.mem s_u1 (wa ptr 3664#u64) = 0#u64 + · refine ⟨s_u1, a_u1, hw_u1, σ_u1, by rw [if_neg (by simp [old_v, hov])], ?_, J_u1, hSV_u1, R6_u1, hvc0_u1, hov, + fun _ _ => ⟨rfl, rfl, rfl, rfl⟩⟩ + rw [nd_bind_oneSc (archThreadGet_eqA _ hkp1), htv1, hov]; rfl + · have hovV : old_v ∈ Z.V := (J_u1.vcRun.tcb ptr hptrT).resolve_left hov + have hov_ne_vc : old_v ≠ vc := fun e => by + have hsym := R6_u1.vcSym1 ptr hptrT hov + change L.mem s_u1 old_v = ptr at hsym + rw [e, hvc0_u1] at hsym; exact absurd hsym.symm hptr_nz + have htv1_s : xp1.tcbArch.tcbVcpu = some (specPtr old_v) := by + rw [htv1]; unfold optRefA; rw [if_neg hov] + obtain ⟨⟨s_u2, a_u2⟩, hu2_eq, hw_u2, σ_u2, hsp_u2, J_u2, hSV_u2, R6_u2, hov0_u2, -, hclr_u2, hkeep_u2, -, -, osk_u2⟩ := + spec_ex_fd (vcpu_unbind_ext_stage3TA J_u1 hSV_u1 R6_u1 hP hAK hVR hCs h1 hovV hov) + dsimp only at J_u2 R6_u2 hov0_u2 hclr_u2 hkeep_u2 osk_u2 + have hvc0_u2 : L.mem s_u2 vc = 0#u64 := hkeep_u2 vc hvc hov_ne_vc.symm hvc0_u1 + have hptr0_u2 : L.mem s_u2 (wa ptr 3664#u64) = 0#u64 := by + have hsym := R6_u1.vcSym1 ptr hptrT hov + change L.mem s_u1 old_v = ptr at hsym + have := hclr_u2 (by rw [hsym]; exact hptr_nz) + rwa [hsym] at this + refine ⟨s_u2, a_u2, hw_u2, σ_u2, by rw [if_pos (by simp [old_v, hov]), hu2_eq], ?_, J_u2, hSV_u2, R6_u2, + hvc0_u2, hptr0_u2, osk_u2⟩ + rw [nd_bind_oneSc (archThreadGet_eqA _ hkp1), htv1_s]; exact hsp_u2 + obtain ⟨s_u2, a_u2, hw_u2, σ_u2, hu2_eq, hsp_u2, J_u2, hSV_u2, R6_u2, hvc0_u2, hptr0_u2, osk_u2⟩ := hstep3 + rw [hu2_eq, bind_ok] at hdec + change (do let (s5, a3) ← smp.remote_tcb_stall st ar s_u2 a_u2 ptr + let i ← layout.tcb.RUN_VCPU + let i1 ← lift (wa ptr i) + let i2 ← st.read s5 i1 + let (s6, a4) ← if (i2 != 0#u64) = true then do + let st_p ← obj.Tcb.state st ptr s5 + if st_p = sched.TS_RUNNING_VM then + vcpu.complete_run st ar s5 a3 ptr fl4_abi.consts.vcpu_run_stop.INTERRUPT 0#u64 0#u64 0#u64 + else vcpu.cancel_run st ar s5 a3 ptr + else ok (s5, a3) + let i3 ← lift (wa vc vcpu.TCB) + let s7 ← st.write s6 i3 ptr + let s8 ← vcpu.set_bound st s7 ptr vc + let t ← sched.cur_thread st s8 + let b1 ← obj.Tcb.Insts.CoreCmpPartialEqTcb.eq ptr t + if b1 = true then do + let (s9, a5) ← vspace.set_vm_root st ar s8 a4 ptr + ok (core.result.Result.Ok (), s9, a5) + else ok (core.result.Result.Ok (), s8, a4)) = ok (core.result.Result.Ok (), s3, a3) at hdec + -- Step 4: `smp.remote_tcb_stall ptr` + rw [remote_tcb_stall_singleA J_u2 hCs h1 hptrT, bind_ok] at hdec + change (do let i ← layout.tcb.RUN_VCPU + let i1 ← lift (wa ptr i) + let i2 ← st.read s_u2 i1 + let (s6, a4) ← if (i2 != 0#u64) = true then do + let st_p ← obj.Tcb.state st ptr s_u2 + if st_p = sched.TS_RUNNING_VM then + vcpu.complete_run st ar s_u2 a_u2 ptr fl4_abi.consts.vcpu_run_stop.INTERRUPT 0#u64 0#u64 0#u64 + else vcpu.cancel_run st ar s_u2 a_u2 ptr + else ok (s_u2, a_u2) + let i3 ← lift (wa vc vcpu.TCB) + let s7 ← st.write s6 i3 ptr + let s8 ← vcpu.set_bound st s7 ptr vc + let t ← sched.cur_thread st s8 + let b1 ← obj.Tcb.Insts.CoreCmpPartialEqTcb.eq ptr t + if b1 = true then do + let (s9, a5) ← vspace.set_vm_root st ar s8 a4 ptr + ok (core.result.Result.Ok (), s9, a5) + else ok (core.result.Result.Ok (), s8, a4)) = ok (core.result.Result.Ok (), s3, a3) at hdec + -- Step 5: optional `complete_run` / `cancel_run` on `ptr` + have hrv_off : layout.tcb.RUN_VCPU = ok 3672#u64 := eq_of_spec_fd Fl4ProofsA64.P0.Consts.layout.tcb.RUN_VCPU.spec + rw [hrv_off, bind_ok, lift_ok2, bind_ok, L.read_eq, bind_ok] at hdec + have hstep5 : ∃ (s_r : S) (a_r : A) (hw_r : HwRootA) (σ_r : Fl4SpecA64.State Unit), + (if (L.mem s_u2 (wa ptr 3672#u64) != 0#u64) = true then do + let st_p ← obj.Tcb.state st ptr s_u2 + if st_p = sched.TS_RUNNING_VM then + vcpu.complete_run st ar s_u2 a_u2 ptr fl4_abi.consts.vcpu_run_stop.INTERRUPT 0#u64 0#u64 0#u64 + else vcpu.cancel_run st ar s_u2 a_u2 ptr + else ok (s_u2, a_u2)) = ok (s_r, a_r) ∧ + (do let rv ← getRunVcpu (specPtr ptr) + NonDet.when (rv ≠ none) do + let stp ← getThreadState (specPtr ptr) + if stp = ThreadState.ext .runningVM then fl4CompleteRun (specPtr ptr) .interrupt 0 0 0 + else fl4CancelRun (specPtr ptr)) σ_u2 = ({((), σ_r)}, False) ∧ + JStage3TA st AR { Z with hw := hw_r } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s_r a_r σ_r m ∧ + SchedValidA σ_r ∧ + VcpuRec6A st { Z with hw := hw_r } OS Bf s_r σ_r ∧ + L.mem s_r vc = 0#u64 ∧ + L.mem s_r (wa ptr 3664#u64) = 0#u64 ∧ + L.mem s_r (runVcpuA ptr) = 0#u64 ∧ + OSKeep st OS s_u2 s_r := by + obtain ⟨xp2, hkp2, htv2, htr2⟩ := J_u2.j.2.1.tcb ptr hptrT + by_cases hrv0 : L.mem s_u2 (wa ptr 3672#u64) = 0#u64 + · have htr2_n : xp2.tcbExt.tcbRunVcpu = none := by rw [htr2]; unfold runVcpuA; rw [hrv0]; rfl + refine ⟨s_u2, a_u2, hw_u2, σ_u2, by rw [if_neg (by simp [hrv0])], ?_, J_u2, hSV_u2, R6_u2, + hvc0_u2, hptr0_u2, hrv0, fun _ _ => ⟨rfl, rfl, rfl, rfl⟩⟩ + rw [nd_bind_oneSc (getRunVcpu_eqA hkp2), htr2_n]; simp [NonDet.when]; rfl + · have htr2_s : xp2.tcbExt.tcbRunVcpu ≠ none := by + rw [htr2]; unfold runVcpuA optRefA; rw [if_neg hrv0]; exact Option.some_ne_none _ + have hst_p : obj.Tcb.state st ptr s_u2 = ok (L.mem s_u2 (stA ptr)) := by + unfold obj.Tcb.state; rw [lift_ok2, bind_ok, L.read_eq]; rfl + have hst_iff : L.mem s_u2 (stA ptr) = sched.TS_RUNNING_VM ↔ xp2.tcbState = .ext .runningVM := by + have hcode := (J_u2.ep.at_tcb hptrT hkp2).st + have h8 : sched.TS_RUNNING_VM.val = 8 := by unfold sched.TS_RUNNING_VM; rfl + constructor + · intro h; rw [h, h8] at hcode + cases hs : xp2.tcbState with + | ext e => cases e; rfl + | _ => rw [hs] at hcode; simp [tsCode] at hcode + · intro h; rw [h] at hcode; simp [tsCode] at hcode + apply UScalar.eq_of_val_eq; rw [h8]; exact hcode + by_cases hvm : L.mem s_u2 (stA ptr) = sched.TS_RUNNING_VM + · have hcr := vcpu_complete_run_stage3TA (Clock := fun a _ => ∃ h, AR a h) J_u2 hSV_u2 R6_u2 + ⟨hw_u2, J_u2.j.1.ar⟩ + (fun s' a' _ u ⟨h, har⟩ => WP.spec_mono (hVR.set_vm_root s' a' h u har) (fun _ ⟨_, h', har'⟩ => ⟨h', har'⟩)) + (fun a' _ v om ⟨h, har⟩ => WP.spec_mono (hAK.vcpu_flush_state a' h v om har) (fun _ har' => ⟨h, har'⟩)) + (fun _ _ _ hc _ _ => hc) hP hAK hVR hptrT + (show fl4_abi.consts.vcpu_run_stop.INTERRUPT.bv = RunStop.interrupt.word by + simp [fl4_abi.consts.vcpu_run_stop.INTERRUPT, RunStop.word]) 0#u64 0#u64 0#u64 + obtain ⟨⟨s_r, a_r⟩, hcr_eq, hw_r, σ_r, hsp_r, J_r, hSV_r, R6_r, -, -, -, hrv_r, -, hvc_eq, htcb_eq, -, -, osk_r, -, -, -⟩ := + spec_ex_fd hcr + refine ⟨s_r, a_r, hw_r, σ_r, by rw [if_pos (by simp [hrv0]), hst_p, bind_ok, if_pos hvm, hcr_eq], ?_, + J_r, hSV_r, R6_r, by rw [hvc_eq vc hvc]; exact hvc0_u2, by rw [htcb_eq ptr hptrT]; exact hptr0_u2, hrv_r, osk_r⟩ + rw [nd_bind_oneSc (getRunVcpu_eqA hkp2)]; unfold NonDet.when; rw [if_pos htr2_s] + unfold getThreadState; rw [nd_bind_oneSc (threadGet_eqSc _ hkp2), if_pos (hst_iff.mp hvm)] + exact hsp_r + · have hcan := vcpu_cancel_run_stage3TA (ar := ar) (Clock := fun _ _ => True) J_u2 hSV_u2 R6_u2 trivial hptrT + obtain ⟨⟨s_r, a_r⟩, hcan_eq, σ_r, ha_eq, hsp_r, J_r, hSV_r, R6_r, -, -, -, hrv_r, -, -, -, osk_r, hrest_r⟩ := + spec_ex_fd hcan + subst ha_eq + obtain ⟨v_rv, hkv_rv, -, -⟩ := J_u2.j.2.1.obj _ ((J_u2.vcRun.run ptr hptrT).resolve_left hrv0) + have hcan_eq_det := fl4CancelRun_someA hkp2 hkv_rv + (by rw [htr2]; unfold runVcpuA optRefA; rw [if_neg hrv0]) + rw [hcan_eq_det] at hsp_r + have hσr := (Prod.mk.inj (Set.mem_singleton_iff.mp hsp_r)).2 + have hvc0_r : L.mem s_r vc = 0#u64 := by + obtain ⟨v_r, hkv_r, hvt_r, -⟩ := J_r.j.2.1.obj vc hvc + obtain ⟨v2, hkv2, hvt2, -⟩ := J_u2.j.2.1.obj vc hvc + have hv2_n : v2.vcpuTcb = none := by rw [hvt2, hvc0_u2]; rfl + have hne_tp : specPtr ptr ≠ specPtr vc := fun e => by rw [e, hkv2] at hkp2; cases hkp2 + rw [hσr, updKSc_other _ hne_tp.symm] at hkv_r + by_cases hrv_vc : specPtr (L.mem s_u2 (runVcpuA ptr)) = specPtr vc + · rw [hrv_vc, updKSc_same] at hkv_r; cases hkv_r + rw [hrv_vc, hkv2] at hkv_rv; cases hkv_rv + rw [hvt_r] at hv2_n; unfold optRefA at hv2_n; split_ifs at hv2_n with hz; exact hz + · rw [updKSc_other _ (fun e => hrv_vc e.symm), hkv2] at hkv_r; cases hkv_r + rw [hvt_r] at hv2_n; unfold optRefA at hv2_n; split_ifs at hv2_n with hz; exact hz + have hptr0_r : L.mem s_r (wa ptr 3664#u64) = 0#u64 := by + obtain ⟨x_r, hkx_r, htv_r, -⟩ := J_r.j.2.1.tcb ptr hptrT + rw [hσr, updKSc_same] at hkx_r; cases hkx_r + have htv2_n : xp2.tcbArch.tcbVcpu = none := by rw [htv2, hptr0_u2]; rfl + rw [htv_r] at htv2_n; unfold optRefA at htv2_n; split_ifs at htv2_n with hz; exact hz + have hsp_det : fl4CancelRun (Z := Unit) (specPtr ptr) σ_u2 = ({((), σ_r)}, False) := by + rw [hcan_eq_det, hσr] + refine ⟨s_r, a_r, hw_u2, σ_r, by rw [if_pos (by simp [hrv0]), hst_p, bind_ok, if_neg hvm, hcan_eq], ?_, + J_r, hSV_r, R6_r, hvc0_r, hptr0_r, hrv_r, osk_r⟩ + rw [nd_bind_oneSc (getRunVcpu_eqA hkp2)]; unfold NonDet.when; rw [if_pos htr2_s] + unfold getThreadState; rw [nd_bind_oneSc (threadGet_eqSc _ hkp2), if_neg (mt hst_iff.mpr hvm)] + exact hsp_det + obtain ⟨s_r, a_r, hw_r, σ_r, hr_eq, hsp_r, J_r, hSV_r, R6_r, hvc0_r, hptr0_r, hrv0_r, osk_r⟩ := hstep5 + rw [hr_eq, bind_ok] at hdec + change (do let i3 ← lift (wa vc vcpu.TCB) + let s7 ← st.write s_r i3 ptr + let s8 ← vcpu.set_bound st s7 ptr vc + let t ← sched.cur_thread st s8 + let b1 ← obj.Tcb.Insts.CoreCmpPartialEqTcb.eq ptr t + if b1 = true then do + let (s9, a5) ← vspace.set_vm_root st ar s8 a_r ptr + ok (core.result.Result.Ok (), s9, a5) + else ok (core.result.Result.Ok (), s8, a_r)) = ok (core.result.Result.Ok (), s3, a3) at hdec + -- Step 6: Link writes (`vc + 0 := ptr`, `ptr + 3664 := vc`) via `vcpu_assoc_stage3TA` + have osk_all : OSKeep st OS s s_r := + fun u hu => ⟨by rw [(osk_r u hu).1, (osk_u2 u hu).1, (osk_u1 u hu).1, (osk1 u hu).1], + by rw [(osk_r u hu).2.1, (osk_u2 u hu).2.1, (osk_u1 u hu).2.1, (osk1 u hu).2.1], + by rw [(osk_r u hu).2.2.1, (osk_u2 u hu).2.2.1, (osk_u1 u hu).2.2.1, (osk1 u hu).2.2.1], + by rw [(osk_r u hu).2.2.2, (osk_u2 u hu).2.2.2, (osk_u1 u hu).2.2.2, (osk1 u hu).2.2.2]⟩ + have hvc_live : ¬ DeadA st OS .vcpu s_r vc := fun hd => + hd slot hslot (by rw [osk_all.capA hslot, hcap]; rfl) + have hptr_live : ¬ DeadA st OS .tcb s_r ptr := fun hd => + hd s1 hs1_OS (by rw [osk_all.capA hs1_OS, hc_s1]; exact Or.inl rfl) + have hptr_nzomb : ∀ u ∈ OS, ∀ num, capA st s_r u ≠ .Zombie ptr capability.ZOMBIE_TCB num := fun u hu num => by + rw [osk_all.capA hu]; exact hNoZomb s1 ptr hs1_idx hc_s1 u hu num + obtain ⟨σ3, hsp_assoc, J_a, hSV_a, R6_a, osk_a⟩ := + vcpu_assoc_stage3TA J_r hSV_r R6_r hvc hptrT hvc_nz_all ht_nz_all hvc_live hptr_live hptr_nzomb hptr_not_idle + hvc0_r hptr0_r hrv0_r + have hvc_off0 : vcpu.TCB = 0#u64 := by unfold vcpu.TCB; rfl + have hsb_eq : vcpu.set_bound st (L.wr s_r vc ptr) ptr vc = ok (L.wr (L.wr s_r vc ptr) (wa ptr 3664#u64) vc) := by + unfold vcpu.set_bound + have hv_off : layout.tcb.VCPU = ok 3664#u64 := eq_of_spec_fd Fl4ProofsA64.P0.Consts.layout.tcb.VCPU.spec + rw [hv_off, bind_ok, lift_ok2, bind_ok, L.write_eq] + rw [hvc_off0, lift_ok2, bind_ok, wa_zero2, L.write_eq, bind_ok, hsb_eq, bind_ok] at hdec + set s_a := L.wr (L.wr s_r vc ptr) (wa ptr 3664#u64) vc + -- Step 7: optional `vspace.set_vm_root ptr` when `ptr = cur_thread` + rw [show sched.cur_thread st s_a = ok (L.curW s_a) from eq_of_spec_fd (sched.cur_thread.spec s_a), bind_ok] at hdec + have hpeq : obj.Tcb.Insts.CoreCmpPartialEqTcb.eq ptr (L.curW s_a) = ok (decide (ptr = L.curW s_a)) := by + unfold obj.Tcb.Insts.CoreCmpPartialEqTcb.eq; rfl + rw [hpeq, bind_ok] at hdec + have hstep7 : ∃ hw_fin, + JStage3TA st AR { Z with hw := hw_fin } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s3 a3 σ3 m ∧ + VcpuRec6A st { Z with hw := hw_fin } OS Bf s3 σ3 ∧ + OSKeep st OS s2 s3 := by + have osk_2a : OSKeep st OS s2 s_a := + fun u hu => ⟨by rw [(osk_a u hu).1, (osk_r u hu).1, (osk_u2 u hu).1, (osk_u1 u hu).1], + by rw [(osk_a u hu).2.1, (osk_r u hu).2.1, (osk_u2 u hu).2.1, (osk_u1 u hu).2.1], + by rw [(osk_a u hu).2.2.1, (osk_r u hu).2.2.1, (osk_u2 u hu).2.2.1, (osk_u1 u hu).2.2.1], + by rw [(osk_a u hu).2.2.2, (osk_r u hu).2.2.2, (osk_u2 u hu).2.2.2, (osk_u1 u hu).2.2.2]⟩ + by_cases hcur : ptr = L.curW s_a + · rw [if_pos (by simp [hcur])] at hdec + obtain ⟨⟨s_vr, a_vr⟩, hvr_eq, hs_vr, hw_fin, hAR_fin⟩ := + spec_ex_fd (hVR.set_vm_root s_a a_r hw_r ptr J_a.j.1.ar) + dsimp only at hs_vr hAR_fin; subst hs_vr + rw [hvr_eq, bind_ok] at hdec + have hp := (Prod.mk.inj (okinj2 hdec)).2 + have hs3_eq := (Prod.mk.inj hp).1 + have ha3_eq := (Prod.mk.inj hp).2 + rw [← hs3_eq, ← ha3_eq] + have hHS_refl : VcpuSpecStepA H σ3 σ3 := + ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, + fun _ => Or.inl rfl, fun _ h => absurd rfl h⟩ + have hw_refl : WritesIn st s_a s_a (VcpuWrNoStA st { Z with hw := hw_r } OS s_a) := ⟨rfl, rfl, fun _ _ _ => rfl⟩ + have J_vr := JStage3TA.of_vcpuStepA (hw' := hw_fin) J_a R6_a hw_refl hHS_refl hAR_fin (fun _ _ => rfl) + { J_a.j.2.1 with } J_a.vcRun J_a.runVcpu J_a.ep.tcb J_a.ep.buf + J_a.deadExt.vcpu J_a.deadExt.tvcpu J_a.deadExt.vcDeadRun J_a.zombieNoVcpu + exact ⟨hw_fin, J_vr, { R6_a with }, osk_2a⟩ + · rw [if_neg (by simp [hcur])] at hdec + have hp := (Prod.mk.inj (okinj2 hdec)).2 + have hs3_eq := (Prod.mk.inj hp).1 + have ha3_eq := (Prod.mk.inj hp).2 + rw [← hs3_eq, ← ha3_eq] + exact ⟨hw_r, J_a, R6_a, osk_2a⟩ + obtain ⟨hw_fin, J_finT, R6_fin, osk_fin⟩ := hstep7 + obtain ⟨-, _, sysl3, symL3, -, -, -⟩ := + J.decodeFinA hA hM hPl hfm hslot hcov hcap hext hSC + (by simp [DecArmS1aEnt, DecArmQH]) (fun _ h => by cases h) (fun _ h => by cases h) + hdec0 σ3 m + have J3_fin : JStage3A st AR { Z with hw := hw_fin } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s3 a3 σ3 m := + J_finT.toJA_vcpuA J1 hSV_a R6_fin osk_fin sysl3 symL3 + -- Spec decode pin + `fl4SetTcb_rust_order_eqA` + have hc0_eq : c0 = .threadCap (specPtr ptr) := by + have := hcapRel0.2.symm; simpa [absCapA] using this + have hpin : VcpuDecPinA call Z.th σ slot + (.invokeArchObject (.vcpu (.setTcb (specPtr vc) (specPtr ptr)))) := by + refine ⟨mi, sl, .archObjectCap (.vcpuCap (specPtr vc)), excaps, buffer, args, hfp, + fun cptr => fl4DecodeInvocation_vcpu_of_archA ?_⟩ + rw [hlbl]; unfold fl4ArchDecodeInvocation + cases hex_eq : excaps with + | nil => rw [hex_eq] at hex0_eq; cases hex0_eq + | cons ep rest_ex => + rw [hex_eq] at hex0_eq; simp only [List.getElem?_cons_zero] at hex0_eq + cases hex0_eq + simp [archL, decodeVcpuSetTcb, hc0_eq, NonDetE.returnOk, ExceptT.bind, ExceptT.bindCont, NonDet.bind, NonDet.pure] + have hrust : fl4VcpuSetTcbRustOrder (specPtr vc) (specPtr ptr) σ1 = ({([], σ3)}, False) := by + unfold fl4VcpuSetTcbRustOrder + rw [nd_bind_oneSc hsp_u1] + obtain ⟨xp1, hkp1, -, -⟩ := J_u1.j.2.1.tcb ptr hptrT + rw [nd_bind_oneSc (archThreadGet_eqA _ hkp1)] at hsp_u2 ⊢ + dsimp only at hsp_u2 ⊢ + have hstep_u2 : (match xp1.tcbArch.tcbVcpu with + | some ov => fl4VcpuUnbindExt ov + | none => pure ()) σ_u1 = ({((), σ_u2)}, False) := hsp_u2 + have hpb : ∀ {α β : Type} (a : α) (f : α → SMonad Unit β) τ, (pure a >>= f) τ = f a τ := fun _ _ _ => by + change NonDet.bind (NonDet.pure _) _ _ = _; unfold NonDet.bind NonDet.pure; simp + have htail : (do + let rv ← getRunVcpu (specPtr ptr) + NonDet.when (rv ≠ none) do + let stp ← getThreadState (specPtr ptr) + if stp = ThreadState.ext .runningVM then fl4CompleteRun (specPtr ptr) .interrupt 0 0 0 + else fl4CancelRun (specPtr ptr) + archThreadSet (fun atcb => { atcb with tcbVcpu := some (specPtr vc) }) (specPtr ptr) + vcpuUpdate (specPtr vc) fun v => { v with vcpuTcb := some (specPtr ptr) } + pure ([] : List Data)) σ_u2 = ({([], σ3)}, False) := by + obtain ⟨xp2, hkp2, -, -⟩ := J_u2.j.2.1.tcb ptr hptrT + rw [nd_bind_oneSc (getRunVcpu_eqA hkp2)] at hsp_r ⊢ + rw [nd_bind_oneSc hsp_r]; exact hsp_assoc + cases hov : xp1.tcbArch.tcbVcpu with + | none => + rw [hov] at hstep_u2; dsimp only at hstep_u2 ⊢ + have hσ2 : σ_u1 = σ_u2 := (Prod.mk.inj (Set.singleton_eq_singleton_iff.mp (Prod.mk.inj hstep_u2).1)).2 + rw [hσ2]; exact htail + | some ov => + rw [hov] at hstep_u2; dsimp only at hstep_u2 ⊢ + rw [nd_bind_oneSc hstep_u2]; exact htail + have hperf_eq : fl4PerformVcpuInv (Z := Unit) (.setTcb (specPtr vc) (specPtr ptr)) σ1 = ({([], σ3)}, False) := by + obtain ⟨v1_0, hkv1_0, -, -⟩ := J1.j.2.1.obj vc hvc + obtain ⟨xt1_0, hkt1_0, -, -⟩ := J1.j.2.1.tcb ptr hptrT + exact fl4SetTcb_rust_order_eqA (inT := fun p => ∃ t ∈ Z.T, specPtr t = p) ⟨ptr, hptrT, rfl⟩ + J1.curVcpuNone hkv1_0 hkt1_0 + (fun v' xv p hkv' hvtp => by + obtain ⟨v, hv, rfl⟩ := J1.j.2.1.back v' xv hkv' + obtain ⟨xv', hkv'', hvt, -⟩ := J1.j.2.1.obj v hv + rw [hkv'] at hkv''; cases hkv'' + have hnz : L.mem s2 v ≠ 0#u64 := fun hz => by rw [hz] at hvt; rw [hvtp] at hvt; cases hvt + have hp_eq : p = specPtr (L.mem s2 v) := by + rw [hvt] at hvtp; unfold optRefA at hvtp; rw [if_neg hnz] at hvtp; exact (Option.some.inj hvtp).symm + have htT : L.mem s2 v ∈ Z.T := (J1.vcRun.vtcb v hv).resolve_left hnz + obtain ⟨xp, hkp, htv, -⟩ := J1.j.2.1.tcb _ htT + refine ⟨⟨_, htT, hp_eq.symm⟩, xp, hp_eq ▸ hkp, ?_⟩ + rw [htv, J1.vcBack v hv hnz]; unfold optRefA; rw [if_neg (hvc_nz_all v hv)]) + (fun p xp v' ⟨t, ht, hp⟩ hkp htv => by + subst hp + obtain ⟨xp', hkp', htv', -⟩ := J1.j.2.1.tcb t ht + rw [hkp] at hkp'; cases hkp' + have hnz : L.mem s2 (wa t 3664#u64) ≠ 0#u64 := fun hz => by rw [hz] at htv'; rw [htv] at htv'; cases htv' + have hv_eq : v' = specPtr (L.mem s2 (wa t 3664#u64)) := by + rw [htv'] at htv; unfold optRefA at htv; rw [if_neg hnz] at htv; exact (Option.some.inj htv).symm + have hvV : L.mem s2 (wa t 3664#u64) ∈ Z.V := (J1.vcRun.tcb t ht).resolve_left hnz + obtain ⟨xv, hkv, hvt, -⟩ := J1.j.2.1.obj _ hvV + refine ⟨xv, hv_eq ▸ hkv, ?_⟩ + rw [hvt, J1.vcSym1 t ht hnz]; unfold optRefA; rw [if_neg (ht_nz_all t ht)]) + (fun v' xv c hkv' hrcp => by + obtain ⟨v, hv, rfl⟩ := J1.j.2.1.back v' xv hkv' + obtain ⟨xv', hkv'', -, hrc⟩ := J1.j.2.1.obj v hv + rw [hkv'] at hkv''; cases hkv'' + have hnz : L.mem s2 (wa v 24#u64) ≠ 0#u64 := fun hz => by rw [hz] at hrc; rw [hrcp] at hrc; cases hrc + have hc_eq : c = specPtr (L.mem s2 (wa v 24#u64)) := by + rw [hrc] at hrcp; unfold optRefA at hrcp; rw [if_neg hnz] at hrcp; exact (Option.some.inj hrcp).symm + have hcT : L.mem s2 (wa v 24#u64) ∈ Z.T := (J1.vcRun.vcpu v hv).resolve_left hnz + obtain ⟨xc, hkc, -, htr⟩ := J1.j.2.1.tcb _ hcT + refine ⟨⟨_, hcT, hc_eq.symm⟩, xc, hc_eq ▸ hkc, ?_⟩ + rw [htr, J1.vcSym2 v hv hnz]; unfold optRefA; rw [if_neg (hvc_nz_all v hv)]) + (fun c xc v' ⟨t, ht, hc⟩ hkc htr => by + subst hc + obtain ⟨xc', hkc', -, htr'⟩ := J1.j.2.1.tcb t ht + rw [hkc] at hkc'; cases hkc' + have hnz : L.mem s2 (runVcpuA t) ≠ 0#u64 := fun hz => by rw [hz] at htr'; rw [htr] at htr'; cases htr' + have hv_eq : v' = specPtr (L.mem s2 (runVcpuA t)) := by + rw [htr'] at htr; unfold optRefA at htr; rw [if_neg hnz] at htr; exact (Option.some.inj htr).symm + have hvV : L.mem s2 (runVcpuA t) ∈ Z.V := (J1.vcRun.run t ht).resolve_left hnz + obtain ⟨xv, hkv, -, hrc⟩ := J1.j.2.1.obj _ hvV + refine ⟨xv, hv_eq ▸ hkv, ?_⟩ + rw [hrc, J1.runBack t ht hnz]; unfold optRefA; rw [if_neg (ht_nz_all t ht)]) + (fun p xp ⟨t, ht, hp⟩ hkp hne => by + subst hp + obtain ⟨xp', hkp', htv', htr'⟩ := J1.j.2.1.tcb t ht + rw [hkp] at hkp'; cases hkp' + have hnz : L.mem s2 (wa t 3664#u64) ≠ 0#u64 := fun hz => hne (by rw [htv', hz]; rfl) + rw [htr', J1.vcRun.disj t ht hnz]; rfl) + (fun p xp sc ⟨u, huT, hp⟩ hkp hsc => by + subst hp + obtain ⟨xp0, hkp0, _, hsc0⟩ := J1.j.1.sch.tcb u huT + have hx_eq : xp0 = xp := KernelObject.tcb.inj (Option.some.inj (hkp0.symm.trans hkp)) + rw [← hx_eq, hsc0] at hsc + have hnz : L.mem s2 (scA u) ≠ 0#u64 := fun e => by + unfold optScA at hsc; rw [e, if_pos rfl] at hsc; cases hsc + have hscC := J1.j.1.rest.scMem u huT hnz + obtain ⟨y, k, hks, -⟩ := J1.j.1.sch.sc _ hscC + unfold optScA at hsc; rw [if_neg hnz] at hsc + rw [← Option.some.inj hsc]; exact ⟨y, k, hks⟩) + hrust + have hperf : (.ok [], σ3) ∈ ((fl4ArchPerformInvocation (Z := Unit) + (.vcpu (.setTcb (specPtr vc) (specPtr ptr)))).run σ1).1 := by + unfold fl4ArchPerformInvocation NonDetE.liftE + change (Except.ok [], σ3) ∈ ((fl4PerformVcpuInv (.setTcb (specPtr vc) (specPtr ptr)) >>= fun r => pure (Except.ok r)) σ1).1 + rw [nd_bind_oneSc hperf_eq]; exact Set.mem_singleton _ + exact ⟨xct, ptr, hw_fin, σ3, hkx, hpin, hsts_spec, hperf, J3_fin⟩ + +/-! ### `vcpu.write_state_from_frame` on `JStage3TA` (reused by `WriteState` 68 & `Run` 69) -/ + +private theorem specPtr_al3 {b : U64} (hb : Aligned b) : specPtr b &&& mask 3 = 0 := by + apply BitVec.eq_of_toNat_eq + have hm3 : (mask 3 : Word).toNat = 7 := by decide + rw [BitVec.toNat_and, hm3] + change (specPtr b).toNat &&& (2 ^ 3 - 1) = 0 + rw [Nat.and_two_pow_sub_one_eq_mod, show (2 ^ 3 : Nat) = 8 from rfl] + rw [show (8 : Nat) = 2 ^ 3 from rfl, specPtr_mod b (by omega)] + exact hb + +private theorem nd_bind_pure_r {α : Type} (m : SMonad Unit α) (σ : Fl4SpecA64.State Unit) : + (m >>= fun x => NonDet.pure x) σ = m σ := by + simp only [Bind.bind, NonDet.bind, NonDet.pure] + ext1 + · ext ⟨a, s⟩; simp + · simp + +def WfSet (vc v_tcb : U64) (b : U64) : Prop := + b = wa vc 16#u64 ∨ (∃ i < 64, b = vcpuRegWA vc i) ∨ + (v_tcb ≠ 0#u64 ∧ ∃ K : U64, K.val % 8 = 0 ∧ 256 ≤ K.val ∧ K.val < 3328 ∧ b = wa v_tcb K) + +private theorem wf_trans {s1 s2 s3 : S} {P : U64 → Prop} + (h1 : WritesIn st s1 s2 P) (h2 : WritesIn st s2 s3 P) : WritesIn st s1 s3 P := + ⟨h2.1.trans h1.1, h2.2.1.trans h1.2.1, fun q hqa hnf => (h2.2.2 q hqa hnf).trans (h1.2.2 q hqa hnf)⟩ + +private theorem vcpuSpecStep_refl (H : Heap) (σ : Fl4SpecA64.State Unit) : VcpuSpecStepA H σ σ true := + VcpuSpecStepA.refl H σ true + +private theorem vcpuSpecStep_trans {H : Heap} {σ0 σ1 σ2 : Fl4SpecA64.State Unit} + (h1 : VcpuSpecStepA H σ0 σ1 true) (h2 : VcpuSpecStepA H σ1 σ2 true) : VcpuSpecStepA H σ0 σ2 true := by + refine ⟨h2.rq.trans h1.rq, h2.rls.trans h1.rls, h2.cur.trans h1.cur, h2.idle.trans h1.idle, + h2.csc.trans h1.csc, h2.act.trans h1.act, h2.cdt.trans h1.cdt, h2.orig.trans h1.orig, + h2.irqN.trans h1.irqN, h2.irqS.trans h1.irqS, h2.dom.trans h1.dom, h2.domT.trans h1.domT, + h2.domI.trans h1.domI, h2.domL.trans h1.domL, h2.curT.trans h1.curT, + h2.consT.trans h1.consT, h2.reprog.trans h1.reprog, h2.asid.trans h1.asid, h2.vcur.trans h1.vcur, + h2.timeS.trans h1.timeS, h2.lastT.trans h1.lastT, h2.domS.trans h1.domS, h2.gvs.trans h1.gvs, fun p => ?_, fun a hne => ?_⟩ + · rcases h1.kh p with e1 | ⟨x0, x1, hk0, hk1, kf1⟩ | ⟨v0, v1, hk0, hk1, -⟩ + · rcases h2.kh p with e2 | ⟨x1, x2, hk1, hk2, kf2⟩ | ⟨v1, v2, hk1, hk2, -⟩ + · exact Or.inl (e2.trans e1) + · exact Or.inr (Or.inl ⟨x1, x2, e1 ▸ hk1, hk2, kf2⟩) + · exact Or.inr (Or.inr ⟨v1, v2, e1 ▸ hk1, hk2, trivial⟩) + · rcases h2.kh p with e2 | ⟨x1', x2, hk1', hk2, kf2⟩ | ⟨v1, v2, hk1', -, -⟩ + · exact Or.inr (Or.inl ⟨x0, x1, hk0, e2.trans hk1, kf1⟩) + · rw [hk1] at hk1'; cases hk1' + exact Or.inr (Or.inl ⟨x0, x2, hk0, hk2, + ⟨kf2.ctable.trans kf1.ctable, kf2.vtable.trans kf1.vtable, kf2.ipcfr.trans kf1.ipcfr, + kf2.fh.trans kf1.fh, kf2.th.trans kf1.th, kf2.s2.trans kf1.s2, kf2.hv.trans kf1.hv, + kf2.ibuf.trans kf1.ibuf, kf2.fault.trans kf1.fault, kf2.bnd.trans kf1.bnd, + kf2.sc.trans kf1.sc, kf2.prio.trans kf1.prio, kf2.mcp.trans kf1.mcp, kf2.dom.trans kf1.dom, + kf2.flags.trans kf1.flags, kf2.state.trans kf1.state, kf2.yield.trans kf1.yield, + fun hs hn => kf2.vcpuNone (kf1.state.trans hs) (kf1.vcpuNone hs hn)⟩⟩) + · rw [hk1] at hk1'; cases hk1' + · rcases h2.kh p with e2 | ⟨x1, x2, hk1', -, -⟩ | ⟨v1', v2, hk1', hk2, -⟩ + · exact Or.inr (Or.inr ⟨v0, v1, hk0, e2.trans hk1, trivial⟩) + · rw [hk1] at hk1'; cases hk1' + · rw [hk1] at hk1'; cases hk1' + exact Or.inr (Or.inr ⟨v0, v2, hk0, hk2, trivial⟩) + · by_cases heq : σ2.machineState.underlyingMemory a = σ1.machineState.underlyingMemory a + · exact h1.umOff a (heq ▸ hne) + · exact h2.umOff a heq + +private structure WfInv (st : machine.Store S) [L : LawfulStore st] + (Z : XSetsA) (H : Heap) (s0 : S) (σ0 : Fl4SpecA64.State Unit) + (vc v_tcb : U64) (s' : S) (σ' : Fl4SpecA64.State Unit) : Prop where + hw : WritesIn st s0 s' (WfSet vc v_tcb) + core : VcpuCoreA (L := L) Z s' σ' + snap : VcpuStateSnapA Z.V (L.mem s') σ' + regs : TcbRegsRelA (L.mem s') σ' Z.T + hHS : VcpuSpecStepA H σ0 σ' true + um : σ'.machineState.underlyingMemory = σ0.machineState.underlyingMemory + +private theorem wfInv_step_vcpu {s0 s1 s2 : S} {σ0 σ1 : Fl4SpecA64.State Unit} + {vc v_tcb : U64} (inv : WfInv st Z H s0 σ0 vc v_tcb s1 σ1) + {v1 v2 : Fl4SpecA64.Vcpu} + (hkv1 : σ1.kheap (specPtr vc) = some (.archObj (.vcpu v1))) + (_hvt : v2.vcpuTcb = v1.vcpuTcb) (_hrc : v2.vcpuExt.runCaller = v1.vcpuExt.runCaller) + (_hro : v2.vcpuExt.runOffset = v1.vcpuExt.runOffset) (_hrm : v2.vcpuExt.runOutMask = v1.vcpuExt.runOutMask) + (_hoc : v2.vcpuExt.ownerCpu = v1.vcpuExt.ownerCpu) + (hw12 : WritesIn st s1 s2 (WfSet vc v_tcb)) + (hC2 : VcpuCoreA (L := L) Z s2 (updKSc σ1 (specPtr vc) (.archObj (.vcpu v2)))) + (hS2 : VcpuStateSnapA Z.V (L.mem s2) (updKSc σ1 (specPtr vc) (.archObj (.vcpu v2)))) + (hreg_eq : ∀ t ∈ Z.T, ∀ r : Register, L.mem s2 (Endp.regA t (regIdxA r)) = L.mem s1 (Endp.regA t (regIdxA r))) : + WfInv st Z H s0 σ0 vc v_tcb s2 (updKSc σ1 (specPtr vc) (.archObj (.vcpu v2))) := by + have h12 : VcpuSpecStepA H σ1 (updKSc σ1 (specPtr vc) (.archObj (.vcpu v2))) true := + ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, + fun p => by + by_cases hp : p = specPtr vc + · subst hp; exact Or.inr (Or.inr ⟨v1, v2, hkv1, updKSc_same _ _ _, trivial⟩) + · exact Or.inl (updKSc_other _ hp), + fun _ hne => absurd rfl hne⟩ + refine ⟨wf_trans inv.hw hw12, hC2, hS2, fun t ht xt hkt r => ?_, vcpuSpecStep_trans inv.hHS h12, inv.um⟩ + have hne : specPtr t ≠ specPtr vc := fun e => by rw [e, updKSc_same] at hkt; cases hkt + rw [updKSc_other _ hne] at hkt + rw [hreg_eq t ht r]; exact inv.regs t ht xt hkt r + +private theorem wf_wr1 {s1 : S} {b w : U64} {P : U64 → Prop} (hba : Aligned b) (hb : P b) : + WritesIn st s1 (L.wr s1 b w) P := + ⟨L.ksb_wr s1 b w, L.cpu_wr s1 b w, fun _ hqa hnf => mem_wr_neA hba hqa (fun e => hnf (e ▸ hb))⟩ + +private theorem wfInv_reg_wr {Ub : Set U64} {s0 s1 : S} {σ0 σ1 : Fl4SpecA64.State Unit} + {vc v_tcb : U64} (inv : WfInv st Z H s0 σ0 vc v_tcb s1 σ1) + (hvc : vc ∈ Z.V) (hVcP : VcPlacedA Z OS Ub (L.ksb s0)) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + {v1 : Fl4SpecA64.Vcpu} (hkv1 : σ1.kheap (specPtr vc) = some (.archObj (.vcpu v1))) + {r : Nat} (hr : r < 64) (w : U64) (hw_u32 : r = vrVgicHcr → w.bv >>> 32 = 0) : + WfInv st Z H s0 σ0 vc v_tcb + (L.wr s1 (vcpuRegWA vc r) w) + (updKSc σ1 (specPtr vc) (.archObj (.vcpu (fl4VcpuRegSet r w.bv v1)))) := by + have hba : Aligned (vcpuRegWA vc r) := hVcP.vcPage_al (vcpuRegWA_in_vcPage hvc hr) + have hw12 : WritesIn st s1 (L.wr s1 (vcpuRegWA vc r) w) (WfSet vc v_tcb) := + wf_wr1 hba (Or.inr (Or.inl ⟨r, hr, rfl⟩)) + have hne_off : ∀ v ∈ Z.V, ∀ o : U64, o.val < 64 → vcpuRegWA vc r ≠ wa v o := fun v hv o ho => by + by_cases hvv : v = vc + · subst hvv + exact hVcP.same_vcpu_ne hv (by rw [vcpuRegOffA_val hr]; omega) (by omega) + (fun e => by have := congrArg UScalar.val e; rw [vcpuRegOffA_val hr] at this; omega) + · exact hVcP.disj_vcpu hvc hv (Ne.symm hvv) (by rw [vcpuRegOffA_val hr]; omega) (by omega) + have hne_tcb : ∀ t ∈ Z.T, ∀ o : U64, o.val < 4096 → vcpuRegWA vc r ≠ wa t o := fun t ht o ho => + hVcP.disj_tcb vc hvc t ht (vcpuRegOffA r) o (by rw [vcpuRegOffA_val hr]; omega) ho + have hmeta := fl4VcpuRegSet_pres_metaA r w.bv v1 + have hC2 : VcpuCoreA (L := L) Z (L.wr s1 (vcpuRegWA vc r) w) + (updKSc σ1 (specPtr vc) (.archObj (.vcpu (fl4VcpuRegSet r w.bv v1)))) := by + refine ⟨fun v hv => ?_, fun p x hkp => ?_, fun t ht => ?_, fun y hy => ?_⟩ + · obtain ⟨x, hkx, hxt, hxr⟩ := inv.core.obj v hv + have halv := hVcP.vc_al hv + rw [mem_wr_neA hba halv (by simpa [wa_zero2] using hne_off v hv 0#u64 (by decide)), + mem_wr_neA hba (aligned_wa halv 24#u64 (by decide)) (hne_off v hv 24#u64 (by decide))] + by_cases hvv : v = vc + · subst hvv; rw [hkv1] at hkx; cases hkx + exact ⟨fl4VcpuRegSet r w.bv v1, updKSc_same _ _ _, by rw [hmeta.1]; exact hxt, + by rw [hmeta.2.2.1]; exact hxr⟩ + · exact ⟨x, by rw [updKSc_other _ (fun e => hvv (specPtr_inj e))]; exact hkx, hxt, hxr⟩ + · by_cases hp : p = specPtr vc + · subst hp; exact ⟨vc, hvc, rfl⟩ + · exact inv.core.back p x (by rwa [updKSc_other _ hp] at hkp) + · obtain ⟨xt, hkt, htv, htr⟩ := inv.core.tcb t ht + have hta := hVcP.al_t t ht + have hne_s : specPtr t ≠ specPtr vc := fun e => by rw [e, hkv1] at hkt; cases hkt + exact ⟨xt, by rw [updKSc_other _ hne_s]; exact hkt, + by rw [mem_wr_neA hba (aligned_wa hta 3664#u64 (by decide)) (hne_tcb t ht 3664#u64 (by decide))]; exact htv, + by unfold runVcpuA; rw [mem_wr_neA hba (aligned_wa hta 3672#u64 (by decide)) (hne_tcb t ht 3672#u64 (by decide))]; exact htr⟩ + · have hos := hVcP.disj_os _ (vcpuRegWA_in_vcPage hvc hr) y (hsub y hy) + have haly := hVcP.al_os y (hsub y hy) + have hcap : capAtA (L.mem (L.wr s1 (vcpuRegWA vc r) w)) y = capAtA (L.mem s1) y := by + unfold capAtA + rw [mem_wr_neA hba haly hos.1, mem_wr_neA hba (aligned_wa haly 8#u64 (by decide)) hos.2.1] + rw [hcap]; exact inv.core.cap y hy + have hS2 : VcpuStateSnapA Z.V (L.mem (L.wr s1 (vcpuRegWA vc r) w)) + (updKSc σ1 (specPtr vc) (.archObj (.vcpu (fl4VcpuRegSet r w.bv v1)))) := by + refine ⟨fun v hv x hkx => ?_, fun v hv x hkx => ?_⟩ + · have halv := hVcP.vc_al hv + rw [mem_wr_neA hba (aligned_wa halv 16#u64 (by decide)) (hne_off v hv 16#u64 (by decide)), + mem_wr_neA hba (aligned_wa halv 32#u64 (by decide)) (hne_off v hv 32#u64 (by decide)), + mem_wr_neA hba (aligned_wa halv 40#u64 (by decide)) (hne_off v hv 40#u64 (by decide)), + mem_wr_neA hba (aligned_wa halv 8#u64 (by decide)) (hne_off v hv 8#u64 (by decide))] + by_cases hvv : v = vc + · subst hvv; rw [updKSc_same] at hkx; cases hkx + obtain ⟨hr0, hvp0, hro0, hrm0, how0⟩ := inv.snap.snap v hv v1 hkv1 + refine ⟨fun i hi => ?_, by rw [hmeta.2.1]; exact hvp0, + by rw [hmeta.2.2.2.1]; exact hro0, by rw [hmeta.2.2.2.2.1]; exact hrm0, how0⟩ + by_cases hir : i = r + · subst hir; rw [show wa v ⟨BitVec.ofNat 64 (64 + 8 * i)⟩ = vcpuRegWA v i from rfl, L.mem_wr_same hba, + fl4VcpuRegGet_set_sameA v1 hr w.bv] + split_ifs with h35 + · apply BitVec.eq_of_toNat_eq + have hlt : w.bv.toNat < 2 ^ 32 := by + have hshr := hw_u32 h35 + have hdiv : w.bv.toNat / 2 ^ 32 = 0 := by + have := congrArg BitVec.toNat hshr + rwa [BitVec.toNat_ushiftRight, Nat.shiftRight_eq_div_pow] at this + exact (Nat.div_eq_zero_iff.1 hdiv).resolve_left (by decide) + change w.bv.toNat = (ucast 64 (ucast 32 w.bv)).toNat + unfold ucast + rw [BitVec.toNat_setWidth, BitVec.toNat_setWidth, Nat.mod_eq_of_lt hlt, + Nat.mod_eq_of_lt (lt_trans hlt (by decide))] + · rfl + · have hne : vcpuRegWA v r ≠ vcpuRegWA v i := + hVcP.same_vcpu_ne hv (by rw [vcpuRegOffA_val hr]; omega) (by rw [vcpuRegOffA_val hi]; omega) + (fun e => by have := congrArg UScalar.val e; rw [vcpuRegOffA_val hr, vcpuRegOffA_val hi] at this; omega) + rw [show wa v ⟨BitVec.ofNat 64 (64 + 8 * i)⟩ = vcpuRegWA v i from rfl, + mem_wr_neA hba (hVcP.vcPage_al (vcpuRegWA_in_vcPage hv hi)) hne, + fl4VcpuRegGet_set_otherA v1 hr hi hir w.bv] + exact hr0 i hi + · rw [updKSc_other _ (fun e => hvv (specPtr_inj e))] at hkx + obtain ⟨hr0, hvp0, hro0, hrm0, how0⟩ := inv.snap.snap v hv x hkx + refine ⟨fun i hi => ?_, hvp0, hro0, hrm0, how0⟩ + have hne : vcpuRegWA vc r ≠ vcpuRegWA v i := + hVcP.disj_vcpu hvc hv (Ne.symm hvv) (by rw [vcpuRegOffA_val hr]; omega) (by rw [vcpuRegOffA_val hi]; omega) + rw [show wa v ⟨BitVec.ofNat 64 (64 + 8 * i)⟩ = vcpuRegWA v i from rfl, + mem_wr_neA hba (hVcP.vcPage_al (vcpuRegWA_in_vcPage hv hi)) hne] + exact hr0 i hi + · by_cases hvv : v = vc + · subst hvv; rw [updKSc_same] at hkx; cases hkx; rw [hmeta.2.2.2.2.1]; exact inv.snap.mask v hv v1 hkv1 + · rw [updKSc_other _ (fun e => hvv (specPtr_inj e))] at hkx; exact inv.snap.mask v hv x hkx + refine wfInv_step_vcpu inv hkv1 hmeta.1 hmeta.2.2.1 hmeta.2.2.2.1 hmeta.2.2.2.2.1 hmeta.2.2.2.2.2 + hw12 hC2 hS2 (fun t ht reg => ?_) + obtain ⟨hK, hwa_r⟩ := regA_eq_wa_KA t reg; have hlt := regIdxA_lt reg; rw [hwa_r] + exact mem_wr_neA hba (aligned_wa (hVcP.al_t t ht) _ (by rw [hK]; omega)) (hne_tcb t ht _ (by rw [hK]; omega)) + +private theorem wfInv_frame_eq {Ub : Set U64} {s0 s1 : S} {σ0 σ1 : Fl4SpecA64.State Unit} + {vc v_tcb frame : U64} (inv : WfInv st Z H s0 σ0 vc v_tcb s1 σ1) + (hvc : vc ∈ Z.V) (hVcP : VcPlacedA Z OS Ub (L.ksb s0)) (hWin : FrameWinA Ub frame) + (hfr_al : Aligned frame) + (hfr_disj_tcb : v_tcb ≠ 0#u64 → ∀ k < 128, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → wa frame ⟨BitVec.ofNat 64 (8 * k)⟩ ≠ wa v_tcb K) + {k : Nat} (hk : k < 128) : + L.mem s1 (wa frame ⟨BitVec.ofNat 64 (8 * k)⟩) = L.mem s0 (wa frame ⟨BitVec.ofNat 64 (8 * k)⟩) := by + have hal : Aligned (wa frame ⟨BitVec.ofNat 64 (8 * k)⟩) := + aligned_wa hfr_al _ (by + change (BitVec.ofNat 64 (8 * k)).toNat % 8 = 0 + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega : 8 * k < 2 ^ 64)]; omega) + exact inv.hw.mem_eq hal (fun hm => by + have hub := hWin.2 k hk + rcases hm with heq | ⟨i, hi, heq⟩ | ⟨hnz, K, -, hK1, hK2, heq⟩ + · exact hVcP.disj_ub _ (vcpuHdr_in_vcPage hvc (by decide) (by decide)) (heq ▸ hub) + · exact hVcP.disj_ub _ (vcpuRegWA_in_vcPage hvc hi) (heq ▸ hub) + · exact hfr_disj_tcb hnz k hk K hK1 hK2 heq) + +private theorem wfInv_copy_range {Ub : Set U64} {s0 s1 : S} {σ0 σ1 : Fl4SpecA64.State Unit} + {vc v_tcb frame lo hi : U64} + (inv : WfInv st Z H s0 σ0 vc v_tcb s1 σ1) + (hvc : vc ∈ Z.V) (hVcP : VcPlacedA Z OS Ub (L.ksb s0)) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hWin : FrameWinA Ub frame) (hfr_al : Aligned frame) + (hAgree : FrameInputAgreeA st s0 σ0 frame) + (hfr_disj_tcb : v_tcb ≠ 0#u64 → ∀ k < 128, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → wa frame ⟨BitVec.ofNat 64 (8 * k)⟩ ≠ wa v_tcb K) + (hlo : lo.val ≤ hi.val) (hhi : hi.val < 64) + (hno_hcr : ∀ r, lo.val ≤ r → r ≤ hi.val → r ≠ vrVgicHcr) : + vcpu.copy_reg_range_from_frame st s1 vc frame lo hi ⦃ s2 => + ∃ σ2, copyRegsFromFrame (Z := Unit) (specPtr vc) (specPtr frame) (idxRange lo.val hi.val) σ1 = ({((), σ2)}, False) ∧ + WfInv st Z H s0 σ0 vc v_tcb s2 σ2 ⦄ := by + have hal3 : ∀ k < 128, bufAddr (specPtr frame) k &&& mask 3 = 0 := fun k hk => by + rw [← specPtr_wa_wordA (by have := hWin.1; omega)] + exact specPtr_al3 (aligned_wa hfr_al _ (by + change (BitVec.ofNat 64 (8 * k)).toNat % 8 = 0 + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega : 8 * k < 2 ^ 64)]; omega)) + unfold vcpu.copy_reg_range_from_frame vcpu.copy_reg_range_from_frame_loop + let Inv : (S × U64) → Prop := fun (sc, r) => + lo.val ≤ r.val ∧ r.val ≤ hi.val + 1 ∧ + ∃ σc, copyRegsFromFrame (Z := Unit) (specPtr vc) (specPtr frame) (List.range' lo.val (r.val - lo.val)) σ1 = ({((), σc)}, False) ∧ + WfInv st Z H s0 σ0 vc v_tcb sc σc + let Post : S → Prop := fun s2 => + ∃ σ2, copyRegsFromFrame (Z := Unit) (specPtr vc) (specPtr frame) (idxRange lo.val hi.val) σ1 = ({((), σ2)}, False) ∧ + WfInv st Z H s0 σ0 vc v_tcb s2 σ2 + apply loop.spec_decr_nat (measure := fun p => hi.val + 1 - p.2.val) (inv := Inv) (post := Post) + swap + · refine ⟨le_refl _, by omega, σ1, ?_, inv⟩ + simp [Nat.sub_self, copyRegsFromFrame, NonDet.mapMx, NonDet.pure] + rintro ⟨sc, r⟩ ⟨hr1, hr2, σc, hsp_c, inv_c⟩ + dsimp only + unfold vcpu.copy_reg_range_from_frame_loop.body + by_cases hle : r ≤ hi + · have hr_le : r.val ≤ hi.val := hle + have hr64 : r.val < 64 := by omega + rw [if_pos hle, u64_mul_ok (by change r.val * 8 < 2 ^ 64; omega), bind_ok, lift_ok2, bind_ok, L.read_eq, bind_ok] + have hmul : (⟨BitVec.ofNat 64 (r.val * 8#u64.val)⟩ : U64) = ⟨BitVec.ofNat 64 (8 * r.val)⟩ := by + congr 1; exact congrArg (BitVec.ofNat 64) (by change r.val * 8 = 8 * r.val; omega) + rw [hmul, wfInv_frame_eq inv_c hvc hVcP hWin hfr_al hfr_disj_tcb (by omega)] + have hset := vcpu_reg_set_eqA (L := L) sc vc hr64 (L.mem s0 (wa frame ⟨BitVec.ofNat 64 (8 * r.val)⟩)) + have hr_of : (⟨BitVec.ofNat 64 r.val⟩ : U64) = r := UScalar.eq_of_val_eq (by + change (BitVec.ofNat 64 r.val).toNat = r.val; rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)]) + rw [hr_of] at hset; rw [hset, bind_ok] + have hadd : r + 1#u64 = ok ⟨BitVec.ofNat 64 (r.val + 1)⟩ := u64_add_ok (by change r.val + 1 < 2 ^ 64; omega) + rw [hadd, bind_ok, WP.spec_ok] + have hr1_val : (⟨BitVec.ofNat 64 (r.val + 1)⟩ : U64).val = r.val + 1 := by + change (BitVec.ofNat 64 (r.val + 1)).toNat = _; rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)] + obtain ⟨vc_obj, hkv_c, -, -⟩ := inv_c.core.obj vc hvc + have inv_n := wfInv_reg_wr inv_c hvc hVcP hsub hkv_c hr64 (L.mem s0 (wa frame ⟨BitVec.ofNat 64 (8 * r.val)⟩)) + (fun e => absurd e (hno_hcr r.val hr1 hr_le)) + have hload : loadWordOffs (Z := Unit) (specPtr frame) r.val σc = + ({((L.mem s0 (wa frame ⟨BitVec.ofNat 64 (8 * r.val)⟩)).bv, σc)}, False) := by + rw [Vcpu.loadWordOffs_eqA (specPtr frame) r.val σc (hal3 r.val (by omega)), + inv_c.um, ← hAgree r.val (by omega)] + refine ⟨⟨by rw [hr1_val]; omega, by rw [hr1_val]; omega, _, ?_, inv_n⟩, by dsimp only; rw [hr1_val]; omega⟩ + rw [hr1_val, show r.val + 1 - lo.val = (r.val - lo.val) + 1 by omega, List.range'_concat] + unfold copyRegsFromFrame at hsp_c ⊢ + rw [mapMx_appendA, nd_bind_oneSc hsp_c] + simp only [NonDet.mapMx, NonDet.pure] + have hidx : lo.val + 1 * (r.val - lo.val) = r.val := by omega + rw [hidx] + change (((loadWordOffs (specPtr frame) r.val >>= fun w => fl4VcpuSet (specPtr vc) r.val w) >>= fun _ => pure ()) σc) = _ + rw [nd_bind_assocSc, nd_bind_oneSc hload, nd_bind_oneSc (fl4VcpuSet_eqA r.val _ hkv_c)]; rfl + · rw [if_neg hle, WP.spec_ok] + have hr_eq : r.val = hi.val + 1 := by + have : ¬ r.val ≤ hi.val := hle; omega + refine ⟨σc, ?_, inv_c⟩ + have hidx_eq : idxRange lo.val hi.val = List.range' lo.val (r.val - lo.val) := by + unfold idxRange; congr 1; omega + rwa [hidx_eq] + +private theorem wfInv_write_lrs {Ub : Set U64} {s0 s1 : S} {σ0 σ1 : Fl4SpecA64.State Unit} + {vc v_tcb : U64} + (inv : WfInv st Z H s0 σ0 vc v_tcb s1 σ1) + (hvc : vc ∈ Z.V) (hVcP : VcPlacedA Z OS Ub (L.ksb s0)) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + {arr : Array U64 16#usize} {lrsList : List Word} + (hlen : lrsList.length = fl4NumLrs) + (harr : ∀ j, j < fl4NumLrs → (arr.val[j]!).bv = lrsList[j]!) : + vcpu.write_state_from_frame_loop0 st s1 vc arr ⟨BitVec.ofNat 64 fl4NumLrs⟩ 0#u64 ⦃ s2 => + ∃ σ2, NonDet.mapMx (fun (i, w) => fl4VcpuSet (Z := Unit) (specPtr vc) (vrVgicLr0 + i) w) + ((List.range fl4NumLrs).zip lrsList) σ1 = ({((), σ2)}, False) ∧ + WfInv st Z H s0 σ0 vc v_tcb s2 σ2 ⦄ := by + have hlrs16 : fl4NumLrs ≤ 16 := fl4NumLrs_le_16 + have hlrs_val : (⟨BitVec.ofNat 64 fl4NumLrs⟩ : U64).val = fl4NumLrs := by + change (BitVec.ofNat 64 fl4NumLrs).toNat = _; rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)] + unfold vcpu.write_state_from_frame_loop0 + let Inv : (S × U64) → Prop := fun (sc, i) => + i.val ≤ fl4NumLrs ∧ + ∃ σc, NonDet.mapMx (fun (j, w) => fl4VcpuSet (Z := Unit) (specPtr vc) (vrVgicLr0 + j) w) + ((List.range i.val).zip (lrsList.take i.val)) σ1 = ({((), σc)}, False) ∧ + WfInv st Z H s0 σ0 vc v_tcb sc σc + let Post : S → Prop := fun s2 => + ∃ σ2, NonDet.mapMx (fun (i, w) => fl4VcpuSet (Z := Unit) (specPtr vc) (vrVgicLr0 + i) w) + ((List.range fl4NumLrs).zip lrsList) σ1 = ({((), σ2)}, False) ∧ + WfInv st Z H s0 σ0 vc v_tcb s2 σ2 + apply loop.spec_decr_nat (measure := fun p => fl4NumLrs - p.2.val) (inv := Inv) (post := Post) + swap + · exact ⟨Nat.zero_le _, σ1, rfl, inv⟩ + rintro ⟨sc, i⟩ ⟨hle, σc, hsp_c, inv_c⟩ + dsimp only + unfold vcpu.write_state_from_frame_loop0.body + by_cases hlt : i < ⟨BitVec.ofNat 64 fl4NumLrs⟩ + · have hilt : i.val < fl4NumLrs := by change i.val < (⟨BitVec.ofNat 64 fl4NumLrs⟩ : U64).val at hlt; omega + have hi16 : i < 16#u64 := by change i.val < 16; omega + rw [if_pos hlt, if_pos hi16, lift_ok2, bind_ok, lift_ok2, bind_ok] + have hcast_val : (UScalar.cast .Usize i).val = i.val := by + rw [UScalar.cast_val_eq] + change i.val % 2 ^ System.Platform.numBits = i.val + exact Nat.mod_eq_of_lt (by rcases System.Platform.numBits_eq with h | h <;> rw [h] <;> omega) + have harr_len : arr.length = 16 := arr.property + refine WP.spec_bind (Array.index_usize_spec arr (UScalar.cast .Usize i) (by rw [hcast_val]; omega)) ?_ + rintro w hw_eq + have hw_bv : w.bv = lrsList[i.val]! := by + have h1 : w = arr.val[i.val]! := by + have hvl : arr.val.length = 16 := arr.property + rw [hw_eq, ← getElem!_pos arr.val (UScalar.cast .Usize i).val (by rw [hcast_val, hvl]; omega), hcast_val] + rw [h1]; exact harr i.val hilt + have hr_val : (wa fl4_abi.consts.vcpu_reg.VGIC_LR0 i).val = vrVgicLr0 + i.val := by + rw [wa_valP] + have h48 : fl4_abi.consts.vcpu_reg.VGIC_LR0.val = 48 := by unfold fl4_abi.consts.vcpu_reg.VGIC_LR0; rfl + rw [h48]; unfold vrVgicLr0; exact Nat.mod_eq_of_lt (by omega) + have hr64 : vrVgicLr0 + i.val < 64 := by unfold vrVgicLr0; omega + have hr_eq : wa fl4_abi.consts.vcpu_reg.VGIC_LR0 i = ⟨BitVec.ofNat 64 (vrVgicLr0 + i.val)⟩ := + UScalar.eq_of_val_eq (by rw [hr_val]; change _ = (BitVec.ofNat 64 _).toNat; rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)]) + rw [hr_eq, vcpu_reg_set_eqA sc vc hr64 _, bind_ok, u64_add_ok (by change i.val + 1 < 2 ^ 64; omega), bind_ok, WP.spec_ok] + have hi1_val : (⟨BitVec.ofNat 64 (i.val + 1#u64.val)⟩ : U64).val = i.val + 1 := by + change (BitVec.ofNat 64 (i.val + 1)).toNat = _; rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)] + obtain ⟨vc_obj, hkv_c, -, -⟩ := inv_c.core.obj vc hvc + have inv_n := wfInv_reg_wr inv_c hvc hVcP hsub hkv_c hr64 w + (fun e => by unfold vrVgicLr0 vrVgicHcr at e; omega) + refine ⟨⟨by rw [hi1_val]; omega, _, ?_, inv_n⟩, by dsimp only; rw [hi1_val]; omega⟩ + rw [hi1_val, List.range_succ, List.take_succ_eq_append_getElem (by omega : i.val < lrsList.length), + List.zip_append (by simp [List.length_take]; omega), mapMx_appendA, nd_bind_oneSc hsp_c] + simp only [List.zip_cons_cons, List.zip_nil_right, NonDet.mapMx, NonDet.pure] + have hel : lrsList[i.val] = w.bv := by + rw [hw_bv]; exact (getElem!_pos lrsList i.val (by omega)).symm + rw [hel] + change (fl4VcpuSet (specPtr vc) (vrVgicLr0 + i.val) w.bv >>= fun _ => pure ()) σc = _ + rw [nd_bind_oneSc (fl4VcpuSet_eqA (vrVgicLr0 + i.val) _ hkv_c)]; rfl + · rw [if_neg hlt, WP.spec_ok] + have hi_eq : i.val = fl4NumLrs := by + have : ¬ i.val < fl4NumLrs := by intro h; exact hlt (by change i.val < (⟨BitVec.ofNat 64 fl4NumLrs⟩ : U64).val; omega) + omega + refine ⟨σc, ?_, inv_c⟩ + rwa [hi_eq, show lrsList.take fl4NumLrs = lrsList from List.take_of_length_le (by omega)] at hsp_c + +set_option maxHeartbeats 600000 in +private theorem wfInv_write_gprs {Ub : Set U64} {s0 s1 : S} {σ0 σ1 : Fl4SpecA64.State Unit} + {vc v_tcb frame : U64} + (inv : WfInv st Z H s0 σ0 vc v_tcb s1 σ1) + (hvc : vc ∈ Z.V) (hvtT : v_tcb ∈ Z.T) (hvt_nz : v_tcb ≠ 0#u64) + (hVcP : VcPlacedA Z OS Ub (L.ksb s0)) (hU : UbFootDisjAtA Z OS Ub (L.ksb s0)) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hWin : FrameWinA Ub frame) (hfr_al : Aligned frame) + (hAgree : FrameInputAgreeA st s0 σ0 frame) + (hfr_disj_tcb : v_tcb ≠ 0#u64 → ∀ k < 128, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → wa frame ⟨BitVec.ofNat 64 (8 * k)⟩ ≠ wa v_tcb K) + (hDisjTcb : ∀ t1 ∈ Z.T, ∀ t2 ∈ Z.T, t1 ≠ t2 → t1.val + 4096 ≤ t2.val ∨ t2.val + 4096 ≤ t1.val) : + (do + let s2 ← vcpu.write_state_from_frame_loop1 st s1 (wa frame 512#u64) v_tcb 0#u64 + let pc ← obj.Tcb.reg st v_tcb s2 layout.reg.ELR_EL1 + obj.Tcb.set_reg st v_tcb s2 layout.reg.FAULT_IP pc) ⦃ s3 => + ∃ σ3, (do + NonDet.mapMx (fun (i, r) => do + let w ← loadWordOffs (Z := Unit) (specPtr frame) (64 + i) + asUser (specPtr v_tcb) (setRegister r (if r = Register.spsrEl1 then sanitiseVcpuSpsr w else w))) + ((List.range fl4UserRegs.length).zip fl4UserRegs) + asUser (specPtr v_tcb) do + let pc ← getRegister nextInstructionRegister + setRegister faultRegister pc) σ1 = ({((), σ3)}, False) ∧ + WfInv st Z H s0 σ0 vc v_tcb s3 σ3 ⦄ := by + have hta : TcbAddr v_tcb := hU.tcb_al v_tcb hvtT + have hal_t : Aligned v_tcb := by unfold Aligned; have := hta.align; omega + have hal3 : ∀ k < 128, bufAddr (specPtr frame) k &&& mask 3 = 0 := fun k hk => by + rw [← specPtr_wa_wordA (by have := hWin.1; omega)] + exact specPtr_al3 (aligned_wa hfr_al _ (by + change (BitVec.ofNat 64 (8 * k)).toNat % 8 = 0 + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega : 8 * k < 2 ^ 64)]; omega)) + have hstep_reg : ∀ (sc : S) (σc : Fl4SpecA64.State Unit) + (invc : WfInv st Z H s0 σ0 vc v_tcb sc σc) (r : Register) (w : U64), + ∃ σn, asUser (Z := Unit) (specPtr v_tcb) (setRegister r w.bv) σc = ({((), σn)}, False) ∧ + WfInv st Z H s0 σ0 vc v_tcb (L.wr sc (Endp.regA v_tcb (regIdxA r)) w) σn := by + intro sc σc invc r w + obtain ⟨xc, hkc, htv_c, htr_c⟩ := invc.core.tcb v_tcb hvtT + obtain ⟨hK, hwa_r⟩ := regA_eq_wa_KA v_tcb r + set Kr : U64 := wa 256#u64 (core.num.U64.wrapping_mul (regIdxA r) 8#u64) + have hlt := regIdxA_lt r + have hKr_val : Kr.val = 256 + (regIdxA r).val * 8 := hK + have hal_r : Aligned (Endp.regA v_tcb (regIdxA r)) := by + rw [hwa_r]; exact aligned_wa hal_t Kr (by rw [hKr_val]; omega) + have hw12 : WritesIn st sc (L.wr sc (Endp.regA v_tcb (regIdxA r)) w) (WfSet vc v_tcb) := by + rw [hwa_r]; exact wf_wr1 (hwa_r ▸ hal_r) (Or.inr (Or.inr ⟨hvt_nz, Kr, by rw [hKr_val]; omega, by omega, by omega, rfl⟩)) + set uc' : UserContext := { xc.tcbArch.tcbContext with userRegs := Function.update xc.tcbArch.tcbContext.userRegs r w.bv } + have hasu : asUser (Z := Unit) (specPtr v_tcb) (setRegister r w.bv) σc = ({((), ctxUpdA σc (specPtr v_tcb) xc uc')}, False) := + asUser_setReg_oneA hkc r w.bv + have hne_vc : ∀ v ∈ Z.V, ∀ o : U64, o.val < 4096 → Endp.regA v_tcb (regIdxA r) ≠ wa v o := fun v hv o ho => by + rw [hwa_r]; exact (hVcP.disj_tcb v hv v_tcb hvtT o Kr ho (by omega)).symm + have hne_t : ∀ t ∈ Z.T, ∀ o : U64, (t = v_tcb → o.val < 256 ∨ 3328 ≤ o.val) → o.val < 4096 → + Endp.regA v_tcb (regIdxA r) ≠ wa t o := fun t ht o ho1 ho2 e => by + rw [hwa_r] at e + have hft := hta.fits + have hft2 := (hU.tcb_al t ht).fits + have h1 : (wa v_tcb Kr).val = v_tcb.val + (256 + (regIdxA r).val * 8) := by + rw [wa_valP, hKr_val]; exact Nat.mod_eq_of_lt (by omega) + have h2 : (wa t o).val = t.val + o.val := by rw [wa_valP]; exact Nat.mod_eq_of_lt (by omega) + have hv := congrArg UScalar.val e; rw [h1, h2] at hv + by_cases ht_eq : t = v_tcb + · subst ht_eq; have := ho1 rfl; omega + · have := hDisjTcb v_tcb hvtT t ht (Ne.symm ht_eq); omega + have hkh_ctx : ∀ p, (ctxUpdA σc (specPtr v_tcb) xc uc').kheap p = + if p = specPtr v_tcb then some (.tcb { xc with tcbArch := { xc.tcbArch with tcbContext := uc' } }) else σc.kheap p := fun p => by + unfold ctxUpdA setObjA; dsimp only + by_cases hp : p = specPtr v_tcb + · subst hp; simp [Function.update_self] + · simp [hp, Function.update_of_ne] + have hC2 : VcpuCoreA (L := L) Z (L.wr sc (Endp.regA v_tcb (regIdxA r)) w) + (ctxUpdA σc (specPtr v_tcb) xc uc') := by + refine ⟨fun v hv => ?_, fun p x hkp => ?_, fun t ht => ?_, fun y hy => ?_⟩ + · obtain ⟨xv, hkv, hxt, hxr⟩ := invc.core.obj v hv + have hne_s : specPtr v ≠ specPtr v_tcb := fun e => by rw [e, hkc] at hkv; cases hkv + have halv := hVcP.vc_al hv + refine ⟨xv, by rw [hkh_ctx, if_neg hne_s]; exact hkv, ?_, ?_⟩ + · rw [mem_wr_neA hal_r halv (by simpa [wa_zero2] using hne_vc v hv 0#u64 (by decide))]; exact hxt + · rw [mem_wr_neA hal_r (aligned_wa halv 24#u64 (by decide)) (hne_vc v hv 24#u64 (by decide))]; exact hxr + · have hne_s : p ≠ specPtr v_tcb := fun e => by rw [e, hkh_ctx, if_pos rfl] at hkp; cases hkp + exact invc.core.back p x (by rwa [hkh_ctx, if_neg hne_s] at hkp) + · obtain ⟨xt, hkt, htv, htr⟩ := invc.core.tcb t ht + have halt := hVcP.al_t t ht + have htv' : xt.tcbArch.tcbVcpu = optRefA (L.mem (L.wr sc (Endp.regA v_tcb (regIdxA r)) w) (wa t 3664#u64)) := by + rw [mem_wr_neA hal_r (aligned_wa halt 3664#u64 (by decide)) (hne_t t ht 3664#u64 (fun _ => by decide) (by decide))]; exact htv + have htr' : xt.tcbExt.tcbRunVcpu = optRefA (L.mem (L.wr sc (Endp.regA v_tcb (regIdxA r)) w) (runVcpuA t)) := by + unfold runVcpuA at htr ⊢ + rw [mem_wr_neA hal_r (aligned_wa halt 3672#u64 (by decide)) (hne_t t ht 3672#u64 (fun _ => by decide) (by decide))]; exact htr + by_cases ht_eq : t = v_tcb + · subst ht_eq; rw [hkc] at hkt; cases hkt + exact ⟨{ xc with tcbArch := { xc.tcbArch with tcbContext := uc' } }, by rw [hkh_ctx, if_pos rfl], htv', htr'⟩ + · exact ⟨xt, by rw [hkh_ctx, if_neg (fun e => ht_eq (specPtr_inj e))]; exact hkt, htv', htr'⟩ + · have hos := hU.tcb_offOs v_tcb hvtT Kr (by omega) (by omega) y (hsub y hy) + have haly := hVcP.al_os y (hsub y hy) + have hcap : capAtA (L.mem (L.wr sc (Endp.regA v_tcb (regIdxA r)) w)) y = capAtA (L.mem sc) y := by + unfold capAtA; rw [hwa_r] + rw [mem_wr_neA (hwa_r ▸ hal_r) haly hos.1, + mem_wr_neA (hwa_r ▸ hal_r) (aligned_wa haly 8#u64 (by decide)) hos.2.1] + rw [hcap]; exact invc.core.cap y hy + have hS2 : VcpuStateSnapA Z.V (L.mem (L.wr sc (Endp.regA v_tcb (regIdxA r)) w)) + (ctxUpdA σc (specPtr v_tcb) xc uc') := by + refine ⟨fun v hv x hkx => ?_, fun v hv x hkx => ?_⟩ + · have hne_s : specPtr v ≠ specPtr v_tcb := fun e => by rw [e, hkh_ctx, if_pos rfl] at hkx; cases hkx + rw [hkh_ctx, if_neg hne_s] at hkx + have halv := hVcP.vc_al hv + obtain ⟨hr0, hvp0, hro0, hrm0, how0⟩ := invc.snap.snap v hv x hkx + refine ⟨fun i hi => ?_, + by rw [mem_wr_neA hal_r (aligned_wa halv 16#u64 (by decide)) (hne_vc v hv 16#u64 (by decide))]; exact hvp0, + by rw [mem_wr_neA hal_r (aligned_wa halv 32#u64 (by decide)) (hne_vc v hv 32#u64 (by decide))]; exact hro0, + by rw [mem_wr_neA hal_r (aligned_wa halv 40#u64 (by decide)) (hne_vc v hv 40#u64 (by decide))]; exact hrm0, + by rw [mem_wr_neA hal_r (aligned_wa halv 8#u64 (by decide)) (hne_vc v hv 8#u64 (by decide))]; exact how0⟩ + rcases vcpuRegWA_in_vcPage hv hi with ⟨v', hv', ov, hov, -, heq⟩ + rw [show wa v ⟨BitVec.ofNat 64 (64 + 8 * i)⟩ = vcpuRegWA v i from rfl, + mem_wr_neA hal_r (hVcP.vcPage_al (vcpuRegWA_in_vcPage hv hi)) (heq ▸ hne_vc v' hv' ov hov)] + exact hr0 i hi + · have hne_s : specPtr v ≠ specPtr v_tcb := fun e => by rw [e, hkh_ctx, if_pos rfl] at hkx; cases hkx + rw [hkh_ctx, if_neg hne_s] at hkx; exact invc.snap.mask v hv x hkx + have h12 : VcpuSpecStepA H σc (ctxUpdA σc (specPtr v_tcb) xc uc') true := + ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, + fun p => by + by_cases hp : p = specPtr v_tcb + · subst hp; exact Or.inr (Or.inl ⟨xc, { xc with tcbArch := { xc.tcbArch with tcbContext := uc' } }, + hkc, by rw [hkh_ctx, if_pos rfl], + ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, fun _ => id⟩⟩) + · exact Or.inl (by rw [hkh_ctx, if_neg hp]), + fun _ hne => absurd rfl hne⟩ + refine ⟨ctxUpdA σc (specPtr v_tcb) xc uc', hasu, + wf_trans invc.hw hw12, hC2, hS2, fun t ht xt hkt reg => ?_, vcpuSpecStep_trans invc.hHS h12, invc.um⟩ + by_cases ht_eq : t = v_tcb + · subst ht_eq; rw [hkh_ctx, if_pos rfl] at hkt; cases hkt + by_cases hrr : reg = r + · subst hrr; rw [L.mem_wr_same hal_r]; simp [uc'] + · obtain ⟨hKrr, hwa_rr⟩ := regA_eq_wa_KA t reg; have hltr := regIdxA_lt reg + set Kreg : U64 := wa 256#u64 (core.num.U64.wrapping_mul (regIdxA reg) 8#u64) + have hne_rr : Endp.regA t (regIdxA r) ≠ Endp.regA t (regIdxA reg) := fun e => hrr (by + rw [hwa_r, hwa_rr] at e + have h1 : (wa t Kr).val = t.val + (256 + (regIdxA r).val * 8) := by rw [wa_valP, hKr_val]; exact Nat.mod_eq_of_lt (by have := hta.fits; omega) + have h2 : (wa t Kreg).val = t.val + (256 + (regIdxA reg).val * 8) := by rw [wa_valP, hKrr]; exact Nat.mod_eq_of_lt (by have := hta.fits; omega) + have hv := congrArg UScalar.val e; rw [h1, h2] at hv + have hi_eq : (regIdxA r).val = (regIdxA reg).val := by omega + have := congrArg idxRegA hi_eq; simpa [idxRegA_regIdxA] using this.symm) + rw [mem_wr_neA hal_r (by rw [hwa_rr]; exact aligned_wa hal_t Kreg (by rw [hKrr]; omega)) hne_rr] + simp [uc', Function.update_of_ne hrr]; exact invc.regs t ht xc hkc reg + · rw [hkh_ctx, if_neg (fun e => ht_eq (specPtr_inj e))] at hkt + obtain ⟨hKrr, hwa_rr⟩ := regA_eq_wa_KA t reg; have hltr := regIdxA_lt reg + set Kreg : U64 := wa 256#u64 (core.num.U64.wrapping_mul (regIdxA reg) 8#u64) + rw [mem_wr_neA hal_r (by rw [hwa_rr]; exact aligned_wa (hVcP.al_t t ht) Kreg (by rw [hKrr]; omega)) + (by rw [hwa_rr]; exact hne_t t ht Kreg (fun e => absurd e ht_eq) (by rw [hKrr]; omega))] + exact invc.regs t ht xt hkt reg + have hloop : vcpu.write_state_from_frame_loop1 st s1 (wa frame 512#u64) v_tcb 0#u64 ⦃ s2 => + ∃ σ2, NonDet.mapMx (fun (i, r) => do + let w ← loadWordOffs (Z := Unit) (specPtr frame) (64 + i) + asUser (specPtr v_tcb) (setRegister r (if r = Register.spsrEl1 then sanitiseVcpuSpsr w else w))) + ((List.range fl4UserRegs.length).zip fl4UserRegs) σ1 = ({((), σ2)}, False) ∧ + WfInv st Z H s0 σ0 vc v_tcb s2 σ2 ⦄ := by + have hlen37 : fl4UserRegs.length = 37 := rfl + unfold vcpu.write_state_from_frame_loop1 + let Inv : (S × U64) → Prop := fun (sc, i) => + i.val ≤ 37 ∧ + ∃ σc, NonDet.mapMx (fun (j, r) => do + let w ← loadWordOffs (Z := Unit) (specPtr frame) (64 + j) + asUser (specPtr v_tcb) (setRegister r (if r = Register.spsrEl1 then sanitiseVcpuSpsr w else w))) + ((List.range i.val).zip (fl4UserRegs.take i.val)) σ1 = ({((), σc)}, False) ∧ + WfInv st Z H s0 σ0 vc v_tcb sc σc + let Post : S → Prop := fun s2 => + ∃ σ2, NonDet.mapMx (fun (i, r) => do + let w ← loadWordOffs (Z := Unit) (specPtr frame) (64 + i) + asUser (specPtr v_tcb) (setRegister r (if r = Register.spsrEl1 then sanitiseVcpuSpsr w else w))) + ((List.range fl4UserRegs.length).zip fl4UserRegs) σ1 = ({((), σ2)}, False) ∧ + WfInv st Z H s0 σ0 vc v_tcb s2 σ2 + apply loop.spec_decr_nat (measure := fun p => 37 - p.2.val) (inv := Inv) (post := Post) + swap + · exact ⟨Nat.zero_le _, σ1, rfl, inv⟩ + rintro ⟨sc, i⟩ ⟨hle, σc, hsp_c, inv_c⟩ + dsimp only + unfold vcpu.write_state_from_frame_loop1.body + by_cases hlt : i < layout.reg.COUNT + · have hilt : i.val < 37 := by unfold layout.reg.COUNT at hlt; exact hlt + rw [if_pos hlt, u64_mul_ok (by change i.val * 8 < 2 ^ 64; omega), bind_ok, lift_ok2, bind_ok, L.read_eq, bind_ok] + have hwa_g : wa (wa frame 512#u64) ⟨BitVec.ofNat 64 (i.val * 8#u64.val)⟩ = + wa frame ⟨BitVec.ofNat 64 (8 * (64 + i.val))⟩ := UScalar.eq_of_val_eq (by + have hfit := hWin.1 + rw [wa_valP, wa_valP, wa_valP] + change ((frame.val + 512) % 2 ^ 64 + (BitVec.ofNat 64 (i.val * 8)).toNat) % 2 ^ 64 = + (frame.val + (BitVec.ofNat 64 (8 * (64 + i.val))).toNat) % 2 ^ 64 + rw [BitVec.toNat_ofNat, BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega), Nat.mod_eq_of_lt (by omega), + Nat.mod_eq_of_lt (by omega)]; congr 1; omega) + rw [hwa_g, wfInv_frame_eq inv_c hvc hVcP hWin hfr_al hfr_disj_tcb (by omega)] + set w0 := L.mem s0 (wa frame ⟨BitVec.ofNat 64 (8 * (64 + i.val))⟩) + have hreg_el : fl4UserRegs[i.val]'(by rw [hlen37]; exact hilt) = idxRegA i.val := by + have : i.val < 37 := hilt; interval_cases (i.val) <;> rfl + have hidx_i : regIdxA (idxRegA i.val) = i := by + have h1 : (regIdxA (idxRegA i.val)).val = i.val := by + have : i.val < 37 := hilt; interval_cases (i.val) <;> rfl + exact UScalar.eq_of_val_eq h1 + have hsan : (if i = layout.reg.SPSR_EL1 then regs.sanitise_vcpu_spsr w0 else ok w0) = + ok (⟨if idxRegA i.val = Register.spsrEl1 then sanitiseVcpuSpsr w0.bv else w0.bv⟩ : U64) := by + by_cases hs : i = layout.reg.SPSR_EL1 + · subst hs; rw [if_pos rfl, sanitise_vcpu_spsr_eqA] + have : idxRegA layout.reg.SPSR_EL1.val = Register.spsrEl1 := by unfold layout.reg.SPSR_EL1 idxRegA; rfl + rw [this, if_pos rfl] + · rw [if_neg hs] + have hne : idxRegA i.val ≠ Register.spsrEl1 := fun e => hs (by rw [← hidx_i, e]; unfold regIdxA layout.reg.SPSR_EL1; rfl) + rw [if_neg hne] + rw [hsan, bind_ok, eq_of_spec_fd (obj.Tcb.set_reg.spec sc v_tcb i _), bind_ok, + u64_add_ok (by change i.val + 1 < 2 ^ 64; omega), bind_ok, WP.spec_ok] + obtain ⟨σn, hasu, inv_n⟩ := hstep_reg sc σc inv_c (idxRegA i.val) _ + rw [hidx_i] at inv_n + have hi1_val : (⟨BitVec.ofNat 64 (i.val + 1#u64.val)⟩ : U64).val = i.val + 1 := by + change (BitVec.ofNat 64 (i.val + 1)).toNat = _; rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)] + have hload : loadWordOffs (Z := Unit) (specPtr frame) (64 + i.val) σc = ({(w0.bv, σc)}, False) := by + rw [Vcpu.loadWordOffs_eqA (specPtr frame) (64 + i.val) σc (hal3 (64 + i.val) (by omega)), + inv_c.um, ← hAgree (64 + i.val) (by omega)] + refine ⟨⟨by rw [hi1_val]; omega, σn, ?_, inv_n⟩, by dsimp only; rw [hi1_val]; omega⟩ + rw [hi1_val, List.range_succ, List.take_succ_eq_append_getElem (by rw [hlen37]; exact hilt), + List.zip_append (by simp [List.length_take, hlen37]; omega), mapMx_appendA, nd_bind_oneSc hsp_c] + simp only [List.zip_cons_cons, List.zip_nil_right, NonDet.mapMx, hreg_el] + change ((loadWordOffs (specPtr frame) (64 + i.val) >>= fun w => + asUser (specPtr v_tcb) (setRegister (idxRegA i.val) (if idxRegA i.val = Register.spsrEl1 then sanitiseVcpuSpsr w else w))) >>= + fun _ => NonDet.pure ()) σc = _ + rw [nd_bind_assocSc, nd_bind_oneSc hload, nd_bind_oneSc hasu]; rfl + · rw [if_neg hlt, WP.spec_ok] + have hi_eq : i.val = 37 := by + have : ¬ i.val < 37 := fun h => hlt (by unfold layout.reg.COUNT; exact h); omega + refine ⟨σc, ?_, inv_c⟩ + rwa [hi_eq, show fl4UserRegs.take 37 = fl4UserRegs from List.take_of_length_le (by rw [hlen37])] at hsp_c + refine WP.spec_bind hloop ?_ + rintro s2 ⟨σ2, hsp2, inv2⟩ + rw [eq_of_spec_fd (obj.Tcb.reg.spec s2 v_tcb layout.reg.ELR_EL1), bind_ok, + eq_of_spec_fd (obj.Tcb.set_reg.spec s2 v_tcb layout.reg.FAULT_IP _), WP.spec_ok] + obtain ⟨x2, hk2, -, -⟩ := inv2.core.tcb v_tcb hvtT + have hpc_eq : (L.mem s2 (Endp.regA v_tcb layout.reg.ELR_EL1)).bv = + x2.tcbArch.tcbContext.userRegs nextInstructionRegister := by + have h1 := inv2.regs v_tcb hvtT x2 hk2 nextInstructionRegister + simpa [nextInstructionRegister, regIdxA, layout.reg.ELR_EL1] using h1 + have hfip_eq : layout.reg.FAULT_IP = regIdxA faultRegister := by unfold layout.reg.FAULT_IP regIdxA faultRegister; rfl + rw [hfip_eq] + obtain ⟨σ3, hasu3, inv3⟩ := hstep_reg s2 σ2 inv2 faultRegister (L.mem s2 (Endp.regA v_tcb layout.reg.ELR_EL1)) + refine ⟨σ3, ?_, inv3⟩ + rw [nd_bind_oneSc hsp2] + have hasu_eq : (asUser (Z := Unit) (specPtr v_tcb) do + let pc ← getRegister nextInstructionRegister + setRegister faultRegister pc) σ2 = + asUser (Z := Unit) (specPtr v_tcb) (setRegister faultRegister (x2.tcbArch.tcbContext.userRegs nextInstructionRegister)) σ2 := by + have hg : NonDet.getsThe (getTcb (Z := Unit) (specPtr v_tcb)) σ2 = ({(x2, σ2)}, False) := by + simp [NonDet.getsThe, getTcb, readObject, hk2, NonDet.assertOpt]; rfl + have hpure : ∀ {α} (x : α), (pure x : SMonad Unit α) σ2 = ({(x, σ2)}, False) := fun _ => rfl + unfold asUser + rw [nd_bind_oneSc hg, nd_bind_oneSc (hpure _), nd_bind_oneSc hg, nd_bind_oneSc (hpure _)] + congr 1; congr 1 + unfold getRegister setRegister nextInstructionRegister faultRegister archTcbContextGet + simp [Bind.bind, NonDet.instMonad, NonDet.bind, NonDet.gets, NonDet.modify] + rw [hasu_eq, ← hpc_eq]; exact hasu3 + +private theorem copyRegsFromFrame_lrs_comm2 {vc frame : ObjRef} {σ : Fl4SpecA64.State Unit} + {v : Fl4SpecA64.Vcpu} (hkv : σ.kheap vc = some (.archObj (.vcpu v))) + (hal : ∀ k < 128, bufAddr frame k &&& mask 3 = 0) + (lrs : List (Nat × Word)) (hlrs : ∀ p ∈ lrs, p.1 < 16) + (regs : List Nat) (hregs : ∀ r ∈ regs, r < 48) : + (do NonDet.mapMx (fun (p : Nat × Word) => fl4VcpuSet (Z := Unit) vc (vrVgicLr0 + p.1) p.2) lrs + copyRegsFromFrame (Z := Unit) vc frame regs) σ = + (do copyRegsFromFrame (Z := Unit) vc frame regs + NonDet.mapMx (fun (p : Nat × Word) => fl4VcpuSet (Z := Unit) vc (vrVgicLr0 + p.1) p.2) lrs) σ := by + have h1 : ∀ (r : Nat) (w : Word) (σ0 : Fl4SpecA64.State Unit) (v0 : Fl4SpecA64.Vcpu), + σ0.kheap vc = some (.archObj (.vcpu v0)) → r < 48 → + (do NonDet.mapMx (fun (p : Nat × Word) => fl4VcpuSet (Z := Unit) vc (vrVgicLr0 + p.1) p.2) lrs + fl4VcpuSet (Z := Unit) vc r w) σ0 = + (do fl4VcpuSet (Z := Unit) vc r w + NonDet.mapMx (fun (p : Nat × Word) => fl4VcpuSet (Z := Unit) vc (vrVgicLr0 + p.1) p.2) lrs) σ0 := by + intro r w + induction lrs with + | nil => + intro σ0 v0 hkv0 _ + simp only [NonDet.mapMx, NonDet.pure] + rw [nd_bind_oneSc (m := NonDet.pure ()) rfl] + change fl4VcpuSet vc r w σ0 = (fl4VcpuSet vc r w >>= fun _ => NonDet.pure ()) σ0 + rw [nd_bind_oneSc (fl4VcpuSet_eqA r w hkv0)]; exact fl4VcpuSet_eqA r w hkv0 + | cons hd tl ih => + intro σ0 v0 hkv0 hr48 + have hhd : hd.1 < 16 := hlrs hd (by simp) + have htl : ∀ p ∈ tl, p.1 < 16 := fun p hp => hlrs p (by simp [hp]) + change ((fl4VcpuSet vc (vrVgicLr0 + hd.1) hd.2 >>= fun _ => + NonDet.mapMx (fun (p : Nat × Word) => fl4VcpuSet vc (vrVgicLr0 + p.1) p.2) tl) >>= fun _ => + fl4VcpuSet vc r w) σ0 = + (fl4VcpuSet vc r w >>= fun _ => + fl4VcpuSet vc (vrVgicLr0 + hd.1) hd.2 >>= fun _ => + NonDet.mapMx (fun (p : Nat × Word) => fl4VcpuSet vc (vrVgicLr0 + p.1) p.2) tl) σ0 + rw [nd_bind_assocSc, nd_bind_oneSc (fl4VcpuSet_eqA _ _ hkv0), + ih htl _ _ (updKSc_same _ _ _) hr48, + nd_bind_oneSc (fl4VcpuSet_eqA r w (updKSc_same _ _ _)), + nd_bind_oneSc (fl4VcpuSet_eqA r w hkv0), + nd_bind_oneSc (fl4VcpuSet_eqA _ _ (updKSc_same _ _ _))] + have hcomm := fl4VcpuRegSet_comm (i := r) (j := vrVgicLr0 + hd.1) (by omega) (by unfold vrVgicLr0; omega) (by unfold vrVgicLr0; omega) w hd.2 v0 + simp only [updKSc, Function.update_idem, hcomm] + induction regs generalizing σ v with + | nil => + simp only [copyRegsFromFrame, NonDet.mapMx, NonDet.pure] + rw [nd_bind_oneSc (m := NonDet.pure ()) rfl] + change (NonDet.mapMx _ lrs >>= fun _ => NonDet.pure ()) σ = NonDet.mapMx _ lrs σ + exact (nd_bind_pure_r _ σ) + | cons r tl ih => + have hr48 : r < 48 := hregs r (by simp) + have htl : ∀ r' ∈ tl, r' < 48 := fun r' hr' => hregs r' (by simp [hr']) + have hload := Vcpu.loadWordOffs_eqA frame r σ (hal r (by omega)) + have hlrs_eval : ∃ v', NonDet.mapMx (fun (p : Nat × Word) => fl4VcpuSet (Z := Unit) vc (vrVgicLr0 + p.1) p.2) lrs σ = + ({((), updKSc σ vc (.archObj (.vcpu v')))}, False) := by + clear ih h1 hregs hr48 htl hload + induction lrs generalizing σ v with + | nil => + have hu : Function.update σ.kheap vc (some (.archObj (.vcpu v))) = σ.kheap := by + funext p; by_cases hp : p = vc + · subst hp; simp [hkv] + · simp [hp] + exact ⟨v, by simp [NonDet.mapMx, NonDet.pure, updKSc, hu]⟩ + | cons p ps ihp => + obtain ⟨v', hv'⟩ := ihp (fun q hq => hlrs q (by simp [hq])) (updKSc_same _ _ _) + refine ⟨v', ?_⟩ + change (fl4VcpuSet vc (vrVgicLr0 + p.1) p.2 >>= fun _ => NonDet.mapMx _ ps) σ = _ + rw [nd_bind_oneSc (fl4VcpuSet_eqA _ _ hkv)] + simpa [updKSc, Function.update_idem] using hv' + obtain ⟨vL, hvL⟩ := hlrs_eval + unfold copyRegsFromFrame + change (NonDet.mapMx _ lrs >>= fun _ => (loadWordOffs frame r >>= fun w => fl4VcpuSet vc r w) >>= fun _ => copyRegsFromFrame vc frame tl) σ = + (((loadWordOffs frame r >>= fun w => fl4VcpuSet vc r w) >>= fun _ => copyRegsFromFrame vc frame tl) >>= fun _ => NonDet.mapMx _ lrs) σ + rw [nd_bind_oneSc hvL, nd_bind_assocSc, nd_bind_oneSc (Vcpu.loadWordOffs_eqA frame r _ (hal r (by omega))), + show (updKSc σ vc (.archObj (.vcpu vL))).machineState.underlyingMemory = σ.machineState.underlyingMemory from rfl, + nd_bind_assocSc, nd_bind_assocSc, nd_bind_oneSc hload, + nd_bind_oneSc (fl4VcpuSet_eqA r _ hkv)] + have hstep := h1 r (loadWordAt σ.machineState.underlyingMemory (bufAddr frame r)) σ v hkv hr48 + rw [nd_bind_oneSc hvL, fl4VcpuSet_eqA r _ (updKSc_same _ _ _), + nd_bind_oneSc (fl4VcpuSet_eqA r _ hkv)] at hstep + rw [← ih (updKSc_same _ _ _) htl] + have hstep' : (NonDet.mapMx (fun (p : Nat × Word) => fl4VcpuSet (Z := Unit) vc (vrVgicLr0 + p.1) p.2) lrs >>= fun _ => + copyRegsFromFrame (Z := Unit) vc frame tl) (updKSc σ vc (.archObj (.vcpu (fl4VcpuRegSet r (loadWordAt σ.machineState.underlyingMemory (bufAddr frame r)) v)))) = + (copyRegsFromFrame (Z := Unit) vc frame tl) (updKSc (updKSc σ vc (.archObj (.vcpu vL))) vc (.archObj (.vcpu (fl4VcpuRegSet r (loadWordAt σ.machineState.underlyingMemory (bufAddr frame r)) vL)))) := by + rw [nd_bind_oneSc hstep.symm] + rw [nd_bind_oneSc (fl4VcpuSet_eqA r _ (updKSc_same _ _ _))] + exact hstep'.symm + +set_option maxHeartbeats 800000 in +/-- **`vcpu.write_state_from_frame` on `JStage3TA`** (zero global `FrameMemRelA`; slice-local +`FrameInputAgreeA` `[scope limit]` when `mask ≠ 0`). Reused by `WriteState` (68) and `Run` (69). -/ +theorem vcpu_write_state_from_frame_stage3TA {ar : machine.Arch A} {Ub : Set U64} + [J7PpA st J7] [J7FrA st J7] + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + (hSV : SchedValidA σ) (R6 : VcpuRec6A st Z OS Bf s σ) + (hP : VcpuPlatAgreeA ar) (hVcP : VcPlacedA Z OS Ub (L.ksb s)) + (hU : UbFootDisjAtA Z OS Ub (L.ksb s)) + (hCs128 : ∀ c ∈ Z.Cs, c.val < 128) (hCsN : ∀ c ∈ Z.Cs, c.val < n) + {vc frame mask : U64} (hvc : vc ∈ Z.V) + (hWin : FrameWinA Ub frame) (hfr_al : Aligned frame) + (hAgree : mask ≠ 0#u64 → FrameInputAgreeA st s σ frame) + (hfr_disj_tcb : ∀ t ∈ Z.T, ∀ k < 128, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → + wa frame ⟨BitVec.ofNat 64 (8 * k)⟩ ≠ wa t K) + {vgic : U64 × Array U64 16#usize} {vgicSpec : Word × List Word} + (hVgic : mask.bv &&& (smVgic ||| smVgicLr) ≠ 0 → + vgic.1.bv = vgicSpec.1 ∧ vgicSpec.1 >>> 32 = 0 ∧ + vgicSpec.2.length = fl4NumLrs ∧ ∀ j, j < fl4NumLrs → (vgic.2.val[j]!).bv = vgicSpec.2[j]!) : + vcpu.write_state_from_frame st ar s a vc frame mask vgic ⦃ s' => + ∃ σ', fl4WriteStateFromFrame (specPtr vc) (specPtr frame) mask.bv vgicSpec σ = ({((), σ')}, False) ∧ + JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s' a σ' m ∧ + SchedValidA σ' ∧ + VcpuRec6A st Z OS Bf s' σ' ∧ + WritesIn st s s' (WfSet vc (L.mem s vc)) ⦄ := by + unfold vcpu.write_state_from_frame + by_cases hm0 : mask = 0#u64 + · rw [if_pos hm0, WP.spec_ok] + refine ⟨σ, ?_, J, hSV, R6, ⟨rfl, rfl, fun _ _ _ => rfl⟩⟩ + subst hm0 + unfold fl4WriteStateFromFrame NonDet.when + simp only [show (0#u64 : U64).bv = 0 from rfl, + show (0 : Word) &&& smEl1 = 0 from rfl, show (0 : Word) &&& smHyp = 0 from rfl, + show (0 : Word) &&& smTimer = 0 from rfl, show (0 : Word) &&& smVgic = 0 from rfl, + show (0 : Word) &&& smVgicLr = 0 from rfl, show (0 : Word) &&& smGprs = 0 from rfl, + ne_eq, not_true_eq_false, ↓reduceIte] + rw [nd_bind_oneSc (m := NonDet.pure ()) rfl, nd_bind_oneSc (m := NonDet.pure ()) rfl, + nd_bind_oneSc (m := NonDet.pure ()) rfl, nd_bind_oneSc (m := NonDet.pure ()) rfl]; rfl + rw [if_neg hm0] + have hAg := hAgree hm0 + set v_tcb := L.mem s vc + have hDisjTcb : ∀ t1 ∈ Z.T, ∀ t2 ∈ Z.T, t1 ≠ t2 → t1.val + 4096 ≤ t2.val ∨ t2.val + 4096 ≤ t1.val := by + intro t1 ht1 t2 ht2 hne + have h1 : H t1 = some .tcb := by have : t1 ∈ H.tcbs := J.tcbs ▸ ht1; exact this + have h2 : H t2 = some .tcb := by have : t2 ∈ H.tcbs := J.tcbs ▸ ht2; exact this + simpa [HKind.bits] using J.ent.heap.disj t1 .tcb t2 .tcb h1 h2 hne + have hfr_dt : v_tcb ≠ 0#u64 → ∀ k < 128, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → + wa frame ⟨BitVec.ofNat 64 (8 * k)⟩ ≠ wa v_tcb K := fun h0 => + hfr_disj_tcb v_tcb ((J.vcRun.vtcb vc hvc).resolve_left h0) + have hal3 : ∀ k < 128, bufAddr (specPtr frame) k &&& Fl4SpecA64.mask 3 = 0 := fun k hk => by + rw [← specPtr_wa_wordA (by have := hWin.1; omega)] + exact specPtr_al3 (aligned_wa hfr_al _ (by + change (BitVec.ofNat 64 (8 * k)).toNat % 8 = 0 + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega : 8 * k < 2 ^ 64)]; omega)) + have hregs0 : TcbRegsRelA (L.mem s) σ Z.T := fun t ht x hkt r => by + obtain ⟨x', hkt', hrel⟩ := J.ep.tcb t ht + rw [hkt] at hkt'; cases hkt' + exact hrel.regs r + have inv0 : WfInv st Z H s σ vc v_tcb s σ := + ⟨⟨rfl, rfl, fun _ _ _ => rfl⟩, J.j.2.1, R6.snap, hregs0, vcpuSpecStep_refl H σ, rfl⟩ + -- Helper: finish `WfInv` → `JStage3TA + SchedValidA + VcpuRec6A + WritesIn` + have hFin : ∀ (sf : S) (σf : Fl4SpecA64.State Unit), WfInv st Z H s σ vc v_tcb sf σf → + JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 sf a σf m ∧ + SchedValidA σf ∧ VcpuRec6A st Z OS Bf sf σf ∧ WritesIn st s sf (WfSet vc v_tcb) := by + intro sf σf invf + have hwf_sub : WritesIn st s sf (WfSet vc v_tcb) := invf.hw + have hw_no : WritesIn st s sf (VcpuWrFootA st true Z OS s) := + ⟨invf.hw.1, invf.hw.2.1, fun q hqa hnf => invf.hw.2.2 q hqa (fun h => hnf (by + rcases h with rfl | ⟨i, hi, rfl⟩ | ⟨hnz, K, hK8, hK1, hK2, rfl⟩ + · exact Or.inl ⟨vc, hvc, 16#u64, by decide, by decide, rfl⟩ + · exact Or.inl ⟨vc, hvc, vcpuRegOffA i, by rw [vcpuRegOffA_val hi]; omega, by rw [vcpuRegOffA_val hi]; omega, rfl⟩ + · exact Or.inr (Or.inl ⟨v_tcb, (J.vcRun.vtcb vc hvc).resolve_left hnz, K, hK8, Or.inl ⟨hK1, hK2⟩, rfl⟩)))⟩ + have hvc_eq : ∀ v ∈ Z.V, L.mem sf v = L.mem s v ∧ L.mem sf (wa v 24#u64) = L.mem s (wa v 24#u64) := fun v hv => by + have halv := hVcP.vc_al hv + refine ⟨invf.hw.mem_eq halv (fun h => ?_), invf.hw.mem_eq (aligned_wa halv 24#u64 (by decide)) (fun h => ?_)⟩ + · rcases h with h | ⟨i, hi, h⟩ | ⟨hnz, K, -, hK1, hK2, h⟩ + · by_cases hvv : v = vc + · subst hvv + exact hVcP.same_vcpu_ne hv (o₁ := 0#u64) (o₂ := 16#u64) (by decide) (by decide) (by decide) (by simpa [wa_zero2] using h) + · exact hVcP.disj_vcpu hv hvc hvv (ov₁ := 0#u64) (ov₂ := 16#u64) (by decide) (by decide) (by simpa [wa_zero2] using h) + · unfold vcpuRegWA at h + by_cases hvv : v = vc + · subst hvv + exact hVcP.same_vcpu_ne hv (o₁ := 0#u64) (o₂ := vcpuRegOffA i) (by decide) (by rw [vcpuRegOffA_val hi]; omega) + (fun e => by have := congrArg UScalar.val e; rw [vcpuRegOffA_val hi] at this; change 0 = _ at this; omega) (by simpa [wa_zero2] using h) + · exact hVcP.disj_vcpu hv hvc hvv (ov₁ := 0#u64) (ov₂ := vcpuRegOffA i) (by decide) (by rw [vcpuRegOffA_val hi]; omega) (by simpa [wa_zero2] using h) + · exact hVcP.disj_tcb v hv v_tcb ((J.vcRun.vtcb vc hvc).resolve_left hnz) 0#u64 K (by decide) (by omega) + (by simpa [wa_zero2] using h) + · rcases h with h | ⟨i, hi, h⟩ | ⟨hnz, K, -, hK1, hK2, h⟩ + · by_cases hvv : v = vc + · subst hvv + exact hVcP.same_vcpu_ne hv (o₁ := 24#u64) (o₂ := 16#u64) (by decide) (by decide) (by decide) h + · exact hVcP.disj_vcpu hv hvc hvv (ov₁ := 24#u64) (ov₂ := 16#u64) (by decide) (by decide) h + · unfold vcpuRegWA at h + by_cases hvv : v = vc + · subst hvv + exact hVcP.same_vcpu_ne hv (o₁ := 24#u64) (o₂ := vcpuRegOffA i) (by decide) (by rw [vcpuRegOffA_val hi]; omega) + (fun e => by have := congrArg UScalar.val e; rw [vcpuRegOffA_val hi] at this; change 24 = _ at this; omega) h + · exact hVcP.disj_vcpu hv hvc hvv (ov₁ := 24#u64) (ov₂ := vcpuRegOffA i) (by decide) (by rw [vcpuRegOffA_val hi]; omega) h + · exact hVcP.disj_tcb v hv v_tcb ((J.vcRun.vtcb vc hvc).resolve_left hnz) 24#u64 K (by decide) (by omega) h + have ht_eq : ∀ t ∈ Z.T, ∀ o : U64, (o.val < 256 ∨ (3328 ≤ o.val ∧ o.val < 4096)) → o.val % 8 = 0 → + L.mem sf (wa t o) = L.mem s (wa t o) := fun t ht o ho ho8 => by + exact invf.hw.mem_eq (aligned_wa (hVcP.al_t t ht) o ho8) (fun h => by + rcases h with h | ⟨i, hi, h⟩ | ⟨hnz, K, -, hK1, hK2, h⟩ + · exact (hVcP.disj_tcb vc hvc t ht 16#u64 o (by decide) (by omega)) h.symm + · rcases vcpuRegWA_in_vcPage hvc hi with ⟨v', hv', ov, hov, -, heq⟩ + exact (hVcP.disj_tcb v' hv' t ht ov o hov (by omega)) (heq ▸ h.symm) + · have hft := (hU.tcb_al t ht).fits; have hfv := (hU.tcb_al v_tcb ((J.vcRun.vtcb vc hvc).resolve_left hnz)).fits + have h1 : (wa t o).val = t.val + o.val := by rw [wa_valP]; exact Nat.mod_eq_of_lt (by omega) + have h2 : (wa v_tcb K).val = v_tcb.val + K.val := by rw [wa_valP]; exact Nat.mod_eq_of_lt (by omega) + have hv := congrArg UScalar.val h; rw [h1, h2] at hv + by_cases hteq : t = v_tcb + · subst hteq; omega + · have := hDisjTcb t ht v_tcb ((J.vcRun.vtcb vc hvc).resolve_left hnz) hteq; omega) + have hvcRun' : VcRunA st Z.T Z.V sf := + ⟨fun t ht => by rw [ht_eq t ht 3664#u64 (Or.inr ⟨by decide, by decide⟩) (by decide)]; exact J.vcRun.tcb t ht, + fun v hv => by rw [(hvc_eq v hv).2]; exact J.vcRun.vcpu v hv, + fun v hv => by rw [(hvc_eq v hv).1]; exact J.vcRun.vtcb v hv, + fun t ht => by unfold runVcpuA; rw [ht_eq t ht 3672#u64 (Or.inr ⟨by decide, by decide⟩) (by decide)]; exact J.vcRun.run t ht, + fun t ht hnz => by + rw [ht_eq t ht 3664#u64 (Or.inr ⟨by decide, by decide⟩) (by decide)] at hnz + unfold runVcpuA; rw [ht_eq t ht 3672#u64 (Or.inr ⟨by decide, by decide⟩) (by decide)]; exact J.vcRun.disj t ht hnz⟩ + have hrunVcpu' : RunVcpuInv st Z.T sf := fun t ht hne => by + unfold runVcpuA at hne + rw [ht_eq t ht 3672#u64 (Or.inr ⟨by decide, by decide⟩) (by decide)] at hne + rw [stA_eq_wa, ht_eq t ht 3328#u64 (Or.inr ⟨by decide, by decide⟩) (by decide), ← stA_eq_wa] + exact J.runVcpu t ht hne + have hEpTcb' : ∀ t ∈ Z.T, ∃ x, σf.kheap (specPtr t) = some (.tcb x) ∧ TcbRelE Rp (L.mem sf) t x := fun t ht => by + obtain ⟨x0, hk0, hrel0⟩ := J.ep.tcb t ht + obtain ⟨xf, hkf, -, -⟩ := invf.core.tcb t ht + have heq_f : ∀ {o : U64}, 3328 ≤ o.val → o.val < 4096 → o.val % 8 = 0 → L.mem sf (wa t o) = L.mem s (wa t o) := + fun h1 h2 h8 => ht_eq t ht _ (Or.inr ⟨h1, h2⟩) h8 + have hTcb : ∀ {x' : Fl4SpecA64.Tcb}, σf.kheap (specPtr t) = some (.tcb x') → + x'.tcbState = x0.tcbState → x'.tcbPriority = x0.tcbPriority → + x'.tcbFault = x0.tcbFault → x'.tcbBoundNotification = x0.tcbBoundNotification → + TcbRelE Rp (L.mem sf) t x' := fun hx' hst hpr hfl hbn => + ⟨by rw [stA_eq_wa, heq_f (by decide) (by decide) (by decide), ← stA_eq_wa, hst]; exact hrel0.st, + fun e he => by rw [show tBlockA t = wa t 3336#u64 from rfl, heq_f (by decide) (by decide) (by decide)]; exact hrel0.blk e (hst ▸ he), + fun e d he => by + rw [show tBadgeA t = wa t 3344#u64 from rfl, show tFlagsA t = wa t 3352#u64 from rfl, + heq_f (by decide) (by decide) (by decide), heq_f (by decide) (by decide) (by decide)] + exact hrel0.send e d (hst ▸ he), + fun e r d he => by rw [show tReplyA t = wa t 3360#u64 from rfl, heq_f (by decide) (by decide) (by decide)]; exact hrel0.recv e r d (hst ▸ he), + fun r he => by rw [show tReplyA t = wa t 3360#u64 from rfl, heq_f (by decide) (by decide) (by decide)]; exact hrel0.reply r (hst ▸ he), + invf.regs t ht _ hx', + by rw [show prioA t = wa t 3448#u64 from rfl, heq_f (by decide) (by decide) (by decide), hpr]; exact hrel0.prio, + by rw [show tFaultA t = wa t 3376#u64 from rfl, heq_f (by decide) (by decide) (by decide), hfl]; exact hrel0.fault0, + by rw [show tFaultA t = wa t 3376#u64 from rfl, heq_f (by decide) (by decide) (by decide), hfl]; exact hrel0.faultT, + by rw [show tBoundA t = wa t 3368#u64 from rfl, heq_f (by decide) (by decide) (by decide), hbn]; exact hrel0.bound, + by rw [hfl]; exact hrel0.faultR.congr + (heq_f (o := 3376#u64) (by decide) (by decide) (by decide)) + (heq_f (o := 3384#u64) (by decide) (by decide) (by decide)) + (heq_f (o := 3392#u64) (by decide) (by decide) (by decide)) + (heq_f (o := 3408#u64) (by decide) (by decide) (by decide)) + (heq_f (o := 3416#u64) (by decide) (by decide) (by decide)) + (heq_f (o := 3424#u64) (by decide) (by decide) (by decide)) + (heq_f (o := 3400#u64) (by decide) (by decide) (by decide))⟩ + rcases invf.hHS.kh (specPtr t) with heq | ⟨x0', xf', h1, h2, hkflds⟩ | ⟨v0, vf, h1, -, -⟩ + · rw [hk0] at heq; rw [heq] at hkf; cases hkf + exact ⟨x0, heq, hTcb heq rfl rfl rfl rfl⟩ + · rw [hk0] at h1; cases h1; rw [hkf] at h2; cases h2 + exact ⟨xf, hkf, hTcb hkf hkflds.state hkflds.prio hkflds.fault hkflds.bnd⟩ + · rw [hk0] at h1; cases h1 + have hcapE : ∀ y ∈ OS, capA st sf y = capA st s y := fun y hy => + (osKeep_of_vcpuWrA J R6.vcOS R6.frOff hw_no).capA hy + have hDeadVcpu' : ∀ v ∈ Z.V, DeadA st OS .vcpu sf v → ∀ x, σf.kheap (specPtr v) = some (.archObj (.vcpu x)) → + x.vcpuTcb = none := fun v hv hd x hx => by + have hd0 : DeadA st OS .vcpu s v := fun y hy => by have := hd y hy; rwa [hcapE y hy] at this + obtain ⟨x0, hk0, hvt0, -⟩ := J.j.2.1.obj v hv + obtain ⟨x', hk', hvt', -⟩ := invf.core.obj v hv + rw [hx] at hk'; cases hk' + rw [hvt', (hvc_eq v hv).1, ← hvt0]; exact J.deadExt.vcpu v hv hd0 x0 hk0 + have hDeadTv' : ∀ t ∈ Z.T, DeadA st OS .tcb sf t → L.mem sf (wa t 3664#u64) = 0#u64 := fun t ht hd => by + rw [ht_eq t ht 3664#u64 (Or.inr ⟨by decide, by decide⟩) (by decide)] + exact J.deadExt.tvcpu t ht (fun u hu => by rw [← hcapE u hu]; exact hd u hu) + have hDeadRun' : ∀ v ∈ Z.V, DeadA st OS .vcpu sf v → L.mem sf (wa v 24#u64) = 0#u64 := fun v hv hd => by + rw [(hvc_eq v hv).2] + exact J.deadExt.vcDeadRun v hv (fun u hu => by rw [← hcapE u hu]; exact hd u hu) + have hZomb' : ZombieTcbNoVcpuA st OS sf := fun z hz u num hzcap => by + have hz0 : capA st s z = .Zombie u capability.ZOMBIE_TCB num := by rw [← hcapE z hz]; exact hzcap + have huT : u ∈ Z.T := by rw [← J.tcbs]; exact J.zombieTcb z hz u num hz0 + rw [ht_eq u huT 3664#u64 (Or.inr ⟨by decide, by decide⟩) (by decide)] + exact J.zombieNoVcpu z hz u num hz0 + have hEpBuf' : ∀ bp ∈ Bf, ∀ k : U64, k.val < 128 → + (L.mem sf (bufW bp.1 k)).bv = loadWordAt σf.machineState.underlyingMemory (bufAddr bp.2 k.val) ∧ + (bufAddr bp.2 k.val).toNat % 8 = 0 := fun bp hbp k hk => by + obtain ⟨h1, h2⟩ := J.ep.buf bp hbp k hk + have hbufW : BufW Bf (bufW bp.1 k) := ⟨bp, hbp, k, hk, rfl⟩ + have hbal : Aligned (bufW bp.1 k) := J.iplaced.e.balign _ hbufW + have hmem_b : L.mem sf (bufW bp.1 k) = L.mem s (bufW bp.1 k) := invf.hw.mem_eq hbal (fun hm => by + rcases hm with h | ⟨i, hi, heq⟩ | ⟨hnz, K, -, hK1, hK2, heq⟩ + · exact J.bufOff.vcw _ hbufW vc hvc 16#u64 (by decide) (by decide) h + · rcases vcpuRegWA_in_vcPage hvc hi with ⟨v', hv', ov, hov, hov8, heq2⟩ + exact J.bufOff.vcw _ hbufW v' hv' ov hov hov8 (heq.trans heq2) + · have hvT := (J.vcRun.vtcb vc hvc).resolve_left hnz + have hft := (hU.tcb_al v_tcb hvT).fits + have ev : (wa v_tcb K).val = v_tcb.val + K.val := by rw [wa_valP]; exact Nat.mod_eq_of_lt (by omega) + have hbt := J.iplaced.e.bt _ hbufW v_tcb hvT + rw [heq, ev] at hbt; omega) + rw [hmem_b, invf.um] + exact ⟨h1, h2⟩ + have Jf := JStage3TA.of_vcpuStepA J R6 hw_no invf.hHS J.j.1.ar + (fun b hb => hw_no.mem_eq (J.vsOk.al b hb) (utVs_off_vcpuWrA J hb)) + invf.core hvcRun' hrunVcpu' hEpTcb' hEpBuf' hDeadVcpu' hDeadTv' hDeadRun' hZomb' + exact ⟨Jf, SchedValidA.of_vcpuStepA hSV invf.hHS, + ⟨invf.snap, + fun t ht hnz => by + rw [ht_eq t ht 3664#u64 (Or.inr ⟨by decide, by decide⟩) (by decide)] at hnz ⊢ + rw [(hvc_eq _ ((J.vcRun.tcb t ht).resolve_left hnz)).1]; exact R6.vcSym1 t ht hnz, + fun v hv hnz => by + rw [(hvc_eq v hv).2] at hnz ⊢; unfold runVcpuA + rw [ht_eq _ ((J.vcRun.vcpu v hv).resolve_left hnz) 3672#u64 (Or.inr ⟨by decide, by decide⟩) (by decide)] + exact R6.vcSym2 v hv hnz, + fun v hv hnz => by + rw [(hvc_eq v hv).1] at hnz ⊢ + rw [ht_eq _ ((J.vcRun.vtcb v hv).resolve_left hnz) 3664#u64 (Or.inr ⟨by decide, by decide⟩) (by decide)] + exact R6.vcBack v hv hnz, + fun t ht hnz => by + unfold runVcpuA at hnz ⊢ + rw [ht_eq t ht 3672#u64 (Or.inr ⟨by decide, by decide⟩) (by decide)] at hnz ⊢ + have hrv := J.vcRun.run t ht; unfold runVcpuA at hrv + rw [(hvc_eq _ (hrv.resolve_left hnz)).2]; exact R6.runBack t ht hnz, + R6.vcOS, + fun y hy p sz r mp ept asid va hc b hb => by + rw [invf.hw.1]; exact R6.frOff y hy p sz r mp ept asid va (by rwa [← hcapE y hy]) b hb, + R6.userFr.keepA hcapE (fun _ _ _ h => invf.hHS.keepObj h (fun _ e => by cases e) (fun _ e => by cases e)), + R6.frTh.keepA hcapE (fun _ _ e => e), + R6.bfSpec⟩, + hwf_sub⟩ + -- Helper for trailing GPRS block (identical across loop1, loop3, loop4) + have hGprs : ∀ (sG : S) (σG : Fl4SpecA64.State Unit), WfInv st Z H s σ vc v_tcb sG σG → + (do + let i13 ← fl4_abi.consts.vcpu_state_mask.GPRS + let i14 ← lift (mask &&& i13) + if i14 != 0#u64 then do + let i15 ← 64#u64 * 8#u64 + let gprs_base ← lift (wa frame i15) + let i16 ← lift (wa vc vcpu.TCB) + let t_ptr ← st.read sG i16 + if t_ptr != 0#u64 then do + let s10 ← vcpu.write_state_from_frame_loop1 st sG gprs_base t_ptr 0#u64 + let pc ← obj.Tcb.reg st t_ptr s10 layout.reg.ELR_EL1 + obj.Tcb.set_reg st t_ptr s10 layout.reg.FAULT_IP pc + else ok sG + else ok sG) ⦃ s' => + ∃ σ', (NonDet.when (mask.bv &&& smGprs ≠ 0) do + let v ← getVcpu (specPtr vc) + match v.vcpuTcb with + | some t => do + NonDet.mapMx (fun (i, r) => do + let w ← loadWordOffs (Z := Unit) (specPtr frame) (64 + i) + asUser t (setRegister r (if r = Register.spsrEl1 then sanitiseVcpuSpsr w else w))) + ((List.range fl4UserRegs.length).zip fl4UserRegs) + asUser t do + let pc ← getRegister nextInstructionRegister + setRegister faultRegister pc + | none => pure ()) σG = ({((), σ')}, False) ∧ + WfInv st Z H s σ vc v_tcb s' σ' ⦄ := by + intro sG σG invG + rw [sm_gprs_ok, bind_ok, lift_ok2, bind_ok] + by_cases hg : (mask &&& 16#u64 != 0#u64) = true + · have hgb : mask.bv &&& smGprs ≠ 0 := fun e => by + have : mask &&& 16#u64 = 0#u64 := u64_eq_of_bv_eq2 e; simp [this] at hg + rw [if_pos hg, u64_mul_ok (by decide), bind_ok, lift_ok2, bind_ok, lift_ok2, bind_ok, L.read_eq, bind_ok] + have hwa0 : wa vc vcpu.TCB = vc := by unfold vcpu.TCB; exact wa_zero2 vc + have hmem_vc : L.mem sG vc = v_tcb := invG.hw.mem_eq (hVcP.vc_al hvc) (fun h => by + rcases h with h | ⟨i, hi, h⟩ | ⟨hnz, K, -, hK1, hK2, h⟩ + · exact hVcP.same_vcpu_ne hvc (o₁ := 0#u64) (o₂ := 16#u64) (by decide) (by decide) (by decide) (by simpa [wa_zero2] using h) + · unfold vcpuRegWA at h + exact hVcP.same_vcpu_ne hvc (o₁ := 0#u64) (o₂ := vcpuRegOffA i) (by decide) (by rw [vcpuRegOffA_val hi]; omega) + (fun e => by have := congrArg UScalar.val e; rw [vcpuRegOffA_val hi] at this; change 0 = _ at this; omega) (by simpa [wa_zero2] using h) + · exact hVcP.disj_tcb vc hvc v_tcb ((J.vcRun.vtcb vc hvc).resolve_left hnz) 0#u64 K (by decide) (by omega) + (by simpa [wa_zero2] using h)) + rw [hwa0, hmem_vc] + obtain ⟨vG, hkvG, hvtG, -⟩ := invG.core.obj vc hvc + by_cases hnz : v_tcb = 0#u64 + · have hnz_f : (v_tcb != 0#u64) = false := by simp [hnz] + rw [if_neg (by simp [hnz_f]), WP.spec_ok] + refine ⟨σG, ?_, invG⟩ + unfold NonDet.when; rw [if_pos hgb, nd_bind_oneSc (getVcpu_eqA hkvG)] + have hv_n : vG.vcpuTcb = none := by rw [hvtG, hmem_vc, hnz]; rfl + rw [hv_n]; rfl + · have hnz_b : (v_tcb != 0#u64) = true := bne_iff_ne.2 hnz + rw [if_pos hnz_b] + have hvtT : v_tcb ∈ Z.T := (J.vcRun.vtcb vc hvc).resolve_left hnz + have hmul512 : (⟨BitVec.ofNat 64 (64#u64.val * 8#u64.val)⟩ : U64) = 512#u64 := rfl + rw [hmul512] + refine WP.spec_mono (wfInv_write_gprs invG hvc hvtT hnz hVcP hU J.j5.2.1 hWin hfr_al hAg hfr_dt hDisjTcb) ?_ + rintro s' ⟨σ', hsp', inv'⟩ + refine ⟨σ', ?_, inv'⟩ + unfold NonDet.when; rw [if_pos hgb, nd_bind_oneSc (getVcpu_eqA hkvG)] + have hv_s : vG.vcpuTcb = some (specPtr v_tcb) := by rw [hvtG, hmem_vc]; unfold optRefA; rw [if_neg hnz] + rw [hv_s]; exact hsp' + · have hgb : ¬ (mask.bv &&& smGprs ≠ 0) := fun h => hg (bne_iff_ne.2 (fun e => h (congrArg UScalar.bv e))) + rw [if_neg hg, WP.spec_ok] + exact ⟨σG, by unfold NonDet.when; rw [if_neg hgb]; rfl, invG⟩ + -- Helper: single frame-word read + reg_set step on `WfInv` + have hReg1 : ∀ (sc : S) (σc : Fl4SpecA64.State Unit), WfInv st Z H s σ vc v_tcb sc σc → + ∀ (r : U64) (hr : r.val < 64) (hnh : r.val ≠ vrVgicHcr), + (do + let off ← r * 8#u64 + let addr ← lift (wa frame off) + let w ← st.read sc addr + vcpu.reg_set st sc vc r w) ⦃ sn => + ∃ σn, copyRegsFromFrame (Z := Unit) (specPtr vc) (specPtr frame) [r.val] σc = ({((), σn)}, False) ∧ + WfInv st Z H s σ vc v_tcb sn σn ⦄ := by + intro sc σc invc r hr hnh + rw [u64_mul_ok (by change r.val * 8 < 2 ^ 64; omega), bind_ok, lift_ok2, bind_ok, L.read_eq, bind_ok] + have hmul : (⟨BitVec.ofNat 64 (r.val * 8#u64.val)⟩ : U64) = ⟨BitVec.ofNat 64 (8 * r.val)⟩ := by + congr 1; exact congrArg (BitVec.ofNat 64) (by change r.val * 8 = 8 * r.val; omega) + rw [hmul, wfInv_frame_eq invc hvc hVcP hWin hfr_al hfr_dt (by omega)] + have hset := vcpu_reg_set_eqA (L := L) sc vc hr (L.mem s (wa frame ⟨BitVec.ofNat 64 (8 * r.val)⟩)) + have hrof : (⟨BitVec.ofNat 64 r.val⟩ : U64) = r := UScalar.eq_of_val_eq (by + change (BitVec.ofNat 64 r.val).toNat = r.val; rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)]) + rw [hrof] at hset; rw [hset, WP.spec_ok] + obtain ⟨vc_o, hkvc, -, -⟩ := invc.core.obj vc hvc + have hload : loadWordOffs (Z := Unit) (specPtr frame) r.val σc = + ({((L.mem s (wa frame ⟨BitVec.ofNat 64 (8 * r.val)⟩)).bv, σc)}, False) := by + rw [Vcpu.loadWordOffs_eqA (specPtr frame) r.val σc (hal3 r.val (by omega)), invc.um, ← hAg r.val (by omega)] + refine ⟨_, ?_, wfInv_reg_wr invc hvc hVcP J.j5.2.1 hkvc hr _ (fun e => absurd e hnh)⟩ + simp only [copyRegsFromFrame, NonDet.mapMx, NonDet.pure] + change ((loadWordOffs (specPtr frame) r.val >>= fun w => fl4VcpuSet (specPtr vc) r.val w) >>= fun _ => NonDet.pure ()) σc = _ + rw [nd_bind_assocSc, nd_bind_oneSc hload, nd_bind_oneSc (fl4VcpuSet_eqA r.val _ hkvc)]; rfl + -- Block 1: EL1_SYSREGS + rw [sm_el1_ok, bind_ok, lift_ok2, bind_ok] + have hB1 : (if (mask &&& 1#u64) != 0#u64 then do + let s2 ← vcpu.copy_reg_range_from_frame st s vc frame 0#u64 fl4_abi.consts.vcpu_reg.CSSELR_EL1 + vcpu.copy_reg_range_from_frame st s2 vc frame fl4_abi.consts.vcpu_reg.ACTLR_EL1 fl4_abi.consts.vcpu_reg.MDCCINT_EL1 + else ok s) ⦃ s1 => + ∃ σ1, (NonDet.when (mask.bv &&& smEl1 ≠ 0) do + copyRegsFromFrame (Z := Unit) (specPtr vc) (specPtr frame) (idxRange 0 vrCsselr) + copyRegsFromFrame (Z := Unit) (specPtr vc) (specPtr frame) (idxRange vrActlr vrMdccint)) σ = ({((), σ1)}, False) ∧ + WfInv st Z H s σ vc v_tcb s1 σ1 ⦄ := by + by_cases h1 : (mask &&& 1#u64 != 0#u64) = true + · have h1b : mask.bv &&& smEl1 ≠ 0 := fun e => by + have : mask &&& 1#u64 = 0#u64 := u64_eq_of_bv_eq2 e; simp [this] at h1 + rw [if_pos h1] + refine WP.spec_bind (wfInv_copy_range inv0 hvc hVcP J.j5.2.1 hWin hfr_al hAg hfr_dt + (by unfold fl4_abi.consts.vcpu_reg.CSSELR_EL1; decide) + (by unfold fl4_abi.consts.vcpu_reg.CSSELR_EL1; decide) + (fun r _ hr => by + have : fl4_abi.consts.vcpu_reg.CSSELR_EL1.val = 19 := by unfold fl4_abi.consts.vcpu_reg.CSSELR_EL1; rfl + unfold vrVgicHcr; omega)) ?_ + rintro s2 ⟨σ2, hsp2, inv2⟩ + refine WP.spec_mono (wfInv_copy_range inv2 hvc hVcP J.j5.2.1 hWin hfr_al hAg hfr_dt + (by unfold fl4_abi.consts.vcpu_reg.ACTLR_EL1 fl4_abi.consts.vcpu_reg.MDCCINT_EL1; decide) + (by unfold fl4_abi.consts.vcpu_reg.MDCCINT_EL1; decide) + (fun r _ hr => by + have : fl4_abi.consts.vcpu_reg.MDCCINT_EL1.val = 30 := by unfold fl4_abi.consts.vcpu_reg.MDCCINT_EL1; rfl + unfold vrVgicHcr; omega)) ?_ + rintro s3 ⟨σ3, hsp3, inv3⟩ + refine ⟨σ3, ?_, inv3⟩ + unfold NonDet.when; rw [if_pos h1b] + have hsp2' : copyRegsFromFrame (Z := Unit) (specPtr vc) (specPtr frame) (idxRange 0 vrCsselr) σ = ({((), σ2)}, False) := by + have h0 : (0#u64 : U64).val = 0 := rfl + have hc : fl4_abi.consts.vcpu_reg.CSSELR_EL1.val = vrCsselr := by unfold fl4_abi.consts.vcpu_reg.CSSELR_EL1 vrCsselr; rfl + rwa [h0, hc] at hsp2 + have hsp3' : copyRegsFromFrame (Z := Unit) (specPtr vc) (specPtr frame) (idxRange vrActlr vrMdccint) σ2 = ({((), σ3)}, False) := by + have ha : fl4_abi.consts.vcpu_reg.ACTLR_EL1.val = vrActlr := by unfold fl4_abi.consts.vcpu_reg.ACTLR_EL1 vrActlr; rfl + have hm : fl4_abi.consts.vcpu_reg.MDCCINT_EL1.val = vrMdccint := by unfold fl4_abi.consts.vcpu_reg.MDCCINT_EL1 vrMdccint; rfl + rwa [ha, hm] at hsp3 + rw [nd_bind_oneSc hsp2']; exact hsp3' + · have h1b : ¬ (mask.bv &&& smEl1 ≠ 0) := fun h => h1 (bne_iff_ne.2 (fun e => h (congrArg UScalar.bv e))) + rw [if_neg h1, WP.spec_ok] + exact ⟨σ, by unfold NonDet.when; rw [if_neg h1b]; rfl, inv0⟩ + refine WP.spec_bind hB1 ?_ + rintro s1 ⟨σ1, hsp1, inv1⟩ + -- Block 2: HYP + rw [sm_hyp_ok, bind_ok, lift_ok2, bind_ok] + have hB2 : (if (mask &&& 2#u64) != 0#u64 then do + let i4 ← lift (mask &&& 1#u64) + let s3 ← if i4 = 0#u64 then do + let i5 ← fl4_abi.consts.vcpu_reg.VMPIDR_EL2 * 8#u64 + let i6 ← lift (wa frame i5) + let i7 ← st.read s1 i6 + vcpu.reg_set st s1 vc fl4_abi.consts.vcpu_reg.VMPIDR_EL2 i7 + else ok s1 + let i5 ← fl4_abi.consts.vcpu_reg.VDISR_EL2 * 8#u64 + let i6 ← lift (wa frame i5) + let i7 ← st.read s3 i6 + let s4 ← vcpu.reg_set st s3 vc fl4_abi.consts.vcpu_reg.VDISR_EL2 i7 + let i8 ← fl4_abi.consts.vcpu_reg.VPIDR_EL2 * 8#u64 + let i9 ← lift (wa frame i8) + let i10 ← st.read s4 i9 + let s5 ← vcpu.reg_set st s4 vc fl4_abi.consts.vcpu_reg.VPIDR_EL2 i10 + let i11 ← fl4_abi.consts.vcpu_reg.HCR_EL2 * 8#u64 + let i12 ← lift (wa frame i11) + let i13 ← st.read s5 i12 + let i14 ← vcpu.sanitize_hcr i13 + let s6 ← vcpu.reg_set st s5 vc fl4_abi.consts.vcpu_reg.HCR_EL2 i14 + let i15 ← fl4_abi.consts.vcpu_reg.VSESR_EL2 * 8#u64 + let i16 ← lift (wa frame i15) + let i17 ← st.read s6 i16 + vcpu.reg_set st s6 vc fl4_abi.consts.vcpu_reg.VSESR_EL2 i17 + else ok s1) ⦃ s2 => + ∃ σ2, (NonDet.when (mask.bv &&& smHyp ≠ 0) do + NonDet.when (mask.bv &&& smEl1 = 0) <| copyRegsFromFrame (Z := Unit) (specPtr vc) (specPtr frame) [vrVmpidr] + copyRegsFromFrame (Z := Unit) (specPtr vc) (specPtr frame) [vrVdisr, vrVpidr] + let hcr ← loadWordOffs (specPtr frame) vrHcr + fl4VcpuSet (specPtr vc) vrHcr (sanitizeHcr hcr) + copyRegsFromFrame (Z := Unit) (specPtr vc) (specPtr frame) [vrVsesr]) σ1 = ({((), σ2)}, False) ∧ + WfInv st Z H s σ vc v_tcb s2 σ2 ⦄ := by + by_cases h2 : (mask &&& 2#u64 != 0#u64) = true + · have h2b : mask.bv &&& smHyp ≠ 0 := fun e => by + have : mask &&& 2#u64 = 0#u64 := u64_eq_of_bv_eq2 e; simp [this] at h2 + rw [if_pos h2, lift_ok2, bind_ok] + have hB2a : (if (mask &&& 1#u64) = 0#u64 then do + let i5 ← fl4_abi.consts.vcpu_reg.VMPIDR_EL2 * 8#u64 + let i6 ← lift (wa frame i5) + let i7 ← st.read s1 i6 + vcpu.reg_set st s1 vc fl4_abi.consts.vcpu_reg.VMPIDR_EL2 i7 + else ok s1) ⦃ s3 => + ∃ σ3, (NonDet.when (mask.bv &&& smEl1 = 0) <| copyRegsFromFrame (Z := Unit) (specPtr vc) (specPtr frame) [vrVmpidr]) σ1 = ({((), σ3)}, False) ∧ + WfInv st Z H s σ vc v_tcb s3 σ3 ⦄ := by + by_cases h2e : mask &&& 1#u64 = 0#u64 + · have h2eb : mask.bv &&& smEl1 = 0 := congrArg UScalar.bv h2e + rw [if_pos h2e] + refine WP.spec_mono (hReg1 s1 σ1 inv1 fl4_abi.consts.vcpu_reg.VMPIDR_EL2 + (by unfold fl4_abi.consts.vcpu_reg.VMPIDR_EL2; decide) + (by unfold fl4_abi.consts.vcpu_reg.VMPIDR_EL2 vrVgicHcr; decide)) ?_ + rintro s3 ⟨σ3, hsp3, inv3⟩ + have hvmp : fl4_abi.consts.vcpu_reg.VMPIDR_EL2.val = vrVmpidr := by unfold fl4_abi.consts.vcpu_reg.VMPIDR_EL2 vrVmpidr; rfl + exact ⟨σ3, by unfold NonDet.when; rw [if_pos h2eb, ← hvmp]; exact hsp3, inv3⟩ + · have h2eb : mask.bv &&& smEl1 ≠ 0 := fun e => h2e (u64_eq_of_bv_eq2 e) + rw [if_neg h2e, WP.spec_ok] + exact ⟨σ1, by unfold NonDet.when; rw [if_neg h2eb]; rfl, inv1⟩ + refine WP.spec_bind hB2a ?_ + rintro s3 ⟨σ3, hsp3, inv3⟩ + -- VDISR_EL2 + rw [u64_mul_ok (by unfold fl4_abi.consts.vcpu_reg.VDISR_EL2; decide), bind_ok, lift_ok2, bind_ok, L.read_eq, bind_ok] + have hmul_vd : (⟨BitVec.ofNat 64 (fl4_abi.consts.vcpu_reg.VDISR_EL2.val * 8#u64.val)⟩ : U64) = ⟨BitVec.ofNat 64 (8 * vrVdisr)⟩ := by + unfold fl4_abi.consts.vcpu_reg.VDISR_EL2 vrVdisr; rfl + rw [hmul_vd, wfInv_frame_eq inv3 hvc hVcP hWin hfr_al hfr_dt (by unfold vrVdisr; omega)] + have hset_vd := vcpu_reg_set_eqA (L := L) s3 vc (i := vrVdisr) (by unfold vrVdisr; omega) + (L.mem s (wa frame ⟨BitVec.ofNat 64 (8 * vrVdisr)⟩)) + rw [show (⟨BitVec.ofNat 64 vrVdisr⟩ : U64) = fl4_abi.consts.vcpu_reg.VDISR_EL2 from by unfold fl4_abi.consts.vcpu_reg.VDISR_EL2 vrVdisr; rfl] at hset_vd + rw [hset_vd, bind_ok] + set s4 := L.wr s3 (vcpuRegWA vc vrVdisr) (L.mem s (wa frame ⟨BitVec.ofNat 64 (8 * vrVdisr)⟩)) + obtain ⟨v3, hkv3, -, -⟩ := inv3.core.obj vc hvc + have inv4 := wfInv_reg_wr inv3 hvc hVcP J.j5.2.1 hkv3 (r := vrVdisr) (by unfold vrVdisr; omega) + (L.mem s (wa frame ⟨BitVec.ofNat 64 (8 * vrVdisr)⟩)) (fun h => by unfold vrVdisr vrVgicHcr at h; omega) + set σ4 := updKSc σ3 (specPtr vc) (.archObj (.vcpu (fl4VcpuRegSet vrVdisr (L.mem s (wa frame ⟨BitVec.ofNat 64 (8 * vrVdisr)⟩)).bv v3))) + have hsp4 : copyRegsFromFrame (Z := Unit) (specPtr vc) (specPtr frame) [vrVdisr] σ3 = ({((), σ4)}, False) := by + unfold copyRegsFromFrame NonDet.mapMx + have hld : loadWordOffs (Z := Unit) (specPtr frame) vrVdisr σ3 = + ({((L.mem s (wa frame ⟨BitVec.ofNat 64 (8 * vrVdisr)⟩)).bv, σ3)}, False) := by + rw [Vcpu.loadWordOffs_eqA (specPtr frame) vrVdisr σ3 (hal3 vrVdisr (by unfold vrVdisr; omega)), inv3.um, ← hAg vrVdisr (by unfold vrVdisr; omega)] + change (((loadWordOffs (specPtr frame) vrVdisr >>= fun w => fl4VcpuSet (specPtr vc) vrVdisr w) >>= fun _ => pure ()) σ3) = _ + rw [nd_bind_assocSc, nd_bind_oneSc hld, nd_bind_oneSc (fl4VcpuSet_eqA vrVdisr _ hkv3)]; rfl + -- VPIDR_EL2 + rw [u64_mul_ok (by unfold fl4_abi.consts.vcpu_reg.VPIDR_EL2; decide), bind_ok, lift_ok2, bind_ok, L.read_eq, bind_ok] + have hmul_vp : (⟨BitVec.ofNat 64 (fl4_abi.consts.vcpu_reg.VPIDR_EL2.val * 8#u64.val)⟩ : U64) = ⟨BitVec.ofNat 64 (8 * vrVpidr)⟩ := by + unfold fl4_abi.consts.vcpu_reg.VPIDR_EL2 vrVpidr; rfl + rw [hmul_vp, wfInv_frame_eq inv4 hvc hVcP hWin hfr_al hfr_dt (k := vrVpidr) (by unfold vrVpidr; omega)] + have hset_vp := vcpu_reg_set_eqA (L := L) s4 vc (i := vrVpidr) (by unfold vrVpidr; omega) + (L.mem s (wa frame ⟨BitVec.ofNat 64 (8 * vrVpidr)⟩)) + rw [show (⟨BitVec.ofNat 64 vrVpidr⟩ : U64) = fl4_abi.consts.vcpu_reg.VPIDR_EL2 from by unfold fl4_abi.consts.vcpu_reg.VPIDR_EL2 vrVpidr; rfl] at hset_vp + rw [hset_vp, bind_ok] + set s5 := L.wr s4 (vcpuRegWA vc vrVpidr) (L.mem s (wa frame ⟨BitVec.ofNat 64 (8 * vrVpidr)⟩)) + obtain ⟨v4, hkv4, -, -⟩ := inv4.core.obj vc hvc + have inv5 := wfInv_reg_wr inv4 hvc hVcP J.j5.2.1 hkv4 (r := vrVpidr) (by unfold vrVpidr; omega) + (L.mem s (wa frame ⟨BitVec.ofNat 64 (8 * vrVpidr)⟩)) (fun h => by unfold vrVpidr vrVgicHcr at h; omega) + set σ5 := updKSc σ4 (specPtr vc) (.archObj (.vcpu (fl4VcpuRegSet vrVpidr (L.mem s (wa frame ⟨BitVec.ofNat 64 (8 * vrVpidr)⟩)).bv v4))) + have hsp5 : copyRegsFromFrame (Z := Unit) (specPtr vc) (specPtr frame) [vrVpidr] σ4 = ({((), σ5)}, False) := by + unfold copyRegsFromFrame NonDet.mapMx + have hld : loadWordOffs (Z := Unit) (specPtr frame) vrVpidr σ4 = + ({((L.mem s (wa frame ⟨BitVec.ofNat 64 (8 * vrVpidr)⟩)).bv, σ4)}, False) := by + rw [Vcpu.loadWordOffs_eqA (specPtr frame) vrVpidr σ4 (hal3 vrVpidr (by unfold vrVpidr; omega)), inv4.um, ← hAg vrVpidr (by unfold vrVpidr; omega)] + change (((loadWordOffs (specPtr frame) vrVpidr >>= fun w => fl4VcpuSet (specPtr vc) vrVpidr w) >>= fun _ => pure ()) σ4) = _ + rw [nd_bind_assocSc, nd_bind_oneSc hld, nd_bind_oneSc (fl4VcpuSet_eqA vrVpidr _ hkv4)]; rfl + -- HCR_EL2 (sanitized) + rw [u64_mul_ok (by unfold fl4_abi.consts.vcpu_reg.HCR_EL2; decide), bind_ok, lift_ok2, bind_ok, L.read_eq, bind_ok, + vcpu_sanitize_hcr_eqA, bind_ok] + have hmul_hcr : (⟨BitVec.ofNat 64 (fl4_abi.consts.vcpu_reg.HCR_EL2.val * 8#u64.val)⟩ : U64) = ⟨BitVec.ofNat 64 (8 * vrHcr)⟩ := by + unfold fl4_abi.consts.vcpu_reg.HCR_EL2 vrHcr; rfl + rw [hmul_hcr, wfInv_frame_eq inv5 hvc hVcP hWin hfr_al hfr_dt (k := vrHcr) (by unfold vrHcr; omega)] + have hset_hcr := vcpu_reg_set_eqA (L := L) s5 vc (i := vrHcr) (by unfold vrHcr; omega) + ⟨sanitizeHcr (L.mem s (wa frame ⟨BitVec.ofNat 64 (8 * vrHcr)⟩)).bv⟩ + rw [show (⟨BitVec.ofNat 64 vrHcr⟩ : U64) = fl4_abi.consts.vcpu_reg.HCR_EL2 from by unfold fl4_abi.consts.vcpu_reg.HCR_EL2 vrHcr; rfl] at hset_hcr + rw [hset_hcr, bind_ok] + obtain ⟨v5, hkv5, -, -⟩ := inv5.core.obj vc hvc + have inv6 := wfInv_reg_wr inv5 hvc hVcP J.j5.2.1 hkv5 (r := vrHcr) (by unfold vrHcr; omega) + ⟨sanitizeHcr (L.mem s (wa frame ⟨BitVec.ofNat 64 (8 * vrHcr)⟩)).bv⟩ (fun h => by unfold vrHcr vrVgicHcr at h; omega) + refine WP.spec_mono (hReg1 _ _ inv6 fl4_abi.consts.vcpu_reg.VSESR_EL2 + (by unfold fl4_abi.consts.vcpu_reg.VSESR_EL2; decide) + (by unfold fl4_abi.consts.vcpu_reg.VSESR_EL2 vrVgicHcr; decide)) ?_ + rintro s7 ⟨σ7, hsp7, inv7⟩ + refine ⟨σ7, ?_, inv7⟩ + rw [NonDet.when, ite_eq_left h2b, nd_bind_oneSc hsp3] + have hvdisr : fl4_abi.consts.vcpu_reg.VDISR_EL2.val = vrVdisr := by unfold fl4_abi.consts.vcpu_reg.VDISR_EL2 vrVdisr; rfl + have hvpidr : fl4_abi.consts.vcpu_reg.VPIDR_EL2.val = vrVpidr := by unfold fl4_abi.consts.vcpu_reg.VPIDR_EL2 vrVpidr; rfl + have hvsesr : fl4_abi.consts.vcpu_reg.VSESR_EL2.val = vrVsesr := by unfold fl4_abi.consts.vcpu_reg.VSESR_EL2 vrVsesr; rfl + have h2pair : copyRegsFromFrame (Z := Unit) (specPtr vc) (specPtr frame) [vrVdisr, vrVpidr] σ3 = ({((), σ5)}, False) := by + unfold copyRegsFromFrame at hsp4 hsp5 ⊢ + rw [show ([vrVdisr, vrVpidr] : List Nat) = [vrVdisr] ++ [vrVpidr] from rfl, mapMx_appendA, nd_bind_oneSc hsp4] + exact hsp5 + rw [nd_bind_oneSc h2pair] + have hload_hcr : loadWordOffs (Z := Unit) (specPtr frame) vrHcr σ5 = + ({((L.mem s (wa frame ⟨BitVec.ofNat 64 (8 * vrHcr)⟩)).bv, σ5)}, False) := by + rw [Vcpu.loadWordOffs_eqA (specPtr frame) vrHcr σ5 (hal3 vrHcr (by unfold vrHcr; omega)), inv5.um, ← hAg vrHcr (by unfold vrHcr; omega)] + rw [nd_bind_oneSc hload_hcr, nd_bind_oneSc (fl4VcpuSet_eqA vrHcr _ hkv5)]; exact hvsesr ▸ hsp7 + · have h2b : ¬ (mask.bv &&& smHyp ≠ 0) := fun h => h2 (bne_iff_ne.2 (fun e => h (congrArg UScalar.bv e))) + rw [if_neg h2, WP.spec_ok] + exact ⟨σ1, by unfold NonDet.when; rw [if_neg h2b]; rfl, inv1⟩ + refine WP.spec_bind hB2 ?_ + rintro s2 ⟨σ2, hsp2, inv2⟩ + -- Block 3: TIMER + rw [sm_timer_ok, bind_ok, lift_ok2, bind_ok] + have hB3 : (if (mask &&& 4#u64) != 0#u64 then do + let s4 ← vcpu.copy_reg_range_from_frame st s2 vc frame fl4_abi.consts.vcpu_reg.CNTV_CTL fl4_abi.consts.vcpu_reg.CNTKCTL_EL1 + let i6 ← 64#u64 + 37#u64 + let i7 ← i6 * 8#u64 + let vppi_addr ← lift (wa frame i7) + let i8 ← st.read s4 vppi_addr + let m ← lift (i8 &&& 1#u64) + let i9 ← lift (wa vc vcpu.VPPI_MASKED) + st.write s4 i9 m + else ok s2) ⦃ s3 => + ∃ σ3, (NonDet.when (mask.bv &&& smTimer ≠ 0) do + copyRegsFromFrame (Z := Unit) (specPtr vc) (specPtr frame) (idxRange vrCntvCtl vrCntkctl) + let w ← loadWordOffs (specPtr frame) vppiWord + let v ← getVcpu (specPtr vc) + let mk := Function.update v.vcpuVppiMasked .vppiEventIRQ_VTimer (decide (w &&& 1 ≠ 0)) + setVcpu (specPtr vc) { v with vcpuVppiMasked := mk }) σ2 = ({((), σ3)}, False) ∧ + WfInv st Z H s σ vc v_tcb s3 σ3 ⦄ := by + by_cases h3 : (mask &&& 4#u64 != 0#u64) = true + · have h3b : mask.bv &&& smTimer ≠ 0 := fun e => by + have : mask &&& 4#u64 = 0#u64 := u64_eq_of_bv_eq2 e; simp [this] at h3 + rw [if_pos h3] + refine WP.spec_bind (wfInv_copy_range inv2 hvc hVcP J.j5.2.1 hWin hfr_al hAg hfr_dt + (by unfold fl4_abi.consts.vcpu_reg.CNTV_CTL fl4_abi.consts.vcpu_reg.CNTKCTL_EL1; decide) + (by unfold fl4_abi.consts.vcpu_reg.CNTKCTL_EL1; decide) + (fun r _ hr => by + have : fl4_abi.consts.vcpu_reg.CNTKCTL_EL1.val = 24 := by unfold fl4_abi.consts.vcpu_reg.CNTKCTL_EL1; rfl + unfold vrVgicHcr; omega)) ?_ + rintro s4 ⟨σ4, hsp4, inv4⟩ + rw [u64_add_ok (by decide), bind_ok, u64_mul_ok (by decide), bind_ok, lift_ok2, bind_ok, + L.read_eq, bind_ok, lift_ok2, bind_ok, lift_ok2, bind_ok, L.write_eq, WP.spec_ok] + have hvp_off : (⟨BitVec.ofNat 64 ((⟨BitVec.ofNat 64 (64#u64.val + 37#u64.val)⟩ : U64).val * 8#u64.val)⟩ : U64) = + ⟨BitVec.ofNat 64 (8 * vppiWord)⟩ := rfl + rw [hvp_off, wfInv_frame_eq inv4 hvc hVcP hWin hfr_al hfr_dt (by unfold vppiWord; omega)] + set wv := L.mem s (wa frame ⟨BitVec.ofNat 64 (8 * vppiWord)⟩) + obtain ⟨v4, hkv4, -, -⟩ := inv4.core.obj vc hvc + set mk := Function.update v4.vcpuVppiMasked .vppiEventIRQ_VTimer (decide (wv.bv &&& 1 ≠ 0)) + have hba16 : Aligned (wa vc 16#u64) := aligned_wa (hVcP.vc_al hvc) 16#u64 (by decide) + have hw45 : WritesIn st s4 (L.wr s4 (wa vc 16#u64) (wv &&& 1#u64)) (WfSet vc v_tcb) := + wf_wr1 hba16 (Or.inl rfl) + have hne_off16 : ∀ v ∈ Z.V, ∀ o : U64, o.val < 64 → (v = vc → o.val ≠ 16) → wa vc 16#u64 ≠ wa v o := fun v hv o ho hne => by + by_cases hvv : v = vc + · subst hvv; exact hVcP.same_vcpu_ne hv (by decide) (by omega) (fun e => hne rfl (by have := congrArg UScalar.val e; change 16 = o.val at this; omega)) + · exact hVcP.disj_vcpu hvc hv (Ne.symm hvv) (by decide) (by omega) + have hC5 : VcpuCoreA (L := L) Z (L.wr s4 (wa vc 16#u64) (wv &&& 1#u64)) + (updKSc σ4 (specPtr vc) (.archObj (.vcpu { v4 with vcpuVppiMasked := mk }))) := by + refine ⟨fun v hv => ?_, fun p x hkp => ?_, fun t ht => ?_, fun y hy => ?_⟩ + · obtain ⟨x, hkx, hxt, hxr⟩ := inv4.core.obj v hv + rw [mem_wr_neA hba16 (hVcP.vc_al hv) (by simpa [wa_zero2] using hne_off16 v hv 0#u64 (by decide) (fun _ => by decide)), + mem_wr_neA hba16 (aligned_wa (hVcP.vc_al hv) 24#u64 (by decide)) (hne_off16 v hv 24#u64 (by decide) (fun _ => by decide))] + by_cases hvv : v = vc + · subst hvv; rw [hkv4] at hkx; cases hkx; exact ⟨_, updKSc_same _ _ _, hxt, hxr⟩ + · exact ⟨x, by rw [updKSc_other _ (fun e => hvv (specPtr_inj e))]; exact hkx, hxt, hxr⟩ + · by_cases hp : p = specPtr vc + · subst hp; exact ⟨vc, hvc, rfl⟩ + · exact inv4.core.back p x (by rwa [updKSc_other _ hp] at hkp) + · obtain ⟨xt, hkt, htv, htr⟩ := inv4.core.tcb t ht + have hne_s : specPtr t ≠ specPtr vc := fun e => by rw [e, hkv4] at hkt; cases hkt + exact ⟨xt, by rw [updKSc_other _ hne_s]; exact hkt, + by rw [mem_wr_neA hba16 (aligned_wa (hVcP.al_t t ht) 3664#u64 (by decide)) (hVcP.disj_tcb vc hvc t ht 16#u64 3664#u64 (by decide) (by decide))]; exact htv, + by unfold runVcpuA; rw [mem_wr_neA hba16 (aligned_wa (hVcP.al_t t ht) 3672#u64 (by decide)) (hVcP.disj_tcb vc hvc t ht 16#u64 3672#u64 (by decide) (by decide))]; exact htr⟩ + · have hos := hVcP.disj_os _ (vcpuHdr_in_vcPage hvc (o := 16#u64) (by decide) (by decide)) y (J.j5.2.1 y hy) + have haly := hVcP.al_os y (J.j5.2.1 y hy) + have hcap : capAtA (L.mem (L.wr s4 (wa vc 16#u64) (wv &&& 1#u64))) y = capAtA (L.mem s4) y := by + unfold capAtA + rw [mem_wr_neA hba16 haly hos.1, mem_wr_neA hba16 (aligned_wa haly 8#u64 (by decide)) hos.2.1] + rw [hcap]; exact inv4.core.cap y hy + have hS5 : VcpuStateSnapA Z.V (L.mem (L.wr s4 (wa vc 16#u64) (wv &&& 1#u64))) + (updKSc σ4 (specPtr vc) (.archObj (.vcpu { v4 with vcpuVppiMasked := mk }))) := by + refine ⟨fun v hv x hkx => ?_, fun v hv x hkx => ?_⟩ + · have halv := hVcP.vc_al hv + have hne_r : ∀ i < 64, wa vc 16#u64 ≠ vcpuRegWA v i := fun i hi => by + by_cases hvv : v = vc + · subst hvv; exact hVcP.same_vcpu_ne hv (by decide) (by rw [vcpuRegOffA_val hi]; omega) + (fun e => by have := congrArg UScalar.val e; rw [vcpuRegOffA_val hi] at this; change 16 = _ at this; omega) + · exact hVcP.disj_vcpu hvc hv (Ne.symm hvv) (by decide) (by rw [vcpuRegOffA_val hi]; omega) + rw [mem_wr_neA hba16 (aligned_wa halv 32#u64 (by decide)) (hne_off16 v hv 32#u64 (by decide) (fun _ => by decide)), + mem_wr_neA hba16 (aligned_wa halv 40#u64 (by decide)) (hne_off16 v hv 40#u64 (by decide) (fun _ => by decide)), + mem_wr_neA hba16 (aligned_wa halv 8#u64 (by decide)) (hne_off16 v hv 8#u64 (by decide) (fun _ => by decide))] + by_cases hvv : v = vc + · subst hvv; rw [updKSc_same] at hkx; cases hkx + obtain ⟨hr0, -, hro0, hrm0, how0⟩ := inv4.snap.snap v hv v4 hkv4 + refine ⟨fun i hi => by + rw [show wa v ⟨BitVec.ofNat 64 (64 + 8 * i)⟩ = vcpuRegWA v i from rfl, + mem_wr_neA hba16 (hVcP.vcPage_al (vcpuRegWA_in_vcPage hv hi)) (hne_r i hi)] + exact hr0 i hi, + by rw [L.mem_wr_same hba16]; simp only [mk, Function.update_self] + by_cases hb : wv.bv &&& 1 = 0 + · have h0 : wv &&& 1#u64 = 0#u64 := u64_eq_of_bv_eq2 hb + rw [h0]; simp only [hb, ne_eq, not_true_eq_false, decide_false, Bool.false_eq_true, ↓reduceIte] + · have h1 : wv &&& 1#u64 = 1#u64 := by + apply UScalar.eq_of_val_eq + have hle : (wv.bv &&& 1).toNat ≤ 1 := by rw [BitVec.toNat_and]; exact Nat.and_le_right + have hne : (wv.bv &&& 1).toNat ≠ 0 := fun e => hb (BitVec.eq_of_toNat_eq e) + change (wv.bv &&& 1).toNat = 1; omega + rw [h1]; simp only [hb, ne_eq, not_false_eq_true, decide_true, ↓reduceIte], + hro0, hrm0, how0⟩ + · rw [updKSc_other _ (fun e => hvv (specPtr_inj e))] at hkx + obtain ⟨hr0, hvp0, hro0, hrm0, how0⟩ := inv4.snap.snap v hv x hkx + refine ⟨fun i hi => by + rw [show wa v ⟨BitVec.ofNat 64 (64 + 8 * i)⟩ = vcpuRegWA v i from rfl, + mem_wr_neA hba16 (hVcP.vcPage_al (vcpuRegWA_in_vcPage hv hi)) (hne_r i hi)] + exact hr0 i hi, + by rw [mem_wr_neA hba16 (aligned_wa halv 16#u64 (by decide)) (hne_off16 v hv 16#u64 (by decide) (fun e => absurd e hvv))]; exact hvp0, + hro0, hrm0, how0⟩ + · by_cases hvv : v = vc + · subst hvv; rw [updKSc_same] at hkx; cases hkx; exact inv4.snap.mask v hv v4 hkv4 + · rw [updKSc_other _ (fun e => hvv (specPtr_inj e))] at hkx; exact inv4.snap.mask v hv x hkx + have inv5 := wfInv_step_vcpu (v2 := { v4 with vcpuVppiMasked := mk }) inv4 hkv4 rfl rfl rfl rfl rfl hw45 hC5 hS5 (fun t ht reg => by + obtain ⟨hK, hwa_r⟩ := regA_eq_wa_KA t reg; have hlt := regIdxA_lt reg; rw [hwa_r] + exact mem_wr_neA hba16 (aligned_wa (hVcP.al_t t ht) _ (by rw [hK]; omega)) + (hVcP.disj_tcb vc hvc t ht 16#u64 _ (by decide) (by rw [hK]; omega))) + unfold vcpu.VPPI_MASKED; refine ⟨_, ?_, inv5⟩ + unfold NonDet.when + have hlo : fl4_abi.consts.vcpu_reg.CNTV_CTL.val = vrCntvCtl := by unfold fl4_abi.consts.vcpu_reg.CNTV_CTL vrCntvCtl; rfl + have hhi : fl4_abi.consts.vcpu_reg.CNTKCTL_EL1.val = vrCntkctl := by unfold fl4_abi.consts.vcpu_reg.CNTKCTL_EL1 vrCntkctl; rfl + rw [if_pos h3b, ← hlo, ← hhi, nd_bind_oneSc hsp4] + have hload_vp : loadWordOffs (Z := Unit) (specPtr frame) vppiWord σ4 = ({(wv.bv, σ4)}, False) := by + rw [Vcpu.loadWordOffs_eqA (specPtr frame) vppiWord σ4 (hal3 vppiWord (by unfold vppiWord; omega)), inv4.um, ← hAg vppiWord (by unfold vppiWord; omega)] + rw [nd_bind_oneSc hload_vp, nd_bind_oneSc (getVcpu_eqA hkv4)] + exact setVcpu_eqA hkv4 + · have h3b : ¬ (mask.bv &&& smTimer ≠ 0) := fun h => h3 (bne_iff_ne.2 (fun e => h (congrArg UScalar.bv e))) + rw [if_neg h3, WP.spec_ok] + exact ⟨σ2, by unfold NonDet.when; rw [if_neg h3b]; rfl, inv2⟩ + refine WP.spec_bind hB3 ?_ + rintro s3 ⟨σ3, hsp3, inv3⟩ + -- Helper for `sync_vgic_status` on `WfInv` + have hSync : ∀ (sc : S) (σc : Fl4SpecA64.State Unit) (invc : WfInv st Z H s σ vc v_tcb sc σc) + {vc_o : Fl4SpecA64.Vcpu} (hkv : σc.kheap (specPtr vc) = some (.archObj (.vcpu vc_o))), + vcpu.sync_vgic_status st sc vc ⟨BitVec.ofNat 64 fl4NumLrs⟩ fl4GicV3 ⦃ sn => + WfInv st Z H s σ vc v_tcb sn + (updKSc σc (specPtr vc) (.archObj (.vcpu (syncVgicStatus vc_o fl4NumLrs fl4GicV3)))) ⦄ := by + intro sc σc invc vc_o hkv + obtain ⟨Jf_c, -, R6_c, -⟩ := hFin sc σc invc + have hV_c : VcpuRelA (L := L) Z OS Ub sc σc := + ⟨invc.core, fun v hv => by obtain ⟨x, hkx, -, -⟩ := invc.core.obj v hv; exact (invc.snap.snap v hv x hkx).2.2.2.2, + fun v hv x hkx => (invc.snap.snap v hv x hkx).2.1, + fun v hv x hkx => (invc.snap.snap v hv x hkx).2.2.1, + fun v hv x hkx => ⟨(invc.snap.snap v hv x hkx).2.2.2.1, invc.snap.mask v hv x hkx⟩, + fun v hv x hkx i hi => (invc.snap.snap v hv x hkx).1 i hi, + fun v hv _ ht hne => ⟨(Jf_c.vcRun.vtcb v hv).resolve_left (ht ▸ hne) |> fun h => ht ▸ h, ht ▸ R6_c.vcBack v hv (ht ▸ hne)⟩, + fun t ht _ hv hne => ⟨(Jf_c.vcRun.tcb t ht).resolve_left (hv ▸ hne) |> fun h => hv ▸ h, hv ▸ R6_c.vcSym1 t ht (hv ▸ hne)⟩, + fun v hv _ hc hne => ⟨(Jf_c.vcRun.vcpu v hv).resolve_left (hc ▸ hne) |> fun h => hc ▸ h, hc ▸ R6_c.vcSym2 v hv (hc ▸ hne)⟩, + fun c hc _ hv hne => ⟨(Jf_c.vcRun.run c hc).resolve_left (hv ▸ hne) |> fun h => hv ▸ h, hv ▸ R6_c.runBack c hc (hv ▸ hne)⟩, + Jf_c.runVcpu, Jf_c.vcRun.disj, + fun c hc => by + set it := L.mem sc (L.nodeAt sc c layout.node.IDLE_THREAD) + have hit : it ∈ Z.T := Jf_c.j.1.idleT c hc + have hspec : m.idleThreadOf c.val = specPtr it := Jf_c.j.1.sch.idleOf c hc + have hcn : c.val < n := hCsN c hc + constructor + · by_contra hne + obtain ⟨xt, hxt, -⟩ := Jf_c.ep.tcb it hit + have hbd := Jf_c.sidle.idleBound c.val hcn + rw [hspec] at hbd; unfold vcpuOfA at hbd; rw [hxt] at hbd; dsimp only at hbd + obtain ⟨xt', hxt', htv, -⟩ := invc.core.tcb it hit + rw [hxt] at hxt'; cases hxt'; rw [hbd] at htv; unfold optRefA at htv + rw [if_neg hne] at htv; cases htv + · by_contra hne + have hst : L.mem sc (stA it) = sched.TS_RUNNING_VM := Jf_c.runVcpu it hit hne + obtain ⟨x, hx, hts⟩ := Jf_c.sidle.f3 c.val hcn + rw [hspec] at hx + obtain ⟨x0, hx0, hrel⟩ := Jf_c.ep.tcb it hit + rw [hx] at hx0; cases hx0 + have h1 := hrel.st; rw [hst, hts] at h1; exact absurd h1 (by unfold sched.TS_RUNNING_VM tsCode; decide), + fun v hv hlive => by + by_contra hnone + have hdead : DeadA st OS .vcpu sc v := fun y hy hyc => hnone ⟨y, hy, by rw [capAtA_eq]; exact hyc⟩ + rcases hlive with h1 | h2 + · obtain ⟨x, hkx, hvt, -⟩ := invc.core.obj v hv + have hx := Jf_c.deadExt.vcpu v hv hdead x hkx + rw [hx] at hvt; unfold optRefA at hvt; rw [if_neg h1] at hvt; cases hvt + · exact h2 (Jf_c.deadExt.vcDeadRun v hv hdead), + invc.hHS.vcur ▸ J.curVcpuNone, invc.hw.1 ▸ hVcP⟩ + have hksFit : (L.ksb sc).val + ksSizeA ≤ 2 ^ 64 := by rw [invc.hw.1]; exact J.j.1.ksFit + have hIdleT : ∀ c ∈ Z.Cs, L.mem sc (L.nodeAt sc c layout.node.IDLE_THREAD) ∈ Z.T := fun c hc => Jf_c.j.1.idleT c hc + refine WP.spec_mono (sync_vgic_status_refinesA hV_c hksFit hCs128 hIdleT hvc hkv) ?_ + rintro sn ⟨hVn, hwn⟩ + have hw_sub : WritesIn st sc sn (WfSet vc v_tcb) := + ⟨hwn.1, hwn.2.1, fun q hqa hnf => hwn.2.2 q hqa (fun h => hnf (by + rcases h with rfl | rfl | rfl + · exact Or.inr (Or.inl ⟨vrVgicElrsr, by unfold vrVgicElrsr; decide, rfl⟩) + · exact Or.inr (Or.inl ⟨vrVgicEisr, by unfold vrVgicEisr; decide, rfl⟩) + · exact Or.inr (Or.inl ⟨vrVgicMisr, by unfold vrVgicMisr; decide, rfl⟩)))⟩ + have hSn : VcpuStateSnapA Z.V (L.mem sn) (updKSc σc (specPtr vc) (.archObj (.vcpu (syncVgicStatus vc_o fl4NumLrs fl4GicV3)))) := + ⟨fun v hv x hkx => ⟨fun i hi => hVn.regs v hv x hkx i hi, hVn.vppi v hv x hkx, hVn.runOff v hv x hkx, + (hVn.runOut v hv x hkx).1, hVn.owner v hv⟩, fun v hv x hkx => (hVn.runOut v hv x hkx).2⟩ + exact wfInv_step_vcpu invc hkv rfl rfl rfl rfl rfl hw_sub hVn.core hSn (fun t ht reg => by + obtain ⟨hK, hwa_r⟩ := regA_eq_wa_KA t reg; have hlt := regIdxA_lt reg; rw [hwa_r] + exact hwn.mem_eq (aligned_wa (hVcP.al_t t ht) _ (by rw [hK]; omega)) (fun h => by + have hdisj : ∀ r < 64, wa t _ ≠ vcpuRegWA vc r := fun r hr e => by + rcases vcpuRegWA_in_vcPage hvc hr with ⟨v', hv', ov, hov, -, heq2⟩ + exact hVcP.disj_tcb v' hv' t ht ov _ hov (by rw [hK]; omega) (heq2.symm.trans e.symm) + rcases h with h | h | h + · exact hdisj vrVgicElrsr (by unfold vrVgicElrsr; decide) h + · exact hdisj vrVgicEisr (by unfold vrVgicEisr; decide) h + · exact hdisj vrVgicMisr (by unfold vrVgicMisr; decide) h)) + -- Block 4: VGIC / VGIC_LR + Block 5: GPRS + rw [sm_vgic_ok, bind_ok, lift_ok2, bind_ok] + by_cases h4 : (mask &&& 8#u64 != 0#u64) = true + · have h4b : mask.bv &&& smVgic ≠ 0 := fun e => by + have : mask &&& 8#u64 = 0#u64 := u64_eq_of_bv_eq2 e; simp [this] at h4 + have h4or : mask.bv &&& (smVgic ||| smVgicLr) ≠ 0 := fun e => h4b (by + have := congrArg (· &&& smVgic) e + simpa [BitVec.and_or_distrib_left, BitVec.and_assoc, smVgic, smVgicLr] using this) + obtain ⟨hvg1, hvg_u32, hvg_len, hvg_arr⟩ := hVgic h4or + obtain ⟨g, hga, hgv⟩ := hP.gic a + rw [if_pos h4, vcpu_num_lrs_eqA hP a, bind_ok, hga, bind_ok] + have hgic_b : decide (g = 3#u64) = fl4GicV3 := hgv + rcases vgic with ⟨vgHcr, vgArr⟩; dsimp only at hvg1 hvg_arr + change (Std.bind (vcpu.reg_set st s3 vc fl4_abi.consts.vcpu_reg.VGIC_HCR vgHcr) _) ⦃ _ ⦄ + have hset_hcr := vcpu_reg_set_eqA (L := L) s3 vc (i := vrVgicHcr) (by unfold vrVgicHcr; decide) vgHcr + rw [show (⟨BitVec.ofNat 64 vrVgicHcr⟩ : U64) = fl4_abi.consts.vcpu_reg.VGIC_HCR from by + unfold vrVgicHcr fl4_abi.consts.vcpu_reg.VGIC_HCR; rfl] at hset_hcr + rw [hset_hcr, bind_ok] + obtain ⟨v3, hkv3, -, -⟩ := inv3.core.obj vc hvc + have inv4 := wfInv_reg_wr inv3 hvc hVcP J.j5.2.1 hkv3 (r := vrVgicHcr) (by unfold vrVgicHcr; decide) vgHcr + (fun _ => by rw [hvg1]; exact hvg_u32) + rw [u64_mul_ok (by unfold fl4_abi.consts.vcpu_reg.VGIC_VMCR; decide), bind_ok, lift_ok2, bind_ok, L.read_eq, bind_ok] + have hmul_vmcr : (⟨BitVec.ofNat 64 (fl4_abi.consts.vcpu_reg.VGIC_VMCR.val * 8#u64.val)⟩ : U64) = + ⟨BitVec.ofNat 64 (8 * fl4_abi.consts.vcpu_reg.VGIC_VMCR.val)⟩ := by + congr 1; exact congrArg (BitVec.ofNat 64) (by change _ * 8 = 8 * _; omega) + rw [hmul_vmcr, wfInv_frame_eq inv4 hvc hVcP hWin hfr_al hfr_dt (by unfold fl4_abi.consts.vcpu_reg.VGIC_VMCR; decide)] + have hvmcr_lt : fl4_abi.consts.vcpu_reg.VGIC_VMCR.val < 64 := by unfold fl4_abi.consts.vcpu_reg.VGIC_VMCR; decide + have hset_vmcr := vcpu_reg_set_eqA (L := L) (L.wr s3 (vcpuRegWA vc vrVgicHcr) vgHcr) vc hvmcr_lt + (L.mem s (wa frame ⟨BitVec.ofNat 64 (8 * fl4_abi.consts.vcpu_reg.VGIC_VMCR.val)⟩)) + rw [show (⟨BitVec.ofNat 64 fl4_abi.consts.vcpu_reg.VGIC_VMCR.val⟩ : U64) = fl4_abi.consts.vcpu_reg.VGIC_VMCR from by + unfold fl4_abi.consts.vcpu_reg.VGIC_VMCR; rfl] at hset_vmcr + rw [hset_vmcr, bind_ok] + obtain ⟨v4, hkv4, -, -⟩ := inv4.core.obj vc hvc + have inv5 := wfInv_reg_wr inv4 hvc hVcP J.j5.2.1 hkv4 hvmcr_lt + (L.mem s (wa frame ⟨BitVec.ofNat 64 (8 * fl4_abi.consts.vcpu_reg.VGIC_VMCR.val)⟩)) + (fun e => absurd e (by unfold fl4_abi.consts.vcpu_reg.VGIC_VMCR vrVgicHcr; decide)) + have hsp5 : copyRegsFromFrame (Z := Unit) (specPtr vc) (specPtr frame) [fl4_abi.consts.vcpu_reg.VGIC_VMCR.val] + (updKSc σ3 (specPtr vc) (.archObj (.vcpu (fl4VcpuRegSet vrVgicHcr vgHcr.bv v3)))) = + ({((), updKSc (updKSc σ3 (specPtr vc) (.archObj (.vcpu (fl4VcpuRegSet vrVgicHcr vgHcr.bv v3)))) + (specPtr vc) (.archObj (.vcpu (fl4VcpuRegSet fl4_abi.consts.vcpu_reg.VGIC_VMCR.val + (L.mem s (wa frame ⟨BitVec.ofNat 64 (8 * fl4_abi.consts.vcpu_reg.VGIC_VMCR.val)⟩)).bv v4))))}, False) := by + have hload_v : loadWordOffs (Z := Unit) (specPtr frame) fl4_abi.consts.vcpu_reg.VGIC_VMCR.val + (updKSc σ3 (specPtr vc) (.archObj (.vcpu (fl4VcpuRegSet vrVgicHcr vgHcr.bv v3)))) = + ({((L.mem s (wa frame ⟨BitVec.ofNat 64 (8 * fl4_abi.consts.vcpu_reg.VGIC_VMCR.val)⟩)).bv, + updKSc σ3 (specPtr vc) (.archObj (.vcpu (fl4VcpuRegSet vrVgicHcr vgHcr.bv v3))))}, False) := by + rw [Vcpu.loadWordOffs_eqA (specPtr frame) _ _ (hal3 _ (by omega)), inv4.um, ← hAg _ (by omega)] + simp only [copyRegsFromFrame, NonDet.mapMx] + change ((loadWordOffs (specPtr frame) _ >>= fun w => fl4VcpuSet (specPtr vc) _ w) >>= fun _ => NonDet.pure ()) _ = _ + rw [nd_bind_assocSc, nd_bind_oneSc hload_v, nd_bind_oneSc (fl4VcpuSet_eqA _ _ hkv4)]; rfl + refine WP.spec_bind (wfInv_copy_range inv5 hvc hVcP J.j5.2.1 hWin hfr_al hAg hfr_dt + (by unfold fl4_abi.consts.vcpu_reg.VGIC_AP0R0 fl4_abi.consts.vcpu_reg.VGIC_AP0R3; decide) + (by unfold fl4_abi.consts.vcpu_reg.VGIC_AP0R3; decide) + (fun r hr1 _ => by + have : fl4_abi.consts.vcpu_reg.VGIC_AP0R0.val = 40 := by unfold fl4_abi.consts.vcpu_reg.VGIC_AP0R0; rfl + unfold vrVgicHcr; omega)) ?_ + rintro s6 ⟨σ6, hsp6, inv6⟩ + have hAp1 : (if g = 3#u64 then + vcpu.copy_reg_range_from_frame st s6 vc frame fl4_abi.consts.vcpu_reg.VGIC_AP1R0 fl4_abi.consts.vcpu_reg.VGIC_AP1R3 + else ok s6) ⦃ s7 => + ∃ σ7, (NonDet.when fl4GicV3 <| copyRegsFromFrame (Z := Unit) (specPtr vc) (specPtr frame) (idxRange vrVgicAp1r0 vrVgicAp1r3)) σ6 = ({((), σ7)}, False) ∧ + WfInv st Z H s σ vc v_tcb s7 σ7 ⦄ := by + by_cases hg3 : g = 3#u64 + · have hgv3 : fl4GicV3 = true := by rw [← hgic_b]; simp [hg3] + rw [if_pos hg3] + refine WP.spec_mono (wfInv_copy_range inv6 hvc hVcP J.j5.2.1 hWin hfr_al hAg hfr_dt + (by unfold fl4_abi.consts.vcpu_reg.VGIC_AP1R0 fl4_abi.consts.vcpu_reg.VGIC_AP1R3; decide) + (by unfold fl4_abi.consts.vcpu_reg.VGIC_AP1R3; decide) + (fun r hr1 _ => by + have : fl4_abi.consts.vcpu_reg.VGIC_AP1R0.val = 44 := by unfold fl4_abi.consts.vcpu_reg.VGIC_AP1R0; rfl + unfold vrVgicHcr; omega)) ?_ + rintro s7 ⟨σ7, hsp7, inv7⟩ + have hlo : fl4_abi.consts.vcpu_reg.VGIC_AP1R0.val = vrVgicAp1r0 := by unfold fl4_abi.consts.vcpu_reg.VGIC_AP1R0 vrVgicAp1r0; rfl + have hhi : fl4_abi.consts.vcpu_reg.VGIC_AP1R3.val = vrVgicAp1r3 := by unfold fl4_abi.consts.vcpu_reg.VGIC_AP1R3 vrVgicAp1r3; rfl + exact ⟨σ7, by unfold NonDet.when; rw [if_pos hgv3, ← hlo, ← hhi]; exact hsp7, inv7⟩ + · have hgv3 : fl4GicV3 = false := by rw [← hgic_b]; simp [hg3] + rw [if_neg hg3, WP.spec_ok]; exact ⟨σ6, by unfold NonDet.when; rw [if_neg (by simp [hgv3])]; rfl, inv6⟩ + refine WP.spec_bind hAp1 ?_ + rintro s7 ⟨σ7, hsp7, inv7⟩ + refine WP.spec_bind (wfInv_write_lrs inv7 hvc hVcP J.j5.2.1 hvg_len hvg_arr) ?_ + rintro s8 ⟨σ8, hsp8, inv8⟩ + rw [hgic_b] + obtain ⟨v8, hkv8, -, -⟩ := inv8.core.obj vc hvc + refine WP.spec_bind (hSync s8 σ8 inv8 hkv8) ?_ + intro s9 inv9 + refine WP.spec_mono (hGprs s9 _ inv9) ?_ + rintro s10 ⟨σ10, hsp10, inv10⟩ + refine ⟨σ10, ?_, hFin s10 σ10 inv10⟩ + unfold fl4WriteStateFromFrame + rw [nd_bind_oneSc hsp1, nd_bind_oneSc hsp2, nd_bind_oneSc hsp3, if_pos h4b, nd_bind_assocSc, + nd_bind_oneSc (fl4VcpuSet_eqA vrVgicHcr vgicSpec.1 hkv3)] + have hcomm_all : (do + NonDet.mapMx (fun (i, w) => fl4VcpuSet (Z := Unit) (specPtr vc) (vrVgicLr0 + i) w) ((List.range fl4NumLrs).zip vgicSpec.2) + copyRegsFromFrame (Z := Unit) (specPtr vc) (specPtr frame) [vrVgicVmcr] + copyRegsFromFrame (Z := Unit) (specPtr vc) (specPtr frame) (idxRange vrVgicAp0r0 vrVgicAp0r3) + NonDet.when fl4GicV3 <| copyRegsFromFrame (Z := Unit) (specPtr vc) (specPtr frame) (idxRange vrVgicAp1r0 vrVgicAp1r3)) + (updKSc σ3 (specPtr vc) (.archObj (.vcpu (fl4VcpuRegSet vrVgicHcr vgicSpec.1 v3)))) = ({((), σ8)}, False) := by + have hlrs_b : ∀ p ∈ (List.range fl4NumLrs).zip vgicSpec.2, p.1 < 16 := fun p hp => by + have := (List.of_mem_zip hp).1; simp only [List.mem_range] at this; have := fl4NumLrs_le_16; omega + have hreg_list : ∃ rs : List Nat, (∀ r ∈ rs, r < 48) ∧ + (∀ σx, (do copyRegsFromFrame (Z := Unit) (specPtr vc) (specPtr frame) [vrVgicVmcr] + copyRegsFromFrame (Z := Unit) (specPtr vc) (specPtr frame) (idxRange vrVgicAp0r0 vrVgicAp0r3) + NonDet.when fl4GicV3 <| copyRegsFromFrame (Z := Unit) (specPtr vc) (specPtr frame) (idxRange vrVgicAp1r0 vrVgicAp1r3)) σx = + copyRegsFromFrame (Z := Unit) (specPtr vc) (specPtr frame) rs σx) := by + cases fl4GicV3 + · refine ⟨[vrVgicVmcr] ++ idxRange vrVgicAp0r0 vrVgicAp0r3, fun r hr => by + simp [vrVgicVmcr, idxRange, vrVgicAp0r0, vrVgicAp0r3] at hr; omega, fun σx => ?_⟩ + unfold copyRegsFromFrame NonDet.when; rw [if_neg (by decide), mapMx_appendA] + change _ = (copyRegsFromFrame (specPtr vc) (specPtr frame) [vrVgicVmcr] >>= fun _ => + copyRegsFromFrame (specPtr vc) (specPtr frame) (idxRange vrVgicAp0r0 vrVgicAp0r3)) σx + have hpr : (fun _ : Unit => copyRegsFromFrame (Z := Unit) (specPtr vc) (specPtr frame) (idxRange vrVgicAp0r0 vrVgicAp0r3) >>= fun _ => NonDet.pure ()) = + (fun _ : Unit => copyRegsFromFrame (Z := Unit) (specPtr vc) (specPtr frame) (idxRange vrVgicAp0r0 vrVgicAp0r3)) := + funext fun _ => funext fun σy => nd_bind_pure_r _ σy + unfold copyRegsFromFrame at hpr; rw [hpr]; rfl + · refine ⟨[vrVgicVmcr] ++ idxRange vrVgicAp0r0 vrVgicAp0r3 ++ idxRange vrVgicAp1r0 vrVgicAp1r3, fun r hr => by + simp [vrVgicVmcr, idxRange, vrVgicAp0r0, vrVgicAp0r3, vrVgicAp1r0, vrVgicAp1r3] at hr; omega, fun σx => ?_⟩ + unfold copyRegsFromFrame NonDet.when; rw [ite_eq_left rfl, mapMx_appendA] + have h1 : NonDet.mapMx (fun r => loadWordOffs (Z := Unit) (specPtr frame) r >>= fun w => fl4VcpuSet (specPtr vc) r w) + ([vrVgicVmcr] ++ idxRange vrVgicAp0r0 vrVgicAp0r3) = + (NonDet.mapMx (fun r => loadWordOffs (Z := Unit) (specPtr frame) r >>= fun w => fl4VcpuSet (specPtr vc) r w) [vrVgicVmcr] >>= fun _ => + NonDet.mapMx (fun r => loadWordOffs (Z := Unit) (specPtr frame) r >>= fun w => fl4VcpuSet (specPtr vc) r w) (idxRange vrVgicAp0r0 vrVgicAp0r3)) := + funext fun τ => mapMx_appendA _ _ _ τ + rw [h1, nd_bind_assocSc] + obtain ⟨rs, hrs48, hrs_eq⟩ := hreg_list + have hcomm := copyRegsFromFrame_lrs_comm2 (vc := specPtr vc) (frame := specPtr frame) + (updKSc_same σ3 (specPtr vc) (.archObj (.vcpu (fl4VcpuRegSet vrVgicHcr vgicSpec.1 v3)))) + hal3 ((List.range fl4NumLrs).zip vgicSpec.2) hlrs_b rs hrs48 + have hrun : (copyRegsFromFrame (Z := Unit) (specPtr vc) (specPtr frame) rs >>= fun _ => + NonDet.mapMx (fun (i, w) => fl4VcpuSet (Z := Unit) (specPtr vc) (vrVgicLr0 + i) w) ((List.range fl4NumLrs).zip vgicSpec.2)) + (updKSc σ3 (specPtr vc) (.archObj (.vcpu (fl4VcpuRegSet vrVgicHcr vgicSpec.1 v3)))) = ({((), σ8)}, False) := by + have hstep4 : copyRegsFromFrame (Z := Unit) (specPtr vc) (specPtr frame) rs + (updKSc σ3 (specPtr vc) (.archObj (.vcpu (fl4VcpuRegSet vrVgicHcr vgicSpec.1 v3)))) = ({((), σ7)}, False) := by + have hvmcr_eq : fl4_abi.consts.vcpu_reg.VGIC_VMCR.val = vrVgicVmcr := by unfold fl4_abi.consts.vcpu_reg.VGIC_VMCR vrVgicVmcr; rfl + have hap00 : fl4_abi.consts.vcpu_reg.VGIC_AP0R0.val = vrVgicAp0r0 := by unfold fl4_abi.consts.vcpu_reg.VGIC_AP0R0 vrVgicAp0r0; rfl + have hap03 : fl4_abi.consts.vcpu_reg.VGIC_AP0R3.val = vrVgicAp0r3 := by unfold fl4_abi.consts.vcpu_reg.VGIC_AP0R3 vrVgicAp0r3; rfl + rw [hvmcr_eq] at hsp5 hsp6; rw [hap00, hap03] at hsp6 + rw [← hrs_eq, ← hvg1, nd_bind_oneSc hsp5, nd_bind_oneSc hsp6]; exact hsp7 + rw [nd_bind_oneSc hstep4]; exact hsp8 + rw [← hrun, ← hcomm] + have hfn : (fun _ : Unit => copyRegsFromFrame (Z := Unit) (specPtr vc) (specPtr frame) rs) = + (fun _ : Unit => do copyRegsFromFrame (Z := Unit) (specPtr vc) (specPtr frame) [vrVgicVmcr] + copyRegsFromFrame (Z := Unit) (specPtr vc) (specPtr frame) (idxRange vrVgicAp0r0 vrVgicAp0r3) + NonDet.when fl4GicV3 <| copyRegsFromFrame (Z := Unit) (specPtr vc) (specPtr frame) (idxRange vrVgicAp1r0 vrVgicAp1r3)) := + funext fun _ => funext fun σx => (hrs_eq σx).symm + rw [hfn] + have hcomm_k : ∀ (k : Unit → SMonad Unit Unit), + (NonDet.mapMx (fun (i, w) => fl4VcpuSet (Z := Unit) (specPtr vc) (vrVgicLr0 + i) w) ((List.range fl4NumLrs).zip vgicSpec.2) >>= fun _ => + copyRegsFromFrame (Z := Unit) (specPtr vc) (specPtr frame) [vrVgicVmcr] >>= fun _ => + copyRegsFromFrame (Z := Unit) (specPtr vc) (specPtr frame) (idxRange vrVgicAp0r0 vrVgicAp0r3) >>= fun _ => + NonDet.when fl4GicV3 (copyRegsFromFrame (Z := Unit) (specPtr vc) (specPtr frame) (idxRange vrVgicAp1r0 vrVgicAp1r3)) >>= k) + (updKSc σ3 (specPtr vc) (.archObj (.vcpu (fl4VcpuRegSet vrVgicHcr vgicSpec.1 v3)))) = k () σ8 := fun k => by + have ha : ((do + NonDet.mapMx (fun (i, w) => fl4VcpuSet (Z := Unit) (specPtr vc) (vrVgicLr0 + i) w) ((List.range fl4NumLrs).zip vgicSpec.2) + copyRegsFromFrame (Z := Unit) (specPtr vc) (specPtr frame) [vrVgicVmcr] + copyRegsFromFrame (Z := Unit) (specPtr vc) (specPtr frame) (idxRange vrVgicAp0r0 vrVgicAp0r3) + NonDet.when fl4GicV3 <| copyRegsFromFrame (Z := Unit) (specPtr vc) (specPtr frame) (idxRange vrVgicAp1r0 vrVgicAp1r3)) >>= k) + (updKSc σ3 (specPtr vc) (.archObj (.vcpu (fl4VcpuRegSet vrVgicHcr vgicSpec.1 v3)))) = k () σ8 := + nd_bind_oneSc hcomm_all k + have hbind_fn : ∀ (m1 m2 : SMonad Unit Unit) (f : Unit → SMonad Unit Unit), + ((m1 >>= fun _ => m2) >>= f) = (m1 >>= fun _ => m2 >>= f) := fun _ _ _ => funext fun τ => nd_bind_assocSc _ _ _ τ + simpa only [nd_bind_assocSc, hbind_fn] using ha + rw [hcomm_k, nd_bind_oneSc (getVcpu_eqA hkv8), nd_bind_oneSc (setVcpu_eqA hkv8)] + exact hsp10 + · have h4b : ¬ (mask.bv &&& smVgic ≠ 0) := fun h => h4 (bne_iff_ne.2 (fun e => h (congrArg UScalar.bv e))) + rw [if_neg h4, sm_vgic_lr_ok, bind_ok, lift_ok2, bind_ok] + by_cases h5 : (mask &&& 32#u64 != 0#u64) = true + · have h5b : mask.bv &&& smVgicLr ≠ 0 := fun e => by + have : mask &&& 32#u64 = 0#u64 := u64_eq_of_bv_eq2 e; simp [this] at h5 + have h5or : mask.bv &&& (smVgic ||| smVgicLr) ≠ 0 := fun e => h5b (by + have := congrArg (· &&& smVgicLr) e + simpa [BitVec.and_or_distrib_left, BitVec.and_assoc, smVgic, smVgicLr] using this) + obtain ⟨hvg1, hvg_u32, hvg_len, hvg_arr⟩ := hVgic h5or + obtain ⟨g, hga, hgv⟩ := hP.gic a + rw [if_pos h5, vcpu_num_lrs_eqA hP a, bind_ok, hga, bind_ok] + have hgic_b : decide (g = 3#u64) = fl4GicV3 := hgv + rcases vgic with ⟨vgHcr, vgArr⟩; dsimp only at hvg1 hvg_arr + change (Std.bind (vcpu.reg_set st s3 vc fl4_abi.consts.vcpu_reg.VGIC_HCR vgHcr) _) ⦃ _ ⦄ + have hset_hcr := vcpu_reg_set_eqA (L := L) s3 vc (i := vrVgicHcr) (by unfold vrVgicHcr; decide) vgHcr + rw [show (⟨BitVec.ofNat 64 vrVgicHcr⟩ : U64) = fl4_abi.consts.vcpu_reg.VGIC_HCR from by + unfold vrVgicHcr fl4_abi.consts.vcpu_reg.VGIC_HCR; rfl] at hset_hcr + rw [hset_hcr, bind_ok] + obtain ⟨v3, hkv3, -, -⟩ := inv3.core.obj vc hvc + have inv4 := wfInv_reg_wr inv3 hvc hVcP J.j5.2.1 hkv3 (r := vrVgicHcr) (by unfold vrVgicHcr; decide) vgHcr + (fun _ => by rw [hvg1]; exact hvg_u32) + refine WP.spec_bind (wfInv_write_lrs inv4 hvc hVcP J.j5.2.1 hvg_len hvg_arr) ?_ + rintro s5 ⟨σ5, hsp5, inv5⟩ + rw [hgic_b] + obtain ⟨v5, hkv5, -, -⟩ := inv5.core.obj vc hvc + refine WP.spec_bind (hSync s5 σ5 inv5 hkv5) ?_ + intro s6 inv6 + refine WP.spec_mono (hGprs s6 _ inv6) ?_ + rintro s7 ⟨σ7, hsp7, inv7⟩ + refine ⟨σ7, ?_, hFin s7 σ7 inv7⟩ + unfold fl4WriteStateFromFrame + rw [nd_bind_oneSc hsp1, nd_bind_oneSc hsp2, nd_bind_oneSc hsp3, if_neg h4b] + unfold NonDet.when; rw [if_pos h5b, nd_bind_assocSc, nd_bind_assocSc, + nd_bind_oneSc (fl4VcpuSet_eqA vrVgicHcr vgicSpec.1 hkv3), ← hvg1, nd_bind_oneSc hsp5, + nd_bind_assocSc, nd_bind_oneSc (getVcpu_eqA hkv5), nd_bind_oneSc (setVcpu_eqA hkv5)]; exact hsp7 + · have h5b : ¬ (mask.bv &&& smVgicLr ≠ 0) := fun h => h5 (bne_iff_ne.2 (fun e => h (congrArg UScalar.bv e))) + rw [if_neg h5] + refine WP.spec_mono (hGprs s3 σ3 inv3) ?_ + rintro s4 ⟨σ4, hsp4, inv4⟩ + refine ⟨σ4, ?_, hFin s4 σ4 inv4⟩ + unfold fl4WriteStateFromFrame + rw [nd_bind_oneSc hsp1, nd_bind_oneSc hsp2, nd_bind_oneSc hsp3, if_neg h4b] + unfold NonDet.when; rw [if_neg h5b, nd_bind_oneSc (m := NonDet.pure ()) rfl]; exact hsp4 + +/-! ### Label 68 (`ARMVCPUWriteState`) on `JSyscallA → JStage3A` -/ + +/-- Local `read_vgic_frame` refinement from slice-local `FrameInputAgreeA` (zero global `FrameMemRelA`). -/ +theorem read_vgic_frame_localA {Z : XSetsA} {s : S} {σ : Fl4SpecA64.State Unit} {frame : U64} + (hfit : frame.val + 1024 ≤ 2 ^ 64) (hfr_al : Aligned frame) + (hAgree : FrameInputAgreeA st s σ frame) + (hKsOff : ∀ k < 128, ¬ ((L.ksb s).val ≤ (wa frame ⟨BitVec.ofNat 64 (8 * k)⟩).val ∧ + (wa frame ⟨BitVec.ofNat 64 (8 * k)⟩).val < (L.ksb s).val + ksSizeA)) : + vcpu.read_vgic_frame st s frame ⟨BitVec.ofNat 64 fl4NumLrs⟩ fl4GicV3 ⦃ res => + match res with + | none => readVgicFrame (specPtr frame) σ = ({(none, σ)}, False) + | some (hcr, arr) => + ∃ lrsList : List Word, + lrsList.length = fl4NumLrs ∧ + (∀ j (hj : j < fl4NumLrs), (arr.val[j]!).bv = lrsList[j]!) ∧ + readVgicFrame (specPtr frame) σ = ({(some (hcr.bv, lrsList), σ)}, False) ⦄ := by + let UbW : Set U64 := { b | ∃ k < 128, b = wa frame ⟨BitVec.ofNat 64 (8 * k)⟩ } + let Z0 : XSetsA := { Z with th := fun _ => none, T := ∅, C := ∅ } + have hF0 : FrameMemRelA Z0 ∅ UbW (L.ksb s) (L.mem s) σ.machineState.underlyingMemory := + ⟨fun b ⟨k, hk, heq⟩ => heq ▸ aligned_wa hfr_al _ (by + change (BitVec.ofNat 64 (8 * k)).toNat % 8 = 0 + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega : 8 * k < 2 ^ 64)]; omega), + fun b ⟨k, hk, heq⟩ => by + rw [heq, specPtr_wa_wordA (by omega), loadWordAtA_eq_loadWordAt] + exact hAgree k hk, + fun _ h => absurd h (Set.notMem_empty _), + fun _ _ _ h => absurd h (Set.notMem_empty _), + fun _ _ _ h => by rcases h with ⟨_, ⟨_, h, -⟩ | ⟨h, -⟩⟩ <;> simp [Z0] at h, + fun _ _ _ h => absurd h (Set.notMem_empty _), + fun _ _ _ h => absurd h (Set.notMem_empty _), + fun _ ⟨k, hk, heq⟩ => heq ▸ hKsOff k hk⟩ + exact read_vgic_frame_refinesA hF0 ⟨hfit, fun k hk => ⟨k, hk, rfl⟩⟩ + +/-- **Label 68 (`ARMVCPUWriteState`)** on `JSyscallA → JStage3A` (zero global `FrameMemRelA`; +slice-local `FrameInputAgreeA` `[scope limit]` when `mask ≠ 0`). -/ +theorem JSyscallA.vcpuWriteStateA {ar : machine.Arch A} + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) (hP : VcpuPlatAgreeA ar) (hAK : VcpuArchKeepsA ar AR) + (hCs : ∀ c ∈ Z.Cs, c.val < Fl4SpecA64.fl4A64Platform.numNodes) (hCsN : ∀ c ∈ Z.Cs, c.val < n) + (h1 : Fl4SpecA64.fl4A64Platform.numNodes = 1) + (hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM) + {slot vc length buf : U64} {blk call don fp : Bool} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) + (hcap : capA st s slot = .Vcpu vc) (hvc : vc ∈ Z.V) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + {mi : MessageInfo} {sl : CslotPtr} {excaps : List (Fl4SpecA64.Cap × CslotPtr)} + {buffer : Option ObjRef} {args : List U64} + (hfp : VcpuFrontPinA Z.th σ slot mi sl (.archObjectCap (.vcpuCap (specPtr vc))) excaps buffer (args.map UScalar.bv)) + (hlbl : fl4LabelOf mi.miLabel = .ext .armVCPUWriteState) + (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) + (hex0 : ∀ s1, Array.index_usize extra 0#usize = ok s1 → s1 ≠ 0#u64 → + ∃ c0 sl0, excaps[0]? = some (c0, sl0) ∧ CapRelA (capA st s s1) c0) + (hNoS2 : ∀ s1 p sz r d mp ept asid va, Array.index_usize extra 0#usize = ok s1 → + capA st s s1 = .Frame p sz r d mp ept asid va → ept = false) + -- `[scope limit]` slice-local input-frame agreement on the 1 KiB window when `mask ≠ 0`. + (hAgree : ∀ s1 p sz r mp ept asid va offset mask, + Array.index_usize extra 0#usize = ok s1 → s1 ≠ 0#u64 → + capA st s s1 = .Frame p sz r false mp ept asid va → + args[0]? = some offset → args[1]? = some mask → mask ≠ 0#u64 → + FrameInputAgreeA st s σ (wa p offset)) + {s3 : S} {a3 : A} + (hdec : syscall.decode_invocation st ar s a fl4_abi.label.ARMVCPUWriteState length slot (.Vcpu vc) + blk call don fp buf extra = ok (core.result.Result.Ok (), s3, a3)) : + ∃ (x : Fl4SpecA64.Tcb) (frame mask : U64) (vgicSpec : Word × List Word) (σ3 : Fl4SpecA64.State Unit), + σ.kheap σ.curThread = some (.tcb x) ∧ + VcpuDecPinA call Z.th σ slot (.invokeArchObject (.vcpu (.stateTransfer (specPtr vc) (specPtr frame) mask.bv false vgicSpec))) ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + (.ok [], σ3) ∈ ((fl4ArchPerformInvocation (Z := Unit) + (.vcpu (.stateTransfer (specPtr vc) (specPtr frame) mask.bv false vgicSpec))).run + (stUpdA σ σ.curThread x .restart)).1 ∧ + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s3 a3 σ3 m := by + have J3 := J.toJStage3A + obtain ⟨Pt, Ub, Pp7, Tb7, Hr, Te, Kt, Lv7, Re, bits7, KLv, W⟩ := J7A.unp J3.j7 + have hpl := J3.vcPlacedA W + have hU := J3.ubFootDisjA W + have C := J.vcpuDecCtxA W hCs hCsN h1 hvc + have RS := J3.rsSiteA hext hslot hcov J.actW J.running J.cur + have hCs128 : ∀ c ∈ Z.Cs, c.val < 128 := fun c hc => by + have := hCs c hc; rw [h1] at this; omega + have hdec0 := hdec + unfold syscall.decode_invocation at hdec + rw [show sched.cur_thread st s = ok (L.curW s) from eq_of_spec_fd (sched.cur_thread.spec s), bind_ok] at hdec + dsimp only at hdec; unfold vcpu.decode at hdec + simp only [fl4_abi.label.ARMVCPUWriteState, fl4_abi.label.ARMVCPUReadState, fl4_abi.label.ARMVCPUSetTCB, + fl4_abi.label.ARMVCPUInjectIRQ, fl4_abi.label.ARMVCPUReadReg, fl4_abi.label.ARMVCPUWriteReg, + fl4_abi.label.ARMVCPUAckVPPI] at hdec + rw [if_neg (by decide), if_neg (by decide), if_neg (by decide), if_neg (by decide), if_neg (by decide), + if_neg (by decide), if_pos trivial] at hdec + unfold vcpu.decode_state_transfer at hdec + by_cases hlen2 : length < 2#u64 + · rw [if_pos hlen2] at hdec; simp [error.truncated, bind_ok] at hdec + rw [if_neg hlen2] at hdec + have hge2 : 2 ≤ args.length := by have : ¬ length.val < 2 := hlen2; omega + cases hargs_eq : args with + | nil => simp [hargs_eq] at hge2 + | cons offset rest1 => + cases hrest_eq : rest1 with + | nil => simp [hargs_eq, hrest_eq] at hge2 + | cons mask rest2 => + have hlt0 : 0 < extra.val.length := by rw [extra.property]; decide + have hs1_idx : Array.index_usize extra 0#usize = ok (extra.val[0]'hlt0) := + eq_of_spec_fd (Array.index_usize_spec extra 0#usize hlt0) + set s1 := extra.val[0]'hlt0 + rw [hs1_idx, bind_ok] at hdec + have hisn : obj.Slot.is_null s1 = ok (decide (s1 = 0#u64)) := by unfold obj.Slot.is_null; rfl + rw [hisn, bind_ok] at hdec + by_cases hs1_0 : s1 = 0#u64 + · simp [hs1_0, error.truncated, bind_ok] at hdec + simp only [hs1_0, decide_false, Bool.false_eq_true, ↓reduceIte] at hdec + have hs1_mem : s1 ∈ extra.val := List.getElem_mem hlt0 + have hs1_OS : s1 ∈ OS := hext s1 hs1_mem hs1_0 + obtain ⟨c0, sl0, hex0_eq, hcapRel0⟩ := hex0 s1 hs1_idx hs1_0 + rw [show sched.cur_thread st s = ok (L.curW s) from eq_of_spec_fd (sched.cur_thread.spec s), bind_ok, + hargs 0#u64 offset (by simp [hargs_eq]), bind_ok, + hargs 1#u64 mask (by simp [hargs_eq, hrest_eq]), bind_ok] at hdec + have hall : fl4_abi.consts.vcpu_state_mask.ALL = ok 63#u64 := + eq_of_spec_fd Fl4ProofsA64.P0.Consts.fl4_abi.consts.vcpu_state_mask.ALL.spec + rw [hall, bind_ok, lift_ok2, bind_ok, lift_ok2, bind_ok] at hdec + by_cases hmask_bad : (mask &&& ~~~63#u64 != 0#u64) = true + · rw [if_pos hmask_bad] at hdec; simp [error.invalid_arg, bind_ok] at hdec + rw [if_neg hmask_bad] at hdec + have hmask_ok : mask.bv &&& ~~~smAll = 0 := by + have h0 : mask &&& ~~~63#u64 = 0#u64 := by + by_contra hne; exact hmask_bad (bne_iff_ne.2 hne) + exact congrArg UScalar.bv h0 + have hcap_s1 : obj.Slot.cap st s1 s = ok (capA st s s1) := Fl4ProofsA64.P1.W7.R.ST.capA_eq s s1 + rw [hcap_s1, bind_ok] at hdec + have hfr_ex : ∃ ptr sz rights is_dev mp ept asid va, + capA st s s1 = .Frame ptr sz rights is_dev mp ept asid va := by + cases hc : capA st s s1 <;> rw [hc] at hdec <;> + first | (simp [error.invalid_cap, bind_ok] at hdec; done) | exact ⟨_, _, _, _, _, _, _, _, rfl⟩ + obtain ⟨ptr, sz, rights, is_dev, mp, ept, asid, va, hc_s1⟩ := hfr_ex + rw [hc_s1] at hdec hcapRel0; dsimp only at hdec + by_cases hdev : is_dev = true + · rw [if_pos hdev] at hdec; simp [error.invalid_cap, bind_ok] at hdec + have hdevF : is_dev = false := by cases is_dev <;> simp_all + subst hdevF + rw [if_neg (by decide)] at hdec + by_cases hko : rights = capability.VM_KERNEL_ONLY + · rw [if_pos hko] at hdec; simp [error.invalid_cap, bind_ok] at hdec + rw [if_neg hko] at hdec + have hbits_ex : ∃ bits, capability.FrameSize.bits sz = ok bits := by + cases sz <;> exact ⟨_, rfl⟩ + obtain ⟨bits, hbits⟩ := hbits_ex + obtain ⟨hbits_sp, hbits_lt, hbits_and, hshl⟩ := frameSize_bits_spec2 hbits + rw [hbits, bind_ok, lift_ok2, bind_ok, hshl, bind_ok, lift_ok2, bind_ok] at hdec + by_cases hoff64 : (offset &&& 63#u64 != 0#u64) = true + · rw [if_pos hoff64] at hdec; simp [error.invalid_arg, bind_ok] at hdec + rw [if_neg hoff64] at hdec + have hoff64_eq : offset &&& 63#u64 = 0#u64 := by + by_contra hne; exact hoff64 (bne_iff_ne.2 hne) + set size1 : U64 := ⟨BitVec.ofNat 64 (2 ^ bits.val)⟩ + have hsize1_val : size1.val = 2 ^ bits.val := by + show (BitVec.ofNat 64 (2 ^ bits.val)).toNat = 2 ^ bits.val + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (Nat.pow_lt_pow_right (by omega) hbits_lt) + by_cases hoff_ge : size1 ≤ offset + · rw [if_pos hoff_ge] at hdec; simp [error.invalid_arg, bind_ok] at hdec + rw [if_neg hoff_ge] at hdec + have hoff_lt : offset.val < 2 ^ bits.val := by + have : ¬ size1.val ≤ offset.val := hoff_ge; omega + have hsub : size1 - offset = ok ⟨BitVec.ofNat 64 (2 ^ bits.val - offset.val)⟩ := by + change UScalar.sub size1 offset = _; unfold UScalar.sub + rw [if_neg (Nat.not_lt.mpr (by omega : offset.val ≤ size1.val))] + congr 1; apply UScalar.eq_of_val_eq; rw [hsize1_val]; rfl + rw [hsub, bind_ok] at hdec + have hsz_c : fl4_abi.consts.vcpu_state_mask.SIZE = 1024#u64 := by + unfold fl4_abi.consts.vcpu_state_mask.SIZE; rfl + rw [hsz_c] at hdec + by_cases hrem_lt : (⟨BitVec.ofNat 64 (2 ^ bits.val - offset.val)⟩ : U64) < 1024#u64 + · rw [if_pos hrem_lt] at hdec; simp [error.invalid_arg, bind_ok] at hdec + rw [if_neg hrem_lt] at hdec + have hoff_sz : 2 ^ bits.val - offset.val ≥ 1024 := by + have : ¬ (⟨BitVec.ofNat 64 (2 ^ bits.val - offset.val)⟩ : U64).val < 1024 := hrem_lt + have hv : (⟨BitVec.ofNat 64 (2 ^ bits.val - offset.val)⟩ : U64).val = 2 ^ bits.val - offset.val := by + show (BitVec.ofNat 64 _).toNat = _; rw [BitVec.toNat_ofNat] + exact Nat.mod_eq_of_lt (by have := Nat.pow_lt_pow_right (a := 2) (by omega) hbits_lt; omega) + omega + rw [lift_ok2, bind_ok, vcpu_num_lrs_eqA hP a, bind_ok] at hdec + obtain ⟨g, hga, hgv⟩ := hP.gic a + rw [hga, bind_ok] at hdec + rw [sm_vgic_ok, bind_ok, sm_vgic_lr_ok, bind_ok, lift_ok2, bind_ok, lift_ok2, bind_ok] at hdec + -- Frame window properties from `J7A` (`W.cc`) + set frame := wa ptr offset + have hptrH : H ptr = some (.other bits.val) := + W.cc.fo s1 hs1_OS ptr sz rights mp ept asid va hc_s1 bits hbits + have hptrFit : ptr.val + 2 ^ bits.val ≤ 2 ^ 64 := by + have := J3.sys.ent.heap.fits ptr _ hptrH; simpa [HKind.bits] using this + have hptrAl : ptr.val % 2 ^ bits.val = 0 := by + have := J3.sys.ent.heap.align ptr _ hptrH; simpa [HKind.bits] using this + have hbits12 : 12 ≤ bits.val := by cases sz <;> simp [capability.FrameSize.bits] at hbits <;> subst hbits <;> decide + have hptr_al8 : ptr.val % 8 = 0 := by + have h8 : 8 ∣ 2 ^ bits.val := ⟨2 ^ (bits.val - 3), by rw [show (8 : Nat) = 2 ^ 3 from rfl, ← Nat.pow_add]; congr 1; omega⟩ + exact Nat.dvd_iff_mod_eq_zero.mp (Nat.dvd_trans h8 (Nat.dvd_iff_mod_eq_zero.mpr hptrAl)) + have hoff_al8 : offset.val % 8 = 0 := by + have h63 := congrArg UScalar.val hoff64_eq + change (offset.val &&& 63) = 0 at h63 + have h64 : offset.val % 64 = 0 := by + have := Nat.and_two_pow_sub_one_eq_mod offset.val 6; simpa using this.symm.trans h63 + omega + have hfr_val : frame.val = ptr.val + offset.val := by + rw [wa_valP]; exact Nat.mod_eq_of_lt (by omega) + have hfr_fit : frame.val + 1024 ≤ 2 ^ 64 := by omega + have hfr_al : Aligned frame := by show frame.val % 8 = 0; rw [hfr_val]; omega + have hWin : FrameWinA Ub frame := by + refine ⟨hfr_fit, fun k hk => ?_⟩ + have hw_val : (wa frame ⟨BitVec.ofNat 64 (8 * k)⟩).val = frame.val + 8 * k := by + rw [wa_valP] + have hk8 : (⟨BitVec.ofNat 64 (8 * k)⟩ : U64).val = 8 * k := by + change (BitVec.ofNat 64 (8 * k)).toNat = _; rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)] + rw [hk8]; exact Nat.mod_eq_of_lt (by omega) + have hwal : Aligned (wa frame ⟨BitVec.ofNat 64 (8 * k)⟩) := by + show (wa frame ⟨BitVec.ofNat 64 (8 * k)⟩).val % 8 = 0; rw [hw_val, hfr_val]; omega + exact W.fu s1 hs1_OS ptr sz rights mp ept asid va hc_s1 bits hbits _ hwal (by omega) (by omega) + have hfr_disj_tcb : ∀ t ∈ Z.T, ∀ k < 128, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → + wa frame ⟨BitVec.ofNat 64 (8 * k)⟩ ≠ wa t K := fun t ht k hk K _ hK2 e => by + have htH : H t = some .tcb := show t ∈ H.tcbs by rw [J3.tcbs]; exact ht + have hne : ptr ≠ t := fun h => by rw [h, htH] at hptrH; cases hptrH + have hd := J3.sys.ent.heap.disj ptr (.other bits.val) t .tcb hptrH htH hne + have hft := (hU.tcb_al t ht).fits + have hw1 : (wa frame ⟨BitVec.ofNat 64 (8 * k)⟩).val = frame.val + 8 * k := by + rw [wa_valP] + have hk8 : (⟨BitVec.ofNat 64 (8 * k)⟩ : U64).val = 8 * k := by + change (BitVec.ofNat 64 (8 * k)).toNat = _; rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)] + rw [hk8]; exact Nat.mod_eq_of_lt (by omega) + have hw2 : (wa t K).val = t.val + K.val := by rw [wa_valP]; exact Nat.mod_eq_of_lt (by omega) + have hev := congrArg UScalar.val e; rw [hw1, hw2] at hev + change ptr.val + 2 ^ bits.val ≤ t.val ∨ t.val + 4096 ≤ ptr.val at hd; omega + have hAg_m : mask ≠ 0#u64 → FrameInputAgreeA st s σ frame := fun hm => + hAgree s1 ptr sz rights mp ept asid va offset mask hs1_idx hs1_0 hc_s1 + (by simp [hargs_eq]) (by simp [hargs_eq, hrest_eq]) hm + -- Step A: read VGIC frame if `(mask &&& (8 ||| 32)) != 0` + have hvg_step : ∃ (vgic : U64 × Array U64 16#usize) (vgicSpec : Word × List Word), + (do let (s1, a1) ← vcpu.stall_vcpu st ar s a vc + let s2 ← sched.set_thread_state st s1 (L.curW s) sched.TS_RESTART + let s3 ← vcpu.write_state_from_frame st ar s2 a1 vc frame mask vgic + ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s3, a1)) = + ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s3, a3) ∧ + (mask.bv &&& (smVgic ||| smVgicLr) ≠ 0 → + vgic.1.bv = vgicSpec.1 ∧ vgicSpec.1 >>> 32 = 0 ∧ + vgicSpec.2.length = fl4NumLrs ∧ + (∀ j, j < fl4NumLrs → (vgic.2.val[j]!).bv = vgicSpec.2[j]!) ∧ + readVgicFrame (specPtr frame) σ = ({(some vgicSpec, σ)}, False)) ∧ + (mask.bv &&& (smVgic ||| smVgicLr) = 0 → vgicSpec = (0, [])) := by + by_cases hvg_m : (mask &&& (8#u64 ||| 32#u64) != 0#u64) = true + · have hvg_mb : mask.bv &&& (smVgic ||| smVgicLr) ≠ 0 := fun e => by + have : mask &&& (8#u64 ||| 32#u64) = 0#u64 := u64_eq_of_bv_eq2 e; simp [this] at hvg_m + have hmnz : mask ≠ 0#u64 := fun e => by subst e; exact hvg_mb (by decide) + rw [if_pos hvg_m] at hdec + have hread_vg := read_vgic_frame_localA (st := st) (Z := Z) hfr_fit hfr_al (hAg_m hmnz) + (fun k hk => by rcases W.col.kb.ksU _ (hWin.2 k hk) with h | h <;> (unfold ksSizeA; omega)) + unfold fl4GicV3 at hread_vg; rw [← hgv] at hread_vg + obtain ⟨res, hres_eq, hres_sp⟩ := spec_ex_fd hread_vg + rw [hres_eq, bind_ok] at hdec + cases res with + | none => simp [error.invalid_arg, bind_ok] at hdec + | some vg => + rcases vg with ⟨vgHcr, vgArr⟩ + obtain ⟨lrsList, hlrs_len, hlrs_arr, hread_spec⟩ := hres_sp + have hal3 : ∀ k < 128, bufAddr (specPtr frame) k &&& Fl4SpecA64.mask 3 = 0 := fun k hk => by + rw [← specPtr_wa_wordA (by omega)] + exact specPtr_al3 (aligned_wa hfr_al _ (by + change (BitVec.ofNat 64 (8 * k)).toNat % 8 = 0 + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega : 8 * k < 2 ^ 64)]; omega)) + have hvg_u32 : vgHcr.bv >>> 32 = 0 := by + rw [readVgicFrame_eqA _ _ hal3] at hread_spec + split_ifs at hread_spec with h_ok + · have heq := (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (Prod.mk.inj hread_spec).1)).1 + obtain ⟨hvg_eq, -⟩ := Prod.mk.inj (Option.some.inj heq) + rw [← hvg_eq] + have hlt : (sanitizeVgicHcr (loadWordAt σ.machineState.underlyingMemory (bufAddr (specPtr frame) vrVgicHcr)) fl4GicV3).toNat < 2 ^ 32 := by + unfold sanitizeVgicHcr + cases fl4GicV3 + · simp only [Bool.false_eq_true, ↓reduceIte, BitVec.toNat_and] + exact lt_of_le_of_lt Nat.and_le_right (by decide) + · simp only [↓reduceIte, BitVec.toNat_and] + exact lt_of_le_of_lt Nat.and_le_right (by decide) + apply BitVec.eq_of_toNat_eq + rw [BitVec.toNat_ushiftRight, Nat.shiftRight_eq_div_pow] + exact Nat.div_eq_of_lt hlt + · have heq := (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (Prod.mk.inj hread_spec).1)).1 + cases heq + exact ⟨(vgHcr, vgArr), (vgHcr.bv, lrsList), hdec, + fun _ => ⟨rfl, hvg_u32, hlrs_len, hlrs_arr, hread_spec⟩, + fun h0 => absurd h0 hvg_mb⟩ + · have hvg_m0 : mask.bv &&& (smVgic ||| smVgicLr) = 0 := by + have h0 : mask &&& (8#u64 ||| 32#u64) = 0#u64 := by + by_contra hne; exact hvg_m (bne_iff_ne.2 hne) + exact congrArg UScalar.bv h0 + rw [if_neg hvg_m] at hdec + exact ⟨(0#u64, Array.repeat 16#usize 0#u64), (0, []), hdec, + fun hne => absurd hvg_m0 hne, + fun _ => rfl⟩ + obtain ⟨vgic, vgicSpec, hdec_vg, hvg_pos, hvg_zero⟩ := hvg_step + -- Step B: `stall_vcpu_local` + `set_thread_state TS_RESTART` + obtain ⟨⟨s1', a1⟩, hst_eq, hs1'_eq, hAR1⟩ := spec_ex_fd (stall_vcpu_localA hAK J3.j.1.ar vc C.hStallT C.hStallC) + dsimp only at hs1'_eq hAR1; rw [hs1'_eq] at hst_eq + rw [hst_eq, bind_ok] at hdec_vg + have hvc_st : ∀ v ∈ Z.V, stA (L.curW s) ≠ v ∧ stA (L.curW s) ≠ wa v 24#u64 := fun v hv => + ⟨by rw [stA_eq_wa]; have := hpl.disj_tcb v hv _ J3.sr.curT 0#u64 3328#u64 (by simp) (by simp) + rw [wa_zero2] at this; exact this.symm, + by rw [stA_eq_wa]; exact (hpl.disj_tcb v hv _ J3.sr.curT 24#u64 3328#u64 (by simp) (by simp)).symm⟩ + obtain ⟨_, _, _, _, hpl_rs⟩ := RS.pl + obtain ⟨x, hkx, hst_ok, hsts_spec, J1, -, osk1, -⟩ := + J3.of_restartCurA hpl_rs J3.symL RS.run hnvm RS.cur RS.vs hvc_st + change Std.bind (sched.set_thread_state st s (L.curW s) sched.TS_RESTART) (fun s2 => + Std.bind (vcpu.write_state_from_frame st ar s2 a1 vc frame mask vgic) (fun s3 => + ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s3, a1))) = _ at hdec_vg + rw [hst_ok, bind_ok] at hdec_vg + set s2 := L.wr s (stA (L.curW s)) sched.TS_RESTART + set σ1 := stUpdA σ σ.curThread x .restart + have J1a : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s2 a1 σ1 m := + { J1 with j := ⟨{ J1.j.1 with ar := hAR1 }, J1.j.2⟩ } + have hSAct1 : SActA σ1 := + ⟨fun u hu => J.sact.f1 u hu, + ⟨{ x with tcbState := .restart }, by unfold σ1 stUpdA setObjA; simp [Function.update], Or.inr (Or.inl rfl)⟩⟩ + have hNoSw1 : ∀ u, σ1.schedulerAction ≠ .switchThread u := J.noSwitch + have hAg_s2 : mask ≠ 0#u64 → FrameInputAgreeA st s2 σ1 frame := fun hm k hk => by + have hst_al : Aligned (stA (L.curW s)) := by + rw [stA_eq_wa]; exact aligned_wa (hpl.al_t _ J3.sr.curT) 3328#u64 (by decide) + have hwal : Aligned (wa frame ⟨BitVec.ofNat 64 (8 * k)⟩) := aligned_wa hfr_al _ (by + change (BitVec.ofNat 64 (8 * k)).toNat % 8 = 0 + have h8k_lt : 8 * k < 2 ^ 64 := by omega + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt h8k_lt]; omega) + have hne : stA (L.curW s) ≠ wa frame ⟨BitVec.ofNat 64 (8 * k)⟩ := fun e => by + rw [stA_eq_wa] at e + have htH : H (L.curW s) = some .tcb := show L.curW s ∈ H.tcbs by rw [J3.tcbs]; exact J3.sr.curT + have hne_pt : ptr ≠ L.curW s := fun h => by rw [h, htH] at hptrH; cases hptrH + have hd := J3.sys.ent.heap.disj ptr (.other bits.val) (L.curW s) .tcb hptrH htH hne_pt + have hft := (hU.tcb_al _ J3.sr.curT).fits + have hw1 : (wa frame ⟨BitVec.ofNat 64 (8 * k)⟩).val = frame.val + 8 * k := by + rw [wa_valP] + have hk8 : (⟨BitVec.ofNat 64 (8 * k)⟩ : U64).val = 8 * k := by + change (BitVec.ofNat 64 (8 * k)).toNat = _; rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)] + rw [hk8]; exact Nat.mod_eq_of_lt (by omega) + have hw2 : (wa (L.curW s) 3328#u64).val = (L.curW s).val + 3328 := by + have h3328 : (3328#u64 : U64).val = 3328 := rfl + rw [wa_valP, h3328]; exact Nat.mod_eq_of_lt (by omega) + have hev := congrArg UScalar.val e; rw [hw1, hw2] at hev + change ptr.val + 2 ^ bits.val ≤ (L.curW s).val ∨ (L.curW s).val + 4096 ≤ ptr.val at hd; omega + rw [mem_wr_neA hst_al hwal hne]; exact hAg_m hm k hk + -- Step C: `vcpu_write_state_from_frame_stage3TA` + have hwrite := vcpu_write_state_from_frame_stage3TA + (J1a.toTA hSAct1 hNoSw1) J1a.schedValid J1a.vcpuRec6A + hP (by rw [show L.ksb s2 = L.ksb s from L.ksb_wr _ _ _]; exact hpl) + (by rw [show L.ksb s2 = L.ksb s from L.ksb_wr _ _ _]; exact hU) + hCs128 hCsN hvc hWin hfr_al hAg_s2 hfr_disj_tcb + (vgic := vgic) (vgicSpec := vgicSpec) + (fun hm => by obtain ⟨h1, h2, h3, h4, -⟩ := hvg_pos hm; exact ⟨h1, h2, h3, h4⟩) + obtain ⟨s4, hwr_eq, σ3, hsp3, J3T, hSV3, R6_3, hwf3⟩ := spec_ex_fd hwrite + rw [hwr_eq, bind_ok] at hdec_vg + obtain ⟨-, rfl, rfl⟩ := Prod.mk.inj (okinj2 hdec_vg) + have hw_no3 : WritesIn st s2 s3 (VcpuWrFootA st true Z OS s2) := + ⟨hwf3.1, hwf3.2.1, fun q hqa hnf => hwf3.2.2 q hqa (fun h => hnf (by + rcases h with rfl | ⟨i, hi, rfl⟩ | ⟨hnz, K, hK8, hK1, hK2, rfl⟩ + · exact Or.inl ⟨vc, hvc, 16#u64, by decide, by decide, rfl⟩ + · exact Or.inl ⟨vc, hvc, vcpuRegOffA i, by rw [vcpuRegOffA_val hi]; omega, by rw [vcpuRegOffA_val hi]; omega, rfl⟩ + · exact Or.inr (Or.inl ⟨L.mem s2 vc, (J1a.vcRun.vtcb vc hvc).resolve_left hnz, K, hK8, Or.inl ⟨hK1, hK2⟩, rfl⟩)))⟩ + have osk3 : OSKeep st OS s2 s3 := osKeep_of_vcpuWrA (J1a.toTA hSAct1 hNoSw1) J1a.vcpuRec6A.vcOS J1a.vcpuRec6A.frOff hw_no3 + obtain ⟨-, _, sysl3, symL3, -, -, -⟩ := + J.decodeFinA hA hM hPl hfm hslot hcov hcap hext hSC + (by simp [DecArmS1aEnt, DecArmQH]) (fun _ h => by cases h) (fun _ h => by cases h) + hdec0 σ3 m + have J3_fin : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s3 a3 σ3 m := by + have hZ_eq : ({ Z with hw := Z.hw } : XSetsA) = Z := by cases Z; rfl + have h := J3T.toJA_vcpuA J1a hSV3 R6_3 osk3 sysl3 symL3 + rwa [hZ_eq] at h + -- Spec decode pin for `ARMVCPUWriteState` + have heptF : ept = false := hNoS2 s1 ptr sz rights false mp ept asid va hs1_idx hc_s1 + subst heptF + have hc0_eq : c0 = .archObjectCap (.frameCap (specPtr ptr) (vmRightsOfA rights) (frameSizeOfA sz) false (mapDataA mp asid va)) := by + have h1 := hcapRel0.2.symm + simpa [absCapA, absFrameE] using h1 + have hframe_eq : stateWindow c0 offset.bv = some (some (specPtr frame)) := by + rw [hc0_eq]; unfold stateWindow; dsimp only + have h63 : (offset.bv &&& 63) = 0 := congrArg UScalar.bv hoff64_eq + have hsp_add : specPtr frame = specPtr ptr + offset.bv := by + unfold frame specPtr; change (ptr.bv + offset.bv) + pptrShiftA = (ptr.bv + pptrShiftA) + offset.bv + rw [BitVec.add_assoc, BitVec.add_comm offset.bv pptrShiftA, ← BitVec.add_assoc] + have hsz_bv : ((1 : Word) <<< pageBitsForSize (frameSizeOfA sz)) = size1.bv := by + rw [← hbits_sp] + apply BitVec.eq_of_toNat_eq + rw [BitVec.toNat_shiftLeft, show (1 : Word).toNat = 1 from rfl, Nat.one_shiftLeft, + Nat.mod_eq_of_lt (Nat.pow_lt_pow_right (by omega) hbits_lt), ← hsize1_val]; rfl + rw [hsz_bv] + have hcond : ¬ (offset.bv &&& 63 ≠ 0 ∨ offset.bv ≥ size1.bv ∨ size1.bv - offset.bv < smSize) := by + rintro (h | h | h) + · exact h h63 + · exact hoff_ge h + · have hsub_toNat : (size1.bv - offset.bv).toNat = 2 ^ bits.val - offset.val := by + have hle : offset.bv.toNat ≤ size1.bv.toNat := by show offset.val ≤ size1.val; omega + rw [BitVec.toNat_sub_of_le hle]; show size1.val - offset.val = _; omega + have hlt : (size1.bv - offset.bv).toNat < (smSize : Word).toNat := h + have hsm : (smSize : Word).toNat = 1024 := by unfold smSize; decide + omega + rw [if_neg hcond, ← hsp_add] + have hfrd_eq : frameRightsDev c0 = some (vmRightsOfA rights, false) := by rw [hc0_eq]; rfl + have hne_ko : vmRightsOfA rights ≠ vmKernelOnly := by + obtain ⟨hr1, hr3, -, -⟩ : 1 ≤ rights.val ∧ rights.val ≤ 3 ∧ _ ∧ _ := hcapRel0.1 + have hrne1 : rights.val ≠ 1 := fun e => hko (UScalar.eq_of_val_eq (by unfold capability.VM_KERNEL_ONLY; exact e)) + intro h + have hmem : Fl4SpecA64.Rights.allowRead ∈ vmRightsOfA rights := by + unfold vmRightsOfA + by_cases h3 : rights.val = 3 + · rw [ite_eq_left h3]; simp [vmReadWrite] + · have h2 : rights.val = 2 := by omega + rw [ite_eq_right h3, ite_eq_left h2]; simp [vmReadOnly] + rw [h] at hmem; simp [vmKernelOnly] at hmem + have hargs2 : args = offset :: mask :: rest2 := by rw [hargs_eq, hrest_eq] + have hfp2 : VcpuFrontPinA Z.th σ slot mi sl (.archObjectCap (.vcpuCap (specPtr vc))) excaps buffer + ((offset :: mask :: rest2).map UScalar.bv) := hargs2 ▸ hfp + have hpin : VcpuDecPinA call Z.th σ slot + (.invokeArchObject (.vcpu (.stateTransfer (specPtr vc) (specPtr frame) mask.bv false vgicSpec))) := by + refine ⟨mi, sl, .archObjectCap (.vcpuCap (specPtr vc)), excaps, buffer, + (offset :: mask :: rest2).map UScalar.bv, hfp2, fun cptr => fl4DecodeInvocation_vcpu_of_archA ?_⟩ + rw [hlbl]; unfold fl4ArchDecodeInvocation + simp only [↓reduceIte, decodeVcpuStateTransfer, NonDetE.whenE, the] + have hex_ne : excaps ≠ [] := fun h => by rw [h] at hex0_eq; cases hex0_eq + simp only [List.map_cons, hex_ne, or_false, + List.getElem?_cons_zero, List.getElem?_cons_succ, hmask_ok, ne_eq, not_true_eq_false] + rw [if_neg (by simp), if_neg (by simp), hex0_eq] + dsimp only; rw [hfrd_eq, hframe_eq] + simp only [Bool.false_eq_true, or_false, false_and, hne_ko, not_false_eq_true, ↓reduceIte, + NonDetE.liftME, NonDetE.returnOk] + have hpure_b : ∀ {α β : Type} (x : α) (f : α → SEMonad Unit β) (τ : State Unit), + (pure x >>= f).run τ = (f x).run τ := fun x f τ => by + change ((NonDet.pure (Except.ok x) >>= ExceptT.bindCont f) τ) = (f x).run τ + rw [nd_bind_oneSc (m := NonDet.pure (Except.ok x)) rfl]; rfl + have hlift_b : ∀ {α : Type} (m : SEMonad Unit α) (f : α → SEMonad Unit Fl4ArchInvocation) + (v : α) (τ : State Unit), + m.run τ = ({(.ok v, τ)}, False) → (m >>= f).run τ = (f v).run τ := fun m f v τ hm => by + change ((m.run >>= ExceptT.bindCont f) τ) = (f v).run τ + rw [nd_bind_oneSc hm]; rfl + refine (hlift_b _ _ (Fl4VcpuInvocation.stateTransfer (specPtr vc) (specPtr frame) mask.bv false vgicSpec) σ ?_).trans rfl + rw [hpure_b, hpure_b, hpure_b] + by_cases hvg_mb : mask.bv &&& (smVgic ||| smVgicLr) ≠ 0 + · rw [ite_eq_left ⟨by decide, hvg_mb⟩] + have hread_s := (hvg_pos hvg_mb).2.2.2.2 + rw [ndE_liftE_bind _ _ _ _ (some vgicSpec) hread_s]; rfl + · push_neg at hvg_mb + rw [ite_eq_right (fun h => h.2 hvg_mb), hvg_zero hvg_mb]; rfl + have hperf : (.ok [], σ3) ∈ ((fl4ArchPerformInvocation (Z := Unit) + (.vcpu (.stateTransfer (specPtr vc) (specPtr frame) mask.bv false vgicSpec))).run σ1).1 := by + unfold fl4ArchPerformInvocation fl4PerformVcpuInv + simp only [Bool.false_eq_true, ↓reduceIte, NonDetE.liftE] + have hfl : fl4VcpuFlush (Z := Unit) (specPtr vc) σ1 = ({((), σ1)}, False) := by + unfold fl4VcpuFlush NonDet.when + rw [nd_bind_oneSc (m := NonDet.gets _) rfl, J1a.curVcpuNone]; simp; rfl + change (Except.ok [], σ3) ∈ (((fl4VcpuFlush (specPtr vc) >>= + fun _ => fl4WriteStateFromFrame (specPtr vc) (specPtr frame) mask.bv vgicSpec >>= + fun _ => pure ([] : List Data)) >>= fun r => pure (Except.ok r)) σ1).1 + rw [nd_bind_oneSc (by rw [nd_bind_oneSc hfl, nd_bind_oneSc hsp3]; rfl)] + exact Set.mem_singleton _ + exact ⟨x, frame, mask, vgicSpec, σ3, hkx, hpin, hsts_spec, hperf, J3_fin⟩ + +end arms2 + +end +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3T3ArmsVcpuA.lean b/proofs/Fl4ProofsA64/P3/Stage3T3ArmsVcpuA.lean new file mode 100644 index 000000000..ca13a7d4f --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3T3ArmsVcpuA.lean @@ -0,0 +1,2129 @@ +/- +P3 (aarch64, A64-P3 S4 VCPU, Phase 1): **the 4 VCPU reg/IRQ invocation arms at a syscall entry** +(`JSyscallA → JStage3A`), with Plan B `hpin`-compatible `VcpuFrontPinA` / `VcpuDecPinA` pinning: + +* **`JSyscallA.vcpuAckVppiA`** (label 64 `ARMVCPUAckVPPI`) +* **`JSyscallA.vcpuReadRegA`** (label 62 `ARMVCPUReadReg`) +* **`JSyscallA.vcpuWriteRegA`** (label 63 `ARMVCPUWriteReg`) +* **`JSyscallA.vcpuInjectIrqA`** (label 61 `ARMVCPUInjectIRQ`) + +Shared lifters: +* `vcpu_reg_sync_wrA`: `VcpuStateSnapA` + non-link `VcPage` `WritesIn` across `vcpu.reg_set` + + optional `vcpu.sync_vgic_status`. +* `JStage3A.of_reply1A`: lifts `vspace.reply1` (used by `ReadReg`) from `J1` (post-`of_restartCurA`) + + `decodeFinA` to `JStage3A` via `JStage3A.of_schedWrN`. + +Named premises: `[P]` `ArchTotal`, `MaxSpi`, `ArchPlat`, `A64FrameMasks`, `VcpuArchKeepsA`, +`VcpuPlatAgreeA` (ReadReg/WriteReg/InjectIRQ); `[S]` `numNodes = 1` (stall); `#8b-C` `Vcpu8bCA` +(3 fields). New module. +-/ +import Fl4ProofsA64.P3.Stage3VcpuBridgeA +import Fl4ProofsA64.P3.Stage3IpcGlueA +import Fl4ProofsA64.P3.Stage3TcbF0A +import Fl4ProofsA64.P3.EpXferA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp + Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Vcpu Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section arms +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} + {s s3 : S} {a a3 : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-! ### Small private helpers -/ + +private theorem lift_okA {α : Type} (x : α) : (lift x : Result α) = ok x := rfl + +private theorem u64_sub_okA {x y : U64} (hle : y.val ≤ x.val) : + x - y = ok ⟨BitVec.ofNat 64 (x.val - y.val)⟩ := by + obtain ⟨r, hr, hval⟩ := spec_ex_fd (UScalar.sub_spec (x := x) (y := y) (by omega)) + rw [hr]; congr 1; apply UScalar.eq_of_val_eq + rw [hval.1] + change _ = (BitVec.ofNat 64 (x.val - y.val)).toNat + rw [BitVec.toNat_ofNat] + have hx := x.hBounds + simp only [U64.size_def, U64.numBits, UScalar.size, UScalarTy.numBits] at hx + exact (Nat.mod_eq_of_lt (by omega)).symm + +private theorem u64_shl_okA (x : U64) (y : I32) (hy0 : 0 ≤ y.val) (hy64 : y.val < 64) : + (x <<< y) = ok ⟨x.bv <<< y.val.toNat⟩ := by + obtain ⟨r, hr, hbv⟩ := spec_ex_fd (U64.ShiftLeft_IScalar_spec (x := x) (y := y) hy0 hy64) + rw [hr]; exact congrArg ok (u64_eq_of_bv_eq hbv.2) + +private theorem u64_shr_okA (x : U64) (y : I32) (hy0 : 0 ≤ y.val) (hy64 : y.val < 64) : + (x >>> y) = ok ⟨x.bv >>> y.val.toNat⟩ := by + obtain ⟨r, hr, hbv⟩ := spec_ex_fd (U64.ShiftRight_IScalar_spec (x := x) (y := y) hy0 hy64) + rw [hr]; exact congrArg ok (u64_eq_of_bv_eq hbv.2) + +private theorem ndE_whenE_falseA {β : Type} {P : Prop} [Decidable P] (h : ¬ P) + (e : SEMonad Unit Unit) (f : Unit → SEMonad Unit β) (σ : State Unit) : + (NonDetE.whenE P e >>= f).run σ = (f ()).run σ := by + unfold NonDetE.whenE; rw [if_neg h] + exact nd_bind_oneSc (f := ExceptT.bindCont f) + (show (pure () : SEMonad Unit Unit).run σ = ({(Except.ok (), σ)}, False) from rfl) + +private theorem ndE_unlessE_trueA {β : Type} {P : Prop} [Decidable P] (h : P) + (e : SEMonad Unit Unit) (f : Unit → SEMonad Unit β) (σ : State Unit) : + (NonDetE.unlessE P e >>= f).run σ = (f ()).run σ := by + unfold NonDetE.unlessE; rw [if_pos h] + exact nd_bind_oneSc (f := ExceptT.bindCont f) + (show (pure () : SEMonad Unit Unit).run σ = ({(Except.ok (), σ)}, False) from rfl) + +private theorem ndE_liftE_bindA {α β : Type} (m : SMonad Unit α) (f : α → SEMonad Unit β) + (σ : State Unit) (x : α) (hm : m σ = ({(x, σ)}, False)) : + (NonDetE.liftE m >>= f).run σ = (f x).run σ := by + have h1 : (NonDetE.liftE m : SEMonad Unit α).run σ = ({(Except.ok x, σ)}, False) := by + unfold NonDetE.liftE ExceptT.lift ExceptT.mk + exact nd_bind_oneSc (f := fun a => NonDet.pure (Except.ok a)) hm + exact nd_bind_oneSc (f := ExceptT.bindCont f) h1 + +private theorem vcpuRelA_toSnapA {Ub : Set U64} {s' : S} {σ' : State Unit} + (hV : VcpuRelA (L := L) Z OS Ub s' σ') : VcpuStateSnapA Z.V (L.mem s') σ' := + ⟨fun v hv x hkx => ⟨hV.regs v hv x hkx, hV.vppi v hv x hkx, hV.runOff v hv x hkx, (hV.runOut v hv x hkx).1, hV.owner v hv⟩, + fun v hv x hkx => (hV.runOut v hv x hkx).2⟩ + +private theorem vcPage_nonLink_of_regA {Ub : Set U64} {ksb : U64} + (hP : VcPlacedA Z OS Ub ksb) {vc : U64} (hvc : vc ∈ Z.V) {i : Nat} (hi : i < 64) : + ∀ b, b = vcpuRegWA vc i → VcPage Z.V b ∧ ∀ u ∈ Z.V, b ≠ u ∧ b ≠ wa u 24#u64 := by + rintro _ rfl + refine ⟨vcpuRegWA_in_vcPage hvc hi, fun u hu => ?_⟩ + have hwa0 : wa u 0#u64 = u := by + apply UScalar.eq_of_val_eq; rw [wa_valP]; simp [UScalar.hBounds] + have hoff_lt : (⟨BitVec.ofNat 64 (64 + 8 * i)⟩ : U64).val < 4096 := by + change (BitVec.ofNat 64 (64 + 8 * i)).toNat < 4096 + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)]; omega + have hoff_ne0 : (⟨BitVec.ofNat 64 (64 + 8 * i)⟩ : U64) ≠ 0#u64 := fun h => by + have hv := congrArg UScalar.val h + change (BitVec.ofNat 64 (64 + 8 * i)).toNat = 0 at hv + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)] at hv; omega + have hoff_ne24 : (⟨BitVec.ofNat 64 (64 + 8 * i)⟩ : U64) ≠ 24#u64 := fun h => by + have hv := congrArg UScalar.val h + change (BitVec.ofNat 64 (64 + 8 * i)).toNat = 24 at hv + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)] at hv; omega + by_cases heq : u = vc + · subst heq + have h1 := hP.same_vcpu_ne (o₁ := ⟨BitVec.ofNat 64 (64 + 8 * i)⟩) (o₂ := 0#u64) hu hoff_lt (by decide) hoff_ne0 + rw [hwa0] at h1 + exact ⟨h1, hP.same_vcpu_ne (o₁ := ⟨BitVec.ofNat 64 (64 + 8 * i)⟩) (o₂ := 24#u64) hu hoff_lt (by decide) hoff_ne24⟩ + · have h1 := hP.disj_vcpu (ov₁ := ⟨BitVec.ofNat 64 (64 + 8 * i)⟩) (ov₂ := 0#u64) hvc hu (Ne.symm heq) hoff_lt (by decide) + rw [hwa0] at h1 + exact ⟨h1, hP.disj_vcpu (ov₁ := ⟨BitVec.ofNat 64 (64 + 8 * i)⟩) (ov₂ := 24#u64) hvc hu (Ne.symm heq) hoff_lt (by decide)⟩ + +private theorem vcpu_reg_sync_wrA {Ub : Set U64} {s2 : S} {σ1 : State Unit} {vc : U64} {v : Fl4SpecA64.Vcpu} + (hV2 : VcpuRelA (L := L) Z OS Ub s2 σ1) + (hksFit2 : (L.ksb s2).val + ksSizeA ≤ 2 ^ 64) + (hCs128 : ∀ c ∈ Z.Cs, c.val < 128) + (hIdleT2 : ∀ c ∈ Z.Cs, L.mem s2 (L.nodeAt s2 c layout.node.IDLE_THREAD) ∈ Z.T) + (hvc : vc ∈ Z.V) (hkv1 : σ1.kheap (specPtr vc) = some (.archObj (.vcpu v))) + (reg : U64) (hreg : reg.val < 64) (val : U64) + (hhcr32 : reg.val = vrVgicHcr → ucast 64 (ucast 32 val.bv) = val.bv) (doSync : Bool) : + (do + let s3 ← vcpu.reg_set st s2 vc reg val + if doSync then vcpu.sync_vgic_status st s3 vc ⟨BitVec.ofNat 64 fl4NumLrs⟩ fl4GicV3 + else ok s3) ⦃ s' => + let v1 := fl4VcpuRegSet reg.val val.bv v + let v2 := if doSync then syncVgicStatus v1 fl4NumLrs fl4GicV3 else v1 + let σ2 := updKSc σ1 (specPtr vc) (.archObj (.vcpu v2)) + VcpuStateSnapA Z.V (L.mem s') σ2 ∧ + WritesIn st s2 s' (fun b => VcPage Z.V b ∧ ∀ u ∈ Z.V, b ≠ u ∧ b ≠ wa u 24#u64) ∧ + v2.vcpuTcb = v.vcpuTcb ∧ v2.vcpuExt.runCaller = v.vcpuExt.runCaller ⦄ := by + have hreg_eq : reg = ⟨BitVec.ofNat 64 reg.val⟩ := by + apply UScalar.eq_of_val_eq + change reg.val = (BitVec.ofNat 64 reg.val).toNat + rw [BitVec.toNat_ofNat]; exact (Nat.mod_eq_of_lt (by omega)).symm + have hset : vcpu.reg_set st s2 vc reg val = ok (L.wr s2 (vcpuRegWA vc reg.val) val) := by + conv_lhs => rw [hreg_eq] + exact vcpu_reg_set_eqA s2 vc hreg val + rw [hset, bind_ok] + set s3 := L.wr s2 (vcpuRegWA vc reg.val) val + have hreg_al : Aligned (vcpuRegWA vc reg.val) := hV2.place.vcPage_al (vcpuRegWA_in_vcPage hvc hreg) + have hV3 := hV2.upd_regA hksFit2 hCs128 hIdleT2 hvc hkv1 hreg val hhcr32 + have hmeta1 := fl4VcpuRegSet_pres_metaA reg.val val.bv v + have hw3 : WritesIn st s2 s3 (fun b => b = vcpuRegWA vc reg.val) := writesIn_wr hreg_al + cases doSync with + | false => + simp only [Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + exact ⟨vcpuRelA_toSnapA hV3, hw3.mono (vcPage_nonLink_of_regA hV2.place hvc hreg), hmeta1.1, hmeta1.2.2.1⟩ + | true => + simp only [↓reduceIte] + have hksFit3 : (L.ksb s3).val + ksSizeA ≤ 2 ^ 64 := by rw [L.ksb_wr]; exact hksFit2 + have hIdleT3 : ∀ c ∈ Z.Cs, L.mem s3 (L.nodeAt s3 c layout.node.IDLE_THREAD) ∈ Z.T := fun c hc => by + have hni : L.nodeAt s3 c layout.node.IDLE_THREAD = L.nodeAt s2 c layout.node.IDLE_THREAD := by + unfold LawfulStore.nodeAt; rw [L.ksb_wr] + have hal_ni := hV2.place.node_al c layout.node.IDLE_THREAD (by unfold layout.node.IDLE_THREAD; decide) + have hne_ni : vcpuRegWA vc reg.val ≠ L.nodeAt s2 c layout.node.IDLE_THREAD := + fun heq => hV2.place.disj_node hksFit2 (vcpuRegWA_in_vcPage hvc hreg) (hCs128 c hc) + (by unfold layout.node.IDLE_THREAD; decide) heq + rw [hni, mem_wr_neA hreg_al hal_ni hne_ni]; exact hIdleT2 c hc + refine WP.spec_mono (sync_vgic_status_refinesA hV3 hksFit3 hCs128 hIdleT3 hvc (updKSc_same σ1 _ _)) ?_ + rintro s4 ⟨hV4, hwr4⟩ + have hmeta2 := syncVgicStatus_pres_metaA (fl4VcpuRegSet reg.val val.bv v) fl4NumLrs fl4GicV3 + have hsnap4 : VcpuStateSnapA Z.V (L.mem s4) + (updKSc σ1 (specPtr vc) (.archObj (.vcpu (syncVgicStatus (fl4VcpuRegSet reg.val val.bv v) fl4NumLrs fl4GicV3)))) := by + have hid : updKSc (updKSc σ1 (specPtr vc) (.archObj (.vcpu (fl4VcpuRegSet reg.val val.bv v)))) + (specPtr vc) (.archObj (.vcpu (syncVgicStatus (fl4VcpuRegSet reg.val val.bv v) fl4NumLrs fl4GicV3))) = + updKSc σ1 (specPtr vc) (.archObj (.vcpu (syncVgicStatus (fl4VcpuRegSet reg.val val.bv v) fl4NumLrs fl4GicV3))) := by + unfold updKSc; simp [Function.update_idem] + rw [← hid]; exact vcpuRelA_toSnapA hV4 + refine ⟨hsnap4, (hw3.trans hwr4).mono ?_, hmeta2.1.trans hmeta1.1, hmeta2.2.2.1.trans hmeta1.2.2.1⟩ + intro b hb + rcases hb with rfl | (rfl | rfl | rfl) + · exact vcPage_nonLink_of_regA hV2.place hvc hreg _ rfl + · exact vcPage_nonLink_of_regA hV2.place hvc (by unfold vrVgicElrsr; omega) _ rfl + · exact vcPage_nonLink_of_regA hV2.place hvc (by unfold vrVgicEisr; omega) _ rfl + · exact vcPage_nonLink_of_regA hV2.place hvc (by unfold vrVgicMisr; omega) _ rfl + +private theorem sanitizeVgicHcr_u32A (v : Fl4SpecA64.Word) (gicV3 : Bool) : + ucast 64 (ucast 32 (sanitizeVgicHcr v gicV3)) = sanitizeVgicHcr v gicV3 := by + have hlt : (sanitizeVgicHcr v gicV3).toNat < 2 ^ 32 := by + unfold sanitizeVgicHcr + cases gicV3 + · simp only [Bool.false_eq_true, ↓reduceIte, BitVec.toNat_and] + exact Nat.lt_of_le_of_lt Nat.and_le_right (by decide) + · simp only [↓reduceIte, BitVec.toNat_and] + exact Nat.lt_of_le_of_lt Nat.and_le_right (by decide) + apply BitVec.eq_of_toNat_eq + simp [ucast]; omega + +private theorem vcpu_inject_lr_eqA (virq priority group : U64) (g : U64) + (hgb : (decide (g = 3#u64)) = fl4GicV3) : + (if g = 3#u64 then do + let i4 ← 1#u64 <<< 62#i32 + let i5 ← lift (group &&& 1#u64) + let i6 ← i5 <<< 60#i32 + let i7 ← lift (i4 ||| i6) + let i8 ← lift (priority &&& 255#u64) + let i9 ← i8 <<< 48#i32 + let i10 ← lift (i7 ||| i9) + let i11 ← 1#u64 <<< 41#i32 + let i12 ← lift (i10 ||| i11) + ok (i12 ||| virq) + else do + let i4 ← lift (group &&& 1#u64) + let i5 ← i4 <<< 30#i32 + let i6 ← 1#u64 <<< 28#i32 + let i7 ← lift (i5 ||| i6) + let i8 ← lift (priority &&& 31#u64) + let i9 ← i8 <<< 23#i32 + let i10 ← lift (i7 ||| i9) + let i11 ← 1#u64 <<< 19#i32 + let i12 ← lift (i10 ||| i11) + let i13 ← lift (virq &&& 1023#u64) + ok (i12 ||| i13)) = ok ⟨fl4InjectLr virq.bv priority.bv group.bv fl4GicV3⟩ := by + unfold fl4InjectLr + by_cases hg3 : g = 3#u64 + · have hfl : fl4GicV3 = true := by rw [← hgb, decide_eq_true hg3] + rw [if_pos hg3, if_pos hfl] + simp only [lift_okA, bind_ok, + u64_shl_okA 1#u64 62#i32 (by decide) (by decide), + u64_shl_okA (group &&& 1#u64) 60#i32 (by decide) (by decide), + u64_shl_okA (priority &&& 255#u64) 48#i32 (by decide) (by decide), + u64_shl_okA 1#u64 41#i32 (by decide) (by decide)] + exact congrArg ok (u64_eq_of_bv_eq rfl) + · have hfl : fl4GicV3 = false := by rw [← hgb, decide_eq_false hg3] + rw [if_neg hg3, if_neg (by rw [hfl]; decide)] + simp only [lift_okA, bind_ok, + u64_shl_okA (group &&& 1#u64) 30#i32 (by decide) (by decide), + u64_shl_okA 1#u64 28#i32 (by decide) (by decide), + u64_shl_okA (priority &&& 31#u64) 23#i32 (by decide) (by decide), + u64_shl_okA 1#u64 19#i32 (by decide) (by decide)] + exact congrArg ok (u64_eq_of_bv_eq rfl) + +private theorem bind_ok_pairA {α β γ : Type} (x : α) (y : β) (f : α → β → Result γ) : + (do let (a, b) ← ok (x, y); f a b) = f x y := by + rw [bind_ok]; rfl + +/-! ### `JStage3A.of_reply1A`: record-level lifter for `vspace.reply1` (`ReadReg`) -/ + +private theorem reply1_regs_eqA {s1 : S} {t v : U64} (hta : TcbAddr t) (call : Bool) : + (if call then do + let buf ← msg.lookup_ipc_buffer st s1 true t + let s2 ← obj.Tcb.set_reg st t s1 layout.reg.BADGE 0#u64 + let (n, s3) ← msg.set_mr st s2 t buf 0#u64 v + let mi ← msginfo.MessageInfo.new 0#u64 0#u64 0#u64 n + msg.set_msg_info st s3 t mi + else ok s1) ⦃ s2 => + WritesIn st s1 s2 (fun b => ∃ K : U64, 256 ≤ K.val ∧ K.val < 3328 ∧ b = wa t K) ∧ + (call = true → + L.mem s2 (Endp.regA t layout.reg.BADGE) = 0#u64 ∧ + L.mem s2 (Endp.regA t layout.reg.X2) = v ∧ + (L.mem s2 (Endp.regA t layout.reg.MSG_INFO)).bv = Fl4SpecA64.messageInfoToData ⟨Fl4SpecA64.natToLen 1, 0, 0, 0⟩ ∧ + ∀ r : U64, r.val < 37 → r ≠ layout.reg.BADGE → r ≠ layout.reg.X2 → r ≠ layout.reg.MSG_INFO → + L.mem s2 (Endp.regA t r) = L.mem s1 (Endp.regA t r)) ∧ + (call = false → s2 = s1) ⦄ := by + cases call with + | false => + simp only [Bool.false_eq_true, ↓reduceIte] + rw [WP.spec_ok] + exact ⟨WritesIn.refl s1 _, fun h => absurd h (by decide), fun _ => rfl⟩ + | true => + have hreg : ∀ (s0 : S) (r val : U64), r.val < 37 → + obj.Tcb.set_reg st t s0 r val ⦃ s' => + s' = L.wr s0 (Endp.regA t r) val ∧ + WritesIn st s0 s' (fun b => ∃ K : U64, 256 ≤ K.val ∧ K.val < 3328 ∧ b = wa t K) ⦄ := fun s0 r val hr => by + rw [set_reg_eqA, WP.spec_ok] + refine ⟨rfl, ?_⟩ + have hK := Endp.regA_val hta hr + have hal := hta.align; have hfit := hta.fits + have hA : Aligned (Endp.regA t r) := by unfold Aligned; omega + refine (writesIn_wr hA).mono fun b hb => ?_ + subst hb + refine ⟨⟨BitVec.ofNat 64 (256 + 8 * r.val)⟩, ?_, ?_, ?_⟩ + · change 256 ≤ (BitVec.ofNat 64 (256 + 8 * r.val)).toNat + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)]; omega + · change (BitVec.ofNat 64 (256 + 8 * r.val)).toNat < 3328 + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)]; omega + · apply UScalar.eq_of_val_eq + rw [wa_valP, hK] + change _ = (t.val + (BitVec.ofNat 64 (256 + 8 * r.val)).toNat) % 2 ^ 64 + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega), Nat.mod_eq_of_lt (by omega)] + omega + have htr : ∀ {s0 s' s'' : S}, + WritesIn st s0 s' (fun b => ∃ K : U64, 256 ≤ K.val ∧ K.val < 3328 ∧ b = wa t K) → + WritesIn st s' s'' (fun b => ∃ K : U64, 256 ≤ K.val ∧ K.val < 3328 ∧ b = wa t K) → + WritesIn st s0 s'' (fun b => ∃ K : U64, 256 ≤ K.val ∧ K.val < 3328 ∧ b = wa t K) := + fun h1 h2 => (h1.trans h2).mono fun _ h => h.elim id id + have hreg_ne : ∀ r1 r2 : U64, r1.val < 37 → r2.val < 37 → r1 ≠ r2 → Endp.regA t r1 ≠ Endp.regA t r2 := by + intro r1 r2 h1 h2 hne e + have hv := congrArg UScalar.val e + rw [Endp.regA_val hta h1, Endp.regA_val hta h2] at hv + exact hne (UScalar.eq_of_val_eq (by omega)) + have hal_r : ∀ r : U64, r.val < 37 → Aligned (Endp.regA t r) := fun r hr => by + have := Endp.regA_val hta hr; have := hta.align; unfold Aligned; omega + simp only [↓reduceIte] + refine WP.spec_bind (P0.msg.lookup_ipc_buffer.p0 st s1 true t L.total) fun buf _ => ?_ + refine WP.spec_bind (hreg s1 layout.reg.BADGE 0#u64 (by simp [layout.reg.BADGE, layout.reg.X0])) ?_ + rintro s2 ⟨rfl, w2⟩ + unfold msg.set_mr + simp only [show (0#u64 < msg.N_MSG_REGISTERS) = True from by simp [msg.N_MSG_REGISTERS], if_true, + msg.msg_register, Aeneas.Std.bind_ok] + refine WP.spec_bind (Pₘ := fun r : U64 × S => r.1 = 1#u64 ∧ r.2 = L.wr (L.wr s1 (Endp.regA t layout.reg.BADGE) 0#u64) (Endp.regA t layout.reg.X2) v ∧ + WritesIn st (L.wr s1 (Endp.regA t layout.reg.BADGE) 0#u64) r.2 (fun b => ∃ K : U64, 256 ≤ K.val ∧ K.val < 3328 ∧ b = wa t K)) ?_ ?_ + · refine WP.spec_bind (hreg _ layout.reg.X2 v (by simp [layout.reg.X2])) ?_ + rintro s3 ⟨rfl, w3⟩ + have h01 : core.num.U64.wrapping_add 0#u64 1#u64 = 1#u64 := by + apply UScalar.eq_of_val_eq; rw [wa_valm]; simp + simp only [h01, lift_okA, Aeneas.Std.bind_ok] + rw [WP.spec_ok] + exact ⟨rfl, rfl, w3⟩ + rintro ⟨n, s3⟩ ⟨rfl, rfl, w3⟩ + unfold msginfo.MessageInfo.new + simp only [Aeneas.Std.bind_ok] + refine WP.spec_mono (set_msg_info_specA (L.wr (L.wr s1 (Endp.regA t layout.reg.BADGE) 0#u64) (Endp.regA t layout.reg.X2) v) t 0#u64 1#u64 (by decide)) ?_ + rintro s4 ⟨wmi, rfl, hwmi⟩ + have w4 : WritesIn st (L.wr (L.wr s1 (Endp.regA t layout.reg.BADGE) 0#u64) (Endp.regA t layout.reg.X2) v) + (L.wr (L.wr (L.wr s1 (Endp.regA t layout.reg.BADGE) 0#u64) (Endp.regA t layout.reg.X2) v) (Endp.regA t layout.reg.MSG_INFO) wmi) + (fun b => ∃ K : U64, 256 ≤ K.val ∧ K.val < 3328 ∧ b = wa t K) := by + have hK := Endp.regA_val hta (by simp [layout.reg.MSG_INFO, layout.reg.X1] : layout.reg.MSG_INFO.val < 37) + have hA : Aligned (Endp.regA t layout.reg.MSG_INFO) := hal_r _ (by simp [layout.reg.MSG_INFO, layout.reg.X1]) + refine (writesIn_wr hA).mono fun b hb => ?_ + subst hb + refine ⟨264#u64, by decide, by decide, ?_⟩ + apply UScalar.eq_of_val_eq + rw [wa_valm, hK] + have h1 : layout.reg.MSG_INFO.val = 1 := by unfold layout.reg.MSG_INFO layout.reg.X1; rfl + rw [h1, Nat.mod_eq_of_lt (by have := hta.fits; change t.val + 264 < 2 ^ 64; omega)]; change _ = t.val + 264; omega + refine ⟨htr w2 (htr w3 w4), fun _ => ?_, fun h => by cases h⟩ + have h0a := hal_r layout.reg.BADGE (by simp [layout.reg.BADGE, layout.reg.X0]) + have h1a := hal_r layout.reg.MSG_INFO (by simp [layout.reg.MSG_INFO, layout.reg.X1]) + have h2a := hal_r layout.reg.X2 (by simp [layout.reg.X2]) + have h10 : Endp.regA t layout.reg.MSG_INFO ≠ Endp.regA t layout.reg.BADGE := + hreg_ne _ _ (by simp [layout.reg.MSG_INFO, layout.reg.X1]) (by simp [layout.reg.BADGE, layout.reg.X0]) + (by simp [layout.reg.MSG_INFO, layout.reg.X1, layout.reg.BADGE, layout.reg.X0]) + have h12 : Endp.regA t layout.reg.MSG_INFO ≠ Endp.regA t layout.reg.X2 := + hreg_ne _ _ (by simp [layout.reg.MSG_INFO, layout.reg.X1]) (by simp [layout.reg.X2]) + (by simp [layout.reg.MSG_INFO, layout.reg.X1, layout.reg.X2]) + have h20 : Endp.regA t layout.reg.X2 ≠ Endp.regA t layout.reg.BADGE := + hreg_ne _ _ (by simp [layout.reg.X2]) (by simp [layout.reg.BADGE, layout.reg.X0]) + (by simp [layout.reg.X2, layout.reg.BADGE, layout.reg.X0]) + refine ⟨?_, ?_, ?_, fun r hr hne0 hne2 hne1 => ?_⟩ + · rw [mem_wr_neA h1a h0a h10, mem_wr_neA h2a h0a h20, L.mem_wr_same h0a] + · rw [mem_wr_neA h1a h2a h12, L.mem_wr_same h2a] + · rw [L.mem_wr_same h1a, hwmi] + have h0 : ((0#u64 : U64).bv &&& Fl4SpecA64.mask Fl4SpecA64.msgLabelBits) = 0 := by simp + rw [h0]; rfl + · have hra := hal_r r hr + rw [mem_wr_neA h1a hra (hreg_ne _ _ (by simp [layout.reg.MSG_INFO, layout.reg.X1]) hr hne1.symm), + mem_wr_neA h2a hra (hreg_ne _ _ (by simp [layout.reg.X2]) hr hne2.symm), + mem_wr_neA h0a hra (hreg_ne _ _ (by simp [layout.reg.BADGE, layout.reg.X0]) hr hne0.symm)] + +private theorem replyCtxA_regsA (x1 : Fl4SpecA64.Tcb) (v : Fl4SpecA64.Word) : + (replyCtxA x1 v).userRegs .x0 = 0 ∧ + (replyCtxA x1 v).userRegs .x1 = Fl4SpecA64.messageInfoToData ⟨Fl4SpecA64.natToLen 1, 0, 0, 0⟩ ∧ + (replyCtxA x1 v).userRegs .x2 = v ∧ + ∀ r : Fl4SpecA64.Register, r ≠ .x0 → r ≠ .x1 → r ≠ .x2 → + (replyCtxA x1 v).userRegs r = x1.tcbArch.tcbContext.userRegs r := by + have hmin1 : min 1 msgMaxLength = 1 := by unfold msgMaxLength; decide + have hmin2 : min msg_registers.length 1 = 1 := by unfold msg_registers; decide + have hlen1 : (match x1.tcbIpcframe with + | .archObjectCap (.frameCap _ R _ false _) => + if vmReadWrite ⊆ R ∨ vmReadOnly ⊆ R ∧ ¬true = true + then natToLen (min [v].length msgMaxLength) + else natToLen (min msg_registers.length [v].length) + | _ => natToLen (min msg_registers.length [v].length)) = natToLen 1 := by + simp only [List.length_singleton, hmin1, hmin2] + cases x1.tcbIpcframe with + | archObjectCap ac => + cases ac with + | frameCap p R sz dev mp => + cases dev with + | true => rfl + | false => by_cases h : vmReadWrite ⊆ R ∨ vmReadOnly ⊆ R ∧ ¬true = true <;> simp [h] + | _ => rfl + | _ => rfl + refine ⟨?_, ?_, ?_, fun r h0 h1 h2 => ?_⟩ + · unfold replyCtxA + simp only [msg_info_register, badge_register, msg_registers, List.length_singleton, List.take, + Function.update, show (Fl4SpecA64.Register.x0 ≠ .x1) from by decide, + show (Fl4SpecA64.Register.x0 ≠ .x2) from by decide, List.mem_singleton, ↓reduceDIte, ↓reduceIte] + rfl + · unfold replyCtxA + simp only [msg_info_register, msgInfoRegister, Function.update, ↓reduceDIte, List.length_singleton, hmin1, hmin2] + cases x1.tcbIpcframe with + | archObjectCap ac => cases ac with + | frameCap p R sz dev mp => cases dev with + | true => rfl + | false => simp only [ite_self] + | _ => rfl + | _ => rfl + · unfold replyCtxA + simp only [msg_info_register, msgInfoRegister, badge_register, badgeRegister, msg_registers, msgRegisters, + List.length_singleton, List.take, Function.update, show (Fl4SpecA64.Register.x2 ≠ .x1) from by decide, + List.mem_singleton, ↓reduceDIte, ↓reduceIte, theIndex, the] + rfl + · unfold replyCtxA msg_info_register msgInfoRegister badge_register badgeRegister msg_registers msgRegisters + dsimp only + rw [Function.update_of_ne h1, if_neg (by intro hm; simp [Fl4SpecA64.Register.x2ToX5, List.take] at hm; exact h2 hm), Function.update_of_ne h0] + +private theorem regIdxA_ne_x0 {r : Fl4SpecA64.Register} (h : r ≠ .x0) : regIdxA r ≠ layout.reg.BADGE := by + unfold layout.reg.BADGE layout.reg.X0; cases r <;> first | exact absurd rfl h | (unfold regIdxA; decide) + +private theorem regIdxA_ne_x1 {r : Fl4SpecA64.Register} (h : r ≠ .x1) : regIdxA r ≠ layout.reg.MSG_INFO := by + unfold layout.reg.MSG_INFO layout.reg.X1; cases r <;> first | exact absurd rfl h | (unfold regIdxA; decide) + +private theorem regIdxA_ne_x2 {r : Fl4SpecA64.Register} (h : r ≠ .x2) : regIdxA r ≠ layout.reg.X2 := by + unfold layout.reg.X2; cases r <;> first | exact absurd rfl h | (unfold regIdxA; decide) + +private theorem JStage3A.of_reply1A {ar : machine.Arch A} {nA : U64} + {s1 s' : S} {a1 : A} {Ac' : Set U64} {σ1 : State Unit} {x1 : Fl4SpecA64.Tcb} + (J1 : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s1 a1 σ1 m) + (hk1 : σ1.kheap σ1.curThread = some (.tcb x1)) + (hcur : (L.mem s1 (inqA (L.curW s1)) &&& sched.Q_RELEASE) = 0#u64 ∧ + L.mem s1 (scA (L.curW s1)) ≠ 0#u64 ∧ L.mem s1 (scA (L.curW s1)) ∈ Ac) + (hrun1 : Runnable (L.mem s1 (stA (L.curW s1)))) + (hnvm1 : L.mem s1 (stA (L.curW s1)) ≠ sched.TS_RUNNING_VM) + (call : Bool) (v : U64) + (hrep : vspace.reply1 st s1 (L.curW s1) call v = ok s') + (sysl : SysL st H Z.Cs OS ia ie s') (symL : SymL st H Z.Cs s') + (bxi' : BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac' s') + (us' : UserOK st H.tcbs Z.Cs H.omega H U s') + (j7' : ∀ σ3, J7A st (J7R2A st ar nA) Z H OS s' σ3 m) : + ∃ σ3, ((), σ3) ∈ ((do + Fl4Spec.NonDet.when call (Fl4SpecA64.replyFromKernel (Z := Unit) σ1.curThread (0, [v.bv])) + Fl4SpecA64.setThreadState (Z := Unit) σ1.curThread .running) σ1).1 ∧ + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s' a1 σ3 m := by + set t := L.curW s1 + have htZ : t ∈ Z.T := J1.sr.curT + have hta : TcbAddr t := J1.sr.core.tcbs t htZ + have hct : σ1.curThread = specPtr t := J1.j.1.sch.curThread + have hx1 : σ1.kheap (specPtr t) = some (.tcb x1) := hct ▸ hk1 + have hscC : L.mem s1 (scA t) ∈ Z.C := J1.j.1.rest.scMem t htZ hcur.2.1 + obtain ⟨ysc, nsc, hysc, -⟩ := J1.j.1.sch.sc _ hscC + obtain ⟨ysc', _, hysc', hrm_eq⟩ := J1.j.1.rest.refillMax _ hscC + rw [hysc] at hysc'; cases hysc' + have hrm_nz : L.rmax s1 (L.mem s1 (scA t)) ≠ 0#u64 := + (J1.sys.ent.bxi.2.2.2.2.act _ (J1.scs ▸ hscC)).1 hcur.2.2 + have hpos_rm : 0 < ysc.scRefillMax := by + rw [hrm_eq] + have e2 : L.rmax s1 (L.mem s1 (scA t)) = L.mem s1 (wa (L.mem s1 (scA t)) 72#u64) := by + unfold LawfulStore.rmax; rw [maxA_eqA] + rw [e2] at hrm_nz + exact Nat.pos_of_ne_zero (fun e => hrm_nz (UScalar.eq_of_val_eq (by simpa using e))) + have hsc_some : x1.tcbSchedContext = some (scRenA m (L.mem s1 (scA t))) := by + obtain ⟨x0, hk0, -, hsc0⟩ := J1.j.1.sch.tcb t htZ + rw [hx1] at hk0; cases hk0 + rw [hsc0, optScA, if_neg hcur.2.1] + have hrq1 : σ1.curThread ∉ σ1.releaseQueue := hct ▸ J1.sr.relCur.1 hcur.1 + set uc' := if call then replyCtxA x1 v.bv else x1.tcbArch.tcbContext + set x3 : Fl4SpecA64.Tcb := { x1 with tcbArch := { x1.tcbArch with tcbContext := uc' }, tcbState := .running } + set σ3 := setObjA σ1 σ1.curThread (.tcb x3) + have hspec : ((), σ3) ∈ ((do + Fl4Spec.NonDet.when call (Fl4SpecA64.replyFromKernel (Z := Unit) σ1.curThread (0, [v.bv])) + Fl4SpecA64.setThreadState (Z := Unit) σ1.curThread .running) σ1).1 := by + have hsp := replyRun_oneA hk1 call v.bv fun uc'' _ _ => by + refine schedulable_ofA (x := { x1 with tcbArch := { x1.tcbArch with tcbContext := uc'' }, tcbState := .running }) + (n := nsc) (by simp [setObjA]) rfl hsc_some ?_ hpos_rm hrq1 + have hne : scRenA m (L.mem s1 (scA t)) ≠ σ1.curThread := fun e => by rw [e, hk1] at hysc; cases hysc + simpa [setObjA, Function.update_of_ne hne] using hysc + have hσ3_eq : stUpdA (ctxUpdA σ1 σ1.curThread x1 uc') σ1.curThread + { x1 with tcbArch := { x1.tcbArch with tcbContext := uc' } } .running = σ3 := by + unfold σ3 stUpdA ctxUpdA setObjA; simp [x3, Function.update_idem] + rw [hsp, hσ3_eq]; exact Set.mem_singleton _ + unfold vspace.reply1 at hrep + obtain ⟨s2, hs2, hw2, hcallT, hcallF⟩ := spec_ex_fd (reply1_regs_eqA (st := st) (s1 := s1) (t := t) (v := v) hta call) + rw [hs2, bind_ok] at hrep + have hF0 : ∀ b, (∃ K : U64, 256 ≤ K.val ∧ K.val < 3328 ∧ b = wa t K) → TcbF0WA Z.T b := by + rintro b ⟨K, hK1, hK2, rfl⟩ + have hv := Notif.tfield_val hta (by omega : K.val < 4096) + refine ⟨t, htZ, by omega, by omega, fun k hk => ?_⟩ + rw [hv]; rcases hk with rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> omega + have hst_al : Aligned (stA t) := by rw [stA_eq]; exact field_aligned hta (by simp) (by simp) + have hmΩ := J1.sys.ent.bxi.2.2.2.2.sub _ (J1.sys.ent.bxi.2.2.2.2.all _ (J1.scs ▸ hscC)).2.1 + have hma : Aligned (maxA (L.mem s1 (scA t))) := (J1.sq.omega _ hmΩ).1 + have hmx : stA t ≠ maxA (L.mem s1 (scA t)) := fun e => by + have hdis := (J1.sq.omega _ hmΩ).2.1 _ (J1.tcbs ▸ htZ) + rw [← e, stA_eq, Notif.tfield_val hta (by decide)] at hdis; simp at hdis + have hsc2_eq : L.mem s2 (scA t) = L.mem s1 (scA t) := by + refine hw2.mem_eq (by rw [scA]; exact field_aligned hta (by decide) (by decide)) ?_ + rintro ⟨K, _, hK2, he⟩; have hv := congrArg UScalar.val he + rw [scA, Notif.tfield_val hta (by decide), Notif.tfield_val hta (by omega)] at hv; simp at hv; omega + have hiq2_eq : L.mem s2 (inqA t) = L.mem s1 (inqA t) := by + refine hw2.mem_eq (by rw [inqA]; exact field_aligned hta (by decide) (by decide)) ?_ + rintro ⟨K, _, hK2, he⟩; have hv := congrArg UScalar.val he + rw [inqA, Notif.tfield_val hta (by decide), Notif.tfield_val hta (by omega)] at hv; simp at hv; omega + have hrm2_eq : L.rmax s2 (L.mem s1 (scA t)) = L.rmax s1 (L.mem s1 (scA t)) := by + refine hw2.mem_eq hma ?_ + rintro ⟨K, _, hK2, he⟩ + have hdis := (J1.sq.omega _ hmΩ).2.1 _ (J1.tcbs ▸ htZ) + rw [he, Notif.tfield_val hta (by omega)] at hdis; omega + have hst_wr := set_thread_state_run_wrA (st := st) (s := s2) hta + (by rw [hsc2_eq]; exact hcur.2.1) + (by rw [hsc2_eq, hrm2_eq]; exact hrm_nz) + (by rw [hiq2_eq]; exact hcur.1) + (by rw [hsc2_eq]; exact hma) + (by rw [hsc2_eq]; exact hmx) + rw [hst_wr] at hrep + have hs3_eq : L.wr s2 (stA t) sched.TS_RUNNING = s' := okinj hrep + subst hs3_eq + set s3 := L.wr s2 (stA t) sched.TS_RUNNING + have wAll : WritesIn st s1 s3 (SchedStepNWA (st := st) N E Rp Z m σ1 s1) := + (hw2.trans (writesIn_wr hst_al)).mono fun b hb => + hb.elim (fun h => TcbF0WA.stepN J1.sr.core.tcbs (hF0 b h)) + (fun h => h ▸ Or.inl (Or.inr (stA_tcbStepWA J1.sr.core.tcbs htZ))) + have wBand : WritesIn st s1 s3 (fun b => ∃ K : U64, 256 ≤ K.val ∧ K.val ≤ 3328 ∧ b = wa t K) := + (hw2.trans (writesIn_wr hst_al)).mono fun b hb => + hb.elim (fun ⟨K, h1, h2, e⟩ => ⟨K, h1, by omega, e⟩) + (fun e => ⟨3328#u64, by decide, by decide, e.trans (stA_eq t)⟩) + have hmem_tcb : ∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → + (u ≠ t ∨ K.val < 256 ∨ 3328 < K.val) → L.mem s3 (wa u K) = L.mem s1 (wa u K) := by + intro u hu K hK4 hK8 hdisj + refine wBand.mem_eq (field_aligned (J1.sr.core.tcbs u hu) hK4 hK8) ?_ + rintro ⟨K', hK'1, hK'2, he⟩ + have h1 := J1.sr.core.tcbs u hu + have hv := congrArg UScalar.val he + rw [Notif.tfield_val h1 hK4, Notif.tfield_val hta (by omega)] at hv + rcases hdisj with hne | hlo | hhi + · have := h1.align; have := hta.align + exact hne (UScalar.eq_of_val_eq (by omega)) + · have := h1.align; have := hta.align; omega + · have := h1.align; have := hta.align; omega + have hmem_nonTcb : ∀ b, Aligned b → (∀ K : U64, 256 ≤ K.val → K.val ≤ 3328 → b ≠ wa t K) → + L.mem s3 b = L.mem s1 b := fun b hb hne => + wBand.mem_eq hb fun ⟨K, h1, h2, e⟩ => hne K h1 h2 e + have hks3 : L.ksb s3 = L.ksb s1 := wAll.1 + have hcpu3 : L.cpu s3 = L.cpu s1 := wAll.2.1 + have hne_ct_other : ∀ p, p ≠ σ1.curThread → σ3.kheap p = σ1.kheap p := fun p hp => by + unfold σ3 setObjA; simp [Function.update, hp] + have hkh_ct : σ3.kheap σ1.curThread = some (.tcb x3) := by + unfold σ3 setObjA; simp [Function.update] + have hHS : SpecSchedA σ1 σ3 := + { kh := fun p => by + by_cases e : p = σ1.curThread + · right; left; subst e; exact ⟨x1, x3, hk1, hkh_ct, ⟨rfl, rfl, rfl⟩⟩ + · left; exact hne_ct_other p e + irqS := rfl, irqN := rfl, asid := rfl, vcur := rfl, cdt := rfl, orig := rfl } + have hSSz : SSzA σ1 σ3 := fun p y k h => by + by_cases e : p = σ1.curThread + · subst e; rw [hkh_ct] at h; cases h + · rw [hne_ct_other p e] at h; exact ⟨y, h⟩ + have hQk : QKeepA σ1 σ3 := + { prio := fun p x hx => by + by_cases e : p = σ1.curThread + · subst e; rw [hkh_ct] at hx; cases hx; exact ⟨x1, hk1, rfl⟩ + · rw [hne_ct_other p e] at hx; exact ⟨x, hx, rfl⟩ + ep := fun p ep h => by + by_cases e : p = σ1.curThread + · subst e; rw [hkh_ct] at h; cases h + · rw [hne_ct_other p e] at h; exact ⟨ep, h, List.Sublist.refl _⟩ + ntfn := fun p nt h => by + by_cases e : p = σ1.curThread + · subst e; rw [hkh_ct] at h; cases h + · rw [hne_ct_other p e] at h; exact ⟨nt, h, List.Sublist.refl _⟩ + sc := fun p y k h => by + by_cases e : p = σ1.curThread + · subst e; rw [hkh_ct] at h; cases h + · rw [hne_ct_other p e] at h; exact ⟨y, k, h, fun hne => ⟨hne, id⟩⟩ + rt := fun p r h => by + by_cases e : p = σ1.curThread + · subst e; rw [hkh_ct] at h; cases h + · rw [hne_ct_other p e] at h; exact ⟨r, h, id⟩ + dom := ⟨rfl, rfl, rfl, rfl⟩ } + have hK : IpcKeepA σ1 σ3 := + { tcb := fun p x hx => by + by_cases e : p = σ1.curThread + · subst e; rw [hk1] at hx; cases hx; exact ⟨x3, hkh_ct, rfl, rfl, rfl, rfl, rfl, rfl⟩ + · exact ⟨x, by rw [hne_ct_other p e]; exact hx, rfl, rfl, rfl, rfl, rfl, rfl⟩ + scF := fun p y k h => by + have hne : p ≠ σ1.curThread := fun e => by rw [e, hk1] at h; cases h + exact ⟨y, k, by rw [hne_ct_other p hne]; exact h, rfl⟩ + scB := fun p y k h => by + have hne : p ≠ σ1.curThread := fun e => by rw [e, hkh_ct] at h; cases h + exact ⟨y, k, by rwa [hne_ct_other p hne] at h, rfl⟩ } + have hsr2 : SchedSRA st Z m κ s2 σ1 := Sched.SchedSRA.f0A J1.sr hw2 hF0 + have hst2 : StEqA Z.T (L.mem s2) σ1 := + stEqA_f0A (fun u hu y hy => (J1.ep.at_tcb hu hy).st) J1.sr.core.tcbs hw2 hF0 + have hrun_sp : runnable x1.tcbState = true := (runnable_iffA ((J1.ep.at_tcb htZ hx1).st)).1 hrun1 + have hcode : sched.TS_RUNNING.val = tsCodeA .running := by rw [ts_vals.1]; rfl + have hsc_step := sc_set_runnable_stateA hsr2 hst2 htZ hx1 hrun_sp (ts := .running) rfl hcode + rw [hst_wr, WP.spec_ok] at hsc_step + obtain ⟨τ, hτ_step, hsr3_τ, -, -, -⟩ := hsc_step + have hτ_eq : τ = stUpdA σ1 σ1.curThread x1 .running := by + have hsp := setThreadState_stsActExA hx1 .running + rw [hsp] at hτ_step + have hns : schedulable (specPtr t) (updKSc σ1 (specPtr t) (.tcb { x1 with tcbState := .running })) = true := by + refine schedulable_ofA (x := { x1 with tcbState := .running }) (n := nsc) + (by simp [updKSc]) rfl hsc_some ?_ hpos_rm (hct ▸ hrq1) + have hne : scRenA m (L.mem s1 (scA t)) ≠ specPtr t := fun e => by rw [e, hx1] at hysc; cases hysc + simpa [updKSc, Function.update_of_ne hne] using hysc + have he := (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst hτ_step))).2.symm + rw [stsActA_eq_self (fun h => h.2.2 hns)] at he; simpa [hct, stUpdA, setObjA, updKSc] using he + subst hτ_eq + have hsr3 : SchedSRA st Z m κ s3 σ3 := by + have hτ_kh_eq : ∀ p, p ≠ σ1.curThread → (stUpdA σ1 σ1.curThread x1 .running).kheap p = σ1.kheap p := fun p hp => by + unfold stUpdA setObjA; simp [Function.update, hp] + have hτ_kh_ct : (stUpdA σ1 σ1.curThread x1 .running).kheap σ1.curThread = some (.tcb { x1 with tcbState := .running }) := by + unfold stUpdA setObjA; simp [Function.update] + have hkc : ∀ c ∈ Z.C, σ3.kheap (scNameA m c) = (stUpdA σ1 σ1.curThread x1 .running).kheap (scNameA m c) := fun c hc => by + obtain ⟨y, k, hy, -⟩ := J1.j.1.sch.sc c hc + have hne : scNameA m c ≠ σ1.curThread := fun e => by + have e' : scRenA m c = σ1.curThread := e + rw [e'] at hy; rw [hk1] at hy; cases hy + rw [hne_ct_other _ hne, hτ_kh_eq _ hne] + have htcb : ∀ u ∈ Z.T, ∀ x, (stUpdA σ1 σ1.curThread x1 .running).kheap (specPtr u) = some (.tcb x) → + ∃ y, σ3.kheap (specPtr u) = some (.tcb y) ∧ + y.tcbSchedContext = x.tcbSchedContext ∧ y.tcbDomain = x.tcbDomain ∧ y.tcbPriority = x.tcbPriority := by + intro u hu x hx + by_cases e : specPtr u = σ1.curThread + · rw [e] at hx; rw [hτ_kh_ct] at hx; cases hx + exact ⟨x3, by rw [e]; exact hkh_ct, rfl, rfl, rfl⟩ + · rw [hτ_kh_eq _ e] at hx + exact ⟨x, by rw [hne_ct_other _ e]; exact hx, rfl, rfl, rfl⟩ + have hfix : SFixA (stUpdA σ1 σ1.curThread x1 .running) σ3 := + ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ + have hrt_mem : ∀ u ∈ (stUpdA σ1 σ1.curThread x1 .running).releaseQueue, + specRtA σ3 u = specRtA (stUpdA σ1 σ1.curThread x1 .running) u := fun u hu => by + obtain ⟨x, p, y, k, hx, hsc, hp, ha⟩ := hsr3_τ.inv.relSc u hu + have hne_p : p ≠ σ1.curThread := fun e => by rw [e, hτ_kh_ct] at hp; cases hp + have hp1 : σ1.kheap p = some (.schedContext y k) := by rwa [hτ_kh_eq _ hne_p] at hp + have hp3 : σ3.kheap p = some (.schedContext y k) := by rw [hne_ct_other _ hne_p]; exact hp1 + have hne_ref : y.scRefills ≠ [] := hsr3_τ.inv.ne hp ha + have hτ_rt : specRtA (stUpdA σ1 σ1.curThread x1 .running) u = (refillHd y).rTime := by + unfold specRtA; rw [readTcbReadyTime_eqA hx hsc hp hne_ref]; rfl + have h3_rt : specRtA σ3 u = (refillHd y).rTime := by + by_cases e : u = σ1.curThread + · subst e; rw [hτ_kh_ct] at hx; cases hx + unfold specRtA; rw [readTcbReadyTime_eqA hkh_ct hsc hp3 hne_ref]; rfl + · rw [hτ_kh_eq _ e] at hx + unfold specRtA; rw [readTcbReadyTime_eqA (by rw [hne_ct_other _ e]; exact hx) hsc hp3 hne_ref]; rfl + exact h3_rt.trans hτ_rt.symm + have hscInv3 : ScInvA m σ3 := + { refills := fun p y k h ha hp => by + have hne : p ≠ σ1.curThread := fun e => by rw [e, hkh_ct] at h; cases h + rw [hne_ct_other _ hne] at h + exact hsr3_τ.inv.refills p y k (by rw [hτ_kh_eq _ hne]; exact h) ha hp + rr := fun p y k h ha hp => by + have hne : p ≠ σ1.curThread := fun e => by rw [e, hkh_ct] at h; cases h + rw [hne_ct_other _ hne] at h + exact hsr3_τ.inv.rr p y k (by rw [hτ_kh_eq _ hne]; exact h) ha hp + ntfnIdle := fun q x h => by + have hne : q ≠ σ1.curThread := fun e => by rw [e, hkh_ct] at h; cases h + rw [hne_ct_other _ hne] at h + exact hsr3_τ.inv.ntfnIdle q x (by rw [hτ_kh_eq _ hne]; exact h) + ntfnIdleOf := fun q x h => by + have hne : q ≠ σ1.curThread := fun e => by rw [e, hkh_ct] at h; cases h + rw [hne_ct_other _ hne] at h + exact hsr3_τ.inv.ntfnIdleOf q x (by rw [hτ_kh_eq _ hne]; exact h) + relSc := fun u hu => by + obtain ⟨x, p, y, k, hx, hsc, hp, ha⟩ := hsr3_τ.inv.relSc u hu + have hne_p : p ≠ σ1.curThread := fun e => by rw [e, hτ_kh_ct] at hp; cases hp + rw [hτ_kh_eq _ hne_p] at hp + by_cases e : u = σ1.curThread + · subst e; rw [hτ_kh_ct] at hx; cases hx + exact ⟨x3, p, y, k, hkh_ct, hsc, by rw [hne_ct_other _ hne_p]; exact hp, ha⟩ + · rw [hτ_kh_eq _ e] at hx + exact ⟨x, p, y, k, by rw [hne_ct_other _ e]; exact hx, hsc, by rw [hne_ct_other _ hne_p]; exact hp, ha⟩ + sorted := by + have hs := List.pairwise_iff_getElem.1 hsr3_τ.inv.sorted + refine List.pairwise_iff_getElem.2 fun i j hi hj hij => ?_ + rw [hrt_mem _ (List.getElem_mem hi), hrt_mem _ (List.getElem_mem hj)] + exact hs i j hi hj hij + back := fun u x p hx hsc => by + have h0 : ∃ y k, (stUpdA σ1 σ1.curThread x1 .running).kheap p = some (.schedContext y k) ∧ y.scTcb = some u := by + by_cases e : u = σ1.curThread + · subst e; rw [hkh_ct] at hx; cases hx + exact hsr3_τ.inv.back _ _ p hτ_kh_ct hsc + · rw [hne_ct_other _ e] at hx + exact hsr3_τ.inv.back u x p (by rw [hτ_kh_eq _ e]; exact hx) hsc + obtain ⟨y, k, hp, ht'⟩ := h0 + have hne_p : p ≠ σ1.curThread := fun e => by rw [e, hτ_kh_ct] at hp; cases hp + rw [hτ_kh_eq _ hne_p] at hp + exact ⟨y, k, by rw [hne_ct_other _ hne_p]; exact hp, ht'⟩ + tcbScNotIdle := fun u x p hx hsc hni => by + by_cases e : u = σ1.curThread + · subst e; rw [hkh_ct] at hx; cases hx + exact hsr3_τ.inv.tcbScNotIdle _ _ p hτ_kh_ct hsc hni + · rw [hne_ct_other _ e] at hx + exact hsr3_τ.inv.tcbScNotIdle u x p (by rw [hτ_kh_eq _ e]; exact hx) hsc hni } + have hrd3 : RdA σ3 := by + intro u x hx hc x' scp y k h1 h2 h3 h4 h5 + have hne_p : scp ≠ σ1.curThread := fun e => by rw [e, hkh_ct] at h3; cases h3 + rw [hne_ct_other _ hne_p] at h3 + have h3' : (stUpdA σ1 σ1.curThread x1 .running).kheap scp = some (.schedContext y k) := by + rw [hτ_kh_eq _ hne_p]; exact h3 + by_cases e : u = σ1.curThread + · subst e; rw [hkh_ct] at hx h1; cases hx; cases h1 + exact hsr3_τ.rd _ { x1 with tcbState := .running } hτ_kh_ct hc + { x1 with tcbState := .running } scp y k hτ_kh_ct h2 h3' h4 h5 + · rw [hne_ct_other _ e] at hx h1 + exact hsr3_τ.rd u x (by rw [hτ_kh_eq _ e]; exact hx) hc + x' scp y k (by rw [hτ_kh_eq _ e]; exact h1) h2 h3' h4 h5 + have hrun3 : RunQA σ3 := by + constructor + · intro d p u hu + obtain ⟨y, hy, hr⟩ := hsr3_τ.run.1 d p u hu + by_cases e : u = σ1.curThread + · subst e; rw [hτ_kh_ct] at hy; cases hy + exact ⟨x3, hkh_ct, hr⟩ + · rw [hτ_kh_eq _ e] at hy + exact ⟨y, by rw [hne_ct_other _ e]; exact hy, hr⟩ + · intro u hu + obtain ⟨y, hy, hr⟩ := hsr3_τ.run.2 u hu + by_cases e : u = σ1.curThread + · subst e; rw [hτ_kh_ct] at hy; cases hy + exact ⟨x3, hkh_ct, hr⟩ + · rw [hτ_kh_eq _ e] at hy + exact ⟨y, by rw [hne_ct_other _ e]; exact hy, hr⟩ + exact hsr3_τ.rebuild rfl rfl (fun _ _ _ _ _ _ => rfl) (fun _ _ => rfl) (fun _ _ _ _ => rfl) + (fun _ _ _ => rfl) hkc htcb hfix hsr3_τ.sch.act hsr3_τ.actOn hrd3 hscInv3 + hsr3_τ.core hsr3_τ.rest.rq hsr3_τ.rest.rqDom rfl hrun3 + have hEp3 : EpStateRel Z.T E Rp Bf (L.mem s3) σ3 := by + have hmem_tcb_ne : ∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → + (K.val < 256 ∨ 3328 < K.val) → L.mem s3 (wa u K) = L.mem s1 (wa u K) := + fun u hu K h1 h2 h3 => hmem_tcb u hu K h1 h2 (Or.inr h3) + have hst_ne : ∀ u ∈ Z.T, u ≠ t → L.mem s3 (stA u) = L.mem s1 (stA u) := fun u hu hne => by + rw [stA_eq]; exact hmem_tcb u hu 3328#u64 (by decide) (by decide) (Or.inl hne) + refine + { tcb := fun u hu => by + obtain ⟨y, hy, htr⟩ := J1.ep.tcb u hu + by_cases e : u = t + · subst e + rw [hx1] at hy; cases hy + refine ⟨x3, hct ▸ hkh_ct, ?_⟩ + have hregs3 : ∀ r, (L.mem s3 (Endp.regA t (regIdxA r))).bv = x3.tcbArch.tcbContext.userRegs r := by + intro r + have hr384 : (regIdxA r).val < 37 := by cases r <;> decide + have hreg_ne_st : Endp.regA t (regIdxA r) ≠ stA t := by + intro heq; have hv := congrArg UScalar.val heq + rw [Endp.regA_val hta hr384, stA_eq, Notif.tfield_val hta (by decide)] at hv + simp at hv; omega + have hreg_al : Aligned (Endp.regA t (regIdxA r)) := by + have := Endp.regA_val hta hr384; have := hta.align; unfold Aligned; omega + rw [mem_wr_neA hst_al hreg_al hreg_ne_st.symm] + cases hcall : call with + | false => + rw [hcallF hcall] + simpa [x3, uc', hcall] using htr.regs r + | true => + obtain ⟨h0, h2, h1, hoth⟩ := hcallT hcall + obtain ⟨hr0, hr1, hr2, hroth⟩ := replyCtxA_regsA x1 v.bv + simp only [x3, uc', hcall, ↓reduceIte] + by_cases r0 : r = .x0 + · subst r0; rw [hr0, show regIdxA .x0 = layout.reg.BADGE from by unfold regIdxA layout.reg.BADGE layout.reg.X0; rfl, h0]; rfl + · by_cases r1 : r = .x1 + · subst r1; rw [hr1, show regIdxA .x1 = layout.reg.MSG_INFO from by unfold regIdxA layout.reg.MSG_INFO layout.reg.X1; rfl]; exact h1 + · by_cases r2 : r = .x2 + · subst r2; rw [hr2, show regIdxA .x2 = layout.reg.X2 from by unfold regIdxA layout.reg.X2; rfl, h2] + · rw [hroth r r0 r1 r2, hoth (regIdxA r) hr384 (regIdxA_ne_x0 r0) (regIdxA_ne_x2 r2) (regIdxA_ne_x1 r1)] + exact htr.regs r + refine + { st := by rw [L.mem_wr_same hst_al, ts_vals.1]; rfl + blk := fun _ h => by simp [x3, epBlocked] at h + send := fun _ _ h => by cases h + recv := fun _ _ _ h => by cases h + reply := fun _ h => by cases h + regs := hregs3 + prio := by + have hp : prioA t = wa t 3448#u64 := rfl + rw [hp, hmem_tcb_ne t htZ 3448#u64 (by decide) (by decide) (Or.inr (by decide)), ← hp]; exact htr.prio + fault0 := by + have hf : tFaultA t = wa t 3376#u64 := rfl + rw [hf, hmem_tcb_ne t htZ 3376#u64 (by decide) (by decide) (Or.inr (by decide)), ← hf]; exact htr.fault0 + faultT := by + have hf : tFaultA t = wa t 3376#u64 := rfl + rw [hf, hmem_tcb_ne t htZ 3376#u64 (by decide) (by decide) (Or.inr (by decide)), ← hf]; exact htr.faultT + bound := by + have hb : tBoundA t = wa t 3368#u64 := rfl + rw [hb, hmem_tcb_ne t htZ 3368#u64 (by decide) (by decide) (Or.inr (by decide)), ← hb]; exact htr.bound + faultR := by + unfold FaultRel at *; have hfr := htr.faultR + have h0 : L.mem s3 (tFaultA t) = L.mem s1 (tFaultA t) := hmem_tcb_ne t htZ 3376#u64 (by decide) (by decide) (Or.inr (by decide)) + have h1 : L.mem s3 (tFw0A t) = L.mem s1 (tFw0A t) := hmem_tcb_ne t htZ 3384#u64 (by decide) (by decide) (Or.inr (by decide)) + have h2 : L.mem s3 (tFw1A t) = L.mem s1 (tFw1A t) := hmem_tcb_ne t htZ 3392#u64 (by decide) (by decide) (Or.inr (by decide)) + have h3 : L.mem s3 (tFw2A t) = L.mem s1 (tFw2A t) := hmem_tcb_ne t htZ 3400#u64 (by decide) (by decide) (Or.inr (by decide)) + have h4 : L.mem s3 (tLfA t) = L.mem s1 (tLfA t) := hmem_tcb_ne t htZ 3408#u64 (by decide) (by decide) (Or.inr (by decide)) + have h5 : L.mem s3 (tLf0A t) = L.mem s1 (tLf0A t) := hmem_tcb_ne t htZ 3416#u64 (by decide) (by decide) (Or.inr (by decide)) + have h6 : L.mem s3 (tLf1A t) = L.mem s1 (tLf1A t) := hmem_tcb_ne t htZ 3424#u64 (by decide) (by decide) (Or.inr (by decide)) + rw [h0, h1, h2, h3, h4, h5, h6]; exact hfr } + · have hne_p : specPtr u ≠ σ1.curThread := fun h => e (specPtr_inj (h.trans hct)) + have hmu : ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → L.mem s3 (wa u K) = L.mem s1 (wa u K) := + fun K h1 h2 => hmem_tcb u hu K h1 h2 (Or.inl e) + refine ⟨y, by rw [hne_ct_other _ hne_p]; exact hy, ?_⟩ + refine + { st := by rw [hst_ne u hu e]; exact htr.st + blk := fun e0 he0 => by + have hb : tBlockA u = wa u 3336#u64 := rfl + rw [hb, hmu 3336#u64 (by decide) (by decide), ← hb]; exact htr.blk e0 he0 + send := fun e0 d hd => by + have h1 : tBadgeA u = wa u 3344#u64 := rfl + have h2 : tFlagsA u = wa u 3352#u64 := rfl + rw [h1, h2, hmu 3344#u64 (by decide) (by decide), hmu 3352#u64 (by decide) (by decide), ← h1, ← h2] + exact htr.send e0 d hd + recv := fun e0 r d hd => by + have hr' : tReplyA u = wa u 3360#u64 := rfl + rw [hr', hmu 3360#u64 (by decide) (by decide), ← hr']; exact htr.recv e0 r d hd + reply := fun r hr => by + have hr' : tReplyA u = wa u 3360#u64 := rfl + rw [hr', hmu 3360#u64 (by decide) (by decide), ← hr']; exact htr.reply r hr + regs := fun r => by + have hr384 : (regIdxA r).val < 37 := by cases r <;> decide + have hlt : (⟨BitVec.ofNat 64 (256 + 8 * (regIdxA r).val)⟩ : U64).val < 4096 := by + change (BitVec.ofNat 64 (256 + 8 * (regIdxA r).val)).toNat < 4096 + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)]; omega + have hal : (⟨BitVec.ofNat 64 (256 + 8 * (regIdxA r).val)⟩ : U64).val % 8 = 0 := by + change (BitVec.ofNat 64 (256 + 8 * (regIdxA r).val)).toNat % 8 = 0 + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (a := 256 + 8 * (regIdxA r).val) (b := 2 ^ 64) (by omega)]; omega + have hre : Endp.regA u (regIdxA r) = wa u ⟨BitVec.ofNat 64 (256 + 8 * (regIdxA r).val)⟩ := by + apply UScalar.eq_of_val_eq + have hu_a := J1.sr.core.tcbs u hu + rw [Endp.regA_val hu_a hr384, Notif.tfield_val hu_a hlt] + change _ = u.val + (BitVec.ofNat 64 (256 + 8 * (regIdxA r).val)).toNat + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)]; omega + rw [hre, hmu _ hlt hal, ← hre] + exact htr.regs r + prio := by + have hp : prioA u = wa u 3448#u64 := rfl + rw [hp, hmu 3448#u64 (by decide) (by decide), ← hp]; exact htr.prio + fault0 := by + have hf : tFaultA u = wa u 3376#u64 := rfl + rw [hf, hmu 3376#u64 (by decide) (by decide), ← hf]; exact htr.fault0 + faultT := by + have hf : tFaultA u = wa u 3376#u64 := rfl + rw [hf, hmu 3376#u64 (by decide) (by decide), ← hf]; exact htr.faultT + bound := by + have hb : tBoundA u = wa u 3368#u64 := rfl + rw [hb, hmu 3368#u64 (by decide) (by decide), ← hb]; exact htr.bound + faultR := by + unfold FaultRel at *; have hfr := htr.faultR + have h0 : L.mem s3 (tFaultA u) = L.mem s1 (tFaultA u) := hmu 3376#u64 (by decide) (by decide) + have h1 : L.mem s3 (tFw0A u) = L.mem s1 (tFw0A u) := hmu 3384#u64 (by decide) (by decide) + have h2 : L.mem s3 (tFw1A u) = L.mem s1 (tFw1A u) := hmu 3392#u64 (by decide) (by decide) + have h3 : L.mem s3 (tFw2A u) = L.mem s1 (tFw2A u) := hmu 3400#u64 (by decide) (by decide) + have h4 : L.mem s3 (tLfA u) = L.mem s1 (tLfA u) := hmu 3408#u64 (by decide) (by decide) + have h5 : L.mem s3 (tLf0A u) = L.mem s1 (tLf0A u) := hmu 3416#u64 (by decide) (by decide) + have h6 : L.mem s3 (tLf1A u) = L.mem s1 (tLf1A u) := hmu 3424#u64 (by decide) (by decide) + rw [h0, h1, h2, h3, h4, h5, h6]; exact hfr } + ep := fun e he => by + obtain ⟨ep, hep, hrel⟩ := J1.ep.ep e he + have hne_p : specPtr e ≠ σ1.curThread := fun h => by rw [h, hk1] at hep; cases hep + have he3 := J1.iplaced.e.ep e he + have hdis := J1.iplaced.e.et e he t htZ + have hword : ∀ o : U64, o.val < 16 → o.val % 8 = 0 → L.mem s3 (wa e o) = L.mem s1 (wa e o) := fun o ho1 ho8 => + hmem_nonTcb _ (wa_al (by unfold Aligned; omega) ho8) + fun K hK1 hK2 heq => by + have hv := congrArg UScalar.val heq + rw [wa_valm, Nat.mod_eq_of_lt (by have := he3.2.2; omega), Notif.tfield_val hta (by omega)] at hv; omega + have hst_e : stOf (L.mem s3) e = stOf (L.mem s1) e := by + unfold stOf + have h8 : epTSA e = wa e 8#u64 := by unfold epTSA layout.ep.TAIL_STATE; rfl + rw [h8, hword 8#u64 (by decide) (by decide)] + have hq_e : qOf (L.mem s3) e = qOf (L.mem s1) e := by + unfold qOf + have h0 : epHeadA e = wa e 0#u64 := by unfold epHeadA layout.ep.HEAD; rfl + have h8 : epTSA e = wa e 8#u64 := by unfold epTSA layout.ep.TAIL_STATE; rfl + rw [h0, h8, hword 0#u64 (by decide) (by decide), hword 8#u64 (by decide) (by decide)] + have hqr : ∀ xs, QueueRel (L.mem s1) Z.T .Ep (qOf (L.mem s1) e) xs → + QueueRel (L.mem s3) Z.T .Ep (qOf (L.mem s3) e) xs := fun xs hq => by + rw [hq_e] + exact ⟨hq.nodup, hq.mem, hq.head, hq.last, hq.seg.congr fun y hy => + ⟨hmem_tcb_ne y (hq.mem y hy) 3504#u64 (by decide) (by decide) (Or.inr (by decide)), + hmem_tcb_ne y (hq.mem y hy) 3496#u64 (by decide) (by decide) (Or.inr (by decide))⟩⟩ + refine ⟨ep, by rw [hne_ct_other _ hne_p]; exact hep, ?_⟩ + cases ep with + | idleEP => exact ⟨by rw [hst_e]; exact hrel.1, hqr [] hrel.2⟩ + | sendEP q => + obtain ⟨h1, xs, hne, hmap, hqr1, hqo⟩ := hrel + refine ⟨by rw [hst_e]; exact h1, xs, hne, hmap, hqr xs hqr1, fun u hu => ?_⟩ + have hqu := hqo u hu + have hne_ut : u ≠ t := fun h => by + subst h; have hqs := hqu.1 + rcases hrun1 with hst_r | hst_r | hst_r <;> rw [hst_r] at hqs <;> + simp [sched.TS_RUNNING, sched.TS_RESTART, sched.TS_RUNNING_VM] at hqs + have huT : u ∈ Z.T := hqr1.mem u hu + exact ⟨by rw [hst_ne u huT hne_ut]; exact hqu.1, + by unfold tBlockA; rw [hmem_tcb_ne u huT 3336#u64 (by decide) (by decide) (Or.inr (by decide))]; exact hqu.2⟩ + | recvEP q => + obtain ⟨h1, xs, hne, hmap, hqr1, hqo⟩ := hrel + refine ⟨by rw [hst_e]; exact h1, xs, hne, hmap, hqr xs hqr1, fun u hu => ?_⟩ + have hqu := hqo u hu + have hne_ut : u ≠ t := fun h => by + subst h; have hqs := hqu.1 + rcases hrun1 with hst_r | hst_r | hst_r <;> rw [hst_r] at hqs <;> + simp [sched.TS_RUNNING, sched.TS_RESTART, sched.TS_RUNNING_VM] at hqs + have huT : u ∈ Z.T := hqr1.mem u hu + exact ⟨by rw [hst_ne u huT hne_ut]; exact hqu.1, + by unfold tBlockA; rw [hmem_tcb_ne u huT 3336#u64 (by decide) (by decide) (Or.inr (by decide))]; exact hqu.2⟩ + reply := fun r hr => by + obtain ⟨rp, hrp, hrel⟩ := J1.ep.reply r hr + have hne_p : specPtr r ≠ σ1.curThread := fun h => by rw [h, hk1] at hrp; cases hrp + have hr3 := J1.iplaced.e.rep r hr + have hdis := J1.iplaced.e.rt r hr t htZ + have h0 : L.mem s3 (Endp.rTcbA r) = L.mem s1 (Endp.rTcbA r) := by + have hrt_val : (Endp.rTcbA r).val = r.val := by + unfold Endp.rTcbA layout.reply.TCB; rw [wa_valP]; simp [UScalar.hBounds] + refine hmem_nonTcb _ (by have := hr3.1; unfold Aligned; rw [hrt_val]; omega) fun K hK1 hK2 heq => by + have hv := congrArg UScalar.val heq + rw [hrt_val, Notif.tfield_val hta (by omega)] at hv; omega + exact ⟨rp, by rw [hne_ct_other _ hne_p]; exact hrp, by rw [h0]; exact hrel⟩ + buf := fun bp hbp k hk' => by + have hb := J1.ep.buf bp hbp k hk' + have hbal := J1.iplaced.e.balign _ ⟨bp, hbp, k, hk', rfl⟩ + have hboff := J1.iplaced.e.bt _ ⟨bp, hbp, k, hk', rfl⟩ t htZ + rw [hmem_nonTcb _ hbal fun K hK1 hK2 heq => by + rw [heq, Notif.tfield_val hta (by omega)] at hboff; omega] + exact hb } + have hNtfn3 : Notif.NtfnStateRel N Z.T (L.mem s3) σ3 := by + refine + { tcb := fun u hu => by + obtain ⟨y, hy, htr⟩ := J1.ntfn.tcb u hu + by_cases e : u = t + · subst e; rw [hx1] at hy; cases hy + refine ⟨x3, hct ▸ hkh_ct, ?_⟩ + have htrE := (hEp3.at_tcb htZ (hct ▸ hkh_ct)) + exact + { st := by rw [L.mem_wr_same hst_al, ts_vals.1]; rfl + blk := fun _ h => by cases h + badge := by + have hbr : Notif.badgeRegA t = Endp.regA t (regIdxA .x0) := by + unfold Notif.badgeRegA Endp.regA regIdxA layout.reg.BADGE layout.reg.X0; rfl + rw [hbr]; exact htrE.regs .x0 + prio := by + have hp : prioA t = wa t 3448#u64 := rfl + rw [hp, hmem_tcb t htZ 3448#u64 (by decide) (by decide) (Or.inr (Or.inr (by decide))), ← hp]; exact htr.prio } + · have hne_p : specPtr u ≠ σ1.curThread := fun h => e (specPtr_inj (h.trans hct)) + have hmu : ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → L.mem s3 (wa u K) = L.mem s1 (wa u K) := + fun K h1 h2 => hmem_tcb u hu K h1 h2 (Or.inl e) + refine ⟨y, by rw [hne_ct_other _ hne_p]; exact hy, ?_⟩ + exact + { st := by rw [stA_eq, hmu 3328#u64 (by decide) (by decide), ← stA_eq]; exact htr.st + blk := fun n hn => by + have hb : tBlockA u = wa u 3336#u64 := rfl + rw [hb, hmu 3336#u64 (by decide) (by decide), ← hb]; exact htr.blk n hn + badge := by + have hbr : Notif.badgeRegA u = wa u 256#u64 := by + apply UScalar.eq_of_val_eq + have hu_a := J1.sr.core.tcbs u hu + have h0 : Notif.badgeRegA u = Endp.regA u layout.reg.BADGE := rfl + rw [h0, Endp.regA_val hu_a (by simp [layout.reg.BADGE, layout.reg.X0]), Notif.tfield_val hu_a (by decide)] + simp [layout.reg.BADGE, layout.reg.X0] + rw [hbr, hmu 256#u64 (by decide) (by decide), ← hbr]; exact htr.badge + prio := by + have hp : prioA u = wa u 3448#u64 := rfl + rw [hp, hmu 3448#u64 (by decide) (by decide), ← hp]; exact htr.prio } + ntfn := fun nf hnf => by + obtain ⟨nv, hnv, hrel⟩ := J1.ntfn.ntfn nf hnf + have hne_p : specPtr nf ≠ σ1.curThread := fun h => by rw [h, hk1] at hnv; cases hnv + have hnf3 := J1.iplaced.n.ntfn nf hnf + have hdis := J1.iplaced.n.disj nf hnf t htZ + have hword : ∀ o : U64, o.val < 64 → o.val % 8 = 0 → L.mem s3 (wa nf o) = L.mem s1 (wa nf o) := fun o ho1 ho8 => + hmem_nonTcb _ (Notif.nfield_aligned hnf3 ho1 ho8) + fun K hK1 hK2 heq => by + have hv := congrArg UScalar.val heq + rw [Notif.nfield_val hnf3 ho1, Notif.tfield_val hta (by omega)] at hv; omega + have hst_n : stOf (L.mem s3) nf = stOf (L.mem s1) nf := by + unfold stOf + have h8 : epTSA nf = wa nf 8#u64 := by unfold epTSA layout.ep.TAIL_STATE; rfl + rw [h8, hword 8#u64 (by decide) (by decide)] + have hq_n : qOf (L.mem s3) nf = qOf (L.mem s1) nf := by + unfold qOf + have h0 : epHeadA nf = wa nf 0#u64 := by unfold epHeadA layout.ep.HEAD; rfl + have h8 : epTSA nf = wa nf 8#u64 := by unfold epTSA layout.ep.TAIL_STATE; rfl + rw [h0, h8, hword 0#u64 (by decide) (by decide), hword 8#u64 (by decide) (by decide)] + have hqr : ∀ xs, QueueRel (L.mem s1) Z.T .Ep (qOf (L.mem s1) nf) xs → + QueueRel (L.mem s3) Z.T .Ep (qOf (L.mem s3) nf) xs := fun xs hq => by + rw [hq_n] + exact ⟨hq.nodup, hq.mem, hq.head, hq.last, hq.seg.congr fun y hy => + ⟨hmem_tcb y (hq.mem y hy) 3504#u64 (by decide) (by decide) (Or.inr (Or.inr (by decide))), + hmem_tcb y (hq.mem y hy) 3496#u64 (by decide) (by decide) (Or.inr (Or.inr (by decide)))⟩⟩ + have hnb : L.mem s3 (Notif.nBoundA nf) = L.mem s1 (Notif.nBoundA nf) := by + have hb : Notif.nBoundA nf = wa nf 24#u64 := by unfold Notif.nBoundA layout.ntfn.BOUND_TCB; rfl + rw [hb, hword 24#u64 (by decide) (by decide)] + have hns : L.mem s3 (Notif.nScA nf) = L.mem s1 (Notif.nScA nf) := by + have hs : Notif.nScA nf = wa nf 32#u64 := by unfold Notif.nScA layout.ntfn.SC; rfl + rw [hs, hword 32#u64 (by decide) (by decide)] + refine ⟨nv, by rw [hne_ct_other _ hne_p]; exact hnv, ?_⟩ + refine + { obj := by + have hobj := hrel.obj + cases ho : nv.ntfnObj with + | idleNtfn => rw [ho] at hobj; exact ⟨by rw [hst_n]; exact hobj.1, hqr [] hobj.2⟩ + | activeNtfn b => + rw [ho] at hobj + refine ⟨by rw [hst_n]; exact hobj.1, hqr [] hobj.2.1, ?_⟩ + have hb : Notif.nBadgeA nf = wa nf 16#u64 := by unfold Notif.nBadgeA layout.ntfn.BADGE; rfl + rw [hb, hword 16#u64 (by decide) (by decide), ← hb]; exact hobj.2.2 + | waitingNtfn q => + rw [ho] at hobj + obtain ⟨h1, xs, hne, hmap, hqr1, hqo⟩ := hobj + refine ⟨by rw [hst_n]; exact h1, xs, hne, hmap, hqr xs hqr1, fun u hu => ?_⟩ + have hqu := hqo u hu + have hne_ut : u ≠ t := fun h => by + subst h; have hqs := hqu.1 + rcases hrun1 with hst_r | hst_r | hst_r <;> rw [hst_r] at hqs <;> + simp [sched.TS_RUNNING, sched.TS_RESTART, sched.TS_RUNNING_VM] at hqs + have huT : u ∈ Z.T := hqr1.mem u hu + exact ⟨by rw [stA_eq, hmem_tcb u huT 3328#u64 (by decide) (by decide) (Or.inl hne_ut), ← stA_eq]; exact hqu.1, + by have hb : tBlockA u = wa u 3336#u64 := rfl + rw [hb, hmem_tcb u huT 3336#u64 (by decide) (by decide) (Or.inl hne_ut), ← hb]; exact hqu.2⟩ + bound := by rw [hnb]; exact hrel.bound + bound_mem := by rw [hnb]; exact hrel.bound_mem + sc := by rw [hns]; exact hrel.sc } } + have hRep3 : ReplyStackRelA (L.mem s3) Rp Z.C σ3 (scNameA m) stack := + J1.reply.frame + (fun r hr => by + have hr3 := J1.iplaced.e.rep r hr; have hdis := J1.iplaced.e.rt r hr t htZ + have hword : ∀ o : U64, o.val = 8 ∨ o.val = 16 → L.mem s3 (wa r o) = L.mem s1 (wa r o) := fun o ho => + hmem_nonTcb _ (wa_al (by unfold Aligned; omega) (by rcases ho with h | h <;> omega)) + fun K hK1 hK2 heq => by + have hv := congrArg UScalar.val heq + rw [wa_valm, Nat.mod_eq_of_lt (by have := hr3.2.2; rcases ho with h | h <;> omega), Notif.tfield_val hta (by omega)] at hv + rcases ho with h | h <;> omega + exact ⟨hword _ (Or.inr (by simp [layout.reply.NEXT])), hword _ (Or.inl (by simp [layout.reply.PREV]))⟩) + (fun c _ y k h => by + have hne : scNameA m c ≠ σ1.curThread := fun e => by rw [e, hk1] at h; cases h + exact ⟨y, k, by rw [hne_ct_other _ hne]; exact h, rfl⟩) + (fun r _ x' h => by + have hne : specPtr r ≠ σ1.curThread := fun e => by rw [e, hkh_ct] at h; cases h + exact ⟨x', by rwa [hne_ct_other _ hne] at h, rfl⟩) + have hct_ne_idle : ∀ c < n, m.idleThreadOf c ≠ σ1.curThread := fun c hc e => by + obtain ⟨xi, hki, hst⟩ := J1.sidle.f3 c hc + rw [e, hk1] at hki; cases hki; rw [hst] at hrun_sp; cases hrun_sp + have hct_ne_idleT : σ1.idleThread ≠ σ1.curThread := fun e => by + obtain ⟨xi, hki, hst⟩ := J1.sinv.idleTcb + rw [e, hk1] at hki; cases hki; rw [hst] at hrun_sp; cases hrun_sp + have hcapOf_eq : ∀ b, Fl4SpecA64.capOf (.tcb x3) b = Fl4SpecA64.capOf (.tcb x1) b := fun _ => rfl + have CF3 := CurFactsA.back (σ := σ1) (σ' := σ3) ⟨J1.sinv.zc, J1.sinv.rlsRun, J1.sinv.curScReady⟩ + (fun _ h => h) rfl rfl rfl + (fun p x' hx => by + by_cases e : p = σ1.curThread + · subst e; rw [hkh_ct] at hx; cases hx; exact ⟨x1, hk1, rfl⟩ + · rw [hne_ct_other _ e] at hx; exact ⟨x', hx, rfl⟩) + (fun p x hx _ => by + by_cases e : p = σ1.curThread + · subst e; exact ⟨x3, hkh_ct, fun _ => rfl⟩ + · exact ⟨x, by rw [hne_ct_other _ e]; exact hx, id⟩) + (fun y k hy => by + have hne : σ1.curSc ≠ σ1.curThread := fun e => by rw [e, hkh_ct] at hy; cases hy + rw [hne_ct_other _ hne] at hy; exact ⟨y, hy, fun a => ⟨a, id⟩⟩) + have hSinv3 : SInvXA σ3 m n := + { mc := J1.sinv.mc + view := J1.sinv.view + zc := CF3.1 + rlsRun := CF3.2.1 + curScReady := CF3.2.2 + faultSt := fun p x hx hf => by + by_cases e : p = σ1.curThread + · subst e; rw [hkh_ct] at hx; cases hx + have h0 := (J1.sinv.faultSt _ x1 hk1 hf).notRun + rw [h0] at hrun_sp; cases hrun_sp + · rw [hne_ct_other _ e] at hx; exact J1.sinv.faultSt p x hx hf + curTcb := ⟨x3, hkh_ct⟩ + idleTcb := by + obtain ⟨xi, hki, hst⟩ := J1.sinv.idleTcb + exact ⟨xi, by change σ3.kheap σ1.idleThread = _; rw [hne_ct_other _ hct_ne_idleT]; exact hki, hst⟩ + onlyIdle := fun p x hkx hst => by + by_cases e : p = σ1.curThread + · subst e; rw [hkh_ct] at hkx; cases hkx; cases hst + · rw [hne_ct_other _ e] at hkx; exact J1.sinv.onlyIdle p x hkx hst + idleSc := by + obtain ⟨xi, y, k, hki, hsc, hky, hst⟩ := J1.sinv.idleSc + have hne_sc : Fl4SpecA64.idleScPtr ≠ σ1.curThread := fun e => by rw [e, hk1] at hky; cases hky + exact ⟨xi, y, k, by change σ3.kheap σ1.idleThread = _; rw [hne_ct_other _ hct_ne_idleT]; exact hki, hsc, + by change σ3.kheap Fl4SpecA64.idleScPtr = _; rw [hne_ct_other _ hne_sc]; exact hky, hst⟩ + curSc := by + obtain ⟨y, k, hky⟩ := J1.sinv.curSc + have hne_sc : σ1.curSc ≠ σ1.curThread := fun e => by rw [e, hk1] at hky; cases hky + exact ⟨y, k, by change σ3.kheap σ1.curSc = _; rw [hne_ct_other _ hne_sc]; exact hky⟩ + ctQ := J1.sinv.ctQ + idleQ := J1.sinv.idleQ + rq := fun d p u hu => by + obtain ⟨x, hx, hr⟩ := J1.sinv.rq d p u hu + by_cases e : u = σ1.curThread + · subst e; exact ⟨x3, hkh_ct, rfl⟩ + · exact ⟨x, by rw [hne_ct_other _ e]; exact hx, hr⟩ + rls := fun u hu => by + obtain ⟨x, hx⟩ := J1.sinv.rls u hu + by_cases e : u = σ1.curThread + · subst e; exact ⟨x3, hkh_ct⟩ + · exact ⟨x, by rw [hne_ct_other _ e]; exact hx⟩ + rpos := fun p y k hky ha => by + have hne : p ≠ σ1.curThread := fun e => by rw [e, hkh_ct] at hky; cases hky + rw [hne_ct_other _ hne] at hky; exact J1.sinv.rpos p y k hky ha + irqN := + { node := by + have hct_ct : ∀ k q, capTableAt k q σ3 ↔ capTableAt k q σ1 := fun k q => by + unfold capTableAt + by_cases e : q = σ1.curThread + · subst e; simp [hkh_ct, hk1] + · simp [hne_ct_other _ e] + exact ⟨⟨J1.sinv.irqN.node.1.1, fun irq hi => (hct_ct 0 _).2 (J1.sinv.irqN.node.1.2 irq hi)⟩, + fun irq hi k hc => J1.sinv.irqN.node.2 irq hi k ((hct_ct k _).1 hc)⟩ + slot := fun irq hi o b a ho hcap => by + by_cases e : σ1.interruptIrqNode irq = σ1.curThread + · change σ3.kheap (σ1.interruptIrqNode irq) = some o at ho + rw [e, hkh_ct] at ho; cases ho + exact J1.sinv.irqN.slot irq hi (.tcb x1) b a (e ▸ hk1) (hcapOf_eq b ▸ hcap) + · change σ3.kheap (σ1.interruptIrqNode irq) = some o at ho + rw [hne_ct_other _ e] at ho; exact J1.sinv.irqN.slot irq hi o b a ho hcap + ref := fun p o b a ho hcap => by + by_cases e : p = σ1.curThread + · subst e; rw [hkh_ct] at ho; cases ho + exact J1.sinv.irqN.ref _ (.tcb x1) b a hk1 (hcapOf_eq b ▸ hcap) + · rw [hne_ct_other _ e] at ho; exact J1.sinv.irqN.ref p o b a ho hcap } } + have hSidle3 : SIdleA σ3 m n := + { f3 := fun c hc => by + obtain ⟨x, hx, hst⟩ := J1.sidle.f3 c hc + exact ⟨x, by rw [hne_ct_other _ (hct_ne_idle c hc)]; exact hx, hst⟩ + f4 := fun y k hky => by + have hne : Fl4SpecA64.idleScPtr ≠ σ1.curThread := fun e => by rw [e, hkh_ct] at hky; cases hky + rw [hne_ct_other _ hne] at hky; exact J1.sidle.f4 y k hky + f5 := fun p y k hky hst => by + have hne : p ≠ σ1.curThread := fun e => by rw [e, hkh_ct] at hky; cases hky + rw [hne_ct_other _ hne] at hky; exact J1.sidle.f5 p y k hky hst + f6 := fun sl => by + have h0 := J1.sidle.f6 sl + have hcap_eq : specCapAtA σ3 sl = specCapAtA σ1 sl := by + unfold specCapAtA + by_cases e : sl.1 = σ1.curThread + · rw [e, hkh_ct, hk1]; exact hcapOf_eq sl.2 + · rw [hne_ct_other _ e] + rw [hcap_eq]; exact h0 + f7 := fun y k hky => by + have hne : Fl4SpecA64.idleScPtr ≠ σ1.curThread := fun e => by rw [e, hkh_ct] at hky; cases hky + rw [hne_ct_other _ hne] at hky; exact J1.sidle.f7 y k hky + idleSlot := fun c hc => by + have h0 := J1.sidle.idleSlot c hc + unfold vtCapA at h0 ⊢; rw [hne_ct_other _ (hct_ne_idle c hc)]; exact h0 + idleBound := fun c hc => by + have h0 := J1.sidle.idleBound c hc + unfold vcpuOfA at h0 ⊢; rw [hne_ct_other _ (hct_ne_idle c hc)]; exact h0 } + have hRunVcpu3 : RunVcpuInv st Z.T s3 := fun u hu hne => by + have hr := hmem_tcb u hu 3672#u64 (by decide) (by decide) (Or.inr (Or.inr (by decide))) + unfold runVcpuA at hne; rw [hr] at hne + have h0 := J1.runVcpu u hu hne + by_cases e : u = t + · subst e; exact absurd h0 hnvm1 + · rw [stA_eq, hmem_tcb u hu 3328#u64 (by decide) (by decide) (Or.inl e), ← stA_eq]; exact h0 + have hBlkQ3 : BlkQueuedA Z.T E N (L.mem s3) σ3 := + { ep := fun u hu x hx e hb => by + by_cases hct_u : specPtr u = σ1.curThread + · rw [hct_u, hkh_ct] at hx; cases hx; simp [x3, epBlocked] at hb + · rw [hne_ct_other _ hct_u] at hx + obtain ⟨hmem, y, hy, hq⟩ := J1.blkQ.ep u hu x hx e hb + have hne_e : e ≠ σ1.curThread := fun h => by rw [h, hk1] at hy; cases hy + have hb_eq : L.mem s3 (Notif.tBlockA u) = L.mem s1 (Notif.tBlockA u) := by + have h0 : Notif.tBlockA u = wa u 3336#u64 := rfl + rw [h0, hmem_tcb u hu 3336#u64 (by decide) (by decide) (Or.inr (Or.inr (by decide)))] + exact ⟨hb_eq ▸ hmem, y, by rw [hne_ct_other _ hne_e]; exact hy, hq⟩ + ntfn := fun u hu x hx nf hb => by + by_cases hct_u : specPtr u = σ1.curThread + · rw [hct_u, hkh_ct] at hx; cases hx; cases hb + · rw [hne_ct_other _ hct_u] at hx + obtain ⟨hmem, y, q, hy, hobj, hq⟩ := J1.blkQ.ntfn u hu x hx nf hb + have hne_n : nf ≠ σ1.curThread := fun h => by rw [h, hk1] at hy; cases hy + have hb_eq : L.mem s3 (Notif.tBlockA u) = L.mem s1 (Notif.tBlockA u) := by + have h0 : Notif.tBlockA u = wa u 3336#u64 := rfl + rw [h0, hmem_tcb u hu 3336#u64 (by decide) (by decide) (Or.inr (Or.inr (by decide)))] + exact ⟨hb_eq ▸ hmem, y, q, by rw [hne_ct_other _ hne_n]; exact hy, hobj, hq⟩ } + have hScAct3 : ∀ p, ScActiveAtA σ1 p → ScActiveAtA σ3 p := fun p ⟨y, k, hy, ha⟩ => + have hne : p ≠ σ1.curThread := fun e => by rw [e, hk1] at hy; cases hy + ⟨y, k, by rw [hne_ct_other _ hne]; exact hy, ha⟩ + have hValid3 : SchedValidA σ3 := + { rq := fun d p u hu => by + obtain ⟨⟨x, sc, hx, hsc, ha⟩, hnr⟩ := J1.schedValid.rq d p u hu + refine ⟨?_, hnr⟩ + by_cases e : u = σ1.curThread + · subst e; rw [hk1] at hx; cases hx; exact ⟨x3, sc, hkh_ct, hsc, hScAct3 sc ha⟩ + · exact ⟨x, sc, by rw [hne_ct_other _ e]; exact hx, hsc, hScAct3 sc ha⟩ + act := fun ha hni => by + obtain ⟨⟨x, sc, hx, hsc, ha'⟩, hnr⟩ := J1.schedValid.act ha hni + rw [hk1] at hx; cases hx + exact ⟨⟨x3, sc, hkh_ct, hsc, hScAct3 sc ha'⟩, hnr⟩ + blk := fun u x sc hx hb hsc => by + by_cases e : u = σ1.curThread + · subst e; rw [hkh_ct] at hx; cases hx; cases hb + · rw [hne_ct_other _ e] at hx + exact hScAct3 sc (J1.schedValid.blk u x sc hx hb hsc) + rls := fun u hu => by + obtain ⟨x, sc, hx, hsc, ha⟩ := J1.schedValid.rls u hu + by_cases e : u = σ1.curThread + · subst e; rw [hk1] at hx; cases hx; exact ⟨x3, sc, hkh_ct, hsc, hScAct3 sc ha⟩ + · exact ⟨x, sc, by rw [hne_ct_other _ e]; exact hx, hsc, hScAct3 sc ha⟩ } + have hAc_iff : ∀ c ∈ H.scs, c ∈ Ac ↔ c ∈ Ac' := fun c hc => by + have hmΩc := J1.sys.ent.bxi.2.2.2.2.sub _ (J1.sys.ent.bxi.2.2.2.2.all _ hc).2.1 + have hrm_c : L.rmax s3 c = L.rmax s1 c := by + refine hmem_nonTcb _ (J1.sq.omega _ hmΩc).1 fun K hK1 hK2 he => ?_ + have hdis := (J1.sq.omega _ hmΩc).2.1 _ (J1.tcbs ▸ htZ) + rw [he, Notif.tfield_val hta (by omega)] at hdis; omega + rw [J1.sys.ent.bxi.2.2.2.2.act c hc, bxi'.2.2.2.2.act c hc, hrm_c] + have hbxi_Ac : BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s3 := + ⟨bxi'.1, + { bxi'.2.1 with + sact := fun u hu hsch hnz => by + have huZ : u ∈ Z.T := J1.tcbs ▸ hu + have hsc_eq : L.mem s3 (scA u) = L.mem s1 (scA u) := by + have h0 : scA u = wa u 3464#u64 := rfl + rw [h0, hmem_tcb u huZ 3464#u64 (by decide) (by decide) (Or.inr (Or.inr (by decide)))] + have hscC : L.mem s3 (scA u) ∈ H.scs := + J1.scs ▸ (hsc_eq ▸ J1.j.1.rest.scMem u huZ (hsc_eq ▸ hnz)) + exact (hAc_iff _ hscC).2 (bxi'.2.1.sact u hu hsch hnz) }, + bxi'.2.2.1, bxi'.2.2.2.1, + { bxi'.2.2.2.2 with + act := fun c hc => (hAc_iff c hc).trans (bxi'.2.2.2.2.act c hc) + spor := fun c hc h => (hAc_iff c hc).2 (bxi'.2.2.2.2.spor c hc h) + rep := fun c hc h => (hAc_iff c hc).2 (bxi'.2.2.2.2.rep c hc h) }⟩ + refine ⟨σ3, hspec, ?_⟩ + exact JStage3A.of_ipcStepA J1 wAll hHS hSSz hQk rfl (fun _ _ => rfl) + hbxi_Ac sysl symL us' (j7' σ3) hsr3 hEp3 hNtfn3 hRep3 hK + (fun u hu O hO hO8 => hmem_tcb u hu O (by rcases hO with h | h | h | h | h <;> omega) hO8 + (by rcases hO with h | h | h | h | h <;> [exact Or.inr (Or.inl (by omega)); exact Or.inr (Or.inr (by omega)); exact Or.inr (Or.inr (by omega)); exact Or.inr (Or.inr (by omega)); exact Or.inr (Or.inr (by omega))])) + hSinv3 hSidle3 hRunVcpu3 hBlkQ3 hValid3 + +/-! ### Phase 1 Arms (4 reg/IRQ labels): `JSyscallA → JStage3A` -/ + +/-- **Label 64 (`ARMVCPUAckVPPI`)** at a syscall entry (`JSyscallA → JStage3A`, Plan B `hpin`). -/ +theorem JSyscallA.vcpuAckVppiA {ar : machine.Arch A} + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) (hAK : VcpuArchKeepsA ar AR) + (hCs : ∀ c ∈ Z.Cs, c.val < Fl4SpecA64.fl4A64Platform.numNodes) (hCsN : ∀ c ∈ Z.Cs, c.val < n) + (h1 : Fl4SpecA64.fl4A64Platform.numNodes = 1) + (hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM) + {slot vc length buf : U64} {blk call don fp : Bool} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) + (hcap : capA st s slot = .Vcpu vc) (hvc : vc ∈ Z.V) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + {mi : MessageInfo} {sl : CslotPtr} {excaps : List (Fl4SpecA64.Cap × CslotPtr)} + {buffer : Option ObjRef} {args : List U64} + (hfp : VcpuFrontPinA Z.th σ slot mi sl (.archObjectCap (.vcpuCap (specPtr vc))) excaps buffer (args.map UScalar.bv)) + (hlbl : fl4LabelOf mi.miLabel = archL .armVCPUAckVPPI) + (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) + (hdec : syscall.decode_invocation st ar s a fl4_abi.label.ARMVCPUAckVPPI length slot (.Vcpu vc) + blk call don fp buf extra = ok (core.result.Result.Ok (), s3, a3)) : + ∃ (x : Fl4SpecA64.Tcb) (σ3 : Fl4SpecA64.State Unit) (Ac' : Set U64), + σ.kheap σ.curThread = some (.tcb x) ∧ + VcpuDecPinA call Z.th σ slot (.invokeArchObject (.vcpu (.ackVppi (specPtr vc)))) ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + (.ok [], σ3) ∈ ((fl4ArchPerformInvocation (Z := Unit) (.vcpu (.ackVppi (specPtr vc)))).run + (stUpdA σ σ.curThread x .restart)).1 ∧ + JStage3A st AR Z H OS U Ac' Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s3 a3 σ3 m := by + have J3 := J.toJStage3A + obtain ⟨Pt, Ub, Pp7, Tb7, Hr, Te, Kt, Lv7, Re, bits, KLv, W⟩ := J7A.unp J3.j7 + have hpl := J3.vcPlacedA W + have C := J.vcpuDecCtxA W hCs hCsN h1 hvc + have RS := J3.rsSiteA hext hslot hcov J.actW J.running J.cur + -- Unfold `syscall.decode_invocation` → `vcpu.decode_ack_vppi` + have hdec0 := hdec + unfold syscall.decode_invocation at hdec + rw [show sched.cur_thread st s = ok (L.curW s) from eq_of_spec_fd (sched.cur_thread.spec s), bind_ok] at hdec + dsimp only at hdec; unfold vcpu.decode at hdec + simp only [fl4_abi.label.ARMVCPUAckVPPI, fl4_abi.label.ARMVCPUSetTCB, fl4_abi.label.ARMVCPUInjectIRQ, + fl4_abi.label.ARMVCPUReadReg, fl4_abi.label.ARMVCPUWriteReg] at hdec + rw [if_neg (by decide), if_neg (by decide), if_neg (by decide), if_neg (by decide), if_pos trivial] at hdec + unfold vcpu.decode_ack_vppi at hdec + by_cases hlen1 : length < 1#u64 + · rw [if_pos hlen1] at hdec; simp [error.truncated, bind_ok] at hdec + rw [if_neg hlen1, show sched.cur_thread st s = ok (L.curW s) from eq_of_spec_fd (sched.cur_thread.spec s), bind_ok] at hdec + have hge1 : 1 ≤ args.length := by have : ¬ length.val < 1 := hlen1; omega + cases hargs_eq : args with + | nil => simp [hargs_eq] at hge1 + | cons a0 rest => + rw [hargs 0#u64 a0 (by simp [hargs_eq]), bind_ok, show layout.irq.TIMER_ID = 27#u64 from by unfold layout.irq.TIMER_ID; rfl] at hdec + by_cases h27 : a0 = 27#u64 + · subst h27 + simp only [bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] at hdec + obtain ⟨⟨s1, a1⟩, hst_eq, hs1_eq, hAR1⟩ := spec_ex_fd (stall_vcpu_localA hAK J3.j.1.ar vc C.hStallT C.hStallC) + dsimp only at hs1_eq hAR1; rw [hs1_eq] at hst_eq + rw [hst_eq, bind_ok_pairA] at hdec + have hvc_st : ∀ v ∈ Z.V, stA (L.curW s) ≠ v ∧ stA (L.curW s) ≠ wa v 24#u64 := fun v hv => + ⟨by rw [stA_eq_wa]; have := hpl.disj_tcb v hv _ J3.sr.curT 0#u64 3328#u64 (by simp) (by simp) + have h0 : wa v 0#u64 = v := by apply UScalar.eq_of_val_eq; rw [wa_valP]; simp + rw [h0] at this; exact this.symm, + by rw [stA_eq_wa]; exact (hpl.disj_tcb v hv _ J3.sr.curT 24#u64 3328#u64 (by simp) (by simp)).symm⟩ + obtain ⟨_, _, _, _, hpl_rs⟩ := RS.pl + obtain ⟨x, hkx, hst_ok, hst_spec, J1, -, -, -⟩ := + J3.of_restartCurA hpl_rs J3.symL RS.run hnvm RS.cur RS.vs hvc_st + rw [hst_ok, bind_ok] at hdec + unfold vcpu.VPPI_MASKED at hdec + simp only [lift_okA, bind_ok, L.read_eq, L.write_eq] at hdec + obtain ⟨-, rfl, rfl⟩ := Prod.mk.inj (okinj hdec) + obtain ⟨v, hkv, -, -⟩ := J3.j.2.1.obj vc hvc + have hne_vc_ct : specPtr vc ≠ σ.curThread := fun h => by rw [h, hkx] at hkv; cases hkv + have hkv1 : (stUpdA σ σ.curThread x .restart).kheap (specPtr vc) = some (.archObj (.vcpu v)) := by + unfold stUpdA setObjA; simp [Function.update, hne_vc_ct, hkv] + set v' : Fl4SpecA64.Vcpu := { v with vcpuVppiMasked := Function.update v.vcpuVppiMasked .vppiEventIRQ_VTimer false } + set σ3 := updKSc (stUpdA σ σ.curThread x .restart) (specPtr vc) (.archObj (.vcpu v')) + have hd_arch (cptr : CapRef) : (fl4ArchDecodeInvocation (Z := Unit) call (fl4LabelOf mi.miLabel) + ((27#u64 :: rest).map UScalar.bv) cptr sl (.vcpuCap (specPtr vc)) excaps).run σ = + ({(.ok (.vcpu (.ackVppi (specPtr vc))), σ)}, False) := by + rw [hlbl]; unfold fl4ArchDecodeInvocation + simp only [archL, ↓reduceIte, decodeFl4VcpuAckVppi, NonDetE.whenE, the] + simp [NonDetE.liftME] + have hpin : VcpuDecPinA call Z.th σ slot (.invokeArchObject (.vcpu (.ackVppi (specPtr vc)))) := + ⟨mi, sl, .archObjectCap (.vcpuCap (specPtr vc)), excaps, buffer, (27#u64 :: rest).map UScalar.bv, + hargs_eq ▸ hfp, fun cptr => fl4DecodeInvocation_vcpu_of_archA (by simpa [fl4ArchDecodeInvocation, hlbl] using hd_arch cptr)⟩ + have hperf : (.ok [], σ3) ∈ ((fl4ArchPerformInvocation (Z := Unit) (.vcpu (.ackVppi (specPtr vc)))).run + (stUpdA σ σ.curThread x .restart)).1 := by + unfold fl4ArchPerformInvocation NonDetE.liftE + change (Except.ok [], σ3) ∈ ((fl4PerformVcpuInv (.ackVppi (specPtr vc)) >>= _) _).1 + rw [nd_bind_oneSc (fl4PerformVcpuInv_ackVppi_eqA J1.curVcpuNone hkv1)] + exact Set.mem_singleton _ + obtain ⟨-, Ac', sysl', symL', bxi', us', j7'⟩ := + J.decodeFinA hA hM hPl hfm hslot hcov hcap hext hSC + (by simp [DecArmS1aEnt, DecArmQH]) (fun _ h => by cases h) (fun _ h => by cases h) + hdec0 σ3 m + have h16_al : Aligned (wa vc 16#u64) := aligned_wa (hpl.vc_al hvc) 16#u64 (by decide) + have hWvc : ∀ b, b = wa vc 16#u64 → VcPage Z.V b ∧ ∀ u ∈ Z.V, b ≠ u ∧ b ≠ wa u 24#u64 := by + rintro _ rfl + refine ⟨vcpuHdr_in_vcPage hvc (by decide) (by decide), fun u hu => ?_⟩ + have hwa0 : wa u 0#u64 = u := by apply UScalar.eq_of_val_eq; rw [wa_valP]; simp [UScalar.hBounds] + by_cases heq : u = vc + · subst heq + have h1' := hpl.same_vcpu_ne (o₁ := 16#u64) (o₂ := 0#u64) hu (by decide) (by decide) (by decide) + rw [hwa0] at h1' + exact ⟨h1', hpl.same_vcpu_ne (o₁ := 16#u64) (o₂ := 24#u64) hu (by decide) (by decide) (by decide)⟩ + · have h1' := hpl.disj_vcpu (ov₁ := 16#u64) (ov₂ := 0#u64) hvc hu (Ne.symm heq) (by decide) (by decide) + rw [hwa0] at h1' + exact ⟨h1', hpl.disj_vcpu (ov₁ := 16#u64) (ov₂ := 24#u64) hvc hu (Ne.symm heq) (by decide) (by decide)⟩ + have hsnap3 : VcpuStateSnapA Z.V (L.mem (L.wr (L.wr s (stA (L.curW s)) sched.TS_RESTART) (wa vc 16#u64) + (L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART) (wa vc 16#u64) &&& ~~~1#u64))) σ3 := by + set s2 := L.wr s (stA (L.curW s)) sched.TS_RESTART + set w16 := L.mem s2 (wa vc 16#u64) &&& ~~~1#u64 + refine ⟨fun u hu xu hkxu => ?_, fun u hu xu hkxu => ?_⟩ + · by_cases heq : u = vc + · subst heq + rw [updKSc_same] at hkxu; cases hkxu + have h1 := J1.vcSnap.snap u hu v hkv1 + refine ⟨fun i hi => ?_, ?_, ?_, ?_, ?_⟩ + · have hne : wa u 16#u64 ≠ wa u ⟨BitVec.ofNat 64 (64 + 8 * i)⟩ := + hpl.same_vcpu_ne (o₁ := 16#u64) (o₂ := ⟨BitVec.ofNat 64 (64 + 8 * i)⟩) hu (by decide) + (by change (BitVec.ofNat 64 _).toNat < 4096; rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)]; omega) + (fun e => by have hv := congrArg UScalar.val e; change 16 = (BitVec.ofNat 64 _).toNat at hv; rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)] at hv; omega) + rw [show wa u ⟨BitVec.ofNat 64 (64 + 8 * i)⟩ = vcpuRegWA u i from rfl, + mem_wr_neA h16_al (hpl.vcPage_al (vcpuRegWA_in_vcPage hu hi)) hne]; exact h1.1 i hi + · rw [L.mem_wr_same h16_al] + simp only [v', Function.update_self, Bool.false_eq_true, ↓reduceIte] + dsimp only [w16]; have hv0 := h1.2.1; rw [hv0] + cases v.vcpuVppiMasked .vppiEventIRQ_VTimer <;> decide + · have hne := hpl.same_vcpu_ne (o₁ := 16#u64) (o₂ := 32#u64) hu (by decide) (by decide) (by decide) + rw [mem_wr_neA h16_al (aligned_wa (hpl.vc_al hu) 32#u64 (by decide)) hne]; exact h1.2.2.1 + · have hne := hpl.same_vcpu_ne (o₁ := 16#u64) (o₂ := 40#u64) hu (by decide) (by decide) (by decide) + rw [mem_wr_neA h16_al (aligned_wa (hpl.vc_al hu) 40#u64 (by decide)) hne]; exact h1.2.2.2.1 + · have hne := hpl.same_vcpu_ne (o₁ := 16#u64) (o₂ := 8#u64) hu (by decide) (by decide) (by decide) + rw [mem_wr_neA h16_al (aligned_wa (hpl.vc_al hu) 8#u64 (by decide)) hne]; exact h1.2.2.2.2 + · rw [updKSc_other _ (fun h => heq (specPtr_inj h))] at hkxu + have h1 := J1.vcSnap.snap u hu xu hkxu + refine ⟨fun i hi => ?_, ?_, ?_, ?_, ?_⟩ + · have hne := hpl.disj_vcpu (ov₁ := 16#u64) (ov₂ := ⟨BitVec.ofNat 64 (64 + 8 * i)⟩) hvc hu (Ne.symm heq) (by decide) + (by change (BitVec.ofNat 64 _).toNat < 4096; rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)]; omega) + rw [show wa u ⟨BitVec.ofNat 64 (64 + 8 * i)⟩ = vcpuRegWA u i from rfl, + mem_wr_neA h16_al (hpl.vcPage_al (vcpuRegWA_in_vcPage hu hi)) hne]; exact h1.1 i hi + · have hne := hpl.disj_vcpu (ov₁ := 16#u64) (ov₂ := 16#u64) hvc hu (Ne.symm heq) (by decide) (by decide) + rw [mem_wr_neA h16_al (aligned_wa (hpl.vc_al hu) 16#u64 (by decide)) hne]; exact h1.2.1 + · have hne := hpl.disj_vcpu (ov₁ := 16#u64) (ov₂ := 32#u64) hvc hu (Ne.symm heq) (by decide) (by decide) + rw [mem_wr_neA h16_al (aligned_wa (hpl.vc_al hu) 32#u64 (by decide)) hne]; exact h1.2.2.1 + · have hne := hpl.disj_vcpu (ov₁ := 16#u64) (ov₂ := 40#u64) hvc hu (Ne.symm heq) (by decide) (by decide) + rw [mem_wr_neA h16_al (aligned_wa (hpl.vc_al hu) 40#u64 (by decide)) hne]; exact h1.2.2.2.1 + · have hne := hpl.disj_vcpu (ov₁ := 16#u64) (ov₂ := 8#u64) hvc hu (Ne.symm heq) (by decide) (by decide) + rw [mem_wr_neA h16_al (aligned_wa (hpl.vc_al hu) 8#u64 (by decide)) hne]; exact h1.2.2.2.2 + · by_cases heq : u = vc + · subst heq; rw [updKSc_same] at hkxu; cases hkxu; exact J1.vcSnap.mask u hu v hkv1 + · rw [updKSc_other _ (fun h => heq (specPtr_inj h))] at hkxu; exact J1.vcSnap.mask u hu xu hkxu + exact ⟨x, σ3, Ac', hkx, hpin, hst_spec, hperf, + J1.of_vcpuNonLinkWrA hvc hkv1 rfl rfl ((writesIn_wr h16_al).mono hWvc) hAR1 hsnap3 sysl' symL' bxi' us' j7'⟩ + · rw [if_pos (bne_iff_ne.2 h27)] at hdec; unfold error.invalid_arg at hdec; simp [bind_ok] at hdec + +/-- **Label 62 (`ARMVCPUReadReg`)** at a syscall entry (`JSyscallA → JStage3A`, Plan B `hpin`). -/ +theorem JSyscallA.vcpuReadRegA {ar : machine.Arch A} + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) (hP : VcpuPlatAgreeA ar) (hAK : VcpuArchKeepsA ar AR) + (hCs : ∀ c ∈ Z.Cs, c.val < Fl4SpecA64.fl4A64Platform.numNodes) (hCsN : ∀ c ∈ Z.Cs, c.val < n) + (h1 : Fl4SpecA64.fl4A64Platform.numNodes = 1) + (hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM) + {slot vc length buf : U64} {blk call don fp : Bool} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) + (hcap : capA st s slot = .Vcpu vc) (hvc : vc ∈ Z.V) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + {mi : MessageInfo} {sl : CslotPtr} {excaps : List (Fl4SpecA64.Cap × CslotPtr)} + {buffer : Option ObjRef} {args : List U64} + (hfp : VcpuFrontPinA Z.th σ slot mi sl (.archObjectCap (.vcpuCap (specPtr vc))) excaps buffer (args.map UScalar.bv)) + (hlbl : fl4LabelOf mi.miLabel = archL .armVCPUReadReg) + (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) + (hdec : syscall.decode_invocation st ar s a fl4_abi.label.ARMVCPUReadReg length slot (.Vcpu vc) + blk call don fp buf extra = ok (core.result.Result.Ok (), s3, a3)) : + ∃ (x : Fl4SpecA64.Tcb) (field : Nat) (σ2 σ3 : Fl4SpecA64.State Unit) (ws : List Fl4SpecA64.Data) (Ac' : Set U64), + σ.kheap σ.curThread = some (.tcb x) ∧ + VcpuDecPinA call Z.th σ slot (.invokeArchObject (.vcpu (.readReg (specPtr vc) field))) ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + (.ok ws, σ2) ∈ ((fl4ArchPerformInvocation (Z := Unit) (.vcpu (.readReg (specPtr vc) field))).run + (stUpdA σ σ.curThread x .restart)).1 ∧ + ((), σ3) ∈ ((do + Fl4Spec.NonDet.when call (Fl4SpecA64.replyFromKernel (Z := Unit) σ.curThread (0, ws)) + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .running) σ2).1 ∧ + JStage3A st AR Z H OS U Ac' Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s3 a3 σ3 m := by + have J3 := J.toJStage3A + obtain ⟨Pt, Ub, Pp7, Tb7, Hr, Te, Kt, Lv7, Re, bits, KLv, W⟩ := J7A.unp J3.j7 + have hpl := J3.vcPlacedA W + have C := J.vcpuDecCtxA W hCs hCsN h1 hvc + have RS := J3.rsSiteA hext hslot hcov J.actW J.running J.cur + have hdec0 := hdec + unfold syscall.decode_invocation at hdec + rw [show sched.cur_thread st s = ok (L.curW s) from eq_of_spec_fd (sched.cur_thread.spec s), bind_ok] at hdec + dsimp only at hdec; unfold vcpu.decode at hdec + simp only [fl4_abi.label.ARMVCPUReadReg, fl4_abi.label.ARMVCPUSetTCB, fl4_abi.label.ARMVCPUInjectIRQ] at hdec + rw [if_neg (by decide), if_neg (by decide), if_pos trivial] at hdec + unfold vcpu.decode_read_reg at hdec + by_cases hlen1 : length < 1#u64 + · rw [if_pos hlen1] at hdec; simp [error.truncated, bind_ok] at hdec + rw [if_neg hlen1, show sched.cur_thread st s = ok (L.curW s) from eq_of_spec_fd (sched.cur_thread.spec s), bind_ok] at hdec + have hge1 : 1 ≤ args.length := by have : ¬ length.val < 1 := hlen1; omega + cases hargs_eq : args with + | nil => simp [hargs_eq] at hge1 + | cons field rest => + rw [hargs 0#u64 field (by simp [hargs_eq]), bind_ok] at hdec + by_cases hge64 : field ≥ fl4_abi.consts.vcpu_reg.NUM + · rw [if_pos hge64] at hdec; unfold error.invalid_arg at hdec; simp [bind_ok] at hdec + rw [if_neg hge64, vcpu_num_lrs_eqA hP a, bind_ok] at hdec + have hf_lt64 : field.val < 64 := by + have : ¬ (64 ≤ field.val) := by unfold fl4_abi.consts.vcpu_reg.NUM at hge64; exact hge64 + omega + have hlr0 : fl4_abi.consts.vcpu_reg.VGIC_LR0.val = 48 := by unfold fl4_abi.consts.vcpu_reg.VGIC_LR0; rfl + have hlr_ok : ¬ (field.val ≥ vrVgicLr0 ∧ field.val - vrVgicLr0 ≥ fl4NumLrs) ∧ + (do + let (s1, a1) ← vcpu.stall_vcpu st ar s a vc + let i1 ← ar.vcpu_gic_version a1 + let val ← + if field = fl4_abi.consts.vcpu_reg.VGIC_VTR then do + let i2 ← ar.vcpu_vgic_vtr a1; vcpu.abi_vgic_vtr i2 (i1 = 3#u64) + else if i1 = 3#u64 then vcpu.reg_get st s1 vc field + else do let b ← vcpu.is_vgic_ap1r field; if b then ok 0#u64 else vcpu.reg_get st s1 vc field + let s2 ← sched.set_thread_state st s1 (L.curW s) sched.TS_RESTART + let s3 ← vcpu.reply1 st s2 (L.curW s) call val + ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s3, a1)) = + ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s3, a3) := by + by_cases hge48 : field ≥ fl4_abi.consts.vcpu_reg.VGIC_LR0 + · rw [if_pos hge48] at hdec + have hge48' : 48 ≤ field.val := by rw [← hlr0]; exact hge48 + rw [u64_sub_okA (by omega), bind_ok] at hdec + by_cases hge_lrs : (⟨BitVec.ofNat 64 (field.val - fl4_abi.consts.vcpu_reg.VGIC_LR0.val)⟩ : U64) ≥ ⟨BitVec.ofNat 64 fl4NumLrs⟩ + · rw [if_pos hge_lrs] at hdec; unfold error.invalid_arg at hdec; simp [bind_ok] at hdec + · rw [if_neg hge_lrs] at hdec + have hlt_lrs : field.val - vrVgicLr0 < fl4NumLrs := by + have h1' : ¬ (fl4NumLrs ≤ field.val - 48) := by + intro hle; apply hge_lrs + rw [hlr0] + change (BitVec.ofNat 64 fl4NumLrs).toNat ≤ (BitVec.ofNat 64 (field.val - 48)).toNat + rw [BitVec.toNat_ofNat, BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by have := fl4NumLrs_le_16; omega), + Nat.mod_eq_of_lt (by omega)]; exact hle + unfold vrVgicLr0; omega + exact ⟨fun h => by omega, hdec⟩ + · rw [if_neg hge48] at hdec + have hlt48 : field.val < 48 := by + have : ¬ (48 ≤ field.val) := by rw [← hlr0]; exact hge48 + omega + exact ⟨fun h => by unfold vrVgicLr0 at h; omega, hdec⟩ + obtain ⟨hlr_ok, hbody⟩ := hlr_ok + obtain ⟨⟨s1, a1⟩, hst_eq, hs1_eq, hAR1⟩ := spec_ex_fd (stall_vcpu_localA hAK J3.j.1.ar vc C.hStallT C.hStallC) + dsimp only at hs1_eq hAR1; rw [hs1_eq] at hst_eq + rw [hst_eq, bind_ok_pairA] at hbody + obtain ⟨g1, hg1, hgb1⟩ := hP.gic a1 + rw [hg1, bind_ok] at hbody + obtain ⟨v, hkv, -, -⟩ := J3.j.2.1.obj vc hvc + have hval_eval : (if field = fl4_abi.consts.vcpu_reg.VGIC_VTR then do + let i2 ← ar.vcpu_vgic_vtr a1; vcpu.abi_vgic_vtr i2 (g1 = 3#u64) + else if g1 = 3#u64 then vcpu.reg_get st s vc field + else do let b ← vcpu.is_vgic_ap1r field; if b then ok 0#u64 else vcpu.reg_get st s vc field) = + ok ⟨if field.val = vrVgicVtr then abiVgicVtr + else if ¬fl4GicV3 ∧ fl4IsVgicAp1r field.val then 0 + else fl4VcpuRegGet v field.val⟩ := by + have hget_f : vcpu.reg_get st s vc field = ok (L.mem s (vcpuRegWA vc field.val)) := by + have hfeq : field = ⟨BitVec.ofNat 64 field.val⟩ := by + apply UScalar.eq_of_val_eq; change field.val = (BitVec.ofNat 64 field.val).toNat + rw [BitVec.toNat_ofNat]; exact (Nat.mod_eq_of_lt (by omega)).symm + conv_lhs => rw [hfeq] + exact vcpu_reg_get_eqA s vc hf_lt64 + by_cases hvtr : field = fl4_abi.consts.vcpu_reg.VGIC_VTR + · subst hvtr + have h34 : fl4_abi.consts.vcpu_reg.VGIC_VTR.val = vrVgicVtr := by unfold fl4_abi.consts.vcpu_reg.VGIC_VTR vrVgicVtr; rfl + rw [if_pos rfl, if_pos h34, hP.vtr a1, bind_ok, + show (decide (g1 = 3#u64)) = fl4GicV3 from hgb1, vcpu_abi_vgic_vtr_eqA hP a1] + · have hne34 : field.val ≠ vrVgicVtr := fun h => hvtr (UScalar.eq_of_val_eq (h.trans (by unfold fl4_abi.consts.vcpu_reg.VGIC_VTR vrVgicVtr; rfl))) + rw [if_neg hvtr, if_neg hne34] + by_cases hg3 : g1 = 3#u64 + · have hfl : fl4GicV3 = true := by change Fl4SpecA64.config_ARM_GIC_V3 = true; rw [← hgb1, decide_eq_true hg3] + rw [if_pos hg3, if_neg (by simp [hfl]), hget_f] + congr 1; apply u64_eq_of_bv_eq; exact (J3.vcSnap.snap vc hvc v hkv).1 field.val hf_lt64 + · have hfl : fl4GicV3 = false := by change Fl4SpecA64.config_ARM_GIC_V3 = false; rw [← hgb1, decide_eq_false hg3] + rw [if_neg hg3, vcpu_is_vgic_ap1r_eqA field, bind_ok] + simp only [hfl, Bool.false_eq_true, not_false_eq_true, true_and] + by_cases hap : fl4IsVgicAp1r field.val = true + · rw [if_pos hap, if_pos hap]; rfl + · rw [if_neg hap, if_neg hap, hget_f] + congr 1; apply u64_eq_of_bv_eq; exact (J3.vcSnap.snap vc hvc v hkv).1 field.val hf_lt64 + rw [hval_eval, bind_ok] at hbody + have hvc_st : ∀ u ∈ Z.V, stA (L.curW s) ≠ u ∧ stA (L.curW s) ≠ wa u 24#u64 := fun u hu => + ⟨by rw [stA_eq_wa]; have := hpl.disj_tcb u hu _ J3.sr.curT 0#u64 3328#u64 (by simp) (by simp) + have h0 : wa u 0#u64 = u := by apply UScalar.eq_of_val_eq; rw [wa_valP]; simp + rw [h0] at this; exact this.symm, + by rw [stA_eq_wa]; exact (hpl.disj_tcb u hu _ J3.sr.curT 24#u64 3328#u64 (by simp) (by simp)).symm⟩ + obtain ⟨_, _, _, _, hpl_rs⟩ := RS.pl + obtain ⟨x, hkx, hst_ok, hst_spec, J1, -, -, -⟩ := + J3.of_restartCurA hpl_rs J3.symL RS.run hnvm RS.cur RS.vs hvc_st + rw [hst_ok, bind_ok, vcpu_reply1_eq_vspace] at hbody + set val : U64 := ⟨if field.val = vrVgicVtr then abiVgicVtr + else if ¬fl4GicV3 ∧ fl4IsVgicAp1r field.val then 0 + else fl4VcpuRegGet v field.val⟩ + obtain ⟨s3', hrep, -⟩ := spec_ex_fd (reply1_regs_eqA (st := st) (s1 := L.wr s (stA (L.curW s)) sched.TS_RESTART) + (t := L.curW s) (v := val) (J3.sr.core.tcbs _ J3.sr.curT) call) + have hst_al : Aligned (stA (L.curW s)) := by + rw [stA_eq_wa]; exact aligned_wa (hpl.al_t _ J3.sr.curT) 3328#u64 (by decide) + have hO0 := J3.toJEntA.stOffA RS.vc + have hcur1 : L.curW (L.wr s (stA (L.curW s)) sched.TS_RESTART) = L.curW s := by + change L.mem _ (L.nodeAt _ (L.cpu _) layout.node.CUR_THREAD) = L.curW s + rw [L.cpu_wr, nodeAt_ksb (L.ksb_wr _ _ _)] + exact mem_wr_neA hst_al (hpl.node_al (L.cpu s) layout.node.CUR_THREAD (by unfold layout.node.CUR_THREAD; decide)) + (hO0.node (L.cpu s) J3.j.1.sch.cpuCs layout.node.CUR_THREAD (by unfold layout.node.CUR_THREAD; decide)).symm + have hcur_inv1 : (L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART) (inqA (L.curW (L.wr s (stA (L.curW s)) sched.TS_RESTART))) &&& sched.Q_RELEASE) = 0#u64 ∧ + L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART) (scA (L.curW (L.wr s (stA (L.curW s)) sched.TS_RESTART))) ≠ 0#u64 ∧ + L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART) (scA (L.curW (L.wr s (stA (L.curW s)) sched.TS_RESTART))) ∈ Ac := by + rw [hcur1] + have hne_sc : stA (L.curW s) ≠ scA (L.curW s) := by + rw [scA]; exact (hO0.fld (L.curW s) J3.sr.curT 3464#u64 (by decide) (fun _ => by decide)).symm + have hne_iq : stA (L.curW s) ≠ inqA (L.curW s) := by + rw [inqA]; exact (hO0.fld (L.curW s) J3.sr.curT 3592#u64 (by decide) (fun _ => by decide)).symm + rw [show inqA (L.curW s) = wa (L.curW s) 3592#u64 from rfl, + show scA (L.curW s) = wa (L.curW s) 3464#u64 from rfl, + mem_wr_neA hst_al (aligned_wa (hpl.al_t _ J3.sr.curT) 3592#u64 (by decide)) hne_iq, + mem_wr_neA hst_al (aligned_wa (hpl.al_t _ J3.sr.curT) 3464#u64 (by decide)) hne_sc] + exact RS.cur + have hrep_ok : ∃ s3', vspace.reply1 st (L.wr s (stA (L.curW s)) sched.TS_RESTART) (L.curW s) call val = ok s3' := by + have h1' : vspace.reply1 st (L.wr s (stA (L.curW s)) sched.TS_RESTART) (L.curW s) call val ⦃ _ => True ⦄ := by + unfold vspace.reply1 + rw [hrep, bind_ok] + have hst_w := set_thread_state_run_wrA (st := st) (s := s3') (t := L.curW s) + (J3.sr.core.tcbs _ J3.sr.curT) + exact WP.spec_mono (P0.sched.set_thread_state.p0 _ _ _ _ L.total) fun _ _ => trivial + obtain ⟨s3'', h3'', -⟩ := spec_ex_fd h1' + exact ⟨s3'', h3''⟩ + obtain ⟨s3'', hrep_eq⟩ := hrep_ok + rw [hrep_eq, bind_ok] at hbody + obtain ⟨-, rfl, rfl⟩ := Prod.mk.inj (okinj hbody) + have hJ1_a1 : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) (L.wr s (stA (L.curW s)) sched.TS_RESTART) a3 + (stUpdA σ σ.curThread x .restart) m := + { J1 with j := ⟨{ J1.j.1 with ar := hAR1 }, J1.j.2⟩ } + have hrun1 : Runnable (L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART) (stA (L.curW (L.wr s (stA (L.curW s)) sched.TS_RESTART)))) := by + rw [hcur1, L.mem_wr_same hst_al]; exact Or.inr (Or.inl rfl) + have hk1_ct : (stUpdA σ σ.curThread x .restart).kheap (stUpdA σ σ.curThread x .restart).curThread = + some (.tcb { x with tcbState := .restart }) := by + unfold stUpdA setObjA; simp [Function.update] + have hfin : ∀ σ3, ∃ Ac', SysL st H Z.Cs OS ia ie s3 ∧ SymL st H Z.Cs s3 ∧ + BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac' s3 ∧ + Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s3 ∧ + J7A st (J7R2A st ar hPl.ipa) Z H OS s3 σ3 m := fun σ3 => by + obtain ⟨-, Ac', h1, h2, h3, h4, h5⟩ := J.decodeFinA hA hM hPl hfm hslot hcov hcap hext hSC + (by simp [DecArmS1aEnt, DecArmQZ2, DecArmKQ2, DecArmKQ]) (fun _ h => by cases h) (fun _ h => by cases h) + hdec0 σ3 m + exact ⟨Ac', h1, h2, h3, h4, h5⟩ + obtain ⟨Ac', sysl', symL', bxi', us', _⟩ := hfin σ + have hj7' : ∀ σ3, J7A st (J7R2A st ar hPl.ipa) Z H OS s3 σ3 m := fun σ3 => (hfin σ3).choose_spec.2.2.2.2 + obtain ⟨σ3, hrep_spec, hJ3'⟩ := hJ1_a1.of_reply1A hk1_ct hcur_inv1 hrun1 + (by rw [hcur1, L.mem_wr_same hst_al]; simp [sched.TS_RESTART, sched.TS_RUNNING_VM]) call val (by rw [hcur1]; exact hrep_eq) + sysl' symL' bxi' us' hj7' + have hd_arch (cptr : CapRef) : (fl4ArchDecodeInvocation (Z := Unit) call (fl4LabelOf mi.miLabel) + ((field :: rest).map UScalar.bv) cptr sl (.vcpuCap (specPtr vc)) excaps).run σ = + ({(.ok (.vcpu (.readReg (specPtr vc) field.val)), σ)}, False) := by + rw [hlbl]; unfold fl4ArchDecodeInvocation + simp only [archL, ↓reduceIte] + have hd' : (decodeFl4VcpuReadReg (Z := Unit) (specPtr vc) ((field :: rest).map UScalar.bv)).run σ = + ({(.ok (.readReg (specPtr vc) field.val), σ)}, False) := by + unfold decodeFl4VcpuReadReg + rw [ndE_whenE_falseA (by simp), ndE_whenE_falseA (by simp [the, vrNum]; omega), + ndE_whenE_falseA (by simpa [the] using hlr_ok)] + rfl + unfold NonDetE.liftME + change ((decodeFl4VcpuReadReg (Z := Unit) (specPtr vc) ((field :: rest).map UScalar.bv)).run >>= _) σ = _ + rw [nd_bind_oneSc hd']; rfl + have hpin : VcpuDecPinA call Z.th σ slot (.invokeArchObject (.vcpu (.readReg (specPtr vc) field.val))) := + ⟨mi, sl, .archObjectCap (.vcpuCap (specPtr vc)), excaps, buffer, (field :: rest).map UScalar.bv, + hargs_eq ▸ hfp, fun cptr => fl4DecodeInvocation_vcpu_of_archA (by simpa [fl4ArchDecodeInvocation, hlbl] using hd_arch cptr)⟩ + have hne_vc_ct : specPtr vc ≠ σ.curThread := fun h => by rw [h, hkx] at hkv; cases hkv + have hkv1 : (stUpdA σ σ.curThread x .restart).kheap (specPtr vc) = some (.archObj (.vcpu v)) := by + unfold stUpdA setObjA; simp [Function.update, hne_vc_ct, hkv] + have hperf : (.ok [val.bv], stUpdA σ σ.curThread x .restart) ∈ + ((fl4ArchPerformInvocation (Z := Unit) (.vcpu (.readReg (specPtr vc) field.val))).run + (stUpdA σ σ.curThread x .restart)).1 := by + unfold fl4ArchPerformInvocation NonDetE.liftE + change (Except.ok [val.bv], _) ∈ ((fl4PerformVcpuInv (.readReg (specPtr vc) field.val) >>= _) _).1 + rw [nd_bind_oneSc (fl4PerformVcpuInv_readReg_eqA field.val J1.curVcpuNone hkv1)] + exact Set.mem_singleton _ + exact ⟨x, field.val, stUpdA σ σ.curThread x .restart, σ3, [val.bv], Ac, + hkx, hpin, hst_spec, hperf, hrep_spec, hJ3'⟩ + +/-- **Label 63 (`ARMVCPUWriteReg`)** at a syscall entry (`JSyscallA → JStage3A`, Plan B `hpin`). -/ +theorem JSyscallA.vcpuWriteRegA {ar : machine.Arch A} + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) (hP : VcpuPlatAgreeA ar) (hAK : VcpuArchKeepsA ar AR) + (hCs : ∀ c ∈ Z.Cs, c.val < Fl4SpecA64.fl4A64Platform.numNodes) (hCsN : ∀ c ∈ Z.Cs, c.val < n) + (h1 : Fl4SpecA64.fl4A64Platform.numNodes = 1) + (hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM) + {slot vc length buf : U64} {blk call don fp : Bool} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) + (hcap : capA st s slot = .Vcpu vc) (hvc : vc ∈ Z.V) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + {mi : MessageInfo} {sl : CslotPtr} {excaps : List (Fl4SpecA64.Cap × CslotPtr)} + {buffer : Option ObjRef} {args : List U64} + (hfp : VcpuFrontPinA Z.th σ slot mi sl (.archObjectCap (.vcpuCap (specPtr vc))) excaps buffer (args.map UScalar.bv)) + (hlbl : fl4LabelOf mi.miLabel = archL .armVCPUWriteReg) + (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) + (hdec : syscall.decode_invocation st ar s a fl4_abi.label.ARMVCPUWriteReg length slot (.Vcpu vc) + blk call don fp buf extra = ok (core.result.Result.Ok (), s3, a3)) : + ∃ (x : Fl4SpecA64.Tcb) (field : Nat) (val : Fl4SpecA64.Data) (σ3 : Fl4SpecA64.State Unit) (Ac' : Set U64), + σ.kheap σ.curThread = some (.tcb x) ∧ + VcpuDecPinA call Z.th σ slot (.invokeArchObject (.vcpu (.writeReg (specPtr vc) field val))) ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + (.ok [], σ3) ∈ ((fl4ArchPerformInvocation (Z := Unit) (.vcpu (.writeReg (specPtr vc) field val))).run + (stUpdA σ σ.curThread x .restart)).1 ∧ + JStage3A st AR Z H OS U Ac' Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s3 a3 σ3 m := by + have J3 := J.toJStage3A + obtain ⟨Pt, Ub, Pp7, Tb7, Hr, Te, Kt, Lv7, Re, bits, KLv, W⟩ := J7A.unp J3.j7 + have hpl := J3.vcPlacedA W + have C := J.vcpuDecCtxA W hCs hCsN h1 hvc + have RS := J3.rsSiteA hext hslot hcov J.actW J.running J.cur + have hCs128 : ∀ c ∈ Z.Cs, c.val < 128 := by + obtain ⟨n0, -, hn128, hcn⟩ := J3.j.1.csN; intro c hc; have := (hcn c).1 hc; omega + have hdec0 := hdec + unfold syscall.decode_invocation at hdec + rw [show sched.cur_thread st s = ok (L.curW s) from eq_of_spec_fd (sched.cur_thread.spec s), bind_ok] at hdec + dsimp only at hdec; unfold vcpu.decode at hdec + simp only [fl4_abi.label.ARMVCPUWriteReg, fl4_abi.label.ARMVCPUSetTCB, fl4_abi.label.ARMVCPUInjectIRQ, + fl4_abi.label.ARMVCPUReadReg] at hdec + rw [if_neg (by decide), if_neg (by decide), if_neg (by decide), if_pos trivial] at hdec + unfold vcpu.decode_write_reg at hdec + by_cases hlen2 : length < 2#u64 + · rw [if_pos hlen2] at hdec; simp [error.truncated, bind_ok] at hdec + rw [if_neg hlen2, show sched.cur_thread st s = ok (L.curW s) from eq_of_spec_fd (sched.cur_thread.spec s), bind_ok] at hdec + have hge2 : 2 ≤ args.length := by have : ¬ length.val < 2 := hlen2; omega + cases hargs_eq : args with + | nil => simp [hargs_eq] at hge2 + | cons field rest => + cases rest with + | nil => simp [hargs_eq] at hge2 + | cons val rest2 => + rw [hargs 0#u64 field (by simp [hargs_eq]), bind_ok, + hargs 1#u64 val (by simp [hargs_eq]), bind_ok] at hdec + by_cases hge64 : field ≥ fl4_abi.consts.vcpu_reg.NUM + · rw [if_pos hge64] at hdec; unfold error.invalid_arg at hdec; simp [bind_ok] at hdec + rw [if_neg hge64, vcpu_is_readonly_reg_eqA field, bind_ok] at hdec + have hf_lt64 : field.val < 64 := by + have : ¬ (64 ≤ field.val) := by unfold fl4_abi.consts.vcpu_reg.NUM at hge64; exact hge64 + omega + by_cases hro : fl4VcpuRegReadOnly field.val = true + · rw [if_pos hro] at hdec; unfold error.invalid_arg at hdec; simp [bind_ok] at hdec + rw [if_neg hro, vcpu_num_lrs_eqA hP a, bind_ok] at hdec + obtain ⟨g, hg, hgb⟩ := hP.gic a + rw [hg, bind_ok] at hdec + have hvc_st : ∀ u ∈ Z.V, stA (L.curW s) ≠ u ∧ stA (L.curW s) ≠ wa u 24#u64 := fun u hu => + ⟨by rw [stA_eq_wa]; have := hpl.disj_tcb u hu _ J3.sr.curT 0#u64 3328#u64 (by simp) (by simp) + have h0 : wa u 0#u64 = u := by apply UScalar.eq_of_val_eq; rw [wa_valP]; simp + rw [h0] at this; exact this.symm, + by rw [stA_eq_wa]; exact (hpl.disj_tcb u hu _ J3.sr.curT 24#u64 3328#u64 (by simp) (by simp)).symm⟩ + obtain ⟨_, _, _, _, hpl_rs⟩ := RS.pl + obtain ⟨x, hkx, hst_ok, hst_spec, J1, -, -, -⟩ := + J3.of_restartCurA hpl_rs J3.symL RS.run hnvm RS.cur RS.vs hvc_st + obtain ⟨Pt1, Ub1, Pp1, Tb1, Hr1, Te1, Kt1, Lv1, Re1, bits1, KLv1, W1⟩ := J7A.unp J1.j7 + have hV1 := J1.vcpuRelA W1 hCsN + have hksFit1 : (L.ksb (L.wr s (stA (L.curW s)) sched.TS_RESTART)).val + ksSizeA ≤ 2 ^ 64 := J1.j.1.ksFit + have hIdle1 : ∀ c ∈ Z.Cs, L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART) + (L.nodeAt (L.wr s (stA (L.curW s)) sched.TS_RESTART) c layout.node.IDLE_THREAD) ∈ Z.T := fun c hc => + J1.j.1.idleT c hc + obtain ⟨v, hkv, -, -⟩ := J3.j.2.1.obj vc hvc + have hne_vc_ct : specPtr vc ≠ σ.curThread := fun h => by rw [h, hkx] at hkv; cases hkv + have hkv1 : (stUpdA σ σ.curThread x .restart).kheap (specPtr vc) = some (.archObj (.vcpu v)) := by + unfold stUpdA setObjA; simp [Function.update, hne_vc_ct, hkv] + have hfin_mk : ∀ σ3, ∃ Ac', SysL st H Z.Cs OS ia ie s3 ∧ SymL st H Z.Cs s3 ∧ + BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac' s3 ∧ + Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s3 ∧ + J7A st (J7R2A st ar hPl.ipa) Z H OS s3 σ3 m := fun σ3 => by + obtain ⟨-, Ac', h1, h2, h3, h4, h5⟩ := J.decodeFinA hA hM hPl hfm hslot hcov hcap hext hSC + (by simp [DecArmS1aEnt, DecArmQH]) (fun _ h => by cases h) (fun _ h => by cases h) + hdec0 σ3 m + exact ⟨Ac', h1, h2, h3, h4, h5⟩ + by_cases hge48 : field ≥ fl4_abi.consts.vcpu_reg.VGIC_LR0 + · -- LR branch (`field ≥ 48`) + rw [if_pos hge48] at hdec + have hlr0 : fl4_abi.consts.vcpu_reg.VGIC_LR0.val = 48 := by unfold fl4_abi.consts.vcpu_reg.VGIC_LR0; rfl + have hge48' : 48 ≤ field.val := by rw [← hlr0]; exact hge48 + rw [u64_sub_okA (by omega), bind_ok] at hdec + by_cases hge_lrs : (⟨BitVec.ofNat 64 (field.val - fl4_abi.consts.vcpu_reg.VGIC_LR0.val)⟩ : U64) ≥ ⟨BitVec.ofNat 64 fl4NumLrs⟩ + · rw [if_pos hge_lrs] at hdec; unfold error.invalid_arg at hdec; simp [bind_ok] at hdec + rw [if_neg hge_lrs, show (decide (g = 3#u64)) = fl4GicV3 from hgb, + vcpu_sanitize_vgic_lr_eqA val fl4GicV3, bind_ok] at hdec + cases hsan : sanitizeVgicLr val.bv fl4GicV3 with + | none => rw [hsan] at hdec; unfold error.invalid_arg at hdec; simp [bind_ok] at hdec + | some san => + simp only [hsan, Option.map_some] at hdec + obtain ⟨⟨s1, a1⟩, hst_eq, hs1_eq, hAR1⟩ := spec_ex_fd (stall_vcpu_localA hAK J3.j.1.ar vc C.hStallT C.hStallC) + dsimp only at hs1_eq hAR1; rw [hs1_eq] at hst_eq + rw [hst_eq, bind_ok_pairA] at hdec + simp only [hst_ok, bind_ok, show (decide (g = 3#u64)) = fl4GicV3 from hgb] at hdec + have hstep := vcpu_reg_sync_wrA hV1 hksFit1 hCs128 hIdle1 hvc hkv1 field hf_lt64 ⟨san⟩ + (fun h => by unfold vrVgicHcr at h; omega) true + simp only [↓reduceIte] at hstep + obtain ⟨s4, hrun_wr, hsnap4, hw4, hvt4, hext4⟩ := spec_ex_fd hstep + rw [← Aeneas.Std.bind_assoc] at hdec + rw [hrun_wr, bind_ok] at hdec + obtain ⟨-, rfl, rfl⟩ := Prod.mk.inj (okinj hdec) + set v2 := syncVgicStatus (fl4VcpuRegSet field.val san v) fl4NumLrs fl4GicV3 + set σ3 := updKSc (stUpdA σ σ.curThread x .restart) (specPtr vc) (.archObj (.vcpu v2)) + have hlt_lrs : ¬ (field.val ≥ vrVgicLr0 ∧ field.val - vrVgicLr0 ≥ fl4NumLrs) := by + intro h; apply hge_lrs + rw [hlr0] + change (BitVec.ofNat 64 fl4NumLrs).toNat ≤ (BitVec.ofNat 64 (field.val - 48)).toNat + rw [BitVec.toNat_ofNat, BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by have := fl4NumLrs_le_16; omega), + Nat.mod_eq_of_lt (by omega)]; unfold vrVgicLr0 at h; omega + have hd_arch (cptr : CapRef) : (fl4ArchDecodeInvocation (Z := Unit) call (fl4LabelOf mi.miLabel) + ((field :: val :: rest2).map UScalar.bv) cptr sl (.vcpuCap (specPtr vc)) excaps).run σ = + ({(.ok (.vcpu (.writeReg (specPtr vc) field.val san)), σ)}, False) := by + rw [hlbl]; unfold fl4ArchDecodeInvocation; simp only [archL, ↓reduceIte] + have hd' : (decodeFl4VcpuWriteReg (Z := Unit) (specPtr vc) ((field :: val :: rest2).map UScalar.bv)).run σ = + ({(.ok (.writeReg (specPtr vc) field.val san), σ)}, False) := by + unfold decodeFl4VcpuWriteReg + have hnot_bad : ¬ (field.val ≥ vrNum ∨ fl4VcpuRegReadOnly field.val = true) := by + rintro (h | h) + · unfold vrNum at h; omega + · exact hro h + have hnot_lr_oob : ¬ (field.bv.toNat - vrVgicLr0 ≥ fl4NumLrs) := + fun h => hlt_lrs ⟨hge48', h⟩ + rw [ndE_whenE_falseA (by simp), ndE_whenE_falseA (by simpa [the] using hnot_bad)] + rw [if_pos (by simpa [the, vrVgicLr0] using hge48')] + rw [ndE_whenE_falseA (by simpa [the] using hnot_lr_oob)] + simp only [List.map_cons, List.getElem?_cons_zero, List.getElem?_cons_succ, the, hsan]; rfl + unfold NonDetE.liftME + change ((decodeFl4VcpuWriteReg (Z := Unit) (specPtr vc) _).run >>= _) σ = _ + rw [nd_bind_oneSc hd']; rfl + have hpin : VcpuDecPinA call Z.th σ slot (.invokeArchObject (.vcpu (.writeReg (specPtr vc) field.val san))) := + ⟨mi, sl, .archObjectCap (.vcpuCap (specPtr vc)), excaps, buffer, (field :: val :: rest2).map UScalar.bv, + hargs_eq ▸ hfp, fun cptr => fl4DecodeInvocation_vcpu_of_archA (by simpa [fl4ArchDecodeInvocation, hlbl] using hd_arch cptr)⟩ + have hperf : (.ok [], σ3) ∈ ((fl4ArchPerformInvocation (Z := Unit) (.vcpu (.writeReg (specPtr vc) field.val san))).run + (stUpdA σ σ.curThread x .restart)).1 := by + unfold fl4ArchPerformInvocation NonDetE.liftE + change (Except.ok [], σ3) ∈ ((fl4PerformVcpuInv (.writeReg (specPtr vc) field.val san) >>= _) _).1 + have hpf := fl4PerformVcpuInv_writeReg_eqA field.val san J1.curVcpuNone hkv1 + have hnot_ap : ¬ (¬fl4GicV3 ∧ fl4IsVgicAp1r field.val) := fun h => by + have h2 := h.2; unfold fl4IsVgicAp1r vrVgicAp1r0 vrVgicAp1r3 at h2; simp at h2; omega + have hsync : field.val ≥ vrVgicLr0 ∨ field.val = vrVgicHcr ∨ field.val = vrVgicVmcr := Or.inl hge48' + simp only [hnot_ap, hsync, ↓reduceIte] at hpf + rw [nd_bind_oneSc hpf]; exact Set.mem_singleton _ + obtain ⟨Ac', sysl', symL', bxi', us', j7'⟩ := hfin_mk σ3 + exact ⟨x, field.val, san, σ3, Ac', hkx, hpin, hst_spec, hperf, + J1.of_vcpuNonLinkWrA hvc hkv1 hvt4 hext4 hw4 hAR1 hsnap4 sysl' symL' bxi' us' j7'⟩ + · -- Non-LR branch (`field < 48`) + rw [if_neg hge48] at hdec + have hlt48 : field.val < 48 := by + have : ¬ (48 ≤ field.val) := by unfold fl4_abi.consts.vcpu_reg.VGIC_LR0 at hge48; exact hge48 + omega + obtain ⟨⟨s1, a1⟩, hst_eq, hs1_eq, hAR1⟩ := spec_ex_fd (stall_vcpu_localA hAK J3.j.1.ar vc C.hStallT C.hStallC) + dsimp only at hs1_eq hAR1; rw [hs1_eq] at hst_eq + rw [hst_eq, bind_ok_pairA] at hdec + rw [hst_ok, bind_ok] at hdec + have hlt_lrs : ¬ (field.val ≥ vrVgicLr0 ∧ field.val - vrVgicLr0 ≥ fl4NumLrs) := fun h => by unfold vrVgicLr0 at h; omega + set hval_san : Fl4SpecA64.Word := + if field.val = vrHcr then sanitizeHcr val.bv + else if field.val = vrVgicHcr then sanitizeVgicHcr val.bv fl4GicV3 + else val.bv + have hsan_eval : (if field = fl4_abi.consts.vcpu_reg.HCR_EL2 then vcpu.sanitize_hcr val + else if field = fl4_abi.consts.vcpu_reg.VGIC_HCR then vcpu.sanitize_vgic_hcr val (g = 3#u64) + else ok val) = ok ⟨hval_san⟩ := by + by_cases hh : field = fl4_abi.consts.vcpu_reg.HCR_EL2 + · subst hh; rw [if_pos rfl, vcpu_sanitize_hcr_eqA] + have h26 : fl4_abi.consts.vcpu_reg.HCR_EL2.val = vrHcr := by unfold fl4_abi.consts.vcpu_reg.HCR_EL2 vrHcr; rfl + simp [hval_san, h26] + · have hh' : field.val ≠ vrHcr := fun e => + hh (UScalar.eq_of_val_eq (e.trans (by unfold fl4_abi.consts.vcpu_reg.HCR_EL2 vrHcr; rfl))) + rw [if_neg hh] + by_cases hvg : field = fl4_abi.consts.vcpu_reg.VGIC_HCR + · subst hvg; rw [if_pos rfl, show (decide (g = 3#u64)) = fl4GicV3 from hgb, vcpu_sanitize_vgic_hcr_eqA] + have h32 : fl4_abi.consts.vcpu_reg.VGIC_HCR.val = vrVgicHcr := by unfold fl4_abi.consts.vcpu_reg.VGIC_HCR vrVgicHcr; rfl + have hne26 : vrVgicHcr ≠ vrHcr := by unfold vrVgicHcr vrHcr; decide + simp [hval_san, h32, hne26] + · have hvg' : field.val ≠ vrVgicHcr := fun e => + hvg (UScalar.eq_of_val_eq (e.trans (by unfold fl4_abi.consts.vcpu_reg.VGIC_HCR vrVgicHcr; rfl))) + rw [if_neg hvg]; simp [hval_san, hh', hvg'] + have hhcr32 : field.val = vrVgicHcr → ucast 64 (ucast 32 hval_san) = hval_san := fun h => by + have hne_hcr : field.val ≠ vrHcr := by unfold vrVgicHcr vrHcr at *; omega + simp only [hval_san, hne_hcr, ↓reduceIte, h]; exact sanitizeVgicHcr_u32A val.bv fl4GicV3 + have hd_arch (cptr : CapRef) : (fl4ArchDecodeInvocation (Z := Unit) call (fl4LabelOf mi.miLabel) + ((field :: val :: rest2).map UScalar.bv) cptr sl (.vcpuCap (specPtr vc)) excaps).run σ = + ({(.ok (.vcpu (.writeReg (specPtr vc) field.val hval_san)), σ)}, False) := by + rw [hlbl]; unfold fl4ArchDecodeInvocation; simp only [archL, ↓reduceIte] + have hd' : (decodeFl4VcpuWriteReg (Z := Unit) (specPtr vc) ((field :: val :: rest2).map UScalar.bv)).run σ = + ({(.ok (.writeReg (specPtr vc) field.val hval_san), σ)}, False) := by + unfold decodeFl4VcpuWriteReg + have hnot_bad : ¬ (field.val ≥ vrNum ∨ fl4VcpuRegReadOnly field.val = true) := by + rintro (h | h) + · unfold vrNum at h; omega + · exact hro h + rw [ndE_whenE_falseA (by simp), ndE_whenE_falseA (by simpa [the] using hnot_bad)] + have hlt_bv : ¬ (field.bv.toNat ≥ vrVgicLr0) := by unfold vrVgicLr0; change ¬ (field.val ≥ 48); omega + simp only [List.map_cons, List.getElem?_cons_zero, List.getElem?_cons_succ, the, hlt_bv, ↓reduceIte] + rfl + unfold NonDetE.liftME + change ((decodeFl4VcpuWriteReg (Z := Unit) (specPtr vc) _).run >>= _) σ = _ + rw [nd_bind_oneSc hd']; rfl + have hpin : VcpuDecPinA call Z.th σ slot (.invokeArchObject (.vcpu (.writeReg (specPtr vc) field.val hval_san))) := + ⟨mi, sl, .archObjectCap (.vcpuCap (specPtr vc)), excaps, buffer, (field :: val :: rest2).map UScalar.bv, + hargs_eq ▸ hfp, fun cptr => fl4DecodeInvocation_vcpu_of_archA (by simpa [fl4ArchDecodeInvocation, hlbl] using hd_arch cptr)⟩ + -- Helper for the non-AP1R write body (shared by g=3 and g≠3 ∧ ¬ap1r) + have hwrite_body : ¬ (¬fl4GicV3 ∧ fl4IsVgicAp1r field.val) → + (do + let v0 ← if field = fl4_abi.consts.vcpu_reg.HCR_EL2 then vcpu.sanitize_hcr val + else if field = fl4_abi.consts.vcpu_reg.VGIC_HCR then vcpu.sanitize_vgic_hcr val (g = 3#u64) + else ok val + let s3' ← vcpu.reg_set st (L.wr s (stA (L.curW s)) sched.TS_RESTART) vc field v0 + if field = fl4_abi.consts.vcpu_reg.VGIC_HCR then do + let s4 ← vcpu.sync_vgic_status st s3' vc ⟨BitVec.ofNat 64 fl4NumLrs⟩ (g = 3#u64) + ok (core.result.Result.Ok (), s4, a1) + else if field = fl4_abi.consts.vcpu_reg.VGIC_VMCR then do + let s4 ← vcpu.sync_vgic_status st s3' vc ⟨BitVec.ofNat 64 fl4NumLrs⟩ (g = 3#u64) + ok (core.result.Result.Ok (), s4, a1) + else ok (core.result.Result.Ok (), s3', a1)) = + ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s3, a3) → + ∃ (σ3 : Fl4SpecA64.State Unit) (Ac' : Set U64), + (.ok [], σ3) ∈ ((fl4ArchPerformInvocation (Z := Unit) (.vcpu (.writeReg (specPtr vc) field.val hval_san))).run + (stUpdA σ σ.curThread x .restart)).1 ∧ + JStage3A st AR Z H OS U Ac' Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s3 a3 σ3 m := by + intro hnot_ap hb + rw [hsan_eval, bind_ok] at hb + set doSync : Bool := decide (field.val = vrVgicHcr ∨ field.val = vrVgicVmcr) + have hsync_eq : (field = fl4_abi.consts.vcpu_reg.VGIC_HCR ∨ field = fl4_abi.consts.vcpu_reg.VGIC_VMCR) ↔ doSync = true := by + unfold doSync vrVgicHcr vrVgicVmcr + constructor + · rintro (rfl | rfl) + · unfold fl4_abi.consts.vcpu_reg.VGIC_HCR; decide + · unfold fl4_abi.consts.vcpu_reg.VGIC_VMCR; decide + · intro h; simp only [decide_eq_true_eq] at h + rcases h with h | h + · exact Or.inl (UScalar.eq_of_val_eq (by unfold fl4_abi.consts.vcpu_reg.VGIC_HCR; exact h)) + · exact Or.inr (UScalar.eq_of_val_eq (by unfold fl4_abi.consts.vcpu_reg.VGIC_VMCR; exact h)) + have hstep := vcpu_reg_sync_wrA hV1 hksFit1 hCs128 hIdle1 hvc hkv1 field hf_lt64 ⟨hval_san⟩ hhcr32 doSync + have hbody_eq : (do + let s3' ← vcpu.reg_set st (L.wr s (stA (L.curW s)) sched.TS_RESTART) vc field ⟨hval_san⟩ + let s4 ← if doSync then vcpu.sync_vgic_status st s3' vc ⟨BitVec.ofNat 64 fl4NumLrs⟩ fl4GicV3 else ok s3' + ok (core.result.Result.Ok (), s4, a1)) = + ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s3, a3) := by + rw [show (decide (g = 3#u64)) = fl4GicV3 from hgb] at hb + by_cases h1' : field = fl4_abi.consts.vcpu_reg.VGIC_HCR + · have hds : doSync = true := hsync_eq.1 (Or.inl h1') + subst h1'; simp only [↓reduceIte] at hb; simp only [hds, ↓reduceIte]; exact hb + · by_cases h2' : field = fl4_abi.consts.vcpu_reg.VGIC_VMCR + · have hds : doSync = true := hsync_eq.1 (Or.inr h2') + subst h2'; simp only [h1', ↓reduceIte] at hb; simp only [hds, ↓reduceIte]; exact hb + · have hds : doSync = false := by + cases hd : doSync + · rfl + · exfalso; rcases hsync_eq.2 hd with e | e <;> contradiction + simp only [h1', h2', ↓reduceIte] at hb; simp only [hds, Bool.false_eq_true, ↓reduceIte, bind_ok]; exact hb + obtain ⟨s4, hrun_wr, hsnap4, hw4, hvt4, hext4⟩ := spec_ex_fd hstep + rw [← Aeneas.Std.bind_assoc] at hbody_eq + rw [hrun_wr, bind_ok] at hbody_eq + obtain ⟨-, rfl, rfl⟩ := Prod.mk.inj (okinj hbody_eq) + set v1 := fl4VcpuRegSet field.val hval_san v + set v2 := if doSync then syncVgicStatus v1 fl4NumLrs fl4GicV3 else v1 + set σ3 := updKSc (stUpdA σ σ.curThread x .restart) (specPtr vc) (.archObj (.vcpu v2)) + have hperf : (.ok [], σ3) ∈ ((fl4ArchPerformInvocation (Z := Unit) (.vcpu (.writeReg (specPtr vc) field.val hval_san))).run + (stUpdA σ σ.curThread x .restart)).1 := by + unfold fl4ArchPerformInvocation NonDetE.liftE + change (Except.ok [], σ3) ∈ ((fl4PerformVcpuInv (.writeReg (specPtr vc) field.val hval_san) >>= _) _).1 + have hpf := fl4PerformVcpuInv_writeReg_eqA field.val hval_san J1.curVcpuNone hkv1 + have hne_lr : ¬ (field.val ≥ vrVgicLr0) := by unfold vrVgicLr0; omega + simp only [hnot_ap, hne_lr, false_or, ↓reduceIte] at hpf + rw [nd_bind_oneSc hpf] + simp only [σ3, v2, v1, doSync, decide_eq_true_eq] + exact Set.mem_singleton _ + obtain ⟨Ac', sysl', symL', bxi', us', j7'⟩ := hfin_mk σ3 + exact ⟨σ3, Ac', hperf, J1.of_vcpuNonLinkWrA hvc hkv1 hvt4 hext4 hw4 hAR1 hsnap4 sysl' symL' bxi' us' j7'⟩ + by_cases hg3 : g = 3#u64 + · have hfl : fl4GicV3 = true := by change Fl4SpecA64.config_ARM_GIC_V3 = true; rw [← hgb, decide_eq_true hg3] + rw [if_pos hg3] at hdec + obtain ⟨σ3, Ac', hperf, hJ3'⟩ := hwrite_body (by simp [hfl]) hdec + exact ⟨x, field.val, hval_san, σ3, Ac', hkx, hpin, hst_spec, hperf, hJ3'⟩ + · have hfl : fl4GicV3 = false := by change Fl4SpecA64.config_ARM_GIC_V3 = false; rw [← hgb, decide_eq_false hg3] + rw [if_neg hg3, vcpu_is_vgic_ap1r_eqA field, bind_ok] at hdec + by_cases hap : fl4IsVgicAp1r field.val = true + · rw [if_pos hap] at hdec + obtain ⟨-, rfl, rfl⟩ := Prod.mk.inj (okinj hdec) + have hperf : (.ok [], stUpdA σ σ.curThread x .restart) ∈ + ((fl4ArchPerformInvocation (Z := Unit) (.vcpu (.writeReg (specPtr vc) field.val hval_san))).run + (stUpdA σ σ.curThread x .restart)).1 := by + unfold fl4ArchPerformInvocation NonDetE.liftE + change (Except.ok [], _) ∈ ((fl4PerformVcpuInv (.writeReg (specPtr vc) field.val hval_san) >>= _) _).1 + have hpf := fl4PerformVcpuInv_writeReg_eqA field.val hval_san J1.curVcpuNone hkv1 + simp only [hfl, Bool.false_eq_true, not_false_eq_true, hap, and_self, ↓reduceIte] at hpf + rw [nd_bind_oneSc hpf]; exact Set.mem_singleton _ + exact ⟨x, field.val, hval_san, stUpdA σ σ.curThread x .restart, Ac, hkx, hpin, hst_spec, hperf, + { J1 with j := ⟨{ J1.j.1 with ar := hAR1 }, J1.j.2⟩ }⟩ + · rw [if_neg hap] at hdec + obtain ⟨σ3, Ac', hperf, hJ3'⟩ := hwrite_body (by simp [hfl, hap]) hdec + exact ⟨x, field.val, hval_san, σ3, Ac', hkx, hpin, hst_spec, hperf, hJ3'⟩ + +/-- **Label 61 (`ARMVCPUInjectIRQ`)** at a syscall entry (`JSyscallA → JStage3A`, Plan B `hpin`). -/ +theorem JSyscallA.vcpuInjectIrqA {ar : machine.Arch A} + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) (hP : VcpuPlatAgreeA ar) (hAK : VcpuArchKeepsA ar AR) + (hCs : ∀ c ∈ Z.Cs, c.val < Fl4SpecA64.fl4A64Platform.numNodes) (hCsN : ∀ c ∈ Z.Cs, c.val < n) + (h1 : Fl4SpecA64.fl4A64Platform.numNodes = 1) + (hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM) + {slot vc length buf : U64} {blk call don fp : Bool} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) + (hcap : capA st s slot = .Vcpu vc) (hvc : vc ∈ Z.V) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + {mi : MessageInfo} {sl : CslotPtr} {excaps : List (Fl4SpecA64.Cap × CslotPtr)} + {buffer : Option ObjRef} {args : List U64} + (hfp : VcpuFrontPinA Z.th σ slot mi sl (.archObjectCap (.vcpuCap (specPtr vc))) excaps buffer (args.map UScalar.bv)) + (hlbl : fl4LabelOf mi.miLabel = archL .armVCPUInjectIRQ) + (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) + (hdec : syscall.decode_invocation st ar s a fl4_abi.label.ARMVCPUInjectIRQ length slot (.Vcpu vc) + blk call don fp buf extra = ok (core.result.Result.Ok (), s3, a3)) : + ∃ (x : Fl4SpecA64.Tcb) (idx : Nat) (lr : Fl4SpecA64.Data) (σ3 : Fl4SpecA64.State Unit) (Ac' : Set U64), + σ.kheap σ.curThread = some (.tcb x) ∧ + VcpuDecPinA call Z.th σ slot (.invokeArchObject (.vcpu (.injectIrq (specPtr vc) idx lr))) ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + (.ok [], σ3) ∈ ((fl4ArchPerformInvocation (Z := Unit) (.vcpu (.injectIrq (specPtr vc) idx lr))).run + (stUpdA σ σ.curThread x .restart)).1 ∧ + JStage3A st AR Z H OS U Ac' Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s3 a3 σ3 m := by + have J3 := J.toJStage3A + obtain ⟨Pt, Ub, Pp7, Tb7, Hr, Te, Kt, Lv7, Re, bits, KLv, W⟩ := J7A.unp J3.j7 + have hpl := J3.vcPlacedA W + have C := J.vcpuDecCtxA W hCs hCsN h1 hvc + have RS := J3.rsSiteA hext hslot hcov J.actW J.running J.cur + have hCs128 : ∀ c ∈ Z.Cs, c.val < 128 := by + obtain ⟨n0, -, hn128, hcn⟩ := J3.j.1.csN; intro c hc; have := (hcn c).1 hc; omega + have hdec0 := hdec + unfold syscall.decode_invocation at hdec + rw [show sched.cur_thread st s = ok (L.curW s) from eq_of_spec_fd (sched.cur_thread.spec s), bind_ok] at hdec + dsimp only at hdec; unfold vcpu.decode at hdec + simp only [fl4_abi.label.ARMVCPUInjectIRQ, fl4_abi.label.ARMVCPUSetTCB] at hdec + rw [if_neg (by decide), if_pos trivial] at hdec + unfold vcpu.decode_inject_irq at hdec + by_cases hlen1 : length < 1#u64 + · rw [if_pos hlen1] at hdec; simp [error.truncated, bind_ok] at hdec + rw [if_neg hlen1, show sched.cur_thread st s = ok (L.curW s) from eq_of_spec_fd (sched.cur_thread.spec s), bind_ok] at hdec + have hge1 : 1 ≤ args.length := by have : ¬ length.val < 1 := hlen1; omega + cases hargs_eq : args with + | nil => simp [hargs_eq] at hge1 + | cons mr0 rest => + rw [hargs 0#u64 mr0 (by simp [hargs_eq]), bind_ok] at hdec + have hsh16 : (mr0 >>> 16#i32) = ok ⟨mr0.bv >>> 16⟩ := u64_shr_okA mr0 16#i32 (by decide) (by decide) + have hsh24 : (mr0 >>> 24#i32) = ok ⟨mr0.bv >>> 24⟩ := u64_shr_okA mr0 24#i32 (by decide) (by decide) + have hsh32 : (mr0 >>> 32#i32) = ok ⟨mr0.bv >>> 32⟩ := u64_shr_okA mr0 32#i32 (by decide) (by decide) + obtain ⟨g, hg, hgb⟩ := hP.gic a + simp only [lift_okA, bind_ok, hsh16, hsh24, hsh32, vcpu_num_lrs_eqA hP a, hg] at hdec + set virq : U64 := mr0 &&& 65535#u64 + set priority : U64 := ⟨mr0.bv >>> 16⟩ &&& 255#u64 + set group : U64 := ⟨mr0.bv >>> 24⟩ &&& 255#u64 + set index : U64 := ⟨mr0.bv >>> 32⟩ &&& 255#u64 + by_cases hidx_ge : index ≥ ⟨BitVec.ofNat 64 fl4NumLrs⟩ + · rw [if_pos hidx_ge] at hdec; unfold error.range at hdec; simp [bind_ok] at hdec + rw [if_neg hidx_ge] at hdec + have hidx_lt : index.val < fl4NumLrs := by + have h1' : ¬ (fl4NumLrs ≤ index.val) := by + intro hle; apply hidx_ge + change (BitVec.ofNat 64 fl4NumLrs).toNat ≤ index.val + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by have := fl4NumLrs_le_16; omega)]; exact hle + omega + by_cases hgrp_gt : group > 1#u64 + · rw [if_pos hgrp_gt] at hdec; unfold error.range at hdec; simp [bind_ok] at hdec + rw [if_neg hgrp_gt] at hdec + have hgrp_ok : ¬ (group.bv > 1) := hgrp_gt + -- Collapse the `virq > 1019` branch split + have hbody_and_ok : ¬ (virq.bv > 1019 ∧ ¬ (fl4GicV3 = true ∧ virq.bv ≥ 8192)) ∧ + (do + let (s1, a1) ← vcpu.stall_vcpu st ar s a vc + let lr_reg ← lift (core.num.U64.wrapping_add fl4_abi.consts.vcpu_reg.VGIC_LR0 index) + let cur ← vcpu.reg_get st s1 vc lr_reg + let b ← vcpu.lr_is_empty cur (g = 3#u64) + if b then do + let lr ← + if g = 3#u64 then do + let i4 ← 1#u64 <<< 62#i32; let i5 ← lift (group &&& 1#u64); let i6 ← i5 <<< 60#i32 + let i7 ← lift (i4 ||| i6); let i8 ← lift (priority &&& 255#u64); let i9 ← i8 <<< 48#i32 + let i10 ← lift (i7 ||| i9); let i11 ← 1#u64 <<< 41#i32; let i12 ← lift (i10 ||| i11) + ok (i12 ||| virq) + else do + let i4 ← lift (group &&& 1#u64); let i5 ← i4 <<< 30#i32; let i6 ← 1#u64 <<< 28#i32 + let i7 ← lift (i5 ||| i6); let i8 ← lift (priority &&& 31#u64); let i9 ← i8 <<< 23#i32 + let i10 ← lift (i7 ||| i9); let i11 ← 1#u64 <<< 19#i32; let i12 ← lift (i10 ||| i11) + let i13 ← lift (virq &&& 1023#u64); ok (i12 ||| i13) + let s2 ← sched.set_thread_state st s1 (L.curW s) sched.TS_RESTART + let s3' ← vcpu.reg_set st s2 vc lr_reg lr + let s4 ← vcpu.sync_vgic_status st s3' vc ⟨BitVec.ofNat 64 fl4NumLrs⟩ (g = 3#u64) + ok (core.result.Result.Ok (), s4, a1) + else ok (core.result.Result.Err (error.Exc.Sys error.SysError.DeleteFirst), s1, a1)) = + ok (core.result.Result.Ok (), s3, a3) := by + by_cases hgt : virq > 1019#u64 + · rw [if_pos hgt] at hdec + by_cases hg3 : g = 3#u64 + · have hfl : fl4GicV3 = true := by change Fl4SpecA64.config_ARM_GIC_V3 = true; rw [← hgb, decide_eq_true hg3] + rw [if_pos hg3] at hdec + by_cases hlpi : virq ≥ 8192#u64 + · rw [if_pos hlpi] at hdec; exact ⟨fun h => h.2 ⟨hfl, hlpi⟩, by simpa only [lift_okA, bind_ok] using hdec⟩ + · rw [if_neg hlpi] at hdec; unfold error.range at hdec; simp [bind_ok] at hdec + · rw [if_neg hg3] at hdec; unfold error.range at hdec; simp [bind_ok] at hdec + · rw [if_neg hgt] at hdec; exact ⟨fun h => hgt h.1, by simpa only [lift_okA, bind_ok] using hdec⟩ + obtain ⟨hvirq_ok, hbody⟩ := hbody_and_ok + obtain ⟨⟨s1, a1⟩, hst_eq, hs1_eq, hAR1⟩ := spec_ex_fd (stall_vcpu_localA hAK J3.j.1.ar vc C.hStallT C.hStallC) + dsimp only at hs1_eq hAR1; rw [hs1_eq] at hst_eq + rw [hst_eq, bind_ok_pairA] at hbody + rw [lift_okA, bind_ok] at hbody + have hlrs16 := fl4NumLrs_le_16 + have hlr_reg_val : (wa fl4_abi.consts.vcpu_reg.VGIC_LR0 index).val = vrVgicLr0 + index.val := by + rw [wa_valP]; have h48 : fl4_abi.consts.vcpu_reg.VGIC_LR0.val = 48 := by unfold fl4_abi.consts.vcpu_reg.VGIC_LR0; rfl + rw [h48]; unfold vrVgicLr0; exact Nat.mod_eq_of_lt (by omega) + have hlr_lt64 : (wa fl4_abi.consts.vcpu_reg.VGIC_LR0 index).val < 64 := by + rw [hlr_reg_val]; unfold vrVgicLr0; omega + have hlr_eq : wa fl4_abi.consts.vcpu_reg.VGIC_LR0 index = ⟨BitVec.ofNat 64 (vrVgicLr0 + index.val)⟩ := by + apply UScalar.eq_of_val_eq; rw [hlr_reg_val] + change _ = (BitVec.ofNat 64 _).toNat + rw [BitVec.toNat_ofNat]; unfold vrVgicLr0; exact (Nat.mod_eq_of_lt (by omega)).symm + rw [hlr_eq, vcpu_reg_get_eqA s vc (by unfold vrVgicLr0; omega), bind_ok, + show (decide (g = 3#u64)) = fl4GicV3 from hgb, vcpu_lr_is_empty_eqA _ fl4GicV3, bind_ok] at hbody + obtain ⟨v, hkv, -, -⟩ := J3.j.2.1.obj vc hvc + have hcur_bv : (L.mem s (vcpuRegWA vc (vrVgicLr0 + index.val))).bv = + fl4VcpuRegGet v (vrVgicLr0 + index.val) := + (J3.vcSnap.snap vc hvc v hkv).1 (vrVgicLr0 + index.val) (by unfold vrVgicLr0; omega) + rw [hcur_bv] at hbody + by_cases hemp : lrIsEmpty (fl4VcpuRegGet v (vrVgicLr0 + index.val)) fl4GicV3 = true + · rw [if_pos hemp, vcpu_inject_lr_eqA virq priority group g hgb, bind_ok] at hbody + have hvc_st : ∀ u ∈ Z.V, stA (L.curW s) ≠ u ∧ stA (L.curW s) ≠ wa u 24#u64 := fun u hu => + ⟨by rw [stA_eq_wa]; have := hpl.disj_tcb u hu _ J3.sr.curT 0#u64 3328#u64 (by simp) (by simp) + have h0 : wa u 0#u64 = u := by apply UScalar.eq_of_val_eq; rw [wa_valP]; simp + rw [h0] at this; exact this.symm, + by rw [stA_eq_wa]; exact (hpl.disj_tcb u hu _ J3.sr.curT 24#u64 3328#u64 (by simp) (by simp)).symm⟩ + obtain ⟨_, _, _, _, hpl_rs⟩ := RS.pl + obtain ⟨x, hkx, hst_ok, hst_spec, J1, -, -, -⟩ := + J3.of_restartCurA hpl_rs J3.symL RS.run hnvm RS.cur RS.vs hvc_st + obtain ⟨Pt1, Ub1, Pp1, Tb1, Hr1, Te1, Kt1, Lv1, Re1, bits1, KLv1, W1⟩ := J7A.unp J1.j7 + have hV1 := J1.vcpuRelA W1 hCsN + have hksFit1 : (L.ksb (L.wr s (stA (L.curW s)) sched.TS_RESTART)).val + ksSizeA ≤ 2 ^ 64 := J1.j.1.ksFit + have hIdle1 : ∀ c ∈ Z.Cs, L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART) + (L.nodeAt (L.wr s (stA (L.curW s)) sched.TS_RESTART) c layout.node.IDLE_THREAD) ∈ Z.T := fun c hc => + J1.j.1.idleT c hc + have hne_vc_ct : specPtr vc ≠ σ.curThread := fun h => by rw [h, hkx] at hkv; cases hkv + have hkv1 : (stUpdA σ σ.curThread x .restart).kheap (specPtr vc) = some (.archObj (.vcpu v)) := by + unfold stUpdA setObjA; simp [Function.update, hne_vc_ct, hkv] + simp only [hst_ok, bind_ok] at hbody + set lrW := fl4InjectLr virq.bv priority.bv group.bv fl4GicV3 + have hstep := vcpu_reg_sync_wrA hV1 hksFit1 hCs128 hIdle1 hvc hkv1 + (wa fl4_abi.consts.vcpu_reg.VGIC_LR0 index) hlr_lt64 ⟨lrW⟩ + (fun h => by rw [hlr_reg_val] at h; unfold vrVgicLr0 vrVgicHcr at h; omega) true + simp only [↓reduceIte] at hstep + rw [hlr_reg_val, hlr_eq] at hstep + obtain ⟨s4, hrun_wr, hsnap4, hw4, hvt4, hext4⟩ := spec_ex_fd hstep + rw [← Aeneas.Std.bind_assoc] at hbody + rw [hrun_wr, bind_ok] at hbody + obtain ⟨-, rfl, rfl⟩ := Prod.mk.inj (okinj hbody) + set v2 := syncVgicStatus (fl4VcpuRegSet (vrVgicLr0 + index.val) lrW v) fl4NumLrs fl4GicV3 + set σ3 := updKSc (stUpdA σ σ.curThread x .restart) (specPtr vc) (.archObj (.vcpu v2)) + have hd_arch (cptr : CapRef) : (fl4ArchDecodeInvocation (Z := Unit) call (fl4LabelOf mi.miLabel) + ((mr0 :: rest).map UScalar.bv) cptr sl (.vcpuCap (specPtr vc)) excaps).run σ = + ({(.ok (.vcpu (.injectIrq (specPtr vc) index.val lrW)), σ)}, False) := by + rw [hlbl]; unfold fl4ArchDecodeInvocation; simp only [archL, ↓reduceIte] + have hd' : (decodeFl4VcpuInjectIrq (Z := Unit) (specPtr vc) ((mr0 :: rest).map UScalar.bv)).run σ = + ({(.ok (.injectIrq (specPtr vc) index.val lrW), σ)}, False) := by + unfold decodeFl4VcpuInjectIrq + have hni : ¬ (((mr0.bv >>> 32) &&& 0xff).toNat ≥ fl4NumLrs) := by change ¬ (index.val ≥ fl4NumLrs); omega + rw [ndE_whenE_falseA (by simp), ndE_whenE_falseA (by simpa [the] using hni), + ndE_whenE_falseA (by simpa [the, group] using hgrp_ok), + ndE_whenE_falseA (by simpa [the, virq] using hvirq_ok)] + have hidx_eq : ((the ((mr0 :: rest).map UScalar.bv)[0]? >>> 32) &&& 0xff).toNat = index.val := rfl + rw [ndE_liftE_bindA _ _ _ () (fl4VcpuFlush_noneA J3.curVcpuNone (specPtr vc)), + hidx_eq, + ndE_liftE_bindA _ _ _ (fl4VcpuRegGet v (vrVgicLr0 + index.val)) + (fl4VcpuGet_eqA (vrVgicLr0 + index.val) hkv)] + exact ndE_unlessE_trueA hemp _ _ _ + unfold NonDetE.liftME + change ((decodeFl4VcpuInjectIrq (Z := Unit) (specPtr vc) _).run >>= _) σ = _ + rw [nd_bind_oneSc hd']; rfl + have hpin : VcpuDecPinA call Z.th σ slot (.invokeArchObject (.vcpu (.injectIrq (specPtr vc) index.val lrW))) := + ⟨mi, sl, .archObjectCap (.vcpuCap (specPtr vc)), excaps, buffer, (mr0 :: rest).map UScalar.bv, + hargs_eq ▸ hfp, fun cptr => fl4DecodeInvocation_vcpu_of_archA (by simpa [fl4ArchDecodeInvocation, hlbl] using hd_arch cptr)⟩ + have hperf : (.ok [], σ3) ∈ ((fl4ArchPerformInvocation (Z := Unit) (.vcpu (.injectIrq (specPtr vc) index.val lrW))).run + (stUpdA σ σ.curThread x .restart)).1 := by + unfold fl4ArchPerformInvocation NonDetE.liftE + change (Except.ok [], σ3) ∈ ((fl4PerformVcpuInv (.injectIrq (specPtr vc) index.val lrW) >>= _) _).1 + rw [nd_bind_oneSc (fl4PerformVcpuInv_injectIrq_eqA index.val lrW hkv1)] + exact Set.mem_singleton _ + obtain ⟨-, Ac', sysl', symL', bxi', us', j7'⟩ := + J.decodeFinA hA hM hPl hfm hslot hcov hcap hext hSC + (by simp [DecArmS1aEnt, DecArmQH]) (fun _ h => by cases h) (fun _ h => by cases h) + hdec0 σ3 m + exact ⟨x, index.val, lrW, σ3, Ac', hkx, hpin, hst_spec, hperf, + J1.of_vcpuNonLinkWrA hvc hkv1 hvt4 hext4 hw4 hAR1 hsnap4 sysl' symL' bxi' us' j7'⟩ + · rw [if_neg hemp] at hbody; exact absurd (okinj hbody) (by simp) + +end arms + +end +end Fl4ProofsA64.P3 + diff --git a/proofs/Fl4ProofsA64/P3/Stage3T3ArmsVcpuRunA.lean b/proofs/Fl4ProofsA64/P3/Stage3T3ArmsVcpuRunA.lean new file mode 100644 index 000000000..ea35e059f --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3T3ArmsVcpuRunA.lean @@ -0,0 +1,3359 @@ +/- +P3 (aarch64, A64-P3 S4 Phase 2E): **Label 69 (`ARMVCPURun`) and unified VCPU decode dispatcher on +`JSyscallA → JStage3A`** (zero global `FrameMemRelA`; zero hand-rebuilt records; reuses +`JStage3TA.of_vcpuStepA`, `JStage3TA.of_schedWr`, `JStage3TA.of_schedWrN`, +`vcpu_write_state_from_frame_stage3TA`, `VcpuDelHookA.vcpu_complete_run_stage3TA`, +`JSyscallA.decodeFinA`, and `JStage3TA.toJA_vcpuA`). + +Eliminated premises (audited from `JSyscallA` + `BufNativeA`): +- `hBufNotS2` derived directly from `J.bufNative` (`EpBufA.BufNativeA`). +- `hCurSched` (`sc ≠ 0`, `rmax ≠ 0`, `Q_RELEASE = 0`, `∉ releaseQueue`) derived directly from + `J.cur` + `J.ent.bxi` + `J.sr`. +- `hCurNoRun` derived directly from `hnvm` + `J.runVcpu`. +- `hCurNotIdle` derived directly from `J.running` + `J.sidle`. +- `hNtfn` (`FinishNotificationA` + disjointness) derived directly from `J.ntfn` + `J.ent.bxi`. +-/ +import Fl4ProofsA64.P3.Stage3T3ArmsVcpu2A +import Fl4ProofsA64.P3.Stage3T3ArmsVcpuA +import Fl4ProofsA64.P3.VcpuRunBodyA +import Fl4ProofsA64.P3.VcpuCheckJA +import Fl4ProofsA64.P3.DelNtfnEntA +import Fl4ProofsA64.P3.DelNtfnLoopFrA +import Fl4ProofsA64.P3.Stage3IpcGlueA +import Fl4ProofsA64.P3.Stage3DelInA + +open Aeneas Aeneas.Std Result fl4_core_a64 + +set_option maxRecDepth 100000 + +namespace Fl4ProofsA64.P3 + +open Fl4Spec Fl4SpecA64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST +open Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Vcpu + +noncomputable section + +set_option maxRecDepth 100000 + +local notation "wa" => core.num.U64.wrapping_add + +section runHelpers + +variable {S A : Type} {st : machine.Store S} [L : LawfulStore st] + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J7 : JSlotA S} + {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +private theorem wa_zeroR (x : U64) : wa x 0#u64 = x := + UScalar.eq_of_val_eq (by rw [wa_valP]; change (x.val + 0) % 2 ^ 64 = x.val; simp [Nat.mod_eq_of_lt x.bv.isLt]) + +private theorem u64_eq_of_bv_eqR {a b : U64} (h : a.bv = b.bv) : a = b := by + cases a; cases b; simp at h; subst h; rfl + +private theorem lift_okR {α : Type} (v : α) : lift v = ok v := rfl + +private theorem frameSize_bits_eqR (sz : capability.FrameSize) : + ∃ bits : U64, capability.FrameSize.bits sz = ok bits ∧ bits.val = frameBitsA sz := by + cases sz + · exact ⟨12#u64, by unfold capability.FrameSize.bits; rfl, rfl⟩ + · exact ⟨21#u64, by unfold capability.FrameSize.bits; rfl, rfl⟩ + · exact ⟨30#u64, by unfold capability.FrameSize.bits; rfl, rfl⟩ + +private theorem frameBitsA_lt64R (sz : capability.FrameSize) : frameBitsA sz < 64 := by + cases sz <;> decide + +private theorem specPtr_al3R {x : U64} (h : Aligned x) : specPtr x &&& Fl4SpecA64.mask 3 = 0 := by + unfold Aligned at h + apply BitVec.eq_of_toNat_eq + have hm3 : (Fl4SpecA64.mask (w := 64) 3).toNat = 7 := by decide + have hp8 : pptrShiftA.toNat % 8 = 0 := by decide + unfold specPtr + rw [BitVec.toNat_and, BitVec.toNat_add, hm3] + have h3 := Nat.and_two_pow_sub_one_eq_mod ((x.val + pptrShiftA.toNat) % 2 ^ 64) 3 + simp only [Nat.reducePow, Nat.reduceSub] at h3 + change ((x.val + pptrShiftA.toNat) % 2 ^ 64) &&& 7 = 0 + rw [h3]; omega + +private theorem bv_clear_lo2_and3 (x : BitVec 64) : + ((x &&& ~~~(3 : BitVec 64)) ||| ((0 : BitVec 64) &&& (3 : BitVec 64))) &&& (3 : BitVec 64) = 0 := by + have h03 : ((0 : BitVec 64) &&& (3 : BitVec 64)) = 0#64 := by decide + have hor : ∀ y : BitVec 64, y ||| 0#64 = y := fun _ => BitVec.or_zero + have hn33 : (~~~(3 : BitVec 64) &&& (3 : BitVec 64)) = 0#64 := by decide + have ha0 : ∀ y : BitVec 64, y &&& 0#64 = 0#64 := fun _ => BitVec.and_zero + rw [h03, hor, BitVec.and_assoc, hn33, ha0]; rfl + +private theorem bv_clear_lo2_andnot3 (x : BitVec 64) : + ((x &&& ~~~(3 : BitVec 64)) ||| ((0 : BitVec 64) &&& (3 : BitVec 64))) &&& ~~~(3 : BitVec 64) = + x &&& ~~~(3 : BitVec 64) := by + have h03 : ((0 : BitVec 64) &&& (3 : BitVec 64)) = 0#64 := by decide + have hor : ∀ y : BitVec 64, y ||| 0#64 = y := fun _ => BitVec.or_zero + rw [h03, hor, BitVec.and_assoc, + show (~~~(3 : BitVec 64) &&& ~~~(3 : BitVec 64)) = ~~~(3 : BitVec 64) from by decide] + +private theorem do_ok_prod {α β γ} (x : α) (y : β) (f : α → β → Result γ) : + (do let (a, b) ← ok (x, y); f a b) = f x y := by + rw [bind_ok]; rfl + +private theorem prod_let2 {α β γ} (x : α) (y : β) (f : α → γ) : + (let (a, _) := (x, y); f a) = f x := rfl + +private theorem updKSc_idemR (σ : Fl4SpecA64.State Unit) (p : ObjRef) (o1 o2 : KernelObject) : + updKSc (updKSc σ p o1) p o2 = updKSc σ p o2 := by + rcases σ with ⟨⟨kh, cdt, orig, ct, it, ctm, tm, csc, rep, act, dl, di, dsi, dt, cd, irqN, irqS, rq, rls, archS, ms⟩, ex⟩ + simp only [updKSc]; congr 2; funext q; by_cases h : q = p <;> simp [h] + +private theorem updKSc_commR (σ : Fl4SpecA64.State Unit) {p1 p2 : ObjRef} (hne : p1 ≠ p2) + (o1 o2 : KernelObject) : + updKSc (updKSc σ p1 o1) p2 o2 = updKSc (updKSc σ p2 o2) p1 o1 := by + rcases σ with ⟨⟨kh, cdt, orig, ct, it, ctm, tm, csc, rep, act, dl, di, dsi, dt, cd, irqN, irqS, rq, rls, archS, ms⟩, ex⟩ + simp only [updKSc]; congr 2; funext q + by_cases h1 : q = p1 + · subst h1; simp [hne] + · by_cases h2 : q = p2 + · subst h2; simp [hne.symm] + · simp [h1, h2] + +private theorem badgeRegA_eq_256 (u : U64) : badgeRegA u = wa u 256#u64 := by + unfold badgeRegA layout.tcb.REGS layout.reg.BADGE layout.reg.X0 + apply UScalar.eq_of_val_eq + simp [core.num.U64.wrapping_add_val_eq, core.num.U64.wrapping_mul_val_eq, U64.size_def, U64.numBits] + +/-- Step 1 of `decode_run`: write `vc + 8 := cpu + 1` (`OWNER_CPU`) on `JStage3TA` (spec unchanged). -/ +theorem vcpu_set_owner_stage3TA {Ub : Set U64} [J7PpA st J7] [J7FrA st J7] + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + (R6 : VcpuRec6A st Z OS Bf s σ) + (hVcP : VcPlacedA Z OS Ub (L.ksb s)) + (hcpu128 : (L.cpu s).val < 128) + {vc : U64} (hvc : vc ∈ Z.V) : + let s1 := L.wr s (wa vc 8#u64) (wa (L.cpu s) 1#u64) + JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s1 a σ m ∧ + VcpuRec6A st Z OS Bf s1 σ ∧ + OSKeep st OS s s1 ∧ + WritesIn st s s1 (fun b => b = wa vc 8#u64) := by + intro s1 + have hal8 : Aligned (wa vc 8#u64) := aligned_wa (hVcP.vc_al hvc) 8#u64 (by decide) + have hw1 : WritesIn st s s1 (fun b => b = wa vc 8#u64) := + ⟨L.ksb_wr _ _ _, L.cpu_wr _ _ _, fun q hqa hnf => mem_wr_neA hal8 hqa (fun e => hnf e.symm)⟩ + have hw_no : WritesIn st s s1 (VcpuWrNoStA st Z OS s) := + hw1.mono (fun _ h => Or.inl ⟨vc, hvc, Or.inr (Or.inl h)⟩) + have osk : OSKeep st OS s s1 := osKeep_of_vcpuWrA J R6.vcOS R6.frOff hw_no + have hcapE : ∀ y ∈ OS, capA st s1 y = capA st s y := fun y hy => osk.capA hy + have hne_vc : ∀ v ∈ Z.V, ∀ o : U64, o.val < 64 → (v = vc → o.val ≠ 8) → wa vc 8#u64 ≠ wa v o := fun v hv o ho hne => by + by_cases hvv : v = vc + · subst hvv; exact hVcP.same_vcpu_ne hv (by decide) (by omega) + (fun e => hne rfl (by have := congrArg UScalar.val e; change 8 = o.val at this; omega)) + · exact hVcP.disj_vcpu hvc hv (Ne.symm hvv) (by decide) (by omega) + have hvc_eq : ∀ v ∈ Z.V, L.mem s1 v = L.mem s v ∧ L.mem s1 (wa v 24#u64) = L.mem s (wa v 24#u64) := fun v hv => + ⟨mem_wr_neA hal8 (hVcP.vc_al hv) (by simpa [wa_zeroR] using hne_vc v hv 0#u64 (by decide) (fun _ => by decide)), + mem_wr_neA hal8 (aligned_wa (hVcP.vc_al hv) 24#u64 (by decide)) (hne_vc v hv 24#u64 (by decide) (fun _ => by decide))⟩ + have ht_eq : ∀ t ∈ Z.T, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → L.mem s1 (wa t o) = L.mem s (wa t o) := fun t ht o ho ho8 => + mem_wr_neA hal8 (aligned_wa (hVcP.al_t t ht) o ho8) (hVcP.disj_tcb vc hvc t ht 8#u64 o (by decide) ho) + have hcore : VcpuCoreA (L := L) Z s1 σ := + ⟨fun v hv => by obtain ⟨x, hx, h1, h2⟩ := J.j.2.1.obj v hv; rw [(hvc_eq v hv).1, (hvc_eq v hv).2]; exact ⟨x, hx, h1, h2⟩, + J.j.2.1.back, + fun t ht => by + obtain ⟨x, hx, h1, h2⟩ := J.j.2.1.tcb t ht + refine ⟨x, hx, by rw [ht_eq t ht 3664#u64 (by decide) (by decide)]; exact h1, + by unfold runVcpuA at h2 ⊢; rw [ht_eq t ht 3672#u64 (by decide) (by decide)]; exact h2⟩, + fun y hy => by rw [capAtA_eq, hcapE y (J.j5.2.1 y hy), ← capAtA_eq]; exact J.j.2.1.cap y hy⟩ + have hvcRun : VcRunA st Z.T Z.V s1 := + ⟨fun t ht => by rw [ht_eq t ht 3664#u64 (by decide) (by decide)]; exact J.vcRun.tcb t ht, + fun v hv => by rw [(hvc_eq v hv).2]; exact J.vcRun.vcpu v hv, + fun v hv => by rw [(hvc_eq v hv).1]; exact J.vcRun.vtcb v hv, + fun t ht => by unfold runVcpuA; rw [ht_eq t ht 3672#u64 (by decide) (by decide)]; exact J.vcRun.run t ht, + fun t ht hnz => by + rw [ht_eq t ht 3664#u64 (by decide) (by decide)] at hnz + unfold runVcpuA; rw [ht_eq t ht 3672#u64 (by decide) (by decide)]; exact J.vcRun.disj t ht hnz⟩ + have hrunVcpu : RunVcpuInv st Z.T s1 := fun t ht hne => by + unfold runVcpuA at hne; rw [ht_eq t ht 3672#u64 (by decide) (by decide)] at hne + rw [stA_eq_wa, ht_eq t ht 3328#u64 (by decide) (by decide), ← stA_eq_wa]; exact J.runVcpu t ht hne + have hEpTcb : ∀ t ∈ Z.T, ∃ x, σ.kheap (specPtr t) = some (.tcb x) ∧ TcbRelE Rp (L.mem s1) t x := fun t ht => by + obtain ⟨x, hx, hr⟩ := J.ep.tcb t ht + refine ⟨x, hx, hr.frame (J.iplaced.e.tcb t ht) (fun b hb htf => ?_)⟩ + obtain ⟨o, ho, ho8, rfl⟩ : ∃ o : U64, o.val < 4096 ∧ o.val % 8 = 0 ∧ b = wa t o := by + have hfit := (J.iplaced.e.tcb t ht).fits + have hal_t := (J.iplaced.e.tcb t ht).align + have hlt4096 : b.val - t.val < 4096 := by + have hto := htf.2; unfold TOffN at hto + rcases hto with h | h | h | h | h | h | h | h | ⟨_, _⟩ | h | h | h | h | h | h <;> omega + have h1 : b.val - t.val < 2 ^ 64 := by omega + refine ⟨⟨BitVec.ofNat 64 (b.val - t.val)⟩, ?_, ?_, UScalar.eq_of_val_eq ?_⟩ + · change (BitVec.ofNat 64 _).toNat < 4096; rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt h1]; exact hlt4096 + · change (BitVec.ofNat 64 _).toNat % 8 = 0; rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt h1]; have := hb; omega + · rw [wa_valP]; change b.val = (t.val + (BitVec.ofNat 64 _).toNat) % 2 ^ 64 + have := htf.1 + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt h1, Nat.mod_eq_of_lt (by omega)]; omega + exact ht_eq t ht o ho ho8 + have hEpBuf : ∀ bp ∈ Bf, ∀ k : U64, k.val < 128 → + (L.mem s1 (bufW bp.1 k)).bv = loadWordAt σ.machineState.underlyingMemory (bufAddr bp.2 k.val) ∧ + (bufAddr bp.2 k.val).toNat % 8 = 0 := fun bp hbp k hk => by + have hbw : BufW Bf (bufW bp.1 k) := ⟨bp, hbp, k, hk, rfl⟩ + rw [mem_wr_neA hal8 (J.iplaced.e.balign _ hbw) (fun e => J.bufOff.vcw _ hbw vc hvc 8#u64 (by decide) (by decide) e.symm)] + exact J.ep.buf bp hbp k hk + have hSnap : VcpuStateSnapA Z.V (L.mem s1) σ := + VcpuStateSnapA.of_vcpuStepA J R6.snap hw_no (VcpuSpecStepA.refl H σ) (fun v hv => by + by_cases hvv : v = vc + · subst hvv; rw [L.mem_wr_same hal8, wa_valP] + have h1 : (1#u64 : U64).val = 1 := rfl; rw [h1, Nat.mod_eq_of_lt (by omega)]; omega + · rw [mem_wr_neA hal8 (aligned_wa (hVcP.vc_al hv) 8#u64 (by decide)) (hne_vc v hv 8#u64 (by decide) (fun h => absurd h hvv))] + obtain ⟨x, hx, -, -⟩ := J.j.2.1.obj v hv; exact (R6.snap.snap v hv x hx).2.2.2.2) + have J1 : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s1 a σ m := by + have h := JStage3TA.of_vcpuStepA J R6 hw_no (VcpuSpecStepA.refl H σ) J.j.1.ar + (fun b hb => hw_no.mem_eq (J.vsOk.al b hb) (utVs_off_vcpuWrA J hb)) + hcore hvcRun hrunVcpu hEpTcb hEpBuf + (fun v hv hd => J.deadExt.vcpu v hv (fun y hy => by rw [← hcapE y hy]; exact hd y hy)) + (fun t ht hd => by rw [ht_eq t ht 3664#u64 (by decide) (by decide)]; exact J.deadExt.tvcpu t ht (fun y hy => by rw [← hcapE y hy]; exact hd y hy)) + (fun v hv hd => by rw [(hvc_eq v hv).2]; exact J.deadExt.vcDeadRun v hv (fun y hy => by rw [← hcapE y hy]; exact hd y hy)) + (fun z hz u num hc => by + have hc0 : capA st s z = .Zombie u capability.ZOMBIE_TCB num := by rw [← hcapE z hz]; exact hc + rw [ht_eq u (J.tcbs ▸ J.zombieTcb z hz u num hc0) 3664#u64 (by decide) (by decide)] + exact J.zombieNoVcpu z hz u num hc0) + have hZ : ({ Z with hw := Z.hw } : XSetsA) = Z := by cases Z; rfl + rwa [hZ] at h + have R6_1 : VcpuRec6A st Z OS Bf s1 σ := + ⟨hSnap, + fun t ht hnz => by + rw [ht_eq t ht 3664#u64 (by decide) (by decide)] at hnz ⊢ + rw [(hvc_eq _ ((J.vcRun.tcb t ht).resolve_left hnz)).1]; exact R6.vcSym1 t ht hnz, + fun v hv hnz => by + rw [(hvc_eq v hv).2] at hnz ⊢; unfold runVcpuA + rw [ht_eq _ ((J.vcRun.vcpu v hv).resolve_left hnz) 3672#u64 (by decide) (by decide)]; exact R6.vcSym2 v hv hnz, + fun v hv hnz => by + rw [(hvc_eq v hv).1] at hnz ⊢ + rw [ht_eq _ ((J.vcRun.vtcb v hv).resolve_left hnz) 3664#u64 (by decide) (by decide)]; exact R6.vcBack v hv hnz, + fun t ht hnz => by + unfold runVcpuA at hnz ⊢; rw [ht_eq t ht 3672#u64 (by decide) (by decide)] at hnz ⊢ + have hr := J.vcRun.run t ht; unfold runVcpuA at hr + rw [(hvc_eq _ (hr.resolve_left hnz)).2]; exact R6.runBack t ht hnz, + R6.vcOS, + fun y hy p sz r mp ept asid va hc b hb => by + rw [hw1.1]; exact R6.frOff y hy p sz r mp ept asid va (by rwa [← hcapE y hy]) b hb, + R6.userFr.keepA hcapE (fun _ _ _ h => h), R6.frTh.keepA hcapE (fun _ _ e => e), R6.bfSpec⟩ + exact ⟨J1, R6_1, osk, hw1⟩ + +private theorem set_thread_state_running_vm_ipcA {ar : machine.Arch A} {nA : U64} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s a σ m) + {ct : U64} (hct : ct ∈ Z.T) {x : Fl4SpecA64.Tcb} + (hk : σ.kheap (specPtr ct) = some (.tcb x)) + (hRunC : Runnable (L.mem s (stA ct))) + (hv : sched.TS_RUNNING_VM.val = tsCodeA (.ext .runningVM)) + (hal_st : Aligned (stA ct)) (hta : TcbAddr ct) + (hTcbOff : ∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → + (u ≠ ct ∨ K ≠ 3328#u64) → L.mem (L.wr s (stA ct) sched.TS_RUNNING_VM) (wa u K) = L.mem s (wa u K)) + (hNonTcb : ∀ p k, H p = some k → k ≠ .tcb → (∀ z, k ≠ .other z) → + ∀ b, Aligned b → p.val ≤ b.val → b.val < p.val + 2 ^ k.bits → + L.mem (L.wr s (stA ct) sched.TS_RUNNING_VM) b = L.mem s b) : + let s_vm := L.wr s (stA ct) sched.TS_RUNNING_VM + let σ_vm := updKSc σ (specPtr ct) (.tcb { x with tcbState := .ext .runningVM }) + EpStateRel Z.T E Rp Bf (L.mem s_vm) σ_vm ∧ + NtfnStateRel N Z.T (L.mem s_vm) σ_vm ∧ + ReplyStackRelA (L.mem s_vm) Rp Z.C σ_vm (scNameA m) stack ∧ + BufCover st Z.T Bf s_vm σ_vm ∧ + IdleNoFaultA m σ_vm ∧ + McpRelA Z.T m (L.mem s_vm) σ_vm ∧ + YieldRelA Z.T Z.C m (L.mem s_vm) σ_vm := by + intro s_vm σ_vm + have hBufEq : ∀ bp ∈ Bf, ∀ k : U64, k.val < 128 → L.mem s_vm (bufW bp.1 k) = L.mem s (bufW bp.1 k) := fun bp hbp k hk => by + have hbw : BufW Bf (bufW bp.1 k) := ⟨bp, hbp, k, hk, rfl⟩ + refine mem_wr_neA hal_st (J.iplaced.e.balign _ hbw) (fun e => ?_) + have hbt := J.iplaced.e.bt _ hbw ct hct + have hv1 : (stA ct).val = ct.val + 3328 := by + have h3328 : (3328#u64 : U64).val = 3328 := rfl + rw [stA_eq, wa_valm, h3328]; exact Nat.mod_eq_of_lt (by have := hta.fits; omega) + rw [← e, hv1] at hbt; omega + have ep' : EpStateRel Z.T E Rp Bf (L.mem s_vm) σ_vm := by + refine ⟨fun e he => ?_, fun r hr => ?_, fun u hu => ?_, fun bp hbp k hk => by rw [hBufEq bp hbp k hk]; exact J.ep.buf bp hbp k hk⟩ + · obtain ⟨xe, hxe, hrel⟩ := J.ep.ep e he + have hepH : H e = some .ep := J.utIpc.ep e he + have hfit := J.sys.ent.heap.fits e .ep hepH; have hal := J.sys.ent.heap.align e .ep hepH + change e.val + 16 ≤ 2 ^ 64 at hfit; change e.val % 16 = 0 at hal + have h0 : L.mem s_vm e = L.mem s e := hNonTcb e .ep hepH (fun h => by cases h) (fun _ h => by cases h) e (by show e.val % 8 = 0; omega) (le_refl _) (by change e.val < e.val + 16; omega) + have h8 : L.mem s_vm (wa e 8#u64) = L.mem s (wa e 8#u64) := by + have h8v : (8#u64 : U64).val = 8 := rfl + have ev : (wa e 8#u64).val = e.val + 8 := by rw [wa_valm, h8v]; exact Nat.mod_eq_of_lt (by omega) + exact hNonTcb e .ep hepH (fun h => by cases h) (fun _ h => by cases h) _ (by show _ % 8 = 0; rw [ev]; omega) (by omega) (by change _ < e.val + 16; omega) + have hne_ep : specPtr e ≠ specPtr ct := fun h => by rw [h, hk] at hxe; cases hxe + refine ⟨xe, by rw [updKSc_other _ hne_ep]; exact hxe, ?_⟩ + exact hrel.frame (by unfold epHeadA layout.ep.HEAD; rwa [wa_zeroR]) (by unfold epTSA layout.ep.TAIL_STATE; exact h8) fun y hy hmem => by + have hny : y ≠ ct := fun ey => by + rcases ey.symm with rfl + cases xe with + | idleEP => cases hmem + | sendEP q => + unfold EpObjRel at hrel + obtain ⟨-, xs, -, hqm, -, hq⟩ := hrel + rw [hqm] at hmem + obtain ⟨u, hu_xs, hu_eq⟩ := List.mem_map.1 hmem + have huc := specPtr_inj hu_eq; subst huc + have hst4 := (hq u hu_xs).1 + rcases hRunC with h | h | h <;> (rw [h] at hst4; simp [sched.TS_RUNNING, sched.TS_RESTART, sched.TS_RUNNING_VM] at hst4) + | recvEP q => + unfold EpObjRel at hrel + obtain ⟨-, xs, -, hqm, -, hq⟩ := hrel + rw [hqm] at hmem + obtain ⟨u, hu_xs, hu_eq⟩ := List.mem_map.1 hmem + have huc := specPtr_inj hu_eq; subst huc + have hst3 := (hq u hu_xs).1 + rcases hRunC with h | h | h <;> (rw [h] at hst3; simp [sched.TS_RUNNING, sched.TS_RESTART, sched.TS_RUNNING_VM] at hst3) + exact ⟨by rw [stA_eq]; exact hTcbOff y hy 3328#u64 (by decide) (by decide) (Or.inl hny), + by unfold tBlockA; exact hTcbOff y hy 3336#u64 (by decide) (by decide) (Or.inr (by decide)), + ⟨by rw [prevA_eq']; exact hTcbOff y hy _ (by simp [prevOff]) (by simp [prevOff]) (Or.inr (by decide)), + by rw [nextA_eq']; exact hTcbOff y hy _ (by simp [nextOff]) (by simp [nextOff]) (Or.inr (by decide))⟩⟩ + · obtain ⟨xr, hxr, hrt⟩ := J.ep.reply r hr + have hrH : H r = some .reply := J.utIpc.reply r hr + have hal := J.sys.ent.heap.align r .reply hrH; change r.val % 32 = 0 at hal + have hne_r : specPtr r ≠ specPtr ct := fun h => by rw [h, hk] at hxr; cases hxr + have hrr : L.mem s_vm (Endp.rTcbA r) = L.mem s (Endp.rTcbA r) := by + have hr0 : Endp.rTcbA r = r := by unfold Endp.rTcbA layout.reply.TCB; exact wa_zeroR r + rw [hr0] + exact hNonTcb r .reply hrH (fun h => by cases h) (fun _ h => by cases h) r (by show r.val % 8 = 0; omega) (le_refl _) (by change r.val < r.val + 32; omega) + refine ⟨xr, by rw [updKSc_other _ hne_r]; exact hxr, by rw [hrr]; exact hrt⟩ + · obtain ⟨xu, hxu, hrel⟩ := J.ep.tcb u hu + by_cases eu : u = ct + · subst eu; rw [hk] at hxu; cases hxu + refine ⟨{ x with tcbState := .ext .runningVM }, updKSc_same _ _ _, ?_⟩ + refine ⟨(by rw [L.mem_wr_same hal_st]; exact hv), + (fun e h => by cases h), (fun e d h => by cases h), (fun e r d h => by cases h), (fun r h => by cases h), + (fun r => by + obtain ⟨hKr, hwa_r⟩ := regA_eq_wa_KA u r; have hlt := regIdxA_lt r + rw [hwa_r, hTcbOff u hu _ (by rw [hKr]; omega) (by rw [hKr]; omega) (Or.inr (fun e => by have := congrArg UScalar.val e; rw [hKr] at this; change _ = 3328 at this; omega)), ← hwa_r] + exact hrel.regs r), + (by rw [show prioA u = wa u 3448#u64 from rfl, hTcbOff u hu 3448#u64 (by decide) (by decide) (Or.inr (by decide))]; exact hrel.prio), + (by rw [show tFaultA u = wa u 3376#u64 from rfl, hTcbOff u hu 3376#u64 (by decide) (by decide) (Or.inr (by decide))]; exact hrel.fault0), + (by rw [show tFaultA u = wa u 3376#u64 from rfl, hTcbOff u hu 3376#u64 (by decide) (by decide) (Or.inr (by decide))]; exact hrel.faultT), + (by rw [show tBoundA u = wa u 3368#u64 from rfl, hTcbOff u hu 3368#u64 (by decide) (by decide) (Or.inr (by decide))]; exact hrel.bound), + hrel.faultR.congr + (hTcbOff u hu 3376#u64 (by decide) (by decide) (Or.inr (by decide))) + (hTcbOff u hu 3384#u64 (by decide) (by decide) (Or.inr (by decide))) + (hTcbOff u hu 3392#u64 (by decide) (by decide) (Or.inr (by decide))) + (hTcbOff u hu 3408#u64 (by decide) (by decide) (Or.inr (by decide))) + (hTcbOff u hu 3416#u64 (by decide) (by decide) (Or.inr (by decide))) + (hTcbOff u hu 3424#u64 (by decide) (by decide) (Or.inr (by decide))) + (hTcbOff u hu 3400#u64 (by decide) (by decide) (Or.inr (by decide)))⟩ + · refine ⟨xu, by rw [updKSc_other _ (fun e => eu (specPtr_inj e))]; exact hxu, + hrel.frame (J.iplaced.e.tcb u hu) (fun b hb htf => ?_)⟩ + have hfit := (J.iplaced.e.tcb u hu).fits; have hal_u := (J.iplaced.e.tcb u hu).align + have hlt4096 : b.val - u.val < 4096 := by + have hto := htf.2; unfold TOffN at hto + rcases hto with h | h | h | h | h | h | h | h | ⟨_, _⟩ | h | h | h | h | h | h <;> omega + have h1 : b.val - u.val < 2 ^ 64 := by omega + have hge := htf.1 + refine hTcbOff u hu ⟨BitVec.ofNat 64 (b.val - u.val)⟩ + (by change (BitVec.ofNat 64 _).toNat < 4096; rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt h1]; exact hlt4096) + (by change (BitVec.ofNat 64 _).toNat % 8 = 0; rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt h1]; have := hb; omega) + (Or.inl eu) |> fun h => by + rwa [show wa u ⟨BitVec.ofNat 64 (b.val - u.val)⟩ = b from UScalar.eq_of_val_eq (by + rw [wa_valP]; change (u.val + (BitVec.ofNat 64 _).toNat) % 2 ^ 64 = b.val + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt h1, Nat.mod_eq_of_lt (by omega)]; omega)] at h + have ntfn' : NtfnStateRel N Z.T (L.mem s_vm) σ_vm := by + refine ⟨fun nu hnu => ?_, fun u hu => ?_⟩ + · obtain ⟨xn, hxn, hrel⟩ := J.ntfn.ntfn nu hnu + have hnH : H nu = some .ntfn := J.utIpc.ntfn nu hnu + have hfit := J.sys.ent.heap.fits nu .ntfn hnH; have hal := J.sys.ent.heap.align nu .ntfn hnH + change nu.val + 64 ≤ 2 ^ 64 at hfit; change nu.val % 64 = 0 at hal + have hne_n : specPtr nu ≠ specPtr ct := fun h => by rw [h, hk] at hxn; cases hxn + refine ⟨xn, by rw [updKSc_other _ hne_n]; exact hxn, ?_⟩ + have hn_w : ∀ o : U64, o.val < 64 → o.val % 8 = 0 → L.mem s_vm (wa nu o) = L.mem s (wa nu o) := fun o ho ho8 => by + have ev : (wa nu o).val = nu.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact hNonTcb nu .ntfn hnH (fun h => by cases h) (fun _ h => by cases h) _ (by show _ % 8 = 0; rw [ev]; omega) (by omega) (by change _ < nu.val + 64; omega) + have hst_n : stOf (L.mem s_vm) nu = stOf (L.mem s) nu := by unfold stOf; rw [epTSA_eq nu, hn_w 8#u64 (by decide) (by decide)] + have hq_n : qOf (L.mem s_vm) nu = qOf (L.mem s) nu := by + unfold qOf; rw [show epHeadA nu = wa nu 0#u64 from by unfold epHeadA layout.ep.HEAD; rfl, hn_w 0#u64 (by decide) (by decide), + epTSA_eq nu, hn_w 8#u64 (by decide) (by decide)] + have hbd_n : L.mem s_vm (nBadgeA nu) = L.mem s (nBadgeA nu) := by rw [nBadgeA_eq]; exact hn_w 16#u64 (by decide) (by decide) + have hbt_n : L.mem s_vm (nBoundA nu) = L.mem s (nBoundA nu) := by rw [nBoundA_eq]; exact hn_w 24#u64 (by decide) (by decide) + have hsc_n : L.mem s_vm (nScA nu) = L.mem s (nScA nu) := by rw [nScA_eq]; exact hn_w 32#u64 (by decide) (by decide) + have ht_ep : ∀ t ∈ Z.T, L.mem s_vm (nextA .Ep t) = L.mem s (nextA .Ep t) ∧ L.mem s_vm (prevA .Ep t) = L.mem s (prevA .Ep t) := fun t ht => + ⟨by rw [nextEp_eq]; exact hTcbOff t ht 3496#u64 (by decide) (by decide) (Or.inr (by decide)), + by rw [prevEp_eq]; exact hTcbOff t ht 3504#u64 (by decide) (by decide) (Or.inr (by decide))⟩ + refine ⟨?_, (by rw [hbt_n]; exact hrel.bound), (by rw [hbt_n]; exact hrel.bound_mem), (by rw [hsc_n]; exact hrel.sc)⟩ + cases ho : xn.ntfnObj with + | idleNtfn => + have h0 := hrel.obj; rw [ho] at h0 + exact ⟨(by rw [hst_n]; exact h0.1), (by rw [hq_n]; exact ⟨h0.2.nodup, h0.2.mem, h0.2.head, h0.2.last, h0.2.seg.congr fun y hy => ⟨(ht_ep y (h0.2.mem y hy)).2, (ht_ep y (h0.2.mem y hy)).1⟩⟩)⟩ + | activeNtfn b => + have h0 := hrel.obj; rw [ho] at h0 + exact ⟨(by rw [hst_n]; exact h0.1), (by rw [hq_n]; exact ⟨h0.2.1.nodup, h0.2.1.mem, h0.2.1.head, h0.2.1.last, h0.2.1.seg.congr fun y hy => ⟨(ht_ep y (h0.2.1.mem y hy)).2, (ht_ep y (h0.2.1.mem y hy)).1⟩⟩), (by rw [hbd_n]; exact h0.2.2)⟩ + | waitingNtfn xs => + have h0 := hrel.obj; rw [ho] at h0 + obtain ⟨hs0, ys, hne_y, hxs, hqr, hblk_y⟩ := h0 + refine ⟨(by rw [hst_n]; exact hs0), ys, hne_y, hxs, (by rw [hq_n]; exact ⟨hqr.nodup, hqr.mem, hqr.head, hqr.last, hqr.seg.congr fun y hy => ⟨(ht_ep y (hqr.mem y hy)).2, (ht_ep y (hqr.mem y hy)).1⟩⟩), fun y hy => ?_⟩ + have hyT := hqr.mem y hy + have hb0 := hblk_y y hy + have hny : y ≠ ct := fun ey => by + rcases ey.symm with rfl + have hst5 := hb0.1 + rcases hRunC with h | h | h <;> (rw [h] at hst5; simp [sched.TS_RUNNING, sched.TS_RESTART, sched.TS_RUNNING_VM] at hst5) + exact ⟨(by rw [stA_eq_wa, hTcbOff y hyT 3328#u64 (by decide) (by decide) (Or.inl hny), ← stA_eq_wa]; exact hb0.1), + (by rw [tBlockA_eq, hTcbOff y hyT 3336#u64 (by decide) (by decide) (Or.inr (by decide)), ← tBlockA_eq]; exact hb0.2)⟩ + · obtain ⟨xu, hxu, hrel⟩ := J.ntfn.tcb u hu + by_cases eu : u = ct + · subst eu; rw [hk] at hxu; cases hxu + refine ⟨{ x with tcbState := .ext .runningVM }, updKSc_same _ _ _, + ⟨(by rw [L.mem_wr_same hal_st]; exact hv), + (fun e h => by cases h), + (by rw [badgeRegA_eq_256, hTcbOff u hu 256#u64 (by decide) (by decide) (Or.inr (by decide)), ← badgeRegA_eq_256]; exact hrel.badge), + (by rw [show prioA u = wa u 3448#u64 from rfl, hTcbOff u hu 3448#u64 (by decide) (by decide) (Or.inr (by decide))]; exact hrel.prio)⟩⟩ + · refine ⟨xu, by rw [updKSc_other _ (fun e => eu (specPtr_inj e))]; exact hxu, + ⟨(by rw [stA_eq, hTcbOff u hu 3328#u64 (by decide) (by decide) (Or.inl eu), ← stA_eq]; exact hrel.st), + (fun e he => by unfold tBlockA; rw [hTcbOff u hu 3336#u64 (by decide) (by decide) (Or.inr (by decide))]; exact hrel.blk e he), + (by rw [badgeRegA_eq_256, hTcbOff u hu 256#u64 (by decide) (by decide) (Or.inl eu), ← badgeRegA_eq_256]; exact hrel.badge), + (by rw [show prioA u = wa u 3448#u64 from rfl, hTcbOff u hu 3448#u64 (by decide) (by decide) (Or.inr (by decide))]; exact hrel.prio)⟩⟩ + have reply' : ReplyStackRelA (L.mem s_vm) Rp Z.C σ_vm (scNameA m) stack := by + refine J.reply.frame (fun r hr => ?_) + (fun c _ y k h => ⟨y, k, by rw [updKSc_other _ (fun e => by rw [e, hk] at h; cases h)]; exact h, rfl⟩) + (fun r _ y h => ⟨y, by + by_cases e : specPtr r = specPtr ct + · rw [e, updKSc_same] at h; cases h + · rwa [updKSc_other _ e] at h, rfl⟩) + have hrH : H r = some .reply := J.utIpc.reply r hr + have hfit := J.sys.ent.heap.fits r .reply hrH; have hal := J.sys.ent.heap.align r .reply hrH + change r.val + 32 ≤ 2 ^ 64 at hfit; change r.val % 32 = 0 at hal + have rw_r : ∀ o : U64, o.val < 32 → o.val % 8 = 0 → L.mem s_vm (wa r o) = L.mem s (wa r o) := fun o ho ho8 => by + have ev : (wa r o).val = r.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact hNonTcb r .reply hrH (fun h => by cases h) (fun _ h => by cases h) _ (by show _ % 8 = 0; rw [ev]; omega) (by omega) (by change _ < r.val + 32; omega) + exact ⟨rw_r _ (by simp [layout.reply.NEXT]) (by simp [layout.reply.NEXT]), + rw_r _ (by simp [layout.reply.PREV]) (by simp [layout.reply.PREV])⟩ + have bufCover' : BufCover st Z.T Bf s_vm σ_vm := by + intro u hu y b sb v hy hb hl + have hib : IbSame (L := L) s s_vm u := + ⟨hTcbOff u hu _ (by simp) (by simp) (Or.inr (by decide)), + by rw [sbWordA_eq]; exact hTcbOff u hu _ (by simp) (by simp) (Or.inr (by decide)), + by rw [sbWordA8_eq]; exact hTcbOff u hu _ (by simp) (by simp) (Or.inr (by decide))⟩ + rw [lookup_ipc_buffer_sameA hib] at hl + by_cases eu : u = ct + · subst eu; rw [updKSc_same] at hy; cases hy; exact J.bufCover u hu x b sb v hk hb hl + · rw [updKSc_other _ (fun e => eu (specPtr_inj e))] at hy; exact J.bufCover u hu y b sb v hy hb hl + have idleNoFault' : IdleNoFaultA m σ_vm := fun c hc' => by + obtain ⟨y, hy, hf⟩ := J.idleNoFault c hc' + by_cases e : m.idleThreadOf c = specPtr ct + · rw [e, hk] at hy; cases hy; exact ⟨{ x with tcbState := .ext .runningVM }, by rw [e, updKSc_same], hf⟩ + · exact ⟨y, by rw [updKSc_other _ e]; exact hy, hf⟩ + have mcp' : McpRelA Z.T m (L.mem s_vm) σ_vm := fun u hu => by + obtain ⟨y, hy, hm, hf⟩ := J.mcp u hu + have hmeq : L.mem s_vm (mcpA u) = L.mem s (mcpA u) := by unfold mcpA; exact hTcbOff u hu _ (by simp) (by simp) (Or.inr (by decide)) + have hfeq : L.mem s_vm (flagsA u) = L.mem s (flagsA u) := by unfold flagsA; exact hTcbOff u hu _ (by simp) (by simp) (Or.inr (by decide)) + by_cases eu : u = ct + · subst eu; rw [hk] at hy; cases hy + exact ⟨{ x with tcbState := .ext .runningVM }, updKSc_same _ _ _, by rw [hmeq]; exact hm, + fun hni => by have := hf hni; unfold FlagsOkA at this ⊢; rwa [hfeq]⟩ + · exact ⟨y, by rw [updKSc_other _ (fun e => eu (specPtr_inj e))]; exact hy, by rw [hmeq]; exact hm, + fun hni => by have := hf hni; unfold FlagsOkA at this ⊢; rwa [hfeq]⟩ + have yieldRel' : YieldRelA Z.T Z.C m (L.mem s_vm) σ_vm := by + have hye : ∀ u ∈ Z.T, L.mem s_vm (yieldA u) = L.mem s (yieldA u) := fun u hu => by + unfold yieldA; exact hTcbOff u hu _ (by simp) (by simp) (Or.inr (by decide)) + refine ⟨fun u hu => ?_, fun u hu h => ?_, fun c hcc y k h p hp => ?_⟩ + · obtain ⟨y, hy, e⟩ := J.yieldRel.tcb u hu + by_cases eu : u = ct + · subst eu; rw [hk] at hy; cases hy + exact ⟨{ x with tcbState := .ext .runningVM }, updKSc_same _ _ _, by rw [hye u hu]; exact e⟩ + · exact ⟨y, by rw [updKSc_other _ (fun e => eu (specPtr_inj e))]; exact hy, by rw [hye u hu]; exact e⟩ + · rw [hye u hu] at h ⊢ + obtain ⟨hC, y, k, hy, e⟩ := J.yieldRel.toSc u hu h + exact ⟨hC, y, k, by rw [updKSc_other _ (fun eq => by rw [eq, hk] at hy; cases hy)]; exact hy, e⟩ + · have h0 : σ.kheap (scNameA m c) = some (.schedContext y k) := by + by_cases e : scNameA m c = specPtr ct + · rw [e, updKSc_same] at h; cases h + · rwa [updKSc_other _ e] at h + obtain ⟨u, hu, e1, e2⟩ := J.yieldRel.frm c hcc y k h0 p hp + exact ⟨u, hu, e1, by rw [hye u hu]; exact e2⟩ + exact ⟨ep', ntfn', reply', bufCover', idleNoFault', mcp', yieldRel'⟩ + +private theorem set_thread_state_running_vm_sinvA + (hsinv : SInvXA σ m n) {ct : U64} {x : Fl4SpecA64.Tcb} + (hk : σ.kheap (specPtr ct) = some (.tcb x)) + (hrun : runnable x.tcbState = true) : + SInvXA (updKSc σ (specPtr ct) (.tcb { x with tcbState := .ext .runningVM })) m n := by + have htNotIdle : x.tcbState ≠ .idleThreadState := fun hi => by rw [hi] at hrun; exact absurd hrun (by decide) + have htNeCurIdle : σ.idleThread ≠ specPtr ct := fun e => by + obtain ⟨y, hy, hs⟩ := hsinv.idleTcb; rw [e, hk] at hy; cases hy; exact htNotIdle hs + have CF := CurFactsA.back (σ := σ) (σ' := updKSc σ (specPtr ct) (.tcb { x with tcbState := .ext .runningVM })) + ⟨hsinv.zc, hsinv.rlsRun, hsinv.curScReady⟩ (fun _ h => h) rfl rfl rfl + (fun t x' hx => by + by_cases e : t = specPtr ct + · subst e; rw [updKSc_same] at hx; cases hx; exact ⟨x, hk, rfl⟩ + · rw [updKSc_other _ e] at hx; exact ⟨x', hx, rfl⟩) + (fun t y hy _ => by + by_cases e : t = specPtr ct + · subst e; exact ⟨{ x with tcbState := .ext .runningVM }, updKSc_same _ _ _, fun _ => rfl⟩ + · exact ⟨y, by rw [updKSc_other _ e]; exact hy, id⟩) + (fun y k hy => by + by_cases e : σ.curSc = specPtr ct + · rw [e, updKSc_same] at hy; cases hy + · rw [updKSc_other _ e] at hy; exact ⟨y, hy, fun a => ⟨a, id⟩⟩) + exact + { zc := CF.1 + rlsRun := CF.2.1 + curScReady := CF.2.2 + faultSt := fun p y hy hf => by + by_cases e : p = specPtr ct + · subst e; rw [updKSc_same] at hy; cases hy + have h0 := (hsinv.faultSt _ x hk hf).notRun + rw [h0] at hrun; cases hrun + · rw [updKSc_other _ e] at hy; exact hsinv.faultSt p y hy hf + mc := hsinv.mc + view := hsinv.view + curTcb := by + obtain ⟨y, hy⟩ := hsinv.curTcb + by_cases e : σ.curThread = specPtr ct + · rw [e, hk] at hy; cases hy; exact ⟨{ x with tcbState := .ext .runningVM }, by change (updKSc σ _ _).kheap σ.curThread = _; rw [e, updKSc_same]⟩ + · exact ⟨y, by change (updKSc σ _ _).kheap σ.curThread = _; rw [updKSc_other _ e]; exact hy⟩ + idleTcb := by + obtain ⟨y, hy, hs⟩ := hsinv.idleTcb + exact ⟨y, by change (updKSc σ _ _).kheap σ.idleThread = _; rw [updKSc_other _ htNeCurIdle]; exact hy, hs⟩ + onlyIdle := fun p y hy hs => by + by_cases e : p = specPtr ct + · rw [e, updKSc_same] at hy; cases hy; cases hs + · rw [updKSc_other _ e] at hy; exact hsinv.onlyIdle p y hy hs + idleSc := by + obtain ⟨y, sc, k, hy, hsc1, hsc2, hsc3⟩ := hsinv.idleSc + have hneSc : Fl4SpecA64.idleScPtr ≠ specPtr ct := fun e => by rw [e, hk] at hsc2; cases hsc2 + exact ⟨y, sc, k, by change (updKSc σ _ _).kheap σ.idleThread = _; rw [updKSc_other _ htNeCurIdle]; exact hy, hsc1, + by change (updKSc σ _ _).kheap Fl4SpecA64.idleScPtr = _; rw [updKSc_other _ hneSc]; exact hsc2, hsc3⟩ + curSc := by + obtain ⟨sc, k, hsc0⟩ := hsinv.curSc + exact ⟨sc, k, by change (updKSc σ _ _).kheap σ.curSc = _; rw [updKSc_other _ (fun e => by rw [e, hk] at hsc0; cases hsc0)]; exact hsc0⟩ + ctQ := hsinv.ctQ + idleQ := hsinv.idleQ + rq := fun d p t ht => by + obtain ⟨y, hy, hr⟩ := hsinv.rq d p t ht + by_cases e : t = specPtr ct + · rw [e, hk] at hy; cases hy; exact ⟨{ x with tcbState := .ext .runningVM }, by rw [e, updKSc_same], rfl⟩ + · exact ⟨y, by rw [updKSc_other _ e]; exact hy, hr⟩ + rls := fun t ht => by + obtain ⟨y, hy⟩ := hsinv.rls t ht + by_cases e : t = specPtr ct + · rw [e, hk] at hy; cases hy; exact ⟨{ x with tcbState := .ext .runningVM }, by rw [e, updKSc_same]⟩ + · exact ⟨y, by rw [updKSc_other _ e]; exact hy⟩ + rpos := fun p y k hp ha => hsinv.rpos p y k (by + by_cases e : p = specPtr ct + · rw [e, updKSc_same] at hp; cases hp + · rwa [updKSc_other _ e] at hp) ha + irqN := SpecIrqA.updTcbA hsinv.irqN hk rfl } + +private theorem set_thread_state_running_vm_sidleA + (hsidle : SIdleA σ m n) {ct : U64} {x : Fl4SpecA64.Tcb} + (hk : σ.kheap (specPtr ct) = some (.tcb x)) + (hrun : runnable x.tcbState = true) : + SIdleA (updKSc σ (specPtr ct) (.tcb { x with tcbState := .ext .runningVM })) m n := by + have htNotIdle : x.tcbState ≠ .idleThreadState := fun hi => by rw [hi] at hrun; exact absurd hrun (by decide) + have htNeIdle : ∀ c < n, m.idleThreadOf c ≠ specPtr ct := fun c hc e => by + obtain ⟨y, hy, hs⟩ := hsidle.f3 c hc; rw [e, hk] at hy; cases hy; exact htNotIdle hs + have hSpecCapEq : ∀ sl, specCapAtA (updKSc σ (specPtr ct) (.tcb { x with tcbState := .ext .runningVM })) sl = specCapAtA σ sl := fun sl => by + unfold specCapAtA; by_cases e : sl.1 = specPtr ct + · rw [e, updKSc_same, hk]; rfl + · rw [updKSc_other _ e] + exact + { f3 := fun c hc => let ⟨y, hy, hs⟩ := hsidle.f3 c hc; ⟨y, by rw [updKSc_other _ (htNeIdle c hc)]; exact hy, hs⟩ + f4 := fun y k hsc0 => hsidle.f4 y k (by rwa [updKSc_other _ (fun e => by rw [e, updKSc_same] at hsc0; cases hsc0)] at hsc0) + f5 := fun p y k hsc0 ht => hsidle.f5 p y k (by rwa [updKSc_other _ (fun e => by rw [e, updKSc_same] at hsc0; cases hsc0)] at hsc0) ht + f6 := hsidle.f6.of_eq hSpecCapEq + f7 := fun y k hsc0 => hsidle.f7 y k (by rwa [updKSc_other _ (fun e => by rw [e, updKSc_same] at hsc0; cases hsc0)] at hsc0) + idleSlot := fun c hc => by + have h0 := hsidle.idleSlot c hc + unfold vtCapA at h0 ⊢; rwa [updKSc_other _ (htNeIdle c hc)] + idleBound := fun c hc => by + have h0 := hsidle.idleBound c hc + unfold vcpuOfA at h0 ⊢; rwa [updKSc_other _ (htNeIdle c hc)] } + +private theorem set_thread_state_running_vm_svA + (hSV : SchedValidA σ) {ct : U64} {x : Fl4SpecA64.Tcb} + (hk : σ.kheap (specPtr ct) = some (.tcb x)) : + SchedValidA (updKSc σ (specPtr ct) (.tcb { x with tcbState := .ext .runningVM })) := + { rq := fun d p t ht => by + obtain ⟨⟨y, sc, hy, hsc0, hact⟩, hnrq⟩ := hSV.rq d p t ht + have hact' : ScActiveAtA (updKSc σ (specPtr ct) (.tcb { x with tcbState := .ext .runningVM })) sc := by + obtain ⟨ys, ks, hys, has⟩ := hact + exact ⟨ys, ks, by rw [updKSc_other _ (fun e => by rw [e, hk] at hys; cases hys)]; exact hys, has⟩ + by_cases e : t = specPtr ct + · subst e; rw [hk] at hy; cases hy; exact ⟨⟨{ x with tcbState := .ext .runningVM }, sc, updKSc_same _ _ _, hsc0, hact'⟩, hnrq⟩ + · exact ⟨⟨y, sc, by rw [updKSc_other _ e]; exact hy, hsc0, hact'⟩, hnrq⟩ + act := fun ha hni => by + obtain ⟨⟨y, sc, hy, hsc0, hact⟩, hnrq⟩ := hSV.act ha hni + have hact' : ScActiveAtA (updKSc σ (specPtr ct) (.tcb { x with tcbState := .ext .runningVM })) sc := by + obtain ⟨ys, ks, hys, has⟩ := hact + exact ⟨ys, ks, by rw [updKSc_other _ (fun e => by rw [e, hk] at hys; cases hys)]; exact hys, has⟩ + by_cases e : σ.curThread = specPtr ct + · rw [e, hk] at hy; cases hy; exact ⟨⟨{ x with tcbState := .ext .runningVM }, sc, by change (updKSc σ _ _).kheap σ.curThread = _; rw [e, updKSc_same], hsc0, hact'⟩, hnrq⟩ + · exact ⟨⟨y, sc, by change (updKSc σ _ _).kheap σ.curThread = _; rw [updKSc_other _ e]; exact hy, hsc0, hact'⟩, hnrq⟩ + blk := fun t y sc hy hb hsc0 => by + by_cases e : t = specPtr ct + · subst e; rw [updKSc_same] at hy; cases hy; cases hb + · rw [updKSc_other _ e] at hy + obtain ⟨ys, ks, hys, has⟩ := hSV.blk t y sc hy hb hsc0 + exact ⟨ys, ks, by rw [updKSc_other _ (fun eq => by rw [eq, hk] at hys; cases hys)]; exact hys, has⟩ + rls := fun t ht => by + obtain ⟨y, sc, hy, hsc0, hact⟩ := hSV.rls t ht + have hact' : ScActiveAtA (updKSc σ (specPtr ct) (.tcb { x with tcbState := .ext .runningVM })) sc := by + obtain ⟨ys, ks, hys, has⟩ := hact + exact ⟨ys, ks, by rw [updKSc_other _ (fun e => by rw [e, hk] at hys; cases hys)]; exact hys, has⟩ + by_cases e : t = specPtr ct + · subst e; rw [hk] at hy; cases hy; exact ⟨{ x with tcbState := .ext .runningVM }, sc, updKSc_same _ _ _, hsc0, hact'⟩ + · exact ⟨y, sc, by rw [updKSc_other _ e]; exact hy, hsc0, hact'⟩ } + +private theorem set_thread_state_running_vm_rvA {ar : machine.Arch A} {nA : U64} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s a σ m) + {ct : U64} {x : Fl4SpecA64.Tcb} + (hk : σ.kheap (specPtr ct) = some (.tcb x)) + (hal_st : Aligned (stA ct)) + (hTcbOff : ∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → + (u ≠ ct ∨ K ≠ 3328#u64) → L.mem (L.wr s (stA ct) sched.TS_RUNNING_VM) (wa u K) = L.mem s (wa u K)) : + let s_vm := L.wr s (stA ct) sched.TS_RUNNING_VM + let σ_vm := updKSc σ (specPtr ct) (.tcb { x with tcbState := .ext .runningVM }) + RunVcpuInv st Z.T s_vm ∧ BlkQueuedA Z.T E N (L.mem s_vm) σ_vm := by + intro s_vm σ_vm + have hrunVcpu' : RunVcpuInv st Z.T s_vm := fun u hu hne => by + unfold runVcpuA at hne; rw [hTcbOff u hu 3672#u64 (by simp) (by simp) (Or.inr (by decide))] at hne + by_cases eu : u = ct + · subst eu; exact L.mem_wr_same hal_st + · rw [stA_eq, hTcbOff u hu 3328#u64 (by simp) (by simp) (Or.inl eu), ← stA_eq]; exact J.runVcpu u hu hne + have blkQ' : BlkQueuedA Z.T E N (L.mem s_vm) σ_vm := by + refine ⟨fun u hu y hy e he => ?_, fun u hu y hy nu hnu => ?_⟩ + · by_cases eu : u = ct + · subst eu; rw [updKSc_same] at hy; cases hy; cases he + · rw [updKSc_other _ (fun e => eu (specPtr_inj e))] at hy + obtain ⟨hE, z, hz, hm⟩ := J.blkQ.ep u hu y hy e he + have hne : e ≠ specPtr ct := fun eq => by rw [eq, hk] at hz; cases hz + exact ⟨by rw [tBlockA_eq, hTcbOff u hu 3336#u64 (by simp) (by simp) (Or.inr (by decide)), ← tBlockA_eq]; exact hE, + z, by rw [updKSc_other _ hne]; exact hz, hm⟩ + · by_cases eu : u = ct + · subst eu; rw [updKSc_same] at hy; cases hy; cases hnu + · rw [updKSc_other _ (fun e => eu (specPtr_inj e))] at hy + obtain ⟨hN, z, q, hz, ho, hm⟩ := J.blkQ.ntfn u hu y hy nu hnu + have hne : nu ≠ specPtr ct := fun eq => by rw [eq, hk] at hz; cases hz + exact ⟨by rw [tBlockA_eq, hTcbOff u hu 3336#u64 (by simp) (by simp) (Or.inr (by decide)), ← tBlockA_eq]; exact hN, + z, q, by rw [updKSc_other _ hne]; exact hz, ho, hm⟩ + exact ⟨hrunVcpu', blkQ'⟩ + +set_option maxHeartbeats 1200000 in +/-- Helper: setting a runnable current TCB (with `sc ≠ 0`, `rmax ≠ 0`, not in release queue) to +`TS_RUNNING_VM` (`s_vm := L.wr s (stA ct) sched.TS_RUNNING_VM`) preserves `JStage3A`, `SchedValidA`, +and `VcpuRec6A` (no `SActA σ_vm` required, since `curThread` is now `.ext .runningVM`). -/ +theorem set_thread_state_running_vm_stage3A {ar : machine.Arch A} {nA : U64} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s a σ m) + (hNoSw : ∀ u, σ.schedulerAction ≠ .switchThread u) + (R6 : VcpuRec6A st Z OS Bf s σ) + {ct : U64} (hct : ct ∈ Z.T) {x : Fl4SpecA64.Tcb} + (hk : σ.kheap (specPtr ct) = some (.tcb x)) + (hrun : runnable x.tcbState = true) + (hsc : L.mem s (scA ct) ≠ 0#u64) (hrm : L.rmax s (L.mem s (scA ct)) ≠ 0#u64) + (hiq : (L.mem s (inqA ct) &&& sched.Q_RELEASE) = 0#u64) + (hNotRel : specPtr ct ∉ σ.releaseQueue) : + let s_vm := L.wr s (stA ct) sched.TS_RUNNING_VM + let σ_vm := updKSc σ (specPtr ct) (.tcb { x with tcbState := .ext .runningVM }) + setThreadState (Z := Unit) (specPtr ct) (.ext .runningVM) σ = ({((), σ_vm)}, False) ∧ + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s_vm a σ_vm m ∧ + (∀ u, σ_vm.schedulerAction ≠ .switchThread u) ∧ + VcpuRec6A st Z OS Bf s_vm σ_vm ∧ + OSKeep st OS s s_vm := by + intro s_vm σ_vm + obtain ⟨Pt, Ub, Pp7, Tb7, Hr, Te, Kt, Lv7, Re, bits7, KLv, W⟩ := J7A.unp J.j7 + have hst : StEqA Z.T (L.mem s) σ := fun u hu y hy => (J.ep.at_tcb hu hy).st + have hv : sched.TS_RUNNING_VM.val = tsCodeA (.ext .runningVM) := by unfold sched.TS_RUNNING_VM tsCodeA; rfl + have htH : ct ∈ H.tcbs := J.tcbs ▸ hct + have hRunC : Runnable (L.mem s (stA ct)) := (runnable_iffA (hst ct hct x hk)).2 hrun + have hnb : ¬ Blocked (L.mem s (stA ct)) := by + intro hb + rcases hRunC with h | h | h <;> rcases hb with h' | h' | h' <;> + (rw [h] at h'; have hv' := congrArg UScalar.val h'; simp [sched.TS_RUNNING, sched.TS_RESTART, + sched.TS_RUNNING_VM, sched.TS_BLOCKED_ON_SEND, sched.TS_BLOCKED_ON_RECV, sched.TS_BLOCKED_ON_NTFN] at hv') + have hnr : L.mem s (stA ct) ≠ sched.TS_BLOCKED_ON_REPLY := by + intro h' + rcases hRunC with h | h | h <;> + (rw [h] at h'; have hv' := congrArg UScalar.val h'; simp [sched.TS_RUNNING, sched.TS_RESTART, + sched.TS_RUNNING_VM, sched.TS_BLOCKED_ON_REPLY] at hv') + obtain ⟨-, hB, hX, hSg, hS⟩ := J.sys.ent.bxi + have hnoref : ∀ r ∈ H.replies, L.mem s (P1.rTcbA r) ≠ ct := + hX.noref (J.sq.tcbs ct htH) hnr (fun e => hnb (Or.inr (Or.inl e))) + have hnb_vm : ¬ Blocked sched.TS_RUNNING_VM := by + rintro (h | h | h) <;> + (have hv' := congrArg UScalar.val h; simp [sched.TS_RUNNING_VM, sched.TS_BLOCKED_ON_SEND, + sched.TS_BLOCKED_ON_RECV, sched.TS_BLOCKED_ON_NTFN] at hv') + have hne_rep : sched.TS_RUNNING_VM ≠ sched.TS_BLOCKED_ON_REPLY ∧ sched.TS_RUNNING_VM ≠ sched.TS_BLOCKED_ON_RECV := + ⟨fun h => by have hv' := congrArg UScalar.val h; simp [sched.TS_RUNNING_VM, sched.TS_BLOCKED_ON_REPLY] at hv', + fun h => by have hv' := congrArg UScalar.val h; simp [sched.TS_RUNNING_VM, sched.TS_BLOCKED_ON_RECV] at hv'⟩ + have hrun_vm : Runnable sched.TS_RUNNING_VM := Or.inr (Or.inr rfl) + -- `sched.set_thread_state st s ct sched.TS_RUNNING_VM = ok s_vm` + have hta := J.iplaced.e.tcb ct hct + have hscC : L.mem s (scA ct) ∈ Z.C := J.sr.rest.scMem ct hct hsc + have hscH : ∃ z, H (L.mem s (scA ct)) = some (.sc z) := by + change L.mem s (scA ct) ∈ H.scs; exact J.scs ▸ hscC + obtain ⟨zsc, hzsc⟩ := hscH + have hma_al : Aligned (maxA (L.mem s (scA ct))) := by + have hmin := J.sys.ent.heap.scmin _ zsc hzsc + have hal := J.sys.ent.heap.align _ (.sc zsc) hzsc + have h8 : 8 ∣ 2 ^ zsc := Nat.pow_dvd_pow 2 (by omega : 3 ≤ zsc) + have hc8 : (L.mem s (scA ct)).val % 8 = 0 := Nat.dvd_iff_mod_eq_zero.mp (Nat.dvd_trans h8 (Nat.dvd_iff_mod_eq_zero.mpr hal)) + unfold maxA; exact aligned_wa hc8 _ (by unfold layout.sc.REFILL_MAX; decide) + have hst_ne_ma : stA ct ≠ maxA (L.mem s (scA ct)) := fun e => by + have htH' : H ct = some .tcb := htH + have hne : ct ≠ L.mem s (scA ct) := fun h => by rw [h, hzsc] at htH'; cases htH' + have hd := J.sys.ent.heap.disj ct .tcb _ (.sc zsc) htH' hzsc hne + have hft := hta.fits + have hfs := J.sys.ent.heap.fits _ (.sc zsc) hzsc + have hmin := J.sys.ent.heap.scmin _ zsc hzsc + have hpow : 128 ≤ 2 ^ zsc := by have : 2 ^ 7 ≤ 2 ^ zsc := Nat.pow_le_pow_right (by omega) hmin; simpa using this + have hv1 : (stA ct).val = ct.val + 3328 := by + have h3328 : (3328#u64 : U64).val = 3328 := rfl + rw [stA_eq, wa_valm, h3328]; exact Nat.mod_eq_of_lt (by omega) + have hv2 : (maxA (L.mem s (scA ct))).val = (L.mem s (scA ct)).val + 72 := by + have h72 : layout.sc.REFILL_MAX.val = 72 := by unfold layout.sc.REFILL_MAX; rfl + change (L.mem s (scA ct)).val + 2 ^ zsc ≤ 2 ^ 64 at hfs + unfold maxA; rw [wa_valm, h72]; exact Nat.mod_eq_of_lt (by omega) + have hev := congrArg UScalar.val e; rw [hv1, hv2] at hev + change ct.val + 4096 ≤ (L.mem s (scA ct)).val ∨ (L.mem s (scA ct)).val + 2 ^ zsc ≤ ct.val at hd; omega + have hsts_eq : sched.set_thread_state st s ct sched.TS_RUNNING_VM = ok s_vm := + set_thread_state_vm_wrA hta hsc hrm hiq hma_al hst_ne_ma + -- Extract scheduler/queue/spec postconditions from `sc_set_runnable_stateA` + `sts_all` + have hall := WP.spec_and (WP.spec_and + (sc_set_runnable_stateA J.sr hst hct hk hrun (ts := .ext .runningVM) rfl hv) + (set_thread_state_exAnyA J.sr hst hct hk (ts := .ext .runningVM) rfl hv)) + (sts_all s ct sched.TS_RUNNING_VM Z.Q Z.R J.sq hB hX hS hSg htH hnb_vm hne_rep (Or.inr hnb) hnoref + ⟨fun _ _ _ _ _ => hrun_vm, fun _ _ _ => hrun_vm, fun _ => hrun_vm⟩) + rw [hsts_eq, WP.spec_ok] at hall + obtain ⟨⟨⟨σ', hsts, sr', hst', hkh', wS⟩, ⟨-, -, -, -, -, w2⟩⟩, ⟨sq1, hB1, hX1, hS1, hSg1, -, -⟩⟩ := hall + have hsts_vm : setThreadState (Z := Unit) (specPtr ct) (.ext .runningVM) σ = ({((), σ_vm)}, False) := by + have hst_eq : stUpdA σ (specPtr ct) x (.ext .runningVM) = σ_vm := rfl + rw [← hst_eq] + apply setThreadState_eqA σ (specPtr ct) x hk (.ext .runningVM) + intro _ _ + obtain ⟨x0', hkt0', -, hsc_eq⟩ := J.j.1.sch.tcb ct hct + rw [hk] at hkt0'; cases hkt0' + have hsc_some : x.tcbSchedContext = some (scRenA m (L.mem s (scA ct))) := by + rw [hsc_eq]; unfold optScA; simp [hsc] + obtain ⟨y, n0, hy, hrm_eq⟩ := J.j.1.rest.refillMax _ hscC + have hrm_eq' : y.scRefillMax = (L.rmax s (L.mem s (scA ct))).val := by + rw [hrm_eq, LawfulStore.rmax, maxA_eqS]; rfl + have hpos : 0 < y.scRefillMax := by + by_contra h0; exact hrm (UScalar.eq_of_val_eq (by change (L.rmax s _).val = 0; omega)) + have hne_sc : scRenA m (L.mem s (scA ct)) ≠ specPtr ct := fun e => by rw [e, hk] at hy; cases hy + exact schedulable_ofA (updKSc_same _ _ _) rfl hsc_some (by change (updKSc σ (specPtr ct) _).kheap _ = _; rw [updKSc_other _ hne_sc]; exact hy) hpos hNotRel + have hσ'_eq : σ' = σ_vm := + congrArg Prod.snd (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst (hsts.symm.trans hsts_vm))) + subst hσ'_eq + have sq' : SchedQueuesInv st H.tcbs Z.Cs H.omega H s_vm Z.Q Z.R := sq1.congrX empty_congr + have hstepW : WritesIn st s s_vm (SchedStepWA (st := st) Z m σ s) := + wS.mono fun b hb => hb.elim (fun h => Or.inl (Or.inr h)) + (fun e => by subst e; exact Or.inr (stA_tcbStepWA (fun u hu => J.sr.core.tcbs u hu) hct)) + have hSpecS : SpecSchedA σ σ_vm := by + refine ⟨fun p => ?_, rfl, rfl, rfl, rfl, rfl, rfl⟩ + by_cases ep : p = specPtr ct + · subst ep; exact Or.inr (Or.inl ⟨x, { x with tcbState := .ext .runningVM }, hk, updKSc_same _ _ _, ⟨rfl, rfl, rfl⟩⟩) + · exact Or.inl (updKSc_other _ ep) + have hSSz : SSzA σ σ_vm := fun p y k h => by + by_cases ep : p = specPtr ct + · subst ep; rw [updKSc_same] at h; cases h + · exact ⟨y, by rwa [updKSc_other _ ep] at h⟩ + have hQk : QKeepA σ σ_vm := by + refine ⟨fun p y hy => ?_, fun p x0 hp => ?_, fun p x0 hp => ?_, fun p y k hp => ?_, fun p y hp => ?_, ⟨rfl, rfl, rfl, rfl⟩⟩ + · by_cases ep : p = specPtr ct + · subst ep; rw [updKSc_same] at hy; cases hy; exact ⟨x, hk, rfl⟩ + · exact ⟨y, by rwa [updKSc_other _ ep] at hy, rfl⟩ + · by_cases ep : p = specPtr ct + · subst ep; rw [updKSc_same] at hp; cases hp + · exact ⟨x0, by rwa [updKSc_other _ ep] at hp, List.Sublist.refl _⟩ + · by_cases ep : p = specPtr ct + · subst ep; rw [updKSc_same] at hp; cases hp + · exact ⟨x0, by rwa [updKSc_other _ ep] at hp, List.Sublist.refl _⟩ + · by_cases ep : p = specPtr ct + · subst ep; rw [updKSc_same] at hp; cases hp + · exact ⟨y, k, by rwa [updKSc_other _ ep] at hp, fun r => ⟨r, id⟩⟩ + · by_cases ep : p = specPtr ct + · subst ep; rw [updKSc_same] at hp; cases hp + · exact ⟨y, by rwa [updKSc_other _ ep] at hp, id⟩ + have kb : L.ksb s_vm = L.ksb s := L.ksb_wr _ _ _ + have osk := J.osKeep_stepA hstepW + have capE : ∀ y ∈ OS, capA st s_vm y = capA st s y := fun y hy => osk.capA hy + have hal_st : Aligned (stA ct) := by rw [stA_eq]; exact aligned_wa (by unfold Aligned; have := hta.align; omega) 3328#u64 (by decide) + have hTcbOff : ∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → + (u ≠ ct ∨ K ≠ 3328#u64) → L.mem s_vm (wa u K) = L.mem s (wa u K) := fun u hu K hK1 hK8 hne => by + refine mem_wr_neA hal_st (field_aligned (J.sr.core.tcbs u hu) hK1 hK8) (fun e => ?_) + rw [stA_eq] at e + exact field_ne hta (J.iplaced.e.tcb u hu) (by decide) hK1 (fun eu ev => by + rcases hne with h | h + · exact h eu.symm + · exact h (UScalar.eq_of_val_eq ev.symm)) e + have hNonTcb : ∀ p k, H p = some k → k ≠ .tcb → (∀ z, k ≠ .other z) → + ∀ b, Aligned b → p.val ≤ b.val → b.val < p.val + 2 ^ k.bits → L.mem s_vm b = L.mem s b := fun p k hp hkT _ b hb h1 h2 => by + refine mem_wr_neA hal_st hb (fun e => ?_) + have hne : p ≠ ct := fun h => by rw [h, htH] at hp; cases hp; exact hkT rfl + have hd := J.sys.ent.heap.disj p k ct .tcb hp htH hne + have hv1 : (stA ct).val = ct.val + 3328 := by + have h3328 : (3328#u64 : U64).val = 3328 := rfl + rw [stA_eq, wa_valm, h3328]; exact Nat.mod_eq_of_lt (by have := hta.fits; omega) + have := congrArg UScalar.val e; rw [hv1] at this + change p.val + 2 ^ k.bits ≤ ct.val ∨ ct.val + 4096 ≤ p.val at hd; omega + have hvsT : VsTyR st (L.ksb s_vm) (PoolSet st Pp s_vm) ∅ Tb Lv s_vm := by + have hvs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → L.mem s_vm b = L.mem s b := fun b hb => by + refine hstepW.mem_eq (J.vsOk.al b hb) ?_ + by_cases ha : AsW (L.ksb s) b + · obtain ⟨o, o1, o2, -, e⟩ := ha + have ev : b.val = (L.ksb s).val + o.val := by rw [e, wa_valm]; exact Nat.mod_eq_of_lt (by have := J.sys.ent.ksw; omega) + exact J.asid_offStepA (by omega) (by omega) + · exact J.vsr_offStepA J.vsOk hb ha + exact (utVsPt_keepA J.sys.ent.vs kb hvs).1 + have ent' : EntryInvA3 st H Z.Cs OS U Ac Pp Tb Lv s_vm := + ppEntA3 J.sys.ent ⟨⟨Z.Q, Z.R, sq'⟩, hB1, hX1, hS1, hSg1⟩ kb hvsT + have poolOff' : PoolOff st H Z.Cs OS s_vm := by + refine poolOff_keepTableA (Z := Z) J.poolOff kb fun i => ?_ + have hv := AtOff.v i + have ev : (wa (L.ksb s) (AtOff i)).val = (L.ksb s).val + (AtOff i).val := by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by have := J.sys.ent.ksw; omega) + exact hstepW.mem_eq (by have := J.j.1.ksbAl; show _ % 8 = 0; omega) (J.asid_offStepA (by omega) (by omega)) + obtain ⟨ep', ntfn', reply', bufCover', idleNoFault', mcp', yieldRel'⟩ := + set_thread_state_running_vm_ipcA J hct hk hRunC hv hal_st hta hTcbOff hNonTcb + have sinv' := set_thread_state_running_vm_sinvA J.sinv hk hrun + have sidle' := set_thread_state_running_vm_sidleA J.sidle hk hrun + have hSV' := set_thread_state_running_vm_svA J.schedValid hk + obtain ⟨hrunVcpu', blkQ'⟩ := set_thread_state_running_vm_rvA J hk hal_st hTcbOff + have wv_vm : WritesIn st s s_vm (VW st H.tcbs Z.Cs Z.V {b | b ∈ Ub ∧ FrBw H OS b ∧ FrCw st OS s b} (L.ksb s) (· = ct)) := + (writesIn_wr hal_st).mono fun _ e => by subst e; exact Or.inr (Or.inr (Or.inl ⟨ct, htH, Or.inr (Or.inr (Or.inr ⟨rfl, rfl⟩))⟩)) + have hi_vm : L.idleW s_vm = L.idleW s := by + unfold LawfulStore.idleW + have h8 : layout.node.IDLE_THREAD.val = 8 := by unfold layout.node.IDLE_THREAD; rfl + have hna8 := nodeAt_val (J.sq.nodes _ J.sr.sch.cpuCs) (by rw [h8]; decide) + have hal := nodeAt_aligned (J.sq.nodes _ J.sr.sch.cpuCs) (by rw [h8]; decide) (by rw [h8]) + rw [L.cpu_wr, nodeAt_of_ksb kb] + exact mem_wr_neA hal_st hal (fun e => by + have hd := (J.sq.nodes _ J.sr.sch.cpuCs).disj ct htH + have h3328 : (3328#u64 : U64).val = 3328 := rfl + have hv1 : (stA ct).val = ct.val + 3328 := by rw [stA_eq, wa_valm, h3328]; exact Nat.mod_eq_of_lt (by have := hta.fits; omega) + have hv := congrArg UScalar.val e; rw [hv1, hna8, h8] at hv; unfold LawfulStore.nodeBase at hd hv; omega) + have hCw_vm : ∀ y, y = ct → L.mem s (stA y) ≠ sched.TS_BLOCKED_ON_REPLY ∧ L.mem s (stA y) ≠ sched.TS_INACTIVE := fun y hy => by + subst y + refine ⟨hnr, fun hi => ?_⟩ + rcases hRunC with h | h | h + · rw [h] at hi; have hv' := congrArg UScalar.val hi; simp [sched.TS_RUNNING, sched.TS_INACTIVE] at hv' + · rw [h] at hi; have hv' := congrArg UScalar.val hi; simp [sched.TS_RESTART, sched.TS_INACTIVE] at hv' + · rw [h] at hi; have hv' := congrArg UScalar.val hi; simp [sched.TS_RUNNING_VM, sched.TS_INACTIVE] at hv' + obtain ⟨sysl_vm, symL_vm⟩ := sysL_symL_of_vwA J W wv_vm hi_vm hCw_vm + have j5' : J5A st Z H OS s_vm σ_vm m := by + have hTr := J.j.1.cdt.tree.grow J.j5.2.1 (fun x hx hn => J.j5.2.2 x hx hn) + refine ⟨⟨MdbInvA.keepO hTr.lk hTr.tree J.j5.1.1 osk, IrqRunA.keepO hTr.lk J.j5.1.2.1 osk, + HandlersActiveA.keepO hTr.lk J.j5.1.2.2 osk kb fun i hi => ?_⟩, J.j5.2.1, + fun y hy hn => by rw [capE y hy]; exact J.j5.2.2 y hy hn⟩ + have hv := irqWordA_val J.sr.ksFit hi + have hal_i : Aligned (irqWordA (L.ksb s) i) := by have := J.j.1.ksbAl; show _ % 8 = 0; omega + exact hstepW.mem_eq hal_i (J.irq_offStepA hi) + have j7' : J7A st (J7R2A st ar nA) Z H OS s_vm σ_vm m := + J7PpKA.keep (st := st) Z H OS s s_vm σ σ_vm m J.j7 kb osk + (fun Pt Ub vr b hb => hstepW.mem_eq (vr.al b hb) (by + by_cases ha : AsW (L.ksb s) b + · obtain ⟨o, o1, o2, -, e⟩ := ha + have ev : b.val = (L.ksb s).val + o.val := by rw [e, wa_valm]; exact Nat.mod_eq_of_lt (by have := J.sys.ent.ksw; omega) + exact J.asid_offStepA (by omega) (by omega) + · exact J.vsr_offStepA vr hb ha)) + have J_vm : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s_vm a σ_vm m := + JStage3A.of_schedWr (Q := Z.Q) (Rl := Z.R) (curOf := Z.curOf) (curScOf := Z.curScOf) J + hstepW hSpecS hSSz hQk rfl J.j.1.ar (fun _ _ => rfl) + (fun u hu y hy => by + by_cases eu : u = ct + · subst eu; rw [updKSc_same] at hy; cases hy; exact J.bufAl u hu x hk + · rw [updKSc_other _ (fun e => eu (specPtr_inj e))] at hy; exact J.bufAl u hu y hy) + ⟨ent', sysl_vm⟩ sq' poolOff' sr' + (fun p y k hp => J.scCov p y k (by + by_cases e : p = specPtr ct + · rw [e, updKSc_same] at hp; cases hp + · rwa [updKSc_other _ e] at hp)) + ep' ntfn' reply' bufCover' idleNoFault' mcp' yieldRel' sinv' sidle' hrunVcpu' blkQ' symL_vm hSV' j5' j7' + exact ⟨hsts_vm, J_vm, hNoSw, J_vm.vcpuRec6A, osk⟩ + +set_option maxHeartbeats 1200000 in +/-- Steps 3–4 of `decode_run`: write `vc.RUN_CALLER := ct`, `vc.RUN_OFFSET := a0`, +`vc.RUN_OUT_MASK := a2`, `ct.RUN_VCPU := vc`, `set_stage2_root`, and `sched.set_thread_state ... TS_RUNNING_VM`, +using write commutation (`set_thread_state_running_vm_stage3A` + `JStage3A.of_vcpuStepA (regWr := true)`). -/ +theorem vcpu_enter_run_vm_stage3A {ar : machine.Arch A} {nA : U64} {Ub : Set U64} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s a σ m) + (hNoSw : ∀ u, σ.schedulerAction ≠ .switchThread u) (R6 : VcpuRec6A st Z OS Bf s σ) + (hS2R : VcpuStage2RootA st ar AR) (hVcP : VcPlacedA Z OS Ub (L.ksb s)) + {vc ct v_tcb a0 a2 : U64} + (hvc : vc ∈ Z.V) (hct : ct ∈ Z.T) (hvt : v_tcb ∈ Z.T) + (hrc0 : L.mem s (wa vc 24#u64) = 0#u64) + (htv0 : L.mem s (wa ct 3664#u64) = 0#u64) + (hrv0 : L.mem s (runVcpuA ct) = 0#u64) + (hout_ok : a2.bv &&& ~~~smAll = 0) + (hvc_live : ¬ DeadA st OS .vcpu s vc) + (hsc : L.mem s (scA ct) ≠ 0#u64) (hrm : L.rmax s (L.mem s (scA ct)) ≠ 0#u64) + (hiq : (L.mem s (inqA ct) &&& sched.Q_RELEASE) = 0#u64) + (hNotRel : specPtr ct ∉ σ.releaseQueue) + {v0 : Fl4SpecA64.Vcpu} {x0 : Fl4SpecA64.Tcb} + (hkv0 : σ.kheap (specPtr vc) = some (.archObj (.vcpu v0))) + (hkt0 : σ.kheap (specPtr ct) = some (.tcb x0)) + (hrun0 : runnable x0.tcbState = true) : + (do + let i22 ← lift (wa vc vcpu.RUN_CALLER) + let s5 ← st.write s i22 ct + let i23 ← lift (wa vc vcpu.RUN_OFFSET) + let s6 ← st.write s5 i23 a0 + let i24 ← lift (wa vc vcpu.RUN_OUT_MASK) + let s7 ← st.write s6 i24 a2 + let i25 ← layout.tcb.RUN_VCPU + let i26 ← lift (wa ct i25) + let s8 ← st.write s7 i26 vc + let (s9, a4) ← vspace.set_stage2_root st ar s8 a v_tcb + let s10 ← sched.set_thread_state st s9 ct sched.TS_RUNNING_VM + ok (s10, a4)) ⦃ res => + ∃ (σ' : Fl4SpecA64.State Unit), + (do fl4VcpuExtUpdate (Z := Unit) (specPtr vc) fun e => + { e with runCaller := some (specPtr ct), runOffset := a0.bv, runOutMask := a2.bv } + setRunVcpu (specPtr ct) (some (specPtr vc)) + setThreadState (specPtr ct) (.ext .runningVM)) σ = ({((), σ')}, False) ∧ + (do fl4VcpuExtUpdate (Z := Unit) (specPtr vc) fun e => + { e with runCaller := some (specPtr ct), runOffset := a0.bv, runOutMask := a2.bv } + setRunVcpu (specPtr ct) (some (specPtr vc)) + setThreadState (specPtr ct) (.ext .runningVM)) (updKSc σ (specPtr ct) (.tcb { x0 with tcbState := .restart })) = ({((), σ')}, False) ∧ + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) res.1 res.2 σ' m ∧ + (∀ u, σ'.schedulerAction ≠ .switchThread u) ∧ + VcpuRec6A st Z OS Bf res.1 σ' ∧ + OSKeep st OS s res.1 ∧ + L.ksb res.1 = L.ksb s ∧ + L.mem res.1 (stA ct) = sched.TS_RUNNING_VM ∧ + L.mem res.1 (runVcpuA ct) = vc ⦄ := by + have hta := J.iplaced.e.tcb ct hct + have hal_t : Aligned ct := by unfold Aligned; have := hta.align; omega + have hal_24 : Aligned (wa vc 24#u64) := aligned_wa (hVcP.vc_al hvc) 24#u64 (by decide) + have hal_32 : Aligned (wa vc 32#u64) := aligned_wa (hVcP.vc_al hvc) 32#u64 (by decide) + have hal_40 : Aligned (wa vc 40#u64) := aligned_wa (hVcP.vc_al hvc) 40#u64 (by decide) + have hal_rv : Aligned (runVcpuA ct) := by unfold runVcpuA; exact aligned_wa hal_t 3672#u64 (by decide) + have hal_st : Aligned (stA ct) := by rw [stA_eq]; exact aligned_wa hal_t 3328#u64 (by decide) + have hrc_off : vcpu.RUN_CALLER = 24#u64 := by unfold vcpu.RUN_CALLER; rfl + have hro_off : vcpu.RUN_OFFSET = 32#u64 := by unfold vcpu.RUN_OFFSET; rfl + have hrm_off : vcpu.RUN_OUT_MASK = 40#u64 := by unfold vcpu.RUN_OUT_MASK; rfl + have hrv_off : layout.tcb.RUN_VCPU = ok 3672#u64 := layout_tcb_run_vcpu_eqA + rw [hrc_off, lift_okR, bind_ok, L.write_eq, bind_ok, + hro_off, lift_okR, bind_ok, L.write_eq, bind_ok, + hrm_off, lift_okR, bind_ok, L.write_eq, bind_ok, + hrv_off, bind_ok, lift_okR, bind_ok, L.write_eq, bind_ok] + set s5 := L.wr s (wa vc 24#u64) ct + set s6 := L.wr s5 (wa vc 32#u64) a0 + set s7 := L.wr s6 (wa vc 40#u64) a2 + set s8 := L.wr s7 (wa ct 3672#u64) vc + obtain ⟨⟨s9, a4⟩, hs2_eq, hs9_eq, hAR'⟩ := spec_ex_fd (hS2R.set_stage2_root s8 a Z.hw v_tcb J.j.1.ar) + dsimp only at hs9_eq hAR'; subst hs9_eq + rw [hs2_eq, do_ok_prod] + -- Evaluate `sched.set_thread_state st s8 ct TS_RUNNING_VM = ok (L.wr s8 (stA ct) TS_RUNNING_VM)` + have hscC : L.mem s (scA ct) ∈ Z.C := J.sr.rest.scMem ct hct hsc + have hscH : ∃ z, H (L.mem s (scA ct)) = some (.sc z) := by change L.mem s (scA ct) ∈ H.scs; exact J.scs ▸ hscC + obtain ⟨zsc, hzsc⟩ := hscH + have hma_al : Aligned (maxA (L.mem s (scA ct))) := by + have hmin := J.sys.ent.heap.scmin _ zsc hzsc + have hal := J.sys.ent.heap.align _ (.sc zsc) hzsc + have h8 : 8 ∣ 2 ^ zsc := Nat.pow_dvd_pow 2 (by omega : 3 ≤ zsc) + have hc8 : (L.mem s (scA ct)).val % 8 = 0 := Nat.dvd_iff_mod_eq_zero.mp (Nat.dvd_trans h8 (Nat.dvd_iff_mod_eq_zero.mpr hal)) + unfold maxA; exact aligned_wa hc8 _ (by unfold layout.sc.REFILL_MAX; decide) + have htH : ct ∈ H.tcbs := J.tcbs ▸ hct + have hst_ne_ma : stA ct ≠ maxA (L.mem s (scA ct)) := fun e => by + have htH' : H ct = some .tcb := htH + have hne : ct ≠ L.mem s (scA ct) := fun h => by rw [h, hzsc] at htH'; cases htH' + have hd := J.sys.ent.heap.disj ct .tcb _ (.sc zsc) htH' hzsc hne + have hft := hta.fits; have hfs := J.sys.ent.heap.fits _ (.sc zsc) hzsc + have hmin := J.sys.ent.heap.scmin _ zsc hzsc + have hpow : 128 ≤ 2 ^ zsc := by have : 2 ^ 7 ≤ 2 ^ zsc := Nat.pow_le_pow_right (by omega) hmin; simpa using this + have h3328 : (3328#u64 : U64).val = 3328 := rfl + have h72 : layout.sc.REFILL_MAX.val = 72 := by unfold layout.sc.REFILL_MAX; rfl + have hv1 : (stA ct).val = ct.val + 3328 := by rw [stA_eq, wa_valm, h3328]; exact Nat.mod_eq_of_lt (by omega) + have hv2 : (maxA (L.mem s (scA ct))).val = (L.mem s (scA ct)).val + 72 := by + change (L.mem s (scA ct)).val + 2 ^ zsc ≤ 2 ^ 64 at hfs + unfold maxA; rw [wa_valm, h72]; exact Nat.mod_eq_of_lt (by omega) + have hev := congrArg UScalar.val e; rw [hv1, hv2] at hev + change ct.val + 4096 ≤ (L.mem s (scA ct)).val ∨ (L.mem s (scA ct)).val + 2 ^ zsc ≤ ct.val at hd; omega + have hmem8 : ∀ q, Aligned q → q ≠ wa vc 24#u64 → q ≠ wa vc 32#u64 → q ≠ wa vc 40#u64 → q ≠ wa ct 3672#u64 → + L.mem s8 q = L.mem s q := fun q hq h1 h2 h3 h4 => by + simp only [s8, s7, s6, s5] + have hal_rv' : Aligned (wa ct 3672#u64) := hal_rv + rw [mem_wr_neA hal_rv' hq h4.symm, mem_wr_neA hal_40 hq h3.symm, mem_wr_neA hal_32 hq h2.symm, mem_wr_neA hal_24 hq h1.symm] + have hsc8 : L.mem s8 (scA ct) = L.mem s (scA ct) := by + rw [scA_eq]; exact hmem8 _ (aligned_wa hal_t 3464#u64 (by decide)) + (hVcP.disj_tcb vc hvc ct hct 24#u64 3464#u64 (by decide) (by decide)).symm + (hVcP.disj_tcb vc hvc ct hct 32#u64 3464#u64 (by decide) (by decide)).symm + (hVcP.disj_tcb vc hvc ct hct 40#u64 3464#u64 (by decide) (by decide)).symm + (field_ne hta hta (by decide) (by decide) (fun _ => by decide)) + have hiq8 : L.mem s8 (inqA ct) = L.mem s (inqA ct) := by + rw [inqA_eq]; exact hmem8 _ (aligned_wa hal_t 3592#u64 (by decide)) + (hVcP.disj_tcb vc hvc ct hct 24#u64 3592#u64 (by decide) (by decide)).symm + (hVcP.disj_tcb vc hvc ct hct 32#u64 3592#u64 (by decide) (by decide)).symm + (hVcP.disj_tcb vc hvc ct hct 40#u64 3592#u64 (by decide) (by decide)).symm + (field_ne hta hta (by decide) (by decide) (fun _ => by decide)) + have hma8 : L.mem s8 (maxA (L.mem s (scA ct))) = L.mem s (maxA (L.mem s (scA ct))) := by + have hdisj_sc : ∀ v ∈ Z.V, ∀ o : U64, o.val < 4096 → wa v o ≠ maxA (L.mem s (scA ct)) := fun v hv o ho e => by + obtain ⟨_, _, -, arm, -⟩ := J7FrA.fr (st := st) _ _ _ _ _ _ J.j7 + have hvH := arm.vh v hv + have hne : v ≠ L.mem s (scA ct) := fun h => by rw [h, hzsc] at hvH; cases hvH + have hd := J.sys.ent.heap.disj v (.other 12) _ (.sc zsc) hvH hzsc hne + have hfv := J.sys.ent.heap.fits v _ hvH; have hfs := J.sys.ent.heap.fits _ (.sc zsc) hzsc + have hmin := J.sys.ent.heap.scmin _ zsc hzsc + have hpow : 128 ≤ 2 ^ zsc := by have : 2 ^ 7 ≤ 2 ^ zsc := Nat.pow_le_pow_right (by omega) hmin; simpa using this + have ev1 : (wa v o).val = v.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by change v.val + 4096 ≤ 2 ^ 64 at hfv; omega) + have ev2 : (maxA (L.mem s (scA ct))).val = (L.mem s (scA ct)).val + 72 := by + have h72 : layout.sc.REFILL_MAX.val = 72 := by unfold layout.sc.REFILL_MAX; rfl + change (L.mem s (scA ct)).val + 2 ^ zsc ≤ 2 ^ 64 at hfs + unfold maxA; rw [wa_valm, h72]; exact Nat.mod_eq_of_lt (by omega) + have := congrArg UScalar.val e; rw [ev1, ev2] at this + change v.val + 4096 ≤ (L.mem s (scA ct)).val ∨ (L.mem s (scA ct)).val + 2 ^ zsc ≤ v.val at hd; omega + have hne_rv : wa ct 3672#u64 ≠ maxA (L.mem s (scA ct)) := fun e => by + have htH' : H ct = some .tcb := htH + have hne : ct ≠ L.mem s (scA ct) := fun h => by rw [h, hzsc] at htH'; cases htH' + have hd := J.sys.ent.heap.disj ct .tcb _ (.sc zsc) htH' hzsc hne + have hft := hta.fits; have hfs := J.sys.ent.heap.fits _ (.sc zsc) hzsc + have hmin := J.sys.ent.heap.scmin _ zsc hzsc + have hpow : 128 ≤ 2 ^ zsc := by have : 2 ^ 7 ≤ 2 ^ zsc := Nat.pow_le_pow_right (by omega) hmin; simpa using this + have ev1 : (wa ct 3672#u64).val = ct.val + 3672 := by + have h3672 : (3672#u64 : U64).val = 3672 := rfl + rw [wa_valm, h3672]; exact Nat.mod_eq_of_lt (by omega) + have ev2 : (maxA (L.mem s (scA ct))).val = (L.mem s (scA ct)).val + 72 := by + have h72 : layout.sc.REFILL_MAX.val = 72 := by unfold layout.sc.REFILL_MAX; rfl + change (L.mem s (scA ct)).val + 2 ^ zsc ≤ 2 ^ 64 at hfs + unfold maxA; rw [wa_valm, h72]; exact Nat.mod_eq_of_lt (by omega) + have := congrArg UScalar.val e; rw [ev1, ev2] at this + change ct.val + 4096 ≤ (L.mem s (scA ct)).val ∨ (L.mem s (scA ct)).val + 2 ^ zsc ≤ ct.val at hd; omega + exact hmem8 _ hma_al (hdisj_sc vc hvc 24#u64 (by decide)).symm (hdisj_sc vc hvc 32#u64 (by decide)).symm + (hdisj_sc vc hvc 40#u64 (by decide)).symm hne_rv.symm + have hsts_eq : sched.set_thread_state st s8 ct sched.TS_RUNNING_VM = ok (L.wr s8 (stA ct) sched.TS_RUNNING_VM) := + set_thread_state_vm_wrA hta (hsc8 ▸ hsc) (by show L.mem s8 (maxA (L.mem s8 (scA ct))) ≠ 0#u64; rw [hsc8, hma8]; exact hrm) + (hiq8 ▸ hiq) (by rw [hsc8]; exact hma_al) (by rw [hsc8]; exact hst_ne_ma) + rw [hsts_eq, bind_ok, WP.spec_ok] + set s10 := L.wr s8 (stA ct) sched.TS_RUNNING_VM + -- Commutation: first `s → s_vm` (`set_thread_state_running_vm_stage3A`), then `s_vm → s10` (`JStage3A.of_vcpuStepA (regWr := true)`)! + obtain ⟨-, J_vm, hNoSw_vm, R6_vm, osk_vm⟩ := + set_thread_state_running_vm_stage3A J hNoSw R6 hct hkt0 hrun0 hsc hrm hiq hNotRel + set s_vm := L.wr s (stA ct) sched.TS_RUNNING_VM + set σ_vm := updKSc σ (specPtr ct) (.tcb { x0 with tcbState := .ext .runningVM }) + set v1 : Fl4SpecA64.Vcpu := { v0 with vcpuExt := { v0.vcpuExt with + runCaller := some (specPtr ct), runOffset := a0.bv, runOutMask := a2.bv } } + set x1 : Fl4SpecA64.Tcb := { x0 with tcbState := .ext .runningVM, tcbExt := { x0.tcbExt with tcbRunVcpu := some (specPtr vc) } } + set σ10 := updKSc (updKSc σ (specPtr vc) (.archObj (.vcpu v1))) (specPtr ct) (.tcb x1) + have hne_vc_ct : specPtr vc ≠ specPtr ct := fun e => by rw [e, hkt0] at hkv0; cases hkv0 + have hσ10_eq : σ10 = updKSc (updKSc σ_vm (specPtr vc) (.archObj (.vcpu v1))) (specPtr ct) (.tcb x1) := by + unfold σ10 σ_vm + rw [updKSc_commR σ hne_vc_ct.symm, updKSc_idemR] + have hw_vm_10 : WritesIn st s_vm s10 (VcpuWrFootA st true Z OS s_vm) := by + refine ⟨by simp [s10, s8, s7, s6, s5, s_vm, L.ksb_wr], + by simp [s10, s8, s7, s6, s5, s_vm, L.cpu_wr], fun q hqa hnf => ?_⟩ + by_cases hq_st : q = stA ct + · subst hq_st; rw [L.mem_wr_same hal_st, L.mem_wr_same hal_st] + · rw [mem_wr_neA hal_st hqa (Ne.symm hq_st), mem_wr_neA hal_st hqa (Ne.symm hq_st)] + exact hmem8 q hqa + (fun e => hnf (e ▸ Or.inl ⟨vc, hvc, 24#u64, by decide, by decide, rfl⟩)) + (fun e => hnf (e ▸ Or.inl ⟨vc, hvc, 32#u64, by decide, by decide, rfl⟩)) + (fun e => hnf (e ▸ Or.inl ⟨vc, hvc, 40#u64, by decide, by decide, rfl⟩)) + (fun e => hnf (e ▸ Or.inr (Or.inl ⟨ct, hct, 3672#u64, by decide, Or.inr (Or.inr rfl), rfl⟩))) + have hHS_vm_10 : VcpuSpecStepA H σ_vm σ10 true := by + rw [hσ10_eq] + refine ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, + fun p => ?_, fun _ h => absurd rfl h⟩ + by_cases h1 : p = specPtr ct + · subst h1; exact Or.inr (Or.inl ⟨{ x0 with tcbState := .ext .runningVM }, x1, updKSc_same _ _ _, updKSc_same _ _ _, + ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, fun hs => by cases hs⟩⟩) + · by_cases h2 : p = specPtr vc + · subst h2; exact Or.inr (Or.inr ⟨v0, v1, by rw [updKSc_other _ hne_vc_ct]; exact hkv0, + by rw [updKSc_other _ hne_vc_ct, updKSc_same], trivial⟩) + · exact Or.inl (by rw [updKSc_other _ h1, updKSc_other _ h2]) + have osk_10 : OSKeep st OS s_vm s10 := osKeep_of_vcpuWrCoreA (J_vm.toVcpuCoreA hNoSw_vm) R6_vm.vcOS R6_vm.frOff hw_vm_10 + have hcapE_10 : ∀ y ∈ OS, capA st s10 y = capA st s_vm y := fun y hy => osk_10.capA hy + have hct_nz : ct ≠ 0#u64 := fun e => hta.ne0 (congrArg UScalar.val e) + have hvc_nz : vc ≠ 0#u64 := (hVcP.al vc hvc).1 + have hmem10_st : L.mem s10 (stA ct) = sched.TS_RUNNING_VM := L.mem_wr_same hal_st + have hmem10_rv : L.mem s10 (runVcpuA ct) = vc := by + have hne_st_rv : stA ct ≠ runVcpuA ct := by + rw [stA_eq]; unfold runVcpuA; exact field_ne hta hta (by decide) (by decide) (fun _ => by decide) + rw [mem_wr_neA hal_st hal_rv hne_st_rv]; unfold runVcpuA; exact L.mem_wr_same hal_rv + have hmem10_vc : ∀ o : U64, (o = 24#u64 ∨ o = 32#u64 ∨ o = 40#u64) → + L.mem s10 (wa vc o) = (if o = 24#u64 then ct else if o = 32#u64 then a0 else a2) := fun o ho => by + have hne_st : stA ct ≠ wa vc o := by + rw [stA_eq]; exact (hVcP.disj_tcb vc hvc ct hct o 3328#u64 (by rcases ho with rfl | rfl | rfl <;> decide) (by decide)).symm + have hne_rv : wa ct 3672#u64 ≠ wa vc o := + (hVcP.disj_tcb vc hvc ct hct o 3672#u64 (by rcases ho with rfl | rfl | rfl <;> decide) (by decide)).symm + have hal_rv' : Aligned (wa ct 3672#u64) := hal_rv + rcases ho with rfl | rfl | rfl + · rw [mem_wr_neA hal_st hal_24 hne_st, mem_wr_neA hal_rv' hal_24 hne_rv, + mem_wr_neA hal_40 hal_24 (hVcP.same_vcpu_ne hvc (by decide) (by decide) (by decide)), + mem_wr_neA hal_32 hal_24 (hVcP.same_vcpu_ne hvc (by decide) (by decide) (by decide)), L.mem_wr_same hal_24]; rfl + · rw [mem_wr_neA hal_st hal_32 hne_st, mem_wr_neA hal_rv' hal_32 hne_rv, + mem_wr_neA hal_40 hal_32 (hVcP.same_vcpu_ne hvc (by decide) (by decide) (by decide)), L.mem_wr_same hal_32]; rfl + · rw [mem_wr_neA hal_st hal_40 hne_st, mem_wr_neA hal_rv' hal_40 hne_rv, L.mem_wr_same hal_40]; rfl + have hmem10_other : ∀ q, Aligned q → q ≠ wa vc 24#u64 → q ≠ wa vc 32#u64 → q ≠ wa vc 40#u64 → q ≠ runVcpuA ct → + L.mem s10 q = L.mem s_vm q := fun q hq h1 h2 h3 h4 => by + by_cases hst : q = stA ct + · subst hst; rw [L.mem_wr_same hal_st, L.mem_wr_same hal_st] + · rw [mem_wr_neA hal_st hq (Ne.symm hst), mem_wr_neA hal_st hq (Ne.symm hst)] + exact hmem8 q hq h1 h2 h3 h4 + have hcore10 : VcpuCoreA (L := L) Z s10 σ10 := by + rw [hσ10_eq] + refine ⟨fun v hv => ?_, fun p x hkp => ?_, fun t ht => ?_, fun y hy => ?_⟩ + · obtain ⟨xv, hkv, hxt, hxr⟩ := J_vm.j.2.1.obj v hv + have h0_eq : L.mem s10 v = L.mem s_vm v := hmem10_other v (hVcP.vc_al hv) + (by by_cases h : v = vc + · subst h; simpa [wa_zeroR] using hVcP.same_vcpu_ne hv (o₁ := 0#u64) (o₂ := 24#u64) (by decide) (by decide) (by decide) + · simpa [wa_zeroR] using hVcP.disj_vcpu hv hvc h (ov₁ := 0#u64) (ov₂ := 24#u64) (by decide) (by decide)) + (by by_cases h : v = vc + · subst h; simpa [wa_zeroR] using hVcP.same_vcpu_ne hv (o₁ := 0#u64) (o₂ := 32#u64) (by decide) (by decide) (by decide) + · simpa [wa_zeroR] using hVcP.disj_vcpu hv hvc h (ov₁ := 0#u64) (ov₂ := 32#u64) (by decide) (by decide)) + (by by_cases h : v = vc + · subst h; simpa [wa_zeroR] using hVcP.same_vcpu_ne hv (o₁ := 0#u64) (o₂ := 40#u64) (by decide) (by decide) (by decide) + · simpa [wa_zeroR] using hVcP.disj_vcpu hv hvc h (ov₁ := 0#u64) (ov₂ := 40#u64) (by decide) (by decide)) + (by unfold runVcpuA; simpa [wa_zeroR] using hVcP.disj_tcb v hv ct hct 0#u64 3672#u64 (by decide) (by decide)) + by_cases hvv : v = vc + · subst hvv + rw [updKSc_other _ hne_vc_ct] at hkv; rw [hkv0] at hkv; cases hkv + refine ⟨v1, by rw [updKSc_other _ hne_vc_ct, updKSc_same], by rw [h0_eq]; exact hxt, ?_⟩ + rw [hmem10_vc 24#u64 (Or.inl rfl)]; simp [v1, optRefA, hct_nz] + · have hne_s : specPtr v ≠ specPtr vc := fun e => hvv (specPtr_inj e) + have hne_c : specPtr v ≠ specPtr ct := fun e => by rw [e, updKSc_same] at hkv; cases hkv + refine ⟨xv, by rw [updKSc_other _ hne_c, updKSc_other _ hne_s]; exact hkv, by rw [h0_eq]; exact hxt, ?_⟩ + rw [hmem10_other (wa v 24#u64) (aligned_wa (hVcP.vc_al hv) 24#u64 (by decide)) + (hVcP.disj_vcpu hv hvc hvv (by decide) (by decide)) + (hVcP.disj_vcpu hv hvc hvv (by decide) (by decide)) + (hVcP.disj_vcpu hv hvc hvv (by decide) (by decide)) + (by unfold runVcpuA; exact hVcP.disj_tcb v hv ct hct 24#u64 3672#u64 (by decide) (by decide))] + exact hxr + · by_cases h1 : p = specPtr ct + · subst h1; rw [updKSc_same] at hkp; cases hkp + · rw [updKSc_other _ h1] at hkp + by_cases h2 : p = specPtr vc + · subst h2; exact ⟨vc, hvc, rfl⟩ + · rw [updKSc_other _ h2] at hkp; exact J_vm.j.2.1.back p x hkp + · obtain ⟨xt, hkt, htv, htr⟩ := J_vm.j.2.1.tcb t ht + have ht3664 : L.mem s10 (wa t 3664#u64) = L.mem s_vm (wa t 3664#u64) := + hmem10_other _ (aligned_wa (hVcP.al_t t ht) 3664#u64 (by decide)) + (hVcP.disj_tcb vc hvc t ht 24#u64 3664#u64 (by decide) (by decide)).symm + (hVcP.disj_tcb vc hvc t ht 32#u64 3664#u64 (by decide) (by decide)).symm + (hVcP.disj_tcb vc hvc t ht 40#u64 3664#u64 (by decide) (by decide)).symm + (by unfold runVcpuA; exact field_ne (J.iplaced.e.tcb t ht) hta (by decide) (by decide) (fun _ => by decide)) + by_cases htt : t = ct + · subst htt; rw [updKSc_same] at hkt; cases hkt + refine ⟨x1, updKSc_same _ _ _, by rw [ht3664]; exact htv, ?_⟩ + rw [hmem10_rv]; simp [x1, optRefA, hvc_nz] + · have hne_c : specPtr t ≠ specPtr ct := fun e => htt (specPtr_inj e) + have hne_v : specPtr t ≠ specPtr vc := fun e => by rw [e, updKSc_other _ hne_vc_ct, hkv0] at hkt; cases hkt + refine ⟨xt, by rw [updKSc_other _ hne_c, updKSc_other _ hne_v]; exact hkt, by rw [ht3664]; exact htv, ?_⟩ + rw [hmem10_other (runVcpuA t) (by unfold runVcpuA; exact aligned_wa (hVcP.al_t t ht) 3672#u64 (by decide)) + (by unfold runVcpuA; exact (hVcP.disj_tcb vc hvc t ht 24#u64 3672#u64 (by decide) (by decide)).symm) + (by unfold runVcpuA; exact (hVcP.disj_tcb vc hvc t ht 32#u64 3672#u64 (by decide) (by decide)).symm) + (by unfold runVcpuA; exact (hVcP.disj_tcb vc hvc t ht 40#u64 3672#u64 (by decide) (by decide)).symm) + (by unfold runVcpuA; exact field_ne (J.iplaced.e.tcb t ht) hta (by decide) (by decide) (fun e _ => htt e))] + exact htr + · rw [capAtA_eq, hcapE_10 y (J_vm.j5.2.1 y hy), ← capAtA_eq]; exact J_vm.j.2.1.cap y hy + have hvcRun10 : VcRunA st Z.T Z.V s10 := by + refine ⟨fun t ht => ?_, fun v hv => ?_, fun v hv => ?_, fun t ht => ?_, fun t ht hnz => ?_⟩ + · rw [hmem10_other _ (aligned_wa (hVcP.al_t t ht) 3664#u64 (by decide)) + (hVcP.disj_tcb vc hvc t ht 24#u64 3664#u64 (by decide) (by decide)).symm + (hVcP.disj_tcb vc hvc t ht 32#u64 3664#u64 (by decide) (by decide)).symm + (hVcP.disj_tcb vc hvc t ht 40#u64 3664#u64 (by decide) (by decide)).symm + (by unfold runVcpuA; exact field_ne (J.iplaced.e.tcb t ht) hta (by decide) (by decide) (fun _ => by decide))] + exact J_vm.vcRun.tcb t ht + · by_cases hvv : v = vc + · subst hvv; rw [hmem10_vc 24#u64 (Or.inl rfl)]; exact Or.inr hct + · rw [hmem10_other _ (aligned_wa (hVcP.vc_al hv) 24#u64 (by decide)) + (hVcP.disj_vcpu hv hvc hvv (by decide) (by decide)) + (hVcP.disj_vcpu hv hvc hvv (by decide) (by decide)) + (hVcP.disj_vcpu hv hvc hvv (by decide) (by decide)) + (by unfold runVcpuA; exact hVcP.disj_tcb v hv ct hct 24#u64 3672#u64 (by decide) (by decide))] + exact J_vm.vcRun.vcpu v hv + · rw [hmem10_other v (hVcP.vc_al hv) + (by by_cases h : v = vc + · subst h; simpa [wa_zeroR] using hVcP.same_vcpu_ne hv (o₁ := 0#u64) (o₂ := 24#u64) (by decide) (by decide) (by decide) + · simpa [wa_zeroR] using hVcP.disj_vcpu hv hvc h (ov₁ := 0#u64) (ov₂ := 24#u64) (by decide) (by decide)) + (by by_cases h : v = vc + · subst h; simpa [wa_zeroR] using hVcP.same_vcpu_ne hv (o₁ := 0#u64) (o₂ := 32#u64) (by decide) (by decide) (by decide) + · simpa [wa_zeroR] using hVcP.disj_vcpu hv hvc h (ov₁ := 0#u64) (ov₂ := 32#u64) (by decide) (by decide)) + (by by_cases h : v = vc + · subst h; simpa [wa_zeroR] using hVcP.same_vcpu_ne hv (o₁ := 0#u64) (o₂ := 40#u64) (by decide) (by decide) (by decide) + · simpa [wa_zeroR] using hVcP.disj_vcpu hv hvc h (ov₁ := 0#u64) (ov₂ := 40#u64) (by decide) (by decide)) + (by unfold runVcpuA; simpa [wa_zeroR] using hVcP.disj_tcb v hv ct hct 0#u64 3672#u64 (by decide) (by decide))] + exact J_vm.vcRun.vtcb v hv + · by_cases htt : t = ct + · subst htt; rw [hmem10_rv]; exact Or.inr hvc + · rw [hmem10_other (runVcpuA t) (by unfold runVcpuA; exact aligned_wa (hVcP.al_t t ht) 3672#u64 (by decide)) + (by unfold runVcpuA; exact (hVcP.disj_tcb vc hvc t ht 24#u64 3672#u64 (by decide) (by decide)).symm) + (by unfold runVcpuA; exact (hVcP.disj_tcb vc hvc t ht 32#u64 3672#u64 (by decide) (by decide)).symm) + (by unfold runVcpuA; exact (hVcP.disj_tcb vc hvc t ht 40#u64 3672#u64 (by decide) (by decide)).symm) + (by unfold runVcpuA; exact field_ne (J.iplaced.e.tcb t ht) hta (by decide) (by decide) (fun e _ => htt e))] + exact J_vm.vcRun.run t ht + · have ht3664 : L.mem s10 (wa t 3664#u64) = L.mem s_vm (wa t 3664#u64) := + hmem10_other _ (aligned_wa (hVcP.al_t t ht) 3664#u64 (by decide)) + (hVcP.disj_tcb vc hvc t ht 24#u64 3664#u64 (by decide) (by decide)).symm + (hVcP.disj_tcb vc hvc t ht 32#u64 3664#u64 (by decide) (by decide)).symm + (hVcP.disj_tcb vc hvc t ht 40#u64 3664#u64 (by decide) (by decide)).symm + (by unfold runVcpuA; exact field_ne (J.iplaced.e.tcb t ht) hta (by decide) (by decide) (fun _ => by decide)) + rw [ht3664] at hnz + by_cases htt : t = ct + · subst t + have h3664_s : L.mem s_vm (wa ct 3664#u64) = L.mem s (wa ct 3664#u64) := + mem_wr_neA hal_st (aligned_wa hal_t 3664#u64 (by decide)) (by rw [stA_eq]; exact field_ne hta hta (by decide) (by decide) (fun _ => by decide)) + exact absurd (h3664_s.trans htv0) hnz + · rw [hmem10_other (runVcpuA t) (by unfold runVcpuA; exact aligned_wa (hVcP.al_t t ht) 3672#u64 (by decide)) + (by unfold runVcpuA; exact (hVcP.disj_tcb vc hvc t ht 24#u64 3672#u64 (by decide) (by decide)).symm) + (by unfold runVcpuA; exact (hVcP.disj_tcb vc hvc t ht 32#u64 3672#u64 (by decide) (by decide)).symm) + (by unfold runVcpuA; exact (hVcP.disj_tcb vc hvc t ht 40#u64 3672#u64 (by decide) (by decide)).symm) + (by unfold runVcpuA; exact field_ne (J.iplaced.e.tcb t ht) hta (by decide) (by decide) (fun e _ => htt e))] + exact J_vm.vcRun.disj t ht hnz + have hrunVcpu10 : RunVcpuInv st Z.T s10 := fun t ht hne => by + by_cases htt : t = ct + · subst htt; exact hmem10_st + · rw [hmem10_other (runVcpuA t) (by unfold runVcpuA; exact aligned_wa (hVcP.al_t t ht) 3672#u64 (by decide)) + (by unfold runVcpuA; exact (hVcP.disj_tcb vc hvc t ht 24#u64 3672#u64 (by decide) (by decide)).symm) + (by unfold runVcpuA; exact (hVcP.disj_tcb vc hvc t ht 32#u64 3672#u64 (by decide) (by decide)).symm) + (by unfold runVcpuA; exact (hVcP.disj_tcb vc hvc t ht 40#u64 3672#u64 (by decide) (by decide)).symm) + (by unfold runVcpuA; exact field_ne (J.iplaced.e.tcb t ht) hta (by decide) (by decide) (fun e _ => htt e))] at hne + rw [hmem10_other (stA t) (by rw [stA_eq]; exact aligned_wa (hVcP.al_t t ht) 3328#u64 (by decide)) + (by rw [stA_eq]; exact (hVcP.disj_tcb vc hvc t ht 24#u64 3328#u64 (by decide) (by decide)).symm) + (by rw [stA_eq]; exact (hVcP.disj_tcb vc hvc t ht 32#u64 3328#u64 (by decide) (by decide)).symm) + (by rw [stA_eq]; exact (hVcP.disj_tcb vc hvc t ht 40#u64 3328#u64 (by decide) (by decide)).symm) + (by rw [stA_eq]; unfold runVcpuA; exact field_ne (J.iplaced.e.tcb t ht) hta (by decide) (by decide) (fun e _ => htt e))] + exact J_vm.runVcpu t ht hne + have hEpTcb10 : ∀ t ∈ Z.T, ∃ x, σ10.kheap (specPtr t) = some (.tcb x) ∧ TcbRelE Rp (L.mem s10) t x := fun t ht => by + obtain ⟨xt, hkt, hrel⟩ := J_vm.ep.tcb t ht + have hfr : TcbRelE Rp (L.mem s10) t xt := hrel.frame (J.iplaced.e.tcb t ht) (fun b hb htf => by + have hfit := (J.iplaced.e.tcb t ht).fits; have hal_u := (J.iplaced.e.tcb t ht).align + have hlt : b.val - t.val < 3664 := by have := htf.2; unfold TOffN at this; omega + let o : U64 := ⟨BitVec.ofNat 64 (b.val - t.val)⟩ + have ho_val : o.val = b.val - t.val := by + change (BitVec.ofNat 64 _).toNat = _; rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)] + have hb_eq : b = wa t o := UScalar.eq_of_val_eq (by rw [wa_valP, ho_val]; rw [Nat.add_sub_of_le htf.1]; exact (Nat.mod_eq_of_lt (by omega)).symm) + rw [hb_eq] + exact hmem10_other _ (hb_eq ▸ hb) + (hVcP.disj_tcb vc hvc t ht 24#u64 o (by decide) (by omega)).symm + (hVcP.disj_tcb vc hvc t ht 32#u64 o (by decide) (by omega)).symm + (hVcP.disj_tcb vc hvc t ht 40#u64 o (by decide) (by omega)).symm + (by unfold runVcpuA; have h3672 : (3672#u64 : U64).val = 3672 := rfl + exact field_ne (J.iplaced.e.tcb t ht) hta (by omega) (by decide) (fun _ e => by omega))) + rw [hσ10_eq] + by_cases htt : t = ct + · subst htt; rw [updKSc_same] at hkt; cases hkt + exact ⟨x1, updKSc_same _ _ _, ⟨hfr.1, hfr.2, hfr.3, hfr.4, hfr.5, hfr.6, hfr.7, hfr.8, hfr.9, hfr.10, hfr.11⟩⟩ + · have hne_c : specPtr t ≠ specPtr ct := fun e => htt (specPtr_inj e) + have hne_v : specPtr t ≠ specPtr vc := fun e => by rw [e, updKSc_other _ hne_vc_ct, hkv0] at hkt; cases hkt + exact ⟨xt, by rw [updKSc_other _ hne_c, updKSc_other _ hne_v]; exact hkt, hfr⟩ + have hEpBuf10 : ∀ bp ∈ Bf, ∀ k : U64, k.val < 128 → + (L.mem s10 (bufW bp.1 k)).bv = loadWordAt σ10.machineState.underlyingMemory (bufAddr bp.2 k.val) ∧ + (bufAddr bp.2 k.val).toNat % 8 = 0 := fun bp hbp k hk => by + have hbw : BufW Bf (bufW bp.1 k) := ⟨bp, hbp, k, hk, rfl⟩ + have hbal := J.iplaced.e.balign _ hbw + rw [hmem10_other _ hbal + (fun e => J.bufOff.vcw _ hbw vc hvc 24#u64 (by decide) (by decide) e) + (fun e => J.bufOff.vcw _ hbw vc hvc 32#u64 (by decide) (by decide) e) + (fun e => J.bufOff.vcw _ hbw vc hvc 40#u64 (by decide) (by decide) e) + (fun e => by + have hbt := J.iplaced.e.bt _ hbw ct hct + have h3672 : (3672#u64 : U64).val = 3672 := rfl + have ev : (runVcpuA ct).val = ct.val + 3672 := by unfold runVcpuA; rw [wa_valP, h3672]; exact Nat.mod_eq_of_lt (by have := hta.fits; omega) + rw [e, ev] at hbt; omega)] + exact J_vm.ep.buf bp hbp k hk + have osk_all : OSKeep st OS s s10 := + fun u hu => ⟨by rw [(osk_10 u hu).1, (osk_vm u hu).1], by rw [(osk_10 u hu).2.1, (osk_vm u hu).2.1], + by rw [(osk_10 u hu).2.2.1, (osk_vm u hu).2.2.1], by rw [(osk_10 u hu).2.2.2, (osk_vm u hu).2.2.2]⟩ + have hSnap10 : VcpuStateSnapA Z.V (L.mem s10) σ10 := by + rw [hσ10_eq] + refine ⟨fun v hv xv hkv => ?_, fun v hv xv hkv => ?_⟩ + · have hne_c : specPtr v ≠ specPtr ct := fun e => by rw [e, updKSc_same] at hkv; cases hkv + rw [updKSc_other _ hne_c] at hkv + have hreg_eq : ∀ i < 64, L.mem s10 (wa v ⟨BitVec.ofNat 64 (64 + 8 * i)⟩) = L.mem s_vm (wa v ⟨BitVec.ofNat 64 (64 + 8 * i)⟩) := fun i hi => by + change L.mem s10 (vcpuRegWA v i) = L.mem s_vm (vcpuRegWA v i) + have hne_o : ∀ o : U64, o.val < 64 → vcpuRegWA v i ≠ wa vc o := fun o ho => by + unfold vcpuRegWA + by_cases hvv : v = vc + · subst hvv; exact hVcP.same_vcpu_ne hv (by rw [vcpuRegOffA_val hi]; omega) (by omega) + (fun e => by have := congrArg UScalar.val e; rw [vcpuRegOffA_val hi] at this; omega) + · exact hVcP.disj_vcpu hv hvc hvv (by rw [vcpuRegOffA_val hi]; omega) (by omega) + exact hmem10_other _ (hVcP.vcPage_al (vcpuRegWA_in_vcPage hv hi)) + (hne_o 24#u64 (by decide)) (hne_o 32#u64 (by decide)) (hne_o 40#u64 (by decide)) + (by unfold vcpuRegWA runVcpuA; exact hVcP.disj_tcb v hv ct hct (vcpuRegOffA i) 3672#u64 (by rw [vcpuRegOffA_val hi]; omega) (by decide)) + have h16_eq : L.mem s10 (wa v 16#u64) = L.mem s_vm (wa v 16#u64) := + hmem10_other _ (aligned_wa (hVcP.vc_al hv) 16#u64 (by decide)) + (if h : v = vc then h ▸ hVcP.same_vcpu_ne hv (by decide) (by decide) (by decide) else hVcP.disj_vcpu hv hvc h (by decide) (by decide)) + (if h : v = vc then h ▸ hVcP.same_vcpu_ne hv (by decide) (by decide) (by decide) else hVcP.disj_vcpu hv hvc h (by decide) (by decide)) + (if h : v = vc then h ▸ hVcP.same_vcpu_ne hv (by decide) (by decide) (by decide) else hVcP.disj_vcpu hv hvc h (by decide) (by decide)) + (by unfold runVcpuA; exact hVcP.disj_tcb v hv ct hct 16#u64 3672#u64 (by decide) (by decide)) + have h8_eq : L.mem s10 (wa v 8#u64) = L.mem s_vm (wa v 8#u64) := + hmem10_other _ (aligned_wa (hVcP.vc_al hv) 8#u64 (by decide)) + (if h : v = vc then h ▸ hVcP.same_vcpu_ne hv (by decide) (by decide) (by decide) else hVcP.disj_vcpu hv hvc h (by decide) (by decide)) + (if h : v = vc then h ▸ hVcP.same_vcpu_ne hv (by decide) (by decide) (by decide) else hVcP.disj_vcpu hv hvc h (by decide) (by decide)) + (if h : v = vc then h ▸ hVcP.same_vcpu_ne hv (by decide) (by decide) (by decide) else hVcP.disj_vcpu hv hvc h (by decide) (by decide)) + (by unfold runVcpuA; exact hVcP.disj_tcb v hv ct hct 8#u64 3672#u64 (by decide) (by decide)) + by_cases hvv : v = vc + · subst hvv; rw [updKSc_same] at hkv; cases hkv + obtain ⟨hr0, hvp0, -, -, how0⟩ := R6_vm.snap.snap v hv v0 (by rw [updKSc_other _ hne_vc_ct]; exact hkv0) + refine ⟨fun i hi => by rw [hreg_eq i hi]; exact hr0 i hi, by rw [h16_eq]; exact hvp0, + by rw [hmem10_vc 32#u64 (Or.inr (Or.inl rfl))]; rfl, by rw [hmem10_vc 40#u64 (Or.inr (Or.inr rfl))]; rfl, + by rw [h8_eq]; exact how0⟩ + · rw [updKSc_other _ (fun e => hvv (specPtr_inj e))] at hkv + obtain ⟨hr0, hvp0, hro0, hrm0, how0⟩ := R6_vm.snap.snap v hv xv hkv + refine ⟨fun i hi => by rw [hreg_eq i hi]; exact hr0 i hi, by rw [h16_eq]; exact hvp0, + by rw [hmem10_other _ (aligned_wa (hVcP.vc_al hv) 32#u64 (by decide)) + (hVcP.disj_vcpu hv hvc hvv (by decide) (by decide)) (hVcP.disj_vcpu hv hvc hvv (by decide) (by decide)) + (hVcP.disj_vcpu hv hvc hvv (by decide) (by decide)) (by unfold runVcpuA; exact hVcP.disj_tcb v hv ct hct 32#u64 3672#u64 (by decide) (by decide))]; exact hro0, + by rw [hmem10_other _ (aligned_wa (hVcP.vc_al hv) 40#u64 (by decide)) + (hVcP.disj_vcpu hv hvc hvv (by decide) (by decide)) (hVcP.disj_vcpu hv hvc hvv (by decide) (by decide)) + (hVcP.disj_vcpu hv hvc hvv (by decide) (by decide)) (by unfold runVcpuA; exact hVcP.disj_tcb v hv ct hct 40#u64 3672#u64 (by decide) (by decide))]; exact hrm0, + by rw [h8_eq]; exact how0⟩ + · have hne_c : specPtr v ≠ specPtr ct := fun e => by rw [e, updKSc_same] at hkv; cases hkv + rw [updKSc_other _ hne_c] at hkv + by_cases hvv : v = vc + · subst hvv; rw [updKSc_same] at hkv; cases hkv; exact hout_ok + · rw [updKSc_other _ (fun e => hvv (specPtr_inj e))] at hkv; exact R6_vm.snap.mask v hv xv hkv + have R6_10 : VcpuRec6A st Z OS Bf s10 σ10 := by + refine ⟨hSnap10, fun t ht hnz => ?_, fun v hv hnz => ?_, fun v hv hnz => ?_, fun t ht hnz => ?_, + R6_vm.vcOS, + fun y hy p sz r mp ept asid va hc b hb => by + rw [hw_vm_10.1]; exact R6_vm.frOff y hy p sz r mp ept asid va (by rwa [← hcapE_10 y hy]) b hb, + R6_vm.userFr.keepA hcapE_10 (fun _ _ _ h => hHS_vm_10.keepObj h (fun _ e => by cases e) (fun _ e => by cases e)), + R6_vm.frTh.keepA hcapE_10 (fun _ _ e => e), + R6_vm.bfSpec⟩ + · have ht3664 : L.mem s10 (wa t 3664#u64) = L.mem s_vm (wa t 3664#u64) := + hmem10_other _ (aligned_wa (hVcP.al_t t ht) 3664#u64 (by decide)) + (hVcP.disj_tcb vc hvc t ht 24#u64 3664#u64 (by decide) (by decide)).symm + (hVcP.disj_tcb vc hvc t ht 32#u64 3664#u64 (by decide) (by decide)).symm + (hVcP.disj_tcb vc hvc t ht 40#u64 3664#u64 (by decide) (by decide)).symm + (by unfold runVcpuA; exact field_ne (J.iplaced.e.tcb t ht) hta (by decide) (by decide) (fun _ => by decide)) + rw [ht3664] at hnz ⊢ + have hv_m := (J_vm.vcRun.tcb t ht).resolve_left hnz + rw [hmem10_other _ (hVcP.vc_al hv_m) + (by by_cases h : L.mem s_vm (wa t 3664#u64) = vc + · rw [h]; simpa [wa_zeroR] using hVcP.same_vcpu_ne hvc (o₁ := 0#u64) (o₂ := 24#u64) (by decide) (by decide) (by decide) + · simpa [wa_zeroR] using hVcP.disj_vcpu hv_m hvc h (ov₁ := 0#u64) (ov₂ := 24#u64) (by decide) (by decide)) + (by by_cases h : L.mem s_vm (wa t 3664#u64) = vc + · rw [h]; simpa [wa_zeroR] using hVcP.same_vcpu_ne hvc (o₁ := 0#u64) (o₂ := 32#u64) (by decide) (by decide) (by decide) + · simpa [wa_zeroR] using hVcP.disj_vcpu hv_m hvc h (ov₁ := 0#u64) (ov₂ := 32#u64) (by decide) (by decide)) + (by by_cases h : L.mem s_vm (wa t 3664#u64) = vc + · rw [h]; simpa [wa_zeroR] using hVcP.same_vcpu_ne hvc (o₁ := 0#u64) (o₂ := 40#u64) (by decide) (by decide) (by decide) + · simpa [wa_zeroR] using hVcP.disj_vcpu hv_m hvc h (ov₁ := 0#u64) (ov₂ := 40#u64) (by decide) (by decide)) + (by unfold runVcpuA; simpa [wa_zeroR] using hVcP.disj_tcb _ hv_m ct hct 0#u64 3672#u64 (by decide) (by decide))] + exact R6_vm.vcSym1 t ht hnz + · by_cases hvv : v = vc + · subst hvv; rw [hmem10_vc 24#u64 (Or.inl rfl)]; exact hmem10_rv + · have h24 : L.mem s10 (wa v 24#u64) = L.mem s_vm (wa v 24#u64) := + hmem10_other _ (aligned_wa (hVcP.vc_al hv) 24#u64 (by decide)) + (hVcP.disj_vcpu hv hvc hvv (by decide) (by decide)) (hVcP.disj_vcpu hv hvc hvv (by decide) (by decide)) + (hVcP.disj_vcpu hv hvc hvv (by decide) (by decide)) (by unfold runVcpuA; exact hVcP.disj_tcb v hv ct hct 24#u64 3672#u64 (by decide) (by decide)) + rw [h24] at hnz ⊢ + have hc_m := (J_vm.vcRun.vcpu v hv).resolve_left hnz + have hnc : L.mem s_vm (wa v 24#u64) ≠ ct := fun e => by + have := R6_vm.vcSym2 v hv hnz; rw [e] at this + have hrv_s : L.mem s_vm (runVcpuA ct) = L.mem s (runVcpuA ct) := + mem_wr_neA hal_st hal_rv (by rw [stA_eq]; unfold runVcpuA; exact field_ne hta hta (by decide) (by decide) (fun _ => by decide)) + rw [hrv_s, hrv0] at this; exact (hVcP.al v hv).1 this.symm + rw [hmem10_other (runVcpuA _) (by unfold runVcpuA; exact aligned_wa (hVcP.al_t _ hc_m) 3672#u64 (by decide)) + (by unfold runVcpuA; exact (hVcP.disj_tcb vc hvc _ hc_m 24#u64 3672#u64 (by decide) (by decide)).symm) + (by unfold runVcpuA; exact (hVcP.disj_tcb vc hvc _ hc_m 32#u64 3672#u64 (by decide) (by decide)).symm) + (by unfold runVcpuA; exact (hVcP.disj_tcb vc hvc _ hc_m 40#u64 3672#u64 (by decide) (by decide)).symm) + (by unfold runVcpuA; exact field_ne (J.iplaced.e.tcb _ hc_m) hta (by decide) (by decide) (fun e _ => hnc e))] + exact R6_vm.vcSym2 v hv hnz + · have hv0_eq : L.mem s10 v = L.mem s_vm v := + hmem10_other v (hVcP.vc_al hv) + (by by_cases h : v = vc + · subst h; simpa [wa_zeroR] using hVcP.same_vcpu_ne hv (o₁ := 0#u64) (o₂ := 24#u64) (by decide) (by decide) (by decide) + · simpa [wa_zeroR] using hVcP.disj_vcpu hv hvc h (ov₁ := 0#u64) (ov₂ := 24#u64) (by decide) (by decide)) + (by by_cases h : v = vc + · subst h; simpa [wa_zeroR] using hVcP.same_vcpu_ne hv (o₁ := 0#u64) (o₂ := 32#u64) (by decide) (by decide) (by decide) + · simpa [wa_zeroR] using hVcP.disj_vcpu hv hvc h (ov₁ := 0#u64) (ov₂ := 32#u64) (by decide) (by decide)) + (by by_cases h : v = vc + · subst h; simpa [wa_zeroR] using hVcP.same_vcpu_ne hv (o₁ := 0#u64) (o₂ := 40#u64) (by decide) (by decide) (by decide) + · simpa [wa_zeroR] using hVcP.disj_vcpu hv hvc h (ov₁ := 0#u64) (ov₂ := 40#u64) (by decide) (by decide)) + (by unfold runVcpuA; simpa [wa_zeroR] using hVcP.disj_tcb v hv ct hct 0#u64 3672#u64 (by decide) (by decide)) + rw [hv0_eq] at hnz ⊢ + have ht_m := (J_vm.vcRun.vtcb v hv).resolve_left hnz + rw [hmem10_other _ (aligned_wa (hVcP.al_t _ ht_m) 3664#u64 (by decide)) + (hVcP.disj_tcb vc hvc _ ht_m 24#u64 3664#u64 (by decide) (by decide)).symm + (hVcP.disj_tcb vc hvc _ ht_m 32#u64 3664#u64 (by decide) (by decide)).symm + (hVcP.disj_tcb vc hvc _ ht_m 40#u64 3664#u64 (by decide) (by decide)).symm + (by unfold runVcpuA; exact field_ne (J.iplaced.e.tcb _ ht_m) hta (by decide) (by decide) (fun _ => by decide))] + exact R6_vm.vcBack v hv hnz + · by_cases htt : t = ct + · subst htt; rw [hmem10_rv]; exact hmem10_vc 24#u64 (Or.inl rfl) + · have hrv_eq : L.mem s10 (runVcpuA t) = L.mem s_vm (runVcpuA t) := + hmem10_other (runVcpuA t) (by unfold runVcpuA; exact aligned_wa (hVcP.al_t t ht) 3672#u64 (by decide)) + (by unfold runVcpuA; exact (hVcP.disj_tcb vc hvc t ht 24#u64 3672#u64 (by decide) (by decide)).symm) + (by unfold runVcpuA; exact (hVcP.disj_tcb vc hvc t ht 32#u64 3672#u64 (by decide) (by decide)).symm) + (by unfold runVcpuA; exact (hVcP.disj_tcb vc hvc t ht 40#u64 3672#u64 (by decide) (by decide)).symm) + (by unfold runVcpuA; exact field_ne (J.iplaced.e.tcb t ht) hta (by decide) (by decide) (fun e _ => htt e)) + rw [hrv_eq] at hnz ⊢ + have hv_m := (J_vm.vcRun.run t ht).resolve_left hnz + have hnv : L.mem s_vm (runVcpuA t) ≠ vc := fun e => by + have := R6_vm.runBack t ht hnz; rw [e] at this + have h24_s : L.mem s_vm (wa vc 24#u64) = L.mem s (wa vc 24#u64) := + mem_wr_neA hal_st hal_24 (by rw [stA_eq]; exact (hVcP.disj_tcb vc hvc ct hct 24#u64 3328#u64 (by decide) (by decide)).symm) + rw [h24_s, hrc0] at this; exact (J.iplaced.e.tcb t ht).ne0 (congrArg UScalar.val this.symm) + rw [hmem10_other _ (aligned_wa (hVcP.vc_al hv_m) 24#u64 (by decide)) + (hVcP.disj_vcpu hv_m hvc hnv (by decide) (by decide)) (hVcP.disj_vcpu hv_m hvc hnv (by decide) (by decide)) + (hVcP.disj_vcpu hv_m hvc hnv (by decide) (by decide)) (by unfold runVcpuA; exact hVcP.disj_tcb _ hv_m ct hct 24#u64 3672#u64 (by decide) (by decide))] + exact R6_vm.runBack t ht hnz + obtain ⟨Pt_vm, Ub_vm, Pp7_vm, Tb7_vm, Hr_vm, Te_vm, Kt_vm, Lv7_vm, Re_vm, bits7_vm, KLv_vm, W_vm⟩ := J7A.unp J_vm.j7 + have wv10 : WritesIn st s_vm s10 (VW st H.tcbs Z.Cs Z.V {b | b ∈ Ub_vm ∧ FrBw H OS b ∧ FrCw st OS s_vm b} (L.ksb s_vm) (fun _ => False)) := by + refine ⟨hw_vm_10.1, hw_vm_10.2.1, fun q hqa hnq => hmem10_other q hqa + (fun e => hnq (e ▸ Or.inr (Or.inl ⟨vc, hvc, 24#u64, by decide, by decide, rfl⟩))) + (fun e => hnq (e ▸ Or.inr (Or.inl ⟨vc, hvc, 32#u64, by decide, by decide, rfl⟩))) + (fun e => hnq (e ▸ Or.inr (Or.inl ⟨vc, hvc, 40#u64, by decide, by decide, rfl⟩))) + (fun e => hnq (e ▸ Or.inr (Or.inr (Or.inl ⟨ct, htH, Or.inr (Or.inr (Or.inl rfl))⟩))))⟩ + have hi10 : L.idleW s10 = L.idleW s_vm := by + unfold LawfulStore.idleW + have h8 : layout.node.IDLE_THREAD.val = 8 := by unfold layout.node.IDLE_THREAD; rfl + have hna8 := nodeAt_val (J_vm.sq.nodes _ J_vm.sr.sch.cpuCs) (by rw [h8]; decide) + have hal := nodeAt_aligned (J_vm.sq.nodes _ J_vm.sr.sch.cpuCs) (by rw [h8]; decide) (by rw [h8]) + rw [hw_vm_10.2.1, nodeAt_of_ksb hw_vm_10.1] + have hcpu128 : (L.cpu s_vm).val < 128 := by + obtain ⟨nn, -, hkn, pcs⟩ := J_vm.sr.toSchedSRN.csN; have := (pcs _).1 J_vm.sr.sch.cpuCs; omega + have hfit := J_vm.sys.ent.ksw + have hVcP_vm : VcPlacedA Z OS Ub (L.ksb s_vm) := by rwa [show L.ksb s_vm = L.ksb s from L.ksb_wr _ _ _] + have hks_off : ∀ o : U64, o.val < 64 → o.val % 8 = 0 → L.nodeAt s_vm (L.cpu s_vm) layout.node.IDLE_THREAD ≠ wa vc o := fun o ho ho8 e => + hVcP_vm.disj_ks _ ⟨vc, hvc, o, by omega, ho8, rfl⟩ ⟨by rw [← e, hna8, h8]; unfold LawfulStore.nodeBase; omega, + by rw [← e, hna8, h8]; unfold LawfulStore.nodeBase ksSizeA; omega⟩ + exact hmem10_other _ hal (hks_off 24#u64 (by decide) (by decide)) (hks_off 32#u64 (by decide) (by decide)) + (hks_off 40#u64 (by decide) (by decide)) (fun e => by + have hd := (J_vm.sq.nodes _ J_vm.sr.sch.cpuCs).disj ct htH + have h3672 : (3672#u64 : U64).val = 3672 := rfl + have hv1 : (runVcpuA ct).val = ct.val + 3672 := by unfold runVcpuA; rw [wa_valm, h3672]; exact Nat.mod_eq_of_lt (by have := hta.fits; omega) + have hv := congrArg UScalar.val e; rw [hna8, h8, hv1] at hv; omega) + obtain ⟨sysl10, symL10⟩ := sysL_symL_of_vwA J_vm W_vm wv10 hi10 (fun _ h => h.elim) + have hZ10 : ({ Z with hw := Z.hw } : XSetsA) = Z := by cases Z; rfl + have J10 : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s10 a4 σ10 m := by + have h := JStage3A.of_vcpuStepA (hw' := Z.hw) J_vm hNoSw_vm R6_vm hw_vm_10 hHS_vm_10 hAR' + (fun b hb => hw_vm_10.mem_eq (J_vm.vsOk.al b hb) (utVs_off_vcpuWrCoreA (J_vm.toVcpuCoreA hNoSw_vm) hb)) + (hZ10.symm ▸ hcore10) hvcRun10 hrunVcpu10 hEpTcb10 hEpBuf10 + (fun v hv hd xv hxv => by + have hd0 : DeadA st OS .vcpu s_vm v := fun u hu => by rw [← hcapE_10 u hu]; exact hd u hu + by_cases hvv : v = vc + · subst hvv; exfalso; exact hvc_live (fun u hu => by rw [← osk_all.capA hu]; exact hd u hu) + · rw [hσ10_eq] at hxv + have hne_c : specPtr v ≠ specPtr ct := fun e => by rw [e, updKSc_same] at hxv; cases hxv + rw [updKSc_other _ hne_c, updKSc_other _ (fun e => hvv (specPtr_inj e))] at hxv + exact J_vm.deadExt.vcpu v hv hd0 xv hxv) + (fun t ht hd => by + rw [hmem10_other _ (aligned_wa (hVcP.al_t t ht) 3664#u64 (by decide)) + (hVcP.disj_tcb vc hvc t ht 24#u64 3664#u64 (by decide) (by decide)).symm + (hVcP.disj_tcb vc hvc t ht 32#u64 3664#u64 (by decide) (by decide)).symm + (hVcP.disj_tcb vc hvc t ht 40#u64 3664#u64 (by decide) (by decide)).symm + (by unfold runVcpuA; exact field_ne (J.iplaced.e.tcb t ht) hta (by decide) (by decide) (fun _ => by decide))] + exact J_vm.deadExt.tvcpu t ht (fun u hu => by rw [← hcapE_10 u hu]; exact hd u hu)) + (fun v hv hd => by + by_cases hvv : v = vc + · subst hvv; exfalso; exact hvc_live (fun u hu => by rw [← osk_all.capA hu]; exact hd u hu) + · rw [hmem10_other _ (aligned_wa (hVcP.vc_al hv) 24#u64 (by decide)) + (hVcP.disj_vcpu hv hvc hvv (by decide) (by decide)) + (hVcP.disj_vcpu hv hvc hvv (by decide) (by decide)) + (hVcP.disj_vcpu hv hvc hvv (by decide) (by decide)) + (by unfold runVcpuA; exact hVcP.disj_tcb v hv ct hct 24#u64 3672#u64 (by decide) (by decide))] + exact J_vm.deadExt.vcDeadRun v hv (fun u hu => by rw [← hcapE_10 u hu]; exact hd u hu)) + (fun z hz u num hc => by + have hc0 : capA st s_vm z = .Zombie u capability.ZOMBIE_TCB num := by rw [← hcapE_10 z hz]; exact hc + have huT : u ∈ Z.T := J_vm.tcbs ▸ J_vm.zombieTcb z hz u num hc0 + rw [hmem10_other _ (aligned_wa (hVcP.al_t u huT) 3664#u64 (by decide)) + (hVcP.disj_tcb vc hvc u huT 24#u64 3664#u64 (by decide) (by decide)).symm + (hVcP.disj_tcb vc hvc u huT 32#u64 3664#u64 (by decide) (by decide)).symm + (hVcP.disj_tcb vc hvc u huT 40#u64 3664#u64 (by decide) (by decide)).symm + (by unfold runVcpuA; exact field_ne (J.iplaced.e.tcb u huT) hta (by decide) (by decide) (fun _ => by decide))] + exact J_vm.zombieNoVcpu z hz u num hc0) + (SchedValidA.of_vcpuStepA J_vm.schedValid hHS_vm_10) (hZ10.symm ▸ R6_10) sysl10 symL10 + rwa [hZ10] at h + -- Spec step + have hsp10 : (do + fl4VcpuExtUpdate (Z := Unit) (specPtr vc) (fun e => + { e with runCaller := some (specPtr ct), runOffset := a0.bv, runOutMask := a2.bv }) + setRunVcpu (specPtr ct) (some (specPtr vc)) + setThreadState (specPtr ct) (.ext .runningVM)) σ = ({((), σ10)}, False) := by + have h1 : fl4VcpuExtUpdate (Z := Unit) (specPtr vc) (fun e => { e with runCaller := some (specPtr ct), runOffset := a0.bv, runOutMask := a2.bv }) σ = + ({((), updKSc σ (specPtr vc) (.archObj (.vcpu v1)))}, False) := by + unfold fl4VcpuExtUpdate; rw [nd_bind_oneSc (getVcpu_eqA hkv0)]; exact setVcpu_eqA hkv0 + have hkt1 : (updKSc σ (specPtr vc) (.archObj (.vcpu v1))).kheap (specPtr ct) = some (.tcb x0) := by + rw [updKSc_other _ hne_vc_ct.symm]; exact hkt0 + have h2 : setRunVcpu (Z := Unit) (specPtr ct) (some (specPtr vc)) (updKSc σ (specPtr vc) (.archObj (.vcpu v1))) = + ({((), updKSc (updKSc σ (specPtr vc) (.archObj (.vcpu v1))) (specPtr ct) (.tcb { x0 with tcbExt := { x0.tcbExt with tcbRunVcpu := some (specPtr vc) } }))}, False) := + setRunVcpu_eqA (some (specPtr vc)) hkt1 + have h3 : setThreadState (Z := Unit) (specPtr ct) (.ext .runningVM) + (updKSc (updKSc σ (specPtr vc) (.archObj (.vcpu v1))) (specPtr ct) (.tcb { x0 with tcbExt := { x0.tcbExt with tcbRunVcpu := some (specPtr vc) } })) = + ({((), σ10)}, False) := by + have hst_eq : stUpdA (updKSc (updKSc σ (specPtr vc) (.archObj (.vcpu v1))) (specPtr ct) (.tcb { x0 with tcbExt := { x0.tcbExt with tcbRunVcpu := some (specPtr vc) } })) + (specPtr ct) { x0 with tcbExt := { x0.tcbExt with tcbRunVcpu := some (specPtr vc) } } (.ext .runningVM) = σ10 := by + unfold stUpdA setObjA σ10; exact updKSc_idemR _ _ _ _ + rw [← hst_eq] + apply setThreadState_eqA _ (specPtr ct) _ (updKSc_same _ _ _) (.ext .runningVM) + intro _ _ + obtain ⟨x0', hkt0', -, hsc_eq⟩ := J.j.1.sch.tcb ct hct + rw [hkt0] at hkt0'; cases hkt0' + have hsc_some : x0.tcbSchedContext = some (scRenA m (L.mem s (scA ct))) := by rw [hsc_eq]; unfold optScA; simp [hsc] + obtain ⟨y, n0, hy, hrm_eq⟩ := J.j.1.rest.refillMax _ hscC + have hne_sc : scRenA m (L.mem s (scA ct)) ≠ specPtr ct := fun e => by rw [e, hkt0] at hy; cases hy + have hne_sv : scRenA m (L.mem s (scA ct)) ≠ specPtr vc := fun e => by rw [e, hkv0] at hy; cases hy + have hrm_eq' : y.scRefillMax = (L.rmax s (L.mem s (scA ct))).val := by + rw [hrm_eq, LawfulStore.rmax, maxA_eqS]; rfl + have hpos : 0 < y.scRefillMax := by + by_contra h0; exact hrm (UScalar.eq_of_val_eq (by change (L.rmax s _).val = 0; omega)) + exact schedulable_ofA (updKSc_same _ _ _) rfl hsc_some + (by simp only [setObjA, Function.update_of_ne hne_sc, updKSc_other _ hne_sc, updKSc_other _ hne_sv]; exact hy) hpos hNotRel + rw [nd_bind_oneSc h1, nd_bind_oneSc h2]; exact h3 + have hsp10_r : (do + fl4VcpuExtUpdate (Z := Unit) (specPtr vc) (fun e => + { e with runCaller := some (specPtr ct), runOffset := a0.bv, runOutMask := a2.bv }) + setRunVcpu (specPtr ct) (some (specPtr vc)) + setThreadState (specPtr ct) (.ext .runningVM)) (updKSc σ (specPtr ct) (.tcb { x0 with tcbState := .restart })) = ({((), σ10)}, False) := by + have hkv_r : (updKSc σ (specPtr ct) (.tcb { x0 with tcbState := .restart })).kheap (specPtr vc) = some (.archObj (.vcpu v0)) := by + rw [updKSc_other _ hne_vc_ct]; exact hkv0 + have h1 : fl4VcpuExtUpdate (Z := Unit) (specPtr vc) (fun e => { e with runCaller := some (specPtr ct), runOffset := a0.bv, runOutMask := a2.bv }) + (updKSc σ (specPtr ct) (.tcb { x0 with tcbState := .restart })) = + ({((), updKSc (updKSc σ (specPtr ct) (.tcb { x0 with tcbState := .restart })) (specPtr vc) (.archObj (.vcpu v1)))}, False) := by + unfold fl4VcpuExtUpdate; rw [nd_bind_oneSc (getVcpu_eqA hkv_r)]; exact setVcpu_eqA hkv_r + have hkt_r : (updKSc (updKSc σ (specPtr ct) (.tcb { x0 with tcbState := .restart })) (specPtr vc) (.archObj (.vcpu v1))).kheap (specPtr ct) = + some (.tcb { x0 with tcbState := .restart }) := by + rw [updKSc_other _ hne_vc_ct.symm, updKSc_same] + have h2 : setRunVcpu (Z := Unit) (specPtr ct) (some (specPtr vc)) + (updKSc (updKSc σ (specPtr ct) (.tcb { x0 with tcbState := .restart })) (specPtr vc) (.archObj (.vcpu v1))) = + ({((), updKSc (updKSc σ (specPtr vc) (.archObj (.vcpu v1))) (specPtr ct) (.tcb { x0 with tcbState := .restart, tcbExt := { x0.tcbExt with tcbRunVcpu := some (specPtr vc) } }))}, False) := by + have h2_raw := setRunVcpu_eqA (some (specPtr vc)) hkt_r + have hcomm : updKSc (updKSc (updKSc σ (specPtr ct) (.tcb { x0 with tcbState := .restart })) (specPtr vc) (.archObj (.vcpu v1))) (specPtr ct) + (.tcb { x0 with tcbState := .restart, tcbExt := { x0.tcbExt with tcbRunVcpu := some (specPtr vc) } }) = + updKSc (updKSc σ (specPtr vc) (.archObj (.vcpu v1))) (specPtr ct) + (.tcb { x0 with tcbState := .restart, tcbExt := { x0.tcbExt with tcbRunVcpu := some (specPtr vc) } }) := by + rw [updKSc_commR σ hne_vc_ct.symm, updKSc_idemR] + rw [hcomm] at h2_raw + exact h2_raw + have h3 : setThreadState (Z := Unit) (specPtr ct) (.ext .runningVM) + (updKSc (updKSc σ (specPtr vc) (.archObj (.vcpu v1))) (specPtr ct) (.tcb { x0 with tcbState := .restart, tcbExt := { x0.tcbExt with tcbRunVcpu := some (specPtr vc) } })) = + ({((), σ10)}, False) := by + have hst_eq : stUpdA (updKSc (updKSc σ (specPtr vc) (.archObj (.vcpu v1))) (specPtr ct) (.tcb { x0 with tcbState := .restart, tcbExt := { x0.tcbExt with tcbRunVcpu := some (specPtr vc) } })) + (specPtr ct) { x0 with tcbState := .restart, tcbExt := { x0.tcbExt with tcbRunVcpu := some (specPtr vc) } } (.ext .runningVM) = σ10 := by + unfold stUpdA setObjA σ10; exact updKSc_idemR _ _ _ _ + rw [← hst_eq] + apply setThreadState_eqA _ (specPtr ct) _ (updKSc_same _ _ _) (.ext .runningVM) + intro _ _ + obtain ⟨x0', hkt0', -, hsc_eq⟩ := J.j.1.sch.tcb ct hct + rw [hkt0] at hkt0'; cases hkt0' + have hsc_some : x0.tcbSchedContext = some (scRenA m (L.mem s (scA ct))) := by rw [hsc_eq]; unfold optScA; simp [hsc] + obtain ⟨y, n0, hy, hrm_eq⟩ := J.j.1.rest.refillMax _ hscC + have hne_sc : scRenA m (L.mem s (scA ct)) ≠ specPtr ct := fun e => by rw [e, hkt0] at hy; cases hy + have hne_sv : scRenA m (L.mem s (scA ct)) ≠ specPtr vc := fun e => by rw [e, hkv0] at hy; cases hy + have hrm_eq' : y.scRefillMax = (L.rmax s (L.mem s (scA ct))).val := by + rw [hrm_eq, LawfulStore.rmax, maxA_eqS]; rfl + have hpos : 0 < y.scRefillMax := by + by_contra h0; exact hrm (UScalar.eq_of_val_eq (by change (L.rmax s _).val = 0; omega)) + exact schedulable_ofA (updKSc_same _ _ _) rfl hsc_some + (by simp only [setObjA, Function.update_of_ne hne_sc, updKSc_other _ hne_sc, updKSc_other _ hne_sv]; exact hy) hpos hNotRel + rw [nd_bind_oneSc h1, nd_bind_oneSc h2]; exact h3 + have hksb10_eq : L.ksb s10 = L.ksb s := by simp only [s10, s8, s7, s6, s5, L.ksb_wr] + exact ⟨σ10, hsp10, hsp10_r, J10, hNoSw_vm, R6_10, osk_all, hksb10_eq, hmem10_st, hmem10_rv⟩ + +private theorem ntfn_set_idle_ipcA {ar : machine.Arch A} {nA : U64} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s a σ m) + {nf : U64} (hnf : nf ∈ N) (hnfH : H nf = some .ntfn) + (hnfAl : nf.val % 64 = 0) (hnfFit : nf.val + 64 ≤ 2 ^ 64) + {ntfn : Notification} (hkn : σ.kheap (specPtr nf) = some (.notification ntfn)) + {badge : Fl4SpecA64.Word} (hact : ntfn.ntfnObj = .activeNtfn badge) + (hal_ep : Aligned (epTSA nf)) (hval_ep : (epTSA nf).val = nf.val + 8) + (hNonNtfn : ∀ p k, H p = some k → (k = .ntfn → p ≠ nf) → + ∀ b, Aligned b → p.val ≤ b.val → b.val < p.val + 2 ^ k.bits → + L.mem (L.wr s (epTSA nf) ((L.mem s (epTSA nf) &&& ~~~3#u64) ||| (obj.NTFN_IDLE &&& 3#u64))) b = L.mem s b) + (hTcbEq : ∀ t ∈ Z.T, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → + L.mem (L.wr s (epTSA nf) ((L.mem s (epTSA nf) &&& ~~~3#u64) ||| (obj.NTFN_IDLE &&& 3#u64))) (wa t o) = L.mem s (wa t o)) : + let s1 := L.wr s (epTSA nf) ((L.mem s (epTSA nf) &&& ~~~3#u64) ||| (obj.NTFN_IDLE &&& 3#u64)) + let σ1 := updKSc σ (specPtr nf) (.notification { ntfn with ntfnObj := .idleNtfn }) + EpStateRel Z.T E Rp Bf (L.mem s1) σ1 ∧ + NtfnStateRel N Z.T (L.mem s1) σ1 ∧ + ReplyStackRelA (L.mem s1) Rp Z.C σ1 (scNameA m) stack ∧ + BufCover st Z.T Bf s1 σ1 ∧ + UtBufAlA Z.T σ1 ∧ + IdleNoFaultA m σ1 ∧ + McpRelA Z.T m (L.mem s1) σ1 ∧ + YieldRelA Z.T Z.C m (L.mem s1) σ1 := by + intro s1 σ1 + have hep' : EpStateRel Z.T E Rp Bf (L.mem s1) σ1 := by + refine ⟨fun e he => ?_, fun r hr => ?_, fun t ht => ?_, fun bp hbp k hk => ?_⟩ + · obtain ⟨x, hx, hr⟩ := J.ep.ep e he + have heH : H e = some .ep := J.utIpc.ep e he + have hfit := J.sys.ent.heap.fits e .ep heH; have hal := J.sys.ent.heap.align e .ep heH + change e.val + 16 ≤ 2 ^ 64 at hfit; change e.val % 16 = 0 at hal + have h0 : L.mem s1 e = L.mem s e := + hNonNtfn e .ep heH (fun h => by cases h) e (by show e.val % 8 = 0; omega) (le_refl _) (by change e.val < e.val + 16; omega) + have h8 : L.mem s1 (wa e 8#u64) = L.mem s (wa e 8#u64) := by + have h8v : (8#u64 : U64).val = 8 := rfl + have ev : (wa e 8#u64).val = e.val + 8 := by rw [wa_valm, h8v]; exact Nat.mod_eq_of_lt (by omega) + exact hNonNtfn e .ep heH (fun h => by cases h) _ (by show _ % 8 = 0; rw [ev]; omega) (by omega) (by change _ < e.val + 16; omega) + have hne : specPtr e ≠ specPtr nf := fun eq => by rw [eq, hkn] at hx; cases hx + refine ⟨x, by rw [updKSc_other _ hne]; exact hx, + hr.frame (by unfold epHeadA layout.ep.HEAD; rwa [wa_zeroR]) (by rw [epTSA_eq]; exact h8) (fun y hy _ => + ⟨by rw [stA_eq_wa]; exact hTcbEq y hy 3328#u64 (by decide) (by decide), + by rw [tBlockA_eq]; exact hTcbEq y hy 3336#u64 (by decide) (by decide), + ⟨by rw [prevA_eq']; exact hTcbEq y hy _ (by simp [prevOff]) (by simp [prevOff]), + by rw [nextA_eq']; exact hTcbEq y hy _ (by simp [nextOff]) (by simp [nextOff])⟩⟩)⟩ + · obtain ⟨xr, hxr, hrt⟩ := J.ep.reply r hr + have hrH : H r = some .reply := J.utIpc.reply r hr + have hal := J.sys.ent.heap.align r .reply hrH; change r.val % 32 = 0 at hal + have hne_r : specPtr r ≠ specPtr nf := fun h => by rw [h, hkn] at hxr; cases hxr + have hrr : L.mem s1 (Endp.rTcbA r) = L.mem s (Endp.rTcbA r) := by + have hr0 : Endp.rTcbA r = r := by unfold Endp.rTcbA layout.reply.TCB; exact wa_zeroR r + rw [hr0] + exact hNonNtfn r .reply hrH (fun h => by cases h) r (by show r.val % 8 = 0; omega) (le_refl _) (by change r.val < r.val + 32; omega) + refine ⟨xr, by rw [updKSc_other _ hne_r]; exact hxr, by rw [hrr]; exact hrt⟩ + · obtain ⟨x, hx, hr⟩ := J.ep.tcb t ht + have hne : specPtr t ≠ specPtr nf := fun eq => by rw [eq, hkn] at hx; cases hx + refine ⟨x, by rw [updKSc_other _ hne]; exact hx, hr.frame (J.iplaced.e.tcb t ht) (fun b hba hb => ?_)⟩ + have htH : H t = some .tcb := by change t ∈ H.tcbs; rw [J.tcbs]; exact ht + have hlt4096 : b.val - t.val < 4096 := by + have hto := hb.2; unfold TOffN at hto + rcases hto with h | h | h | h | h | h | h | h | ⟨_, _⟩ | h | h | h | h | h | h <;> omega + exact hNonNtfn t .tcb htH (fun h => by cases h) b hba hb.1 (by change b.val < t.val + 4096; omega) + · have hbw : BufW Bf (bufW bp.1 k) := ⟨bp, hbp, k, hk, rfl⟩ + have hnb := J.iplaced.nb nf hnf (bufW bp.1 k) hbw + rw [mem_wr_neA hal_ep (J.iplaced.e.balign _ hbw) (fun e => by have := congrArg UScalar.val e; rw [hval_ep] at this; omega)] + exact J.ep.buf bp hbp k hk + have hntfn' : NtfnStateRel N Z.T (L.mem s1) σ1 := by + refine ⟨fun q hq => ?_, fun t ht => ?_⟩ + · by_cases hqn : q = nf + · rcases hqn.symm with rfl + obtain ⟨x0, hx0, hr0⟩ := J.ntfn.ntfn nf hnf + rw [hkn] at hx0; cases hx0 + refine ⟨{ ntfn with ntfnObj := .idleNtfn }, updKSc_same _ _ _, ?_⟩ + have hobj0 := hr0.obj; rw [hact] at hobj0 + have hval_wr : L.mem s1 (epTSA nf) = (L.mem s (epTSA nf) &&& ~~~3#u64) ||| (obj.NTFN_IDLE &&& 3#u64) := + L.mem_wr_same hal_ep + have hst0 : stOf (L.mem s1) nf = 0#u64 := by + unfold stOf; rw [hval_wr] + apply u64_eq_of_bv_eqR; unfold obj.NTFN_IDLE + exact bv_clear_lo2_and3 (L.mem s (epTSA nf)).bv + have hq0 : qOf (L.mem s1) nf = qOf (L.mem s) nf := by + unfold qOf + have hhead : L.mem s1 (epHeadA nf) = L.mem s (epHeadA nf) := by + have hh0 : epHeadA nf = nf := by unfold epHeadA layout.ep.HEAD; exact wa_zeroR nf + rw [hh0] + exact mem_wr_neA hal_ep (by unfold Aligned; omega) (by + intro e; have := congrArg UScalar.val e; rw [hval_ep] at this; omega) + have htail : L.mem s1 (epTSA nf) &&& ~~~3#u64 = L.mem s (epTSA nf) &&& ~~~3#u64 := by + rw [hval_wr]; apply u64_eq_of_bv_eqR; unfold obj.NTFN_IDLE + exact bv_clear_lo2_andnot3 (L.mem s (epTSA nf)).bv + rw [hhead, htail] + have hneq_off : ∀ o : U64, o.val < 64 → o.val % 8 = 0 → o.val ≠ 8 → L.mem s1 (wa nf o) = L.mem s (wa nf o) := fun o ho ho8 hne8 => + mem_wr_neA hal_ep (aligned_wa (by unfold Aligned; omega) o ho8) (by + intro e; have := congrArg UScalar.val e + rw [hval_ep, wa_valP, Nat.mod_eq_of_lt (by omega)] at this; omega) + have hnb : L.mem s1 (nBoundA nf) = L.mem s (nBoundA nf) := by rw [nBoundA_eq]; exact hneq_off 24#u64 (by decide) (by decide) (by decide) + have hns : L.mem s1 (nScA nf) = L.mem s (nScA nf) := by rw [nScA_eq]; exact hneq_off 32#u64 (by decide) (by decide) (by decide) + have hht_ep : ∀ t ∈ Z.T, L.mem s1 (nextA .Ep t) = L.mem s (nextA .Ep t) ∧ L.mem s1 (prevA .Ep t) = L.mem s (prevA .Ep t) := fun t ht => + ⟨by rw [nextEp_eq]; exact hTcbEq t ht 3496#u64 (by decide) (by decide), + by rw [prevEp_eq]; exact hTcbEq t ht 3504#u64 (by decide) (by decide)⟩ + refine ⟨⟨hst0, by rw [hq0]; exact ⟨hobj0.2.1.nodup, hobj0.2.1.mem, hobj0.2.1.head, hobj0.2.1.last, hobj0.2.1.seg.congr fun y hy => ⟨(hht_ep y (hobj0.2.1.mem y hy)).2, (hht_ep y (hobj0.2.1.mem y hy)).1⟩⟩⟩, + (by rw [hnb]; exact hr0.bound), (by rw [hnb]; exact hr0.bound_mem), (by rw [hns]; exact hr0.sc)⟩ + · obtain ⟨x, hx, hr⟩ := J.ntfn.ntfn q hq + have hne : specPtr q ≠ specPtr nf := fun e => hqn (specPtr_inj e) + have hqH : H q = some .ntfn := J.utIpc.ntfn q hq + have hqAl : q.val % 64 = 0 := J.sys.ent.heap.align q .ntfn hqH + have hqFit : q.val + 64 ≤ 2 ^ 64 := J.sys.ent.heap.fits q .ntfn hqH + have hqw : ∀ o : U64, o.val < 64 → o.val % 8 = 0 → L.mem s1 (wa q o) = L.mem s (wa q o) := fun o ho ho8 => by + have hev : (wa q o).val = q.val + o.val := by rw [wa_valP]; exact Nat.mod_eq_of_lt (by omega) + exact hNonNtfn q .ntfn hqH (fun _ => hqn) (wa q o) (aligned_wa (by unfold Aligned; omega) o ho8) (by omega) (by change _ < q.val + 64; omega) + refine ⟨x, by rw [updKSc_other _ hne]; exact hx, ?_⟩ + have hstq : stOf (L.mem s1) q = stOf (L.mem s) q := by unfold stOf; rw [epTSA_eq q, hqw 8#u64 (by decide) (by decide)] + have hqq : qOf (L.mem s1) q = qOf (L.mem s) q := by + unfold qOf; rw [show epHeadA q = wa q 0#u64 from by unfold epHeadA layout.ep.HEAD; rfl, hqw 0#u64 (by decide) (by decide), + epTSA_eq q, hqw 8#u64 (by decide) (by decide)] + have hbq : L.mem s1 (nBadgeA q) = L.mem s (nBadgeA q) := by rw [nBadgeA_eq]; exact hqw 16#u64 (by decide) (by decide) + have hbdq : L.mem s1 (nBoundA q) = L.mem s (nBoundA q) := by rw [nBoundA_eq]; exact hqw 24#u64 (by decide) (by decide) + have hscq : L.mem s1 (nScA q) = L.mem s (nScA q) := by rw [nScA_eq]; exact hqw 32#u64 (by decide) (by decide) + have ht_ep : ∀ t ∈ Z.T, L.mem s1 (nextA .Ep t) = L.mem s (nextA .Ep t) ∧ L.mem s1 (prevA .Ep t) = L.mem s (prevA .Ep t) := fun t ht => + ⟨by rw [nextEp_eq]; exact hTcbEq t ht 3496#u64 (by decide) (by decide), + by rw [prevEp_eq]; exact hTcbEq t ht 3504#u64 (by decide) (by decide)⟩ + refine ⟨?_, (by rw [hbdq]; exact hr.bound), (by rw [hbdq]; exact hr.bound_mem), (by rw [hscq]; exact hr.sc)⟩ + cases ho : x.ntfnObj with + | idleNtfn => + have h0 := hr.obj; rw [ho] at h0 + exact ⟨(by rw [hstq]; exact h0.1), (by rw [hqq]; exact ⟨h0.2.nodup, h0.2.mem, h0.2.head, h0.2.last, h0.2.seg.congr fun y hy => ⟨(ht_ep y (h0.2.mem y hy)).2, (ht_ep y (h0.2.mem y hy)).1⟩⟩)⟩ + | activeNtfn b => + have h0 := hr.obj; rw [ho] at h0 + exact ⟨(by rw [hstq]; exact h0.1), (by rw [hqq]; exact ⟨h0.2.1.nodup, h0.2.1.mem, h0.2.1.head, h0.2.1.last, h0.2.1.seg.congr fun y hy => ⟨(ht_ep y (h0.2.1.mem y hy)).2, (ht_ep y (h0.2.1.mem y hy)).1⟩⟩), (by rw [hbq]; exact h0.2.2)⟩ + | waitingNtfn xs => + have h0 := hr.obj; rw [ho] at h0 + obtain ⟨hs0, ys, hne_y, hxs, hqr, hblk_y⟩ := h0 + refine ⟨(by rw [hstq]; exact hs0), ys, hne_y, hxs, (by rw [hqq]; exact ⟨hqr.nodup, hqr.mem, hqr.head, hqr.last, hqr.seg.congr fun y hy => ⟨(ht_ep y (hqr.mem y hy)).2, (ht_ep y (hqr.mem y hy)).1⟩⟩), fun y hy => ?_⟩ + have hyT := hqr.mem y hy + have hb0 := hblk_y y hy + exact ⟨(by rw [stA_eq_wa, hTcbEq y hyT 3328#u64 (by decide) (by decide), ← stA_eq_wa]; exact hb0.1), + (by rw [tBlockA_eq, hTcbEq y hyT 3336#u64 (by decide) (by decide), ← tBlockA_eq]; exact hb0.2)⟩ + · obtain ⟨x, hx, hr⟩ := J.ntfn.tcb t ht + have hne : specPtr t ≠ specPtr nf := fun e => by rw [e, hkn] at hx; cases hx + refine ⟨x, by rw [updKSc_other _ hne]; exact hx, + ⟨(by rw [stA_eq_wa, hTcbEq t ht 3328#u64 (by decide) (by decide), ← stA_eq_wa]; exact hr.st), + (fun n0 hn0 => by rw [tBlockA_eq, hTcbEq t ht 3336#u64 (by decide) (by decide), ← tBlockA_eq]; exact hr.blk n0 hn0), + (by rw [badgeRegA_eq_256, hTcbEq t ht 256#u64 (by decide) (by decide), ← badgeRegA_eq_256]; exact hr.badge), + (by rw [prioA_eq', hTcbEq t ht 3448#u64 (by decide) (by decide), ← prioA_eq']; exact hr.prio)⟩⟩ + have hreply' : ReplyStackRelA (L.mem s1) Rp Z.C σ1 (scNameA m) stack := by + refine J.reply.frame (fun r hr => ?_) (fun c hc y k h => by + have hne : scNameA m c ≠ specPtr nf := fun e => by rw [e, hkn] at h; cases h + exact ⟨y, k, by rw [updKSc_other _ hne]; exact h, rfl⟩) (fun r hr x h => by + have hne : specPtr r ≠ specPtr nf := fun e => by rw [e, updKSc_same] at h; cases h + exact ⟨x, by rwa [updKSc_other _ hne] at h, rfl⟩) + have hrH : H r = some .reply := J.utIpc.reply r hr + have hrAl : r.val % 32 = 0 := J.sys.ent.heap.align r .reply hrH + have hrFit : r.val + 32 ≤ 2 ^ 64 := J.sys.ent.heap.fits r .reply hrH + have hrw : ∀ o : U64, o.val < 32 → o.val % 8 = 0 → L.mem s1 (wa r o) = L.mem s (wa r o) := fun o ho ho8 => by + have hev : (wa r o).val = r.val + o.val := by rw [wa_valP]; exact Nat.mod_eq_of_lt (by omega) + exact hNonNtfn r .reply hrH (fun h => by cases h) (wa r o) (aligned_wa (by unfold Aligned; omega) o ho8) (by omega) (by change _ < r.val + 32; omega) + exact ⟨hrw _ (by simp [layout.reply.NEXT]) (by simp [layout.reply.NEXT]), + hrw _ (by simp [layout.reply.PREV]) (by simp [layout.reply.PREV])⟩ + have hbuf' : BufCover st Z.T Bf s1 σ1 := by + intro u hu y b sb v hy hb hl + have hne : specPtr u ≠ specPtr nf := fun e => by rw [e, updKSc_same] at hy; cases hy + rw [updKSc_other _ hne] at hy + have hib : IbSame (L := L) s s1 u := + ⟨hTcbEq u hu _ (by simp) (by simp), + by rw [sbWordA_eq]; exact hTcbEq u hu _ (by simp) (by simp), + by rw [sbWordA8_eq]; exact hTcbEq u hu _ (by simp) (by simp)⟩ + rw [lookup_ipc_buffer_sameA hib] at hl + exact J.bufCover u hu y b sb v hy hb hl + have hbufAl' : UtBufAlA Z.T σ1 := fun u hu z hz => by + have hne : specPtr u ≠ specPtr nf := fun e => by rw [e, updKSc_same] at hz; cases hz + rw [updKSc_other _ hne] at hz; exact J.bufAl u hu z hz + have hidle' : IdleNoFaultA m σ1 := fun c hc => by + obtain ⟨x0, hx0, hf⟩ := J.idleNoFault c hc + have hne : m.idleThreadOf c ≠ specPtr nf := fun e => by rw [e, hkn] at hx0; cases hx0 + exact ⟨x0, by rw [updKSc_other _ hne]; exact hx0, hf⟩ + have hmcp' : McpRelA Z.T m (L.mem s1) σ1 := fun u hu => by + obtain ⟨y, hy, hm, hf⟩ := J.mcp u hu + have hne : specPtr u ≠ specPtr nf := fun e => by rw [e, hkn] at hy; cases hy + refine ⟨y, by rw [updKSc_other _ hne]; exact hy, + by unfold mcpA; rw [hTcbEq u hu 3440#u64 (by decide) (by decide)]; exact hm, + fun hni => by have := hf hni; unfold FlagsOkA at this ⊢; unfold flagsA; rwa [hTcbEq u hu 3456#u64 (by decide) (by decide)]⟩ + have hyr' : YieldRelA Z.T Z.C m (L.mem s1) σ1 := by + have hye : ∀ u ∈ Z.T, L.mem s1 (yieldA u) = L.mem s (yieldA u) := fun u hu => by + unfold yieldA; exact hTcbEq u hu 3472#u64 (by decide) (by decide) + refine ⟨fun u hu => ?_, fun u hu h => ?_, fun c hcc y k h p hp => ?_⟩ + · obtain ⟨y, hy, e⟩ := J.yieldRel.tcb u hu + have hne : specPtr u ≠ specPtr nf := fun eq => by rw [eq, hkn] at hy; cases hy + exact ⟨y, by rw [updKSc_other _ hne]; exact hy, by rw [hye u hu]; exact e⟩ + · rw [hye u hu] at h ⊢ + obtain ⟨hC, y, k, hy, e⟩ := J.yieldRel.toSc u hu h + have hne : scNameA m (L.mem s (yieldA u)) ≠ specPtr nf := fun eq => by rw [eq, hkn] at hy; cases hy + exact ⟨hC, y, k, by rw [updKSc_other _ hne]; exact hy, e⟩ + · have h0 : σ.kheap (scNameA m c) = some (.schedContext y k) := by + by_cases e : scNameA m c = specPtr nf + · rw [e, updKSc_same] at h; cases h + · rwa [updKSc_other _ e] at h + obtain ⟨u, hu, e1, e2⟩ := J.yieldRel.frm c hcc y k h0 p hp + exact ⟨u, hu, e1, by rw [hye u hu]; exact e2⟩ + exact ⟨hep', hntfn', hreply', hbuf', hbufAl', hidle', hmcp', hyr'⟩ + +private theorem ntfn_set_idle_sinvA + (hsinv : SInvXA σ m n) {nf : U64} {ntfn : Notification} + (hkn : σ.kheap (specPtr nf) = some (.notification ntfn)) : + SInvXA (updKSc σ (specPtr nf) (.notification { ntfn with ntfnObj := .idleNtfn })) m n := by + let σ1 := updKSc σ (specPtr nf) (.notification { ntfn with ntfnObj := .idleNtfn }) + have hSSm1 : SchedSameA σ σ1 := + { kh := fun q ko hko => by + have hne : ∀ y, ko ≠ .notification y := fun y e => by + rcases hko with ⟨_, e'⟩ | ⟨_, _, e'⟩ <;> rw [e] at e' <;> cases e' + constructor + · intro h + by_cases e : q = specPtr nf + · rw [e, updKSc_same] at h; exact absurd (Option.some.inj h).symm (hne _) + · rwa [updKSc_other _ e] at h + · intro h + rw [updKSc_other _ (fun e => by rw [e, hkn] at h; exact hne _ (Option.some.inj h).symm)] + exact h + cur := rfl, idle := rfl, csc := rfl, rq := rfl, rls := rfl, time := rfl, cons := rfl } + have hirqN1 : SpecIrqA σ1 := by + have hct : ∀ n0 p, capTableAt n0 p σ1 ↔ capTableAt n0 p σ := fun n0 p => by + unfold capTableAt; by_cases e : p = specPtr nf + · subst e; simp [σ1, updKSc, hkn] + · simp [σ1, updKSc, Function.update_of_ne e] + have hcapK : ∀ q ob b a', σ1.kheap q = some ob → capOf ob b = some a' → σ.kheap q = some ob := fun q ob b a' h hc' => by + by_cases e : q = specPtr nf + · rw [e, updKSc_same] at h; cases h; cases hc' + · rwa [updKSc_other _ e] at h + obtain ⟨⟨⟨hinj, hcn⟩, hdom⟩, hsl, href⟩ := hsinv.irqN + exact ⟨⟨⟨hinj, fun irq hi => (hct _ _).2 (hcn irq hi)⟩, fun irq hi n0 hn0 => hdom irq hi n0 ((hct _ _).1 hn0)⟩, + fun irq hi o b a' h hc => hsl irq hi o b a' (hcapK _ _ _ _ h hc) hc, + fun p o b a' h hc => href p o b a' (hcapK _ _ _ _ h hc) hc⟩ + exact hsinv.of_spec hSSm1 rfl hirqN1 + +private theorem ntfn_set_idle_sidleA + (hsidle : SIdleA σ m n) {nf : U64} {ntfn : Notification} + (hkn : σ.kheap (specPtr nf) = some (.notification ntfn)) : + SIdleA (updKSc σ (specPtr nf) (.notification { ntfn with ntfnObj := .idleNtfn })) m n := by + have hcap1 : ∀ sl, specCapAtA (updKSc σ (specPtr nf) (.notification { ntfn with ntfnObj := .idleNtfn })) sl = specCapAtA σ sl := fun sl => by + unfold specCapAtA; by_cases e : sl.1 = specPtr nf + · rw [e, updKSc_same, hkn] + · rw [updKSc_other _ e] + exact + { f3 := fun c hc => let ⟨x, hx, hs⟩ := hsidle.f3 c hc; ⟨x, by rw [updKSc_other _ (fun e => by rw [e, hkn] at hx; cases hx)]; exact hx, hs⟩ + f4 := fun y k hy => hsidle.f4 y k (by rwa [updKSc_other _ (fun e => by rw [e, updKSc_same] at hy; cases hy)] at hy) + f5 := fun p y k hy => hsidle.f5 p y k (by rwa [updKSc_other _ (fun e => by rw [e, updKSc_same] at hy; cases hy)] at hy) + f6 := hsidle.f6.of_eq hcap1 + f7 := fun y k hy => hsidle.f7 y k (by rwa [updKSc_other _ (fun e => by rw [e, updKSc_same] at hy; cases hy)] at hy) + idleSlot := fun c hc => by + have h0 := hsidle.idleSlot c hc + have hne : m.idleThreadOf c ≠ specPtr nf := fun e => by obtain ⟨x, hx, -⟩ := hsidle.f3 c hc; rw [e, hkn] at hx; cases hx + unfold vtCapA at h0 ⊢; rwa [updKSc_other _ hne] + idleBound := fun c hc => by + have h0 := hsidle.idleBound c hc + have hne : m.idleThreadOf c ≠ specPtr nf := fun e => by obtain ⟨x, hx, -⟩ := hsidle.f3 c hc; rw [e, hkn] at hx; cases hx + unfold vcpuOfA at h0 ⊢; rwa [updKSc_other _ hne] } + +private theorem ntfn_set_idle_svA + (hSV : SchedValidA σ) {nf : U64} {ntfn : Notification} + (hkn : σ.kheap (specPtr nf) = some (.notification ntfn)) : + SchedValidA (updKSc σ (specPtr nf) (.notification { ntfn with ntfnObj := .idleNtfn })) := + hSV.congr rfl rfl rfl rfl rfl rfl + (fun p y k hy => by rw [updKSc_other _ (fun e => by rw [e, hkn] at hy; cases hy)]; exact hy) + (fun t x hx => ⟨x, by rw [updKSc_other _ (fun e => by rw [e, hkn] at hx; cases hx)]; exact hx, rfl⟩) + (fun t x hx => ⟨x, by rwa [updKSc_other _ (fun e => by rw [e, updKSc_same] at hx; cases hx)] at hx, rfl, rfl⟩) + +private theorem ntfn_set_idle_rvA {ar : machine.Arch A} {nA : U64} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s a σ m) + {nf : U64} {ntfn : Notification} + (hkn : σ.kheap (specPtr nf) = some (.notification ntfn)) + {badge : Fl4SpecA64.Word} (hact : ntfn.ntfnObj = .activeNtfn badge) + (hTcbEq : ∀ t ∈ Z.T, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → + L.mem (L.wr s (epTSA nf) ((L.mem s (epTSA nf) &&& ~~~3#u64) ||| (obj.NTFN_IDLE &&& 3#u64))) (wa t o) = L.mem s (wa t o)) : + let s1 := L.wr s (epTSA nf) ((L.mem s (epTSA nf) &&& ~~~3#u64) ||| (obj.NTFN_IDLE &&& 3#u64)) + let σ1 := updKSc σ (specPtr nf) (.notification { ntfn with ntfnObj := .idleNtfn }) + RunVcpuInv st Z.T s1 ∧ BlkQueuedA Z.T E N (L.mem s1) σ1 := by + intro s1 σ1 + have hrv' : RunVcpuInv st Z.T s1 := fun t ht hne => by + unfold runVcpuA at hne; rw [hTcbEq t ht 3672#u64 (by simp) (by simp)] at hne + rw [stA_eq_wa, hTcbEq t ht 3328#u64 (by simp) (by simp), ← stA_eq_wa]; exact J.runVcpu t ht hne + have hbq' : BlkQueuedA Z.T E N (L.mem s1) σ1 := + ⟨fun t ht x hx e he => by + have hne : specPtr t ≠ specPtr nf := fun eq => by rw [eq, updKSc_same] at hx; cases hx + rw [updKSc_other _ hne] at hx + obtain ⟨hE, y, hy, hm⟩ := J.blkQ.ep t ht x hx e he + have hne_e : e ≠ specPtr nf := fun eq => by rw [eq, hkn] at hy; cases hy + exact ⟨by rw [tBlockA_eq, hTcbEq t ht 3336#u64 (by simp) (by simp), ← tBlockA_eq]; exact hE, + y, by rw [updKSc_other _ hne_e]; exact hy, hm⟩, + fun t ht x hx p hp => by + have hne : specPtr t ≠ specPtr nf := fun eq => by rw [eq, updKSc_same] at hx; cases hx + rw [updKSc_other _ hne] at hx + obtain ⟨hN, y, q, hy, ho, hm⟩ := J.blkQ.ntfn t ht x hx p hp + have hne_p : p ≠ specPtr nf := fun eq => by + rw [eq, hkn] at hy; cases hy; rw [hact] at ho; exact absurd ho (by intro h; cases h) + exact ⟨by rw [tBlockA_eq, hTcbEq t ht 3336#u64 (by simp) (by simp), ← tBlockA_eq]; exact hN, + y, q, by rw [updKSc_other _ hne_p]; exact hy, ho, hm⟩⟩ + exact ⟨hrv', hbq'⟩ + +private theorem ntfn_set_idle_syslA {ar : machine.Arch A} {nA : U64} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s a σ m) + {nf : U64} (hnfH : H nf = some .ntfn) + (hal_ep : Aligned (epTSA nf)) (hval_ep : (epTSA nf).val = nf.val + 8) + (hw_ipc : IpcObjWA N E Rp (epTSA nf)) + (w' : WritesIn st s (L.wr s (epTSA nf) ((L.mem s (epTSA nf) &&& ~~~3#u64) ||| (obj.NTFN_IDLE &&& 3#u64))) (fun b => b = epTSA nf)) + (hei2 : L.idleW (L.wr s (epTSA nf) ((L.mem s (epTSA nf) &&& ~~~3#u64) ||| (obj.NTFN_IDLE &&& 3#u64))) = L.idleW s) + (hNonNtfn : ∀ p k, H p = some k → (k = .ntfn → p ≠ nf) → + ∀ b, Aligned b → p.val ≤ b.val → b.val < p.val + 2 ^ k.bits → + L.mem (L.wr s (epTSA nf) ((L.mem s (epTSA nf) &&& ~~~3#u64) ||| (obj.NTFN_IDLE &&& 3#u64))) b = L.mem s b) + (hTcbEq : ∀ t ∈ Z.T, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → + L.mem (L.wr s (epTSA nf) ((L.mem s (epTSA nf) &&& ~~~3#u64) ||| (obj.NTFN_IDLE &&& 3#u64))) (wa t o) = L.mem s (wa t o)) : + let s1 := L.wr s (epTSA nf) ((L.mem s (epTSA nf) &&& ~~~3#u64) ||| (obj.NTFN_IDLE &&& 3#u64)) + SysL st H Z.Cs OS ia ie s1 ∧ SymL st H Z.Cs s1 := by + intro s1 + have kb : L.ksb s1 = L.ksb s := w'.1 + obtain ⟨Pt_n, Ub_n, Pp_n, Tb_n, Hr_n, Te_n, Kt_n, Lv_n, Re_n, bits_n, KLv_n, W_n⟩ := J7A.unp J.j7 + have hB : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s := J.sys.ent.bxi.blk + have h0 : H 0#u64 = none := J.sys.sysl.ly.h0 + have V := J.vcOK W_n J.vcOS + have vty := J.vcTy W_n V.bcov + have hW : ∀ b, VcW Z.V b → b ∈ H.omega ∧ b ∉ H.frame := fun b ⟨v, hv, o, ho, h8, e⟩ => by + subst e; exact ⟨(vty.obj v hv).1 o ho h8 |>.2.1, (vty.obj v hv).1 o ho h8 |>.2.2⟩ + have hSus : WritesIn st s s1 (SusW st H Z.Cs Z.V s) := + w'.mono fun _ hb => hb ▸ Or.inl (Or.inl (K8.kw_ep (show nf ∈ EH H from Or.inr hnfH) (Or.inr rfl))) + have hLvF : LvF st H.tcbs Z.Cs H.replies H.scs H.ntfns LvX.none s s1 := + { ksb := kb, cpu := w'.2.1, idle := hei2 + hd := fun c hc => by + have nd := J.sq.nodes c hc + have hdb : layout.node.DEBUG_TCBS.val < 4288 := by unfold layout.node.DEBUG_TCBS; decide + have hdb8 : layout.node.DEBUG_TCBS.val % 8 = 0 := by unfold layout.node.DEBUG_TCBS; decide + rw [nodeAt_of_ksb kb] + have hnv := nodeAt_val nd hdb + exact mem_wr_neA hal_ep (nodeAt_aligned nd hdb hdb8) (fun e => by + have hk := J.ipc_ksA hw_ipc + have hc128 : c.val < 128 := by + obtain ⟨nn, -, hkn', pcs⟩ := J.sr.toSchedSRN.csN; have := (pcs c).1 hc; omega + have hv := congrArg UScalar.val e; rw [hnv] at hv; unfold LawfulStore.nodeBase at hv; unfold ksSizeA at hk; omega) + t := fun y hy K hK _ => by + have hyT : y ∈ Z.T := J.tcbs.symm ▸ hy + have hK1 : K.val < 4096 ∧ K.val % 8 = 0 := by unfold LvO at hK; rcases hK with h | h | h | h | h | h | h | h <;> omega + exact hTcbEq y hyT K hK1.1 hK1.2 + c := fun c hc o ho _ => by + obtain ⟨z, hz⟩ : ∃ z, H c = some (.sc z) := hc + have hmin := J.sys.ent.heap.scmin c z hz + have hpow : 128 ≤ 2 ^ z := by have : 2 ^ 7 ≤ 2 ^ z := Nat.pow_le_pow_right (by omega) hmin; simpa using this + have hft := J.sys.ent.heap.fits c (.sc z) hz; have hal := J.sys.ent.heap.align c (.sc z) hz + have h8 : 8 ∣ 2 ^ z := Nat.pow_dvd_pow 2 (by omega : 3 ≤ z) + have hc8 : c.val % 8 = 0 := Nat.dvd_iff_mod_eq_zero.mp (Nat.dvd_trans h8 (Nat.dvd_iff_mod_eq_zero.mpr hal)) + have ho1 : o.val < 128 ∧ o.val % 8 = 0 := by unfold LvSO at ho; rcases ho with h | h | h | h | h <;> omega + have hev : (wa c o).val = c.val + o.val := by rw [wa_valP]; exact Nat.mod_eq_of_lt (by change c.val + 2 ^ z ≤ 2 ^ 64 at hft; omega) + exact hNonNtfn c (.sc z) hz (fun h => by cases h) (wa c o) (aligned_wa hc8 o ho1.2) (by omega) (by change (wa c o).val < c.val + 2 ^ z; omega) + n := fun nu hnu o ho _ => by + have hft := J.sys.ent.heap.fits nu .ntfn hnu; have hal := J.sys.ent.heap.align nu .ntfn hnu + change nu.val + 64 ≤ 2 ^ 64 at hft; change nu.val % 64 = 0 at hal + have ho1 : o.val < 64 ∧ o.val % 8 = 0 ∧ o.val ≠ 8 := by unfold LvNO at ho; rcases ho with h | h <;> omega + have hev : (wa nu o).val = nu.val + o.val := by rw [wa_valP]; exact Nat.mod_eq_of_lt (by omega) + by_cases hnn : nu = nf + · subst hnn + exact mem_wr_neA hal_ep (aligned_wa (by unfold Aligned; omega) o ho1.2.1) (fun e => by + have := congrArg UScalar.val e; rw [hval_ep, hev] at this; omega) + · exact hNonNtfn nu .ntfn hnu (fun _ => hnn) (wa nu o) (aligned_wa (by unfold Aligned; omega) o ho1.2.1) (by omega) (by change (wa nu o).val < nu.val + 64; omega) + r := fun r hr o ho _ => by + have hft := J.sys.ent.heap.fits r .reply hr; have hal := J.sys.ent.heap.align r .reply hr + change r.val + 32 ≤ 2 ^ 64 at hft; change r.val % 32 = 0 at hal + have ho1 : o.val < 32 ∧ o.val % 8 = 0 := by unfold LvRO at ho; rcases ho with h | h | h <;> omega + have hev : (wa r o).val = r.val + o.val := by rw [wa_valP]; exact Nat.mod_eq_of_lt (by omega) + exact hNonNtfn r .reply hr (fun h => by cases h) (wa r o) (aligned_wa (by unfold Aligned; omega) o ho1.2) (by omega) (by change (wa r o).val < r.val + 32; omega) } + have hEpM : EpM st H s s1 := by + refine ⟨fun e he hne => ?_, fun nu hnu h1 => ?_⟩ + · have hft := J.sys.ent.heap.fits e .ep he; have hal := J.sys.ent.heap.align e .ep he + change e.val + 16 ≤ 2 ^ 64 at hft; change e.val % 16 = 0 at hal + have he8 : (epTSA e).val = e.val + 8 := by rw [epTSA_eq, wa_valP]; exact Nat.mod_eq_of_lt (by simp; omega) + have heq : L.mem s1 (epTSA e) = L.mem s (epTSA e) := + hNonNtfn e .ep he (fun h => by cases h) (epTSA e) (by unfold Aligned; omega) (by omega) (by change (epTSA e).val < e.val + 16; omega) + rwa [LawfulStore.epState, heq] at hne + · by_cases hnn : nu = nf + · subst nu + have h0_nf : L.epState s1 nf = 0#u64 := by + rw [LawfulStore.epState, L.mem_wr_same hal_ep] + apply u64_eq_of_bv_eqR; unfold obj.NTFN_IDLE + exact bv_clear_lo2_and3 (L.mem s (epTSA nf)).bv + rw [h0_nf] at h1; exact absurd h1 (by decide) + · have hft := J.sys.ent.heap.fits nu .ntfn hnu; have hal := J.sys.ent.heap.align nu .ntfn hnu + change nu.val + 64 ≤ 2 ^ 64 at hft; change nu.val % 64 = 0 at hal + have hn8 : (epTSA nu).val = nu.val + 8 := by rw [epTSA_eq, wa_valP]; exact Nat.mod_eq_of_lt (by simp; omega) + have heq : L.mem s1 (epTSA nu) = L.mem s (epTSA nu) := + hNonNtfn nu .ntfn hnu (fun _ => hnn) (epTSA nu) (by unfold Aligned; omega) (by omega) (by change (epTSA nu).val < nu.val + 64; omega) + rwa [LawfulStore.epState, heq] at h1 + have d : DMB st H Z.Cs Z.V OS s 0#u64 0#u64 s s1 := + (DMR.ofNoneE hB h0 J.symL J.sys.sysl.vr hSus hLvF hEpM).toU.toB + have hp0 : ∀ k, H 0#u64 = some k → ∃ y ∈ OS, NamesA (capA st s y) 0#u64 k := fun k hk => by rw [h0] at hk; cases hk + exact SysL.dfMDB J.sys.ent.heap J.sys.ent.os V (fun b hb => (hW b hb).1) hB J.ap.sca hp0 hp0 J.sys.sysl d + +set_option maxHeartbeats 800000 in +/-- Step 4a of `vcpu.check`: clear an active notification's state word (`epTSA nf`) to `NTFN_IDLE` +on `JStage3A` via `JStage3A.of_stepFinA` (NO `SActA σ` required — valid when `curThread` is in +`.ext .runningVM`). -/ +theorem ntfn_set_idle_stage3A {ar : machine.Arch A} {nA : U64} {Ub : Set U64} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s a σ m) + (hNoSw : ∀ u, σ.schedulerAction ≠ .switchThread u) + (_R6 : VcpuRec6A st Z OS Bf s σ) + (_hVcP : VcPlacedA Z OS Ub (L.ksb s)) + {nf : U64} (hnf : nf ∈ N) {ntfn : Notification} + (hkn : σ.kheap (specPtr nf) = some (.notification ntfn)) + {badge : Fl4SpecA64.Word} (hact : ntfn.ntfnObj = .activeNtfn badge) : + let s1 := L.wr s (epTSA nf) ((L.mem s (epTSA nf) &&& ~~~3#u64) ||| (obj.NTFN_IDLE &&& 3#u64)) + let σ1 := updKSc σ (specPtr nf) (.notification { ntfn with ntfnObj := .idleNtfn }) + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s1 a σ1 m ∧ + (∀ u, σ1.schedulerAction ≠ .switchThread u) ∧ + VcpuRec6A st Z OS Bf s1 σ1 ∧ + OSKeep st OS s s1 := by + intro s1 σ1 + have hnfH : H nf = some .ntfn := J.utIpc.ntfn nf hnf + have hnfAl : nf.val % 64 = 0 := J.sys.ent.heap.align nf .ntfn hnfH + have hnfFit : nf.val + 64 ≤ 2 ^ 64 := J.sys.ent.heap.fits nf .ntfn hnfH + have hep8 : epTSA nf = wa nf 8#u64 := epTSA_eq nf + have hal_ep : Aligned (epTSA nf) := by rw [hep8]; exact aligned_wa (by unfold Aligned; omega) 8#u64 (by decide) + have hval_ep : (epTSA nf).val = nf.val + 8 := by + have h8 : (8#u64 : U64).val = 8 := rfl + rw [hep8, wa_valP, h8]; exact Nat.mod_eq_of_lt (by omega) + have hw_ipc : IpcObjWA N E Rp (epTSA nf) := Or.inl ⟨nf, hnf, by omega, by omega⟩ + have w' : WritesIn st s s1 (fun b => b = epTSA nf) := + ⟨L.ksb_wr _ _ _, L.cpu_wr _ _ _, fun q hqa hne => mem_wr_neA hal_ep hqa (fun e => hne e.symm)⟩ + have w : WritesIn st s s1 (SchedStepNWA (st := st) N E Rp Z m σ s) := + w'.mono fun b hb => Or.inr (hb ▸ hw_ipc) + have hNonNtfn : ∀ p k, H p = some k → (k = .ntfn → p ≠ nf) → + ∀ b, Aligned b → p.val ≤ b.val → b.val < p.val + 2 ^ k.bits → L.mem s1 b = L.mem s b := fun p k hp hne b hba hb1 hb2 => by + have hnp : p ≠ nf := fun e => by subst e; rw [hnfH] at hp; cases hp; exact hne rfl rfl + have hd := J.sys.ent.heap.disj nf .ntfn p k hnfH hp hnp.symm + exact mem_wr_neA hal_ep hba (fun e => by + have hv := congrArg UScalar.val e; rw [hval_ep] at hv; change nf.val + 64 ≤ p.val ∨ p.val + 2 ^ k.bits ≤ nf.val at hd; omega) + have hTcbEq : ∀ t ∈ Z.T, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → L.mem s1 (wa t o) = L.mem s (wa t o) := fun t ht o ho ho8 => by + have htH : H t = some .tcb := by change t ∈ H.tcbs; rw [J.tcbs]; exact ht + have hft := J.sys.ent.heap.fits t .tcb htH + have hat := J.sys.ent.heap.align t .tcb htH + have hev : (wa t o).val = t.val + o.val := by rw [wa_valP]; exact Nat.mod_eq_of_lt (by change t.val + 4096 ≤ 2 ^ 64 at hft; omega) + exact hNonNtfn t .tcb htH (fun h => by cases h) (wa t o) (aligned_wa (by unfold Aligned; change t.val % 4096 = 0 at hat; omega) o ho8) + (by omega) (by change (wa t o).val < t.val + 4096; omega) + have hHS : SpecSchedA σ σ1 := + { kh := fun p => by + by_cases e : p = specPtr nf + · subst e; exact Or.inr (Or.inr ⟨_, _, hkn, updKSc_same _ _ _, Or.inr (Or.inl ⟨ntfn, { ntfn with ntfnObj := .idleNtfn }, rfl, rfl⟩)⟩) + · exact Or.inl (updKSc_other _ e) + irqS := rfl, irqN := rfl, asid := rfl, vcur := rfl, cdt := rfl, orig := rfl } + have hSSz : SSzA σ σ1 := fun p y k hp => by + by_cases e : p = specPtr nf + · rw [e, updKSc_same] at hp; cases hp + · rw [updKSc_other _ e] at hp; exact ⟨y, hp⟩ + have hQk : QKeepA σ σ1 := + ⟨fun t y hy => by + by_cases e : t = specPtr nf + · rw [e, updKSc_same] at hy; cases hy + · rw [updKSc_other _ e] at hy; exact ⟨y, hy, rfl⟩, + fun p x hx => by + by_cases e : p = specPtr nf + · rw [e, updKSc_same] at hx; cases hx + · rw [updKSc_other _ e] at hx; exact ⟨x, hx, List.Sublist.refl _⟩, + fun p x hx => by + by_cases e : p = specPtr nf + · subst e; rw [updKSc_same] at hx; cases hx; exact ⟨ntfn, hkn, List.nil_sublist _⟩ + · rw [updKSc_other _ e] at hx; exact ⟨x, hx, List.Sublist.refl _⟩, + fun p y k hy => by + by_cases e : p = specPtr nf + · rw [e, updKSc_same] at hy; cases hy + · rw [updKSc_other _ e] at hy; exact ⟨y, k, hy, fun r => ⟨r, id⟩⟩, + fun p y hy => by + by_cases e : p = specPtr nf + · rw [e, updKSc_same] at hy; cases hy + · rw [updKSc_other _ e] at hy; exact ⟨y, hy, id⟩, + ⟨rfl, rfl, rfl, rfl⟩⟩ + obtain ⟨-, hB0, hX0, hSg0, hS0⟩ := J.sys.ent.bxi + have hst_act : L.epState s nf = 2#u64 := by + obtain ⟨x0, hx0, hr0⟩ := J.ntfn.ntfn nf hnf + rw [hkn] at hx0; cases hx0 + have hobj0 := hr0.obj; rw [hact] at hobj0 + exact hobj0.1 + obtain ⟨hsq', hB2, hX2, hS2, hSg2, -, -, -, hei2, -⟩ := + ntfn_idle_all J.sq hB0 hX0 hS0 hSg0 hnfH hst_act + have hblk' : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s1 := + ⟨⟨Z.Q, Z.R, hsq'⟩, hB2, hX2, hS2, hSg2⟩ + have kb : L.ksb s1 = L.ksb s := w.1 + have hbxi' : BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s1 := + ⟨hblk'.1, hblk'.2.1, hblk'.2.2.1, hblk'.2.2.2.2, hblk'.2.2.2.1⟩ + have hsr' : SchedSRA st Z m κ s1 σ1 := by + have hS_step : SStepA σ σ1 := + { fix := ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ + act := rfl, rq := rfl, rel := rfl + sc := fun u x k hx => by + have hne : u ≠ specPtr nf := fun e => by rw [e, hkn] at hx; cases hx + exact updKSc_other _ hne + tcb := fun u x hx => by + have hne : u ≠ specPtr nf := fun e => by rw [e, hkn] at hx; cases hx + exact ⟨x, by rw [updKSc_other _ hne]; exact hx, rfl, rfl, rfl⟩ + scBack := fun u x k hx => by + have hne : u ≠ specPtr nf := fun e => by rw [e, updKSc_same] at hx; cases hx + exact (updKSc_other _ hne).symm + tcbBack := fun u y hy => by + have hne : u ≠ specPtr nf := fun e => by rw [e, updKSc_same] at hy; cases hy + exact ⟨y, by rwa [updKSc_other _ hne] at hy⟩ + ntfnBack := fun u y hy => by + by_cases e : u = specPtr nf + · subst e; rw [updKSc_same] at hy; cases hy; exact ⟨ntfn, hkn, rfl⟩ + · exact ⟨y, by rwa [updKSc_other _ e] at hy, rfl⟩ } + have hNB : NoBlockESc σ σ1 := fun u x y hx hy => by + have hne : u ≠ specPtr nf := fun e => by rw [e, hkn] at hx; cases hx + rw [updKSc_other _ hne, hx] at hy; cases hy; exact Or.inl ⟨rfl, id⟩ + refine J.sr.stable w' (fun b hb => ?_) hS_step hNB + subst hb + rintro (⟨c, hc', o, ho, e⟩ | e | e | ⟨u, hu, K, hK, e⟩ | h) + · have hn := J.sr.node_in hc' ho + have hk := J.ipc_ksA hw_ipc + rw [← e] at hn; omega + · have hk := J.ipc_ksA hw_ipc + have hfit := J.sr.ksFit + have h8 : (wa (L.ksb s) 8#u64).val = (L.ksb s).val + 8 := by + rw [wa_valm, show (8#u64 : U64).val = 8 by simp]; unfold ksSizeA at hfit; exact Nat.mod_eq_of_lt (by omega) + rw [e, h8] at hk; unfold ksSizeA at hk; omega + · have hk := J.ipc_ksA hw_ipc + have hfit := J.sr.ksFit + have h16 : (wa (L.ksb s) 16#u64).val = (L.ksb s).val + 16 := by + rw [wa_valm, show (16#u64 : U64).val = 16 by simp]; unfold ksSizeA at hfit; exact Nat.mod_eq_of_lt (by omega) + rw [e, h16] at hk; unfold ksSizeA at hk; omega + · have huH : H u = some .tcb := by change u ∈ H.tcbs; rw [J.tcbs]; exact hu + have k4 : K.val < 4096 := by rcases hK with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> simp + have hfit := (J.sr.core.tcbs u hu).fits + have hv : (wa u K).val = u.val + K.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact J.ipc_off_hobjA (v := u) (kv := .tcb) huH (by simp) + ⟨by rw [e, hv]; omega, by rw [e, hv]; simp only [HKind.bits]; omega⟩ hw_ipc + · have := J.sr.scp.th_out h (J.ipcPlace.notification nf hnf) + simp only [ObjKindA.size] at this; omega + have hscCov' : ScCovA Z.C m σ1 := fun q z k h => by + obtain ⟨z', hz⟩ := hSSz q z k h; exact J.scCov q z' k hz + obtain ⟨hep', hntfn', hreply', hbuf', hbufAl', hidle', hmcp', hyr'⟩ := + ntfn_set_idle_ipcA J hnf hnfH hnfAl hnfFit hkn hact hal_ep hval_ep hNonNtfn hTcbEq + have hsinv1 := ntfn_set_idle_sinvA J.sinv hkn + have hsidle1 := ntfn_set_idle_sidleA J.sidle hkn + have hSV1 := ntfn_set_idle_svA J.schedValid hkn + obtain ⟨hrv', hbq'⟩ := ntfn_set_idle_rvA J hkn hact hTcbEq + obtain ⟨sysl1, symL1⟩ := ntfn_set_idle_syslA J hnfH hal_ep hval_ep hw_ipc w' hei2 hNonNtfn hTcbEq + have osk1 : OSKeep st OS s s1 := J.osKeep_stepNA w + have hj7_1 : J7A st (J7R2A st ar nA) Z H OS s1 σ1 m := + J7PpKA.keep (st := st) Z H OS s s1 σ σ1 m J.j7 kb osk1 (fun Pt Ub vr b hb => + w.mem_eq (vr.al b hb) (by + by_cases ha : AsW (L.ksb s) b + · obtain ⟨o, o1, o2, -, e⟩ := ha + have ev : b.val = (L.ksb s).val + o.val := by rw [e, wa_valm]; exact Nat.mod_eq_of_lt (by have := J.sys.ent.ksw; omega) + exact J.asid_offStepNA (by omega) (by omega) + · exact J.vsr_offStepNA vr hb ha)) + have huser1 : Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s1 := + J.sys.ent.user.ksb kb + have J1 : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s1 a σ1 m := + JStage3A.of_stepFinA (Q := Z.Q) (Rl := Z.R) (curOf := Z.curOf) (curScOf := Z.curScOf) J + w hHS hSSz hQk rfl (fun _ _ => rfl) hbxi' sysl1 symL1 huser1 hj7_1 hsr' + hep' hntfn' hreply' hbuf' hbufAl' hidle' hmcp' hyr' hsinv1 hsidle1 hrv' hbq' hSV1 + exact ⟨J1, hNoSw, J1.vcpuRec6A, osk1⟩ + +set_option maxHeartbeats 600000 in +/-- Step 4 of `decode_run`: `vcpu.check` when `ct` is in `TS_RUNNING_VM` with `runVcpuA ct ≠ 0` +refines `fl4VcpuCheck` on `JStage3A` (zero `FrameMemRelA`; NO `SActA σ`). -/ +theorem vcpu_check_running_vm_stage3A {ar : machine.Arch A} {nA : U64} {Ub : Set U64} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s a σ m) + (hNoSw : ∀ u, σ.schedulerAction ≠ .switchThread u) + (R6 : VcpuRec6A st Z OS Bf s σ) + (hVcP : VcPlacedA Z OS Ub (L.ksb s)) + (hP : VcpuPlatAgreeA ar) (hAK : VcpuArchKeepsA ar AR) (hVR : VcpuVmRootA st ar AR) + {ct : U64} (hct : ct ∈ Z.T) + (hvm : L.mem s (stA ct) = sched.TS_RUNNING_VM) + (hrv_nz : L.mem s (runVcpuA ct) ≠ 0#u64) : + vcpu.check st ar s a ct ⦃ res => + ∃ (hw' : HwRootA) (σ' : Fl4SpecA64.State Unit), + fl4VcpuCheck (Z := Unit) (specPtr ct) σ = ({((), σ')}, False) ∧ + JStage3A st AR { Z with hw := hw' } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) res.1 res.2 σ' m ∧ + OSKeep st OS s res.1 ⦄ := by + unfold vcpu.check + rw [eq_of_spec_fd (obj.Tcb.state.spec s ct), bind_ok] + rw [if_neg (by rw [bne_iff_ne]; exact not_not.2 hvm)] + have hrv_const : layout.tcb.RUN_VCPU = ok 3672#u64 := + eq_of_spec_fd Fl4ProofsA64.P0.Consts.layout.tcb.RUN_VCPU.spec + have hrun_eq : wa ct 3672#u64 = runVcpuA ct := rfl + rw [hrv_const, bind_ok, show lift (wa ct 3672#u64) = ok (wa ct 3672#u64) from rfl, bind_ok, hrun_eq, L.read_eq, bind_ok] + rw [if_neg hrv_nz, eq_of_spec_fd (obj.Tcb.bound_ntfn.val s ct), bind_ok] + obtain ⟨xct, hkc, -, hrv_c⟩ := J.j.2.1.tcb ct hct + obtain ⟨xct_n, hkc_n, htr_n⟩ := J.ntfn.tcb ct hct + rw [hkc] at hkc_n; cases hkc_n + have hvm_s : xct.tcbState = .ext .runningVM := by + have hst := htr_n.st; rw [hvm] at hst + have h8 : sched.TS_RUNNING_VM.val = 8 := by unfold sched.TS_RUNNING_VM; rfl + rw [h8] at hst; cases hs : xct.tcbState with + | ext e => cases e; rfl + | _ => rw [hs] at hst; cases hst + have hsp_pre : fl4VcpuCheck (Z := Unit) (specPtr ct) σ = (do + let nOpt ← getTcbObjRef Tcb.tcbBoundNotification (specPtr ct) + match nOpt with + | some n0 => do + let ntfn ← getNotification n0 + match ntfn.ntfnObj with + | .activeNtfn badge => do + setNotification n0 { ntfn with ntfnObj := .idleNtfn } + fl4CompleteRun (specPtr ct) .interrupt badge 0 0 + | _ => pure () + | none => pure ()) σ := by + unfold fl4VcpuCheck + rw [nd_bind_oneSc (by unfold getThreadState; exact threadGet_eqSc _ hkc), hvm_s]; unfold NonDet.when; rw [if_pos rfl] + rw [nd_bind_oneSc (getRunVcpu_eqA hkc), hrv_c]; unfold optRefA; rw [if_neg hrv_nz]; rfl + obtain ⟨xct_e, hkc_e, htr_e⟩ := J.ep.tcb ct hct + rw [hkc] at hkc_e; cases hkc_e + have hbound : getTcbObjRef (Z := Unit) Tcb.tcbBoundNotification (specPtr ct) σ = + ({(optRefA (L.mem s (boundA ct)), σ)}, False) := by + unfold getTcbObjRef; rw [threadGet_eqSc _ hkc]; exact congrArg (fun r => ({(r, σ)}, False)) htr_e.bound + set nf := L.mem s (boundA ct) + by_cases hnf0 : nf = 0#u64 + · rw [if_neg (by rw [bne_iff_ne]; exact not_not.2 hnf0), WP.spec_ok] + refine ⟨Z.hw, σ, ?_, J, fun _ _ => ⟨rfl, rfl, rfl, rfl⟩⟩ + rw [hsp_pre, nd_bind_oneSc hbound]; unfold optRefA; rw [if_pos hnf0]; rfl + · rw [if_pos (bne_iff_ne.2 hnf0)] + have hnfN : nf ∈ N := + J.ipcHeapCov.ntfn nf (J.sys.ent.bxi.2.2.1.2.2.bnd ct (J.tcbs.symm ▸ hct) hnf0) + obtain ⟨ntfn, hkn, hnr⟩ := J.ntfn.ntfn nf hnfN + rw [eq_of_spec_fd (obj.Ntfn.state.spec s nf), bind_ok] + have hsp_n : fl4VcpuCheck (Z := Unit) (specPtr ct) σ = (do + match ntfn.ntfnObj with + | .activeNtfn badge => do + setNotification (specPtr nf) { ntfn with ntfnObj := .idleNtfn } + fl4CompleteRun (specPtr ct) .interrupt badge 0 0 + | _ => pure ()) σ := by + rw [hsp_pre, nd_bind_oneSc hbound]; unfold optRefA; rw [if_neg hnf0]; dsimp only + change (getNotification (specPtr nf) >>= _) σ = _ + rw [nd_bind_oneSc (getNotification_eq hkn)] + cases hno : ntfn.ntfnObj with + | idleNtfn => + have hst0 := hnr.obj; rw [hno] at hst0 + have hne2 : L.epState s nf ≠ obj.NTFN_ACTIVE := by + rw [epState_eq, hst0.1]; unfold obj.NTFN_ACTIVE; decide + rw [if_neg hne2, WP.spec_ok] + exact ⟨Z.hw, σ, by rw [hsp_n, hno]; rfl, J, fun _ _ => ⟨rfl, rfl, rfl, rfl⟩⟩ + | waitingNtfn q => + have hst0 := hnr.obj; rw [hno] at hst0 + have hne2 : L.epState s nf ≠ obj.NTFN_ACTIVE := by + rw [epState_eq, hst0.1]; unfold obj.NTFN_ACTIVE; decide + rw [if_neg hne2, WP.spec_ok] + exact ⟨Z.hw, σ, by rw [hsp_n, hno]; rfl, J, fun _ _ => ⟨rfl, rfl, rfl, rfl⟩⟩ + | activeNtfn b => + have hst0 := hnr.obj; rw [hno] at hst0 + have heq2 : L.epState s nf = obj.NTFN_ACTIVE := by + rw [epState_eq, hst0.1]; unfold obj.NTFN_ACTIVE; rfl + rw [if_pos heq2, eq_of_spec_fd (obj.Ntfn.badge.val s nf), bind_ok, + eq_of_spec_fd (obj.Ntfn.set_state.val (st := st) s nf obj.NTFN_IDLE), bind_ok] + obtain ⟨J1, hNoSw1, R6_1, osk1⟩ := ntfn_set_idle_stage3A J hNoSw R6 hVcP hnfN hkn hno + have hstop_i : fl4_abi.consts.vcpu_run_stop.INTERRUPT.bv = RunStop.interrupt.word := by + unfold fl4_abi.consts.vcpu_run_stop.INTERRUPT RunStop.word; rfl + have hbd_eq : (L.mem s (ntfnBdA nf)).bv = b := hst0.2.2 + refine WP.spec_mono (vcpu_complete_run_stage3A (Clock := fun a' _ => ∃ h', AR a' h') J1 hNoSw1 ⟨Z.hw, J1.j.1.ar⟩ + (fun s' a' _ u ⟨h', har'⟩ => WP.spec_mono (hVR.set_vm_root s' a' h' u har') (fun _ h => h.2)) + (fun a' _ vc om ⟨h', har'⟩ => WP.spec_mono (hAK.vcpu_flush_state a' h' vc om har') (fun _ h => ⟨h', h⟩)) + (fun _ _ _ h _ _ => h) hP hAK hVR hct hstop_i (L.mem s (ntfnBdA nf)) 0#u64 0#u64) ?_ + rintro ⟨s2, a2⟩ ⟨hw', σ2, hcr, J2, -, -, -, -, -, -, -, osk2⟩ + refine ⟨hw', σ2, ?_, J2, + fun y hy => ⟨(osk2 y hy).1.trans (osk1 y hy).1, (osk2 y hy).2.1.trans (osk1 y hy).2.1, + (osk2 y hy).2.2.1.trans (osk1 y hy).2.2.1, (osk2 y hy).2.2.2.trans (osk1 y hy).2.2.2⟩⟩ + rw [hsp_n, hno] + change (setNotification (specPtr nf) { ntfn with ntfnObj := .idleNtfn } >>= + fun _ => fl4CompleteRun (specPtr ct) .interrupt b 0 0) σ = _ + rw [nd_bind_oneSc (setNotification_eq _ hkn), ← hbd_eq]; exact hcr + +end runHelpers + +section decodeRun + +variable {S A : Type} {st : machine.Store S} [L : LawfulStore st] + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} + {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +private theorem u64_sub_okR {x y : U64} (h : y.val ≤ x.val) : + x - y = ok ⟨BitVec.ofNat 64 (x.val - y.val)⟩ := by + change UScalar.sub x y = _; unfold UScalar.sub; rw [if_neg (Nat.not_lt.mpr h)]; rfl + +private theorem u64_shl_okR {x y : U64} (hy : y.val < 64) : + x <<< y = ok ⟨x.bv <<< y.val⟩ := by + change UScalar.shiftLeft x y = _; unfold UScalar.shiftLeft + have hy' : y.val < UScalarTy.numBits UScalarTy.U64 := hy + rw [if_pos hy']; rfl + +set_option maxHeartbeats 1200000 in +/-- **Label 69 (`ARMVCPURun`) on `JSyscallA → JStage3A`** (zero global `FrameMemRelA`; zero +hand-rebuilt records; all `hBufNotS2`/`hCurSched`/`hCurNoRun`/`hCurNotIdle`/`hNtfn` premises +eliminated from `JSyscallA`; only slice-local `FrameInputAgreeA` `[scope limit]` when `in_mask ≠ 0`). -/ +theorem JSyscallA.vcpuRunA {ar : machine.Arch A} + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hP : VcpuPlatAgreeA ar) (hAK : VcpuArchKeepsA ar AR) + (hVR : VcpuVmRootA st ar AR) (hS2R : VcpuStage2RootA st ar AR) + (hCs : ∀ c ∈ Z.Cs, c.val < Fl4SpecA64.fl4A64Platform.numNodes) (hCsN : ∀ c ∈ Z.Cs, c.val < n) + (h1 : Fl4SpecA64.fl4A64Platform.numNodes = 1) + (hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM) + {slot vc length buf : U64} {blk call don fp : Bool} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) + (hcap : capA st s slot = .Vcpu vc) (hvc : vc ∈ Z.V) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + {mi : MessageInfo} {sl : CslotPtr} {excaps : List (Fl4SpecA64.Cap × CslotPtr)} + {buffer : Option ObjRef} {args : List U64} + (hfp : VcpuFrontPinA Z.th σ slot mi sl (.archObjectCap (.vcpuCap (specPtr vc))) excaps buffer (args.map UScalar.bv)) + (hlbl : fl4LabelOf mi.miLabel = .ext .armVCPURun) + (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) + -- `[scope limit]` slice-local input-frame agreement on the 1 KiB window when `in_mask ≠ 0`. + (hAgree : ∀ ptr sz rights mp ept asid va offset in_mask, + capA st s (sbWordA (L.curW s)) = .Frame ptr sz rights false mp ept asid va → + args[0]? = some offset → args[1]? = some in_mask → in_mask ≠ 0#u64 → + FrameInputAgreeA st s σ (wa ptr offset)) + {s3 : S} {a3 : A} + (hdec : syscall.decode_invocation st ar s a fl4_abi.label.ARMVCPURun length slot (.Vcpu vc) + blk call don fp buf extra = ok (core.result.Result.Ok (), s3, a3)) : + ∃ (x : Fl4SpecA64.Tcb) (frame offset inMask outMask : U64) (vgicSpec : Word × List Word) + (hw' : HwRootA) (σ3 : Fl4SpecA64.State Unit), + σ.kheap σ.curThread = some (.tcb x) ∧ + VcpuDecPinA call Z.th σ slot (.invokeArchObject (.vcpu (.run (specPtr vc) (specPtr (L.curW s)) (specPtr frame) offset.bv inMask.bv outMask.bv vgicSpec))) ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + (.ok [], σ3) ∈ ((fl4ArchPerformInvocation (Z := Unit) + (.vcpu (.run (specPtr vc) (specPtr (L.curW s)) (specPtr frame) offset.bv inMask.bv outMask.bv vgicSpec))).run + (stUpdA σ σ.curThread x .restart)).1 ∧ + JStage3A st AR { Z with hw := hw' } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s3 a3 σ3 m := by + have J3 := J.toJStage3A + obtain ⟨Pt, Ub, Pp7, Tb7, Hr, Te, Kt, Lv7, Re, bits7, KLv, W⟩ := J7A.unp J3.j7 + have hpl := J3.vcPlacedA W + have hU := J3.ubFootDisjA W + have C := J.vcpuDecCtxA W hCs hCsN h1 hvc + have RS := J3.rsSiteA hext hslot hcov J.actW J.running J.cur + have hCs128 : ∀ c ∈ Z.Cs, c.val < 128 := fun c hc => by + have := hCs c hc; rw [h1] at this; omega + have hcpu128 : (L.cpu s).val < 128 := hCs128 _ J3.j.1.sch.cpuCs + set ct := L.curW s + have hcurT : ct ∈ Z.T := J3.sr.curT + have hdec0 := hdec + unfold syscall.decode_invocation at hdec + rw [show sched.cur_thread st s = ok ct from eq_of_spec_fd (sched.cur_thread.spec s), bind_ok] at hdec + dsimp only at hdec; unfold vcpu.decode at hdec + simp only [fl4_abi.label.ARMVCPURun, fl4_abi.label.ARMVCPUWriteState, fl4_abi.label.ARMVCPUReadState, + fl4_abi.label.ARMVCPUSetTCB, fl4_abi.label.ARMVCPUInjectIRQ, fl4_abi.label.ARMVCPUReadReg, + fl4_abi.label.ARMVCPUWriteReg, fl4_abi.label.ARMVCPUAckVPPI] at hdec + rw [if_neg (by decide), if_neg (by decide), if_neg (by decide), if_neg (by decide), if_neg (by decide), + if_neg (by decide), if_neg (by decide), if_pos trivial] at hdec + unfold vcpu.decode_run at hdec + cases hcall : call with + | false => rw [hcall] at hdec; simp [error.illegal, bind_ok] at hdec + | true => + rw [hcall, if_pos rfl] at hdec + by_cases hlen3 : length < 3#u64 + · rw [if_pos hlen3] at hdec; simp [error.truncated, bind_ok] at hdec + rw [if_neg hlen3] at hdec + have hge3 : 3 ≤ args.length := by have : ¬ length.val < 3 := hlen3; omega + cases hargs_eq : args with + | nil => simp [hargs_eq] at hge3 + | cons offset rest1 => + cases hrest1_eq : rest1 with + | nil => simp [hargs_eq, hrest1_eq] at hge3 + | cons in_mask rest2 => + cases hrest2_eq : rest2 with + | nil => simp [hargs_eq, hrest1_eq, hrest2_eq] at hge3 + | cons out_mask rest3 => + rw [show sched.cur_thread st s = ok ct from eq_of_spec_fd (sched.cur_thread.spec s), bind_ok, + hargs 0#u64 offset (by simp [hargs_eq]), bind_ok, + hargs 1#u64 in_mask (by simp [hargs_eq, hrest1_eq]), bind_ok, + hargs 2#u64 out_mask (by simp [hargs_eq, hrest1_eq, hrest2_eq]), bind_ok] at hdec + have hall_ok : fl4_abi.consts.vcpu_state_mask.ALL = ok 63#u64 := + eq_of_spec_fd Fl4ProofsA64.P0.Consts.fl4_abi.consts.vcpu_state_mask.ALL.spec + rw [hall_ok, bind_ok, lift_okR, bind_ok, lift_okR, bind_ok] at hdec + by_cases hm1 : ((in_mask &&& ~~~63#u64) != 0#u64) = true + · rw [if_pos hm1] at hdec; simp [error.invalid_arg, bind_ok] at hdec + rw [if_neg hm1, lift_okR, bind_ok, lift_okR, bind_ok] at hdec + have hm1_0 : in_mask &&& ~~~63#u64 = 0#u64 := by by_contra hne; exact hm1 (bne_iff_ne.2 hne) + have hm1_bv : ¬ (in_mask.bv &&& ~~~smAll ≠ 0) := by + intro h; exact h (congrArg UScalar.bv hm1_0) + by_cases hm2 : ((out_mask &&& ~~~63#u64) != 0#u64) = true + · rw [if_pos hm2] at hdec; simp [error.invalid_arg, bind_ok] at hdec + rw [if_neg hm2] at hdec + have hm2_0 : out_mask &&& ~~~63#u64 = 0#u64 := by by_contra hne; exact hm2 (bne_iff_ne.2 hne) + have hm2_bv : ¬ (out_mask.bv &&& ~~~smAll ≠ 0) := by + intro h; exact h (congrArg UScalar.bv hm2_0) + -- Read buffer cap from `obj.Tcb.slot ct SLOT_BUFFER` + have hslot_buf : obj.Tcb.slot ct layout.tcb.SLOT_BUFFER = ok (sbWordA ct) := by + simp [obj.Tcb.slot, lift, sbWordA, layout.tcb.SLOT_BUFFER, layout.cte.SIZE] + have hsb_os : sbWordA ct ∈ OS := + J3.j5.2.1 _ ⟨ct, Or.inr ⟨(J3.sr.thT ct).2 hcurT, 2, by unfold tcbSlotsA; decide, (bufSlot_eqA ct).symm⟩⟩ + have hbuf_slot_cap : obj.Slot.cap st (sbWordA ct) s = ok (capA st s (sbWordA ct)) := + Fl4ProofsA64.P1.W7.R.ST.capA_eq s (sbWordA ct) + rw [hslot_buf, bind_ok, hbuf_slot_cap, bind_ok] at hdec + cases hcap_buf : capA st s (sbWordA ct) with + | Frame ptr sz rights is_dev mapped ept asid va => + rw [hcap_buf] at hdec + cases hdev : is_dev with + | true => rw [hdev] at hdec; simp [error.illegal, bind_ok] at hdec + | false => + rw [hdev] at hdec; dsimp only at hdec; rw [if_neg Bool.false_ne_true] at hdec + by_cases hrw : (rights != capability.VM_READ_WRITE) = true + · rw [if_pos hrw] at hdec; simp [error.illegal, bind_ok] at hdec + rw [if_neg hrw] at hdec + have hrights : rights = capability.VM_READ_WRITE := by + by_contra hne; exact hrw (bne_iff_ne.2 hne) + -- Derive `ept = false` directly from `J.bufNative`! + have hept_f : ept = false := by + have hbn := J.bufNative ct hcurT + rw [bufSlot_eqA ct, capAtA_eq, hcap_buf] at hbn; exact hbn + obtain ⟨bits, hbits_ok, hbits_val⟩ := frameSize_bits_eqR sz + rw [hbits_ok, bind_ok, lift_okR, bind_ok] at hdec + have hbits_lt64 : bits.val < 64 := by rw [hbits_val]; exact frameBitsA_lt64R sz + have hbits_and : bits &&& 63#u64 = bits := by + apply UScalar.eq_of_val_eq + change bits.val &&& 63 = bits.val + have h6 := Nat.and_two_pow_sub_one_eq_mod bits.val 6 + simp only [Nat.reducePow, Nat.reduceSub] at h6 + rw [h6]; exact Nat.mod_eq_of_lt hbits_lt64 + rw [hbits_and, u64_shl_okR hbits_lt64, bind_ok, lift_okR, bind_ok] at hdec + set size1 : U64 := ⟨(1#u64 : U64).bv <<< bits.val⟩ + have hsize1_val : size1.val = 2 ^ frameBitsA sz := by + have h1bv : (1#u64 : U64).bv = (1 : BitVec 64) := rfl + change ((1#u64 : U64).bv <<< bits.val).toNat = 2 ^ frameBitsA sz + rw [h1bv, hbits_val]; cases sz <;> decide + by_cases hoff63 : ((offset &&& 63#u64) != 0#u64) = true + · rw [if_pos hoff63] at hdec; simp [error.invalid_arg, bind_ok] at hdec + rw [if_neg hoff63] at hdec + have hoff63_0 : offset &&& 63#u64 = 0#u64 := by by_contra hne; exact hoff63 (bne_iff_ne.2 hne) + have hoff_al64 : offset.val % 64 = 0 := by + have hv : (offset &&& 63#u64).val = 0 := by rw [hoff63_0]; rfl + change offset.val &&& 63 = 0 at hv + have h6 := Nat.and_two_pow_sub_one_eq_mod offset.val 6 + simp only [Nat.reducePow, Nat.reduceSub] at h6; omega + by_cases hge_sz : offset >= size1 + · rw [if_pos hge_sz] at hdec; simp [error.invalid_arg, bind_ok] at hdec + rw [if_neg hge_sz] at hdec + have hoff_lt : offset.val < size1.val := by + have : ¬ size1.val ≤ offset.val := hge_sz; omega + rw [u64_sub_okR (by omega), bind_ok] at hdec + by_cases hlt_sm : (⟨BitVec.ofNat 64 (size1.val - offset.val)⟩ : U64) < fl4_abi.consts.vcpu_state_mask.SIZE + · rw [if_pos hlt_sm] at hdec; simp [error.invalid_arg, bind_ok] at hdec + rw [if_neg hlt_sm] at hdec + have hrem : 1024 ≤ size1.val - offset.val := by + have hsm : fl4_abi.consts.vcpu_state_mask.SIZE.val = 1024 := by + unfold fl4_abi.consts.vcpu_state_mask.SIZE; rfl + have hsub_v : (⟨BitVec.ofNat 64 (size1.val - offset.val)⟩ : U64).val = size1.val - offset.val := by + change (BitVec.ofNat 64 _).toNat = _; rw [BitVec.toNat_ofNat] + exact Nat.mod_eq_of_lt (by have := size1.bv.isLt; change size1.val < 2 ^ 64 at this; omega) + change ¬ (⟨BitVec.ofNat 64 (size1.val - offset.val)⟩ : U64).val < fl4_abi.consts.vcpu_state_mask.SIZE.val at hlt_sm + omega + -- Read `vc.TCB`, `vcpu.bound ct`, `vc.RUN_CALLER` + have htcb_off : vcpu.TCB = 0#u64 := by unfold vcpu.TCB; rfl + have hrc_off : vcpu.RUN_CALLER = 24#u64 := by unfold vcpu.RUN_CALLER; rfl + rw [htcb_off, lift_okR, bind_ok, wa_zeroR, L.read_eq, bind_ok] at hdec + set v_tcb := L.mem s vc + by_cases hvt0 : v_tcb = 0#u64 + · rw [if_pos hvt0] at hdec; simp [error.illegal, bind_ok] at hdec + rw [if_neg hvt0] at hdec + by_cases hvt_ct : v_tcb = ct + · rw [if_pos hvt_ct] at hdec; simp [error.illegal, bind_ok] at hdec + rw [if_neg hvt_ct] at hdec + have hvb_eq : vcpu.bound st s ct = ok (L.mem s (wa ct 3664#u64)) := by + unfold vcpu.bound + rw [eq_of_spec_fd Fl4ProofsA64.P0.Consts.layout.tcb.VCPU.spec, bind_ok, lift_okR, bind_ok, L.read_eq] + rw [hvb_eq, bind_ok] at hdec + by_cases hct_v : (L.mem s (wa ct 3664#u64) != 0#u64) = true + · rw [if_pos hct_v] at hdec; simp [error.illegal, bind_ok] at hdec + rw [if_neg hct_v, hrc_off, lift_okR, bind_ok, L.read_eq, bind_ok] at hdec + have hct_v0 : L.mem s (wa ct 3664#u64) = 0#u64 := by by_contra hne; exact hct_v (bne_iff_ne.2 hne) + by_cases hrc_nz : (L.mem s (wa vc 24#u64) != 0#u64) = true + · rw [if_pos hrc_nz] at hdec; simp at hdec + rw [if_neg hrc_nz] at hdec + have hrc0 : L.mem s (wa vc 24#u64) = 0#u64 := by by_contra hne; exact hrc_nz (bne_iff_ne.2 hne) + -- Frame window properties from `W.fu` + `J3.sys.ent.heap` + have hvtT : v_tcb ∈ Z.T := (J3.vcRun.vtcb vc hvc).resolve_left hvt0 + set frame := wa ptr offset + have hptrH : H ptr = some (.other (frameBitsA sz)) := by + rw [← hbits_val] + exact W.cc.fo (sbWordA ct) hsb_os ptr sz rights mapped ept asid va (by rwa [hdev] at hcap_buf) bits hbits_ok + have hptrAl : ptr.val % 2 ^ frameBitsA sz = 0 := J3.sys.ent.heap.align ptr _ hptrH + have hptrFit : ptr.val + 2 ^ frameBitsA sz ≤ 2 ^ 64 := J3.sys.ent.heap.fits ptr _ hptrH + have hfr_val : frame.val = ptr.val + offset.val := by + rw [wa_valP, Nat.mod_eq_of_lt (by omega)] + have hfr_fit : frame.val + 1024 ≤ 2 ^ 64 := by omega + have hfr_al : Aligned frame := by + unfold Aligned; rw [hfr_val] + have h64_dvd : 64 ∣ 2 ^ frameBitsA sz := by cases sz <;> decide + have hptr8 : ptr.val % 8 = 0 := + Nat.dvd_iff_mod_eq_zero.mp (Nat.dvd_trans (by decide : 8 ∣ 64) (Nat.dvd_trans h64_dvd (Nat.dvd_iff_mod_eq_zero.mpr hptrAl))) + omega + have hWin : FrameWinA Ub frame := by + refine ⟨hfr_fit, fun k hk => ?_⟩ + have h8k_lt : 8 * k < 2 ^ 64 := by omega + have h8k_v : (⟨BitVec.ofNat 64 (8 * k)⟩ : U64).val = 8 * k := by + change (BitVec.ofNat 64 (8 * k)).toNat = 8 * k + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt h8k_lt] + have hal_fk : Aligned (wa frame ⟨BitVec.ofNat 64 (8 * k)⟩) := + aligned_wa hfr_al _ (by rw [h8k_v]; omega) + have hval_fk : (wa frame ⟨BitVec.ofNat 64 (8 * k)⟩).val = ptr.val + offset.val + 8 * k := by + rw [wa_valP, h8k_v, hfr_val, Nat.mod_eq_of_lt (by omega)] + exact W.fu (sbWordA ct) hsb_os ptr sz rights mapped ept asid va (by rwa [hdev] at hcap_buf) bits hbits_ok + _ hal_fk (by omega) (by rw [hbits_val]; omega) + have hfr_dt : ∀ t ∈ Z.T, ∀ k < 128, ∀ K : U64, K.val < 4096 → wa frame ⟨BitVec.ofNat 64 (8 * k)⟩ ≠ wa t K := fun t ht k hk K hK e => by + have htH : t ∈ H.tcbs := J3.tcbs ▸ ht + change H t = some .tcb at htH + have hne_pt : ptr ≠ t := fun h => by rw [h, htH] at hptrH; cases hptrH + have hd := J3.sys.ent.heap.disj ptr _ t .tcb hptrH htH hne_pt + have hft := (hU.tcb_al t ht).fits + have hv1 : (wa frame ⟨BitVec.ofNat 64 (8 * k)⟩).val = frame.val + 8 * k := by + rw [wa_valP]; change (frame.val + (BitVec.ofNat 64 (8 * k)).toNat) % 2 ^ 64 = _ + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega), Nat.mod_eq_of_lt (by omega)] + have hv2 : (wa t K).val = t.val + K.val := by rw [wa_valP, Nat.mod_eq_of_lt (by omega)] + have hv := congrArg UScalar.val e; rw [hv1, hv2] at hv + change ptr.val + 2 ^ frameBitsA sz ≤ t.val ∨ t.val + 4096 ≤ ptr.val at hd; omega + -- Bind `num_lrs`, `vcpu_gic_version`, VGIC masks + rw [lift_okR, bind_ok, vcpu_num_lrs_eqA hP a, bind_ok] at hdec + obtain ⟨g, hg_eq, hgv⟩ := hP.gic a + rw [hg_eq, bind_ok] at hdec + have hsmV : fl4_abi.consts.vcpu_state_mask.VGIC = ok 8#u64 := by unfold fl4_abi.consts.vcpu_state_mask.VGIC; rfl + have hsmL : fl4_abi.consts.vcpu_state_mask.VGIC_LR = ok 32#u64 := by unfold fl4_abi.consts.vcpu_state_mask.VGIC_LR; rfl + rw [hsmV, bind_ok, hsmL, bind_ok, lift_okR, bind_ok, lift_okR, bind_ok] at hdec + have hAg_m : in_mask ≠ 0#u64 → FrameInputAgreeA st s σ frame := fun hm => by + simpa [frame] using hAgree ptr sz rights mapped ept asid va offset in_mask (hdev ▸ hcap_buf) + (by simp [hargs_eq]) (by simp [hargs_eq, hrest1_eq]) hm + -- Step A + B: read VGIC frame (if `(in_mask &&& (8 ||| 32)) != 0`), stall (no-op on 1-node), arch flush (`AR a3' Z.hw`), write `vc.OWNER_CPU` + have hst_noop : ∀ (x : U64), (if x != sched.TS_INACTIVE then smp.remote_tcb_stall st ar s a v_tcb else ok (s, a)) = ok (s, a) := fun x => by + by_cases hi : (x != sched.TS_INACTIVE) = true + · rw [if_pos hi]; exact C.hStallT hvt0 + · rw [if_neg hi] + have hfl_state : ∀ (fi : U64), ∃ a4, ar.vcpu_flush_state a vc fi = ok a4 ∧ AR a4 Z.hw := fun fi => + spec_ex_fd (hAK.vcpu_flush_state a Z.hw vc fi J3.j.1.ar) + have hvg_step : ∃ (a3' : A) (vgic : U64 × Array U64 16#usize) (vgicSpec : Word × List Word), + AR a3' Z.hw ∧ + (do + let s4' ← vcpu.write_state_from_frame st ar (L.wr s (wa vc 8#u64) (wa (L.cpu s) 1#u64)) a3' vc frame in_mask vgic + let i22 ← lift (wa vc vcpu.RUN_CALLER) + let s5' ← st.write s4' i22 ct + let i23 ← lift (wa vc vcpu.RUN_OFFSET) + let s6' ← st.write s5' i23 offset + let i24 ← lift (wa vc vcpu.RUN_OUT_MASK) + let s7' ← st.write s6' i24 out_mask + let i25 ← layout.tcb.RUN_VCPU + let i26 ← lift (wa ct i25) + let s8' ← st.write s7' i26 vc + let (s9', a4') ← vspace.set_stage2_root st ar s8' a3' v_tcb + let s10' ← sched.set_thread_state st s9' ct sched.TS_RUNNING_VM + let (s11', a5') ← vcpu.check st ar s10' a4' ct + ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s11', a5')) = + ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s3, a3) ∧ + (in_mask.bv &&& (smVgic ||| smVgicLr) ≠ 0 → + vgic.1.bv = vgicSpec.1 ∧ vgicSpec.1 >>> 32 = 0 ∧ + vgicSpec.2.length = fl4NumLrs ∧ + (∀ j, j < fl4NumLrs → (vgic.2.val[j]!).bv = vgicSpec.2[j]!) ∧ + readVgicFrame (specPtr frame) σ = ({(some vgicSpec, σ)}, False)) ∧ + (in_mask.bv &&& (smVgic ||| smVgicLr) = 0 → vgicSpec = (0, [])) := by + have how_off : vcpu.OWNER_CPU = 8#u64 := by unfold vcpu.OWNER_CPU; rfl + by_cases hvg_m : (in_mask &&& (8#u64 ||| 32#u64) != 0#u64) = true + · have hvg_mb : in_mask.bv &&& (smVgic ||| smVgicLr) ≠ 0 := fun e => by + have : in_mask &&& (8#u64 ||| 32#u64) = 0#u64 := u64_eq_of_bv_eqR e; simp [this] at hvg_m + have hmnz : in_mask ≠ 0#u64 := fun e => by subst e; exact hvg_mb (by decide) + rw [if_pos hvg_m] at hdec + have hread_vg := read_vgic_frame_localA (st := st) (Z := Z) hfr_fit hfr_al (hAg_m hmnz) + (fun k hk => by rcases W.col.kb.ksU _ (hWin.2 k hk) with h | h <;> (unfold ksSizeA; omega)) + unfold fl4GicV3 at hread_vg; rw [← hgv] at hread_vg + obtain ⟨res, hres_eq, hres_sp⟩ := spec_ex_fd hread_vg + rw [hres_eq, bind_ok] at hdec + cases res with + | none => simp [error.invalid_arg, bind_ok] at hdec + | some vg => + rcases vg with ⟨vgHcr, vgArr⟩ + obtain ⟨lrsList, hlrs_len, hlrs_arr, hread_spec⟩ := hres_sp + have hal3 : ∀ k < 128, bufAddr (specPtr frame) k &&& Fl4SpecA64.mask 3 = 0 := fun k hk => by + rw [← specPtr_wa_wordA (by omega)] + exact specPtr_al3R (aligned_wa hfr_al _ (by + change (BitVec.ofNat 64 (8 * k)).toNat % 8 = 0 + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega : 8 * k < 2 ^ 64)]; omega)) + have hvg_u32 : vgHcr.bv >>> 32 = 0 := by + rw [readVgicFrame_eqA _ _ hal3] at hread_spec + split_ifs at hread_spec with h_ok + · have heq := (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (Prod.mk.inj hread_spec).1)).1 + obtain ⟨hvg_eq, -⟩ := Prod.mk.inj (Option.some.inj heq) + rw [← hvg_eq] + have hlt : (sanitizeVgicHcr (loadWordAt σ.machineState.underlyingMemory (bufAddr (specPtr frame) vrVgicHcr)) fl4GicV3).toNat < 2 ^ 32 := by + unfold sanitizeVgicHcr + cases fl4GicV3 + · simp only [Bool.false_eq_true, ↓reduceIte, BitVec.toNat_and] + exact lt_of_le_of_lt Nat.and_le_right (by decide) + · simp only [↓reduceIte, BitVec.toNat_and] + exact lt_of_le_of_lt Nat.and_le_right (by decide) + apply BitVec.eq_of_toNat_eq + rw [BitVec.toNat_ushiftRight, Nat.shiftRight_eq_div_pow] + exact Nat.div_eq_of_lt hlt + · have heq := (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (Prod.mk.inj hread_spec).1)).1 + cases heq + dsimp only at hdec + have hst_c : (if (L.mem s (stA v_tcb) != sched.TS_INACTIVE) = true then smp.remote_tcb_stall st ar s a v_tcb else ok (s, a)) = ok (s, a) := by + split_ifs + · exact C.hStallT hvt0 + · rfl + rw [eq_of_spec_fd (obj.Tcb.state.spec s v_tcb), bind_ok, hst_c, do_ok_prod, + L.cur_cpu_eq, bind_ok, lift_okR, bind_ok, how_off, lift_okR, bind_ok, L.read_eq, bind_ok] at hdec + have hfinish : ∀ a3' : A, AR a3' Z.hw → + (do + let (a3_0, vgic_0) ← ok (a3', (vgHcr, vgArr)) + let i21 ← ok (wa vc 8#u64) + let s3_0 ← st.write s i21 (wa (L.cpu s) 1#u64) + let s4' ← vcpu.write_state_from_frame st ar s3_0 a3_0 vc frame in_mask vgic_0 + let i22 ← ok (wa vc 24#u64) + let s5' ← st.write s4' i22 ct + let i23 ← lift (wa vc vcpu.RUN_OFFSET) + let s6' ← st.write s5' i23 offset + let i24 ← lift (wa vc vcpu.RUN_OUT_MASK) + let s7' ← st.write s6' i24 out_mask + let i25 ← layout.tcb.RUN_VCPU + let i26 ← lift (wa ct i25) + let s8' ← st.write s7' i26 vc + let (s9', a4') ← vspace.set_stage2_root st ar s8' a3_0 v_tcb + let s10' ← sched.set_thread_state st s9' ct sched.TS_RUNNING_VM + let (s11', a5') ← vcpu.check st ar s10' a4' ct + ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s11', a5')) = + ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s3, a3) → + ∃ (a3' : A) (vgic : U64 × Array U64 16#usize) (vgicSpec : Fl4SpecA64.Word × List Fl4SpecA64.Word), + AR a3' Z.hw ∧ + (do + let s4' ← vcpu.write_state_from_frame st ar (L.wr s (wa vc 8#u64) (wa (L.cpu s) 1#u64)) a3' vc frame in_mask vgic + let i22 ← lift (wa vc vcpu.RUN_CALLER) + let s5' ← st.write s4' i22 ct + let i23 ← lift (wa vc vcpu.RUN_OFFSET) + let s6' ← st.write s5' i23 offset + let i24 ← lift (wa vc vcpu.RUN_OUT_MASK) + let s7' ← st.write s6' i24 out_mask + let i25 ← layout.tcb.RUN_VCPU + let i26 ← lift (wa ct i25) + let s8' ← st.write s7' i26 vc + let (s9', a4') ← vspace.set_stage2_root st ar s8' a3' v_tcb + let s10' ← sched.set_thread_state st s9' ct sched.TS_RUNNING_VM + let (s11', a5') ← vcpu.check st ar s10' a4' ct + ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s11', a5')) = + ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s3, a3) ∧ + (in_mask.bv &&& (smVgic ||| smVgicLr) ≠ 0 → + vgic.1.bv = vgicSpec.1 ∧ vgicSpec.1 >>> 32 = 0 ∧ + vgicSpec.2.length = fl4NumLrs ∧ + (∀ j, j < fl4NumLrs → (vgic.2.val[j]!).bv = vgicSpec.2[j]!) ∧ + readVgicFrame (specPtr frame) σ = ({(some vgicSpec, σ)}, False)) ∧ + (in_mask.bv &&& (smVgic ||| smVgicLr) = 0 → vgicSpec = (0, [])) := by + intro a3' hAR3' hd_in + rw [do_ok_prod, bind_ok, L.write_eq, bind_ok, + show (ok (wa vc 24#u64) : Result U64) = lift (wa vc vcpu.RUN_CALLER) from by unfold vcpu.RUN_CALLER; rfl] at hd_in + exact ⟨a3', (vgHcr, vgArr), (vgHcr.bv, lrsList), hAR3', hd_in, + fun _ => ⟨rfl, hvg_u32, hlrs_len, hlrs_arr, hread_spec⟩, + fun h0 => absurd h0 hvg_mb⟩ + have hfl_branch : (do + let (a3_0, vgic_0) ← (do + let i21 ← ok (in_mask &&& 8#u64) + let (vgic1, flush_in) ← + if i21 = 0#u64 then do + let i22 ← lift (in_mask &&& 32#u64) + if i22 != 0#u64 then + let i24 ← lift (vgHcr &&& 240#u64) + let i25 ← if i24 != 0#u64 then ok (in_mask ||| 8#u64) else ok in_mask + ok ((vgHcr, vgArr), i25) + else ok ((vgHcr, vgArr), in_mask) + else ok ((vgHcr, vgArr), in_mask) + let a4 ← ar.vcpu_flush_state a vc flush_in + ok (a4, vgic1)) + let i21 ← ok (wa vc 8#u64) + let s3_0 ← st.write s i21 (wa (L.cpu s) 1#u64) + let s4' ← vcpu.write_state_from_frame st ar s3_0 a3_0 vc frame in_mask vgic_0 + let i22 ← ok (wa vc 24#u64) + let s5' ← st.write s4' i22 ct + let i23 ← lift (wa vc vcpu.RUN_OFFSET) + let s6' ← st.write s5' i23 offset + let i24 ← lift (wa vc vcpu.RUN_OUT_MASK) + let s7' ← st.write s6' i24 out_mask + let i25 ← layout.tcb.RUN_VCPU + let i26 ← lift (wa ct i25) + let s8' ← st.write s7' i26 vc + let (s9', a4') ← vspace.set_stage2_root st ar s8' a3_0 v_tcb + let s10' ← sched.set_thread_state st s9' ct sched.TS_RUNNING_VM + let (s11', a5') ← vcpu.check st ar s10' a4' ct + ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s11', a5')) = + ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s3, a3) → + ∃ (a3' : A) (vgic : U64 × Array U64 16#usize) (vgicSpec : Fl4SpecA64.Word × List Fl4SpecA64.Word), + AR a3' Z.hw ∧ + (do + let s4' ← vcpu.write_state_from_frame st ar (L.wr s (wa vc 8#u64) (wa (L.cpu s) 1#u64)) a3' vc frame in_mask vgic + let i22 ← lift (wa vc vcpu.RUN_CALLER) + let s5' ← st.write s4' i22 ct + let i23 ← lift (wa vc vcpu.RUN_OFFSET) + let s6' ← st.write s5' i23 offset + let i24 ← lift (wa vc vcpu.RUN_OUT_MASK) + let s7' ← st.write s6' i24 out_mask + let i25 ← layout.tcb.RUN_VCPU + let i26 ← lift (wa ct i25) + let s8' ← st.write s7' i26 vc + let (s9', a4') ← vspace.set_stage2_root st ar s8' a3' v_tcb + let s10' ← sched.set_thread_state st s9' ct sched.TS_RUNNING_VM + let (s11', a5') ← vcpu.check st ar s10' a4' ct + ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s11', a5')) = + ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s3, a3) ∧ + (in_mask.bv &&& (smVgic ||| smVgicLr) ≠ 0 → + vgic.1.bv = vgicSpec.1 ∧ vgicSpec.1 >>> 32 = 0 ∧ + vgicSpec.2.length = fl4NumLrs ∧ + (∀ j, j < fl4NumLrs → (vgic.2.val[j]!).bv = vgicSpec.2[j]!) ∧ + readVgicFrame (specPtr frame) σ = ({(some vgicSpec, σ)}, False)) ∧ + (in_mask.bv &&& (smVgic ||| smVgicLr) = 0 → vgicSpec = (0, [])) := by + intro hd_in + rw [bind_ok] at hd_in + by_cases h1i : in_mask &&& 8#u64 = 0#u64 + · rw [if_pos h1i, lift_okR, bind_ok] at hd_in + by_cases h2i : (in_mask &&& 32#u64 != 0#u64) = true + · rw [if_pos h2i, lift_okR, bind_ok] at hd_in + by_cases h3i : (vgHcr &&& 240#u64 != 0#u64) = true + · rw [if_pos h3i, bind_ok, do_ok_prod] at hd_in + obtain ⟨a4, h4, hAR4⟩ := hfl_state (in_mask ||| 8#u64) + rw [h4, bind_ok] at hd_in; exact hfinish a4 hAR4 hd_in + · rw [if_neg h3i, bind_ok, do_ok_prod] at hd_in + obtain ⟨a4, h4, hAR4⟩ := hfl_state in_mask + rw [h4, bind_ok] at hd_in; exact hfinish a4 hAR4 hd_in + · rw [if_neg h2i, do_ok_prod] at hd_in + obtain ⟨a4, h4, hAR4⟩ := hfl_state in_mask + rw [h4, bind_ok] at hd_in; exact hfinish a4 hAR4 hd_in + · rw [if_neg h1i, do_ok_prod] at hd_in + obtain ⟨a4, h4, hAR4⟩ := hfl_state in_mask + rw [h4, bind_ok] at hd_in; exact hfinish a4 hAR4 hd_in + by_cases hp0 : (L.mem s (wa vc 8#u64) != 0#u64) = true + · rw [if_pos hp0] at hdec + by_cases hpm : (L.mem s (wa vc 8#u64) != wa (L.cpu s) 1#u64) = true + · rw [if_pos hpm] at hdec + obtain ⟨a4, h4, hAR4⟩ := spec_ex_fd (hAK.vcpu_flush a Z.hw vc J3.j.1.ar) + obtain ⟨a5, h5, hAR5⟩ := spec_ex_fd (hAK.fpu_flush a4 Z.hw v_tcb hAR4) + obtain ⟨a6, h6, hAR6⟩ := spec_ex_fd (hAK.fpu_release a5 Z.hw v_tcb hAR5) + rw [h4, bind_ok, h5, bind_ok, h6, bind_ok] at hdec + exact hfinish a6 hAR6 hdec + · rw [if_neg hpm] at hdec; exact hfl_branch hdec + · rw [if_neg hp0] at hdec; exact hfl_branch hdec + · have hvg_m0 : in_mask.bv &&& (smVgic ||| smVgicLr) = 0 := by + have h0 : in_mask &&& (8#u64 ||| 32#u64) = 0#u64 := by + by_contra hne; exact hvg_m (bne_iff_ne.2 hne) + exact congrArg UScalar.bv h0 + have hst_c : (if (L.mem s (stA v_tcb) != sched.TS_INACTIVE) = true then smp.remote_tcb_stall st ar s a v_tcb else ok (s, a)) = ok (s, a) := by + split_ifs + · exact C.hStallT hvt0 + · rfl + rw [if_neg hvg_m, eq_of_spec_fd (obj.Tcb.state.spec s v_tcb), bind_ok, hst_c, do_ok_prod, + L.cur_cpu_eq, bind_ok, lift_okR, bind_ok, how_off, lift_okR, bind_ok, L.read_eq, bind_ok] at hdec + have hfinish0 : ∀ a3' : A, AR a3' Z.hw → + (do + let a3_0 ← ok a3' + let i21 ← ok (wa vc 8#u64) + let s3_0 ← st.write s i21 (wa (L.cpu s) 1#u64) + let s4' ← vcpu.write_state_from_frame st ar s3_0 a3_0 vc frame in_mask (0#u64, Array.repeat 16#usize 0#u64) + let i22 ← ok (wa vc 24#u64) + let s5' ← st.write s4' i22 ct + let i23 ← lift (wa vc vcpu.RUN_OFFSET) + let s6' ← st.write s5' i23 offset + let i24 ← lift (wa vc vcpu.RUN_OUT_MASK) + let s7' ← st.write s6' i24 out_mask + let i25 ← layout.tcb.RUN_VCPU + let i26 ← lift (wa ct i25) + let s8' ← st.write s7' i26 vc + let (s9', a4') ← vspace.set_stage2_root st ar s8' a3_0 v_tcb + let s10' ← sched.set_thread_state st s9' ct sched.TS_RUNNING_VM + let (s11', a5') ← vcpu.check st ar s10' a4' ct + ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s11', a5')) = + ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s3, a3) → + ∃ (a3' : A) (vgic : U64 × Array U64 16#usize) (vgicSpec : Fl4SpecA64.Word × List Fl4SpecA64.Word), + AR a3' Z.hw ∧ + (do + let s4' ← vcpu.write_state_from_frame st ar (L.wr s (wa vc 8#u64) (wa (L.cpu s) 1#u64)) a3' vc frame in_mask vgic + let i22 ← lift (wa vc vcpu.RUN_CALLER) + let s5' ← st.write s4' i22 ct + let i23 ← lift (wa vc vcpu.RUN_OFFSET) + let s6' ← st.write s5' i23 offset + let i24 ← lift (wa vc vcpu.RUN_OUT_MASK) + let s7' ← st.write s6' i24 out_mask + let i25 ← layout.tcb.RUN_VCPU + let i26 ← lift (wa ct i25) + let s8' ← st.write s7' i26 vc + let (s9', a4') ← vspace.set_stage2_root st ar s8' a3' v_tcb + let s10' ← sched.set_thread_state st s9' ct sched.TS_RUNNING_VM + let (s11', a5') ← vcpu.check st ar s10' a4' ct + ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s11', a5')) = + ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s3, a3) ∧ + (in_mask.bv &&& (smVgic ||| smVgicLr) ≠ 0 → + vgic.1.bv = vgicSpec.1 ∧ vgicSpec.1 >>> 32 = 0 ∧ + vgicSpec.2.length = fl4NumLrs ∧ + (∀ j, j < fl4NumLrs → (vgic.2.val[j]!).bv = vgicSpec.2[j]!) ∧ + readVgicFrame (specPtr frame) σ = ({(some vgicSpec, σ)}, False)) ∧ + (in_mask.bv &&& (smVgic ||| smVgicLr) = 0 → vgicSpec = (0, [])) := by + intro a3' hAR3' hd_in + rw [bind_ok, bind_ok, L.write_eq, bind_ok, + show (ok (wa vc 24#u64) : Result U64) = lift (wa vc vcpu.RUN_CALLER) from by unfold vcpu.RUN_CALLER; rfl] at hd_in + exact ⟨a3', (0#u64, Array.repeat 16#usize 0#u64), (0, []), hAR3', hd_in, + fun hne => absurd hvg_m0 hne, fun _ => rfl⟩ + have hfl_branch0 : (do + let a3_0 ← (do + let i21 ← ok (in_mask &&& 8#u64) + let flush_in ← + if i21 = 0#u64 then do + let i22 ← lift (in_mask &&& 32#u64) + if i22 != 0#u64 then + let i23 ← lift (0#u64 &&& 240#u64) + if i23 != 0#u64 then ok (in_mask ||| 8#u64) else ok in_mask + else ok in_mask + else ok in_mask + ar.vcpu_flush_state a vc flush_in) + let i21 ← ok (wa vc 8#u64) + let s3_0 ← st.write s i21 (wa (L.cpu s) 1#u64) + let s4' ← vcpu.write_state_from_frame st ar s3_0 a3_0 vc frame in_mask (0#u64, Array.repeat 16#usize 0#u64) + let i22 ← ok (wa vc 24#u64) + let s5' ← st.write s4' i22 ct + let i23 ← lift (wa vc vcpu.RUN_OFFSET) + let s6' ← st.write s5' i23 offset + let i24 ← lift (wa vc vcpu.RUN_OUT_MASK) + let s7' ← st.write s6' i24 out_mask + let i25 ← layout.tcb.RUN_VCPU + let i26 ← lift (wa ct i25) + let s8' ← st.write s7' i26 vc + let (s9', a4') ← vspace.set_stage2_root st ar s8' a3_0 v_tcb + let s10' ← sched.set_thread_state st s9' ct sched.TS_RUNNING_VM + let (s11', a5') ← vcpu.check st ar s10' a4' ct + ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s11', a5')) = + ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s3, a3) → + ∃ (a3' : A) (vgic : U64 × Array U64 16#usize) (vgicSpec : Fl4SpecA64.Word × List Fl4SpecA64.Word), + AR a3' Z.hw ∧ + (do + let s4' ← vcpu.write_state_from_frame st ar (L.wr s (wa vc 8#u64) (wa (L.cpu s) 1#u64)) a3' vc frame in_mask vgic + let i22 ← lift (wa vc vcpu.RUN_CALLER) + let s5' ← st.write s4' i22 ct + let i23 ← lift (wa vc vcpu.RUN_OFFSET) + let s6' ← st.write s5' i23 offset + let i24 ← lift (wa vc vcpu.RUN_OUT_MASK) + let s7' ← st.write s6' i24 out_mask + let i25 ← layout.tcb.RUN_VCPU + let i26 ← lift (wa ct i25) + let s8' ← st.write s7' i26 vc + let (s9', a4') ← vspace.set_stage2_root st ar s8' a3' v_tcb + let s10' ← sched.set_thread_state st s9' ct sched.TS_RUNNING_VM + let (s11', a5') ← vcpu.check st ar s10' a4' ct + ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s11', a5')) = + ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s3, a3) ∧ + (in_mask.bv &&& (smVgic ||| smVgicLr) ≠ 0 → + vgic.1.bv = vgicSpec.1 ∧ vgicSpec.1 >>> 32 = 0 ∧ + vgicSpec.2.length = fl4NumLrs ∧ + (∀ j, j < fl4NumLrs → (vgic.2.val[j]!).bv = vgicSpec.2[j]!) ∧ + readVgicFrame (specPtr frame) σ = ({(some vgicSpec, σ)}, False)) ∧ + (in_mask.bv &&& (smVgic ||| smVgicLr) = 0 → vgicSpec = (0, [])) := by + intro hd_in + rw [bind_ok] at hd_in + by_cases h1i : in_mask &&& 8#u64 = 0#u64 + · rw [if_pos h1i, lift_okR, bind_ok] at hd_in + by_cases h2i : (in_mask &&& 32#u64 != 0#u64) = true + · rw [if_pos h2i, lift_okR, bind_ok, if_neg (by decide), bind_ok] at hd_in + obtain ⟨a4, h4, hAR4⟩ := hfl_state in_mask + rw [h4] at hd_in; exact hfinish0 a4 hAR4 hd_in + · rw [if_neg h2i, bind_ok] at hd_in + obtain ⟨a4, h4, hAR4⟩ := hfl_state in_mask + rw [h4] at hd_in; exact hfinish0 a4 hAR4 hd_in + · rw [if_neg h1i, bind_ok] at hd_in + obtain ⟨a4, h4, hAR4⟩ := hfl_state in_mask + rw [h4] at hd_in; exact hfinish0 a4 hAR4 hd_in + by_cases hp0 : (L.mem s (wa vc 8#u64) != 0#u64) = true + · rw [if_pos hp0] at hdec + by_cases hpm : (L.mem s (wa vc 8#u64) != wa (L.cpu s) 1#u64) = true + · rw [if_pos hpm] at hdec + obtain ⟨a4, h4, hAR4⟩ := spec_ex_fd (hAK.vcpu_flush a Z.hw vc J3.j.1.ar) + obtain ⟨a5, h5, hAR5⟩ := spec_ex_fd (hAK.fpu_flush a4 Z.hw v_tcb hAR4) + obtain ⟨a6, h6, hAR6⟩ := spec_ex_fd (hAK.fpu_release a5 Z.hw v_tcb hAR5) + rw [h4, bind_ok, h5, bind_ok, h6] at hdec + exact hfinish0 a6 hAR6 hdec + · rw [if_neg hpm] at hdec; exact hfl_branch0 hdec + · rw [if_neg hp0] at hdec; exact hfl_branch0 hdec + obtain ⟨a3', vgic, vgicSpec, hAR3', hdec_vg, hvg_pos, hvg_zero⟩ := hvg_step + -- Step C: `vcpu_set_owner_stage3TA` → `vcpu_write_state_from_frame_stage3TA` → `vcpu_enter_run_vm_stage3A` → `vcpu_check_running_vm_stage3A`! + have hJ3_a3 : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s a3' σ m := + { J3.toTA J.sact J.noSwitch with j := ⟨{ J3.j.1 with ar := hAR3' }, J3.j.2⟩ } + have R6_0 : VcpuRec6A st Z OS Bf s σ := J3.vcpuRec6A + obtain ⟨J_ow, R6_ow, osk_ow, hw_ow⟩ := vcpu_set_owner_stage3TA hJ3_a3 R6_0 hpl hcpu128 hvc + set s_ow := L.wr s (wa vc 8#u64) (wa (L.cpu s) 1#u64) + have hksb_ow : L.ksb s_ow = L.ksb s := hw_ow.1 + have hAg_ow : in_mask ≠ 0#u64 → FrameInputAgreeA st s_ow σ frame := fun hm k hk => by + have h8k_lt : 8 * k < 2 ^ 64 := by omega + have hwal : Aligned (wa frame ⟨BitVec.ofNat 64 (8 * k)⟩) := aligned_wa hfr_al _ (by + change (BitVec.ofNat 64 (8 * k)).toNat % 8 = 0 + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt h8k_lt]; omega) + have hne : wa vc 8#u64 ≠ wa frame ⟨BitVec.ofNat 64 (8 * k)⟩ := fun e => + hpl.disj_ub (wa vc 8#u64) ⟨vc, hvc, 8#u64, by decide, by decide, rfl⟩ (e ▸ hWin.2 k hk) + rw [mem_wr_neA (aligned_wa (hpl.vc_al hvc) 8#u64 (by decide)) hwal hne] + exact hAg_m hm k hk + have hwsf := vcpu_write_state_from_frame_stage3TA (Ub := Ub) J_ow J3.schedValid R6_ow hP + (hksb_ow ▸ hpl) (hksb_ow ▸ hU) hCs128 hCsN hvc hWin hfr_al hAg_ow + (fun t ht k hk K _ hK2 => hfr_dt t ht k hk K (by omega)) + (vgic := vgic) (vgicSpec := vgicSpec) + (fun h => let ⟨a, b, c, d, _⟩ := hvg_pos h; ⟨a, b, c, d⟩) + obtain ⟨s4, hs4_eq, σ4, hsp4, J4, hSV4, R6_4, hw4⟩ := spec_ex_fd hwsf + rw [hs4_eq, bind_ok] at hdec_vg + have hvc_tcb_ow : L.mem s_ow vc = v_tcb := + mem_wr_neA (aligned_wa (hpl.vc_al hvc) 8#u64 (by decide)) (hpl.vc_al hvc) + (by simpa [wa_zeroR] using hpl.same_vcpu_ne hvc (o₁ := 8#u64) (o₂ := 0#u64) (by decide) (by decide) (by decide)) + have hw_no4 : WritesIn st s_ow s4 (VcpuWrFootA st true Z OS s_ow) := + hw4.mono fun b hb => by + rcases hb with rfl | ⟨i, hi, rfl⟩ | ⟨_, K, hK8, hK1, hK2, rfl⟩ + · exact Or.inl ⟨vc, hvc, 16#u64, by decide, by decide, rfl⟩ + · exact Or.inl ⟨vc, hvc, vcpuRegOffA i, by rw [vcpuRegOffA_val hi]; omega, by rw [vcpuRegOffA_val hi]; omega, rfl⟩ + · rw [hvc_tcb_ow]; exact Or.inr (Or.inl ⟨v_tcb, hvtT, K, hK8, Or.inl ⟨hK1, hK2⟩, rfl⟩) + have osk4 : OSKeep st OS s_ow s4 := osKeep_of_vcpuWrA J_ow R6_ow.vcOS R6_ow.frOff hw_no4 + have hw_s_s4_vw : WritesIn st s s4 (VW st H.tcbs Z.Cs Z.V {b | b ∈ Ub ∧ FrBw H OS b ∧ FrCw st OS s b} (L.ksb s) (fun _ => False)) := by + refine ⟨hw4.1.trans hksb_ow, hw4.2.1.trans hw_ow.2.1, fun q hqa hnf => ?_⟩ + rw [hw4.mem_eq hqa (fun hb => hnf (by + rcases hb with rfl | ⟨i, hi, rfl⟩ | ⟨_, K, hK8, hK1, hK2, rfl⟩ + · exact Or.inr (Or.inl ⟨vc, hvc, 16#u64, by decide, by decide, rfl⟩) + · exact Or.inr (Or.inl ⟨vc, hvc, vcpuRegOffA i, by rw [vcpuRegOffA_val hi]; omega, by rw [vcpuRegOffA_val hi]; omega, rfl⟩) + · rw [hvc_tcb_ow]; exact Or.inr (Or.inr (Or.inl ⟨v_tcb, J3.tcbs ▸ hvtT, Or.inl ⟨K, hK1, hK2, hK8, rfl⟩⟩)))), + hw_ow.mem_eq hqa (fun hb => hnf (hb ▸ Or.inr (Or.inl ⟨vc, hvc, 8#u64, by decide, by decide, rfl⟩)))] + have hi_s4 : L.idleW s4 = L.idleW s := by + unfold LawfulStore.idleW + have h8 : layout.node.IDLE_THREAD.val = 8 := by unfold layout.node.IDLE_THREAD; rfl + have hna8 := nodeAt_val (J3.sq.nodes _ J3.sr.sch.cpuCs) (by rw [h8]; decide) + have hal := nodeAt_aligned (J3.sq.nodes _ J3.sr.sch.cpuCs) (by rw [h8]; decide) (by rw [h8]) + have hnd_in : (L.ksb s).val ≤ (L.nodeAt s (L.cpu s) layout.node.IDLE_THREAD).val ∧ + (L.nodeAt s (L.cpu s) layout.node.IDLE_THREAD).val < (L.ksb s).val + ksSizeA := by + rw [hna8, h8]; unfold LawfulStore.nodeBase ksSizeA; omega + rw [hw_s_s4_vw.2.1, nodeAt_of_ksb hw_s_s4_vw.1] + rw [hw4.mem_eq hal (fun hb => by + rcases hb with heq | ⟨i, hi, heq⟩ | ⟨_, K, hK8, hK1, hK2, heq⟩ + · exact hpl.disj_ks _ ⟨vc, hvc, 16#u64, by decide, by decide, heq⟩ hnd_in + · exact hpl.disj_ks _ ⟨vc, hvc, vcpuRegOffA i, by rw [vcpuRegOffA_val hi]; omega, by rw [vcpuRegOffA_val hi]; omega, heq⟩ hnd_in + · rw [hvc_tcb_ow] at heq + have hnd := (J3.sq.nodes _ J3.sr.sch.cpuCs).disj v_tcb (J3.tcbs ▸ hvtT) + have hft := (J3.iplaced.e.tcb v_tcb hvtT).fits + have hv := congrArg UScalar.val heq; rw [hna8, wa_valm, Nat.mod_eq_of_lt (by omega)] at hv + unfold LawfulStore.nodeBase at hnd hv; omega), + hw_ow.mem_eq hal (fun hb => hpl.disj_ks _ ⟨vc, hvc, 8#u64, by decide, by decide, hb⟩ hnd_in)] + obtain ⟨sysl4, symL4⟩ := sysL_symL_of_vwA J3 W hw_s_s4_vw hi_s4 (fun _ h => h.elim) + have osk_s4 : OSKeep st OS s s4 := fun u hu => + ⟨(osk4 u hu).1.trans (osk_ow u hu).1, (osk4 u hu).2.1.trans (osk_ow u hu).2.1, + (osk4 u hu).2.2.1.trans (osk_ow u hu).2.2.1, (osk4 u hu).2.2.2.trans (osk_ow u hu).2.2.2⟩ + have J4_A : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s4 a3' σ4 m := by + have hZ_eq : ({ Z with hw := Z.hw } : XSetsA) = Z := by cases Z; rfl + have h := J4.toJA_vcpuA { J3 with j := ⟨{ J3.j.1 with ar := hAR3' }, J3.j.2⟩ } hSV4 R6_4 osk_s4 sysl4 symL4 + rwa [hZ_eq] at h + have hksb4 : L.ksb s4 = L.ksb s := hw4.1.trans hksb_ow + have hcpu4 : L.cpu s4 = L.cpu s := hw4.2.1.trans hw_ow.2.1 + have hvc_tcb4 : L.mem s4 vc = v_tcb := by + have h1 : L.mem s4 vc = L.mem s_ow vc := hw4.mem_eq (hpl.vc_al hvc) (by + intro h; rcases h with h16 | ⟨i, hi, heq⟩ | ⟨_, K, _, hK1, hK2, heq⟩ + · exact hpl.same_vcpu_ne hvc (o₁ := 0#u64) (o₂ := 16#u64) (by decide) (by decide) (by decide) (by simpa [wa_zeroR] using h16) + · exact hpl.same_vcpu_ne hvc (o₁ := 0#u64) (o₂ := vcpuRegOffA i) (by decide) (by rw [vcpuRegOffA_val hi]; omega) + (fun e => by have := congrArg UScalar.val e; rw [vcpuRegOffA_val hi] at this; change 0 = _ at this; omega) (by simpa [wa_zeroR, vcpuRegWA] using heq) + · rw [hvc_tcb_ow] at heq + exact hpl.disj_tcb vc hvc v_tcb hvtT 0#u64 K (by decide) (by omega) (by simpa [wa_zeroR] using heq)) + rw [h1]; exact mem_wr_neA (aligned_wa (hpl.vc_al hvc) 8#u64 (by decide)) (hpl.vc_al hvc) + (by simpa [wa_zeroR] using hpl.same_vcpu_ne hvc (o₁ := 8#u64) (o₂ := 0#u64) (by decide) (by decide) (by decide)) + have hct_eq4 : ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → L.mem s4 (wa ct o) = L.mem s (wa ct o) := fun o ho ho8 => by + have hal_o : Aligned (wa ct o) := aligned_wa (hpl.al_t ct hcurT) o ho8 + have h1 : L.mem s4 (wa ct o) = L.mem s_ow (wa ct o) := hw4.mem_eq hal_o (by + intro h; rcases h with h16 | ⟨i, hi, heq⟩ | ⟨_, K, _, hK1, hK2, heq⟩ + · exact hpl.disj_tcb vc hvc ct hcurT 16#u64 o (by decide) ho h16.symm + · exact hpl.disj_tcb vc hvc ct hcurT (vcpuRegOffA i) o (by rw [vcpuRegOffA_val hi]; omega) ho heq.symm + · rw [hvc_tcb_ow] at heq + exact field_ne (J3.iplaced.e.tcb ct hcurT) (J3.iplaced.e.tcb v_tcb hvtT) ho (by omega) (fun e _ => hvt_ct e.symm) heq) + rw [h1]; exact mem_wr_neA (aligned_wa (hpl.vc_al hvc) 8#u64 (by decide)) hal_o (hpl.disj_tcb vc hvc ct hcurT 8#u64 o (by decide) ho) + have hrc4 : L.mem s4 (wa vc 24#u64) = 0#u64 := by + have hal24 : Aligned (wa vc 24#u64) := aligned_wa (hpl.vc_al hvc) 24#u64 (by decide) + have h1 : L.mem s4 (wa vc 24#u64) = L.mem s_ow (wa vc 24#u64) := hw4.mem_eq hal24 (by + intro h; rcases h with h16 | ⟨i, hi, heq⟩ | ⟨_, K, _, hK1, hK2, heq⟩ + · exact hpl.same_vcpu_ne hvc (o₁ := 24#u64) (o₂ := 16#u64) (by decide) (by decide) (by decide) h16 + · exact hpl.same_vcpu_ne hvc (o₁ := 24#u64) (o₂ := vcpuRegOffA i) (by decide) (by rw [vcpuRegOffA_val hi]; omega) + (fun e => by have := congrArg UScalar.val e; rw [vcpuRegOffA_val hi] at this; change 24 = _ at this; omega) heq + · rw [hvc_tcb_ow] at heq + exact hpl.disj_tcb vc hvc v_tcb hvtT 24#u64 K (by decide) (by omega) heq) + rw [h1, mem_wr_neA (aligned_wa (hpl.vc_al hvc) 8#u64 (by decide)) hal24 + (hpl.same_vcpu_ne hvc (o₁ := 8#u64) (o₂ := 24#u64) (by decide) (by decide) (by decide))]; exact hrc0 + have hrv0 : L.mem s (runVcpuA ct) = 0#u64 := by + by_contra hne; exact hnvm (J3.runVcpu ct hcurT hne) + have hrv4 : L.mem s4 (runVcpuA ct) = 0#u64 := by + unfold runVcpuA at hrv0 ⊢; rw [hct_eq4 3672#u64 (by decide) (by decide)]; exact hrv0 + have hct_v4 : L.mem s4 (wa ct 3664#u64) = 0#u64 := by rw [hct_eq4 3664#u64 (by decide) (by decide)]; exact hct_v0 + -- Derive `hcur_s4`, `hsc4`, `hrm4`, `hiq4`, `hNotRel4`, `hrun4` from `J3` + `J.running` + `J.cur`! + have hct_spec : σ.curThread = specPtr ct := J3.j.1.sch.curThread + obtain ⟨xct, hkx_ct, hsc_xct⟩ := J3.sr.sch.tcb ct hcurT + have hkx : σ.kheap σ.curThread = some (.tcb xct) := by rwa [hct_spec] + have hrun_xct : runnable xct.tcbState = true := + (runnable_iffA (J3.ep.at_tcb hcurT hkx_ct).st).1 J.running + have hiq_0 : (L.mem s (inqA ct) &&& sched.Q_RELEASE) = 0#u64 := J.cur.1 + have hsc_nz : L.mem s (scA ct) ≠ 0#u64 := J.cur.2 + have hscC : L.mem s (scA ct) ∈ Z.C := J3.sr.rest.scMem ct hcurT hsc_nz + have hsc_s : xct.tcbSchedContext = some (scRenA m (L.mem s (scA ct))) := by + rw [hsc_xct]; unfold optScA; simp [hsc_nz] + have hrm_nz : L.rmax s (L.mem s (scA ct)) ≠ 0#u64 := by + obtain ⟨y, n0, hy, hrm_eq⟩ := J3.sr.rest.refillMax _ hscC + have hni : σ.curThread ≠ σ.idleThread := fun e => by + obtain ⟨xi, hxi, hsti⟩ := J.sinv.idleTcb + rw [← e, hkx] at hxi; cases hxi; rw [hsti] at hrun_xct; exact absurd hrun_xct (by decide) + have hact_y : scActive y = true := by + obtain ⟨⟨x0, sc0, hx0, hsc0, ys, ks, hys, has⟩, -⟩ := J3.schedValid.act J.act hni + rw [hkx] at hx0; cases hx0; rw [hsc_s] at hsc0; cases hsc0 + rw [hy] at hys; cases hys; exact has + have hpos : 0 < y.scRefillMax := by + unfold scActive activeSc at hact_y; simp at hact_y; omega + intro h0 + have h0' : (L.mem s (wa (L.mem s (scA ct)) 72#u64)).val = 0 := by + have := congrArg UScalar.val h0 + simpa [LawfulStore.rmax, maxA, layout.sc.REFILL_MAX] using this + have : y.scRefillMax = 0 := by rw [hrm_eq, h0'] + omega + have hNotRel0 : specPtr ct ∉ σ.releaseQueue := fun hm => by + rw [J3.sr.rest.rel] at hm + obtain ⟨u, hu, he⟩ := List.mem_map.1 hm + have heu : u = ct := specPtr_inj he; subst heu + have haff : L.mem s (affA ct) = L.cpu s := UScalar.eq_of_val_eq (by + have h1' := hCs _ J3.sr.sch.cpuCs + have h2' := hCs _ (J3.sr.core.aff ct hcurT) + omega) + exact (J3.sr.core.released ct hcurT).2 (haff.symm ▸ hu) hiq_0 + have hsts_spec_r : setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), stUpdA σ σ.curThread xct .restart)}, False) := by + rw [hct_spec] + apply setThreadState_eqA σ (specPtr ct) xct hkx_ct .restart + intro _ _ + obtain ⟨y, n0, hy, hrm_eq⟩ := J3.sr.rest.refillMax _ hscC + have hne_sc : scRenA m (L.mem s (scA ct)) ≠ specPtr ct := fun e => by rw [e, hkx_ct] at hy; cases hy + exact schedulable_ofA (updKSc_same _ _ _) rfl hsc_s + (by change (updKSc σ (specPtr ct) _).kheap _ = _; rw [updKSc_other _ hne_sc]; exact hy) + (by by_contra h0 + exact hrm_nz (UScalar.eq_of_val_eq (by + show (L.mem s (wa (L.mem s (scA ct)) layout.sc.REFILL_MAX)).val = 0 + have h72 : layout.sc.REFILL_MAX = 72#u64 := by unfold layout.sc.REFILL_MAX; rfl + rw [h72, ← hrm_eq]; omega))) hNotRel0 + obtain ⟨v0, hkv0, hvt0_s, -⟩ := J3.j.2.1.obj vc hvc + have hvt0_some : v0.vcpuTcb = some (specPtr v_tcb) := by + rw [hvt0_s]; unfold optRefA; rw [if_neg (show L.mem s vc ≠ 0#u64 from hvt0)] + have hne_ct_vc : specPtr ct ≠ specPtr vc := fun e => by rw [e, hkv0] at hkx_ct; cases hkx_ct + have hne_ct_vt : specPtr ct ≠ specPtr v_tcb := fun e => hvt_ct (specPtr_inj e).symm + obtain ⟨hkh4_other, -, hrel4, -, hsp4_r⟩ := + fl4WriteStateFromFrame_comm_updA (x1 := { xct with tcbState := .restart }) hne_ct_vc hne_ct_vt hkv0 hvt0_some hsp4 + have hkt4 : σ4.kheap (specPtr ct) = some (.tcb xct) := by + rw [hkh4_other (specPtr ct) hne_ct_vc hne_ct_vt]; exact hkx_ct + have hsc4_eq : L.mem s4 (scA ct) = L.mem s (scA ct) := by unfold scA; exact hct_eq4 3464#u64 (by decide) (by decide) + have hrm4_nz : L.rmax s4 (L.mem s4 (scA ct)) ≠ 0#u64 := by + rw [hsc4_eq] + obtain ⟨zsc, hzsc⟩ : ∃ z, H (L.mem s (scA ct)) = some (.sc z) := by + change L.mem s (scA ct) ∈ H.scs; exact J3.scs ▸ hscC + have hmin_sc := J3.sys.ent.heap.scmin _ zsc hzsc + have hal_sc := J3.sys.ent.heap.align _ (.sc zsc) hzsc + have h8_sc : 8 ∣ 2 ^ zsc := Nat.pow_dvd_pow 2 (by omega : 3 ≤ zsc) + have hc8 : (L.mem s (scA ct)).val % 8 = 0 := + Nat.dvd_iff_mod_eq_zero.mp (Nat.dvd_trans h8_sc (Nat.dvd_iff_mod_eq_zero.mpr hal_sc)) + have h72_off : layout.sc.REFILL_MAX = 72#u64 := by unfold layout.sc.REFILL_MAX; rfl + have hal_mx : Aligned (maxA (L.mem s (scA ct))) := by + unfold maxA; rw [h72_off]; exact aligned_wa hc8 72#u64 (by decide) + have hmx_eq : L.rmax s4 (L.mem s (scA ct)) = L.rmax s (L.mem s (scA ct)) := by + unfold LawfulStore.rmax + have h1 : L.mem s4 (maxA (L.mem s (scA ct))) = L.mem s_ow (maxA (L.mem s (scA ct))) := + hw4.mem_eq hal_mx (by + intro h; rcases h with h16 | ⟨i, hi, heq⟩ | ⟨_, K, _, hK1, hK2, heq⟩ + · exact hpl.disj_sc vc hvc _ hscC 16#u64 layout.sc.REFILL_MAX (by decide) (by rw [h72_off]; decide) h16.symm + · exact hpl.disj_sc vc hvc _ hscC (vcpuRegOffA i) layout.sc.REFILL_MAX (by rw [vcpuRegOffA_val hi]; omega) (by rw [h72_off]; decide) heq.symm + · rw [hvc_tcb_ow] at heq + have hvtH : H v_tcb = some .tcb := by change v_tcb ∈ H.tcbs; exact J3.tcbs ▸ hvtT + have hne_st : L.mem s (scA ct) ≠ v_tcb := fun e => by rw [e, hvtH] at hzsc; cases hzsc + have hd := J3.sys.ent.heap.disj _ (.sc zsc) v_tcb .tcb hzsc hvtH hne_st + have hfs := J3.sys.ent.heap.fits _ (.sc zsc) hzsc + have hft := (J3.iplaced.e.tcb v_tcb hvtT).fits + have hpow : 128 ≤ 2 ^ zsc := by have : 2 ^ 7 ≤ 2 ^ zsc := Nat.pow_le_pow_right (by omega) hmin_sc; simpa using this + have h72v : (72#u64 : U64).val = 72 := rfl + have hv1 : (maxA (L.mem s (scA ct))).val = (L.mem s (scA ct)).val + 72 := by + unfold maxA; rw [h72_off, wa_valm, h72v]; exact Nat.mod_eq_of_lt (by change (L.mem s (scA ct)).val + 2 ^ zsc ≤ 2 ^ 64 at hfs; omega) + have hv2 : (wa v_tcb K).val = v_tcb.val + K.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have hev := congrArg UScalar.val heq; rw [hv1, hv2] at hev + change (L.mem s (scA ct)).val + 2 ^ zsc ≤ v_tcb.val ∨ v_tcb.val + 4096 ≤ (L.mem s (scA ct)).val at hd; omega) + rw [h1]; exact mem_wr_neA (aligned_wa (hpl.vc_al hvc) 8#u64 (by decide)) hal_mx + (hpl.disj_sc vc hvc _ hscC 8#u64 layout.sc.REFILL_MAX (by decide) (by rw [h72_off]; decide)) + rw [hmx_eq]; exact hrm_nz + have hiq4_0 : (L.mem s4 (inqA ct) &&& sched.Q_RELEASE) = 0#u64 := by + unfold inqA; rw [hct_eq4 3592#u64 (by decide) (by decide)]; exact hiq_0 + have hNotRel4 : specPtr ct ∉ σ4.releaseQueue := by rw [hrel4]; exact hNotRel0 + -- Step D: `vcpu_enter_run_vm_stage3A`! + obtain ⟨v4, hkv4, -, -⟩ := J4.j.2.1.obj vc hvc + have hvc_live4 : ¬ DeadA st OS .vcpu s4 vc := fun hd => + hd slot hslot (by rw [osk_s4.capA hslot]; exact hcap) + have hEnter := vcpu_enter_run_vm_stage3A (Ub := Ub) (a0 := offset) J4_A J4.noSwitch R6_4 hS2R (hksb4 ▸ hpl) + hvc hcurT hvtT hrc4 hct_v4 hrv4 (by by_contra hne; exact hm2_bv hne) hvc_live4 + (hsc4_eq ▸ hsc_nz) hrm4_nz hiq4_0 hNotRel4 hkv4 hkt4 hrun_xct + obtain ⟨⟨s10, a4'⟩, hEnter_eq, σ10, hsp10, hsp10_r, J10, hNoSw10, R6_10, osk10, hksb10_4, hvm10, hrv10_vc⟩ := spec_ex_fd hEnter + -- Match `hdec_vg` with `hEnter_eq` + have hdec_ent : (do + let (s10', a4'') ← (do + let i22 ← lift (wa vc vcpu.RUN_CALLER) + let s5' ← st.write s4 i22 ct + let i23 ← lift (wa vc vcpu.RUN_OFFSET) + let s6' ← st.write s5' i23 offset + let i24 ← lift (wa vc vcpu.RUN_OUT_MASK) + let s7' ← st.write s6' i24 out_mask + let i25 ← layout.tcb.RUN_VCPU + let i26 ← lift (wa ct i25) + let s8' ← st.write s7' i26 vc + let (s9', a4') ← vspace.set_stage2_root st ar s8' a3' v_tcb + let s10' ← sched.set_thread_state st s9' ct sched.TS_RUNNING_VM + ok (s10', a4')) + let (s11', a5') ← vcpu.check st ar s10' a4'' ct + ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s11', a5')) = + ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s3, a3) := by + rw [← hdec_vg] + simp only [Aeneas.Std.bind_assoc] + congr 1; funext _; congr 1; funext _; congr 1; funext _; congr 1; funext _ + congr 1; funext _; congr 1; funext _; congr 1; funext _; congr 1; funext _ + congr 1; funext _; congr 1; funext ⟨s9', a4'⟩ + change (do + let (s10', a4'') ← (do + let s10' ← sched.set_thread_state st s9' ct sched.TS_RUNNING_VM + ok (s10', a4')) + let (s11', a5') ← vcpu.check st ar s10' a4'' ct + ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s11', a5')) = (do + let s10' ← sched.set_thread_state st s9' ct sched.TS_RUNNING_VM + let (s11', a5') ← vcpu.check st ar s10' a4' ct + ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s11', a5')) + rw [LawfulMonad.bind_assoc]; congr 1; funext s10' + rw [show (ok (s10', a4') : Result (S × A)) = pure (s10', a4') from rfl, LawfulMonad.pure_bind] + all_goals rfl + rw [hEnter_eq, do_ok_prod] at hdec_ent + -- Step E: `vcpu_check_running_vm_stage3A`! + have hrv10_nz : L.mem s10 (runVcpuA ct) ≠ 0#u64 := by rw [hrv10_vc]; exact (hpl.al vc hvc).1 + have hksb10 : L.ksb s10 = L.ksb s := hksb10_4.trans hksb4 + obtain ⟨⟨s11, a5'⟩, hchk_eq, hw11, σ11, hsp11, J11, -⟩ := + spec_ex_fd (vcpu_check_running_vm_stage3A J10 hNoSw10 R6_10 (hksb10 ▸ hpl) hP hAK hVR hcurT hvm10 hrv10_nz) + rw [hchk_eq, do_ok_prod] at hdec_ent + have hpair := ok_inj_ff hdec_ent + have hs11_eq : s11 = s3 := congrArg (fun p => p.2.1) hpair + have ha5_eq : a5' = a3 := congrArg (fun p => p.2.2) hpair + subst hs11_eq; subst ha5_eq + -- Spec `VcpuDecPinA` + `fl4ArchPerformInvocation` + have hframe_spec : specPtr frame = specPtr ptr + offset.bv := by + unfold frame specPtr + rw [core.num.U64.wrapping_add_bv_eq, BitVec.add_assoc, BitVec.add_comm offset.bv pptrShiftA, ← BitVec.add_assoc] + have hd_ok : (decodeVcpuRun (Z := Unit) (specPtr vc) ((offset :: in_mask :: out_mask :: rest3).map UScalar.bv) true).run σ = + ({(.ok (.run (specPtr vc) (specPtr ct) (specPtr frame) offset.bv in_mask.bv out_mask.bv vgicSpec), σ)}, False) := by + unfold decodeVcpuRun + rw [NonDetE.whenE, if_neg (by simp), ndE_pure_bindA] + rw [NonDetE.unlessE, if_pos rfl, ndE_pure_bindA] + unfold NonDetE.liftE + change ((NonDet.gets (fun s : Fl4SpecA64.State Unit => s.curThread) >>= fun c => NonDet.pure (Except.ok c)) >>= _) σ = _ + rw [nd_bind_assocSc, nd_bind_oneSc (m := NonDet.gets _) rfl, nd_bind_oneSc (m := NonDet.pure _) rfl, hct_spec] + simp only [ExceptT.bindCont] + rw [show the (List.map UScalar.bv (offset :: in_mask :: out_mask :: rest3))[1]? = in_mask.bv from rfl, + show the (List.map UScalar.bv (offset :: in_mask :: out_mask :: rest3))[2]? = out_mask.bv from rfl, + show the (List.map UScalar.bv (offset :: in_mask :: out_mask :: rest3))[0]? = offset.bv from rfl] + rw [show NonDetE.whenE (σ := Fl4SpecA64.State Unit) (in_mask.bv &&& ~~~smAll ≠ 0) (NonDetE.throwError (SyscallError.invalidArgument 1)) = pure () from by + unfold NonDetE.whenE; exact if_neg hm1_bv, ndE_pure_bindA] + rw [show NonDetE.whenE (σ := Fl4SpecA64.State Unit) (out_mask.bv &&& ~~~smAll ≠ 0) (NonDetE.throwError (SyscallError.invalidArgument 2)) = pure () from by + unfold NonDetE.whenE; exact if_neg hm2_bv, ndE_pure_bindA] + -- `getCap (specPtr ct, tcbCnodeIndex 2)` = `xct.tcbIpcframe` + obtain ⟨xct_s, hkct_s, hslots_ct⟩ := C.E.j.1.st.tcb ct ((C.E.j.1.thT ct).2 hcurT) + rw [hkx_ct] at hkct_s; cases hkct_s + have hsr2 := hslots_ct 2 (by unfold tcbSlotsA; decide) + rw [bufSlot_eqA ct] at hsr2 + have hbuf_rel : CapRelA (capAtA (L.mem s) (sbWordA ct)) xct.tcbIpcframe := by + simpa [SlotRelA, show tcbCnodeMap xct (tcbCnodeIndex 2) = some xct.tcbIpcframe from rfl] using hsr2 + have hcap_at : capAtA (L.mem s) (sbWordA ct) = + capability.Cap.Frame ptr sz rights false mapped false asid va := by + have h1 : capA st s (sbWordA ct) = capAtA (L.mem s) (sbWordA ct) := rfl + rw [← h1, hcap_buf, hdev, hept_f] + rw [hcap_at] at hbuf_rel + have hbuf_spec : xct.tcbIpcframe = + .archObjectCap (.frameCap (specPtr ptr) (vmRightsOfA rights) (frameSizeOfA sz) false (mapDataA mapped asid va)) := by + rw [← hbuf_rel.2]; rfl + change ((getCap (specPtr ct, tcbCnodeIndex 2) >>= fun c => NonDet.pure (Except.ok c)) >>= _) σ = _ + rw [nd_bind_assocSc, nd_bind_oneSc (getCap_tcbIpcframe_runA hkx_ct), nd_bind_oneSc (m := NonDet.pure _) rfl] + simp only [ExceptT.bindCont]; rw [hbuf_spec] + unfold frameRightsDev stateWindow; dsimp only + have hsz_bv : ((1 : Fl4SpecA64.Word) <<< pageBitsForSize (frameSizeOfA sz)) = size1.bv := by + apply BitVec.eq_of_toNat_eq + have hpb : pageBitsForSize (frameSizeOfA sz) = frameBitsA sz := by cases sz <;> rfl + rw [hpb, BitVec.toNat_shiftLeft, show (1 : Fl4SpecA64.Word).toNat = 1 from rfl, Nat.one_shiftLeft, + Nat.mod_eq_of_lt (Nat.pow_lt_pow_right (by decide : 1 < 2) (frameBitsA_lt64R sz)), ← hsize1_val]; rfl + rw [hsz_bv] + have hwin_ok : ¬ (offset.bv &&& 63 ≠ 0 ∨ offset.bv ≥ size1.bv ∨ size1.bv - offset.bv < smSize) := by + rintro (h | h | h) + · exact h (congrArg UScalar.bv hoff63_0) + · exact hge_sz h + · have hsub_toNat : (size1.bv - offset.bv).toNat = size1.val - offset.val := by + have hle : offset.bv.toNat ≤ size1.bv.toNat := by show offset.val ≤ size1.val; omega + rw [BitVec.toNat_sub_of_le hle]; rfl + have hlt : (size1.bv - offset.bv).toNat < (smSize : Fl4SpecA64.Word).toNat := h + have hsm : (smSize : Fl4SpecA64.Word).toNat = 1024 := by unfold smSize; decide + omega + rw [if_neg hwin_ok]; dsimp only + have hr_sp : vmRightsOfA rights = vmReadWrite := by + rw [hrights]; unfold vmRightsOfA capability.VM_READ_WRITE vmReadWrite; rfl + rw [show NonDetE.whenE (σ := Fl4SpecA64.State Unit) (false = true ∨ vmRightsOfA rights ≠ vmReadWrite) + (NonDetE.throwError SyscallError.illegalOperation) = pure () from by + unfold NonDetE.whenE; exact if_neg (by simp [hr_sp]), ndE_pure_bindA] + change ((getVcpu (specPtr vc) >>= fun v => NonDet.pure (Except.ok v)) >>= _) σ = _ + rw [nd_bind_assocSc, nd_bind_oneSc (getVcpu_eqA hkv0), nd_bind_oneSc (m := NonDet.pure _) rfl] + simp only [ExceptT.bindCont] + change ((archThreadGet ArchTcb.tcbVcpu (specPtr ct) >>= fun tv => NonDet.pure (Except.ok tv)) >>= _) σ = _ + rw [nd_bind_assocSc, nd_bind_oneSc (archThreadGet_eqA _ hkx_ct), nd_bind_oneSc (m := NonDet.pure _) rfl] + simp only [ExceptT.bindCont] + obtain ⟨xct', hkx', htv_ct, -⟩ := J3.j.2.1.tcb ct hcurT + rw [hkx_ct] at hkx'; cases hkx' + obtain ⟨v0', hkv0', -, hrc0_s⟩ := J3.j.2.1.obj vc hvc + rw [hkv0] at hkv0'; cases hkv0' + have hno_ill : ¬ (v0.vcpuTcb = none ∨ v0.vcpuTcb = some (specPtr ct) ∨ xct.tcbArch.tcbVcpu ≠ none) := by + push_neg + refine ⟨by rw [hvt0_s]; unfold optRefA; rw [if_neg hvt0]; simp, + by rw [hvt0_s]; unfold optRefA; rw [if_neg hvt0]; intro e; exact hvt_ct (specPtr_inj (Option.some.inj e)), + by rw [htv_ct, hct_v0]; rfl⟩ + have hrc_none : v0.vcpuExt.runCaller = none := by rw [hrc0_s, hrc0]; rfl + rw [show NonDetE.whenE (σ := Fl4SpecA64.State Unit) (v0.vcpuTcb = none ∨ v0.vcpuTcb = some (specPtr ct) ∨ xct.tcbArch.tcbVcpu ≠ none) + (NonDetE.throwError SyscallError.illegalOperation) = pure () from by + unfold NonDetE.whenE; exact if_neg hno_ill, ndE_pure_bindA, + show NonDetE.whenE (σ := Fl4SpecA64.State Unit) (v0.vcpuExt.runCaller ≠ none) + (NonDetE.throwError SyscallError.deleteFirst) = pure () from by + unfold NonDetE.whenE; exact if_neg (by simp [hrc_none]), ndE_pure_bindA, ← hframe_spec] + by_cases hvg_mb : in_mask.bv &&& (smVgic ||| smVgicLr) ≠ 0 + · rw [if_pos hvg_mb] + obtain ⟨-, -, -, -, hread_sp⟩ := hvg_pos hvg_mb + change ((readVgicFrame (specPtr frame) >>= fun r => NonDet.pure (Except.ok r)) >>= _) σ = _ + rw [nd_bind_assocSc, nd_bind_oneSc hread_sp, nd_bind_oneSc (m := NonDet.pure _) rfl] + simp only [ExceptT.bindCont]; rfl + · rw [if_neg hvg_mb, hvg_zero (not_not.1 hvg_mb)]; rfl + have hargs3 : args = offset :: in_mask :: out_mask :: rest3 := by rw [hargs_eq, hrest1_eq, hrest2_eq] + have hpin : VcpuDecPinA true Z.th σ slot + (.invokeArchObject (.vcpu (.run (specPtr vc) (specPtr ct) (specPtr frame) offset.bv in_mask.bv out_mask.bv vgicSpec))) := by + refine ⟨mi, sl, .archObjectCap (.vcpuCap (specPtr vc)), excaps, buffer, + (offset :: in_mask :: out_mask :: rest3).map UScalar.bv, + hargs3 ▸ hfp, fun cptr => fl4DecodeInvocation_vcpu_of_archA ?_⟩ + rw [hlbl]; unfold fl4ArchDecodeInvocation + simp only [↓reduceIte] + unfold NonDetE.liftME; change ((decodeVcpuRun _ _ true).run >>= _) σ = _ + rw [nd_bind_oneSc hd_ok]; rfl + have hperf : (.ok ([] : List Data), σ11) ∈ ((fl4ArchPerformInvocation (Z := Unit) + (.vcpu (.run (specPtr vc) (specPtr ct) (specPtr frame) offset.bv in_mask.bv out_mask.bv vgicSpec))).run + (stUpdA σ σ.curThread xct .restart)).1 := by + unfold fl4ArchPerformInvocation NonDetE.liftE + change (Except.ok [], σ11) ∈ ((fl4PerformVcpuInv _ >>= _) _).1 + have hperf_r : fl4PerformVcpuInv (Z := Unit) + (.run (specPtr vc) (specPtr ct) (specPtr frame) offset.bv in_mask.bv out_mask.bv vgicSpec) + (stUpdA σ σ.curThread xct .restart) = ({([], σ11)}, False) := by + unfold fl4PerformVcpuInv; dsimp only + have hfl : fl4VcpuFlush (Z := Unit) (specPtr vc) (stUpdA σ σ.curThread xct .restart) = + ({((), stUpdA σ σ.curThread xct .restart)}, False) := by + unfold fl4VcpuFlush NonDet.when + rw [nd_bind_oneSc (m := NonDet.gets _) rfl] + have hcur : (stUpdA σ σ.curThread xct .restart).archState.armCurrentVcpu = none := J3.curVcpuNone + rw [hcur]; rfl + rw [nd_bind_oneSc hfl] + have hsr_eq : stUpdA σ σ.curThread xct .restart = updKSc σ (specPtr ct) (.tcb { xct with tcbState := .restart }) := by + rw [hct_spec]; rfl + rw [hsr_eq, nd_bind_oneSc hsp4_r] + have h10_assoc : (do + fl4VcpuExtUpdate (Z := Unit) (specPtr vc) (fun e => + { e with runCaller := some (specPtr ct), runOffset := offset.bv, runOutMask := out_mask.bv }) + setRunVcpu (specPtr ct) (some (specPtr vc)) + setThreadState (specPtr ct) (.ext .runningVM) + fl4VcpuCheck (specPtr ct) + pure ([] : List Data)) (updKSc σ4 (specPtr ct) (.tcb { xct with tcbState := .restart })) = + ({([], σ11)}, False) := by + have h10' := hsp10_r + rw [← nd_bind_assocSc] at h10' + rw [← nd_bind_assocSc, ← nd_bind_assocSc, nd_bind_oneSc h10'] + rw [nd_bind_oneSc hsp11]; rfl + exact h10_assoc + rw [nd_bind_oneSc hperf_r]; exact Set.mem_singleton _ + exact ⟨xct, frame, offset, in_mask, out_mask, vgicSpec, hw11, σ11, hkx, hpin, hsts_spec_r, hperf, J11⟩ + | _ => rw [hcap_buf] at hdec; simp [error.illegal, bind_ok] at hdec + +/-- **The VCPU invocations that touch no thread state but the caller's own** (every one but `SetTCB` and `Run`): +`fl4PerformVcpuInv` of these is a `DlA` step (`P3/Stage3VcpuKeepA`). -/ +def VcpuPlainA : Fl4VcpuInvocation → Prop + | .setTcb _ _ => False + | .run _ _ _ _ _ _ _ => False + | _ => True + +/-- The VCPU label constants (`fl4_abi::label`) that `vcpuDecodeA` tells apart. -/ +theorem vcpuLabelNe : fl4_abi.label.ARMVCPUSetTCB ≠ fl4_abi.label.ARMVCPURun ∧ + fl4_abi.label.ARMVCPUInjectIRQ ≠ fl4_abi.label.ARMVCPURun ∧ fl4_abi.label.ARMVCPUReadReg ≠ fl4_abi.label.ARMVCPURun ∧ + fl4_abi.label.ARMVCPUWriteReg ≠ fl4_abi.label.ARMVCPURun ∧ fl4_abi.label.ARMVCPUAckVPPI ≠ fl4_abi.label.ARMVCPURun ∧ + fl4_abi.label.ARMVCPUReadState ≠ fl4_abi.label.ARMVCPURun ∧ + fl4_abi.label.ARMVCPUWriteState ≠ fl4_abi.label.ARMVCPURun ∧ + fl4_abi.label.ARMVCPURun ≠ fl4_abi.label.ARMVCPUSetTCB := by + unfold fl4_abi.label.ARMVCPUSetTCB fl4_abi.label.ARMVCPURun fl4_abi.label.ARMVCPUInjectIRQ + fl4_abi.label.ARMVCPUReadReg fl4_abi.label.ARMVCPUWriteReg fl4_abi.label.ARMVCPUAckVPPI + fl4_abi.label.ARMVCPUReadState fl4_abi.label.ARMVCPUWriteState + decide + +/-- **Unified VCPU invocation dispatcher (`JSyscallA → JStage3A`)** covering all 8 AArch64 VCPU +labels (`60` SetTCB, `61` InjectIRQ, `62` ReadReg, `63` WriteReg, `64` AckVPPI, `67` ReadState, +`68` WriteState, `69` Run) plus illegal labels. Zero global `FrameMemRelA`; zero hand-rebuilt +records; only slice-local `FrameInputAgreeA` `[scope limit]` when `WriteState`/`Run` has non-zero +input mask. -/ +theorem JSyscallA.vcpuDecodeA {ar : machine.Arch A} + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hP : VcpuPlatAgreeA ar) (hAK : VcpuArchKeepsA ar AR) + (hVR : VcpuVmRootA st ar AR) (hS2R : VcpuStage2RootA st ar AR) + (hCs : ∀ c ∈ Z.Cs, c.val < Fl4SpecA64.fl4A64Platform.numNodes) (hCsN : ∀ c ∈ Z.Cs, c.val < n) + (h1 : Fl4SpecA64.fl4A64Platform.numNodes = 1) + (hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM) + {label slot vc length buf : U64} {blk call don fp : Bool} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) + (hcap : capA st s slot = .Vcpu vc) (hvc : vc ∈ Z.V) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + {mi : MessageInfo} {sl : CslotPtr} {excaps : List (Fl4SpecA64.Cap × CslotPtr)} + {buffer : Option ObjRef} {args : List U64} + (hfp : VcpuFrontPinA Z.th σ slot mi sl (.archObjectCap (.vcpuCap (specPtr vc))) excaps buffer (args.map UScalar.bv)) + (hlbl : mi.miLabel = label.bv) + (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) + (hex0 : ∀ s1, Array.index_usize extra 0#usize = ok s1 → s1 ≠ 0#u64 → + ∃ c0 sl0, excaps[0]? = some (c0, sl0) ∧ CapRelA (capA st s s1) c0) + (hNoZomb : ∀ s1 ptr, Array.index_usize extra 0#usize = ok s1 → capA st s s1 = .Thread ptr → + ∀ u ∈ OS, ∀ num, capA st s u ≠ .Zombie ptr capability.ZOMBIE_TCB num) + (hNoS2 : ∀ s1 p sz r d mp ept asid va, Array.index_usize extra 0#usize = ok s1 → + capA st s s1 = .Frame p sz r d mp ept asid va → ept = false) + -- `[scope limit]` slice-local input-frame agreement for `WriteState` (68) when `mask ≠ 0`. + (hAgreeWS : ∀ s1 p sz r mp ept asid va offset mask, + Array.index_usize extra 0#usize = ok s1 → s1 ≠ 0#u64 → + capA st s s1 = .Frame p sz r false mp ept asid va → + args[0]? = some offset → args[1]? = some mask → mask ≠ 0#u64 → + FrameInputAgreeA st s σ (wa p offset)) + -- `[scope limit]` slice-local input-frame agreement for `Run` (69) when `in_mask ≠ 0`. + (hAgreeRun : ∀ ptr sz rights mp ept asid va offset in_mask, + capA st s (sbWordA (L.curW s)) = .Frame ptr sz rights false mp ept asid va → + args[0]? = some offset → args[1]? = some in_mask → in_mask ≠ 0#u64 → + FrameInputAgreeA st s σ (wa ptr offset)) + {s3 : S} {a3 : A} + (hdec : syscall.decode_invocation st ar s a label length slot (.Vcpu vc) + blk call don fp buf extra = ok (core.result.Result.Ok (), s3, a3)) : + ∃ (x : Fl4SpecA64.Tcb) (inv : Fl4VcpuInvocation) (hw' : HwRootA) (Ac' : Set U64) (σ3 : Fl4SpecA64.State Unit), + σ.kheap σ.curThread = some (.tcb x) ∧ + VcpuDecPinA call Z.th σ slot (.invokeArchObject (.vcpu inv)) ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + (((.ok [], σ3) ∈ ((fl4ArchPerformInvocation (Z := Unit) (.vcpu inv)).run + (stUpdA σ σ.curThread x .restart)).1) ∨ + (∃ σ2 ws, + (.ok ws, σ2) ∈ ((fl4ArchPerformInvocation (Z := Unit) (.vcpu inv)).run + (stUpdA σ σ.curThread x .restart)).1 ∧ + ((), σ3) ∈ ((do + Fl4Spec.NonDet.when call (Fl4SpecA64.replyFromKernel (Z := Unit) σ.curThread (0, ws)) + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .running) σ2).1)) ∧ + JStage3A st AR { Z with hw := hw' } H OS U Ac' Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s3 a3 σ3 m ∧ + ((label ≠ fl4_abi.label.ARMVCPUSetTCB → label ≠ fl4_abi.label.ARMVCPURun → VcpuPlainA inv) ∧ + (label = fl4_abi.label.ARMVCPUSetTCB → (∃ v t, inv = .setTcb v t) ∧ + (.ok [], σ3) ∈ ((fl4ArchPerformInvocation (Z := Unit) (.vcpu inv)).run + (stUpdA σ σ.curThread x .restart)).1) ∧ + (label = fl4_abi.label.ARMVCPURun → + (∃ v fr o i p g, inv = .run v (specPtr (L.curW s)) fr o i p g) ∧ + (.ok [], σ3) ∈ ((fl4ArchPerformInvocation (Z := Unit) (.vcpu inv)).run + (stUpdA σ σ.curThread x .restart)).1)) := by + by_cases h60 : label = fl4_abi.label.ARMVCPUSetTCB + · subst h60 + obtain ⟨x, ptr, hw', σ3, hx, hpin, hst, hperf, J3'⟩ := + J.vcpuSetTcbA hA hM hPl hfm hP hAK hVR hCs h1 hnvm hslot hcov hcap hvc hext hSC hfp + (by rw [hlbl]; unfold fl4_abi.label.ARMVCPUSetTCB fl4LabelOf; rfl) hex0 hNoZomb hdec + exact ⟨x, .setTcb (specPtr vc) (specPtr ptr), hw', Ac, σ3, hx, hpin, hst, Or.inl hperf, ⟨J3', fun h => absurd rfl h, fun _ => ⟨⟨_, _, rfl⟩, hperf⟩, fun h => absurd h vcpuLabelNe.1⟩⟩ + by_cases h61 : label = fl4_abi.label.ARMVCPUInjectIRQ + · subst h61 + obtain ⟨x, idx, lr, σ3, Ac', hx, hpin, hst, hperf, J3'⟩ := + J.vcpuInjectIrqA hA hM hPl hfm hP hAK hCs hCsN h1 hnvm hslot hcov hcap hvc hext hSC hfp + (by rw [hlbl]; unfold fl4_abi.label.ARMVCPUInjectIRQ fl4LabelOf; rfl) hlen hargs hdec + exact ⟨x, .injectIrq (specPtr vc) idx lr, Z.hw, Ac', σ3, hx, hpin, hst, Or.inl hperf, ⟨J3', fun _ _ => trivial, fun h => absurd h h60, fun h => absurd h vcpuLabelNe.2.1⟩⟩ + by_cases h62 : label = fl4_abi.label.ARMVCPUReadReg + · subst h62 + obtain ⟨x, field, σ2, σ3, ws, Ac', hx, hpin, hst, hperf, hrep, J3'⟩ := + J.vcpuReadRegA hA hM hPl hfm hP hAK hCs hCsN h1 hnvm hslot hcov hcap hvc hext hSC hfp + (by rw [hlbl]; unfold fl4_abi.label.ARMVCPUReadReg fl4LabelOf; rfl) hlen hargs hdec + exact ⟨x, .readReg (specPtr vc) field, Z.hw, Ac', σ3, hx, hpin, hst, Or.inr ⟨σ2, ws, hperf, hrep⟩, ⟨J3', fun _ _ => trivial, fun h => absurd h h60, fun h => absurd h vcpuLabelNe.2.2.1⟩⟩ + by_cases h63 : label = fl4_abi.label.ARMVCPUWriteReg + · subst h63 + obtain ⟨x, field, val, σ3, Ac', hx, hpin, hst, hperf, J3'⟩ := + J.vcpuWriteRegA hA hM hPl hfm hP hAK hCs hCsN h1 hnvm hslot hcov hcap hvc hext hSC hfp + (by rw [hlbl]; unfold fl4_abi.label.ARMVCPUWriteReg fl4LabelOf; rfl) hlen hargs hdec + exact ⟨x, .writeReg (specPtr vc) field val, Z.hw, Ac', σ3, hx, hpin, hst, Or.inl hperf, ⟨J3', fun _ _ => trivial, fun h => absurd h h60, fun h => absurd h vcpuLabelNe.2.2.2.1⟩⟩ + by_cases h64 : label = fl4_abi.label.ARMVCPUAckVPPI + · subst h64 + obtain ⟨x, σ3, Ac', hx, hpin, hst, hperf, J3'⟩ := + J.vcpuAckVppiA hA hM hPl hfm hAK hCs hCsN h1 hnvm hslot hcov hcap hvc hext hSC hfp + (by rw [hlbl]; unfold fl4_abi.label.ARMVCPUAckVPPI fl4LabelOf; rfl) hlen hargs hdec + exact ⟨x, .ackVppi (specPtr vc), Z.hw, Ac', σ3, hx, hpin, hst, Or.inl hperf, ⟨J3', fun _ _ => trivial, fun h => absurd h h60, fun h => absurd h vcpuLabelNe.2.2.2.2.1⟩⟩ + by_cases h67 : label = fl4_abi.label.ARMVCPUReadState + · subst h67 + obtain ⟨x, frame, mask, σ3, hx, hpin, hst, hperf, J3'⟩ := + J.vcpuReadStateA hA hM hPl hfm hP hAK hCs hCsN h1 hnvm hslot hcov hcap hvc hext hSC hfp + (by rw [hlbl]; unfold fl4_abi.label.ARMVCPUReadState fl4LabelOf; rfl) hlen hargs hex0 hNoS2 hdec + exact ⟨x, .stateTransfer (specPtr vc) (specPtr frame) mask.bv true (0, []), Z.hw, Ac, σ3, hx, hpin, hst, Or.inl hperf, ⟨J3', fun _ _ => trivial, fun h => absurd h h60, fun h => absurd h vcpuLabelNe.2.2.2.2.2.1⟩⟩ + by_cases h68 : label = fl4_abi.label.ARMVCPUWriteState + · subst h68 + obtain ⟨x, frame, mask, vgicSpec, σ3, hx, hpin, hst, hperf, J3'⟩ := + J.vcpuWriteStateA hA hM hPl hfm hP hAK hCs hCsN h1 hnvm hslot hcov hcap hvc hext hSC hfp + (by rw [hlbl]; unfold fl4_abi.label.ARMVCPUWriteState fl4LabelOf; rfl) hlen hargs hex0 hNoS2 hAgreeWS hdec + exact ⟨x, .stateTransfer (specPtr vc) (specPtr frame) mask.bv false vgicSpec, Z.hw, Ac, σ3, hx, hpin, hst, Or.inl hperf, ⟨J3', fun _ _ => trivial, fun h => absurd h h60, fun h => absurd h vcpuLabelNe.2.2.2.2.2.2.1⟩⟩ + by_cases h69 : label = fl4_abi.label.ARMVCPURun + · subst h69 + obtain ⟨x, frame, offset, inMask, outMask, vgicSpec, hw', σ3, hx, hpin, hst, hperf, J3'⟩ := + J.vcpuRunA hA hM hPl hfm hP hAK hVR hS2R hCs hCsN h1 hnvm hslot hcov hcap hvc hext hSC hfp + (by rw [hlbl]; unfold fl4_abi.label.ARMVCPURun fl4LabelOf; rfl) hlen hargs hAgreeRun hdec + exact ⟨x, .run (specPtr vc) (specPtr (L.curW s)) (specPtr frame) offset.bv inMask.bv outMask.bv vgicSpec, hw', Ac, σ3, hx, hpin, hst, Or.inl hperf, ⟨J3', fun _ h => absurd rfl h, fun h => absurd h vcpuLabelNe.2.2.2.2.2.2.2, fun _ => ⟨⟨_, _, _, _, _, _, rfl⟩, hperf⟩⟩⟩ + -- Illegal label: `vcpu.decode` returns `error.illegal` + unfold syscall.decode_invocation at hdec + rw [show sched.cur_thread st s = ok (L.curW s) from eq_of_spec_fd (sched.cur_thread.spec s), bind_ok] at hdec + dsimp only at hdec; unfold vcpu.decode at hdec + rw [if_neg h60, if_neg h61, if_neg h62, if_neg h63, if_neg h64, if_neg h67, if_neg h68, if_neg h69] at hdec + simp [error.illegal, bind_ok] at hdec + +end decodeRun + +end +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3T3DecEqA.lean b/proofs/Fl4ProofsA64/P3/Stage3T3DecEqA.lean new file mode 100644 index 000000000..ab545f7ec --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3T3DecEqA.lean @@ -0,0 +1,129 @@ +/- +P3 (aarch64, A64-P3 Stage 3, T3 step 2): **decode equations** from the decode-site lookup results to the kernel call +of a CNode Copy / Mint / Move / Mutate / Rotate invocation and of a high-window frame map. + +Each equation is `decode_invocation` (or `decode_frame`) unfolded along its success path; the hypotheses are the +results FL4's own checks return at the entry state (message words, slot lookups, derived caps). + +Named premises: none beyond those results. +-/ +import Fl4ProofsA64.P3.CnodeTopA +import Fl4ProofsA64.P3.CnodeRotA +import Fl4ProofsA64.P3.VsArmA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 + +theorem capIsNull_falseA {c : capability.Cap} (h : c ≠ .Null) : capability.Cap.is_null c = ok false := by + cases c <;> first | rfl | exact absurd rfl h + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {s : S} {a : A} {lbl length slot buf : U64} {block call donate : Bool} +variable {extra : Aeneas.Std.Array obj.Slot 3#usize} {p r gs g index depth : U64} {dest src x0 : U64} + +/-- **CNode Copy / Mint / Move / Mutate**: the invocation is `Restart`, then `move_cap` or `insert_with_cap`. -/ +theorem decCnode_copyA (hlo : ¬ lbl < fl4_abi.label.CNodeRevoke) (hhi : ¬ lbl > fl4_abi.label.CNodeRotate) + (g1 : lbl ≥ fl4_abi.label.CNodeCopy) (g2 : lbl ≤ fl4_abi.label.CNodeMutate) + (hl2 : ¬ length < 2#u64) (hl4 : ¬ length < 4#u64) + (hi : msg.syscall_arg st s (L.curW s) buf 0#u64 = ok index) + (hd : msg.syscall_arg st s (L.curW s) buf 1#u64 = ok depth) + (hres : cspace.lookup_target_slot st s (.CNode p r gs g) index depth = ok (.Ok dest)) + (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) (h0 : x0 ≠ 0#u64) + {si sdp : U64} (h2 : msg.syscall_arg st s (L.curW s) buf 2#u64 = ok si) + (h3 : msg.syscall_arg st s (L.curW s) buf 3#u64 = ok sdp) + {root : capability.Cap} (hroot : obj.Slot.cap st x0 s = ok root) + (hemp : cdt.ensure_empty_slot st s dest = ok (.Ok ())) + (hsrc : cspace.lookup_source_slot st s root si sdp = ok (.Ok src)) + {c : capability.Cap} (hcap : obj.Slot.cap st src s = ok c) (hc : c ≠ .Null) + {nc : capability.Cap} {mv : Bool} + (hcop : cnode.copied_cap st s lbl length buf src c = ok (.Ok (nc, mv))) (hnc : nc ≠ .Null) : + syscall.decode_invocation st ar s a lbl length slot (.CNode p r gs g) block call donate false buf extra = + (do let s2 ← sched.set_thread_state st s (L.curW s) sched.TS_RESTART + let s3 ← if mv then cdt.move_cap st s2 nc src dest else cdt.insert_with_cap st s2 nc c src dest + ok (core.result.Result.Ok (), s3, a)) := by + rw [decodeCnode_programA, if_neg Bool.false_ne_true] + unfold cnode.decode + rw [if_neg hlo, if_neg hhi, if_neg hl2] + simp only [cur_thread_ok, bind_ok, hi, hd, hres, core.result.Result.Insts.CoreOpsTry.branch, if_pos g1, if_pos g2] + unfold cnode.decode_copy + have hx : obj.Slot.is_null x0 = ok false := by + unfold obj.Slot.is_null; simp [h0] + rw [if_neg hl4] + simp only [hx0, hx, cur_thread_ok, bind_ok, h2, h3, hroot, hemp, hsrc, hcap, capIsNull_falseA hc, + capIsNull_falseA hnc, hcop, core.result.Result.Insts.CoreOpsTry.branch, Bool.false_eq_true, ↓reduceIte, + uncurry_apply_pair] + cases mv <;> simp only [Bool.false_eq_true, ↓reduceIte, bind_assoc, bind_assoc_eq, Aeneas.Std.bind_assoc, bind_tc_ok, bind_ok, uncurry_apply_pair, Aeneas.Std.uncurry] + +/-- **CNode Rotate**: `Restart`, then `swap` (source = destination) or the two moves. -/ +theorem decCnode_rotateA (hlbl : lbl = fl4_abi.label.CNodeRotate) + (hlo : ¬ lbl < fl4_abi.label.CNodeRevoke) (hhi : ¬ lbl > fl4_abi.label.CNodeRotate) + (hl2 : ¬ length < 2#u64) (hl8 : ¬ length < 8#u64) + (hi : msg.syscall_arg st s (L.curW s) buf 0#u64 = ok index) + (hd : msg.syscall_arg st s (L.curW s) buf 1#u64 = ok depth) + (hres : cspace.lookup_target_slot st s (.CNode p r gs g) index depth = ok (.Ok dest)) + (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) (h0 : x0 ≠ 0#u64) + {x1 : U64} (hx1 : Aeneas.Std.Array.index_usize extra 1#usize = ok x1) (h1 : x1 ≠ 0#u64) + {pdata sdata : U64} (h2 : msg.syscall_arg st s (L.curW s) buf 2#u64 = ok pdata) + (h5 : msg.syscall_arg st s (L.curW s) buf 5#u64 = ok sdata) + {pivot : U64} (hrot : cnode.rotate_slots st s buf extra dest = ok (.Ok (src, pivot))) + {cs cp ns np : capability.Cap} (hcs : obj.Slot.cap st src s = ok cs) (hcp : obj.Slot.cap st pivot s = ok cp) + (hns : objects.update_cap_data true sdata cs = ok ns) (hnp : objects.update_cap_data true pdata cp = ok np) + (hns0 : ns ≠ .Null) (hnp0 : np ≠ .Null) : + syscall.decode_invocation st ar s a lbl length slot (.CNode p r gs g) block call donate false buf extra = + (do let s3 ← sched.set_thread_state st s (L.curW s) sched.TS_RESTART + let s5 ← if src = dest then cdt.swap st s3 ns src np pivot + else (do let s4 ← cdt.move_cap st s3 np pivot dest + cdt.move_cap st s4 ns src pivot) + ok (core.result.Result.Ok (), s5, a)) := by + rw [decodeCnode_programA, if_neg Bool.false_ne_true] + unfold cnode.decode + rw [if_neg hlo, if_neg hhi, if_neg hl2] + have g1 : lbl ≥ fl4_abi.label.CNodeCopy := by subst hlbl; have := lblA_copy; have := lblA_rotate; scalar_tac + have g2 : ¬ lbl ≤ fl4_abi.label.CNodeMutate := by + subst hlbl; have := lblA_mutate; have := lblA_rotate; scalar_tac + have n1 : ¬ lbl = fl4_abi.label.CNodeRevoke := fun e => by + have h := congrArg UScalar.val (hlbl.symm.trans e); have := lblA_revoke; have := lblA_rotate; omega + have n2 : ¬ lbl = fl4_abi.label.CNodeDelete := fun e => by + have h := congrArg UScalar.val (hlbl.symm.trans e); have := lblA_delete; have := lblA_rotate; omega + have n3 : ¬ lbl = fl4_abi.label.CNodeCancelBadgedSends := fun e => by + have h := congrArg UScalar.val (hlbl.symm.trans e); have := lblA_cancel; have := lblA_rotate; omega + simp only [cur_thread_ok, bind_ok, hi, hd, hres, core.result.Result.Insts.CoreOpsTry.branch, if_pos g1, if_neg g2, + if_neg n1, if_neg n2, if_neg n3, if_pos hlbl] + unfold cnode.decode_rotate + have hx : obj.Slot.is_null x0 = ok false := by + unfold obj.Slot.is_null; simp [h0] + have hx' : obj.Slot.is_null x1 = ok false := by + unfold obj.Slot.is_null; simp [h1] + rw [if_neg hl8] + simp only [hx0, hx, hx1, hx', cur_thread_ok, bind_ok, h2, h5, hrot, hcs, hcp, hns, hnp, capIsNull_falseA hns0, + capIsNull_falseA hnp0, core.result.Result.Insts.CoreOpsTry.branch, Bool.false_eq_true, ↓reduceIte, + uncurry_apply_pair, slot_eq_eqA] + by_cases e : src = dest <;> simp only [e, decide_true, decide_false, Bool.false_eq_true, ↓reduceIte, bind_assoc, + bind_assoc_eq, Aeneas.Std.bind_assoc, bind_tc_ok, bind_ok, uncurry_apply_pair, Aeneas.Std.uncurry] + +/-- **A frame map with a `HighVSpace` extra cap** is `decode_high_page_map`. -/ +theorem decFrame_hiA {fp rt asid va : U64} {sz : capability.FrameSize} {dev mp : Bool} + (hx : vspace.extra extra 0#usize = ok (some x0)) (hlen : length ≥ 3#u64) + {vaddr rmask attr vm : U64} (h0 : msg.syscall_arg st s (L.curW s) buf 0#u64 = ok vaddr) + (h1 : msg.syscall_arg st s (L.curW s) buf 1#u64 = ok rmask) + (h2 : msg.syscall_arg st s (L.curW s) buf 2#u64 = ok attr) + {rr : capability.Rights} (hr : capability.Rights.from_word rmask = ok rr) + (hvm : capability.mask_vm_rights rt rr = ok vm) + {p1 asid1 : U64} {mp1 : Bool} (hc : obj.Slot.cap st x0 s = ok (.HighVSpace p1 mp1 asid1)) : + vspace.decode_frame st ar s a fl4_abi.label.ARMPageMap length slot (.Frame fp sz rt dev mp false asid va) call buf + extra = + vspace.decode_high_page_map st ar s a slot + { ptr := fp, size := sz, rights := rt, is_device := dev, mapped := mp, is_ept := false, asid := asid, + vaddr := va } + (.HighVSpace p1 mp1 asid1) vaddr vm attr := by + unfold vspace.decode_frame + simp only [vspace.as_frame, cur_thread_ok, bind_ok, ↓reduceIte] + unfold vspace.decode_page_map + simp only [cur_thread_ok, bind_ok, hx, if_pos hlen, h0, h1, h2, hr, hvm, hc, Bool.false_eq_true, ↓reduceIte, + ite_self] + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3TA.lean b/proofs/Fl4ProofsA64/P3/Stage3TA.lean new file mode 100644 index 000000000..0f4655379 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3TA.lean @@ -0,0 +1,493 @@ +/- +P3 (AArch64, delete hooks E4, Architect ruling Option T, 2026-12-03 08:39Z): **the deletion loop's per-step record**. +`JStage3TA` is `JStage3A` (`P3/Stage3A`) with `JEntA.sys : SysInvA` replaced by its first half `ent : EntryInvA`: A64 P1 +keeps `SysL` through the deletion loop only with per-slot loop facts (`DelS.fieldS`), so the per-step record drops it and +the deletion's twin top restores it once from P1's whole-function deletion tops. Generated by +`handoff/tools/gen_stage3ta.py` from the two structures (fields verbatim, same names). +`JStage3A.toTA` forgets `SysL`; `JStage3TA.toJA` puts it back. +No named premise beyond `JStage3A`'s [J] fields (same fields). New module. +-/ +import Fl4ProofsA64.P3.Stage3A +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched + +noncomputable section + +section +variable {S : Type} (st : machine.Store S) [L : LawfulStore st] {A : Type} + +/-- **`JEntA` without `SysL`** (module docstring). -/ +structure JEntTA (AR : A → HwRootA → Prop) (Z : XSetsA) (H : Heap) (OS U Ac Pp Tb : Set U64) (Lv : U64 → Nat) + (ia ie : Nat) (s : S) (a : A) (σ : Fl4SpecA64.State Unit) (m : Fl4SpecA64.McState) : Prop where + /-- (Option T) A64 P1's entry invariants without `SysL` (restored at the deletion top). -/ + ent : EntryInvA3 st H Z.Cs OS U Ac Pp Tb Lv s + j : JAtZAX (L := L) AR Z s a σ m + tcbs : H.tcbs = Z.T + /-- (gen 3, ruling 09:59Z item 1) The P1 heap's SCs are the relation's (x86 `JAtZB.zH`'s SC half). -/ + scs : H.scs = Z.C + /-- (gen 3, `SchedRestA`) The relation's queue ghosts are A64 P1's queue lists (`SchedQueuesInv`: FL4's ready / + release queue words hold `Z.Q` / `Z.R`; x86 `SchedCore.ready` / `release`, inside x86's relation). -/ + sq : SchedQueuesInv st H.tcbs Z.Cs H.omega H s Z.Q Z.R + /-- (gen 7, ruling 12:26Z item 1) The slot apartness facts (`SlotApA`; x86: inside `JAtZ` / `TopPlaced`). -/ + ap : SlotApA Z H OS (L.ksb s) + /-- (gen 13, ruling 15:58Z item 1) The relation's ASID pools are A64 P1's (`VsTy`'s `Pp`). -/ + pp : Z.Pp = Pp + /-- (gen 13) The relation's Stage-1 tables are among A64 P1's tables (`VsTy`'s `Tb`; VS gen 2 Q2). -/ + s1 : Z.S1 ⊆ Tb + /-- (gen 13) A64 P1's `HeapKsOff` (S1a fact 1): heap objects off the kernel-state block, `other` ones possibly in + its two static regions (x86 `JKern.hks` / `hksIn`). -/ + ko : HeapKsOff H (L.ksb s) + /-- (gen 13) The pools' and Stage-1 tables' pages lie inside `other` objects of `H` (`PtHA`). -/ + ptH : PtHA H (Z.Pp ∪ Z.S1) + /-- The ASID table is injective on its occupied entries (`SpecVsA.tabInj`). -/ + tabInj : ∀ i j p, σ.archState.armAsidTable i = some p → σ.archState.armAsidTable j = some p → i = j + /-- The live pools' words have P1's `PoolOff` placement. -/ + poolOff : PoolOff st H Z.Cs OS s + /-- (Architect 21:51Z D1, J1) The retained joint objects lie off untyped memory or below its watermark. -/ + objAlloc : ObjAllocA (st := st) Z H OS s + /-- (D1, J1) The synthetic idle-SC anchor is off untyped memory. -/ + idleAnchor : IdleAnchorOffA (st := st) OS s + /-- The VSpace footprint misses the VCPU words (`VrOK.vc` on this relation's pages). -/ + vsVc : ∀ b, VsFootA Z.Pp Z.S1 Z.Y (L.ksb s) b → ¬ VcW Z.V b + +/-- **The per-step record of the deletion loop** (module docstring). -/ +structure JStage3TA (AR : A → HwRootA → Prop) (Z : XSetsA) (H : Heap) (OS U Ac Pp Tb : Set U64) (Lv : U64 → Nat) + (ia ie : Nat) (N E Rp : Set U64) (Bf : Set (U64 × Fl4SpecA64.Word)) (stack : U64 → List U64) (κ : U64 → U64) + (n : Nat) (J5 J7 : JSlotA S) (s : S) (a : A) (σ : Fl4SpecA64.State Unit) (m : Fl4SpecA64.McState) : Prop + extends JEntTA st AR Z H OS U Ac Pp Tb Lv ia ie s a σ m where + sr : SchedSRA st Z m κ s σ + scCov : ScCovA Z.C m σ + /-- (Architect ruling, 2026-12-03) [J] Every related spec SC object lies inside its heap object (kept by `SSzA`; + Retype preservation is the Untyped line's); (record edit #8b-D, Architect 11:22Z) a non-idle one fills it exactly + (idle SCs are 256 B with size exponent 0, so not unconditional). -/ + scSize : ∀ c ∈ Z.C, ∀ x n, σ.kheap (scNameA m c) = some (.schedContext x n) → + n + Fl4SpecA64.minSchedContextBits ≤ H.scBits c ∧ + (Sched.NotIdleScA m (scNameA m c) → n + Fl4SpecA64.minSchedContextBits = H.scBits c) + ep : EpStateRel Z.T E Rp Bf (L.mem s) σ + ntfn : NtfnStateRel N Z.T (L.mem s) σ + reply : ReplyStackRelA (L.mem s) Rp Z.C σ (scNameA m) stack + bufCover : BufCover st Z.T Bf s σ + bufNative : BufNativeA Z.T (L.mem s) + iplaced : IPlacedA N Z.T E Rp Bf + ipcPlace : IpcSchedPlacementA Z.th N E Rp Bf + idleNoFault : IdleNoFaultA m σ + mcp : McpRelA Z.T m (L.mem s) σ + yieldRel : YieldRelA Z.T Z.C m (L.mem s) σ + sinv : SInvXA σ m n + setsInH : SetsInHA Z H + /-- (Architect 00:54Z) [J] The IPC ghosts' notifications, endpoints and replies are heap objects of those kinds. -/ + utIpc : UtIpcHeapA H N E Rp + ipcTh : IpcThA Z.th N E Rp + deadExt : DeadExtA st Z OS Rp stack s σ + specFree : SpecFreeA st OS s σ + utBlk : UtBlk39A st OS s + unmPt : UnmPtSpecA st Z m OS s σ + tabS1 : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s q + tabExt : ExtTabOffA (L := L) Z m s + specZero : SpecZeroFreeA st OS s σ + /-- (Architect 03:22Z) [J] **`OrigLiveA`**: every original cap's slot is a slot of `th` (x86 `JKern.orig`). -/ + origLive : ∀ sl, σ.isOriginalCap sl = true → AbsSlotA Z.th sl + /-- (Architect 03:29Z) [J] **`nullOrig`** (l4v `valid_nullcaps`): a null slot is not original. -/ + nullOrig : ∀ sl, specCapAtA σ sl = some .nullCap → σ.isOriginalCap sl = false + /-- (Architect 00:06Z, closure D) [J] every `other` object of `th` is nonempty. -/ + thPos : ∀ p z, Z.th p = some (.other z) → 0 < z + /-- [J] (D1) No `OS` frame cap shares its base with a range key cap (`P3/CnodeFinalA`). -/ + frBase : FrBaseApartA st OS s + /-- [J] (D1) A device frame cap names no non-data-page object (`P3/DevFrDisjA`). -/ + devFr : DevFrDisjA st H OS s σ + /-- [J] (D1) Every spec CNode and TCB is in the typed heap (`P3/CnodeFinalA`). -/ + finCov : FinCovA Z.th σ + /-- [J] (D1, J13) Every core's idle objects lie in the idle area. -/ + idleAll : IdleAllA st ia ie Z.Cs m s + /-- [J] (D1, N1) The converse of `utIpc`. -/ + ipcHeapCov : IpcHeapCovA H N E Rp + /-- [J] (D1, N2) A TCB under a TCB zombie has no VCPU. -/ + zombieNoVcpu : ZombieTcbNoVcpuA st OS s + /-- [J] (D1) An `OS` TCB zombie's base is a heap TCB. -/ + zombieTcb : ZombieTcbTA st H OS s + /-- [J] (D1) The tracked buffer words are off the `OS` slots, VSpace words, table pages and VCPU words. -/ + bufOff : BufOffA st Z OS s Bf + /-- [J] (D1b) The installed pools' and reachable tables' words are placed (`P3/Stage3D1bA`). -/ + vsOk : UtVsOkA st H OS Z.V Pp Tb Lv s + /-- [J] (D1b) Every TCB's IPC buffer is aligned to 1024. -/ + bufAl : UtBufAlA Z.T σ + /-- [J] (D1b) A `Zombie`'s count fits its object. -/ + zombieBnd : UtZombieA st OS s + /-- [J] (D1b-4) Device untyped regions are apart from the record (`P3/Stage3FieldsA`). -/ + devUtOff : DevUtOffA st Z H OS ia ie s + /-- [J] (F3–F6, Architect 15:06Z) The idle objects of all cores (`SIdleA`). -/ + sidle : SIdleA σ m n + /-- [J] (Architect 17:31Z) The `OS` caps' heap kinds (`CapTyA`). -/ + capTy : CapTyA st H OS s + /-- [J] (record edit #3, Architect 19:30Z) Every related SC's core (`κ`, its `scCoreA` word) is a kernel core. -/ + scCpu : ∀ c ∈ Z.C, (κ c).val < Fl4SpecA64.fl4A64Platform.numNodes + /-- [J] (record edit #3) No related SC is at address `0`. -/ + scNZ : ∀ c ∈ Z.C, c ≠ 0#u64 + /-- [J] (record edit #3) The VCPU binding words (`VcRunA`). -/ + vcRun : VcRunA st Z.T Z.V s + /-- [J] (record edit #4, Architect 20:21Z) P1's `RunVcpuInv`. -/ + runVcpu : RunVcpuInv st Z.T s + /-- [J] (record edit #4) Every reply on an SC's stack is a blocked reply (weak `ValidReplies`). -/ + vr : ∀ sc ∈ Z.C, ∀ r ∈ stack sc, RBlk st s r + /-- [J] (record edit #5) Blocked ⇒ queued (`BlkQueuedA`). -/ + blkQ : BlkQueuedA Z.T E N (L.mem s) σ + /-- [J] (record edit #4) No `OS` SC cap names the current core's idle SC (the SC half of `SysL.ci`). -/ + ciSc : ∀ u ∈ OS, ∀ p b, capA st s u = .SchedContext p b → specPtr p ≠ m.idleScOf m.curCore + /-- [J] (record edit #4) No thread switch is pending (holds at hook boundaries). -/ + noSwitch : ∀ u, σ.schedulerAction ≠ .switchThread u + /-- [J] (F1–F2, Architect 15:06Z) Scheduler action and current thread (`SActA`): carried by the loop, not by + `JStage3A`. -/ + sact : SActA σ + /-- [J] (record edit #7) `arm_current_vcpu` is empty. -/ + curVcpuNone : σ.archState.armCurrentVcpu = none + /-- [J] (record edit #8) Endpoint queues and notification wait queues are sorted by priority (`IpcSortedA`). -/ + ipcSorted : IpcSortedA (specPtr '' E) (specPtr '' N) σ + /-- [J] (record edit #8) An SC with replies is active (l4v `active_reply_scs`). -/ + activeReplyScs : ActRepA σ + /-- [J] (record edit #8) No tracked IPC-buffer word is a reachable VSpace word. -/ + bufVs : ∀ b, BufW Bf b → ¬ VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b + /-- [J] (record edit #8b-B1) The concrete domain-schedule words match the spec's (`NdOKA`). -/ + domOK : Sched.NdOKA (st := st) s σ + /-- [J] (record edit B2) This core's IPI pair is empty (`SilentScheduleIpiA`). -/ + ipiZero : Sched.SilentScheduleIpiA (st := st) s + j5 : J5 Z H OS s σ m + j7 : J7 Z H OS s σ m + +variable {st} {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- `JEntA` without `SysL`. -/ +theorem JEntA.toTA (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) : + JEntTA st AR Z H OS U Ac Pp Tb Lv ia ie s a σ m where + ent := E.sys.ent + j := E.j + tcbs := E.tcbs + scs := E.scs + sq := E.sq + ap := E.ap + pp := E.pp + s1 := E.s1 + ko := E.ko + ptH := E.ptH + tabInj := E.tabInj + poolOff := E.poolOff + objAlloc := E.objAlloc + idleAnchor := E.idleAnchor + vsVc := E.vsVc + +/-- The SCs are aligned (`JEntA.scAl`). -/ +theorem JEntTA.scAl (J : JEntTA st AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) : ∀ c ∈ Z.C, Aligned c := by + intro c hc + rw [← J.scs] at hc + obtain ⟨z, hz⟩ := hc + have h1 := J.ent.heap.align c _ hz + have h2 := J.ent.heap.scmin c z hz + simp only [HKind.bits] at h1 + have : 8 ∣ 2 ^ z := ⟨2 ^ (z - 3), by rw [show z = 3 + (z - 3) by omega, pow_add]; simp⟩ + exact Nat.mod_eq_zero_of_dvd (Nat.dvd_trans this (Nat.dvd_of_mod_eq_zero h1)) + +/-- The table words are off the ASID table (`JEntA.vsLeafA`). -/ +theorem JEntTA.vsLeafA (J : JEntTA st AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) : + Aligned (L.ksb s) ∧ (∀ p ∈ Z.S1, ∀ i : U64, i.val < 512 → ¬ AsW (L.ksb s) (peAF p i)) ∧ + (∀ p ∈ Z.Pp, ∀ i : U64, i.val < 512 → ¬ AsW (L.ksb s) (peAF p i)) := by + have h32 := J.j.1.ksbAl + refine ⟨show (L.ksb s).val % 8 = 0 by omega, fun p hp i hi => ?_, fun p hp i hi => ?_⟩ + · exact ptHA_not_asW J.ent.heap J.ko J.ent.ksw J.ptH (Or.inr hp) hi + · exact ptHA_not_asW J.ent.heap J.ko J.ent.ksw J.ptH (Or.inl hp) hi + +/-- The reply words from an SC's `reply` word along its stack are blocked replies. -/ +theorem stackBlkA (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) : + ∀ sc ∈ Z.C, ∀ r ∈ stack sc, RBlk st s r := by + intro sc hc r hr + obtain ⟨x, k, hk, hrel⟩ := J.sr.scr.sc sc hc + obtain ⟨⟨y, k', hy, eq⟩, hm, -, hw⟩ := J.reply.stackRel sc hc + rw [hk] at hy; cases hy + have PE := J.iplaced.e + have head : ∀ r0 l, stack sc = r0 :: l → RBlk st s r0 := by + intro r0 l e + have he := hrel.reply + rw [← Sched.scReplyA_eqS, eq, e] at he + have hne : L.mem s (scReplyA sc) ≠ 0#u64 := fun h => by + rw [h, Sched.optRefS_zero] at he; cases he + rw [Sched.optRefS_ne hne] at he + have e0 : L.mem s (scReplyA sc) = r0 := (specPtr_inj (Option.some.inj he)).symm + have := J.sys.sysl.vr.screp sc (by rw [J.scs]; exact hc) hne + rwa [e0] at this + have all : ∀ i, ∀ hi : i < (stack sc).length, RBlk st s (stack sc)[i] := by + intro i + induction i with + | zero => + intro hi + obtain ⟨r0, l, e⟩ : ∃ r0 l, stack sc = r0 :: l := by + revert hi + generalize stack sc = rs + intro hi + cases rs with + | nil => exact absurd hi (Nat.not_lt_zero _) + | cons r0 l => exact ⟨r0, l, rfl⟩ + have h0 : (stack sc)[0] = r0 := by simp [e] + exact h0 ▸ head r0 l e + | succ i ih => + intro hi + have hi' : i < (stack sc).length := Nat.lt_of_succ_lt hi + have hp := (hw i hi').1 + have hq : (stack sc).getD (i + 1) 0#u64 = (stack sc)[i + 1] := by + rw [List.getD_eq_getElem?_getD, List.getElem?_eq_getElem hi]; rfl + rw [hq] at hp + have mR : (stack sc)[i + 1] ∈ Rp := hm _ (List.getElem_mem hi) + have rep := PE.rep _ mR + have mask : (stack sc)[i + 1] &&& ~~~1#u64 = (stack sc)[i + 1] := + mask1_of_even (by have := rep.1; omega) + have ne0 : (stack sc)[i + 1] ≠ 0#u64 := fun e => rep.2.1 (by rw [e]; rfl) + have h := J.sys.sysl.vr.rprev (stack sc)[i] (J.utIpc.reply _ (hm _ (List.getElem_mem hi'))) + (by rw [hp, mask]; exact ne0) + rwa [hp, mask] at h + obtain ⟨i, hi, e⟩ := List.getElem_of_mem hr + exact e ▸ all i hi + +/-- The SC half of `SysL.ci` at the current core's idle SC. -/ +theorem ciScA (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) : + ∀ u ∈ OS, ∀ p b, capA st s u = .SchedContext p b → specPtr p ≠ m.idleScOf m.curCore := by + intro u hu p b hc hp + obtain ⟨x, y, k, hx, hsc, -, -⟩ := J.sinv.idleSc + have hid : σ.idleThread = specPtr (L.idleW s) := J.sr.sch.idle + have htZ : L.idleW s ∈ Z.T := J.j.1.idleT _ J.sr.sch.cpuCs + obtain ⟨x', hx', hs⟩ := J.sr.sch.tcb _ htZ + rw [← hid, hx] at hx' + have ex := Fl4SpecA64.KernelObject.tcb.inj (Option.some.inj hx') + rw [← ex, hsc] at hs + have hw0 : L.mem s (scA (L.idleW s)) ≠ 0#u64 := fun h => by + rw [h] at hs; unfold optScA at hs; simp at hs + unfold optScA at hs + rw [if_neg hw0] at hs + have hC := J.sr.rest.scMem _ htZ hw0 + have hI : specPtr (L.mem s (scA (L.idleW s))) = m.idleScOf m.curCore := by + by_contra hne + refine J.sr.sch.scOff _ hC hne ?_ + have h2 := Option.some.inj hs + unfold scRenA at h2 + rw [if_neg hne] at h2 + exact h2.symm + have e : p = L.mem s (scA (L.idleW s)) := specPtr_inj (hp.trans hI.symm) + exact (J.sys.sysl.ci u hu).2.1 b (e ▸ hc) + +/-- `JStage3A` without `SysL`. -/ +theorem JStage3A.toTA (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (sact : SActA σ) (noSwitch : ∀ u, σ.schedulerAction ≠ .switchThread u) : + JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m where + ent := J.sys.ent + j := J.j + tcbs := J.tcbs + scs := J.scs + sq := J.sq + ap := J.ap + pp := J.pp + s1 := J.s1 + ko := J.ko + ptH := J.ptH + tabInj := J.tabInj + poolOff := J.poolOff + objAlloc := J.objAlloc + idleAnchor := J.idleAnchor + vsVc := J.vsVc + sr := J.sr + scCov := J.scCov + scSize := J.scSize + ep := J.ep + ntfn := J.ntfn + reply := J.reply + bufCover := J.bufCover + bufNative := J.bufNative + iplaced := J.iplaced + ipcPlace := J.ipcPlace + idleNoFault := J.idleNoFault + mcp := J.mcp + yieldRel := J.yieldRel + sinv := J.sinv + setsInH := J.setsInH + utIpc := J.utIpc + ipcTh := J.ipcTh + deadExt := J.deadExt + specFree := J.specFree + utBlk := J.utBlk + unmPt := J.unmPt + tabS1 := J.tabS1 + tabExt := J.tabExt + specZero := J.specZero + origLive := J.origLive + nullOrig := J.nullOrig + thPos := J.thPos + frBase := J.frBase + devFr := J.devFr + finCov := J.finCov + idleAll := J.idleAll + ipcHeapCov := J.ipcHeapCov + zombieNoVcpu := J.zombieNoVcpu + zombieTcb := J.zombieTcb + bufOff := J.bufOff + vsOk := J.vsOk + bufAl := J.bufAl + zombieBnd := J.zombieBnd + devUtOff := J.devUtOff + sidle := J.sidle + capTy := J.capTy + scCpu := J.scCpu + scNZ := J.scNZ + vcRun := J.vcRun + runVcpu := J.runVcpu + vr := stackBlkA J + blkQ := J.blkQ + ciSc := ciScA J + noSwitch := noSwitch + sact := sact + curVcpuNone := J.curVcpuNone + ipcSorted := J.ipcSorted + activeReplyScs := J.activeReplyScs + bufVs := J.bufVs + domOK := J.domOK + ipiZero := J.ipiZero + j5 := J.j5 + j7 := J.j7 + +/-- `JStage3TA` with `SysL` back. -/ +theorem JStage3TA.toJA (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (sysl : SysL st H Z.Cs OS ia ie s) (vcSym1 : VcSym1A st Z.T s) (vcSym2 : VcSym2A st Z.V s) + (heapWin : ∀ p k, H p = some k → + layout.PPTR_BASE.val ≤ p.val ∧ p.val + 2 ^ k.bits ≤ layout.PPTR_BASE.val + 2 ^ 44) + (coresAll : ∀ k < Fl4SpecA64.fl4A64Platform.numNodes, ∃ c ∈ Z.Cs, c.val = k) + (ptCapIn : PtCapInA Z (L.mem s)) (symL : SymL st H Z.Cs s) + (fhOK : ∀ t ∈ Z.T, NullOrEp H (capA st s (slotIdx t layout.tcb.SLOT_FAULT_HANDLER))) + (zwf : DelInvA st Z.th s) (schedValid : SchedValidA σ) + (utWin : UtWinCapA st OS s) (devWin : DevWinCapA st OS s) + (vcBack : ∀ v ∈ Z.V, L.mem s v ≠ 0#u64 → L.mem s (core.num.U64.wrapping_add (L.mem s v) 3664#u64) = v) + (runBack : ∀ t ∈ Z.T, L.mem s (runVcpuA t) ≠ 0#u64 → + L.mem s (core.num.U64.wrapping_add (L.mem s (runVcpuA t)) 24#u64) = t) + (vcSnap : VcpuStateSnapA Z.V (L.mem s) σ) (vcOS : ∀ b, VcW Z.V b → ¬ InOS OS b) + (userFr : UserFrSpecA st OS s σ) (frTh : FrThA st Z.th OS s) (bfSpec : ∀ bp ∈ Bf, bp.2 = specPtr bp.1) : + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m where + sys := ⟨J.ent, sysl⟩ + j := J.j + tcbs := J.tcbs + scs := J.scs + sq := J.sq + ap := J.ap + pp := J.pp + s1 := J.s1 + ko := J.ko + ptH := J.ptH + tabInj := J.tabInj + poolOff := J.poolOff + objAlloc := J.objAlloc + idleAnchor := J.idleAnchor + vsVc := J.vsVc + sr := J.sr + scCov := J.scCov + scSize := J.scSize + ep := J.ep + ntfn := J.ntfn + reply := J.reply + bufCover := J.bufCover + bufNative := J.bufNative + iplaced := J.iplaced + ipcPlace := J.ipcPlace + idleNoFault := J.idleNoFault + mcp := J.mcp + yieldRel := J.yieldRel + sinv := J.sinv + setsInH := J.setsInH + utIpc := J.utIpc + ipcTh := J.ipcTh + deadExt := J.deadExt + specFree := J.specFree + utBlk := J.utBlk + unmPt := J.unmPt + tabS1 := J.tabS1 + tabExt := J.tabExt + specZero := J.specZero + origLive := J.origLive + nullOrig := J.nullOrig + thPos := J.thPos + frBase := J.frBase + devFr := J.devFr + finCov := J.finCov + idleAll := J.idleAll + ipcHeapCov := J.ipcHeapCov + zombieNoVcpu := J.zombieNoVcpu + zombieTcb := J.zombieTcb + bufOff := J.bufOff + vsOk := J.vsOk + bufAl := J.bufAl + zombieBnd := J.zombieBnd + devUtOff := J.devUtOff + sidle := J.sidle + capTy := J.capTy + scCpu := J.scCpu + scNZ := J.scNZ + vcRun := J.vcRun + vcSym1 := vcSym1 + runVcpu := J.runVcpu + vcSym2 := vcSym2 + blkQ := J.blkQ + heapWin := heapWin + coresAll := coresAll + ptCapIn := ptCapIn + symL := symL + fhOK := fhOK + zwf := zwf + schedValid := schedValid + utWin := utWin + devWin := devWin + vcBack := vcBack + runBack := runBack + vcSnap := vcSnap + vcOS := vcOS + userFr := userFr + frTh := frTh + bfSpec := bfSpec + curVcpuNone := J.curVcpuNone + ipcSorted := J.ipcSorted + activeReplyScs := J.activeReplyScs + bufVs := J.bufVs + domOK := J.domOK + ipiZero := J.ipiZero + j5 := J.j5 + j7 := J.j7 + +/-- A stacked reply's `TCB` word: aligned, at the reply's address. -/ +theorem JStage3TA.rTcbValA (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {r : U64} (hr : r ∈ Rp) : (Fl4ProofsA64.P1.rTcbA r).val = r.val ∧ Aligned (Fl4ProofsA64.P1.rTcbA r) := by + have rep := J.iplaced.e.rep r hr + have e : (Fl4ProofsA64.P1.rTcbA r).val = r.val := by + unfold Fl4ProofsA64.P1.rTcbA layout.reply.TCB + simp [core.num.U64.wrapping_add_val_eq, U64.size_def, U64.numBits] + exact ⟨e, by unfold Aligned; rw [e]; omega⟩ + +/-- **`vr` across a step keeping the replies' `TCB` words and the TCBs' state words** (`ReplyInv.back`: a reply's +caller is a TCB). -/ +theorem JStage3TA.vr_keepA (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {s' : S} (hr : ∀ r ∈ Rp, L.mem s' (Fl4ProofsA64.P1.rTcbA r) = L.mem s (Fl4ProofsA64.P1.rTcbA r)) + (hs : ∀ t ∈ Z.T, L.mem s' (stA t) = L.mem s (stA t)) : ∀ sc ∈ Z.C, ∀ r ∈ stack sc, RBlk st s' r := by + intro sc hc r h + have mR : r ∈ Rp := (J.reply.stackRel sc hc).2.1 r h + have old := J.vr sc hc r h + have mt : L.mem s (Fl4ProofsA64.P1.rTcbA r) ∈ Z.T := by + rw [← J.tcbs] + exact (J.ent.bxi.2.2.1.2.1.back r (J.utIpc.reply r mR) old.tcb).2.1 + exact ⟨by rw [hr r mR]; exact old.tcb, by rw [hr r mR, hs _ mt]; exact old.blk⟩ + +theorem JStage3TA.vsLeafA (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) : + Aligned (L.ksb s) ∧ (∀ p ∈ Z.S1, ∀ i : U64, i.val < 512 → ¬ AsW (L.ksb s) (peAF p i)) ∧ + (∀ p ∈ Z.Pp, ∀ i : U64, i.val < 512 → ¬ AsW (L.ksb s) (peAF p i)) := J.toJEntTA.vsLeafA + +theorem JStage3TA.scAl (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) : + ∀ c ∈ Z.C, Aligned c := J.toJEntTA.scAl + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3TcbBindA.lean b/proofs/Fl4ProofsA64/P3/Stage3TcbBindA.lean new file mode 100644 index 000000000..7cb5a5b88 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3TcbBindA.lean @@ -0,0 +1,711 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 3): **TCB BindNotification at the record**. + +* **`Endp.EpStateRel.bindBound`**: the endpoint relation across the two backlink writes (the binding twin of + `unbindBound`). +* **`JStage3A.bindNtfn_stepA`**: `bind_notification` of an unbound TCB and an unbound notification keeps the record. +* **`JSyscallA.bindNtfnA`**: `decode_invocation` on a Thread cap with the BindNotification label at a syscall entry. + +Named premises: [P] `ArchTotal`, `MaxSpi`, `ArchPlat`, `A64FrameMasks`, `hCs`, `hmb`, `hmax`, `htu`; [S] `h1` +(`numNodes = 1`, the remote stall). The decode succeeds (`he`, `hfree`, `hc`, `hq`, `hnfree`); the `Err` returns are +not lifted. +New module. +-/ +import Fl4ProofsA64.P3.Stage3Lift3QA +import Fl4ProofsA64.P3.Stage3IpcGlueA +import Fl4ProofsA64.P3.Stage3SignalIdleA +import Fl4ProofsA64.P3.Stage3RestartA +import Fl4ProofsA64.P3.Stage3RsSiteA +import Fl4ProofsA64.P3.Stage3FinA +import Fl4ProofsA64.P3.Stage3PreemptTA +import Fl4ProofsA64.P3.Stage3TcbNtfnFrA +import Fl4ProofsA64.P3.Stage3TcbF0A +import Fl4ProofsA64.P3.IpcScGlueA +import Fl4ProofsA64.P3.DelNtfnBoundA +import Fl4ProofsA64.P3.DelScStallA +import Fl4ProofsA64.P3.Stage3TcbBodyA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched +open Fl4SpecA64 + +/-- **`EpStateRel` across the binding** (module docstring). -/ +theorem EpStateRel.bindBound {N T E R : Set U64} {Bf : Set (U64 × Word)} {m m' : U64 → U64} + {σ : Fl4SpecA64.State Unit} (hR : EpStateRel T E R Bf m σ) (hP : IPlacedA N T E R Bf) {n t : U64} + (htT : t ∈ T) {x : Notification} {y : Tcb} (hn0 : n ≠ 0#u64) + (hk : σ.kheap (specPtr n) = some (.notification x)) (ht : σ.kheap (specPtr t) = some (.tcb y)) + (hm : ∀ a, Aligned a → EFoot T E R Bf a → a ≠ tBoundA t → m' a = m a) (h0 : m' (tBoundA t) = n) : + EpStateRel T E R Bf m' (updKSc (updKSc σ (specPtr n) (.notification { x with ntfnBoundTcb := some (specPtr t) })) + (specPtr t) (.tcb { y with tcbBoundNotification := some (specPtr n) })) := by + have hta := hP.e.tcb t htT + have kh : ∀ p, p ≠ specPtr n → p ≠ specPtr t → + (updKSc (updKSc σ (specPtr n) (.notification { x with ntfnBoundTcb := some (specPtr t) })) + (specPtr t) (.tcb { y with tcbBoundNotification := some (specPtr n) })).kheap p = σ.kheap p := fun p h1 h2 => by + rw [updKSc_kheap, Function.update_of_ne h2, updKSc_kheap, Function.update_of_ne h1] + have off : ∀ a : U64, (a.val < t.val ∨ t.val + 4096 ≤ a.val) → a ≠ tBoundA t := fun a h e => by + have := val_bound hta + rw [← e] at this + omega + have inT : ∀ u ∈ T, ∀ a : U64, u.val ≤ a.val → a.val < u.val + 4096 → a.val ≠ u.val + 3368 → a ≠ tBoundA t := by + intro u hu a h1 h2 h3 + by_cases e : u = t + · subst e + intro h + rw [h, val_bound hta] at h3 + exact h3 rfl + · have := tcb_disj hP.e htT hu e + exact off a (by omega) + have hl : ∀ u ∈ T, ∀ a, Aligned a → TL u a → m' a = m a := fun u hu a ha h => + hm a ha (Or.inr (Or.inr (Or.inl ⟨u, hu, Or.inr h⟩))) + (inT u hu a (by unfold TL at h; omega) (by unfold TL at h; omega) (by unfold TL at h; omega)) + have hf : ∀ u ∈ T, ∀ a, Aligned a → TF u a → a.val ≠ u.val + 3368 → m' a = m a := fun u hu a ha h hne => + hm a ha (Or.inr (Or.inr (Or.inl ⟨u, hu, Or.inl h⟩))) (inT u hu a h.1 (by have := TF.range h; omega) hne) + have tcbR : ∀ u ∈ T, ∀ w z : Tcb, TcbRelE R m u w → z.tcbState = w.tcbState → z.tcbArch = w.tcbArch → + z.tcbFault = w.tcbFault → z.tcbPriority = w.tcbPriority → + z.tcbBoundNotification = optRef (m' (tBoundA u)) → TcbRelE R m' u z := by + intro u hu w z hx es ea ef ep eb + have hua := hP.e.tcb u hu + have wd : ∀ a, Aligned a ∧ TF u a → a.val ≠ u.val + 3368 → m' a = m a := fun a h hne => hf u hu a h.1 h.2 hne + have e_st : m' (stA u) = m (stA u) := wd _ (a_st hua) (by rw [val_st hua]; omega) + have e_blk : m' (tBlockA u) = m (tBlockA u) := wd _ (a_blk hua) (by rw [val_blk hua]; omega) + have e_badge : m' (tBadgeA u) = m (tBadgeA u) := wd _ (a_badge hua) (by rw [val_badge hua]; omega) + have e_flags : m' (tFlagsA u) = m (tFlagsA u) := wd _ (a_flags hua) (by rw [val_flags hua]; omega) + have e_reply : m' (tReplyA u) = m (tReplyA u) := wd _ (a_reply hua) (by rw [val_reply hua]; omega) + have e_fault : m' (tFaultA u) = m (tFaultA u) := wd _ (a_fault hua) (by rw [val_fault hua]; omega) + have e_prio : m' (prioA u) = m (prioA u) := wd _ (a_prio hua) (by rw [val_prio hua]; omega) + refine ⟨by rw [e_st, es]; exact hx.st, fun e h => by rw [e_blk]; exact hx.blk e (es ▸ h), + fun e d h => by rw [e_badge, e_flags]; exact hx.send e d (es ▸ h), + fun e r d h => by rw [e_reply]; exact hx.recv e r d (es ▸ h), + fun r h => by rw [e_reply]; exact hx.reply r (es ▸ h), fun r => ?_, + by rw [e_prio, ep]; exact hx.prio, by rw [e_fault, ef]; exact hx.fault0, + by rw [e_fault, ef]; exact hx.faultT, eb, ?_⟩ + · rw [wd _ (a_reg hua r) (by rw [regA_val hua (regIdxA_lt r)]; have := regIdxA_lt r; omega), ea] + exact hx.regs r + · rw [ef] + exact hx.faultR.congr e_fault (wd _ (a_fw0 hua) (by rw [val_fw0 hua]; omega)) + (wd _ (a_fw1 hua) (by rw [val_fw1 hua]; omega)) (wd _ (a_lf hua) (by rw [val_lf hua]; omega)) + (wd _ (a_lf0 hua) (by rw [val_lf0 hua]; omega)) (wd _ (a_lf1 hua) (by rw [val_lf1 hua]; omega)) + (wd _ (a_fw2 hua) (by rw [val_fw2 hua]; omega)) + refine ⟨fun e he => ?_, fun r hr => ?_, fun u hu => ?_, fun bp hb k hk' => ?_⟩ + · obtain ⟨z, hkz, hx⟩ := hR.ep e he + have hew : ∀ a : U64, (a.val = e.val ∨ a.val = e.val + 8) → a ≠ tBoundA t := fun a ha => by + have := hP.e.et e he t htT + exact off a (by omega) + refine ⟨z, (kh _ (fun h => by rw [h, hk] at hkz; cases hkz) (fun h => by rw [h, ht] at hkz; cases hkz)).trans hkz, + hx.frame (hm _ (a_head hP.e he).1 (a_head hP.e he).2 (hew _ (Or.inl (val_head e)))) + (hm _ (a_ts hP.e he).1 (a_ts hP.e he).2 (hew _ (Or.inr (val_ts hP.e he)))) fun u hu _ => ?_⟩ + have hua := hP.e.tcb u hu + exact ⟨hf u hu _ (a_st hua).1 (a_st hua).2 (by rw [val_st hua]; omega), + hf u hu _ (a_blk hua).1 (a_blk hua).2 (by rw [val_blk hua]; omega), + hl u hu _ (a_prev hua).1 (a_prev hua).2, hl u hu _ (a_next hua).1 (a_next hua).2⟩ + · obtain ⟨z, hkz, hx⟩ := hR.reply r hr + refine ⟨z, (kh _ (fun h => by rw [h, hk] at hkz; cases hkz) (fun h => by rw [h, ht] at hkz; cases hkz)).trans hkz, + ?_⟩ + have := hP.e.rt r hr t htT + rw [hx, hm _ (a_rtcb hP.e hr).1 (a_rtcb hP.e hr).2 (off _ (by rw [rTcbA_val]; omega))] + · obtain ⟨w, hkw, hx⟩ := hR.tcb u hu + by_cases e : u = t + · subst e + rw [ht] at hkw; cases hkw + refine ⟨{ y with tcbBoundNotification := some (specPtr n) }, by rw [updKSc_kheap, Function.update_self], + tcbR u hu y _ hx rfl rfl rfl rfl ?_⟩ + rw [h0, optRef_ne hn0] + · have e1 : specPtr u ≠ specPtr t := fun h => e (specPtr_inj h) + have e2 : specPtr u ≠ specPtr n := fun h => by rw [h, hk] at hkw; cases hkw + have hua := hP.e.tcb u hu + have eb : m' (tBoundA u) = m (tBoundA u) := by + refine hm _ (a_bound hua).1 (Or.inr (Or.inr (Or.inl ⟨u, hu, Or.inl (a_bound hua).2⟩))) ?_ + have := tcb_disj hP.e htT hu e + exact off _ (by rw [val_bound hua]; omega) + exact ⟨w, (kh _ e2 e1).trans hkw, tcbR u hu w w hx rfl rfl rfl rfl (by rw [eb]; exact hx.bound)⟩ + · have hw : BufW Bf (bufW bp.1 k) := ⟨bp, hb, k, hk', rfl⟩ + rw [hm _ (a_buf hP.e hb hk').1 (a_buf hP.e hb hk').2 (off _ (hP.e.bt _ hw t htT))] + exact hR.buf bp hb k hk' + +end Fl4ProofsA64.P3.Endp + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s : S} {a : A} {σ : State Unit} {m : McState} + +/-- `NtfnObjRel` reads of the notification only its head, state and badge words (not the bound TCB's). -/ +theorem ntfnObj_frameHTBA {m m' : U64 → U64} {T : Set U64} {n : U64} {o : Ntfn} (h : NtfnObjRel m T n o) + (hd : m' (epHeadA n) = m (epHeadA n)) (ts : m' (epTSA n) = m (epTSA n)) (bd : m' (nBadgeA n) = m (nBadgeA n)) + (hy : ∀ y ∈ T, Notif.MemberEq m m' y) : NtfnObjRel m' T n o := by + have es : stOf m' n = stOf m n := by unfold stOf; rw [ts] + have eq : qOf m' n = qOf m n := by unfold qOf; rw [hd, ts] + cases o with + | idleNtfn => + obtain ⟨h1, h2⟩ := h + exact ⟨es ▸ h1, eq ▸ queueRel_congr_links h2 (by simp)⟩ + | activeNtfn b => + obtain ⟨h1, h2, h3⟩ := h + exact ⟨es ▸ h1, eq ▸ queueRel_congr_links h2 (by simp), bd ▸ h3⟩ + | waitingNtfn q => + obtain ⟨h1, xs, hne, rfl, h2, h3⟩ := h + refine ⟨es ▸ h1, xs, hne, rfl, eq ▸ queueRel_congr_links h2 (fun y hyx => (hy y (h2.mem y hyx)).2.2), + fun y hyx => ?_⟩ + obtain ⟨e1, e2⟩ := h3 y hyx + exact ⟨(hy y (h2.mem y hyx)).1 ▸ e1, (hy y (h2.mem y hyx)).2.1 ▸ e2⟩ + +/-- The spec state after binding the notification at `p` to the thread at `t`. -/ +def bindStateA (σ : State Unit) (p t : ObjRef) (x : Notification) (y : Tcb) : State Unit := + updKSc (updKSc σ p (.notification { x with ntfnBoundTcb := some t })) t (.tcb { y with tcbBoundNotification := some p }) + +set_option maxHeartbeats 2000000 in +/-- **`bind_notification` at the record** (module docstring). -/ +theorem JStage3A.bindNtfn_stepA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (hc : m.curCore < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {t p : U64} (ht : t ∈ Z.T) (hpN : p ∈ N) (h0t : L.mem s (boundA t) = 0#u64) (h0n : L.mem s (ntfnBtA p) = 0#u64) + {x : Notification} {y : Tcb} (hk : σ.kheap (specPtr p) = some (.notification x)) + (hy : σ.kheap (specPtr t) = some (.tcb y)) : + BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac + (L.wr (L.wr s (ntfnBtA p) t) (boundA t) p) ∧ + (SysL st H Z.Cs OS ia ie (L.wr (L.wr s (ntfnBtA p) t) (boundA t) p) → + SymL st H Z.Cs (L.wr (L.wr s (ntfnBtA p) t) (boundA t) p) → + Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U (L.wr (L.wr s (ntfnBtA p) t) (boundA t) p) → + J7A st (J7R2A st ar nA) Z H OS (L.wr (L.wr s (ntfnBtA p) t) (boundA t) p) + (bindStateA σ (specPtr p) (specPtr t) x y) m → + ∃ Q Rl, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) (L.wr (L.wr s (ntfnBtA p) t) (boundA t) p) a + (bindStateA σ (specPtr p) (specPtr t) x y) m) := by + set sa := L.wr s (ntfnBtA p) t with hsa + set s' := L.wr sa (boundA t) p with hs' + set σ' := bindStateA σ (specPtr p) (specPtr t) x y with hσ' + have hP := J.iplaced + have htT : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + have hta : TcbAddr t := J.sr.core.tcbs t ht + have hpH : p ∈ H.ntfns := J.utIpc.ntfn p hpN + have t0 : t ≠ 0#u64 := J.sr.place.nz t _ ((J.sr.thT t).2 ht) + have p0 : p ≠ 0#u64 := J.sr.place.nz p _ (J.ipcPlace.notification p hpN) + have hna := hP.n.ntfn p hpN + have eB : ntfnBtA p = Notif.nBoundA p := rfl + have aN : Aligned (ntfnBtA p) := by + rw [eB, Notif.nBoundA_eq]; exact Notif.nfield_aligned hna (by simp) (by simp) + have vN : (ntfnBtA p).val = p.val + 24 := by + rw [eB, Notif.nBoundA_eq, Notif.nfield_val hna (by simp)]; simp + have aB : Aligned (boundA t) := field_aligned hta (K := 3368#u64) (by simp) (by simp) + have vB : (boundA t).val = t.val + 3368 := by + show (wa t 3368#u64).val = _ + rw [Notif.tfield_val hta (by decide : (3368#u64 : U64).val < 4096)]; rfl + have disT : ∀ u ∈ Z.T, p.val + 64 ≤ u.val ∨ u.val + 4096 ≤ p.val := fun u hu => (hP.n.disj p hpN u hu).symm + have blk' : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s' := by + have h := Fl4ProofsA64.P1.notification.bind_notification.blk (st := st) s t p (BXI.blk J.sys.ent.bxi) htT hpH + h0t h0n + rw [tcbBindNtfnWritesA, WP.spec_ok] at h + exact h + refine ⟨blk', fun sysl symL userOK j7 => ?_⟩ + have w1 : WritesIn st s sa (fun b => b = ntfnBtA p) := writesIn_wr aN + have w2 : WritesIn st sa s' (fun b => b = boundA t) := writesIn_wr aB + have w12 : WritesIn st s s' (fun b => b = ntfnBtA p ∨ b = boundA t) := + (w1.trans w2).mono fun _ h => h + have PF : PartFrA (st := st) N E Rp Z m s s' σ σ' := + tcbNtfn_partA hP.e.tcb ht hpN hna.fits w12 hk hy ⟨rfl, rfl, rfl⟩ + have F : SchedFrameA σ σ' := PF.fr + have off : ∀ b : U64, Aligned b → b.val ≠ p.val + 24 → b.val ≠ t.val + 3368 → L.mem s' b = L.mem s b := + fun b hb n1 n2 => w12.mem_eq hb fun h => by + rcases h with h | h + · exact n1 (by rw [h]; exact vN) + · exact n2 (by rw [h]; exact vB) + have tw : ∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → K.val ≠ 3368 → L.mem s' (wa u K) = L.mem s (wa u K) := + fun u hu K k1 k2 k3 => by + have hua : TcbAddr u := J.sr.core.tcbs u hu + have hv := Notif.tfield_val hua k1 + have a1 : t.val % 4096 = 0 := hta.align + have a2 : u.val % 4096 = 0 := hua.align + have := disT u hu + exact off _ (field_aligned hua k1 k2) (by rw [hv]; omega) (by rw [hv]; omega) + have hbn : L.mem s' (boundA t) = p := by rw [hs', L.mem_wr_same aB] + have hbp : L.mem s' (ntfnBtA p) = t := by + have : L.mem s' (ntfnBtA p) = L.mem sa (ntfnBtA p) := by + refine w2.mem_eq aN fun h => ?_ + have := congrArg UScalar.val h + rw [vN, vB] at this + have := disT t ht + omega + rw [this, hsa, L.mem_wr_same aN] + have ne : specPtr t ≠ specPtr p := fun e => by rw [e, hk] at hy; cases hy + -- the notification half of the spec step + set σa := updKSc σ (specPtr p) (.notification { x with ntfnBoundTcb := some (specPtr t) }) with hσa + have ka : ∀ q, σa.kheap q = + if q = specPtr p then some (.notification { x with ntfnBoundTcb := some (specPtr t) }) else σ.kheap q := by + intro q + simp only [hσa, updKSc_kheap, Function.update_apply] + have hya : σa.kheap (specPtr t) = some (.tcb y) := by rw [ka, if_neg ne]; exact hy + have key : ∀ q, σ'.kheap q = + if q = specPtr t then some (.tcb { y with tcbBoundNotification := some (specPtr p) }) + else if q = specPtr p then some (.notification { x with ntfnBoundTcb := some (specPtr t) }) + else σ.kheap q := by + intro q + simp only [hσ', bindStateA, updKSc_kheap, Function.update_apply] + have keepO : ∀ q o, σ.kheap q = some o → (∀ z, o ≠ .tcb z) → (∀ z, o ≠ .notification z) → + σ'.kheap q = some o := fun q o h n1 n2 => by + rw [key, if_neg (fun e => by rw [e, hy] at h; exact n1 y (Option.some.inj h).symm), + if_neg (fun e => by rw [e, hk] at h; exact n2 x (Option.some.inj h).symm)] + exact h + have backO : ∀ q o, σ'.kheap q = some o → (∀ z, o ≠ .tcb z) → (∀ z, o ≠ .notification z) → + σ.kheap q = some o := fun q o h n1 n2 => by + rw [key] at h + split at h + · exact absurd (Option.some.inj h).symm (n1 _) + · split at h + · exact absurd (Option.some.inj h).symm (n2 _) + · exact h + have ntf : ∀ q z, σ.kheap q = some (.notification z) → ∃ w, σ'.kheap q = some (.notification w) ∧ + w.ntfnObj = z.ntfnObj ∧ w.ntfnSc = z.ntfnSc := by + intro q z h + have e1 : q ≠ specPtr t := fun e => by rw [e, hy] at h; cases h + by_cases e : q = specPtr p + · rw [e, hk] at h + cases h + exact ⟨{ x with ntfnBoundTcb := some (specPtr t) }, by rw [key, if_neg e1, if_pos e], rfl, rfl⟩ + · exact ⟨z, by rw [key, if_neg e1, if_neg e]; exact h, rfl, rfl⟩ + have ntfB : ∀ q w, σ'.kheap q = some (.notification w) → ∃ z, σ.kheap q = some (.notification z) ∧ + w.ntfnSc = z.ntfnSc := by + intro q w h + rw [key] at h + split at h + · cases h + · split at h + · rename_i e + cases h + exact ⟨x, e ▸ hk, rfl⟩ + · exact ⟨w, h, rfl⟩ + have fwdT : ∀ q z, σ.kheap q = some (.tcb z) → ∃ w, σ'.kheap q = some (.tcb w) ∧ + w = { z with tcbBoundNotification := w.tcbBoundNotification } := by + intro q z h + by_cases e : q = specPtr t + · rw [e, hy] at h + cases h + exact ⟨{ y with tcbBoundNotification := some (specPtr p) }, by rw [key, if_pos e], rfl⟩ + · have e' : q ≠ specPtr p := fun c => by rw [c, hk] at h; cases h + exact ⟨z, by rw [key, if_neg e, if_neg e']; exact h, rfl⟩ + have bwdT : ∀ q w, σ'.kheap q = some (.tcb w) → ∃ z, σ.kheap q = some (.tcb z) ∧ + w = { z with tcbBoundNotification := w.tcbBoundNotification } := by + intro q w h + rw [key] at h + split at h + · rename_i e + cases h + exact ⟨y, e ▸ hy, rfl⟩ + · split at h + · cases h + · exact ⟨w, h, rfl⟩ + -- the scheduler relation: the notification word, the TCB word, then the spec step + have SR : ∃ Q Rl, SchedSRA st { Z with Q := Q, R := Rl } m κ s' σ' := by + have ops := (ipcSchedOpsA (st := st) (ar := ar) (κ := κ) hP J.ipcPlace hc hmb hmax htu).1 + have wW : WritesIn st s sa (NWrite N Z.T) := w1.mono fun b hb => Or.inl ⟨p, hpN, by rw [hb, vN]; omega, + by rw [hb, vN]; omega⟩ + obtain ⟨Q, Rl, sr⟩ := ops.stable s sa σ σ ⟨Z.Q, Z.R, J.sr⟩ wW ⟨rfl, rfl, fun _ => Or.inl rfl⟩ + (fun q y z hy hz => by + rw [hy] at hz + cases hz + exact Or.inl ⟨rfl, id⟩) + have f0 : TcbF0WA Z.T (boundA t) := ⟨t, ht, by omega, by omega, fun k hk => by omega⟩ + have sr2 : SchedSRA st { Z with Q := Q, R := Rl } m κ s' σ := + Sched.SchedSRA.f0A sr w2 fun b hb => hb ▸ f0 + refine ⟨Q, Rl, sr2.stable (WritesIn.refl _ _) (W := fun _ => False) (fun _ h => h.elim) ?_ ?_⟩ + · exact + { fix := ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ + act := rfl + rq := rfl + rel := rfl + sc := fun q z k h => (keepO q _ h (fun _ e => by cases e) (fun _ e => by cases e)).trans h.symm + tcb := fun q z h => by + obtain ⟨w, hw, e⟩ := fwdT q z h + exact ⟨w, hw, by rw [e], by rw [e], by rw [e]⟩ + scBack := fun q z k h => (backO q _ h (fun _ e => by cases e) (fun _ e => by cases e)).trans h.symm + tcbBack := fun q w h => by + obtain ⟨z, hz, -⟩ := bwdT q w h + exact ⟨z, hz⟩ + ntfnBack := ntfB } + · intro q z w hz hw + obtain ⟨w', hw', e⟩ := fwdT q z hz + rw [hw] at hw' + cases hw' + refine Or.inl ⟨by rw [e], fun h => ?_⟩ + rw [e] at h + exact h + obtain ⟨Q, Rl, sr⟩ := SR + have ep : EpStateRel Z.T E Rp Bf (L.mem s') σ' := by + refine J.ep.bindBound hP (n := p) ht p0 hk hy (fun b hb hf nb => off b hb (fun h => ?_) (fun h => ?_)) hbn + · have inP : p.val ≤ b.val ∧ b.val < p.val + 64 := by omega + rcases hf with ⟨e, he, h1⟩ | ⟨r, hr, h1⟩ | ⟨u, hu, h1⟩ | h1 + · have := hP.ne p hpN e he; omega + · have := hP.nr p hpN r hr; omega + · have := disT u hu + have hfit := (J.sr.core.tcbs u hu).fits + have : u.val ≤ b.val ∧ b.val < u.val + 4096 := by + rcases h1 with h1 | h1 + · unfold Endp.TF TOffN at h1 + omega + · unfold TL at h1 + omega + omega + · have := hP.nb p hpN b h1; omega + · exact nb (UScalar.eq_of_val_eq (h.trans vB.symm)) + have ntfn : NtfnStateRel N Z.T (L.mem s') σ' := by + refine ⟨fun q hq => ?_, fun u hu => ?_⟩ + · obtain ⟨z, hz, hr⟩ := J.ntfn.ntfn q hq + have hqa := hP.n.ntfn q hq + have eqp : specPtr q = specPtr p → q = p := fun e => specPtr_inj e + have e1 : specPtr q ≠ specPtr t := fun e => by rw [e, hy] at hz; cases hz + have mem : ∀ u ∈ Z.T, Notif.MemberEq (L.mem s) (L.mem s') u := fun u hu => by + have hua := J.sr.core.tcbs u hu + refine ⟨?_, ?_, ?_, ?_⟩ + · rw [stA_eq]; exact tw u hu _ (by simp) (by simp) (by simp) + · rw [Notif.tBlockA_eq]; exact tw u hu _ (by simp) (by simp) (by simp) + · rw [Notif.prevEp_eq]; exact tw u hu _ (by simp) (by simp) (by simp) + · rw [Notif.nextEp_eq]; exact tw u hu _ (by simp) (by simp) (by simp) + have word : ∀ b : U64, Aligned b → q.val ≤ b.val → b.val < q.val + 40 → (q = p → b.val ≠ p.val + 24) → + L.mem s' b = L.mem s b := fun b hb l h1 h2 => by + have hd := hP.n.disj q hq t ht + refine off b hb (fun e => ?_) (by omega) + by_cases eq : q = p + · exact h2 eq e + · have := J.sr.place.apart q p (.other 64) (.other 64) (J.ipcPlace.notification q hq) + (J.ipcPlace.notification p hpN) eq + change q.val + 64 ≤ p.val ∨ p.val + 64 ≤ q.val at this + omega + by_cases e : q = p + · subst e + rw [hk] at hz + cases hz + have ts : L.mem s' (epTSA q) = L.mem s (epTSA q) := by + refine word _ (foot_ts hP.n hq).1 ?_ ?_ (fun _ => ?_) <;> + rw [epTSA_eq, Notif.nfield_val hqa (by simp)] <;> simp + have hd : L.mem s' (epHeadA q) = L.mem s (epHeadA q) := by + refine word _ (foot_head hP.n hq).1 ?_ ?_ (fun _ => ?_) <;> + rw [Notif.epHeadA_eq, Notif.nfield_val hqa (by simp)] <;> simp + have bd : L.mem s' (nBadgeA q) = L.mem s (nBadgeA q) := by + refine word _ (foot_badge hP.n hq).1 ?_ ?_ (fun _ => ?_) <;> + rw [nBadgeA_eq, Notif.nfield_val hqa (by simp)] <;> simp + have sc : L.mem s' (nScA q) = L.mem s (nScA q) := by + refine word _ (foot_sc hP.n hq).1 ?_ ?_ (fun _ => ?_) <;> + rw [nScA_eq, Notif.nfield_val hqa (by simp)] <;> simp + have obj : NtfnObjRel (L.mem s') Z.T q x.ntfnObj := + ntfnObj_frameHTBA hr.obj hd ts bd fun u hu => mem u hu + refine ⟨{ x with ntfnBoundTcb := some (specPtr t) }, by rw [key, if_neg e1, if_pos rfl], + ⟨obj, ?_, ?_, ?_⟩⟩ + · show some (specPtr t) = optRef (L.mem s' (nBoundA q)) + rw [← eB, hbp, optRef_ne t0] + · intro _; rw [← eB, hbp]; exact ht + · rw [sc]; exact hr.sc + · have e2 : specPtr q ≠ specPtr p := fun c => e (eqp c) + have nw : NWordsEq (L.mem s) (L.mem s') q := by + refine ⟨?_, ?_, ?_, ?_, ?_⟩ + · refine word _ (foot_head hP.n hq).1 ?_ ?_ (fun c => absurd c e) <;> + rw [Notif.epHeadA_eq, Notif.nfield_val hqa (by simp)] <;> simp + · refine word _ (foot_ts hP.n hq).1 ?_ ?_ (fun c => absurd c e) <;> + rw [epTSA_eq, Notif.nfield_val hqa (by simp)] <;> simp + · refine word _ (foot_badge hP.n hq).1 ?_ ?_ (fun c => absurd c e) <;> + rw [nBadgeA_eq, Notif.nfield_val hqa (by simp)] <;> simp + · refine word _ (foot_bound hP.n hq).1 ?_ ?_ (fun c => absurd c e) <;> + rw [nBoundA_eq, Notif.nfield_val hqa (by simp)] <;> simp + · refine word _ (foot_sc hP.n hq).1 ?_ ?_ (fun c => absurd c e) <;> + rw [nScA_eq, Notif.nfield_val hqa (by simp)] <;> simp + exact ⟨z, by rw [key, if_neg e1, if_neg e2]; exact hz, hr.frame nw fun u hu _ => mem u hu⟩ + · obtain ⟨z, hz, hr⟩ := J.ntfn.tcb u hu + obtain ⟨w, hw, e⟩ := fwdT _ z hz + have hf : TFieldsEq (L.mem s) (L.mem s') u := by + refine ⟨?_, ?_, ?_, ?_⟩ + · rw [stA_eq]; exact tw u hu _ (by simp) (by simp) (by simp) + · rw [Notif.tBlockA_eq]; exact tw u hu _ (by simp) (by simp) (by simp) + · rw [Notif.badgeRegA_eq]; exact tw u hu _ (by simp) (by simp) (by simp) + · rw [Notif.prioA_eq']; exact tw u hu _ (by simp) (by simp) (by simp) + have hr' := hr.frame hf + refine ⟨w, hw, ?_⟩ + rw [e] + exact ⟨hr'.st, hr'.blk, hr'.badge, hr'.prio⟩ + have reply : ReplyStackRelA (L.mem s') Rp Z.C σ' (scNameA m) stack := by + refine J.reply.frame (fun r hr => ?_) + (fun c _ z k h => ⟨z, k, keepO _ _ h (fun _ e => by cases e) (fun _ e => by cases e), rfl⟩) + (fun r _ x' h => ⟨x', backO _ _ h (fun _ e => by cases e) (fun _ e => by cases e), rfl⟩) + have hr3 := hP.e.rep r hr + have dis := hP.e.rt r hr t ht + have dis2 := hP.nr p hpN r hr + have one : ∀ O : U64, O.val = 8 ∨ O.val = 16 → L.mem s' (wa r O) = L.mem s (wa r O) := by + intro O hO + have hv : (wa r O).val = r.val + O.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact off _ (by unfold Aligned; rw [hv]; omega) (by omega) (by omega) + exact ⟨one _ (Or.inr (by simp [layout.reply.NEXT])), one _ (Or.inl (by simp [layout.reply.PREV]))⟩ + have K : IpcKeepA σ σ' := by + refine ⟨fun q z h => ?_, + fun q z k h => ⟨z, k, keepO q _ h (fun _ e => by cases e) (fun _ e => by cases e), rfl⟩, + fun q z k h => ⟨z, k, backO q _ h (fun _ e => by cases e) (fun _ e => by cases e), rfl⟩⟩ + obtain ⟨w, hw, e⟩ := fwdT q z h + exact ⟨w, hw, by rw [e], by rw [e], by rw [e], by rw [e], by rw [e], by rw [e]⟩ + have sinv : SInvXA σ' m n := by + have same' : SchedSameA σ σa := + { kh := fun q ko hko => by + have ne' : ∀ z, ko ≠ .notification z := fun z e => by + rcases hko with ⟨_, e'⟩ | ⟨_, _, e'⟩ <;> rw [e] at e' <;> cases e' + rw [ka] + split + · rename_i e + constructor + · exact fun h => absurd (Option.some.inj h).symm (ne' _) + · intro h + rw [e, hk] at h + exact absurd (Option.some.inj h).symm (ne' _) + · exact Iff.rfl + cur := rfl + idle := rfl + csc := rfl + rq := rfl + rls := rfl + time := rfl + cons := rfl } + have I := J.sinv.irqN + have capK : ∀ q ob b a', σa.kheap q = some ob → capOf ob b = some a' → σ.kheap q = some ob := + fun q ob b a' h hc' => by + rw [ka] at h + split at h + · cases h; cases hc' + · exact h + have Ia : SpecIrqA σa := by + refine ⟨?_, fun irq hi ob b a' h hc' => ?_, fun q ob b a' h hc' => ?_⟩ + · have ct : ∀ k q, capTableAt k q σa ↔ capTableAt k q σ := fun k q => by + unfold capTableAt + rw [ka] + split + · rename_i e + rw [e, hk] + constructor <;> rintro ⟨cs, h, -⟩ <;> simp at h + · exact Iff.rfl + obtain ⟨⟨inj, tab⟩, dom⟩ := I.node + exact ⟨⟨inj, fun irq h => (ct _ _).2 (tab irq h)⟩, fun irq h k c => dom irq h k ((ct _ _).1 c)⟩ + · exact I.slot irq hi ob b a' (capK _ _ _ _ h hc') hc' + · exact I.ref q ob b a' (capK _ _ _ _ h hc') hc' + have Sa : SInvXA σa m n := J.sinv.of_spec same' rfl Ia + have eσ : σ' = setObjA σa (specPtr t) (.tcb { y with tcbBoundNotification := some (specPtr p) }) := rfl + have kb : ∀ q, σ'.kheap q = if q = specPtr t then some (.tcb { y with tcbBoundNotification := some (specPtr p) }) + else σa.kheap q := fun q => by rw [key, ka] + refine Sa.of_fr + { tcb := fun q z h => by + rw [kb] + split + · rename_i e + rw [e, hya] at h + cases h + exact ⟨_, rfl, rfl, rfl, rfl⟩ + · exact ⟨z, h, rfl, rfl, rfl⟩ + tcb' := fun q w h => by + rw [kb] at h + split at h + · rename_i e + cases h + exact ⟨y, e ▸ hya, rfl, rfl, rfl⟩ + · exact ⟨w, h, rfl, rfl, rfl⟩ + sc := fun q z k => by + rw [kb] + split + · rename_i e + rw [e, hya] + constructor <;> intro h <;> cases h + · exact Iff.rfl + cur := rfl + idle := rfl + csc := rfl + rq := rfl + rls := rfl + act := rfl + time := rfl + cons := rfl } ?_ + rw [eσ] + exact Ia.updTcbA hya rfl + have sidle : SIdleA σ' m n := + { f3 := fun c hcc => by + obtain ⟨z, hz, hs⟩ := J.sidle.f3 c hcc + obtain ⟨w, hw, e⟩ := fwdT _ z hz + exact ⟨w, hw, by rw [e]; exact hs⟩ + f4 := fun z k h => J.sidle.f4 z k (backO _ _ h (fun _ e => by cases e) (fun _ e => by cases e)) + f5 := fun q z k h => J.sidle.f5 q z k (backO _ _ h (fun _ e => by cases e) (fun _ e => by cases e)) + f6 := J.sidle.f6.of_eq fun sl => by + unfold specCapAtA + by_cases e : sl.1 = specPtr t + · rw [key, if_pos e, e, hy] + rfl + · by_cases e2 : sl.1 = specPtr p + · rw [key, if_neg e, if_pos e2, e2, hk] + · rw [key, if_neg e, if_neg e2] + f7 := fun y k h => J.sidle.f7 y k (backO _ _ h (fun _ e => by cases e) (fun _ e => by cases e)) + idleSlot := fun c hc => by + have h0 := J.sidle.idleSlot c hc + unfold vtCapA at h0 ⊢ + by_cases e : m.idleThreadOf c = specPtr t + · rw [key, if_pos e]; rw [e, hy] at h0; exact h0 + · by_cases e2 : m.idleThreadOf c = specPtr p + · rw [key, if_neg e, if_pos e2] <;> (rw [e2, hk] at h0; exact h0) + · rw [key, if_neg e, if_neg e2]; exact h0 + idleBound := fun c hc => by + have h0 := J.sidle.idleBound c hc + unfold vcpuOfA at h0 ⊢ + by_cases e : m.idleThreadOf c = specPtr t + · rw [key, if_pos e]; rw [e, hy] at h0; exact h0 + · by_cases e2 : m.idleThreadOf c = specPtr p + · rw [key, if_neg e, if_pos e2] <;> (rw [e2, hk] at h0; exact h0) + · rw [key, if_neg e, if_neg e2]; exact h0 } + have rv : RunVcpuInv st Z.T s' := fun u hu h0 => by + have e1 : L.mem s' (runVcpuA u) = L.mem s (runVcpuA u) := by + unfold runVcpuA + exact tw u hu 3672#u64 (by simp) (by simp) (by simp) + have e2 : L.mem s' (Fl4ProofsA64.P1.stA u) = L.mem s (Fl4ProofsA64.P1.stA u) := by + rw [Fl4ProofsA64.P1.stA_eq] + exact tw u hu 3328#u64 (by simp) (by simp) (by simp) + rw [e2]; exact J.runVcpu u hu (e1 ▸ h0) + have blkQ : BlkQueuedA Z.T E N (L.mem s') σ' := by + have tb : ∀ u ∈ Z.T, L.mem s' (tBlockA u) = L.mem s (tBlockA u) := fun u hu => by + rw [Notif.tBlockA_eq]; exact tw u hu 3336#u64 (by simp) (by simp) (by simp) + refine ⟨fun u hu w h e hb => ?_, fun u hu w h q hb => ?_⟩ + · obtain ⟨z, hz, ez⟩ := bwdT _ w h + rw [ez] at hb + obtain ⟨c, o, ho, hm⟩ := J.blkQ.ep u hu z hz e hb + exact ⟨by rw [tb u hu]; exact c, o, keepO _ _ ho (fun _ e => by cases e) (fun _ e => by cases e), hm⟩ + · obtain ⟨z, hz, ez⟩ := bwdT _ w h + rw [ez] at hb + obtain ⟨c, o, l, ho, hl, hm⟩ := J.blkQ.ntfn u hu z hz q hb + obtain ⟨o', ho', e', -⟩ := ntf q o ho + exact ⟨by rw [tb u hu]; exact c, o', l, ho', e'.trans hl, hm⟩ + have sv : SchedValidA σ' := J.schedValid.congr rfl rfl rfl rfl rfl rfl + (fun q z k h => keepO q _ h (fun _ e => by cases e) (fun _ e => by cases e)) + (fun u z h => by + obtain ⟨w, hw, e⟩ := fwdT u z h + exact ⟨w, hw, by rw [e]⟩) + (fun u w h => by + obtain ⟨z, hz, e⟩ := bwdT u w h + exact ⟨z, hz, by rw [e], by rw [e]⟩) + refine ⟨Q, Rl, ?_⟩ + have Qk : QKeepA σ σ' := QKeepA.ofBwd fun q o h => by + rw [key] at h + split at h + · rename_i e + cases h + exact ⟨.tcb y, by rw [e]; exact hy, QObjA.ofTcb rfl⟩ + · split at h + · rename_i e + cases h + exact ⟨.notification x, by rw [e]; exact hk, QObjA.ofNtfn (List.Sublist.refl _)⟩ + · exact ⟨o, h, QObjA.refl o⟩ + exact J.of_ipcStepA (Q := Q) (Rl := Rl) (curOf := Z.curOf) (curScOf := Z.curScOf) PF.wr F.1 F.2.1 Qk F.2.2 + (fun _ _ => rfl) (BlkOK.bxi blk') sysl symL userOK j7 sr ep ntfn reply K + (fun u hu O h h8 => tw u hu O (by omega) h8 (by omega)) + sinv sidle rv blkQ sv + +set_option maxHeartbeats 2000000 in +/-- **TCB BindNotification at a syscall entry** (module docstring). -/ +theorem JSyscallA.bindNtfnA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {slot lbl len buf t p badge ns : U64} {blk call don send : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Thread t) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hlbl : lbl = fl4_abi.label.TCBBindNotification) + (he : tcb.extra ⟨len, buf, call, slot, extra⟩ 0#usize = ok (some ns)) + (hns : ns ∈ OS) (hncap : capA st s ns = .Notification p badge send true) + (hfree : L.mem s (boundA t) = 0#u64) (hq : obj.Ntfn.head st p s = ok 0#u64) + (hnfree : obj.Ntfn.bound_tcb st p s = ok 0#u64) : + ∃ xc σ1 σ2 s2, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + (.ok [], σ2) ∈ ((fl4InvokeTcb (.notificationControl (specPtr t) (some (specPtr p)))).run σ1).1 ∧ + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (.Ok (), s2, a) ∧ + ∃ Q Rl, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s2 a σ2 m ∧ Sched.BodyEndA σ σ2 m := by + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hslot hcov J.actW J.running J.cur + have hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM := J.toJStage3EntryA.curNotVmA + obtain ⟨xc, hkc, hst, hsp, J1, -, -, -⟩ := J.toJStage3A.of_restartCurA RS.pl.choose_spec.choose_spec.choose_spec.choose_spec + RS.sym RS.run hnvm RS.cur RS.vs RS.vc + have ht : t ∈ Z.T := J.tcbs ▸ (J.toJStage3A.slotCapTy hslot).1.2.2.2.2.2 t hcap + have hpN : p ∈ N := by + have hH := (J.toJStage3A.slotCapTy hns).1.1 + rw [hncap] at hH + exact J.ipcHeapCov.ntfn p (hH.2.1 p badge send true rfl).2 + have T0 := J.toJStage3A.toTA J.sact J.noSwitch + have hstall : smp.remote_tcb_stall st ar s a t = ok (s, a) := T0.stall_singleA hCs h1 ht + have hc := T0.curCore_lt hCs + set s1 := L.wr s (stA (L.curW s)) sched.TS_RESTART with hs1 + set σ1 := stUpdA σ σ.curThread xc .restart with hσ1 + have hct : TcbAddr (L.curW s) := J.sr.core.tcbs _ J.sr.curT + have hta : TcbAddr t := J.sr.core.tcbs t ht + have aS : Aligned (stA (L.curW s)) := by rw [stA_eq]; exact field_aligned hct (by simp) (by simp) + have vS : (stA (L.curW s)).val = (L.curW s).val + 3328 := by + rw [stA_eq, Notif.tfield_val hct (by decide : (3328#u64 : U64).val < 4096)]; rfl + have hbound : L.mem s1 (boundA t) = 0#u64 := by + rw [← hfree] + refine mem_wr_ne' aS (field_aligned hta (K := 3368#u64) (by simp) (by simp)) fun e => ?_ + have ev := congrArg UScalar.val e + have v2 : (boundA t).val = t.val + 3368 := by + show (wa t 3368#u64).val = _ + rw [Notif.tfield_val hta (by decide : (3368#u64 : U64).val < 4096)]; rfl + rw [vS, v2] at ev + have a1 : t.val % 4096 = 0 := hta.align + have a2 : (L.curW s).val % 4096 = 0 := hct.align + omega + have hna := J.iplaced.n.ntfn p hpN + have hnb : L.mem s1 (ntfnBtA p) = 0#u64 := by + have h0 : L.mem s (ntfnBtA p) = 0#u64 := by + have h := eq_of_spec_fd (Notif.obj.Ntfn.bound_tcb.spec (st := st) s p) + rw [hnfree] at h + exact (ok_inj_ff h).symm + rw [← h0] + have eB : ntfnBtA p = Notif.nBoundA p := rfl + have vN : (ntfnBtA p).val = p.val + 24 := by + rw [eB, Notif.nBoundA_eq, Notif.nfield_val hna (by simp)]; simp + refine mem_wr_ne' aS (by rw [eB, Notif.nBoundA_eq]; exact Notif.nfield_aligned hna (by simp) (by simp)) + fun e => ?_ + have ev := congrArg UScalar.val e + rw [vS, vN] at ev + have := J.iplaced.n.disj p hpN (L.curW s) J.sr.curT + omega + have hr : tcb.restart_caller st s = ok s1 := by + unfold tcb.restart_caller + rw [cur_thread_ok, bind_ok, hst] + obtain ⟨y, hy, -⟩ := J1.mcp t ht + obtain ⟨x, hk, -⟩ := J1.ntfn.ntfn p hpN + have hcap' : obj.Slot.cap st ns s = ok (.Notification p badge send true) := by rw [capA_eq, hncap] + have perf := tcbBindNotification_performA (st := st) (ar := ar) (T := Z.T) (N := N) + (ctx := ⟨len, buf, call, slot, extra⟩) (σRestart := σ1) hstall he hfree hcap' hq hnfree hr J1.iplaced.n ht hpN hk hy + obtain ⟨⟨r, s3, a3⟩, ed, er, ea, es, -, hmem⟩ := exists_of_specA perf + simp only at er ea es hmem + subst er ea es + have hdec : syscall.decode_invocation st ar s a3 lbl len slot (capA st s slot) blk call don false buf extra = + ok (.Ok (), L.wr (L.wr s1 (ntfnBtA p) t) (boundA t) p, a3) := by + rw [hcap, decodeTcb_programA, if_neg Bool.false_ne_true, hlbl] + exact ed + obtain ⟨-, K⟩ := J1.bindNtfn_stepA (st := st) (ar := ar) hc hmb hmax htu ht hpN hbound hnb hk hy + obtain ⟨-, -, sysl, symL, -, userOK, j7⟩ := J.decodeFinA hA hM hPl hfm hslot hcov + (rfl : capA st s slot = capA st s slot) hext hSC (lbl := lbl) + (by + rw [hcap, hlbl] + exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨t, rfl, Or.inr (Or.inr (Or.inl rfl))⟩))))))) + (fun _ h => by rw [hcap] at h; cases h) (fun _ h => by rw [hcap] at h; cases h) hdec + (bindStateA σ1 (specPtr p) (specPtr t) x y) m + obtain ⟨Q, Rl, J2⟩ := K sysl symL userOK j7 + exact ⟨xc, σ1, _, _, hkc, rfl, hsp, hmem, hdec, Q, Rl, J2, Sched.BodyEndA.ofNtfnTcb J hkc hk hy rfl⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3TcbBodyA.lean b/proofs/Fl4ProofsA64/P3/Stage3TcbBodyA.lean new file mode 100644 index 000000000..221c15e16 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3TcbBodyA.lean @@ -0,0 +1,357 @@ +/- +P3 (aarch64, A64-P3 Lift 3b): **shape lemmas for `BodyOKA` after a TCB arm** (`P3/Stage3BodyOKA`). + +Every lift-3 TCB / SchedContext arm ends with the Plan B `DispPostA`, which needs `Sched.BodyOKA σ' m` at the arm's +spec state. The arms replace one or two heap objects of the post-`Restart` state; the lemmas here give `BodyKeepA` +for the object replacements that keep every TCB state and every SC (`ofTcbUpd`, `ofKh`), and `BodyOKA` from the +entry record across `Restart` and such a step (`BodyOKA.ofRestartKh`). + +Named premises: none. New module. +-/ +import Fl4ProofsA64.P3.Stage3BodyOKA +import Fl4ProofsA64.P3.ScRelA +import Fl4ProofsA64.P3.DelSinvLoopA +import Fl4ProofsA64.P3.ScNtfnLinkA +import Fl4ProofsA64.P3.Stage3LiftBodyA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +/-- `IdleRdyA` across an SC replaced by one with the same refills. -/ +theorem IdleRdyA.updScA {σ : State Unit} {p : ObjRef} {x y : SchedContext} {k : Nat} + (hs : σ.kheap p = some (.schedContext x k)) (h : y.scRefills = x.scRefills) (hr : IdleRdyA σ) : + IdleRdyA (updKSc σ p (.schedContext y k)) := fun y2 n g => by + change (refillHd y2).rTime ≤ σ.curTime + by_cases e : idleScPtr = p + · have g' : (updKSc σ p (.schedContext y k)).kheap idleScPtr = some (.schedContext y2 n) := g + rw [e] at g' + simp only [updKSc_kheap, Function.update_self, Option.some.injEq, KernelObject.schedContext.injEq] at g' + obtain ⟨h1, -⟩ := g' + have h0 := hr x k (by rw [e]; exact hs) + have : refillHd y2 = refillHd x := by rw [← h1]; unfold refillHd; rw [h] + rw [this]; exact h0 + · have g' : (updKSc σ p (.schedContext y k)).kheap idleScPtr = some (.schedContext y2 n) := g + simp only [updKSc_kheap, Function.update_of_ne e] at g' + exact hr y2 n g' + +/-- `IdleRdyA` across a replacement by a non-SC object. -/ +theorem IdleRdyA.updOtherA {σ : State Unit} {p : ObjRef} {o : KernelObject} + (ho : ∀ y k, o ≠ .schedContext y k) (hr : IdleRdyA σ) : IdleRdyA (updKSc σ p o) := fun y2 n g => by + change (refillHd y2).rTime ≤ σ.curTime + by_cases e : idleScPtr = p + · have g' : (updKSc σ p o).kheap idleScPtr = some (.schedContext y2 n) := g + rw [e] at g' + simp only [updKSc_kheap, Function.update_self, Option.some.injEq] at g' + exact (ho y2 n g').elim + · have g' : (updKSc σ p o).kheap idleScPtr = some (.schedContext y2 n) := g + simp only [updKSc_kheap, Function.update_of_ne e] at g' + exact hr y2 n g' + +/-- A TCB replaced by one with the same state. -/ +theorem BodyKeepA.ofTcbUpd {σ : State Unit} {p : ObjRef} {y y' : Tcb} (hy : σ.kheap p = some (.tcb y)) + (hs : y'.tcbState = y.tcbState) : BodyKeepA σ (updKSc σ p (.tcb y')) where + act := rfl + time := rfl + last := rfl + cur := rfl + csc := rfl + cons := rfl + rel := rfl + sc := fun q z k h => by + by_cases e : q = p + · subst e; simp [updKSc] at h + · simpa [updKSc, Function.update_of_ne e] using h + tcb := fun q x' h => by + by_cases e : q = p + · subst e + simp only [updKSc, Function.update_self, Option.some.injEq, KernelObject.tcb.injEq] at h + subst h + exact ⟨y, hy, hs⟩ + · exact ⟨x', by simpa [updKSc, Function.update_of_ne e] using h, rfl⟩ + +/-- A step that keeps the scalar fields and changes the heap only by TCBs with the same state and objects that are +neither TCBs nor SCs (both before and after). -/ +theorem BodyKeepA.ofKh {σ σ' : State Unit} (act : σ'.schedulerAction = σ.schedulerAction) + (time : σ'.curTime = σ.curTime) (last : σ'.machineState.lastMachineTime = σ.machineState.lastMachineTime) + (cur : σ'.curThread = σ.curThread) (csc : σ'.curSc = σ.curSc) (cons : σ'.consumedTime = σ.consumedTime) + (rel : σ'.releaseQueue = σ.releaseQueue) + (kh : ∀ q, σ'.kheap q = σ.kheap q ∨ + (∃ x x', σ.kheap q = some (.tcb x) ∧ σ'.kheap q = some (.tcb x') ∧ x'.tcbState = x.tcbState) ∨ + (∀ o, σ'.kheap q = some o → (∀ y k, o ≠ .schedContext y k) ∧ ∀ x, o ≠ .tcb x)) : + BodyKeepA σ σ' where + act := act + time := time + last := last + cur := cur + csc := csc + cons := cons + rel := rel + sc := fun q y k h => by + rcases kh q with e | ⟨x, x', -, e2, -⟩ | f + · rw [← e]; exact h + · rw [h] at e2; cases e2 + · exact absurd rfl ((f _ h).1 y k) + tcb := fun q x' h => by + rcases kh q with e | ⟨x, x2, e1, e2, f⟩ | f + · exact ⟨x', by rw [← e]; exact h, rfl⟩ + · rw [h] at e2; cases e2; exact ⟨x, e1, f⟩ + · exact absurd rfl ((f _ h).2 x') + +/-- A notification and a TCB replaced; the TCB keeps its state. -/ +theorem BodyKeepA.ofNtfnTcb {σ : State Unit} {p t : ObjRef} {x x' : Notification} {y y' : Tcb} + (hp : σ.kheap p = some (.notification x)) (hy : σ.kheap t = some (.tcb y)) (hs : y'.tcbState = y.tcbState) : + BodyKeepA σ (updKSc (updKSc σ p (.notification x')) t (.tcb y')) := by + have hpt : p ≠ t := fun e => by subst e; rw [hp] at hy; cases hy + refine BodyKeepA.ofKh rfl rfl rfl rfl rfl rfl rfl (fun q => ?_) + by_cases e1 : q = t + · subst e1 + exact Or.inr (Or.inl ⟨y, y', hy, by simp [updKSc], hs⟩) + · by_cases e2 : q = p + · subst e2 + refine Or.inr (Or.inr fun o ho => ?_) + simp only [updKSc, Function.update_of_ne hpt, Function.update_self, Option.some.injEq] at ho + subst ho + exact ⟨fun _ _ h => (by cases h), fun _ h => (by cases h)⟩ + · exact Or.inl (by simp [updKSc, Function.update_of_ne e1, Function.update_of_ne e2]) + +/-- **What a dispatch arm leaves at its end state** (`DispPostA`): the body-end facts and the unchanged current thread. -/ +def BodyEndA (σ σ' : State Unit) (m : McState) : Prop := BodyOKA σ' m ∧ σ'.curThread = σ.curThread + +section +variable {S A : Type} {st : machine.Store S} [L : LawfulStore st] +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s : S} {a : A} {σ σ' : State Unit} {m : McState} + +/-- `BodyEndA` across `Restart` of the caller and a `BodyKeepA` step. -/ +theorem BodyEndA.ofKeep (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {x : Tcb} (hk : σ.kheap σ.curThread = some (.tcb x)) + (K : BodyKeepA (stUpdA σ σ.curThread x .restart) σ') : BodyEndA σ σ' m := + ⟨BodyOKA.ofKeep J hk K, K.cur⟩ + +/-- `BodyEndA` across `Restart` of the caller and one TCB replaced by one with the same state. -/ +theorem BodyEndA.ofTcbUpd (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {x : Tcb} (hk : σ.kheap σ.curThread = some (.tcb x)) {p : ObjRef} {y y' : Tcb} + (hy : (stUpdA σ σ.curThread x .restart).kheap p = some (.tcb y)) (hs : y'.tcbState = y.tcbState) : + BodyEndA σ (updKSc (stUpdA σ σ.curThread x .restart) p (.tcb y')) m := + BodyEndA.ofKeep J hk (BodyKeepA.ofTcbUpd hy hs) + +/-- `BodyEndA` across `Restart` and one notification and one TCB replaced (the TCB keeps its state). -/ +theorem BodyEndA.ofNtfnTcb (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {x : Tcb} (hk : σ.kheap σ.curThread = some (.tcb x)) {p t : ObjRef} {nx nx' : Notification} {y y' : Tcb} + (hp : (stUpdA σ σ.curThread x .restart).kheap p = some (.notification nx)) + (hy : (stUpdA σ σ.curThread x .restart).kheap t = some (.tcb y)) (hs : y'.tcbState = y.tcbState) : + BodyEndA σ (updKSc (updKSc (stUpdA σ σ.curThread x .restart) p (.notification nx')) t (.tcb y')) m := + BodyEndA.ofKeep J hk (BodyKeepA.ofNtfnTcb hp hy hs) + +/-- `BodyEndA` across `Restart` and one TCB replaced by one with the same state, with the scheduler action then +`chooseNewThread` (a `reschedule_required` of the current thread). -/ +theorem BodyEndA.ofTcbUpdChoose (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {x : Tcb} (hk : σ.kheap σ.curThread = some (.tcb x)) {p : ObjRef} {y y' : Tcb} + (hy : (stUpdA σ σ.curThread x .restart).kheap p = some (.tcb y)) : + BodyEndA σ { updKSc (stUpdA σ σ.curThread x .restart) p (.tcb y') with schedulerAction := .chooseNewThread } m := by + refine ⟨BodyOKA.ofEntry J (Or.inr rfl) rfl rfl rfl rfl rfl rfl (fun y2 k h => ?_) (fun e => by cases e), rfl⟩ + have ne1 : σ.curSc ≠ σ.curThread := fun e => by + obtain ⟨y0, j, h0⟩ := J.curInv.sc + rw [e, hk] at h0; cases h0 + have e1 : (stUpdA σ σ.curThread x .restart).kheap σ.curSc = σ.kheap σ.curSc := by + simp [stUpdA, setObjA, Function.update_of_ne ne1] + have nep : p ≠ σ.curSc := fun e => by + subst e + obtain ⟨y0, j, h0⟩ := J.curInv.sc + rw [e1, h0] at hy; cases hy + simp only [updKSc, Function.update_of_ne nep.symm] at h + exact ⟨y2, by rw [← e1]; exact h, rfl, rfl, rfl⟩ + +/-- `BodyEndA` across `Restart` and a step that replaces one TCB by one with the same state in the heap, sets the +scheduler action to `chooseNewThread` and keeps the clock, the current thread and SC, the consumed time and the +release queue (a re-queue and `reschedule_required`). -/ +theorem BodyEndA.ofChoose (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {x : Tcb} (hk : σ.kheap σ.curThread = some (.tcb x)) {p : ObjRef} {y y' : Tcb} + (hy : (stUpdA σ σ.curThread x .restart).kheap p = some (.tcb y)) + (hkh : σ'.kheap = Function.update (stUpdA σ σ.curThread x .restart).kheap p (some (.tcb y'))) + (hact : σ'.schedulerAction = .chooseNewThread) (htime : σ'.curTime = σ.curTime) + (hlast : σ'.machineState.lastMachineTime = σ.machineState.lastMachineTime) + (hcur : σ'.curThread = σ.curThread) (hcsc : σ'.curSc = σ.curSc) (hcons : σ'.consumedTime = σ.consumedTime) + (hrel : σ'.releaseQueue = σ.releaseQueue) : BodyEndA σ σ' m := by + refine ⟨BodyOKA.ofEntry J (Or.inr hact) htime hlast hcur hcsc hcons hrel (fun y2 k h => ?_) + (fun e => by rw [hact] at e; cases e), hcur⟩ + have ne1 : σ.curSc ≠ σ.curThread := fun e => by + obtain ⟨y0, j, h0⟩ := J.curInv.sc + rw [e, hk] at h0; cases h0 + have e1 : (stUpdA σ σ.curThread x .restart).kheap σ.curSc = σ.kheap σ.curSc := by + simp [stUpdA, setObjA, Function.update_of_ne ne1] + have nep : p ≠ σ.curSc := fun e => by + subst e + obtain ⟨y0, j, h0⟩ := J.curInv.sc + rw [e1, h0] at hy; cases hy + rw [hkh, Function.update_of_ne nep.symm] at h + exact ⟨y2, by rw [← e1]; exact h, rfl, rfl, rfl⟩ + +/-- **`BodyEndA` for any action** (Integration round 2): the pending action kept, `chooseNewThread`, or +`switchThread t` with `SwA` all enter as `SwA σ'` (which `hrun` completes for the no-switch case). The caller is +`Restart`ed, the clock, the current thread and SC and the consumed time are kept, the current thread stays outside +the release queue (another thread's `sched_context_resume` may enqueue itself), and the current SC keeps its refills, +bound and thread. -/ +theorem BodyEndA.ofRestartSw (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {x : Tcb} (hk : σ.kheap σ.curThread = some (.tcb x)) (hsw : SwA σ') (htime : σ'.curTime = σ.curTime) + (hlast : σ'.machineState.lastMachineTime = σ.machineState.lastMachineTime) + (hcur : σ'.curThread = σ.curThread) (hcsc : σ'.curSc = σ.curSc) (hcons : σ'.consumedTime = σ.consumedTime) + (hrel : σ.curThread ∉ σ'.releaseQueue) + (hobj : ∀ y' k, σ'.kheap σ.curSc = some (.schedContext y' k) → + ∃ y, (stUpdA σ σ.curThread x .restart).kheap σ.curSc = some (.schedContext y k) ∧ + y'.scRefills = y.scRefills ∧ y'.scRefillMax = y.scRefillMax ∧ y'.scTcb = y.scTcb) + (hrun : σ'.schedulerAction = .resumeCurThread → ∀ x', σ'.kheap σ.curThread = some (.tcb x') → + x'.tcbState = .running ∨ x'.tcbState = .restart) : BodyEndA σ σ' m := by + have ne1 : σ.curSc ≠ σ.curThread := fun e => by + obtain ⟨y0, j, h0⟩ := J.curInv.sc + rw [e, hk] at h0; cases h0 + have e1 : (stUpdA σ σ.curThread x .restart).kheap σ.curSc = σ.kheap σ.curSc := by + simp [stUpdA, setObjA, Function.update_of_ne ne1] + exact ⟨BodyOKA.ofEntryHT J hsw htime hlast hcur hcsc hcons hrel (fun y' k h => by + obtain ⟨y, hy, h1, h2, h3⟩ := hobj y' k h + exact ⟨y, by rw [← e1]; exact hy, h1, h2, h3⟩) hrun, hcur⟩ + +/-- `BodyEndA` across `Restart` and the SC–notification link step (`scNtfnLinkStA`: the SC keeps its refills, the +notification is no SC and no TCB). -/ +theorem BodyEndA.ofScNtfn (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {x : Tcb} (hk : σ.kheap σ.curThread = some (.tcb x)) {p q : ObjRef} {xs : SchedContext} {ns : Nat} + {xn : Notification} {b : Bool} + (hp : (stUpdA σ σ.curThread x .restart).kheap p = some (.schedContext xs ns)) + (hq : (stUpdA σ σ.curThread x .restart).kheap q = some (.notification xn)) : + BodyEndA σ (scNtfnLinkStA (stUpdA σ σ.curThread x .restart) p q xs ns xn b) m := by + have hct : (stUpdA σ σ.curThread x .restart).kheap σ.curThread = some (.tcb { x with tcbState := .restart }) := by + simp [stUpdA, setObjA] + have hpq : p ≠ q := fun e => by rw [e, hq] at hp; cases hp + have hpc : p ≠ σ.curThread := fun e => by rw [e, hct] at hp; cases hp + have hqc : q ≠ σ.curThread := fun e => by rw [e, hct] at hq; cases hq + have ne1 : σ.curSc ≠ σ.curThread := fun e => by + obtain ⟨y0, j, h0⟩ := J.curInv.sc + rw [e, hk] at h0; cases h0 + have e1 : (stUpdA σ σ.curThread x .restart).kheap σ.curSc = σ.kheap σ.curSc := by + simp [stUpdA, setObjA, Function.update_of_ne ne1] + refine ⟨BodyOKA.ofEntry J (Or.inl rfl) rfl rfl rfl rfl rfl rfl (fun y2 k h => ?_) (fun _ x' hx' => ?_), rfl⟩ + · by_cases e : σ.curSc = p + · rw [e] at h + have h' := h + simp only [scNtfnLinkStA, updKSc_kheap, Function.update_self, Option.some.injEq, + KernelObject.schedContext.injEq] at h' + obtain ⟨rfl, rfl⟩ := h' + refine ⟨xs, ?_, rfl, rfl, rfl⟩ + have hp' : (stUpdA σ σ.curThread x .restart).kheap p = σ.kheap p := by + simp [stUpdA, setObjA, Function.update_of_ne hpc] + rw [e, ← hp']; exact hp + · by_cases e2 : σ.curSc = q + · rw [e2] at h + have h' := h + simp only [scNtfnLinkStA, updKSc_kheap, Function.update_of_ne (Ne.symm hpq), Function.update_self] at h' + cases h' + · simp only [scNtfnLinkStA, updKSc_kheap, Function.update_of_ne e, Function.update_of_ne e2] at h + exact ⟨y2, by rw [← e1]; exact h, rfl, rfl, rfl⟩ + · simp only [scNtfnLinkStA, updKSc_kheap, Function.update_of_ne hpc.symm, Function.update_of_ne hqc.symm] at hx' + rw [hct] at hx' + cases hx' + exact Or.inr rfl + +/-- `BodyEndA` across `Restart`, a step that changes only the ready queues (`σq`), an SC consumed-time update, and the +caller's one-word reply (`ReplyShapeA`: its context replaced and its state `Running`). -/ +theorem BodyEndA.ofConsumedReply (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {x : Tcb} (hk : σ.kheap σ.curThread = some (.tcb x)) {σq σu σ3 : State Unit} {p : ObjRef} {y : SchedContext} + {k : Nat} {v : Word} {x1 : Tcb} + (eqσ : σq = { stUpdA σ σ.curThread x .restart with readyQueues := σq.readyQueues }) + (hy : σq.kheap p = some (.schedContext y k)) + (hσu : σu = updKSc σq p (.schedContext { y with scConsumed := v } k)) + (hS : ReplyShapeA σu σ3 x1) : BodyEndA σ σ3 m := by + have cT : σq.curTime = σ.curTime := congrArg (fun z => z.curTime) eqσ + have cL : σq.machineState.lastMachineTime = σ.machineState.lastMachineTime := + congrArg (fun z => z.machineState.lastMachineTime) eqσ + have cC : σq.curThread = σ.curThread := congrArg (fun z => z.curThread) eqσ + have cS : σq.curSc = σ.curSc := congrArg (fun z => z.curSc) eqσ + have cK : σq.consumedTime = σ.consumedTime := congrArg (fun z => z.consumedTime) eqσ + have cR : σq.releaseQueue = σ.releaseQueue := congrArg (fun z => z.releaseQueue) eqσ + have cA : σq.schedulerAction = σ.schedulerAction := congrArg (fun z => z.schedulerAction) eqσ + have cH : σq.kheap = (stUpdA σ σ.curThread x .restart).kheap := congrArg (fun z => z.kheap) eqσ + obtain ⟨uc, e3⟩ := hS + have hcu : σu.curThread = σ.curThread := by rw [hσu]; exact cC + have hku : ∀ q, σu.kheap q = Function.update σq.kheap p (some (.schedContext { y with scConsumed := v } k)) q := + fun q => by rw [hσu]; rfl + have k3 : ∀ q, σ3.kheap q = Function.update σu.kheap σ.curThread (some (.tcb (replyTcbA x1 uc))) q := fun q => by + rw [e3]; show Function.update σu.kheap σu.curThread _ q = _; rw [hcu] + have a3 : σ3.schedulerAction = σ.schedulerAction := by + rw [e3]; show σu.schedulerAction = _; rw [hσu]; exact cA + have t3 : σ3.curTime = σ.curTime := by rw [e3]; show σu.curTime = _; rw [hσu]; exact cT + have l3 : σ3.machineState.lastMachineTime = σ.machineState.lastMachineTime := by + rw [e3]; show σu.machineState.lastMachineTime = _; rw [hσu]; exact cL + have c3 : σ3.curThread = σ.curThread := by rw [e3]; exact hcu + have s3 : σ3.curSc = σ.curSc := by rw [e3]; show σu.curSc = _; rw [hσu]; exact cS + have n3 : σ3.consumedTime = σ.consumedTime := by rw [e3]; show σu.consumedTime = _; rw [hσu]; exact cK + have r3 : σ3.releaseQueue = σ.releaseQueue := by rw [e3]; show σu.releaseQueue = _; rw [hσu]; exact cR + have ne1 : σ.curSc ≠ σ.curThread := fun e => by + obtain ⟨y0, j, h0⟩ := J.curInv.sc + rw [e, hk] at h0; cases h0 + have e1 : (stUpdA σ σ.curThread x .restart).kheap σ.curSc = σ.kheap σ.curSc := by + simp [stUpdA, setObjA, Function.update_of_ne ne1] + have hct : (stUpdA σ σ.curThread x .restart).kheap σ.curThread = some (.tcb { x with tcbState := .restart }) := by + simp [stUpdA, setObjA] + have hpc : p ≠ σ.curThread := fun e => by + rw [e, cH, hct] at hy; cases hy + have e2 : (stUpdA σ σ.curThread x .restart).kheap p = σ.kheap p := by + simp [stUpdA, setObjA, Function.update_of_ne hpc] + refine ⟨BodyOKA.ofEntry J (Or.inl a3) t3 l3 c3 s3 n3 r3 (fun y2 k2 h => ?_) (fun _ x' hx' => ?_), c3⟩ + · rw [k3, Function.update_of_ne ne1, hku] at h + by_cases e : σ.curSc = p + · rw [e, Function.update_self] at h + simp only [Option.some.injEq, KernelObject.schedContext.injEq] at h + obtain ⟨h1, h2⟩ := h + subst h2 + refine ⟨y, ?_, by rw [← h1], by rw [← h1], by rw [← h1]⟩ + rw [e, ← e2, ← cH]; exact hy + · rw [Function.update_of_ne e, cH, e1] at h + exact ⟨y2, h, rfl, rfl, rfl⟩ + · rw [k3, Function.update_self] at hx' + cases hx' + exact Or.inl rfl + +/-- `BodyEndA` across `Restart`, one TCB replaced by one with the same state, and the caller's one-word reply. -/ +theorem BodyEndA.ofTcbReply (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {x : Tcb} (hk : σ.kheap σ.curThread = some (.tcb x)) {σu σ3 : State Unit} {p : ObjRef} {y y' : Tcb} {x1 : Tcb} + (hy : (stUpdA σ σ.curThread x .restart).kheap p = some (.tcb y)) + (hσu : σu = updKSc (stUpdA σ σ.curThread x .restart) p (.tcb y')) + (hS : ReplyShapeA σu σ3 x1) : BodyEndA σ σ3 m := by + obtain ⟨uc, e3⟩ := hS + have hcu : σu.curThread = σ.curThread := by rw [hσu]; rfl + have hku : ∀ q, σu.kheap q = Function.update (stUpdA σ σ.curThread x .restart).kheap p (some (.tcb y')) q := + fun q => by rw [hσu]; rfl + have k3 : ∀ q, σ3.kheap q = Function.update σu.kheap σ.curThread (some (.tcb (replyTcbA x1 uc))) q := fun q => by + rw [e3]; show Function.update σu.kheap σu.curThread _ q = _; rw [hcu] + have a3 : σ3.schedulerAction = σ.schedulerAction := by rw [e3, hσu]; rfl + have t3 : σ3.curTime = σ.curTime := by rw [e3, hσu]; rfl + have l3 : σ3.machineState.lastMachineTime = σ.machineState.lastMachineTime := by rw [e3, hσu]; rfl + have c3 : σ3.curThread = σ.curThread := by rw [e3, hσu]; rfl + have s3 : σ3.curSc = σ.curSc := by rw [e3, hσu]; rfl + have n3 : σ3.consumedTime = σ.consumedTime := by rw [e3, hσu]; rfl + have r3 : σ3.releaseQueue = σ.releaseQueue := by rw [e3, hσu]; rfl + have ne1 : σ.curSc ≠ σ.curThread := fun e => by + obtain ⟨y0, j, h0⟩ := J.curInv.sc + rw [e, hk] at h0; cases h0 + have e1 : (stUpdA σ σ.curThread x .restart).kheap σ.curSc = σ.kheap σ.curSc := by + simp [stUpdA, setObjA, Function.update_of_ne ne1] + have nep : p ≠ σ.curSc := fun e => by + subst e + obtain ⟨y0, j, h0⟩ := J.curInv.sc + rw [e1, h0] at hy; cases hy + refine ⟨BodyOKA.ofEntry J (Or.inl a3) t3 l3 c3 s3 n3 r3 (fun y2 k2 h => ?_) (fun _ x' hx' => ?_), c3⟩ + · rw [k3, Function.update_of_ne ne1, hku, Function.update_of_ne nep.symm] at h + exact ⟨y2, by rw [← e1]; exact h, rfl, rfl, rfl⟩ + · rw [k3, Function.update_self] at hx' + cases hx' + exact Or.inl rfl + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3TcbBufStepA.lean b/proofs/Fl4ProofsA64/P3/Stage3TcbBufStepA.lean new file mode 100644 index 000000000..da4114bc5 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3TcbBufStepA.lean @@ -0,0 +1,346 @@ +/- +P3 (aarch64, A64-P3 Lift 3b labels): **TCB `set_ipc_buffer` of zero at the record** (the word write and the spec's +`threadSet tcbIpcBuffer := 0` after the buffer slot was emptied). + +* **`bufStateA`** / **`bufStateVA`**: the spec state after the `threadSet` of zero / of a word. +* **`JStage3A.setIpcBuf_stepVA`**: the step below for any 1024-aligned word (`bufAl`). +* **`ipcBufOfA_buf_noneA`**: `ipcBufOfA` is `none` independently of the buffer field when it is `none`. +* **`JStage3A.setIpcBuf_stepA`**: the word at `[t + 3512]` set to 0 and the spec's buffer field set to 0 keep the record + when the target's IPC-buffer cap is not a usable frame (the emptied slot): the buffer coverage is then vacuous for + `t` (the relaxed `tcbIpcBuffer` keep of `of_ipcStepFlA`). + +No named premise (`hnb` is the emptied slot). New module. +-/ +import Fl4ProofsA64.P3.Stage3TcbMcpA +import Fl4ProofsA64.P3.Stage3TcbRestartFrA +import Fl4ProofsA64.P3.Stage3TcbF0A +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-- The spec state after `thread_set (tcb_ipc_buffer := 0)`. -/ +def bufStateA (σ : State Unit) (t : ObjRef) (y : Tcb) : State Unit := + updKSc σ t (.tcb { y with tcbIpcBuffer := 0 }) + +/-- The spec state after `thread_set (tcb_ipc_buffer := v)` (`bufStateA` is `v = 0`). -/ +def bufStateVA (σ : State Unit) (t : ObjRef) (y : Tcb) (v : U64) : State Unit := + updKSc σ t (.tcb { y with tcbIpcBuffer := v.bv }) + +theorem ipcBufOfA_buf_noneA {b : Bool} {y : Tcb} (v : Word) (h : ipcBufOfA b y = none) : + ipcBufOfA b { y with tcbIpcBuffer := v } = none := by + unfold ipcBufOfA at h ⊢ + split at h <;> rename_i heq <;> simp_all + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s : S} {a : A} {σ : State Unit} {m : McState} + +set_option maxHeartbeats 2000000 in +/-- **`set_ipc_buffer` of a 1024-aligned word at the record**, on an emptied buffer slot (module docstring). -/ +theorem JStage3A.setIpcBuf_stepVA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {t : U64} (ht : t ∈ Z.T) {y : Tcb} (hy : σ.kheap (specPtr t) = some (.tcb y)) + (hnb : ∀ b, ipcBufOfA b y = none) (v : U64) (hv : v.val % 1024 = 0) : + BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac (L.wr s (wa t 3512#u64) v) ∧ + (SysL st H Z.Cs OS ia ie (L.wr s (wa t 3512#u64) v) → SymL st H Z.Cs (L.wr s (wa t 3512#u64) v) → + Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U (L.wr s (wa t 3512#u64) v) → + J7A st (J7R2A st ar nA) Z H OS (L.wr s (wa t 3512#u64) v) + (bufStateVA σ (specPtr t) y v) m → + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) + (L.wr s (wa t 3512#u64) v) a (bufStateVA σ (specPtr t) y v) m) := by + set s' := L.wr s (wa t 3512#u64) v with hs' + set σ' := bufStateVA σ (specPtr t) y v with hσ' + have hP := J.iplaced + have htT : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + have hta : TcbAddr t := J.sr.core.tcbs t ht + have blk' : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s' := by + have h := Fl4ProofsA64.P1.obj.Tcb.set_ipc_buffer.blk (st := st) s t v (BXI.blk J.sys.ent.bxi) htT + have e : obj.Tcb.set_ipc_buffer st t s v = ok s' := eq_of_spec_fd (Fl4ProofsA64.P1.obj.Tcb.set_ipc_buffer.wr s t v) + rw [e, WP.spec_ok] at h + exact h.1 + refine ⟨blk', fun sysl symL userOK j7 => ?_⟩ + have aM : Aligned (wa t 3512#u64) := field_aligned hta (K := 3512#u64) (by simp) (by simp) + have vM : (wa t 3512#u64).val = t.val + 3512 := by + show (wa t 3512#u64).val = _ + rw [Notif.tfield_val hta (by decide : (3512#u64 : U64).val < 4096)]; rfl + have w1 : WritesIn st s s' (fun b => b = wa t 3512#u64) := writesIn_wr aM + have PF : PartFrA (st := st) N E Rp Z m s s' σ σ' := + tcbWord_partA (PartFrA.refl N E Rp Z m s σ) hP.e.tcb ht 3512#u64 (by decide) (by decide) (by decide) (by decide) aM + v hy (y' := { y with tcbIpcBuffer := v.bv }) ⟨rfl, rfl, rfl⟩ + have F : SchedFrameA σ σ' := PF.fr + have f0 : TcbF0WA Z.T (wa t 3512#u64) := ⟨t, ht, by omega, by omega, fun k hk => by omega⟩ + -- every TCB word but the written one + have tw : ∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → (u = t → K.val ≠ 3512) → L.mem s' (wa u K) = L.mem s (wa u K) := + fun u hu K h1 h2 h3 => by + have hua : TcbAddr u := J.sr.core.tcbs u hu + refine mem_wr_ne' aM (field_aligned hua h1 h2) fun e => ?_ + have ev := congrArg UScalar.val e + rw [vM, Notif.tfield_val hua h1] at ev + have a1 : t.val % 4096 = 0 := hta.align + have a2 : u.val % 4096 = 0 := hua.align + by_cases hut : u = t + · have h3' := h3 hut + subst hut + omega + · have : u.val ≠ t.val := fun h => hut (UScalar.eq_of_val_eq h) + omega + have key : ∀ q, σ'.kheap q = + if q = specPtr t then some (.tcb { y with tcbIpcBuffer := v.bv }) else σ.kheap q := by + intro q + simp only [hσ', bufStateVA, updKSc_kheap, Function.update_apply] + have keepO : ∀ q o, σ.kheap q = some o → (∀ z, o ≠ .tcb z) → σ'.kheap q = some o := fun q o h n1 => by + rw [key, if_neg (fun e => by rw [e, hy] at h; exact n1 y (Option.some.inj h).symm)] + exact h + have backO : ∀ q o, σ'.kheap q = some o → (∀ z, o ≠ .tcb z) → σ.kheap q = some o := fun q o h n1 => by + rw [key] at h + split at h + · exact absurd (Option.some.inj h).symm (n1 _) + · exact h + have fwdT : ∀ q z, σ.kheap q = some (.tcb z) → ∃ w, σ'.kheap q = some (.tcb w) ∧ + w = { z with tcbIpcBuffer := w.tcbIpcBuffer } := by + intro q z h + by_cases e : q = specPtr t + · rw [e, hy] at h + cases h + exact ⟨{ y with tcbIpcBuffer := v.bv }, by rw [key, if_pos e], rfl⟩ + · exact ⟨z, by rw [key, if_neg e]; exact h, rfl⟩ + have bwdT : ∀ q w, σ'.kheap q = some (.tcb w) → ∃ z, σ.kheap q = some (.tcb z) ∧ + w = { z with tcbIpcBuffer := w.tcbIpcBuffer } := by + intro q w h + rw [key] at h + split at h + · rename_i e + cases h + exact ⟨y, e ▸ hy, rfl⟩ + · exact ⟨w, h, rfl⟩ + have SR : SchedSRA st Z m κ s' σ' := by + refine J.sr.stable w1 (fun b hb => hb ▸ f0WordOffReadA J.sr.toSchedSRN f0) ?_ ?_ + · refine + { fix := ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ + act := rfl + rq := rfl + rel := rfl + sc := fun q x k h => (keepO q _ h (fun _ e => by cases e)).trans h.symm + tcb := fun q x h => by + obtain ⟨w, hw, e⟩ := fwdT q x h + exact ⟨w, hw, by rw [e], by rw [e], by rw [e]⟩ + scBack := fun q x k h => (backO q _ h (fun _ e => by cases e)).trans h.symm + tcbBack := fun q w h => by + obtain ⟨z, hz, -⟩ := bwdT q w h + exact ⟨z, hz⟩ + ntfnBack := fun q z h => ⟨z, backO q _ h (fun _ e => by cases e), rfl⟩ } + · intro q x z hx hz + obtain ⟨w, hw, e⟩ := fwdT q x hx + rw [hz] at hw + cases hw + refine Or.inl ⟨by rw [e], fun h => ?_⟩ + rw [e] at h + exact h + have off : ∀ b : U64, Aligned b → b.val ≠ t.val + 3512 → L.mem s' b = L.mem s b := fun b hb h => + mem_wr_ne' aM hb fun e => h (by rw [← e]; exact vM) + have feE : FootEq Z.T E Rp Bf (L.mem s) (L.mem s') := by + intro b hb hf + refine off b hb fun h => ?_ + have inT : t.val ≤ b.val ∧ b.val < t.val + 4096 := by omega + rcases hf with ⟨e, he, h1⟩ | ⟨r, hr, h1⟩ | ⟨u, hu, h1⟩ | h1 + · have := hP.e.et e he t ht; omega + · have := hP.e.rt r hr t ht; omega + · have hua := hP.e.tcb u hu + have a1 : t.val % 4096 = 0 := hta.align + have a2 : u.val % 4096 = 0 := hua.align + rcases h1 with h1 | h1 + · unfold Endp.TF TOffN at h1 + omega + · unfold TL at h1 + omega + · have := hP.e.bt _ h1 t ht; omega + have kvE : Endp.KView σ σ' := by + refine ⟨fun q z h => keepO q _ h (fun _ e => by cases e), fun q z h => ⟨z, keepO q _ h (fun _ e => by cases e), rfl⟩, + fun q z h => ?_, F.2.2⟩ + obtain ⟨w, hw, e⟩ := fwdT q z h + exact ⟨w, hw, by rw [e], by rw [e], by rw [e], by rw [e], by rw [e]⟩ + have ep : EpStateRel Z.T E Rp Bf (L.mem s') σ' := J.ep.frame hP.e feE kvE + have feN : Notif.FootEq N Z.T (L.mem s) (L.mem s') := by + intro b hb hf + refine off b hb fun h => ?_ + rcases hf with ⟨q, hq, h1, h2⟩ | ⟨u, hu, h1⟩ + · have := hP.n.disj q hq t ht; omega + · have hua := hP.e.tcb u hu + have a1 : t.val % 4096 = 0 := hta.align + have a2 : u.val % 4096 = 0 := hua.align + have hv : b.val = u.val + 3328 ∨ b.val = u.val + 3336 ∨ b.val = u.val + 256 ∨ b.val = u.val + 3448 ∨ + b.val = u.val + 3496 ∨ b.val = u.val + 3504 := by + rcases h1 with h1 | h1 | h1 | h1 | h1 | h1 <;> + rw [h1] <;> + simp only [stA_eq, Notif.tBlockA_eq, Notif.badgeRegA_eq, Notif.prioA_eq', Notif.nextEp_eq, + Notif.prevEp_eq] <;> + rw [Notif.tfield_val hua] <;> simp + omega + have kvN : Notif.KView σ σ' := by + refine ⟨fun q z h => keepO q _ h (fun _ e => by cases e), fun q z h => ?_⟩ + obtain ⟨w, hw, e⟩ := fwdT q z h + refine ⟨w, hw, by rw [e], by rw [e]; rfl, by rw [e]⟩ + have ntfn : NtfnStateRel N Z.T (L.mem s') σ' := J.ntfn.frame hP.n feN kvN + have reply : ReplyStackRelA (L.mem s') Rp Z.C σ' (scNameA m) stack := by + refine J.reply.frame (fun r hr => ?_) (fun c _ z k h => ⟨z, k, keepO _ _ h (fun _ e => by cases e), rfl⟩) + (fun r _ x' h => ⟨x', backO _ _ h (fun _ e => by cases e), rfl⟩) + have hr3 := hP.e.rep r hr + have dis := hP.e.rt r hr t ht + have one : ∀ O : U64, O.val = 8 ∨ O.val = 16 → L.mem s' (wa r O) = L.mem s (wa r O) := by + intro O hO + have hv : (wa r O).val = r.val + O.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact off _ (by unfold Aligned; rw [hv]; omega) (by omega) + exact ⟨one _ (Or.inr (by simp [layout.reply.NEXT])), one _ (Or.inl (by simp [layout.reply.PREV]))⟩ + have bufCover : BufCover st Z.T Bf s' σ' := by + intro u hu z b sb w hz hb hl + by_cases hut : u = t + · subst hut + rw [key, if_pos rfl] at hz + obtain rfl := KernelObject.tcb.inj (Option.some.inj hz) + exact absurd hb (by rw [ipcBufOfA_buf_noneA v.bv (hnb b)]; simp) + · have hx : σ.kheap (specPtr u) = some (.tcb z) := by + rw [key, if_neg (fun e => hut (specPtr_inj e))] at hz; exact hz + have hb0 := hb + have hib : IbSame (L := L) s s' u := by + refine ⟨?_, ?_, ?_⟩ + · rw [ibWordA_eq]; exact tw u hu _ (by simp) (by simp) (fun h => absurd h hut) + · rw [sbWordA_eq]; exact tw u hu _ (by simp) (by simp) (fun h => absurd h hut) + · rw [sbWordA8_eq]; exact tw u hu _ (by simp) (by simp) (fun h => absurd h hut) + rw [lookup_ipc_buffer_sameA hib] at hl + exact J.bufCover u hu z b sb w hx hb0 hl + have bufAl : UtBufAlA Z.T σ' := fun u hu z hz => by + by_cases hut : u = t + · subst hut + rw [key, if_pos rfl] at hz + obtain rfl := KernelObject.tcb.inj (Option.some.inj hz) + exact hv + · have hx : σ.kheap (specPtr u) = some (.tcb z) := by + rw [key, if_neg (fun e => hut (specPtr_inj e))] at hz; exact hz + exact J.bufAl u hu z hx + have idle : IdleNoFaultA m σ' := fun c hc => by + obtain ⟨x, hx, hf⟩ := J.idleNoFault c hc + obtain ⟨w, hw, e⟩ := fwdT _ x hx + exact ⟨w, hw, by rw [e]; exact hf⟩ + have mcp : McpRelA Z.T m (L.mem s') σ' := + J.mcp.congrA (fun u hu => ⟨tw u hu _ (by simp) (by simp) (fun _ => by simp), + tw u hu _ (by simp) (by simp) (fun _ => by simp)⟩) fun u hu x hx => by + obtain ⟨w, hw, e⟩ := fwdT _ x hx + exact ⟨w, hw, by rw [e], by rw [e]⟩ + have yr : YieldRelA Z.T Z.C m (L.mem s') σ' := by + have ye : ∀ u ∈ Z.T, L.mem s' (yieldA u) = L.mem s (yieldA u) := fun u hu => by + unfold yieldA; exact tw u hu _ (by simp) (by simp) (fun _ => by simp) + refine ⟨fun u hu => ?_, fun u hu h => ?_, fun c hc z k h q hq => ?_⟩ + · obtain ⟨x, hx, e1⟩ := J.yieldRel.tcb u hu + obtain ⟨w, hw, e⟩ := fwdT _ x hx + exact ⟨w, hw, by rw [ye u hu, e]; exact e1⟩ + · rw [ye u hu] at h ⊢ + obtain ⟨hC, z, k, hz, e⟩ := J.yieldRel.toSc u hu h + exact ⟨hC, z, k, keepO _ _ hz (fun _ e => by cases e), e⟩ + · obtain ⟨u, hu, e1, e2⟩ := J.yieldRel.frm c hc z k (backO _ _ h (fun _ e => by cases e)) q hq + exact ⟨u, hu, e1, by rw [ye u hu]; exact e2⟩ + have sinv : SInvXA σ' m n := by + refine J.sinv.of_fr + { tcb := fun q x h => by + obtain ⟨w, hw, e⟩ := fwdT q x h + exact ⟨w, hw, by rw [e], by rw [e], by rw [e]⟩ + tcb' := fun q w h => by + obtain ⟨z, hz, e⟩ := bwdT q w h + exact ⟨z, hz, by rw [e], by rw [e], by rw [e]⟩ + sc := fun q z k => ⟨fun h => backO q _ h (fun _ e => by cases e), fun h => keepO q _ h (fun _ e => by cases e)⟩ + cur := rfl + idle := rfl + csc := rfl + rq := rfl + rls := rfl + act := rfl + time := rfl + cons := rfl } ?_ + exact J.sinv.irqN.updTcbA hy rfl + have sidle : SIdleA σ' m n := + { f3 := fun c hc => by + obtain ⟨x, hx, hs⟩ := J.sidle.f3 c hc + obtain ⟨w, hw, e⟩ := fwdT _ x hx + exact ⟨w, hw, by rw [e]; exact hs⟩ + f4 := fun z k h => J.sidle.f4 z k (backO _ _ h (fun _ e => by cases e)) + f5 := fun q z k h => J.sidle.f5 q z k (backO _ _ h (fun _ e => by cases e)) + f6 := J.sidle.f6.of_eq fun sl => by + unfold specCapAtA + by_cases e : sl.1 = specPtr t + · rw [key, if_pos e, e, hy] + rfl + · rw [key, if_neg e] + f7 := fun y k h => J.sidle.f7 y k (backO _ _ h (fun _ e => by cases e)) + idleSlot := fun c hc => by + have h0 := J.sidle.idleSlot c hc + unfold vtCapA at h0 ⊢ + by_cases e : m.idleThreadOf c = specPtr t + · rw [key, if_pos e]; rw [e, hy] at h0; exact h0 + · rw [key, if_neg e]; exact h0 + idleBound := fun c hc => by + have h0 := J.sidle.idleBound c hc + unfold vcpuOfA at h0 ⊢ + by_cases e : m.idleThreadOf c = specPtr t + · rw [key, if_pos e]; rw [e, hy] at h0; exact h0 + · rw [key, if_neg e]; exact h0 } + have rv : RunVcpuInv st Z.T s' := fun u hu h0 => by + have e1 : L.mem s' (runVcpuA u) = L.mem s (runVcpuA u) := by + unfold runVcpuA + exact tw u hu 3672#u64 (by simp) (by simp) (fun _ => by simp) + have e2 : L.mem s' (Fl4ProofsA64.P1.stA u) = L.mem s (Fl4ProofsA64.P1.stA u) := by + rw [Fl4ProofsA64.P1.stA_eq] + exact tw u hu 3328#u64 (by simp) (by simp) (fun _ => by simp) + rw [e2]; exact J.runVcpu u hu (e1 ▸ h0) + have blkQ : BlkQueuedA Z.T E N (L.mem s') σ' := by + refine J.blkQ.keepA (fun u hu => ?_) (fun u _ x' h => ?_) (fun q z h => keepO q _ h (fun _ e => by cases e)) + (fun q z h => keepO q _ h (fun _ e => by cases e)) + · rw [Notif.tBlockA_eq]; exact tw u hu 3336#u64 (by simp) (by simp) (fun _ => by simp) + · obtain ⟨z, hz, e⟩ := bwdT _ x' h + exact Or.inr ⟨z, hz, by rw [e]⟩ + have sv : SchedValidA σ' := J.schedValid.congr rfl rfl rfl rfl rfl rfl + (fun q z k h => keepO q _ h (fun _ e => by cases e)) + (fun u x h => by + obtain ⟨w, hw, e⟩ := fwdT u x h + exact ⟨w, hw, by rw [e]⟩) + (fun u w h => by + obtain ⟨z, hz, e⟩ := bwdT u w h + exact ⟨z, hz, by rw [e], by rw [e]⟩) + have Qk : QKeepA σ σ' := QKeepA.ofBwd fun q o h => by + rw [key] at h + split at h + · rename_i e + cases h + exact ⟨.tcb y, by rw [e]; exact hy, QObjA.ofTcb rfl⟩ + · exact ⟨o, h, QObjA.refl o⟩ + exact J.of_stepFinA (Q := Z.Q) (Rl := Z.R) (curOf := Z.curOf) (curScOf := Z.curScOf) PF.wr F.1 F.2.1 Qk F.2.2 + (fun _ _ => rfl) (BlkOK.bxi blk') sysl symL userOK j7 SR ep ntfn reply bufCover bufAl idle mcp yr sinv sidle + rv blkQ sv + + +set_option maxHeartbeats 2000000 in +/-- **`set_ipc_buffer` of zero at the record** (module docstring). -/ +theorem JStage3A.setIpcBuf_stepA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {t : U64} (ht : t ∈ Z.T) {y : Tcb} (hy : σ.kheap (specPtr t) = some (.tcb y)) + (hnb : ∀ b, ipcBufOfA b y = none) : + BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac (L.wr s (wa t 3512#u64) 0#u64) ∧ + (SysL st H Z.Cs OS ia ie (L.wr s (wa t 3512#u64) 0#u64) → SymL st H Z.Cs (L.wr s (wa t 3512#u64) 0#u64) → + Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U (L.wr s (wa t 3512#u64) 0#u64) → + J7A st (J7R2A st ar nA) Z H OS (L.wr s (wa t 3512#u64) 0#u64) + (bufStateA σ (specPtr t) y) m → + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) + (L.wr s (wa t 3512#u64) 0#u64) a (bufStateA σ (specPtr t) y) m) := + J.setIpcBuf_stepVA ht hy hnb 0#u64 (by simp) + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3TcbBufStepsA.lean b/proofs/Fl4ProofsA64/P3/Stage3TcbBufStepsA.lean new file mode 100644 index 000000000..b8a9f032d --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3TcbBufStepsA.lean @@ -0,0 +1,250 @@ +/- +P3 (aarch64, A64-P3 setipcbuf): **step lemmas of the SetIPCBuffer ≠ 0 arm** (`Stage3LiftTcbBufNzA`). + +* **`JStage3A.p1StepSyA`**: `JStage3A.p1StepA` with the `SysL ∧ SymL` at the post state given (not from a `DMD` step). +* **`JSyscallA.sysDelBufSetA`**: the `SysL ∧ SymL` after the caller's restart, the delete of slot 2 and the word write + `[t + 3512] := v` (`DelR` chain, as `tcb.install_buffer.sysL`). +* **`tcbStillValidFr_keptA`**: after the delete, `still_valid` of the derived (unmapped) copy of a source Frame cap holds + exactly when the source still holds that Frame cap and the invoked slot still holds the thread's cap. +* **`tcbInstallBuffer_skipA`**: `install_buffer` when `still_valid` fails. +* **`setIpcBufG_invokeErrA`** / **`setIpcBufG_invokeA`**: the spec's `fl4InvokeTcb (.threadControlCaps .. (some (ptr, some + (newCap, src))))` from the outcome of its `cap_delete` (Preempted), and from the outcome `σm` of its two + `check_cap_at`s and `cap_insert` at the state after the `threadSet`. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3TcbDelGA +import Fl4ProofsA64.P3.Stage3LiftTcbBufA +import Fl4ProofsA64.P3.Stage3LiftTcbTimeoutNnA +import Fl4ProofsA64.P3.Stage3CapInsertBufA +import Fl4ProofsA64.P3.Stage3P1StepA +import Fl4ProofsA64.P3.TcbInvokeBufferCheckA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Vcpu Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {s s' : S} {a : A} {σ : State Unit} {m : McState} + +/-- **`JStage3A.p1StepA` with `SysL ∧ SymL` given** (module docstring). -/ +theorem JStage3A.p1StepSyA (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (w : WritesIn st s s' (SchedStepNWA (st := st) N E Rp Z m σ s)) + (SY : SysL st H Z.Cs OS ia ie s' ∧ SymL st H Z.Cs s') + {Ac' : Set U64} (hb : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac' s') : + Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s' ∧ + BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac' s' ∧ + ∀ σ' m', J7A st (J7R2A st ar hPl.ipa) Z H OS s' σ' m' := by + obtain ⟨Pt, Ub, Pp7, Tb7, Hr, Te, Kt, Lv7, Re, bits, KLv, W⟩ := J7A.unp J.j7 + have kb : L.ksb s' = L.ksb s := w.1 + have osk : OSKeep st OS s s' := J.osKeep_stepNA w + have notAs : ∀ b, SchedStepNWA (st := st) N E Rp Z m σ s b → ¬ AsW (L.ksb s) b := by + rintro b hb ⟨o, o1, o2, -, rfl⟩ + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.sys.ent.ksw + have ev : (wa (L.ksb s) o).val = (L.ksb s).val + o.val := by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact J.asid_offStepNA (by omega) (by omega) hb + have hvk : ArmVsK st H OS Z.V s s' := ArmVsK.ofW w fun Ub' Pt' hvr _ b hb hv => by + rcases hv with hv | hv + · exact J.vsr_offStepNA hvr (Or.inl hv) (notAs b hb) hb + · exact notAs b hb hv + obtain ⟨arm, tk⟩ := ArmQIAt.ofWK W.vr w notAs osk SY hb hvk.toVs + have Fx := S1aStepFix.ofKeep ⟨arm, tk.toDel, hvk, osk⟩ W.vr W.fu W.vcc W.col W.df W.r2 W.sep W.hiK + have pn : PoolNamed st OS s' := PoolNamed.ofTab kb tk.2 osk W.pn + have hsp : ∀ p, SchedPageA st Z OS s' p → p ∈ Pt := by + obtain ⟨_, -, -, -, ⟨_, _, _, _, _, _, _, R'⟩, -⟩ := id Fx + exact schedPage_ptA R' W.sp + refine ⟨J.sys.ent.user.ksb kb, BlkOK.bxi hb, fun σ' m' => ?_⟩ + exact J7A.of_stepFixedA Fx W.vr kb W.al W.vu pn hsp + (fun b hb o ho => by rw [kb]; exact W.ks b hb o ho) (J7R2K.of_stepFixedA (σ' := σ') (m := m') Fx W.ktK) + +variable {Clock : A → Fl4SpecA64.MachineState → Prop} + +/-- **`SysL ∧ SymL` after the caller's restart, the delete of slot 2 of `t` and the write `[t + 3512] := v`** +(module docstring). -/ +theorem JSyscallA.sysDelBufSetA (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + {slot : U64} (hsl : slot ∈ OS) (hcov : SlotCov st OS s slot) + {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) + {t : U64} (hcap : capA st s slot = .Thread t) + (hstall : smp.remote_tcb_stall st ar s a t = ok (s, a)) {s1 : S} + (hr : tcb.restart_caller st s = ok s1) {sd : S} {ad : A} + (hdel : cdt.delete st ar s1 a (slotIdx t layout.tcb.SLOT_BUFFER) true = ok (.Ok (), sd, ad)) + (v : U64) (hv : v.val % 1024 = 0) : + SysL st H Z.Cs OS ia ie (L.wr sd (wa t 3512#u64) v) ∧ SymL st H Z.Cs (L.wr sd (wa t 3512#u64) v) := by + obtain ⟨Pt, Ub, Pp7, Tb7, Hr, Te, Kt, Lv7, Re, bits, KLv, W⟩ := J7A.unp J.j7 + have I := J.toJStage3A.delIn hA hPl W + have D : DecPre st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits U Ac s (.Thread t) extra := by + have := J.decPreA hsl hext + rwa [hcap] at this + have P := J.toJStage3A.sysPlace hext hsl hcov + have st1 := (WP.spec_ok _).1 (hstall ▸ smp.remote_tcb_stall.delS hA D I a t) + have R1 := (WP.spec_ok _).1 (hr ▸ tcb.restart_caller.delR I D P J.sys.sysl J.symL st1.1.1 st1.1.2 st1.2.1 st1.2.2) + have ht : t ∈ H.tcbs := D.cap.2.1 t rfl + have hx := R1.2.slot I ht (show layout.tcb.SLOT_BUFFER.val < 7 by simp [layout.tcb.SLOT_BUFFER]) + have R2 := (WP.spec_ok _).1 (hdel ▸ cdt.delete.delR (a := a) I R1.2 hx) + have R3 := obj.Tcb.set_ipc_buffer.delR I R2 ht hv + rw [eq_of_spec_fd (Fl4ProofsA64.P1.obj.Tcb.set_ipc_buffer.wr sd t v), WP.spec_ok] at R3 + exact ⟨R3.1.sys, R3.1.sym⟩ + +end + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + +/-- **After the delete, `still_valid` of the derived copy of a source Frame cap** (module docstring). -/ +theorem tcbStillValidFr_keptA (s : S) (target slot src : U64) (hsrc : src ≠ 0#u64) + (ptr rights asid va : U64) (size : capability.FrameSize) (mp ep : Bool) + (hl : capA st s src = .Frame ptr size rights false mp ep asid va ∨ capA st s src = .Null ∨ HomeZA st s src) + (hi : capA st s slot = .Thread target ∨ capA st s slot = .Null ∨ HomeZA st s slot) : + tcb.still_valid st s target slot (.Frame ptr size rights false false false 0#u64 0#u64) src = + ok (decide (capA st s src = .Frame ptr size rights false mp ep asid va ∧ capA st s slot = .Thread target)) := by + have hn : obj.Slot.is_null src = ok false := by + unfold obj.Slot.is_null; rw [decide_eq_false hsrc] + have key : objects.same_object_as (.Thread target) (capA st s slot) = + ok (decide (capA st s slot = .Thread target)) := by + rcases hi with e | e | ⟨zt, n, e⟩ <;> rw [e] <;> simp [objects.same_object_as, objects.same_region_as] + have hsame : objects.same_object_as (.Frame ptr size rights false false false 0#u64 0#u64) (capA st s src) = + ok (decide (capA st s src = .Frame ptr size rights false mp ep asid va)) := by + rcases hl with e | e | ⟨zt, n, e⟩ <;> rw [e] <;> simp [objects.same_object_as, objects.same_region_as] + all_goals (cases size <;> first | rfl | simp [capability.FrameSize.Insts.CoreCmpPartialEqFrameSize.eq] | (unfold capability.FrameSize.Insts.CoreCmpPartialEqFrameSize.eq; simp) | (simp [capability.FrameSize.Insts.CoreCmpPartialEqFrameSize.eq, capability.FrameSize.Insts.CoreCmpPartialEqFrameSize.eq.eq_1]; rfl)) + rw [tcbStillValid_liveA s target slot src _ (capA st s src) hn (capA_eq s src), capA_eq s slot, hsame] + simp only [bind_ok, decide_eq_true_eq] + by_cases hg : capA st s src = .Frame ptr size rights false mp ep asid va + · simp only [hg, if_true, key] + simp + · simp [hg] + +/-- **`install_buffer` when `still_valid` fails**: no insert, then the conditional reschedule. -/ +theorem tcbInstallBuffer_skipA {s sDeleted sBuffer : S} {a aDeleted : A} + {t invSlot src dest addr : U64} {cap : capability.Cap} + (hSlot : obj.Tcb.slot t layout.tcb.SLOT_BUFFER = ok dest) + (hDelete : cdt.delete st ar s a dest true = ok (.Ok (), sDeleted, aDeleted)) + (hBuffer : obj.Tcb.set_ipc_buffer st t sDeleted addr = ok sBuffer) + (hLive : tcb.still_valid st sBuffer t invSlot cap src = ok false) : + tcb.install_buffer st ar s a t invSlot addr (cap, src) = (do + let sFinal ← if t = L.curW sBuffer then sched.reschedule_required st sBuffer else ok sBuffer + ok (.Ok (), sFinal, aDeleted)) := by + rw [tcb.install_buffer] + simp only [hSlot, hDelete, bind_ok, uncurry_apply_pair, core.result.Result.Insts.CoreOpsTry.branch, hBuffer] + simp only [hLive, Bool.false_eq_true, ite_false, bind_ok, cur_thread_ok, + obj.Tcb.Insts.CoreCmpPartialEqTcb.eq, decide_eq_true_eq] + by_cases h : t = L.curW sBuffer + · simp only [if_pos h, Std.bind_assoc, bind_ok] + · simp only [if_neg h, bind_ok] + +end + +section spec + +/-- **The spec's `invoke_tcb` for the buffer, when its `cap_delete` fails** (`Preempted`). -/ +theorem setIpcBufG_invokeErrA {σ1 σd : State Unit} {t : U64} (κs : CslotPtr) (ptr : VSpaceRef) + (frame : Option (Cap × CslotPtr)) + (hdel : (.error (), σd) ∈ ((fl4CapDelete (Z := Unit) (specPtr t, tcbCnodeIndex 2)).run σ1).1) : + (.error (), σd) ∈ ((fl4InvokeTcb (.threadControlCaps (specPtr t) κs none none none none (some (ptr, frame)))).run σ1).1 := by + have hrk : ∀ σ : State Unit, (.ok (), σ) ∈ ((NonDetE.returnOk (ε := Unit) ()).run σ).1 := fun σ => by + rw [run_returnOkD]; simp + simp only [fl4InvokeTcb, fl4InstallTcbCap, fl4InstallTcbFrameCap] + refine mem_bindE_okD (a := ()) (s' := σ1) (hrk σ1) ?_ + refine mem_bindE_okD (a := ()) (s' := σ1) (hrk σ1) ?_ + refine mem_bindE_okD (a := ()) (s' := σ1) (hrk σ1) ?_ + refine mem_bindE_okD (a := ()) (s' := σ1) (hrk σ1) ?_ + exact mem_bindE_errD (mem_bindE_errD hdel) + +/-- **The spec's `invoke_tcb` for the buffer from the outcome `σm` of its `check_cap_at`s and `cap_insert`** at the +state after the `threadSet`, then its `reschedule_required` when the target is the current thread. -/ +theorem setIpcBufG_invokeA {σ1 σd σm : State Unit} {t : U64} {y : Tcb} (κs : CslotPtr) {newCap : Cap} + {κsrc : CslotPtr} (v : U64) + (hdel : (.ok (), σd) ∈ ((fl4CapDelete (Z := Unit) (specPtr t, tcbCnodeIndex 2)).run σ1).1) + (hy : σd.kheap (specPtr t) = some (.tcb y)) + (hmid : ((), σm) ∈ ((getCap (Z := Unit) κsrc >>= fun live => + Fl4Spec.NonDet.when (fl4TcbSameObject newCap live) (getCap (Z := Unit) κs >>= fun cap => + Fl4Spec.NonDet.when (fl4TcbSameObject (.threadCap (specPtr t)) cap) + (capInsert (Z := Unit) newCap κsrc (specPtr t, tcbCnodeIndex 2)))) (bufStateVA σd (specPtr t) y v)).1) + {σf : State Unit} + (hlast : (σm.curThread ≠ specPtr t ∧ σf = σm) ∨ + (σm.curThread = specPtr t ∧ ((), σf) ∈ (rescheduleRequired (Z := Unit) σm).1)) : + (.ok [], σf) ∈ ((fl4InvokeTcb (.threadControlCaps (specPtr t) κs none none none none + (some (Fl4SpecA64.dataToVref v.bv, some (newCap, κsrc))))).run σ1).1 := by + have hset : threadSet (Z := Unit) (fun x : Tcb => { x with tcbIpcBuffer := Fl4SpecA64.dataToVref v.bv }) + (specPtr t) σd = ({((), bufStateVA σd (specPtr t) y v)}, False) := threadSet_eqSc _ hy + have hrk : ∀ σ : State Unit, (.ok (), σ) ∈ ((NonDetE.returnOk (ε := Unit) ()).run σ).1 := fun σ => by + rw [run_returnOkD]; simp + have hs1 : ((), bufStateVA σd (specPtr t) y v) ∈ ((threadSet (Z := Unit) + (fun x : Tcb => { x with tcbIpcBuffer := Fl4SpecA64.dataToVref v.bv }) (specPtr t)) σd).1 := by + rw [hset]; simp + simp only [fl4InvokeTcb, fl4InstallTcbCap, fl4InstallTcbFrameCap] + refine mem_bindE_okD (a := ()) (s' := σ1) (hrk σ1) ?_ + refine mem_bindE_okD (a := ()) (s' := σ1) (hrk σ1) ?_ + refine mem_bindE_okD (a := ()) (s' := σ1) (hrk σ1) ?_ + refine mem_bindE_okD (a := ()) (s' := σ1) (hrk σ1) ?_ + refine mem_bindE_okD (a := ()) (s' := σf) ?_ (by rw [run_returnOkD]; simp) + refine mem_bindE_okD (a := ()) (s' := σd) hdel ?_ + refine mem_bindE_okD (a := ()) (s' := bufStateVA σd (specPtr t) y v) (mem_liftED hs1) ?_ + refine mem_bindE_okD (a := ()) (s' := σm) (mem_liftED hmid) ?_ + refine mem_bindE_okD (a := σm.curThread) (s' := σm) (mem_liftED ?_) ?_ + · simp [Fl4Spec.NonDet.gets] + · refine mem_liftED ?_ + rcases hlast with ⟨hne, rfl⟩ | ⟨he, hm⟩ + · simp [Fl4Spec.NonDet.when, if_neg (Ne.symm hne)] + all_goals first | exact Set.mem_singleton _ | exact rfl | simp [Fl4Spec.NonDet.pure] + · simpa [Fl4Spec.NonDet.when, if_pos he.symm] using hm + +end spec + +section frag +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {κ : U64 → U64} {n : Nat} {m : McState} {Ag : Set U64} {G : MdbGhostA} + {s : S} {a : A} {σ : State Unit} + +/-- **A loop state back at `JStage3A`, with its frame-word agreement** (`DelXA6G.toStage3A` and `Std6GA.frAg`). -/ +theorem DelXA6G.toStage3FrA {R7 : Set U64 → JSlotA S} + (h : DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st R7) s a σ G) + (sysl : SysL st H Z.Cs OS ia ie s) (symL : SymL st H Z.Cs s) (sv : SchedValidA σ) : + ∃ (Q : U64 → U64 → List U64) (R : U64 → List U64) (hw : HwRootA) (Ac : Set U64) + (Bf : Set (U64 × Fl4SpecA64.Word)) (stack : U64 → List U64), + JStage3A st AR { Z with G := G, Q := Q, R := R, hw := hw } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n + (J5A st) (J7A st R7) s a σ m ∧ FrAgA st OS Ag s σ := by + obtain ⟨⟨Q, R, hw, Ac, Bf, stack, J, X⟩, -, -, -, -, fx⟩ := h + have J' : JStage3TA st AR { Z with G := G, Q := Q, R := R, hw := hw } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n + (J5A st) (J7A st R7) s a σ m := { J with j7 := J7LA.toA J.j7 sysl.userOffFr } + exact ⟨Q, R, hw, Ac, Bf, stack, J'.toJA sysl X.vcSym1 X.vcSym2 X.heapWin X.coresAll fx.ptCapIn symL fx.fhOK + X.zwf sv X.utWin X.devWin X.vcBack X.runBack X.vcSnap X.vcOS X.userFr X.frTh X.bfSpec, X.frAg⟩ + +/-- **`cdt::insert` keeps the current thread** (its writes are cap-slot words: `lrF`, `blk_of_capw`). -/ +theorem cdt.insert.curWA (hO : OSOK H.tcbs H.omega H.frame OS) {s : S} (hS : MdbS st OS s) + {Cs Ac : Set U64} (hb : BlkOK st H.tcbs Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s) + {x y : U64} {nc : capability.Cap} + (hx : x ∈ OS) (hy : y ∈ OS) (hxy : x ≠ y) (hyc : obj.sext48 y = ok y) (hsc0 : capA st s x ≠ .Null) + (hw : CapRt.EncWf nc) : cdt.insert st s nc x y ⦃ s' => L.curW s' = L.curW s ⦄ := by + obtain ⟨-, nn⟩ := hS.lnk hO hx + unfold cdt.insert + rw [capA_eq, bind_ok] + exact WP.spec_mono (cdt.insert_with_cap.lrF hO (LR.self s) hx hy hxy hyc rfl hsc0 hw nn) fun s' h => + (W7.blk_of_capw hb h.1 fun b hb' => hb').2.2.1 + +/-- A 1024-aligned address is aligned to the IPC-buffer size. -/ +theorem bufAligned_ofModA (addr : U64) (h : addr.val % 1024 = 0) : isAligned addr.bv msgAlignBits := by + rw [tcbBufferAlignedA] + have e : (addr &&& 1023#u64).val = addr.val % 1024 := by + change (addr.bv &&& (1023 : Word)).toNat = addr.bv.toNat % 1024 + rw [BitVec.toNat_and, show (1023 : Word).toNat = 2 ^ 10 - 1 by rfl, Nat.and_two_pow_sub_one_eq_mod] + exact UScalar.eq_of_val_eq (e.trans h) + +end frag + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3TcbDelGA.lean b/proofs/Fl4ProofsA64/P3/Stage3TcbDelGA.lean new file mode 100644 index 000000000..bc3535159 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3TcbDelGA.lean @@ -0,0 +1,139 @@ +/- +P3 (aarch64, A64-P3 setipcbuf): **the caller's restart and one deletion of a slot, keeping frame-word agreement** on a +guard set `Ag` (the guarded twin of `JSyscallA.restartDelA`). + +* **`JSyscallA.delRestartWGA`**: `DelWTopGA` at the caller's restarted state, from the entry agreement `FrAgA st OS Ag s σ` + (the restart writes only the caller's state word, which no live frame word is: `JStage3TA.frw_offStepNA`). +* **`JSyscallA.restartDelGA`**: `restartDelA` with the loop record at `DelWTopGA st Ag`; its post (`WPostA`) carries + `FrAgA` at the deletion's end (`Std6GA.frAg`), through the delframe engine (`delHypsA_topGA`, `Vcpu.vcpuHookA`). + +Named premise: the entry agreement `hag` ([S], Architect ruling (b′) 2026-12-05 20:24Z: the entry-state 128-word +agreement of the new IPC buffer), plus those of `JSyscallA.restartDelA`. New module. +-/ +import Fl4ProofsA64.P3.Stage3TcbDelStepA +import Fl4ProofsA64.P3.Stage3ReplyWA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Vcpu Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +section +variable {m : Fl4SpecA64.McState} +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {Clock : A → Fl4SpecA64.MachineState → Prop} {Ag : Set U64} + +/-- **`DelWTopGA` at the caller's restarted state**, from the entry agreement (module docstring). -/ +theorem JSyscallA.delRestartWGA {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + {slot : U64} (hsl : slot ∈ OS) (hcov : SlotCov st OS s slot) + {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hCl : Clock a σ.machineState) (hCnt : CounterStepA σ) + (hag : FrAgA st OS Ag s σ) + (x : Fl4SpecA64.Tcb) (hkx : σ.kheap σ.curThread = some (.tcb x)) : + DelWTopGA st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar hPl.ipa Clock s + (L.wr s (stA (L.curW s)) sched.TS_RESTART) a (stUpdA σ σ.curThread x .restart) Z.G := by + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hsl hcov J.actW J.running J.cur + have hT : TreeA st OS Z.G.chains Z.G.par s := J.delTreeA + have hPar : ParentAdjA st Z.G.chains Z.G.par s := J.j5.1.1.parentAdj + have hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM := J.toJStage3EntryA.curNotVmA + obtain ⟨x', hkx', -, -, J', -, hOK, -⟩ := J.toJStage3A.of_restartCurA (J.toJStage3A.sysPlace hext hsl hcov) + RS.sym RS.run hnvm RS.cur RS.vs RS.vc + obtain rfl : x' = x := by + rw [hkx] at hkx' + cases hkx' + rfl + have noSw : ∀ u, (stUpdA σ σ.curThread x' .restart).schedulerAction ≠ .switchThread u := fun u h => by + have h' : σ.schedulerAction = .switchThread u := h + rw [J.act] at h' + cases h' + have sact : SActA (stUpdA σ σ.curThread x' .restart) := + { f1 := fun u h => absurd h (noSw u) + f2 := ⟨{ x' with tcbState := .restart }, by simp [stUpdA, setObjA], Or.inr (Or.inl rfl)⟩ } + have JT := J'.toTA sact noSw + have JL : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7LA st (J7R2A st ar hPl.ipa)) + (L.wr s (stA (L.curW s)) sched.TS_RESTART) a (stUpdA σ σ.curThread x' .restart) m := + { JT with j7 := J7A.toLA JT.j7 } + -- the entry agreement at the restarted state + have JT0 := J.toJStage3A.toTA J.sact J.noSwitch + have JL0 : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7LA st (J7R2A st ar hPl.ipa)) + s a σ m := { JT0 with j7 := J7A.toLA JT0.j7 } + have htZ : L.curW s ∈ Z.T := J.sr.curT + have hta : TcbAddr (L.curW s) := J.sr.core.tcbs _ htZ + have sA : Aligned (stA (L.curW s)) := by rw [stA_eq]; exact field_aligned hta (by simp) (by simp) + have fa : FrAgA st OS Ag (L.wr s (stA (L.curW s)) sched.TS_RESTART) (stUpdA σ σ.curThread x' .restart) := + FrAgA.stepA hag (fun b lv => FrameWordLive.keepA (fun u hu => hOK.capA hu) lv) + (fun b al lv => mem_wr_ne' sA al fun e => + JL0.frw_offStepNA J.sys.sysl.fo al lv + (e ▸ Or.inl (Or.inr (stA_tcbStepWA (fun t ht => J.sr.core.tcbs t ht) htZ)))) rfl + have fx : DelCapFxA st Z H (L.wr s (stA (L.curW s)) sched.TS_RESTART) := + { fhSlot := fun t ht => isSlot_fhA ((J'.sr.thT t).2 ht) + fhOK := J'.fhOK + ptCapIn := J'.ptCapIn } + have X : DelXA6G st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m (J5A st) (J7LA st (J7R2A st ar hPl.ipa)) + (L.wr s (stA (L.curW s)) sched.TS_RESTART) a (stUpdA σ σ.curThread x' .restart) Z.G := + ⟨⟨Z.Q, Z.R, Z.hw, Ac, Bf, stack, JL, Std6GA.ofJA J' fa⟩, ParentAdjA.keepOS hT.lk hOK hPar, J'.zombieNoVcpu, + J'.sys.sysl.bh, J'.sys.sysl.ct, fx⟩ + have V : ValidMachineTimeA (stUpdA σ σ.curThread x' .restart) := J.validTime + have CE : CurEntryGA (stUpdA σ σ.curThread x' .restart) := + (CurEntryGA.of_cur J.toJStage3EntryA.curInv J.toJStage3EntryA.offReady J.sr.inv).keepA + (tcbW_tmA (x' := { x' with tcbState := .restart }) hkx' (fun _ h => h)).2.2.2 (BitVec.le_refl _) + exact ⟨⟨⟨L.ksb_wr _ _ _, by rw [LawfulStore.cpu_wr]⟩, X, J'.schedValid⟩, V, hCl, hCnt, CE⟩ + +set_option maxHeartbeats 4000000 in +/-- **The caller's restart, then the deletion of the slot `sl`, keeping the agreement on `Ag`** (module docstring). -/ +theorem JSyscallA.restartDelGA [PlainStore st] (s : S) (a : A) (σ : Fl4SpecA64.State Unit) (m : Fl4SpecA64.McState) + (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hCnt : CounterStepA σ) + {slot : U64} (hsl : slot ∈ OS) (hcov : SlotCov st OS s slot) + {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) + (P : DelPlatA st ar AR Clock) (hCl : Clock a σ.machineState) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (hag : FrAgA st OS Ag s σ) + (sl : Fl4SpecA64.CslotPtr) (hAd : AbsSlotA Z.th sl) : + ∃ x, sched.set_thread_state st s (L.curW s) sched.TS_RESTART = ok (L.wr s (stA (L.curW s)) sched.TS_RESTART) ∧ + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + DStateA st Z.th OS (L.wr s (stA (L.curW s)) sched.TS_RESTART) (stUpdA σ σ.curThread x .restart) Z.G ∧ + cdt.delete st ar (L.wr s (stA (L.curW s)) sched.TS_RESTART) a (cslotAddrA sl) true ⦃ res => + ∃ σ' G', DStateA st Z.th OS res.2.1 σ' G' ∧ + ((res.1 = .Ok () ∧ capA st res.2.1 (cslotAddrA sl) = .Null ∧ ((.ok (), σ') ∈ ((capDeleteGA Fl4SpecA64.fl4FinaliseCap sl).run + (stUpdA σ σ.curThread x .restart)).1) ∧ ∀ y ∈ OS, y ≠ cslotAddrA sl → ChgZA st s res.2.1 y) ∨ + (res.1 = .Err .Preempted ∧ (.error (), σ') ∈ ((capDeleteGA Fl4SpecA64.fl4FinaliseCap sl).run + (stUpdA σ σ.curThread x .restart)).1)) ∧ + WPostA + (DelWBodyA (DelWTopGA st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar hPl.ipa Clock s) σ.curThread m) + (DelWBodyPA (DelWTopGA st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar hPl.ipa Clock s) σ.curThread m) + res.1 res.2.1 res.2.2 σ' G' ⦄ := by + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hsl hcov J.actW J.running J.cur + obtain ⟨x, heq, hset, D1, hW1⟩ := decDel_restartWA J.toJEntA (J.toJStage3A.sysPlace hext hsl hcov) J.symL + J.running RS.cur RS.vs RS.vc J.delTreeA J.j5.2.1 J.j5.2.2 J.delSiteA + (W := DelWBodyA (DelWTopGA st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar hPl.ipa Clock s) σ.curThread m) + (fun x hkx _ => ⟨J.delRestartWGA hPl hsl hcov hext hCl hCnt hag x hkx, J.delRestartBodyA x hkx⟩) + obtain ⟨_, -, heq', -, -, -, hOK, -, -⟩ := J.toJEntA.restartA (J.toJStage3A.sysPlace hext hsl hcov) J.symL + J.running RS.cur RS.vs RS.vc + refine ⟨x, heq, hset, D1, ?_⟩ + refine WP.spec_mono (delete_exposedGA J.toJEntA.sys.ent.os J.delCanA + (J.delHypsTopA hPl P hCs) sl hAd _ a _ Z.G D1 hW1) ?_ + rintro r ⟨σ', G', D', -, -, hres, hW'⟩ + rcases hres with ⟨h1, ⟨v, hv⟩, hnul, -, hch⟩ | ⟨h1, hv⟩ + · refine ⟨σ', G', D', Or.inl ⟨h1, hnul, capDeleteGA_ok hv, fun y hy hne => ?_⟩, hW'⟩ + have e : capA st (L.wr s (stA (L.curW s)) sched.TS_RESTART) y = capA st s y := hOK.capA hy + rcases hch y hy hne with h | h | h + · exact Or.inl (h.trans e) + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr h) + · exact ⟨σ', G', D', Or.inr ⟨h1, capDeleteGA_err hv⟩, hW'⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3TcbDelStepA.lean b/proofs/Fl4ProofsA64/P3/Stage3TcbDelStepA.lean new file mode 100644 index 000000000..569526107 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3TcbDelStepA.lean @@ -0,0 +1,106 @@ +/- +P3 (aarch64, A64-P3 Lift 3b labels): **the caller's restart and one deletion of a slot, at a syscall entry, with the +DelBodyA body frame** (the shared head of SetIPCBuffer, SetTimeoutEndpoint and SetSchedParams). + +* **`JSyscallA.restartDelA`**: `decDel_restartWA` (the caller's Restart) then `delete_exposed_capDeleteA` on an abstract + slot `sl` at `DelWBodyA` / `DelWBodyPA` over `DelWTopA`, i.e. `cdt::delete st ar s1 a (cslotAddrA sl) true`'s + post: `DStateA`, the spec's `capDeleteGA fl4FinaliseCap sl` outcome and `WPostA` (the loop record with the body + invariant at `Ok`, the preempted exit's body facts at `Preempted`). At `Ok` every other `OS` slot keeps its cap, is + emptied, or is a zombie's home (`ChgZA`). + +Named premises: those of `JSyscallA.delHypsTopA` (`DelPlatA`, `hCl`, `hCs`) and `hCnt`. New module. +-/ +import Fl4ProofsA64.P3.Stage3DelAsmTopA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Vcpu Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +section +variable {m : Fl4SpecA64.McState} +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {Clock : A → Fl4SpecA64.MachineState → Prop} + +set_option maxHeartbeats 4000000 in +/-- **The caller's restart, then the deletion of the slot `sl`** (module docstring). -/ +theorem JSyscallA.restartDelA [PlainStore st] (s : S) (a : A) (σ : Fl4SpecA64.State Unit) (m : Fl4SpecA64.McState) + (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hCnt : CounterStepA σ) + {slot : U64} (hsl : slot ∈ OS) (hcov : SlotCov st OS s slot) + {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) + (P : DelPlatA st ar AR Clock) (hCl : Clock a σ.machineState) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) + (sl : Fl4SpecA64.CslotPtr) (hAd : AbsSlotA Z.th sl) : + ∃ x, sched.set_thread_state st s (L.curW s) sched.TS_RESTART = ok (L.wr s (stA (L.curW s)) sched.TS_RESTART) ∧ + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + DStateA st Z.th OS (L.wr s (stA (L.curW s)) sched.TS_RESTART) (stUpdA σ σ.curThread x .restart) Z.G ∧ + cdt.delete st ar (L.wr s (stA (L.curW s)) sched.TS_RESTART) a (cslotAddrA sl) true ⦃ res => + ∃ σ' G', DStateA st Z.th OS res.2.1 σ' G' ∧ + ((res.1 = .Ok () ∧ capA st res.2.1 (cslotAddrA sl) = .Null ∧ ((.ok (), σ') ∈ ((capDeleteGA Fl4SpecA64.fl4FinaliseCap sl).run + (stUpdA σ σ.curThread x .restart)).1) ∧ ∀ y ∈ OS, y ≠ cslotAddrA sl → ChgZA st s res.2.1 y) ∨ + (res.1 = .Err .Preempted ∧ (.error (), σ') ∈ ((capDeleteGA Fl4SpecA64.fl4FinaliseCap sl).run + (stUpdA σ σ.curThread x .restart)).1)) ∧ + WPostA + (DelWBodyA (DelWTopA st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar hPl.ipa Clock s) σ.curThread m) + (DelWBodyPA (DelWTopA st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar hPl.ipa Clock s) σ.curThread m) + res.1 res.2.1 res.2.2 σ' G' ⦄ := by + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hsl hcov J.actW J.running J.cur + obtain ⟨x, heq, hset, D1, hW1⟩ := decDel_restartWA J.toJEntA (J.toJStage3A.sysPlace hext hsl hcov) J.symL + J.running RS.cur RS.vs RS.vc J.delTreeA J.j5.2.1 J.j5.2.2 J.delSiteA + (W := DelWBodyA (DelWTopA st AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar hPl.ipa Clock s) σ.curThread m) + (fun x hkx _ => ⟨J.delRestartWA hPl hsl hcov hext hCl hCnt x hkx, J.delRestartBodyA x hkx⟩) + obtain ⟨_, -, heq', -, -, -, hOK, -, -⟩ := J.toJEntA.restartA (J.toJStage3A.sysPlace hext hsl hcov) J.symL + J.running RS.cur RS.vs RS.vc + refine ⟨x, heq, hset, D1, ?_⟩ + refine WP.spec_mono (delete_exposedGA J.toJEntA.sys.ent.os J.delCanA + (J.delHypsTopA hPl P hCs) sl hAd _ a _ Z.G D1 hW1) ?_ + rintro r ⟨σ', G', D', -, -, hres, hW'⟩ + rcases hres with ⟨h1, ⟨v, hv⟩, hnul, -, hch⟩ | ⟨h1, hv⟩ + · refine ⟨σ', G', D', Or.inl ⟨h1, hnul, capDeleteGA_ok hv, fun y hy hne => ?_⟩, hW'⟩ + have e : capA st (L.wr s (stA (L.curW s)) sched.TS_RESTART) y = capA st s y := hOK.capA hy + rcases hch y hy hne with h | h | h + · exact Or.inl (h.trans e) + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr h) + · exact ⟨σ', G', D', Or.inr ⟨h1, capDeleteGA_err hv⟩, hW'⟩ + +/-- **`SysL` / `SymL` after the caller's restart and the deletion of a TCB slot** (the `DelR` chain of P1's +`tcb.install_buffer.sysL`, as the head of the label arms): the stall is quiet, `tcb.restart_caller`, then `cdt::delete` +of slot `i < 7` of the target thread `t`, whatever its result. -/ +theorem JSyscallA.sysDelSlotA (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + {slot : U64} (hsl : slot ∈ OS) (hcov : SlotCov st OS s slot) + {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) + {t : U64} (hcap : capA st s slot = .Thread t) (i : U64) (hi : i.val < 7) + (hstall : smp.remote_tcb_stall st ar s a t = ok (s, a)) {s1 : S} + (hr : tcb.restart_caller st s = ok s1) {r : core.result.Result Unit error.Exc} {sd : S} {ad : A} + (hdel : cdt.delete st ar s1 a (slotIdx t i) true = ok (r, sd, ad)) : + SysL st H Z.Cs OS ia ie sd ∧ SymL st H Z.Cs sd := by + obtain ⟨Pt, Ub, Pp7, Tb7, Hr, Te, Kt, Lv7, Re, bits, KLv, W⟩ := J7A.unp J.j7 + have I := J.toJStage3A.delIn hA hPl W + have D : DecPre st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits U Ac s (.Thread t) extra := by + have := J.decPreA hsl hext + rwa [hcap] at this + have P := J.toJStage3A.sysPlace hext hsl hcov + have st1 := (WP.spec_ok _).1 (hstall ▸ smp.remote_tcb_stall.delS hA D I a t) + have R1 := (WP.spec_ok _).1 (hr ▸ tcb.restart_caller.delR I D P J.sys.sysl J.symL st1.1.1 st1.1.2 st1.2.1 st1.2.2) + have ht : t ∈ H.tcbs := D.cap.2.1 t rfl + have hx := R1.2.slot I ht hi + have := (WP.spec_ok _).1 (hdel ▸ cdt.delete.delR (a := a) I R1.2 hx) + exact ⟨this.sys, this.sym⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3TcbF0A.lean b/proofs/Fl4ProofsA64/P3/Stage3TcbF0A.lean new file mode 100644 index 000000000..027c6e083 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3TcbF0A.lean @@ -0,0 +1,268 @@ +/- +P3 (aarch64, A64-P3 Stage 3, D8 step 3): **the F0 frames of the TCB labels that write only plain TCB words**. + +* `TcbF0WA T b`: `b` is a word of a TCB in `T` at offset `≥ 224`, not the state word (3328), not the two words + `TcbStepWA` excludes (3664/3672) and not a word the scheduler relation reads (`SReadA`'s TCB offsets). +* For a write set inside `TcbF0WA`: `SchedSRA` and `StEqA` are kept with the spec state unchanged + (`SchedSRA.f0A`, `StEqA.f0A`) and a `PartFrA` extends (`PartFrA.f0A`). +* Labels: **SetFlags** (`tcbSetFlags_partA`: caller `Restart`, flags word, the Call reply registers, caller + `Running`), **SetPACKeys** (`tcbPacPost_partA`, the twelve PAC words after `Restart`), **SetPriority** on an + inactive target (`tcbSetPriority_inactivePartA`), **WriteFPU** (`tcbFpuImage_partA`, the 65-word TCB image). + +No premise. Ratified statements unchanged. +-/ +import Fl4ProofsA64.P3.Stage3TcbRestartFrA +import Fl4ProofsA64.P3.TcbInvokeFlagsA +import Fl4ProofsA64.P3.TcbInvokePacA +import Fl4ProofsA64.P3.TcbInvokePriorityA +import Fl4ProofsA64.P3.TcbInvokeFpuA +import Fl4ProofsA64.P3.ScYieldFrameA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4SpecA64 Sched +noncomputable section + +section conc +variable {S A : Type} {st : machine.Store S} [L : LawfulStore st] +local notation "wa" => core.num.U64.wrapping_add + +/-- A plain TCB word (module docstring). -/ +def TcbF0WA (T : Set U64) (b : U64) : Prop := + ∃ t ∈ T, t.val + 224 ≤ b.val ∧ b.val < t.val + 4096 ∧ + ∀ k : Nat, (k = 3328 ∨ k = 3664 ∨ k = 3672 ∨ k = 3520 ∨ k = 3448 ∨ k = 3432 ∨ k = 3464 ∨ k = 3592 ∨ + k = 3480 ∨ k = 3488) → b.val - t.val ≠ k + +theorem TcbF0WA.stepN {N E Rp : Set U64} {Z : XSetsA} {m : McState} {σ : State Unit} {s : S} + (hP : TcbPlaced Z.T) {b : U64} (h : TcbF0WA Z.T b) : SchedStepNWA (st := st) N E Rp Z m σ s b := by + obtain ⟨t, ht, h1, h2, hk⟩ := h + exact Or.inl (Or.inr (Endp.tcbStepWA_of_off hP ht (b.val - t.val) (by omega) (by omega) + (hk _ (by simp)) (hk _ (by simp)) (by omega))) + +/-- A plain TCB word is not read by the scheduler relation. -/ +theorem f0WordOffReadA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRN st Z m κ s σ) {b : U64} (hb : TcbF0WA Z.T b) : ¬ SReadA st Z m s σ b := by + obtain ⟨t, ht, hlo, hhi, hoff⟩ := hb + have hks : b.val < (L.ksb s).val ∨ (L.ksb s).val + ksSizeA ≤ b.val := by + have h := H.tcb_ks ht + omega + have hsc : ¬ ScFootA Z.C m σ b := by + intro h + have hn := H.scp.th_out h ((H.thT t).2 ht) + simp only [ObjKindA.size] at hn + omega + have glob {k : U64} (hk : k.val < ksSizeA) : b ≠ wa (L.ksb s) k := by + intro e + have hf := H.ksFit + have hv : (wa (L.ksb s) k).val = (L.ksb s).val + k.val := by rw [wa_valm]; omega + rw [e, hv] at hks + omega + rintro (⟨c, hc, o, ho, e⟩ | e | e | ⟨u, hu, k, hk, e⟩ | h) + · have hv := H.node_in hc ho + rw [e] at hks + omega + · exact glob (by decide) e + · exact glob (by decide) e + · have hkv : k.val = 3520 ∨ k.val = 3448 ∨ k.val = 3432 ∨ k.val = 3464 ∨ k.val = 3592 ∨ k.val = 3480 ∨ + k.val = 3488 := by + rcases hk with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> simp + have hb := congrArg UScalar.val e + rw [field_val (H.core.tcbs u hu) (by omega)] at hb + have a1 : t.val % 4096 = 0 := (H.core.tcbs t ht).align + have a2 : u.val % 4096 = 0 := (H.core.tcbs u hu).align + exact hoff k.val (by omega) (by omega) + · exact hsc h + +/-- `SchedSRA` across plain TCB word writes, the spec state unchanged. -/ +theorem Sched.SchedSRA.f0A {Z : XSetsA} {m : McState} {κ : U64 → U64} {s s' : S} {σ : State Unit} + (H : SchedSRA st Z m κ s σ) {W : U64 → Prop} (hw : WritesIn st s s' W) (hW : ∀ b, W b → TcbF0WA Z.T b) : + SchedSRA st Z m κ s' σ := by + refine H.stable hw (fun b hb => f0WordOffReadA H.toSchedSRN (hW b hb)) (⟨SFixA.refl _, rfl, rfl, rfl, + fun _ _ _ _ => rfl, fun _ x hx => ⟨x, hx, rfl, rfl, rfl⟩, fun _ _ _ _ => rfl, fun _ x hx => ⟨x, hx⟩, + fun _ x hx => ⟨x, hx, rfl⟩⟩ : SStepA σ σ) ?_ + intro p x y hx hy + rw [hx] at hy + cases hy + exact Or.inl ⟨rfl, id⟩ + +/-- The state words are kept by plain TCB word writes. -/ +theorem stEqA_f0A {T : Set U64} {s s' : S} {σ : State Unit} (h : StEqA T (L.mem s) σ) (hP : TcbPlaced T) + {W : U64 → Prop} (hw : WritesIn st s s' W) (hW : ∀ b, W b → TcbF0WA T b) : StEqA T (L.mem s') σ := by + intro u hu x hx + have hu' := hP u hu + rw [hw.mem_eq (by rw [stA_eq]; exact field_aligned hu' (by decide) (by decide)) ?_] + · exact h u hu x hx + · intro hb + obtain ⟨t, ht, hlo, hhi, hoff⟩ := hW _ hb + have hv : (stA u).val = u.val + 3328 := by rw [stA_eq, field_val hu' (by decide)]; rfl + have a1 : t.val % 4096 = 0 := (hP t ht).align + have a2 : u.val % 4096 = 0 := hu'.align + exact hoff 3328 (by simp) (by omega) + +/-- A `PartFrA` extended by plain TCB word writes. -/ +theorem Sched.PartFrA.f0A {N E Rp : Set U64} {Z : XSetsA} {m : McState} {s sR s' : S} {σ σR : State Unit} + (F : PartFrA (st := st) N E Rp Z m s sR σ σR) (hP : TcbPlaced Z.T) {W : U64 → Prop} + (hw : WritesIn st sR s' W) (hW : ∀ b, W b → TcbF0WA Z.T b) : + PartFrA (st := st) N E Rp Z m s s' σ σR := + F.trans ⟨hw.mono fun b hb => (hW b hb).stepN hP, PreO.refl σR⟩ + +/-- The three facts carried along an F0 label: scheduler relation, state words and the footprint. -/ +def F0StA (N E Rp : Set U64) (Z : XSetsA) (m : McState) (κ : U64 → U64) (s : S) (σ : State Unit) (s' : S) + (σ' : State Unit) : Prop := + SchedSRA st Z m κ s' σ' ∧ StEqA Z.T (L.mem s') σ' ∧ PartFrA (st := st) N E Rp Z m s s' σ σ' + +theorem F0StA.wr {N E Rp : Set U64} {Z : XSetsA} {m : McState} {κ : U64 → U64} {s s1 s2 : S} + {σ σ1 : State Unit} (h : F0StA (st := st) N E Rp Z m κ s σ s1 σ1) (hP : TcbPlaced Z.T) {W : U64 → Prop} + (hw : WritesIn st s1 s2 W) (hW : ∀ b, W b → TcbF0WA Z.T b) : F0StA (st := st) N E Rp Z m κ s σ s2 σ1 := + ⟨Sched.SchedSRA.f0A h.1 hw hW, stEqA_f0A h.2.1 hP hw hW, Sched.PartFrA.f0A h.2.2 hP hw hW⟩ + +theorem F0StA.wr1 {N E Rp : Set U64} {Z : XSetsA} {m : McState} {κ : U64 → U64} {s s1 : S} + {σ σ1 : State Unit} (h : F0StA (st := st) N E Rp Z m κ s σ s1 σ1) (hP : TcbPlaced Z.T) {b : U64} + (hal : Aligned b) (hb : TcbF0WA Z.T b) (v : U64) : F0StA (st := st) N E Rp Z m κ s σ (L.wr s1 b v) σ1 := + F0StA.wr h hP (writesIn_wr hal) fun _ e => e ▸ hb + +/-! ### Word instances -/ + +theorem f0_tflA {T : Set U64} (hP : TcbPlaced T) {t : U64} (ht : t ∈ T) : + Aligned (Endp.tTflA t) ∧ TcbF0WA T (Endp.tTflA t) := by + have hv : (Endp.tTflA t).val = t.val + 3456 := by + unfold Endp.tTflA; rw [field_val (hP t ht) (by decide)]; rfl + refine ⟨by unfold Endp.tTflA; exact field_aligned (hP t ht) (by decide) (by decide), t, ht, by omega, + by omega, fun k hk => by omega⟩ + +theorem f0_regA {T : Set U64} (hP : TcbPlaced T) {t i : U64} (ht : t ∈ T) (hi : i.val < 37) : + Aligned (Endp.regA t i) ∧ TcbF0WA T (Endp.regA t i) := by + have hv := Endp.regA_val (hP t ht) hi + exact ⟨(Endp.regA_tf (hP t ht) hi).1, t, ht, by omega, by omega, fun k hk => by omega⟩ + +/-- A word range `[t + lo, t + hi)` of a TCB, inside the plain words. -/ +theorem f0_rangeA {T : Set U64} {t : U64} (ht : t ∈ T) {lo hi : Nat} (h1 : 224 ≤ lo) (h2 : hi ≤ 3328) + {b : U64} (hb1 : t.val + lo ≤ b.val) (hb2 : b.val < t.val + hi) : TcbF0WA T b := + ⟨t, ht, by omega, by omega, fun k hk => by omega⟩ + +/-! ### SetFlags (label 17) -/ + +theorem f0_set_runningA {N E Rp : Set U64} {Z : XSetsA} {m : McState} {κ : U64 → U64} {s s3 : S} + {σ σR : State Unit} (h : F0StA (st := st) N E Rp Z m κ s σ s3 σR) (hP : TcbPlaced Z.T) {ct : U64} + (hct : ct ∈ Z.T) {x : Tcb} (hk : σR.kheap (specPtr ct) = some (.tcb x)) (hr : runnable x.tcbState = true) : + sched.set_thread_state st s3 ct sched.TS_RUNNING ⦃ s4 => ∃ σ4, + setThreadState (Z := Unit) (specPtr ct) .running σR = ({((), σ4)}, False) ∧ + F0StA (st := st) N E Rp Z m κ s σ s4 σ4 ⦄ := by + refine WP.spec_mono (sc_set_runnable_stateA h.1 h.2.1 hct hk hr (ts := .running) rfl + (by rw [ts_vals.1]; rfl)) ?_ + rintro s4 ⟨σ4, h4, H4, hst4, -, w4⟩ + refine ⟨σ4, h4, H4, hst4, h.2.2.trans ?_⟩ + refine mcs_partA (X := fun b => b = stA ct) ?_ ?_ (frame_of_runA + (inferInstance : SPresInst (setThreadState (Z := Unit) (specPtr ct) .running)).pres h4) + · rintro b rfl; exact stA_stepNA hP hct + · exact w4.mono fun b hb => hb.elim (fun h => Or.inl (Or.inr h)) Or.inr + +/-- **SetFlags (label 17)** as an F0 step: the caller's `Restart`, the flags word, on Call the reply registers +(MR0, badge, message info) and the caller's `Running` write only `SchedStepNWA` words; `SchedSRA`/`StEqA` hold at +the end with the spec's two caller-state updates (the flags field and the reply data are TCB fields the frame +`tcbUpd_frameA` adds). -/ +theorem tcbSetFlags_partA {ar : machine.Arch A} {N E Rp : Set U64} {Z : XSetsA} {m : McState} + {κ : U64 → U64} {s s1 : S} {a a' : A} {σ : State Unit} + (H : SchedSRA st Z m κ s σ) (hst : StEqA Z.T (L.mem s) σ) (hP : TcbPlaced Z.T) + (hct : L.curW s ∈ Z.T) {xc : Tcb} (hkc : σ.kheap (specPtr (L.curW s)) = some (.tcb xc)) + (hrun : runnable xc.tcbState = true) + {t clear set : U64} (ht : t ∈ Z.T) {ctx : tcb.Ctx} + (hstall : smp.remote_tcb_stall st ar s a t = ok (s, a)) + (hlen : ¬ ctx.length < 2#u64) (hr : tcb.restart_caller st s = ok s1) + (hclear : tcb.arg st s ctx 0#u64 = ok clear) (hset : tcb.arg st s ctx 1#u64 = ok set) + (harch : let new := (L.mem s1 (Endp.tTflA t) &&& ~~~clear) ||| (set &&& fl4_abi.consts.tcb_flag.MASK) + (if (new &&& fl4_abi.consts.tcb_flag.FPU_DISABLED) != 0#u64 then ar.fpu_release a t else ok a) = ok a') + (hlk : ctx.call = true → ∃ w, msg.lookup_ipc_buffer st + (L.wr s1 (Endp.tTflA t) ⟨fl4TcbFlagsWord (L.mem s1 (Endp.tTflA t)).bv clear.bv set.bv⟩) true + (L.curW s) = ok w) : + tcb.decode st ar s a fl4_abi.label.TCBSetFlags t ctx ⦃ r => r.1 = .Ok () ∧ r.2.2 = a' ∧ ∃ σR σ4, + setThreadState (Z := Unit) (specPtr (L.curW s)) .restart σ = ({((), σR)}, False) ∧ + setThreadState (Z := Unit) (specPtr (L.curW s)) .running σR = ({((), σ4)}, False) ∧ + F0StA (st := st) N E Rp Z m κ s σ r.2.1 σ4 ⦄ := by + have he := tcbSetFlags_exactA hstall hlen hr hclear hset harch + dsimp only at he + rw [he] + have hW := tcbRestartCaller_partA (N := N) (E := E) (Rp := Rp) H hst hct hP hkc hrun (cur_thread_ok s) + rw [hr] at hW + obtain ⟨σR, hR, HR, hstR, hkR, FR⟩ := (WP.spec_ok _).1 hW + have hk3 : σR.kheap (specPtr (L.curW s)) = some (.tcb { xc with tcbState := .restart }) := by + rw [hkR]; simp + have h1 : F0StA (st := st) N E Rp Z m κ s σ s1 σR := ⟨HR, hstR, FR⟩ + have hfl := f0_tflA hP ht + have h2 := F0StA.wr1 h1 hP hfl.1 hfl.2 ⟨fl4TcbFlagsWord (L.mem s1 (Endp.tTflA t)).bv clear.bv set.bv⟩ + have hreg := fun (i : U64) (hi : i.val < 37) => f0_regA hP hct hi + have hX2 : layout.reg.X2.val < 37 := by unfold layout.reg.X2; decide + have hBd : layout.reg.BADGE.val < 37 := by unfold layout.reg.BADGE layout.reg.X0; decide + have hMi : layout.reg.MSG_INFO.val < 37 := by unfold layout.reg.MSG_INFO layout.reg.X1; decide + refine WP.spec_bind (Pₘ := fun s3 => F0StA (st := st) N E Rp Z m κ s σ s3 σR) ?_ ?_ + · by_cases hc : ctx.call = true + · obtain ⟨w, hw⟩ := hlk hc + rw [if_pos hc, hw, bind_ok] + refine (WP.spec_ok _).2 ?_ + unfold tcbFlagReplyA + exact F0StA.wr1 (F0StA.wr1 (F0StA.wr1 h2 hP (hreg _ hX2).1 (hreg _ hX2).2 _) hP (hreg _ hBd).1 + (hreg _ hBd).2 _) hP (hreg _ hMi).1 (hreg _ hMi).2 _ + · rw [if_neg hc] + exact (WP.spec_ok _).2 h2 + · intro s3 h3 + refine WP.spec_bind (f0_set_runningA h3 hP hct hk3 rfl) ?_ + rintro s4 ⟨σ4, h4, F4⟩ + exact (WP.spec_ok _).2 ⟨rfl, rfl, σR, σ4, hR, h4, F4⟩ + +/-! ### SetPACKeys (label 72) -/ + +/-- **SetPACKeys (label 72)**: the concrete post state of `tcbSetPacKeys_exactA` (the caller's `Restart`, then +the ten key words, flags and epoch at `[t + 1680, t + 1776)`) is an F0 step. -/ +theorem tcbPacPost_partA {N E Rp : Set U64} {Z : XSetsA} {m : McState} {κ : U64 → U64} {s s1 : S} + {σ : State Unit} (H : SchedSRA st Z m κ s σ) (hst : StEqA Z.T (L.mem s) σ) (hP : TcbPlaced Z.T) + (hct : L.curW s ∈ Z.T) {xc : Tcb} (hkc : σ.kheap (specPtr (L.curW s)) = some (.tcb xc)) + (hrun : runnable xc.tcbState = true) (hr : tcb.restart_caller st s = ok s1) + {t : U64} (ht : t ∈ Z.T) (keys : Aeneas.Std.Array U64 10#usize) (flags epoch : U64) : + ∃ σR, setThreadState (Z := Unit) (specPtr (L.curW s)) .restart σ = ({((), σR)}, False) ∧ + F0StA (st := st) N E Rp Z m κ s σ (tcbPacPostA st s1 t keys flags epoch) σR := by + have hW := tcbRestartCaller_partA (N := N) (E := E) (Rp := Rp) H hst hct hP hkc hrun (cur_thread_ok s) + rw [hr] at hW + obtain ⟨σR, hR, HR, hstR, -, FR⟩ := (WP.spec_ok _).1 hW + obtain ⟨k1, k2, k3⟩ := tcbPacPost_frameA (st := st) s1 t (hP t ht) keys flags epoch + refine ⟨σR, hR, F0StA.wr (⟨HR, hstR, FR⟩ : F0StA (st := st) N E Rp Z m κ s σ s1 σR) hP + (W := fun b => t.val + 1680 ≤ b.val ∧ b.val < t.val + 1680 + 96) ⟨k1, k2, fun b hb hn => k3 b hb ?_⟩ ?_⟩ + · omega + · rintro b ⟨h1, h2⟩ + exact f0_rangeA ht (lo := 1680) (hi := 1776) (by omega) (by omega) h1 (by omega) + +/-! ### SetPriority (label 6), inactive target -/ + +/-- **SetPriority on an inactive target**: the concrete post `L.wr sRestart (prioA t) p` of +`tcbSetPriority_inactivePerformA` with its spec post (`tcbWord_partA`; the priority word is read by the scheduler, +so this label is a `PartFrA` step only, the relation is the perform theorem's). -/ +theorem tcbSetPriority_inactivePartA {N E Rp : Set U64} {Z : XSetsA} {m : McState} {s sR : S} + {σ σR : State Unit} (F : PartFrA (st := st) N E Rp Z m s sR σ σR) (hP : TcbPlaced Z.T) {t : U64} + (ht : t ∈ Z.T) (p : U64) (p' : Priority) {x : Tcb} (hx : σR.kheap (specPtr t) = some (.tcb x)) : + PartFrA (st := st) N E Rp Z m s (L.wr sR (prioA t) p) σ + (updKSc σR (specPtr t) (.tcb { x with tcbPriority := p' })) := + tcbWord_partA F hP ht 3448#u64 (by decide) (by decide) (by decide) (by decide) + (field_aligned (hP t ht) (K := 3448#u64) (by decide) (by decide)) p hx ⟨rfl, rfl, rfl⟩ + +/-! ### WriteFPU (label 19) -/ + +/-- **WriteFPU (label 19)**: the 65-word copy into the target's FPU image (`tcbFpuTransfer_exactA`, `toFrame = +false`) after the caller's `Restart` is an F0 step. -/ +theorem tcbFpuImage_partA {N E Rp : Set U64} {Z : XSetsA} {m : McState} {κ : U64 → U64} {s s1 : S} + {σ σ1 : State Unit} (h : F0StA (st := st) N E Rp Z m κ s σ s1 σ1) (hP : TcbPlaced Z.T) {t : U64} + (ht : t ∈ Z.T) (src : U64) : + F0StA (st := st) N E Rp Z m κ s σ (tcbFpuCopyA st s1 src (wa t layout.tcb.FPU)) σ1 := by + have hF : layout.tcb.FPU.val = 1088 := by unfold layout.tcb.FPU; decide + have hv : (wa t layout.tcb.FPU).val = t.val + 1088 := by + rw [field_val (hP t ht) (by rw [hF]; decide), hF] + have hal : Aligned (wa t layout.tcb.FPU) := + field_aligned (hP t ht) (by unfold layout.tcb.FPU; decide) (by unfold layout.tcb.FPU; decide) + have hfit : (wa t layout.tcb.FPU).val + fl4FpuStateSize ≤ 2 ^ 64 := by + have := (hP t ht).fits; unfold fl4FpuStateSize; omega + refine F0StA.wr h hP (tcbFpuCopy_exactA s1 src _ hal hfit).2 ?_ + rintro b ⟨k, hk, hb⟩ + have : (65#u64).val = 65 := by decide + exact f0_rangeA ht (lo := 1088) (hi := 1608) (by omega) (by omega) (by omega) (by omega) + +end conc +end +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3TcbFlagsA.lean b/proofs/Fl4ProofsA64/P3/Stage3TcbFlagsA.lean new file mode 100644 index 000000000..cfa4c049c --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3TcbFlagsA.lean @@ -0,0 +1,630 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 3): **TCB SetFlags at the record**. + +* **`flagReply_reply1A`**: SetFlags' inline reply (`x2`, badge, message info, `Running`) is `vspace.reply1` of the new + word (`x2` and the badge are written in the other order). +* **`setFlags_specA`**: the spec's `invokeFl4Tcb setFlags` when the target need not release the FPU. +* **`JSyscallA.setFlagsA`**: `decode_invocation` on a Thread cap with the SetFlags label at a syscall entry: the + caller's `Restart`, the target's flags word `[t + 3456]`, the FPU release call, then `replyOneMidA`. + +Named premises: +* [J] `hfl`: the target's flags word is the spec's `tcbFlags`. **Missing record fact**: the A64 record has no relation + for `[t + 3456]`; the conclusion re-establishes it for the target. +* [P] `ArchTotal`, `MaxSpi`, `ArchPlat`, `A64FrameMasks`, `hCs`; [P] `hX : StoreExt st` (the two reply writes + commute); [P] `hrel` (the architecture's FPU release call), `hk : TcbArchKeepsA ar AR`. +* [S] `h1` (`numNodes = 1`, the remote stall); [S] `howner` (with `FPU_DISABLED` set, the target does not own the + FPU; the spec's owner hand-over is not lifted). + +The decode succeeds; the `Err` returns are not lifted. New module. +-/ +import Fl4ProofsA64.P3.Stage3Lift3QA +import Fl4ProofsA64.P3.Stage3ReplyMidA +import Fl4ProofsA64.P3.Stage3TcbFpuA +import Fl4ProofsA64.P3.TcbInvokeFlagsA +import Fl4ProofsA64.P3.ScConsumeOpA +import Fl4ProofsA64.P1.FastFault +import Fl4ProofsA64.P3.Stage3TcbBodyA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-- The spec's TCB with the flags word `w`. -/ +def flagsTcbA (y : Tcb) (w : Word) : Tcb := { y with tcbFlags := wordToTcbFlags w } + +/-- **The spec's SetFlags** (module docstring). -/ +theorem setFlags_specA {σ : State Unit} {t : ObjRef} {y : Tcb} (hk : σ.kheap t = some (.tcb y)) + {old clear set : Word} (hold : wordToTcbFlags old = y.tcbFlags) + (howner : fl4TcbFlagsWord old clear set &&& 1 ≠ 0 → σ.archState.armCurrentFpuOwner ≠ some t) : + (.ok [fl4TcbFlagsWord old clear set], updKSc σ t (.tcb (flagsTcbA y (fl4TcbFlagsWord old clear set)))) ∈ + ((invokeFl4Tcb (.setFlags t clear set)).run σ).1 := by + set w := fl4TcbFlagsWord old clear set with hw + set σ' := updKSc σ t (.tcb (flagsTcbA y w)) with hσ' + have hget : (y.tcbFlags, σ) ∈ (threadGet (Z := Unit) Tcb.tcbFlags t σ).1 := by + rw [threadGet_eqSc _ hk]; exact rfl + have hset : ((), σ') ∈ (Fl4SpecA64.setFlags (Z := Unit) t (wordToTcbFlags w) σ).1 := by + unfold Fl4SpecA64.setFlags threadSet + refine ⟨(y, σ), ?_, ?_⟩ + · rw [Notif.getTcb_eq hk]; exact rfl + · change ((), σ') ∈ (setObject (Z := Unit) t (.tcb (flagsTcbA y w)) σ).1 + rw [setObject_eqSc (o' := .tcb (flagsTcbA y w)) hk rfl]; exact rfl + have hrel : ((), σ') ∈ (Fl4Spec.NonDet.when (w &&& 1 ≠ 0) (fpuRelease (Z := Unit) t) σ').1 := by + unfold Fl4Spec.NonDet.when + split + · rename_i h + have h' : ¬ σ'.archState.armCurrentFpuOwner = some t := howner h + unfold fpuRelease + rw [nd_gets_bindA] + show ((), σ') ∈ (Fl4Spec.NonDet.when (σ'.archState.armCurrentFpuOwner = some t) (switchLocalFpuOwner none) σ').1 + unfold Fl4Spec.NonDet.when + rw [if_neg h'] + exact rfl + · exact rfl + apply mem_liftED + refine mem_bind_ndD (x := (y.tcbFlags, σ)) hget ?_ + refine mem_bind_ndD (x := (old, σ)) ⟨hold, rfl⟩ ?_ + refine mem_bind_ndD (x := ((), σ')) hset ?_ + refine mem_bind_ndD (x := ((), σ')) hrel ?_ + exact rfl + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {s : S} {a : A} {σ : State Unit} {m : McState} + +/-- **SetFlags' inline reply is `vspace.reply1`** (module docstring). -/ +theorem flagReply_reply1A (hX : StoreExt st) (s2 : S) {ct v : U64} (hct : TcbAddr ct) (call : Bool) : + ((if call then do + let _ ← msg.lookup_ipc_buffer st s2 true ct + ok (tcbFlagReplyA st s2 ct v) + else ok s2) >>= fun s3 => sched.set_thread_state st s3 ct sched.TS_RUNNING) = + vspace.reply1 st s2 ct call v := by + unfold vspace.reply1 + cases call + · rfl + · simp only [↓reduceIte] + obtain ⟨buf, hbuf, -⟩ := spec_existsA (P0.msg.lookup_ipc_buffer.p0 st s2 true ct L.total) + have hB : layout.reg.BADGE.val < 37 := by unfold layout.reg.BADGE layout.reg.X0; decide + have h2 : layout.reg.X2.val < 37 := by unfold layout.reg.X2; decide + have hm : msg.set_mr st (L.wr s2 (Endp.regA ct layout.reg.BADGE) 0#u64) ct buf 0#u64 v = + ok (1#u64, L.wr (L.wr s2 (Endp.regA ct layout.reg.BADGE) 0#u64) (Endp.regA ct layout.reg.X2) v) := by + rw [Endp.set_mr_reg _ _ _ _ _ (by decide)] + have hr : Endp.regIdxA (Endp.mrIdx (0#u64 : U64).val) = layout.reg.X2 := by + show Endp.regIdxA (Endp.mrIdx 0) = _ + unfold Endp.mrIdx Endp.regIdxA layout.reg.X2 + rfl + rw [hr] + rfl + have hword : msginfo.MessageInfo.to_word ⟨0#u64, 0#u64, 0#u64, 1#u64⟩ = ok 1#u64 := by + apply eq_of_spec_fd + refine WP.spec_mono (Endp.to_word_specA 0#u64 1#u64 (by decide)) fun x hx => ?_ + apply UScalar.eq_of_val_eq + simp only [messageInfoToData, BitVec.zero_and, BitVec.zero_shiftLeft, BitVec.zero_or] at hx + exact congrArg BitVec.toNat hx + have hmi (q : S) : msg.set_msg_info st q ct ⟨0#u64, 0#u64, 0#u64, 1#u64⟩ = + ok (L.wr q (Endp.regA ct layout.reg.MSG_INFO) 1#u64) := by + unfold msg.set_msg_info + rw [hword, bind_ok, Endp.set_reg_eqA] + have vB := Endp.regA_val hct hB + have v2 := Endp.regA_val hct h2 + have aB : Aligned (Endp.regA ct layout.reg.BADGE) := by + have := hct.align; unfold Aligned; omega + have a2 : Aligned (Endp.regA ct layout.reg.X2) := by + have := hct.align; unfold Aligned; omega + have ne : Endp.regA ct layout.reg.BADGE ≠ Endp.regA ct layout.reg.X2 := by + intro e + have := congrArg UScalar.val e + rw [vB, v2] at this + have b0 : layout.reg.BADGE.val = 0 := by unfold layout.reg.BADGE layout.reg.X0; rfl + have b2 : layout.reg.X2.val = 2 := by unfold layout.reg.X2; rfl + omega + have ew : tcbFlagReplyA st s2 ct v = L.wr (L.wr (L.wr s2 (Endp.regA ct layout.reg.BADGE) 0#u64) + (Endp.regA ct layout.reg.X2) v) (Endp.regA ct layout.reg.MSG_INFO) 1#u64 := by + unfold tcbFlagReplyA + rw [wr_comm hX s2 v 0#u64 a2 aB ne.symm] + rw [hbuf] + simp only [bind_ok, Endp.set_reg_eqA, hm, uncurry_apply_pair, msginfo.MessageInfo.new, hmi, ew] + exact bind_tc_ok _ _ + +set_option maxHeartbeats 4000000 in +/-- **TCB SetFlags at a syscall entry** (module docstring). -/ +theorem JSyscallA.setFlagsA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) (hX : StoreExt st) + {slot lbl len buf t clear set : U64} {a' : A} {blk call don : Bool} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Thread t) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hlbl : lbl = fl4_abi.label.TCBSetFlags) (hlen : ¬ len < 2#u64) + (hclear : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 0#u64 = ok clear) + (hset : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 1#u64 = ok set) + (hfl : ∀ y, σ.kheap (specPtr t) = some (.tcb y) → wordToTcbFlags (L.mem s (tTflA t)).bv = y.tcbFlags) + (hrel : (if (fl4TcbFlagsWord (L.mem s (tTflA t)).bv clear.bv set.bv &&& 1 ≠ 0) then ar.fpu_release a t + else ok a) = ok a') + (hk : TcbArchKeepsA ar AR) + (howner : fl4TcbFlagsWord (L.mem s (tTflA t)).bv clear.bv set.bv &&& 1 ≠ 0 → + σ.archState.armCurrentFpuOwner ≠ some (specPtr t)) : + ∃ xc σ1 σ2 d σ3 s3, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + (.ok d, σ2) ∈ ((invokeFl4Tcb (.setFlags (specPtr t) clear.bv set.bv)).run σ1).1 ∧ + σ2.curThread = σ.curThread ∧ + ((), σ3) ∈ ((do + Fl4Spec.NonDet.when call (Fl4SpecA64.replyFromKernel (Z := Unit) σ.curThread (0, d)) + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .running) σ2).1 ∧ + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (.Ok (), s3, a') ∧ + (∀ y, σ2.kheap (specPtr t) = some (.tcb y) → wordToTcbFlags (L.mem s3 (tTflA t)).bv = y.tcbFlags) ∧ + ∃ Q Rl, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s3 a' σ3 m ∧ Sched.BodyEndA σ σ3 m := by + -- the entry: restart, the target, the stall + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hslot hcov J.actW J.running J.cur + have hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM := J.toJStage3EntryA.curNotVmA + obtain ⟨xc, hkc, hst, hsp, J1, -, -, -⟩ := J.toJStage3A.of_restartCurA RS.pl.choose_spec.choose_spec.choose_spec.choose_spec + RS.sym RS.run hnvm RS.cur RS.vs RS.vc + have ht : t ∈ Z.T := J.tcbs ▸ (J.toJStage3A.slotCapTy hslot).1.2.2.2.2.2 t hcap + have T0 := J.toJStage3A.toTA J.sact J.noSwitch + have hstall : smp.remote_tcb_stall st ar s a t = ok (s, a) := T0.stall_singleA hCs h1 ht + set s1 := L.wr s (stA (L.curW s)) sched.TS_RESTART with hs1 + set σ1 := stUpdA σ σ.curThread xc .restart with hσ1 + have sact1 : SActA σ1 := + ⟨fun u h => J.sact.f1 u h, ⟨{ xc with tcbState := .restart }, by simp only [hσ1, stUpdA, setObjA, + Function.update_self], Or.inr (Or.inl rfl)⟩⟩ + have ct : σ.curThread = specPtr (L.curW s) := J.j.1.sch.curThread + set c0 := L.curW s with hc0 + have hcZ : c0 ∈ Z.T := J.sr.curT + have hca : TcbAddr c0 := J.sr.core.tcbs _ hcZ + have hta : TcbAddr t := J.sr.core.tcbs t ht + have sA : Aligned (stA c0) := by rw [stA_eq]; exact field_aligned hca (by simp) (by simp) + have farA : ∀ u : U64, TcbAddr u → ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → K.val ≠ 3328 → + L.mem s1 (wa u K) = L.mem s (wa u K) := + fun u hu K h1 h2 h3 => mem_wr_ne' sA (field_aligned hu h1 h2) + (by rw [stA_eq]; exact field_ne (K2 := K) hca hu (by simp) h1 (fun _ => by simpa using Ne.symm h3)) + have hcur1 : L.curW s1 = c0 := specPtr_inj (J1.j.1.sch.curThread.symm.trans ct) + -- the flags word + have efl : L.mem s1 (tTflA t) = L.mem s (tTflA t) := by + unfold tTflA; exact farA t hta 3456#u64 (by simp) (by simp) (by simp) + set old := L.mem s (tTflA t) with hold + set nw : U64 := ⟨fl4TcbFlagsWord old.bv clear.bv set.bv⟩ with hnw + set s2 := L.wr s1 (tTflA t) nw with hs2 + have htf := f0_tflA J1.iplaced.e.tcb ht + have w2 : WritesIn st s1 s2 (fun b => b = tTflA t) := writesIn_wr htf.1 + have tcbW : ∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → K.val ≠ 3456 → + L.mem s2 (wa u K) = L.mem s1 (wa u K) := by + intro u hu K k1 k2 k3 + have hua : TcbAddr u := J1.sr.core.tcbs u hu + refine w2.mem_eq (field_aligned hua k1 k2) fun hb => ?_ + exact field_ne hua hta k1 (by simp) (fun _ => by simpa using k3) hb + -- the spec step + obtain ⟨y, hy, -⟩ := J1.mcp t ht + have hyfl : wordToTcbFlags old.bv = y.tcbFlags := by + by_cases e : specPtr t = σ.curThread + · have h0 : σ1.kheap (specPtr t) = some (.tcb { xc with tcbState := .restart }) := by + rw [e]; simp only [hσ1, stUpdA, setObjA, Function.update_self] + rw [hy] at h0; cases h0 + exact hfl xc (e ▸ hkc) + · have h0 : σ1.kheap (specPtr t) = σ.kheap (specPtr t) := by + simp only [hσ1, stUpdA, setObjA, Function.update_of_ne e] + exact hfl y (h0 ▸ hy) + set σu := updKSc σ1 (specPtr t) (.tcb (flagsTcbA y (fl4TcbFlagsWord old.bv clear.bv set.bv))) with hσu + have hspec := setFlags_specA (clear := clear.bv) (set := set.bv) hy hyfl (fun h => howner h) + rw [← hσu] at hspec + have key : ∀ q, σu.kheap q = if q = specPtr t then + some (.tcb (flagsTcbA y (fl4TcbFlagsWord old.bv clear.bv set.bv))) else σ1.kheap q := by + intro q + simp only [hσu, updKSc_kheap, Function.update_apply] + have keepO : ∀ q o, σ1.kheap q = some o → (∀ z, o ≠ .tcb z) → σu.kheap q = some o := fun q o h n1 => by + rw [key, if_neg (fun e => by rw [e, hy] at h; exact n1 y (Option.some.inj h).symm)] + exact h + have backO : ∀ q o, σu.kheap q = some o → (∀ z, o ≠ .tcb z) → σ1.kheap q = some o := fun q o h n1 => by + rw [key] at h + split at h + · exact absurd (Option.some.inj h).symm (n1 _) + · exact h + have fwdT : ∀ q z, σ1.kheap q = some (.tcb z) → ∃ v, σu.kheap q = some (.tcb v) ∧ + v = { z with tcbFlags := v.tcbFlags } := by + intro q z h + by_cases e : q = specPtr t + · rw [e, hy] at h + cases h + exact ⟨_, by rw [key, if_pos e], rfl⟩ + · exact ⟨z, by rw [key, if_neg e]; exact h, rfl⟩ + have bwdT : ∀ q v, σu.kheap q = some (.tcb v) → ∃ z, σ1.kheap q = some (.tcb z) ∧ + v = { z with tcbFlags := v.tcbFlags } := by + intro q v h + rw [key] at h + split at h + · rename_i e + cases h + exact ⟨y, e ▸ hy, rfl⟩ + · exact ⟨v, h, rfl⟩ + have hP := J1.iplaced + have hT : TcbPlaced Z.T := hP.e.tcb + have Fb : SchedFrameA σ1 σu := SchedFrameA.upd_tcb hy ⟨rfl, rfl, rfl⟩ + have wb : WritesIn st s1 s2 (SchedStepNWA (st := st) N E Rp Z m σ1 s1) := + w2.mono fun b hb => hb ▸ htf.2.stepN hT + have Kb : IpcKeepFA σ1 σu := + ⟨fun p x h => by + obtain ⟨v, hv, e⟩ := fwdT p x h + exact ⟨v, hv, by rw [e], by rw [e], by rw [e], by rw [e], by rw [e]⟩, + fun p z k h => ⟨z, k, keepO p _ h (fun _ e => by cases e), rfl⟩, + fun p z k h => ⟨z, k, backO p _ h (fun _ e => by cases e), rfl⟩⟩ + have srM : SchedSRA st Z m κ s2 σu := by + refine J1.sr.stable w2 (fun b hb => f0WordOffReadA J1.sr.toSchedSRN (hb ▸ htf.2)) ?_ ?_ + · refine + { fix := ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ + act := rfl + rq := rfl + rel := rfl + sc := fun q x k h => (keepO q _ h (fun _ e => by cases e)).trans h.symm + tcb := fun q x h => by + obtain ⟨v, hv, e⟩ := fwdT q x h + exact ⟨v, hv, by rw [e]; try rfl, by rw [e]; try rfl, by rw [e]; try rfl⟩ + scBack := fun q x k h => (backO q _ h (fun _ e => by cases e)).trans h.symm + tcbBack := fun q v h => by + obtain ⟨z, hz, -⟩ := bwdT q v h + exact ⟨z, hz⟩ + ntfnBack := fun q z h => ⟨z, backO q _ h (fun _ e => by cases e), rfl⟩ } + · intro q x z hx hz + obtain ⟨v, hv, e⟩ := fwdT q x hx + rw [hz] at hv + cases hv + refine Or.inl ⟨by rw [e], fun h => ?_⟩ + rw [e] at h + exact h + have off : ∀ b : U64, b.val ≠ t.val + 3456 → Aligned b → L.mem s2 b = L.mem s1 b := fun b h hb => + w2.mem_eq hb fun c => h (by + rw [c]; unfold tTflA; rw [Notif.tfield_val hta (by simp)]; simp) + have feE : FootEq Z.T E Rp Bf (L.mem s1) (L.mem s2) := by + intro b hb hf + refine off b (fun h => ?_) hb + have inT : t.val ≤ b.val ∧ b.val < t.val + 4096 := by omega + rcases hf with ⟨e, he, h1⟩ | ⟨r, hr, h1⟩ | ⟨u, hu, h1⟩ | h1 + · have := hP.e.et e he t ht; omega + · have := hP.e.rt r hr t ht; omega + · have hua := hP.e.tcb u hu + have a1 : t.val % 4096 = 0 := hta.align + have a2 : u.val % 4096 = 0 := hua.align + rcases h1 with h1 | h1 + · unfold Endp.TF TOffN at h1 + omega + · unfold TL at h1 + omega + · have := hP.e.bt _ h1 t ht; omega + have kvE : Endp.KView σ1 σu := by + refine ⟨fun q z h => keepO q _ h (fun _ e => by cases e), fun q z h => ⟨z, keepO q _ h (fun _ e => by cases e), rfl⟩, + fun q z h => ?_, rfl⟩ + obtain ⟨v, hv, e⟩ := fwdT q z h + exact ⟨v, hv, by rw [e], by rw [e], by rw [e], by rw [e], by rw [e]⟩ + have epM : EpStateRel Z.T E Rp Bf (L.mem s2) σu := J1.ep.frame hP.e feE kvE + have feN : Notif.FootEq N Z.T (L.mem s1) (L.mem s2) := by + intro b hb hf + refine off b (fun h => ?_) hb + rcases hf with ⟨q, hq, h1, h2⟩ | ⟨u, hu, h1⟩ + · have := hP.n.disj q hq t ht; omega + · have hua := hP.e.tcb u hu + have a1 : t.val % 4096 = 0 := hta.align + have a2 : u.val % 4096 = 0 := hua.align + have hv : b.val = u.val + 3328 ∨ b.val = u.val + 3336 ∨ b.val = u.val + 256 ∨ b.val = u.val + 3448 ∨ + b.val = u.val + 3496 ∨ b.val = u.val + 3504 := by + rcases h1 with h1 | h1 | h1 | h1 | h1 | h1 <;> + rw [h1] <;> + simp only [stA_eq, Notif.tBlockA_eq, Notif.badgeRegA_eq, Notif.prioA_eq', Notif.nextEp_eq, + Notif.prevEp_eq] <;> + rw [Notif.tfield_val hua] <;> simp + omega + have kvN : Notif.KView σ1 σu := by + refine ⟨fun q z h => keepO q _ h (fun _ e => by cases e), fun q z h => ?_⟩ + obtain ⟨v, hv, e⟩ := fwdT q z h + exact ⟨v, hv, by rw [e]; try rfl, by rw [e]; try rfl, by rw [e]; try rfl⟩ + have ntM : NtfnStateRel N Z.T (L.mem s2) σu := J1.ntfn.frame hP.n feN kvN + have rpM : ReplyStackRelA (L.mem s2) Rp Z.C σu (scNameA m) stack := by + refine J1.reply.frame (fun r hr => ?_) (fun c _ z k h => ⟨z, k, keepO _ _ h (fun _ e => by cases e), rfl⟩) + (fun r _ x' h => ⟨x', backO _ _ h (fun _ e => by cases e), rfl⟩) + have hr3 := hP.e.rep r hr + have hdis := hP.e.rt r hr t ht + have one : ∀ O : U64, O.val = 8 ∨ O.val = 16 → L.mem s2 (wa r O) = L.mem s1 (wa r O) := by + intro O hO + have hv : (wa r O).val = r.val + O.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact off _ (by omega) (by unfold Aligned; rw [hv]; omega) + exact ⟨one _ (Or.inr (by simp [layout.reply.NEXT])), one _ (Or.inl (by simp [layout.reply.PREV]))⟩ + have mcpM : McpRelA Z.T m (L.mem s2) σu := fun u hu => by + obtain ⟨z, hz, e, ef⟩ := J1.mcp u hu + obtain ⟨v, hv, ev⟩ := fwdT _ z hz + refine ⟨v, hv, ?_, fun g => ?_⟩ + · rw [ev] + unfold mcpA at e ⊢ + rw [tcbW u hu _ (by simp) (by simp) (by simp)] + exact e + · by_cases eu : u = t + · subst eu + rw [key, if_pos rfl] at hv + cases hv + show wordToTcbFlags (L.mem s2 (tTflA u)).bv = wordToTcbFlags (fl4TcbFlagsWord old.bv clear.bv set.bv) + rw [hs2, rd_wr_eq htf.1] + · have hne : specPtr u ≠ specPtr t := fun h => eu (specPtr_inj h) + rw [key, if_neg hne, hz] at hv + cases hv + refine (ef g).keep ?_ rfl + have hua : TcbAddr u := J1.sr.core.tcbs u hu + refine w2.mem_eq (field_aligned hua (by simp) (by simp)) fun hb => ?_ + exact field_ne hua hta (by simp) (by simp) (fun h => absurd h eu) hb + have ye : ∀ u ∈ Z.T, L.mem s2 (yieldA u) = L.mem s1 (yieldA u) := fun u hu => by + unfold yieldA; exact tcbW u hu _ (by simp) (by simp) (by simp) + have yM : YieldRelA Z.T Z.C m (L.mem s2) σu := by + refine ⟨fun u hu => ?_, fun u hu h => ?_, fun c' hc' z k h p hp => ?_⟩ + · obtain ⟨z, hz, e⟩ := J1.yieldRel.tcb u hu + obtain ⟨v, hv, ev⟩ := fwdT _ z hz + exact ⟨v, hv, by rw [ev, ye u hu]; exact e⟩ + · rw [ye u hu] at h ⊢ + obtain ⟨hC, z, k, hz, e⟩ := J1.yieldRel.toSc u hu h + exact ⟨hC, z, k, keepO _ _ hz (fun _ e => by cases e), e⟩ + · obtain ⟨u, hu, e1, e2⟩ := J1.yieldRel.frm c' hc' z k (backO _ _ h (fun _ e => by cases e)) p hp + exact ⟨u, hu, e1, by rw [ye u hu]; exact e2⟩ + have sinvM : SInvXA σu m n := by + refine J1.sinv.of_fr + { tcb := fun q x h => by + obtain ⟨v, hv, e⟩ := fwdT q x h + exact ⟨v, hv, by rw [e], by rw [e], by rw [e]⟩ + tcb' := fun q v h => by + obtain ⟨z, hz, e⟩ := bwdT q v h + exact ⟨z, hz, by rw [e], by rw [e], by rw [e]⟩ + sc := fun q z k => ⟨fun h => backO q _ h (fun _ e => by cases e), fun h => keepO q _ h (fun _ e => by cases e)⟩ + cur := rfl + idle := rfl + csc := rfl + rq := rfl + rls := rfl + act := rfl, time := rfl, cons := rfl } ?_ + exact J1.sinv.irqN.updTcbA hy rfl + have sidleM : SIdleA σu m n := + { f3 := fun c hc => by + obtain ⟨x, hx, hs⟩ := J1.sidle.f3 c hc + obtain ⟨v, hv, e⟩ := fwdT _ x hx + exact ⟨v, hv, by rw [e]; exact hs⟩ + f4 := fun z k h => J1.sidle.f4 z k (backO _ _ h (fun _ e => by cases e)) + f5 := fun q z k h => J1.sidle.f5 q z k (backO _ _ h (fun _ e => by cases e)) + f6 := J1.sidle.f6.of_eq fun sl => by + unfold specCapAtA + by_cases e : sl.1 = specPtr t + · rw [key, if_pos e, e, hy] + rfl + · rw [key, if_neg e] + f7 := fun y k h => J1.sidle.f7 y k (backO _ _ h (fun _ e => by cases e)) + idleSlot := fun c hc => by + have h0 := J1.sidle.idleSlot c hc + unfold vtCapA at h0 ⊢ + by_cases e : m.idleThreadOf c = specPtr t + · rw [key, if_pos e]; rw [e, hy] at h0; exact h0 + · rw [key, if_neg e]; exact h0 + idleBound := fun c hc => by + have h0 := J1.sidle.idleBound c hc + unfold vcpuOfA at h0 ⊢ + by_cases e : m.idleThreadOf c = specPtr t + · rw [key, if_pos e]; rw [e, hy] at h0; exact h0 + · rw [key, if_neg e]; exact h0 } + have rvM : RunVcpuInv st Z.T s2 := fun u hu h0 => by + have e1 : L.mem s2 (runVcpuA u) = L.mem s1 (runVcpuA u) := by + unfold runVcpuA + exact tcbW u hu 3672#u64 (by simp) (by simp) (by simp) + have e2 : L.mem s2 (Fl4ProofsA64.P1.stA u) = L.mem s1 (Fl4ProofsA64.P1.stA u) := by + rw [Fl4ProofsA64.P1.stA_eq] + exact tcbW u hu 3328#u64 (by simp) (by simp) (by simp) + rw [e2]; exact J1.runVcpu u hu (e1 ▸ h0) + have bqM : BlkQueuedA Z.T E N (L.mem s2) σu := by + refine J1.blkQ.keepA (fun u hu => ?_) (fun u _ x' h => ?_) (fun q z h => keepO q _ h (fun _ e => by cases e)) + (fun q z h => keepO q _ h (fun _ e => by cases e)) + · rw [Notif.tBlockA_eq]; exact tcbW u hu 3336#u64 (by simp) (by simp) (by simp) + · obtain ⟨z, hz, e⟩ := bwdT _ x' h + exact Or.inr ⟨z, hz, by rw [e]⟩ + have svM : SchedValidA σu := J1.schedValid.congr rfl rfl rfl rfl rfl rfl + (fun q z k h => keepO q _ h (fun _ e => by cases e)) + (fun u x h => by + obtain ⟨v, hv, e⟩ := fwdT u x h + exact ⟨v, hv, by rw [e]⟩) + (fun u v h => by + obtain ⟨z, hz, e⟩ := bwdT u v h + exact ⟨z, hz, by rw [e], by rw [e]⟩) + have blkM : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s2 := by + have htT : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + exact (plainW_all (v := nw) (BXI.blk J1.sys.ent.bxi) htT (K := 3456#u64) (by simp) (by simp) (by simp) + (by simp) (by simp) (by simp) (by simp)).1 + -- the current thread's facts at the mid state + have hcurU : L.curW s2 = c0 := by + have e1 := srM.sch.curThread + have e0 : σu.curThread = specPtr c0 := J1.j.1.sch.curThread.trans (congrArg specPtr hcur1) + exact specPtr_inj (e1.symm.trans e0) + have tw2 : ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → K.val ≠ 3328 → K.val ≠ 3456 → + L.mem s2 (wa c0 K) = L.mem s (wa c0 K) := + fun K h1 h2 h3 h4 => (tcbW c0 hcZ K h1 h2 h4).trans (farA c0 hca K h1 h2 h3) + have h10 : σ1.kheap σ1.curThread = some (.tcb { xc with tcbState := .restart }) := by + simp only [hσ1, stUpdA, setObjA, Function.update_self] + obtain ⟨xu, hxu, exu⟩ := fwdT _ _ h10 + have hx1 : σu.kheap σu.curThread = some (.tcb xu) := hxu + have hsc : L.mem s (scA c0) ≠ 0#u64 := RS.cur.2.1 + set w := L.mem s (scA c0) with hw + have hscU : L.mem s2 (scA c0) = w := tw2 3464#u64 (by simp) (by simp) (by simp) (by simp) + have hiqU : L.mem s2 (inqA c0) = L.mem s (inqA c0) := tw2 3592#u64 (by simp) (by simp) (by simp) (by simp) + have hwC : w ∈ Z.C := J.j.1.rest.scMem c0 hcZ hsc + have hscx : xc.tcbSchedContext = some (scRenA m w) := by + obtain ⟨z, hz, -, hsx⟩ := J.j.1.sch.tcb c0 hcZ + rw [← ct, hkc] at hz; cases hz + rw [hsx]; unfold optScA; rw [← hw, if_neg hsc] + have hpos : ∃ z k, σu.kheap (scRenA m w) = some (.schedContext z k) ∧ 0 < z.scRefillMax := by + have hbx := J.sys.ent.bxi + obtain ⟨-, -, -, -, hS⟩ := hbx + have hwH : w ∈ H.scs := by rw [J.scs]; exact hwC + have hrm : L.rmax s w ≠ 0#u64 := (hS.act w hwH).1 RS.cur.2.2 + obtain ⟨z, k, ez, ezr⟩ := J.j.1.rest.refillMax w hwC + have e3 : L.rmax s w = L.mem s (wa w 72#u64) := by unfold LawfulStore.rmax; rw [maxA_eqA] + rw [e3] at hrm + have hp0 : 0 < z.scRefillMax := by + rw [ezr]; exact Nat.pos_of_ne_zero (fun e => hrm (UScalar.eq_of_val_eq (by simpa using e))) + have ez1 : σ1.kheap (scRenA m w) = some (.schedContext z k) := by + simp only [hσ1, stUpdA, setObjA] + rw [Function.update_of_ne (fun e => by rw [e, hkc] at ez; cases ez)]; exact ez + exact ⟨z, k, keepO _ _ ez1 (fun _ e => by cases e), hp0⟩ + have hrq : σu.curThread ∉ σu.releaseQueue := by + show σ.curThread ∉ σ.releaseQueue + rw [ct, J.j.1.rest.rel]; intro hm + obtain ⟨q, hq, eq⟩ := List.mem_map.1 hm + have e' : q = c0 := specPtr_inj eq + subst e' + have haf := J.sq.inR _ J.sq.cur _ hq + exact ((J.sq.released _ (by rw [J.tcbs]; exact hcZ)).2 (by rw [haf]; exact hq)) RS.cur.1 + -- the program + have hr : tcb.restart_caller st s = ok s1 := by + unfold tcb.restart_caller + rw [cur_thread_ok, bind_ok, hst] + have hnwA : ((L.mem s1 (tTflA t) &&& ~~~clear) ||| (set &&& fl4_abi.consts.tcb_flag.MASK)) = nw := by + apply UScalar.eq_of_val_eq + rw [efl] + exact congrArg BitVec.toNat (tcbFlagsWord_eqA _ _ _) + have hbit : ((nw &&& fl4_abi.consts.tcb_flag.FPU_DISABLED) != 0#u64) = true ↔ + fl4TcbFlagsWord old.bv clear.bv set.bv &&& 1 ≠ 0 := by + have e1 : fl4_abi.consts.tcb_flag.FPU_DISABLED = 1#u64 := by + unfold fl4_abi.consts.tcb_flag.FPU_DISABLED; rfl + rw [e1, bne_iff_ne, Ne, Ne, UScalar.eq_equiv_bv_eq] + rfl + have harch : (if ((nw &&& fl4_abi.consts.tcb_flag.FPU_DISABLED) != 0#u64) = true then ar.fpu_release a t else ok a) = + ok a' := by + by_cases h : fl4TcbFlagsWord old.bv clear.bv set.bv &&& 1 ≠ 0 + · rw [if_pos (hbit.2 h)]; rwa [if_pos h] at hrel + · rw [if_neg (fun c => h (hbit.1 c))]; rwa [if_neg h] at hrel + have hprog : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + (do let s4 ← vspace.reply1 st s2 c0 call nw; ok (core.result.Result.Ok (), s4, a')) := by + rw [hcap, decodeTcb_programA, if_neg Bool.false_ne_true, hlbl] + have q := tcbSetFlags_exactA (st := st) (ar := ar) (ctx := ⟨len, buf, call, slot, extra⟩) (a' := a') + hstall hlen hr hclear hset (by rw [hnwA]; exact harch) + dsimp only at q + rw [q, efl, ← hnw, ← hs2, ← hc0, ← flagReply_reply1A hX s2 hca call] + obtain ⟨b0, hb0, -⟩ := spec_existsA (P0.msg.lookup_ipc_buffer.p0 st s2 true c0 L.total) + cases call <;> simp only [hb0, bind_tc_ok, bind_ok, Bool.false_eq_true, ↓reduceIte] + -- the reply + have Qb : QKeepA σ1 σu := QKeepA.ofBwd fun q o h => by + rw [key] at h + split at h + · rename_i e + cases h + exact ⟨.tcb y, by rw [e]; exact hy, QObjA.ofTcb rfl⟩ + · exact ⟨o, h, QObjA.refl o⟩ + have R := J1.replyOneMidA (Q0 := Z.Q) (R0 := Z.R) wb Fb Kb Qb + (fun u hu O h h8 => tcbW u hu O (by omega) h8 (by omega)) srM epM ntM rpM mcpM yM sinvM sidleM rvM bqM svM blkM + srM.sch.curThread hx1 (by rw [exu]) (by rw [hcurU, hscU]; exact hsc) (by rw [hcurU, hscU]; exact RS.cur.2.2) + (by rw [hcurU, hiqU]; exact RS.cur.1) hrq (by rw [hcurU, hscU]; exact hwC) + (by rw [hcurU, hscU, exu]; exact hscx) (by rw [hcurU, hscU]; exact hpos) call nw + (fun s' h2 σX => by + have hd : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (core.result.Result.Ok (), s', a') := by rw [hprog, ← hcurU, h2, bind_ok] + obtain ⟨-, -, sysl, symL, -, userOK, j7⟩ := J.decodeFinA hA hM hPl hfm hslot hcov + (rfl : capA st s slot = capA st s slot) hext hSC (lbl := lbl) + (by rw [hcap, hlbl]; simp [DecArmS1aEnt, DecArmQH, DecArmQZ2, DecArmKQ2, DecArmKQ, DecArmKQ3, DecArmDelQ, + DecArmS1aC, capFam]) + (fun _ h => by rw [hcap] at h; cases h) (fun _ h => by rw [hcap] at h; cases h) hd σX m + exact ⟨sysl, symL, userOK, j7⟩) + rw [hcurU] at R + obtain ⟨s3, e3, σ3, hrun, hsh, Q, Rl, J2⟩ := exists_of_specA R + have har : AR a' Z.hw := by + have h0 : AR a Z.hw := J.toJStage3A.j.1.ar + by_cases h : fl4TcbFlagsWord old.bv clear.bv set.bv &&& 1 ≠ 0 + · have q := hk.fpu_release a Z.hw t h0 + rw [if_pos h] at hrel + rw [hrel, WP.spec_ok] at q + exact q + · rw [if_neg h] at hrel + have : a = a' := ok_inj_ff hrel + exact this ▸ h0 + -- the flags word at the end + have hlast : ∀ z, σu.kheap (specPtr t) = some (.tcb z) → wordToTcbFlags (L.mem s3 (tTflA t)).bv = z.tcbFlags := by + intro z hz + rw [key, if_pos rfl] at hz + cases hz + have e : L.mem s3 (tTflA t) = nw := by + have hk2 : σu.kheap (specPtr c0) = some (.tcb xu) := by + rw [← hcurU, ← srM.sch.curThread]; exact hx1 + have V : TcbIpcViewsA st Z m κ N E Rp Bf stack s2 σu := ⟨⟨Z.Q, Z.R, srM⟩, ntM, epM, rpM, mcpM, yM⟩ + obtain ⟨sr2, er, -, wr2⟩ := reply1_regs_viewsA J1.iplaced J1.ipcPlace V hcZ hk2 call nw + have PE := J1.iplaced + have hbx := BlkOK.bxi blkM + obtain ⟨-, -, -, -, hS⟩ := hbx + obtain ⟨Q0', R0', q⟩ := blkM.1 + have hct : c0 ∈ H.tcbs := by rw [J.tcbs]; exact hcZ + have hwH : w ∈ H.scs := by rw [J.scs]; exact hwC + have hrm : L.rmax s2 w ≠ 0#u64 := (hS.act w hwH).1 RS.cur.2.2 + have hmΩ : maxA w ∈ H.omega := hS.sub _ (hS.all _ hwH).2.1 + have hma : Aligned (maxA w) := (q.omega _ hmΩ).1 + have far : ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → K.val ≠ 256 → K.val ≠ 264 → K.val ≠ 272 → + L.mem sr2 (wa c0 K) = L.mem s2 (wa c0 K) := by + intro K h1 h2 h3 h4 h5 + refine wr2.mem_eq (field_aligned hca h1 h2) fun h => ?_ + rcases h with h | h | h + · exact field_ne hca hca h1 (by simp) (fun _ => by simpa using h3) h + · exact field_ne hca hca h1 (by simp) (fun _ => by simpa using h4) h + · exact field_ne hca hca h1 (by simp) (fun _ => by simpa using h5) h + have hsc2 : L.mem sr2 (scA c0) = w := (far 3464#u64 (by simp) (by simp) (by simp) (by simp) (by simp)).trans hscU + have hiq2 : (L.mem sr2 (inqA c0) &&& sched.Q_RELEASE) = 0#u64 := by + have h := (far 3592#u64 (by simp) (by simp) (by simp) (by simp) (by simp)).trans hiqU + show (L.mem sr2 (wa c0 3592#u64) &&& sched.Q_RELEASE) = 0#u64 + rw [h]; exact RS.cur.1 + have hrm2 : L.rmax sr2 w ≠ 0#u64 := by + show L.mem sr2 (maxA w) ≠ 0#u64 + rw [wr2.mem_eq hma fun h => ?_] + · exact hrm + rcases h with h | h | h <;> + exact out_ne_field (q.omega _ hmΩ) hct hca (by simp) (congrArg UScalar.val h.symm) + have hmx : stA c0 ≠ maxA w := fun h => + out_ne_field (q.omega _ hmΩ) hct hca (K := 3328#u64) (by simp) + (by rw [← stA_eq]; exact congrArg UScalar.val h) + have eRun : sched.set_thread_state st sr2 c0 sched.TS_RUNNING = ok (L.wr sr2 (stA c0) sched.TS_RUNNING) := + set_thread_state_run_wrA hca (by rw [hsc2]; exact hsc) (by rw [hsc2]; exact hrm2) hiq2 + (by rw [hsc2]; exact hma) (by rw [hsc2]; exact hmx) + have es3 : s3 = L.wr sr2 (stA c0) sched.TS_RUNNING := by + have eProg : vspace.reply1 st s2 c0 call nw = ok (L.wr sr2 (stA c0) sched.TS_RUNNING) := by + unfold vspace.reply1 + rw [er, bind_ok, eRun] + exact ok_inj_ff (e3.symm.trans eProg) + have vfl : (tTflA t).val = t.val + 3456 := by + unfold tTflA; rw [Notif.tfield_val hta (by simp)]; simp + have a1 : t.val % 4096 = 0 := hta.align + have a2 : c0.val % 4096 = 0 := hca.align + have n1 : stA c0 ≠ tTflA t := fun h => by + have := congrArg UScalar.val h + rw [stA_eq, Notif.tfield_val hca (by simp), vfl] at this + simp at this; omega + have n2 : ¬ ScConsumedRegsA c0 (tTflA t) := by + intro h + have key : ∀ i : U64, i.val < 37 → tTflA t ≠ Endp.regA c0 i := fun i hi h => by + have := congrArg UScalar.val h + rw [Endp.regA_val hca hi, vfl] at this + omega + rcases h with h | h | h + · refine absurd h (fun h => ?_) + have := congrArg UScalar.val h + rw [Notif.tfield_val hca (by simp), vfl] at this + simp at this; omega + · have := congrArg UScalar.val h + rw [Notif.tfield_val hca (by simp), vfl] at this + simp at this; omega + · have := congrArg UScalar.val h + rw [Notif.tfield_val hca (by simp), vfl] at this + simp at this; omega + rw [es3, mem_wr_ne' sA htf.1 n1, wr2.mem_eq htf.1 n2, hs2] + exact L.mem_wr_same htf.1 + rw [e] + rfl + refine ⟨xc, σ1, σu, [nw.bv], σ3, s3, hkc, rfl, hsp, hspec, rfl, hrun, ?_, hlast, Q, Rl, + { J2 with j := ⟨{ J2.j.1 with ar := har }, J2.j.2⟩ }, Sched.BodyEndA.ofTcbReply J hkc hy hσu hsh⟩ + rw [hprog, e3, bind_ok] + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3TcbFpuA.lean b/proofs/Fl4ProofsA64/P3/Stage3TcbFpuA.lean new file mode 100644 index 000000000..3bab6d2e9 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3TcbFpuA.lean @@ -0,0 +1,350 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 3): **TCB WriteFPU at the record**. + +* **`tcb.copy_words_loop.rfA`**: `copy_words`' loop keeps any state predicate kept by a write of a destination word. +* **`fpuWriteSpecA`**: the spec's WriteFPU, when the target does not own the FPU, sets the target's FPU image to the + frame's words and changes nothing else. +* **`JStage3A.fpuWrite_stepA`**: the 65-word copy into the target's FPU image `[t + 1088, t + 1608)` with the spec's + image write keeps the record (`of_plainTcbA`). +* **`JSyscallA.writeFpuA`**: `decode_invocation` on a Thread cap with the WriteFPU label at a syscall entry. + +Named premises: [P] `ArchTotal`, `MaxSpi`, `ArchPlat`, `A64FrameMasks`, `hCs`; [P] `hflush`, `hsan`, `hrel` (the +architecture's flush, sanitise and reload calls) and `hk : TcbArchKeepsA ar AR` (they keep `AR`); [S] `h1` +(`numNodes = 1`, the remote stall), `howner` (the target does not own the FPU; known gap: the spec's FPU owner hand-over is not +lifted). The frame base's alignment (the spec's word loads assert it) comes from the record (J7 `FrameOK`, +`HeapOK.align`). The decode succeeds; the `Err` returns are not lifted. +(Lift 3d) The spec invocation names the frame at the spec's pointer `specPtr (ptr + offset)`, as the spec's decode of +the frame cap does (`base + offset` with `base = specPtr ptr`). +New module. +-/ +import Fl4ProofsA64.P3.Stage3TcbWordsA +import Fl4ProofsA64.P3.TcbInvokeFpuA +import Fl4ProofsA64.P3.TcbArchKeepsA +import Fl4ProofsA64.P3.FrontEndExtraA +import Fl4ProofsA64.P3.DelSpecA +import Fl4ProofsA64.P1.QueueTcbInv +import Fl4ProofsA64.P3.Stage3TcbBodyA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-- The byte size of a frame (public twin of the decoder's sizes). -/ +def fpuFrameBytesPubA : capability.FrameSize → Nat + | .Small => 4096 + | .Large => 2097152 + | .Huge => 1073741824 + +private theorem mapM_loadWordA (ms : MachineState) (f : Nat → Word) : + ∀ l : List Nat, (∀ i ∈ l, (f i).toNat % 8 = 0) → + Fl4Spec.NonDet.mapM (fun i => loadWord (f i)) l ms = + ({(l.map fun i => loadWordAtA ms.underlyingMemory (f i), ms)}, False) + | [], _ => rfl + | i :: l, h => by + show Fl4Spec.NonDet.bind (loadWord (f i)) (fun b => Fl4Spec.NonDet.bind + (Fl4Spec.NonDet.mapM (fun i => loadWord (f i)) l) fun bs => Fl4Spec.NonDet.pure (b :: bs)) ms = _ + rw [nd_bind_singleA' (loadWord_eqA ms _ (h i (List.mem_cons_self ..))), + nd_bind_singleA' (mapM_loadWordA ms f l fun j hj => h j (List.mem_cons_of_mem _ hj))] + rfl + +private theorem fr_alignA {fr : Word} (h : fr.toNat % 8 = 0) (k : Nat) : + (fr + BitVec.ofNat 64 (8 * k)).toNat % 8 = 0 := by + rw [BitVec.toNat_add, BitVec.toNat_ofNat] + omega + +/-- The spec's frame read is one image, the state unchanged. -/ +theorem fpuFromFrame_eqA (σ : State Unit) {fr : Word} (h : fr.toNat % 8 = 0) : + ∃ img, doMachineOp (Z := Unit) (fl4FpuFromFrame fr) σ = ({(img, σ)}, False) := by + have hm : ∃ img, fl4FpuFromFrame fr σ.machineState = ({(img, σ.machineState)}, False) := by + unfold fl4FpuFromFrame + rw [nd_bind_singleA (mapM_loadWordA σ.machineState (fun i => fr + BitVec.ofNat 64 (8 * i)) _ + fun i _ => fr_alignA h i)] + rw [nd_bind_singleA (loadWord_eqA _ _ (by simpa using fr_alignA h 64))] + exact ⟨_, rfl⟩ + obtain ⟨img, hi⟩ := hm + refine ⟨img, ?_⟩ + unfold doMachineOp + rw [nd_gets_bindA, hi, nd_bind_singleA (selectF_oneA _ σ)] + dsimp only + rw [nd_modify_bindA] + rfl + +/-- The spec's TCB after the FPU image write. -/ +def fpuWriteTcbA (y : Tcb) (img : FpuState) : Tcb := + { y with tcbArch := archTcbContextSet ⟨img, y.tcbArch.tcbContext.userRegs⟩ y.tcbArch } + +/-- **The spec's WriteFPU when the target does not own the FPU** (module docstring). -/ +theorem fpuWriteSpecA {σ : State Unit} {t : ObjRef} {y : Tcb} (hk : σ.kheap t = some (.tcb y)) + (howner : σ.archState.armCurrentFpuOwner ≠ some t) {fr : Word} (hfr : fr.toNat % 8 = 0) : + ∃ img, (.ok [], updKSc σ t (.tcb (fpuWriteTcbA y img))) ∈ ((invokeFl4Tcb (.fpuTransfer t false fr)).run σ).1 := by + obtain ⟨img, himg⟩ := fpuFromFrame_eqA σ hfr + refine ⟨img, ?_⟩ + have hfl : ((), σ) ∈ (fl4FpuFlush (Z := Unit) t σ).1 := by + unfold fl4FpuFlush + rw [nd_gets_bindA] + show ((), σ) ∈ (Fl4Spec.NonDet.when (σ.archState.armCurrentFpuOwner = some t) (saveFpuState t) σ).1 + unfold Fl4Spec.NonDet.when + rw [if_neg howner] + exact rfl + have hus : ((), updKSc σ t (.tcb (fpuWriteTcbA y img))) ∈ (asUser (Z := Unit) t (setFPUState img) σ).1 := by + refine ⟨(y, σ), ?_, (archTcbContextGet y.tcbArch, σ), rfl, + (((), ⟨img, y.tcbArch.tcbContext.userRegs⟩), σ), ⟨rfl, rfl⟩, (fpuWriteTcbA y img, σ), rfl, ((), _), ?_, rfl⟩ + · rw [Notif.getTcb_eq hk]; exact rfl + · rw [setObject_eqSc (o' := .tcb (fpuWriteTcbA y img)) hk rfl]; exact rfl + have how : ((), updKSc σ t (.tcb (fpuWriteTcbA y img))) ∈ (Fl4Spec.NonDet.when + ((updKSc σ t (.tcb (fpuWriteTcbA y img))).archState.armCurrentFpuOwner = some t) (setArmCurrentFpuOwner none) + (updKSc σ t (.tcb (fpuWriteTcbA y img)))).1 := by + unfold Fl4Spec.NonDet.when + have h' : ¬ (updKSc σ t (.tcb (fpuWriteTcbA y img))).archState.armCurrentFpuOwner = some t := howner + rw [if_neg h'] + exact rfl + apply mem_liftED + refine mem_bind_ndD (x := ((), σ)) hfl ?_ + simp only [Bool.false_eq_true, ↓reduceIte] + refine mem_bind_ndD (x := (img, σ)) (by rw [himg]; exact rfl) ?_ + refine mem_bind_ndD (x := ((), updKSc σ t (.tcb (fpuWriteTcbA y img)))) hus ?_ + refine mem_bind_ndD (x := ((updKSc σ t (.tcb (fpuWriteTcbA y img))).archState.armCurrentFpuOwner, + updKSc σ t (.tcb (fpuWriteTcbA y img)))) rfl ?_ + refine mem_bind_ndD (x := ((), updKSc σ t (.tcb (fpuWriteTcbA y img)))) how ?_ + exact rfl + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s : S} {a : A} {σ : State Unit} {m : McState} + +/-- **`copy_words`' loop keeps a predicate kept by a destination write** (module docstring). -/ +theorem tcb.copy_words_loop.rfA {R : S → Prop} {src dst k i : U64} (hfit : dst.val + 8 * k.val ≤ 2 ^ 64) + (hR : ∀ (q : S) (b v : U64), (∃ j : Nat, j < k.val ∧ b.val = dst.val + 8 * j) → R q → R (L.wr q b v)) + (h : R s) : tcb.copy_words_loop st s src dst k i ⦃ s' => R s' ⦄ := by + unfold tcb.copy_words_loop + apply loop.spec_decr_nat (measure := fun x => k.val - x.2.val) (inv := fun x => R x.1) + · rintro ⟨s1, j⟩ w1 + simp only at w1 + dsimp only + unfold tcb.copy_words_loop.body + split + · rename_i hi + have hi' : j.val < k.val := by simpa using hi + step* + have hv : i4.val = dst.val + 8 * j.val := by + rw [i4_post, i3_post] + simp [core.num.U64.wrapping_add_val_eq, core.num.U64.wrapping_mul_val_eq, U64.size_def, U64.numBits] + omega + subst s1_post + refine ⟨hR _ _ _ ⟨j.val, hi', hv⟩ w1, ?_⟩ + omega + · exact (WP.spec_ok _).2 w1 + · exact h + +set_option maxHeartbeats 2000000 in +/-- **WriteFPU's copy at the record** (module docstring). -/ +theorem JStage3A.fpuWrite_stepA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {t src : U64} (ht : t ∈ Z.T) {y : Tcb} (hy : σ.kheap (specPtr t) = some (.tcb y)) (img : FpuState) : + let s' := tcbFpuCopyA st s src (wa t layout.tcb.FPU) + tcb.copy_words st s src (wa t layout.tcb.FPU) 65#u64 = ok s' ∧ + BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s' ∧ + (∀ a' : A, AR a' Z.hw → SysL st H Z.Cs OS ia ie s' → SymL st H Z.Cs s' → + Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s' → + J7A st (J7R2A st ar nA) Z H OS s' (updKSc σ (specPtr t) (.tcb (fpuWriteTcbA y img))) m → + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) + s' a' (updKSc σ (specPtr t) (.tcb (fpuWriteTcbA y img))) m) := by + intro s' + have hP := J.iplaced + have htT : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + have hta : TcbAddr t := J.sr.core.tcbs t ht + have vF : (wa t layout.tcb.FPU).val = t.val + 1088 := by + rw [Notif.tfield_val hta (by simp)]; simp + have aF : Aligned (wa t layout.tcb.FPU) := field_aligned hta (by simp) (by simp) + have hfit : (wa t layout.tcb.FPU).val + fl4FpuStateSize ≤ 2 ^ 64 := by + have := hta.fits; rw [vF]; simp [fl4FpuStateSize]; omega + obtain ⟨ecp, wr⟩ := tcbFpuCopy_exactA (st := st) s src (wa t layout.tcb.FPU) aF hfit + have blk' : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s' := by + have h := tcb.copy_words_loop.rfA (st := st) (s := s) (src := src) (dst := wa t layout.tcb.FPU) (k := 65#u64) + (i := 0#u64) (R := fun q => BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac q) + (by rw [vF]; have := hta.fits; simp; omega) (fun q b v ⟨j, hj, hb⟩ hq => by + have hj' : j < 65 := by simpa using hj + have hK : (⟨BitVec.ofNat 64 (1088 + 8 * j)⟩ : U64).val = 1088 + 8 * j := by + show (BitVec.ofNat 64 (1088 + 8 * j)).toNat = _ + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + have eb : b = wa t ⟨BitVec.ofNat 64 (1088 + 8 * j)⟩ := by + apply UScalar.eq_of_val_eq + rw [Notif.tfield_val hta (by rw [hK]; omega), hK, hb, vF]; ring + rw [eb] + exact (plainW_all (v := v) hq htT (by rw [hK]; omega) (by rw [hK]; omega) (by rw [hK]; omega) + (by rw [hK]; omega) (by rw [hK]; omega) (by rw [hK]; omega) (by rw [hK]; omega)).1) + (BXI.blk J.sys.ent.bxi) + change tcb.copy_words st s src (wa t layout.tcb.FPU) 65#u64 ⦃ q => _ ⦄ at h + rw [ecp, WP.spec_ok] at h + exact h + refine ⟨ecp, blk', fun a' har sysl symL userOK j7 => ?_⟩ + have rng : ∀ b, CWW (wa t layout.tcb.FPU) 65#u64 b → t.val + 1088 ≤ b.val ∧ b.val < t.val + 1608 := by + rintro b ⟨j, hj, hb⟩ + have hj' : j < 65 := by simpa using hj + rw [hb, vF]; omega + have w : WritesIn st s s' (PlainRegW Z.T) := wr.mono fun b hb => ⟨t, ht, by have := rng b hb; omega, + by have := rng b hb; omega⟩ + have off : ∀ b : U64, Aligned b → (b.val < t.val + 1088 ∨ t.val + 1608 ≤ b.val) → L.mem s' b = L.mem s b := + fun b hb h => wr.mem_eq hb fun c => by have := rng b c; omega + set σ' := updKSc σ (specPtr t) (.tcb (fpuWriteTcbA y img)) with hσ' + have key : ∀ q, σ'.kheap q = if q = specPtr t then some (.tcb (fpuWriteTcbA y img)) else σ.kheap q := by + intro q + simp only [hσ', updKSc_kheap, Function.update_apply] + have feE : FootEq Z.T E Rp Bf (L.mem s) (L.mem s') := by + intro b hb hf + refine off b hb ?_ + by_contra h + have inT : t.val ≤ b.val ∧ b.val < t.val + 4096 := by omega + rcases hf with ⟨e, he, h1⟩ | ⟨r, hr, h1⟩ | ⟨u, hu, h1⟩ | h1 + · have := hP.e.et e he t ht; omega + · have := hP.e.rt r hr t ht; omega + · have hua := hP.e.tcb u hu + have a1 : t.val % 4096 = 0 := hta.align + have a2 : u.val % 4096 = 0 := hua.align + rcases h1 with h1 | h1 + · unfold Endp.TF TOffN at h1 + omega + · unfold TL at h1 + omega + · have := hP.e.bt _ h1 t ht; omega + have kvE : Endp.KView σ σ' := by + refine ⟨fun q z h => ?_, fun q z h => ?_, fun q z h => ?_, rfl⟩ + · rw [key, if_neg (fun e => by rw [e, hy] at h; cases h)]; exact h + · exact ⟨z, by rw [key, if_neg (fun e => by rw [e, hy] at h; cases h)]; exact h, rfl⟩ + · by_cases e : q = specPtr t + · rw [e, hy] at h + cases h + exact ⟨fpuWriteTcbA y img, by rw [key, if_pos e], rfl, rfl, rfl, rfl, rfl⟩ + · exact ⟨z, by rw [key, if_neg e]; exact h, rfl, rfl, rfl, rfl, rfl⟩ + have ep : EpStateRel Z.T E Rp Bf (L.mem s') σ' := J.ep.frame hP.e feE kvE + have feN : Notif.FootEq N Z.T (L.mem s) (L.mem s') := by + intro b hb hf + refine off b hb ?_ + by_contra h + rcases hf with ⟨q, hq, h1, h2⟩ | ⟨u, hu, h1⟩ + · have := hP.n.disj q hq t ht; omega + · have hua := hP.e.tcb u hu + have a1 : t.val % 4096 = 0 := hta.align + have a2 : u.val % 4096 = 0 := hua.align + have hv : b.val = u.val + 3328 ∨ b.val = u.val + 3336 ∨ b.val = u.val + 256 ∨ b.val = u.val + 3448 ∨ + b.val = u.val + 3496 ∨ b.val = u.val + 3504 := by + rcases h1 with h1 | h1 | h1 | h1 | h1 | h1 <;> + rw [h1] <;> + simp only [stA_eq, Notif.tBlockA_eq, Notif.badgeRegA_eq, Notif.prioA_eq', Notif.nextEp_eq, + Notif.prevEp_eq] <;> + rw [Notif.tfield_val hua] <;> simp + omega + have kvN : Notif.KView σ σ' := by + refine ⟨fun q z h => ?_, fun q z h => ?_⟩ + · rw [key, if_neg (fun e => by rw [e, hy] at h; cases h)]; exact h + · by_cases e : q = specPtr t + · rw [e, hy] at h + cases h + exact ⟨fpuWriteTcbA y img, by rw [key, if_pos e], rfl, rfl, rfl⟩ + · exact ⟨z, by rw [key, if_neg e]; exact h, rfl, rfl, rfl⟩ + have ntfn : NtfnStateRel N Z.T (L.mem s') σ' := J.ntfn.frame hP.n feN kvN + have R := J.of_plainTcbA ht hy (fpuWriteTcbA y img).tcbArch rfl w blk' sysl symL userOK j7 ep ntfn + exact { R with j := ⟨{ R.j.1 with ar := har }, R.j.2⟩ } + +set_option maxHeartbeats 2000000 in +/-- **TCB WriteFPU at a syscall entry** (module docstring). -/ +theorem JSyscallA.writeFpuA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + {slot lbl len buf t fs offset ptr rights asid va : U64} {size : capability.FrameSize} {mapped stage2 : Bool} + {aF aS a' : A} {blk call don : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Thread t) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hlbl : lbl = fl4_abi.label.TCBWriteFPU) + (he : tcb.extra ⟨len, buf, call, slot, extra⟩ 0#usize = ok (some fs)) (hlen : len ≥ 1#u64) + (hoff : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 0#u64 = ok offset) + (hfcap : capA st s fs = .Frame ptr size rights false mapped stage2 asid va) + (hread : rights ≠ capability.VM_KERNEL_ONLY) (halign : offset.val % 64 = 0) + (hroom : offset.val + 520 ≤ fpuFrameBytesPubA size) + (hflush : ar.fpu_flush a t = ok aF) (hsan : ar.fpu_sanitise aF t = ok aS) (hrel : ar.fpu_reload aS t = ok a') + (hk : TcbArchKeepsA ar AR) + (howner : σ.archState.armCurrentFpuOwner ≠ some (specPtr t)) (hfs : fs ∈ OS) : + ∃ xc σ1 σ2 s2, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + (.ok [], σ2) ∈ ((invokeFl4Tcb (.fpuTransfer (specPtr t) false (specPtr (wa ptr offset)))).run σ1).1 ∧ + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (.Ok (), s2, a') ∧ + ∃ Q Rl, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s2 a' σ2 m ∧ Sched.BodyEndA σ σ2 m := by + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hslot hcov J.actW J.running J.cur + have hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM := J.toJStage3EntryA.curNotVmA + obtain ⟨xc, hkc, hst, hsp, J1, -, -, -⟩ := J.toJStage3A.of_restartCurA RS.pl.choose_spec.choose_spec.choose_spec.choose_spec + RS.sym RS.run hnvm RS.cur RS.vs RS.vc + have ht : t ∈ Z.T := J.tcbs ▸ (J.toJStage3A.slotCapTy hslot).1.2.2.2.2.2 t hcap + have T0 := J.toJStage3A.toTA J.sact J.noSwitch + have hstall : smp.remote_tcb_stall st ar s a t = ok (s, a) := T0.stall_singleA hCs h1 ht + set s1 := L.wr s (stA (L.curW s)) sched.TS_RESTART with hs1 + set σ1 := stUpdA σ σ.curThread xc .restart with hσ1 + have hr : tcb.restart_caller st s = ok s1 := by + unfold tcb.restart_caller + rw [cur_thread_ok, bind_ok, hst] + obtain ⟨y, hy, -⟩ := J1.mcp t ht + have hta : TcbAddr t := J.sr.core.tcbs t ht + have hpa : ptr.val % 8 = 0 := by + obtain ⟨Pt, Ub, -, -, -, cc, -⟩ := J.toJStage3A.j7 + have hfo := cc.fo fs hfs ptr size rights mapped stage2 asid va hfcap + have key : ∀ b : U64, size.bits = ok b → 3 ≤ b.val → ptr.val % 8 = 0 := fun b hb h3 => by + have h : ptr.val % 2 ^ b.val = 0 := J.sys.ent.heap.align ptr _ (hfo b hb) + exact Nat.mod_eq_zero_of_dvd ((Nat.pow_dvd_pow 2 h3 : 2 ^ 3 ∣ 2 ^ b.val).trans (Nat.dvd_of_mod_eq_zero h)) + cases size + · exact key 12#u64 rfl (by decide) + · exact key 21#u64 rfl (by decide) + · exact key 30#u64 rfl (by decide) + have hfr : (specPtr (wa ptr offset)).toNat % 8 = 0 := by + have h8 : (wa ptr offset).bv.toNat % 8 = 0 := by + show (wa ptr offset).val % 8 = 0 + rw [wa_valm, Nat.mod_mod_of_dvd _ (⟨2 ^ 61, by norm_num⟩ : 8 ∣ 2 ^ 64)] + omega + have e : (0x0000808000000000#64 : BitVec 64).toNat = 0x0000808000000000 := by decide + rw [specPtr_def, BitVec.toNat_add, pptrShiftA_eq, e] + omega + have hown1 : σ1.archState.armCurrentFpuOwner ≠ some (specPtr t) := howner + obtain ⟨img, hspec⟩ := fpuWriteSpecA hy hown1 hfr + obtain ⟨ecp, -, K⟩ := J1.fpuWrite_stepA (src := wa ptr offset) ht hy img + have hdec : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (.Ok (), tcbFpuCopyA st s1 (wa ptr offset) (wa t layout.tcb.FPU), a') := by + rw [hcap, decodeTcb_programA, if_neg Bool.false_ne_true, hlbl, tcbWriteFPU_programA hstall] + have q := tcbFpuTransfer_exactA (st := st) (ar := ar) (toFrame := false) he hlen hoff (by rw [capA_eq, hfcap]) + hread (fun h => by cases h) halign (by cases size <;> exact hroom) hr hflush (fun _ => hsan) (fun _ => hrel) + (by + simp only [Bool.false_eq_true, ↓reduceIte] + exact ⟨field_aligned hta (by simp) (by simp), by + rw [Notif.tfield_val hta (by simp)]; have := hta.fits; simp [fl4FpuStateSize]; omega⟩) + simp only [Bool.false_eq_true, ↓reduceIte] at q + exact q + have har : AR a' Z.hw := by + have h0 := hk.fpu_flush a Z.hw t J.toJStage3A.j.1.ar + rw [hflush, WP.spec_ok] at h0 + have h2 := hk.fpu_sanitise aF Z.hw t h0 + rw [hsan, WP.spec_ok] at h2 + have h3 := hk.fpu_reload aS Z.hw t h2 + rw [hrel, WP.spec_ok] at h3 + exact h3 + obtain ⟨-, -, sysl, symL, -, userOK, j7⟩ := J.decodeFinA hA hM hPl hfm hslot hcov + (rfl : capA st s slot = capA st s slot) hext hSC (lbl := lbl) + (by rw [hcap, hlbl]; simp [DecArmS1aEnt, DecArmQH, DecArmQZ2, DecArmKQ2, DecArmKQ, DecArmKQ3, DecArmDelQ, + DecArmS1aC, capFam]) + (fun _ h => by rw [hcap] at h; cases h) (fun _ h => by rw [hcap] at h; cases h) hdec + (updKSc σ1 (specPtr t) (.tcb (fpuWriteTcbA y img))) m + have J2 := K a' har sysl symL userOK j7 + exact ⟨xc, σ1, _, _, hkc, rfl, hsp, hspec, hdec, Z.Q, Z.R, J2, Sched.BodyEndA.ofTcbUpd J hkc hy rfl⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3TcbFpuRdA.lean b/proofs/Fl4ProofsA64/P3/Stage3TcbFpuRdA.lean new file mode 100644 index 000000000..5f0b3f5e2 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3TcbFpuRdA.lean @@ -0,0 +1,670 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 3d): **TCB ReadFPU at the record**. + +* **`fpuToFrameSpecA`**: the spec's frame store changes user memory only, and only in the 520 bytes at the frame. +* **`fpuReadSpecA`**: the spec's ReadFPU, when the target does not own the FPU, changes user memory only (there). +* **`utVsPt_j7A`**: the record's installed pools and reachable tables are among J7's page-table pages (both typings + read the same ASID table, so reachability coincides). +* **`JStage3A.fpuRead_stepA`**: the 65-word copy of the target's FPU image into a frame, with the spec's user-memory + write, keeps the record. The written words are user words (J7's `Ub`, through `FrUb`): off the VSpace words, the + table pages, the VCPU words (`VrOK`), the kernel-state block (`S1aArmCol.kb`), the `OS` slots and nodes (`SysL.fo`) + and every typed heap object (`HeapOK.disj`). +* **`JSyscallA.readFpuA`**: `decode_invocation` on a Thread cap with the ReadFPU label at a syscall entry. + +Named premises: [P] `ArchTotal`, `MaxSpi`, `ArchPlat`, `A64FrameMasks`, `hCs`; [P] `hflush` (the architecture's flush +call) and `hk : TcbArchKeepsA ar AR`; [S] `h1` (`numNodes = 1`, the remote stall), `howner` (the target does not own +the FPU; known gap: the spec's FPU owner hand-over is not lifted), and **[S] `hbuf`** (the destination frame holds no +word of a tracked IPC buffer). + +**Known scope limit (`hbuf`)**: the record relates the words of a tracked IPC buffer to the spec's user memory +(`EpStateRel.buf`) but has no relation between FL4's FPU image of a thread and the spec's `userFpuState`, so the values +the copy writes are not known to agree with the spec's. A ReadFPU into a frame holding a tracked buffer is therefore +not lifted (Architect ruling: option (a)). The decode succeeds; the `Err` returns are not lifted. New module. +-/ +import Fl4ProofsA64.P3.Stage3TcbFpuA +import Fl4ProofsA64.P3.Stage3BufWrA +import Fl4ProofsA64.P3.EpBufA +import Fl4ProofsA64.P3.UtZeroSpecA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-- The byte offsets `[0, 520)` of `x` from `fr` that the spec's FPU frame store writes. -/ +def FpuByteA (fr x : Fl4SpecA64.Word) : Prop := ∃ k : Nat, k < 520 ∧ x = fr + BitVec.ofNat 64 k + +private theorem mapMx_storesA {α : Type} (f v : α → Fl4SpecA64.Word) : + ∀ (l : List α), (∀ i ∈ l, (f i).toNat % 8 = 0) → ∀ ms : MachineState, + ∃ um', Fl4Spec.NonDet.mapMx (fun i => storeWord (f i) (v i)) l ms = + ({((), { ms with underlyingMemory := um' })}, False) ∧ + ∀ x, (∀ i ∈ l, ∀ y : Fl4SpecA64.Word, y.toNat < 8 → x ≠ f i + y) → um' x = ms.underlyingMemory x + | [], _, ms => ⟨ms.underlyingMemory, rfl, fun _ _ => rfl⟩ + | i :: l, h, ms => by + have hi := h i (List.mem_cons_self ..) + obtain ⟨um', e, k⟩ := mapMx_storesA f v l (fun j hj => h j (List.mem_cons_of_mem _ hj)) + { ms with underlyingMemory := storeMem ms.underlyingMemory (f i) (v i) } + refine ⟨um', ?_, fun x hx => ?_⟩ + · show (storeWord (f i) (v i) >>= fun _ => Fl4Spec.NonDet.mapMx (fun i => storeWord (f i) (v i)) l) ms = _ + rw [nd_bind_oneA (storeWord_eq ms (f i) (v i) hi)] + exact e + · rw [k x fun j hj => hx j (List.mem_cons_of_mem _ hj)] + exact storeMem_other _ _ _ _ (hx i (List.mem_cons_self ..)) + +/-- **The spec's frame store** (module docstring). -/ +theorem fpuToFrameSpecA (ms : MachineState) {fr : Fl4SpecA64.Word} (hfr : fr.toNat % 8 = 0) (img : FpuState) : + ∃ um', fl4FpuToFrame fr img ms = ({((), { ms with underlyingMemory := um' })}, False) ∧ + ∀ x, ¬ FpuByteA fr x → um' x = ms.underlyingMemory x := by + have al : ∀ k : Nat, (fr + BitVec.ofNat 64 (8 * k)).toNat % 8 = 0 := fun k => by + rw [BitVec.toNat_add, BitVec.toNat_ofNat]; omega + have off : ∀ (k : Nat) (x y : Fl4SpecA64.Word), 8 * k + 8 ≤ 520 → y.toNat < 8 → + x = fr + BitVec.ofNat 64 (8 * k) + y → FpuByteA fr x := by + intro k x y hk hy e + refine ⟨8 * k + y.toNat, by omega, ?_⟩ + rw [e] + have ey : y = BitVec.ofNat 64 y.toNat := by rw [BitVec.ofNat_toNat, BitVec.setWidth_eq] + conv_lhs => rw [ey] + rw [BitVec.add_assoc, ← BitVec.ofNat_add] + obtain ⟨um1, e1, k1⟩ := mapMx_storesA (fun i : Fin fpuRegsLen => fr + BitVec.ofNat 64 (8 * i.val)) + (fun i => img.fpuRegs i) (List.finRange fpuRegsLen) (fun i _ => al i.val) ms + have a512 : (fr + 512 : Fl4SpecA64.Word).toNat % 8 = 0 := by simpa using al 64 + refine ⟨storeMem um1 (fr + 512) (BitVec.ofNat 64 (img.fpuSr.toNat + img.fpuCr.toNat * 2 ^ 32)), ?_, fun x hx => ?_⟩ + · unfold fl4FpuToFrame + rw [nd_bind_oneA e1] + exact storeWord_eq _ _ _ a512 + · rw [storeMem_other _ _ _ _ (fun y hy e => hx (off 64 x y (by omega) hy (by simpa using e)))] + refine k1 x fun i _ y hy e => hx (off i.val x y ?_ hy e) + have : i.val < 64 := i.isLt + omega + +/-- **The spec's ReadFPU when the target does not own the FPU** (module docstring). -/ +theorem fpuReadSpecA {σ : State Unit} {t : ObjRef} {y : Tcb} (hk : σ.kheap t = some (.tcb y)) + (howner : σ.archState.armCurrentFpuOwner ≠ some t) {fr : Fl4SpecA64.Word} (hfr : fr.toNat % 8 = 0) : + ∃ um', (.ok [], setUm σ um') ∈ ((invokeFl4Tcb (.fpuTransfer t true fr)).run σ).1 ∧ + ∀ x, ¬ FpuByteA fr x → um' x = σ.machineState.underlyingMemory x := by + obtain ⟨img, himg⟩ : ∃ img : FpuState, img = y.tcbArch.tcbContext.userFpuState := ⟨_, rfl⟩ + obtain ⟨um', hm, keep⟩ := fpuToFrameSpecA σ.machineState hfr img + refine ⟨um', ?_, keep⟩ + have hfl : ((), σ) ∈ (fl4FpuFlush (Z := Unit) t σ).1 := by + unfold fl4FpuFlush + rw [nd_gets_bindA] + show ((), σ) ∈ (Fl4Spec.NonDet.when (σ.archState.armCurrentFpuOwner = some t) (saveFpuState t) σ).1 + unfold Fl4Spec.NonDet.when + rw [if_neg howner] + exact rfl + have same : updKSc σ t (.tcb y) = σ := by + unfold updKSc; rw [← hk, Function.update_eq_self] + have hus : (img, σ) ∈ (asUser (Z := Unit) t getFPUState σ).1 := by + refine ⟨(y, σ), ?_, (archTcbContextGet y.tcbArch, σ), rfl, + ((img, archTcbContextGet y.tcbArch), σ), ⟨by rw [himg]; rfl, rfl⟩, (y, σ), rfl, ((), σ), ?_, rfl⟩ + · rw [Notif.getTcb_eq hk]; exact rfl + · have e := setObject_eqSc (o' := .tcb y) hk rfl + change ((), σ) ∈ (setObject t (.tcb y) σ).1 + rw [e, same]; exact rfl + have hmo : ((), setUm σ um') ∈ (doMachineOp (fl4FpuToFrame fr img) σ).1 := by + unfold doMachineOp + rw [nd_gets_bindA, hm, nd_bind_singleA (selectF_oneA _ σ)] + dsimp only + rw [nd_modify_bindA] + exact rfl + apply mem_liftED + refine mem_bind_ndD (x := ((), σ)) hfl ?_ + simp only [↓reduceIte] + refine mem_bind_ndD (x := (img, σ)) hus ?_ + exact mem_bind_ndD (x := ((), setUm σ um')) hmo rfl + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + +/-- **The record's tables are J7's** (module docstring). -/ +theorem utVsPt_j7A {Pp Tb Pp' Tb' Pt : Set U64} {Lv Lv' : U64 → Nat} {kb : U64} {s : S} + (V : VsTyR st kb (PoolSet st Pp s) ∅ Tb Lv s) (V' : VsTyR st kb (PoolSet st Pp' s) ∅ Tb' Lv' s) + (hkb : kb = L.ksb s) (hp : PoolSet st Pp' s ⊆ Pt) (ht : Tb' ⊆ Pt) : + utVsPtA st Pp Tb Lv s ⊆ Pt := by + have pools : PoolSet st Pp s ⊆ PoolSet st Pp' s := by + rintro p ⟨-, o, h1, h2, h8, e, hne⟩ + have h := V'.tbl o h1 h2 h8 (by rw [hkb, e]; exact hne) + rwa [hkb, e] at h + have reach : ∀ p, VsReach st (PoolSet st Pp s) ∅ Lv s p → + p ∈ Tb ∧ p ∈ Tb' ∧ VsReach st (PoolSet st Pp' s) ∅ Lv' s p ∧ Lv' p = Lv p := by + intro p h + induction h with + | hr h => exact absurd h (Set.notMem_empty _) + | @root0 p o hpP ho h8 hne htag => + have a := (V.pool p hpP o ho h8 hne).1 htag + have b := (V'.pool p (pools hpP) o ho h8 hne).1 htag + exact ⟨a.1, b.1, .root0 (pools hpP) ho h8 hne htag, b.2.trans a.2.symm⟩ + | @root2 p o hpP ho h8 htag => + have hne : L.mem s (wa p o) ≠ 0#u64 := fun e => by + rw [e] at htag + have h0 : ((0#u64 : U64) &&& 3#u64) = 0#u64 := by decide + rw [h0] at htag + exact absurd (congrArg UScalar.val htag) (by decide) + have a := (V.pool p hpP o ho h8 hne).2 htag + have b := (V'.pool p (pools hpP) o ho h8 hne).2 htag + exact ⟨a.1, b.1, .root2 (pools hpP) ho h8 htag, b.2.trans a.2.symm⟩ + | @step p o hr hlv ho h8 htag ih => + obtain ⟨hT, hT', hr', e⟩ := ih + have a := V.desc p hT hr hlv o ho h8 htag + have b := V'.desc p hT' hr' (e ▸ hlv) o ho h8 htag + exact ⟨a.1, b.1, .step hr' (e ▸ hlv) ho h8 htag, by rw [a.2, b.2, e]⟩ + rintro p (h | ⟨-, h⟩) + · exact hp (pools h) + · exact ht (reach p h).2.1 + +/-- A user word of a frame object (module docstring). -/ +def FpuFrameW (H : Heap) (ptr bits : U64) (dst : U64) (b : U64) : Prop := + CWW dst 65#u64 b ∧ H ptr = some (.other bits.val) + +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s : S} {a a' : A} {σ : State Unit} {m : McState} + +set_option maxHeartbeats 4000000 in +/-- **ReadFPU's copy at the record** (module docstring). -/ +theorem JStage3A.fpuRead_stepA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {t fs offset ptr rights asid va bits : U64} {size : capability.FrameSize} {mapped stage2 : Bool} + (ht : t ∈ Z.T) (hfs : fs ∈ OS) (hfc : capA st s fs = .Frame ptr size rights false mapped stage2 asid va) + (hbits : capability.FrameSize.bits size = ok bits) (hoff : offset.val % 64 = 0) + (hroom : offset.val + 520 ≤ 2 ^ bits.val) + (hbuf : ∀ b, BufW Bf b → ¬ (ptr.val ≤ b.val ∧ b.val < ptr.val + 2 ^ bits.val)) + {um' : Fl4SpecA64.Word → Word8} + (hum : ∀ x, ¬ FpuByteA (specPtr (wa ptr offset)) x → um' x = σ.machineState.underlyingMemory x) : + let dst := wa ptr offset + let s' := tcbFpuCopyA st s (wa t layout.tcb.FPU) dst + tcb.copy_words st s (wa t layout.tcb.FPU) dst 65#u64 = ok s' ∧ + BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s' ∧ + (∀ a' : A, AR a' Z.hw → SysL st H Z.Cs OS ia ie s' → SymL st H Z.Cs s' → + Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s' → + J7A st (J7R2A st ar nA) Z H OS s' (setUm σ um') m → + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) + s' a' (setUm σ um') m) := by + intro dst s' + have hP := J.iplaced + have hta : TcbAddr t := J.sr.core.tcbs t ht + have h12 : 12 ≤ bits.val := by + cases size <;> simp only [capability.FrameSize.bits] at hbits <;> rw [← okinj hbits] <;> simp + have hH : H ptr = some (.other bits.val) := + J.sys.sysl.sys.fr fs hfs ptr size rights mapped stage2 asid va hfc bits hbits + obtain ⟨hfa, -, hW⟩ := frame_words J.sys.ent.heap hH h12 hoff hroom + have hsrcfit : (wa t layout.tcb.FPU).val + 520 ≤ 2 ^ 64 ∧ Aligned (wa t layout.tcb.FPU) := by + have vF : (wa t layout.tcb.FPU).val = t.val + 1088 := by rw [Notif.tfield_val hta (by simp)]; simp + refine ⟨?_, field_aligned hta (by simp) (by simp)⟩ + rw [vF]; have := hta.fits; omega + have hdfit : dst.val + fl4FpuStateSize ≤ 2 ^ 64 := by + have h1 := J.sys.ent.heap.fits ptr _ hH + simp only [HKind.bits] at h1 + have : dst.val = ptr.val + offset.val := by + show (wa ptr offset).val = _ + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have e : fl4FpuStateSize = 520 := by decide + omega + obtain ⟨ecp, wr⟩ := tcbFpuCopy_exactA (st := st) s (wa t layout.tcb.FPU) dst hfa hdfit + have blk' : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s' := + (blk_of_capw (BXI.blk J.sys.ent.bxi) wr fun b hb => Or.inr (hW b hb).2).1 + refine ⟨ecp, blk', fun a' har sysl symL userOK j7 => ?_⟩ + -- the written words + set X : U64 → Prop := CWW dst 65#u64 with hX + have inF : ∀ b, X b → ptr.val ≤ b.val ∧ b.val < ptr.val + 2 ^ bits.val := fun b hb => (hW b hb).1 + have keep : ∀ b, Aligned b → ¬ X b → L.mem s' b = L.mem s b := fun b hb h => wr.mem_eq hb h + have kb : L.ksb s' = L.ksb s := wr.1 + have hfo := J.sys.sysl.fo fs hfs ptr size rights mapped stage2 asid va hfc bits hbits + have osOff : ∀ b, X b → ¬ InOS OS b := by + rintro b hb ⟨y, hy, g1, g2⟩ + have := inF b hb + rcases hfo.1 y hy with g | g <;> omega + have hobj : ∀ q k, H q = some k → (∀ z, k ≠ .other z) → ∀ c : U64, q.val ≤ c.val → c.val < q.val + 2 ^ k.bits → + ¬ X c := by + intro q k hq hk c c1 c2 hc + have hne : q ≠ ptr := by + rintro rfl + rw [hH] at hq + cases hq + exact hk _ rfl + have := J.sys.ent.heap.disj q k ptr _ hq hH hne + have := inF c hc + simp only [HKind.bits] at * + omega + have tcbH : ∀ u ∈ Z.T, H u = some .tcb := fun u hu => by + have : u ∈ H.tcbs := by rw [J.tcbs]; exact hu + exact this + have tcbOff : ∀ u ∈ Z.T, ∀ c : U64, u.val ≤ c.val → c.val < u.val + 4096 → ¬ X c := fun u hu c c1 c2 => + hobj u .tcb (tcbH u hu) (fun _ h => by cases h) c c1 c2 + have tcbKeep : ∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → L.mem s' (wa u K) = L.mem s (wa u K) := by + intro u hu K h1 h8 + have hua := J.sr.core.tcbs u hu + refine keep _ (field_aligned hua h1 h8) ?_ + refine tcbOff u hu _ ?_ ?_ <;> rw [Notif.tfield_val hua h1] <;> omega + -- J7's user words + obtain ⟨Pt, Ub, vr, -, arm, -, -, -, -, link, -, fu, -, vcu, Pp', Tb', Hr, Te, Kt, Lv', Re, bits', R2, -⟩ := J.j7 + have inUb : ∀ b, X b → b ∈ Ub := fun b hb => by + have hal : Aligned b := by + obtain ⟨k, -, e⟩ := hb + have : dst.val % 8 = 0 := hfa + show b.val % 8 = 0 + omega + exact fu fs hfs ptr size rights mapped stage2 asid va hfc bits hbits b hal (inF b hb).1 (inF b hb).2 + have vsOff : ∀ b, VsR (L.ksb s) Pt b → ¬ X b := fun b hb hx => vr.ub b hb (inUb b hx) + have utPt : utVsPtA st Pp Tb Lv s ⊆ Pt := + utVsPt_j7A J.sys.ent.vs R2.core.stage1 rfl R2.place.pools R2.place.tables + have hXvs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → ¬ X b := by + intro b hb + refine vsOff b ?_ + rcases hb with ⟨p, hp, h⟩ | h + · exact Or.inl ⟨p, utPt hp, h⟩ + · exact Or.inr h + have vcwOff : ∀ v ∈ Z.V, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → ¬ X (wa v o) := fun v hv o h1 h8 hx => + vcu _ ⟨v, hv, o, h1, h8, rfl⟩ (inUb _ hx) + have ksOff : ∀ b, X b → b.val < (L.ksb s).val ∨ (L.ksb s).val + 788736 ≤ b.val := fun b hb => + arm.kb.ksU b (inUb b hb) + have XO : XOffA (st := st) Z H OS s X := + { os := osOff + slot := fun _ y hy h1 h2 hx => osOff _ hx ⟨_, J.j5.2.1 _ hy, h1, h2⟩ + link := fun u hu K hK => by + have hua := J.sr.core.tcbs u hu + have hKv : K.val < 4096 := by rcases hK with rfl | rfl <;> simp + refine tcbOff u hu _ ?_ ?_ <;> rw [Notif.tfield_val hua hKv] <;> omega + irq := fun i hi hx => by + have hv := irqWordA_val J.sr.ksFit hi + rcases ksOff _ hx with h | h <;> omega + vs := fun b hb => by + refine vsOff b ?_ + rcases hb.vsR with ⟨p, hp, h⟩ | h + · refine Or.inl ⟨p, link p ?_, h⟩ + rcases hp with hp | hp | hp + · exact Or.inl hp + · exact Or.inr (Or.inl hp) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl hp)))))) + · exact Or.inr h + page := fun p hp o h1 h8 => vsOff _ (Or.inl ⟨p, link p hp, o, h1, h8, rfl⟩) + vcw := fun v hv o ho => vcwOff v hv o (by omega) (by omega) + dom := fun b hd hx => by + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.sys.ent.ksw + unfold DomW at hd + rcases ksOff b hx with h | h <;> omega + ipi := fun o ho hx => by + obtain ⟨hv, hcl⟩ := J.sr.toSchedSRN.ipi_val ho + rcases ksOff _ hx with h | h <;> omega } + -- the scheduler relation + have nodeOff : ∀ c ∈ Z.Cs, ∀ o : U64, o.val < 4288 → ¬ X (L.nodeAt s c o) := by + intro c hc o ho hx + have hv := nodeAt_val (J.sq.nodes c hc) ho + have h := hfo.2.1 c hc + have := inF _ hx + rw [hv] at this + simp only [LawfulStore.nodeBase] at this + omega + have kwOff : ∀ o : U64, o.val = 8 ∨ o.val = 16 → ¬ X (wa (L.ksb s) o) := by + intro o ho hx + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.sys.ent.ksw + have ev : (wa (L.ksb s) o).val = (L.ksb s).val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have := inF _ hx + rcases hfo.2.2 with g | g <;> omega + have hrd : ∀ b, X b → ¬ SReadA st Z m s σ b := by + rintro b hb (⟨c, hc, o, ho, rfl⟩ | rfl | rfl | ⟨u, hu, K, hK, rfl⟩ | ⟨c, hc, x, k, hx, h1, h2⟩) + · exact nodeOff c hc o ho hb + · exact kwOff _ (Or.inl (by simp)) hb + · exact kwOff _ (Or.inr (by simp)) hb + · have hua := J.sr.core.tcbs u hu + have hKv : K.val < 4096 := by rcases hK with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> simp + refine tcbOff u hu _ ?_ ?_ hb <;> rw [Notif.tfield_val hua hKv] <;> omega + · have hcs : c ∈ H.scs := by rw [J.scs]; exact hc + obtain ⟨z, hz⟩ := hcs + have hsz := (J.scSize c hc x k hx).1 + rw [Heap.scBits_eq hz] at hsz + have : 2 ^ (k + Fl4SpecA64.minSchedContextBits) ≤ 2 ^ z := Nat.pow_le_pow_right (by omega) hsz + exact hobj c (.sc z) hz (fun _ h => by cases h) b h1 (by simp only [HKind.bits]; omega) hb + set σ' := setUm σ um' with hσ' + have SR : SchedSRA st Z m κ s' σ' := by + refine J.sr.stable wr hrd ?_ ?_ + · exact + { fix := ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ + act := rfl + rq := rfl + rel := rfl + sc := fun _ _ _ _ => rfl + tcb := fun _ x h => ⟨x, h, rfl, rfl, rfl⟩ + scBack := fun _ _ _ _ => rfl + tcbBack := fun _ y h => ⟨y, h⟩ + ntfnBack := fun _ y h => ⟨y, h, rfl⟩ } + · intro q x z hx hz + have : z = x := by + change σ.kheap q = _ at hz + rw [hx] at hz; cases hz; rfl + subst this + exact Or.inl ⟨rfl, id⟩ + -- the IPC relations + have feE : FootEq Z.T E Rp Bf (L.mem s) (L.mem s') := by + intro b hb hf + refine keep b hb fun hx => ?_ + rcases hf with ⟨e, he, h1⟩ | ⟨r, hr, h1⟩ | ⟨u, hu, h1⟩ | h1 + · refine hobj e .ep (J.utIpc.ep e he) (fun _ h => by cases h) b ?_ ?_ hx <;> (try simp only [HKind.bits]) <;> omega + · refine hobj r .reply (J.utIpc.reply r hr) (fun _ h => by cases h) b ?_ ?_ hx <;> (try simp only [HKind.bits]) <;> omega + · refine tcbOff u hu b ?_ ?_ hx + · rcases h1 with h1 | h1 + · exact h1.1 + · unfold TL at h1; omega + · rcases h1 with h1 | h1 + · unfold Endp.TF TOffN at h1; omega + · unfold TL at h1; omega + · exact hbuf b h1 (inF b hx) + have kvE : Endp.KView σ σ := + ⟨fun _ _ h => h, fun _ x h => ⟨x, h, rfl⟩, fun _ x h => ⟨x, h, rfl, rfl, rfl, rfl, rfl⟩, rfl⟩ + have ep0 : EpStateRel Z.T E Rp Bf (L.mem s') σ := J.ep.frame hP.e feE kvE + have ep : EpStateRel Z.T E Rp Bf (L.mem s') σ' := by + refine ⟨ep0.ep, ep0.reply, ep0.tcb, fun bp hbp k hk => ?_⟩ + obtain ⟨h1, h2⟩ := ep0.buf bp hbp k hk + refine ⟨?_, h2⟩ + rw [h1] + symm + refine ut_loadWordAt_congrA fun j hj => hum _ fun ⟨i, hi, e⟩ => ?_ + have hbw : BufW Bf (bufW bp.1 k) := ⟨bp, hbp, k, hk, rfl⟩ + have ba : (bufW bp.1 k).val % 8 = 0 := hP.e.balign _ hbw + have eb : bufAddr bp.2 k.val = specPtr (bufW bp.1 k) := by + have em : (bufW bp.1 k).bv = bp.1.bv + BitVec.ofNat 64 (k.val * 8) := by + apply BitVec.eq_of_toNat_eq + have h8 : (8#u64 : U64).val = 8 := by simp + show (bufW bp.1 k).val = _ + unfold bufW + rw [core.num.U64.wrapping_add_val_eq, core.num.U64.wrapping_mul_val_eq, h8] + simp [U64.size_def, U64.numBits, BitVec.toNat_add, BitVec.toNat_ofNat] + unfold bufAddr + rw [J.bfSpec bp hbp, specPtr_def, specPtr_def, em] + have ew : wordSize = 8 := by decide + rw [ew, BitVec.add_assoc, BitVec.add_assoc, BitVec.add_comm pptrShiftA] + rw [eb] at e + -- the written bytes are bytes of copied words + have h8 : (65#u64 : U64).val = 65 := by simp + have hd : dst.val = ptr.val + offset.val := by + have h1 := J.sys.ent.heap.fits ptr _ hH + simp only [HKind.bits] at h1 + show (wa ptr offset).val = _ + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have e' : (bufW bp.1 k).bv + BitVec.ofNat 64 j = dst.bv + BitVec.ofNat 64 i := by + rw [specPtr_def, specPtr_def] at e + have : (bufW bp.1 k).bv + BitVec.ofNat 64 j + pptrShiftA = dst.bv + BitVec.ofNat 64 i + pptrShiftA := by + rw [show (bufW bp.1 k).bv + BitVec.ofNat 64 j + pptrShiftA = + (bufW bp.1 k).bv + pptrShiftA + BitVec.ofNat 64 j by ac_rfl, e] + ac_rfl + exact (BitVec.add_left_inj _).1 this + have en := congrArg BitVec.toNat e' + rw [BitVec.toNat_add, BitVec.toNat_add, BitVec.toNat_ofNat, BitVec.toNat_ofNat] at en + change ((bufW bp.1 k).val + j % 2 ^ 64) % 2 ^ 64 = (dst.val + i % 2 ^ 64) % 2 ^ 64 at en + have hub : (bufW bp.1 k).val < 2 ^ 64 := (bufW bp.1 k).hBounds + have hfit := J.sys.ent.heap.fits ptr _ hH + simp only [HKind.bits] at hfit + rw [Nat.mod_eq_of_lt (show j < 2 ^ 64 by omega), Nat.mod_eq_of_lt (show i < 2 ^ 64 by omega), + Nat.mod_eq_of_lt (show (bufW bp.1 k).val + j < 2 ^ 64 by omega), + Nat.mod_eq_of_lt (show dst.val + i < 2 ^ 64 by omega)] at en + refine hbuf _ hbw ⟨?_, ?_⟩ + · have : dst.val % 8 = 0 := hfa + omega + · omega + have feN : Notif.FootEq N Z.T (L.mem s) (L.mem s') := by + intro b hb hf + refine keep b hb fun hx => ?_ + rcases hf with ⟨q, hq, h1, h2⟩ | ⟨u, hu, h1⟩ + · refine hobj q .ntfn (J.utIpc.ntfn q hq) (fun _ h => by cases h) b h1 ?_ hx + simp only [HKind.bits]; omega + · have hua := J.sr.core.tcbs u hu + have hv : b.val = u.val + 3328 ∨ b.val = u.val + 3336 ∨ b.val = u.val + 256 ∨ b.val = u.val + 3448 ∨ + b.val = u.val + 3496 ∨ b.val = u.val + 3504 := by + rcases h1 with h1 | h1 | h1 | h1 | h1 | h1 <;> + rw [h1] <;> + simp only [stA_eq, Notif.tBlockA_eq, Notif.badgeRegA_eq, Notif.prioA_eq', Notif.nextEp_eq, + Notif.prevEp_eq] <;> + rw [Notif.tfield_val hua] <;> simp + exact tcbOff u hu b (by omega) (by omega) hx + have ntfn : NtfnStateRel N Z.T (L.mem s') σ' := + J.ntfn.frame hP.n feN ⟨fun _ _ h => h, fun _ z h => ⟨z, h, rfl, rfl, rfl⟩⟩ + have reply : ReplyStackRelA (L.mem s') Rp Z.C σ' (scNameA m) stack := by + refine J.reply.frame (fun r hr => ?_) (fun c _ z k h => ⟨z, k, h, rfl⟩) (fun r _ x' h => ⟨x', h, rfl⟩) + have hr3 := hP.e.rep r hr + have one : ∀ O : U64, O.val = 8 ∨ O.val = 16 → L.mem s' (wa r O) = L.mem s (wa r O) := by + intro O hO + have hv : (wa r O).val = r.val + O.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + refine keep _ (by unfold Aligned; rw [hv]; omega) ?_ + refine hobj r .reply (J.utIpc.reply r hr) (fun _ h => by cases h) _ ?_ ?_ <;> (try simp only [HKind.bits]) <;> omega + exact ⟨one _ (Or.inr (by simp [layout.reply.NEXT])), one _ (Or.inl (by simp [layout.reply.PREV]))⟩ + have sys : SysInvA st H Z.Cs OS U Ac Pp Tb Lv ia ie s' := by + have vsr : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → L.mem s' b = L.mem s b := fun b hb => + keep b (J.vsOk.al b hb) (hXvs b hb) + exact ⟨⟨J.sys.ent.heap, BlkOK.bxi blk', J.sys.ent.os, userOK, (utVsPt_keepA J.sys.ent.vs kb vsr).1, + by rw [kb]; exact J.sys.ent.ksw⟩, sysl⟩ + have sq : SchedQueuesInv st H.tcbs Z.Cs H.omega H s' Z.Q Z.R := by + obtain ⟨Q0, R0, h0⟩ := (BlkOK.bxi blk').1 + exact SchedQueuesInv.reCoreA h0 (by rw [J.tcbs]; exact SR.core) + have osk : OSKeep st OS s s' := by + refine OSKeep.of_off J.sys.ent.os (wr.mono fun _ h => h) fun y hy o o1 o2 h => ?_ + have hv : (wa y o).val = y.val + o.val := by + have := J.sys.ent.os.fit y hy + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact osOff _ h ⟨y, hy, by omega, by omega⟩ + have poolOff : PoolOff st H Z.Cs OS s' := fun i => by + have h0 := J.poolOff i + rw [vspace.asid_table.eq] at h0 ⊢ + have v := AtOff.v i + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.sys.ent.ksw + have ev : (wa (L.ksb s) (AtOff i)).val = (L.ksb s).val + (AtOff i).val := by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have e : L.mem s' (wa (L.ksb s') (AtOff i)) = L.mem s (wa (L.ksb s) (AtOff i)) := by + rw [kb] + have h32 := J.j.1.ksbAl + have al : Aligned (wa (L.ksb s) (AtOff i)) := by + show (wa (L.ksb s) (AtOff i)).val % 8 = 0 + omega + refine keep _ al (hXvs _ (Or.inr ⟨AtOff i, by omega, by omega, by omega, rfl⟩)) + rw [e] + refine (WP.spec_ok _).2 fun hne asid => ?_ + have q := (WP.spec_ok _).1 h0 hne asid + exact ⟨q.al, q.om, q.sg, q.os, RegionOffNodes.ksb kb q.ks⟩ + have scCov : ScCovA Z.C m σ' := fun q x k h => J.scCov q x k h + have hTr : TreeA st OS Z.G.chains Z.G.par s := + J.j.1.cdt.tree.grow J.j5.2.1 (fun x hx hn => J.j5.2.2 x hx hn) + have j5 : J5A st Z H OS s' σ' m := + ⟨⟨MdbInvA.keepO hTr.lk hTr.tree J.j5.1.1 osk, IrqRunA.keepO hTr.lk J.j5.1.2.1 osk, + HandlersActiveA.keepO hTr.lk J.j5.1.2.2 osk kb (fun i hi => by + have eI : irqStAddrA (L.ksb s) i = irqWordA (L.ksb s) i := rfl + have hv := irqWordA_val J.sr.ksFit hi + have hal : Aligned (irqWordA (L.ksb s) i) := by + have := J.sr.toSchedSRN.ksb_al + show _ % 8 = 0 + omega + rw [eI] + exact keep _ hal (XO.irq i hi))⟩, J.j5.2.1, + fun y hy hn => by rw [osk.capA hy]; exact J.j5.2.2 y hy hn⟩ + have bufCover : BufCover st Z.T Bf s' σ' := by + intro u hu y b sb v hy hb hl + have hib : IbSame (L := L) s s' u := by + refine ⟨?_, ?_, ?_⟩ + · rw [ibWordA_eq]; exact tcbKeep u hu _ (by simp) (by simp) + · rw [sbWordA_eq]; exact tcbKeep u hu _ (by simp) (by simp) + · rw [sbWordA8_eq]; exact tcbKeep u hu _ (by simp) (by simp) + rw [lookup_ipc_buffer_sameA hib] at hl + exact J.bufCover u hu y b sb v hy hb hl + have mcp : McpRelA Z.T m (L.mem s') σ' := + J.mcp.congrA (fun u hu => ⟨by unfold mcpA; exact tcbKeep u hu _ (by simp) (by simp), + by unfold flagsA; exact tcbKeep u hu _ (by simp) (by simp)⟩) fun u hu x hx => ⟨x, hx, rfl, rfl⟩ + have ye : ∀ u ∈ Z.T, L.mem s' (yieldA u) = L.mem s (yieldA u) := fun u hu => by + unfold yieldA; exact tcbKeep u hu _ (by simp) (by simp) + have yr : YieldRelA Z.T Z.C m (L.mem s') σ' := by + refine ⟨fun u hu => ?_, fun u hu h => ?_, fun c hc y k h p hp => ?_⟩ + · obtain ⟨x, hx, e⟩ := J.yieldRel.tcb u hu + exact ⟨x, hx, by rw [ye u hu]; exact e⟩ + · rw [ye u hu] at h ⊢ + exact J.yieldRel.toSc u hu h + · obtain ⟨u, hu, e1, e2⟩ := J.yieldRel.frm c hc y k h p hp + exact ⟨u, hu, e1, by rw [ye u hu]; exact e2⟩ + have sinv : SInvXA σ' m n := by + refine J.sinv.of_fr + { tcb := fun _ x h => ⟨x, h, rfl, rfl, rfl⟩ + tcb' := fun _ y h => ⟨y, h, rfl, rfl, rfl⟩ + sc := fun _ _ _ => Iff.rfl + cur := rfl + idle := rfl + csc := rfl + rq := rfl + rls := rfl + act := rfl + time := rfl + cons := rfl } (J.sinv.irqN.of_eq rfl rfl) + have sidle : SIdleA σ' m n := + { f3 := J.sidle.f3 + f4 := J.sidle.f4 + f5 := J.sidle.f5 + f6 := J.sidle.f6.of_eq fun _ => rfl + f7 := J.sidle.f7 + idleSlot := J.sidle.idleSlot + idleBound := J.sidle.idleBound } + have rv : RunVcpuInv st Z.T s' := fun u hu h0 => by + have e1 : L.mem s' (runVcpuA u) = L.mem s (runVcpuA u) := by + unfold runVcpuA + exact tcbKeep u hu 3672#u64 (by simp) (by simp) + have e2 : L.mem s' (Fl4ProofsA64.P1.stA u) = L.mem s (Fl4ProofsA64.P1.stA u) := by + rw [Fl4ProofsA64.P1.stA_eq] + exact tcbKeep u hu 3328#u64 (by simp) (by simp) + rw [e2]; exact J.runVcpu u hu (e1 ▸ h0) + have blkQ : BlkQueuedA Z.T E N (L.mem s') σ' := by + refine J.blkQ.keepA (fun u hu => ?_) (fun u _ x' h => Or.inr ⟨x', h, rfl⟩) (fun _ _ h => h) (fun _ _ h => h) + rw [Notif.tBlockA_eq]; exact tcbKeep u hu 3336#u64 (by simp) (by simp) + have sv : SchedValidA σ' := J.schedValid.congr rfl rfl rfl rfl rfl rfl (fun _ _ _ h => h) + (fun _ x h => ⟨x, h, rfl⟩) (fun _ y h => ⟨y, h, rfl, rfl⟩) + have hz : ∀ x : Fl4SpecA64.Word, UtFreeA st OS s (fun d => d = false) (fl4Ptr x).val → + σ'.machineState.underlyingMemory x = σ.machineState.underlyingMemory x := by + intro x hf + by_contra hne + have hb : FpuByteA (specPtr dst) x := by + by_contra h + exact hne (hum x h) + obtain ⟨k, hk, e⟩ := hb + have hfit := J.sys.ent.heap.fits ptr _ hH + simp only [HKind.bits] at hfit + have hd : dst.val = ptr.val + offset.val := by + show (wa ptr offset).val = _ + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have ex : (fl4Ptr x).val = dst.val + k := by + have ep : x = specPtr (⟨dst.bv + BitVec.ofNat 64 k⟩ : U64) := by + rw [e, specPtr_def, specPtr_def] + change dst.bv + pptrShiftA + BitVec.ofNat 64 k = dst.bv + BitVec.ofNat 64 k + pptrShiftA + ac_rfl + rw [ep, fl4Ptr_specPtr] + change (dst.bv + BitVec.ofNat 64 k).toNat = dst.val + k + rw [BitVec.toNat_add, BitVec.toNat_ofNat] + change (dst.val + k % 2 ^ 64) % 2 ^ 64 = _ + rw [Nat.mod_eq_of_lt (show k < 2 ^ 64 by omega), Nat.mod_eq_of_lt (by omega)] + obtain ⟨u, hu, q, b, fi, d, hc, -, f1, f2⟩ := hf + have hO := J.objAlloc ptr (2 ^ bits.val) (Or.inr (Or.inr (Or.inr ⟨_, hH, by simp [HKind.bits]⟩))) u hu q b fi d hc + omega + exact JStage3A.of_schedWrX (symL := symL) (schedValid := sv) (vcX := vcwOff) J XO hXvs J.bufAl + (wr.mono fun _ h => Or.inr h) (σ' := σ') ⟨fun _ => Or.inl rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ + (SSzA.of_kheap rfl) (QKeepA.of_kheap rfl ⟨rfl, rfl, rfl, rfl⟩) hz har (fun _ _ => rfl) sys sq poolOff SR scCov ep ntfn reply + bufCover J.idleNoFault mcp yr sinv sidle rv blkQ j5 j7 + +set_option maxHeartbeats 2000000 in +/-- **TCB ReadFPU at a syscall entry** (module docstring). -/ +theorem JSyscallA.readFpuA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + {slot lbl len buf t fs offset ptr rights asid va bits : U64} {size : capability.FrameSize} {mapped stage2 : Bool} + {aF : A} {blk call don : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Thread t) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hlbl : lbl = fl4_abi.label.TCBReadFPU) + (he : tcb.extra ⟨len, buf, call, slot, extra⟩ 0#usize = ok (some fs)) (hlen : len ≥ 1#u64) + (hoff : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 0#u64 = ok offset) + (hfcap : capA st s fs = .Frame ptr size rights false mapped stage2 asid va) + (hwrite : rights = capability.VM_READ_WRITE) (halign : offset.val % 64 = 0) + (hbits : capability.FrameSize.bits size = ok bits) + (hroom : offset.val + 520 ≤ fpuFrameBytesPubA size) + (hbuf : ∀ b, BufW Bf b → ¬ (ptr.val ≤ b.val ∧ b.val < ptr.val + 2 ^ bits.val)) + (hflush : ar.fpu_flush a t = ok aF) (hk : TcbArchKeepsA ar AR) + (howner : σ.archState.armCurrentFpuOwner ≠ some (specPtr t)) (hfs : fs ∈ OS) : + ∃ xc σ1 σ2 s2, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + (.ok [], σ2) ∈ ((invokeFl4Tcb (.fpuTransfer (specPtr t) true (specPtr (wa ptr offset)))).run σ1).1 ∧ + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (.Ok (), s2, aF) ∧ + ∃ Q Rl, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s2 aF σ2 m ∧ Sched.BodyEndA σ σ2 m := by + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hslot hcov J.actW J.running J.cur + have hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM := J.toJStage3EntryA.curNotVmA + obtain ⟨xc, hkc, hst, hsp, J1, -, osk1, -⟩ := J.toJStage3A.of_restartCurA + RS.pl.choose_spec.choose_spec.choose_spec.choose_spec RS.sym RS.run hnvm RS.cur RS.vs RS.vc + have ht : t ∈ Z.T := J.tcbs ▸ (J.toJStage3A.slotCapTy hslot).1.2.2.2.2.2 t hcap + have T0 := J.toJStage3A.toTA J.sact J.noSwitch + have hstall : smp.remote_tcb_stall st ar s a t = ok (s, a) := T0.stall_singleA hCs h1 ht + set s1 := L.wr s (stA (L.curW s)) sched.TS_RESTART with hs1 + set σ1 := stUpdA σ σ.curThread xc .restart with hσ1 + have hr : tcb.restart_caller st s = ok s1 := by + unfold tcb.restart_caller + rw [cur_thread_ok, bind_ok, hst] + obtain ⟨y, hy, -⟩ := J1.mcp t ht + have hta : TcbAddr t := J.sr.core.tcbs t ht + have h12 : 12 ≤ bits.val := by + cases size <;> simp only [capability.FrameSize.bits] at hbits <;> rw [← okinj hbits] <;> simp + have hbytes : fpuFrameBytesPubA size = 2 ^ bits.val := by + cases size <;> simp only [capability.FrameSize.bits] at hbits <;> rw [← okinj hbits] <;> rfl + have hroom' : offset.val + 520 ≤ 2 ^ bits.val := hbytes ▸ hroom + have hH : H ptr = some (.other bits.val) := + J.sys.sysl.sys.fr fs hfs ptr size rights mapped stage2 asid va hfcap bits hbits + have hfc1 : capA st s1 fs = .Frame ptr size rights false mapped stage2 asid va := by + rw [osk1.capA hfs]; exact hfcap + obtain ⟨hfa, -, -⟩ := frame_words J.sys.ent.heap hH h12 halign hroom' + have hpa : ptr.val % 8 = 0 := by + have h := J.sys.ent.heap.align ptr _ hH + simp only [HKind.bits] at h + exact Nat.mod_eq_zero_of_dvd ((Nat.pow_dvd_pow 2 (by omega : 3 ≤ bits.val) : 2 ^ 3 ∣ 2 ^ bits.val).trans + (Nat.dvd_of_mod_eq_zero h)) + have hfr : (specPtr (wa ptr offset)).toNat % 8 = 0 := by + rw [specPtr_def, BitVec.toNat_add, pptrShiftA_eq] + have h8 : (wa ptr offset).bv.toNat % 8 = 0 := hfa + have : (0x0000808000000000#64 : BitVec 64).toNat = 0x0000808000000000 := by decide + rw [this] + omega + have hown1 : σ1.archState.armCurrentFpuOwner ≠ some (specPtr t) := howner + obtain ⟨um', hspec, hum⟩ := fpuReadSpecA hy hown1 hfr + obtain ⟨ecp, -, K⟩ := J1.fpuRead_stepA ht hfs hfc1 hbits halign hroom' hbuf hum + have hdec : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (.Ok (), tcbFpuCopyA st s1 (wa t layout.tcb.FPU) (wa ptr offset), aF) := by + rw [hcap, decodeTcb_programA, if_neg Bool.false_ne_true, hlbl, tcbReadFPU_programA hstall] + have q := tcbFpuTransfer_exactA (st := st) (ar := ar) (aSanitised := a) (aForget := a) (toFrame := true) he hlen + hoff (by rw [capA_eq, hfcap]) + (by rw [hwrite]; simp [capability.VM_READ_WRITE, capability.VM_KERNEL_ONLY]) (fun _ => hwrite) halign + (by + have e : fl4FpuStateSize = 520 := by decide + rw [e]; cases size <;> exact hroom) hr hflush (fun h => by cases h) (fun h => by cases h) + (by + simp only [↓reduceIte] + exact ⟨hfa, by + have h1' := J.sys.ent.heap.fits ptr _ hH + simp only [HKind.bits] at h1' + have : (wa ptr offset).val = ptr.val + offset.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have e : fl4FpuStateSize = 520 := by decide + omega⟩) + simp only [↓reduceIte] at q + exact q + have har : AR aF Z.hw := by + have h0 := hk.fpu_flush a Z.hw t J.toJStage3A.j.1.ar + rw [hflush, WP.spec_ok] at h0 + exact h0 + obtain ⟨-, -, sysl, symL, -, userOK, j7⟩ := J.decodeFinA hA hM hPl hfm hslot hcov + (rfl : capA st s slot = capA st s slot) hext hSC (lbl := lbl) + (by rw [hcap, hlbl]; simp [DecArmS1aEnt, DecArmQH, DecArmQZ2, DecArmKQ2, DecArmKQ, DecArmKQ3, DecArmDelQ, + DecArmS1aC, capFam]) + (fun _ h => by rw [hcap] at h; cases h) (fun _ h => by rw [hcap] at h; cases h) hdec (setUm σ1 um') m + have J2 := K aF har sysl symL userOK j7 + exact ⟨xc, σ1, _, _, hkc, rfl, hsp, hspec, hdec, Z.Q, Z.R, J2, + Sched.BodyEndA.ofKeep J hkc (Sched.BodyKeepA.ofKh rfl rfl rfl rfl rfl rfl rfl fun _ => Or.inl rfl)⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3TcbMcpA.lean b/proofs/Fl4ProofsA64/P3/Stage3TcbMcpA.lean new file mode 100644 index 000000000..bdf7c4687 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3TcbMcpA.lean @@ -0,0 +1,380 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 3): **TCB SetMCPriority at the record**. + +* **`JStage3A.setMcp_stepA`**: the one-word write of the target's MCP and the spec's `set_mcpriority` keep the record + (`of_stepFinA`; every other relation reads neither the word nor the field). +* **`JSyscallA.setMcpA`**: `decode_invocation` on a Thread cap with the SetMCPriority label at a syscall entry. + +Named premises: [P] `ArchTotal`, `MaxSpi`, `ArchPlat`, `A64FrameMasks`, `hCs`; [S] `h1` (`numNodes = 1`, the remote +stall). The decode succeeds (`he`, `hp`, `hac`, `hle`); the `Err` returns are not lifted. +New module. +-/ +import Fl4ProofsA64.P3.Stage3Lift3QA +import Fl4ProofsA64.P3.Stage3TcbBodyA +import Fl4ProofsA64.P3.Stage3IpcGlueA +import Fl4ProofsA64.P3.Stage3SignalIdleA +import Fl4ProofsA64.P3.Stage3RestartA +import Fl4ProofsA64.P3.Stage3RsSiteA +import Fl4ProofsA64.P3.Stage3FinA +import Fl4ProofsA64.P3.Stage3PreemptTA +import Fl4ProofsA64.P3.Stage3TcbRestartFrA +import Fl4ProofsA64.P3.Stage3TcbF0A +import Fl4ProofsA64.P3.TcbInvokeMcpA +import Fl4ProofsA64.P3.DelScStallA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-- The spec state after `set_mcpriority`. -/ +def mcpStateA (σ : State Unit) (t : ObjRef) (y : Tcb) (p : Priority) : State Unit := + updKSc σ t (.tcb { y with tcbMcpriority := p }) + +/-- The FL4 SetMCPriority invocation on a TCB runs to `set_mcpriority`'s state. -/ +theorem setMcp_invokeA {σ : State Unit} {t auth : U64} {slot : CslotPtr} {y : Tcb} (p : Priority) + (hk : σ.kheap (specPtr t) = some (.tcb y)) : + (fl4InvokeTcb (.threadControlSched (specPtr t) slot none (some (p, specPtr auth)) none none)).run σ = + ({(.ok [], mcpStateA σ (specPtr t) y p)}, False) := by + simp only [fl4InvokeTcb, fl4InstallTcbCap, Fl4Spec.NonDet.maybeM] + have hset : setMcpriority (Z := Unit) (specPtr t) p σ = ({((), mcpStateA σ (specPtr t) y p)}, False) := + threadSet_eqSc _ hk + rw [run_bind_okD (run_returnOkD () σ), run_bind_okD (run_liftE_stD hset)] + rw [run_bind_okD (run_liftE_stD (m := Fl4Spec.NonDet.pure ()) rfl), + run_bind_okD (run_liftE_stD (m := Fl4Spec.NonDet.pure ()) rfl)] + rfl + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s : S} {a : A} {σ : State Unit} {m : McState} + +set_option maxHeartbeats 2000000 in +/-- **`set_mcp` at the record** (module docstring). -/ +theorem JStage3A.setMcp_stepA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {t p : U64} (ht : t ∈ Z.T) (hp : p.val < 256) {y : Tcb} (hy : σ.kheap (specPtr t) = some (.tcb y)) : + BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac (L.wr s (mcpA t) p) ∧ + (SysL st H Z.Cs OS ia ie (L.wr s (mcpA t) p) → SymL st H Z.Cs (L.wr s (mcpA t) p) → + Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U (L.wr s (mcpA t) p) → + J7A st (J7R2A st ar nA) Z H OS (L.wr s (mcpA t) p) + (mcpStateA σ (specPtr t) y (BitVec.ofNat 8 p.val)) m → + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) + (L.wr s (mcpA t) p) a (mcpStateA σ (specPtr t) y (BitVec.ofNat 8 p.val)) m) := by + set s' := L.wr s (mcpA t) p with hs' + set σ' := mcpStateA σ (specPtr t) y (BitVec.ofNat 8 p.val) with hσ' + have hP := J.iplaced + have htT : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + have hta : TcbAddr t := J.sr.core.tcbs t ht + have blk' : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s' := by + have h := Fl4ProofsA64.P1.obj.Tcb.set_mcp.blk (st := st) s t p (BXI.blk J.sys.ent.bxi) htT + have e : obj.Tcb.set_mcp st t s p = ok s' := eq_of_spec_fd (Fl4ProofsA64.P1.obj.Tcb.set_mcp.wr s t p) + rw [e, WP.spec_ok] at h + exact h.1 + refine ⟨blk', fun sysl symL userOK j7 => ?_⟩ + have aM : Aligned (mcpA t) := field_aligned hta (K := 3440#u64) (by simp) (by simp) + have vM : (mcpA t).val = t.val + 3440 := by + show (wa t 3440#u64).val = _ + rw [Notif.tfield_val hta (by decide : (3440#u64 : U64).val < 4096)]; rfl + have w1 : WritesIn st s s' (fun b => b = mcpA t) := writesIn_wr aM + have PF : PartFrA (st := st) N E Rp Z m s s' σ σ' := + tcbSetMCPriority_partA (PartFrA.refl N E Rp Z m s σ) hP.e.tcb ht p _ hy + have F : SchedFrameA σ σ' := PF.fr + have f0 : TcbF0WA Z.T (mcpA t) := ⟨t, ht, by omega, by omega, fun k hk => by omega⟩ + -- every TCB word but the written one + have tw : ∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → K.val ≠ 3440 → L.mem s' (wa u K) = L.mem s (wa u K) := + fun u hu K h1 h2 h3 => by + have hua : TcbAddr u := J.sr.core.tcbs u hu + refine mem_wr_ne' aM (field_aligned hua h1 h2) fun e => ?_ + have ev := congrArg UScalar.val e + rw [vM, Notif.tfield_val hua h1] at ev + have a1 : t.val % 4096 = 0 := hta.align + have a2 : u.val % 4096 = 0 := hua.align + omega + have key : ∀ q, σ'.kheap q = + if q = specPtr t then some (.tcb { y with tcbMcpriority := BitVec.ofNat 8 p.val }) else σ.kheap q := by + intro q + simp only [hσ', mcpStateA, updKSc_kheap, Function.update_apply] + have keepO : ∀ q o, σ.kheap q = some o → (∀ z, o ≠ .tcb z) → σ'.kheap q = some o := fun q o h n1 => by + rw [key, if_neg (fun e => by rw [e, hy] at h; exact n1 y (Option.some.inj h).symm)] + exact h + have backO : ∀ q o, σ'.kheap q = some o → (∀ z, o ≠ .tcb z) → σ.kheap q = some o := fun q o h n1 => by + rw [key] at h + split at h + · exact absurd (Option.some.inj h).symm (n1 _) + · exact h + have fwdT : ∀ q z, σ.kheap q = some (.tcb z) → ∃ w, σ'.kheap q = some (.tcb w) ∧ + w = { z with tcbMcpriority := w.tcbMcpriority } := by + intro q z h + by_cases e : q = specPtr t + · rw [e, hy] at h + cases h + exact ⟨{ y with tcbMcpriority := BitVec.ofNat 8 p.val }, by rw [key, if_pos e], rfl⟩ + · exact ⟨z, by rw [key, if_neg e]; exact h, rfl⟩ + have bwdT : ∀ q w, σ'.kheap q = some (.tcb w) → ∃ z, σ.kheap q = some (.tcb z) ∧ + w = { z with tcbMcpriority := w.tcbMcpriority } := by + intro q w h + rw [key] at h + split at h + · rename_i e + cases h + exact ⟨y, e ▸ hy, rfl⟩ + · exact ⟨w, h, rfl⟩ + have SR : SchedSRA st Z m κ s' σ' := by + refine J.sr.stable w1 (fun b hb => hb ▸ f0WordOffReadA J.sr.toSchedSRN f0) ?_ ?_ + · refine + { fix := ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ + act := rfl + rq := rfl + rel := rfl + sc := fun q x k h => (keepO q _ h (fun _ e => by cases e)).trans h.symm + tcb := fun q x h => by + obtain ⟨w, hw, e⟩ := fwdT q x h + exact ⟨w, hw, by rw [e], by rw [e], by rw [e]⟩ + scBack := fun q x k h => (backO q _ h (fun _ e => by cases e)).trans h.symm + tcbBack := fun q w h => by + obtain ⟨z, hz, -⟩ := bwdT q w h + exact ⟨z, hz⟩ + ntfnBack := fun q z h => ⟨z, backO q _ h (fun _ e => by cases e), rfl⟩ } + · intro q x z hx hz + obtain ⟨w, hw, e⟩ := fwdT q x hx + rw [hz] at hw + cases hw + refine Or.inl ⟨by rw [e], fun h => ?_⟩ + rw [e] at h + exact h + have off : ∀ b : U64, Aligned b → b.val ≠ t.val + 3440 → L.mem s' b = L.mem s b := fun b hb h => + mem_wr_ne' aM hb fun e => h (by rw [← e]; exact vM) + have feE : FootEq Z.T E Rp Bf (L.mem s) (L.mem s') := by + intro b hb hf + refine off b hb fun h => ?_ + have inT : t.val ≤ b.val ∧ b.val < t.val + 4096 := by omega + rcases hf with ⟨e, he, h1⟩ | ⟨r, hr, h1⟩ | ⟨u, hu, h1⟩ | h1 + · have := hP.e.et e he t ht; omega + · have := hP.e.rt r hr t ht; omega + · have hua := hP.e.tcb u hu + have a1 : t.val % 4096 = 0 := hta.align + have a2 : u.val % 4096 = 0 := hua.align + rcases h1 with h1 | h1 + · unfold Endp.TF TOffN at h1 + omega + · unfold TL at h1 + omega + · have := hP.e.bt _ h1 t ht; omega + have kvE : Endp.KView σ σ' := by + refine ⟨fun q z h => keepO q _ h (fun _ e => by cases e), fun q z h => ⟨z, keepO q _ h (fun _ e => by cases e), rfl⟩, + fun q z h => ?_, F.2.2⟩ + obtain ⟨w, hw, e⟩ := fwdT q z h + exact ⟨w, hw, by rw [e], by rw [e], by rw [e], by rw [e], by rw [e]⟩ + have ep : EpStateRel Z.T E Rp Bf (L.mem s') σ' := J.ep.frame hP.e feE kvE + have feN : Notif.FootEq N Z.T (L.mem s) (L.mem s') := by + intro b hb hf + refine off b hb fun h => ?_ + rcases hf with ⟨q, hq, h1, h2⟩ | ⟨u, hu, h1⟩ + · have := hP.n.disj q hq t ht; omega + · have hua := hP.e.tcb u hu + have a1 : t.val % 4096 = 0 := hta.align + have a2 : u.val % 4096 = 0 := hua.align + have hv : b.val = u.val + 3328 ∨ b.val = u.val + 3336 ∨ b.val = u.val + 256 ∨ b.val = u.val + 3448 ∨ + b.val = u.val + 3496 ∨ b.val = u.val + 3504 := by + rcases h1 with h1 | h1 | h1 | h1 | h1 | h1 <;> + rw [h1] <;> + simp only [stA_eq, Notif.tBlockA_eq, Notif.badgeRegA_eq, Notif.prioA_eq', Notif.nextEp_eq, + Notif.prevEp_eq] <;> + rw [Notif.tfield_val hua] <;> simp + omega + have kvN : Notif.KView σ σ' := by + refine ⟨fun q z h => keepO q _ h (fun _ e => by cases e), fun q z h => ?_⟩ + obtain ⟨w, hw, e⟩ := fwdT q z h + refine ⟨w, hw, by rw [e], by rw [e]; rfl, by rw [e]⟩ + have ntfn : NtfnStateRel N Z.T (L.mem s') σ' := J.ntfn.frame hP.n feN kvN + have reply : ReplyStackRelA (L.mem s') Rp Z.C σ' (scNameA m) stack := by + refine J.reply.frame (fun r hr => ?_) (fun c _ z k h => ⟨z, k, keepO _ _ h (fun _ e => by cases e), rfl⟩) + (fun r _ x' h => ⟨x', backO _ _ h (fun _ e => by cases e), rfl⟩) + have hr3 := hP.e.rep r hr + have dis := hP.e.rt r hr t ht + have one : ∀ O : U64, O.val = 8 ∨ O.val = 16 → L.mem s' (wa r O) = L.mem s (wa r O) := by + intro O hO + have hv : (wa r O).val = r.val + O.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact off _ (by unfold Aligned; rw [hv]; omega) (by omega) + exact ⟨one _ (Or.inr (by simp [layout.reply.NEXT])), one _ (Or.inl (by simp [layout.reply.PREV]))⟩ + have bufCover : BufCover st Z.T Bf s' σ' := by + intro u hu z b sb v hz hb hl + obtain ⟨x, hx, e⟩ := bwdT _ z hz + have hb0 : ipcBufOfA b x = some sb := by + rw [e] at hb + exact hb + have hib : IbSame (L := L) s s' u := by + refine ⟨?_, ?_, ?_⟩ + · rw [ibWordA_eq]; exact tw u hu _ (by simp) (by simp) (by simp) + · rw [sbWordA_eq]; exact tw u hu _ (by simp) (by simp) (by simp) + · rw [sbWordA8_eq]; exact tw u hu _ (by simp) (by simp) (by simp) + rw [lookup_ipc_buffer_sameA hib] at hl + exact J.bufCover u hu x b sb v hx hb0 hl + have bufAl : UtBufAlA Z.T σ' := fun u hu z hz => by + obtain ⟨x, hx, e⟩ := bwdT _ z hz + rw [e]; exact J.bufAl u hu x hx + have idle : IdleNoFaultA m σ' := fun c hc => by + obtain ⟨x, hx, hf⟩ := J.idleNoFault c hc + obtain ⟨w, hw, e⟩ := fwdT _ x hx + exact ⟨w, hw, by rw [e]; exact hf⟩ + have mcp : McpRelA Z.T m (L.mem s') σ' := tcbMcpRel_writeA hP.e.tcb ht J.mcp hy hp + have yr : YieldRelA Z.T Z.C m (L.mem s') σ' := by + have ye : ∀ u ∈ Z.T, L.mem s' (yieldA u) = L.mem s (yieldA u) := fun u hu => by + unfold yieldA; exact tw u hu _ (by simp) (by simp) (by simp) + refine ⟨fun u hu => ?_, fun u hu h => ?_, fun c hc z k h q hq => ?_⟩ + · obtain ⟨x, hx, e1⟩ := J.yieldRel.tcb u hu + obtain ⟨w, hw, e⟩ := fwdT _ x hx + exact ⟨w, hw, by rw [ye u hu, e]; exact e1⟩ + · rw [ye u hu] at h ⊢ + obtain ⟨hC, z, k, hz, e⟩ := J.yieldRel.toSc u hu h + exact ⟨hC, z, k, keepO _ _ hz (fun _ e => by cases e), e⟩ + · obtain ⟨u, hu, e1, e2⟩ := J.yieldRel.frm c hc z k (backO _ _ h (fun _ e => by cases e)) q hq + exact ⟨u, hu, e1, by rw [ye u hu]; exact e2⟩ + have sinv : SInvXA σ' m n := by + refine J.sinv.of_fr + { tcb := fun q x h => by + obtain ⟨w, hw, e⟩ := fwdT q x h + exact ⟨w, hw, by rw [e], by rw [e], by rw [e]⟩ + tcb' := fun q w h => by + obtain ⟨z, hz, e⟩ := bwdT q w h + exact ⟨z, hz, by rw [e], by rw [e], by rw [e]⟩ + sc := fun q z k => ⟨fun h => backO q _ h (fun _ e => by cases e), fun h => keepO q _ h (fun _ e => by cases e)⟩ + cur := rfl + idle := rfl + csc := rfl + rq := rfl + rls := rfl + act := rfl + time := rfl + cons := rfl } ?_ + exact J.sinv.irqN.updTcbA hy rfl + have sidle : SIdleA σ' m n := + { f3 := fun c hc => by + obtain ⟨x, hx, hs⟩ := J.sidle.f3 c hc + obtain ⟨w, hw, e⟩ := fwdT _ x hx + exact ⟨w, hw, by rw [e]; exact hs⟩ + f4 := fun z k h => J.sidle.f4 z k (backO _ _ h (fun _ e => by cases e)) + f5 := fun q z k h => J.sidle.f5 q z k (backO _ _ h (fun _ e => by cases e)) + f6 := J.sidle.f6.of_eq fun sl => by + unfold specCapAtA + by_cases e : sl.1 = specPtr t + · rw [key, if_pos e, e, hy] + rfl + · rw [key, if_neg e] + f7 := fun y k h => J.sidle.f7 y k (backO _ _ h (fun _ e => by cases e)) + idleSlot := fun c hc => by + have h0 := J.sidle.idleSlot c hc + unfold vtCapA at h0 ⊢ + by_cases e : m.idleThreadOf c = specPtr t + · rw [key, if_pos e]; rw [e, hy] at h0; exact h0 + · rw [key, if_neg e]; exact h0 + idleBound := fun c hc => by + have h0 := J.sidle.idleBound c hc + unfold vcpuOfA at h0 ⊢ + by_cases e : m.idleThreadOf c = specPtr t + · rw [key, if_pos e]; rw [e, hy] at h0; exact h0 + · rw [key, if_neg e]; exact h0 } + have rv : RunVcpuInv st Z.T s' := fun u hu h0 => by + have e1 : L.mem s' (runVcpuA u) = L.mem s (runVcpuA u) := by + unfold runVcpuA + exact tw u hu 3672#u64 (by simp) (by simp) (by simp) + have e2 : L.mem s' (Fl4ProofsA64.P1.stA u) = L.mem s (Fl4ProofsA64.P1.stA u) := by + rw [Fl4ProofsA64.P1.stA_eq] + exact tw u hu 3328#u64 (by simp) (by simp) (by simp) + rw [e2]; exact J.runVcpu u hu (e1 ▸ h0) + have blkQ : BlkQueuedA Z.T E N (L.mem s') σ' := by + refine J.blkQ.keepA (fun u hu => ?_) (fun u _ x' h => ?_) (fun q z h => keepO q _ h (fun _ e => by cases e)) + (fun q z h => keepO q _ h (fun _ e => by cases e)) + · rw [Notif.tBlockA_eq]; exact tw u hu 3336#u64 (by simp) (by simp) (by simp) + · obtain ⟨z, hz, e⟩ := bwdT _ x' h + exact Or.inr ⟨z, hz, by rw [e]⟩ + have sv : SchedValidA σ' := J.schedValid.congr rfl rfl rfl rfl rfl rfl + (fun q z k h => keepO q _ h (fun _ e => by cases e)) + (fun u x h => by + obtain ⟨w, hw, e⟩ := fwdT u x h + exact ⟨w, hw, by rw [e]⟩) + (fun u w h => by + obtain ⟨z, hz, e⟩ := bwdT u w h + exact ⟨z, hz, by rw [e], by rw [e]⟩) + have Qk : QKeepA σ σ' := QKeepA.ofBwd fun q o h => by + rw [key] at h + split at h + · rename_i e + cases h + exact ⟨.tcb y, by rw [e]; exact hy, QObjA.ofTcb rfl⟩ + · exact ⟨o, h, QObjA.refl o⟩ + exact J.of_stepFinA (Q := Z.Q) (Rl := Z.R) (curOf := Z.curOf) (curScOf := Z.curScOf) PF.wr F.1 F.2.1 Qk F.2.2 + (fun _ _ => rfl) (BlkOK.bxi blk') sysl symL userOK j7 SR ep ntfn reply bufCover bufAl idle mcp yr sinv sidle + rv blkQ sv + +set_option maxHeartbeats 2000000 in +/-- **TCB SetMCPriority at a syscall entry** (module docstring). -/ +theorem JSyscallA.setMcpA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + {slot lbl len buf t p auth argSlot : U64} {blk call don : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Thread t) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hlbl : lbl = fl4_abi.label.TCBSetMCPriority) + (he : tcb.extra_with_length ⟨len, buf, call, slot, extra⟩ 0#usize 1#u64 = ok (.Ok argSlot)) + (hp : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 0#u64 = ok p) + (harg : argSlot ∈ OS) (hac : capA st s argSlot = .Thread auth) (hle : ¬ L.mem s (mcpA auth) < p) : + ∃ xc σ1 σ2 s2, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + (∀ κs : CslotPtr, (.ok [], σ2) ∈ ((fl4InvokeTcb (.threadControlSched (specPtr t) κs none + (some (BitVec.ofNat 8 p.val, specPtr auth)) none none)).run σ1).1) ∧ + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (.Ok (), s2, a) ∧ + ∃ Q Rl, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s2 a σ2 m ∧ Sched.BodyEndA σ σ2 m := by + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hslot hcov J.actW J.running J.cur + have hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM := J.toJStage3EntryA.curNotVmA + obtain ⟨xc, hkc, hst, hsp, J1, -, -, -⟩ := J.toJStage3A.of_restartCurA RS.pl.choose_spec.choose_spec.choose_spec.choose_spec + RS.sym RS.run hnvm RS.cur RS.vs RS.vc + have ht : t ∈ Z.T := J.tcbs ▸ (J.toJStage3A.slotCapTy hslot).1.2.2.2.2.2 t hcap + have haT : auth ∈ Z.T := J.tcbs ▸ (J.toJStage3A.slotCapTy harg).1.2.2.2.2.2 auth hac + have T0 := J.toJStage3A.toTA J.sact J.noSwitch + have hstall : smp.remote_tcb_stall st ar s a t = ok (s, a) := T0.stall_singleA hCs h1 ht + set s1 := L.wr s (stA (L.curW s)) sched.TS_RESTART with hs1 + set σ1 := stUpdA σ σ.curThread xc .restart with hσ1 + have hWidth : p.val < 256 := by + obtain ⟨x, -, hw⟩ := J.mcp auth haT + have hv : p.val ≤ (L.mem s (mcpA auth)).val := Nat.le_of_not_lt hle + rw [hw.1] at hv + exact Nat.lt_of_le_of_lt hv x.tcbMcpriority.isLt + have hr : tcb.restart_caller st s = ok s1 := by + unfold tcb.restart_caller + rw [cur_thread_ok, bind_ok, hst] + have ha : tcb.thread_arg st s argSlot 1#u64 = ok (.Ok auth) := by + unfold tcb.thread_arg + rw [capA_eq, bind_ok, hac] + have hdec : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (.Ok (), L.wr s1 (mcpA t) p, a) := by + rw [hcap, decodeTcb_programA, if_neg Bool.false_ne_true, hlbl] + exact tcbSetMCPriority_postA hstall he hp ha hle hr + obtain ⟨y, hy, -⟩ := J1.mcp t ht + have R := J1.setMcp_stepA (st := st) ht hWidth hy + obtain ⟨-, K⟩ := R + obtain ⟨-, -, sysl, symL, -, userOK, j7⟩ := J.decodeFinA hA hM hPl hfm hslot hcov + (rfl : capA st s slot = capA st s slot) hext hSC (lbl := lbl) + (by rw [hcap, hlbl]; simp [DecArmS1aEnt, DecArmQH, DecArmQZ2, DecArmKQ2, DecArmKQ, capFam]) + (fun _ h => by rw [hcap] at h; cases h) (fun _ h => by rw [hcap] at h; cases h) hdec + (mcpStateA σ1 (specPtr t) y (BitVec.ofNat 8 p.val)) m + have J2 := K sysl symL userOK j7 + refine ⟨xc, σ1, _, _, hkc, rfl, hsp, fun κs => ?_, hdec, Z.Q, Z.R, J2, Sched.BodyEndA.ofTcbUpd J hkc hy rfl⟩ + rw [setMcp_invokeA _ hy] + rfl + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3TcbNtfnFrA.lean b/proofs/Fl4ProofsA64/P3/Stage3TcbNtfnFrA.lean new file mode 100644 index 000000000..0b0d60844 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3TcbNtfnFrA.lean @@ -0,0 +1,64 @@ +/- +P3 (aarch64, A64-P3 Stage 3, D8 step 3): **TCB BindNotification / UnbindNotification are F0** (no cap or VSpace +write): their two Store words are Stage-3 write-set words and the spec step is a `SchedFrameA` step. No premise. +-/ +import Fl4ProofsA64.P3.TcbInvokeNtfnPostA +import Fl4ProofsA64.P3.Stage3McsFrA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4SpecA64 Sched +noncomputable section + +/-- The spec step of Bind/Unbind: one notification replaced, one TCB replaced keeping its slot map: a scheduler frame. -/ +theorem tcbNtfnPair_frameA {σ : State Unit} {n t : ObjRef} {x x' : Notification} {y y' : Tcb} + (hk : σ.kheap n = some (.notification x)) (hy : σ.kheap t = some (.tcb y)) (hy' : TcbSlotKeepA y y') : + SchedFrameA σ (updKSc (updKSc σ n (.notification x')) t (.tcb y')) := by + have hnt : t ≠ n := fun e => by subst e; rw [hk] at hy; cases hy + have key : ∀ p, (updKSc (updKSc σ n (.notification x')) t (.tcb y')).kheap p = + if p = t then some (.tcb y') else if p = n then some (.notification x') else σ.kheap p := by + intro p + simp only [updKSc_kheap, Function.update_apply] + refine ⟨⟨fun p => ?_, rfl, rfl, rfl, rfl, rfl, rfl⟩, fun p z k hp => ?_, rfl⟩ + · rw [key p] + by_cases e1 : p = t + · subst e1 + exact Or.inr (Or.inl ⟨y, y', hy, by simp, hy'⟩) + · by_cases e2 : p = n + · subst e2 + exact Or.inr (Or.inr ⟨_, _, hk, by simp [e1], Or.inr (Or.inl ⟨x, x', rfl, rfl⟩)⟩) + · exact Or.inl (by simp [e1, e2]) + · rw [key p] at hp + by_cases e1 : p = t + · rw [if_pos e1] at hp; cases hp + · by_cases e2 : p = n + · rw [if_neg e1, if_pos e2] at hp; cases hp + · rw [if_neg e1, if_neg e2] at hp; exact ⟨z, hp⟩ + +section conc +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +theorem ntfnBtA_stepNA {N E Rp : Set U64} {Z : XSetsA} {m : McState} {σ : State Unit} {s : S} + {n : U64} (hn : n ∈ N) (hfit : n.val + 64 ≤ 2 ^ 64) : SchedStepNWA (st := st) N E Rp Z m σ s (ntfnBtA n) := by + refine Or.inr (Or.inl ⟨n, hn, ?_, ?_⟩) <;> unfold ntfnBtA <;> rw [wa_valm] <;> + (have : (layout.ntfn.BOUND_TCB).val < 64 := by unfold layout.ntfn.BOUND_TCB; decide) <;> omega + +theorem boundA_stepNA {N E Rp : Set U64} {Z : XSetsA} {m : McState} {σ : State Unit} {s : S} + (hP : TcbPlaced Z.T) {t : U64} (ht : t ∈ Z.T) : SchedStepNWA (st := st) N E Rp Z m σ s (boundA t) := + tcbWord_stepNA hP ht 3368#u64 (by decide) (by decide) (by decide) (by decide) + +/-- **`PartFrA` of BindNotification / UnbindNotification** from the two-word write footprint of +`tcbBindNotification_performA` / `tcbUnbindNotification_performA` and the spec post state. -/ +theorem tcbNtfn_partA {N E Rp : Set U64} {Z : XSetsA} {m : McState} {s s' : S} {σ : State Unit} + {n t : U64} {x x' : Notification} {y y' : Tcb} + (hP : TcbPlaced Z.T) (ht : t ∈ Z.T) (hn : n ∈ N) (hfit : n.val + 64 ≤ 2 ^ 64) + (w : WritesIn st s s' (fun w => w = ntfnBtA n ∨ w = boundA t)) + (hk : σ.kheap (specPtr n) = some (.notification x)) (hy : σ.kheap (specPtr t) = some (.tcb y)) + (hy' : TcbSlotKeepA y y') : + PartFrA (st := st) N E Rp Z m s s' σ (updKSc (updKSc σ (specPtr n) (.notification x')) (specPtr t) (.tcb y')) := + ⟨w.mono fun b hb => hb.elim (fun e => e ▸ ntfnBtA_stepNA hn hfit) (fun e => e ▸ boundA_stepNA hP ht), + tcbNtfnPair_frameA hk hy hy'⟩ + +end conc +end +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3TcbPacA.lean b/proofs/Fl4ProofsA64/P3/Stage3TcbPacA.lean new file mode 100644 index 000000000..2243baff5 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3TcbPacA.lean @@ -0,0 +1,208 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 3): **TCB SetPACKeys at the record**. + +* **`JStage3A.pacKeys_stepA`**: the twelve PAC words `[t + 1680, t + 1776)` keep the record; the spec state is + unchanged (it has no PAC fields). +* **`JSyscallA.setPacKeysA`**: `decode_invocation` on a Thread cap with the SetPACKeys label at a syscall entry. + +Named premises: [P] `ArchTotal`, `MaxSpi`, `ArchPlat`, `A64FrameMasks`, `hCs`; [P] `hprobe`, `hsupported`, `hepoch` +(the architecture's PAC probe and epoch call) and `hk : TcbArchKeepsA ar AR` (the epoch call keeps the +hardware-root relation `AR`, quantified over the call); +`hbuf` (the argument words in the IPC buffer are not the words the caller's `Restart` writes); [S] `h1` +(`numNodes = 1`, the remote stall). The decode succeeds; the `Err` returns are not lifted. +New module. +-/ +import Fl4ProofsA64.P3.Stage3TcbWordsA +import Fl4ProofsA64.P3.TcbArchKeepsA +import Fl4ProofsA64.P3.TcbInvokePacSelectA +import Fl4ProofsA64.P1.QueueDecTcb2 +import Fl4ProofsA64.P1.QueueTcbInv +import Fl4ProofsA64.P3.Stage3TcbBodyA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s : S} {a : A} {σ : State Unit} {m : McState} + +set_option maxHeartbeats 2000000 in +/-- **SetPACKeys at the record** (module docstring). -/ +theorem JStage3A.pacKeys_stepA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {t flags epoch : U64} (ht : t ∈ Z.T) (keys : Aeneas.Std.Array U64 10#usize) : + let s' := tcbPacPostA st s t keys flags epoch + BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s' ∧ + (∀ a' : A, AR a' Z.hw → SysL st H Z.Cs OS ia ie s' → SymL st H Z.Cs s' → + Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s' → + J7A st (J7R2A st ar nA) Z H OS s' σ m → + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) + s' a' σ m) := by + intro s' + have hP := J.iplaced + have htT : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + have hta : TcbAddr t := J.sr.core.tcbs t ht + have blk' : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s' := by + have step : ∀ (q : S) (K v : U64), 1680 ≤ K.val → K.val < 1776 → K.val % 8 = 0 → + BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac q → + BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac (L.wr q (wa t K) v) := + fun q K v h1 h2 h8 h => (plainW_all (v := v) h htT (by omega) (by omega) h8 (by omega) (by omega) + (by omega) (by omega)).1 + obtain ⟨p, ep, es⟩ : ∃ p, tcb.write_pac_keys st s t keys = ok p ∧ + tcbPacPostA st s t keys flags epoch = + L.wr (L.wr p (wa t layout.tcb.PAC_FLAGS) flags) (wa t layout.tcb.PAC_EPOCH) epoch := + ⟨_, tcbWritePacKeys_exactA s t keys, rfl⟩ + have hp : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac p := by + have h := tcb.write_pac_keys_loop.rf (st := st) (s := s) (t := t) (i := 0#u64) (keys := keys) + (R := fun q => BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac q) + (fun q K v h1 h2 h8 h => step q K v (by omega) h2 h8 h) (BXI.blk J.sys.ent.bxi) + unfold tcb.write_pac_keys at ep + rw [ep, WP.spec_ok] at h + exact h + show BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac (tcbPacPostA st s t keys flags epoch) + rw [es] + have f1 : layout.tcb.PAC_FLAGS.val = 1680 := by simp + have f2 : layout.tcb.PAC_EPOCH.val = 1688 := by simp + exact step _ _ _ (by omega) (by omega) (by omega) (step _ _ _ (by omega) (by omega) (by omega) hp) + refine ⟨blk', fun a' har sysl symL userOK j7 => ?_⟩ + obtain ⟨k1, k2, k3⟩ := tcbPacPost_frameA (st := st) s t hta keys flags epoch + have wr : WritesIn st s s' (fun b => t.val + 1680 ≤ b.val ∧ b.val < t.val + 1776) := + ⟨k1, k2, fun b hb hn => k3 b hb (by omega)⟩ + have w : WritesIn st s s' (PlainRegW Z.T) := wr.mono fun b hb => ⟨t, ht, by omega, by omega⟩ + have off : ∀ b : U64, Aligned b → (b.val < t.val + 1680 ∨ t.val + 1776 ≤ b.val) → L.mem s' b = L.mem s b := + fun b hb h => wr.mem_eq hb fun c => by omega + have feE : FootEq Z.T E Rp Bf (L.mem s) (L.mem s') := by + intro b hb hf + refine off b hb ?_ + by_contra h + have inT : t.val ≤ b.val ∧ b.val < t.val + 4096 := by omega + rcases hf with ⟨e, he, h1⟩ | ⟨r, hr, h1⟩ | ⟨u, hu, h1⟩ | h1 + · have := hP.e.et e he t ht; omega + · have := hP.e.rt r hr t ht; omega + · have hua := hP.e.tcb u hu + have a1 : t.val % 4096 = 0 := hta.align + have a2 : u.val % 4096 = 0 := hua.align + rcases h1 with h1 | h1 + · unfold Endp.TF TOffN at h1 + omega + · unfold TL at h1 + omega + · have := hP.e.bt _ h1 t ht; omega + have ep : EpStateRel Z.T E Rp Bf (L.mem s') σ := J.ep.frame hP.e feE (Endp.KView.refl σ) + have feN : Notif.FootEq N Z.T (L.mem s) (L.mem s') := by + intro b hb hf + refine off b hb ?_ + by_contra h + rcases hf with ⟨q, hq, h1, h2⟩ | ⟨u, hu, h1⟩ + · have := hP.n.disj q hq t ht; omega + · have hua := hP.e.tcb u hu + have a1 : t.val % 4096 = 0 := hta.align + have a2 : u.val % 4096 = 0 := hua.align + have hv : b.val = u.val + 3328 ∨ b.val = u.val + 3336 ∨ b.val = u.val + 256 ∨ b.val = u.val + 3448 ∨ + b.val = u.val + 3496 ∨ b.val = u.val + 3504 := by + rcases h1 with h1 | h1 | h1 | h1 | h1 | h1 <;> + rw [h1] <;> + simp only [stA_eq, Notif.tBlockA_eq, Notif.badgeRegA_eq, Notif.prioA_eq', Notif.nextEp_eq, + Notif.prevEp_eq] <;> + rw [Notif.tfield_val hua] <;> simp + omega + have ntfn : NtfnStateRel N Z.T (L.mem s') σ := J.ntfn.frame hP.n feN (Notif.KView.refl σ) + obtain ⟨y, hy, -⟩ := J.mcp t ht + have eσ : updKSc σ (specPtr t) (.tcb { y with tcbArch := y.tcbArch }) = σ := by + have e : Function.update σ.kheap (specPtr t) (some (.tcb { y with tcbArch := y.tcbArch })) = σ.kheap := by + rw [show some (KernelObject.tcb { y with tcbArch := y.tcbArch }) = σ.kheap (specPtr t) from by rw [hy]] + exact Function.update_eq_self _ _ + unfold updKSc + rw [e] + have R := J.of_plainTcbA (s' := s') ht hy y.tcbArch rfl w blk' sysl symL userOK (eσ.symm ▸ j7) (eσ.symm ▸ ep) + (eσ.symm ▸ ntfn) + rw [eσ] at R + exact { R with j := ⟨{ R.j.1 with ar := har }, R.j.2⟩ } + +set_option maxHeartbeats 2000000 in +/-- **TCB SetPACKeys at a syscall entry** (module docstring). -/ +theorem JSyscallA.setPacKeysA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + {slot lbl len buf t flags epoch : U64} {a' : A} {blk call don : Bool} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Thread t) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hlbl : lbl = fl4_abi.label.TCBSetPACKeys) (k0 k1 k2 k3 k4 k5 k6 k7 k8 k9 : U64) + (hlen : ¬ len < 11#u64) + (hflags : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 0#u64 = ok flags) + (hmask : flags &&& ~~~fl4_abi.consts.pac_flag.MASK = 0#u64) + (hprobe : flags ≠ 0#u64 → ar.has_pauth a = ok fl4A64Platform.hasPauth) + (hsupported : flags ≠ 0#u64 → fl4A64Platform.hasPauth = true) + (hepoch : ar.next_pac_epoch a = ok (epoch, a')) (hk : TcbArchKeepsA ar AR) + (hbuf : ∀ i : U64, i.val < 11 → ¬ i < msg.N_MSG_REGISTERS → buf ≠ 0#u64 → + let b := bufWA buf (wa i 1#u64) + Aligned b ∧ b ≠ stA (L.curW s) ∧ b ≠ L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) + (g0 : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 1#u64 = ok k0) + (g1 : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 2#u64 = ok k1) + (g2 : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 3#u64 = ok k2) + (g3 : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 4#u64 = ok k3) + (g4 : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 5#u64 = ok k4) + (g5 : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 6#u64 = ok k5) + (g6 : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 7#u64 = ok k6) + (g7 : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 8#u64 = ok k7) + (g8 : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 9#u64 = ok k8) + (g9 : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 10#u64 = ok k9) : + ∃ xc σ1 s2, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + (∀ f ks, (invokeFl4Tcb (.setPacKeys (specPtr t) f ks)).run σ1 = ({(.ok [], σ1)}, False)) ∧ + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (.Ok (), s2, a') ∧ + ∃ Q Rl, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s2 a' σ1 m ∧ Sched.BodyEndA σ σ1 m := by + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hslot hcov J.actW J.running J.cur + have hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM := J.toJStage3EntryA.curNotVmA + obtain ⟨xc, hkc, hst, hsp, J1, -, -, -⟩ := J.toJStage3A.of_restartCurA RS.pl.choose_spec.choose_spec.choose_spec.choose_spec + RS.sym RS.run hnvm RS.cur RS.vs RS.vc + have ht : t ∈ Z.T := J.tcbs ▸ (J.toJStage3A.slotCapTy hslot).1.2.2.2.2.2 t hcap + have T0 := J.toJStage3A.toTA J.sact J.noSwitch + have hstall : smp.remote_tcb_stall st ar s a t = ok (s, a) := T0.stall_singleA hCs h1 ht + set s1 := L.wr s (stA (L.curW s)) sched.TS_RESTART with hs1 + set σ1 := stUpdA σ σ.curThread xc .restart with hσ1 + have hr : tcb.restart_caller st s = ok s1 := by + unfold tcb.restart_caller + rw [cur_thread_ok, bind_ok, hst] + have hq := J.sys.ent.bxi.1.choose_spec.choose_spec + have hn : NodePlaced st Z.T s (L.cpu s) := J.tcbs ▸ hq.nodes _ hq.cur + have R := J1.pacKeys_stepA (st := st) (flags := flags) (epoch := epoch) ht + (Array.make 10#usize [k0, k1, k2, k3, k4, k5, k6, k7, k8, k9]) + obtain ⟨-, K⟩ := R + have hdec : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (.Ok (), tcbPacPostA st s1 t (Array.make 10#usize [k0, k1, k2, k3, k4, k5, k6, k7, k8, k9]) flags epoch, + a') := by + rw [hcap, decodeTcb_programA, if_neg Bool.false_ne_true, hlbl] + exact tcbSetPacKeys_exactA (ctx := ⟨len, buf, call, slot, extra⟩) (T := Z.T) k0 k1 k2 k3 k4 k5 k6 k7 k8 k9 + hstall hlen hflags hmask hprobe hsupported hepoch hr J.sr.curT (J.sr.core.tcbs _ J.sr.curT) hn hbuf + g0 g1 g2 g3 g4 g5 g6 g7 g8 g9 + obtain ⟨-, -, sysl, symL, -, userOK, j7⟩ := J.decodeFinA hA hM hPl hfm hslot hcov + (rfl : capA st s slot = capA st s slot) hext hSC (lbl := lbl) + (by rw [hcap, hlbl]; simp [DecArmS1aEnt, DecArmQH, DecArmQZ2, DecArmKQ2, DecArmKQ, capFam]) + (fun _ h => by rw [hcap] at h; cases h) (fun _ h => by rw [hcap] at h; cases h) hdec σ1 m + have har : AR a' Z.hw := by + have h := hk.next_pac_epoch a Z.hw J.toJStage3A.j.1.ar + rw [hepoch, WP.spec_ok] at h + exact h + have J2 := K a' har sysl symL userOK j7 + exact ⟨xc, σ1, _, hkc, rfl, hsp, fun f ks => tcbSetPacKeys_specPostA σ1 _ f ks, hdec, Z.Q, Z.R, J2, + Sched.BodyEndA.ofKeep J hkc (Sched.BodyKeepA.refl _)⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3TcbPrioA.lean b/proofs/Fl4ProofsA64/P3/Stage3TcbPrioA.lean new file mode 100644 index 000000000..5e440c450 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3TcbPrioA.lean @@ -0,0 +1,394 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 3e): **TCB SetPriority at the record** (the priority write of a thread in no +ready queue and no IPC queue). + +* **`JStage3A.prioWr_stepA`**: FL4's `PRIO` word write and the spec's `thread_set_priority` keep the record, given + P1's facts at the new state. Glue `JStage3A.of_ipcStepUSA` (`IpcKeepA` lets the priority change); the queue + invariant by `IpcSortedA.of_reorder` (every IPC queue kept, the thread in none); `SchedSRA` by + `SchedSRA.prioWrA`; `EpStateRel` / `NtfnStateRel` by their `prioWrA`. +* **`JSyscallA.setPrioInactiveA`**: `decode_invocation` on a Thread cap with the SetPriority label for an inactive + target at a syscall entry (`tcbSetPriority_inactivePerformA` for the program and the spec). + +Named premises: [P] `ArchTotal`, `MaxSpi`, `ArchPlat`, `A64FrameMasks`, `hCs`; [S] `h1` (`numNodes = 1`, the remote +stall). The decode succeeds (`he`, `hp`, `hac`, `hle`); the `Err` returns are not lifted. New module. +-/ +import Fl4ProofsA64.P3.Stage3PrioSchedA +import Fl4ProofsA64.P3.Stage3PrioIpcA +import Fl4ProofsA64.P3.Stage3TcbMcpA +import Fl4ProofsA64.P3.Stage3IpcGlueUA +import Fl4ProofsA64.P3.Stage3TcbBodyA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-- The spec state after `thread_set_priority`. -/ +def prioStateA (σ : State Unit) (t : ObjRef) (y : Tcb) (p : Priority) : State Unit := + updKSc σ t (.tcb { y with tcbPriority := p }) + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s : S} {a : A} {σ : State Unit} {m : McState} + +set_option maxHeartbeats 4000000 in +/-- **The priority write at the record, for any post state that differs from the entry by the priority of `t` and +by the ready queues and the scheduler action** (the relations given at the post state). -/ +theorem JStage3A.prioG_stepA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {t v : U64} (ht : t ∈ Z.T) {y : Tcb} (hy : σ.kheap (specPtr t) = some (.tcb y)) + (hcq : tsCode y.tcbState ≠ 3 ∧ tsCode y.tcbState ≠ 4) (hnb : ∀ p, y.tcbState ≠ .blockedOnNotification p) + {s' : S} {σ' : State Unit} {Q : U64 → U64 → List U64} + (key : ∀ q, σ'.kheap q = if q = specPtr t then some (.tcb { y with tcbPriority := BitVec.ofNat 8 v.val }) + else σ.kheap q) + (eσ : σ' = { prioStateA σ (specPtr t) y (BitVec.ofNat 8 v.val) with + readyQueues := σ'.readyQueues, schedulerAction := σ'.schedulerAction }) + (wN : WritesIn st s s' (fun b => SchedStepNWA (st := st) N E Rp Z m σ s b ∨ BufW Bf b)) + (tw : ∀ u ∈ Z.T, ∀ O : U64, (O.val < 224 ∨ O.val ∈ [3328, 3336, 3440, 3456, 3472, 3512, 3672]) → O.val % 8 = 0 → + L.mem s' (wa u O) = L.mem s (wa u O)) + (bxi : BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s') + (sysl : SysL st H Z.Cs OS ia ie s') (symL : SymL st H Z.Cs s') + (userOK : Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s') + (j7 : J7A st (J7R2A st ar nA) Z H OS s' σ' m) + (SR : SchedSRA st { Z with Q := Q } m κ s' σ') (ep : EpStateRel Z.T E Rp Bf (L.mem s') σ') + (ntfn : NtfnStateRel N Z.T (L.mem s') σ') (reply : ReplyStackRelA (L.mem s') Rp Z.C σ' (scNameA m) stack) + (sinv : SInvXA σ' m n) (sidle : SIdleA σ' m n) (sv : SchedValidA σ') : + JStage3A st AR { Z with Q := Q } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s' a σ' m := by + set y' : Tcb := { y with tcbPriority := BitVec.ofNat 8 v.val } with hy' + have um : σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory := by rw [eσ]; rfl + have keepO : ∀ q o, σ.kheap q = some o → (∀ z, o ≠ .tcb z) → σ'.kheap q = some o := fun q o h n1 => by + rw [key, if_neg (fun e => by rw [e, hy] at h; exact n1 y (Option.some.inj h).symm)] + exact h + have backO : ∀ q o, σ'.kheap q = some o → (∀ z, o ≠ .tcb z) → σ.kheap q = some o := fun q o h n1 => by + rw [key] at h + split at h + · exact absurd (Option.some.inj h).symm (n1 _) + · exact h + have fwdT : ∀ q z, σ.kheap q = some (.tcb z) → ∃ w, σ'.kheap q = some (.tcb w) ∧ + w = { z with tcbPriority := w.tcbPriority } := by + intro q z h + by_cases e : q = specPtr t + · rw [e, hy] at h + cases h + exact ⟨y', by rw [key, if_pos e], rfl⟩ + · exact ⟨z, by rw [key, if_neg e]; exact h, rfl⟩ + have bwdT : ∀ q w, σ'.kheap q = some (.tcb w) → ∃ z, σ.kheap q = some (.tcb z) ∧ + w = { z with tcbPriority := w.tcbPriority } := by + intro q w h + rw [key] at h + split at h + · rename_i e + cases h + exact ⟨y, e ▸ hy, rfl⟩ + · exact ⟨w, h, rfl⟩ + have HS : SpecSchedA σ σ' := + { kh := fun p => by + by_cases e : p = specPtr t + · subst e + exact Or.inr (Or.inl ⟨y, y', hy, by rw [key, if_pos rfl], rfl, rfl, rfl⟩) + · exact Or.inl (by rw [key, if_neg e]) + irqS := by rw [eσ]; rfl, irqN := by rw [eσ]; rfl, asid := by rw [eσ]; rfl, vcur := by rw [eσ]; rfl + cdt := by rw [eσ]; rfl, orig := by rw [eσ]; rfl } + have SSz : SSzA σ σ' := fun p z k h => ⟨z, backO _ _ h (fun _ c => by cases c)⟩ + have hqE := not_queued_codeA J.ep ht hy hcq + have hqN := Notif.not_member J.ntfn ht hy hnb + have Qk : QBlkA (specPtr '' E) (specPtr '' N) σ σ' := by + refine ⟨J.ipcSorted.of_reorder (t := specPtr t) (yt := y') (by rw [key, if_pos rfl]) + (fun q z e h => ?_) (fun q w e h => ?_) (fun p hp x' h => ?_) (fun p hp x' h => ?_), + fun hA p z k h ne => hA p z k (backO _ _ h (fun _ c => by cases c)) ne, + fun p z h => ⟨z, backO _ _ h (fun _ c => by cases c), id⟩, by rw [eσ]; exact ⟨rfl, rfl, rfl, rfl⟩⟩ + · exact ⟨z, by rw [key, if_neg e]; exact h, rfl⟩ + · rw [key, if_neg e] at h + exact ⟨w, h, rfl⟩ + · obtain ⟨e, he, rfl⟩ := hp + have h0 := backO _ _ h (fun _ c => by cases c) + exact ⟨x', h0, Or.inl ⟨List.Sublist.refl _, hqE e he x' h0⟩⟩ + · obtain ⟨q, hq, rfl⟩ := hp + have h0 := backO _ _ h (fun _ c => by cases c) + exact ⟨x', h0, Or.inl ⟨List.Sublist.refl _, hqN q hq x' h0⟩⟩ + have K : IpcKeepA σ σ' := + ⟨fun p z h => by + obtain ⟨w, hw, e⟩ := fwdT p z h + exact ⟨w, hw, by rw [e], by rw [e], by rw [e], by rw [e], by rw [e], by rw [e]⟩, + fun p z k h => ⟨z, k, keepO _ _ h (fun _ c => by cases c), rfl⟩, + fun p z k h => ⟨z, k, backO _ _ h (fun _ c => by cases c), rfl⟩⟩ + have rv : RunVcpuInv st Z.T s' := fun u hu h0 => by + have e1 : L.mem s' (runVcpuA u) = L.mem s (runVcpuA u) := by + unfold runVcpuA + exact tw u hu 3672#u64 (Or.inr (by simp)) (by simp) + have e2 : L.mem s' (Fl4ProofsA64.P1.stA u) = L.mem s (Fl4ProofsA64.P1.stA u) := by + rw [Fl4ProofsA64.P1.stA_eq] + exact tw u hu 3328#u64 (Or.inr (by simp)) (by simp) + rw [e2]; exact J.runVcpu u hu (e1 ▸ h0) + have blkQ : BlkQueuedA Z.T E N (L.mem s') σ' := by + refine J.blkQ.keepA (fun u hu => ?_) (fun u _ x' h => ?_) (fun q z h => keepO q _ h (fun _ e => by cases e)) + (fun q z h => keepO q _ h (fun _ e => by cases e)) + · rw [Notif.tBlockA_eq]; exact tw u hu 3336#u64 (Or.inr (by simp)) (by simp) + · obtain ⟨z, hz, e⟩ := bwdT _ x' h + exact Or.inr ⟨z, hz, by rw [e]⟩ + exact J.of_ipcStepUSA (Q := Q) (Rl := Z.R) (curOf := Z.curOf) (curScOf := Z.curScOf) wN HS SSz Qk + (fun _ _ => by rw [um]) (fun _ _ => rfl) bxi sysl symL userOK j7 SR ep ntfn reply J.deadExt.reply K + (fun u hu O h1 h2 => tw u hu O (by + rcases h1 with h | h | h | h | h + · exact Or.inl h + · exact Or.inr (by simp [h]) + · exact Or.inr (by simp [h]) + · exact Or.inr (by simp [h]) + · exact Or.inr (by simp [h])) h2) sinv sidle rv blkQ sv + +set_option maxHeartbeats 4000000 in +/-- **The priority write at the record** (module docstring). -/ +theorem JStage3A.prioWr_stepA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {t v : U64} (ht : t ∈ Z.T) (hv : v.val < 256) {y : Tcb} (hy : σ.kheap (specPtr t) = some (.tcb y)) + (hq0 : (L.mem s (inqA t) &&& sched.Q_QUEUED) = 0#u64) + (hcq : tsCode y.tcbState ≠ 3 ∧ tsCode y.tcbState ≠ 4) (hnb : ∀ p, y.tcbState ≠ .blockedOnNotification p) + (bxi : BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac (L.wr s (prioA t) v)) + (sysl : SysL st H Z.Cs OS ia ie (L.wr s (prioA t) v)) (symL : SymL st H Z.Cs (L.wr s (prioA t) v)) + (userOK : Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U (L.wr s (prioA t) v)) + (j7 : J7A st (J7R2A st ar nA) Z H OS (L.wr s (prioA t) v) (prioStateA σ (specPtr t) y (BitVec.ofNat 8 v.val)) m) : + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) + (L.wr s (prioA t) v) a (prioStateA σ (specPtr t) y (BitVec.ofNat 8 v.val)) m := by + set s' := L.wr s (prioA t) v with hs' + set y' : Tcb := { y with tcbPriority := BitVec.ofNat 8 v.val } with hy' + set σ' := prioStateA σ (specPtr t) y (BitVec.ofNat 8 v.val) with hσ' + have hP := J.iplaced + have hta : TcbAddr t := J.sr.core.tcbs t ht + have aP : Aligned (prioA t) := (a_prio hta).1 + have vP := val_prio hta + have off : ∀ b : U64, Aligned b → b.val ≠ t.val + 3448 → L.mem s' b = L.mem s b := fun b hb h => + mem_wr_ne' aP hb (fun e => h (by rw [← e]; exact vP)) + have w1 : WritesIn st s s' (fun b => b = prioA t) := writesIn_wr aP + have tw : ∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → K.val ≠ 3448 → + L.mem s' (wa u K) = L.mem s (wa u K) := fun u hu K h1 h2 h3 => by + have hua : TcbAddr u := J.sr.core.tcbs u hu + refine off _ (field_aligned hua h1 h2) ?_ + rw [Notif.tfield_val hua h1] + by_cases e : u = t + · subst e; omega + · have := tcb_disj hP.e ht hu e + omega + have key : ∀ q, σ'.kheap q = if q = specPtr t then some (.tcb y') else σ.kheap q := by + intro q + simp only [hσ', prioStateA, updKSc_kheap, Function.update_apply] + rfl + have keepO : ∀ q o, σ.kheap q = some o → (∀ z, o ≠ .tcb z) → σ'.kheap q = some o := fun q o h n1 => by + rw [key, if_neg (fun e => by rw [e, hy] at h; exact n1 y (Option.some.inj h).symm)] + exact h + have backO : ∀ q o, σ'.kheap q = some o → (∀ z, o ≠ .tcb z) → σ.kheap q = some o := fun q o h n1 => by + rw [key] at h + split at h + · exact absurd (Option.some.inj h).symm (n1 _) + · exact h + have fwdT : ∀ q z, σ.kheap q = some (.tcb z) → ∃ w, σ'.kheap q = some (.tcb w) ∧ + w = { z with tcbPriority := w.tcbPriority } := by + intro q z h + by_cases e : q = specPtr t + · rw [e, hy] at h + cases h + exact ⟨y', by rw [key, if_pos e], rfl⟩ + · exact ⟨z, by rw [key, if_neg e]; exact h, rfl⟩ + have bwdT : ∀ q w, σ'.kheap q = some (.tcb w) → ∃ z, σ.kheap q = some (.tcb z) ∧ + w = { z with tcbPriority := w.tcbPriority } := by + intro q w h + rw [key] at h + split at h + · rename_i e + cases h + exact ⟨y, e ▸ hy, rfl⟩ + · exact ⟨w, h, rfl⟩ + -- the write set + have wN : WritesIn st s s' (fun b => SchedStepNWA (st := st) N E Rp Z m σ s b ∨ BufW Bf b) := + w1.mono fun b hb => Or.inl (Or.inl (Or.inr (Endp.tcbStepWA_of_off hP.e.tcb ht 3448 (by norm_num) + (by norm_num) (by norm_num) (by norm_num) (by rw [hb]; exact vP)))) + have SR : SchedSRA st Z m κ s' σ' := J.sr.prioWrA ht hy hq0 hv + have ep : EpStateRel Z.T E Rp Bf (L.mem s') σ' := EpStateRel.prioWrA J.ep hP.e ht hy hv + have ntfn : NtfnStateRel N Z.T (L.mem s') σ' := NtfnStateRel.prioWrA J.ntfn hP.n hP.e ht hy hv + have reply : ReplyStackRelA (L.mem s') Rp Z.C σ' (scNameA m) stack := by + refine J.reply.frame (fun r hr => ?_) (fun c _ z k h => ⟨z, k, keepO _ _ h (fun _ e => by cases e), rfl⟩) + (fun r _ x' h => ⟨x', backO _ _ h (fun _ e => by cases e), rfl⟩) + have hr3 := hP.e.rep r hr + have dis := hP.e.rt r hr t ht + have one : ∀ O : U64, O.val = 8 ∨ O.val = 16 → L.mem s' (wa r O) = L.mem s (wa r O) := by + intro O hO + have hv' : (wa r O).val = r.val + O.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact off _ (by unfold Aligned; rw [hv']; omega) (by omega) + exact ⟨one _ (Or.inr (by simp [layout.reply.NEXT])), one _ (Or.inl (by simp [layout.reply.PREV]))⟩ + have sinv : SInvXA σ' m n := by + refine J.sinv.of_fr + { tcb := fun q x h => by + obtain ⟨w, hw, e⟩ := fwdT q x h + exact ⟨w, hw, by rw [e], by rw [e], by rw [e]⟩ + tcb' := fun q w h => by + obtain ⟨z, hz, e⟩ := bwdT q w h + exact ⟨z, hz, by rw [e], by rw [e], by rw [e]⟩ + sc := fun q z k => ⟨fun h => backO q _ h (fun _ e => by cases e), fun h => keepO q _ h (fun _ e => by cases e)⟩ + cur := rfl + idle := rfl + csc := rfl + rq := rfl + rls := rfl + act := rfl, time := rfl, cons := rfl } ?_ + exact J.sinv.irqN.updTcbA hy rfl + have sidle : SIdleA σ' m n := + { f3 := fun c hc => by + obtain ⟨x, hx, hs⟩ := J.sidle.f3 c hc + obtain ⟨w, hw, e⟩ := fwdT _ x hx + exact ⟨w, hw, by rw [e]; exact hs⟩ + f4 := fun z k h => J.sidle.f4 z k (backO _ _ h (fun _ e => by cases e)) + f5 := fun q z k h => J.sidle.f5 q z k (backO _ _ h (fun _ e => by cases e)) + f6 := J.sidle.f6.of_eq fun sl => by + unfold specCapAtA + by_cases e : sl.1 = specPtr t + · rw [key, if_pos e, e, hy] + rfl + · rw [key, if_neg e] + f7 := fun y k h => J.sidle.f7 y k (backO _ _ h (fun _ e => by cases e)) + idleSlot := fun c hc => by + have h0 := J.sidle.idleSlot c hc + unfold vtCapA at h0 ⊢ + by_cases e : m.idleThreadOf c = specPtr t + · rw [key, if_pos e]; rw [e, hy] at h0; exact h0 + · rw [key, if_neg e]; exact h0 + idleBound := fun c hc => by + have h0 := J.sidle.idleBound c hc + unfold vcpuOfA at h0 ⊢ + by_cases e : m.idleThreadOf c = specPtr t + · rw [key, if_pos e]; rw [e, hy] at h0; exact h0 + · rw [key, if_neg e]; exact h0 } + have sv : SchedValidA σ' := J.schedValid.congr rfl rfl rfl rfl rfl rfl + (fun q z k h => keepO q _ h (fun _ e => by cases e)) + (fun u x h => by + obtain ⟨w, hw, e⟩ := fwdT u x h + exact ⟨w, hw, by rw [e]⟩) + (fun u w h => by + obtain ⟨z, hz, e⟩ := bwdT u w h + exact ⟨z, hz, by rw [e], by rw [e]⟩) + exact J.prioG_stepA (Q := Z.Q) ht hy hcq hnb key rfl wN + (fun u hu O h1 h2 => by + have h1' : O.val < 224 ∨ O.val = 3328 ∨ O.val = 3336 ∨ O.val = 3440 ∨ O.val = 3456 ∨ O.val = 3472 ∨ O.val = 3512 ∨ + O.val = 3672 := by simpa using h1 + exact tw u hu O (by omega) h2 (by omega)) + bxi sysl symL userOK j7 SR ep ntfn reply sinv sidle sv + +set_option maxHeartbeats 4000000 in +/-- **TCB SetPriority of an inactive thread at a syscall entry** (module docstring). -/ +theorem JSyscallA.setPrioInactiveA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + {slot lbl len buf t p auth argSlot : U64} {blk call don : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Thread t) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hlbl : lbl = fl4_abi.label.TCBSetPriority) + (he : tcb.extra_with_length ⟨len, buf, call, slot, extra⟩ 0#usize 1#u64 = ok (.Ok argSlot)) + (hp : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 0#u64 = ok p) + (harg : argSlot ∈ OS) (hac : capA st s argSlot = .Thread auth) (hle : ¬ L.mem s (mcpA auth) < p) + {y : Tcb} (hy : σ.kheap (specPtr t) = some (.tcb y)) (hin : y.tcbState = .inactive) : + ∃ xc σ1 σ2 s2, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + (∀ κs : CslotPtr, (.ok [], σ2) ∈ ((fl4InvokeTcb (.threadControlSched (specPtr t) κs none none + (some (BitVec.ofNat 8 p.val, specPtr auth)) none)).run σ1).1) ∧ + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (.Ok (), s2, a) ∧ + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s2 a σ2 m ∧ Sched.BodyEndA σ σ2 m := by + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hslot hcov J.actW J.running J.cur + have hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM := J.toJStage3EntryA.curNotVmA + obtain ⟨xc, hkc, hst, hsp, J1, -, -, -⟩ := J.toJStage3A.of_restartCurA RS.pl.choose_spec.choose_spec.choose_spec.choose_spec + RS.sym RS.run hnvm RS.cur RS.vs RS.vc + have ht : t ∈ Z.T := J.tcbs ▸ (J.toJStage3A.slotCapTy hslot).1.2.2.2.2.2 t hcap + have haT : auth ∈ Z.T := J.tcbs ▸ (J.toJStage3A.slotCapTy harg).1.2.2.2.2.2 auth hac + have T0 := J.toJStage3A.toTA J.sact J.noSwitch + have hstall : smp.remote_tcb_stall st ar s a t = ok (s, a) := T0.stall_singleA hCs h1 ht + set s1 := L.wr s (stA (L.curW s)) sched.TS_RESTART with hs1 + set σ1 := stUpdA σ σ.curThread xc .restart with hσ1 + have hr : tcb.restart_caller st s = ok s1 := by + unfold tcb.restart_caller + rw [cur_thread_ok, bind_ok, hst] + have ha : tcb.thread_arg st s argSlot 1#u64 = ok (.Ok auth) := by + unfold tcb.thread_arg + rw [capA_eq, bind_ok, hac] + -- the target is not the current thread (inactive vs running) + have htc : specPtr t ≠ σ.curThread := fun e => by + have ec : t = L.curW s := specPtr_inj (e.trans J.sr.sch.curThread) + obtain ⟨z, hz, r0⟩ := J.ep.tcb t ht + rw [hy] at hz; cases hz + have h := J.running + rw [← ec] at h + have c := r0.st + rw [hin] at c + unfold Fl4ProofsA64.P1.Runnable at h + rcases h with h | h | h <;> rw [show Fl4ProofsA64.P1.stA t = stA t from rfl] at * <;> + simp_all [Notif.tsCode, sched.TS_RUNNING, sched.TS_RESTART, sched.TS_RUNNING_VM] + have hy1 : σ1.kheap (specPtr t) = some (.tcb y) := by + rw [hσ1]; simp [stUpdA, setObjA, Function.update_of_ne htc]; exact hy + have hSt : StEqA Z.T (L.mem s1) σ1 := fun u hu z hz => (J1.ep.at_tcb hu hz).st + have P := fun κs : CslotPtr => tcbSetPriority_inactivePerformA (slot := κs) (T := Z.T) (ctx := ⟨len, buf, call, slot, extra⟩) + hstall he hp ha hle hr J.mcp haT hSt ht hy1 hin + have hWidth : p.val < 256 := by + obtain ⟨x, -, hw⟩ := J.mcp auth haT + have hv : p.val ≤ (L.mem s (mcpA auth)).val := Nat.le_of_not_lt hle + rw [hw.1] at hv + exact Nat.lt_of_le_of_lt hv x.tcbMcpriority.isLt + -- the target is in no ready queue + have hon1 : L.mem s1 (affA t) = L.cpu s1 := by + have x := hCs _ (J1.sr.core.aff t ht) + have y := hCs _ J1.sr.sch.cpuCs + exact UScalar.eq_of_val_eq (by omega) + have hnq : ∀ d q, specPtr t ∉ σ1.readyQueues d q := fun d q hm => by + obtain ⟨z, hz, hr⟩ := J1.sr.run.1 d q _ hm + rw [hy1] at hz; cases hz + rw [hin] at hr; cases hr + have hq0 : (L.mem s1 (inqA t) &&& sched.Q_QUEUED) = 0#u64 := J1.sr.toSchedSRN.qBit0 ht hon1 hnq + -- BlkOK at the end of set_priority + have hsp1 : sched.set_priority st s1 t p = ok (L.wr s1 (prioA t) p) := by + have hWord := hSt t ht y hy1 + rw [hin] at hWord + have hZero : L.mem s1 (stA t) = 0#u64 := UScalar.eq_of_val_eq hWord + rw [sched.set_priority, eq_of_spec_fd (Fl4ProofsA64.P1.obj.Tcb.state.spec s1 t), bind_ok, hZero] + simp (config := {decide := true}) only [sched.TS_RUNNING, sched.TS_RESTART, + sched.TS_RUNNING_VM, sched.TS_BLOCKED_ON_RECV, sched.TS_BLOCKED_ON_SEND, + sched.TS_BLOCKED_ON_NTFN, ite_false] + exact eq_of_spec_fd (obj.Tcb.set_prio.spec s1 t p) + have hdec : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (.Ok (), L.wr s1 (prioA t) p, a) := by + rw [hcap, decodeTcb_programA, if_neg Bool.false_ne_true, hlbl, tcbSetPriority_postA hstall he hp ha hle hr, + hsp1, bind_ok] + have htH : t ∈ H.tcbs := by rw [J1.tcbs]; exact ht + have hcurNe : t ≠ L.mem s1 (L.nodeAt s1 (L.cpu s1) layout.node.CUR_THREAD) := fun e => htc (by + rw [show L.mem s1 (L.nodeAt s1 (L.cpu s1) layout.node.CUR_THREAD) = L.curW s1 from rfl] at e + rw [e, ← J1.sr.sch.curThread] + rfl) + have blk : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac (L.wr s1 (prioA t) p) := by + have h := sched.set_priority.blk (st := st) s1 t p (BXI.blk J1.sys.ent.bxi) htH hWidth + (fun e => absurd e hcurNe) (fun e => absurd e hcurNe) (fun e => absurd e hcurNe) + rw [hsp1, WP.spec_ok] at h + exact h.1 + obtain ⟨-, -, sysl, symL, -, userOK, j7⟩ := J.decodeFinA hA hM hPl hfm hslot hcov + (rfl : capA st s slot = capA st s slot) hext hSC (lbl := lbl) + (by rw [hcap, hlbl]; simp [DecArmS1aEnt, DecArmQH, DecArmQZ2, DecArmKQ2, DecArmKQ, DecArmKQ3, DecArmDelQ, + DecArmS1aC, capFam]) + (fun _ h => by rw [hcap] at h; cases h) (fun _ h => by rw [hcap] at h; cases h) hdec + (prioStateA σ1 (specPtr t) y (BitVec.ofNat 8 p.val)) m + have J2 := J1.prioWr_stepA (st := st) ht hWidth hy1 hq0 + (by rw [hin]; simp [Notif.tsCode]) (fun q h => by rw [hin] at h; cases h) + (BlkOK.bxi blk) sysl symL userOK j7 + refine ⟨xc, σ1, _, _, hkc, rfl, hsp, fun κs => ?_, hdec, J2, Sched.BodyEndA.ofTcbUpd J hkc hy1 rfl⟩ + have e := (P κs).2.1 + rw [e] + rfl + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3TcbPrioAnyA.lean b/proofs/Fl4ProofsA64/P3/Stage3TcbPrioAnyA.lean new file mode 100644 index 000000000..f140e9216 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3TcbPrioAnyA.lean @@ -0,0 +1,269 @@ +/- +P3 (aarch64, A64-P3 Lift 3b labels): **`set_priority` of a non-current thread from any record state**. + +SetPriority's arms (`JSyscallA.setPrio*A`) split the target's state at the entry. A label that runs `set_priority` after +other steps (SetSchedParams, after the fault-handler deletion and `set_mcp`) needs the split at its own state. + +* **`JStage3A.prioAnyA`**: from `J` at any record state, for a target that is not the current thread and no core's + idle thread: the concrete and spec `set_priority`, a `DelStepA` body step, `BlkOK`, and the record at the post state + given the P1 facts there. Cases: queued (`prioQ_stepA`, as `JSyscallA.setPrioQueuedA`), inactive / reply-blocked / + runnable and not queued (`prioWr_stepA`), endpoint-blocked (`prioEp_stepA`), notification-blocked + (`prioNtfn_stepA`); the idle-thread state is excluded by `SInvXA.onlyIdle`. + +Named premises: [P] `hmb`, `hmax`, `htu` (as `JSyscallA.setPrioEpA`), `hCs`; [S] `h1`. New module. +-/ +import Fl4ProofsA64.P3.Stage3TcbPrioQArmA +import Fl4ProofsA64.P3.Stage3TcbPrioPlainA +import Fl4ProofsA64.P3.Stage3TcbPrioEpArmA +import Fl4ProofsA64.P3.Stage3TcbPrioNtfnA +import Fl4ProofsA64.P3.Stage3LiftTcbTimeoutNnA +import Fl4ProofsA64.P3.Stage3SetPrioFrA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s : S} {a : A} {σ : State Unit} {m : McState} + +/-- A step that replaces one TCB and sets `choose_new_thread` (`rescheduleRequired`) is a deletion body step. -/ +theorem delStep_ofChooseA {σ σ' : State Unit} {p : ObjRef} {y y' : Tcb} (hy : σ.kheap p = some (.tcb y)) + (hkh : σ'.kheap = Function.update σ.kheap p (some (.tcb y'))) + (hact : σ'.schedulerAction = .chooseNewThread) (htime : σ'.curTime = σ.curTime) + (hlast : σ'.machineState.lastMachineTime = σ.machineState.lastMachineTime) + (hcur : σ'.curThread = σ.curThread) (hcsc : σ'.curSc = σ.curSc) (hcons : σ'.consumedTime = σ.consumedTime) + (hrel : σ'.releaseQueue = σ.releaseQueue) (off : CtOffA σ.curThread σ → CtOffA σ.curThread σ') + (ctSc : CtScKA σ.curThread σ σ') : DelStepA σ σ' where + cur := hcur + time := htime + last := hlast + csc := hcsc + cons := hcons + rel := fun u hu => by rw [hrel] at hu; exact hu + sc := fun y2 k h => by + have ne : σ.curSc ≠ p := fun e => by + rw [hkh, e, Function.update_self] at h; cases h + rw [hkh, Function.update_of_ne ne] at h + exact ⟨y2, h, Or.inl ⟨rfl, rfl, rfl⟩⟩ + act := Or.inr hact + ctSt := fun h => by rw [hact] at h; cases h + off := off + ctSc := ctSc + +set_option maxHeartbeats 8000000 in +/-- **`set_priority` of a non-current thread from any record state** (module docstring). -/ +theorem JStage3A.prioAnyA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (hns : ∀ u, σ.schedulerAction ≠ .switchThread u) (sact : SActA σ) + {t p : U64} (ht : t ∈ Z.T) (hWidth : p.val < 256) (htc : specPtr t ≠ σ.curThread) + (hidles : ∀ c < n, m.idleThreadOf c ≠ specPtr t) : + ∃ s3 σ3, sched.set_priority st s t p = ok s3 ∧ + setPriority (Z := Unit) (specPtr t) (BitVec.ofNat 8 p.val) σ = ({((), σ3)}, False) ∧ DelStepA σ σ3 ∧ + BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s3 ∧ + (SysL st H Z.Cs OS ia ie s3 → SymL st H Z.Cs s3 → Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s3 → + J7A st (J7R2A st ar nA) Z H OS s3 σ3 m → + ∃ Q Rl, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s3 a σ3 m) := by + obtain ⟨y, hy, -⟩ := J.mcp t ht + have hSt : StEqA Z.T (L.mem s) σ := fun u hu z hz => (J.ep.at_tcb hu hz).st + have hon : L.mem s (affA t) = L.cpu s := by + have x := hCs _ (J.sr.core.aff t ht) + have y := hCs _ J.sr.sch.cpuCs + exact UScalar.eq_of_val_eq (by omega) + have hcurNe : t ≠ L.mem s (L.nodeAt s (L.cpu s) layout.node.CUR_THREAD) := fun e => htc (by + rw [show L.mem s (L.nodeAt s (L.cpu s) layout.node.CUR_THREAD) = L.curW s from rfl] at e + rw [e, ← J.sr.sch.curThread]) + have htH : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + have blkOf : ∀ s3, sched.set_priority st s t p = ok s3 → + BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s3 := fun s3 e => by + have h := sched.set_priority.blk (st := st) s t p (BXI.blk J.sys.ent.bxi) htH hWidth + (fun e => absurd e hcurNe) (fun e => absurd e hcurNe) (fun e => absurd e hcurNe) + rw [e, WP.spec_ok] at h + exact h.1 + have hc : m.curCore < fl4A64Platform.numNodes := by rw [J.sr.sch.core]; exact hCs _ J.sr.sch.cpuCs + by_cases hqd : ∃ d q, specPtr t ∈ σ.readyQueues d q + · -- queued: dequeue, write, enqueue, reschedule + have hrun : runnable y.tcbState = true := by + obtain ⟨d, q, hm⟩ := hqd + obtain ⟨z, hz, hr⟩ := J.sinv.rq d q _ hm + rw [hy] at hz; cases hz; exact hr + have hcq : tsCode y.tcbState ≠ 3 ∧ tsCode y.tcbState ≠ 4 := by + revert hrun + cases y.tcbState <;> simp [Notif.tsCode, runnable] + have hnb : ∀ q, y.tcbState ≠ .blockedOnNotification q := fun q h => by + rw [h] at hrun; simp [runnable] at hrun + have hqc1 : specPtr t ∈ σ.readyQueues y.tcbDomain y.tcbPriority ∨ specPtr t = σ.curThread := by + obtain ⟨d, q, hm⟩ := hqd + left + obtain ⟨hd, hq⟩ := rqDomPrio_of_schedA J.sr _ d q y hy hm + rw [← hd, ← hq]; exact hm + have hqcC : (L.mem s (inqA t) &&& sched.Q_QUEUED) ≠ 0#u64 ∨ t = L.curW s := by + rcases hqc1 with hm | h + · exact Or.inl (J.sr.qBitNeA ht hon hy hm) + · exact absurd h htc + have L1 : LoopInvA σ m n := ⟨J.sinv, J.sidle, sact⟩ + have hT : TgtA (specPtr t) σ := by + rcases hqc1 with hm | hc' + · exact J.schedValid.rq _ _ _ hm + · exact absurd hc' htc + obtain ⟨sd, ed, σd, d, kd, rd, -, SRd, Fd⟩ := exists_of_specA (dequeue_refines_localA J.sr ht hon) + have hond := yieldAffKeepA J.sr SRd ht ht hon + have dm : ((), σd) ∈ (tcbSchedAction (Z := Unit) tcbSchedDequeue (specPtr t) σ).1 := by + rw [d]; exact Set.mem_singleton _ + have hnqd : NoQ (specPtr t) σd := + (deqD (specPtr t) (fun _ => True) (fun _ _ _ => trivial) σ + ⟨Sched.SchedValidA.toFIA J.schedValid, rqDomPrio_of_schedA J.sr, trivial⟩ _ dm).2.2 + have hq0 : (L.mem sd (inqA t) &&& sched.Q_QUEUED) = 0#u64 := SRd.toSchedSRN.qBit0 ht hond hnqd + have Sd := tcbSchedAction_shapeA tcbSchedDequeue (fun _ _ _ h => Or.inr (List.mem_filter.1 h).1) (specPtr t) dm + have hyd : σd.kheap (specPtr t) = some (.tcb y) := by rw [congrArg (fun z => z.kheap) Sd.1.1]; exact hy + have states_d : StEqA Z.T (L.mem sd) σd := + hSt.keep (fun u hu => by + rw [stA_eq] + exact yieldWordKeepA J.sr.toSchedSRN J.sr.curT (Fd.wr.mono fun _ h => Or.inl h) hu (Or.inl rfl)) + (fun u x hx => ⟨x, by rw [← congrArg (fun z => z.kheap) Sd.1.1]; exact hx, rfl⟩) + obtain ⟨SR2, S2⟩ := prioStageA SRd states_d ht hyd hq0 hWidth + have hon2 := yieldAffKeepA SRd SR2 ht ht hond + have hrunp : ∀ x, (prioStateA σd (specPtr t) y (BitVec.ofNat 8 p.val)).kheap (specPtr t) = some (.tcb x) → + runnable x.tcbState = true := fun x hx => by + rw [prioKhA, if_pos rfl] at hx + obtain rfl := KernelObject.tcb.inj (Option.some.inj hx) + exact hrun + have hnrp : specPtr t ∉ (prioStateA σd (specPtr t) y (BitVec.ofNat 8 p.val)).releaseQueue := by + show specPtr t ∉ σd.releaseQueue + rw [rd]; exact hT.2 + obtain ⟨se, ee, σe, e, ke, re, hae, wE, SRe⟩ := exists_of_specA + (enqueue_refines_localA SR2 t ht hon2 (fun _ => (SR2.relOn ht hon2).2 hnrp) hrunp) + have FE := MFrA.of_sw (Z := { Z with Q := deqQA (st := st) s t Z.Q }) (m := m) ke + (wE.mono fun _ h => SR2.toSchedSRN.enq_swr ht h) + have states_e : StEqA Z.T (L.mem se) σe := + S2.keep (fun u hu => by + rw [stA_eq] + exact yieldWordKeepA SR2.toSchedSRN SR2.curT (FE.wr.mono fun _ h => Or.inl h) hu (Or.inl rfl)) + (fun u x hx => ⟨x, by rw [ke] at hx; exact hx, rfl⟩) + obtain ⟨sr, er, σr, Qr, r, kr, rr, wR, SRr⟩ := exists_of_specA (reschedule_required_refinesA SRe states_e) + have FR := MFrA.of_sw (Z := { Z with Q := enqQA st (L.wr sd (prioA t) p) t (deqQA (st := st) s t Z.Q) }) (m := m) + kr wR + have F2 := FE.trans FR rfl rfl + have em : ((), σe) ∈ (tcbSchedAction (Z := Unit) tcbSchedEnqueue (specPtr t) + (prioStateA σd (specPtr t) y (BitVec.ofNat 8 p.val))).1 := by rw [e]; exact Set.mem_singleton _ + have rm : ((), σr) ∈ (rescheduleRequired (Z := Unit) σe).1 := by rw [r]; exact Set.mem_singleton _ + have Se := tcbSchedAction_enq_memA (specPtr t) em + have Rs := rescheduleRequired_memA rm + have eqd : σd = { σ with readyQueues := σd.readyQueues, schedulerAction := σd.schedulerAction } := Sd.1.1 + have hσe : σe = { prioStateA σd (specPtr t) y (BitVec.ofNat 8 p.val) with + readyQueues := σe.readyQueues, schedulerAction := σe.schedulerAction } := Se.1.1 + have hσr : σr = { σe with readyQueues := σr.readyQueues, schedulerAction := σr.schedulerAction } := Rs.1.1 + have eqr : σr = { prioStateA σd (specPtr t) y (BitVec.ofNat 8 p.val) with + readyQueues := σr.readyQueues, schedulerAction := σr.schedulerAction } := + hσr.trans (by rw [hσe]) + obtain ⟨LI, sv, -, -⟩ := prioQ_specInvA L1 J.schedValid (rqDomPrio_of_schedA J.sr) hy hrun hT hidles d rfl e r + have hsp1 : sched.set_priority st s t p = ok sr := + setPriority_queuedA (hSt t ht y hy) hrun hqcC ed ee er + have hSet := setPriority_queuedSpecA (σp := prioStateA σd (specPtr t) y (BitVec.ofNat 8 p.val)) + (BitVec.ofNat 8 p.val) hy hrun hqc1 d (congrArg (fun z => z.kheap) eqd) rfl e r + have kh : σr.kheap = Function.update σ.kheap (specPtr t) + (some (.tcb { y with tcbPriority := BitVec.ofNat 8 p.val })) := by + have h := congrArg (fun z => z.kheap) eqr + simp only [prioStateA, updKSc] at h + rw [h, kd] + have CK := Sched.setPriority_ckA hSet htc + have DS : DelStepA σ σr := delStep_ofChooseA hy kh Rs.2 + ((congrArg (fun z => z.curTime) eqr).trans (congrArg (fun z => z.curTime) eqd)) + ((congrArg (fun z => z.machineState.lastMachineTime) eqr).trans + (congrArg (fun z => z.machineState.lastMachineTime) eqd)) + ((congrArg (fun z => z.curThread) eqr).trans (congrArg (fun z => z.curThread) eqd)) + ((congrArg (fun z => z.curSc) eqr).trans (congrArg (fun z => z.curSc) eqd)) + ((congrArg (fun z => z.consumedTime) eqr).trans (congrArg (fun z => z.consumedTime) eqd)) + ((congrArg (fun z => z.releaseQueue) eqr).trans (congrArg (fun z => z.releaseQueue) eqd)) CK.off CK.ctSc + refine ⟨sr, σr, hsp1, hSet, DS, blkOf sr hsp1, fun sysl symL userOK j7 => ⟨Qr, Z.R, ?_⟩⟩ + exact J.prioQ_stepA ht hWidth hy hcq hnb SRd Fd eqd hq0 F2 SRr eqr LI sv (BlkOK.bxi (blkOf sr hsp1)) sysl symL + userOK j7 + · have hnq : ∀ d q, specPtr t ∉ σ.readyQueues d q := fun d q hm => hqd ⟨d, q, hm⟩ + have hq0 : (L.mem s (inqA t) &&& sched.Q_QUEUED) = 0#u64 := J.sr.toSchedSRN.qBit0 ht hon hnq + have hns' : ∀ u, σ.schedulerAction ≠ .switchThread u := hns + -- the endpoint- and notification-blocked arms + have arm : ∀ (s3 : S) (σ3 : State Unit), sched.set_priority st s t p = ok s3 → + setPriority (Z := Unit) (specPtr t) (BitVec.ofNat 8 p.val) σ = ({((), σ3)}, False) → Sched.BodyKeepA σ σ3 → + (BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s3 → + SysL st H Z.Cs OS ia ie s3 → SymL st H Z.Cs s3 → Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s3 → + J7A st (J7R2A st ar nA) Z H OS s3 σ3 m → + ∃ Q Rl, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s3 a σ3 m) → + ∃ s3 σ3, sched.set_priority st s t p = ok s3 ∧ + setPriority (Z := Unit) (specPtr t) (BitVec.ofNat 8 p.val) σ = ({((), σ3)}, False) ∧ DelStepA σ σ3 ∧ + BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s3 ∧ + (SysL st H Z.Cs OS ia ie s3 → SymL st H Z.Cs s3 → + Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s3 → J7A st (J7R2A st ar nA) Z H OS s3 σ3 m → + ∃ Q Rl, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s3 a σ3 m) := + fun s3 σ3 h1 h2 hK hJ => ⟨s3, σ3, h1, h2, + delStep_ofKeepA hK (Sched.setPriority_ckA h2 htc).off (Sched.setPriority_ckA h2 htc).ctSc, blkOf s3 h1, + fun sysl symL userOK j7 => hJ (BlkOK.bxi (blkOf s3 h1)) sysl symL userOK j7⟩ + -- the plain arm: one priority word + have plain : (y.tcbState = .inactive ∨ (∃ r, y.tcbState = .blockedOnReply r) ∨ + (runnable y.tcbState = true ∧ ∀ d q, specPtr t ∉ σ.readyQueues d q)) → + ∃ s3 σ3, sched.set_priority st s t p = ok s3 ∧ + setPriority (Z := Unit) (specPtr t) (BitVec.ofNat 8 p.val) σ = ({((), σ3)}, False) ∧ DelStepA σ σ3 ∧ + BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s3 ∧ + (SysL st H Z.Cs OS ia ie s3 → SymL st H Z.Cs s3 → + Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s3 → J7A st (J7R2A st ar nA) Z H OS s3 σ3 m → + ∃ Q Rl, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s3 a σ3 m) := fun hpl => by + have hcq : tsCode y.tcbState ≠ 3 ∧ tsCode y.tcbState ≠ 4 := by + rcases hpl with h | ⟨r, h⟩ | ⟨h, -⟩ + · rw [h]; simp [Notif.tsCode] + · rw [h]; simp [Notif.tsCode] + · revert h + cases y.tcbState <;> simp [Notif.tsCode, runnable] + have hnb : ∀ q, y.tcbState ≠ .blockedOnNotification q := fun q h => by + rcases hpl with h' | ⟨r, h'⟩ | ⟨h', -⟩ + · rw [h'] at h; cases h + · rw [h'] at h; cases h + · rw [h] at h'; cases h' + have hsp1 : sched.set_priority st s t p = ok (L.wr s (prioA t) p) := + setPriority_wordA (hSt t ht y hy) hpl hq0 (fun e => hcurNe e) + have hS := setPriority_plainA (BitVec.ofNat 8 p.val) hy hpl (fun e => htc e) hnq + have CK := Sched.setPriority_ckA hS htc + refine ⟨_, _, hsp1, hS, + delStep_ofKeepA (BodyKeepA.ofTcbUpd hy rfl) CK.off CK.ctSc, blkOf _ hsp1, fun sysl symL userOK j7 => ⟨Z.Q, Z.R, ?_⟩⟩ + exact J.prioWr_stepA (st := st) ht hWidth hy hq0 hcq hnb (BlkOK.bxi (blkOf _ hsp1)) sysl symL userOK j7 + cases hs : y.tcbState with + | running => exact plain (Or.inr (Or.inr ⟨by rw [hs]; rfl, hnq⟩)) + | restart => exact plain (Or.inr (Or.inr ⟨by rw [hs]; rfl, hnq⟩)) + | ext e => exact plain (Or.inr (Or.inr ⟨by rw [hs]; cases e; rfl, hnq⟩)) + | inactive => exact plain (Or.inl hs) + | blockedOnReply r => exact plain (Or.inr (Or.inl ⟨r, hs⟩)) + | idleThreadState => + exfalso + obtain ⟨c, hc', e⟩ := J.sinv.onlyIdle _ _ hy hs + exact hidles c hc' e.symm + | blockedOnReceive e r d => + obtain ⟨s3, σ3, h1, h2, hK, hJ⟩ := J.prioEp_stepA (st := st) (ar := ar) hc hmb hmax htu hns' ht hWidth hy hq0 + (p := e) (by rw [hs]; rfl) + exact arm s3 σ3 h1 h2 hK hJ + | blockedOnSend e d => + obtain ⟨s3, σ3, h1, h2, hK, hJ⟩ := J.prioEp_stepA (st := st) (ar := ar) hc hmb hmax htu hns' ht hWidth hy hq0 + (p := e) (by rw [hs]; rfl) + exact arm s3 σ3 h1 h2 hK hJ + | blockedOnNotification q => + obtain ⟨s3, σ3, h1, h2, hK, hJ⟩ := J.prioNtfn_stepA (st := st) (ar := ar) hc hmb hmax htu hns' ht hWidth hy hq0 + hs + exact arm s3 σ3 h1 h2 hK hJ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3TcbPrioEpA.lean b/proofs/Fl4ProofsA64/P3/Stage3TcbPrioEpA.lean new file mode 100644 index 000000000..99e889244 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3TcbPrioEpA.lean @@ -0,0 +1,472 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 3e): **TCB SetPriority of a thread blocked on an endpoint, at the record.** + +* **`JStage3A.prioEp_stepA`**: FL4's `PRIO` word write and `reorder_ep` refine `thread_set_priority` and + `reorder_ep`, and keep the record given P1's facts at the new state (`of_ipcStepUSA`; the queue invariant by + `IpcSortedA.of_reorder`). + +Named premises as the blocking-send arm ([P] `hmb`, `hmax`, `htu`); [S] `hns` (no pending switch). New module. +-/ +import Fl4ProofsA64.P3.Stage3TcbPrioPlainA +import Fl4ProofsA64.P3.Stage3SendBlkA +import Fl4ProofsA64.P3.Stage3TcbBodyA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s : S} {a : A} {σ : State Unit} {m : McState} + +set_option maxHeartbeats 4000000 in +/-- **SetPriority of an endpoint-blocked thread at the record** (module docstring). -/ +theorem JStage3A.prioEp_stepA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (hc : m.curCore < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (hns : ∀ u, σ.schedulerAction ≠ .switchThread u) + {t v : U64} (ht : t ∈ Z.T) (hv : v.val < 256) {y : Tcb} (hy : σ.kheap (specPtr t) = some (.tcb y)) + (hq0 : (L.mem s (inqA t) &&& sched.Q_QUEUED) = 0#u64) + {p : ObjRef} (hb : epBlocked y.tcbState = some p) : + ∃ s3 σ3, sched.set_priority st s t v = ok s3 ∧ + setPriority (Z := Unit) (specPtr t) (BitVec.ofNat 8 v.val) σ = ({((), σ3)}, False) ∧ Sched.BodyKeepA σ σ3 ∧ + (BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s3 → + SysL st H Z.Cs OS ia ie s3 → SymL st H Z.Cs s3 → Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s3 → + J7A st (J7R2A st ar nA) Z H OS s3 σ3 m → + ∃ Q Rl, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s3 a σ3 m) := by + have hP := J.iplaced + have hta : TcbAddr t := J.sr.core.tcbs t ht + have aP : Aligned (prioA t) := (a_prio hta).1 + have vP := val_prio hta + have hbst : (∃ e r d, y.tcbState = .blockedOnReceive e r d) ∨ ∃ e d, y.tcbState = .blockedOnSend e d := by + revert hb + cases y.tcbState <;> simp [epBlocked] + obtain ⟨hE, x, hk, htq⟩ := J.blkQ.ep t ht y hy p hb + obtain ⟨z0, hz0, r0⟩ := J.ep.tcb t ht + rw [hy] at hz0; cases hz0 + set e := L.mem s (Notif.tBlockA t) with hedef + have hpe : specPtr e = p := r0.blk p hb + subst hpe + have htp : specPtr t ≠ specPtr e := fun h => by rw [h, hk] at hy; cases hy + set s2 := L.wr s (prioA t) v with hs2 + set y' : Tcb := { y with tcbPriority := BitVec.ofNat 8 v.val } with hy' + set σ2 := prioStateA σ (specPtr t) y (BitVec.ofNat 8 v.val) with hσ2 + have off : ∀ b : U64, Aligned b → b.val ≠ t.val + 3448 → L.mem s2 b = L.mem s b := fun b hb h => + mem_wr_ne' aP hb (fun e => h (by rw [← e]; exact vP)) + have key2 : ∀ q, σ2.kheap q = if q = specPtr t then some (.tcb y') else σ.kheap q := by + intro q + simp only [hσ2, prioStateA, updKSc_kheap, Function.update_apply] + rfl + have hk2 : σ2.kheap (specPtr e) = some (.endpoint x) := by rw [key2, if_neg htp.symm]; exact hk + have hy2 : σ2.kheap (specPtr t) = some (.tcb y') := by rw [key2, if_pos rfl] + -- the relations after the priority write + have ep2 : EpStateRel Z.T E Rp Bf (L.mem s2) σ2 := EpStateRel.prioWrA J.ep hP.e ht hy hv + have ntfn2 : NtfnStateRel N Z.T (L.mem s2) σ2 := NtfnStateRel.prioWrA J.ntfn hP.n hP.e ht hy hv + have sr2 : IpcSchedRelA st Z m κ s2 σ2 := ⟨Z.Q, Z.R, J.sr.prioWrA ht hy hq0 hv⟩ + have hO : SchedOpsEA st Z.T E Rp Bf (IpcSchedRelA st Z m κ) := + (ipcSchedOpsA (ar := ar) J.iplaced J.ipcPlace hc hmb hmax htu).2.2.1 + have hx1 := J.ep.at_ep hE hk + obtain ⟨zs, hQ1, hzs, hB1⟩ := hx1.queue + have htzs : t ∈ zs := by + rw [hzs] at htq; obtain ⟨u, hu, eu⟩ := List.mem_map.1 htq; rw [← specPtr_inj eu]; exact hu + have hsort : ∀ xs, QueueRel (L.mem s2) Z.T .Ep (qOf (L.mem s2) e) xs → + P1.PrioSorted (fun u => L.mem s2 (prioA u)) (xs.erase t) := by + intro xs hxs + obtain ⟨ys, g1, g2, -⟩ := (ep2.at_ep hE hk2).queue + have e1 : xs = ys := hxs.unique hP.e.tcb g1 + have e2 : ys = zs := List.map_injective_iff.2 (fun a b h => specPtr_inj h) (g2.symm.trans hzs) + rw [e1, e2] + have hs := prioSorted_of_specA hP.e.tcb J.ep hx1 (J.ipcSorted.1 (specPtr e) ⟨e, hE, rfl⟩ x hk) zs hQ1 + have hs' : P1.PrioSorted (fun u => L.mem s (prioA u)) (zs.erase t) := List.Pairwise.sublist List.erase_sublist hs + have same : ∀ u ∈ zs.erase t, L.mem s2 (prioA u) = L.mem s (prioA u) := fun u hu => by + have hne : u ≠ t := fun h => hQ1.nodup.not_mem_erase (h ▸ hu) + have huT := hQ1.mem u (List.mem_of_mem_erase hu) + have hua := hP.e.tcb u huT + refine off _ (a_prio hua).1 ?_ + rw [val_prio hua] + have := tcb_disj hP.e ht huT hne + omega + refine List.Pairwise.imp_of_mem (fun {a b} ha hb h => ?_) hs' + dsimp only + rw [same a ha, same b hb] + exact h + obtain ⟨s3, h3, σ3, hspec, ep3, sr3, w23, ⟨yn, eσ3, hmemn⟩, eform, W, hW, hfr⟩ := exists_of_specA + (ep_reorder_relA (st := st) hO.to0 hP.e s2 σ2 ep2 sr2 ht hy2 hE hk2 htq hsort) + have eyn : yn = updateEpQueue x + (specIns (removeAll (specPtr t) x.epQueue) (prioOf σ2) y'.tcbPriority (specPtr t)) true := by + have h := congrArg (fun σ : State Unit => σ.kheap (specPtr e)) (eσ3.symm.trans eform) + simpa [updK] using h + have key3 : ∀ q, σ3.kheap q = if q = specPtr e then some (.endpoint yn) + else if q = specPtr t then some (.tcb y') else σ.kheap q := by + intro q + rw [eσ3, ← key2] + simp only [updK, Function.update_apply] + have hy3 : σ3.kheap (specPtr t) = some (.tcb y') := by rw [key3, if_neg htp, if_pos rfl] + have hK : Sched.BodyKeepA σ σ3 := by + refine Sched.BodyKeepA.ofKh (by rw [eσ3]; rfl) (by rw [eσ3]; rfl) (by rw [eσ3]; rfl) (by rw [eσ3]; rfl) + (by rw [eσ3]; rfl) (by rw [eσ3]; rfl) (by rw [eσ3]; rfl) (fun q => ?_) + by_cases c1 : q = specPtr e + · refine Or.inr (Or.inr fun o ho => ?_) + rw [key3, if_pos c1] at ho + cases ho + exact ⟨fun _ _ h => (by cases h), fun _ h => (by cases h)⟩ + · by_cases c2 : q = specPtr t + · subst c2 + exact Or.inr (Or.inl ⟨y, y', hy, hy3, rfl⟩) + · exact Or.inl (by rw [key3, if_neg c1, if_neg c2]) + -- the concrete program + have hsp : sched.set_priority st s t v = ok s3 := by + have hw : L.mem s (stA t) = sched.TS_BLOCKED_ON_RECV ∨ L.mem s (stA t) = sched.TS_BLOCKED_ON_SEND := by + have c := r0.st + rcases hbst with ⟨e', r, d, h⟩ | ⟨e', d, h⟩ + · rw [h] at c + exact Or.inl (UScalar.eq_of_val_eq (by simpa [Notif.tsCode, sched.TS_BLOCKED_ON_RECV] using c)) + · rw [h] at c + exact Or.inr (UScalar.eq_of_val_eq (by simpa [Notif.tsCode, sched.TS_BLOCKED_ON_SEND] using c)) + have hsetp : obj.Tcb.set_prio st t s v = ok s2 := eq_of_spec_fd (obj.Tcb.set_prio.spec s t v) + have hblk : obj.Tcb.block_obj st t s2 = ok e := by + have h1 : L.mem s2 (wa t 3336#u64) = L.mem s (wa t 3336#u64) := by + have hv' := Notif.tfield_val (K := 3336#u64) hta (by simp) + exact off _ (field_aligned hta (by simp) (by simp)) (by rw [hv']; simp) + exact (eq_of_spec_fd (Fl4ProofsA64.P1.obj.Tcb.block_obj.spec s2 t)).trans (congrArg ok h1) + rw [sched.set_priority, eq_of_spec_fd (Fl4ProofsA64.P1.obj.Tcb.state.spec s t), bind_ok] + rcases hw with h | h <;> rw [show L.mem s (stA t) = _ from h] <;> + simp (config := {decide := true}) only [sched.TS_RUNNING, sched.TS_RESTART, sched.TS_RUNNING_VM, + sched.TS_BLOCKED_ON_RECV, sched.TS_BLOCKED_ON_SEND, sched.TS_BLOCKED_ON_NTFN, ite_false, ite_true, + hsetp, hblk, bind_ok, h3] + have hSpecP : setPriority (Z := Unit) (specPtr t) (BitVec.ofNat 8 v.val) σ = ({((), σ3)}, False) := by + have hSet0 : threadSetPriority (Z := Unit) (specPtr t) (BitVec.ofNat 8 v.val) σ = ({((), σ2)}, False) := + threadSet_eqSc (fun z => {z with tcbPriority := BitVec.ofNat 8 v.val}) hy + have hnr : ¬ runnable y.tcbState = true := by + rcases hbst with ⟨e', r, d, h⟩ | ⟨e', d, h⟩ <;> rw [h] <;> simp [runnable] + have hnb : ntfnBlocked y.tcbState = none := by + rcases hbst with ⟨e', r, d, h⟩ | ⟨e', d, h⟩ <;> rw [h] <;> rfl + unfold setPriority + rw [nd_bind_oneSc (Notif.getThreadState_eq hy), if_neg hnr, nd_bind_oneSc hSet0, hb, hnb] + simp only [Fl4Spec.NonDet.maybeM] + rw [nd_bind_oneSc hspec] + rfl + refine ⟨s3, σ3, hsp, hSpecP, hK, fun bxi sysl symL userOK j7 => ?_⟩ + -- memory frames from `s` + have f23 : ∀ b : U64, Aligned b → ¬ EW e b → (∀ w ∈ W, ¬ TL w b) → L.mem s3 b = L.mem s2 b := + fun b hb h1 h2 => hfr b hb ⟨h1, fun ⟨w, hw, hl⟩ => h2 w hw hl⟩ + have eNot : ∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → ¬ EW e (wa u K) := fun u hu K h1 h2 h => by + have hua := hP.e.tcb u hu + have := hP.e.et e hE u hu + unfold EW at h + rw [Notif.tfield_val hua h1] at h + omega + have tw : ∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → K.val ≠ 3448 → K.val ≠ 3496 → K.val ≠ 3504 → + L.mem s3 (wa u K) = L.mem s (wa u K) := fun u hu K h1 h2 h3 h4 h5 => by + have hua := hP.e.tcb u hu + have hal := field_aligned hua h1 h2 + have hv' := Notif.tfield_val hua h1 + rw [f23 _ hal (eNot u hu K h1 h2) fun w hw hl => ?_] + · refine off _ hal ?_ + rw [hv'] + by_cases eu : u = t + · subst eu; omega + · have := tcb_disj hP.e ht hu eu + omega + · unfold TL at hl + rw [hv'] at hl + by_cases ew : w = u + · subst ew; omega + · have := tcb_disj hP.e (hW w hw).1 hu (Ne.symm ew) + omega + have links : ∀ u ∈ Z.T, u ∉ W → ∀ K : U64, K.val = 3496 ∨ K.val = 3504 → L.mem s3 (wa u K) = L.mem s (wa u K) := + fun u hu huW K hK => by + have hua := hP.e.tcb u hu + have h1 : K.val < 4096 := by omega + have h2 : K.val % 8 = 0 := by omega + have hal := field_aligned hua h1 h2 + have hv' := Notif.tfield_val hua h1 + rw [f23 _ hal (eNot u hu K h1 h2) fun w hw hl => ?_] + · refine off _ hal ?_ + rw [hv'] + by_cases eu : u = t + · subst eu; omega + · have := tcb_disj hP.e ht hu eu + omega + · unfold TL at hl + rw [hv'] at hl + by_cases ew : w = u + · exact huW (ew ▸ hw) + · have := tcb_disj hP.e (hW w hw).1 hu (Ne.symm ew) + omega + -- the write set + have w1 : WritesIn st s s2 (fun b => b = prioA t) := writesIn_wr aP + have wN : WritesIn st s s3 (fun b => SchedStepNWA (st := st) N E Rp Z m σ s b ∨ BufW Bf b) := + (w1.trans w23).mono fun b hb => hb.elim + (fun hb => Or.inl (Or.inl (Or.inr (Endp.tcbStepWA_of_off hP.e.tcb ht 3448 (by norm_num) + (by norm_num) (by norm_num) (by norm_num) (by rw [hb]; exact vP))))) + (ewrite_stepNA_buf J.sr.core.tcbs) + -- the spec frame + have keepO : ∀ q o, σ.kheap q = some o → (∀ z, o ≠ .tcb z) → (∀ z, o ≠ .endpoint z) → σ3.kheap q = some o := + fun q o h n1 n2 => by + rw [key3, if_neg (fun c => by rw [c, hk] at h; exact n2 x (Option.some.inj h).symm), + if_neg (fun c => by rw [c, hy] at h; exact n1 y (Option.some.inj h).symm)] + exact h + have backO : ∀ q o, σ3.kheap q = some o → (∀ z, o ≠ .tcb z) → (∀ z, o ≠ .endpoint z) → σ.kheap q = some o := + fun q o h n1 n2 => by + rw [key3] at h + split at h + · exact absurd (Option.some.inj h).symm (n2 _) + · split at h + · exact absurd (Option.some.inj h).symm (n1 _) + · exact h + have fwdT : ∀ q z, σ.kheap q = some (.tcb z) → ∃ w, σ3.kheap q = some (.tcb w) ∧ + w = { z with tcbPriority := w.tcbPriority } := by + intro q z h + have ne : q ≠ specPtr e := fun c => by rw [c, hk] at h; cases h + by_cases c : q = specPtr t + · rw [c, hy] at h; cases h + exact ⟨y', c ▸ hy3, rfl⟩ + · exact ⟨z, by rw [key3, if_neg ne, if_neg c]; exact h, rfl⟩ + have bwdT : ∀ q w, σ3.kheap q = some (.tcb w) → ∃ z, σ.kheap q = some (.tcb z) ∧ + w = { z with tcbPriority := w.tcbPriority } := by + intro q w h + rw [key3] at h + split at h + · cases h + · split at h + · rename_i c + cases h + exact ⟨y, c ▸ hy, rfl⟩ + · exact ⟨w, h, rfl⟩ + have hE3 : σ3.kheap (specPtr e) = some (.endpoint yn) := by rw [key3, if_pos rfl] + have epF : ∀ q (z : Endpoint), σ.kheap q = some (.endpoint z) → ∃ z', σ3.kheap q = some (.endpoint z') ∧ + ∀ u, u ∈ z.epQueue → u ∈ z'.epQueue := by + intro q z h + by_cases c : q = specPtr e + · rw [c, hk] at h; cases h + exact ⟨yn, c ▸ hE3, fun u hu => (hmemn u).2 hu⟩ + · exact ⟨z, by rw [key3, if_neg c, if_neg (fun d => by rw [d, hy] at h; cases h)]; exact h, fun _ h => h⟩ + have HS : SpecSchedA σ σ3 := + { kh := fun q => by + by_cases c : q = specPtr e + · subst c + exact Or.inr (Or.inr ⟨_, _, hk, hE3, Or.inr (Or.inr (Or.inl ⟨x, yn, rfl, rfl⟩))⟩) + · by_cases d : q = specPtr t + · subst d + exact Or.inr (Or.inl ⟨y, y', hy, hy3, rfl, rfl, rfl⟩) + · exact Or.inl (by rw [key3, if_neg c, if_neg d]) + irqS := by rw [eσ3]; rfl + irqN := by rw [eσ3]; rfl + asid := by rw [eσ3]; rfl + vcur := by rw [eσ3]; rfl + cdt := by rw [eσ3]; rfl + orig := by rw [eσ3]; rfl } + have SSz : SSzA σ σ3 := fun q z k h => ⟨z, backO _ _ h (fun _ c => by cases c) (fun _ c => by cases c)⟩ + -- the queue invariant + have pr32 : prioOf σ3 = prioOf σ2 := by + funext q + unfold prioOf + by_cases c : q = specPtr e + · rw [c, hE3, hk2] + · rw [key3, if_neg c, key2] + have hqN := Notif.not_member J.ntfn ht hy (fun q h => by + rw [h] at hb; simp [epBlocked] at hb) + have Qk : QBlkA (specPtr '' E) (specPtr '' N) σ σ3 := by + refine ⟨J.ipcSorted.of_reorder (t := specPtr t) (yt := y') hy3 + (fun q z c h => ?_) (fun q w c h => ?_) (fun q hq x' h => ?_) (fun q hq x' h => ?_), + fun hA q z k h ne => hA q z k (backO _ _ h (fun _ c => by cases c) (fun _ c => by cases c)) ne, + fun q z h => ⟨z, backO _ _ h (fun _ c => by cases c) (fun _ c => by cases c), id⟩, + by rw [eσ3]; exact ⟨rfl, rfl, rfl, rfl⟩⟩ + · have ne : q ≠ specPtr e := fun d => by rw [d, hk] at h; cases h + exact ⟨z, by rw [key3, if_neg ne, if_neg c]; exact h, rfl⟩ + · have ne : q ≠ specPtr e := fun d => by rw [d, hE3] at h; cases h + rw [key3, if_neg ne, if_neg c] at h + exact ⟨w, h, rfl⟩ + · obtain ⟨e', he', rfl⟩ := hq + by_cases c : specPtr e' = specPtr e + · rw [c, hE3] at h; cases h + refine ⟨x, c ▸ hk, Or.inr ⟨?_, fun u hu => ?_⟩⟩ + · rw [eyn, pr32] + cases x with + | idleEP => simp [Endpoint.epQueue] at htq + | sendEP q => rfl + | recvEP q => rfl + · rw [hzs] at hu + obtain ⟨w, hw, rfl⟩ := List.mem_map.1 hu + obtain ⟨z, hz, -⟩ := J.ep.tcb w (hQ1.mem w hw) + exact ⟨z, hz⟩ + · have h0 : σ.kheap (specPtr e') = some (.endpoint x') := by + rw [key3, if_neg c] at h + split at h + · cases h + · exact h + refine ⟨x', h0, Or.inl ⟨List.Sublist.refl _, fun hmem => ?_⟩⟩ + obtain ⟨xs, hQ, hxs, hB⟩ := (J.ep.at_ep he' h0).queue + rw [hxs] at hmem + obtain ⟨w, hw, ew⟩ := List.mem_map.1 hmem + have : w = t := specPtr_inj ew + subst this + exact c (by rw [← (hB w hw).1]) + · obtain ⟨q0, hq0', rfl⟩ := hq + have h0 := backO _ _ h (fun _ c => by cases c) (fun _ c => by cases c) + exact ⟨x', h0, Or.inl ⟨List.Sublist.refl _, hqN q0 hq0' x' h0⟩⟩ + -- the relations + have ntfn : NtfnStateRel N Z.T (L.mem s3) σ3 := by + have nw : ∀ n' ∈ N, ∀ K : U64, K.val < 40 → K.val % 8 = 0 → + L.mem s3 (wa n' K) = L.mem s2 (wa n' K) := fun n' hn K h1 h2 => by + have hal := (nword hP.n hn h1 h2).1 + have hv' := nfield_val (K := K) (hP.n.ntfn n' hn) (by omega) + refine f23 _ hal (fun h => ?_) fun w hw h => ?_ + · have := hP.ne n' hn e hE + unfold EW at h + rw [hv'] at h + omega + · have := hP.n.disj n' hn w (hW w hw).1 + unfold TL at h + rw [hv'] at h + omega + have tf : ∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → K.val ≠ 3496 → K.val ≠ 3504 → + L.mem s3 (wa u K) = L.mem s2 (wa u K) := fun u hu K h1 h2 h4 h5 => by + have hua := hP.e.tcb u hu + refine f23 _ (field_aligned hua h1 h2) (eNot u hu K h1 h2) fun w hw hl => ?_ + unfold TL at hl + rw [Notif.tfield_val hua h1] at hl + by_cases ew : w = u + · subst ew; omega + · have := tcb_disj hP.e (hW w hw).1 hu (Ne.symm ew) + omega + refine ⟨fun n' hn => ?_, fun u hu => ?_⟩ + · obtain ⟨xn, hkn, hxn⟩ := ntfn2.ntfn n' hn + have hkn3 : σ3.kheap (specPtr n') = some (.notification xn) := by + rw [eσ3] + have ne : specPtr n' ≠ specPtr e := fun c => by rw [c, hk2] at hkn; cases hkn + simp only [updK, Function.update_of_ne ne] + exact hkn + refine ⟨xn, hkn3, hxn.frame ?_ fun u hu hm => ?_⟩ + · exact ⟨by rw [Notif.epHeadA_eq]; exact nw n' hn _ (by simp) (by simp), + by rw [Notif.epTSA_eq]; exact nw n' hn _ (by simp) (by simp), + by rw [Notif.nBadgeA_eq]; exact nw n' hn _ (by simp) (by simp), + by rw [Notif.nBoundA_eq]; exact nw n' hn _ (by simp) (by simp), + by rw [Notif.nScA_eq]; exact nw n' hn _ (by simp) (by simp)⟩ + · have huW : u ∉ W := fun c => by + have h6 := (hxn.obj.blocked hm).2.1 + have h2 : (L.mem s2 (stA u)).val = (L.mem s (stA u)).val := by + have hua := hP.e.tcb u hu + rw [off _ (a_st hua).1 (by + rw [val_st hua] + by_cases eu : u = t + · subst eu; omega + · have := tcb_disj hP.e ht hu eu + omega)] + obtain ⟨-, hqw⟩ := hW u c + rw [hzs] at hqw + obtain ⟨w, hw, ew⟩ := List.mem_map.1 hqw + have : w = u := specPtr_inj ew + subst this + have := (hB1 w hw).2 + omega + refine ⟨by rw [Fl4ProofsA64.P1.stA_eq]; exact tf u hu _ (by simp) (by simp) (by simp) (by simp), + by rw [Notif.tBlockA_eq]; exact tf u hu _ (by simp) (by simp) (by simp) (by simp), ?_⟩ + have lk : ∀ K : U64, K.val = 3496 ∨ K.val = 3504 → L.mem s3 (wa u K) = L.mem s2 (wa u K) := fun K hK => by + have hua := hP.e.tcb u hu + have h1 : K.val < 4096 := by omega + have h2 : K.val % 8 = 0 := by omega + refine f23 _ (field_aligned hua h1 h2) (eNot u hu K h1 h2) fun w hw hl => ?_ + unfold TL at hl + rw [Notif.tfield_val hua h1] at hl + by_cases ew : w = u + · exact huW (ew ▸ hw) + · have := tcb_disj hP.e (hW w hw).1 hu (Ne.symm ew) + omega + exact ⟨by rw [prevEp_eq]; exact lk _ (by simp), by rw [nextEp_eq]; exact lk _ (by simp)⟩ + · obtain ⟨xu, hku, hxu⟩ := ntfn2.tcb u hu + have ne : specPtr u ≠ specPtr e := fun c => by rw [c, hk2] at hku; cases hku + refine ⟨xu, by rw [eσ3]; simp only [updK, Function.update_of_ne ne]; exact hku, hxu.frame ?_⟩ + exact ⟨by rw [Fl4ProofsA64.P1.stA_eq]; exact tf u hu _ (by simp) (by simp) (by simp) (by simp), + by rw [Notif.tBlockA_eq]; exact tf u hu _ (by simp) (by simp) (by simp) (by simp), + by rw [Notif.badgeRegA_eq]; exact tf u hu _ (by simp) (by simp) (by simp) (by simp), + by rw [Notif.prioA_eq']; exact tf u hu _ (by simp) (by simp) (by simp) (by simp)⟩ + have reply : ReplyStackRelA (L.mem s3) Rp Z.C σ3 (scNameA m) stack := by + refine J.reply.frame (fun r hr => ?_) + (fun c _ z k h => ⟨z, k, keepO _ _ h (fun _ c => by cases c) (fun _ c => by cases c), rfl⟩) + (fun r _ x' h => ⟨x', backO _ _ h (fun _ c => by cases c) (fun _ c => by cases c), rfl⟩) + have hr3 := hP.e.rep r hr + have one : ∀ O : U64, O.val = 8 ∨ O.val = 16 → L.mem s3 (wa r O) = L.mem s (wa r O) := by + intro O hO + have hv' : (wa r O).val = r.val + O.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have hal : Aligned (wa r O) := by unfold Aligned; rw [hv']; omega + rw [f23 _ hal (fun h => ?_) fun w hw h => ?_] + · refine off _ hal ?_ + have := hP.e.rt r hr t ht + omega + · have := hP.e.er e hE r hr + unfold EW at h + omega + · have := hP.e.rt r hr w (hW w hw).1 + unfold TL at h + omega + exact ⟨one _ (Or.inr (by simp [layout.reply.NEXT])), one _ (Or.inl (by simp [layout.reply.PREV]))⟩ + have K : SIKA σ.idleThread (fun _ => False) (fun _ => False) false σ σ3 := by + have K1 : SIKA σ.idleThread (fun _ => False) (fun _ => False) false σ σ2 := + SIKA.upd hy (ObjIpcA.tcb y y' + { fault := rfl, frame := rfl, buf := rfl, mcp := rfl, yt := rfl, caps := rfl, st := Or.inl rfl, + sc := Or.inl rfl, vcpu := rfl, flags := rfl }) + (fun hr => hr.updNs _ _ (fun _ _ h => by cases h)) + have K2 : SIKA σ.idleThread (fun _ => False) (fun _ => False) false σ2 σ3 := by + rw [eσ3] + exact SIKA.upd hk2 (ObjIpcA.ep x yn fun u hu _ => (hmemn u).2 hu) + (fun hr => hr.updNs _ _ (fun _ _ h => by cases h)) + exact K1.trans K2 + have sinv : SInvXA σ3 m n := + K.sinv J.sinv (fun _ _ h => h) (fun h => h) (fun _ h => h.elim) (fun _ h => h.elim) (fun _ h => h.elim) hns + (fun _ h => h.elim) (J.sidle.rdyA J.sinv) (fun _ h => h.elim) + have sidle : SIdleA σ3 m n := K.sidle J.sidle (fun _ _ h => h) (J.sidle.rdyA J.sinv) + have rv : RunVcpuInv st Z.T s3 := fun u hu h0 => by + have e1 : L.mem s3 (runVcpuA u) = L.mem s (runVcpuA u) := by + unfold runVcpuA + exact tw u hu 3672#u64 (by simp) (by simp) (by simp) (by simp) (by simp) + have e2 : L.mem s3 (Fl4ProofsA64.P1.stA u) = L.mem s (Fl4ProofsA64.P1.stA u) := by + rw [Fl4ProofsA64.P1.stA_eq] + exact tw u hu 3328#u64 (by simp) (by simp) (by simp) (by simp) (by simp) + rw [e2]; exact J.runVcpu u hu (e1 ▸ h0) + have blkQ : BlkQueuedA Z.T E N (L.mem s3) σ3 := by + have bm : ∀ u ∈ Z.T, L.mem s3 (Notif.tBlockA u) = L.mem s (Notif.tBlockA u) := fun u hu => by + rw [Notif.tBlockA_eq]; exact tw u hu 3336#u64 (by simp) (by simp) (by simp) (by simp) (by simp) + refine ⟨fun u hu w hw q hq => ?_, fun u hu w hw q hq => ?_⟩ + · obtain ⟨z, hz, ez⟩ := bwdT _ w hw + obtain ⟨hE', x0, hx0, hm⟩ := J.blkQ.ep u hu z hz q (by rw [ez] at hq; exact hq) + obtain ⟨x1, hx1', hsub⟩ := epF q x0 hx0 + exact ⟨by rw [bm u hu]; exact hE', x1, hx1', hsub _ hm⟩ + · obtain ⟨z, hz, ez⟩ := bwdT _ w hw + obtain ⟨hN', z', q0, hq0', ho, hm⟩ := J.blkQ.ntfn u hu z hz q (by rw [ez] at hq; exact hq) + exact ⟨by rw [bm u hu]; exact hN', z', q0, + keepO _ _ hq0' (fun _ c => by cases c) (fun _ c => by cases c), ho, hm⟩ + have sv : SchedValidA σ3 := J.schedValid.congr (by rw [eσ3]; rfl) (by rw [eσ3]; rfl) (by rw [eσ3]; rfl) + (by rw [eσ3]; rfl) (by rw [eσ3]; rfl) (by rw [eσ3]; rfl) + (fun q z k h => keepO q _ h (fun _ c => by cases c) (fun _ c => by cases c)) + (fun u z h => by + obtain ⟨w, hw, ez⟩ := fwdT u z h + exact ⟨w, hw, by rw [ez]⟩) + (fun u w h => by + obtain ⟨z, hz, ez⟩ := bwdT u w h + exact ⟨z, hz, by rw [ez], by rw [ez]⟩) + have tw' : ∀ u ∈ Z.T, ∀ O : U64, O.val < 224 ∨ O.val = 3440 ∨ O.val = 3472 ∨ O.val = 3512 ∨ O.val = 3456 → O.val % 8 = 0 → + L.mem s3 (wa u O) = L.mem s (wa u O) := fun u hu O h1 h2 => + tw u hu O (by omega) h2 (by omega) (by omega) (by omega) + obtain ⟨Q, Rl, sr3'⟩ := sr3 + exact ⟨Q, Rl, J.of_ipcStepUSA (Q := Q) (Rl := Rl) (curOf := Z.curOf) (curScOf := Z.curScOf) wN HS SSz Qk + (fun _ _ => by rw [eσ3]; rfl) (fun _ _ => rfl) bxi sysl symL userOK j7 sr3' ep3 ntfn reply J.deadExt.reply + K.ipcKeep tw' sinv sidle rv blkQ sv⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3TcbPrioEpArmA.lean b/proofs/Fl4ProofsA64/P3/Stage3TcbPrioEpArmA.lean new file mode 100644 index 000000000..fbf8cecd2 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3TcbPrioEpArmA.lean @@ -0,0 +1,136 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 3e): **the SetPriority decode arm for an endpoint-blocked target**. + +* **`tcbPriority_specOfA`**: `invokeTcb` of the priority update, given `setPriority`'s result. +* **`JSyscallA.setPrioEpA`**: `decode_invocation` at a syscall entry (record step `JStage3A.prioEp_stepA`). + +Named premises: as `JSyscallA.setPrioPlainA`, plus [P] `hmb`, `hmax`, `htu`. The `Err` returns are not lifted. New module. +-/ +import Fl4ProofsA64.P3.Stage3TcbPrioEpA +import Fl4ProofsA64.P3.Stage3TcbBodyA +import Fl4ProofsA64.P3.Stage3TcbBodyA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +theorem tcbPriority_specOfA {σ σ' : State Unit} {t auth : ObjRef} {slot : CslotPtr} {prio : Priority} + (hSet : setPriority (Z := Unit) t prio σ = ({((), σ')}, False)) : + (fl4InvokeTcb (.threadControlSched t slot none none (some (prio, auth)) none)).run σ = + ({(.ok [], σ')}, False) := by + simp only [fl4InvokeTcb, fl4InstallTcbCap, Fl4Spec.NonDet.maybeM] + rw [run_bind_okD (run_returnOkD () σ), + run_bind_okD (run_liftE_stD (m := Fl4Spec.NonDet.pure ()) rfl), + run_bind_okD (run_liftE_stD hSet), + run_bind_okD (run_liftE_stD (m := Fl4Spec.NonDet.pure ()) rfl)] + rfl + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s : S} {a : A} {σ : State Unit} {m : McState} + +set_option maxHeartbeats 4000000 in +/-- **TCB SetPriority of an endpoint-blocked thread at a syscall entry** (module docstring). -/ +theorem JSyscallA.setPrioEpA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + {slot lbl len buf t p auth argSlot : U64} {blk call don : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Thread t) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hlbl : lbl = fl4_abi.label.TCBSetPriority) + (he : tcb.extra_with_length ⟨len, buf, call, slot, extra⟩ 0#usize 1#u64 = ok (.Ok argSlot)) + (hp : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 0#u64 = ok p) + (harg : argSlot ∈ OS) (hac : capA st s argSlot = .Thread auth) (hle : ¬ L.mem s (mcpA auth) < p) + {y : Tcb} (hy : σ.kheap (specPtr t) = some (.tcb y)) (htc : specPtr t ≠ σ.curThread) + {p0 : ObjRef} (hb : epBlocked y.tcbState = some p0) : + ∃ xc σ1 σ2 s2, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + (∀ κs : CslotPtr, (.ok [], σ2) ∈ ((fl4InvokeTcb (.threadControlSched (specPtr t) κs none none + (some (BitVec.ofNat 8 p.val, specPtr auth)) none)).run σ1).1) ∧ + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (.Ok (), s2, a) ∧ + ∃ Q Rl, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s2 a σ2 m ∧ Sched.BodyEndA σ σ2 m := by + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hslot hcov J.actW J.running J.cur + have hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM := J.toJStage3EntryA.curNotVmA + obtain ⟨xc, hkc, hst, hsp, J1, -, -, -⟩ := J.toJStage3A.of_restartCurA RS.pl.choose_spec.choose_spec.choose_spec.choose_spec + RS.sym RS.run hnvm RS.cur RS.vs RS.vc + have ht : t ∈ Z.T := J.tcbs ▸ (J.toJStage3A.slotCapTy hslot).1.2.2.2.2.2 t hcap + have haT : auth ∈ Z.T := J.tcbs ▸ (J.toJStage3A.slotCapTy harg).1.2.2.2.2.2 auth hac + have T0 := J.toJStage3A.toTA J.sact J.noSwitch + have hstall : smp.remote_tcb_stall st ar s a t = ok (s, a) := T0.stall_singleA hCs h1 ht + set s1 := L.wr s (stA (L.curW s)) sched.TS_RESTART with hs1 + set σ1 := stUpdA σ σ.curThread xc .restart with hσ1 + have hr : tcb.restart_caller st s = ok s1 := by + unfold tcb.restart_caller + rw [cur_thread_ok, bind_ok, hst] + have ha : tcb.thread_arg st s argSlot 1#u64 = ok (.Ok auth) := by + unfold tcb.thread_arg + rw [capA_eq, bind_ok, hac] + have hy1 : σ1.kheap (specPtr t) = some (.tcb y) := by + rw [hσ1]; simp [stUpdA, setObjA, Function.update_of_ne htc]; exact hy + have hSt : StEqA Z.T (L.mem s1) σ1 := fun u hu z hz => (J1.ep.at_tcb hu hz).st + have hWidth : p.val < 256 := by + obtain ⟨x, -, hw⟩ := J.mcp auth haT + have hv : p.val ≤ (L.mem s (mcpA auth)).val := Nat.le_of_not_lt hle + rw [hw.1] at hv + exact Nat.lt_of_le_of_lt hv x.tcbMcpriority.isLt + have hbst : (∃ e r d, y.tcbState = .blockedOnReceive e r d) ∨ ∃ e d, y.tcbState = .blockedOnSend e d := by + revert hb + cases y.tcbState <;> simp [epBlocked] + have hnr : ¬ runnable y.tcbState = true := by + rcases hbst with ⟨e', r, d, h⟩ | ⟨e', d, h⟩ <;> rw [h] <;> simp [runnable] + have hon1 : L.mem s1 (affA t) = L.cpu s1 := by + have x := hCs _ (J1.sr.core.aff t ht) + have y := hCs _ J1.sr.sch.cpuCs + exact UScalar.eq_of_val_eq (by omega) + have hnq : ∀ d q, specPtr t ∉ σ1.readyQueues d q := fun d q hm => by + obtain ⟨z, hz, hr⟩ := J1.sr.run.1 d q _ hm + rw [hy1] at hz; cases hz + exact hnr hr + have hq0 : (L.mem s1 (inqA t) &&& sched.Q_QUEUED) = 0#u64 := J1.sr.toSchedSRN.qBit0 ht hon1 hnq + have hcurNe : t ≠ L.mem s1 (L.nodeAt s1 (L.cpu s1) layout.node.CUR_THREAD) := fun e => htc (by + rw [show L.mem s1 (L.nodeAt s1 (L.cpu s1) layout.node.CUR_THREAD) = L.curW s1 from rfl] at e + rw [e, ← J1.sr.sch.curThread] + rfl) + have hc : m.curCore < fl4A64Platform.numNodes := by rw [J1.sr.sch.core]; exact hCs _ J1.sr.sch.cpuCs + have hns1 : ∀ u, σ1.schedulerAction ≠ .switchThread u := fun u => J.noSwitch u + obtain ⟨s3, σ3, hsp1, hSpec, hK, hJ⟩ := J1.prioEp_stepA (st := st) (ar := ar) hc hmb hmax htu hns1 ht hWidth hy1 hq0 hb + have hdec : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (.Ok (), s3, a) := by + rw [hcap, decodeTcb_programA, if_neg Bool.false_ne_true, hlbl, tcbSetPriority_postA hstall he hp ha hle hr, + hsp1, bind_ok] + have htH : t ∈ H.tcbs := by rw [J1.tcbs]; exact ht + have blk : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s3 := by + have h := sched.set_priority.blk (st := st) s1 t p (BXI.blk J1.sys.ent.bxi) htH hWidth + (fun e => absurd e hcurNe) (fun e => absurd e hcurNe) (fun e => absurd e hcurNe) + rw [hsp1, WP.spec_ok] at h + exact h.1 + obtain ⟨-, -, sysl, symL, -, userOK, j7⟩ := J.decodeFinA hA hM hPl hfm hslot hcov + (rfl : capA st s slot = capA st s slot) hext hSC (lbl := lbl) + (by rw [hcap, hlbl]; simp [DecArmS1aEnt, DecArmQH, DecArmQZ2, DecArmKQ2, DecArmKQ, DecArmKQ3, DecArmDelQ, + DecArmS1aC, capFam]) + (fun _ h => by rw [hcap] at h; cases h) (fun _ h => by rw [hcap] at h; cases h) hdec + σ3 m + obtain ⟨Q, Rl, J2⟩ := hJ (BlkOK.bxi blk) sysl symL userOK j7 + refine ⟨xc, σ1, σ3, s3, hkc, rfl, hsp, fun κs => ?_, hdec, Q, Rl, J2, Sched.BodyEndA.ofKeep J hkc hK⟩ + rw [tcbPriority_specOfA hSpec] + rfl + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3TcbPrioNtfnA.lean b/proofs/Fl4ProofsA64/P3/Stage3TcbPrioNtfnA.lean new file mode 100644 index 000000000..1137583e0 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3TcbPrioNtfnA.lean @@ -0,0 +1,699 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 3e): **TCB SetPriority of a thread blocked on a notification, at the record.** + +* **`reorderNtfn_eqA`**: the spec's `reorder_ntfn` of a waiting notification. +* **`SpecIrqA.updNtfnA`**: `SpecIrqA` across a notification update. +* **`JStage3A.prioNtfn_stepA`**: FL4's `PRIO` word write and `reorder_ntfn` refine the spec and keep the record + (`ObjIpcA` has no notification case, so `SInvXA.of_fr` and `SIdleA` directly). +* **`JSyscallA.setPrioNtfnA`**: the decode arm at a syscall entry. + +Named premises: as `JSyscallA.setPrioEpA`. The `Err` returns are not lifted. New module. +-/ +import Fl4ProofsA64.P3.Stage3TcbPrioEpArmA +import Fl4ProofsA64.P3.Stage3KEntryChargeA +import Fl4ProofsA64.P3.Stage3NtfnFootA +import Fl4ProofsA64.P3.Stage3TcbBodyA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +theorem reorderNtfn_eqA {σ : State Unit} {p t : ObjRef} {x : Notification} {xt : Tcb} {q : List ObjRef} + (hk : σ.kheap p = some (.notification x)) (hob : x.ntfnObj = .waitingNtfn q) (hne : q ≠ []) + (hkt : σ.kheap t = some (.tcb xt)) (g : ObjRef → Priority) + (hq : ∀ y ∈ removeAll t q, ∃ z, σ.kheap y = some (.tcb z) ∧ z.tcbPriority = g y) : + reorderNtfn (Z := Unit) p t σ = ({((), updK σ p (.notification + { x with ntfnObj := .waitingNtfn (specIns (removeAll t q) g xt.tcbPriority t) }))}, False) := by + have hw : x.ntfnObj.isWaitingNtfn = true := by rw [hob]; rfl + have hqo : ntfnQueueOf x = q := by unfold ntfnQueueOf; rw [hob]; rfl + unfold reorderNtfn + rw [nd_bind_one (getNotification_eq hk), nd_bind_one (assert_trueA hw σ), nd_pure_bind', hqo, + nd_bind_one (assert_trueA hne σ), nd_pure_bind', nd_bind_one (tcbAppend_eq hkt _ g hq)] + exact setNotification_eq _ hk + +theorem SpecIrqA.updNtfnA {σ : State Unit} {p : ObjRef} {x y : Notification} (h : SpecIrqA σ) + (hk : σ.kheap p = some (.notification x)) : SpecIrqA (updK σ p (.notification y)) := by + have hct : ∀ c q, capTableAt c q (updK σ p (.notification y)) ↔ capTableAt c q σ := fun c q => by + unfold capTableAt + by_cases e : q = p + · subst e; simp [updK, hk] + · simp [updK, Function.update_of_ne e] + obtain ⟨⟨⟨hinj, hcn⟩, hdom⟩, hsl, href⟩ := h + refine ⟨⟨⟨hinj, fun irq hi => (hct _ _).2 (hcn irq hi)⟩, fun irq hi c hc => hdom irq hi c ((hct _ _).1 hc)⟩, + fun irq hi o b c e hc' => ?_, fun q o b c e hc' => ?_⟩ + · by_cases ep : σ.interruptIrqNode irq = p + · obtain ⟨cs, hcs, -⟩ := hcn irq hi + rw [ep, hk] at hcs; cases hcs + · exact hsl irq hi o b c (by simpa [updK, Function.update_of_ne ep] using e) hc' + · by_cases ep : q = p + · subst ep + simp [updK] at e; subst e + simp [capOf] at hc' + · exact href q o b c (by simpa [updK, Function.update_of_ne ep] using e) hc' + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {N T : Set U64} + {SR : S → Fl4SpecA64.State Unit → Prop} + +/-- A related notification queue with a sorted spec queue is sorted over memory. -/ +theorem prioSorted_ntfnA {m : U64 → U64} {σ : State Unit} (hR : NtfnStateRel N T m σ) {zs : List U64} + (hm : ∀ u ∈ zs, u ∈ T) (hs : PrioSortedA σ (zs.map specPtr)) : P1.PrioSorted (fun u => m (prioA u)) zs := by + unfold PrioSortedA at hs + rw [List.pairwise_map] at hs + refine hs.imp_of_mem fun {a b} ha hb hab => ?_ + obtain ⟨xa, hka, ra⟩ := hR.tcb a (hm a ha) + obtain ⟨xb, hkb, rb⟩ := hR.tcb b (hm b hb) + have h := hab xa xb hka hkb + rw [← ra.prio, ← rb.prio] at h + exact h + +set_option maxHeartbeats 4000000 in +/-- **`reorder_ntfn` refines `reorder_ntfn`** for a waiter, the rest of the queue sorted by priority. -/ +theorem ntfn_reorder_relA (hH : Notif.SchedHypsA st N T SR) (hP : NPlaced T N) (s : S) (σ : State Unit) + (hR : NtfnStateRel N T (L.mem s) σ) (hSR : SR s σ) {t : U64} (ht : t ∈ T) {xt : Tcb} + (hkt : σ.kheap (specPtr t) = some (.tcb xt)) {e : U64} (he : e ∈ N) {x : Notification} {q0 : List ObjRef} + (hk : σ.kheap (specPtr e) = some (.notification x)) (hob : x.ntfnObj = .waitingNtfn q0) + (htq : specPtr t ∈ q0) + (hsort : ∀ xs, QueueRel (L.mem s) T .Ep (qOf (L.mem s) e) xs → + P1.PrioSorted (fun u => L.mem s (prioA u)) (xs.erase t)) : + notification.reorder_ntfn st s e t ⦃ s' => + ∃ σ' q1 W, + σ' = updK σ (specPtr e) (.notification { x with ntfnObj := .waitingNtfn q1 }) ∧ + reorderNtfn (Z := Unit) (specPtr e) (specPtr t) σ = ({((), σ')}, False) ∧ + q1 = specIns (removeAll (specPtr t) q0) (prioOf σ) xt.tcbPriority (specPtr t) ∧ + (∀ u, u ∈ q1 ↔ u ∈ q0) ∧ SR s' σ' ∧ NtfnStateRel N T (L.mem s') σ' ∧ QKeeps st N T e W s s' ∧ + (∀ w ∈ W, w ∈ T ∧ specPtr w ∈ q0 ∧ L.mem s (tBlockA w) = e) ⦄ := by + have hx := hR.at_ntfn he hk + have hxo := hx.obj + rw [hob] at hxo + obtain ⟨hst, zs, -, hzs, hQ, hB⟩ := hxo + have htzs : t ∈ zs := by + rw [hzs] at htq; obtain ⟨u, hu, eu⟩ := List.mem_map.1 htq; rw [← specPtr_inj eu]; exact hu + have hsorted := hsort zs hQ + refine WP.spec_mono (Notif.ntfn_reorder_fullA (st := st) hP s he t zs hQ htzs) ?_ + rintro s' ⟨hA1, hA2, K⟩ + have hnd := hQ.nodup + have hrm : removeAll (specPtr t) q0 = (zs.erase t).map specPtr := by + rw [hzs]; exact removeAll_map hnd t + have hq : ∀ u ∈ removeAll (specPtr t) q0, ∃ z, σ.kheap u = some (.tcb z) ∧ z.tcbPriority = prioOf σ u := by + intro u hu + rw [hrm] at hu + obtain ⟨w, hw, rfl⟩ := List.mem_map.1 hu + obtain ⟨z, hz, -⟩ := hR.tcb w (hQ.mem w (List.mem_of_mem_erase hw)) + exact ⟨z, hz, by simp [prioOf, hz]⟩ + have hxt := hR.at_tcb ht hkt + obtain ⟨ys, hys⟩ : ∃ ys, ys = insPrio (fun u => L.mem s (prioA u)) (L.mem s (prioA t)) t (zs.erase t) := + ⟨_, rfl⟩ + rw [← hys] at hA1 + have hlist : specIns (removeAll (specPtr t) q0) (prioOf σ) xt.tcbPriority (specPtr t) = ys.map specPtr := by + rw [hrm, hys] + exact specIns_map hsorted hxt.prio fun u hu => by + obtain ⟨z, hz, hzr⟩ := hR.tcb u (hQ.mem u (List.mem_of_mem_erase hu)) + exact ⟨z, hz, hzr.prio⟩ + have fwd : ∀ w ∈ ys, w ∈ zs := fun w hw => by + rcases mem_insPrio_sorted hsorted (hys ▸ hw) with rfl | hw' + · exact htzs + · exact List.mem_of_mem_erase hw' + have bwd : ∀ w ∈ zs, w ∈ ys := fun w hw => by + rw [hys, insPrio_eq_sorted _ _ t _ hsorted, List.mem_append, List.mem_cons] + by_cases c : w = t + · exact Or.inr (Or.inl c) + · have h' := (List.mem_erase_of_ne c).2 hw + rw [← List.takeWhile_append_dropWhile (l := zs.erase t) + (p := fun u => !decide (L.mem s (prioA t) > L.mem s (prioA u))), List.mem_append] at h' + rcases h' with h' | h' + · exact Or.inl h' + · exact Or.inr (Or.inr h') + have ystne : ys ≠ [] := List.ne_nil_of_mem (bwd t htzs) + have hmemn : ∀ u, u ∈ ys.map specPtr ↔ u ∈ q0 := fun u => by + rw [hzs, List.mem_map, List.mem_map] + exact ⟨fun ⟨w, hw, c⟩ => ⟨w, fwd w hw, c⟩, fun ⟨w, hw, c⟩ => ⟨w, bwd w hw, c⟩⟩ + have hspec := reorderNtfn_eqA (t := specPtr t) hk hob (show q0 ≠ [] from List.ne_nil_of_mem htq) hkt + (prioOf σ) hq + rw [hlist] at hspec + obtain ⟨σ', eσ'⟩ : ∃ σ', σ' = updK σ (specPtr e) (.notification + { x with ntfnObj := .waitingNtfn (ys.map specPtr) }) := ⟨_, rfl⟩ + rw [← eσ'] at hspec + have sr' : SR s' σ' := by + rw [eσ'] + refine hH.stable _ _ _ _ hSR K.foot ⟨rfl, rfl, fun q => ?_⟩ + (Endp.NoBlockE.updO _ _ fun _ h => by cases h) + by_cases c : q = specPtr e + · subst c + exact Or.inr (Or.inl ⟨x, { x with ntfnObj := .waitingNtfn (ys.map specPtr) }, hk, + by rw [updK_kheap, Function.update_self], rfl, rfl⟩) + · exact Or.inl (by rw [updK_kheap, Function.update_of_ne c]) + have ntfn' : NtfnStateRel N T (L.mem s') σ' := by + rw [eσ'] + refine hR.upd_ntfn he _ (fun n' hn' c => K.ntfn n' hn' c) K.tcb (fun u hu hm => ?_) ?_ + · exact K.links u hu fun c => hm (hB u c).2 + · refine ⟨⟨by rw [hA2]; exact hst, ys, ystne, rfl, hA1, fun u hu => ?_⟩, ?_, ?_, ?_⟩ + · have huz := fwd u hu + have huT := hQ.mem u huz + exact ⟨by rw [(K.tcb u huT).st]; exact (hB u huz).1, by rw [(K.tcb u huT).blk]; exact (hB u huz).2⟩ + · rw [K.bound]; exact hx.bound + · rw [K.bound]; exact hx.bound_mem + · rw [K.sc]; exact hx.sc + refine ⟨σ', ys.map specPtr, zs, eσ', hspec, hlist.symm, hmemn, sr', ntfn', K, fun w hw => + ⟨hQ.mem w hw, by rw [hzs]; exact List.mem_map_of_mem hw, (hB w hw).2⟩⟩ + +end + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s : S} {a : A} {σ : State Unit} {m : McState} + +set_option maxHeartbeats 4000000 in +/-- **SetPriority of a notification-blocked thread at the record** (module docstring). -/ +theorem JStage3A.prioNtfn_stepA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (hc : m.curCore < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (hns : ∀ u, σ.schedulerAction ≠ .switchThread u) + {t v : U64} (ht : t ∈ Z.T) (hv : v.val < 256) {y : Tcb} (hy : σ.kheap (specPtr t) = some (.tcb y)) + (hq0 : (L.mem s (inqA t) &&& sched.Q_QUEUED) = 0#u64) + {p : ObjRef} (hb : y.tcbState = .blockedOnNotification p) : + ∃ s3 σ3, sched.set_priority st s t v = ok s3 ∧ + setPriority (Z := Unit) (specPtr t) (BitVec.ofNat 8 v.val) σ = ({((), σ3)}, False) ∧ Sched.BodyKeepA σ σ3 ∧ + (BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s3 → + SysL st H Z.Cs OS ia ie s3 → SymL st H Z.Cs s3 → Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s3 → + J7A st (J7R2A st ar nA) Z H OS s3 σ3 m → + ∃ Q Rl, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s3 a σ3 m) := by + have hP := J.iplaced + have hta : TcbAddr t := J.sr.core.tcbs t ht + have aP : Aligned (prioA t) := (a_prio hta).1 + have vP := val_prio hta + obtain ⟨hE, x, q0, hk, hob, htq0⟩ := J.blkQ.ntfn t ht y hy p hb + obtain ⟨z0, hz0, r0⟩ := J.ntfn.tcb t ht + rw [hy] at hz0; cases hz0 + set e := L.mem s (Notif.tBlockA t) with hedef + have hpe : specPtr e = p := r0.blk p hb + subst hpe + have htp : specPtr t ≠ specPtr e := fun h => by rw [h, hk] at hy; cases hy + have htq : specPtr t ∈ x.ntfnObj.ntfnQueue := by rw [hob]; exact htq0 + set s2 := L.wr s (prioA t) v with hs2 + set y' : Tcb := { y with tcbPriority := BitVec.ofNat 8 v.val } with hy' + set σ2 := prioStateA σ (specPtr t) y (BitVec.ofNat 8 v.val) with hσ2 + have off : ∀ b : U64, Aligned b → b.val ≠ t.val + 3448 → L.mem s2 b = L.mem s b := fun b hb h => + mem_wr_ne' aP hb (fun e => h (by rw [← e]; exact vP)) + have key2 : ∀ q, σ2.kheap q = if q = specPtr t then some (.tcb y') else σ.kheap q := by + intro q + simp only [hσ2, prioStateA, updKSc_kheap, Function.update_apply] + rfl + have hk2 : σ2.kheap (specPtr e) = some (.notification x) := by rw [key2, if_neg htp.symm]; exact hk + have hy2 : σ2.kheap (specPtr t) = some (.tcb y') := by rw [key2, if_pos rfl] + -- the relations after the priority write + have ep2 : EpStateRel Z.T E Rp Bf (L.mem s2) σ2 := EpStateRel.prioWrA J.ep hP.e ht hy hv + have ntfn2 : NtfnStateRel N Z.T (L.mem s2) σ2 := NtfnStateRel.prioWrA J.ntfn hP.n hP.e ht hy hv + have sr2 : IpcSchedRelA st Z m κ s2 σ2 := ⟨Z.Q, Z.R, J.sr.prioWrA ht hy hq0 hv⟩ + have hH : Notif.SchedHypsA st N Z.T (IpcSchedRelA st Z m κ) := + (ipcSchedOpsA (ar := ar) J.iplaced J.ipcPlace hc hmb hmax htu).1 + have hx1 := J.ntfn.at_ntfn hE hk + have hx1o := hx1.obj + rw [hob] at hx1o + obtain ⟨-, zs, -, hzs, hQ1, hB1⟩ := hx1o + have hs : P1.PrioSorted (fun u => L.mem s (prioA u)) zs := by + have h := J.ipcSorted.2 (specPtr e) ⟨e, hE, rfl⟩ x hk + rw [hob] at h + change PrioSortedA σ q0 at h + rw [hzs] at h + exact prioSorted_ntfnA J.ntfn hQ1.mem h + have hsort : ∀ xs, QueueRel (L.mem s2) Z.T .Ep (qOf (L.mem s2) e) xs → + P1.PrioSorted (fun u => L.mem s2 (prioA u)) (xs.erase t) := by + intro xs hxs + have hx2o := (ntfn2.at_ntfn hE hk2).obj + rw [hob] at hx2o + obtain ⟨-, ys0, -, hys0, hQ2, -⟩ := hx2o + have e1 : xs = ys0 := hxs.unique hP.e.tcb hQ2 + have e2 : ys0 = zs := List.map_injective_iff.2 (fun a b h => specPtr_inj h) (hys0.symm.trans hzs) + rw [e1, e2] + have hs' : P1.PrioSorted (fun u => L.mem s (prioA u)) (zs.erase t) := + List.Pairwise.sublist List.erase_sublist hs + have same : ∀ u ∈ zs.erase t, L.mem s2 (prioA u) = L.mem s (prioA u) := fun u hu => by + have hne : u ≠ t := fun h => hQ1.nodup.not_mem_erase (h ▸ hu) + have huT := hQ1.mem u (List.mem_of_mem_erase hu) + have hua := hP.e.tcb u huT + refine off _ (a_prio hua).1 ?_ + rw [val_prio hua] + have := tcb_disj hP.e ht huT hne + omega + refine List.Pairwise.imp_of_mem (fun {a b} ha hb h => ?_) hs' + dsimp only + rw [same a ha, same b hb] + exact h + obtain ⟨s3, h3, σ3, q1, W, eσ3, hspec, eq1, hmemn, sr3, ntfn, K3, hW⟩ := exists_of_specA + (ntfn_reorder_relA (st := st) hH hP.n s2 σ2 ntfn2 sr2 ht hy2 hE hk2 hob htq0 hsort) + obtain ⟨yn, hyn⟩ : ∃ yn : Notification, yn = { x with ntfnObj := .waitingNtfn q1 } := ⟨_, rfl⟩ + rw [← hyn] at eσ3 + have key3 : ∀ q, σ3.kheap q = if q = specPtr e then some (.notification yn) + else if q = specPtr t then some (.tcb y') else σ.kheap q := by + intro q + rw [eσ3, ← key2] + simp only [updK, Function.update_apply] + have hy3 : σ3.kheap (specPtr t) = some (.tcb y') := by rw [key3, if_neg htp, if_pos rfl] + have hK : Sched.BodyKeepA σ σ3 := by + refine Sched.BodyKeepA.ofKh (by rw [eσ3]; rfl) (by rw [eσ3]; rfl) (by rw [eσ3]; rfl) (by rw [eσ3]; rfl) + (by rw [eσ3]; rfl) (by rw [eσ3]; rfl) (by rw [eσ3]; rfl) (fun q => ?_) + by_cases c1 : q = specPtr e + · refine Or.inr (Or.inr fun o ho => ?_) + rw [key3, if_pos c1] at ho + cases ho + exact ⟨fun _ _ h => (by cases h), fun _ h => (by cases h)⟩ + · by_cases c2 : q = specPtr t + · subst c2 + exact Or.inr (Or.inl ⟨y, y', hy, hy3, rfl⟩) + · exact Or.inl (by rw [key3, if_neg c1, if_neg c2]) + -- the concrete program + have hsp : sched.set_priority st s t v = ok s3 := by + have hw : L.mem s (stA t) = sched.TS_BLOCKED_ON_NTFN := by + have c := r0.st + rw [hb] at c + exact UScalar.eq_of_val_eq (by simpa [Notif.tsCode, sched.TS_BLOCKED_ON_NTFN] using c) + have hsetp : obj.Tcb.set_prio st t s v = ok s2 := eq_of_spec_fd (obj.Tcb.set_prio.spec s t v) + have hblk : obj.Tcb.block_obj st t s2 = ok e := by + have h1 : L.mem s2 (wa t 3336#u64) = L.mem s (wa t 3336#u64) := by + have hv' := Notif.tfield_val (K := 3336#u64) hta (by simp) + exact off _ (field_aligned hta (by simp) (by simp)) (by rw [hv']; simp) + exact (eq_of_spec_fd (Fl4ProofsA64.P1.obj.Tcb.block_obj.spec s2 t)).trans (congrArg ok h1) + rw [sched.set_priority, eq_of_spec_fd (Fl4ProofsA64.P1.obj.Tcb.state.spec s t), bind_ok, + show L.mem s (stA t) = _ from hw] + simp (config := {decide := true}) only [sched.TS_RUNNING, sched.TS_RESTART, sched.TS_RUNNING_VM, + sched.TS_BLOCKED_ON_RECV, sched.TS_BLOCKED_ON_SEND, sched.TS_BLOCKED_ON_NTFN, ite_false, ite_true, + hsetp, hblk, bind_ok, h3] + have hSpecP : setPriority (Z := Unit) (specPtr t) (BitVec.ofNat 8 v.val) σ = ({((), σ3)}, False) := by + have hSet0 : threadSetPriority (Z := Unit) (specPtr t) (BitVec.ofNat 8 v.val) σ = ({((), σ2)}, False) := + threadSet_eqSc (fun z => {z with tcbPriority := BitVec.ofNat 8 v.val}) hy + have hnr : ¬ runnable y.tcbState = true := by rw [hb]; simp [runnable] + have hep : epBlocked y.tcbState = none := by rw [hb]; rfl + have hnb : ntfnBlocked y.tcbState = some (specPtr e) := by rw [hb]; rfl + unfold setPriority + rw [nd_bind_oneSc (Notif.getThreadState_eq hy), if_neg hnr, nd_bind_oneSc hSet0, hep, hnb] + simp only [Fl4Spec.NonDet.maybeM] + exact (nd_bind_oneSc (m := Fl4Spec.NonDet.pure ()) (a := ()) rfl _).trans hspec + refine ⟨s3, σ3, hsp, hSpecP, hK, fun bxi sysl symL userOK j7 => ?_⟩ + -- memory frames from `s` + have hQW : ∀ w ∈ W, w ∈ Z.T := fun w hw => (hW w hw).1 + have nal : ∀ K : U64, K.val < 40 → K.val % 8 = 0 → Aligned (wa e K) ∧ (wa e K).val = e.val + K.val := + fun K h1 h2 => ⟨(nword hP.n hE h1 h2).1, nfield_val (K := K) (hP.n.ntfn e hE) (by omega)⟩ + have f23 : ∀ b : U64, Aligned b → (b.val < e.val ∨ e.val + 40 ≤ b.val) → + (∀ w ∈ W, b.val ≠ w.val + 3496 ∧ b.val ≠ w.val + 3504) → L.mem s3 b = L.mem s2 b := + fun b hb h1 h2 => by + refine K3.agree b hb (fun w hw => ?_) (fun c => ?_) (fun c => ?_) + · have hwa := hP.e.tcb w (hQW w hw) + refine ⟨fun c => (h2 w hw).1 ?_, fun c => (h2 w hw).2 ?_⟩ + · rw [c]; exact Endp.val_next hwa + · rw [c]; exact Endp.val_prev hwa + · have := (nal 0#u64 (by simp) (by simp)).2 + rw [Notif.epHeadA_eq] at c + have hc := congrArg (fun x : U64 => x.val) c + have h0 : (0#u64 : U64).val = 0 := rfl + omega + · have := (nal 8#u64 (by simp) (by simp)).2 + rw [Notif.epTSA_eq] at c + have hc := congrArg (fun x : U64 => x.val) c + have h0 : (8#u64 : U64).val = 8 := rfl + omega + have tcbOut : ∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → + (wa u K).val < e.val ∨ e.val + 40 ≤ (wa u K).val := fun u hu K h1 h2 => by + have hua := hP.e.tcb u hu + have := hP.n.disj e hE u hu + rw [Notif.tfield_val hua h1] + omega + have tf : ∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → K.val ≠ 3496 → K.val ≠ 3504 → + L.mem s3 (wa u K) = L.mem s2 (wa u K) := fun u hu K h1 h2 h4 h5 => by + have hua := hP.e.tcb u hu + refine f23 _ (field_aligned hua h1 h2) (tcbOut u hu K h1 h2) fun w hw => ?_ + rw [Notif.tfield_val hua h1] + by_cases ew : w = u + · subst ew; omega + · have := tcb_disj hP.e (hQW w hw) hu (Ne.symm ew) + omega + have tw : ∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → K.val ≠ 3448 → K.val ≠ 3496 → K.val ≠ 3504 → + L.mem s3 (wa u K) = L.mem s (wa u K) := fun u hu K h1 h2 h3 h4 h5 => by + have hua := hP.e.tcb u hu + rw [tf u hu K h1 h2 h4 h5] + refine off _ (field_aligned hua h1 h2) ?_ + rw [Notif.tfield_val hua h1] + by_cases eu : u = t + · subst eu; omega + · have := tcb_disj hP.e ht hu eu + omega + -- the write set + have w1 : WritesIn st s s2 (fun b => b = prioA t) := writesIn_wr aP + have wN : WritesIn st s s3 (fun b => SchedStepNWA (st := st) N E Rp Z m σ s b ∨ BufW Bf b) := + (w1.trans K3.foot).mono fun b hb => hb.elim + (fun hb => Or.inl (Or.inl (Or.inr (Endp.tcbStepWA_of_off hP.e.tcb ht 3448 (by norm_num) + (by norm_num) (by norm_num) (by norm_num) (by rw [hb]; exact vP))))) + (fun hb => Or.inl (nwrite_stepNA J.sr.core.tcbs hb)) + -- the spec frame + have keepO : ∀ q o, σ.kheap q = some o → (∀ z, o ≠ .tcb z) → (∀ z, o ≠ .notification z) → σ3.kheap q = some o := + fun q o h n1 n2 => by + rw [key3, if_neg (fun c => by rw [c, hk] at h; exact n2 x (Option.some.inj h).symm), + if_neg (fun c => by rw [c, hy] at h; exact n1 y (Option.some.inj h).symm)] + exact h + have backO : ∀ q o, σ3.kheap q = some o → (∀ z, o ≠ .tcb z) → (∀ z, o ≠ .notification z) → σ.kheap q = some o := + fun q o h n1 n2 => by + rw [key3] at h + split at h + · exact absurd (Option.some.inj h).symm (n2 _) + · split at h + · exact absurd (Option.some.inj h).symm (n1 _) + · exact h + have fwdT : ∀ q z, σ.kheap q = some (.tcb z) → ∃ w, σ3.kheap q = some (.tcb w) ∧ + w = { z with tcbPriority := w.tcbPriority } := by + intro q z h + have ne : q ≠ specPtr e := fun c => by rw [c, hk] at h; cases h + by_cases c : q = specPtr t + · rw [c, hy] at h; cases h + exact ⟨y', c ▸ hy3, rfl⟩ + · exact ⟨z, by rw [key3, if_neg ne, if_neg c]; exact h, rfl⟩ + have bwdT : ∀ q w, σ3.kheap q = some (.tcb w) → ∃ z, σ.kheap q = some (.tcb z) ∧ + w = { z with tcbPriority := w.tcbPriority } := by + intro q w h + rw [key3] at h + split at h + · cases h + · split at h + · rename_i c + cases h + exact ⟨y, c ▸ hy, rfl⟩ + · exact ⟨w, h, rfl⟩ + have hE3 : σ3.kheap (specPtr e) = some (.notification yn) := by rw [key3, if_pos rfl] + have HS : SpecSchedA σ σ3 := + { kh := fun q => by + by_cases c : q = specPtr e + · subst c + exact Or.inr (Or.inr ⟨_, _, hk, hE3, Or.inr (Or.inl ⟨x, yn, rfl, rfl⟩)⟩) + · by_cases d : q = specPtr t + · subst d + exact Or.inr (Or.inl ⟨y, y', hy, hy3, rfl, rfl, rfl⟩) + · exact Or.inl (by rw [key3, if_neg c, if_neg d]) + irqS := by rw [eσ3]; rfl + irqN := by rw [eσ3]; rfl + asid := by rw [eσ3]; rfl + vcur := by rw [eσ3]; rfl + cdt := by rw [eσ3]; rfl + orig := by rw [eσ3]; rfl } + have SSz : SSzA σ σ3 := fun q z k h => ⟨z, backO _ _ h (fun _ c => by cases c) (fun _ c => by cases c)⟩ + -- the queue invariant + have pr32 : prioOf σ3 = prioOf σ2 := by + funext q + unfold prioOf + by_cases c : q = specPtr e + · rw [c, hE3, hk2] + · rw [key3, if_neg c, key2] + have hqE := not_queued_codeA J.ep ht hy (by rw [hb]; simp [Notif.tsCode]) + have Qk : QBlkA (specPtr '' E) (specPtr '' N) σ σ3 := by + refine ⟨J.ipcSorted.of_reorder (t := specPtr t) (yt := y') hy3 + (fun q z c h => ?_) (fun q w c h => ?_) (fun q hq x' h => ?_) (fun q hq x' h => ?_), + fun hA q z k h ne => hA q z k (backO _ _ h (fun _ c => by cases c) (fun _ c => by cases c)) ne, + fun q z h => ⟨z, backO _ _ h (fun _ c => by cases c) (fun _ c => by cases c), id⟩, + by rw [eσ3]; exact ⟨rfl, rfl, rfl, rfl⟩⟩ + · have ne : q ≠ specPtr e := fun d => by rw [d, hk] at h; cases h + exact ⟨z, by rw [key3, if_neg ne, if_neg c]; exact h, rfl⟩ + · have ne : q ≠ specPtr e := fun d => by rw [d, hE3] at h; cases h + rw [key3, if_neg ne, if_neg c] at h + exact ⟨w, h, rfl⟩ + · obtain ⟨e', he', rfl⟩ := hq + have h0 := backO _ _ h (fun _ c => by cases c) (fun _ c => by cases c) + exact ⟨x', h0, Or.inl ⟨List.Sublist.refl _, hqE e' he' x' h0⟩⟩ + · obtain ⟨n', hn', rfl⟩ := hq + by_cases c : specPtr n' = specPtr e + · rw [c, hE3] at h; cases h + have eqq : x.ntfnObj.ntfnQueue = q0 := by rw [hob]; rfl + refine ⟨x, c ▸ hk, Or.inr ⟨?_, fun u hu => ?_⟩⟩ + · have pr32 : prioOf σ3 = prioOf σ2 := by + funext q + unfold prioOf + by_cases c : q = specPtr e + · rw [c, hE3, hk2] + · rw [key3, if_neg c, key2] + rw [eqq, pr32, hyn] + exact eq1 + · rw [eqq, hzs] at hu + obtain ⟨w, hw, rfl⟩ := List.mem_map.1 hu + obtain ⟨z, hz, -⟩ := J.ntfn.tcb w (hQ1.mem w hw) + exact ⟨z, hz⟩ + · have h0 : σ.kheap (specPtr n') = some (.notification x') := by + rw [key3, if_neg c] at h + split at h + · cases h + · exact h + refine ⟨x', h0, Or.inl ⟨List.Sublist.refl _, fun hmem => ?_⟩⟩ + have h6 := (J.ntfn.at_ntfn hn' h0).obj.blocked hmem + exact c (by rw [← h6.2.2]) + -- the relations + have ep3 : EpStateRel Z.T E Rp Bf (L.mem s3) σ3 := by + refine ep2.frameX hP.e (fun b hb hk' => ?_) ?_ + · obtain ⟨hf, hnl⟩ := hk' + refine f23 b hb ?_ fun w hw => ?_ + · rcases hf with ⟨e', he', h⟩ | ⟨r, hr, h⟩ | ⟨u, hu, h⟩ | h + · have := hP.ne e hE e' he' + omega + · have := hP.nr e hE r hr + have := hP.e.rep r hr + omega + · have := hP.n.disj e hE u hu + have : u.val ≤ b.val ∧ b.val < u.val + 4096 := by + rcases h with h | h + · have r := h.range; omega + · unfold TL at h; omega + omega + · have := hP.nb e hE b h + omega + · have hwn : L.mem s2 (tBlockA w) ∉ E := fun c => by + rw [show L.mem s2 (tBlockA w) = e from (hW w hw).2.2] at c + have := hP.ne e hE e c + omega + refine ⟨fun c => hnl ⟨w, hQW w hw, hwn, Or.inl c⟩, fun c => hnl ⟨w, hQW w hw, hwn, Or.inr c⟩⟩ + · refine ⟨fun q z h => ?_, fun q z h => ?_, fun q z h => ?_, by rw [eσ3]; rfl⟩ + · have ne : q ≠ specPtr e := fun c => by rw [c, hk2] at h; cases h + rw [eσ3]; simp only [updK, Function.update_of_ne ne]; exact h + · have ne : q ≠ specPtr e := fun c => by rw [c, hk2] at h; cases h + exact ⟨z, by rw [eσ3]; simp only [updK, Function.update_of_ne ne]; exact h, rfl⟩ + · have ne : q ≠ specPtr e := fun c => by rw [c, hk2] at h; cases h + exact ⟨z, by rw [eσ3]; simp only [updK, Function.update_of_ne ne]; exact h, rfl, rfl, rfl, rfl, rfl⟩ + have reply : ReplyStackRelA (L.mem s3) Rp Z.C σ3 (scNameA m) stack := by + refine J.reply.frame (fun r hr => ?_) + (fun c _ z k h => ⟨z, k, keepO _ _ h (fun _ c => by cases c) (fun _ c => by cases c), rfl⟩) + (fun r _ x' h => ⟨x', backO _ _ h (fun _ c => by cases c) (fun _ c => by cases c), rfl⟩) + have hr3 := hP.e.rep r hr + have one : ∀ O : U64, O.val = 8 ∨ O.val = 16 → L.mem s3 (wa r O) = L.mem s (wa r O) := by + intro O hO + have hv' : (wa r O).val = r.val + O.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have hal : Aligned (wa r O) := by unfold Aligned; rw [hv']; omega + rw [f23 _ hal (by have := hP.nr e hE r hr; omega) fun w hw => ?_] + · refine off _ hal ?_ + have := hP.e.rt r hr t ht + omega + · have := hP.e.rt r hr w (hQW w hw) + omega + exact ⟨one _ (Or.inr (by simp [layout.reply.NEXT])), one _ (Or.inl (by simp [layout.reply.PREV]))⟩ + have K : IpcKeepA σ σ3 := + ⟨fun q z h => by + obtain ⟨w, hw, ez⟩ := fwdT q z h + exact ⟨w, hw, by rw [ez], by rw [ez], by rw [ez], by rw [ez], by rw [ez], by rw [ez]⟩, + fun q z k h => ⟨z, k, keepO _ _ h (fun _ c => by cases c) (fun _ c => by cases c), rfl⟩, + fun q z k h => ⟨z, k, backO _ _ h (fun _ c => by cases c) (fun _ c => by cases c), rfl⟩⟩ + have sinv : SInvXA σ3 m n := by + refine J.sinv.of_fr + { tcb := fun q z h => by + obtain ⟨w, hw, ez⟩ := fwdT q z h + exact ⟨w, hw, by rw [ez], by rw [ez], by rw [ez]⟩ + tcb' := fun q w h => by + obtain ⟨z, hz, ez⟩ := bwdT q w h + exact ⟨z, hz, by rw [ez], by rw [ez], by rw [ez]⟩ + sc := fun q z k => ⟨fun h => backO q _ h (fun _ c => by cases c) (fun _ c => by cases c), + fun h => keepO q _ h (fun _ c => by cases c) (fun _ c => by cases c)⟩ + cur := by rw [eσ3]; rfl + idle := by rw [eσ3]; rfl + csc := by rw [eσ3]; rfl + rq := by rw [eσ3]; rfl + rls := by rw [eσ3]; rfl + act := by rw [eσ3]; rfl + time := by rw [eσ3]; rfl + cons := by rw [eσ3]; rfl } ?_ + rw [eσ3] + exact SpecIrqA.updNtfnA (J.sinv.irqN.updTcbA hy (x' := y') rfl) hk2 + have sidle : SIdleA σ3 m n := + { f3 := fun c hc => by + obtain ⟨z, hz, hs⟩ := J.sidle.f3 c hc + obtain ⟨w, hw, ez⟩ := fwdT _ z hz + exact ⟨w, hw, by rw [ez]; exact hs⟩ + f4 := fun z k h => J.sidle.f4 z k (backO _ _ h (fun _ c => by cases c) (fun _ c => by cases c)) + f5 := fun q z k h hs => J.sidle.f5 q z k (backO _ _ h (fun _ c => by cases c) (fun _ c => by cases c)) + (by rw [eσ3] at hs; exact hs) + f6 := J.sidle.f6.of_eq fun sl => by + unfold specCapAtA + by_cases c : sl.1 = specPtr e + · rw [key3, if_pos c, c, hk] + · by_cases d : sl.1 = specPtr t + · rw [key3, if_neg c, if_pos d, d, hy] + rfl + · rw [key3, if_neg c, if_neg d] + f7 := fun y k h => by + have h7 := J.sidle.f7 y k (backO _ _ h (fun _ c => by cases c) (fun _ c => by cases c)) + have hT : σ3.curTime = σ.curTime := by rw [eσ3]; rfl + rw [hT]; exact h7 + idleSlot := fun c hc => by + have h0 := J.sidle.idleSlot c hc + unfold vtCapA at h0 ⊢ + by_cases e1 : m.idleThreadOf c = specPtr e + · rw [key3, if_pos e1] <;> (rw [e1, hk] at h0; exact h0) + · by_cases e2 : m.idleThreadOf c = specPtr t + · rw [key3, if_neg e1, if_pos e2]; rw [e2, hy] at h0; exact h0 + · rw [key3, if_neg e1, if_neg e2]; exact h0 + idleBound := fun c hc => by + have h0 := J.sidle.idleBound c hc + unfold vcpuOfA at h0 ⊢ + by_cases e1 : m.idleThreadOf c = specPtr e + · rw [key3, if_pos e1] <;> (rw [e1, hk] at h0; exact h0) + · by_cases e2 : m.idleThreadOf c = specPtr t + · rw [key3, if_neg e1, if_pos e2]; rw [e2, hy] at h0; exact h0 + · rw [key3, if_neg e1, if_neg e2]; exact h0 } + have rv : RunVcpuInv st Z.T s3 := fun u hu h0 => by + have e1 : L.mem s3 (runVcpuA u) = L.mem s (runVcpuA u) := by + unfold runVcpuA + exact tw u hu 3672#u64 (by simp) (by simp) (by simp) (by simp) (by simp) + have e2 : L.mem s3 (Fl4ProofsA64.P1.stA u) = L.mem s (Fl4ProofsA64.P1.stA u) := by + rw [Fl4ProofsA64.P1.stA_eq] + exact tw u hu 3328#u64 (by simp) (by simp) (by simp) (by simp) (by simp) + rw [e2]; exact J.runVcpu u hu (e1 ▸ h0) + have blkQ : BlkQueuedA Z.T E N (L.mem s3) σ3 := by + have bm : ∀ u ∈ Z.T, L.mem s3 (Notif.tBlockA u) = L.mem s (Notif.tBlockA u) := fun u hu => by + rw [Notif.tBlockA_eq]; exact tw u hu 3336#u64 (by simp) (by simp) (by simp) (by simp) (by simp) + refine ⟨fun u hu w hw q hq => ?_, fun u hu w hw q hq => ?_⟩ + · obtain ⟨z, hz, ez⟩ := bwdT _ w hw + obtain ⟨hE', x0, hx0, hm⟩ := J.blkQ.ep u hu z hz q (by rw [ez] at hq; exact hq) + exact ⟨by rw [bm u hu]; exact hE', x0, + keepO _ _ hx0 (fun _ c => by cases c) (fun _ c => by cases c), hm⟩ + · obtain ⟨z, hz, ez⟩ := bwdT _ w hw + obtain ⟨hN', z', q1, hq1, ho, hm⟩ := J.blkQ.ntfn u hu z hz q (by rw [ez] at hq; exact hq) + refine ⟨by rw [bm u hu]; exact hN', ?_⟩ + by_cases c : q = specPtr e + · rw [c, hk] at hq1; cases hq1 + rw [hob] at ho; cases ho + exact ⟨yn, q1, c ▸ hE3, by rw [hyn], (hmemn _).2 hm⟩ + · exact ⟨z', q1, by rw [key3, if_neg c, if_neg (fun d => by rw [d, hy] at hq1; cases hq1)]; exact hq1, + ho, hm⟩ + have sv : SchedValidA σ3 := J.schedValid.congr (by rw [eσ3]; rfl) (by rw [eσ3]; rfl) (by rw [eσ3]; rfl) + (by rw [eσ3]; rfl) (by rw [eσ3]; rfl) (by rw [eσ3]; rfl) + (fun q z k h => keepO q _ h (fun _ c => by cases c) (fun _ c => by cases c)) + (fun u z h => by + obtain ⟨w, hw, ez⟩ := fwdT u z h + exact ⟨w, hw, by rw [ez]⟩) + (fun u w h => by + obtain ⟨z, hz, ez⟩ := bwdT u w h + exact ⟨z, hz, by rw [ez], by rw [ez]⟩) + have tw' : ∀ u ∈ Z.T, ∀ O : U64, O.val < 224 ∨ O.val = 3440 ∨ O.val = 3472 ∨ O.val = 3512 ∨ O.val = 3456 → O.val % 8 = 0 → + L.mem s3 (wa u O) = L.mem s (wa u O) := fun u hu O h1 h2 => + tw u hu O (by omega) h2 (by omega) (by omega) (by omega) + obtain ⟨Q, Rl, sr3'⟩ := sr3 + exact ⟨Q, Rl, J.of_ipcStepUSA (Q := Q) (Rl := Rl) (curOf := Z.curOf) (curScOf := Z.curScOf) wN HS SSz Qk + (fun _ _ => by rw [eσ3]; rfl) (fun _ _ => rfl) bxi sysl symL userOK j7 sr3' ep3 ntfn reply J.deadExt.reply + K tw' sinv sidle rv blkQ sv⟩ + +set_option maxHeartbeats 4000000 in +/-- **TCB SetPriority of a notification-blocked thread at a syscall entry** (module docstring). -/ +theorem JSyscallA.setPrioNtfnA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + {slot lbl len buf t p auth argSlot : U64} {blk call don : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Thread t) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hlbl : lbl = fl4_abi.label.TCBSetPriority) + (he : tcb.extra_with_length ⟨len, buf, call, slot, extra⟩ 0#usize 1#u64 = ok (.Ok argSlot)) + (hp : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 0#u64 = ok p) + (harg : argSlot ∈ OS) (hac : capA st s argSlot = .Thread auth) (hle : ¬ L.mem s (mcpA auth) < p) + {y : Tcb} (hy : σ.kheap (specPtr t) = some (.tcb y)) (htc : specPtr t ≠ σ.curThread) + {p0 : ObjRef} (hb : y.tcbState = .blockedOnNotification p0) : + ∃ xc σ1 σ2 s2, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + (∀ κs : CslotPtr, (.ok [], σ2) ∈ ((fl4InvokeTcb (.threadControlSched (specPtr t) κs none none + (some (BitVec.ofNat 8 p.val, specPtr auth)) none)).run σ1).1) ∧ + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (.Ok (), s2, a) ∧ + ∃ Q Rl, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s2 a σ2 m ∧ Sched.BodyEndA σ σ2 m := by + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hslot hcov J.actW J.running J.cur + have hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM := J.toJStage3EntryA.curNotVmA + obtain ⟨xc, hkc, hst, hsp, J1, -, -, -⟩ := J.toJStage3A.of_restartCurA RS.pl.choose_spec.choose_spec.choose_spec.choose_spec + RS.sym RS.run hnvm RS.cur RS.vs RS.vc + have ht : t ∈ Z.T := J.tcbs ▸ (J.toJStage3A.slotCapTy hslot).1.2.2.2.2.2 t hcap + have haT : auth ∈ Z.T := J.tcbs ▸ (J.toJStage3A.slotCapTy harg).1.2.2.2.2.2 auth hac + have T0 := J.toJStage3A.toTA J.sact J.noSwitch + have hstall : smp.remote_tcb_stall st ar s a t = ok (s, a) := T0.stall_singleA hCs h1 ht + set s1 := L.wr s (stA (L.curW s)) sched.TS_RESTART with hs1 + set σ1 := stUpdA σ σ.curThread xc .restart with hσ1 + have hr : tcb.restart_caller st s = ok s1 := by + unfold tcb.restart_caller + rw [cur_thread_ok, bind_ok, hst] + have ha : tcb.thread_arg st s argSlot 1#u64 = ok (.Ok auth) := by + unfold tcb.thread_arg + rw [capA_eq, bind_ok, hac] + have hy1 : σ1.kheap (specPtr t) = some (.tcb y) := by + rw [hσ1]; simp [stUpdA, setObjA, Function.update_of_ne htc]; exact hy + have hSt : StEqA Z.T (L.mem s1) σ1 := fun u hu z hz => (J1.ep.at_tcb hu hz).st + have hWidth : p.val < 256 := by + obtain ⟨x, -, hw⟩ := J.mcp auth haT + have hv : p.val ≤ (L.mem s (mcpA auth)).val := Nat.le_of_not_lt hle + rw [hw.1] at hv + exact Nat.lt_of_le_of_lt hv x.tcbMcpriority.isLt + have hnr : ¬ runnable y.tcbState = true := by rw [hb]; simp [runnable] + have hon1 : L.mem s1 (affA t) = L.cpu s1 := by + have x := hCs _ (J1.sr.core.aff t ht) + have y := hCs _ J1.sr.sch.cpuCs + exact UScalar.eq_of_val_eq (by omega) + have hnq : ∀ d q, specPtr t ∉ σ1.readyQueues d q := fun d q hm => by + obtain ⟨z, hz, hr⟩ := J1.sr.run.1 d q _ hm + rw [hy1] at hz; cases hz + exact hnr hr + have hq0 : (L.mem s1 (inqA t) &&& sched.Q_QUEUED) = 0#u64 := J1.sr.toSchedSRN.qBit0 ht hon1 hnq + have hcurNe : t ≠ L.mem s1 (L.nodeAt s1 (L.cpu s1) layout.node.CUR_THREAD) := fun e => htc (by + rw [show L.mem s1 (L.nodeAt s1 (L.cpu s1) layout.node.CUR_THREAD) = L.curW s1 from rfl] at e + rw [e, ← J1.sr.sch.curThread] + rfl) + have hc : m.curCore < fl4A64Platform.numNodes := by rw [J1.sr.sch.core]; exact hCs _ J1.sr.sch.cpuCs + have hns1 : ∀ u, σ1.schedulerAction ≠ .switchThread u := fun u => J.noSwitch u + obtain ⟨s3, σ3, hsp1, hSpec, hK, hJ⟩ := J1.prioNtfn_stepA (st := st) (ar := ar) hc hmb hmax htu hns1 ht hWidth hy1 hq0 hb + have hdec : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (.Ok (), s3, a) := by + rw [hcap, decodeTcb_programA, if_neg Bool.false_ne_true, hlbl, tcbSetPriority_postA hstall he hp ha hle hr, + hsp1, bind_ok] + have htH : t ∈ H.tcbs := by rw [J1.tcbs]; exact ht + have blk : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s3 := by + have h := sched.set_priority.blk (st := st) s1 t p (BXI.blk J1.sys.ent.bxi) htH hWidth + (fun e => absurd e hcurNe) (fun e => absurd e hcurNe) (fun e => absurd e hcurNe) + rw [hsp1, WP.spec_ok] at h + exact h.1 + obtain ⟨-, -, sysl, symL, -, userOK, j7⟩ := J.decodeFinA hA hM hPl hfm hslot hcov + (rfl : capA st s slot = capA st s slot) hext hSC (lbl := lbl) + (by rw [hcap, hlbl]; simp [DecArmS1aEnt, DecArmQH, DecArmQZ2, DecArmKQ2, DecArmKQ, DecArmKQ3, DecArmDelQ, + DecArmS1aC, capFam]) + (fun _ h => by rw [hcap] at h; cases h) (fun _ h => by rw [hcap] at h; cases h) hdec + σ3 m + obtain ⟨Q, Rl, J2⟩ := hJ (BlkOK.bxi blk) sysl symL userOK j7 + refine ⟨xc, σ1, σ3, s3, hkc, rfl, hsp, fun κs => ?_, hdec, Q, Rl, J2, Sched.BodyEndA.ofKeep J hkc hK⟩ + rw [tcbPriority_specOfA hSpec] + rfl + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3TcbPrioPlainA.lean b/proofs/Fl4ProofsA64/P3/Stage3TcbPrioPlainA.lean new file mode 100644 index 000000000..135501db2 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3TcbPrioPlainA.lean @@ -0,0 +1,223 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 3e): **TCB SetPriority of a thread whose priority word is the only write**: +inactive, blocked on a reply, or runnable but neither queued nor current. + +* **`setPriority_wordA`**: FL4's `set_priority` is one `PRIO` word write in these cases. +* **`tcbPriority_plainSpecA`**: the spec writes `tcbPriority` only. +* **`JSyscallA.setPrioPlainA`**: `decode_invocation` at a syscall entry (record step `JStage3A.prioWr_stepA`). + +Named premises as `JSyscallA.setPrioInactiveA`. The decode succeeds; the `Err` returns are not lifted. New module. +-/ +import Fl4ProofsA64.P3.Stage3TcbPrioA +import Fl4ProofsA64.P3.Stage3TcbBodyA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +theorem setPriority_wordA {s : S} {t p : U64} {y : Tcb} {P : Prop} + (hSt : (L.mem s (stA t)).val = tsCodeA y.tcbState) + (hpl : y.tcbState = .inactive ∨ (∃ r, y.tcbState = .blockedOnReply r) ∨ + (runnable y.tcbState = true ∧ P)) + (hq0 : (L.mem s (inqA t) &&& sched.Q_QUEUED) = 0#u64) + (hc : t ≠ L.curW s) : + sched.set_priority st s t p = ok (L.wr s (prioA t) p) := by + have hw : (L.mem s (stA t)) = 0#u64 ∨ (L.mem s (stA t)) = 5#u64 ∨ (L.mem s (stA t)) = sched.TS_RUNNING ∨ + (L.mem s (stA t)) = sched.TS_RESTART ∨ (L.mem s (stA t)) = sched.TS_RUNNING_VM := by + rcases hpl with h | ⟨r, h⟩ | ⟨h, -⟩ + · rw [h] at hSt; exact Or.inl (UScalar.eq_of_val_eq hSt) + · rw [h] at hSt; exact Or.inr (Or.inl (UScalar.eq_of_val_eq hSt)) + · revert h hSt + cases hs : y.tcbState with + | running => intro hSt _; exact Or.inr (Or.inr (Or.inl (UScalar.eq_of_val_eq (by simpa [tsCodeA, sched.TS_RUNNING] using hSt)))) + | restart => intro hSt _; exact Or.inr (Or.inr (Or.inr (Or.inl (UScalar.eq_of_val_eq (by simpa [tsCodeA, sched.TS_RESTART] using hSt))))) + | ext e => cases e; intro hSt _; exact Or.inr (Or.inr (Or.inr (Or.inr (UScalar.eq_of_val_eq (by simpa [tsCodeA, sched.TS_RUNNING_VM] using hSt))))) + | _ => intro _ h; simp [runnable] at h + have hq : sched.queued st s t = ok false := by + rw [eq_of_spec_fd (Fl4ProofsA64.P1.sched.queued.spec (L := L) s t)]; simp [hq0] + have hne : obj.Tcb.Insts.CoreCmpPartialEqTcb.eq t (L.curW s) = ok false := by + unfold obj.Tcb.Insts.CoreCmpPartialEqTcb.eq; simp [hc] + have hsp : obj.Tcb.set_prio st t s p = ok (L.wr s (prioA t) p) := eq_of_spec_fd (obj.Tcb.set_prio.spec s t p) + rw [sched.set_priority, eq_of_spec_fd (Fl4ProofsA64.P1.obj.Tcb.state.spec s t), bind_ok] + rcases hw with h | h | h | h | h <;> rw [h] <;> + simp (config := {decide := true}) only [sched.TS_RUNNING, sched.TS_RESTART, sched.TS_RUNNING_VM, + sched.TS_BLOCKED_ON_RECV, sched.TS_BLOCKED_ON_SEND, sched.TS_BLOCKED_ON_NTFN, ite_false, ite_true, + hq, bind_ok, Bool.false_eq_true, cur_thread_ok, hne, hsp] + +/-- The spec's `set_priority` of an inactive, reply-blocked, or runnable thread that is neither queued nor current +writes `tcbPriority` only. -/ +theorem setPriority_plainA {σ : State Unit} {t : ObjRef} + {x : Tcb} {P : Prop} (prio : Priority) (hk : σ.kheap t = some (.tcb x)) + (hpl : x.tcbState = .inactive ∨ (∃ r, x.tcbState = .blockedOnReply r) ∨ + (runnable x.tcbState = true ∧ P)) + (hc : t ≠ σ.curThread) (hnq : ∀ d q, t ∉ σ.readyQueues d q) : + setPriority t prio σ = ({((), updKSc σ t (.tcb {x with tcbPriority := prio}))}, False) := by + have hGet := Notif.getThreadState_eq hk + have hSet0 : threadSetPriority (Z := Unit) t prio σ = + ({((), updKSc σ t (.tcb {x with tcbPriority := prio}))}, False) := + threadSet_eqSc (fun y => {y with tcbPriority := prio}) hk + unfold setPriority + rw [nd_bind_oneSc hGet] + rcases hpl with h | ⟨r, h⟩ | ⟨h, -⟩ + · rw [if_neg (show ¬ runnable x.tcbState = true by rw [h]; decide), nd_bind_oneSc hSet0, h] + change (Fl4Spec.NonDet.pure () >>= fun _ => Fl4Spec.NonDet.pure ()) _ = _ + exact nd_bind_oneSc (m := Fl4Spec.NonDet.pure ()) (a := ()) rfl _ + · rw [if_neg (show ¬ runnable x.tcbState = true by rw [h]; simp [runnable]), nd_bind_oneSc hSet0, h] + change (Fl4Spec.NonDet.pure () >>= fun _ => Fl4Spec.NonDet.pure ()) _ = _ + exact nd_bind_oneSc (m := Fl4Spec.NonDet.pure ()) (a := ()) rfl _ + · rw [if_pos h] + have hd : threadGet (Z := Unit) Tcb.tcbDomain t σ = ({(x.tcbDomain, σ)}, False) := + threadGet_eqSc _ hk + have hp : threadGet (Z := Unit) Tcb.tcbPriority t σ = ({(x.tcbPriority, σ)}, False) := + threadGet_eqSc _ hk + rw [nd_bind_oneSc hd, nd_bind_oneSc hp, + nd_bind_oneSc (show getTcbQueue (Z := Unit) x.tcbDomain x.tcbPriority σ = + ({(σ.readyQueues x.tcbDomain x.tcbPriority, σ)}, False) from by + unfold getTcbQueue + exact nd_bind_oneSc (m := Fl4Spec.NonDet.gets _) rfl _), + nd_bind_oneSc (show (Fl4Spec.NonDet.gets fun s : State Unit => s.curThread) σ = + ({(σ.curThread, σ)}, False) from rfl), + if_neg (show ¬ (t ∈ σ.readyQueues x.tcbDomain x.tcbPriority ∨ t = σ.curThread) from + fun h => h.elim (hnq _ _) hc)] + exact hSet0 + +theorem tcbPriority_plainSpecA {σ : State Unit} {t auth : ObjRef} {slot : CslotPtr} + {x : Tcb} {P : Prop} (prio : Priority) (hk : σ.kheap t = some (.tcb x)) + (hpl : x.tcbState = .inactive ∨ (∃ r, x.tcbState = .blockedOnReply r) ∨ + (runnable x.tcbState = true ∧ P)) + (hc : t ≠ σ.curThread) (hnq : ∀ d q, t ∉ σ.readyQueues d q) : + (fl4InvokeTcb (.threadControlSched t slot none none (some (prio, auth)) none)).run σ = + ({(.ok [], updKSc σ t (.tcb {x with tcbPriority := prio}))}, False) := by + have hSet := setPriority_plainA prio hk hpl hc hnq + simp only [fl4InvokeTcb, fl4InstallTcbCap, Fl4Spec.NonDet.maybeM] + rw [run_bind_okD (run_returnOkD () σ), + run_bind_okD (run_liftE_stD (m := Fl4Spec.NonDet.pure ()) rfl), + run_bind_okD (run_liftE_stD hSet), + run_bind_okD (run_liftE_stD (m := Fl4Spec.NonDet.pure ()) rfl)] + rfl + +end + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s : S} {a : A} {σ : State Unit} {m : McState} + +set_option maxHeartbeats 4000000 in +/-- **TCB SetPriority of a thread that only has its priority word written** (module docstring). -/ +theorem JSyscallA.setPrioPlainA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + {slot lbl len buf t p auth argSlot : U64} {blk call don : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Thread t) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hlbl : lbl = fl4_abi.label.TCBSetPriority) + (he : tcb.extra_with_length ⟨len, buf, call, slot, extra⟩ 0#usize 1#u64 = ok (.Ok argSlot)) + (hp : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 0#u64 = ok p) + (harg : argSlot ∈ OS) (hac : capA st s argSlot = .Thread auth) (hle : ¬ L.mem s (mcpA auth) < p) + {y : Tcb} (hy : σ.kheap (specPtr t) = some (.tcb y)) (htc : specPtr t ≠ σ.curThread) + (hpl : y.tcbState = .inactive ∨ (∃ r, y.tcbState = .blockedOnReply r) ∨ + (runnable y.tcbState = true ∧ ∀ d q, specPtr t ∉ σ.readyQueues d q)) : + ∃ xc σ1 σ2 s2, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + (∀ κs : CslotPtr, (.ok [], σ2) ∈ ((fl4InvokeTcb (.threadControlSched (specPtr t) κs none none + (some (BitVec.ofNat 8 p.val, specPtr auth)) none)).run σ1).1) ∧ + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (.Ok (), s2, a) ∧ + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s2 a σ2 m ∧ Sched.BodyEndA σ σ2 m := by + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hslot hcov J.actW J.running J.cur + have hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM := J.toJStage3EntryA.curNotVmA + obtain ⟨xc, hkc, hst, hsp, J1, -, -, -⟩ := J.toJStage3A.of_restartCurA RS.pl.choose_spec.choose_spec.choose_spec.choose_spec + RS.sym RS.run hnvm RS.cur RS.vs RS.vc + have ht : t ∈ Z.T := J.tcbs ▸ (J.toJStage3A.slotCapTy hslot).1.2.2.2.2.2 t hcap + have haT : auth ∈ Z.T := J.tcbs ▸ (J.toJStage3A.slotCapTy harg).1.2.2.2.2.2 auth hac + have T0 := J.toJStage3A.toTA J.sact J.noSwitch + have hstall : smp.remote_tcb_stall st ar s a t = ok (s, a) := T0.stall_singleA hCs h1 ht + set s1 := L.wr s (stA (L.curW s)) sched.TS_RESTART with hs1 + set σ1 := stUpdA σ σ.curThread xc .restart with hσ1 + have hr : tcb.restart_caller st s = ok s1 := by + unfold tcb.restart_caller + rw [cur_thread_ok, bind_ok, hst] + have ha : tcb.thread_arg st s argSlot 1#u64 = ok (.Ok auth) := by + unfold tcb.thread_arg + rw [capA_eq, bind_ok, hac] + have hy1 : σ1.kheap (specPtr t) = some (.tcb y) := by + rw [hσ1]; simp [stUpdA, setObjA, Function.update_of_ne htc]; exact hy + have hSt : StEqA Z.T (L.mem s1) σ1 := fun u hu z hz => (J1.ep.at_tcb hu hz).st + have hWidth : p.val < 256 := by + obtain ⟨x, -, hw⟩ := J.mcp auth haT + have hv : p.val ≤ (L.mem s (mcpA auth)).val := Nat.le_of_not_lt hle + rw [hw.1] at hv + exact Nat.lt_of_le_of_lt hv x.tcbMcpriority.isLt + -- the target's state and ready-queue bit + have hcq : tsCode y.tcbState ≠ 3 ∧ tsCode y.tcbState ≠ 4 := by + rcases hpl with h | ⟨r, h⟩ | ⟨h, -⟩ + · rw [h]; simp [Notif.tsCode] + · rw [h]; simp [Notif.tsCode] + · revert h + cases y.tcbState <;> simp [Notif.tsCode, runnable] + have hnb : ∀ q, y.tcbState ≠ .blockedOnNotification q := fun q h => by + rcases hpl with h' | ⟨r, h'⟩ | ⟨h', -⟩ + · rw [h'] at h; cases h + · rw [h'] at h; cases h + · rw [h] at h'; cases h' + have hon1 : L.mem s1 (affA t) = L.cpu s1 := by + have x := hCs _ (J1.sr.core.aff t ht) + have y := hCs _ J1.sr.sch.cpuCs + exact UScalar.eq_of_val_eq (by omega) + have hnq : ∀ d q, specPtr t ∉ σ1.readyQueues d q := fun d q hm => by + rcases hpl with h | ⟨r, h⟩ | ⟨-, h⟩ + · obtain ⟨z, hz, hr⟩ := J1.sr.run.1 d q _ hm + rw [hy1] at hz; cases hz + rw [h] at hr; cases hr + · obtain ⟨z, hz, hr⟩ := J1.sr.run.1 d q _ hm + rw [hy1] at hz; cases hz + rw [h] at hr; cases hr + · exact h d q hm + have hq0 : (L.mem s1 (inqA t) &&& sched.Q_QUEUED) = 0#u64 := J1.sr.toSchedSRN.qBit0 ht hon1 hnq + have hcurNe : t ≠ L.mem s1 (L.nodeAt s1 (L.cpu s1) layout.node.CUR_THREAD) := fun e => htc (by + rw [show L.mem s1 (L.nodeAt s1 (L.cpu s1) layout.node.CUR_THREAD) = L.curW s1 from rfl] at e + rw [e, ← J1.sr.sch.curThread] + rfl) + have hsp1 : sched.set_priority st s1 t p = ok (L.wr s1 (prioA t) p) := + setPriority_wordA (hSt t ht y hy1) hpl hq0 (fun e => hcurNe e) + have hdec : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (.Ok (), L.wr s1 (prioA t) p, a) := by + rw [hcap, decodeTcb_programA, if_neg Bool.false_ne_true, hlbl, tcbSetPriority_postA hstall he hp ha hle hr, + hsp1, bind_ok] + have htH : t ∈ H.tcbs := by rw [J1.tcbs]; exact ht + have blk : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac (L.wr s1 (prioA t) p) := by + have h := sched.set_priority.blk (st := st) s1 t p (BXI.blk J1.sys.ent.bxi) htH hWidth + (fun e => absurd e hcurNe) (fun e => absurd e hcurNe) (fun e => absurd e hcurNe) + rw [hsp1, WP.spec_ok] at h + exact h.1 + obtain ⟨-, -, sysl, symL, -, userOK, j7⟩ := J.decodeFinA hA hM hPl hfm hslot hcov + (rfl : capA st s slot = capA st s slot) hext hSC (lbl := lbl) + (by rw [hcap, hlbl]; simp [DecArmS1aEnt, DecArmQH, DecArmQZ2, DecArmKQ2, DecArmKQ, DecArmKQ3, DecArmDelQ, + DecArmS1aC, capFam]) + (fun _ h => by rw [hcap] at h; cases h) (fun _ h => by rw [hcap] at h; cases h) hdec + (prioStateA σ1 (specPtr t) y (BitVec.ofNat 8 p.val)) m + have J2 := J1.prioWr_stepA (st := st) ht hWidth hy1 hq0 + hcq hnb + (BlkOK.bxi blk) sysl symL userOK j7 + refine ⟨xc, σ1, _, _, hkc, rfl, hsp, fun κs => ?_, hdec, J2, Sched.BodyEndA.ofTcbUpd J hkc hy1 rfl⟩ + rw [tcbPriority_plainSpecA _ hy1 hpl (fun e => htc e) hnq] + rfl + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3TcbPrioQA.lean b/proofs/Fl4ProofsA64/P3/Stage3TcbPrioQA.lean new file mode 100644 index 000000000..23756f6ab --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3TcbPrioQA.lean @@ -0,0 +1,115 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 3e): **TCB SetPriority of a runnable thread that is queued or current** +(arm 4): FL4's `set_priority` is `dequeue; set_prio; enqueue; reschedule_required` and the spec's `setPriority` is +`tcbSchedDequeue; thread_set_priority; tcbSchedEnqueue; reschedule_required`. + +* **`setPriority_queuedA`**: the concrete equation (the three runnable state words, queued or current). +* **`setPriority_queuedSpecA`**: the spec equation, and `fl4InvokeTcb` of a `threadControlSched` priority. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3TcbPrioPlainA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +theorem setPriority_queuedA {s sd se sr : S} {t p : U64} {y : Tcb} + (hSt : (L.mem s (stA t)).val = tsCodeA y.tcbState) (hrun : runnable y.tcbState = true) + (hqc : (L.mem s (inqA t) &&& sched.Q_QUEUED) ≠ 0#u64 ∨ t = L.curW s) + (ed : sched.dequeue st s t = ok sd) (ee : sched.enqueue st (L.wr sd (prioA t) p) t = ok se) + (er : sched.reschedule_required st se = ok sr) : sched.set_priority st s t p = ok sr := by + have hw : (L.mem s (stA t)) = sched.TS_RUNNING ∨ (L.mem s (stA t)) = sched.TS_RESTART ∨ + (L.mem s (stA t)) = sched.TS_RUNNING_VM := by + revert hSt hrun + cases hs : y.tcbState with + | running => intro hSt _; exact Or.inl (UScalar.eq_of_val_eq (by simpa [tsCodeA, sched.TS_RUNNING] using hSt)) + | restart => intro hSt _; exact Or.inr (Or.inl (UScalar.eq_of_val_eq (by simpa [tsCodeA, sched.TS_RESTART] using hSt))) + | ext e => cases e; intro hSt _; exact Or.inr (Or.inr (UScalar.eq_of_val_eq (by simpa [tsCodeA, sched.TS_RUNNING_VM] using hSt))) + | _ => intro _ h; simp [runnable] at h + have hsp : obj.Tcb.set_prio st t sd p = ok (L.wr sd (prioA t) p) := eq_of_spec_fd (obj.Tcb.set_prio.spec sd t p) + have hq : sched.queued st s t = ok (decide ((L.mem s (inqA t) &&& sched.Q_QUEUED) ≠ 0#u64)) := + eq_of_spec_fd (Fl4ProofsA64.P1.sched.queued.spec (L := L) s t) + have hct : sched.cur_thread st s = ok (L.curW s) := cur_thread_ok s + have key : (do + let b ← sched.queued st s t + if b then do + let s1 ← sched.dequeue st s t + let s2 ← obj.Tcb.set_prio st t s1 p + let s3 ← sched.enqueue st s2 t + sched.reschedule_required st s3 + else do + let t1 ← sched.cur_thread st s + let b1 ← obj.Tcb.Insts.CoreCmpPartialEqTcb.eq t t1 + if b1 then do + let s1 ← sched.dequeue st s t + let s2 ← obj.Tcb.set_prio st t s1 p + let s3 ← sched.enqueue st s2 t + sched.reschedule_required st s3 + else obj.Tcb.set_prio st t s p) = ok sr := by + rw [hq, bind_ok] + by_cases h : (L.mem s (inqA t) &&& sched.Q_QUEUED) ≠ 0#u64 + · rw [if_pos (decide_eq_true h)] + simp only [ed, hsp, ee, er, bind_ok] + · have hc : t = L.curW s := hqc.resolve_left h + have hne : obj.Tcb.Insts.CoreCmpPartialEqTcb.eq t (L.curW s) = ok true := by + unfold obj.Tcb.Insts.CoreCmpPartialEqTcb.eq; simp [hc] + rw [if_neg (by simp [h]), hct, bind_ok, hne, bind_ok, if_pos rfl] + simp only [ed, hsp, ee, er, bind_ok] + rw [sched.set_priority, eq_of_spec_fd (Fl4ProofsA64.P1.obj.Tcb.state.spec s t), bind_ok] + rcases hw with h | h | h <;> rw [h] <;> + simp (config := {decide := true}) only [sched.TS_RUNNING, sched.TS_RESTART, sched.TS_RUNNING_VM, + sched.TS_BLOCKED_ON_RECV, sched.TS_BLOCKED_ON_SEND, sched.TS_BLOCKED_ON_NTFN, ite_false, ite_true] <;> + exact key + +end + +/-- The spec's `set_priority` of a runnable thread that is queued or current. -/ +theorem setPriority_queuedSpecA {σ σd σp σe σr : State Unit} {t : ObjRef} {x : Tcb} (prio : Priority) + (hk : σ.kheap t = some (.tcb x)) (hrun : runnable x.tcbState = true) + (hqc : t ∈ σ.readyQueues x.tcbDomain x.tcbPriority ∨ t = σ.curThread) + (d : tcbSchedAction (Z := Unit) tcbSchedDequeue t σ = ({((), σd)}, False)) + (kd : σd.kheap = σ.kheap) + (hp : σp = updKSc σd t (.tcb {x with tcbPriority := prio})) + (e : tcbSchedAction (Z := Unit) tcbSchedEnqueue t σp = ({((), σe)}, False)) + (r : rescheduleRequired (Z := Unit) σe = ({((), σr)}, False)) : + setPriority t prio σ = ({((), σr)}, False) := by + have hGet := Notif.getThreadState_eq hk + have hd : threadGet (Z := Unit) Tcb.tcbDomain t σ = ({(x.tcbDomain, σ)}, False) := threadGet_eqSc _ hk + have hpr : threadGet (Z := Unit) Tcb.tcbPriority t σ = ({(x.tcbPriority, σ)}, False) := threadGet_eqSc _ hk + have hkd : σd.kheap t = some (.tcb x) := by rw [kd]; exact hk + have hSet : threadSetPriority (Z := Unit) t prio σd = ({((), σp)}, False) := by + rw [hp]; exact threadSet_eqSc (fun y => {y with tcbPriority := prio}) hkd + unfold setPriority + rw [nd_bind_oneSc hGet, if_pos hrun, nd_bind_oneSc hd, nd_bind_oneSc hpr, + nd_bind_oneSc (show getTcbQueue (Z := Unit) x.tcbDomain x.tcbPriority σ = + ({(σ.readyQueues x.tcbDomain x.tcbPriority, σ)}, False) from by + unfold getTcbQueue + exact nd_bind_oneSc (m := Fl4Spec.NonDet.gets _) rfl _), + nd_bind_oneSc (show (Fl4Spec.NonDet.gets fun s : State Unit => s.curThread) σ = + ({(σ.curThread, σ)}, False) from rfl), if_pos hqc, nd_bind_oneSc d, nd_bind_oneSc hSet, nd_bind_oneSc e] + exact r + +/-- `fl4InvokeTcb` of a `threadControlSched` priority change is `set_priority`. -/ +theorem fl4InvokeTcb_setPriorityA {σ σr : State Unit} {t auth : ObjRef} {slot : CslotPtr} (prio : Priority) + (hSet : setPriority t prio σ = ({((), σr)}, False)) : + (fl4InvokeTcb (.threadControlSched t slot none none (some (prio, auth)) none)).run σ = + ({(.ok [], σr)}, False) := by + simp only [fl4InvokeTcb, fl4InstallTcbCap, Fl4Spec.NonDet.maybeM] + rw [run_bind_okD (run_returnOkD () σ), + run_bind_okD (run_liftE_stD (m := Fl4Spec.NonDet.pure ()) rfl), + run_bind_okD (run_liftE_stD hSet), + run_bind_okD (run_liftE_stD (m := Fl4Spec.NonDet.pure ()) rfl)] + rfl + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3TcbPrioQArmA.lean b/proofs/Fl4ProofsA64/P3/Stage3TcbPrioQArmA.lean new file mode 100644 index 000000000..805ea2cc3 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3TcbPrioQArmA.lean @@ -0,0 +1,293 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 3e): **TCB SetPriority of a runnable thread that is queued or current** +(`JSyscallA.setPrioQueuedA`): `decode_invocation` at a syscall entry for the SetPriority label, `set_priority` being +`dequeue; set_prio; enqueue; reschedule_required` (`setPriority_queuedA`, spec `setPriority_queuedSpecA`), the record step +`JStage3A.prioQ_stepA`. + +* `SchedSRA.qBitNeA`: a thread of the current core in a spec ready queue has its `Q_QUEUED` bit set. +* `prioStageA`: the scheduler relation and the state words across the `PRIO` write after the dequeue. + +Named premises as `JSyscallA.setPrioInactiveA`. The decode succeeds; the `Err` returns are not lifted. New module. +-/ +import Fl4ProofsA64.P3.Stage3TcbPrioQStepA +import Fl4ProofsA64.P3.Stage3TcbBodyA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +theorem Sched.SchedSRA.qBitNeA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRA st Z m κ s σ) {t : U64} (ht : t ∈ Z.T) (hon : L.mem s (affA t) = L.cpu s) {x : Tcb} + (hk : σ.kheap (specPtr t) = some (.tcb x)) (hm : specPtr t ∈ σ.readyQueues x.tcbDomain x.tcbPriority) : + (L.mem s (inqA t) &&& sched.Q_QUEUED) ≠ 0#u64 := by + intro h0 + obtain ⟨x', hk', hd, -, hpr⟩ := H.rest.prio t ht + rw [hk] at hk' + obtain rfl := KernelObject.tcb.inj (Option.some.inj hk') + have hpe := ofNat_prio_eqA hpr + have hq0 := H.rest.rq _ (H.core.prio t ht) + rw [hpe] at hq0 + rw [hd, hq0, ← hon, mem_map_specPtrSc] at hm + exact H.core.notQ ht h0 (H.core.aff t ht) (H.core.prio t ht) hm + +theorem prioStageA {Z : XSetsA} {m : McState} {κ : U64 → U64} {sd : S} {σd : State Unit} + (H : SchedSRA st Z m κ sd σd) (S0 : StEqA Z.T (L.mem sd) σd) {t v : U64} (ht : t ∈ Z.T) {y : Tcb} + (hy : σd.kheap (specPtr t) = some (.tcb y)) (hq0 : (L.mem sd (inqA t) &&& sched.Q_QUEUED) = 0#u64) + (hv : v.val < 256) : + SchedSRA st Z m κ (L.wr sd (prioA t) v) (prioStateA σd (specPtr t) y (BitVec.ofNat 8 v.val)) ∧ + StEqA Z.T (L.mem (L.wr sd (prioA t) v)) (prioStateA σd (specPtr t) y (BitVec.ofNat 8 v.val)) := by + have aP : Aligned (prioA t) := (a_prio (H.core.tcbs t ht)).1 + have vP := val_prio (H.core.tcbs t ht) + refine ⟨H.prioWrA ht hy hq0 hv, S0.keep (fun u hu => ?_) (fun u x hx => ?_)⟩ + · have hua := H.core.tcbs u hu + rw [stA_eq] + refine mem_wr_ne' aP (field_aligned hua (by simp) (by simp)) (fun e => ?_) + have k : (3328#u64 : U64).val = 3328 := by simp + have e' : (wa u 3328#u64).val = t.val + 3448 := by rw [← e]; exact vP + rw [Notif.tfield_val hua (by simp)] at e' + by_cases e2 : u = t + · subst e2; omega + · have := H.place.apart u t .tcb .tcb ((H.thT u).2 hu) ((H.thT t).2 ht) e2 + rw [show ObjKindA.size .tcb = 4096 from rfl] at this + omega + · obtain ⟨z, hz, e⟩ := prioBwdA hy hx + exact ⟨z, hz, by rw [e]⟩ + +end + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s : S} {a : A} {σ : State Unit} {m : McState} + +set_option maxHeartbeats 4000000 in +/-- **TCB SetPriority of a runnable thread that is queued or current** (module docstring). -/ +theorem JSyscallA.setPrioQueuedA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + {slot lbl len buf t p auth argSlot : U64} {blk call don : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Thread t) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hlbl : lbl = fl4_abi.label.TCBSetPriority) + (he : tcb.extra_with_length ⟨len, buf, call, slot, extra⟩ 0#usize 1#u64 = ok (.Ok argSlot)) + (hp : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 0#u64 = ok p) + (harg : argSlot ∈ OS) (hac : capA st s argSlot = .Thread auth) (hle : ¬ L.mem s (mcpA auth) < p) + {y : Tcb} (hy : σ.kheap (specPtr t) = some (.tcb y)) (hrun : runnable y.tcbState = true) + (hqc : (∃ d q, specPtr t ∈ σ.readyQueues d q) ∨ specPtr t = σ.curThread) : + ∃ xc σ1 σ2 s2, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + (∀ κs : CslotPtr, (.ok [], σ2) ∈ ((fl4InvokeTcb (.threadControlSched (specPtr t) κs none none + (some (BitVec.ofNat 8 p.val, specPtr auth)) none)).run σ1).1) ∧ + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (.Ok (), s2, a) ∧ + ∃ Q Rl, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s2 a σ2 m ∧ Sched.BodyEndA σ σ2 m := by + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hslot hcov J.actW J.running J.cur + have hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM := J.toJStage3EntryA.curNotVmA + obtain ⟨xc, hkc, hst, hsp, J1, -, -, -⟩ := J.toJStage3A.of_restartCurA RS.pl.choose_spec.choose_spec.choose_spec.choose_spec + RS.sym RS.run hnvm RS.cur RS.vs RS.vc + have ht : t ∈ Z.T := J.tcbs ▸ (J.toJStage3A.slotCapTy hslot).1.2.2.2.2.2 t hcap + have haT : auth ∈ Z.T := J.tcbs ▸ (J.toJStage3A.slotCapTy harg).1.2.2.2.2.2 auth hac + have T0 := J.toJStage3A.toTA J.sact J.noSwitch + have hstall : smp.remote_tcb_stall st ar s a t = ok (s, a) := T0.stall_singleA hCs h1 ht + set s1 := L.wr s (stA (L.curW s)) sched.TS_RESTART with hs1 + set σ1 := stUpdA σ σ.curThread xc .restart with hσ1 + have hr : tcb.restart_caller st s = ok s1 := by + unfold tcb.restart_caller + rw [cur_thread_ok, bind_ok, hst] + have ha : tcb.thread_arg st s argSlot 1#u64 = ok (.Ok auth) := by + unfold tcb.thread_arg + rw [capA_eq, bind_ok, hac] + have hWidth : p.val < 256 := by + obtain ⟨x, -, hw⟩ := J.mcp auth haT + have hv : p.val ≤ (L.mem s (mcpA auth)).val := Nat.le_of_not_lt hle + rw [hw.1] at hv + exact Nat.lt_of_le_of_lt hv x.tcbMcpriority.isLt + -- the target's TCB after the restart write + obtain ⟨y1, hy1, hy1c, hy1o⟩ : ∃ y1, σ1.kheap (specPtr t) = some (.tcb y1) ∧ + (specPtr t = σ.curThread → y1.tcbState = .restart) ∧ (specPtr t ≠ σ.curThread → y1 = y) := by + by_cases e : specPtr t = σ.curThread + · rw [e, hkc] at hy + obtain rfl := KernelObject.tcb.inj (Option.some.inj hy) + exact ⟨{ xc with tcbState := .restart }, by rw [e]; simp only [hσ1, stUpdA, setObjA, Function.update_self], + fun _ => rfl, fun h => absurd e h⟩ + · refine ⟨y, ?_, fun h => absurd h e, fun _ => rfl⟩ + rw [hσ1]; simp [stUpdA, setObjA, Function.update_of_ne e]; exact hy + have hrun1 : runnable y1.tcbState = true := by + by_cases e : specPtr t = σ.curThread + · rw [hy1c e]; rfl + · rw [hy1o e]; exact hrun + have hSt : StEqA Z.T (L.mem s1) σ1 := fun u hu z hz => (J1.ep.at_tcb hu hz).st + have hcq : tsCode y1.tcbState ≠ 3 ∧ tsCode y1.tcbState ≠ 4 := by + revert hrun1 + cases y1.tcbState <;> simp [Notif.tsCode, runnable] + have hnb : ∀ q, y1.tcbState ≠ .blockedOnNotification q := fun q h => by + rw [h] at hrun1; simp [runnable] at hrun1 + have hon1 : L.mem s1 (affA t) = L.cpu s1 := by + have x := hCs _ (J1.sr.core.aff t ht) + have y := hCs _ J1.sr.sch.cpuCs + exact UScalar.eq_of_val_eq (by omega) + -- queued or current, on both sides + have hqc1 : specPtr t ∈ σ1.readyQueues y1.tcbDomain y1.tcbPriority ∨ specPtr t = σ1.curThread := by + rcases hqc with ⟨d, q, hm⟩ | h + · left + obtain ⟨hd, hq⟩ := rqDomPrio_of_schedA J1.sr _ d q y1 hy1 hm + rw [← hd, ← hq]; exact hm + · right; exact h + have hqcC : (L.mem s1 (inqA t) &&& sched.Q_QUEUED) ≠ 0#u64 ∨ t = L.curW s1 := by + rcases hqc1 with hm | h + · exact Or.inl (J1.sr.qBitNeA ht hon1 hy1 hm) + · exact Or.inr (specPtr_inj (h.trans J1.sr.sch.curThread)) + have hcw : L.curW s1 = L.curW s := specPtr_inj (J1.sr.sch.curThread.symm.trans J.sr.sch.curThread) + -- the spec invariants at the restart state and the target + have sact1 : SActA σ1 := + ⟨fun u h => by rw [hσ1] at h ⊢; exact J.sact.f1 u h, + ⟨{ xc with tcbState := .restart }, by simp only [hσ1, stUpdA, setObjA, Function.update_self], + Or.inr (Or.inl rfl)⟩⟩ + have L1 : LoopInvA σ1 m n := ⟨J1.sinv, J1.sidle, sact1⟩ + have hnidle : ∀ c < n, m.idleThreadOf c ≠ specPtr t := fun c hc e => by + obtain ⟨z, hz, hz'⟩ := J1.sidle.f3 c hc + rw [e, hy1] at hz + obtain rfl := KernelObject.tcb.inj (Option.some.inj hz) + rw [hz'] at hrun1 + cases hrun1 + have hT : TgtA (specPtr t) σ1 := by + rcases hqc1 with hm | hc + · exact J1.schedValid.rq _ _ _ hm + · have hcn : σ1.curThread ≠ σ1.idleThread := fun e => by + obtain ⟨x, hx, hs⟩ := J1.sinv.idleTcb + rw [← e, ← hc, hy1] at hx + obtain rfl := KernelObject.tcb.inj (Option.some.inj hx) + rw [hy1c hc] at hs + cases hs + have := J1.schedValid.act J.act hcn + rw [← hc] at this + exact this + -- dequeue + obtain ⟨sd, ed, σd, d, kd, rd, -, SRd, Fd⟩ := exists_of_specA (dequeue_refines_localA J1.sr ht hon1) + have hond := yieldAffKeepA J1.sr SRd ht ht hon1 + have dm : ((), σd) ∈ (tcbSchedAction (Z := Unit) tcbSchedDequeue (specPtr t) σ1).1 := by + rw [d]; exact Set.mem_singleton _ + have hnqd : NoQ (specPtr t) σd := + (deqD (specPtr t) (fun _ => True) (fun _ _ _ => trivial) σ1 + ⟨Sched.SchedValidA.toFIA J1.schedValid, rqDomPrio_of_schedA J1.sr, trivial⟩ _ dm).2.2 + have hq0 : (L.mem sd (inqA t) &&& sched.Q_QUEUED) = 0#u64 := SRd.toSchedSRN.qBit0 ht hond hnqd + have Sd := tcbSchedAction_shapeA tcbSchedDequeue (fun _ _ _ h => Or.inr (List.mem_filter.1 h).1) (specPtr t) dm + have hyd : σd.kheap (specPtr t) = some (.tcb y1) := by rw [congrArg (fun z => z.kheap) Sd.1.1]; exact hy1 + have states_d : StEqA Z.T (L.mem sd) σd := + hSt.keep (fun u hu => by + rw [stA_eq] + exact yieldWordKeepA J1.sr.toSchedSRN J1.sr.curT (Fd.wr.mono fun _ h => Or.inl h) hu (Or.inl rfl)) + (fun u x hx => ⟨x, by rw [← congrArg (fun z => z.kheap) Sd.1.1]; exact hx, rfl⟩) + -- the priority write, enqueue, reschedule + obtain ⟨SR2, S2⟩ := prioStageA SRd states_d ht hyd hq0 hWidth + have hon2 := yieldAffKeepA SRd SR2 ht ht hond + have hrunp : ∀ x, (prioStateA σd (specPtr t) y1 (BitVec.ofNat 8 p.val)).kheap (specPtr t) = some (.tcb x) → + runnable x.tcbState = true := fun x hx => by + rw [prioKhA, if_pos rfl] at hx + obtain rfl := KernelObject.tcb.inj (Option.some.inj hx) + exact hrun1 + have hnrp : specPtr t ∉ (prioStateA σd (specPtr t) y1 (BitVec.ofNat 8 p.val)).releaseQueue := by + show specPtr t ∉ σd.releaseQueue + rw [rd]; exact hT.2 + obtain ⟨se, ee, σe, e, ke, re, hae, wE, SRe⟩ := exists_of_specA + (enqueue_refines_localA SR2 t ht hon2 (fun _ => (SR2.relOn ht hon2).2 hnrp) hrunp) + have FE := MFrA.of_sw (Z := { Z with Q := deqQA (st := st) s1 t Z.Q }) (m := m) ke + (wE.mono fun _ h => SR2.toSchedSRN.enq_swr ht h) + have states_e : StEqA Z.T (L.mem se) σe := + S2.keep (fun u hu => by + rw [stA_eq] + exact yieldWordKeepA SR2.toSchedSRN SR2.curT (FE.wr.mono fun _ h => Or.inl h) hu (Or.inl rfl)) + (fun u x hx => ⟨x, by rw [ke] at hx; exact hx, rfl⟩) + obtain ⟨sr, er, σr, Qr, r, kr, rr, wR, SRr⟩ := exists_of_specA (reschedule_required_refinesA SRe states_e) + have FR := MFrA.of_sw (Z := { Z with Q := enqQA st (L.wr sd (prioA t) p) t (deqQA (st := st) s1 t Z.Q) }) (m := m) kr wR + have F2 := FE.trans FR rfl rfl + have em : ((), σe) ∈ (tcbSchedAction (Z := Unit) tcbSchedEnqueue (specPtr t) + (prioStateA σd (specPtr t) y1 (BitVec.ofNat 8 p.val))).1 := by rw [e]; exact Set.mem_singleton _ + have rm : ((), σr) ∈ (rescheduleRequired (Z := Unit) σe).1 := by rw [r]; exact Set.mem_singleton _ + have Se := tcbSchedAction_enq_memA (specPtr t) em + have Rs := rescheduleRequired_memA rm + have eqd : σd = { σ1 with readyQueues := σd.readyQueues, schedulerAction := σd.schedulerAction } := Sd.1.1 + have hσe : σe = { prioStateA σd (specPtr t) y1 (BitVec.ofNat 8 p.val) with + readyQueues := σe.readyQueues, schedulerAction := σe.schedulerAction } := Se.1.1 + have hσr : σr = { σe with readyQueues := σr.readyQueues, schedulerAction := σr.schedulerAction } := Rs.1.1 + have eqr : σr = { prioStateA σd (specPtr t) y1 (BitVec.ofNat 8 p.val) with + readyQueues := σr.readyQueues, schedulerAction := σr.schedulerAction } := + hσr.trans (by rw [hσe]) + -- the spec invariants + obtain ⟨LI, sv, -, -⟩ := prioQ_specInvA L1 J1.schedValid (rqDomPrio_of_schedA J1.sr) hy1 hrun1 hT hnidle d rfl e r + -- the concrete equation and the decode + have hsp1 : sched.set_priority st s1 t p = ok sr := + setPriority_queuedA (hSt t ht y1 hy1) hrun1 hqcC ed ee er + have hdec : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (.Ok (), sr, a) := by + rw [hcap, decodeTcb_programA, if_neg Bool.false_ne_true, hlbl, tcbSetPriority_postA hstall he hp ha hle hr, + hsp1, bind_ok] + have htH : t ∈ H.tcbs := by rw [J1.tcbs]; exact ht + -- the concrete invariant + have hca : TcbAddr (L.curW s) := J.sr.core.tcbs _ J.sr.curT + have sA : Aligned (stA (L.curW s)) := by rw [stA_eq]; exact field_aligned hca (by simp) (by simp) + have far : ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → K.val ≠ 3328 → + L.mem s1 (wa (L.curW s) K) = L.mem s (wa (L.curW s) K) := fun K h1 h2 h3 => + mem_wr_ne' sA (field_aligned hca h1 h2) + (by rw [stA_eq]; exact field_ne (K2 := K) hca hca (by simp) h1 (fun _ => by simpa using Ne.symm h3)) + have hcN : t = L.mem s1 (L.nodeAt s1 (L.cpu s1) layout.node.CUR_THREAD) → t = L.curW s := fun e => by + rw [show L.mem s1 (L.nodeAt s1 (L.cpu s1) layout.node.CUR_THREAD) = L.curW s1 from rfl] at e + exact e.trans hcw + have blk : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac sr := by + have h := sched.set_priority.blk (st := st) s1 t p (BXI.blk J1.sys.ent.bxi) htH hWidth + (fun e => by + rw [hcN e] + rw [show L.mem s1 (inqA (L.curW s)) = L.mem s (inqA (L.curW s)) from far 3592#u64 (by simp) (by simp) (by simp)] + exact RS.cur.1) + (fun e => by + rw [hcN e] + rw [show L.mem s1 (scA (L.curW s)) = L.mem s (scA (L.curW s)) from far 3464#u64 (by simp) (by simp) (by simp)] + exact RS.cur.2.1) + (fun e => by + rw [hcN e] + rw [show L.mem s1 (scA (L.curW s)) = L.mem s (scA (L.curW s)) from far 3464#u64 (by simp) (by simp) (by simp)] + exact RS.cur.2.2) + rw [hsp1, WP.spec_ok] at h + exact h.1 + obtain ⟨-, -, sysl, symL, -, userOK, j7⟩ := J.decodeFinA hA hM hPl hfm hslot hcov + (rfl : capA st s slot = capA st s slot) hext hSC (lbl := lbl) + (by rw [hcap, hlbl]; simp [DecArmS1aEnt, DecArmQH, DecArmQZ2, DecArmKQ2, DecArmKQ, DecArmKQ3, DecArmDelQ, + DecArmS1aC, capFam]) + (fun _ h => by rw [hcap] at h; cases h) (fun _ h => by rw [hcap] at h; cases h) hdec σr m + have J2 := J1.prioQ_stepA ht hWidth hy1 hcq hnb SRd Fd eqd hq0 F2 SRr eqr LI sv (BlkOK.bxi blk) sysl symL userOK j7 + have hSet := setPriority_queuedSpecA (σp := prioStateA σd (specPtr t) y1 (BitVec.ofNat 8 p.val)) + (BitVec.ofNat 8 p.val) hy1 hrun1 hqc1 d (congrArg (fun z => z.kheap) eqd) rfl e r + have hB : Sched.BodyEndA σ σr m := by + have kh : σr.kheap = Function.update σ1.kheap (specPtr t) + (some (.tcb { y1 with tcbPriority := BitVec.ofNat 8 p.val })) := by + have h := congrArg (fun z => z.kheap) eqr + simp only [prioStateA, updKSc] at h + rw [h, kd] + exact Sched.BodyEndA.ofChoose J hkc hy1 kh Rs.2 + ((congrArg (fun z => z.curTime) eqr).trans (congrArg (fun z => z.curTime) eqd)) + ((congrArg (fun z => z.machineState.lastMachineTime) eqr).trans + (congrArg (fun z => z.machineState.lastMachineTime) eqd)) + ((congrArg (fun z => z.curThread) eqr).trans (congrArg (fun z => z.curThread) eqd)) + ((congrArg (fun z => z.curSc) eqr).trans (congrArg (fun z => z.curSc) eqd)) + ((congrArg (fun z => z.consumedTime) eqr).trans (congrArg (fun z => z.consumedTime) eqd)) + ((congrArg (fun z => z.releaseQueue) eqr).trans (congrArg (fun z => z.releaseQueue) eqd)) + exact ⟨xc, σ1, σr, sr, hkc, rfl, hsp, fun κs => by + rw [fl4InvokeTcb_setPriorityA _ hSet]; rfl, hdec, _, _, J2, hB⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3TcbPrioQInvA.lean b/proofs/Fl4ProofsA64/P3/Stage3TcbPrioQInvA.lean new file mode 100644 index 000000000..c4210988a --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3TcbPrioQInvA.lean @@ -0,0 +1,249 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 3e): **the spec invariants across SetPriority of a queued or current thread** +(dequeue, priority write, enqueue, `reschedule_required`). + +* `prioKhA`, **`LoopInvA.prioWrA`**: the three spec invariants across the priority write. +* **`LoopInvA.queueEn`**: `LoopInvA.queueE` for a step that ends with the action not `resumeCurThread`, so the current + thread may be enqueued (the `ctQ` clause is vacuous). +* **`prioQ_specInvA`**: `LoopInvA` (dequeue by `queueE` with no additions: the thread leaves every queue; priority write; + enqueue and `reschedule_required` as one `queueEn` step) and `SchedValidA` (`HT.deqD`, `congr`, `HT.enq`, + `resched_fia`), and the thread's absence from the queues after the dequeue. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3TcbPrioQA +import Fl4ProofsA64.P3.Stage3DelReplyRemoveA +import Fl4ProofsA64.P3.Stage3ScYieldRqA +import Fl4ProofsA64.P3.Stage3RqDomPrioA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 + +noncomputable section + +theorem prioKhA {σ : State Unit} {t : ObjRef} {y : Tcb} {pr : Priority} (q : ObjRef) : + (prioStateA σ t y pr).kheap q = if q = t then some (.tcb { y with tcbPriority := pr }) else σ.kheap q := by + unfold prioStateA + rw [updKSc_kheap, Function.update_apply] + +section +variable {σ : State Unit} {m : McState} {n : Nat} {t : ObjRef} {y : Tcb} {pr : Priority} + +theorem prioFwdA (hy : σ.kheap t = some (.tcb y)) {q : ObjRef} {z : Tcb} (h : σ.kheap q = some (.tcb z)) : + ∃ w, (prioStateA σ t y pr).kheap q = some (.tcb w) ∧ w = { z with tcbPriority := w.tcbPriority } := by + by_cases e : q = t + · rw [e, hy] at h + obtain rfl : y = z := KernelObject.tcb.inj (Option.some.inj h) + exact ⟨{ y with tcbPriority := pr }, by rw [prioKhA, if_pos e], rfl⟩ + · exact ⟨z, by rw [prioKhA, if_neg e]; exact h, rfl⟩ + +theorem prioBwdA (hy : σ.kheap t = some (.tcb y)) {q : ObjRef} {w : Tcb} (h : (prioStateA σ t y pr).kheap q = some (.tcb w)) : + ∃ z, σ.kheap q = some (.tcb z) ∧ w = { z with tcbPriority := w.tcbPriority } := by + by_cases e : q = t + · rw [prioKhA, if_pos e] at h + cases h + exact ⟨y, e ▸ hy, rfl⟩ + · rw [prioKhA, if_neg e] at h + exact ⟨w, h, rfl⟩ + +theorem prioKeepA (hy : σ.kheap t = some (.tcb y)) {q : ObjRef} {o : KernelObject} (h : σ.kheap q = some o) + (n1 : ∀ z, o ≠ .tcb z) : (prioStateA σ t y pr).kheap q = some o := by + rw [prioKhA, if_neg (fun e => by rw [e, hy] at h; exact n1 y (Option.some.inj h).symm)] + exact h + +theorem prioBackA {q : ObjRef} {o : KernelObject} (h : (prioStateA σ t y pr).kheap q = some o) + (n1 : ∀ z, o ≠ .tcb z) : σ.kheap q = some o := by + rw [prioKhA] at h + split at h + · exact absurd (Option.some.inj h).symm (n1 _) + · exact h + +theorem prioStateRq_congrA {σd : State Unit} + (e : σd = { σ with readyQueues := σd.readyQueues, schedulerAction := σd.schedulerAction }) : + prioStateA σd t y pr = { prioStateA σ t y pr with + readyQueues := σd.readyQueues, schedulerAction := σd.schedulerAction } := by + conv_lhs => rw [e] + rfl + +theorem LoopInvA.prioWrA (h : LoopInvA σ m n) (hy : σ.kheap t = some (.tcb y)) : + LoopInvA (prioStateA σ t y pr) m n := by + have keepO : ∀ q o, σ.kheap q = some o → (∀ z, o ≠ .tcb z) → (prioStateA σ t y pr).kheap q = some o := + fun q o h' n1 => prioKeepA hy h' n1 + have backO : ∀ q o, (prioStateA σ t y pr).kheap q = some o → (∀ z, o ≠ .tcb z) → σ.kheap q = some o := + fun q o h' n1 => prioBackA h' n1 + refine ⟨?_, ?_, ?_⟩ + · refine h.sinv.of_fr + { tcb := fun q x hq => by + obtain ⟨w, hw, e⟩ := prioFwdA hy hq + exact ⟨w, hw, by rw [e], by rw [e], by rw [e]⟩ + tcb' := fun q w hq => by + obtain ⟨z, hz, e⟩ := prioBwdA hy hq + exact ⟨z, hz, by rw [e], by rw [e], by rw [e]⟩ + sc := fun q z k => ⟨fun hq => backO q _ hq (fun _ e => by cases e), fun hq => keepO q _ hq (fun _ e => by cases e)⟩ + cur := rfl + idle := rfl + csc := rfl + rq := rfl + rls := rfl + act := rfl, time := rfl, cons := rfl } ?_ + exact h.sinv.irqN.updTcbA hy rfl + · exact + { f3 := fun c hc => by + obtain ⟨x, hx, hs⟩ := h.sidle.f3 c hc + obtain ⟨w, hw, e⟩ := prioFwdA hy hx + exact ⟨w, hw, by rw [e]; exact hs⟩ + f4 := fun z k hq => h.sidle.f4 z k (backO _ _ hq (fun _ e => by cases e)) + f5 := fun q z k hq => h.sidle.f5 q z k (backO _ _ hq (fun _ e => by cases e)) + f6 := h.sidle.f6.of_eq fun sl => by + unfold specCapAtA + by_cases e : sl.1 = t + · rw [prioKhA, if_pos e, e, hy] + rfl + · rw [prioKhA, if_neg e] + f7 := fun y k hq => h.sidle.f7 y k (backO _ _ hq (fun _ e => by cases e)) + idleSlot := fun c hc => by + have h0 := h.sidle.idleSlot c hc + unfold vtCapA at h0 ⊢ + by_cases e : m.idleThreadOf c = t + · rw [prioKhA, if_pos e]; rw [e, hy] at h0; exact h0 + · rw [prioKhA, if_neg e]; exact h0 + idleBound := fun c hc => by + have h0 := h.sidle.idleBound c hc + unfold vcpuOfA at h0 ⊢ + by_cases e : m.idleThreadOf c = t + · rw [prioKhA, if_pos e]; rw [e, hy] at h0; exact h0 + · rw [prioKhA, if_neg e]; exact h0 } + · refine ⟨fun u eu => h.sact.f1 u eu, ?_⟩ + obtain ⟨x, hx, hs⟩ := h.sact.f2 + obtain ⟨w, hw, e⟩ := prioFwdA hy hx + exact ⟨w, hw, by rw [e]; exact hs⟩ + +theorem LoopInvA.queueEn {P : ObjRef → Prop} {σ σ' : State Unit} {m : McState} {n : Nat} (h : LoopInvA σ m n) + (Q : QueueE P σ σ') + (hP : ∀ y, P y → (∀ c < n, m.idleThreadOf c ≠ y) ∧ ∃ x, σ.kheap y = some (.tcb x) ∧ runnable x.tcbState = true) + (hna : σ'.schedulerAction ≠ .resumeCurThread) + (hA : ∀ u, σ'.schedulerAction = .switchThread u → + σ.schedulerAction = .switchThread u ∨ (u ≠ σ.curThread ∧ u ≠ σ.idleThread)) : LoopInvA σ' m n := by + have e := Q.1 + have hk : σ'.kheap = σ.kheap := by rw [e] + have hcur : σ'.curThread = σ.curThread := by rw [e] + have hidle : σ'.idleThread = σ.idleThread := by rw [e] + have hcsc : σ'.curSc = σ.curSc := by rw [e] + have hrls : σ'.releaseQueue = σ.releaseQueue := by rw [e] + have hnode : σ'.interruptIrqNode = σ.interruptIrqNode := by rw [e] + have hct : ∀ k p, capTableAt k p σ' ↔ capTableAt k p σ := fun k p => by + unfold capTableAt; rw [hk] + have S := h.sinv + refine + { sinv := + { mc := S.mc + view := by unfold McView; rw [hidle]; exact S.view + curTcb := by rw [hk, hcur]; exact S.curTcb + idleTcb := by rw [hk, hidle]; exact S.idleTcb + onlyIdle := by rw [hk]; exact S.onlyIdle + idleSc := by rw [hk, hidle]; exact S.idleSc + curSc := by rw [hk, hcsc]; exact S.curSc + ctQ := fun hres => (hna hres).elim + idleQ := fun d p c hc hm => by + rcases Q.2 d p _ hm with a | a + · exact S.idleQ d p c hc a + · exact (hP _ a).1 c hc rfl + rq := fun d p t hm => by + rw [hk] + rcases Q.2 d p t hm with a | a + · exact S.rq d p t a + · exact (hP _ a).2 + rls := fun t ht => by rw [hrls] at ht; rw [hk]; exact S.rls t ht + rpos := by rw [hk]; exact S.rpos + irqN := by + obtain ⟨⟨⟨hinj, hcn⟩, hdom⟩, hsl, href⟩ := S.irqN + refine ⟨⟨⟨?_, fun irq hi => ?_⟩, fun irq hi k hk' => ?_⟩, fun irq hi o b c ho hc => ?_, + fun p o b c ho hc => ?_⟩ + · rw [hnode]; exact hinj + · rw [hnode]; exact (hct _ _).2 (hcn irq hi) + · rw [hnode] at hk'; exact hdom irq hi k ((hct _ _).1 hk') + · rw [hk, hnode] at ho; exact hsl irq hi o b c ho hc + · rw [hnode]; rw [hk] at ho; exact href p o b c ho hc + zc := by + have hcons : σ'.consumedTime = σ.consumedTime := by rw [e] + unfold ZeroConsA; rw [hk, hcsc, hrls, hcons]; exact S.zc + rlsRun := by unfold RlsRunA; rw [hrls, hk]; exact S.rlsRun + curScReady := by + have hcons : σ'.consumedTime = σ.consumedTime := by rw [e] + have htime : σ'.curTime = σ.curTime := by rw [e] + unfold CurRdyA; rw [hk, hcsc, hcons, htime]; exact S.curScReady + faultSt := by unfold FaultStOkA; rw [hk]; exact S.faultSt } + sidle := h.sidle.congr hk hidle (by rw [show σ'.curTime = σ.curTime by rw [e]]; exact Nat.le_refl _) + sact := + { f1 := fun u eu => by + rw [hcur, hidle] + rcases hA u eu with a | a + · exact h.sact.f1 u a + · exact a + f2 := by rw [hk, hcur]; exact h.sact.f2 } } + + +theorem prioQ_specInvA {σ σd σp σe σr : State Unit} (L1 : LoopInvA σ m n) (sv : SchedValidA σ) (hrd : RqDomPrio σ) + (hk : σ.kheap t = some (.tcb y)) (hrun : runnable y.tcbState = true) (hT : TgtA t σ) + (hnidle : ∀ c < n, m.idleThreadOf c ≠ t) + (d : tcbSchedAction (Z := Unit) tcbSchedDequeue t σ = ({((), σd)}, False)) + (hp : σp = prioStateA σd t y pr) + (e : tcbSchedAction (Z := Unit) tcbSchedEnqueue t σp = ({((), σe)}, False)) + (r : rescheduleRequired (Z := Unit) σe = ({((), σr)}, False)) : + LoopInvA σr m n ∧ SchedValidA σr ∧ NoQ t σd ∧ σd.kheap = σ.kheap := by + have dm : ((), σd) ∈ (tcbSchedAction (Z := Unit) tcbSchedDequeue t σ).1 := by rw [d]; exact Set.mem_singleton _ + have em : ((), σe) ∈ (tcbSchedAction (Z := Unit) tcbSchedEnqueue t σp).1 := by rw [e]; exact Set.mem_singleton _ + have rm : ((), σr) ∈ (rescheduleRequired (Z := Unit) σe).1 := by rw [r]; exact Set.mem_singleton _ + have Sd := tcbSchedAction_shapeA tcbSchedDequeue (fun _ _ _ h => Or.inr (List.mem_filter.1 h).1) t dm + have Se := tcbSchedAction_enq_memA t em + have Rs := rescheduleRequired_memA rm + have h2 := deqD t (fun _ => True) (fun _ _ _ => trivial) σ ⟨Sched.SchedValidA.toFIA sv, hrd, trivial⟩ _ dm + have hkd : σd.kheap = σ.kheap := congrArg (fun z => z.kheap) Sd.1.1 + have hkdt : σd.kheap t = some (.tcb y) := by rw [hkd]; exact hk + have QF : QueueE (fun _ => False) σ σd := + ⟨Sd.1.1, fun d' p' y' hy' => (Sd.1.2 d' p' y' hy').imp_right fun e' => absurd (e' ▸ hy') (h2.2.2 d' p')⟩ + have Ld : LoopInvA σd m n := L1.queueE QF (fun _ p => p.elim) (fun u h' => Or.inl (Sd.2.symm.trans h')) + (fun h' => Sd.2.symm.trans h') + subst hp + have Lp := Ld.prioWrA (pr := pr) hkdt + have hσe : σe = { prioStateA σd t y pr with readyQueues := σe.readyQueues, schedulerAction := σe.schedulerAction } := + Se.1.1 + have Q2 : QueueE (fun u => u = t ∨ PendA σe u) (prioStateA σd t y pr) σr := + (Se.1.mono fun _ h' => Or.inl h').trans (Rs.1.mono fun _ h' => Or.inr h') + have hsch : ∀ u, schedulable u σe = schedulable u (prioStateA σd t y pr) := fun u => by rw [hσe]; rfl + have L3 : LoopInvA σr m n := Lp.queueEn Q2 (fun u hu => by + rcases hu with rfl | ⟨ha, hs⟩ + · exact ⟨hnidle, { y with tcbPriority := pr }, by rw [prioKhA, if_pos rfl], hrun⟩ + · have := (Lp.pendOk (y := u) ⟨by rw [← Se.2]; exact ha, by rw [← hsch]; exact hs⟩).2 + exact this) + (by rw [Rs.2]; intro h'; cases h') (fun u hu => by rw [Rs.2] at hu; cases hu) + -- `SchedValidA` + have hrel : (prioStateA σd t y pr).releaseQueue = σ.releaseQueue := by + have := congrArg (fun z => z.releaseQueue) Sd.1.1 + exact this + have F1 : SchedValidA (prioStateA σd t y pr) := by + have hA := h2.1.1.toA h2.1.2 + refine hA.congr rfl rfl rfl rfl rfl rfl (fun q o k hq => by rw [prioKhA, if_neg (fun e' => by rw [e', hkdt] at hq; cases hq)]; exact hq) + (fun u x hx => ?_) (fun u x hx => ?_) + · obtain ⟨w, hw, e'⟩ := prioFwdA hkdt hx + exact ⟨w, hw, by rw [e']⟩ + · obtain ⟨z, hz, e'⟩ := prioBwdA hkdt hx + exact ⟨z, hz, by rw [e'], by rw [e']⟩ + have hTp : ActTcbA (prioStateA σd t y pr) t ∧ t ∉ (prioStateA σd t y pr).releaseQueue := by + obtain ⟨⟨x0, sc, e0, es, y0, k, ey, ea⟩, hn⟩ := hT + rw [hk] at e0 + cases e0 + refine ⟨⟨{ y with tcbPriority := pr }, sc, by rw [prioKhA, if_pos rfl], es, y0, k, ?_, ea⟩, by rw [hrel]; exact hn⟩ + rw [prioKhA, if_neg (fun e' => by rw [e', hk] at ey; cases ey)] + rw [hkd]; exact ey + have h3 := HT.enq t (fun _ => True) (fun _ _ _ => trivial) _ ⟨Sched.SchedValidA.toFIA F1, trivial, hTp⟩ _ em + have h4 := resched_fia (fun _ => True) (fun _ _ _ => trivial) (fun _ _ _ => trivial) σe + ⟨h3.1, trivial⟩ _ rm + exact ⟨L3, h4.1.1.toA h4.1.2, h2.2.2, hkd⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3TcbPrioQStepA.lean b/proofs/Fl4ProofsA64/P3/Stage3TcbPrioQStepA.lean new file mode 100644 index 000000000..23e6521f4 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3TcbPrioQStepA.lean @@ -0,0 +1,154 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 3e): **SetPriority of a queued or current thread at the record** +(`JStage3A.prioQ_stepA`): the ready-queue step of `dequeue` (`YieldQueueRelA`, heap kept), the `PRIO` word write of +`set_prio`, then `enqueue` and `reschedule_required` (heap kept, scheduler words), composed into one record step. + +* `ipcRelsQueueA`: the endpoint, notification and reply relations across a ready-queue-only step (the first half of + `consumedMidA`). +* `replyPrioA`: the reply relation across the `PRIO` word write. +* **`JStage3A.prioQ_stepA`**: the relations stage by stage (`ipcRelsQueueA`, `*.prioWrA`, `ipcRelsQueueA`), the words + of the composite footprint (`MWrA` and the `PRIO` word) for `JStage3A.prioG_stepA`. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3TcbPrioQInvA +import Fl4ProofsA64.P3.Stage3ScConsumedA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s : S} {a : A} {σ : State Unit} {m : McState} + +theorem ipcRelsQueueA {s sq : S} {σ σq : State Unit} + (SR : SchedSRN st Z m κ s σ) (hP : IPlacedA N Z.T E Rp Bf) (pl : IpcSchedPlacementA Z.th N E Rp Bf) + (ep : EpStateRel Z.T E Rp Bf (L.mem s) σ) (nt : NtfnStateRel N Z.T (L.mem s) σ) + (rp : ReplyStackRelA (L.mem s) Rp Z.C σ (scNameA m) stack) + (wq : WritesIn st s sq (MWrA (st := st) Z m σ s)) (kq : σq.kheap = σ.kheap) + (um : σq.machineState.underlyingMemory = σ.machineState.underlyingMemory) : + EpStateRel Z.T E Rp Bf (L.mem sq) σq ∧ NtfnStateRel N Z.T (L.mem sq) σq ∧ + ReplyStackRelA (L.mem sq) Rp Z.C σq (scNameA m) stack := by + refine ⟨ep.frame hP.e (fun b hb hf => wq.mem_eq hb fun h => endpFootOffWrite SR pl hf h) + ⟨fun _ _ h => by rw [kq]; exact h, fun _ x h => ⟨x, by rw [kq]; exact h, rfl⟩, + fun _ y h => ⟨y, by rw [kq]; exact h, rfl, rfl, rfl, rfl, rfl⟩, um⟩, + nt.frame hP.n (fun b hb hf => wq.mem_eq hb fun h => notifFootOffMWrA SR pl.notification hf h) + ⟨fun _ _ h => by rw [kq]; exact h, fun _ y h => ⟨y, by rw [kq]; exact h, rfl, rfl, rfl⟩⟩, ?_⟩ + refine rp.frame (fun r hr => ?_) (fun _ _ y k h => ⟨y, k, by rw [kq]; exact h, rfl⟩) + (fun r _ x' h => ⟨x', by rw [kq] at h; exact h, rfl⟩) + have hr3 := hP.e.rep r hr + have one : ∀ O : U64, O.val = 8 ∨ O.val = 16 → L.mem sq (wa r O) = L.mem s (wa r O) := by + intro O hO + have hv : (wa r O).val = r.val + O.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + refine wq.mem_eq (by unfold Aligned; rw [hv]; omega) fun h => ?_ + exact otherWordOffMWrA SR (pl.reply r hr) (by omega) (by omega) h + exact ⟨one _ (Or.inr (by simp [layout.reply.NEXT])), one _ (Or.inl (by simp [layout.reply.PREV]))⟩ + +theorem replyPrioA {s : S} {σ : State Unit} {t v : U64} {y : Tcb} (ht : t ∈ Z.T) (hP : IPlacedA N Z.T E Rp Bf) + (hy : σ.kheap (specPtr t) = some (.tcb y)) + (rp : ReplyStackRelA (L.mem s) Rp Z.C σ (scNameA m) stack) : + ReplyStackRelA (L.mem (L.wr s (prioA t) v)) Rp Z.C (prioStateA σ (specPtr t) y (BitVec.ofNat 8 v.val)) + (scNameA m) stack := by + have hta := hP.e.tcb t ht + have aP := (a_prio hta).1 + have vP := val_prio hta + have off : ∀ b : U64, Aligned b → b.val ≠ t.val + 3448 → L.mem (L.wr s (prioA t) v) b = L.mem s b := + fun b hb h => mem_wr_ne' aP hb (fun e => h (by rw [← e]; exact vP)) + refine rp.frame (fun r hr => ?_) (fun c _ z k h => ⟨z, k, prioKeepA hy h (fun _ e => by cases e), rfl⟩) + (fun r _ x' h => ⟨x', prioBackA h (fun _ e => by cases e), rfl⟩) + have hr3 := hP.e.rep r hr + have dis := hP.e.rt r hr t ht + have one : ∀ O : U64, O.val = 8 ∨ O.val = 16 → L.mem (L.wr s (prioA t) v) (wa r O) = L.mem s (wa r O) := by + intro O hO + have hv' : (wa r O).val = r.val + O.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact off _ (by unfold Aligned; rw [hv']; omega) (by omega) + exact ⟨one _ (Or.inr (by simp [layout.reply.NEXT])), one _ (Or.inl (by simp [layout.reply.PREV]))⟩ + +set_option maxHeartbeats 4000000 in +/-- **SetPriority of a queued or current thread at the record** (module docstring). -/ +theorem JStage3A.prioQ_stepA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {t v : U64} (ht : t ∈ Z.T) (hv : v.val < 256) {y : Tcb} (hy : σ.kheap (specPtr t) = some (.tcb y)) + (hcq : tsCode y.tcbState ≠ 3 ∧ tsCode y.tcbState ≠ 4) (hnb : ∀ p, y.tcbState ≠ .blockedOnNotification p) + {sd sr : S} {σd σr : State Unit} {Qd Qr : U64 → U64 → List U64} + (Hd : SchedSRA st { Z with Q := Qd } m κ sd σd) (Fd : MFrA (st := st) Z m s sd σ σd) + (eqd : σd = { σ with readyQueues := σd.readyQueues, schedulerAction := σd.schedulerAction }) + (hq0 : (L.mem sd (inqA t) &&& sched.Q_QUEUED) = 0#u64) + (F2 : MFrA (st := st) { Z with Q := Qd } m (L.wr sd (prioA t) v) sr + (prioStateA σd (specPtr t) y (BitVec.ofNat 8 v.val)) σr) + (Hr : SchedSRA st { Z with Q := Qr } m κ sr σr) + (eqr : σr = { prioStateA σd (specPtr t) y (BitVec.ofNat 8 v.val) with + readyQueues := σr.readyQueues, schedulerAction := σr.schedulerAction }) + (LI : LoopInvA σr m n) (sv : SchedValidA σr) + (bxi : BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac sr) + (sysl : SysL st H Z.Cs OS ia ie sr) (symL : SymL st H Z.Cs sr) + (userOK : Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U sr) + (j7 : J7A st (J7R2A st ar nA) Z H OS sr σr m) : + JStage3A st AR { Z with Q := Qr } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) sr a σr m := by + have kd : σd.kheap = σ.kheap := congrArg (fun z => z.kheap) eqd + have hyd : σd.kheap (specPtr t) = some (.tcb y) := by rw [kd]; exact hy + have hP := J.iplaced + have hta : TcbAddr t := J.sr.core.tcbs t ht + have aP : Aligned (prioA t) := (a_prio hta).1 + have vP := val_prio hta + have um1 : σd.machineState.underlyingMemory = σ.machineState.underlyingMemory := + congrArg (fun z => z.machineState.underlyingMemory) eqd + have kr : σr.kheap = (prioStateA σd (specPtr t) y (BitVec.ofNat 8 v.val)).kheap := + congrArg (fun z => z.kheap) eqr + have um2 : σr.machineState.underlyingMemory = + (prioStateA σd (specPtr t) y (BitVec.ofNat 8 v.val)).machineState.underlyingMemory := + congrArg (fun z => z.machineState.underlyingMemory) eqr + -- the relations, stage by stage + obtain ⟨ep1, nt1, rp1⟩ := ipcRelsQueueA J.sr.toSchedSRN hP J.ipcPlace J.ep J.ntfn J.reply Fd.wr kd um1 + have SR2 := Hd.prioWrA ht hyd hq0 hv + have ep2 := EpStateRel.prioWrA ep1 hP.e ht hyd hv + have nt2 := NtfnStateRel.prioWrA nt1 hP.n hP.e ht hyd hv + have rp2 := replyPrioA (v := v) ht hP hyd rp1 + obtain ⟨ep3, nt3, rp3⟩ := ipcRelsQueueA SR2.toSchedSRN hP J.ipcPlace ep2 nt2 rp2 F2.wr kr um2 + -- the composite footprint + have wp : WritesIn st sd (L.wr sd (prioA t) v) (fun b => b = prioA t) := writesIn_wr aP + have SSz1 : SSzA σ (prioStateA σd (specPtr t) y (BitVec.ofNat 8 v.val)) := + Fd.sz.trans (fun p z k h => ⟨z, prioBackA h (fun _ c => by cases c)⟩) + have wF : WritesIn st s sr (fun b => MWrA (st := st) Z m σ s b ∨ b = prioA t) := + (Fd.wr.trans (wp.trans F2.wr)).mono fun b hb => + hb.elim Or.inl fun h => h.elim Or.inr fun h2 => Or.inl (mwr_back rfl rfl SSz1 (wp.1.trans Fd.wr.1) h2) + have wN : WritesIn st s sr (fun b => SchedStepNWA (st := st) N E Rp Z m σ s b ∨ BufW Bf b) := + wF.mono fun b hb => hb.elim (fun h => Or.inl (Or.inl (Or.inl h))) fun h => + Or.inl (Or.inl (Or.inr (Endp.tcbStepWA_of_off hP.e.tcb ht 3448 (by norm_num) (by norm_num) (by norm_num) + (by norm_num) (by rw [h]; exact vP)))) + have tw : ∀ u ∈ Z.T, ∀ O : U64, (O.val < 224 ∨ O.val ∈ [3328, 3336, 3440, 3456, 3472, 3512, 3672]) → O.val % 8 = 0 → + L.mem sr (wa u O) = L.mem s (wa u O) := fun u hu O h1 h2 => by + have h1' : O.val < 224 ∨ O.val = 3328 ∨ O.val = 3336 ∨ O.val = 3440 ∨ O.val = 3456 ∨ O.val = 3472 ∨ O.val = 3512 ∨ + O.val = 3672 := by simpa using h1 + have hua : TcbAddr u := J.sr.core.tcbs u hu + refine wF.mem_eq (field_aligned hua (by omega) h2) fun h => h.elim + (tcbWordNotMWrA J.sr.toSchedSRN hu (by omega) (by omega) (by omega) (by omega) (by omega)) fun e => ?_ + have e' : (wa u O).val = t.val + 3448 := by rw [e]; exact vP + rw [Notif.tfield_val hua (by omega)] at e' + by_cases e2 : u = t + · subst e2; omega + · have := tcb_disj hP.e ht hu e2 + omega + have key : ∀ q, σr.kheap q = if q = specPtr t then + some (.tcb { y with tcbPriority := BitVec.ofNat 8 v.val }) else σ.kheap q := fun q => by + rw [kr, prioKhA, kd] + have eσ : σr = { prioStateA σ (specPtr t) y (BitVec.ofNat 8 v.val) with + readyQueues := σr.readyQueues, schedulerAction := σr.schedulerAction } := + eqr.trans (by rw [prioStateRq_congrA eqd]) + exact J.prioG_stepA (Q := Qr) ht hy hcq hnb key eσ wN tw bxi sysl symL userOK j7 Hr ep3 nt3 rp3 LI.sinv + LI.sidle sv + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3TcbReadFrA.lean b/proofs/Fl4ProofsA64/P3/Stage3TcbReadFrA.lean new file mode 100644 index 000000000..abbaa3c45 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3TcbReadFrA.lean @@ -0,0 +1,170 @@ +/- +P3 (AArch64, merge line, D8 step 3, TCB F0): **the footprints of `ReadRegisters` without source suspension** (from the +pre-Restart state) and of `ReadFPU`. + +* `swr_stepNA`: a scheduler write word (`SWrA`) is a `SchedStepNWA` word. +* **`tcbReadRegisters_sendFrA`** (Send): the whole step from the pre-Restart joint state is a `PartFrA` + (`SchedStepNWA` writes and the spec frame `SchedFrameA`). +* **`tcbReadRegisters_callFootA`** (Call): the whole step writes `SchedStepNWA ∨ BufW Bf` words (the reply's IPC + buffer words are the `BufW` part). The spec side of this label changes user memory (the reply registers), so it + is not a `SchedFrameA`; its `SpecSchedA` / user-memory facts (`JStage3A.of_schedWrU`'s `HS`, `hum`) are not + exported here. + +* **`tcbFpuFrame_footA`** (ReadFPU): `SchedStepNWA` or the copied user-frame words. + +The suspension variants (`flags & 1 ≠ 0`) need a footprint twin of `suspend_refinesA`. No named premise beyond those +of `tcbReadRegisters_callJointA`. New module. +-/ +import Fl4ProofsA64.P3.TcbReadJointA +import Fl4ProofsA64.P3.TcbInvokeReadPostA +import Fl4ProofsA64.P3.TcbInvokeFpuA +import Fl4ProofsA64.P3.Stage3TcbRestartFrA +import Fl4ProofsA64.P3.Stage3SignalFootA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4SpecA64 Endp Sched + +noncomputable section + +section +variable {S A : Type} {st : machine.Store S} [L : LawfulStore st] {ar : machine.Arch A} + +/-- A scheduler write word is a Stage-3 step word. -/ +theorem swr_stepNA {N E R : Set U64} {Z : XSetsA} {m : McState} {σ : State Unit} {s : S} {b : U64} + (h : SWrA (st := st) Z s b) : SchedStepNWA (st := st) N E R Z m σ s b := + Or.inl (Or.inl (Or.inr h)) + +private theorem curThread_of_stsA {σ σ1 : State Unit} {p : ObjRef} {x : Tcb} {ts : ThreadState} + (hx : σ.kheap p = some (.tcb x)) (h : setThreadState (Z := Unit) p ts σ = ({((), σ1)}, False)) : + σ1.curThread = σ.curThread := by + rw [setThreadState_stsActExA hx ts] at h + rw [← (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst h))).2] + unfold stsActA + split <;> rfl + +private theorem finishSendA {σ σ' : State Unit} {words : List Data} + (h : setThreadState (Z := Unit) σ.curThread .running σ = ({((), σ')}, False)) : + fl4FinishTcbReply false words σ = ({((), σ')}, False) := by + unfold fl4FinishTcbReply + rw [nd_bind_oneSc (show (Fl4Spec.NonDet.gets fun s : State Unit => s.curThread) σ = ({(σ.curThread, σ)}, False) + from rfl)] + rw [nd_bind_oneSc (show (Fl4Spec.NonDet.when false (replyFromKernel (Z := Unit) σ.curThread (0, words))) σ = + ({((), σ)}, False) from rfl)] + exact h + +/-- **`ReadRegisters` Send without source suspension is a `PartFrA` step** (module docstring). -/ +theorem tcbReadRegisters_sendFrA {N E R : Set U64} {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s sRestart : S} {a : A} {σ : State Unit} + {t flags count : U64} {ctx : tcb.Ctx} {src x : Tcb} {block donate : Bool} + (hst : smp.remote_tcb_stall st ar s a t = ok (s, a)) + (hlen : ¬ctx.length < 2#u64) + (hf : tcb.arg st s ctx 0#u64 = ok flags) (hn : tcb.arg st s ctx 1#u64 = ok count) + (hlo : ¬count < 1#u64) (hhi : ¬36#u64 < count) (hself : t ≠ L.curW s) + (hr : tcb.restart_caller st s = ok sRestart) + (hcall : ctx.call = false) (hsuspend : (flags &&& 1#u64) = 0#u64) + (H : SchedSRA st Z m κ s σ) (hstate : StEqA Z.T (L.mem s) σ) + (hsrc : σ.kheap (specPtr t) = some (.tcb src)) + (hx : σ.kheap (specPtr (L.curW s)) = some (.tcb x)) (hrun : runnable x.tcbState = true) + (hcur : sched.cur_thread st s = ok (L.curW s)) : + tcb.decode st ar s a fl4_abi.label.TCBReadRegisters t ctx ⦃ r => + r.1 = .Ok () ∧ r.2.2 = a ∧ ∃ σRestart σFinal, + setThreadState (Z := Unit) (specPtr (L.curW s)) .restart σ = ({((), σRestart)}, False) ∧ + (.ok [], σFinal) ∈ ((fl4PerformInvocation block false donate + (.invokeTCB (.readRegisters (specPtr t) false count.bv ArchDefaultExtraRegisters))).run σRestart).1 ∧ + SchedSRA st Z m κ r.2.1 σFinal ∧ StEqA Z.T (L.mem r.2.1) σFinal ∧ + PartFrA (st := st) N E R Z m s r.2.1 σ σFinal ⦄ := by + have hct : L.curW s ∈ Z.T := H.curT + have hP : TcbPlaced Z.T := H.core.tcbs + have hR := tcbRestartCaller_partA (N := N) (E := E) (Rp := R) H hstate hct hP hx hrun hcur + rw [hr, WP.spec_ok] at hR + obtain ⟨σR, hspec, HR, stR, hkh, F⟩ := hR + have hcw : L.curW sRestart = L.curW s := + specPtr_inj (HR.sch.curThread.symm.trans ((curThread_of_stsA hx hspec).trans H.sch.curThread)) + have hpt : specPtr t ≠ specPtr (L.curW s) := fun e => hself (specPtr_inj e) + have hsrcR : σR.kheap (specPtr t) = some (.tcb src) := by + rw [hkh, updKSc_kheap, Function.update_of_ne hpt]; exact hsrc + have hcR : σR.kheap (specPtr (L.curW sRestart)) = some (.tcb { x with tcbState := .restart }) := by + rw [hcw, hkh, updKSc_kheap, Function.update_self] + rw [tcbReadRegisters_acceptedA hst hlen hf hn hlo hhi hself hr, hcall, hsuspend] + simp only [bne_self_eq_false, tcbReadRegisters_sendA, Bool.false_eq_true, ite_false, bind_ok, + uncurry_apply_pair, Std.bind_assoc] + have hv : sched.TS_RUNNING.val = tsCodeA .running := by rw [ts_vals.1]; rfl + refine WP.spec_bind (sc_set_runnable_stateA HR stR HR.curT hcR rfl (ts := .running) rfl hv) ?_ + rintro sFinal ⟨σFinal, hrunSpec, HFinal, hStateFinal, _, hFrame⟩ + have G : PartFrA (st := st) N E R Z m sRestart sFinal σR σFinal := by + refine ⟨hFrame.mono fun b hb => ?_, frame_of_runA + (inferInstance : SPresInst (setThreadState (Z := Unit) (specPtr (L.curW sRestart)) .running)).pres hrunSpec⟩ + rcases hb with hb | hb + · exact swr_stepNA hb + · rw [hb]; exact stA_stepNA hP HR.curT + refine (WP.spec_ok _).2 ⟨rfl, rfl, σR, σFinal, hspec, ?_, HFinal, hStateFinal, F.trans G⟩ + have hFinish := finishSendA (words := ((frame_registers ++ gp_registers).take count.bv.toNat).map + src.tcbArch.tcbContext.userRegs) (HR.sch.curThread.symm ▸ hrunSpec) + unfold fl4PerformInvocation + rw [run_bind_okD (readRegsSpec_noSuspendA hsrcR count.bv), run_bind_okD (run_liftE_stD hFinish)] + rfl + +/-- **`ReadRegisters` Call without source suspension writes `SchedStepNWA ∨ BufW Bf`** (module docstring). -/ +theorem tcbReadRegisters_callFootA {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} + {Bf : Set (U64 × Word)} {s sRestart : S} {a : A} {σ : State Unit} + {t flags count : U64} {ctx : tcb.Ctx} {src x : Tcb} {block donate : Bool} + (hst0 : smp.remote_tcb_stall st ar s a t = ok (s, a)) + (hlen : ¬ctx.length < 2#u64) + (hf : tcb.arg st s ctx 0#u64 = ok flags) (hn : tcb.arg st s ctx 1#u64 = ok count) + (hlo : ¬count < 1#u64) (hhi : ¬36#u64 < count) (hself : t ≠ L.curW s) + (hr : tcb.restart_caller st s = ok sRestart) + (hcall : ctx.call = true) (hsuspend : (flags &&& 1#u64) = 0#u64) + (H : SchedSRA st Z m κ s σ) (hst : StEqA Z.T (L.mem s) σ) + (P' : IpcSchedPlacementA Z.th N E R Bf) (P : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + (hoff : ∀ y, IsSlotA Z.th y → ¬ EWrite Z.T E R Bf y ∧ ¬ EWrite Z.T E R Bf (core.num.U64.wrapping_add y 8#u64)) + (hSt : StateRelA Z.th (L.mem s) σ) (hV : SlotsValidA Z.th (L.mem s)) + (hC : BufCover st Z.T Bf s σ) (hNative : BufNativeA Z.T (L.mem s)) + (ht : t ∈ Z.T) (hsrc : σ.kheap (specPtr t) = some (.tcb src)) + (hx : σ.kheap (specPtr (L.curW s)) = some (.tcb x)) (hrun : runnable x.tcbState = true) + (hu : ∀ rp, ipcBufOfA true x = some rp → ∀ k, k < 128 → inUserFrame (bufAddr rp k) σ) + (hcur : sched.cur_thread st s = ok (L.curW s)) : + tcb.decode st ar s a fl4_abi.label.TCBReadRegisters t ctx ⦃ r => + r.1 = .Ok () ∧ r.2.2 = a ∧ ∃ σRestart σFinal, + setThreadState (Z := Unit) (specPtr (L.curW s)) .restart σ = ({((), σRestart)}, False) ∧ + (.ok [], σFinal) ∈ ((fl4PerformInvocation block true donate + (.invokeTCB (.readRegisters (specPtr t) false count.bv ArchDefaultExtraRegisters))).run σRestart).1 ∧ + IpcSchedRelA st Z m κ r.2.1 σFinal ∧ StEqA Z.T (L.mem r.2.1) σFinal ∧ + WritesIn st s r.2.1 (fun b => SchedStepNWA (st := st) N E R Z m σ s b ∨ BufW Bf b) ⦄ := by + have hct : L.curW s ∈ Z.T := H.curT + have hP : TcbPlaced Z.T := H.core.tcbs + have hR := tcbRestartCaller_partA (N := N) (E := E) (Rp := R) H hst hct hP hx hrun hcur + rw [hr, WP.spec_ok] at hR + obtain ⟨σR, hspec, HR, -, -, F⟩ := hR + have hcw : L.curW sRestart = L.curW s := + specPtr_inj (HR.sch.curThread.symm.trans ((curThread_of_stsA hx hspec).trans H.sch.curThread)) + refine WP.spec_mono (tcbReadRegisters_callJointA (block := block) (donate := donate) hst0 hlen hf hn hlo hhi + hself hr hcall hsuspend H hst P' P HE hoff hSt hV hC hNative ht hsrc hx hrun hu) ?_ + rintro r ⟨h1, h2, σR', σF, hspec', hm, hS, hE, hW⟩ + have e : σR' = σR := by + rw [hspec] at hspec' + exact ((Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst hspec'))).2).symm + subst e + refine ⟨h1, h2, σR', σF, hspec, hm, hS, hE, ?_⟩ + refine (F.wr.trans hW).mono fun b hb => ?_ + rcases hb with hb | hb | hb | hb + · exact Or.inl hb + · exact Or.inl (swr_stepNA (swr_ksb rfl F.wr.1 hb)) + · exact ewrite_stepNA_buf hP hb + · rw [hb, hcw]; exact Or.inl (stA_stepNA hP hct) + +/-- **`ReadFPU` (`toFrame = true`): the footprint of the 65-word copy into the user frame** after the caller's +`Restart` (`tcbFpuTransfer_exactA`'s post `tcbFpuCopyA st s1 (t.FPU) dst`): `SchedStepNWA` words or the copied +frame words `CWW dst 65`. The frame words are user memory (frame class F0u outside `BufW`); the spec's user-memory +change is not exported. -/ +theorem tcbFpuFrame_footA {N E Rp : Set U64} {Z : XSetsA} {m : McState} {s s1 : S} {σ σ1 : State Unit} + (F : PartFrA (st := st) N E Rp Z m s s1 σ σ1) (src dst : U64) (ha : Aligned dst) + (hfit : dst.val + fl4FpuStateSize ≤ 2 ^ 64) : + WritesIn st s (tcbFpuCopyA st s1 src dst) (fun b => SchedStepNWA (st := st) N E Rp Z m σ s b ∨ + CWW dst 65#u64 b) := + F.wr.trans (tcbFpuCopy_exactA s1 src dst ha hfit).2 + +end +end +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3TcbRegsFrA.lean b/proofs/Fl4ProofsA64/P3/Stage3TcbRegsFrA.lean new file mode 100644 index 000000000..41397bf93 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3TcbRegsFrA.lean @@ -0,0 +1,90 @@ +/- +P3 (AArch64, merge line, D8 step 3, TCB F0 rest): **the write footprints of the register invocations' optional +restart / suspension** (siblings of `tcbRestartIf_contA` / `tcbSuspendIf_contA`, `P3/TcbRegsContA`, same +premises): the spec outcome, the write set `SchedStepNWA N E R Z m σ s ∨ BufW Bf` and `SchedFrameA σ σ'`. + +Generated by `handoff/tools/ptu/gen_regs.py`. No named premise beyond the `*_contA` theorems'. New module. +-/ +import Fl4ProofsA64.P3.TcbRegsContA +import Fl4ProofsA64.P3.Stage3TcbRestartFr2A +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4SpecA64 Sched + +noncomputable section + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + +/-- The optional restart's write footprint and spec frame. -/ +theorem tcbRestartIf_footA (hP : IPlacedA N Z.T E R Bf) (P : IpcSchedPlacementA Z.th N E R Bf) + (hc : m.curCore < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hmbF : ∀ a, Fl4MinBudgetAgreeA ar a) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {s : S} {a : A} {σ : State Unit} {stack : U64 → List U64} + (V : TcbIpcViewsA st Z m κ N E R Bf stack s σ) (hC0 : ∀ c ∈ Z.C, c ≠ 0#u64) + {t : U64} (ht : t ∈ Z.T) (resume : Bool) + (hidle : ∀ c < fl4A64Platform.numNodes, specPtr t ≠ m.idleThreadOf c) + (ready : resume = true → ∃ x, TcbRestartReadyA st Z m κ N E R s σ t x) : + (if resume then sched.restart st ar s a t else ok (s, a)) ⦃ r => ∃ σ', + (Fl4Spec.NonDet.when resume (fl4TcbRestart (Z := Unit) (specPtr t))) σ = ({((), σ')}, False) ∧ + WritesIn st s r.1 (fun b => SchedStepNWA (st := st) N E R Z m σ s b ∨ BufW Bf b) ∧ + SchedFrameA σ σ' ⦄ := by + cases resume with + | false => + rw [if_neg (by simp), WP.spec_ok] + refine ⟨σ, ?_, WritesIn.refl _ _, PreO.refl σ⟩ + unfold Fl4Spec.NonDet.when + rw [if_neg (by simp)] + rfl + | true => + obtain ⟨x, X⟩ := ready rfl + rw [if_pos rfl] + refine WP.spec_mono (restart_footA hP P hc hmb hmbF hmax htu s a σ V.rel V.ntfn V.ep V.stk V.mcp V.yld + hC0 ht X.tcb X.reply X.ep X.ntfn X.rep X.on hidle X.tail) ?_ + rintro r ⟨σ', hspec, w, G⟩ + refine ⟨σ', ?_, w, G⟩ + unfold Fl4Spec.NonDet.when + rw [if_pos rfl] + exact hspec + +/-- The optional suspension's write footprint and spec frame. -/ +theorem tcbSuspendIf_footA (hP : IPlacedA N Z.T E R Bf) (P : IpcSchedPlacementA Z.th N E R Bf) + (hc : m.curCore < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {s : S} {a : A} {σ : State Unit} {stack : U64 → List U64} + (V : TcbIpcViewsA st Z m κ N E R Bf stack s σ) (hC0 : ∀ c ∈ Z.C, c ≠ 0#u64) + {t : U64} (ht : t ∈ Z.T) (susp : Bool) + (ready : susp = true → ∃ x, TcbSuspendReadyA st Z m N E R s σ t x) : + (if susp then sched.suspend st ar s a t else ok (s, a)) ⦃ r => ∃ σ', + (Fl4Spec.NonDet.when susp (fl4CancelRun (Z := Unit) (specPtr t) >>= fun _ => + Fl4SpecA64.suspend (specPtr t))) σ = ({((), σ')}, False) ∧ + WritesIn st s r.1 (fun b => SchedStepNWA (st := st) N E R Z m σ s b ∨ BufW Bf b) ∧ + SchedFrameA σ σ' ⦄ := by + cases susp with + | false => + rw [if_neg (by simp), WP.spec_ok] + refine ⟨σ, ?_, WritesIn.refl _ _, PreO.refl σ⟩ + unfold Fl4Spec.NonDet.when + rw [if_neg (by simp)] + rfl + | true => + obtain ⟨x, X⟩ := ready rfl + rw [if_pos rfl] + refine WP.spec_mono (suspend_footA hP P hc hmb hmax htu s a σ V.rel V.ntfn V.ep V.stk V.mcp V.yld + hC0 ht X.tcb X.reply X.ep X.ntfn X.rep X.runW X.runS X.on) ?_ + rintro r ⟨σ', hspec, w, G⟩ + refine ⟨σ', ?_, w, G⟩ + unfold Fl4Spec.NonDet.when + rw [if_pos rfl] + exact hspec + +end +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/Stage3TcbReschedA.lean b/proofs/Fl4ProofsA64/P3/Stage3TcbReschedA.lean new file mode 100644 index 000000000..d9699556e --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3TcbReschedA.lean @@ -0,0 +1,149 @@ +/- +P3 (aarch64, A64-P3 setipcbuf2): **`reschedule_required` of the current thread with no switch pending, at the record** +(`JStage3A.reschedSelf_stepA`): concretely one word, `SCHED_ACTION := CHOOSE_NEW`; in the spec +`schedulerAction := chooseNewThread`. The record step is `JStage3A.prioG_stepA` at the thread's own priority. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3TcbTlsSelfA +import Fl4ProofsA64.P3.Stage3TcbPrioA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s : S} {a : A} {σ : State Unit} {m : McState} + +set_option maxHeartbeats 4000000 in +/-- **`reschedule_required` with no switch pending, at the record** (module docstring). -/ +theorem JStage3A.reschedSelf_stepA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (sact : SActA σ) (nsw : ∀ u, σ.schedulerAction ≠ .switchThread u) : + ∃ s' σ', sched.reschedule_required st s = ok s' ∧ L.curW s' = L.curW s ∧ + rescheduleRequired (Z := Unit) σ = ({((), σ')}, False) ∧ + σ' = { σ with schedulerAction := .chooseNewThread } ∧ + (SysL st H Z.Cs OS ia ie s' → SymL st H Z.Cs s' → Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s' → + J7A st (J7R2A st ar nA) Z H OS s' σ' m → + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s' a σ' m) := by + set s' := L.wr s (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) 1#u64 with hs' + set σ' : State Unit := { σ with schedulerAction := .chooseNewThread } with hσ' + have hP := J.iplaced + have ht : L.curW s ∈ Z.T := J.sr.curT + set t := L.curW s with htdef + have hcur : σ.curThread = specPtr t := J.sr.sch.curThread + have heq : sched.reschedule_required st s = ok s' := by + have h01 : L.actionW s = 0#u64 ∨ L.actionW s = 1#u64 := by + by_cases e0 : L.actionW s = 0#u64 + · exact Or.inl e0 + by_cases e1 : L.actionW s = 1#u64 + · exact Or.inr e1 + exact absurd (by rw [J.sr.sch.act]; simp [actOfA, e0, e1]) (nsw (specPtr (L.actionW s))) + rcases h01 with e0 | e1 + · exact eq_of_spec_fd (sched.reschedule_required.resume (st := st) s e0) + · refine eq_of_spec_fd ?_ + unfold sched.reschedule_required + refine WP.spec_bind (sched.action.spec s) ?_ + rintro i rfl + rw [if_pos (bne_iff_ne.2 (by rw [e1, resume_valSc]; simp)), if_neg (by rw [e1, choose_valSc]; simp), bind_ok] + refine WP.spec_mono (sched.set_action.spec s _) ?_ + rintro s1 rfl + simp [hs', sched.ACTION_CHOOSE_NEW] + have blk' : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s' := by + have R := sched.reschedule_required.blk (st := st) s (BXI.blk J.sys.ent.bxi) + rw [heq, WP.spec_ok] at R + exact R.1 + have W : WritesIn st s s' (SWrA (st := st) Z s) := J.sr.toSchedSRN.act_wr 1#u64 + have SR : SchedSRA st Z m κ s' σ' := J.sr.set_action 1#u64 (by simp [actOfA]) (fun u h => by cases h) + have hno : NodePlaced st Z.T s (L.cpu s) := J.sr.core.nodes _ J.sr.sch.cpuCs + have c0 : layout.node.SCHED_ACTION.val < 4288 := by simp [layout.node.SCHED_ACTION] + have hcurW : L.curW s' = L.curW s := by + have e := SR.sch.curThread.symm.trans (show σ'.curThread = specPtr t from hcur) + exact specPtr_inj e + have hres : rescheduleRequired (Z := Unit) σ = ({((), σ')}, False) := rescheduleRequired_noswitchA nsw + refine ⟨s', σ', heq, hcurW, hres, rfl, fun sysl symL userOK j7 => ?_⟩ + have hT : TcbPlaced Z.T := hP.e.tcb + have tw : ∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → L.mem s' (wa u K) = L.mem s (wa u K) := + fun u hu K h1 h2 => by + have hua := hT u hu + exact mem_wr_ne' (nodeAt_aligned hno c0 (by simp [layout.node.SCHED_ACTION])) (field_aligned hua h1 h2) + (node_ne_field hno c0 hu hua h1) + obtain ⟨y, hy, -⟩ := J.mcp t ht + obtain ⟨x, hx, hst⟩ := sact.f2 + rw [hcur, hy] at hx + cases hx + have hcq : tsCode y.tcbState ≠ 3 ∧ tsCode y.tcbState ≠ 4 := by + rcases hst with h | h | h | h <;> rw [h] <;> simp [Notif.tsCode] + have hnb : ∀ p, y.tcbState ≠ .blockedOnNotification p := fun p h => by + rcases hst with e | e | e | e <;> rw [e] at h <;> cases h + -- the relations across the action word + have F : SchedFrameA σ σ' := SchedFrameA.of_eq (σ := σ) (σ' := σ') rfl rfl rfl rfl rfl rfl rfl rfl + have feE : Endp.FootEq Z.T E Rp Bf (L.mem s) (L.mem s') := by + intro b hb hf + exact W.mem_eq hb (fun h => endpFootOffWrite J.sr.toSchedSRN J.ipcPlace hf (Or.inr h)) + have kvE : Endp.KView σ σ' := + ⟨fun q z h => h, fun q z h => ⟨z, h, rfl⟩, fun q z h => ⟨z, h, rfl, rfl, rfl, rfl, rfl⟩, rfl⟩ + have ep : EpStateRel Z.T E Rp Bf (L.mem s') σ' := J.ep.frame hP.e feE kvE + have feN : Notif.FootEq N Z.T (L.mem s) (L.mem s') := by + intro b hb hf + exact W.mem_eq hb (fun h => notifFootOffMWrA J.sr.toSchedSRN J.ipcPlace.notification hf (Or.inr h)) + have kvN : Notif.KView σ σ' := ⟨fun q z h => h, fun q z h => ⟨z, h, rfl, rfl, rfl⟩⟩ + have ntfn : NtfnStateRel N Z.T (L.mem s') σ' := J.ntfn.frame hP.n feN kvN + have reply : ReplyStackRelA (L.mem s') Rp Z.C σ' (scNameA m) stack := by + refine J.reply.frame (fun r hr => ?_) (fun c _ z k h => ⟨z, k, h, rfl⟩) (fun r _ x' h => ⟨x', h, rfl⟩) + have hr3 := hP.e.rep r hr + have one : ∀ O : U64, O.val = 8 ∨ O.val = 16 → L.mem s' (wa r O) = L.mem s (wa r O) := by + intro O hO + have hv : (wa r O).val = r.val + O.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have hal : Aligned (wa r O) := by unfold Aligned; rw [hv]; omega + refine W.mem_eq hal fun hb => ?_ + exact otherWordOffMWrA J.sr.toSchedSRN (J.ipcPlace.reply r hr) (by omega) (by omega) (Or.inr hb) + exact ⟨one _ (Or.inr (by simp [layout.reply.NEXT])), one _ (Or.inl (by simp [layout.reply.PREV]))⟩ + have L1 : LoopInvA σ' m n := (show LoopInvA σ m n from ⟨J.sinv, J.sidle, sact⟩).reschedule + (r := ((), σ')) (by rw [hres]; exact Set.mem_singleton _) + have sv : SchedValidA σ' := + ⟨fun d p u hm => J.schedValid.rq d p u hm, fun e1 _ => by exfalso; revert e1; simp [hσ'], + fun u x sc e1 e2 e3 => J.schedValid.blk u x sc e1 e2 e3, fun u hu => J.schedValid.rls u hu⟩ + set v : U64 := ⟨y.tcbPriority.setWidth 64⟩ with hv + have hp : (BitVec.ofNat 8 v.val : Priority) = y.tcbPriority := by + apply BitVec.eq_of_toNat_eq + have h8 : y.tcbPriority.toNat < 256 := y.tcbPriority.isLt + show (BitVec.ofNat 8 (y.tcbPriority.setWidth 64).toNat).toNat = y.tcbPriority.toNat + rw [BitVec.toNat_ofNat, BitVec.toNat_setWidth] + omega + have ey : ({ y with tcbPriority := BitVec.ofNat 8 v.val } : Tcb) = y := by rw [hp] + have key : ∀ q, σ'.kheap q = + if q = specPtr t then some (.tcb { y with tcbPriority := BitVec.ofNat 8 v.val }) else σ.kheap q := by + intro q + rw [ey] + split + · rename_i e; rw [e]; exact hy + · rfl + have eσ : σ' = { prioStateA σ (specPtr t) y (BitVec.ofNat 8 v.val) with + readyQueues := σ'.readyQueues, schedulerAction := σ'.schedulerAction } := by + have e : prioStateA σ (specPtr t) y (BitVec.ofNat 8 v.val) = σ := by + unfold prioStateA updKSc + rw [ey, ← hy, Function.update_eq_self] + rw [e] + have h := JStage3A.prioG_stepA (J := J) (t := t) (v := v) ht hy hcq hnb (s' := s') (σ' := σ') (Q := Z.Q) key eσ + (W.mono fun _ hb => Or.inl (Or.inl (Or.inl (Or.inr hb)))) + (fun u hu O h1 h2 => tw u hu O (by + rcases h1 with h1 | h1 + · omega + · simp at h1; omega) h2) + (BlkOK.bxi blk') sysl symL userOK j7 SR ep ntfn reply L1.sinv L1.sidle sv + exact h + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3TcbRestartFr2A.lean b/proofs/Fl4ProofsA64/P3/Stage3TcbRestartFr2A.lean new file mode 100644 index 000000000..b9f7fd771 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3TcbRestartFr2A.lean @@ -0,0 +1,274 @@ +/- +P3 (AArch64, merge line, D8 step 3, TCB F0 rest): **the write footprint and spec frame of `sched.restart`** +(`restart_refinesA`, `P3/TcbRestartA`, exports neither). + +* `SPresInst` instance for `fl4TcbRestart`. +* **`restart_footA`**: `restart_refinesA`'s chain (same premises), concluding the spec outcome, the write set + `SchedStepNWA N E R Z m σ s ∨ BufW Bf` from the entry state and `SchedFrameA σ σ'`. Use with `restart_refinesA` + through `WP.spec_and` (the spec outcome is a singleton). + +Generated by `handoff/tools/ptu/gen_rst.py` from `restart_refinesA`'s proof. No named premise beyond +`restart_refinesA`'s. New module. +-/ +import Fl4ProofsA64.P3.TcbRestartA +import Fl4ProofsA64.P3.Stage3TcbSuspFrA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4SpecA64 Fl4Spec Sched + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +instance (t : ObjRef) : SPresInst (fl4TcbRestart (Z := Unit) t) := ⟨by unfold fl4TcbRestart; spres⟩ + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + +set_option maxHeartbeats 4000000 in +/-- `restart_footA` with the two spec steps that reach the post-`Restart` state handed to `tailS`. -/ +theorem restart_footAtA (hP : IPlacedA N Z.T E R Bf) (P : IpcSchedPlacementA Z.th N E R Bf) + (hc : m.curCore < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hmbF : ∀ a, Fl4MinBudgetAgreeA ar a) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (s : S) (a : A) (σ : State Unit) {stack : U64 → List U64} + (H : IpcSchedRelA st Z m κ s σ) (RN : NtfnStateRel N Z.T (L.mem s) σ) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + (M : McpRelA Z.T m (L.mem s) σ) (Y : YieldRelA Z.T Z.C m (L.mem s) σ) (hC0 : ∀ c ∈ Z.C, c ≠ 0#u64) + {t : U64} (ht : t ∈ Z.T) {x : Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hReply : ∀ r', x.tcbState = .blockedOnReply r' → ∃ r ∈ R, specPtr r = r' ∧ + ∃ xr, σ.kheap r' = some (.reply xr) ∧ xr.replyTcb = some (specPtr t) ∧ ScReplyCovA Z.C m σ r') + (hE : ∀ e', epBlocked x.tcbState = some e' → L.mem s (tBlockA t) ∈ E ∧ + ∃ z, σ.kheap e' = some (.endpoint z) ∧ specPtr t ∈ z.epQueue) + (hN : ∀ n', x.tcbState = .blockedOnNotification n' → L.mem s (tBlockA t) ∈ N ∧ + ∃ z, σ.kheap n' = some (.notification z) ∧ specPtr t ∈ z.ntfnObj.ntfnQueue) + (hrep : RepOKA σ (specPtr t)) + (hon : L.mem s (affA t) = L.cpu s) + (hidle : ∀ c < fl4A64Platform.numNodes, specPtr t ≠ m.idleThreadOf c) + (tailS : ∀ (s2 : S) (σ2 : State Unit) (Q : U64 → U64 → List U64) (RR : U64 → List U64) (σc : State Unit), + SchedSRB st { Z with Q := Q, R := RR } m κ s2 σ2 (specPtr t) → + (∃ y, σ2.kheap (specPtr t) = some (.tcb y) ∧ y.tcbState = .restart) → + cancelIpc (Z := Unit) (specPtr t) σ = ({((), σc)}, False) → + setThreadState (Z := Unit) (specPtr t) .restart σc = ({((), σ2)}, False) → + (L.mem s2 (scA t) ≠ 0#u64 → L.mem s2 (scCoreA (L.mem s2 (scA t))) = L.cpu s2)) : + sched.restart st ar s a t ⦃ out => ∃ σ', + fl4TcbRestart (Z := Unit) (specPtr t) σ = ({((), σ')}, False) ∧ + WritesIn st s out.1 (fun b => SchedStepNWA (st := st) N E R Z m σ s b ∨ BufW Bf b) ∧ + SchedFrameA σ σ' ⦄ := by + have ops := ipcSchedOpsA (st := st) (ar := ar) (κ := κ) hP P hc hmb hmax htu + have hSched : CancelScheduleA st N Z.T (IpcSchedRelA st Z m κ) := cancelScheduleA_of_placement P + have PE := hP.e + have T0 : StEqA Z.T (L.mem s) σ := fun u hu y hy => (HE.at_tcb hu hy).st + obtain ⟨b, hb, hbiff⟩ := isStopped_eqA (st := st) (s := s) T0 ht hk + rw [restart_progA st ar s a t, hb, bind_ok] + have hspec0 : fl4TcbRestart (Z := Unit) (specPtr t) σ = + (Fl4Spec.NonDet.when (¬runnable x.tcbState ∧ ¬idle x.tcbState) (do + cancelIpc (specPtr t) + setThreadState (specPtr t) .restart + let sc ← getTcbObjRef Tcb.tcbSchedContext (specPtr t) + ifSporadicCurScTestRefillUnblockCheck sc + NonDet.maybeM fl4SchedContextResume sc + testPossibleSwitchTo (specPtr t)) : SMonad Unit Unit) σ := by + unfold fl4TcbRestart + rw [nd_bind_oneSc (Notif.getThreadState_eq hk)] + cases b with + | false => + rw [if_neg (by simp), WP.spec_ok] + have hns : ¬(¬runnable x.tcbState = true ∧ ¬idle x.tcbState) := fun h => by + have := hbiff.2 h + cases this + refine ⟨σ, ?_, WritesIn.refl _ _, PreO.refl σ⟩ + rw [hspec0] + unfold Fl4Spec.NonDet.when + rw [if_neg hns] + rfl + | true => + have hstop := hbiff.1 rfl + rw [if_pos rfl] + obtain ⟨Q0, R0, H0⟩ := id H + -- the thread is in no queue before the cancellation + have hnqs0 : ∀ d p, specPtr t ∉ σ.readyQueues d p := by + intro d p hm + obtain ⟨x', hk', hr⟩ := H0.run.1 d p _ hm + rw [hk] at hk' + cases hk' + exact hstop.1 hr + have hnrel0 : specPtr t ∉ σ.releaseQueue := by + intro hm + obtain ⟨x', hk', hr⟩ := H0.run.2 _ hm + rw [hk] at hk' + cases hk' + exact hstop.1 hr + have hq0 : (L.mem s (inqA t) &&& sched.Q_QUEUED) = 0#u64 := H0.toSchedSRN.qBit0 ht hon hnqs0 + have hr0 : (L.mem s (inqA t) &&& sched.Q_RELEASE) = 0#u64 := (H0.toSchedSRN.relOn ht hon).2 hnrel0 + -- cancel_ipc + refine WP.spec_bind (suspendCancelA ops.1 hSched ops.2.2.1 hP P s a σ H RN HE HS ht hk hReply hE hN hrep hC0) ?_ + rintro r ⟨ha, σ1, stk1, hc1, RN1, H1, HE1, HS1, w1, K1, x1, hk1, hfree⟩ + have hoff : ∀ u ∈ Z.T, ∀ K : U64, (K = 3440#u64 ∨ K = 3472#u64 ∨ K = 3520#u64) → + L.mem r.1 (wa u K) = L.mem s (wa u K) := by + intro u hu K hK + refine w1.mem_eq (field_aligned (H0.core.tcbs u hu) ?_ ?_) (suspendOffTcbA H0.toSchedSRN hP hu hK) + · rcases hK with rfl | rfl | rfl <;> simp + · rcases hK with rfl | rfl | rfl <;> simp + have hoffF : ∀ u ∈ Z.T, L.mem r.1 (flagsA u) = L.mem s (flagsA u) := fun u hu => + w1.mem_eq (field_aligned (H0.core.tcbs u hu) (K := 3456#u64) (by decide) (by decide)) + (suspendOffFlagsA H0.toSchedSRN hP hu) + have hoffQ : L.mem r.1 (inqA t) = L.mem s (inqA t) := + w1.mem_eq (field_aligned (K := 3592#u64) (H0.core.tcbs t ht) (by simp) (by simp)) + (restartOffInqA H0.toSchedSRN hP ht rfl) + have htcb1 : ∀ u ∈ Z.T, ∀ y, σ.kheap (specPtr u) = some (.tcb y) → + ∃ y', σ1.kheap (specPtr u) = some (.tcb y') ∧ y'.tcbMcpriority = y.tcbMcpriority ∧ + y'.tcbYieldTo = y.tcbYieldTo ∧ y'.tcbFlags = y.tcbFlags := by + intro u _ y hy + obtain ⟨y1, hy1, e1⟩ := K1.tcb _ _ hy + exact ⟨y1, hy1, by rw [e1], by rw [e1], by rw [e1]⟩ + have hsc1 : ∀ c ∈ Z.C, ∃ y n y', σ.kheap (scNameA m c) = some (.schedContext y n) ∧ + σ1.kheap (scNameA m c) = some (.schedContext y' n) ∧ y'.scYieldFrom = y.scYieldFrom := by + intro c hc' + obtain ⟨y, n, hy, -⟩ := H0.scr.sc c hc' + obtain ⟨y', hy', e⟩ := K1.sc _ _ _ hy + exact ⟨y, n, y', hy, hy', by rw [e]⟩ + obtain ⟨M1, Y1⟩ := yieldFrameA (mem' := L.mem r.1) (σ' := σ1) M Y + (fun u hu => ⟨hoff u hu 3440#u64 (Or.inl rfl), hoffF u hu⟩) + (fun u hu => hoff u hu 3472#u64 (Or.inr (Or.inl rfl))) + htcb1 hsc1 + -- set Restart + obtain ⟨Q1, R1, H1'⟩ := H1 + have T1 : StEqA Z.T (L.mem r.1) σ1 := fun u hu y hy => (HE1.at_tcb hu hy).st + have hv : sched.TS_RESTART.val = tsCodeA .restart := by unfold sched.TS_RESTART; rfl + refine WP.spec_bind (WP.spec_and + (set_thread_state_exAnyA (Z := { Z with Q := Q1, R := R1 }) H1' T1 ht hk1 (ts := .restart) rfl hv) + (restartStateIpcA (Z := Z) hP P (Q := Q1) (RR := R1) (H1'.toB (specPtr t)) T1 RN1 HE1 HS1 ht hk1 hfree)) ?_ + rintro s2 ⟨⟨σ2, hsts, hkh2, H2, T2, w2⟩, RN2, E2, S2⟩ + have hσ2 : σ2 = stsActA (updKSc σ1 (specPtr t) (.tcb { x1 with tcbState := .restart })) (specPtr t) := + congrArg Prod.snd (Set.singleton_eq_singleton_iff.1 + (congrArg Prod.fst (hsts.symm.trans (setThreadState_stsActExA hk1 _)))) + rw [← hσ2] at RN2 E2 S2 + have h2t : σ2.kheap (specPtr t) = some (.tcb { x1 with tcbState := .restart }) := by + rw [hkh2, updKSc_same] + have h2far : ∀ q, q ≠ specPtr t → σ2.kheap q = σ1.kheap q := fun q hq => by + rw [hkh2, updKSc_kheap, Function.update_of_ne hq] + have off2 : ∀ u ∈ Z.T, ∀ K : U64, (K = 3440#u64 ∨ K = 3472#u64 ∨ K = 3520#u64 ∨ K = 3592#u64 ∨ K = 3456#u64) → + L.mem s2 (wa u K) = L.mem r.1 (wa u K) := by + intro u hu K hK + have hua := PE.tcb u hu + have hKv : K.val = 3440 ∨ K.val = 3472 ∨ K.val = 3520 ∨ K.val = 3592 ∨ K.val = 3456 := by + rcases hK with rfl | rfl | rfl | rfl | rfl <;> simp + have hv' : (wa u K).val = u.val + K.val := Notif.tfield_val hua (by omega) + refine w2.mem_eq (field_aligned (H1'.core.tcbs u hu) (by omega) + (by rcases hK with rfl | rfl | rfl | rfl | rfl <;> simp)) (fun h => ?_) + rcases h with h | h + · have h' := congrArg UScalar.val h + rw [hv', val_st (hP.n.tcb t ht)] at h' + by_cases e : u = t + · subst u; omega + · have := tcb_disj PE ht hu e; omega + · have hty := (H1'.thT u).2 hu + have hK' : (wa u K).val < (L.ksb r.1).val ∨ (L.ksb r.1).val + ksSizeA ≤ (wa u K).val := by + have g := H1'.place.ks u _ hty (fun irq => by + have := H1'.place.irqK hty irq + cases this) + change u.val + 4096 ≤ (L.ksb r.1).val ∨ _ at g + omega + have hn := H1'.node_in H1'.sch.cpuCs (o := layout.node.SCHED_ACTION) (by simp [layout.node.SCHED_ACTION]) + rw [h] at hK' + omega + obtain ⟨M2, Y2⟩ := yieldFrameA (mem' := L.mem s2) (σ' := σ2) M1 Y1 + (fun u hu => ⟨off2 u hu 3440#u64 (Or.inl rfl), off2 u hu 3456#u64 (Or.inr (Or.inr (Or.inr (Or.inr rfl))))⟩) + (fun u hu => off2 u hu 3472#u64 (Or.inr (Or.inl rfl))) + (fun u _ y hy => by + by_cases e : specPtr u = specPtr t + · rw [e, hk1] at hy + cases hy + exact ⟨{ x1 with tcbState := .restart }, by rw [e]; exact h2t, rfl, rfl, rfl⟩ + · exact ⟨y, by rw [h2far _ e]; exact hy, rfl, rfl, rfl⟩) + (fun c hc' => by + obtain ⟨y, n, hy, -⟩ := H1'.scr.sc c hc' + exact ⟨y, n, y, hy, by rw [h2far _ (fun h => by rw [h, hk1] at hy; cases hy)]; exact hy, rfl⟩) + -- the SC tail + have hon2 : L.mem s2 (affA t) = L.cpu s2 := by + have e1 : L.mem s2 (affA t) = L.mem s (affA t) := + (off2 t ht 3520#u64 (Or.inr (Or.inr (Or.inl rfl)))).trans (hoff t ht 3520#u64 (Or.inr (Or.inr rfl))) + rw [e1, w2.2.1, w1.2.1] + exact hon + have hq2 : L.mem s2 (inqA t) = L.mem s (inqA t) := + (off2 t ht 3592#u64 (Or.inr (Or.inr (Or.inr (Or.inl rfl))))).trans hoffQ + have hnqs2 : ∀ d p, specPtr t ∉ σ2.readyQueues d p := + H2.toSchedSRN.notReady_bitA ht (by rw [hq2]; exact hq0) + have hnrel2 : specPtr t ∉ σ2.releaseQueue := + (H2.toSchedSRN.relOn ht hon2).1 (by rw [hq2]; exact hr0) + have hcpu := tailS s2 σ2 Q1 R1 σ1 H2 ⟨_, h2t, rfl⟩ hc1 hsts + rw [ha] + refine WP.spec_mono (restartTailAnyA (ar := ar) (a1 := a) H2 T2 ht h2t rfl hon2 (hmbF a) hidle hnqs2 hnrel2 + hcpu) ?_ + rintro out ⟨ho, σ3, Q', R', htail, H3, T3, F⟩ + obtain ⟨RN3, E3, S3, M3, Y3, -⟩ := mcsStepFramesA (Z := { Z with Q := Q1, R := R1 }) hP P + H2.toSchedSRN F.fr F.mem F.sc RN2 E2 S2 M2 Y2 + have hS3 : fl4TcbRestart (Z := Unit) (specPtr t) σ = ({((), σ3)}, False) := by + rw [hspec0] + unfold Fl4Spec.NonDet.when + rw [if_pos hstop, nd_bind_oneSc hc1, nd_bind_oneSc hsts] + change (threadGet Tcb.tcbSchedContext (specPtr t) >>= _) σ2 = _ + rw [nd_bind_oneSc (threadGet_eqSc _ h2t)] + exact htail + have hPT : TcbPlaced Z.T := H0.core.tcbs + have z1 : SSzA σ σ1 := + (frame_of_runA (inferInstance : SPresInst (cancelIpc (Z := Unit) (specPtr t))).pres hc1).2.1 + have z2 : SSzA σ σ2 := fun p y n h => by + by_cases e : p = specPtr t + · rw [e, h2t] at h; cases h + · rw [h2far p e] at h; exact z1 p y n h + have k1 : L.ksb r.1 = L.ksb s := w1.1 + have k2 : L.ksb s2 = L.ksb s := w2.1.trans k1 + have g1 : WritesIn st s r.1 (fun b => SchedStepNWA (st := st) N E R Z m σ s b ∨ BufW Bf b) := + w1.mono fun b hb => cancelReplyW_stepNA H0.toSchedSRN hP hb + have g2 : WritesIn st r.1 s2 (fun b => SchedStepNWA (st := st) N E R Z m σ s b ∨ BufW Bf b) := + w2.mono fun b hb => hb.elim (fun h => by subst h; exact Or.inl (stA_stepNA hPT ht)) + (fun h => by rw [h, nodeAt_ksb k1, w1.2.1]; exact Or.inl (by have hh := SchedSRN.act_stepNA (N := N) (E := E) (Rp := R) H0.toSchedSRN; exact hh)) + have g3 : WritesIn st s2 out.1 (fun b => SchedStepNWA (st := st) N E R Z m σ s b ∨ BufW Bf b) := + F.fr.wr.mono fun b hb => Or.inl (Or.inl (Or.inl (mwr_back rfl rfl z2 k2 hb))) + exact ⟨σ3, hS3, (g1.trans1 g2).trans1 g3, + frame_of_runA (inferInstance : SPresInst (fl4TcbRestart (Z := Unit) (specPtr t))).pres hS3⟩ + + +set_option maxHeartbeats 4000000 in +/-- **`sched.restart`'s write footprint and spec frame** (module docstring). -/ +theorem restart_footA (hP : IPlacedA N Z.T E R Bf) (P : IpcSchedPlacementA Z.th N E R Bf) + (hc : m.curCore < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hmbF : ∀ a, Fl4MinBudgetAgreeA ar a) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (s : S) (a : A) (σ : State Unit) {stack : U64 → List U64} + (H : IpcSchedRelA st Z m κ s σ) (RN : NtfnStateRel N Z.T (L.mem s) σ) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + (M : McpRelA Z.T m (L.mem s) σ) (Y : YieldRelA Z.T Z.C m (L.mem s) σ) (hC0 : ∀ c ∈ Z.C, c ≠ 0#u64) + {t : U64} (ht : t ∈ Z.T) {x : Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hReply : ∀ r', x.tcbState = .blockedOnReply r' → ∃ r ∈ R, specPtr r = r' ∧ + ∃ xr, σ.kheap r' = some (.reply xr) ∧ xr.replyTcb = some (specPtr t) ∧ ScReplyCovA Z.C m σ r') + (hE : ∀ e', epBlocked x.tcbState = some e' → L.mem s (tBlockA t) ∈ E ∧ + ∃ z, σ.kheap e' = some (.endpoint z) ∧ specPtr t ∈ z.epQueue) + (hN : ∀ n', x.tcbState = .blockedOnNotification n' → L.mem s (tBlockA t) ∈ N ∧ + ∃ z, σ.kheap n' = some (.notification z) ∧ specPtr t ∈ z.ntfnObj.ntfnQueue) + (hrep : RepOKA σ (specPtr t)) + (hon : L.mem s (affA t) = L.cpu s) + (hidle : ∀ c < fl4A64Platform.numNodes, specPtr t ≠ m.idleThreadOf c) + (tailS : ∀ (s2 : S) (σ2 : State Unit) (Q : U64 → U64 → List U64) (RR : U64 → List U64), + SchedSRB st { Z with Q := Q, R := RR } m κ s2 σ2 (specPtr t) → + (∃ y, σ2.kheap (specPtr t) = some (.tcb y) ∧ y.tcbState = .restart) → + (∀ sc y n, L.mem s2 (scA t) = sc → sc ≠ 0#u64 → + σ2.kheap (scNameA m sc) = some (.schedContext y n) → scActive y = true) ∧ + (L.mem s2 (scA t) ≠ 0#u64 → L.mem s2 (scCoreA (L.mem s2 (scA t))) = L.cpu s2)) : + sched.restart st ar s a t ⦃ out => ∃ σ', + fl4TcbRestart (Z := Unit) (specPtr t) σ = ({((), σ')}, False) ∧ + WritesIn st s out.1 (fun b => SchedStepNWA (st := st) N E R Z m σ s b ∨ BufW Bf b) ∧ + SchedFrameA σ σ' ⦄ := + restart_footAtA (st := st) (ar := ar) (κ := κ) hP P hc hmb hmbF hmax htu s a σ H RN HE HS M Y hC0 ht hk hReply hE hN hrep hon hidle (fun s2 σ2 Q RR _ h h' _ _ => (tailS s2 σ2 Q RR h h').2) + +end +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/Stage3TcbRestartFrA.lean b/proofs/Fl4ProofsA64/P3/Stage3TcbRestartFrA.lean new file mode 100644 index 000000000..f220108c4 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3TcbRestartFrA.lean @@ -0,0 +1,79 @@ +/- +P3 (aarch64, A64-P3 Stage 3, D8 step 3): **the F0 frame of the TCB invocations' shared prefix and of the single-word +TCB labels**: the caller `Restart` (`tcb.restart_caller`) as a `PartFrA` step, a one-TCB spec replacement as a +`SchedFrameA` step, a one-word TCB write as `SchedStepNWA`, and the SetMCPriority label assembled from them. +No premise. +-/ +import Fl4ProofsA64.P3.TcbInvokeNtfnPostA +import Fl4ProofsA64.P3.TcbInvokeMcpA +import Fl4ProofsA64.P3.Stage3McsFrA +import Fl4ProofsA64.P3.Stage3TcbNtfnFrA +import Fl4ProofsA64.P3.ScSetStateA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4SpecA64 Sched +noncomputable section + +/-- One TCB replaced keeping its slot map is a scheduler frame. -/ +theorem tcbUpd_frameA {σ : State Unit} {t : ObjRef} {y y' : Tcb} (hy : σ.kheap t = some (.tcb y)) + (hy' : TcbSlotKeepA y y') : SchedFrameA σ (updKSc σ t (.tcb y')) := by + have key : ∀ p, (updKSc σ t (.tcb y')).kheap p = if p = t then some (.tcb y') else σ.kheap p := by + intro p; simp only [updKSc_kheap, Function.update_apply] + refine ⟨⟨fun p => ?_, rfl, rfl, rfl, rfl, rfl, rfl⟩, fun p z k hp => ?_, rfl⟩ + · rw [key p] + by_cases e1 : p = t + · subst e1; exact Or.inr (Or.inl ⟨y, y', hy, by simp, hy'⟩) + · exact Or.inl (by rw [if_neg e1]) + · rw [key p] at hp + by_cases e1 : p = t + · rw [if_pos e1] at hp; cases hp + · rw [if_neg e1] at hp; exact ⟨z, hp⟩ + +section conc +variable {S A : Type} {st : machine.Store S} [L : LawfulStore st] +local notation "wa" => core.num.U64.wrapping_add + +/-- **A one-word TCB write step**: the word `K` of a placed TCB is written, the spec replaces that TCB keeping its slot +map. `PartFrA` from the previous step's `PartFrA`. -/ +theorem tcbWord_partA {N E Rp : Set U64} {Z : XSetsA} {m : McState} {s sR : S} {σ σR : State Unit} + (F : PartFrA (st := st) N E Rp Z m s sR σ σR) + (hP : TcbPlaced Z.T) {t : U64} (ht : t ∈ Z.T) (K : U64) (hK : K.val < 4096) (h1 : 224 ≤ K.val) + (h3 : K.val ≠ 3664) (h4 : K.val ≠ 3672) (hal : Aligned (wa t K)) (v : U64) + {y y' : Tcb} (hy : σR.kheap (specPtr t) = some (.tcb y)) (hy' : TcbSlotKeepA y y') : + PartFrA (st := st) N E Rp Z m s (L.wr sR (wa t K) v) σ (updKSc σR (specPtr t) (.tcb y')) := + F.trans ⟨(writesIn_wr hal).mono fun b hb => hb ▸ tcbWord_stepNA hP ht K hK h1 h3 h4, + tcbUpd_frameA hy hy'⟩ + +/-- **The caller `Restart` prefix as `PartFrA`** (`tcb.restart_caller` = current thread's `set_thread_state Restart`). -/ +theorem tcbRestartCaller_partA {N E Rp : Set U64} {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} + {σ : State Unit} (H : SchedSRA st Z m κ s σ) (hst : StEqA Z.T (L.mem s) σ) {ct : U64} (hct : ct ∈ Z.T) + (hP : TcbPlaced Z.T) {xc : Tcb} (hkc : σ.kheap (specPtr ct) = some (.tcb xc)) + (hr : runnable xc.tcbState = true) (hcur : sched.cur_thread st s = ok ct) : + tcb.restart_caller st s ⦃ sR => ∃ σR, setThreadState (Z := Unit) (specPtr ct) .restart σ = ({((), σR)}, False) ∧ + SchedSRA st Z m κ sR σR ∧ StEqA Z.T (L.mem sR) σR ∧ + σR.kheap = (updKSc σ (specPtr ct) (.tcb { xc with tcbState := .restart })).kheap ∧ + PartFrA (st := st) N E Rp Z m s sR σ σR ⦄ := by + unfold tcb.restart_caller + rw [hcur, bind_ok] + have hv : sched.TS_RESTART.val = tsCodeA .restart := by rw [ts_vals.2.1]; rfl + refine WP.spec_mono (sc_set_runnable_stateA H hst hct hkc hr (ts := .restart) rfl hv) ?_ + rintro sR ⟨σR, hR, H', hst', hk', w⟩ + refine ⟨σR, hR, H', hst', hk', ?_⟩ + refine mcs_partA (X := fun b => b = stA ct) ?_ ?_ (frame_of_runA + (inferInstance : SPresInst (setThreadState (Z := Unit) (specPtr ct) .restart)).pres hR) + · rintro b rfl; exact stA_stepNA hP hct + · exact w.mono fun b hb => hb.elim (fun h => Or.inl (Or.inr h)) Or.inr + +/-- **SetMCPriority (label 7)** after the caller `Restart`: the written word is `mcpA t` and the spec replaces the +target's MCP. The concrete post state is `tcbSetMCPriority_postA`'s `L.wr s1 (mcpA t) p`. -/ +theorem tcbSetMCPriority_partA {N E Rp : Set U64} {Z : XSetsA} {m : McState} {s sR : S} {σ σR : State Unit} + (F : PartFrA (st := st) N E Rp Z m s sR σ σR) (hP : TcbPlaced Z.T) {t : U64} (ht : t ∈ Z.T) (p : U64) + (mp : Priority) {y : Tcb} (hy : σR.kheap (specPtr t) = some (.tcb y)) : + PartFrA (st := st) N E Rp Z m s (L.wr sR (mcpA t) p) σ (updKSc σR (specPtr t) (.tcb { y with tcbMcpriority := mp })) := + tcbWord_partA F hP ht 3440#u64 (by decide) (by decide) (by decide) (by decide) + (field_aligned (hP t ht) (K := 3440#u64) (by decide) (by decide)) p hy ⟨rfl, rfl, rfl⟩ + +end conc +end +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3TcbResumeNoopA.lean b/proofs/Fl4ProofsA64/P3/Stage3TcbResumeNoopA.lean new file mode 100644 index 000000000..f310e71df --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3TcbResumeNoopA.lean @@ -0,0 +1,113 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 3): **TCB Resume of a runnable thread at the record**. + +`sched.restart` and the spec's `restart` do nothing on a thread that is not stopped, so the record after the caller's +`Restart` is the record of the whole invocation. + +* **`JSyscallA.resumeRunnableA`**: `decode_invocation` on a Thread cap with the Resume label at a syscall entry, the + target running or restarting. + +Named premises: [P] `ArchPlat`, `hCs`; [S] `h1` (`numNodes = 1`, the remote stall). Resume of a stopped thread +(inactive or blocked) is `P3/Stage3TcbResumeRqA`. +New module. +-/ +import Fl4ProofsA64.P3.Stage3TcbMcpA +import Fl4ProofsA64.P3.TcbInvokeDispatchA +import Fl4ProofsA64.P3.Stage3TcbBodyA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +/-- The spec's Resume of a runnable thread changes nothing. -/ +theorem resumeRunnable_specA {σ : State Unit} {t : ObjRef} {y : Tcb} (hk : σ.kheap t = some (.tcb y)) + (hr : runnable y.tcbState = true) : + (.ok [], σ) ∈ ((fl4InvokeTcb (.resume t)).run σ).1 := by + have h1 : getThreadState (Z := Unit) t σ = ({(y.tcbState, σ)}, False) := threadGet_eqSc _ hk + have h : fl4TcbRestart (Z := Unit) t σ = ({((), σ)}, False) := by + unfold fl4TcbRestart + rw [nd_bind_oneD h1, Fl4Spec.NonDet.when, if_neg (by simp [hr])] + rfl + simp only [fl4InvokeTcb] + apply mem_liftED + refine mem_bind_ndD (show ((), σ) ∈ (fl4TcbRestart (Z := Unit) t σ).1 by rw [h]; rfl) ?_ + rfl + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {s : S} {a : A} {σ : State Unit} {m : McState} + +/-- A running or restarting thread is not stopped. -/ +theorem isStopped_runnableA {s : S} {t : U64} (h : L.mem s (stA t) = sched.TS_RUNNING ∨ + L.mem s (stA t) = sched.TS_RESTART) : sched.is_stopped st s t = ok false := by + have e : obj.Tcb.state st t s = ok (L.mem s (stA t)) := eq_of_spec_fd (obj.Tcb.state.spec s t) + unfold sched.is_stopped sched.is_blocked + rw [e, bind_ok, bind_ok] + rcases h with h | h <;> rw [h] <;> + simp [sched.TS_INACTIVE, sched.TS_RUNNING, sched.TS_RESTART, sched.TS_BLOCKED_ON_RECV, + sched.TS_BLOCKED_ON_SEND, sched.TS_BLOCKED_ON_NTFN, sched.TS_BLOCKED_ON_REPLY] + +set_option maxHeartbeats 2000000 in +/-- **TCB Resume of a runnable thread at a syscall entry** (module docstring). -/ +theorem JSyscallA.resumeRunnableA + (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + {slot lbl len buf t : U64} {blk call don : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Thread t) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) + (hlbl : lbl = fl4_abi.label.TCBResume) + {y : Tcb} (hy : σ.kheap (specPtr t) = some (.tcb y)) (hrun : y.tcbState = .running ∨ y.tcbState = .restart) : + ∃ xc σ1 s2, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + (.ok [], σ1) ∈ ((fl4InvokeTcb (.resume (specPtr t))).run σ1).1 ∧ + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (.Ok (), s2, a) ∧ + ∃ Q Rl, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s2 a σ1 m ∧ Sched.BodyEndA σ σ1 m := by + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hslot hcov J.actW J.running J.cur + have hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM := J.toJStage3EntryA.curNotVmA + obtain ⟨xc, hkc, hst, hsp, J1, -, -, -⟩ := J.toJStage3A.of_restartCurA RS.pl.choose_spec.choose_spec.choose_spec.choose_spec + RS.sym RS.run hnvm RS.cur RS.vs RS.vc + have ht : t ∈ Z.T := J.tcbs ▸ (J.toJStage3A.slotCapTy hslot).1.2.2.2.2.2 t hcap + have T0 := J.toJStage3A.toTA J.sact J.noSwitch + have hstall : smp.remote_tcb_stall st ar s a t = ok (s, a) := T0.stall_singleA hCs h1 ht + set s1 := L.wr s (stA (L.curW s)) sched.TS_RESTART with hs1 + set σ1 := stUpdA σ σ.curThread xc .restart with hσ1 + have hr : tcb.restart_caller st s = ok s1 := by + unfold tcb.restart_caller + rw [cur_thread_ok, bind_ok, hst] + have h1t : ∃ y1, σ1.kheap (specPtr t) = some (.tcb y1) ∧ (y1.tcbState = .running ∨ y1.tcbState = .restart) := by + by_cases e : specPtr t = σ.curThread + · exact ⟨{ xc with tcbState := .restart }, by rw [e, hσ1]; simp [stUpdA, setObjA], Or.inr rfl⟩ + · exact ⟨y, by rw [hσ1]; simp [stUpdA, setObjA, Function.update_of_ne e, hy], hrun⟩ + obtain ⟨y1, hy1, hrun1⟩ := h1t + have hcode : L.mem s1 (stA t) = sched.TS_RUNNING ∨ L.mem s1 (stA t) = sched.TS_RESTART := by + have h := (J1.ep.at_tcb ht hy1).st + rcases hrun1 with e | e <;> rw [e] at h + · left + apply UScalar.eq_of_val_eq + rw [ts_vals.1]; exact h + · right + apply UScalar.eq_of_val_eq + rw [ts_vals.2.1]; exact h + have hr1 : runnable y1.tcbState = true := by rcases hrun1 with e | e <;> rw [e] <;> rfl + have hdec : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (.Ok (), s1, a) := by + rw [hcap, decodeTcb_programA, if_neg Bool.false_ne_true, hlbl, + tcbResume_programA (ctx := ⟨len, buf, call, slot, extra⟩) hstall, hr, bind_ok] + unfold sched.restart + rw [isStopped_runnableA hcode, bind_ok] + simp only [Bool.false_eq_true, ite_false, bind_ok, uncurry_apply_pair] + exact ⟨xc, σ1, s1, hkc, rfl, hsp, resumeRunnable_specA hy1 hr1, hdec, Z.Q, Z.R, J1, Sched.BodyEndA.ofKeep J hkc (Sched.BodyKeepA.refl _)⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3TcbResumeRqA.lean b/proofs/Fl4ProofsA64/P3/Stage3TcbResumeRqA.lean new file mode 100644 index 000000000..cb00e47ee --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3TcbResumeRqA.lean @@ -0,0 +1,577 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 3c): **TCB Resume of a stopped thread at the record**. + +* **`JStage3A.of_stepStkA`**: `of_stepFinA` for a body that also shrinks the reply stacks (`stack'`). +* `JStage3A.restartStopped_stepA`: `sched.restart` of a stopped thread (`cancel_ipc`, `Restart`, the sporadic + unblock, `schedcontext::resume`, `possible_switch_to`) keeps the record and refines `fl4TcbRestart`, given the + record at the final state's `SysL` / `SymL` / `UserOK` / `J7A` (the syscall's `decodeFinA`). +* **`JSyscallA.resumeStoppedA`**: `decode_invocation` on a Thread cap with the Resume label at a syscall entry. + +`restart_refinesA`'s `tailS` is quantified over every related post state, which no entry fact discharges (the record +has no "bound SC is active" clause), so this uses the siblings `restart_refinesAtA` / `restart_footAtA`, whose +`tailS` also receives the two spec steps that reach that state. + +Named premises: [P] `ArchTotal`, `MaxSpi`, `ArchPlat`, `A64FrameMasks`, `hCs`, `hmb`, `hmbF`, `hmax`, `htu`; +[S] `h1` (`numNodes = 1`); `hsl` pins the spec cap at a slot to the target's thread cap (so the target is no core's +idle thread). No [J]: a bound SC that is inactive (`SchedContextBind` may bind one to an inactive thread) leaves the +target unschedulable, and the resume and switch do nothing (`restartTailAnyA`, `restart_validIA`). The decode +succeeds; the `Err` returns are not lifted. New module. +-/ +import Fl4ProofsA64.P3.Stage3TcbResumeSpecA +import Fl4ProofsA64.P3.Stage3TcbResumeNoopA +import Fl4ProofsA64.P3.Stage3TcbSuspResA +import Fl4ProofsA64.P3.Stage3KEntryBudgetA +import Fl4ProofsA64.P3.Stage3IpcGlueA +import Fl4ProofsA64.P3.Stage3FinA +import Fl4ProofsA64.P1.S1aCRestart +import Fl4ProofsA64.P3.Stage3BodyCurA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +instance (p : ObjRef) : QInst (fl4SchedContextResume (Z := Unit) p) := + ⟨by unfold fl4SchedContextResume fl4GetScRefillSufficient; qpres⟩ +instance (t : ObjRef) : QInst (testPossibleSwitchTo (Z := Unit) t) := ⟨by unfold testPossibleSwitchTo; qpres⟩ +instance (t : ObjRef) : QInst (fl4TcbRestart (Z := Unit) t) := ⟨by unfold fl4TcbRestart; qpres⟩ +instance (p : ObjRef) : KPresBInst (fl4SchedContextResume (Z := Unit) p) := + ⟨by unfold fl4SchedContextResume; kbres⟩ +instance (t : ObjRef) : KPresBInst (testPossibleSwitchTo (Z := Unit) t) := ⟨by unfold testPossibleSwitchTo; kbres⟩ + +/-- **`BodyEndA` after the caller's `Restart` and the Resume of a stopped thread** (dispatch restore): the budget +frame from `KPresB`, the current thread's binding from `SvKA` (`cancel_ipc`, `Restart`) and `TailSA` (the tail keeps +every non-SC object), its state from `BlkOthA`; `SwA` and `BlkOthA` are `restartStopped_stepA`'s. -/ +theorem Sched.BodyEndA.ofRestartCurA {S A : Type} {st : machine.Store S} [L : LawfulStore st] + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s : S} {a : A} {σ σ2 : State Unit} {m : McState} + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {xc : Tcb} (hkc : σ.kheap σ.curThread = some (.tcb xc)) {t : ObjRef} (htc : t ≠ σ.curThread) {y : Tcb} + (hy : (stUpdA σ σ.curThread xc .restart).kheap t = some (.tcb y)) + (hs : ¬ runnable y.tcbState = true ∧ ¬ idle y.tcbState) + (hm : ((), σ2) ∈ (fl4TcbRestart (Z := Unit) t (stUpdA σ σ.curThread xc .restart)).1) + (hsw : SwA σ2) (KB : BlkOthA t (stUpdA σ σ.curThread xc .restart) σ2) + (hinv : ScInvA m σ2) (hzc : ZeroConsA σ2) : BodyEndA σ σ2 m := by + have hk1 : (stUpdA σ σ.curThread xc .restart).kheap σ.curThread = + some (.tcb { xc with tcbState := .restart }) := by simp [stUpdA, setObjA] + have ne : σ.curSc ≠ σ.curThread := fun e => by + obtain ⟨y0, j, h0⟩ := J.curInv.sc + rw [e, hkc] at h0; cases h0 + have e1 : (stUpdA σ σ.curThread xc .restart).kheap σ.curSc = σ.kheap σ.curSc := by + simp [stUpdA, setObjA, Function.update_of_ne ne] + obtain ⟨t0, ht0, hts, -⟩ := J.curInv.tcb + rw [hkc] at ht0 + obtain rfl := KernelObject.tcb.inj (Option.some.inj ht0) + obtain ⟨σa, σb, h1, h2, h3⟩ := restart_partsA hy hs hm + have T3 : KPresB (do + let sc ← getTcbObjRef Tcb.tcbSchedContext t + ifSporadicCurScTestRefillUnblockCheck sc + NonDet.maybeM fl4SchedContextResume sc + testPossibleSwitchTo (Z := Unit) t : SMonad Unit Unit) := + PresR.bind KPresBInst.pres fun _ => PresR.bind KPresBInst.pres fun _ => PresR.bind KPresBInst.pres + fun _ => KPresBInst.pres + have T2 : KPresB (setThreadState (Z := Unit) t .restart) := by unfold setThreadState setThreadStateAct; kbres + have BK : BKeepA (stUpdA σ σ.curThread xc .restart) σ2 := + PreO.trans ((inferInstance : KPresBInst (cancelIpc (Z := Unit) t)).pres _ _ h1) + (PreO.trans (T2 _ _ h2) (T3 _ _ h3)) + have SV : SvKA (stUpdA σ σ.curThread xc .restart) σb := + PreO.trans (cancelIpc_svkA t _ _ h1) (setThreadState_svkA t .restart rfl _ _ h2) + obtain ⟨yb, hyb, esb, -⟩ := SV.tcb _ _ hk1 + have hk2 : σ2.kheap σ.curThread = some (.tcb yb) := + (restartTail_tailSA t _ _ h3).obj _ _ hyb (fun _ _ e => by cases e) + refine BodyEndA.ofCurBA J hinv hzc hsw BK.time (by rw [BK.ms]; rfl) BK.ct BK.csc BK.used (fun y' k h => ?_) + ⟨yb, by rw [BK.ct]; exact hk2, by rw [esb, BK.csc]; exact hts⟩ (fun _ x hx => ?_) + · obtain ⟨y, hy, h1, h2⟩ := BK.sc y' k h + exact ⟨y, by rw [← e1]; exact hy, h1, h2⟩ + · obtain ⟨z, hz, ez⟩ := KB.tcb _ _ htc.symm hk1 + rw [hx] at hz + obtain rfl := KernelObject.tcb.inj (Option.some.inj hz) + exact Or.inr ez + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack stack' : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s s' : S} {a : A} {σ σ' : State Unit} {m : McState} {Q : U64 → U64 → List U64} + {Rl : U64 → List U64} {curOf curScOf : CoreId → ObjRef} + +set_option maxHeartbeats 2000000 in +/-- **`JStage3A` across an operation body that may shrink the reply stacks** (module docstring). -/ +theorem JStage3A.of_stepStkA {Ac' : Set U64} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (w : WritesIn st s s' (SchedStepNWA (st := st) N E Rp Z m σ s)) (HS : SpecSchedA σ σ') (SSz : SSzA σ σ') + (Qk : QKeepA σ σ') (hum : σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory) + (hcurOf : ∀ k, k ≠ m.curCore → curOf k = Z.curOf k) + (bxi : BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac' s') + (sysl : SysL st H Z.Cs OS ia ie s') (symL : SymL st H Z.Cs s') + (userOK : Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s') + (j7 : J7A st (J7R2A st ar nA) Z H OS s' σ' m) + (sr : SchedSRA st { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } m κ s' σ') + (ep : EpStateRel Z.T E Rp Bf (L.mem s') σ') + (ntfn : NtfnStateRel N Z.T (L.mem s') σ') + (reply : ReplyStackRelA (L.mem s') Rp Z.C σ' (scNameA m) stack') + (hdr : ∀ r ∈ Rp, DeadA st OS .reply s r → ∀ c ∈ Z.C, r ∉ stack' c) + (bufCover : BufCover st Z.T Bf s' σ') (bufAl : UtBufAlA Z.T σ') + (idleNoFault : IdleNoFaultA m σ') (mcp : McpRelA Z.T m (L.mem s') σ') + (yieldRel : YieldRelA Z.T Z.C m (L.mem s') σ') + (sinv : SInvXA σ' m n) (sidle : SIdleA σ' m n) + (runVcpu : RunVcpuInv st Z.T s') (blkQ : BlkQueuedA Z.T E N (L.mem s') σ') + (schedValid : SchedValidA σ') : + JStage3A st AR { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } H OS U Ac' Pp Tb Lv ia ie N E Rp + Bf stack' κ n (J5A st) (J7A st (J7R2A st ar nA)) s' a σ' m := by + have kb : L.ksb s' = L.ksb s := w.1 + have osk : OSKeep st OS s s' := J.osKeep_stepNA w + have vsr : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → L.mem s' b = L.mem s b := fun b hb => by + refine w.mem_eq (J.vsOk.al b hb) ?_ + by_cases h : AsW (L.ksb s) b + · obtain ⟨o, o1, o2, -, e⟩ := h + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.sys.ent.ksw + have ev : b.val = (L.ksb s).val + o.val := by + rw [e, wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact J.asid_offStepNA (by omega) (by omega) + · exact J.vsr_offStepNA J.vsOk hb h + have sys : SysInvA st H Z.Cs OS U Ac' Pp Tb Lv ia ie s' := + ⟨⟨J.sys.ent.heap, bxi, J.sys.ent.os, userOK, (utVsPt_keepA J.sys.ent.vs kb vsr).1, + by rw [kb]; exact J.sys.ent.ksw⟩, sysl⟩ + have sq : SchedQueuesInv st H.tcbs Z.Cs H.omega H s' Q Rl := by + obtain ⟨Q0, R0, h0⟩ := bxi.1 + exact SchedQueuesInv.reCoreA h0 (by rw [J.tcbs]; exact sr.core) + have poolOff : PoolOff st H Z.Cs OS s' := fun i => by + have h0 := J.poolOff i + rw [vspace.asid_table.eq] at h0 ⊢ + have v := AtOff.v i + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.sys.ent.ksw + have ev : (wa (L.ksb s) (AtOff i)).val = (L.ksb s).val + (AtOff i).val := by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have e : L.mem s' (wa (L.ksb s') (AtOff i)) = L.mem s (wa (L.ksb s) (AtOff i)) := by + rw [kb] + refine w.mem_eq ?_ (J.asid_offStepNA (by omega) (by omega)) + have h32 := J.j.1.ksbAl + show (wa (L.ksb s) (AtOff i)).val % 8 = 0 + omega + rw [e] + refine (WP.spec_ok _).2 fun hne asid => ?_ + have q := (WP.spec_ok _).1 h0 hne asid + exact ⟨q.al, q.om, q.sg, q.os, RegionOffNodes.ksb kb q.ks⟩ + have scCov : ScCovA Z.C m σ' := fun q x k h => by + obtain ⟨y, hy⟩ := SSz q x k h + exact J.scCov q y k hy + have hTr : TreeA st OS Z.G.chains Z.G.par s := + J.j.1.cdt.tree.grow J.j5.2.1 (fun x hx hn => J.j5.2.2 x hx hn) + have j5 : J5A st { Z with Q := Q, R := Rl, curOf := curOf, curScOf := curScOf } H OS s' σ' m := + ⟨⟨MdbInvA.keepO hTr.lk hTr.tree J.j5.1.1 osk, IrqRunA.keepO hTr.lk J.j5.1.2.1 osk, + HandlersActiveA.keepO hTr.lk J.j5.1.2.2 osk kb (fun i hi => by + have eI : irqStAddrA (L.ksb s) i = irqWordA (L.ksb s) i := rfl + have hv := irqWordA_val J.sr.ksFit hi + have hal : Aligned (irqWordA (L.ksb s) i) := by + have := J.sr.toSchedSRN.ksb_al + show _ % 8 = 0 + omega + rw [eI] + exact w.mem_eq hal (J.irq_offStepNA hi))⟩, J.j5.2.1, + fun y hy hn => by rw [osk.capA hy]; exact J.j5.2.2 y hy hn⟩ + exact JStage3A.of_schedWrXS J (Q := Q) (Rl := Rl) (curOf := curOf) (curScOf := curScOf) (a' := a) + (X := fun _ => False) + ⟨fun _ h => h.elim, fun _ _ _ _ _ h => h, fun _ _ _ _ h => h, fun _ _ h => h, fun _ _ h => h, + fun _ _ _ _ _ h => h, fun _ _ _ _ h => h, fun _ _ h => h, fun _ _ h => h⟩ + (fun _ _ h => h) bufAl (w.mono fun _ h => Or.inl h) HS SSz Qk.toSend (fun x _ => by rw [hum]) J.j.1.ar hcurOf + sys sq poolOff sr scCov ep ntfn reply hdr bufCover idleNoFault mcp yieldRel sinv sidle runVcpu blkQ symL + schedValid (fun _ _ _ _ _ h => h) j5 j7 + +/-- A `SchedStepNWA` step keeps every TCB's `RUN_VCPU` word. -/ +theorem JStage3TA.stepNW_runA {J5 J7 : JSlotA S} [J7PpA st J7] + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (w : WritesIn st s s' (SchedStepNWA (st := st) N E Rp Z m σ s)) {u : U64} (hu : u ∈ Z.T) : + L.mem s' (runVcpuA u) = L.mem s (runVcpuA u) := by + have huH : u ∈ H.tcbs := by rw [J.tcbs]; exact hu + have hfit := (J.sr.core.tcbs u hu).fits + have hv : (wa u 3672#u64).val = u.val + 3672 := by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by simp; omega) + show L.mem s' (wa u 3672#u64) = L.mem s (wa u 3672#u64) + refine w.mem_eq (field_aligned (J.sr.core.tcbs u hu) (by simp) (by simp)) (fun h => ?_) + rcases h with (hm | ⟨v, hv', h1, h2, -, h4⟩) | hipc + · exact J.tcbWord_offMA hu (by simp) (by simp) hm + · by_cases e : v = u + · subst e; exact h4 rfl + · have := J.sr.place.apart v u .tcb .tcb ((J.sr.thT v).2 hv') ((J.sr.thT u).2 hu) e + have hsz : ObjKindA.size .tcb = 4096 := rfl + omega + · exact J.ipc_off_hobjA (v := u) (kv := .tcb) huH (by simp) + ⟨by omega, by simp only [HKind.bits]; omega⟩ hipc + +/-- **`BlkQueuedA` after `restart`**. -/ +theorem JStage3A.blkQ_restartA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {t : U64} (hy : ∃ y, σ'.kheap (specPtr t) = some (.tcb y) ∧ y.tcbState = .restart) + (K : BlkOthA (specPtr t) σ σ') (E' : EpStateRel Z.T E Rp Bf (L.mem s') σ') + (rn : NtfnStateRel N Z.T (L.mem s') σ') : BlkQueuedA Z.T E N (L.mem s') σ' where + ep := fun u hu x' hk e hb => by + have ne : specPtr u ≠ specPtr t := fun h => by + obtain ⟨y, h1, h2⟩ := hy + rw [h, h1] at hk; cases hk + rw [h2] at hb; cases hb + obtain ⟨x, hx, rel⟩ := J.ep.tcb u hu + obtain ⟨z, hz, es⟩ := K.tcb _ _ ne hx + rw [hk] at hz; cases hz + have hb0 : Fl4SpecA64.epBlocked x.tcbState = some e := es ▸ hb + obtain ⟨mE, y, h1, h2⟩ := J.blkQ.ep u hu x hx e hb0 + obtain ⟨y', h1', h2'⟩ := K.ep _ _ _ ne h1 h2 + obtain ⟨x2, hx2, rel'⟩ := E'.tcb u hu + rw [hk] at hx2; cases hx2 + have em : L.mem s' (tBlockA u) = L.mem s (tBlockA u) := + specPtr_inj ((rel'.blk e hb).trans (rel.blk e hb0).symm) + refine ⟨?_, y', h1', h2'⟩ + show L.mem s' (tBlockA u) ∈ E + rw [em]; exact mE + ntfn := fun u hu x' hk p hb => by + have ne : specPtr u ≠ specPtr t := fun h => by + obtain ⟨y, h1, h2⟩ := hy + rw [h, h1] at hk; cases hk + rw [h2] at hb; cases hb + obtain ⟨x, hx, rel⟩ := J.ntfn.tcb u hu + obtain ⟨z, hz, es⟩ := K.tcb _ _ ne hx + rw [hk] at hz; cases hz + have hb0 : x.tcbState = .blockedOnNotification p := es.symm.trans hb + obtain ⟨mN, y, q, h1, h2, h3⟩ := J.blkQ.ntfn u hu x hx p hb0 + obtain ⟨y', q', h1', h2', h3'⟩ := K.ntfn _ _ _ _ ne h1 h2 h3 + obtain ⟨x2, hx2, rel'⟩ := rn.tcb u hu + rw [hk] at hx2; cases hx2 + have em : L.mem s' (Notif.tBlockA u) = L.mem s (Notif.tBlockA u) := + specPtr_inj ((rel'.blk p hb).trans (rel.blk p hb0).symm) + exact ⟨by rw [em]; exact mN, y', q', h1', h2', h3'⟩ + +/-- **`SchedValidA` across `fl4TcbRestart` of a stopped thread, any bound SC** (`restart_validA` without `hact`): +when the bound SC is inactive after the sporadic unblock, the thread is not schedulable and the resume and the +switch test do nothing. -/ +theorem restart_validIA {σ σ' : State Unit} {t : ObjRef} {x : Tcb} (h : SchedValidA σ) (D : RqDomPrio σ) + (hk : σ.kheap t = some (.tcb x)) (hs : ¬ runnable x.tcbState = true ∧ ¬ idle x.tcbState) (htc : t ≠ σ.curThread) + (hlink : ∀ p, x.tcbSchedContext = some p → ∃ y k, σ.kheap p = some (.schedContext y k) ∧ y.scTcb = some t) + (hr : ((), σ') ∈ (fl4TcbRestart (Z := Unit) t σ).1) : SchedValidA σ' := by + obtain ⟨σ1, σ2, h1, h2, h3⟩ := restart_partsA hk hs hr + have S2 : SvKA σ σ2 := PreO.trans (cancelIpc_svkA t _ _ h1) (setThreadState_svkA t .restart rfl _ _ h2) + have V2 := h.svkA S2 + have D2 : RqDomPrio σ2 := RqDomPrio.svkA D S2 + obtain ⟨y, hy, ysc, -⟩ := S2.tcb _ _ hk + obtain ⟨sc, hsc, h3⟩ := eqPres_mem (by unfold getTcbObjRef; exact threadGet_eqPres _ t) h3 + have esc : sc = x.tcbSchedContext := by + have e : getTcbObjRef (Z := Unit) Tcb.tcbSchedContext t σ2 = ({(y.tcbSchedContext, σ2)}, False) := + threadGet_eqSc _ hy + rw [e] at hsc + exact (congrArg Prod.fst (show (sc, σ2) = (y.tcbSchedContext, σ2) from hsc)).trans ysc + obtain ⟨⟨⟨⟩, σ3⟩, h4, h3⟩ := mem_bindA.1 h3 + have R3 : RefK σ2 σ3 := ifCondRefillUnblockCheck_refK _ _ _ _ _ h4 + have T3 : TailSA σ2 σ3 := ifCondRefillUnblockCheck_tailSA _ _ _ _ _ h4 + have V3 := V2.refKA R3 + have D3 : RqDomPrio σ3 := fun f d p z hz hm => by + rw [show σ3.readyQueues = σ2.readyQueues by rw [R3.1]] at hm + obtain ⟨w, hw, -⟩ := (R3.2.1 f).tcbB hz + have e := T3.obj f _ hw (fun _ _ c => by cases c) + rw [hz] at e; cases e + exact D2 f d p z hw hm + have link3 : ∀ p, x.tcbSchedContext = some p → ∃ w k, σ3.kheap p = some (.schedContext w k) ∧ + w.scTcb = some t := fun p ex => by + obtain ⟨w, k, hw, l⟩ := hlink p ex + obtain ⟨w2, hw2, -, l2⟩ := S2.sc p w k hw + obtain ⟨w3, hw3, -, -, l3⟩ := T3.sc p w2 k hw2 + exact ⟨w3, k, hw3, l3.trans (l2.trans l)⟩ + have hy3 : σ3.kheap t = some (.tcb y) := T3.obj t _ hy (fun _ _ c => by cases c) + obtain ⟨⟨⟨⟩, σ4⟩, h5, h6⟩ := mem_bindA.1 h3 + by_cases B3 : BoundActive t σ3 + · have F4 : FIA σ4 ∧ BoundActive t σ4 := by + cases sc with + | none => + have e : ((), σ4) = ((), σ3) := h5 + cases e; exact ⟨Sched.FIA.ofValid V3, B3⟩ + | some p => + refine fl4SchedContextResume_fiaA p t (Sched.FIA.ofValid V3) D3 B3 (fun w k t' hw ht' => Or.inl ?_) h5 + obtain ⟨v, kv, hv, l⟩ := link3 p esc.symm + rw [hv] at hw; cases hw + rw [l] at ht' + rw [← Option.some.inj ht', show σ3.curThread = σ2.curThread by rw [R3.1], S2.cur] + exact htc.symm + unfold testPossibleSwitchTo at h6 + obtain ⟨b, -, h6⟩ := eqPres_mem (PresR.gets _) h6 + unfold NonDet.when at h6 + split_ifs at h6 + · exact (possibleSwitchTo_fia' t (fun _ => True) (fun _ _ h => h) (fun _ _ h => h) σ4 + ⟨F4.1, F4.2, trivial⟩ _ h6).1.toValid + · have e : ((), σ') = ((), σ4) := h6 + cases e; exact F4.1.toValid + · -- the bound SC is inactive: `t` is not schedulable, so the resume and the switch test do nothing + have hns : ¬ schedulable t σ3 = true := fun hb => B3 fun _ _ _ _ => (schedulable_actA hb).1 + have e4 : σ4 = σ3 := by + cases sc with + | none => + have e : ((), σ4) = ((), σ3) := h5 + cases e; rfl + | some p => + obtain ⟨w, k, hw, l⟩ := link3 p esc.symm + have eR : fl4SchedContextResume (Z := Unit) p σ3 = ({((), σ3)}, False) := by + unfold fl4SchedContextResume fl4GetScRefillSufficient + rw [nd_bind_oneSc (Sched.getSchedContext_eqA hw), l, + nd_bind_oneSc (show NonDet.assertOpt (σ := State Unit) (some t) σ3 = ({(t, σ3)}, False) from rfl), + nd_gets_bindSc] + unfold NonDet.when + rw [if_neg hns] + rfl + have h5' : ((), σ4) ∈ (fl4SchedContextResume (Z := Unit) p σ3).1 := h5 + rw [eR] at h5' + exact congrArg Prod.snd h5' + subst e4 + unfold testPossibleSwitchTo at h6 + rw [nd_gets_bindSc] at h6 + unfold NonDet.when at h6 + rw [if_neg hns] at h6 + have e : ((), σ') = ((), σ4) := h6 + cases e; exact V3 + +set_option maxHeartbeats 4000000 in +/-- **`sched.restart` of a stopped thread at the record** (module docstring). -/ +theorem JStage3A.restartStopped_stepA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (sact : SActA σ) (ns : ∀ u, σ.schedulerAction ≠ .switchThread u) + (hA : P0.ArchTotal ar) (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hmbF : ∀ a, Fl4MinBudgetAgreeA ar a) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {t : U64} (ht : t ∈ Z.T) {x : Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hs : ¬ runnable x.tcbState = true ∧ ¬ idle x.tcbState) (htc : specPtr t ≠ σ.curThread) + (hti : specPtr t ≠ σ.idleThread) (hidles : ∀ c < n, m.idleThreadOf c ≠ specPtr t) + (hcsc : ∃ xc, σ.kheap σ.curThread = some (.tcb xc) ∧ xc.tcbSchedContext = some σ.curSc) : + sched.restart st ar s a t ⦃ out => out.2 = a ∧ ∃ σ' stack' Q Rl, + fl4TcbRestart (Z := Unit) (specPtr t) σ = ({((), σ')}, False) ∧ SwA σ' ∧ BlkOthA (specPtr t) σ σ' ∧ + (SysL st H Z.Cs OS ia ie out.1 → SymL st H Z.Cs out.1 → + Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U out.1 → + J7A st (J7R2A st ar nA) Z H OS out.1 σ' m → + JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack' κ n (J5A st) + (J7A st (J7R2A st ar nA)) out.1 a σ' m) ⦄ := by + have T := J.toTA sact ns + have htH : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + have hc : m.curCore < fl4A64Platform.numNodes := T.curCore_lt hCs + have hidleN : ∀ c < fl4A64Platform.numNodes, specPtr t ≠ m.idleThreadOf c := fun c hc' e => by + have ec : c = m.curCore := by + have a1 := hc + rw [h1] at a1 hc' + exact (Nat.lt_one_iff.1 hc').trans (Nat.lt_one_iff.1 a1).symm + refine hti ?_ + rw [e, ec, J.sr.sch.idle, J.sr.sch.core, J.sr.sch.idleOf _ J.sr.sch.cpuCs] + rfl + have hon := T.onCore_single hCs h1 ht + have hrep := T.repOKA ht + obtain ⟨x0, hx0, hrel⟩ := J.ep.tcb t ht + have ex : x0 = x := by rw [hk] at hx0; cases hx0; rfl + subst ex + have hReply : ∀ r', x0.tcbState = .blockedOnReply r' → ∃ r ∈ Rp, specPtr r = r' ∧ + ∃ xr, σ.kheap r' = some (.reply xr) ∧ xr.replyTcb = some (specPtr t) ∧ ScReplyCovA Z.C m σ r' := by + intro r' hb + have e1 : L.mem s (Fl4ProofsA64.P1.stA t) = sched.TS_BLOCKED_ON_REPLY := by + apply UScalar.eq_of_val_eq + show (L.mem s (Fl4ProofsA64.P1.stA t)).val = _ + rw [hrel.st, hb]; unfold sched.TS_BLOCKED_ON_REPLY; rfl + obtain ⟨hin, back⟩ := T.blkA.2.2.1.2.1.reply t htH e1 + have hiR : L.mem s (tReplyA t) ∈ Rp := J.ipcHeapCov.reply _ hin + obtain ⟨xr, hkr, hb'⟩ := J.ep.reply _ hiR + have er : specPtr (L.mem s (tReplyA t)) = r' := hrel.reply r' hb + have t0 : t ≠ 0#u64 := J.sr.place.nz t _ ((J.sr.thT t).2 ht) + have back' : L.mem s (Endp.rTcbA (L.mem s (tReplyA t))) = t := back + exact ⟨_, hiR, er, xr, by rw [← er]; exact hkr, by rw [hb', back', optRef_ne t0], J.scCov.reply _⟩ + have hE : ∀ e', epBlocked x0.tcbState = some e' → L.mem s (tBlockA t) ∈ E ∧ + ∃ z, σ.kheap e' = some (.endpoint z) ∧ specPtr t ∈ z.epQueue := fun e' he => J.blkQ.ep t ht x0 hk e' he + have hN : ∀ n', x0.tcbState = .blockedOnNotification n' → L.mem s (Notif.tBlockA t) ∈ N ∧ + ∃ z, σ.kheap n' = some (.notification z) ∧ specPtr t ∈ z.ntfnObj.ntfnQueue := fun n' hn => by + obtain ⟨a1, y, q, hy, hq, hm⟩ := J.blkQ.ntfn t ht x0 hk n' hn + refine ⟨a1, y, hy, ?_⟩ + rw [hq] + simpa only [Fl4SpecA64.Ntfn.ntfnQueue] using hm + have tailS : ∀ (s2 : S) (σ2 : State Unit) (Q : U64 → U64 → List U64) (RR : U64 → List U64) (σc : State Unit), + SchedSRB st { Z with Q := Q, R := RR } m κ s2 σ2 (specPtr t) → + (∃ y, σ2.kheap (specPtr t) = some (.tcb y) ∧ y.tcbState = .restart) → + cancelIpc (Z := Unit) (specPtr t) σ = ({((), σc)}, False) → + setThreadState (Z := Unit) (specPtr t) .restart σc = ({((), σ2)}, False) → + (L.mem s2 (scA t) ≠ 0#u64 → L.mem s2 (scCoreA (L.mem s2 (scA t))) = L.cpu s2) := by + intro s2 σ2 Q RR σc H2 _ _ _ h0 + have hC := H2.rest.scMem t ht h0 + rw [H2.scCore _ hC] + have a1 := J.scCpu _ hC + have a2 := hCs _ H2.sch.cpuCs + rw [h1] at a1 a2 + exact UScalar.eq_of_val_eq (by omega) + have PE0 : EPlaced Z.T E Rp (∅ : Set (U64 × Fl4SpecA64.Word)) := + { J.iplaced.e with + balign := fun _ ⟨_, h, _⟩ => h.elim + bt := fun _ ⟨_, h, _⟩ => h.elim + be := fun _ ⟨_, h, _⟩ => h.elim + br := fun _ ⟨_, h, _⟩ => h.elim + binj := fun _ h => h.elim } + have hP0 : IPlacedA N Z.T E Rp (∅ : Set (U64 × Fl4SpecA64.Word)) := + ⟨J.iplaced.n, PE0, J.iplaced.ne, J.iplaced.nr, fun _ _ _ ⟨_, h, _⟩ => h.elim⟩ + have P0' : IpcSchedPlacementA Z.th N E Rp (∅ : Set (U64 × Fl4SpecA64.Word)) := + ⟨J.ipcPlace.notification, J.ipcPlace.endpoint, J.ipcPlace.reply, fun _ ⟨_, h, _⟩ => h.elim⟩ + have HE0 : EpStateRel Z.T E Rp (∅ : Set (U64 × Fl4SpecA64.Word)) (L.mem s) σ := + ⟨J.ep.ep, J.ep.reply, J.ep.tcb, fun _ h => h.elim⟩ + refine WP.spec_mono (WP.spec_and + (restart_refinesAtA (st := st) (ar := ar) (κ := κ) J.iplaced J.ipcPlace hc hmb hmbF hmax htu s a σ + ⟨Z.Q, Z.R, J.sr⟩ J.ntfn J.ep J.reply J.mcp J.yieldRel J.scNZ ht hk hReply hE hN hrep hon hidleN tailS) + (WP.spec_and + (restart_footAtA (st := st) (ar := ar) (κ := κ) hP0 P0' hc hmb hmbF hmax htu s a σ ⟨Z.Q, Z.R, J.sr⟩ J.ntfn HE0 + J.reply J.mcp J.yieldRel J.scNZ ht hk hReply hE hN hrep hon hidleN tailS) + (sched.restart.dm (st := st) (ar := ar) hA a t T.blkA htH J.sys.sysl.ly.h0 J.sys.sysl.vr J.symL))) ?_ + rintro ⟨s', a'⟩ ⟨⟨ha, σ', stk', hspec, rn, rel, ep, rs, mcp, yr, -, hy⟩, ⟨σ2, hspec2, w, F⟩, blk, foot, -⟩ + have e2 : σ2 = σ' := + congrArg Prod.snd (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst (hspec2.symm.trans hspec))) + subst e2 + simp only at ha + subst a' + have w' : WritesIn st s s' (SchedStepNWA (st := st) N E Rp Z m σ s) := + w.mono fun _ h => h.resolve_right fun ⟨_, h, _⟩ => h.elim + have hmem : ((), σ2) ∈ (fl4TcbRestart (Z := Unit) (specPtr t) σ).1 := by rw [hspec]; exact rfl + have K : SStepA σ σ2 := restart_sstepA hk hs hmem + have KB : BlkOthA (specPtr t) σ σ2 := restart_blkOthA hk hs (fun e' he => (hE e' he).2) + (fun n' he => (hN n' he).2.imp fun _ h => h.1) hmem + have hlink : ∀ p, x0.tcbSchedContext = some p → ∃ y k, σ.kheap p = some (.schedContext y k) ∧ + y.scTcb = some (specPtr t) := fun p ep => J.sr.inv.back _ _ _ hk ep + have hzc : x0.tcbSchedContext = some σ.curSc → σ.consumedTime = 0 := fun e => by + exfalso + obtain ⟨y, k, hy, l⟩ := hlink _ e + obtain ⟨ct, hct, hsc⟩ := hcsc + obtain ⟨y2, n2, hy2, l2⟩ := J.sr.inv.back _ _ _ hct hsc + rw [hy] at hy2 + obtain ⟨e1, -⟩ := KernelObject.schedContext.inj (Option.some.inj hy2) + subst e1 + exact htc (Option.some.inj (l.symm.trans l2)) + obtain ⟨I, Isw⟩ := restart_invA (m := m) (n := n) ⟨J.sinv, J.sidle, sact⟩ ns hk hs (fun e' he => (hE e' he).2) + (fun n' he => (hN n' he).2.imp fun _ h => h.1) hrep htc hti hidles hlink hzc hmem + have V := restart_validIA J.schedValid (Sched.JStage3A.rqDomPrioA J) hk hs htc + (fun p ep => J.sr.inv.back _ _ _ hk ep) hmem + obtain ⟨y', hy', hys⟩ := hy hs + obtain ⟨Q, Rl, sr⟩ := rel + obtain ⟨HS, SSz, hum⟩ := F + have rv : RunVcpuInv st Z.T s' := fun u hu hne => by + rw [T.stepNW_runA w' hu] at hne + by_cases eu : u = t + · subst eu + refine absurd (RunVcpuInv.zero J.runVcpu hu fun e => ?_) hne + have h := hrel.st + rw [e] at h + revert h hs + cases x0.tcbState with + | ext q => cases q; simp [Notif.tsCode, sched.TS_RUNNING_VM, runnable, idle, ExtThreadState.runnable] + | _ => simp [Notif.tsCode, sched.TS_RUNNING_VM, runnable, idle] + · obtain ⟨z, hz, r0⟩ := J.ep.tcb u hu + obtain ⟨z', hz', es⟩ := KB.tcb _ _ (fun h => eu (specPtr_inj h)) hz + obtain ⟨z2, hz2, r1⟩ := ep.tcb u hu + rw [hz'] at hz2; cases hz2 + have e : L.mem s' (Fl4ProofsA64.P1.stA u) = L.mem s (Fl4ProofsA64.P1.stA u) := by + apply UScalar.eq_of_val_eq + exact (r1.st.trans (by rw [es])).trans r0.st.symm + exact e.trans (J.runVcpu u hu hne) + have bq : BlkQueuedA Z.T E N (L.mem s') σ2 := J.blkQ_restartA ⟨y', hy', hys⟩ KB ep rn + have bufAl : UtBufAlA Z.T σ2 := K.utBufAlA J.bufAl fun u hu => by + obtain ⟨z, hz, -⟩ := J.ntfn.tcb u hu + exact ⟨z, hz⟩ + have idl : IdleNoFaultA m σ2 := K.idleNoFaultA J.idleNoFault + have buf : BufCover st Z.T Bf s' σ2 := T.toBA.bufCover_susA w' (Fl4ProofsA64.P1.K8.Foot.d foot) K + refine ⟨rfl, σ2, stk', Q, Rl, hspec, Isw, KB, fun sysl symL userOK j7 => ?_⟩ + exact J.of_stepStkA (Q := Q) (Rl := Rl) (curOf := Z.curOf) (curScOf := Z.curScOf) w' HS SSz (Sched.qk_of hmem) hum + (fun _ _ => rfl) (BlkOK.bxi blk) sysl symL userOK j7 sr ep rn rs + (fun r hr hd c hc' h => J.deadExt.reply r hr hd c hc' (K.stackSubA J.reply rs c hc' r h)) buf bufAl idl mcp yr + I.sinv I.sidle rv bq V + +/-- The spec's Resume is `fl4TcbRestart`. -/ +theorem resumeStopped_specA {σ σ' : State Unit} {t : ObjRef} + (h : fl4TcbRestart (Z := Unit) t σ = ({((), σ')}, False)) : + (.ok [], σ') ∈ ((fl4InvokeTcb (.resume t)).run σ).1 := by + simp only [fl4InvokeTcb] + apply mem_liftED + refine mem_bind_ndD (show ((), σ') ∈ (fl4TcbRestart (Z := Unit) t σ).1 by rw [h]; rfl) ?_ + rfl + +set_option maxHeartbeats 4000000 in +/-- **TCB Resume of a stopped thread at a syscall entry** (module docstring). -/ +theorem JSyscallA.resumeStoppedA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hmbF : ∀ a, Fl4MinBudgetAgreeA ar a) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {slot lbl len buf t : U64} {blk call don : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Thread t) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hlbl : lbl = fl4_abi.label.TCBResume) + {sl : CslotPtr} (hsl : specCapAtA σ sl = some (.threadCap (specPtr t))) + {y : Tcb} (hy : σ.kheap (specPtr t) = some (.tcb y)) + (hstop : ¬ runnable y.tcbState = true ∧ ¬ idle y.tcbState) : + ∃ xc σ1 σ2 s2, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + (.ok [], σ2) ∈ ((fl4InvokeTcb (.resume (specPtr t))).run σ1).1 ∧ + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (.Ok (), s2, a) ∧ + ∃ Q Rl stack', JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack' κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s2 a σ2 m ∧ + (((), σ2) ∈ (fl4TcbRestart (Z := Unit) (specPtr t) σ1).1 ∧ SwA σ2 ∧ BlkOthA (specPtr t) σ1 σ2 ∧ + specPtr t ≠ σ.curThread) := by + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hslot hcov J.actW J.running J.cur + have hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM := J.toJStage3EntryA.curNotVmA + obtain ⟨xc, hkc, hst, hsp, J1, -, -, -⟩ := J.toJStage3A.of_restartCurA RS.pl.choose_spec.choose_spec.choose_spec.choose_spec + RS.sym RS.run hnvm RS.cur RS.vs RS.vc + have ht : t ∈ Z.T := J.tcbs ▸ (J.toJStage3A.slotCapTy hslot).1.2.2.2.2.2 t hcap + have T0 := J.toJStage3A.toTA J.sact J.noSwitch + have hstall : smp.remote_tcb_stall st ar s a t = ok (s, a) := T0.stall_singleA hCs h1 ht + have hni := T0.notIdle_of_capA J.sinv.mc.curCore_lt hsl + have htc : specPtr t ≠ σ.curThread := fun e => by + have ec : t = L.curW s := specPtr_inj (e.trans J.sr.sch.curThread) + obtain ⟨z, hz, r0⟩ := J.ep.tcb t ht + rw [hy] at hz; cases hz + have h := J.running + rw [← ec] at h + have c := r0.st + unfold Fl4ProofsA64.P1.Runnable at h + revert c hstop + rcases h with h | h | h <;> rw [show Fl4ProofsA64.P1.stA t = stA t from rfl] at * <;> + cases y.tcbState <;> simp_all [Notif.tsCode, runnable, idle, sched.TS_RUNNING, sched.TS_RESTART, + sched.TS_RUNNING_VM] + set s1 := L.wr s (stA (L.curW s)) sched.TS_RESTART with hs1 + set σ1 := stUpdA σ σ.curThread xc .restart with hσ1 + have hr : tcb.restart_caller st s = ok s1 := by + unfold tcb.restart_caller + rw [cur_thread_ok, bind_ok, hst] + have far : ∀ q, q ≠ σ.curThread → σ1.kheap q = σ.kheap q := fun q hq => by + rw [hσ1]; simp [stUpdA, setObjA, Function.update_of_ne hq] + have sact1 : SActA σ1 := + ⟨fun u h => J.sact.f1 u h, ⟨{ xc with tcbState := .restart }, by simp only [hσ1, stUpdA, setObjA, + Function.update_self], Or.inr (Or.inl rfl)⟩⟩ + have hy1 : σ1.kheap (specPtr t) = some (.tcb y) := (far _ htc).trans hy + have R := J1.restartStopped_stepA sact1 (fun u h => J.noSwitch u h) hA hCs h1 hmb hmbF hmax htu ht hy1 hstop + htc hni.1 hni.2 ⟨{ xc with tcbState := .restart }, by simp only [hσ1, stUpdA, setObjA, Function.update_self], + by obtain ⟨t0, ht0, hs0, -⟩ := J.toJStage3EntryA.curInv.tcb; rw [hkc] at ht0; cases ht0; exact hs0⟩ + obtain ⟨⟨s2, a2⟩, hrs, ha, σ2, stk', Q, Rl, hspec, hsw, KB, fin⟩ := Fl4ProofsA64.P1.W7.R.ST.ok_of_spec R + simp only at ha + subst a2 + have hdec : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (.Ok (), s2, a) := by + rw [hcap, decodeTcb_programA, if_neg Bool.false_ne_true, hlbl, + tcbResume_programA (ctx := ⟨len, buf, call, slot, extra⟩) hstall, hr, bind_ok, hrs, bind_ok] + simp only [uncurry_apply_pair] + obtain ⟨-, -, sysl, symL, -, userOK, j7⟩ := J.decodeFinA hA hM hPl hfm hslot hcov + (rfl : capA st s slot = capA st s slot) hext hSC (lbl := lbl) + (by rw [hcap, hlbl]; simp [DecArmS1aEnt, DecArmQH, DecArmQZ2, DecArmKQ2, DecArmKQ, DecArmKQ3, DecArmDelQ, + DecArmS1aC, capFam]) + (fun _ h => by rw [hcap] at h; cases h) (fun _ h => by rw [hcap] at h; cases h) hdec σ2 m + have J2 := fin sysl symL userOK j7 + have hm2 : ((), σ2) ∈ (fl4TcbRestart (Z := Unit) (specPtr t) σ1).1 := by rw [hspec]; exact Set.mem_singleton _ + exact ⟨xc, σ1, σ2, s2, hkc, rfl, hsp, resumeStopped_specA hspec, hdec, Q, Rl, stk', J2, hm2, hsw, KB, htc⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3TcbResumeSpecA.lean b/proofs/Fl4ProofsA64/P3/Stage3TcbResumeSpecA.lean new file mode 100644 index 000000000..f585d9245 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3TcbResumeSpecA.lean @@ -0,0 +1,739 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 3): **the spec side of Resume of a stopped thread** (`fl4TcbRestart`: `cancel_ipc`, +`Restart`, the sporadic unblock check, `sched_context_resume`, `possible_switch_to`). + +* `SvKA`: what `cancel_ipc` and a non-blocked state write keep of the data `SchedValidA` and `RqDomPrio` read; + `cancelIpc_svkA`. +* `TailSA`: what the tail keeps of the heap (everything but SC refills); `tail_tailSA`. +* `restart_sstepA`, `restart_blkOthA`: `SStepA` and `BlkOthA` of the whole `fl4TcbRestart`. +* `restart_invA`: `SInvXA` / `SIdleA` across `fl4TcbRestart` (`LoopInvA`), and `SwA` after it (dispatch restore). +* `restart_validA`: `SchedValidA` across `fl4TcbRestart`. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.DelThrSusA +import Fl4ProofsA64.P3.Stage3WakeInvA +import Fl4ProofsA64.P3.Stage3SigSchedValidA +import Fl4ProofsA64.P3.Stage3QKeepPresA +import Fl4ProofsA64.P3.Stage3McsFrA +import Fl4ProofsA64.P3.Stage3DelXSchedValidA +import Fl4ProofsA64.P3.Stage3ScNtfnA +import Fl4ProofsA64.P3.Stage3ExitAtA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open Fl4Spec Fl4SpecA64 Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Endp + +noncomputable section + +/-! ## `cancel_ipc` and the data of `SchedValidA` -/ + +/-- Module docstring. -/ +structure SvKA (σ σ' : State Unit) : Prop where + rq : σ'.readyQueues = σ.readyQueues + rls : σ'.releaseQueue = σ.releaseQueue + cur : σ'.curThread = σ.curThread + idle : σ'.idleThread = σ.idleThread + act : σ'.schedulerAction = .resumeCurThread → σ.schedulerAction = .resumeCurThread + tcb : ∀ p x, σ.kheap p = some (.tcb x) → ∃ y, σ'.kheap p = some (.tcb y) ∧ + y.tcbSchedContext = x.tcbSchedContext ∧ y.tcbPriority = x.tcbPriority ∧ y.tcbDomain = x.tcbDomain ∧ + (y.tcbState = x.tcbState ∨ BlockedTsA y.tcbState = false) ∧ (x.tcbFault = none → y.tcbFault = none) + tcbB : ∀ p y, σ'.kheap p = some (.tcb y) → ∃ x, σ.kheap p = some (.tcb x) + sc : ∀ p x n, σ.kheap p = some (.schedContext x n) → ∃ y, σ'.kheap p = some (.schedContext y n) ∧ + (scActive x = true → scActive y = true) ∧ y.scTcb = x.scTcb + /-- (item 11, integration round 2) The current SC and the consumed time are kept. -/ + csc : σ'.curSc = σ.curSc + cons : σ'.consumedTime = σ.consumedTime + +instance : PreO SvKA := + ⟨fun _ => ⟨rfl, rfl, rfl, rfl, id, fun _ x h => ⟨x, h, rfl, rfl, rfl, Or.inl rfl, id⟩, fun _ y h => ⟨y, h⟩, + fun _ x _ h => ⟨x, h, id, rfl⟩, rfl, rfl⟩, fun {a b c} h1 h2 => by + refine ⟨h2.rq.trans h1.rq, h2.rls.trans h1.rls, h2.cur.trans h1.cur, h2.idle.trans h1.idle, + fun e => h1.act (h2.act e), fun p x h => ?_, fun p z h => ?_, fun p x n h => ?_, h2.csc.trans h1.csc, h2.cons.trans h1.cons⟩ + · obtain ⟨y, hy, s1, p1, d1, t1, f1⟩ := h1.tcb p x h + obtain ⟨z, hz, s2, p2, d2, t2, f2⟩ := h2.tcb p y hy + refine ⟨z, hz, s2.trans s1, p2.trans p1, d2.trans d1, ?_, fun e => f2 (f1 e)⟩ + rcases t2 with t2 | t2 + · rcases t1 with t1 | t1 + · exact Or.inl (t2.trans t1) + · exact Or.inr (by rw [t2]; exact t1) + · exact Or.inr t2 + · obtain ⟨y, hy⟩ := h2.tcbB p z h + exact h1.tcbB p y hy + · obtain ⟨y, hy, a1, l1⟩ := h1.sc p x n h + obtain ⟨z, hz, a2, l2⟩ := h2.sc p y n hy + exact ⟨z, hz, fun e => a2 (a1 e), l2.trans l1⟩⟩ + +/-- Replacing one object by one `SvKA` accepts. -/ +theorem SvKA.upd {σ : State Unit} {p : ObjRef} {o o' : KernelObject} (hp : σ.kheap p = some o) + (ht : ∀ x, o = .tcb x → ∃ y, o' = .tcb y ∧ y.tcbSchedContext = x.tcbSchedContext ∧ + y.tcbPriority = x.tcbPriority ∧ y.tcbDomain = x.tcbDomain ∧ + (y.tcbState = x.tcbState ∨ BlockedTsA y.tcbState = false) ∧ (x.tcbFault = none → y.tcbFault = none)) + (htb : ∀ y, o' = .tcb y → ∃ x, o = .tcb x) + (hs : ∀ x n, o = .schedContext x n → ∃ y, o' = .schedContext y n ∧ (scActive x = true → scActive y = true) ∧ + y.scTcb = x.scTcb) : + SvKA σ { σ with kheap := Function.update σ.kheap p (some o') } := by + refine ⟨rfl, rfl, rfl, rfl, id, fun q x h => ?_, fun q y h => ?_, fun q x n h => ?_, rfl, rfl⟩ + · by_cases e : q = p + · subst e; rw [hp] at h + obtain ⟨y, rfl, r⟩ := ht x (Option.some.inj h) + exact ⟨y, Function.update_self _ _ _, r⟩ + · exact ⟨x, by show Function.update σ.kheap p _ q = _; rw [Function.update_of_ne e]; exact h, rfl, rfl, rfl, + Or.inl rfl, id⟩ + · change Function.update σ.kheap p (some o') q = _ at h + by_cases e : q = p + · subst e; rw [Function.update_self] at h + obtain ⟨x, rfl⟩ := htb y (Option.some.inj h) + exact ⟨x, hp⟩ + · rw [Function.update_of_ne e] at h; exact ⟨y, h⟩ + · by_cases e : q = p + · subst e; rw [hp] at h + obtain ⟨y, rfl, r⟩ := hs x n (Option.some.inj h) + exact ⟨y, Function.update_self _ _ _, r⟩ + · exact ⟨x, by show Function.update σ.kheap p _ q = _; rw [Function.update_of_ne e]; exact h, id, rfl⟩ + +theorem eq_svkA {α : Type} {m : NonDet (State Unit) α} (h : PresR (@Eq (State Unit)) m) : PresR SvKA m := + fun σ r hr => by rw [← h σ r hr]; exact PreO.refl σ + +theorem threadSet_svkA (f : Tcb → Tcb) (t : ObjRef) + (hf : ∀ x, (f x).tcbSchedContext = x.tcbSchedContext ∧ (f x).tcbPriority = x.tcbPriority ∧ + (f x).tcbDomain = x.tcbDomain ∧ ((f x).tcbState = x.tcbState ∨ BlockedTsA (f x).tcbState = false) ∧ + (x.tcbFault = none → (f x).tcbFault = none)) : + PresR SvKA (threadSet (Z := Unit) f t) := fun σ r hr => by + obtain ⟨x, hx, e⟩ := threadSet_memA f t hr + rw [e] + exact SvKA.upd hx (fun z hz => by cases hz; exact ⟨_, rfl, hf x⟩) (fun _ _ => ⟨x, rfl⟩) (fun _ _ h => by cases h) + +theorem setThreadState_svkA (t : ObjRef) (ts : ThreadState) (hts : BlockedTsA ts = false) : + PresR SvKA (setThreadState (Z := Unit) t ts) := by + unfold setThreadState + refine PresR.bind (threadSet_svkA _ t fun _ => ⟨rfl, rfl, rfl, Or.inr hts, id⟩) (fun _ σ r hr => ?_) + rw [setThreadStateAct_eq] at hr + have e : r = ((), stsAct σ t) := hr + rw [e] + show SvKA σ (stsAct σ t) + unfold stsAct + split_ifs + · exact ⟨rfl, rfl, rfl, rfl, (fun h => nomatch h), fun _ x h => ⟨x, h, rfl, rfl, rfl, Or.inl rfl, id⟩, + fun _ y h => ⟨y, h⟩, fun _ x _ h => ⟨x, h, id, rfl⟩, rfl, rfl⟩ + · exact PreO.refl _ + +theorem updateSchedContext_svkA (p : ObjRef) (f : SchedContext → SchedContext) + (hf : ∀ x, (scActive x = true → scActive (f x) = true) ∧ (f x).scTcb = x.scTcb) : PresR SvKA (updateSchedContext (Z := Unit) p f) := + fun σ r hr => by + obtain ⟨y, n, hy, e⟩ := updateSchedContext_memA p f hr + rw [e] + exact SvKA.upd hy (fun _ h => by cases h) (fun _ h => by cases h) + (fun x k h => by cases h; exact ⟨_, rfl, hf y⟩) + +theorem setSimpleKo_svkA {α : Type} [Nonempty α] (C : α → KernelObject) (p : ObjRef) (v : α) + (ht : ∀ a x, C a ≠ .tcb x) (hs : ∀ a x n, C a ≠ .schedContext x n) : + PresR SvKA (setSimpleKo (Z := Unit) C p v) := fun σ r hr => by + unfold setSimpleKo at hr + obtain ⟨o, ho, hr⟩ := eqPres_mem (getObject_eqPres p) hr + obtain ⟨_, -, hr⟩ := eqPres_mem (PresR.assert _) hr + obtain ⟨_, hpi, hr⟩ := eqPres_mem (PresR.assert _) hr + obtain ⟨ko, -, e⟩ := setObject_memA hr + have hgo : σ.kheap p = some o := by + have ho' : (o, σ) ∈ (NonDet.assertOpt (σ.kheap p) σ).1 := ho + cases hk : σ.kheap p with + | none => rw [hk] at ho'; exact absurd ho' (Set.notMem_empty _) + | some o' => + rw [hk] at ho' + have := congrArg Prod.fst (show (o, σ) = (o', σ) from ho') + exact congrArg some this.symm + have hn : ∃ a, C a = o := by + have h1 := hpi.1 + unfold partialInv at h1 + split_ifs at h1 with hu + · obtain ⟨a, ha, -⟩ := hu + exact ⟨a, ha⟩ + · exact absurd rfl h1 + obtain ⟨a, ea⟩ := hn + rw [e] + exact SvKA.upd hgo (fun x h => absurd (ea.trans h) (ht a x)) (fun y h => absurd h (ht _ y)) + (fun x n h => absurd (ea.trans h) (hs a x n)) + +theorem getSimpleKo_svkA {α : Type} [Nonempty α] (C : α → KernelObject) (p : ObjRef) : + PresR SvKA (getSimpleKo (Z := Unit) C p) := by + refine eq_svkA ?_ + unfold getSimpleKo + refine PresR.bind (getObject_eqPres p) (fun _ => ?_) + eqpres + +theorem getThreadState_svkA (t : ObjRef) : PresR SvKA (getThreadState (Z := Unit) t) := by + unfold getThreadState + exact eq_svkA (threadGet_eqPres _ t) + +theorem replyUnlinkTcb_svkA (t rp : ObjRef) : PresR SvKA (replyUnlinkTcb (Z := Unit) t rp) := by + unfold replyUnlinkTcb + refine PresR.bind (eq_svkA (getReply_eqPres rp)) (fun _ => ?_) + refine PresR.bind (eq_svkA (PresR.assert _)) (fun _ => ?_) + refine PresR.bind (getThreadState_svkA t) (fun _ => ?_) + refine PresR.bind (eq_svkA (PresR.assert _)) (fun _ => ?_) + refine PresR.bind ?_ (fun _ => setThreadState_svkA _ _ rfl) + unfold setReplyObjRef updateSkObjRef + exact PresR.bind (getSimpleKo_svkA _ _) (fun _ => + setSimpleKo_svkA _ _ _ (fun _ _ h => by cases h) (fun _ _ _ h => by cases h)) + +theorem tcbEpDequeue_svkA (t p : ObjRef) : PresR SvKA (tcbEpDequeue (Z := Unit) t p) := by + unfold tcbEpDequeue + refine PresR.bind (getSimpleKo_svkA _ _) (fun _ => ?_) + refine PresR.bind (eq_svkA (PresR.pure _)) (fun _ => ?_) + refine PresR.bind (eq_svkA (PresR.assert _)) (fun _ => ?_) + refine PresR.bind (eq_svkA (PresR.pure _)) (fun _ => ?_) + refine PresR.bind (eq_svkA (PresR.pure _)) (fun _ => ?_) + exact setSimpleKo_svkA _ _ _ (fun _ _ h => by cases h) (fun _ _ _ h => by cases h) + +theorem tcbNtfnDequeue_svkA (t p : ObjRef) : PresR SvKA (tcbNtfnDequeue (Z := Unit) t p) := by + unfold tcbNtfnDequeue + refine PresR.bind (getSimpleKo_svkA _ _) (fun _ => ?_) + refine PresR.bind (eq_svkA (PresR.pure _)) (fun _ => ?_) + refine PresR.bind (eq_svkA (PresR.assert _)) (fun _ => ?_) + refine PresR.bind (eq_svkA (PresR.pure _)) (fun _ => ?_) + refine PresR.bind (eq_svkA (PresR.pure _)) (fun _ => ?_) + exact setSimpleKo_svkA _ _ _ (fun _ _ h => by cases h) (fun _ _ _ h => by cases h) + +theorem replyRemoveTcb_svkA (t rp : ObjRef) : PresR SvKA (replyRemoveTcb (Z := Unit) t rp) := by + unfold replyRemoveTcb + refine PresR.bind (getThreadState_svkA t) (fun _ => ?_) + refine PresR.bind (eq_svkA (PresR.assert _)) (fun _ => ?_) + refine PresR.bind (eq_svkA (PresR.gets _)) (fun so => ?_) + cases so with + | none => exact replyUnlinkTcb_svkA _ _ + | some p => + dsimp only + refine PresR.bind (eq_svkA ?_) (fun _ => ?_) + · unfold NonDet.liftM + refine PresR.bind (getSchedContext_eqPres p) (fun _ => ?_) + eqpres + refine PresR.bind (eq_svkA ?_) (fun _ => ?_) + · unfold getReplyObjRef getSkObjRef getSimpleKo + refine PresR.bind (PresR.bind (getObject_eqPres _) (fun _ => by eqpres)) (fun _ => ?_) + eqpres + refine PresR.bind (eq_svkA (PresR.assert _)) (fun _ => ?_) + refine PresR.bind (updateSchedContext_svkA _ _ fun x => ⟨id, rfl⟩) (fun _ => ?_) + refine PresR.bind ?_ (fun _ => replyUnlinkTcb_svkA _ _) + unfold setReplyObjRef updateSkObjRef + exact PresR.bind (getSimpleKo_svkA _ _) (fun _ => + setSimpleKo_svkA _ _ _ (fun _ _ h => by cases h) (fun _ _ _ h => by cases h)) + +theorem blockedCancelIpc_svkA (t : ObjRef) (ts : ThreadState) (ro : Option ObjRef) : + PresR SvKA (blockedCancelIpc (Z := Unit) ts t ro) := by + unfold blockedCancelIpc getBlockingObject + refine PresR.bind (eq_svkA (PresR.assertOpt _)) (fun _ => ?_) + refine PresR.bind (tcbEpDequeue_svkA _ _) (fun _ => ?_) + cases ro with + | none => + show PresR SvKA (setThreadState t .inactive) + exact setThreadState_svkA _ _ rfl + | some r => + show PresR SvKA (replyUnlinkTcb t r >>= fun _ => setThreadState t .inactive) + exact PresR.bind (replyUnlinkTcb_svkA _ _) (fun _ => setThreadState_svkA _ _ rfl) + +theorem cancelIpc_svkA (t : ObjRef) : PresR SvKA (cancelIpc (Z := Unit) t) := by + unfold cancelIpc + refine PresR.bind (getThreadState_svkA t) (fun ts => ?_) + refine PresR.bind (threadSet_svkA _ t fun _ => ⟨rfl, rfl, rfl, Or.inl rfl, fun _ => rfl⟩) (fun _ => ?_) + cases ts <;> dsimp only <;> try exact PresR.pure _ + all_goals first + | exact blockedCancelIpc_svkA _ _ _ + | exact replyRemoveTcb_svkA _ _ + | exact PresR.bind (tcbNtfnDequeue_svkA _ _) (fun _ => setThreadState_svkA _ _ rfl) + +/-- (ruling 14:18Z item 3) **`cancel_ipc` clears the thread's fault** (`threadSet tcbFault := none` runs before the +`match`, `ipc.rs:242-245`), and the rest keeps it none (`SvKA.tcb`). -/ +theorem cancelIpc_faultNoneA {σ σ' : State Unit} (t : ObjRef) (hr : ((), σ') ∈ (cancelIpc (Z := Unit) t σ).1) : + ∀ y, σ'.kheap t = some (.tcb y) → y.tcbFault = none := by + unfold cancelIpc at hr + obtain ⟨ts, -, hr⟩ := eqPres_mem (by unfold getThreadState; exact threadGet_eqPres _ t) hr + obtain ⟨⟨⟨⟩, σ1⟩, h1, h2⟩ := mem_bindA.1 hr + obtain ⟨x, -, e1⟩ := threadSet_memA _ t h1 + have hk1 : σ1.kheap t = some (.tcb { x with tcbFault := none }) := by + rw [show σ1 = _ from e1]; exact Function.update_self _ _ _ + have fin : ∀ {m : NonDet (State Unit) Unit}, PresR SvKA m → ((), σ') ∈ (m σ1).1 → + ∀ y, σ'.kheap t = some (.tcb y) → y.tcbFault = none := fun hm hm1 y hy => by + obtain ⟨y', hy', -, -, -, -, hf⟩ := (hm σ1 _ hm1).tcb t _ hk1 + rw [hy] at hy'; cases hy'; exact hf rfl + cases ts <;> dsimp only at h2 + all_goals first + | exact fin (PresR.pure _) h2 + | exact fin (blockedCancelIpc_svkA _ _ _) h2 + | exact fin (replyRemoveTcb_svkA _ _) h2 + | exact fin (PresR.bind (tcbNtfnDequeue_svkA _ _) (fun _ => setThreadState_svkA _ _ rfl)) h2 + +theorem SchedValidA.svkA {σ σ' : State Unit} (h : SchedValidA σ) (k : SvKA σ σ') : SchedValidA σ' := by + have act : ∀ p, ScActiveAtA σ p → ScActiveAtA σ' p := fun p ⟨y, n, e, a⟩ => by + obtain ⟨z, hz, f, -⟩ := k.sc p y n e + exact ⟨z, n, hz, f a⟩ + have tc : ∀ t x sc, σ.kheap t = some (.tcb x) → x.tcbSchedContext = some sc → ScActiveAtA σ sc → + ∃ y sc', σ'.kheap t = some (.tcb y) ∧ y.tcbSchedContext = some sc' ∧ ScActiveAtA σ' sc' := + fun t x sc e es a => by + obtain ⟨y, hy, s, -⟩ := k.tcb t x e + exact ⟨y, sc, hy, s.trans es, act _ a⟩ + refine ⟨fun d p t hm => ?_, fun e1 e2 => ?_, fun t y sc e1 e2 e3 => ?_, fun t ht => ?_⟩ + · rw [k.rq] at hm + obtain ⟨⟨x, sc, e, es, a⟩, n⟩ := h.rq d p t hm + exact ⟨tc t x sc e es a, by rw [k.rls]; exact n⟩ + · rw [k.cur, k.idle] at e2 + obtain ⟨⟨x, sc, e, es, a⟩, n⟩ := h.act (k.act e1) e2 + rw [k.cur, k.rls] + exact ⟨tc _ x sc e es a, n⟩ + · obtain ⟨x, hx⟩ := k.tcbB t y e1 + obtain ⟨y', hy', s, -, -, st, -⟩ := k.tcb t x hx + rw [e1] at hy'; cases hy' + rcases st with st | st + · exact act _ (h.blk t x sc hx (st ▸ e2) (s ▸ e3)) + · rw [st] at e2; cases e2 + · rw [k.rls] at ht + obtain ⟨x, sc, e, es, a⟩ := h.rls t ht + exact tc t x sc e es a + +theorem RqDomPrio.svkA {σ σ' : State Unit} (h : RqDomPrio σ) (k : SvKA σ σ') : RqDomPrio σ' := by + intro f d p x hx hm + rw [k.rq] at hm + obtain ⟨y, hy⟩ := k.tcbB _ x hx + obtain ⟨x', hx', -, pr, dm, -⟩ := k.tcb _ y hy + rw [hx] at hx'; cases hx' + have := h f d p y hy hm + exact ⟨this.1.trans dm.symm, this.2.trans pr.symm⟩ + +/-! ## The tail: only SC refills change -/ + +/-- Module docstring. -/ +structure TailSA (σ σ' : State Unit) : Prop where + idle : σ'.idleThread = σ.idleThread + obj : ∀ p o, σ.kheap p = some o → (∀ y n, o ≠ .schedContext y n) → σ'.kheap p = some o + sc : ∀ p x n, σ.kheap p = some (.schedContext x n) → ∃ y, σ'.kheap p = some (.schedContext y n) ∧ + y.scReplies = x.scReplies ∧ scActive y = scActive x ∧ y.scTcb = x.scTcb + +instance : PreO TailSA := + ⟨fun _ => ⟨rfl, fun _ _ h _ => h, fun _ x _ h => ⟨x, h, rfl, rfl, rfl⟩⟩, fun {a b c} h1 h2 => by + refine ⟨h2.idle.trans h1.idle, fun p o h n => h2.obj p o (h1.obj p o h n) n, fun p x n h => ?_⟩ + obtain ⟨y, hy, r1, a1, l1⟩ := h1.sc p x n h + obtain ⟨z, hz, r2, a2, l2⟩ := h2.sc p y n hy + exact ⟨z, hz, r2.trans r1, a2.trans a1, l2.trans l1⟩⟩ + +theorem TailSA.of_kheap {σ σ' : State Unit} (hi : σ'.idleThread = σ.idleThread) (hk : σ'.kheap = σ.kheap) : + TailSA σ σ' := + ⟨hi, fun _ _ h _ => by rw [hk]; exact h, fun _ x _ h => ⟨x, by rw [hk]; exact h, rfl, rfl, rfl⟩⟩ + +theorem TailSA.sstepA {σ σ' : State Unit} (h : TailSA σ σ') : SStepA σ σ' := by + refine ⟨h.idle, fun p x e => ⟨x, h.obj p _ e (fun _ _ c => by cases c), ⟨rfl, rfl⟩, id⟩, fun p x n e => ?_⟩ + obtain ⟨y, hy, r, -, -⟩ := h.sc p x n e + exact ⟨y, hy, fun q hq => r ▸ hq⟩ + +theorem TailSA.blkOthA {σ σ' : State Unit} (t : ObjRef) (h : TailSA σ σ') : BlkOthA t σ σ' := + ⟨fun _ x _ e => ⟨x, h.obj _ _ e (fun _ _ c => by cases c), rfl⟩, + fun _ y _ _ e m => ⟨y, h.obj _ _ e (fun _ _ c => by cases c), m⟩, + fun _ y q _ _ e w m => ⟨y, q, h.obj _ _ e (fun _ _ c => by cases c), w, m⟩⟩ + +theorem eq_tailSA {α : Type} {m : NonDet (State Unit) α} (h : PresR (@Eq (State Unit)) m) : PresR TailSA m := + fun σ r hr => by rw [← h σ r hr]; exact PreO.refl σ + +theorem updateSchedContext_tailSA (p : ObjRef) (f : SchedContext → SchedContext) + (hf : ∀ x, (f x).scReplies = x.scReplies ∧ scActive (f x) = scActive x ∧ (f x).scTcb = x.scTcb) : + PresR TailSA (updateSchedContext (Z := Unit) p f) := fun σ r hr => by + obtain ⟨y, n, hy, e⟩ := updateSchedContext_memA p f hr + rw [e] + refine ⟨rfl, fun q o h ns => ?_, fun q x k h => ?_⟩ + · have c : q ≠ p := fun c => by rw [c, hy] at h; exact ns y n (Option.some.inj h).symm + show Function.update σ.kheap p _ q = _ + rw [Function.update_of_ne c]; exact h + · by_cases c : q = p + · subst c; rw [hy] at h; cases h + exact ⟨f y, Function.update_self _ _ _, hf y⟩ + · exact ⟨x, by show Function.update σ.kheap p _ q = _; rw [Function.update_of_ne c]; exact h, rfl, rfl, rfl⟩ + +theorem updateRefillHd_tailSA (p : ObjRef) (f : Refill → Refill) : + PresR TailSA (updateRefillHd (Z := Unit) p f) := by + unfold updateRefillHd + exact updateSchedContext_tailSA _ _ fun _ => ⟨rfl, rfl, rfl⟩ + +theorem mergeOverlappingRefills_tailSA (p : ObjRef) : PresR TailSA (mergeOverlappingRefills (Z := Unit) p) := by + unfold mergeOverlappingRefills refillPopHead + refine PresR.bind (PresR.bind (eq_tailSA ?_) (fun _ => PresR.bind (updateSchedContext_tailSA _ _ fun _ => ⟨rfl, rfl, rfl⟩) + (fun _ => PresR.pure _))) (fun _ => updateRefillHd_tailSA _ _) + unfold getRefillHead; exact PresR.getsThe _ + +theorem refillUnblockCheck_tailSA (p : ObjRef) : PresR TailSA (refillUnblockCheck (Z := Unit) p) := by + unfold refillUnblockCheck + refine PresR.bind (eq_tailSA ?_) (fun _ => PresR.bind (eq_tailSA (getScRefillReady_eqPres p)) (fun _ => ?_)) + · unfold isRoundRobin + exact PresR.bind (getSchedContext_eqPres p) (fun _ => PresR.pure _) + refine PresR.when ?_ + refine PresR.bind (PresR.gets _) (fun _ => PresR.bind (updateRefillHd_tailSA _ _) (fun _ => ?_)) + refine PresR.bind (PresR.modify _ (fun σ => TailSA.of_kheap rfl rfl)) (fun _ => ?_) + unfold refillHeadOverlappingLoop + exact PresR.whileLoop (fun _ => mergeOverlappingRefills_tailSA p) _ + +theorem ifCondRefillUnblockCheck_tailSA (o : Option ObjRef) (a b : Option Bool) : + PresR TailSA (ifCondRefillUnblockCheck (Z := Unit) o a b) := by + unfold ifCondRefillUnblockCheck + cases o with + | none => exact PresR.pure _ + | some p => + show PresR TailSA (_ >>= _) + refine PresR.bind (eq_tailSA (getSchedContext_eqPres p)) (fun _ => ?_) + refine PresR.bind (PresR.gets _) (fun _ => PresR.bind (PresR.pure _) (fun _ => ?_)) + exact PresR.when (PresR.when (refillUnblockCheck_tailSA p)) + +theorem tcbSchedAction_tailSA (f : ObjRef → List ObjRef → List ObjRef) (t : ObjRef) : + PresR TailSA (tcbSchedAction (Z := Unit) f t) := by + unfold tcbSchedAction + refine PresR.bind (eq_tailSA (threadGet_eqPres _ t)) (fun d => ?_) + refine PresR.bind (eq_tailSA (threadGet_eqPres _ t)) (fun p => ?_) + refine PresR.bind (eq_tailSA (by unfold getTcbQueue; eqpres)) (fun q => ?_) + refine PresR.bind (eq_tailSA (PresR.pure _)) (fun _ σ r hr => ?_) + have e : r = ((), _) := hr + rw [e] + exact TailSA.of_kheap rfl rfl + +theorem possibleSwitchTo_tailSA (t : ObjRef) : PresR TailSA (possibleSwitchTo (Z := Unit) t) := fun σ r hr => by + obtain ⟨q, -⟩ := possibleSwitchTo_memA t hr + rw [q.1] + exact TailSA.of_kheap rfl rfl + +theorem postpone_tailSA (p : ObjRef) : PresR TailSA (postpone (Z := Unit) p) := by + unfold postpone getScObjRef + refine PresR.bind (eq_tailSA (PresR.bind (getSchedContext_eqPres p) (fun _ => by eqpres))) (fun _ => ?_) + refine PresR.bind (eq_tailSA (PresR.assertOpt _)) (fun t => ?_) + refine PresR.bind (tcbSchedAction_tailSA _ t) (fun _ => ?_) + refine PresR.bind ?_ (fun _ => PresR.modify _ (fun σ => TailSA.of_kheap rfl rfl)) + unfold tcbReleaseEnqueue readyTimesAppend + refine PresR.bind (PresR.gets _) (fun _ => ?_) + refine PresR.bind (eq_tailSA ?_) (fun _ => ?_) + · intro σ r hr + exact (orderedInsert_spec _ _ _ _ hr).1.symm + refine PresR.bind (PresR.modify _ (fun σ => TailSA.of_kheap rfl rfl)) (fun _ => ?_) + exact PresR.when (PresR.modify _ (fun σ => TailSA.of_kheap rfl rfl)) + +theorem fl4GetScRefillSufficient_eqPresA (p : ObjRef) (u : Ticks) : + PresR (@Eq (State Unit)) (fl4GetScRefillSufficient (Z := Unit) p u) := by + unfold fl4GetScRefillSufficient; exact PresR.getsThe _ + +theorem fl4SchedContextResume_tailSA (p : ObjRef) : PresR TailSA (fl4SchedContextResume (Z := Unit) p) := by + unfold fl4SchedContextResume + refine PresR.bind (eq_tailSA (getSchedContext_eqPres p)) (fun _ => ?_) + refine PresR.bind (eq_tailSA (PresR.assertOpt _)) (fun t => ?_) + refine PresR.bind (PresR.gets _) (fun _ => PresR.when ?_) + refine PresR.bind (eq_tailSA (threadGet_eqPres _ t)) (fun _ => ?_) + refine PresR.bind (eq_tailSA (getScRefillReady_eqPres p)) (fun _ => ?_) + refine PresR.bind (eq_tailSA (fl4GetScRefillSufficient_eqPresA p 0)) (fun _ => PresR.when ?_) + refine PresR.bind (eq_tailSA (threadGet_eqPres _ t)) (fun d => ?_) + refine PresR.bind (eq_tailSA (threadGet_eqPres _ t)) (fun q => ?_) + refine PresR.bind (eq_tailSA (by unfold getTcbQueue; eqpres)) (fun _ => ?_) + exact PresR.bind (eq_tailSA (PresR.assert _)) (fun _ => postpone_tailSA p) + +theorem testPossibleSwitchTo_tailSA (t : ObjRef) : PresR TailSA (testPossibleSwitchTo (Z := Unit) t) := by + unfold testPossibleSwitchTo + exact PresR.bind (PresR.gets _) (fun _ => PresR.when (possibleSwitchTo_tailSA t)) + +/-- The tail of `fl4TcbRestart` after the `Restart` write. -/ +theorem restartTail_tailSA (t : ObjRef) : + PresR TailSA (do + let sc ← getTcbObjRef Tcb.tcbSchedContext t + ifSporadicCurScTestRefillUnblockCheck sc + NonDet.maybeM fl4SchedContextResume sc + testPossibleSwitchTo (Z := Unit) t) := by + refine PresR.bind (eq_tailSA (by unfold getTcbObjRef; exact threadGet_eqPres _ t)) (fun sc => ?_) + refine PresR.bind (ifCondRefillUnblockCheck_tailSA _ _ _) (fun _ => ?_) + refine PresR.bind ?_ (fun _ => testPossibleSwitchTo_tailSA t) + cases sc with + | none => exact PresR.pure _ + | some p => exact fl4SchedContextResume_tailSA p + +/-! ## The whole of `fl4TcbRestart` -/ + +/-- `fl4TcbRestart` of a stopped thread, as its three parts. -/ +theorem restart_stoppedEqA {σ : State Unit} {t : ObjRef} {x : Tcb} (hk : σ.kheap t = some (.tcb x)) + (hs : ¬ runnable x.tcbState = true ∧ ¬ idle x.tcbState) : + fl4TcbRestart (Z := Unit) t σ = (do + cancelIpc (Z := Unit) t + setThreadState t .restart + let sc ← getTcbObjRef Tcb.tcbSchedContext t + ifSporadicCurScTestRefillUnblockCheck sc + NonDet.maybeM fl4SchedContextResume sc + testPossibleSwitchTo t : SMonad Unit Unit) σ := by + unfold fl4TcbRestart + rw [nd_bind_oneSc (Notif.getThreadState_eq hk)] + unfold Fl4Spec.NonDet.when + rw [if_pos hs] + +/-- The parts of a run of `fl4TcbRestart` of a stopped thread. -/ +theorem restart_partsA {σ σ' : State Unit} {t : ObjRef} {x : Tcb} (hk : σ.kheap t = some (.tcb x)) + (hs : ¬ runnable x.tcbState = true ∧ ¬ idle x.tcbState) (hr : ((), σ') ∈ (fl4TcbRestart (Z := Unit) t σ).1) : + ∃ σ1 σ2, ((), σ1) ∈ (cancelIpc (Z := Unit) t σ).1 ∧ ((), σ2) ∈ (setThreadState (Z := Unit) t .restart σ1).1 ∧ + ((), σ') ∈ ((do + let sc ← getTcbObjRef Tcb.tcbSchedContext t + ifSporadicCurScTestRefillUnblockCheck sc + NonDet.maybeM fl4SchedContextResume sc + testPossibleSwitchTo (Z := Unit) t : SMonad Unit Unit) σ2).1 := by + rw [restart_stoppedEqA hk hs] at hr + obtain ⟨⟨⟨⟩, σ1⟩, h1, hr⟩ := mem_bindA.1 hr + obtain ⟨⟨⟨⟩, σ2⟩, h2, hr⟩ := mem_bindA.1 hr + exact ⟨σ1, σ2, h1, h2, hr⟩ + +theorem restart_sstepA {σ σ' : State Unit} {t : ObjRef} {x : Tcb} (hk : σ.kheap t = some (.tcb x)) + (hs : ¬ runnable x.tcbState = true ∧ ¬ idle x.tcbState) (hr : ((), σ') ∈ (fl4TcbRestart (Z := Unit) t σ).1) : + SStepA σ σ' := by + obtain ⟨σ1, σ2, h1, h2, h3⟩ := restart_partsA hk hs hr + exact PreO.trans (cancelIpc_sstepA t _ _ h1) + (PreO.trans (setThreadState_sstepA t _ _ _ h2) (restartTail_tailSA t _ _ h3).sstepA) + +theorem restart_blkOthA {σ σ' : State Unit} {t : ObjRef} {x : Tcb} (hk : σ.kheap t = some (.tcb x)) + (hs : ¬ runnable x.tcbState = true ∧ ¬ idle x.tcbState) + (hE : ∀ e', epBlocked x.tcbState = some e' → ∃ z, σ.kheap e' = some (.endpoint z) ∧ t ∈ z.epQueue) + (hN : ∀ n', x.tcbState = .blockedOnNotification n' → ∃ z, σ.kheap n' = some (.notification z)) + (hr : ((), σ') ∈ (fl4TcbRestart (Z := Unit) t σ).1) : BlkOthA t σ σ' := by + obtain ⟨σ1, σ2, h1, h2, h3⟩ := restart_partsA hk hs hr + exact PreO.trans (cancelIpc_blkOthA hk hE hN h1) + (PreO.trans (setThreadState_blkOthA t _ _ _ h2) ((restartTail_tailSA t _ _ h3).blkOthA t)) + +/-- Setting a thread that is not current or idle `Restart`. -/ +theorem setRestart_invA {σ σ' : State Unit} {m : McState} {n : Nat} {t : ObjRef} {x : Tcb} + (h : LoopInvA σ m n) (hk : σ.kheap t = some (.tcb x)) (htc : t ≠ σ.curThread) (hti : t ≠ σ.idleThread) + (hidles : ∀ c < n, m.idleThreadOf c ≠ t) (hfn : x.tcbFault = none) + (hr : ((), σ') ∈ (setThreadState (Z := Unit) t .restart σ).1) : + LoopInvA σ' m n ∧ σ' = stUpdA σ t x .restart := by + rw [setThreadState_eq _ hk] at hr + have e : σ' = stsAct (updK σ t (.tcb { x with tcbState := .restart })) t := congrArg Prod.snd hr + have e' : stsAct (updK σ t (.tcb { x with tcbState := .restart })) t = stUpdA σ t x .restart := by + unfold stsAct + rw [if_neg (fun c => htc c.1)] + rfl + rw [e'] at e + subst e + refine ⟨{ sinv := h.sinv.stateA hk hti (Or.inl rfl) (Or.inl rfl) (fun c => by cases c) (fun hf => absurd hfn hf) + sidle := h.sidle.stUpdA hk hidles + sact := { f1 := fun u c => h.sact.f1 u c + f2 := by + obtain ⟨z, hz, hz'⟩ := h.sact.f2 + exact ⟨z, (Function.update_of_ne htc.symm _ _).trans hz, hz'⟩ } }, rfl⟩ + +/-- **`sched_context_resume` (FL4's)** keeps the loop invariants. -/ +theorem fl4SchedContextResume_stepA {m : McState} {n : Nat} (p : ObjRef) {σ : State Unit} (h : LoopInvA σ m n) + (hz : ∀ y k t' x', σ.kheap p = some (.schedContext y k) → y.scTcb = some t' → σ.kheap t' = some (.tcb x') → + x'.tcbSchedContext = some σ.curSc → σ.consumedTime = 0) + {r : Unit × State Unit} (hr : r ∈ (fl4SchedContextResume (Z := Unit) p σ).1) : RStepA m n σ r.2 := by + unfold fl4SchedContextResume at hr + obtain ⟨sc, hsc, hr⟩ := eqPres_mem (getSchedContext_eqPres p) hr + obtain ⟨-, k, hk⟩ := getSchedContext_mem hsc + obtain ⟨tp, htp, hr⟩ := eqPres_mem (PresR.assertOpt sc.scTcb) hr + have etp : sc.scTcb = some tp := by + have : (tp, σ) ∈ (NonDet.assertOpt sc.scTcb σ).1 := htp + cases e : sc.scTcb with + | none => rw [e] at this; exact absurd this (Set.notMem_empty _) + | some a => + rw [e] at this + exact congrArg some (congrArg Prod.fst (show (tp, σ) = (a, σ) from this)).symm + obtain ⟨sched, hs, hr⟩ := eqPres_mem (PresR.gets _) hr + have es : sched = schedulable tp σ := congrArg Prod.fst (show (sched, σ) = (schedulable tp σ, σ) from hs) + have refl : r = ((), σ) → RStepA m n σ r.2 := fun e => by rw [e]; exact RStepA.refl h + unfold NonDet.when at hr + split_ifs at hr with hsch + · obtain ⟨ts, -, hr⟩ := eqPres_mem (threadGet_eqPres _ tp) hr + obtain ⟨ready, -, hr⟩ := eqPres_mem (getScRefillReady_eqPres p) hr + obtain ⟨suff, -, hr⟩ := eqPres_mem (fl4GetScRefillSufficient_eqPresA p 0) hr + split_ifs at hr with hc + · obtain ⟨d, -, hr⟩ := eqPres_mem (threadGet_eqPres _ tp) hr + obtain ⟨pr, -, hr⟩ := eqPres_mem (threadGet_eqPres _ tp) hr + obtain ⟨q, -, hr⟩ := eqPres_mem (by unfold getTcbQueue; eqpres : PresR (@Eq (State Unit)) + (getTcbQueue (Z := Unit) d pr)) hr + obtain ⟨-, -, hr⟩ := eqPres_mem (PresR.assert _) hr + refine postpone_stepA p h (fun y k' t' hy ht' => ?_) hr + rw [hk] at hy + cases hy + rw [etp] at ht' + cases ht' + obtain ⟨-, -, x1, hx1, hrun1⟩ := schedulable_actA (by rw [← es]; exact hsch) + exact ⟨x1, hx1, hrun1, fun e => hz _ k _ x1 hk etp hx1 e⟩ + · exact refl hr + · exact refl hr + +/-- **`SInvXA ∧ SIdleA` across `fl4TcbRestart` of a stopped thread** (module docstring). -/ +theorem restart_invA {σ σ' : State Unit} {m : McState} {n : Nat} {t : ObjRef} {x : Tcb} + (h : LoopInvA σ m n) (ns : NoSwA σ) (hk : σ.kheap t = some (.tcb x)) + (hs : ¬ runnable x.tcbState = true ∧ ¬ idle x.tcbState) + (hE : ∀ e', epBlocked x.tcbState = some e' → ∃ z, σ.kheap e' = some (.endpoint z) ∧ t ∈ z.epQueue) + (hN : ∀ n', x.tcbState = .blockedOnNotification n' → ∃ z, σ.kheap n' = some (.notification z)) + (hrep : RepOKA σ t) (htc : t ≠ σ.curThread) (hti : t ≠ σ.idleThread) (hidles : ∀ c < n, m.idleThreadOf c ≠ t) + (hlink : ∀ p, x.tcbSchedContext = some p → ∃ y k, σ.kheap p = some (.schedContext y k) ∧ y.scTcb = some t) + (hzc : x.tcbSchedContext = some σ.curSc → σ.consumedTime = 0) + (hr : ((), σ') ∈ (fl4TcbRestart (Z := Unit) t σ).1) : LoopInvA σ' m n ∧ Sched.SwA σ' := by + obtain ⟨σ1, σ2, h1, h2, h3⟩ := restart_partsA hk hs hr + obtain ⟨I1, N1⟩ := cancelIpc_invA h ns hk hE hN hrep h1 + have S1 := cancelIpc_svkA t _ _ h1 + obtain ⟨y, hy, -⟩ := S1.tcb _ _ hk + obtain ⟨I2, e2⟩ := setRestart_invA I1 hy (by rw [S1.cur]; exact htc) (by rw [S1.idle]; exact hti) hidles + (cancelIpc_faultNoneA t h1 y hy) h2 + have c2 : σ2.curThread = σ.curThread := by rw [e2]; exact S1.cur + have N2 : NoSwA σ2 := fun u => by rw [e2]; exact N1 u + have i2 : σ2.idleThread = σ.idleThread := by rw [e2]; exact S1.idle + have S2 : SvKA σ σ2 := PreO.trans S1 (setThreadState_svkA t .restart rfl _ _ h2) + obtain ⟨y2, hy2, ysc2, -⟩ := S2.tcb _ _ hk + obtain ⟨sc, hsc, h3⟩ := eqPres_mem (by unfold getTcbObjRef; exact threadGet_eqPres _ t) h3 + have esc : sc = x.tcbSchedContext := by + have e : getTcbObjRef (Z := Unit) Tcb.tcbSchedContext t σ2 = ({(y2.tcbSchedContext, σ2)}, False) := + threadGet_eqSc _ hy2 + rw [e] at hsc + exact (congrArg Prod.fst (show (sc, σ2) = (y2.tcbSchedContext, σ2) from hsc)).trans ysc2 + obtain ⟨⟨⟨⟩, σ3⟩, h4, h3⟩ := mem_bindA.1 h3 + have R3 : RefK σ2 σ3 := ifCondRefillUnblockCheck_refK _ _ _ _ _ h4 + have I3 := I2.refK R3 + have c3 : σ3.curThread = σ2.curThread := by rw [R3.1] + have i3 : σ3.idleThread = σ2.idleThread := by rw [R3.1] + obtain ⟨⟨⟨⟩, σ4⟩, h5, h3⟩ := mem_bindA.1 h3 + have R4 : RStepA m n σ3 σ4 := by + cases sc with + | none => + have e : ((), σ4) = ((), σ3) := h5 + cases e; exact RStepA.refl I3 + | some p => + refine fl4SchedContextResume_stepA p I3 (fun y' k' t' x' hy' ht' hx' hxs => ?_) h5 + obtain ⟨w, k, hw, l⟩ := hlink p esc.symm + obtain ⟨w2, hw2, -, l2⟩ := S2.sc p w k hw + obtain ⟨w3, hw3, g3⟩ := (R3.2.1 p).scF hw2 + rw [hw3] at hy' + cases hy' + have et : t' = t := Option.some.inj (ht'.symm.trans (g3.1.trans (l2.trans l))) + subst et + obtain ⟨xb, hxb, g⟩ := (R3.2.1 t').tcbB hx' + rw [hy2] at hxb + cases hxb + have e1 : x'.tcbSchedContext = x.tcbSchedContext := g.2.1.trans ysc2 + have hc3 : σ3.curSc = σ.curSc := by rw [R3.1]; exact S2.csc + rw [hc3, e1] at hxs + have hn3 : σ3.consumedTime = σ.consumedTime := by rw [R3.1]; exact S2.cons + rw [hn3] + exact hzc hxs + unfold testPossibleSwitchTo at h3 + obtain ⟨b, hb, h3⟩ := eqPres_mem (PresR.gets _) h3 + have eb : b = schedulable t σ4 := congrArg Prod.fst (show (b, σ4) = (schedulable t σ4, σ4) from hb) + unfold NonDet.when at h3 + split_ifs at h3 with hsch + · have hsc : schedulable t σ4 = true := by rw [← eb]; exact hsch + obtain ⟨z, hz⟩ := schedulable_tcbA hsc + have hz' : runnable z.tcbState = true := by + have h2t : σ2.kheap t = some (.tcb { y with tcbState := .restart }) := by + rw [e2]; exact Function.update_self _ _ _ + obtain ⟨w, hw, ew⟩ := (R3.2.1 t).tcbF h2t + obtain ⟨v, hv, ev⟩ := R4.st t w hw + rw [hz] at hv; cases hv + rw [ev, ew.1]; rfl + have c4 : σ4.curThread = σ.curThread := R4.cur.trans (c3.trans c2) + have i4 : σ4.idleThread = σ.idleThread := R4.idle.trans (i3.trans i2) + have N4 : NoSwA σ4 := R4.ns (R3.noSwA N2) + exact ⟨(R4.inv.possibleSwitch ⟨by rw [c4]; exact htc, hidles, z, hz, hz'⟩ (by rw [i4]; exact hti) h3).1, + Sched.possibleSwitchTo_swa t σ4 ⟨fun u e => absurd e (N4 u), hsc⟩ _ h3⟩ + · have e : ((), σ') = ((), σ4) := h3 + cases e; exact ⟨R4.inv, fun u e => absurd e (R4.ns (R3.noSwA N2) u)⟩ + +/-- **`sched_context_resume` (FL4's) keeps `FIA`** when its thread is not the current one and `t`'s bound SC is +active before it; `BoundActive t` is kept. -/ +theorem fl4SchedContextResume_fiaA (p t : ObjRef) {σ : State Unit} (h : FIA σ) (D : RqDomPrio σ) + (B : BoundActive t σ) (hne : ScThCurNe p σ) {r : Unit × State Unit} + (hr : r ∈ (fl4SchedContextResume (Z := Unit) p σ).1) : FIA r.2 ∧ BoundActive t r.2 := by + unfold fl4SchedContextResume at hr + obtain ⟨sc, hsc, hr⟩ := eqPres_mem (getSchedContext_eqPres p) hr + obtain ⟨-, k, hk⟩ := getSchedContext_mem hsc + obtain ⟨tp, htp, hr⟩ := eqPres_mem (PresR.assertOpt sc.scTcb) hr + have etp : sc.scTcb = some tp := by + have : (tp, σ) ∈ (NonDet.assertOpt sc.scTcb σ).1 := htp + cases e : sc.scTcb with + | none => rw [e] at this; exact absurd this (Set.notMem_empty _) + | some a => + rw [e] at this + exact congrArg some (congrArg Prod.fst (show (tp, σ) = (a, σ) from this)).symm + obtain ⟨sched, hs, hr⟩ := eqPres_mem (PresR.gets _) hr + have es : sched = schedulable tp σ := congrArg Prod.fst (show (sched, σ) = (schedulable tp σ, σ) from hs) + have refl : r = ((), σ) → FIA r.2 ∧ BoundActive t r.2 := fun e => by rw [e]; exact ⟨h, B⟩ + unfold NonDet.when at hr + split_ifs at hr with hsch + · obtain ⟨ts, -, hr⟩ := eqPres_mem (threadGet_eqPres _ tp) hr + obtain ⟨ready, -, hr⟩ := eqPres_mem (getScRefillReady_eqPres p) hr + obtain ⟨suff, -, hr⟩ := eqPres_mem (fl4GetScRefillSufficient_eqPresA p 0) hr + split_ifs at hr with hc + · obtain ⟨d, -, hr⟩ := eqPres_mem (threadGet_eqPres _ tp) hr + obtain ⟨pr, -, hr⟩ := eqPres_mem (threadGet_eqPres _ tp) hr + obtain ⟨q, -, hr⟩ := eqPres_mem (by unfold getTcbQueue; eqpres : PresR (@Eq (State Unit)) + (getTcbQueue (Z := Unit) d pr)) hr + obtain ⟨-, -, hr⟩ := eqPres_mem (PresR.assert _) hr + have ha := (schedulable_actA (by rw [← es]; exact hsch)).1 + refine postpone_fia p (BoundActive t) + (fun τ τ' hk' _ b x s e es' => ?_) σ ⟨h, D, B, fun y k' t' hy ht' => ?_, hne⟩ r hr + · rw [hk'] at e + obtain ⟨x0, s0, e0, es0, y0, k0, ey0, a0⟩ := b x s e es' + exact ⟨x0, s0, by rw [hk']; exact e0, es0, y0, k0, by rw [hk']; exact ey0, a0⟩ + · rw [hk] at hy; cases hy + rw [etp] at ht'; cases ht' + exact ha + · exact refl hr + · exact refl hr + +/-- **`SchedValidA` across `fl4TcbRestart` of a stopped thread** (module docstring): the thread's bound SC is active +and names it back. -/ +theorem restart_validA {σ σ' : State Unit} {t : ObjRef} {x : Tcb} (h : SchedValidA σ) (D : RqDomPrio σ) + (hk : σ.kheap t = some (.tcb x)) (hs : ¬ runnable x.tcbState = true ∧ ¬ idle x.tcbState) (htc : t ≠ σ.curThread) + (hact : ∀ p y k, x.tcbSchedContext = some p → σ.kheap p = some (.schedContext y k) → scActive y = true) + (hlink : ∀ p, x.tcbSchedContext = some p → ∃ y k, σ.kheap p = some (.schedContext y k) ∧ y.scTcb = some t) + (hr : ((), σ') ∈ (fl4TcbRestart (Z := Unit) t σ).1) : SchedValidA σ' := by + obtain ⟨σ1, σ2, h1, h2, h3⟩ := restart_partsA hk hs hr + have S2 : SvKA σ σ2 := PreO.trans (cancelIpc_svkA t _ _ h1) (setThreadState_svkA t .restart rfl _ _ h2) + have V2 := h.svkA S2 + have D2 : RqDomPrio σ2 := RqDomPrio.svkA D S2 + obtain ⟨y, hy, ysc, -⟩ := S2.tcb _ _ hk + obtain ⟨sc, hsc, h3⟩ := eqPres_mem (by unfold getTcbObjRef; exact threadGet_eqPres _ t) h3 + have esc : sc = x.tcbSchedContext := by + have e : getTcbObjRef (Z := Unit) Tcb.tcbSchedContext t σ2 = ({(y.tcbSchedContext, σ2)}, False) := + threadGet_eqSc _ hy + rw [e] at hsc + exact (congrArg Prod.fst (show (sc, σ2) = (y.tcbSchedContext, σ2) from hsc)).trans ysc + obtain ⟨⟨⟨⟩, σ3⟩, h4, h3⟩ := mem_bindA.1 h3 + have R3 : RefK σ2 σ3 := ifCondRefillUnblockCheck_refK _ _ _ _ _ h4 + have T3 : TailSA σ2 σ3 := ifCondRefillUnblockCheck_tailSA _ _ _ _ _ h4 + have V3 := V2.refKA R3 + have D3 : RqDomPrio σ3 := fun f d p z hz hm => by + rw [show σ3.readyQueues = σ2.readyQueues by rw [R3.1]] at hm + obtain ⟨w, hw, -⟩ := (R3.2.1 f).tcbB hz + have e := T3.obj f _ hw (fun _ _ c => by cases c) + rw [hz] at e; cases e + exact D2 f d p z hw hm + have link3 : ∀ p, x.tcbSchedContext = some p → ∃ w k, σ3.kheap p = some (.schedContext w k) ∧ + w.scTcb = some t ∧ scActive w = true := fun p ex => by + obtain ⟨w, k, hw, l⟩ := hlink p ex + obtain ⟨w2, hw2, a2, l2⟩ := S2.sc p w k hw + obtain ⟨w3, hw3, -, a3, l3⟩ := T3.sc p w2 k hw2 + exact ⟨w3, k, hw3, l3.trans (l2.trans l), a3.trans (a2 (hact p w k ex hw))⟩ + have B3 : BoundActive t σ3 := fun z p ez es => by + have e := T3.obj t _ hy (fun _ _ c => by cases c) + rw [ez] at e + have ez' : z = y := KernelObject.tcb.inj (Option.some.inj e) + subst ez' + obtain ⟨w, k, hw, -, a⟩ := link3 p (ysc ▸ es) + exact ⟨z, p, ez, es, w, k, hw, a⟩ + obtain ⟨⟨⟨⟩, σ4⟩, h5, h3⟩ := mem_bindA.1 h3 + have F4 : FIA σ4 ∧ BoundActive t σ4 := by + cases sc with + | none => + have e : ((), σ4) = ((), σ3) := h5 + cases e; exact ⟨Sched.FIA.ofValid V3, B3⟩ + | some p => + refine fl4SchedContextResume_fiaA p t (Sched.FIA.ofValid V3) D3 B3 (fun w k t' hw ht' => Or.inl ?_) h5 + obtain ⟨v, kv, hv, l, -⟩ := link3 p esc.symm + rw [hv] at hw; cases hw + rw [l] at ht' + rw [← Option.some.inj ht', show σ3.curThread = σ2.curThread by rw [R3.1], S2.cur] + exact htc.symm + unfold testPossibleSwitchTo at h3 + obtain ⟨b, -, h3⟩ := eqPres_mem (PresR.gets _) h3 + unfold NonDet.when at h3 + split_ifs at h3 + · exact (possibleSwitchTo_fia' t (fun _ => True) (fun _ _ h => h) (fun _ _ h => h) σ4 + ⟨F4.1, F4.2, trivial⟩ _ h3).1.toValid + · have e : ((), σ') = ((), σ4) := h3 + cases e; exact F4.1.toValid + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3TcbSuspFrA.lean b/proofs/Fl4ProofsA64/P3/Stage3TcbSuspFrA.lean new file mode 100644 index 000000000..39cfd0d56 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3TcbSuspFrA.lean @@ -0,0 +1,453 @@ +/- +P3 (AArch64, merge line, D8 step 3, TCB F0 rest): **the write footprint and spec frame of `sched.suspend`** +(`suspend_refinesA`, `P3/TcbSuspendA`, exports neither). + +* `SPresInst` instances for l4v's `update_restart_pc` and `suspend` (`SchedFrameA` is kept). +* `cancelReplyW_stepNA`: the cancellation's write set (`CancelReplyWA`) is `SchedStepNWA ∨ BufW Bf`. +* **`suspend_footA`**: `suspend_refinesA`'s chain (same premises), concluding the spec outcome, the write set + `SchedStepNWA N E R Z m σ s ∨ BufW Bf` from the entry state and `SchedFrameA σ σ'`. Use it with `suspend_refinesA` + through `WP.spec_and`; the spec outcomes agree (a singleton). + +Generated by `handoff/tools/ptu/gen_susp.py` from `suspend_refinesA`'s proof. No named premise beyond +`suspend_refinesA`'s. New module. +-/ +import Fl4ProofsA64.P3.TcbSuspendA +import Fl4ProofsA64.P3.Stage3TcbReadFrA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4SpecA64 Fl4Spec Sched + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +instance (t : ObjRef) : SPresInst (updateRestartPc (Z := Unit) t) := ⟨by unfold updateRestartPc; spres⟩ +instance (t : ObjRef) : SPresInst (Fl4SpecA64.suspend (Z := Unit) t) := ⟨by unfold Fl4SpecA64.suspend; spres⟩ + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + +/-- **The cancellation's write set is a Stage-3 write set or a buffer word.** -/ +theorem cancelReplyW_stepNA {s : S} {σ : State Unit} (H : SchedSRN st Z m κ s σ) (hP : IPlacedA N Z.T E R Bf) + {b : U64} (h : CancelReplyWA N Z.T Z.C E R Bf (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) b) : + SchedStepNWA (st := st) N E R Z m σ s b ∨ BufW Bf b := by + have PE := hP.e + rcases h with h | ⟨q, hq, h⟩ | ⟨sc, hc, h⟩ + · exact cancelWA_stepNA H.core.tcbs (SchedSRN.act_stepNA H) h + · have PEq := replyNextPlaceA (Z := Z) PE hq + have PEp := replyPrevPlaceA (Z := Z) PE hq + refine Or.inl (Or.inr (Or.inr (Or.inr ⟨q, hq, ?_, ?_⟩))) <;> rcases h with rfl | rfl <;> omega + · have hfoot : ScFootA Z.C m σ b := by + obtain ⟨y, k, hk, hx⟩ := H.scr.sc sc hc + refine ⟨sc, hc, y, k, hk, ?_⟩ + have h32 := hx.hdr (K := 32#u64) (by simp) (by simp) + have ee : scReplyA sc = scWA sc 32#u64 := by unfold scReplyA scWA layout.sc.REPLY; rfl + rw [h, ee] + exact hx.in_obj h32.2 + exact Or.inl (Or.inl (Or.inl (Or.inl hfoot))) + +set_option maxHeartbeats 4000000 in +/-- **`sched.suspend`'s write footprint and spec frame** (module docstring). -/ +theorem suspend_footA (hP : IPlacedA N Z.T E R Bf) (P : IpcSchedPlacementA Z.th N E R Bf) + (hc : m.curCore < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (s : S) (a : A) (σ : State Unit) {stack : U64 → List U64} + (H : IpcSchedRelA st Z m κ s σ) (RN : NtfnStateRel N Z.T (L.mem s) σ) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + (M : McpRelA Z.T m (L.mem s) σ) (Y : YieldRelA Z.T Z.C m (L.mem s) σ) (hC0 : ∀ c ∈ Z.C, c ≠ 0#u64) + {t : U64} (ht : t ∈ Z.T) {x : Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hReply : ∀ r', x.tcbState = .blockedOnReply r' → ∃ r ∈ R, specPtr r = r' ∧ + ∃ xr, σ.kheap r' = some (.reply xr) ∧ xr.replyTcb = some (specPtr t) ∧ ScReplyCovA Z.C m σ r') + (hE : ∀ e', epBlocked x.tcbState = some e' → L.mem s (tBlockA t) ∈ E ∧ + ∃ z, σ.kheap e' = some (.endpoint z) ∧ specPtr t ∈ z.epQueue) + (hN : ∀ n', x.tcbState = .blockedOnNotification n' → L.mem s (tBlockA t) ∈ N ∧ + ∃ z, σ.kheap n' = some (.notification z) ∧ specPtr t ∈ z.ntfnObj.ntfnQueue) + (hrep : RepOKA σ (specPtr t)) + (hrun0 : L.mem s (runVcpuA t) = 0#u64) (hrunS : x.tcbExt.tcbRunVcpu = none) + (hon : L.mem s (affA t) = L.cpu s) : + sched.suspend st ar s a t ⦃ out => ∃ σ', + (fl4CancelRun (Z := Unit) (specPtr t) >>= fun _ => Fl4SpecA64.suspend (specPtr t)) σ = + ({((), σ')}, False) ∧ + WritesIn st s out.1 (fun b => SchedStepNWA (st := st) N E R Z m σ s b ∨ BufW Bf b) ∧ + SchedFrameA σ σ' ⦄ := by + have ops := ipcSchedOpsA (st := st) (ar := ar) (κ := κ) hP P hc hmb hmax htu + have hSched : CancelScheduleA st N Z.T (IpcSchedRelA st Z m κ) := cancelScheduleA_of_placement P + have PE := hP.e + have h1 := cancel_run_quietA (st := st) (ar := ar) s a t hrun0 + have s1 := fl4CancelRun_quietA hk hrunS + have hsusp : (fl4CancelRun (Z := Unit) (specPtr t) >>= fun _ => Fl4SpecA64.suspend (specPtr t)) σ = + Fl4SpecA64.suspend (specPtr t) σ := nd_bind_oneSc s1 _ + have hprog : sched.suspend st ar s a t = (do + let r ← ipc.cancel_ipc st ar s a t + let s3 ← suspendPcProgA st r.1 t + let s4 ← sched.dequeue st s3 t + let s5 ← sched.release_remove st s4 t + let s6 ← schedcontext.cancel_yield_to st s5 t + let s7 ← sched.set_thread_state st s6 t sched.TS_INACTIVE + ok (s7, r.2)) := by + unfold sched.suspend suspendPcProgA + rw [h1] + simp only [bind_ok, Std.bind_assoc] + rfl + rw [hprog] + -- cancel_ipc + refine WP.spec_bind (suspendCancelA ops.1 hSched ops.2.2.1 hP P s a σ H RN HE HS ht hk hReply hE hN hrep hC0) ?_ + rintro r ⟨ha, σ1, stk1, hc1, RN1, H1, HE1, HS1, w1, K1, x1, hk1, hfree⟩ + obtain ⟨Q0, R0, H0⟩ := id H + have hoff : ∀ u ∈ Z.T, ∀ K : U64, (K = 3440#u64 ∨ K = 3472#u64 ∨ K = 3520#u64) → + L.mem r.1 (wa u K) = L.mem s (wa u K) := by + intro u hu K hK + refine w1.mem_eq (field_aligned (H0.core.tcbs u hu) ?_ ?_) (suspendOffTcbA H0.toSchedSRN hP hu hK) + · rcases hK with rfl | rfl | rfl <;> simp + · rcases hK with rfl | rfl | rfl <;> simp + have hoffF : ∀ u ∈ Z.T, L.mem r.1 (flagsA u) = L.mem s (flagsA u) := fun u hu => + w1.mem_eq (field_aligned (H0.core.tcbs u hu) (K := 3456#u64) (by decide) (by decide)) + (suspendOffFlagsA H0.toSchedSRN hP hu) + -- FAULT_IP := NEXT_IP + refine WP.spec_bind (suspendRestartPcA hP P RN1 H1 HE1 HS1 ht hk1) ?_ + rintro s3 ⟨σ3, hpc, Hpc⟩ + obtain ⟨x3, hk3, hx3⟩ := Hpc.tcb + have hst3 : x3.tcbState = x1.tcbState := by rw [hx3] + have w13 : WritesIn st s s3 (fun b => + CancelReplyWA N Z.T Z.C E R Bf (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) b ∨ + b = regA t layout.reg.FAULT_IP) := w1.trans Hpc.wr + have hrv : (regA t layout.reg.FAULT_IP).val = t.val + 528 := by + rw [← regIdxA_faultIP, regA_val (PE.tcb t ht) (regIdxA_lt _)] + rfl + have hoff3 : ∀ u ∈ Z.T, ∀ K : U64, (K = 3440#u64 ∨ K = 3472#u64 ∨ K = 3520#u64) → + L.mem s3 (wa u K) = L.mem s (wa u K) := by + intro u hu K hK + have hua := H0.core.tcbs u hu + have hKv : K.val = 3440 ∨ K.val = 3472 ∨ K.val = 3520 := by rcases hK with rfl | rfl | rfl <;> simp + have hv : (wa u K).val = u.val + K.val := Notif.tfield_val hua (by omega) + rw [← hoff u hu K hK] + refine Hpc.wr.mem_eq (field_aligned hua (by omega) (by rcases hK with rfl | rfl | rfl <;> simp)) (fun e => ?_) + have e' := congrArg UScalar.val e + rw [hv, hrv] at e' + by_cases h : u = t + · subst u; omega + · have := tcb_disj PE ht hu h; omega + have hoff3F : ∀ u ∈ Z.T, L.mem s3 (flagsA u) = L.mem s (flagsA u) := fun u hu => by + have hua := H0.core.tcbs u hu + have hv : (flagsA u).val = u.val + 3456 := by + rw [show flagsA u = wa u 3456#u64 from rfl, Notif.tfield_val hua (by decide)]; rfl + rw [← hoffF u hu] + refine Hpc.wr.mem_eq (field_aligned hua (K := 3456#u64) (by decide) (by decide)) (fun e => ?_) + have e' := congrArg UScalar.val e + rw [hv, hrv] at e' + by_cases h : u = t + · subst u; omega + · have := tcb_disj PE ht hu h; omega + -- MCP and yield links + have htcb3 : ∀ u ∈ Z.T, ∀ y, σ.kheap (specPtr u) = some (.tcb y) → + ∃ y', σ3.kheap (specPtr u) = some (.tcb y') ∧ y'.tcbMcpriority = y.tcbMcpriority ∧ + y'.tcbYieldTo = y.tcbYieldTo ∧ y'.tcbFlags = y.tcbFlags := by + intro u _ y hy + obtain ⟨y1, hy1, e1⟩ := K1.tcb _ _ hy + by_cases e : specPtr u = specPtr t + · rw [e, hk1] at hy1 + cases hy1 + exact ⟨x3, by rw [e]; exact hk3, by rw [hx3, e1], by rw [hx3, e1], by rw [hx3, e1]⟩ + · exact ⟨y1, by rw [Hpc.far _ e]; exact hy1, by rw [e1], by rw [e1], by rw [e1]⟩ + have hsc3 : ∀ c ∈ Z.C, ∃ y n y', σ.kheap (scNameA m c) = some (.schedContext y n) ∧ + σ3.kheap (scNameA m c) = some (.schedContext y' n) ∧ y'.scYieldFrom = y.scYieldFrom := by + intro c hc' + obtain ⟨y, n, hy, -⟩ := H0.scr.sc c hc' + obtain ⟨y', hy', e⟩ := K1.sc _ _ _ hy + refine ⟨y, n, y', hy, ?_, by rw [e]⟩ + rw [Hpc.far _ (fun h => by rw [h, hk1] at hy'; cases hy')] + exact hy' + obtain ⟨M3, Y3⟩ := yieldFrameA (mem' := L.mem s3) (σ' := σ3) M Y + (fun u hu => ⟨hoff3 u hu 3440#u64 (Or.inl rfl), hoff3F u hu⟩) + (fun u hu => hoff3 u hu 3472#u64 (Or.inr (Or.inl rfl))) + htcb3 hsc3 + have hon3 : L.mem s3 (affA t) = L.cpu s3 := by + rw [w13.2.1, ← hon] + exact hoff3 t ht 3520#u64 (Or.inr (Or.inr rfl)) + -- dequeue + obtain ⟨Q, RR, H3⟩ := Hpc.sched + have ht' : t ∈ ({ Z with Q := Q, R := RR } : XSetsA).T := ht + refine WP.spec_bind (dequeue_refines_nodeA H3.toSchedSRN ht' hon3) ?_ + rintro s4 ⟨σ4, hdq, hk4h, hrel4, hfix4, -, -, H4N, F4⟩ + have H4 : SchedSRA st { Z with Q := deqQA (st := st) s3 t Q, R := RR } m κ s4 σ4 := + ⟨H4N, RdA.of_eq H3.rd hk4h hfix4.curTime hrel4⟩ + have hready : ∀ d p, specPtr t ∉ σ4.readyQueues d p := H3.toSchedSRN.ready_after_dequeueA ht' H4N + have hms4 : σ4.machineState = σ3.machineState := by + obtain ⟨y, hy, hd, -, -⟩ := H3.rest.prio t ht + have h := (tcbSchedAction_eqA hy hd Fl4SpecA64.tcbSchedDequeue).symm.trans hdq + have e := congrArg (fun p : Unit × State Unit => p.2.machineState) + (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst h)) + exact e.symm + -- frames across a scheduler step + have frame : ∀ (s0 s9 : S) (σ0 σ9 : State Unit) (Q9 : U64 → U64 → List U64) (R9 : U64 → List U64) + (H9 : SchedSRN st { Z with Q := Q9, R := R9 } m κ s0 σ0), + WritesIn st s0 s9 (MWrA (st := st) { Z with Q := Q9, R := R9 } m σ0 s0) → + σ9.kheap = σ0.kheap → σ9.machineState = σ0.machineState → + NtfnStateRel N Z.T (L.mem s0) σ0 → EpStateRel Z.T E R Bf (L.mem s0) σ0 → + ReplyStackRelA (L.mem s0) R Z.C σ0 (scNameA m) stk1 → StEqA Z.T (L.mem s0) σ0 → + NtfnStateRel N Z.T (L.mem s9) σ9 ∧ EpStateRel Z.T E R Bf (L.mem s9) σ9 ∧ + ReplyStackRelA (L.mem s9) R Z.C σ9 (scNameA m) stk1 ∧ StEqA Z.T (L.mem s9) σ9 ∧ + (∀ u ∈ Z.T, ∀ K : U64, (K = 3440#u64 ∨ K = 3472#u64 ∨ K = 3520#u64) → + L.mem s9 (wa u K) = L.mem s0 (wa u K)) ∧ + (∀ u ∈ Z.T, L.mem s9 (flagsA u) = L.mem s0 (flagsA u)) := by + intro s0 s9 σ0 σ9 Q9 R9 H9 w hkh hms RN0 E0 S0 T0 + have KV : Endp.KView σ0 σ9 := ⟨fun _ _ h => by rw [hkh]; exact h, fun _ z h => ⟨z, by rw [hkh]; exact h, rfl⟩, + fun _ z h => ⟨z, by rw [hkh]; exact h, rfl, rfl, rfl, rfl, rfl⟩, by rw [hms]⟩ + have off : ∀ u ∈ Z.T, ∀ K : U64, (K = 3328#u64 ∨ K = 3440#u64 ∨ K = 3472#u64) → + L.mem s9 (wa u K) = L.mem s0 (wa u K) := by + intro u hu K hK + refine w.mem_eq (field_aligned (H9.core.tcbs u hu) ?_ ?_) (yieldTcbNotMWrA H9 hu hK) + · rcases hK with rfl | rfl | rfl <;> simp + · rcases hK with rfl | rfl | rfl <;> simp + refine ⟨RN0.frame hP.n (fun b hb hf => w.mem_eq hb fun h => notifFootOffMWrA H9 P.notification hf h) + ⟨fun _ _ h => by rw [hkh]; exact h, fun _ z h => ⟨z, by rw [hkh]; exact h, rfl, rfl, rfl⟩⟩, + E0.frame PE (epFrameOfMcsA H9 P (σ := σ0) (σ' := σ0) ⟨w, SKeepA.refl _, SSzA.refl _⟩ rfl).1 KV, + S0.frame (fun d hd => ?_) (fun c _ y k hy => ⟨y, k, by rw [hkh]; exact hy, rfl⟩) + (fun d _ y hy => ⟨y, by rw [hkh] at hy; exact hy, rfl⟩), fun u hu y hy => ?_, fun u hu K hK => ?_, + fun u hu => w.mem_eq (field_aligned (H9.core.tcbs u hu) (K := 3456#u64) (by decide) (by decide)) + (yieldFlagsNotMWrA H9 hu)⟩ + · have n := replyNextPlaceA (Z := Z) PE hd + have p := replyPrevPlaceA (Z := Z) PE hd + exact ⟨w.mem_eq n.1 (replyBlockOffMWrA H9 P hd (by omega) (by omega)), + w.mem_eq p.1 (replyBlockOffMWrA H9 P hd (by omega) (by omega))⟩ + · rw [hkh] at hy + rw [stA_eq, off u hu 3328#u64 (Or.inl rfl), ← stA_eq] + exact T0 u hu y hy + · rcases hK with h | h | h + · exact off u hu K (Or.inr (Or.inl h)) + · exact off u hu K (Or.inr (Or.inr h)) + · subst h + have hua := H9.core.tcbs u hu + have hv : (wa u 3520#u64).val = u.val + 3520 := by rw [Notif.tfield_val hua] <;> simp + refine w.mem_eq (field_aligned hua (by simp) (by simp)) (fun h => ?_) + have hty := (H9.thT u).2 hu + have hK' : (wa u 3520#u64).val < (L.ksb s0).val ∨ (L.ksb s0).val + ksSizeA ≤ (wa u 3520#u64).val := by + have h := H9.place.ks u _ hty (fun irq => by + have := H9.place.irqK hty irq + cases this) + change u.val + 4096 ≤ (L.ksb s0).val ∨ _ at h + omega + have hS : ¬ ScFootA Z.C m σ0 (wa u 3520#u64) := by + intro hf + have hd := H9.scp.th_out hf hty + change (wa u 3520#u64).val < u.val ∨ u.val + 4096 ≤ (wa u 3520#u64).val at hd + omega + refine not_mwrA hS (fun v hv' => ?_) hK' h + have hva := H9.core.tcbs v hv' + refine ⟨field_ne hua hva (by decide) (by decide) (fun _ => by decide), + field_ne hua hva (by decide) (by decide) (fun _ => by decide), + field_ne hua hva (by decide) (by decide) (fun _ => by decide), + field_ne hua hva (by decide) (by decide) (fun _ => by decide)⟩ + have T3 : StEqA Z.T (L.mem s3) σ3 := fun u hu y hy => (Hpc.ep.at_tcb hu hy).st + obtain ⟨RN4, E4, S4, T4, off4, offF4⟩ := frame s3 s4 σ3 σ4 Q RR H3.toSchedSRN F4.wr hk4h hms4 Hpc.ntfn Hpc.ep + Hpc.stk T3 + -- release_remove + have hon4 : L.mem s4 (affA t) = L.cpu s4 := by + rw [F4.wr.2.1, ← hon3] + exact off4 t ht 3520#u64 (Or.inr (Or.inr rfl)) + refine WP.spec_bind (release_remove_refines_localA H4 (t := t) ht hon4) ?_ + rintro s5 ⟨R5, -, H5, F5⟩ + obtain ⟨σ5, hσ5⟩ : ∃ σ5, σ5 = releaseRemoveStA σ4 (specPtr t) := ⟨_, rfl⟩ + rw [← hσ5] at H5 F5 + have hk5h : σ5.kheap = σ4.kheap := by rw [hσ5]; rfl + have hms5 : σ5.machineState = σ4.machineState := by rw [hσ5]; rfl + have hrq5 : σ5.readyQueues = σ4.readyQueues := by rw [hσ5]; rfl + have hnr5 : specPtr t ∉ σ5.releaseQueue := by rw [hσ5]; exact releaseRemoveStA_notMem _ _ + obtain ⟨RN5, E5, S5, T5, off5, offF5⟩ := frame s4 s5 σ4 σ5 _ RR H4N F5.wr hk5h hms5 RN4 E4 S4 T4 + have hmy : ∀ u ∈ Z.T, ∀ K : U64, (K = 3440#u64 ∨ K = 3472#u64) → L.mem s5 (wa u K) = L.mem s3 (wa u K) := + fun u hu K hK => by + have hK' : K = 3440#u64 ∨ K = 3472#u64 ∨ K = 3520#u64 := hK.imp id Or.inl + rw [off5 u hu K hK', off4 u hu K hK'] + have hk53 : σ5.kheap = σ3.kheap := hk5h.trans hk4h + obtain ⟨M5, Y5⟩ := yieldFrameA (mem' := L.mem s5) (σ' := σ5) M3 Y3 + (fun u hu => ⟨hmy u hu 3440#u64 (Or.inl rfl), by rw [offF5 u hu, offF4 u hu]⟩) + (fun u hu => hmy u hu 3472#u64 (Or.inr rfl)) + (fun u _ y hy => ⟨y, by rw [hk53]; exact hy, rfl, rfl, rfl⟩) + (fun c hc' => by + obtain ⟨y, n, hy, -⟩ := H3.scr.sc c hc' + exact ⟨y, n, y, hy, by rw [hk53]; exact hy, rfl⟩) + -- cancel_yield_to + refine WP.spec_bind (cancel_yield_to_refinesB H5 T5 M5 Y5 hC0 (t := t) ht) ?_ + rintro s6 ⟨σ6, hy6, H6, T6, M6, Y6, z56, -, hrel6, hrq6, -, htcb6, hobj6, w6, hms6, hsc6⟩ + have hk5 : σ5.kheap (specPtr t) = some (.tcb x3) := by rw [hk53]; exact hk3 + obtain ⟨x6, hk6, hx6⟩ := htcb6 _ _ hk5 + -- the IPC relations across the yield-link writes + have hyv : (yieldA t).val = t.val + 3472 := by + change (wa t 3472#u64).val = _ + rw [Notif.tfield_val (PE.tcb t ht)] <;> simp + have tk : ∀ q z, σ5.kheap q = some (.tcb z) → ∃ z', σ6.kheap q = some (.tcb z') ∧ + z' = { z with tcbYieldTo := z'.tcbYieldTo } := htcb6 + have RN6 : NtfnStateRel N Z.T (L.mem s6) σ6 := by + refine RN5.frame hP.n (fun b hb hf => w6.mem_eq hb fun h => ?_) ⟨fun q z h => ?_, fun q z h => ?_⟩ + · rcases h with h | h + · exact notifFootOffMWrA H5.toSchedSRN P.notification hf h + · rw [h] at hf + rcases hf with ⟨n, hn, g1, g2⟩ | ⟨u, hu, hf⟩ + · have := hP.n.disj n hn t ht + omega + · have hua := PE.tcb u hu + have hval : (yieldA t).val = u.val + 3328 ∨ (yieldA t).val = u.val + 3336 ∨ + (yieldA t).val = u.val + 256 ∨ (yieldA t).val = u.val + 3448 ∨ + (yieldA t).val = u.val + 3496 ∨ (yieldA t).val = u.val + 3504 := by + rcases hf with h | h | h | h | h | h <;> + rw [h] <;> + (try simp only [stA_eq, Notif.tBlockA_eq, Notif.badgeRegA_eq, Notif.prioA_eq', Notif.nextEp_eq, + Notif.prevEp_eq]) <;> + rw [Notif.tfield_val hua] <;> simp + by_cases e : u = t + · subst u; omega + · have := tcb_disj PE ht hu e; omega + · exact hobj6 q _ h (fun _ _ e => by cases e) (fun _ e => by cases e) + · obtain ⟨z', hz', e⟩ := tk q z h + exact ⟨z', hz', by rw [e], by rw [e]; rfl, by rw [e]⟩ + have E6 : EpStateRel Z.T E R Bf (L.mem s6) σ6 := by + refine E5.frame PE (fun b hb hf => w6.mem_eq hb fun h => ?_) + ⟨fun q z h => ?_, fun q z h => ⟨z, ?_, rfl⟩, fun q z h => ?_, by rw [hms6]⟩ + · rcases h with h | h + · exact endpFootOffWrite H5.toSchedSRN P hf h + · rw [h] at hf + rcases hf with ⟨ep, hep, g⟩ | ⟨r', hr', g⟩ | ⟨u, hu, g | g⟩ | g + · have := PE.et ep hep t ht + omega + · have := PE.rt r' hr' t ht + omega + · obtain ⟨g1, g2⟩ := g + unfold TOffN at g2 + by_cases e : u = t + · subst u; omega + · have := tcb_disj PE ht hu e; omega + · unfold TL at g + by_cases e : u = t + · subst u; omega + · have := tcb_disj PE ht hu e; omega + · have := PE.bt _ g t ht + omega + · exact hobj6 q _ h (fun _ _ e => by cases e) (fun _ e => by cases e) + · exact hobj6 q _ h (fun _ _ e => by cases e) (fun _ e => by cases e) + · obtain ⟨z', hz', e⟩ := tk q z h + exact ⟨z', hz', by rw [e], by rw [e], by rw [e], by rw [e], by rw [e]⟩ + have S6 : ReplyStackRelA (L.mem s6) R Z.C σ6 (scNameA m) stk1 := by + refine S5.frame (fun d hd => ?_) (fun c _ y n hy => ?_) (fun d hd y hy => ?_) + · have nx := replyNextPlaceA (Z := Z) PE hd + have pv := replyPrevPlaceA (Z := Z) PE hd + have off : ∀ b : U64, d.val ≤ b.val → b.val < d.val + 32 → b ≠ yieldA t := by + intro b g1 g2 e + have := PE.rt d hd t ht + rw [e, hyv] at g1 g2 + omega + exact ⟨w6.mem_eq nx.1 (fun h => h.elim (replyBlockOffMWrA H5.toSchedSRN P hd (by omega) (by omega)) + (off _ (by omega) (by omega))), + w6.mem_eq pv.1 (fun h => h.elim (replyBlockOffMWrA H5.toSchedSRN P hd (by omega) (by omega)) + (off _ (by omega) (by omega)))⟩ + · obtain ⟨y', hy', e⟩ := hsc6 _ _ _ hy + exact ⟨y', n, hy', by rw [e]⟩ + · obtain ⟨z, hz, -⟩ := E5.reply d hd + have h6 := hobj6 _ _ hz (fun _ _ e => by cases e) (fun _ e => by cases e) + rw [h6] at hy + cases hy + exact ⟨_, hz, rfl⟩ + -- set Inactive + have hnq : (∀ d p, specPtr t ∉ σ6.readyQueues d p) ∧ specPtr t ∉ σ6.releaseQueue := by + rw [hrq6, hrq5, hrel6] + exact ⟨hready, hnr5⟩ + have hfree6 : IpcFreeA x6.tcbState := by + rw [hx6] + change IpcFreeA x3.tcbState + rw [hst3] + exact hfree + refine WP.spec_bind (WP.spec_and (set_thread_state_inactiveExA H6 T6 ht hk6 hnq) + (suspendInactiveIpcA hP P H6 T6 RN6 E6 S6 ht hk6 hfree6)) ?_ + rintro s7 ⟨⟨σ7, hsts, hkh7, H7, T7, w7⟩, RN7, E7, S7⟩ + have hσ7 : σ7 = stsActA (updKSc σ6 (specPtr t) (.tcb { x6 with tcbState := .inactive })) (specPtr t) := + congrArg Prod.snd (Set.singleton_eq_singleton_iff.1 + (congrArg Prod.fst (hsts.symm.trans (setThreadState_stsActExA hk6 _)))) + rw [← hσ7] at RN7 E7 S7 + have h7t : σ7.kheap (specPtr t) = some (.tcb { x6 with tcbState := .inactive }) := by + rw [hkh7, updKSc_same] + have h7far : ∀ q, q ≠ specPtr t → σ7.kheap q = σ6.kheap q := fun q hq => by + rw [hkh7, updKSc_kheap, Function.update_of_ne hq] + have off7 : ∀ u ∈ Z.T, ∀ K : U64, (K = 3440#u64 ∨ K = 3472#u64 ∨ K = 3456#u64) → + L.mem s7 (wa u K) = L.mem s6 (wa u K) := by + intro u hu K hK + have hua := PE.tcb u hu + have hKv : K.val = 3440 ∨ K.val = 3472 ∨ K.val = 3456 := by rcases hK with rfl | rfl | rfl <;> simp + have hv : (wa u K).val = u.val + K.val := Notif.tfield_val hua (by omega) + refine w7.mem_eq (field_aligned (H6.core.tcbs u hu) (by omega) (by rcases hK with rfl | rfl | rfl <;> simp)) + (fun h => ?_) + rcases h with h | h + · have h' := congrArg UScalar.val h + rw [hv, val_st (hP.n.tcb t ht)] at h' + by_cases e : u = t + · subst u; omega + · have := tcb_disj PE ht hu e; omega + · have hty := (H6.thT u).2 hu + have hK' : (wa u K).val < (L.ksb s6).val ∨ (L.ksb s6).val + ksSizeA ≤ (wa u K).val := by + have g := H6.place.ks u _ hty (fun irq => by + have := H6.place.irqK hty irq + cases this) + change u.val + 4096 ≤ (L.ksb s6).val ∨ _ at g + omega + have hn := H6.node_in H6.sch.cpuCs (o := layout.node.SCHED_ACTION) (by simp [layout.node.SCHED_ACTION]) + rw [h] at hK' + omega + obtain ⟨M7, Y7⟩ := yieldFrameA (mem' := L.mem s7) (σ' := σ7) M6 Y6 + (fun u hu => ⟨off7 u hu 3440#u64 (Or.inl rfl), off7 u hu 3456#u64 (Or.inr (Or.inr rfl))⟩) + (fun u hu => off7 u hu 3472#u64 (Or.inr (Or.inl rfl))) + (fun u _ y hy => by + by_cases e : specPtr u = specPtr t + · rw [e, hk6] at hy + cases hy + exact ⟨{ x6 with tcbState := .inactive }, by rw [e]; exact h7t, rfl, rfl, rfl⟩ + · exact ⟨y, by rw [h7far _ e]; exact hy, rfl, rfl, rfl⟩) + (fun c hc' => by + obtain ⟨y, n, hy, -⟩ := H6.scr.sc c hc' + exact ⟨y, n, y, hy, by rw [h7far _ (fun h => by rw [h, hk6] at hy; cases hy)]; exact hy, rfl⟩) + have hS7 : Fl4SpecA64.suspend (specPtr t) σ = ({((), σ7)}, False) := by + unfold Fl4SpecA64.suspend + rw [nd_bind_oneSc hc1, hpc, nd_bind_oneSc hdq, nd_bind_oneSc (tcbReleaseRemove_eqA σ4 _), ← hσ5, + nd_bind_oneSc hy6] + exact hsts + -- the write footprint + have hPT : TcbPlaced Z.T := H0.core.tcbs + have Hs := H0.toSchedSRN + have z1 : SSzA σ σ1 := (frame_of_runA (inferInstance : SPresInst (cancelIpc (Z := Unit) (specPtr t))).pres hc1).2.1 + have z13 : SSzA σ1 σ3 := fun p y n h => by + by_cases e : p = specPtr t + · rw [e, hk3] at h; cases h + · rw [Hpc.far p e] at h; exact ⟨y, h⟩ + have z3 : SSzA σ σ3 := SSzA.trans z1 z13 + have k3 : L.ksb s3 = L.ksb s := w13.1 + have z4 : SSzA σ σ4 := fun p y n h => z3 p y n (by rw [← hk4h]; exact h) + have k4 : L.ksb s4 = L.ksb s := F4.wr.1.trans k3 + have z5 : SSzA σ σ5 := fun p y n h => z4 p y n (by rw [← hk5h]; exact h) + have k5 : L.ksb s5 = L.ksb s := F5.wr.1.trans k4 + have k6 : L.ksb s6 = L.ksb s := w6.1.trans k5 + have c6 : L.cpu s6 = L.cpu s := w6.2.1.trans (F5.wr.2.1.trans (F4.wr.2.1.trans w13.2.1)) + have g1 : WritesIn st s r.1 (fun b => SchedStepNWA (st := st) N E R Z m σ s b ∨ BufW Bf b) := + w1.mono fun b hb => cancelReplyW_stepNA Hs hP hb + have g3 : WritesIn st r.1 s3 (fun b => SchedStepNWA (st := st) N E R Z m σ s b ∨ BufW Bf b) := + Hpc.wr.mono fun b hb => by + subst hb + exact Or.inl (Or.inl (Or.inr (tcbStepWA_of_off hPT ht 528 (by omega) (by omega) (by omega) (by omega) hrv))) + have g4 : WritesIn st s3 s4 (fun b => SchedStepNWA (st := st) N E R Z m σ s b ∨ BufW Bf b) := + F4.wr.mono fun b hb => Or.inl (Or.inl (Or.inl (mwr_back rfl rfl z3 k3 hb))) + have g5 : WritesIn st s4 s5 (fun b => SchedStepNWA (st := st) N E R Z m σ s b ∨ BufW Bf b) := + F5.wr.mono fun b hb => Or.inl (Or.inl (Or.inl (mwr_back rfl rfl z4 k4 hb))) + have g6 : WritesIn st s5 s6 (fun b => SchedStepNWA (st := st) N E R Z m σ s b ∨ BufW Bf b) := + w6.mono fun b hb => hb.elim (fun h => Or.inl (Or.inl (Or.inl (mwr_back rfl rfl z5 k5 h)))) + (fun h => by subst h; exact Or.inl (yieldA_stepNA hPT ht)) + have g7 : WritesIn st s6 s7 (fun b => SchedStepNWA (st := st) N E R Z m σ s b ∨ BufW Bf b) := + w7.mono fun b hb => hb.elim (fun h => by subst h; exact Or.inl (stA_stepNA hPT ht)) + (fun h => by rw [h, nodeAt_ksb k6, c6]; exact Or.inl (by have hh := SchedSRN.act_stepNA (N := N) (E := E) (Rp := R) Hs; exact hh)) + rw [WP.spec_ok] + exact ⟨σ7, by rw [hsusp]; exact hS7, ((((g1.trans1 g3).trans1 g4).trans1 g5).trans1 g6).trans1 g7, + frame_of_runA (inferInstance : SPresInst (Fl4SpecA64.suspend (Z := Unit) (specPtr t))).pres hS7⟩ + + +end +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/Stage3TcbSuspResA.lean b/proofs/Fl4ProofsA64/P3/Stage3TcbSuspResA.lean new file mode 100644 index 000000000..bcc4c1c35 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3TcbSuspResA.lean @@ -0,0 +1,133 @@ +/- +P3 (AArch64, merge line, D8 step 3, TCB F0 rest): **the write footprints of TCB labels 12 (Suspend) and 13 +(Resume)**, siblings of `tcbSuspend_localA` / `tcbResume_localA` (`P3/TcbResumeLocalA`, same premises): the caller's +`Restart` (`tcbRestartCaller_partA`) then `suspend_footA` / `restart_footA`. Each concludes the two spec steps, the +write set `SchedStepNWA N E R Z m σ s ∨ BufW Bf` from the pre-`Restart` state and `SchedFrameA σ σ2`; with the +`*_localA` theorem (through `WP.spec_and`) this is what `JStage3A.of_schedWrX` takes. + +Generated by `handoff/tools/ptu/gen_sr.py`. No named premise beyond the `*_localA` theorems'. New module. +-/ +import Fl4ProofsA64.P3.TcbResumeLocalA +import Fl4ProofsA64.P3.Stage3TcbRestartFr2A +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4SpecA64 Fl4Spec Sched + +noncomputable section + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + +/-- **Suspend (label 12): write footprint and spec frame** (module docstring). -/ +theorem tcbSuspend_footA (hP : IPlacedA N Z.T E R Bf) (P : IpcSchedPlacementA Z.th N E R Bf) + (hc : m.curCore < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {s : S} {a : A} {σ : State Unit} {stack : U64 → List U64} + (V : TcbIpcViewsA st Z m κ N E R Bf stack s σ) (hC0 : ∀ c ∈ Z.C, c ≠ 0#u64) + {t : U64} (ht : t ∈ Z.T) {ctx : tcb.Ctx} {block call donate : Bool} + (hstall : smp.remote_tcb_stall st ar s a t = ok (s, a)) + {xc : Tcb} (hxc : σ.kheap (specPtr (L.curW s)) = some (.tcb xc)) (hrc : runnable xc.tcbState = true) + (tgt : ∀ (s1 : S) (σ1 : State Unit), + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) → + TcbIpcViewsA st Z m κ N E R Bf stack s1 σ1 → ∃ x, σ1.kheap (specPtr t) = some (.tcb x) ∧ + (∀ r', x.tcbState = .blockedOnReply r' → ∃ r ∈ R, specPtr r = r' ∧ + ∃ xr, σ1.kheap r' = some (.reply xr) ∧ xr.replyTcb = some (specPtr t) ∧ ScReplyCovA Z.C m σ1 r') ∧ + (∀ e', epBlocked x.tcbState = some e' → L.mem s1 (tBlockA t) ∈ E ∧ + ∃ z, σ1.kheap e' = some (.endpoint z) ∧ specPtr t ∈ z.epQueue) ∧ + (∀ n', x.tcbState = .blockedOnNotification n' → L.mem s1 (tBlockA t) ∈ N ∧ + ∃ z, σ1.kheap n' = some (.notification z) ∧ specPtr t ∈ z.ntfnObj.ntfnQueue) ∧ + RepOKA σ1 (specPtr t) ∧ L.mem s1 (runVcpuA t) = 0#u64 ∧ x.tcbExt.tcbRunVcpu = none ∧ + L.mem s1 (affA t) = L.cpu s1) : + tcb.decode st ar s a fl4_abi.label.TCBSuspend t ctx ⦃ r => ∃ σ1 σ2, + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + (fl4CancelRun (Z := Unit) (specPtr t) >>= fun _ => Fl4SpecA64.suspend (specPtr t)) σ1 = + ({((), σ2)}, False) ∧ + WritesIn st s r.2.1 (fun b => SchedStepNWA (st := st) N E R Z m σ s b ∨ BufW Bf b) ∧ + SchedFrameA σ σ2 ⦄ := by + rw [tcbSuspend_programA hstall] + obtain ⟨Q0, R0, H0⟩ := id V.rel + have hct : L.curW s ∈ Z.T := H0.curT + have T0 : StEqA Z.T (L.mem s) σ := fun u hu y hy => (V.ep.at_tcb hu hy).st + have hcur : σ.curThread = specPtr (L.curW s) := H0.sch.curThread + refine WP.spec_bind (WP.spec_and (restartCaller_mcsA hP P V hxc hrc) + (tcbRestartCaller_partA (N := N) (E := E) (Rp := R) H0 T0 hct H0.core.tcbs hxc hrc (cur_thread_ok s))) ?_ + rintro s1 ⟨⟨σ1, hsts, V1⟩, σR, hR, -, -, -, F1⟩ + have eR : σR = σ1 := by + rw [hcur] at hsts + have h := hR.symm.trans hsts + exact congrArg Prod.snd (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst h)) + subst eR + obtain ⟨x, hk, hReply, hE, hN, hrep, hrun0, hrunS, hon⟩ := tgt s1 σR hsts V1 + refine WP.spec_bind (suspend_footA hP P hc hmb hmax htu s1 a σR V1.rel V1.ntfn V1.ep V1.stk + V1.mcp V1.yld hC0 ht hk hReply hE hN hrep hrun0 hrunS hon) ?_ + rintro ⟨s2, a2⟩ ⟨σ2, hspec, w2, G2⟩ + refine (WP.spec_ok _).2 ⟨σR, σ2, hsts, hspec, (F1.wr.trans w2).mono fun b hb => ?_, PreO.trans F1.fr G2⟩ + rcases hb with h | h | h + · exact Or.inl h + · exact Or.inl (schedStepN_backA F1.fr.2.1 F1.wr.1 h) + · exact Or.inr h + +/-- **Resume (label 13): write footprint and spec frame** (module docstring). -/ +theorem tcbResume_footA (hP : IPlacedA N Z.T E R Bf) (P : IpcSchedPlacementA Z.th N E R Bf) + (hc : m.curCore < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hmbF : ∀ a, Fl4MinBudgetAgreeA ar a) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {s : S} {a : A} {σ : State Unit} {stack : U64 → List U64} + (V : TcbIpcViewsA st Z m κ N E R Bf stack s σ) (hC0 : ∀ c ∈ Z.C, c ≠ 0#u64) + {t : U64} (ht : t ∈ Z.T) {ctx : tcb.Ctx} {block call donate : Bool} + (hstall : smp.remote_tcb_stall st ar s a t = ok (s, a)) + {xc : Tcb} (hxc : σ.kheap (specPtr (L.curW s)) = some (.tcb xc)) (hrc : runnable xc.tcbState = true) + (hidle : ∀ c < fl4A64Platform.numNodes, specPtr t ≠ m.idleThreadOf c) + (tgt : ∀ (s1 : S) (σ1 : State Unit), + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) → + TcbIpcViewsA st Z m κ N E R Bf stack s1 σ1 → ∃ x, σ1.kheap (specPtr t) = some (.tcb x) ∧ + (∀ r', x.tcbState = .blockedOnReply r' → ∃ r ∈ R, specPtr r = r' ∧ + ∃ xr, σ1.kheap r' = some (.reply xr) ∧ xr.replyTcb = some (specPtr t) ∧ ScReplyCovA Z.C m σ1 r') ∧ + (∀ e', epBlocked x.tcbState = some e' → L.mem s1 (tBlockA t) ∈ E ∧ + ∃ z, σ1.kheap e' = some (.endpoint z) ∧ specPtr t ∈ z.epQueue) ∧ + (∀ n', x.tcbState = .blockedOnNotification n' → L.mem s1 (tBlockA t) ∈ N ∧ + ∃ z, σ1.kheap n' = some (.notification z) ∧ specPtr t ∈ z.ntfnObj.ntfnQueue) ∧ + RepOKA σ1 (specPtr t) ∧ L.mem s1 (affA t) = L.cpu s1 ∧ + (∀ (s2 : S) (σ2 : State Unit) (Q : U64 → U64 → List U64) (RR : U64 → List U64), + SchedSRB st { Z with Q := Q, R := RR } m κ s2 σ2 (specPtr t) → + (∃ y, σ2.kheap (specPtr t) = some (.tcb y) ∧ y.tcbState = .restart) → + (∀ sc y n, L.mem s2 (scA t) = sc → sc ≠ 0#u64 → + σ2.kheap (scNameA m sc) = some (.schedContext y n) → scActive y = true) ∧ + (L.mem s2 (scA t) ≠ 0#u64 → L.mem s2 (scCoreA (L.mem s2 (scA t))) = L.cpu s2))) : + tcb.decode st ar s a fl4_abi.label.TCBResume t ctx ⦃ r => ∃ σ1 σ2, + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + fl4TcbRestart (Z := Unit) (specPtr t) σ1 = ({((), σ2)}, False) ∧ + WritesIn st s r.2.1 (fun b => SchedStepNWA (st := st) N E R Z m σ s b ∨ BufW Bf b) ∧ + SchedFrameA σ σ2 ⦄ := by + rw [tcbResume_programA hstall] + obtain ⟨Q0, R0, H0⟩ := id V.rel + have hct : L.curW s ∈ Z.T := H0.curT + have T0 : StEqA Z.T (L.mem s) σ := fun u hu y hy => (V.ep.at_tcb hu hy).st + have hcur : σ.curThread = specPtr (L.curW s) := H0.sch.curThread + refine WP.spec_bind (WP.spec_and (restartCaller_mcsA hP P V hxc hrc) + (tcbRestartCaller_partA (N := N) (E := E) (Rp := R) H0 T0 hct H0.core.tcbs hxc hrc (cur_thread_ok s))) ?_ + rintro s1 ⟨⟨σ1, hsts, V1⟩, σR, hR, -, -, -, F1⟩ + have eR : σR = σ1 := by + rw [hcur] at hsts + have h := hR.symm.trans hsts + exact congrArg Prod.snd (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst h)) + subst eR + obtain ⟨x, hk, hReply, hE, hN, hrep, hon, tailS⟩ := tgt s1 σR hsts V1 + refine WP.spec_bind (restart_footA hP P hc hmb hmbF hmax htu s1 a σR V1.rel V1.ntfn V1.ep V1.stk + V1.mcp V1.yld hC0 ht hk hReply hE hN hrep hon hidle tailS) ?_ + rintro ⟨s2, a2⟩ ⟨σ2, hspec, w2, G2⟩ + refine (WP.spec_ok _).2 ⟨σR, σ2, hsts, hspec, (F1.wr.trans w2).mono fun b hb => ?_, PreO.trans F1.fr G2⟩ + rcases hb with h | h | h + · exact Or.inl h + · exact Or.inl (schedStepN_backA F1.fr.2.1 F1.wr.1 h) + · exact Or.inr h + +end +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/Stage3TcbSuspendArmA.lean b/proofs/Fl4ProofsA64/P3/Stage3TcbSuspendArmA.lean new file mode 100644 index 000000000..589eb0d7a --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3TcbSuspendArmA.lean @@ -0,0 +1,146 @@ +/- +P3 (aarch64, A64-P3 Stage 3, contract §94): **TCB Suspend at the record**, live VCPU run allowed. + +* **`JSyscallA.tcbSuspendA`**: `decode_invocation` on a Thread cap with the Suspend label at a syscall entry: the + stall, the caller's `Restart`, then `sched.suspend` (`JStage3TA.suspend_liveRecA`). The spec step is + `fl4InvokeTcb (.suspend t)` at the restarted state. The whole record `JStage3A` holds at the end: the decode-level + facts (`SysL`, `SymL`, `J7A`) are `JSyscallA.decodeFinA`'s (its Suspend arm is P1's `suspendQAt`, which allows a live + run), the loop facts `Std6A` and `SchedValidA` are `suspend_liveRecA`'s. + +Named premises: [P] `ArchTotal`, `MaxSpi`, `ArchPlat`, `A64FrameMasks`, `hCs`, `hmb`, `hmax`, `htu`; [S] `h1` +(`numNodes = 1`, the remote stall). +New module. +-/ +import Fl4ProofsA64.P3.DelSuspendLiveA +import Fl4ProofsA64.P3.DelSuspendA +import Fl4ProofsA64.P3.DelScStallA +import Fl4ProofsA64.P3.DelRecA +import Fl4ProofsA64.P3.DelJ7LA +import Fl4ProofsA64.P3.DelSpecA +import Fl4ProofsA64.P3.Stage3FinA +import Fl4ProofsA64.P3.Stage3RestartA +import Fl4ProofsA64.P3.Stage3RsSiteA +import Fl4ProofsA64.P3.Stage3SignalIdleA +import Fl4ProofsA64.P3.Stage3DelAsmA +import Fl4ProofsA64.P3.Stage3DelAsmTopA +import Fl4ProofsA64.P3.TcbInvokeDispatchA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Vcpu Fl4SpecA64 + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {s : S} {a : A} {σ : State Unit} {m : McState} + +set_option maxHeartbeats 4000000 in +/-- **TCB Suspend at a syscall entry** (module docstring). -/ +theorem JSyscallA.tcbSuspendA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {slot lbl len buf t : U64} {blk call don : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Thread t) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hlbl : lbl = fl4_abi.label.TCBSuspend) : + ∃ xc σ1 σ2 s2 a2, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + (.ok [], σ2) ∈ ((fl4InvokeTcb (.suspend (specPtr t))).run σ1).1 ∧ + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (.Ok (), s2, a2) ∧ a2 = a ∧ + ∃ Q Rl stack', JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack' κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s2 a σ2 m ∧ BodyEndA σ σ2 m := by + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hslot hcov J.actW J.running J.cur + have hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM := J.toJStage3EntryA.curNotVmA + obtain ⟨xc, hkc, hst, hsp, J1, -, osk0, -⟩ := J.toJStage3A.of_restartCurA + RS.pl.choose_spec.choose_spec.choose_spec.choose_spec RS.sym RS.run hnvm RS.cur RS.vs RS.vc + have ht : t ∈ Z.T := J.tcbs ▸ (J.toJStage3A.slotCapTy hslot).1.2.2.2.2.2 t hcap + have T0 := J.toJStage3A.toTA J.sact J.noSwitch + have hstall : smp.remote_tcb_stall st ar s a t = ok (s, a) := T0.stall_singleA hCs h1 ht + set s1 := L.wr s (stA (L.curW s)) sched.TS_RESTART with hs1 + set σ1 := stUpdA σ σ.curThread xc .restart with hσ1 + have sact : SActA σ1 := + { f1 := fun u h => by + have : σ1.schedulerAction = σ.schedulerAction := rfl + rw [this, J.act] at h; cases h + f2 := ⟨{ xc with tcbState := .restart }, by rw [hσ1]; simp [stUpdA, setObjA], Or.inr (Or.inl rfl)⟩ } + have noSw : ∀ u, σ1.schedulerAction ≠ .switchThread u := fun u => J.noSwitch u + have hr : tcb.restart_caller st s = ok s1 := by + unfold tcb.restart_caller + rw [cur_thread_ok, bind_ok, hst] + -- the record at the restarted state, in the loop form + have JT := J1.toTA sact noSw + have JL : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7LA st (J7R2A st ar hPl.ipa)) + s1 a σ1 m := { JT with j7 := J7A.toLA JT.j7 } + have R6 : VcpuRec6A st Z OS Bf s1 σ1 := + ⟨J1.vcSnap, J1.vcSym1, J1.vcSym2, J1.vcBack, J1.runBack, J1.vcOS, J1.sys.sysl.fo, J1.userFr, J1.frTh, J1.bfSpec⟩ + -- the target is no core's idle thread + have hcap1 : capA st s1 slot = .Thread t := (osk0.capA hslot).trans hcap + have hy : IsSlotA Z.th slot := by + by_contra h + have e := J1.j5.2.2 slot hslot h + rw [hcap1] at e + cases e + obtain ⟨sl, hsl, esl⟩ := hy.absA J1.j.1.place + obtain ⟨c, hg, hrel⟩ := J1.j.1.st.getAbsA hsl + rw [esl, capAtA_eq, hcap1] at hrel + have ec : c = .threadCap (specPtr t) := by + have h := hrel.2 + cases c <;> simp_all [CapEqA, absCapA] + have hspec : specCapAtA σ1 sl = some (.threadCap (specPtr t)) := by + rw [← ec] + exact (getCap_outA (c := c) (σ' := σ1) hg).2 + obtain ⟨hti, hidles⟩ := JL.notIdle_of_capA JL.sinv.mc.curCore_lt hspec + have key := JL.suspend_liveRecA (ar := ar) J1.schedValid R6 hCs h1 hmb hmax htu ht (fun _ _ => rfl) hti hidles + obtain ⟨⟨s2, a2⟩, hsus, ea, sc, σc, σ2, stk', Q, Rl, sp, -, -, -, F, osk, -, J2, sv2, k6⟩ := exists_of_specA key + simp only at ea hsus osk J2 k6 + subst a2 + have hdec : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (.Ok (), s2, a) := by + rw [hcap, decodeTcb_programA, if_neg Bool.false_ne_true, hlbl, + tcbSuspend_programA (ctx := ⟨len, buf, call, slot, extra⟩) hstall, hr, bind_ok, hsus] + simp only [bind_ok, uncurry_apply_pair] + have hm : ((), σ2) ∈ ((fl4CancelRun (Z := Unit) (specPtr t) >>= fun _ => Fl4SpecA64.suspend (specPtr t)) σ1).1 := by + rw [sp]; rfl + -- the body-end facts: the cancel and the suspension are an `RsA` step, a `DelStepA` step + have hBody : BodyEndA σ σ2 m := by + obtain ⟨⟨_, σx⟩, hc, hs⟩ := hm + have R1 : RsA σ1 σx := (fl4CancelRun_dlA (specPtr t) σ1 _ hc).rs + have R2 : RsA σx σ2 := suspend_rsA (specPtr t) σx _ hs + have K : DelStepA σ1 σ2 := (PreO.trans R1 R2).step JL.sinv.curSc + obtain ⟨hB, hcur, -⟩ := BodyOKA.stepA (J.delRestartBodyA xc hkc).1 K + exact ⟨hB, hcur⟩ + have hspecI : (.ok [], σ2) ∈ ((fl4InvokeTcb (.suspend (specPtr t))).run σ1).1 := by + obtain ⟨⟨_, σx⟩, hc, hs⟩ := hm + simp only [fl4InvokeTcb, invokeTcb] + exact mem_bindE_okD (mem_liftED hc) (mem_liftED (mem_bind_ndD hs rfl)) + obtain ⟨-, -, sysl, symL, -, -, j7⟩ := J.decodeFinA hA hM hPl hfm hslot hcov + (rfl : capA st s slot = capA st s slot) hext hSC (lbl := lbl) + (by rw [hcap, hlbl]; simp [DecArmS1aEnt, DecArmQH]) + (fun _ h => by rw [hcap] at h; cases h) (fun _ h => by rw [hcap] at h; cases h) hdec σ2 m + have X := (k6 _ rfl J1.j5.2.1 (Std6A.ofJA J1)).ghostA Z.G Q Rl Z.hw + have fx : DelCapFxA st Z H s1 := + { fhSlot := fun t ht => isSlot_fhA ((J1.sr.thT t).2 ht) + fhOK := J1.fhOK + ptCapIn := J1.ptCapIn } + have fx2 : DelCapFxA st Z H s2 := fx.keepA st J1.j5.2.1 fun y hy => osk.capA hy + have J3 : JStage3TA st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stk' κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s2 a σ2 m := { J2 with j7 := j7 } + exact ⟨xc, σ1, σ2, s2, a, hkc, rfl, hsp, hspecI, hdec, rfl, Q, Rl, stk', + J3.toJA sysl X.vcSym1 X.vcSym2 X.heapWin X.coresAll fx2.ptCapIn symL fx2.fhOK X.zwf sv2 X.utWin X.devWin X.vcBack + X.runBack X.vcSnap X.vcOS X.userFr X.frTh X.bfSpec, hBody⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3TcbTlsA.lean b/proofs/Fl4ProofsA64/P3/Stage3TcbTlsA.lean new file mode 100644 index 000000000..d2535524c --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3TcbTlsA.lean @@ -0,0 +1,231 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 3): **TCB SetTLSBase on another thread, at the record**. + +* **`JStage3A.setTls_stepA`**: the TPIDR_EL0 word of the target and the spec's register write keep the record. +* **`JSyscallA.setTlsA`**: `decode_invocation` on a Thread cap with the SetTLSBase label at a syscall entry. + +Named premises: [P] `ArchTotal`, `MaxSpi`, `ArchPlat`, `A64FrameMasks`, `hCs`; [S] `h1` (`numNodes = 1`, the remote +stall). Success path with `t ≠ L.curW s`; the self-targeted call (which reschedules) and the `Err` returns are not +lifted. +New module. +-/ +import Fl4ProofsA64.P3.Stage3TcbWordsA +import Fl4ProofsA64.P3.TcbInvokeThreadFieldsA +import Fl4ProofsA64.P3.EpFootHelpA +import Fl4ProofsA64.P3.ScSwitchA +import Fl4ProofsA64.P3.Stage3TcbBodyA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +private theorem tlsSpecEqA {σ σTls σFinal : State Unit} {t : ObjRef} {x : Tcb} {word : Word} + (hk : σ.kheap t = some (.tcb x)) + (hTls : σTls = Notif.updK σ t (.tcb (Notif.setReg x tlsBaseRegister word))) + (htail : ((), σFinal) ∈ ((Fl4Spec.NonDet.when (t = σTls.curThread) + (rescheduleRequired (Z := Unit))) σTls).1) : + (.ok [], σFinal) ∈ ((fl4InvokeTcb (.setTLSBase t word)).run σ).1 := by + have hInfo : archGetSanitiseRegisterInfo (Z := Unit) t σ = + ({(decide (x.tcbArch.tcbVcpu ≠ none), σ)}, False) := by + have hVcpu : archThreadGet (Z := Unit) ArchTcb.tcbVcpu t σ = ({(x.tcbArch.tcbVcpu, σ)}, False) := by + unfold archThreadGet + rw [nd_bind_oneD (Notif.getTcb_eq hk)] + rfl + unfold archGetSanitiseRegisterInfo + rw [nd_bind_oneD hVcpu] + rfl + have hSet := Notif.asUser_setRegister_eq tlsBaseRegister word hk + rw [← hTls] at hSet + change (.ok [], σFinal) ∈ ((invokeTcb (.setTLSBase t word)).run σ).1 + simp only [invokeTcb] + apply mem_liftED + rw [nd_bind_oneD hInfo] + have hWord : sanitiseRegister (decide (x.tcbArch.tcbVcpu ≠ none)) tlsBaseRegister word = word := rfl + rw [hWord, nd_bind_oneD hSet, Notif.nd_gets_bind] + exact mem_bind_ndD htail rfl + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s : S} {a : A} {σ : State Unit} {m : McState} + +/-- The spec state after the TLS register write. -/ +def tlsStateA (σ : State Unit) (t : ObjRef) (y : Tcb) (v : Fl4SpecA64.Word) : State Unit := + updKSc σ t (.tcb (Notif.setReg y tlsBaseRegister v)) + +/-- The spec's SetTLSBase on a thread other than the current one. -/ +theorem tlsSpecOtherA {σ : State Unit} {t : ObjRef} {y : Tcb} {w : Fl4SpecA64.Word} + (hk : σ.kheap t = some (.tcb y)) (hOther : t ≠ σ.curThread) : + (.ok [], tlsStateA σ t y w) ∈ ((fl4InvokeTcb (.setTLSBase t w)).run σ).1 := by + refine tlsSpecEqA (σTls := Notif.updK σ t (.tcb (Notif.setReg y tlsBaseRegister w))) hk rfl ?_ + have h : t ≠ (Notif.updK σ t (.tcb (Notif.setReg y tlsBaseRegister w))).curThread := hOther + rw [Fl4Spec.NonDet.when, if_neg h] + rfl + +/-- The spec's SetTLSBase on the current thread with no pending switch (the reschedule only sets +`chooseNewThread`). -/ +theorem tlsSpecSelfA {σ : State Unit} {t : ObjRef} {y : Tcb} {w : Fl4SpecA64.Word} + (hk : σ.kheap t = some (.tcb y)) (hc : t = σ.curThread) (hn : ∀ u, σ.schedulerAction ≠ .switchThread u) : + (.ok [], { tlsStateA σ t y w with schedulerAction := .chooseNewThread }) ∈ + ((fl4InvokeTcb (.setTLSBase t w)).run σ).1 := by + refine tlsSpecEqA (σTls := Notif.updK σ t (.tcb (Notif.setReg y tlsBaseRegister w))) hk rfl ?_ + have h : t = (Notif.updK σ t (.tcb (Notif.setReg y tlsBaseRegister w))).curThread := hc + rw [Fl4Spec.NonDet.when, if_pos h] + rw [rescheduleRequired_noswitchA (σ := Notif.updK σ t (.tcb (Notif.setReg y tlsBaseRegister w))) (fun u => hn u)] + exact Set.mem_singleton _ + +set_option maxHeartbeats 2000000 in +/-- **SetTLSBase at the record** (module docstring). -/ +theorem JStage3A.setTls_stepA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {t v : U64} (ht : t ∈ Z.T) {y : Tcb} (hy : σ.kheap (specPtr t) = some (.tcb y)) : + let s' := L.wr s (Endp.regA t (regIdxA tlsBaseRegister)) v + BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s' ∧ + (SysL st H Z.Cs OS ia ie s' → SymL st H Z.Cs s' → Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s' → + J7A st (J7R2A st ar nA) Z H OS s' (tlsStateA σ (specPtr t) y v.bv) m → + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) + s' a (tlsStateA σ (specPtr t) y v.bv) m) := by + intro s' + have hP := J.iplaced + have htT : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + have hta : TcbAddr t := J.sr.core.tcbs t ht + have hidx : (regIdxA tlsBaseRegister).val = 35 := rfl + have hlt : (regIdxA tlsBaseRegister).val < 37 := by omega + have vb : (Endp.regA t (regIdxA tlsBaseRegister)).val = t.val + 536 := by + rw [Endp.regA_val hta hlt, hidx] + have al : Aligned (Endp.regA t (regIdxA tlsBaseRegister)) := (Endp.regA_tf hta hlt).1 + have blk' : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s' := by + have h := obj.Tcb.set_reg.blk (st := st) s (t := t) (r := regIdxA tlsBaseRegister) (v := v) (by omega) + (BXI.blk J.sys.ent.bxi) htT + rw [Endp.set_reg_eqA, WP.spec_ok] at h + exact h.1 + refine ⟨blk', fun sysl symL userOK j7 => ?_⟩ + have w : WritesIn st s s' (PlainRegW Z.T) := + (writesIn_wr al).mono fun b hb => ⟨t, ht, by rw [hb, vb]; omega, by rw [hb, vb]; omega⟩ + obtain ⟨hE, -, -⟩ := write_reg_rel hP.e s σ J.ep ht hy tlsBaseRegister v + have F : SchedFrameA σ (tlsStateA σ (specPtr t) y v.bv) := + SchedFrameA.upd_tcb (x' := Notif.setReg y tlsBaseRegister v.bv) hy ⟨rfl, rfl, rfl⟩ + have ntfn : NtfnStateRel N Z.T (L.mem s') (tlsStateA σ (specPtr t) y v.bv) := by + have key : ∀ q, (tlsStateA σ (specPtr t) y v.bv).kheap q = + if q = specPtr t then some (.tcb (Notif.setReg y tlsBaseRegister v.bv)) else σ.kheap q := by + intro q + simp only [tlsStateA, updKSc_kheap, Function.update_apply] + have feN : Notif.FootEq N Z.T (L.mem s) (L.mem s') := by + intro b hb hf + refine mem_wr_ne' al hb fun h => ?_ + have ev : b.val = t.val + 536 := by rw [← h]; exact vb + rcases hf with ⟨q, hq, h1, h2⟩ | ⟨u, hu, h1⟩ + · have := hP.n.disj q hq t ht; omega + · have hua := hP.e.tcb u hu + have a1 : t.val % 4096 = 0 := hta.align + have a2 : u.val % 4096 = 0 := hua.align + have hv : b.val = u.val + 3328 ∨ b.val = u.val + 3336 ∨ b.val = u.val + 256 ∨ b.val = u.val + 3448 ∨ + b.val = u.val + 3496 ∨ b.val = u.val + 3504 := by + rcases h1 with h1 | h1 | h1 | h1 | h1 | h1 <;> + rw [h1] <;> + simp only [stA_eq, Notif.tBlockA_eq, Notif.badgeRegA_eq, Notif.prioA_eq', Notif.nextEp_eq, + Notif.prevEp_eq] <;> + rw [Notif.tfield_val hua] <;> simp + omega + have kvN : Notif.KView σ (tlsStateA σ (specPtr t) y v.bv) := by + refine ⟨fun q z h => ?_, fun q z h => ?_⟩ + · rw [key, if_neg (fun e => by rw [e, hy] at h; cases h)]; exact h + · by_cases e : q = specPtr t + · rw [e, hy] at h + cases h + exact ⟨Notif.setReg y tlsBaseRegister v.bv, by rw [key, if_pos e], rfl, rfl, rfl⟩ + · exact ⟨z, by rw [key, if_neg e]; exact h, rfl, rfl, rfl⟩ + exact J.ntfn.frame hP.n feN kvN + exact J.of_plainTcbA ht hy (Notif.setReg y tlsBaseRegister v.bv).tcbArch rfl w blk' sysl symL userOK j7 hE ntfn + +set_option maxHeartbeats 2000000 in +/-- **TCB SetTLSBase on another thread at a syscall entry** (module docstring). -/ +theorem JSyscallA.setTlsA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + {slot lbl len buf t v : U64} {blk call don : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Thread t) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hlbl : lbl = fl4_abi.label.TCBSetTLSBase) (hlen : ¬ len < 1#u64) + (hv : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 0#u64 = ok v) (hne : t ≠ L.curW s) : + ∃ xc σ1 σ2 s2, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + (.ok [], σ2) ∈ ((fl4InvokeTcb (.setTLSBase (specPtr t) v.bv)).run σ1).1 ∧ + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (.Ok (), s2, a) ∧ + ∃ Q Rl, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s2 a σ2 m ∧ Sched.BodyEndA σ σ2 m := by + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hslot hcov J.actW J.running J.cur + have hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM := J.toJStage3EntryA.curNotVmA + obtain ⟨xc, hkc, hst, hsp, J1, -, -, -⟩ := J.toJStage3A.of_restartCurA RS.pl.choose_spec.choose_spec.choose_spec.choose_spec + RS.sym RS.run hnvm RS.cur RS.vs RS.vc + have ht : t ∈ Z.T := J.tcbs ▸ (J.toJStage3A.slotCapTy hslot).1.2.2.2.2.2 t hcap + have T0 := J.toJStage3A.toTA J.sact J.noSwitch + have hstall : smp.remote_tcb_stall st ar s a t = ok (s, a) := T0.stall_singleA hCs h1 ht + set s1 := L.wr s (stA (L.curW s)) sched.TS_RESTART with hs1 + set σ1 := stUpdA σ σ.curThread xc .restart with hσ1 + have hr : tcb.restart_caller st s = ok s1 := by + unfold tcb.restart_caller + rw [cur_thread_ok, bind_ok, hst] + obtain ⟨y, hy, -⟩ := J1.mcp t ht + have R := J1.setTls_stepA (st := st) (v := v) ht hy + obtain ⟨-, K⟩ := R + set s2 := L.wr s1 (Endp.regA t (regIdxA tlsBaseRegister)) v with hs2 + have hcur1 : σ1.curThread = specPtr (L.curW s1) := J1.j.1.sch.curThread + have hcur0 : σ.curThread = specPtr (L.curW s) := J.j.1.sch.curThread + have eq1 : L.curW s1 = L.curW s := specPtr_inj (hcur1.symm.trans hcur0) + have hidx : layout.reg.TLS_BASE = regIdxA tlsBaseRegister := by + rw [layout.reg.TLS_BASE, layout.reg.TPIDR_EL0] + rfl + have hdec : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (.Ok (), s2, a) := by + rw [hcap, decodeTcb_programA, if_neg Bool.false_ne_true, hlbl, + tcbSetTLSBase_postA (ctx := ⟨len, buf, call, slot, extra⟩) hstall hlen hv hr, hidx] + dsimp only + rw [← hs2] + have e2 : L.curW s2 = L.curW s := by + refine Eq.trans ?_ eq1 + have hq := J1.sys.ent.bxi.1.choose_spec.choose_spec + have hn : NodePlaced st H.tcbs s1 (L.cpu s1) := hq.nodes _ hq.cur + have hta : TcbAddr t := J1.sr.core.tcbs t ht + have htT : t ∈ H.tcbs := by rw [J1.tcbs]; exact ht + have c0 : layout.node.CUR_THREAD.val < 4288 := by simp + have hlt : (regIdxA tlsBaseRegister).val < 37 := Endp.regIdxA_lt _ + have al : Aligned (Endp.regA t (regIdxA tlsBaseRegister)) := (Endp.regA_tf hta hlt).1 + have er : Endp.regA t (regIdxA tlsBaseRegister) = wa t 536#u64 := by + apply UScalar.eq_of_val_eq + have h1 : (regIdxA tlsBaseRegister).val = 35 := rfl + have h2 : (536#u64 : U64).val = 536 := by simp + rw [Endp.regA_val hta hlt, Notif.tfield_val hta (by omega : (536#u64 : U64).val < 4096), h1, h2] + have hw : WritesIn st s1 s2 (· = Endp.regA t (regIdxA tlsBaseRegister)) := writesIn_wr al + have ecpu : L.cpu s2 = L.cpu s1 := L.cpu_wr _ _ _ + unfold LawfulStore.curW + rw [ecpu, nodeAt_of_ksb hw.1] + refine hw.mem_eq (nodeAt_aligned hn c0 (by simp)) fun e => ?_ + exact node_ne_field hn c0 htT hta (K := 536#u64) (by simp) (e.trans er) + rw [if_neg (fun e => hne (e.trans e2)), bind_ok] + obtain ⟨-, -, sysl, symL, -, userOK, j7⟩ := J.decodeFinA hA hM hPl hfm hslot hcov + (rfl : capA st s slot = capA st s slot) hext hSC (lbl := lbl) + (by rw [hcap, hlbl]; simp [DecArmS1aEnt, DecArmQH, DecArmQZ2, DecArmKQ2, DecArmKQ, capFam]) + (fun _ h => by rw [hcap] at h; cases h) (fun _ h => by rw [hcap] at h; cases h) hdec + (tlsStateA σ1 (specPtr t) y v.bv) m + have J2 := K sysl symL userOK j7 + refine ⟨xc, σ1, _, _, hkc, rfl, hsp, ?_, hdec, Z.Q, Z.R, J2, Sched.BodyEndA.ofTcbUpd J hkc hy rfl⟩ + refine tlsSpecOtherA hy ?_ + show specPtr t ≠ σ1.curThread + rw [hcur1, eq1] + exact fun e => hne (specPtr_inj e) + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3TcbTlsSelfA.lean b/proofs/Fl4ProofsA64/P3/Stage3TcbTlsSelfA.lean new file mode 100644 index 000000000..bdffb75e7 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3TcbTlsSelfA.lean @@ -0,0 +1,447 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 3): **TCB SetTLSBase on the current thread, at the record** (the record step). + +At a syscall entry the spec action is `resume_cur_thread` and the caller is `Restart`, so the self-targeted call's +`reschedule_required` enqueues nothing: concretely one word, `SCHED_ACTION := CHOOSE_NEW`, and in the spec +`schedulerAction := chooseNewThread`. + +* **`tlsActA`**: the spec state after the TLS register write and the action change. +* **`JStage3A.setTlsSelf_stepA`**: the record across the TLS register word and the action word (`of_ipcStepA` with the + composite footprint); the scheduler relation by `SchedSRA.set_action`, the invariants `SInvXA` / `SIdleA` by + `LoopInvA.reschedule`, `SchedValidA` by hand (its `act` clause is vacuous once the action is `chooseNewThread`). + +Named premises: those of `fin`'s producer (`sysl`, `symL`, `userOK`, `j7`) and [S] `h0` (the concrete action word is 0, +`resume_cur_thread`). New module. +-/ +import Fl4ProofsA64.P3.Stage3TcbTlsA +import Fl4ProofsA64.P3.Stage3TcbWordsA +import Fl4ProofsA64.P3.Stage3Lift3QA +import Fl4ProofsA64.P3.DelSinvLoopA +import Fl4ProofsA64.P3.ScSwitchA +import Fl4ProofsA64.P3.CancelIpcReplyProgA +import Fl4ProofsA64.P3.Stage3TcbBodyA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-- The spec state after the TLS register write and `schedulerAction := chooseNewThread`. -/ +def tlsActA (σ : State Unit) (t : ObjRef) (y : Tcb) (v : Fl4SpecA64.Word) : State Unit := + { tlsStateA σ t y v with schedulerAction := .chooseNewThread } + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s : S} {a : A} {σ : State Unit} {m : McState} + +/-- A word of a placed `other` object is outside the scheduler write set. -/ +theorem otherWordOffMWrA {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} {Z : XSetsA} + (H : SchedSRN st Z m κ s σ) {p b : U64} {len : Nat} + (hp : Z.th p = some (.other len)) (hlo : p.val ≤ b.val) (hhi : b.val < p.val + len) : + ¬ MWrA (st := st) Z m σ s b := by + have hsz : (ObjKindA.other len).size = len := rfl + have hne : (ObjKindA.other len) ≠ .cnode 0 := by intro e; cases e + have hK : b.val < (L.ksb s).val ∨ (L.ksb s).val + ksSizeA ≤ b.val := by + have h := H.place.ks p (.other len) hp (fun irq => hne (H.place.irqK hp irq)) + rw [hsz] at h + omega + have hS : ¬ ScFootA Z.C m σ b := by + intro h + have hd := H.scp.th_out h hp + rw [hsz] at hd + omega + have hT : ∀ t ∈ Z.T, b.val < t.val ∨ t.val + 4096 ≤ b.val := by + intro t ht + have hty := (H.thT t).2 ht + have hne : p ≠ t := by + rintro rfl + rw [hty] at hp + cases hp + have h := H.place.apart p t (.other len) .tcb hp hty hne + change p.val + len ≤ t.val ∨ t.val + 4096 ≤ p.val at h + omega + exact H.not_mwr hS hT hK + +set_option maxHeartbeats 4000000 in +/-- **SetTLSBase on the current thread at the record** (module docstring). -/ +theorem JStage3A.setTlsSelf_stepA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (sact : SActA σ) (nsw : ∀ u, σ.schedulerAction ≠ .switchThread u) (h0 : L.actionW s = 0#u64) + {t v : U64} (ht : t ∈ Z.T) {y : Tcb} (hy : σ.kheap (specPtr t) = some (.tcb y)) : + let s2 := L.wr s (Endp.regA t (regIdxA tlsBaseRegister)) v + let s3 := L.wr s2 (L.nodeAt s2 (L.cpu s2) layout.node.SCHED_ACTION) 1#u64 + BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s3 ∧ + sched.reschedule_required st s2 = ok s3 ∧ + (SysL st H Z.Cs OS ia ie s3 → SymL st H Z.Cs s3 → Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s3 → + J7A st (J7R2A st ar nA) Z H OS s3 (tlsActA σ (specPtr t) y v.bv) m → + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) + s3 a (tlsActA σ (specPtr t) y v.bv) m) := by + intro s2 s3 + have hP := J.iplaced + have htT : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + have hta : TcbAddr t := J.sr.core.tcbs t ht + have hidx : (regIdxA tlsBaseRegister).val = 35 := rfl + have hlt : (regIdxA tlsBaseRegister).val < 37 := by omega + have vb : (Endp.regA t (regIdxA tlsBaseRegister)).val = t.val + 536 := by + rw [Endp.regA_val hta hlt, hidx] + have al : Aligned (Endp.regA t (regIdxA tlsBaseRegister)) := (Endp.regA_tf hta hlt).1 + -- the TLS write + obtain ⟨blk2, -⟩ := J.setTls_stepA (v := v) ht hy + have w1 : WritesIn st s s2 (PlainRegW Z.T) := + (writesIn_wr al).mono fun b hb => ⟨t, ht, by rw [hb, vb]; omega, by rw [hb, vb]; omega⟩ + set σt := tlsStateA σ (specPtr t) y v.bv with hσt + set σ3 := tlsActA σ (specPtr t) y v.bv with hσ3 + obtain ⟨hE, -, -⟩ := write_reg_rel hP.e s σ J.ep ht hy tlsBaseRegister v + have F1 : SchedFrameA σ σt := + SchedFrameA.upd_tcb (x' := Notif.setReg y tlsBaseRegister v.bv) hy ⟨rfl, rfl, rfl⟩ + have key : ∀ q, σt.kheap q = + if q = specPtr t then some (.tcb (Notif.setReg y tlsBaseRegister v.bv)) else σ.kheap q := by + intro q + simp only [hσt, tlsStateA, updKSc_kheap, Function.update_apply] + have key3 : ∀ q, σ3.kheap q = + if q = specPtr t then some (.tcb (Notif.setReg y tlsBaseRegister v.bv)) else σ.kheap q := by + intro q + simp only [hσ3, tlsActA, hσt, tlsStateA, updKSc_kheap, Function.update_apply] + have keepO : ∀ q o, σ.kheap q = some o → (∀ z, o ≠ .tcb z) → σ3.kheap q = some o := fun q o h n1 => by + rw [key3, if_neg (fun e => by rw [e, hy] at h; exact n1 y (Option.some.inj h).symm)] + exact h + have backO : ∀ q o, σ3.kheap q = some o → (∀ z, o ≠ .tcb z) → σ.kheap q = some o := fun q o h n1 => by + rw [key3] at h + split at h + · exact absurd (Option.some.inj h).symm (n1 _) + · exact h + have fwdT : ∀ q z, σ.kheap q = some (.tcb z) → ∃ w, σ3.kheap q = some (.tcb w) ∧ + w = { z with tcbArch := w.tcbArch } := by + intro q z h + by_cases e : q = specPtr t + · rw [e, hy] at h + cases h + exact ⟨Notif.setReg y tlsBaseRegister v.bv, by rw [key3, if_pos e], rfl⟩ + · exact ⟨z, by rw [key3, if_neg e]; exact h, rfl⟩ + have bwdT : ∀ q w, σ3.kheap q = some (.tcb w) → ∃ z, σ.kheap q = some (.tcb z) ∧ + w = { z with tcbArch := w.tcbArch } := by + intro q w h + rw [key3] at h + split at h + · rename_i e + cases h + exact ⟨y, e ▸ hy, rfl⟩ + · exact ⟨w, h, rfl⟩ + -- the scheduler relation: TLS word, then the action word + have SR2 : SchedSRA st Z m κ s2 σt := by + refine J.sr.stable w1 (fun b hb => f0WordOffReadA J.sr.toSchedSRN hb.f0) ?_ ?_ + · refine + { fix := ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ + act := rfl + rq := rfl + rel := rfl + sc := fun q x k h => (keepO q _ h (fun _ e => by cases e)).trans h.symm + tcb := fun q x h => by + obtain ⟨w, hw, e⟩ := fwdT q x h + exact ⟨w, hw, by rw [e], by rw [e], by rw [e]⟩ + scBack := fun q x k h => (backO q _ h (fun _ e => by cases e)).trans h.symm + tcbBack := fun q w h => by + obtain ⟨z, hz, -⟩ := bwdT q w h + exact ⟨z, hz⟩ + ntfnBack := fun q z h => ⟨z, backO q _ h (fun _ e => by cases e), rfl⟩ } + · intro q x z hx hz + obtain ⟨w, hw, e⟩ := fwdT q x hx + have hw' : σt.kheap q = some (.tcb w) := hw + rw [hz] at hw' + cases hw' + refine Or.inl ⟨by rw [e], fun h => ?_⟩ + rw [e] at h + exact h + have hno0 : NodePlaced st Z.T s (L.cpu s) := J.sr.core.nodes _ J.sr.sch.cpuCs + have er : Endp.regA t (regIdxA tlsBaseRegister) = wa t 536#u64 := by + apply UScalar.eq_of_val_eq + have h2 : (536#u64 : U64).val = 536 := by simp + rw [Endp.regA_val hta hlt, Notif.tfield_val hta (by omega : (536#u64 : U64).val < 4096), hidx, h2] + have hact2 : L.actionW s2 = 0#u64 := + (act_wr_field hno0 ht hta er (by simp) (by simp)).trans h0 + have eq3 : sched.reschedule_required st s2 = ok s3 := + eq_of_spec_fd (sched.reschedule_required.resume (st := st) s2 hact2) + have blk3 : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s3 := by + have R := sched.reschedule_required.blk (st := st) s2 blk2 + rw [eq3, WP.spec_ok] at R + exact R.1 + refine ⟨blk3, eq3, fun sysl symL userOK j7 => ?_⟩ + -- the action word + have W3 : WritesIn st s2 s3 (SWrA (st := st) Z s2) := SR2.toSchedSRN.act_wr 1#u64 + have SR3 : SchedSRA st Z m κ s3 σ3 := + SR2.set_action 1#u64 (by simp [actOfA]) (fun u h => by cases h) + have hT : TcbPlaced Z.T := hP.e.tcb + have F : SchedFrameA σ σ3 := + PreO.trans F1 (SchedFrameA.of_eq (σ := σt) (σ' := σ3) rfl rfl rfl rfl rfl rfl rfl rfl) + -- the endpoint and notification relations + have feE : Endp.FootEq Z.T E Rp Bf (L.mem s2) (L.mem s3) := by + intro b hb hf + exact W3.mem_eq hb (fun h => endpFootOffWrite SR2.toSchedSRN J.ipcPlace hf (Or.inr h)) + have kvE : Endp.KView σt σ3 := + ⟨fun q z h => h, fun q z h => ⟨z, h, rfl⟩, fun q z h => ⟨z, h, rfl, rfl, rfl, rfl, rfl⟩, rfl⟩ + have ep2 := hE + have ep3 : EpStateRel Z.T E Rp Bf (L.mem s3) σ3 := ep2.frame hP.e feE kvE + have feN2 : Notif.FootEq N Z.T (L.mem s) (L.mem s2) := by + intro b hb hf + refine mem_wr_ne' al hb fun h => ?_ + have ev : b.val = t.val + 536 := by rw [← h]; exact vb + rcases hf with ⟨q, hq, h1, h2⟩ | ⟨u, hu, h1⟩ + · have := hP.n.disj q hq t ht; omega + · have hua := hP.e.tcb u hu + have a1 : t.val % 4096 = 0 := hta.align + have a2 : u.val % 4096 = 0 := hua.align + have hv : b.val = u.val + 3328 ∨ b.val = u.val + 3336 ∨ b.val = u.val + 256 ∨ b.val = u.val + 3448 ∨ + b.val = u.val + 3496 ∨ b.val = u.val + 3504 := by + rcases h1 with h1 | h1 | h1 | h1 | h1 | h1 <;> + rw [h1] <;> + simp only [stA_eq, Notif.tBlockA_eq, Notif.badgeRegA_eq, Notif.prioA_eq', Notif.nextEp_eq, + Notif.prevEp_eq] <;> + rw [Notif.tfield_val hua] <;> simp + omega + have kvN2 : Notif.KView σ σt := by + refine ⟨fun q z h => ?_, fun q z h => ?_⟩ + · have e : q ≠ specPtr t := fun e => by rw [e, hy] at h; cases h + show σt.kheap q = some (.notification z) + rw [key, if_neg e]; exact h + · by_cases e : q = specPtr t + · rw [e, hy] at h + cases h + exact ⟨Notif.setReg y tlsBaseRegister v.bv, by rw [key, if_pos e], rfl, rfl, rfl⟩ + · exact ⟨z, by rw [key, if_neg e]; exact h, rfl, rfl, rfl⟩ + have ntfn2 : NtfnStateRel N Z.T (L.mem s2) σt := J.ntfn.frame hP.n feN2 kvN2 + have feN : Notif.FootEq N Z.T (L.mem s2) (L.mem s3) := by + intro b hb hf + exact W3.mem_eq hb (fun h => notifFootOffMWrA SR2.toSchedSRN J.ipcPlace.notification hf (Or.inr h)) + have kvN : Notif.KView σt σ3 := ⟨fun q z h => h, fun q z h => ⟨z, h, rfl, rfl, rfl⟩⟩ + have ntfn3 : NtfnStateRel N Z.T (L.mem s3) σ3 := ntfn2.frame hP.n feN kvN + -- reply stacks, IPC fields, TCB words + have reply : ReplyStackRelA (L.mem s3) Rp Z.C σ3 (scNameA m) stack := by + refine J.reply.frame (fun r hr => ?_) (fun c _ z k h => ⟨z, k, keepO _ _ h (fun _ e => by cases e), rfl⟩) + (fun r _ x' h => ⟨x', backO _ _ h (fun _ e => by cases e), rfl⟩) + have hr3 := hP.e.rep r hr + have one : ∀ O : U64, O.val = 8 ∨ O.val = 16 → L.mem s3 (wa r O) = L.mem s (wa r O) := by + intro O hO + have hv : (wa r O).val = r.val + O.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have hal : Aligned (wa r O) := by unfold Aligned; rw [hv]; omega + have e1 : L.mem s2 (wa r O) = L.mem s (wa r O) := by + refine w1.mem_eq hal fun hb => ?_ + obtain ⟨u, hu, l, h⟩ := hb + have := hP.e.rt r hr u hu + omega + have e2 : L.mem s3 (wa r O) = L.mem s2 (wa r O) := by + refine W3.mem_eq hal fun hb => ?_ + exact otherWordOffMWrA SR2.toSchedSRN (J.ipcPlace.reply r hr) (by omega) (by omega) (Or.inr hb) + rw [e2, e1] + exact ⟨one _ (Or.inr (by simp [layout.reply.NEXT])), one _ (Or.inl (by simp [layout.reply.PREV]))⟩ + have K : IpcKeepA σ σ3 := + ⟨fun p x h => by + obtain ⟨v', hv, e⟩ := fwdT p x h + exact ⟨v', hv, by rw [e], by rw [e], by rw [e], by rw [e], by rw [e], by rw [e]⟩, + fun p z k h => ⟨z, k, keepO p _ h (fun _ e => by cases e), rfl⟩, + fun p z k h => ⟨z, k, backO p _ h (fun _ e => by cases e), rfl⟩⟩ + have hno2 : NodePlaced st Z.T s2 (L.cpu s2) := SR2.core.nodes _ SR2.sch.cpuCs + have c0 : layout.node.SCHED_ACTION.val < 4288 := by simp [layout.node.SCHED_ACTION] + have tw3 : ∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → (K.val < 256 ∨ 3328 ≤ K.val) → + L.mem s3 (wa u K) = L.mem s (wa u K) := fun u hu K h1 h2 h3 => by + have hua := hT u hu + have e : L.mem s3 (wa u K) = L.mem s2 (wa u K) := + mem_wr_ne' (nodeAt_aligned hno2 c0 (by simp [layout.node.SCHED_ACTION])) (field_aligned hua h1 h2) + (node_ne_field hno2 c0 hu hua h1) + rw [e] + exact plainReg_keepA hT w1 hu K h1 h2 h3 + have rv : RunVcpuInv st Z.T s3 := fun u hu h0' => by + have e1 : L.mem s3 (runVcpuA u) = L.mem s (runVcpuA u) := by + unfold runVcpuA + exact tw3 u hu 3672#u64 (by simp) (by simp) (Or.inr (by simp)) + have e2 : L.mem s3 (Fl4ProofsA64.P1.stA u) = L.mem s (Fl4ProofsA64.P1.stA u) := by + rw [Fl4ProofsA64.P1.stA_eq] + exact tw3 u hu 3328#u64 (by simp) (by simp) (Or.inr (by simp)) + rw [e2]; exact J.runVcpu u hu (e1 ▸ h0') + have blkQ : BlkQueuedA Z.T E N (L.mem s3) σ3 := by + refine J.blkQ.keepA (fun u hu => ?_) (fun u _ x' h => ?_) (fun q z h => keepO q _ h (fun _ e => by cases e)) + (fun q z h => keepO q _ h (fun _ e => by cases e)) + · rw [Notif.tBlockA_eq]; exact tw3 u hu 3336#u64 (by simp) (by simp) (Or.inr (by simp)) + · obtain ⟨z, hz, e⟩ := bwdT _ x' h + exact Or.inr ⟨z, hz, by rw [e]⟩ + -- the spec invariants + have svT : SchedValidA σt := J.schedValid.congr rfl rfl rfl rfl rfl rfl + (fun q z k h => keepO q _ h (fun _ e => by cases e)) + (fun u x h => by + obtain ⟨v', hv, e⟩ := fwdT u x h + exact ⟨v', hv, by rw [e]⟩) + (fun u v' h => by + obtain ⟨z, hz, e⟩ := bwdT u v' h + exact ⟨z, hz, by rw [e], by rw [e]⟩) + have sv : SchedValidA σ3 := + ⟨fun d p u hm => svT.rq d p u hm, fun e1 _ => by exfalso; revert e1; simp [hσ3, tlsActA], + fun u x sc e1 e2 e3 => svT.blk u x sc e1 e2 e3, fun u hu => svT.rls u hu⟩ + have sinvT : SInvXA σt m n := by + refine J.sinv.of_fr + { tcb := fun q x h => by + obtain ⟨v', hv, e⟩ := fwdT q x h + exact ⟨v', hv, by rw [e], by rw [e], by rw [e]⟩ + tcb' := fun q v' h => by + obtain ⟨z, hz, e⟩ := bwdT q v' h + exact ⟨z, hz, by rw [e], by rw [e], by rw [e]⟩ + sc := fun q z k => ⟨fun h => backO q _ h (fun _ e => by cases e), fun h => keepO q _ h (fun _ e => by cases e)⟩ + cur := rfl + idle := rfl + csc := rfl + rq := rfl + rls := rfl + act := rfl, time := rfl, cons := rfl } ?_ + exact J.sinv.irqN.updTcbA hy rfl + have sidleT : SIdleA σt m n := + { f3 := fun c hc => by + obtain ⟨x, hx, hs⟩ := J.sidle.f3 c hc + obtain ⟨v', hv, e⟩ := fwdT _ x hx + exact ⟨v', hv, by rw [e]; exact hs⟩ + f4 := fun z k h => J.sidle.f4 z k (backO _ _ h (fun _ e => by cases e)) + f5 := fun q z k h => J.sidle.f5 q z k (backO _ _ h (fun _ e => by cases e)) + f6 := J.sidle.f6.of_eq fun sl => by + unfold specCapAtA + by_cases e : sl.1 = specPtr t + · rw [key, if_pos e, e, hy] + rfl + · rw [key, if_neg e] + f7 := fun y k h => J.sidle.f7 y k (backO _ _ h (fun _ e => by cases e)) + idleSlot := fun c hc => by + have h0 := J.sidle.idleSlot c hc + unfold vtCapA at h0 ⊢ + by_cases e : m.idleThreadOf c = specPtr t + · rw [key, if_pos e]; rw [e, hy] at h0; exact h0 + · rw [key, if_neg e]; exact h0 + idleBound := fun c hc => by + have h0 := J.sidle.idleBound c hc + unfold vcpuOfA at h0 ⊢ + by_cases e : m.idleThreadOf c = specPtr t + · rw [key, if_pos e]; rw [e, hy] at h0; exact h0 + · rw [key, if_neg e]; exact h0 } + have sactT : SActA σt := by + refine ⟨fun u e => sact.f1 u e, ?_⟩ + obtain ⟨x, hx, hs⟩ := sact.f2 + obtain ⟨v', hv, e⟩ := fwdT _ x hx + exact ⟨v', hv, by rw [e]; exact hs⟩ + have LI : LoopInvA σ3 m n := by + have hr : ((), σ3) ∈ (rescheduleRequired (Z := Unit) σt).1 := by + rw [rescheduleRequired_noswitchA (σ := σt) (fun u => nsw u)] + exact Set.mem_singleton _ + exact LoopInvA.reschedule ⟨sinvT, sidleT, sactT⟩ hr + have Qk : QKeepA σ σ3 := QKeepA.ofBwd fun q o h => by + rw [key3] at h + split at h + · rename_i e + cases h + exact ⟨.tcb y, by rw [e]; exact hy, QObjA.ofTcb rfl⟩ + · exact ⟨o, h, QObjA.refl o⟩ + have wAll : WritesIn st s s3 (SchedStepNWA (st := st) N E Rp Z m σ s) := + (w1.trans W3).mono fun b hb => hb.elim (fun h => h.f0.stepN hT) + (fun h => schedStepN_backA F1.2.1 w1.1 (Or.inl (Or.inl (Or.inr h)))) + exact J.of_ipcStepA (Q := Z.Q) (Rl := Z.R) (curOf := Z.curOf) (curScOf := Z.curScOf) + wAll F.1 F.2.1 Qk F.2.2 (fun _ _ => rfl) (BlkOK.bxi blk3) sysl symL userOK j7 SR3 ep3 ntfn3 reply K + (fun u hu O h h8 => tw3 u hu O (by omega) h8 (by omega)) LI.sinv LI.sidle rv blkQ sv + +set_option maxHeartbeats 2000000 in +/-- **TCB SetTLSBase on the current thread at a syscall entry** (module docstring). -/ +theorem JSyscallA.setTlsSelfA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + {slot lbl len buf t v : U64} {blk call don : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Thread t) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hlbl : lbl = fl4_abi.label.TCBSetTLSBase) (hlen : ¬ len < 1#u64) + (hv : tcb.arg st s ⟨len, buf, call, slot, extra⟩ 0#u64 = ok v) (hself : t = L.curW s) : + ∃ xc σ1 σ2 s2, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + (.ok [], σ2) ∈ ((fl4InvokeTcb (.setTLSBase (specPtr t) v.bv)).run σ1).1 ∧ + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (.Ok (), s2, a) ∧ + ∃ Q Rl, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s2 a σ2 m ∧ Sched.BodyEndA σ σ2 m := by + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hslot hcov J.actW J.running J.cur + have hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM := J.toJStage3EntryA.curNotVmA + obtain ⟨xc, hkc, hst, hsp, J1, -, -, -⟩ := J.toJStage3A.of_restartCurA RS.pl.choose_spec.choose_spec.choose_spec.choose_spec + RS.sym RS.run hnvm RS.cur RS.vs RS.vc + have ht : t ∈ Z.T := J.tcbs ▸ (J.toJStage3A.slotCapTy hslot).1.2.2.2.2.2 t hcap + have T0 := J.toJStage3A.toTA J.sact J.noSwitch + have hstall : smp.remote_tcb_stall st ar s a t = ok (s, a) := T0.stall_singleA hCs h1 ht + set s1 := L.wr s (stA (L.curW s)) sched.TS_RESTART with hs1 + set σ1 := stUpdA σ σ.curThread xc .restart with hσ1 + have hr : tcb.restart_caller st s = ok s1 := by + unfold tcb.restart_caller + rw [cur_thread_ok, bind_ok, hst] + obtain ⟨y, hy, -⟩ := J1.mcp t ht + have sact1 : SActA σ1 := + ⟨fun u h => J.sact.f1 u h, ⟨{ xc with tcbState := .restart }, by simp only [hσ1, stUpdA, setObjA, Function.update_self], + Or.inr (Or.inl rfl)⟩⟩ + have hcur1 : σ1.curThread = specPtr (L.curW s1) := J1.j.1.sch.curThread + have hcur0 : σ.curThread = specPtr (L.curW s) := J.j.1.sch.curThread + have eq1 : L.curW s1 = L.curW s := specPtr_inj (hcur1.symm.trans hcur0) + have hno0 : NodePlaced st Z.T s (L.cpu s) := J.sr.core.nodes _ J.sr.sch.cpuCs + have hcta : TcbAddr (L.curW s) := J.sr.core.tcbs _ J.sr.curT + have h01 : L.actionW s1 = 0#u64 := by + have e : L.actionW s1 = L.actionW s := + act_wr_field hno0 J.sr.curT hcta (stA_eq _) (by simp) (by simp) + have h0 : L.actionW s = 0#u64 := by + have hh := J.j.1.sch.act.symm.trans J.act + by_contra hn0 + unfold actOfA at hh + simp [hn0] at hh + split at hh <;> simp at hh + rw [e, h0] + obtain ⟨-, eq3, K⟩ := J1.setTlsSelf_stepA (v := v) (y := y) sact1 (fun u h => J.noSwitch u h) h01 ht hy + set s2 := L.wr s1 (Endp.regA t (regIdxA tlsBaseRegister)) v with hs2 + have hidx : layout.reg.TLS_BASE = regIdxA tlsBaseRegister := by + rw [layout.reg.TLS_BASE, layout.reg.TPIDR_EL0] + rfl + have hdec : syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (.Ok (), L.wr s2 (L.nodeAt s2 (L.cpu s2) layout.node.SCHED_ACTION) 1#u64, a) := by + rw [hcap, decodeTcb_programA, if_neg Bool.false_ne_true, hlbl, + tcbSetTLSBase_postA (ctx := ⟨len, buf, call, slot, extra⟩) hstall hlen hv hr, hidx] + dsimp only + rw [← hs2] + have e2 : L.curW s2 = L.curW s := by + refine Eq.trans ?_ eq1 + have hq := J1.sys.ent.bxi.1.choose_spec.choose_spec + have hn : NodePlaced st H.tcbs s1 (L.cpu s1) := hq.nodes _ hq.cur + have hta : TcbAddr t := J1.sr.core.tcbs t ht + have htT : t ∈ H.tcbs := by rw [J1.tcbs]; exact ht + have c0 : layout.node.CUR_THREAD.val < 4288 := by simp + have hlt : (regIdxA tlsBaseRegister).val < 37 := Endp.regIdxA_lt _ + have al : Aligned (Endp.regA t (regIdxA tlsBaseRegister)) := (Endp.regA_tf hta hlt).1 + have er : Endp.regA t (regIdxA tlsBaseRegister) = wa t 536#u64 := by + apply UScalar.eq_of_val_eq + have h1 : (regIdxA tlsBaseRegister).val = 35 := rfl + have h2 : (536#u64 : U64).val = 536 := by simp + rw [Endp.regA_val hta hlt, Notif.tfield_val hta (by omega : (536#u64 : U64).val < 4096), h1, h2] + have hw : WritesIn st s1 s2 (· = Endp.regA t (regIdxA tlsBaseRegister)) := writesIn_wr al + have ecpu : L.cpu s2 = L.cpu s1 := L.cpu_wr _ _ _ + unfold LawfulStore.curW + rw [ecpu, nodeAt_of_ksb hw.1] + refine hw.mem_eq (nodeAt_aligned hn c0 (by simp)) fun e => ?_ + exact node_ne_field hn c0 htT hta (K := 536#u64) (by simp) (e.trans er) + rw [if_pos (hself.trans e2.symm), eq3, bind_ok] + obtain ⟨-, -, sysl, symL, -, userOK, j7⟩ := J.decodeFinA hA hM hPl hfm hslot hcov + (rfl : capA st s slot = capA st s slot) hext hSC (lbl := lbl) + (by rw [hcap, hlbl]; simp [DecArmS1aEnt, DecArmQH, DecArmQZ2, DecArmKQ2, DecArmKQ, capFam]) + (fun _ h => by rw [hcap] at h; cases h) (fun _ h => by rw [hcap] at h; cases h) hdec + (tlsActA σ1 (specPtr t) y v.bv) m + have J2 := K sysl symL userOK j7 + refine ⟨xc, σ1, _, _, hkc, rfl, hsp, ?_, hdec, Z.Q, Z.R, J2, Sched.BodyEndA.ofTcbUpdChoose J hkc hy⟩ + refine tlsSpecSelfA hy ?_ (fun u h => J.noSwitch u h) + show specPtr t = σ1.curThread + rw [hcur1, eq1, ← hself] + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3TcbUnbindA.lean b/proofs/Fl4ProofsA64/P3/Stage3TcbUnbindA.lean new file mode 100644 index 000000000..9601c6e9d --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3TcbUnbindA.lean @@ -0,0 +1,252 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 3): **TCB UnbindNotification at the record**. + +* **`JStage3A.unbindNtfn_stepA`**: `unbind_notification t` on a bound thread keeps the record (the TCB-side unlink of + delete-hooks' `thrUnbind_recA`, carried back to `JStage3A` with `of_ipcStepA`). +* **`JSyscallA.unbindNtfnA`**: `decode_invocation` on a Thread cap with the UnbindNotification label at a syscall + entry. + +Named premises: [P] `ArchTotal`, `MaxSpi`, `ArchPlat`, `A64FrameMasks`, `hCs`; [S] `h1` (`numNodes = 1`, the remote +stall). The thread has a bound notification (`hb`; the other case is an `Err` return, not lifted). +New module. +-/ +import Fl4ProofsA64.P3.Stage3Lift3QA +import Fl4ProofsA64.P3.Stage3IpcGlueA +import Fl4ProofsA64.P3.Stage3SignalIdleA +import Fl4ProofsA64.P3.Stage3RestartA +import Fl4ProofsA64.P3.Stage3RsSiteA +import Fl4ProofsA64.P3.Stage3FinA +import Fl4ProofsA64.P3.Stage3PreemptTA +import Fl4ProofsA64.P3.Stage3TcbNtfnFrA +import Fl4ProofsA64.P3.DelThrUnbindA +import Fl4ProofsA64.P3.DelScStallA +import Fl4ProofsA64.P3.Stage3TcbBodyA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s : S} {a : A} {σ : State Unit} {m : McState} + +/-- The spec state after unbinding the notification at `p` from the thread at `t`. -/ +def unbindStateA (σ : State Unit) (p t : ObjRef) (x : Notification) (y : Tcb) : State Unit := + updKSc (updKSc σ p (.notification { x with ntfnBoundTcb := none })) t (.tcb { y with tcbBoundNotification := none }) + +set_option maxHeartbeats 2000000 in +/-- **`unbind_notification` at the record** (module docstring). -/ +theorem JStage3A.unbindNtfn_stepA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (sact : SActA σ) (noSwitch : ∀ u, σ.schedulerAction ≠ .switchThread u) + {t : U64} (ht : t ∈ Z.T) (h0 : L.mem s (boundA t) ≠ 0#u64) : + ∃ x y, L.mem s (boundA t) ∈ N ∧ + σ.kheap (specPtr (L.mem s (boundA t))) = some (.notification x) ∧ σ.kheap (specPtr t) = some (.tcb y) ∧ + y.tcbBoundNotification = some (specPtr (L.mem s (boundA t))) ∧ + ∃ s', notification.unbind_notification st s t = ok s' ∧ + BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s' ∧ + (SysL st H Z.Cs OS ia ie s' → SymL st H Z.Cs s' → Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s' → + J7A st (J7R2A st ar nA) Z H OS s' + (unbindStateA σ (specPtr (L.mem s (boundA t))) (specPtr t) x y) m → + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s' a + (unbindStateA σ (specPtr (L.mem s (boundA t))) (specPtr t) x y) m) := by + have T := J.toTA sact noSwitch + have hP := J.iplaced + have htT : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + have blk := BXI.blk J.sys.ent.bxi + set p := L.mem s (boundA t) with hpdef + have hpN : p ∈ N := J.ipcHeapCov.ntfn _ (blk.2.2.1.2.2.bnd t htT h0) + have back : L.mem s (Notif.nBoundA p) = t := blk.2.2.1.2.2.bs.tb t htT h0 + have t0 : t ≠ 0#u64 := J.sr.place.nz t _ ((J.sr.thT t).2 ht) + obtain ⟨y, hy, hrel⟩ := J.ep.tcb t ht + obtain ⟨x, hk, hnrel⟩ := J.ntfn.ntfn p hpN + have hxb : x.ntfnBoundTcb = some (specPtr t) := by rw [hnrel.bound, back]; exact Notif.optRef_ne t0 + have hyb : y.tcbBoundNotification = some (specPtr p) := by + have : tBoundA t = boundA t := rfl + rw [hrel.bound, this, ← hpdef]; exact Notif.optRef_ne h0 + have eun : notification.unbind_notification st s t = ok (L.wr (L.wr s (ntfnBtA p) 0#u64) (boundA t) 0#u64) := + tcbUnbindNtfnWritesA s t p h0 hpdef.symm + set s' := L.wr (L.wr s (ntfnBtA p) 0#u64) (boundA t) 0#u64 with hs' + set σ' := unbindStateA σ (specPtr p) (specPtr t) x y with hσ' + have sp : unbindNotification (Z := Unit) (specPtr t) σ = ({((), σ')}, False) := + tcbUnbindNotification_specA hk hy hyb + have blk' : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s' := by + have h := Fl4ProofsA64.P1.notification.unbind_notification.blk (st := st) s t blk htT + rwa [eun, WP.spec_ok] at h + refine ⟨x, y, hpN, hk, hy, hyb, s', eun, blk', fun sysl symL userOK j7 => ?_⟩ + obtain ⟨s2, e2, σ2, sp2, T2, w2, -, -, -, -, -, tw⟩ := + exists_of_specA (T.thrUnbind_recA (st := st) ht) + rw [eun] at e2 + obtain rfl := ok_inj_ff e2 + have eσ : σ2 = σ' := by + rw [sp] at sp2 + exact (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst sp2))).2.symm + subst eσ + rw [← hpdef] at w2 + have ne : specPtr t ≠ specPtr p := fun e => by rw [e, hk] at hy; cases hy + have key : ∀ q, σ'.kheap q = + if q = specPtr t then some (.tcb { y with tcbBoundNotification := none }) + else if q = specPtr p then some (.notification { x with ntfnBoundTcb := none }) else σ.kheap q := by + intro q + simp only [hσ', unbindStateA, updKSc_kheap, Function.update_apply] + have keepO : ∀ q o, σ.kheap q = some o → (∀ z, o ≠ .tcb z) → (∀ z, o ≠ .notification z) → + σ'.kheap q = some o := fun q o h n1 n2 => by + rw [key, if_neg (fun e => by rw [e, hy] at h; exact n1 y (Option.some.inj h).symm), + if_neg (fun e => by rw [e, hk] at h; exact n2 x (Option.some.inj h).symm)] + exact h + have backO : ∀ q o, σ'.kheap q = some o → (∀ z, o ≠ .tcb z) → (∀ z, o ≠ .notification z) → + σ.kheap q = some o := fun q o h n1 n2 => by + rw [key] at h + split at h + · exact absurd (Option.some.inj h).symm (n1 _) + · split at h + · exact absurd (Option.some.inj h).symm (n2 _) + · exact h + have fwdT : ∀ q z, σ.kheap q = some (.tcb z) → ∃ w, σ'.kheap q = some (.tcb w) ∧ w.tcbState = z.tcbState ∧ + w.tcbSchedContext = z.tcbSchedContext ∧ w.tcbFault = z.tcbFault ∧ w.tcbIpcframe = z.tcbIpcframe ∧ + w.tcbIpcBuffer = z.tcbIpcBuffer ∧ w.tcbMcpriority = z.tcbMcpriority ∧ w.tcbYieldTo = z.tcbYieldTo ∧ w.tcbFlags = z.tcbFlags := by + intro q z h + by_cases e : q = specPtr t + · rw [e, hy] at h + cases h + exact ⟨{ y with tcbBoundNotification := none }, by rw [key, if_pos e], rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ + · have e' : q ≠ specPtr p := fun c => by rw [c, hk] at h; cases h + exact ⟨z, by rw [key, if_neg e, if_neg e']; exact h, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ + have bwdT : ∀ q w, σ'.kheap q = some (.tcb w) → ∃ z, σ.kheap q = some (.tcb z) ∧ w.tcbState = z.tcbState ∧ + w.tcbSchedContext = z.tcbSchedContext := by + intro q w h + rw [key] at h + split at h + · rename_i e + cases h + exact ⟨y, e ▸ hy, rfl, rfl⟩ + · split at h + · cases h + · exact ⟨w, h, rfl, rfl⟩ + have K : IpcKeepA σ σ' := by + refine ⟨fun q z h => ?_, fun q z k h => ⟨z, k, keepO q _ h (fun _ e => by cases e) (fun _ e => by cases e), rfl⟩, + fun q z k h => ⟨z, k, backO q _ h (fun _ e => by cases e) (fun _ e => by cases e), rfl⟩⟩ + obtain ⟨w, hw, -, -, f, g, h', i, j, j'⟩ := fwdT q z h + exact ⟨w, hw, f, g, h', i, j, j'⟩ + have F : SchedFrameA σ σ' := tcbNtfnPair_frameA hk hy ⟨rfl, rfl, rfl⟩ + have hfit : p.val + 64 ≤ 2 ^ 64 := by + have := hP.n.ntfn p hpN + exact this.fits + have w : WritesIn st s s' (SchedStepNWA (st := st) N E Rp Z m σ s) := w2.mono fun b hb => by + rcases hb with rfl | rfl + · exact ntfnBtA_stepNA hpN hfit + · exact boundA_stepNA hP.e.tcb ht + have sv : SchedValidA σ' := J.schedValid.congr rfl rfl rfl rfl rfl rfl + (fun q z k h => keepO q _ h (fun _ e => by cases e) (fun _ e => by cases e)) + (fun u z h => by + obtain ⟨w, hw, -, e, -⟩ := fwdT u z h + exact ⟨w, hw, e⟩) + (fun u w h => bwdT u w h) + have Qk : QKeepA σ σ' := QKeepA.ofBwd fun q o h => by + rw [key] at h + split at h + · rename_i e + cases h + exact ⟨.tcb y, by rw [e]; exact hy, QObjA.ofTcb rfl⟩ + · split at h + · rename_i e + cases h + exact ⟨.notification x, by rw [e]; exact hk, QObjA.ofNtfn (List.Sublist.refl _)⟩ + · exact ⟨o, h, QObjA.refl o⟩ + exact J.of_ipcStepA (Q := Z.Q) (Rl := Z.R) (curOf := Z.curOf) (curScOf := Z.curScOf) w F.1 F.2.1 Qk F.2.2 + (fun _ _ => rfl) (BlkOK.bxi blk') sysl symL userOK j7 T2.sr T2.ep T2.ntfn T2.reply K + (fun u hu O h h8 => tw.1 u hu O (by omega) h8 (by omega)) + T2.sinv T2.sidle T2.runVcpu T2.blkQ sv + +set_option maxHeartbeats 2000000 in +/-- **TCB UnbindNotification at a syscall entry** (module docstring). -/ +theorem JSyscallA.unbindNtfnA + (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (h1 : fl4A64Platform.numNodes = 1) + {slot lbl len buf t : U64} {blk call don : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hslot : slot ∈ OS) (hcov : SlotCov st OS s slot) (hcap : capA st s slot = .Thread t) + (hext : ∀ x ∈ extra.val, x ≠ 0#u64 → x ∈ OS) (hSC : ∀ x ∈ extra.val, x ≠ 0#u64 → SlotCov st OS s x) + (hlbl : lbl = fl4_abi.label.TCBUnbindNotification) (hb : L.mem s (boundA t) ≠ 0#u64) : + ∃ xc σ1 σ2 s2, σ.kheap σ.curThread = some (.tcb xc) ∧ σ1 = stUpdA σ σ.curThread xc .restart ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + (.ok [], σ2) ∈ ((fl4InvokeTcb (.notificationControl (specPtr t) none)).run σ1).1 ∧ + syscall.decode_invocation st ar s a lbl len slot (capA st s slot) blk call don false buf extra = + ok (.Ok (), s2, a) ∧ + ∃ Q Rl, JStage3A st AR { Z with Q := Q, R := Rl } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s2 a σ2 m ∧ Sched.BodyEndA σ σ2 m := by + have RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s := J.toJStage3A.rsSiteA hext hslot hcov J.actW J.running J.cur + have hnvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM := J.toJStage3EntryA.curNotVmA + obtain ⟨xc, hkc, hst, hsp, J1, -, -, -⟩ := J.toJStage3A.of_restartCurA RS.pl.choose_spec.choose_spec.choose_spec.choose_spec + RS.sym RS.run hnvm RS.cur RS.vs RS.vc + have ht : t ∈ Z.T := J.tcbs ▸ (J.toJStage3A.slotCapTy hslot).1.2.2.2.2.2 t hcap + have T0 := J.toJStage3A.toTA J.sact J.noSwitch + have hstall : smp.remote_tcb_stall st ar s a t = ok (s, a) := T0.stall_singleA hCs h1 ht + set s1 := L.wr s (stA (L.curW s)) sched.TS_RESTART with hs1 + set σ1 := stUpdA σ σ.curThread xc .restart with hσ1 + have sact : SActA σ1 := + { f1 := fun u h => by + have : σ1.schedulerAction = σ.schedulerAction := rfl + rw [this, J.act] at h; cases h + f2 := ⟨{ xc with tcbState := .restart }, by rw [hσ1]; simp [stUpdA, setObjA], Or.inr (Or.inl rfl)⟩ } + have noSw : ∀ u, σ1.schedulerAction ≠ .switchThread u := fun u => J.noSwitch u + have hbound : L.mem s1 (boundA t) = L.mem s (boundA t) := by + have hct : TcbAddr (L.curW s) := J.sr.core.tcbs _ J.sr.curT + have hta : TcbAddr t := J.sr.core.tcbs t ht + refine mem_wr_ne' (by rw [stA_eq]; exact field_aligned hct (by simp) (by simp)) + (field_aligned hta (K := 3368#u64) (by simp) (by simp)) ?_ + intro e + have ev := congrArg UScalar.val e + have v1 : (stA (L.curW s)).val = (L.curW s).val + 3328 := by + rw [stA_eq, Notif.tfield_val hct (by decide : (3328#u64 : U64).val < 4096)]; rfl + have v2 : (boundA t).val = t.val + 3368 := by + show (wa t 3368#u64).val = _ + rw [Notif.tfield_val hta (by decide : (3368#u64 : U64).val < 4096)]; rfl + have a1 : t.val % 4096 = 0 := hta.align + have a2 : (L.curW s).val % 4096 = 0 := hct.align + omega + have hb1 : L.mem s1 (boundA t) ≠ 0#u64 := by rw [hbound]; exact hb + have R := J1.unbindNtfn_stepA (st := st) sact noSw ht hb1 + obtain ⟨x, y, hpN, hk, hy, hyb, s2, eun, -, K⟩ := R + have hr : tcb.restart_caller st s = ok s1 := by + unfold tcb.restart_caller + rw [cur_thread_ok, bind_ok, hst] + have hnz : L.mem s (boundA t) ≠ 0#u64 := hb + have perf := tcbUnbindNotification_performA (st := st) (ar := ar) (T := Z.T) (N := N) + (ctx := ⟨len, buf, call, slot, extra⟩) (σRestart := σ1) hstall hb hr hnz hbound J1.iplaced.n ht + (hbound ▸ hpN) (hbound ▸ hk) hy (hbound ▸ hyb) + obtain ⟨⟨r, s3, a3⟩, ed, er, ea, es, -, hmem⟩ := exists_of_specA perf + simp only at er ea es hmem + subst er ea + have es2 : s3 = s2 := by + have := tcbUnbindNtfnWritesA (st := st) s1 t (L.mem s (boundA t)) hnz hbound + rw [this] at eun + rw [es]; exact ok_inj_ff eun + subst es2 + have hdec : syscall.decode_invocation st ar s a3 lbl len slot (capA st s slot) blk call don false buf extra = + ok (.Ok (), s3, a3) := by + rw [hcap, decodeTcb_programA, if_neg Bool.false_ne_true, hlbl] + exact ed + obtain ⟨-, -, sysl, symL, -, userOK, j7⟩ := J.decodeFinA hA hM hPl hfm hslot hcov + (rfl : capA st s slot = capA st s slot) hext hSC (lbl := lbl) + (by rw [hcap, hlbl]; simp [DecArmS1aEnt, DecArmQH, DecArmQZ2, DecArmKQ2, DecArmKQ, capFam]) + (fun _ h => by rw [hcap] at h; cases h) (fun _ h => by rw [hcap] at h; cases h) hdec + (unbindStateA σ1 (specPtr (L.mem s1 (boundA t))) (specPtr t) x y) m + have J2 := K sysl symL userOK j7 + refine ⟨xc, σ1, _, s3, hkc, rfl, hsp, ?_, hdec, Z.Q, Z.R, J2, Sched.BodyEndA.ofNtfnTcb J hkc hk hy rfl⟩ + rw [hbound] + exact hmem + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3TcbWordsA.lean b/proofs/Fl4ProofsA64/P3/Stage3TcbWordsA.lean new file mode 100644 index 000000000..d346765df --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3TcbWordsA.lean @@ -0,0 +1,218 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 3): **a step writing only register-range words of one TCB**. + +* **`PlainRegW`**: a word of a TCB in `T` at offset `[256, 3328)` (registers, FPU image, PAC keys). +* **`JStage3A.of_plainTcbA`**: writes inside `PlainRegW`, with the spec replacing the architecture part of one TCB, + keep the record. The caller supplies the two relations that read registers (endpoint, notification), `BlkOK` and + the facts `decodeFinA` gives. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3Lift3QA +import Fl4ProofsA64.P3.Stage3TcbMcpA +import Fl4ProofsA64.P3.Stage3TcbF0A +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-- A register-range word of a TCB in `T` (module docstring). -/ +def PlainRegW (T : Set U64) (b : U64) : Prop := ∃ t ∈ T, t.val + 256 ≤ b.val ∧ b.val < t.val + 3328 + +theorem PlainRegW.f0 {T : Set U64} {b : U64} (h : PlainRegW T b) : TcbF0WA T b := by + obtain ⟨t, ht, h1, h2⟩ := h + exact f0_rangeA ht (lo := 256) (hi := 3328) (by omega) (by omega) h1 h2 + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s s' : S} {a : A} {σ : State Unit} {m : McState} + +/-- A TCB word outside the register range is not written. -/ +theorem plainReg_keepA {T : Set U64} (hP : TcbPlaced T) (w : WritesIn st s s' (PlainRegW T)) {u : U64} (hu : u ∈ T) + (K : U64) (h1 : K.val < 4096) (h2 : K.val % 8 = 0) (h3 : K.val < 256 ∨ 3328 ≤ K.val) : + L.mem s' (wa u K) = L.mem s (wa u K) := by + have hua := hP u hu + refine w.mem_eq (field_aligned hua h1 h2) fun hb => ?_ + obtain ⟨t, ht, l, r⟩ := hb + rw [Notif.tfield_val hua h1] at l r + have a1 : t.val % 4096 = 0 := (hP t ht).align + have a2 : u.val % 4096 = 0 := hua.align + omega + +set_option maxHeartbeats 2000000 in +/-- **The record across register-range writes** (module docstring). -/ +theorem JStage3A.of_plainTcbA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {t : U64} (ht : t ∈ Z.T) {y : Tcb} (hy : σ.kheap (specPtr t) = some (.tcb y)) (g : ArchTcb) (hg : g.tcbVcpu = y.tcbArch.tcbVcpu) + (w : WritesIn st s s' (PlainRegW Z.T)) + (blk' : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s') + (sysl : SysL st H Z.Cs OS ia ie s') (symL : SymL st H Z.Cs s') + (userOK : Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s') + (j7 : J7A st (J7R2A st ar nA) Z H OS s' (updKSc σ (specPtr t) (.tcb { y with tcbArch := g })) m) + (ep : EpStateRel Z.T E Rp Bf (L.mem s') (updKSc σ (specPtr t) (.tcb { y with tcbArch := g }))) + (ntfn : NtfnStateRel N Z.T (L.mem s') (updKSc σ (specPtr t) (.tcb { y with tcbArch := g }))) : + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s' a + (updKSc σ (specPtr t) (.tcb { y with tcbArch := g })) m := by + set σ' := updKSc σ (specPtr t) (.tcb { y with tcbArch := g }) with hσ' + have hP := J.iplaced + have F : SchedFrameA σ σ' := SchedFrameA.upd_tcb (x' := { y with tcbArch := g }) hy ⟨rfl, hg, rfl⟩ + have hT : TcbPlaced Z.T := hP.e.tcb + have key : ∀ q, σ'.kheap q = + if q = specPtr t then some (.tcb { y with tcbArch := g }) else σ.kheap q := by + intro q + simp only [hσ', updKSc_kheap, Function.update_apply] + have keepO : ∀ q o, σ.kheap q = some o → (∀ z, o ≠ .tcb z) → σ'.kheap q = some o := fun q o h n1 => by + rw [key, if_neg (fun e => by rw [e, hy] at h; exact n1 y (Option.some.inj h).symm)] + exact h + have backO : ∀ q o, σ'.kheap q = some o → (∀ z, o ≠ .tcb z) → σ.kheap q = some o := fun q o h n1 => by + rw [key] at h + split at h + · exact absurd (Option.some.inj h).symm (n1 _) + · exact h + have fwdT : ∀ q z, σ.kheap q = some (.tcb z) → ∃ v, σ'.kheap q = some (.tcb v) ∧ + v = { z with tcbArch := v.tcbArch } := by + intro q z h + by_cases e : q = specPtr t + · rw [e, hy] at h + cases h + exact ⟨{ y with tcbArch := g }, by rw [key, if_pos e], rfl⟩ + · exact ⟨z, by rw [key, if_neg e]; exact h, rfl⟩ + have bwdT : ∀ q v, σ'.kheap q = some (.tcb v) → ∃ z, σ.kheap q = some (.tcb z) ∧ + v = { z with tcbArch := v.tcbArch } := by + intro q v h + rw [key] at h + split at h + · rename_i e + cases h + exact ⟨y, e ▸ hy, rfl⟩ + · exact ⟨v, h, rfl⟩ + have SR : SchedSRA st Z m κ s' σ' := by + refine J.sr.stable w (fun b hb => f0WordOffReadA J.sr.toSchedSRN hb.f0) ?_ ?_ + · refine + { fix := ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ + act := rfl + rq := rfl + rel := rfl + sc := fun q x k h => (keepO q _ h (fun _ e => by cases e)).trans h.symm + tcb := fun q x h => by + obtain ⟨v, hv, e⟩ := fwdT q x h + exact ⟨v, hv, by rw [e], by rw [e], by rw [e]⟩ + scBack := fun q x k h => (backO q _ h (fun _ e => by cases e)).trans h.symm + tcbBack := fun q v h => by + obtain ⟨z, hz, -⟩ := bwdT q v h + exact ⟨z, hz⟩ + ntfnBack := fun q z h => ⟨z, backO q _ h (fun _ e => by cases e), rfl⟩ } + · intro q x z hx hz + obtain ⟨v, hv, e⟩ := fwdT q x hx + rw [hz] at hv + cases hv + refine Or.inl ⟨by rw [e], fun h => ?_⟩ + rw [e] at h + exact h + have reply : ReplyStackRelA (L.mem s') Rp Z.C σ' (scNameA m) stack := by + refine J.reply.frame (fun r hr => ?_) (fun c _ z k h => ⟨z, k, keepO _ _ h (fun _ e => by cases e), rfl⟩) + (fun r _ x' h => ⟨x', backO _ _ h (fun _ e => by cases e), rfl⟩) + have hr3 := hP.e.rep r hr + have one : ∀ O : U64, O.val = 8 ∨ O.val = 16 → L.mem s' (wa r O) = L.mem s (wa r O) := by + intro O hO + have hv : (wa r O).val = r.val + O.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + refine w.mem_eq (by unfold Aligned; rw [hv]; omega) fun hb => ?_ + obtain ⟨u, hu, l, h⟩ := hb + have := hP.e.rt r hr u hu + omega + exact ⟨one _ (Or.inr (by simp [layout.reply.NEXT])), one _ (Or.inl (by simp [layout.reply.PREV]))⟩ + have K : IpcKeepA σ σ' := + ⟨fun p x h => by + obtain ⟨v, hv, e⟩ := fwdT p x h + exact ⟨v, hv, by rw [e], by rw [e], by rw [e], by rw [e], by rw [e], by rw [e]⟩, + fun p z k h => ⟨z, k, keepO p _ h (fun _ e => by cases e), rfl⟩, + fun p z k h => ⟨z, k, backO p _ h (fun _ e => by cases e), rfl⟩⟩ + have sinv : SInvXA σ' m n := by + refine J.sinv.of_fr + { tcb := fun q x h => by + obtain ⟨v, hv, e⟩ := fwdT q x h + exact ⟨v, hv, by rw [e], by rw [e], by rw [e]⟩ + tcb' := fun q v h => by + obtain ⟨z, hz, e⟩ := bwdT q v h + exact ⟨z, hz, by rw [e], by rw [e], by rw [e]⟩ + sc := fun q z k => ⟨fun h => backO q _ h (fun _ e => by cases e), fun h => keepO q _ h (fun _ e => by cases e)⟩ + cur := rfl + idle := rfl + csc := rfl + rq := rfl + rls := rfl + act := rfl, time := rfl, cons := rfl } ?_ + exact J.sinv.irqN.updTcbA hy rfl + have sidle : SIdleA σ' m n := + { f3 := fun c hc => by + obtain ⟨x, hx, hs⟩ := J.sidle.f3 c hc + obtain ⟨v, hv, e⟩ := fwdT _ x hx + exact ⟨v, hv, by rw [e]; exact hs⟩ + f4 := fun z k h => J.sidle.f4 z k (backO _ _ h (fun _ e => by cases e)) + f5 := fun q z k h => J.sidle.f5 q z k (backO _ _ h (fun _ e => by cases e)) + f6 := J.sidle.f6.of_eq fun sl => by + unfold specCapAtA + by_cases e : sl.1 = specPtr t + · rw [key, if_pos e, e, hy] + rfl + · rw [key, if_neg e] + f7 := fun y k h => J.sidle.f7 y k (backO _ _ h (fun _ e => by cases e)) + idleSlot := fun c hc => by + have h0 := J.sidle.idleSlot c hc + unfold vtCapA at h0 ⊢ + by_cases e : m.idleThreadOf c = specPtr t + · rw [key, if_pos e]; rw [e, hy] at h0; exact h0 + · rw [key, if_neg e]; exact h0 + idleBound := fun c hc => by + have h0 := J.sidle.idleBound c hc + unfold vcpuOfA at h0 ⊢ + by_cases e : m.idleThreadOf c = specPtr t + · rw [key, if_pos e]; rw [e, hy] at h0; show g.tcbVcpu = none; rw [hg]; exact h0 + · rw [key, if_neg e]; exact h0 } + have rv : RunVcpuInv st Z.T s' := fun u hu h0 => by + have e1 : L.mem s' (runVcpuA u) = L.mem s (runVcpuA u) := by + unfold runVcpuA + exact plainReg_keepA hT w hu 3672#u64 (by simp) (by simp) (by simp) + have e2 : L.mem s' (Fl4ProofsA64.P1.stA u) = L.mem s (Fl4ProofsA64.P1.stA u) := by + rw [Fl4ProofsA64.P1.stA_eq] + exact plainReg_keepA hT w hu 3328#u64 (by simp) (by simp) (by simp) + rw [e2]; exact J.runVcpu u hu (e1 ▸ h0) + have blkQ : BlkQueuedA Z.T E N (L.mem s') σ' := by + refine J.blkQ.keepA (fun u hu => ?_) (fun u _ x' h => ?_) (fun q z h => keepO q _ h (fun _ e => by cases e)) + (fun q z h => keepO q _ h (fun _ e => by cases e)) + · rw [Notif.tBlockA_eq]; exact plainReg_keepA hT w hu 3336#u64 (by simp) (by simp) (by simp) + · obtain ⟨z, hz, e⟩ := bwdT _ x' h + exact Or.inr ⟨z, hz, by rw [e]⟩ + have sv : SchedValidA σ' := J.schedValid.congr rfl rfl rfl rfl rfl rfl + (fun q z k h => keepO q _ h (fun _ e => by cases e)) + (fun u x h => by + obtain ⟨v, hv, e⟩ := fwdT u x h + exact ⟨v, hv, by rw [e]⟩) + (fun u v h => by + obtain ⟨z, hz, e⟩ := bwdT u v h + exact ⟨z, hz, by rw [e], by rw [e]⟩) + have Qk : QKeepA σ σ' := QKeepA.ofBwd fun q o h => by + rw [key] at h + split at h + · rename_i e + cases h + exact ⟨.tcb y, by rw [e]; exact hy, QObjA.ofTcb rfl⟩ + · exact ⟨o, h, QObjA.refl o⟩ + exact J.of_ipcStepA (Q := Z.Q) (Rl := Z.R) (curOf := Z.curOf) (curScOf := Z.curScOf) + (w.mono fun b hb => hb.f0.stepN hT) F.1 F.2.1 Qk F.2.2 + (fun _ _ => rfl) (BlkOK.bxi blk') sysl symL userOK j7 SR ep ntfn reply K + (fun u hu O h h8 => plainReg_keepA hT w hu O (by omega) h8 (by omega)) + sinv sidle rv blkQ sv + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3TimeoutFootA.lean b/proofs/Fl4ProofsA64/P3/Stage3TimeoutFootA.lean new file mode 100644 index 000000000..def6ceb8b --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3TimeoutFootA.lean @@ -0,0 +1,63 @@ +/- +P3 (aarch64, A64-P3 Stage-3 integration, D8/T1 step 2): **the footprint of the timeout send** (`faults.handle_timeout`, +`EntryTimeoutA.send`, the `PartFrA.wr` of the `check_budget` timeout-send frame). + +`faults.handle_timeout` reads the timeout-handler slot, stores the four timeout-fault words of the TCB +(`timeoutFaultWordsA`, offsets 3376..3400: `TcbStepWA`), then runs `send_ipc` (blocking, no call) on the handler +endpoint. **`handleTimeout_footA`**: if that `send_ipc` (at the faulted state) writes inside `W`, the whole +`handle_timeout` writes inside `TcbStepWA ∪ W`. **`handleTimeout_stepNA`**: the `send_ipc` footprint is +`SchedStepNWA` of its own entry state `(s1, σ1)`; the `handle_timeout` footprint is `SchedStepNWA` of `(s, σ)` when the +spec states differ by a TCB write (`SSzA`). + +No named premise. +-/ +import Fl4ProofsA64.P3.Stage3IpcFootA +import Fl4ProofsA64.P3.Stage3PartialA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4ProofsA64.P3.Sched + +noncomputable section + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +theorem handleTimeout_footA {T : Set U64} (hP : TcbPlaced T) {W : U64 → Prop} {s : S} {a : A} + {t faultBadge ep handlerBadge slot : U64} {send recv grant grantReply : Bool} (ht : t ∈ T) + (hslot : obj.Tcb.slot t layout.tcb.SLOT_TIMEOUT_HANDLER = ok slot) + (hc : obj.Slot.cap st slot s = ok (.Endpoint ep handlerBadge send recv grant grantReply)) + (deliver : endpoint.send_ipc st ar (timeoutFaultWordsA st s t faultBadge) a true false handlerBadge + grant grantReply false t ep ⦃ r => WritesIn st (timeoutFaultWordsA st s t faultBadge) r.1 W ⦄) : + faults.handle_timeout st ar s a t (.Timeout faultBadge) ⦃ r => + WritesIn st s r.1 (fun b => TcbStepWA T b ∨ W b) ⦄ := by + unfold faults.handle_timeout faults.send_fault_ipc + simp only [hslot, bind_ok, hc, setTimeoutFault_exactA, uncurry_apply_pair, Std.bind_assoc] + refine WP.spec_bind deliver ?_ + rintro ⟨s', a'⟩ hw + simp only [bind_ok, uncurry_apply_pair, WP.spec_ok] + exact (WritesIn.trans (timeoutFaultWordsA_wr hP ht s faultBadge) hw) + +/-- The `handle_timeout` footprint as a Stage-3 write set of its entry state, from the `send_ipc` footprint at the +faulted state. -/ +theorem handleTimeout_stepNA {N E Rp : Set U64} {Z : XSetsA} {m : Fl4SpecA64.McState} (hP : TcbPlaced Z.T) + {s : S} {a : A} {σ σ1 : Fl4SpecA64.State Unit} (hz : SSzA σ σ1) + {t faultBadge ep handlerBadge slot : U64} {send recv grant grantReply : Bool} (ht : t ∈ Z.T) + (hslot : obj.Tcb.slot t layout.tcb.SLOT_TIMEOUT_HANDLER = ok slot) + (hc : obj.Slot.cap st slot s = ok (.Endpoint ep handlerBadge send recv grant grantReply)) + (deliver : endpoint.send_ipc st ar (timeoutFaultWordsA st s t faultBadge) a true false handlerBadge + grant grantReply false t ep ⦃ r => + WritesIn st (timeoutFaultWordsA st s t faultBadge) r.1 + (SchedStepNWA (st := st) N E Rp Z m σ1 (timeoutFaultWordsA st s t faultBadge)) ⦄) : + faults.handle_timeout st ar s a t (.Timeout faultBadge) ⦃ r => + WritesIn st s r.1 (SchedStepNWA (st := st) N E Rp Z m σ s) ⦄ := by + refine WP.spec_mono (handleTimeout_footA hP ht hslot hc deliver) ?_ + intro r hw + have hk : L.ksb (timeoutFaultWordsA st s t faultBadge) = L.ksb s := (timeoutFaultWordsA_wr hP ht s faultBadge).1 + exact hw.mono fun b hb => hb.elim (fun h => Or.inl (Or.inr h)) (fun h => schedStepN_backA hz hk h) + +end + +end + +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/Stage3TimeoutSendBA.lean b/proofs/Fl4ProofsA64/P3/Stage3TimeoutSendBA.lean new file mode 100644 index 000000000..b1ae0bd55 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3TimeoutSendBA.lean @@ -0,0 +1,528 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2b): **`handle_timeout` of the current thread after the charge head** (kernel +entry 2's `hSend`, the handler endpoint with a waiting receiver on the current core). + +The facts are kernel entry 2's at the state after the charge head of `charge_budget`: `SchedSRB … t` (the current +thread `t`, the timeout sender, excepted from `released_ipc_queues`), `McpRelA`, `YieldRelA`, `EpStateRel` and the +IPC placement carried from the record. + +* `faultStepExA`, `faultWords_wrA`, `faultOffReadA`, `SchedSRB.faultTmoA`, `EpStateRel.faultTmoA`, + `slot_not_faultA`: `set_fault … (.Timeout b)` (`timeoutFaultWordsA`, four TCB words) keeps `SchedSRB … t` and moves + `EpStateRel` to the faulted TCB. +* `fl4HandleTimeout_sendA`: the spec's `handle_timeout` is the faulted `send_ipc` (one outcome). +* **`handle_timeout_recvBA`**: the program, one spec outcome of `fl4HandleTimeout`, `YieldQueueRelA` (queues + existential), `EpStateRel`, the write set (fault words and `TmoFpA` of the entry state; `tmoFpA_splitA`: inside + `IpcFpA`) and `SSzA`; also the faulted `send_ipc`'s outcome. + +Named premises: [P] `hmax`, `htu`, `hconv` (`ipcSchedOpsA`'s and `fl4SendIpc_eq_l4v`'s). Case selectors: the handler +cap (`hslot`, `hcap`, `hgc`) and a receiver `d` waiting on its endpoint on the current core (`hk`, `hon`), the sender's +SC (`hsc0`, `hsc`, `hky`). Record inputs: `hV` (`SlotsValidA`), `hal` (the receiver's buffer alignment), `hD` +(`DonA`). New module. +-/ +import Fl4ProofsA64.P3.EpSendTmoBA +import Fl4ProofsA64.P3.Stage3SendDonateKeepA +import Fl4ProofsA64.P3.Stage3TimeoutFootA +import Fl4ProofsA64.P3.IpcScReplyTimeoutA +import Fl4ProofsA64.P3.DelNtfnBufA +import Fl4ProofsA64.P3.BufNativeFrA +import Fl4ProofsA64.P3.ScYieldFrameA +import Fl4ProofsA64.P3.MoveA +import Fl4ProofsA64.P3.Stage3SendFaultSpecA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-- Writing a TCB's fault: an `SStepA` step keeping the queues runnable and `RdExA` at that TCB. -/ +theorem faultStepExA {σ : State Unit} {p : ObjRef} {x : Tcb} (hk : σ.kheap p = some (.tcb x)) (f : Option Fault) + (hrun : RunQA σ) (hrd : RdExA σ p) : + Sched.SStepA σ (updK σ p (.tcb { x with tcbFault := f })) ∧ RunQA (updK σ p (.tcb { x with tcbFault := f })) ∧ + RdExA (updK σ p (.tcb { x with tcbFault := f })) p := by + let σ' := updK σ p (.tcb { x with tcbFault := f }) + have hh : σ'.kheap p = some (.tcb { x with tcbFault := f }) := by simp [σ'] + have far : ∀ q, q ≠ p → σ'.kheap q = σ.kheap q := fun q hq => by simp [σ', hq] + have hS : Sched.SStepA σ σ' := by + refine ⟨⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩, rfl, rfl, rfl, + fun q y n hq => far q (fun e => by rw [e, hk] at hq; cases hq), + fun q y hq => ?_, fun q y n hq => (far q (fun e => by rw [e, hh] at hq; cases hq)).symm, + fun q y hq => ?_, fun q y hq => ?_⟩ + · by_cases e : q = p + · subst q + rw [hk] at hq + cases hq + exact ⟨_, hh, rfl, rfl, rfl⟩ + · exact ⟨y, (far _ e).trans hq, rfl, rfl, rfl⟩ + · by_cases e : q = p + · exact ⟨x, e.symm ▸ hk⟩ + · exact ⟨y, (far _ e).symm.trans hq⟩ + · have hq' := far q (fun e => by rw [e, hh] at hq; cases hq) + exact ⟨y, hq'.symm.trans hq, rfl⟩ + have k : ∀ u, (∃ y, σ.kheap u = some (.tcb y) ∧ runnable y.tcbState = true) → + ∃ y, σ'.kheap u = some (.tcb y) ∧ runnable y.tcbState = true := by + rintro u ⟨y, hy, hr⟩ + by_cases e : u = p + · subst u + rw [hk] at hy + cases hy + exact ⟨_, hh, hr⟩ + · exact ⟨y, (far _ e).trans hy, hr⟩ + refine ⟨hS, ⟨fun d q u hu => k u (hrun.1 d q u hu), fun u hu => k u (hrun.2 u hu)⟩, ?_⟩ + intro u y hu hy hc + have hy0 : σ.kheap u = some (.tcb y) := (far _ hu).symm.trans hy + exact (hrd u y hu hy0 hc).step hS hy0 + +/-- The four words `set_fault … (.Timeout b)` stores. -/ +def FaultWA (t b : U64) : Prop := b = tFaultA t ∨ b = tFw0A t ∨ b = tFw1A t ∨ b = tFw2A t + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +theorem faultWords_wrA {T : Set U64} (hP : TcbPlaced T) {t : U64} (ht : t ∈ T) (s : S) (badge : U64) : + WritesIn st s (timeoutFaultWordsA st s t badge) (FaultWA t) := by + have hta := hP t ht + unfold timeoutFaultWordsA + refine (((writesIn_wr (a_fault hta).1).trans (writesIn_wr (a_fw0 hta).1)).trans + ((writesIn_wr (a_fw1 hta).1).trans (writesIn_wr (a_fw2 hta).1))).mono fun b hb => ?_ + rcases hb with (hb | hb) | (hb | hb) + · exact Or.inl hb + · exact Or.inr (Or.inl hb) + · exact Or.inr (Or.inr (Or.inl hb)) + · exact Or.inr (Or.inr (Or.inr hb)) + +theorem faultWA_eq {t b : U64} (hb : FaultWA t b) : + ∃ K : U64, (K = 3376#u64 ∨ K = 3384#u64 ∨ K = 3392#u64 ∨ K = 3400#u64) ∧ b = wa t K := by + rcases hb with rfl | rfl | rfl | rfl + · exact ⟨_, Or.inl rfl, rfl⟩ + · exact ⟨_, Or.inr (Or.inl rfl), rfl⟩ + · exact ⟨_, Or.inr (Or.inr (Or.inl rfl)), rfl⟩ + · exact ⟨_, Or.inr (Or.inr (Or.inr rfl)), rfl⟩ + +/-- A TCB word away from the four fault words is not one of them. -/ +theorem not_faultWA {t u K : U64} (ht : TcbAddr t) (hu : TcbAddr u) (hK : K.val < 4096) + (hK' : K.val ≠ 3376 ∧ K.val ≠ 3384 ∧ K.val ≠ 3392 ∧ K.val ≠ 3400) : ¬ FaultWA t (wa u K) := by + intro hb + obtain ⟨K', hK2, e⟩ := faultWA_eq hb + rcases hK2 with rfl | rfl | rfl | rfl <;> + exact field_ne hu ht hK (by simp) (fun _ => by simp; omega) e + +/-- The fault words are outside what `SchedSRA` reads. -/ +theorem faultOffReadA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRN st Z m κ s σ) {t : U64} (ht : t ∈ Z.T) {b : U64} (hb : FaultWA t b) : + ¬ SReadA st Z m s σ b := by + obtain ⟨K, hK, rfl⟩ := faultWA_eq hb + have htA := H.core.tcbs t ht + have hfit := htA.fits + have hKv : 3376 ≤ K.val ∧ K.val ≤ 3400 := by rcases hK with rfl | rfl | rfl | rfl | rfl <;> simp + have hst : (wa t K).val = t.val + K.val := field_val htA (by omega) + have hk := H.tcb_ks ht + have hks : ∀ b : U64, (L.ksb s).val ≤ b.val → b.val < (L.ksb s).val + ksSizeA → b ≠ wa t K := by + intro b hb hb' e + rw [e, hst] at hb hb' + omega + have glob : ∀ k : U64, k.val < 223488 → core.num.U64.wrapping_add (L.ksb s) k ≠ wa t K := by + intro k ho + have hfit := H.ksFit + unfold ksSizeA at hfit + have hv : (core.num.U64.wrapping_add (L.ksb s) k).val = (L.ksb s).val + k.val := by + rw [wa_valm] + omega + exact hks _ (by rw [hv]; omega) (by unfold ksSizeA; rw [hv]; omega) + intro hr + rcases hr with ⟨c, hc, o, ho, e⟩ | e | e | ⟨u, hu, k, hK', e⟩ | hf + · obtain ⟨n, -, hn, hcs⟩ := H.csN + have hc' := (hcs _).1 hc + have hv := nodeAt_valSc H.ksFit (show c.val < 128 by omega) ho + exact hks _ (by rw [hv]; omega) (by unfold ksSizeA; rw [hv]; omega) e.symm + · exact glob _ (by simp) e.symm + · exact glob _ (by simp) e.symm + · rcases hK with rfl | rfl | rfl | rfl | rfl <;> rcases hK' with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> + exact field_ne htA (H.core.tcbs u hu) (by simp) (by simp) (fun _ => by simp) e + · have hn := H.scp.th_out hf ((H.thT t).2 ht) + simp only [ObjKindA.size] at hn + omega + +/-- `set_fault` of the timeout fault keeps `SchedSRB … t`. -/ +theorem SchedSRB.faultTmoA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} {t : U64} + (H : SchedSRB st Z m κ s σ (specPtr t)) (ht : t ∈ Z.T) {x : Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + (badge : U64) (f : Option Fault) : + SchedSRB st Z m κ (timeoutFaultWordsA st s t badge) (updK σ (specPtr t) (.tcb { x with tcbFault := f })) + (specPtr t) := by + obtain ⟨hS, hrun, hrd⟩ := faultStepExA hk f H.run H.rdEx + exact H.stableExBA (faultWords_wrA (st := st) H.core.tcbs ht s badge) (fun _ hb => faultOffReadA H.toSchedSRN ht hb) hS + hrun hrd + +/-- `set_fault` of the timeout fault moves `EpStateRel` to the faulted TCB. -/ +theorem EpStateRel.faultTmoA {T E R : Set U64} {Bf : Set (U64 × Word)} {s : S} {σ : State Unit} + (hR : EpStateRel T E R Bf (L.mem s) σ) (hP : EPlaced T E R Bf) {t : U64} (ht : t ∈ T) {x : Tcb} + (hk : σ.kheap (specPtr t) = some (.tcb x)) (badge : U64) : + EpStateRel T E R Bf (L.mem (timeoutFaultWordsA st s t badge)) + (updK σ (specPtr t) (.tcb { x with tcbFault := some (.timeout badge.bv) })) := by + have hta := hP.tcb t ht + have w := faultWords_wrA (st := st) hP.tcb ht s badge + have fw : ∀ b, FaultWA t b → TF t b := by + rintro b (rfl | rfl | rfl | rfl) + · exact (a_fault hta).2 + · exact (a_fw0 hta).2 + · exact (a_fw1 hta).2 + · exact (a_fw2 hta).2 + have keep : ∀ {K : U64}, K.val < 4096 → K.val % 8 = 0 → + (K.val ≠ 3376 ∧ K.val ≠ 3384 ∧ K.val ≠ 3392 ∧ K.val ≠ 3400) → + L.mem (timeoutFaultWordsA st s t badge) (wa t K) = L.mem s (wa t K) := + fun h1 h2 h3 => w.mem_eq (field_aligned hta h1 h2) (not_faultWA hta hta h1 h3) + have hv : ∀ K : U64, K.val < 4096 → (wa t K).val = t.val + K.val := fun K hK => field_val hta hK + have v0 : L.mem (timeoutFaultWordsA st s t badge) (tFaultA t) = 5#u64 := by + unfold timeoutFaultWordsA + rw [show faultA t = tFaultA t from rfl] + rw [L.mem_wr_ne (a_fw2 hta).1 (a_fault hta).1 (by unfold tFw2A tFaultA; rw [hv _ (by simp), hv _ (by simp)]; simp), + L.mem_wr_ne (a_fw1 hta).1 (a_fault hta).1 (by unfold tFw1A tFaultA; rw [hv _ (by simp), hv _ (by simp)]; simp), + L.mem_wr_ne (a_fw0 hta).1 (a_fault hta).1 (by unfold tFw0A tFaultA; rw [hv _ (by simp), hv _ (by simp)]; simp), + L.mem_wr_same (a_fault hta).1] + unfold fl4_abi.consts.fault.Timeout; rfl + have v1 : L.mem (timeoutFaultWordsA st s t badge) (tFw0A t) = badge := by + unfold timeoutFaultWordsA + rw [L.mem_wr_ne (a_fw2 hta).1 (a_fw0 hta).1 (by unfold tFw2A tFw0A; rw [hv _ (by simp), hv _ (by simp)]; simp), + L.mem_wr_ne (a_fw1 hta).1 (a_fw0 hta).1 (by unfold tFw1A tFw0A; rw [hv _ (by simp), hv _ (by simp)]; simp), + L.mem_wr_same (a_fw0 hta).1] + have hxt := hR.at_tcb ht hk + have hx : TcbRelE R (L.mem (timeoutFaultWordsA st s t badge)) t { x with tcbFault := some (.timeout badge.bv) } := by + refine ⟨?_, fun e h => ?_, fun e d h => ?_, fun e r d h => ?_, fun r h => ?_, fun r => ?_, ?_, ?_, ?_, ?_, ?_⟩ + · rw [stA_eq, keep (K := 3328#u64) (by simp) (by simp) (by simp), ← stA_eq]; exact hxt.st + · rw [tBlockA, keep (K := 3336#u64) (by simp) (by simp) (by simp)]; exact hxt.blk e h + · rw [tBadgeA, tFlagsA, keep (K := 3344#u64) (by simp) (by simp) (by simp), + keep (K := 3352#u64) (by simp) (by simp) (by simp)] + exact hxt.send e d h + · rw [tReplyA, keep (K := 3360#u64) (by simp) (by simp) (by simp)]; exact hxt.recv e r d h + · rw [tReplyA, keep (K := 3360#u64) (by simp) (by simp) (by simp)]; exact hxt.reply r h + · rw [w.mem_eq (a_reg hta r).1 fun hb => ?_] + · exact hxt.regs r + · obtain ⟨K, hK, e⟩ := faultWA_eq hb + have h1 := regA_val hta (regIdxA_lt r) + have h2 := regIdxA_lt r + rw [e, hv K (by rcases hK with rfl | rfl | rfl | rfl | rfl <;> simp)] at h1 + rcases hK with rfl | rfl | rfl | rfl | rfl <;> simp at h1 <;> omega + · rw [prioA, keep (K := 3448#u64) (by simp) (by simp) (by simp)]; exact hxt.prio + · rw [v0]; simp + · rw [v0]; exact ⟨fun _ => ⟨badge.bv, rfl⟩, fun _ => rfl⟩ + · rw [tBoundA, keep (K := 3368#u64) (by simp) (by simp) (by simp)]; exact hxt.bound + · unfold FaultRel + rw [v0, v1] + exact ⟨rfl, rfl⟩ + refine hR.upd_tcb hP ht _ (fun b hb hn => w.mem_eq hb fun h => hn.2 (fw b h)) (Or.inr ⟨?_, ?_⟩) hx + · rw [stA_eq]; exact keep (K := 3328#u64) (by simp) (by simp) (by simp) + · rw [tBlockA]; exact keep (K := 3336#u64) (by simp) (by simp) (by simp) + +/-- A slot's words are not a TCB's fault words. -/ +theorem slot_not_faultA {th : TypedHeapA} {kb : U64} (hP : PlaceA th kb) {t : U64} (hth : th t = some .tcb) + (hta : TcbAddr t) {y b : U64} (hy : IsSlotA th y) (y1 : y.val ≤ b.val) (y2 : b.val < y.val + 32) : + ¬ FaultWA t b := by + intro hb + have obj : ∃ p k, th p = some k ∧ p.val ≤ y.val ∧ y.val + 32 ≤ p.val + k.size ∧ + (k = .tcb → y.val + 32 ≤ p.val + 224) := by + obtain ⟨p, ⟨r, hr, i, hi, rfl⟩ | ⟨ht, i, hi, rfl⟩⟩ := hy + · obtain ⟨k, hk, hs, hv⟩ := slotVA_of hP hr (j := i) (by rw [cnode_size]; omega) + exact ⟨p, k, hk, by omega, by omega, fun e => by rw [e, hr] at hk; cases hk⟩ + · obtain ⟨k, hk, hs, hv⟩ := tcb_slotV hP ht hi + have : i < 7 := hi + exact ⟨p, k, hk, by omega, by omega, fun _ => by omega⟩ + obtain ⟨p, k, hk, p1, p2, pt⟩ := obj + obtain ⟨K, hK, rfl⟩ := faultWA_eq hb + have hbv : (wa t K).val = t.val + K.val := field_val hta (by rcases hK with rfl | rfl | rfl | rfl | rfl <;> simp) + have hKv : 3376 ≤ K.val ∧ K.val ≤ 3400 := by rcases hK with rfl | rfl | rfl | rfl | rfl <;> simp + by_cases e : p = t + · subst e + rw [hth] at hk + cases hk + have := pt rfl + omega + · have := hP.apart p t k .tcb hk hth e + have : ObjKindA.size .tcb = 4096 := rfl + omega + +end + +section Spec + +/-- **The spec's `handle_timeout` with a valid endpoint handler is the faulted `send_ipc`** (one outcome). -/ +theorem fl4HandleTimeout_sendA (hcap : fl4MaxConsumedTicks = maxTicksToUs) (hconv : Fl4TicksToUsLegacyAgree) + {σ σ' : State Unit} {t ep : ObjRef} {b badge : Word} {x : Tcb} {send recv grant grantReply : Bool} + (hk : σ.kheap t = some (.tcb x)) + (hc : getCap (Z := Unit) (getTcbTimeoutHandlerPtr t) σ = + ({(.endpointCap ep b (rightsOfA send recv grant grantReply), σ)}, False)) + (hs : sendIpc (Z := Unit) true false b grant grantReply false t ep + (updK σ t (.tcb { x with tcbFault := some (.timeout badge) })) = ({((), σ')}, False)) : + fl4HandleTimeout (Z := Unit) t (.timeout badge) σ = ({((), σ')}, False) := by + have valid : isValidTimeoutHandler (Z := Unit) t σ = ({(true, σ)}, False) := by + unfold isValidTimeoutHandler + rw [nd_pure_bindSc, nd_bind_oneSc hc] + rfl + unfold fl4HandleTimeout + rw [nd_bind_oneSc valid, nd_bind_oneSc (assert_trueSc (show true = true from rfl) σ), + nd_pure_bindSc, nd_bind_oneSc hc] + have hg (d : Decidable (Rights.allowGrant ∈ rightsOfA send recv grant grantReply)) : + @decide _ d = grant := by + apply Bool.eq_iff_iff.2 + simp only [decide_eq_true_eq] + rfl + have hgr (d : Decidable (Rights.allowGrantReply ∈ rightsOfA send recv grant grantReply)) : + @decide _ d = grantReply := by + apply Bool.eq_iff_iff.2 + simp only [decide_eq_true_eq] + rfl + simp only [fl4SendFaultIpc, capEpBadge, capEpPtr, hg, hgr, nd_bind_assocSc] + rw [nd_bind_oneSc (threadSet_eqSc _ hk), fl4SendIpc_eq_l4v hcap hconv] + first + | exact (nd_bind_oneSc hs _).trans rfl + | exact (nd_bind_oneSc (show (sendIpc (Z := Unit) true false b grant grantReply false t ep >>= + fun _ => (pure true : SMonad Unit Bool)) (updK σ t (.tcb { x with tcbFault := some (.timeout badge) })) = + ({(true, σ')}, False) from (nd_bind_oneSc hs _).trans rfl) _).trans rfl + +end Spec + +section Main +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + +/-- `TmoFpA` (the timeout send's write set) is inside `IpcFpA`. -/ +theorem tmoFpA_splitA {σ : State Unit} {s : S} (hP : TcbPlaced Z.T) {t e d b : U64} (ht : t ∈ Z.T) (he : e ∈ E) (hd : d ∈ Z.T) + (h : TmoFpA st Z m R Bf σ s t e d b) : IpcFpA st Z E R Bf m σ s b := by + have tw : ∀ u ∈ Z.T, ∀ c : U64, Aligned c ∧ TF u c → TWOffN (c.val - u.val) → IpcFpA st Z E R Bf m σ s c := + fun u hu c h w => Or.inl (Or.inr (Or.inr (Or.inl ⟨u, hu, Or.inl ⟨h.2.1, w⟩⟩))) + have stw : ∀ u ∈ Z.T, IpcFpA st Z E R Bf m σ s (stA u) := fun u hu => by + refine tw u hu _ (a_st (hP u hu)) ?_ + rw [val_st (hP u hu), Nat.add_sub_cancel_left]; unfold TWOffN; simp + rcases h with (h | ⟨r, hr, h⟩ | ⟨-, h⟩ | h | h) | h + · rcases h with h | ⟨w, hw, -, h⟩ | ⟨r, h⟩ | h + · exact Or.inl (Or.inl ⟨e, he, h⟩) + · exact Or.inl (Or.inr (Or.inr (Or.inl ⟨w, hw, Or.inr h⟩))) + · have hv := regA_val (hP d hd) (regIdxA_lt r) + have := regIdxA_lt r + refine Or.inl (Or.inr (Or.inr (Or.inl ⟨d, hd, Or.inl ⟨by omega, ?_⟩⟩))) + unfold TWOffN; omega + · exact Or.inl (Or.inr (Or.inr (Or.inr h))) + · exact Or.inl (Or.inr (Or.inl ⟨r, hr, by rw [h, Endp.rTcbA_val]⟩)) + · rw [h]; exact stw t ht + · rw [h]; exact stw d hd + · exact Or.inr h + · rw [h] + refine tw t ht _ (a_reply (hP t ht)) ?_ + rw [val_reply (hP t ht), Nat.add_sub_cancel_left]; unfold TWOffN; simp + +set_option maxHeartbeats 2000000 in +/-- **`handle_timeout` of the current thread after the charge head** (module docstring): the handler endpoint `ep` +has the receiver `d` waiting; the timeout sender `t` (excepted from `released_ipc_queues`, `H`) is blocked by the +send (reply push or `Inactive`), so `SchedSRA` (in `YieldQueueRelA`) holds after. + +[P] `hmax`, `htu`, `hconv`. -/ +theorem handle_timeout_recvBA {s : S} {σ : State Unit} (a : A) + (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + {kb : U64} (hPl : PlaceA Z.th kb) (hSt : StateRelA Z.th (L.mem s) σ) (hV : SlotsValidA Z.th (L.mem s)) + (hC : BufCover st Z.T Bf s σ) (hNative : BufNativeA Z.T (L.mem s)) + (hoff : ∀ y, IsSlotA Z.th y → ¬ EWrite Z.T E R Bf y ∧ ¬ EWrite Z.T E R Bf (wa y 8#u64)) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) (hconv : Fl4TicksToUsLegacyAgree) + {t : U64} (H : SchedSRB st Z m κ s σ (specPtr t)) (hM : McpRelA Z.T m (L.mem s) σ) + (hY : YieldRelA Z.T Z.C m (L.mem s) σ) + (ht : t ∈ Z.T) {xt : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hRun : SndRunA σ (specPtr t) xt) (hD : DonA σ (specPtr t) xt) + {slot ep hb : U64} {sd rv g gr : Bool} + (hslot : obj.Tcb.slot t layout.tcb.SLOT_TIMEOUT_HANDLER = ok slot) + (hcap : obj.Slot.cap st slot s = ok (.Endpoint ep hb sd rv g gr)) + (hgc : getCap (Z := Unit) (getTcbTimeoutHandlerPtr (specPtr t)) σ = + ({(.endpointCap (specPtr ep) hb.bv (rightsOfA sd rv g gr), σ)}, False)) + (he : ep ∈ E) {d : U64} {q : List ObjRef} + (hk : σ.kheap (specPtr ep) = some (.endpoint (.recvEP (specPtr d :: q)))) + (hd : d ∈ Z.T) {xd : Tcb} (hkd : σ.kheap (specPtr d) = some (.tcb xd)) + (hu : ∀ rp, ipcBufOfA true xd = some rp → ∀ k, k < 128 → inUserFrame (bufAddr rp k) σ) + (hal : ∀ rb, ipcBufOfA true xd = some rb → (bufAddr rb 0).toNat % 8 = 0) + (hrep : RepOKA σ (specPtr d)) (hon : L.mem s (affA d) = L.cpu s) + (hni : ∀ c < fl4A64Platform.numNodes, specPtr t ≠ m.idleThreadOf c) (hsc0 : L.mem s (scA t) ≠ 0#u64) + (hsc : xt.tcbSchedContext = some (specPtr (L.mem s (scA t)))) {y : SchedContext} {n : Nat} + (hky : σ.kheap (specPtr (L.mem s (scA t))) = some (.schedContext y n)) (badge : U64) : + faults.handle_timeout st ar s a t (.Timeout badge) ⦃ r => r.2 = a ∧ ∃ τ, + fl4HandleTimeout (Z := Unit) (specPtr t) (.timeout badge.bv) σ = ({((), τ)}, False) ∧ + sendIpc (Z := Unit) true false hb.bv g gr false (specPtr t) (specPtr ep) + (updK σ (specPtr t) (.tcb { xt with tcbFault := some (.timeout badge.bv) })) = ({((), τ)}, False) ∧ + (∃ Q' R', YieldQueueRelA (st := st) { Z with Q := Q', R := R' } m κ r.1 τ) ∧ + EpStateRel Z.T E R Bf (L.mem r.1) τ ∧ + WritesIn st s r.1 (fun b => FaultWA t b ∨ TmoFpA st Z m R Bf σ s t ep d b) ∧ SSzA σ τ ⦄ := by + have hta := PE.tcb t ht + have hth : Z.th t = some .tcb := (H.thT t).2 ht + have w1 := faultWords_wrA (st := st) PE.tcb ht s badge + let x1 : Tcb := { xt with tcbFault := some (.timeout badge.bv) } + let σ1 := updK σ (specPtr t) (.tcb x1) + have keep1 : ∀ b, Aligned b → ¬ FaultWA t b → L.mem (timeoutFaultWordsA st s t badge) b = L.mem s b := + fun b hb hn => w1.mem_eq hb hn + have tw : ∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → + (K.val ≠ 3376 ∧ K.val ≠ 3384 ∧ K.val ≠ 3392 ∧ K.val ≠ 3400) → + L.mem (timeoutFaultWordsA st s t badge) (wa u K) = L.mem s (wa u K) := + fun u hu K h1 h2 h3 => keep1 _ (field_aligned (PE.tcb u hu) h1 h2) (not_faultWA hta (PE.tcb u hu) h1 h3) + have far : ∀ p, p ≠ specPtr t → σ1.kheap p = σ.kheap p := fun p hp => by simp [σ1, hp] + have hkt1 : σ1.kheap (specPtr t) = some (.tcb x1) := by simp [σ1] + have hdt : specPtr d ≠ specPtr t := by + obtain ⟨-, xs, hxs0, hq, -, hB⟩ := HE.at_ep he hk + obtain ⟨d', xs', rfl⟩ := List.exists_cons_of_ne_nil hxs0 + simp only [List.map_cons, List.cons.injEq] at hq + have hdd : d = d' := specPtr_inj hq.1 + subst hdd + have hd3 : (L.mem s (stA d)).val = 3 := (hB d (by simp)).1 + intro e + have e' : d = t := specPtr_inj e + subst e' + exact (runnable_codeA hRun.1).1 ((HE.at_tcb ht hkt).st.symm.trans hd3) + -- `set_fault` + have H1 : SchedSRB st Z m κ (timeoutFaultWordsA st s t badge) σ1 (specPtr t) := SchedSRB.faultTmoA H ht hkt badge _ + have HE1 : EpStateRel Z.T E R Bf (L.mem (timeoutFaultWordsA st s t badge)) σ1 := EpStateRel.faultTmoA HE PE ht hkt badge + have capK : ∀ y, IsSlotA Z.th y → + capAtA (L.mem (timeoutFaultWordsA st s t badge)) y = capAtA (L.mem s) y := by + intro y hy + obtain ⟨h32, hfit⟩ := IsSlotA.al32 hPl hy + have al : Aligned y := by unfold Aligned; omega + have v8 : (wa y 8#u64).val = y.val + 8 := by + rw [wa_valm] + have h8 : (8#u64 : U64).val = 8 := by simp + omega + unfold capAtA + rw [keep1 y al (slot_not_faultA hPl hth hta hy (le_refl _) (by omega)), + keep1 _ (wa_al al (by simp)) (slot_not_faultA hPl hth hta hy (by rw [v8]; omega) (by rw [v8]; omega))] + have hSt1 : StateRelA Z.th (L.mem (timeoutFaultWordsA st s t badge)) σ1 := by + have h := hSt.congrA (mem' := L.mem (timeoutFaultWordsA st s t badge)) fun y hy => capK y (slotW_isSlot hPl hy) + refine ⟨fun p r hp => ?_, fun u hu => ?_⟩ + · obtain ⟨cs, h1, h2, h3⟩ := h.cnode p r hp + have hne : specPtr p ≠ specPtr t := fun e => by rw [e, hkt] at h1; cases h1 + exact ⟨cs, by rw [far _ hne]; exact h1, h2, h3⟩ + · obtain ⟨x0, h1, h2⟩ := h.tcb u hu + by_cases e : specPtr u = specPtr t + · rw [e, hkt] at h1 + cases h1 + exact ⟨x1, by rw [e]; exact hkt1, h2⟩ + · exact ⟨x0, by rw [far _ e]; exact h1, h2⟩ + have hV1 : SlotsValidA Z.th (L.mem (timeoutFaultWordsA st s t badge)) := fun y hy => by + rw [capK y hy]; exact hV y hy + have hC1 : BufCover st Z.T Bf (timeoutFaultWordsA st s t badge) σ1 := + BufCover.frameK hC (fun u hu => by rw [ibWordA_eq]; exact tw u hu _ (by simp) (by simp) (by simp)) + (fun u hu => by + show capAtA (L.mem (timeoutFaultWordsA st s t badge)) (sbWordA u) = capAtA (L.mem s) (sbWordA u) + unfold capAtA + rw [sbWordA8_eq, sbWordA_eq, tw u hu (64#u64) (by simp) (by simp) (by simp), + tw u hu (72#u64) (by simp) (by simp) (by simp)]) + (fun u hu => let ⟨x, hx, _⟩ := HE.tcb u hu; ⟨x, hx⟩) + (fun p x hx => by + by_cases e : p = specPtr t + · rw [e, hkt] at hx + cases hx + exact ⟨x1, by rw [e]; exact hkt1, rfl, rfl⟩ + · exact ⟨x, by rw [far _ e]; exact hx, rfl, rfl⟩) + have hN1 : BufNativeA Z.T (L.mem (timeoutFaultWordsA st s t badge)) := + hNative.writesIn PE.tcb w1 fun u hu K hK => + not_faultWA hta (PE.tcb u hu) (by rcases hK with rfl | rfl | rfl <;> simp) (by rcases hK with rfl | rfl | rfl <;> simp) + have hk1 : σ1.kheap (specPtr ep) = some (.endpoint (.recvEP (specPtr d :: q))) := by + rw [far _ (fun e => by rw [e, hkt] at hk; cases hk)]; exact hk + have hkd1 : σ1.kheap (specPtr d) = some (.tcb xd) := by rw [far _ hdt]; exact hkd + have hu1 : ∀ rp, ipcBufOfA true xd = some rp → ∀ k, k < 128 → inUserFrame (bufAddr rp k) σ1 := + fun rp hrp k hk' => inUserFrame_updK hkt (fun _ h => by cases h) _ (hu rp hrp k hk') + have hrep1 : RepOKA σ1 (specPtr d) := by + intro x hx e0 r dd hst + rw [far _ hdt] at hx + obtain ⟨xr, hkr, hbk⟩ := hrep x hx e0 r dd hst + exact ⟨xr, by rw [far _ (fun e => by rw [e, hkt] at hkr; cases hkr)]; exact hkr, hbk⟩ + have hon1 : L.mem (timeoutFaultWordsA st s t badge) (affA d) = L.cpu (timeoutFaultWordsA st s t badge) := by + rw [show affA d = wa d 3520#u64 from rfl, tw d hd _ (by simp) (by simp) (by simp), w1.2.1] + exact hon + have hscw : L.mem (timeoutFaultWordsA st s t badge) (scA t) = L.mem s (scA t) := by + rw [scA_eq]; exact tw t ht _ (by simp) (by simp) (by simp) + have hky1 : σ1.kheap (specPtr (L.mem (timeoutFaultWordsA st s t badge) (scA t))) = + some (.schedContext y n) := by + rw [hscw, far _ (fun e => by rw [e, hkt] at hky; cases hky)]; exact hky + have hD1 : DonA σ1 (specPtr t) x1 := fun sc hs => + ⟨(hD sc hs).1, fun y' k hy' => (hD sc hs).2 y' k (by + have hne : sc ≠ specPtr t := fun e => by rw [e, hkt1] at hy'; cases hy' + rwa [far _ hne] at hy')⟩ + have hz1 : SSzA σ σ1 := ssz_updK_tcb σ _ _ + -- the program + unfold faults.handle_timeout faults.send_fault_ipc + simp only [hslot, bind_ok, hcap, setTimeoutFault_exactA, uncurry_apply_pair, Std.bind_assoc] + refine WP.spec_bind (send_ipc_recv_tmoBA (ar := ar) a P PE HE1 hPl hSt1 hC1 hN1 hoff hmax htu H1 ht hkt1 + (show SndRunA σ1 (specPtr t) x1 from hRun) he hk1 hd hkd1 hu1 hrep1 hon1 hb g gr true + (show x1.tcbFault = some (.timeout badge.bv) from rfl) hni (by rw [hscw]; exact hsc0) + (by rw [hscw]; exact hsc) hky1) ?_ + rintro ⟨s', a'⟩ ⟨ha, τ, hsp, HE', ⟨Q', R', H'⟩, hw⟩ + simp only [bind_ok, uncurry_apply_pair, WP.spec_ok] + -- the write set + have wall0 : WritesIn st s s' (fun b => FaultWA t b ∨ TmoFpA st Z m R Bf σ s t ep d b) := + (w1.trans hw).mono fun b h => h.elim Or.inl fun h => Or.inr (by + rcases h with ((h1 | ⟨w, hw', hb', hl⟩ | h3 | h4) | h5 | h6 | h7 | h8) | h9 + · exact Or.inl (Or.inl (Or.inl h1)) + · exact Or.inl (Or.inl (Or.inr (Or.inl ⟨w, hw', + (tw w hw' (3336#u64) (by simp) (by simp) (by simp)).symm.trans hb', hl⟩))) + · exact Or.inl (Or.inl (Or.inr (Or.inr (Or.inl h3)))) + · exact Or.inl (Or.inl (Or.inr (Or.inr (Or.inr h4)))) + · exact Or.inl (Or.inr (Or.inl h5)) + · exact Or.inl (Or.inr (Or.inr (Or.inl h6))) + · exact Or.inl (Or.inr (Or.inr (Or.inr (Or.inl h7)))) + · exact Or.inl (Or.inr (Or.inr (Or.inr (Or.inr (mwr_back rfl rfl hz1 w1.1 h8))))) + · exact Or.inr h9) + have wall : WritesIn st s s' (fun b => FaultWA t b ∨ IpcFpA st Z E R Bf m σ s b) := + wall0.mono fun b h => h.imp id (tmoFpA_splitA PE.tcb ht he hd) + -- the spec keeps + have hmem : ((), τ) ∈ (sendIpc (Z := Unit) true false hb.bv g gr false (specPtr t) (specPtr ep) σ1).1 := by + rw [hsp]; exact Set.mem_singleton _ + have K : SIKA (specPtr ep) (fun _ => True) (fun _ => True) false σ1 τ := + sendIpc_recv_sikFA (i := specPtr ep) (M := fun _ => True) (N := fun _ => True) hPl + (timeoutFaultWordsA st s t badge) hSt1 hV1 ((H.thT d).2 hd) hk1 hkt1 hkd1 hal true false hb.bv g gr false + (Or.inr ⟨_, rfl⟩) trivial trivial trivial (fun e => by rw [e, hk] at hkt; cases hkt) + (fun e => by rw [e, hk] at hkd; cases hkd) hD1 (fun h => by cases h) (fun _ => rfl) _ hmem + have KI := K.ipcKeep + have htcb : ∀ u ∈ Z.T, ∀ x, σ.kheap (specPtr u) = some (.tcb x) → + ∃ x', τ.kheap (specPtr u) = some (.tcb x') ∧ x'.tcbMcpriority = x.tcbMcpriority ∧ + x'.tcbYieldTo = x.tcbYieldTo ∧ x'.tcbFlags = x.tcbFlags := by + intro u _ x hx + by_cases e : specPtr u = specPtr t + · rw [e, hkt] at hx + cases hx + obtain ⟨y', hy', -, -, -, m2, y2, f2⟩ := KI.tcb _ _ hkt1 + exact ⟨y', by rw [e]; exact hy', m2, y2, f2⟩ + · obtain ⟨y', hy', -, -, -, m2, y2, f2⟩ := KI.tcb _ _ (show σ1.kheap (specPtr u) = some (.tcb x) by + rw [far _ e]; exact hx) + exact ⟨y', hy', m2, y2, f2⟩ + have hscY : ∀ c ∈ Z.C, ∃ y n y', σ.kheap (scNameA m c) = some (.schedContext y n) ∧ + τ.kheap (scNameA m c) = some (.schedContext y' n) ∧ y'.scYieldFrom = y.scYieldFrom := by + intro c hc + obtain ⟨y0, n0, hy0, -⟩ := H.scr.sc c hc + have hne : scNameA m c ≠ specPtr t := fun e => by rw [e, hkt] at hy0; cases hy0 + obtain ⟨y', hy', hs'⟩ := K.scF (show σ1.kheap (scNameA m c) = some (.schedContext y0 n0) by + rw [far _ hne]; exact hy0) + exact ⟨y0, n0, y', hy0, hy', hs'.yf⟩ + have wk : ∀ u ∈ Z.T, ∀ K : U64, (K = 3440#u64 ∨ K = 3472#u64 ∨ K = 3456#u64) → L.mem s' (wa u K) = L.mem s (wa u K) := by + intro u hu K hK + have hua := PE.tcb u hu + have hK1 : K.val < 4096 := by rcases hK with rfl | rfl | rfl <;> simp + refine wall.mem_eq (field_aligned hua hK1 (by rcases hK with rfl | rfl | rfl <;> simp)) ?_ + rintro (c | c) + · exact not_faultWA hta hua hK1 (by rcases hK with rfl | rfl | rfl <;> simp) c + · unfold IpcFpA at c + rcases c with c | c + · exact not_ew_offA PE hu hK1 (by rcases hK with rfl | rfl | rfl <;> (unfold TWOffN; simp)) + (by rcases hK with rfl | rfl | rfl <;> simp) (by rcases hK with rfl | rfl | rfl <;> simp) c + · rcases hK with rfl | rfl | rfl + · exact yieldTcbNotMWrA H.toSchedSRN hu (by simp) c + · exact yieldTcbNotMWrA H.toSchedSRN hu (by simp) c + · exact yieldFlagsNotMWrA H.toSchedSRN hu c + obtain ⟨Mc, Yl⟩ := yieldFrameA hM hY (fun u hu => ⟨wk u hu _ (Or.inl rfl), wk u hu _ (Or.inr (Or.inr rfl))⟩) (fun u hu => wk u hu _ (Or.inr (Or.inl rfl))) + htcb hscY + have hzτ : SSzA σ1 τ := fun p x k hp => let ⟨x0, h0, _⟩ := K.scB hp; ⟨x0, h0⟩ + have hcapT : fl4MaxConsumedTicks = maxTicksToUs := + (show fl4MaxConsumedTicks = core.num.U64.MAX.bv from rfl).trans hmax.symm + exact ⟨ha, τ, fl4HandleTimeout_sendA hcapT hconv hkt hgc hsp, + hsp, ⟨Q', R', H', fun u hu x hx => (HE'.at_tcb hu hx).st, Mc, Yl⟩, HE', wall0, hz1.trans hzτ⟩ + +end Main + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3TimeoutSendBlkA.lean b/proofs/Fl4ProofsA64/P3/Stage3TimeoutSendBlkA.lean new file mode 100644 index 000000000..7153ef708 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3TimeoutSendBlkA.lean @@ -0,0 +1,175 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2b-1): **`handle_timeout` of the current thread after the charge head, no receiver +waiting** (kernel entry 2's `hSendO`, the blocking case): the sibling of `Stage3TimeoutSendBA.handle_timeout_recvBA`. +The timeout handler endpoint is idle or holds waiting senders; the timeout sender (excepted from +`released_ipc_queues`) blocks on it with its timeout fault, so `SchedSRA` (in `YieldQueueRelA`) holds after. + +The spec's state is `blkSendA` of the faulted state; `BlkStepA` (`blkSendA_step`) gives the consumers their spec-side +facts (`SIKA`, `SpecSchedA`, `SchedValidA`, `QBlkA`). + +No named premise but `hconv`, `hmax` ([P], as `handle_timeout_recvBA`). New module. +-/ +import Fl4ProofsA64.P3.Stage3SendBlkSpecA +import Fl4ProofsA64.P3.Stage3TimeoutSendBA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4SpecA64 + Fl4Spec + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + +set_option maxHeartbeats 2000000 in +/-- **`handle_timeout` with no receiver waiting** (module docstring). -/ +theorem handle_timeout_blockBA {s : S} {σ : State Unit} (a : A) + (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) (hconv : Fl4TicksToUsLegacyAgree) + {t : U64} (H : SchedSRB st Z m κ s σ (specPtr t)) (hM : McpRelA Z.T m (L.mem s) σ) + (hY : YieldRelA Z.T Z.C m (L.mem s) σ) + (ht : t ∈ Z.T) {xt : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) + (hRun : SndRunA σ (specPtr t) xt) + {slot ep hb : U64} {sd rv g gr : Bool} + (hslot : obj.Tcb.slot t layout.tcb.SLOT_TIMEOUT_HANDLER = ok slot) + (hcap : obj.Slot.cap st slot s = ok (.Endpoint ep hb sd rv g gr)) + (hgc : getCap (Z := Unit) (getTcbTimeoutHandlerPtr (specPtr t)) σ = + ({(.endpointCap (specPtr ep) hb.bv (rightsOfA sd rv g gr), σ)}, False)) + (he : ep ∈ E) {x : Endpoint} (hk : σ.kheap (specPtr ep) = some (.endpoint x)) (hc : CanBlockA false x) + (hs : PrioSortedA σ x.epQueue) (badge : U64) : + faults.handle_timeout st ar s a t (.Timeout badge) ⦃ r => r.2 = a ∧ + let x1 : Tcb := { xt with tcbFault := some (.timeout badge.bv) } + let σ1 := updK σ (specPtr t) (.tcb x1) + let τ := blkSendA σ1 (specPtr t) (specPtr ep) x x1 (sndPlA hb.bv g gr false) + fl4HandleTimeout (Z := Unit) (specPtr t) (.timeout badge.bv) σ = ({((), τ)}, False) ∧ + sendIpc (Z := Unit) true false hb.bv g gr false (specPtr t) (specPtr ep) σ1 = ({((), τ)}, False) ∧ + BlkStepA σ1 τ (specPtr t) (specPtr ep) x x1 (sndPlA hb.bv g gr false) ∧ + (∃ Q' R', YieldQueueRelA (st := st) { Z with Q := Q', R := R' } m κ r.1 τ) ∧ + EpStateRel Z.T E R Bf (L.mem r.1) τ ∧ + WritesIn st s r.1 (fun b => FaultWA t b ∨ + BlkSendFpA st Z.T (fun σ s => MWrA (st := st) Z m σ s) σ s t ep b) ∧ + SSzA σ τ ⦄ := by + have hta := PE.tcb t ht + have w1 := faultWords_wrA (st := st) PE.tcb ht s badge + let x1 : Tcb := { xt with tcbFault := some (.timeout badge.bv) } + let σ1 := updK σ (specPtr t) (.tcb x1) + have tw : ∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → + (K.val ≠ 3376 ∧ K.val ≠ 3384 ∧ K.val ≠ 3392 ∧ K.val ≠ 3400) → + L.mem (timeoutFaultWordsA st s t badge) (wa u K) = L.mem s (wa u K) := + fun u hu K h1 h2 h3 => w1.mem_eq (field_aligned (PE.tcb u hu) h1 h2) (not_faultWA hta (PE.tcb u hu) h1 h3) + have far : ∀ p, p ≠ specPtr t → σ1.kheap p = σ.kheap p := fun p hp => by simp [σ1, hp] + have hkt1 : σ1.kheap (specPtr t) = some (.tcb x1) := by simp [σ1] + have hne : specPtr ep ≠ specPtr t := fun e => by rw [e, hkt] at hk; cases hk + have H1 : SchedSRB st Z m κ (timeoutFaultWordsA st s t badge) σ1 (specPtr t) := SchedSRB.faultTmoA H ht hkt badge _ + have HE1 : EpStateRel Z.T E R Bf (L.mem (timeoutFaultWordsA st s t badge)) σ1 := EpStateRel.faultTmoA HE PE ht hkt badge + have hk1 : σ1.kheap (specPtr ep) = some (.endpoint x) := by rw [far _ hne]; exact hk + have hz1 : SSzA σ σ1 := ssz_updK_tcb σ _ _ + have hmq : ∀ a ∈ x.epQueue, ∃ y, σ.kheap a = some (.tcb y) := by + intro a ha + obtain ⟨xs, hQ, hxs, -⟩ := (HE.at_ep he hk).queue + rw [hxs] at ha + obtain ⟨u, hu, rfl⟩ := List.mem_map.1 ha + obtain ⟨y, hy, -⟩ := HE.tcb u (hQ.mem u hu) + exact ⟨y, hy⟩ + have hs1 : PrioSortedA σ1 x.epQueue := by + refine hs.imp_of_mem fun {a b} _ _ hab ya yb ha hb => ?_ + have back : ∀ c y, σ1.kheap c = some (.tcb y) → ∃ y0, σ.kheap c = some (.tcb y0) ∧ + y.tcbPriority = y0.tcbPriority := fun c y hc => by + by_cases e : c = specPtr t + · rw [e, hkt1] at hc; cases hc; exact ⟨xt, by rw [e]; exact hkt, rfl⟩ + · exact ⟨y, by rw [← far c e]; exact hc, rfl⟩ + obtain ⟨ya0, ha0, ea⟩ := back a ya ha + obtain ⟨yb0, hb0, eb⟩ := back b yb hb + rw [ea, eb] + exact hab ya0 yb0 ha0 hb0 + have hsort := prioSorted_of_specA PE.tcb HE1 (HE1.at_ep he hk1) hs1 + have hte : specPtr t ≠ specPtr ep := fun e => hne e.symm + have B : BlkStepA σ1 (blkSendA σ1 (specPtr t) (specPtr ep) x x1 (sndPlA hb.bv g gr false)) (specPtr t) + (specPtr ep) x x1 (sndPlA hb.bv g gr false) := blkSendA_step hte + -- the program + unfold faults.handle_timeout faults.send_fault_ipc + simp only [hslot, bind_ok, hcap, setTimeoutFault_exactA, uncurry_apply_pair, Std.bind_assoc] + refine WP.spec_bind (send_ipc_block_tmoA (ar := ar) P PE _ a σ1 HE1 H1 ht hkt1 + (show SndRunA σ1 (specPtr t) x1 from hRun) (b0 := badge.bv) rfl he hk1 hc hsort false hb g gr false) ?_ + rintro ⟨s', a'⟩ ⟨ha, hsp, HE', ⟨Q', R', H'⟩, hw, hz⟩ + simp only [bind_ok, uncurry_apply_pair, WP.spec_ok] + generalize hτ : blkSendA σ1 (specPtr t) (specPtr ep) x x1 (sndPlA hb.bv g gr false) = τ at B hsp HE' H' hz + have wall : WritesIn st s s' (fun b => FaultWA t b ∨ + BlkSendFpA st Z.T (fun σ s => MWrA (st := st) Z m σ s) σ s t ep b) := by + refine (w1.trans hw).mono fun b h => h.imp id fun h => ?_ + rcases h with h | h | h | h | h | ⟨v, hv, h⟩ | h + · exact Or.inl h + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr (Or.inl h)) + · exact Or.inr (Or.inr (Or.inr (Or.inl h))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inl h)))) + · refine Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨v, ?_, h⟩))))) + exact hv.imp id fun hv => ⟨hv.1, (tw v hv.1 3336#u64 (by simp) (by simp) (by simp)).symm.trans hv.2⟩ + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (mwr_back rfl rfl hz1 w1.1 h)))))) + have K : SIKA (specPtr ep) (fun o => o = specPtr t) (fun _ => False) false σ1 τ := B.sika hkt1 hk1 rfl + have KI := K.ipcKeep + have htcb : ∀ u ∈ Z.T, ∀ y, σ.kheap (specPtr u) = some (.tcb y) → + ∃ y', τ.kheap (specPtr u) = some (.tcb y') ∧ y'.tcbMcpriority = y.tcbMcpriority ∧ + y'.tcbYieldTo = y.tcbYieldTo ∧ y'.tcbFlags = y.tcbFlags := by + intro u _ y hy + by_cases e : specPtr u = specPtr t + · rw [e, hkt] at hy + cases hy + obtain ⟨y', hy', -, -, -, m2, y2, f2⟩ := KI.tcb _ _ hkt1 + exact ⟨y', by rw [e]; exact hy', m2, y2, f2⟩ + · obtain ⟨y', hy', -, -, -, m2, y2, f2⟩ := KI.tcb _ _ (show σ1.kheap (specPtr u) = some (.tcb y) by + rw [far _ e]; exact hy) + exact ⟨y', hy', m2, y2, f2⟩ + have hscY : ∀ c ∈ Z.C, ∃ y n y', σ.kheap (scNameA m c) = some (.schedContext y n) ∧ + τ.kheap (scNameA m c) = some (.schedContext y' n) ∧ y'.scYieldFrom = y.scYieldFrom := by + intro c hc' + obtain ⟨y0, n0, hy0, -⟩ := H.scr.sc c hc' + have hne' : scNameA m c ≠ specPtr t := fun e => by rw [e, hkt] at hy0; cases hy0 + obtain ⟨y', hy', hs'⟩ := K.scF (show σ1.kheap (scNameA m c) = some (.schedContext y0 n0) by + rw [far _ hne']; exact hy0) + exact ⟨y0, n0, y', hy0, hy', hs'.yf⟩ + have wk : ∀ u ∈ Z.T, ∀ K : U64, (K = 3440#u64 ∨ K = 3472#u64 ∨ K = 3456#u64) → L.mem s' (wa u K) = L.mem s (wa u K) := by + intro u hu K hK + have hua := PE.tcb u hu + have hK1 : K.val < 4096 := by rcases hK with rfl | rfl | rfl <;> simp + refine wall.mem_eq (field_aligned hua hK1 (by rcases hK with rfl | rfl | rfl <;> simp)) ?_ + rintro (c | c) + · exact not_faultWA hta hua hK1 (by rcases hK with rfl | rfl | rfl <;> simp) c + have notE : ¬ EWrite Z.T E R Bf (wa u K) := not_ew_offA PE hu hK1 + (by rcases hK with rfl | rfl | rfl <;> (unfold TWOffN; simp)) + (by rcases hK with rfl | rfl | rfl <;> simp) (by rcases hK with rfl | rfl | rfl <;> simp) + have tf : ∀ (b : U64) (k : Nat), wa u K = b → b.val = t.val + k → TWOffN k → False := fun b k e hv hk' => + notE (e ▸ Or.inr (Or.inr (Or.inl ⟨t, ht, Or.inl (twf_ofA k hv hk')⟩))) + rcases c with c | c | c | c | c | ⟨v, hv, c⟩ | c + · exact tf _ 3328 c (Endp.val_st hta) (by unfold TWOffN; omega) + · exact tf _ 3336 c (Endp.val_blk hta) (by unfold TWOffN; omega) + · exact tf _ 3344 c (Endp.val_badge hta) (by unfold TWOffN; omega) + · exact tf _ 3352 c (Endp.val_flags hta) (by unfold TWOffN; omega) + · exact notE (Or.inl ⟨ep, he, c⟩) + · have hvT : v ∈ Z.T := by + rcases hv with rfl | hv + · exact ht + · exact hv.1 + exact notE (Or.inr (Or.inr (Or.inl ⟨v, hvT, Or.inr c⟩))) + · rcases hK with rfl | rfl | rfl + · exact yieldTcbNotMWrA H.toSchedSRN hu (by simp) c + · exact yieldTcbNotMWrA H.toSchedSRN hu (by simp) c + · exact yieldFlagsNotMWrA H.toSchedSRN hu c + obtain ⟨Mc, Yl⟩ := yieldFrameA hM hY (fun u hu => ⟨wk u hu _ (Or.inl rfl), wk u hu _ (Or.inr (Or.inr rfl))⟩) + (fun u hu => wk u hu _ (Or.inr (Or.inl rfl))) + htcb hscY + have hcapT : fl4MaxConsumedTicks = maxTicksToUs := + (show fl4MaxConsumedTicks = core.num.U64.MAX.bv from rfl).trans hmax.symm + exact ⟨ha, fl4HandleTimeout_sendA hcapT hconv hkt hgc hsp, + hsp, B, ⟨Q', R', H', fun u hu y hy => (HE'.at_tcb hu hy).st, Mc, Yl⟩, HE', wall, hz1.trans hz⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3TimeoutSenderA.lean b/proofs/Fl4ProofsA64/P3/Stage3TimeoutSenderA.lean new file mode 100644 index 000000000..6614e9db8 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3TimeoutSenderA.lean @@ -0,0 +1,175 @@ +/- +P3 (aarch64, A64-P3 Stage 3, kernel-entry composition, step 2): **the faulted sender after `send_ipc` to a waiting +receiver** (no call, no donation): it is `Inactive` or `BlockedOnReply`. + +* **`sendIpc_fault_senderA`**: every outcome. The sender's fault is tracked through the dequeue, the transfer and the + receiver's reply unlink (`FaultIsA`, as `sendIpc_recv_schedValidFA`), so the send takes the fault branch: a reply + push (`BlockedOnReply`) or `Inactive`; the receiver's wake keeps the sender's state. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3SendFaultValidA +import Fl4ProofsA64.P3.Stage3SendDonateKeepA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P3 Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Classical + +noncomputable section + +section +open fl4_core_a64 Fl4ProofsA64.P1 +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- The receiver's wake keeps the sender's state. -/ +theorem sendTail_stIsA {t d : ObjRef} (htd : t ≠ d) (ts : ThreadState) : HT (StIs t ts) (do + setThreadState (Z := Unit) d .running + let destSc ← getTcbObjRef Tcb.tcbSchedContext d + ifSporadicCurScTestRefillUnblockCheck destSc + possibleSwitchTo d) (fun _ σ => StIs t ts σ) := by + refine HT.bind (Q := fun _ σ => StIs t ts σ) ?_ fun _ => ?_ + · unfold Fl4SpecA64.setThreadState + refine HT.bind (Q := fun _ σ => StIs t ts σ) (fun σ h r hr y hy => ?_) fun _ σ h r hr => ?_ + · obtain ⟨x, -, e⟩ := threadSet_memA _ d hr + rw [e] at hy + exact h y (by simpa only [Function.update_of_ne htd] using hy) + · rw [setThreadStateAct_eq] at hr + have e : r = ((), stsAct σ d) := hr + rw [e] + exact fun y hy => h y (by rwa [stsAct_kheap] at hy) + refine HT.bind (Q := fun _ σ => StIs t ts σ) (fun σ h r hr => by + have e : r.2 = σ := ((threadGet_eqPres _ _) σ r hr).symm + rw [e]; exact h) fun sc => ?_ + have hT : TPres (ifSporadicCurScTestRefillUnblockCheck (Z := Unit) sc) := by + unfold ifSporadicCurScTestRefillUnblockCheck ifCondRefillUnblockCheck; tpres + refine HT.bind (Q := fun _ σ => StIs t ts σ) (fun σ h r hr => + (StIs.frame t _).keep σ _ (hT σ r hr) h) fun _ => ?_ + intro σ h r hr y hy + exact h y (((KPresInst.pres : PresR TcbCellsEq (possibleSwitchTo (Z := Unit) d)) σ r hr).2 _ _ |>.2 hy) + +/-- `setThreadState t ts` leaves `t` in `ts`. -/ +theorem setThreadState_stIsA (t : ObjRef) (ts : ThreadState) : + HT (fun _ => True) (setThreadState (Z := Unit) t ts) (fun _ σ => StIs t ts σ) := by + unfold Fl4SpecA64.setThreadState + refine HT.bind (Q := fun _ σ => StIs t ts σ) (fun σ _ z hz y hy => ?_) fun _ σ h z hz => ?_ + · obtain ⟨x, -, e⟩ := threadSet_memA _ t hz + rw [e] at hy + have : ({ x with tcbState := ts } : Tcb) = y := by simpa using hy + rw [← this] + · rw [setThreadStateAct_eq] at hz + have e : z = ((), stsAct σ t) := hz + rw [e] + exact fun y hy => h y (by rwa [stsAct_kheap] at hy) + +/-- A reply push without donation leaves the caller `BlockedOnReply`. -/ +theorem replyPush_noDon_stIsA (t d r : ObjRef) : + HT (fun _ => True) (replyPush (Z := Unit) t d r false) (fun _ σ => StIs t (.blockedOnReply r) σ) := by + have skip : ∀ {α β : Type} {m : SMonad Unit α} {f : α → SMonad Unit β} {Q : β → State Unit → Prop}, + (∀ a, HT (fun _ => True) (f a) Q) → HT (fun _ => True) (m >>= f) Q := + fun h => HT.bind (Q := fun _ _ => True) (fun _ _ _ _ => trivial) h + unfold replyPush + refine skip fun _ => skip fun _ => skip fun _ => ?_ + refine HT.bind (Q := fun _ σ => StIs t (.blockedOnReply r) σ) (setThreadState_stIsA _ _) fun _ => ?_ + refine HT.when (fun _ => ?_) (fun _ _ h => h) + refine HT.bind (Q := fun _ σ => StIs t (.blockedOnReply r) σ) + (fun σ h z hz => h.stk (bindScReply_stkA _ _ σ z hz)) fun _ => ?_ + intro σ h z hz + exact h.stk (schedContextDonate_stkA _ _ σ z hz) + +set_option maxHeartbeats 1000000 in +/-- **The faulted sender ends `Inactive` or `BlockedOnReply`** (module docstring). -/ +theorem sendIpc_fault_senderA {th : TypedHeapA} {kb : U64} (hPl : PlaceA th kb) (sf : S) {σ : State Unit} + (hR : StateRelA th (L.mem sf) σ) (hV : SlotsValidA th (L.mem sf)) {t d : U64} {e : ObjRef} {q : List ObjRef} + (htr : th d = some .tcb) (hk : σ.kheap e = some (.endpoint (.recvEP (specPtr d :: q)))) + {xt xd : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb xt)) (hkd : σ.kheap (specPtr d) = some (.tcb xd)) + (hal : ∀ rb, ipcBufOfA true xd = some rb → (bufAddr rb 0).toNat % 8 = 0) + {e' : ObjRef} {rep : Option ObjRef} {dd : ReceiverPayload} (hst : xd.tcbState = .blockedOnReceive e' rep dd) + (htd : specPtr t ≠ specPtr d) (hf : xt.tcbFault ≠ none) (blk : Bool) (badge : Badge) (g gr : Bool) + (hcase : XferCaseA xt g) : + ∀ x ∈ (sendIpc (Z := Unit) blk false badge g gr false (specPtr t) e σ).1, + StIs (specPtr t) .inactive x.2 ∨ ∃ r, StIs (specPtr t) (.blockedOnReply r) x.2 := by + let B : State Unit → Prop := fun σ => StIs (specPtr t) .inactive σ ∨ ∃ r, StIs (specPtr t) (.blockedOnReply r) σ + have tail : HT B (do + setThreadState (Z := Unit) (specPtr d) .running + let destSc ← getTcbObjRef Tcb.tcbSchedContext (specPtr d) + ifSporadicCurScTestRefillUnblockCheck destSc + possibleSwitchTo (specPtr d)) (fun _ σ => B σ) := fun σ h r hr => + h.elim (fun h => Or.inl (sendTail_stIsA htd _ σ h r hr)) + (fun ⟨r', h⟩ => Or.inr ⟨r', sendTail_stIsA htd _ σ h r hr⟩) + suffices H : HT (fun s => s = σ) (sendIpc (Z := Unit) blk false badge g gr false (specPtr t) e) + (fun _ s => B s) from fun x hx => H σ rfl x hx + unfold sendIpc + intro s hs x hx + subst hs + obtain ⟨y, hy, hx'⟩ := mem_bindA.1 hx + clear hx + rw [getEndpoint_eq hk] at hy + have ey : y = (.recvEP (specPtr d :: q), s) := hy + subst ey + revert x hx' + cases blk <;> dsimp only + all_goals + refine HT.atA (P := fun σ' => σ' = s) ?_ rfl + refine HT.bind (Q := fun _ s1 => TcbKeepA s s1 ∧ SchedFrameA s s1 ∧ FaultIsA (specPtr t) xt.tcbFault s1) + (fun σ' h z hz => ?_) fun _ => ?_ + · subst h + exact ⟨TKInst.pres σ' z hz, SPresInst.pres σ' z hz, + FaultIsA.sik (fun x hx => by rw [hkt] at hx; cases hx; rfl) + (tcbEpDequeue_sikA (i := σ'.idleThread) (M := fun _ => True) (N := fun _ => False) (don := false) _ _ + trivial σ' z hz)⟩ + refine HT.bind (Q := fun rs s1 => (TcbKeepA s s1 ∧ SchedFrameA s s1 ∧ FaultIsA (specPtr t) xt.tcbFault s1) ∧ + rs = .blockedOnReceive e' rep dd) (fun σ' h z hz => ?_) fun rs => ?_ + · have hz' : z = (xd.tcbState, σ') := by + unfold getThreadState at hz + exact threadGet_valA _ (h.1 _ _ hkd) hz + subst hz' + exact ⟨h, hst⟩ + intro σ' ⟨h, ers⟩ z hz + subst ers + dsimp only at hz + obtain ⟨w, hw, hz1⟩ := mem_bindA.1 hz + clear hz + have ew : w = (rep, σ') := hw + subst ew + obtain ⟨u, hu, hz2⟩ := mem_bindA.1 hz1 + clear hz1 + have hS : SIKA s.idleThread (fun _ => True) (fun _ => False) false σ' u.2 := + doIpcTransfer_any_sikA hPl sf (hR.specSchedA h.2.1.1) hV htr (h.1 _ _ hkt) (h.1 _ _ hkd) hal _ _ _ hcase + σ' rfl u hu + have hNF : FaultIsA (specPtr t) xt.tcbFault u.2 := h.2.2.sik hS + revert z hz2 + refine HT.atA (P := fun s1 => FaultIsA (specPtr t) xt.tcbFault s1) ?_ hNF + refine HT.bind (Q := fun _ s1 => FaultIsA (specPtr t) xt.tcbFault s1) ?_ fun _ => ?_ + · cases rep with + | none => exact HT.pure () (fun _ h => h) + | some r => + intro s1 h z hz + exact h.sik (replyUnlinkTcb_sikA (i := s.idleThread) (M := fun _ => True) (N := fun _ => False) + (don := false) (specPtr d) r trivial s1 z hz) + refine HT.bind (Q := fun _ s1 => FaultIsA (specPtr t) xt.tcbFault s1) + (fun s1 h z hz => by + have e : z.2 = s1 := ((threadGet_eqPres _ _) s1 z hz).symm + rw [e]; exact h) fun scOpt => ?_ + refine HT.bind (Q := fun fault _ => fault = xt.tcbFault) + (HT.threadGet _ (specPtr t) fun s1 h x hx => h x hx) fun fault => ?_ + intro s1 ef z hz + subst ef + revert z hz + refine HT.atA (P := fun _ => True) ?_ trivial + have hc1 : (false = true ∨ xt.tcbFault ≠ none) := Or.inr hf + by_cases c2 : (g = true ∨ gr = true) ∧ rep ≠ none + · rw [if_pos hc1, if_pos c2] + cases rep with + | none => exact absurd rfl c2.2 + | some r => + exact HT.bind (HT.mono (replyPush_noDon_stIsA _ _ _) (fun _ _ => trivial) (fun _ _ h => Or.inr ⟨_, h⟩)) + fun _ => tail + · rw [if_pos hc1, if_neg c2] + exact HT.bind (HT.mono (setThreadState_stIsA _ .inactive) (fun _ _ => trivial) (fun _ _ h => Or.inl h)) + fun _ => tail + +end + +end + +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/Stage3UnmapExtSpecA.lean b/proofs/Fl4ProofsA64/P3/Stage3UnmapExtSpecA.lean new file mode 100644 index 000000000..230070da7 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3UnmapExtSpecA.lean @@ -0,0 +1,140 @@ +/- +P3 (AArch64, merge line, D8 step 5 (ii)): **the spec side of the Ext (Stage-2 / high-window) unmaps as a `VsSpecA` step**. + +`P3/Stage3UnmapSpecA` shows l4v's `unmap_page` invalidates page-table entries (`InvA`). FL4's own unmaps +(`extUnmapPageStX`: one entry of a Stage-2 root, a high window's L1 table or a normal `PageTable` cleared, nothing else) +also invalidate entries of the two Ext objects. `ArchInvA`/`InvXA` is the common relation over all three (an arch object +is replaced by one of the same shape with entries kept or invalid); `InvA.toX` embeds the page-table one, +`extSlotWriteX_invX` / `extUnmapPageStX_invX` show the Ext write is such a step, `VsSpecA.of_invX` closes to `VsSpecA`. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3UnmapSpecA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4Spec Fl4SpecA64 + +noncomputable section + +/-- An arch object replaced by one of the same shape whose entries are kept or invalid. -/ +def ArchInvA : Fl4SpecA64.ArchKernelObj → Fl4SpecA64.ArchKernelObj → Prop + | .pageTable t, .pageTable t' => Fl4SpecA64.ptType t' = Fl4SpecA64.ptType t ∧ + ∀ i, Fl4SpecA64.ptApply t' i = Fl4SpecA64.ptApply t i ∨ Fl4SpecA64.ptApply t' i = .invalidPTE + | .ext (.stage2Root e), .ext (.stage2Root e') => ∀ j, e' j = e j ∨ e' j = .invalidPTE + | .ext (.highVSpace e), .ext (.highVSpace e') => ∀ j, e' j = e j ∨ e' j = .invalidPTE + | _, _ => False + +theorem ArchInvA.nonvcpu {o o' : Fl4SpecA64.ArchKernelObj} (h : ArchInvA o o') : + (∀ v, o ≠ .vcpu v) ∧ (∀ v, o' ≠ .vcpu v) := by + constructor + · intro v e; subst e; cases o' <;> exact absurd h (by simp [ArchInvA]) + · intro v e; subst e; cases o <;> exact absurd h (by simp [ArchInvA]) + +theorem ArchInvA.pt {t : Fl4SpecA64.PT} {o' : Fl4SpecA64.ArchKernelObj} (h : ArchInvA (.pageTable t) o') : + ∃ t', o' = .pageTable t' ∧ Fl4SpecA64.ptType t' = Fl4SpecA64.ptType t ∧ + ∀ i, Fl4SpecA64.ptApply t' i = Fl4SpecA64.ptApply t i ∨ Fl4SpecA64.ptApply t' i = .invalidPTE := by + cases o' with + | pageTable t' => exact ⟨t', rfl, h.1, h.2⟩ + | _ => exact absurd h (by simp [ArchInvA]) + +theorem ArchInvA.s2 {e : Fin 1024 → Fl4SpecA64.PTE} {o' : Fl4SpecA64.ArchKernelObj} + (h : ArchInvA (.ext (.stage2Root e)) o') : + ∃ e', o' = .ext (.stage2Root e') ∧ ∀ j, e' j = e j ∨ e' j = .invalidPTE := by + cases o' with + | ext x => + cases x with + | stage2Root e' => exact ⟨e', rfl, h⟩ + | highVSpace e' => exact absurd h (by simp [ArchInvA]) + | _ => exact absurd h (by simp [ArchInvA]) + +theorem ArchInvA.hi {e : Fin 512 → Fl4SpecA64.PTE} {o' : Fl4SpecA64.ArchKernelObj} + (h : ArchInvA (.ext (.highVSpace e)) o') : + ∃ e', o' = .ext (.highVSpace e') ∧ ∀ j, e' j = e j ∨ e' j = .invalidPTE := by + cases o' with + | ext x => + cases x with + | highVSpace e' => exact ⟨e', rfl, h⟩ + | stage2Root e' => exact absurd h (by simp [ArchInvA]) + | _ => exact absurd h (by simp [ArchInvA]) + +/-- **Arch objects only get entries invalidated** (every other object kept). -/ +def InvXA (σ σ' : Fl4SpecA64.State Unit) : Prop := + ∀ q, σ'.kheap q = σ.kheap q ∨ ∃ o o', σ.kheap q = some (.archObj o) ∧ σ'.kheap q = some (.archObj o') ∧ ArchInvA o o' + +theorem InvA.toX {σ σ' : Fl4SpecA64.State Unit} (h : InvA σ σ') : InvXA σ σ' := fun q => by + rcases h q with e | ⟨t, t', a, b, ty, ap⟩ + · exact Or.inl e + · exact Or.inr ⟨_, _, a, b, ⟨ty, ap⟩⟩ + +theorem VsSpecA.of_invX {σ σ' : Fl4SpecA64.State Unit} (h : QvA σ σ') (hi : InvXA σ σ') : VsSpecA σ σ' := + VsSpecA.of_qvA h (fun p => by + rcases hi p with e | ⟨o, o', a, b, hn⟩ + · exact Or.inl e + · exact Or.inr ⟨_, _, a, b, hn.nonvcpu.1, hn.nonvcpu.2⟩) h.asidTable + +theorem InvXA.refl (σ : Fl4SpecA64.State Unit) : InvXA σ σ := fun _ => Or.inl rfl + + +/-- **An Ext table-slot write of an invalid entry is an `InvXA` step** (and `QvA`: only the heap changes). -/ +theorem extSlotWriteX_invX (σ : Fl4SpecA64.State Unit) (sl : Fl4SpecA64.ExtSlot) : + QvA σ (extSlotWriteX σ sl .invalidPTE) ∧ InvXA σ (extSlotWriteX σ sl .invalidPTE) := by + refine ⟨vmA_refl _, fun q => ?_⟩ + show extSlotKhX σ sl .invalidPTE q = σ.kheap q ∨ ∃ o o', σ.kheap q = some (.archObj o) ∧ + extSlotKhX σ sl .invalidPTE q = some (.archObj o') ∧ ArchInvA o o' + cases sl with + | root r i => + cases ho : extObjAt σ r with + | none => exact Or.inl (by simp only [extSlotKhX, ho]) + | some o => + have e : extSlotKhX σ (.root r i) .invalidPTE = + Function.update σ.kheap r (some (.archObj (.ext (o.setEntry i .invalidPTE)))) := by + simp only [extSlotKhX, ho] + rw [e] + by_cases hq : q = r + · subst hq + have hk : σ.kheap q = some (.archObj (.ext o)) := extObjAt_someX.1 ho + refine Or.inr ⟨_, .ext (o.setEntry i .invalidPTE), hk, (by first | exact Function.update_self _ _ _ | simp [Function.update]), ?_⟩ + cases o with + | stage2Root e => exact fun j => by simp only [ExtArchObj.setEntry]; split_ifs <;> simp + | highVSpace e => exact fun j => by simp only [ExtArchObj.setEntry]; split_ifs <;> simp + · exact Or.inl (Function.update_of_ne hq _ _) + | pt p => + cases hk : σ.kheap (tableBase .normalPtT p) with + | none => exact Or.inl (by simp only [extSlotKhX, hk]) + | some ko => + cases ko with + | archObj ao => + cases ao with + | pageTable t => + have e : extSlotKhX σ (.pt p) .invalidPTE = Function.update σ.kheap (tableBase .normalPtT p) + (some (.archObj (.pageTable (ptUpd t (tableIndex .normalPtT p) .invalidPTE)))) := by + simp only [extSlotKhX, hk] + rw [e] + by_cases hq : q = tableBase .normalPtT p + · subst hq + exact Or.inr ⟨_, .pageTable (ptUpd t (tableIndex .normalPtT p) .invalidPTE), hk, (by first | exact Function.update_self _ _ _ | simp [Function.update]), + ptUpd_inv t (tableIndex .normalPtT p)⟩ + · exact Or.inl (Function.update_of_ne hq _ _) + | _ => exact Or.inl (by simp only [extSlotKhX, hk]) + | _ => exact Or.inl (by simp only [extSlotKhX, hk]) + +theorem extUnmapPageStX_invX (σ : Fl4SpecA64.State Unit) (k : Fl4SpecA64.ExtRootKind) (sz : Fl4SpecA64.VMPageSize) + (asid : Fl4SpecA64.Asid) (va : Fl4SpecA64.VSpaceRef) (p : Fl4SpecA64.ObjRef) : + QvA σ (extUnmapPageStX σ k sz asid va p) ∧ InvXA σ (extUnmapPageStX σ k sz asid va p) := by + unfold extUnmapPageStX + cases extPageSlotX σ k sz asid va p with + | none => exact ⟨QvA.refl σ, InvXA.refl σ⟩ + | some sl => exact extSlotWriteX_invX σ sl + +theorem extUnmapTableStX_invX (σ : Fl4SpecA64.State Unit) (k : Fl4SpecA64.ExtRootKind) + (asid : Fl4SpecA64.Asid) (va : Fl4SpecA64.VSpaceRef) (pt : Fl4SpecA64.ObjRef) : + QvA σ (extUnmapTableStX σ k asid va pt) ∧ InvXA σ (extUnmapTableStX σ k asid va pt) := by + unfold extUnmapTableStX + cases extTableSlotX σ k asid va pt with + | none => exact ⟨QvA.refl σ, InvXA.refl σ⟩ + | some sl => exact extSlotWriteX_invX σ sl + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3UnmapSpecA.lean b/proofs/Fl4ProofsA64/P3/Stage3UnmapSpecA.lean new file mode 100644 index 000000000..90c5896bf --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3UnmapSpecA.lean @@ -0,0 +1,301 @@ +/- +P3 (AArch64, merge line, D8 step 5 (ii)): **l4v's `unmap_page` is a `VsSpecA` step** (given the Stage-1 relation). + +`OutKmA` (`P3/FinVsJA`) shows an unmap changes only the heap and the machine state; the record's VSpace-write step +`VsSpecA` (`P3/Stage3VsWriteA`) also needs the machine state to change only in its opaque part, and the heap only by +replacing arch objects by arch objects. Here: + +* **`OutQ Q m`** and its combinators: every outcome of `m` is `Q`-related to its start (a relation-generic copy of + `OutKmA`'s, for a reflexive transitive `Q`). +* **`QvA`**: heap free, the machine state's opaque part and the three VMID fields of the arch state free + (`VmA` after the heap); **`unmapPage_qvA`** (and `unmapPageTable_qvA`). +* **`VsSpecA.of_qvA`**: `QvA` plus `∀ q, kheap q kept or an arch object replaced by an arch object (not a VCPU)` is + `VsSpecA`, given that the ASID table is kept. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.FinVsJA +import Fl4ProofsA64.P3.Stage3VsWriteA +import Fl4ProofsA64.P3.ExtVsSpecA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4Spec + +noncomputable section + +/-- Every outcome of `m` is `Q`-related to its start. -/ +def OutQ (Q : Fl4SpecA64.State Unit → Fl4SpecA64.State Unit → Prop) {α : Type} + (m : NonDet (Fl4SpecA64.State Unit) α) : Prop := + ∀ σ r, r ∈ (m σ).1 → Q σ r.2 + +section gen +variable {Q : Fl4SpecA64.State Unit → Fl4SpecA64.State Unit → Prop} {α β ε : Type} + +theorem OutQ.bind (ht : ∀ a b c, Q a b → Q b c → Q a c) {m : NonDet (Fl4SpecA64.State Unit) α} + {f : α → NonDet (Fl4SpecA64.State Unit) β} (hm : OutQ Q m) (hf : ∀ a, OutQ Q (f a)) : OutQ Q (m >>= f) := + fun σ r hr => by + obtain ⟨x, hx, hr⟩ := hr + exact ht _ _ _ (hm σ x hx) (hf x.1 x.2 r hr) + +theorem OutQ.one (hr : ∀ σ, Q σ σ) {m : NonDet (Fl4SpecA64.State Unit) α} + (h : ∀ σ, ∃ v, m σ = ({(v, σ)}, False)) : OutQ Q m := fun σ r hr' => by + obtain ⟨v, e⟩ := h σ + rw [e] at hr' + obtain rfl : r = (v, σ) := hr' + exact hr σ + +theorem OutQ.pure (hr : ∀ σ, Q σ σ) (a : α) : OutQ Q (Pure.pure a : NonDet (Fl4SpecA64.State Unit) α) := + OutQ.one hr fun _ => ⟨a, rfl⟩ + +theorem OutQ.gets (hr : ∀ σ, Q σ σ) (g : Fl4SpecA64.State Unit → α) : OutQ Q (NonDet.gets g) := + OutQ.one hr fun _ => ⟨_, rfl⟩ + +theorem OutQ.assertOpt (hr : ∀ σ, Q σ σ) (o : Option α) : + OutQ Q (NonDet.assertOpt o : NonDet (Fl4SpecA64.State Unit) α) := by + cases o with + | none => exact fun _ _ h => absurd h (Set.notMem_empty _) + | some a => exact OutQ.one hr fun _ => ⟨a, rfl⟩ + +theorem OutQ.getsThe (hr : ∀ σ, Q σ σ) (g : Fl4SpecA64.State Unit → Option α) : OutQ Q (NonDet.getsThe g) := + fun σ r hr' => OutQ.assertOpt hr (g σ) σ r hr' + +theorem OutQ.getsMap (hr : ∀ σ, Q σ σ) (ht : ∀ a b c, Q a b → Q b c → Q a c) {γ : Type} + (g : Fl4SpecA64.State Unit → γ → Option α) (p : γ) : OutQ Q (NonDet.getsMap g p) := + OutQ.bind ht (OutQ.gets hr g) fun x => OutQ.assertOpt hr (x p) + +theorem OutQ.assert (hr : ∀ σ, Q σ σ) (P : Prop) : + OutQ Q (NonDet.assert P : NonDet (Fl4SpecA64.State Unit) Unit) := fun σ r hr' => by + obtain ⟨-, rfl⟩ := hr' + exact hr σ + +theorem OutQ.selectF (hr : ∀ σ, Q σ σ) (S : Set α × Prop) : + OutQ Q (NonDet.selectF S : NonDet (Fl4SpecA64.State Unit) α) := + fun σ r hr' => by rw [hr'.2]; exact hr σ + +theorem OutQ.getPut {g : Fl4SpecA64.State Unit → Fl4SpecA64.State Unit} (hg : ∀ σ, Q σ (g σ)) : + OutQ Q (NonDet.get >>= fun s => NonDet.put (g s)) := fun σ r hr => by + obtain ⟨x, hx, hr⟩ := hr + obtain rfl : x = (σ, σ) := hx + obtain rfl : r = ((), g σ) := hr + exact hg σ + +theorem OutQ.ebind (ht : ∀ a b c, Q a b → Q b c → Q a c) {m : NonDetE (Fl4SpecA64.State Unit) ε α} + {f : α → NonDetE (Fl4SpecA64.State Unit) ε β} (hm : OutQ Q m.run) (hf : ∀ a, OutQ Q (f a).run) : + OutQ Q (m >>= f).run := fun σ r hr => by + obtain ⟨⟨e | a, σ1⟩, hx, hr⟩ := hr + · obtain rfl : r = (.error e, σ1) := hr + exact hm σ _ hx + · exact ht _ _ _ (hm σ _ hx) (hf a σ1 r hr) + +theorem OutQ.liftE {m : NonDet (Fl4SpecA64.State Unit) α} (hm : OutQ Q m) : + OutQ Q (NonDetE.liftE (ε := ε) m).run := fun σ r hr => by + obtain ⟨x, hx, hr⟩ := hr + obtain rfl : r = (.ok x.1, x.2) := hr + exact hm σ x hx + +theorem OutQ.ret (hr : ∀ σ, Q σ σ) (a : α) : + OutQ Q (Pure.pure a : NonDetE (Fl4SpecA64.State Unit) ε α).run := + OutQ.one hr fun _ => ⟨_, rfl⟩ + +theorem OutQ.retOk (hr : ∀ σ, Q σ σ) (a : α) : + OutQ Q (NonDetE.returnOk a : NonDetE (Fl4SpecA64.State Unit) ε α).run := + OutQ.one hr fun _ => ⟨_, rfl⟩ + +theorem OutQ.throw (hr : ∀ σ, Q σ σ) (e : ε) : + OutQ Q (NonDetE.throwError e : NonDetE (Fl4SpecA64.State Unit) ε α).run := + OutQ.one hr fun _ => ⟨_, rfl⟩ + +theorem OutQ.unlessE (hr : ∀ σ, Q σ σ) {P : Prop} [Decidable P] {m : NonDetE (Fl4SpecA64.State Unit) ε Unit} + (hm : OutQ Q m.run) : OutQ Q (NonDetE.unlessE P m).run := by + unfold NonDetE.unlessE + split + · exact OutQ.ret hr () + · exact hm + +theorem OutQ.catch (ht : ∀ a b c, Q a b → Q b c → Q a c) {m : NonDetE (Fl4SpecA64.State Unit) ε α} + {h : ε → NonDet (Fl4SpecA64.State Unit) α} (hm : OutQ Q m.run) (hh : ∀ e, OutQ Q (h e)) : + OutQ Q (NonDetE.catch m h) := fun σ r hr => by + obtain ⟨⟨e | a, σ1⟩, hx, hr⟩ := hr + · exact ht _ _ _ (hm σ _ hx) (hh e σ1 r hr) + · obtain rfl : r = (a, σ1) := hr + exact hm σ _ hx + +end gen + +/-! ## The relation -/ + +/-- **Heap free; the machine's opaque part and the VMID fields free** (module docstring). -/ +def QvA (σ σ' : Fl4SpecA64.State Unit) : Prop := VmA { σ with kheap := σ'.kheap } σ' + +theorem QvA.refl (σ : Fl4SpecA64.State Unit) : QvA σ σ := vmA_refl σ + +theorem QvA.trans {a b c : Fl4SpecA64.State Unit} (h1 : QvA a b) (h2 : QvA b c) : QvA a c := by + obtain ⟨m1, a1, v1, n1, e1⟩ := h1 + have h3 : VmA { a with kheap := c.kheap } { b with kheap := c.kheap } := + ⟨m1, a1, v1, n1, by rw [e1] <;> rfl⟩ + exact vmA_trans _ _ _ h3 h2 + +/-- **Page-table objects only get entries invalidated** (every other object kept). -/ +def InvA (σ σ' : Fl4SpecA64.State Unit) : Prop := + ∀ q, σ'.kheap q = σ.kheap q ∨ ∃ t t', σ.kheap q = some (.archObj (.pageTable t)) ∧ + σ'.kheap q = some (.archObj (.pageTable t')) ∧ Fl4SpecA64.ptType t' = Fl4SpecA64.ptType t ∧ + ∀ i, Fl4SpecA64.ptApply t' i = Fl4SpecA64.ptApply t i ∨ Fl4SpecA64.ptApply t' i = .invalidPTE + +theorem InvA.refl (σ : Fl4SpecA64.State Unit) : InvA σ σ := fun _ => Or.inl rfl + +theorem InvA.trans {a b c : Fl4SpecA64.State Unit} (h1 : InvA a b) (h2 : InvA b c) : InvA a c := fun q => by + rcases h1 q with e1 | ⟨t, t', a1, b1, ty1, ap1⟩ + · rcases h2 q with e2 | ⟨u, u', a2, b2, ty2, ap2⟩ + · exact Or.inl (e2.trans e1) + · exact Or.inr ⟨u, u', e1.symm.trans a2, b2, ty2, ap2⟩ + · rcases h2 q with e2 | ⟨u, u', a2, b2, ty2, ap2⟩ + · exact Or.inr ⟨t, t', a1, e2.trans b1, ty1, ap1⟩ + · rw [b1] at a2 + obtain rfl : t' = u := by injection a2 with h; injection h with h; injection h + refine Or.inr ⟨t, u', a1, b2, ty2.trans ty1, fun i => ?_⟩ + rcases ap2 i with h | h + · rcases ap1 i with h' | h' + · exact Or.inl (h.trans h') + · exact Or.inr (h.trans h') + · exact Or.inr h + +/-- Heap: only invalidations; the machine: opaque part and VMID fields. -/ +def UnQ (σ σ' : Fl4SpecA64.State Unit) : Prop := QvA σ σ' ∧ InvA σ σ' + +theorem UnQ.refl (σ : Fl4SpecA64.State Unit) : UnQ σ σ := ⟨QvA.refl σ, InvA.refl σ⟩ + +theorem UnQ.trans {a b c : Fl4SpecA64.State Unit} (h1 : UnQ a b) (h2 : UnQ b c) : UnQ a c := + ⟨QvA.trans h1.1 h2.1, InvA.trans h1.2 h2.2⟩ + +abbrev trQ : ∀ a b c : Fl4SpecA64.State Unit, UnQ a b → UnQ b c → UnQ a c := fun _ _ _ => UnQ.trans + +/-! ## The spec operations -/ + +theorem ptUpd_inv (pt : Fl4SpecA64.PT) (idx : Fl4SpecA64.Word) : + Fl4SpecA64.ptType (Fl4SpecA64.ptUpd pt idx .invalidPTE) = Fl4SpecA64.ptType pt ∧ + ∀ i, Fl4SpecA64.ptApply (Fl4SpecA64.ptUpd pt idx .invalidPTE) i = Fl4SpecA64.ptApply pt i ∨ + Fl4SpecA64.ptApply (Fl4SpecA64.ptUpd pt idx .invalidPTE) i = .invalidPTE := by + cases pt with + | normalPT f => + refine ⟨rfl, fun i => ?_⟩ + simp only [Fl4SpecA64.ptUpd, Fl4SpecA64.ptApply, Function.update_apply] + split_ifs <;> simp + | vsRootPT f => + refine ⟨rfl, fun i => ?_⟩ + simp only [Fl4SpecA64.ptUpd, Fl4SpecA64.ptApply, Function.update_apply] + split_ifs <;> simp + +theorem getPt_memA {σ : Fl4SpecA64.State Unit} {q : Fl4SpecA64.ObjRef} {x : Fl4SpecA64.PT × Fl4SpecA64.State Unit} + (h : x ∈ (Fl4SpecA64.getPt (Z := Unit) q σ).1) : + x.2 = σ ∧ σ.kheap q = some (.archObj (.pageTable x.1)) := by + obtain ⟨y, hy, h⟩ := h + have hy' : y = (Fl4SpecA64.ptsOf σ, σ) := hy + subst hy' + cases hp : Fl4SpecA64.ptsOf σ q with + | none => simp only [hp] at h; exact absurd h (Set.notMem_empty _) + | some a => + simp only [hp] at h + obtain rfl : x = (a, σ) := h + exact ⟨rfl, ptsOf_someX hp⟩ + +/-- **`store_pte` of an invalid entry** invalidates entries of one table. -/ +theorem storePte_unQ (ptT : Fl4SpecA64.PtType) (p : Fl4SpecA64.ObjRef) : + OutQ UnQ (Fl4SpecA64.storePte (Z := Unit) ptT p .invalidPTE) := fun σ r hr => by + unfold Fl4SpecA64.storePte at hr + obtain ⟨x1, hx1, hr⟩ := mem_bindA.1 hr + obtain ⟨-, rfl⟩ := hx1 + obtain ⟨x2, hx2, hr⟩ := mem_bindA.1 hr + obtain rfl : x2 = (Fl4SpecA64.tableBase ptT p, σ) := hx2 + obtain ⟨x3, hx3, hr⟩ := mem_bindA.1 hr + obtain ⟨e3, k3⟩ := getPt_memA hx3 + obtain ⟨x4, hx4, hr⟩ := mem_bindA.1 hr + obtain ⟨-, rfl⟩ := hx4 + unfold Fl4SpecA64.setPt at hr + obtain ⟨x5, hx5, hr⟩ := mem_bindA.1 hr + obtain ⟨e5, k5⟩ := getPt_memA hx5 + obtain ⟨o0, k0, e⟩ := setObject_mem hr + have hx : x5.2 = σ := e5.trans e3 + rw [hx] at e + obtain ⟨hty, hap⟩ := ptUpd_inv x3.1 (Fl4SpecA64.tableIndex ptT p) + have kb : σ.kheap (Fl4SpecA64.tableBase ptT p) = some (.archObj (.pageTable x3.1)) := k3 + have e' : r.2 = { σ with kheap := (Function.update σ.kheap (Fl4SpecA64.tableBase ptT p) (some (Fl4SpecA64.KernelObject.archObj (Fl4SpecA64.ArchKernelObj.pageTable (Fl4SpecA64.ptUpd x3.1 (Fl4SpecA64.tableIndex ptT p) Fl4SpecA64.PTE.invalidPTE))))) } := e + refine ⟨?_, fun q => ?_⟩ + · show VmA { σ with kheap := r.2.kheap } r.2 + rw [e'] + exact vmA_refl _ + · rw [e'] + by_cases hq : q = Fl4SpecA64.tableBase ptT p + · subst hq + exact Or.inr ⟨x3.1, _, kb, by simp only [Function.update_self], hty, hap⟩ + · exact Or.inl (by simp only [Function.update_of_ne hq]) + +theorem doMachineOp_unQ (g : Fl4SpecA64.MachineRestMonad Unit) : + OutQ UnQ (Fl4SpecA64.doMachineOp (Z := Unit) (Fl4SpecA64.machineOpLift g)) := fun σ r hr => by + have h := (doMachineOp_vmA g σ).2.2 r hr + have e := h.kheap + refine ⟨?_, fun q => Or.inl (by rw [e])⟩ + show VmA { σ with kheap := r.2.kheap } r.2 + rw [e] + exact h + +theorem invTlbVa_unQ (asid : Fl4SpecA64.Asid) (v : Fl4SpecA64.VSpaceRef) : + OutQ UnQ (Fl4SpecA64.invalidateTlbByAsidVa (Z := Unit) asid v) := by + unfold Fl4SpecA64.invalidateTlbByAsidVa Fl4SpecA64.loadVmid + refine OutQ.bind trQ (OutQ.bind trQ (OutQ.gets UnQ.refl _) fun _ => OutQ.pure UnQ.refl _) fun o => ?_ + cases o with + | none => exact OutQ.pure UnQ.refl _ + | some _ => exact doMachineOp_unQ _ + +theorem findVs_unQ (asid : Fl4SpecA64.Asid) : OutQ UnQ (Fl4SpecA64.findVspaceForAsid (Z := Unit) asid).run := by + unfold Fl4SpecA64.findVspaceForAsid + refine OutQ.ebind trQ (OutQ.liftE (OutQ.gets UnQ.refl _)) fun _ => + OutQ.ebind trQ (OutQ.liftE (OutQ.gets UnQ.refl _)) fun o => ?_ + cases o with + | none => exact OutQ.one UnQ.refl fun _ => ⟨_, rfl⟩ + | some a => exact OutQ.ret UnQ.refl a + +/-- **l4v's `unmap_page` is a `UnQ` step**: the machine's opaque part changes, page-table entries are invalidated. -/ +theorem unmapPage_unQ (sz : Fl4SpecA64.VMPageSize) (asid : Fl4SpecA64.Asid) (v : Fl4SpecA64.VSpaceRef) + (p : Fl4SpecA64.ObjRef) : OutQ UnQ (Fl4SpecA64.unmapPage (Z := Unit) sz asid v p) := by + unfold Fl4SpecA64.unmapPage + refine OutQ.catch trQ ?_ fun _ => OutQ.pure UnQ.refl _ + refine OutQ.ebind trQ (findVs_unQ _) fun _ => ?_ + refine OutQ.ebind trQ (OutQ.liftE (OutQ.getsThe UnQ.refl _)) fun x => ?_ + obtain ⟨lev, slot⟩ := x + refine OutQ.ebind trQ (OutQ.unlessE UnQ.refl (OutQ.throw UnQ.refl _)) fun _ => ?_ + refine OutQ.ebind trQ (OutQ.liftE (OutQ.getsMap UnQ.refl trQ _ _)) fun _ => ?_ + refine OutQ.ebind trQ (OutQ.unlessE UnQ.refl (OutQ.throw UnQ.refl _)) fun _ => ?_ + refine OutQ.ebind trQ (OutQ.liftE (storePte_unQ _ _)) fun _ => ?_ + refine OutQ.ebind trQ (OutQ.liftE (doMachineOp_unQ _)) fun _ => ?_ + exact OutQ.liftE (invTlbVa_unQ _ _) + +/-- **A `QvA` step that replaces arch objects by arch objects and keeps the ASID table is a `VsSpecA` step.** -/ +theorem VsSpecA.of_qvA {σ σ' : Fl4SpecA64.State Unit} (h : QvA σ σ') + (hkh : ∀ p, σ'.kheap p = σ.kheap p ∨ ∃ a a', σ.kheap p = some (.archObj a) ∧ + σ'.kheap p = some (.archObj a') ∧ (∀ v, a ≠ .vcpu v) ∧ (∀ v, a' ≠ .vcpu v)) + (hasid : σ'.archState.armAsidTable = σ.archState.armAsidTable) : VsSpecA σ σ' := by + obtain ⟨mr, am, vt, nv, e⟩ := h + have em : σ'.machineState = { σ.machineState with machineStateRest := mr } := + congrArg (fun x : Fl4SpecA64.State Unit => x.machineState) e + have ea : σ'.archState = { σ.archState with armAsidMap := am, armVmidTable := vt, armNextVmid := nv } := + congrArg (fun x : Fl4SpecA64.State Unit => x.archState) e + have e1 : σ'.machineState.machineStateRest = mr := by rw [em] + have e2 : σ'.archState.armAsidMap = am := by rw [ea] + have e3 : σ'.archState.armVmidTable = vt := by rw [ea] + have e4 : σ'.archState.armNextVmid = nv := by rw [ea] + refine ⟨?_, hkh, fun i p h => by rw [← hasid]; exact h⟩ + conv_lhs => rw [e] + rw [e1, e2, e3, e4, hasid] + +/-- **A `UnQ` step is a `VsSpecA` step.** -/ +theorem VsSpecA.of_unQ {σ σ' : Fl4SpecA64.State Unit} (h : UnQ σ σ') : VsSpecA σ σ' := + VsSpecA.of_qvA h.1 (fun p => by + rcases h.2 p with e | ⟨t, t', a, b, -, -⟩ + · exact Or.inl e + · exact Or.inr ⟨_, _, a, b, (fun _ e => by cases e), (fun _ e => by cases e)⟩) h.1.asidTable + + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3VcpuBridgeA.lean b/proofs/Fl4ProofsA64/P3/Stage3VcpuBridgeA.lean new file mode 100644 index 000000000..a615a5058 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3VcpuBridgeA.lean @@ -0,0 +1,1443 @@ +/- +P3 (aarch64, A64-P3 S4 VCPU): **record-level bridge (`JSyscallA` ↔ `VcpuDecCtxA` ↔ `JStage3A`)** +and Plan B `hpin`-compatible decode pinning for the VCPU invocation arms and VM-exit entries. + +* **`Vcpu8bCA`**: the 3 residual `#8b-C` premises kept named until record edit #8b-C lands + (`vcOwner`: `mem(v+8) ≤ 128`, `runOutMask`: `runOutMask &&& ~~~smAll = 0`, `bind_run_disj`). +* **Derived lemmas (zero new record fields, Architect ruling 2026-12-04):** + - `JStage3A.vcpuLiveCapA`: `VcpuLiveCapA` from `DeadExtA.vcpu` + `DeadExtA.vcDeadRun`. + - `JStage3A.idleNoVcpuA`: `idle_no_vcpu` from `SIdleA.idleBound` (#8b-A) + `RunVcpuInv` + `SIdleA.f3`. + - `JStage3A.vcPlacedA`: `VcPlacedA` from `J7UnpA` + `JStage3A.vcOS` + `HeapOK` + `J5A`. + - `JStage3A.tcbRegsRelA`: `TcbRegsRelA` from `JStage3A.ep` (`TcbRelE.regs`). + - `JStage3A.ubUserA`: `∀ q ∈ Ub, inUserFrame (specPtr q) σ` from `UbFr` + `UserFrSpecA`. + - `JStage3A.vcpuRelA` / `JSyscallA.vcpuDecCtxA`: assemble `VcpuRelA` and `VcpuDecCtxA` from `JSyscallA`. +* **Plan B `hpin`-compatible front-end & decode bridge (`VcpuFrontPinA`, `VcpuDecPinA`, + `fl4DecodeInvocation_vcpu_of_archA`)**: identical equation shape to Plan B's `FrontPinA`/`DecPinA` + so wiring to Plan B is a 1-line projection. +* **Record-level post lifters:** + - `JStage3A.of_vcpuNonLinkWrA`: lifts a non-link VCPU word write + `updKSc` (`AckVPPI`, `WriteReg`, `InjectIRQ`) + from `J1` (post-`of_restartCurA`) + `decodeFinA` to full `JStage3A`. + - `JStage3A.of_reply1A`: lifts `vspace.reply1` (`ReadReg`) from `J1` (post-`of_restartCurA`) + `decodeFinA` + to full `JStage3A`. + +Named premises: `[P]` `ArchTotal`, `MaxSpi`, `ArchPlat`, `A64FrameMasks`; `[S]` `h1` (`numNodes = 1`, stall); +`#8b-C` `Vcpu8bCA` (3 fields). New module. +-/ +import Fl4ProofsA64.P3.VcpuRegJA +import Fl4ProofsA64.P3.VcpuXferBaseA +import Fl4ProofsA64.P3.VcpuDelFrameA +import Fl4ProofsA64.P3.Stage3FinA +import Fl4ProofsA64.P3.Stage3RestartA +import Fl4ProofsA64.P3.Stage3RsSiteA +import Fl4ProofsA64.P3.Stage3SyscallA +import Fl4ProofsA64.P3.DelScStallA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp + Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Vcpu Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section bridge +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-! ### Derived lemmas from `JStage3A` (zero new record fields) -/ + +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J5 J7 : JSlotA S} + {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- **`VcpuLiveCapA`** derived from `DeadExtA.vcpu` and `DeadExtA.vcDeadRun`. -/ +theorem JStage3A.vcpuLiveCapA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) : + ∀ v ∈ Z.V, (L.mem s v ≠ 0#u64 ∨ L.mem s (wa v 24#u64) ≠ 0#u64) → + ∃ y ∈ OS, capAtA (L.mem s) y = .Vcpu v := by + intro v hv hor + by_contra hnone + have hdead : DeadA st OS .vcpu s v := by + intro y hy hc + have hnot : capAtA (L.mem s) y ≠ .Vcpu v := fun e => hnone ⟨y, hy, e⟩ + rw [capAtA_eq] at hnot + generalize hce : capA st s y = c at hc hnot + cases c <;> simp [NamesDA] at hc + subst hc; exact hnot rfl + rcases hor with h0 | h24 + · obtain ⟨x, hkx, hvt, -⟩ := J.j.2.1.obj v hv + have hn := J.deadExt.vcpu v hv hdead x hkx + rw [hn] at hvt + unfold optRefA at hvt + split at hvt + · exact h0 (by assumption) + · cases hvt + · exact h24 (J.deadExt.vcDeadRun v hv hdead) + +/-- **`idle_no_vcpu`** derived from `#8b-A` (`SIdleA.idleBound`), `SIdleA.f3`, and `RunVcpuInv`. -/ +theorem JStage3A.idleNoVcpuA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (hCsN : ∀ c ∈ Z.Cs, c.val < n) : + ∀ c ∈ Z.Cs, + L.mem s (wa (L.mem s (L.nodeAt s c layout.node.IDLE_THREAD)) 3664#u64) = 0#u64 ∧ + L.mem s (runVcpuA (L.mem s (L.nodeAt s c layout.node.IDLE_THREAD))) = 0#u64 := by + intro c hc + set it := L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) + have hit : it ∈ Z.T := J.j.1.idleT c hc + have hspec : m.idleThreadOf c.val = specPtr it := J.j.1.sch.idleOf c hc + have hcn : c.val < n := hCsN c hc + constructor + · by_contra hne + obtain ⟨xt, hxt, -⟩ := J.ep.tcb it hit + have hbd := J.sidle.idleBound c.val hcn + rw [hspec] at hbd + unfold vcpuOfA at hbd + rw [hxt] at hbd + dsimp only at hbd + obtain ⟨xt', hxt', htv, -⟩ := J.j.2.1.tcb it hit + rw [hxt] at hxt'; cases hxt' + rw [hbd] at htv + unfold optRefA at htv + split at htv + · exact hne (by assumption) + · cases htv + · by_contra hne + have hst : L.mem s (stA it) = sched.TS_RUNNING_VM := J.runVcpu it hit hne + obtain ⟨x, hx, hts⟩ := J.sidle.f3 c.val hcn + rw [hspec] at hx + obtain ⟨x0, hx0, hrel⟩ := J.ep.tcb it hit + rw [hx] at hx0; cases hx0 + have hstVal := hrel.st + rw [hst, hts] at hstVal + simp [sched.TS_RUNNING_VM, tsCode] at hstVal + +/-- **`TcbRegsRelA`** derived directly from `JStage3A.ep` (`TcbRelE.regs`). -/ +theorem JStage3A.tcbRegsRelA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) : + TcbRegsRelA (L.mem s) σ Z.T := by + intro t ht x hkx r + obtain ⟨x0, hx0, hrel⟩ := J.ep.tcb t ht + rw [hx0] at hkx; cases hkx + exact hrel.regs r + +private theorem mask_page_eqA (q ptr : U64) (k : Nat) (hk : k ≤ 39) + (hal : ptr.val % 2 ^ k = 0) (hle : ptr.val ≤ q.val) (hlt : q.val < ptr.val + 2 ^ k) : + (specPtr q &&& ~~~(@Fl4SpecA64.mask 64 k)) = specPtr ptr := by + have hk64 : k < 64 := by omega + have hpos : 0 < 2 ^ k := Nat.pow_pos (by decide) + have hlt64 : 2 ^ k < 2 ^ 64 := Nat.pow_lt_pow_right (by decide) hk64 + have e : ~~~(@Fl4SpecA64.mask 64 k) = BitVec.allOnes 64 <<< k := by + apply BitVec.eq_of_toNat_eq + unfold Fl4SpecA64.mask + rw [BitVec.toNat_not, BitVec.toNat_sub, BitVec.toNat_shiftLeft, BitVec.toNat_allOnes] + have h1 : (1 : BitVec 64).toNat = 1 := rfl + have hcast : ((1 <<< k : Nat) : BitVec 64).toNat = 2 ^ k := by + change (BitVec.ofNat 64 (1 <<< k)).toNat = 2 ^ k + rw [BitVec.toNat_ofNat, Nat.shiftLeft_eq, Nat.one_mul, Nat.mod_eq_of_lt hlt64] + rw [h1, hcast] + simp only [Nat.shiftLeft_eq] + have hge1 : 1 ≤ 2 ^ 64 := Nat.lt_trans hpos hlt64 + have hsub : (2 ^ 64 - 1 + 2 ^ k) % 2 ^ 64 = 2 ^ k - 1 := by + have hsum : 2 ^ 64 - 1 + 2 ^ k = 2 ^ 64 + (2 ^ k - 1) := by omega + rw [hsum, Nat.add_mod_left, Nat.mod_eq_of_lt (Nat.lt_of_le_of_lt (Nat.sub_le _ _) hlt64)] + have hmul : (2 ^ 64 - 1) * 2 ^ k = 2 ^ 64 * (2 ^ k - 1) + (2 ^ 64 - 2 ^ k) := by + rw [Nat.mul_sub_right_distrib, Nat.one_mul, Nat.mul_sub_left_distrib, Nat.mul_one] + have hle' : 2 ^ 64 ≤ 2 ^ 64 * 2 ^ k := Nat.le_mul_of_pos_right _ hpos + have hle2 : 2 ^ k ≤ 2 ^ 64 := Nat.le_of_lt hlt64 + omega + have hlt_rem : 2 ^ 64 - 2 ^ k < 2 ^ 64 := Nat.sub_lt (Nat.lt_trans hpos hlt64) hpos + rw [hsub, hmul, Nat.mul_add_mod, Nat.mod_eq_of_lt hlt_rem] + exact Nat.sub_sub_sub_cancel_right hpos + rw [e, ← BitVec.shiftLeft_ushiftRight] + apply BitVec.eq_of_toNat_eq + rw [BitVec.toNat_shiftLeft, BitVec.toNat_ushiftRight] + simp only [Nat.shiftLeft_eq, Nat.shiftRight_eq_div_pow] + have hqlt : (specPtr q).toNat / 2 ^ k * 2 ^ k < 2 ^ 64 := + Nat.lt_of_le_of_lt (Nat.div_mul_le_self _ _) (specPtr q).isLt + rw [Nat.mod_eq_of_lt hqlt] + have hsp_al : (specPtr ptr).toNat % 2 ^ k = 0 := (specPtr_mod ptr hk).trans hal + have hsp_ptr : (specPtr ptr).toNat = (specPtr ptr).toNat / 2 ^ k * 2 ^ k := by + have := Nat.div_add_mod (specPtr ptr).toNat (2 ^ k) + rw [Nat.mul_comm, hsp_al, Nat.add_zero] at this + exact this.symm + have hsp_lt : (specPtr ptr).toNat + 2 ^ k ≤ 2 ^ 64 := by + have h64 : 2 ^ 64 = (2 ^ 64 / 2 ^ k) * 2 ^ k := by + have hdiv := Nat.div_mul_cancel (Nat.pow_dvd_pow 2 (Nat.le_of_lt hk64)) + exact hdiv.symm + have hq_lt : (specPtr ptr).toNat / 2 ^ k < 2 ^ 64 / 2 ^ k := by + by_contra hge + have h1 : 2 ^ 64 / 2 ^ k ≤ (specPtr ptr).toNat / 2 ^ k := Nat.le_of_not_lt hge + have h2 := Nat.mul_le_mul_right (2 ^ k) h1 + rw [← h64, ← hsp_ptr] at h2 + exact Nat.not_le_of_lt (specPtr ptr).isLt h2 + have h3 := Nat.mul_le_mul_right (2 ^ k) (Nat.succ_le_of_lt hq_lt) + rw [Nat.succ_mul, ← hsp_ptr, ← h64] at h3 + exact h3 + set off : U64 := ⟨BitVec.ofNat 64 (q.val - ptr.val)⟩ + have hoff_val : off.val = q.val - ptr.val := by + change (BitVec.ofNat 64 (q.val - ptr.val)).toNat = q.val - ptr.val + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)] + have hq_eq : q = wa ptr off := by + apply UScalar.eq_of_val_eq + rw [wa_valP, hoff_val, Nat.add_sub_of_le hle] + exact (Nat.mod_eq_of_lt (show q.val < 2 ^ 64 from q.bv.isLt)).symm + have hsp_q : (specPtr q).toNat = (specPtr ptr).toNat + off.val := by + rw [hq_eq, specPtr_wa, BitVec.toNat_add] + change ((specPtr ptr).toNat + off.val) % 2 ^ 64 = _ + rw [Nat.mod_eq_of_lt (by omega)] + have hdiv : (specPtr q).toNat / 2 ^ k = (specPtr ptr).toNat / 2 ^ k := by + apply Nat.div_eq_of_lt_le + · rw [← hsp_ptr, hsp_q]; exact Nat.le_add_right _ _ + · rw [Nat.succ_mul, ← hsp_ptr, hsp_q]; omega + rw [hdiv, ← hsp_ptr] + +/-- **`hUbUser`**: every word in `Ub` lies in a spec user data page, from `UbFr` + `UserFrSpecA`. -/ +theorem JStage3A.ubUserA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {Ub : Set U64} (huf : UbFr st OS Ub s) (hfo : FrameOK st H OS s) : + ∀ q ∈ Ub, inUserFrame (specPtr q) σ := by + intro q hq + obtain ⟨y, hy, ptr, size, rights, x1, x2, x3, x4, bits, hcap, hbits, hle, hlt⟩ := huf q hq + have hdp := J.userFr y hy ptr size rights x1 x2 x3 x4 hcap + have hfp := hfo y hy ptr size rights x1 x2 x3 x4 hcap bits hbits + have hal : ptr.val % 2 ^ bits.val = 0 := by + have h := J.sys.ent.heap.align ptr _ hfp + simpa [HKind.bits] using h + have hpb : pageBitsForSize (frameSizeOfA size) = bits.val := by + cases size <;> simp [frameSizeOfA, pageBitsForSize, capability.FrameSize.bits] at hbits ⊢ <;> + cases hbits <;> decide + have hle39 : bits.val ≤ 39 := by + cases size <;> simp [capability.FrameSize.bits] at hbits <;> cases hbits <;> decide + refine ⟨frameSizeOfA size, ?_⟩ + rw [hpb, mask_page_eqA q ptr bits.val hle39 hal hle hlt] + exact hdp + +/-- **`VcPlacedA`** derived from `J7UnpA` + `JStage3A.vcOS` + `HeapOK` + `J5A`. -/ +theorem JStage3A.vcPlacedA {ar : machine.Arch A} {nA : U64} + {Pt Ub Pp7 Tb7 Hr Te Kt : Set U64} {Lv7 Re bits KLv : U64 → Nat} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s a σ m) + (W : J7UnpA st ar nA Z H OS s Pt Ub Pp7 Tb7 Hr Te Kt Lv7 Re bits KLv) : + VcPlacedA Z OS Ub (L.ksb s) where + al := fun v hv => by + have hvH := W.col.vh v hv + have hal := J.sys.ent.heap.align v _ hvH; simp only [HKind.bits] at hal + have hfit := J.sys.ent.heap.fits v _ hvH; simp only [HKind.bits] at hfit + have hwin := (J.heapWin v _ hvH).1 + have hnz : v ≠ 0#u64 := by + intro h0; rw [h0] at hwin + have h0v : (0#u64 : U64).val = 0 := rfl + have hpv := Fl4ProofsA64.P0.Consts.layout.PPTR_BASE.val_eq + rw [h0v, hpv] at hwin; omega + exact ⟨hnz, hal, hfit⟩ + al_t := fun t ht => by + have h := (J.sq.tcbs t (J.tcbs ▸ ht)).align + change t.val % 8 = 0; omega + al_os := fun _ hy => J.sys.ent.os.al0 hy + al_slot := fun y hy => J.sys.ent.os.al0 (J.j5.2.1 y hy) + al_ksb := J.toJEntA.vsLeafA.1 + disj_os := fun b hb y hy => by + have hnos := J.vcOS b hb + have hyfit := J.sys.ent.os.fit y hy + have h8v : (8#u64 : U64).val = 8 := rfl + have h16v : (16#u64 : U64).val = 16 := rfl + have h24v : (24#u64 : U64).val = 24 := rfl + have hlt8 : (wa y 8#u64).val = y.val + 8 := by rw [wa_valP, h8v]; exact Nat.mod_eq_of_lt (by omega) + have hlt16 : (wa y 16#u64).val = y.val + 16 := by rw [wa_valP, h16v]; exact Nat.mod_eq_of_lt (by omega) + have hlt24 : (wa y 24#u64).val = y.val + 24 := by rw [wa_valP, h24v]; exact Nat.mod_eq_of_lt (by omega) + refine ⟨fun e => hnos ⟨y, hy, e ▸ le_refl _, by rw [e]; omega⟩, + fun e => hnos ⟨y, hy, by rw [e, hlt8]; omega, by rw [e, hlt8]; omega⟩, + fun e => hnos ⟨y, hy, by rw [e, hlt16]; omega, by rw [e, hlt16]; omega⟩, + fun e => hnos ⟨y, hy, by rw [e, hlt24]; omega, by rw [e, hlt24]; omega⟩⟩ + disj_slots := fun b hb y hy => by + have hnos := J.vcOS b hb + have hyOS := J.j5.2.1 y hy + have hyfit := J.sys.ent.os.fit y hyOS + have h8v : (8#u64 : U64).val = 8 := rfl + have hlt8 : (wa y 8#u64).val = y.val + 8 := by rw [wa_valP, h8v]; exact Nat.mod_eq_of_lt (by omega) + exact ⟨fun e => hnos ⟨y, hyOS, e ▸ le_refl _, by rw [e]; omega⟩, + fun e => hnos ⟨y, hyOS, by rw [e, hlt8]; omega, by rw [e, hlt8]; omega⟩⟩ + disj_tcb := fun v hv t ht ov ot hov hot heq => by + have hvH := W.col.vh v hv + have htH : H t = some .tcb := by have : t ∈ H.tcbs := J.tcbs ▸ ht; exact this + have hne : v ≠ t := fun e => by rw [e, htH] at hvH; cases hvH + have hd := J.sys.ent.heap.disj v _ t _ hvH htH hne + simp only [HKind.bits] at hd + have hf1 := J.sys.ent.heap.fits v _ hvH; simp only [HKind.bits] at hf1 + have hf2 := J.sys.ent.heap.fits t _ htH; simp only [HKind.bits] at hf2 + have h1 : (wa v ov).val = v.val + ov.val := by rw [wa_valP]; exact Nat.mod_eq_of_lt (by omega) + have h2 : (wa t ot).val = t.val + ot.val := by rw [wa_valP]; exact Nat.mod_eq_of_lt (by omega) + have hval := congrArg UScalar.val heq; rw [h1, h2] at hval; omega + disj_sc := fun v hv c hc ov oc hov hoc heq => by + have hvH := W.col.vh v hv + have hcH : c ∈ H.scs := J.scs ▸ hc + obtain ⟨k, hk⟩ := hcH + have hmin := J.sys.ent.heap.scmin c k hk + have hpow : 128 ≤ 2 ^ k := by + have : 2 ^ 7 ≤ 2 ^ k := Nat.pow_le_pow_right (by omega) hmin; simpa using this + have hne : v ≠ c := fun e => by rw [e, hk] at hvH; cases hvH + have hd := J.sys.ent.heap.disj v _ c _ hvH hk hne + simp only [HKind.bits] at hd + have hf1 := J.sys.ent.heap.fits v _ hvH; simp only [HKind.bits] at hf1 + have hf2 := J.sys.ent.heap.fits c _ hk; simp only [HKind.bits] at hf2 + have h1 : (wa v ov).val = v.val + ov.val := by rw [wa_valP]; exact Nat.mod_eq_of_lt (by omega) + have h2 : (wa c oc).val = c.val + oc.val := by rw [wa_valP]; exact Nat.mod_eq_of_lt (by omega) + have hval := congrArg UScalar.val heq; rw [h1, h2] at hval; omega + disj_ks := fun b hb hks => by + rcases hb with ⟨v, hv, o, ho, ho8, rfl⟩ + have hvH := W.col.vh v hv + have hf1 := J.sys.ent.heap.fits v _ hvH; simp only [HKind.bits] at hf1 + have hbv : (wa v o).val = v.val + o.val := by rw [wa_valP]; exact Nat.mod_eq_of_lt (by omega) + rw [hbv] at hks + have hko := J.ko.2 v 12 hvH + have hksf := J.j.1.ksFit; unfold ksSizeA at hks hksf + rcases hko with h1 | h2 | ⟨h3a, h3b⟩ | ⟨h4a, h4b⟩ + · omega + · omega + · -- `v + o ∈ [ksb, ksb + 223488)`, contradicting `W.ks` + let off : U64 := ⟨BitVec.ofNat 64 (v.val + o.val - (L.ksb s).val)⟩ + have hoff_val : off.val = v.val + o.val - (L.ksb s).val := by + change (BitVec.ofNat 64 _).toNat = _; rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + have heq : wa (L.ksb s) off = wa v o := by + apply UScalar.eq_of_val_eq + rw [wa_valP, hbv, hoff_val, Nat.mod_eq_of_lt (by omega)]; omega + exact W.ks (wa v o) ⟨v, hv, o, ho, ho8, rfl⟩ off (by omega) heq + · omega + disj_vs := fun b hb hvs => by + have hvsr : VsR (L.ksb s) Pt b := by + rcases hvs.vsR with ⟨p, hp, h⟩ | h + · refine Or.inl ⟨p, W.sp p ?_, h⟩ + rcases hp with hp | hp | hp + · exact Or.inl hp + · exact Or.inr (Or.inl hp) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl hp)))))) + · exact Or.inr h + exact W.vr.vc b hvsr hb + disj_ub := fun b hb => W.vu b hb + +/-- Full `VcpuRelA` assembled from `JStage3A` + `J7UnpA` (all `#8b-C` fields discharged from record). -/ +theorem JStage3A.vcpuRelA {ar : machine.Arch A} {nA : U64} + {Pt Ub Pp7 Tb7 Hr Te Kt : Set U64} {Lv7 Re bits KLv : U64 → Nat} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s a σ m) + (W : J7UnpA st ar nA Z H OS s Pt Ub Pp7 Tb7 Hr Te Kt Lv7 Re bits KLv) + (hCsN : ∀ c ∈ Z.Cs, c.val < n) : + VcpuRelA (L := L) Z OS Ub s σ where + core := J.j.2.1 + owner := fun v hv => by + obtain ⟨x, hkx, -, -⟩ := J.j.2.1.obj v hv + exact (J.vcSnap.snap v hv x hkx).2.2.2.2 + vppi := fun v hv x hkx => (J.vcSnap.snap v hv x hkx).2.1 + runOff := fun v hv x hkx => (J.vcSnap.snap v hv x hkx).2.2.1 + runOut := fun v hv x hkx => ⟨(J.vcSnap.snap v hv x hkx).2.2.2.1, J.vcSnap.mask v hv x hkx⟩ + regs := fun v hv x hkx i hi => (J.vcSnap.snap v hv x hkx).1 i hi + bind_v2t := fun v hv _ ht hne => + ⟨(J.vcRun.vtcb v hv).resolve_left (ht ▸ hne) |> fun h => ht ▸ h, ht ▸ J.vcBack v hv (ht ▸ hne)⟩ + bind_t2v := fun t ht _ hv hne => + ⟨(J.vcRun.tcb t ht).resolve_left (hv ▸ hne) |> fun h => hv ▸ h, hv ▸ J.vcSym1 t ht (hv ▸ hne)⟩ + run_v2c := fun v hv _ hc hne => + ⟨(J.vcRun.vcpu v hv).resolve_left (hc ▸ hne) |> fun h => hc ▸ h, hc ▸ J.vcSym2 v hv (hc ▸ hne)⟩ + run_c2v := fun c hc _ hv hne => + ⟨(J.vcRun.run c hc).resolve_left (hv ▸ hne) |> fun h => hv ▸ h, hv ▸ J.runBack c hc (hv ▸ hne)⟩ + run_state := J.runVcpu + bind_run_disj := J.vcRun.disj + idle_no_vcpu := J.idleNoVcpuA hCsN + live := J.vcpuLiveCapA + arch := J.curVcpuNone + place := J.vcPlacedA W + +/-! ### Plan B `hpin`-compatible front-end & decode bridge -/ + +/-- Exact Plan B `FrontPinA` equation bundle (7 fields), so wiring to Plan B is a 1-line projection. -/ +structure VcpuFrontPinA (th : TypedHeapA) (σ : State Unit) (slot : U64) (mi : MessageInfo) (sl : CslotPtr) + (c : Fl4SpecA64.Cap) (excaps : List (Fl4SpecA64.Cap × CslotPtr)) (buffer : Option ObjRef) + (args : List Fl4SpecA64.Data) : Prop where + hmi : getMessageInfo (Z := Unit) σ.curThread σ = ({(mi, σ)}, False) + hslot : slot = cslotAddrA sl + habs : AbsSlotA th sl + hcap : getCap (Z := Unit) sl σ = ({(c, σ)}, False) + hbuf : lookupIpcBuffer (Z := Unit) false σ.curThread σ = ({(buffer, σ)}, False) + hex : (lookupExtraCaps (Z := Unit) σ.curThread buffer mi).run σ = ({(.ok excaps, σ)}, False) + hargs : getMrs (Z := Unit) σ.curThread buffer mi σ = ({(args, σ)}, False) + +/-- Exact Plan B `DecPinA` equation shape (membership in `fl4DecodeInvocation`). -/ +def VcpuDecPinA (calling : Bool) (th : TypedHeapA) (σ : State Unit) (slot : U64) (oper : Fl4Invocation) : Prop := + ∃ mi sl c excaps buffer args, VcpuFrontPinA th σ slot mi sl c excaps buffer args ∧ + ∀ cptr : CapRef, (.ok oper, σ) ∈ + ((fl4DecodeInvocation (Z := Unit) calling false mi.miLabel args cptr sl c excaps buffer).run σ).1 + +/-- Lift `fl4ArchDecodeInvocation` `.ok inv` on `.vcpuCap` to `fl4DecodeInvocation` membership. -/ +theorem fl4DecodeInvocation_vcpu_of_archA {calling : Bool} {lbl : Fl4SpecA64.Data} + {args : List Fl4SpecA64.Data} {cptr : CapRef} {sl : CslotPtr} {v : ObjRef} + {excaps : List (Fl4SpecA64.Cap × CslotPtr)} {buffer : Option ObjRef} + {σ : State Unit} {inv : Fl4ArchInvocation} + (h : (fl4ArchDecodeInvocation (Z := Unit) calling (fl4LabelOf lbl) args cptr sl (.vcpuCap v) excaps).run σ = + ({(.ok inv, σ)}, False)) : + (.ok (.invokeArchObject inv), σ) ∈ + ((fl4DecodeInvocation (Z := Unit) calling false lbl args cptr sl + (.archObjectCap (.vcpuCap v)) excaps buffer).run σ).1 := by + unfold fl4DecodeInvocation NonDetE.liftME + change (Except.ok (Fl4Invocation.invokeArchObject inv), σ) ∈ + (((fl4ArchDecodeInvocation (Z := Unit) calling (fl4LabelOf lbl) args cptr sl (.vcpuCap v) excaps).run >>= _) σ).1 + rw [nd_bind_oneSc h] + exact Set.mem_singleton _ + +/-! ### `JSyscallA → VcpuDecCtxA` and `JStage3A` helper lemmas -/ + +/-- `TreeA` at `OS` from `JStage3A` (`J5A`). -/ +theorem JStage3A.treeA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) : + TreeA st OS Z.G.chains Z.G.par s := + J.j.1.cdt.tree.grow J.j5.2.1 (fun x hx hn => J.j5.2.2 x hx hn) + +/-- Every `SlotWA Z.th y` is an `IsSlotA Z.th y`. -/ +theorem JStage3A.slotWA_isSlotA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {y : U64} (hy : SlotWA Z.th y) : IsSlotA Z.th y := by + rcases hy with h | ⟨p, r, hp, idx, hl, rfl⟩ + · exact h + · obtain ⟨e, hb, -⟩ := cn_slotV J.sr.place hp hl + exact ⟨p, Or.inl ⟨r, hp, blvA idx, hb, e⟩⟩ + +/-- Every `VcPage Z.V b` is off `ExtFootE Z.X Z.Y Z.T2`. -/ +theorem JStage3A.vcOffExtA {ar : machine.Arch A} {nA : U64} + {Pt Ub Pp7 Tb7 Hr Te Kt : Set U64} {Lv7 Re bits KLv : U64 → Nat} + (_J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s a σ m) + (W : J7UnpA st ar nA Z H OS s Pt Ub Pp7 Tb7 Hr Te Kt Lv7 Re bits KLv) : + ∀ b, VcPage Z.V b → ¬ ExtFootE Z.X Z.Y Z.T2 b := by + intro b hb hext + have hword : ∀ p, SchedPageA st Z OS s p → ∀ i : U64, i.val < 512 → ¬ VcPage Z.V (peAF p i) := by + intro p hp i hi hvc + have hvsr : VsR (L.ksb s) Pt (peAF p i) := + Or.inl ⟨p, W.sp p hp, core.num.U64.wrapping_mul i 8#u64, + by rw [wm8_valA hi]; omega, by rw [wm8_valA hi]; omega, rfl⟩ + exact W.vr.vc _ hvsr hvc + cases hext with + | s2lo r i hr hi => exact hword r (Or.inr (Or.inr (Or.inr (Or.inl hr)))) i hi hb + | s2hi r i hr hi => exact hword _ (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨r, hr, rfl⟩))))) i hi hb + | s2pt t i ht hi => exact hword t (Or.inr (Or.inr (Or.inl ht))) i hi hb + | wl0 y hy => exact hword y (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl hy)))))) _ (by simp) hb + | wlo y i hy hi => exact hword y (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl hy)))))) i (by omega) hb + +/-- Every `VcPage Z.V b` misses every `SlotWA Z.th y` (4 words). -/ +theorem JStage3A.vcOffSlotWA {ar : machine.Arch A} {nA : U64} + {Pt Ub Pp7 Tb7 Hr Te Kt : Set U64} {Lv7 Re bits KLv : U64 → Nat} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s a σ m) + (W : J7UnpA st ar nA Z H OS s Pt Ub Pp7 Tb7 Hr Te Kt Lv7 Re bits KLv) : + ∀ b, VcPage Z.V b → ∀ y, SlotWA Z.th y → + b ≠ y ∧ b ≠ wa y 8#u64 ∧ b ≠ wa y 16#u64 ∧ b ≠ wa y 24#u64 := + fun b hb y hy => (J.vcPlacedA W).disj_os b hb y (J.j5.2.1 y (J.slotWA_isSlotA hy)) + +/-- Every word in `FootA Z s` is 8-byte aligned. -/ +theorem JStage3A.footAlignedA {ar : machine.Arch A} {nA : U64} + {Pt Ub Pp7 Tb7 Hr Te Kt : Set U64} {Lv7 Re bits KLv : U64 → Nat} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s a σ m) + (W : J7UnpA st ar nA Z H OS s Pt Ub Pp7 Tb7 Hr Te Kt Lv7 Re bits KLv) : + ∀ b, FootA (L := L) Z s b → Aligned b := by + intro b hb + have hpl := J.vcPlacedA W + cases hb with + | slot y _ hy hw => + have hyal := hpl.al_os y (J.j5.2.1 y (J.slotWA_isSlotA hy)) + rcases hw with rfl | rfl | rfl | rfl + · exact hyal + · exact aligned_wa hyal 8#u64 (by decide) + · exact aligned_wa hyal 16#u64 (by decide) + · exact aligned_wa hyal 24#u64 (by decide) + | nslot _ hna => + have hscAl : ∀ c ∈ Z.C, Aligned c := fun c hc => by + have hcH : c ∈ H.scs := J.scs ▸ hc + obtain ⟨z, hz⟩ := hcH + have hmin := J.sys.ent.heap.scmin c z hz + have hal := J.sys.ent.heap.align c (.sc z) hz; change c.val % 2 ^ z = 0 at hal + have h8div : 8 ∣ 2 ^ z := ⟨2 ^ (z - 3), by rw [show (8 : Nat) = 2 ^ 3 from rfl, ← Nat.pow_add]; congr 1; omega⟩ + exact Nat.dvd_iff_mod_eq_zero.mp (Nat.dvd_trans h8div (Nat.dvd_iff_mod_eq_zero.mpr hal)) + cases hna with + | node c _ _ ho => + rcases ho with rfl | rfl | rfl | rfl + · exact hpl.node_al c _ (by unfold layout.node.CUR_THREAD; decide) + · exact hpl.node_al c _ (by unfold layout.node.IDLE_THREAD; decide) + · exact hpl.node_al c _ (by unfold layout.node.CUR_SC; decide) + · exact hpl.node_al c _ (by unfold layout.node.IDLE_SC; decide) + | cur _ ho => + rcases ho with rfl | rfl + · exact hpl.node_al (L.cpu s) _ (by unfold layout.node.SCHED_ACTION; decide) + · exact hpl.node_al (L.cpu s) _ (by unfold layout.node.CUR_TIME; decide) + | tcb t _ ht ho => + have hta := hpl.al_t t ht + rcases ho with rfl | rfl | rfl | rfl | rfl + · rw [stA_eq_wa]; exact aligned_wa hta 3328#u64 (by decide) + · exact aligned_wa hta _ (by decide) + · exact aligned_wa hta _ (by decide) + · exact aligned_wa hta 3664#u64 (by decide) + · exact aligned_wa hta _ (by decide) + | sc c hc => exact aligned_wa (hscAl c hc) 24#u64 (by decide) + | vcpu v _ hv hw => + rcases hw with rfl | rfl + · exact hpl.vc_al hv + · exact aligned_wa (hpl.vc_al hv) 24#u64 (by decide) + | irq i hi => + have hval := irqWordA_val J.j.1.ksFit hi + have hks8 : (L.ksb s).val % 8 = 0 := hpl.al_ksb + change (irqWordA (L.ksb s) i).val % 8 = 0 + rw [hval]; omega + | cur2 _ ho => + rcases ho with rfl | rfl + · exact hpl.node_al (L.cpu s) _ (by unfold layout.node.CONSUMED; decide) + · exact hpl.node_al (L.cpu s) _ (by unfold layout.node.REPROGRAM; decide) + | glob _ ho => + rcases ho with rfl | rfl + · exact aligned_wa hpl.al_ksb 8#u64 (by decide) + · exact aligned_wa hpl.al_ksb 16#u64 (by decide) + | tcb2 t _ ht ho => + have hta := hpl.al_t t ht + rcases ho with rfl | rfl + · exact aligned_wa hta _ (by decide) + · exact aligned_wa hta _ (by decide) + | sc2 c hc => exact aligned_wa (hscAl c hc) 72#u64 (by decide) + | vs _ hvs => + have hvsr : VsR (L.ksb s) Pt b := by + rcases hvs.vsR with ⟨p, hp, h⟩ | h + · refine Or.inl ⟨p, W.sp p ?_, h⟩ + rcases hp with hp | hp | hp + · exact Or.inl hp + · exact Or.inr (Or.inl hp) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl hp)))))) + · exact Or.inr h + exact W.vr.al b hvsr + | ext _ hext => + have hword : ∀ p, SchedPageA st Z OS s p → ∀ i : U64, i.val < 512 → Aligned (peAF p i) := fun p hp i hi => + W.vr.al _ (Or.inl ⟨p, W.sp p hp, core.num.U64.wrapping_mul i 8#u64, + by rw [wm8_valA hi]; omega, by rw [wm8_valA hi]; omega, rfl⟩) + cases hext with + | s2lo r i hr hi => exact hword r (Or.inr (Or.inr (Or.inr (Or.inl hr)))) i hi + | s2hi r i hr hi => exact hword _ (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨r, hr, rfl⟩))))) i hi + | s2pt t i ht hi => exact hword t (Or.inr (Or.inr (Or.inl ht))) i hi + | wl0 y hy => exact hword y (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl hy)))))) _ (by simp) + | wlo y i hy hi => exact hword y (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl hy)))))) i (by omega) + +/-- Assemble `VcpuDecCtxA` from `JSyscallA` + `J7UnpA` + `[P]/[S]` stall (no `Vcpu8bCA`). -/ +theorem JSyscallA.vcpuDecCtxA {ar : machine.Arch A} {nA : U64} + {Pt Ub Pp7 Tb7 Hr Te Kt : Set U64} {Lv7 Re bits KLv : U64 → Nat} {vc : U64} + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s a σ m) + (W : J7UnpA st ar nA Z H OS s Pt Ub Pp7 Tb7 Hr Te Kt Lv7 Re bits KLv) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) (hCsN : ∀ c ∈ Z.Cs, c.val < n) + (h1 : fl4A64Platform.numNodes = 1) (hvc : vc ∈ Z.V) : + VcpuDecCtxA (L := L) (ar := ar) AR Z H OS U Ac Pp Tb Ub Lv ia ie s a σ m vc where + E := J.toJEntA + hV := J.toJStage3A.vcpuRelA W hCsN + hTr := J.toJStage3A.treeA + hvc := hvc + hExt := J.toJStage3A.vcOffExtA W + hSw := J.toJStage3A.vcOffSlotWA W + hfa := J.toJStage3A.footAlignedA W + hsub := J.j5.2.1 + hIdleT := J.j.1.idleT + hCurNoRun := by + by_contra hne + exact J.toJStage3EntryA.curNotVmA (J.runVcpu _ J.sr.curT hne) + hStallT := fun hne => + (J.toJStage3A.toTA J.sact J.noSwitch).stall_singleA hCs h1 ((J.vcRun.vtcb vc hvc).resolve_left hne) + hStallC := fun hne => + (J.toJStage3A.toTA J.sact J.noSwitch).stall_singleA hCs h1 ((J.vcRun.vcpu vc hvc).resolve_left hne) + +/-! ### Record-level post lifters (`JStage3A` after non-link VCPU reg/IRQ writes) -/ + +private structure VcNlEqA (Z : XSetsA) (H : Heap) (OS : Set U64) (Bf : Set (U64 × Fl4SpecA64.Word)) + (s s' : S) (σ σ' : Fl4SpecA64.State Unit) (vc : U64) : Prop where + hk : L.ksb s' = L.ksb s + hc : L.cpu s' = L.cpu s + khOther : ∀ p, p ≠ specPtr vc → σ'.kheap p = σ.kheap p + khNe : ∀ p o, σ.kheap p = some o → (∀ xv, o ≠ .archObj (.vcpu xv)) → σ'.kheap p = some o + khBack : ∀ p o, σ'.kheap p = some o → (∀ xv, o ≠ .archObj (.vcpu xv)) → σ.kheap p = some o + specCap : ∀ sl, specCapAtA σ' sl = specCapAtA σ sl + os : OSKeep st OS s s' + capE : ∀ y ∈ OS, capA st s' y = capA st s y + tcb : ∀ t ∈ Z.T, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → L.mem s' (wa t o) = L.mem s (wa t o) + heapObj : ∀ p k, H p = some k → (∀ b, k ≠ .other b) → + ∀ o : U64, o.val < 2 ^ k.bits → o.val % 8 = 0 → 8 ∣ 2 ^ k.bits → + L.mem s' (wa p o) = L.mem s (wa p o) + buf : ∀ bp ∈ Bf, ∀ k : U64, k.val < 128 → L.mem s' (bufW bp.1 k) = L.mem s (bufW bp.1 k) + vcLink : ∀ u ∈ Z.V, L.mem s' u = L.mem s u ∧ L.mem s' (wa u 24#u64) = L.mem s (wa u 24#u64) + node : ∀ c ∈ Z.Cs, ∀ o : U64, o.val < 4288 → o.val % 8 = 0 → + L.mem s' (L.nodeAt s c o) = L.mem s (L.nodeAt s c o) + ks : ∀ o : U64, o.val < ksSizeA → o.val % 8 = 0 → L.mem s' (wa (L.ksb s) o) = L.mem s (wa (L.ksb s) o) + +private theorem vcNl_sqSrA {ar : machine.Arch A} {nA : U64} + {s' : S} {vc : U64} {v' : Fl4SpecA64.Vcpu} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s a σ m) + {Pt Ub Pp7 Tb7 Hr Te Kt : Set U64} {Lv7 Re bits KLv : U64 → Nat} + (W : J7UnpA st ar nA Z H OS s Pt Ub Pp7 Tb7 Hr Te Kt Lv7 Re bits KLv) + (hw : WritesIn st s s' (fun b => VcPage Z.V b ∧ ∀ u ∈ Z.V, b ≠ u ∧ b ≠ wa u 24#u64)) + (Eq : VcNlEqA (st := st) Z H OS Bf s s' σ (updKSc σ (specPtr vc) (.archObj (.vcpu v'))) vc) : + SchedQueuesInv st H.tcbs Z.Cs H.omega H s' Z.Q Z.R ∧ + SchedSRA st Z m κ s' (updKSc σ (specPtr vc) (.archObj (.vcpu v'))) ∧ + PoolOff st H Z.Cs OS s' := by + set σ' := updKSc σ (specPtr vc) (.archObj (.vcpu v')) + have hCurW : L.curW s' = L.curW s := by + unfold LawfulStore.curW; rw [Eq.hc, nodeAt_ksb Eq.hk] + exact Eq.node _ J.j.1.sch.cpuCs layout.node.CUR_THREAD (by unfold layout.node.CUR_THREAD; decide) + (by unfold layout.node.CUR_THREAD; decide) + have hsq' : SchedQueuesInv st H.tcbs Z.Cs H.omega H s' Z.Q Z.R := by + have hsq := J.sq + have hsb : ∀ y ∈ H.tcbs, L.mem s' (stA y) = L.mem s (stA y) ∧ L.mem s' (blockA y) = L.mem s (blockA y) := + fun y hy => ⟨by rw [stA_eq_wa]; exact Eq.tcb y (J.tcbs ▸ hy) 3328#u64 (by decide) (by decide), + by rw [blockA_eq]; exact Eq.tcb y (J.tcbs ▸ hy) _ (by decide) (by decide)⟩ + have hact : L.actionW s' = L.actionW s := by + unfold LawfulStore.actionW; rw [Eq.hc, nodeAt_ksb Eq.hk] + exact Eq.node _ J.j.1.sch.cpuCs _ (by unfold layout.node.SCHED_ACTION; decide) (by unfold layout.node.SCHED_ACTION; decide) + obtain ⟨E0, EQ, w1, w2, w5, w3, w4, w6⟩ := hsq.eps + have ew : ∀ e ∈ E0, L.mem s' (epHeadA e) = L.mem s (epHeadA e) ∧ L.mem s' (epTSA e) = L.mem s (epTSA e) := fun e he => by + rcases (w6.2.2.1 e).1 he with heH | heH + · exact ⟨by unfold epHeadA layout.ep.HEAD; exact Eq.heapObj e .ep heH (fun _ h => by cases h) 0#u64 (by decide) (by decide) (by decide), + by unfold epTSA layout.ep.TAIL_STATE; exact Eq.heapObj e .ep heH (fun _ h => by cases h) 8#u64 (by decide) (by decide) (by decide)⟩ + · exact ⟨by unfold epHeadA layout.ep.HEAD; exact Eq.heapObj e .ntfn heH (fun _ h => by cases h) 0#u64 (by decide) (by decide) (by decide), + by unfold epTSA layout.ep.TAIL_STATE; exact Eq.heapObj e .ntfn heH (fun _ h => by cases h) 8#u64 (by decide) (by decide) (by decide)⟩ + refine inv_of_agree hsq Eq.hk Eq.hc + (fun y hy => ⟨by rw [nextA_eq']; exact Eq.tcb y (J.tcbs ▸ hy) _ (by decide) (by decide), + by rw [prevA_eq']; exact Eq.tcb y (J.tcbs ▸ hy) _ (by decide) (by decide)⟩) + (fun c hc' o h1 h2 _ => Eq.node c hc' o h1 h2) + (fun y hy => by unfold inqA; exact Eq.tcb y (J.tcbs ▸ hy) _ (by decide) (by decide)) + (fun y hy => by unfold affA; exact Eq.tcb y (J.tcbs ▸ hy) _ (by decide) (by decide)) + (fun y hy => Or.inl (by unfold prioA; exact Eq.tcb y (J.tcbs ▸ hy) _ (by decide) (by decide))) + (by rw [hact]; exact hsq.action) hsb + (by rw [hact]; intro haT; rw [(hsb _ haT).1]; exact hsq.actOK haT) + ⟨E0, EQ, + fun e he => by + obtain ⟨hE, hEN, hq⟩ := w1 e he + have eq : L.epQ s' e = L.epQ s e := by simp only [LawfulStore.epQ, (ew e he).1, (ew e he).2] + refine ⟨hE, ⟨fun c hc' => by simp only [LawfulStore.nodeBase, Eq.hk]; exact hEN.1 c hc', by rw [Eq.hk]; exact hEN.2⟩, ?_⟩ + rw [eq] + exact ⟨hq.nodup, hq.mem, hq.head, hq.last, hq.seg.congr fun y hy => + ⟨by rw [prevA_eq']; exact Eq.tcb y (J.tcbs ▸ hq.mem y hy) _ (by decide) (by decide), + by rw [nextA_eq']; exact Eq.tcb y (J.tcbs ▸ hq.mem y hy) _ (by decide) (by decide)⟩⟩, + w2, w5, + fun e he y hy => by rw [(hsb y ((w1 e he).2.2.mem y hy)).1, (hsb y ((w1 e he).2.2.mem y hy)).2]; exact w3 e he y hy, + fun y hy hyX hb => by rw [(hsb y hy).1] at hb; rw [(hsb y hy).2]; exact w4 y hy hyX hb, + w6.agree (fun y hy => Or.inl hy) (fun y hy _ _ => hsb y hy) (fun _ _ _ hy => hy) + (fun e he y hy => (hsb y ((w1 e he).2.2.mem y hy)).1) + (fun e he _ => by simp only [LawfulStore.epState, (ew e he).2]) + (fun e he h0 => Or.inl ⟨h0, by simp only [LawfulStore.epState, (ew e he).2]⟩)⟩ + (by change L.curW s' ∈ H.tcbs; rw [hCurW]; exact hsq.curT) + (by + change L.idleW s' ∈ H.tcbs + unfold LawfulStore.idleW; rw [Eq.hc, nodeAt_ksb Eq.hk] + rw [Eq.node _ J.j.1.sch.cpuCs layout.node.IDLE_THREAD (by unfold layout.node.IDLE_THREAD; decide) + (by unfold layout.node.IDLE_THREAD; decide)] + exact hsq.idleT) + have hSr' : SchedSRA st Z m κ s' σ' := by + have hcore' : SchedCoreA st Z.T Z.Cs s' Z.Q Z.R := by + have h0 := J.sr.core + have haff : ∀ t ∈ Z.T, L.mem s' (affA t) = L.mem s (affA t) := fun t ht => by + unfold affA; exact Eq.tcb t ht _ (by decide) (by decide) + have hprio : ∀ t ∈ Z.T, L.mem s' (prioA t) = L.mem s (prioA t) := fun t ht => by + unfold prioA; exact Eq.tcb t ht _ (by decide) (by decide) + have hinq : ∀ t ∈ Z.T, L.mem s' (inqA t) = L.mem s (inqA t) := fun t ht => by + unfold inqA; exact Eq.tcb t ht _ (by decide) (by decide) + refine ⟨J.tcbs ▸ hsq'.tcbs, fun c hc' => J.tcbs ▸ hsq'.nodes c hc', + fun t ht => by rw [haff t ht]; exact h0.aff t ht, + fun t ht => by rw [hprio t ht]; exact h0.prio t ht, + fun c hc' => J.tcbs ▸ hsq'.ready c hc', + fun c hc' => J.tcbs ▸ hsq'.release c hc', + fun c hc' p hp t ht => by + have htT : t ∈ Z.T := ((h0.ready c hc').1.rel p hp).mem t ht + rw [haff t htT, hprio t htT]; exact h0.inQ c hc' p hp t ht, + fun c hc' t ht => by + have htT : t ∈ Z.T := (h0.release c hc').mem t ht + rw [haff t htT]; exact h0.inR c hc' t ht, + fun t ht => by rw [hinq t ht, haff t ht, hprio t ht]; exact h0.queued t ht, + fun t ht => by rw [hinq t ht, haff t ht]; exact h0.released t ht, + fun t ht => by rw [haff t ht, hprio t ht]; exact h0.excl t ht⟩ + have hinv' : ScInvA m σ' := + ⟨fun p y k h => J.sr.inv.refills p y k (Eq.khBack _ _ h (fun _ e => by cases e)), + fun p y k h => J.sr.inv.rr p y k (Eq.khBack _ _ h (fun _ e => by cases e)), + fun q x h => J.sr.inv.ntfnIdle q x (Eq.khBack _ _ h (fun _ e => by cases e)), + fun q x h => J.sr.inv.ntfnIdleOf q x (Eq.khBack _ _ h (fun _ e => by cases e)), + fun u hu => let ⟨x, p, y, k, h1, h2, h3, h4⟩ := J.sr.inv.relSc u hu + ⟨x, p, y, k, Eq.khNe _ _ h1 (fun _ e => by cases e), h2, Eq.khNe _ _ h3 (fun _ e => by cases e), h4⟩, + J.sr.inv.sorted.imp_of_mem fun {a b} ha hb h => by + have hrt : ∀ u ∈ σ.releaseQueue, specRtA σ' u = specRtA σ u := fun u hu => by + obtain ⟨x, p, y, k, h1, h2, h3, h4⟩ := J.sr.inv.relSc u hu + have hne := J.sinv.rpos p y k h3 h4 + rw [specRtA_of (Eq.khNe _ _ h1 (fun _ e => by cases e)) h2 (Eq.khNe _ _ h3 (fun _ e => by cases e)) hne, + specRtA_of h1 h2 h3 hne] + rwa [hrt a ha, hrt b hb], + fun u x p hx hsc => let ⟨y, k, h1, h2⟩ := J.sr.inv.back u x p (Eq.khBack _ _ hx (fun _ e => by cases e)) hsc + ⟨y, k, Eq.khNe _ _ h1 (fun _ e => by cases e), h2⟩, + fun u x p hx hsc hi => J.sr.inv.tcbScNotIdle u x p (Eq.khBack _ _ hx (fun _ e => by cases e)) hsc hi⟩ + have hrun' : RunQA σ' := + ⟨fun d p t ht => let ⟨x, hx, hs⟩ := J.sr.run.1 d p t ht; ⟨x, Eq.khNe _ _ hx (fun _ e => by cases e), hs⟩, + fun t ht => let ⟨x, hx, hs⟩ := J.sr.run.2 t ht; ⟨x, Eq.khNe _ _ hx (fun _ e => by cases e), hs⟩⟩ + have hrd' : RdA σ' := fun u x hx hin x' scp y k hx' hsc hkp hact hnrel => + J.sr.rd u x (Eq.khBack _ _ hx (fun _ e => by cases e)) hin x' scp y k + (Eq.khBack _ _ hx' (fun _ e => by cases e)) hsc (Eq.khBack _ _ hkp (fun _ e => by cases e)) hact hnrel + exact J.sr.rebuild (Q' := Z.Q) Eq.hk Eq.hc + (fun c hc' o ho h8 _ => Eq.node c hc' o (by omega) h8) + (fun K hK => Eq.ks K (by rcases hK with rfl | rfl <;> unfold ksSizeA <;> decide) (by rcases hK with rfl | rfl <;> decide)) + (fun t ht K hK => Eq.tcb t ht K (by rcases hK with rfl | rfl | rfl | rfl <;> decide) (by rcases hK with rfl | rfl | rfl | rfl <;> decide)) + (fun a hal ha => by + obtain ⟨c, hc', x, k, hkx, h1, h2⟩ := ha + have hcH : c ∈ H.scs := J.scs ▸ hc' + obtain ⟨z, hz⟩ := hcH + have hbits : H.scBits c = z := by unfold Heap.scBits; rw [hz] + have h2' : a.val < c.val + 2 ^ z := lt_of_lt_of_le h2 + (by rw [← hbits]; exact Nat.add_le_add_left (Nat.pow_le_pow_right (by decide) (J.scSize c hc' x k hkx).1) _) + have hfit := J.sys.ent.heap.fits c (.sc z) hz; simp only [HKind.bits] at hfit + exact hw.mem_eq hal fun ⟨⟨u, hu, ou, hou, _, heq⟩, _⟩ => by + have huH := W.col.vh u hu + have hne : u ≠ c := fun e => by rw [e, hz] at huH; cases huH + have hd := J.sys.ent.heap.disj u _ c (.sc z) huH hz hne + have huf := J.sys.ent.heap.fits u _ huH + have h12 : (2 ^ (HKind.other 12).bits : Nat) = 4096 := by decide + have hscz : (HKind.sc z).bits = z := rfl + rw [h12, hscz] at hd; rw [h12] at huf + have hv : (wa u ou).val = u.val + ou.val := by rw [wa_valP]; exact Nat.mod_eq_of_lt (by omega) + have he := congrArg UScalar.val heq; rw [hv] at he; rcases hd with hd | hd <;> omega) + (fun c hc' => by + obtain ⟨x, k, hx, -⟩ := J.sr.scr.sc c hc' + rw [Eq.khNe _ _ hx (fun _ e => by cases e), hx]) + (fun t ht x hx => ⟨x, Eq.khNe _ _ hx (fun _ e => by cases e), rfl, rfl, rfl⟩) + ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ + (by rw [Eq.node _ J.j.1.sch.cpuCs _ (by unfold layout.node.SCHED_ACTION; decide) (by unfold layout.node.SCHED_ACTION; decide)]; exact J.sr.sch.act) + J.sr.actOn hrd' hinv' hcore' J.sr.rest.rq J.sr.rest.rqDom rfl hrun' + have hPool' : PoolOff st H Z.Cs OS s' := PoolOff.ofTab Eq.hk (fun i => by + rw [Eq.hk] + have v := AtOff.v i + exact Eq.ks (AtOff i) (by unfold ksSizeA; omega) (by omega)) J.poolOff + exact ⟨hsq', hSr', hPool'⟩ + +private theorem vcNl_ipcA {ar : machine.Arch A} {nA : U64} + {s' : S} {vc : U64} {v' : Fl4SpecA64.Vcpu} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s a σ m) + (Eq : VcNlEqA (st := st) Z H OS Bf s s' σ (updKSc σ (specPtr vc) (.archObj (.vcpu v'))) vc) : + EpStateRel Z.T E Rp Bf (L.mem s') (updKSc σ (specPtr vc) (.archObj (.vcpu v'))) ∧ + NtfnStateRel N Z.T (L.mem s') (updKSc σ (specPtr vc) (.archObj (.vcpu v'))) ∧ + ReplyStackRelA (L.mem s') Rp Z.C (updKSc σ (specPtr vc) (.archObj (.vcpu v'))) (scNameA m) stack ∧ + BufCover st Z.T Bf s' (updKSc σ (specPtr vc) (.archObj (.vcpu v'))) ∧ + McpRelA Z.T m (L.mem s') (updKSc σ (specPtr vc) (.archObj (.vcpu v'))) ∧ + YieldRelA Z.T Z.C m (L.mem s') (updKSc σ (specPtr vc) (.archObj (.vcpu v'))) := by + have hRepW : ∀ r ∈ Rp, L.mem s' (rPrevA r) = L.mem s (rPrevA r) ∧ L.mem s' (rNextA r) = L.mem s (rNextA r) := fun r hr => by + have hrH : H r = some .reply := J.utIpc.reply r hr + exact ⟨by unfold rPrevA layout.reply.PREV; exact Eq.heapObj r .reply hrH (fun _ h => by cases h) 8#u64 (by decide) (by decide) (by decide), + by unfold rNextA layout.reply.NEXT; exact Eq.heapObj r .reply hrH (fun _ h => by cases h) 16#u64 (by decide) (by decide) (by decide)⟩ + refine ⟨?_, ?_, ?_, ?_, ?_, ?_⟩ + · exact + { ep := fun e he => by + obtain ⟨x, hx, hrel⟩ := J.ep.ep e he + have hepH : H e = some .ep := J.utIpc.ep e he + exact ⟨x, Eq.khNe _ _ hx (fun _ h => by cases h), + hrel.frame + (by unfold epHeadA layout.ep.HEAD; exact Eq.heapObj e .ep hepH (fun _ h => by cases h) 0#u64 (by decide) (by decide) (by decide)) + (by unfold epTSA layout.ep.TAIL_STATE; exact Eq.heapObj e .ep hepH (fun _ h => by cases h) 8#u64 (by decide) (by decide) (by decide)) + fun y hy _ => ⟨by rw [stA_eq_wa]; exact Eq.tcb y hy 3328#u64 (by decide) (by decide), + by unfold tBlockA; exact Eq.tcb y hy _ (by decide) (by decide), + ⟨by unfold prevA; exact Eq.tcb y hy _ (by decide) (by decide), + by unfold nextA; exact Eq.tcb y hy _ (by decide) (by decide)⟩⟩⟩ + reply := fun r hr => by + obtain ⟨x, hx, hrt⟩ := J.ep.reply r hr + have hrH : H r = some .reply := J.utIpc.reply r hr + exact ⟨x, Eq.khNe _ _ hx (fun _ h => by cases h), by + unfold Endp.rTcbA layout.reply.TCB at hrt ⊢ + rw [Eq.heapObj r .reply hrH (fun _ h => by cases h) 0#u64 (by decide) (by decide) (by decide)] + exact hrt⟩ + tcb := fun t ht => by + obtain ⟨x, hx, hrel⟩ := J.ep.tcb t ht + refine ⟨x, Eq.khNe _ _ hx (fun _ h => by cases h), + ⟨by rw [stA_eq_wa, Eq.tcb t ht 3328#u64 (by decide) (by decide), ← stA_eq_wa]; exact hrel.st, + fun e he => by unfold tBlockA; rw [Eq.tcb t ht _ (by decide) (by decide)]; exact hrel.blk e he, + fun e d hd => by + unfold tBadgeA tFlagsA + rw [Eq.tcb t ht _ (by decide) (by decide), Eq.tcb t ht _ (by decide) (by decide)] + exact hrel.send e d hd, + fun e r d hd => by unfold tReplyA; rw [Eq.tcb t ht _ (by decide) (by decide)]; exact hrel.recv e r d hd, + fun r hr => by unfold tReplyA; rw [Eq.tcb t ht _ (by decide) (by decide)]; exact hrel.reply r hr, + fun r => by + have hi := regIdxA_lt r + have hwa2 : wa (wa t layout.tcb.REGS) (core.num.U64.wrapping_mul (regIdxA r) 8#u64) = + wa t (wa layout.tcb.REGS (core.num.U64.wrapping_mul (regIdxA r) 8#u64)) := + u64_eq_of_bv_eq (BitVec.add_assoc _ _ _) + have hmul : (core.num.U64.wrapping_mul (regIdxA r) 8#u64).val = (regIdxA r).val * 8 := by + rw [core.num.U64.wrapping_mul_val_eq] + have h8v : (8#u64 : U64).val = 8 := rfl; rw [h8v] + exact Nat.mod_eq_of_lt (by simp only [UScalar.size, UScalarTy.numBits]; omega) + have hadd : (wa layout.tcb.REGS (core.num.U64.wrapping_mul (regIdxA r) 8#u64)).val = + 256 + (regIdxA r).val * 8 := by + rw [wa_valP, hmul] + have h256 : (layout.tcb.REGS : U64).val = 256 := by unfold layout.tcb.REGS; rfl + rw [h256]; exact Nat.mod_eq_of_lt (by omega) + unfold Endp.regA; rw [hwa2, Eq.tcb t ht _ (by omega) (by omega), ← hwa2] + exact hrel.regs r, + by unfold prioA; rw [Eq.tcb t ht _ (by decide) (by decide)]; exact hrel.prio, + by unfold tFaultA; rw [Eq.tcb t ht _ (by decide) (by decide)]; exact hrel.fault0, + by unfold tFaultA; rw [Eq.tcb t ht _ (by decide) (by decide)]; exact hrel.faultT, + by unfold tBoundA; rw [Eq.tcb t ht _ (by decide) (by decide)]; exact hrel.bound, + by + have hfr := hrel.faultR + unfold Endp.FaultRel at hfr ⊢ + unfold tFaultA Endp.tFw0A Endp.tFw1A Endp.tFw2A Endp.tLfA Endp.tLf0A Endp.tLf1A at hfr ⊢ + rw [Eq.tcb t ht _ (by decide) (by decide), Eq.tcb t ht _ (by decide) (by decide), + Eq.tcb t ht _ (by decide) (by decide), Eq.tcb t ht _ (by decide) (by decide), + Eq.tcb t ht _ (by decide) (by decide), Eq.tcb t ht _ (by decide) (by decide), + Eq.tcb t ht _ (by decide) (by decide)] + exact hfr⟩⟩ + buf := fun bp hbp k hk' => by rw [Eq.buf bp hbp k hk']; exact J.ep.buf bp hbp k hk' } + · exact + { ntfn := fun p hp => by + obtain ⟨x, hx, hrel⟩ := J.ntfn.ntfn p hp + have hnH : H p = some .ntfn := J.utIpc.ntfn p hp + exact ⟨x, Eq.khNe _ _ hx (fun _ h => by cases h), + hrel.frame + ⟨by rw [Notif.epHeadA_eq]; exact Eq.heapObj p .ntfn hnH (fun _ h => by cases h) 0#u64 (by decide) (by decide) (by decide), + by rw [epTSA_eq]; exact Eq.heapObj p .ntfn hnH (fun _ h => by cases h) 8#u64 (by decide) (by decide) (by decide), + by rw [nBadgeA_eq]; exact Eq.heapObj p .ntfn hnH (fun _ h => by cases h) 16#u64 (by decide) (by decide) (by decide), + by rw [nBoundA_eq]; exact Eq.heapObj p .ntfn hnH (fun _ h => by cases h) 24#u64 (by decide) (by decide) (by decide), + by rw [nScA_eq]; exact Eq.heapObj p .ntfn hnH (fun _ h => by cases h) 32#u64 (by decide) (by decide) (by decide)⟩ + fun y hy _ => ⟨by rw [stA_eq_wa]; exact Eq.tcb y hy 3328#u64 (by decide) (by decide), + by unfold tBlockA; exact Eq.tcb y hy _ (by decide) (by decide), + ⟨by rw [prevA_eq']; exact Eq.tcb y hy _ (by decide) (by decide), + by rw [nextA_eq']; exact Eq.tcb y hy _ (by decide) (by decide)⟩⟩⟩ + tcb := fun t ht => by + obtain ⟨x, hx, hrel⟩ := J.ntfn.tcb t ht + exact ⟨x, Eq.khNe _ _ hx (fun _ h => by cases h), + ⟨by rw [stA_eq_wa, Eq.tcb t ht 3328#u64 (by decide) (by decide), ← stA_eq_wa]; exact hrel.st, + fun n hn => by unfold tBlockA; rw [Eq.tcb t ht _ (by decide) (by decide)]; exact hrel.blk n hn, + by + have hwa2 : badgeRegA t = wa t (wa layout.tcb.REGS (core.num.U64.wrapping_mul layout.reg.BADGE 8#u64)) := + u64_eq_of_bv_eq (BitVec.add_assoc _ _ _) + have hbg : wa layout.tcb.REGS (core.num.U64.wrapping_mul layout.reg.BADGE 8#u64) = 256#u64 := by + unfold layout.tcb.REGS layout.reg.BADGE layout.reg.X0; rfl + rw [hwa2, hbg, Eq.tcb t ht 256#u64 (by decide) (by decide), ← hbg, ← hwa2]; exact hrel.badge, + by unfold prioA; rw [Eq.tcb t ht _ (by decide) (by decide)]; exact hrel.prio⟩⟩ } + · exact + ⟨fun c hc' => by + obtain ⟨⟨y, k, hy, eq⟩, hmem, hnd, hw'⟩ := J.reply.stackRel c hc' + refine ⟨⟨y, k, Eq.khNe _ _ hy (fun _ h => by cases h), eq⟩, hmem, hnd, fun i hi => ?_⟩ + have hri : (stack c)[i] ∈ Rp := hmem _ (List.getElem_mem hi) + rw [(hRepW _ hri).1, (hRepW _ hri).2]; exact hw' i hi, + J.reply.unique, + fun r hr x hx => J.reply.owner r hr x (Eq.khBack _ _ hx (fun _ h => by cases h)), + fun r hr hdet => by + obtain ⟨hb, hn, hp⟩ := J.reply.detached r hr hdet + rw [(hRepW r hr).1, (hRepW r hr).2] + exact ⟨hb, fun hne => let ⟨h1, h2, h3⟩ := hn hne; ⟨h1, h2, by rw [(hRepW _ h1).1]; exact h3⟩, + fun hne => let ⟨h1, h2, h3⟩ := hp hne; ⟨h1, h2, by rw [(hRepW _ h1).2]; exact h3⟩⟩⟩ + · intro t ht x' b sb w0 hx' hib hlu + have hx := Eq.khBack _ _ hx' (fun _ h => by cases h) + have hsl : sbWordA t ∈ OS := by + have h1 : sbWordA t = slotAddrA (specPtr t) (BitVec.ofNat 64 2) := by + rw [slotAddrA_sp]; unfold sbWordA layout.tcb.SLOT_BUFFER layout.cte.SIZE + apply UScalar.eq_of_val_eq; rw [wa_valP] + have h2m : (core.num.U64.wrapping_mul 2#u64 32#u64).val = 64 := by decide + have h2b : (2#64 * 32#64 : BitVec 64).toNat = 64 := by decide + rw [h2m]; change (t.val + 64) % _ = (t.bv + 2#64 * 32#64).toNat + rw [BitVec.toNat_add, h2b]; rfl + exact J.j5.2.1 _ (h1 ▸ ⟨t, Or.inr ⟨(J.sr.thT t).2 ht, 2, by decide, rfl⟩⟩) + have hlu0 : msg.lookup_ipc_buffer st s b t = ok w0 := by + unfold msg.lookup_ipc_buffer at hlu ⊢ + have hcap : obj.Slot.cap st (wa t (core.num.U64.wrapping_mul 2#u64 layout.cte.SIZE)) s' = + obj.Slot.cap st (wa t (core.num.U64.wrapping_mul 2#u64 layout.cte.SIZE)) s := by + have hsb : wa t (core.num.U64.wrapping_mul 2#u64 layout.cte.SIZE) = sbWordA t := by + unfold sbWordA layout.tcb.SLOT_BUFFER layout.cte.SIZE; rfl + rw [hsb]; unfold obj.Slot.cap + rw [L.read_pair_eq, L.read_pair_eq, (Eq.os _ hsl).1, (Eq.os _ hsl).2.1] + have hipc : L.mem s' (wa t 3512#u64) = L.mem s (wa t 3512#u64) := + Eq.tcb t ht 3512#u64 (by decide) (by decide) + have h184 : layout.tcb.F + 184#u64 = ok 3512#u64 := by unfold layout.tcb.F; rfl + simpa only [obj.Tcb.slot, layout.tcb.SLOT_BUFFER, obj.Tcb.ipc_buffer, layout.tcb.IPC_BUFFER, + lift, bind_ok, L.read_eq, h184, hipc, hcap] using hlu + exact J.bufCover t ht x' b sb w0 hx hib hlu0 + · intro t ht + obtain ⟨x, hx, hm, hfl⟩ := J.mcp t ht + exact ⟨x, Eq.khNe _ _ hx (fun _ h => by cases h), + by unfold mcpA; rw [Eq.tcb t ht _ (by decide) (by decide)]; exact hm, + fun hni => by + have hf := hfl hni; unfold FlagsOkA flagsA at hf ⊢ + rw [Eq.tcb t ht _ (by decide) (by decide)]; exact hf⟩ + · exact + { tcb := fun t ht => by + obtain ⟨x, hx, hy⟩ := J.yieldRel.tcb t ht + exact ⟨x, Eq.khNe _ _ hx (fun _ h => by cases h), by unfold yieldA; rw [Eq.tcb t ht _ (by decide) (by decide)]; exact hy⟩ + toSc := fun t ht hne => by + unfold yieldA at hne ⊢ + rw [Eq.tcb t ht _ (by decide) (by decide)] at hne ⊢ + obtain ⟨hc', y, n, hy, hfy⟩ := J.yieldRel.toSc t ht hne + exact ⟨hc', y, n, Eq.khNe _ _ hy (fun _ h => by cases h), hfy⟩ + frm := fun c hc' y n hy p hp => by + obtain ⟨t, ht, htp, hyt⟩ := J.yieldRel.frm c hc' y n (Eq.khBack _ _ hy (fun _ h => by cases h)) p hp + exact ⟨t, ht, htp, by unfold yieldA; rw [Eq.tcb t ht _ (by decide) (by decide)]; exact hyt⟩ } + +private theorem vcNl_sinvA {ar : machine.Arch A} {nA : U64} + {s' : S} {vc : U64} {v v' : Fl4SpecA64.Vcpu} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s a σ m) + (hkv : σ.kheap (specPtr vc) = some (.archObj (.vcpu v))) + (hvt : v'.vcpuTcb = v.vcpuTcb) + (Eq : VcNlEqA (st := st) Z H OS Bf s s' σ (updKSc σ (specPtr vc) (.archObj (.vcpu v'))) vc) : + SInvXA (updKSc σ (specPtr vc) (.archObj (.vcpu v'))) m n ∧ + DeadExtA st Z OS Rp stack s' (updKSc σ (specPtr vc) (.archObj (.vcpu v'))) ∧ + SpecFreeA st OS s' (updKSc σ (specPtr vc) (.archObj (.vcpu v'))) ∧ + SIdleA (updKSc σ (specPtr vc) (.archObj (.vcpu v'))) m n ∧ + SchedValidA (updKSc σ (specPtr vc) (.archObj (.vcpu v'))) := by + set σ' := updKSc σ (specPtr vc) (.archObj (.vcpu v')) + refine ⟨?_, ?_, ?_, ?_, ?_⟩ + · have CF := CurFactsA.back (σ := σ) (σ' := σ') ⟨J.sinv.zc, J.sinv.rlsRun, J.sinv.curScReady⟩ + (fun _ h => h) rfl rfl rfl + (fun t x' hx => ⟨x', Eq.khBack _ _ hx (fun _ h => by cases h), rfl⟩) + (fun t x hx _ => ⟨x, Eq.khNe _ _ hx (fun _ h => by cases h), id⟩) + (fun y k hy => ⟨y, Eq.khBack _ _ hy (fun _ h => by cases h), fun a => ⟨a, id⟩⟩) + exact + { mc := J.sinv.mc, view := J.sinv.view + zc := CF.1, rlsRun := CF.2.1, curScReady := CF.2.2 + faultSt := fun p y hy f => J.sinv.faultSt p y (Eq.khBack _ _ hy (fun _ h => by cases h)) f + curTcb := let ⟨x, hx⟩ := J.sinv.curTcb; ⟨x, Eq.khNe _ _ hx (fun _ h => by cases h)⟩ + idleTcb := let ⟨x, hx, hs⟩ := J.sinv.idleTcb; ⟨x, Eq.khNe _ _ hx (fun _ h => by cases h), hs⟩ + onlyIdle := fun t x hx hs => J.sinv.onlyIdle t x (Eq.khBack _ _ hx (fun _ h => by cases h)) hs + idleSc := let ⟨x, y, k, h1, h2, h3, h4⟩ := J.sinv.idleSc + ⟨x, y, k, Eq.khNe _ _ h1 (fun _ h => by cases h), h2, Eq.khNe _ _ h3 (fun _ h => by cases h), h4⟩ + curSc := let ⟨y, k, hy⟩ := J.sinv.curSc; ⟨y, k, Eq.khNe _ _ hy (fun _ h => by cases h)⟩ + ctQ := J.sinv.ctQ, idleQ := J.sinv.idleQ + rq := fun d p q hq => let ⟨x, hx, hs⟩ := J.sinv.rq d p q hq; ⟨x, Eq.khNe _ _ hx (fun _ h => by cases h), hs⟩ + rls := fun q hq => let ⟨x, hx⟩ := J.sinv.rls q hq; ⟨x, Eq.khNe _ _ hx (fun _ h => by cases h)⟩ + rpos := fun p y k hy ha => J.sinv.rpos p y k (Eq.khBack _ _ hy (fun _ h => by cases h)) ha + irqN := + { node := + ⟨⟨J.sinv.irqN.node.1.1, fun irq hi => + let ⟨cs, hcs, hwf⟩ := J.sinv.irqN.node.1.2 irq hi + ⟨cs, Eq.khNe _ _ hcs (fun _ h => by cases h), hwf⟩⟩, + fun irq hi nc ⟨cs, hcs, hwf⟩ => + J.sinv.irqN.node.2 irq hi nc ⟨cs, Eq.khBack _ _ hcs (fun _ h => by cases h), hwf⟩⟩ + slot := fun irq hi o b a ho hcap => by + cases o with + | archObj ao => + cases ao with + | vcpu _ => simp [capOf] at hcap + | _ => exact J.sinv.irqN.slot irq hi _ b a (Eq.khBack _ _ ho (fun _ h => by cases h)) hcap + | _ => exact J.sinv.irqN.slot irq hi _ b a (Eq.khBack _ _ ho (fun _ h => by cases h)) hcap + ref := fun p o b a ho hcap => by + cases o with + | archObj ao => + cases ao with + | vcpu _ => simp [capOf] at hcap + | _ => exact J.sinv.irqN.ref p _ b a (Eq.khBack _ _ ho (fun _ h => by cases h)) hcap + | _ => exact J.sinv.irqN.ref p _ b a (Eq.khBack _ _ ho (fun _ h => by cases h)) hcap } } + · exact + { vcpu := fun u hu hdead x hx => by + have hdead0 : DeadA st OS .vcpu s u := fun y hy hnm => hdead y hy (by rwa [Eq.capE y hy]) + by_cases e : u = vc + · subst e; unfold σ' updKSc at hx; simp [Function.update] at hx; cases hx + rw [hvt]; exact J.deadExt.vcpu u hu hdead0 v hkv + · exact J.deadExt.vcpu u hu hdead0 x (by rwa [Eq.khOther _ (fun h => e (specPtr_inj h))] at hx) + reply := fun r hr hdead => J.deadExt.reply r hr (fun y hy hnm => hdead y hy (by rwa [Eq.capE y hy])) + asid := fun i p h hdead => J.deadExt.asid i p h (fun y hy hnm => hdead y hy (by rwa [Eq.capE y hy])) + tvcpu := fun t ht hdead => by + rw [Eq.tcb t ht 3664#u64 (by decide) (by decide)] + exact J.deadExt.tvcpu t ht (fun y hy hnm => hdead y hy (by rwa [Eq.capE y hy])) + vcDeadRun := fun u hu hdead => by + rw [(Eq.vcLink u hu).2] + exact J.deadExt.vcDeadRun u hu (fun y hy hnm => hdead y hy (by rwa [Eq.capE y hy])) + replyTcb := fun r hr hdead x hx => + J.deadExt.replyTcb r hr (fun y hy hnm => hdead y hy (by rwa [Eq.capE y hy])) x + (Eq.khBack _ _ hx (fun _ h => by cases h)) } + · exact + { heap := fun p hf => by + obtain ⟨u, hu, q, b, fi, d, hc', hd, h1, h2⟩ := hf + have h0 := J.specFree.heap p ⟨u, hu, q, b, fi, d, (Eq.capE u hu).symm.trans hc', hd, h1, h2⟩ + by_cases e : specPtr p = specPtr vc + · rw [e, hkv] at h0; cases h0 + · rw [Eq.khOther _ e]; exact h0 + cdt := fun x y h p ⟨u, hu, q, b, fi, d, hc', hd, h1, h2⟩ => + J.specFree.cdt x y h p ⟨u, hu, q, b, fi, d, (Eq.capE u hu).symm.trans hc', hd, h1, h2⟩ + orig := fun x h p ⟨u, hu, q, b, fi, d, hc', hd, h1, h2⟩ => + J.specFree.orig x h p ⟨u, hu, q, b, fi, d, (Eq.capE u hu).symm.trans hc', hd, h1, h2⟩ } + · exact + { f3 := fun c hc' => let ⟨x, hx, hs⟩ := J.sidle.f3 c hc'; ⟨x, Eq.khNe _ _ hx (fun _ e => by cases e), hs⟩ + f4 := fun y k hy => J.sidle.f4 y k (Eq.khBack _ _ hy (fun _ e => by cases e)) + f5 := fun p y k hy => J.sidle.f5 p y k (Eq.khBack _ _ hy (fun _ e => by cases e)) + f6 := J.sidle.f6.of_eq Eq.specCap + f7 := fun y k hy => J.sidle.f7 y k (Eq.khBack _ _ hy (fun _ e => by cases e)) + idleSlot := fun c hc' => by + have h0 := J.sidle.idleSlot c hc' + obtain ⟨x, hx, -⟩ := J.sidle.f3 c hc' + unfold vtCapA at h0 ⊢; rw [Eq.khNe _ _ hx (fun _ e => by cases e)]; rwa [hx] at h0 + idleBound := fun c hc' => by + have h0 := J.sidle.idleBound c hc' + obtain ⟨x, hx, -⟩ := J.sidle.f3 c hc' + unfold vcpuOfA at h0 ⊢; rw [Eq.khNe _ _ hx (fun _ e => by cases e)]; rwa [hx] at h0 } + · exact J.schedValid.congr rfl rfl rfl rfl rfl rfl + (fun p y k hy => Eq.khNe _ _ hy (fun _ e => by cases e)) + (fun t x hx => ⟨x, Eq.khNe _ _ hx (fun _ e => by cases e), rfl⟩) + (fun t x hx => ⟨x, Eq.khBack _ _ hx (fun _ e => by cases e), rfl, rfl⟩) + +private theorem vcNl_eq_heap {ar : machine.Arch A} {nA : U64} + {s' : S} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s a σ m) + {Pt Ub Pp7 Tb7 Hr Te Kt : Set U64} {Lv7 Re bits KLv : U64 → Nat} + (W : J7UnpA st ar nA Z H OS s Pt Ub Pp7 Tb7 Hr Te Kt Lv7 Re bits KLv) + (hw : WritesIn st s s' (fun b => VcPage Z.V b ∧ ∀ u ∈ Z.V, b ≠ u ∧ b ≠ wa u 24#u64)) : + ∀ p k, H p = some k → (∀ b, k ≠ .other b) → + ∀ o : U64, o.val < 2 ^ k.bits → o.val % 8 = 0 → 8 ∣ 2 ^ k.bits → + L.mem s' (wa p o) = L.mem s (wa p o) := by + intro p k hp hno o ho ho8 h8d + have hal := J.sys.ent.heap.align p k hp + have hpa : Aligned p := Nat.dvd_iff_mod_eq_zero.mp (Nat.dvd_trans h8d (Nat.dvd_iff_mod_eq_zero.mpr hal)) + have hfit := J.sys.ent.heap.fits p k hp + refine hw.mem_eq (aligned_wa hpa o ho8) fun ⟨⟨u, hu, ou, hou, _, heq⟩, _⟩ => ?_ + have huH := W.col.vh u hu + have hne : u ≠ p := fun e => by rw [e, hp] at huH; cases huH; exact hno 12 rfl + have hd := J.sys.ent.heap.disj u _ p k huH hp hne + have huf := J.sys.ent.heap.fits u _ huH + have h12 : (2 ^ (HKind.other 12).bits : Nat) = 4096 := by decide + rw [h12] at hd huf + have h1 : (wa u ou).val = u.val + ou.val := by rw [wa_valP]; exact Nat.mod_eq_of_lt (by omega) + have h2 : (wa p o).val = p.val + o.val := by + have hpo : p.val + o.val < 2 ^ 64 := by omega + rw [wa_valP]; exact Nat.mod_eq_of_lt hpo + have hv := congrArg UScalar.val heq; rw [h1, h2] at hv; rcases hd with hd | hd <;> omega + +private theorem vcNl_eq_kh {vc : U64} {v v' : Fl4SpecA64.Vcpu} + (hkv : σ.kheap (specPtr vc) = some (.archObj (.vcpu v))) : + let σ' := updKSc σ (specPtr vc) (.archObj (.vcpu v')) + (∀ p, p ≠ specPtr vc → σ'.kheap p = σ.kheap p) ∧ + (∀ p o, σ.kheap p = some o → (∀ xv, o ≠ .archObj (.vcpu xv)) → σ'.kheap p = some o) ∧ + (∀ p o, σ'.kheap p = some o → (∀ xv, o ≠ .archObj (.vcpu xv)) → σ.kheap p = some o) ∧ + (∀ sl, specCapAtA σ' sl = specCapAtA σ sl) := by + have hKhOther : ∀ p, p ≠ specPtr vc → (updKSc σ (specPtr vc) (.archObj (.vcpu v'))).kheap p = σ.kheap p := + fun p hp => by rw [updKSc_kheap]; exact Function.update_of_ne hp _ _ + refine ⟨hKhOther, fun p o hp hnv => ?_, fun p o hp hnv => ?_, fun sl => ?_⟩ + · by_cases e : p = specPtr vc + · subst e; rw [hkv] at hp; cases hp; exact absurd rfl (hnv v) + · rw [hKhOther p e]; exact hp + · by_cases e : p = specPtr vc + · subst e; rw [updKSc_kheap, Function.update_self] at hp; cases hp; exact absurd rfl (hnv v') + · rwa [hKhOther p e] at hp + · unfold specCapAtA + by_cases e : sl.1 = specPtr vc + · rw [e, updKSc_kheap, Function.update_self, hkv] + · rw [hKhOther sl.1 e] + +private theorem vcNl_eq_os {s' : S} {Ub : Set U64} + (hpl : VcPlacedA Z OS Ub (L.ksb s)) + (hw : WritesIn st s s' (fun b => VcPage Z.V b ∧ ∀ u ∈ Z.V, b ≠ u ∧ b ≠ wa u 24#u64)) : + OSKeep st OS s s' ∧ (∀ y ∈ OS, capA st s' y = capA st s y) := by + have hOS : OSKeep st OS s s' := fun y hy => by + have hyal := hpl.al_os y hy + refine ⟨hw.mem_eq hyal fun h => (hpl.disj_os _ h.1 y hy).1 rfl, + hw.mem_eq (aligned_wa hyal 8#u64 (by decide)) fun h => (hpl.disj_os _ h.1 y hy).2.1 rfl, + hw.mem_eq (aligned_wa hyal 16#u64 (by decide)) fun h => (hpl.disj_os _ h.1 y hy).2.2.1 rfl, + hw.mem_eq (aligned_wa hyal 24#u64 (by decide)) fun h => (hpl.disj_os _ h.1 y hy).2.2.2 rfl⟩ + exact ⟨hOS, fun y hy => by unfold capA; rw [(hOS y hy).1, (hOS y hy).2.1]⟩ + +private theorem vcNl_eq_ks {s' : S} {Ub : Set U64} + (hpl : VcPlacedA Z OS Ub (L.ksb s)) + (hksf : (L.ksb s).val + ksSizeA ≤ 2 ^ 64) + (hCs128 : ∀ c ∈ Z.Cs, c.val < 128) + (hw : WritesIn st s s' (fun b => VcPage Z.V b ∧ ∀ u ∈ Z.V, b ≠ u ∧ b ≠ wa u 24#u64)) : + (∀ c ∈ Z.Cs, ∀ o : U64, o.val < 4288 → o.val % 8 = 0 → + L.mem s' (L.nodeAt s c o) = L.mem s (L.nodeAt s c o)) ∧ + (∀ o : U64, o.val < ksSizeA → o.val % 8 = 0 → L.mem s' (wa (L.ksb s) o) = L.mem s (wa (L.ksb s) o)) := + ⟨fun c hc' o ho ho8 => hw.mem_eq (hpl.node_al c o ho8) fun h => + hpl.disj_ks _ h.1 (by have hcv := hCs128 c hc'; have hv := nodeAt_valSc hksf hcv ho; unfold ksSizeA; omega), + fun o ho ho8 => hw.mem_eq (aligned_wa hpl.al_ksb o ho8) fun h => + hpl.disj_ks _ h.1 (by + have hv : (wa (L.ksb s) o).val = (L.ksb s).val + o.val := by rw [wa_valP]; exact Nat.mod_eq_of_lt (by omega) + rw [hv]; omega)⟩ + +private theorem vcNl_eqA {ar : machine.Arch A} {nA : U64} + {s' : S} {vc : U64} {v v' : Fl4SpecA64.Vcpu} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s a σ m) + {Pt Ub Pp7 Tb7 Hr Te Kt : Set U64} {Lv7 Re bits KLv : U64 → Nat} + (W : J7UnpA st ar nA Z H OS s Pt Ub Pp7 Tb7 Hr Te Kt Lv7 Re bits KLv) + (hkv : σ.kheap (specPtr vc) = some (.archObj (.vcpu v))) + (hw : WritesIn st s s' (fun b => VcPage Z.V b ∧ ∀ u ∈ Z.V, b ≠ u ∧ b ≠ wa u 24#u64)) : + VcNlEqA (st := st) Z H OS Bf s s' σ (updKSc σ (specPtr vc) (.archObj (.vcpu v'))) vc := by + have hpl := J.vcPlacedA W + have hCs128 : ∀ c ∈ Z.Cs, c.val < 128 := by + obtain ⟨n0, -, hn128, hcn⟩ := J.j.1.csN + intro c hc'; have := (hcn c).1 hc'; omega + obtain ⟨hKhOther, hKhNe, hKhBack, hSpecCap⟩ := vcNl_eq_kh (v' := v') hkv + obtain ⟨hOS, hCapE⟩ := vcNl_eq_os hpl hw + obtain ⟨hNode, hKs⟩ := vcNl_eq_ks hpl J.j.1.ksFit hCs128 hw + exact + { hk := hw.1, hc := hw.2.1, khOther := hKhOther, khNe := hKhNe, khBack := hKhBack, specCap := hSpecCap + os := hOS, capE := hCapE + tcb := fun t ht o ho ho8 => hw.mem_eq (aligned_wa (hpl.al_t t ht) o ho8) fun ⟨⟨u, hu, ou, hou, _, heq⟩, _⟩ => + hpl.disj_tcb u hu t ht ou o hou ho heq.symm + heapObj := vcNl_eq_heap J W hw + buf := fun bp hbp k hk' => + hw.mem_eq (J.iplaced.e.balign _ ⟨bp, hbp, k, hk', rfl⟩) fun ⟨⟨u, hu, ou, hou, hou8, heq⟩, _⟩ => + J.bufOff.vcw _ ⟨bp, hbp, k, hk', rfl⟩ u hu ou hou hou8 heq + vcLink := fun u hu => ⟨hw.mem_eq (hpl.vc_al hu) fun h => (h.2 u hu).1 rfl, + hw.mem_eq (aligned_wa (hpl.vc_al hu) 24#u64 (by decide)) fun h => (h.2 u hu).2 rfl⟩ + node := hNode, ks := hKs } + +private theorem vcNl_unmPtA {ar : machine.Arch A} {nA : U64} + {s' : S} {vc : U64} {v' : Fl4SpecA64.Vcpu} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s a σ m) + (hw : WritesIn st s s' (fun b => VcPage Z.V b ∧ ∀ u ∈ Z.V, b ≠ u ∧ b ≠ wa u 24#u64)) + (Eq : VcNlEqA (st := st) Z H OS Bf s s' σ (updKSc σ (specPtr vc) (.archObj (.vcpu v'))) vc) : + UnmPtSpecA st Z m OS s' (updKSc σ (specPtr vc) (.archObj (.vcpu v'))) := by + obtain ⟨Pt0, _, vr0, -, -, -, -, -, -, link, -, -⟩ := J.j7 + have hPageWord : ∀ p, SchedPageA st Z OS s p → ∀ i : U64, i.val < 512 → L.mem s' (peAF p i) = L.mem s (peAF p i) := + fun p hp i hi => + have hvsr : VsR (L.ksb s) Pt0 (peAF p i) := + Or.inl ⟨p, link p hp, core.num.U64.wrapping_mul i 8#u64, by rw [wm8_valA hi]; omega, by rw [wm8_valA hi]; omega, rfl⟩ + hw.mem_eq (vr0.al _ hvsr) fun h => vr0.vc _ hvsr h.1 + exact + { pt := fun u hu p asid va h => by + have h0 := (Eq.capE u hu).symm.trans h + have U1 := J.unmPt.pt u hu p asid va h0 + obtain ⟨tb, h1, h2, h3⟩ := U1.spec + exact ⟨fun i hi => by rw [hPageWord p (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ⟨u, hu, Or.inl ⟨_, _, _, h0⟩⟩))))))) i hi]; exact U1.inv i hi, + ⟨tb, Eq.khNe _ _ h1 (fun _ e => by cases e), h2, h3⟩, + ⟨fun i hi h => U1.off.mem i hi (footA_schedA Eq.hk Eq.hc h), U1.off.obj⟩⟩ + vs := fun u hu p asid h => by + have h0 := (Eq.capE u hu).symm.trans h + have U1 := J.unmPt.vs u hu p asid h0 + obtain ⟨tb, h1, h2, h3⟩ := U1.spec + exact ⟨fun i hi => by rw [hPageWord p (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ⟨u, hu, Or.inr (Or.inl ⟨_, _, h0⟩)⟩))))))) i hi]; exact U1.inv i hi, + ⟨tb, Eq.khNe _ _ h1 (fun _ e => by cases e), h2, h3⟩, + ⟨fun i hi h => U1.off.mem i hi (footA_schedA Eq.hk Eq.hc h), U1.off.obj⟩⟩ + s2 := fun u hu p asid va h => by + have h0 := (Eq.capE u hu).symm.trans h + have U1 := J.unmPt.s2 u hu p asid va h0 + obtain ⟨es, h1, h2⟩ := U1.spec + refine ⟨fun i hi => ?_, ⟨es, Eq.khNe _ _ h1 (fun _ e => by cases e), h2⟩⟩ + by_cases hlo : i.val < 512 + · rw [hPageWord p (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ⟨u, hu, Or.inr (Or.inr (Or.inl ⟨asid, va, h0⟩))⟩))))))) i hlo] + exact U1.inv i hi + · obtain ⟨j, hj, e⟩ := peAF_hiA p i (by omega) hi + rw [e, hPageWord _ (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ⟨u, hu, Or.inr (Or.inr (Or.inr (Or.inl ⟨p, asid, va, h0, rfl⟩)))⟩))))))) j hj, ← e] + exact U1.inv i hi + hv := fun u hu p asid h => by + have h0 := (Eq.capE u hu).symm.trans h + have U1 := J.unmPt.hv u hu p asid h0 + obtain ⟨l1, h1, h2⟩ := U1.spec + exact ⟨fun i hi => by + rw [hPageWord _ (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ⟨u, hu, Or.inr (Or.inr (Or.inr (Or.inr ⟨p, asid, h0, rfl⟩)))⟩))))))) i hi] + exact U1.inv i hi, + ⟨l1, Eq.khNe _ _ h1 (fun _ e => by cases e), h2⟩⟩ } + +/-- Lift `JStage3A` across a pure non-link VCPU word write (`AckVPPI`, `WriteReg`, `InjectIRQ`): +memory only changes at `VcPage Z.V` words off `v+0` and `v+24`; spec only updates `kheap (specPtr vc)` +with another `.vcpu v'` keeping `vcpuTcb` and `vcpuExt`; `SysL`/`SymL`/`BXI`/`UserOK`/`J7A` come +from `JSyscallA.decodeFinA`. -/ +theorem JStage3A.of_vcpuNonLinkWrA {ar : machine.Arch A} {nA : U64} + {s' : S} {a' : A} {Ac' : Set U64} {vc : U64} {v v' : Fl4SpecA64.Vcpu} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s a σ m) + (hvc : vc ∈ Z.V) + (hkv : σ.kheap (specPtr vc) = some (.archObj (.vcpu v))) + (hvt : v'.vcpuTcb = v.vcpuTcb) (hrc : v'.vcpuExt.runCaller = v.vcpuExt.runCaller) + (hw : WritesIn st s s' (fun b => VcPage Z.V b ∧ ∀ u ∈ Z.V, b ≠ u ∧ b ≠ wa u 24#u64)) + (har : AR a' Z.hw) + (hsnap : VcpuStateSnapA Z.V (L.mem s') (updKSc σ (specPtr vc) (.archObj (.vcpu v')))) + (sysl : SysL st H Z.Cs OS ia ie s') (symL : SymL st H Z.Cs s') + (bxi' : BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac' s') + (us' : UserOK st H.tcbs Z.Cs H.omega H U s') + (j7' : J7A st (J7R2A st ar nA) Z H OS s' (updKSc σ (specPtr vc) (.archObj (.vcpu v'))) m) : + JStage3A st AR Z H OS U Ac' Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s' a' (updKSc σ (specPtr vc) (.archObj (.vcpu v'))) m := by + set σ' := updKSc σ (specPtr vc) (.archObj (.vcpu v')) + obtain ⟨Pt, Ub, Pp7, Tb7, Hr, Te, Kt, Lv7, Re, bits, KLv, W⟩ := J7A.unp J.j7 + have hpl := J.vcPlacedA W + have hCs128 : ∀ c ∈ Z.Cs, c.val < 128 := by + obtain ⟨n0, -, hn128, hcn⟩ := J.j.1.csN + intro c hc'; have := (hcn c).1 hc'; omega + have hcpu128 : (L.cpu s).val < 128 := hCs128 _ J.j.1.sch.cpuCs + have Eq := vcNl_eqA (v' := v') J W hkv hw + obtain ⟨Pt1, _, vr1, -, -, -, -, -, -, -, hut0⟩ := J7FrA.fr (st := st) _ _ _ _ _ _ J.j7 + have hUtVs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → L.mem s' b = L.mem s b := fun b hb => by + have hvsr : VsR (L.ksb s) Pt1 b := by + rcases hb with ⟨p, hp, h⟩ | h + · exact Or.inl ⟨p, hut0 Pp Tb Lv J.sys.ent.vs hp, h⟩ + · exact Or.inr h + exact hw.mem_eq (vr1.al b hvsr) fun h => vr1.vc b hvsr h.1 + have hJ1 : JAtZAX (L := L) AR Z s a' σ m := ⟨{ J.j.1 with ar := har }, J.j.2⟩ + obtain ⟨hJ', -, -⟩ := vcpuNonLinkWr_frameA hJ1 J.treeA hpl hCs128 hcpu128 + (J.vcOffExtA W) (J.vcOffSlotWA W) (J.footAlignedA W) hw (fun b hb => hb) + hvc hkv hvt hrc J.j5.2.1 + have ⟨vsTy', utVsEq⟩ := utVsPt_keepA J.sys.ent.vs Eq.hk hUtVs + have ent' : EntryInvA3 st H Z.Cs OS U Ac' Pp Tb Lv s' := + ⟨J.sys.ent.heap, bxi', J.sys.ent.os, us', vsTy', by rw [Eq.hk]; exact J.sys.ent.ksw⟩ + obtain ⟨hsq', hSr', hPool'⟩ := vcNl_sqSrA J W hw Eq + obtain ⟨hEp', hNtfn', hRep', hBufCov', hMcp', hYield'⟩ := vcNl_ipcA J Eq + obtain ⟨hSinv', hDead', hFree', hSidle', hValid'⟩ := vcNl_sinvA J hkv hvt Eq + have hUnmPt' := vcNl_unmPtA J hw Eq + have hIdleAll' : IdleAllA st ia ie Z.Cs m s' := + ⟨fun c hc' => by + rw [nodeAt_ksb Eq.hk, Eq.node c hc' layout.node.IDLE_THREAD (by unfold layout.node.IDLE_THREAD; decide) + (by unfold layout.node.IDLE_THREAD; decide)] + exact J.idleAll.tcb c hc', + fun c hc' => by + rw [nodeAt_ksb Eq.hk, Eq.node c hc' layout.node.IDLE_SC (by unfold layout.node.IDLE_SC; decide) + (by unfold layout.node.IDLE_SC; decide)] + exact J.idleAll.sc c hc', + J.idleAll.spec⟩ + have hTr := J.treeA + have hJ5' : J5A st Z H OS s' σ' m := + ⟨⟨MdbInvA.keepO hTr.lk hTr.tree J.j5.1.1 Eq.os, IrqRunA.keepO hTr.lk J.j5.1.2.1 Eq.os, + HandlersActiveA.keepO hTr.lk J.j5.1.2.2 Eq.os Eq.hk (fun i hi => by + have hval := irqWordA_val J.j.1.ksFit hi + have hks8 : (L.ksb s).val % 8 = 0 := hpl.al_ksb + change L.mem s' (irqWordA (L.ksb s) i) = L.mem s (irqWordA (L.ksb s) i) + exact hw.mem_eq (by change (irqWordA (L.ksb s) i).val % 8 = 0; rw [hval]; omega) fun h => + hpl.disj_ks _ h.1 (by rw [hval]; unfold ksSizeA; omega))⟩, + J.j5.2.1, fun y hy hn => by rw [Eq.capE y hy]; exact J.j5.2.2 y hy hn⟩ + exact + { sys := ⟨ent', sysl⟩, j := hJ', tcbs := J.tcbs, scs := J.scs + sq := hsq', ap := Eq.hk ▸ J.ap, pp := J.pp, s1 := J.s1 + ko := by rw [Eq.hk]; exact J.ko + ptH := J.ptH, tabInj := J.tabInj, poolOff := hPool' + objAlloc := ObjAllocA.keep (Z := Z) J.objAlloc Eq.os + idleAnchor := IdleAnchorOffA.keep J.idleAnchor Eq.os + vsVc := by rw [Eq.hk]; exact J.vsVc + sr := hSr' + scCov := fun p x n h => J.scCov p x n (Eq.khBack _ _ h (fun _ e => by cases e)) + scSize := fun c hc' x k hx => J.scSize c hc' x k (Eq.khBack _ _ hx (fun _ h => by cases h)) + ep := hEp', ntfn := hNtfn', reply := hRep', bufCover := hBufCov' + bufNative := fun t ht => by + have h0 := J.bufNative t ht + rwa [capAtA_eq, Eq.capE _ (J.j5.2.1 _ ⟨t, Or.inr ⟨(J.sr.thT t).2 ht, 2, by decide, rfl⟩⟩), ← capAtA_eq] + iplaced := J.iplaced, ipcPlace := J.ipcPlace + idleNoFault := fun c hc' => let ⟨x, hx, hf⟩ := J.idleNoFault c hc'; ⟨x, Eq.khNe _ _ hx (fun _ h => by cases h), hf⟩ + mcp := hMcp', yieldRel := hYield', sinv := hSinv' + setsInH := J.setsInH, utIpc := J.utIpc, ipcTh := J.ipcTh + deadExt := hDead', specFree := hFree' + utBlk := fun u hu q b fi d h => J.utBlk u hu q b fi d ((Eq.capE u hu).symm.trans h) + unmPt := hUnmPt' + tabS1 := fun q hq => ⟨fun i hi h => (J.tabS1 q hq).mem i hi (footA_schedA Eq.hk Eq.hc h), (J.tabS1 q hq).obj⟩ + tabExt := ⟨fun q hq i hi h => J.tabExt.mem q hq i hi (footA_schedA (Z := extZA Z) Eq.hk Eq.hc h), J.tabExt.obj⟩ + specZero := fun p ⟨u, hu, q, b, fi, d, hc', hd, h1, h2⟩ => + J.specZero p ⟨u, hu, q, b, fi, d, (Eq.capE u hu).symm.trans hc', hd, h1, h2⟩ + origLive := J.origLive + nullOrig := fun sl h => J.nullOrig sl (by rwa [Eq.specCap] at h) + thPos := J.thPos + frBase := J.frBase.keepA Eq.capE + devFr := J.devFr.keepA Eq.capE (fun p dev pg h => Eq.khNe _ _ h (fun _ e => by cases e)) + finCov := J.finCov.keepA + (fun q r cs h => Eq.khBack _ _ h (fun _ e => by cases e)) + (fun q x h => ⟨x, Eq.khBack _ _ h (fun _ e => by cases e)⟩) + idleAll := hIdleAll', ipcHeapCov := J.ipcHeapCov + zombieNoVcpu := fun y hy p k hc' => by + have hc0 := (Eq.capE y hy).symm.trans hc' + rw [Eq.tcb p (J.tcbs ▸ J.zombieTcb y hy p k hc0) 3664#u64 (by decide) (by decide)] + exact J.zombieNoVcpu y hy p k hc0 + zombieTcb := fun y hy p k hc' => J.zombieTcb y hy p k ((Eq.capE y hy).symm.trans hc') + ipiZero := J.ipiZero.keepW Eq.hk Eq.hc fun o ho => by + have h4 : o.val < 4288 ∧ o.val % 8 = 0 := by rcases ho with h | h <;> rw [h] <;> decide + exact Eq.node _ J.sr.sch.cpuCs o h4.1 h4.2 + bufOff := J.bufOff.keepA Eq.hk rfl rfl rfl rfl rfl rfl rfl Eq.capE + vsOk := by unfold UtVsOkA; rw [Eq.hk, utVsEq]; exact J.vsOk + bufAl := fun t ht x hx => J.bufAl t ht x (Eq.khBack _ _ hx (fun _ e => by cases e)) + zombieBnd := J.zombieBnd.keepA Eq.capE + devUtOff := J.devUtOff.keepA (Z := Z) Eq.capE Eq.hk (fun _ _ h => h) rfl + sidle := hSidle', capTy := J.capTy.keepA Eq.capE, scCpu := J.scCpu, scNZ := J.scNZ + vcRun := + { tcb := fun t ht => by rw [Eq.tcb t ht 3664#u64 (by decide) (by decide)]; exact J.vcRun.tcb t ht + vcpu := fun u hu => by rw [(Eq.vcLink u hu).2]; exact J.vcRun.vcpu u hu + vtcb := fun u hu => by rw [(Eq.vcLink u hu).1]; exact J.vcRun.vtcb u hu + run := fun t ht => by unfold runVcpuA; rw [Eq.tcb t ht 3672#u64 (by decide) (by decide)]; exact J.vcRun.run t ht + disj := fun t ht hne => by + rw [Eq.tcb t ht 3664#u64 (by decide) (by decide)] at hne + unfold runVcpuA; rw [Eq.tcb t ht 3672#u64 (by decide) (by decide)]; exact J.vcRun.disj t ht hne } + vcSym1 := fun t ht hne => by + rw [Eq.tcb t ht 3664#u64 (by decide) (by decide)] at hne ⊢ + rw [(Eq.vcLink _ ((J.vcRun.tcb t ht).resolve_left hne)).1]; exact J.vcSym1 t ht hne + runVcpu := fun t ht hne => by + unfold runVcpuA at hne; rw [Eq.tcb t ht 3672#u64 (by decide) (by decide)] at hne + rw [stA_eq_wa, Eq.tcb t ht 3328#u64 (by decide) (by decide), ← stA_eq_wa]; exact J.runVcpu t ht hne + vcSym2 := fun u hu hne => by + rw [(Eq.vcLink u hu).2] at hne ⊢ + unfold runVcpuA; rw [Eq.tcb _ ((J.vcRun.vcpu u hu).resolve_left hne) 3672#u64 (by decide) (by decide)] + exact J.vcSym2 u hu hne + blkQ := J.blkQ.keepA (fun t ht => Eq.tcb t ht 3336#u64 (by decide) (by decide)) + (fun t ht x hx => Or.inr ⟨x, Eq.khBack _ _ hx (fun _ e => by cases e), rfl⟩) + (fun p y hy => Eq.khNe _ _ hy (fun _ e => by cases e)) + (fun p y hy => Eq.khNe _ _ hy (fun _ e => by cases e)) + heapWin := J.heapWin, coresAll := J.coresAll + ptCapIn := fun y hy => by have := J.ptCapIn y hy; rwa [capAtA_eq, Eq.capE y (J.j5.2.1 y hy), ← capAtA_eq] + symL := symL + fhOK := fun t ht => by rw [Eq.capE _ (J.j5.2.1 _ (isSlot_fhA ((J.sr.thT t).2 ht)))]; exact J.fhOK t ht + zwf := fun y hy p zt n hc' => J.zwf y hy p zt n ((Eq.capE y (J.j5.2.1 y hy)).symm.trans hc') + schedValid := hValid', utWin := J.utWin.keepA Eq.capE + devWin := fun u hu => + ⟨fun p sb fi h => (J.devWin u hu).1 p sb fi ((Eq.capE u hu).symm.trans h), + fun p sz r m e a v h => (J.devWin u hu).2 p sz r m e a v ((Eq.capE u hu).symm.trans h)⟩ + vcBack := fun u hu hne => by + rw [(Eq.vcLink u hu).1] at hne ⊢ + rw [Eq.tcb _ ((J.vcRun.vtcb u hu).resolve_left hne) 3664#u64 (by decide) (by decide)]; exact J.vcBack u hu hne + runBack := fun t ht hne => by + unfold runVcpuA at hne ⊢; rw [Eq.tcb t ht 3672#u64 (by decide) (by decide)] at hne ⊢ + have hvV := (J.vcRun.run t ht).resolve_left hne; unfold runVcpuA at hvV + rw [(Eq.vcLink _ hvV).2]; exact J.runBack t ht hne + vcSnap := hsnap, vcOS := J.vcOS + userFr := J.userFr.keepA Eq.capE (fun p dev pg h => Eq.khNe _ _ h (fun _ e => by cases e)) + frTh := J.frTh.keepA Eq.capE fun _ _ e => e + bfSpec := J.bfSpec, curVcpuNone := J.curVcpuNone + ipcSorted := IpcSortedA.of_sub J.ipcSorted + (fun t y hy => ⟨y, Eq.khBack _ _ hy (fun _ e => by cases e), rfl⟩) + (fun p x hx => ⟨x, Eq.khBack _ _ hx (fun _ e => by cases e), List.Sublist.refl _⟩) + (fun p x hx => ⟨x, Eq.khBack _ _ hx (fun _ e => by cases e), List.Sublist.refl _⟩) + activeReplyScs := ActRepA.of_keep J.activeReplyScs + (fun p y k hy => ⟨y, k, Eq.khBack _ _ hy (fun _ e => by cases e), fun h => ⟨h, id⟩⟩) + bufVs := fun b hb => by rw [Eq.hk, utVsEq]; exact J.bufVs b hb + domOK := J.domOK.keepS J.sr.toSchedSRN ⟨rfl, rfl, rfl, rfl⟩ Eq.hk Eq.hc fun b ha hb => by + obtain ⟨o, ho, rfl⟩ := Sched.DomW.wa_exists J.j.1.ksFit hb + have ho8 : o.val % 8 = 0 := by + have hk8 := J.sr.toSchedSRN.ksb_al; have hb8 : (wa (L.ksb s) o).val % 8 = 0 := ha; rw [wa_valm] at hb8 + have hfit : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.j.1.ksFit + rw [Nat.mod_eq_of_lt (b := 2 ^ 64) (by omega)] at hb8; omega + exact Eq.ks o (by unfold ksSizeA; omega) ho8 + j5 := hJ5', j7 := j7' } + +/-! ### `JStage3A` ↔ `JStage3TA` + `VcpuRec6A` glue and `UbFootDisjAtA` -/ + +/-- Project `VcpuRec6A` directly from `JStage3A`. -/ +theorem JStage3A.vcpuRec6A + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) : + VcpuRec6A st Z OS Bf s σ := + ⟨J.vcSnap, J.vcSym1, J.vcSym2, J.vcBack, J.runBack, J.vcOS, J.sys.sysl.fo, J.userFr, J.frTh, J.bfSpec⟩ + +/-- Generic lift from `JStage3TA` + `SchedValidA` + `VcpuRec6A` back to `JStage3A` across any VCPU +operation preserving `OS` caps (`OSKeep`), with `SysL` and `SymL` supplied by `JSyscallA.decodeFinA` +or a step-specific frame lemma. -/ +theorem JStage3TA.toJA_vcpuA + {hw' : HwRootA} {s' : S} {a' : A} {σ' : Fl4SpecA64.State Unit} + (J0 : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + (J' : JStage3TA st AR { Z with hw := hw' } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s' a' σ' m) + (hSV' : SchedValidA σ') + (R6' : VcpuRec6A st { Z with hw := hw' } OS Bf s' σ') + (osk : OSKeep st OS s s') + (sysl : SysL st H Z.Cs OS ia ie s') (symL : SymL st H Z.Cs s') : + JStage3A st AR { Z with hw := hw' } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s' a' σ' m := + have capE : ∀ y ∈ OS, capA st s' y = capA st s y := fun y hy => osk.capA hy + J'.toJA sysl R6'.vcSym1 R6'.vcSym2 J0.heapWin J0.coresAll + (fun y hy => by have := J0.ptCapIn y hy; rwa [capAtA_eq, capE y (J0.j5.2.1 y hy), ← capAtA_eq]) + symL + (fun t ht => by rw [capE _ (J0.j5.2.1 _ (isSlot_fhA ((J0.sr.thT t).2 ht)))]; exact J0.fhOK t ht) + (fun y hy p zt n hc => J0.zwf y hy p zt n ((capE y (J0.j5.2.1 y hy)).symm.trans hc)) + hSV' (J0.utWin.keepA capE) (J0.devWin.keepA capE) + R6'.vcBack R6'.runBack R6'.snap R6'.vcOS R6'.userFr R6'.frTh R6'.bfSpec + +/-- Fixed-`ksb` placement side-conditions for user frames `Ub` and TCB user registers `[256, 3328)`. +Fixes the unsatisfiable universal `∀ ksb` quantifier in `UbFootDisjA` (`VcpuXferBaseA`) to the +state's actual kernel-state base `ksb`. -/ +structure UbFootDisjAtA (Z : XSetsA) (OS Ub : Set U64) (ksb : U64) : Prop where + al_os : ∀ y ∈ OS, Aligned y + disj_slotW : ∀ b ∈ Ub, ∀ y, SlotWA Z.th y → + b ≠ y ∧ b ≠ wa y 8#u64 ∧ b ≠ wa y 16#u64 ∧ b ≠ wa y 24#u64 + disj_vs : ∀ b ∈ Ub, ¬ VsFootA Z.Pp Z.S1 Z.Y ksb b + disj_ext : ∀ b ∈ Ub, ¬ ExtFootE Z.X Z.Y Z.T2 b + tcb_al : ∀ t ∈ Z.T, TcbAddr t + tcb_offVs : ∀ t ∈ Z.T, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → ¬ VsFootA Z.Pp Z.S1 Z.Y ksb (wa t K) + tcb_offExt : ∀ t ∈ Z.T, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → ¬ ExtFootE Z.X Z.Y Z.T2 (wa t K) + tcb_offOs : ∀ t ∈ Z.T, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → + ∀ y ∈ OS, wa t K ≠ y ∧ wa t K ≠ wa y 8#u64 ∧ wa t K ≠ wa y 16#u64 ∧ wa t K ≠ wa y 24#u64 + tcb_offSc : ∀ t ∈ Z.T, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → + ∀ c ∈ Z.C, ∀ o : U64, o.val < 128 → wa t K ≠ wa c o + +/-- **`UbFootDisjAtA`** derived from `JStage3A` + `J7UnpA` at `ksb := L.ksb s`. -/ +theorem JStage3A.ubFootDisjA {ar : machine.Arch A} {nA : U64} + {Pt Ub Pp7 Tb7 Hr Te Kt : Set U64} {Lv7 Re bits KLv : U64 → Nat} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s a σ m) + (W : J7UnpA st ar nA Z H OS s Pt Ub Pp7 Tb7 Hr Te Kt Lv7 Re bits KLv) : + UbFootDisjAtA Z OS Ub (L.ksb s) := by + have hVsIn : ∀ b, VsFootA Z.Pp Z.S1 Z.Y (L.ksb s) b → VsR (L.ksb s) Pt b := fun b hvs => by + rcases hvs.vsR with ⟨p, hp, h⟩ | h + · refine Or.inl ⟨p, W.sp p ?_, h⟩ + rcases hp with hp | hp | hp + · exact Or.inl hp + · exact Or.inr (Or.inl hp) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl hp)))))) + · exact Or.inr h + have hExtIn : ∀ b, ExtFootE Z.X Z.Y Z.T2 b → VsR (L.ksb s) Pt b := fun b hext => by + have hword : ∀ p, SchedPageA st Z OS s p → ∀ i : U64, i.val < 512 → + VsR (L.ksb s) Pt (wa p (core.num.U64.wrapping_mul i 8#u64)) := fun p hp i hi => + Or.inl ⟨p, W.sp p hp, core.num.U64.wrapping_mul i 8#u64, + by rw [wm8_valA hi]; omega, by rw [wm8_valA hi]; omega, rfl⟩ + cases hext with + | s2lo r i hr hi => exact hword r (Or.inr (Or.inr (Or.inr (Or.inl hr)))) i hi + | s2hi r i hr hi => exact hword _ (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨r, hr, rfl⟩))))) i hi + | s2pt t i ht hi => exact hword t (Or.inr (Or.inr (Or.inl ht))) i hi + | wl0 y hy => exact hword y (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl hy)))))) _ (by simp) + | wlo y i hy hi => exact hword y (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl hy)))))) i (by omega) + have hTcbOffOm : ∀ t ∈ Z.T, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → wa t K ∉ H.omega := fun t ht K hK1 hK2 hom => by + have htH : H t = some .tcb := by have : t ∈ H.tcbs := J.tcbs ▸ ht; exact this + have hft := J.sys.ent.heap.fits t .tcb htH; change t.val + 4096 ≤ 2 ^ 64 at hft + have hKval : (wa t K).val = t.val + K.val := by rw [wa_valP]; exact Nat.mod_eq_of_lt (by omega) + obtain ⟨-, p, k, hp, hk, hin⟩ := hom + have hne : t ≠ p := fun e => by + rw [← e, htH] at hp; cases hp + rcases hk with h | ⟨_, h⟩ | ⟨_, h⟩ | ⟨h, -⟩ <;> cases h + have hd := J.sys.ent.heap.disj t .tcb p k htH hp hne + have hlt := hin.2; have hle := hin.1; rw [hKval] at hle hlt + change t.val + 4096 ≤ p.val ∨ p.val + 2 ^ k.bits ≤ t.val at hd; omega + have hwa0 : ∀ y : U64, wa y 0#u64 = y := fun y => UScalar.eq_of_val_eq (by + rw [wa_valP]; change (y.val + 0) % 2 ^ 64 = y.val; rw [Nat.add_zero]; exact Nat.mod_eq_of_lt y.hBounds) + refine + { al_os := fun _ hy => J.sys.ent.os.al0 hy + disj_slotW := fun b hb y hsw => by + have hyOS := J.j5.2.1 y (J.slotWA_isSlotA hsw) + have hyfit := J.sys.ent.os.fit y hyOS + obtain ⟨u, hu, p, sz, r, mp, ept, a0, va, b0, hc, hbi, hlo, hhi⟩ := W.cc.uf b hb + have hnos : ¬ InOS OS b := fun ⟨y', hy', h1, h2⟩ => by + have := (J.sys.sysl.fo u hu p sz r mp ept a0 va hc b0 hbi).1 y' hy'; omega + have hlt8 : (wa y 8#u64).val = y.val + 8 := by rw [wa_valP]; change (y.val + 8) % _ = _; exact Nat.mod_eq_of_lt (by omega) + have hlt16 : (wa y 16#u64).val = y.val + 16 := by rw [wa_valP]; change (y.val + 16) % _ = _; exact Nat.mod_eq_of_lt (by omega) + have hlt24 : (wa y 24#u64).val = y.val + 24 := by rw [wa_valP]; change (y.val + 24) % _ = _; exact Nat.mod_eq_of_lt (by omega) + exact ⟨fun e => hnos ⟨y, hyOS, e ▸ le_refl _, by rw [e]; omega⟩, + fun e => hnos ⟨y, hyOS, by rw [e, hlt8]; omega, by rw [e, hlt8]; omega⟩, + fun e => hnos ⟨y, hyOS, by rw [e, hlt16]; omega, by rw [e, hlt16]; omega⟩, + fun e => hnos ⟨y, hyOS, by rw [e, hlt24]; omega, by rw [e, hlt24]; omega⟩⟩ + disj_vs := fun b hb hvs => W.vr.ub b (hVsIn b hvs) hb + disj_ext := fun b hb hext => W.vr.ub b (hExtIn b hext) hb + tcb_al := fun t ht => J.sq.tcbs t (J.tcbs ▸ ht) + tcb_offVs := fun t ht K hK1 hK2 hvs => hTcbOffOm t ht K hK1 hK2 (W.vr.om _ (hVsIn _ hvs)) + tcb_offExt := fun t ht K hK1 hK2 hext => hTcbOffOm t ht K hK1 hK2 (W.vr.om _ (hExtIn _ hext)) + tcb_offOs := fun t ht K hK1 hK2 y hy => by + have htH : H t = some .tcb := by have : t ∈ H.tcbs := J.tcbs ▸ ht; exact this + have hft := J.sys.ent.heap.fits t .tcb htH; change t.val + 4096 ≤ 2 ^ 64 at hft + have hKval : (wa t K).val = t.val + K.val := by rw [wa_valP]; exact Nat.mod_eq_of_lt (by omega) + have hslot := J.sys.ent.os.slot y hy + have hne : ∀ o : U64, o.val < 32 → o.val % 8 = 0 → wa t K ≠ wa y o := fun o ho1 ho8 e => by + rcases hslot.2 o ho1 ho8 with ⟨t', ht', K', hK', e'⟩ | ⟨hom, -⟩ + · rw [← e] at e' + have hft' := J.sys.ent.heap.fits t' .tcb ht'; change t'.val + 4096 ≤ 2 ^ 64 at hft' + have hK'val : (wa t' K').val = t'.val + K'.val := by rw [wa_valP]; exact Nat.mod_eq_of_lt (by omega) + have hval := congrArg UScalar.val e'; rw [hKval, hK'val] at hval + by_cases et : t = t' + · subst et; omega + · have hd := J.sys.ent.heap.disj t .tcb t' .tcb htH ht' et; simp only [HKind.bits] at hd; omega + · exact hTcbOffOm t ht K hK1 hK2 (e ▸ hom) + exact ⟨by simpa [hwa0] using hne 0#u64 (by decide) (by decide), + hne 8#u64 (by decide) (by decide), hne 16#u64 (by decide) (by decide), + hne 24#u64 (by decide) (by decide)⟩ + tcb_offSc := fun t ht K hK1 hK2 c hc o ho heq => by + have htH : H t = some .tcb := by have : t ∈ H.tcbs := J.tcbs ▸ ht; exact this + obtain ⟨k, hk⟩ : c ∈ H.scs := J.scs ▸ hc + have hmin := J.sys.ent.heap.scmin c k hk + have hpow : 128 ≤ 2 ^ k := by have : 2 ^ 7 ≤ 2 ^ k := Nat.pow_le_pow_right (by omega) hmin; simpa using this + have hne : t ≠ c := fun e => by rw [e, hk] at htH; cases htH + have hd := J.sys.ent.heap.disj t .tcb c (.sc k) htH hk hne; simp only [HKind.bits] at hd + have hf1 := J.sys.ent.heap.fits t .tcb htH; simp only [HKind.bits] at hf1 + have hf2 := J.sys.ent.heap.fits c (.sc k) hk; simp only [HKind.bits] at hf2 + have h1 : (wa t K).val = t.val + K.val := by rw [wa_valP]; exact Nat.mod_eq_of_lt (by omega) + have h2 : (wa c o).val = c.val + o.val := by rw [wa_valP]; exact Nat.mod_eq_of_lt (by omega) + have hval := congrArg UScalar.val heq; rw [h1, h2] at hval; omega } + +end bridge + +end +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3VcpuExitA.lean b/proofs/Fl4ProofsA64/P3/Stage3VcpuExitA.lean new file mode 100644 index 000000000..de46b9f73 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3VcpuExitA.lean @@ -0,0 +1,899 @@ +/- +P3 (aarch64, A64-P3 S4 Phase 2F): **§3 VM-exit refinement (`vcpuRunExitA`, `vcpuRunVppiA`) on +`JStage3A`** (pre-`syscall.finish` bodies of `syscall.handle_vcpu_run_exit` and +`syscall.handle_vcpu_run_vppi`). + +Composes existing `JStage3A` building blocks with zero hand-rebuilt records, zero global +`FrameMemRelA`, and **NO vacuous `SActA σ` hypothesis** (valid when `curThread` is in +`.ext .runningVM`): +- `JStage3A.timestampA_vcpu` (via `update_timestamp_refinesTA` + `sysL_symL_of_vwA` + `JStage3A.of_schedWr`) +- optional 1-word VPPI mask update (`wa vc 16#u64 := masked ||| 1#u64`) via `JStage3A.of_vcpuStepA` +- `ar.mask_irq` via `JStage3A.of_schedWr` (`WritesIn.refl`) +- `VcpuDelHookA.vcpu_complete_run_stage3A` (zero `FrameMemRelA`, zero `SActA σ`) +- `check_budget_sufficientJ` (`ScCheckA.lean`) +-/ +import Fl4ProofsA64.P3.VcpuDelHookA +import Fl4ProofsA64.P3.VcpuExitJA +import Fl4ProofsA64.P3.Stage3T3ArmsVcpu2A +import Fl4ProofsA64.P3.Stage3VcpuRunA + +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3 + +open Fl4Spec Fl4SpecA64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST +open Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Vcpu + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section exitHelpers + +variable {S A : Type} {st : machine.Store S} [L : LawfulStore st] + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J7 : JSlotA S} + {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +private theorem lift_okE {α : Type} (x : α) : lift x = ok x := rfl + +private theorem wa_zeroE (x : U64) : wa x 0#u64 = x := + UScalar.eq_of_val_eq (by rw [wa_valP]; change (x.val + 0) % 2 ^ 64 = x.val; simp [Nat.mod_eq_of_lt x.bv.isLt]) + +private theorem u64_eq_of_bv_eqE {a b : U64} (h : a.bv = b.bv) : a = b := by + cases a; cases b; simp at h; subst h; rfl + +private theorem fl4HandleVcpuRunExit_nonvppi_stage3A {σ σ1 σ2 : Fl4SpecA64.State Unit} {ct : ObjRef} + {stopSpec : RunStop} {w1 w2 w3 : Word} + (hne_vppi : stopSpec ≠ .vppi) + (hsp_ts : updateTimeStamp (Z := Unit) σ = ({((), σ1)}, False)) + (hct1 : σ1.curThread = ct) + (hsp_cr : fl4CompleteRun (Z := Unit) ct stopSpec w1 w2 w3 σ1 = ({((), σ2)}, False)) + (hcb_sp : checkBudget (Z := Unit) σ2 = ({(true, σ2)}, False)) : + fl4HandleVcpuRunExit (Z := Unit) ⟨stopSpec, w1, w2, w3⟩ σ = ({((), σ2)}, False) := by + unfold fl4HandleVcpuRunExit + rw [nd_bind_oneSc hsp_ts, nd_bind_oneSc (m := NonDet.gets _) rfl, hct1] + rw [nd_bind_oneSc (by unfold NonDet.when; rw [if_neg hne_vppi]; rfl)] + rw [nd_bind_oneSc hsp_cr, nd_bind_oneSc hcb_sp]; rfl + +private theorem vppi_when_eqE (ct : ObjRef) (irq : Word) (σ1 : Fl4SpecA64.State Unit) : + (do let vcOpt ← getRunVcpu (Z := Unit) ct + match vcOpt with + | some vc => NonDet.when (irq = 27) do + let v ← getVcpu vc + let mk := Function.update v.vcpuVppiMasked .vppiEventIRQ_VTimer true + setVcpu vc { v with vcpuVppiMasked := mk } + | none => pure () + doMachineOp (maskInterrupt true (ucast irqLen irq))) σ1 = + ((do let vcOpt ← getRunVcpu (Z := Unit) ct + match vcOpt with + | some vc => NonDet.when (irq = 27) do + let v ← getVcpu vc + let mk := Function.update v.vcpuVppiMasked .vppiEventIRQ_VTimer true + setVcpu vc { v with vcpuVppiMasked := mk } + | none => pure ()) >>= fun _ => doMachineOp (maskInterrupt true (ucast irqLen irq))) σ1 := by + rw [nd_bind_assocSc] + apply congrFun; apply congrArg (Bind.bind (getRunVcpu ct)); funext vcOpt + cases vcOpt with + | some vc => rfl + | none => funext s; rw [nd_bind_oneSc (m := pure ()) rfl] + +private theorem fl4HandleVcpuRunExit_vppi_stage3A {σ σ1 σ2 σ3 : Fl4SpecA64.State Unit} {ct : ObjRef} + {irq : Word} + (hsp_ts : updateTimeStamp (Z := Unit) σ = ({((), σ1)}, False)) + (hct1 : σ1.curThread = ct) + (hsp_vp : (do let vcOpt ← getRunVcpu (Z := Unit) ct + match vcOpt with + | some vc => NonDet.when (irq = 27) do + let v ← getVcpu vc + let mk := Function.update v.vcpuVppiMasked .vppiEventIRQ_VTimer true + setVcpu vc { v with vcpuVppiMasked := mk } + | none => pure ()) σ1 = ({((), σ2)}, False)) + (hsp_cr : fl4CompleteRun (Z := Unit) ct .vppi irq 0 0 + { σ2 with machineState := { σ2.machineState with + irqMasks := Function.update σ2.machineState.irqMasks (ucast irqLen irq) true } } = ({((), σ3)}, False)) + (hcb_sp : checkBudget (Z := Unit) σ3 = ({(true, σ3)}, False)) : + fl4HandleVcpuRunExit (Z := Unit) ⟨.vppi, irq, 0, 0⟩ σ = ({((), σ3)}, False) := by + unfold fl4HandleVcpuRunExit + rw [nd_bind_oneSc hsp_ts, nd_bind_oneSc (m := NonDet.gets _) rfl, hct1] + rw [nd_bind_oneSc (by + rw [NonDet.when, if_pos rfl] + exact (vppi_when_eqE ct irq σ1).trans (by rw [nd_bind_oneSc hsp_vp, maskInterrupt_stateA]))] + rw [nd_bind_oneSc hsp_cr, nd_bind_oneSc hcb_sp]; rfl + +/-- **`sched.update_timestamp` on `JStage3A`** (NO `SActA σ` required — valid when `curThread` is +in `.ext .runningVM`). `hE : CurEntryGA σ` is discharged at kernel entry via `CurEntryGA.of_cur` (as DelPpA). -/ +private theorem JStage3A.timestampA_vcpu {ar : machine.Arch A} {nA : U64} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s a σ m) + (hNoSw : ∀ u, σ.schedulerAction ≠ .switchThread u) + {Clock : A → Fl4SpecA64.MachineState → Prop} (P : NowAgreeA ar Clock) (hClock : Clock a σ.machineState) + (hnd : numDomains = 1) (V : ValidMachineTimeA σ) (C : CounterStepA σ) + (hNow : fl4ReadRawCounter σ.machineState = getCurrentTime σ.machineState) + (hAr : sched.update_timestamp st ar s a ⦃ r => AR r.2 Z.hw ⦄) (hE : CurEntryGA σ) : + sched.update_timestamp st ar s a ⦃ r => + ∃ σ1, updateTimeStamp (Z := Unit) σ = ({((), σ1)}, False) ∧ + σ1.kheap = σ.kheap ∧ + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) r.1 r.2 σ1 m ∧ + (∀ u, σ1.schedulerAction ≠ .switchThread u) ∧ + Clock r.2 σ1.machineState ∧ + L.ksb r.1 = L.ksb s ∧ L.cpu r.1 = L.cpu s ⦄ := by + obtain ⟨Pt, Ub, Pp7, Tb7, Hr, Te, Kt, Lv7, Re, bits7, KLv, W⟩ := J7A.unp J.j7 + have E0 : YieldQueueRelA (st := st) Z m κ s σ := + ⟨J.sr, fun _ ht _ hx => (J.ep.at_tcb ht hx).st, J.mcp, J.yieldRel⟩ + refine WP.spec_mono (WP.spec_and (WP.spec_and (update_timestamp_refinesTA E0 a P hClock hnd V C) + (update_timestamp_wordsA E0.sched.toSchedSRN a P hClock)) hAr) ?_ + rintro ⟨s', a'⟩ ⟨⟨⟨τ, hτ, E1, hCl, -, -, -, F⟩, now, -, -, es⟩, har⟩ + dsimp only at hτ E1 hCl F es har ⊢ + have eτ := fl4UpdateTimestamp_eqA σ + rw [hτ] at eτ + have eτ' : τ = fl4TimestampState (rawNextA σ.machineState) { σ with machineState := rawStateA σ.machineState } := + (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst eτ))).2 + have hsp : updateTimeStamp (Z := Unit) σ = ({((), τ)}, False) := by + rw [← fl4UpdateTimestamp_eq_l4vA σ hnd hNow, hτ] + have mem : ((), τ) ∈ (updateTimeStamp (Z := Unit) σ).1 := by rw [hsp]; exact Set.mem_singleton _ + have kh : τ.kheap = σ.kheap := by rw [eτ']; rfl + have um : τ.machineState.underlyingMemory = σ.machineState.underlyingMemory := by rw [eτ']; rfl + have irqN : τ.interruptIrqNode = σ.interruptIrqNode := by rw [eτ']; rfl + have HS : SpecSchedA σ τ := + ⟨fun _ => Or.inl (by rw [kh]), by rw [eτ']; rfl, irqN, by rw [eτ']; rfl, by rw [eτ']; rfl, + by rw [eτ']; rfl, by rw [eτ']; rfl⟩ + have SSm : SchedSameA σ { τ with curTime := σ.curTime, consumedTime := σ.consumedTime } := + { kh := fun q ko _ => by show τ.kheap q = some ko ↔ σ.kheap q = some ko; rw [kh] + cur := by rw [eτ']; rfl + idle := by rw [eτ']; rfl + csc := by rw [eτ']; rfl + rq := by rw [eτ']; rfl + rls := by rw [eτ']; rfl + time := rfl + cons := rfl } + have hidle : τ.idleThread = σ.idleThread := by rw [eτ']; rfl + have ecs : τ.curSc = σ.curSc := by rw [eτ']; rfl + have ers : τ.releaseQueue = σ.releaseQueue := by rw [eτ']; rfl + have hmono : σ.curTime ≤ τ.curTime := by rw [eτ']; exact BitVec.le_trans V C + have sinv0 : SInvXA { τ with curTime := σ.curTime, consumedTime := σ.consumedTime } m n := + J.sinv.of_spec SSm (by rw [eτ']; rfl) (SpecIrqA.of_eq J.sinv.irqN kh irqN) + have sinv : SInvXA τ m n := + { sinv0 with + zc := fun t x hx e hr => by + rw [kh] at hx + rw [ecs] at e + rw [ers] at hr + by_cases hc0 : σ.consumedTime = 0 + · obtain ⟨x', p, y, k, hx', hp, hy, a⟩ := J.sr.inv.relSc t hr + rw [hx] at hx' + obtain rfl := KernelObject.tcb.inj (Option.some.inj hx') + rw [e] at hp + obtain rfl := Option.some.inj hp + exact absurd hr ((hE hc0 y k hy a).2 t x hx e) + · exact absurd (J.sinv.zc t x hx e hr) hc0 + rlsRun := fun t ht => by + rw [ers] at ht + obtain ⟨x, hx, hr⟩ := J.sinv.rlsRun t ht + exact ⟨x, by rw [kh]; exact hx, hr⟩ + curScReady := fun hc y k hy a => by + rw [ecs, kh] at hy + by_cases hc0 : σ.consumedTime = 0 + · exact refillReady_monoA ((hE hc0 y k hy a).1) hmono + · exact refillReady_monoA (J.sinv.curScReady hc0 y k hy a) hmono + irqN := SpecIrqA.of_eq J.sinv.irqN kh irqN } + have SSz : SSzA σ τ := fun _ y _ h => ⟨y, by rw [← kh]; exact h⟩ + have hc0 := J.sq.cur + have nd := J.sq.nodes _ hc0 + have d48 : layout.node.CUR_TIME.val < 4288 := by simp [layout.node.CUR_TIME] + have d40 : layout.node.CONSUMED.val < 4288 := by simp [layout.node.CONSUMED] + have v48 : layout.node.CUR_TIME.val = 48 := by unfold layout.node.CUR_TIME; rfl + have v40 : layout.node.CONSUMED.val = 40 := by unfold layout.node.CONSUMED; rfl + have a48 := nodeAt_aligned nd d48 (by rw [v48]) + have a40 := nodeAt_aligned nd d40 (by rw [v40]) + obtain ⟨tA, etA⟩ : ∃ b, b = L.nodeAt s (L.cpu s) layout.node.CUR_TIME := ⟨_, rfl⟩ + obtain ⟨cA, ecA⟩ : ∃ b, b = L.nodeAt s (L.cpu s) layout.node.CONSUMED := ⟨_, rfl⟩ + rw [← etA] at a48; rw [← ecA] at a40 + obtain ⟨used, es'⟩ : ∃ used, s' = L.wr (L.wr s tA now) cA used := + ⟨_, by rw [etA, ecA]; exact es⟩ + have hw : WritesIn st s s' (fun b => b = tA ∨ b = cA) := by + rw [es']; exact (writesIn_wr (st := st) (s := s) (v := now) a48).trans (writesIn_wr (st := st) (v := used) a40) + have kb : L.ksb s' = L.ksb s := hw.1 + have cpu : L.cpu s' = L.cpu s := hw.2.1 + have tv : tA.val = L.nodeBase s (L.cpu s) + 48 := by rw [etA, nodeAt_val nd d48, v48] + have cv : cA.val = L.nodeBase s (L.cpu s) + 40 := by rw [ecA, nodeAt_val nd d40, v40] + have hnb : L.nodeBase s (L.cpu s) = (L.ksb s).val + 223488 + (L.cpu s).val * 4288 := rfl + have hcpu128 : (L.cpu s).val < 128 := by + obtain ⟨k, -, hk, h⟩ := J.j.1.csN; have := (h _).1 hc0; omega + have inKs : ∀ b : U64, (b = tA ∨ b = cA) → (L.ksb s).val + 223488 ≤ b.val ∧ b.val < (L.ksb s).val + 772352 := by + rintro b (rfl | rfl) <;> [rw [tv, hnb]; rw [cv, hnb]] <;> omega + have inKs2 : ∀ b : U64, (b = tA ∨ b = cA) → (L.ksb s).val + 223488 ≤ b.val ∧ b.val < (L.ksb s).val + 772352 ∧ + (b.val - (L.ksb s).val - 223488) % 4288 ≠ 120 ∧ (b.val - (L.ksb s).val - 223488) % 4288 ≠ 128 := by + rintro b (e | e) <;> rw [e] + · rw [tv, hnb]; refine ⟨by omega, by omega, by omega, by omega⟩ + · rw [cv, hnb]; refine ⟨by omega, by omega, by omega, by omega⟩ + have wS : WritesIn st s s' (SchedStepWA (st := st) Z m σ s) := + hw.mono fun b hb => Or.inl (Or.inr (Or.inl (Or.inr (Or.inr (Or.inr (inKs2 b hb)))))) + have osk : OSKeep st OS s s' := J.osKeep_stepA wS + have offT : ∀ b : U64, (b = tA ∨ b = cA) → ∀ t ∈ H.tcbs, b.val < t.val ∨ t.val + 4096 ≤ b.val := by + intro b hb t ht; have d := nd.disj t ht + rcases hb with rfl | rfl <;> [rw [tv]; rw [cv]] <;> (unfold LawfulStore.nodeBase at d ⊢; omega) + have offW : ∀ b : U64, Aligned b → ¬ (b = tA ∨ b = cA) → L.mem s' b = L.mem s b := fun b hb n => hw.mem_eq hb n + have tcbK : ∀ t ∈ Z.T, ∀ b : U64, Aligned b → t.val ≤ b.val → b.val < t.val + 4096 → L.mem s' b = L.mem s b := by + intro t ht b hb h1 h2; have htH : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + refine offW b hb (fun h => ?_); rcases offT b h t htH with g | g <;> omega + have tK : ∀ t ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → L.mem s' (wa t K) = L.mem s (wa t K) := by + intro t ht K h1 h2; have f := (J.sr.core.tcbs t ht).fits + have ev : (wa t K).val = t.val + K.val := by rw [wa_valP]; exact Nat.mod_eq_of_lt (by omega) + exact tcbK t ht _ (field_aligned (J.sr.core.tcbs t ht) h1 h2) (by omega) (by omega) + have hN1 : L.wr s tA now = L.wr s (L.nodeAt s (L.cpu s) layout.node.CUR_TIME) now := by rw [etA] + have e2 : s' = L.wr (L.wr s tA now) (L.nodeAt (L.wr s tA now) (L.cpu (L.wr s tA now)) layout.node.CONSUMED) used := by + rw [es', L.cpu_wr, nodeAt_of_ksb (L.ksb_wr _ _ _), ← ecA] + have lf48 : LowFree layout.node.CUR_TIME.val := by rw [v48]; unfold LowFree; omega + have lf40 : LowFree layout.node.CONSUMED.val := by rw [v40]; unfold LowFree; omega + have bx1 := nodew_bxc (o := layout.node.CUR_TIME) (v := now) J.sys.ent.bxi lf48 (by rw [v48]; omega) (by rw [v48]; omega) + rw [← hN1] at bx1 + have bx2 := nodew_bxc (o := layout.node.CONSUMED) (v := used) bx1.1.1 lf40 (by rw [v40]; omega) (by rw [v40]; omega) + rw [← e2] at bx2 + have sq2 : SchedQueuesInv st H.tcbs Z.Cs H.omega H s' Z.Q Z.R := by + obtain ⟨-, hB, hX, hSg, -⟩ := J.sys.ent.bxi + have n1 := node_bx (o := layout.node.CUR_TIME) (v := now) J.sq hB hX hSg lf48 (by rw [v48]; omega) (by rw [v48]; omega) + rw [← hN1] at n1; obtain ⟨q1, hB1, hX1, hSg1, -⟩ := n1 + have n2 := node_bx (o := layout.node.CONSUMED) (v := used) q1 hB1 hX1 hSg1 lf40 (by rw [v40]; omega) (by rw [v40]; omega) + rw [← e2] at n2; exact n2.1 + have hvsKeep : ∀ Pt' Ub' : Set U64, VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub' (L.ksb s) Pt' → + VsKeep st (L.ksb s) Pt' s s' := fun Pt' Ub' vr b hb => wS.mem_eq (vr.al b hb) (by + by_cases ha : AsW (L.ksb s) b + · obtain ⟨o, o1, o2, -, e⟩ := ha + have hfit := J.sys.ent.ksw + have ev : b.val = (L.ksb s).val + o.val := by rw [e, wa_valP]; exact Nat.mod_eq_of_lt (by omega) + exact J.asid_offStepA (by omega) (by omega) + · exact J.vsr_offStepA vr hb ha) + have hvsT := (utVsPt_keepA (s' := s') J.sys.ent.vs kb (hvsKeep _ _ J.vsOk)).1 + have hTable (j : U64) : L.mem s' (wa (L.ksb s) (AtOff j)) = L.mem s (wa (L.ksb s) (AtOff j)) := + hvsKeep _ _ J.vsOk _ (.inr (asW_at _ j)) + have offObj : ∀ b : U64, + ((∃ t ∈ Z.T, t.val ≤ b.val ∧ b.val < t.val + 4096) ∨ + ∃ q len, Z.th q = some (.other len) ∧ q.val ≤ b.val ∧ b.val < q.val + len) → ¬ (b = tA ∨ b = cA) := by + intro b h hb; have g := inKs b hb + rcases h with ⟨t, ht, h1, h2⟩ | ⟨q, len, hq, h1, h2⟩ + · have htH : t ∈ H.tcbs := J.tcbs ▸ ht + rcases offT b hb t htH with k | k <;> omega + · have k := J.j.1.place.ks q _ hq (fun h => by cases J.j.1.place.irqK hq h) + simp only [ObjKindA.size, ksSizeA] at k; omega + have hppObj : ∀ {b : U64}, (EFoot Z.T E Rp Bf b ∨ NFoot N Z.T b ∨ + (∃ r ∈ Rp, b = rNextA r ∨ b = rPrevA r)) → + (∃ t ∈ Z.T, t.val ≤ b.val ∧ b.val < t.val + 4096) ∨ + ∃ q len, Z.th q = some (.other len) ∧ q.val ≤ b.val ∧ b.val < q.val + len := fun {b} h => by + have hP := J.ipcPlace + rcases h with he | hn | ⟨r, hr, h1 | h1⟩ + · rcases he with ⟨e, he, hv⟩ | ⟨r, hr, hv⟩ | ⟨t, ht, hv⟩ | hbuf + · exact Or.inr ⟨e, _, hP.endpoint e he, by omega, by omega⟩ + · exact Or.inr ⟨r, _, hP.reply r hr, by omega, by omega⟩ + · rcases hv with ⟨h1, h2⟩ | h2 + · unfold TOffN at h2; exact Or.inl ⟨t, ht, by omega, by omega⟩ + · unfold TL at h2; exact Or.inl ⟨t, ht, by omega, by omega⟩ + · obtain ⟨p, len, hq, a, b0⟩ := hP.buffer b hbuf; exact Or.inr ⟨p, len, hq, a, b0⟩ + · rcases hn with ⟨nf, hnf, h1, h2⟩ | ⟨t, ht, e⟩ + · exact Or.inr ⟨nf, _, hP.notification nf hnf, h1, by omega⟩ + · have hf := (J.sr.core.tcbs t ht).fits + have tf : ∀ K : U64, K.val < 4096 → (∃ t' ∈ Z.T, t'.val ≤ (wa t K).val ∧ (wa t K).val < t'.val + 4096) := fun K hK => by + have ev : (wa t K).val = t.val + K.val := by rw [wa_valP]; exact Nat.mod_eq_of_lt (by omega) + exact ⟨t, ht, by omega, by omega⟩ + rcases e with rfl | rfl | rfl | rfl | rfl | rfl + · rw [stA_eq]; exact Or.inl (tf _ (by simp)) + · exact Or.inl (tf 3336#u64 (by simp)) + · have eb : badgeRegA t = wa t 256#u64 := by + unfold badgeRegA layout.tcb.REGS layout.reg.BADGE layout.reg.X0 + apply UScalar.eq_of_val_eq + simp [core.num.U64.wrapping_add_val_eq, core.num.U64.wrapping_mul_val_eq, U64.size_def, U64.numBits] + rw [eb]; exact Or.inl (tf _ (by simp)) + · exact Or.inl (tf 3448#u64 (by simp)) + · exact Or.inl (tf 3496#u64 (by simp)) + · exact Or.inl (tf 3504#u64 (by simp)) + · exact Or.inr ⟨r, _, hP.reply r hr, by + obtain ⟨-, -, g3⟩ := J.iplaced.e.rep r hr + rw [h1]; unfold rNextA layout.reply.NEXT; rw [wa_valP, Nat.mod_eq_of_lt (by simp; omega)]; simp, by + obtain ⟨-, -, g3⟩ := J.iplaced.e.rep r hr + rw [h1]; unfold rNextA layout.reply.NEXT; rw [wa_valP, Nat.mod_eq_of_lt (by simp; omega)]; simp⟩ + · exact Or.inr ⟨r, _, hP.reply r hr, by + obtain ⟨-, -, g3⟩ := J.iplaced.e.rep r hr + rw [h1]; unfold rPrevA layout.reply.PREV; rw [wa_valP, Nat.mod_eq_of_lt (by simp; omega)]; simp, by + obtain ⟨-, -, g3⟩ := J.iplaced.e.rep r hr + rw [h1]; unfold rPrevA layout.reply.PREV; rw [wa_valP, Nat.mod_eq_of_lt (by simp; omega)]; simp⟩ + have ep : EpStateRel Z.T E Rp Bf (L.mem s') τ := + J.ep.frame J.iplaced.e (fun b hb hf => offW b hb (offObj b (hppObj (Or.inl hf)))) + ⟨fun _ _ hk => by rw [kh]; exact hk, fun _ _ hk => ⟨_, by rw [kh]; exact hk, rfl⟩, + fun _ _ hk => ⟨_, by rw [kh]; exact hk, rfl, rfl, rfl, rfl, rfl⟩, um⟩ + have ntfn : NtfnStateRel N Z.T (L.mem s') τ := + J.ntfn.frame J.iplaced.n (fun b hb hf => offW b hb (offObj b (hppObj (Or.inr (Or.inl hf))))) + ⟨fun _ _ hk => by rw [kh]; exact hk, fun _ _ hk => ⟨_, by rw [kh]; exact hk, rfl, rfl, rfl⟩⟩ + have reply : ReplyStackRelA (L.mem s') Rp Z.C τ (scNameA m) stack := by + refine J.reply.frame (fun r hr => ?_) (fun _ _ y k h => ⟨y, k, by rw [kh]; exact h, rfl⟩) + (fun _ _ y h => ⟨y, by rw [← kh]; exact h, rfl⟩) + obtain ⟨g1, -, g3⟩ := J.iplaced.e.rep r hr + have e8 : (rPrevA r).val = r.val + 8 := by unfold rPrevA layout.reply.PREV; rw [wa_valP]; exact Nat.mod_eq_of_lt (by simp; omega) + have e16 : (rNextA r).val = r.val + 16 := by unfold rNextA layout.reply.NEXT; rw [wa_valP]; exact Nat.mod_eq_of_lt (by simp; omega) + exact ⟨offW _ (by show _ % 8 = 0; omega) (offObj _ (hppObj (Or.inr (Or.inr ⟨r, hr, Or.inl rfl⟩)))), + offW _ (by show _ % 8 = 0; omega) (offObj _ (hppObj (Or.inr (Or.inr ⟨r, hr, Or.inr rfl⟩))))⟩ + have bufCover : BufCover st Z.T Bf s' τ := by + intro t ht y b sb w hy hb hl + have al := (J.sr.core.tcbs t ht).align; have f := (J.sr.core.tcbs t ht).fits + have hib : IbSame (L := L) s s' t := + ⟨tK t ht _ (by simp) (by simp), + by rw [sbWordA_eq]; exact tK t ht _ (by simp) (by simp), + by rw [sbWordA8_eq]; exact tK t ht _ (by simp) (by simp)⟩ + rw [lookup_ipc_buffer_sameA hib] at hl + exact J.bufCover t ht y b sb w (by rw [← kh]; exact hy) hb hl + have wv : WritesIn st s s' (VW st H.tcbs Z.Cs Z.V {b | b ∈ Ub ∧ FrBw H OS b ∧ FrCw st OS s b} (L.ksb s) (fun _ => False)) := + hw.mono fun b hb => by + rcases hb with rfl | rfl + · exact Or.inr (Or.inr (Or.inr ⟨L.cpu s, hc0, layout.node.CUR_TIME, by rw [v48]; omega, by rw [v48]; omega, s, rfl, etA⟩)) + · exact Or.inr (Or.inr (Or.inr ⟨L.cpu s, hc0, layout.node.CONSUMED, by rw [v40]; omega, by rw [v40]; omega, s, rfl, ecA⟩)) + have hi : L.idleW s' = L.idleW s := by + unfold LawfulStore.idleW + have h8 : layout.node.IDLE_THREAD.val = 8 := by unfold layout.node.IDLE_THREAD; rfl + have hna8 := nodeAt_val nd (by rw [h8]; decide) + have hal := nodeAt_aligned nd (by rw [h8]; decide) (by rw [h8]) + rw [cpu, nodeAt_of_ksb kb] + exact offW _ hal (fun h => by + rcases h with h | h + · have hv := congrArg UScalar.val h; rw [hna8, h8, tv] at hv; omega + · have hv := congrArg UScalar.val h; rw [hna8, h8, cv] at hv; omega) + obtain ⟨sysl', symL'⟩ := sysL_symL_of_vwA J W wv hi (fun _ h => h.elim) + have hTr : TreeA st OS Z.G.chains Z.G.par s := + J.j.1.cdt.tree.grow J.j5.2.1 (fun y hy hn => J.j5.2.2 y hy hn) + have capE : ∀ y ∈ OS, capA st s' y = capA st s y := fun y hy => osk.capA hy + have j5' : J5A st Z H OS s' τ m := by + refine ⟨⟨MdbInvA.keepO hTr.lk hTr.tree J.j5.1.1 osk, IrqRunA.keepO hTr.lk J.j5.1.2.1 osk, + HandlersActiveA.keepO hTr.lk J.j5.1.2.2 osk kb fun j hj => ?_⟩, J.j5.2.1, + fun y hy hn => by rw [capE y hy]; exact J.j5.2.2 y hy hn⟩ + have eI : irqStAddrA (L.ksb s) j = irqWordA (L.ksb s) j := rfl + have hv := irqWordA_val J.sr.ksFit hj + have hal : Aligned (irqWordA (L.ksb s) j) := by have := J.sr.toSchedSRN.ksb_al; show _ % 8 = 0; omega + rw [eI]; exact offW _ hal (fun h => by have g := inKs _ h; omega) + have j7' : J7A st (J7R2A st ar nA) Z H OS s' τ m := + J7PpKA.keep (st := st) Z H OS s s' σ τ m J.j7 kb osk hvsKeep + have hSV' : SchedValidA τ := + ⟨by rw [eτ']; exact J.schedValid.rq, by rw [eτ']; exact J.schedValid.act, + by rw [eτ']; exact J.schedValid.blk, by rw [eτ']; exact J.schedValid.rls⟩ + have J' := JStage3A.of_schedWr (Q := Z.Q) (Rl := Z.R) (curOf := Z.curOf) (curScOf := Z.curScOf) + J wS HS SSz (Sched.qk_of mem) um har (fun _ _ => rfl) + (fun t ht y hy => J.bufAl t ht y (by rw [← kh]; exact hy)) + ⟨ppEntA3 J.sys.ent (BXI.blk bx2.1.1) kb hvsT, sysl'⟩ sq2 (poolOff_keepTableA J.poolOff kb hTable) + E1.sched (fun q y k h => J.scCov q y k (by rw [← kh]; exact h)) + ep ntfn reply bufCover + (fun c hc => by obtain ⟨y, hy, hf⟩ := J.idleNoFault c hc; exact ⟨y, by rw [kh]; exact hy, hf⟩) + E1.mcp E1.links sinv + (J.sidle.congr kh hidle (by rw [eτ']; exact BitVec.le_trans V C)) + (J.runVcpu.keep (fun t ht => by rw [stA_eq]; exact tK t ht _ (by simp) (by simp)) + (fun t ht => tK t ht 3672#u64 (by simp) (by simp))) + (J.blkQ.keepA (fun t ht => tK t ht 3336#u64 (by simp) (by simp)) + (fun _ _ x' hx => Or.inr ⟨x', by rwa [kh] at hx, rfl⟩) (fun _ _ h => by rw [kh]; exact h) + (fun _ _ h => by rw [kh]; exact h)) + symL' hSV' j5' j7' + exact ⟨τ, hsp, kh, J', fun u => by rw [eτ']; exact hNoSw u, hCl, kb, cpu⟩ + +/-- Set bit 0 of `vc.VPPI_MASKED` (`wa vc 16#u64`) on `JStage3A` via `JStage3A.of_vcpuStepA` +(NO `SActA σ` required). -/ +private theorem vcpu_mask_vppi_stage3A {ar : machine.Arch A} {nA : U64} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s a σ m) + (hNoSw : ∀ u, σ.schedulerAction ≠ .switchThread u) + {vc : U64} (hvc : vc ∈ Z.V) {v0 : Fl4SpecA64.Vcpu} + (hkv0 : σ.kheap (specPtr vc) = some (.archObj (.vcpu v0))) : + let s1 := L.wr s (wa vc 16#u64) (L.mem s (wa vc 16#u64) ||| 1#u64) + let mk := Function.update v0.vcpuVppiMasked .vppiEventIRQ_VTimer true + let σ1 := updKSc σ (specPtr vc) (.archObj (.vcpu { v0 with vcpuVppiMasked := mk })) + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s1 a σ1 m ∧ + (∀ u, σ1.schedulerAction ≠ .switchThread u) := by + intro s1 mk σ1 + obtain ⟨Pt, Ub, Pp7, Tb7, Hr, Te, Kt, Lv7, Re, bits7, KLv, W⟩ := J7A.unp J.j7 + have hVcP := J.vcPlacedA W + have R6 := J.vcpuRec6A + have hal16 : Aligned (wa vc 16#u64) := aligned_wa (hVcP.vc_al hvc) 16#u64 (by decide) + have hw : WritesIn st s s1 (VcpuWrFootA st true Z OS s) := + ⟨L.ksb_wr _ _ _, L.cpu_wr _ _ _, fun q hqa hnf => + mem_wr_neA hal16 hqa (fun e => hnf (Or.inl ⟨vc, hvc, 16#u64, by decide, by decide, e.symm⟩))⟩ + have hHS : VcpuSpecStepA H σ σ1 true := + ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, + fun p => by + by_cases e : p = specPtr vc + · subst e; exact Or.inr (Or.inr ⟨v0, _, hkv0, updKSc_same _ _ _, trivial⟩) + · exact Or.inl (updKSc_other _ e), + fun _ h => absurd rfl h⟩ + have osk : OSKeep st OS s s1 := osKeep_of_vcpuWrCoreA (J.toVcpuCoreA hNoSw) R6.vcOS R6.frOff hw + have hne_vc : ∀ v ∈ Z.V, ∀ o : U64, o.val < 64 → (v = vc → o.val ≠ 16) → wa vc 16#u64 ≠ wa v o := fun v hv o ho hne => by + by_cases hvv : v = vc + · subst hvv; exact hVcP.same_vcpu_ne hv (by decide) (by omega) + (fun e => hne rfl (by have := congrArg UScalar.val e; change 16 = o.val at this; omega)) + · exact hVcP.disj_vcpu hvc hv (Ne.symm hvv) (by decide) (by omega) + have ht_eq : ∀ t ∈ Z.T, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → L.mem s1 (wa t o) = L.mem s (wa t o) := fun t ht o ho ho8 => + mem_wr_neA hal16 (aligned_wa (hVcP.al_t t ht) o ho8) (hVcP.disj_tcb vc hvc t ht 16#u64 o (by decide) ho) + have hvc_eq : ∀ v ∈ Z.V, L.mem s1 v = L.mem s v ∧ L.mem s1 (wa v 24#u64) = L.mem s (wa v 24#u64) := fun v hv => + ⟨mem_wr_neA hal16 (hVcP.vc_al hv) (by simpa [wa_zeroE] using hne_vc v hv 0#u64 (by decide) (fun _ => by decide)), + mem_wr_neA hal16 (aligned_wa (hVcP.vc_al hv) 24#u64 (by decide)) (hne_vc v hv 24#u64 (by decide) (fun _ => by decide))⟩ + have hcore : VcpuCoreA (L := L) Z s1 σ1 := + ⟨fun v hv => by + obtain ⟨x, hx, h1, h2⟩ := J.j.2.1.obj v hv + rw [(hvc_eq v hv).1, (hvc_eq v hv).2] + by_cases hvv : v = vc + · subst hvv; rw [hkv0] at hx; cases hx; exact ⟨_, updKSc_same _ _ _, h1, h2⟩ + · exact ⟨x, by rw [updKSc_other _ (fun e => hvv (specPtr_inj e))]; exact hx, h1, h2⟩, + fun p x hx => by + by_cases hp : p = specPtr vc + · subst hp; exact ⟨vc, hvc, rfl⟩ + · exact J.j.2.1.back p x (by rwa [updKSc_other _ hp] at hx), + fun t ht => by + obtain ⟨x, hx, h1, h2⟩ := J.j.2.1.tcb t ht + have hne : specPtr t ≠ specPtr vc := fun e => by rw [e, hkv0] at hx; cases hx + refine ⟨x, by rw [updKSc_other _ hne]; exact hx, + by rw [ht_eq t ht 3664#u64 (by decide) (by decide)]; exact h1, + by unfold runVcpuA at h2 ⊢; rw [ht_eq t ht 3672#u64 (by decide) (by decide)]; exact h2⟩, + fun y hy => by rw [capAtA_eq, osk.capA (J.j5.2.1 y hy), ← capAtA_eq]; exact J.j.2.1.cap y hy⟩ + have hvcRun : VcRunA st Z.T Z.V s1 := + ⟨fun t ht => by rw [ht_eq t ht 3664#u64 (by decide) (by decide)]; exact J.vcRun.tcb t ht, + fun v hv => by rw [(hvc_eq v hv).2]; exact J.vcRun.vcpu v hv, + fun v hv => by rw [(hvc_eq v hv).1]; exact J.vcRun.vtcb v hv, + fun t ht => by unfold runVcpuA; rw [ht_eq t ht 3672#u64 (by decide) (by decide)]; exact J.vcRun.run t ht, + fun t ht hnz => by + rw [ht_eq t ht 3664#u64 (by decide) (by decide)] at hnz + unfold runVcpuA; rw [ht_eq t ht 3672#u64 (by decide) (by decide)]; exact J.vcRun.disj t ht hnz⟩ + have hrunVcpu : RunVcpuInv st Z.T s1 := fun t ht hne => by + unfold runVcpuA at hne; rw [ht_eq t ht 3672#u64 (by decide) (by decide)] at hne + rw [stA_eq_wa, ht_eq t ht 3328#u64 (by decide) (by decide), ← stA_eq_wa]; exact J.runVcpu t ht hne + have hEpTcb : ∀ t ∈ Z.T, ∃ x, σ1.kheap (specPtr t) = some (.tcb x) ∧ TcbRelE Rp (L.mem s1) t x := fun t ht => by + obtain ⟨x, hx, hr⟩ := J.ep.tcb t ht + have hne : specPtr t ≠ specPtr vc := fun e => by rw [e, hkv0] at hx; cases hx + refine ⟨x, by rw [updKSc_other _ hne]; exact hx, hr.frame (J.iplaced.e.tcb t ht) (fun b hb htf => ?_)⟩ + obtain ⟨o, ho, ho8, rfl⟩ : ∃ o : U64, o.val < 4096 ∧ o.val % 8 = 0 ∧ b = wa t o := by + have hfit := (J.iplaced.e.tcb t ht).fits; have hal_t := (J.iplaced.e.tcb t ht).align + have hlt4096 : b.val - t.val < 4096 := by + have hto := htf.2; unfold TOffN at hto + rcases hto with h | h | h | h | h | h | h | h | ⟨_, _⟩ | h | h | h | h | h | h <;> omega + have h1 : b.val - t.val < 2 ^ 64 := by omega + refine ⟨⟨BitVec.ofNat 64 (b.val - t.val)⟩, ?_, ?_, UScalar.eq_of_val_eq ?_⟩ + · change (BitVec.ofNat 64 _).toNat < 4096; rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt h1]; exact hlt4096 + · change (BitVec.ofNat 64 _).toNat % 8 = 0; rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt h1]; have := hb; omega + · rw [wa_valP]; change b.val = (t.val + (BitVec.ofNat 64 _).toNat) % 2 ^ 64 + have htb := htf.1 + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt h1, Nat.mod_eq_of_lt (by omega)]; omega + exact ht_eq t ht o ho ho8 + have hEpBuf : ∀ bp ∈ Bf, ∀ k : U64, k.val < 128 → + (L.mem s1 (bufW bp.1 k)).bv = loadWordAt σ1.machineState.underlyingMemory (bufAddr bp.2 k.val) ∧ + (bufAddr bp.2 k.val).toNat % 8 = 0 := fun bp hbp k hk => by + have hbw : BufW Bf (bufW bp.1 k) := ⟨bp, hbp, k, hk, rfl⟩ + rw [mem_wr_neA hal16 (J.iplaced.e.balign _ hbw) (fun e => J.bufOff.vcw _ hbw vc hvc 16#u64 (by decide) (by decide) e.symm)] + exact J.ep.buf bp hbp k hk + have hSnap : VcpuStateSnapA Z.V (L.mem s1) σ1 := by + refine ⟨fun v hv x hkx => ?_, fun v hv x hkx => ?_⟩ + · have halv := hVcP.vc_al hv + have hne_r : ∀ i < 64, wa vc 16#u64 ≠ vcpuRegWA v i := fun i hi => by + by_cases hvv : v = vc + · subst hvv; exact hVcP.same_vcpu_ne hv (by decide) (by rw [vcpuRegOffA_val hi]; omega) + (fun e => by have := congrArg UScalar.val e; rw [vcpuRegOffA_val hi] at this; change 16 = _ at this; omega) + · exact hVcP.disj_vcpu hvc hv (Ne.symm hvv) (by decide) (by rw [vcpuRegOffA_val hi]; omega) + rw [mem_wr_neA hal16 (aligned_wa halv 32#u64 (by decide)) (hne_vc v hv 32#u64 (by decide) (fun _ => by decide)), + mem_wr_neA hal16 (aligned_wa halv 40#u64 (by decide)) (hne_vc v hv 40#u64 (by decide) (fun _ => by decide)), + mem_wr_neA hal16 (aligned_wa halv 8#u64 (by decide)) (hne_vc v hv 8#u64 (by decide) (fun _ => by decide))] + by_cases hvv : v = vc + · subst hvv; rw [updKSc_same] at hkx; cases hkx + obtain ⟨hr0, hvp0, hro0, hrm0, how0⟩ := R6.snap.snap v hv v0 hkv0 + refine ⟨fun i hi => by + rw [show wa v ⟨BitVec.ofNat 64 (64 + 8 * i)⟩ = vcpuRegWA v i from rfl, + mem_wr_neA hal16 (hVcP.vcPage_al (vcpuRegWA_in_vcPage hv hi)) (hne_r i hi)] + exact hr0 i hi, + by rw [L.mem_wr_same hal16, hvp0]; simp only [mk, Function.update_self, ↓reduceIte] + cases v0.vcpuVppiMasked .vppiEventIRQ_VTimer <;> decide, + hro0, hrm0, how0⟩ + · rw [updKSc_other _ (fun e => hvv (specPtr_inj e))] at hkx + obtain ⟨hr0, hvp0, hro0, hrm0, how0⟩ := R6.snap.snap v hv x hkx + refine ⟨fun i hi => by + rw [show wa v ⟨BitVec.ofNat 64 (64 + 8 * i)⟩ = vcpuRegWA v i from rfl, + mem_wr_neA hal16 (hVcP.vcPage_al (vcpuRegWA_in_vcPage hv hi)) (hne_r i hi)] + exact hr0 i hi, + by rw [mem_wr_neA hal16 (aligned_wa halv 16#u64 (by decide)) (hne_vc v hv 16#u64 (by decide) (fun e => absurd e hvv))]; exact hvp0, + hro0, hrm0, how0⟩ + · by_cases hvv : v = vc + · subst hvv; rw [updKSc_same] at hkx; cases hkx; exact R6.snap.mask v hv v0 hkv0 + · rw [updKSc_other _ (fun e => hvv (specPtr_inj e))] at hkx; exact R6.snap.mask v hv x hkx + have R6_1 : VcpuRec6A st Z OS Bf s1 σ1 := + { snap := hSnap + vcSym1 := fun t ht hnz => by + rw [ht_eq t ht 3664#u64 (by decide) (by decide)] at hnz ⊢ + have hv_m := (J.vcRun.tcb t ht).resolve_left hnz + rw [(hvc_eq _ hv_m).1]; exact R6.vcSym1 t ht hnz + vcSym2 := fun v hv hnz => by + rw [(hvc_eq v hv).2] at hnz ⊢ + have ht_m := (J.vcRun.vcpu v hv).resolve_left hnz + unfold runVcpuA; rw [ht_eq _ ht_m 3672#u64 (by decide) (by decide)]; exact R6.vcSym2 v hv hnz + vcBack := fun v hv hnz => by + rw [(hvc_eq v hv).1] at hnz ⊢ + have ht_m := (J.vcRun.vtcb v hv).resolve_left hnz + rw [ht_eq _ ht_m 3664#u64 (by decide) (by decide)]; exact R6.vcBack v hv hnz + runBack := fun t ht hnz => by + unfold runVcpuA at hnz ⊢; rw [ht_eq t ht 3672#u64 (by decide) (by decide)] at hnz ⊢ + have hv_m := (J.vcRun.run t ht).resolve_left hnz + unfold runVcpuA at hv_m + rw [(hvc_eq _ hv_m).2]; exact R6.runBack t ht hnz + vcOS := R6.vcOS + frOff := fun y hy p sz r mp ept asid va hcap => by + rw [L.ksb_wr]; exact R6.frOff y hy p sz r mp ept asid va (by rwa [osk.capA hy] at hcap) + frTh := R6.frTh.keepA (fun y hy => osk.capA hy) fun _ _ e => e + userFr := R6.userFr.keepA (fun y hy => osk.capA hy) + (fun p sz _ h => by rw [updKSc_other _ (fun e => by rw [e, hkv0] at h; cases h)]; exact h) + bfSpec := R6.bfSpec } + have wv : WritesIn st s s1 (VW st H.tcbs Z.Cs Z.V {b | b ∈ Ub ∧ FrBw H OS b ∧ FrCw st OS s b} (L.ksb s) (fun _ => False)) := + (writesIn_wr hal16).mono fun _ e => by subst e; exact Or.inr (Or.inl ⟨vc, hvc, 16#u64, by decide, by decide, rfl⟩) + have hi : L.idleW s1 = L.idleW s := by + unfold LawfulStore.idleW + have h8 : layout.node.IDLE_THREAD.val = 8 := by unfold layout.node.IDLE_THREAD; rfl + have hna8 := nodeAt_val (J.sq.nodes _ J.sr.sch.cpuCs) (by rw [h8]; decide) + unfold LawfulStore.nodeBase at hna8 + have hal := nodeAt_aligned (J.sq.nodes _ J.sr.sch.cpuCs) (by rw [h8]; decide) (by rw [h8]) + rw [L.cpu_wr, nodeAt_of_ksb (L.ksb_wr _ _ _)] + have hcpu128 : (L.cpu s).val < 128 := by + obtain ⟨nn, -, hkn, pcs⟩ := J.sr.toSchedSRN.csN; have := (pcs _).1 J.sr.sch.cpuCs; omega + have hfit := J.sys.ent.ksw + exact mem_wr_neA hal16 hal (fun e => hVcP.disj_ks _ ⟨vc, hvc, 16#u64, by decide, by decide, rfl⟩ + ⟨by rw [e, hna8, h8]; omega, by rw [e, hna8, h8]; unfold ksSizeA; omega⟩) + obtain ⟨sysl1, symL1⟩ := sysL_symL_of_vwA J W wv hi (fun _ h => h.elim) + have hZ : ({ Z with hw := Z.hw } : XSetsA) = Z := by cases Z; rfl + have J1 := JStage3A.of_vcpuStepA J hNoSw R6 hw hHS J.j.1.ar + (fun b hb => hw.mem_eq (J.vsOk.al b hb) (utVs_off_vcpuWrCoreA (J.toVcpuCoreA hNoSw) hb)) + hcore hvcRun hrunVcpu hEpTcb hEpBuf + (fun v hv hd x hx => by + have hd0 : DeadA st OS .vcpu s v := fun y hy hc => hd y hy (by rw [osk.capA hy]; exact hc) + by_cases hvv : v = vc + · subst hvv; rw [updKSc_same] at hx; cases hx; exact J.deadExt.vcpu v hv hd0 v0 hkv0 + · rw [updKSc_other _ (fun e => hvv (specPtr_inj e))] at hx; exact J.deadExt.vcpu v hv hd0 x hx) + (fun t ht hd => by + rw [ht_eq t ht 3664#u64 (by decide) (by decide)] + exact J.deadExt.tvcpu t ht (fun y hy hc => hd y hy (by rw [osk.capA hy]; exact hc))) + (fun v hv hd => by + rw [(hvc_eq v hv).2] + exact J.deadExt.vcDeadRun v hv (fun y hy hc => hd y hy (by rw [osk.capA hy]; exact hc))) + (fun y hy u num hz => by + have hz0 : capA st s y = .Zombie u capability.ZOMBIE_TCB num := by rwa [osk.capA hy] at hz + have huT : u ∈ Z.T := J.tcbs ▸ J.zombieTcb y hy u num hz0 + rw [ht_eq u huT 3664#u64 (by decide) (by decide)]; exact J.zombieNoVcpu y hy u num hz0) + (SchedValidA.of_vcpuStepA J.schedValid hHS) R6_1 sysl1 symL1 + rw [hZ] at J1 + exact ⟨J1, hNoSw⟩ + +/-- Lift `JStage3A` across a pure `ar.mask_irq` update to `σ.machineState.irqMasks` +(NO `SActA σ` required). -/ +private theorem jstage3A_of_maskInterruptA {Rj : Set U64 → JSlotA S} + [J7PpA st (J7A st Rj)] [J7PpKA st (J7A st Rj)] {a2 : A} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st Rj) s a σ m) + (hAR2 : AR a2 Z.hw) (b : Bool) (irq : Fl4SpecA64.Irq) : + let σm := { σ with machineState := { σ.machineState with + irqMasks := Function.update σ.machineState.irqMasks irq b } } + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st Rj) s a2 σm m := by + intro σm + have hwS : WritesIn st s s (SchedStepWA (st := st) Z m σ s) := ⟨rfl, rfl, fun _ _ _ => rfl⟩ + have hj5' : J5A st Z H OS s σm m := J.j5 + have hj7' : J7A st Rj Z H OS s σm m := + J7PpKA.keep (st := st) Z H OS s s σ σm m J.j7 rfl (fun _ _ => ⟨rfl, rfl, rfl, rfl⟩) (fun _ _ _ _ _ => rfl) + have hsr_m : SchedSRA st Z m κ s σm := + J.sr.stable (W := fun _ => False) ⟨rfl, rfl, fun _ _ _ => rfl⟩ (fun _ h => h.elim) + ⟨⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩, rfl, rfl, rfl, + fun _ _ _ _ => rfl, fun _ x hx => ⟨x, hx, rfl, rfl, rfl⟩, + fun _ _ _ _ => rfl, fun _ y hy => ⟨y, hy⟩, fun _ y hy => ⟨y, hy, rfl⟩⟩ + (fun _ _ _ hx hy => by rw [hx] at hy; cases hy; exact Or.inl ⟨rfl, id⟩) + have hZ : ({ Z with Q := Z.Q, R := Z.R, curOf := Z.curOf, curScOf := Z.curScOf } : XSetsA) = Z := by cases Z; rfl + have h := JStage3A.of_schedWr (Q := Z.Q) (Rl := Z.R) (curOf := Z.curOf) (curScOf := Z.curScOf) + J (s' := s) (a' := a2) (σ' := σm) hwS + ⟨fun _ => Or.inl rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ (fun _ y _ h => ⟨y, h⟩) + ⟨fun _ x h => ⟨x, h, rfl⟩, fun _ x h => ⟨x, h, List.Sublist.refl _⟩, + fun _ x h => ⟨x, h, List.Sublist.refl _⟩, fun _ y k h => ⟨y, k, h, fun r => ⟨r, id⟩⟩, + fun _ y h => ⟨y, h, id⟩, ⟨rfl, rfl, rfl, rfl⟩⟩ + rfl hAR2 (fun _ _ => rfl) J.bufAl J.sys J.sq J.poolOff (hZ.symm ▸ hsr_m) J.scCov + ⟨J.ep.1, J.ep.2, J.ep.3, J.ep.4⟩ ⟨J.ntfn.1, J.ntfn.2⟩ + (by cases J.reply; constructor <;> assumption) J.bufCover + J.idleNoFault J.mcp (by cases J.yieldRel; constructor <;> assumption) + (J.sinv.of_spec ⟨fun _ _ _ => Iff.rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ rfl + (by rcases J.sinv.irqN with ⟨⟨⟨a1, a2⟩, a3⟩, a4, a5⟩; exact ⟨⟨⟨a1, a2⟩, a3⟩, a4, a5⟩)) + ⟨J.sidle.f3, J.sidle.f4, J.sidle.f5, J.sidle.f6.of_eq (fun _ => rfl), J.sidle.f7, J.sidle.idleSlot, J.sidle.idleBound⟩ + J.runVcpu ⟨J.blkQ.1, J.blkQ.2⟩ J.symL + (J.schedValid.congr rfl rfl rfl rfl rfl rfl (fun _ _ _ h => h) (fun _ x h => ⟨x, h, rfl⟩) (fun _ x h => ⟨x, h, rfl, rfl⟩)) + hj5' hj7' + rwa [hZ] at h + +/-- The VPPI mask step on the current thread's VCPU is a `DlA` step. -/ +private theorem vppiBlock_dlA (ct : ObjRef) (irq : Word) : NdDlA (do + let vcOpt ← getRunVcpu (Z := Unit) ct + match vcOpt with + | some vc => NonDet.when (irq = 27) do + let v ← getVcpu vc + let mk := Function.update v.vcpuVppiMasked .vppiEventIRQ_VTimer true + setVcpu vc { v with vcpuVppiMasked := mk } + | none => pure ()) := by + refine NdDlA.bind (getRunVcpu_dlA _) fun o => ?_ + cases o with + | none => exact NdDlA.pure _ + | some vc => exact NdDlA.when (NdDlA.bind (getVcpu_dlA _) fun _ => setVcpu_dlA _ _) + +end exitHelpers + +section vcpuExit + +variable {S A : Type} {st : machine.Store S} [L : LawfulStore st] + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} + {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +set_option maxHeartbeats 800000 in +/-- **§3 VM-exit refinement (non-VPPI, pre-`syscall.finish`) on `JStage3A`**: +the pre-`syscall.finish` body of `syscall.handle_vcpu_run_exit` refines +`fl4HandleVcpuRunExit ⟨stopSpec, w1.bv, w2.bv, w3.bv⟩` for any `stopSpec ≠ .vppi`. +Zero global `FrameMemRelA`; zero hand-rebuilt records. +`hE : CurEntryGA σ` is discharged at kernel entry via `CurEntryGA.of_cur` (as DelPpA). -/ +theorem JStage3A.vcpuRunExitA {ar : machine.Arch A} (hPl : ArchPlat ar) + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hNoSw : ∀ u, σ.schedulerAction ≠ .switchThread u) + {Clock : A → Fl4SpecA64.MachineState → Prop} + (hNow : NowAgreeA ar Clock) (hNowAR : ∀ a0 h, AR a0 h → ar.now a0 ⦃ r => AR r.2 h ⦄) + (hClock : Clock a σ.machineState) + (hClVm : ∀ s' a' ms (u : U64), Clock a' ms → vspace.set_vm_root st ar s' a' u ⦃ r => Clock r.2 ms ⦄) + (hClFlushState : ∀ a' ms (vc om : U64), Clock a' ms → ar.vcpu_flush_state a' vc om ⦃ a'' => Clock a'' ms ⦄) + (hClFr : ∀ a' ms ms', Clock a' ms → ms'.timeState = ms.timeState → + ms'.lastMachineTime = ms.lastMachineTime → Clock a' ms') + (hnd : numDomains = 1) (hVMT : ValidMachineTimeA σ) (hCS : CounterStepA σ) + (hNowEq : fl4ReadRawCounter σ.machineState = getCurrentTime σ.machineState) + (hE : CurEntryGA σ) + (hP : VcpuPlatAgreeA ar) (hAK : VcpuArchKeepsA ar AR) (hVR : VcpuVmRootA st ar AR) + (hScPost : ∀ (s2 : S) (a2 : A) (hw2 : HwRootA) (σ2 : Fl4SpecA64.State Unit), + JStage3A st AR { Z with hw := hw2 } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s2 a2 σ2 m → + ScStateRelA st Z.C m s2 σ2 ∧ + (∃ xsc nsc, σ2.kheap σ2.curSc = some (.schedContext xsc nsc) ∧ scActive xsc = true) ∧ + sporadic.min_budget ar a2 ⦃ mb => mb.bv = MIN_BUDGET ⦄ ∧ + getScRefillSufficient (Z := Unit) σ2.curSc σ2.consumedTime σ2 = ({(true, σ2)}, False)) + {stop : U64} {stopSpec : RunStop} (hstop : stop.bv = stopSpec.word) (hne_vppi : stopSpec ≠ .vppi) + (w1 w2 w3 : U64) : + (do + let (s1, a1) ← sched.update_timestamp st ar s a + let ct ← sched.cur_thread st s1 + let (s2, a2) ← vcpu.complete_run st ar s1 a1 ct stop w1 w2 w3 + let (_, s3, a3) ← sched.check_budget st ar s2 a2 + ok (s3, a3)) ⦃ res => + ∃ hw' σ' σ1, + fl4HandleVcpuRunExit (Z := Unit) ⟨stopSpec, w1.bv, w2.bv, w3.bv⟩ σ = ({((), σ')}, False) ∧ + updateTimeStamp (Z := Unit) σ = ({((), σ1)}, False) ∧ DrA σ1 σ' ∧ + JStage3A st AR { Z with hw := hw' } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) res.1 res.2 σ' m ⦄ := by + have hArTs : sched.update_timestamp st ar s a ⦃ r => AR r.2 Z.hw ⦄ := by + have hct : sched.cur_time st s = ok (L.curTime s) := by + unfold sched.cur_time LawfulStore.curTime LawfulStore.nodeA + rw [L.cur_cpu_eq, bind_ok, eq_of_spec_fd (obj.node_get.spec s _ _)] + unfold sched.update_timestamp + rw [hct, bind_ok] + refine WP.spec_bind (hNowAR a Z.hw J.j.1.ar) ?_ + rintro ⟨now, a1⟩ hAR1 + simp only [uncurry_apply_pair] + rw [L.cur_cpu_eq, bind_ok, eq_of_spec_fd (obj.node_set.spec s _ _ _)] + simp only [bind_ok, lift] + refine WP.spec_bind (sched.consumed.specA _) ?_ + rintro _ rfl + refine WP.spec_bind (sched.set_consumed.spec _ _) ?_ + rintro s2 rfl + simp only [layout.NUM_DOMAINS, fl4_abi.config.NUM_DOMAINS, + show ¬ ((1#u64 : U64) > 1#u64) from by decide, ↓reduceIte, WP.spec_ok] + exact hAR1 + refine WP.spec_bind (J.timestampA_vcpu hNoSw hNow hClock hnd hVMT hCS hNowEq hArTs hE) ?_ + rintro ⟨s1, a1⟩ ⟨σ1, hsp_ts, -, J1, hNoSw1, hCl1, -, -⟩ + dsimp only at J1 hCl1 + have hσ1_ct : σ1.curThread = specPtr (L.curW s1) := by + rw [← J1.j.1.sch.cur, J1.j.1.sch.core]; exact J1.j.1.sch.curOfW _ J1.j.1.sch.cpuCs + change (do let ct ← sched.cur_thread st s1 + let (s2, a2) ← vcpu.complete_run st ar s1 a1 ct stop w1 w2 w3 + let (_, s3, a3) ← sched.check_budget st ar s2 a2 + ok (s3, a3)) ⦃ _ ⦄ + rw [show sched.cur_thread st s1 = ok (L.curW s1) from eq_of_spec_fd (sched.cur_thread.spec s1), bind_ok] + have hct1 : L.curW s1 ∈ Z.T := J1.sr.curT + refine WP.spec_bind (vcpu_complete_run_stage3A (Clock := Clock) J1 hNoSw1 hCl1 hClVm hClFlushState hClFr + hP hAK hVR hct1 hstop w1 w2 w3) ?_ + rintro ⟨s2, a2⟩ ⟨hw2, σ2, hsp_cr, J2, -, -, -, -, -, -, -, -⟩ + obtain ⟨hSc2, ⟨xsc, nsc, hks2, hact2⟩, hmb2, hsuf2⟩ := hScPost s2 a2 hw2 σ2 J2 + have hks2' : σ2.kheap (scNameA m (L.cscW s2)) = some (.schedContext xsc nsc) := by + have hcw : σ2.curSc = scNameA m (L.cscW s2) := J2.j.1.sch.curScW + rw [← hcw]; exact hks2 + refine WP.spec_bind (check_budget_sufficientJ (AR := AR) a2 J2.j.1 hSc2 hks2' hact2 hmb2 hnd hsuf2) ?_ + rintro ⟨b, s3, a3⟩ ⟨heq3, hcb_sp⟩ + have hs3 : s3 = s2 := congrArg (fun t => t.2.1) heq3 + have ha3 : a3 = a2 := congrArg (fun t => t.2.2) heq3 + rw [hs3, ha3] + exact (WP.spec_ok (s2, a2)).2 ⟨hw2, σ2, σ1, + fl4HandleVcpuRunExit_nonvppi_stage3A hne_vppi hsp_ts hσ1_ct hsp_cr hcb_sp, hsp_ts, + fl4CompleteRun_drC (specPtr (L.curW s1)) stopSpec w1.bv w2.bv w3.bv σ1 hσ1_ct.symm ((), σ2) + (by rw [hsp_cr]; exact Set.mem_singleton _), J2⟩ + +set_option maxHeartbeats 1000000 in +/-- **§3 VM-exit refinement (VPPI, pre-`syscall.finish`) on `JStage3A`**: +the pre-`syscall.finish` body of `syscall.handle_vcpu_run_vppi` refines +`fl4HandleVcpuRunExit ⟨.vppi, irq_id.bv, 0, 0⟩`. +Zero global `FrameMemRelA`; zero hand-rebuilt records; zero vacuous `SActA σ`. +`hE : CurEntryGA σ` is discharged at kernel entry via `CurEntryGA.of_cur` (as DelPpA). -/ +theorem JStage3A.vcpuRunVppiA {ar : machine.Arch A} (hPl : ArchPlat ar) + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hNoSw : ∀ u, σ.schedulerAction ≠ .switchThread u) + {Clock : A → Fl4SpecA64.MachineState → Prop} + (hNow : NowAgreeA ar Clock) (hNowAR : ∀ a0 h, AR a0 h → ar.now a0 ⦃ r => AR r.2 h ⦄) + (hClock : Clock a σ.machineState) + (hClMask : ∀ a' ms (irq : U64) (b : Bool), Clock a' ms → ar.mask_irq a' irq b ⦃ a'' => Clock a'' ms ⦄) + (hClVm : ∀ s' a' ms (u : U64), Clock a' ms → vspace.set_vm_root st ar s' a' u ⦃ r => Clock r.2 ms ⦄) + (hClFlushState : ∀ a' ms (vc om : U64), Clock a' ms → ar.vcpu_flush_state a' vc om ⦃ a'' => Clock a'' ms ⦄) + (hClFr : ∀ a' ms ms', Clock a' ms → ms'.timeState = ms.timeState → + ms'.lastMachineTime = ms.lastMachineTime → Clock a' ms') + (hnd : numDomains = 1) (hVMT : ValidMachineTimeA σ) (hCS : CounterStepA σ) + (hNowEq : fl4ReadRawCounter σ.machineState = getCurrentTime σ.machineState) + (hE : CurEntryGA σ) + (hP : VcpuPlatAgreeA ar) (hAK : VcpuArchKeepsA ar AR) (hVR : VcpuVmRootA st ar AR) + (hScPost : ∀ (s3 : S) (a3 : A) (hw3 : HwRootA) (σ3 : Fl4SpecA64.State Unit), + JStage3A st AR { Z with hw := hw3 } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s3 a3 σ3 m → + ScStateRelA st Z.C m s3 σ3 ∧ + (∃ xsc nsc, σ3.kheap σ3.curSc = some (.schedContext xsc nsc) ∧ scActive xsc = true) ∧ + sporadic.min_budget ar a3 ⦃ mb => mb.bv = MIN_BUDGET ⦄ ∧ + getScRefillSufficient (Z := Unit) σ3.curSc σ3.consumedTime σ3 = ({(true, σ3)}, False)) + (irq_id : U64) : + (do + let (s1, a1) ← sched.update_timestamp st ar s a + let ct ← sched.cur_thread st s1 + let i ← layout.tcb.RUN_VCPU + let i1 ← lift (wa ct i) + let v ← st.read s1 i1 + let s2 ← if v != 0#u64 then + if irq_id = layout.irq.TIMER_ID then do + let i2 ← lift (wa v vcpu.VPPI_MASKED) + let masked ← st.read s1 i2 + let i3 ← lift (wa v vcpu.VPPI_MASKED) + let i4 ← lift (masked ||| 1#u64) + st.write s1 i3 i4 + else ok s1 + else ok s1 + let i2 ← st.cur_cpu s2 + let i3 ← layout.irq.of irq_id i2 + let a2 ← ar.mask_irq a1 i3 true + let (s3, a3) ← vcpu.complete_run st ar s2 a2 ct fl4_abi.consts.vcpu_run_stop.VPPI irq_id 0#u64 0#u64 + let (_, s4, a4) ← sched.check_budget st ar s3 a3 + ok (s4, a4)) ⦃ res => + ∃ hw' σ' σ1, + fl4HandleVcpuRunExit (Z := Unit) ⟨.vppi, irq_id.bv, 0, 0⟩ σ = ({((), σ')}, False) ∧ + updateTimeStamp (Z := Unit) σ = ({((), σ1)}, False) ∧ DrA σ1 σ' ∧ + JStage3A st AR { Z with hw := hw' } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) res.1 res.2 σ' m ⦄ := by + have hArTs : sched.update_timestamp st ar s a ⦃ r => AR r.2 Z.hw ⦄ := by + have hct : sched.cur_time st s = ok (L.curTime s) := by + unfold sched.cur_time LawfulStore.curTime LawfulStore.nodeA + rw [L.cur_cpu_eq, bind_ok, eq_of_spec_fd (obj.node_get.spec s _ _)] + unfold sched.update_timestamp + rw [hct, bind_ok] + refine WP.spec_bind (hNowAR a Z.hw J.j.1.ar) ?_ + rintro ⟨now, a1⟩ hAR1 + simp only [uncurry_apply_pair] + rw [L.cur_cpu_eq, bind_ok, eq_of_spec_fd (obj.node_set.spec s _ _ _)] + simp only [bind_ok, lift] + refine WP.spec_bind (sched.consumed.specA _) ?_ + rintro _ rfl + refine WP.spec_bind (sched.set_consumed.spec _ _) ?_ + rintro s2 rfl + simp only [layout.NUM_DOMAINS, fl4_abi.config.NUM_DOMAINS, + show ¬ ((1#u64 : U64) > 1#u64) from by decide, ↓reduceIte, WP.spec_ok] + exact hAR1 + refine WP.spec_bind (J.timestampA_vcpu hNoSw hNow hClock hnd hVMT hCS hNowEq hArTs hE) ?_ + rintro ⟨s1, a1⟩ ⟨σ1, hsp_ts, -, J1, hNoSw1, hCl1, -, -⟩ + dsimp only at J1 hCl1 + have hσ1_ct : σ1.curThread = specPtr (L.curW s1) := by + rw [← J1.j.1.sch.cur, J1.j.1.sch.core]; exact J1.j.1.sch.curOfW _ J1.j.1.sch.cpuCs + change (do let ct ← sched.cur_thread st s1 + let i ← layout.tcb.RUN_VCPU + let i1 ← lift (wa ct i) + let v ← st.read s1 i1 + let s2 ← if v != 0#u64 then + if irq_id = layout.irq.TIMER_ID then do + let i2 ← lift (wa v vcpu.VPPI_MASKED) + let masked ← st.read s1 i2 + let i3 ← lift (wa v vcpu.VPPI_MASKED) + let i4 ← lift (masked ||| 1#u64) + st.write s1 i3 i4 + else ok s1 + else ok s1 + let i2 ← st.cur_cpu s2 + let i3 ← layout.irq.of irq_id i2 + let a2 ← ar.mask_irq a1 i3 true + let (s3, a3) ← vcpu.complete_run st ar s2 a2 ct fl4_abi.consts.vcpu_run_stop.VPPI irq_id 0#u64 0#u64 + let (_, s4, a4) ← sched.check_budget st ar s3 a3 + ok (s4, a4)) ⦃ _ ⦄ + rw [show sched.cur_thread st s1 = ok (L.curW s1) from eq_of_spec_fd (sched.cur_thread.spec s1), bind_ok, + eq_of_spec_fd Fl4ProofsA64.P0.Consts.layout.tcb.RUN_VCPU.spec, bind_ok, lift_okE, bind_ok, + show wa (L.curW s1) 3672#u64 = runVcpuA (L.curW s1) from rfl, L.read_eq, bind_ok] + set ct := L.curW s1 + have hct1 : ct ∈ Z.T := J1.sr.curT + obtain ⟨xct1, hkc1, -, hrv1_c⟩ := J1.j.2.1.tcb ct hct1 + have hVppiStep : (if L.mem s1 (runVcpuA ct) != 0#u64 then + if irq_id = layout.irq.TIMER_ID then do + let i2 ← lift (wa (L.mem s1 (runVcpuA ct)) vcpu.VPPI_MASKED) + let masked ← st.read s1 i2 + let i3 ← lift (wa (L.mem s1 (runVcpuA ct)) vcpu.VPPI_MASKED) + let i4 ← lift (masked ||| 1#u64) + st.write s1 i3 i4 + else ok s1 + else ok s1) ⦃ s2 => + ∃ σ2, + (do let vcOpt ← getRunVcpu (Z := Unit) (specPtr ct) + match vcOpt with + | some vc => NonDet.when (irq_id.bv = 27) do + let v ← getVcpu vc + let mk := Function.update v.vcpuVppiMasked .vppiEventIRQ_VTimer true + setVcpu vc { v with vcpuVppiMasked := mk } + | none => pure ()) σ1 = ({((), σ2)}, False) ∧ + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s2 a1 σ2 m ∧ + (∀ u, σ2.schedulerAction ≠ .switchThread u) ∧ + σ2.machineState = σ1.machineState ⦄ := by + by_cases hrv_nz : (L.mem s1 (runVcpuA ct) != 0#u64) = true + · have hrv_ne0 : L.mem s1 (runVcpuA ct) ≠ 0#u64 := bne_iff_ne.1 hrv_nz + rw [if_pos hrv_nz] + set vc := L.mem s1 (runVcpuA ct) + have hvcV : vc ∈ Z.V := (J1.vcRun.run ct hct1).resolve_left hrv_ne0 + have hrv_some : xct1.tcbExt.tcbRunVcpu = some (specPtr vc) := by rw [hrv1_c]; unfold optRefA; rw [if_neg hrv_ne0] + by_cases htm : irq_id = layout.irq.TIMER_ID + · have htm_bv : irq_id.bv = 27 := by rw [htm]; unfold layout.irq.TIMER_ID; rfl + rw [if_pos htm, show vcpu.VPPI_MASKED = 16#u64 from by unfold vcpu.VPPI_MASKED; rfl, + lift_okE, bind_ok, L.read_eq, bind_ok, + lift_okE, bind_ok, bind_ok, L.write_eq, WP.spec_ok] + obtain ⟨v1, hkv1, -, -⟩ := J1.j.2.1.obj vc hvcV + obtain ⟨J2, hNoSw2⟩ := vcpu_mask_vppi_stage3A J1 hNoSw1 hvcV hkv1 + refine ⟨_, ?_, J2, hNoSw2, rfl⟩ + rw [nd_bind_oneSc (getRunVcpu_eqA hkc1), hrv_some]; dsimp only; unfold NonDet.when; rw [if_pos htm_bv] + rw [nd_bind_oneSc (getVcpu_eqA hkv1), setVcpu_eqA hkv1] + · have htm_nbv : ¬ (irq_id.bv = 27) := fun e => htm (u64_eq_of_bv_eqE (by rw [e]; unfold layout.irq.TIMER_ID; rfl)) + rw [if_neg htm, WP.spec_ok] + refine ⟨σ1, ?_, J1, hNoSw1, rfl⟩ + rw [nd_bind_oneSc (getRunVcpu_eqA hkc1), hrv_some]; dsimp only; unfold NonDet.when; rw [if_neg htm_nbv]; rfl + · have hrv_eq0 : L.mem s1 (runVcpuA ct) = 0#u64 := by by_contra hne; exact hrv_nz (bne_iff_ne.2 hne) + rw [if_neg hrv_nz, WP.spec_ok] + have hrv_none : xct1.tcbExt.tcbRunVcpu = none := by rw [hrv1_c, hrv_eq0]; rfl + refine ⟨σ1, ?_, J1, hNoSw1, rfl⟩ + rw [nd_bind_oneSc (getRunVcpu_eqA hkc1), hrv_none]; rfl + refine WP.spec_bind hVppiStep ?_ + rintro s2 ⟨σ2, hsp_vp, J2, hNoSw2, hms2⟩ + rw [L.cur_cpu_eq, bind_ok] + refine WP.spec_bind (layout_irq_of_totalA irq_id (L.cpu s2)) ?_ + intro irq_hw _ + refine WP.spec_bind (WP.spec_and (hAK.mask_irq a1 Z.hw irq_hw true J2.j.1.ar) (hClMask a1 σ1.machineState irq_hw true hCl1)) ?_ + rintro a2 ⟨hAR2, hCl2⟩ + set σ2m := { σ2 with machineState := { σ2.machineState with + irqMasks := Function.update σ2.machineState.irqMasks (ucast irqLen irq_id.bv) true } } + have J2m := jstage3A_of_maskInterruptA J2 hAR2 true (ucast irqLen irq_id.bv) + have hCl2m : Clock a2 σ2m.machineState := hClFr a2 σ1.machineState σ2m.machineState hCl2 + (by change σ2.machineState.timeState = σ1.machineState.timeState; rw [hms2]) + (by change σ2.machineState.lastMachineTime = σ1.machineState.lastMachineTime; rw [hms2]) + have hvppi_bv : fl4_abi.consts.vcpu_run_stop.VPPI.bv = RunStop.vppi.word := by + unfold fl4_abi.consts.vcpu_run_stop.VPPI RunStop.word; rfl + refine WP.spec_bind (vcpu_complete_run_stage3A (Clock := Clock) J2m hNoSw2 hCl2m hClVm hClFlushState hClFr + hP hAK hVR hct1 hvppi_bv irq_id 0#u64 0#u64) ?_ + rintro ⟨s3, a3⟩ ⟨hw3, σ3, hsp_cr, J3, -, -, -, -, -, -, -, -⟩ + obtain ⟨hSc3, ⟨xsc, nsc, hks3, hact3⟩, hmb3, hsuf3⟩ := hScPost s3 a3 hw3 σ3 J3 + have hks3' : σ3.kheap (scNameA m (L.cscW s3)) = some (.schedContext xsc nsc) := by + have hcw : σ3.curSc = scNameA m (L.cscW s3) := J3.j.1.sch.curScW + rw [← hcw]; exact hks3 + refine WP.spec_bind (check_budget_sufficientJ (AR := AR) a3 J3.j.1 hSc3 hks3' hact3 hmb3 hnd hsuf3) ?_ + rintro ⟨b, s4, a4⟩ ⟨heq4, hcb_sp⟩ + have hs4 : s4 = s3 := congrArg (fun t => t.2.1) heq4 + have ha4 : a4 = a3 := congrArg (fun t => t.2.2) heq4 + rw [hs4, ha4] + have K1 : DlA σ1 σ2 := vppiBlock_dlA (specPtr ct) irq_id.bv σ1 ((), σ2) (by rw [hsp_vp]; exact Set.mem_singleton _) + have K2 : DlA σ2 σ2m := dlA_eqA rfl rfl rfl rfl rfl rfl rfl rfl rfl + exact (WP.spec_ok (s3, a3)).2 ⟨hw3, σ3, σ1, + fl4HandleVcpuRunExit_vppi_stage3A hsp_ts hσ1_ct hsp_vp hsp_cr hcb_sp, hsp_ts, + (DlA.toDr (dlA_trans K1 K2)).trans (fl4CompleteRun_drC (specPtr ct) RunStop.vppi irq_id.bv (0#u64).bv (0#u64).bv + σ2m ((K1.cur.trans hσ1_ct).symm) ((), σ3) (by rw [hsp_cr]; exact Set.mem_singleton _)), J3⟩ + +end vcpuExit + +end +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3VcpuExitWireA.lean b/proofs/Fl4ProofsA64/P3/Stage3VcpuExitWireA.lean new file mode 100644 index 000000000..c83ba47e7 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3VcpuExitWireA.lean @@ -0,0 +1,261 @@ +/- +P3 (aarch64, A64-P3 Stage 3, s4w checkpoint c): **the VM-exit guest entries `runExit` / `runVppi` wired into +`kernelEntry_recGA`**. + +`JStage3A.vcpuRunExitA` / `vcpuRunVppiA` (`P3/Stage3VcpuExitA`) conclude the spec equation and `JStage3A` only; they now +also export the time-stamp state `σ1` and `DrA σ1 σ'` (the exit's `complete_run` is a current-thread frame). +This module adds the part `kernelEntry_recGA`'s `hbody` owes: `Sched.BodyOKA` at the exit, hence `A64RelS3`. + +* `VcpuEntryA σ m`: [J] the entry facts `JStage3EntryA` holds besides `SActA` (vacuous for a guest entry): `CurInvA`, + `OffReadyA`, `ActIdleA`, a non-idle current SC is no core's idle SC, and the current thread's state is one + activation accepts (`runningVM`). +* `VcpuExitInA`: the premises of the exit theorems (state/record-level) and [P] `hMin : fl4MinBudget = MIN_BUDGET` + (`SchedGapFreeA.minAgree`'s fact, needed for `OffOKA` at the exit). +* `Sched.BodyOKA.ofExitA`: `BodyOKA` after a time stamp and a `DrA` step. Entry facts at `σ1` come from + `update_timestamp_refinesA` (`tsFactsA`). +* `kBody_runExitA` / `kBody_runVppiA`: `hbody` of `kernelEntry_recGA` for the two bodies, and + `kernelEntry_runExitA` / `kernelEntry_runVppiA`: the entry tops, with `hGap` and `hNat` (the ratified [S4] + `hNative`) as `kernelEntry_recGA`'s. + +`hE : CurEntryGA σ` is `CurEntryGA.of_cur` of `VcpuEntryA`'s `CurInvA` / `OffReadyA` (as `Stage3KEntryBudgetRecA`). +Named premises: none new ([J] entry facts as `JStage3EntryA`'s, [P] `minAgree`). New module. +-/ +import Fl4ProofsA64.P3.Stage3VcpuExitA +import Fl4ProofsA64.P3.Stage3SysTopA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4ProofsA64.P3.Vcpu Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} {ia ie : Nat} + {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {s : S} {a : A} {σ σ1 σ' : State Unit} {m : McState} {Cs : Set U64} {Clock : A → MachineState → Prop} + +/-- [J] **The entry facts every entry kind shares**: `JStage3EntryA`'s fields without `offOK`, `validTime` and `sact` +(`JStage3EntryA.toBase`). -/ +structure EntryBaseA (σ : State Unit) (m : McState) : Prop where + curInv : CurInvA σ + offReady : OffReadyA σ + actIdle : ActIdleA σ + curScNotIdle : σ.curSc ≠ idleScPtr → NotIdleScA m σ.curSc + +/-- The shared entry facts of a unified entry record. -/ +theorem JStage3EntryA.toBase {J5 J7 : JSlotA S} {n : Nat} + (J : JStage3EntryA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) : EntryBaseA σ m := + ⟨J.curInv, J.offReady, J.actIdle, J.curScNotIdle⟩ + +/-- [J] **The entry facts of a guest entry** (module docstring): `EntryBaseA` and the activation-legal state. -/ +structure VcpuEntryA (σ : State Unit) (m : McState) : Prop extends EntryBaseA σ m where + ctAct : ∀ x, σ.kheap σ.curThread = some (.tcb x) → ActivateStateOKA x.tcbState + +/-- **The premises of the VM-exit bodies** (module docstring): those of `JStage3A.vcpuRunExitA` / `vcpuRunVppiA`. -/ +structure VcpuExitInA (st : machine.Store S) [L : LawfulStore st] (ar : machine.Arch A) (AR : A → HwRootA → Prop) + (Z : XSetsA) (H : Heap) (OS U Ac Pp Tb : Set U64) (Lv : U64 → Nat) (ia ie : Nat) (N E Rp : Set U64) + (Bf : Set (U64 × Fl4SpecA64.Word)) (stack : U64 → List U64) (κ : U64 → U64) (nA : U64) (m : McState) + (Clock : A → MachineState → Prop) (s : S) (a : A) (σ : State Unit) : Prop where + en : VcpuEntryA σ m + hNoSw : ∀ u, σ.schedulerAction ≠ .switchThread u + hNow : NowAgreeA ar Clock + hNowAR : ∀ a0 h, AR a0 h → ar.now a0 ⦃ r => AR r.2 h ⦄ + hClock : Clock a σ.machineState + hClVm : ∀ s' a' ms (u : U64), Clock a' ms → vspace.set_vm_root st ar s' a' u ⦃ r => Clock r.2 ms ⦄ + hClFlushState : ∀ a' ms (vc om : U64), Clock a' ms → ar.vcpu_flush_state a' vc om ⦃ a'' => Clock a'' ms ⦄ + hClFr : ∀ a' ms ms', Clock a' ms → ms'.timeState = ms.timeState → + ms'.lastMachineTime = ms.lastMachineTime → Clock a' ms' + hnd : numDomains = 1 + hVMT : ValidMachineTimeA σ + hCS : CounterStepA σ + hNowEq : fl4ReadRawCounter σ.machineState = getCurrentTime σ.machineState + hP : VcpuPlatAgreeA ar + hAK : VcpuArchKeepsA ar AR + hVR : VcpuVmRootA st ar AR + hMin : fl4MinBudget = MIN_BUDGET + hScPost : ∀ (s2 : S) (a2 : A) (hw2 : HwRootA) (σ2 : State Unit), + JStage3A st AR { Z with hw := hw2 } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar nA)) s2 a2 σ2 m → + ScStateRelA st Z.C m s2 σ2 ∧ + (∃ xsc nsc, σ2.kheap σ2.curSc = some (.schedContext xsc nsc) ∧ scActive xsc = true) ∧ + sporadic.min_budget ar a2 ⦃ mb => mb.bv = MIN_BUDGET ⦄ ∧ + getScRefillSufficient (Z := Unit) σ2.curSc σ2.consumedTime σ2 = ({(true, σ2)}, False) + +/-- **The time-stamp state's facts** (`update_timestamp_refinesA` at the entry record, `σ1` by determinism). -/ +theorem tsFactsA {nA : U64} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar nA)) s a σ m) + (I : VcpuExitInA st ar AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ nA m Clock s a σ) + (hts : updateTimeStamp (Z := Unit) σ = ({((), σ1)}, False)) : + ValidMachineTimeA σ1 ∧ CurInvA σ1 ∧ OffReadyA σ1 ∧ σ1.kheap = σ.kheap ∧ σ1.curThread = σ.curThread ∧ + σ1.curSc = σ.curSc ∧ σ1.schedulerAction = σ.schedulerAction := by + have E0 : YieldQueueRelA (st := st) Z m κ s σ := ⟨J.sr, fun _ ht _ hx => (J.ep.at_tcb ht hx).st, J.mcp, J.yieldRel⟩ + obtain ⟨v, -, τ, hτ, -, -, V1, CI1, R1, -, -, -, -⟩ := + spec_ex_fd (update_timestamp_refinesA E0 a I.hNow I.hClock I.hnd I.hVMT I.hCS I.en.curInv I.en.offReady I.en.actIdle) + have eτ := fl4UpdateTimestamp_eqA σ + rw [hτ] at eτ + have eτ' : τ = fl4TimestampState (rawNextA σ.machineState) { σ with machineState := rawStateA σ.machineState } := + (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst eτ))).2 + rw [fl4UpdateTimestamp_eq_l4vA σ I.hnd I.hNowEq, hts] at hτ + have e1 : σ1 = τ := (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst hτ))).2 + subst e1 + exact ⟨V1, CI1, R1, by rw [eτ']; rfl, by rw [eτ']; rfl, by rw [eτ']; rfl, by rw [eτ']; rfl⟩ + +/-- **The body-end facts after a time stamp and a `DrA` step** (module docstring). -/ +theorem Sched.BodyOKA.ofExitA {nA : U64} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar nA)) s a σ m) + (En : VcpuEntryA σ m) (hNoSw : ∀ u, σ.schedulerAction ≠ .switchThread u) + (V1 : ValidMachineTimeA σ1) (CI1 : CurInvA σ1) (R1 : OffReadyA σ1) (hkh : σ1.kheap = σ.kheap) + (hcur : σ1.curThread = σ.curThread) (hcsc : σ1.curSc = σ.curSc) + (hact : σ1.schedulerAction = σ.schedulerAction) (K : DrA σ1 σ') (hMin : fl4MinBudget = MIN_BUDGET) + (hsc : ∃ xsc nsc, σ'.kheap σ'.curSc = some (.schedContext xsc nsc) ∧ scActive xsc = true) + (hsuf : getScRefillSufficient (Z := Unit) σ'.curSc σ'.consumedTime σ' = ({(true, σ')}, False)) : + BodyOKA σ' m := by + have hk1 : ∀ y n, σ'.kheap σ'.curSc = some (.schedContext y n) → σ.kheap σ.curSc = some (.schedContext y n) := + fun y n hk => by + have := K.sc _ y n hk + rw [K.csc, hkh, hcsc] at this + exact this + have hOR : OffReadyA σ' := fun y n hk => by + have := R1 y n (by rw [hkh, hcsc]; exact hk1 y n hk) + rw [K.cons, K.time]; exact this + have hs : ∀ y n, σ'.kheap σ'.curSc = some (.schedContext y n) → + fl4MinBudget ≤ refillCapacity σ'.consumedTime (refillHd y) := fun y n hk => by + obtain ⟨xsc, nsc, hks, hax⟩ := hsc + rw [hk] at hks + have hy : y = xsc := (KernelObject.schedContext.inj (Option.some.inj hks)).1 + have hne := J.sinv.rpos _ y n (hk1 y n hk) (by rw [hy]; exact hax) + rw [getScRefillSufficient_eqA hk hne _] at hsuf + have h := (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst hsuf))).1 + rw [hMin] + exact of_decide_eq_true h + refine ⟨⟨?_, fun y n hk _ _ => ⟨hOR, OffReadyA.ok hOR hs⟩, fun _ x hx => ?_⟩, fun t ht => ?_, fun h => ?_, + fun y n hk _ _ _ o ho => ?_⟩ + · unfold ValidMachineTimeA at V1 ⊢ + rw [K.time, K.last]; exact V1 + · rw [K.cur] at hx + obtain ⟨x0, hx0, e⟩ := K.ct x hx + have h0 := En.ctAct x0 (by rwa [hkh, hcur] at hx0) + rcases e with e | e | e + · rw [e]; exact h0 + · rw [e]; exact Or.inl rfl + · rw [e]; exact Or.inr (Or.inr (Or.inr rfl)) + · rcases K.act with e | e + · rw [e, hact] at ht; exact absurd ht (hNoSw t) + · rw [e] at ht; cases ht + · rw [K.csc, hcsc] at h ⊢; exact En.curScNotIdle h + · obtain ⟨t, ht, hts, -⟩ := CI1.tcb + obtain ⟨z, j, hz, hzt⟩ := J.sr.inv.back _ t _ (by rwa [hkh, hcur] at ht) (by rwa [hcsc] at hts) + rw [hk1 y n hk] at hz + obtain ⟨rfl, -⟩ := KernelObject.schedContext.inj (Option.some.inj hz) + rw [hzt] at ho + rw [← Option.some.inj ho, K.rel, ← hcur] + exact CI1.notRel + +/-- **The record at a VM exit's end** from the exit theorems' conclusion. -/ +theorem exitRelA {nA : U64} {hw' : HwRootA} {s' : S} {a' : A} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar nA)) s a σ m) + (I : VcpuExitInA st ar AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ nA m Clock s a σ) (hCs : Z.Cs = Cs) + (hts : updateTimeStamp (Z := Unit) σ = ({((), σ1)}, False)) (K : DrA σ1 σ') + (J' : JStage3A st AR { Z with hw := hw' } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes + (J5A st) (J7A st (J7R2A st ar nA)) s' a' σ' m) : + A64RelS3 st AR ar nA fl4A64Platform.numNodes Cs s' a' σ' m := by + obtain ⟨V1, CI1, R1, hkh, hcur, hcsc, hact⟩ := tsFactsA J I hts + obtain ⟨-, hsc, -, hsuf⟩ := I.hScPost s' a' hw' σ' J' + exact ⟨{ Z with hw := hw' }, H, OS, U, Ac, Pp, Tb, Lv, ia, ie, N, E, Rp, Bf, stack, κ, hCs, J', + Sched.BodyOKA.ofExitA J I.en I.hNoSw V1 CI1 R1 hkh hcur hcsc hact K I.hMin hsc hsuf⟩ + +/-- The guest entry `runExit`. -/ +abbrev kRunExitE (stop w1 w2 w3 : U64) (stopSpec : RunStop) : KEventA := + .guest (.runExit stop w1 w2 w3) (fl4HandleVcpuRunExit (Z := Unit) ⟨stopSpec, w1.bv, w2.bv, w3.bv⟩) + +/-- The guest entry `runVppi`. -/ +abbrev kRunVppiE (irq : U64) : KEventA := + .guest (.runVppi irq) (fl4HandleVcpuRunExit (Z := Unit) ⟨.vppi, irq.bv, 0, 0⟩) + +/-- **`hbody` of `kernelEntry_recGA` for `runExit`** (module docstring). -/ +theorem kBody_runExitA (hPl : ArchPlat ar) + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (I : VcpuExitInA st ar AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ hPl.ipa m Clock s a σ) (hCs : Z.Cs = Cs) + {stop : U64} {stopSpec : RunStop} (hstop : stop.bv = stopSpec.word) (hne : stopSpec ≠ .vppi) (w1 w2 w3 : U64) : + kBodyA st ar s a (kRunExitE stop w1 w2 w3 stopSpec) ⦃ r => StepPostA (fun s' a' τ => + A64RelS3 st AR ar hPl.ipa fl4A64Platform.numNodes Cs s' a' τ m) (kOpA (kRunExitE stop w1 w2 w3 stopSpec)) σ r ⦄ := by + have hE := CurEntryGA.of_cur I.en.curInv I.en.offReady J.sr.inv + refine WP.spec_mono (JStage3A.vcpuRunExitA hPl J I.hNoSw I.hNow I.hNowAR I.hClock I.hClVm I.hClFlushState I.hClFr + I.hnd I.hVMT I.hCS I.hNowEq hE I.hP I.hAK I.hVR I.hScPost hstop hne w1 w2 w3) ?_ + rintro ⟨s', a'⟩ ⟨hw', σ', σ1, hspec, hts, K, J'⟩ + have hm : ((), σ') ∈ (fl4HandleVcpuRunExit (Z := Unit) ⟨stopSpec, w1.bv, w2.bv, w3.bv⟩ σ).1 := by + rw [hspec]; exact Set.mem_singleton _ + exact ⟨σ', hm, exitRelA J I hCs hts K J'⟩ + +/-- **`hbody` of `kernelEntry_recGA` for `runVppi`** (module docstring). -/ +theorem kBody_runVppiA (hPl : ArchPlat ar) + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (I : VcpuExitInA st ar AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ hPl.ipa m Clock s a σ) (hCs : Z.Cs = Cs) + (hClMask : ∀ a' ms (irq : U64) (b : Bool), Clock a' ms → ar.mask_irq a' irq b ⦃ a'' => Clock a'' ms ⦄) + (irq : U64) : + kBodyA st ar s a (kRunVppiE irq) ⦃ r => StepPostA (fun s' a' τ => + A64RelS3 st AR ar hPl.ipa fl4A64Platform.numNodes Cs s' a' τ m) (kOpA (kRunVppiE irq)) σ r ⦄ := by + have hE := CurEntryGA.of_cur I.en.curInv I.en.offReady J.sr.inv + refine WP.spec_mono (JStage3A.vcpuRunVppiA hPl J I.hNoSw I.hNow I.hNowAR I.hClock hClMask I.hClVm I.hClFlushState + I.hClFr I.hnd I.hVMT I.hCS I.hNowEq hE I.hP I.hAK I.hVR I.hScPost irq) ?_ + rintro ⟨s', a'⟩ ⟨hw', σ', σ1, hspec, hts, K, J'⟩ + have hm : ((), σ') ∈ (fl4HandleVcpuRunExit (Z := Unit) ⟨.vppi, irq.bv, 0, 0⟩ σ).1 := by + rw [hspec]; exact Set.mem_singleton _ + exact ⟨σ', hm, exitRelA J I hCs hts K J'⟩ + +/-- **The `runExit` entry at the unified top** (module docstring): `kernelEntry_recGA` with `hbody` from +`kBody_runExitA`; `hGap` and `hNat` (the ratified [S4] `hNative`) are the top's. -/ +theorem kernelEntry_runExitA {tm : A → Nat → DeadlineViewA} {view : State Unit → Fl4TimerView} + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) (arch : ArchSwFA st ar) + (hCs : ∀ c ∈ Cs, c.val < fl4A64Platform.numNodes) (P : FinPlatA ar AR) + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (I : VcpuExitInA st ar AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ hPl.ipa m Clock s a σ) (hZ : Z.Cs = Cs) + {stop : U64} {stopSpec : RunStop} (hstop : stop.bv = stopSpec.word) (hne : stopSpec ≠ .vppi) (w1 w2 w3 : U64) + (hGap : ∀ s' a' τ, kBodyA st ar s a (kRunExitE stop w1 w2 w3 stopSpec) = ok (s', a') → + ((), τ) ∈ (kOpA (kRunExitE stop w1 w2 w3 stopSpec) σ).1 → + A64RelS3 st AR ar hPl.ipa fl4A64Platform.numNodes Cs s' a' τ m → SchedGapFreeA st m s' a' τ ar tm view) + (hNat : ∀ s' a' τ, kBodyA st ar s a (kRunExitE stop w1 w2 w3 stopSpec) = ok (s', a') → + ((), τ) ∈ (kOpA (kRunExitE stop w1 w2 w3 stopSpec) σ).1 → + A64RelS3 st AR ar hPl.ipa fl4A64Platform.numNodes Cs s' a' τ m → ∀ τ1 τ2, + ((), τ1) ∈ (fl4Schedule kLimitA view τ).1 → ((), τ2) ∈ (fl4ActivateThread (Z := Unit) τ1).1 → + ∀ y, τ2.kheap τ2.curThread = some (.tcb y) → y.tcbState ≠ .ext .runningVM) : + kCodeA st ar s a (kRunExitE stop w1 w2 w3 stopSpec) ⦃ out => + StepPostA (KExitA st ar AR hPl.ipa m) + (kOpA (kRunExitE stop w1 w2 w3 stopSpec) >>= fun _ => fl4Finish kLimitA view) σ out ⦄ := + kernelEntry_recGA hA hPl arch hCs P s a σ (kRunExitE stop w1 w2 w3 stopSpec) (show True from trivial) + (kBody_runExitA hPl J I hZ hstop hne w1 w2 w3) + hGap hNat + +/-- **The `runVppi` entry at the unified top** (module docstring): as `kernelEntry_runExitA`. -/ +theorem kernelEntry_runVppiA {tm : A → Nat → DeadlineViewA} {view : State Unit → Fl4TimerView} + (hA : P0.ArchTotal ar) (hPl : ArchPlat ar) (arch : ArchSwFA st ar) + (hCs : ∀ c ∈ Cs, c.val < fl4A64Platform.numNodes) (P : FinPlatA ar AR) + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (I : VcpuExitInA st ar AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ hPl.ipa m Clock s a σ) (hZ : Z.Cs = Cs) + (hClMask : ∀ a' ms (irq : U64) (b : Bool), Clock a' ms → ar.mask_irq a' irq b ⦃ a'' => Clock a'' ms ⦄) + (irq : U64) + (hGap : ∀ s' a' τ, kBodyA st ar s a (kRunVppiE irq) = ok (s', a') → ((), τ) ∈ (kOpA (kRunVppiE irq) σ).1 → + A64RelS3 st AR ar hPl.ipa fl4A64Platform.numNodes Cs s' a' τ m → SchedGapFreeA st m s' a' τ ar tm view) + (hNat : ∀ s' a' τ, kBodyA st ar s a (kRunVppiE irq) = ok (s', a') → ((), τ) ∈ (kOpA (kRunVppiE irq) σ).1 → + A64RelS3 st AR ar hPl.ipa fl4A64Platform.numNodes Cs s' a' τ m → ∀ τ1 τ2, + ((), τ1) ∈ (fl4Schedule kLimitA view τ).1 → ((), τ2) ∈ (fl4ActivateThread (Z := Unit) τ1).1 → + ∀ y, τ2.kheap τ2.curThread = some (.tcb y) → y.tcbState ≠ .ext .runningVM) : + kCodeA st ar s a (kRunVppiE irq) ⦃ out => + StepPostA (KExitA st ar AR hPl.ipa m) (kOpA (kRunVppiE irq) >>= fun _ => fl4Finish kLimitA view) σ out ⦄ := + kernelEntry_recGA hA hPl arch hCs P s a σ (kRunVppiE irq) (show True from trivial) + (kBody_runVppiA hPl J I hZ hClMask irq) hGap hNat + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3VcpuKeepA.lean b/proofs/Fl4ProofsA64/P3/Stage3VcpuKeepA.lean new file mode 100644 index 000000000..ad1cac5a1 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3VcpuKeepA.lean @@ -0,0 +1,193 @@ +/- +P3 (aarch64, A64-P3 Stage 3, s4w checkpoint a): **the body-end facts of the plain VCPU invocations** +(`ReadState`, `WriteState`, `ReadReg`, `WriteReg`, `InjectIRQ`, `AckVPPI`; `SetTCB` and `Run` are checkpoint b). + +`fl4PerformVcpuInv` of a plain invocation (`VcpuPlainA`) is a `DlA` step (`P3/Stage3DelFinVsA`: the current thread / SC, +the time fields, the release queue, the scheduler action, every TCB state and every SC object are kept): its writes are +VCPU objects, frame words (`storeWordOffs`), thread contexts (`asUser`), `armCurrentVcpu` and machine registers, and +`vcpuFlush` (the machine operations keep `lastMachineTime`, `P3/Stage3DelFinVcpuA`). + +* `fl4WriteStateFromFrame_dlA`, `fl4PerformVcpuInv_plain_dlA`: the frames. +* `BodyOKA.ofVcpuPlain`: the body-end facts after `Restart`, a plain perform and the spec's reply (`replyFromKernel` + when the invocation is a call, then the caller `Running`): the caller is `Running` and the scheduler action is kept + or `choose_new_thread` (`set_thread_state`'s own). + +Named premises: none. New module. +-/ +import Fl4ProofsA64.P3.Stage3DelFinScA +import Fl4ProofsA64.P3.Stage3T3ArmsVcpuRunA +import Fl4ProofsA64.P3.Stage3LiftBodyA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4Spec Fl4SpecA64 + +noncomputable section + +theorem loadWord_mlkA (p : Fl4SpecA64.Word) : MlkA (loadWord p) := by unfold loadWord; mlk + +theorem loadWordOffs_dlA (p : ObjRef) (k : Nat) : NdDlA (loadWordOffs (Z := Unit) p k) := by + unfold loadWordOffs; exact NdDlA.machine (loadWord_mlkA _) + +theorem fl4VcpuSet_dlA (vc : ObjRef) (i : Nat) (w : Fl4SpecA64.Word) : NdDlA (fl4VcpuSet (Z := Unit) vc i w) := by + unfold fl4VcpuSet; exact NdDlA.bind (getVcpu_dlA _) fun _ => setVcpu_dlA _ _ + +theorem copyRegsFromFrame_dlA (vc fr : ObjRef) (rs : List Nat) : + NdDlA (copyRegsFromFrame (Z := Unit) vc fr rs) := by + unfold copyRegsFromFrame + exact NdDlA.mapMx (fun _ => NdDlA.bind (loadWordOffs_dlA _ _) fun _ => fl4VcpuSet_dlA _ _ _) _ + +macro "vcp_auto" : tactic => `(tactic| repeat' first + | with_reducible exact copyRegsFromFrame_dlA _ _ _ | with_reducible exact loadWordOffs_dlA _ _ + | with_reducible exact fl4VcpuSet_dlA _ _ _ + | with_reducible exact fl4VcpuFlush_dlA _ | with_reducible exact fl4VcpuGet_dlA _ _ + | with_reducible exact fl4ReadStateToFrame_dlA _ _ _ + | with_reducible exact vcpuUpdate_dlA _ _ + | with_reducible exact getVcpu_dlA _ | with_reducible exact setVcpu_dlA _ _ | with_reducible exact asUser_dlA _ _ + | with_reducible exact NdDlA.pure _ | with_reducible exact NdDlA.npure _ + | with_reducible refine NdDlA.mapMx (fun _ => ?_) _ | with_reducible refine NdDlA.when ?_ + | with_reducible refine NdDlA.bind ?_ (fun _ => ?_) | dsimp only | split) + +set_option maxHeartbeats 1000000 in +theorem fl4WriteStateFromFrame_dlA (vc fr : ObjRef) (m : Fl4SpecA64.Word) (vg : Fl4SpecA64.Word × List Fl4SpecA64.Word) : + NdDlA (fl4WriteStateFromFrame (Z := Unit) vc fr m vg) := by + unfold fl4WriteStateFromFrame + vcp_auto + +/-- **The plain VCPU performs are `DlA` steps.** -/ +theorem fl4PerformVcpuInv_plain_dlA {i : Fl4VcpuInvocation} (h : VcpuPlainA i) : + NdDlA (fl4PerformVcpuInv (Z := Unit) i) := by + cases i with + | setTcb _ _ => exact h.elim + | run _ _ _ _ _ _ _ => exact h.elim + | stateTransfer vc fr m toFrame vg => + unfold fl4PerformVcpuInv + vcp_auto + exact fl4WriteStateFromFrame_dlA _ _ _ _ + | readReg vc field => unfold fl4PerformVcpuInv; vcp_auto + | writeReg vc field val => unfold fl4PerformVcpuInv; vcp_auto + | injectIrq vc index lr => unfold fl4PerformVcpuInv; vcp_auto + | ackVppi vc => unfold fl4PerformVcpuInv; vcp_auto + +theorem fl4ArchPerform_vcpuPlain_dlA {i : Fl4VcpuInvocation} (h : VcpuPlainA i) : + NdDlA (fl4ArchPerformInvocation (Z := Unit) (.vcpu i)).run := + NdDlA.liftE (fl4PerformVcpuInv_plain_dlA h) + +theorem DlA.toKeep {σ σ' : State Unit} (h : DlA σ σ') : Sched.BodyKeepA σ σ' := + ⟨h.act, h.time, h.last, h.cur, h.csc, h.cons, h.rel, h.sc, h.tcb⟩ + +section +variable {S A : Type} {st : machine.Store S} [L : LawfulStore st] +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s : S} {a : A} {σ σ4 σ3 : State Unit} {m : McState} + +/-- **The body-end facts after a `DlA` step and `set_thread_state ct Running`** on the current thread. -/ +theorem Sched.BodyOKA.ofDlRunning + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {x : Tcb} (hk : σ.kheap σ.curThread = some (.tcb x)) + (K : DlA (stUpdA σ σ.curThread x .restart) σ4) + (hr : ((), σ3) ∈ (Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .running σ4).1) : + Sched.BodyOKA σ3 m ∧ σ3.curThread = σ.curThread := by + have ne : σ.curSc ≠ σ.curThread := fun e => by + obtain ⟨y, j, hy'⟩ := J.curInv.sc + rw [e, hk] at hy'; cases hy' + unfold Fl4SpecA64.setThreadState at hr + obtain ⟨a1, ha1, h2⟩ := mem_bindA.1 hr + obtain ⟨y, hy, ea⟩ := threadSet_memA _ _ ha1 + unfold Fl4SpecA64.setThreadStateAct at h2 + obtain ⟨d, hd, h2⟩ := mem_bindA.1 h2 + obtain rfl : d = (a1.2.curThread, a1.2) := hd + obtain ⟨e, he, h2⟩ := mem_bindA.1 h2 + obtain rfl : e = (a1.2.schedulerAction, a1.2) := he + obtain ⟨f, hf, h2⟩ := mem_bindA.1 h2 + obtain rfl : f = (schedulable σ.curThread a1.2, a1.2) := hf + have key : σ3 = a1.2 ∨ σ3 = { a1.2 with schedulerAction := .chooseNewThread } := by + unfold NonDet.when at h2 + split at h2 + · right + unfold setSchedulerAction at h2 + have h3 : ((), σ3) = ((), { a1.2 with schedulerAction := .chooseNewThread }) := Set.mem_singleton_iff.1 h2 + exact (Prod.mk.inj h3).2 + · left + have h3 : ((), σ3) = ((), a1.2) := Set.mem_singleton_iff.1 h2 + exact (Prod.mk.inj h3).2 + have kh : σ3.kheap = Function.update σ4.kheap σ.curThread (some (.tcb { y with tcbState := .running })) := by + rcases key with h | h <;> rw [h, ea] + have hf : σ3.curTime = σ4.curTime ∧ σ3.machineState = σ4.machineState ∧ σ3.curThread = σ4.curThread ∧ + σ3.curSc = σ4.curSc ∧ σ3.consumedTime = σ4.consumedTime ∧ σ3.releaseQueue = σ4.releaseQueue := by + rcases key with h | h <;> rw [h, ea] <;> exact ⟨rfl, rfl, rfl, rfl, rfl, rfl⟩ + have ha : σ3.schedulerAction = σ4.schedulerAction ∨ σ3.schedulerAction = .chooseNewThread := by + rcases key with h | h + · left; rw [h, ea] + · right; rw [h] + have kt : σ4.curTime = σ.curTime := K.time + have kl : σ4.machineState.lastMachineTime = σ.machineState.lastMachineTime := K.last + have kc : σ4.curThread = σ.curThread := K.cur + have ks : σ4.curSc = σ.curSc := K.csc + have kn : σ4.consumedTime = σ.consumedTime := K.cons + have kr : σ4.releaseQueue = σ.releaseQueue := K.rel + have kA : σ4.schedulerAction = σ.schedulerAction := K.act + refine ⟨?_, hf.2.2.1.trans kc⟩ + refine Sched.BodyOKA.ofEntry J ?_ (hf.1.trans kt) ?_ (hf.2.2.1.trans kc) (hf.2.2.2.1.trans ks) + (hf.2.2.2.2.1.trans kn) (hf.2.2.2.2.2.trans kr) (fun y' k hy => ?_) (fun _ x' hx' => ?_) + · rcases ha with h | h + · exact Or.inl (h.trans kA) + · exact Or.inr h + · rw [hf.2.1]; exact kl + · rw [kh, Function.update_of_ne ne] at hy + have h1 := K.sc _ _ _ hy + simp only [stUpdA, setObjA, Function.update_of_ne ne] at h1 + exact ⟨y', h1, rfl, rfl, rfl⟩ + · rw [kh, Function.update_self] at hx' + obtain rfl := KernelObject.tcb.inj (Option.some.inj hx') + exact Or.inl rfl + +/-- **The body-end facts after `Restart`, a plain VCPU perform and the spec's reply** (module docstring). -/ +theorem Sched.BodyOKA.ofVcpuPlain + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {x : Tcb} (hk : σ.kheap σ.curThread = some (.tcb x)) {inv : Fl4VcpuInvocation} (hpl : VcpuPlainA inv) + {call : Bool} + (h : ((.ok [], σ3) ∈ ((fl4ArchPerformInvocation (Z := Unit) (.vcpu inv)).run + (stUpdA σ σ.curThread x .restart)).1) ∨ + (∃ σ2 ws, + (.ok ws, σ2) ∈ ((fl4ArchPerformInvocation (Z := Unit) (.vcpu inv)).run + (stUpdA σ σ.curThread x .restart)).1 ∧ + ((), σ3) ∈ ((do + Fl4Spec.NonDet.when call (Fl4SpecA64.replyFromKernel (Z := Unit) σ.curThread (0, ws)) + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .running) σ2).1)) : + Sched.BodyOKA σ3 m ∧ σ3.curThread = σ.curThread := by + rcases h with h | ⟨σ2, ws, hp, hr⟩ + · have K := fl4ArchPerform_vcpuPlain_dlA hpl _ _ h + exact ⟨Sched.BodyOKA.ofKeep J hk K.toKeep, K.cur⟩ + · have K := fl4ArchPerform_vcpuPlain_dlA hpl _ _ hp + obtain ⟨y, hy, h2⟩ := mem_bindA.1 hr + have K2 := NdDlA.when (inst := inferInstance) (Sched.replyFromKernel_dlA (σ.curThread) (0, ws)) σ2 y hy + exact Sched.BodyOKA.ofDlRunning J hk (dlA_trans K K2) h2 + +/-- **The caller's TCB at the state after a plain perform with a reply is `Restart`** (what the spec's epilogue +`epiSpecA` reads). -/ +theorem vcpuPlain_restartAt {σ σ2 σ3 : State Unit} {x : Tcb} (hk : σ.kheap σ.curThread = some (.tcb x)) + {inv : Fl4VcpuInvocation} (hpl : VcpuPlainA inv) {call : Bool} {ws : List Fl4SpecA64.Word} + (hp : (.ok ws, σ2) ∈ ((fl4ArchPerformInvocation (Z := Unit) (.vcpu inv)).run + (stUpdA σ σ.curThread x .restart)).1) + (hr : ((), σ3) ∈ ((do + Fl4Spec.NonDet.when call (Fl4SpecA64.replyFromKernel (Z := Unit) σ.curThread (0, ws)) + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .running) σ2).1) : + ∃ x2, σ2.kheap σ.curThread = some (.tcb x2) ∧ x2.tcbState = .restart := by + have K := fl4ArchPerform_vcpuPlain_dlA hpl _ _ hp + obtain ⟨y, hy, h2⟩ := mem_bindA.1 hr + have K2 := NdDlA.when (inst := inferInstance) (Sched.replyFromKernel_dlA (σ.curThread) (0, ws)) σ2 y hy + unfold Fl4SpecA64.setThreadState at h2 + obtain ⟨a1, ha1, -⟩ := mem_bindA.1 h2 + obtain ⟨xy, hxy, -⟩ := threadSet_memA _ _ ha1 + obtain ⟨x2, hx2, -⟩ := K2.tcb _ _ hxy + obtain ⟨x0, hx0, e⟩ := K.tcb _ _ hx2 + simp only [stUpdA, setObjA, Function.update_self, Option.some.injEq, KernelObject.tcb.injEq] at hx0 + exact ⟨x2, hx2, by rw [e, ← hx0]⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3VcpuRunA.lean b/proofs/Fl4ProofsA64/P3/Stage3VcpuRunA.lean new file mode 100644 index 000000000..18006bddd --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3VcpuRunA.lean @@ -0,0 +1,443 @@ +/- +P3 (aarch64, A64-P3 Stage 3, s4w checkpoint b): **the body-end facts of `ARMVCPUSetTCB` (60) and `ARMVCPURun` (69)**. + +`DrA σ σ'`: the frame of a VCPU step that may change the *current thread's* state to `Running` or `runningVM` (a run +parks its caller in `runningVM`, and `vcpu::check` / `complete_run` resume it) and may leave the scheduler action at +`choose_new_thread` (`set_thread_state` on the current thread). Other threads' states are not tracked: at a syscall +entry the scheduler action is `resume_cur_thread` (`JSyscallA.act`), so `SwA` holds at the end by the action alone +(`BodyOKA.ofEntryA`), and `BodyOKA` reads no other thread's state. `DlA` and `DcA` (`P3/Stage3DelFinVcpuA`) are `DrA` +steps (`DlA.toDr`, `DcA.toDr`). + +* `SetTCB` (60) is a `DcA` step (`fl4VcpuSetTcbPre`: `fl4VcpuEndRun` / `fl4CompleteRun` of other threads only, the caller + being `Restart`) followed by `DlA` steps (`fl4AssociateVcpuTcb`). +* `Run` (69) is a `DrA` step: `set_thread_state ct runningVM`, then `vcpu::check` (which may `complete_run` the caller). +* `BodyOKA.ofDr`: the body-end facts after `Restart` and a `DrA` step. + +Named premises: none. New module. +-/ +import Fl4ProofsA64.P3.Stage3VcpuKeepA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4Spec Fl4SpecA64 + +noncomputable section + +/-! ## Notification accessors and `liftE` -/ + +theorem getNotification_dlA (p : ObjRef) : NdDlA (getNotification (Z := Unit) p) := by + show NdDlA (getSimpleKo (Z := Unit) KernelObject.notification p) + unfold getSimpleKo getObject + vc_auto + +theorem setNotification_dlA (p : ObjRef) (a : Notification) : NdDlA (setNotification (Z := Unit) p a) := by + show NdDlA (setSimpleKo (Z := Unit) KernelObject.notification p a) + unfold setSimpleKo getObject + refine NdDlA.bind (NdDlA.getsThe _) fun _ => NdDlA.bind (NdDlA.assert _) fun _ => NdDlA.bind (NdDlA.assert _) fun _ => ?_ + exact setObject_dlA _ _ (fun _ h => nomatch h) (fun _ _ h => nomatch h) (fun _ h => nomatch h) + +theorem liftE_run_memA {ε α : Type} {m : NonDet (State Unit) α} {σ σ' : State Unit} {ws : α} + (h : (.ok ws, σ') ∈ ((NonDetE.liftE m : NonDetE (State Unit) ε α).run σ).1) : (ws, σ') ∈ (m σ).1 := by + unfold NonDetE.liftE at h + rw [ExceptT.run_lift] at h + obtain ⟨y, hy, h2⟩ := mem_bindA.1 h + have h3 : ((Except.ok ws : Except ε α), σ') = (Except.ok y.1, y.2) := h2 + obtain ⟨e1, e2⟩ := Prod.mk.inj h3 + have e1' := Except.ok.inj e1 + subst e1' + subst e2 + exact hy + +/-! ## `SetTCB` -/ + +theorem archThreadSet_dlA (f : ArchTcb → ArchTcb) (t : ObjRef) : NdDlA (archThreadSet (Z := Unit) f t) := by + intro σ r hr + unfold archThreadSet at hr + obtain ⟨y, hy, hr⟩ := mem_bindA.1 hr + change y ∈ (NonDet.assertOpt (getTcb (Z := Unit) t σ) σ).1 at hy + cases hg : getTcb (Z := Unit) t σ with + | none => rw [hg] at hy; exact absurd hy (Set.notMem_empty _) + | some x => + rw [hg] at hy + obtain rfl : y = (x, σ) := hy + obtain ⟨ko, -, e⟩ := Sched.setObject_memA hr + rw [e]; exact dlA_updTcbA (Sched.getTcb_kheapA hg) rfl rfl rfl + +theorem fl4DissociateVcpuTcb_dlA (v t : ObjRef) : NdDlA (fl4DissociateVcpuTcb (Z := Unit) v t) := by + unfold fl4DissociateVcpuTcb + exact NdDlA.bind (dissociateVcpuTcb_dlA _ _) fun _ => fl4VcpuExtUpdate_dlA _ _ + +theorem fl4AssociateVcpuTcb_dlA (v t : ObjRef) : NdDlA (fl4AssociateVcpuTcb (Z := Unit) v t) := by + unfold fl4AssociateVcpuTcb + repeat' first + | with_reducible exact archThreadGet_dlA _ _ | with_reducible exact getVcpu_dlA _ + | with_reducible exact archThreadSet_dlA _ _ | with_reducible exact vcpuUpdate_dlA _ _ + | with_reducible exact fl4DissociateVcpuTcb_dlA _ _ | with_reducible exact NdDlA.pure _ + | with_reducible refine NdDlA.bind ?_ (fun _ => ?_) | dsimp only | split + +theorem fl4CancelRun_dlA (t : ObjRef) : NdDlA (fl4CancelRun (Z := Unit) t) := by + unfold fl4CancelRun + refine NdDlA.bind (getRunVcpu_dlA _) fun o => ?_ + cases o with + | none => exact NdDlA.pure _ + | some vc => exact NdDlA.bind (fl4VcpuExtUpdate_dlA _ _) fun _ => setRunVcpu_dlA _ _ + +theorem NotRunVmA.dc {σ σ' : State Unit} (hn : NotRunVmA σ) (K : DcA σ σ') : NotRunVmA σ' := by + intro x' hx' + rw [K.cur] at hx' + obtain ⟨x, hx, e⟩ := K.ct x' hx' + rw [e]; exact hn x hx + +theorem notRunVm_restartA {σ : State Unit} {x : Tcb} : NotRunVmA (stUpdA σ σ.curThread x .restart) := by + intro x' hx' + have h : (stUpdA σ σ.curThread x .restart).kheap σ.curThread = some (.tcb x') := hx' + simp only [stUpdA, setObjA, Function.update_self, Option.some.injEq, KernelObject.tcb.injEq] at h + subst h + intro h + simp at h + +theorem setTcbRest_dcA (t : ObjRef) (σ : State Unit) (hn : NotRunVmA σ) (r : Unit × State Unit) + (hr : r ∈ ((do + let rv ← getRunVcpu (Z := Unit) t + NonDet.when (rv ≠ none) (do + let st ← getThreadState (Z := Unit) t + if st = ThreadState.ext ExtThreadState.runningVM then fl4CompleteRun t .interrupt 0 0 0 + else fl4CancelRun t)) σ).1) : + DcA σ r.2 := by + obtain ⟨a4, ha4, hr⟩ := mem_bindA.1 hr + have K4 := (getRunVcpu_dlA t σ a4 ha4).toDc + have hn4 := NotRunVmA.dc hn K4 + have K5 : DcA a4.2 r.2 := by + unfold NonDet.when at hr + split at hr + · obtain ⟨a5, ha5, hr⟩ := mem_bindA.1 hr + unfold getThreadState threadGet at ha5 + obtain ⟨y, hy, hb⟩ := mem_bindA.1 ha5 + change y ∈ (NonDet.assertOpt (getTcb (Z := Unit) t a4.2) a4.2).1 at hy + cases hg : getTcb (Z := Unit) t a4.2 with + | none => rw [hg] at hy; exact absurd hy (Set.notMem_empty _) + | some x => + rw [hg] at hy + obtain rfl : y = (x, a4.2) := hy + obtain rfl : a5 = (x.tcbState, a4.2) := hb + dsimp only at hr + split at hr + · rename_i hrun + refine fl4CompleteRun_dcA t _ _ _ _ a4.2 ⟨fun e => ?_, fun x' hx' => ?_⟩ r hr + · exact hn4 x (e ▸ Sched.getTcb_kheapA hg) hrun + · rw [Sched.getTcb_kheapA hg] at hx' + obtain rfl := KernelObject.tcb.inj (Option.some.inj hx') + rw [hrun]; rfl + · exact (fl4CancelRun_dlA t a4.2 r hr).toDc + · obtain rfl : r = ((), a4.2) := Set.mem_singleton_iff.1 hr + exact (dlA_refl _).toDc + exact K4.trans K5 + +theorem fl4VcpuSetTcbPre_dcA (vc t : ObjRef) (σ : State Unit) (hn : NotRunVmA σ) (r : Unit × State Unit) + (hr : r ∈ (fl4VcpuSetTcbPre (Z := Unit) vc t σ).1) : DcA σ r.2 := by + unfold fl4VcpuSetTcbPre at hr + obtain ⟨a1, ha1, hr⟩ := mem_bindA.1 hr + have K1 := fl4VcpuEndRun_dcA vc σ hn a1 ha1 + have hn1 := NotRunVmA.dc hn K1 + obtain ⟨a2, ha2, hr⟩ := mem_bindA.1 hr + obtain ⟨tv, σ2⟩ := a2 + have K2 := (archThreadGet_dlA _ _ a1.2 (tv, σ2) ha2).toDc + have hn2 := NotRunVmA.dc hn1 K2 + cases tv with + | none => exact K1.trans (K2.trans (setTcbRest_dcA t σ2 hn2 r hr)) + | some old => + obtain ⟨a3, ha3, hr3⟩ := mem_bindA.1 hr + have K3 := fl4VcpuEndRun_dcA old σ2 hn2 a3 ha3 + exact K1.trans (K2.trans (K3.trans (setTcbRest_dcA t a3.2 (NotRunVmA.dc hn2 K3) r hr3))) + +theorem fl4PerformSetTcb_dcA (vc t : ObjRef) (σ : State Unit) (hn : NotRunVmA σ) (r : List Data × State Unit) + (hr : r ∈ (fl4PerformVcpuInv (Z := Unit) (.setTcb vc t) σ).1) : DcA σ r.2 := by + unfold fl4PerformVcpuInv at hr + obtain ⟨a, ha, h2⟩ := mem_bindA.1 hr + obtain ⟨b, hb, h3⟩ := mem_bindA.1 h2 + have e : r = ([], b.2) := Set.mem_singleton_iff.1 h3 + rw [e] + exact (fl4VcpuSetTcbPre_dcA vc t σ hn a ha).trans ((fl4AssociateVcpuTcb_dlA vc t a.2 b hb).toDc) + +/-! ## The current-thread frame -/ + +/-- **The frame of a VCPU step on the current thread** (module docstring). -/ +structure DrA (σ σ' : State Unit) : Prop where + cur : σ'.curThread = σ.curThread + time : σ'.curTime = σ.curTime + last : σ'.machineState.lastMachineTime = σ.machineState.lastMachineTime + csc : σ'.curSc = σ.curSc + cons : σ'.consumedTime = σ.consumedTime + rel : σ'.releaseQueue = σ.releaseQueue + act : σ'.schedulerAction = σ.schedulerAction ∨ σ'.schedulerAction = .chooseNewThread + sc : ∀ p y k, σ'.kheap p = some (.schedContext y k) → σ.kheap p = some (.schedContext y k) + ct : ∀ x', σ'.kheap σ.curThread = some (.tcb x') → ∃ x, σ.kheap σ.curThread = some (.tcb x) ∧ + (x'.tcbState = x.tcbState ∨ x'.tcbState = .running ∨ x'.tcbState = .ext .runningVM) + +theorem DlA.toDr {σ σ' : State Unit} (h : DlA σ σ') : DrA σ σ' := + ⟨h.cur, h.time, h.last, h.csc, h.cons, h.rel, Or.inl h.act, h.sc, fun x' hx' => by + obtain ⟨x, hx, e⟩ := h.tcb _ x' hx' + exact ⟨x, hx, Or.inl e⟩⟩ + +theorem DcA.toDr {σ σ' : State Unit} (h : DcA σ σ') : DrA σ σ' := + ⟨h.cur, h.time, h.last, h.csc, h.cons, h.rel, Or.inl h.act, h.sc, fun x' hx' => by + obtain ⟨x, hx, e⟩ := h.ct x' hx' + exact ⟨x, hx, Or.inl e⟩⟩ + +theorem DrA.trans {a b c : State Unit} (h1 : DrA a b) (h2 : DrA b c) : DrA a c where + cur := h2.cur.trans h1.cur + time := h2.time.trans h1.time + last := h2.last.trans h1.last + csc := h2.csc.trans h1.csc + cons := h2.cons.trans h1.cons + rel := h2.rel.trans h1.rel + act := by + rcases h2.act with e | e + · rcases h1.act with e' | e' + · exact Or.inl (e.trans e') + · exact Or.inr (e.trans e') + · exact Or.inr e + sc := fun p y k h => h1.sc p y k (h2.sc p y k h) + ct := fun x hx => by + obtain ⟨y, hy, e⟩ := h2.ct x (by rw [h1.cur]; exact hx) + rw [h1.cur] at hy + obtain ⟨z, hz, f⟩ := h1.ct y hy + refine ⟨z, hz, ?_⟩ + rcases e with e | e | e + · rcases f with f | f | f + · exact Or.inl (e.trans f) + · exact Or.inr (Or.inl (e.trans f)) + · exact Or.inr (Or.inr (e.trans f)) + · exact Or.inr (Or.inl e) + · exact Or.inr (Or.inr e) + +/-- A computation, started with `t` the current thread, that is a `DrA` step. -/ +def NdDrC {α : Type} (t : ObjRef) (m : NonDet (State Unit) α) : Prop := + ∀ σ : State Unit, t = σ.curThread → ∀ r ∈ (m σ).1, DrA σ r.2 + +theorem NdDrC.ofDl {α : Type} {t : ObjRef} {m : NonDet (State Unit) α} (h : NdDlA m) : NdDrC t m := + fun σ _ r hr => (h σ r hr).toDr + +theorem NdDrC.bind {α β : Type} {t : ObjRef} {m : NonDet (State Unit) α} {f : α → NonDet (State Unit) β} + (hm : NdDrC t m) (hf : ∀ a, NdDrC t (f a)) : NdDrC t (m >>= f) := fun σ ht r hr => by + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + have K := hm σ ht x hx + exact K.trans (hf x.1 x.2 (ht.trans K.cur.symm) r hr) + +theorem setThreadState_drC (t : ObjRef) (ts : ThreadState) (hs : ts = .running ∨ ts = .ext .runningVM) : + NdDrC t (Fl4SpecA64.setThreadState (Z := Unit) t ts) := by + intro σ ht r hr + unfold Fl4SpecA64.setThreadState at hr + obtain ⟨a1, ha1, h2⟩ := mem_bindA.1 hr + obtain ⟨y, hy, ea⟩ := threadSet_memA _ _ ha1 + unfold Fl4SpecA64.setThreadStateAct at h2 + obtain ⟨d, hd, h2⟩ := mem_bindA.1 h2 + obtain rfl : d = (a1.2.curThread, a1.2) := hd + obtain ⟨e, he, h2⟩ := mem_bindA.1 h2 + obtain rfl : e = (a1.2.schedulerAction, a1.2) := he + obtain ⟨f, hf, h2⟩ := mem_bindA.1 h2 + obtain rfl : f = (schedulable t a1.2, a1.2) := hf + have key : r.2 = a1.2 ∨ r.2 = { a1.2 with schedulerAction := .chooseNewThread } := by + unfold NonDet.when at h2 + split at h2 + · right + unfold setSchedulerAction at h2 + have h3 := Set.mem_singleton_iff.1 h2 + rw [h3] + · left + have h3 := Set.mem_singleton_iff.1 h2 + rw [h3] + have kh : r.2.kheap = Function.update σ.kheap t (some (.tcb { y with tcbState := ts })) := by + rcases key with h | h <;> rw [h, ea] + have hf2 : r.2.curThread = σ.curThread ∧ r.2.curTime = σ.curTime ∧ r.2.machineState = σ.machineState ∧ + r.2.curSc = σ.curSc ∧ r.2.consumedTime = σ.consumedTime ∧ r.2.releaseQueue = σ.releaseQueue := by + rcases key with h | h <;> rw [h, ea] <;> exact ⟨rfl, rfl, rfl, rfl, rfl, rfl⟩ + refine ⟨hf2.1, hf2.2.1, by rw [hf2.2.2.1], hf2.2.2.2.1, hf2.2.2.2.2.1, hf2.2.2.2.2.2, ?_, + fun p y' k hp => ?_, fun x' hx' => ?_⟩ + · rcases key with h | h + · left; rw [h, ea] + · right; rw [h] + · rw [kh] at hp + by_cases e : p = t + · subst e; rw [Function.update_self] at hp; cases hp + · rwa [Function.update_of_ne e] at hp + · rw [kh, ← ht, Function.update_self] at hx' + obtain rfl := KernelObject.tcb.inj (Option.some.inj hx') + refine ⟨y, by rw [← ht]; exact hy, Or.inr ?_⟩ + rcases hs with h | h + · exact Or.inl h + · exact Or.inr h + +theorem NdDrC.when {t : ObjRef} {P : Prop} {inst : Decidable P} {m : NonDet (State Unit) Unit} (h : NdDrC t m) : + NdDrC t (NonDet.when P m) := fun σ ht r hr => by + unfold NonDet.when at hr + split at hr + · exact h σ ht r hr + · obtain rfl : r = ((), σ) := Set.mem_singleton_iff.1 hr + exact (dlA_refl σ).toDr + +theorem fl4CompleteRun_drC (t : ObjRef) (stop : RunStop) (w1 w2 w3 : Fl4SpecA64.Word) : + NdDrC t (fl4CompleteRun (Z := Unit) t stop w1 w2 w3) := by + unfold fl4CompleteRun + refine NdDrC.bind (NdDrC.ofDl (getRunVcpu_dlA _)) fun o => ?_ + cases o with + | none => exact NdDrC.ofDl (NdDlA.pure _) + | some vc => + refine NdDrC.bind (NdDrC.ofDl (fl4VcpuFlush_dlA _)) fun _ => ?_ + refine NdDrC.bind (NdDrC.ofDl (getVcpu_dlA _)) fun v => ?_ + refine NdDrC.bind ?_ fun _ => ?_ + · refine NdDrC.ofDl (NdDlA.when (NdDlA.bind (getCap_dlA _) fun buf => ?_)) + split + · exact NdDlA.when (fl4ReadStateToFrame_dlA _ _ _) + · exact NdDlA.pure _ + refine NdDrC.bind (NdDrC.ofDl (fl4VcpuExtUpdate_dlA _ _)) fun _ => ?_ + refine NdDrC.bind (NdDrC.ofDl (setRunVcpu_dlA _ _)) fun _ => ?_ + refine NdDrC.bind (NdDrC.ofDl (asUser_dlA _ _)) fun _ => ?_ + refine NdDrC.bind (NdDrC.ofDl (asUser_dlA _ _)) fun _ => ?_ + refine NdDrC.bind (NdDrC.ofDl (setMessageInfo_dlA _ _)) fun _ => ?_ + refine NdDrC.bind (NdDrC.ofDl (asUser_dlA _ _)) fun _ => ?_ + exact setThreadState_drC t _ (Or.inl rfl) + +theorem fl4VcpuCheck_drC (t : ObjRef) : NdDrC t (fl4VcpuCheck (Z := Unit) t) := by + unfold fl4VcpuCheck + refine NdDrC.bind (NdDrC.ofDl (threadGet_dlA _ _)) fun st => ?_ + refine NdDrC.when ?_ + refine NdDrC.bind (NdDrC.ofDl (getRunVcpu_dlA _)) fun vo => ?_ + cases vo with + | none => + exact NdDrC.bind (NdDrC.ofDl (asUser_dlA _ _)) fun _ => setThreadState_drC t _ (Or.inl rfl) + | some _ => + refine NdDrC.bind (NdDrC.ofDl (threadGet_dlA _ _)) fun no => ?_ + cases no with + | none => exact NdDrC.ofDl (NdDlA.pure _) + | some n => + refine NdDrC.bind (NdDrC.ofDl (getNotification_dlA _)) fun ntfn => ?_ + split + · exact NdDrC.bind (NdDrC.ofDl (setNotification_dlA _ _)) fun _ => fl4CompleteRun_drC t _ _ _ _ + · exact NdDrC.ofDl (NdDlA.pure _) + +theorem fl4PerformRun_drC (vc ct frame : ObjRef) (offset inMask outMask : Fl4SpecA64.Word) + (vg : Fl4SpecA64.Word × List Fl4SpecA64.Word) : + NdDrC ct (fl4PerformVcpuInv (Z := Unit) (.run vc ct frame offset inMask outMask vg)) := by + unfold fl4PerformVcpuInv + refine NdDrC.bind (NdDrC.ofDl (fl4VcpuFlush_dlA _)) fun _ => ?_ + refine NdDrC.bind (NdDrC.ofDl (fl4WriteStateFromFrame_dlA _ _ _ _)) fun _ => ?_ + refine NdDrC.bind (NdDrC.ofDl (fl4VcpuExtUpdate_dlA _ _)) fun _ => ?_ + refine NdDrC.bind (NdDrC.ofDl (setRunVcpu_dlA _ _)) fun _ => ?_ + refine NdDrC.bind (setThreadState_drC ct _ (Or.inr rfl)) fun _ => ?_ + refine NdDrC.bind (fl4VcpuCheck_drC ct) fun _ => ?_ + exact NdDrC.ofDl (NdDlA.pure _) + +theorem archPerformVcpu_memA {i : Fl4VcpuInvocation} {σ σ' : State Unit} {ws : List Data} + (h : (.ok ws, σ') ∈ ((fl4ArchPerformInvocation (Z := Unit) (.vcpu i)).run σ).1) : + (ws, σ') ∈ (fl4PerformVcpuInv (Z := Unit) i σ).1 := by + have e : fl4ArchPerformInvocation (Z := Unit) (.vcpu i) = NonDetE.liftE (fl4PerformVcpuInv i) := rfl + rw [e] at h + exact liftE_run_memA h + +/-! ## The body-end facts -/ + +section +variable {S A : Type} {st : machine.Store S} [L : LawfulStore st] +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s : S} {a : A} {σ σ' : State Unit} {m : McState} + +open Fl4ProofsA64.P3.Sched in +/-- **`BodyOKA.ofEntry` with the current thread's state any `ActivateStateOKA` one** (a VCPU run leaves the caller in +`runningVM`). -/ +theorem Sched.BodyOKA.ofEntryA (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (hact : σ'.schedulerAction = σ.schedulerAction ∨ σ'.schedulerAction = .chooseNewThread) (htime : σ'.curTime = σ.curTime) + (hlast : σ'.machineState.lastMachineTime = σ.machineState.lastMachineTime) + (hthr : σ'.curThread = σ.curThread) (hsc : σ'.curSc = σ.curSc) (hcon : σ'.consumedTime = σ.consumedTime) + (hrel : σ'.releaseQueue = σ.releaseQueue) + (hobj : ∀ y' k, σ'.kheap σ.curSc = some (.schedContext y' k) → ∃ y, σ.kheap σ.curSc = some (.schedContext y k) ∧ + y'.scRefills = y.scRefills ∧ y'.scRefillMax = y.scRefillMax ∧ y'.scTcb = y.scTcb) + (hrun : σ'.schedulerAction = .resumeCurThread → ∀ x, σ'.kheap σ.curThread = some (.tcb x) → + ActivateStateOKA x.tcbState) : + BodyOKA σ' m := by + have hd : ∀ y' y : SchedContext, y'.scRefills = y.scRefills → refillHd y' = refillHd y := fun y' y e => by + unfold refillHd; rw [e] + have ha : ∀ y' y : SchedContext, y'.scRefillMax = y.scRefillMax → scActive y' = scActive y := fun y' y e => by + unfold scActive; rw [e] + refine ⟨⟨?_, fun y' k hk _ _ => ?_, fun h x hx => ?_⟩, fun t e => ?_, fun h => ?_, fun y' k hk _ _ _ o ho => ?_⟩ + · have := J.validTime + unfold ValidMachineTimeA at this ⊢ + rw [htime, hlast]; exact this + · rw [hsc] at hk + obtain ⟨y, hy, e1, e2, -⟩ := hobj y' k hk + constructor + · intro z j hz + rw [hsc, hk] at hz + obtain ⟨rfl, -⟩ := KernelObject.schedContext.inj (Option.some.inj hz) + rw [hd _ _ e1, hcon, htime] + exact J.offReady y k hy + · intro hpos z j hz hactive + rw [hsc, hk] at hz + obtain ⟨rfl, -⟩ := KernelObject.schedContext.inj (Option.some.inj hz) + rw [hd _ _ e1, hcon, htime] + rw [hcon] at hpos + refine J.offOK hpos y k hy fun h => ?_ + rw [← ha _ _ e2]; exact hactive (hsc ▸ h) + · exact hrun h x (hthr ▸ hx) + · rcases hact with h | h + · rw [h, J.act] at e; cases e + · rw [h] at e; cases e + · rw [hsc] at h ⊢; exact J.curScNotIdle h + · rw [hsc] at hk + obtain ⟨y, hy, -, -, e3⟩ := hobj y' k hk + obtain ⟨x, hx, hxs, -⟩ := J.curInv.tcb + obtain ⟨z, j, hz, hzt⟩ := J.sr.inv.back _ x _ hx hxs + rw [hy] at hz + obtain ⟨rfl, -⟩ := KernelObject.schedContext.inj (Option.some.inj hz) + rw [e3, hzt] at ho + rw [hrel, ← Option.some.inj ho] + exact J.curInv.notRel + +/-- **The body-end facts after `Restart` and a `DrA` step** (module docstring). -/ +theorem Sched.BodyOKA.ofDr + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {x : Tcb} (hk : σ.kheap σ.curThread = some (.tcb x)) (K : DrA (stUpdA σ σ.curThread x .restart) σ') : + Sched.BodyOKA σ' m ∧ σ'.curThread = σ.curThread := by + have ne : σ.curSc ≠ σ.curThread := fun e => by + obtain ⟨y, j, hy'⟩ := J.curInv.sc + rw [e, hk] at hy'; cases hy' + refine ⟨Sched.BodyOKA.ofEntryA J K.act K.time K.last K.cur K.csc K.cons K.rel (fun y' k hy => ?_) + (fun _ x' hx' => ?_), K.cur⟩ + · have h1 := K.sc _ _ _ hy + simp only [stUpdA, setObjA, Function.update_of_ne ne] at h1 + exact ⟨y', h1, rfl, rfl, rfl⟩ + · obtain ⟨x0, hx0, e⟩ := K.ct x' hx' + simp only [stUpdA, setObjA, Function.update_self, Option.some.injEq, KernelObject.tcb.injEq] at hx0 + subst hx0 + rcases e with e | e | e + · rw [e]; exact Or.inr (Or.inl rfl) + · rw [e]; exact Or.inl rfl + · rw [e]; exact Or.inr (Or.inr (Or.inr rfl)) + +/-- **`SetTCB`'s body-end facts** from its perform. -/ +theorem Sched.BodyOKA.ofVcpuSetTcb + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {x : Tcb} (hk : σ.kheap σ.curThread = some (.tcb x)) {vc t : ObjRef} + (h : (.ok [], σ') ∈ ((fl4ArchPerformInvocation (Z := Unit) (.vcpu (.setTcb vc t))).run + (stUpdA σ σ.curThread x .restart)).1) : Sched.BodyOKA σ' m ∧ σ'.curThread = σ.curThread := + Sched.BodyOKA.ofDr J hk (DcA.toDr (fl4PerformSetTcb_dcA vc t _ notRunVm_restartA ([], σ') + (archPerformVcpu_memA h))) + +/-- **`Run`'s body-end facts** from its perform (the caller `ct` is the current thread). -/ +theorem Sched.BodyOKA.ofVcpuRun + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {x : Tcb} (hk : σ.kheap σ.curThread = some (.tcb x)) {vc frame : ObjRef} + {offset inMask outMask : Fl4SpecA64.Word} {vg : Fl4SpecA64.Word × List Fl4SpecA64.Word} + (h : (.ok [], σ') ∈ ((fl4ArchPerformInvocation (Z := Unit) + (.vcpu (.run vc σ.curThread frame offset inMask outMask vg))).run (stUpdA σ σ.curThread x .restart)).1) : + Sched.BodyOKA σ' m ∧ σ'.curThread = σ.curThread := by + have hp := archPerformVcpu_memA h + exact Sched.BodyOKA.ofDr J hk (fl4PerformRun_drC vc σ.curThread frame offset inMask outMask vg + (stUpdA σ σ.curThread x .restart) rfl ([], σ') hp) + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3VsArmFactsA.lean b/proofs/Fl4ProofsA64/P3/Stage3VsArmFactsA.lean new file mode 100644 index 000000000..2c8e75671 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3VsArmFactsA.lean @@ -0,0 +1,173 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Plan B): **the VSpace arms' decode-site facts from the record.** + +* `JSyscallA.unmPtA`: `UnmPtA` of an unmapped page-table cap at an `OS` slot, except `nt` (`ptr ∉ Z.S1`). +* `JSyscallA.ptOffRootsA`: such a table is no Stage-2 root page and no high window (`hY`; `hS2` up to `ptr ∉ Z.T2`). +* `JSyscallA.frPlA`: `FrPlA` of a frame cap at an `OS` slot; for a device frame the kernel-window bound is asked + (`hdev`): no record field bounds a device frame's address. +* `JSyscallA.ptMappedA`: a mapped table cap's table is in `Z.S1` at depth ≥ 1, unless it is a Stage-2 table. + +Named premises: [J] `JSyscallA`. New module. +-/ +import Fl4ProofsA64.P3.Stage3ScopeA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-- `p - PPTR_BASE` of a pointer of the kernel window. -/ +theorem wsub_baseA {p : U64} (h : layout.PPTR_BASE.val ≤ p.val) : + (core.num.U64.wrapping_sub p layout.PPTR_BASE).val = p.val - layout.PPTR_BASE.val := by + have e := Fl4ProofsA64.P0.Consts.layout.PPTR_BASE.val_eq + have hp : p.val < 18446744073709551616 := by scalar_tac + rw [e] at h + simp [core.num.U64.wrapping_sub_val_eq, e, U64.size_def, U64.numBits] + omega + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {ar : machine.Arch A} {s : S} {a : A} {σ : State Unit} {m : McState} {nA : U64} +variable (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) +include J + +/-- **A page-table cap's table is a 4 KiB heap object** (module docstring). -/ +theorem JSyscallA.ptHeapA {u ptr asid va : U64} {mp : Bool} (hu : u ∈ OS) + (hcap : capA st s u = .PageTable ptr mp asid va) : H ptr = some (.other 12) := by + obtain ⟨Pt, Ub, Pp7, Tb7, Hr, Te, Kt, Lv7, Re, bits, KLv, W⟩ := J7A.unp J.j7 + exact W.r2.pageObjects u hu ptr mp asid va hcap + +/-- **A 4 KiB heap object is no 8 KiB root of `Z.X` / `Z.Y`, nor such a root's upper page.** -/ +theorem JSyscallA.ptOffRootsA {ptr : U64} (hH : H ptr = some (.other 12)) : + ∀ r ∈ Z.X ∪ Z.Y, ptr ≠ r ∧ ptr ≠ wa r 4096#u64 := by + intro r hr + have hh := J.sys.ent.heap + obtain ⟨q, z, hq, h1, h2⟩ := J.setsInH.roots r hr + have fq : q.val + 2 ^ z ≤ 2 ^ 64 := hh.fits q _ hq + have fp : ptr.val + 2 ^ 12 ≤ 2 ^ 64 := hh.fits ptr _ hH + have disj : q ≠ ptr → q.val + 2 ^ z ≤ ptr.val ∨ ptr.val + 2 ^ 12 ≤ q.val := fun h => + hh.disj q _ ptr _ hq hH h + have same : q = ptr → z = 12 := fun h => by + rw [h, hH] at hq + simp only [Option.some.injEq, HKind.other.injEq] at hq + exact hq.symm + have contra : r.val ≤ ptr.val → ptr.val < r.val + 8192 → (q = ptr → r.val = ptr.val) → False := by + intro a1 a2 a3 + by_cases e : q = ptr + · have := same e + subst this + have := a3 e + rw [e] at h2 + omega + · rcases disj e with h | h <;> omega + have e4 : (4096#u64 : U64).val = 4096 := rfl + constructor + · intro e + subst e + exact contra (le_refl _) (by omega) (fun _ => rfl) + · intro e + have hr4 : (wa r 4096#u64).val = r.val + 4096 := by + simp [core.num.U64.wrapping_add_val_eq, e4, U64.size_def, U64.numBits] + omega + rw [← e] at hr4 + refine contra (by omega) (by omega) fun h => ?_ + rw [h] at h1 + omega + +/-- **`UnmPtA` of an unmapped table** (module docstring); `hnt` is not derivable: `Retype` puts every new table into +`Z.S1`. -/ +theorem JSyscallA.unmPtA {u ptr asid va : U64} (hu : u ∈ OS) (hcap : capA st s u = .PageTable ptr false asid va) + (hnt : ptr ∉ Z.S1) : UnmPtA Z.S1 Z.Y OS H.tcbs (L.mem s) σ ptr := by + obtain ⟨Pt, Ub, Pp7, Tb7, Hr, Te, Kt, Lv7, Re, bits, KLv, W⟩ := J7A.unp J.j7 + have hH := J.ptHeapA hu hcap + have hPt : ptr ∈ Pt := W.r2.pageTables u hu ptr false asid va hcap + have hh := J.sys.ent.heap + have hal : ptr.val % 2 ^ 12 = 0 := hh.align ptr _ hH + have hfit : ptr.val + 2 ^ 12 ≤ 2 ^ 64 := hh.fits ptr _ hH + obtain ⟨w1, w2⟩ := J.heapWin ptr _ hH + have w2' : ptr.val + 2 ^ 12 ≤ layout.PPTR_BASE.val + 2 ^ 44 := w2 + have U0 := J.unmPt.pt u hu ptr asid va hcap + have e8 : (8#u64 : U64).val = 8 := rfl + have hmul : ∀ i : U64, i.val < 512 → (core.num.U64.wrapping_mul i 8#u64).val = i.val * 8 := by + intro i hi + simp [core.num.U64.wrapping_mul_val_eq, e8, U64.size_def, U64.numBits] + omega + have hpe : ∀ i : U64, i.val < 512 → (peAF ptr i).val = ptr.val + i.val * 8 := by + intro i hi + unfold peAF + simp [core.num.U64.wrapping_add_val_eq, hmul i hi, U64.size_def, U64.numBits] + omega + refine + { nt := hnt + al := by omega + ytb := fun y hy e => (J.ptOffRootsA hH y (Or.inr hy)).2 e.symm + pa := by rw [wsub_baseA w1]; omega + inv := U0.inv + os := fun i hi => W.vr.os _ (Or.inl ⟨ptr, hPt, core.num.U64.wrapping_mul i 8#u64, by rw [hmul i hi]; omega, + by rw [hmul i hi]; omega, rfl⟩) + tcb := fun i hi t ht => ?_ + spec := U0.spec } + have htc : H t = some .tcb := ht + have hne : ptr ≠ t := fun e => by rw [e, htc] at hH; cases hH + have d : ptr.val + 2 ^ 12 ≤ t.val ∨ t.val + 2 ^ 12 ≤ ptr.val := hh.disj ptr _ t _ hH htc hne + rw [hpe i hi] + omega + +/-- **`FrPlA` of a frame cap at an `OS` slot** (module docstring). -/ +theorem JSyscallA.frPlA {u fp rt asid va : U64} {sz : capability.FrameSize} {dev mp ept : Bool} (hu : u ∈ OS) + (hcap : capA st s u = .Frame fp sz rt dev mp ept asid va) (hwf : CapWfA (capA st s u)) + -- [J] a device frame lies in the kernel window (no record field says so) + (hdev : dev = true → (core.num.U64.wrapping_sub fp layout.PPTR_BASE).val < 2 ^ 44) : + FrPlA sz fp mp asid := by + obtain ⟨Pt, Ub, Pp7, Tb7, Hr, Te, Kt, Lv7, Re, bits, KLv, W⟩ := J7A.unp J.j7 + have hmz : mp = true ↔ asid ≠ 0#u64 := by + rw [hcap] at hwf + obtain ⟨-, -, ⟨-, h1, h2, -⟩, -⟩ := hwf + cases mp + · constructor + · intro h; cases h + · intro h + exact absurd (UScalar.eq_of_val_eq (h1 rfl)) h + · exact ⟨fun _ => h2 rfl, fun _ => rfl⟩ + obtain ⟨b, hb, hbv⟩ : ∃ b : U64, sz.bits = ok b ∧ b.val = (psA sz).val := by + cases sz <;> exact ⟨_, rfl, rfl⟩ + have hv := psA_valA sz + cases dev with + | false => + have hf : H fp = some (.other b.val) := W.cc.fo u hu fp sz rt mp ept asid va hcap b hb + have hal : fp.val % 2 ^ b.val = 0 := J.sys.ent.heap.align fp _ hf + obtain ⟨w1, w2⟩ := J.heapWin fp _ hf + have w2' : fp.val + 2 ^ b.val ≤ layout.PPTR_BASE.val + 2 ^ 44 := w2 + have e := Fl4ProofsA64.P0.Consts.layout.PPTR_BASE.val_eq + refine ⟨?_, ?_, hmz⟩ + · rw [wsub_baseA w1, ← hbv] + rw [← hbv] at hv + rcases hv with h | h | h <;> rw [h] at hal ⊢ <;> rw [e] <;> omega + · rw [wsub_baseA w1] + rw [← hbv] at hv + rcases hv with h | h | h <;> rw [h] at w2' <;> omega + | true => + have hal : fp.val % 2 ^ b.val = 0 := W.df u hu fp sz rt mp ept asid va hcap b hb + refine ⟨?_, hdev rfl, hmz⟩ + have e := Fl4ProofsA64.P0.Consts.layout.PPTR_BASE.val_eq + rw [← hbv] at hv ⊢ + rcases hv with h | h | h <;> rw [h] at hal ⊢ <;> + simp [core.num.U64.wrapping_sub_val_eq, e, U64.size_def, U64.numBits] <;> omega + +/-- **A mapped table cap's table is a tracked Stage-1 table**, unless it is a Stage-2 table (`hT2`). -/ +theorem JSyscallA.ptMappedA {sl : CslotPtr} (hab : AbsSlotA Z.th sl) {ptr asid va : U64} + (hcap : capA st s (cslotAddrA sl) = .PageTable ptr true asid va) (hT2 : ptr ∉ Z.T2) : + ptr ∈ Z.S1 ∧ 1 ≤ Z.Lv ptr := + (J.ptCapIn.hpA J.j.1.place hab hcap).resolve_right hT2 + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3VsBodyA.lean b/proofs/Fl4ProofsA64/P3/Stage3VsBodyA.lean new file mode 100644 index 000000000..c68c34754 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3VsBodyA.lean @@ -0,0 +1,245 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Plan B): **the closed Send / NBSend / Call bodies of the VSpace arm groups**, the +VSpace counterparts of `JSyscallA.cnodeCopyBodyA`. Each takes `JSyscallA`, the platform, the case selector `hSel` +and the other lines' `hFault` / `hErr` / `HEnv` (`HPreEnvA`); the arm inputs come from the run and the record +(`P3/Stage3VsRunA`). + +* `JSyscallA.sendBodyRunA`: the body from a member at each successful decode and no preempted decode. +* `frameUnmapBodyA`, `ptUnmapBodyA`, `ptMapS1BodyA`, `frameMapBodyA` (Stage-1 root and high window), + `asidAssignBodyA`. + +Named premises: [J] `JSyscallA`, `hFault`, `hErr`, `HEnv` (`HPreEnvA`); inside `hSel`, [J] the high-window canonical cap +(`ptUnmapBodyA`), the device-frame window (`frameMapBodyA`); open cases inside `hSel`: +`ptr ∉ Z.S1` (`ptMapS1BodyA`) and Stage-2 tables (`ptr ∉ Z.T2`). [P] `ArchKeepsA`, `S2IpaE`. New module. + +`ptMapS1BodyA` covers no table made by `Retype` (see `P3/Stage3VsRunA`). +-/ +import Fl4ProofsA64.P3.Stage3VsRunA +import Fl4ProofsA64.P3.Stage3HPreA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {Clock : A → Fl4SpecA64.MachineState → Prop} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {ar : machine.Arch A} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} +variable (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) (hIP : IrqPlatA st ar) (rep : Bool) {pending : SMonad Unit Unit} (call : Fl4SpecA64.Syscall) + (hc : call = .sysSend ∨ call = .sysNBSend ∨ call = .sysCall) +variable (hFault : ∀ calling blocking donate, + DispatchInvokeAtA (L.mem s (regWA (L.curW s) layout.reg.CAP)) (L.mem s (regWA (L.curW s) layout.reg.REPLY)) + (L.mem s (regWA (L.curW s) layout.reg.NBSEND_RECV_DEST)) call calling blocking donate false + (L.mem s (regWA (L.curW s) layout.reg.CAP)) → + blocking = true → ∀ (f : error.Fault) (f' : Fl4SpecA64.Fault), CapFaultCorrA f f' → + faults.handle_fault st ar s a (L.curW s) f ⦃ r => ∃ τ, ((), τ) ∈ (handleFault (Z := Unit) σ.curThread f' σ).1 ∧ + A64RelS3 st AR ar hPl.ipa n Z.Cs r.1 r.2 τ m ⦄) + (hErr : ∀ calling blocking donate, + DispatchInvokeAtA (L.mem s (regWA (L.curW s) layout.reg.CAP)) (L.mem s (regWA (L.curW s) layout.reg.REPLY)) + (L.mem s (regWA (L.curW s) layout.reg.NBSEND_RECV_DEST)) call calling blocking donate false + (L.mem s (regWA (L.curW s) layout.reg.CAP)) → + ∀ (info : msginfo.MessageInfo) (buf : U64) (r1 : cspace.Resolved) + (e : Aeneas.Std.Array obj.Slot 3#usize) (x : error.Exc) (s' : S) (a' : A), + msg.msg_info st s (L.curW s) = ok info → msg.lookup_ipc_buffer st s false (L.curW s) = ok buf → + cspace.lookup_slot st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.CAP)) = ok (.Ok r1) → + ipc.lookup_extra_caps st s (L.curW s) buf info = ok (.Ok e) → + syscall.decode_invocation st ar s a info.label (msgLenA info buf) r1.slot (capA st s r1.slot) blocking + calling donate false buf e = ok (.Err x, s', a') → x ≠ error.Exc.Preempted → + (match x with + | .Sys e2 => if calling then (do let s2 ← syscall.reply_error st s' (L.curW s) e2 + ok (core.result.Result.Ok (), s2, a')) + else ok (core.result.Result.Ok (), s', a') + | _ => ok (core.result.Result.Ok (), s', a') : Result (core.result.Result Unit error.Exc × S × A)) + ⦃ r => r.1 = .Ok () ∧ ∃ τ, + (.ok (), τ) ∈ ((fl4HandleInvocationR (Z := Unit) rep calling blocking donate false + (Fl4SpecA64.toBl (L.mem s (regWA (L.curW s) layout.reg.CAP)).bv)).run σ).1 ∧ A64RelS3 st AR ar hPl.ipa n Z.Cs r.2.1 r.2.2 τ m ⦄) + (XP : ExitPlatA st ar Z) (HEnv : HPreEnvA st AR ar hPl n Z.Cs m Clock pending s a call) + (hCnt : Sched.CounterStepA σ) +include hA hM hPl J hCnt hfm hIP hc hFault hErr XP HEnv + +set_option maxHeartbeats 1000000 + +/-- **Send, NBSend and Call from a scope member at each successful decode** (module docstring): the decode of this +case is never preempted. -/ +theorem JSyscallA.sendBodyRunA [PlainStore st] + (hArm : ∀ calling blocking donate, + DispatchInvokeAtA (L.mem s (regWA (L.curW s) layout.reg.CAP)) (L.mem s (regWA (L.curW s) layout.reg.REPLY)) + (L.mem s (regWA (L.curW s) layout.reg.NBSEND_RECV_DEST)) call calling blocking donate false + (L.mem s (regWA (L.curW s) layout.reg.CAP)) → + ∀ (info : msginfo.MessageInfo) (buf : U64) (r1 : cspace.Resolved) (e : Aeneas.Std.Array obj.Slot 3#usize) + (mi : Fl4SpecA64.MessageInfo) (sl : Fl4SpecA64.CslotPtr) (c : Fl4SpecA64.Cap) + (excaps : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) (buffer : Option Fl4SpecA64.ObjRef) (argsU : List U64), + msg.msg_info st s (L.curW s) = ok info → msg.lookup_ipc_buffer st s false (L.curW s) = ok buf → + cspace.lookup_slot st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.CAP)) = ok (.Ok r1) → + ipc.lookup_extra_caps st s (L.curW s) buf info = ok (.Ok e) → + SiteA st Z OS s σ info buf r1.slot e mi sl c excaps buffer argsU → + ∀ (r0 : core.result.Result Unit error.Exc) (s0 : S) (a0 : A), + syscall.decode_invocation st ar s a info.label (msgLenA info buf) r1.slot (capA st s r1.slot) blocking + calling donate false buf e = ok (r0, s0, a0) → + (∀ x, r0 = .Err x → x ≠ error.Exc.Preempted) ∧ + (r0 = .Ok () → ∃ Out, DispScope4A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ m Clock hPl.ipa + info.label (msgLenA info buf) r1.slot blocking calling donate false buf e Out)) : + syscall.dispatch_syscall st ar s a (syscallNumberA call) ⦃ r => + StepPostA (fun s a τ => A64RelS3 st AR ar hPl.ipa n Z.Cs s a τ m) + (fl4SettleDispatch pending (fl4DispatchSyscall (fl4HandleInvocationR (Z := Unit) rep) call)) σ r ⦄ := + J.sendBodyScopeA (hCnt := hCnt) hA hM hPl hfm hIP rep call hc hFault + (fun calling blocking donate h info buf r1 e mi sl c excaps buffer argsU einfo ebuf elr eex Q r0 s0 a0 hdec + hgood => by + obtain ⟨np, mem⟩ := hArm calling blocking donate h info buf r1 e mi sl c excaps buffer argsU einfo ebuf elr eex Q + r0 s0 a0 hdec + refine DispScopeA.of4 (mem ?_) + cases r0 with + | Ok u => cases u; rfl + | Err x => exact absurd (hgood x rfl) (np x rfl)) + hErr XP HEnv + +/-- **`ARMPageUnmap` of a frame cap end to end**, every case (module docstring). -/ +theorem JSyscallA.frameUnmapBodyA [PlainStore st] (K : ArchKeepsA ar AR) (hIpa : S2IpaE ar a) + -- this body's case + (hSel : ∀ (info : msginfo.MessageInfo) (buf : U64) (r1 : cspace.Resolved) (e : Aeneas.Std.Array obj.Slot 3#usize) + (mi : Fl4SpecA64.MessageInfo) (sl : Fl4SpecA64.CslotPtr) (c : Fl4SpecA64.Cap) + (excaps : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) (buffer : Option Fl4SpecA64.ObjRef) (argsU : List U64), + msg.msg_info st s (L.curW s) = ok info → msg.lookup_ipc_buffer st s false (L.curW s) = ok buf → + cspace.lookup_slot st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.CAP)) = ok (.Ok r1) → + ipc.lookup_extra_caps st s (L.curW s) buf info = ok (.Ok e) → + SiteA st Z OS s σ info buf r1.slot e mi sl c excaps buffer argsU → + info.label = fl4_abi.label.ARMPageUnmap ∧ + ∃ fp sz rt dev mp isEpt asid va, capA st s r1.slot = .Frame fp sz rt dev mp isEpt asid va) : + syscall.dispatch_syscall st ar s a (syscallNumberA call) ⦃ r => + StepPostA (fun s a τ => A64RelS3 st AR ar hPl.ipa n Z.Cs s a τ m) + (fl4SettleDispatch pending (fl4DispatchSyscall (fl4HandleInvocationR (Z := Unit) rep) call)) σ r ⦄ := + J.sendBodyRunA (hCnt := hCnt) hA hM hPl hfm hIP rep call hc hFault hErr XP HEnv + (fun calling blocking donate _ info buf r1 e mi sl c excaps buffer argsU einfo ebuf elr eex Q r0 s0 a0 hdec => by + obtain ⟨hlab, fp, sz, rt, dev, mp, isEpt, asid, va, hcap⟩ := + hSel info buf r1 e mi sl c excaps buffer argsU einfo ebuf elr eex Q + have h := hdec + rw [hcap, decodeFrame_programA, hlab] at h + have hr0 := frameUnmap_okA (st := st) ar s a _ _ _ _ _ _ _ _ _ _ _ _ _ h + refine ⟨fun x hx => (?_ : False).elim, fun _ => J.frameUnmap_runA hA hM hPl hfm Q hlab K hIpa hcap⟩ + rw [hr0] at hx + cases hx) + +/-- **`ARMPageTableUnmap` end to end** (module docstring). -/ +theorem JSyscallA.ptUnmapBodyA [PlainStore st] (K : ArchKeepsA ar AR) (hIpa : S2IpaE ar a) + -- this body's case + (hSel : ∀ (info : msginfo.MessageInfo) (buf : U64) (r1 : cspace.Resolved) (e : Aeneas.Std.Array obj.Slot 3#usize) + (mi : Fl4SpecA64.MessageInfo) (sl : Fl4SpecA64.CslotPtr) (c : Fl4SpecA64.Cap) + (excaps : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) (buffer : Option Fl4SpecA64.ObjRef) (argsU : List U64), + msg.msg_info st s (L.curW s) = ok info → msg.lookup_ipc_buffer st s false (L.curW s) = ok buf → + cspace.lookup_slot st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.CAP)) = ok (.Ok r1) → + ipc.lookup_extra_caps st s (L.curW s) buf info = ok (.Ok e) → + SiteA st Z OS s σ info buf r1.slot e mi sl c excaps buffer argsU → + info.label = fl4_abi.label.ARMPageTableUnmap ∧ + ∃ ptr mp asid va, capA st s r1.slot = .PageTable ptr mp asid va ∧ (mp = true → ptr ∉ Z.T2) ∧ + (mp = true → 0xffff000000000000 ≤ va.val → c = absCapA (capA st s r1.slot))) : + syscall.dispatch_syscall st ar s a (syscallNumberA call) ⦃ r => + StepPostA (fun s a τ => A64RelS3 st AR ar hPl.ipa n Z.Cs s a τ m) + (fl4SettleDispatch pending (fl4DispatchSyscall (fl4HandleInvocationR (Z := Unit) rep) call)) σ r ⦄ := + J.sendBodyRunA (hCnt := hCnt) hA hM hPl hfm hIP rep call hc hFault hErr XP HEnv + (fun calling blocking donate _ info buf r1 e mi sl c excaps buffer argsU einfo ebuf elr eex Q r0 s0 a0 hdec => by + obtain ⟨hlab, ptr, mp, asid, va, hcap, hT2, hcan⟩ := + hSel info buf r1 e mi sl c excaps buffer argsU einfo ebuf elr eex Q + refine ⟨fun x hx => ?_, fun hr => ?_⟩ + · have h := hdec + rw [hcap, decodePt_programA, hlab, hx] at h + exact ptUnmap_npA (st := st) ar s a _ _ _ _ _ _ _ _ x _ h + · rw [hr] at hdec + exact J.ptUnmap_runA hA hM hPl hfm Q hlab K hIpa hcap hT2 hcan hdec) + +/-- **`ARMPageTableMap` into a Stage-1 root end to end** (module docstring). -/ +theorem JSyscallA.ptMapS1BodyA [PlainStore st] -- this body's case + (hSel : ∀ (info : msginfo.MessageInfo) (buf : U64) (r1 : cspace.Resolved) (e : Aeneas.Std.Array obj.Slot 3#usize) + (mi : Fl4SpecA64.MessageInfo) (sl : Fl4SpecA64.CslotPtr) (c : Fl4SpecA64.Cap) + (excaps : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) (buffer : Option Fl4SpecA64.ObjRef) (argsU : List U64), + msg.msg_info st s (L.curW s) = ok info → msg.lookup_ipc_buffer st s false (L.curW s) = ok buf → + cspace.lookup_slot st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.CAP)) = ok (.Ok r1) → + ipc.lookup_extra_caps st s (L.curW s) buf info = ok (.Ok e) → + SiteA st Z OS s σ info buf r1.slot e mi sl c excaps buffer argsU → + info.label = fl4_abi.label.ARMPageTableMap ∧ + ∃ ptr mp asid va, capA st s r1.slot = .PageTable ptr mp asid va ∧ ptr ∉ Z.S1 ∧ ptr ∉ Z.T2 ∧ + (∀ p l mp asid va, capA st s (exAtA e 0) ≠ .Ept p l mp asid va) ∧ + (∀ p mp asid, capA st s (exAtA e 0) ≠ .HighVSpace p mp asid)) : + syscall.dispatch_syscall st ar s a (syscallNumberA call) ⦃ r => + StepPostA (fun s a τ => A64RelS3 st AR ar hPl.ipa n Z.Cs s a τ m) + (fl4SettleDispatch pending (fl4DispatchSyscall (fl4HandleInvocationR (Z := Unit) rep) call)) σ r ⦄ := + J.sendBodyRunA (hCnt := hCnt) hA hM hPl hfm hIP rep call hc hFault hErr XP HEnv + (fun calling blocking donate _ info buf r1 e mi sl c excaps buffer argsU einfo ebuf elr eex Q r0 s0 a0 hdec => by + obtain ⟨hlab, ptr, mp, asid, va, hcap, hnt, hT2, hS1, hHi⟩ := + hSel info buf r1 e mi sl c excaps buffer argsU einfo ebuf elr eex Q + refine ⟨fun x hx => ?_, fun hr => ?_⟩ + · have h := hdec + rw [hcap, decodePt_programA, hlab, hx] at h + exact ptMap_npA (st := st) ar s a _ _ _ _ _ _ _ _ (exAtA_idx0 e) hS1 hHi x _ h + · rw [hr] at hdec + exact J.ptMapS1_recA hA hM hPl hfm Q hlab hcap (exAtA_idx0 e) hS1 hHi hnt hT2 hdec) + +/-- **`ARMPageMap` into a Stage-1 root or the high window end to end** (module docstring). -/ +theorem JSyscallA.frameMapBodyA [PlainStore st] (K : ArchKeepsA ar AR) + -- this body's case + (hSel : ∀ (info : msginfo.MessageInfo) (buf : U64) (r1 : cspace.Resolved) (e : Aeneas.Std.Array obj.Slot 3#usize) + (mi : Fl4SpecA64.MessageInfo) (sl : Fl4SpecA64.CslotPtr) (c : Fl4SpecA64.Cap) + (excaps : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) (buffer : Option Fl4SpecA64.ObjRef) (argsU : List U64), + msg.msg_info st s (L.curW s) = ok info → msg.lookup_ipc_buffer st s false (L.curW s) = ok buf → + cspace.lookup_slot st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.CAP)) = ok (.Ok r1) → + ipc.lookup_extra_caps st s (L.curW s) buf info = ok (.Ok e) → + SiteA st Z OS s σ info buf r1.slot e mi sl c excaps buffer argsU → + info.label = fl4_abi.label.ARMPageMap ∧ + ∃ fp sz rt dev mp asid va, capA st s r1.slot = .Frame fp sz rt dev mp false asid va ∧ + (dev = true → (core.num.U64.wrapping_sub fp layout.PPTR_BASE).val < 2 ^ 44) ∧ + (∀ p l mp asid va, capA st s (exAtA e 0) ≠ .Ept p l mp asid va)) : + syscall.dispatch_syscall st ar s a (syscallNumberA call) ⦃ r => + StepPostA (fun s a τ => A64RelS3 st AR ar hPl.ipa n Z.Cs s a τ m) + (fl4SettleDispatch pending (fl4DispatchSyscall (fl4HandleInvocationR (Z := Unit) rep) call)) σ r ⦄ := + J.sendBodyRunA (hCnt := hCnt) hA hM hPl hfm hIP rep call hc hFault hErr XP HEnv + (fun calling blocking donate _ info buf r1 e mi sl c excaps buffer argsU einfo ebuf elr eex Q r0 s0 a0 hdec => by + obtain ⟨hlab, fp, sz, rt, dev, mp, asid, va, hcap, hdev, hS1⟩ := + hSel info buf r1 e mi sl c excaps buffer argsU einfo ebuf elr eex Q + refine ⟨fun x hx => ?_, fun hr => ?_⟩ + · have h := hdec + rw [hcap, decodeFrame_programA, hlab, hx] at h + exact frameMap_npA (st := st) ar s a _ _ _ _ _ _ _ _ _ _ _ _ (exAtA_idx0 e) hS1 x _ h + · rw [hr] at hdec + by_cases hhi : ∃ p mp asid, capA st s (exAtA e 0) = .HighVSpace p mp asid + · obtain ⟨p1, mp1, asid1, hx0c⟩ := hhi + exact J.frameMapHi_runA hA hM hPl hfm Q hlab K hcap hdev (exAtA_idx0 e) hx0c hdec + · exact J.frameMapS1_recA hA hM hPl hfm Q hlab K hcap hdev (exAtA_idx0 e) hS1 + (fun p mp asid h => hhi ⟨p, mp, asid, h⟩) hdec) + +/-- **`ARMASIDPoolAssign` end to end** (module docstring). -/ +theorem JSyscallA.asidAssignBodyA [PlainStore st] -- this body's case + (hSel : ∀ (info : msginfo.MessageInfo) (buf : U64) (r1 : cspace.Resolved) (e : Aeneas.Std.Array obj.Slot 3#usize) + (mi : Fl4SpecA64.MessageInfo) (sl : Fl4SpecA64.CslotPtr) (c : Fl4SpecA64.Cap) + (excaps : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) (buffer : Option Fl4SpecA64.ObjRef) (argsU : List U64), + msg.msg_info st s (L.curW s) = ok info → msg.lookup_ipc_buffer st s false (L.curW s) = ok buf → + cspace.lookup_slot st s (L.curW s) (L.mem s (regWA (L.curW s) layout.reg.CAP)) = ok (.Ok r1) → + ipc.lookup_extra_caps st s (L.curW s) buf info = ok (.Ok e) → + SiteA st Z OS s σ info buf r1.slot e mi sl c excaps buffer argsU → + info.label = fl4_abi.label.ARMASIDPoolAssign ∧ + ∃ p base, capA st s r1.slot = .AsidPool p base) : + syscall.dispatch_syscall st ar s a (syscallNumberA call) ⦃ r => + StepPostA (fun s a τ => A64RelS3 st AR ar hPl.ipa n Z.Cs s a τ m) + (fl4SettleDispatch pending (fl4DispatchSyscall (fl4HandleInvocationR (Z := Unit) rep) call)) σ r ⦄ := + J.sendBodyRunA (hCnt := hCnt) hA hM hPl hfm hIP rep call hc hFault hErr XP HEnv + (fun calling blocking donate _ info buf r1 e mi sl c excaps buffer argsU einfo ebuf elr eex Q r0 s0 a0 hdec => by + obtain ⟨hlab, p, base, hcap⟩ := hSel info buf r1 e mi sl c excaps buffer argsU einfo ebuf elr eex Q + refine ⟨fun x hx => ?_, fun hr => ?_⟩ + · have h := hdec + rw [hcap, decodePool_programA, hlab, hx] at h + exact asidAssign_npA (st := st) ar s a _ _ _ _ _ _ _ x _ h + · rw [hr] at hdec + exact J.asidAssign_runA hA hM hPl hfm Q hlab hcap hdec) + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3VsCloseA.lean b/proofs/Fl4ProofsA64/P3/Stage3VsCloseA.lean new file mode 100644 index 000000000..c78164291 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3VsCloseA.lean @@ -0,0 +1,100 @@ +/- +P3 (AArch64, merge line, D8 step 4 (b)): **closing the VSpace footprints into `JStage3A`** where no cap word is +written, and composing the frame-unmap arm. + +* `vsWord_s1A` / `vsWord_t2A`: an entry word of a Stage-1 / Stage-2 table of the joint relation (`Z.S1`, `Z.T2`) is a + `VsWordA` word (so the leaf the unmap tops already export, `UnmapRelA`'s `∃ p ∈ Tb, slot = peAF p i`, fits). +* **`JStage3A.of_leafWriteA`**: `of_vsWordsA` for one `VsWordA` word write `L.wr s x v` (the unmap leaf clear). +* **`frame_unmap_footA`**: `frame_unmap_shapeA` composed with `ept_unmap_footA` / `unmap_page_footA`: after the + Restart, the unmap walk's leaf entry (a stage-2 or stage-1 lookup result) and the invoked cap slot (`SlotWordA`, + closed by step 5's `of_capFrame`). +* A flush arm (`FlushFootA`) needs no lemma here: its post state is the Restart state, so `JStage3A.of_restartCurA` + is its closure. + +Pure facts; no existing statement changed; no named premise. +-/ +import Fl4ProofsA64.P3.Stage3VsFootFlushA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-- An entry word of a Stage-1 table of the joint relation is a `VsWordA` word. -/ +theorem vsWord_s1A {Z : XSetsA} {OS : Set U64} {s : S} {p i : U64} (hp : p ∈ Z.S1) (hi : i.val < 512) : + VsWordA st Z OS s (peAF p i) := + vsWord_entryA (Or.inr (Or.inl hp)) hi + +/-- An entry word of a Stage-2 table of the joint relation is a `VsWordA` word. -/ +theorem vsWord_t2A {Z : XSetsA} {OS : Set U64} {s : S} {p i : U64} (hp : p ∈ Z.T2) (hi : i.val < 512) : + VsWordA st Z OS s (peAF p i) := + vsWord_entryA (Or.inr (Or.inr (Or.inl hp))) hi + +/-- **`of_vsWordsA` for one word write** (the unmap leaf clear). -/ +theorem JStage3A.of_leafWriteA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J5 : JSlotA S} {Rj : Set U64 → JSlotA S} {s : S} + {a a' : A} {σ σ' : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7A st Rj) s a σ m) + {hw : HwRootA} {x v : U64} (hx : Aligned x) (hv : VsWordA st Z OS s x) + (HS : VsSpecA σ σ') + (hdp : ∀ p dev pg, σ.kheap p = some (.archObj (.dataPage dev pg)) → + σ'.kheap p = some (.archObj (.dataPage dev pg))) + (jx : JAtZAX (L := L) AR { Z with hw := hw } (L.wr s x v) a' σ' m) + (sys : SysInvA st H Z.Cs OS U Ac Pp Tb Lv ia ie (L.wr s x v)) + (vsOk' : UtVsOkA st H OS Z.V Pp Tb Lv (L.wr s x v)) + (bufVs' : ∀ b, BufW Bf b → ¬ VsR (L.ksb (L.wr s x v)) (utVsPtA st Pp Tb Lv (L.wr s x v)) b) + (sq : SchedQueuesInv st H.tcbs Z.Cs H.omega H (L.wr s x v) Z.Q Z.R) + (poolOff : PoolOff st H Z.Cs OS (L.wr s x v)) + (unmPt : UnmPtSpecA st { Z with hw := hw } m OS (L.wr s x v) σ') + (j5 : J5 { Z with hw := hw } H OS (L.wr s x v) σ' m) + (j7 : J7A st Rj { Z with hw := hw } H OS (L.wr s x v) σ' m) + (symL : SymL st H Z.Cs (L.wr s x v)) (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) : + JStage3A st AR { Z with hw := hw } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7A st Rj) (L.wr s x v) a' σ' + m := + J.of_vsWordsA (P := (· = x)) (writesIn_wr hx) (fun b hb => hb ▸ hv) HS hdp jx sys vsOk' bufVs' sq poolOff unmPt + j5 j7 symL hsub + +/-- **The `ARMPageUnmap` arm of `decode_frame`, composed**: after the Restart, one unmap walk's leaf entry (a stage-2 +or stage-1 lookup result, or nothing) and the invoked cap slot. -/ +theorem frame_unmap_footA {ar : machine.Arch A} {s : S} {a : A} {length slot buf : U64} {cap : capability.Cap} + {f : vspace.FrameCap} {call : Bool} {ex : Aeneas.Std.Array obj.Slot 3#usize} + (hf : vspace.as_frame cap = ok (some f)) (hx : Aligned slot) (hx8 : Aligned (wa slot 8#u64)) + (hal2 : ∀ s1 root bits lu, ept.lookup_slot st s1 root f.vaddr bits = ok lu → Aligned lu.slot) + (hal1 : ∀ s1 root lu, vspace.lookup_pt_slot st s1 root f.vaddr = ok lu → Aligned lu.slot) + {r : core.result.Result Unit error.Exc × S × A} + (h : vspace.decode_frame st ar s a fl4_abi.label.ARMPageUnmap length slot cap call buf ex = ok r) : + r.1 = .Ok () → ∃ s1 : S, sched.set_thread_state st s (L.curW s) sched.TS_RESTART = ok s1 ∧ + WritesIn st s1 r.2.1 (fun b => SlotWordA slot b ∨ + (∃ root bits lu, ept.lookup_slot st s1 root f.vaddr bits = ok lu ∧ b = lu.slot) ∨ + ∃ root lu, vspace.lookup_pt_slot st s1 root f.vaddr = ok lu ∧ b = lu.slot) := by + intro hr + obtain ⟨s1, s2, hs1, hu, hw⟩ := frame_unmap_shapeA hf hx hx8 h hr + refine ⟨s1, hs1, ?_⟩ + have hw' : WritesIn st s2 r.2.1 (fun b => SlotWordA slot b ∨ + (∃ root bits lu, ept.lookup_slot st s1 root f.vaddr bits = ok lu ∧ b = lu.slot) ∨ + ∃ root lu, vspace.lookup_pt_slot st s1 root f.vaddr = ok lu ∧ b = lu.slot) := + hw.mono (fun _ hb => Or.inl hb) + rcases hu with rfl | ⟨i, a1, -, hu⟩ | ⟨a1, hu⟩ + · exact hw' + · rcases ept_unmap_footA (fun root bits lu h => hal2 s1 root bits lu h) hu with e | ⟨root, bits, lu, hlu, w⟩ + · have e' : s2 = s1 := e + rw [e'] at hw'; exact hw' + · exact (w.mono (fun b hb => Or.inr (Or.inl ⟨root, bits, lu, hlu, hb⟩))).trans1 hw' + · rcases unmap_page_footA (fun root lu h => hal1 s1 root lu h) hu with e | ⟨root, lu, hlu, w⟩ + · have e' : s2 = s1 := e + rw [e'] at hw'; exact hw' + · exact (w.mono (fun b hb => Or.inr (Or.inr ⟨root, lu, hlu, hb⟩))).trans1 hw' + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3VsDecFactsA.lean b/proofs/Fl4ProofsA64/P3/Stage3VsDecFactsA.lean new file mode 100644 index 000000000..199a06dbb --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3VsDecFactsA.lean @@ -0,0 +1,83 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Plan B): **the Stage-1 map arms' arch decode from the run.** `frameMapS1_scopeA` and +`ptMapS1_scopeA` ask `vspace.decode_frame` / `vspace.decode_page_table` to succeed; here that comes from FL4's +`decode_invocation`. The other hypotheses of those theorems are case selectors or record facts. + +Named premises: [J] `JSyscallA`. New module. +-/ +import Fl4ProofsA64.P3.Stage3ScopeArmsA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {Clock : A → Fl4SpecA64.MachineState → Prop} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {ar : machine.Arch A} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + {info : msginfo.MessageInfo} {buf slot : U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} + {mi : Fl4SpecA64.MessageInfo} {sl : Fl4SpecA64.CslotPtr} {c : Fl4SpecA64.Cap} + {excaps : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)} {buffer : Option Fl4SpecA64.ObjRef} {argsU : List U64} + +theorem decodeFrame_programA {lbl len fp rt asid va : U64} {sz : capability.FrameSize} {dev mp ept : Bool} + {block call cd fi : Bool} : + syscall.decode_invocation st ar s a lbl len slot (.Frame fp sz rt dev mp ept asid va) block call cd fi buf extra = + vspace.decode_frame st ar s a lbl len slot (.Frame fp sz rt dev mp ept asid va) call buf extra := by + simp only [syscall.decode_invocation, vspace.decode, cur_thread_ok, bind_ok] + +theorem decodePt_programA {lbl len ptr asid va : U64} {mp : Bool} {block call cd fi : Bool} : + syscall.decode_invocation st ar s a lbl len slot (.PageTable ptr mp asid va) block call cd fi buf extra = + vspace.decode_page_table st ar s a lbl len slot (.PageTable ptr mp asid va) buf extra := by + simp only [syscall.decode_invocation, vspace.decode, cur_thread_ok, bind_ok] + +/-- **The Stage-1 frame map's member from FL4's successful decode** (module docstring). -/ +theorem JSyscallA.frameMapS1_runA (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (Q : SiteA st Z OS s σ info buf slot extra mi sl c excaps buffer argsU) + (hlab : info.label = fl4_abi.label.ARMPageMap) {fp asid0 va0 rt : U64} {sz : capability.FrameSize} + {dev mp bB call bD : Bool} {s3 : S} {a3 : A} (K : ArchKeepsA ar AR) + (hcap : capA st s slot = .Frame fp sz rt dev mp false asid0 va0) (FP : FrPlA sz fp mp asid0) {x0 : U64} + (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) + (hS1 : ∀ p l mp' asid va, capA st s x0 ≠ .Ept p l mp' asid va) + (hHi : ∀ p mp' asid, capA st s x0 ≠ .HighVSpace p mp' asid) + (hdec : syscall.decode_invocation st ar s a info.label (msgLenA info buf) slot (capA st s slot) bB call bD false + buf extra = ok (core.result.Result.Ok (), s3, a3)) : + ∃ Out, DispScope4A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ m Clock hPl.ipa info.label + (msgLenA info buf) slot bB call bD false buf extra Out := by + rw [hcap, decodeFrame_programA, hlab] at hdec + exact J.frameMapS1_scopeA hA hM hPl hfm Q hlab (K := K) (hcap := (capAtA_eq _ _).trans hcap) (FP := FP) + (call := call) (x0 := x0) (hx0 := hx0) (hS1 := fun _ _ _ _ _ => by rw [capAtA_eq]; exact hS1 _ _ _ _ _) + (hHi := fun _ _ _ => by rw [capAtA_eq]; exact hHi _ _ _) (hdec := hdec) (bB := bB) (bD := bD) (bF := false) + +/-- **The Stage-1 table map's member from FL4's successful decode** (module docstring). -/ +theorem JSyscallA.ptMapS1_runA (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) + (Q : SiteA st Z OS s σ info buf slot extra mi sl c excaps buffer argsU) + (hlab : info.label = fl4_abi.label.ARMPageTableMap) {ptr asid0 va0 : U64} {bB bC bD : Bool} {s3 : S} {a3 : A} + (hcap : capA st s slot = .PageTable ptr false asid0 va0) + (UP : UnmPtA Z.S1 Z.Y OS H.tcbs (L.mem s) σ ptr) (hS2 : ptr ∉ S2PgE Z.X Z.T2) (hY : ptr ∉ Z.Y) {x0 : U64} + (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) + (hS1 : ∀ p l mp' asid va, capA st s x0 ≠ .Ept p l mp' asid va) + (hHi : ∀ p mp' asid, capA st s x0 ≠ .HighVSpace p mp' asid) + (hdec : syscall.decode_invocation st ar s a info.label (msgLenA info buf) slot (capA st s slot) bB bC bD false + buf extra = ok (core.result.Result.Ok (), s3, a3)) : + ∃ Out, DispScope4A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ m Clock hPl.ipa info.label + (msgLenA info buf) slot bB bC bD false buf extra Out := by + rw [hcap, decodePt_programA, hlab] at hdec + exact J.ptMapS1_scopeA hA hM hPl hfm Q hlab (hcap := (capAtA_eq _ _).trans hcap) (UP := UP) (hS2 := hS2) + (hY := hY) (x0 := x0) (hx0 := hx0) (hS1 := fun _ _ _ _ _ => by rw [capAtA_eq]; exact hS1 _ _ _ _ _) + (hHi := fun _ _ _ => by rw [capAtA_eq]; exact hHi _ _ _) (hdec := hdec) (bB := bB) (bC := bC) (bD := bD) + (bF := false) + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3VsFootA.lean b/proofs/Fl4ProofsA64/P3/Stage3VsFootA.lean new file mode 100644 index 000000000..1cf753042 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3VsFootA.lean @@ -0,0 +1,769 @@ +/- +P3 (AArch64, merge line, D8 step 4, Architect option (a)): **per-arm write footprints of the VSpace operations**, +and the `of_vsWrite` wrapper they feed. + +* The footprints are pure program facts (`op … = ok r → …`); they use no refinement hypothesis, so each one is + conjoined with the existing arm theorem by `spec_with_eqA`. `ArchTopPostA` / `ArchDecTopPostA` are untouched. +* A mapping arm writes, after the caller's Restart (`JStage3A.of_restartCurA`), the invoked cap slot (`SlotWordA`: + two words, the cap-write family of D8 step 5) and table words. A flush arm writes nothing after the Restart. +* **`VsWordA`**: a word of a scheduler-or-VSpace table page (`SchedPageA`) or of the ASID table. **`JStage3A.of_vsWordsA`** + is `of_vsWrite` for such words: its `hP` comes from the J7 link and its `hbf` from `JStage3A.bufOff`. + +No existing statement changed. +-/ +import Fl4ProofsA64.P3.Stage3VsWriteA +import Fl4ProofsA64.P1.MdbRstD +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-- A total spec together with an equational fact about the same run. -/ +theorem spec_with_eqA {α : Type} {x : Result α} {P Q : α → Prop} (h : x ⦃ P ⦄) (q : ∀ r, x = ok r → Q r) : + x ⦃ r => P r ∧ Q r ⦄ := by + obtain ⟨r, e, hp⟩ := spec_ex_fd h + rw [e] + exact (WP.spec_ok _).2 ⟨hp, q r e⟩ + +/-- The two words of a cap slot. -/ +def SlotWordA (slot b : U64) : Prop := b = slot ∨ b = wa slot 8#u64 + +/-- A word of a scheduler-or-VSpace table page, or of the ASID table. -/ +def VsWordA (st : machine.Store S) [L : LawfulStore st] (Z : XSetsA) (OS : Set U64) (s : S) (b : U64) : Prop := + (∃ p, SchedPageA st Z OS s p ∧ ∃ o : U64, o.val < 4096 ∧ o.val % 8 = 0 ∧ b = wa p o) ∨ + ∃ i : U64, i.val < 128 ∧ b = atAF (L.ksb s) i + +/-- An entry word of a table page is a `VsWordA` word. -/ +theorem vsWord_entryA {Z : XSetsA} {OS : Set U64} {s : S} {p i : U64} (hp : SchedPageA st Z OS s p) + (hi : i.val < 512) : VsWordA st Z OS s (peAF p i) := by + refine Or.inl ⟨p, hp, core.num.U64.wrapping_mul i 8#u64, ?_, ?_, rfl⟩ + all_goals rw [wm8_valA hi]; omega + +/-- **`of_vsWrite` for `VsWordA` write sets** (module docstring). -/ +theorem JStage3A.of_vsWordsA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J5 : JSlotA S} {Rj : Set U64 → JSlotA S} {s s' : S} + {a a' : A} {σ σ' : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7A st Rj) s a σ m) + {hw : HwRootA} {P : U64 → Prop} (w : WritesIn st s s' P) (hP : ∀ b, P b → VsWordA st Z OS s b) + (HS : VsSpecA σ σ') + (hdp : ∀ p dev pg, σ.kheap p = some (.archObj (.dataPage dev pg)) → + σ'.kheap p = some (.archObj (.dataPage dev pg))) + (jx : JAtZAX (L := L) AR { Z with hw := hw } s' a' σ' m) + (sys : SysInvA st H Z.Cs OS U Ac Pp Tb Lv ia ie s') + (vsOk' : UtVsOkA st H OS Z.V Pp Tb Lv s') + (bufVs' : ∀ b, BufW Bf b → ¬ VsR (L.ksb s') (utVsPtA st Pp Tb Lv s') b) + (sq : SchedQueuesInv st H.tcbs Z.Cs H.omega H s' Z.Q Z.R) + (poolOff : PoolOff st H Z.Cs OS s') + (unmPt : UnmPtSpecA st { Z with hw := hw } m OS s' σ') + (j5 : J5 { Z with hw := hw } H OS s' σ' m) + (j7 : J7A st Rj { Z with hw := hw } H OS s' σ' m) + (symL : SymL st H Z.Cs s') (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) : + JStage3A st AR { Z with hw := hw } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7A st Rj) s' a' σ' m := by + refine J.of_vsWrite w (fun Pt Ub _ link b hb => ?_) (fun b hb => ?_) HS hdp jx sys vsOk' bufVs' sq poolOff unmPt j5 j7 symL hsub + · rcases hP b hb with ⟨p, hp, o, h1, h2, rfl⟩ | ⟨i, hi, rfl⟩ + · exact Or.inl ⟨p, link p hp, o, h1, h2, rfl⟩ + · exact vsW_asidA hi + · rcases hP b hb with ⟨p, hp, o, h1, h2, rfl⟩ | ⟨i, hi, rfl⟩ + · exact fun h => J.bufOff.page _ h p hp o h1 h2 rfl + · exact vsW_asid_bufA J.sr.place J.ipcPlace J.sr.ksFit hi + +/-! ## Primitive footprints -/ + +theorem writesIn_writeA {s s' : S} {x v : U64} (hx : Aligned x) (h : st.write s x v = ok s') : + WritesIn st s s' (fun b => b = x) := by + rw [L.write_eq] at h + rw [← ok_inj_ff h] + exact writesIn_wr hx + +theorem writesIn_setCapA {s s' : S} {x : U64} {c : capability.Cap} (hx : Aligned x) (hx8 : Aligned (wa x 8#u64)) + (h : obj.Slot.set_cap st x s c = ok s') : WritesIn st s s' (SlotWordA x) := by + have := obj.Slot.set_cap.wrx (st := st) (s := s) hx hx8 c + rw [h, WP.spec_ok] at this + exact this + +theorem writesIn_zeroPageA {s s' : S} {p : U64} (hp : Aligned p) (hfit : p.val + 4096 ≤ 2 ^ 64) + (h : st.zero s p 4096#u64 = ok s') : + WritesIn st s s' (fun b => p.val ≤ b.val ∧ b.val < p.val + 4096) := by + rw [L.zero_eq] at h + rw [← ok_inj_ff h] + refine ⟨L.ksb_zr _ _ _, L.cpu_zr _ _ _, fun b hb hn => ?_⟩ + have e : (4096#u64 : U64).val = 4096 := by simp + exact LawfulStore.mem_zr_out hp (by rw [e]) (by rw [e]; exact hfit) hb (by rw [e]; omega) + +/-! ## The arms -/ + +/-- **`ept.map_table`**: after the Restart, the invoked slot and the looked-up stage-2 entry. -/ +theorem ept_map_table_footA {ar : machine.Arch A} {s : S} {a : A} {root asid ipa pt slot : U64} + (hx : Aligned slot) (hx8 : Aligned (wa slot 8#u64)) + (hal : ∀ bits lu, ar.s2_ipa_bits a = ok bits → ept.lookup_slot st s root ipa bits = ok lu → Aligned lu.slot) + {r : core.result.Result Unit error.Exc × S × A} (h : ept.map_table st ar s a root asid ipa pt slot = ok r) : + r.1 = .Ok () → ∃ bits lu s1, ar.s2_ipa_bits a = ok bits ∧ ept.lookup_slot st s root ipa bits = ok lu ∧ + sched.set_thread_state st s (L.curW s) sched.TS_RESTART = ok s1 ∧ + WritesIn st s1 r.2.1 (fun b => SlotWordA slot b ∨ b = lu.slot) := by + intro hr + unfold ept.map_table at h + obtain ⟨bits, hbits, h⟩ := bind_eq_ok h + obtain ⟨i, -, h⟩ := bind_eq_ok h + split at h + · obtain ⟨e, he, h⟩ := bind_eq_ok h + rw [← ok_inj_ff h] at hr + simp only [error.invalid_arg] at he + rw [← ok_inj_ff he] at hr + cases hr + obtain ⟨lu, hlu, h⟩ := bind_eq_ok h + split at h + · rw [← ok_inj_ff h] at hr; cases hr + obtain ⟨i1, -, h⟩ := bind_eq_ok h + obtain ⟨i2, -, h⟩ := bind_eq_ok h + obtain ⟨i3, -, h⟩ := bind_eq_ok h + split at h + · rw [← ok_inj_ff h] at hr; cases hr + obtain ⟨i4, -, h⟩ := bind_eq_ok h + obtain ⟨i5, -, h⟩ := bind_eq_ok h + obtain ⟨i6, -, h⟩ := bind_eq_ok h + obtain ⟨i7, -, h⟩ := bind_eq_ok h + obtain ⟨base, -, h⟩ := bind_eq_ok h + obtain ⟨ct, hct, h⟩ := bind_eq_ok h + rw [cur_thread_ok] at hct + obtain ⟨s1, hs1, h⟩ := bind_eq_ok h + obtain ⟨s2, hs2, h⟩ := bind_eq_ok h + obtain ⟨i8, -, h⟩ := bind_eq_ok h + obtain ⟨i9, -, h⟩ := bind_eq_ok h + obtain ⟨s3, hs3, h⟩ := bind_eq_ok h + rw [← ok_inj_ff h] + rw [← ok_inj_ff hct] at hs1 + exact ⟨bits, lu, s1, hbits, hlu, hs1, + (writesIn_setCapA hx hx8 hs2).trans (writesIn_writeA (hal bits lu hbits hlu) hs3)⟩ + +/-- **`vspace.map_high_page_table`**: after the Restart, the invoked slot and the looked-up entry. -/ +theorem map_high_pt_footA {s : S} {slot ptr vaddr : U64} {vcap : capability.Cap} + (hx : Aligned slot) (hx8 : Aligned (wa slot 8#u64)) + (hal : ∀ root lu, vspace.lookup_pt_slot st s root vaddr = ok lu → Aligned lu.slot) + {r : core.result.Result Unit error.Exc × S} (h : vspace.map_high_page_table st s slot ptr vcap vaddr = ok r) : + r.1 = .Ok () → ∃ root lu s1, vspace.lookup_pt_slot st s root vaddr = ok lu ∧ + sched.set_thread_state st s (L.curW s) sched.TS_RESTART = ok s1 ∧ + WritesIn st s1 r.2 (fun b => SlotWordA slot b ∨ b = lu.slot) := by + intro hr + unfold vspace.map_high_page_table at h + obtain ⟨ct, hct, h⟩ := bind_eq_ok h + rw [cur_thread_ok] at hct + obtain ⟨rc, -, h⟩ := bind_eq_ok h + obtain ⟨cf, hcf, h⟩ := bind_eq_ok h + rcases cf with ⟨root, asid⟩ | residual + swap + · rcases residual with v | e + · cases v + · have h0 : (do + let r1 ← core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual Unit + (core.convert.FromSame error.Exc) (core.result.Result.Err e) + ok (r1, s)) = ok r := h + obtain ⟨y, hy, h1⟩ := bind_eq_ok h0 + unfold core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual at hy + obtain ⟨f, -, hf⟩ := bind_eq_ok hy + rw [← ok_inj_ff h1, ← ok_inj_ff hf] at hr; cases hr + try dsimp only at h + split at h + · obtain ⟨e, he, h⟩ := bind_eq_ok h + simp only [error.invalid_arg] at he + rw [← ok_inj_ff h, ← ok_inj_ff he] at hr; cases hr + split at h + · obtain ⟨e, he, h⟩ := bind_eq_ok h + simp only [error.invalid_arg] at he + rw [← ok_inj_ff h, ← ok_inj_ff he] at hr; cases hr + obtain ⟨lu, hlu, h⟩ := bind_eq_ok h + split at h + · rw [← ok_inj_ff h] at hr; cases hr + split at h + · rw [← ok_inj_ff h] at hr; cases hr + obtain ⟨i, -, h⟩ := bind_eq_ok h + obtain ⟨i1, -, h⟩ := bind_eq_ok h + obtain ⟨i2, -, h⟩ := bind_eq_ok h + split at h + · rw [← ok_inj_ff h] at hr; cases hr + obtain ⟨i3, -, h⟩ := bind_eq_ok h + obtain ⟨i4, -, h⟩ := bind_eq_ok h + obtain ⟨i5, -, h⟩ := bind_eq_ok h + obtain ⟨i6, -, h⟩ := bind_eq_ok h + obtain ⟨base, -, h⟩ := bind_eq_ok h + obtain ⟨s1, hs1, h⟩ := bind_eq_ok h + obtain ⟨s2, hs2, h⟩ := bind_eq_ok h + obtain ⟨i7, -, h⟩ := bind_eq_ok h + obtain ⟨i8, -, h⟩ := bind_eq_ok h + obtain ⟨s3, hs3, h⟩ := bind_eq_ok h + rw [← ok_inj_ff h] + rw [← ok_inj_ff hct] at hs1 + exact ⟨root, lu, s1, hlu, hs1, (writesIn_setCapA hx hx8 hs2).trans (writesIn_writeA (hal root lu hlu) hs3)⟩ + +/-- **`ept.unmap_table`**: nothing, or the one entry the walk found. -/ +theorem ept_unmap_table_footA {ar : machine.Arch A} {s : S} {a : A} {asid ipa pt : U64} + (hal : ∀ table shift mask rem slot, ept.unmap_table_loop st s ipa pt table shift mask rem = ok (some slot) → + Aligned slot) + {r : S × A} (h : ept.unmap_table st ar s a asid ipa pt = ok r) : + r.1 = s ∨ ∃ table shift mask rem slot, ept.unmap_table_loop st s ipa pt table shift mask rem = ok (some slot) ∧ + WritesIn st s r.1 (fun b => b = slot) := by + unfold ept.unmap_table at h + obtain ⟨rf, -, h⟩ := bind_eq_ok h + rcases rf with r1 | e + swap + · exact Or.inl (by rw [← ok_inj_ff h]) + try dsimp only at h + obtain ⟨i, -, h⟩ := bind_eq_ok h + obtain ⟨g, -, h⟩ := bind_eq_ok h + obtain ⟨rem, -, h⟩ := bind_eq_ok h + obtain ⟨found, hf, h⟩ := bind_eq_ok h + rcases found with _ | slot + · exact Or.inl (by rw [← ok_inj_ff h]) + try dsimp only at h + obtain ⟨s1, hs1, h⟩ := bind_eq_ok h + obtain ⟨a1, -, h⟩ := bind_eq_ok h + rw [← ok_inj_ff h] + exact Or.inr ⟨_, _, _, _, slot, hf, writesIn_writeA (hal _ _ _ _ _ hf) hs1⟩ + +/-- **`ept.unmap`**: nothing, or the looked-up leaf entry. -/ +theorem ept_unmap_footA {ar : machine.Arch A} {s : S} {a : A} {asid ipa ptr shift : U64} {tb : Bool} + (hal : ∀ root bits lu, ept.lookup_slot st s root ipa bits = ok lu → Aligned lu.slot) + {r : S × A} (h : ept.unmap st ar s a asid ipa ptr shift tb = ok r) : + r.1 = s ∨ ∃ root bits lu, ept.lookup_slot st s root ipa bits = ok lu ∧ + WritesIn st s r.1 (fun b => b = lu.slot) := by + unfold ept.unmap at h + obtain ⟨rf, -, h⟩ := bind_eq_ok h + rcases rf with r1 | e + swap + · exact Or.inl (by rw [← ok_inj_ff h]) + try dsimp only at h + obtain ⟨i, -, h⟩ := bind_eq_ok h + obtain ⟨lu, hlu, h⟩ := bind_eq_ok h + split at h + · exact Or.inl (by rw [← ok_inj_ff h]) + obtain ⟨e, -, h⟩ := bind_eq_ok h + obtain ⟨b, -, h⟩ := bind_eq_ok h + split at h + swap + · exact Or.inl (by rw [← ok_inj_ff h]) + obtain ⟨i1, -, h⟩ := bind_eq_ok h + obtain ⟨i2, -, h⟩ := bind_eq_ok h + split at h + · exact Or.inl (by rw [← ok_inj_ff h]) + obtain ⟨s1, hs1, h⟩ := bind_eq_ok h + obtain ⟨a1, -, h⟩ := bind_eq_ok h + rw [← ok_inj_ff h] + exact Or.inr ⟨r1, i, lu, hlu, writesIn_writeA (hal _ _ _ hlu) hs1⟩ + +/-- **`vspace.unmap_page`**: nothing, or the looked-up page entry. -/ +theorem unmap_page_footA {ar : machine.Arch A} {s : S} {a : A} {size : capability.FrameSize} {asid vaddr pptr : U64} + (hal : ∀ root lu, vspace.lookup_pt_slot st s root vaddr = ok lu → Aligned lu.slot) + {r : S × A} (h : vspace.unmap_page st ar s a size asid vaddr pptr = ok r) : + r.1 = s ∨ ∃ root lu, vspace.lookup_pt_slot st s root vaddr = ok lu ∧ + WritesIn st s r.1 (fun b => b = lu.slot) := by + unfold vspace.unmap_page at h + obtain ⟨o, -, h⟩ := bind_eq_ok h + rcases o with _ | ⟨root, high⟩ + · exact Or.inl (by rw [← ok_inj_ff h]) + try dsimp only at h + obtain ⟨lu, hlu, h⟩ := bind_eq_ok h + obtain ⟨i, -, h⟩ := bind_eq_ok h + split at h + · exact Or.inl (by rw [← ok_inj_ff h]) + obtain ⟨e, -, h⟩ := bind_eq_ok h + obtain ⟨b, -, h⟩ := bind_eq_ok h + split at h + swap + · exact Or.inl (by rw [← ok_inj_ff h]) + obtain ⟨i1, -, h⟩ := bind_eq_ok h + obtain ⟨i2, -, h⟩ := bind_eq_ok h + split at h + · exact Or.inl (by rw [← ok_inj_ff h]) + obtain ⟨s1, hs1, h⟩ := bind_eq_ok h + have key : r.1 = s1 := by + split at h + all_goals + obtain ⟨a1, -, h⟩ := bind_eq_ok h + rw [← ok_inj_ff h] + rw [key] + exact Or.inr ⟨root, lu, hlu, writesIn_writeA (hal _ _ hlu) hs1⟩ + +/-- **`vspace.unmap_page_table`**: nothing, or one entry found by a stage-2 or stage-1 walk. -/ +theorem unmap_page_table_footA {ar : machine.Arch A} {s : S} {a : A} {asid vaddr pt : U64} + (hal2 : ∀ table shift mask rem slot, ept.unmap_table_loop st s vaddr pt table shift mask rem = ok (some slot) → + Aligned slot) + (hal1 : ∀ table shift level slot, vspace.unmap_page_table_loop st s vaddr pt table shift level = ok (some slot) → + Aligned slot) + {r : S × A} (h : vspace.unmap_page_table st ar s a asid vaddr pt = ok r) : + r.1 = s ∨ ∃ slot, ((∃ table shift mask rem, ept.unmap_table_loop st s vaddr pt table shift mask rem = + ok (some slot)) ∨ + ∃ table shift level, vspace.unmap_page_table_loop st s vaddr pt table shift level = ok (some slot)) ∧ + WritesIn st s r.1 (fun b => b = slot) := by + unfold vspace.unmap_page_table at h + obtain ⟨rf, -, h⟩ := bind_eq_ok h + rcases rf with r1 | e + swap + · exact Or.inl (by rw [← ok_inj_ff h]) + try dsimp only at h + obtain ⟨tag, -, h⟩ := bind_eq_ok h + split at h + · rcases ept_unmap_table_footA hal2 h with e | ⟨t, sh, mk, rm, slot, hf, w⟩ + · exact Or.inl e + · exact Or.inr ⟨slot, Or.inl ⟨t, sh, mk, rm, hf⟩, w⟩ + obtain ⟨i, -, h⟩ := bind_eq_ok h + obtain ⟨root, -, h⟩ := bind_eq_ok h + obtain ⟨⟨table, shift, level⟩, -, h⟩ := bind_eq_ok h + try dsimp only at h + obtain ⟨found, hf, h⟩ := bind_eq_ok h + rcases found with _ | slot + · exact Or.inl (by rw [← ok_inj_ff h]) + try dsimp only at h + obtain ⟨s1, hs1, h⟩ := bind_eq_ok h + have key : r.1 = s1 := by + split at h + all_goals + obtain ⟨a1, -, h⟩ := bind_eq_ok h + rw [← ok_inj_ff h] + rw [key] + exact Or.inr ⟨slot, Or.inr ⟨_, _, _, hf⟩, writesIn_writeA (hal1 _ _ _ _ hf) hs1⟩ + +/-- **`ept.install_s2_leaf`**: only the looked-up entry (break-before-make writes it twice). -/ +theorem install_s2_leaf_footA {ar : machine.Arch A} {s : S} {a : A} {lu : ept.S2Slot} + {e root asid ipa ptr attr : U64} {dev : Bool} (hal : Aligned lu.slot) + {r : S × A} (h : ept.install_s2_leaf st ar s a lu e root asid ipa ptr dev attr = ok r) : + WritesIn st s r.1 (fun b => b = lu.slot) := by + have two : ∀ s1 s2 : S, ∀ v w : U64, st.write s lu.slot v = ok s1 → st.write s1 lu.slot w = ok s2 → + WritesIn st s s2 (fun b => b = lu.slot) := fun s1 s2 v w h1 h2 => + ((writesIn_writeA hal h1).trans (writesIn_writeA hal h2)).mono (fun _ hb => hb.elim id id) + unfold ept.install_s2_leaf at h + obtain ⟨old, -, h⟩ := bind_eq_ok h + obtain ⟨b, -, h⟩ := bind_eq_ok h + split at h + · split at h + · obtain ⟨i, -, h⟩ := bind_eq_ok h + obtain ⟨i1, -, h⟩ := bind_eq_ok h + obtain ⟨i2, -, h⟩ := bind_eq_ok h + split at h + · obtain ⟨s1, hs1, h⟩ := bind_eq_ok h + obtain ⟨a1, -, h⟩ := bind_eq_ok h + rw [← ok_inj_ff h] + exact writesIn_writeA hal hs1 + · obtain ⟨s1, hs1, h⟩ := bind_eq_ok h + obtain ⟨a1, -, h⟩ := bind_eq_ok h + obtain ⟨a2, -, h⟩ := bind_eq_ok h + obtain ⟨s2, hs2, h⟩ := bind_eq_ok h + rw [← ok_inj_ff h] + exact two _ _ _ _ hs1 hs2 + · obtain ⟨s1, hs1, h⟩ := bind_eq_ok h + obtain ⟨a1, -, h⟩ := bind_eq_ok h + obtain ⟨a2, -, h⟩ := bind_eq_ok h + obtain ⟨s2, hs2, h⟩ := bind_eq_ok h + rw [← ok_inj_ff h] + exact two _ _ _ _ hs1 hs2 + · obtain ⟨a1, -, h⟩ := bind_eq_ok h + obtain ⟨s1, hs1, h⟩ := bind_eq_ok h + rw [← ok_inj_ff h] + exact writesIn_writeA hal hs1 + +/-- The shared tail of `ept.map_frame`: the slot write, then the leaf install. -/ +theorem map_frame_tail_footA {ar : machine.Arch A} {s1 : S} {a : A} {lu : ept.S2Slot} {slot : U64} + {e root asid ipa ptr attr : U64} {dev : Bool} {c : capability.Cap} + (hx : Aligned slot) (hx8 : Aligned (wa slot 8#u64)) (hal : Aligned lu.slot) + {r : core.result.Result Unit error.Exc × S × A} + (h : (do + let s2 ← obj.Slot.set_cap st slot s1 c + let (s3, a1) ← ept.install_s2_leaf st ar s2 a lu e root asid ipa ptr dev attr + ok (core.result.Result.Ok (), s3, a1)) = ok r) : + WritesIn st s1 r.2.1 (fun b => SlotWordA slot b ∨ b = lu.slot) := by + obtain ⟨s2, hs2, h⟩ := bind_eq_ok h + obtain ⟨⟨s3, a1⟩, hs3, h⟩ := bind_eq_ok h + rw [← ok_inj_ff h] + exact (writesIn_setCapA hx hx8 hs2).trans (install_s2_leaf_footA hal hs3) + +/-- **`ept.map_frame`**: after the Restart, the invoked slot and the looked-up leaf entry. -/ +theorem ept_map_frame_footA {ar : machine.Arch A} {s : S} {a : A} {root asid ipa vm attr ptr rights masid mva slot : U64} + {size : capability.FrameSize} {dev : Bool} + (hx : Aligned slot) (hx8 : Aligned (wa slot 8#u64)) + (hal : ∀ bits lu, ept.lookup_slot st s root ipa bits = ok lu → Aligned lu.slot) + {r : core.result.Result Unit error.Exc × S × A} + (h : ept.map_frame st ar s a root asid ipa vm attr ptr size rights dev masid mva slot = ok r) : + r.1 = .Ok () → ∃ bits lu s1, ept.lookup_slot st s root ipa bits = ok lu ∧ + sched.set_thread_state st s (L.curW s) sched.TS_RESTART = ok s1 ∧ + WritesIn st s1 r.2.1 (fun b => SlotWordA slot b ∨ b = lu.slot) := by + intro hr + have bad2 : ∀ x : core.result.Result Unit error.Exc, + error.invalid_arg Unit 0#u64 = ok x → (x, s, a) = r → False := fun x hx e => by + simp only [error.invalid_arg] at hx + rw [← e, ← ok_inj_ff hx] at hr; cases hr + unfold ept.map_frame at h + obtain ⟨shift, -, h⟩ := bind_eq_ok h + obtain ⟨bits, -, h⟩ := bind_eq_ok h + obtain ⟨i, -, h⟩ := bind_eq_ok h + obtain ⟨i1, -, h⟩ := bind_eq_ok h + obtain ⟨i2, -, h⟩ := bind_eq_ok h + obtain ⟨i3, -, h⟩ := bind_eq_ok h + split at h + · rw [← ok_inj_ff h] at hr; cases hr + have tail : ∀ (lu : ept.S2Slot), ept.lookup_slot st s root ipa bits = ok lu → ∀ (e : U64) (ct : U64), + sched.cur_thread st s = ok ct → ∀ s1, sched.set_thread_state st s ct sched.TS_RESTART = ok s1 → + ∀ c : capability.Cap, (do + let s2 ← obj.Slot.set_cap st slot s1 c + let (s3, a1) ← ept.install_s2_leaf st ar s2 a lu e root asid ipa ptr dev attr + ok (core.result.Result.Ok (), s3, a1)) = ok r → + ∃ bits lu s1, ept.lookup_slot st s root ipa bits = ok lu ∧ + sched.set_thread_state st s (L.curW s) sched.TS_RESTART = ok s1 ∧ + WritesIn st s1 r.2.1 (fun b => SlotWordA slot b ∨ b = lu.slot) := by + intro lu hlu e ct hct s1 hs1 c hc + rw [cur_thread_ok] at hct + rw [← ok_inj_ff hct] at hs1 + exact ⟨bits, lu, s1, hlu, hs1, map_frame_tail_footA hx hx8 (hal _ _ hlu) hc⟩ + split at h + · split at h + · obtain ⟨x, hx', h⟩ := bind_eq_ok h + simp only [error.invalid_cap] at hx' + rw [← ok_inj_ff h, ← ok_inj_ff hx'] at hr; cases hr + split at h + · obtain ⟨x, hx', h⟩ := bind_eq_ok h + exact (bad2 x hx' (ok_inj_ff h)).elim + obtain ⟨lu, hlu, h⟩ := bind_eq_ok h + split at h + · rw [← ok_inj_ff h] at hr; cases hr + obtain ⟨cr, -, h⟩ := bind_eq_ok h + obtain ⟨i4, -, h⟩ := bind_eq_ok h + obtain ⟨e, -, h⟩ := bind_eq_ok h + obtain ⟨ct, hct, h⟩ := bind_eq_ok h + obtain ⟨s1, hs1, h⟩ := bind_eq_ok h + split at h + all_goals try split at h + all_goals exact tail lu hlu e ct hct s1 hs1 _ h + · obtain ⟨i4, -, h⟩ := bind_eq_ok h + obtain ⟨i5, -, h⟩ := bind_eq_ok h + obtain ⟨i6, -, h⟩ := bind_eq_ok h + obtain ⟨last, -, h⟩ := bind_eq_ok h + obtain ⟨i7, -, h⟩ := bind_eq_ok h + split at h + · obtain ⟨x, hx', h⟩ := bind_eq_ok h + exact (bad2 x hx' (ok_inj_ff h)).elim + split at h + · obtain ⟨x, hx', h⟩ := bind_eq_ok h + exact (bad2 x hx' (ok_inj_ff h)).elim + obtain ⟨lu, hlu, h⟩ := bind_eq_ok h + split at h + · rw [← ok_inj_ff h] at hr; cases hr + obtain ⟨cr, -, h⟩ := bind_eq_ok h + obtain ⟨i8, -, h⟩ := bind_eq_ok h + obtain ⟨e, -, h⟩ := bind_eq_ok h + obtain ⟨ct, hct, h⟩ := bind_eq_ok h + obtain ⟨s1, hs1, h⟩ := bind_eq_ok h + split at h + all_goals try split at h + all_goals exact tail lu hlu e ct hct s1 hs1 _ h + +/-- **The `ARMPageTableUnmap` arm of `decode_page_table`**: after the Restart, the entry the unmap walk found, the +unmapped table's page, and the invoked slot. -/ +theorem pt_unmap_footA {ar : machine.Arch A} {s : S} {a : A} {length slot buf ptr asid vaddr : U64} {mapped : Bool} + {ex : Aeneas.Std.Array obj.Slot 3#usize} + (hx : Aligned slot) (hx8 : Aligned (wa slot 8#u64)) (hp : Aligned ptr) (hfit : ptr.val + 4096 ≤ 2 ^ 64) + (hal2 : ∀ s1 table shift mask rem x, ept.unmap_table_loop st s1 vaddr ptr table shift mask rem = ok (some x) → + Aligned x) + (hal1 : ∀ s1 table shift level x, vspace.unmap_page_table_loop st s1 vaddr ptr table shift level = ok (some x) → + Aligned x) + {r : core.result.Result Unit error.Exc × S × A} + (h : vspace.decode_page_table st ar s a fl4_abi.label.ARMPageTableUnmap length slot + (.PageTable ptr mapped asid vaddr) buf ex = ok r) : + r.1 = .Ok () → ∃ s1, sched.set_thread_state st s (L.curW s) sched.TS_RESTART = ok s1 ∧ + ∃ s2 : S, (s2 = s1 ∨ ∃ x, ((∃ table shift mask rem, ept.unmap_table_loop st s1 vaddr ptr table shift mask rem = + ok (some x)) ∨ + ∃ table shift level, vspace.unmap_page_table_loop st s1 vaddr ptr table shift level = ok (some x)) ∧ + WritesIn st s1 s2 (fun b => b = x)) ∧ + WritesIn st s2 r.2.1 (fun b => (ptr.val ≤ b.val ∧ b.val < ptr.val + 4096) ∨ SlotWordA slot b) := by + intro hr + unfold vspace.decode_page_table at h + obtain ⟨ct, hct, h⟩ := bind_eq_ok h + rw [cur_thread_ok] at hct + try dsimp only at h + rw [if_pos rfl] at h + obtain ⟨fin, -, h⟩ := bind_eq_ok h + split at h + swap + · rw [← ok_inj_ff h] at hr; cases hr + obtain ⟨s1, hs1, h⟩ := bind_eq_ok h + rw [← ok_inj_ff hct] at hs1 + obtain ⟨⟨s2, a1⟩, hs2, h⟩ := bind_eq_ok h + try dsimp only at h + obtain ⟨s3, hs3, h⟩ := bind_eq_ok h + rw [← ok_inj_ff h] + refine ⟨s1, hs1, ?_⟩ + have fin3 := writesIn_setCapA hx hx8 hs3 + split at hs2 + · obtain ⟨⟨s4, a2⟩, hu, hs2⟩ := bind_eq_ok hs2 + try dsimp only at hs2 + obtain ⟨s5, hz, hs2⟩ := bind_eq_ok hs2 + have e := ok_inj_ff hs2 + simp only [Prod.mk.injEq] at e + obtain ⟨rfl, -⟩ := e + refine ⟨s4, ?_, (writesIn_zeroPageA hp hfit hz).trans fin3⟩ + rcases unmap_page_table_footA (hal2 s1) (hal1 s1) hu with e | ⟨x, hx', w⟩ + · exact Or.inl e + · exact Or.inr ⟨x, hx', w⟩ + · have e := ok_inj_ff hs2 + simp only [Prod.mk.injEq] at e + obtain ⟨rfl, -⟩ := e + exact ⟨s1, Or.inl rfl, fin3.mono (fun _ h => Or.inr h)⟩ + +/-- An error-returning step of a decoder: the outcome is not `Ok`. -/ +theorem errStep_footA {α : Type} {f : Result (core.result.Result Unit error.Exc)} {s : S} {a : α} + (hf : ∀ x, f = ok x → x ≠ .Ok ()) {r : core.result.Result Unit error.Exc × S × α} + (h : (do let x ← f; ok (x, s, a)) = ok r) (hr : r.1 = .Ok ()) : False := by + obtain ⟨x, hx, h⟩ := bind_eq_ok h + rw [← ok_inj_ff h] at hr + exact hf x hx hr + +theorem invCap_neA {n : U64} : ∀ x, error.invalid_cap Unit n = ok x → x ≠ .Ok () := fun x h e => by + simp only [error.invalid_cap] at h; rw [← ok_inj_ff h] at e; cases e + +theorem invArg_neA {n : U64} : ∀ x, error.invalid_arg Unit n = ok x → x ≠ .Ok () := fun x h e => by + simp only [error.invalid_arg] at h; rw [← ok_inj_ff h] at e; cases e + +theorem illegal_neA : ∀ x, error.illegal Unit = ok x → x ≠ .Ok () := fun x h e => by + simp only [error.illegal] at h; rw [← ok_inj_ff h] at e; cases e + +theorem trunc_neA : ∀ x, error.truncated Unit = ok x → x ≠ .Ok () := fun x h e => by + simp only [error.truncated] at h; rw [← ok_inj_ff h] at e; cases e + +theorem range_neA {lo hi : U64} : ∀ x, error.range Unit lo hi = ok x → x ≠ .Ok () := fun x h e => by + simp only [error.range] at h; rw [← ok_inj_ff h] at e; cases e + +/-- **`decode_vspace_flush`** (the VSpace-root flush arm): an accepted flush writes only the caller's Restart. -/ +theorem vsFlush_footA {ar : machine.Arch A} {s : S} {a : A} {lbl length buf : U64} {cap : capability.Cap} + {r : core.result.Result Unit error.Exc × S × A} + (h : vspace.decode_vspace_flush st ar s a lbl length cap buf = ok r) : + r.1 = .Ok () → sched.set_thread_state st s (L.curW s) sched.TS_RESTART = ok r.2.1 := by + intro hr + unfold vspace.decode_vspace_flush at h + obtain ⟨ct, hct, h⟩ := bind_eq_ok h + rw [cur_thread_ok] at hct + rw [← ok_inj_ff hct] at h + split at h + · exact (errStep_footA trunc_neA h hr).elim + obtain ⟨start, -, h⟩ := bind_eq_ok h + obtain ⟨en, -, h⟩ := bind_eq_ok h + split at h + · exact (errStep_footA invArg_neA h hr).elim + split at h + · exact (errStep_footA illegal_neA h hr).elim + cases cap + case Pml4 ptr b asid => + dsimp only at h + split at h + swap + · exact (errStep_footA invCap_neA h hr).elim + obtain ⟨rv, -, h⟩ := bind_eq_ok h + rcases rv with r1 | lf + swap + · rw [← ok_inj_ff h] at hr; cases hr + dsimp only at h + split at h + · exact (errStep_footA invCap_neA h hr).elim + obtain ⟨lu, -, h⟩ := bind_eq_ok h + obtain ⟨e, -, h⟩ := bind_eq_ok h + obtain ⟨b1, -, h⟩ := bind_eq_ok h + split at h + swap + · obtain ⟨s1, hs1, h⟩ := bind_eq_ok h + rw [← ok_inj_ff h] + exact hs1 + obtain ⟨i, -, h⟩ := bind_eq_ok h + obtain ⟨i1, -, h⟩ := bind_eq_ok h + obtain ⟨mk, -, h⟩ := bind_eq_ok h + obtain ⟨i2, -, h⟩ := bind_eq_ok h + obtain ⟨i3, -, h⟩ := bind_eq_ok h + obtain ⟨i4, -, h⟩ := bind_eq_ok h + obtain ⟨i5, -, h⟩ := bind_eq_ok h + obtain ⟨i6, -, h⟩ := bind_eq_ok h + split at h + · obtain ⟨i7, -, h⟩ := bind_eq_ok h + obtain ⟨i8, -, h⟩ := bind_eq_ok h + obtain ⟨i9, -, h⟩ := bind_eq_ok h + exact (errStep_footA range_neA h hr).elim + obtain ⟨b2, -, h⟩ := bind_eq_ok h + split at h + · obtain ⟨i7, -, h⟩ := bind_eq_ok h + split at h + · exact (errStep_footA illegal_neA h hr).elim + obtain ⟨s1, hs1, h⟩ := bind_eq_ok h + obtain ⟨i8, -, h⟩ := bind_eq_ok h + obtain ⟨i9, -, h⟩ := bind_eq_ok h + obtain ⟨ps, -, h⟩ := bind_eq_ok h + obtain ⟨i10, -, h⟩ := bind_eq_ok h + obtain ⟨i11, -, h⟩ := bind_eq_ok h + obtain ⟨a1, -, h⟩ := bind_eq_ok h + rw [← ok_inj_ff h] + exact hs1 + · obtain ⟨s1, hs1, h⟩ := bind_eq_ok h + obtain ⟨i7, -, h⟩ := bind_eq_ok h + obtain ⟨i8, -, h⟩ := bind_eq_ok h + obtain ⟨ps, -, h⟩ := bind_eq_ok h + obtain ⟨i9, -, h⟩ := bind_eq_ok h + obtain ⟨i10, -, h⟩ := bind_eq_ok h + obtain ⟨a1, -, h⟩ := bind_eq_ok h + rw [← ok_inj_ff h] + exact hs1 + all_goals exact (errStep_footA invCap_neA h hr).elim + +/-- The word an ASID assignment writes in the pool page. -/ +def PoolWordA (pool b : U64) : Prop := ∃ o : U64, o.val < 4096 ∧ o.val % 8 = 0 ∧ b = wa pool o + +/-- The shared tail of `ASIDPoolAssign`: the root cap write, then the pool entry. -/ +theorem asidAssign_tail_footA {s1 : S} {a : A} {x pool asid v : U64} {sc : Result S} + (hsc : ∀ s2, sc = ok s2 → WritesIn st s1 s2 (SlotWordA x)) + (hal : ∀ b, PoolWordA pool b → Aligned b) {r : core.result.Result Unit error.Exc × S × A} + (h : (do + let s2 ← sc + let i2 ← lift (asid &&& 511#u64) + let i3 ← i2 * 8#u64 + let i4 ← lift (core.num.U64.wrapping_add pool i3) + let i5 ← lift v + let s3 ← st.write s2 i4 i5 + ok (core.result.Result.Ok (), s3, a)) = ok r) : + WritesIn st s1 r.2.1 (fun b => SlotWordA x b ∨ PoolWordA pool b) := by + obtain ⟨s2, hs2, h⟩ := bind_eq_ok h + obtain ⟨i2, hi2, h⟩ := bind_eq_ok h + obtain ⟨i3, hi3, h⟩ := bind_eq_ok h + obtain ⟨i4, hi4, h⟩ := bind_eq_ok h + obtain ⟨i5, -, h⟩ := bind_eq_ok h + obtain ⟨s3, hs3, h⟩ := bind_eq_ok h + rw [← ok_inj_ff h] + have e2 : i2.val ≤ 511 := by + simp only [lift] at hi2 + rw [← ok_inj_ff hi2] + have : (asid &&& 511#u64).val ≤ (511#u64 : U64).val := by + simp only [UScalar.val_and]; exact Nat.and_le_right + simpa using this + have e3 : i3.val = i2.val * 8 := by + obtain ⟨z, hz, hzv⟩ := spec_ex_fd (U64.mul_spec (x := i2) (y := 8#u64) (by scalar_tac)) + rw [hz] at hi3 + rw [← ok_inj_ff hi3] + simpa using hzv + have hw : PoolWordA pool i4 := by + simp only [lift] at hi4 + exact ⟨i3, by omega, by omega, (ok_inj_ff hi4).symm⟩ + exact ((hsc s2 hs2).trans (writesIn_writeA (hal _ hw) hs3)).mono fun b hb => hb.imp id fun e => by rw [e]; exact hw + +/-- **`ARMASIDPoolAssign`** (`vspace.decode` on an ASID-pool cap): after the Restart, the root cap's slot and the +pool entry. -/ +theorem asidAssign_footA {ar : machine.Arch A} {s : S} {a : A} {length slot ptr base buf : U64} {call : Bool} + {ex : Aeneas.Std.Array obj.Slot 3#usize} + (hx : ∀ x, vspace.extra ex 0#usize = ok (some x) → Aligned x ∧ Aligned (wa x 8#u64)) + (hal : ∀ b, PoolWordA ptr b → Aligned b) {r : core.result.Result Unit error.Exc × S × A} + (h : vspace.decode st ar s a fl4_abi.label.ARMASIDPoolAssign length slot (.AsidPool ptr base) call buf ex = ok r) : + r.1 = .Ok () → ∃ x s1, vspace.extra ex 0#usize = ok (some x) ∧ + sched.set_thread_state st s (L.curW s) sched.TS_RESTART = ok s1 ∧ + WritesIn st s1 r.2.1 (fun b => SlotWordA x b ∨ PoolWordA ptr b) := by + intro hr + unfold vspace.decode at h + obtain ⟨ct, hct, h⟩ := bind_eq_ok h + rw [cur_thread_ok] at hct + rw [← ok_inj_ff hct] at h + dsimp only at h + rw [if_neg (by simp)] at h + obtain ⟨o, ho, h⟩ := bind_eq_ok h + rcases o with _ | x + · exact (errStep_footA trunc_neA h hr).elim + dsimp only at h + obtain ⟨c, -, h⟩ := bind_eq_ok h + obtain ⟨hx1, hx2⟩ := hx x ho + have done : ∀ s1 (sc : Result S) (asid v : U64), sched.set_thread_state st s (L.curW s) sched.TS_RESTART = ok s1 → + (∀ s2, sc = ok s2 → WritesIn st s1 s2 (SlotWordA x)) → (do + let s2 ← sc + let i2 ← lift (asid &&& 511#u64) + let i3 ← i2 * 8#u64 + let i4 ← lift (core.num.U64.wrapping_add ptr i3) + let i5 ← lift v + let s3 ← st.write s2 i4 i5 + ok (core.result.Result.Ok (), s3, a)) = ok r → + ∃ x s1, vspace.extra ex 0#usize = ok (some x) ∧ + sched.set_thread_state st s (L.curW s) sched.TS_RESTART = ok s1 ∧ + WritesIn st s1 r.2.1 (fun b => SlotWordA x b ∨ PoolWordA ptr b) := fun s1 sc asid v h1 hsc ht => + ⟨x, s1, ho, h1, asidAssign_tail_footA hsc hal ht⟩ + have capw : ∀ (s1 : S) (c1 c2 c3 : capability.Cap) (p q : Prop) [Decidable p] [Decidable q], ∀ s2, + (if p then obj.Slot.set_cap st x s1 c1 else if q then obj.Slot.set_cap st x s1 c2 + else obj.Slot.set_cap st x s1 c3) = ok s2 → WritesIn st s1 s2 (SlotWordA x) := by + intro s1 c1 c2 c3 p q _ _ s2 e + split at e + · exact writesIn_setCapA hx1 hx2 e + split at e + all_goals exact writesIn_setCapA hx1 hx2 e + have pe : ∀ (pool : U64), ¬ (pool != ptr) = true → pool = ptr := fun pool h => + UScalar.eq_of_val_eq (by simpa using h) + cases c + case Pml4 ptr1 b b2 => + dsimp only at h + split at h + · exact (errStep_footA invCap_neA h hr).elim + obtain ⟨i, -, h⟩ := bind_eq_ok h + obtain ⟨pool, -, h⟩ := bind_eq_ok h + split at h + · rw [← ok_inj_ff h] at hr; cases hr + by_cases hp : (pool != ptr) = true + · rw [if_pos hp] at h + exact (errStep_footA invCap_neA h hr).elim + rw [if_neg hp] at h + rw [pe _ hp] at h + obtain ⟨i1, -, h⟩ := bind_eq_ok h + split at h + · rw [← ok_inj_ff h] at hr; cases hr + obtain ⟨asid, -, h⟩ := bind_eq_ok h + obtain ⟨s1, hs1, h⟩ := bind_eq_ok h + exact done s1 _ asid _ hs1 (capw s1 _ _ _ _ _) h + case Ept ptr1 i b b2 b3 => + dsimp only at h + split at h + swap + · exact (errStep_footA invCap_neA h hr).elim + split at h + · exact (errStep_footA invCap_neA h hr).elim + obtain ⟨i1, -, h⟩ := bind_eq_ok h + obtain ⟨pool, -, h⟩ := bind_eq_ok h + split at h + · rw [← ok_inj_ff h] at hr; cases hr + by_cases hp : (pool != ptr) = true + · rw [if_pos hp] at h + exact (errStep_footA invCap_neA h hr).elim + rw [if_neg hp] at h + rw [pe _ hp] at h + obtain ⟨i2, -, h⟩ := bind_eq_ok h + split at h + · rw [← ok_inj_ff h] at hr; cases hr + obtain ⟨asid, -, h⟩ := bind_eq_ok h + obtain ⟨s1, hs1, h⟩ := bind_eq_ok h + exact done s1 _ asid _ hs1 (capw s1 _ _ _ _ _) h + case HighVSpace ptr1 b b2 => + dsimp only at h + split at h + · exact (errStep_footA invCap_neA h hr).elim + obtain ⟨i, -, h⟩ := bind_eq_ok h + obtain ⟨pool, -, h⟩ := bind_eq_ok h + split at h + · rw [← ok_inj_ff h] at hr; cases hr + by_cases hp : (pool != ptr) = true + · rw [if_pos hp] at h + exact (errStep_footA invCap_neA h hr).elim + rw [if_neg hp] at h + rw [pe _ hp] at h + obtain ⟨i1, -, h⟩ := bind_eq_ok h + split at h + · rw [← ok_inj_ff h] at hr; cases hr + obtain ⟨asid, -, h⟩ := bind_eq_ok h + obtain ⟨s1, hs1, h⟩ := bind_eq_ok h + exact done s1 _ asid _ hs1 (capw s1 _ _ _ _ _) h + all_goals exact (errStep_footA invCap_neA h hr).elim + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3VsFootFlushA.lean b/proofs/Fl4ProofsA64/P3/Stage3VsFootFlushA.lean new file mode 100644 index 000000000..9ceecf608 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3VsFootFlushA.lean @@ -0,0 +1,194 @@ +/- +P3 (AArch64, merge line, D8 step 4 (a), rest): **write footprints of the Stage-2 and frame cache flushes** +(`ept.decode`, `ept.flush_frame`, `vspace.decode_page_flush`): an accepted flush writes only the caller's Restart +(`FlushFootA`; the VSpace-root flush is `vsFlush_footA`). A high-window cap has no invocation (`vspace.decode` answers +`illegal`). Pure program facts. New module; no existing statement changed. No named premise. +-/ +import Fl4ProofsA64.P3.Stage3VsFootMapA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +variable (st) in +/-- Every accepted outcome of `x` is the caller's Restart of `s`. -/ +def FlushFootA (s : S) (x : Result (core.result.Result Unit error.Exc × S × A)) : Prop := + ∀ r, x = ok r → r.1 = .Ok () → sched.set_thread_state st s (L.curW s) sched.TS_RESTART = ok r.2.1 + +/-- Every outcome of `x` has the state `s1`. -/ +def KeepStA (s1 : S) (x : Result (core.result.Result Unit error.Exc × S × A)) : Prop := + ∀ r, x = ok r → r.2.1 = s1 + +theorem keepStA_ok {s1 : S} {y : core.result.Result Unit error.Exc} {a : A} : KeepStA s1 (ok (y, s1, a)) := + fun r h => by rw [← ok_inj_ff h] + +theorem keepStA_bind {α : Type} {s1 : S} {f : Result α} {g : α → Result (core.result.Result Unit error.Exc × S × A)} + (h : ∀ y, f = ok y → KeepStA s1 (g y)) : KeepStA s1 (f >>= g) := fun r e => by + obtain ⟨y, hy, e⟩ := bind_eq_ok e + exact h y hy r e + +theorem flushFootA_bind {α : Type} {s : S} {f : Result α} + {g : α → Result (core.result.Result Unit error.Exc × S × A)} + (h : ∀ y, f = ok y → FlushFootA st s (g y)) : FlushFootA st s (f >>= g) := fun r e => by + obtain ⟨y, hy, e⟩ := bind_eq_ok e + exact h y hy r e + +theorem flushFootA_ct {s : S} {g : U64 → Result (core.result.Result Unit error.Exc × S × A)} + (h : FlushFootA st s (g (L.curW s))) : FlushFootA st s (sched.cur_thread st s >>= g) := + flushFootA_bind fun y hy => by + rw [cur_thread_ok] at hy + rw [← ok_inj_ff hy]; exact h + +theorem flushFootA_set {s : S} {g : S → Result (core.result.Result Unit error.Exc × S × A)} + (h : ∀ s1, KeepStA s1 (g s1)) : + FlushFootA st s (sched.set_thread_state st s (L.curW s) sched.TS_RESTART >>= g) := fun r e _ => by + obtain ⟨s1, hs1, e⟩ := bind_eq_ok e + rw [h s1 r e]; exact hs1 + +theorem flushFootA_ite {s : S} {c : Prop} [Decidable c] {x y : Result (core.result.Result Unit error.Exc × S × A)} + (hx : FlushFootA st s x) (hy : FlushFootA st s y) : FlushFootA st s (if c then x else y) := by + by_cases h : c + · rw [if_pos h]; exact hx + · rw [if_neg h]; exact hy + +theorem keepStA_ite {s1 : S} {c : Prop} [Decidable c] {x y : Result (core.result.Result Unit error.Exc × S × A)} + (hx : KeepStA s1 x) (hy : KeepStA s1 y) : KeepStA s1 (if c then x else y) := by + by_cases h : c + · rw [if_pos h]; exact hx + · rw [if_neg h]; exact hy + +theorem flushFootA_retErr {s : S} {er : error.Exc} {a : A} : + FlushFootA st s (ok (core.result.Result.Err er, s, a) : Result _) := fun _ e hr => + (retErr_footA e hr).elim + +theorem flushFootA_errfn {s : S} {a : A} {f : Result (core.result.Result Unit error.Exc)} + (hf : ∀ x, f = ok x → x ≠ .Ok ()) : FlushFootA st s (do let x ← f; ok (x, s, a)) := fun _ e hr => + (errStep_footA hf e hr).elim + +macro "flush_foot" : tactic => `(tactic| repeat' (first + | exact flushFootA_retErr + | exact flushFootA_errfn trunc_neA + | exact flushFootA_errfn invArg_neA + | exact flushFootA_errfn illegal_neA + | exact flushFootA_errfn range_neA + | exact flushFootA_errfn invCap_neA + | exact flushFootA_errfn residual_neA + | apply flushFootA_ct + | apply flushFootA_ite + | (apply flushFootA_set; intro s1 + repeat' (first | exact keepStA_ok | apply keepStA_ite | (apply keepStA_bind; intro _ _) | split)) + | (apply flushFootA_bind; intro _ _) + | split + | dsimp only)) + +/-- **`ept.flush_frame`**. -/ +theorem ept_flush_frame_footA {ar : machine.Arch A} {s : S} {a : A} {op : U64} {inv dev : Bool} + {asid ipa ptr start en : U64} {size : capability.FrameSize} : + FlushFootA st s (ept.flush_frame st ar s a op inv asid ipa ptr size dev start en) := by + unfold ept.flush_frame + flush_foot + +/-- **`ept.decode`** (the Stage-2 VSpace cache flushes). -/ +theorem ept_decode_footA {ar : machine.Arch A} {s : S} {a : A} {lbl length buf : U64} {cap : capability.Cap} + {slot : obj.Slot} {ex : Aeneas.Std.Array obj.Slot 3#usize} : + FlushFootA st s (ept.decode st ar s a lbl length cap slot buf ex) := by + unfold ept.decode + flush_foot + +/-- **`vspace.decode_page_flush`** (frame cache flushes, Stage-1 and Stage-2). -/ +theorem page_flush_footA {ar : machine.Arch A} {s : S} {a : A} {lbl length buf : U64} {f : vspace.FrameCap} : + FlushFootA st s (vspace.decode_page_flush st ar s a lbl length f buf) := by + unfold vspace.decode_page_flush + repeat' (first + | exact ept_flush_frame_footA + | exact flushFootA_retErr + | exact flushFootA_errfn trunc_neA + | exact flushFootA_errfn invArg_neA + | exact flushFootA_errfn illegal_neA + | exact flushFootA_errfn invCap_neA + | apply flushFootA_ct + | apply flushFootA_ite + | (apply flushFootA_set; intro s1 + repeat' (first | exact keepStA_ok | apply keepStA_ite | (apply keepStA_bind; intro _ _) | split)) + | (apply flushFootA_bind; intro _ _) + | split + | dsimp only) + +/-- **`vspace.derive_subframe`**: after the Restart, only `insert_subframes` (its cap inserts are D8 step 5). -/ +theorem derive_subframe_shapeA {s : S} {length buf : U64} {src : obj.Slot} {cap : capability.Cap} + {ex : Aeneas.Std.Array obj.Slot 3#usize} {r : core.result.Result Unit error.Exc × S} + (h : vspace.derive_subframe st s length src cap buf ex = ok r) : + r.1 = .Ok () → ∃ s1 dest first base count vm, + sched.set_thread_state st s (L.curW s) sched.TS_RESTART = ok s1 ∧ + vspace.insert_subframes st s1 src dest first base count vm = ok r.2 := by + intro hr + have bad : ∀ f : Result (core.result.Result Unit error.Exc), (∀ x, f = ok x → x ≠ .Ok ()) → + (do let x ← f; ok (x, s)) = ok r → False := fun f hf e => by + obtain ⟨x, hx, e⟩ := bind_eq_ok e + rw [← ok_inj_ff e] at hr + exact hf x hx hr + unfold vspace.derive_subframe at h + obtain ⟨ct, hct, h⟩ := bind_eq_ok h + obtain ⟨o, -, h⟩ := bind_eq_ok h + rcases o with _ | x + · exact (bad _ trunc_neA h).elim + dsimp only at h + by_cases hl : length >= 6#u64 + swap + · rw [if_neg hl] at h; exact (bad _ trunc_neA h).elim + rw [if_pos hl] at h + obtain ⟨o1, -, h⟩ := bind_eq_ok h + rcases o1 with _ | ⟨ptr, rights⟩ + · exact (bad _ illegal_neA h).elim + dsimp only at h + obtain ⟨ni, -, h⟩ := bind_eq_ok h + obtain ⟨nd, -, h⟩ := bind_eq_ok h + obtain ⟨no, -, h⟩ := bind_eq_ok h + obtain ⟨off, -, h⟩ := bind_eq_ok h + obtain ⟨count, -, h⟩ := bind_eq_ok h + obtain ⟨i, -, h⟩ := bind_eq_ok h + split at h + · exact (bad _ invArg_neA h).elim + obtain ⟨i1, -, h⟩ := bind_eq_ok h + split at h + · exact (bad _ invArg_neA h).elim + obtain ⟨i2, -, h⟩ := bind_eq_ok h + obtain ⟨mx, -, h⟩ := bind_eq_ok h + split at h + · exact (bad _ range_neA h).elim + split at h + · exact (bad _ range_neA h).elim + obtain ⟨rd, -, h⟩ := bind_eq_ok h + obtain ⟨cf, -, h⟩ := bind_eq_ok h + rcases cf with val | residual + swap + · exact (bad _ residual_neA h).elim + dsimp only at h + obtain ⟨re, -, h⟩ := bind_eq_ok h + obtain ⟨cf1, -, h⟩ := bind_eq_ok h + rcases cf1 with u | residual + swap + · exact (bad _ residual_neA h).elim + dsimp only at h + obtain ⟨mask, -, h⟩ := bind_eq_ok h + obtain ⟨r2, -, h⟩ := bind_eq_ok h + obtain ⟨vm, -, h⟩ := bind_eq_ok h + obtain ⟨s1, hs1, h⟩ := bind_eq_ok h + obtain ⟨i3, -, h⟩ := bind_eq_ok h + obtain ⟨s2, hs2, h⟩ := bind_eq_ok h + rw [← ok_inj_ff h] + rw [cur_thread_ok] at hct + rw [← ok_inj_ff hct] at hs1 + exact ⟨s1, _, _, _, _, _, hs1, hs2⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3VsFootMapA.lean b/proofs/Fl4ProofsA64/P3/Stage3VsFootMapA.lean new file mode 100644 index 000000000..eb911b31d --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3VsFootMapA.lean @@ -0,0 +1,480 @@ +/- +P3 (AArch64, merge line, D8 step 4 (a), rest): **write footprints of the frame / page-table map and unmap arms** of +`decode_page_table`, `decode_page_map`, `decode_high_page_map` and `decode_frame`. Pure program facts, as in +`P3/Stage3VsFootA`. The looked-up entry is a **`MapLookA`** word (a result of `lookup_pt_slot` / `ept.lookup_slot` in +the pre-state); its alignment is a lemma hypothesis. New module; no existing statement changed. No named premise. +-/ +import Fl4ProofsA64.P3.Stage3VsFootA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +variable (st) in +/-- An entry word that a Stage-1 or Stage-2 lookup in `s` returns. -/ +def MapLookA (s : S) (x : U64) : Prop := + (∃ root va lu, vspace.lookup_pt_slot st s root va = ok lu ∧ lu.slot = x) ∨ + ∃ root va bits lu, ept.lookup_slot st s root va bits = ok lu ∧ lu.slot = x + +variable (st) in +/-- The footprint of an accepted map arm: the caller's Restart, then the invoked slot and one looked-up entry. -/ +def MapFootA (s : S) (slot : U64) (s' : S) : Prop := + ∃ s1 x, sched.set_thread_state st s (L.curW s) sched.TS_RESTART = ok s1 ∧ MapLookA st s x ∧ + WritesIn st s1 s' (fun b => SlotWordA slot b ∨ b = x) + +theorem residual_neA {residual : core.result.Result Never error.Exc} : + ∀ x, core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual Unit + (core.convert.FromSame error.Exc) residual = ok x → x ≠ .Ok () := by + intro x h e + rcases residual with v | er + · cases v + · unfold core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual at h + obtain ⟨f, -, hf⟩ := bind_eq_ok h + rw [← ok_inj_ff hf] at e; cases e + +/-- A returned error: the outcome is not `Ok`. -/ +theorem retErr_footA {α : Type} {er : error.Exc} {s : S} {a : α} {r : core.result.Result Unit error.Exc × S × α} + (h : (ok (core.result.Result.Err er, s, a) : Result _) = ok r) (hr : r.1 = .Ok ()) : False := by + rw [← ok_inj_ff h] at hr; cases hr + +/-- **`vspace.install_page`**: only the looked-up entry. -/ +theorem install_page_footA {ar : machine.Arch A} {s : S} {a : A} {lu : vspace.PtSlot} {e root asid vaddr attr : U64} + {f : vspace.FrameCap} {high : Bool} (hal : Aligned lu.slot) {r : S × A} + (h : vspace.install_page st ar s a lu e root asid vaddr f attr high = ok r) : + WritesIn st s r.1 (fun b => b = lu.slot) := by + have two : ∀ s1 s2 : S, ∀ v w : U64, st.write s lu.slot v = ok s1 → st.write s1 lu.slot w = ok s2 → + WritesIn st s s2 (fun b => b = lu.slot) := fun s1 s2 v w h1 h2 => + ((writesIn_writeA hal h1).trans (writesIn_writeA hal h2)).mono (fun _ hb => hb.elim id id) + unfold vspace.install_page at h + obtain ⟨old, -, h⟩ := bind_eq_ok h + obtain ⟨b, -, h⟩ := bind_eq_ok h + split at h + · obtain ⟨i, -, h⟩ := bind_eq_ok h + obtain ⟨i1, -, h⟩ := bind_eq_ok h + obtain ⟨i2, -, h⟩ := bind_eq_ok h + split at h + · obtain ⟨s1, hs1, h⟩ := bind_eq_ok h + obtain ⟨a1, -, h⟩ := bind_eq_ok h + rw [← ok_inj_ff h] + exact writesIn_writeA hal hs1 + · obtain ⟨s1, hs1, h⟩ := bind_eq_ok h + obtain ⟨a1, -, h⟩ := bind_eq_ok h + obtain ⟨a2, -, h⟩ := bind_eq_ok h + obtain ⟨s2, hs2, h⟩ := bind_eq_ok h + rw [← ok_inj_ff h] + exact two _ _ _ _ hs1 hs2 + · obtain ⟨a1, -, h⟩ := bind_eq_ok h + obtain ⟨s1, hs1, h⟩ := bind_eq_ok h + rw [← ok_inj_ff h] + exact writesIn_writeA hal hs1 + +/-- The shared block of the Stage-1 and high map arms, from the lookup on. -/ +theorem pageMap_block_footA {ar : machine.Arch A} {s : S} {a : A} {slot root vaddr shift vm attr rt asid ct : U64} + {f : vspace.FrameCap} {c : capability.Cap} {high : Bool} + (hx : Aligned slot) (hx8 : Aligned (wa slot 8#u64)) (hal : ∀ x, MapLookA st s x → Aligned x) + (hct : sched.cur_thread st s = ok ct) {r : core.result.Result Unit error.Exc × S × A} + (h : (do + let lu ← vspace.lookup_pt_slot st s root vaddr + if lu.shift != shift + then + ok (core.result.Result.Err (error.Exc.Sys (error.SysError.FailedLookup false + (error.LookupFault.MissingCapability lu.shift))), s, a) + else + let cap_read ← vspace.rights_readable f.rights + let i7 ← layout.paddr f.ptr + let e ← vspace.make_user_page i7 vm cap_read attr shift + let s1 ← sched.set_thread_state st s ct sched.TS_RESTART + let s2 ← obj.Slot.set_cap st slot s1 c + let (s3, a1) ← vspace.install_page st ar s2 a lu e rt asid vaddr f attr high + ok (core.result.Result.Ok (), s3, a1)) = ok r) + (hr : r.1 = .Ok ()) : MapFootA st s slot r.2.1 := by + obtain ⟨lu, hlu, h⟩ := bind_eq_ok h + split at h + · exact (retErr_footA h hr).elim + obtain ⟨cr, -, h⟩ := bind_eq_ok h + obtain ⟨i, -, h⟩ := bind_eq_ok h + obtain ⟨e, -, h⟩ := bind_eq_ok h + obtain ⟨s1, hs1, h⟩ := bind_eq_ok h + obtain ⟨s2, hs2, h⟩ := bind_eq_ok h + obtain ⟨⟨s3, a1⟩, hs3, h⟩ := bind_eq_ok h + rw [← ok_inj_ff h] + rw [cur_thread_ok] at hct + rw [← ok_inj_ff hct] at hs1 + have look : MapLookA st s lu.slot := Or.inl ⟨_, _, lu, hlu, rfl⟩ + exact ⟨s1, lu.slot, hs1, look, + (writesIn_setCapA hx hx8 hs2).trans (install_page_footA (hal _ look) hs3)⟩ + +/-- **`vspace.decode_high_page_map`**. -/ +theorem high_page_map_footA {ar : machine.Arch A} {s : S} {a : A} {slot vaddr vm attr : U64} + {f : vspace.FrameCap} {vcap : capability.Cap} + (hx : Aligned slot) (hx8 : Aligned (wa slot 8#u64)) (hal : ∀ x, MapLookA st s x → Aligned x) + {r : core.result.Result Unit error.Exc × S × A} + (h : vspace.decode_high_page_map st ar s a slot f vcap vaddr vm attr = ok r) : + r.1 = .Ok () → MapFootA st s slot r.2.1 := by + intro hr + unfold vspace.decode_high_page_map at h + obtain ⟨ct, hct, h⟩ := bind_eq_ok h + obtain ⟨rc, -, h⟩ := bind_eq_ok h + obtain ⟨cf, -, h⟩ := bind_eq_ok h + rcases cf with ⟨root, asid⟩ | residual + swap + · exact (errStep_footA residual_neA h hr).elim + dsimp only at h + obtain ⟨shift, -, h⟩ := bind_eq_ok h + obtain ⟨i, -, h⟩ := bind_eq_ok h + obtain ⟨i1, -, h⟩ := bind_eq_ok h + obtain ⟨i2, -, h⟩ := bind_eq_ok h + obtain ⟨i3, -, h⟩ := bind_eq_ok h + split at h + · exact (retErr_footA h hr).elim + split at h + · split at h + · exact (errStep_footA invCap_neA h hr).elim + split at h + · exact (errStep_footA invArg_neA h hr).elim + obtain ⟨i4, -, h⟩ := bind_eq_ok h + obtain ⟨i5, -, h⟩ := bind_eq_ok h + obtain ⟨i6, -, h⟩ := bind_eq_ok h + obtain ⟨last, -, h⟩ := bind_eq_ok h + split at h + · exact (errStep_footA invArg_neA h hr).elim + split at h + · exact (errStep_footA invArg_neA h hr).elim + split at h + · exact (errStep_footA invArg_neA h hr).elim + exact pageMap_block_footA hx hx8 hal hct h hr + · obtain ⟨i4, -, h⟩ := bind_eq_ok h + obtain ⟨i5, -, h⟩ := bind_eq_ok h + obtain ⟨i6, -, h⟩ := bind_eq_ok h + obtain ⟨last, -, h⟩ := bind_eq_ok h + split at h + · exact (errStep_footA invArg_neA h hr).elim + split at h + · exact (errStep_footA invArg_neA h hr).elim + split at h + · exact (errStep_footA invArg_neA h hr).elim + exact pageMap_block_footA hx hx8 hal hct h hr + +/-- `ept.map_frame` as a `MapFootA`. -/ +theorem eptMapFrame_mapFootA {ar : machine.Arch A} {s : S} {a : A} {root asid ipa vm attr ptr rights masid mva slot : U64} + {size : capability.FrameSize} {dev : Bool} + (hx : Aligned slot) (hx8 : Aligned (wa slot 8#u64)) (hal : ∀ x, MapLookA st s x → Aligned x) + {r : core.result.Result Unit error.Exc × S × A} + (h : ept.map_frame st ar s a root asid ipa vm attr ptr size rights dev masid mva slot = ok r) + (hr : r.1 = .Ok ()) : MapFootA st s slot r.2.1 := by + obtain ⟨bits, lu, s1, hlu, hs1, w⟩ := ept_map_frame_footA hx hx8 + (fun bits lu e => hal _ (Or.inr ⟨_, _, _, lu, e, rfl⟩)) h hr + exact ⟨s1, lu.slot, hs1, Or.inr ⟨_, _, _, lu, hlu, rfl⟩, w⟩ + +/-- The Stage-1 arm of `decode_page_map` after `check_vspace`. -/ +theorem pageMap_s1_footA {ar : machine.Arch A} {s : S} {a : A} {slot vaddr vm attr ct : U64} + {f : vspace.FrameCap} {vcap : capability.Cap} + (hx : Aligned slot) (hx8 : Aligned (wa slot 8#u64)) (hal : ∀ x, MapLookA st s x → Aligned x) + (hct : sched.cur_thread st s = ok ct) {r : core.result.Result Unit error.Exc × S × A} + (h : (do + let r1 ← vspace.check_vspace st s vcap 1#u64 + let cf ← core.result.Result.Insts.CoreOpsTry.branch r1 + match cf with + | core.ops.control_flow.ControlFlow.Continue val => + let (root, asid) := val + let shift ← vspace.page_shift f.size + let i ← lift (shift &&& 63#u64) + let i1 ← 1#u64 <<< i + let i2 ← lift (core.num.U64.wrapping_sub i1 1#u64) + let i3 ← lift (vaddr &&& i2) + if i3 != 0#u64 + then + ok (core.result.Result.Err (error.Exc.Sys error.SysError.AlignmentError), s, a) + else + if f.asid != 0#u64 + then + if f.asid != asid + then let r2 ← error.invalid_cap Unit 1#u64 + ok (r2, s, a) + else + if f.vaddr != vaddr + then let r2 ← error.invalid_arg Unit 0#u64 + ok (r2, s, a) + else + let lu ← vspace.lookup_pt_slot st s root vaddr + if lu.shift != shift + then + ok (core.result.Result.Err (error.Exc.Sys (error.SysError.FailedLookup false + (error.LookupFault.MissingCapability lu.shift))), s, a) + else + let cap_read ← vspace.rights_readable f.rights + let i4 ← layout.paddr f.ptr + let e ← vspace.make_user_page i4 vm cap_read attr shift + let s1 ← sched.set_thread_state st s ct sched.TS_RESTART + let s2 ← obj.Slot.set_cap st slot s1 + (capability.Cap.Frame f.ptr f.size f.rights f.is_device true false asid vaddr) + let (s3, a1) ← vspace.install_page st ar s2 a lu e root asid vaddr f attr false + ok (core.result.Result.Ok (), s3, a1) + else + let i4 ← lift (shift &&& 63#u64) + let i5 ← 1#u64 <<< i4 + let i6 ← lift (core.num.U64.wrapping_sub i5 1#u64) + let last ← lift (core.num.U64.wrapping_add vaddr i6) + if last > fl4_abi.consts.USER_TOP + then let r2 ← error.invalid_arg Unit 0#u64 + ok (r2, s, a) + else + if last < vaddr + then let r2 ← error.invalid_arg Unit 0#u64 + ok (r2, s, a) + else + let lu ← vspace.lookup_pt_slot st s root vaddr + if lu.shift != shift + then + ok (core.result.Result.Err (error.Exc.Sys (error.SysError.FailedLookup false + (error.LookupFault.MissingCapability lu.shift))), s, a) + else + let cap_read ← vspace.rights_readable f.rights + let i7 ← layout.paddr f.ptr + let e ← vspace.make_user_page i7 vm cap_read attr shift + let s1 ← sched.set_thread_state st s ct sched.TS_RESTART + let s2 ← obj.Slot.set_cap st slot s1 + (capability.Cap.Frame f.ptr f.size f.rights f.is_device true false asid vaddr) + let (s3, a1) ← vspace.install_page st ar s2 a lu e root asid vaddr f attr false + ok (core.result.Result.Ok (), s3, a1) + | core.ops.control_flow.ControlFlow.Break residual => + let r2 ← + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual + Unit (core.convert.FromSame error.Exc) residual + ok (r2, s, a)) = ok r) + (hr : r.1 = .Ok ()) : MapFootA st s slot r.2.1 := by + obtain ⟨rc, -, h⟩ := bind_eq_ok h + obtain ⟨cf, -, h⟩ := bind_eq_ok h + rcases cf with ⟨root, asid⟩ | residual + swap + · exact (errStep_footA residual_neA h hr).elim + dsimp only at h + obtain ⟨shift, -, h⟩ := bind_eq_ok h + obtain ⟨i, -, h⟩ := bind_eq_ok h + obtain ⟨i1, -, h⟩ := bind_eq_ok h + obtain ⟨i2, -, h⟩ := bind_eq_ok h + obtain ⟨i3, -, h⟩ := bind_eq_ok h + split at h + · exact (retErr_footA h hr).elim + split at h + · split at h + · exact (errStep_footA invCap_neA h hr).elim + split at h + · exact (errStep_footA invArg_neA h hr).elim + exact pageMap_block_footA hx hx8 hal hct h hr + · obtain ⟨i4, -, h⟩ := bind_eq_ok h + obtain ⟨i5, -, h⟩ := bind_eq_ok h + obtain ⟨i6, -, h⟩ := bind_eq_ok h + obtain ⟨last, -, h⟩ := bind_eq_ok h + split at h + · exact (errStep_footA invArg_neA h hr).elim + split at h + · exact (errStep_footA invArg_neA h hr).elim + exact pageMap_block_footA hx hx8 hal hct h hr + +/-- **`decode_page_map`** (Stage-2, high and Stage-1 targets). -/ +theorem page_map_footA {ar : machine.Arch A} {s : S} {a : A} {length slot buf : U64} {f : vspace.FrameCap} + {ex : Aeneas.Std.Array obj.Slot 3#usize} + (hx : Aligned slot) (hx8 : Aligned (wa slot 8#u64)) (hal : ∀ x, MapLookA st s x → Aligned x) + {r : core.result.Result Unit error.Exc × S × A} + (h : vspace.decode_page_map st ar s a length slot f buf ex = ok r) : + r.1 = .Ok () → MapFootA st s slot r.2.1 := by + intro hr + unfold vspace.decode_page_map at h + obtain ⟨ct, hct, h⟩ := bind_eq_ok h + obtain ⟨o, -, h⟩ := bind_eq_ok h + rcases o with _ | x + · exact (errStep_footA trunc_neA h hr).elim + dsimp only at h + by_cases hl : length >= 3#u64 + swap + · rw [if_neg hl] at h; exact (errStep_footA trunc_neA h hr).elim + rw [if_pos hl] at h + obtain ⟨vaddr, -, h⟩ := bind_eq_ok h + obtain ⟨rm, -, h⟩ := bind_eq_ok h + obtain ⟨attr, -, h⟩ := bind_eq_ok h + obtain ⟨rr, -, h⟩ := bind_eq_ok h + obtain ⟨vm, -, h⟩ := bind_eq_ok h + obtain ⟨vcap, -, h⟩ := bind_eq_ok h + obtain ⟨b, -, h⟩ := bind_eq_ok h + by_cases hb : b = true + · rw [if_pos hb] at h + obtain ⟨r1, -, h⟩ := bind_eq_ok h + obtain ⟨cf, -, h⟩ := bind_eq_ok h + rcases cf with ⟨root, asid⟩ | residual + swap + · exact (errStep_footA residual_neA h hr).elim + change (if (f.asid != 0#u64) = true then + if f.is_ept = true then + ept.map_frame st ar s a root asid vaddr vm attr f.ptr f.size f.rights f.is_device f.asid f.vaddr slot + else do + let r2 ← error.invalid_cap Unit 1#u64 + ok (r2, s, a) + else + ept.map_frame st ar s a root asid vaddr vm attr f.ptr f.size f.rights f.is_device f.asid f.vaddr slot) = ok r at h + by_cases ha : (f.asid != 0#u64) = true + · rw [if_pos ha] at h + by_cases he : f.is_ept = true + · rw [if_pos he] at h; exact eptMapFrame_mapFootA hx hx8 hal h hr + · rw [if_neg he] at h; exact (errStep_footA invCap_neA h hr).elim + · rw [if_neg ha] at h; exact eptMapFrame_mapFootA hx hx8 hal h hr + rw [if_neg hb] at h + by_cases ha : (f.asid != 0#u64) = true + · rw [if_pos ha] at h + by_cases he : f.is_ept = true + · rw [if_pos he] at h; exact (errStep_footA invCap_neA h hr).elim + rw [if_neg he] at h + obtain ⟨b1, -, h⟩ := bind_eq_ok h + by_cases h1 : b1 = true + · rw [if_pos h1] at h; exact high_page_map_footA hx hx8 hal h hr + · rw [if_neg h1] at h; exact pageMap_s1_footA hx hx8 hal hct h hr + · rw [if_neg ha] at h + obtain ⟨b1, -, h⟩ := bind_eq_ok h + by_cases h1 : b1 = true + · rw [if_pos h1] at h; exact high_page_map_footA hx hx8 hal h hr + · rw [if_neg h1] at h; exact pageMap_s1_footA hx hx8 hal hct h hr + +/-- **The `ARMPageTableMap` arm of `decode_page_table`** (Stage-2, high and Stage-1 targets). -/ +theorem pt_map_footA {ar : machine.Arch A} {s : S} {a : A} {length slot buf ptr asid vaddr : U64} {mapped : Bool} + {ex : Aeneas.Std.Array obj.Slot 3#usize} + (hx : Aligned slot) (hx8 : Aligned (wa slot 8#u64)) (hal : ∀ x, MapLookA st s x → Aligned x) + {r : core.result.Result Unit error.Exc × S × A} + (h : vspace.decode_page_table st ar s a fl4_abi.label.ARMPageTableMap length slot + (.PageTable ptr mapped asid vaddr) buf ex = ok r) : + r.1 = .Ok () → MapFootA st s slot r.2.1 := by + intro hr + unfold vspace.decode_page_table at h + obtain ⟨ct, hct, h⟩ := bind_eq_ok h + dsimp only at h + have ne : fl4_abi.label.ARMPageTableMap ≠ fl4_abi.label.ARMPageTableUnmap := by + unfold fl4_abi.label.ARMPageTableMap fl4_abi.label.ARMPageTableUnmap; decide + rw [if_neg ne, if_neg (by simp)] at h + obtain ⟨o, -, h⟩ := bind_eq_ok h + rcases o with _ | x + · exact (errStep_footA trunc_neA h hr).elim + dsimp only at h + by_cases hl : length >= 2#u64 + swap + · rw [if_neg hl] at h; exact (errStep_footA trunc_neA h hr).elim + rw [if_pos hl] at h + split at h + · exact (errStep_footA invCap_neA h hr).elim + obtain ⟨va, -, h⟩ := bind_eq_ok h + obtain ⟨vcap, -, h⟩ := bind_eq_ok h + obtain ⟨b, -, h⟩ := bind_eq_ok h + by_cases hb : b = true + · rw [if_pos hb] at h + obtain ⟨⟨r1, s1⟩, hm, h⟩ := bind_eq_ok h + rw [← ok_inj_ff h] at hr ⊢ + obtain ⟨root, lu, s2, hlu, hs2, w⟩ := map_high_pt_footA hx hx8 + (fun _ lu e => hal _ (Or.inl ⟨_, _, lu, e, rfl⟩)) hm hr + exact ⟨s2, lu.slot, hs2, Or.inl ⟨_, _, lu, hlu, rfl⟩, w⟩ + rw [if_neg hb] at h + obtain ⟨b1, -, h⟩ := bind_eq_ok h + by_cases h1 : b1 = true + swap + · rw [if_neg h1] at h; exact (errStep_footA invCap_neA h hr).elim + rw [if_pos h1] at h + obtain ⟨b2, -, h⟩ := bind_eq_ok h + by_cases h2 : b2 = true + · rw [if_pos h2] at h + obtain ⟨r1, -, h⟩ := bind_eq_ok h + obtain ⟨cf, -, h⟩ := bind_eq_ok h + rcases cf with ⟨root, asid1⟩ | residual + swap + · exact (errStep_footA residual_neA h hr).elim + dsimp only at h + obtain ⟨bits, lu, s1, -, hlu, hs1, w⟩ := ept_map_table_footA hx hx8 + (fun bits lu _ e => hal _ (Or.inr ⟨_, _, _, lu, e, rfl⟩)) h hr + exact ⟨s1, lu.slot, hs1, Or.inr ⟨_, _, _, lu, hlu, rfl⟩, w⟩ + rw [if_neg h2] at h + split at h + · exact (errStep_footA invArg_neA h hr).elim + obtain ⟨r1, -, h⟩ := bind_eq_ok h + obtain ⟨cf, -, h⟩ := bind_eq_ok h + rcases cf with ⟨root, asid1⟩ | residual + swap + · exact (errStep_footA residual_neA h hr).elim + dsimp only at h + obtain ⟨lu, hlu, h⟩ := bind_eq_ok h + split at h + · exact (retErr_footA h hr).elim + obtain ⟨i, -, h⟩ := bind_eq_ok h + obtain ⟨i1, -, h⟩ := bind_eq_ok h + obtain ⟨i2, -, h⟩ := bind_eq_ok h + split at h + · exact (retErr_footA h hr).elim + obtain ⟨i3, -, h⟩ := bind_eq_ok h + obtain ⟨i4, -, h⟩ := bind_eq_ok h + obtain ⟨i5, -, h⟩ := bind_eq_ok h + obtain ⟨i6, -, h⟩ := bind_eq_ok h + obtain ⟨base, -, h⟩ := bind_eq_ok h + obtain ⟨s1, hs1, h⟩ := bind_eq_ok h + obtain ⟨s2, hs2, h⟩ := bind_eq_ok h + obtain ⟨i7, -, h⟩ := bind_eq_ok h + obtain ⟨i8, -, h⟩ := bind_eq_ok h + obtain ⟨s3, hs3, h⟩ := bind_eq_ok h + rw [← ok_inj_ff h] + rw [cur_thread_ok] at hct + rw [← ok_inj_ff hct] at hs1 + have look : MapLookA st s lu.slot := Or.inl ⟨_, _, lu, hlu, rfl⟩ + exact ⟨s1, lu.slot, hs1, look, (writesIn_setCapA hx hx8 hs2).trans (writesIn_writeA (hal _ look) hs3)⟩ + +/-- **The `ARMPageUnmap` arm of `decode_frame`**: after the Restart, the entry an unmap walk found, then the invoked +slot. The unmap's own footprint is `ept_unmap_footA` / `unmap_page_footA`. -/ +theorem frame_unmap_shapeA {ar : machine.Arch A} {s : S} {a : A} {length slot buf : U64} {cap : capability.Cap} + {f : vspace.FrameCap} {call : Bool} {ex : Aeneas.Std.Array obj.Slot 3#usize} + (hf : vspace.as_frame cap = ok (some f)) (hx : Aligned slot) (hx8 : Aligned (wa slot 8#u64)) + {r : core.result.Result Unit error.Exc × S × A} + (h : vspace.decode_frame st ar s a fl4_abi.label.ARMPageUnmap length slot cap call buf ex = ok r) : + r.1 = .Ok () → ∃ s1 s2 : S, sched.set_thread_state st s (L.curW s) sched.TS_RESTART = ok s1 ∧ + (s2 = s1 ∨ + (∃ i a1, capability.FrameSize.bits f.size = ok i ∧ + ept.unmap st ar s1 a f.asid f.vaddr f.ptr i false = ok (s2, a1)) ∨ + ∃ a1, vspace.unmap_page st ar s1 a f.size f.asid f.vaddr f.ptr = ok (s2, a1)) ∧ + WritesIn st s2 r.2.1 (SlotWordA slot) := by + intro hr + unfold vspace.decode_frame at h + rw [hf] at h + obtain ⟨o, ho, h⟩ := bind_eq_ok h + rw [← ok_inj_ff ho] at h + try dsimp only at h + obtain ⟨ct, hct, h⟩ := bind_eq_ok h + have ne : fl4_abi.label.ARMPageUnmap ≠ fl4_abi.label.ARMPageMap := by + unfold fl4_abi.label.ARMPageUnmap fl4_abi.label.ARMPageMap; decide + rw [if_neg ne, if_pos rfl] at h + obtain ⟨s1, hs1, h⟩ := bind_eq_ok h + rw [cur_thread_ok] at hct + rw [← ok_inj_ff hct] at hs1 + obtain ⟨⟨s2, a1⟩, hs2, h⟩ := bind_eq_ok h + try dsimp only at h + obtain ⟨c, -, h⟩ := bind_eq_ok h + obtain ⟨s3, hs3, h⟩ := bind_eq_ok h + rw [← ok_inj_ff h] + refine ⟨s1, s2, hs1, ?_, writesIn_setCapA hx hx8 hs3⟩ + have same : (ok (s1, a) : Result (S × A)) = ok (s2, a1) → s2 = s1 := fun e => by + have := ok_inj_ff e + simp only [Prod.mk.injEq] at this + exact this.1.symm + split at hs2 + · split at hs2 + · split at hs2 + · obtain ⟨i, hi, hs2⟩ := bind_eq_ok hs2 + exact Or.inr (Or.inl ⟨i, a1, hi, hs2⟩) + · exact Or.inr (Or.inr ⟨a1, hs2⟩) + · exact Or.inl (same hs2) + · exact Or.inl (same hs2) + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3VsLeafA.lean b/proofs/Fl4ProofsA64/P3/Stage3VsLeafA.lean new file mode 100644 index 000000000..06ff066fe --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3VsLeafA.lean @@ -0,0 +1,294 @@ +/- +P3 (AArch64, merge line, D8 step 5 (ii), the map arms; Architect 19:46Z: shared machinery in its own module): **the +record's P1 VSpace typing across a leaf write** and the facts that tie the record's ghost depths to the relation's. + +* **`PoolSet.activeA` / `PoolSet.ofActiveA`**: the record's installed pools (`PoolSet`) are `VsRelA`'s active pools. +* **`recReach_alA`**: every table the record's typing reaches (`VsReach` from the installed pools) is 4096-aligned + (a native root is a `Tb` table of the relation, a window root an 8192-aligned `Y` object, a child a descriptor's + page address). +* **`recReach_pathA`** (the open check of the 19:46Z ruling: the lookup level ties to the record's depth): every node of + a `VsRelA` walk (`VsPathA`, tags 0 / 2) is reached by the record's typing, lies in its tables and has record depth + `(39 - shift) / 9`; so a reachable Stage-1 table's record depth is the relation's (`recLv_eqA`). +* **`VsTyR.leafWrA`**: one word written to a value that is no table descriptor for any reachable table of depth `< 3` + holding that word (and no pool / ASID-table word), every other `VsR` word kept: the typing is kept at the same + ghosts and the reachable tables only shrink. **`leafOkA`**: the side condition for a page / block descriptor written + into a reachable Stage-1 table (`PteRelA`'s bit 1 at depth `< 3`; at depth 3 no other reachable table of depth `< 3` + holds the word). + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3VsZeroA +import Fl4ProofsA64.P3.VsMapPageA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +theorem peAF_offA (t o : U64) (ho : o.val < 4096) (h8 : o.val % 8 = 0) : + ∃ i : U64, i.val = o.val / 8 ∧ peAF t i = wa t o := by + refine ⟨⟨BitVec.ofNat 64 (o.val / 8)⟩, ?_, ?_⟩ + · show (BitVec.ofNat 64 (o.val / 8)).toNat = o.val / 8 + rw [BitVec.toNat_ofNat] + exact Nat.mod_eq_of_lt (by simp only [Nat.reducePow]; omega) + · unfold peAF + congr 1 + apply UScalar.eq_of_val_eq + have hv : (⟨BitVec.ofNat 64 (o.val / 8)⟩ : U64).val = o.val / 8 := by + show (BitVec.ofNat 64 (o.val / 8)).toNat = o.val / 8 + rw [BitVec.toNat_ofNat] + exact Nat.mod_eq_of_lt (by simp only [Nat.reducePow]; omega) + simp [core.num.U64.wrapping_mul_val_eq, U64.size_def, U64.numBits, hv] + omega + +theorem wm8_offA {i : U64} (hi : i.val < 512) : + (core.num.U64.wrapping_mul i 8#u64).val < 4096 ∧ (core.num.U64.wrapping_mul i 8#u64).val % 8 = 0 := by + have e : (core.num.U64.wrapping_mul i 8#u64).val = i.val * 8 := by + simp [core.num.U64.wrapping_mul_val_eq, U64.size_def, U64.numBits]; omega + rw [e]; omega + +theorem addr_pptrA (w : U64) : + core.num.U64.wrapping_add (w &&& vspace.ADDR_MASK) layout.PPTR_BASE = dTabA w := by + unfold vspace.ADDR_MASK layout.PPTR_BASE dTabA dAddrA; rfl + +theorem dTab_alA (w : U64) : (dTabA w).val % 4096 = 0 := by + unfold dTabA + rw [wa_valm, Nat.mod_mod_of_dvd _ (by norm_num : 4096 ∣ 2 ^ 64)] + have h1 := (landM_A w.val).1 + have e : (dAddrA w).val = w.val &&& 281474976706560 := by + unfold dAddrA; rw [UScalar.val_and]; rfl + have e2 : (18446603336221196288#u64 : U64).val = 18446603336221196288 := rfl + rw [e, e2] + omega + +/-- An installed pool of the record is an active pool of the relation. -/ +theorem PoolSet.activeA {Pp : Set U64} {s : S} {p : U64} (h : p ∈ PoolSet st Pp s) : + p ∈ Pp ∧ ActivePoolA (L.mem s) (L.ksb s) p := by + obtain ⟨hp, o, h1, h2, h3, h4, h5⟩ := h + refine ⟨hp, h5, ⟨BitVec.ofNat 64 ((o.val - 772352) / 8)⟩, ?_, ?_⟩ + · show (BitVec.ofNat 64 _).toNat < 128 + rw [BitVec.toNat_ofNat] + exact lt_of_le_of_lt (Nat.mod_le _ _) (by omega) + · rw [← h4] + unfold atAF + congr 1 + apply UScalar.eq_of_val_eq + have hv : (⟨BitVec.ofNat 64 ((o.val - 772352) / 8)⟩ : U64).val = (o.val - 772352) / 8 := by + show (BitVec.ofNat 64 _).toNat = _ + rw [BitVec.toNat_ofNat] + exact Nat.mod_eq_of_lt (by simp only [Nat.reducePow]; omega) + rw [wa_valm] + simp [core.num.U64.wrapping_mul_val_eq, U64.size_def, U64.numBits, hv] + omega + +/-- An active pool of `Pp` is an installed pool of the record. -/ +theorem PoolSet.ofActiveA {Pp : Set U64} {s : S} {p : U64} (hp : p ∈ Pp) + (h : ActivePoolA (L.mem s) (L.ksb s) p) (hks : (L.ksb s).val + 788736 ≤ 2 ^ 64) : p ∈ PoolSet st Pp s := by + obtain ⟨hne, hi, hhi, hm⟩ := h + refine ⟨hp, core.num.U64.wrapping_add 772352#u64 (core.num.U64.wrapping_mul hi 8#u64), ?_, ?_, ?_, hm, hne⟩ + all_goals + have e : (core.num.U64.wrapping_add 772352#u64 (core.num.U64.wrapping_mul hi 8#u64)).val = 772352 + hi.val * 8 := by + rw [wa_valm] + simp [core.num.U64.wrapping_mul_val_eq, U64.size_def, U64.numBits] + omega + rw [e]; omega + +variable {Pp Tb X Y T2 : Set U64} {Lz Sh2 : U64 → Nat} {σ : Fl4SpecA64.State Unit} + +/-- **Every table the record's typing reaches is 4096-aligned.** -/ +theorem recReach_alA {s : S} {Lv : U64 → Nat} (R : VsRelA Pp Tb Lz X Y (L.mem s) (L.ksb s) σ) {q : U64} + (hr : VsReach st (PoolSet st Pp s) ∅ Lv s q) : q.val % 4096 = 0 := by + induction hr with + | hr h => exact absurd h (Set.notMem_empty _) + | @root0 p o hp ho h8 hne h0 => + obtain ⟨hpp, hact⟩ := PoolSet.activeA hp + obtain ⟨lo, hlo, he⟩ := peAF_offA p o ho h8 + have hk := R.kind p hpp hact lo (by omega) + rw [he] at hk + have ht0 : tagA (L.mem s (wa p o)) = 0 := (tag_iffA _).1 h0 + rcases hk with e | ⟨-, htb, -⟩ | ⟨h1, -⟩ | ⟨h2, -⟩ + · exact absurd e hne + · rw [untagA_of ht0] at htb + exact Nat.mod_eq_zero_of_dvd (R.al _ htb) + · omega + · omega + | @root2 p o hp ho h8 h2 => + obtain ⟨hpp, hact⟩ := PoolSet.activeA hp + obtain ⟨lo, hlo, he⟩ := peAF_offA p o ho h8 + have hk := R.kind p hpp hact lo (by omega) + rw [he] at hk + have ht2 : tagA (L.mem s (wa p o)) = 2 := by + unfold tagA; have := congrArg UScalar.val h2; rw [and3_valA] at this; exact this + have hun : (L.mem s (wa p o) &&& ~~~3#u64) = untagA (L.mem s (wa p o)) := rfl + rw [hun] + rcases hk with e | ⟨h0, -⟩ | ⟨h1, -⟩ | ⟨-, hy⟩ + · rw [e] at ht2; simp [tagA] at ht2 + · omega + · omega + · have := Nat.mod_eq_zero_of_dvd (R.yal _ hy) + omega + | step _ _ _ _ _ _ => + rw [addr_pptrA]; exact dTab_alA _ + +/-- **A `VsRelA` walk is reached by the record's typing, at record depth `(39 - shift) / 9`** (module docstring). -/ +theorem recReach_pathA {s : S} {Tr : Set U64} {Lv : U64 → Nat} + (R : VsRelA Pp Tb Lz X Y (L.mem s) (L.ksb s) σ) (E : ExtVsRelA Pp Tb X Y T2 Sh2 (L.mem s) σ) + (h : VsTyR st (L.ksb s) (PoolSet st Pp s) ∅ Tr Lv s) (hks : (L.ksb s).val + 788736 ≤ 2 ^ 64) + {tag : U64} {sh : Nat} {q : U64} (hp : VsPathA Pp Tb Y (L.mem s) (L.ksb s) tag sh q) : + VsReach st (PoolSet st Pp s) ∅ Lv s q ∧ q ∈ Tr ∧ 9 * Lv q + sh = 39 := by + induction hp with + | @native asid raw hact ht0 _ => + obtain ⟨pool, hpool, hactP, lo, hlo, hw, hrne⟩ := hact.activePool + have hP := PoolSet.ofActiveA (st := st) hpool hactP hks + obtain ⟨o1, o2⟩ := wm8_offA hlo + have hw' : L.mem s (wa pool (core.num.U64.wrapping_mul lo 8#u64)) = raw := hw + have h0 : (L.mem s (wa pool (core.num.U64.wrapping_mul lo 8#u64)) &&& 3#u64) = 0#u64 := by + rw [hw']; exact (tag_iffA raw).2 ht0 + have hne : L.mem s (wa pool (core.num.U64.wrapping_mul lo 8#u64)) ≠ 0#u64 := by rw [hw']; exact hrne + have r0 := VsReach.root0 (st := st) (Lv := Lv) (Hr := ∅) hP o1 o2 hne h0 + obtain ⟨a, b⟩ := (h.pool pool hP _ o1 o2 hne).1 h0 + rw [hw'] at r0 a b + exact ⟨r0, a, by omega⟩ + | @high asid raw hact ht2 hy => + obtain ⟨pool, hpool, hactP, lo, hlo, hw, hrne⟩ := hact.activePool + have hP := PoolSet.ofActiveA (st := st) hpool hactP hks + obtain ⟨o1, o2⟩ := wm8_offA hlo + have hw' : L.mem s (wa pool (core.num.U64.wrapping_mul lo 8#u64)) = raw := hw + have h2 : (L.mem s (wa pool (core.num.U64.wrapping_mul lo 8#u64)) &&& 3#u64) = 2#u64 := by + rw [hw'] + apply UScalar.eq_of_val_eq + rw [and3_valA]; exact ht2 + have r2 := VsReach.root2 (st := st) (Lv := Lv) (Hr := ∅) hP o1 o2 h2 + obtain ⟨a, b⟩ := (h.pool pool hP _ o1 o2 (fun e => by rw [e] at h2; exact absurd h2 (by decide))).2 h2 + have hun : (raw &&& ~~~3#u64) = untagA raw := rfl + rw [hw', hun] at r2 a b + obtain ⟨w0, w1, wt⟩ := E.wl0 _ hy + obtain ⟨p1, p2⟩ := wm8_offA (i := 511#u64) (by decide) + have h3 : (L.mem s (wa (untagA raw) (core.num.U64.wrapping_mul 511#u64 8#u64)) &&& 3#u64) = 3#u64 := + (and3_bitsA _).2 ⟨w0, w1⟩ + have r3 := VsReach.step r2 (by omega) p1 p2 h3 + obtain ⟨c, d⟩ := h.desc _ a r2 (by omega) _ p1 p2 h3 + have ew : wa (L.mem s (wa (untagA raw) (core.num.U64.wrapping_mul 511#u64 8#u64)) &&& vspace.ADDR_MASK) + layout.PPTR_BASE = wa (untagA raw) 4096#u64 := by + rw [addr_pptrA]; exact wt + rw [ew] at r3 c d + exact ⟨r3, c, by omega⟩ + | @child tag t i sh _ _ h12 hi h3 ih => + obtain ⟨rt, ht, hl⟩ := ih + obtain ⟨o1, o2⟩ := wm8_offA hi + have h3' : (L.mem s (wa t (core.num.U64.wrapping_mul i 8#u64)) &&& 3#u64) = 3#u64 := h3 + have r := VsReach.step rt (by omega) o1 o2 h3' + obtain ⟨c, d⟩ := h.desc t ht rt (by omega) _ o1 o2 h3' + have ew : wa (L.mem s (wa t (core.num.U64.wrapping_mul i 8#u64)) &&& vspace.ADDR_MASK) layout.PPTR_BASE = + dTabA (L.mem s (peAF t i)) := addr_pptrA _ + rw [ew] at r c d + exact ⟨r, c, by omega⟩ + +/-- **The record depth of a reachable Stage-1 table is the relation's** (the open check of the 19:46Z ruling). -/ +theorem recLv_eqA {s : S} {Tr : Set U64} {Lv : U64 → Nat} + (R : VsRelA Pp Tb Lz X Y (L.mem s) (L.ksb s) σ) (E : ExtVsRelA Pp Tb X Y T2 Sh2 (L.mem s) σ) + (h : VsTyR st (L.ksb s) (PoolSet st Pp s) ∅ Tr Lv s) (hks : (L.ksb s).val + 788736 ≤ 2 ^ 64) + {p : U64} (hp : p ∈ Tb) (hr : VsReachA Pp Tb Y (L.mem s) (L.ksb s) p) : + VsReach st (PoolSet st Pp s) ∅ Lv s p ∧ p ∈ Tr ∧ Lv p = Lz p := by + obtain ⟨tag, sh, htag, hpa⟩ := hr + obtain ⟨a, b, c⟩ := recReach_pathA R E h hks hpa + rcases (R.path_props hpa).2 with ⟨-, hl, hsh⟩ | ⟨-, -, y, hy, -, rfl⟩ + · exact ⟨a, b, by omega⟩ + · exact absurd hp (R.ytb y hy) + +/-- **The record's typing across one leaf write** (module docstring). -/ +theorem VsTyR.leafWrA {Tr : Set U64} {Lv : U64 → Nat} {s s' : S} {b w : U64} + (h : VsTyR st (L.ksb s) (PoolSet st Pp s) ∅ Tr Lv s) (hk : L.ksb s' = L.ksb s) + (keep : ∀ b', VsR (L.ksb s) (utVsPtA st Pp Tr Lv s) b' → b' ≠ b → L.mem s' b' = L.mem s b') + (hb : L.mem s' b = w) (nAs : ¬ AsW (L.ksb s) b) + (nPool : ∀ p ∈ PoolSet st Pp s, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → wa p o ≠ b) + (leaf : ∀ q, VsReach st (PoolSet st Pp s) ∅ Lv s q → Lv q < 3 → ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → + wa q o = b → (w &&& 3#u64) ≠ 3#u64) : + VsTyR st (L.ksb s') (PoolSet st Pp s') ∅ Tr Lv s' ∧ utVsPtA st Pp Tr Lv s' ⊆ utVsPtA st Pp Tr Lv s := by + have kAs : ∀ b', AsW (L.ksb s) b' → L.mem s' b' = L.mem s b' := fun b' hb' => + keep b' (Or.inr hb') (fun e => nAs (e ▸ hb')) + have ep : PoolSet st Pp s' = PoolSet st Pp s := PoolSet.keepAsA hk kAs + have kP : ∀ p ∈ PoolSet st Pp s, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → + L.mem s' (wa p o) = L.mem s (wa p o) := fun p hp o ho h8 => + keep _ (Or.inl ⟨p, Or.inl hp, o, ho, h8, rfl⟩) (nPool p hp o ho h8) + have kT : ∀ q, VsReach st (PoolSet st Pp s) ∅ Lv s q → Lv q < 3 → ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → + (L.mem s' (wa q o) &&& 3#u64) = 3#u64 → L.mem s' (wa q o) = L.mem s (wa q o) := by + intro q hq hl o ho h8 h3 + by_cases e : wa q o = b + · rw [e, hb] at h3; exact absurd h3 (leaf q hq hl o ho h8 e) + · exact keep _ (Or.inl ⟨q, Or.inr ⟨(h.sub.reach (hr_rt (Set.empty_subset _)) hq).1, hq⟩, o, ho, h8, rfl⟩) e + have back : ∀ q, VsReach st (PoolSet st Pp s') ∅ Lv s' q → VsReach st (PoolSet st Pp s) ∅ Lv s q := by + intro q hr + rw [ep] at hr + induction hr with + | hr h0 => exact absurd h0 (Set.notMem_empty _) + | @root0 p o hp ho h8 hne h0 => + rw [kP p hp o ho h8] at hne h0 ⊢; exact VsReach.root0 hp ho h8 hne h0 + | @root2 p o hp ho h8 h2 => + rw [kP p hp o ho h8] at h2 ⊢; exact VsReach.root2 hp ho h8 h2 + | @step q o _ hl ho h8 h3 ih => + have e := kT q ih hl o ho h8 h3 + rw [e] at h3 ⊢; exact VsReach.step ih hl ho h8 h3 + refine ⟨⟨fun o h1 h2 h8 hne => ?_, fun p hp o ho h8 hne => ?_, fun p hp hr hl o ho h8 h3 => ?_⟩, ?_⟩ + · rw [hk] at hne ⊢ + rw [kAs _ ⟨o, h1, h2, h8, rfl⟩] at hne ⊢ + rw [ep]; exact h.tbl o h1 h2 h8 hne + · rw [ep] at hp + rw [kP p hp o ho h8] at hne ⊢ + exact h.pool p hp o ho h8 hne + · have hr0 := back p hr + have e := kT p hr0 hl o ho h8 h3 + rw [e] at h3 ⊢ + exact h.desc p hp hr0 hl o ho h8 h3 + · intro q hq + unfold utVsPtA at hq ⊢ + rw [ep] at hq + rcases hq with hq | ⟨ht, hr⟩ + · exact Or.inl hq + · exact Or.inr ⟨ht, back q (by rw [ep]; exact hr)⟩ + +/-- **The leaf side condition** for a descriptor written at `peAF p i` of a reachable Stage-1 table `p` whose bit 1 +is clear below depth 3 (`PteRelA`'s page / block case; module docstring). -/ +theorem leafOkA {s : S} {Tr : Set U64} {Lv : U64 → Nat} + (R : VsRelA Pp Tb Lz X Y (L.mem s) (L.ksb s) σ) (E : ExtVsRelA Pp Tb X Y T2 Sh2 (L.mem s) σ) + (h : VsTyR st (L.ksb s) (PoolSet st Pp s) ∅ Tr Lv s) (hks : (L.ksb s).val + 788736 ≤ 2 ^ 64) + {p i w : U64} (hp : p ∈ Tb) (hr : VsReachA Pp Tb Y (L.mem s) (L.ksb s) p) (hi : i.val < 512) + (hb1 : Lz p < 3 → dBitA w 1 = false) : + ∀ q, VsReach st (PoolSet st Pp s) ∅ Lv s q → Lv q < 3 → ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → + wa q o = peAF p i → (w &&& 3#u64) ≠ 3#u64 := by + intro q hq hl o ho h8 e + by_cases hz : Lz p < 3 + · intro h3 + have := ((and3_bitsA w).1 h3).2 + rw [hb1 hz] at this + exact absurd this (by decide) + · exfalso + have hqa := recReach_alA R hq + have hpa : p.val % 4096 = 0 := Nat.mod_eq_zero_of_dvd (R.al p hp) + obtain ⟨o1, o2⟩ := wm8_offA hi + have hv := congrArg UScalar.val e + unfold peAF at hv + rw [wa_valm, wa_valm] at hv + have hq64 := q.bv.isLt + have hp64 := p.bv.isLt + have hqv : q.val = q.bv.toNat := rfl + have hpv : p.val = p.bv.toNat := rfl + have hqp : q = p := UScalar.eq_of_val_eq (by + simp only [Nat.reducePow] at hv hq64 hp64 + omega) + subst hqp + obtain ⟨-, -, hlv⟩ := recLv_eqA R E h hks hp hr + have := R.lv q hp + omega + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3VsNoPreA.lean b/proofs/Fl4ProofsA64/P3/Stage3VsNoPreA.lean new file mode 100644 index 000000000..53621ce90 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3VsNoPreA.lean @@ -0,0 +1,369 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Plan B): **the VSpace decodes of the scope's arms are never preempted.** Walks of the +extracted `vspace::decode_*` programs; no premise on the state. + +* `frameUnmap_okA` (`ARMPageUnmap` of a frame only returns `Ok`). +* `ptUnmap_npA`, `ptMap_npA`, `frameMap_npA`, `asidAssign_npA`. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3ScopeA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Classical + +noncomputable section + +/-! ## "No preemption" combinators -/ + +/-- A checked value that is never `Err Preempted`. -/ +def NPvA {α : Type} (m : Result (core.result.Result α error.Exc)) : Prop := + ∀ x, m = ok (.Err x) → x ≠ error.Exc.Preempted + +/-- A decode that never returns `Err Preempted`. -/ +def NP3A {β : Type} (m : Result (core.result.Result Unit error.Exc × β)) : Prop := + ∀ x t, m = ok (.Err x, t) → x ≠ error.Exc.Preempted + +theorem NPvA.okOk {α : Type} (v : α) : NPvA (ok (.Ok v)) := fun x h => by cases okinj h + +theorem NPvA.sys {α : Type} (e : error.SysError) : NPvA (α := α) (ok (.Err (.Sys e))) := fun x h => by + cases okinj h; intro c; cases c + +theorem NPvA.invCap (α : Type) (i : U64) : NPvA (error.invalid_cap α i) := NPvA.sys _ +theorem NPvA.invArg (α : Type) (i : U64) : NPvA (error.invalid_arg α i) := NPvA.sys _ +theorem NPvA.illegal (α : Type) : NPvA (error.illegal α) := NPvA.sys _ +theorem NPvA.truncated (α : Type) : NPvA (error.truncated α) := NPvA.sys _ + +theorem NPvA.bind {α γ : Type} {m : Result γ} {f : γ → Result (core.result.Result α error.Exc)} + (h : ∀ y, m = ok y → NPvA (f y)) : NPvA (m >>= f) := fun x e => by + obtain ⟨y, e1, e2⟩ := bind_eq_ok e + exact h y e1 x e2 + +theorem NPvA.ite {α : Type} {c : Prop} [Decidable c] {p q : Result (core.result.Result α error.Exc)} + (hp : c → NPvA p) (hq : ¬ c → NPvA q) : NPvA (if c then p else q) := by + by_cases h : c + · rw [if_pos h]; exact hp h + · rw [if_neg h]; exact hq h + +theorem NP3A.okOk {β : Type} (u : Unit) (t : β) : NP3A (ok (.Ok u, t)) := fun x t' h => by + have := okinj h + simp only [Prod.mk.injEq] at this + cases this.1 + +theorem NP3A.sys {β : Type} (e : error.SysError) (t : β) : NP3A (ok (.Err (.Sys e), t)) := fun x t' h => by + have := okinj h + simp only [Prod.mk.injEq] at this + cases this.1; intro c; cases c + +theorem NP3A.pass {β : Type} {r : core.result.Result Unit error.Exc} (hr : ∀ x, r = .Err x → x ≠ error.Exc.Preempted) + (t : β) : NP3A (ok (r, t)) := fun x t' h => by + have := okinj h + simp only [Prod.mk.injEq] at this + exact hr x this.1 + +theorem NP3A.bind {β γ : Type} {m : Result γ} {f : γ → Result (core.result.Result Unit error.Exc × β)} + (h : ∀ y, m = ok y → NP3A (f y)) : NP3A (m >>= f) := fun x t e => by + obtain ⟨y, e1, e2⟩ := bind_eq_ok e + exact h y e1 x t e2 + +theorem NP3A.ite {β : Type} {c : Prop} [Decidable c] {p q : Result (core.result.Result Unit error.Exc × β)} + (hp : c → NP3A p) (hq : ¬ c → NP3A q) : NP3A (if c then p else q) := by + by_cases h : c + · rw [if_pos h]; exact hp h + · rw [if_neg h]; exact hq h + +/-- `let r ← ; ok (r, t)`. -/ +theorem NP3A.errBind {β : Type} {m : Result (core.result.Result Unit error.Exc)} (hm : NPvA m) (t : β) : + NP3A (m >>= fun r => ok (r, t)) := + NP3A.bind fun r e => NP3A.pass (fun x hx => hm x (by rw [e, hx])) t + +/-- `?` after a checked value that is never preempted: continue, or return its error. -/ +theorem NP3A.tryA {α β : Type} {m : Result (core.result.Result α error.Exc)} (hm : NPvA m) + {f : core.result.Result α error.Exc → Result (core.result.Result Unit error.Exc × β)} + (hf : ∀ r, (∀ x, r = .Err x → x ≠ error.Exc.Preempted) → NP3A (f r)) : NP3A (m >>= f) := + NP3A.bind fun r er => hf r fun x hx => hm x (by rw [er, hx]) + +theorem branch_okA {α : Type} (v : α) : + core.result.Result.Insts.CoreOpsTry.branch (E := error.Exc) (.Ok v) = ok (.Continue v) := rfl + +theorem branch_errA {α : Type} (e : error.Exc) : + core.result.Result.Insts.CoreOpsTry.branch (T := α) (.Err e) = ok (.Break (.Err e)) := rfl + +theorem residual_errA (e : error.Exc) : + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual Unit + (core.convert.FromSame error.Exc) (.Err e) = ok (.Err e) := by + simp [core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame, + core.convert.FromSame.from] + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +theorem check_vspace_npA (s : S) (c : capability.Cap) (i : U64) : NPvA (vspace.check_vspace st s c i) := by + unfold vspace.check_vspace + cases c <;> try exact NPvA.invCap _ _ + rename_i p b asid + refine NPvA.ite (fun _ => NPvA.bind fun r _ => ?_) fun _ => NPvA.invCap _ _ + cases r with + | Ok r1 => exact NPvA.ite (fun _ => NPvA.invCap _ _) fun _ => NPvA.okOk _ + | Err lf => exact NPvA.sys _ + +theorem check_high_vspace_npA (s : S) (c : capability.Cap) (i : U64) : NPvA (vspace.check_high_vspace st s c i) := by + unfold vspace.check_high_vspace + cases c <;> try exact NPvA.invCap _ _ + rename_i p b asid + refine NPvA.ite (fun _ => NPvA.bind fun r _ => ?_) fun _ => NPvA.invCap _ _ + cases r with + | Ok r1 => exact NPvA.ite (fun _ => NPvA.invCap _ _) fun _ => NPvA.okOk _ + | Err lf => exact NPvA.sys _ + +/-- One step of a decode walk: a leaf, an `if`, or a `let ←`. -/ +macro "np_step" : tactic => + `(tactic| first + | exact NP3A.okOk _ _ + | exact NP3A.sys _ _ + | exact NP3A.errBind (NPvA.invCap _ _) _ + | exact NP3A.errBind (NPvA.invArg _ _) _ + | exact NP3A.errBind (NPvA.illegal _) _ + | exact NP3A.errBind (NPvA.truncated _) _ + | (refine NP3A.ite (fun _ => ?_) (fun _ => ?_)) + | (refine NP3A.bind (fun _ _ => ?_)) + | split) + +theorem decode_high_page_map_npA (ar : machine.Arch A) (s : S) (a : A) (slot : U64) (f : vspace.FrameCap) + (vcap : capability.Cap) (vaddr vm attr : U64) : + NP3A (vspace.decode_high_page_map st ar s a slot f vcap vaddr vm attr) := by + unfold vspace.decode_high_page_map + refine NP3A.bind fun ct _ => ?_ + refine NP3A.tryA (check_high_vspace_npA s vcap 1#u64) fun r hr => ?_ + cases r with + | Err e => + rw [branch_errA, bind_ok] + dsimp only + rw [residual_errA, bind_ok] + exact NP3A.pass (fun x hx => by cases hx; exact hr e rfl) _ + | Ok v => + rw [branch_okA, bind_ok] + obtain ⟨root, asid⟩ := v + dsimp only + repeat' np_step + +/-- **An `ARMPageUnmap` of a frame cap only returns `Ok`.** -/ +theorem frameUnmap_okA (ar : machine.Arch A) (s : S) (a : A) (len slot fp rt asid va : U64) + (sz : capability.FrameSize) (dev mp ept call : Bool) (buf : U64) (ex : Aeneas.Std.Array obj.Slot 3#usize) + {r : core.result.Result Unit error.Exc} {s' : S} {a' : A} + (h : vspace.decode_frame st ar s a fl4_abi.label.ARMPageUnmap len slot (.Frame fp sz rt dev mp ept asid va) + call buf ex = ok (r, s', a')) : r = .Ok () := by + have ne : fl4_abi.label.ARMPageUnmap ≠ fl4_abi.label.ARMPageMap := by + rw [Ne, u64_eq_iffA]; simp [fl4_abi.label.ARMPageUnmap, fl4_abi.label.ARMPageMap] + unfold vspace.decode_frame at h + simp only [vspace.as_frame, cur_thread_ok, bind_ok, bind_tc_ok] at h + rw [if_neg ne] at h + simp only [↓reduceIte] at h + obtain ⟨_, -, h⟩ := bind_eq_ok h + obtain ⟨⟨_, _⟩, -, h⟩ := bind_eq_ok h + obtain ⟨_, -, h⟩ := bind_eq_ok h + obtain ⟨_, -, h⟩ := bind_eq_ok h + have := okinj h + simp only [Prod.mk.injEq] at this + exact this.1.symm + +theorem ptUnmap_npA (ar : machine.Arch A) (s : S) (a : A) (len slot ptr asid va : U64) (mp : Bool) + (buf : U64) (ex : Aeneas.Std.Array obj.Slot 3#usize) : + NP3A (vspace.decode_page_table st ar s a fl4_abi.label.ARMPageTableUnmap len slot (.PageTable ptr mp asid va) + buf ex) := by + unfold vspace.decode_page_table + refine NP3A.bind fun ct _ => ?_ + dsimp only + rw [if_pos rfl] + repeat' np_step + +/-- An `ARMPageTableMap` whose first extra cap is a plain VSpace cap (not `Ept`, not `HighVSpace`). -/ +theorem ptMap_npA (ar : machine.Arch A) (s : S) (a : A) (len slot ptr asid va : U64) (mp : Bool) + (buf : U64) (ex : Aeneas.Std.Array obj.Slot 3#usize) {x0 : U64} + (hx0 : Aeneas.Std.Array.index_usize ex 0#usize = ok x0) + (hS1 : ∀ p l mp' asid va, capA st s x0 ≠ .Ept p l mp' asid va) + (hHi : ∀ p mp' asid, capA st s x0 ≠ .HighVSpace p mp' asid) : + NP3A (vspace.decode_page_table st ar s a fl4_abi.label.ARMPageTableMap len slot (.PageTable ptr mp asid va) + buf ex) := by + have ne : fl4_abi.label.ARMPageTableMap ≠ fl4_abi.label.ARMPageTableUnmap := by + rw [Ne, u64_eq_iffA]; simp [fl4_abi.label.ARMPageTableUnmap, fl4_abi.label.ARMPageTableMap] + unfold vspace.decode_page_table + refine NP3A.bind fun ct _ => ?_ + dsimp only + rw [if_neg ne] + refine NP3A.ite (fun _ => NP3A.errBind (NPvA.illegal _) _) fun _ => ?_ + rw [extra0_eqA hx0, bind_ok] + by_cases h0 : x0 = 0#u64 + · rw [if_pos h0]; exact NP3A.errBind (NPvA.truncated _) _ + rw [if_neg h0] + dsimp only + refine NP3A.ite (fun _ => ?_) fun _ => NP3A.errBind (NPvA.truncated _) _ + refine NP3A.ite (fun _ => NP3A.errBind (NPvA.invCap _ _) _) fun _ => ?_ + refine NP3A.bind fun vaddr1 _ => ?_ + rw [capA_eq, bind_ok] + refine NP3A.bind fun b hb => ?_ + have eb : b = false := by + cases hv : capA st s x0 <;> rw [hv] at hb <;> first + | exact (okinj hb).symm + | exact absurd hv (hHi _ _ _) + subst eb + rw [if_neg Bool.false_ne_true] + refine NP3A.bind fun b1 _ => ?_ + refine NP3A.ite (fun _ => ?_) fun _ => NP3A.errBind (NPvA.invCap _ _) _ + refine NP3A.bind fun b2 hb2 => ?_ + have eb2 : b2 = false := by + cases hv : capA st s x0 <;> rw [hv] at hb2 <;> first + | exact (okinj hb2).symm + | exact absurd hv (hS1 _ _ _ _ _) + subst eb2 + rw [if_neg Bool.false_ne_true] + refine NP3A.ite (fun _ => NP3A.errBind (NPvA.invArg _ _) _) fun _ => ?_ + refine NP3A.tryA (check_vspace_npA s _ 1#u64) fun r hr => ?_ + cases r with + | Err e => + rw [branch_errA, bind_ok] + dsimp only + rw [residual_errA, bind_ok] + exact NP3A.pass (fun x hx => by cases hx; exact hr e rfl) _ + | Ok v => + rw [branch_okA, bind_ok] + obtain ⟨root, asid1⟩ := v + dsimp only + repeat' np_step + +/-- An `ARMPageMap` of a non-Stage-2 frame cap whose first extra cap is not a Stage-2 root. -/ +theorem frameMap_npA (ar : machine.Arch A) (s : S) (a : A) (len slot fp rt asid va : U64) + (sz : capability.FrameSize) (dev mp call : Bool) (buf : U64) (ex : Aeneas.Std.Array obj.Slot 3#usize) {x0 : U64} + (hx0 : Aeneas.Std.Array.index_usize ex 0#usize = ok x0) + (hS1 : ∀ p l mp' asid va, capA st s x0 ≠ .Ept p l mp' asid va) : + NP3A (vspace.decode_frame st ar s a fl4_abi.label.ARMPageMap len slot (.Frame fp sz rt dev mp false asid va) + call buf ex) := by + unfold vspace.decode_frame + simp only [vspace.as_frame, cur_thread_ok, bind_ok, bind_tc_ok, ↓reduceIte] + unfold vspace.decode_page_map + refine NP3A.bind fun ct _ => ?_ + rw [extra0_eqA hx0, bind_ok] + by_cases h0 : x0 = 0#u64 + · rw [if_pos h0]; exact NP3A.errBind (NPvA.truncated _) _ + rw [if_neg h0] + dsimp only + refine NP3A.ite (fun _ => ?_) fun _ => NP3A.errBind (NPvA.truncated _) _ + refine NP3A.bind fun vaddr _ => NP3A.bind fun rmask _ => NP3A.bind fun attr _ => NP3A.bind fun rr _ => + NP3A.bind fun vm _ => ?_ + rw [capA_eq, bind_ok] + refine NP3A.bind fun b hb => ?_ + have eb : b = false := by + cases hv : capA st s x0 <;> rw [hv] at hb <;> first + | exact (okinj hb).symm + | exact absurd hv (hS1 _ _ _ _ _) + subst eb + rw [if_neg Bool.false_ne_true] + have tail : ∀ (b1 : Bool) (vcap : capability.Cap) (f : vspace.FrameCap), NP3A (if b1 = true then + vspace.decode_high_page_map st ar s a slot f vcap vaddr vm attr + else do + let r1 ← vspace.check_vspace st s vcap 1#u64 + let cf ← core.result.Result.Insts.CoreOpsTry.branch r1 + match cf with + | core.ops.control_flow.ControlFlow.Continue val => + let (root, asid) := val + let shift ← vspace.page_shift f.size + let i ← lift (shift &&& 63#u64) + let i1 ← 1#u64 <<< i + let i2 ← lift (core.num.U64.wrapping_sub i1 1#u64) + let i3 ← lift (vaddr &&& i2) + if i3 != 0#u64 + then + ok (core.result.Result.Err (error.Exc.Sys error.SysError.AlignmentError), s, a) + else + if f.asid != 0#u64 + then + if f.asid != asid + then let r2 ← error.invalid_cap Unit 1#u64 + ok (r2, s, a) + else + if f.vaddr != vaddr + then + let r2 ← error.invalid_arg Unit 0#u64 + ok (r2, s, a) + else + let lu ← vspace.lookup_pt_slot st s root vaddr + if lu.shift != shift + then + ok (core.result.Result.Err (error.Exc.Sys (error.SysError.FailedLookup false + (error.LookupFault.MissingCapability lu.shift))), s, a) + else + let cap_read ← vspace.rights_readable f.rights + let i4 ← layout.paddr f.ptr + let e ← vspace.make_user_page i4 vm cap_read attr shift + let s1 ← sched.set_thread_state st s ct sched.TS_RESTART + let s2 ← obj.Slot.set_cap st slot s1 + (capability.Cap.Frame f.ptr f.size f.rights f.is_device true false asid vaddr) + let (s3, a1) ← vspace.install_page st ar s2 a lu e root asid vaddr f attr false + ok (core.result.Result.Ok (), s3, a1) + else + let i4 ← lift (shift &&& 63#u64) + let i5 ← 1#u64 <<< i4 + let i6 ← lift (core.num.U64.wrapping_sub i5 1#u64) + let last ← lift (core.num.U64.wrapping_add vaddr i6) + if last > fl4_abi.consts.USER_TOP + then let r2 ← error.invalid_arg Unit 0#u64 + ok (r2, s, a) + else + if last < vaddr + then + let r2 ← error.invalid_arg Unit 0#u64 + ok (r2, s, a) + else + let lu ← vspace.lookup_pt_slot st s root vaddr + if lu.shift != shift + then + ok (core.result.Result.Err (error.Exc.Sys (error.SysError.FailedLookup false + (error.LookupFault.MissingCapability lu.shift))), s, a) + else + let cap_read ← vspace.rights_readable f.rights + let i7 ← layout.paddr f.ptr + let e ← vspace.make_user_page i7 vm cap_read attr shift + let s1 ← sched.set_thread_state st s ct sched.TS_RESTART + let s2 ← obj.Slot.set_cap st slot s1 + (capability.Cap.Frame f.ptr f.size f.rights f.is_device true false asid vaddr) + let (s3, a1) ← vspace.install_page st ar s2 a lu e root asid vaddr f attr false + ok (core.result.Result.Ok (), s3, a1) + | core.ops.control_flow.ControlFlow.Break residual => + let r2 ← core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual + Unit (core.convert.FromSame error.Exc) residual + ok (r2, s, a)) := fun b1 vcap f => by + refine NP3A.ite (fun _ => decode_high_page_map_npA ar s a slot f vcap vaddr vm attr) fun _ => ?_ + refine NP3A.tryA (check_vspace_npA s _ 1#u64) fun r hr => ?_ + cases r with + | Err e => + rw [branch_errA, bind_ok] + dsimp only + rw [residual_errA, bind_ok] + exact NP3A.pass (fun x hx => by cases hx; exact hr e rfl) _ + | Ok v => + rw [branch_okA, bind_ok] + obtain ⟨root, asid1⟩ := v + dsimp only + repeat' np_step + refine NP3A.ite (fun _ => ?_) fun _ => ?_ + · refine NP3A.ite (fun _ => NP3A.errBind (NPvA.invCap _ _) _) fun _ => ?_ + refine NP3A.bind fun b1 _ => ?_ + exact tail b1 _ _ + · refine NP3A.bind fun b1 _ => ?_ + exact tail b1 _ _ + +theorem asidAssign_npA (ar : machine.Arch A) (s : S) (a : A) (len slot p base : U64) (call : Bool) + (buf : U64) (ex : Aeneas.Std.Array obj.Slot 3#usize) : + NP3A (vspace.decode st ar s a fl4_abi.label.ARMASIDPoolAssign len slot (.AsidPool p base) call buf ex) := by + unfold vspace.decode + refine NP3A.bind fun ct _ => ?_ + dsimp only + repeat' np_step + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3VsRunA.lean b/proofs/Fl4ProofsA64/P3/Stage3VsRunA.lean new file mode 100644 index 000000000..ca54292cd --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3VsRunA.lean @@ -0,0 +1,400 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Plan B): **the VSpace arms' scope members from the run and the record.** One theorem +per VSpace arm group: + +* `JSyscallA.frameUnmap_runA`: `ARMPageUnmap` of a frame cap, every case (`frameUnmapS1/X/U`; the root kind of a + Stage-2 frame cap is `.stage2`, else it is read off the spec's ASID table; `frameUnmapZ/S2Z` cannot occur, a mapped + frame has a nonzero ASID). +* `JSyscallA.ptUnmap_runA`: `ARMPageTableUnmap` (`ptUnmapS1/X/U`; finality from the successful decode). +* `JSyscallA.ptMapS1_recA`: `ARMPageTableMap` into a Stage-1 root. +* `JSyscallA.frameMapS1_recA`, `JSyscallA.frameMapHi_runA`: `ARMPageMap` into a Stage-1 root / the high window. +* `JSyscallA.asidAssign_runA`: `ARMASIDPoolAssign`. + +Open cases, as hypotheses: `ptr ∉ Z.S1` (`UnmPtA.nt`, the table map), Stage-2 tables (`ptr ∉ Z.T2`), a device +frame outside the kernel window (`hdev`). + +**`ptr ∉ Z.S1` is strong**: `Retype` puts every new page table into `Z.S1` (`utS1NewA`, type 11) and the unmap arms +keep it there, so `ptMapS1_recA` covers no table made by `Retype`. + +Named premises: [J] `JSyscallA`, `hcan` (high-window mapped tables), `hdev`. New module. +-/ +import Fl4ProofsA64.P3.Stage3VsArmFactsA +import Fl4ProofsA64.P3.Stage3VsNoPreA +import Fl4ProofsA64.P3.Stage3VsDecFactsA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {Clock : A → Fl4SpecA64.MachineState → Prop} + +theorem decodePool_programA (ar : machine.Arch A) (s : S) (a : A) {lbl len slot p base buf : U64} + {block call cd fi : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} : + syscall.decode_invocation st ar s a lbl len slot (.AsidPool p base) block call cd fi buf extra = + vspace.decode st ar s a lbl len slot (.AsidPool p base) call buf extra := by + simp only [syscall.decode_invocation, cur_thread_ok, bind_ok] + +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {ar : machine.Arch A} {s : S} {a : A} {σ : State Unit} {m : McState} + {info : msginfo.MessageInfo} {buf slot : U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} {mi : MessageInfo} + {sl : CslotPtr} {c : Cap} {excaps : List (Cap × CslotPtr)} {buffer : Option ObjRef} {argsU : List U64} +variable (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) (hfm : A64FrameMasks st s) +variable (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) + s a σ m) (Q : SiteA st Z OS s σ info buf slot extra mi sl c excaps buffer argsU) +include hA hM hPl hfm J Q + +set_option maxHeartbeats 1000000 + +/-- **`ARMPageUnmap` of a frame cap** (module docstring). -/ +theorem JSyscallA.frameUnmap_runA (hlab : info.label = fl4_abi.label.ARMPageUnmap) {fp rt asid va : U64} + {sz : capability.FrameSize} {dev mp isEpt bB call bD : Bool} (K : ArchKeepsA ar AR) (hIpa : S2IpaE ar a) + (hcap : capA st s slot = .Frame fp sz rt dev mp isEpt asid va) : + ∃ Out, DispScope4A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ m Clock hPl.ipa info.label + (msgLenA info buf) slot bB call bD false buf extra Out := by + have hwf : CapWfA (capA st s slot) := Q.F.cap.1 + rw [hcap] at hwf + obtain ⟨-, -, ⟨-, -, hm1, -⟩, -⟩ := hwf + cases mp with + | false => + exact J.frameUnmapU_scopeA hA hM hPl hfm Q hlab (bB := bB) (bD := bD) (bF := false) (call := call) (fp := fp) + (rt := rt) (asid := asid) (va := va) (sz := sz) (dev := dev) (isEpt := isEpt) (hcap := hcap) + | true => + have h0 : asid ≠ 0#u64 := hm1 rfl + have X : ∀ k : ExtRootKind, (isEpt = false → asidRootKind σ (BitVec.ofNat 16 asid.val) = some k ∧ k ≠ .stage1) → + (isEpt = true → k = .stage2) → + ∃ Out, DispScope4A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ m Clock hPl.ipa info.label + (msgLenA info buf) slot bB call bD false buf extra Out := fun k hkA hk2 => + J.frameUnmapX_scopeA hA hM hPl hfm Q hlab (bB := bB) (bD := bD) (bF := false) (K := K) (hTO := J.tabS1) + (hEO := J.tabExt) (hIpa := hIpa) (call := call) (fp := fp) (rt := rt) (asid := asid) (va := va) (sz := sz) + (dev := dev) (isEpt := isEpt) (h0 := h0) (hcap := hcap) (k := k) (hkA := hkA) (hk2 := hk2) + cases isEpt with + | true => exact X .stage2 (fun h => by cases h) (fun _ => rfl) + | false => + have S1 : (asidRootKind σ (BitVec.ofNat 16 asid.val) = none ∨ + asidRootKind σ (BitVec.ofNat 16 asid.val) = some .stage1) → + ∃ Out, DispScope4A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ m Clock hPl.ipa info.label + (msgLenA info buf) slot bB call bD false buf extra Out := fun hS1 => + J.frameUnmapS1_scopeA hA hM hPl hfm Q hlab (bB := bB) (bD := bD) (bF := false) (K := K) (hTO := J.tabS1) + (call := call) (fp := fp) (rt := rt) (asid := asid) (va := va) (sz := sz) (dev := dev) (h0 := h0) + (hcap := hcap) (hS1 := hS1) + rcases hk : asidRootKind σ (BitVec.ofNat 16 asid.val) with _ | k + · exact S1 (Or.inl hk) + by_cases hk1 : k = .stage1 + · exact S1 (Or.inr (by rw [hk, hk1])) + exact X k (fun _ => ⟨hk, hk1⟩) (fun h => by cases h) + +/-- **`ARMPageTableUnmap`** (module docstring). -/ +theorem JSyscallA.ptUnmap_runA (hlab : info.label = fl4_abi.label.ARMPageTableUnmap) {ptr asid va : U64} + {mp bB bC bD : Bool} (K : ArchKeepsA ar AR) (hIpa : S2IpaE ar a) + (hcap : capA st s slot = .PageTable ptr mp asid va) + -- open: Stage-2 tables (tracked in `Z.T2`) + (hT2 : mp = true → ptr ∉ Z.T2) + -- [J] the canonical spec cap of a mapped high-window table (record edit #8b part C) + (hcan : mp = true → 0xffff000000000000 ≤ va.val → c = absCapA (capA st s slot)) + {s' : S} {a' : A} + (hdec : syscall.decode_invocation st ar s a info.label (msgLenA info buf) slot (capA st s slot) bB bC bD false + buf extra = ok (core.result.Result.Ok (), s', a')) : + ∃ Out, DispScope4A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ m Clock hPl.ipa info.label + (msgLenA info buf) slot bB bC bD false buf extra Out := by + have hf : cdt.is_final_capability st s slot = ok true := by + rw [hcap, decodePt_programA, hlab] at hdec + unfold vspace.decode_page_table at hdec + simp only [cur_thread_ok, bind_ok, bind_tc_ok] at hdec + rw [if_pos trivial] at hdec + obtain ⟨b, hb, h⟩ := bind_eq_ok hdec + cases b + · rw [if_neg Bool.false_ne_true] at h + have := okinj h + simp only [Prod.mk.injEq] at this + cases this.1 + · exact hb + cases mp with + | false => + exact J.ptUnmapU_scopeA hA hM hPl hfm Q hlab (bB := bB) (bC := bC) (bD := bD) (bF := false) (ptr := ptr) + (asid := asid) (va := va) (hcap := hcap) (hf := hf) + | true => + have hpS : ptr ∈ Z.S1 ∧ 1 ≤ Z.Lv ptr := + J.ptMappedA Q.F.pin.habs (by rw [← Q.F.pin.hslot]; exact hcap) (hT2 rfl) + have hc : c = absCapA (capA st s slot) := by + by_cases h : va.val < 0xffff000000000000 + · have e : CapEqA (.PageTable ptr true asid va) c := hcap ▸ Q.F.cap.2 + rw [hcap]; exact ptCanon_loA e h + · exact hcan rfl (by omega) + have S1 : (asidRootKind σ (BitVec.ofNat 16 asid.val) = none ∨ + asidRootKind σ (BitVec.ofNat 16 asid.val) = some .stage1) → + ∃ Out, DispScope4A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ m Clock hPl.ipa info.label + (msgLenA info buf) slot bB bC bD false buf extra Out := fun hS1 => + J.ptUnmapS1_scopeA hA hM hPl hfm Q hlab (bB := bB) (bC := bC) (bD := bD) (bF := false) (K := K) + (hTO := J.tabS1) (hEO := J.tabExt) (ptr := ptr) (asid := asid) (va := va) (hpS := hpS) (hcap := hcap) + (hf := hf) (hS1 := hS1) (hcan := hc) + rcases hk : asidRootKind σ (BitVec.ofNat 16 asid.val) with _ | k + · exact S1 (Or.inl hk) + by_cases hk1 : k = .stage1 + · exact S1 (Or.inr (by rw [hk, hk1])) + exact J.ptUnmapX_scopeA hA hM hPl hfm Q hlab (bB := bB) (bC := bC) (bD := bD) (bF := false) (K := K) + (hTO := J.tabS1) (hEO := J.tabExt) (hIpa := hIpa) (ptr := ptr) (asid := asid) (va := va) (hpS := hpS) + (hcap := hcap) (hf := hf) (k := k) (hkR := hk) (hkR1 := hk1) (hcan := hc) + +/-- **`ARMPageTableMap` into a Stage-1 root** (module docstring). -/ +theorem JSyscallA.ptMapS1_recA (hlab : info.label = fl4_abi.label.ARMPageTableMap) {ptr asid va : U64} + {mp bB bC bD : Bool} (hcap : capA st s slot = .PageTable ptr mp asid va) {x0 : U64} + (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) + (hS1 : ∀ p l mp' asid va, capA st s x0 ≠ .Ept p l mp' asid va) + (hHi : ∀ p mp' asid, capA st s x0 ≠ .HighVSpace p mp' asid) + -- open (module docstring): every retyped table is already in `Z.S1`; Stage-2 tables + (hnt : ptr ∉ Z.S1) (hT2 : ptr ∉ Z.T2) {s' : S} {a' : A} + (hdec : syscall.decode_invocation st ar s a info.label (msgLenA info buf) slot (capA st s slot) bB bC bD false + buf extra = ok (core.result.Result.Ok (), s', a')) : + ∃ Out, DispScope4A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ m Clock hPl.ipa info.label + (msgLenA info buf) slot bB bC bD false buf extra Out := by + have hmp : mp = false := by + have h := hdec + rw [hcap, decodePt_programA, hlab] at h + have ne : fl4_abi.label.ARMPageTableMap ≠ fl4_abi.label.ARMPageTableUnmap := by + rw [Ne, u64_eq_iffA]; simp [fl4_abi.label.ARMPageTableUnmap, fl4_abi.label.ARMPageTableMap] + have bad : ∀ {m : Result (core.result.Result Unit error.Exc)} {e : error.SysError}, m = ok (.Err (.Sys e)) → + (m >>= fun r => ok (r, s, a)) = ok (core.result.Result.Ok (), s', a') → False := fun {m e} hm h => by + simp only [hm, bind_tc_ok, bind_ok] at h + have := okinj h + simp only [Prod.mk.injEq] at this + cases this.1 + unfold vspace.decode_page_table at h + simp only [cur_thread_ok, bind_ok, bind_tc_ok] at h + rw [if_neg ne, if_neg (by simp), extra0_eqA hx0, bind_ok] at h + by_cases h0 : x0 = 0#u64 + · rw [if_pos h0] at h; exact (bad rfl h).elim + rw [if_neg h0] at h + dsimp only at h + by_cases hl : msgLenA info buf ≥ 2#u64 + swap + · rw [if_neg hl] at h; exact (bad rfl h).elim + rw [if_pos hl] at h + cases mp + · rfl + · rw [if_pos rfl] at h; exact (bad rfl h).elim + subst hmp + have hH := J.ptHeapA Q.hOS hcap + have hS2 : ptr ∉ S2PgE Z.X Z.T2 := by + rintro (h | ⟨r, hr, h | h⟩) + · exact hT2 h + · exact (J.ptOffRootsA hH r (Or.inl hr)).1 h + · exact (J.ptOffRootsA hH r (Or.inl hr)).2 h + exact J.ptMapS1_runA hA hM hPl hfm Q hlab (hcap := hcap) (UP := J.unmPtA Q.hOS hcap hnt) (hS2 := hS2) + (hY := fun h => (J.ptOffRootsA hH ptr (Or.inr h)).1 rfl) (hx0 := hx0) (hS1 := hS1) (hHi := hHi) (hdec := hdec) + +/-- **`ARMPageMap` into a Stage-1 root** (module docstring). -/ +theorem JSyscallA.frameMapS1_recA (hlab : info.label = fl4_abi.label.ARMPageMap) {fp rt asid va : U64} + {sz : capability.FrameSize} {dev mp bB call bD : Bool} (K : ArchKeepsA ar AR) + (hcap : capA st s slot = .Frame fp sz rt dev mp false asid va) + -- [J] a device frame lies in the kernel window + (hdev : dev = true → (core.num.U64.wrapping_sub fp layout.PPTR_BASE).val < 2 ^ 44) {x0 : U64} + (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) + (hS1 : ∀ p l mp' asid va, capA st s x0 ≠ .Ept p l mp' asid va) + (hHi : ∀ p mp' asid, capA st s x0 ≠ .HighVSpace p mp' asid) {s' : S} {a' : A} + (hdec : syscall.decode_invocation st ar s a info.label (msgLenA info buf) slot (capA st s slot) bB call bD false + buf extra = ok (core.result.Result.Ok (), s', a')) : + ∃ Out, DispScope4A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ m Clock hPl.ipa info.label + (msgLenA info buf) slot bB call bD false buf extra Out := + J.frameMapS1_runA hA hM hPl hfm Q hlab (K := K) (hcap := hcap) (FP := J.frPlA Q.hOS hcap Q.F.cap.1 hdev) + (hx0 := hx0) (hS1 := hS1) (hHi := hHi) (hdec := hdec) + +/-- **`ARMPageMap` into the high window** (module docstring). -/ +theorem JSyscallA.frameMapHi_runA (hlab : info.label = fl4_abi.label.ARMPageMap) {fp rt asid va : U64} + {sz : capability.FrameSize} {dev mp bB call bD : Bool} (K : ArchKeepsA ar AR) + (hcap : capA st s slot = .Frame fp sz rt dev mp false asid va) + -- [J] a device frame lies in the kernel window + (hdev : dev = true → (core.num.U64.wrapping_sub fp layout.PPTR_BASE).val < 2 ^ 44) {x0 p1 asid1 : U64} + {mp1 : Bool} (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) + (hx0c : capA st s x0 = .HighVSpace p1 mp1 asid1) {s' : S} {a' : A} + (hdec : syscall.decode_invocation st ar s a info.label (msgLenA info buf) slot (capA st s slot) bB call bD false + buf extra = ok (core.result.Result.Ok (), s', a')) : + ∃ Out, DispScope4A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ m Clock hPl.ipa info.label + (msgLenA info buf) slot bB call bD false buf extra Out := by + have h := hdec + rw [hcap, decodeFrame_programA, hlab] at h + have hres := h + have bad : ∀ {m : Result (core.result.Result Unit error.Exc)} {e : error.SysError}, m = ok (.Err (.Sys e)) → + (m >>= fun r => ok (r, s, a)) = ok (core.result.Result.Ok (), s', a') → False := fun {m e} hm h => by + rw [hm] at h + simp only [bind_ok, bind_tc_ok] at h + have := okinj h + simp only [Prod.mk.injEq] at this + cases this.1 + unfold vspace.decode_frame at h + simp only [vspace.as_frame, cur_thread_ok, bind_ok, bind_tc_ok, ↓reduceIte] at h + unfold vspace.decode_page_map at h + simp only [cur_thread_ok, bind_ok, bind_tc_ok] at h + rw [extra0_eqA hx0, bind_ok] at h + by_cases h0 : x0 = 0#u64 + · rw [if_pos h0] at h; exact (bad rfl h).elim + rw [if_neg h0] at h + dsimp only at h + by_cases hlen : msgLenA info buf ≥ 3#u64 + swap + · rw [if_neg hlen] at h; exact (bad rfl h).elim + rw [if_pos hlen] at h + obtain ⟨vaddr, a0, h⟩ := bind_eq_ok h + obtain ⟨rmask, a1, h⟩ := bind_eq_ok h + obtain ⟨attr, a2, h⟩ := bind_eq_ok h + obtain ⟨rr, hr, h⟩ := bind_eq_ok h + obtain ⟨vm, hvm, -⟩ := bind_eq_ok h + have hx : vspace.extra extra 0#usize = ok (some x0) := by rw [extra0_eqA hx0, if_neg h0] + rw [decFrame_hiA hx hlen a0 a1 a2 hr hvm (by rw [capA_eq, hx0c])] at hres + have err : rr = rightsWA rmask := okinj (hr.symm.trans (from_word_eqA rmask)) + have hvmEq : vmRightsOfA vm = maskVmRights (vmRightsOfA rt) (dataToRights rmask.bv) := by + obtain ⟨v, hv, e⟩ := spec_existsA (mask_vmA rt rr) + rw [okinj (hvm.symm.trans hv), e, err, rightsW_specA] + have ex : x0 = exAtA extra 0 := okinj (hx0.symm.trans (exAtA_idx0 extra)) + have hmem : x0 ∈ extra.val := by + have l3 : extra.val.length = 3 := by simp + rw [ex] + unfold exAtA + rw [List.getD_eq_getElem _ _ (by omega)] + exact List.getElem_mem _ + have h16 : asid1.val < 2 ^ 16 := by + obtain ⟨y, rest, hexc⟩ : ∃ y rest, excaps = y :: rest := by + cases h : excaps with + | nil => exact absurd (ex.trans (Q.F.extra.2.2 0 (by simp [h]) (by omega))) h0 + | cons y rest => exact ⟨y, rest, rfl⟩ + obtain ⟨ps, -, -, -, hrel⟩ := Q.F.extra.2.1 0 (by simp [hexc]) + have hwf := hrel.1 + rw [← ex, capAtA_eq (st := st), hx0c] at hwf + exact hwf + exact J.frameMapHi_scopeA hA hM hPl hfm Q hlab (x0 := x0) (D := _) (dc := capA st s slot) (callD := call) + (rr := rr) (bB := bB) (bD := bD) (bF := false) (K := K) (hTO := J.tabS1) (hEO := J.tabExt) (fp := fp) (rt := rt) + (asid := asid) (va := va) (sz := sz) (dev := dev) (mp := mp) (hcap := hcap) + (FP := J.frPlA Q.hOS hcap Q.F.cap.1 hdev) (p1 := p1) (asid1 := asid1) (mp1 := mp1) (h16 := h16) + (hx0 := Q.hext x0 hmem h0) (hx0c := hx0c) (vaddr := vaddr) (rightsMask := rmask) (vm := vm) (attr := attr) + (hvmEq := hvmEq) (hD := rfl) (d := J.decPreA Q.hOS Q.hext) (hres := hres) (hx := hx) (hlen := hlen) (h0 := a0) + (h1 := a1) (h2 := a2) (hr := hr) (hvm := hvm) + +/-- **`ARMASIDPoolAssign`** (module docstring). -/ +theorem JSyscallA.asidAssign_runA (hlab : info.label = fl4_abi.label.ARMASIDPoolAssign) {p base : U64} + {block call cd : Bool} (hpool : capA st s slot = .AsidPool p base) + {s' : S} {a' : A} + (hdec : syscall.decode_invocation st ar s a info.label (msgLenA info buf) slot (capA st s slot) block call cd false + buf extra = ok (core.result.Result.Ok (), s', a')) : + ∃ Out, DispScope4A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ m Clock hPl.ipa info.label + (msgLenA info buf) slot block call cd false buf extra Out := by + have h := hdec + rw [hpool, decodePool_programA, hlab] at h + have bad : ∀ {m : Result (core.result.Result Unit error.Exc)} {e : error.SysError}, m = ok (.Err (.Sys e)) → + (m >>= fun r => ok (r, s, a)) = ok (core.result.Result.Ok (), s', a') → False := fun {m e} hm h => by + rw [hm] at h + simp only [bind_ok, bind_tc_ok] at h + have := okinj h + simp only [Prod.mk.injEq] at this + cases this.1 + -- the first extra cap + have hx0 := exAtA_idx0 extra + set x0 := exAtA extra 0 with hx0d + unfold vspace.decode at h + simp only [cur_thread_ok, bind_ok, bind_tc_ok] at h + rw [if_neg (by simp), extra0_eqA hx0, bind_ok] at h + by_cases h0 : x0 = 0#u64 + · rw [if_pos h0] at h; exact (bad rfl h).elim + rw [if_neg h0] at h + dsimp only at h + rw [capA_eq, bind_ok] at h + have hx : vspace.extra extra 0#usize = ok (some x0) := by rw [extra0_eqA hx0, if_neg h0] + -- the extra cap is related to the spec's: its slot and its well-formedness + obtain ⟨y, rest, hexc⟩ : ∃ y rest, excaps = y :: rest := by + cases h : excaps with + | nil => exact absurd (Q.F.extra.2.2 0 (by simp [h]) (by omega)) h0 + | cons y rest => exact ⟨y, rest, rfl⟩ + obtain ⟨ps, hth, hsp, hxa, hrel⟩ := Q.F.extra.2.1 0 (by simp [hexc]) + simp only [hexc, List.getElem_cons_zero] at hth hsp hxa hrel + have hd0 : AbsSlotA Z.th y.2 := ⟨ps, hsp, Or.inl hth⟩ + have hxd : x0 = cslotAddrA y.2 := hxa + have hwf : CapWfA (capA st s x0) := by + have := hrel.1 + rwa [← hx0d, capAtA_eq (st := st)] at this + have hbase : base.val < 2 ^ 16 ∧ base.val % 512 = 0 := by + have := Q.F.cap.1 + rw [hpool] at this + exact this + -- the cap is an unassigned root, and the pool and a free ASID are found + have key : ∀ (k : ExtRootKind) (root oldAsid oldVa : U64), capA st s x0 = assignUnmappedA k root oldAsid oldVa → + ∃ Out, DispScope4A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ m Clock hPl.ipa info.label + (msgLenA info buf) slot block call cd false buf extra Out := by + intro k root oldAsid oldVa hc + have hc' : capA st s (cslotAddrA y.2) = assignUnmappedA k root oldAsid oldVa := hxd ▸ hc + have hx' : vspace.extra extra 0#usize = ok (some (cslotAddrA y.2)) := hxd ▸ hx + have run := hdec + rw [hpool, hlab, Q.F.pin.hslot, decodeAssignRunA (st := st) ar s a k root p base oldAsid oldVa sl y.2 + (msgLenA info buf) buf block call cd false extra hx' hc'] at run + unfold assignFl4A at run + obtain ⟨hi, ehi, run⟩ := bind_eq_ok run + obtain ⟨pool, epool, run⟩ := bind_eq_ok run + have errc : ∀ {e : error.SysError} {s3 : S} {a3 : A}, + (ok (core.result.Result.Err (error.Exc.Sys e), s3, a3) : + Result (core.result.Result Unit error.Exc × S × A)) = ok (core.result.Result.Ok (), s', a') → False := + fun h => by + have := okinj h + simp only [Prod.mk.injEq] at this + cases this.1 + by_cases hp0 : pool = 0#u64 + · rw [if_pos hp0] at run; exact (errc run).elim + rw [if_neg hp0] at run + by_cases hpp : pool = p + swap + · rw [if_pos ((bne_iff_ne).2 hpp)] at run; exact (bad rfl run).elim + rw [if_neg (by rw [hpp]; simp), hpp] at run + obtain ⟨i, ei, run⟩ := bind_eq_ok run + by_cases hfull : i = 512#u64 + · rw [if_pos hfull] at run; exact (errc run).elim + -- the ASID table word of `base` + have hiv : hi.val = base.val / 512 := by + have e9 : (vspace.ASID_LOW_BITS : U64).val = 9 := by simp [vspace.ASID_LOW_BITS] + have sp := U64.ShiftRight_spec base vspace.ASID_LOW_BITS (by rw [e9]; norm_num) + obtain ⟨v, hv, e, -⟩ := spec_existsA sp + rw [okinj (ehi.symm.trans hv), e, e9, Nat.shiftRight_eq_div_pow] + have htab : ∀ h' : U64, h'.val = base.val / 512 → L.mem s (atAF (L.ksb s) h') = p := by + intro h' hh' + have e : h' = hi := UScalar.eq_of_val_eq (by rw [hh', hiv]) + rw [vspace.asid_table.eq] at epool + have h127 : hi &&& 127#u64 = hi := by + apply UScalar.eq_of_val_eq + have : hi.val < 128 := by rw [hiv]; omega + rw [UScalar.val_and, show (127#u64 : U64).val = 2 ^ 7 - 1 by rfl, Nat.and_two_pow_sub_one_eq_mod] + exact Nat.mod_eq_of_lt this + have eA : atAF (L.ksb s) hi = wa (L.ksb s) (AtOff hi) := by + unfold atAF AtOff + rw [h127] + rw [e, eA] + exact (okinj epool).trans hpp + exact J.asidAssign_scopeA hA hM hPl hfm Q hlab (p := p) (base := base) (hpool := hpool) (block := block) + (call := call) (cd := cd) (fp := false) (dst := y.2) (hd0 := hd0) (hex := hx') (k := k) (root := root) + (oldAsid := oldAsid) (oldVa := oldVa) (hcap := hc') (htab := htab) (hp0 := fun h => hp0 (hpp.trans h)) (i := i) (hsearch := ei) + (hfull := hfull) + cases hv : capA st s x0 <;> rw [hv] at h <;> dsimp only at h <;> try exact (bad rfl h).elim + case Pml4 q b asid1 => + cases b + · exact key .stage1 q asid1 0#u64 hv + · rw [if_pos rfl] at h; exact (bad rfl h).elim + case Ept q lvl b asid1 va1 => + have hl : lvl = 0#u64 := by + rw [hv] at hwf + exact UScalar.eq_of_val_eq hwf.1 + subst hl + cases b + · exact key .stage2 q asid1 va1 hv + · rw [if_pos rfl] at h; exact (bad rfl h).elim + case HighVSpace q b asid1 => + cases b + · exact key .high q asid1 0#u64 hv + · rw [if_pos rfl] at h; exact (bad rfl h).elim + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3VsTyPostA.lean b/proofs/Fl4ProofsA64/P3/Stage3VsTyPostA.lean new file mode 100644 index 000000000..e78098178 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3VsTyPostA.lean @@ -0,0 +1,95 @@ +/- +P3 (AArch64, merge line, D8 step 5 (ii)): **the record's Stage-1 typing from the final-state J7**. An arm that changes +which tables are reachable (ASIDPool Assign installs a root) cannot keep the record's tables `Tb` / depths `Lv`. Option +A already takes J7 at the final state as an arm hypothesis, and its `S1aR2.core.stage1` is a narrowed typing there. So +the record's new tables are that typing's, joined with the relation's Stage-1 tables `Z.S1` (which the record must +contain); the added tables are unreachable or already typed, and the placement `UtVsOkA` is J7's `VrOK`. + +* `VsTyR.unionTblA`: a narrowed typing still holds with more tables. +* **`vsPost_of_j7A`**: from J7 at `s` and the installed ASID-table entries lying in `Pp`: some `T`, `L` with the + record's typing at `S1 ∪ T` and `L`, and `UtVsOkA` there. +* **`vsPostB_of_j7A`** (12′ Part 2): the same `T`, `L`, and every pinned tracked buffer is off the typed VSpace words. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3J7A +import Fl4ProofsA64.P3.Stage3D1bA +import Fl4ProofsA64.P3.Stage3VsZeroA +import Fl4ProofsA64.P3.Stage3MapUnmA +import Fl4ProofsA64.P1.S1aR2MapReach +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- A narrowed typing holds with more tables: the added ones matter only when reachable, and every reachable table is +already one of the typing's. -/ +theorem VsTyR.unionTblA {s : S} {Pp Tb S1 : Set U64} {Lv : U64 → Nat} + (h : VsTyR st (L.ksb s) (PoolSet st Pp s) ∅ Tb Lv s) : + VsTyR st (L.ksb s) (PoolSet st Pp s) ∅ (S1 ∪ Tb) Lv s := by + refine ⟨h.tbl, fun p hp o ho h8 hne => ?_, fun p _ hr hl o ho h8 h3 => ?_⟩ + · obtain ⟨a, b⟩ := h.pool p hp o ho h8 hne + exact ⟨fun e => ⟨Or.inr (a e).1, (a e).2⟩, fun e => ⟨Or.inr (b e).1, (b e).2⟩⟩ + · obtain ⟨c, d⟩ := h.desc p (VsTyR.reachableMember h hr) hr hl o ho h8 h3 + exact ⟨Or.inr c, d⟩ + +/-- **The record's typing and placement from J7, with the tracked buffers off the new typed words** (12′ Part 2): a +buffer pinned in a live frame is a word of J7's user-frame set `Ub` (`FrUb`), which the placed VSpace words miss +(`VrOK.ub`). -/ +theorem vsPostB_of_j7A {A : Type} {ar : machine.Arch A} {nA : U64} {Z : XSetsA} {H : Heap} {OS Pp S1 : Set U64} + {s : S} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (hj7 : J7A st (J7R2A st ar nA) Z H OS s σ m) + (htbl : ∀ o : U64, 772352 ≤ o.val → o.val < 773376 → o.val % 8 = 0 → L.mem s (wa (L.ksb s) o) ≠ 0#u64 → + L.mem s (wa (L.ksb s) o) ∈ Pp) : + ∃ (T : Set U64) (Lv : U64 → Nat), VsTyR st (L.ksb s) (PoolSet st Pp s) ∅ (S1 ∪ T) Lv s ∧ + UtVsOkA st H OS Z.V Pp (S1 ∪ T) Lv s ∧ + ∀ (Tc : Set U64) (Bf : Set (U64 × Fl4SpecA64.Word)), (∀ bp ∈ Bf, BufPinA st Tc OS s bp.1) → + ∀ b, Fl4ProofsA64.P3.Endp.BufW Bf b → ¬ VsR (L.ksb s) (utVsPtA st Pp (S1 ∪ T) Lv s) b := by + obtain ⟨Pt, Ub, vr, -, -, -, -, -, -, -, -, frub, -, -, R⟩ := hj7 + obtain ⟨PpJ, TbJ, Hr, Te, Kt, LvJ, Re, bits, R2, -, -⟩ := R + have h1 := R2.core.stage1 + have eP : PoolSet st Pp s = PoolSet st PpJ s := by + ext p + constructor + · rintro ⟨-, o, a, b, c, d, e⟩ + have := h1.tbl o a b c (by rw [d]; exact e) + rw [d] at this + exact ⟨this.1, o, a, b, c, d, e⟩ + · rintro ⟨-, o, a, b, c, d, e⟩ + have := htbl o a b c (by rw [d]; exact e) + rw [d] at this + exact ⟨this, o, a, b, c, d, e⟩ + have h1' : VsTyR st (L.ksb s) (PoolSet st Pp s) ∅ TbJ LvJ s := by rw [eP]; exact h1 + have sub : ∀ p ∈ utVsPtA st Pp (S1 ∪ TbJ) LvJ s, p ∈ Pt := by + rintro p (hp | ⟨-, hr⟩) + · rw [eP] at hp + exact R2.place.pools hp + · exact R2.place.tables (VsTyR.reachableMember h1' hr) + refine ⟨TbJ, LvJ, VsTyR.unionTblA h1', + VrOK.monoPtA (VrOK.ofUb vr fun b hb => (Set.notMem_empty b hb).elim) sub, fun T Bf pin b hb hv => ?_⟩ + obtain ⟨bp, hbp, k, hk, rfl⟩ := hb + exact (VrOK.monoPtA vr sub).ub _ hv ((pin bp hbp).ubA frub hk) + +/-- **The record's typing and placement from J7** (module docstring). -/ +theorem vsPost_of_j7A {A : Type} {ar : machine.Arch A} {nA : U64} {Z : XSetsA} {H : Heap} {OS Pp S1 : Set U64} + {s : S} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (hj7 : J7A st (J7R2A st ar nA) Z H OS s σ m) + (htbl : ∀ o : U64, 772352 ≤ o.val → o.val < 773376 → o.val % 8 = 0 → L.mem s (wa (L.ksb s) o) ≠ 0#u64 → + L.mem s (wa (L.ksb s) o) ∈ Pp) : + ∃ (T : Set U64) (Lv : U64 → Nat), VsTyR st (L.ksb s) (PoolSet st Pp s) ∅ (S1 ∪ T) Lv s ∧ + UtVsOkA st H OS Z.V Pp (S1 ∪ T) Lv s := by + obtain ⟨T, Lv, a, b, -⟩ := vsPostB_of_j7A (S1 := S1) hj7 htbl + exact ⟨T, Lv, a, b⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3VsWriteA.lean b/proofs/Fl4ProofsA64/P3/Stage3VsWriteA.lean new file mode 100644 index 000000000..9c8657bcc --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3VsWriteA.lean @@ -0,0 +1,589 @@ +/- +P3 (AArch64, shared by the delete hooks and the merge line's D8 / T1): **`JStage3A` across VSpace table / pool / +ASID-table word writes**, the VSpace sibling of `Stage3SchedWrNA`'s `JStage3A.of_schedWrN`. + +* The write set is any `P` whose words are `VsR` words (`VrOK`'s table pages `Pt` and the ASID table): one word, a + flush loop, a table being zeroed. `vsW_pageA` / `vsW_asidA` build that fact. +* **`VsSpecA σ σ'`**: the spec step changes only non-VCPU arch objects, the machine's opaque part, the VMID fields and + removes ASID-table entries (`DelAsidSpecA`'s `VmA`, pool updates, `dropPoolA`). +* **`JStage3A.of_vsWrite`**: the whole record at `{Z with hw}`. The caller gives the joint relation `JAtZAX` at the + new state (e.g. `delete_asid_jA`, `delete_asid_pool_jA`), `sys` / `sq` / `poolOff` (P1 whole-function theorems), + `unmPt` and the modular slots `j5` / `j7`. Everything else is carried here. + +Lemma hypothesis **`hdp`**: the spec data pages are kept (`VsSpecA` lets arch objects change; `devFr`). + +Lemma hypothesis not given by the record: **`hbf`** (no written word is a word of a tracked IPC buffer of `Bf`). It is +automatic for ASID-table words (`vsW_asid_bufA`); for table / pool words no record fact separates them from `Bf`. + +No existing statement changed. +-/ +import Fl4ProofsA64.P3.Stage3SchedWrNA +import Fl4ProofsA64.P3.DelAsidSpecA +import Fl4ProofsA64.P3.VsTabJA +import Fl4ProofsA64.P3.VsRelA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-- **The spec side of a VSpace word write** (module docstring). -/ +structure VsSpecA (σ σ' : Fl4SpecA64.State Unit) : Prop where + eq : σ' = { σ with + kheap := σ'.kheap + machineState := { σ.machineState with machineStateRest := σ'.machineState.machineStateRest } + archState := { σ.archState with + armAsidTable := σ'.archState.armAsidTable, armAsidMap := σ'.archState.armAsidMap + armVmidTable := σ'.archState.armVmidTable, armNextVmid := σ'.archState.armNextVmid } } + kh : ∀ p, σ'.kheap p = σ.kheap p ∨ ∃ a a', σ.kheap p = some (.archObj a) ∧ σ'.kheap p = some (.archObj a') ∧ + (∀ v, a ≠ .vcpu v) ∧ (∀ v, a' ≠ .vcpu v) + asid : ∀ i p, σ'.archState.armAsidTable i = some p → σ.archState.armAsidTable i = some p + +/-- (record edit #8b-B1) A `VsSpecA` step keeps the domain schedule. -/ +theorem VsSpecA.domk {σ σ' : Fl4SpecA64.State Unit} (H : VsSpecA σ σ') : DomKeepA σ σ' := + ⟨congrArg (·.domainIndex) H.eq, congrArg (·.domainStartIndex) H.eq, congrArg (·.domainList) H.eq, + congrArg (·.archState.armUsGlobalVspace) H.eq⟩ + +/-- A `VsSpecA` step keeps `arm_current_vcpu`. -/ +theorem VsSpecA.vcur {σ σ' : Fl4SpecA64.State Unit} (H : VsSpecA σ σ') : + σ'.archState.armCurrentVcpu = σ.archState.armCurrentVcpu := by + have e := congrArg (fun τ => τ.archState.armCurrentVcpu) H.eq + exact e + +/-- A `VmA` step is a `VsSpecA` step. -/ +theorem VsSpecA.of_vmA {σ σ' : Fl4SpecA64.State Unit} (h : VmA σ σ') : VsSpecA σ σ' := by + obtain ⟨mr, am, vt, nv, rfl⟩ := h + exact ⟨rfl, fun _ => Or.inl rfl, fun _ _ h => h⟩ + +theorem VsSpecA.keep {σ σ' : Fl4SpecA64.State Unit} (H : VsSpecA σ σ') {p : Fl4SpecA64.ObjRef} + {o : Fl4SpecA64.KernelObject} (h : σ.kheap p = some o) (hn : ∀ a, o ≠ .archObj a) : σ'.kheap p = some o := by + rcases H.kh p with e | ⟨a, _, e1, -⟩ + · rw [e]; exact h + · rw [h] at e1; exact absurd (Option.some.inj e1) (hn a) + +theorem VsSpecA.back {σ σ' : Fl4SpecA64.State Unit} (H : VsSpecA σ σ') {p : Fl4SpecA64.ObjRef} + {o : Fl4SpecA64.KernelObject} (h : σ'.kheap p = some o) (hn : ∀ a, o ≠ .archObj a) : σ.kheap p = some o := by + rcases H.kh p with e | ⟨_, a', -, e1, -⟩ + · rw [← e]; exact h + · rw [h] at e1; exact absurd (Option.some.inj e1) (hn a') + +/-- (record edit #8) A VSpace word write keeps the queue invariants' ingredients. -/ +theorem QKeepA.of_vsA {σ σ' : Fl4SpecA64.State Unit} (H : VsSpecA σ σ') : QKeepA σ σ' := by + refine ⟨fun t x hx => ⟨x, H.back hx (fun _ e => by cases e), rfl⟩, fun p x' hp => ?_, + fun p x' hp => ?_, fun p y k hp => ?_, + fun p y hp => ⟨y, H.back hp (fun _ e => by cases e), id⟩, + H.domk⟩ + · exact ⟨x', H.back hp (fun _ e => by cases e), List.Sublist.refl _⟩ + · exact ⟨x', H.back hp (fun _ e => by cases e), List.Sublist.refl _⟩ + · exact ⟨y, k, H.back hp (fun _ e => by cases e), fun r => ⟨r, id⟩⟩ + +theorem VsSpecA.none {σ σ' : Fl4SpecA64.State Unit} (H : VsSpecA σ σ') {p : Fl4SpecA64.ObjRef} + (h : σ.kheap p = .none) : σ'.kheap p = .none := by + rcases H.kh p with e | ⟨a, _, e1, -⟩ + · rw [e]; exact h + · rw [h] at e1; cases e1 + +theorem specCapAtA_vsA {σ σ' : Fl4SpecA64.State Unit} (HS : VsSpecA σ σ') (sl : Fl4SpecA64.CslotPtr) : + specCapAtA σ' sl = specCapAtA σ sl := by + unfold specCapAtA + rcases HS.kh sl.1 with e | ⟨_, _, e1, e2, -⟩ + · rw [e] + · rw [e1, e2] + +/-- **F3–F6 across a VSpace step** (only non-VCPU arch objects change). -/ +theorem SIdleA.vsA {σ σ' : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} {n : Nat} (h : SIdleA σ m n) + (HS : VsSpecA σ σ') : SIdleA σ' m n where + f3 := fun c hc => by + obtain ⟨x, hx, hs⟩ := h.f3 c hc + exact ⟨x, HS.keep hx (fun _ e => by cases e), hs⟩ + f4 := fun y k hy => h.f4 y k (HS.back hy (fun _ e => by cases e)) + f5 := fun p y k hy he => by + have e : σ'.idleThread = σ.idleThread := by rw [HS.eq] + exact h.f5 p y k (HS.back hy (fun _ e => by cases e)) (by rw [← e]; exact he) + f6 := h.f6.of_eq (specCapAtA_vsA HS) + f7 := fun y k hy => by + have e : σ'.curTime = σ.curTime := by rw [HS.eq] + rw [e]; exact h.f7 y k (HS.back hy (fun _ e => by cases e)) + idleSlot := fun c hc => by + obtain ⟨x, hx, -⟩ := h.f3 c hc + exact (idleVtSameA (by rw [hx]; exact HS.keep hx (fun _ e => by cases e)) (h.idleSlot c hc) (h.idleBound c hc)).1 + idleBound := fun c hc => by + obtain ⟨x, hx, -⟩ := h.f3 c hc + exact (idleVtSameA (by rw [hx]; exact HS.keep hx (fun _ e => by cases e)) (h.idleSlot c hc) (h.idleBound c hc)).2 + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-- A word of a scheduler-or-VSpace table page is a `VsR` word. -/ +theorem vsW_pageA {Z : XSetsA} {OS : Set U64} {s : S} {Pt : Set U64} + (link : ∀ p, SchedPageA st Z OS s p → p ∈ Pt) {p : U64} (hp : SchedPageA st Z OS s p) {i : U64} + (hi : i.val < 512) : VsR (L.ksb s) Pt (peAF p i) := by + refine Or.inl ⟨p, link p hp, core.num.U64.wrapping_mul i 8#u64, ?_, ?_, rfl⟩ + all_goals rw [wm8_valA hi]; omega + +/-- An ASID-table word is a `VsR` word. -/ +theorem vsW_asidA {kb : U64} {Pt : Set U64} {i : U64} (hi : i.val < 128) : VsR kb Pt (atAF kb i) := + Or.inr (asW_iff_atAF.2 ⟨i, hi, rfl⟩) + +/-- A tracked buffer word is no ASID-table word: it lies in a typed-heap `other` object off the kernel-state block. -/ +theorem vsW_asid_bufA {th : TypedHeapA} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {kb : U64} + (P : PlaceA th kb) (Pl : IpcSchedPlacementA th N E Rp Bf) (hfit : kb.val + 788736 ≤ 2 ^ 64) {i : U64} + (hi : i.val < 128) : + ¬ BufW Bf (atAF kb i) := by + intro h + obtain ⟨p, len, hp, h1, h2⟩ := Pl.buffer _ h + have := P.ks p _ hp (fun hn => by have := P.irqK hp hn; cases this) + have hsz : ObjKindA.size (.other len) = len := rfl + have hksz : ksSizeA = 788736 := rfl + have hv := atAF_valA hfit hi + omega + +/-- A VCPU's `RUN_CALLER` word is aligned. -/ +theorem vcAl24A {v : U64} (h : Aligned v) : Aligned (wa v 24#u64) := by + unfold Aligned at h ⊢ + rw [wa_valm, Nat.mod_mod_of_dvd _ (by norm_num : 8 ∣ 2 ^ 64)] + have e : (24#u64 : U64).val = 24 := by simp + rw [e]; omega + +set_option maxHeartbeats 1000000 in +theorem SchedValidA.ofVs {σ σ' : Fl4SpecA64.State Unit} (h : SchedValidA σ) (HS : VsSpecA σ σ') : + SchedValidA σ' := by + have g : σ'.readyQueues = σ.readyQueues ∧ σ'.releaseQueue = σ.releaseQueue ∧ + σ'.schedulerAction = σ.schedulerAction ∧ σ'.curThread = σ.curThread ∧ σ'.idleThread = σ.idleThread ∧ + σ'.curSc = σ.curSc := by + rw [HS.eq]; exact ⟨rfl, rfl, rfl, rfl, rfl, rfl⟩ + obtain ⟨g1, g2, g3, g4, g5, g6⟩ := g + refine h.congr g1 g2 g3 g4 g5 g6 (fun p y k e => ?_) (fun t x e => ?_) (fun t x' e => ?_) + · rcases HS.kh p with e1 | ⟨a, a', e1, -⟩ + · rw [e1]; exact e + · rw [e] at e1; cases e1 + · rcases HS.kh t with e1 | ⟨a, a', e1, -⟩ + · exact ⟨x, by rw [e1]; exact e, rfl⟩ + · rw [e] at e1; cases e1 + · rcases HS.kh t with e1 | ⟨a, a', -, e2, -⟩ + · exact ⟨x', by rw [← e1]; exact e, rfl, rfl⟩ + · rw [e] at e2; cases e2 + +/-- **`JStage3A` across VSpace table / pool / ASID-table word writes** (module docstring). -/ +theorem JStage3A.of_vsWrite {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J5 : JSlotA S} {Rj : Set U64 → JSlotA S} {s s' : S} + {a a' : A} {σ σ' : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7A st Rj) s a σ m) + {hw : HwRootA} {P : U64 → Prop} (w : WritesIn st s s' P) + (hP : ∀ Pt Ub : Set U64, VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt → + (∀ p, SchedPageA st Z OS s p → p ∈ Pt) → ∀ b, P b → VsR (L.ksb s) Pt b) + (hbf : ∀ b, P b → ¬ BufW Bf b) (HS : VsSpecA σ σ') + (hdp : ∀ p dev pg, σ.kheap p = some (.archObj (.dataPage dev pg)) → + σ'.kheap p = some (.archObj (.dataPage dev pg))) + (jx : JAtZAX (L := L) AR { Z with hw := hw } s' a' σ' m) + (sys : SysInvA st H Z.Cs OS U Ac Pp Tb Lv ia ie s') + (vsOk' : UtVsOkA st H OS Z.V Pp Tb Lv s') + (bufVs' : ∀ b, BufW Bf b → ¬ VsR (L.ksb s') (utVsPtA st Pp Tb Lv s') b) + (sq : SchedQueuesInv st H.tcbs Z.Cs H.omega H s' Z.Q Z.R) + (poolOff : PoolOff st H Z.Cs OS s') + (unmPt : UnmPtSpecA st { Z with hw := hw } m OS s' σ') + (j5 : J5 { Z with hw := hw } H OS s' σ' m) + (j7 : J7A st Rj { Z with hw := hw } H OS s' σ' m) + (symL : SymL st H Z.Cs s') (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) : + JStage3A st AR { Z with hw := hw } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7A st Rj) s' a' σ' m := by + have hk := w.1 + have hc := w.2.1 + obtain ⟨Pt, Ub, vr, -, -, -, -, -, -, link, -, -⟩ := J.j7 + have V : ∀ b, P b → VsR (L.ksb s) Pt b := hP Pt Ub vr link + have keep : ∀ b, Aligned b → ¬ P b → L.mem s' b = L.mem s b := fun b hb hn => w.mem_eq hb hn + obtain ⟨Qb, Rb, q⟩ := J.sys.ent.bxi.1 + have hT : ∀ t ∈ Z.T, t ∈ H.tcbs := fun t ht => by rw [J.tcbs]; exact ht + have Pl := J.sr.place + have hf := J.sr.ksFit + have hksz : ksSizeA = 788736 := rfl + -- what a written word is off + have offT : ∀ b, P b → ∀ t ∈ Z.T, b.val < t.val ∨ t.val + 4096 ≤ b.val := fun b hb t ht => by + have := (q.omega b (vr.om b (V b hb))).2.1 t (hT t ht) + omega + have offI : ∀ b, P b → ¬ IpcObjWA N E Rp b := fun b hb => J.ipc_omegaA (vr.om b (V b hb)) (vr.sg b (V b hb)) + have offOS : ∀ b, P b → ¬ InOS OS b := fun b hb => vr.os b (V b hb) + have offSC : ∀ b, P b → ¬ ScFootA Z.C m σ b := fun b hb h => by + obtain ⟨c, hcC, z, hz, hin⟩ := J.scFoot_heapA h + exact vr.sc b (V b hb) c ⟨z, hz⟩ hin + have offN : ∀ b, P b → ¬ ((L.ksb s).val + 223488 ≤ b.val ∧ b.val < (L.ksb s).val + 772352) := fun b hb => by + obtain ⟨-, p, k, hp, -, hin⟩ := vr.om b (V b hb) + exact J.heap_offNodesA hp hin.1 hin.2 + -- the TCB words + have tw : ∀ t ∈ Z.T, ∀ K : U64, K.val + 8 ≤ 4096 → K.val % 8 = 0 → L.mem s' (wa t K) = L.mem s (wa t K) := by + intro t ht K hK h8 + have hta := J.sr.core.tcbs t (ht) + have hv : (wa t K).val = t.val + K.val := field_val hta (K := K) (by omega) + refine keep _ (field_aligned hta (by omega) h8) (fun h => ?_) + have := offT _ h t ht + omega + have osk : OSKeep st OS s s' := by + intro y hy + have al0 := J.sys.ent.os.al0 hy + have hfit := J.sys.ent.os.fit y hy + have key : ∀ o : U64, o.val < 32 → o.val % 8 = 0 → L.mem s' (wa y o) = L.mem s (wa y o) := by + intro o o1 o2 + have hv : (wa y o).val = y.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact keep _ (J.sys.ent.os.al hy o1 o2) (fun h => offOS _ h ⟨y, hy, by omega, by omega⟩) + have e0 : wa y 0#u64 = y := by apply UScalar.eq_of_val_eq; rw [wa_valm]; simp + exact ⟨by have := key 0#u64 (by simp) (by simp); rwa [e0] at this, key 8#u64 (by simp) (by simp), + key 16#u64 (by simp) (by simp), key 24#u64 (by simp) (by simp)⟩ + have capE : ∀ u ∈ OS, capA st s' u = capA st s u := fun u hu => by + unfold capA; rw [(osk u hu).1, (osk u hu).2.1] + -- the spec side + have kT : ∀ p x, σ.kheap p = some (.tcb x) → σ'.kheap p = some (.tcb x) := fun p x h => + HS.keep h (fun _ e => by cases e) + have bT : ∀ p x, σ'.kheap p = some (.tcb x) → σ.kheap p = some (.tcb x) := fun p x h => + HS.back h (fun _ e => by cases e) + have kS : ∀ p x k, σ.kheap p = some (.schedContext x k) → σ'.kheap p = some (.schedContext x k) := + fun p x k h => HS.keep h (fun _ e => by cases e) + have bS : ∀ p x k, σ'.kheap p = some (.schedContext x k) → σ.kheap p = some (.schedContext x k) := + fun p x k h => HS.back h (fun _ e => by cases e) + have gl : SFixA σ σ' := by rw [HS.eq]; exact ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ + have act : σ'.schedulerAction = σ.schedulerAction := by rw [HS.eq] + have rq : σ'.readyQueues = σ.readyQueues := by rw [HS.eq] + have rl : σ'.releaseQueue = σ.releaseQueue := by rw [HS.eq] + have um : σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory := by rw [HS.eq] + have hcdt : σ'.cdt = σ.cdt := by rw [HS.eq] + have horig : σ'.isOriginalCap = σ.isOriginalCap := by rw [HS.eq] + have SS : SStepA σ σ' := + { fix := gl + act := act + rq := rq + rel := rl + sc := fun p x k h => (kS p x k h).trans h.symm + tcb := fun p x h => ⟨x, kT p x h, rfl, rfl, rfl⟩ + scBack := fun p x k h => (bS p x k h).trans h.symm + tcbBack := fun p y h => ⟨y, bT p y h⟩ + ntfnBack := fun p y h => ⟨y, HS.back h (fun _ e => by cases e), rfl⟩ } + have NB : NoBlockESc σ σ' := fun p x y h1 h2 => by + rw [kT p x h1] at h2 + cases h2 + exact Or.inl ⟨rfl, id⟩ + -- the scheduler relation + have sr0 : SchedSRA st Z m κ s' σ' := by + refine J.sr.stable w (fun b hb hr => ?_) SS NB + rcases hr with ⟨c, hcs, o, ho, e⟩ | e | e | ⟨t, ht, K, hK, e⟩ | h + · have hin := J.sr.toSchedSRN.node_in hcs ho + obtain ⟨nn, -, hn, hcs'⟩ := J.sr.csN + have hcl : c.val < 128 := by have := (hcs' c).1 hcs; omega + have hv := nodeAt_valSc (s := s) J.sr.ksFit hcl ho + exact offN b hb (by rw [e, hv]; omega) + · exact vr.ks b (V b hb) 8#u64 (by simp) e.symm + · exact vr.ks b (V b hb) 16#u64 (by simp) e.symm + · have hta := J.sr.core.tcbs t ht + have hKv : K.val + 8 ≤ 4096 := by rcases hK with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> simp + have hv : (wa t K).val = t.val + K.val := field_val hta (K := K) (by omega) + have := offT b hb t ht + rw [e, hv] at this + omega + · exact offSC b hb h + have sr : SchedSRA st { Z with hw := hw } m κ s' σ' := + ⟨{ sr0.toSchedSRN with rest := { sr0.rest with } }, sr0.rd⟩ + -- the IPC relations + have ef : Endp.FootEq Z.T E Rp Bf (L.mem s) (L.mem s') := by + intro b hb h + refine keep b hb (fun hp => ?_) + rcases h with ⟨e, he, h⟩ | ⟨r, hr, h⟩ | ⟨t, ht, h⟩ | h + · exact offI b hp (Or.inr (Or.inl ⟨e, he, by omega, by omega⟩)) + · exact offI b hp (Or.inr (Or.inr ⟨r, hr, by omega, by omega⟩)) + · have := offT b hp t ht + rcases h with ⟨h1, h2⟩ | h + · unfold TOffN at h2; omega + · unfold TL at h; omega + · exact hbf b hp h + have ekv : Endp.KView σ σ' := + ⟨fun p x h => HS.keep h (fun _ e => by cases e), fun p x h => ⟨x, HS.keep h (fun _ e => by cases e), rfl⟩, + fun p x h => ⟨x, kT p x h, rfl, rfl, rfl, rfl, rfl⟩, um⟩ + have ep : EpStateRel Z.T E Rp Bf (L.mem s') σ' := J.ep.frame J.iplaced.e ef ekv + have ntfn : NtfnStateRel N Z.T (L.mem s') σ' := by + refine J.ntfn.frame J.iplaced.n (fun b hb h => keep b hb (fun hp => ?_)) + ⟨fun p x h => HS.keep h (fun _ e => by cases e), fun p x h => ⟨x, kT p x h, rfl, rfl, rfl⟩⟩ + rcases h with ⟨nn, hn, h1, h2⟩ | ⟨t, ht, h⟩ + · exact offI b hp (Or.inl ⟨nn, hn, h1, by omega⟩) + · have hta := J.sr.core.tcbs t ht + have := offT b hp t ht + have fv : ∀ K : U64, K.val < 4096 → (wa t K).val = t.val + K.val := fun K hK => tfield_val hta hK + have inT : t.val ≤ b.val ∧ b.val < t.val + 4096 := by + rcases h with rfl | rfl | rfl | rfl | rfl | rfl + · rw [stA_eq, fv _ (by simp)]; simp + · unfold tBlockA; rw [fv _ (by simp)]; simp + · rw [badgeRegA_eq, fv _ (by simp)]; simp + · unfold prioA; rw [fv _ (by simp)]; simp + · rw [nextA_val hta]; simp [nextOff] + · rw [prevA_val hta]; simp [prevOff] + omega + have reply : ReplyStackRelA (L.mem s') Rp Z.C σ' (scNameA m) stack := by + refine J.reply.frame (fun r hr => ?_) (fun c _ x k h => ⟨x, k, kS _ _ _ h, rfl⟩) + (fun r _ x' h => ⟨x', HS.back h (fun _ e => by cases e), rfl⟩) + have hr3 := J.iplaced.e.rep r hr + have key : ∀ o : U64, o.val = 8 ∨ o.val = 16 → L.mem s' (wa r o) = L.mem s (wa r o) := by + intro o ho + have hv : (wa r o).val = r.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + refine keep _ (by unfold Aligned; rw [hv]; omega) (fun hp => ?_) + exact offI _ hp (Or.inr (Or.inr ⟨r, hr, by omega, by omega⟩)) + exact ⟨key _ (Or.inr (by simp [layout.reply.NEXT])), key _ (Or.inl (by simp [layout.reply.PREV]))⟩ + have bufCover : BufCover st Z.T Bf s' σ' := by + intro t ht x b sb v hx hb hl + have hib : IbSame (L := L) s s' t := by + refine ⟨tw t ht _ (by simp) (by simp), ?_, ?_⟩ + · rw [sbWordA_eq]; exact tw t ht _ (by simp) (by simp) + · rw [sbWordA8_eq]; exact tw t ht _ (by simp) (by simp) + rw [lookup_ipc_buffer_sameA hib] at hl + exact J.bufCover t ht x b sb v (bT _ _ hx) hb hl + have mcp : McpRelA Z.T m (L.mem s') σ' := fun t ht => by + obtain ⟨x, hx, e, hf⟩ := J.mcp t ht + refine ⟨x, kT _ _ hx, ?_, fun g => (hf g).keep ?_ rfl⟩ + · unfold mcpA at e ⊢ + rw [tw t ht _ (by simp) (by simp)] + exact e + · unfold flagsA + exact tw t ht _ (by simp) (by simp) + have ye : ∀ t ∈ Z.T, L.mem s' (yieldA t) = L.mem s (yieldA t) := fun t ht => by + unfold yieldA; exact tw t ht _ (by simp) (by simp) + have yieldRel : YieldRelA Z.T Z.C m (L.mem s') σ' := by + refine ⟨fun t ht => ?_, fun t ht h => ?_, fun c hcc y k h p hp => ?_⟩ + · obtain ⟨x, hx, e⟩ := J.yieldRel.tcb t ht + exact ⟨x, kT _ _ hx, by rw [ye t ht]; exact e⟩ + · rw [ye t ht] at h ⊢ + obtain ⟨hC, y, k, hy, e⟩ := J.yieldRel.toSc t ht h + exact ⟨hC, y, k, kS _ _ _ hy, e⟩ + · obtain ⟨t, ht, e1, e2⟩ := J.yieldRel.frm c hcc y k (bS _ _ _ h) p hp + exact ⟨t, ht, e1, by rw [ye t ht]; exact e2⟩ + have idleNoFault : IdleNoFaultA m σ' := fun c hcn => by + obtain ⟨x, hx, e⟩ := J.idleNoFault c hcn + exact ⟨x, kT _ _ hx, e⟩ + have irqN : σ'.interruptIrqNode = σ.interruptIrqNode := by rw [HS.eq] + have capNone : ∀ p o b c, σ'.kheap p = some o → Fl4SpecA64.capOf o b = some c → σ.kheap p = some o := by + intro p o b c h e + refine HS.back h (fun x ex => ?_) + subst ex + cases e + have table : ∀ k p, Fl4SpecA64.capTableAt k p σ' ↔ Fl4SpecA64.capTableAt k p σ := fun k p => + ⟨fun ⟨cs, h, w⟩ => ⟨cs, HS.back h (fun _ e => by cases e), w⟩, + fun ⟨cs, h, w⟩ => ⟨cs, HS.keep h (fun _ e => by cases e), w⟩⟩ + have irq : SpecIrqA σ' := + { node := by + have h := J.sinv.irqN.node + unfold Fl4SpecA64.IrqNodeFl4 Fl4SpecA64.validIrqNodeFl4 Fl4SpecA64.irqNodeDomFl4 at h ⊢ + simp only [irqN, table]; exact h + slot := fun i hi o b c h e => + J.sinv.irqN.slot i hi o b c (by rw [irqN] at h; exact capNone _ _ _ _ h e) e + ref := fun p o b c h e => by + rw [irqN]; exact J.sinv.irqN.ref p o b c (capNone _ _ _ _ h e) e } + have sinv : SInvXA σ' m n := by + have S := J.sinv + have ct : σ'.curThread = σ.curThread := gl.curThread + have it : σ'.idleThread = σ.idleThread := gl.idleThread + have cs : σ'.curSc = σ.curSc := gl.curSc + have CF := CurFactsA.back (σ := σ) (σ' := σ') ⟨S.zc, S.rlsRun, S.curScReady⟩ (fun t h => by rw [rl] at h; exact h) + (by rw [HS.eq]) cs (by rw [HS.eq]) (fun t x h => ⟨x, bT _ _ h, rfl⟩) (fun t x h _ => ⟨x, kT _ _ h, id⟩) + (fun y k h => ⟨y, bS _ _ _ h, fun a => ⟨a, id⟩⟩) + exact + { mc := S.mc + view := by show σ'.idleThread = _; rw [it]; exact S.view + curTcb := by + obtain ⟨x, hx⟩ := S.curTcb + exact ⟨x, by rw [ct]; exact kT _ _ hx⟩ + idleTcb := by + obtain ⟨x, hx, e⟩ := S.idleTcb + exact ⟨x, by rw [it]; exact kT _ _ hx, e⟩ + onlyIdle := fun t x h e => S.onlyIdle t x (bT _ _ h) e + idleSc := by + obtain ⟨x, y, k, h1, h2, h3, h4⟩ := S.idleSc + exact ⟨x, y, k, by rw [it]; exact kT _ _ h1, h2, kS _ _ _ h3, by rw [it]; exact h4⟩ + curSc := by + obtain ⟨y, k, h⟩ := S.curSc + exact ⟨y, k, by rw [cs]; exact kS _ _ _ h⟩ + ctQ := fun ha d p => by rw [ct, rq]; exact S.ctQ (act.symm.trans ha) d p + idleQ := fun d p c hcn => by rw [rq]; exact S.idleQ d p c hcn + rq := fun d p t h => by + rw [rq] at h + obtain ⟨x, hx, e⟩ := S.rq d p t h + exact ⟨x, kT _ _ hx, e⟩ + rls := fun t h => by + rw [rl] at h + obtain ⟨x, hx⟩ := S.rls t h + exact ⟨x, kT _ _ hx⟩ + rpos := fun p y k h e => S.rpos p y k (bS _ _ _ h) e + zc := CF.1 + rlsRun := CF.2.1 + curScReady := CF.2.2 + faultSt := fun p x h f => S.faultSt p x (bT _ _ h) f + irqN := irq } + have bufNative : BufNativeA Z.T (L.mem s') := + J.bufNative.writesIn J.iplaced.e.tcb w (fun t ht K hK h => by + have hta := J.sr.core.tcbs t ht + have hKv : K.val < 4096 := by rcases hK with rfl | rfl <;> simp + have hv : (wa t K).val = t.val + K.val := field_val hta (K := K) (by omega) + have := offT _ h t ht + omega) + have dead : ∀ k p, DeadA st OS k s' p → DeadA st OS k s p := fun k p h u hu hp => by + have := h u hu + rw [capE u hu] at this + exact this hp + have free : ∀ dev x, UtFreeA st OS s' dev x → UtFreeA st OS s dev x := by + rintro dev x ⟨u, hu, qq, b, fi, d, hcap, h⟩ + exact ⟨u, hu, qq, b, fi, d, (capE u hu).symm.trans hcap, h⟩ + have sca : ∀ sl, specCapAtA σ' sl = specCapAtA σ sl := fun sl => by + unfold specCapAtA + rcases HS.kh sl.1 with e | ⟨_, _, e1, e2, -⟩ + · rw [e] + · rw [e1, e2] + exact + { sys := sys + j := jx + tcbs := J.tcbs + scs := J.scs + sq := sq + ap := by rw [show L.ksb s' = L.ksb s from hk]; exact { J.ap with } + pp := J.pp + s1 := J.s1 + ko := by rw [show L.ksb s' = L.ksb s from hk]; exact J.ko + ptH := J.ptH + tabInj := fun i j p h1 h2 => J.tabInj i j p (HS.asid _ _ h1) (HS.asid _ _ h2) + poolOff := poolOff + objAlloc := ObjAllocA.keep (Z := Z) J.objAlloc osk + idleAnchor := IdleAnchorOffA.keep J.idleAnchor osk + vsVc := by rw [show L.ksb s' = L.ksb s from hk]; exact J.vsVc + sr := sr + scCov := fun p x k h => J.scCov p x k (bS _ _ _ h) + scSize := fun c hcm x k h => J.scSize c hcm x k (bS _ _ _ h) + ep := ep + ntfn := ntfn + reply := reply + bufCover := bufCover + bufNative := bufNative + iplaced := J.iplaced + ipcPlace := J.ipcPlace + idleNoFault := idleNoFault + mcp := mcp + yieldRel := yieldRel + sinv := sinv + setsInH := { J.setsInH with } + utIpc := J.utIpc + ipcTh := J.ipcTh + deadExt := + { vcpu := fun v hv hd x hx => by + refine J.deadExt.vcpu v hv (dead _ v hd) x ?_ + rcases HS.kh (specPtr v) with e | ⟨_, a2, -, e2, -, nv⟩ + · rw [← e]; exact hx + · rw [hx] at e2; cases e2; exact absurd rfl (nv x) + reply := fun r hr hd => J.deadExt.reply r hr (dead _ r hd) + asid := fun i p h hd => J.deadExt.asid i p (HS.asid _ _ h) (dead _ p hd) + tvcpu := fun t ht hd => by + rw [tw t ht 3664#u64 (by simp) (by simp)] + exact J.deadExt.tvcpu t ht (dead _ t hd) + vcDeadRun := fun v hv hd => by + rw [keep _ (vcAl24A (J.ap.vcAl v hv)) (fun hp => vr.vc _ (V _ hp) ⟨v, hv, 24#u64, by simp, by simp, rfl⟩)] + exact J.deadExt.vcDeadRun v hv (dead _ v hd) + replyTcb := fun r hr hd x hx => by + obtain ⟨y, hy, k⟩ := (QKeepA.of_vsA HS).rt _ x hx + exact k (J.deadExt.replyTcb r hr (dead _ r hd) y hy) } + specFree := + { heap := fun p h => HS.none (J.specFree.heap p (free _ _ h)) + cdt := fun x y h p hf => J.specFree.cdt x y (by rw [hcdt] at h; exact h) p (free _ _ hf) + orig := fun x h p hf => J.specFree.orig x (by rw [horig] at h; exact h) p (free _ _ hf) } + utBlk := fun u hu qq b fi d h => J.utBlk u hu qq b fi d ((capE u hu).symm.trans h) + unmPt := unmPt + tabS1 := fun qq hq => + ⟨fun i hi h => (J.tabS1 qq hq).mem i hi + (footA_schedA (Z := Z) (Q := Z.Q) (Rl := Z.R) (curOf := Z.curOf) (curScOf := Z.curScOf) hk hc + (FootA.monoA (Z := { { Z with hw := hw } with S1 := ∅ }) rfl rfl (fun _ h => h) (fun _ h => h) + (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) h)), + (J.tabS1 qq hq).obj⟩ + tabExt := + ⟨fun qq hq i hi h => J.tabExt.mem qq hq i hi + (footA_schedA (Z := extZA Z) (Q := Z.Q) (Rl := Z.R) (curOf := Z.curOf) (curScOf := Z.curScOf) hk hc + (FootA.monoA (Z := extZA { Z with hw := hw }) rfl rfl (fun _ h => h) (fun _ h => h) + (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) h)), + J.tabExt.obj⟩ + specZero := fun x h => by rw [um]; exact J.specZero x (free _ _ h) + origLive := fun x h => J.origLive x (by rw [horig] at h; exact h) + nullOrig := fun sl h => by + rw [horig] + exact J.nullOrig sl (by rw [← sca sl]; exact h) + thPos := fun p z h => J.thPos p z h + frBase := J.frBase.keepA capE + devFr := J.devFr.keepA capE hdp + vsOk := vsOk' + bufAl := fun t ht y h => J.bufAl t ht y (HS.back h (fun _ e => by cases e)) + zombieBnd := J.zombieBnd.keepA capE + devUtOff := J.devUtOff.keepA (Z := Z) capE hk (fun _ _ h => h) rfl + sidle := J.sidle.vsA HS + capTy := J.capTy.keepA capE + scCpu := J.scCpu + scNZ := J.scNZ + vcRun := J.vcRun.keepA (fun t ht => tw t ht 3664#u64 (by simp) (by simp)) (fun v hv => + keep _ (vcAl24A (J.ap.vcAl v hv)) (fun hp => vr.vc _ (V _ hp) ⟨v, hv, 24#u64, by simp, by simp, rfl⟩)) + (fun v hv => keep _ (J.ap.vcAl v hv) (fun hp => vr.vc _ (V _ hp) ⟨v, hv, 0#u64, by simp, by simp, (wa_zeroVcA v).symm⟩)) + (fun t ht => tw t ht 3672#u64 (by simp) (by simp)) + vcSym1 := J.vcSym1.keepA J.vcRun (fun t ht => tw t ht 3664#u64 (by simp) (by simp)) (fun v hv => + keep _ (J.ap.vcAl v hv) (fun hp => vr.vc _ (V _ hp) ⟨v, hv, 0#u64, by simp, by simp, (wa_zeroVcA v).symm⟩)) + runVcpu := J.runVcpu.keep (fun y hy => by rw [stA_eq]; exact tw y hy 3328#u64 (by simp) (by simp)) + (fun y hy => tw y hy 3672#u64 (by simp) (by simp)) + vcSym2 := J.vcSym2.keepA J.vcRun (fun v hv => + keep _ (vcAl24A (J.ap.vcAl v hv)) (fun hp => vr.vc _ (V _ hp) ⟨v, hv, 24#u64, by simp, by simp, rfl⟩)) + (fun t ht => tw t ht 3672#u64 (by simp) (by simp)) + blkQ := J.blkQ.keepA (fun t ht => tw t ht 3336#u64 (by simp) (by simp)) + (fun t _ x' h => Or.inr ⟨x', bT _ x' h, rfl⟩) + (fun p y h => HS.keep h (fun _ e => by cases e)) (fun p y h => HS.keep h (fun _ e => by cases e)) + finCov := J.finCov.keepA (fun q r cs h => HS.back h (fun _ e => by cases e)) + (fun q x h => ⟨x, bT q x h⟩) + idleAll := J.idleAll.keepA + (fun c hcs => specPtr_inj ((sr.sch.idleOf c hcs).symm.trans (J.sr.sch.idleOf c hcs))) + (fun c hcs => specPtr_inj ((sr.sch.idleScOf c hcs).symm.trans (J.sr.sch.idleScOf c hcs))) + ipcHeapCov := J.ipcHeapCov + zombieNoVcpu := J.zombieNoVcpu.keepA capE J.zombieTcb + fun t ht => tw t (J.tcbs ▸ ht) 3664#u64 (by simp) (by simp) + zombieTcb := J.zombieTcb.keepA capE + bufOff := J.bufOff.keepA (Z := Z) (Z' := { Z with hw := hw }) + hk rfl rfl rfl rfl rfl rfl rfl capE + heapWin := J.heapWin + coresAll := J.coresAll + ptCapIn := PtCapInA.of_capsA J.ptCapIn rfl (fun _ h => h) (fun _ h => h) + (fun y hy => Or.inl (capE y (hsub y hy))) + symL := symL + fhOK := fun t ht => by + rw [capE _ (hsub _ (isSlot_fhA ((J.sr.thT t).2 ht)))]; exact J.fhOK t ht + zwf := DelInvA.keepW J.zwf fun y hy => capE y (hsub y hy) + schedValid := J.schedValid.ofVs HS + utWin := J.utWin.keepA capE + devWin := J.devWin.keepA capE + vcBack := vcBack_keepA J.vcRun.vtcb + (fun v hv => keep _ (J.ap.vcAl v hv) (fun hp => vr.vc _ (V _ hp) ⟨v, hv, 0#u64, by simp, by simp, (wa_zeroVcA v).symm⟩)) + (fun t ht => tw t ht 3664#u64 (by simp) (by simp)) J.vcBack + runBack := runBack_keepA J.vcRun.run (fun t ht => tw t ht 3672#u64 (by simp) (by simp)) + (fun v hv => keep _ (vcAl24A (J.ap.vcAl v hv)) (fun hp => vr.vc _ (V _ hp) ⟨v, hv, 24#u64, by simp, by simp, rfl⟩)) + J.runBack + vcSnap := J.vcSnap.keepA (fun v hv o h1 h2 => keep _ (by + have := J.ap.vcAl v hv + unfold Aligned at this ⊢ + rw [wa_valm, Nat.mod_mod_of_dvd _ (by norm_num : 8 ∣ 2 ^ 64)]; omega) + (fun hp => vr.vc _ (V _ hp) ⟨v, hv, o, h1, h2, rfl⟩)) + (fun v _ x hx => by + rcases HS.kh (specPtr v) with e | ⟨_, a2, -, e2, -, nv⟩ + · rw [← e]; exact hx + · rw [hx] at e2; cases e2; exact absurd rfl (nv x)) + vcOS := J.vcOS + userFr := J.userFr.keepA capE hdp + frTh := J.frTh.keepA capE fun _ _ e => e + bfSpec := J.bfSpec + curVcpuNone := HS.vcur.trans J.curVcpuNone + ipcSorted := J.ipcSorted.of_q (QKeepA.of_vsA HS) + activeReplyScs := J.activeReplyScs.of_q (QKeepA.of_vsA HS) + domOK := J.domOK.keepS J.sr.toSchedSRN HS.domk hk hc (fun b hb hd => w.mem_eq hb (fun hp => by obtain ⟨o, ho, eo⟩ := hd.wa_exists J.sr.ksFit; exact vr.ks b (V b hp) o (by omega) eo)) + ipiZero := J.ipiKeepOf w fun o ho hp => J.ipi_omegaA ho (vr.om _ (V _ hp)) + bufVs := bufVs' + j5 := j5 + j7 := j7 } + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3VsWriteTA.lean b/proofs/Fl4ProofsA64/P3/Stage3VsWriteTA.lean new file mode 100644 index 000000000..52fceb37b --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3VsWriteTA.lean @@ -0,0 +1,484 @@ +/- +P3 (AArch64, delete hooks E4, Option T): **`JStage3TA` across VSpace table / pool / ASID-table word writes**: +`P3/Stage3VsWriteA`'s `JStage3A.of_vsWrite` and the record lemmas it uses, for the per-step record `JStage3TA` +(`P3/Stage3TA`; `sys` there was `ent` here: the caller gives `EntryInvA` after the write). Proofs copied verbatim by +`handoff/tools/gen_vswta.sh` (`copy_ta.py`: `JStage3A` → `JStage3TA`, `.sys.ent` → `.ent`). +No named premise. New module. +-/ +import Fl4ProofsA64.P3.DelJ7LA +import Fl4ProofsA64.P3.Stage3TA +import Fl4ProofsA64.P3.Stage3VsWriteA +import Fl4ProofsA64.P3.Stage3SchedWrTA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-- **`SActA` across a VSpace step** (only non-VCPU arch objects change). -/ +theorem SActA.vsA {σ σ' : Fl4SpecA64.State Unit} (h : SActA σ) (HS : VsSpecA σ σ') : SActA σ' where + f1 := fun u e => by + have e1 : σ'.schedulerAction = σ.schedulerAction := by rw [HS.eq] + have e2 : σ'.curThread = σ.curThread := by rw [HS.eq] + have e3 : σ'.idleThread = σ.idleThread := by rw [HS.eq] + rw [e2, e3] + exact h.f1 u (e1 ▸ e) + f2 := by + obtain ⟨x, hx, hs⟩ := h.f2 + have e2 : σ'.curThread = σ.curThread := by rw [HS.eq] + exact ⟨x, by rw [e2]; exact HS.keep hx (fun _ e => by cases e), hs⟩ + +section offTA +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 : JSlotA S} {R : Set U64 → JSlotA S} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7LA st R) s a σ m) +include J + +/-- An SC word lies inside its heap SC object (`scSize`). -/ +theorem JStage3TA.scFoot_heapA {b : U64} (h : ScFootA Z.C m σ b) : + ∃ c ∈ Z.C, ∃ z, H c = some (.sc z) ∧ InSC c (H.scBits c) b := by + obtain ⟨c, hc, x, k, hk, h1, h2⟩ := h + have hcH : c ∈ H.scs := by rw [J.scs]; exact hc + obtain ⟨z, hz⟩ := hcH + refine ⟨c, hc, z, hz, h1, lt_of_lt_of_le h2 ?_⟩ + have := (J.scSize c hc x k hk).1 + exact Nat.add_le_add_left (Nat.pow_le_pow_right (by decide) this) _ + +/-- A word of a heap object that is no state-block `other` region is off the 128 nodes. -/ +theorem JStage3TA.heap_offNodesA {p b : U64} {k : HKind} (hp : H p = some k) (h1 : p.val ≤ b.val) + (h2 : b.val < p.val + 2 ^ k.bits) : + ¬ ((L.ksb s).val + 223488 ≤ b.val ∧ b.val < (L.ksb s).val + 772352) := by + rintro ⟨n1, n2⟩ + by_cases ho : ∃ z, k = .other z + · obtain ⟨z, rfl⟩ := ho + simp only [HKind.bits] at h2 + rcases J.ko.2 p z hp with h | h | h | h <;> omega + · have := J.ko.1 p k hp (fun z e => ho ⟨z, e⟩) + omega + +/-- An IPC word lies in an endpoint, notification or reply heap object. -/ +theorem JStage3TA.ipc_hobjA {b : U64} (h : IpcObjWA N E Rp b) : + ∃ p k, H p = some k ∧ (k = .ep ∨ k = .ntfn ∨ k = .reply) ∧ InObj p k b := by + rcases h with ⟨n, hn, h1, h2⟩ | ⟨e, he, h1, h2⟩ | ⟨r, hr, h1, h2⟩ + · exact ⟨n, .ntfn, J.utIpc.ntfn n hn, Or.inr (Or.inl rfl), h1, by simpa [HKind.bits] using h2⟩ + · exact ⟨e, .ep, J.utIpc.ep e he, Or.inl rfl, h1, by simpa [HKind.bits] using h2⟩ + · exact ⟨r, .reply, J.utIpc.reply r hr, Or.inr (Or.inr rfl), h1, by simpa [HKind.bits] using h2⟩ + +/-- An IPC word is not in `H.omega ∖ H.frame` (the CSpace, VSpace, table and VCPU words). -/ +theorem JStage3TA.ipc_omegaA {b : U64} (hΩ : b ∈ H.omega) (hF : b ∉ H.frame) : ¬ IpcObjWA N E Rp b := by + intro h + obtain ⟨p, k, hp, hk, hin⟩ := J.ipc_hobjA h + obtain ⟨hal, q, kq, hq, hkq, hinq⟩ := hΩ + by_cases e : q = p + · subst e; rw [hp] at hq; cases hq + rcases hk with rfl | rfl | rfl + · rcases hkq with hh | ⟨z, hh⟩ | ⟨z, hh⟩ | ⟨hh, -⟩ <;> cases hh + · rcases hkq with hh | ⟨z, hh⟩ | ⟨z, hh⟩ | ⟨-, hw⟩ <;> try cases hh + exact hF ⟨hal, q, .ntfn, hp, Or.inr (Or.inr ⟨rfl, hw⟩), hin⟩ + · exact hF ⟨hal, q, .reply, hp, Or.inl rfl, hin⟩ + · have := J.ent.heap.disj p k q kq hp hq (Ne.symm e) + unfold InObj at hin hinq + omega + +end offTA + +set_option maxHeartbeats 1000000 in +/-- **`JStage3A` across VSpace table / pool / ASID-table word writes** (module docstring). -/ +theorem JStage3TA.of_vsWrite {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J5 : JSlotA S} {Rj : Set U64 → JSlotA S} {s s' : S} + {a a' : A} {σ σ' : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7LA st Rj) s a σ m) + {hw : HwRootA} {P : U64 → Prop} (w : WritesIn st s s' P) + (hP : ∀ Pt Ub : Set U64, VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt → + (∀ p, SchedPageA st Z OS s p → p ∈ Pt) → ∀ b, P b → VsR (L.ksb s) Pt b) + (hbf : ∀ b, P b → ¬ BufW Bf b) (HS : VsSpecA σ σ') + (hdp : ∀ p dev pg, σ.kheap p = some (.archObj (.dataPage dev pg)) → + σ'.kheap p = some (.archObj (.dataPage dev pg))) + (jx : JAtZAX (L := L) AR { Z with hw := hw } s' a' σ' m) + (ent : EntryInvA3 st H Z.Cs OS U Ac Pp Tb Lv s') + (vsOk' : UtVsOkA st H OS Z.V Pp Tb Lv s') + (bufVs' : ∀ b, BufW Bf b → ¬ VsR (L.ksb s') (utVsPtA st Pp Tb Lv s') b) + (sq : SchedQueuesInv st H.tcbs Z.Cs H.omega H s' Z.Q Z.R) + (poolOff : PoolOff st H Z.Cs OS s') + (unmPt : UnmPtSpecA st { Z with hw := hw } m OS s' σ') + (j5 : J5 { Z with hw := hw } H OS s' σ' m) + (j7 : J7LA st Rj { Z with hw := hw } H OS s' σ' m) : + JStage3TA st AR { Z with hw := hw } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7LA st Rj) s' a' σ' m := by + have hk := w.1 + have hc := w.2.1 + obtain ⟨Pt, Ub, vr, -, -, -, -, -, -, link, -, -⟩ := J.j7 + have V : ∀ b, P b → VsR (L.ksb s) Pt b := hP Pt Ub vr link + have keep : ∀ b, Aligned b → ¬ P b → L.mem s' b = L.mem s b := fun b hb hn => w.mem_eq hb hn + obtain ⟨Qb, Rb, q⟩ := J.ent.bxi.1 + have hT : ∀ t ∈ Z.T, t ∈ H.tcbs := fun t ht => by rw [J.tcbs]; exact ht + have Pl := J.sr.place + have hf := J.sr.ksFit + have hksz : ksSizeA = 788736 := rfl + -- what a written word is off + have offT : ∀ b, P b → ∀ t ∈ Z.T, b.val < t.val ∨ t.val + 4096 ≤ b.val := fun b hb t ht => by + have := (q.omega b (vr.om b (V b hb))).2.1 t (hT t ht) + omega + have offI : ∀ b, P b → ¬ IpcObjWA N E Rp b := fun b hb => J.ipc_omegaA (vr.om b (V b hb)) (vr.sg b (V b hb)) + have offOS : ∀ b, P b → ¬ InOS OS b := fun b hb => vr.os b (V b hb) + have offSC : ∀ b, P b → ¬ ScFootA Z.C m σ b := fun b hb h => by + obtain ⟨c, hcC, z, hz, hin⟩ := J.scFoot_heapA h + exact vr.sc b (V b hb) c ⟨z, hz⟩ hin + have offN : ∀ b, P b → ¬ ((L.ksb s).val + 223488 ≤ b.val ∧ b.val < (L.ksb s).val + 772352) := fun b hb => by + obtain ⟨-, p, k, hp, -, hin⟩ := vr.om b (V b hb) + exact J.heap_offNodesA hp hin.1 hin.2 + -- the TCB words + have tw : ∀ t ∈ Z.T, ∀ K : U64, K.val + 8 ≤ 4096 → K.val % 8 = 0 → L.mem s' (wa t K) = L.mem s (wa t K) := by + intro t ht K hK h8 + have hta := J.sr.core.tcbs t (ht) + have hv : (wa t K).val = t.val + K.val := field_val hta (K := K) (by omega) + refine keep _ (field_aligned hta (by omega) h8) (fun h => ?_) + have := offT _ h t ht + omega + have osk : OSKeep st OS s s' := by + intro y hy + have al0 := J.ent.os.al0 hy + have hfit := J.ent.os.fit y hy + have key : ∀ o : U64, o.val < 32 → o.val % 8 = 0 → L.mem s' (wa y o) = L.mem s (wa y o) := by + intro o o1 o2 + have hv : (wa y o).val = y.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact keep _ (J.ent.os.al hy o1 o2) (fun h => offOS _ h ⟨y, hy, by omega, by omega⟩) + have e0 : wa y 0#u64 = y := by apply UScalar.eq_of_val_eq; rw [wa_valm]; simp + exact ⟨by have := key 0#u64 (by simp) (by simp); rwa [e0] at this, key 8#u64 (by simp) (by simp), + key 16#u64 (by simp) (by simp), key 24#u64 (by simp) (by simp)⟩ + have capE : ∀ u ∈ OS, capA st s' u = capA st s u := fun u hu => by + unfold capA; rw [(osk u hu).1, (osk u hu).2.1] + -- the spec side + have kT : ∀ p x, σ.kheap p = some (.tcb x) → σ'.kheap p = some (.tcb x) := fun p x h => + HS.keep h (fun _ e => by cases e) + have bT : ∀ p x, σ'.kheap p = some (.tcb x) → σ.kheap p = some (.tcb x) := fun p x h => + HS.back h (fun _ e => by cases e) + have kS : ∀ p x k, σ.kheap p = some (.schedContext x k) → σ'.kheap p = some (.schedContext x k) := + fun p x k h => HS.keep h (fun _ e => by cases e) + have bS : ∀ p x k, σ'.kheap p = some (.schedContext x k) → σ.kheap p = some (.schedContext x k) := + fun p x k h => HS.back h (fun _ e => by cases e) + have gl : SFixA σ σ' := by rw [HS.eq]; exact ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ + have act : σ'.schedulerAction = σ.schedulerAction := by rw [HS.eq] + have rq : σ'.readyQueues = σ.readyQueues := by rw [HS.eq] + have rl : σ'.releaseQueue = σ.releaseQueue := by rw [HS.eq] + have um : σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory := by rw [HS.eq] + have hcdt : σ'.cdt = σ.cdt := by rw [HS.eq] + have horig : σ'.isOriginalCap = σ.isOriginalCap := by rw [HS.eq] + have SS : SStepA σ σ' := + { fix := gl + act := act + rq := rq + rel := rl + sc := fun p x k h => (kS p x k h).trans h.symm + tcb := fun p x h => ⟨x, kT p x h, rfl, rfl, rfl⟩ + scBack := fun p x k h => (bS p x k h).trans h.symm + tcbBack := fun p y h => ⟨y, bT p y h⟩ + ntfnBack := fun p y h => ⟨y, HS.back h (fun _ e => by cases e), rfl⟩ } + have NB : NoBlockESc σ σ' := fun p x y h1 h2 => by + rw [kT p x h1] at h2 + cases h2 + exact Or.inl ⟨rfl, id⟩ + -- the scheduler relation + have sr0 : SchedSRA st Z m κ s' σ' := by + refine J.sr.stable w (fun b hb hr => ?_) SS NB + rcases hr with ⟨c, hcs, o, ho, e⟩ | e | e | ⟨t, ht, K, hK, e⟩ | h + · have hin := J.sr.toSchedSRN.node_in hcs ho + obtain ⟨nn, -, hn, hcs'⟩ := J.sr.csN + have hcl : c.val < 128 := by have := (hcs' c).1 hcs; omega + have hv := nodeAt_valSc (s := s) J.sr.ksFit hcl ho + exact offN b hb (by rw [e, hv]; omega) + · exact vr.ks b (V b hb) 8#u64 (by simp) e.symm + · exact vr.ks b (V b hb) 16#u64 (by simp) e.symm + · have hta := J.sr.core.tcbs t ht + have hKv : K.val + 8 ≤ 4096 := by rcases hK with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> simp + have hv : (wa t K).val = t.val + K.val := field_val hta (K := K) (by omega) + have := offT b hb t ht + rw [e, hv] at this + omega + · exact offSC b hb h + have sr : SchedSRA st { Z with hw := hw } m κ s' σ' := + ⟨{ sr0.toSchedSRN with rest := { sr0.rest with } }, sr0.rd⟩ + -- the IPC relations + have ef : Endp.FootEq Z.T E Rp Bf (L.mem s) (L.mem s') := by + intro b hb h + refine keep b hb (fun hp => ?_) + rcases h with ⟨e, he, h⟩ | ⟨r, hr, h⟩ | ⟨t, ht, h⟩ | h + · exact offI b hp (Or.inr (Or.inl ⟨e, he, by omega, by omega⟩)) + · exact offI b hp (Or.inr (Or.inr ⟨r, hr, by omega, by omega⟩)) + · have := offT b hp t ht + rcases h with ⟨h1, h2⟩ | h + · unfold TOffN at h2; omega + · unfold TL at h; omega + · exact hbf b hp h + have ekv : Endp.KView σ σ' := + ⟨fun p x h => HS.keep h (fun _ e => by cases e), fun p x h => ⟨x, HS.keep h (fun _ e => by cases e), rfl⟩, + fun p x h => ⟨x, kT p x h, rfl, rfl, rfl, rfl, rfl⟩, um⟩ + have ep : EpStateRel Z.T E Rp Bf (L.mem s') σ' := J.ep.frame J.iplaced.e ef ekv + have ntfn : NtfnStateRel N Z.T (L.mem s') σ' := by + refine J.ntfn.frame J.iplaced.n (fun b hb h => keep b hb (fun hp => ?_)) + ⟨fun p x h => HS.keep h (fun _ e => by cases e), fun p x h => ⟨x, kT p x h, rfl, rfl, rfl⟩⟩ + rcases h with ⟨nn, hn, h1, h2⟩ | ⟨t, ht, h⟩ + · exact offI b hp (Or.inl ⟨nn, hn, h1, by omega⟩) + · have hta := J.sr.core.tcbs t ht + have := offT b hp t ht + have fv : ∀ K : U64, K.val < 4096 → (wa t K).val = t.val + K.val := fun K hK => tfield_val hta hK + have inT : t.val ≤ b.val ∧ b.val < t.val + 4096 := by + rcases h with rfl | rfl | rfl | rfl | rfl | rfl + · rw [stA_eq, fv _ (by simp)]; simp + · unfold tBlockA; rw [fv _ (by simp)]; simp + · rw [badgeRegA_eq, fv _ (by simp)]; simp + · unfold prioA; rw [fv _ (by simp)]; simp + · rw [nextA_val hta]; simp [nextOff] + · rw [prevA_val hta]; simp [prevOff] + omega + have reply : ReplyStackRelA (L.mem s') Rp Z.C σ' (scNameA m) stack := by + refine J.reply.frame (fun r hr => ?_) (fun c _ x k h => ⟨x, k, kS _ _ _ h, rfl⟩) + (fun r _ x' h => ⟨x', HS.back h (fun _ e => by cases e), rfl⟩) + have hr3 := J.iplaced.e.rep r hr + have key : ∀ o : U64, o.val = 8 ∨ o.val = 16 → L.mem s' (wa r o) = L.mem s (wa r o) := by + intro o ho + have hv : (wa r o).val = r.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + refine keep _ (by unfold Aligned; rw [hv]; omega) (fun hp => ?_) + exact offI _ hp (Or.inr (Or.inr ⟨r, hr, by omega, by omega⟩)) + exact ⟨key _ (Or.inr (by simp [layout.reply.NEXT])), key _ (Or.inl (by simp [layout.reply.PREV]))⟩ + have bufCover : BufCover st Z.T Bf s' σ' := by + intro t ht x b sb v hx hb hl + have hib : IbSame (L := L) s s' t := by + refine ⟨tw t ht _ (by simp) (by simp), ?_, ?_⟩ + · rw [sbWordA_eq]; exact tw t ht _ (by simp) (by simp) + · rw [sbWordA8_eq]; exact tw t ht _ (by simp) (by simp) + rw [lookup_ipc_buffer_sameA hib] at hl + exact J.bufCover t ht x b sb v (bT _ _ hx) hb hl + have mcp : McpRelA Z.T m (L.mem s') σ' := fun t ht => by + obtain ⟨x, hx, e, hf⟩ := J.mcp t ht + refine ⟨x, kT _ _ hx, ?_, fun g => (hf g).keep ?_ rfl⟩ + · unfold mcpA at e ⊢ + rw [tw t ht _ (by simp) (by simp)] + exact e + · unfold flagsA + exact tw t ht _ (by simp) (by simp) + have ye : ∀ t ∈ Z.T, L.mem s' (yieldA t) = L.mem s (yieldA t) := fun t ht => by + unfold yieldA; exact tw t ht _ (by simp) (by simp) + have yieldRel : YieldRelA Z.T Z.C m (L.mem s') σ' := by + refine ⟨fun t ht => ?_, fun t ht h => ?_, fun c hcc y k h p hp => ?_⟩ + · obtain ⟨x, hx, e⟩ := J.yieldRel.tcb t ht + exact ⟨x, kT _ _ hx, by rw [ye t ht]; exact e⟩ + · rw [ye t ht] at h ⊢ + obtain ⟨hC, y, k, hy, e⟩ := J.yieldRel.toSc t ht h + exact ⟨hC, y, k, kS _ _ _ hy, e⟩ + · obtain ⟨t, ht, e1, e2⟩ := J.yieldRel.frm c hcc y k (bS _ _ _ h) p hp + exact ⟨t, ht, e1, by rw [ye t ht]; exact e2⟩ + have idleNoFault : IdleNoFaultA m σ' := fun c hcn => by + obtain ⟨x, hx, e⟩ := J.idleNoFault c hcn + exact ⟨x, kT _ _ hx, e⟩ + have irqN : σ'.interruptIrqNode = σ.interruptIrqNode := by rw [HS.eq] + have capNone : ∀ p o b c, σ'.kheap p = some o → Fl4SpecA64.capOf o b = some c → σ.kheap p = some o := by + intro p o b c h e + refine HS.back h (fun x ex => ?_) + subst ex + cases e + have table : ∀ k p, Fl4SpecA64.capTableAt k p σ' ↔ Fl4SpecA64.capTableAt k p σ := fun k p => + ⟨fun ⟨cs, h, w⟩ => ⟨cs, HS.back h (fun _ e => by cases e), w⟩, + fun ⟨cs, h, w⟩ => ⟨cs, HS.keep h (fun _ e => by cases e), w⟩⟩ + have irq : SpecIrqA σ' := + { node := by + have h := J.sinv.irqN.node + unfold Fl4SpecA64.IrqNodeFl4 Fl4SpecA64.validIrqNodeFl4 Fl4SpecA64.irqNodeDomFl4 at h ⊢ + simp only [irqN, table]; exact h + slot := fun i hi o b c h e => + J.sinv.irqN.slot i hi o b c (by rw [irqN] at h; exact capNone _ _ _ _ h e) e + ref := fun p o b c h e => by + rw [irqN]; exact J.sinv.irqN.ref p o b c (capNone _ _ _ _ h e) e } + have sinv : SInvXA σ' m n := by + have S := J.sinv + have ct : σ'.curThread = σ.curThread := gl.curThread + have it : σ'.idleThread = σ.idleThread := gl.idleThread + have cs : σ'.curSc = σ.curSc := gl.curSc + have CF := CurFactsA.back (σ := σ) (σ' := σ') ⟨S.zc, S.rlsRun, S.curScReady⟩ (fun t h => by rw [rl] at h; exact h) + (by rw [HS.eq]) cs (by rw [HS.eq]) (fun t x h => ⟨x, bT _ _ h, rfl⟩) (fun t x h _ => ⟨x, kT _ _ h, id⟩) + (fun y k h => ⟨y, bS _ _ _ h, fun a => ⟨a, id⟩⟩) + exact + { mc := S.mc + view := by show σ'.idleThread = _; rw [it]; exact S.view + curTcb := by + obtain ⟨x, hx⟩ := S.curTcb + exact ⟨x, by rw [ct]; exact kT _ _ hx⟩ + idleTcb := by + obtain ⟨x, hx, e⟩ := S.idleTcb + exact ⟨x, by rw [it]; exact kT _ _ hx, e⟩ + onlyIdle := fun t x h e => S.onlyIdle t x (bT _ _ h) e + idleSc := by + obtain ⟨x, y, k, h1, h2, h3, h4⟩ := S.idleSc + exact ⟨x, y, k, by rw [it]; exact kT _ _ h1, h2, kS _ _ _ h3, by rw [it]; exact h4⟩ + curSc := by + obtain ⟨y, k, h⟩ := S.curSc + exact ⟨y, k, by rw [cs]; exact kS _ _ _ h⟩ + ctQ := fun ha d p => by rw [ct, rq]; exact S.ctQ (act.symm.trans ha) d p + idleQ := fun d p c hcn => by rw [rq]; exact S.idleQ d p c hcn + rq := fun d p t h => by + rw [rq] at h + obtain ⟨x, hx, e⟩ := S.rq d p t h + exact ⟨x, kT _ _ hx, e⟩ + rls := fun t h => by + rw [rl] at h + obtain ⟨x, hx⟩ := S.rls t h + exact ⟨x, kT _ _ hx⟩ + rpos := fun p y k h e => S.rpos p y k (bS _ _ _ h) e + zc := CF.1 + rlsRun := CF.2.1 + curScReady := CF.2.2 + faultSt := fun p x h f => S.faultSt p x (bT _ _ h) f + irqN := irq } + have bufNative : BufNativeA Z.T (L.mem s') := + J.bufNative.writesIn J.iplaced.e.tcb w (fun t ht K hK h => by + have hta := J.sr.core.tcbs t ht + have hKv : K.val < 4096 := by rcases hK with rfl | rfl <;> simp + have hv : (wa t K).val = t.val + K.val := field_val hta (K := K) (by omega) + have := offT _ h t ht + omega) + have dead : ∀ k p, DeadA st OS k s' p → DeadA st OS k s p := fun k p h u hu hp => by + have := h u hu + rw [capE u hu] at this + exact this hp + have free : ∀ dev x, UtFreeA st OS s' dev x → UtFreeA st OS s dev x := by + rintro dev x ⟨u, hu, qq, b, fi, d, hcap, h⟩ + exact ⟨u, hu, qq, b, fi, d, (capE u hu).symm.trans hcap, h⟩ + have sca : ∀ sl, specCapAtA σ' sl = specCapAtA σ sl := fun sl => by + unfold specCapAtA + rcases HS.kh sl.1 with e | ⟨_, _, e1, e2, -⟩ + · rw [e] + · rw [e1, e2] + exact + { ent := ent + j := jx + tcbs := J.tcbs + scs := J.scs + sq := sq + ap := by rw [show L.ksb s' = L.ksb s from hk]; exact { J.ap with } + pp := J.pp + s1 := J.s1 + ko := by rw [show L.ksb s' = L.ksb s from hk]; exact J.ko + ptH := J.ptH + tabInj := fun i j p h1 h2 => J.tabInj i j p (HS.asid _ _ h1) (HS.asid _ _ h2) + poolOff := poolOff + objAlloc := ObjAllocA.keep (Z := Z) J.objAlloc osk + idleAnchor := IdleAnchorOffA.keep J.idleAnchor osk + vsVc := by rw [show L.ksb s' = L.ksb s from hk]; exact J.vsVc + sr := sr + scCov := fun p x k h => J.scCov p x k (bS _ _ _ h) + scSize := fun c hcm x k h => J.scSize c hcm x k (bS _ _ _ h) + ep := ep + ntfn := ntfn + reply := reply + bufCover := bufCover + bufNative := bufNative + iplaced := J.iplaced + ipcPlace := J.ipcPlace + idleNoFault := idleNoFault + mcp := mcp + yieldRel := yieldRel + sinv := sinv + sidle := J.sidle.vsA HS + runVcpu := J.runVcpu.keep (fun y hy => by rw [stA_eq]; exact tw y hy 3328#u64 (by simp) (by simp)) + (fun y hy => tw y hy 3672#u64 (by simp) (by simp)) + vr := J.vr_keepA (fun r hr => keep _ (J.rTcbValA hr).2 (fun hp => by + have hv := (J.rTcbValA hr).1 + exact offI _ hp (Or.inr (Or.inr ⟨r, hr, by omega, by omega⟩)))) + (fun y hy => by rw [stA_eq]; exact tw y hy 3328#u64 (by simp) (by simp)) + ciSc := fun u hu p b h => J.ciSc u hu p b ((capE u hu).symm.trans h) + noSwitch := fun u e => J.noSwitch u (by rw [act] at e; exact e) + sact := J.sact.vsA HS + setsInH := { J.setsInH with } + utIpc := J.utIpc + ipcTh := J.ipcTh + deadExt := + { vcpu := fun v hv hd x hx => by + refine J.deadExt.vcpu v hv (dead _ v hd) x ?_ + rcases HS.kh (specPtr v) with e | ⟨_, a2, -, e2, -, nv⟩ + · rw [← e]; exact hx + · rw [hx] at e2; cases e2; exact absurd rfl (nv x) + reply := fun r hr hd => J.deadExt.reply r hr (dead _ r hd) + asid := fun i p h hd => J.deadExt.asid i p (HS.asid _ _ h) (dead _ p hd) + tvcpu := fun t ht hd => by + rw [tw t ht 3664#u64 (by simp) (by simp)] + exact J.deadExt.tvcpu t ht (dead _ t hd) + vcDeadRun := fun v hv hd => by + rw [keep _ (vcAl24A (J.ap.vcAl v hv)) (fun hp => vr.vc _ (V _ hp) ⟨v, hv, 24#u64, by simp, by simp, rfl⟩)] + exact J.deadExt.vcDeadRun v hv (dead _ v hd) + replyTcb := fun r hr hd x hx => by + obtain ⟨y, hy, k⟩ := (QKeepA.of_vsA HS).rt _ x hx + exact k (J.deadExt.replyTcb r hr (dead _ r hd) y hy) } + specFree := + { heap := fun p h => HS.none (J.specFree.heap p (free _ _ h)) + cdt := fun x y h p hf => J.specFree.cdt x y (by rw [hcdt] at h; exact h) p (free _ _ hf) + orig := fun x h p hf => J.specFree.orig x (by rw [horig] at h; exact h) p (free _ _ hf) } + utBlk := fun u hu qq b fi d h => J.utBlk u hu qq b fi d ((capE u hu).symm.trans h) + unmPt := unmPt + tabS1 := fun qq hq => + ⟨fun i hi h => (J.tabS1 qq hq).mem i hi + (footA_schedA (Z := Z) (Q := Z.Q) (Rl := Z.R) (curOf := Z.curOf) (curScOf := Z.curScOf) hk hc + (FootA.monoA (Z := { { Z with hw := hw } with S1 := ∅ }) rfl rfl (fun _ h => h) (fun _ h => h) + (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) h)), + (J.tabS1 qq hq).obj⟩ + tabExt := + ⟨fun qq hq i hi h => J.tabExt.mem qq hq i hi + (footA_schedA (Z := extZA Z) (Q := Z.Q) (Rl := Z.R) (curOf := Z.curOf) (curScOf := Z.curScOf) hk hc + (FootA.monoA (Z := extZA { Z with hw := hw }) rfl rfl (fun _ h => h) (fun _ h => h) + (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) h)), + J.tabExt.obj⟩ + specZero := fun x h => by rw [um]; exact J.specZero x (free _ _ h) + origLive := fun x h => J.origLive x (by rw [horig] at h; exact h) + nullOrig := fun sl h => by + rw [horig] + exact J.nullOrig sl (by rw [← sca sl]; exact h) + thPos := fun p z h => J.thPos p z h + frBase := J.frBase.keepA capE + devFr := J.devFr.keepA capE hdp + vsOk := vsOk' + bufAl := fun t ht y h => J.bufAl t ht y (HS.back h (fun _ e => by cases e)) + zombieBnd := J.zombieBnd.keepA capE + devUtOff := J.devUtOff.keepA (Z := Z) capE hk (fun _ _ h => h) rfl + capTy := J.capTy.keepA capE + scCpu := J.scCpu + scNZ := J.scNZ + vcRun := J.vcRun.keepA (fun t ht => tw t ht 3664#u64 (by simp) (by simp)) (fun v hv => + keep _ (vcAl24A (J.ap.vcAl v hv)) (fun hp => vr.vc _ (V _ hp) ⟨v, hv, 24#u64, by simp, by simp, rfl⟩)) + (fun v hv => keep _ (J.ap.vcAl v hv) (fun hp => vr.vc _ (V _ hp) ⟨v, hv, 0#u64, by simp, by simp, (wa_zeroVcA v).symm⟩)) + (fun t ht => tw t ht 3672#u64 (by simp) (by simp)) + blkQ := J.blkQ.keepA (fun t ht => tw t ht 3336#u64 (by simp) (by simp)) + (fun t _ x' h => Or.inr ⟨x', bT _ x' h, rfl⟩) + (fun p y h => HS.keep h (fun _ e => by cases e)) (fun p y h => HS.keep h (fun _ e => by cases e)) + finCov := J.finCov.keepA (fun q r cs h => HS.back h (fun _ e => by cases e)) + (fun q x h => ⟨x, bT q x h⟩) + idleAll := J.idleAll.keepA + (fun c hcs => specPtr_inj ((sr.sch.idleOf c hcs).symm.trans (J.sr.sch.idleOf c hcs))) + (fun c hcs => specPtr_inj ((sr.sch.idleScOf c hcs).symm.trans (J.sr.sch.idleScOf c hcs))) + ipcHeapCov := J.ipcHeapCov + zombieNoVcpu := J.zombieNoVcpu.keepA capE J.zombieTcb + fun t ht => tw t (J.tcbs ▸ ht) 3664#u64 (by simp) (by simp) + zombieTcb := J.zombieTcb.keepA capE + bufOff := J.bufOff.keepA (Z := Z) (Z' := { Z with hw := hw }) + hk rfl rfl rfl rfl rfl rfl rfl capE + curVcpuNone := HS.vcur.trans J.curVcpuNone + ipcSorted := J.ipcSorted.of_q (QKeepA.of_vsA HS) + activeReplyScs := J.activeReplyScs.of_q (QKeepA.of_vsA HS) + domOK := J.domOK.keepS J.sr.toSchedSRN HS.domk hk hc (fun b hb hd => w.mem_eq hb (fun hp => by obtain ⟨o, ho, eo⟩ := hd.wa_exists J.sr.ksFit; exact vr.ks b (V b hp) o (by omega) eo)) + ipiZero := J.ipiKeepOf w fun o ho hp => J.ipi_omegaA ho (vr.om _ (V _ hp)) + bufVs := bufVs' + j5 := j5 + j7 := j7 } + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3VsZeroA.lean b/proofs/Fl4ProofsA64/P3/Stage3VsZeroA.lean new file mode 100644 index 000000000..33268e6a9 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3VsZeroA.lean @@ -0,0 +1,178 @@ +/- +P3 (AArch64, merge line, D8 step 5 (ii), Architect option X): the two generic facts the **mapped unmap arms** need. + +* **The commute**: a `set_cap` taken after a VSpace step (the VSpace-first order of `perform_pg_inv_unmap` / + `perform_pt_inv_unmap`) is a VSpace step after the same `set_cap` taken first (`VsSpecA.setCapCommA`); `set_cap` + reads only the CNode or TCB object at its slot, which a VSpace step keeps. +* **A VSpace step that only zeroes** (`VsZ`) keeps the record's P1 VSpace typing at the same ghosts + (`VsTyR.zeroStepA`) and `UtVsOkA` (`UtVsOkA.zeroStepA`): the reachable tables only shrink. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3CapVsFrameA +import Fl4ProofsA64.P3.Stage3D1bA +import Fl4ProofsA64.P1.QueueUnmR +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## The commute -/ + +/-- The object `set_cap cap` writes for a slot index `cref` of the object read (`none`: `set_cap` fails). -/ +def setCapObjA (cap : Cap) (cref : CnodeIndex) : KernelObject → Option KernelObject + | .cnode sz cn => + if cref ∈ dom cn ∧ wellFormedCnodeN sz cn then some (.cnode sz (Function.update cn cref (some cap))) else none + | .tcb tcb => + if cref = tcbCnodeIndex 0 then some (.tcb { tcb with tcbCtable := cap }) + else if cref = tcbCnodeIndex 1 then some (.tcb { tcb with tcbVtable := cap }) + else if cref = tcbCnodeIndex 2 then some (.tcb { tcb with tcbIpcframe := cap }) + else if cref = tcbCnodeIndex 3 then some (.tcb { tcb with tcbFaultHandler := cap }) + else if cref = tcbCnodeIndex 4 then some (.tcb { tcb with tcbTimeoutHandler := cap }) + else if cref = tcbCnodeIndex tcbStage2Slot then + some (.tcb { tcb with tcbExt := { tcb.tcbExt with tcbStage2 := cap } }) + else if cref = tcbCnodeIndex tcbHighVSpaceSlot then + some (.tcb { tcb with tcbExt := { tcb.tcbExt with tcbHighVSpace := cap } }) + else none + | _ => none + +/-- **`set_cap`'s outcome as a function of the object read.** -/ +theorem setCap_objA {cap : Cap} {p : ObjRef} {cref : CnodeIndex} {σ : State Unit} {r : Unit × State Unit} + (hr : r ∈ (Fl4SpecA64.setCap (Z := Unit) cap (p, cref) σ).1) : + ∃ o o', σ.kheap p = some o ∧ setCapObjA cap cref o = some o' ∧ + r.2 = { σ with kheap := Function.update σ.kheap p (some o') } := by + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + obtain ⟨e1, e2⟩ := getObject_mem hx + obtain ⟨obj, σ1⟩ := x + simp only at e1 e2 hr + subst e1 + have key : ∀ o, setCapObjA cap cref obj = some o → + r ∈ ((Pure.pure o >>= fun o' => setObject (Z := Unit) p o') σ1).1 → + ∃ o o', σ1.kheap p = some o ∧ setCapObjA cap cref o = some o' ∧ + r.2 = { σ1 with kheap := Function.update σ1.kheap p (some o') } := by + intro o ho h + obtain ⟨y, hy, h⟩ := mem_bindA.1 h + obtain rfl : y = (o, σ1) := hy + obtain ⟨o0, -, hr2⟩ := setObject_mem h + exact ⟨obj, o, e2, ho, hr2⟩ + have kfail : ∀ f : KernelObject → NonDet (State Unit) Unit, r ∈ ((NonDet.fail >>= f) σ1).1 → False := by + intro f h + obtain ⟨y, hy, -⟩ := mem_bindA.1 h + exact absurd hy (Set.notMem_empty _) + cases obj with + | cnode sz cn => + simp only at hr + split_ifs at hr with hd + · exact key _ (by simp only [setCapObjA, if_pos hd]) hr + · exact (kfail _ hr).elim + | tcb t => + simp only at hr + split_ifs at hr <;> + first + | exact (kfail _ hr).elim + | exact key _ (by unfold setCapObjA; simp only; (repeat' split) <;> first | rfl | contradiction) hr + | _ => + try simp only at hr + exact (kfail _ hr).elim + +/-- `set_cap` has an object to write only at a CNode or a TCB. -/ +theorem setCapObjA_not_arch {cap : Cap} {cref : CnodeIndex} {o o' : KernelObject} + (h : setCapObjA cap cref o = some o') : ∀ a, o ≠ .archObj a := by + intro a e + subst e + cases h + +/-- **The commute**: `σ → σu` a VSpace step, `set_cap` taken from `σ` (`σc`) and from `σu` (`σ'`): `σc → σ'` is the +VSpace step. -/ +theorem VsSpecA.setCapCommA {σ σu σc σ' : State Unit} (HS : VsSpecA σ σu) {cap : Cap} {sl : CslotPtr} + (hc : ((), σc) ∈ (Fl4SpecA64.setCap (Z := Unit) cap sl σ).1) + (hu : ((), σ') ∈ (Fl4SpecA64.setCap (Z := Unit) cap sl σu).1) : VsSpecA σc σ' := by + obtain ⟨p, cref⟩ := sl + obtain ⟨o, o', h1, h2, e⟩ := setCap_objA hc + obtain ⟨q, q', g1, g2, f⟩ := setCap_objA hu + simp only at e f + have eq : σu.kheap p = σ.kheap p := by + rcases HS.kh p with h | ⟨a, a', k1, -⟩ + · exact h + · exact absurd (Option.some.inj (h1.symm.trans k1)) (setCapObjA_not_arch h2 a) + have eo : q = o := Option.some.inj (g1.symm.trans (eq.trans h1)) + rw [eo] at g2 + have eo' : q' = o' := Option.some.inj (g2.symm.trans h2) + rw [eo'] at f + have eu := HS.eq + refine ⟨?_, fun r => ?_, fun i x hx => ?_⟩ + · rw [f, e] + conv_lhs => rw [eu] + · rw [f, e] + show Function.update σu.kheap p (some o') r = Function.update σ.kheap p (some o') r ∨ _ + by_cases er : r = p + · subst er + exact Or.inl (by rw [Function.update_self, Function.update_self]) + · simp only [Function.update_of_ne er] + exact HS.kh r + · rw [f] at hx + rw [e] + exact HS.asid i x hx + +/-! ## A VSpace step that only zeroes -/ + +section zero +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {H : Heap} {OS V Pp Tb : Set U64} {Lv : U64 → Nat} {s s' : S} + +/-- `VrOK` over fewer page-table pages. -/ +theorem VrOK.monoPtA {Ω Sg Scs Vc Ub P Q : Set U64} {Z : U64 → Nat} {kb : U64} + (h : VrOK Ω Sg Scs Z OS Vc Ub kb Q) (sub : P ⊆ Q) : VrOK Ω Sg Scs Z OS Vc Ub kb P := by + have m : ∀ b, VsR kb P b → VsR kb Q b := fun b hb => hb.imp (ptw_mono sub) id + exact ⟨fun b hb => h.al b (m b hb), fun b hb => h.om b (m b hb), fun b hb => h.sg b (m b hb), + fun b hb => h.sc b (m b hb), fun b hb => h.os b (m b hb), fun b hb => h.vc b (m b hb), + fun b hb => h.ub b (m b hb), fun b hb => h.ks b (m b hb)⟩ + +/-- The installed pools are the same when the ASID-table words are. -/ +theorem PoolSet.keepAsA (hk : L.ksb s' = L.ksb s) (hs : ∀ b, AsW (L.ksb s) b → L.mem s' b = L.mem s b) : + PoolSet st Pp s' = PoolSet st Pp s := by + have key : ∀ o : U64, 772352 ≤ o.val → o.val < 773376 → o.val % 8 = 0 → + L.mem s' (wa (L.ksb s') o) = L.mem s (wa (L.ksb s) o) := fun o h1 h2 h3 => by + rw [hk]; exact hs _ ⟨o, h1, h2, h3, rfl⟩ + ext p + constructor + · rintro ⟨hp, o, h1, h2, h3, h4, h5⟩ + exact ⟨hp, o, h1, h2, h3, (key o h1 h2 h3).symm.trans h4, h5⟩ + · rintro ⟨hp, o, h1, h2, h3, h4, h5⟩ + exact ⟨hp, o, h1, h2, h3, (key o h1 h2 h3).trans h4, h5⟩ + +/-- **The record's Stage-1 typing across a zeroing VSpace step**: the same ghosts, the reachable tables shrink. -/ +theorem VsTyR.zeroStepA (hk : L.ksb s' = L.ksb s) (hs : ∀ b, AsW (L.ksb s) b → L.mem s' b = L.mem s b) + (z : VsZ st (L.ksb s) (utVsPtA st Pp Tb Lv s) s s') + (h : VsTyR st (L.ksb s) (PoolSet st Pp s) ∅ Tb Lv s) : + VsTyR st (L.ksb s') (PoolSet st Pp s') ∅ Tb Lv s' ∧ + utVsPtA st Pp Tb Lv s' ⊆ utVsPtA st Pp Tb Lv s := by + have ep : PoolSet st Pp s' = PoolSet st Pp s := PoolSet.keepAsA hk hs + have p1 : PoolSet st Pp s ⊆ utVsPtA st Pp Tb Lv s := Set.subset_union_left + have p2 : VsRt st (PoolSet st Pp s) ∅ Tb Lv s ⊆ utVsPtA st Pp Tb Lv s := Set.subset_union_right + have hR : VsTyR st (L.ksb s) (PoolSet st Pp s) ∅ (VsRt st (PoolSet st Pp s) ∅ Tb Lv s) Lv s := h.sub.toR + have y : VsTyR st (L.ksb s) (PoolSet st Pp s) ∅ Tb Lv s' := + (h.sub.keepZ p1 p2 z).toRsub VsRt_sub (hr_rt (Set.empty_subset _)) + refine ⟨by rw [hk, ep]; exact y, ?_⟩ + unfold utVsPtA + rw [ep] + refine Set.union_subset_union_right _ fun p hp => ?_ + exact ⟨hp.1, VsReach.vsz p1 p2 (Set.empty_subset _) hR z hp.2⟩ + +/-- **`UtVsOkA` across a zeroing VSpace step.** -/ +theorem UtVsOkA.zeroStepA (hk : L.ksb s' = L.ksb s) + (sub : utVsPtA st Pp Tb Lv s' ⊆ utVsPtA st Pp Tb Lv s) (h : UtVsOkA st H OS V Pp Tb Lv s) : + UtVsOkA st H OS V Pp Tb Lv s' := by + unfold UtVsOkA at h ⊢ + rw [hk] + exact VrOK.monoPtA h sub + +end zero + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3WakeInvA.lean b/proofs/Fl4ProofsA64/P3/Stage3WakeInvA.lean new file mode 100644 index 000000000..d55c6a9f9 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3WakeInvA.lean @@ -0,0 +1,378 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2a): **`SInvXA ∧ SIdleA ∧ SActA` across the spec's wake of a notification waiter** +(`LoopInvA.wakeA`): `set_thread_state running`, the badge register, then `specWakeTail` (`maybe_donate_sc`, +`possible_switch_to`, the sporadic check). New steps on the spec definitions (the style of `P3/ReplyRemoveInvA`): + +* `LoopInvA.relGrow`, `tcbReleaseEnqueue_stepA`, `postpone_stepA`: the release queue grows by a TCB; +* **`schedContextResume_stepA`**: a resumed SC's thread is postponed (the preservation lemma the donation needs); +* `maybeDonateSc_stepA`: `donate_stepA` (an SC that is not `idle_sc_ptr`) then the resume. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.ReplyRemoveInvA +import Fl4ProofsA64.P3.Stage3SigSchedValidA +import Fl4ProofsA64.P3.NtfnWakeA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open Fl4Spec Fl4SpecA64 Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Notif + +noncomputable section + +/-- The release queue grows by runnable TCBs that either are queued already or are no owner of the current SC with time +consumed (record edit #8b-C2, item 11; the timer flag changes). -/ +theorem LoopInvA.relGrow {σ : State Unit} {m : McState} {n : Nat} (h : LoopInvA σ m n) {q : List ObjRef} {b : Bool} + (hq : ∀ y ∈ q, y ∈ σ.releaseQueue ∨ ∃ x, σ.kheap y = some (.tcb x) ∧ runnable x.tcbState = true ∧ + (x.tcbSchedContext = some σ.curSc → σ.consumedTime = 0)) : + LoopInvA { σ with releaseQueue := q, reprogramTimer := b } m n := + ⟨{ h.sinv with + rls := fun y hy => (hq y hy).elim (h.sinv.rls y) (fun h0 => by obtain ⟨x, hx, -⟩ := h0; exact ⟨x, hx⟩) + rlsRun := fun y hy => (hq y hy).elim (h.sinv.rlsRun y) (fun h0 => by + obtain ⟨x, hx, hr, -⟩ := h0 + exact ⟨x, hx, hr⟩) + zc := fun t x hx e hr => (hq t hr).elim (fun h0 => h.sinv.zc t x hx e h0) (fun h0 => by + obtain ⟨x', hx', -, hz⟩ := h0 + have := KernelObject.tcb.inj (Option.some.inj (hx.symm.trans hx')) + subst this + exact hz e) + irqN := SpecIrqA.of_eq h.sinv.irqN rfl rfl }, h.sidle.congr rfl rfl (BitVec.le_refl _), + h.sact.congr rfl rfl rfl rfl⟩ + +/-- A schedulable thread is a TCB. -/ +theorem schedulable_tcbA {σ : State Unit} {y : ObjRef} (hs : schedulable y σ = true) : + ∃ x, σ.kheap y = some (.tcb x) := by + unfold schedulable at hs + simp only [decide_eq_true_eq] at hs + have h1 := hs.1 + cases hk : σ.kheap y with + | none => simp [optPred, optMapSome, optMap, tcbsOfKh, hk] at h1 + | some o => + cases o with + | tcb x => exact ⟨x, rfl⟩ + | _ => simp [optPred, optMapSome, optMap, tcbsOfKh, tcbOf, hk] at h1 + +/-- **`tcb_release_enqueue`** of a TCB. -/ +theorem tcbReleaseEnqueue_stepA {m : McState} {n : Nat} (t : ObjRef) {σ : State Unit} (h : LoopInvA σ m n) + (ht : ∃ x, σ.kheap t = some (.tcb x) ∧ runnable x.tcbState = true ∧ + (x.tcbSchedContext = some σ.curSc → σ.consumedTime = 0)) {r : Unit × State Unit} + (hr : r ∈ (tcbReleaseEnqueue (Z := Unit) t σ).1) : RStepA m n σ r.2 := by + unfold tcbReleaseEnqueue at hr + obtain ⟨qs, hqs, hr⟩ := eqPres_mem (PresR.gets _) hr + have eqs : qs = σ.releaseQueue := congrArg Prod.fst (show (qs, σ) = (σ.releaseQueue, σ) from hqs) + obtain ⟨u1, h1, hr⟩ := mem_bindA.1 hr + unfold readyTimesAppend at h1 + obtain ⟨hσ1, hmem⟩ := orderedInsert_spec _ _ _ _ h1 + obtain ⟨u2, h2, hr⟩ := mem_bindA.1 hr + have e2 : u2 = ((), { u1.2 with releaseQueue := u1.1 }) := h2 + have e3 : ∃ b, r.2 = { σ with releaseQueue := u1.1, reprogramTimer := b } := by + unfold NonDet.when at hr + split_ifs at hr + · have : r = ((), { u2.2 with reprogramTimer := true }) := hr + refine ⟨true, ?_⟩ + rw [this, e2] + show ({ u1.2 with releaseQueue := u1.1, reprogramTimer := true } : State Unit) = _ + rw [hσ1] + · have : r = u2 := hr + refine ⟨σ.reprogramTimer, ?_⟩ + rw [this, e2] + show ({ u1.2 with releaseQueue := u1.1 } : State Unit) = _ + rw [hσ1] + obtain ⟨b, e3⟩ := e3 + have hq : ∀ y ∈ u1.1, y ∈ σ.releaseQueue ∨ ∃ x, σ.kheap y = some (.tcb x) ∧ runnable x.tcbState = true ∧ + (x.tcbSchedContext = some σ.curSc → σ.consumedTime = 0) := by + intro y hy + rcases hmem y hy with e | e + · subst e; exact Or.inr ht + · rw [eqs] at e + exact Or.inl e + rw [e3] + exact ⟨h.relGrow hq, id, rfl, rfl, fun _ x hx => ⟨x, hx, rfl⟩, fun _ x _ hx => ⟨x, hx, rfl, rfl, rfl⟩, rfl⟩ + +/-- **`postpone`** of an SC whose thread is a TCB. -/ +theorem postpone_stepA {m : McState} {n : Nat} (p : ObjRef) {σ : State Unit} (h : LoopInvA σ m n) + (hS : ∀ y k t', σ.kheap p = some (.schedContext y k) → y.scTcb = some t' → ∃ x, σ.kheap t' = some (.tcb x) ∧ + runnable x.tcbState = true ∧ (x.tcbSchedContext = some σ.curSc → σ.consumedTime = 0)) + {r : Unit × State Unit} (hr : r ∈ (postpone (Z := Unit) p σ).1) : RStepA m n σ r.2 := by + unfold postpone at hr + have hgp : PresR (@Eq (State Unit)) (getScObjRef (Z := Unit) SchedContext.scTcb p) := by + unfold getScObjRef + refine PresR.bind (getSchedContext_eqPres p) (fun _ => ?_) + eqpres + obtain ⟨tcbOpt, hg, hr⟩ := eqPres_mem hgp hr + obtain ⟨tp, htp, hr⟩ := eqPres_mem (PresR.assertOpt tcbOpt) hr + have etp : tcbOpt = some tp := by + have : (tp, σ) ∈ (NonDet.assertOpt tcbOpt σ).1 := htp + cases tcbOpt with + | none => exact absurd this (Set.notMem_empty _) + | some a => exact congrArg some (congrArg Prod.fst (show (tp, σ) = (a, σ) from this)).symm + have hex : ∃ x, σ.kheap tp = some (.tcb x) ∧ runnable x.tcbState = true ∧ + (x.tcbSchedContext = some σ.curSc → σ.consumedTime = 0) := by + unfold getScObjRef at hg + obtain ⟨sc, hsc, hg'⟩ := eqPres_mem (getSchedContext_eqPres p) hg + obtain ⟨-, k, hk⟩ := getSchedContext_mem hsc + have e : tcbOpt = sc.scTcb := congrArg Prod.fst (show (tcbOpt, σ) = (sc.scTcb, σ) from hg') + exact hS sc k tp hk (e.symm.trans etp) + obtain ⟨a, ha, hr⟩ := mem_bindA.1 hr + have A := tcbSchedDeq_stepA tp h ha + obtain ⟨b, hb, hr⟩ := mem_bindA.1 hr + obtain ⟨x, hx, hrun, hz⟩ := hex + have rA := tcbSchedDeq_rlA tp ha + have B := tcbReleaseEnqueue_stepA tp A.inv ⟨x, by rw [rA.2.2.2]; exact hx, hrun, + by rw [rA.2.2.1, rA.2.1]; exact hz⟩ hb + have C : RStepA m n b.2 r.2 := by + have er : r = ((), { b.2 with reprogramTimer := true }) := hr + rw [er] + exact ⟨B.inv.refK ⟨rfl, fun _ => .same _, id⟩, id, rfl, rfl, fun _ x hx => ⟨x, hx, rfl⟩, fun _ x _ hx => ⟨x, hx, rfl, rfl, rfl⟩, rfl⟩ + exact A.trans (B.trans C) + +/-- **`sched_context_resume`**, the current SC's sufficiency asked only when it is the resumed SC with time consumed +(Lift 3d ConfigureFlags: there `commit_time` has zeroed the consumed time). -/ +theorem schedContextResume_stepZA {m : McState} {n : Nat} (p : ObjRef) {σ : State Unit} (h : LoopInvA σ m n) + (hsuf : σ.consumedTime ≠ 0 → p = σ.curSc → ∀ y k, σ.kheap σ.curSc = some (.schedContext y k) → + scActive y = true → refillSufficient 0 (refillHd y) = true) + (hsym : ∀ y k t', σ.kheap p = some (.schedContext y k) → y.scTcb = some t' → + ∀ x, σ.kheap t' = some (.tcb x) → x.tcbSchedContext = some p) + {r : Unit × State Unit} (hr : r ∈ (schedContextResume (Z := Unit) p σ).1) : RStepA m n σ r.2 := by + unfold schedContextResume at hr + obtain ⟨sc, hsc, hr⟩ := eqPres_mem (getSchedContext_eqPres p) hr + obtain ⟨-, k, hk⟩ := getSchedContext_mem hsc + obtain ⟨tp, htp, hr⟩ := eqPres_mem (PresR.assertOpt sc.scTcb) hr + have etp : sc.scTcb = some tp := by + have : (tp, σ) ∈ (NonDet.assertOpt sc.scTcb σ).1 := htp + cases e : sc.scTcb with + | none => rw [e] at this; exact absurd this (Set.notMem_empty _) + | some a => + rw [e] at this + exact congrArg some (congrArg Prod.fst (show (tp, σ) = (a, σ) from this)).symm + obtain ⟨sched, hs, hr⟩ := eqPres_mem (PresR.gets _) hr + have es : sched = schedulable tp σ := congrArg Prod.fst (show (sched, σ) = (schedulable tp σ, σ) from hs) + have refl : r = ((), σ) → RStepA m n σ r.2 := fun e => by rw [e]; exact RStepA.refl h + unfold NonDet.when at hr + split_ifs at hr with hsch + · obtain ⟨ts, -, hr⟩ := eqPres_mem (threadGet_eqPres _ tp) hr + obtain ⟨ready, hrd, hr⟩ := eqPres_mem (getScRefillReady_eqPres p) hr + obtain ⟨suff, hsf, hr⟩ := eqPres_mem (getScRefillSufficient_eqPres p 0) hr + split_ifs at hr with hc + · obtain ⟨d, -, hr⟩ := eqPres_mem (threadGet_eqPres _ tp) hr + obtain ⟨pr, -, hr⟩ := eqPres_mem (threadGet_eqPres _ tp) hr + obtain ⟨q, -, hr⟩ := eqPres_mem (by unfold getTcbQueue; eqpres : PresR (@Eq (State Unit)) + (getTcbQueue (Z := Unit) d pr)) hr + obtain ⟨-, -, hr⟩ := eqPres_mem (PresR.assert _) hr + refine postpone_stepA p h (fun y k' t' hy ht' => ?_) hr + rw [hk] at hy + cases hy + rw [etp] at ht' + cases ht' + have hsch' : schedulable tp σ = true := by rw [← es]; exact hsch + obtain ⟨x, hx⟩ := schedulable_tcbA hsch' + refine ⟨x, hx, (Sched.schedulable_partsA hsch').1 x hx, fun hs => ?_⟩ + obtain ⟨-, hact, hnot⟩ := hc + have hp : p = σ.curSc := by + have := hsym sc k tp hk etp x hx + rw [this] at hs + exact Option.some.inj hs + by_contra hne + have hne0 : sc.scRefills ≠ [] := h.sinv.rpos p sc k hk hact + have hrdy := h.sinv.curScReady hne sc k (by rw [← hp]; exact hk) hact + have hsu := hsuf hne hp sc k (by rw [← hp]; exact hk) hact + rw [getScRefillReady_eqA hk hne0] at hrd + rw [getScRefillSufficient_eqA hk hne0] at hsf + have e1 : ready = refillReady σ.curTime (refillHd sc) := congrArg Prod.fst (Set.mem_singleton_iff.1 hrd) + have e2 : suff = refillSufficient 0 (refillHd sc) := congrArg Prod.fst (Set.mem_singleton_iff.1 hsf) + exact hnot ⟨by rw [e1]; exact hrdy, by rw [e2]; exact hsu⟩ + · exact refl hr + · exact refl hr + +/-- **`sched_context_resume`**. -/ +theorem schedContextResume_stepA {m : McState} {n : Nat} (p : ObjRef) {σ : State Unit} (h : LoopInvA σ m n) + (hsuf : p = σ.curSc → ∀ y k, σ.kheap σ.curSc = some (.schedContext y k) → scActive y = true → + refillSufficient 0 (refillHd y) = true) + (hsym : ∀ y k t', σ.kheap p = some (.schedContext y k) → y.scTcb = some t' → + ∀ x, σ.kheap t' = some (.tcb x) → x.tcbSchedContext = some p) + {r : Unit × State Unit} (hr : r ∈ (schedContextResume (Z := Unit) p σ).1) : RStepA m n σ r.2 := + schedContextResume_stepZA p h (fun _ hp => hsuf hp) hsym hr + +theorem getNtfnObjRef_eqA {σ : State Unit} {np : ObjRef} {y : Notification} (hk : σ.kheap np = some (.notification y)) + (f : Notification → Option ObjRef) : getNtfnObjRef (Z := Unit) f np σ = ({(f y, σ)}, False) := by + unfold getNtfnObjRef getSkObjRef + show (getNotification np >>= _) σ = _ + rw [nd_bind_one (getNotification_eq hk)] + rfl + +/-- **`maybe_donate_sc`** of a thread that is not idle, from a notification whose SC is not `idle_sc_ptr`. -/ +theorem maybeDonateSc_stepA {m : McState} {n : Nat} (t np : ObjRef) {σ : State Unit} (h : LoopInvA σ m n) + (ht : t ≠ σ.idleThread) {y : Notification} (hkn : σ.kheap np = some (.notification y)) + (hsc : ∀ sc, y.ntfnSc = some sc → sc ≠ idleScPtr) (hnr : t ∉ σ.releaseQueue) + (hsuf : y.ntfnSc = some σ.curSc → ∀ y k, σ.kheap σ.curSc = some (.schedContext y k) → scActive y = true → + refillSufficient 0 (refillHd y) = true) + {r : Unit × State Unit} (hr : r ∈ (maybeDonateSc (Z := Unit) t np σ).1) : RStepA m n σ r.2 := by + unfold maybeDonateSc at hr + obtain ⟨scOpt, -, hr⟩ := eqPres_mem (threadGet_eqPres _ t) hr + have refl : r = ((), σ) → RStepA m n σ r.2 := fun e => by rw [e]; exact RStepA.refl h + unfold NonDet.when at hr + split_ifs at hr with h0 + · rw [nd_bind_one' (getNtfnObjRef_eqA hkn _)] at hr + rcases e : y.ntfnSc with _ | scPtr + · rw [e] at hr + exact refl hr + · rw [e] at hr + have hp := hsc scPtr e + simp only [NonDet.maybeM] at hr + have hgs : PresR (@Eq (State Unit)) (getScObjRef (Z := Unit) SchedContext.scTcb scPtr) := by + unfold getScObjRef + refine PresR.bind (getSchedContext_eqPres scPtr) (fun _ => ?_) + eqpres + obtain ⟨fo, hfo, hr⟩ := eqPres_mem hgs hr + split_ifs at hr with h1 + · obtain ⟨u, hu, hr⟩ := mem_bindA.1 hr + have D := donate_stepA h hp ht hnr hu + refine D.1.trans (schedContextResume_stepA scPtr D.1.inv (fun hpc y' k' hy' a => ?_) (fun y' k' t' hy' hs' x' hx' => ?_) hr) + · obtain ⟨x0, k0, hx0⟩ := h.sinv.curSc + obtain ⟨y0, hy0, -, er, em⟩ := D.1.sc σ.curSc x0 k0 hx0 + rw [D.1.csc, hy0] at hy' + have e12 := Option.some.inj hy' + injection e12 with e1 e2 + subst e1 + have a0 : scActive x0 = true := by unfold scActive at a ⊢; rw [← em]; exact a + have := hsuf (by rw [e, hpc, D.1.csc]) x0 k0 hx0 a0 + unfold refillHd at this ⊢ + rw [er]; exact this + · obtain ⟨xd, hxd, hxs⟩ := D.2.1 + have := D.2.2 y' k' hy' + rw [this] at hs' + have e := Option.some.inj hs' + subst e + rw [hxd] at hx' + have := KernelObject.tcb.inj (Option.some.inj hx') + subst this + exact hxs + · exact refl hr + · exact refl hr + +set_option maxHeartbeats 2000000 in +/-- **The three invariants across the spec's wake of a notification waiter** (module docstring). -/ +theorem LoopInvA.wakeGenA {σ1 : State Unit} {m : McState} {n : Nat} (h : LoopInvA σ1 m n) {np d : ObjRef} + {y : Notification} {xd : Tcb} (hkn : σ1.kheap np = some (.notification y)) + (hsc : ∀ sc, y.ntfnSc = some sc → sc ≠ idleScPtr) (hkd : σ1.kheap d = some (.tcb xd)) + (hs : runnable xd.tcbState = false) (hfn : xd.tcbFault = none) (tcur : d ≠ σ1.curThread) + (tidle : d ≠ σ1.idleThread) (tidles : ∀ c < n, m.idleThreadOf c ≠ d) (badge : Word) + (hsuf : ∀ y k, σ1.kheap σ1.curSc = some (.schedContext y k) → scActive y = true → + refillSufficient 0 (refillHd y) = true) + {r : Unit × State Unit} (hr : r ∈ (specWake d np badge σ1).1) : LoopInvA r.2 m n := by + rw [specWake_eq hkd] at hr + have eact : stsAct (updK σ1 d (.tcb { xd with tcbState := .running })) d = stUpdA σ1 d xd .running := by + unfold stsAct + rw [if_neg (fun h => tcur h.1)] + rfl + rw [eact] at hr + have h2 : LoopInvA (stUpdA σ1 d xd .running) m n := + { sinv := h.sinv.restartA hkd tidle rfl (fun e => by cases e) + hfn + sidle := h.sidle.stUpdA hkd tidles + sact := + { f1 := fun u e => h.sact.f1 u e + f2 := by + obtain ⟨z, hz, hz'⟩ := h.sact.f2 + exact ⟨z, (Function.update_of_ne tcur.symm _ _).trans hz, hz'⟩ } } + have hx2 : (stUpdA σ1 d xd .running).kheap d = some (.tcb { xd with tcbState := .running }) := + Function.update_self _ _ _ + have R3 : RefK (stUpdA σ1 d xd .running) + (updK (stUpdA σ1 d xd .running) d (.tcb (setReg { xd with tcbState := .running } badge_register badge))) := by + refine ⟨rfl, fun q => ?_, fun g => g.updNs d _ fun _ _ e => by cases e⟩ + show RefObjA ((stUpdA σ1 d xd .running).kheap q) (Function.update (stUpdA σ1 d xd .running).kheap d _ q) + by_cases e : q = d + · subst e + rw [Function.update_self, hx2] + exact .tcb _ _ ⟨rfl, rfl, rfl, rfl, Or.inl rfl⟩ + · rw [Function.update_of_ne e]; exact .same _ + have h3 := h2.refK R3 + set σ3 := updK (stUpdA σ1 d xd .running) d (.tcb (setReg { xd with tcbState := .running } badge_register badge)) + with hσ3 + have nd : np ≠ d := fun e => by rw [e, hkd] at hkn; cases hkn + have hk3 : σ3.kheap np = some (.notification y) := by + show Function.update (stUpdA σ1 d xd .running).kheap d _ np = _ + rw [Function.update_of_ne nd] + exact (Function.update_of_ne nd _ _).trans hkn + have hkd3 : σ3.kheap d = some (.tcb (setReg { xd with tcbState := .running } badge_register badge)) := + Function.update_self _ _ _ + have cur3 : σ3.curThread = σ1.curThread := rfl + have idle3 : σ3.idleThread = σ1.idleThread := rfl + unfold specWakeTail at hr + obtain ⟨u1, hu1, hr⟩ := mem_bindA.1 hr + have hnr3 : d ∉ σ3.releaseQueue := fun hq => by + obtain ⟨z, hz, hr0⟩ := h.sinv.rlsRun d hq + rw [hkd] at hz + have := KernelObject.tcb.inj (Option.some.inj hz) + subst this + rw [hs] at hr0 + cases hr0 + have hsuf3 : y.ntfnSc = some σ3.curSc → ∀ y k, σ3.kheap σ3.curSc = some (.schedContext y k) → scActive y = true → + refillSufficient 0 (refillHd y) = true := fun hyn y' k' hy' a => by + obtain ⟨x0, k0, hx0⟩ := h.sinv.curSc + have ne : σ1.curSc ≠ d := fun e => by rw [e, hkd] at hx0; cases hx0 + have e3 : σ3.kheap σ1.curSc = σ1.kheap σ1.curSc := by + show Function.update (stUpdA σ1 d xd .running).kheap d _ σ1.curSc = _ + rw [Function.update_of_ne ne] + exact Function.update_of_ne ne _ _ + exact hsuf y' k' (by rw [← e3]; exact hy') a + have D := maybeDonateSc_stepA d np h3 (by rw [idle3]; exact tidle) hk3 hsc hnr3 hsuf3 hu1 + obtain ⟨sched, hsd, hr⟩ := eqPres_mem (PresR.gets _) hr + obtain ⟨u2, hu2, hr⟩ := mem_bindA.1 hr + have K2 : LoopInvA u2.2 m n := by + unfold NonDet.when at hu2 + split_ifs at hu2 with hsch + · obtain ⟨x3, hx3, e3⟩ := D.st d _ hkd3 + have ok : EnqOkA u1.2 m n d := by + refine ⟨by rw [D.cur, cur3]; exact tcur, fun c hc e => ?_, x3, hx3, by rw [e3]; rfl⟩ + obtain ⟨z, hz, hz'⟩ := D.inv.sidle.f3 c hc + rw [e, hx3] at hz + have := KernelObject.tcb.inj (Option.some.inj hz) + subst this + rw [e3] at hz' + cases hz' + exact (LoopInvA.possibleSwitch D.inv ok (by rw [D.idle, idle3]; exact tidle) hu2).1 + · have : u2 = ((), u1.2) := hu2 + rw [this]; exact D.inv + obtain ⟨scopt, -, hr⟩ := eqPres_mem (threadGet_eqPres _ d) hr + exact K2.refK (ifCondRefillUnblockCheck_refK _ _ _ _ r hr) + + +set_option maxHeartbeats 2000000 in +/-- **The three invariants across the spec's wake of a notification waiter** (module docstring). -/ +theorem LoopInvA.wakeA {σ1 : State Unit} {m : McState} {n : Nat} (h : LoopInvA σ1 m n) {np d : ObjRef} + {y : Notification} {xd : Tcb} (hkn : σ1.kheap np = some (.notification y)) + (hsc : ∀ sc, y.ntfnSc = some sc → sc ≠ idleScPtr) (hkd : σ1.kheap d = some (.tcb xd)) + (hst : ∃ o, xd.tcbState = .blockedOnNotification o) (badge : Word) + (hsuf : ∀ y k, σ1.kheap σ1.curSc = some (.schedContext y k) → scActive y = true → + refillSufficient 0 (refillHd y) = true) + {r : Unit × State Unit} (hr : r ∈ (specWake d np badge σ1).1) : LoopInvA r.2 m n := by + obtain ⟨o, hs⟩ := hst + have tcur : d ≠ σ1.curThread := fun e => by + obtain ⟨z, hz, hz'⟩ := h.sact.f2 + rw [← e, hkd] at hz + have := KernelObject.tcb.inj (Option.some.inj hz) + subst this + rw [hs] at hz' + simp at hz' + have tidle : d ≠ σ1.idleThread := fun e => by + obtain ⟨z, hz, hz'⟩ := h.sinv.idleTcb + rw [← e, hkd] at hz + have := KernelObject.tcb.inj (Option.some.inj hz) + subst this + rw [hs] at hz' + cases hz' + have tidles : ∀ c < n, m.idleThreadOf c ≠ d := fun c hc e => by + obtain ⟨z, hz, hz'⟩ := h.sidle.f3 c hc + rw [e, hkd] at hz + have := KernelObject.tcb.inj (Option.some.inj hz) + subst this + rw [hs] at hz' + cases hz' + exact h.wakeGenA hkn hsc hkd (by rw [hs]; rfl) + (FaultStOkA.none h.sinv.faultSt hkd (by rw [hs]; rintro (⟨_, _, h⟩ | ⟨_, h⟩ | h) <;> cases h)) tcur tidle tidles + badge hsuf hr + + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3WakeMsA.lean b/proofs/Fl4ProofsA64/P3/Stage3WakeMsA.lean new file mode 100644 index 000000000..15b264170 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3WakeMsA.lean @@ -0,0 +1,164 @@ +/- +P3 (aarch64, A64-P3 sigwake2): **the spec's machine state across the wake** (`wake_msA`): no step of `specWake` writes +it. The instance list is `P3/Stage3WkKeepA`'s. No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3WkKeepA +import Fl4ProofsA64.P3.NtfnWakeA +import Fl4ProofsA64.P3.IpcXferLoopA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open Fl4SpecA64 Fl4Spec Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Sched + +noncomputable section + +/-- The machine state is kept. -/ +def MsEqA (σ σ' : State Unit) : Prop := σ'.machineState = σ.machineState + +instance : PreO MsEqA := ⟨fun _ => rfl, fun h1 h2 => Eq.trans h2 h1⟩ + +class MsInst {α : Type} (m : SMonad Unit α) : Prop where + pres : PresR MsEqA m + +theorem msEq_maybeM {α : Type} {f : α → SMonad Unit Unit} (hf : ∀ a, PresR MsEqA (f a)) (o : Option α) : + PresR MsEqA (NonDet.maybeM f o) := by + cases o + · exact PresR.pure _ + · exact hf _ + +macro "mspres" : tactic => `(tactic| + repeat' (first + | exact MsInst.pres + | exact PresR.pure _ + | exact PresR.gets _ + | exact PresR.get + | exact PresR.getsThe _ + | exact PresR.assertOpt _ + | exact PresR.assert _ + | exact PresR.fail + | exact PresR.selectF _ + | exact PresR.modify _ (fun _ => rfl) + | refine msEq_maybeM (fun _ => ?_) _ + | refine PresR.bind_pure _ _ ?_ + | refine PresR.bind ?_ (fun _ => ?_) + | refine PresR.when ?_ + | refine PresR.unless' ?_ + | refine PresR.ite' ?_ ?_ + | refine PresR.mapM (fun _ => ?_) _ + | refine PresR.whileLoop (fun _ => ?_) _ + | dsimp only + | split)) + +instance (p : ObjRef) (o : KernelObject) : MsInst (setObject (Z := Unit) p o) := ⟨setObject_msA p o⟩ +instance (f : Tcb → Tcb) (t : ObjRef) : MsInst (threadSet (Z := Unit) f t) := ⟨by unfold threadSet; mspres⟩ +instance (p : ObjRef) (f : SchedContext → SchedContext) : MsInst (updateSchedContext (Z := Unit) p f) := + ⟨by unfold updateSchedContext; mspres⟩ +instance (f : (Option ObjRef → Option ObjRef) → SchedContext → SchedContext) (p : ObjRef) (v : Option ObjRef) : + MsInst (setScObjRef (Z := Unit) f p v) := ⟨by unfold setScObjRef; mspres⟩ +instance (f : (Option ObjRef → Option ObjRef) → Tcb → Tcb) (t : ObjRef) (v : Option ObjRef) : + MsInst (setTcbObjRef (Z := Unit) f t v) := ⟨by unfold setTcbObjRef; mspres⟩ +instance (f : Notification → Option ObjRef) (p : ObjRef) : MsInst (getNtfnObjRef (Z := Unit) f p) := + ⟨by unfold getNtfnObjRef getSkObjRef getSimpleKo; mspres⟩ +instance {α : Type} (f : Tcb → α) (t : ObjRef) : MsInst (threadGet (Z := Unit) f t) := ⟨by unfold threadGet; mspres⟩ +instance (f : Tcb → Option ObjRef) (r : ObjRef) : MsInst (getTcbObjRef (Z := + Unit) f r) := + ⟨by unfold getTcbObjRef; mspres⟩ +instance (r : ObjRef) : MsInst (getThreadState (Z := Unit) r) := ⟨by unfold getThreadState; mspres⟩ +instance (d : Domain) (p : Priority) : MsInst (getTcbQueue (Z := Unit) d p) := ⟨by unfold getTcbQueue; mspres⟩ +instance (d : Domain) (p : Priority) (q : ReadyQueue) : MsInst (setTcbQueue (Z := + Unit) d p q) := + ⟨by unfold setTcbQueue; mspres⟩ +instance (a : ObjRef → List ObjRef → List ObjRef) (t : ObjRef) : MsInst (tcbSchedAction (Z := + Unit) a t) := + ⟨by unfold tcbSchedAction; mspres⟩ +instance (a : SchedulerAction) : MsInst (setSchedulerAction (Z := Unit) a) := ⟨by unfold setSchedulerAction; mspres⟩ +instance (t : ObjRef) : MsInst (tcbReleaseRemove (Z := Unit) t) := ⟨by unfold tcbReleaseRemove; mspres⟩ +instance (p : ObjRef) : MsInst (getObject (Z := Unit) p) := ⟨by unfold getObject; mspres⟩ +instance (p : ObjRef) : MsInst (getSchedContext (Z := Unit) p) := ⟨by unfold getSchedContext; mspres⟩ +instance (f : SchedContext → Option ObjRef) (p : ObjRef) : MsInst (getScObjRef (Z := + Unit) f p) := + ⟨by unfold getScObjRef; mspres⟩ +instance (p : ObjRef) : MsInst (getRefills (Z := Unit) p) := ⟨by unfold getRefills; mspres⟩ +instance (p : ObjRef) : MsInst (getRefillHead (Z := Unit) p) := ⟨by unfold getRefillHead; mspres⟩ +instance (p : ObjRef) : MsInst (getRefillTail (Z := Unit) p) := ⟨by unfold getRefillTail; mspres⟩ +instance (p : ObjRef) : MsInst (getScRefillReady (Z := Unit) p) := ⟨by unfold getScRefillReady; mspres⟩ +instance (p : ObjRef) (u : Ticks) : MsInst (getScRefillSufficient (Z := + Unit) p u) := + ⟨by unfold getScRefillSufficient; mspres⟩ +instance (p : ObjRef) (u : Ticks) : MsInst (fl4GetScRefillSufficient (Z := + Unit) p u) := + ⟨by unfold fl4GetScRefillSufficient; mspres⟩ +instance (p : ObjRef) : MsInst (getScActive (Z := Unit) p) := ⟨by unfold getScActive; mspres⟩ +instance (p : ObjRef) : MsInst (isRoundRobin (Z := Unit) p) := ⟨by unfold isRoundRobin; mspres⟩ +instance : MsInst (rescheduleRequired (Z := Unit)) := ⟨by unfold rescheduleRequired; mspres⟩ +instance (t : ObjRef) : MsInst (possibleSwitchTo (Z := Unit) t) := ⟨by unfold possibleSwitchTo; mspres⟩ +instance (p : ObjRef) (f : Refill → Refill) : MsInst (updateRefillHd (Z := + Unit) p f) := + ⟨by unfold updateRefillHd; mspres⟩ +instance (p : ObjRef) (f : Refill → Refill) : MsInst (updateRefillTl (Z := + Unit) p f) := + ⟨by unfold updateRefillTl; mspres⟩ +instance (p : ObjRef) (r : Refill) : MsInst (refillAddTail (Z := Unit) p r) := ⟨by unfold refillAddTail; mspres⟩ +instance (p : ObjRef) : MsInst (refillPopHead (Z := Unit) p) := ⟨by unfold refillPopHead; mspres⟩ +instance (p : ObjRef) : MsInst (refillSize (Z := Unit) p) := ⟨by unfold refillSize; mspres⟩ +instance (p : ObjRef) : MsInst (refillSingle (Z := Unit) p) := ⟨by unfold refillSingle; mspres⟩ +instance (p : ObjRef) : MsInst (refillFull (Z := Unit) p) := ⟨by unfold refillFull; mspres⟩ +instance (p : ObjRef) (r : Refill) : MsInst (scheduleUsed (Z := Unit) p r) := ⟨by unfold scheduleUsed; mspres⟩ +instance (p : ObjRef) : MsInst (mergeOverlappingRefills (Z := Unit) p) := ⟨by unfold mergeOverlappingRefills; mspres⟩ +instance (p : ObjRef) : MsInst (refillHeadOverlappingLoop (Z := + Unit) p) := + ⟨by unfold refillHeadOverlappingLoop; mspres⟩ +instance (p : ObjRef) : MsInst (refillUnblockCheck (Z := Unit) p) := ⟨by unfold refillUnblockCheck; mspres⟩ +instance (o : Option ObjRef) (a b : Option Bool) : MsInst (ifCondRefillUnblockCheck (Z := + Unit) o a b) := + ⟨by unfold ifCondRefillUnblockCheck; mspres⟩ +instance (p : ObjRef) : MsInst (mergeNonoverlappingHeadRefill (Z := + Unit) p) := + ⟨by unfold mergeNonoverlappingHeadRefill; mspres⟩ +instance (m : Ticks) (p : ObjRef) : MsInst (headInsufficientLoopWith (Z := + Unit) m p) := + ⟨by unfold headInsufficientLoopWith; mspres⟩ +instance (p : ObjRef) (u : Ticks) : MsInst (chargeEntireHeadRefill (Z := + Unit) p u) := + ⟨by unfold chargeEntireHeadRefill; mspres⟩ +instance (l : Ticks) (p : ObjRef) (u : Ticks) : MsInst (handleOverrunWith (Z := + Unit) l p u) := + ⟨by unfold handleOverrunWith; mspres⟩ +instance (l m u : Ticks) : MsInst (refillBudgetCheckWith (Z := + Unit) l m u) := + ⟨by unfold refillBudgetCheckWith; mspres⟩ +instance (u : Ticks) : MsInst (fl4RefillBudgetCheck (Z := Unit) u) := ⟨by unfold fl4RefillBudgetCheck; mspres⟩ +instance (u : Ticks) : MsInst (refillBudgetCheckRoundRobin (Z := + Unit) u) := + ⟨by unfold refillBudgetCheckRoundRobin; mspres⟩ +instance (t : ObjRef) : MsInst (setThreadStateAct (Z := Unit) t) := ⟨by unfold setThreadStateAct; mspres⟩ +instance {α β : Type} [Nonempty β] (t : α) (ts : List α) (f : α → RMonad Unit β) (R : β → β → Prop) : + MsInst (orderedInsert (Z := + Unit) t ts f R) := + ⟨by unfold orderedInsert; mspres⟩ +instance (t : ObjRef) : MsInst (tcbReleaseEnqueue (Z := Unit) t) := ⟨by unfold tcbReleaseEnqueue; mspres⟩ +instance (p : ObjRef) : MsInst (postpone (Z := Unit) p) := ⟨by unfold postpone; mspres⟩ +instance (p : ObjRef) : MsInst (schedContextResume (Z := Unit) p) := ⟨by unfold schedContextResume; mspres⟩ +instance (t : ObjRef) : MsInst (testReschedule (Z := Unit) t) := ⟨by unfold testReschedule; mspres⟩ +instance (sc t : ObjRef) : MsInst (schedContextDonate (Z := Unit) sc t) := ⟨by unfold schedContextDonate; mspres⟩ +instance (t n : ObjRef) : MsInst (maybeDonateSc (Z := Unit) t n) := ⟨by unfold maybeDonateSc; mspres⟩ + +instance (o : Option ObjRef) : MsInst (ifSporadicCurScTestRefillUnblockCheck (Z := Unit) o) := + ⟨by unfold ifSporadicCurScTestRefillUnblockCheck; mspres⟩ + +theorem specWakeTail_msA (t n : ObjRef) : PresR MsEqA (specWakeTail t n) := by + unfold specWakeTail; mspres + +/-- **The spec's wake keeps the machine state.** -/ +theorem wake_msA {σ σ' : State Unit} {np dsp : ObjRef} {x' : Notification} {xd : Tcb} {b : Word} + (hkd : σ.kheap dsp = some (.tcb xd)) (hnd : np ≠ dsp) + (hm : ((), σ') ∈ (specWake dsp np b (updK σ np (.notification x'))).1) : σ'.machineState = σ.machineState := by + have hk1 : (updK σ np (.notification x')).kheap dsp = some (.tcb xd) := by + rw [updK_kheap, Function.update_of_ne hnd.symm]; exact hkd + rw [specWake_eq hk1] at hm + exact (specWakeTail_msA dsp np _ ((), σ') hm).trans + (show (stsAct _ dsp).machineState = σ.machineState by unfold stsAct; split_ifs <;> rfl) + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3WakePInvA.lean b/proofs/Fl4ProofsA64/P3/Stage3WakePInvA.lean new file mode 100644 index 000000000..bded4f692 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3WakePInvA.lean @@ -0,0 +1,566 @@ +/- +P3 (aarch64, A64-P3 sigwake2): **`SInvXA ∧ SIdleA` across the spec's wake, without `SActA`** (`SPairA.wake`). A waking +signal may run with a switch already pending, or with the current thread blocked; `LoopInvA`'s third part (`SActA`) is +then false, while the record (`JStage3A`) only asks for the first two. + +`hatA σ` (current thread := the idle thread, action := `chooseNewThread`) satisfies `SActA`, so the object and queue +steps are `LoopInvA`'s own, at `hatA σ` (`SPairA.hat` / `SPairA.ofHat`). The woken thread may be the current one: its +domain is the current domain (`hdom`), so `possible_switch_to` never enqueues while the action stays +`resumeCurThread`. No named premise. New module. +-/ +import Fl4ProofsA64.P3.Stage3WakeInvA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open Fl4Spec Fl4SpecA64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Notif + +noncomputable section + +/-- The two spec invariants of `JStage3A` (`LoopInvA` without `SActA`). -/ +structure SPairA (σ : State Unit) (m : McState) (n : Nat) : Prop where + sinv : SInvXA σ m n + sidle : SIdleA σ m n + +/-- The state with the idle thread current and no switch pending. -/ +def hatA (σ : State Unit) : State Unit := + { σ with curThread := σ.idleThread, schedulerAction := .chooseNewThread } + +theorem SPairA.hat {σ : State Unit} {m : McState} {n : Nat} (h : SPairA σ m n) : LoopInvA (hatA σ) m n := by + obtain ⟨x, hx, hs⟩ := h.sinv.idleTcb + exact + { sinv := { h.sinv with + curTcb := ⟨x, hx⟩ + ctQ := fun e => by cases e + irqN := SpecIrqA.of_eq h.sinv.irqN rfl rfl } + sidle := { h.sidle with } + sact := + { f1 := fun u e => by cases e + f2 := ⟨x, hx, Or.inr (Or.inr (Or.inr hs))⟩ } } + +theorem SPairA.ofHat {σ : State Unit} {m : McState} {n : Nat} (H : LoopInvA (hatA σ) m n) + (hc : ∃ x, σ.kheap σ.curThread = some (.tcb x)) + (hq : σ.schedulerAction = .resumeCurThread → ∀ d p, σ.curThread ∉ σ.readyQueues d p) : SPairA σ m n := + ⟨{ H.sinv with + curTcb := hc + ctQ := hq + irqN := SpecIrqA.of_eq H.sinv.irqN rfl rfl }, { H.sidle with }⟩ + +/-- What one step of the wake keeps. -/ +structure PStepA (m : McState) (n : Nat) (σ σ' : State Unit) : Prop where + inv : SPairA σ' m n + idle : σ'.idleThread = σ.idleThread + dom : σ'.curDomain = σ.curDomain + tcb : ∀ q x, σ.kheap q = some (.tcb x) → ∃ y, σ'.kheap q = some (.tcb y) ∧ y.tcbState = x.tcbState ∧ + y.tcbDomain = x.tcbDomain + sc : ∀ q x k, σ.kheap q = some (.schedContext x k) → + ∃ y, σ'.kheap q = some (.schedContext y k) ∧ y.scRefills = x.scRefills ∧ y.scRefillMax = x.scRefillMax + csc : σ'.curSc = σ.curSc + +theorem PStepA.refl {m : McState} {n : Nat} {σ : State Unit} (h : SPairA σ m n) : PStepA m n σ σ := + ⟨h, rfl, rfl, fun _ x hx => ⟨x, hx, rfl, rfl⟩, fun _ x _ hx => ⟨x, hx, rfl, rfl⟩, rfl⟩ + +theorem PStepA.trans {m : McState} {n : Nat} {σ σ1 σ2 : State Unit} (h1 : PStepA m n σ σ1) + (h2 : PStepA m n σ1 σ2) : PStepA m n σ σ2 := by + refine ⟨h2.inv, h2.idle.trans h1.idle, h2.dom.trans h1.dom, fun q x hx => ?_, fun q x k hx => ?_, + h2.csc.trans h1.csc⟩ + · obtain ⟨y, hy, a, b⟩ := h1.tcb q x hx + obtain ⟨z, hz, a', b'⟩ := h2.tcb q y hy + exact ⟨z, hz, a'.trans a, b'.trans b⟩ + · obtain ⟨y, hy, a, b⟩ := h1.sc q x k hx + obtain ⟨z, hz, a', b'⟩ := h2.sc q y k hy + exact ⟨z, hz, a'.trans a, b'.trans b⟩ + +/-- A step that keeps the heap, the idle thread, the current domain and SC. -/ +theorem PStepA.of_kheap {m : McState} {n : Nat} {σ σ' : State Unit} (h : SPairA σ' m n) (hk : σ'.kheap = σ.kheap) + (hi : σ'.idleThread = σ.idleThread) (hd : σ'.curDomain = σ.curDomain) (hc : σ'.curSc = σ.curSc) : + PStepA m n σ σ' := + ⟨h, hi, hd, fun _ x hx => ⟨x, by rw [hk]; exact hx, rfl, rfl⟩, fun _ x _ hx => ⟨x, by rw [hk]; exact hx, rfl, rfl⟩, + hc⟩ + +/-- A thread that may be enqueued. -/ +def EnqSA (σ : State Unit) (m : McState) (n : Nat) (y : ObjRef) : Prop := + y ≠ σ.idleThread ∧ (∀ c < n, m.idleThreadOf c ≠ y) ∧ ∃ x, σ.kheap y = some (.tcb x) ∧ runnable x.tcbState = true + +theorem SPairA.enqS {σ : State Unit} {m : McState} {n : Nat} (h : SPairA σ m n) {y : ObjRef} {x : Tcb} + (hx : σ.kheap y = some (.tcb x)) (hr : runnable x.tcbState = true) : EnqSA σ m n y := by + refine ⟨fun e => ?_, fun c hc e => ?_, x, hx, hr⟩ + · obtain ⟨z, hz, hs⟩ := h.sinv.idleTcb + rw [← e, hx] at hz + have := KernelObject.tcb.inj (Option.some.inj hz) + subst this + rw [hs] at hr + cases hr + · obtain ⟨z, hz, hs⟩ := h.sidle.f3 c hc + rw [e, hx] at hz + have := KernelObject.tcb.inj (Option.some.inj hz) + subst this + rw [hs] at hr + cases hr + +/-- A queue / action step that does not leave the action at `resumeCurThread`. -/ +theorem SPairA.queueA {P : ObjRef → Prop} {σ σ' : State Unit} {m : McState} {n : Nat} (h : SPairA σ m n) + (Q : QueueE P σ σ') (hP : ∀ y, P y → EnqSA σ m n y) (ha : σ'.schedulerAction ≠ .resumeCurThread) : + PStepA m n σ σ' := by + have e := Q.1 + have Q' : QueueE P (hatA σ) (hatA σ') := ⟨by rw [e]; rfl, Q.2⟩ + have H := h.hat.queueE Q' (fun y p => ⟨(hP y p).1, (hP y p).2.1, (hP y p).2.2⟩) (fun u eu => by cases eu) + (fun eu => by cases eu) + have hk : σ'.kheap = σ.kheap := by rw [e] + have hcur : σ'.curThread = σ.curThread := by rw [e] + refine PStepA.of_kheap (SPairA.ofHat H (by rw [hk, hcur]; exact h.sinv.curTcb) (fun hr => absurd hr ha)) hk + (by rw [e]) (by rw [e]) (by rw [e]) + +theorem SPairA.reschedule {σ : State Unit} {m : McState} {n : Nat} (h : SPairA σ m n) + {r : Unit × State Unit} (hr : r ∈ (rescheduleRequired (Z := Unit) σ).1) : + PStepA m n σ r.2 ∧ r.2.schedulerAction = .chooseNewThread := by + obtain ⟨q, e⟩ := rescheduleRequired_memA hr + refine ⟨h.queueA q (fun y p => ?_) (by rw [e]; intro c; cases c), e⟩ + have hs := p.2 + obtain ⟨x, hx⟩ := schedulable_tcbA hs + exact h.enqS hx ((Sched.schedulable_partsA hs).1 x hx) + +/-- **`possible_switch_to`** of a runnable thread of the current domain. -/ +theorem SPairA.possibleSwitch {σ : State Unit} {m : McState} {n : Nat} (h : SPairA σ m n) {t : ObjRef} {x : Tcb} + (hx : σ.kheap t = some (.tcb x)) (hrun : runnable x.tcbState = true) (hdom : x.tcbDomain = σ.curDomain) + {r : Unit × State Unit} (hr : r ∈ (possibleSwitchTo (Z := Unit) t σ).1) : PStepA m n σ r.2 := by + unfold possibleSwitchTo getTcbObjRef at hr + obtain ⟨scOpt, -, hr⟩ := eqPres_mem (threadGet_eqPres _ t) hr + obtain ⟨inq, -, hr⟩ := eqPres_mem (PresR.gets _) hr + unfold NonDet.when at hr + split_ifs at hr with hg + swap + · have : r = ((), σ) := hr + rw [this]; exact PStepA.refl h + obtain ⟨cd, hcd, hr⟩ := eqPres_mem (PresR.gets _) hr + obtain ⟨td, htd, hr⟩ := eqPres_mem (threadGet_eqPres _ t) hr + obtain ⟨act, hact, hr⟩ := eqPres_mem (PresR.gets _) hr + have ecd : cd = σ.curDomain := congrArg Prod.fst (show (cd, σ) = (σ.curDomain, σ) from hcd) + have etd : td = x.tcbDomain := by + rw [threadGet_eq _ hx] at htd + exact congrArg Prod.fst (show (td, σ) = (x.tcbDomain, σ) from htd) + have eact : act = σ.schedulerAction := congrArg Prod.fst (show (act, σ) = (σ.schedulerAction, σ) from hact) + have same : ¬ td ≠ cd := fun c => c (by rw [etd, ecd]; exact hdom) + rw [if_neg same] at hr + have ht := h.enqS hx hrun + by_cases h2 : act ≠ .resumeCurThread + · rw [if_pos h2] at hr + obtain ⟨u, hu, hr⟩ := mem_bindA.1 hr + obtain ⟨A, e1⟩ := h.reschedule hu + obtain ⟨q2, e2⟩ := tcbSchedAction_enq_memA t hr + have ht' : EnqSA u.2 m n t := by + obtain ⟨y, hy, es, -⟩ := A.tcb t x hx + exact A.inv.enqS hy (by rw [es]; exact hrun) + exact A.trans (A.inv.queueA q2 (fun y p => by rw [p]; exact ht') (by rw [e2, e1]; intro c; cases c)) + · rw [if_neg h2] at hr + obtain ⟨q, e⟩ := setSchedulerAction_memA _ hr + exact h.queueA q (fun _ p => p.elim) (by rw [e]; intro c; cases c) + +theorem SPairA.refK {σ σ' : State Unit} {m : McState} {n : Nat} (h : SPairA σ m n) (R : RefK σ σ') : + SPairA σ' m n := by + have e := R.1 + have R' : RefK (hatA σ) (hatA σ') := ⟨by rw [e]; rfl, R.2.1, R.2.2⟩ + refine SPairA.ofHat (h.hat.refK R') ?_ (fun hr d p => ?_) + · obtain ⟨x, hx⟩ := h.sinv.curTcb + obtain ⟨y, hy, -⟩ := (R.2.1 _).tcbF hx + exact ⟨y, by rw [show σ'.curThread = σ.curThread by rw [e]]; exact hy⟩ + · rw [show σ'.curThread = σ.curThread by rw [e], show σ'.readyQueues = σ.readyQueues by rw [e]] + exact h.sinv.ctQ (by rw [e] at hr; exact hr) d p + +/-- One TCB's SC binding, off the idle thread. -/ +theorem SPairA.tcbSc {σ : State Unit} {m : McState} {n : Nat} (h : SPairA σ m n) {t : ObjRef} {x : Tcb} + (hk : σ.kheap t = some (.tcb x)) (ht : t ≠ σ.idleThread) (v : Option ObjRef) + (hz : v = some σ.curSc → t ∈ σ.releaseQueue → σ.consumedTime = 0) : + PStepA m n σ { σ with kheap := Function.update σ.kheap t (some (.tcb { x with tcbSchedContext := v })) } := by + have R := RStepA.tcbSc (m := m) (n := n) (σ := hatA σ) h.hat (x := x) hk ht v hz + refine ⟨SPairA.ofHat R.inv ?_ h.sinv.ctQ, rfl, rfl, fun q y hy => ?_, fun q y k hy => ?_, rfl⟩ + · show ∃ z, Function.update σ.kheap t _ σ.curThread = some (.tcb z) + by_cases e : σ.curThread = t + · rw [e, Function.update_self]; exact ⟨_, rfl⟩ + · rw [Function.update_of_ne e]; exact h.sinv.curTcb + · show ∃ z, Function.update σ.kheap t _ q = some (.tcb z) ∧ _ + by_cases e : q = t + · subst e + rw [hk] at hy + cases hy + exact ⟨_, Function.update_self _ _ _, rfl, rfl⟩ + · exact ⟨y, by rw [Function.update_of_ne e]; exact hy, rfl, rfl⟩ + · have e : q ≠ t := fun e => by rw [e, hk] at hy; cases hy + exact ⟨y, by show Function.update σ.kheap t _ q = _; rw [Function.update_of_ne e]; exact hy, rfl, rfl⟩ + +/-- One SC off `idle_sc_ptr`: the binding (not to the idle thread). -/ +theorem SPairA.updSc {σ : State Unit} {m : McState} {n : Nat} (h : SPairA σ m n) {p : ObjRef} + {x y : SchedContext} {k : Nat} (hk : σ.kheap p = some (.schedContext x k)) (hp : p ≠ idleScPtr) + (hr : y.scRefillMax = x.scRefillMax) (hf : y.scRefills = x.scRefills) (hy : y.scYieldFrom = x.scYieldFrom) + (hn : y.scTcb = x.scTcb ∨ y.scTcb ≠ some σ.idleThread) : + PStepA m n σ { σ with kheap := Function.update σ.kheap p (some (.schedContext y k)) } := by + have R := RStepA.updSc (m := m) (n := n) (σ := hatA σ) h.hat (x := x) (y := y) (k := k) hk hp hr hf hy hn + refine ⟨SPairA.ofHat R.inv ?_ h.sinv.ctQ, rfl, rfl, fun q z hz => ?_, fun q z j hz => ?_, rfl⟩ + · obtain ⟨c, hc⟩ := h.sinv.curTcb + have e : σ.curThread ≠ p := fun e => by rw [e, hk] at hc; cases hc + exact ⟨c, by show Function.update σ.kheap p _ σ.curThread = _; rw [Function.update_of_ne e]; exact hc⟩ + · have e : q ≠ p := fun e => by rw [e, hk] at hz; cases hz + exact ⟨z, by show Function.update σ.kheap p _ q = _; rw [Function.update_of_ne e]; exact hz, rfl, rfl⟩ + · show ∃ w, Function.update σ.kheap p _ q = some (.schedContext w j) ∧ _ + by_cases e : q = p + · subst e + rw [hk] at hz + cases hz + exact ⟨_, Function.update_self _ _ _, hf, hr⟩ + · exact ⟨z, by rw [Function.update_of_ne e]; exact hz, rfl, rfl⟩ + +theorem SPairA.deq {σ : State Unit} {m : McState} {n : Nat} (h : SPairA σ m n) (t : ObjRef) + {r : Unit × State Unit} (hr : r ∈ (tcbSchedAction (Z := Unit) tcbSchedDequeue t σ).1) : PStepA m n σ r.2 := by + unfold tcbSchedAction at hr + obtain ⟨d, -, hr⟩ := eqPres_mem (threadGet_eqPres _ t) hr + obtain ⟨pr, -, hr⟩ := eqPres_mem (threadGet_eqPres _ t) hr + have hq : PresR (@Eq (State Unit)) (getTcbQueue (Z := Unit) d pr) := by unfold getTcbQueue; eqpres + obtain ⟨q, hq', hr⟩ := eqPres_mem hq hr + have eq : q = σ.readyQueues d pr := by + unfold getTcbQueue at hq' + obtain ⟨x, hx, hq'⟩ := mem_bindA.1 hq' + have ex : x = (σ.readyQueues, σ) := hx + subst ex + exact congrArg Prod.fst (show (q, σ) = (σ.readyQueues d pr, σ) from hq') + obtain ⟨q', hq'', hr⟩ := mem_bindA.1 hr + have eq' : q' = (tcbSchedDequeue t q, σ) := hq'' + subst eq' + have er : r = ((), { σ with readyQueues := fun d' p => + if d' = d ∧ p = pr then tcbSchedDequeue t q else σ.readyQueues d' p }) := hr + rw [er] + have sub : ∀ d' p y, y ∈ (if d' = d ∧ p = pr then tcbSchedDequeue t q else σ.readyQueues d' p) → + y ∈ σ.readyQueues d' p := fun d' p y hy => by + split_ifs at hy with hh + · unfold tcbSchedDequeue at hy + rw [hh.1, hh.2, ← eq] + exact (List.mem_filter.1 hy).1 + · exact hy + have H := h.hat.heapD (σ' := hatA { σ with readyQueues := fun d' p => + if d' = d ∧ p = pr then tcbSchedDequeue t q else σ.readyQueues d' p }) rfl sub (fun _ a => a) + (NewScTcbA.of_kheap rfl) (fun _ => .same _) + exact PStepA.of_kheap (SPairA.ofHat H h.sinv.curTcb (fun hs d' p c => h.sinv.ctQ hs d' p (sub _ _ _ c))) + rfl rfl rfl rfl + +theorem SPairA.releaseEnqueue {m : McState} {n : Nat} (t : ObjRef) {σ : State Unit} (h : SPairA σ m n) + (ht : ∃ x, σ.kheap t = some (.tcb x) ∧ runnable x.tcbState = true ∧ + (x.tcbSchedContext = some σ.curSc → σ.consumedTime = 0)) {r : Unit × State Unit} + (hr : r ∈ (tcbReleaseEnqueue (Z := Unit) t σ).1) : PStepA m n σ r.2 := by + unfold tcbReleaseEnqueue at hr + obtain ⟨qs, hqs, hr⟩ := eqPres_mem (PresR.gets _) hr + have eqs : qs = σ.releaseQueue := congrArg Prod.fst (show (qs, σ) = (σ.releaseQueue, σ) from hqs) + obtain ⟨u1, h1, hr⟩ := mem_bindA.1 hr + unfold readyTimesAppend at h1 + obtain ⟨hσ1, hmem⟩ := orderedInsert_spec _ _ _ _ h1 + obtain ⟨u2, h2, hr⟩ := mem_bindA.1 hr + have e2 : u2 = ((), { u1.2 with releaseQueue := u1.1 }) := h2 + have e3 : ∃ b, r.2 = { σ with releaseQueue := u1.1, reprogramTimer := b } := by + unfold NonDet.when at hr + split_ifs at hr + · have : r = ((), { u2.2 with reprogramTimer := true }) := hr + refine ⟨true, ?_⟩ + rw [this, e2] + show ({ u1.2 with releaseQueue := u1.1, reprogramTimer := true } : State Unit) = _ + rw [hσ1] + · have : r = u2 := hr + refine ⟨σ.reprogramTimer, ?_⟩ + rw [this, e2] + show ({ u1.2 with releaseQueue := u1.1 } : State Unit) = _ + rw [hσ1] + obtain ⟨b, e3⟩ := e3 + have hq : ∀ y ∈ u1.1, y ∈ σ.releaseQueue ∨ ∃ x, σ.kheap y = some (.tcb x) ∧ runnable x.tcbState = true ∧ + (x.tcbSchedContext = some σ.curSc → σ.consumedTime = 0) := by + intro y hy + rcases hmem y hy with e | e + · subst e; exact Or.inr ht + · rw [eqs] at e + exact Or.inl e + rw [e3] + have H := h.hat.relGrow (q := u1.1) (b := b) hq + exact PStepA.of_kheap (SPairA.ofHat (σ := { σ with releaseQueue := u1.1, reprogramTimer := b }) H h.sinv.curTcb + h.sinv.ctQ) rfl rfl rfl rfl + +theorem SPairA.postpone {m : McState} {n : Nat} (p : ObjRef) {σ : State Unit} (h : SPairA σ m n) + (hS : ∀ y k t', σ.kheap p = some (.schedContext y k) → y.scTcb = some t' → ∃ x, σ.kheap t' = some (.tcb x) ∧ + runnable x.tcbState = true ∧ (x.tcbSchedContext = some σ.curSc → σ.consumedTime = 0)) + {r : Unit × State Unit} (hr : r ∈ (postpone (Z := Unit) p σ).1) : PStepA m n σ r.2 := by + unfold Fl4SpecA64.postpone at hr + have hgp : PresR (@Eq (State Unit)) (getScObjRef (Z := Unit) SchedContext.scTcb p) := by + unfold getScObjRef + refine PresR.bind (getSchedContext_eqPres p) (fun _ => ?_) + eqpres + obtain ⟨tcbOpt, hg, hr⟩ := eqPres_mem hgp hr + obtain ⟨tp, htp, hr⟩ := eqPres_mem (PresR.assertOpt tcbOpt) hr + have etp : tcbOpt = some tp := by + have : (tp, σ) ∈ (NonDet.assertOpt tcbOpt σ).1 := htp + cases tcbOpt with + | none => exact absurd this (Set.notMem_empty _) + | some a => exact congrArg some (congrArg Prod.fst (show (tp, σ) = (a, σ) from this)).symm + have hex : ∃ x, σ.kheap tp = some (.tcb x) ∧ runnable x.tcbState = true ∧ + (x.tcbSchedContext = some σ.curSc → σ.consumedTime = 0) := by + unfold getScObjRef at hg + obtain ⟨sc, hsc, hg'⟩ := eqPres_mem (getSchedContext_eqPres p) hg + obtain ⟨-, k, hk⟩ := getSchedContext_mem hsc + have e : tcbOpt = sc.scTcb := congrArg Prod.fst (show (tcbOpt, σ) = (sc.scTcb, σ) from hg') + exact hS sc k tp hk (e.symm.trans etp) + obtain ⟨a, ha, hr⟩ := mem_bindA.1 hr + have A := h.deq tp ha + obtain ⟨b, hb, hr⟩ := mem_bindA.1 hr + obtain ⟨x, hx, hrun, hz⟩ := hex + have rA := tcbSchedDeq_rlA tp ha + have B := A.inv.releaseEnqueue tp ⟨x, by rw [rA.2.2.2]; exact hx, hrun, by rw [rA.2.2.1, rA.2.1]; exact hz⟩ hb + have C : PStepA m n b.2 r.2 := by + have er : r = ((), { b.2 with reprogramTimer := true }) := hr + rw [er] + exact PStepA.of_kheap (B.inv.refK ⟨rfl, fun _ => .same _, id⟩) rfl rfl rfl rfl + exact A.trans (B.trans C) + +/-- **`sched_context_resume`** (`schedContextResume_stepZA` without `SActA`). -/ +theorem SPairA.resume {m : McState} {n : Nat} (p : ObjRef) {σ : State Unit} (h : SPairA σ m n) + (hsuf : σ.consumedTime ≠ 0 → p = σ.curSc → ∀ y k, σ.kheap σ.curSc = some (.schedContext y k) → + scActive y = true → refillSufficient 0 (refillHd y) = true) + (hsym : ∀ y k t', σ.kheap p = some (.schedContext y k) → y.scTcb = some t' → + ∀ x, σ.kheap t' = some (.tcb x) → x.tcbSchedContext = some p) + {r : Unit × State Unit} (hr : r ∈ (schedContextResume (Z := Unit) p σ).1) : PStepA m n σ r.2 := by + unfold schedContextResume at hr + obtain ⟨sc, hsc, hr⟩ := eqPres_mem (getSchedContext_eqPres p) hr + obtain ⟨-, k, hk⟩ := getSchedContext_mem hsc + obtain ⟨tp, htp, hr⟩ := eqPres_mem (PresR.assertOpt sc.scTcb) hr + have etp : sc.scTcb = some tp := by + have : (tp, σ) ∈ (NonDet.assertOpt sc.scTcb σ).1 := htp + cases e : sc.scTcb with + | none => rw [e] at this; exact absurd this (Set.notMem_empty _) + | some a => + rw [e] at this + exact congrArg some (congrArg Prod.fst (show (tp, σ) = (a, σ) from this)).symm + obtain ⟨sched, hs, hr⟩ := eqPres_mem (PresR.gets _) hr + have es : sched = schedulable tp σ := congrArg Prod.fst (show (sched, σ) = (schedulable tp σ, σ) from hs) + have refl : r = ((), σ) → PStepA m n σ r.2 := fun e => by rw [e]; exact PStepA.refl h + unfold NonDet.when at hr + split_ifs at hr with hsch + · obtain ⟨ts, -, hr⟩ := eqPres_mem (threadGet_eqPres _ tp) hr + obtain ⟨ready, hrd, hr⟩ := eqPres_mem (getScRefillReady_eqPres p) hr + obtain ⟨suff, hsf, hr⟩ := eqPres_mem (getScRefillSufficient_eqPres p 0) hr + split_ifs at hr with hc + · obtain ⟨d, -, hr⟩ := eqPres_mem (threadGet_eqPres _ tp) hr + obtain ⟨pr, -, hr⟩ := eqPres_mem (threadGet_eqPres _ tp) hr + obtain ⟨q, -, hr⟩ := eqPres_mem (by unfold getTcbQueue; eqpres : PresR (@Eq (State Unit)) + (getTcbQueue (Z := Unit) d pr)) hr + obtain ⟨-, -, hr⟩ := eqPres_mem (PresR.assert _) hr + refine h.postpone p (fun y k' t' hy ht' => ?_) hr + rw [hk] at hy + cases hy + rw [etp] at ht' + cases ht' + have hsch' : schedulable tp σ = true := by rw [← es]; exact hsch + obtain ⟨x, hx⟩ := schedulable_tcbA hsch' + refine ⟨x, hx, (Sched.schedulable_partsA hsch').1 x hx, fun hs => ?_⟩ + obtain ⟨-, hact, hnot⟩ := hc + have hp : p = σ.curSc := by + have := hsym sc k tp hk etp x hx + rw [this] at hs + exact Option.some.inj hs + by_contra hne + have hne0 : sc.scRefills ≠ [] := h.sinv.rpos p sc k hk hact + have hrdy := h.sinv.curScReady hne sc k (by rw [← hp]; exact hk) hact + have hsu := hsuf hne hp sc k (by rw [← hp]; exact hk) hact + rw [getScRefillReady_eqA hk hne0] at hrd + rw [getScRefillSufficient_eqA hk hne0] at hsf + have e1 : ready = refillReady σ.curTime (refillHd sc) := congrArg Prod.fst (Set.mem_singleton_iff.1 hrd) + have e2 : suff = refillSufficient 0 (refillHd sc) := congrArg Prod.fst (Set.mem_singleton_iff.1 hsf) + exact hnot ⟨by rw [e1]; exact hrdy, by rw [e2]; exact hsu⟩ + · exact refl hr + · exact refl hr + +/-- **`maybe_donate_sc`** of a thread that is not idle, from a notification whose SC is not `idle_sc_ptr`. The current +SC's sufficiency is asked only when it is the notification's unbound SC. -/ +theorem SPairA.maybeDonate {m : McState} {n : Nat} (t np : ObjRef) {σ : State Unit} (h : SPairA σ m n) + (ht : t ≠ σ.idleThread) {y : Notification} (hkn : σ.kheap np = some (.notification y)) + (hsc : ∀ sc, y.ntfnSc = some sc → sc ≠ idleScPtr) (hnr : t ∉ σ.releaseQueue) + (hsuf : y.ntfnSc = some σ.curSc → ∀ z k, σ.kheap σ.curSc = some (.schedContext z k) → z.scTcb = none → + scActive z = true → refillSufficient 0 (refillHd z) = true) + {r : Unit × State Unit} (hr : r ∈ (maybeDonateSc (Z := Unit) t np σ).1) : PStepA m n σ r.2 := by + unfold maybeDonateSc at hr + obtain ⟨scOpt, -, hr⟩ := eqPres_mem (threadGet_eqPres _ t) hr + have refl : r = ((), σ) → PStepA m n σ r.2 := fun e => by rw [e]; exact PStepA.refl h + unfold NonDet.when at hr + split_ifs at hr with h0 + swap + · exact refl hr + rw [nd_bind_one' (getNtfnObjRef_eqA hkn _)] at hr + rcases e : y.ntfnSc with _ | p + · rw [e] at hr + exact refl hr + rw [e] at hr + have hp := hsc p e + simp only [NonDet.maybeM] at hr + have hgs : PresR (@Eq (State Unit)) (getScObjRef (Z := Unit) SchedContext.scTcb p) := by + unfold getScObjRef + refine PresR.bind (getSchedContext_eqPres p) (fun _ => ?_) + eqpres + obtain ⟨fo, hfo, hr⟩ := eqPres_mem hgs hr + split_ifs at hr with h1 + swap + · exact refl hr + have val : ∀ fo', (fo', σ) ∈ (getScObjRef (Z := Unit) SchedContext.scTcb p σ).1 → + ∃ z k, σ.kheap p = some (.schedContext z k) ∧ fo' = z.scTcb := fun fo' hf => by + unfold getScObjRef at hf + obtain ⟨z, hz, hf⟩ := eqPres_mem (getSchedContext_eqPres p) hf + obtain ⟨-, k, hk⟩ := getSchedContext_mem hz + exact ⟨z, k, hk, congrArg Prod.fst (show (fo', σ) = (z.scTcb, σ) from hf)⟩ + obtain ⟨z, k, hz, ez⟩ := val fo hfo + have hnone : z.scTcb = none := by rw [← ez]; exact h1 + obtain ⟨u, hu, hr⟩ := mem_bindA.1 hr + -- the donation of an unbound SC: the two bindings + unfold schedContextDonate at hu + obtain ⟨fo2, hfo2, hu⟩ := eqPres_mem hgs hu + obtain ⟨z2, k2, hz2, ez2⟩ := val fo2 hfo2 + have efo2 : fo2 = none := by + rw [hz] at hz2 + cases hz2 + rw [ez2]; exact hnone + obtain ⟨u0, hu0, hu⟩ := mem_bindA.1 hu + have e0 : u0 = ((), σ) := by + unfold NonDet.when at hu0 + rw [if_neg (fun c => c efo2)] at hu0 + exact hu0 + subst e0 + obtain ⟨v, hv, hu⟩ := mem_bindA.1 hu + unfold setScObjRef at hv + obtain ⟨x2, j2, hk2, ev⟩ := updateSchedContext_memA _ _ hv + have S2 : PStepA m n σ v.2 := by + rw [ev] + exact h.updSc hk2 hp rfl rfl rfl (Or.inr fun c => ht (Option.some.inj c)) + have rel : v.2.releaseQueue = σ.releaseQueue := by rw [ev] + unfold setTcbObjRef at hu + obtain ⟨x, hx, eu⟩ := threadSet_memA _ _ hu + have S3 : PStepA m n v.2 u.2 := by + rw [eu] + exact S2.inv.tcbSc hx (by rw [S2.idle]; exact ht) (some p) (fun _ hq => absurd (rel ▸ hq) hnr) + have D := S2.trans S3 + have hpt : p ≠ t := fun c => by + have h1 : v.2.kheap p = some (.schedContext { x2 with scTcb := some t } j2) := by + rw [ev]; exact Function.update_self .. + rw [c, hx] at h1 + cases h1 + have dt : ∃ xd, u.2.kheap t = some (.tcb xd) ∧ xd.tcbSchedContext = some p := + ⟨{ x with tcbSchedContext := some p }, by rw [eu]; exact Function.update_self .., rfl⟩ + have dsc : ∀ y' k', u.2.kheap p = some (.schedContext y' k') → y'.scTcb = some t := fun y' k' hy => by + rw [eu] at hy + change Function.update v.2.kheap t _ p = _ at hy + rw [Function.update_of_ne hpt, ev] at hy + change Function.update σ.kheap p _ p = _ at hy + rw [Function.update_self] at hy + have := Option.some.inj hy + injection this with a b + rw [← a] + rfl + refine D.trans (D.inv.resume p (fun _ hpc y' k' hy' a => ?_) (fun y' k' t' hy' hs' x' hx' => ?_) hr) + · obtain ⟨c0, k0, hc0⟩ := h.sinv.curSc + obtain ⟨y0, hy0, er, -⟩ := D.sc σ.curSc c0 k0 hc0 + rw [D.csc, hy0] at hy' + have e12 := Option.some.inj hy' + injection e12 with a1 a2 + subst a1 + have hpc' : p = σ.curSc := hpc.trans D.csc + have c0z : c0 = z := by + rw [← hpc', hz] at hc0 + injection (Option.some.inj hc0) with a _ + exact a.symm + have mx : y0.scRefillMax = c0.scRefillMax := by + obtain ⟨y1, hy1, -, mx⟩ := D.sc σ.curSc c0 k0 hc0 + rw [hy0] at hy1 + injection (Option.some.inj hy1) with a _ + rw [a]; exact mx + have a0 : scActive c0 = true := by unfold scActive at a ⊢; rw [← mx]; exact a + have := hsuf (by rw [e, hpc']) c0 k0 hc0 (by rw [c0z]; exact hnone) a0 + unfold refillHd at this ⊢ + rw [er]; exact this + · obtain ⟨xd, hxd, hxs⟩ := dt + rw [dsc y' k' hy'] at hs' + have e := Option.some.inj hs' + subst e + rw [hxd] at hx' + have := KernelObject.tcb.inj (Option.some.inj hx') + subst this + exact hxs + +set_option maxHeartbeats 2000000 in +/-- **The two invariants across the spec's wake of a thread that is not runnable** (module docstring). -/ +theorem SPairA.wake {σ1 : State Unit} {m : McState} {n : Nat} (h : SPairA σ1 m n) {np d : ObjRef} + {y : Notification} {xd : Tcb} (hkn : σ1.kheap np = some (.notification y)) + (hsc : ∀ sc, y.ntfnSc = some sc → sc ≠ idleScPtr) (hkd : σ1.kheap d = some (.tcb xd)) + (hs : runnable xd.tcbState = false) (hfn : xd.tcbFault = none) (tidle : d ≠ σ1.idleThread) + (tidles : ∀ c < n, m.idleThreadOf c ≠ d) (hdom : xd.tcbDomain = σ1.curDomain) (badge : Word) + (hsuf : y.ntfnSc = some σ1.curSc → ∀ z k, σ1.kheap σ1.curSc = some (.schedContext z k) → z.scTcb = none → + scActive z = true → refillSufficient 0 (refillHd z) = true) + {r : Unit × State Unit} (hr : r ∈ (specWake d np badge σ1).1) : SPairA r.2 m n := by + rw [specWake_eq hkd] at hr + have h2 : SPairA (stUpdA σ1 d xd .running) m n := + ⟨h.sinv.restartA hkd tidle rfl (fun e => by cases e) hfn, h.sidle.stUpdA hkd tidles⟩ + have hx2 : (stUpdA σ1 d xd .running).kheap d = some (.tcb { xd with tcbState := .running }) := + Function.update_self _ _ _ + have eact : ∃ σ2, stsAct (updK σ1 d (.tcb { xd with tcbState := .running })) d = σ2 ∧ SPairA σ2 m n ∧ + σ2.kheap = (stUpdA σ1 d xd .running).kheap ∧ σ2.idleThread = σ1.idleThread ∧ σ2.curDomain = σ1.curDomain ∧ + σ2.curSc = σ1.curSc ∧ σ2.releaseQueue = σ1.releaseQueue := by + refine ⟨_, rfl, ?_⟩ + unfold stsAct + split_ifs + · refine ⟨?_, rfl, rfl, rfl, rfl, rfl⟩ + exact SPairA.ofHat (σ := { stUpdA σ1 d xd .running with schedulerAction := .chooseNewThread }) h2.hat + h2.sinv.curTcb (fun e => by cases e) + · exact ⟨h2, rfl, rfl, rfl, rfl, rfl⟩ + obtain ⟨σ2, e2, I2, k2, i2, d2, c2, r2⟩ := eact + rw [e2] at hr + have hx2' : σ2.kheap d = some (.tcb { xd with tcbState := .running }) := by rw [k2]; exact hx2 + have R3 : RefK σ2 (updK σ2 d (.tcb (setReg { xd with tcbState := .running } badge_register badge))) := by + refine ⟨rfl, fun q => ?_, fun g => g.updNs d _ fun _ _ e => by cases e⟩ + show RefObjA (σ2.kheap q) (Function.update σ2.kheap d _ q) + by_cases e : q = d + · subst e + rw [Function.update_self, hx2'] + exact .tcb _ _ ⟨rfl, rfl, rfl, rfl, Or.inl rfl⟩ + · rw [Function.update_of_ne e]; exact .same _ + have h3 := I2.refK R3 + set σ3 := updK σ2 d (.tcb (setReg { xd with tcbState := .running } badge_register badge)) with hσ3 + have nd : np ≠ d := fun e => by rw [e, hkd] at hkn; cases hkn + have k3 : ∀ q, q ≠ d → σ3.kheap q = σ1.kheap q := fun q e => by + show Function.update σ2.kheap d _ q = _ + rw [Function.update_of_ne e, k2] + exact Function.update_of_ne e _ _ + have hk3 : σ3.kheap np = some (.notification y) := by rw [k3 _ nd]; exact hkn + have hkd3 : σ3.kheap d = some (.tcb (setReg { xd with tcbState := .running } badge_register badge)) := + Function.update_self _ _ _ + have idle3 : σ3.idleThread = σ1.idleThread := i2 + have dom3 : σ3.curDomain = σ1.curDomain := d2 + have csc3 : σ3.curSc = σ1.curSc := c2 + unfold specWakeTail at hr + obtain ⟨u1, hu1, hr⟩ := mem_bindA.1 hr + have hnr3 : d ∉ σ3.releaseQueue := fun hq => by + obtain ⟨z, hz, hr0⟩ := h.sinv.rlsRun d (r2 ▸ hq) + rw [hkd] at hz + have := KernelObject.tcb.inj (Option.some.inj hz) + subst this + rw [hs] at hr0 + cases hr0 + have hsuf3 : y.ntfnSc = some σ3.curSc → ∀ z k, σ3.kheap σ3.curSc = some (.schedContext z k) → z.scTcb = none → + scActive z = true → refillSufficient 0 (refillHd z) = true := fun e z k hz hn a => by + obtain ⟨x0, k0, hx0⟩ := h.sinv.curSc + have ne : σ1.curSc ≠ d := fun e => by rw [e, hkd] at hx0; cases hx0 + rw [csc3] at e hz + rw [k3 _ ne] at hz + exact hsuf e z k hz hn a + have D := h3.maybeDonate d np (by rw [idle3]; exact tidle) hk3 hsc hnr3 hsuf3 hu1 + obtain ⟨sched, hsd, hr⟩ := eqPres_mem (PresR.gets _) hr + obtain ⟨u2, hu2, hr⟩ := mem_bindA.1 hr + have K2 : SPairA u2.2 m n := by + unfold NonDet.when at hu2 + split_ifs at hu2 with hsch + · obtain ⟨x3, hx3, e3, ed3⟩ := D.tcb d _ hkd3 + exact (D.inv.possibleSwitch hx3 (by rw [e3]; rfl) (by rw [ed3, D.dom, dom3]; exact hdom) hu2).inv + · have : u2 = ((), u1.2) := hu2 + rw [this]; exact D.inv + obtain ⟨scopt, -, hr⟩ := eqPres_mem (threadGet_eqPres _ d) hr + exact K2.refK (ifCondRefillUnblockCheck_refK _ _ _ _ r hr) + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3WkKeepA.lean b/proofs/Fl4ProofsA64/P3/Stage3WkKeepA.lean new file mode 100644 index 000000000..62899eb8c --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3WkKeepA.lean @@ -0,0 +1,325 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 2a): **the spec side of the wake tail** (`maybe_donate_sc`, `possible_switch_to`, the +sporadic check) for a Notification send waking a waiter. + +`WkKeepA σ σ'` is `NtfnSpecKeepA σ σ'` (SC `scReplies` / `scYieldFrom`, TCB `mcp` / `yield-to` kept) together with +`TsOnlyA σ σ'` (every non-TCB, non-SC object kept; a TCB changes at most its SC pointer; an SC keeps its refill +maximum; user memory kept). The calculus is a copy of `P3/SigKeepA`'s `NKInst`, over `WkKeepA`; the new leaves are the +SC-pointer writers. `specWakeTail_wkA`: the tail keeps `WkKeepA`. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.SigKeepA +import Fl4ProofsA64.P3.SpecFrameIpcA +import Fl4ProofsA64.P3.SchedOpsA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open Fl4SpecA64 Fl4Spec + +/-- Non-TCB, non-SC objects kept; a TCB changes at most its SC pointer; an SC keeps its refill maximum; user memory +kept. -/ +def TsOnlyA (σ σ' : State Unit) : Prop := + (∀ p o, σ.kheap p = some o → (∀ sc n, o ≠ .schedContext sc n) → (∀ x, o ≠ .tcb x) → σ'.kheap p = some o) ∧ + (∀ p o, σ'.kheap p = some o → (∀ sc n, o ≠ .schedContext sc n) → (∀ x, o ≠ .tcb x) → σ.kheap p = some o) ∧ + (∀ p x, σ.kheap p = some (.tcb x) → ∃ y, σ'.kheap p = some (.tcb y) ∧ + y = { x with tcbSchedContext := y.tcbSchedContext }) ∧ + (∀ p y, σ'.kheap p = some (.tcb y) → ∃ x, σ.kheap p = some (.tcb x)) ∧ + (∀ p y k, σ'.kheap p = some (.schedContext y k) → ∃ x, σ.kheap p = some (.schedContext x k) ∧ + y.scRefillMax = x.scRefillMax) ∧ + σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory + +theorem TsOnlyA.refl (σ : State Unit) : TsOnlyA σ σ := + ⟨fun _ _ h _ _ => h, fun _ _ h _ _ => h, fun _ x h => ⟨x, h, rfl⟩, fun _ y h => ⟨y, h⟩, + fun _ y k h => ⟨y, h, rfl⟩, rfl⟩ + +theorem TsOnlyA.trans {σ1 σ2 σ3 : State Unit} (h1 : TsOnlyA σ1 σ2) (h2 : TsOnlyA σ2 σ3) : TsOnlyA σ1 σ3 := by + refine ⟨fun p o h a b => h2.1 p o (h1.1 p o h a b) a b, fun p o h a b => h1.2.1 p o (h2.2.1 p o h a b) a b, + fun p x h => ?_, fun p y h => ?_, fun p y k h => ?_, h2.2.2.2.2.2.trans h1.2.2.2.2.2⟩ + · obtain ⟨y, hy, ey⟩ := h1.2.2.1 p x h + obtain ⟨z, hz, ez⟩ := h2.2.2.1 p y hy + refine ⟨z, hz, ?_⟩ + rw [ey] at ez + exact ez + · obtain ⟨x, hx⟩ := h2.2.2.2.1 p y h + exact h1.2.2.2.1 p x hx + · obtain ⟨x, hx, ex⟩ := h2.2.2.2.2.1 p y k h + obtain ⟨w, hw, ew⟩ := h1.2.2.2.2.1 p x k hx + exact ⟨w, hw, ex.trans ew⟩ + +/-- **The wake tail's spec relation** (module docstring). -/ +def WkKeepA (σ σ' : State Unit) : Prop := NtfnSpecKeepA σ σ' ∧ TsOnlyA σ σ' + +namespace Sched +open Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Endp + +instance : PreO TsOnlyA := ⟨fun σ => TsOnlyA.refl σ, fun h1 h2 => TsOnlyA.trans h1 h2⟩ +instance : PreO WkKeepA := + ⟨fun σ => ⟨NtfnSpecKeepA.refl σ, TsOnlyA.refl σ⟩, fun h1 h2 => ⟨NtfnSpecKeepA.trans h1.1 h2.1, TsOnlyA.trans h1.2 h2.2⟩⟩ + +noncomputable section + +abbrev WkPresA {α : Type} (m : SMonad Unit α) : Prop := PresR WkKeepA m + +class WkInst {α : Type} (m : SMonad Unit α) : Prop where + pres : WkPresA m + +instance {α β : Type} {m : SMonad Unit α} {f : α → SMonad Unit β} [hm : WkInst m] + [hf : ∀ a, WkInst (f a)] : WkInst (m >>= f) := + ⟨PresR.bind hm.pres fun a => (hf a).pres⟩ + +instance {α : Type} (a : α) : WkInst (pure a : SMonad Unit α) := ⟨PresR.pure a⟩ +instance {α : Type} (f : State Unit → α) : WkInst (NonDet.gets f : SMonad Unit α) := ⟨PresR.gets f⟩ +instance : WkInst (NonDet.get : SMonad Unit (State Unit)) := ⟨PresR.get⟩ +instance {α : Type} (f : State Unit → Option α) : WkInst (NonDet.getsThe f : SMonad Unit α) := + ⟨PresR.getsThe f⟩ +instance {α : Type} (o : Option α) : WkInst (NonDet.assertOpt o : SMonad Unit α) := ⟨PresR.assertOpt o⟩ +instance (P : Prop) : WkInst (NonDet.assert P : SMonad Unit Unit) := ⟨PresR.assert P⟩ +instance {α : Type} : WkInst (NonDet.fail : SMonad Unit α) := ⟨PresR.fail⟩ + +instance {P : Prop} {d : Decidable P} {m : SMonad Unit Unit} [h : WkInst m] : + WkInst (@NonDet.when (State Unit) P d m) := ⟨PresR.when h.pres⟩ +instance {P : Prop} {d : Decidable P} {m : SMonad Unit Unit} [h : WkInst m] : + WkInst (@NonDet.unless (State Unit) P d m) := ⟨PresR.unless' h.pres⟩ + +instance {α : Type} {P : Prop} {d : Decidable P} {a b : SMonad Unit α} [ha : WkInst a] [hb : WkInst b] : + WkInst (@ite _ P d a b) := ⟨PresR.ite' ha.pres hb.pres⟩ + +instance {β γ : Type} {f : γ → SMonad Unit β} [hf : ∀ a, WkInst (f a)] (l : List γ) : + WkInst (NonDet.mapM f l) := ⟨PresR.mapM (fun a => (hf a).pres) l⟩ + +instance {α : Type} {C : α → State Unit → Prop} {B : α → SMonad Unit α} [hB : ∀ a, WkInst (B a)] (r : α) : + WkInst (NonDet.whileLoop C B r) := ⟨PresR.whileLoop (fun a => (hB a).pres) r⟩ + +instance {α : Type} (S : Set α × Prop) : WkInst (NonDet.selectF S : SMonad Unit α) := ⟨PresR.selectF S⟩ + +instance {α : Type} {f : α → SMonad Unit Unit} [hf : ∀ a, WkInst (f a)] (o : Option α) : + WkInst (NonDet.maybeM f o) := by + cases o + · exact ⟨PresR.pure _⟩ + · exact hf _ + +/-- A state modification that keeps the heap and user memory. -/ +theorem WkPresA.modify_fix (f : State Unit → State Unit) (hk : ∀ σ, (f σ).kheap = σ.kheap) + (hm : ∀ σ, (f σ).machineState.underlyingMemory = σ.machineState.underlyingMemory) : + WkPresA (NonDet.modify f : SMonad Unit Unit) := + PresR.modify f fun σ => ⟨fun p => Or.inl (by rw [hk]), fun p o h _ _ => by rw [hk]; exact h, + fun p o h _ _ => by rw [hk] at h; exact h, fun p x h => ⟨x, by rw [hk]; exact h, rfl⟩, + fun p y h => ⟨y, by rw [hk] at h; exact h⟩, fun p y k h => ⟨y, by rw [hk] at h; exact h, rfl⟩, hm σ⟩ + +macro "wkpres" : tactic => `(tactic| + repeat' (first + | exact WkInst.pres + | refine PresR.bind_pure _ _ ?_ + | refine PresR.bind ?_ (fun _ => ?_) + | refine PresR.when ?_ + | refine PresR.unless' ?_ + | refine PresR.ite' ?_ ?_ + | (refine WkPresA.modify_fix _ (fun _ => rfl) (fun _ => rfl)) + | dsimp only + | split)) + +/-- A TCB function changing at most the SC pointer. -/ +class WkTcbScFn (f : Tcb → Tcb) : Prop where + sc : ∀ x, f x = { x with tcbSchedContext := (f x).tcbSchedContext } + +/-- An SC function keeping the refill maximum. -/ +class WkScRmFn (f : SchedContext → SchedContext) : Prop where + keep : ∀ x, (f x).scRefillMax = x.scRefillMax + +instance (g : Option ObjRef → Option ObjRef) : WkTcbScFn (tcbSchedContextUpdate g) := ⟨fun _ => rfl⟩ +instance (g) : ScRyFn (scTcbUpdate g) := ⟨fun _ => ⟨rfl, rfl⟩⟩ +instance (g) : WkScRmFn (scTcbUpdate g) := ⟨fun _ => rfl⟩ +instance (g) : WkScRmFn (scRefillsUpdate g) := ⟨fun _ => rfl⟩ + +theorem threadSet_wkA (f : Tcb → Tcb) [TcbMyFn f] [WkTcbScFn f] (t : ObjRef) : WkPresA (threadSet (Z := Unit) f t) := by + intro σ r hr + refine ⟨threadSet_nkA f t σ r hr, ?_⟩ + obtain ⟨y1, hy1, h⟩ := hr + have hy1' : y1 ∈ (NonDet.assertOpt (getTcb (Z := Unit) t σ) σ).1 := hy1 + cases hg : getTcb (Z := Unit) t σ with + | none => rw [hg] at hy1'; exact absurd hy1' (Set.notMem_empty _) + | some x => + rw [hg] at hy1' + have e1 : y1 = (x, σ) := hy1' + subst e1 + obtain ⟨ko, hko, hr2⟩ := setObject_memA h + have hx := getTcb_kheapA hg + rw [hr2] + refine ⟨fun p o hp a b => ?_, fun p o hp a b => ?_, fun p z hp => ?_, fun p y hp => ?_, fun p y k hp => ?_, rfl⟩ + · have : p ≠ t := fun e => by rw [e, hx] at hp; injection hp with hp; exact b x hp.symm + exact (Function.update_of_ne this _ _).trans hp + · have : p ≠ t := fun e => by + have h1 : (Function.update σ.kheap t (some (.tcb (f x)))) p = some o := hp + rw [e, Function.update_self] at h1 + exact b _ (Option.some.inj h1).symm + have h1 : (Function.update σ.kheap t (some (.tcb (f x)))) p = some o := hp + rw [Function.update_of_ne this] at h1 + exact h1 + · by_cases e : p = t + · subst e + rw [hx] at hp + cases hp + exact ⟨_, by show Function.update σ.kheap p _ p = _; rw [Function.update_self], WkTcbScFn.sc _⟩ + · exact ⟨z, by show Function.update σ.kheap t _ p = _; rw [Function.update_of_ne e]; exact hp, rfl⟩ + · by_cases e : p = t + · subst e; exact ⟨x, hx⟩ + · have h1 : (Function.update σ.kheap t (some (.tcb (f x)))) p = some (.tcb y) := hp + rw [Function.update_of_ne e] at h1 + exact ⟨y, h1⟩ + · have : p ≠ t := fun e => by + have h1 : (Function.update σ.kheap t (some (.tcb (f x)))) p = some (.schedContext y k) := hp + rw [e, Function.update_self] at h1 + cases h1 + have h1 : (Function.update σ.kheap t (some (.tcb (f x)))) p = some (.schedContext y k) := hp + rw [Function.update_of_ne this] at h1 + exact ⟨y, h1, rfl⟩ + +instance (f : Tcb → Tcb) [TcbMyFn f] [WkTcbScFn f] (t : ObjRef) : WkInst (threadSet (Z := Unit) f t) := + ⟨threadSet_wkA f t⟩ + +theorem updateSchedContext_wkA (f : SchedContext → SchedContext) [ScRyFn f] [WkScRmFn f] (p : ObjRef) : + WkPresA (updateSchedContext (Z := Unit) p f) := by + intro σ r hr + refine ⟨updateSchedContext_nkA f p σ r hr, ?_⟩ + obtain ⟨y1, hy1, h⟩ := hr + have hy1' : y1 ∈ (NonDet.assertOpt (σ.kheap p) σ).1 := hy1 + cases hk : σ.kheap p with + | none => rw [hk] at hy1'; exact absurd hy1' (Set.notMem_empty _) + | some ko => + rw [hk] at hy1' + have e1 : y1 = (ko, σ) := hy1' + subst e1 + cases ko with + | schedContext sc n => + obtain ⟨ko', hko, hr2⟩ := setObject_memA h + rw [hr2] + refine ⟨fun q o hq ho ht => ?_, fun q o hq ho ht => ?_, fun q x hq => ?_, fun q y hq => ?_, + fun q y k hq => ?_, rfl⟩ + · have : q ≠ p := fun e => by rw [e, hk] at hq; injection hq with hq; exact ho sc n hq.symm + exact (Function.update_of_ne this _ _).trans hq + · have : q ≠ p := fun e => by + have h1 : (Function.update σ.kheap p (some (.schedContext (f sc) n))) q = some o := hq + rw [e, Function.update_self] at h1 + exact ho _ _ (Option.some.inj h1).symm + have h1 : (Function.update σ.kheap p (some (.schedContext (f sc) n))) q = some o := hq + rw [Function.update_of_ne this] at h1 + exact h1 + · have : q ≠ p := fun e => by rw [e, hk] at hq; cases hq + exact ⟨x, by show Function.update σ.kheap p _ q = _; rw [Function.update_of_ne this]; exact hq, rfl⟩ + · have : q ≠ p := fun e => by + have h1 : (Function.update σ.kheap p (some (.schedContext (f sc) n))) q = some (.tcb y) := hq + rw [e, Function.update_self] at h1 + cases h1 + have h1 : (Function.update σ.kheap p (some (.schedContext (f sc) n))) q = some (.tcb y) := hq + rw [Function.update_of_ne this] at h1 + exact ⟨y, h1⟩ + · by_cases e : q = p + · subst e + have h1 : (Function.update σ.kheap q (some (.schedContext (f sc) n))) q = some (.schedContext y k) := hq + rw [Function.update_self] at h1 + cases h1 + exact ⟨sc, hk, WkScRmFn.keep _⟩ + · have h1 : (Function.update σ.kheap p (some (.schedContext (f sc) n))) q = some (.schedContext y k) := hq + rw [Function.update_of_ne e] at h1 + exact ⟨y, h1, rfl⟩ + | _ => exact absurd h (Set.notMem_empty _) + +instance (p : ObjRef) (f : SchedContext → SchedContext) [ScRyFn f] [WkScRmFn f] : WkInst (updateSchedContext (Z := Unit) p f) := + ⟨updateSchedContext_wkA f p⟩ + +/-! ## Readers and small writers (copied from `P3/SigKeepA`) -/ + +instance {α : Type} (f : Tcb → α) (t : ObjRef) : WkInst (threadGet (Z := Unit) f t) := + ⟨by unfold threadGet; wkpres⟩ +instance (f : Tcb → Option ObjRef) (r : ObjRef) : WkInst (getTcbObjRef (Z := Unit) f r) := + ⟨by unfold getTcbObjRef; wkpres⟩ +instance (r : ObjRef) : WkInst (getThreadState (Z := Unit) r) := ⟨by unfold getThreadState; wkpres⟩ +instance (d : Domain) (p : Priority) : WkInst (getTcbQueue (Z := Unit) d p) := ⟨by unfold getTcbQueue; wkpres⟩ +instance (d : Domain) (p : Priority) (q : ReadyQueue) : WkInst (setTcbQueue (Z := Unit) d p q) := + ⟨by unfold setTcbQueue; wkpres⟩ +instance (a : ObjRef → List ObjRef → List ObjRef) (t : ObjRef) : WkInst (tcbSchedAction (Z := Unit) a t) := + ⟨by unfold tcbSchedAction; wkpres⟩ +instance (a : SchedulerAction) : WkInst (setSchedulerAction (Z := Unit) a) := + ⟨by unfold setSchedulerAction; wkpres⟩ +instance (t : ObjRef) : WkInst (tcbReleaseRemove (Z := Unit) t) := ⟨by unfold tcbReleaseRemove; wkpres⟩ + +instance (p : ObjRef) : WkInst (getObject (Z := Unit) p) := ⟨by unfold getObject; wkpres⟩ +instance (p : ObjRef) : WkInst (getSchedContext (Z := Unit) p) := ⟨by unfold getSchedContext; wkpres⟩ +instance (f : SchedContext → Option ObjRef) (p : ObjRef) : WkInst (getScObjRef (Z := Unit) f p) := + ⟨by unfold getScObjRef; wkpres⟩ +instance (p : ObjRef) : WkInst (getRefills (Z := Unit) p) := ⟨by unfold getRefills; wkpres⟩ +instance (p : ObjRef) : WkInst (getRefillHead (Z := Unit) p) := ⟨by unfold getRefillHead; wkpres⟩ +instance (p : ObjRef) : WkInst (getRefillTail (Z := Unit) p) := ⟨by unfold getRefillTail; wkpres⟩ +instance (p : ObjRef) : WkInst (getScRefillReady (Z := Unit) p) := ⟨by unfold getScRefillReady; wkpres⟩ +instance (p : ObjRef) (u : Ticks) : WkInst (getScRefillSufficient (Z := Unit) p u) := + ⟨by unfold getScRefillSufficient; wkpres⟩ +instance (p : ObjRef) (u : Ticks) : WkInst (fl4GetScRefillSufficient (Z := Unit) p u) := + ⟨by unfold fl4GetScRefillSufficient; wkpres⟩ +instance (p : ObjRef) : WkInst (getScActive (Z := Unit) p) := ⟨by unfold getScActive; wkpres⟩ +instance (p : ObjRef) : WkInst (isRoundRobin (Z := Unit) p) := ⟨by unfold isRoundRobin; wkpres⟩ + +/-! ## Scheduler queues -/ + +instance : WkInst (rescheduleRequired (Z := Unit)) := ⟨by unfold rescheduleRequired; wkpres⟩ +instance (t : ObjRef) : WkInst (possibleSwitchTo (Z := Unit) t) := ⟨by unfold possibleSwitchTo; wkpres⟩ + + +/-! ### Refills -/ + +instance (p : ObjRef) (f : Refill → Refill) : WkInst (updateRefillHd (Z := Unit) p f) := + ⟨by unfold updateRefillHd; wkpres⟩ +instance (p : ObjRef) (f : Refill → Refill) : WkInst (updateRefillTl (Z := Unit) p f) := + ⟨by unfold updateRefillTl; wkpres⟩ +instance (p : ObjRef) (r : Refill) : WkInst (refillAddTail (Z := Unit) p r) := ⟨by unfold refillAddTail; wkpres⟩ +instance (p : ObjRef) : WkInst (refillPopHead (Z := Unit) p) := ⟨by unfold refillPopHead; wkpres⟩ +instance (p : ObjRef) : WkInst (refillSize (Z := Unit) p) := ⟨by unfold refillSize; wkpres⟩ +instance (p : ObjRef) : WkInst (refillSingle (Z := Unit) p) := ⟨by unfold refillSingle; wkpres⟩ +instance (p : ObjRef) : WkInst (refillFull (Z := Unit) p) := ⟨by unfold refillFull; wkpres⟩ +instance (p : ObjRef) (r : Refill) : WkInst (scheduleUsed (Z := Unit) p r) := ⟨by unfold scheduleUsed; wkpres⟩ +instance (p : ObjRef) : WkInst (mergeOverlappingRefills (Z := Unit) p) := ⟨by unfold mergeOverlappingRefills; wkpres⟩ +instance (p : ObjRef) : WkInst (refillHeadOverlappingLoop (Z := Unit) p) := + ⟨by unfold refillHeadOverlappingLoop; wkpres⟩ +instance (p : ObjRef) : WkInst (refillUnblockCheck (Z := Unit) p) := ⟨by unfold refillUnblockCheck; wkpres⟩ +instance (o : Option ObjRef) (a b : Option Bool) : WkInst (ifCondRefillUnblockCheck (Z := Unit) o a b) := + ⟨by unfold ifCondRefillUnblockCheck; wkpres⟩ +instance (p : ObjRef) : WkInst (mergeNonoverlappingHeadRefill (Z := Unit) p) := + ⟨by unfold mergeNonoverlappingHeadRefill; wkpres⟩ +instance (m : Ticks) (p : ObjRef) : WkInst (headInsufficientLoopWith (Z := Unit) m p) := + ⟨by unfold headInsufficientLoopWith; wkpres⟩ +instance (p : ObjRef) (u : Ticks) : WkInst (chargeEntireHeadRefill (Z := Unit) p u) := + ⟨by unfold chargeEntireHeadRefill; wkpres⟩ +instance (l : Ticks) (p : ObjRef) (u : Ticks) : WkInst (handleOverrunWith (Z := Unit) l p u) := + ⟨by unfold handleOverrunWith; wkpres⟩ +instance (l m u : Ticks) : WkInst (refillBudgetCheckWith (Z := Unit) l m u) := + ⟨by unfold refillBudgetCheckWith; wkpres⟩ +instance (u : Ticks) : WkInst (fl4RefillBudgetCheck (Z := Unit) u) := ⟨by unfold fl4RefillBudgetCheck; wkpres⟩ +instance (u : Ticks) : WkInst (refillBudgetCheckRoundRobin (Z := Unit) u) := + ⟨by unfold refillBudgetCheckRoundRobin; wkpres⟩ + + +instance (t : ObjRef) : WkInst (setThreadStateAct (Z := Unit) t) := ⟨by unfold setThreadStateAct; wkpres⟩ +instance : WkInst (rescheduleRequired (Z := Unit)) := ⟨by unfold rescheduleRequired; wkpres⟩ +instance (t : ObjRef) : WkInst (possibleSwitchTo (Z := Unit) t) := ⟨by unfold possibleSwitchTo; wkpres⟩ + + +/-! ## The tail -/ + +instance {α β : Type} [Nonempty β] (t : α) (ts : List α) (f : α → RMonad Unit β) (R : β → β → Prop) : + WkInst (orderedInsert (Z := Unit) t ts f R) := ⟨by unfold orderedInsert; wkpres⟩ +instance (t : ObjRef) : WkInst (tcbReleaseEnqueue (Z := Unit) t) := ⟨by unfold tcbReleaseEnqueue; wkpres⟩ +instance (p : ObjRef) : WkInst (postpone (Z := Unit) p) := ⟨by unfold postpone; wkpres⟩ +instance (p : ObjRef) : WkInst (schedContextResume (Z := Unit) p) := ⟨by unfold schedContextResume; wkpres⟩ +instance (t : ObjRef) : WkInst (testReschedule (Z := Unit) t) := ⟨by unfold testReschedule; wkpres⟩ +instance (sc t : ObjRef) : WkInst (schedContextDonate (Z := Unit) sc t) := ⟨by unfold schedContextDonate; wkpres⟩ +instance (t n : ObjRef) : WkInst (maybeDonateSc (Z := Unit) t n) := ⟨by unfold maybeDonateSc; wkpres⟩ + +/-- **The wake tail keeps `WkKeepA`.** -/ +theorem specWakeTail_wkA (t n : ObjRef) : WkPresA (specWakeTail t n) := by + unfold specWakeTail; wkpres + +end + +end Sched + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3WrapA.lean b/proofs/Fl4ProofsA64/P3/Stage3WrapA.lean new file mode 100644 index 000000000..1242eec11 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3WrapA.lean @@ -0,0 +1,693 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Plan B step 3): **`handle_invocation` at the record, over the dispatch scope** (Send, +NBSend, Call). + +From the syscall-entry record, FL4's `handle_invocation` returns, FL4's spec `fl4HandleInvocationR` has the outcome, and +the record at a body's end (`A64RelS3`) holds. The prefix (message info, capability lookup, IPC buffer, extra caps, +message words) is proved here; the decode and perform come from the dispatch top (`Stage3DispTopA`). + +* `BodyOKA.ofSame`: the body-end facts of the unchanged entry state. +* `InvSpecA` (`Stage3DispBaseA`): [J] the spec side of one invocation: the spec decode at the looked-up cap returns an operation, and the + perform from the restarted state reaches the arm's state (with empty reply data if the caller is still restarting), + or is preempted there. +* `EpiP1A`: [P1] the P1 invariants after the success epilogue's writes. +* `JSyscallA.invocationA`: the wrapper. +* `JSyscallA.sendBodyA`: `dispatch_syscall` for Send, NBSend and Call (with `after_invocation`). + +Named plug-ins: `hFault` [J] (a lookup fault of a blocking invocation: `handle_fault` against `handle_fault`); +`hErr` [J] (the code after a decode that returned an `Err` other than preemption: Lift 1b's error lemma); `hPre` [J] +(`after_invocation` after a preempted invocation, from `A64RelPS3` to `A64RelS3`: **discharged by `afterPreemptA`**, +`P3/Stage3AfterPreemptA`, over that theorem's named premises, of which the narrowed [J] `take` and [J] +`CurScChargeableA` (`chargeable`) must be discharged before merge; not wired here, the callers keep `hPre`); the `InvSpecA` clause of `hScope` [J] (the spec's decode and +perform, at the spec prefix's own outcome); `hP1` [P1] (`EpiP1A`). + +New module. +-/ +import Fl4ProofsA64.P3.Stage3DispTopA +import Fl4ProofsA64.P3.Stage3EpilogueA +import Fl4ProofsA64.P3.FrontEndExtraA +import Fl4ProofsA64.P3.EpBufA +import Fl4ProofsA64.P3.OuterEntryA +import Fl4ProofsA64.P3.ReplyLiveSpecA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## The spec's `handle_invocation`, in named pieces -/ + +/-- The prefix lookups of `fl4HandleInvocationR`. -/ +def frontSpecA (t : Fl4SpecA64.ObjRef) (cptr : Fl4SpecA64.CapRef) (mi : Fl4SpecA64.MessageInfo) : + FMonad Unit (Fl4SpecA64.CslotPtr × Fl4SpecA64.Cap × List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr) × + Option Fl4SpecA64.ObjRef) := do + let (cap, slot) ← capFaultOnFailure (ofBl cptr) false <| lookupCapAndSlot t cptr + let buffer ← NonDetE.liftE <| lookupIpcBuffer false t + let extracaps ← lookupExtraCaps t buffer mi + NonDetE.returnOk (slot, cap, extracaps, buffer) + +/-- The message words and the decode of `fl4HandleInvocationR`. -/ +def decSpecA (calling : Bool) (t : Fl4SpecA64.ObjRef) (cptr : Fl4SpecA64.CapRef) (mi : Fl4SpecA64.MessageInfo) : + Fl4SpecA64.CslotPtr × Fl4SpecA64.Cap × List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr) × + Option Fl4SpecA64.ObjRef → SEMonad Unit Fl4Invocation := + fun (slot, cap, extracaps, buffer) => do + let args ← NonDetE.liftE <| getMrs t buffer mi + fl4DecodeInvocation calling false mi.miLabel args cptr slot cap extracaps buffer + +/-- The perform of `fl4HandleInvocationR`. -/ +def finSpecA (blocking calling donate : Bool) (t : Fl4SpecA64.ObjRef) (oper : Fl4Invocation) : PMonad Unit Unit := do + withoutPreemption <| setThreadState t .restart + let reply ← fl4PerformInvocation blocking calling donate oper + withoutPreemption <| epiSpecA calling t reply + +theorem fl4HandleInvocationR_piecesA (rep calling blocking donate : Bool) (cptr : Fl4SpecA64.CapRef) : + fl4HandleInvocationR (Z := Unit) rep calling blocking donate false cptr = (do + let thread ← NonDetE.liftE <| NonDet.gets fun s : Fl4SpecA64.State Unit => s.curThread + let info ← withoutPreemption <| getMessageInfo thread + syscall (frontSpecA thread cptr info) (fun fault => NonDet.when blocking <| handleFault thread fault) + (decSpecA calling thread cptr info) + (fun err => NonDet.when calling <| fl4ReplyFromKernelError rep thread <| msgFromSyscallError err) + (finSpecA blocking calling donate thread)) := rfl + +theorem syscall_okA {α β γ : Type} {mF : FMonad Unit α} {hF : Fl4SpecA64.Fault → SMonad Unit γ} + {mE : α → SEMonad Unit β} {hE : SyscallError → SMonad Unit γ} {mFin : β → PMonad Unit γ} + {σ : Fl4SpecA64.State Unit} {x : α} {y : β} (h1 : mF.run σ = ({(.ok x, σ)}, False)) + (h2 : (mE x).run σ = ({(.ok y, σ)}, False)) : (syscall mF hF mE hE mFin).run σ = (mFin y).run σ := by + unfold syscall withoutPreemption + rw [run_bind_okA (run_liftEA (show (ExceptT.run mF : SMonad Unit _) σ = ({(.ok x, σ)}, False) from h1))] + dsimp only + rw [run_bind_okA (run_liftEA (show (ExceptT.run (mE x) : SMonad Unit _) σ = ({(.ok y, σ)}, False) from h2))] + +/-- `syscall` along a successful front end and one outcome of the decode. -/ +theorem syscall_memA {α β γ : Type} {mF : FMonad Unit α} {hF : Fl4SpecA64.Fault → SMonad Unit γ} + {mE : α → SEMonad Unit β} {hE : SyscallError → SMonad Unit γ} {mFin : β → PMonad Unit γ} + {σ τ : Fl4SpecA64.State Unit} {x : α} {y : β} {v : Except Unit γ} (h1 : mF.run σ = ({(.ok x, σ)}, False)) + (h2 : (.ok y, σ) ∈ ((mE x).run σ).1) (h : (v, τ) ∈ ((mFin y).run σ).1) : + (v, τ) ∈ ((syscall mF hF mE hE mFin).run σ).1 := by + unfold syscall withoutPreemption + rw [run_bind_okA (run_liftEA (show (ExceptT.run mF : SMonad Unit _) σ = ({(.ok x, σ)}, False) from h1))] + dsimp only + rw [ExceptT.run_bind] + exact mem_bindA.2 ⟨(.ok (.ok y), σ), liftE_memA (m := ExceptT.run (mE x)) h2, h⟩ + +theorem syscall_faultA {α β γ : Type} {mF : FMonad Unit α} {hF : Fl4SpecA64.Fault → SMonad Unit γ} + {mE : α → SEMonad Unit β} {hE : SyscallError → SMonad Unit γ} {mFin : β → PMonad Unit γ} + {σ τ : Fl4SpecA64.State Unit} {f : Fl4SpecA64.Fault} {v : γ} (h1 : mF.run σ = ({(.error f, σ)}, False)) + (h2 : (v, τ) ∈ (hF f σ).1) : (.ok v, τ) ∈ ((syscall mF hF mE hE mFin).run σ).1 := by + unfold syscall withoutPreemption + rw [run_bind_okA (run_liftEA (show (ExceptT.run mF : SMonad Unit _) σ = ({(.error f, σ)}, False) from h1))] + exact liftE_memA h2 + +/-- FL4's register writes of the success epilogue. -/ +def epiRegsA {S : Type} (st : machine.Store S) (calling : Bool) (s : S) (t : U64) : Result S := + if calling then msg.reply_success_empty st s t else ok s + +/-- [P1] **The P1 invariants after the success epilogue** (module docstring): at any record state with the current +thread restarting, after the epilogue's register writes and `TS_RUNNING`. -/ +def EpiP1A {S : Type} (st : machine.Store S) [L : LawfulStore st] {A : Type} (AR : A → HwRootA → Prop) + (ar : machine.Arch A) (nA : U64) (n : Nat) (m : Fl4SpecA64.McState) (calling : Bool) : Prop := + ∀ (Z : XSetsA) (H : Heap) (OS U Ac Pp Tb : Set U64) (Lv : U64 → Nat) (ia ie : Nat) (N E Rp : Set U64) + (Bf : Set (U64 × Fl4SpecA64.Word)) (stack : U64 → List U64) (κ : U64 → U64) (s : S) (a : A) + (σ : Fl4SpecA64.State Unit), + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m → + L.mem s (stA (L.curW s)) = sched.TS_RESTART → + ∀ s2, epiRegsA st calling s (L.curW s) = ok s2 → ∀ σX : Fl4SpecA64.State Unit, + let s' := L.wr s2 (stA (L.curW s)) sched.TS_RUNNING + sched.set_thread_state st s2 (L.curW s) sched.TS_RUNNING = ok s' → + SysL st H Z.Cs OS ia ie s' ∧ SymL st H Z.Cs s' ∧ + BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s' ∧ + Fl4ProofsA64.P1.UserOK st H.tcbs Z.Cs H.omega H U s' ∧ J7A st (J7R2A st ar nA) Z H OS s' σX m + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {Clock : A → Fl4SpecA64.MachineState → Prop} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {ar : machine.Arch A} {nA : U64} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- **The body-end facts of the unchanged entry state.** -/ +theorem Sched.BodyOKA.ofSame {J5 J7 : JSlotA S} + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) : BodyOKA σ m := by + refine BodyOKA.ofEntry J (Or.inl rfl) rfl rfl rfl rfl rfl rfl (fun y' k h => ⟨y', h, rfl, rfl, rfl⟩) + (fun _ x hx => ?_) + have htZ : L.curW s ∈ Z.T := J.sr.curT + have hk : σ.kheap (specPtr (L.curW s)) = some (.tcb x) := J.j.1.sch.curThread ▸ hx + have c := (J.ep.at_tcb htZ hk).st + have nvm : L.mem s (stA (L.curW s)) ≠ sched.TS_RUNNING_VM := J.toJStage3EntryA.curNotVmA + rcases J.running with h | h | h + · rw [h, ts_vals.1] at c + cases e : x.tcbState <;> rw [e] at c <;> first | exact Or.inl rfl | exact Or.inr rfl | (simp [Notif.tsCode] at c) + · rw [h, ts_vals.2.1] at c + cases e : x.tcbState <;> rw [e] at c <;> first | exact Or.inl rfl | exact Or.inr rfl | (simp [Notif.tsCode] at c) + · exact absurd h nvm + +/-- **The success epilogue after a decode that returned** (`set Running`, with the empty reply for a call), at the +record at a body's end. -/ +theorem A64RelS3.epilogueA {Cs : Set U64} (R : A64RelS3 st AR ar nA n Cs s a σ m) (calling : Bool) + (hP1 : EpiP1A st AR ar nA n m calling) {x : Fl4SpecA64.Tcb} (hx : σ.kheap σ.curThread = some (.tcb x)) + (hr : x.tcbState = .restart) : + ∃ s2 σ', epiRegsA st calling s (L.curW s) = ok s2 ∧ + sched.set_thread_state st s2 (L.curW s) sched.TS_RUNNING = ok (L.wr s2 (stA (L.curW s)) sched.TS_RUNNING) ∧ + ((), σ') ∈ ((do + Fl4Spec.NonDet.when (calling = true) (replyFromKernel (Z := Unit) σ.curThread (0, [])) + setThreadState (Z := Unit) σ.curThread .running) σ).1 ∧ + A64RelS3 st AR ar nA n Cs (L.wr s2 (stA (L.curW s)) sched.TS_RUNNING) a σ' m := by + obtain ⟨Z, H, OS, U, Ac, Pp, Tb, Lv, ia, ie, N, E, Rp, Bf, stack, κ, hCs, J, B⟩ := R + have ct : σ.curThread = specPtr (L.curW s) := J.j.1.sch.curThread + have htZ : L.curW s ∈ Z.T := J.sr.curT + have hk : σ.kheap (specPtr (L.curW s)) = some (.tcb x) := ct ▸ hx + have hw : L.mem s (stA (L.curW s)) = sched.TS_RESTART := by + have c := (J.ep.at_tcb htZ hk).st + rw [hr] at c + exact UScalar.eq_of_val_eq (by rw [c, ts_vals.2.1]; rfl) + have V : TcbIpcViewsA st Z m κ N E Rp Bf stack s σ := + ⟨⟨Z.Q, Z.R, J.sr⟩, J.ntfn, J.ep, J.reply, J.mcp, J.yieldRel⟩ + have step : ∀ (uc : Fl4SpecA64.UserContext) (s2 : S), epiRegsA st calling s (L.curW s) = ok s2 → + TcbIpcViewsA st Z m κ N E Rp Bf stack s2 + (setObjA σ (specPtr (L.curW s)) (.tcb { x with tcbArch := { x.tcbArch with tcbContext := uc } })) → + WritesIn st s s2 (ScConsumedRegsA (L.curW s)) → + ((do + Fl4Spec.NonDet.when (calling = true) (replyFromKernel (Z := Unit) σ.curThread (0, [])) + setThreadState (Z := Unit) σ.curThread .running) σ) = + setThreadState (Z := Unit) (specPtr (L.curW s)) .running + (setObjA σ (specPtr (L.curW s)) (.tcb { x with tcbArch := { x.tcbArch with tcbContext := uc } })) → + ∃ s2 σ', epiRegsA st calling s (L.curW s) = ok s2 ∧ + sched.set_thread_state st s2 (L.curW s) sched.TS_RUNNING = + ok (L.wr s2 (stA (L.curW s)) sched.TS_RUNNING) ∧ + ((), σ') ∈ ((do + Fl4Spec.NonDet.when (calling = true) (replyFromKernel (Z := Unit) σ.curThread (0, [])) + setThreadState (Z := Unit) σ.curThread .running) σ).1 ∧ + A64RelS3 st AR ar nA n Cs (L.wr s2 (stA (L.curW s)) sched.TS_RUNNING) a σ' m := by + intro uc s2 e2 V2 w2 spec + have fin := fun σX : Fl4SpecA64.State Unit => + hP1 Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ s a σ J hw s2 e2 σX + obtain ⟨eRun, σ', hset, e', Q, Rl, J'⟩ := J.runRestartA (t := L.curW s) rfl hk hr uc V2 w2 fin + have mem := (congrArg (fun p : Set (Unit × Fl4SpecA64.State Unit) × Prop => ((), σ') ∈ p.1) + (spec.trans hset)).mpr (Set.mem_singleton _) + have e'' : σ' = setObjA σ σ.curThread + (.tcb { x with tcbArch := { x.tcbArch with tcbContext := uc }, tcbState := .running }) := + e'.trans (congrArg (fun p => setObjA σ p + (.tcb { x with tcbArch := { x.tcbArch with tcbContext := uc }, tcbState := .running })) ct.symm) + have B' : Sched.BodyOKA σ' m := Sched.BodyOKA.runA (uc := uc) B hx hr e'' + exact ⟨s2, σ', e2, eRun, mem, A64RelS3.ofJ J' B' hCs⟩ + cases calling with + | false => + have e : setObjA σ (specPtr (L.curW s)) (.tcb { x with tcbArch := { x.tcbArch with tcbContext := x.tcbArch.tcbContext } }) = + σ := by + show { σ with kheap := Function.update σ.kheap (specPtr (L.curW s)) (some (.tcb x)) } = σ + rw [← hk, Function.update_eq_self] + refine step x.tcbArch.tcbContext s rfl (by rw [e]; exact V) (WritesIn.refl _ _) ?_ + rw [e, ct] + unfold Fl4Spec.NonDet.when + rw [if_neg Bool.false_ne_true, nd_bind_oneA (show (Fl4Spec.NonDet.pure () : SMonad Unit Unit) σ = _ from rfl)] + | true => + have hP := J.iplaced + have hta := hP.n.tcb _ htZ + obtain ⟨mi, hmi, hmiv⟩ := spec_existsA (to_word_specA 0#u64 0#u64 (by simp)) + have V1 := tcbRegWrite_viewsA hP J.ipcPlace V htZ hk Fl4SpecA64.badge_register 0#u64 + set x1 := Notif.setReg x Fl4SpecA64.badge_register (0#u64 : U64).bv with hx1 + have h1 : (Notif.updK σ (specPtr (L.curW s)) (.tcb x1)).kheap (specPtr (L.curW s)) = some (.tcb x1) := by + simp [Notif.updK] + have V2 := tcbRegWrite_viewsA hP J.ipcPlace V1 htZ h1 Fl4SpecA64.msg_info_register mi + set x2 := Notif.setReg x1 Fl4SpecA64.msg_info_register mi.bv with hx2 + have ex : x2 = { x with tcbArch := { x.tcbArch with tcbContext := emptyUcA x } } := by + have hmi' : mi.bv = messageInfoToData ⟨natToLen 0, 0, 0, 0⟩ := by + have z : (U64.bv 0#u64 &&& mask msgLabelBits) = 0 := by + have z0 : U64.bv 0#u64 = 0#64 := rfl + rw [z0]; simp + rw [hmiv, z]; rfl + rw [hx2, hx1, hmi'] + rfl + have eσ : Notif.updK (Notif.updK σ (specPtr (L.curW s)) (.tcb x1)) (specPtr (L.curW s)) (.tcb x2) = + setObjA σ (specPtr (L.curW s)) (.tcb { x with tcbArch := { x.tcbArch with tcbContext := emptyUcA x } }) := by + rw [← ex] + unfold Notif.updK setObjA + simp only [Function.update_idem] + rw [eσ] at V2 + refine step (emptyUcA x) _ ?_ V2 ?_ ?_ + · show msg.reply_success_empty st s (L.curW s) = ok + (L.wr (L.wr s (Endp.regA (L.curW s) (regIdxA Fl4SpecA64.badge_register)) 0#u64) + (Endp.regA (L.curW s) (regIdxA Fl4SpecA64.msg_info_register)) mi) + unfold msg.reply_success_empty + rw [← regIdxA_badge, ut_set_regA, bind_ok] + unfold msginfo.MessageInfo.new msg.set_msg_info + simp only [bind_ok] + rw [← regIdxA_msgInfo] + change (msginfo.MessageInfo.to_word + { label := 0#u64, caps_unwrapped := 0#u64, extra_caps := 0#u64, length := 0#u64 } >>= fun i => + obj.Tcb.set_reg st (L.curW s) (L.wr s (Endp.regA (L.curW s) (regIdxA Fl4SpecA64.badge_register)) 0#u64) + (regIdxA Fl4SpecA64.msg_info_register) i) = _ + rw [hmi, bind_tc_ok] + exact ut_set_regA _ _ _ _ + · have w : ∀ (s0 : S) (r : Fl4SpecA64.Register) (w : U64) (K : U64), (regIdxA r).val < 37 → + K.val = 256 + 8 * (regIdxA r).val → + WritesIn st s0 (L.wr s0 (Endp.regA (L.curW s) (regIdxA r)) w) (· = wa (L.curW s) K) := by + intro s0 r w K hr hK + have e : Endp.regA (L.curW s) (regIdxA r) = wa (L.curW s) K := by + apply UScalar.eq_of_val_eq + rw [regA_val hta hr, field_val hta (by omega)] + omega + rw [e] + exact writesIn_wr (field_aligned hta (by omega) (by omega)) + have w0 := w s Fl4SpecA64.badge_register 0#u64 256#u64 (regIdxA_lt _) + (by rw [regIdxA_badge]; simp [layout.reg.BADGE, layout.reg.X0]) + have w1 := w (L.wr s (Endp.regA (L.curW s) (regIdxA Fl4SpecA64.badge_register)) 0#u64) + Fl4SpecA64.msg_info_register mi 264#u64 (regIdxA_lt _) + (by rw [regIdxA_msgInfo]; simp [layout.reg.MSG_INFO, layout.reg.X1]) + refine (w0.trans w1).mono fun b h => ?_ + rcases h with e | e + · exact Or.inl e + · exact Or.inr (Or.inl e) + · have hc : ctxUpdA σ (specPtr (L.curW s)) x (emptyUcA x) = setObjA σ (specPtr (L.curW s)) + (.tcb { x with tcbArch := { x.tcbArch with tcbContext := emptyUcA x } }) := rfl + rw [ct, ← hc] + unfold Fl4Spec.NonDet.when + rw [if_pos rfl] + exact nd_bind_oneA (replyFromKernel_emptyA hk) _ + +theorem getThreadState_memA {σ σ1 : State Unit} {t : ObjRef} {s0 : ThreadState} + (h : (s0, σ1) ∈ (getThreadState (Z := Unit) t σ).1) : + σ1 = σ ∧ ∃ y, σ.kheap t = some (.tcb y) ∧ y.tcbState = s0 := by + unfold getThreadState threadGet at h + obtain ⟨x, hx, h2⟩ := mem_bindA.1 h + change x ∈ (NonDet.assertOpt (getTcb t σ) σ).1 at hx + cases hg : getTcb t σ with + | none => rw [hg] at hx; exact absurd hx (Set.notMem_empty _) + | some tcb => + rw [hg] at hx + obtain rfl : x = (tcb, σ) := hx + have e : (s0, σ1) = (tcb.tcbState, σ) := h2 + obtain ⟨e1, e2⟩ := Prod.mk.inj e + exact ⟨e2, tcb, getTcb_kheapA hg, e1.symm⟩ + +/-- **After the spec's epilogue the caller is no longer `Restart`.** -/ +theorem epiSpec_notRestartA {calling : Bool} {t : ObjRef} {reply : List Data} {σ2 σ' : State Unit} + (h : ((), σ') ∈ (epiSpecA calling t reply σ2).1) : + ∀ y, σ'.kheap t = some (.tcb y) → y.tcbState ≠ .restart := by + intro y hy + unfold epiSpecA at h + obtain ⟨⟨s0, σ1⟩, hx, h2⟩ := mem_bindA.1 h + obtain ⟨hx1, y0, hy0, hys⟩ := getThreadState_memA hx + dsimp only at h2 + split at h2 + · obtain ⟨z, hz, h3⟩ := mem_bindA.1 h2 + obtain ⟨y1, hy1, hy1s⟩ := setThreadState_memA h3 + rw [hy1] at hy + have e := Option.some.inj hy + obtain rfl : y1 = y := KernelObject.tcb.inj e + rw [hy1s]; intro hh; cases hh + · rename_i hne + have e : σ' = σ1 := congrArg Prod.snd h2 + rw [e, hx1, hy0] at hy + obtain rfl : y0 = y := KernelObject.tcb.inj (Option.some.inj hy) + rw [hys]; exact hne + +/-- **`handle_invocation` at the record** (module docstring). -/ +theorem JSyscallA.invocationA [PlainStore st] (hA : P0.ArchTotal ar) (hM : MaxSpi ar) (hPl : ArchPlat ar) + (hCnt : Sched.CounterStepA σ) + (J : JSyscallA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar hPl.ipa)) s a σ m) + (hfm : A64FrameMasks st s) (rep calling blocking donate : Bool) (cptr : U64) + (hFault : blocking = true → ∀ (f : error.Fault) (f' : Fl4SpecA64.Fault), CapFaultCorrA f f' → + faults.handle_fault st ar s a (L.curW s) f ⦃ r => ∃ τ, ((), τ) ∈ (handleFault (Z := Unit) σ.curThread f' σ).1 ∧ + A64RelS3 st AR ar hPl.ipa n Z.Cs r.1 r.2 τ m ⦄) + (hScope : ∀ (info : msginfo.MessageInfo) (buf : U64) (r1 : cspace.Resolved) + (e : Aeneas.Std.Array obj.Slot 3#usize), + msg.msg_info st s (L.curW s) = ok info → msg.lookup_ipc_buffer st s false (L.curW s) = ok buf → + cspace.lookup_slot st s (L.curW s) cptr = ok (.Ok r1) → + ipc.lookup_extra_caps st s (L.curW s) buf info = ok (.Ok e) → + -- only where the decode succeeds or is preempted; other errors are `hErr`'s + ∀ (r0 : core.result.Result Unit error.Exc) (s0 : S) (a0 : A), + syscall.decode_invocation st ar s a info.label (msgLenA info buf) r1.slot (capA st s r1.slot) blocking + calling donate false buf e = ok (r0, s0, a0) → (∀ x, r0 = .Err x → x = error.Exc.Preempted) → + ∃ Out, DispScopeA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n ar s a σ m Clock hPl.ipa info.label + (msgLenA info buf) r1.slot blocking calling donate false buf e Out ∧ + ∀ (r : core.result.Result Unit error.Exc) (s' : S) (a' : A) (σ' : Fl4SpecA64.State Unit), + syscall.decode_invocation st ar s a info.label (msgLenA info buf) r1.slot (capA st s r1.slot) blocking + calling donate false buf e = ok (r, s', a') → Out σ' → + ∀ (done : Bool), (done = true ∧ r = .Ok ()) ∨ (done = false ∧ r = .Err error.Exc.Preempted) → + ∀ (mi : Fl4SpecA64.MessageInfo) (sl : Fl4SpecA64.CslotPtr) (c : Fl4SpecA64.Cap) + (excaps : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) (buffer : Option Fl4SpecA64.ObjRef) + (args : List Fl4SpecA64.Data), + getMessageInfo (Z := Unit) σ.curThread σ = ({(mi, σ)}, False) → + (frontSpecA σ.curThread (Fl4SpecA64.toBl cptr.bv) mi).run σ = ({(.ok (sl, c, excaps, buffer), σ)}, False) → + r1.slot = cslotAddrA sl → CapRelA (capA st s r1.slot) c → ExtraOKA Z.th (L.mem s) e excaps → + AbsSlotA Z.th sl → BufViewA (L.mem s) σ.machineState.underlyingMemory buf buffer → + getCap (Z := Unit) sl σ = ({(c, σ)}, False) → + lookupIpcBuffer (Z := Unit) false σ.curThread σ = ({(buffer, σ)}, False) → + (lookupExtraCaps (Z := Unit) σ.curThread buffer mi).run σ = ({(.ok excaps, σ)}, False) → + getMrs (Z := Unit) σ.curThread buffer mi σ = ({(args, σ)}, False) → + InvSpecA calling blocking donate done mi.miLabel args (Fl4SpecA64.toBl cptr.bv) sl c excaps buffer σ σ') + (hErr : ∀ (info : msginfo.MessageInfo) (buf : U64) (r1 : cspace.Resolved) + (e : Aeneas.Std.Array obj.Slot 3#usize) (x : error.Exc) (s' : S) (a' : A), + msg.msg_info st s (L.curW s) = ok info → msg.lookup_ipc_buffer st s false (L.curW s) = ok buf → + cspace.lookup_slot st s (L.curW s) cptr = ok (.Ok r1) → + ipc.lookup_extra_caps st s (L.curW s) buf info = ok (.Ok e) → + syscall.decode_invocation st ar s a info.label (msgLenA info buf) r1.slot (capA st s r1.slot) blocking + calling donate false buf e = ok (.Err x, s', a') → x ≠ error.Exc.Preempted → + (match x with + | .Sys e2 => if calling then (do let s2 ← syscall.reply_error st s' (L.curW s) e2 + ok (core.result.Result.Ok (), s2, a')) + else ok (core.result.Result.Ok (), s', a') + | _ => ok (core.result.Result.Ok (), s', a') : Result (core.result.Result Unit error.Exc × S × A)) + ⦃ r => r.1 = .Ok () ∧ ∃ τ, + (.ok (), τ) ∈ ((fl4HandleInvocationR (Z := Unit) rep calling blocking donate false + (Fl4SpecA64.toBl cptr.bv)).run σ).1 ∧ A64RelS3 st AR ar hPl.ipa n Z.Cs r.2.1 r.2.2 τ m ⦄) + (hP1 : EpiP1A st AR ar hPl.ipa n m calling) : + syscall.handle_invocation st ar s a calling blocking donate false cptr ⦃ r => + ∃ e τ, (e, τ) ∈ ((fl4HandleInvocationR (Z := Unit) rep calling blocking donate false + (Fl4SpecA64.toBl cptr.bv)).run σ).1 ∧ + r.1 = (match e with | .ok _ => .Ok () | .error _ => .Err error.Exc.Preempted) ∧ + A64RelES3 st AR ar hPl.ipa n Z.Cs e r.2.1 r.2.2 τ m Clock ⦄ := by + have ct : σ.curThread = specPtr (L.curW s) := J.j.1.sch.curThread + have htZ : L.curW s ∈ Z.T := J.sr.curT + have hth : Z.th (L.curW s) = some .tcb := (J.j.1.thT _).2 htZ + obtain ⟨x, hk, -⟩ := J.ep.tcb (L.curW s) htZ + set t := L.curW s with ht + -- the prefix, both sides + have ecur : sched.cur_thread st s = ok t := cur_thread_ok s + obtain ⟨info, einfo, hlab, -, hext, hlen⟩ := spec_existsA (msg_info_specA (L := L) s t) + set mi := Fl4SpecA64.dataToMessageInfo (x.tcbArch.tcbContext.userRegs Fl4SpecA64.msg_info_register) with hmi + have hmw := (J.ep.at_tcb htZ hk).regView.1 + have gmi : getMessageInfo (Z := Unit) (specPtr t) σ = ({(mi, σ)}, False) := getMessageInfo_eqA hk + have specHead : ∀ (e : Except Unit Unit) (τ : Fl4SpecA64.State Unit), + (e, τ) ∈ ((syscall (frontSpecA (specPtr t) (Fl4SpecA64.toBl cptr.bv) mi) + (fun fault => NonDet.when blocking <| handleFault (specPtr t) fault) + (decSpecA calling (specPtr t) (Fl4SpecA64.toBl cptr.bv) mi) + (fun err => NonDet.when calling <| fl4ReplyFromKernelError rep (specPtr t) <| msgFromSyscallError err) + (finSpecA blocking calling donate (specPtr t))).run σ).1 → + (e, τ) ∈ ((fl4HandleInvocationR (Z := Unit) rep calling blocking donate false + (Fl4SpecA64.toBl cptr.bv)).run σ).1 := fun e τ h => by + have gc : (NonDet.gets fun s : Fl4SpecA64.State Unit => s.curThread) σ = ({(specPtr t, σ)}, False) := by + rw [← ct]; rfl + rw [fl4HandleInvocationR_piecesA, run_bind_okA (run_liftEA gc)] + unfold withoutPreemption + rw [run_bind_okA (run_liftEA gmi)] + exact h + -- the buffer + obtain ⟨buf, ebuf, hbuf0, hbufS⟩ := spec_existsA (lookup_buf_full s σ J.j.1.st hth htZ hk J.bufCover J.bufNative false) + have eIB : lookupIpcBuffer (Z := Unit) false (specPtr t) σ = ({(ipcBufOfA false x, σ)}, False) := + lookupIpcBuffer_eqA hk false + have hBV : BufViewA (L.mem s) σ.machineState.underlyingMemory buf (ipcBufOfA false x) := by + cases ho : ipcBufOfA false x with + | none => rw [hbuf0.2 ho]; exact bufViewA_none _ _ + | some sb => + refine J.ep.bufView (hbufS sb ho) fun h => ?_ + rw [hbuf0.1 h] at ho; cases ho + -- the capability lookup + obtain ⟨lr, elr, se, hse, hrel⟩ := spec_existsA + (lookup_slot_refinesA Z.th s σ J.j.1.st J.j.1.cv t hth cptr) + unfold syscall.handle_invocation + rw [ecur, bind_ok, einfo, bind_ok, elr, bind_ok] + -- a fault of the prefix + have faultCase : ∀ (f : error.Fault) (f' : Fl4SpecA64.Fault), CapFaultCorrA f f' → + (frontSpecA (specPtr t) (Fl4SpecA64.toBl cptr.bv) mi).run σ = ({(.error f', σ)}, False) → + (if blocking = true then do + let (s1, a1) ← faults.handle_fault st ar s a t f + ok (core.result.Result.Ok (), s1, a1) + else ok (core.result.Result.Ok (), s, a)) ⦃ r => + ∃ e τ, (e, τ) ∈ ((fl4HandleInvocationR (Z := Unit) rep calling blocking donate false + (Fl4SpecA64.toBl cptr.bv)).run σ).1 ∧ + r.1 = (match e with | .ok _ => .Ok () | .error _ => .Err error.Exc.Preempted) ∧ + A64RelES3 st AR ar hPl.ipa n Z.Cs e r.2.1 r.2.2 τ m Clock ⦄ := by + intro f f' corr run + have go : ∀ τ, ((), τ) ∈ ((NonDet.when blocking <| handleFault (Z := Unit) (specPtr t) f') σ).1 → + (.ok (), τ) ∈ ((fl4HandleInvocationR (Z := Unit) rep calling blocking donate false + (Fl4SpecA64.toBl cptr.bv)).run σ).1 := fun τ h => specHead _ _ (syscall_faultA run h) + by_cases hb : blocking = true + · rw [if_pos hb] + refine WP.spec_bind (hFault hb f f' corr) ?_ + rintro ⟨s1, a1⟩ ⟨τ, mem, R⟩ + simp only [uncurry_apply_pair, WP.spec_ok] + refine ⟨.ok (), τ, go τ ?_, rfl, R⟩ + unfold Fl4Spec.NonDet.when + rw [if_pos hb, ← ct]; exact mem + · rw [if_neg hb, WP.spec_ok] + refine ⟨.ok (), σ, go σ ?_, rfl, A64RelS3.ofJ J.toJStage3A (Sched.BodyOKA.ofSame J)⟩ + unfold Fl4Spec.NonDet.when + rw [if_neg hb]; exact Set.mem_singleton _ + cases lr with + | Err lf => + cases se with + | ok _ => exact hrel.1.elim + | error lf' => + dsimp only + refine faultCase _ (.capFault (ofBl (Fl4SpecA64.toBl cptr.bv)) false lf') ?_ ?_ + · exact ⟨_, false, lf, lf', rfl, by rw [ofBl_toBlA], hrel.1⟩ + · unfold frontSpecA + exact run_bind_errorA (lcs_errA _ hse) _ + | Ok r1 => + cases se with + | error _ => exact hrel.1.elim + | ok v => + obtain ⟨sl, rest⟩ := v + obtain ⟨⟨hsl, -⟩, p, hp1, hp, -⟩ := hrel + obtain ⟨c, hcr, hgc⟩ := J.j.1.st.slot_capA hp1 hp + have lcs := lcs_okA (ofBl (Fl4SpecA64.toBl cptr.bv)) hse hgc + dsimp only + rw [capA_eq, bind_ok, ebuf, bind_ok] + have hn : info.extra_caps.val = unat mi.miExtraCaps := by + show info.extra_caps.bv.toNat = _; rw [hext, hmw] + have hn3 : info.extra_caps.val ≤ 3 := by + show info.extra_caps.bv.toNat ≤ 3; rw [hext, hmw]; exact extra_le3A _ + obtain ⟨r2, er2, hx⟩ := spec_existsA + (lookup_extra_relA J.j.1.st J.j.1.cv hth buf (ipcBufOfA false x) hBV info mi hn hn3) + rw [er2, bind_ok] + cases r2 with + | Err f => + obtain ⟨f', hrun, corr⟩ := hx + dsimp only + refine faultCase f f' corr ?_ + unfold frontSpecA + rw [run_bind_okA lcs] + dsimp only + rw [run_bind_okA (run_liftEA eIB), run_bind_errorA hrun] + | Ok e => + obtain ⟨excaps, hrun, hok⟩ := hx + dsimp only + have elen : (if info.length > msg.N_MSG_REGISTERS then + if buf = 0#u64 then ok msg.N_MSG_REGISTERS else ok info.length + else ok info.length) = ok (msgLenA info buf) := by + unfold msgLenA; split <;> (try split) <;> rfl + rw [elen, bind_ok] + obtain ⟨⟨r0, s0, a0⟩, edec0, -⟩ := spec_existsA (P0.syscall.decode_invocation.p0 st ar s a info.label + (msgLenA info buf) r1.slot (capA st s r1.slot) blocking calling donate false buf e L.total hA) + by_cases hbad : ∃ x, r0 = .Err x ∧ x ≠ error.Exc.Preempted + · obtain ⟨ex, rfl, hp⟩ := hbad + rw [edec0, bind_ok] + simp only [uncurry_apply_pair] + have E := hErr info buf r1 e ex s0 a0 einfo ebuf elr er2 edec0 hp + cases ex with + | Preempted => exact absurd rfl hp + | Sys e2 => + dsimp only at E ⊢ + refine WP.spec_mono E ?_ + rintro r ⟨e1, τ, mem, R'⟩ + exact ⟨.ok (), τ, mem, e1, R'⟩ + | Fault f => + dsimp only at E ⊢ + refine WP.spec_mono E ?_ + rintro r ⟨e1, τ, mem, R'⟩ + exact ⟨.ok (), τ, mem, e1, R'⟩ + | Lookup f => + dsimp only at E ⊢ + refine WP.spec_mono E ?_ + rintro r ⟨e1, τ, mem, R'⟩ + exact ⟨.ok (), τ, mem, e1, R'⟩ + obtain ⟨Out, hS, hSpec⟩ := hScope info buf r1 e einfo ebuf elr er2 r0 s0 a0 edec0 + (fun x h => by_contra fun hx => hbad ⟨x, h, hx⟩) + obtain ⟨r, s', a', σ', edec, hOut, hcw, R, hclk⟩ := J.dispatchA (hCnt := hCnt) hA hM hPl hfm hS + rw [edec, bind_ok] + simp only [uncurry_apply_pair] + obtain ⟨args, gargs⟩ : ∃ args, getMrs (Z := Unit) (specPtr t) (ipcBufOfA false x) mi σ = ({(args, σ)}, False) := + ⟨_, getMrs_eqA hk _ hBV.al mi⟩ + -- the spec's run of a decoded operation + have decoded : ∀ done, (done = true ∧ r = .Ok ()) ∨ (done = false ∧ r = .Err error.Exc.Preempted) → + ∃ oper σ1, setThreadState (Z := Unit) (specPtr t) .restart σ = ({((), σ1)}, False) ∧ + PerfSpecA blocking calling donate done oper (specPtr t) σ1 σ' ∧ (done = false → PreExitA σ') ∧ + ∀ (e : Except Unit Unit) (τ : Fl4SpecA64.State Unit), + (e, τ) ∈ ((do + let reply ← fl4PerformInvocation (Z := Unit) blocking calling donate oper + withoutPreemption <| epiSpecA calling (specPtr t) reply : PMonad Unit Unit).run σ1).1 → + (e, τ) ∈ ((fl4HandleInvocationR (Z := Unit) rep calling blocking donate false + (Fl4SpecA64.toBl cptr.bv)).run σ).1 := by + intro done hd + have gmi' : getMessageInfo (Z := Unit) σ.curThread σ = ({(mi, σ)}, False) := by rw [ct]; exact gmi + have gargs' : getMrs (Z := Unit) σ.curThread (ipcBufOfA false x) mi σ = ({(args, σ)}, False) := by + rw [ct]; exact gargs + have hcr' : CapRelA (capA st s r1.slot) c := by rw [hsl, ← capAtA_eq]; exact hcr + have front : (frontSpecA (specPtr t) (Fl4SpecA64.toBl cptr.bv) mi).run σ = + ({(.ok (sl, c, excaps, ipcBufOfA false x), σ)}, False) := by + unfold frontSpecA + rw [run_bind_okA lcs] + dsimp only + rw [run_bind_okA (run_liftEA eIB), run_bind_okA hrun] + rfl + have front' : (frontSpecA σ.curThread (Fl4SpecA64.toBl cptr.bv) mi).run σ = + ({(.ok (sl, c, excaps, ipcBufOfA false x), σ)}, False) := by rw [ct]; exact front + obtain ⟨oper, σ1, dec, set1, perf, pre⟩ := hSpec r s' a' σ' edec hOut done hd mi sl c excaps (ipcBufOfA false x) + args gmi' front' hsl hcr' hok ⟨p, hp1, Or.inl hp⟩ hBV hgc (by rw [ct]; exact eIB) (by rw [ct]; exact hrun) gargs' + rw [ct] at set1 perf + refine ⟨oper, σ1, set1, perf, pre, fun e τ h => specHead e τ ?_⟩ + have decRun : (.ok oper, σ) ∈ ((decSpecA calling (specPtr t) (Fl4SpecA64.toBl cptr.bv) mi + (sl, c, excaps, ipcBufOfA false x)).run σ).1 := by + unfold decSpecA + dsimp only + rw [run_bind_okA (run_liftEA gargs)]; exact dec + refine syscall_memA front decRun ?_ + unfold finSpecA withoutPreemption + rw [ExceptT.run_bind] + have m1 : ((), σ1) ∈ (setThreadState (Z := Unit) (specPtr t) .restart σ).1 := + (congrArg (fun p : Set (Unit × Fl4SpecA64.State Unit) × Prop => ((), σ1) ∈ p.1) set1).mpr + (Set.mem_singleton _) + exact mem_bindA.2 ⟨(.ok (), σ1), liftE_memA m1, h⟩ + have curt : sched.cur_thread st s = ok t := ecur + cases r with + | Ok u => + cases u + replace R : A64RelS3 st AR ar hPl.ipa n Z.Cs s' a' σ' m := R + obtain ⟨oper, σ1, -, perfs, -, whole⟩ := decoded true (Or.inl ⟨rfl, rfl⟩) + unfold PerfSpecA at perfs + rw [if_pos rfl] at perfs + obtain ⟨reply, hcase⟩ := perfs + obtain ⟨Z', H', OS', U', Ac', Pp', Tb', Lv', ia', ie', N', E', Rp', Bf', stack', κ', hCs, J', B⟩ := id R + have htZ' : t ∈ Z'.T := by + have h := J'.sr.curT + rw [hcw] at h; exact h + have ct' : σ'.curThread = specPtr t := by + have h := J'.j.1.sch.curThread + rw [hcw] at h; exact h + obtain ⟨y, hy, -⟩ := J'.ep.tcb t htZ' + have code := (J'.ep.at_tcb htZ' hy).st + dsimp only + rw [eq_of_spec_fd (obj.Tcb.state.spec (L := L) s' t), bind_ok] + have getSt : getThreadState (Z := Unit) (specPtr t) σ' = ({(y.tcbState, σ')}, False) := + threadGet_eqA Tcb.tcbState hy + have finish : ∀ σ0 τ, (.ok reply, σ0) ∈ ((fl4PerformInvocation (Z := Unit) blocking calling donate + oper).run σ1).1 → ((), τ) ∈ (epiSpecA calling (specPtr t) reply σ0).1 → + (.ok (), τ) ∈ ((fl4HandleInvocationR (Z := Unit) rep calling blocking donate false + (Fl4SpecA64.toBl cptr.bv)).run σ).1 := fun σ0 τ perf h => by + refine whole _ _ ?_ + rw [ExceptT.run_bind] + exact mem_bindA.2 ⟨(.ok reply, σ0), perf, liftE_memA h⟩ + by_cases hR : L.mem s' (stA t) = sched.TS_RESTART + · rw [if_pos hR] + have yr : y.tcbState = .restart := by + rw [hR, ts_vals.2.1] at code + cases e : y.tcbState <;> rw [e] at code <;> first | rfl | (simp [Notif.tsCode] at code) + obtain ⟨perf, empty⟩ : (.ok reply, σ') ∈ ((fl4PerformInvocation (Z := Unit) blocking calling donate + oper).run σ1).1 ∧ ∀ y, σ'.kheap (specPtr t) = some (.tcb y) → y.tcbState = .restart → reply = [] := by + rcases hcase with h | ⟨σ2, -, hep⟩ + · exact h + · exact absurd yr (epiSpec_notRestartA hep y hy) + have er := empty y hy yr + obtain ⟨s2, τ, e2, eRun, mem, R2⟩ := A64RelS3.epilogueA R calling hP1 (ct' ▸ hy) yr + rw [hcw] at e2 eRun R2 + unfold epiRegsA at e2 + rw [show (if calling = true then msg.reply_success_empty st s' t else ok s') = ok s2 from e2, bind_ok, + eRun, bind_ok, WP.spec_ok] + refine ⟨.ok (), τ, finish σ' τ perf ?_, rfl, R2⟩ + unfold epiSpecA + rw [nd_bind_oneA getSt, yr, er] + rw [ct'] at mem + exact mem + · rw [if_neg hR, WP.spec_ok] + have yr : y.tcbState ≠ .restart := fun h => by + rw [h] at code + exact hR (UScalar.eq_of_val_eq (by rw [code, ts_vals.2.1]; rfl)) + refine ⟨.ok (), σ', ?_, rfl, R⟩ + rcases hcase with ⟨perf, -⟩ | ⟨σ2, perf2, hep⟩ + · refine finish σ' σ' perf ?_ + unfold epiSpecA + rw [nd_bind_oneA getSt] + cases e : y.tcbState <;> first | exact absurd e yr | exact Set.mem_singleton _ + · exact finish σ2 σ' perf2 hep + | Err ex => + by_cases hp : ex = error.Exc.Preempted + · subst hp + obtain ⟨oper, σ1, -, perf, pre, whole⟩ := decoded false (Or.inr ⟨rfl, rfl⟩) + unfold PerfSpecA at perf + rw [if_neg Bool.false_ne_true] at perf + dsimp only + rw [WP.spec_ok] + refine ⟨.error (), σ', whole _ _ ?_, rfl, R, pre rfl, hclk rfl⟩ + rw [ExceptT.run_bind] + exact mem_bindA.2 ⟨(.error (), σ'), perf, Set.mem_singleton _⟩ + · have E := hErr info buf r1 e ex s' a' einfo ebuf elr er2 edec hp + cases ex with + | Preempted => exact absurd rfl hp + | Sys e2 => + dsimp only at E ⊢ + refine WP.spec_mono E ?_ + rintro r ⟨e1, τ, mem, R'⟩ + exact ⟨.ok (), τ, mem, e1, R'⟩ + | Fault f => + dsimp only at E ⊢ + refine WP.spec_mono E ?_ + rintro r ⟨e1, τ, mem, R'⟩ + exact ⟨.ok (), τ, mem, e1, R'⟩ + | Lookup f => + dsimp only at E ⊢ + refine WP.spec_mono E ?_ + rintro r ⟨e1, τ, mem, R'⟩ + exact ⟨.ok (), τ, mem, e1, R'⟩ + +/-- **Send, NBSend and Call** (module docstring): `dispatch_syscall` with `after_invocation`, from the invocation +wrapper's outcome, whose relation `InvRel e` depends on the spec outcome (`A64RelES3`: `A64RelPS3` after a preemption); +`hPre` runs from `InvRel (.error ())`. -/ +theorem sendBody_of_invocationA {S A : Type} {st : machine.Store S} [L : LawfulStore st] {ar : machine.Arch A} + {InvRel : Except Unit Unit → S → A → Fl4SpecA64.State Unit → Prop} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {rep : Bool} + {pending : SMonad Unit Unit} (call : Fl4SpecA64.Syscall) + (hc : call = .sysSend ∨ call = .sysNBSend ∨ call = .sysCall) {t : U64} {x : Fl4SpecA64.Tcb} + (hct : sched.cur_thread st s = ok t) (hcur : σ.curThread = specPtr t) (hk : σ.kheap (specPtr t) = some (.tcb x)) + (cap : x.tcbArch.tcbContext.userRegs cap_register = (L.mem s (regWA t layout.reg.CAP)).bv) + (reply : x.tcbArch.tcbContext.userRegs replyRegister = (L.mem s (regWA t layout.reg.REPLY)).bv) + (dest : x.tcbArch.tcbContext.userRegs nbsendRecvDest = (L.mem s (regWA t layout.reg.NBSEND_RECV_DEST)).bv) + (inv : ∀ calling blocking donate, + DispatchInvokeAtA (L.mem s (regWA t layout.reg.CAP)) (L.mem s (regWA t layout.reg.REPLY)) + (L.mem s (regWA t layout.reg.NBSEND_RECV_DEST)) call calling blocking donate false + (L.mem s (regWA t layout.reg.CAP)) → + syscall.handle_invocation st ar s a calling blocking donate false (L.mem s (regWA t layout.reg.CAP)) ⦃ r => + ∃ e τ, (e, τ) ∈ ((fl4HandleInvocationR (Z := Unit) rep calling blocking donate false + (Fl4SpecA64.toBl (L.mem s (regWA t layout.reg.CAP)).bv)).run σ).1 ∧ + r.1 = (match e with | .ok _ => .Ok () | .error _ => .Err error.Exc.Preempted) ∧ InvRel e r.2.1 r.2.2 τ ⦄) + (hPre : ∀ calling blocking donate s' a' τ, + DispatchInvokeAtA (L.mem s (regWA t layout.reg.CAP)) (L.mem s (regWA t layout.reg.REPLY)) + (L.mem s (regWA t layout.reg.NBSEND_RECV_DEST)) call calling blocking donate false + (L.mem s (regWA t layout.reg.CAP)) → + syscall.handle_invocation st ar s a calling blocking donate false (L.mem s (regWA t layout.reg.CAP)) = + ok (.Err error.Exc.Preempted, s', a') → InvRel (.error ()) s' a' τ → + syscall.after_invocation st ar s' a' (.Err error.Exc.Preempted) ⦃ r => + r.1 = false ∧ StepPostA (InvRel (.ok ())) (fl4PreemptionPath pending) τ r.2 ⦄) : + syscall.dispatch_syscall st ar s a (syscallNumberA call) ⦃ r => + StepPostA (InvRel (.ok ())) (fl4SettleDispatch pending (fl4DispatchSyscall (fl4HandleInvocationR (Z := Unit) rep) call)) σ r ⦄ := by + have same : ∀ calling blocking donate first (cptr : U64), + DispatchInvokeAtA (L.mem s (regWA t layout.reg.CAP)) (L.mem s (regWA t layout.reg.REPLY)) + (L.mem s (regWA t layout.reg.NBSEND_RECV_DEST)) call calling blocking donate first cptr → + first = false ∧ cptr = L.mem s (regWA t layout.reg.CAP) := by + intro calling blocking donate first cptr h + rcases hc with rfl | rfl | rfl <;> cases h <;> exact ⟨rfl, rfl⟩ + refine dispatchSyscall_refinesA (InvRel := InvRel) call hct ?_ hcur hk cap reply dest + refine { inv := ?_, done := ?_, preempt := ?_, combined := ?_, recv := ?_, yield := ?_ } + · intro calling blocking donate first cptr h + obtain ⟨rfl, rfl⟩ := same _ _ _ _ _ h + exact inv _ _ _ h + · intro calling blocking donate first cptr s' a' τ _ _ _ _ R; exact R + · intro calling blocking donate first cptr s' a' τ h e _ R + obtain ⟨rfl, rfl⟩ := same _ _ _ _ _ h + exact hPre _ _ _ s' a' τ h e R + · intro cptr rr s' a' τ h _ _ _ _ + exact absurd (same _ _ _ _ _ h).1 (by simp) + · intro b rr h + rcases hc with rfl | rfl | rfl <;> cases h + · intro h + rcases hc with rfl | rfl | rfl <;> cases h + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/Stage3YieldBodyA.lean b/proofs/Fl4ProofsA64/P3/Stage3YieldBodyA.lean new file mode 100644 index 000000000..f163d8816 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/Stage3YieldBodyA.lean @@ -0,0 +1,154 @@ +/- +P3 (aarch64, A64-P3 Stage 3, line yield): **the Yield syscall body**, in the shape of `DispatchBranchesA.yield` +(`OuterEntryA`). + +* `fl4HandleYield_chargedA`: from a state whose current thread is `schedulable`, `fl4HandleYield` leaves no consumed + time and a pending `chooseNewThread`, with the current SC, the time and the last observation kept (the charge, + `fl4ChargeBudget_doneA` at `canTimeout = false`, then the SC's consumed-time store, which writes only the heap). +* `bodyOKA_chargedA`: `BodyOKA` after such a charge from the entry's `validTime` and `scNotIdle`. +* `JStage3A.curScNotIdleSA`: the current SC of a `schedulable` current thread is not the idle SC. +* **`yieldBodyA`**: `syscall.handle_yield` ends in `FaultRelA` and `BodyOKA`, by `JStage3A.yieldA`. +* **`yieldBranchA`**: the `DispatchBranchesA.yield` field, for any end relation `Rel` the front derives from them. + +Named premises (all inherited from `JStage3A.yieldA` / the budget path; none new): [J] `J`, `noSwitch`, `I`, `Idle`, +`V`, `En`, `hrun` (the syscall-entry record after `budget_ok`, as `JStage3A.timestampA` returns them), [J] `T` +(timeout leaf, read for validity only), [S] `hcore`, `hni`, [P] `hag`, `hMin`, `hLimit`, `hA`, `clk`. +New module. +-/ +import Fl4ProofsA64.P3.Stage3KEntryChargeA +import Fl4ProofsA64.P3.Stage3KEntryFaultA +import Fl4ProofsA64.P3.Stage3BudgetFalseA +import Fl4ProofsA64.P3.Stage3KEntryRestartA +import Fl4ProofsA64.P3.Stage3ScCfgSpecA +import Fl4ProofsA64.P3.OuterEntryA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.OuterEntry Fl4SpecA64 Fl4Spec Classical + +noncomputable section + +/-- An `update_sched_context` changes only the heap. -/ +theorem updateSchedContext_heapA {σ τ : State Unit} {p : ObjRef} {f : SchedContext → SchedContext} + (h : ((), τ) ∈ (updateSchedContext (Z := Unit) p f σ).1) : ∃ k, τ = { σ with kheap := k } := by + obtain ⟨y1, hy1, h⟩ := h + have hy1' : y1 ∈ (NonDet.assertOpt (σ.kheap p) σ).1 := hy1 + cases hk : σ.kheap p with + | none => rw [hk] at hy1'; exact absurd hy1' (Set.notMem_empty _) + | some ko => + rw [hk] at hy1' + have e1 : y1 = (ko, σ) := hy1' + subst e1 + cases ko with + | schedContext sc n => + obtain ⟨ko', hko, hr2⟩ := setObject_memA h + exact ⟨_, hr2⟩ + | _ => exact absurd h (Set.notMem_empty _) + +/-- **`fl4HandleYield` from a `schedulable` current thread charges** (module docstring). -/ +theorem fl4HandleYield_chargedA {σ τ : State Unit} (h : schedulable σ.curThread σ = true) + (m : ((), τ) ∈ (fl4HandleYield (Z := Unit) σ).1) : ChargedA σ τ := by + unfold fl4HandleYield at m + rw [nd_gets_bindSc] at m + obtain ⟨⟨sc, σ1⟩, h1, m⟩ := mem_bindA.1 m + have e1 : σ = σ1 := getSchedContext_eqPres _ σ _ h1 + subst e1 + rw [nd_gets_bindSc] at m + obtain ⟨⟨hd, σ2⟩, h2, m⟩ := mem_bindA.1 m + have e2 : σ = σ2 := getRefillHead_eqPresA _ σ _ h2 + subst e2 + obtain ⟨⟨⟨⟩, σ3⟩, h3, m⟩ := mem_bindA.1 m + have C := fl4ChargeBudget_doneA h h3 + obtain ⟨k, hk⟩ := updateSchedContext_heapA m + subst hk + exact ⟨C.used, C.act, C.csc, C.time, C.lmt⟩ + +/-- **The body-end facts after a charge** (module docstring). -/ +theorem bodyOKA_chargedA {σ τ : State Unit} {m : McState} (V : ValidMachineTimeA σ) + (N : σ.curSc ≠ idleScPtr → NotIdleScA m σ.curSc) (C : ChargedA σ τ) : BodyOKA τ m := by + refine ⟨⟨?_, fun y n _ _ h => ?_, fun h => ?_⟩, fun t e => ?_, fun h => ?_, fun y n _ _ _ h => absurd C.used h⟩ + · unfold ValidMachineTimeA at V ⊢ + rw [C.time, C.lmt]; exact V + · rcases h with h | h + · exact absurd C.used h + · rw [C.act] at h; cases h + · rw [C.act] at h; cases h + · rw [C.act] at e; cases e + · rw [C.csc] at h ⊢; exact N h + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {nA : U64} {s : S} {a : A} {σ : State Unit} {m : McState} + +/-- **The current SC is not the idle SC** at a `schedulable` current thread: its SC is bound to the thread, and the +idle SC to the idle thread. -/ +theorem JStage3A.curScNotIdleSA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar nA)) s a σ m) + (I : CurInvA σ) (sch : schedulable σ.curThread σ = true) : σ.curSc ≠ idleScPtr := fun e => by + obtain ⟨t', hkt, hB, -⟩ := I.tcb + obtain ⟨z, j, hz, ho⟩ := J.sr.inv.back _ _ _ hkt hB + obtain ⟨xi, yi, ki, -, -, hyi, hti⟩ := J.sinv.idleSc + rw [← e, hz] at hyi + cases hyi + have eq : σ.curThread = σ.idleThread := Option.some.inj (ho.symm.trans hti) + obtain ⟨-, nrel, xr, hxr, hrunr⟩ := schedulable_actA sch + obtain ⟨xi', hxi, hsi⟩ := J.sinv.idleTcb + rw [eq] at hxr + have := KernelObject.tcb.inj (Option.some.inj (hxr.symm.trans hxi)) + subst this + rw [hsi] at hrunr + cases hrunr + +set_option maxHeartbeats 4000000 in +/-- **The Yield body** (module docstring): `handle_yield` at the dispatch point's record ends in the record and +`BodyOKA`. The hypotheses are the facts `JStage3A.timestampA` returns at the state after a `true` `budget_ok`. -/ +theorem yieldBodyA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar nA)) s a σ m) + (noSwitch : ∀ u, σ.schedulerAction ≠ .switchThread u) (I : CurInvA σ) (Idle : ActIdleA σ) + (V : ValidMachineTimeA σ) + (hcore : L.mem s (scCoreA (L.cscW s)) = L.cpu s) (hni : σ.curSc ≠ idleScPtr → NotIdleScA m σ.curSc) + {Clock : A → MachineState → Prop} (clk : Clock a σ.machineState) + (T : EntryValidFromA (st := st) Z m κ Clock s σ) + (hag : fl4MinBudget = MIN_BUDGET) (hMin : Fl4MinBudgetAgreeA ar a) + (hLimit : sporadic.max_release_time ar a ⦃ v => v.bv = fl4MaxReleaseTime ⦄) + (hA : P0.ArchTotal ar) (En : ChargeEntryA st Ac s) + (hrun : Fl4ProofsA64.P1.Runnable (L.mem s (stA (L.curW s)))) : + syscall.handle_yield st ar s a ⦃ r => StepPostA (fun s' a' τ => + FaultRelA st ar AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ nA m s' a' τ ∧ BodyOKA τ m) + (fl4HandleYield (Z := Unit)) σ r ⦄ := by + have sch : schedulable σ.curThread σ = true := J.curSchedulableA En hrun + refine WP.spec_mono (J.yieldA noSwitch I Idle hcore hni (J.curScNotIdleSA I sch) clk T hag hMin hLimit hA En hrun) ?_ + rintro ⟨s', a'⟩ ⟨τ, he, -, Q, -⟩ + have mem : ((), τ) ∈ (fl4HandleYield (Z := Unit) σ).1 := by rw [he]; exact Set.mem_singleton _ + exact ⟨τ, mem, ⟨stack, Q⟩, bodyOKA_chargedA V hni (fl4HandleYield_chargedA sch mem)⟩ + +/-- **`DispatchBranchesA.yield`**: for an end relation `Rel` implied by the record and `BodyOKA`. -/ +theorem yieldBranchA {Rel : S → A → State Unit → Prop} + (hRel : ∀ s' a' τ, FaultRelA st ar AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ nA m s' a' τ → + BodyOKA τ m → Rel s' a' τ) + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ fl4A64Platform.numNodes (J5A st) + (J7A st (J7R2A st ar nA)) s a σ m) + (noSwitch : ∀ u, σ.schedulerAction ≠ .switchThread u) (I : CurInvA σ) (Idle : ActIdleA σ) + (V : ValidMachineTimeA σ) + (hcore : L.mem s (scCoreA (L.cscW s)) = L.cpu s) (hni : σ.curSc ≠ idleScPtr → NotIdleScA m σ.curSc) + {Clock : A → MachineState → Prop} (clk : Clock a σ.machineState) + (T : EntryValidFromA (st := st) Z m κ Clock s σ) + (hag : fl4MinBudget = MIN_BUDGET) (hMin : Fl4MinBudgetAgreeA ar a) + (hLimit : sporadic.max_release_time ar a ⦃ v => v.bv = fl4MaxReleaseTime ⦄) + (hA : P0.ArchTotal ar) (En : ChargeEntryA st Ac s) + (hrun : Fl4ProofsA64.P1.Runnable (L.mem s (stA (L.curW s)))) {call : Syscall} (_ : call = .sysYield) : + syscall.handle_yield st ar s a ⦃ r => StepPostA Rel (fl4HandleYield (Z := Unit)) σ r ⦄ := + WP.spec_mono (yieldBodyA J noSwitch I Idle V hcore hni clk T hag hMin hLimit hA En hrun) fun r ⟨τ, mem, F, B⟩ => + ⟨τ, mem, hRel _ _ _ F B⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/StateRelA.lean b/proofs/Fl4ProofsA64/P3/StateRelA.lean new file mode 100644 index 000000000..74527c655 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/StateRelA.lean @@ -0,0 +1,256 @@ +/- +P3 (aarch64, A64-P3 S3 gens 1–2): **the CSpace and CDT parts of the A64 state relation** — the twins of x86's +`P3/StateRel` (`TypedHeap`, `StateRel`), `P3/Mdb` (`MdbGhost`, `AbsSlot`, `CdtRel`) and the placement part of +`P3/Layout`, between FL4's AArch64 memory (a `LawfulStore` view `mem : U64 → U64`) and `Fl4SpecA64.State Unit`. + +* `TypedHeapA` (x86 `TypedHeap`): the ghost kinds of the objects (CNodes of a radix, TCBs, `other` of a byte size). +* `capAtA mem a` (x86 `capAt`): the cap the slot at `a` holds, decoded by A64 P1's total `CapRt.decodeF` (so it is + A64 P1's `capA st s a` at `mem = L.mem s`, definitionally). +* **`StateRelA th mem σ`** (x86 `StateRel`): every CNode of `th` is a well-formed `cnode r cs` of the spec at the same + pointer, slot `idx` the 32-byte slot at `p + 32 · of_bl idx`, `CapRelA`-related; every TCB of `th` is a spec TCB + whose **seven** cap slots (`tcb_cnode_map` with the `ext` hook's slots 5 and 6, `fl4TcbSlotCount`) are related to + FL4's seven (`layout.tcb::SLOT_*`, at `t + 32 i`). x86 has six. +* `MdbGhostA` (x86 `MdbGhost`): chains of slot addresses in `NEXT` order and the parent map. **`CdtRelA st th s σ G`** + (x86 `CdtRel`, plus x86 `MdbLinks`): **A64 P1's `TreeA` directly** (Architect's ruling 08:46Z item 4) over the slot + set `{a | IsSlotA th a}` with the ghost's chains and parent map (`MdbLk`: the `PREV` / `NEXT` link words and the + members, `Mdb.MdbOK`, `UtInvA`), the spec's `cdt` is the ghost parent map through `cslotAddrA`, `is_original_cap` + is the slot's `MDB_REVOCABLE` bit (`NEXT` word bit 48, `obj.rs`). `capAtA (L.mem s) = capA st s` by `rfl`. +* **`PlaceA th ksb`** (x86 `Placed` with `TopPlaced.tko` and `IntLayout.node`): `th`'s objects aligned to their + size, inside the address space, pairwise apart and (gen 3, relaxed by the ruling of 09:59Z item 2) apart from the + kernel-state block `[ksb, ksb + 788736)` (`ks::SIZE` on AArch64, 128 nodes; A64-Boot's `BootLay`) **unless they are + IRQ-node slots** (`IrqNodeA`), and FL4's 5376 IRQ-node slots `ksb + 43232 + 32 i` are one-slot CNodes of `th` + (`irq`), so they join `th` and the CSpace / CDT relations cover them. + +**S3 gen 18 (ruling 18:13Z items 2, 4).** `PlaceA.nz`: no object of `th` is at address 0 (gen 16's decode-site +`hnz`, now `PlaceA.slot_ne0`). `CapValidA` gains a frame arm: a mapped plain frame cap's address is `VaOkE` (a Stage-1 +or a high-window address; moved here from `P3/ExtVsTopA`), so `JAtZA.cv` carries ExtVsA gen 3's `hVa`. + +Pointers are named by `specPtr` (gen 12, ruling 15:01Z item 2: every object kind shares the shift of `P3/PtrA`; +x86 names them by the identity). No named premise; new definitions, `capAtA_eq` and the boot-form +lemma `CdtRelA.empty` only. +-/ +import Fl4ProofsA64.P3.CapRelA +import Fl4ProofsA64.P1.MdbTreeA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## The typed heap and slots -/ + +/-- The kind of an object at a pointer (x86 `ObjKind`). -/ +inductive ObjKindA where + | cnode (radix : Nat) + | tcb + | other (size : Nat) +deriving DecidableEq + +/-- Ghost typed heap (x86 `TypedHeap`). -/ +abbrev TypedHeapA := U64 → Option ObjKindA + +/-- An object's size in bytes: a CNode of radix `r` has `2^r` 32-byte slots; an AArch64 TCB is 4 KiB (FL4 +`bits::TCB = 12`, `Ext/TcbSize`). -/ +def ObjKindA.size : ObjKindA → Nat + | .cnode r => 2 ^ (r + 5) + | .tcb => 4096 + | .other z => z + +/-- Address of slot `i` of the object the spec names `p` (`Slot::index`, `Tcb::slot`: `p + 32 i`, wrapping; gen 12: +`p` is the spec name `specPtr q` of the FL4 object `q`, so the slot is `q + 32 i`, `slotAddrA_sp`). -/ +def slotAddrA (p i : BitVec 64) : U64 := ⟨p - pptrShiftA + i * 32#64⟩ + +/-- (gen 12) The slot address of a spec-named FL4 object (`specPtr`) is FL4's `q + 32 i`. -/ +theorem slotAddrA_sp (q : U64) (i : BitVec 64) : slotAddrA (specPtr q) i = ⟨q.bv + i * 32#64⟩ := by + unfold slotAddrA; rw [specPtr_def, BitVec.add_sub_cancel] + +/-- The address of an abstract slot (x86 `cslotAddr`). -/ +def cslotAddrA (sl : Fl4SpecA64.CslotPtr) : U64 := slotAddrA sl.1 (Fl4SpecA64.ofBl sl.2) + +/-- The cap the slot at `a` holds (x86 `capAt`; A64 P1's `capA`). -/ +def capAtA (mem : U64 → U64) (a : U64) : capability.Cap := + Fl4ProofsA64.P1.CapRt.decodeF (mem a).bv (mem (wa a 8#u64)).bv + +/-- The slot's `MDB_REVOCABLE` bit (`obj::Slot::revocable`: bit 48 of the `NEXT` word at `+24`). -/ +def revA (mem : U64 → U64) (a : U64) : Bool := (mem (wa a 24#u64) &&& 281474976710656#u64) != 0#u64 + +/-- FL4's number of TCB cap slots on AArch64 (`layout.tcb::NUM_SLOTS`; the spec's `fl4TcbSlotCount`). -/ +def tcbSlotsA : Nat := 7 + +/-- The slot at `a` holds a cap related to the abstract slot contents (x86 `SlotRel`). -/ +def SlotRelA (mem : U64 → U64) (a : U64) (oc : Option Fl4SpecA64.Cap) : Prop := + ∃ ac, oc = some ac ∧ CapRelA (capAtA mem a) ac + +/-- A slot address of an object of `th` (x86 `IsSlot`). -/ +def IsSlotA (th : TypedHeapA) (a : U64) : Prop := + ∃ p, (∃ r, th p = some (.cnode r) ∧ ∃ i < 2 ^ r, a = slotAddrA (specPtr p) (BitVec.ofNat 64 i)) ∨ + (th p = some .tcb ∧ ∃ i < tcbSlotsA, a = slotAddrA (specPtr p) (BitVec.ofNat 64 i)) + +/-- A slot of the typed heap, as the spec names it (x86 `AbsSlot`). -/ +def AbsSlotA (th : TypedHeapA) (sl : Fl4SpecA64.CslotPtr) : Prop := + ∃ p, sl.1 = specPtr p ∧ (th p = some (.cnode sl.2.length) ∨ + (th p = some .tcb ∧ ∃ i, i < tcbSlotsA ∧ sl.2 = Fl4SpecA64.tcbCnodeIndex i)) + +/-- The TCB slot indices are distinct (seven of FL4's, in a 3-bit index). -/ +theorem tcbIdx_inj : ∀ i < 8, ∀ j < 8, Fl4SpecA64.tcbCnodeIndex i = Fl4SpecA64.tcbCnodeIndex j → i = j := by + decide + +theorem tcbIdx_eq {i j : Nat} (hi : i < 8) (hj : j < 8) : + Fl4SpecA64.tcbCnodeIndex i = Fl4SpecA64.tcbCnodeIndex j ↔ i = j := + ⟨tcbIdx_inj i hi j hj, fun h => h ▸ rfl⟩ + +/-- A TCB slot index read back as a slot number. -/ +theorem ofBl_tcbIdx : ∀ i < 8, (Fl4SpecA64.ofBl (Fl4SpecA64.tcbCnodeIndex i) : BitVec 64) = BitVec.ofNat 64 i := by + decide + +/-- **The address of a mapped plain frame cap** (ExtVsA gen 3, moved here at S3 gen 18 from `P3/ExtVsTopA`): a +Stage-1 address (`va ≤ USER_TOP`, `consts.rs:129`) or one in the high window (`HIGH_VSPACE_BASE ≤ va < +HIGH_VSPACE_TOP`, `consts.rs:265`), as FL4's map checks write it (`decode_page_map`'s `last > USER_TOP`, +`decode_high_page_map`'s window check). Part of `CapValidA` (S3 gen 18), so `JAtZA.cv` carries it for every slot. -/ +abbrev VaOkE (va : U64) : Prop := + va.val ≤ 0x7fffffffffff ∨ (0xffffffe000000000 ≤ va.val ∧ va.val < 0xfffffffc00000000) + +/-- (gen 9) **A cap names the typed-heap object the CSpace walk reads** (x86 `CapValid`'s CNode / thread part): a +CNode cap names a CNode of `th` of its radix, and the radix is at least 1 (as on x86: the walk and the spec disagree +only on a radix-0, guard-0 CNode cap; FL4's radix-0 CNodes are the IRQ-node slots, which no CNode cap names); a +thread cap names a TCB of `th`; (S3 gen 18, ruling 18:13Z item 4) a mapped plain frame cap's address is +`VaOkE` (the frame half of the `ext` integration: FL4's `unmap_page` walk for it is the spec's, `ExtVsTopA`). -/ +def CapValidA (th : TypedHeapA) : capability.Cap → Prop + | .CNode p r _ _ => th p = some (.cnode r.val) ∧ r.val ≠ 0 + | .Thread p => th p = some .tcb + | .Frame _ _ _ _ m ept _ va => m = true → ept = false → VaOkE va + | _ => True + +/-! ## The CSpace relation -/ + +/-- **The CSpace part of the state relation** (module docstring; x86 `StateRel`). -/ +structure StateRelA (th : TypedHeapA) (mem : U64 → U64) (σ : Fl4SpecA64.State Unit) : Prop where + cnode : ∀ p r, th p = some (.cnode r) → ∃ cs, σ.kheap (specPtr p) = some (.cnode r cs) ∧ + Fl4SpecA64.wellFormedCnodeN r cs ∧ + ∀ idx : List Bool, idx.length = r → SlotRelA mem (slotAddrA (specPtr p) (Fl4SpecA64.ofBl idx)) (cs idx) + tcb : ∀ t, th t = some .tcb → ∃ x, σ.kheap (specPtr t) = some (.tcb x) ∧ + ∀ i : Nat, i < tcbSlotsA → + SlotRelA mem (slotAddrA (specPtr t) (BitVec.ofNat 64 i)) (Fl4SpecA64.tcbCnodeMap x (Fl4SpecA64.tcbCnodeIndex i)) + +/-! ## The CDT relation -/ + +/-- Ghost state of the derivation tree (x86 `MdbGhost`). -/ +structure MdbGhostA where + chains : List (List U64) + par : U64 → Option U64 + +section cdt +variable {S : Type} (st : machine.Store S) [L : Fl4ProofsA64.P1.LawfulStore st] + +/-- **The CDT relation** (module docstring; x86 `CdtRel` with `MdbLinks`): A64 P1's `TreeA` over the slots of `th`. -/ +structure CdtRelA (th : TypedHeapA) (s : S) (σ : Fl4SpecA64.State Unit) (G : MdbGhostA) : Prop where + tree : Fl4ProofsA64.P1.W7.R.ST.TreeA st {a | IsSlotA th a} G.chains G.par s + cdt : ∀ sl, AbsSlotA th sl → (σ.cdt sl).map cslotAddrA = G.par (cslotAddrA sl) + cdt_slot : ∀ sl q, σ.cdt sl = some q → AbsSlotA th sl ∧ AbsSlotA th q + orig : ∀ sl, AbsSlotA th sl → σ.isOriginalCap sl = revA (L.mem s) (cslotAddrA sl) + par_slot : ∀ a b, G.par a = some b → ∃ sl, AbsSlotA th sl ∧ cslotAddrA sl = a + /-- (gen 7, ruling 12:26Z item 2) **Every ghost parent's slot is revocable** (x86 `MdbInv.orig`: a parent is an + original cap); with `orig`, every spec `cdt` parent is an original cap (`CdtRelA.po`). -/ + prev : ∀ a p, G.par a = some p → revA (L.mem s) p = true + +end cdt + +section cdt2 +variable {S : Type} {st : machine.Store S} [L : Fl4ProofsA64.P1.LawfulStore st] + +/-- (gen 7) The spec-side form of `CdtRelA.prev`: every `cdt` parent is an original cap (x86 `MdbInv.orig`). -/ +def SpecPOA (σ : Fl4SpecA64.State Unit) : Prop := + ∀ sl q, σ.cdt sl = some q → σ.isOriginalCap q = true + +/-- (gen 7) `CdtRelA.prev` read on the spec side. -/ +theorem CdtRelA.po {th : TypedHeapA} {s : S} {σ : Fl4SpecA64.State Unit} {G : MdbGhostA} + (h : CdtRelA st th s σ G) : SpecPOA σ := by + intro sl q hq + obtain ⟨hs, hq'⟩ := h.cdt_slot sl q hq + have e := h.cdt sl hs + rw [hq] at e + rw [h.orig q hq'] + exact h.prev _ _ (by rw [← e]; rfl) + +/-- (gen 7) **`CdtRelA` from its first five fields and `SpecPOA`** (`prev` read back through `cdt` / `orig`). -/ +theorem CdtRelA.mkA {th : TypedHeapA} {s : S} {σ : Fl4SpecA64.State Unit} {G : MdbGhostA} + (tree : Fl4ProofsA64.P1.W7.R.ST.TreeA st {a | IsSlotA th a} G.chains G.par s) + (cdt : ∀ sl, AbsSlotA th sl → (σ.cdt sl).map cslotAddrA = G.par (cslotAddrA sl)) + (cdt_slot : ∀ sl q, σ.cdt sl = some q → AbsSlotA th sl ∧ AbsSlotA th q) + (orig : ∀ sl, AbsSlotA th sl → σ.isOriginalCap sl = revA (L.mem s) (cslotAddrA sl)) + (par_slot : ∀ a b, G.par a = some b → ∃ sl, AbsSlotA th sl ∧ cslotAddrA sl = a) + (po : SpecPOA σ) : CdtRelA st th s σ G := by + refine ⟨tree, cdt, cdt_slot, orig, par_slot, fun a p hp => ?_⟩ + obtain ⟨sl, hsl, rfl⟩ := par_slot a p hp + have e := cdt sl hsl + rw [hp] at e + obtain ⟨q, hq, rfl⟩ := Option.map_eq_some_iff.1 e + rw [← orig q (cdt_slot sl q hq).2] + exact po sl q hq + +end cdt2 + +/-- `capAtA` at a store state is A64 P1's `capA` (definitionally). -/ +theorem capAtA_eq {S : Type} {st : machine.Store S} [L : Fl4ProofsA64.P1.LawfulStore st] (s : S) (a : U64) : + capAtA (L.mem s) a = Fl4ProofsA64.P1.W7.R.ST.capA st s a := rfl + +/-! ## Placement -/ + +/-- AArch64 `ks::SIZE` (the nodes start at 223488, 128 nodes of 4288 bytes, then the tail; A64-Boot's `BootLay`). -/ +def ksSizeA : Nat := 788736 + +/-- **The IRQ-node slots** (gen 3, ruling 09:59Z item 2): the slot of IRQ `i < 5376` at `ksb + IRQ_NODE + 32 i` +(`interrupt::irq_slot`, `ks::IRQ_NODE` = 43232; A64 P1's `IslN` / `irqSlotA`). -/ +abbrev IrqNodeA (ksb p : U64) : Prop := Fl4ProofsA64.P1.W7.R.ST.IslN ksb p + +/-- **The typed heap's placement** (module docstring; x86 `Placed` + `TopPlaced.tko` + `IntLayout.node`). -/ +structure PlaceA (th : TypedHeapA) (ksb : U64) : Prop where + align : ∀ p k, th p = some k → p.val % k.size = 0 + fit : ∀ p k, th p = some k → p.val + k.size ≤ 2 ^ 64 + apart : ∀ p q kp kq, th p = some kp → th q = some kq → p ≠ q → p.val + kp.size ≤ q.val ∨ q.val + kq.size ≤ p.val + /-- (gen 3, relaxed: ruling 09:59Z item 2) Every object but an IRQ-node slot is off the kernel-state block. -/ + ks : ∀ p k, th p = some k → ¬ IrqNodeA ksb p → p.val + k.size ≤ ksb.val ∨ ksb.val + ksSizeA ≤ p.val + /-- (gen 3) **FL4's 5376 IRQ-node slots are one-slot CNodes of `th`**, placed explicitly inside the block (x86 + `IntLayout.node`, there 128 slots at `ksb + 1248 + 32 i`). -/ + irq : ∀ i : U64, i.val < 5376 → th (Fl4ProofsA64.P1.irqSlotA ksb i) = some (.cnode 0) + /-- (S3 gen 18, ruling 18:13Z item 2; x86 `Placed`'s objects above 0) **No object of `th` is at address 0**, so + no slot is (`PlaceA.slot_ne0`; gen 16's decode-site hypothesis `hnz`). -/ + nz : ∀ p k, th p = some k → p ≠ 0#u64 + +/-- An IRQ-node slot by value (x86 `nodeA_val`). -/ +theorem irqSlotA_val {kb i : U64} (hf : kb.val + ksSizeA ≤ 2 ^ 64) (hi : i.val < 5376) : + (Fl4ProofsA64.P1.irqSlotA kb i).val = kb.val + 43232 + 32 * i.val := by + unfold Fl4ProofsA64.P1.irqSlotA + unfold ksSizeA at hf + simp [core.num.U64.wrapping_add_val_eq, core.num.U64.wrapping_mul_val_eq, U64.size_def, U64.numBits] + omega + +/-- An object of `th` at an IRQ-node slot is that one-slot CNode. -/ +theorem PlaceA.irqK {th : TypedHeapA} {ksb p : U64} {k : ObjKindA} (P : PlaceA th ksb) (hp : th p = some k) + (hn : IrqNodeA ksb p) : k = .cnode 0 := by + obtain ⟨i, hi, rfl⟩ := hn + rw [P.irq i hi] at hp + exact (Option.some.inj hp).symm + +/-! ## Boot forms -/ + +/-- **`CdtRelA` with the empty ghost**: no non-null slot of `th`, no spec `cdt` edge, no original cap and every +slot's revocable bit clear (the boot state). -/ +theorem CdtRelA.empty {S : Type} {st : machine.Store S} [L : Fl4ProofsA64.P1.LawfulStore st] {th : TypedHeapA} + {s : S} {σ : Fl4SpecA64.State Unit} + (hnull : ∀ a, IsSlotA th a → capAtA (L.mem s) a = .Null) (hcdt : ∀ sl, σ.cdt sl = none) + (horig : ∀ sl, AbsSlotA th sl → σ.isOriginalCap sl = revA (L.mem s) (cslotAddrA sl)) : + CdtRelA st th s σ ⟨[], fun _ => none⟩ where + tree := Fl4ProofsA64.P1.W7.R.ST.TreeA.empty fun a ha => hnull a ha + cdt sl _ := by rw [hcdt sl]; rfl + cdt_slot sl q h := by rw [hcdt sl] at h; cases h + orig := horig + par_slot a b e := by cases e + prev a b e := by cases e + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/SwapA.lean b/proofs/Fl4ProofsA64/P3/SwapA.lean new file mode 100644 index 000000000..69e94e1ad --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/SwapA.lean @@ -0,0 +1,484 @@ +/- +P3 (aarch64, A64-P3 S3 gen 6, ruling 11:49Z step 1): **the joint `cap_swap` and `cap_swap_for_delete` leaves: FL4's +`cdt::swap` / `cdt::cap_swap_for_delete` refine l4v `cap_swap` / `cap_swap_for_delete`** (CSpace_A, nondeterministic +spec, `Z = Unit`) between two slots of the typed heap (gen 8: CNode or TCB slots), with the whole `JAtZAX` carried and the spec invariant +`SInvXA` — the twin of x86's `P3/SwapRefine` (`swap_refines`, `CdtRel.swap`, `swap_cdt_eq`, `swap_io_eq`), on +`MoveA`'s two-slot machinery. + +* **Bit 48 exactly** (A64 P1's `cdt.swap.rvb` gives only "set ⇒ set"): **`swap_bitA`** (`swap.rvb`'s walk with + `set_nf_rvE` for the two flag writes and `MoveA.linkBitA` — the shared helper, ruling q2 — for the last two link + writes): the slots exchange their bits, every other `OS` slot keeps its own. +* **`osokA`**: an `OSOK` at the `OS` slot words (`osw`), so A64 P1's `cdt.swap.lrWF` gives `swap`'s write set as + `OS` slot words (the set `footN_offA` takes). +* Spec side: `swapFinA` / **`capSwap_memA`** (an outcome of `cap_swap`, x86 `capSwap_eq`), `swap_cdt_eqA` / + `swap_io_eqA` (x86 `swap_cdt_eq` / `swap_io_eq`), `SpecSlotA.swFin`. +* **`CdtRelA.swapA`** (x86 `CdtRel.swap`): the spec's CDT and original-cap map renamed by the transposition of the + two slots against the ghost renamed by the transposition of their addresses (`swGA`, A64 P1's `cdt.swap.tree`). +* **`cap_swap_refinesA`** (x86 `swap_refines`): two distinct non-null canonical slots, related encodable + non-null new caps alike the replaced ones ⇒ ∃ outcome of `cap_swap` with `StateRelA`, `CdtRelA` and P1's tree at + `swGA`, a slot-only spec step, and every slot unchanged or one of the two new caps. +* **`cap_swap_jA`**: at `JEntA`, the whole `JAtZAX` at `swGA` across `swap` (`JAtZAX.of_slotWrites`, `footN_offA`), + given the new caps name counted objects (`CapObjOkA`), and `SInvXA` across the spec step when the caps may go + where they are written (`CapOkA`, `SInvXA.capSwap`). +* **`cap_swap_for_delete_jA`**: the same for `cap_swap_for_delete` of two non-null canonical slots (equal slots: + both sides keep the state, `swGA_self`), the caps the slots' own (`getCap_cnode_memA`), `SInvXA` for slots off the + IRQ node (`SInvXA.capSwapForDelete`). + +Gen 8: CNode or TCB slots (`AbsSlotA`, `StateRelA.setAbsA` / `getAbsA`). Named premise: none (gen 7: `SlotApA` is `JEntA.ap`, no longer a premise; `CdtRelA.prev` kept, gen 7). +-/ +import Fl4ProofsA64.P3.MoveA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {T Ω Sg OS : Set U64} + +/-- An `OSOK` at the `OS` slot words (A64 P1's `osw`): P1's write-frame lemmas then give `OS` slot words. -/ +theorem osokA (hO : OSOK T Ω Sg OS) : OSOK T {b | InOS OS b} ∅ OS := + ⟨fun _ hx => osw hO hx, hO.fit, hO.disj, hO.nz⟩ + +set_option maxHeartbeats 4000000 in +/-- **`swap` and bit 48, exactly** (module docstring; A64 P1's `cdt.swap.rvb` walk): the two slots exchange their +bits, every other `OS` slot keeps its own. Hypotheses as `cdt.swap.lrW`. -/ +theorem swap_bitA (hO : OSOK T Ω Sg OS) {s : S} {pv nx : U64 → U64} (hL : LR st OS s pv nx) {x1 x2 : U64} + {c1 c2 : capability.Cap} (hx1 : x1 ∈ OS) (hx2 : x2 ∈ OS) (hne : x1 ≠ x2) (hs1 : obj.sext48 x1 = ok x1) + (hs2 : obj.sext48 x2 = ok x2) (hw1 : CapRt.EncWf c1) (hw2 : CapRt.EncWf c2) + (hp1 : pv x1 ≠ 0#u64 → pv x1 ∈ OS) (hn1 : nx x1 ≠ 0#u64 → nx x1 ∈ OS) + (hp2 : pv x2 ≠ 0#u64 → pv x2 ∈ OS) (hn2 : nx x2 ≠ 0#u64 → nx x2 ∈ OS) : + cdt.swap st s c1 x1 c2 x2 ⦃ s' => + (∀ y ∈ OS, y ≠ x1 → y ≠ x2 → L.mem s' (wa y 24#u64) &&& RVB = L.mem s (wa y 24#u64) &&& RVB) ∧ + L.mem s' (wa x1 24#u64) &&& RVB = (if ((L.mem s (wa x2 24#u64) &&& RVB) != 0#u64) then RVB else 0#u64) ∧ + L.mem s' (wa x2 24#u64) &&& RVB = (if ((L.mem s (wa x1 24#u64) &&& RVB) != 0#u64) then RVB else 0#u64) ⦄ := by + have hS : P0.StoreTotal st := L.total + obtain ⟨a1, b1, he1, -⟩ := CapRt.decode_encode c1 hw1 + obtain ⟨a2, b2, he2, -⟩ := CapRt.decode_encode c2 hw2 + unfold cdt.swap + rw [set_cap_eq he2, bind_ok, set_cap_eq he1, bind_ok] + have L2 : LR st OS (L.wr (L.wr (L.wr (L.wr s x1 a2) (wa x1 8#u64) b2) x2 a1) (wa x2 8#u64) b1) pv nx := + (hL.wr2 hO hx1).wr2 hO hx2 + have K2 : ∀ y ∈ OS, L.mem (L.wr (L.wr (L.wr (L.wr s x1 a2) (wa x1 8#u64) b2) x2 a1) (wa x2 8#u64) b1) + (wa y 24#u64) = L.mem s (wa y 24#u64) := fun y hy => + ((set_cap_links hO hx2 y hy).2).trans ((set_cap_links hO hx1 y hy).2) + generalize L.wr (L.wr (L.wr (L.wr s x1 a2) (wa x1 8#u64) b2) x2 a1) (wa x2 8#u64) b1 = s2 at L2 K2 ⊢ + rw [slot_prev_eq s2 x1, bind_ok, (L2 x1 hx1).1, next_eq, bind_ok, (L2 x1 hx1).2, revocable_eqA, bind_ok] + refine WP.spec_bind (P0.obj.Slot.first_badged.p0 st x1 s2 hS) fun f1 _ => ?_ + simp only [obj.Slot.is_null, bind_ok] + -- stage 3: `P1.NEXT := x2` + refine WP.spec_bind (Pₘ := fun s3 => LR st OS s3 pv (LkAbs.s3 0#u64 pv nx x1 x2) ∧ + ∀ y ∈ OS, L.mem s3 (wa y 24#u64) &&& RVB = L.mem s (wa y 24#u64) &&& RVB) ?_ fun s3 h3 => ?_ + · split + · rename_i hp0 + have hP0 : pv x1 = 0#u64 := by simpa using hp0 + refine (WP.spec_ok _).2 ⟨L2.congr (fun _ _ => rfl) fun z _ => ?_, fun y hy => by rw [K2 y hy]⟩ + unfold LkAbs.s3; simp [hP0] + · rename_i hp0 + have hP0 : pv x1 ≠ 0#u64 := by simpa using hp0 + rw [set_next_eq, WP.spec_ok] + refine ⟨(L2.nw hO (hp1 hP0) (link_bits _ _) hs2).congr (fun _ _ => rfl) fun z _ => ?_, + fun y hy => by rw [bit_next hO s2 (hp1 hP0) hy x2, K2 y hy]⟩ + unfold LkAbs.s3; by_cases e : z = pv x1 <;> simp [e, hP0] + obtain ⟨L3, B3⟩ := h3 + -- stage 4: `N1.PREV := x2` + refine WP.spec_bind (Pₘ := fun s4 => LR st OS s4 (LkAbs.s4 0#u64 pv nx x1 x2) (LkAbs.s3 0#u64 pv nx x1 x2) ∧ + ∀ y ∈ OS, L.mem s4 (wa y 24#u64) &&& RVB = L.mem s (wa y 24#u64) &&& RVB) ?_ fun s4 h4 => ?_ + · split + · rename_i hn0 + have hN0 : nx x1 = 0#u64 := by simpa using hn0 + refine (WP.spec_ok _).2 ⟨L3.congr (fun z _ => ?_) (fun _ _ => rfl), B3⟩ + unfold LkAbs.s4; simp [hN0] + · rename_i hn0 + have hN0 : nx x1 ≠ 0#u64 := by simpa using hn0 + rw [set_prev_eq, WP.spec_ok] + refine ⟨(L3.prev hO (hn1 hN0) x2).congr (fun z _ => ?_) (fun _ _ => rfl), + fun y hy => by rw [w24_wr16 hO s3 (hn1 hN0) hy]; exact B3 y hy⟩ + unfold LkAbs.s4; by_cases e : z = nx x1 <;> simp [e, hN0] + obtain ⟨L4, B4⟩ := h4 + have hP2 : LkAbs.s4 0#u64 pv nx x1 x2 x2 ≠ 0#u64 → LkAbs.s4 0#u64 pv nx x1 x2 x2 ∈ OS := by + unfold LkAbs.s4; split + · exact fun _ => hx2 + · exact hp2 + have hN2 : LkAbs.s3 0#u64 pv nx x1 x2 x2 ≠ 0#u64 → LkAbs.s3 0#u64 pv nx x1 x2 x2 ∈ OS := by + unfold LkAbs.s3; split + · exact fun _ => hx2 + · exact hn2 + rw [slot_prev_eq s4 x2, bind_ok, (L4 x2 hx2).1, next_eq, bind_ok, (L4 x2 hx2).2, revocable_eqA, bind_ok] + refine WP.spec_bind (P0.obj.Slot.first_badged.p0 st x2 s4 hS) fun f2 _ => ?_ + -- stages 5–8: slot 1 and slot 2 get each other's flags, exactly + rw [set_prev_eq s4 x1 _, bind_ok] + refine WP.spec_bind (set_nf_rvE _ x1 _ _ f2) ?_ + rintro s6 ⟨w6, rfl, hw6⟩ + rw [set_prev_eq _ x2 _, bind_ok] + refine WP.spec_bind (set_nf_rvE _ x2 _ _ f1) ?_ + rintro s8 ⟨w8, rfl, hw8⟩ + rw [B4 x2 hx2] at hw6 + rw [K2 x1 hx1] at hw8 + have B8 : ∀ y ∈ OS, y ≠ x1 → y ≠ x2 → L.mem (L.wr (L.wr (L.wr (L.wr s4 (wa x1 16#u64) + (LkAbs.s4 0#u64 pv nx x1 x2 x2)) (wa x1 24#u64) w6) (wa x2 16#u64) (pv x1)) (wa x2 24#u64) w8) + (wa y 24#u64) &&& RVB = L.mem s (wa y 24#u64) &&& RVB := fun y hy e1 e2 => by + rw [w24_wr24 hO _ hx2 hy e2, w24_wr16 hO _ hx2 hy, w24_wr24 hO _ hx1 hy e1, w24_wr16 hO _ hx1 hy] + exact B4 y hy + have X1 : L.mem (L.wr (L.wr (L.wr (L.wr s4 (wa x1 16#u64) (LkAbs.s4 0#u64 pv nx x1 x2 x2)) (wa x1 24#u64) w6) + (wa x2 16#u64) (pv x1)) (wa x2 24#u64) w8) (wa x1 24#u64) &&& RVB = + (if ((L.mem s (wa x2 24#u64) &&& RVB) != 0#u64) then RVB else 0#u64) := by + rw [w24_wr24 hO _ hx2 hx1 hne, w24_wr16 hO _ hx2 hx1, w24_self hO _ hx1]; exact hw6 + have X2 : L.mem (L.wr (L.wr (L.wr (L.wr s4 (wa x1 16#u64) (LkAbs.s4 0#u64 pv nx x1 x2 x2)) (wa x1 24#u64) w6) + (wa x2 16#u64) (pv x1)) (wa x2 24#u64) w8) (wa x2 24#u64) &&& RVB = + (if ((L.mem s (wa x1 24#u64) &&& RVB) != 0#u64) then RVB else 0#u64) := by + rw [w24_self hO _ hx2]; exact hw8 + generalize L.wr (L.wr (L.wr (L.wr s4 (wa x1 16#u64) (LkAbs.s4 0#u64 pv nx x1 x2 x2)) (wa x1 24#u64) w6) + (wa x2 16#u64) (pv x1)) (wa x2 24#u64) w8 = s8 at B8 X1 X2 ⊢ + try simp only [obj.Slot.is_null, bind_ok] + -- stages 9–10: `P2.NEXT := x1`, `N2.PREV := x1` (the shared helper) + refine WP.spec_bind (linkBitA hO s8 (p := LkAbs.s4 0#u64 pv nx x1 x2 x2) (x := x1) ?_ (Or.inl rfl)) + fun s9 h9 => ?_ + · exact fun h => hP2 (by simpa using h) + refine WP.spec_mono (linkBitA hO s9 (p := LkAbs.s3 0#u64 pv nx x1 x2 x2) (x := x1) ?_ (Or.inr rfl)) + fun s10 h10 => ?_ + · exact fun h => hN2 (by simpa using h) + exact ⟨fun y hy e1 e2 => by rw [h10 y hy, h9 y hy]; exact B8 y hy e1 e2, + by rw [h10 x1 hx1, h9 x1 hx1]; exact X1, by rw [h10 x2 hx2, h9 x2 hx2]; exact X2⟩ + +end + +/-! ## The spec's outcome -/ + +/-- The state after `cap_swap`'s two `set_cap`s: its CDT and original-cap updates (x86 `capSwap_eq`'s state). -/ +def swapFinA (σ : State Unit) (s1 s2 : CslotPtr) : State Unit := + { σ with + cdt := Function.update (Function.update + (fun n => if σ.cdt n = some s1 then some s2 else if σ.cdt n = some s2 then some s1 else σ.cdt n) s1 + (if σ.cdt s2 = some s1 then some s2 else if σ.cdt s2 = some s2 then some s1 else σ.cdt s2)) s2 + (if σ.cdt s1 = some s1 then some s2 else if σ.cdt s1 = some s2 then some s1 else σ.cdt s1) + isOriginalCap := Function.update (Function.update σ.isOriginalCap s1 (σ.isOriginalCap s2)) s2 + (σ.isOriginalCap s1) } + +/-- **An outcome of `cap_swap`** (x86 `capSwap_eq`, as a membership). -/ +theorem capSwap_memA {σ σ1 σ2 : State Unit} {a1 a2 : Cap} {s1 s2 : CslotPtr} + (h1 : ((), σ1) ∈ (setCap (Z := Unit) a2 s1 σ).1) (h2 : ((), σ2) ∈ (setCap (Z := Unit) a1 s2 σ1).1) : + ((), swapFinA σ2 s1 s2) ∈ (Fl4SpecA64.capSwap (Z := Unit) a1 s1 a2 s2 σ).1 := by + unfold Fl4SpecA64.capSwap + refine mem_bindA.2 ⟨((), σ1), h1, ?_⟩ + refine mem_bindA.2 ⟨((), σ2), h2, ?_⟩ + refine mem_bindA.2 ⟨(σ2.cdt s1, σ2), rfl, ?_⟩ + refine mem_bindA.2 ⟨(σ2.cdt s2, σ2), rfl, ?_⟩ + refine mem_bindA.2 ⟨(σ2.cdt, σ2), rfl, ?_⟩ + refine mem_bindA.2 ⟨(_, σ2), rfl, ?_⟩ + refine mem_bindA.2 ⟨((), _), mem_bindA.2 ⟨(σ2, σ2), rfl, rfl⟩, ?_⟩ + refine mem_bindA.2 ⟨((), _), doExtendedOp_memA _ _, ?_⟩ + refine mem_bindA.2 ⟨(_, _), rfl, ?_⟩ + refine mem_bindA.2 ⟨((), _), mem_bindA.2 ⟨(_, _), rfl, rfl⟩, ?_⟩ + exact mem_bindA.2 ⟨(_, _), rfl, rfl⟩ + +/-- `cap_swap`'s CDT is the old one renamed by the transposition (x86 `swap_cdt_eq`). -/ +theorem swap_cdt_eqA (σ : State Unit) (s1 s2 : CslotPtr) (hne : s1 ≠ s2) : + (swapFinA σ s1 s2).cdt = fun r => (σ.cdt (Equiv.swap s1 s2 r)).map (Equiv.swap s1 s2) := by + have hc : ∀ n, (if σ.cdt n = some s1 then some s2 else if σ.cdt n = some s2 then some s1 else σ.cdt n) = + (σ.cdt n).map (Equiv.swap s1 s2) := by + intro n + cases h : σ.cdt n with + | none => simp + | some q => + by_cases h1 : q = s1 + · subst h1; simp + · by_cases h2 : q = s2 + · subst h2; simp [Ne.symm hne] + · simp [h1, h2, Equiv.swap_apply_of_ne_of_ne h1 h2] + funext r + simp only [swapFinA, hc] + by_cases h1 : r = s1 + · subst h1; simp [hne] + · by_cases h2 : r = s2 + · subst h2; simp + · simp [Function.update_of_ne h1, Function.update_of_ne h2, Equiv.swap_apply_of_ne_of_ne h1 h2] + +/-- `cap_swap`'s original-cap map is the old one renamed by the transposition (x86 `swap_io_eq`). -/ +theorem swap_io_eqA (σ : State Unit) (s1 s2 : CslotPtr) (hne : s1 ≠ s2) : + (swapFinA σ s1 s2).isOriginalCap = fun r => σ.isOriginalCap (Equiv.swap s1 s2 r) := by + funext r + simp only [swapFinA] + by_cases h1 : r = s1 + · subst h1; simp [hne] + · by_cases h2 : r = s2 + · subst h2; simp + · simp [Function.update_of_ne h1, Function.update_of_ne h2, Equiv.swap_apply_of_ne_of_ne h1 h2] + +theorem SpecSlotA.swFin (σ : State Unit) (s1 s2 : CslotPtr) : SpecSlotA σ (swapFinA σ s1 s2) := + ⟨⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, ⟨rfl, rfl, rfl, rfl⟩⟩, fun _ => Or.inl rfl⟩ + +/-! ## The CDT after `cap_swap` -/ + +/-- The ghost after swapping `x1` and `x2` (A64 P1's `cdt.swap.tree`; x86 `renG` with the transposition). -/ +def swGA (G : MdbGhostA) (x1 x2 : U64) : MdbGhostA := + ⟨renG x1 x2 G.chains, Fl4Proofs.P3.Mdb.conjPar (Equiv.swap x1 x2) G.par⟩ + +/-- Swapping a slot with itself keeps the ghost. -/ +theorem swGA_self (G : MdbGhostA) (x : U64) : swGA G x x = G := by + have h0 : swp x x = id := by funext y; unfold swp; by_cases e : y = x <;> simp [e] + have h1 : renG x x G.chains = G.chains := by unfold renG; rw [h0]; simp + have h2 : Fl4Proofs.P3.Mdb.conjPar (Equiv.swap x x) G.par = G.par := by + funext y; simp [Fl4Proofs.P3.Mdb.conjPar] + unfold swGA; rw [h1, h2] + +section cdt +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **The spec's CDT after `cap_swap`** against the ghost renamed by the transposition (x86 `CdtRel.swap`). -/ +theorem CdtRelA.swapA {th : TypedHeapA} {kb : U64} (hP : PlaceA th kb) {s s' : S} {σ σ' : State Unit} + {G : MdbGhostA} (hC : CdtRelA st th s σ G) {chains' : List (List U64)} {sl1 sl2 : CslotPtr} + (h1 : AbsSlotA th sl1) (h2 : AbsSlotA th sl2) + (htr : TreeA st {a | IsSlotA th a} chains' + (Fl4Proofs.P3.Mdb.conjPar (Equiv.swap (cslotAddrA sl1) (cslotAddrA sl2)) G.par) s') + (hcdt : σ'.cdt = fun r => (σ.cdt (Equiv.swap sl1 sl2 r)).map (Equiv.swap sl1 sl2)) + (hio : σ'.isOriginalCap = fun r => σ.isOriginalCap (Equiv.swap sl1 sl2 r)) + (hrev : ∀ sl, AbsSlotA th sl → + revA (L.mem s') (cslotAddrA sl) = revA (L.mem s) (cslotAddrA (Equiv.swap sl1 sl2 sl))) : + CdtRelA st th s' σ' + ⟨chains', Fl4Proofs.P3.Mdb.conjPar (Equiv.swap (cslotAddrA sl1) (cslotAddrA sl2)) G.par⟩ := by + have hA : ∀ q, AbsSlotA th q → AbsSlotA th (Equiv.swap sl1 sl2 q) := by + intro q hq + by_cases e1 : q = sl1 + · rw [e1, Equiv.swap_apply_left]; exact h2 + · by_cases e2 : q = sl2 + · rw [e2, Equiv.swap_apply_right]; exact h1 + · rw [Equiv.swap_apply_of_ne_of_ne e1 e2]; exact hq + have haddr : ∀ q, AbsSlotA th q → + cslotAddrA (Equiv.swap sl1 sl2 q) = Equiv.swap (cslotAddrA sl1) (cslotAddrA sl2) (cslotAddrA q) := by + intro q hq + by_cases e1 : q = sl1 + · rw [e1, Equiv.swap_apply_left, Equiv.swap_apply_left] + · by_cases e2 : q = sl2 + · rw [e2, Equiv.swap_apply_right, Equiv.swap_apply_right] + · rw [Equiv.swap_apply_of_ne_of_ne e1 e2, Equiv.swap_apply_of_ne_of_ne + (fun h => e1 (cslotAddrA_inj hP hq h1 h)) (fun h => e2 (cslotAddrA_inj hP hq h2 h))] + have hcp : ∀ x, Fl4Proofs.P3.Mdb.conjPar (Equiv.swap (cslotAddrA sl1) (cslotAddrA sl2)) G.par x = + (G.par (Equiv.swap (cslotAddrA sl1) (cslotAddrA sl2) x)).map (Equiv.swap (cslotAddrA sl1) (cslotAddrA sl2)) := by + intro x; simp [Fl4Proofs.P3.Mdb.conjPar, Equiv.symm_swap] + refine CdtRelA.mkA htr ?_ ?_ ?_ ?_ ?_ + · intro sl hsl + show Option.map cslotAddrA (σ'.cdt sl) = Fl4Proofs.P3.Mdb.conjPar _ G.par (cslotAddrA sl) + rw [hcdt, hcp, ← haddr sl hsl, ← hC.cdt _ (hA sl hsl)] + beta_reduce + cases hq : σ.cdt (Equiv.swap sl1 sl2 sl) with + | none => simp + | some q => + have hq' := (hC.cdt_slot _ _ hq).2 + simp [haddr q hq'] + · intro sl q hq + rw [hcdt] at hq + simp only [Option.map_eq_some_iff] at hq + obtain ⟨q0, hq0, rfl⟩ := hq + obtain ⟨ha, hb⟩ := hC.cdt_slot _ _ hq0 + refine ⟨?_, hA _ hb⟩ + have := hA _ ha; simpa using this + · intro sl hsl + rw [hio, hrev _ hsl] + exact hC.orig _ (hA sl hsl) + · intro a b hab + change Fl4Proofs.P3.Mdb.conjPar _ G.par a = some b at hab + rw [hcp] at hab + simp only [Option.map_eq_some_iff] at hab + obtain ⟨b0, hb0, -⟩ := hab + obtain ⟨sl0, hsl0, he0⟩ := hC.par_slot _ _ hb0 + refine ⟨Equiv.swap sl1 sl2 sl0, hA _ hsl0, ?_⟩ + rw [haddr _ hsl0, he0, Equiv.swap_apply_self] + · -- (gen 7) every parent is original: the flags and the `cdt` renamed alike + intro sl q hq + rw [hcdt] at hq + simp only [Option.map_eq_some_iff] at hq + obtain ⟨q0, hq0, rfl⟩ := hq + rw [hio] + simp only [Equiv.swap_apply_self] + exact hC.po _ _ hq0 + +end cdt + +/-! ## The joint leaves -/ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +set_option maxHeartbeats 2000000 in +/-- **FL4's `cdt::swap` refines l4v `cap_swap`** between two slots of the typed heap (gen 8: CNode or TCB +slots, `AbsSlotA`; module docstring; x86 `swap_refines`). -/ +theorem cap_swap_refinesA {T Ω Sg OS : Set U64} {th : TypedHeapA} {kb : U64} (s : S) (σ : State Unit) + (G : MdbGhostA) (hR : StateRelA th (L.mem s) σ) (hC : CdtRelA st th s σ G) (hP : PlaceA th kb) + (hO : OSOK T Ω Sg OS) (hT : TreeA st OS G.chains G.par s) (hsub : ∀ y, IsSlotA th y → y ∈ OS) + (hnul : ∀ y ∈ OS, ¬ IsSlotA th y → capA st s y = .Null) + {sa sb : CslotPtr} (hA1 : AbsSlotA th sa) (hA2 : AbsSlotA th sb) (hne : sa ≠ sb) + (hn1 : capAtA (L.mem s) (cslotAddrA sa) ≠ .Null) + (hn2 : capAtA (L.mem s) (cslotAddrA sb) ≠ .Null) + (hs1 : obj.sext48 (cslotAddrA sa) = ok (cslotAddrA sa)) + (hs2 : obj.sext48 (cslotAddrA sb) = ok (cslotAddrA sb)) + {c1 c2 : capability.Cap} {a1 a2 : Cap} (hca1 : CapRelA c1 a1) (hca2 : CapRelA c2 a2) + (hw1 : CapRt.EncWf c1) (hw2 : CapRt.EncWf c2) (hc1 : c1 ≠ .Null) (hc2 : c2 ≠ .Null) + (ha1 : UA c1 (capA st s (cslotAddrA sa))) (ha2 : UA c2 (capA st s (cslotAddrA sb))) : + cdt.swap st s c1 (cslotAddrA sa) c2 (cslotAddrA sb) ⦃ s' => + ∃ σ', ((), σ') ∈ (Fl4SpecA64.capSwap (Z := Unit) a1 sa a2 sb σ).1 ∧ + StateRelA th (L.mem s') σ' ∧ + CdtRelA st th s' σ' (swGA G (cslotAddrA sa) (cslotAddrA sb)) ∧ + TreeA st OS (swGA G (cslotAddrA sa) (cslotAddrA sb)).chains + (swGA G (cslotAddrA sa) (cslotAddrA sb)).par s' ∧ SpecSlotA σ σ' ∧ + ∀ y, IsSlotA th y → capAtA (L.mem s') y = capAtA (L.mem s) y ∨ capAtA (L.mem s') y = c1 ∨ + capAtA (L.mem s') y = c2 ⦄ := by + have hS1 := hA1.isSlot hP + have hS2 := hA2.isSlot hP + have hAne : cslotAddrA sa ≠ cslotAddrA sb := fun h => hne (cslotAddrA_inj hP hA1 hA2 h) + have hG1 : cslotAddrA sa ∈ G.chains.flatten := (hT.lk.members _).2 ⟨hsub _ hS1, hn1⟩ + have hG2 : cslotAddrA sb ∈ G.chains.flatten := (hT.lk.members _).2 ⟨hsub _ hS2, hn2⟩ + have F := hT.lk.lkF hO.nz + have inOS : ∀ y ∈ G.chains.flatten, y ∈ OS := fun y hy => ((hT.lk.members y).1 hy).1 + refine WP.spec_mono (WP.spec_and (WP.spec_and (cdt.swap.tree hO hT hG1 hG2 hAne hs1 hs2 hw1 hw2 hc1 hc2 ha1 ha2) + (cdt.swap.lrW hO (LR.self s) (inOS _ hG1) (inOS _ hG2) hAne hs1 hs2 hw1 hw2 + (fun h => inOS _ (F.pvm _ hG1 h)) (fun h => inOS _ (F.nxm _ hG1 h)) + (fun h => inOS _ (F.pvm _ hG2 h)) (fun h => inOS _ (F.nxm _ hG2 h)))) + (swap_bitA hO (LR.self s) (inOS _ hG1) (inOS _ hG2) hAne hs1 hs2 hw1 hw2 + (fun h => inOS _ (F.pvm _ hG1 h)) (fun h => inOS _ (F.nxm _ hG1 h)) + (fun h => inOS _ (F.pvm _ hG2 h)) (fun h => inOS _ (F.nxm _ hG2 h)))) ?_ + rintro s' ⟨⟨hT', -, e1, e2, ek⟩, hb, hb1, hb2⟩ + have hk' : ∀ y, IsSlotA th y → y ≠ cslotAddrA sa → y ≠ cslotAddrA sb → + capAtA (L.mem s') y = capAtA (L.mem s) y := fun y hy f1 f2 => ek y (hsub y hy) f1 f2 + -- the spec: `set_cap` at slot 1 (through the memory with slot 2's cap words from before), then at slot 2 + obtain ⟨o1, h1, hR1, hQ1⟩ := StateRelA.setAbsA (mem' := mixA (L.mem s) (L.mem s') (cslotAddrA sb)) hR hP hA1 + (a := a2) (by + rw [capAt_mixA_other _ _ (slot_ne8 hP hS2 hS1 hAne)] + show CapRelA (capA st s' _) a2 + rw [e1]; exact hca2) + (fun y hy hy1 => by + by_cases e : y = cslotAddrA sb + · rw [e]; exact capAt_mixA_self _ _ _ + · rw [capAt_mixA_other _ _ (slot_ne8 hP hS2 hy e)]; exact hk' y hy hy1 e) + obtain ⟨o2, h2, hR2, hQ2⟩ := StateRelA.setAbsA (mem' := L.mem s') hR1 hP hA2 (a := a1) + (by show CapRelA (capA st s' _) a1; rw [e2]; exact hca1) + (fun y hy hy2 => (capAt_mixA_other _ _ (slot_ne8 hP hS2 hy hy2)).symm) + refine ⟨_, capSwap_memA h1 h2, hR2.of_kheap rfl, CdtRelA.swapA hP hC hA1 hA2 ?_ (swap_cdt_eqA _ _ _ hne) + (swap_io_eqA _ _ _ hne) ?_, hT', + (hQ1.trans hQ2).trans (SpecSlotA.swFin _ _ _), + fun y hy => ?_⟩ + · refine hT'.sub (fun y hy => hsub y hy) fun y hy hn' => ?_ + have f1 : y ≠ cslotAddrA sa := fun e => hn' (e ▸ hS1) + have f2 : y ≠ cslotAddrA sb := fun e => hn' (e ▸ hS2) + rw [ek y hy f1 f2]; exact hnul y hy hn' + · intro sl hsl + by_cases f1 : sl = sa + · rw [f1, Equiv.swap_apply_left]; exact revA_of_bit hb1 + · by_cases f2 : sl = sb + · rw [f2, Equiv.swap_apply_right]; exact revA_of_bit hb2 + · rw [Equiv.swap_apply_of_ne_of_ne f1 f2] + exact revA_of_keep (hb _ (hsub _ (hsl.isSlot hP)) (fun h => f1 (cslotAddrA_inj hP hsl hA1 h)) + (fun h => f2 (cslotAddrA_inj hP hsl hA2 h))) + · by_cases f1 : y = cslotAddrA sa + · exact Or.inr (Or.inr (by rw [f1]; exact e1)) + · by_cases f2 : y = cslotAddrA sb + · exact Or.inr (Or.inl (by rw [f2]; exact e2)) + · exact Or.inl (hk' y hy f1 f2) + +variable {A : Type} + +/-- **FL4's `cdt::swap` refines l4v `cap_swap` with the whole relation** (module docstring; ruling 11:49Z step 1). +`hno1` / `hno2`: the new caps name counted objects. The spec invariant: `SInvXA` across the spec step when each new +cap may go into the slot it is written to (`CapOkA`). -/ +theorem cap_swap_jA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} + {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} {ia ie : Nat} {s : S} {a : A} {σ : State Unit} {m : McState} + (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + (hT : TreeA st OS Z.G.chains Z.G.par s) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) + {sa sb : CslotPtr} (hA1 : AbsSlotA Z.th sa) (hA2 : AbsSlotA Z.th sb) (hne : sa ≠ sb) + (hn1 : capAtA (L.mem s) (cslotAddrA sa) ≠ .Null) + (hn2 : capAtA (L.mem s) (cslotAddrA sb) ≠ .Null) + (hs1 : obj.sext48 (cslotAddrA sa) = ok (cslotAddrA sa)) + (hs2 : obj.sext48 (cslotAddrA sb) = ok (cslotAddrA sb)) + {c1 c2 : capability.Cap} {a1 a2 : Cap} (hca1 : CapRelA c1 a1) (hca2 : CapRelA c2 a2) + (hw1 : CapRt.EncWf c1) (hw2 : CapRt.EncWf c2) (hc1 : c1 ≠ .Null) (hc2 : c2 ≠ .Null) + (ha1 : UA c1 (capA st s (cslotAddrA sa))) (ha2 : UA c2 (capA st s (cslotAddrA sb))) + (hno1 : CapObjOkA Z c1) (hno2 : CapObjOkA Z c2) : + cdt.swap st s c1 (cslotAddrA sa) c2 (cslotAddrA sb) ⦃ s' => + ∃ σ', ((), σ') ∈ (Fl4SpecA64.capSwap (Z := Unit) a1 sa a2 sb σ).1 ∧ + JAtZAX (L := L) AR { Z with G := swGA Z.G (cslotAddrA sa) (cslotAddrA sb) } s' a σ' m ∧ + TreeA st OS (swGA Z.G (cslotAddrA sa) (cslotAddrA sb)).chains + (swGA Z.G (cslotAddrA sa) (cslotAddrA sb)).par s' ∧ + (CapOkA σ.interruptIrqNode sa a2 → CapOkA σ.interruptIrqNode sb a1 → + ∀ n, SInvXA σ m n → SInvXA σ' m n) ⦄ := by + have hO := E.sys.ent.os + have hP := E.j.1.place + have hS1 : IsSlotA Z.th (cslotAddrA sa) := AbsSlotA.isSlot hP hA1 + have hS2 : IsSlotA Z.th (cslotAddrA sb) := AbsSlotA.isSlot hP hA2 + have hG1 : cslotAddrA sa ∈ Z.G.chains.flatten := (hT.lk.members _).2 ⟨hsub _ hS1, hn1⟩ + have hG2 : cslotAddrA sb ∈ Z.G.chains.flatten := (hT.lk.members _).2 ⟨hsub _ hS2, hn2⟩ + have hAne : cslotAddrA sa ≠ cslotAddrA sb := fun h => hne (cslotAddrA_inj hP hA1 hA2 h) + have F := hT.lk.lkF hO.nz + have inOS : ∀ y ∈ Z.G.chains.flatten, y ∈ OS := fun y hy => ((hT.lk.members y).1 hy).1 + refine WP.spec_mono (WP.spec_and (cap_swap_refinesA s σ Z.G E.j.1.st E.j.1.cdt hP hO hT hsub hnul + hA1 hA2 hne hn1 hn2 hs1 hs2 hca1 hca2 hw1 hw2 hc1 hc2 ha1 ha2) + (cdt.swap.lrWF (osokA hO) (LR.self s) (inOS _ hG1) (inOS _ hG2) hAne hs1 hs2 hw1 hw2 + (fun h => inOS _ (F.pvm _ hG1 h)) (fun h => inOS _ (F.nxm _ hG1 h)) + (fun h => inOS _ (F.pvm _ hG2 h)) (fun h => inOS _ (F.nxm _ hG2 h)))) ?_ + rintro s' ⟨⟨σ', hm, hst, hcdt, hT', hSS, hcs⟩, hw', -⟩ + refine ⟨σ', hm, E.j.of_slotWrites hw' (fun b hb' hb => footN_offA E b hb hb') + (FootNA.aligned E.j.1 (bxi_nodes E.sys.ent.bxi) E.scAl E.ap.vcAl) _ hSS hst hcdt fun y hy => ?_, hT', + fun o1 o2 n hI => SInvXA.capSwap hI o1 o2 hm⟩ + rcases hcs y hy with e | e | e <;> rw [e] + · exact E.j.capObj hy + · exact hno1 + · exact hno2 + +/-- **FL4's `cdt::cap_swap_for_delete` refines l4v `cap_swap_for_delete` with the whole relation** (module +docstring; ruling 11:49Z step 1): two non-null canonical slots (gen 8: CNode or TCB slots) (equal slots: both sides keep the state), the +ghost `swGA` (`swGA_self` when equal), `SInvXA` for slots off the IRQ node. -/ +theorem cap_swap_for_delete_jA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} + {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} {ia ie : Nat} {s : S} {a : A} {σ : State Unit} {m : McState} + (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + (hT : TreeA st OS Z.G.chains Z.G.par s) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) + {sa sb : CslotPtr} (hA1 : AbsSlotA Z.th sa) (hA2 : AbsSlotA Z.th sb) + (hn1 : capAtA (L.mem s) (cslotAddrA sa) ≠ .Null) + (hn2 : capAtA (L.mem s) (cslotAddrA sb) ≠ .Null) + (hs1 : obj.sext48 (cslotAddrA sa) = ok (cslotAddrA sa)) + (hs2 : obj.sext48 (cslotAddrA sb) = ok (cslotAddrA sb)) : + cdt.cap_swap_for_delete st s (cslotAddrA sa) (cslotAddrA sb) ⦃ s' => + ∃ σ', ((), σ') ∈ (Fl4SpecA64.capSwapForDelete (Z := Unit) sa sb σ).1 ∧ + JAtZAX (L := L) AR { Z with G := swGA Z.G (cslotAddrA sa) (cslotAddrA sb) } s' a σ' m ∧ + TreeA st OS (swGA Z.G (cslotAddrA sa) (cslotAddrA sb)).chains + (swGA Z.G (cslotAddrA sa) (cslotAddrA sb)).par s' ∧ + (OffNodeA σ.interruptIrqNode sa.1 → OffNodeA σ.interruptIrqNode sb.1 → + ∀ n, SInvXA σ m n → SInvXA σ' m n) ⦄ := by + have hP := E.j.1.place + unfold cdt.cap_swap_for_delete + simp only [obj.Slot.Insts.CoreCmpPartialEqSlot.eq, bind_ok] + split + · -- equal slots: nothing happens on either side + rename_i heq + have he : cslotAddrA sa = cslotAddrA sb := by simpa using heq + have hsl : sa = sb := cslotAddrA_inj hP hA1 hA2 he + refine (WP.spec_ok _).2 ⟨σ, ?_, ?_, ?_, fun _ _ n hI => hI⟩ + · unfold Fl4SpecA64.capSwapForDelete NonDet.when + rw [if_neg (by simpa using hsl)]; exact rfl + · rw [← he, swGA_self]; exact E.j + · rw [← he, swGA_self]; exact hT + · rename_i hne' + have hAne : cslotAddrA sa ≠ cslotAddrA sb := by simpa using hne' + have hne : sa ≠ sb := fun h => hAne (by rw [h]) + rw [capA_eq, bind_ok, capA_eq, bind_ok] + obtain ⟨b1, hg1, hcb1⟩ := E.j.1.st.getAbsA hA1 + obtain ⟨b2, hg2, hcb2⟩ := E.j.1.st.getAbsA hA2 + refine WP.spec_mono (cap_swap_jA E hT hsub hnul hA1 hA2 hne hn1 hn2 hs1 hs2 hcb1 hcb2 + (CapRt.decodeF_encWf _ _) (CapRt.decodeF_encWf _ _) hn1 hn2 (UA.refl _) (UA.refl _) + (E.j.capObj (AbsSlotA.isSlot hP hA1)) (E.j.capObj (AbsSlotA.isSlot hP hA2))) ?_ + rintro s' ⟨σ', hm, hJ, hT', -⟩ + have hmem : ((), σ') ∈ (Fl4SpecA64.capSwapForDelete (Z := Unit) sa sb σ).1 := by + unfold Fl4SpecA64.capSwapForDelete NonDet.when + rw [if_pos hne] + exact mem_bindA.2 ⟨(b1, σ), hg1, mem_bindA.2 ⟨(b2, σ), hg2, hm⟩⟩ + exact ⟨σ', hmem, hJ, hT', fun o1 o2 n hI0 => + SInvXA.capSwapForDelete (s1 := sa) (s2 := sb) hI0 o1 o2 hmem⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/TabNewA.lean b/proofs/Fl4ProofsA64/P3/TabNewA.lean new file mode 100644 index 000000000..959a351b4 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TabNewA.lean @@ -0,0 +1,286 @@ +/- +P3 (aarch64, A64-P3 S3 gen 23, ruling 21:26Z item 1): **`TabOffA` across `pt_map_jA`** — the newly mapped table +`ptr` joins the Stage-1 tables (`insert ptr Z.S1`), so the next lift needs `TabOffA` for it and for the old tables at +the post state. x86 has no counterpart (x86's tables are placed by `TopPlaced` inside its relation). + +* **`tabOff_newA`**: `TabOffA Z m s ptr` for the table being mapped, from the map's decode-site facts — gen 19's + `UnmPtA` (its words in no `OS` slot and in no TCB, not a window's level-1 table, the spec object a page table), + ruling (c)'s `ptr ∉ S2PgE Z.X Z.T2 ∧ ptr ∉ Z.Y`, `JEntA` (the slots of `th` are `OS` slots: `RsSiteA.sub`; the + pools' / windows' alignment and spec objects: `VsRelA`; the Stage-2 pages: `ExtFootE.pgE`; every related spec + object's kind: `StateRelA`, `SchedRelA`, the extension records) — and the **residual placement `NewTabA`**: `ptr` is + no `other` object of `th` (`th`'s `other` objects have no spec object, so their names cannot be told apart from + `specPtr ptr` by the spec), and its words are no per-core node word, IRQ-state / global / ASID-table word (the + kernel-state block), SC word or VCPU word (`FootNA` at `ksZA Z`, the sets emptied but `C`, `V`, `Cs`). Gen 21's + finding stands: A64 P1's `HeapKsOff` allows `other` objects (where the tables live, `PtHA`) inside the + kernel-state block's static regions, so these words are not derivable from `JEntA`; `NewTabA` is decode-site, of + `TabOffA`'s kind (discharged with `TabOffA` by S1A's placement, as `UnmPtA` by S1A's B3). +* **`pt_map_jTA`** (the chaining form of `pt_map_jA`): with `TabOffA` for every table of `insert ptr Z.S1` at the + entry (`hTO`), the outcome (`PtMapJTPostA`) is `pt_map_jA`'s plus `TabOffA` for every table of `insert ptr Z.S1` + at the post state and the new ghosts — the shape of `hTO` for the next lift. From `MapJA`'s `ksb` / `cpu` + (gen 23) by `TabOffA.liftA`. + +* (gen 24, ruling 21:50Z item 3) **`page_map_jTA`** (the chaining form of `page_map_jA`): the page map keeps the + ghosts, so the outcome (`PgMapJTA`) is `page_map_jA`'s plus `TabOffA` for every table of `Z.S1` at the post state + (`PgMapRelGA.liftTA`: `MapJA`'s `ksb` / `cpu` and `TabOffA.of_ksA`). + +**Changed statements (gen 30, forced by ruling 01:13Z item 1):** `PtMapJTPostA` / `PgMapJTA` add `CarryA Z (L.mem s) +σ → CarryA Z' (L.mem s') σ'` (`carry_ptMapA` / `carry_pgMapA`, `P3/VsMapJA`); `PtMapPostGA.liftTA` / +`PgMapRelGA.liftTA` take `hsub` and the invoked cap `hcap`. `pt_map_jTA` / `page_map_jTA` otherwise unchanged. + +No named premise. Decode-site hypotheses: `TabOffA` (gen 21), `NewTabA` (new, the residual above). +-/ +import Fl4ProofsA64.P3.VsMapJA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-! ## The new table off the footprint -/ + +/-- The ghosts with every object set emptied but the SCs, the VCPUs and the cores: `FootNA` there is the per-core node +words, the IRQ-state / global / ASID-table words, and the SC / VCPU words. -/ +def ksZA (Z : XSetsA) : XSetsA := { Z with T := ∅, Pp := ∅, S1 := ∅, Y := ∅, X := ∅, T2 := ∅ } + +/-- **The residual placement of a new table** (decode-site, module docstring). -/ +structure NewTabA (Z : XSetsA) (s : S) (p : U64) : Prop where + oth : ∀ z, Z.th p ≠ some (.other z) + ks : ∀ i : U64, i.val < 512 → ¬ FootNA (L := L) (ksZA Z) s (peAF p i) + +/-- A word inside a TCB's 4 KiB is inside it. -/ +theorem tcbW_offA {t K w : U64} (tq : TcbAddr t) (hK : K.val < 4096) (hw : w = wa t K) + (hr : w.val < t.val ∨ t.val + 4096 ≤ w.val) : False := by + subst hw; have := field_val tq hK; omega + +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +set_option maxHeartbeats 1000000 in +/-- **`TabOffA` for the table being mapped** (module docstring). -/ +theorem tabOff_newA (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) {ptr : U64} (UP : UnmPtA Z.S1 Z.Y OS H.tcbs (L.mem s) σ ptr) + (hS2 : ptr ∉ S2PgE Z.X Z.T2) (hY : ptr ∉ Z.Y) (N : NewTabA (L := L) Z s ptr) : + TabOffA (L := L) Z m s ptr := by + obtain ⟨t0, hk0, -, -⟩ := UP.spec + have J := E.j + have hpa : ptr.val % 4096 = 0 := Nat.mod_eq_zero_of_dvd UP.al + obtain ⟨⟨Q, R, q⟩, -⟩ := E.sys.ent.bxi + have tT : ∀ t, t ∈ Z.T → t ∈ H.tcbs := fun t ht => by rw [E.tcbs]; exact ht + refine ⟨fun i hi h => ?_, fun n hn e => ?_⟩ + · generalize hb : peAF ptr i = b at h + cases h with + | slot y w hy hw => + have hyO := RS.sub y (slotW_isSlot J.1.place hy) + obtain ⟨v8, v16, v24⟩ := vals E.sys.ent.os hyO + refine UP.os i hi ⟨y, hyO, ?_⟩ + rw [hb] + rcases hw with rfl | rfl | rfl | rfl <;> omega + | nslot w h => + cases h with + | node c w hc hw => exact N.ks i hi (by rw [hb]; exact .node c _ hc hw) + | cur w hw => exact N.ks i hi (by rw [hb]; exact .cur _ hw) + | tcb t w ht hw => + have hr := UP.tcb i hi t (tT t ht) + rw [hb] at hr + have tq := q.tcbs t (tT t ht) + rcases hw with rfl | rfl | rfl | rfl | rfl + · exact tcbW_offA tq (K := 3328#u64) (by simp) (stA_eq t) hr + · exact tcbW_offA tq (K := 3464#u64) (by simp) rfl hr + · exact tcbW_offA tq (K := 3520#u64) (by simp) rfl hr + · exact tcbW_offA tq (K := 3664#u64) (by simp) rfl hr + · exact tcbW_offA tq (K := 3672#u64) (by simp) rfl hr + | sc c hc => exact N.ks i hi (by rw [hb]; exact .sc c hc) + | vcpu v w hv hw => exact N.ks i hi (by rw [hb]; exact .vcpu v _ hv hw) + | irq j hj => exact N.ks i hi (by rw [hb]; exact .irq j hj) + | cur2 w hw => exact N.ks i hi (by rw [hb]; exact .cur2 _ hw) + | glob w hw => exact N.ks i hi (by rw [hb]; exact .glob _ hw) + | tcb2 t w ht hw => + have hr := UP.tcb i hi t (tT t ht) + rw [hb] at hr + have tq := q.tcbs t (tT t ht) + rcases hw with rfl | rfl + · exact tcbW_offA tq (K := 3448#u64) (by simp) rfl hr + · exact tcbW_offA tq (K := 3432#u64) (by simp) rfl hr + | sc2 c hc => exact N.ks i hi (by rw [hb]; exact .sc2 c hc) + | vs w hw => + cases hw with + | asid j hj => exact N.ks i hi (by rw [hb]; exact .vs _ (.asid j hj)) + | pool p lo hp hlo => + obtain ⟨e1, -⟩ := peAF_injA hpa (Nat.mod_eq_zero_of_dvd (J.1.vs.pal p hp)) hi hlo hb + subst e1 + obtain ⟨f, hf, -⟩ := J.1.vs.pool _ hp + rw [hk0] at hf; cases hf + | pt p j hp hj => exact absurd hp (Set.notMem_empty _) + | high y j hy hj => + obtain ⟨e1, -⟩ := peAF_injA hpa (waWin_modA (J.1.vs.yal y hy)) hi hj hb + exact UP.ytb y hy e1.symm + | ext w hw => + obtain ⟨q', j, hj, rfl, hq', hc⟩ := ExtFootE.pgE J.1.ext J.1.vs.yal hw + obtain ⟨e1, -⟩ := peAF_injA hpa (Nat.mod_eq_zero_of_dvd hq') hi hj hb + subst e1 + rcases hc with hc | hc + · exact hS2 hc + · exact hY hc + · subst e + rcases hn with ⟨q', hq', e⟩ | ⟨t, ht, e⟩ | ⟨c, hc, e⟩ | ⟨v, hv, e⟩ | ⟨r, hr, e⟩ | ⟨r, hr, e⟩ | ⟨q', hq', e⟩ | + ⟨q', hq', e⟩ | ⟨q', hq', e⟩ + · have e' := specPtr_inj e + subst e' + cases hth : Z.th q' with + | none => exact hq' hth + | some k => + cases k with + | cnode r => + obtain ⟨cs, hx, -⟩ := J.1.st.cnode q' r hth + rw [hk0] at hx; cases hx + | tcb => + obtain ⟨x, hx, -⟩ := J.1.st.tcb q' hth + rw [hk0] at hx; cases hx + | other z => exact N.oth z hth + · obtain ⟨x, hx, -⟩ := J.1.sch.tcb t ht + rw [e, hk0] at hx; cases hx + · obtain ⟨y, n, hx, -⟩ := J.1.sch.sc c hc + rw [e, hk0] at hx; cases hx + · obtain ⟨x, hx, -⟩ := J.2.1.obj v hv + rw [e, hk0] at hx; cases hx + · obtain ⟨es, hx⟩ := J.2.2.1.obj r hr + rw [e, hk0] at hx; cases hx + · obtain ⟨l1, hx⟩ := J.2.2.2.obj r hr + rw [e, hk0] at hx; cases hx + · obtain ⟨f, hx, -⟩ := J.1.vs.pool q' hq' + rw [e, hk0] at hx; cases hx + · exact absurd hq' (Set.notMem_empty _) + · have e' := specPtr_inj e + subst e' + exact hS2 (Or.inl hq') + +/-! ## The chaining form of `pt_map_jA` -/ + +/-- **`PtMapJPostA` with `TabOffA` at the post state** (module docstring). -/ +def PtMapJTPostA (AR : A → HwRootA → Prop) (Z : XSetsA) (OS : Set U64) (m : Fl4SpecA64.McState) (ptr : U64) + (σ : Fl4SpecA64.State Unit) + (D : Set (Except Fl4SpecA64.SyscallError Fl4SpecA64.ArchInvocation × Fl4SpecA64.State Unit) × Prop) + (s : S) (a : A) : core.result.Result Unit error.Exc × S × A → Prop + | (.Err e, s', a') => s' = s ∧ a' = a ∧ ∃ e', D = ({(.error e', σ)}, False) ∧ VsErrRelA e e' + | (.Ok (), s', a') => a' = a ∧ ∃ iv, D = ({(.ok (.invokePageTable iv), σ)}, False) ∧ ∃ σ1, + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ ∃ σ' n, + ((), σ') ∈ (Fl4SpecA64.performPageTableInvocation (Z := Unit) iv σ1).1 ∧ + JAtZAX (L := L) AR { Z with S1 := insert ptr Z.S1, Lv := Function.update Z.Lv ptr n } s' a σ' m ∧ + TreeA st OS Z.G.chains Z.G.par s' ∧ + (∀ q ∈ insert ptr Z.S1, + TabOffA (L := L) { Z with S1 := insert ptr Z.S1, Lv := Function.update Z.Lv ptr n } m s' q) ∧ + (CarryA Z (L.mem s) σ → CarryA { Z with S1 := insert ptr Z.S1, Lv := Function.update Z.Lv ptr n } (L.mem s') σ') + +theorem PtMapPostGA.liftTA {OS : Set U64} {sl ptr : U64} + {D : Set (Except Fl4SpecA64.SyscallError Fl4SpecA64.ArchInvocation × Fl4SpecA64.State Unit) × Prop} + (hT : TreeA st OS Z.G.chains Z.G.par s) + (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) + (hTO : ∀ q ∈ insert ptr Z.S1, TabOffA (L := L) Z m s q) (hc : ptr ∉ S2PgE Z.X Z.T2 ∧ ptr ∉ Z.Y) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) {mp : Bool} {asid0 va0 : U64} + (hcap : capAtA (L.mem s) sl = .PageTable ptr mp asid0 va0) : + ∀ {res}, PtMapPostGA (L := L) AR Z OS sl m ptr σ D s a res → PtMapJTPostA (L := L) AR Z OS m ptr σ D s a res + | (.Err _, _, _), h => h + | (.Ok (), _, _), ⟨h1, iv, h2, σ1, h3, σ', n, h4, _, hn, _, _, h6⟩ => by + obtain ⟨J, T, hk, hc', F⟩ := h6 hT hnul (fun q hq => hTO q (Set.mem_insert_of_mem _ hq)) hc + exact ⟨h1, iv, h2, σ1, h3, σ', n, h4, J, T, fun q hq => (hTO q hq).liftA hk hc' _ _, + carry_ptMapA hsub hT hcap h3 h4 hn F⟩ + +/-- **Stage-1 `ARMPageTableMap` with `JAtZAX` and `TabOffA` after it** (module docstring): `pt_map_jA` with `TabOffA` +for every table of `insert ptr Z.S1` at the entry (for `ptr`: `tabOff_newA`), and so at the post state. -/ +theorem pt_map_jTA (ar : machine.Arch A) (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) + (cte : Fl4SpecA64.CslotPtr) (hab : AbsSlotA Z.th cte) (hos : cslotAddrA cte ∈ OS) + {ptr asid0 va0 : U64} {mp : Bool} (hcap : capAtA (L.mem s) (cslotAddrA cte) = .PageTable ptr mp asid0 va0) + (UP : UnmPtA Z.S1 Z.Y OS H.tcbs (L.mem s) σ ptr) + (slbl : Fl4SpecA64.Data) (hl : Fl4SpecA64.invocationType slbl = .archInvocationLabel .armPageTableMap) + (length buf : U64) (args : List U64) (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) + (x0 : U64) (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) + (exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) (hex : ExtraRelA Z.th (L.mem s) x0 (exs.map Prod.fst)) + (hS1 : ∀ p l mp' asid va, capAtA (L.mem s) x0 ≠ .Ept p l mp' asid va) + (hHi : ∀ p mp' asid, capAtA (L.mem s) x0 ≠ .HighVSpace p mp' asid) + (hT : TreeA st OS Z.G.chains Z.G.par s) (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) + (hTO : ∀ q ∈ insert ptr Z.S1, TabOffA (L := L) Z m s q) (hS2 : ptr ∉ S2PgE Z.X Z.T2) (hY : ptr ∉ Z.Y) : + vspace.decode_page_table st ar s a fl4_abi.label.ARMPageTableMap length (cslotAddrA cte) + (.PageTable ptr mp asid0 va0) buf extra ⦃ res => PtMapJTPostA (L := L) AR Z OS m ptr σ + ((Fl4SpecA64.decodePageTableInvocation (Z := Unit) slbl (args.map UScalar.bv) cte + (.pageTableCap (specPtr ptr) .normalPtT (mapDataA mp asid0 va0)) exs).run σ) s a res ⦄ := + WP.spec_mono (pt_map_refinesGA ar E RS cte hab hos hcap UP slbl hl length buf args hlen + hargs x0 hx0 exs hex hS1 hHi) fun _ h => PtMapPostGA.liftTA hT hnul hTO ⟨hS2, hY⟩ RS.sub hcap h + +/-- **`hTO` of `pt_map_jTA` from `pt_map_jA`'s and the new table's facts** (`tabOff_newA`). -/ +theorem tabOff_insNewA (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) {ptr : U64} (UP : UnmPtA Z.S1 Z.Y OS H.tcbs (L.mem s) σ ptr) + (hS2 : ptr ∉ S2PgE Z.X Z.T2) (hY : ptr ∉ Z.Y) (N : NewTabA (L := L) Z s ptr) + (hTO : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s q) : ∀ q ∈ insert ptr Z.S1, TabOffA (L := L) Z m s q := by + intro q hq + rcases Set.mem_insert_iff.1 hq with rfl | h + · exact tabOff_newA E RS UP hS2 hY N + · exact hTO q h + + +/-! ## The chaining form of `page_map_jA` (gen 24) -/ + +/-- (gen 24) **`PgMapJA` with `TabOffA` at the post state** (module docstring): the page map keeps the ghosts, so +every Stage-1 table stays off the footprint at the post state. -/ +def PgMapJTA (ar : machine.Arch A) (AR : A → HwRootA → Prop) (Z : XSetsA) (OS : Set U64) + (m : Fl4SpecA64.McState) (σ : Fl4SpecA64.State Unit) + (D : Set (Except Fl4SpecA64.SyscallError Fl4SpecA64.ArchInvocation × Fl4SpecA64.State Unit) × Prop) + (s : S) (a : A) (r : Result (core.result.Result Unit error.Exc × S × A)) : Prop := + (∃ e e', r = ok (.Err e, s, a) ∧ D = ({(.error e', σ)}, False) ∧ VsErrRelA e e') ∨ + ∃ s' iv, D = ({(.ok (.invokePage iv), σ)}, False) ∧ ∃ σ1, + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ ∃ σ', + (([] : List Fl4SpecA64.Data), σ') ∈ (Fl4SpecA64.performPageInvocation (Z := Unit) iv σ1).1 ∧ + r ⦃ x => ∃ a', x = ((.Ok () : core.result.Result Unit error.Exc), s', a') ∧ + JAtZAX (L := L) AR Z s' a' σ' m ∧ TreeA st OS Z.G.chains Z.G.par s' ∧ + (∀ q ∈ Z.S1, TabOffA (L := L) Z m s' q) ∧ (CarryA Z (L.mem s) σ → CarryA Z (L.mem s') σ') ⦄ + +theorem PgMapRelGA.liftTA {ar : machine.Arch A} (K : ArchKeepsA ar AR) {OS : Set U64} + {D : Set (Except Fl4SpecA64.SyscallError Fl4SpecA64.ArchInvocation × Fl4SpecA64.State Unit) × Prop} + {r : Result (core.result.Result Unit error.Exc × S × A)} (ha : AR a Z.hw) + (hT : TreeA st OS Z.G.chains Z.G.par s) + (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) (hTO : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s q) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) {sl fp rt asid0 va0 : U64} {sz : capability.FrameSize} {dev mp : Bool} + (hcap : capAtA (L.mem s) sl = .Frame fp sz rt dev mp false asid0 va0) + (h : PgMapRelGA (L := L) ar AR Z OS sl m σ D s a r) : PgMapJTA (L := L) ar AR Z OS m σ D s a r := + h.imp_right fun ⟨s', hF, iv, h2, σ1, h3, σ', h4, _, _, _, h6⟩ => ⟨s', iv, h2, σ1, h3, σ', h4, + WP.spec_mono (hF.arA K ha) fun _ ⟨a', hx, ha'⟩ => by + obtain ⟨J, T, hk, hc, F⟩ := h6 a' ha' hT hnul hTO trivial + exact ⟨a', hx, J, T, fun q hq => (hTO q hq).of_ksA hk hc, carry_pgMapA hsub hT hcap h3 h4 F⟩⟩ + +/-- (gen 24) **Stage-1 `ARMPageMap` with `JAtZAX` and `TabOffA` after it** (module docstring): `page_map_jA`'s +hypotheses, and `TabOffA` for every Stage-1 table at the post state (`MapJA`'s `ksb` / `cpu`, `TabOffA.of_ksA`). -/ +theorem page_map_jTA (ar : machine.Arch A) (K : ArchKeepsA ar AR) + (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) + (cte : Fl4SpecA64.CslotPtr) (hab : AbsSlotA Z.th cte) (hos : cslotAddrA cte ∈ OS) + {fp asid0 va0 rt : U64} {sz : capability.FrameSize} {dev mp : Bool} + (hcap : capAtA (L.mem s) (cslotAddrA cte) = .Frame fp sz rt dev mp false asid0 va0) + (FP : FrPlA sz fp mp asid0) + (slbl : Fl4SpecA64.Data) (hl : Fl4SpecA64.invocationType slbl = .archInvocationLabel .armPageMap) + (length buf : U64) (args : List U64) (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) (call : Bool) + (x0 : U64) (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) + (exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) (hex : ExtraRelA Z.th (L.mem s) x0 (exs.map Prod.fst)) + (hS1 : ∀ p l mp' asid va, capAtA (L.mem s) x0 ≠ .Ept p l mp' asid va) + (hHi : ∀ p mp' asid, capAtA (L.mem s) x0 ≠ .HighVSpace p mp' asid) + (hT : TreeA st OS Z.G.chains Z.G.par s) (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) + (hTO : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s q) : + PgMapJTA (L := L) ar AR Z OS m σ ((Fl4SpecA64.decodeFrameInvocation (Z := Unit) slbl (args.map UScalar.bv) cte + (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev (mapDataA mp asid0 va0)) exs).run σ) s a + (vspace.decode_frame st ar s a fl4_abi.label.ARMPageMap length (cslotAddrA cte) + (.Frame fp sz rt dev mp false asid0 va0) call buf extra) := + (page_map_refinesGA ar E RS cte hab hos hcap FP slbl hl length buf args hlen hargs call x0 + hx0 exs hex hS1 hHi).liftTA K E.j.1.ar hT hnul hTO RS.sub hcap + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/TabOffW1A.lean b/proofs/Fl4ProofsA64/P3/TabOffW1A.lean new file mode 100644 index 000000000..a7a2c6176 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TabOffW1A.lean @@ -0,0 +1,181 @@ +/- +P3 (aarch64, A64-P3 S3 gen 22, ruling 20:56Z item 3, (S3-a)): **`TabOffA` at `w1`** — a non-vacuous witness of the +Stage-1 maps' decode-site hypothesis (`P3/VsTabJA`) over A64-Boot's post-`create_root_task` state, whose relation has +the four Stage-1 tables `w1Tb` (root `VSb`, `L1b`, `L2b`, `L3b`; `P1/BootW1`). At `w0` it is vacuous (`tabOff_w0A`). + +* `W1OffA w`: `w` is below the root page `VSb`, between it and `L1b`, or above `L3b`'s page — no word of the tables. +* `w1_tcbOffA` / `w1_scOffA` / `w1_nodeOffA`: a TCB's words (`IdleT` below `RRb`, the root TCB `RTb`, whose page ends at + `L1b`), an SC's words (the idle SCs below `RRb`, the root SC `SCb` right above `L3b`), and the nodes' words (in the + kernel-state block, below `RRb` by `BootLay1.kbR`) are `W1OffA`. +* **`tabOff_w1A`**: every table of `w1Tb` satisfies `TabOffA (w1ZA kb ib nc) (w1MA ib nc) S1`: each `FootA` word at + `{w1ZA with S1 := ∅}` (the slots by `slotW_valA` / `w1ThA_cases` — the root CNode below `VSb`, the root TCB's 7 + slots, the boot objects below `RRb`; the nodes, IRQ, global and ASID-table words in the block; the pool page + `PLb`; no VCPU, window or Stage-2 word) is `W1OffA`, and no related object (`RelPA`: `w1ThA`'s objects, the TCBs, + the renamed SCs — `idle_sc_ptr` is below `RRb` —, the pool) is named `specPtr p`. + +Hypotheses: `hL`, `hK`, `P` (the boot layout, as `w1_jEntA`). No named premise. New module; no existing statement +changed. +-/ +import Fl4ProofsA64.P3.JEntW1A +import Fl4ProofsA64.P3.VsTabJA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P1.W7.R.ST.BootWit + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +theorem waV_A {t K : U64} (h : t.val + K.val < 2 ^ 64) : (wa t K).val = t.val + K.val := by + rw [wa_valm]; exact Nat.mod_eq_of_lt h + +/-- A word off `w1`'s Stage-1 tables' pages (module docstring). -/ +def W1OffA (w : U64) : Prop := w.val < VSb ∨ (VSb + 4096 ≤ w.val ∧ w.val < L1b) ∨ L3b + 4096 ≤ w.val + +section +variable {kb ib r now mt sl : U64} {nc : Nat} {uw : U64 → U64} + +set_option hygiene false in +local notation "S1" => w1S kb ib nc r now mt sl uw + +variable (hL : BootLayA ib nc) (hK : BootKsA kb ib nc) (P : BootLay1 kb ib nc) + +include P in +theorem w1_tcbOffA {t K : U64} (ht : t ∈ (w1H kb ib nc).tcbs) (hK4 : K.val < 4096) : W1OffA (wa t K) := by + have h2 := P.ibR + unfold W1OffA + rcases w1H_tcbs P ht with ⟨c, hc, e⟩ | e + · rw [waV_A (by omega)]; left; omega + · rw [waV_A (by omega)]; right; left; omega + +include hL P in +theorem w1_scOffA {c K : U64} (hc : c ∈ (w1H kb ib nc).scs) (hK4 : K.val < 4096) : W1OffA (wa c K) := by + have h2 := P.ibR + unfold W1OffA + rcases w1H_scs P hc with ⟨hb, -⟩ | ⟨e, -⟩ + · obtain ⟨c', hc', rfl⟩ := bootC_of (kb := kb) hL hb + have := idleSA_val' hL hc' + rw [waV_A (by omega)]; left; omega + · rw [waV_A (by omega)]; right; right; omega + +include P in +theorem w1_nodeOffA {c o : U64} (hc : c.val < nc) (ho : o.val < 4288) : W1OffA (bL.nodeAt S1 c o) := by + have h1 := P.kbR + have h128 := P.w0.nc128 + have hv := nodeAt_bv (r := r) (now := now) (mt := mt) P.w0 (c := c) (o := o) (by omega) ho + left + show (bL.nodeAt (bootS kb ib nc r now mt) c o).val < _ + rw [hv]; omega + +set_option maxHeartbeats 1000000 in +include hL hK P in +/-- **`TabOffA` at `w1`** (module docstring). -/ +theorem tabOff_w1A {p : U64} (hp : p ∈ w1Tb) : TabOffA (L := bL) (w1ZA kb ib nc) (w1MA ib nc) S1 p := by + have h1 := P.kbR; have h2 := P.ibR; have hkw := P.w0.kbw; have h128 := P.w0.nc128 + have hks : bL.ksb S1 = kb := rfl + have hpv : p.val = VSb ∨ p.val = L1b ∨ p.val = L2b ∨ p.val = L3b := w1Tb_val hp + refine ⟨fun i hi hF => ?_, fun q hq e => ?_⟩ + · have hb := peAF_valA (p := p) (by rcases hpv with e | e | e | e <;> omega) hi + generalize hbw : peAF p i = b at hF hb + have key : ∀ w : U64, W1OffA w → w = b → False := by + intro w hw e; subst e; unfold W1OffA at hw; rcases hpv with e | e | e | e <;> omega + cases hF with + | slot y w hy hw => + obtain ⟨q, k, j, hq, hs, hyv, htj⟩ := slotW_valA (w1_placeA hL hK P) hy + have hy32 : y.val + 32 ≤ VSb ∨ (VSb + 4096 ≤ y.val ∧ y.val + 32 ≤ L1b) ∨ + (L3b + 4096 ≤ y.val ∧ y.val + 32 < 2 ^ 64) := by + rcases w1ThA_cases hq with ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨hF, rfl⟩ | hb0 + · rw [show (ObjKindA.cnode 13).size = 262144 from rfl] at hs + rw [vRR] at hyv; left; omega + · have := htj rfl + rw [vRT] at hyv; right; left; omega + · rw [show (ObjKindA.other 4096).size = 4096 from rfl] at hs + unfold FrPg at hF; omega + · have := (bootTh_ltA hL hK P hb0).1; left; omega + have e8 : (8#u64 : U64).val = 8 := rfl + have e16 : (16#u64 : U64).val = 16 := rfl + have e24 : (24#u64 : U64).val = 24 := rfl + refine key _ ?_ rfl + unfold W1OffA + rcases hw with rfl | rfl | rfl | rfl + · omega + all_goals rw [waV_A (by omega)]; omega + | nslot w h => + cases h with + | node c w hc hw => + have hc' : c.val < nc := hc + rcases hw with rfl | rfl | rfl | rfl <;> exact key _ (w1_nodeOffA P hc' (by simp [layout.node.CUR_THREAD, + layout.node.IDLE_THREAD, layout.node.CUR_SC, layout.node.IDLE_SC])) rfl + | cur w hw => + have hc' : (bL.cpu S1).val < nc := hL.nc0 + rcases hw with rfl | rfl <;> exact key _ (w1_nodeOffA P hc' (by simp [layout.node.SCHED_ACTION, + layout.node.CUR_TIME])) rfl + | cur2 w hw => + have hc' : (bL.cpu S1).val < nc := hL.nc0 + rcases hw with rfl | rfl <;> exact key _ (w1_nodeOffA P hc' (by simp [layout.node.CONSUMED, + layout.node.REPROGRAM])) rfl + | tcb t w ht hw => + rcases hw with rfl | rfl | rfl | rfl | rfl + · exact key _ (by rw [stA_eq]; exact w1_tcbOffA P ht (by simp)) rfl + all_goals exact key _ (w1_tcbOffA P ht (by simp)) rfl + | tcb2 t w ht hw => rcases hw with rfl | rfl <;> exact key _ (w1_tcbOffA P ht (by simp)) rfl + | sc c hc => exact key _ (w1_scOffA hL P hc (by simp)) rfl + | sc2 c hc => exact key _ (w1_scOffA hL P hc (by simp)) rfl + | vcpu v w hv => exact absurd hv (Set.notMem_empty _) + | irq j hj => + refine key _ ?_ rfl + rw [hks]; unfold W1OffA; rw [irqWordA_val hK.fit hj]; left; omega + | glob w hw => + refine key _ ?_ rfl + have e8 : (8#u64 : U64).val = 8 := rfl + have e16 : (16#u64 : U64).val = 16 := rfl + rw [hks] at hw; unfold W1OffA + rcases hw with rfl | rfl <;> rw [waV_A (by omega)] <;> omega + | vs w hw => + cases hw with + | asid hi hh => + refine key _ ?_ rfl + unfold W1OffA; rw [hks, atAF_valA hkw hh]; left; omega + | pool q lo hq hl => + have e : q.val = PLb := hq + refine key _ ?_ rfl + unfold W1OffA; rw [peAF_valA (by omega) hl]; right; left; omega + | pt q j hq _ => exact absurd hq (Set.notMem_empty _) + | high y j hy _ => exact absurd hy (Set.notMem_empty _) + | ext w hw => exact extFoot_emptyE hw + · have hpq : ∀ x : U64, specPtr x = specPtr p → x.val = p.val := fun x h => by rw [specPtr_inj h] + rcases hq with ⟨q', hth, rfl⟩ | ⟨t, ht, rfl⟩ | ⟨c, hc, rfl⟩ | ⟨v, hv, -⟩ | ⟨x, hx, -⟩ | ⟨x, hx, -⟩ | + ⟨q', hq', rfl⟩ | ⟨q', hq', -⟩ | ⟨q', hq', -⟩ + · have he := hpq q' e + obtain ⟨k, hk⟩ := Option.ne_none_iff_exists'.1 hth + rcases w1ThA_cases hk with ⟨rfl, -⟩ | ⟨rfl, -⟩ | ⟨hF, -⟩ | hb0 + · rw [vRR] at he; rcases hpv with h | h | h | h <;> omega + · rw [vRT] at he; rcases hpv with h | h | h | h <;> omega + · unfold FrPg at hF; rcases hpv with h | h | h | h <;> omega + · have := (bootTh_ltA hL hK P hb0).1; rcases hpv with h | h | h | h <;> omega + · have he := hpq t e + rcases w1H_tcbs P ht with ⟨c, hc, e'⟩ | e' <;> rcases hpv with h | h | h | h <;> omega + · unfold scRenA at e + split_ifs at e + · have := specPtr_eq_idleSc.1 e.symm; rcases hpv with h | h | h | h <;> omega + · have he := hpq c e + rcases w1H_scs P hc with ⟨hb, -⟩ | ⟨e', -⟩ + · obtain ⟨c', hc', rfl⟩ := bootC_of (kb := kb) hL hb + have := idleSA_val' hL hc' + rcases hpv with h | h | h | h <;> omega + · rcases hpv with h | h | h | h <;> omega + · exact absurd hv (Set.notMem_empty _) + · exact absurd hx (Set.notMem_empty _) + · exact absurd hx (Set.notMem_empty _) + · have he := hpq q' e + have e' : q'.val = PLb := hq' + rcases hpv with h | h | h | h <;> omega + · exact absurd hq' (Set.notMem_empty _) + · exact absurd hq' (Set.notMem_empty _) + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/TabZeroA.lean b/proofs/Fl4ProofsA64/P3/TabZeroA.lean new file mode 100644 index 000000000..acaefdd68 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TabZeroA.lean @@ -0,0 +1,302 @@ +/- +P3 (aarch64, A64-P3 S3 gen 25, ruling 22:14Z item 2): **the table zeroing of `ARMPageTableUnmap`** — FL4's +`zero(ptr, 4096)` after `unmap_page_table` (`vspace.rs` `decode_page_table`) against the spec's +`mapMx (storePte t · InvalidPTE)` over the table's slots in `performExtVSpace (.tableUnmap …)` (`Ext/Stage2`), which +pins gen 24's existential slot list `zs`. The x86 counterpart is `P3/VsPtUnmap`'s `clearPt_eq` / `slots_eq`. + +* **`slotsA`**: the spec's slot list `uptoEnumStep p (p + 2^pteBits) (p + mask (ptBits normalPtT))` of the table at + `specPtr p` is the 512 words `specPtr (peAF p j)`, `j < 512`, in order. +* (`P3/ExtVsPerfA`'s `perform_tabUnmapZA` / `perform_tabUnmapXA` state the spec run over that list.) +* **`wrsA`**: FL4's 512 zero writes, one per word; `mem_wrsA`, and **`zr_wrsA`**: on aligned words FL4's `zero(p, + 4096)` is those writes. +* **`zeroStepsA`**: the zeroing one word at a time, the spec's `storePte` and FL4's write together, each step by the + Ext unmaps' slot writes (`writeS2E` for a Stage-2 table, `writeHiE` for a Stage-1 table below a window, both + `P3/ExtVsUnmapA`): the spec run has the single outcome `ptSetA σ (specPtr p) (ptClrA t js)` (never fails), and + `VsRelA` / `ExtVsRelA` hold after. +* **`zero_relA`**: for a table `p` of `Z.T2`, or of the Stage-1 tables below the root (depth ≥ 1): the spec's + zeroing over `slotsA`'s list is the single outcome `ptSetA σ (specPtr p) t'` with every entry of `t'` invalid (a + normal table), FL4's `zero(p, 4096)` leaves every word of `p` zero, and both VSpace relations hold after. +* **`JAtZAX.of_zeroA`**: `JAtZAX` across the zeroing, through `P3/FinVsXA`'s `JAtZAX.of_extPageA` (`TabOffA` / + `ExtTabOffA` keep the page off the rest of the relation). + +No named premise. Decode-site hypotheses: `TabOffA`, `ExtTabOffA` (read by `JAtZAX.of_zeroA`). Lemma hypotheses as +the Ext unmaps (`hK`, `hT : OffAsE`) and the table's membership `hp`. No existing statement changed. +-/ +import Fl4ProofsA64.P3.FinVsXA +import Fl4ProofsA64.P3.ExtVsPerfA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4Spec + +noncomputable section + +/-! ## The slot list -/ + +/-- The index `j` as an FL4 word. -/ +def ujA (j : Nat) : U64 := ⟨BitVec.ofNat 64 j⟩ + +theorem ujA_val {j : Nat} (hj : j < 512) : (ujA j).val = j := by + show (BitVec.ofNat 64 j).toNat = j + rw [BitVec.toNat_ofNat] + exact Nat.mod_eq_of_lt (by omega) + +theorem ujA_eq {i : U64} (hi : i.val < 512) : ujA i.val = i := UScalar.eq_of_val_eq (ujA_val hi) + +theorem slotBvA {x : BitVec 64} (hq : x.toNat % 4096 = 0) : + ¬ (x + 4095#64 < x) ∧ x + 4095#64 - x = 4095#64 ∧ x + 8#64 - x = 8#64 := by + refine ⟨?_, ?_, ?_⟩ <;> bv_omega + +theorem uptoEnum511A : Fl4SpecA64.uptoEnum (0 : BitVec 64) 511#64 = (List.range 512).map (BitVec.ofNat 64) := by + unfold Fl4SpecA64.uptoEnum + rw [show (0 : BitVec 64).toNat = 0 from rfl, show (511#64 : BitVec 64).toNat = 511 from rfl, List.range_eq_range'] + +theorem slotEltA {p : U64} (hp : p.val % 4096 = 0) {j : Nat} (hj : j < 512) : + specPtr p + BitVec.ofNat 64 j * 8#64 = specPtr (peAF p (ujA j)) := by + obtain ⟨e, hq⟩ := slot_toNatA hp (i := ujA j) (by rw [ujA_val hj]; exact hj) + have hlt := (specPtr p).isLt + apply BitVec.eq_of_toNat_eq + rw [e, ujA_val hj, BitVec.toNat_add, BitVec.toNat_mul, BitVec.toNat_ofNat, BitVec.toNat_ofNat] + simp only [Nat.reducePow, Nat.reduceMod] at hlt ⊢ + omega + +theorem slotConstA : ((1 : Fl4SpecA64.Word) <<< Fl4SpecA64.pteBits) = 8#64 ∧ + (Fl4SpecA64.mask (Fl4SpecA64.ptBits .normalPtT) : Fl4SpecA64.Word) = 4095#64 ∧ + (4095#64 : BitVec 64) / 8#64 = 511#64 := ⟨by decide, by decide, by decide⟩ + +/-- **The spec's slot list of a table** (module docstring; x86 `slots_eq`). -/ +theorem slotsA {p : U64} (hp : p.val % 4096 = 0) : + Fl4SpecA64.uptoEnumStep (specPtr p) (specPtr p + ((1 : Fl4SpecA64.Word) <<< Fl4SpecA64.pteBits)) + (specPtr p + Fl4SpecA64.mask (Fl4SpecA64.ptBits .normalPtT)) = + (List.range 512).map fun j => specPtr (peAF p (ujA j)) := by + obtain ⟨h1, h2, h3⟩ := slotBvA (slot_toNatA hp (i := 0#u64) (by decide)).2 + obtain ⟨e1, e2, e3⟩ := slotConstA + unfold Fl4SpecA64.uptoEnumStep + rw [e1, e2, if_neg h1, h2, h3, e3, uptoEnum511A, List.map_map] + exact List.map_congr_left fun j hj => slotEltA hp (List.mem_range.1 hj) + + +/-! ## The cleared table -/ + +/-- The table `t` with the entries `js` cleared, in order. -/ +def ptClrA (t : Fl4SpecA64.PT) : List Nat → Fl4SpecA64.PT + | [] => t + | j :: js => ptClrA (Fl4SpecA64.ptUpd t (ujA j).bv .invalidPTE) js + +theorem ptClr_typeA : ∀ (js : List Nat) (t : Fl4SpecA64.PT), Fl4SpecA64.ptType (ptClrA t js) = Fl4SpecA64.ptType t + | [], _ => rfl + | j :: js, t => by rw [ptClrA, ptClr_typeA js, ptUpd_typeA] + +theorem ptClr_applyA {i : U64} (hi : i.val < 512) : ∀ (js : List Nat), (∀ j ∈ js, j < 512) → + ∀ t : Fl4SpecA64.PT, Fl4SpecA64.ptApply (ptClrA t js) i.bv = + if i.val ∈ js then .invalidPTE else Fl4SpecA64.ptApply t i.bv + | [], _, t => by simp only [ptClrA, List.not_mem_nil, if_false] + | j :: js, hjs, t => by + have hj : j < 512 := hjs j (List.mem_cons.2 (Or.inl rfl)) + rw [ptClrA, ptClr_applyA hi js (fun j' h => hjs j' (List.mem_cons.2 (Or.inr h))), + ptApply_updA t (by rw [ujA_val hj]; exact hj) hi] + by_cases h1 : i.val ∈ js + · rw [if_pos h1, if_pos (List.mem_cons.2 (Or.inr h1))] + · rw [if_neg h1] + by_cases h2 : i = ujA j + · rw [if_pos h2, if_pos (List.mem_cons.2 (Or.inl (by rw [h2, ujA_val hj])))] + · rw [if_neg h2, if_neg] + intro h + rcases List.mem_cons.1 h with e | e + · exact h2 (UScalar.eq_of_val_eq (by rw [ujA_val hj]; exact e)) + · exact h1 e + +theorem ptSetA_selfA {σ : Fl4SpecA64.State Unit} {q : Fl4SpecA64.ObjRef} {t : Fl4SpecA64.PT} + (hk : σ.kheap q = some (.archObj (.pageTable t))) : ptSetA σ q t = σ := by + have : Function.update σ.kheap q (some (.archObj (.pageTable t))) = σ.kheap := by + rw [Function.update_eq_self_iff]; exact hk.symm + unfold ptSetA; rw [this] + +theorem ptSetA_twiceA (σ : Fl4SpecA64.State Unit) (q : Fl4SpecA64.ObjRef) (t1 t2 : Fl4SpecA64.PT) : + ptSetA (ptSetA σ q t1) q t2 = ptSetA σ q t2 := by + unfold ptSetA; simp only [Function.update_idem] + +/-! ## FL4's zeroing as 512 writes -/ + +/-- The zero writes at the words `js` of the page `p`, in order. -/ +def wrsA {S : Type} (wr : S → U64 → U64 → S) (s : S) (p : U64) : List Nat → S + | [] => s + | j :: js => wrsA wr (wr s (peAF p (ujA j)) 0#u64) p js + +section store +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +theorem ksb_wrsA (p : U64) : ∀ (js : List Nat) (s : S), L.ksb (wrsA L.wr s p js) = L.ksb s + | [], _ => rfl + | _ :: js, s => by rw [wrsA, ksb_wrsA p js, L.ksb_wr] + +theorem cpu_wrsA (p : U64) : ∀ (js : List Nat) (s : S), L.cpu (wrsA L.wr s p js) = L.cpu s + | [], _ => rfl + | _ :: js, s => by rw [wrsA, cpu_wrsA p js, L.cpu_wr] + +theorem mem_wrsA {p : U64} (hp : p.val % 4096 = 0) {b : U64} (hb : Aligned b) : + ∀ (js : List Nat), (∀ j ∈ js, j < 512) → ∀ s : S, + L.mem (wrsA L.wr s p js) b = if ∃ j ∈ js, peAF p (ujA j) = b then 0#u64 else L.mem s b + | [], _, s => by simp only [wrsA, List.not_mem_nil, false_and, exists_false, if_false] + | j :: js, hjs, s => by + have hj : j < 512 := hjs j (List.mem_cons.2 (Or.inl rfl)) + rw [wrsA, mem_wrsA hp hb js (fun j' h => hjs j' (List.mem_cons.2 (Or.inr h))), + L.mem_wr _ _ _ _ (peAF_alignedA hp (by rw [ujA_val hj]; exact hj)) hb] + by_cases h1 : ∃ j' ∈ js, peAF p (ujA j') = b + · obtain ⟨j', hj', e⟩ := h1 + rw [if_pos ⟨j', hj', e⟩, if_pos ⟨j', List.mem_cons.2 (Or.inr hj'), e⟩] + · rw [if_neg h1] + by_cases h2 : peAF p (ujA j) = b + · rw [if_pos h2, if_pos ⟨j, List.mem_cons.2 (Or.inl rfl), h2⟩] + · rw [if_neg h2, if_neg] + rintro ⟨j', hj', e⟩ + rcases List.mem_cons.1 hj' with rfl | h + · exact h2 e + · exact h1 ⟨j', h, e⟩ + +/-- **On aligned words, FL4's `zero(p, 4096)` is the 512 zero writes** (module docstring). -/ +theorem zr_wrsA {p : U64} (hp : p.val % 4096 = 0) (s : S) {b : U64} (hb : Aligned b) : + L.mem (L.zr s p 4096#u64) b = L.mem (wrsA L.wr s p (List.range 512)) b := by + have hpl : p.val < 2 ^ 64 := p.bv.isLt + have hb8 : b.val % 8 = 0 := hb + have h4 : (4096#u64 : U64).val = 4096 := rfl + have hfit : p.val + 4096 ≤ 2 ^ 64 := by simp only [Nat.reducePow] at hpl ⊢; omega + rw [L.mem_zr s p 4096#u64 b (show p.val % 8 = 0 by omega) (by simp) (by rw [h4]; exact hfit) hb, + mem_wrsA hp hb _ (fun j h => List.mem_range.1 h), h4] + by_cases hc : p.val ≤ b.val ∧ b.val < p.val + 4096 + · rw [if_pos hc, if_pos] + refine ⟨(b.val - p.val) / 8, List.mem_range.2 (by omega), UScalar.eq_of_val_eq ?_⟩ + rw [peAF_valA hfit (by rw [ujA_val (by omega)]; omega), ujA_val (by omega)] + omega + · rw [if_neg hc, if_neg] + rintro ⟨j, hj, rfl⟩ + have hj' := List.mem_range.1 hj + rw [peAF_valA hfit (by rw [ujA_val hj']; exact hj'), ujA_val hj'] at hc + omega + +/-! ## The zeroing, step by step -/ + +variable {Pp Tb : Set U64} {Lv : U64 → Nat} {X Y T2 : Set U64} {Sh2 : U64 → Nat} + +/-- **The zeroing one word at a time** (module docstring), given the single slot write `hwr`. -/ +theorem zeroStepsA {p : U64} (hp : p.val % 4096 = 0) (kb : U64) + (hwr : ∀ (s : S) (σ : Fl4SpecA64.State Unit) (i : U64), i.val < 512 → L.ksb s = kb → + VsRelA Pp Tb Lv X Y (L.mem s) (L.ksb s) σ → ExtVsRelA Pp Tb X Y T2 Sh2 (L.mem s) σ → + VsRelA Pp Tb Lv X Y (L.mem (L.wr s (peAF p i) 0#u64)) (L.ksb (L.wr s (peAF p i) 0#u64)) + (extSlotWriteX σ (.pt (specPtr (peAF p i))) .invalidPTE) ∧ + ExtVsRelA Pp Tb X Y T2 Sh2 (L.mem (L.wr s (peAF p i) 0#u64)) + (extSlotWriteX σ (.pt (specPtr (peAF p i))) .invalidPTE)) : + ∀ js : List Nat, (∀ j ∈ js, j < 512) → ∀ (s : S) (σ : Fl4SpecA64.State Unit) (t : Fl4SpecA64.PT), + L.ksb s = kb → σ.kheap (specPtr p) = some (.archObj (.pageTable t)) → Fl4SpecA64.ptType t = .normalPtT → + VsRelA Pp Tb Lv X Y (L.mem s) (L.ksb s) σ → ExtVsRelA Pp Tb X Y T2 Sh2 (L.mem s) σ → + NonDet.mapMx (fun sl => Fl4SpecA64.storePte (Z := Unit) .normalPtT sl .invalidPTE) + (js.map fun j => specPtr (peAF p (ujA j))) σ = ({((), ptSetA σ (specPtr p) (ptClrA t js))}, False) ∧ + VsRelA Pp Tb Lv X Y (L.mem (wrsA L.wr s p js)) (L.ksb (wrsA L.wr s p js)) (ptSetA σ (specPtr p) (ptClrA t js)) ∧ + ExtVsRelA Pp Tb X Y T2 Sh2 (L.mem (wrsA L.wr s p js)) (ptSetA σ (specPtr p) (ptClrA t js)) + | [], _, s, σ, t, _, hk, _, R, E => by + simp only [ptClrA, wrsA, List.map_nil, ptSetA_selfA hk] + exact ⟨rfl, R, E⟩ + | j :: js, hjs, s, σ, t, hks, hk, ht, R, E => by + have hj : j < 512 := hjs j (List.mem_cons.2 (Or.inl rfl)) + have hi : (ujA j).val < 512 := by rw [ujA_val hj]; exact hj + have hst := storePte_runA hk ht hp hi .invalidPTE + obtain ⟨R1, E1⟩ := hwr s σ (ujA j) hi hks R E + rw [extPtSetE hk hp hi] at R1 E1 + obtain ⟨hrun, R2, E2⟩ := zeroStepsA hp kb hwr js (fun j' h => hjs j' (List.mem_cons.2 (Or.inr h))) + (L.wr s (peAF p (ujA j)) 0#u64) _ (Fl4SpecA64.ptUpd t (ujA j).bv .invalidPTE) + (by rw [L.ksb_wr]; exact hks) (by rw [ptSetA_kheap, if_pos rfl]) (by rw [ptUpd_typeA]; exact ht) R1 E1 + rw [ptSetA_twiceA] at hrun R2 E2 + refine ⟨?_, R2, E2⟩ + rw [List.map_cons, mapMx_cons_oneA (g := fun sl => Fl4SpecA64.storePte (Z := Unit) .normalPtT sl .invalidPTE) hst] + exact hrun + +/-- **The table zeroing** (module docstring): the spec's run over `slotsA`'s list, FL4's `zero(p, 4096)`, both +VSpace relations after. -/ +theorem zero_relA {s : S} {σ : Fl4SpecA64.State Unit} (R : VsRelA Pp Tb Lv X Y (L.mem s) (L.ksb s) σ) + (E : ExtVsRelA Pp Tb X Y T2 Sh2 (L.mem s) σ) (hK : Aligned (L.ksb s)) (hT : OffAsE Pp Tb X Y T2 (L.ksb s)) + {p : U64} (hp : (p ∈ Tb ∧ 1 ≤ Lv p) ∨ p ∈ T2) : + ∃ t', Fl4SpecA64.ptType t' = .normalPtT ∧ (∀ i : U64, i.val < 512 → Fl4SpecA64.ptApply t' i.bv = .invalidPTE) ∧ + NonDet.mapMx (fun sl => Fl4SpecA64.storePte (Z := Unit) .normalPtT sl .invalidPTE) + (Fl4SpecA64.uptoEnumStep (specPtr p) (specPtr p + ((1 : Fl4SpecA64.Word) <<< Fl4SpecA64.pteBits)) + (specPtr p + Fl4SpecA64.mask (Fl4SpecA64.ptBits .normalPtT))) σ = + ({((), ptSetA σ (specPtr p) t')}, False) ∧ + (∀ i : U64, i.val < 512 → L.mem (L.zr s p 4096#u64) (peAF p i) = 0#u64) ∧ + VsRelA Pp Tb Lv X Y (L.mem (L.zr s p 4096#u64)) (L.ksb (L.zr s p 4096#u64)) (ptSetA σ (specPtr p) t') ∧ + ExtVsRelA Pp Tb X Y T2 Sh2 (L.mem (L.zr s p 4096#u64)) (ptSetA σ (specPtr p) t') := by + have hp4 : p.val % 4096 = 0 := by + rcases hp with ⟨h, -⟩ | h + · exact R.al_mod h + · exact Nat.mod_eq_zero_of_dvd (E.tal p h) + obtain ⟨t, hk, ht⟩ : ∃ t, σ.kheap (specPtr p) = some (.archObj (.pageTable t)) ∧ + Fl4SpecA64.ptType t = .normalPtT := by + rcases hp with ⟨h, h1⟩ | h + · obtain ⟨t, hk, ht, -⟩ := R.pt p h + refine ⟨t, hk, ?_⟩ + rw [ht]; unfold ptTyA; rw [if_neg (by omega)] + · obtain ⟨t, hk, ht, -⟩ := E.s2pt p h + exact ⟨t, hk, ht⟩ + have hwr : ∀ (s' : S) (σ' : Fl4SpecA64.State Unit) (i : U64), i.val < 512 → L.ksb s' = L.ksb s → + VsRelA Pp Tb Lv X Y (L.mem s') (L.ksb s') σ' → ExtVsRelA Pp Tb X Y T2 Sh2 (L.mem s') σ' → + VsRelA Pp Tb Lv X Y (L.mem (L.wr s' (peAF p i) 0#u64)) (L.ksb (L.wr s' (peAF p i) 0#u64)) + (extSlotWriteX σ' (.pt (specPtr (peAF p i))) .invalidPTE) ∧ + ExtVsRelA Pp Tb X Y T2 Sh2 (L.mem (L.wr s' (peAF p i) 0#u64)) + (extSlotWriteX σ' (.pt (specPtr (peAF p i))) .invalidPTE) := by + intro s' σ' i hi hks R' E' + have hK' : Aligned (L.ksb s') := by rw [hks]; exact hK + have hT' : OffAsE Pp Tb X Y T2 (L.ksb s') := by rw [hks]; exact hT + rcases hp with ⟨h, h1⟩ | h + · exact writeHiE R' E' hK' hT' (sh := 39 - 9 * Lv p) (Or.inl ⟨p, i, ⟨h, h1, rfl⟩, hi, rfl, rfl⟩) + · exact writeS2E R' E' hK' hT' (sh := Sh2 p) (Or.inl ⟨p, i, ⟨h, rfl⟩, hi, rfl, rfl⟩) + obtain ⟨hrun, R2, E2⟩ := zeroStepsA hp4 (L.ksb s) hwr (List.range 512) (fun j h => List.mem_range.1 h) s σ t rfl + hk ht R E + have hks : L.ksb (L.zr s p 4096#u64) = L.ksb (wrsA L.wr s p (List.range 512)) := by + rw [L.ksb_zr, ksb_wrsA] + have hK2 : Aligned (L.ksb (wrsA L.wr s p (List.range 512))) := by rw [ksb_wrsA]; exact hK + refine ⟨ptClrA t (List.range 512), by rw [ptClr_typeA]; exact ht, fun i hi => ?_, by rw [slotsA hp4]; exact hrun, + fun i hi => ?_, ?_, ?_⟩ + · rw [ptClr_applyA hi _ (fun j h => List.mem_range.1 h), if_pos (List.mem_range.2 hi)] + · rw [zr_wrsA hp4 s (peAF_alignedA hp4 hi), mem_wrsA hp4 (peAF_alignedA hp4 hi) _ (fun j h => List.mem_range.1 h), + if_pos ⟨i.val, List.mem_range.2 hi, by rw [ujA_eq hi]⟩] + · rw [hks] + exact R2.of_mem fun b hb => zr_wrsA hp4 s (R2.foot_alignedA hK2 b hb) + · exact E2.of_mem fun b hb => zr_wrsA hp4 s (E2.foot_alignedE R2.yal b hb) + +end store + +/-! ## `JAtZAX` across the zeroing -/ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} + +theorem ExtTabOffA.of_ksA {s' : S} (T : ExtTabOffA (L := L) Z m s) (hks : L.ksb s' = L.ksb s) + (hcpu : L.cpu s' = L.cpu s) : ExtTabOffA (L := L) Z m s' := + ⟨fun q hq i hi h => T.mem q hq i hi (FootA.of_ksA hks.symm hcpu.symm h), T.obj⟩ + +/-- **`JAtZAX` across the table zeroing** (module docstring). -/ +theorem JAtZAX.of_zeroA (J : JAtZAX (L := L) AR Z s a σ m) (hfa : ∀ w, FootA (L := L) Z s w → Aligned w) + (hTO : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s q) (hEO : ExtTabOffA (L := L) Z m s) {p : U64} + (hp : p ∈ Z.S1 ∨ p ∈ Z.T2) (hp4 : p.val % 4096 = 0) {t' : Fl4SpecA64.PT} + (VR : VsRelA Z.Pp Z.S1 Z.Lv Z.X Z.Y (L.mem (L.zr s p 4096#u64)) (L.ksb (L.zr s p 4096#u64)) + (ptSetA σ (specPtr p) t')) + (ER : ExtVsRelA Z.Pp Z.S1 Z.X Z.Y Z.T2 Z.Sh2 (L.mem (L.zr s p 4096#u64)) (ptSetA σ (specPtr p) t')) : + JAtZAX (L := L) AR Z (L.zr s p 4096#u64) a (ptSetA σ (specPtr p) t') m := by + refine J.of_extPageA (q := p) (p0 := p) (kh := Function.update σ.kheap (specPtr p) (some (.archObj (.pageTable t')))) + hfa hTO hEO ?_ (L.ksb_zr _ _ _) (L.cpu_zr _ _ _) (fun w hw hn => ?_) ?_ + (fun q hq => Function.update_of_ne hq _ _) VR ER + · rcases hp with h | h + · exact Or.inr (Or.inr h) + · exact Or.inl (Or.inl h) + · rw [zr_wrsA hp4 s hw, mem_wrsA hp4 hw _ (fun j h => List.mem_range.1 h), if_neg] + rintro ⟨j, hj, e⟩ + exact hn (ujA j) (by rw [ujA_val (List.mem_range.1 hj)]; exact List.mem_range.1 hj) e.symm + · rcases hp with h | h + · exact Or.inr (Or.inr (Or.inr h)) + · exact Or.inr (Or.inr (Or.inl h)) + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/TcbArchKeepsA.lean b/proofs/Fl4ProofsA64/P3/TcbArchKeepsA.lean new file mode 100644 index 000000000..9013956cf --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbArchKeepsA.lean @@ -0,0 +1,24 @@ +/- +P3 (aarch64, A64-P3 Stage 3, Lift 3): **the TCB-invocation arch keep bundle**. + +Definitions only. `[P] TcbArchKeepsA ar AR`: the pure-arch calls that the TCB invocations make (SetPACKeys' +`next_pac_epoch`, the FPU calls of ReadFPU / WriteFPU and of SetFlags' FPU-disable) keep the hardware-root relation +`AR · h`, in the style of `Vcpu.VcpuArchKeepsA`. Lifts take it as a property quantified over the arch op, never as a +fact at a post-state. +New module. +-/ +import Fl4ProofsA64.P3.JointA + +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3 + +/-- `[P]` The arch calls of the TCB invocations keep the hardware-root relation `AR · h`. -/ +structure TcbArchKeepsA {A : Type} (ar : machine.Arch A) (AR : A → HwRootA → Prop) : Prop where + next_pac_epoch : ∀ a h, AR a h → ar.next_pac_epoch a ⦃ r => AR r.2 h ⦄ + fpu_flush : ∀ a h t, AR a h → ar.fpu_flush a t ⦃ a' => AR a' h ⦄ + fpu_sanitise : ∀ a h t, AR a h → ar.fpu_sanitise a t ⦃ a' => AR a' h ⦄ + fpu_reload : ∀ a h t, AR a h → ar.fpu_reload a t ⦃ a' => AR a' h ⦄ + fpu_release : ∀ a h t, AR a h → ar.fpu_release a t ⦃ a' => AR a' h ⦄ + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/TcbCancelRunA.lean b/proofs/Fl4ProofsA64/P3/TcbCancelRunA.lean new file mode 100644 index 000000000..c4c86bbf4 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbCancelRunA.lean @@ -0,0 +1,38 @@ +/- +P3 (AArch64): `vcpu::cancel_run` under the frozen no-live-run selector. + +[S] (TCB contract, "Quiet local view and S4"): a suspended thread has no live run link, its +`RUN_VCPU` word is zero. Then FL4's `cancel_run` writes nothing and, with the joint VCPU +relation's `tcbRunVcpu = optRefA (RUN_VCPU)` (`VcpuCoreA.tcb`), l4v-side `fl4CancelRun` is the +identity: `cancel_run_quietA`, `fl4CancelRun_quietA`. The live branch is the S4 seam. +-/ +import Fl4ProofsA64.P3.ScFinishVcpuA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4SpecA64 Fl4Spec Sched + +noncomputable section + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +theorem cancel_run_quietA (s : S) (a : A) (t : U64) (h0 : L.mem s (runVcpuA t) = 0#u64) : + vcpu.cancel_run st ar s a t = ok (s, a) := by + have h0' : L.mem s (core.num.U64.wrapping_add t 3672#u64) = 0#u64 := h0 + unfold vcpu.cancel_run + rw [eq_of_spec_fd Fl4ProofsA64.P0.Consts.layout.tcb.RUN_VCPU.spec] + simp only [bind_ok, lift, L.read_eq, h0', bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + +end + +theorem fl4CancelRun_quietA {σ : State Unit} {t : ObjRef} {x : Tcb} + (hk : σ.kheap t = some (.tcb x)) (h : x.tcbExt.tcbRunVcpu = none) : + fl4CancelRun (Z := Unit) t σ = ({((), σ)}, False) := by + unfold fl4CancelRun getRunVcpu + rw [nd_bind_oneSc (threadGet_eqSc _ hk), h] + rfl + +end +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/TcbCopyViewA.lean b/proofs/Fl4ProofsA64/P3/TcbCopyViewA.lean new file mode 100644 index 000000000..6ed9fef93 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbCopyViewA.lean @@ -0,0 +1,192 @@ +/- +P3 (AArch64): the CopyRegisters transfer over the merged views. + +* **`tcbCopyRegisters_viewsTransferA`**: the frame and GP transfers (with the `FaultIP -> NextIP` + copy) refine the abstract transfer and keep every merged view. + +No named premise. +-/ +import Fl4ProofsA64.P3.TcbRegViewA +import Fl4ProofsA64.P3.TcbInvokeCopySpecA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4SpecA64 Fl4Spec Sched + +noncomputable section + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + +private theorem copyViews_appendA {α StateType : Type} (f : α → Fl4Spec.NonDet StateType Unit) (xs ys : List α) : + Fl4Spec.NonDet.mapMx f (xs ++ ys) = + (Fl4Spec.NonDet.mapMx f xs >>= fun _ => Fl4Spec.NonDet.mapMx f ys) := by + induction xs with + | nil => + change Fl4Spec.NonDet.mapMx f ys = + ((pure () : Fl4Spec.NonDet StateType Unit) >>= fun _ => Fl4Spec.NonDet.mapMx f ys) + rw [nd_pure_bindD] + | cons x xs ih => + funext state + change (f x >>= fun _ => Fl4Spec.NonDet.mapMx f (xs ++ ys)) state = + ((f x >>= fun _ => Fl4Spec.NonDet.mapMx f xs) >>= fun _ => Fl4Spec.NonDet.mapMx f ys) state + rw [ih, Sched.nd_bind_assocSc] + +private def CopyViewInv (st : machine.Store S) [L : LawfulStore st] (Z : XSetsA) (m : McState) + (κ : U64 → U64) (N E R : Set U64) (Bf : Set (U64 × Word)) (stack : U64 → List U64) (σ0 : State Unit) + (dest src p : U64) (rs : List Register) (i : Nat) (s : S) : Prop := + ∃ σ : State Unit, + ((), σ) ∈ (Fl4Spec.NonDet.mapMx (tcbCopyRegisterA (specPtr dest) (specPtr src) (p != 0#u64)) + (rs.take i) σ0).1 ∧ + TcbIpcViewsA st Z m κ N E R Bf stack s σ ∧ vcpu.bound st s dest = ok p + +private theorem tcbCopyViewStepA (hP : IPlacedA N Z.T E R Bf) (P : IpcSchedPlacementA Z.th N E R Bf) + {stack : U64 → List U64} {σ0 : State Unit} {dest src p : U64} (hd : dest ∈ Z.T) (hs : src ∈ Z.T) + (rs : List Register) (s : S) (j : U64) (hj : j.val < rs.length) + (hOld : CopyViewInv st Z m κ N E R Bf stack σ0 dest src p rs j.val s) : + (do + let r := regIdxA rs[j.val] + let raw ← obj.Tcb.reg st src s r + let v ← regs.sanitise_for_tcb st s dest r raw + obj.Tcb.set_reg st dest s r v) ⦃ s' => + CopyViewInv st Z m κ N E R Bf stack σ0 dest src p rs (j.val + 1) s' ⦄ := by + obtain ⟨σ, hRun, V, hVcpu⟩ := hOld + obtain ⟨xd, hkd, _⟩ := V.ep.tcb dest hd + obtain ⟨xs, hks, _⟩ := V.ep.tcb src hs + let r := rs[j.val] + let v : U64 := ⟨fl4SanitiseRegister (p != 0#u64) r (xs.tcbArch.tcbContext.userRegs r)⟩ + have O := schedOpsEAOfPlacementA (st := st) (m := m) (κ := κ) P + obtain ⟨hWrite, hSpec, -, -⟩ := tcbCopyRegister_stepA O hP.e V.ep V.rel hd hs hkd hks + (r := r) (guest := (p != 0#u64)) (fun _ => ⟨p, hVcpu, rfl⟩) + rw [hWrite, WP.spec_ok] + have V' := tcbRegWrite_viewsA hP P V hd hkd r v + have hNew : tcbCopyRegisterA (specPtr dest) (specPtr src) (p != 0#u64) r σ = + ({((), Notif.updK σ (specPtr dest) (.tcb (Notif.setReg xd r v.bv)))}, False) := hSpec + refine ⟨_, ?_, V', (tcbVcpuLinkRegFrameA s (hP.e.tcb dest hd) r v).trans hVcpu⟩ + rw [List.take_succ, List.getElem?_eq_getElem hj, Option.toList_some, copyViews_appendA] + refine mem_bind_ndD hRun ?_ + change _ ∈ ((tcbCopyRegisterA (specPtr dest) (specPtr src) (p != 0#u64) r >>= + fun _ => Fl4Spec.NonDet.pure ()) σ).1 + rw [Notif.nd_bind_one hNew] + rfl + +private theorem tcbCopyViews_frameA (hP : IPlacedA N Z.T E R Bf) (P : IpcSchedPlacementA Z.th N E R Bf) + {s : S} {σ : State Unit} {stack : U64 → List U64} + (V : TcbIpcViewsA st Z m κ N E R Bf stack s σ) {dest src p : U64} (hd : dest ∈ Z.T) (hs : src ∈ Z.T) + (hVcpu : vcpu.bound st s dest = ok p) : + tcb.copy_registers_loop0 st s dest src 0#u64 ⦃ s' => ∃ σ', + ((), σ') ∈ (Fl4Spec.NonDet.mapMx (tcbCopyRegisterA (specPtr dest) (specPtr src) (p != 0#u64)) + frameRegisters σ).1 ∧ + TcbIpcViewsA st Z m κ N E R Bf stack s' σ' ∧ vcpu.bound st s' dest = ok p ⦄ := by + have hStart : CopyViewInv st Z m κ N E R Bf stack σ dest src p frameRegisters 0 s := ⟨σ, rfl, V, hVcpu⟩ + refine WP.spec_mono (tcbCopyRegisters_frameLoopA + (I := fun n s' => CopyViewInv st Z m κ N E R Bf stack σ dest src p frameRegisters n s') + hStart (tcbCopyViewStepA hP P hd hs frameRegisters)) ?_ + rintro s' ⟨σ', hRun, V', hLink⟩ + rw [List.take_length] at hRun + exact ⟨σ', hRun, V', hLink⟩ + +private theorem tcbCopyViews_gpA (hP : IPlacedA N Z.T E R Bf) (P : IpcSchedPlacementA Z.th N E R Bf) + {s : S} {σ : State Unit} {stack : U64 → List U64} + (V : TcbIpcViewsA st Z m κ N E R Bf stack s σ) {dest src p : U64} (hd : dest ∈ Z.T) (hs : src ∈ Z.T) + (hVcpu : vcpu.bound st s dest = ok p) : + tcb.copy_registers_loop1 st s dest src 0#u64 ⦃ s' => ∃ σ', + ((), σ') ∈ (Fl4Spec.NonDet.mapMx (tcbCopyRegisterA (specPtr dest) (specPtr src) (p != 0#u64)) + gpRegisters σ).1 ∧ + TcbIpcViewsA st Z m κ N E R Bf stack s' σ' ∧ vcpu.bound st s' dest = ok p ⦄ := by + have hStart : CopyViewInv st Z m κ N E R Bf stack σ dest src p gpRegisters 0 s := ⟨σ, rfl, V, hVcpu⟩ + refine WP.spec_mono (tcbCopyRegisters_gpLoopA + (I := fun n s' => CopyViewInv st Z m κ N E R Bf stack σ dest src p gpRegisters n s') + hStart (tcbCopyViewStepA hP P hd hs gpRegisters)) ?_ + rintro s' ⟨σ', hRun, V', hLink⟩ + rw [List.take_length] at hRun + exact ⟨σ', hRun, V', hLink⟩ + +private theorem tcbCopyViews_framePcA (hP : IPlacedA N Z.T E R Bf) (P : IpcSchedPlacementA Z.th N E R Bf) + {s : S} {σ : State Unit} {stack : U64 → List U64} + (V : TcbIpcViewsA st Z m κ N E R Bf stack s σ) {dest src p : U64} (hd : dest ∈ Z.T) (hs : src ∈ Z.T) + (hVcpu : vcpu.bound st s dest = ok p) (frame : Bool) : + (if frame then do + let sFrame ← tcb.copy_registers_loop0 st s dest src 0#u64 + let pc := L.mem sFrame (regA dest layout.reg.FAULT_IP) + ok (L.wr sFrame (regA dest layout.reg.NEXT_IP) pc) + else ok s) ⦃ s' => ∃ σ', + ((), σ') ∈ (Fl4Spec.NonDet.when frame (do + Fl4Spec.NonDet.mapMx (tcbCopyRegisterA (specPtr dest) (specPtr src) (p != 0#u64)) frameRegisters + let pc ← asUser (specPtr dest) getRestartPC + asUser (specPtr dest) (setNextPC pc)) σ).1 ∧ + TcbIpcViewsA st Z m κ N E R Bf stack s' σ' ∧ vcpu.bound st s' dest = ok p ⦄ := by + by_cases hFrame : frame = true + · rw [if_pos hFrame] + refine WP.spec_bind (tcbCopyViews_frameA hP P V hd hs hVcpu) ?_ + rintro sF ⟨σF, hRun, VF, hLink⟩ + obtain ⟨x, hk, hx⟩ := VF.ep.tcb dest hd + let pc := L.mem sF (regA dest layout.reg.FAULT_IP) + have hPc : pc.bv = x.tcbArch.tcbContext.userRegs .faultIP := by + show (L.mem sF (regA dest layout.reg.FAULT_IP)).bv = _ + rw [← regIdxA_faultIP] + exact hx.regs .faultIP + have V1 := tcbRegWrite_viewsA hP P VF hd hk .nextIP pc + have hLink1 := (tcbVcpuLinkRegFrameA sF (hP.e.tcb dest hd) .nextIP pc).trans hLink + rw [regIdxA_nextIP] at V1 hLink1 + refine (WP.spec_ok _).2 ⟨_, ?_, V1, hLink1⟩ + unfold Fl4Spec.NonDet.when + rw [if_pos hFrame] + refine mem_bind_ndD hRun ?_ + have hSpec : (do + let word ← asUser (specPtr dest) getRestartPC + asUser (specPtr dest) (setNextPC word)) σF = + ({((), Notif.updK σF (specPtr dest) (.tcb (Notif.setReg x .nextIP pc.bv)))}, False) := by + change ((asUser (specPtr dest) (getRegister .faultIP)) >>= fun word => + asUser (specPtr dest) (setRegister .nextIP word)) σF = _ + rw [Notif.nd_bind_one (asUser_getRegister_eqA hk .faultIP), ← hPc] + exact Notif.asUser_setRegister_eq .nextIP pc.bv hk + rw [hSpec] + rfl + · rw [if_neg hFrame, WP.spec_ok] + refine ⟨σ, ?_, V, hVcpu⟩ + unfold Fl4Spec.NonDet.when + rw [if_neg hFrame] + rfl + +/-- **The CopyRegisters transfer keeps the merged views.** -/ +theorem tcbCopyRegisters_viewsTransferA (hP : IPlacedA N Z.T E R Bf) (P : IpcSchedPlacementA Z.th N E R Bf) + {s : S} {σ : State Unit} {stack : U64 → List U64} + (V : TcbIpcViewsA st Z m κ N E R Bf stack s σ) {dest src p : U64} (hd : dest ∈ Z.T) (hs : src ∈ Z.T) + (hVcpu : vcpu.bound st s dest = ok p) (frame gp : Bool) : + (do + let sFrame ← if frame then do + let s' ← tcb.copy_registers_loop0 st s dest src 0#u64 + let pc := L.mem s' (regA dest layout.reg.FAULT_IP) + ok (L.wr s' (regA dest layout.reg.NEXT_IP) pc) + else ok s + if gp then tcb.copy_registers_loop1 st sFrame dest src 0#u64 else ok sFrame) ⦃ s' => ∃ σ', + ((), σ') ∈ ((do + Fl4Spec.NonDet.when frame (do + Fl4Spec.NonDet.mapMx (tcbCopyRegisterA (specPtr dest) (specPtr src) (p != 0#u64)) frameRegisters + let pc ← asUser (specPtr dest) getRestartPC + asUser (specPtr dest) (setNextPC pc)) + Fl4Spec.NonDet.when gp + (Fl4Spec.NonDet.mapMx (tcbCopyRegisterA (specPtr dest) (specPtr src) (p != 0#u64)) gpRegisters)) σ).1 ∧ + TcbIpcViewsA st Z m κ N E R Bf stack s' σ' ⦄ := by + refine WP.spec_bind (tcbCopyViews_framePcA hP P V hd hs hVcpu frame) ?_ + rintro sF ⟨σF, hFrame, VF, hLink⟩ + by_cases hGp : gp = true + · rw [if_pos hGp] + refine WP.spec_mono (tcbCopyViews_gpA hP P VF hd hs hLink) ?_ + rintro s' ⟨σ', hRun, V', -⟩ + refine ⟨σ', mem_bind_ndD hFrame ?_, V'⟩ + unfold Fl4Spec.NonDet.when + rw [if_pos hGp] + exact hRun + · rw [if_neg hGp, WP.spec_ok] + refine ⟨σF, mem_bind_ndD hFrame ?_, VF⟩ + unfold Fl4Spec.NonDet.when + rw [if_neg hGp] + rfl + +end +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/TcbInactiveExA.lean b/proofs/Fl4ProofsA64/P3/TcbInactiveExA.lean new file mode 100644 index 000000000..71d658ba6 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbInactiveExA.lean @@ -0,0 +1,169 @@ +/- +P3 (AArch64): setting a TCB Inactive from the relaxed relation `SchedSRB … t`. + +* `SchedSRB.stableExBA`: `SchedSRA.stableExA` from `SchedSRB` (it reads only `SchedSRN`). +* **`set_thread_state_inactiveExA`**: FL4 `set_thread_state t TS_INACTIVE` of a TCB in no spec + queue, from `SchedSRB … t`, refines `setThreadState t .inactive` and closes back to `SchedSRA` + (`SchedSRB.toA_ncA`: an Inactive TCB is outside `inClassR`), with `StEqA` and the writes in + the state word and `SWrA`. + +No named premise. +-/ +import Fl4ProofsA64.P3.ReplyRunExA +import Fl4ProofsA64.P3.ReplyRemoveBorA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +theorem SchedSRB.stableExBA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s s' : S} + {σ σ' : Fl4SpecA64.State Unit} + {p0 : Fl4SpecA64.ObjRef} (H : SchedSRB st Z m κ s σ p0) {W : U64 → Prop} (hw : WritesIn st s s' W) (hW : ∀ a, W a → ¬ SReadA st Z m s σ a) + (hS : SStepA σ σ') (hrun : RunQA σ') {p : Fl4SpecA64.ObjRef} (hrd : RdExA σ' p) : + SchedSRB st Z m κ s' σ' p := by + have keep : ∀ a, Aligned a → SReadA st Z m s σ a → L.mem s' a = L.mem s a := + fun a ha hr => hw.mem_eq ha (fun h => hW a h hr) + have hT := H.core.tcbs + have tK : ∀ t ∈ Z.T, ∀ K : U64, (K = 3520#u64 ∨ K = 3448#u64 ∨ K = 3432#u64 ∨ K = 3464#u64 ∨ K = 3592#u64 ∨ + K = 3480#u64 ∨ K = 3488#u64) → L.mem s' (wa t K) = L.mem s (wa t K) := by + intro t ht K hK + refine keep _ ?_ (Or.inr (Or.inr (Or.inr (Or.inl ⟨t, ht, K, hK, rfl⟩)))) + rcases hK with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> exact field_aligned (hT t ht) (by simp) (by simp) + have nK : ∀ c ∈ Z.Cs, ∀ o : U64, o.val < 4288 → o.val % 8 = 0 → + L.mem s' (L.nodeAt s c o) = L.mem s (L.nodeAt s c o) := + fun c hc o ho h8 => keep _ (nodeAt_aligned (H.core.nodes c hc) ho h8) (Or.inl ⟨c, hc, o, ho, rfl⟩) + have hkb := H.ksb_al + have hf := H.ksFit + have gK : ∀ K : U64, (K = 8#u64 ∨ K = 16#u64) → L.mem s' (wa (L.ksb s) K) = L.mem s (wa (L.ksb s) K) := by + intro K hK + have hK' : K.val = 8 ∨ K.val = 16 := by rcases hK with rfl | rfl <;> simp + have hv : (wa (L.ksb s) K).val = (L.ksb s).val + K.val := by rw [wa_valm]; unfold ksSizeA at hf; omega + refine keep _ (by unfold Aligned; rw [hv]; omega) ?_ + rcases hK with rfl | rfl + · exact Or.inr (Or.inl rfl) + · exact Or.inr (Or.inr (Or.inl rfl)) + have hcore : SchedCoreA st Z.T Z.Cs s' Z.Q Z.R := + H.core.agree hw.1 (fun y hy => ⟨tK y hy 3480#u64 (by simp), tK y hy 3488#u64 (by simp)⟩) + (fun c hc o h1 h2 _ => nK c hc o h1 h2) (fun y hy => tK y hy 3592#u64 (by simp)) + (fun y hy => tK y hy 3520#u64 (by simp)) (fun y hy => tK y hy 3448#u64 (by simp)) + have hkc : ∀ c ∈ Z.C, σ'.kheap (scNameA m c) = σ.kheap (scNameA m c) := fun c hc => by + obtain ⟨x, n, hk, -⟩ := H.scr.sc c hc + exact hS.sc _ x n hk + exact ⟨H.toSchedSRN.rebuildQueues (Q' := Z.Q) hw.1 hw.2.1 (fun c hc o h1 h2 _ => nK c hc o (by omega) h2) gK + (fun t ht K hK => tK t ht K (by rcases hK with h | h | h | h <;> simp [h])) + (fun a ha hf => keep a ha (Or.inr (Or.inr (Or.inr (Or.inr hf))))) + hkc + (fun _ _ x hx => hS.tcb _ x hx) hS.fix + (by + rw [hS.act, nK _ H.sch.cpuCs layout.node.SCHED_ACTION (by simp [layout.node.SCHED_ACTION]) + (by simp [layout.node.SCHED_ACTION])] + exact H.sch.act) + (fun u hu => H.actOn u (by rw [← hS.act]; exact hu)) (H.inv.step hS) + hcore (fun p hp => by rw [hS.rq]; exact H.rest.rq p hp) (fun d p hd => by rw [hS.rq]; exact H.rest.rqDom d p hd) + hS.rel hrun, hrd⟩ + +end + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +open Fl4SpecA64 + +/-- **Inactive from `SchedSRB` at the TCB** (module docstring). -/ +theorem set_thread_state_inactiveExA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + {t : U64} (H : SchedSRB st Z m κ s σ (specPtr t)) (hst : StEqA Z.T (L.mem s) σ) (ht : t ∈ Z.T) + {x : Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hnq : (∀ d p, specPtr t ∉ σ.readyQueues d p) ∧ specPtr t ∉ σ.releaseQueue) : + sched.set_thread_state st s t sched.TS_INACTIVE ⦃ s' => ∃ σ', + setThreadState (Z := Unit) (specPtr t) .inactive σ = ({((), σ')}, False) ∧ + σ'.kheap = (updKSc σ (specPtr t) (.tcb { x with tcbState := .inactive })).kheap ∧ + SchedSRA st Z m κ s' σ' ∧ StEqA Z.T (L.mem s') σ' ∧ + WritesIn st s s' (fun b => b = stA t ∨ b = L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) ⦄ := by + let σ1 := updKSc σ (specPtr t) (.tcb { x with tcbState := .inactive }) + have hh : σ1.kheap (specPtr t) = some (.tcb { x with tcbState := .inactive }) := by simp [σ1, updKSc] + have far : ∀ q, q ≠ specPtr t → σ1.kheap q = σ.kheap q := fun q hq => by simp [σ1, updKSc, hq] + have hal : Aligned (stA t) := by + rw [stA_eq] + exact field_aligned (H.core.tcbs t ht) (by simp) (by simp) + have code : sched.TS_INACTIVE.val = tsCodeA .inactive := by unfold sched.TS_INACTIVE; rfl + have w1 : WritesIn st s (L.wr s (stA t) sched.TS_INACTIVE) (· = stA t) := writesIn_wr hal + have hS : SStepA σ σ1 := by + refine ⟨⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩, rfl, rfl, rfl, + fun q y n hq => far q (fun e => by rw [e, hk] at hq; cases hq), + fun q y hq => ?_, fun q y n hq => (far q (fun e => by rw [e, hh] at hq; cases hq)).symm, + fun q y hq => ?_, fun q y hq => ?_⟩ + · by_cases e : q = specPtr t + · subst q + rw [hk] at hq + cases hq + exact ⟨_, hh, rfl, rfl, rfl⟩ + · exact ⟨y, (far _ e).trans hq, rfl, rfl, rfl⟩ + · by_cases e : q = specPtr t + · exact ⟨x, e.symm ▸ hk⟩ + · exact ⟨y, (far _ e).symm.trans hq⟩ + · have hq' := far q (fun e => by rw [e, hh] at hq; cases hq) + exact ⟨y, hq'.symm.trans hq, rfl⟩ + have k : ∀ u, u ≠ specPtr t → (∃ y, σ.kheap u = some (.tcb y) ∧ runnable y.tcbState = true) → + ∃ y, σ1.kheap u = some (.tcb y) ∧ runnable y.tcbState = true := by + rintro u hu ⟨y, hy, hr⟩ + exact ⟨y, (far _ hu).trans hy, hr⟩ + have hrun : RunQA σ1 := ⟨fun d q u hu => k u (fun e => hnq.1 d q (e ▸ hu)) (H.run.1 d q u hu), + fun u hu => k u (fun e => hnq.2 (e ▸ hu)) (H.run.2 u hu)⟩ + have hrd : RdExA σ1 (specPtr t) := by + intro u y hu hy hc + have hy0 : σ.kheap u = some (.tcb y) := (far _ hu).symm.trans hy + exact (H.rdEx u y hu hy0 hc).step hS hy0 + have H1 : SchedSRB st Z m κ (L.wr s (stA t) sched.TS_INACTIVE) σ1 (specPtr t) := + H.stableExBA w1 (fun _ e => e ▸ stateOffExA H.toSchedSRN ht) hS hrun hrd + have HA : SchedSRA st Z m κ (L.wr s (stA t) sched.TS_INACTIVE) σ1 := + H1.toA_ncA hh (by simp [inClassRSc, runnable]) + have ht1 : StEqA Z.T (L.mem (L.wr s (stA t) sched.TS_INACTIVE)) σ1 := by + intro u hu y hy + by_cases e : u = t + · subst u + rw [hh] at hy + cases hy + rw [L.mem_wr_same hal] + exact code + · have hne : specPtr u ≠ specPtr t := fun eq => e (specPtr_inj eq) + have hy0 : σ.kheap (specPtr u) = some (.tcb y) := (far _ hne).symm.trans hy + have hne' : stA u ≠ stA t := by + rw [stA_eq, stA_eq] + exact field_ne (H.core.tcbs u hu) (H.core.tcbs t ht) (by simp) (by simp) (fun e' => absurd e' e) + have ha : Aligned (stA u) := by + rw [stA_eq] + exact field_aligned (H.core.tcbs u hu) (by simp) (by simp) + rw [w1.mem_eq ha hne'] + exact hst u hu y hy0 + unfold sched.set_thread_state + refine WP.spec_bind (obj.Tcb.set_state.spec s t sched.TS_INACTIVE) ?_ + intro s₂ hs₂ + rw [hs₂] + refine WP.spec_mono (schedule_tcb_refinesA HA t ht1) ?_ + rintro s' ⟨w2, H2⟩ + have hk' : (stsActA σ1 (specPtr t)).kheap = σ1.kheap := by unfold stsActA; split <;> rfl + refine ⟨stsActA σ1 (specPtr t), setThreadState_stsActExA hk _, hk', H2, ?_, ?_⟩ + · intro u hu y hy + rw [hk'] at hy + have al : Aligned (stA u) := by + rw [stA_eq] + exact field_aligned (H.core.tcbs u hu) (by simp) (by simp) + have hr1 := stateOffExA HA.toSchedSRN hu + have hne : stA u ≠ L.nodeAt (L.wr s (stA t) sched.TS_INACTIVE) (L.cpu (L.wr s (stA t) sched.TS_INACTIVE)) + layout.node.SCHED_ACTION := by + intro e + exact hr1 (Or.inl ⟨_, HA.sch.cpuCs, _, by simp [layout.node.SCHED_ACTION], e⟩) + rw [w2.mem_eq al hne] + exact ht1 u hu y hy + · refine (w1.trans w2).mono fun b h => h.elim Or.inl fun e => Or.inr ?_ + rw [e, nodeAt_of_ksb w1.1, w1.2.1] + +end +end +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/TcbInvokeBufferA.lean b/proofs/Fl4ProofsA64/P3/TcbInvokeBufferA.lean new file mode 100644 index 000000000..5e31c3f39 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbInvokeBufferA.lean @@ -0,0 +1,100 @@ +/- +P3 (AArch64): SetIPCBuffer derivation, accepted dispatch, and local install. +[S] The dispatch stall is quiet. +[J] The live checks use the named state after slot deletion and buffer write. +-/ +import Fl4ProofsA64.P3.TcbInvokeTimeoutA +import Fl4ProofsA64.P1.MdbIpcBuf + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 + +section +variable {S A : Type} {st : machine.Store S} [L : LawfulStore st] {ar : machine.Arch A} +variable {s sRestart : S} {a : A} {t baddr src : U64} {ctx : tcb.Ctx} + +set_option trace.profiler true in +/-- A zero buffer still reads the extra slot. It selects a null source +without deriving the cap so the installation clears the old buffer slot. -/ +theorem tcbDeriveBuffer_zeroA {cap : capability.Cap} (hc : obj.Slot.cap st src s = ok cap) : + tcb.derive_for_buffer st s 0#u64 src = ok (.Ok (cap, 0#u64)) := by + simp only [tcb.derive_for_buffer, if_pos rfl, ite_true, hc, bind_ok] + +set_option trace.profiler true in +/-- For a nonzero address, the concrete validity guard is applied to the +derived native, unmapped frame, even when the source frame was Stage 2. -/ +theorem tcbDeriveBuffer_frameA {p rights asid va : U64} {size : capability.FrameSize} + {dev mapped stage2 : Bool} (hn : baddr ≠ 0#u64) + (hc : obj.Slot.cap st src s = ok (.Frame p size rights dev mapped stage2 asid va)) : + tcb.derive_for_buffer st s baddr src = (do + let frame := capability.Cap.Frame p size rights dev false false 0#u64 0#u64 + let r ← vspace.check_valid_ipc_buffer baddr frame + match r with + | .Ok _ => ok (.Ok (frame, src)) + | .Err e => ok (.Err e)) := by + simp only [tcb.derive_for_buffer, if_neg hn, hc, bind_ok, objects.derive_cap, + core.result.Result.Insts.CoreOpsTry.branch] + congr 1 + funext r + cases r <;> simp only [bind_ok, Std.bind_tc_ok, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from] + +set_option trace.profiler true in +/-- A successful decode performs Restart and installs the selected buffer. +A full install error is returned with the state it reached. -/ +theorem tcbSetIPCBuffer_selectedA {sel : capability.Cap × obj.Slot} + (hStall : smp.remote_tcb_stall st ar s a t = ok (s, a)) + (hExtra : tcb.extra_with_length ctx 0#usize 1#u64 = ok (.Ok src)) + (hArg : tcb.arg st s ctx 0#u64 = ok baddr) + (hDerive : tcb.derive_for_buffer st s baddr src = ok (.Ok sel)) + (hRestart : tcb.restart_caller st s = ok sRestart) : + tcb.decode st ar s a fl4_abi.label.TCBSetIPCBuffer t ctx = + tcb.install_buffer st ar sRestart a t ctx.slot baddr sel := by + rw [tcbSetIPCBuffer_programA hStall] + simp only [tcb.decode_set_ipc_buffer, hExtra, hArg, hDerive, hRestart, + core.result.Result.Insts.CoreOpsTry.branch, bind_ok] + have h1 : ((tcb.UPDATE_IPC_BUFFER &&& tcb.UPDATE_FAULT) != 0#u64) = false := by + rw [tcb.UPDATE_IPC_BUFFER, tcb.UPDATE_FAULT]; decide + have h2 : ((tcb.UPDATE_IPC_BUFFER &&& tcb.UPDATE_TIMEOUT) != 0#u64) = false := by + rw [tcb.UPDATE_IPC_BUFFER, tcb.UPDATE_TIMEOUT]; decide + have h3 : ((tcb.UPDATE_IPC_BUFFER &&& tcb.UPDATE_SPACE) != 0#u64) = false := by + rw [tcb.UPDATE_IPC_BUFFER, tcb.UPDATE_SPACE]; decide + have h4 : ((tcb.UPDATE_IPC_BUFFER &&& tcb.UPDATE_IPC_BUFFER) != 0#u64) = true := by + rw [tcb.UPDATE_IPC_BUFFER]; decide + simp only [tcb.thread_control_caps, lift, bind_ok, h1, h2, h3, h4, + Bool.false_eq_true, ite_false, ite_true] + calc + _ = Aeneas.Std.bind (tcb.install_buffer st ar sRestart a t ctx.slot baddr sel) ok := by + change Aeneas.Std.bind (tcb.install_buffer st ar sRestart a t ctx.slot baddr sel) _ = _ + congr 1 + funext r + rcases r with ⟨r, sFinal, aFinal⟩ + exact tcbCapControl_returnA r sFinal aFinal + _ = _ := Std.bind_pure _ + +set_option trace.profiler true in +/-- With source zero the successful clear writes the raw zero buffer +address after deletion and skips both live cap reads and insertion. -/ +theorem tcbInstallBuffer_clearA {sDeleted sBuffer : S} {aDeleted : A} + {invSlot dest : U64} {cap : capability.Cap} + (hSlot : obj.Tcb.slot t layout.tcb.SLOT_BUFFER = ok dest) + (hDelete : cdt.delete st ar s a dest true = ok (.Ok (), sDeleted, aDeleted)) + (hBuffer : obj.Tcb.set_ipc_buffer st t sDeleted 0#u64 = ok sBuffer) : + tcb.install_buffer st ar s a t invSlot 0#u64 (cap, 0#u64) = (do + let sFinal ← if t = L.curW sBuffer then sched.reschedule_required st sBuffer else ok sBuffer + ok (.Ok (), sFinal, aDeleted)) := by + have h0 : obj.Slot.is_null (0#u64) = ok true := rfl + rw [tcb.install_buffer] + simp only [hSlot, hDelete, bind_ok, uncurry_apply_pair, + core.result.Result.Insts.CoreOpsTry.branch, hBuffer] + simp only [tcb.still_valid, h0, bind_ok, ite_true, Bool.false_eq_true, ite_false, + cur_thread_ok, obj.Tcb.Insts.CoreCmpPartialEqTcb.eq, decide_eq_true_eq] + by_cases h : t = L.curW sBuffer + · simp only [if_pos h, Std.bind_assoc, bind_ok] + · simp only [if_neg h, bind_ok] + +end +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/TcbInvokeBufferCheckA.lean b/proofs/Fl4ProofsA64/P3/TcbInvokeBufferCheckA.lean new file mode 100644 index 000000000..1a684b7d6 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbInvokeBufferCheckA.lean @@ -0,0 +1,114 @@ +/- +P3 (AArch64): check an already derived native IPC-buffer cap, and follow +the live insertion through the same conditional reschedule as a clear. +[J] Deletion, the buffer-address write and live cap checks supply the named +intermediate states. The staged slot install still uses the shared CDT rule. +-/ +import Fl4ProofsA64.P3.TcbInvokeBufferA +import Fl4ProofsA64.P3.EpBufA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4SpecA64 Classical + +theorem tcbBufferAlignBitsA : msgAlignBits = 10 := by + unfold msgAlignBits + have h : Nat.find (⟨7, by decide⟩ : ∃ n, maxIpcLength ≤ 2 ^ n) = 7 := by + rw [Nat.find_eq_iff] + refine ⟨by decide, fun n hn => ?_⟩ + interval_cases n <;> decide + rw [h] + rfl + +theorem tcbBufferAlignedA (addr : U64) : + isAligned addr.bv msgAlignBits ↔ (addr &&& 1023#u64) = 0#u64 := by + rw [tcbBufferAlignBitsA, isAligned] + have h : (addr &&& 1023#u64).val = addr.val % 1024 := by + change (addr.bv &&& (1023 : Word)).toNat = addr.bv.toNat % 1024 + rw [BitVec.toNat_and, show (1023 : Word).toNat = 2 ^ 10 - 1 by rfl, + Nat.and_two_pow_sub_one_eq_mod] + exact ⟨fun e => UScalar.eq_of_val_eq (h.trans (Nat.mod_eq_zero_of_dvd e)), + fun e => Nat.dvd_of_mod_eq_zero (h.symm.trans (congrArg UScalar.val e))⟩ + +set_option trace.profiler true in +/-- The concrete native-frame guard tests the device bit before alignment. -/ +theorem tcbBufferCheck_nativeA (addr ptr rights : U64) (sz : capability.FrameSize) (dev : Bool) : + vspace.check_valid_ipc_buffer addr (.Frame ptr sz rights dev false false 0#u64 0#u64) = + ok (if dev then .Err (.Sys .IllegalOperation) else + if isAligned addr.bv msgAlignBits then .Ok () else .Err (.Sys .AlignmentError)) := by + have hShift : (1#u64 <<< fl4_abi.consts.bits.IPC_BUFFER : Result U64) = ok 1024#u64 := by + rw [fl4_abi.consts.bits.IPC_BUFFER] + rfl + have hSub : (1024#u64 - 1#u64 : Result U64) = ok 1023#u64 := rfl + cases dev + · simp only [vspace.check_valid_ipc_buffer, Bool.false_eq_true, ite_false, hShift, hSub, + bind_ok, lift, tcbBufferAlignedA, bne_iff_ne, ite_not] + by_cases he : (addr &&& 1023#u64) = 0#u64 + · simp only [if_pos he] + · simp only [if_neg he] + · rfl + +set_option trace.profiler true in +/-- The matching l4v guard returns the exact error or succeeds in the same +state using the same native cap, device bit, and alignment observation. -/ +theorem tcbBufferCheck_specA (addr ptr rights : U64) (sz : capability.FrameSize) (dev : Bool) (σ : State Unit) : + (checkValidIpcBuffer (Z := Unit) addr.bv (absCapA (.Frame ptr sz rights dev false false 0#u64 0#u64))).run σ = + ({(if dev then .error .illegalOperation else + if isAligned addr.bv msgAlignBits then .ok () else .error .alignmentError, σ)}, False) := by + cases dev + · have hSpec : checkValidIpcBuffer (Z := Unit) addr.bv + (absCapA (.Frame ptr sz rights false false false 0#u64 0#u64)) = + Fl4Spec.NonDetE.whenE (¬isAligned addr.bv msgAlignBits) + (Fl4Spec.NonDetE.throwError .alignmentError) := rfl + simp only [hSpec, Bool.false_eq_true, ite_false] + by_cases ha : isAligned addr.bv msgAlignBits + · simp only [if_pos ha, Fl4Spec.NonDetE.whenE, if_neg (not_not_intro ha)] + rfl + · simp only [if_neg ha, Fl4Spec.NonDetE.whenE, if_pos ha] + rfl + · rfl + +section +variable {S A : Type} {st : machine.Store S} [L : LawfulStore st] {ar : machine.Arch A} + +set_option trace.profiler true in +/-- After the live post-delete check, the new cap goes into slot 2. The +current thread is read after insertion and the architecture state from +deletion is returned. -/ +theorem tcbInstallBuffer_liveA {s sDeleted sBuffer sInserted : S} {a aDeleted : A} + {t invSlot src dest addr : U64} {cap : capability.Cap} + (hSlot : obj.Tcb.slot t layout.tcb.SLOT_BUFFER = ok dest) + (hDelete : cdt.delete st ar s a dest true = ok (.Ok (), sDeleted, aDeleted)) + (hBuffer : obj.Tcb.set_ipc_buffer st t sDeleted addr = ok sBuffer) + (hLive : tcb.still_valid st sBuffer t invSlot cap src = ok true) + (hInsert : cdt.insert st sBuffer cap src dest = ok sInserted) : + tcb.install_buffer st ar s a t invSlot addr (cap, src) = (do + let sFinal ← if t = L.curW sInserted then sched.reschedule_required st sInserted else ok sInserted + ok (.Ok (), sFinal, aDeleted)) := by + rw [tcb.install_buffer] + simp only [hSlot, hDelete, bind_ok, uncurry_apply_pair, core.result.Result.Insts.CoreOpsTry.branch, hBuffer] + simp only [hLive, ite_true, hInsert, bind_ok, cur_thread_ok, + obj.Tcb.Insts.CoreCmpPartialEqTcb.eq, decide_eq_true_eq] + by_cases h : t = L.curW sInserted + · simp only [if_pos h, Std.bind_assoc, bind_ok] + · simp only [if_neg h, bind_ok] + +/-- Installing a derived native frame preserves the slot invariant when +the cap frame from the actual deletion and insertion covers other TCBs. -/ +theorem tcbBufferNative_afterInstallA {T : Set U64} {s s' : S} {t ptr rights : U64} + {sz : capability.FrameSize} {dev : Bool} + (hOld : Endp.BufNativeA T (L.mem s)) + (hCap : capAtA (L.mem s') (sbWordA t) = + .Frame ptr sz rights dev false false 0#u64 0#u64) + (hOther : ∀ y ∈ T, y ≠ t → capAtA (L.mem s') (sbWordA y) = capAtA (L.mem s) (sbWordA y)) : + Endp.BufNativeA T (L.mem s') := by + intro y hy + by_cases e : y = t + · subst y + rw [Endp.bufSlot_eqA, hCap] + · rw [Endp.bufSlot_eqA, hOther y hy e, ← Endp.bufSlot_eqA] + exact hOld y hy + +end +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/TcbInvokeCopyLoopsA.lean b/proofs/Fl4ProofsA64/P3/TcbInvokeCopyLoopsA.lean new file mode 100644 index 000000000..7ba377adc --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbInvokeCopyLoopsA.lean @@ -0,0 +1,88 @@ +/- +P3 (AArch64): CopyRegisters walks the complete frame or GP register list. +[J] The invariant holds across each actual source read and sanitising write. +-/ +import Fl4ProofsA64.P3.TcbInvokeCopyOrderA +import Fl4ProofsA64.P3.TcbInvokeWriteLoopsA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4SpecA64 + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {I : Nat → S → Prop} {s : S} {dest src : U64} + +private theorem tcbCopyRegisters_loopA {n : Usize} (a : Aeneas.Std.Array U64 n) (limit : U64) + (rs : List Register) (hLen : limit.val = rs.length) (hSmall : rs.length < 2 ^ 32) + (hMap : a.val = rs.map Endp.regIdxA) (hStart : I 0 s) + (hStep : ∀ (s' : S) (j : U64) (hj : j.val < rs.length), I j.val s' → + (do + let r := Endp.regIdxA rs[j.val] + let raw ← obj.Tcb.reg st src s' r + let v ← regs.sanitise_for_tcb st s' dest r raw + obj.Tcb.set_reg st dest s' r v) ⦃ s'' => I (j.val + 1) s'' ⦄) : + loop (fun (s', j) => + if j < limit then do + let k ← lift (UScalar.cast .Usize j) + let r ← Aeneas.Std.Array.index_usize a k + let raw ← obj.Tcb.reg st src s' r + let v ← regs.sanitise_for_tcb st s' dest r raw + let s'' ← obj.Tcb.set_reg st dest s' r v + let j' ← j + 1#u64 + ok (ControlFlow.cont (s'', j')) + else ok (ControlFlow.done s')) (s, 0#u64) ⦃ s' => I rs.length s' ⦄ := by + apply loop.spec_decr_nat (measure := fun y => rs.length - y.2.val) + (inv := fun y => I y.2.val y.1 ∧ y.2.val ≤ rs.length) + · rintro ⟨s', j⟩ ⟨hI, hBound⟩ + dsimp only + change j.val ≤ rs.length at hBound + change I j.val s' at hI + by_cases h : j < limit + · rw [if_pos h] + have hj : j.val < rs.length := by rw [← hLen]; exact h + simp only [lift, bind_ok, tcbRegArray_indexA a rs hMap j hj (by omega)] + refine tcbRegWriteThenA _ _ _ _ (hStep s' j hj hI) ?_ + intro s'' hJ + step as ⟨j', hj'⟩ + refine ⟨?_, ?_, ?_⟩ + · change I j'.val s'' + rw [show j'.val = j.val + 1 by omega] + exact hJ + · omega + · omega + · rw [if_neg h] + change ¬j.val < limit.val at h + have e : j.val = rs.length := by omega + exact (WP.spec_ok _).2 (e ▸ hI) + · exact ⟨hStart, Nat.zero_le _⟩ + +/-- Frame transfer iterates to the end of the same frame list as the spec. -/ +theorem tcbCopyRegisters_frameLoopA (hStart : I 0 s) + (hStep : ∀ (s' : S) (j : U64) (hj : j.val < frameRegisters.length), I j.val s' → + (do + let r := Endp.regIdxA frameRegisters[j.val] + let raw ← obj.Tcb.reg st src s' r + let v ← regs.sanitise_for_tcb st s' dest r raw + obj.Tcb.set_reg st dest s' r v) ⦃ s'' => I (j.val + 1) s'' ⦄) : + tcb.copy_registers_loop0 st s dest src 0#u64 ⦃ s' => I frameRegisters.length s' ⦄ := by + have e : tcb.N_FRAME.val = frameRegisters.length := by rw [tcb.N_FRAME]; rfl + have he : frameRegisters.length < 2 ^ 32 := by decide + exact tcbCopyRegisters_loopA regs.FRAME_REGISTERS tcb.N_FRAME frameRegisters e he tcbFrameRegisters_eqA hStart hStep + +/-- GP transfer iterates to the end of the same GP list as the spec. -/ +theorem tcbCopyRegisters_gpLoopA (hStart : I 0 s) + (hStep : ∀ (s' : S) (j : U64) (hj : j.val < gpRegisters.length), I j.val s' → + (do + let r := Endp.regIdxA gpRegisters[j.val] + let raw ← obj.Tcb.reg st src s' r + let v ← regs.sanitise_for_tcb st s' dest r raw + obj.Tcb.set_reg st dest s' r v) ⦃ s'' => I (j.val + 1) s'' ⦄) : + tcb.copy_registers_loop1 st s dest src 0#u64 ⦃ s' => I gpRegisters.length s' ⦄ := by + have e : tcb.N_GP.val = gpRegisters.length := by rw [tcb.N_GP]; rfl + have he : gpRegisters.length < 2 ^ 32 := by decide + exact tcbCopyRegisters_loopA regs.GP_REGISTERS tcb.N_GP gpRegisters e he tcbGpRegisters_eqA hStart hStep + +end +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/TcbInvokeCopyOrderA.lean b/proofs/Fl4ProofsA64/P3/TcbInvokeCopyOrderA.lean new file mode 100644 index 000000000..b6a7992a5 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbInvokeCopyOrderA.lean @@ -0,0 +1,90 @@ +/- +P3 (AArch64): CopyRegisters' concrete transfer order. The selected +source suspension precedes the target restart. Frame transfer sets NextIP +before GP transfer, and the final hook tests the current destination. +-/ +import Fl4ProofsA64.P3.TcbInvokeRegOrderA +import Fl4ProofsA64.P3.SchedOpsA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4SpecA64 + +section +variable {S A : Type} {st : machine.Store S} [L : LawfulStore st] {ar : machine.Arch A} + +/-- The duplicated extraction of the GP loop is the same operation. -/ +theorem tcbCopyRegisters_gpLoop_eqA (s : S) (dest src i : U64) : + tcb.copy_registers_loop2 st s dest src i = tcb.copy_registers_loop1 st s dest src i := rfl + +/-- The exact four flag selections and the Store and scheduler calls they + sequence in the extracted CopyRegisters body. -/ +theorem tcbCopyRegisters_effectsA (s : S) (a : A) (dest src flags : U64) : + tcb.copy_registers st ar s a dest src flags = (do + let (sSuspend, aSuspend) ← if (flags &&& 1#u64) != 0#u64 then + sched.suspend st ar s a src else ok (s, a) + let (sRestart, aRestart) ← if (flags &&& 2#u64) != 0#u64 then + sched.restart st ar sSuspend aSuspend dest else ok (sSuspend, aSuspend) + let sFrame ← if (flags &&& 4#u64) != 0#u64 then do + let s' ← tcb.copy_registers_loop0 st sRestart dest src 0#u64 + let pc := L.mem s' (Endp.regA dest layout.reg.FAULT_IP) + ok (L.wr s' (Endp.regA dest layout.reg.NEXT_IP) pc) + else ok sRestart + let sGp ← if (flags &&& 8#u64) != 0#u64 then + tcb.copy_registers_loop1 st sFrame dest src 0#u64 else ok sFrame + if dest = L.curW sGp then + let sFinal ← sched.reschedule_required st sGp + ok (.Ok (), sFinal, aRestart) + else ok (.Ok (), sGp, aRestart)) := by + have hReg : regWA dest layout.reg.FAULT_IP = Endp.regA dest layout.reg.FAULT_IP := rfl + simp only [tcb.copy_registers, lift, bind_ok, tcbCopyRegisters_gpLoop_eqA, tcb_reg_eqA, hReg, + Endp.set_reg_eqA, tcb.post_modify_registers, cur_thread_ok, obj.Tcb.Insts.CoreCmpPartialEqTcb.eq, + decide_eq_true_eq] + by_cases hFrame : (flags &&& 4#u64 != 0#u64) = true <;> + by_cases hGp : (flags &&& 8#u64 != 0#u64) = true <;> + simp only [hFrame, hGp, Bool.false_eq_true, ite_true, ite_false, bind_ok, Std.bind_assoc] + +end +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- One actual register-copy iteration. The read is from the current + typed source, including when source and destination denote one TCB. -/ +theorem tcbCopyRegister_stepA {T E R : Set U64} {Bf : Set (U64 × Word)} {SR : S → State Unit → Prop} + (O : Endp.SchedOpsEA st T E R Bf SR) (P : Endp.EPlaced T E R Bf) + {s : S} {σ : State Unit} {dest src : U64} {xd xs : Tcb} {r : Register} {guest : Bool} + (hE : Endp.EpStateRel T E R Bf (L.mem s) σ) (hS : SR s σ) (hd : dest ∈ T) (hs : src ∈ T) + (hDest : σ.kheap (specPtr dest) = some (.tcb xd)) (hSrc : σ.kheap (specPtr src) = some (.tcb xs)) + (hGuest : r = .spsrEl1 → ∃ p, vcpu.bound st s dest = ok p ∧ guest = (p != 0#u64)) : + let word := fl4SanitiseRegister guest r (xs.tcbArch.tcbContext.userRegs r) + let s' := L.wr s (Endp.regA dest (Endp.regIdxA r)) ⟨word⟩ + let σ' := Notif.updK σ (specPtr dest) (.tcb (Notif.setReg xd r word)) + (do + let raw ← obj.Tcb.reg st src s (Endp.regIdxA r) + let sanitised ← regs.sanitise_for_tcb st s dest (Endp.regIdxA r) raw + obj.Tcb.set_reg st dest s (Endp.regIdxA r) sanitised) = ok s' ∧ + (do + let raw ← asUser (specPtr src) (getRegister r) + asUser (specPtr dest) (setRegister r (fl4SanitiseRegister guest r raw))) σ = ({((), σ')}, False) ∧ + Endp.EpStateRel T E R Bf (L.mem s') σ' ∧ SR s' σ' := by + let read := L.mem s (Endp.regA src (Endp.regIdxA r)) + let v : U64 := ⟨fl4SanitiseRegister guest r (xs.tcbArch.tcbContext.userRegs r)⟩ + let s' := L.wr s (Endp.regA dest (Endp.regIdxA r)) v + let σ' := Notif.updK σ (specPtr dest) (.tcb (Notif.setReg xd r v.bv)) + have hWord : read.bv = xs.tcbArch.tcbContext.userRegs r := (hE.at_tcb hs hSrc).regs r + have hRead : obj.Tcb.reg st src s (Endp.regIdxA r) = ok read := tcb_reg_eqA s src _ + have hRun : (do + let raw ← obj.Tcb.reg st src s (Endp.regIdxA r) + let sanitised ← regs.sanitise_for_tcb st s dest (Endp.regIdxA r) raw + obj.Tcb.set_reg st dest s (Endp.regIdxA r) sanitised) = ok s' := by + rw [hRead, bind_ok, tcbWriteSanitisedReg_eqA s dest r read guest hGuest, hWord] + obtain ⟨hStep, hWrites, hEp⟩ := Endp.write_reg_rel P s σ hE hd hDest r v + have hSched : SR s' σ' := O.stable _ _ _ _ hS hWrites hEp (Endp.NoBlockE.tcbK hDest _ rfl rfl) + refine ⟨hRun, ?_, hStep, hSched⟩ + rw [Notif.nd_bind_one (asUser_getRegister_eqA hSrc r)] + exact Notif.asUser_setRegister_eq r _ hDest + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/TcbInvokeCopyPostA.lean b/proofs/Fl4ProofsA64/P3/TcbInvokeCopyPostA.lean new file mode 100644 index 000000000..ae4b6a0d9 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbInvokeCopyPostA.lean @@ -0,0 +1,112 @@ +/- +P3 (AArch64): the concrete CopyRegisters loops follow the spec's +register-copy operations in order, including aliased source and target. +[J] The input relation holds after any selected source suspension and target restart. +-/ +import Fl4ProofsA64.P3.TcbInvokeCopyLoopsA +import Fl4ProofsA64.P3.TcbInvokeWritePrefixA +import Fl4ProofsA64.P3.DelSpecA +import Fl4ProofsA64.P3.ScCheckA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4SpecA64 Endp +open Fl4Spec (NonDet) + +/-- A single register copy at the current spec heap. -/ +def tcbCopyRegisterA (dest src : ObjRef) (guest : Bool) (r : Register) : SMonad Unit Unit := do + let raw ← asUser src (getRegister r) + asUser dest (setRegister r (fl4SanitiseRegister guest r raw)) + +private theorem mapMx_copyAppendA {α StateType : Type} (f : α → NonDet StateType Unit) (xs ys : List α) : + NonDet.mapMx f (xs ++ ys) = (NonDet.mapMx f xs >>= fun _ => NonDet.mapMx f ys) := by + induction xs with + | nil => + change NonDet.mapMx f ys = ((pure () : NonDet StateType Unit) >>= fun _ => NonDet.mapMx f ys) + rw [nd_pure_bindD] + | cons x xs ih => + funext state + change (f x >>= fun _ => NonDet.mapMx f (xs ++ ys)) state = + ((f x >>= fun _ => NonDet.mapMx f xs) >>= fun _ => NonDet.mapMx f ys) state + rw [ih, Sched.nd_bind_assocSc] + +private theorem tcbCopyPrefixAdvanceA {dest src : ObjRef} {guest : Bool} {rs : List Register} + {j : Nat} {σ0 σ σ' : State Unit} (hj : j < rs.length) + (hOld : ((), σ) ∈ (NonDet.mapMx (tcbCopyRegisterA dest src guest) (rs.take j) σ0).1) + (hStep : tcbCopyRegisterA dest src guest rs[j] σ = ({((), σ')}, False)) : + ((), σ') ∈ (NonDet.mapMx (tcbCopyRegisterA dest src guest) (rs.take (j + 1)) σ0).1 := by + rw [List.take_succ, List.getElem?_eq_getElem hj, Option.toList_some, mapMx_copyAppendA] + refine mem_bind_ndD hOld ?_ + change ((), σ') ∈ ((tcbCopyRegisterA dest src guest rs[j] >>= fun _ => NonDet.pure ()) σ).1 + rw [Notif.nd_bind_one hStep] + rfl + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {T E R : Set U64} {Bf : Set (U64 × Word)} {SR : S → State Unit → Prop} +variable {dest src p : U64} {σ0 : State Unit} + +private def CopyPrefixRel (rs : List Register) (i : Nat) (s : S) : Prop := + ∃ σ : State Unit, + ((), σ) ∈ (NonDet.mapMx (tcbCopyRegisterA (specPtr dest) (specPtr src) (p != 0#u64)) (rs.take i) σ0).1 ∧ + EpStateRel T E R Bf (L.mem s) σ ∧ SR s σ ∧ vcpu.bound st s dest = ok p + +private theorem tcbCopyPrefixStepA (O : SchedOpsEA st T E R Bf SR) (P : EPlaced T E R Bf) + (hd : dest ∈ T) (hs : src ∈ T) (rs : List Register) (s : S) (j : U64) (hj : j.val < rs.length) + (hOld : CopyPrefixRel (st := st) (T := T) (E := E) (R := R) (Bf := Bf) (SR := SR) + (dest := dest) (src := src) (p := p) (σ0 := σ0) rs j.val s) : + (do + let r := regIdxA rs[j.val] + let raw ← obj.Tcb.reg st src s r + let v ← regs.sanitise_for_tcb st s dest r raw + obj.Tcb.set_reg st dest s r v) ⦃ s' => CopyPrefixRel (st := st) (T := T) (E := E) (R := R) (Bf := Bf) (SR := SR) + (dest := dest) (src := src) (p := p) (σ0 := σ0) rs (j.val + 1) s' ⦄ := by + obtain ⟨σ, hRun, hE, hS, hVcpu⟩ := hOld + obtain ⟨xd, hkd, _⟩ := hE.tcb dest hd + obtain ⟨xs, hks, _⟩ := hE.tcb src hs + let r := rs[j.val] + let v : U64 := ⟨fl4SanitiseRegister (p != 0#u64) r (xs.tcbArch.tcbContext.userRegs r)⟩ + let s' := L.wr s (Endp.regA dest (regIdxA r)) v + let σ' := Notif.updK σ (specPtr dest) (.tcb (Notif.setReg xd r v.bv)) + obtain ⟨hWrite, hSpec, hEp, hSched⟩ := tcbCopyRegister_stepA O P hE hS hd hs hkd hks + (r := r) (guest := (p != 0#u64)) (fun _ => ⟨p, hVcpu, rfl⟩) + rw [hWrite, WP.spec_ok] + have hNew : tcbCopyRegisterA (specPtr dest) (specPtr src) (p != 0#u64) r σ = ({((), σ')}, False) := hSpec + have hLink : vcpu.bound st s' dest = ok p := (tcbVcpuLinkRegFrameA s (P.tcb dest hd) r v).trans hVcpu + exact ⟨σ', tcbCopyPrefixAdvanceA (σ := σ) hj hRun hNew, hEp, hSched, hLink⟩ + +variable {s : S} + +/-- Copy the architectural frame list, preserving the scheduler relation and VCPU link. -/ +theorem tcbCopyRegisters_framePostA (O : SchedOpsEA st T E R Bf SR) (P : EPlaced T E R Bf) + (hd : dest ∈ T) (hs : src ∈ T) (hE : EpStateRel T E R Bf (L.mem s) σ0) (hS : SR s σ0) + (hVcpu : vcpu.bound st s dest = ok p) : + tcb.copy_registers_loop0 st s dest src 0#u64 ⦃ s' => ∃ σ', + ((), σ') ∈ (NonDet.mapMx (tcbCopyRegisterA (specPtr dest) (specPtr src) (p != 0#u64)) frameRegisters σ0).1 ∧ + EpStateRel T E R Bf (L.mem s') σ' ∧ SR s' σ' ∧ vcpu.bound st s' dest = ok p ⦄ := by + let I := CopyPrefixRel (st := st) (T := T) (E := E) (R := R) (Bf := Bf) (SR := SR) + (dest := dest) (src := src) (p := p) (σ0 := σ0) frameRegisters + have hStart : I 0 s := ⟨σ0, rfl, hE, hS, hVcpu⟩ + refine WP.spec_mono (tcbCopyRegisters_frameLoopA hStart (tcbCopyPrefixStepA O P hd hs frameRegisters)) ?_ + rintro s' ⟨σ', hRun, hEp, hSched, hLink⟩ + rw [List.take_length] at hRun + exact ⟨σ', hRun, hEp, hSched, hLink⟩ + +/-- Copy the architectural GP list, with the same live-source semantics. -/ +theorem tcbCopyRegisters_gpPostA (O : SchedOpsEA st T E R Bf SR) (P : EPlaced T E R Bf) + (hd : dest ∈ T) (hs : src ∈ T) (hE : EpStateRel T E R Bf (L.mem s) σ0) (hS : SR s σ0) + (hVcpu : vcpu.bound st s dest = ok p) : + tcb.copy_registers_loop1 st s dest src 0#u64 ⦃ s' => ∃ σ', + ((), σ') ∈ (NonDet.mapMx (tcbCopyRegisterA (specPtr dest) (specPtr src) (p != 0#u64)) gpRegisters σ0).1 ∧ + EpStateRel T E R Bf (L.mem s') σ' ∧ SR s' σ' ∧ vcpu.bound st s' dest = ok p ⦄ := by + let I := CopyPrefixRel (st := st) (T := T) (E := E) (R := R) (Bf := Bf) (SR := SR) + (dest := dest) (src := src) (p := p) (σ0 := σ0) gpRegisters + have hStart : I 0 s := ⟨σ0, rfl, hE, hS, hVcpu⟩ + refine WP.spec_mono (tcbCopyRegisters_gpLoopA hStart (tcbCopyPrefixStepA O P hd hs gpRegisters)) ?_ + rintro s' ⟨σ', hRun, hEp, hSched, hLink⟩ + rw [List.take_length] at hRun + exact ⟨σ', hRun, hEp, hSched, hLink⟩ + +end +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/TcbInvokeCopySelectA.lean b/proofs/Fl4ProofsA64/P3/TcbInvokeCopySelectA.lean new file mode 100644 index 000000000..427f17f11 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbInvokeCopySelectA.lean @@ -0,0 +1,106 @@ +/- +P3 (AArch64): correspondence of the four CopyRegisters selection bits +and the accepted abstract decoder result. +-/ +import Fl4ProofsA64.P3.TcbInvokeRegisterSelectA +import Fl4ProofsA64.P3.DelSpecA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open Fl4ProofsA64.P1 Fl4SpecA64 + +private theorem tcbRegisterFlagBitA (flags mask : U64) (bit : Nat) (hBit : bit < 64) + (hMask : mask.bv = (1#u64).bv <<< bit) : + (flags &&& mask != 0#u64) = testBit flags.bv bit := by + have hSelect (j : Nat) : + (flags &&& mask).bv.getLsbD j = (flags.bv.getLsbD j && decide (j = bit)) := by + simp only [UScalar.bv_and, BitVec.getLsbD_and, hMask, BitVec.getLsbD_shiftLeft, + one_bit, UScalarTy.U64_numBits_eq] + congr 1 + by_cases hj : j = bit <;> simp [hj] <;> omega + change _ = flags.bv.getLsbD bit + cases h : flags.bv.getLsbD bit with + | false => + have hz : (flags &&& mask) = 0#u64 := by + rw [UScalar.eq_equiv_bv_eq] + apply BitVec.eq_of_getLsbD_eq + intro j _ + rw [hSelect] + by_cases hj : j = bit <;> simp [hj, h, BitVec.getLsbD_zero] + simp [hz, h] + | true => + have hz : (flags &&& mask) ≠ 0#u64 := by + intro he + have hf := congrArg (fun w : U64 => w.bv.getLsbD bit) he + rw [hSelect, h] at hf + simp [BitVec.getLsbD_zero] at hf + simp [hz, h] + +/-- The extracted masks and the abstract decode inspect the same four bits. -/ +theorem tcbCopyRegisters_flagBitsA (flags : U64) : + (flags &&& 1#u64 != 0#u64) = testBit flags.bv 0 ∧ + (flags &&& 2#u64 != 0#u64) = testBit flags.bv 1 ∧ + (flags &&& 4#u64 != 0#u64) = testBit flags.bv 2 ∧ + (flags &&& 8#u64 != 0#u64) = testBit flags.bv 3 := + ⟨tcbRegisterFlagBitA flags 1#u64 0 (by decide) rfl, + tcbRegisterFlagBitA flags 2#u64 1 (by decide) rfl, + tcbRegisterFlagBitA flags 4#u64 2 (by decide) rfl, + tcbRegisterFlagBitA flags 8#u64 3 (by decide) rfl⟩ + +set_option maxHeartbeats 1200000 in +private theorem tcbCopyRegisters_decodeEqA (dest src : ObjRef) + (flags : Word) (args : List Word) (caps : List Cap) : + decodeCopyRegisters (Z := Unit) (flags :: args) (.threadCap dest) (.threadCap src :: caps) = + Fl4Spec.NonDetE.returnOk (.copyRegisters dest src (testBit flags 0) (testBit flags 1) + (testBit flags 2) (testBit flags 3) ArchDefaultExtraRegisters) := by + unfold decodeCopyRegisters + simp only [Fl4Spec.NonDetE.whenE, List.cons_ne_nil, if_false, Fl4Spec.NonDetE.returnOk] + rw [ndE_pure_bindD, ndE_pure_bindD, ndE_pure_bindD, ndE_pure_bindD, + ndE_pure_bindD, ndE_pure_bindD, ndE_pure_bindD] + +/-- The accepted abstract decoder selects the source and all four flags. -/ +theorem tcbCopyRegisters_selectedSpecA (σ : State Unit) (dest src : ObjRef) + (flags : Word) (args : List Word) (caps : List Cap) : + (decodeCopyRegisters (Z := Unit) (flags :: args) (.threadCap dest) (.threadCap src :: caps)).run σ = + ({(.ok (.copyRegisters dest src (testBit flags 0) (testBit flags 1) + (testBit flags 2) (testBit flags 3) ArchDefaultExtraRegisters), σ)}, False) := by + rw [tcbCopyRegisters_decodeEqA, run_returnOkD] + +private theorem whenE_posA {σ ε : Type} {P : Prop} [Decidable P] (h : P) + (m : Fl4Spec.NonDetE σ ε Unit) : Fl4Spec.NonDetE.whenE P m = m := if_pos h + +private theorem whenE_negA {σ ε : Type} {P : Prop} [Decidable P] (h : ¬ P) + (m : Fl4Spec.NonDetE σ ε Unit) : Fl4Spec.NonDetE.whenE P m = pure () := if_neg h + +/-- Both ways the abstract CopyRegisters arguments can be truncated. -/ +theorem tcbCopyRegisters_truncatedSpecA (σ : State Unit) (dest : ObjRef) + (flags : Word) (args : List Word) (caps : List Cap) : + (decodeCopyRegisters (Z := Unit) [] (.threadCap dest) caps).run σ = + ({(.error .truncatedMessage, σ)}, False) ∧ + (decodeCopyRegisters (Z := Unit) (flags :: args) (.threadCap dest) []).run σ = + ({(.error .truncatedMessage, σ)}, False) := by + refine ⟨?_, ?_⟩ + · unfold decodeCopyRegisters + exact run_throwErrorD _ _ + · unfold decodeCopyRegisters + dsimp only [Fl4Spec.NonDetE.returnOk] + rw [ndE_pure_bindD, ndE_pure_bindD, ndE_pure_bindD, ndE_pure_bindD] + rw [whenE_posA rfl] + exact run_bind_errorD (run_throwErrorD _ σ) _ + +/-- A present extra cap of the wrong kind is an invalid cap at index one. -/ +theorem tcbCopyRegisters_badSourceSpecA (σ : State Unit) (dest : ObjRef) + (flags : Word) (args : List Word) (cap : Cap) (caps : List Cap) + (hKind : ∀ p, cap ≠ .threadCap p) : + (decodeCopyRegisters (Z := Unit) (flags :: args) (.threadCap dest) (cap :: caps)).run σ = + ({(.error (.invalidCapability 1), σ)}, False) := by + unfold decodeCopyRegisters + dsimp only [Fl4Spec.NonDetE.returnOk] + rw [ndE_pure_bindD, ndE_pure_bindD, ndE_pure_bindD, ndE_pure_bindD] + rw [whenE_negA (List.cons_ne_nil cap caps), ndE_pure_bindD] + cases cap with + | threadCap p => exact (hKind p rfl).elim + | _ => exact run_bind_errorD (run_throwErrorD (SyscallError.invalidCapability 1) σ) _ + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/TcbInvokeCopySpecA.lean b/proofs/Fl4ProofsA64/P3/TcbInvokeCopySpecA.lean new file mode 100644 index 000000000..88f227270 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbInvokeCopySpecA.lean @@ -0,0 +1,85 @@ +/- +P3 (AArch64): the abstract CopyRegisters invocation over named states. +[J] The selected suspend/restart, transfer and reschedule phases supply +their outcomes at their actual input states. +-/ +import Fl4ProofsA64.P3.TcbInvokeCopySelectA +import Fl4ProofsA64.P3.TcbInvokeCopyTransferA + +namespace Fl4ProofsA64.P3 +open Fl4SpecA64 +open Fl4Spec (NonDet) + +/-- A64 register sanitisation selects the guest mode of the live destination. -/ +theorem tcbCopyRegisters_guestA {σ : State Unit} {dest : ObjRef} {x : Tcb} + (hk : σ.kheap dest = some (.tcb x)) : + archGetSanitiseRegisterInfo (Z := Unit) dest σ = + ({(decide (x.tcbArch.tcbVcpu ≠ none), σ)}, False) := by + have hVcpu : archThreadGet (Z := Unit) ArchTcb.tcbVcpu dest σ = + ({(x.tcbArch.tcbVcpu, σ)}, False) := by + unfold archThreadGet + rw [nd_bind_oneD (Notif.getTcb_eq hk)] + rfl + unfold archGetSanitiseRegisterInfo + rw [nd_bind_oneD hVcpu] + rfl + +/-- Compose the four selected phases at the actual intermediate states. -/ +theorem tcbCopyRegisters_specA {σ σSuspend σRestart σTransfer σFinal : State Unit} + {dest src : ObjRef} {suspendSource resumeTarget frame gp guest : Bool} + (hSuspend : ((), σSuspend) ∈ (NonDet.when suspendSource (do fl4CancelRun src; suspend src) σ).1) + (hRestart : ((), σRestart) ∈ (NonDet.when resumeTarget (fl4TcbRestart dest) σSuspend).1) + (hGuest : archGetSanitiseRegisterInfo dest σRestart = ({(guest, σRestart)}, False)) + (hTransfer : ((), σTransfer) ∈ ((do + NonDet.when frame (do + NonDet.mapMx (tcbCopyRegisterA dest src guest) frameRegisters + let pc ← asUser dest getRestartPC + asUser dest (setNextPC pc)) + NonDet.when gp (NonDet.mapMx (tcbCopyRegisterA dest src guest) gpRegisters)) σRestart).1) + (hFinal : ((), σFinal) ∈ (NonDet.when (dest = σTransfer.curThread) rescheduleRequired σTransfer).1) : + (.ok [], σFinal) ∈ ((fl4InvokeTcb + (.copyRegisters dest src suspendSource resumeTarget frame gp ArchDefaultExtraRegisters)).run σ).1 := by + simp only [fl4InvokeTcb] + apply mem_liftED + refine mem_bind_ndD hSuspend ?_ + refine mem_bind_ndD hRestart ?_ + rw [nd_bind_oneD hGuest] + change ([], σFinal) ∈ ((do + NonDet.when frame (do + NonDet.mapMx (tcbCopyRegisterA dest src guest) frameRegisters + let pc ← asUser dest getRestartPC + asUser dest (setNextPC pc)) + NonDet.when gp (NonDet.mapMx (tcbCopyRegisterA dest src guest) gpRegisters) + let cur ← NonDet.gets fun s => s.curThread + archPostModifyRegisters cur dest + NonDet.when (dest = cur) rescheduleRequired + return ([] : List Data)) σRestart).1 + rw [← Sched.nd_bind_assocSc] + refine mem_bind_ndD hTransfer ?_ + rw [Notif.nd_gets_bind] + have hPost : archPostModifyRegisters (Z := Unit) σTransfer.curThread dest σTransfer = + ({((), σTransfer)}, False) := rfl + rw [nd_bind_oneD hPost] + exact mem_bind_ndD hFinal rfl + +open Aeneas Aeneas.Std Result fl4_core_a64 Fl4ProofsA64.P1 in +/-- [J] Current-thread agreement is needed only at the transfer successor. +When the destination differs, neither tail changes the successor. -/ +theorem tcbCopyRegisters_noRescheduleA {S A : Type} {st : machine.Store S} + [L : LawfulStore st] {s : S} {a : A} {σ : State Unit} {dest : U64} + (hCur : σ.curThread = specPtr (L.curW s)) (hOther : dest ≠ L.curW s) : + (if dest = L.curW s then do + let s' ← sched.reschedule_required st s + ok (core.result.Result.Ok (), s', a) + else ok (core.result.Result.Ok (), s, a)) = + (ok (core.result.Result.Ok (), s, a) : Result (core.result.Result Unit error.Exc × S × A)) ∧ + (NonDet.when (specPtr dest = σ.curThread) rescheduleRequired σ) = ({((), σ)}, False) := by + have hSpecOther : specPtr dest ≠ σ.curThread := by + rw [hCur] + exact fun he => hOther (specPtr_inj he) + constructor + · rw [ite_eq_right hOther] + · rw [NonDet.when, ite_eq_right hSpecOther] + rfl + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/TcbInvokeCopyTransferA.lean b/proofs/Fl4ProofsA64/P3/TcbInvokeCopyTransferA.lean new file mode 100644 index 000000000..0f973138f --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbInvokeCopyTransferA.lean @@ -0,0 +1,113 @@ +/- +P3 (AArch64): the CopyRegisters register transfer after the scheduler prefix. +[J] The typed destination and source, endpoint relation, scheduler relation, +and destination VCPU link hold after any selected suspension or restart. +-/ +import Fl4ProofsA64.P3.TcbInvokeCopyPostA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4SpecA64 Endp +open Fl4Spec (NonDet) + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {T E R : Set U64} {Bf : Set (U64 × Word)} {SR : S → State Unit → Prop} +variable {dest src p : U64} + +/-- The frame copy reads FaultIP from the resulting destination and writes +NextIP there before any GP copy. -/ +theorem tcbCopyRegisters_nextIpA (O : SchedOpsEA st T E R Bf SR) (P : EPlaced T E R Bf) + {s : S} {σ : State Unit} (hd : dest ∈ T) + (hE : EpStateRel T E R Bf (L.mem s) σ) (hS : SR s σ) + (hVcpu : vcpu.bound st s dest = ok p) : + let pc := L.mem s (Endp.regA dest layout.reg.FAULT_IP) + let s' := L.wr s (Endp.regA dest layout.reg.NEXT_IP) pc + ∃ σ', (do + let word ← asUser (specPtr dest) getRestartPC + asUser (specPtr dest) (setNextPC word)) σ = ({((), σ')}, False) ∧ + EpStateRel T E R Bf (L.mem s') σ' ∧ SR s' σ' ∧ vcpu.bound st s' dest = ok p := by + obtain ⟨x, hk, hx⟩ := hE.tcb dest hd + let pc := L.mem s (Endp.regA dest layout.reg.FAULT_IP) + let s' := L.wr s (Endp.regA dest layout.reg.NEXT_IP) pc + let y := Notif.setReg x .nextIP pc.bv + let σ' := Notif.updK σ (specPtr dest) (.tcb y) + have hPc : pc.bv = x.tcbArch.tcbContext.userRegs .faultIP := by + change (L.mem s (Endp.regA dest layout.reg.FAULT_IP)).bv = _ + rw [← regIdxA_faultIP] + exact hx.regs .faultIP + obtain ⟨hEp, hWrites, hStep⟩ := write_reg_rel P s σ hE hd hk .nextIP pc + have hSched := O.stable _ _ _ _ hS hWrites hStep (NoBlockE.tcbK hk y rfl rfl) + have hLink := (tcbVcpuLinkRegFrameA s (P.tcb dest hd) .nextIP pc).trans hVcpu + rw [regIdxA_nextIP] at hEp hSched hLink + refine ⟨σ', ?_, hEp, hSched, hLink⟩ + change ((asUser (specPtr dest) (getRegister .faultIP)) >>= fun word => + asUser (specPtr dest) (setRegister .nextIP word)) σ = _ + rw [Notif.nd_bind_one (asUser_getRegister_eqA hk .faultIP), ← hPc] + exact Notif.asUser_setRegister_eq .nextIP pc.bv hk + +/-- Match the optional frame transfer, including its derived program counter. -/ +theorem tcbCopyRegisters_framePcA (O : SchedOpsEA st T E R Bf SR) (P : EPlaced T E R Bf) + {s : S} {σ : State Unit} (hd : dest ∈ T) (hs : src ∈ T) + (hE : EpStateRel T E R Bf (L.mem s) σ) (hS : SR s σ) (hVcpu : vcpu.bound st s dest = ok p) + (frame : Bool) : + (if frame then do + let sFrame ← tcb.copy_registers_loop0 st s dest src 0#u64 + let pc := L.mem sFrame (Endp.regA dest layout.reg.FAULT_IP) + ok (L.wr sFrame (Endp.regA dest layout.reg.NEXT_IP) pc) + else ok s) ⦃ s' => ∃ σ', + ((), σ') ∈ (NonDet.when frame (do + NonDet.mapMx (tcbCopyRegisterA (specPtr dest) (specPtr src) (p != 0#u64)) frameRegisters + let pc ← asUser (specPtr dest) getRestartPC + asUser (specPtr dest) (setNextPC pc)) σ).1 ∧ + EpStateRel T E R Bf (L.mem s') σ' ∧ SR s' σ' ∧ vcpu.bound st s' dest = ok p ⦄ := by + by_cases hFrame : frame = true + · rw [if_pos hFrame] + refine WP.spec_bind (tcbCopyRegisters_framePostA O P hd hs hE hS hVcpu) ?_ + rintro sFrame ⟨σFrame, hRun, hEp, hSched, hLink⟩ + obtain ⟨σ', hPc, hEp', hSched', hLink'⟩ := + tcbCopyRegisters_nextIpA O P (s := sFrame) (σ := σFrame) hd hEp hSched hLink + refine (WP.spec_ok _).2 ⟨σ', ?_, hEp', hSched', hLink'⟩ + rw [NonDet.when, if_pos hFrame] + exact mem_bind_ndD hRun (by rw [hPc]; rfl) + · rw [if_neg hFrame, WP.spec_ok] + refine ⟨σ, ?_, hE, hS, hVcpu⟩ + rw [NonDet.when, if_neg hFrame] + rfl + +/-- The independently selected GP copy consumes the live state after the frame +phase. Both flags may be clear. -/ +theorem tcbCopyRegisters_transferA (O : SchedOpsEA st T E R Bf SR) (P : EPlaced T E R Bf) + {s : S} {σ : State Unit} (hd : dest ∈ T) (hs : src ∈ T) + (hE : EpStateRel T E R Bf (L.mem s) σ) (hS : SR s σ) (hVcpu : vcpu.bound st s dest = ok p) + (frame gp : Bool) : + (do + let sFrame ← if frame then do + let s' ← tcb.copy_registers_loop0 st s dest src 0#u64 + let pc := L.mem s' (Endp.regA dest layout.reg.FAULT_IP) + ok (L.wr s' (Endp.regA dest layout.reg.NEXT_IP) pc) + else ok s + if gp then tcb.copy_registers_loop1 st sFrame dest src 0#u64 else ok sFrame) ⦃ s' => ∃ σ', + ((), σ') ∈ ((do + NonDet.when frame (do + NonDet.mapMx (tcbCopyRegisterA (specPtr dest) (specPtr src) (p != 0#u64)) frameRegisters + let pc ← asUser (specPtr dest) getRestartPC + asUser (specPtr dest) (setNextPC pc)) + NonDet.when gp + (NonDet.mapMx (tcbCopyRegisterA (specPtr dest) (specPtr src) (p != 0#u64)) gpRegisters)) σ).1 ∧ + EpStateRel T E R Bf (L.mem s') σ' ∧ SR s' σ' ∧ vcpu.bound st s' dest = ok p ⦄ := by + refine WP.spec_bind (tcbCopyRegisters_framePcA O P hd hs hE hS hVcpu frame) ?_ + rintro sFrame ⟨σFrame, hFrame, hEp, hSched, hLink⟩ + by_cases hGp : gp = true + · rw [ite_eq_left hGp] + refine WP.spec_mono (tcbCopyRegisters_gpPostA O P hd hs hEp hSched hLink) ?_ + rintro s' ⟨σ', hRun, hEp', hSched', hLink'⟩ + exact ⟨σ', mem_bind_ndD hFrame (by rw [NonDet.when, ite_eq_left hGp]; exact hRun), + hEp', hSched', hLink'⟩ + · rw [ite_eq_right hGp, WP.spec_ok] + exact ⟨σFrame, mem_bind_ndD hFrame (by rw [NonDet.when, ite_eq_right hGp]; rfl), + hEp, hSched, hLink⟩ + +end +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/TcbInvokeDispatchA.lean b/proofs/Fl4ProofsA64/P3/TcbInvokeDispatchA.lean new file mode 100644 index 000000000..35e5154a4 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbInvokeDispatchA.lean @@ -0,0 +1,338 @@ +/- +P3 (AArch64): TCB invocation dispatch and rejection. +[S] A quiet second-phase dispatch requires both the target and the +bound guest's RUN_CALLER stall checks to be inactive. +-/ +import Fl4ProofsA64.P3.CnodeFrontA +import Fl4ProofsA64.P1.LocalTimerSet + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4SpecA64 + +noncomputable section + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {s : S} {a : A} {lbl len slot buf t : U64} {block call donate first : Bool} +variable {extra : Array obj.Slot 3#usize} + +theorem decodeTcb_programA : + syscall.decode_invocation st ar s a lbl len slot (.Thread t) block call donate first buf extra = + if first then ok (.Err (.Sys (.InvalidCapability 0#u64)), s, a) + else tcb.decode st ar s a lbl t ⟨len, buf, call, slot, extra⟩ := by + simp only [syscall.decode_invocation, cur_thread_ok, bind_ok] + +end + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} +variable {s : S} {a : A} {t cpu here v : U64} + +/-- A selected actor is either local or is not current on its own core. -/ +def tcbStallGuardA (st : machine.Store S) (s : S) (t cpu here : U64) : Prop := + cpu = here ∨ ∃ curr, obj.node_get st s cpu layout.node.CUR_THREAD = ok curr ∧ curr ≠ t + +/-- [S] Both checks in the actual extracted stall are quiet. -/ +theorem tcb_stall_noneA + (haff : obj.Tcb.affinity st t s = ok cpu) (hcpu : st.cur_cpu s = ok here) + (ht : tcbStallGuardA st s t cpu here) + (hv : vcpu.bound st s t = ok v) + (hg : v = 0#u64 ∨ ∃ caller, + st.read s (core.num.U64.wrapping_add v vcpu.RUN_CALLER) = ok caller ∧ + (caller = 0#u64 ∨ ∃ ccpu, obj.Tcb.affinity st caller s = ok ccpu ∧ + tcbStallGuardA st s caller ccpu here)) : + smp.remote_tcb_stall st ar s a t = ok (s, a) := by + have firstOk : + (if (cpu != here) = true then do + let curr ← obj.node_get st s cpu layout.node.CUR_THREAD + let (s1, a1) ← if curr = t then do + let a' ← ar.remote_stall a cpu + smp.remote_reschedule st ar s a' cpu + else ok (s, a) + ok (s1, a1, t) + else ok (s, a, t)) = ok (s, a, t) := by + rcases ht with h | ⟨curr, hcurr, hne⟩ + · simp only [h, bne_self_eq_false, Bool.false_eq_true, if_false] + · by_cases h : cpu = here + · simp only [h, bne_self_eq_false, Bool.false_eq_true, if_false] + · simp only [bne_iff_ne, hcurr, bind_ok, if_neg hne, uncurry_apply_pair, ite_self] + simp only [smp.remote_tcb_stall, haff, hcpu, bind_ok, firstOk, uncurry_apply_pair, hv] + by_cases h : v = 0#u64 + · simp [h] + · obtain ⟨caller, hread, hcaller⟩ := hg.resolve_left h + by_cases hc : caller = 0#u64 + · simp [h, hc, lift, hread, bind_ok] + · obtain ⟨ccpu, hcaff, hlocal⟩ := hcaller.resolve_left hc + by_cases heq : ccpu = here + · simp [h, hc, lift, hread, bind_ok, hcaff, hcpu, heq] + · obtain ⟨ccur, hccur, hne⟩ := hlocal.resolve_left heq + simp [h, hc, lift, hread, bind_ok, hcaff, hcpu, heq, hccur, hne] + +end + +/-- FL4's ABI TCB labels, all of which fit in the 52-bit message label. -/ +def tcbLabelNumberA (n : Nat) : Prop := (2 ≤ n ∧ n ≤ 20) ∨ n = 72 ∨ n = 73 + +instance (n : Nat) : Decidable (tcbLabelNumberA n) := inferInstanceAs + (Decidable ((2 ≤ n ∧ n ≤ 20) ∨ n = 72 ∨ n = 73)) + +def tcbLabelA : Fl4Label → Bool + | .spec (.genInvocationLabel .tcbReadRegisters) => true + | .spec (.genInvocationLabel .tcbWriteRegisters) => true + | .spec (.genInvocationLabel .tcbCopyRegisters) => true + | .spec (.genInvocationLabel .tcbConfigure) => true + | .spec (.genInvocationLabel .tcbSetPriority) => true + | .spec (.genInvocationLabel .tcbSetMCPriority) => true + | .spec (.genInvocationLabel .tcbSetSchedParams) => true + | .spec (.genInvocationLabel .tcbSetTimeoutEndpoint) => true + | .spec (.genInvocationLabel .tcbSetIPCBuffer) => true + | .spec (.genInvocationLabel .tcbSetSpace) => true + | .spec (.genInvocationLabel .tcbSuspend) => true + | .spec (.genInvocationLabel .tcbResume) => true + | .spec (.genInvocationLabel .tcbBindNotification) => true + | .spec (.genInvocationLabel .tcbUnbindNotification) => true + | .spec (.genInvocationLabel .tcbSetTLSBase) => true + | .spec (.genInvocationLabel .tcbSetFlags) => true + | .ext .tcbReadFpu => true + | .ext .tcbWriteFpu => true + | .ext .tcbSetTimer => true + | .ext .tcbSetPACKeys => true + | .ext .tcbSetHighVSpace => true + | _ => false + +set_option maxRecDepth 8000 in +private theorem tcbLabel_tableA : ∀ n, n < 74 → + tcbLabelA ((fl4Numbering[n]?).getD (.spec (.genInvocationLabel .invalidInvocation))) = + decide (tcbLabelNumberA n) := by + decide + +theorem tcbLabel_eqA (label : Data) : + tcbLabelA (fl4LabelOf label) = decide (tcbLabelNumberA label.toNat) := by + unfold fl4LabelOf + by_cases h : label.toNat < 74 + · exact tcbLabel_tableA _ h + · rw [List.getElem?_eq_none (by show 74 ≤ label.toNat; omega)] + have hout : ¬ tcbLabelNumberA label.toNat := by unfold tcbLabelNumberA; omega + simp [tcbLabelA, hout] + +theorem decodeTcb_firstPhase_specA (label : Data) (args : List Data) (t : ObjRef) + (slot : CslotPtr) (caps : List (Cap × CslotPtr)) (buf : Option ObjRef) + (call : Bool) (index : CapRef) : + fl4DecodeInvocation (Z := Unit) call true label args index slot (.threadCap t) caps buf = + Fl4Spec.NonDetE.throwError (.invalidCapability 0) := rfl + +private theorem decodeTcb_unknownLabel_specA (l : Fl4Label) (h : tcbLabelA l = false) + (args : List Data) (t : ObjRef) (slot : CslotPtr) (caps : List (Cap × CslotPtr)) + (buf : Option ObjRef) : + fl4DecodeTcbInvocation (Z := Unit) l args (.threadCap t) slot caps buf = + Fl4Spec.NonDetE.throwError .illegalOperation := by + have hz : genInvocationType (0#64 : Data) = .invalidInvocation := + gen_specWordA (.ext .tcbReadFpu) + cases l with + | ext e => + cases e <;> simp only [tcbLabelA, Bool.true_eq_false] at h <;> + simp [fl4DecodeTcbInvocation, decodeFl4TcbExt?, decodeTcbInvocation, hz, + Fl4Spec.NonDetE.liftME, Fl4Spec.NonDetE.throwError, ndE_throw_bindA] + | spec l => + cases l with + | archInvocationLabel g => + simp [fl4DecodeTcbInvocation, decodeTcbInvocation, gen_specWordA, fl4GenA, + Fl4Spec.NonDetE.liftME, Fl4Spec.NonDetE.throwError, ndE_throw_bindA] + | genInvocationLabel g => + cases g <;> simp only [tcbLabelA, Bool.true_eq_false] at h <;> + simp [fl4DecodeTcbInvocation, decodeTcbInvocation, gen_specWordA, fl4GenA, + Fl4Spec.NonDetE.liftME, Fl4Spec.NonDetE.throwError, ndE_throw_bindA] + +theorem decodeTcb_unknown_specA {label : Data} (h : ¬ tcbLabelNumberA label.toNat) + (args : List Data) (t : ObjRef) (slot : CslotPtr) (caps : List (Cap × CslotPtr)) + (buf : Option ObjRef) (call : Bool) (index : CapRef) : + fl4DecodeInvocation (Z := Unit) call false label args index slot (.threadCap t) caps buf = + Fl4Spec.NonDetE.throwError .illegalOperation := by + have hlabel : tcbLabelA (fl4LabelOf label) = false := by rw [tcbLabel_eqA]; simp [h] + exact decodeTcb_unknownLabel_specA _ hlabel args t slot caps buf + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} +variable {s : S} {a : A} {lbl t : U64} {ctx : tcb.Ctx} + +private theorem tcbLabel_ne_rangeA (h : ¬ tcbLabelNumberA lbl.val) + {n : U64} (hn : tcbLabelNumberA n.val) : lbl ≠ n := fun e => h (e ▸ hn) + +set_option maxHeartbeats 4000000 in +/-- [S] The unknown-label branch runs the actual target stall before I. +The message label bound excludes the disabled raw u64 sentinels. -/ +theorem decodeTcb_unknown_programA (habi : lbl.val < 2 ^ 52) + (hout : ¬ tcbLabelNumberA lbl.val) (hst : smp.remote_tcb_stall st ar s a t = ok (s, a)) : + tcb.decode st ar s a lbl t ctx = ok (.Err (.Sys .IllegalOperation), s, a) := by + have hmax : lbl ≠ core.num.U64.MAX := by + intro e + rw [e] at habi + exact (by decide : ¬ core.num.U64.MAX.val < 2 ^ 52) habi + have hn (n : U64) (h : tcbLabelNumberA n.val) : lbl ≠ n := tcbLabel_ne_rangeA hout h + unfold tcb.decode + simp only [hst, bind_ok, uncurry_apply_pair, + if_neg (hn fl4_abi.label.TCBSetPACKeys (by rw [fl4_abi.label.TCBSetPACKeys]; decide)), + if_neg (hn fl4_abi.label.TCBSetHighVSpace (by rw [fl4_abi.label.TCBSetHighVSpace]; decide)), + if_neg (hn fl4_abi.label.TCBReadRegisters (by rw [fl4_abi.label.TCBReadRegisters]; decide)), + if_neg (hn fl4_abi.label.TCBWriteRegisters (by rw [fl4_abi.label.TCBWriteRegisters]; decide)), + if_neg (hn fl4_abi.label.TCBCopyRegisters (by rw [fl4_abi.label.TCBCopyRegisters]; decide)), + if_neg (hn fl4_abi.label.TCBSuspend (by rw [fl4_abi.label.TCBSuspend]; decide)), + if_neg (hn fl4_abi.label.TCBResume (by rw [fl4_abi.label.TCBResume]; decide)), + if_neg (hn fl4_abi.label.TCBConfigure (by rw [fl4_abi.label.TCBConfigure]; decide)), + if_neg (hn fl4_abi.label.TCBSetPriority (by rw [fl4_abi.label.TCBSetPriority]; decide)), + if_neg (hn fl4_abi.label.TCBSetMCPriority (by rw [fl4_abi.label.TCBSetMCPriority]; decide)), + if_neg (hn fl4_abi.label.TCBSetSchedParams (by rw [fl4_abi.label.TCBSetSchedParams]; decide)), + if_neg (hn fl4_abi.label.TCBSetIPCBuffer (by rw [fl4_abi.label.TCBSetIPCBuffer]; decide)), + if_neg (hn fl4_abi.label.TCBSetSpace (by rw [fl4_abi.label.TCBSetSpace]; decide)), + if_neg (hn fl4_abi.label.TCBSetTimeoutEndpoint (by rw [fl4_abi.label.TCBSetTimeoutEndpoint]; decide)), + if_neg (hn fl4_abi.label.TCBBindNotification (by rw [fl4_abi.label.TCBBindNotification]; decide)), + if_neg (hn fl4_abi.label.TCBUnbindNotification (by rw [fl4_abi.label.TCBUnbindNotification]; decide)), + if_neg (hn fl4_abi.label.TCBSetTLSBase (by rw [fl4_abi.label.TCBSetTLSBase]; decide)), + if_neg (hn fl4_abi.label.TCBSetFlags (by rw [fl4_abi.label.TCBSetFlags]; decide)), + if_neg (hn fl4_abi.label.TCBSetTimer (by rw [fl4_abi.label.TCBSetTimer]; decide)), + if_neg (hn fl4_abi.label.TCBReadFPU (by rw [fl4_abi.label.TCBReadFPU]; decide)), + if_neg (hn fl4_abi.label.TCBWriteFPU (by rw [fl4_abi.label.TCBWriteFPU]; decide)), + fl4_abi.label.TCBSetEPTRoot, fl4_abi.label.TCBConfigureSingleStepping, + fl4_abi.label.TCBSetBreakpoint, fl4_abi.label.TCBGetBreakpoint, fl4_abi.label.TCBUnsetBreakpoint, + if_neg hmax] + +set_option hygiene false in +local macro "tcb_dispatch" : tactic => `(tactic| + (unfold tcb.decode + rw [hst] + simp (config := { decide := true }) only [bind_ok, uncurry_apply_pair, + fl4_abi.label.TCBSetPACKeys, fl4_abi.label.TCBSetHighVSpace, + fl4_abi.label.TCBReadRegisters, fl4_abi.label.TCBWriteRegisters, + fl4_abi.label.TCBCopyRegisters, fl4_abi.label.TCBSuspend, + fl4_abi.label.TCBResume, fl4_abi.label.TCBConfigure, + fl4_abi.label.TCBSetPriority, fl4_abi.label.TCBSetMCPriority, + fl4_abi.label.TCBSetSchedParams, fl4_abi.label.TCBSetIPCBuffer, + fl4_abi.label.TCBSetSpace, fl4_abi.label.TCBSetTimeoutEndpoint, + fl4_abi.label.TCBBindNotification, fl4_abi.label.TCBUnbindNotification, + fl4_abi.label.TCBSetTLSBase, fl4_abi.label.TCBSetFlags, + fl4_abi.label.TCBSetTimer, fl4_abi.label.TCBReadFPU, + fl4_abi.label.TCBWriteFPU, fl4_abi.label.TCBSetEPTRoot, + fl4_abi.label.TCBConfigureSingleStepping, fl4_abi.label.TCBSetBreakpoint, + fl4_abi.label.TCBGetBreakpoint, fl4_abi.label.TCBUnsetBreakpoint, + ↓reduceIte, if_true, if_false])) + +theorem tcbReadRegisters_programA (hst : smp.remote_tcb_stall st ar s a t = ok (s, a)) : + tcb.decode st ar s a fl4_abi.label.TCBReadRegisters t ctx = + tcb.decode_read_registers st ar s a t ctx := by + tcb_dispatch + +theorem tcbWriteRegisters_programA (hst : smp.remote_tcb_stall st ar s a t = ok (s, a)) : + tcb.decode st ar s a fl4_abi.label.TCBWriteRegisters t ctx = + tcb.decode_write_registers st ar s a t ctx := by + tcb_dispatch + +theorem tcbCopyRegisters_programA (hst : smp.remote_tcb_stall st ar s a t = ok (s, a)) : + tcb.decode st ar s a fl4_abi.label.TCBCopyRegisters t ctx = + tcb.decode_copy_registers st ar s a t ctx := by + tcb_dispatch + +theorem tcbConfigure_programA (hst : smp.remote_tcb_stall st ar s a t = ok (s, a)) : + tcb.decode st ar s a fl4_abi.label.TCBConfigure t ctx = + tcb.decode_configure st ar s a t ctx := by + tcb_dispatch + +theorem tcbSetPriority_programA (hst : smp.remote_tcb_stall st ar s a t = ok (s, a)) : + tcb.decode st ar s a fl4_abi.label.TCBSetPriority t ctx = + tcb.decode_set_priority st ar s a t ctx true := by + tcb_dispatch <;> rfl + +theorem tcbSetMCPriority_programA (hst : smp.remote_tcb_stall st ar s a t = ok (s, a)) : + tcb.decode st ar s a fl4_abi.label.TCBSetMCPriority t ctx = + tcb.decode_set_priority st ar s a t ctx false := by + tcb_dispatch <;> rfl + +theorem tcbSetSchedParams_programA (hst : smp.remote_tcb_stall st ar s a t = ok (s, a)) : + tcb.decode st ar s a fl4_abi.label.TCBSetSchedParams t ctx = + tcb.decode_set_sched_params st ar s a t ctx := by + tcb_dispatch + +theorem tcbSetIPCBuffer_programA (hst : smp.remote_tcb_stall st ar s a t = ok (s, a)) : + tcb.decode st ar s a fl4_abi.label.TCBSetIPCBuffer t ctx = + tcb.decode_set_ipc_buffer st ar s a t ctx := by + tcb_dispatch + +theorem tcbSetSpace_programA (hst : smp.remote_tcb_stall st ar s a t = ok (s, a)) : + tcb.decode st ar s a fl4_abi.label.TCBSetSpace t ctx = + tcb.decode_set_space st ar s a t ctx := by + tcb_dispatch + +theorem tcbSetTimeoutEndpoint_programA (hst : smp.remote_tcb_stall st ar s a t = ok (s, a)) : + tcb.decode st ar s a fl4_abi.label.TCBSetTimeoutEndpoint t ctx = + tcb.decode_set_timeout_endpoint st ar s a t ctx := by + tcb_dispatch + +theorem tcbSuspend_programA (hst : smp.remote_tcb_stall st ar s a t = ok (s, a)) : + tcb.decode st ar s a fl4_abi.label.TCBSuspend t ctx = (do + let s1 ← tcb.restart_caller st s + let (s2, a2) ← sched.suspend st ar s1 a t + ok (.Ok (), s2, a2)) := by + tcb_dispatch + +theorem tcbResume_programA (hst : smp.remote_tcb_stall st ar s a t = ok (s, a)) : + tcb.decode st ar s a fl4_abi.label.TCBResume t ctx = (do + let s1 ← tcb.restart_caller st s + let (s2, a2) ← sched.restart st ar s1 a t + ok (.Ok (), s2, a2)) := by + tcb_dispatch + +theorem tcbBindNotification_programA (hst : smp.remote_tcb_stall st ar s a t = ok (s, a)) : + tcb.decode st ar s a fl4_abi.label.TCBBindNotification t ctx = (do + let (r, s1) ← tcb.decode_bind_notification st s t ctx + ok (r, s1, a)) := by + tcb_dispatch + +theorem tcbUnbindNotification_programA (hst : smp.remote_tcb_stall st ar s a t = ok (s, a)) : + tcb.decode st ar s a fl4_abi.label.TCBUnbindNotification t ctx = (do + let n ← obj.Tcb.bound_ntfn st t s + if n = 0#u64 then + let r ← error.illegal Unit + ok (r, s, a) + else + let s1 ← tcb.restart_caller st s + let s2 ← notification.unbind_notification st s1 t + ok (.Ok (), s2, a)) := by + tcb_dispatch + +theorem tcbSetTLSBase_programA (hst : smp.remote_tcb_stall st ar s a t = ok (s, a)) : + tcb.decode st ar s a fl4_abi.label.TCBSetTLSBase t ctx = (do + let (r, s1) ← tcb.decode_set_tls_base st s t ctx + ok (r, s1, a)) := by + tcb_dispatch + +theorem tcbSetFlags_programA (hst : smp.remote_tcb_stall st ar s a t = ok (s, a)) : + tcb.decode st ar s a fl4_abi.label.TCBSetFlags t ctx = + tcb.decode_set_flags st ar s a t ctx := by + tcb_dispatch + +theorem tcbReadFPU_programA (hst : smp.remote_tcb_stall st ar s a t = ok (s, a)) : + tcb.decode st ar s a fl4_abi.label.TCBReadFPU t ctx = + tcb.fpu_transfer st ar s a t ctx true := by + tcb_dispatch <;> rfl + +theorem tcbWriteFPU_programA (hst : smp.remote_tcb_stall st ar s a t = ok (s, a)) : + tcb.decode st ar s a fl4_abi.label.TCBWriteFPU t ctx = + tcb.fpu_transfer st ar s a t ctx false := by + tcb_dispatch <;> rfl + +theorem tcbSetPACKeys_programA (hst : smp.remote_tcb_stall st ar s a t = ok (s, a)) : + tcb.decode st ar s a fl4_abi.label.TCBSetPACKeys t ctx = + tcb.decode_arch st ar s a fl4_abi.label.TCBSetPACKeys t ctx := by + tcb_dispatch + +theorem tcbSetHighVSpace_programA (hst : smp.remote_tcb_stall st ar s a t = ok (s, a)) : + tcb.decode st ar s a fl4_abi.label.TCBSetHighVSpace t ctx = + tcb.decode_arch st ar s a fl4_abi.label.TCBSetHighVSpace t ctx := by + tcb_dispatch + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/TcbInvokeFlagsA.lean b/proofs/Fl4ProofsA64/P3/TcbInvokeFlagsA.lean new file mode 100644 index 000000000..68b62d3ce --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbInvokeFlagsA.lean @@ -0,0 +1,144 @@ +/- +P3 (AArch64): exact SetFlags word, release, and early reply. +[P] The release branch takes the architecture's local FPU-release +transition; the result in the other branch carries the input arch state. +-/ +import Fl4ProofsA64.P3.TcbInvokeDispatchA +import Fl4ProofsA64.P3.EpFaultMoreA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4Spec Fl4SpecA64 + +noncomputable section + +private theorem tcbFlagMaskA : fl4_abi.consts.tcb_flag.MASK = 3#u64 := by + unfold fl4_abi.consts.tcb_flag.MASK fl4_abi.consts.tcb_flag.FPU_DISABLED + fl4_abi.consts.tcb_flag.TRAP_SYSCALLS + decide + +/-- Both ABI bits, including TRAP_SYSCALLS, are in the concrete result word. -/ +theorem tcbFlagsWord_eqA (old clear set : U64) : + ((old &&& ~~~clear) ||| (set &&& fl4_abi.consts.tcb_flag.MASK)).bv = + fl4TcbFlagsWord old.bv clear.bv set.bv := by + rw [tcbFlagMaskA] + simp only [UScalar.bv_and, UScalar.bv_or, UScalar.bv_not, fl4TcbFlagsWord] + rfl + +section +variable {S A : Type} {st : machine.Store S} [L : LawfulStore st] {ar : machine.Arch A} + +/-- The Call reply uses inline MR0, clears the badge and returns length one. -/ +def tcbFlagReplyA (st : machine.Store S) [L : LawfulStore st] (s : S) (ct v : U64) : S := + L.wr (L.wr (L.wr s (Endp.regA ct layout.reg.X2) v) (Endp.regA ct layout.reg.BADGE) 0#u64) + (Endp.regA ct layout.reg.MSG_INFO) 1#u64 + +private theorem flagReplyWords_eqA (s : S) (ct : U64) : + tcb.reply_words st s ct 1#u64 = + ok (L.wr (L.wr s (Endp.regA ct layout.reg.BADGE) 0#u64) + (Endp.regA ct layout.reg.MSG_INFO) 1#u64) := by + unfold tcb.reply_words + rw [Endp.set_reg_eqA, bind_ok] + simp only [msginfo.MessageInfo.new, bind_ok] + change msg.set_msg_info st _ ct ⟨0#u64, 0#u64, 0#u64, 1#u64⟩ = _ + unfold msg.set_msg_info + have hword : msginfo.MessageInfo.to_word ⟨0#u64, 0#u64, 0#u64, 1#u64⟩ = ok 1#u64 := by + apply eq_of_spec_fd + refine WP.spec_mono (Endp.to_word_specA 0#u64 1#u64 (by decide)) fun x hx => ?_ + apply UScalar.eq_of_val_eq + simp only [messageInfoToData, BitVec.zero_and, BitVec.zero_shiftLeft, BitVec.zero_or] at hx + exact congrArg BitVec.toNat hx + rw [hword, bind_ok, Endp.set_reg_eqA] + +private theorem flagReplyReg_eqA (s : S) (ct buf v : U64) : + msg.set_mr st s ct buf 0#u64 v = ok (1#u64, L.wr s (Endp.regA ct layout.reg.X2) v) := by + rw [Endp.set_mr_reg _ _ _ _ _ (by decide)] + change ok (1#u64, L.wr s (Endp.regA ct (Endp.regIdxA (Endp.mrIdx 0))) v) = _ + have hr : Endp.regIdxA (Endp.mrIdx 0) = layout.reg.X2 := by + unfold Endp.mrIdx Endp.regIdxA layout.reg.X2 + rfl + rw [hr] + +/-- An accepted SetFlags writes the exact shared flags word. Then it +releases FPU ownership if disabled and performs the caller's early return. +The final Running transition is the extracted scheduler operation. -/ +theorem tcbSetFlags_exactA {s s1 : S} {a a' : A} {t clear set : U64} {ctx : tcb.Ctx} + (hst : smp.remote_tcb_stall st ar s a t = ok (s, a)) + (hlen : ¬ ctx.length < 2#u64) (hr : tcb.restart_caller st s = ok s1) + (hclear : tcb.arg st s ctx 0#u64 = ok clear) + (hset : tcb.arg st s ctx 1#u64 = ok set) + (harch : let new := (L.mem s1 (Endp.tTflA t) &&& ~~~clear) ||| (set &&& fl4_abi.consts.tcb_flag.MASK) + (if (new &&& fl4_abi.consts.tcb_flag.FPU_DISABLED) != 0#u64 then ar.fpu_release a t else ok a) = ok a') : + let new : U64 := ⟨fl4TcbFlagsWord (L.mem s1 (Endp.tTflA t)).bv clear.bv set.bv⟩ + let s2 := L.wr s1 (Endp.tTflA t) new + tcb.decode st ar s a fl4_abi.label.TCBSetFlags t ctx = (do + let s3 ← if ctx.call then do + let _ ← msg.lookup_ipc_buffer st s2 true (L.curW s) + ok (tcbFlagReplyA st s2 (L.curW s) new) + else ok s2 + let s4 ← sched.set_thread_state st s3 (L.curW s) sched.TS_RUNNING + ok (.Ok (), s4, a')) := by + dsimp only + rw [tcbSetFlags_programA hst] + let new := (L.mem s1 (Endp.tTflA t) &&& ~~~clear) ||| (set &&& fl4_abi.consts.tcb_flag.MASK) + have hn : new = ⟨fl4TcbFlagsWord (L.mem s1 (Endp.tTflA t)).bv clear.bv set.bv⟩ := by + apply UScalar.eq_of_val_eq + exact congrArg BitVec.toNat (tcbFlagsWord_eqA _ _ _) + rw [← hn] + unfold tcb.decode_set_flags + simp only [if_neg hlen, cur_thread_ok, hclear, hset, bind_ok, hr, + eq_of_spec_fd (Endp.tcb_flags_eqA _ _), lift, + eq_of_spec_fd (obj.Tcb.set_flags.wr _ _ _)] + change (if (new &&& fl4_abi.consts.tcb_flag.FPU_DISABLED) != 0#u64 then _ else _) = _ + have htail (arch : A) : + (if ctx.call = true then do + let buf ← msg.lookup_ipc_buffer st (L.wr s1 (Endp.tTflA t) new) true (L.curW s) + let (len, s3) ← msg.set_mr st (L.wr s1 (Endp.tTflA t) new) (L.curW s) buf 0#u64 new + let s4 ← tcb.reply_words st s3 (L.curW s) len + let s5 ← sched.set_thread_state st s4 (L.curW s) sched.TS_RUNNING + ok ((.Ok () : core.result.Result Unit error.Exc), s5, arch) + else do + let s3 ← sched.set_thread_state st (L.wr s1 (Endp.tTflA t) new) (L.curW s) sched.TS_RUNNING + ok (.Ok (), s3, arch)) = + (do + let s3 ← if ctx.call then do + let _ ← msg.lookup_ipc_buffer st (L.wr s1 (Endp.tTflA t) new) true (L.curW s) + ok (tcbFlagReplyA st (L.wr s1 (Endp.tTflA t) new) (L.curW s) new) + else ok (L.wr s1 (Endp.tTflA t) new) + let s4 ← sched.set_thread_state st s3 (L.curW s) sched.TS_RUNNING + ok (.Ok (), s4, arch)) := by + cases ctx.call + · simp only [Bool.false_eq_true, ite_false, bind_ok] + · simp only [ite_true, Std.bind_assoc, bind_ok] + apply congrArg (fun k => msg.lookup_ipc_buffer st (L.wr s1 (Endp.tTflA t) new) + true (L.curW s) >>= k) + funext buf + rw [flagReplyReg_eqA] + let payload := L.wr (L.wr s1 (Endp.tTflA t) new) (Endp.regA (L.curW s) layout.reg.X2) new + change Aeneas.Std.bind (ok (1#u64, payload)) (fun r => + tcb.reply_words st r.2 (L.curW s) r.1 >>= fun s4 => + sched.set_thread_state st s4 (L.curW s) sched.TS_RUNNING >>= fun s5 => + ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s5, arch)) = _ + rw [bind_ok, flagReplyWords_eqA] + change Aeneas.Std.bind + (ok (L.wr (L.wr payload (Endp.regA (L.curW s) layout.reg.BADGE) 0#u64) + (Endp.regA (L.curW s) layout.reg.MSG_INFO) 1#u64)) + (fun s4 => sched.set_thread_state st s4 (L.curW s) sched.TS_RUNNING >>= fun s5 => + ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s5, arch)) = _ + rw [bind_ok] <;> rfl + change (if (new &&& fl4_abi.consts.tcb_flag.FPU_DISABLED) != 0#u64 + then ar.fpu_release a t else ok a) = ok a' at harch + by_cases h : ((new &&& fl4_abi.consts.tcb_flag.FPU_DISABLED) != 0#u64) = true + · rw [if_pos h] at harch ⊢ + rw [harch, bind_ok] + exact htail _ + · rw [if_neg h] at harch ⊢ + have ha : a' = a := (WP.spec_ok (p := fun x => x = a) a').1 + (harch ▸ (WP.spec_ok _).2 rfl) + rw [ha] + exact htail _ + +end +end +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/TcbInvokeFpuA.lean b/proofs/Fl4ProofsA64/P3/TcbInvokeFpuA.lean new file mode 100644 index 000000000..a9fc432f1 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbInvokeFpuA.lean @@ -0,0 +1,160 @@ +/- +P3 (AArch64): the Store part of ReadFPU and WriteFPU. +[P] Flush, sanitise and reload are the architecture transitions. On +AArch64 reload is the backend's forget operation, with no eager restore. +[S] The matching Ext owner transition applies when there is no remote +FPU owner. The concrete copy and its frame have no architecture premise. +-/ +import Fl4ProofsA64.P3.TcbInvokeDispatchA +import Fl4ProofsA64.P1.QueueFpu + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4SpecA64 + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +private def copyWordAddrA (base : U64) (index : Nat) : U64 := + wa base ⟨BitVec.ofNat 64 (index * 8)⟩ + +private def copyPrefixA (st : machine.Store S) [L : LawfulStore st] (s : S) + (src dst : U64) (n : Nat) : S := + (List.range n).foldl (fun q i => L.wr q (copyWordAddrA dst i) (L.mem q (copyWordAddrA src i))) s + +/-- Sequential Store semantics of the 520-byte image copy. -/ +def tcbFpuCopyA (st : machine.Store S) [L : LawfulStore st] (s : S) (src dst : U64) : S := + copyPrefixA st s src dst (fl4FpuStateSize / 8) + +private theorem copyPrefix_succA (s : S) (src dst : U64) (n : Nat) : + copyPrefixA st s src dst (n + 1) = + L.wr (copyPrefixA st s src dst n) (copyWordAddrA dst n) + (L.mem (copyPrefixA st s src dst n) (copyWordAddrA src n)) := by + simp only [copyPrefixA, List.range_succ, List.foldl_append, List.foldl_cons, List.foldl_nil] + +/-- The extracted loop copies exactly 65 words. Its frame is the destination image. -/ +theorem tcbFpuCopy_exactA (s : S) (src dst : U64) (ha : Aligned dst) + (hfit : dst.val + fl4FpuStateSize ≤ 2 ^ 64) : + tcb.copy_words st s src dst 65#u64 = ok (tcbFpuCopyA st s src dst) ∧ + WritesIn st s (tcbFpuCopyA st s src dst) (CWW dst 65#u64) := by + have he : tcb.copy_words st s src dst 65#u64 = ok (tcbFpuCopyA st s src dst) := by + apply eq_of_spec_fd + unfold tcbFpuCopyA tcb.copy_words tcb.copy_words_loop + change (loop (fun p => tcb.copy_words_loop.body st src dst 65#u64 p.1 p.2) (s, 0#u64)) ⦃ + s' => s' = copyPrefixA st s src dst 65 ⦄ + apply loop.spec_decr_nat (measure := fun p => 65 - p.2.val) + (inv := fun p => p.2.val ≤ 65 ∧ p.1 = copyPrefixA st s src dst p.2.val) + · rintro ⟨q, i⟩ ⟨hi, hq⟩ + dsimp only at hi hq ⊢ + unfold tcb.copy_words_loop.body + by_cases h : i < 65#u64 + swap + · rw [if_neg h] + have he : i.val = 65 := by scalar_tac + exact (WP.spec_ok _).2 (hq.trans (congrArg (copyPrefixA st s src dst) he)) + rw [if_pos h] + have hi' : i.val < 65 := by scalar_tac + simp only [lift, bind_ok, L.read_eq, L.write_eq] + step as ⟨j, hj⟩ + have off : core.num.U64.wrapping_mul i 8#u64 = ⟨BitVec.ofNat 64 (i.val * 8)⟩ := by + apply UScalar.eq_of_val_eq + rw [core.num.U64.wrapping_mul_val_eq] + change i.val * (8#u64 : U64).val % _ = (BitVec.ofNat 64 (i.val * 8)).toNat + simp [U64.size_def, U64.numBits] + have hj' : j.val = i.val + 1 := by omega + have hstep : L.wr q (wa dst (core.num.U64.wrapping_mul i 8#u64)) + (L.mem q (wa src (core.num.U64.wrapping_mul i 8#u64))) = copyPrefixA st s src dst j.val := by + rw [hj', copyPrefix_succA, hq, off] + rfl + first + | exact ⟨⟨by omega, hstep⟩, by omega⟩ + | exact ⟨by omega, hstep, by omega⟩ + · exact ⟨by change (0#u64 : U64).val ≤ 65; decide, rfl⟩ + have hw := tcb.copy_words_loop.w (st := st) s src dst 65#u64 0#u64 ha (by + simpa [fl4FpuStateSize] using hfit) + change tcb.copy_words st s src dst 65#u64 ⦃ s' => WritesIn st s s' (CWW dst 65#u64) ⦄ at hw + rw [he, WP.spec_ok] at hw + exact ⟨he, hw⟩ + + +private def fpuFrameBitsA : capability.FrameSize → U64 + | .Small => 12#u64 + | .Large => 21#u64 + | .Huge => 30#u64 + +private def fpuFrameBytesA : capability.FrameSize → U64 + | .Small => 4096#u64 + | .Large => 2097152#u64 + | .Huge => 1073741824#u64 + +private theorem fpuFrameSize_eqA (size : capability.FrameSize) : + capability.FrameSize.bits size = ok (fpuFrameBitsA size) ∧ + (1#u64 <<< (fpuFrameBitsA size &&& 63#u64)) = ok (fpuFrameBytesA size) := by + cases size <;> constructor <;> with_unfolding_all rfl + +private theorem fpuOffAlign_eqA (offset : U64) (h : offset.val % 64 = 0) : + offset % 64#u64 = ok (0#u64 : U64) := by + apply eq_of_spec_fd + step as ⟨r, hr⟩ + apply UScalar.eq_of_val_eq + simpa [h] using hr + +private theorem fpuOffRoom_eqA (offset : U64) (size : capability.FrameSize) + (h : offset.val + fl4FpuStateSize ≤ (fpuFrameBytesA size).val) : + ∃ room, (fpuFrameBytesA size - offset) = ok room ∧ ¬ room < 520#u64 := by + have hs := (show fl4FpuStateSize = 520 by decide) + have hsub : (fpuFrameBytesA size - offset) ⦃ r => ¬ r < 520#u64 ⦄ := by + step as ⟨room, hroom⟩ + scalar_tac + exact spec_ex_fd hsub + +/-- The accepted decoder path for both frame kinds. The Stage-2 tag and +mapping metadata have no effect on the 520-byte image transfer. -/ +theorem tcbFpuTransfer_exactA {A : Type} {ar : machine.Arch A} + {s s1 : S} {a aFlush aSanitised aForget : A} + {t slot ptr offset rights asid va : U64} {size : capability.FrameSize} + {mapped stage2 toFrame : Bool} {ctx : tcb.Ctx} + (he : tcb.extra ctx 0#usize = ok (some slot)) (hlen : ctx.length ≥ 1#u64) + (hoff : tcb.arg st s ctx 0#u64 = ok offset) + (hcap : obj.Slot.cap st slot s = ok (.Frame ptr size rights false mapped stage2 asid va)) + (hread : rights ≠ capability.VM_KERNEL_ONLY) + (hwrite : toFrame = true → rights = capability.VM_READ_WRITE) + (halign : offset.val % 64 = 0) + (hroom : offset.val + fl4FpuStateSize ≤ (fpuFrameBytesA size).val) + (hr : tcb.restart_caller st s = ok s1) + (hflush : ar.fpu_flush a t = ok aFlush) + (hsanitise : toFrame = false → ar.fpu_sanitise aFlush t = ok aSanitised) + (hforget : toFrame = false → ar.fpu_reload aSanitised t = ok aForget) + (harea : let dst := if toFrame then wa ptr offset else wa t layout.tcb.FPU + Aligned dst ∧ dst.val + fl4FpuStateSize ≤ 2 ^ 64) : + tcb.fpu_transfer st ar s a t ctx toFrame = (if toFrame then + ok (.Ok (), tcbFpuCopyA st s1 (wa t layout.tcb.FPU) (wa ptr offset), aFlush) + else ok (.Ok (), tcbFpuCopyA st s1 (wa ptr offset) (wa t layout.tcb.FPU), aForget)) := by + have hs : fl4FpuStateSize = 520 := by decide + have hf : ¬ offset ≥ fpuFrameBytesA size := by + rw [hs] at hroom + scalar_tac + obtain ⟨room, hsub, hlarge⟩ := fpuOffRoom_eqA offset size hroom + obtain ⟨hbits, hbytes⟩ := fpuFrameSize_eqA size + have hn := eq_of_spec_fd Fl4ProofsA64.P0.Consts.fl4_abi.consts.FPU_STATE_SIZE.spec + have hadd : (520#u64 + 7#u64) = ok (527#u64 : U64) := by with_unfolding_all rfl + have hdiv : (527#u64 / 8#u64) = ok (65#u64 : U64) := by with_unfolding_all rfl + unfold tcb.fpu_transfer + simp only [he, hlen, bind_ok, ite_true, hoff, hcap, Bool.false_eq_true, ite_false, if_neg hread] + cases toFrame + · obtain ⟨ha, hfit⟩ := harea + simp only [Bool.false_eq_true, ite_false] at ha hfit + simp only [Bool.false_eq_true, ite_false, hbits, hbytes, hn, hsub, if_neg hf, if_neg hlarge, + fpuOffAlign_eqA offset halign, bne_self_eq_false, bind_ok, lift, hr, hflush, hadd, hdiv, + obj.Tcb.fpu_addr, (tcbFpuCopy_exactA s1 _ _ ha hfit).1, hsanitise rfl, hforget rfl] + · obtain ⟨ha, hfit⟩ := harea + simp only [ite_true] at ha hfit + simp only [ite_true, hwrite rfl, bne_self_eq_false, Bool.false_eq_true, ite_false, + hbits, hbytes, hn, hsub, if_neg hf, if_neg hlarge, fpuOffAlign_eqA offset halign, + bind_ok, lift, hr, hflush, hadd, hdiv, obj.Tcb.fpu_addr, (tcbFpuCopy_exactA s1 _ _ ha hfit).1] + +end +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/TcbInvokeHighSelectA.lean b/proofs/Fl4ProofsA64/P3/TcbInvokeHighSelectA.lean new file mode 100644 index 000000000..9d23d0aef --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbInvokeHighSelectA.lean @@ -0,0 +1,77 @@ +/- +P3 (AArch64): SetHighVSpace's selection before the actual slot replacement. +[J] S3 supplies the tagged ASID and object observation at lookup. +[S] The target stall has no effects. +-/ +import Fl4ProofsA64.P3.TcbInvokeSlotPostA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} +variable {s sRestart : S} {a : A} {t src root hit asid : U64} {ctx : tcb.Ctx} + +private theorem setHighVSpace_selectA (hst : smp.remote_tcb_stall st ar s a t = ok (s, a)) : + tcb.decode st ar s a fl4_abi.label.TCBSetHighVSpace t ctx = + tcb.decode_set_high_vspace st ar s a t ctx := by + rw [tcbSetHighVSpace_programA hst] + have h : fl4_abi.label.TCBSetHighVSpace ≠ fl4_abi.label.TCBSetPACKeys := by + rw [fl4_abi.label.TCBSetHighVSpace, fl4_abi.label.TCBSetPACKeys] + decide + simp only [tcb.decode_arch, if_neg h] + +theorem tcbSetHighVSpace_truncatedA (hst : smp.remote_tcb_stall st ar s a t = ok (s, a)) + (he : tcb.extra ctx 0#usize = ok none) : + tcb.decode st ar s a fl4_abi.label.TCBSetHighVSpace t ctx = + ok (.Err (.Sys .TruncatedMessage), s, a) := by + rw [setHighVSpace_selectA hst] + simp only [tcb.decode_set_high_vspace, he, bind_ok, error.truncated] + +/-- A present Null cap clears the high slot through the real installer. -/ +theorem tcbSetHighVSpace_clearA (hst : smp.remote_tcb_stall st ar s a t = ok (s, a)) + (he : tcb.extra ctx 0#usize = ok (some src)) + (hc : obj.Slot.cap st src s = ok .Null) (hr : tcb.restart_caller st s = ok sRestart) : + tcb.decode st ar s a fl4_abi.label.TCBSetHighVSpace t ctx = + tcb.install_tcb_cap st ar sRestart a t ctx.slot layout.tcb.SLOT_HIGH_VSPACE .Null src := by + rw [setHighVSpace_selectA hst] + simp only [tcb.decode_set_high_vspace, he, hc, hr, bind_ok] + +/-- The mapped root must be the exact live object in the tagged high ASID +entry before derivation and Restart. -/ +theorem tcbSetHighVSpace_liveA {newCap : capability.Cap} + (hst : smp.remote_tcb_stall st ar s a t = ok (s, a)) + (he : tcb.extra ctx 0#usize = ok (some src)) + (hc : obj.Slot.cap st src s = ok (.HighVSpace root true asid)) + (hfind : vspace.find_high_asid st s asid = ok (.Ok root)) + (hd : objects.derive_cap st s src (.HighVSpace root true asid) = ok (.Ok newCap)) + (hr : tcb.restart_caller st s = ok sRestart) : + tcb.decode st ar s a fl4_abi.label.TCBSetHighVSpace t ctx = + tcb.install_tcb_cap st ar sRestart a t ctx.slot layout.tcb.SLOT_HIGH_VSPACE newCap src := by + rw [setHighVSpace_selectA hst] + simp only [tcb.decode_set_high_vspace, he, hc, hfind, hd, hr, bind_ok, ite_true, + core.result.Result.Insts.CoreOpsTry.branch] + +theorem tcbSetHighVSpace_lookupErrorA {e : error.LookupFault} + (hst : smp.remote_tcb_stall st ar s a t = ok (s, a)) + (he : tcb.extra ctx 0#usize = ok (some src)) + (hc : obj.Slot.cap st src s = ok (.HighVSpace root true asid)) + (hfind : vspace.find_high_asid st s asid = ok (.Err e)) : + tcb.decode st ar s a fl4_abi.label.TCBSetHighVSpace t ctx = + ok (.Err (.Sys (.InvalidCapability 1#u64)), s, a) := by + rw [setHighVSpace_selectA hst] + simp only [tcb.decode_set_high_vspace, he, hc, hfind, bind_ok, ite_true, error.invalid_cap] + +theorem tcbSetHighVSpace_identityErrorA (hst : smp.remote_tcb_stall st ar s a t = ok (s, a)) + (he : tcb.extra ctx 0#usize = ok (some src)) + (hc : obj.Slot.cap st src s = ok (.HighVSpace root true asid)) + (hfind : vspace.find_high_asid st s asid = ok (.Ok hit)) (hne : hit ≠ root) : + tcb.decode st ar s a fl4_abi.label.TCBSetHighVSpace t ctx = + ok (.Err (.Sys (.InvalidCapability 1#u64)), s, a) := by + rw [setHighVSpace_selectA hst] + simp only [tcb.decode_set_high_vspace, he, hc, hfind, bind_ok, ite_true, if_neg hne, error.invalid_cap] + +end +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/TcbInvokeMcpA.lean b/proofs/Fl4ProofsA64/P3/TcbInvokeMcpA.lean new file mode 100644 index 000000000..8b396d5db --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbInvokeMcpA.lean @@ -0,0 +1,105 @@ +/- +P3 (AArch64): SetMCPriority after the actual caller Restart. +[J] Use the pre-Restart MCP observation for the authority and the +MCP relation at the post-Restart state for the target update. +[S] The target stall has no effects. +-/ +import Fl4ProofsA64.P3.TcbInvokeThreadFieldsA +import Fl4ProofsA64.P3.ScDonRefA +import Fl4ProofsA64.P3.DelSpecA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4SpecA64 Sched + +noncomputable section + +private theorem tcbMcp_specA {σ : State Unit} {t auth : U64} {slot : CslotPtr} + {x : Tcb} (m : Priority) (hk : σ.kheap (specPtr t) = some (.tcb x)) : + (fl4InvokeTcb (.threadControlSched (specPtr t) slot none (some (m, specPtr auth)) none none)).run σ = + ({(.ok [], updKSc σ (specPtr t) (.tcb {x with tcbMcpriority := m}))}, False) := by + simp only [fl4InvokeTcb, fl4InstallTcbCap, Fl4Spec.NonDet.maybeM] + have hset : setMcpriority (Z := Unit) (specPtr t) m σ = + ({((), updKSc σ (specPtr t) (.tcb {x with tcbMcpriority := m}))}, False) := + threadSet_eqSc _ hk + rw [run_bind_okD (run_returnOkD () σ), run_bind_okD (run_liftE_stD hset)] + rw [run_bind_okD (run_liftE_stD (m := Fl4Spec.NonDet.pure ()) rfl), + run_bind_okD (run_liftE_stD (m := Fl4Spec.NonDet.pure ()) rfl)] + rfl + +section +variable {S A : Type} {st : machine.Store S} [L : LawfulStore st] {T : Set U64} + +private theorem mcpAlignedA (hP : TcbPlaced T) {t : U64} (ht : t ∈ T) : Aligned (mcpA t) := + field_aligned (hP t ht) (K := 3440#u64) (by decide) (by decide) + +private theorem mcpField_neA (hP : TcbPlaced T) {t y : U64} (ht : t ∈ T) (hy : y ∈ T) (hne : y ≠ t) : + (mcpA t).val ≠ (mcpA y).val := by + intro e + exact field_ne (K1 := 3440#u64) (K2 := 3440#u64) (hP t ht) (hP y hy) + (by decide) (by decide) (fun h => (hne h.symm).elim) (UScalar.eq_of_val_eq e) + +private theorem flagsAlignedA (hP : TcbPlaced T) {t : U64} (ht : t ∈ T) : Aligned (flagsA t) := + field_aligned (hP t ht) (K := 3456#u64) (by decide) (by decide) + +private theorem mcpFlags_neA (hP : TcbPlaced T) {t y : U64} (ht : t ∈ T) (hy : y ∈ T) : + (mcpA t).val ≠ (flagsA y).val := by + intro e + exact field_ne (K1 := 3440#u64) (K2 := 3456#u64) (hP t ht) (hP y hy) + (by decide) (by decide) (fun _ => by decide) (UScalar.eq_of_val_eq e) + +/-- The one-word update has the same 8-bit value in the abstract target, +and leaves the MCP of every other placed TCB intact. -/ +theorem tcbMcpRel_writeA {s : S} {σ : State Unit} {t p : U64} {x : Tcb} + (hP : TcbPlaced T) (ht : t ∈ T) (hM : McpRelA T m (L.mem s) σ) + (hk : σ.kheap (specPtr t) = some (.tcb x)) (hp : p.val < 256) : + McpRelA T m (L.mem (L.wr s (mcpA t) p)) + (updKSc σ (specPtr t) (.tcb {x with tcbMcpriority := BitVec.ofNat 8 p.val})) := by + intro y hy + obtain ⟨old, hOld, hWord, hFl⟩ := hM y hy + have hfw : L.mem (L.wr s (mcpA t) p) (flagsA y) = L.mem s (flagsA y) := + L.mem_wr_ne (mcpAlignedA hP ht) (flagsAlignedA hP hy) (mcpFlags_neA hP ht hy) + by_cases he : y = t + · subst y + obtain rfl : x = old := by + rw [hk] at hOld; exact KernelObject.tcb.inj (Option.some.inj hOld) + refine ⟨{x with tcbMcpriority := BitVec.ofNat 8 p.val}, ?_, ?_, fun g => (hFl g).keep hfw rfl⟩ + · rw [updKSc_kheap, Function.update_self] + · rw [L.mem_wr_same (mcpAlignedA hP ht)] + exact (Nat.mod_eq_of_lt hp).symm + · refine ⟨old, ?_, ?_, fun g => (hFl g).keep hfw rfl⟩ + · rw [updKSc_kheap, Function.update_of_ne (fun e => he (specPtr_inj e)), hOld] + · rw [L.mem_wr_ne (mcpAlignedA hP ht) (mcpAlignedA hP hy) (mcpField_neA hP ht hy he)] + exact hWord + +/-- The accepted SetMCPriority reaches the FL4 abstract operation and its +MCP relation. The authority's observed word proves the cast is lossless. -/ +theorem tcbSetMCPriority_performA {ar : machine.Arch A} {s sRestart : S} {a : A} + {σ σRestart : State Unit} {t p auth argSlot : U64} {κ : CslotPtr} {ctx : tcb.Ctx} + (hst : smp.remote_tcb_stall st ar s a t = ok (s, a)) + (he : tcb.extra_with_length ctx 0#usize 1#u64 = ok (.Ok argSlot)) + (hp : tcb.arg st s ctx 0#u64 = ok p) + (ha : tcb.thread_arg st s argSlot 1#u64 = ok (.Ok auth)) + (hle : ¬L.mem s (mcpA auth) < p) (hr : tcb.restart_caller st s = ok sRestart) + (hPre : McpRelA T m (L.mem s) σ) (haT : auth ∈ T) + (hP : TcbPlaced T) (ht : t ∈ T) (hM : McpRelA T m (L.mem sRestart) σRestart) : + tcb.decode st ar s a fl4_abi.label.TCBSetMCPriority t ctx ⦃ r => + r.1 = .Ok () ∧ r.2.2 = a ∧ ∃ σFinal, + (.ok [], σFinal) ∈ + ((fl4InvokeTcb (.threadControlSched (specPtr t) κ none + (some (BitVec.ofNat 8 p.val, specPtr auth)) none none)).run σRestart).1 ∧ + McpRelA T m (L.mem r.2.1) σFinal ⦄ := by + obtain ⟨authority, _, hw, -⟩ := hPre auth haT + have hb : authority.tcbMcpriority.toNat < 256 := authority.tcbMcpriority.isLt + have hv : p.val ≤ (L.mem s (mcpA auth)).val := Nat.le_of_not_lt hle + have hWidth : p.val < 256 := by rw [hw] at hv; exact Nat.lt_of_le_of_lt hv hb + obtain ⟨x, hx, _⟩ := hM t ht + rw [tcbSetMCPriority_postA hst he hp ha hle hr] + refine (WP.spec_ok _).2 ⟨rfl, rfl, _, ?_, tcbMcpRel_writeA hP ht hM hx hWidth⟩ + rw [tcbMcp_specA _ hx] + rfl + +end +end +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/TcbInvokeNtfnA.lean b/proofs/Fl4ProofsA64/P3/TcbInvokeNtfnA.lean new file mode 100644 index 000000000..776a02b09 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbInvokeNtfnA.lean @@ -0,0 +1,137 @@ +/- +P3 (AArch64): BindNotification and UnbindNotification. +The successful paths write the notification backlink and then the TCB +backlink. Unbind takes its early error before Restart and re-reads the +backlink afterwards. The abstract unbind shares the IRQ finaliser leaf. +-/ +import Fl4ProofsA64.P3.TcbInvokeDispatchA +import Fl4ProofsA64.P3.IrqInvokeUnbindA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4Spec Fl4SpecA64 Sched + +noncomputable section + +set_option Elab.async false +set_option maxRecDepth 768 + +section +variable {S A : Type} {st : machine.Store S} [L : LawfulStore st] {ar : machine.Arch A} + +private theorem tcbBoundReadA (s : S) (t : U64) : + obj.Tcb.bound_ntfn st t s = ok (L.mem s (boundA t)) := by + unfold obj.Tcb.bound_ntfn + simp only [eq_of_spec_fd Fl4ProofsA64.P0.Consts.layout.tcb.BOUND_NTFN.spec, + bind_ok, lift, L.read_eq, boundA] + +/-- The paired Store writes of binding. -/ +theorem tcbBindNtfnWritesA (s : S) (t n : U64) : + notification.bind_notification st s t n = ok (L.wr (L.wr s (ntfnBtA n) t) (boundA t) n) := by + unfold notification.bind_notification + rw [eq_of_spec_fd (P1.obj.Ntfn.set_bound_tcb.wr s n t), bind_ok] + exact eq_of_spec_fd (P1.obj.Tcb.set_bound_ntfn.wr _ t n) + +/-- The paired Store writes of unbinding. -/ +theorem tcbUnbindNtfnWritesA (s : S) (t n : U64) (hn : n ≠ 0#u64) + (h : L.mem s (boundA t) = n) : + notification.unbind_notification st s t = + ok (L.wr (L.wr s (ntfnBtA n) 0#u64) (boundA t) 0#u64) := by + unfold notification.unbind_notification + rw [tcbBoundReadA, h, bind_ok, if_pos (bne_iff_ne.mpr hn), + eq_of_spec_fd (P1.obj.Ntfn.set_bound_tcb.wr s n 0#u64), bind_ok] + exact eq_of_spec_fd (P1.obj.Tcb.set_bound_ntfn.wr _ t 0#u64) + +theorem tcbBindNotification_exactA {s s1 : S} {a : A} {t n ns badge : U64} {send : Bool} + {ctx : tcb.Ctx} (hst : smp.remote_tcb_stall st ar s a t = ok (s, a)) + (hextra : tcb.extra ctx 0#usize = ok (some ns)) (hbound : L.mem s (boundA t) = 0#u64) + (hcap : obj.Slot.cap st ns s = ok (.Notification n badge send true)) + (hhead : obj.Ntfn.head st n s = ok 0#u64) (hntfn : obj.Ntfn.bound_tcb st n s = ok 0#u64) + (hr : tcb.restart_caller st s = ok s1) : + tcb.decode st ar s a fl4_abi.label.TCBBindNotification t ctx = + ok (.Ok (), L.wr (L.wr s1 (ntfnBtA n) t) (boundA t) n, a) := by + rw [tcbBindNotification_programA hst] + have hlen : ctx.length ≥ 0#u64 := by scalar_tac + have hx : tcb.extra_with_length ctx 0#usize 0#u64 = ok (.Ok ns) := by + simp only [tcb.extra_with_length, hextra, bind_ok, hlen, ite_true] + have hn : tcb.bindable_ntfn st s ns = ok (.Ok n) := by + simp only [tcb.bindable_ntfn, hcap, bind_ok, ite_true, hhead, hntfn, + bne_self_eq_false, Bool.false_eq_true, ite_false] + have branchOk (v : U64) : + core.result.Result.Insts.CoreOpsTry.branch (.Ok v : core.result.Result U64 error.Exc) = + ok (.Continue v) := rfl + simp only [tcb.decode_bind_notification, hx, hn, branchOk, bind_ok, tcbBoundReadA, hbound, + bne_self_eq_false, Bool.false_eq_true, ite_false, hr, tcbBindNtfnWritesA, uncurry_apply_pair] + +theorem tcbUnbindNotification_exactA {s s1 : S} {a : A} {t n : U64} {ctx : tcb.Ctx} + (hst : smp.remote_tcb_stall st ar s a t = ok (s, a)) + (h0 : L.mem s (boundA t) ≠ 0#u64) + (hr : tcb.restart_caller st s = ok s1) (hn : n ≠ 0#u64) (h : L.mem s1 (boundA t) = n) : + tcb.decode st ar s a fl4_abi.label.TCBUnbindNotification t ctx = + ok (.Ok (), L.wr (L.wr s1 (ntfnBtA n) 0#u64) (boundA t) 0#u64, a) := by + rw [tcbUnbindNotification_programA hst, tcbBoundReadA, bind_ok, if_neg h0, hr, bind_ok, + tcbUnbindNtfnWritesA s1 t n hn h, bind_ok] + +theorem tcbUnbindNotification_emptyA {s : S} {a : A} {t : U64} {ctx : tcb.Ctx} + (hst : smp.remote_tcb_stall st ar s a t = ok (s, a)) (h0 : L.mem s (boundA t) = 0#u64) : + tcb.decode st ar s a fl4_abi.label.TCBUnbindNotification t ctx = + ok (.Err (.Sys .IllegalOperation), s, a) := by + rw [tcbUnbindNotification_programA hst, tcbBoundReadA, h0, bind_ok, if_pos rfl] + simp only [error.illegal, bind_ok] + +private theorem tcbNtfnBacklink_eqA {σ : State Unit} {p : ObjRef} {x : Notification} + (hk : σ.kheap p = some (.notification x)) (target : Option ObjRef) : + setNtfnObjRef (Z := Unit) ntfnBoundTcbUpdate p target σ = + ({((), updKSc σ p (.notification { x with ntfnBoundTcb := target }))}, False) := by + unfold setNtfnObjRef updateSkObjRef + rw [nd_bind_oneSc (getSimpleKo_ntfnSc hk)] + unfold setSimpleKo + rw [nd_bind_oneSc (getObject_eqSc hk)] + rw [nd_bind_oneSc (show (NonDet.assert (σ := State Unit) (isSimpleType (.notification x))) σ = + ({((), σ)}, False) by simp [NonDet.assert, isSimpleType, aType])] + rw [nd_bind_oneSc (show (NonDet.assert (σ := State Unit) + (partialInv KernelObject.notification (.notification x) ≠ none)) σ = ({((), σ)}, False) by + simp [NonDet.assert, partialInv_ntfnSc])] + exact setObject_eqSc hk rfl + +private theorem tcbAfterNtfnWriteA {σ : State Unit} {n t : ObjRef} {x : Notification} {y : Tcb} + (hk : σ.kheap n = some (.notification x)) (ht : σ.kheap t = some (.tcb y)) + (new : Option ObjRef) : + (updKSc σ n (.notification { x with ntfnBoundTcb := new })).kheap t = some (.tcb y) := by + have hne : t ≠ n := by + intro e + rw [e, hk] at ht + cases ht + rw [updKSc_kheap, Function.update_of_ne hne, ht] + +theorem tcbBindNotification_specA {σ : State Unit} {n t : U64} {x : Notification} {y : Tcb} + (hk : σ.kheap (specPtr n) = some (.notification x)) + (ht : σ.kheap (specPtr t) = some (.tcb y)) : + bindNotification (Z := Unit) (specPtr t) (specPtr n) σ = + ({((), updKSc (updKSc σ (specPtr n) (.notification { x with ntfnBoundTcb := some (specPtr t) })) + (specPtr t) (.tcb { y with tcbBoundNotification := some (specPtr n) }))}, False) := by + unfold bindNotification + rw [nd_bind_oneSc (tcbNtfnBacklink_eqA hk _)] + exact threadSet_eqSc _ (tcbAfterNtfnWriteA hk ht _) + +/-- TCB and IRQ-node unbinding use the same abstract two-link update. -/ +theorem tcbUnbindNotification_specA {σ : State Unit} {n t : U64} {x : Notification} {y : Tcb} + (hk : σ.kheap (specPtr n) = some (.notification x)) + (ht : σ.kheap (specPtr t) = some (.tcb y)) (hfwd : y.tcbBoundNotification = some (specPtr n)) : + unbindNotification (Z := Unit) (specPtr t) σ = + ({((), updKSc (updKSc σ (specPtr n) (.notification { x with ntfnBoundTcb := none })) + (specPtr t) (.tcb { y with tcbBoundNotification := none }))}, False) := by + have hn : getTcbObjRef (Z := Unit) Tcb.tcbBoundNotification (specPtr t) σ = + ({(some (specPtr n), σ)}, False) := by + unfold getTcbObjRef + rw [threadGet_eqA _ ht, hfwd] + unfold unbindNotification + rw [nd_bind_oneSc hn] + simp only [NonDet.maybeM, doUnbindNotification] + rw [nd_bind_oneSc (tcbNtfnBacklink_eqA hk none)] + exact threadSet_eqSc _ (tcbAfterNtfnWriteA hk ht none) + +end +end +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/TcbInvokeNtfnPostA.lean b/proofs/Fl4ProofsA64/P3/TcbInvokeNtfnPostA.lean new file mode 100644 index 000000000..198d911c7 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbInvokeNtfnPostA.lean @@ -0,0 +1,90 @@ +/- +P3 (AArch64): the binding operations match the two heap backlink +updates and frame the two concrete Store words after caller Restart. +[J] The target TCB and notification have their typed placement at that state. +[S] The target stall has no effects. +-/ +import Fl4ProofsA64.P3.TcbInvokeNtfnA +import Fl4ProofsA64.P3.DelSpecA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4SpecA64 Sched + +noncomputable section + +private theorem tcbNtfnPerformA {σ σ' : State Unit} {op : Fl4Spec.NonDet (State Unit) Unit} + (h : op σ = ({((), σ')}, False)) : + (.ok ([] : List Data), σ') ∈ + ((Fl4Spec.NonDetE.liftE (ε := Unit) (op >>= fun _ => Fl4Spec.NonDet.pure [])).run σ).1 := by + apply mem_liftED + refine mem_bind_ndD (x := ((), σ')) ?_ rfl + rw [h] + rfl + +section +variable {S A : Type} {st : machine.Store S} [L : LawfulStore st] {T N : Set U64} + +private theorem tcbNtfnWriteFrameA {s : S} {t n nt tn : U64} + (H : Notif.NPlaced T N) (ht : t ∈ T) (hn : n ∈ N) : + WritesIn st s (L.wr (L.wr s (ntfnBtA n) nt) (boundA t) tn) + (fun w => w = ntfnBtA n ∨ w = boundA t) := by + have ha : Aligned (ntfnBtA n) := (Notif.foot_bound H hn).1 + have hb : Aligned (boundA t) := field_aligned (H.tcb t ht) (K := 3368#u64) (by decide) (by decide) + exact (writesIn_wr ha).trans (writesIn_wr hb) + +/-- BindNotification produces the paired abstract backlink updates. +The write footprint contains exactly the two backlink fields. -/ +theorem tcbBindNotification_performA {ar : machine.Arch A} {s sRestart : S} {a : A} + {σRestart : State Unit} {t n ns badge : U64} {send : Bool} {ctx : tcb.Ctx} + (hst : smp.remote_tcb_stall st ar s a t = ok (s, a)) + (he : tcb.extra ctx 0#usize = ok (some ns)) (htFree : L.mem s (boundA t) = 0#u64) + (hc : obj.Slot.cap st ns s = ok (.Notification n badge send true)) + (hq : obj.Ntfn.head st n s = ok 0#u64) (hnFree : obj.Ntfn.bound_tcb st n s = ok 0#u64) + (hr : tcb.restart_caller st s = ok sRestart) + (H : Notif.NPlaced T N) (ht : t ∈ T) (hn : n ∈ N) + {x : Notification} {y : Tcb} + (hk : σRestart.kheap (specPtr n) = some (.notification x)) + (hy : σRestart.kheap (specPtr t) = some (.tcb y)) : + tcb.decode st ar s a fl4_abi.label.TCBBindNotification t ctx ⦃ r => + r.1 = .Ok () ∧ r.2.2 = a ∧ + r.2.1 = L.wr (L.wr sRestart (ntfnBtA n) t) (boundA t) n ∧ + WritesIn st sRestart r.2.1 (fun w => w = ntfnBtA n ∨ w = boundA t) ∧ + let σFinal := updKSc + (updKSc σRestart (specPtr n) (.notification { x with ntfnBoundTcb := some (specPtr t) })) + (specPtr t) (.tcb { y with tcbBoundNotification := some (specPtr n) }) + (.ok [], σFinal) ∈ + ((fl4InvokeTcb (.notificationControl (specPtr t) (some (specPtr n)))).run σRestart).1 ⦄ := by + rw [tcbBindNotification_exactA hst he htFree hc hq hnFree hr] + exact (WP.spec_ok _).2 ⟨rfl, rfl, rfl, tcbNtfnWriteFrameA H ht hn, + tcbNtfnPerformA (tcbBindNotification_specA hk hy)⟩ + +/-- UnbindNotification clears the two abstract backlinks and frames the +other Store words. It takes the live binding from the state after Restart. -/ +theorem tcbUnbindNotification_performA {ar : machine.Arch A} {s sRestart : S} {a : A} + {σRestart : State Unit} {t n : U64} {ctx : tcb.Ctx} + (hst : smp.remote_tcb_stall st ar s a t = ok (s, a)) + (hpre : L.mem s (boundA t) ≠ 0#u64) (hr : tcb.restart_caller st s = ok sRestart) + (hnz : n ≠ 0#u64) (hread : L.mem sRestart (boundA t) = n) + (H : Notif.NPlaced T N) (ht : t ∈ T) (hn : n ∈ N) + {x : Notification} {y : Tcb} + (hk : σRestart.kheap (specPtr n) = some (.notification x)) + (hy : σRestart.kheap (specPtr t) = some (.tcb y)) + (hback : y.tcbBoundNotification = some (specPtr n)) : + tcb.decode st ar s a fl4_abi.label.TCBUnbindNotification t ctx ⦃ r => + r.1 = .Ok () ∧ r.2.2 = a ∧ + r.2.1 = L.wr (L.wr sRestart (ntfnBtA n) 0#u64) (boundA t) 0#u64 ∧ + WritesIn st sRestart r.2.1 (fun w => w = ntfnBtA n ∨ w = boundA t) ∧ + let σFinal := updKSc + (updKSc σRestart (specPtr n) (.notification { x with ntfnBoundTcb := none })) + (specPtr t) (.tcb { y with tcbBoundNotification := none }) + (.ok [], σFinal) ∈ + ((fl4InvokeTcb (.notificationControl (specPtr t) none)).run σRestart).1 ⦄ := by + rw [tcbUnbindNotification_exactA hst hpre hr hnz hread] + exact (WP.spec_ok _).2 ⟨rfl, rfl, rfl, tcbNtfnWriteFrameA H ht hn, + tcbNtfnPerformA (tcbUnbindNotification_specA hk hy hback)⟩ + +end +end +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/TcbInvokePacA.lean b/proofs/Fl4ProofsA64/P3/TcbInvokePacA.lean new file mode 100644 index 000000000..0abd90912 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbInvokePacA.lean @@ -0,0 +1,237 @@ +/- +P3 (AArch64): SetPACKeys on the extracted TCB invocation path. +[P] The architecture probe agrees with fl4A64Platform.hasPauth, and +next_pac_epoch supplies the epoch word and resulting machine state. +[J] The IPC buffer argument words are aligned and disjoint from the +caller's state and the node scheduler-action word. Register separation +follows from TCB placement. +-/ +import Fl4ProofsA64.P3.TcbInvokeDispatchA +import Fl4ProofsA64.P1.QueueDecTcb2 +import Fl4ProofsA64.P1.MdbDelIn + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4SpecA64 + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S A : Type} {st : machine.Store S} [L : LawfulStore st] {ar : machine.Arch A} + +private theorem argRegIndexA {i r : U64} (h : msg.msg_register i = ok r) : + r.val = 2 ∨ r.val = 3 ∨ r.val = 4 ∨ r.val = 5 := by + have hv {v : U64} (e : ok v = ok r) : r = v := by + exact (WP.spec_ok (p := fun x => x = v) r).1 (e ▸ (WP.spec_ok _).2 rfl) + unfold msg.msg_register at h + split at h + · rw [hv h]; exact Or.inl (by simp) + · split at h + · rw [hv h]; exact Or.inr (Or.inl (by simp)) + · split at h + · rw [hv h]; exact Or.inr (Or.inr (Or.inl (by simp))) + · rw [hv h]; exact Or.inr (Or.inr (Or.inr (by simp))) + +/-- Restart can update the caller's STATE and its node's scheduler action. +The register and buffer arguments survive both writes. -/ +theorem tcbArg_restartA {s s1 : S} {T : Set U64} {ctx : tcb.Ctx} {i : U64} + (hr : tcb.restart_caller st s = ok s1) + (ht : L.curW s ∈ T) (hta : TcbAddr (L.curW s)) + (hn : NodePlaced st T s (L.cpu s)) + (hbuf : ¬ i < msg.N_MSG_REGISTERS → ctx.buf ≠ 0#u64 → + let b := bufWA ctx.buf (wa i 1#u64) + Aligned b ∧ b ≠ stA (L.curW s) ∧ b ≠ L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) : + tcb.arg st s1 ctx i = tcb.arg st s ctx i := by + have hw := W7.R.ST.tcb.restart_caller.sts hn hta + rw [hr, WP.spec_ok] at hw + have c0 : layout.node.CUR_THREAD.val < 4288 := by simp + have a0 : layout.node.SCHED_ACTION.val < 4288 := by simp + have hcur : L.curW s1 = L.curW s := by + unfold LawfulStore.curW + rw [hw.2.1, nodeAt_of_ksb hw.1] + exact hw.mem_eq (nodeAt_aligned hn c0 (by simp)) fun h => + h.elim (node_ne_field hn c0 ht hta (by simp)) (nodeAt_ne hn c0 a0 (by simp)) + unfold tcb.arg + simp only [cur_thread_ok, hcur, bind_ok] + unfold msg.syscall_arg + by_cases hi : i < msg.N_MSG_REGISTERS + · obtain ⟨r, hr⟩ := msg_register_okA i + simp only [if_pos hi, hr, bind_ok, tcb_reg_eqA] + let offset := wa layout.tcb.REGS (core.num.U64.wrapping_mul r 8#u64) + have hrge : 2 ≤ r.val ∧ r.val ≤ 5 := by rcases argRegIndexA hr with h|h|h|h <;> omega + have hoff : offset.val = 256 + r.val * 8 := by + simp [offset, core.num.U64.wrapping_add_val_eq, core.num.U64.wrapping_mul_val_eq, + U64.size_def, U64.numBits] + omega + have hre : regWA (L.curW s) r = wa (L.curW s) offset := by + unfold regWA offset + apply UScalar.eq_of_val_eq + simp [core.num.U64.wrapping_add_val_eq, U64.size_def, U64.numBits] + omega + have hne : stA (L.curW s) ≠ wa (L.curW s) offset := + field_ne hta hta (by simp) (by omega) (by simp; omega) + rw [hre, hw.mem_eq (field_aligned hta (by omega) (by omega)) (fun h => + h.elim (Ne.symm hne) (Ne.symm (node_ne_field hn a0 ht hta (by omega))))] + · rw [if_neg hi, if_neg hi] + by_cases hb : ctx.buf = 0#u64 + · simp [hb] + · obtain ⟨ha, hne, hna⟩ := hbuf hi hb + simp only [bne_iff_ne, if_pos hb, lift, bind_ok, buf_get_eqA] + rw [hw.mem_eq ha (fun h => h.elim hne hna)] + +/-- The key words are stored in ABI order starting at the PAC_KEYS field. -/ +def tcbPacKeyAddrA (t : U64) (i : Nat) : U64 := + wa (wa t layout.tcb.PAC_KEYS) ⟨BitVec.ofNat 64 (8 * i)⟩ + +private def pacWritePrefixA (st : machine.Store S) [L : LawfulStore st] (s : S) (t : U64) + (keys : Aeneas.Std.Array U64 10#usize) (n : Nat) : S := + (List.range n).foldl (fun q i => L.wr q (tcbPacKeyAddrA t i) (keys.val[i]?.getD 0#u64)) s + +/-- Ten key words followed by flags and epoch. The sequence models the Store +writes of a successful call, starting just after the caller's Restart. -/ +def tcbPacPostA (st : machine.Store S) [L : LawfulStore st] (s : S) (t : U64) + (keys : Aeneas.Std.Array U64 10#usize) (flags epoch : U64) : S := + L.wr (L.wr (pacWritePrefixA st s t keys 10) (wa t layout.tcb.PAC_FLAGS) flags) + (wa t layout.tcb.PAC_EPOCH) epoch + +private theorem pacWritePrefix_succA (s : S) (t : U64) (keys : Aeneas.Std.Array U64 10#usize) (n : Nat) : + pacWritePrefixA st s t keys (n + 1) = + L.wr (pacWritePrefixA st s t keys n) (tcbPacKeyAddrA t n) (keys.val[n]?.getD 0#u64) := by + simp only [pacWritePrefixA, List.range_succ, List.foldl_append, List.foldl_cons, List.foldl_nil] + +theorem tcbWritePacKeys_exactA (s : S) (t : U64) (keys : Aeneas.Std.Array U64 10#usize) : + tcb.write_pac_keys st s t keys = ok (pacWritePrefixA st s t keys 10) := by + apply eq_of_spec_fd + unfold tcb.write_pac_keys tcb.write_pac_keys_loop + apply loop.spec_decr_nat (measure := fun y => 10 - y.2.2.val) + (inv := fun y => y.2.1 = t ∧ y.2.2.val ≤ 10 ∧ y.1 = pacWritePrefixA st s t keys y.2.2.val) + · rintro ⟨q, t', i⟩ ⟨et, hi, eq⟩ + dsimp only at et hi eq + subst t' + dsimp only + unfold tcb.write_pac_keys_loop.body + by_cases hi' : i < 10#u64 + swap + · rw [if_neg hi'] + have he : i.val = 10 := by scalar_tac + exact (WP.spec_ok _).2 (eq.trans (congrArg (pacWritePrefixA st s t keys) he)) + rw [if_pos hi'] + have hi10 : i.val < 10 := by scalar_tac + simp only [lift, bind_ok] + step as ⟨addrOffset, haddrOffset⟩ + try simp only [lift, bind_ok] + have hcast := cast_usize_small (j := i) (by omega) + have hk : (UScalar.cast .Usize i : Usize).val < keys.val.length := by rw [hcast, keys.property]; simpa + refine WP.spec_bind (Aeneas.Std.Array.index_usize_spec _ _ hk) fun key hkey => ?_ + refine WP.spec_bind (LawfulStore.write_spec q _ key) fun q' e => ?_ + subst q' + step as ⟨j, hj⟩ + have haddr : addrOffset = ⟨BitVec.ofNat 64 (8 * i.val)⟩ := by + apply UScalar.eq_of_val_eq + change addrOffset.bv.toNat = (BitVec.ofNat 64 (8 * i.val)).toNat + simp [BitVec.toNat_ofNat, haddrOffset] + omega + have hword : key = keys.val[i.val]?.getD 0#u64 := by + rw [List.getElem?_eq_getElem (by rw [keys.property]; simpa)] + simpa [hcast] using hkey + have hnext : j.val = i.val + 1 := by omega + have hq : L.wr q (wa (wa t layout.tcb.PAC_KEYS) addrOffset) key = + pacWritePrefixA st s t keys j.val := by + rw [hnext, pacWritePrefix_succA, eq, haddr, hword] + rfl + first + | exact ⟨⟨rfl, by omega, hq⟩, by omega⟩ + | exact ⟨⟨by omega, hq⟩, by omega⟩ + | exact ⟨by omega, hq, by omega⟩ + | simp only [hnext, hq] <;> omega + · exact ⟨rfl, by change (0#u64 : U64).val ≤ 10; decide, rfl⟩ + +private theorem pacKeyAddr_valA {t : U64} (hta : TcbAddr t) {i : Nat} (hi : i < 10) : + (tcbPacKeyAddrA t i).val = t.val + 1696 + 8 * i := by + unfold tcbPacKeyAddrA + rw [core.num.U64.wrapping_add_val_eq, field_val hta (by simp)] + have hfit := hta.fits + change (t.val + layout.tcb.PAC_KEYS.val + (BitVec.ofNat 64 (8 * i)).toNat) % _ = _ + simp [BitVec.toNat_ofNat, U64.size_def, U64.numBits] + omega + +/-- The Store frame of the PAC tail covers just the twelve adjacent words. -/ +theorem tcbPacPost_frameA (s : S) (t : U64) (hta : TcbAddr t) + (keys : Aeneas.Std.Array U64 10#usize) (flags epoch : U64) : + L.FrameOut s (tcbPacPostA st s t keys flags epoch) (t.val + 1680) 96 := by + have hp : ∀ n ≤ 10, L.FrameOut s (pacWritePrefixA st s t keys n) (t.val + 1680) 96 := by + intro n + induction n with + | zero => intro _; exact LawfulStore.FrameOut.refl _ _ _ + | succ n ih => + intro hn + rw [pacWritePrefix_succA] + refine (ih (by omega)).trans (LawfulStore.FrameOut.wr ?_ ?_) + · unfold Aligned + rw [pacKeyAddr_valA hta (by omega)] + have := hta.align + omega + · rw [pacKeyAddr_valA hta (by omega)] + omega + unfold tcbPacPostA + have f₁ : layout.tcb.PAC_FLAGS.val = 1680 := by simp + have f₂ : layout.tcb.PAC_EPOCH.val = 1688 := by simp + refine ((hp 10 (by decide)).trans + (LawfulStore.FrameOut.wr (field_aligned hta (by omega) (by omega)) ?_)).trans + (LawfulStore.FrameOut.wr (field_aligned hta (by omega) (by omega)) ?_) + · rw [field_val hta (by omega), f₁] + omega + · rw [field_val hta (by omega), f₂] + omega + +/-- FL4's PAC probe and epoch call are explicit [P] premises. The key +arguments come from the entry state and are read again after Restart. -/ +theorem tcbSetPacKeys_exactA {s s1 : S} {a a' : A} {t flags epoch : U64} {ctx : tcb.Ctx} + {T : Set U64} (k0 k1 k2 k3 k4 k5 k6 k7 k8 k9 : U64) + (hst : smp.remote_tcb_stall st ar s a t = ok (s, a)) + (hlen : ¬ ctx.length < 11#u64) + (hflags : tcb.arg st s ctx 0#u64 = ok flags) + (hmask : flags &&& ~~~fl4_abi.consts.pac_flag.MASK = 0#u64) + (hprobe : flags ≠ 0#u64 → ar.has_pauth a = ok fl4A64Platform.hasPauth) + (hsupported : flags ≠ 0#u64 → fl4A64Platform.hasPauth = true) + (hepoch : ar.next_pac_epoch a = ok (epoch, a')) + (hr : tcb.restart_caller st s = ok s1) + (ht : L.curW s ∈ T) (hta : TcbAddr (L.curW s)) (hn : NodePlaced st T s (L.cpu s)) + (hbuf : ∀ i : U64, i.val < 11 → ¬ i < msg.N_MSG_REGISTERS → ctx.buf ≠ 0#u64 → + let b := bufWA ctx.buf (wa i 1#u64) + Aligned b ∧ b ≠ stA (L.curW s) ∧ b ≠ L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) + (h0 : tcb.arg st s ctx 1#u64 = ok k0) (h1 : tcb.arg st s ctx 2#u64 = ok k1) + (h2 : tcb.arg st s ctx 3#u64 = ok k2) (h3 : tcb.arg st s ctx 4#u64 = ok k3) + (h4 : tcb.arg st s ctx 5#u64 = ok k4) (h5 : tcb.arg st s ctx 6#u64 = ok k5) + (h6 : tcb.arg st s ctx 7#u64 = ok k6) (h7 : tcb.arg st s ctx 8#u64 = ok k7) + (h8 : tcb.arg st s ctx 9#u64 = ok k8) (h9 : tcb.arg st s ctx 10#u64 = ok k9) : + tcb.decode st ar s a fl4_abi.label.TCBSetPACKeys t ctx = + ok (.Ok (), tcbPacPostA st s1 t + (Array.make 10#usize [k0, k1, k2, k3, k4, k5, k6, k7, k8, k9]) flags epoch, a') := by + have harg (i : U64) (hi : i.val < 11) : + tcb.arg st s1 ctx i = tcb.arg st s ctx i := + tcbArg_restartA hr ht hta hn (hbuf i hi) + rw [tcbSetPACKeys_programA hst] + unfold tcb.decode_arch tcb.decode_set_pac_keys + simp only [ite_true, if_neg hlen, hflags, bind_ok, lift, hmask, bne_self_eq_false, + Bool.false_eq_true, ite_false] + have htail (i : U64) (hi : i.val < 11) (x : U64) (e : tcb.arg st s ctx i = ok x) : + tcb.arg st s1 ctx i = ok x := (harg i hi).trans e + by_cases hz : flags = 0#u64 + · simp only [hz, bne_self_eq_false, Bool.false_eq_true, ite_false, hr, bind_ok, + htail _ (by decide) _ h0, htail _ (by decide) _ h1, htail _ (by decide) _ h2, + htail _ (by decide) _ h3, htail _ (by decide) _ h4, htail _ (by decide) _ h5, + htail _ (by decide) _ h6, htail _ (by decide) _ h7, htail _ (by decide) _ h8, + htail _ (by decide) _ h9, tcbWritePacKeys_exactA, lift, L.write_eq, hepoch, + uncurry_apply_pair, tcbPacPostA] + · have hb : (flags != 0#u64) = true := bne_iff_ne.mpr hz + simp only [hb, ite_true, hprobe hz, hsupported hz, hr, bind_ok, + htail _ (by decide) _ h0, htail _ (by decide) _ h1, htail _ (by decide) _ h2, + htail _ (by decide) _ h3, htail _ (by decide) _ h4, htail _ (by decide) _ h5, + htail _ (by decide) _ h6, htail _ (by decide) _ h7, htail _ (by decide) _ h8, + htail _ (by decide) _ h9, tcbWritePacKeys_exactA, lift, L.write_eq, hepoch, + uncurry_apply_pair, tcbPacPostA] + +end +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/TcbInvokePacSelectA.lean b/proofs/Fl4ProofsA64/P3/TcbInvokePacSelectA.lean new file mode 100644 index 000000000..09b705742 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbInvokePacSelectA.lean @@ -0,0 +1,115 @@ +/- +P3 (AArch64): SetPACKeys' guard order, decoded key payload, and abstract result. +[P] The actual feature probe agrees with fl4A64Platform.hasPauth. +[J] The caller's covered argument buffer is word aligned. +[S] The target stall is quiet. +-/ +import Fl4ProofsA64.P3.TcbInvokePacA +import Fl4ProofsA64.P3.FrontEndExtraA +import Fl4ProofsA64.P3.DelSpecA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4SpecA64 + +/-- The concrete and abstract decoders reject the same out-of-mask flags. -/ +theorem tcbPacMask_eqA (flags : U64) : + flags &&& ~~~fl4_abi.consts.pac_flag.MASK = 0#u64 ↔ flags.bv &&& ~~~fl4PacFlagMask = 0 := by + unfold fl4_abi.consts.pac_flag.MASK fl4PacFlagMask + simp only [UScalar.eq_equiv_bv_eq, UScalar.bv_and, UScalar.bv_not] + rfl + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} +variable {s : S} {a : A} {t flags : U64} {ctx : tcb.Ctx} + +theorem tcbSetPacKeys_truncatedA (hst : smp.remote_tcb_stall st ar s a t = ok (s, a)) + (hLen : ctx.length < 11#u64) : + tcb.decode st ar s a fl4_abi.label.TCBSetPACKeys t ctx = + ok (.Err (.Sys .TruncatedMessage), s, a) := by + rw [tcbSetPACKeys_programA hst] + simp only [tcb.decode_arch, tcb.decode_set_pac_keys, ite_true, ite_eq_left hLen, + error.truncated, bind_ok] + +theorem tcbSetPacKeys_badFlagsA (hst : smp.remote_tcb_stall st ar s a t = ok (s, a)) + (hLen : ¬ctx.length < 11#u64) (hArg : tcb.arg st s ctx 0#u64 = ok flags) + (hMask : flags &&& ~~~fl4_abi.consts.pac_flag.MASK ≠ 0#u64) : + tcb.decode st ar s a fl4_abi.label.TCBSetPACKeys t ctx = + ok (.Err (.Sys (.InvalidArgument 0#u64)), s, a) := by + rw [tcbSetPACKeys_programA hst] + simp only [tcb.decode_arch, tcb.decode_set_pac_keys, ite_true, ite_eq_right hLen, + hArg, bind_ok, lift, bne_iff_ne.mpr hMask, error.invalid_arg] + +theorem tcbSetPacKeys_unsupportedA (hst : smp.remote_tcb_stall st ar s a t = ok (s, a)) + (hLen : ¬ctx.length < 11#u64) (hArg : tcb.arg st s ctx 0#u64 = ok flags) + (hMask : flags &&& ~~~fl4_abi.consts.pac_flag.MASK = 0#u64) (hNonzero : flags ≠ 0#u64) + (hProbe : ar.has_pauth a = ok false) : + tcb.decode st ar s a fl4_abi.label.TCBSetPACKeys t ctx = + ok (.Err (.Sys .IllegalOperation), s, a) := by + rw [tcbSetPACKeys_programA hst] + simp only [tcb.decode_arch, tcb.decode_set_pac_keys, ite_true, ite_eq_right hLen, hArg, + bind_ok, lift, hMask, bne_self_eq_false, Bool.false_eq_true, ite_false, + bne_iff_ne.mpr hNonzero, hProbe, error.illegal] + +end + +private theorem pacArg_runA {σ : State Unit} {caller : Tcb} (hCaller : σ.kheap σ.curThread = some (.tcb caller)) + (buf : Option ObjRef) + (hAlign : ∀ b, buf = some b → ∀ k, k < 128 → (bufAddrA b k).toNat % 8 = 0) + (i : Nat) (hi : i < 127) : + (fl4Arg (Z := Unit) buf i).run σ = + ({(.ok (specArgA caller.tcbArch.tcbContext.userRegs buf σ.machineState.underlyingMemory i), σ)}, False) := by + unfold fl4Arg + have hGet : (Fl4Spec.NonDet.gets fun s : State Unit => s.curThread) σ = ({(σ.curThread, σ)}, False) := rfl + rw [run_bind_okD (run_liftE_stD hGet)] + exact run_liftE_stD (fl4GetMr_eqA hCaller buf hAlign i hi) + +private theorem pacArgs_runA {σ : State Unit} {buf : Option ObjRef} {value : Nat → Data} + (indices : List Nat) (hArgs : ∀ i ∈ indices, (fl4Arg (Z := Unit) buf i).run σ = ({(.ok (value i), σ)}, False)) : + (Fl4Spec.NonDetE.mapME (fl4Arg buf) indices).run σ = ({(.ok (indices.map value), σ)}, False) := by + induction indices with + | nil => rfl + | cons i is ih => + have hArg := hArgs i (List.mem_cons_self ..) + have hTail := ih (fun j hj => hArgs j (List.mem_cons_of_mem i hj)) + simp only [Fl4Spec.NonDetE.mapME, List.map_cons] + rw [run_bind_okD hArg, run_bind_okD hTail] + rfl + +/-- The accepted decoder reads the flag and ten key words from the related +caller view, in the same order as the concrete post-Restart reads. -/ +theorem tcbSetPacKeys_selectedSpecA {σ : State Unit} {caller : Tcb} {target : ObjRef} + {buf : Option ObjRef} {args : List Data} {value : Nat → Data} + (hCaller : σ.kheap σ.curThread = some (.tcb caller)) + (hAlign : ∀ b, buf = some b → ∀ k, k < 128 → (bufAddrA b k).toNat % 8 = 0) + (hLen : ¬args.length < 11) + (hValue : ∀ i, i < 11 → value i = + specArgA caller.tcbArch.tcbContext.userRegs buf σ.machineState.underlyingMemory i) + (hMask : value 0 &&& ~~~fl4PacFlagMask = 0) + (hSupport : value 0 ≠ 0 → fl4A64Platform.hasPauth = true) : + (decodeFl4SetPacKeys (Z := Unit) args buf target).run σ = + ({(.ok (.setPacKeys target (value 0) ((List.range' 1 10).map value)), σ)}, False) := by + have hArg (i : Nat) (hi : i < 11) : (fl4Arg (Z := Unit) buf i).run σ = ({(.ok (value i), σ)}, False) := by + rw [hValue i hi] + exact pacArg_runA hCaller buf hAlign i (by omega) + have hArgs : ∀ i ∈ List.range' 1 10, (fl4Arg (Z := Unit) buf i).run σ = ({(.ok (value i), σ)}, False) := by + intro i hi + apply hArg + rw [List.mem_range'] at hi + omega + have hBad : ¬(value 0 ≠ 0 ∧ fl4A64Platform.hasPauth = false) := + fun h => Bool.noConfusion ((hSupport h.1).symm.trans h.2) + have hSkip : (pure () : SEMonad Unit Unit).run σ = ({(.ok (), σ)}, False) := rfl + simp only [decodeFl4SetPacKeys, Fl4Spec.NonDetE.whenE, ite_eq_right hLen] + rw [run_bind_okD hSkip, run_bind_okD (hArg 0 (by decide))] + simp only [hMask, ne_self_iff_false, ite_false, ite_eq_right hBad] + rw [run_bind_okD hSkip, run_bind_okD hSkip, run_bind_okD (pacArgs_runA _ hArgs)] + rfl + +/-- PAC storage and epoch are concrete observations. The abstract invocation +returns an empty reply at the post-Restart state. -/ +theorem tcbSetPacKeys_specPostA (σ : State Unit) (target : ObjRef) (flags : Data) (keys : List Data) : + (invokeFl4Tcb (.setPacKeys target flags keys)).run σ = ({(.ok [], σ)}, False) := rfl + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/TcbInvokePriorityA.lean b/proofs/Fl4ProofsA64/P3/TcbInvokePriorityA.lean new file mode 100644 index 000000000..72f1a6ba3 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbInvokePriorityA.lean @@ -0,0 +1,82 @@ +/- +P3 (AArch64): the SetPriority update for an inactive target. +[S] Target stall is quiet. +[J] The typed target and state relation are observed after caller Restart. +The pre-Restart raw MCP comparison authorises the new priority. +-/ +import Fl4ProofsA64.P3.TcbInvokeMcpA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4SpecA64 Sched + +section +variable {S A : Type} {st : machine.Store S} [L : LawfulStore st] + +set_option trace.profiler true in +private theorem schedPriority_inactiveA {s : S} {t prio : U64} + (hState : L.mem s (stA t) = 0#u64) : + sched.set_priority st s t prio = ok (L.wr s (prioA t) prio) := by + rw [sched.set_priority, eq_of_spec_fd (Fl4ProofsA64.P1.obj.Tcb.state.spec s t), bind_ok, hState] + simp (config := {decide := true}) only [sched.TS_RUNNING, sched.TS_RESTART, + sched.TS_RUNNING_VM, sched.TS_BLOCKED_ON_RECV, sched.TS_BLOCKED_ON_SEND, + sched.TS_BLOCKED_ON_NTFN, ite_false] + exact eq_of_spec_fd (obj.Tcb.set_prio.spec s t prio) + +set_option trace.profiler true in +private theorem tcbPriority_inactiveSpecA {σ : State Unit} {t auth : ObjRef} {slot : CslotPtr} + {x : Tcb} (prio : Priority) (hk : σ.kheap t = some (.tcb x)) (hState : x.tcbState = .inactive) : + (fl4InvokeTcb (.threadControlSched t slot none none (some (prio, auth)) none)).run σ = + ({(.ok [], updKSc σ t (.tcb {x with tcbPriority := prio}))}, False) := by + have hGet := Notif.getThreadState_eq hk + rw [hState] at hGet + have hSet : setPriority t prio σ = + ({((), updKSc σ t (.tcb {x with tcbPriority := prio}))}, False) := by + unfold setPriority + rw [nd_bind_oneSc hGet] + rw [if_neg (show ¬ runnable (.inactive : ThreadState) = true by decide)] + change (threadSet (Z := Unit) (fun y => {y with tcbPriority := prio}) t >>= _) σ = _ + rw [nd_bind_oneSc (threadSet_eqSc (fun y => {y with tcbPriority := prio}) hk)] + change (Fl4Spec.NonDet.pure () >>= fun _ => Fl4Spec.NonDet.pure ()) _ = _ + exact nd_bind_oneSc (m := Fl4Spec.NonDet.pure ()) (a := ()) rfl _ + simp only [fl4InvokeTcb, fl4InstallTcbCap, Fl4Spec.NonDet.maybeM] + rw [run_bind_okD (run_returnOkD () σ), + run_bind_okD (run_liftE_stD (m := Fl4Spec.NonDet.pure ()) rfl), + run_bind_okD (run_liftE_stD hSet), + run_bind_okD (run_liftE_stD (m := Fl4Spec.NonDet.pure ()) rfl)] + rfl + +set_option trace.profiler true in +/-- SetPriority on an inactive target updates the raw word and the matching +abstract TCB field. MCP authorisation ensures the abstract value fits. -/ +theorem tcbSetPriority_inactivePerformA {ar : machine.Arch A} {s sRestart : S} {a : A} + {σ σRestart : State Unit} {T : Set U64} {t p auth argSlot : U64} + {slot : CslotPtr} {ctx : tcb.Ctx} {x : Tcb} + (hst : smp.remote_tcb_stall st ar s a t = ok (s, a)) + (he : tcb.extra_with_length ctx 0#usize 1#u64 = ok (.Ok argSlot)) + (hp : tcb.arg st s ctx 0#u64 = ok p) + (ha : tcb.thread_arg st s argSlot 1#u64 = ok (.Ok auth)) + (hle : ¬L.mem s (mcpA auth) < p) (hr : tcb.restart_caller st s = ok sRestart) + (hPre : McpRelA T m (L.mem s) σ) (haT : auth ∈ T) + (hSt : StEqA T (L.mem sRestart) σRestart) (ht : t ∈ T) + (hk : σRestart.kheap (specPtr t) = some (.tcb x)) (hInactive : x.tcbState = .inactive) : + let p' := BitVec.ofNat 8 p.val + let σFinal := updKSc σRestart (specPtr t) (.tcb {x with tcbPriority := p'}) + tcb.decode st ar s a fl4_abi.label.TCBSetPriority t ctx = + ok (.Ok (), L.wr sRestart (prioA t) p, a) ∧ + (fl4InvokeTcb (.threadControlSched (specPtr t) slot none none (some (p', specPtr auth)) none)).run + σRestart = ({(.ok [], σFinal)}, False) ∧ p'.toNat = p.val := by + dsimp only + have hWord := hSt t ht x hk + rw [hInactive, tsCodeA] at hWord + have hZero : L.mem sRestart (stA t) = 0#u64 := UScalar.eq_of_val_eq hWord + obtain ⟨authority, _, hMcp, -⟩ := hPre auth haT + have hpBound : p.val ≤ (L.mem s (mcpA auth)).val := Nat.le_of_not_lt hle + rw [hMcp] at hpBound + have hWidth : p.val < 256 := Nat.lt_of_le_of_lt hpBound authority.tcbMcpriority.isLt + refine ⟨?_, tcbPriority_inactiveSpecA _ hk hInactive, Nat.mod_eq_of_lt hWidth⟩ + rw [tcbSetPriority_postA hst he hp ha hle hr, schedPriority_inactiveA hZero, bind_ok] + +end +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/TcbInvokeReadCallA.lean b/proofs/Fl4ProofsA64/P3/TcbInvokeReadCallA.lean new file mode 100644 index 000000000..ce001fb67 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbInvokeReadCallA.lean @@ -0,0 +1,109 @@ +/- +P3 (AArch64): concrete Call reply of ReadRegisters. The inline loop +runs first. The receive buffer can carry the remaining frame and GP +words. The final badge, message tag and Running actions are explicit. +-/ +import Fl4ProofsA64.P3.TcbInvokeReadLoopsA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4SpecA64 + +section +variable {S A : Type} {st : machine.Store S} [L : LawfulStore st] {ar : machine.Arch A} + +/-- ReadRegisters payload after the badge has been cleared. -/ +def tcbReadRegisters_dataProgramA (s : S) (src caller n buf : U64) : Result (S × U64) := do + let (sInline, i) ← tcb.read_registers_loop0 st s src n caller 0#u64 + let (sFrame, frameCount) ← + if buf != 0#u64 then + if i < n ∧ i < tcb.N_FRAME then tcb.read_registers_loop1 st sInline src n buf i + else ok (sInline, i) + else ok (sInline, i) + let (sGp, gpCount) ← + if buf != 0#u64 then + if 17#u64 < n then tcb.read_registers_loop3 st sFrame src n buf 0#u64 + else ok (sFrame, 0#u64) + else ok (sFrame, 0#u64) + ok (sGp, core.num.U64.wrapping_add gpCount frameCount) + +set_option maxHeartbeats 1400000 in +/-- The extracted Call sends a zero badge and a zero-label, no-caps reply. +The caller used for all of these writes is captured before source suspension. -/ +theorem tcbReadRegisters_callProgramA (s : S) (a : A) (src n : U64) (suspendSource : Bool) : + tcb.read_registers st ar s a src suspendSource n true = (do + let caller := L.curW s + let (sSuspend, aSuspend) ← + if suspendSource then sched.suspend st ar s a src else ok (s, a) + let buf ← msg.lookup_ipc_buffer st sSuspend true caller + let sBadge ← obj.Tcb.set_reg st caller sSuspend layout.reg.BADGE 0#u64 + let (sGp, count) ← tcbReadRegisters_dataProgramA (st := st) sBadge src caller n buf + let tag ← msginfo.MessageInfo.new 0#u64 0#u64 0#u64 count + let sTag ← msg.set_msg_info st sGp caller tag + let sRun ← sched.set_thread_state st sTag caller sched.TS_RUNNING + ok (sRun, aSuspend)) := by + simp only [tcb.read_registers, tcbReadRegisters_dataProgramA, cur_thread_ok, + Std.bind_assoc, bind_ok, uncurry_apply_pair, ite_true] + congr 1 + funext suspended + obtain ⟨sSuspend, aSuspend⟩ := suspended + simp only [uncurry_apply_pair] + congr 1 + funext buf + congr 1 + funext sBadge + congr 1 + funext inline + obtain ⟨sInline, i⟩ := inline + simp only [uncurry_apply_pair] + have h2 (u : S) := tcbReadRegisters_gpInlineEmptyA (st := st) u src n (L.curW s) + have h4 (u : S) := (tcbReadRegisters_gpInlineEqA (st := st) u src n (L.curW s) 0#u64).1 + have h6 (u : S) := (tcbReadRegisters_gpInlineEqA (st := st) u src n (L.curW s) 0#u64).2.1 + have h8 (u : S) := (tcbReadRegisters_gpInlineEqA (st := st) u src n (L.curW s) 0#u64).2.2 + have h5 (u : S) := (tcbReadRegisters_gpBufferEqA (st := st) u src n buf 0#u64).1 + have h7 (u : S) := (tcbReadRegisters_gpBufferEqA (st := st) u src n buf 0#u64).2.1 + have h9 (u : S) := (tcbReadRegisters_gpBufferEqA (st := st) u src n buf 0#u64).2.2 + have hGP : 0#u64 < tcb.N_GP := by rw [tcb.N_GP]; decide + have hAdd : 0#u64 + tcb.N_FRAME = ok 17#u64 := by rw [tcb.N_FRAME]; rfl + by_cases hBuf : (buf != 0#u64) = true + · by_cases hCount : i < n + · by_cases hFrame : i < tcb.N_FRAME + · simp only [hBuf, hCount, hFrame, and_self, ite_true, h2, bind_ok, uncurry_apply_pair, + hGP, hAdd, lift] + by_cases hMore : 17#u64 < n <;> + simp only [hMore, ite_true, ite_false, Std.bind_assoc, bind_ok, uncurry_apply_pair] <;> + (congr 1; funext q; obtain ⟨q, j⟩ := q; simp only [Std.bind_assoc, bind_ok, uncurry_apply_pair]) <;> + (congr 1; funext q; obtain ⟨q, j⟩ := q; simp only [bind_ok, uncurry_apply_pair]) + · simp only [hBuf, hCount, hFrame, and_false, ite_true, ite_false, h4, h2, bind_ok, + uncurry_apply_pair, hGP, hAdd, h5, lift] + by_cases hMore : 17#u64 < n <;> + simp only [hMore, ite_true, ite_false, Std.bind_assoc, bind_ok, uncurry_apply_pair] <;> + (congr 1; funext q; obtain ⟨q, j⟩ := q; simp only [bind_ok, uncurry_apply_pair]) + · simp only [hBuf, hCount, false_and, ite_true, ite_false, h6, h2, bind_ok, + uncurry_apply_pair, hGP, hAdd, h7, lift] + by_cases hMore : 17#u64 < n <;> + simp only [hMore, ite_true, ite_false, Std.bind_assoc, bind_ok, uncurry_apply_pair] <;> + (congr 1; funext q; obtain ⟨q, j⟩ := q; simp only [bind_ok, uncurry_apply_pair]) + · simp only [ite_eq_right hBuf, h8, h2, bind_ok, uncurry_apply_pair, lift] + +/-- A bounded ReadRegisters count fits the tag field. With a buffer the +frame and GP counts sum to the requested count. Without one the inline +count is the whole reply. -/ +theorem tcbReadRegisters_replyCountA {n frameCount gpCount : U64} + (hBound : n.val ≤ 36) (hasBuffer : Bool) + (hFrame : frameCount.val = if hasBuffer then min n.val 17 else min n.val 4) + (hGp : gpCount.val = if hasBuffer then min 19 (n.val - 17) else 0) : + (core.num.U64.wrapping_add gpCount frameCount).val = + (if hasBuffer then n.val else min n.val 4) := by + have hSmall : gpCount.val + frameCount.val < 2 ^ 64 := by + cases hasBuffer <;> simp only [Bool.false_eq_true, ite_false, ite_true] at hFrame hGp <;> omega + have hWrap : (core.num.U64.wrapping_add gpCount frameCount).val = + gpCount.val + frameCount.val := by + simp [core.num.U64.wrapping_add_val_eq, U64.size_def, U64.numBits] + omega + rw [hWrap] + cases hasBuffer <;> simp only [Bool.false_eq_true, ite_false, ite_true] at hFrame hGp ⊢ <;> omega + +end +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/TcbInvokeReadCallPerformA.lean b/proofs/Fl4ProofsA64/P3/TcbInvokeReadCallPerformA.lean new file mode 100644 index 000000000..f50ab8f51 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbInvokeReadCallPerformA.lean @@ -0,0 +1,96 @@ +/- +P3 (AArch64): the accepted ReadRegisters Call without source suspension. +The concrete reply of `TcbInvokeReadCallPostA` is followed by the caller's +Running transition. [J] The scheduler relation, the endpoint/message relation, +the caller's buffer coverage and the source and caller TCBs are available at the +post-Restart state; the instance `O` of the endpoint scheduler interface over +`SchedSRA` is produced at Stage-3 integration. [S] The target stall has no +effects, and this arm does not request source suspension. +-/ +import Fl4ProofsA64.P3.TcbInvokeReadCallPostA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4SpecA64 Endp Sched + +noncomputable section + +section +variable {S A : Type} {st : machine.Store S} [L : LawfulStore st] {ar : machine.Arch A} + +theorem tcbReadRegisters_callPerformA + {Z : XSetsA} {m : McState} {κ : U64 → U64} {E R : Set U64} {Bf : Set (U64 × Word)} + {th : TypedHeapA} {s sRestart : S} {a : A} {σRestart : State Unit} + {t flags count : U64} {ctx : tcb.Ctx} {src caller : Tcb} {block donate : Bool} + (hst : smp.remote_tcb_stall st ar s a t = ok (s, a)) + (hlen : ¬ctx.length < 2#u64) + (hf : tcb.arg st s ctx 0#u64 = ok flags) (hn : tcb.arg st s ctx 1#u64 = ok count) + (hlo : ¬count < 1#u64) (hhi : ¬36#u64 < count) (hself : t ≠ L.curW s) + (hr : tcb.restart_caller st s = ok sRestart) + (hcall : ctx.call = true) (hsuspend : (flags &&& 1#u64) = 0#u64) + (O : SchedOpsEA st Z.T E R Bf (SchedSRA st Z m κ)) (P : EPlaced Z.T E R Bf) + (H : SchedSRA st Z m κ sRestart σRestart) + (hE : EpStateRel Z.T E R Bf (L.mem sRestart) σRestart) + (hSt : StateRelA th (L.mem sRestart) σRestart) (hC : BufCover st Z.T Bf sRestart σRestart) + (hNative : BufNativeA Z.T (L.mem sRestart)) + (ht : t ∈ Z.T) (hne : t ≠ L.curW sRestart) (htc : th (L.curW sRestart) = some .tcb) + (hsrc : σRestart.kheap (specPtr t) = some (.tcb src)) + (hcaller : σRestart.kheap (specPtr (L.curW sRestart)) = some (.tcb caller)) + (hu : ∀ rp, ipcBufOfA true caller = some rp → ∀ k, k < 128 → + inUserFrame (bufAddr rp k) σRestart) + (hrun : runnable caller.tcbState = true) : + tcb.decode st ar s a fl4_abi.label.TCBReadRegisters t ctx ⦃ r => + r.1 = .Ok () ∧ r.2.2 = a ∧ ∃ σFinal, + (.ok [], σFinal) ∈ ((fl4PerformInvocation block true donate + (.invokeTCB (.readRegisters (specPtr t) false count.bv ArchDefaultExtraRegisters))).run σRestart).1 ∧ + SchedSRA st Z m κ r.2.1 σFinal ∧ StEqA Z.T (L.mem r.2.1) σFinal ∧ + WritesIn st sRestart r.2.1 (fun b => SWrA (st := st) Z sRestart b ∨ + EWrite Z.T E R Bf b ∨ b = stA (L.curW sRestart)) ⦄ := by + have hN : count.val ≤ 36 := by + have h : ¬ 36 < count.val := hhi + omega + rw [tcbReadRegisters_acceptedA hst hlen hf hn hlo hhi hself hr, hcall, hsuspend] + refine WP.spec_bind (Pₘ := fun r : S × A => r.2 = a ∧ ∃ σFinal, + (.ok [], σFinal) ∈ ((fl4PerformInvocation block true donate + (.invokeTCB (.readRegisters (specPtr t) false count.bv ArchDefaultExtraRegisters))).run σRestart).1 ∧ + SchedSRA st Z m κ r.1 σFinal ∧ StEqA Z.T (L.mem r.1) σFinal ∧ + WritesIn st sRestart r.1 (fun b => SWrA (st := st) Z sRestart b ∨ + EWrite Z.T E R Bf b ∨ b = stA (L.curW sRestart))) ?_ ?_ + swap + · rintro ⟨sFinal, aFinal⟩ ⟨h1, h2⟩ + exact (WP.spec_ok _).2 ⟨rfl, h1, h2⟩ + rw [tcbReadRegisters_callProgramA] + simp only [bne_self_eq_false, Bool.false_eq_true, ite_false, bind_ok] + have hc : L.curW sRestart ∈ Z.T := H.curT + refine tcbReadRegisters_callReplyKA O P sRestart σRestart hE H hSt hC hNative ht hc hne htc hsrc hcaller + hu hN _ ?_ + rintro sTag ⟨σT, hReply, hET, hST, hWT, ⟨xT, hkT, hxT⟩, hOth⟩ + have hStT : StEqA Z.T (L.mem sTag) σT := fun _ ht' _ hx => (hET.at_tcb ht' hx).st + have hstate : xT.tcbState = caller.tcbState := by + have h := congrArg Tcb.tcbState hxT + exact h + have hrunT : runnable xT.tcbState = true := by rw [hstate]; exact hrun + have hv : sched.TS_RUNNING.val = tsCodeA .running := by rw [ts_vals.1]; rfl + refine WP.spec_bind (sc_set_runnable_stateA hST hStT hc hkT hrunT (ts := .running) rfl hv) ?_ + rintro sFinal ⟨σFinal, hrunSpec, HFinal, hStateFinal, _, hFrame⟩ + refine (WP.spec_ok _).2 ⟨rfl, σFinal, ?_, HFinal, hStateFinal, ?_⟩ + · have hFinish : fl4FinishTcbReply true (((frame_registers ++ gp_registers).take count.bv.toNat).map + src.tcbArch.tcbContext.userRegs) σRestart = ({((), σFinal)}, False) := by + unfold fl4FinishTcbReply + rw [nd_bind_oneSc (show (Fl4Spec.NonDet.gets fun s : State Unit => s.curThread) σRestart = + ({(σRestart.curThread, σRestart)}, False) from rfl)] + rw [H.sch.curThread] + rw [nd_bind_oneSc (show (Fl4Spec.NonDet.when true (replyFromKernel (Z := Unit) (specPtr (L.curW sRestart)) + (0, ((frame_registers ++ gp_registers).take count.bv.toNat).map src.tcbArch.tcbContext.userRegs))) σRestart = + ({((), σT)}, False) from hReply)] + exact hrunSpec + unfold fl4PerformInvocation + rw [run_bind_okD (readRegsSpec_noSuspendA hsrc count.bv), run_bind_okD (run_liftE_stD hFinish)] + rfl + · exact (hWT.trans hFrame).mono fun b hb => hb.elim (fun h => Or.inr (Or.inl h)) + (fun h => h.elim (fun h => Or.inl (swr_ksb rfl hWT.1 h)) fun h => Or.inr (Or.inr h)) + +end +end +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/TcbInvokeReadCallPostA.lean b/proofs/Fl4ProofsA64/P3/TcbInvokeReadCallPostA.lean new file mode 100644 index 000000000..5fb11d2c6 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbInvokeReadCallPostA.lean @@ -0,0 +1,139 @@ +/- +P3 (AArch64): the concrete ReadRegisters Call reply (lookup, zero badge, payload, +message tag) refines the abstract `replyFromKernel` over the selected source +registers. [J] The caller is a live, related TCB distinct from the source, its +receive buffer is covered by the message relation (`BufCover`, `BufNativeA`) and +its physical user frames (`hu`). The caller's thread-state update is separate. +-/ +import Fl4ProofsA64.P3.TcbInvokeReadReplyA +import Fl4ProofsA64.P3.TcbInvokeReadPostA +import Fl4ProofsA64.P3.EpFaultXferA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4SpecA64 Endp + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {T E R : Set U64} {Bf : Set (U64 × Word)} {SR : S → State Unit → Prop} + +/-- The words a ReadRegisters count `n ≤ 36` delivers have length `n`. -/ +private theorem readWordsLengthA (regs : Register → Word) (n : Nat) (hN : n ≤ 36) : + (((frameRegisters ++ gpRegisters).take n).map regs).length = n := by + simp only [List.length_map, List.length_take, List.length_append, + show frameRegisters.length = 17 from rfl, show gpRegisters.length = 19 from rfl] + omega + +private theorem countBvA {count : U64} {k : Nat} (h : count.val = k) (hk : k < 2 ^ 64) : + count.bv = natToLen k := by + apply BitVec.eq_of_toNat_eq + unfold natToLen + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt hk] + exact h + +theorem tcbReadRegisters_callReplyKA {X : Type} {Q : X → Prop} (O : SchedOpsEA st T E R Bf SR) (P : EPlaced T E R Bf) + {th : TypedHeapA} (s : S) (σ : State Unit) + (hE : EpStateRel T E R Bf (L.mem s) σ) (hS : SR s σ) + (hSt : StateRelA th (L.mem s) σ) (hC : BufCover st T Bf s σ) (hNative : BufNativeA T (L.mem s)) + {src caller n : U64} (hs : src ∈ T) (hc : caller ∈ T) (hne : src ≠ caller) + (htc : th caller = some .tcb) {xs xc : Tcb} + (hks : σ.kheap (specPtr src) = some (.tcb xs)) (hkc : σ.kheap (specPtr caller) = some (.tcb xc)) + (hu : ∀ rp, ipcBufOfA true xc = some rp → ∀ k, k < 128 → inUserFrame (bufAddr rp k) σ) + (hN : n.val ≤ 36) (K : S → Result X) + (hK : ∀ s', (∃ σ', + replyFromKernel (specPtr caller) + (0, ((frameRegisters ++ gpRegisters).take n.val).map xs.tcbArch.tcbContext.userRegs) σ = + ({((), σ')}, False) ∧ + EpStateRel T E R Bf (L.mem s') σ' ∧ SR s' σ' ∧ WritesIn st s s' (EWrite T E R Bf) ∧ + (∃ xc', σ'.kheap (specPtr caller) = some (.tcb xc') ∧ xc' = { xc with tcbArch := xc'.tcbArch }) ∧ + (∀ p, p ≠ specPtr caller → σ'.kheap p = σ.kheap p)) → K s' ⦃ Q ⦄) : + (do + let buf ← msg.lookup_ipc_buffer st s true caller + let sBadge ← obj.Tcb.set_reg st caller s layout.reg.BADGE 0#u64 + let (sGp, count) ← tcbReadRegisters_dataProgramA (st := st) sBadge src caller n buf + let tag ← msginfo.MessageInfo.new 0#u64 0#u64 0#u64 count + let sTag ← msg.set_msg_info st sGp caller tag + K sTag) ⦃ Q ⦄ := by + have hu' : ∀ rp, ipcBufOfA true xc = some rp → ∀ k, k < 128 → + inUserFrame (bufAddr rp k) + (Notif.updK σ (specPtr caller) (.tcb (Notif.setReg xc badge_register 0))) := + fun rp h k hk => inUserFrame_updK hkc (fun _ h => by cases h) _ (hu rp h k hk) + have hOther : specPtr caller ≠ specPtr src := fun h => hne (specPtr_inj h).symm + have hksB : (Notif.updK σ (specPtr caller) (.tcb (Notif.setReg xc badge_register 0))).kheap + (specPtr src) = some (.tcb xs) := by + rw [Notif.updK_kheap, Function.update_of_ne (Ne.symm hOther)] + exact hks + have hxB : ∃ a, Notif.setReg xc badge_register 0 = { xc with tcbArch := a } := ⟨_, rfl⟩ + refine WP.spec_bind (lookup_buf_full s σ hSt htc hc hkc hC hNative true) ?_ + rintro w ⟨hw0, hwB⟩ + refine WP.spec_bind (tcbReadRegisters_badgePostA O P hc hkc hE hS) ?_ + rintro sB ⟨hsB, hBadge, hkB, hEB, hSB, hWB⟩ + obtain ⟨aB, hxBdef⟩ := hxB + generalize hσB : Notif.updK σ (specPtr caller) (.tcb (Notif.setReg xc badge_register 0)) = σB at * + generalize Notif.setReg xc badge_register 0 = xB at * + have hOthers : ∀ p, p ≠ specPtr caller → σB.kheap p = σ.kheap p := by + intro p hp + rw [← hσB, Notif.updK_kheap, Function.update_of_ne hp] + have hRegs : ∀ r, (L.mem sB (Endp.regA src (regIdxA r))).bv = xs.tcbArch.tcbContext.userRegs r := + fun r => (hEB.at_tcb hs hksB).regs r + rcases buf_caseA hw0 hwB with ⟨hnone, rfl⟩ | ⟨bp, hbp, hsome, rfl, hbp0⟩ + · -- no receive buffer: the message is truncated to the four inline registers + have hM := (msgLaw_nobuf O.to0 P hEB hSB hc hkB).withW P hc (Or.inl rfl) + refine WP.spec_bind (tcbReadRegisters_dataA P hs hc hne hM hRegs hN) ?_ + rintro ⟨sGp, count⟩ ⟨hCount, hI, hWI⟩ + have hCount' : count.val = min n.val 4 := by simpa using hCount + set ms := ((frameRegisters ++ gpRegisters).take n.val).map xs.tcbArch.tcbContext.userRegs with hms + have hlen : ms.length = n.val := readWordsLengthA _ _ hN + have hcountBv : count.bv = natToLen (min 4 ms.length) := by + rw [hlen, Nat.min_comm] + exact countBvA hCount' (by omega) + refine tcbReadRegisters_tagPostKA O P hc + (mrStateNA_kheap σB (specPtr caller) xB ms) hI.1 hI.2.1 count (by omega) K ?_ + rintro sT ⟨σT, hTag, hET, hST, hWT, ⟨xT, hkT, hxT⟩, hOth⟩ + refine hK sT ⟨σT, ?_, hET, hST, (hWB.trans1 hWI).trans1 hWT, ⟨xT, hkT, ?_⟩, fun p hp => ?_⟩ + · refine tcbReadRegisters_replySpecA (σBadge := σB) + (σData := mrStateNA σB (specPtr caller) xB ms) (buf := none) (len := natToLen (min 4 ms.length)) ?_ hBadge ?_ ?_ + · rw [lookupIpcBuffer_eqA hkc true, hnone] + · exact setMrs_eqNA hkB ms + · rw [hcountBv] at hTag + exact hTag + · exact hxT.trans (by rw [hxBdef]; rfl) + · rw [hOth p hp] + unfold mrStateNA + rw [Notif.updK_kheap, Function.update_of_ne hp, hOthers p hp] + · -- a tracked receive buffer + have hM := (msgLaw_buf O.to0 P hEB hSB hc hkB hbp hbp0).withW P hc (Or.inr ⟨bp, hbp, rfl⟩) + refine WP.spec_bind (tcbReadRegisters_dataA P hs hc hne hM hRegs hN) ?_ + rintro ⟨sGp, count⟩ ⟨hCount, hI, hWI⟩ + have hCount' : count.val = n.val := by simpa [hbp0] using hCount + set ms := ((frameRegisters ++ gpRegisters).take n.val).map xs.tcbArch.tcbContext.userRegs with hms + have hlen : ms.length = n.val := readWordsLengthA _ _ hN + have hcountBv : count.bv = natToLen (min ms.length msgMaxLength) := by + rw [hlen, Nat.min_eq_left (show n.val ≤ msgMaxLength by unfold msgMaxLength; omega)] + exact countBvA hCount' (by omega) + have hal : ∀ q ∈ mrPairs ms, (bufAddr bp.2 q.1).toNat % 8 = 0 ∧ + inUserFrame (bufAddr bp.2 q.1) σB := by + intro q hq + have := (List.of_mem_zip hq).1 + simp [List.mem_range'] at this + exact ⟨bufAddr_align (hEB.buf bp hbp 0#u64 (by decide)).2 _ (by omega), + hu' bp.2 hsome _ (by omega)⟩ + refine tcbReadRegisters_tagPostKA O P hc + (mrState_kheap σB (specPtr caller) xB bp.2 ms) hI.1 hI.2.1 count (by omega) K ?_ + rintro sT ⟨σT, hTag, hET, hST, hWT, ⟨xT, hkT, hxT⟩, hOth⟩ + refine hK sT ⟨σT, ?_, hET, hST, (hWB.trans1 hWI).trans1 hWT, ⟨xT, hkT, ?_⟩, fun p hp => ?_⟩ + · refine tcbReadRegisters_replySpecA (σBadge := σB) + (σData := mrState σB (specPtr caller) xB bp.2 ms) (buf := some bp.2) (len := natToLen (min ms.length msgMaxLength)) ?_ hBadge ?_ ?_ + · rw [lookupIpcBuffer_eqA hkc true, hsome] + · exact setMrs_eq hkB bp.2 ms hal + · rw [hcountBv] at hTag + exact hTag + · exact hxT.trans (by rw [hxBdef]; rfl) + · rw [hOth p hp] + show (Notif.updK σB (specPtr caller) _).kheap p = _ + rw [Notif.updK_kheap, Function.update_of_ne hp, hOthers p hp] + + +end +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/TcbInvokeReadDataA.lean b/proofs/Fl4ProofsA64/P3/TcbInvokeReadDataA.lean new file mode 100644 index 000000000..f4f1c78ea --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbInvokeReadDataA.lean @@ -0,0 +1,212 @@ +/- +P3 (AArch64): ReadRegisters writes the selected source register words +to the same inline registers and buffer slots as an ordinary kernel reply. +[J] The caller's reply buffer is covered by the message relation, and the +source TCB is distinct from the caller at the actual post-suspend state. +-/ +import Fl4ProofsA64.P3.TcbInvokeReadCallA +import Fl4ProofsA64.P3.EpFaultMoreA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4SpecA64 Endp + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {T E R : Set U64} {Bf : Set (U64 × Word)} +variable {I : S → List Word → Prop} {s : S} {caller src buf n : U64} +variable {g : Nat → Nat} {regs : Register → Word} + +private theorem readSrcWordA (P : EPlaced T E R Bf) (hs : src ∈ T) (hc : caller ∈ T) + (hOther : src ≠ caller) (M : MsgLawA st Bf I caller buf g s) + (hRegs : ∀ r, (L.mem s (Endp.regA src (regIdxA r))).bv = regs r) + {s' : S} {words : List Word} (hI : I s' words) (r : Register) : + (L.mem s' (Endp.regA src (regIdxA r))).bv = regs r := by + rw [snd_word_eqA P hs hc hOther (M.agree _ _ hI) (a_reg (P.tcb src hs) r), hRegs] + +/-- The inline loop writes the chosen prefix of frame words via the +message relation. Each source read still sees the post-suspend registers. -/ +theorem tcbReadRegisters_frameInlinePostA (P : EPlaced T E R Bf) (hs : src ∈ T) (hc : caller ∈ T) + (hOther : src ≠ caller) (M : MsgLawA st Bf I caller buf g s) + (hRegs : ∀ r, (L.mem s (Endp.regA src (regIdxA r))).bv = regs r) : + tcb.read_registers_loop0 st s src n caller 0#u64 ⦃ r => + r.2.val = min n.val 4 ∧ I r.1 ((frameRegisters.take r.2.val).map regs) ⦄ := by + refine tcbReadRegisters_frameInlineA (I := fun i q => I q ((frameRegisters.take i).map regs)) ?_ ?_ + · exact M.start + intro s' i hi hIndex hI + have hi4 : i.val < 4 := by omega + have hLen : ((frameRegisters.take i.val).map regs).length = i.val := by simp; omega + let r := frameRegisters[i.val] + let v := L.mem s' (Endp.regA src (regIdxA r)) + have hRead := readSrcWordA P hs hc hOther M hRegs hI r + rw [msg_register_regIdxA hi4, bind_ok, set_reg_eqA, WP.spec_ok] + change I (L.wr s' (Endp.regA caller (regIdxA (mrIdx i.val))) v) + ((frameRegisters.take (i.val + 1)).map regs) + rw [take_succ_mapA regs frameRegisters i.val hi] + change I _ (_ ++ [regs r]) + rw [← hRead] + exact M.set.regs hI i hLen.symm hi4 v + +private theorem readRegBuffer_offsetA {i j : U64} (hi : i.val < 120) (hj : j.val = i.val + 1) : + j = core.num.U64.wrapping_add i 1#u64 := by + apply UScalar.eq_of_val_eq + rw [hj, core.num.U64.wrapping_add_val_eq] + simp [U64.size_def, U64.numBits] + omega + +/-- The buffered frame loop continues at the first word after the inline prefix. -/ +theorem tcbReadRegisters_frameBufferPostA (P : EPlaced T E R Bf) (hs : src ∈ T) (hc : caller ∈ T) + (hOther : src ≠ caller) (M : MsgLawA st Bf I caller buf g s) + (hRegs : ∀ r, (L.mem s (Endp.regA src (regIdxA r))).bv = regs r) + {sInline : S} {start : U64} (hb : buf ≠ 0#u64) (hStart : 4 ≤ start.val) + (hStartBound : start.val ≤ min n.val frameRegisters.length) + (hI : I sInline ((frameRegisters.take start.val).map regs)) : + tcb.read_registers_loop1 st sInline src n buf start ⦃ r => + r.2.val = min n.val frameRegisters.length ∧ I r.1 ((frameRegisters.take r.2.val).map regs) ⦄ := by + refine WP.spec_mono + (tcbReadRegisters_frameBufferA (I := fun i q => 4 ≤ i ∧ I q ((frameRegisters.take i).map regs)) + ⟨hStart, hI⟩ hStartBound ?_) ?_ + swap + · rintro r ⟨hCount, _, hMessage⟩ + exact ⟨hCount, hMessage⟩ + rintro s' i j hi _ hj ⟨hi4, hMessage⟩ + have hFrame : frameRegisters.length = 17 := rfl + have hSmall : i.val < 120 := by omega + have hLen : ((frameRegisters.take i.val).map regs).length = i.val := by simp; omega + let r := frameRegisters[i.val] + let v := L.mem s' (Endp.regA src (regIdxA r)) + have hRead := readSrcWordA P hs hc hOther M hRegs hMessage r + have hAddr := readRegBuffer_offsetA hSmall hj + rw [buf_set_eq, WP.spec_ok] + refine ⟨by omega, ?_⟩ + rw [hj, take_succ_mapA regs frameRegisters i.val hi, hAddr] + change I (L.wr s' (bufW buf (core.num.U64.wrapping_add i 1#u64)) v) (_ ++ [regs r]) + rw [← hRead] + exact M.set.bufs hb hMessage i hLen.symm hi4 hSmall v + +/-- A buffered GP word extends the full frame prefix at the matching message offset. -/ +theorem tcbReadRegisters_gpBufferPostA (P : EPlaced T E R Bf) (hs : src ∈ T) (hc : caller ∈ T) + (hOther : src ≠ caller) (M : MsgLawA st Bf I caller buf g s) + (hRegs : ∀ r, (L.mem s (Endp.regA src (regIdxA r))).bv = regs r) + {sFrame : S} (hb : buf ≠ 0#u64) (hI : I sFrame (frameRegisters.map regs)) : + tcb.read_registers_loop3 st sFrame src n buf 0#u64 ⦃ r => + r.2.val = min gpRegisters.length (n.val - 17) ∧ + I r.1 (frameRegisters.map regs ++ (gpRegisters.take r.2.val).map regs) ⦄ := by + refine tcbReadRegisters_gpBufferA (I := fun k q => + I q (frameRegisters.map regs ++ (gpRegisters.take k).map regs)) ?_ ?_ + · simpa only [List.take_zero, List.map_nil, List.append_nil] using hI + intro s' k j hk _ hj hMessage + have hFrame : frameRegisters.length = 17 := rfl + have hGp : gpRegisters.length = 19 := rfl + let i := core.num.U64.wrapping_add k 17#u64 + have hi : i.val = k.val + 17 := by + simp [i, core.num.U64.wrapping_add_val_eq, U64.size_def, U64.numBits] + omega + have hi4 : 4 ≤ i.val := by omega + have hSmall : i.val < 120 := by omega + have hLen : (frameRegisters.map regs ++ (gpRegisters.take k.val).map regs).length = i.val := by + simp only [List.length_append, List.length_map, List.length_take] + omega + let r := gpRegisters[k.val] + let v := L.mem s' (Endp.regA src (regIdxA r)) + have hRead := readSrcWordA P hs hc hOther M hRegs hMessage r + have hAddr := readRegBuffer_offsetA (i := i) (j := j) hSmall (by omega) + rw [buf_set_eq, WP.spec_ok, take_succ_mapA regs gpRegisters k.val hk, ← List.append_assoc, hAddr] + change I (L.wr s' (bufW buf (core.num.U64.wrapping_add i 1#u64)) v) (_ ++ [regs r]) + rw [← hRead] + exact M.set.bufs hb hMessage i hLen.symm hi4 hSmall v + +private theorem readRegisterWords_appendA (regs : Register → Word) (n : Nat) : + ((frameRegisters ++ gpRegisters).take n).map regs = + (frameRegisters.take n).map regs ++ (gpRegisters.take (n - 17)).map regs := by + simp only [List.take_append, List.map_append, show frameRegisters.length = 17 from rfl] + +/-- The selected payload loops produce the same message as the abstract +register list. Without a buffer the invariant extends over the omitted +suffix and the count records only the words actually delivered. -/ +theorem tcbReadRegisters_dataA (P : EPlaced T E R Bf) (hs : src ∈ T) (hc : caller ∈ T) + (hOther : src ≠ caller) (M : MsgLawA st Bf I caller buf g s) + (hRegs : ∀ r, (L.mem s (Endp.regA src (regIdxA r))).bv = regs r) (hN : n.val ≤ 36) : + tcbReadRegisters_dataProgramA (st := st) s src caller n buf ⦃ r => + r.2.val = (if buf = 0#u64 then min n.val 4 else n.val) ∧ + I r.1 (((frameRegisters ++ gpRegisters).take n.val).map regs) ⦄ := by + have hFrame : frameRegisters.length = 17 := rfl + have hNF : tcb.N_FRAME.val = 17 := by rw [tcb.N_FRAME]; rfl + have hGp : gpRegisters.length = 19 := rfl + unfold tcbReadRegisters_dataProgramA + refine WP.spec_bind (tcbReadRegisters_frameInlinePostA P hs hc hOther M hRegs) ?_ + rintro ⟨sInline, i⟩ ⟨hCount, hI⟩ + change i.val = min n.val 4 at hCount + change I sInline ((frameRegisters.take i.val).map regs) at hI + by_cases hb : buf = 0#u64 + · subst buf + simp only [bne_self_eq_false, Bool.false_eq_true, ite_false, ite_true, bind_ok, + uncurry_apply_pair, WP.spec_ok] + refine ⟨?_, ?_⟩ + · exact tcbReadRegisters_replyCountA hN false hCount rfl + · let words := (frameRegisters ++ gpRegisters).take n.val + have hHead : (words.take i.val).map regs = (frameRegisters.take i.val).map regs := by + have hMin : min i.val n.val = i.val := by omega + have hSub : i.val - frameRegisters.length = 0 := by omega + rw [show words = (frameRegisters ++ gpRegisters).take n.val from rfl, + List.take_take, hMin, List.take_append, hSub, List.take_zero, List.append_nil] + have hMsg : I sInline ((words.take i.val).map regs) := hHead.symm ▸ hI + by_cases hSmall : n.val ≤ 4 + · rw [show i.val = n.val by omega, List.take_take, min_self] at hMsg + exact hMsg + · have hLen : 4 ≤ ((words.take i.val).map regs).length := by + simp only [words, List.length_map, List.length_take, List.length_append] + omega + have hFull := M.ext rfl sInline _ ((words.drop i.val).map regs) hMsg hLen + rw [← List.map_append, List.take_append_drop] at hFull + exact hFull + · have hb' : (buf != 0#u64) = true := bne_iff_ne.mpr hb + simp only [hb', ite_true, ite_eq_right hb] + have hFrameRun : (if i < n ∧ i < tcb.N_FRAME then + tcb.read_registers_loop1 st sInline src n buf i else ok (sInline, i)) ⦃ r => + r.2.val = min n.val 17 ∧ I r.1 ((frameRegisters.take r.2.val).map regs) ⦄ := by + by_cases hGo : i < n ∧ i < tcb.N_FRAME + · rw [ite_eq_left hGo] + have hi : i.val < n.val := hGo.1 + have hStart : 4 ≤ i.val := by omega + have hLimit : i.val ≤ min n.val frameRegisters.length := by omega + simpa only [hFrame] using tcbReadRegisters_frameBufferPostA P hs hc hOther M hRegs hb hStart hLimit hI + · rw [ite_eq_right hGo, WP.spec_ok] + have hStop : ¬ (i.val < n.val ∧ i.val < tcb.N_FRAME.val) := hGo + have hIcount : i.val = min n.val 17 := by omega + exact ⟨hIcount, hI⟩ + refine WP.spec_bind hFrameRun ?_ + rintro ⟨sFrame, frameCount⟩ ⟨hFcount, hFrameMsg⟩ + simp only [uncurry_apply_pair] + change frameCount.val = min n.val 17 at hFcount + change I sFrame ((frameRegisters.take frameCount.val).map regs) at hFrameMsg + by_cases hMore : 17#u64 < n + · rw [ite_eq_left hMore] + have hn : 17 < n.val := hMore + have hFullFrame : frameRegisters.take frameCount.val = frameRegisters := by + apply List.take_of_length_le + omega + rw [hFullFrame] at hFrameMsg + refine WP.spec_bind (tcbReadRegisters_gpBufferPostA P hs hc hOther M hRegs hb hFrameMsg) ?_ + rintro ⟨sGp, gpCount⟩ ⟨hGcount, hGpMsg⟩ + change gpCount.val = min gpRegisters.length (n.val - 17) at hGcount + change I sGp (frameRegisters.map regs ++ (gpRegisters.take gpCount.val).map regs) at hGpMsg + have hGexact : gpCount.val = n.val - 17 := by omega + have hFrameTake : frameRegisters.take n.val = frameRegisters := List.take_of_length_le (by omega) + refine (WP.spec_ok _).2 ⟨tcbReadRegisters_replyCountA hN true hFcount (hGp ▸ hGcount), ?_⟩ + rw [readRegisterWords_appendA, hFrameTake, ← hGexact] + exact hGpMsg + · rw [ite_eq_right hMore, bind_ok, uncurry_apply_pair, WP.spec_ok] + have hn : ¬17 < n.val := hMore + have hCountFrame : frameCount.val = n.val := by omega + have hNoGp : (0#u64 : U64).val = (if true then min 19 (n.val - 17) else 0) := by + change 0 = min 19 (n.val - 17) + omega + refine ⟨tcbReadRegisters_replyCountA hN true hFcount hNoGp, ?_⟩ + rw [readRegisterWords_appendA, show n.val - 17 = 0 by omega, + List.take_zero, List.map_nil, List.append_nil, ← hCountFrame] + exact hFrameMsg + +end +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/TcbInvokeReadLoopsA.lean b/proofs/Fl4ProofsA64/P3/TcbInvokeReadLoopsA.lean new file mode 100644 index 000000000..f6b9febf4 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbInvokeReadLoopsA.lean @@ -0,0 +1,210 @@ +/- +P3 (AArch64): the inline part of a ReadRegisters Call reply. +[J] Read each source register from the state at that iteration and apply +the existing message-register relation to each destination write. +-/ +import Fl4ProofsA64.P3.TcbInvokeWriteLoopsA +import Fl4ProofsA64.P3.EpMrsA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4SpecA64 + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- Only the beginning of the frame list can fit into the inline reply. -/ +theorem tcbReadRegisters_frameInlineA {I : Nat → S → Prop} {s : S} {src caller n : U64} + (hStart : I 0 s) + (hWrite : ∀ (s' : S) (i : U64) (hi : i.val < frameRegisters.length), + i.val < min n.val 4 → I i.val s' → + (do + let r ← msg.msg_register i + obj.Tcb.set_reg st caller s' r + (L.mem s' (Endp.regA src (Endp.regIdxA frameRegisters[i.val])))) + ⦃ s'' => I (i.val + 1) s'' ⦄) : + tcb.read_registers_loop0 st s src n caller 0#u64 ⦃ r => + r.2.val = min n.val 4 ∧ I r.2.val r.1 ⦄ := by + have hFrame : frameRegisters.length = 17 := rfl + have hNF : tcb.N_FRAME.val = 17 := by rw [tcb.N_FRAME]; rfl + have hNM : msg.N_MSG_REGISTERS.val = 4 := by rw [msg.N_MSG_REGISTERS]; rfl + unfold tcb.read_registers_loop0 + apply loop.spec_decr_nat (measure := fun r => min n.val 4 - r.2.val) + (inv := fun r => I r.2.val r.1 ∧ r.2.val ≤ min n.val 4) + · rintro ⟨s', i⟩ ⟨hI, hLimit⟩ + change i.val ≤ min n.val 4 at hLimit + change I i.val s' at hI + unfold tcb.read_registers_loop0.body + dsimp only + by_cases hi : i.val < min n.val 4 + · have hn : i < n := show i.val < n.val from by omega + have hf : i < tcb.N_FRAME := show i.val < tcb.N_FRAME.val from by omega + have hm : i < msg.N_MSG_REGISTERS := show i.val < msg.N_MSG_REGISTERS.val from by omega + have hr : i.val < frameRegisters.length := by omega + have hSmall : i.val < 2 ^ 32 := by omega + rw [ite_eq_left hn, ite_eq_left hf, ite_eq_left hm] + simp only [lift, bind_ok, tcbRegArray_indexA _ _ tcbFrameRegisters_eqA _ hr hSmall, + tcb_reg_eqA, show regWA src (Endp.regIdxA frameRegisters[i.val]) = + Endp.regA src (Endp.regIdxA frameRegisters[i.val]) from rfl] + rw [← Std.bind_assoc] + refine WP.spec_bind (hWrite s' i hr hi hI) ?_ + intro s'' hJ + step as ⟨j, hj⟩ + refine ⟨?_, ?_, ?_⟩ + · change I j.val s'' + rw [show j.val = i.val + 1 by omega] + exact hJ + · omega + · omega + · have hEq : i.val = min n.val 4 := by omega + have hDone : i.val = min n.val 4 ∧ I i.val s' := ⟨hEq, hI⟩ + by_cases hn : i < n + · have hN : i.val < n.val := hn + have hf : i < tcb.N_FRAME := show i.val < tcb.N_FRAME.val from by omega + have hm : ¬i < msg.N_MSG_REGISTERS := show ¬i.val < msg.N_MSG_REGISTERS.val from by omega + rw [ite_eq_left hn, ite_eq_left hf, ite_eq_right hm] + exact (WP.spec_ok _).2 hDone + · rw [ite_eq_right hn] + exact (WP.spec_ok _).2 hDone + · exact ⟨hStart, Nat.zero_le _⟩ + +set_option maxHeartbeats 800000 in +/-- Continue the frame prefix in the receive buffer, whose first word is +reserved for the tag. The input index is the number of inline words. -/ +theorem tcbReadRegisters_frameBufferA {I : Nat → S → Prop} {s : S} {src n buf start : U64} + (hStart : I start.val s) (hStartBound : start.val ≤ min n.val frameRegisters.length) + (hWrite : ∀ (s' : S) (i j : U64) (hi : i.val < frameRegisters.length), + i.val < n.val → j.val = i.val + 1 → I i.val s' → + msg.buf_set st s' buf j + (L.mem s' (Endp.regA src (Endp.regIdxA frameRegisters[i.val]))) + ⦃ s'' => I j.val s'' ⦄) : + tcb.read_registers_loop1 st s src n buf start ⦃ r => + r.2.val = min n.val frameRegisters.length ∧ I r.2.val r.1 ⦄ := by + have hFrame : frameRegisters.length = 17 := rfl + have hNF : tcb.N_FRAME.val = 17 := by rw [tcb.N_FRAME]; rfl + unfold tcb.read_registers_loop1 + apply loop.spec_decr_nat (measure := fun r => min n.val frameRegisters.length - r.2.val) + (inv := fun r => I r.2.val r.1 ∧ r.2.val ≤ min n.val frameRegisters.length) + · rintro ⟨s', i⟩ ⟨hI, hLimit⟩ + change i.val ≤ min n.val frameRegisters.length at hLimit + change I i.val s' at hI + unfold tcb.read_registers_loop1.body + dsimp only + by_cases hn : i < n + · rw [ite_eq_left hn] + have hn' : i.val < n.val := hn + by_cases hf : i < tcb.N_FRAME + · rw [ite_eq_left hf] + have hf' : i.val < tcb.N_FRAME.val := hf + have hr : i.val < frameRegisters.length := by omega + have hSmall : i.val < 2 ^ 32 := by omega + simp only [lift, bind_ok, tcbRegArray_indexA _ _ tcbFrameRegisters_eqA _ hr hSmall, + tcb_reg_eqA, show regWA src (Endp.regIdxA frameRegisters[i.val]) = + Endp.regA src (Endp.regIdxA frameRegisters[i.val]) from rfl] + step as ⟨j, hj⟩ + have hNext : j.val = i.val + 1 := by omega + refine WP.spec_bind (hWrite s' i j hr hn' hNext hI) ?_ + intro s'' hJ + have hBound : j.val ≤ min n.val frameRegisters.length := by omega + have hDecrease : min n.val frameRegisters.length - j.val < + min n.val frameRegisters.length - i.val := by omega + exact (WP.spec_ok _).2 ⟨hJ, hBound, hDecrease⟩ + · rw [ite_eq_right hf] + have hf' : ¬i.val < tcb.N_FRAME.val := hf + have he : i.val = min n.val frameRegisters.length := by omega + exact (WP.spec_ok _).2 ⟨he, hI⟩ + · rw [ite_eq_right hn] + have hn' : ¬i.val < n.val := hn + have he : i.val = min n.val frameRegisters.length := by omega + exact (WP.spec_ok _).2 ⟨he, hI⟩ + · exact ⟨hStart, hStartBound⟩ + +set_option maxHeartbeats 800000 in +/-- GP words follow the seventeen frame words in the receive buffer. The +returned index counts GP words only, and each write skips the tag word. -/ +theorem tcbReadRegisters_gpBufferA {I : Nat → S → Prop} {s : S} {src n buf : U64} + (hStart : I 0 s) + (hWrite : ∀ (s' : S) (k j : U64) (hk : k.val < gpRegisters.length), + k.val + 17 < n.val → j.val = k.val + 18 → I k.val s' → + msg.buf_set st s' buf j + (L.mem s' (Endp.regA src (Endp.regIdxA gpRegisters[k.val]))) + ⦃ s'' => I (k.val + 1) s'' ⦄) : + tcb.read_registers_loop3 st s src n buf 0#u64 ⦃ r => + r.2.val = min gpRegisters.length (n.val - 17) ∧ I r.2.val r.1 ⦄ := by + have hGp : gpRegisters.length = 19 := rfl + have hNG : tcb.N_GP.val = 19 := by rw [tcb.N_GP]; rfl + have hNF : tcb.N_FRAME.val = 17 := by rw [tcb.N_FRAME]; rfl + unfold tcb.read_registers_loop3 + apply loop.spec_decr_nat (measure := fun r => min gpRegisters.length (n.val - 17) - r.2.val) + (inv := fun r => I r.2.val r.1 ∧ r.2.val ≤ min gpRegisters.length (n.val - 17)) + · rintro ⟨s', k⟩ ⟨hI, hLimit⟩ + change k.val ≤ min gpRegisters.length (n.val - 17) at hLimit + change I k.val s' at hI + unfold tcb.read_registers_loop3.body + dsimp only + by_cases hg : k < tcb.N_GP + · rw [ite_eq_left hg] + have hg' : k.val < tcb.N_GP.val := hg + have hk : k.val < gpRegisters.length := by omega + step as ⟨i, hi⟩ + by_cases hn : i < n + · rw [ite_eq_left hn] + have hn' : i.val < n.val := hn + have hSmall : k.val < 2 ^ 32 := by omega + simp only [lift, bind_ok, tcbRegArray_indexA _ _ tcbGpRegisters_eqA _ hk hSmall, + tcb_reg_eqA, show regWA src (Endp.regIdxA gpRegisters[k.val]) = + Endp.regA src (Endp.regIdxA gpRegisters[k.val]) from rfl] + step as ⟨j, hj⟩ + have hIndex : j.val = k.val + 18 := by omega + refine WP.spec_bind (hWrite s' k j hk (by omega) hIndex hI) ?_ + intro s'' hJ + step as ⟨k', hk'⟩ + refine ⟨?_, ?_, ?_⟩ + · change I k'.val s'' + rw [show k'.val = k.val + 1 by omega] + exact hJ + · omega + · omega + · rw [ite_eq_right hn] + have hn' : ¬i.val < n.val := hn + have he : k.val = min gpRegisters.length (n.val - 17) := by omega + exact (WP.spec_ok _).2 ⟨he, hI⟩ + · rw [ite_eq_right hg] + have hg' : ¬k.val < tcb.N_GP.val := hg + have he : k.val = min gpRegisters.length (n.val - 17) := by omega + exact (WP.spec_ok _).2 ⟨he, hI⟩ + · exact ⟨hStart, Nat.zero_le _⟩ + +/-- The extracted GP inline variants all start after the four inline words. -/ +theorem tcbReadRegisters_gpInlineEqA (s : S) (src n caller i : U64) : + tcb.read_registers_loop4 st s src n caller i = tcb.read_registers_loop2 st s src n caller i ∧ + tcb.read_registers_loop6 st s src n caller i = tcb.read_registers_loop2 st s src n caller i ∧ + tcb.read_registers_loop8 st s src n caller i = tcb.read_registers_loop2 st s src n caller i := + ⟨rfl, rfl, rfl⟩ + +/-- All three alternatives for buffered GP reply use the same loop. -/ +theorem tcbReadRegisters_gpBufferEqA (s : S) (src n buf i : U64) : + tcb.read_registers_loop5 st s src n buf i = tcb.read_registers_loop3 st s src n buf i ∧ + tcb.read_registers_loop7 st s src n buf i = tcb.read_registers_loop3 st s src n buf i ∧ + tcb.read_registers_loop9 st s src n buf i = tcb.read_registers_loop3 st s src n buf i := + ⟨rfl, rfl, rfl⟩ + +/-- On A64 no GP source word can occupy an inline message register. -/ +theorem tcbReadRegisters_gpInlineEmptyA (s : S) (src n caller : U64) : + tcb.read_registers_loop2 st s src n caller 0#u64 = ok (s, 0#u64) := by + have hZero : 0#u64 < tcb.N_GP := by rw [tcb.N_GP]; decide + have hAdd : 0#u64 + tcb.N_FRAME = ok 17#u64 := by rw [tcb.N_FRAME]; rfl + have hMR : ¬17#u64 < msg.N_MSG_REGISTERS := by rw [msg.N_MSG_REGISTERS]; decide + have hLoop : tcb.read_registers_loop2 st s src n caller 0#u64 ⦃ r => r = (s, 0#u64) ⦄ := by + unfold tcb.read_registers_loop2 + apply loop.spec_decr_nat (measure := fun _ => 0) (inv := fun r => r = (s, 0#u64)) + · rintro r rfl + unfold tcb.read_registers_loop2.body + simp only [ite_eq_left hZero, hAdd, bind_ok, ite_eq_right hMR, ite_self] + exact (WP.spec_ok _).2 rfl + · rfl + exact eq_of_spec_fd hLoop + +end +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/TcbInvokeReadPostA.lean b/proofs/Fl4ProofsA64/P3/TcbInvokeReadPostA.lean new file mode 100644 index 000000000..191b1d77f --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbInvokeReadPostA.lean @@ -0,0 +1,160 @@ +/- +P3 (AArch64): register readback and the local Send completion of +ReadRegisters. [J] The scheduler relation and the typed source TCB +are available after caller Restart. [S] The target stall has no effects, +and this arm does not request source suspension. +-/ +import Fl4ProofsA64.P3.TcbInvokeRegisterSelectA +import Fl4ProofsA64.P3.ScSetStateA +import Fl4ProofsA64.P3.DelSpecA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4SpecA64 Sched + +noncomputable section + +private theorem mapM_registerReadA (uc : UserContext) : + ∀ rs : List Register, Fl4Spec.NonDet.mapM getRegister rs uc = ({(rs.map uc.userRegs, uc)}, False) + | [] => rfl + | r :: rs => by + change ((getRegister r >>= fun b => Fl4Spec.NonDet.mapM getRegister rs >>= fun bs => + Fl4Spec.NonDet.pure (b :: bs)) uc) = _ + rw [Notif.nd_bind_one (show getRegister r uc = ({(uc.userRegs r, uc)}, False) from rfl), + Notif.nd_bind_one (mapM_registerReadA uc rs)] + rfl + +private theorem selectRegisterReadA {α StateType : Type} (r : α) (s : StateType) : + Fl4Spec.NonDet.selectF ({r}, False) s = ({(r, s)}, False) := by + apply Prod.ext + · ext ⟨v, t⟩ + simp [Fl4Spec.NonDet.selectF] + · rfl + +/-- Reading user registers returns the chosen register values and leaves the +same spec state. -/ +theorem tcbReadUserRegisters_specA {σ : State Unit} {p : ObjRef} {x : Tcb} + (hk : σ.kheap p = some (.tcb x)) (rs : List Register) : + asUser (Z := Unit) p (Fl4Spec.NonDet.mapM getRegister rs) σ = + ({(rs.map x.tcbArch.tcbContext.userRegs, σ)}, False) := by + unfold asUser + rw [nd_bind_oneSc (Notif.getTcb_eq hk)] + rw [Notif.nd_pure_bind'] + rw [mapM_registerReadA, nd_bind_oneSc (selectRegisterReadA _ _)] + dsimp only + have he : ({ x with tcbArch := archTcbContextSet (archTcbContextGet x.tcbArch) x.tcbArch } : Tcb) = x := by + cases x + rfl + rw [Notif.nd_pure_bind'] + rw [he, nd_bind_oneSc (setObject_eqSc hk rfl)] + change ({(_, updKSc σ p (.tcb x))}, False) = _ + rw [show updKSc σ p (.tcb x) = σ from Endp.updK_selfA hk] <;> rfl + +private theorem unlessE_posA {σ ε : Type} {P : Prop} [Decidable P] (h : P) + (m : Fl4Spec.NonDetE σ ε Unit) : Fl4Spec.NonDetE.unlessE P m = pure () := if_pos h + +private theorem unlessE_negA {σ ε : Type} {P : Prop} [Decidable P] (h : ¬ P) + (m : Fl4Spec.NonDetE σ ε Unit) : Fl4Spec.NonDetE.unlessE P m = m := if_neg h + +private theorem whenE_negA {σ ε : Type} {P : Prop} [Decidable P] (h : ¬ P) + (m : Fl4Spec.NonDetE σ ε Unit) : Fl4Spec.NonDetE.whenE P m = pure () := if_neg h + +private theorem readRegisterRangeA : + BitVec.ofNat 64 (frameRegisters.length + gpRegisters.length) = (36 : Word) := rfl + +/-- ReadRegisters decodes the checked count and suspend bit. The abstract +range check is evaluated before the current-thread rejection. -/ +theorem tcbReadRegisters_selectedSpecA {σ : State Unit} {t : ObjRef} {flags n : Word} + {args : List Word} (hRange : n ≥ 1 ∧ n ≤ 36) (hOther : t ≠ σ.curThread) : + (decodeReadRegisters (Z := Unit) (flags :: n :: args) (.threadCap t)).run σ = + ({(.ok (.readRegisters t (testBit flags 0) n ArchDefaultExtraRegisters), σ)}, False) := by + simp only [decodeReadRegisters, rangeCheck] + rw [unlessE_posA (P := n ≥ 1 ∧ n ≤ BitVec.ofNat 64 (frameRegisters.length + gpRegisters.length)) hRange, ndE_pure_bindD] + rw [run_bind_okD (run_returnOkD t σ)] + rw [run_bind_okD (run_liftE_stD (show (Fl4Spec.NonDet.gets fun s : State Unit => s.curThread) σ = + ({(σ.curThread, σ)}, False) from rfl))] + rw [whenE_negA hOther, ndE_pure_bindD] + rfl + +/-- An out-of-range count rejects even when the target is current. -/ +theorem tcbReadRegisters_rangeSpecA (σ : State Unit) (t : ObjRef) (flags n : Word) + (args : List Word) (hRange : ¬ (n ≥ 1 ∧ n ≤ 36)) : + (decodeReadRegisters (Z := Unit) (flags :: n :: args) (.threadCap t)).run σ = + ({(.error (.rangeError 1 36), σ)}, False) := by + simp only [decodeReadRegisters, rangeCheck] + rw [unlessE_negA (P := n ≥ 1 ∧ n ≤ BitVec.ofNat 64 (frameRegisters.length + gpRegisters.length)) hRange] + exact run_bind_errorD (run_throwErrorD (SyscallError.rangeError 1 36) σ) _ + +/-- The selected cancel-run and suspension phases precede the read of the +actual source TCB's register list in their common successor. -/ +theorem tcbReadRegisters_specA {σ σCancel σSuspend : State Unit} {p : ObjRef} {x : Tcb} + (suspendSource : Bool) (count : Word) + (hCancel : Fl4Spec.NonDet.when suspendSource (fl4CancelRun p) σ = ({((), σCancel)}, False)) + (hSuspend : Fl4Spec.NonDet.when suspendSource (suspend p) σCancel = ({((), σSuspend)}, False)) + (hk : σSuspend.kheap p = some (.tcb x)) : + (fl4InvokeTcb (.readRegisters p suspendSource count ArchDefaultExtraRegisters)).run σ = + ({(.ok (((frame_registers ++ gp_registers).take count.toNat).map x.tcbArch.tcbContext.userRegs), + σSuspend)}, False) := by + simp only [fl4InvokeTcb, invokeTcb] + rw [run_bind_okD (run_liftE_stD hCancel)] + apply run_liftE_stD + rw [nd_bind_oneSc hSuspend] + rw [nd_bind_oneSc (show (Fl4Spec.NonDet.gets fun s : State Unit => s.curThread) σSuspend = + ({(σSuspend.curThread, σSuspend)}, False) from rfl)] + rw [Notif.nd_pure_bind'] + exact tcbReadUserRegisters_specA hk _ + +theorem readRegsSpec_noSuspendA {σ : State Unit} {p : ObjRef} {x : Tcb} + (hk : σ.kheap p = some (.tcb x)) (count : Word) : + (fl4InvokeTcb (.readRegisters p false count ArchDefaultExtraRegisters)).run σ = + ({(.ok (((frame_registers ++ gp_registers).take count.toNat).map x.tcbArch.tcbContext.userRegs), σ)}, False) := by + exact tcbReadRegisters_specA false count rfl rfl hk + +private theorem finishTcbReply_send_specA {σ σ' : State Unit} {words : List Data} + (h : setThreadState (Z := Unit) σ.curThread .running σ = ({((), σ')}, False)) : + fl4FinishTcbReply false words σ = ({((), σ')}, False) := by + unfold fl4FinishTcbReply + rw [nd_bind_oneSc (show (Fl4Spec.NonDet.gets fun s : State Unit => s.curThread) σ = ({(σ.curThread, σ)}, False) + from rfl)] + rw [nd_bind_oneSc (show (Fl4Spec.NonDet.when false (replyFromKernel (Z := Unit) σ.curThread (0, words))) σ = + ({((), σ)}, False) from rfl)] + exact h + +/-- A Send with no source suspension reads the source without a Store +update at the abstract level, then completes the caller's Running transition. -/ +theorem tcbReadRegisters_sendPerformA {S A : Type} {st : machine.Store S} [L : LawfulStore st] + {ar : machine.Arch A} {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s sRestart : S} {a : A} {σRestart : State Unit} + {t flags count : U64} {ctx : tcb.Ctx} {src caller : Tcb} {block donate : Bool} + (hst : smp.remote_tcb_stall st ar s a t = ok (s, a)) + (hlen : ¬ctx.length < 2#u64) + (hf : tcb.arg st s ctx 0#u64 = ok flags) (hn : tcb.arg st s ctx 1#u64 = ok count) + (hlo : ¬count < 1#u64) (hhi : ¬36#u64 < count) (hself : t ≠ L.curW s) + (hr : tcb.restart_caller st s = ok sRestart) + (hcall : ctx.call = false) (hsuspend : (flags &&& 1#u64) = 0#u64) + (H : SchedSRA st Z m κ sRestart σRestart) (hstate : StEqA Z.T (L.mem sRestart) σRestart) + (hsrc : σRestart.kheap (specPtr t) = some (.tcb src)) + (hcaller : σRestart.kheap (specPtr (L.curW sRestart)) = some (.tcb caller)) + (hrun : runnable caller.tcbState = true) : + tcb.decode st ar s a fl4_abi.label.TCBReadRegisters t ctx ⦃ r => + r.1 = .Ok () ∧ r.2.2 = a ∧ ∃ σFinal, + (.ok [], σFinal) ∈ ((fl4PerformInvocation block false donate + (.invokeTCB (.readRegisters (specPtr t) false count.bv ArchDefaultExtraRegisters))).run σRestart).1 ∧ + SchedSRA st Z m κ r.2.1 σFinal ∧ StEqA Z.T (L.mem r.2.1) σFinal ∧ + WritesIn st sRestart r.2.1 (fun b => SWrA (st := st) Z sRestart b ∨ b = stA (L.curW sRestart)) ⦄ := by + rw [tcbReadRegisters_acceptedA hst hlen hf hn hlo hhi hself hr, hcall, hsuspend] + simp only [bne_self_eq_false, tcbReadRegisters_sendA, Bool.false_eq_true, ite_false, bind_ok, + uncurry_apply_pair, Std.bind_assoc] + have hv : sched.TS_RUNNING.val = tsCodeA .running := by rw [ts_vals.1]; rfl + refine WP.spec_bind (sc_set_runnable_stateA H hstate H.curT hcaller hrun (ts := .running) rfl hv) ?_ + rintro sFinal ⟨σFinal, hrunSpec, HFinal, hStateFinal, _, hFrame⟩ + refine (WP.spec_ok _).2 ⟨rfl, rfl, σFinal, ?_, HFinal, hStateFinal, hFrame⟩ + have hFinish := finishTcbReply_send_specA (words := ((frame_registers ++ gp_registers).take count.bv.toNat).map + src.tcbArch.tcbContext.userRegs) (H.sch.curThread.symm ▸ hrunSpec) + unfold fl4PerformInvocation + rw [run_bind_okD (readRegsSpec_noSuspendA hsrc count.bv), run_bind_okD (run_liftE_stD hFinish)] + rfl + +end +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/TcbInvokeReadReplyA.lean b/proofs/Fl4ProofsA64/P3/TcbInvokeReadReplyA.lean new file mode 100644 index 000000000..c63090ecd --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbInvokeReadReplyA.lean @@ -0,0 +1,105 @@ +/- +P3 (AArch64): ReadRegisters Call badge and tag, and the matching +replyFromKernel steps. [J] The caller is a live, related TCB. +-/ +import Fl4ProofsA64.P3.EpXferA +import Fl4ProofsA64.P3.TcbInvokeReadDataA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4SpecA64 Endp + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {T E R : Set U64} {Bf : Set (U64 × Word)} {SR : S → State Unit → Prop} +variable {s : S} {σ : State Unit} {caller : U64} {x : Tcb} + +theorem tcbReadRegisters_badgePostA (O : SchedOpsEA st T E R Bf SR) (P : EPlaced T E R Bf) + (hc : caller ∈ T) (hk : σ.kheap (specPtr caller) = some (.tcb x)) + (hE : EpStateRel T E R Bf (L.mem s) σ) (hS : SR s σ) : + obj.Tcb.set_reg st caller s layout.reg.BADGE 0#u64 ⦃ s' => + let x' := Notif.setReg x badge_register 0 + let σ' := Notif.updK σ (specPtr caller) (.tcb x') + s' = L.wr s (Endp.regA caller layout.reg.BADGE) 0#u64 ∧ + asUser (specPtr caller) (setRegister badge_register 0) σ = ({((), σ')}, False) ∧ + σ'.kheap (specPtr caller) = some (.tcb x') ∧ + EpStateRel T E R Bf (L.mem s') σ' ∧ SR s' σ' ∧ WritesIn st s s' (EWrite T E R Bf) ⦄ := by + obtain ⟨hRel, hWrite, hStep⟩ := Endp.write_reg_rel P s σ hE hc hk badge_register 0#u64 + have hSR := O.stable _ _ _ _ hS hWrite hStep (NoBlockE.tcbK hk _ rfl rfl) + rw [regIdxA_badge] at hRel hSR hWrite + rw [Endp.set_reg_eqA, WP.spec_ok] + exact ⟨rfl, Notif.asUser_setRegister_eq badge_register 0 hk, by simp [Notif.updK], hRel, hSR, hWrite⟩ + +theorem tcbReadRegisters_tagPostA (O : SchedOpsEA st T E R Bf SR) (P : EPlaced T E R Bf) + (hc : caller ∈ T) (hk : σ.kheap (specPtr caller) = some (.tcb x)) + (hE : EpStateRel T E R Bf (L.mem s) σ) (hS : SR s σ) (count : U64) (hn : count.val < 128) : + (do + let tag ← msginfo.MessageInfo.new 0#u64 0#u64 0#u64 count + msg.set_msg_info st s caller tag) ⦃ s' => ∃ σ', + setMessageInfo (specPtr caller) ⟨count.bv, 0, 0, 0⟩ σ = ({((), σ')}, False) ∧ + EpStateRel T E R Bf (L.mem s') σ' ∧ SR s' σ' ∧ WritesIn st s s' (EWrite T E R Bf) ∧ + (∃ x', σ'.kheap (specPtr caller) = some (.tcb x') ∧ x' = { x with tcbArch := x'.tcbArch }) ∧ + (∀ p, p ≠ specPtr caller → σ'.kheap p = σ.kheap p) ⦄ := by + simp only [msginfo.MessageInfo.new, bind_ok] + refine WP.spec_mono (set_msg_info_specA s caller 0#u64 count hn) ?_ + rintro s' ⟨v, rfl, hv⟩ + have hZero : (0#u64 : U64).bv &&& mask msgLabelBits = (0 : Word) := by rfl + have hWord := hv.trans (congrArg (fun label : Word => + messageInfoToData ⟨count.bv, 0, 0, label⟩) hZero) + obtain ⟨hRel, hWrite, hStep⟩ := Endp.write_reg_rel P s σ hE hc hk msg_info_register v + have hSR := O.stable _ _ _ _ hS hWrite hStep (NoBlockE.tcbK hk _ rfl rfl) + rw [regIdxA_msgInfo] at hRel hSR hWrite + refine ⟨_, ?_, hRel, hSR, hWrite, ⟨Notif.setReg x msg_info_register v.bv, ?_, rfl⟩, fun p hp => ?_⟩ + · unfold setMessageInfo + rw [← hWord] + exact Notif.asUser_setRegister_eq msg_info_register v.bv hk + · rw [Notif.updK_kheap, Function.update_self] + · rw [Notif.updK_kheap, Function.update_of_ne hp] + +theorem tcbReadRegisters_tagPostKA (O : SchedOpsEA st T E R Bf SR) (P : EPlaced T E R Bf) + (hc : caller ∈ T) (hk : σ.kheap (specPtr caller) = some (.tcb x)) + (hE : EpStateRel T E R Bf (L.mem s) σ) (hS : SR s σ) (count : U64) (hn : count.val < 128) + {X : Type} {Q : X → Prop} (K : S → Result X) + (hK : ∀ s', (∃ σ', + setMessageInfo (specPtr caller) ⟨count.bv, 0, 0, 0⟩ σ = ({((), σ')}, False) ∧ + EpStateRel T E R Bf (L.mem s') σ' ∧ SR s' σ' ∧ WritesIn st s s' (EWrite T E R Bf) ∧ + (∃ x', σ'.kheap (specPtr caller) = some (.tcb x') ∧ x' = { x with tcbArch := x'.tcbArch }) ∧ + (∀ p, p ≠ specPtr caller → σ'.kheap p = σ.kheap p)) → K s' ⦃ Q ⦄) : + (do + let tag ← msginfo.MessageInfo.new 0#u64 0#u64 0#u64 count + let sTag ← msg.set_msg_info st s caller tag + K sTag) ⦃ Q ⦄ := by + simp only [msginfo.MessageInfo.new, bind_ok] + refine WP.spec_bind (set_msg_info_specA s caller 0#u64 count hn) ?_ + rintro s' ⟨v, rfl, hv⟩ + have hZero : (0#u64 : U64).bv &&& mask msgLabelBits = (0 : Word) := by rfl + have hWord := hv.trans (congrArg (fun label : Word => + messageInfoToData ⟨count.bv, 0, 0, label⟩) hZero) + obtain ⟨hRel, hWrite, hStep⟩ := Endp.write_reg_rel P s σ hE hc hk msg_info_register v + have hSR := O.stable _ _ _ _ hS hWrite hStep (NoBlockE.tcbK hk _ rfl rfl) + rw [regIdxA_msgInfo] at hRel hSR hWrite + refine hK _ ⟨_, ?_, hRel, hSR, hWrite, ⟨Notif.setReg x msg_info_register v.bv, ?_, rfl⟩, fun p hp => ?_⟩ + · unfold setMessageInfo + rw [← hWord] + exact Notif.asUser_setRegister_eq msg_info_register v.bv hk + · rw [Notif.updK_kheap, Function.update_self] + · rw [Notif.updK_kheap, Function.update_of_ne hp] + +end + +/-- Compose the abstract kernel reply over the same badge and payload +successors returned by the concrete ReadRegisters reply. -/ +theorem tcbReadRegisters_replySpecA {σ σBadge σData σTag : State Unit} + {caller : ObjRef} {buf : Option ObjRef} {words : List Word} {len : LengthType} + (hLookup : lookupIpcBuffer true caller σ = ({(buf, σ)}, False)) + (hBadge : asUser caller (setRegister badge_register 0) σ = ({((), σBadge)}, False)) + (hData : setMrs caller buf words σBadge = ({(len, σData)}, False)) + (hTag : setMessageInfo caller ⟨len, 0, 0, 0⟩ σData = ({((), σTag)}, False)) : + replyFromKernel caller (0, words) σ = ({((), σTag)}, False) := by + unfold replyFromKernel + rw [Notif.nd_pure_bind'] + dsimp only + rw [Notif.nd_bind_one hLookup, Notif.nd_bind_one hBadge, Notif.nd_bind_one hData, hTag] + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/TcbInvokeRegOrderA.lean b/proofs/Fl4ProofsA64/P3/TcbInvokeRegOrderA.lean new file mode 100644 index 000000000..5a8fd34d6 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbInvokeRegOrderA.lean @@ -0,0 +1,70 @@ +/- +P3 (AArch64): the concrete register array order and the WriteRegisters +continuation after the frame and GP transfers. +[J] The named loop results are at the actual bounded counts. +-/ +import Fl4ProofsA64.P3.TcbInvokeRegisterSelectA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4SpecA64 + +/-- The frame transfer starts with FaultIP, SP and SPSR. The rest follows +the abstract A64 frame-register order. -/ +theorem tcbFrameRegisters_eqA : + regs.FRAME_REGISTERS.val = frameRegisters.map Endp.regIdxA := by + simp only [regs.FRAME_REGISTERS, frameRegisters, Register.x0ToX8, Endp.regIdxA, + layout.reg.FAULT_IP, layout.reg.SP_EL0, layout.reg.SPSR_EL1, layout.reg.X0, + layout.reg.X1, layout.reg.X2, layout.reg.X3, layout.reg.X4, layout.reg.X5, + layout.reg.X6, layout.reg.X7, layout.reg.X8, layout.reg.X16, + layout.reg.X17, layout.reg.X18, layout.reg.X29, layout.reg.X30] <;> rfl + +/-- The remaining general registers and both TLS registers come from the +same ordered list used by the abstract operation. -/ +theorem tcbGpRegisters_eqA : + regs.GP_REGISTERS.val = gpRegisters.map Endp.regIdxA := by + simp only [regs.GP_REGISTERS, gpRegisters, Register.x9ToX15, Register.x19ToX28, + Endp.regIdxA, layout.reg.X9, layout.reg.X10, layout.reg.X11, layout.reg.X12, + layout.reg.X13, layout.reg.X14, layout.reg.X15, layout.reg.X19, layout.reg.X20, + layout.reg.X21, layout.reg.X22, layout.reg.X23, layout.reg.X24, layout.reg.X25, + layout.reg.X26, layout.reg.X27, layout.reg.X28, layout.reg.TPIDR_EL0, + layout.reg.TPIDRRO_EL0] <;> rfl + +section +variable {S A : Type} {st : machine.Store S} [L : LawfulStore st] {ar : machine.Arch A} + +set_option trace.profiler true in +/-- The real WriteRegisters body transfers at most 36 registers, then +copies FaultIP to NextIP before any restart. Rescheduling compares the +current thread only after the optional restart. -/ +theorem tcbWriteRegisters_afterLoopsA {s sFrame sGp : S} {a : A} {dest count buf : U64} + (resume : Bool) + (hFrame : tcb.write_registers_loop0 st s dest buf (L.curW s) + (if 36#u64 < count then 36#u64 else count) 0#u64 = ok sFrame) + (hGp : tcb.write_registers_loop1 st sFrame dest buf (L.curW s) + (if 36#u64 < count then 36#u64 else count) 0#u64 = ok sGp) : + tcb.write_registers st ar s a dest resume count buf = (do + let pc := L.mem sGp (Endp.regA dest layout.reg.FAULT_IP) + let sIp := L.wr sGp (Endp.regA dest layout.reg.NEXT_IP) pc + let (sRestart, aRestart) ← if resume then sched.restart st ar sIp a dest else ok (sIp, a) + if dest = L.curW sRestart then + let sFinal ← sched.reschedule_required st sRestart + ok (sFinal, aRestart) + else ok (sRestart, aRestart)) := by + have hN : (tcb.N_FRAME + tcb.N_GP : Result U64) = ok 36#u64 := by + rw [tcb.N_FRAME, tcb.N_GP] + rfl + have hCount : (if 36#u64 < count then ok 36#u64 else ok count) = + ok (if 36#u64 < count then 36#u64 else count) := by + by_cases h : 36#u64 < count + · simp only [if_pos h] + · simp only [if_neg h] + rw [tcb.write_registers, cur_thread_ok, bind_ok, hN, bind_ok, hCount, bind_ok, + hFrame, bind_ok, hGp, bind_ok] + have hReg : regWA dest layout.reg.FAULT_IP = Endp.regA dest layout.reg.FAULT_IP := rfl + simp only [tcb_reg_eqA, hReg, Endp.set_reg_eqA, bind_ok, tcb.post_modify_registers, cur_thread_ok, + obj.Tcb.Insts.CoreCmpPartialEqTcb.eq, decide_eq_true_eq] + +end +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/TcbInvokeRegisterSelectA.lean b/proofs/Fl4ProofsA64/P3/TcbInvokeRegisterSelectA.lean new file mode 100644 index 000000000..0b664eea0 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbInvokeRegisterSelectA.lean @@ -0,0 +1,174 @@ +/- +P3 (AArch64): the ReadRegisters, WriteRegisters and CopyRegisters +selectors, with the actual pre-Restart error order. +[S] Where a target or source stall is executed, it leaves the states unchanged. +-/ +import Fl4ProofsA64.P3.TcbInvokeRegsA +import Fl4ProofsA64.P1.VcpuHooks + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 + +section +variable {S A : Type} {st : machine.Store S} [L : LawfulStore st] {ar : machine.Arch A} +variable {s sRestart : S} {a : A} {target src flags count tailLength srcSlot : U64} {ctx : tcb.Ctx} + +private theorem tcbRegCountA : tcb.N_FRAME + tcb.N_GP = ok 36#u64 := by + rw [tcb.N_FRAME, tcb.N_GP] + rfl + +theorem tcbReadRegisters_countErrorA + (hst : smp.remote_tcb_stall st ar s a target = ok (s, a)) + (hlen : ¬ctx.length < 2#u64) + (hf : tcb.arg st s ctx 0#u64 = ok flags) (hn : tcb.arg st s ctx 1#u64 = ok count) + (hrange : count < 1#u64 ∨ 36#u64 < count) : + tcb.decode st ar s a fl4_abi.label.TCBReadRegisters target ctx = + ok (.Err (.Sys (.RangeError 1#u64 36#u64)), s, a) := by + rw [tcbReadRegisters_programA hst] + simp only [tcb.decode_read_registers, if_neg hlen, hf, hn, bind_ok, tcbRegCountA] + by_cases hlo : count < 1#u64 + · simp only [if_pos hlo, error.range, bind_ok] + · simp only [if_neg hlo, if_pos (hrange.resolve_left hlo), error.range, bind_ok] + +theorem tcbReadRegisters_acceptedA + (hst : smp.remote_tcb_stall st ar s a target = ok (s, a)) + (hlen : ¬ctx.length < 2#u64) + (hf : tcb.arg st s ctx 0#u64 = ok flags) (hn : tcb.arg st s ctx 1#u64 = ok count) + (hlo : ¬count < 1#u64) (hhi : ¬36#u64 < count) (hself : target ≠ L.curW s) + (hr : tcb.restart_caller st s = ok sRestart) : + tcb.decode st ar s a fl4_abi.label.TCBReadRegisters target ctx = (do + let (sFinal, aFinal) ← tcb.read_registers st ar sRestart a target + ((flags &&& 1#u64) != 0#u64) count ctx.call + ok (.Ok (), sFinal, aFinal)) := by + rw [tcbReadRegisters_programA hst] + simp only [tcb.decode_read_registers, if_neg hlen, hf, hn, bind_ok, tcbRegCountA, if_neg hlo, + if_neg hhi, cur_thread_ok, obj.Tcb.Insts.CoreCmpPartialEqTcb.eq, + decide_eq_false hself, Bool.false_eq_true, ite_false, hr, lift] + +/-- A Send performs optional source suspension and sets the caller Running. +It takes the caller from before source suspension. -/ +theorem tcbReadRegisters_sendA (s : S) (a : A) (src count : U64) (suspend : Bool) : + tcb.read_registers st ar s a src suspend count false = (do + let (sSuspended, aSuspended) ← if suspend then sched.suspend st ar s a src else ok (s, a) + let sFinal ← sched.set_thread_state st sSuspended (L.curW s) sched.TS_RUNNING + ok (sFinal, aSuspended)) := by + simp only [tcb.read_registers, cur_thread_ok, bind_ok, Bool.false_eq_true, ite_false] + +/-- The raw count is compared with the actual remaining message length +before any self-target check or Restart. -/ +theorem tcbWriteRegisters_countErrorA + (hst : smp.remote_tcb_stall st ar s a target = ok (s, a)) + (hlen : ¬ctx.length < 2#u64) + (hf : tcb.arg st s ctx 0#u64 = ok flags) (hn : tcb.arg st s ctx 1#u64 = ok count) + (htail : ctx.length - 2#u64 = ok tailLength) (hshort : tailLength < count) : + tcb.decode st ar s a fl4_abi.label.TCBWriteRegisters target ctx = + ok (.Err (.Sys .TruncatedMessage), s, a) := by + rw [tcbWriteRegisters_programA hst] + simp only [tcb.decode_write_registers, if_neg hlen, hf, hn, htail, bind_ok, if_pos hshort, error.truncated] + +theorem tcbWriteRegisters_acceptedA + (hst : smp.remote_tcb_stall st ar s a target = ok (s, a)) + (hlen : ¬ctx.length < 2#u64) + (hf : tcb.arg st s ctx 0#u64 = ok flags) (hn : tcb.arg st s ctx 1#u64 = ok count) + (htail : ctx.length - 2#u64 = ok tailLength) (henough : ¬tailLength < count) + (hself : target ≠ L.curW s) (hr : tcb.restart_caller st s = ok sRestart) : + tcb.decode st ar s a fl4_abi.label.TCBWriteRegisters target ctx = (do + let (sFinal, aFinal) ← tcb.write_registers st ar sRestart a target + ((flags &&& 1#u64) != 0#u64) count ctx.buf + ok (.Ok (), sFinal, aFinal)) := by + rw [tcbWriteRegisters_programA hst] + simp only [tcb.decode_write_registers, if_neg hlen, hf, hn, htail, bind_ok, if_neg henough, + cur_thread_ok, obj.Tcb.Insts.CoreCmpPartialEqTcb.eq, decide_eq_false hself, Bool.false_eq_true, + ite_false, hr, lift] + +/-- The source Thread-cap check precedes the source stall. The actual +caller Restart then hands the unchanged flag word to the copy body. -/ +theorem tcbCopyRegisters_acceptedA + (hst : smp.remote_tcb_stall st ar s a target = ok (s, a)) + (he : tcb.extra_with_length ctx 0#usize 1#u64 = ok (.Ok srcSlot)) + (hf : tcb.arg st s ctx 0#u64 = ok flags) + (hc : obj.Slot.cap st srcSlot s = ok (.Thread src)) + (hsrc : smp.remote_tcb_stall st ar s a src = ok (s, a)) + (hr : tcb.restart_caller st s = ok sRestart) : + tcb.decode st ar s a fl4_abi.label.TCBCopyRegisters target ctx = + tcb.copy_registers st ar sRestart a target src flags := by + rw [tcbCopyRegisters_programA hst] + simp only [tcb.decode_copy_registers, he, hf, tcb.thread_arg, hc, bind_ok, + core.result.Result.Insts.CoreOpsTry.branch, hsrc, uncurry_apply_pair, hr] + +/-- ReadRegisters rejects a short message before reading either argument. -/ +theorem tcbReadRegisters_truncatedA + (hst : smp.remote_tcb_stall st ar s a target = ok (s, a)) + (hlen : ctx.length < 2#u64) : + tcb.decode st ar s a fl4_abi.label.TCBReadRegisters target ctx = + ok (.Err (.Sys .TruncatedMessage), s, a) := by + rw [tcbReadRegisters_programA hst] + simp only [tcb.decode_read_registers, if_pos hlen, error.truncated, bind_ok] + +/-- ReadRegisters rejects self only after the count passes both bounds. -/ +theorem tcbReadRegisters_selfA + (hst : smp.remote_tcb_stall st ar s a target = ok (s, a)) + (hlen : ¬ctx.length < 2#u64) + (hf : tcb.arg st s ctx 0#u64 = ok flags) (hn : tcb.arg st s ctx 1#u64 = ok count) + (hlo : ¬count < 1#u64) (hhi : ¬36#u64 < count) (hself : target = L.curW s) : + tcb.decode st ar s a fl4_abi.label.TCBReadRegisters target ctx = + ok (.Err (.Sys .IllegalOperation), s, a) := by + rw [tcbReadRegisters_programA hst] + simp only [tcb.decode_read_registers, if_neg hlen, hf, hn, bind_ok, tcbRegCountA, + if_neg hlo, if_neg hhi, cur_thread_ok, obj.Tcb.Insts.CoreCmpPartialEqTcb.eq, + hself, decide_eq_true_eq, ite_true, error.illegal] + +/-- WriteRegisters requires both its flag and count words. -/ +theorem tcbWriteRegisters_truncatedA + (hst : smp.remote_tcb_stall st ar s a target = ok (s, a)) + (hlen : ctx.length < 2#u64) : + tcb.decode st ar s a fl4_abi.label.TCBWriteRegisters target ctx = + ok (.Err (.Sys .TruncatedMessage), s, a) := by + rw [tcbWriteRegisters_programA hst] + simp only [tcb.decode_write_registers, if_pos hlen, error.truncated, bind_ok] + +/-- A complete WriteRegisters payload still rejects the current thread. -/ +theorem tcbWriteRegisters_selfA + (hst : smp.remote_tcb_stall st ar s a target = ok (s, a)) + (hlen : ¬ctx.length < 2#u64) + (hf : tcb.arg st s ctx 0#u64 = ok flags) (hn : tcb.arg st s ctx 1#u64 = ok count) + (htail : ctx.length - 2#u64 = ok tailLength) (henough : ¬tailLength < count) + (hself : target = L.curW s) : + tcb.decode st ar s a fl4_abi.label.TCBWriteRegisters target ctx = + ok (.Err (.Sys .IllegalOperation), s, a) := by + rw [tcbWriteRegisters_programA hst] + simp only [tcb.decode_write_registers, if_neg hlen, hf, hn, htail, bind_ok, if_neg henough, + cur_thread_ok, obj.Tcb.Insts.CoreCmpPartialEqTcb.eq, hself, decide_eq_true_eq, ite_true, error.illegal] + +/-- CopyRegisters checks the extra slot and message length before reading flags. -/ +theorem tcbCopyRegisters_truncatedA + (hst : smp.remote_tcb_stall st ar s a target = ok (s, a)) + (he : tcb.extra_with_length ctx 0#usize 1#u64 = ok (.Err (.Sys .TruncatedMessage))) : + tcb.decode st ar s a fl4_abi.label.TCBCopyRegisters target ctx = + ok (.Err (.Sys .TruncatedMessage), s, a) := by + rw [tcbCopyRegisters_programA hst] + simp only [tcb.decode_copy_registers, he, bind_ok, core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, Std.bind_tc_ok] + +/-- A non-Thread extra capability fails before either source stall or caller Restart. -/ +theorem tcbCopyRegisters_badSourceA {cap : capability.Cap} + (hst : smp.remote_tcb_stall st ar s a target = ok (s, a)) + (he : tcb.extra_with_length ctx 0#usize 1#u64 = ok (.Ok srcSlot)) + (hf : tcb.arg st s ctx 0#u64 = ok flags) (hc : obj.Slot.cap st srcSlot s = ok cap) + (hKind : ∀ p, cap ≠ .Thread p) : + tcb.decode st ar s a fl4_abi.label.TCBCopyRegisters target ctx = + ok (.Err (.Sys (.InvalidCapability 1#u64)), s, a) := by + rw [tcbCopyRegisters_programA hst] + cases cap with + | Thread p => exact (hKind p rfl).elim + | _ => + simp only [tcb.decode_copy_registers, he, hf, tcb.thread_arg, hc, bind_ok, error.invalid_cap, + core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, Std.bind_tc_ok] + +end +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/TcbInvokeRegsA.lean b/proofs/Fl4ProofsA64/P3/TcbInvokeRegsA.lean new file mode 100644 index 000000000..f513679d1 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbInvokeRegsA.lean @@ -0,0 +1,93 @@ +/- +P3 (AArch64): register sanitisation in the TCB invocation register paths. +The native SPSR projection is the l4v conversion. The guest branch +checks the concrete bound VCPU and uses the FL4 guest SPSR conversion. +-/ +import Fl4ProofsA64.P3.TcbInvokeDispatchA +import Fl4ProofsA64.P3.EpCopyMrsA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4SpecA64 + +private theorem scalar_eq_bvA {u v : U64} (e : u.bv = v.bv) : u = v := + UScalar.eq_of_val_eq (congrArg BitVec.toNat e) + +private theorem spsrMode_eqA (m : U64) (c : U64) : m = c ↔ m.bv = c.bv := + ⟨fun h => congrArg UScalar.bv h, scalar_eq_bvA⟩ + +/-- Evaluate the concrete sanitiser against the Ext projection, including +the restricted guest modes and the EL1h fallback. The bound VCPU read +is needed only for writes to SPSR_EL1. -/ +theorem tcbSanitiseRegister_eqA {S : Type} {st : machine.Store S} (s : S) (t : U64) + (r : Register) (v : U64) (guest : Bool) + (hguest : r = .spsrEl1 → ∃ p, vcpu.bound st s t = ok p ∧ guest = (p != 0#u64)) : + regs.sanitise_for_tcb st s t (Endp.regIdxA r) v = + ok (⟨fl4SanitiseRegister guest r v.bv⟩ : U64) := by + by_cases hr : r = .spsrEl1 + · subst r + obtain ⟨p, hv, e⟩ := hguest rfl + have hr : Endp.regIdxA .spsrEl1 = layout.reg.SPSR_EL1 := by + unfold Endp.regIdxA layout.reg.SPSR_EL1 + rfl + rw [hr] + unfold regs.sanitise_for_tcb + simp only [ite_true, hv, bind_ok] + cases guest + · have hh : (p != 0#u64) = false := e.symm + rw [hh] + simp only [Bool.false_eq_true, ite_false, regs.sanitise, ite_true, lift, bind_ok, + eq_of_spec_fd Fl4ProofsA64.P0.Consts.regs.PSTATE_USER.spec] + congr 1 + apply scalar_eq_bvA + simp only [fl4SanitiseRegister, sanitiseRegister, Bool.false_eq_true, ite_false, + UScalar.bv_or, UScalar.bv_and, regs.PSTATE_FLAGS] + rfl + · have hh : (p != 0#u64) = true := e.symm + rw [hh] + simp only [ite_true, regs.sanitise_vcpu_spsr, lift, bind_ok] + rw [eq_of_spec_fd Fl4ProofsA64.P0.Consts.regs.VCPU_PSTATE_ALLOWED.spec] + let m : U64 := v &&& 31#u64 + have hm : m.bv = v.bv &&& 31 := UScalar.bv_and _ _ + have e0 : m.bv = 0 ↔ m = 0#u64 := (spsrMode_eqA m 0#u64).symm + have e4 : m.bv = 4 ↔ m = 4#u64 := (spsrMode_eqA m 4#u64).symm + have e5 : m.bv = 5 ↔ m = 5#u64 := (spsrMode_eqA m 5#u64).symm + have hmode : + (if m = 0#u64 then ok m else if m = 4#u64 then ok m + else if m = 5#u64 then ok m else ok 5#u64) = + ok (if m = 0#u64 ∨ m = 4#u64 ∨ m = 5#u64 then m else 5#u64) := by + by_cases h0 : m = 0#u64 + · simp [h0] + · by_cases h4 : m = 4#u64 + · simp [h0, h4] + · by_cases h5 : m = 5#u64 <;> simp [h0, h4, h5] + change ((if m = 0#u64 then ok m else if m = 4#u64 then ok m + else if m = 5#u64 then ok m else ok 5#u64) >>= _) = _ + rw [hmode] + change Aeneas.Std.bind (ok (if m = 0#u64 ∨ m = 4#u64 ∨ m = 5#u64 then m else 5#u64)) + (fun mode => Aeneas.Std.bind (ok 4092600256#u64) (fun mask => ok (v &&& mask ||| mode))) = _ + rw [bind_ok, bind_ok] + congr 1 + apply scalar_eq_bvA + simp only [UScalar.bv_or, UScalar.bv_and] + change (v.bv &&& 4092600256) ||| (if m = 0#u64 ∨ m = 4#u64 ∨ m = 5#u64 then m else 5#u64).bv = + fl4SanitiseRegister true .spsrEl1 v.bv + simp only [fl4SanitiseRegister, ite_true, ← hm, e0, e4, e5] + split <;> rfl + · have hn : Endp.regIdxA r ≠ layout.reg.SPSR_EL1 := by + cases r <;> simp_all [Endp.regIdxA] + simp only [regs.sanitise_for_tcb, regs.sanitise, if_neg hn] + cases r <;> simp_all [fl4SanitiseRegister] + +/-- A sanitising register write touches exactly the selected TCB word. -/ +theorem tcbWriteSanitisedReg_eqA {S : Type} {st : machine.Store S} [L : LawfulStore st] + (s : S) (t : U64) (r : Register) (v : U64) (guest : Bool) + (hguest : r = .spsrEl1 → ∃ p, vcpu.bound st s t = ok p ∧ guest = (p != 0#u64)) : + (do + let x ← regs.sanitise_for_tcb st s t (Endp.regIdxA r) v + obj.Tcb.set_reg st t s (Endp.regIdxA r) x) = + ok (L.wr s (Endp.regA t (Endp.regIdxA r)) ⟨fl4SanitiseRegister guest r v.bv⟩) := by + rw [tcbSanitiseRegister_eqA s t r v guest hguest, bind_ok, Endp.set_reg_eqA] + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/TcbInvokeResumeA.lean b/proofs/Fl4ProofsA64/P3/TcbInvokeResumeA.lean new file mode 100644 index 000000000..a308690ff --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbInvokeResumeA.lean @@ -0,0 +1,172 @@ +/- +P3 (AArch64): the shared FL4 SC resume tail of Resume and SetSchedParams. +[P] Fl4MinBudgetAgreeA is the shared row-12 timer observation. +[J] The selected thread's schedulable flag and the result of postponing +are related in the post-cancellation state, using the scheduler relation. +-/ +import Fl4ProofsA64.P3.TcbInvokeDispatchA +import Fl4ProofsA64.P3.IpcScReplyTransferA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4Spec Fl4SpecA64 Sched + +noncomputable section + +/-- The matching MCS operation asserts a bound thread. The null-SC +branch is handled by the caller's optional SC read. -/ +theorem tcbResumeSc_unboundA {σ : State Unit} {p : ObjRef} + {sc : SchedContext} {n : Nat} + (hk : σ.kheap p = some (.schedContext sc n)) (h0 : sc.scTcb = none) : + fl4SchedContextResume (Z := Unit) p σ = (∅, True) := by + unfold fl4SchedContextResume fl4GetScRefillSufficient + rw [nd_bind_oneSc (getSchedContext_eqA hk), h0] + simp [NonDet.assertOpt, NonDet.fail, nd_bind_applySc] + +private theorem tcbResumeSufficientReadA {σ : State Unit} {p : ObjRef} {sc : SchedContext} {n : Nat} + (hk : σ.kheap p = some (.schedContext sc n)) (hne : sc.scRefills ≠ []) : + (NonDet.getsThe fun s : State Unit => (do + let head ← readRefillHead p + Lookup.oreturn (fl4RefillSufficient 0 head)) s) σ = + ({(fl4RefillSufficient 0 (refillHd sc), σ)}, False) := by + have hRead : (readRefillHead p >>= fun head => Lookup.oreturn (fl4RefillSufficient 0 head)) σ = + some (fl4RefillSufficient 0 (refillHd sc)) := by + show Option.bind (readRefillHead p σ) (fun head => some (fl4RefillSufficient 0 head)) = _ + rw [readRefillHead_eqA hk hne] + rfl + change NonDet.assertOpt ((readRefillHead p >>= fun head => Lookup.oreturn (fl4RefillSufficient 0 head)) σ) σ = _ + rw [hRead] + rfl + +/-- Bound SC resume uses the MCS active and ready-queue checks before postpone. -/ +theorem tcbResumeSc_boundA {σ : State Unit} {p t : ObjRef} + {sc : SchedContext} {n : Nat} {x : Tcb} + (hk : σ.kheap p = some (.schedContext sc n)) (hbound : sc.scTcb = some t) + (hkt : σ.kheap t = some (.tcb x)) (hne : scActive sc = true → sc.scRefills ≠ []) + (hact : schedulable t σ = true → scActive sc = true) + (hnq : t ∉ σ.readyQueues x.tcbDomain x.tcbPriority) : + fl4SchedContextResume (Z := Unit) p σ = + if schedulable t σ = true ∧ (refillReady σ.curTime (refillHd sc) && + fl4RefillSufficient 0 (refillHd sc)) = false + then postpone p σ else ({((), σ)}, False) := by + unfold fl4SchedContextResume fl4GetScRefillSufficient + rw [nd_bind_oneSc (getSchedContext_eqA hk), hbound, + nd_bind_oneSc (show NonDet.assertOpt (σ := State Unit) (some t) σ = ({(t, σ)}, False) from rfl), nd_gets_bindSc] + by_cases hs : schedulable t σ = true + · have hr := (schedulable_partsA hs).1 x hkt + have ha := hact hs + simp only [NonDet.when, hs, ↓reduceIte, true_and] + rw [nd_bind_oneSc (threadGet_eqSc _ hkt), nd_bind_oneSc (getScRefillReady_eqA hk (hne ha)), + nd_bind_oneSc (tcbResumeSufficientReadA hk (hne ha))] + by_cases hb : (refillReady σ.curTime (refillHd sc) && fl4RefillSufficient 0 (refillHd sc)) = false + · have hn : ¬(refillReady σ.curTime (refillHd sc) = true ∧ fl4RefillSufficient 0 (refillHd sc) = true) := by + simp only [← Bool.and_eq_true]; rw [hb]; decide + rw [if_pos ⟨hr, ha, hn⟩, if_pos hb] + unfold getTcbQueue + rw [nd_bind_oneSc (threadGet_eqSc _ hkt), nd_bind_oneSc (threadGet_eqSc _ hkt), + nd_bind_oneSc (nd_gets_bindSc _ _ σ), + nd_bind_oneSc (assert_trueSc hnq σ)] + · have hBoth : refillReady σ.curTime (refillHd sc) = true ∧ fl4RefillSufficient 0 (refillHd sc) = true := by + rw [← Bool.and_eq_true]; exact Bool.eq_true_of_not_eq_false hb + rw [if_neg (fun h => h.2.2 hBoth), if_neg hb] + rfl + · simp only [NonDet.when, hs, ↓reduceIte, false_and] + rfl + +section +variable {S A : Type} {st : machine.Store S} [L : LawfulStore st] {ar : machine.Arch A} + +theorem tcbScResume_nullA (s : S) (a : A) : schedcontext.resume st ar s a 0#u64 = ok (s, a) := by + unfold schedcontext.resume + rw [eq_of_spec_fd (obj.Sc.is_null.spec _), bind_ok] + rfl + +theorem tcbScResume_unboundA (s : S) (a : A) (q : U64) (hq : q ≠ 0#u64) + (ht : obj.Sc.tcb st q s = ok 0#u64) : + schedcontext.resume st ar s a q = ok (s, a) := by + unfold schedcontext.resume + simp only [eq_of_spec_fd (obj.Sc.is_null.spec _), hq, decide_false, Bool.false_eq_true, + ite_false, bind_ok, ht, eq_of_spec_fd (obj.Tcb.is_null.spec _)] + rfl + +/-- The live SC tail reads readiness first and consults the FL4 minimum +only when the head is ready. Both late and insufficient heads postpone. -/ +theorem tcbScResume_fl4A {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {a : A} {σ : State Unit} + (H : SchedSRN st Z m κ s σ) {q t : U64} (hq : q ≠ 0#u64) (ht : t ≠ 0#u64) + (hc : q ∈ Z.C) {sc : SchedContext} {n : Nat} + (hk : σ.kheap (scNameA m q) = some (.schedContext sc n)) + (hbt : sc.scTcb = some (specPtr t)) (htcb : obj.Sc.tcb st q s = ok t) + (hsched : sched.is_schedulable st s t = ok (schedulable (specPtr t) σ)) + {x : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb x)) + (hact : schedulable (specPtr t) σ = true → scActive sc = true) + (hnq : specPtr t ∉ σ.readyQueues x.tcbDomain x.tcbPriority) + (hm : Fl4MinBudgetAgreeA ar a) + (R : S → State Unit → Prop) (hR : R s σ) + (hp : sched.postpone st s q ⦃ s' => ∃ σ', postpone (scNameA m q) σ = ({((), σ')}, False) ∧ R s' σ' ⦄) : + schedcontext.resume st ar s a q ⦃ r => r.2 = a ∧ ∃ σ', + fl4SchedContextResume (scNameA m q) σ = ({((), σ')}, False) ∧ R r.1 σ' ⦄ := by + have hspec := tcbResumeSc_boundA hk hbt hkt (H.inv.ne hk) hact hnq + unfold schedcontext.resume + simp only [eq_of_spec_fd (obj.Sc.is_null.spec _), hq, decide_false, Bool.false_eq_true, ite_false, + bind_ok, htcb, eq_of_spec_fd (obj.Tcb.is_null.spec _), ht, hsched] + by_cases hs : schedulable (specPtr t) σ = true + swap + · rw [if_neg hs, WP.spec_ok] + exact ⟨rfl, σ, hspec.trans (if_neg (fun h => hs h.1)), hR⟩ + rw [if_pos hs] + have ha := hact hs + have hne := H.inv.ne hk ha + have late (hb : (refillReady σ.curTime (refillHd sc) && + fl4RefillSufficient 0 (refillHd sc)) = false) : + (do let s' ← sched.postpone st s q; ok (s', a)) ⦃ r => r.2 = a ∧ ∃ σ', + fl4SchedContextResume (scNameA m q) σ = ({((), σ')}, False) ∧ R r.1 σ' ⦄ := by + refine WP.spec_bind hp fun s' ⟨σ', hσ, hR'⟩ => ?_ + rw [WP.spec_ok] + exact ⟨rfl, σ', hspec.trans ((if_pos ⟨hs, hb⟩).trans hσ), hR'⟩ + refine WP.spec_bind (ready_refinesA H.scr H.sch.time hc hk ha) ?_ + intro b hb + rw [getScRefillReady_eqA hk hne] at hb + have he : refillReady σ.curTime (refillHd sc) = b := + (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst hb))).1 + cases b + · simp only [Bool.false_eq_true, ite_false] + exact late (by rw [he, Bool.false_and]) + · simp only [ite_true] + refine WP.spec_bind (fl4_sufficient_refinesA a H.scr hc hk ha 0#u64 hm) ?_ + intro enough henough + cases enough + · simp only [Bool.false_eq_true, ite_false] + exact late (by rw [he, Bool.true_and]; exact henough) + · change fl4RefillSufficient (0 : Data) (refillHd sc) = true at henough + simp only [ite_true, WP.spec_ok] + exact ⟨trivial, σ, hspec.trans (if_neg (by rw [he, Bool.true_and, henough]; simp)), hR⟩ + +/-- A stopped thread can return a donated SC during cancellation. The +raw SC read follows cancellation and the Restart write, then the same +word is carried through the sporadic check and into the FL4 resume tail. -/ +theorem tcbRestart_readAfterCancelA {s s1 s2 : S} {a a1 : A} {t : U64} + (hstop : sched.is_stopped st s t = ok true) + (hcancel : ipc.cancel_ipc st ar s a t = ok (s1, a1)) + (hstate : sched.set_thread_state st s1 t sched.TS_RESTART = ok s2) : + sched.restart st ar s a t = (do + let q := L.mem s2 (scA t) + let sp ← sporadic.sc_sporadic st s2 q + let s3 ← if sp then do + let curr ← sched.cur_sc st s2 + let different ← core.cmp.PartialEq.ne.trait_default obj.Sc.Insts.CoreCmpPartialEqSc q curr + if different then sporadic.unblock_check st s2 q else ok s2 + else ok s2 + let (s4, a2) ← schedcontext.resume st ar s3 a1 q + let canSwitch ← sched.is_schedulable st s4 t + if canSwitch then + let s5 ← sched.possible_switch_to st s4 t + ok (s5, a2) + else ok (s4, a2)) := by + unfold sched.restart + simp only [hstop, bind_ok, ite_true, hcancel, uncurry_apply_pair, hstate, + eq_of_spec_fd (obj.Tcb.sc.spec (L := L) s2 t)] + +end +end +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/TcbInvokeRootsA.lean b/proofs/Fl4ProofsA64/P3/TcbInvokeRootsA.lean new file mode 100644 index 000000000..5319986ff --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbInvokeRootsA.lean @@ -0,0 +1,51 @@ +/- +P3 (AArch64): the CSpace and VSpace root checks shared by Configure and +SetSpace. [J] The existing capability projection supplies CapWfA at the +result of derivation, including level zero on a Stage-2 root. +-/ +import Fl4ProofsA64.P3.TcbInvokeSlotA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4SpecA64 + +/-- The concrete predicate and the Ext predicate accept precisely the +same derived native and Stage-2 VTable roots. -/ +theorem tcbVtableRoot_eqA (c : capability.Cap) (h : CapWfA c) : + vspace.is_valid_vtable_root c = + ok (isValidVtableRoot (absCapA c) || fl4MappedStage2Root (absCapA c)) := by + cases c <;> try rfl + case Frame p size rights dev mapped stage2 asid va => + cases stage2 <;> rfl + case Pml4 p mapped a => + cases mapped <;> rfl + case Ept p level mapped a v => + have hz : level = 0#u64 := UScalar.eq_of_val_eq h.1 + subst level + cases mapped <;> rfl + +section +variable {S : Type} {st : machine.Store S} {s : S} +variable {t cdata vdata cslot vslot ctable vtable : U64} + +/-- Given successful derivation, both root decoders use the same VTable +guard, after checking both deletion guards and the derived CNode. -/ +theorem tcbDeriveRoots_postA {p radix guardSize guard : U64} {vcap : capability.Cap} + (hcslot : obj.Tcb.slot t layout.tcb.SLOT_CTABLE = ok ctable) + (hvslot : obj.Tcb.slot t layout.tcb.SLOT_VTABLE = ok vtable) + (hcd : cdt.slot_cap_long_running_delete st s ctable = ok false) + (hvd : cdt.slot_cap_long_running_delete st s vtable = ok false) + (hcr : tcb.derive_with_data st s cslot cdata = ok (.Ok (.CNode p radix guardSize guard))) + (hvr : tcb.derive_with_data st s vslot vdata = ok (.Ok vcap)) + (hwf : CapWfA vcap) : + tcb.derive_roots st s t cdata vdata cslot vslot = + if isValidVtableRoot (absCapA vcap) || fl4MappedStage2Root (absCapA vcap) then + ok (.Ok (.CNode p radix guardSize guard, vcap)) + else ok (.Err (.Sys .IllegalOperation)) := by + simp only [tcb.derive_roots, hcslot, hvslot, hcd, hvd, hcr, hvr, bind_ok, Bool.false_eq_true, + ite_false, ite_true, core.result.Result.Insts.CoreOpsTry.branch, tcb.is_cnode, + tcbVtableRoot_eqA vcap hwf, error.illegal] + +end +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/TcbInvokeScBacklinkA.lean b/proofs/Fl4ProofsA64/P3/TcbInvokeScBacklinkA.lean new file mode 100644 index 000000000..4abb8975d --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbInvokeScBacklinkA.lean @@ -0,0 +1,161 @@ +/- +P3 (AArch64): shared MCS resume at TCB call sites. +[J] The concrete ready-queue bit is clear after cancellation or binding. +The actual postpone operation supplies the scheduler successor on its branch. +-/ +import Fl4ProofsA64.P3.TcbInvokeResumeA +import Fl4ProofsA64.P3.ScBindLinkA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4Spec Fl4SpecA64 Sched + +noncomputable section +variable {S A : Type} {st : machine.Store S} [L : LawfulStore st] {ar : machine.Arch A} + +set_option trace.profiler true in +/-- At the restart or bind resume site, the live TCB link identifies a + reciprocal SC. The decoded queue exclusion discharges the MCS assert. -/ +theorem tcbResumeSc_fromTargetA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {a : A} {σ : State Unit} + (H : SchedSRN st Z m κ s σ) (hst : StEqA Z.T (L.mem s) σ) {t : U64} + (ht : t ∈ Z.T) (hon : L.mem s (affA t) = L.cpu s) + (hQ : (L.mem s (inqA t) &&& sched.Q_QUEUED) = 0#u64) (hm : Fl4MinBudgetAgreeA ar a) + (R : S → State Unit → Prop) (hR : R s σ) + (hp : + let q := L.mem s (scA t) + q ≠ 0#u64 → sched.postpone st s q ⦃ s' => ∃ σ', + postpone (scNameA m q) σ = ({((), σ')}, False) ∧ R s' σ' ⦄) : + schedcontext.resume st ar s a (L.mem s (scA t)) ⦃ r => r.2 = a ∧ ∃ σ', + (do + let p ← getTcbObjRef Tcb.tcbSchedContext (specPtr t) + NonDet.maybeM fl4SchedContextResume p) σ = ({((), σ')}, False) ∧ R r.1 σ' ⦄ := by + obtain ⟨x, hkt, hx⟩ := H.sch.tcb t ht + let q := L.mem s (scA t) + have hx' : x.tcbSchedContext = optScA m q := hx + have hRead : getTcbObjRef Tcb.tcbSchedContext (specPtr t) σ = ({(x.tcbSchedContext, σ)}, False) := + threadGet_eqSc _ hkt + by_cases h0 : q = 0#u64 + · have hn : x.tcbSchedContext = none := by rw [hx', h0, optScA, if_pos rfl] + change schedcontext.resume st ar s a q ⦃ _ ⦄ + rw [h0, tcbScResume_nullA, WP.spec_ok] + refine ⟨rfl, σ, ?_, hR⟩ + rw [nd_bind_oneSc hRead, hn] + rfl + have ht0 : t ≠ 0#u64 := (tcb_ne01Sc (H.core.tcbs t ht)).1 + have hc : q ∈ Z.C := H.rest.scMem t ht h0 + have hxp : x.tcbSchedContext = some (scNameA m q) := by + rw [hx', optScA, if_neg h0, scNameA_eq] + obtain ⟨y, ny, hky, hym⟩ := H.scr.sc q hc + obtain ⟨z, nz, hkz, hz⟩ := H.inv.back _ _ _ hkt hxp + rw [hky] at hkz + cases hkz + have hTcbWord : L.mem s (scTcbA q) = t := by + have he := hym.tcb + rw [← scTcbA_eqSc, hz] at he + by_cases h : L.mem s (scTcbA q) = 0#u64 + · rw [h, optRefS_zero] at he + cases he + · rw [optRefS_ne h] at he + exact specPtr_inj (Option.some.inj he).symm + have hScRead : obj.Sc.tcb st q s = ok t := + (eq_of_spec_fd (obj.Sc.tcb.spec (L := L) s q)).trans (congrArg ok hTcbWord) + have hnq : specPtr t ∉ σ.readyQueues x.tcbDomain x.tcbPriority := by + obtain ⟨w, hkw, hd, _, hPriority⟩ := H.rest.prio t ht + rw [hkt] at hkw + cases hkw + have hRp : BitVec.ofNat 8 (L.mem s (prioA t)).val = x.tcbPriority := by + apply BitVec.eq_of_toNat_eq + rw [BitVec.toNat_ofNat, hPriority] + exact Nat.mod_eq_of_lt x.tcbPriority.isLt + rw [hd, ← hRp, H.rest.rq _ (H.core.prio t ht), mem_map_specPtrSc, ← hon] + exact fun h => (H.core.queued t ht).2 h hQ + have hSchedEq := schedulable_eqA H ht (H.relOn ht hon) hst + have hSched : sched.is_schedulable st s t = ok (schedulable (specPtr t) σ) := by + rw [is_schedulable_eqA, hSchedEq] + have hAct : schedulable (specPtr t) σ = true → scActive y = true := by + intro h + have hp0 := (of_decide_eq_true (hSchedEq.trans h)).2.2.1 + change L.rmax s q > 0#u64 at hp0 + rw [gt_iff_valSc, hym.max] at hp0 + change 0 < y.scRefillMax at hp0 + simpa only [scActive, activeSc, decide_eq_true_eq] using hp0 + change schedcontext.resume st ar s a q ⦃ _ ⦄ + refine WP.spec_mono (tcbScResume_fl4A H h0 ht0 hc hky hz hScRead hSched hkt hAct hnq hm R hR (hp h0)) ?_ + rintro r ⟨ha, σ', hr, hR'⟩ + refine ⟨ha, σ', ?_, hR'⟩ + rw [nd_bind_oneSc hRead, hxp] + exact hr + +set_option trace.profiler true in +/-- The same resume operation at Bind's named post-unblock state. The + stored link written by Bind selects the direct SC argument. -/ +theorem tcbBindSc_resumeFromLinksA {Z : XSetsA} {m : McState} {κ : U64 → U64} + {s : S} {a : A} {σ : State Unit} (H : SchedSRN st Z m κ s σ) (hst : StEqA Z.T (L.mem s) σ) + {t q : U64} (ht : t ∈ Z.T) (hon : L.mem s (affA t) = L.cpu s) + (hLink : L.mem s (scA t) = q) (h0 : q ≠ 0#u64) + (hQ : (L.mem s (inqA t) &&& sched.Q_QUEUED) = 0#u64) (hm : Fl4MinBudgetAgreeA ar a) + (R : S → State Unit → Prop) (hR : R s σ) + (hp : sched.postpone st s q ⦃ s' => ∃ σ', postpone (scNameA m q) σ = ({((), σ')}, False) ∧ R s' σ' ⦄) : + schedcontext.resume st ar s a q ⦃ r => r.2 = a ∧ ∃ σ', + fl4SchedContextResume (scNameA m q) σ = ({((), σ')}, False) ∧ R r.1 σ' ⦄ := by + have hPost : L.mem s (scA t) ≠ 0#u64 → + sched.postpone st s (L.mem s (scA t)) ⦃ s' => ∃ σ', + postpone (scNameA m (L.mem s (scA t))) σ = ({((), σ')}, False) ∧ R s' σ' ⦄ := by + rw [hLink] + exact fun _ => hp + have hResume := tcbResumeSc_fromTargetA H hst ht hon hQ hm R hR hPost + rw [hLink] at hResume + obtain ⟨x, hkt, hx⟩ := H.sch.tcb t ht + have hBound : x.tcbSchedContext = some (scNameA m q) := by + rw [hx, hLink, optScA, if_neg h0, scNameA_eq] + have hRead : getTcbObjRef Tcb.tcbSchedContext (specPtr t) σ = ({(some (scNameA m q), σ)}, False) := by + change threadGet Tcb.tcbSchedContext (specPtr t) σ = _ + rw [threadGet_eqSc _ hkt, hBound] + refine WP.spec_mono hResume ?_ + rintro r ⟨ea, σ', hr, hR'⟩ + rw [nd_bind_oneSc hRead] at hr + exact ⟨ea, σ', hr, hR'⟩ + +set_option trace.profiler true in +/-- The stopped-target Resume reads the SC from the post-cancellation TCB. + Each successor here is the result of the corresponding spec operation. -/ +theorem tcbRestart_scContinuationA {σ σCancel σRestart σUnblock σResume σFinal : State Unit} + {t : ObjRef} {before restarted : Tcb} + (hkt : σ.kheap t = some (.tcb before)) (hStop : ¬runnable before.tcbState ∧ ¬idle before.tcbState) + (hCancel : cancelIpc (Z := Unit) t σ = ({((), σCancel)}, False)) + (hRestart : setThreadState (Z := Unit) t .restart σCancel = ({((), σRestart)}, False)) + (hRead : σRestart.kheap t = some (.tcb restarted)) + (hUnblock : ifSporadicCurScTestRefillUnblockCheck (Z := Unit) restarted.tcbSchedContext σRestart = + ({((), σUnblock)}, False)) + (hResume : NonDet.maybeM fl4SchedContextResume restarted.tcbSchedContext σUnblock = ({((), σResume)}, False)) + (hSwitch : testPossibleSwitchTo (Z := Unit) t σResume = ({((), σFinal)}, False)) : + fl4TcbRestart (Z := Unit) t σ = ({((), σFinal)}, False) := by + unfold fl4TcbRestart + rw [nd_bind_oneSc (Notif.getThreadState_eq hkt), NonDet.when, if_pos hStop, + nd_bind_oneSc hCancel, nd_bind_oneSc hRestart] + change (threadGet Tcb.tcbSchedContext t >>= _) σRestart = _ + rw [nd_bind_oneSc (threadGet_eqSc _ hRead), nd_bind_oneSc hUnblock, nd_bind_oneSc hResume] + exact hSwitch + +set_option trace.profiler true in +/-- SetSchedParams uses the MCS resume after writing both backlinks and + running the unblock check, before enqueue and reschedule. -/ +theorem tcbBindSc_continuationA {σ σUnblock σResume σFinal : State Unit} + {p t : ObjRef} {x : Tcb} {sc : SchedContext} {n : Nat} + (hks : σ.kheap p = some (.schedContext sc n)) (hkt : σ.kheap t = some (.tcb x)) + (hUnblock : ifSporadicActiveCurScTestRefillUnblockCheck (Z := Unit) (some p) (donStA σ p t sc n x) = + ({((), σUnblock)}, False)) + (hResume : fl4SchedContextResume p σUnblock = ({((), σResume)}, False)) + (hEnqueue : (do + let eligible ← NonDet.gets (schedulable (Z := Unit) t) + NonDet.when eligible (do tcbSchedAction tcbSchedEnqueue t; rescheduleRequired)) σResume = + ({((), σFinal)}, False)) : + fl4TcbBindSc (Z := Unit) p t σ = ({((), σFinal)}, False) := by + unfold fl4TcbBindSc + rw [← nd_bind_assocSc, nd_bind_oneSc (bind_links_specA hks hkt), nd_bind_oneSc hUnblock, + nd_bind_oneSc hResume] + exact hEnqueue + +end +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/TcbInvokeSchedControlA.lean b/proofs/Fl4ProofsA64/P3/TcbInvokeSchedControlA.lean new file mode 100644 index 000000000..e7d1d4979 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbInvokeSchedControlA.lean @@ -0,0 +1,66 @@ +/- +P3 (AArch64): the SetSchedParams control path after Restart. +[J] The actual fault-handler installation and priority updates supply the +named successor states. The SC backlink is read after both priority updates. +-/ +import Fl4ProofsA64.P3.TcbInvokeSchedSelectA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} + +set_option trace.profiler true in +/-- A successful fault-handler installation is followed by MCP and priority. +When changed, a null selection reads the current SC and detaches it if +present. A non-null selection reaches the actual bind operation. -/ +theorem tcbSetSchedParams_effectsA (changeSc : Bool) + {sRestart sInstalled sMcp sPriority : S} {a aInstalled : A} + {t invSlot faultSlot mcp prio sc : U64} {fh : capability.Cap} + (hInstall : tcb.install_tcb_cap st ar sRestart a t invSlot + layout.tcb.SLOT_FAULT_HANDLER fh faultSlot = ok (core.result.Result.Ok (), sInstalled, aInstalled)) + (hMcp : obj.Tcb.set_mcp st t sInstalled mcp = ok sMcp) + (hPriority : sched.set_priority st sMcp t prio = ok sPriority) : + tcb.thread_control_sched st ar sRestart a t invSlot (fh, faultSlot) mcp prio sc + (if changeSc then 15#u64 else 14#u64) = + (if changeSc then + if sc = 0#u64 then do + let boundSc ← obj.Tcb.sc st t sPriority + if boundSc = 0#u64 then ok (core.result.Result.Ok (), sPriority, aInstalled) + else + let sFinal ← schedcontext.unbind_tcb st sPriority boundSc + ok (core.result.Result.Ok (), sFinal, aInstalled) + else do + let (sFinal, aFinal) ← schedcontext.bind_tcb st ar sPriority aInstalled sc t + ok (core.result.Result.Ok (), sFinal, aFinal) + else ok (core.result.Result.Ok (), sPriority, aInstalled)) := by + have hNull (p : U64) : obj.Sc.is_null p = ok (decide (p = 0#u64)) := + eq_of_spec_fd (obj.Sc.is_null.spec p) + have hMask (f : U64) (hf : f = 14#u64 ∨ f = 15#u64) : + ((f &&& tcb.SCHED_UPDATE_FAULT) != 0#u64) = true ∧ + ((f &&& tcb.SCHED_UPDATE_MCP) != 0#u64) = true ∧ + ((f &&& tcb.SCHED_UPDATE_PRIO) != 0#u64) = true := by + rcases hf with (rfl | rfl) <;> + rw [tcb.SCHED_UPDATE_FAULT, tcb.SCHED_UPDATE_MCP, tcb.SCHED_UPDATE_PRIO] <;> decide + have h0 : ((14#u64 &&& tcb.SCHED_UPDATE_SC) != 0#u64) = false := by + rw [tcb.SCHED_UPDATE_SC] + decide + have h1 : ((15#u64 &&& tcb.SCHED_UPDATE_SC) != 0#u64) = true := by + rw [tcb.SCHED_UPDATE_SC] + decide + obtain ⟨hf0, hm0, hp0⟩ := hMask 14#u64 (Or.inl rfl) + obtain ⟨hf1, hm1, hp1⟩ := hMask 15#u64 (Or.inr rfl) + clear hMask + cases changeSc <;> + simp only [tcb.thread_control_sched, Bool.false_eq_true, + ite_false, ite_true, lift, bind_ok, hInstall, hMcp, hPriority, + core.result.Result.Insts.CoreOpsTry.branch, hf0, hm0, hp0, hf1, hm1, hp1, + h0, h1, hNull, decide_eq_true_eq] <;> + (try dsimp only) <;> + simp only [hInstall, hMcp, hPriority, bind_ok, uncurry_apply_pair, + core.result.Result.Insts.CoreOpsTry.branch] + +end +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/TcbInvokeSchedSelectA.lean b/proofs/Fl4ProofsA64/P3/TcbInvokeSchedSelectA.lean new file mode 100644 index 000000000..c54a1b4a3 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbInvokeSchedSelectA.lean @@ -0,0 +1,91 @@ +/- +P3 (AArch64): SC selection and control-word decoding for SetSchedParams. +[S] The accepted dispatch uses the quiet stall. +[J] The slot, SC, blocked-state and release observations are provided at decode. +-/ +import Fl4ProofsA64.P3.TcbInvokeThreadFieldsA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 + +section +variable {S A : Type} {st : machine.Store S} [L : LawfulStore st] {ar : machine.Arch A} +variable {s sRestart : S} {a : A} {t slot sc old : U64} {ctx : tcb.Ctx} + +set_option trace.profiler true in +/-- Null detaches an SC only from a TCB other than the current thread. -/ +theorem tcbSchedParamsSc_nullA (hc : obj.Slot.cap st slot s = ok .Null) : + tcb.sched_params_sc st s t slot = + if t = L.curW s then ok (.Err (.Sys .IllegalOperation)) else ok (.Ok 0#u64) := by + simp only [tcb.sched_params_sc, hc, bind_ok, cur_thread_ok, + obj.Tcb.Insts.CoreCmpPartialEqTcb.eq, decide_eq_true_eq, error.illegal] + +set_option trace.profiler true in +/-- Selection checks the two backlinks before inspecting the blocked state. +The release test is shared by the new-SC and same-SC arms. -/ +theorem tcbSchedParamsSc_contextA {badge : U64} + (hc : obj.Slot.cap st slot s = ok (.SchedContext sc badge)) + (hOld : obj.Tcb.sc st t s = ok old) : + tcb.sched_params_sc st s t slot = (do + let checkRelease : Result (core.result.Result obj.Sc error.Exc) := do + let blocked ← sched.is_blocked st s t + if blocked then + let released ← sporadic.sc_released st s sc + if released then ok (.Ok sc) else error.illegal obj.Sc + else ok (.Ok sc) + if old = sc then checkRelease + else if old = 0#u64 then + let bound ← obj.Sc.tcb st sc s + if bound = 0#u64 then checkRelease else error.illegal obj.Sc + else error.illegal obj.Sc) := by + simp only [tcb.sched_params_sc, hc, bind_ok, hOld, bne_iff_ne, ite_not] + +set_option trace.profiler true in +/-- A cap outside the Null and SchedContext cases is rejected at extra 2. -/ +theorem tcbSchedParamsSc_badCapA {c : capability.Cap} + (hc : obj.Slot.cap st slot s = ok c) (hn : c ≠ .Null) + (hsc : ∀ p b, c ≠ .SchedContext p b) : + tcb.sched_params_sc st s t slot = ok (.Err (.Sys (.InvalidCapability 2#u64))) := by + rw [tcb.sched_params_sc, hc, bind_ok] + cases c + all_goals first | exact (hn rfl).elim | exact (hsc _ _ rfl).elim | rfl + +set_option trace.profiler true in +/-- After the four ordered validations and the fault-handler check, the +decoder restarts the caller and selects the SC-update bit by comparing the +two SC pointers. The control phase starts in the named Restart state. -/ +theorem tcbSetSchedParams_controlA {authSlot scSlot faultSlot auth mcp prio : U64} + {fh : capability.Cap} (hst : smp.remote_tcb_stall st ar s a t = ok (s, a)) + (hExtra0 : tcb.extra_with_length ctx 0#usize 2#u64 = ok (.Ok authSlot)) + (hExtra1 : tcb.extra_with_length ctx 1#usize 2#u64 = ok (.Ok scSlot)) + (hExtra2 : tcb.extra_with_length ctx 2#usize 2#u64 = ok (.Ok faultSlot)) + (hMcp : tcb.arg st s ctx 0#u64 = ok mcp) + (hPrio : tcb.arg st s ctx 1#u64 = ok prio) + (hAuth : tcb.thread_arg st s authSlot 1#u64 = ok (.Ok auth)) + (hMcpCheck : tcb.check_prio st s mcp auth = ok (.Ok ())) + (hPrioCheck : tcb.check_prio st s prio auth = ok (.Ok ())) + (hSc : tcb.sched_params_sc st s t scSlot = ok (.Ok sc)) + (hOld : obj.Tcb.sc st t s = ok old) + (hFault : obj.Slot.cap st faultSlot s = ok fh) + (hAllowed : faults.valid_fault_handler fh = ok true) + (hRestart : tcb.restart_caller st s = ok sRestart) : + tcb.decode st ar s a fl4_abi.label.TCBSetSchedParams t ctx = + tcb.thread_control_sched st ar sRestart a t ctx.slot (fh, faultSlot) mcp prio sc + (if old = sc then 14#u64 else 15#u64) := by + rw [tcbSetSchedParams_programA hst] + by_cases h : old = sc + · simp only [tcb.decode_set_sched_params, hExtra0, hExtra1, hExtra2, hMcp, hPrio, hAuth, + hMcpCheck, hPrioCheck, hSc, hOld, hFault, hAllowed, hRestart, + core.result.Result.Insts.CoreOpsTry.branch, bind_ok, lift, h, bne_self_eq_false, + Bool.false_eq_true, ite_false, ite_true, tcb.SCHED_UPDATE_MCP, tcb.SCHED_UPDATE_PRIO, + tcb.SCHED_UPDATE_FAULT] <;> rfl + · have hb : (old != sc) = true := bne_iff_ne.mpr h + simp only [tcb.decode_set_sched_params, hExtra0, hExtra1, hExtra2, hMcp, hPrio, hAuth, + hMcpCheck, hPrioCheck, hSc, hOld, hFault, hAllowed, hRestart, + core.result.Result.Insts.CoreOpsTry.branch, bind_ok, lift, hb, if_neg h, ite_true, + tcb.SCHED_UPDATE_MCP, tcb.SCHED_UPDATE_PRIO, tcb.SCHED_UPDATE_FAULT, tcb.SCHED_UPDATE_SC] <;> rfl + +end +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/TcbInvokeSetTimerA.lean b/proofs/Fl4ProofsA64/P3/TcbInvokeSetTimerA.lean new file mode 100644 index 000000000..7b6bc91da --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbInvokeSetTimerA.lean @@ -0,0 +1,35 @@ +/- +P3 (AArch64): the local SetTimer Store post against fl4SetTcbTimer. +[S] A previously armed target belongs to the selected CPU. +-/ +import Fl4ProofsA64.P3.TcbInvokeTimerStoreA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4SpecA64 + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {T Cs : Set U64} + +attribute [local irreducible] tdA thA fl4SetTcbTimer + +set_option trace.profiler true in +/-- Combine the local removal and insertion proofs using their named +intermediate state. The cached minimum is part of the final tree. -/ +theorem tcbTimerInsert_queueA (s : S) (t d bd : U64) {W : TRB.RBT} + (hT : TcbPlaced T) (ht : t ∈ T) (hR : TimerRB st T Cs s) + (hN : ∀ cpu ∈ Cs, NodePlaced st T s cpu) (hcpu : L.cpu s ∈ Cs) + (hW : CoreTree st T s (L.cpu s) W) + (hlocal : L.mem s (tdA t) ≠ 0#u64 → (L.mem s (thA t)).val >>> 8 = (L.cpu s).val) : + timer.insert st s t d bd ⦃ sFinal => TcbTimerStorePostA st T s sFinal + (fl4SetTcbTimer (tcbTimerQueueOfA st s W) (specPtr t) d.bv bd.bv) ⦄ := by + have hrem := tcbTimerRemove_postA s t hT ht hR hN hcpu hW hlocal + refine WP.spec_mono (tcbTimerInsert_orderA s t d bd hT ht hrem (fun _ h => h.localTree)) ?_ + rintro sFinal (⟨rfl, hRemoved⟩ | + ⟨hd, sRemoved, ctx, WFinal, hRemoved, hOld, hpre, hpost, hFinal, hdeadline, hbadge, hkeys, hprog⟩) + · exact tcbTimerCancel_postA (bd := bd) hRemoved + · exact tcbTimerInsert_postA hRemoved hOld hFinal hd hkeys hpre hpost hdeadline hbadge hprog + +end +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/TcbInvokeSetTimerEntryA.lean b/proofs/Fl4ProofsA64/P3/TcbInvokeSetTimerEntryA.lean new file mode 100644 index 000000000..6fd7f4db0 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbInvokeSetTimerEntryA.lean @@ -0,0 +1,44 @@ +/- +P3 (AArch64): connect the accepted SetTimer dispatch to the Store-only post. +[S] No effective stall and no removal from a remote CPU. +-/ +import Fl4ProofsA64.P3.TcbInvokeSetTimerA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4SpecA64 + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {T Cs : Set U64} + +attribute [local irreducible] tdA thA fl4SetTcbTimer + +set_option trace.profiler true in +/-- The accepted SetTimer invocation includes the caller's Restart and +the Store-only tree operation. The architectural state is the input state. -/ +theorem tcbSetTimer_queueA {A : Type} {ar : machine.Arch A} {s s1 : S} {a : A} + {t d bd : U64} {ctx : tcb.Ctx} {W : TRB.RBT} + (hst : smp.remote_tcb_stall st ar s a t = ok (s, a)) + (hlen : ¬ ctx.length < 2#u64) (hd : tcb.arg st s ctx 0#u64 = ok d) + (hb : tcb.arg st s ctx 1#u64 = ok bd) (hr : tcb.restart_caller st s = ok s1) + (hT : TcbPlaced T) (ht : t ∈ T) (hR : TimerRB st T Cs s1) + (hN : ∀ cpu ∈ Cs, NodePlaced st T s1 cpu) (hcpu : L.cpu s1 ∈ Cs) + (hW : CoreTree st T s1 (L.cpu s1) W) + (hlocal : L.mem s1 (tdA t) ≠ 0#u64 → (L.mem s1 (thA t)).val >>> 8 = (L.cpu s1).val) : + tcb.decode st ar s a fl4_abi.label.TCBSetTimer t ctx ⦃ r => + r.1 = .Ok () ∧ r.2.2 = a ∧ + TcbTimerStorePostA st T s1 r.2.1 + (fl4SetTcbTimer (tcbTimerQueueOfA st s1 W) (specPtr t) d.bv bd.bv) ⦄ := by + have run : tcb.decode st ar s a fl4_abi.label.TCBSetTimer t ctx = (do + let s2 ← timer.insert st s1 t d bd + ok (.Ok (), s2, a)) := by + rw [decode_set_timer ar hst hlen] + simp only [hd, hb, hr, bind_ok, Std.bind_tc_ok] + rw [run] + refine WP.spec_bind (tcbTimerInsert_queueA (T := T) (Cs := Cs) s1 t d bd hT ht hR hN hcpu hW hlocal) + fun s' h => ?_ + exact (WP.spec_ok _).2 ⟨rfl, rfl, h⟩ + +end +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/TcbInvokeSlotA.lean b/proofs/Fl4ProofsA64/P3/TcbInvokeSlotA.lean new file mode 100644 index 000000000..90f017f50 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbInvokeSlotA.lean @@ -0,0 +1,88 @@ +/- +P3 (AArch64): TCB slot replacement's post-delete object checks. +The frame comparison uses the shared Stage-3 cap projection. Rights and +mapping metadata can change while the underlying frame identity stays fixed. +-/ +import Fl4ProofsA64.P3.TcbInvokeDispatchA +import Fl4ProofsA64.P3.CapRelA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4SpecA64 Classical + +private theorem frameIdentity_wordA (p q rights rights' a a' v v' : U64) + (size size' : capability.FrameSize) (dev dev' mapped mapped' ept ept' : Bool) : + objects.same_object_as (.Frame p size rights dev mapped ept a v) + (.Frame q size' rights' dev' mapped' ept' a' v') = + ok (decide (p = q ∧ size = size' ∧ dev = dev')) := by + change (if p = q then (do + let sameSize ← capability.FrameSize.Insts.CoreCmpPartialEqFrameSize.eq size size' + if sameSize then ok (decide (dev = dev')) else ok false) else ok false) = _ + by_cases h : p = q + · rw [if_pos h] + cases size <;> cases size' <;> + simp [capability.FrameSize.Insts.CoreCmpPartialEqFrameSize.eq, read_discriminant, + capability.FrameSize.read_discriminant, h, bind_ok] + · rw [if_neg h] + simp [h] + +/-- The common frame identity test applies to native and Stage-2 caps, +including a native source being rechecked through a Stage-2 mapping. -/ +theorem tcbFrameObject_eqA (p q rights rights' a a' v v' : U64) + (size size' : capability.FrameSize) (dev dev' mapped mapped' ept ept' : Bool) : + objects.same_object_as (.Frame p size rights dev mapped ept a v) + (.Frame q size' rights' dev' mapped' ept' a' v') = + ok (decide (fl4TcbSameObject (absCapA (.Frame p size rights dev mapped ept a v)) + (absCapA (.Frame q size' rights' dev' mapped' ept' a' v')))) := by + rw [frameIdentity_wordA] + congr 1 + apply Bool.eq_iff_iff.2 + simp only [decide_eq_true_eq] + have hp : specPtr p = specPtr q ↔ p = q := ⟨specPtr_inj, congrArg specPtr⟩ + cases ept <;> cases ept' <;> cases size <;> cases size' <;> + simp [fl4TcbSameObject, fl4TcbFrame, absCapA, absFrameE, frameSizeOfA, ArchCap.isFrameCap, + ArchCap.acapObj, ArchCap.acapRights, ArchCap.acapFsize, ExtArchCap.objRef, archCapIsDevice, hp] + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} + +theorem tcbStillValid_nullA (s : S) (target slot src : U64) (newCap : capability.Cap) + (hsrc : obj.Slot.is_null src = ok true) : + tcb.still_valid st s target slot newCap src = ok false := by + simp only [tcb.still_valid, hsrc, bind_ok, ite_true] + +/-- The invoked Thread cap is read only after the live source passes its +object check. These reads take the state after deletion. -/ +theorem tcbStillValid_liveA (s : S) (target slot src : U64) (newCap live : capability.Cap) + (hsrc : obj.Slot.is_null src = ok false) (hcap : obj.Slot.cap st src s = ok live) : + tcb.still_valid st s target slot newCap src = (do + let good ← objects.same_object_as newCap live + if good then + let invoked ← obj.Slot.cap st slot s + objects.same_object_as (.Thread target) invoked + else ok false) := by + simp only [tcb.still_valid, hsrc, bind_ok, Bool.false_eq_true, ite_false, hcap] + +/-- Successful full deletion is sequenced before source/invoked-cap checks +and before insertion, even when the existing cap is null. -/ +theorem tcbInstallCap_afterDeleteA {s s1 : S} {a a1 : A} + (target slot index src root : U64) (newCap : capability.Cap) + (hroot : obj.Tcb.slot target index = ok root) + (hdel : cdt.delete st ar s a root true = ok (.Ok (), s1, a1)) : + tcb.install_tcb_cap st ar s a target slot index newCap src = (do + let nullCap ← capability.Cap.is_null newCap + if nullCap then ok (.Ok (), s1, a1) + else do + let same ← tcb.still_valid st s1 target slot newCap src + if same then + let s2 ← cdt.insert st s1 newCap src root + ok (.Ok (), s2, a1) + else ok (.Ok (), s1, a1)) := by + unfold tcb.install_tcb_cap + have hb : core.result.Result.Insts.CoreOpsTry.branch (.Ok () : core.result.Result Unit error.Exc) = + ok (.Continue ()) := rfl + simp only [hroot, bind_ok, hdel, hb, uncurry_apply_pair] + +end +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/TcbInvokeSlotFinalA.lean b/proofs/Fl4ProofsA64/P3/TcbInvokeSlotFinalA.lean new file mode 100644 index 000000000..effbbf8b8 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbInvokeSlotFinalA.lean @@ -0,0 +1,58 @@ +/- +P3 (AArch64): endpoint finalisation at TCB slot replacement. +[J] The operation is run on the finality decision from the current typed +slot and CDT relations. Each waiter has its live Reply correspondence. +[S] Waiters of a final endpoint are local at cancellation. +-/ +import Fl4ProofsA64.P3.TcbInvokeSlotA +import Fl4ProofsA64.P3.EpCancelAllA +import Fl4ProofsA64.P3.DelSpecA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4SpecA64 + +section +variable {S A : Type} {st : machine.Store S} [L : LawfulStore st] {ar : machine.Arch A} +variable {T E R : Set U64} {Bf : Set (U64 × Word)} {SR : S → State Unit → Prop} + +private theorem finaliseEndpoint_specA (p badge : U64) (send recv grant grantReply final : Bool) : + fl4FinaliseCap (Z := Unit) (absCapA (.Endpoint p badge send recv grant grantReply)) final = + Fl4Spec.NonDet.liftM (fun _ => (Cap.nullCap, Cap.nullCap)) + (Fl4Spec.NonDet.when final (cancelAllIpc (Z := Unit) (specPtr p))) := rfl + +/-- Both endpoint branches of the actual slot finaliser. The final branch +runs the local `cancelAllIpc` refinement, including live receive replies. -/ +theorem tcbSlotFinaliseEndpointA {s : S} {a : A} {σ : State Unit} {p badge : U64} + {send recv grant grantReply final : Bool} (hO : Endp.SchedOpsEA st T E R Bf SR) + (hP : Endp.EPlaced T E R Bf) (he : p ∈ E) + (hR : Endp.EpStateRel T E R Bf (L.mem s) σ) (hSR : SR s σ) + (hrep : final = true → ∀ z, σ.kheap (specPtr p) = some (.endpoint z) → + ∀ t ∈ z.epQueue, Endp.RepOKA σ t) + (hon : final = true → ∀ z, σ.kheap (specPtr p) = some (.endpoint z) → + ∀ t ∈ T, specPtr t ∈ z.epQueue → L.mem s (affA t) = L.cpu s) + (exposed : Bool) : + objects.finalise_cap st ar s a (.Endpoint p badge send recv grant grantReply) final exposed ⦃ r => + r.1 = (.Null, .Null) ∧ r.2.2 = a ∧ ∃ σ', + ((Cap.nullCap, Cap.nullCap), σ') ∈ + (fl4FinaliseCap (Z := Unit) (absCapA (.Endpoint p badge send recv grant grantReply)) final σ).1 ∧ + Endp.EpStateRel T E R Bf (L.mem r.2.1) σ' ∧ SR r.2.1 σ' ⦄ := by + cases final + · simp only [objects.finalise_cap, objects.finalise_cap_object, bind_ok, uncurry_apply_pair, + Bool.false_eq_true, ite_false, WP.spec_ok] + refine ⟨trivial, trivial, σ, ?_, hR, hSR⟩ + rw [finaliseEndpoint_specA] + exact mem_bind_ndD (x := ((), σ)) rfl rfl + · simp only [objects.finalise_cap, objects.finalise_cap_object, bind_ok, uncurry_apply_pair, ite_true] + refine WP.spec_bind (Endp.cancel_all_rel hO hP s σ hR hSR he (hrep rfl) (hon rfl)) ?_ + rintro s' ⟨_, σ', hm, hR', hSR'⟩ + refine (WP.spec_ok _).2 ⟨rfl, rfl, σ', ?_, hR', hSR'⟩ + rw [finaliseEndpoint_specA] + change ((Cap.nullCap, Cap.nullCap), σ') ∈ + ((cancelAllIpc (Z := Unit) (specPtr p) >>= fun _ => + Fl4Spec.NonDet.pure (Cap.nullCap, Cap.nullCap)) σ).1 + exact mem_bind_ndD hm rfl + +end +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/TcbInvokeSlotPostA.lean b/proofs/Fl4ProofsA64/P3/TcbInvokeSlotPostA.lean new file mode 100644 index 000000000..62889a7ef --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbInvokeSlotPostA.lean @@ -0,0 +1,105 @@ +/- +P3 (AArch64): the live insertion arm of replacement after full deletion. +[J] Full deletion supplies the exact Store, architecture and joint spec +successors. At that state both live checks succeed and the existing CDT +insertion premises hold. This arm propagates the actual updated CDT ghost. +-/ +import Fl4ProofsA64.P3.TcbInvokeSlotA +import Fl4ProofsA64.P3.InsDisA +import Fl4ProofsA64.P3.DelSpecA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4SpecA64 + +noncomputable section +open Classical + +/-- The live insertion certificate uses both caps from the state reached +by the full abstract delete. -/ +theorem tcbSlotInsert_specA {σ σDeleted σFinal : State Unit} {target : ObjRef} {index : Nat} + {invoked src : CslotPtr} {newCap current source : Cap} + (hdel : (.ok (), σDeleted) ∈ ((fl4CapDelete (Z := Unit) (target, tcbCnodeIndex index)).run σ).1) + (hnew : newCap ≠ .nullCap) + (hsource : (source, σDeleted) ∈ (getCap (Z := Unit) src σDeleted).1) + (hmatch : fl4TcbSameObject newCap source) + (hinvoked : (current, σDeleted) ∈ (getCap (Z := Unit) invoked σDeleted).1) + (htarget : fl4TcbSameObject (.threadCap target) current) + (hinsert : ((), σFinal) ∈ (capInsert (Z := Unit) newCap src (target, tcbCnodeIndex index) σDeleted).1) : + (.ok (), σFinal) ∈ ((fl4InstallTcbCap (Z := Unit) target invoked index (some (newCap, src))).run σ).1 := by + unfold fl4InstallTcbCap + refine mem_bindE_okD hdel ?_ + rw [Fl4Spec.NonDetE.unlessE, if_neg hnew] + apply mem_liftED + change ((), σFinal) ∈ ((getCap (Z := Unit) src >>= fun live => + Fl4Spec.NonDet.when (fl4TcbSameObject newCap live) (getCap (Z := Unit) invoked >>= fun cap => + Fl4Spec.NonDet.when (fl4TcbSameObject (.threadCap target) cap) + (capInsert (Z := Unit) newCap src (target, tcbCnodeIndex index)))) σDeleted).1 + refine mem_bind_ndD hsource ?_ + rw [Fl4Spec.NonDet.when, if_pos hmatch] + refine mem_bind_ndD hinvoked ?_ + rw [Fl4Spec.NonDet.when, if_pos htarget] + exact hinsert + +section +variable {S A : Type} {st : machine.Store S} [L : LawfulStore st] {ar : machine.Arch A} + +/-- A real CDT insertion at the post-delete state establishes the updated +joint entry relation and the abstract installed slot. The rule uses the +CDT ghost computed by the existing insertion theorem. -/ +theorem tcbSlotInsert_afterDeleteA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} + {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} {ia ie : Nat} + {s0 sDeleted : S} {a0 aDeleted : A} {σ σDeleted : State Unit} {m : McState} + (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie sDeleted aDeleted σDeleted m) + (hT : TreeA st OS Z.G.chains Z.G.par sDeleted) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st sDeleted y = .Null) + {t indexWord : U64} {index : Nat} {invoked src : CslotPtr} + {nc : capability.Cap} {current source : Cap} + (hroot : obj.Tcb.slot t indexWord = ok (cslotAddrA (specPtr t, tcbCnodeIndex index))) + (hdel : cdt.delete st ar s0 a0 (cslotAddrA (specPtr t, tcbCnodeIndex index)) true = ok (.Ok (), sDeleted, aDeleted)) + (hdelSpec : (.ok (), σDeleted) ∈ ((fl4CapDelete (Z := Unit) (specPtr t, tcbCnodeIndex index)).run σ).1) + (hnull : capability.Cap.is_null nc = ok false) (hnSpec : absCapA nc ≠ .nullCap) + (hcheck : tcb.still_valid st sDeleted t (cslotAddrA invoked) nc (cslotAddrA src) = ok true) + (hsource : (source, σDeleted) ∈ (getCap (Z := Unit) src σDeleted).1) + (hmatch : fl4TcbSameObject (absCapA nc) source) + (hinvoked : (current, σDeleted) ∈ (getCap (Z := Unit) invoked σDeleted).1) + (htarget : fl4TcbSameObject (.threadCap (specPtr t)) current) + (hAs : AbsSlotA Z.th src) (hAd : AbsSlotA Z.th (specPtr t, tcbCnodeIndex index)) + (hsn : capAtA (L.mem sDeleted) (cslotAddrA src) ≠ .Null) + (hdn : capAtA (L.mem sDeleted) (cslotAddrA (specPtr t, tcbCnodeIndex index)) = .Null) + (hdc : obj.sext48 (cslotAddrA (specPtr t, tcbCnodeIndex index)) = + ok (cslotAddrA (specPtr t, tcbCnodeIndex index))) + (hwf : CapWfA nc) (hw : CapRt.EncWf nc) (hn : nc ≠ .Null) + (hal : UA nc (capA st sDeleted (cslotAddrA src))) + (hk : SameBadgeKindA nc (capA st sDeleted (cslotAddrA src))) + (hut : isUtA (capA st sDeleted (cslotAddrA src)) = true → + ∀ y, ¬ Fl4Proofs.P3.Mdb.Desc Z.G.par y (cslotAddrA src)) + (hsh : shapeA nc = shapeA (capA st sDeleted (cslotAddrA src))) + (hz : ∀ p t n, capA st sDeleted (cslotAddrA src) ≠ .Zombie p t n) + (hbd : ∀ r b rs, (absCapA (capA st sDeleted (cslotAddrA src)) = .endpointCap r b rs ∨ + absCapA (capA st sDeleted (cslotAddrA src)) = .notificationCap r b rs) → b ≠ 0 → + capEpBadge (absCapA nc) = b) + (hdo : ¬ IrqNodeA (L.ksb sDeleted) (fl4Ptr (specPtr t)) ∨ IrqCapA (absCapA nc)) + (hcv : CapValidA Z.th (capA st sDeleted (cslotAddrA src)) → CapValidA Z.th nc) : + tcb.install_tcb_cap st ar s0 a0 t (cslotAddrA invoked) indexWord nc (cslotAddrA src) ⦃ r => + r.1 = .Ok () ∧ r.2.2 = aDeleted ∧ ∃ σFinal, + (.ok (), σFinal) ∈ ((fl4InstallTcbCap (Z := Unit) (specPtr t) invoked index + (some (absCapA nc, src))).run σ).1 ∧ + let dest := cslotAddrA (specPtr t, tcbCnodeIndex index) + let parent := insBA σDeleted src (absCapA nc) (absCapA (capA st sDeleted (cslotAddrA src))) + JAtZAX (L := L) AR { Z with G := insGA Z.G (cslotAddrA src) dest parent } r.2.1 aDeleted σFinal m ∧ + TreeA st OS (insG (cslotAddrA src) dest Z.G.chains) (insGA Z.G (cslotAddrA src) dest parent).par r.2.1 ∧ + ∀ n, SInvXA σDeleted m n → SInvXA σFinal m n ⦄ := by + rw [tcbInstallCap_afterDeleteA t (cslotAddrA invoked) indexWord (cslotAddrA src) + (cslotAddrA (specPtr t, tcbCnodeIndex index)) nc hroot hdel] + simp only [hnull, bind_ok, Bool.false_eq_true, ite_false, hcheck, ite_true] + refine WP.spec_bind (cap_insert_dA E hT hsub hnul hAs hAd hsn hdn hdc hwf (CapEqA.of_abs rfl) hw hn hal hk hut + hsh hz hbd hdo hcv) ?_ + rintro sFinal ⟨σFinal, hinsert, hJ, htree, hinv⟩ + exact (WP.spec_ok _).2 ⟨rfl, rfl, σFinal, + tcbSlotInsert_specA hdelSpec hnSpec hsource hmatch hinvoked htarget hinsert, hJ, htree, hinv⟩ + +end +end +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/TcbInvokeThreadFieldsA.lean b/proofs/Fl4ProofsA64/P3/TcbInvokeThreadFieldsA.lean new file mode 100644 index 000000000..dcc61f1b7 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbInvokeThreadFieldsA.lean @@ -0,0 +1,94 @@ +/- +P3 (AArch64): TLS and priority word operations at the TCB dispatch. +[S] The dispatch stall leaves the Store and Arch states unchanged. +-/ +import Fl4ProofsA64.P3.TcbInvokeRegsA +import Fl4ProofsA64.P3.ScYieldRelA +import Fl4ProofsA64.P1.VcpuHooks + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4SpecA64 Sched + +section +variable {S A : Type} {st : machine.Store S} [L : LawfulStore st] {ar : machine.Arch A} +variable {s s1 : S} {a : A} {t p auth argSlot : U64} {ctx : tcb.Ctx} + +theorem tcbSetTLSBase_truncatedA (hst : smp.remote_tcb_stall st ar s a t = ok (s, a)) + (hlen : ctx.length < 1#u64) : + tcb.decode st ar s a fl4_abi.label.TCBSetTLSBase t ctx = + ok (.Err (.Sys .TruncatedMessage), s, a) := by + simp only [tcbSetTLSBase_programA hst, tcb.decode_set_tls_base, if_pos hlen, error.truncated, + bind_ok, uncurry_apply_pair] + +/-- SetTLSBase writes the raw argument to TPIDR_EL0 and reschedules precisely +when the target is the current TCB after the register write. -/ +theorem tcbSetTLSBase_postA (hst : smp.remote_tcb_stall st ar s a t = ok (s, a)) + (hlen : ¬ctx.length < 1#u64) (hp : tcb.arg st s ctx 0#u64 = ok p) + (hr : tcb.restart_caller st s = ok s1) : + tcb.decode st ar s a fl4_abi.label.TCBSetTLSBase t ctx = (do + let s2 := L.wr s1 (Endp.regA t layout.reg.TLS_BASE) p + let s3 ← if t = L.curW s2 then sched.reschedule_required st s2 else ok s2 + ok (.Ok (), s3, a)) := by + have htls : layout.reg.TLS_BASE ≠ layout.reg.SPSR_EL1 := by + simp [layout.reg.TLS_BASE, layout.reg.TPIDR_EL0, layout.reg.SPSR_EL1] + rw [tcbSetTLSBase_programA hst] + simp only [tcb.decode_set_tls_base, if_neg hlen, hp, hr, bind_ok, regs.sanitise, if_neg htls, + Endp.set_reg_eqA, cur_thread_ok, obj.Tcb.Insts.CoreCmpPartialEqTcb.eq] + by_cases h : t = L.curW (L.wr s1 (Endp.regA t layout.reg.TLS_BASE) p) + · have hb := decide_eq_true h + simp only [hb, ite_true, if_pos h, bind_ok, Std.bind_assoc, uncurry_apply_pair] + · have hb := decide_eq_false h + simp only [hb, Bool.false_eq_true, ite_false, if_neg h, bind_ok, uncurry_apply_pair] + +private theorem setMcp_wordA (s : S) (t p : U64) : + obj.Tcb.set_mcp st t s p = ok (L.wr s (mcpA t) p) := by + unfold obj.Tcb.set_mcp + rw [eq_of_spec_fd Fl4ProofsA64.P0.Consts.layout.tcb.MCP.spec, bind_ok] + simp only [lift, bind_ok] + exact L.write_eq s (mcpA t) p + +theorem tcbSetMCPriority_postA (hst : smp.remote_tcb_stall st ar s a t = ok (s, a)) + (he : tcb.extra_with_length ctx 0#usize 1#u64 = ok (.Ok argSlot)) + (hp : tcb.arg st s ctx 0#u64 = ok p) + (ha : tcb.thread_arg st s argSlot 1#u64 = ok (.Ok auth)) + (hle : ¬L.mem s (mcpA auth) < p) (hr : tcb.restart_caller st s = ok s1) : + tcb.decode st ar s a fl4_abi.label.TCBSetMCPriority t ctx = + ok (.Ok (), L.wr s1 (mcpA t) p, a) := by + rw [tcbSetMCPriority_programA hst] + simp only [tcb.decode_set_priority, he, hp, ha, tcb.check_prio, Fl4ProofsA64.P3.Sched.obj.Tcb.mcp.specA, hle, ite_false, + core.result.Result.Insts.CoreOpsTry.branch, bind_ok, hr, Bool.false_eq_true, + tcb.thread_control_sched, lift, tcb.SCHED_UPDATE_FAULT, tcb.SCHED_UPDATE_MCP, + tcb.SCHED_UPDATE_PRIO, tcb.SCHED_UPDATE_SC, setMcp_wordA] <;> simp + +/-- Both priority labels use the authority TCB's raw MCP as the upper bound +and return the exact FL4 range error before Restart if validation fails. -/ +theorem tcbPriority_rangeA (prio : Bool) + (he : tcb.extra_with_length ctx 0#usize 1#u64 = ok (.Ok argSlot)) + (hp : tcb.arg st s ctx 0#u64 = ok p) + (ha : tcb.thread_arg st s argSlot 1#u64 = ok (.Ok auth)) + (hlt : L.mem s (mcpA auth) < p) : + tcb.decode_set_priority st ar s a t ctx prio = + ok (.Err (.Sys (.RangeError 0#u64 (L.mem s (mcpA auth)))), s, a) := by + simp only [tcb.decode_set_priority, he, hp, ha, tcb.check_prio, Fl4ProofsA64.P3.Sched.obj.Tcb.mcp.specA, + if_pos hlt, error.range, core.result.Result.Insts.CoreOpsTry.branch, bind_ok, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, Std.bind_assoc, Std.bind_tc_ok] + +theorem tcbSetPriority_postA (hst : smp.remote_tcb_stall st ar s a t = ok (s, a)) + (he : tcb.extra_with_length ctx 0#usize 1#u64 = ok (.Ok argSlot)) + (hp : tcb.arg st s ctx 0#u64 = ok p) + (ha : tcb.thread_arg st s argSlot 1#u64 = ok (.Ok auth)) + (hle : ¬L.mem s (mcpA auth) < p) (hr : tcb.restart_caller st s = ok s1) : + tcb.decode st ar s a fl4_abi.label.TCBSetPriority t ctx = (do + let s2 ← sched.set_priority st s1 t p + ok (.Ok (), s2, a)) := by + rw [tcbSetPriority_programA hst] + simp only [tcb.decode_set_priority, he, hp, ha, tcb.check_prio, Fl4ProofsA64.P3.Sched.obj.Tcb.mcp.specA, hle, ite_false, + core.result.Result.Insts.CoreOpsTry.branch, bind_ok, hr, ite_true, + tcb.thread_control_sched, lift, tcb.SCHED_UPDATE_FAULT, tcb.SCHED_UPDATE_MCP, + tcb.SCHED_UPDATE_PRIO, tcb.SCHED_UPDATE_SC] <;> simp + +end +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/TcbInvokeTimeoutA.lean b/proofs/Fl4ProofsA64/P3/TcbInvokeTimeoutA.lean new file mode 100644 index 000000000..e99309f9f --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbInvokeTimeoutA.lean @@ -0,0 +1,129 @@ +/- +P3 (AArch64): SetTimeoutEndpoint and the shared fault-handler cap check. +[S] Target stall is quiet. +[J] The typed extra cap and Restart are observed at decode. The slot-4 +installation relation is supplied at the post-Restart state. +-/ +import Fl4ProofsA64.P3.TcbInvokeSlotPostA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4SpecA64 + +private theorem timeoutRightsA (send recv grant grantReply : Bool) : + hasHandlerRights (.endpointCap p b (rightsOfA send recv grant grantReply)) ↔ + send = true ∧ (grant = true ∨ grantReply = true) := by + unfold hasHandlerRights + rw [← Set.nonempty_iff_ne_empty] + change (send = true ∧ ({Rights.allowGrant, .allowGrantReply} ∩ + rightsOfA send recv grant grantReply).Nonempty) ↔ _ + constructor + · rintro ⟨hs, r, hr, hrights⟩ + simp only [Set.mem_insert_iff, Set.mem_singleton_iff] at hr + rcases hr with (rfl | rfl) + · exact ⟨hs, Or.inl hrights⟩ + · exact ⟨hs, Or.inr hrights⟩ + · rintro ⟨hs, hg | hgr⟩ + · exact ⟨hs, .allowGrant, Set.mem_insert _ _, hg⟩ + · exact ⟨hs, .allowGrantReply, Set.mem_insert_of_mem _ rfl, hgr⟩ + +set_option trace.profiler true in +/-- Supported capabilities give the same result in both fault-handler checks. -/ +theorem tcbValidFaultHandlerA (c : capability.Cap) (hw : CapWfA c) : + faults.valid_fault_handler c = ok true ↔ validFaultHandler (absCapA c) := by + have hne : (ok false : Result Bool) ≠ ok true := fun h => Bool.noConfusion (ok_inj_ff h) + cases c <;> try exact hw.elim + all_goals simp only [faults.valid_fault_handler, validFaultHandler, absCapA, isEpCap, + Bool.false_eq_true, false_and, false_or, true_or, true_and, reduceCtorEq, hne, iff_self] + case Endpoint p b send recv grant grantReply => + rw [timeoutRightsA] + cases send <;> cases grant <;> cases grantReply <;> + simp only [Bool.false_eq_true, ite_true, ite_false, eq_self, hne, true_and, + false_and, true_or, false_or, or_false, iff_self] + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} +variable {s sRestart : S} {a : A} {t src : U64} {ctx : tcb.Ctx} {cap : capability.Cap} + +theorem tcbCapControl_returnA (r : core.result.Result Unit error.Exc) (sFinal : S) (aFinal : A) : + (do + let cf ← core.result.Result.Insts.CoreOpsTry.branch r + match cf with + | .Continue _ => ok (core.result.Result.Ok (), sFinal, aFinal) + | .Break residual => + let r' ← core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual + Unit (core.convert.FromSame error.Exc) residual + ok (r', sFinal, aFinal)) = ok (r, sFinal, aFinal) := by + cases r with + | Ok u => + cases u + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok] + | Err e => + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok, Std.bind_tc_ok, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from] + +set_option trace.profiler true in +theorem tcbSetTimeoutEndpoint_invalidA + (hStall : smp.remote_tcb_stall st ar s a t = ok (s, a)) + (hExtra : tcb.extra_with_length ctx 0#usize 0#u64 = ok (.Ok src)) + (hCap : obj.Slot.cap st src s = ok cap) + (hValid : faults.valid_fault_handler cap = ok false) : + tcb.decode st ar s a fl4_abi.label.TCBSetTimeoutEndpoint t ctx = + ok (.Err (.Sys (.InvalidCapability 1#u64)), s, a) := by + rw [tcbSetTimeoutEndpoint_programA hStall] + simp only [tcb.decode_set_timeout_endpoint, hExtra, hCap, hValid, bind_ok, + core.result.Result.Insts.CoreOpsTry.branch, Bool.false_eq_true, ite_false, error.invalid_cap] + +set_option trace.profiler true in +/-- Acceptance exposes the actual timeout slot installation after Restart. -/ +theorem tcbSetTimeoutEndpoint_selectedA + (hStall : smp.remote_tcb_stall st ar s a t = ok (s, a)) + (hExtra : tcb.extra_with_length ctx 0#usize 0#u64 = ok (.Ok src)) + (hCap : obj.Slot.cap st src s = ok cap) + (hValid : faults.valid_fault_handler cap = ok true) + (hRestart : tcb.restart_caller st s = ok sRestart) : + tcb.decode st ar s a fl4_abi.label.TCBSetTimeoutEndpoint t ctx = (do + let (r, sFinal, aFinal) ← tcb.install_tcb_cap st ar sRestart a t ctx.slot + layout.tcb.SLOT_TIMEOUT_HANDLER cap src + ok (r, sFinal, aFinal)) := by + rw [tcbSetTimeoutEndpoint_programA hStall] + simp only [tcb.decode_set_timeout_endpoint, hExtra, hCap, hValid, hRestart, bind_ok, + core.result.Result.Insts.CoreOpsTry.branch, ite_true] + have hn0 : ((tcb.UPDATE_TIMEOUT &&& tcb.UPDATE_FAULT) != 0#u64) = false := by + rw [tcb.UPDATE_TIMEOUT, tcb.UPDATE_FAULT]; decide + have hn1 : ((tcb.UPDATE_TIMEOUT &&& tcb.UPDATE_SPACE) != 0#u64) = false := by + rw [tcb.UPDATE_TIMEOUT, tcb.UPDATE_SPACE]; decide + have hn2 : ((tcb.UPDATE_TIMEOUT &&& tcb.UPDATE_IPC_BUFFER) != 0#u64) = false := by + rw [tcb.UPDATE_TIMEOUT, tcb.UPDATE_IPC_BUFFER]; decide + have ht : ((tcb.UPDATE_TIMEOUT &&& tcb.UPDATE_TIMEOUT) != 0#u64) = true := by + rw [tcb.UPDATE_TIMEOUT]; decide + simp only [tcb.thread_control_caps, lift, bind_ok, hn0, hn1, hn2, ht, + Bool.false_eq_true, ite_false, ite_true] + change Aeneas.Std.bind (tcb.install_tcb_cap st ar sRestart a t ctx.slot + layout.tcb.SLOT_TIMEOUT_HANDLER cap src) _ = Aeneas.Std.bind _ _ + congr 1 + funext r + rcases r with ⟨r, sFinal, aFinal⟩ + exact tcbCapControl_returnA r sFinal aFinal + +end + +set_option trace.profiler true in +/-- The slot-4 outcome is an outcome of the actual abstract TCB invocation. -/ +theorem tcbSetTimeoutEndpoint_specPostA {σRestart σFinal : State Unit} + {t : ObjRef} {slot src : CslotPtr} {cap : Cap} + (hInstall : (.ok (), σFinal) ∈ ((fl4InstallTcbCap t slot 4 (some (cap, src))).run σRestart).1) : + (.ok [], σFinal) ∈ + ((fl4InvokeTcb (.threadControlCaps t slot none (some (cap, src)) none none none)).run σRestart).1 := by + have hSkip (n : Nat) : fl4InstallTcbCap (Z := Unit) t slot n none = Fl4Spec.NonDetE.returnOk () := rfl + have hFrame : fl4InstallTcbFrameCap (Z := Unit) t slot none = Fl4Spec.NonDetE.returnOk () := rfl + simp only [fl4InvokeTcb, hSkip, hFrame] + rw [run_bind_okD (run_returnOkD () σRestart)] + refine mem_bindE_okD hInstall ?_ + rw [run_bind_okD (run_returnOkD () σFinal), run_bind_okD (run_returnOkD () σFinal), + run_bind_okD (run_returnOkD () σFinal)] + rfl + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/TcbInvokeTimerA.lean b/proofs/Fl4ProofsA64/P3/TcbInvokeTimerA.lean new file mode 100644 index 000000000..f40faea9f --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbInvokeTimerA.lean @@ -0,0 +1,343 @@ +/- +P3 (AArch64): SetTimer's Store order and earliest-deadline flag. +[S] An armed target being replaced is owned by the current CPU. The +Store-only attach lemmas reuse the P1 red-black tree and its descent. +The resulting ordered projection is the input for the MCS timer step. +-/ +import Fl4ProofsA64.P3.TcbInvokeDispatchA +import Fl4ProofsA64.P1.QueueTimerSet +import Fl4ProofsA64.P3.TcbInvokeTimerAttachA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4SpecA64 + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {T : Set U64} + +private theorem attachReprogramA {s1 s8 : S} {c t d : U64} {ctx : TRB.Ctx} + (hm : InsMid st T s1 s8 c t d ctx) (hN : NodePlaced st T s1 c) (hT : TcbPlaced T) : + L.mem s8 (L.nodeAt s8 c layout.node.REPROGRAM) = + L.mem s1 (L.nodeAt s1 c layout.node.REPROGRAM) := by + rw [nodeAt_of_ksb hm.wi.1] + refine hm.wi.mem_eq (nodeAt_aligned hN (by simp) (by simp)) ?_ + have hpar : Tz T ctx.par := by + obtain ⟨-, -, mpk, -, -, -, -⟩ := TRB.mid_facts hm.old.rep hm.old.nodup + by_cases hh : ctx = .top + · subst ctx; exact Tz.zero + · exact Tz.of_mem (hm.old.inT _ (mpk hh)) + rintro (⟨K, ha, hb, e⟩ | e | e | e) + · exact node_ne_tz hT hN (by simp) (Tz.of_mem hm.tT) (by omega) (congrArg UScalar.val e) + · exact node_ne_tz hT hN (by simp) hpar (K := 3544#u64) (by simp) (congrArg UScalar.val e) + · exact node_ne_tz hT hN (by simp) hpar (K := 3552#u64) (by simp) (congrArg UScalar.val e) + · exact nodeAt_ne hN (by simp) (by rw [timer_root_val]; simp) + (by rw [timer_root_val]; simp) e + +private theorem fixupReprogramA {s8 s9 : S} {c : U64} {keys : List U64} + (w : WritesIn st s8 s9 (FixFoot st s8 c keys)) (hT : TcbPlaced T) (hN : NodePlaced st T s8 c) + (hin : ∀ y ∈ keys, y ∈ T) : + L.mem s9 (L.nodeAt s9 c layout.node.REPROGRAM) = + L.mem s8 (L.nodeAt s8 c layout.node.REPROGRAM) := by + rw [nodeAt_of_ksb w.1] + refine w.mem_eq (nodeAt_aligned hN (by simp) (by simp)) ?_ + rintro (⟨y, hy, e | e | e | e⟩ | e) + · exact node_ne_tz hT hN (by simp) (hy.imp_right (hin y)) (K := 3544#u64) (by simp) (congrArg UScalar.val e) + · exact node_ne_tz hT hN (by simp) (hy.imp_right (hin y)) (K := 3552#u64) (by simp) (congrArg UScalar.val e) + · exact node_ne_tz hT hN (by simp) (hy.imp_right (hin y)) (K := 3560#u64) (by simp) (congrArg UScalar.val e) + · exact node_ne_tz hT hN (by simp) (hy.imp_right (hin y)) (K := 3568#u64) (by simp) (congrArg UScalar.val e) + · exact nodeAt_ne hN (by simp) (by rw [timer_root_val]; simp) (by rw [timer_root_val]; simp) e + +private theorem fixupBadgeA {s0 s : S} {c y : U64} {keys : List U64} + (hT : TcbPlaced T) (hk : ∀ u ∈ keys, u ∈ T) (hN : NodePlaced st T s0 c) + (w : WritesIn st s0 s (FixFoot st s0 c keys)) (hy : y ∈ T) : + L.mem s (tbA y) = L.mem s0 (tbA y) := by + refine w.mem_eq (tz_aligned hT (Tz.of_mem hy) (K := 3536#u64) (by simp) (by simp)) ?_ + rintro (⟨z, hz, e | e | e | e⟩ | e) + · exact tz_ne hT (Tz.of_mem hy) (hz.imp_right (hk z)) (K1 := 3536#u64) (K2 := 3544#u64) + (by simp) (by simp) (by simp) (congrArg UScalar.val e) + · exact tz_ne hT (Tz.of_mem hy) (hz.imp_right (hk z)) (K1 := 3536#u64) (K2 := 3552#u64) + (by simp) (by simp) (by simp) (congrArg UScalar.val e) + · exact tz_ne hT (Tz.of_mem hy) (hz.imp_right (hk z)) (K1 := 3536#u64) (K2 := 3560#u64) + (by simp) (by simp) (by simp) (congrArg UScalar.val e) + · exact tz_ne hT (Tz.of_mem hy) (hz.imp_right (hk z)) (K1 := 3536#u64) (K2 := 3568#u64) + (by simp) (by simp) (by simp) (congrArg UScalar.val e) + · exact node_ne_tz hT hN (by rw [timer_root_val]; simp) (Tz.of_mem hy) (K := 3536#u64) + (by simp) (congrArg UScalar.val e).symm + +/-- The Store's update of REPROGRAM is exactly at an empty prefix of the +remaining tree. Prefix deadlines include ties, suffix deadlines are strict. -/ +theorem tcbTimerInsert_orderA (s : S) (t d bd : U64) {P : S → Prop} + (hT : TcbPlaced T) (ht : t ∈ T) + (hrem : timer.remove st s t ⦃ s1 => P s1 ⦄) + (hP : ∀ s1, P s1 → NodePlaced st T s1 (L.cpu s1) ∧ + ∃ W, CoreTree st T s1 (L.cpu s1) W ∧ t ∉ W.keys) : + timer.insert st s t d bd ⦃ s' => + (d = 0#u64 ∧ P s') ∨ + (d ≠ 0#u64 ∧ ∃ (s1 : S) (ctx : TRB.Ctx) (W : TRB.RBT), P s1 ∧ + CoreTree st T s1 (L.cpu s1) (ctx.plug .leaf) ∧ + (∀ y ∈ ctx.pre, (L.mem s1 (tdA y)).val ≤ d.val) ∧ + (∀ y ∈ ctx.post, d.val < (L.mem s1 (tdA y)).val) ∧ + CoreTree st T s' (L.cpu s1) W ∧ + (∀ y ∈ W.keys, L.mem s' (tdA y) = if y = t then d else L.mem s1 (tdA y)) ∧ + (∀ y ∈ W.keys, L.mem s' (tbA y) = if y = t then bd else L.mem s1 (tbA y)) ∧ + W.keys = ctx.pre ++ t :: ctx.post ∧ + L.mem s' (L.nodeAt s' (L.cpu s1) layout.node.REPROGRAM) = + (if ctx.pre = [] then 1#u64 else L.mem s1 (L.nodeAt s1 (L.cpu s1) layout.node.REPROGRAM))) ⦄ := by + let Q (s8 s9 : S) := + ∃ W0 W n c, TRB.Rep (lk st s8) 0#u64 (L.mem s8 (L.nodeAt s8 c layout.node.TIMER_ROOT)) W0 ∧ + FB st T s8 c W0.keys s9 W ∧ TRB.Bal W n ∧ W.isRed = false ∧ c = L.cpu s8 + have hfix : ∀ s1 s8 ctx, P s1 → InsMid st T s1 s8 (L.cpu s1) t d ctx → L.mem s8 (tbA t) = bd → + timer.insert_fixup st s8 (L.cpu s1) t ⦃ s9 => Q s8 s9 ⦄ := by + intro s1 s8 ctx _ hm _ + obtain ⟨n, hb⟩ := hm.old.bal + refine WP.spec_mono (timer.insert_fixup.rb s8 (L.cpu s1) t hT hm.np hm.rep hb hm.old.black + hm.nodup hm.inT) fun s9 ⟨W, k, hF, hbal, hblack⟩ => ?_ + exact ⟨_, W, k, L.cpu s1, hm.rep, hF, hbal, hblack, hm.wi.2.1.symm⟩ + refine WP.spec_mono (tcbTimerInsert_attachBadgeA s t d bd hT ht hrem hP hfix) fun s' hs => ?_ + rcases hs with h0 | ⟨hd, s1, s8, ctx, s9, hs1, hm, hbd, ⟨W0, W, n, c, hr, hF, hb, hblack, hc⟩, hfirst, hlater⟩ + · exact Or.inl h0 + rw [hm.wi.2.1] at hc + subst c + have e := TRB.Rep.det hm.rep hr + subst e + have hkeys : W.keys = ctx.pre ++ t :: ctx.post := by rw [hF.keys, TRB.attach_keys] + have hN := (hP s1 hs1).1 + have hrep := (fixupReprogramA hF.wi hT hm.np hm.inT).trans (attachReprogramA hm hN hT) + have hmin := (hF.min hT hm.inT hm.np).trans (hm.min_old hT) + have hm0 : layout.node.TIMER_MIN.val < 4288 := by rw [timer_min_val]; decide + have hm8 : layout.node.TIMER_MIN.val % 8 = 0 := by rw [timer_min_val] + have hr0 : layout.node.TIMER_ROOT.val < 4288 := by rw [timer_root_val]; decide + have hr8 : layout.node.TIMER_ROOT.val % 8 = 0 := by rw [timer_root_val] + have emk : W.minKey = (ctx.plug (.node true .leaf t .leaf)).minKey := by + unfold TRB.RBT.minKey + rw [hF.keys] + have htd (y : U64) (hy : y ∈ W.keys) : + L.mem s9 (tdA y) = if y = t then d else L.mem s1 (tdA y) := by + have hyT := hm.inT y (hF.keys ▸ hy) + rw [wi_fix_td hT hm.inT hm.np hF.wi hyT] + by_cases he : y = t + · subst y; rw [if_pos rfl]; exact hm.td + · rw [if_neg he] + exact hm.tdo y hyT he + have htb (y : U64) (hy : y ∈ W.keys) : + L.mem s9 (tbA y) = if y = t then bd else L.mem s1 (tbA y) := by + have hyT := hm.inT y (hF.keys ▸ hy) + rw [fixupBadgeA hT hm.inT hm.np hF.wi hyT] + by_cases he : y = t + · subst y; rw [if_pos rfl]; exact hbd + · rw [if_neg he] + have hpar : Tz T ctx.par := by + obtain ⟨-, -, mpk, -, -, -, -⟩ := TRB.mid_facts hm.old.rep hm.old.nodup + by_cases h0 : ctx = .top + · subst ctx; exact Tz.zero + · exact Tz.of_mem (hm.old.inT _ (mpk h0)) + exact hm.wi.mem_eq (field_aligned (hT y hyT) (K := 3536#u64) (by simp) (by simp)) + (insFoot_not (K' := 3536#u64) hT hN ht hpar hyT (by simp) he (Or.inr ⟨by simp, by simp⟩)) + refine Or.inr ⟨hd, s1, ctx, W, hs1, hm.old, hm.pre, hm.post, ?_, ?_, ?_, hkeys, ?_⟩ + · by_cases hp : ctx.pre = [] + · rw [hfirst hp] + refine CoreTree.of_fix hT hm hd hF hb hblack (fun K hK h8 y hy => ?_) ?_ ?_ + · rw [trb_fld_wr_node hT hF.np.wr (by simp) (by simp) hy hK h8, + trb_fld_wr_node hT hF.np hm0 hm8 hy hK h8] + · rw [node_wr_ne hF.np.wr (by simp) (by simp) hr0 hr8 (by rw [timer_root_val]; simp), + node_wr_ne hF.np hm0 hm8 hr0 hr8 (by rw [timer_min_val, timer_root_val]; simp)] + · rw [node_wr_ne hF.np.wr (by simp) (by simp) hm0 hm8 (by rw [timer_min_val]; simp), + node_wr_eq hF.np hm0 hm8, emk, TRB.attach_min, if_pos hp] + · rw [hlater hp] + refine CoreTree.of_fix hT hm hd hF hb hblack (fun _ _ _ _ _ => rfl) rfl ?_ + rw [hmin, hm.old.min, emk, TRB.attach_min, if_neg hp] + · intro y hy + by_cases hp : ctx.pre = [] + · have hyT := hm.inT y (hF.keys ▸ hy) + rw [hfirst hp, show tdA y = core.num.U64.wrapping_add y 3528#u64 from rfl, + trb_fld_wr_node hT hF.np.wr (by simp) (by simp) hyT (by simp) (by simp), + trb_fld_wr_node hT hF.np hm0 hm8 hyT (by simp) (by simp)] + exact htd y hy + · rw [hlater hp] + exact htd y hy + · intro y hy + by_cases hp : ctx.pre = [] + · have hyT := hm.inT y (hF.keys ▸ hy) + rw [hfirst hp, show tbA y = core.num.U64.wrapping_add y 3536#u64 from rfl, + trb_fld_wr_node hT hF.np.wr (by simp) (by simp) hyT (by simp) (by simp), + trb_fld_wr_node hT hF.np hm0 hm8 hyT (by simp) (by simp)] + exact htb y hy + · rw [hlater hp] + exact htb y hy + · by_cases hp : ctx.pre = [] + · rw [hfirst hp, if_pos hp] + exact node_wr_eq (o := layout.node.REPROGRAM) (v := 1#u64) hF.np.wr (by simp) (by simp) + · simpa only [hlater hp, if_neg hp] using hrep + +/-- Removing the old local timer erases its key in order and leaves the +retained raw deadline, badge and REPROGRAM words intact. -/ +theorem tcbTimerRemove_orderA {Cs : Set U64} (s : S) (t : U64) {W : TRB.RBT} + (hT : TcbPlaced T) (ht : t ∈ T) (hR : TimerRB st T Cs s) + (hN : ∀ c ∈ Cs, NodePlaced st T s c) (hcpu : L.cpu s ∈ Cs) + (hW : CoreTree st T s (L.cpu s) W) + (hlocal : L.mem s (tdA t) ≠ 0#u64 → (L.mem s (thA t)).val >>> 8 = (L.cpu s).val) : + timer.remove st s t ⦃ s' => + L.cpu s' = L.cpu s ∧ L.mem s' (tdA t) = 0#u64 ∧ NodePlaced st T s' (L.cpu s) ∧ + ∃ W', CoreTree st T s' (L.cpu s) W' ∧ W'.keys = W.keys.filter (· != t) ∧ + (∀ y ∈ W'.keys, L.mem s' (tdA y) = L.mem s (tdA y) ∧ L.mem s' (tbA y) = L.mem s (tbA y)) ∧ + L.mem s' (L.nodeAt s' (L.cpu s) layout.node.REPROGRAM) = + L.mem s (L.nodeAt s (L.cpu s) layout.node.REPROGRAM) ⦄ := by + have hoM : layout.node.TIMER_MIN.val < 4288 := by rw [timer_min_val]; simp + have hoR : layout.node.TIMER_ROOT.val < 4288 := by rw [timer_root_val]; simp + have hoR8 : layout.node.TIMER_ROOT.val % 8 = 0 := by rw [timer_root_val] + refine WP.spec_mono (WP.spec_and (timer.remove.rb s t hT ht hR hN) + (timer.remove.struct s t (Q := RmQ st T s t) hT ht hR hN ?_)) fun s' hs => ?_ + · intro c ctx ct a b s1 ctxX tx rr s7 hc ct0 sh m hrr + subst hrr + have htW : t ∈ (ctx.plug (.node ct a t b)).keys := TRB.Ctx.keys_sub ctx _ t (by simp [TRB.RBT.keys]) + obtain ⟨mb, hb⟩ := ct0.bal + obtain ⟨n, hcx, hdb, hRB, hdep⟩ := rf_init hb ct0.black ct0.height_le sh + have ek := sh.keys + have hsub := delKeys_sub ctx ct a b t + have hd7 : (ctxX.plug tx).keys.Nodup := by rw [ek]; exact ct0.nodup.sublist hsub + have hk7 : ∀ y ∈ (ctxX.plug tx).keys, y ∈ T := fun y hy => by + rw [ek] at hy; exact ct0.inT y (hsub.subset hy) + refine WP.spec_mono (timer.remove_fixup.rb s7 c hT m.np m.rep hd7 hk7 hcx hdb hRB (by omega)) + fun s8 ⟨W', m', hF, hb', hbl'⟩ => ?_ + intro c' W0 hc' hr0 + have ec : c' = c := UScalar.eq_of_val_eq (hc'.symm.trans (ct0.core t htW)) + subst ec + have e0 := TRB.Rep.det m.rep hr0 + subst e0 + exact ⟨W', m', hF, hb', hbl'⟩ + obtain ⟨⟨hR', htd, hN', _⟩, hs⟩ := hs + rcases hs with ⟨h0, rfl⟩ | ⟨c, hc, ctx, ct, a, b, ct0, s1, ctxX, tx, rr, s7, s8, w1, hm1, sh, m, h1, h2, rfl⟩ + · have hn : t ∉ W.keys := fun ht' => hW.armed _ ht' h0 + have hf : W.keys.filter (· != t) = W.keys := by + apply List.filter_eq_self.mpr + exact fun y hy => bne_iff_ne.mpr (fun e => hn (e ▸ hy)) + exact ⟨rfl, htd, hN' _ hcpu, W, hW, hf.symm, (fun _ _ => ⟨rfl, rfl⟩), rfl⟩ + have hNc := hN c hc + have htW : t ∈ (ctx.plug (.node ct a t b)).keys := TRB.Ctx.keys_sub ctx _ t (by simp [TRB.RBT.keys]) + have hsub := delKeys_sub ctx ct a b t + have hkW : ∀ y ∈ (ctx.plug (.node ct a t b)).keys, y ∈ T := ct0.inT + -- the state `s1` (after the `TIMER_MIN` write) + have eb1 : L.nodeBase s1 c = L.nodeBase s c := by simp [LawfulStore.nodeBase, w1.1] + have hN1 : NodePlaced st T s1 c := ⟨by rw [eb1]; exact hNc.align, by rw [eb1]; exact hNc.fits, by rw [eb1]; exact hNc.disj⟩ + have k1 := KeepOut.of_wi (X := fun _ => False) hT hNc (w1.mono fun b hb => Or.inr ⟨_, hoM, hb⟩) + -- the tree `W8` before the clearing + have A : ∃ W8, TRB.Rep (lk st s8) 0#u64 (L.mem s8 (L.nodeAt s8 c layout.node.TIMER_ROOT)) W8 ∧ + W8.keys = TRB.delKeys ctx a b ∧ (∃ m8, TRB.Bal W8 m8) ∧ W8.isRed = false ∧ + WritesIn st s1 s8 (FixFoot st s1 c (ctx.plug (.node ct a t b)).keys) ∧ + (∀ y ∈ T, (L.mem s8 (thA y)).val >>> 8 = (L.mem s1 (thA y)).val >>> 8) ∧ NodePlaced st T s8 c := by + obtain ⟨mb, hb⟩ := ct0.bal + cases rr with + | true => + have e8 := h1 rfl + subst e8 + obtain ⟨m', hb', hbl'⟩ := del_red_bal hb ct0.black sh + exact ⟨_, m.rep, sh.keys, ⟨m', hb'⟩, hbl', m.wi, m.core, m.np⟩ + | false => + obtain ⟨W', m', hF, hb', hbl'⟩ := h2 rfl c _ (ct0.core t htW) m.rep + refine ⟨W', hF.rep, hF.keys.trans sh.keys, ⟨m', hb'⟩, hbl', ?_, fun y hy => (hF.core y hy).trans (m.core y hy), + hF.np⟩ + refine (m.wi.trans hF.wi).mono fun w hw => hw.elim id fun hw => ?_ + rcases hw with ⟨z, hz, e⟩ | e + · refine Or.inl ⟨z, hz.imp_right fun hz => ?_, e⟩ + rw [sh.keys] at hz; exact hsub.subset hz + · exact Or.inr (by rw [e, nodeAt_of_ksb m.wi.1]) + obtain ⟨W8, rep8, keys8, ⟨m8, bal8⟩, black8, wi18, core18, np8⟩ := A + -- the clearing + have wc := clrT_wi (st := st) s8 hT ht + have kc := KeepOut.of_wi (X := (· = t)) hT np8 (wc.mono fun w ⟨K, hK, e⟩ => (Or.inl ⟨t, Or.inr ⟨ht, rfl⟩, K, hK, e⟩ : XFoot st T s8 (· = t) c w)) + have nodec : ∀ o : U64, o.val < 4288 → o.val % 8 = 0 → + L.mem (clrT st s8 t) (L.nodeAt (clrT st s8 t) c o) = L.mem s8 (L.nodeAt s8 c o) := fun o ho ho8 => by + rw [nodeAt_of_ksb wc.1] + exact wc.mem_eq (nodeAt_aligned np8 ho ho8) fun ⟨K, hK, e⟩ => + node_ne_tz hT np8 ho (Tz.of_mem ht) hK (congrArg UScalar.val e) + + have hc0 : c = L.cpu s := UScalar.eq_of_val_eq ((ct0.core t htW).symm.trans (hlocal (ct0.armed t htW))) + have eW : (ctx.plug (.node ct a t b)) = W := ct0.uniq (hc0.symm ▸ hW) + have keyfilter : W8.keys = W.keys.filter (· != t) := by + rw [keys8, ← eW, TRB.plug_t_keys, TRB.delKeys_eq] + have hn := delKeys_not ct0.nodup + rw [TRB.delKeys_eq] at hn + have ha : (ctx.pre ++ a.keys).filter (· != t) = ctx.pre ++ a.keys := + List.filter_eq_self.mpr fun y hy => bne_iff_ne.mpr fun e => hn (e ▸ List.mem_append_left _ hy) + have hb : (b.keys ++ ctx.post).filter (· != t) = b.keys ++ ctx.post := + List.filter_eq_self.mpr fun y hy => bne_iff_ne.mpr fun e => hn (e ▸ List.mem_append_right _ hy) + rw [List.filter_append, ha, List.filter_cons] + simp only [bne_self_eq_false, Bool.false_eq_true, ite_false, hb] + have hkeep (y : U64) (hy : y ∈ W8.keys) : + L.mem (clrT st s8 t) (tdA y) = L.mem s (tdA y) ∧ L.mem (clrT st s8 t) (tbA y) = L.mem s (tbA y) := by + have hmem : y ∈ (ctx.plug (.node ct a t b)).keys := hsub.subset (keys8 ▸ hy) + have hyT := hkW y hmem + have hne : y ≠ t := fun e => delKeys_not ct0.nodup (e ▸ keys8 ▸ hy) + constructor + · exact (kc.fld y hyT hne 3528#u64 (by simp) (by simp)).trans + ((wi_fix_td hT hkW hN1 wi18 hyT).trans (k1.fld y hyT id 3528#u64 (by simp) (by simp))) + · exact (kc.fld y hyT hne 3536#u64 (by simp) (by simp)).trans + ((fixupBadgeA hT hkW hN1 wi18 hyT).trans (k1.fld y hyT id 3536#u64 (by simp) (by simp))) + have hrep : TRB.Rep (lk st (clrT st s8 t)) 0#u64 + (L.mem (clrT st s8 t) (L.nodeAt (clrT st s8 t) c layout.node.TIMER_ROOT)) W8 := by + rw [nodec _ hoR hoR8] + refine rep8.congr fun y hy => ?_ + have hyT := hkW y (hsub.subset (keys8 ▸ hy)) + have hne : y ≠ t := fun e => delKeys_not ct0.nodup (e ▸ keys8 ▸ hy) + have f := kc.fld y hyT hne + refine ⟨f 3544#u64 (by simp) (by simp), f 3552#u64 (by simp) (by simp), + f 3560#u64 (by simp) (by simp), ?_⟩ + show ((L.mem (clrT st s8 t) (thA y) &&& 1#u64) != 0#u64) = ((L.mem s8 (thA y) &&& 1#u64) != 0#u64) + rw [show thA y = core.num.U64.wrapping_add y 3568#u64 from rfl, f 3568#u64 (by simp) (by simp)] + obtain ⟨W', hW'⟩ := hR'.tree c hc + have e := TRB.Rep.det hW'.rep hrep + subst W' + have hprog1 : L.mem s1 (L.nodeAt s1 c layout.node.REPROGRAM) = + L.mem s (L.nodeAt s c layout.node.REPROGRAM) := by + rw [nodeAt_of_ksb w1.1] + exact w1.mem_eq (nodeAt_aligned hNc (by simp) (by simp)) + (nodeAt_ne hNc (by simp) hoM (by rw [timer_min_val]; simp)) + have hprog := (nodec layout.node.REPROGRAM (by simp) (by simp)).trans + ((fixupReprogramA wi18 hT hN1 hkW).trans hprog1) + have ecpu := wc.2.1.trans (wi18.2.1.trans w1.2.1) + rw [hc0] at hW' hprog + exact ⟨ecpu, htd, hN' _ hcpu, W8, hW', keyfilter, hkeep, hprog⟩ + + +end + +private theorem timerSplitA {α : Type} {p : α → Bool} (pre post : List α) + (hpre : ∀ x ∈ pre, p x = true) (hpost : ∀ x ∈ post, p x = false) : + (pre ++ post).takeWhile p = pre ∧ (pre ++ post).dropWhile p = post := by + induction pre with + | nil => + cases post with + | nil => simp + | cons y ys => simp [hpost y (by simp)] + | cons x xs ih => + obtain ⟨ht, hd⟩ := ih (fun y hy => hpre y (by simp [hy])) + simp [hpre x (by simp), ht, hd] + +/-- The Ext selector uses the same prefix as the concrete descent, +so equal deadlines precede the new entry. The queue is after removing +the previous entry for this target. -/ +theorem fl4SetTcbTimer_placementA {target : ObjRef} {deadline : Time} {badge : Data} + (pre post : List (ObjRef × Fl4TcbTimer)) (hd : deadline ≠ 0) + (hn : ∀ e ∈ pre ++ post, e.1 ≠ target) + (hpre : ∀ e ∈ pre, e.2.deadline ≤ deadline) + (hpost : ∀ e ∈ post, deadline < e.2.deadline) : + fl4SetTcbTimer (pre ++ post) target deadline badge = + (pre ++ (target, ⟨deadline, badge⟩) :: post, pre.isEmpty) := by + have hfilter : (pre ++ post).filter (fun e => e.1 != target) = pre ++ post := by + apply List.filter_eq_self.mpr + intro e he + simpa using hn e he + have hle (e : ObjRef × Fl4TcbTimer) (he : e ∈ pre) : decide (e.2.deadline ≤ deadline) = true := + decide_eq_true (hpre e he) + have hgt (e : ObjRef × Fl4TcbTimer) (he : e ∈ post) : decide (e.2.deadline ≤ deadline) = false := + decide_eq_false (by + intro h + have h' := hpost e he + change e.2.deadline.toNat ≤ deadline.toNat at h + change deadline.toNat < e.2.deadline.toNat at h' + omega) + obtain ⟨ht, hdrop⟩ := timerSplitA pre post hle hgt + simp only [fl4SetTcbTimer, hfilter, if_neg hd, ht, hdrop, List.append_assoc, List.singleton_append] + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/TcbInvokeTimerAttachA.lean b/proofs/Fl4ProofsA64/P3/TcbInvokeTimerAttachA.lean new file mode 100644 index 000000000..9e46cbb48 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbInvokeTimerAttachA.lean @@ -0,0 +1,185 @@ +/- +P3 (AArch64): timer attachment with the raw badge in the post. +The P1 descent and InsMid already give the ordered placement, owner and +deadline. This variant of timer.insert.attach retains the badge write +for the user-visible SetTimer contract. +-/ +import Fl4ProofsA64.P1.TimerRBInsH + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P0.Consts + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {T : Set U64} + +theorem tcbTimerInsert_attachBadgeA (s : S) (t d bd : U64) {P : S → Prop} {Q : S → S → Prop} + (hT : TcbPlaced T) (ht : t ∈ T) + (hrem : timer.remove st s t ⦃ s1 => P s1 ⦄) + (hP : ∀ s1, P s1 → NodePlaced st T s1 (L.cpu s1) ∧ ∃ t0, CoreTree st T s1 (L.cpu s1) t0 ∧ t ∉ t0.keys) + (hfix : ∀ s1 s8 ctx, P s1 → InsMid st T s1 s8 (L.cpu s1) t d ctx → + L.mem s8 (tbA t) = bd → + timer.insert_fixup st s8 (L.cpu s1) t ⦃ s9 => Q s8 s9 ⦄) : + timer.insert st s t d bd ⦃ s' => (d = 0#u64 ∧ P s') ∨ + (d ≠ 0#u64 ∧ ∃ s1 s8 ctx s9, P s1 ∧ InsMid st T s1 s8 (L.cpu s1) t d ctx ∧ L.mem s8 (tbA t) = bd ∧ Q s8 s9 ∧ + (ctx.pre = [] → s' = L.wr (L.wr s9 (L.nodeAt s9 (L.cpu s1) layout.node.TIMER_MIN) t) + (L.nodeAt (L.wr s9 (L.nodeAt s9 (L.cpu s1) layout.node.TIMER_MIN) t) (L.cpu s1) layout.node.REPROGRAM) + 1#u64) ∧ + (ctx.pre ≠ [] → s' = s9)) ⦄ := by + have ht0 : t ≠ 0#u64 := fun e => (hT t ht).ne0 (by rw [e]; rfl) + unfold timer.insert + refine WP.spec_bind hrem fun s1 h1 => ?_ + split + · rename_i hd; exact (WP.spec_ok _).2 (Or.inl ⟨hd, h1⟩) + rename_i hd + obtain ⟨hN, t0, ct, htn⟩ := hP s1 h1 + refine WP.spec_bind (L.cur_cpu_spec s1) fun cpu ec => ?_ + subst ec + refine WP.spec_bind (timer.root.val s1 (L.cpu s1)) fun cur ecur => ?_ + subst ecur + have hh : t0.height ≤ layout.TREE_FUEL.val := by rw [layout.TREE_FUEL.val_eq]; exact ct.height_le + refine WP.spec_bind (timer.insert_loop.desc s1 d layout.TREE_FUEL ct.rep ct.sorted hh) ?_ + rintro ⟨p, gl, fst⟩ ⟨ctx, ep, epp, edir, efst, hpre, hpost⟩ + simp only at epp edir efst + subst ep + subst epp + try simp only [WP.uncurry'_pair] + obtain ⟨-, -, mpk, mp0, -, -, -⟩ := TRB.mid_facts ct.rep ct.nodup + have hpT : Tz T ctx.par := by + by_cases et : ctx = .top + · subst et; exact Tz.zero + · exact Tz.of_mem (ct.inT _ (mpk et)) + have hcs : (L.cpu s1).val < 2 ^ 56 := by + have := hN.fits; simp only [LawfulStore.nodeBase] at this; omega + have F1 : ∀ K : U64, 3528 ≤ K.val → K.val < 3576 → + InsFoot st s1 (L.cpu s1) t ctx.par (core.num.U64.wrapping_add t K) := fun K a b => Or.inl ⟨K, a, b, rfl⟩ + have r0 := RW.init hN (InsFoot st s1 (L.cpu s1) t ctx.par) + step with obj.Tcb.is_null.spec as ⟨b, eb⟩ + refine WP.spec_bind (Pₘ := fun r : S × U64 => RW st T s1 r.1 (L.cpu s1) (TRB.holeSet ctx t (lk st s1)) + (TRB.holeR ctx t (L.mem s1 (L.nodeAt s1 (L.cpu s1) layout.node.TIMER_ROOT))) + (InsFoot st s1 (L.cpu s1) t ctx.par) ∧ r.2 = t) ?_ ?_ + · cases ctx with + | top => + split + · unfold timer.set_root + refine WP.spec_bind (obj.node_set.spec s1 _ _ t) fun s3 e => ?_ + subst e + exact (WP.spec_ok _).2 ⟨r0.wr_root hT (Or.inr (Or.inr (Or.inr rfl))), rfl⟩ + · rename_i hb; exact absurd (by rw [eb]; exact decide_eq_true rfl) hb + | lc cp k r up => + have hk0 : k ≠ 0#u64 := mp0 (by intro e; cases e) + have hkT : k ∈ T := ct.inT _ (mpk (by intro e; cases e)) + have edir' : gl = true := edir + split + · rename_i hb; rw [eb] at hb; exact absurd (of_decide_eq_true hb) hk0 + · refine WP.spec_bind (Pₘ := fun s2 => s2 = L.wr s1 (tlA k) t) ?_ fun s2 e => ?_ + · first + | exact timer.set_left.on s1 k t hT hkT + | (split + · exact timer.set_left.on s1 k t hT hkT + · rename_i hg; exact absurd edir' hg) + · subst e; exact (WP.spec_ok _).2 ⟨r0.wr_tl hT hkT (Or.inr (Or.inl rfl)), rfl⟩ + | rc cp l k up => + have hk0 : k ≠ 0#u64 := mp0 (by intro e; cases e) + have hkT : k ∈ T := ct.inT _ (mpk (by intro e; cases e)) + have edir' : gl = false := edir + split + · rename_i hb; rw [eb] at hb; exact absurd (of_decide_eq_true hb) hk0 + · refine WP.spec_bind (Pₘ := fun s2 => s2 = L.wr s1 (trA k) t) ?_ fun s2 e => ?_ + · first + | exact timer.set_right.on s1 k t hT hkT + | (split + · rename_i hg; rw [edir'] at hg; exact absurd hg (by simp) + · exact timer.set_right.on s1 k t hT hkT) + · subst e; exact (WP.spec_ok _).2 ⟨r0.wr_tr hT hkT (Or.inr (Or.inr (Or.inl rfl))), rfl⟩ + rintro ⟨s2, t1⟩ ⟨r2, e⟩ + simp only at r2 e + subst e + try simp only [WP.uncurry'_pair] + step as ⟨i, hi1, hi2⟩ + simp only [lift, timer.RED, Aeneas.Std.bind_ok] + refine WP.spec_bind (obj.Tcb.set_timer_height.wr s2 t1 _) fun s3 e3 => ?_ + subst e3 + have r3 := r2.wr_th (v := i ||| 1#u64) hT ht (F1 3568#u64 (by simp) (by simp)) + rw [or1_red] at r3 + refine WP.spec_bind (obj.Tcb.set_timer_deadline.wr _ t1 d) fun s4 e4 => ?_ + subst e4 + have r4 : RW st T s1 (L.wr (L.wr s2 (thA t1) (i ||| 1#u64)) (tdA t1) d) _ _ _ _ := + r3.wr_oth (K := 3528#u64) (v := d) hT ht (by simp) (by simp) (by simp) (by simp) (by simp) (by simp) + (F1 _ (by simp) (by simp)) + refine WP.spec_bind (obj.Tcb.set_timer_badge.wr _ t1 bd) fun s5 e5 => ?_ + subst e5 + have r5 : RW st T s1 (L.wr (L.wr (L.wr s2 (thA t1) (i ||| 1#u64)) (tdA t1) d) (tbA t1) bd) _ _ _ _ := + r4.wr_oth (K := 3536#u64) (v := bd) hT ht (by simp) (by simp) (by simp) (by simp) (by simp) (by simp) + (F1 _ (by simp) (by simp)) + refine WP.spec_bind (timer.set_parent.on _ t1 ctx.par hT ht) fun s6 e6 => ?_ + subst e6 + have r6 := r5.wr_tp (v := ctx.par) hT ht (F1 3560#u64 (by simp) (by simp)) + refine WP.spec_bind (timer.set_left.on _ t1 0#u64 hT ht) fun s7 e7 => ?_ + subst e7 + have r7 := r6.wr_tl (v := 0#u64) hT ht (F1 3544#u64 (by simp) (by simp)) + refine WP.spec_bind (timer.set_right.on _ t1 0#u64 hT ht) fun s8 e8 => ?_ + have r8 := r7.wr_tr (v := 0#u64) hT ht (F1 3552#u64 (by simp) (by simp)) + rw [← e8] at r8 + -- the deadline / height words of `t` at `s8` + have hd8 : L.mem s8 (tdA t1) = d := by + rw [e8] + simp only [tdA, thA, trA, tlA, tpA, tbA] + rw [fld_keep (K := 3552#u64) (K' := 3528#u64) hT ht ht (by simp) (by simp) (by simp) (by simp) (by simp), + fld_keep (K := 3544#u64) (K' := 3528#u64) hT ht ht (by simp) (by simp) (by simp) (by simp) (by simp), + fld_keep (K := 3560#u64) (K' := 3528#u64) hT ht ht (by simp) (by simp) (by simp) (by simp) (by simp), + fld_keep (K := 3536#u64) (K' := 3528#u64) hT ht ht (by simp) (by simp) (by simp) (by simp) (by simp)] + exact L.mem_wr_same (tz_aligned hT (Tz.of_mem ht) (K := 3528#u64) (by simp) (by simp)) + have hbd8 : L.mem s8 (tbA t1) = bd := by + rw [e8] + simp only [trA, tlA, tpA, tbA] + rw [fld_keep (K := 3552#u64) (K' := 3536#u64) hT ht ht (by simp) (by simp) (by simp) (by simp) (by simp), + fld_keep (K := 3544#u64) (K' := 3536#u64) hT ht ht (by simp) (by simp) (by simp) (by simp) (by simp), + fld_keep (K := 3560#u64) (K' := 3536#u64) hT ht ht (by simp) (by simp) (by simp) (by simp) (by simp)] + exact L.mem_wr_same (tz_aligned hT (Tz.of_mem ht) (K := 3536#u64) (by simp) (by simp)) + have hth8 : L.mem s8 (thA t1) = i ||| 1#u64 := by + rw [e8] + simp only [tdA, thA, trA, tlA, tpA, tbA] + rw [fld_keep (K := 3552#u64) (K' := 3568#u64) hT ht ht (by simp) (by simp) (by simp) (by simp) (by simp), + fld_keep (K := 3544#u64) (K' := 3568#u64) hT ht ht (by simp) (by simp) (by simp) (by simp) (by simp), + fld_keep (K := 3560#u64) (K' := 3568#u64) hT ht ht (by simp) (by simp) (by simp) (by simp) (by simp), + fld_keep (K := 3536#u64) (K' := 3568#u64) hT ht ht (by simp) (by simp) (by simp) (by simp) (by simp), + fld_keep (K := 3528#u64) (K' := 3568#u64) hT ht ht (by simp) (by simp) (by simp) (by simp) (by simp)] + exact L.mem_wr_same (tz_aligned hT (Tz.of_mem ht) (K := 3568#u64) (by simp) (by simp)) + have tdo : ∀ y ∈ T, y ≠ t1 → L.mem s8 (tdA y) = L.mem s1 (tdA y) := fun y hy hyt => + r8.wi.mem_eq (tz_aligned hT (Tz.of_mem hy) (K := 3528#u64) (by simp) (by simp)) + (insFoot_not (K' := 3528#u64) hT hN ht hpT hy (by simp) hyt (Or.inr ⟨by simp, by simp⟩)) + have kmem : ∀ z ∈ (ctx.plug (.node true .leaf t1 .leaf)).keys, z ∈ T := fun z hz => + (TRB.attach_mem hz).elim (fun e => e ▸ ht) (ct.inT z) + have hrep : TRB.Rep (lk st s8) 0#u64 (L.mem s8 (L.nodeAt s8 (L.cpu s1) layout.node.TIMER_ROOT)) + (ctx.plug (.node true .leaf t1 .leaf)) := by + rw [r8.root] + exact (TRB.attachK_rep ct.rep ct.nodup ht0 htn).congr_on kmem r8.eq + have nt : ∀ z ∈ (ctx.plug .leaf).keys, z ≠ t1 := fun z hz e => htn (e ▸ hz) + have hsorted : (ctx.plug (.node true .leaf t1 .leaf)).keys.Pairwise + (fun a b => (L.mem s8 (tdA a)).val ≤ (L.mem s8 (tdA b)).val) := by + refine TRB.attach_sorted ?_ (fun z hz => ?_) (fun z hz => ?_) + · exact ct.sorted.imp_of_mem fun ha hb hab => by + rw [tdo _ (ct.inT _ ha) (nt _ ha), tdo _ (ct.inT _ hb) (nt _ hb)]; exact hab + · have hz' := TRB.pre_mem hz + rw [tdo _ (ct.inT _ hz') (nt _ hz'), hd8]; exact hpre z hz + · have hz' : z ∈ (ctx.plug .leaf).keys := by rw [TRB.Ctx.plug_keys]; simp [hz, TRB.RBT.keys] + rw [tdo _ (ct.inT _ hz') (nt _ hz'), hd8]; exact hpost z hz + have mid : InsMid st T s1 s8 (L.cpu s1) t1 d ctx := + ⟨ct, htn, ht, hpre, hpost, hrep, hd8, tdo, + by rw [hth8, or1_shr8, shl8_shr8 hcs hi1], hsorted, r8.wi, r8.np⟩ + refine WP.spec_bind (hfix s1 s8 ctx h1 mid hbd8) fun s9 h9 => ?_ + split + · rename_i hf + have hpre0 : ctx.pre = [] := efst.1 hf + refine WP.spec_bind (obj.node_set.spec s9 _ _ t1) fun s10 e10 => ?_ + refine WP.spec_mono (obj.node_set.spec _ _ _ 1#u64) fun s11 e11 => ?_ + subst e10 e11 + exact Or.inr ⟨hd, s1, s8, ctx, s9, h1, mid, hbd8, h9, fun _ => rfl, fun h => absurd hpre0 h⟩ + · rename_i hf + exact (WP.spec_ok _).2 (Or.inr ⟨hd, s1, s8, ctx, s9, h1, mid, hbd8, h9, + fun h => absurd (efst.2 h) hf, fun _ => rfl⟩) + +end +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/TcbInvokeTimerProjectionA.lean b/proofs/Fl4ProofsA64/P3/TcbInvokeTimerProjectionA.lean new file mode 100644 index 000000000..7f8bc639c --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbInvokeTimerProjectionA.lean @@ -0,0 +1,160 @@ +/- +P3 (AArch64): project timer-tree keys and Store words into the existing +FL4 timer queue. This module contains no concrete timer execution. +-/ +import Fl4ProofsA64.P3.TcbInvokeTimerA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4SpecA64 + +private theorem ne_iff_of_eq_iff {α β : Sort _} {a b : α} {x y : β} + (h : a = b ↔ x = y) : a ≠ b ↔ x ≠ y := not_congr h + +private theorem timerKeyBne {α β : Type} [BEq α] [LawfulBEq α] [BEq β] [LawfulBEq β] + (f : α → β) (hinj : Function.Injective f) (y t : α) : + (f y != f t) = (y != t) := by + apply Bool.eq_iff_iff.2 + simp only [bne_iff_ne] + exact ne_iff_of_eq_iff ⟨fun e => hinj e, congrArg f⟩ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +private def timerEntryA (s : S) (t : U64) : ObjRef × Fl4TcbTimer := + (specPtr t, ⟨(L.mem s (tdA t)).bv, (L.mem s (tbA t)).bv⟩) + +private theorem timerEntry_eqA (s : S) (t : U64) : + timerEntryA (st := st) s t = + (specPtr t, ⟨(L.mem s (tdA t)).bv, (L.mem s (tbA t)).bv⟩) := rfl + +/-- The in-order projection of the existing concrete tree. -/ +@[irreducible] def tcbTimerQueueOfA (st : machine.Store S) [L : LawfulStore st] (s : S) (W : TRB.RBT) : + List (ObjRef × Fl4TcbTimer) := W.keys.map (timerEntryA (st := st) s) + +private theorem tcbTimerQueueOf_eqA (s : S) (W : TRB.RBT) : + tcbTimerQueueOfA st s W = W.keys.map (timerEntryA (st := st) s) := by + unfold tcbTimerQueueOfA + rfl + +attribute [local irreducible] timerEntryA tdA tbA + +set_option trace.profiler true in +private theorem timerQueue_filterA (s : S) (t : U64) (xs : List U64) : + (xs.map (timerEntryA (st := st) s)).filter (fun e => e.1 != specPtr t) = + (xs.filter (· != t)).map (timerEntryA (st := st) s) := by + rw [List.filter_map] + have he : ((fun e => e.1 != specPtr t) ∘ timerEntryA (st := st) s) = (fun y => y != t) := by + funext y + rw [Function.comp_apply, timerEntry_eqA] + exact timerKeyBne specPtr (fun {_ _} e => specPtr_inj e) y t + rw [he] + +set_option trace.profiler true in +/-- Removing a key projects to the FL4 filter. Retained timer words +come from the named Store state after removal. -/ +theorem tcbTimerQueue_removeA {s sRemoved : S} {t : U64} {W WRemoved : TRB.RBT} + (hkeys : WRemoved.keys = W.keys.filter (· != t)) + (hkeep : ∀ y ∈ WRemoved.keys, + L.mem sRemoved (tdA y) = L.mem s (tdA y) ∧ L.mem sRemoved (tbA y) = L.mem s (tbA y)) : + tcbTimerQueueOfA st sRemoved WRemoved = + (tcbTimerQueueOfA st s W).filter (fun e => e.1 != specPtr t) := by + rw [tcbTimerQueueOf_eqA, tcbTimerQueueOf_eqA, timerQueue_filterA, ← hkeys] + clear hkeys + apply List.map_congr_left + intro y hy + obtain ⟨hd, hb⟩ := hkeep y hy + rw [timerEntry_eqA, timerEntry_eqA, hd, hb] + +set_option trace.profiler true in +private theorem timerEntry_afterInsertA {sRemoved sFinal : S} {t d bd y : U64} + (hd : L.mem sFinal (tdA y) = if y = t then d else L.mem sRemoved (tdA y)) + (hb : L.mem sFinal (tbA y) = if y = t then bd else L.mem sRemoved (tbA y)) + (hne : y ≠ t) : + timerEntryA (st := st) sFinal y = timerEntryA (st := st) sRemoved y := by + rw [if_neg hne] at hd hb + rw [timerEntry_eqA, timerEntry_eqA, hd, hb] + +set_option trace.profiler true in +/-- The insertion frame keeps both words of every retained entry and +projects the inserted entry from the final Store words. -/ +theorem tcbTimerQueue_insertA {sRemoved sFinal : S} {t d bd : U64} {WFinal : TRB.RBT} + {pre post : List U64} + (hkeys : WFinal.keys = pre ++ t :: post) (hnew : t ∉ pre ++ post) + (hdeadline : ∀ y ∈ WFinal.keys, L.mem sFinal (tdA y) = if y = t then d else L.mem sRemoved (tdA y)) + (hbadge : ∀ y ∈ WFinal.keys, L.mem sFinal (tbA y) = if y = t then bd else L.mem sRemoved (tbA y)) : + tcbTimerQueueOfA st sFinal WFinal = + pre.map (timerEntryA (st := st) sRemoved) ++ (specPtr t, ⟨d.bv, bd.bv⟩) :: + post.map (timerEntryA (st := st) sRemoved) := by + have hold (y : U64) (hy : y ∈ pre ++ post) : + timerEntryA (st := st) sFinal y = timerEntryA (st := st) sRemoved y := by + have hy' : y ∈ WFinal.keys := by + rw [hkeys, List.mem_append, List.mem_cons] + exact (List.mem_append.1 hy).imp_right Or.inr + exact timerEntry_afterInsertA (hdeadline y hy') (hbadge y hy') (fun e => hnew (e ▸ hy)) + have hpre : pre.map (timerEntryA (st := st) sFinal) = pre.map (timerEntryA (st := st) sRemoved) := + List.map_congr_left (fun y hy => hold y (List.mem_append_left _ hy)) + have hpost : post.map (timerEntryA (st := st) sFinal) = post.map (timerEntryA (st := st) sRemoved) := + List.map_congr_left (fun y hy => hold y (List.mem_append_right _ hy)) + have ht : t ∈ WFinal.keys := by + rw [hkeys] + exact List.mem_append_right _ List.mem_cons_self + have hd := (hdeadline t ht).trans (if_pos rfl) + have hb := (hbadge t ht).trans (if_pos rfl) + clear hold hnew hdeadline hbadge ht + rw [tcbTimerQueueOf_eqA, hkeys, List.map_append, List.map_cons, hpre, hpost, timerEntry_eqA, hd, hb] + +private theorem fl4SetTcbTimer_filter_eqA {queue queue' : List (ObjRef × Fl4TcbTimer)} + {target : ObjRef} {d : Time} {bd : Data} + (he : queue.filter (fun e => e.1 != target) = queue'.filter (fun e => e.1 != target)) : + fl4SetTcbTimer queue target d bd = fl4SetTcbTimer queue' target d bd := by + unfold fl4SetTcbTimer + rw [he] + +/-- Cancellation at zero ticks removes the target and leaves REPROGRAM unchanged. -/ +theorem fl4SetTcbTimer_zeroA (queue : List (ObjRef × Fl4TcbTimer)) (target : ObjRef) (bd : Data) : + fl4SetTcbTimer queue target 0 bd = (queue.filter (fun e => e.1 != target), false) := by + unfold fl4SetTcbTimer + rw [if_pos rfl] + +attribute [local irreducible] fl4SetTcbTimer + +set_option trace.profiler true in +/-- The concrete descent's ordered split determines the FL4 queue and its +earliest-deadline flag after removal. -/ +theorem tcbTimerPlacement_queueA {sRemoved : S} {t d bd : U64} {WRemoved : TRB.RBT} + {ctx : TRB.Ctx} {queue : List (ObjRef × Fl4TcbTimer)} + (hqueue : tcbTimerQueueOfA st sRemoved WRemoved = queue.filter (fun e => e.1 != specPtr t)) + (hkeys : WRemoved.keys = ctx.pre ++ ctx.post) (hd : d ≠ 0#u64) + (hpre : ∀ y ∈ ctx.pre, (L.mem sRemoved (tdA y)).val ≤ d.val) + (hpost : ∀ y ∈ ctx.post, d.val < (L.mem sRemoved (tdA y)).val) : + fl4SetTcbTimer queue (specPtr t) d.bv bd.bv = + (ctx.pre.map (timerEntryA (st := st) sRemoved) ++ (specPtr t, ⟨d.bv, bd.bv⟩) :: + ctx.post.map (timerEntryA (st := st) sRemoved), ctx.pre.isEmpty) := by + let left := ctx.pre.map (timerEntryA (st := st) sRemoved) + let right := ctx.post.map (timerEntryA (st := st) sRemoved) + have hf : queue.filter (fun e => e.1 != specPtr t) = left ++ right := by + rw [← hqueue, tcbTimerQueueOf_eqA, hkeys, List.map_append] + have hn (e : ObjRef × Fl4TcbTimer) (he : e ∈ left ++ right) : e.1 ≠ specPtr t := by + rw [← hf, List.mem_filter] at he + exact bne_iff_ne.1 he.2 + have hl (e : ObjRef × Fl4TcbTimer) (he : e ∈ left) : e.2.deadline ≤ d.bv := by + obtain ⟨y, hy, rfl⟩ := List.mem_map.1 he + rw [timerEntry_eqA] + exact hpre y hy + have hr (e : ObjRef × Fl4TcbTimer) (he : e ∈ right) : d.bv < e.2.deadline := by + obtain ⟨y, hy, rfl⟩ := List.mem_map.1 he + rw [timerEntry_eqA] + exact hpost y hy + have hd' : d.bv ≠ 0 := fun e => hd (UScalar.eq_of_val_eq (congrArg BitVec.toNat e)) + have hkeep : (left ++ right).filter (fun e => e.1 != specPtr t) = left ++ right := + List.filter_eq_self.mpr (fun e he => bne_iff_ne.mpr (hn e he)) + rw [fl4SetTcbTimer_filter_eqA (hf.trans hkeep.symm)] + clear hqueue hkeys hpre hpost hd hf hkeep + exact (fl4SetTcbTimer_placementA (badge := bd.bv) left right hd' hn hl hr).trans + (congrArg (fun b => (left ++ (specPtr t, ⟨d.bv, bd.bv⟩) :: right, b)) + (List.isEmpty_map (f := timerEntryA (st := st) sRemoved))) + +end +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/TcbInvokeTimerSpecA.lean b/proofs/Fl4ProofsA64/P3/TcbInvokeTimerSpecA.lean new file mode 100644 index 000000000..b0043d587 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbInvokeTimerSpecA.lean @@ -0,0 +1,73 @@ +/- +P3 (AArch64): connect the local Store-only timer operation to the chosen +queue in the FL4 spec and to the existing scheduler REPROGRAM observation. +[J] Read that observation at the actual caller Restart successor. +[S] The previous armed owner is local and the target stall has no effects. +-/ +import Fl4ProofsA64.P3.TcbInvokeSetTimerEntryA +import Fl4ProofsA64.P3.DelSpecA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4SpecA64 + +attribute [local irreducible] fl4SetTcbTimer + +/-- Choose the queue represented by the concrete tree at the timer operation. -/ +theorem tcbSetTimer_specA (σ : State Unit) (target : ObjRef) (deadline : Time) (badge : Data) + (queue : List (ObjRef × Fl4TcbTimer)) : + (.ok [], { σ with reprogramTimer := σ.reprogramTimer || + (fl4SetTcbTimer queue target deadline badge).2 }) ∈ + ((invokeFl4Tcb (.setTimer target deadline badge)).run σ).1 := by + unfold invokeFl4Tcb + apply mem_liftED + refine mem_bind_ndD (x := (queue, σ)) ⟨Set.mem_univ _, rfl⟩ ?_ + let flag := (fl4SetTcbTimer queue target deadline badge).2 + let update := fun x : State Unit => { x with reprogramTimer := x.reprogramTimer || flag } + have hm : ((), update σ) ∈ (Fl4Spec.NonDet.modify update σ).1 := by rfl + exact mem_bind_ndD hm rfl + +private theorem timerReprogram_wordA (old : U64) (earliest : Bool) : + decide ((if earliest then 1#u64 else old) ≠ 0#u64) = + (decide (old ≠ 0#u64) || earliest) := by + cases earliest + · simp only [Bool.false_eq_true, ite_false, Bool.or_false] + · simp only [ite_true, Bool.or_true] + decide + +section +variable {S A : Type} {st : machine.Store S} [L : LawfulStore st] {T Cs : Set U64} + +set_option trace.profiler true in +/-- The accepted Store operation picks its related spec queue and returns +the same scheduler-visible REPROGRAM observation. -/ +theorem tcbSetTimer_performA {ar : machine.Arch A} {s sRestart : S} {a : A} + {σRestart : State Unit} {t d bd : U64} {ctx : tcb.Ctx} {W : TRB.RBT} + (hst : smp.remote_tcb_stall st ar s a t = ok (s, a)) + (hlen : ¬ctx.length < 2#u64) (hd : tcb.arg st s ctx 0#u64 = ok d) + (hb : tcb.arg st s ctx 1#u64 = ok bd) (hr : tcb.restart_caller st s = ok sRestart) + (hT : TcbPlaced T) (ht : t ∈ T) (hR : TimerRB st T Cs sRestart) + (hN : ∀ c ∈ Cs, NodePlaced st T sRestart c) (hcpu : L.cpu sRestart ∈ Cs) + (hW : CoreTree st T sRestart (L.cpu sRestart) W) + (hlocal : L.mem sRestart (tdA t) ≠ 0#u64 → + (L.mem sRestart (thA t)).val >>> 8 = (L.cpu sRestart).val) + (hprog : σRestart.reprogramTimer = + decide (L.mem sRestart (L.nodeAt sRestart (L.cpu sRestart) layout.node.REPROGRAM) ≠ 0#u64)) : + tcb.decode st ar s a fl4_abi.label.TCBSetTimer t ctx ⦃ r => + r.1 = .Ok () ∧ r.2.2 = a ∧ + TcbTimerStorePostA st T sRestart r.2.1 + (fl4SetTcbTimer (tcbTimerQueueOfA st sRestart W) (specPtr t) d.bv bd.bv) ∧ + ∃ σFinal, (.ok [], σFinal) ∈ ((invokeFl4Tcb (.setTimer (specPtr t) d.bv bd.bv)).run σRestart).1 ∧ + σFinal.reprogramTimer = + decide (L.mem r.2.1 (L.nodeAt r.2.1 (L.cpu sRestart) layout.node.REPROGRAM) ≠ 0#u64) ⦄ := by + refine WP.spec_mono (tcbSetTimer_queueA hst hlen hd hb hr hT ht hR hN hcpu hW hlocal) ?_ + rintro r ⟨hok, ha, hStore⟩ + refine ⟨hok, ha, hStore, _, tcbSetTimer_specA σRestart (specPtr t) d.bv bd.bv + (tcbTimerQueueOfA st sRestart W), ?_⟩ + let post := fl4SetTcbTimer (tcbTimerQueueOfA st sRestart W) (specPtr t) d.bv bd.bv + change (σRestart.reprogramTimer || post.2) = _ + rw [hStore.reprogram, timerReprogram_wordA, hprog] + +end +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/TcbInvokeTimerStoreA.lean b/proofs/Fl4ProofsA64/P3/TcbInvokeTimerStoreA.lean new file mode 100644 index 000000000..25990a3b2 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbInvokeTimerStoreA.lean @@ -0,0 +1,114 @@ +/- +P3 (AArch64): independent Store frame lemmas for local SetTimer. +[S] An armed timer being removed belongs to the selected CPU. +-/ +import Fl4ProofsA64.P3.TcbInvokeTimerProjectionA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4SpecA64 + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {T Cs : Set U64} + +attribute [local irreducible] tdA tbA thA fl4SetTcbTimer + +/-- The state between removal and insertion. The old timer is disarmed, +and the retained tree keeps both payload words and the REPROGRAM word. -/ +structure TcbTimerRemovedA (st : machine.Store S) [L : LawfulStore st] (T : Set U64) + (s sRemoved : S) (t : U64) (W : TRB.RBT) : Prop where + cpu : L.cpu sRemoved = L.cpu s + disarmed : L.mem sRemoved (tdA t) = 0#u64 + node : NodePlaced st T sRemoved (L.cpu s) + tree : ∃ WRemoved, CoreTree st T sRemoved (L.cpu s) WRemoved ∧ + WRemoved.keys = W.keys.filter (· != t) ∧ + ∀ y ∈ WRemoved.keys, L.mem sRemoved (tdA y) = L.mem s (tdA y) ∧ L.mem sRemoved (tbA y) = L.mem s (tbA y) + reprogram : L.mem sRemoved (L.nodeAt sRemoved (L.cpu s) layout.node.REPROGRAM) = + L.mem s (L.nodeAt s (L.cpu s) layout.node.REPROGRAM) + +/-- The selected core has the final tree and cached minimum, the Ext +queue, and the exact Store flag governing the next timer programming. -/ +structure TcbTimerStorePostA (st : machine.Store S) [L : LawfulStore st] (T : Set U64) + (s sFinal : S) (queuePost : List (ObjRef × Fl4TcbTimer) × Bool) : Prop where + tree : ∃ WFinal, CoreTree st T sFinal (L.cpu s) WFinal ∧ + tcbTimerQueueOfA st sFinal WFinal = queuePost.1 + reprogram : L.mem sFinal (L.nodeAt sFinal (L.cpu s) layout.node.REPROGRAM) = + if queuePost.2 then 1#u64 else L.mem s (L.nodeAt s (L.cpu s) layout.node.REPROGRAM) + +set_option trace.profiler true in +theorem tcbTimerRemove_postA (s : S) (t : U64) {W : TRB.RBT} + (hT : TcbPlaced T) (ht : t ∈ T) (hR : TimerRB st T Cs s) + (hN : ∀ c ∈ Cs, NodePlaced st T s c) (hcpu : L.cpu s ∈ Cs) + (hW : CoreTree st T s (L.cpu s) W) + (hlocal : L.mem s (tdA t) ≠ 0#u64 → (L.mem s (thA t)).val >>> 8 = (L.cpu s).val) : + timer.remove st s t ⦃ sRemoved => TcbTimerRemovedA st T s sRemoved t W ⦄ := by + refine WP.spec_mono (tcbTimerRemove_orderA s t hT ht hR hN hcpu hW hlocal) ?_ + rintro sRemoved ⟨hc, hd, hn, WRemoved, htree, hkeys, hkeep, hprog⟩ + exact ⟨hc, hd, hn, ⟨WRemoved, htree, hkeys, hkeep⟩, hprog⟩ + +set_option trace.profiler true in +theorem TcbTimerRemovedA.localTree {s sRemoved : S} {t : U64} {W : TRB.RBT} + (h : TcbTimerRemovedA st T s sRemoved t W) : + NodePlaced st T sRemoved (L.cpu sRemoved) ∧ + ∃ WRemoved, CoreTree st T sRemoved (L.cpu sRemoved) WRemoved ∧ t ∉ WRemoved.keys := by + obtain ⟨WRemoved, ht, _, _⟩ := h.tree + rw [h.cpu] + exact ⟨h.node, WRemoved, ht, fun hk => ht.armed t hk h.disarmed⟩ + +set_option trace.profiler true in +theorem tcbTimerCancel_postA {s sRemoved : S} {t bd : U64} {W : TRB.RBT} + (h : TcbTimerRemovedA st T s sRemoved t W) : + TcbTimerStorePostA st T s sRemoved (fl4SetTcbTimer (tcbTimerQueueOfA st s W) (specPtr t) 0 bd.bv) := by + obtain ⟨WRemoved, ht, hkeys, hkeep⟩ := h.tree + rw [fl4SetTcbTimer_zeroA] + exact ⟨⟨WRemoved, ht, tcbTimerQueue_removeA hkeys hkeep⟩, h.reprogram⟩ + +set_option trace.profiler true in +private theorem tcbTimerInsert_reprogramA {s sRemoved sFinal : S} {c : U64} + {pre : List U64} {queuePost : List (ObjRef × Fl4TcbTimer) × Bool} + (hbefore : L.mem sRemoved (L.nodeAt sRemoved c layout.node.REPROGRAM) = + L.mem s (L.nodeAt s c layout.node.REPROGRAM)) + (hafter : L.mem sFinal (L.nodeAt sFinal c layout.node.REPROGRAM) = + if pre = [] then 1#u64 else L.mem sRemoved (L.nodeAt sRemoved c layout.node.REPROGRAM)) + (hflag : queuePost.2 = pre.isEmpty) : + L.mem sFinal (L.nodeAt sFinal c layout.node.REPROGRAM) = + if queuePost.2 then 1#u64 else L.mem s (L.nodeAt s c layout.node.REPROGRAM) := by + rw [hafter, hbefore, hflag] + cases pre with + | nil => simp only [List.isEmpty_nil, ite_true] + | cons _ _ => simp only [List.isEmpty_cons, List.cons_ne_nil, Bool.false_eq_true, ite_false] + +set_option trace.profiler true in +/-- Compose both projection equalities at the exact Store after removal +and the resulting Store after insertion. -/ +theorem tcbTimerInsert_postA {s sRemoved sFinal : S} {t d bd : U64} {W WFinal : TRB.RBT} {ctx : TRB.Ctx} + (hRemoved : TcbTimerRemovedA st T s sRemoved t W) + (hOld : CoreTree st T sRemoved (L.cpu sRemoved) (ctx.plug .leaf)) + (hFinal : CoreTree st T sFinal (L.cpu sRemoved) WFinal) + (hd : d ≠ 0#u64) (hkeys : WFinal.keys = ctx.pre ++ t :: ctx.post) + (hpre : ∀ y ∈ ctx.pre, (L.mem sRemoved (tdA y)).val ≤ d.val) + (hpost : ∀ y ∈ ctx.post, d.val < (L.mem sRemoved (tdA y)).val) + (hdeadline : ∀ y ∈ WFinal.keys, L.mem sFinal (tdA y) = if y = t then d else L.mem sRemoved (tdA y)) + (hbadge : ∀ y ∈ WFinal.keys, L.mem sFinal (tbA y) = if y = t then bd else L.mem sRemoved (tbA y)) + (hprog : L.mem sFinal (L.nodeAt sFinal (L.cpu sRemoved) layout.node.REPROGRAM) = + if ctx.pre = [] then 1#u64 else L.mem sRemoved (L.nodeAt sRemoved (L.cpu sRemoved) layout.node.REPROGRAM)) : + TcbTimerStorePostA st T s sFinal + (fl4SetTcbTimer (tcbTimerQueueOfA st s W) (specPtr t) d.bv bd.bv) := by + obtain ⟨WRemoved, hTree, hRemoveKeys, hKeep⟩ := hRemoved.tree + have he : WRemoved = ctx.plug .leaf := hTree.uniq (hRemoved.cpu ▸ hOld) + have hqueue := (congrArg (tcbTimerQueueOfA st sRemoved) he).symm.trans + (tcbTimerQueue_removeA hRemoveKeys hKeep) + have hsplit : (ctx.plug .leaf).keys = ctx.pre ++ ctx.post := by + rw [TRB.Ctx.plug_keys, TRB.RBT.keys, List.append_nil] + have hn : t ∉ ctx.pre ++ ctx.post := by + rw [← hsplit] + exact fun ht => hOld.armed t ht hRemoved.disarmed + have hplacement := tcbTimerPlacement_queueA (bd := bd) hqueue hsplit hd hpre hpost + have hresult := tcbTimerQueue_insertA hkeys hn hdeadline hbadge + have hflag := congrArg Prod.snd hplacement + have hreg := tcbTimerInsert_reprogramA hRemoved.reprogram (hRemoved.cpu ▸ hprog) hflag + exact ⟨⟨WFinal, hRemoved.cpu ▸ hFinal, hresult.trans (congrArg Prod.fst hplacement).symm⟩, hreg⟩ + +end +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/TcbInvokeTlsPostA.lean b/proofs/Fl4ProofsA64/P3/TcbInvokeTlsPostA.lean new file mode 100644 index 000000000..41ae3d0af --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbInvokeTlsPostA.lean @@ -0,0 +1,168 @@ +/- +P3 (AArch64): the SetTLSBase register update and its local reschedule. +[S] Target stall is quiet. +[J] The existing endpoint and scheduler relations hold after caller Restart. +The scheduler interface supplies its current-thread projection. +-/ +import Fl4ProofsA64.P3.TcbInvokeThreadFieldsA +import Fl4ProofsA64.P3.SchedOpsA +import Fl4ProofsA64.P3.DelSpecA +import Fl4ProofsA64.P3.SchedFootA +import Fl4ProofsA64.P3.EpFootHelpA +import Fl4ProofsA64.P3.SpecFrameSchedA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4SpecA64 Endp + +set_option trace.profiler true in +private theorem tcbTlsSpecA {σ σTls σFinal : State Unit} {t : ObjRef} {x : Tcb} {word : Word} + (hk : σ.kheap t = some (.tcb x)) + (hTls : σTls = Notif.updK σ t (.tcb (Notif.setReg x tlsBaseRegister word))) + (htail : ((), σFinal) ∈ ((Fl4Spec.NonDet.when (t = σTls.curThread) + (rescheduleRequired (Z := Unit))) σTls).1) : + (.ok [], σFinal) ∈ ((fl4InvokeTcb (.setTLSBase t word)).run σ).1 := by + have hInfo : archGetSanitiseRegisterInfo (Z := Unit) t σ = + ({(decide (x.tcbArch.tcbVcpu ≠ none), σ)}, False) := by + have hVcpu : archThreadGet (Z := Unit) ArchTcb.tcbVcpu t σ = ({(x.tcbArch.tcbVcpu, σ)}, False) := by + unfold archThreadGet + rw [nd_bind_oneD (Notif.getTcb_eq hk)] + rfl + unfold archGetSanitiseRegisterInfo + rw [nd_bind_oneD hVcpu] + rfl + have hSet := Notif.asUser_setRegister_eq tlsBaseRegister word hk + rw [← hTls] at hSet + change (.ok [], σFinal) ∈ ((invokeTcb (.setTLSBase t word)).run σ).1 + simp only [invokeTcb] + apply mem_liftED + rw [nd_bind_oneD hInfo] + have hWord : sanitiseRegister (decide (x.tcbArch.tcbVcpu ≠ none)) tlsBaseRegister word = word := rfl + rw [hWord, nd_bind_oneD hSet, Notif.nd_gets_bind] + exact mem_bind_ndD htail rfl + +set_option trace.profiler true in +private theorem tcbTlsSpec_selfA {σ σTls σFinal : State Unit} {t : ObjRef} {x : Tcb} {word : Word} + (hk : σ.kheap t = some (.tcb x)) + (hTls : σTls = Notif.updK σ t (.tcb (Notif.setReg x tlsBaseRegister word))) + (hSelf : t = σTls.curThread) (hRun : ((), σFinal) ∈ (rescheduleRequired (Z := Unit) σTls).1) : + (.ok [], σFinal) ∈ ((fl4InvokeTcb (.setTLSBase t word)).run σ).1 := by + refine tcbTlsSpecA (σTls := σTls) hk hTls ?_ + rw [Fl4Spec.NonDet.when, if_pos hSelf] + exact hRun + +set_option trace.profiler true in +private theorem tcbTlsSpec_otherA {σ σTls : State Unit} {t : ObjRef} {x : Tcb} {word : Word} + (hk : σ.kheap t = some (.tcb x)) + (hTls : σTls = Notif.updK σ t (.tcb (Notif.setReg x tlsBaseRegister word))) + (hOther : t ≠ σTls.curThread) : + (.ok [], σTls) ∈ ((fl4InvokeTcb (.setTLSBase t word)).run σ).1 := by + refine tcbTlsSpecA (σTls := σTls) hk hTls ?_ + rw [Fl4Spec.NonDet.when, if_neg hOther] + rfl + +section +variable {S A : Type} {st : machine.Store S} [L : LawfulStore st] {ar : machine.Arch A} + +set_option trace.profiler true in +/-- The real register update establishes both relations. The concrete self +test agrees with the spec, and only that arm uses the scheduler reschedule. -/ +theorem tcbSetTLSBase_performA {T E R : Set U64} {Bf : Set (U64 × Word)} + {SR : S → State Unit → Prop} (O : SchedOpsEA st T E R Bf SR) + (hCur : ∀ s σ, SR s σ → σ.curThread = specPtr (L.curW s)) + {s sRestart : S} {a : A} {σRestart : State Unit} {t v : U64} {ctx : tcb.Ctx} + (hStall : smp.remote_tcb_stall st ar s a t = ok (s, a)) + (hlen : ¬ctx.length < 1#u64) (hArg : tcb.arg st s ctx 0#u64 = ok v) + (hRestart : tcb.restart_caller st s = ok sRestart) + (hP : EPlaced T E R Bf) (hE : EpStateRel T E R Bf (L.mem sRestart) σRestart) + (hS : SR sRestart σRestart) + (ht : t ∈ T) {x : Tcb} (hk : σRestart.kheap (specPtr t) = some (.tcb x)) : + tcb.decode st ar s a fl4_abi.label.TCBSetTLSBase t ctx ⦃ r => + r.1 = .Ok () ∧ r.2.2 = a ∧ ∃ σFinal, + (.ok [], σFinal) ∈ ((fl4InvokeTcb (.setTLSBase (specPtr t) v.bv)).run σRestart).1 ∧ + EpStateRel T E R Bf (L.mem r.2.1) σFinal ∧ SR r.2.1 σFinal ⦄ := by + let sTls := L.wr sRestart (Endp.regA t (regIdxA tlsBaseRegister)) v + let σTls := Notif.updK σRestart (specPtr t) (.tcb (Notif.setReg x tlsBaseRegister v.bv)) + have hTls : σTls = Notif.updK σRestart (specPtr t) (.tcb (Notif.setReg x tlsBaseRegister v.bv)) := rfl + obtain ⟨hEtls, hWrite, hStep⟩ := write_reg_rel hP sRestart σRestart hE ht hk tlsBaseRegister v + have hStls : SR sTls σTls := + O.stable _ _ _ _ hS hWrite hStep (NoBlockE.tcbK hk _ rfl rfl) + have eh : (specPtr t = σTls.curThread) ↔ t = L.curW sTls := by + rw [hCur sTls σTls hStls] + exact ⟨specPtr_inj, congrArg specPtr⟩ + have hIdx : layout.reg.TLS_BASE = regIdxA tlsBaseRegister := by + rw [layout.reg.TLS_BASE, layout.reg.TPIDR_EL0] + rfl + rw [tcbSetTLSBase_postA hStall hlen hArg hRestart, hIdx] + change (do + let sFinal ← if t = L.curW sTls then sched.reschedule_required st sTls else ok sTls + ok (core.result.Result.Ok (), sFinal, a)) ⦃ _ ⦄ + by_cases hSelf : t = L.curW sTls + · rw [if_pos hSelf] + refine WP.spec_bind (O.reschedule sTls σTls hEtls hStls) ?_ + rintro sFinal ⟨_, σFinal, hRun, hFoot, hK, hFinal⟩ + have hSpec := tcbTlsSpec_selfA (σTls := σTls) hk hTls (eh.mpr hSelf) hRun + exact (WP.spec_ok _).2 ⟨rfl, rfl, σFinal, hSpec, hEtls.frame hP hFoot hK, hFinal⟩ + · rw [if_neg hSelf, bind_ok] + have hSpec := tcbTlsSpec_otherA (σTls := σTls) hk hTls (fun e => hSelf (eh.mp e)) + exact (WP.spec_ok _).2 ⟨rfl, rfl, σTls, hSpec, hEtls, hStls⟩ + + +open Fl4ProofsA64.P3.Sched in +/-- The real register update establishes both relations. The concrete self +test agrees with the spec, and only that arm uses the scheduler reschedule. -/ +theorem tcbSetTLSBase_footA {T E R : Set U64} {Bf : Set (U64 × Word)} + {SR : S → State Unit → Prop} (O : SchedOpsEA st T E R Bf SR) {Wr : State Unit → S → U64 → Prop} + (F : SchedFootEA st T E R Bf SR Wr) + (hCur : ∀ s σ, SR s σ → σ.curThread = specPtr (L.curW s)) + {s sRestart : S} {a : A} {σRestart : State Unit} {t v : U64} {ctx : tcb.Ctx} + (hStall : smp.remote_tcb_stall st ar s a t = ok (s, a)) + (hlen : ¬ctx.length < 1#u64) (hArg : tcb.arg st s ctx 0#u64 = ok v) + (hRestart : tcb.restart_caller st s = ok sRestart) + (hP : EPlaced T E R Bf) (hE : EpStateRel T E R Bf (L.mem sRestart) σRestart) + (hS : SR sRestart σRestart) + (ht : t ∈ T) {x : Tcb} (hk : σRestart.kheap (specPtr t) = some (.tcb x)) : + tcb.decode st ar s a fl4_abi.label.TCBSetTLSBase t ctx ⦃ r => + r.1 = .Ok () ∧ r.2.2 = a ∧ ∃ σFinal, + (.ok [], σFinal) ∈ ((fl4InvokeTcb (.setTLSBase (specPtr t) v.bv)).run σRestart).1 ∧ + EpStateRel T E R Bf (L.mem r.2.1) σFinal ∧ SR r.2.1 σFinal ∧ + WritesIn st sRestart r.2.1 (fun b => EWrite T E R Bf b ∨ + Wr (Notif.updK σRestart (specPtr t) (.tcb (Notif.setReg x tlsBaseRegister v.bv))) + (L.wr sRestart (Endp.regA t (regIdxA tlsBaseRegister)) v) b) ∧ + SSzA σRestart (Notif.updK σRestart (specPtr t) (.tcb (Notif.setReg x tlsBaseRegister v.bv))) ∧ + L.ksb (L.wr sRestart (Endp.regA t (regIdxA tlsBaseRegister)) v) = L.ksb sRestart ∧ + SchedFrameA σRestart σFinal ⦄ := by + let sTls := L.wr sRestart (Endp.regA t (regIdxA tlsBaseRegister)) v + let σTls := Notif.updK σRestart (specPtr t) (.tcb (Notif.setReg x tlsBaseRegister v.bv)) + have hTls : σTls = Notif.updK σRestart (specPtr t) (.tcb (Notif.setReg x tlsBaseRegister v.bv)) := rfl + obtain ⟨hEtls, hWrite, hStep⟩ := write_reg_rel hP sRestart σRestart hE ht hk tlsBaseRegister v + have hStls : SR sTls σTls := + O.stable _ _ _ _ hS hWrite hStep (NoBlockE.tcbK hk _ rfl rfl) + have eh : (specPtr t = σTls.curThread) ↔ t = L.curW sTls := by + rw [hCur sTls σTls hStls] + exact ⟨specPtr_inj, congrArg specPtr⟩ + have hIdx : layout.reg.TLS_BASE = regIdxA tlsBaseRegister := by + rw [layout.reg.TLS_BASE, layout.reg.TPIDR_EL0] + rfl + rw [tcbSetTLSBase_postA hStall hlen hArg hRestart, hIdx] + change (do + let sFinal ← if t = L.curW sTls then sched.reschedule_required st sTls else ok sTls + ok (core.result.Result.Ok (), sFinal, a)) ⦃ _ ⦄ + by_cases hSelf : t = L.curW sTls + · rw [if_pos hSelf] + refine WP.spec_bind (WP.spec_and (O.reschedule sTls σTls hEtls hStls) (F.reschedule sTls σTls hEtls hStls)) ?_ + rintro sFinal ⟨⟨_, σFinal, hRun, hFoot, hK, hFinal⟩, hW⟩ + have hSpec := tcbTlsSpec_selfA (σTls := σTls) hk hTls (eh.mpr hSelf) hRun + exact (WP.spec_ok _).2 ⟨rfl, rfl, σFinal, hSpec, hEtls.frame hP hFoot hK, hFinal, + (hWrite.trans hW).mono fun _ h => h, ssz_updK_tcb _ _ _, L.ksb_wr _ _ _, + PreO.trans (SchedFrameA.upd_tcb (x' := Notif.setReg x tlsBaseRegister v.bv) hk ⟨rfl, rfl, rfl⟩) + ((inferInstance : SPresInst (rescheduleRequired (Z := Unit))).pres σTls ((), σFinal) hRun)⟩ + · rw [if_neg hSelf, bind_ok] + have hSpec := tcbTlsSpec_otherA (σTls := σTls) hk hTls (fun e => hSelf (eh.mp e)) + exact (WP.spec_ok _).2 ⟨rfl, rfl, σTls, hSpec, hEtls, hStls, hWrite.mono fun _ h => Or.inl h, + ssz_updK_tcb _ _ _, L.ksb_wr _ _ _, SchedFrameA.upd_tcb hk ⟨rfl, rfl, rfl⟩⟩ + + +end +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/TcbInvokeWriteLoopsA.lean b/proofs/Fl4ProofsA64/P3/TcbInvokeWriteLoopsA.lean new file mode 100644 index 000000000..337cef5f2 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbInvokeWriteLoopsA.lean @@ -0,0 +1,159 @@ +/- +P3 (AArch64): exact count and index rules for the two WriteRegisters loops. +[J] An intermediate-state relation carries each caller argument read, +sanitising write, and updated prefix through the loop. +-/ +import Fl4ProofsA64.P3.TcbInvokeRegOrderA +import Fl4ProofsA64.P1.QueueDecTcb + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4SpecA64 + +set_option trace.profiler true in +theorem tcbRegArray_indexA {n : Usize} (arr : Aeneas.Std.Array U64 n) + (rs : List Register) (hMap : arr.val = rs.map Endp.regIdxA) + (j : U64) (hj : j.val < rs.length) (hSmall : j.val < 2 ^ 32) : + Aeneas.Std.Array.index_usize arr (UScalar.cast .Usize j) = ok (Endp.regIdxA rs[j.val]) := by + have hc := cast_usize_small hSmall + have hi : (UScalar.cast .Usize j : Usize).val < arr.val.length := by + rw [hc, hMap, List.length_map] + exact hj + have he : arr.val[(UScalar.cast .Usize j : Usize).val]? = some (Endp.regIdxA rs[j.val]) := by + rw [hMap, hc, List.getElem?_map, List.getElem?_eq_getElem hj] + rfl + rw [List.getElem?_eq_getElem hi] at he + rw [eq_of_spec_fd (Aeneas.Std.Array.index_usize_spec arr _ hi), Option.some.inj he] + +theorem tcbRegWriteThenA {S R : Type} (arg : Result U64) + (sanitise : U64 → Result U64) (write : U64 → Result S) (next : S → Result R) + {P : S → Prop} {Q : R → Prop} + (h : (do let raw ← arg; let val ← sanitise raw; write val) ⦃ P ⦄) + (hNext : ∀ s, P s → next s ⦃ Q ⦄) : + (do let raw ← arg; let val ← sanitise raw; let s ← write val; next s) ⦃ Q ⦄ := by + have eq : Aeneas.Std.bind arg (fun raw => Aeneas.Std.bind (sanitise raw) (fun val => + Aeneas.Std.bind (write val) next)) = + Aeneas.Std.bind (Aeneas.Std.bind arg (fun raw => Aeneas.Std.bind (sanitise raw) write)) next := by + simp only [Std.bind_assoc] + change Aeneas.Std.bind arg (fun raw => Aeneas.Std.bind (sanitise raw) (fun val => + Aeneas.Std.bind (write val) next)) ⦃ Q ⦄ + rw [eq] + exact WP.spec_bind h hNext + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {I : Nat → S → Prop} +variable {s : S} {dest buf caller count : U64} + +set_option trace.profiler true in +/-- The frame loop reads syscall arguments at register index plus two and +stops at the lesser of the requested count and the frame list length. -/ +theorem tcbWriteRegisters_frameLoopA (hStart : I 0 s) + (hWrite : ∀ (s' : S) (j arg : U64) (hj : j.val < frameRegisters.length), + j.val < count.val → arg.val = j.val + 2 → I j.val s' → + (do + let raw ← msg.syscall_arg st s' caller buf arg + let reg := Endp.regIdxA frameRegisters[j.val] + let sanitised ← regs.sanitise_for_tcb st s' dest reg raw + obj.Tcb.set_reg st dest s' reg sanitised) ⦃ s'' => I (j.val + 1) s'' ⦄) : + tcb.write_registers_loop0 st s dest buf caller count 0#u64 ⦃ s' => + I (min frameRegisters.length count.val) s' ⦄ := by + have hFrame : frameRegisters.length = 17 := rfl + have hN : tcb.N_FRAME.val = 17 := by rw [tcb.N_FRAME]; rfl + unfold tcb.write_registers_loop0 + apply loop.spec_decr_nat (measure := fun x => 17 - x.2.val) + (inv := fun x => I x.2.val x.1 ∧ x.2.val ≤ min frameRegisters.length count.val) + · rintro ⟨s', j⟩ ⟨hI, hLimit⟩ + change j.val ≤ min frameRegisters.length count.val at hLimit + change I j.val s' at hI + unfold tcb.write_registers_loop0.body + dsimp only + by_cases hFirst : j < tcb.N_FRAME + · rw [if_pos hFirst] + by_cases hCount : j < count + · rw [if_pos hCount] + have hj : j.val < frameRegisters.length := by rw [hFrame, ← hN]; exact hFirst + have hj17 : j.val < 17 := hFrame ▸ hj + have hCountVal : j.val < count.val := hCount + have hSmall : j.val < 2 ^ 32 := by omega + simp only [lift, bind_ok, tcbRegArray_indexA _ _ tcbFrameRegisters_eqA j hj hSmall] + step as ⟨arg, hArg⟩ + refine tcbRegWriteThenA _ _ _ _ (hWrite s' j arg hj hCountVal (by omega) hI) ?_ + intro s'' hJ + step as ⟨j', hJ'⟩ + refine ⟨?_, ?_, ?_⟩ + · change I j'.val s'' + rw [show j'.val = j.val + 1 by omega] + exact hJ + · omega + · omega + · rw [if_neg hCount] + have he : j.val = min frameRegisters.length count.val := by + change ¬j.val < count.val at hCount + omega + exact (WP.spec_ok _).2 (he ▸ hI) + · rw [if_neg hFirst] + have he : j.val = min frameRegisters.length count.val := by + change ¬j.val < tcb.N_FRAME.val at hFirst + omega + exact (WP.spec_ok _).2 (he ▸ hI) + · exact ⟨hStart, Nat.zero_le _⟩ + +set_option trace.profiler true in +/-- The GP loop follows the frame prefix. Its first argument has index 19, +and each accepted iteration writes the corresponding GP register. -/ +theorem tcbWriteRegisters_gpLoopA (hStart : I 0 s) + (hWrite : ∀ (s' : S) (j arg : U64) (hj : j.val < gpRegisters.length), + j.val + 17 < count.val → arg.val = j.val + 19 → I j.val s' → + (do + let raw ← msg.syscall_arg st s' caller buf arg + let reg := Endp.regIdxA gpRegisters[j.val] + let sanitised ← regs.sanitise_for_tcb st s' dest reg raw + obj.Tcb.set_reg st dest s' reg sanitised) ⦃ s'' => I (j.val + 1) s'' ⦄) : + tcb.write_registers_loop1 st s dest buf caller count 0#u64 ⦃ s' => + I (min gpRegisters.length (count.val - 17)) s' ⦄ := by + have hGp : gpRegisters.length = 19 := rfl + have hN : tcb.N_GP.val = 19 := by rw [tcb.N_GP]; rfl + have hFrame : tcb.N_FRAME.val = 17 := by rw [tcb.N_FRAME]; rfl + unfold tcb.write_registers_loop1 + apply loop.spec_decr_nat (measure := fun x => 19 - x.2.val) + (inv := fun x => I x.2.val x.1 ∧ x.2.val ≤ min gpRegisters.length (count.val - 17)) + · rintro ⟨s', j⟩ ⟨hI, hLimit⟩ + change j.val ≤ min gpRegisters.length (count.val - 17) at hLimit + change I j.val s' at hI + unfold tcb.write_registers_loop1.body + dsimp only + by_cases hFirst : j < tcb.N_GP + · rw [if_pos hFirst] + have hj : j.val < gpRegisters.length := by rw [hGp, ← hN]; exact hFirst + have hj19 : j.val < 19 := hGp ▸ hj + step as ⟨i, hi⟩ + by_cases hCount : i < count + · rw [if_pos hCount] + have hiCount : i.val < count.val := hCount + have hSmall : j.val < 2 ^ 32 := by omega + simp only [lift, bind_ok, tcbRegArray_indexA _ _ tcbGpRegisters_eqA j hj hSmall] + step as ⟨arg, hArg⟩ + refine tcbRegWriteThenA _ _ _ _ (hWrite s' j arg hj (by omega) (by omega) hI) ?_ + intro s'' hJ + step as ⟨j', hJ'⟩ + refine ⟨?_, ?_, ?_⟩ + · change I j'.val s'' + rw [show j'.val = j.val + 1 by omega] + exact hJ + · omega + · omega + · rw [if_neg hCount] + have he : j.val = min gpRegisters.length (count.val - 17) := by + change ¬i.val < count.val at hCount + omega + exact (WP.spec_ok _).2 (he ▸ hI) + · rw [if_neg hFirst] + have he : j.val = min gpRegisters.length (count.val - 17) := by + change ¬j.val < tcb.N_GP.val at hFirst + omega + exact (WP.spec_ok _).2 (he ▸ hI) + · exact ⟨hStart, Nat.zero_le _⟩ + +end +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/TcbInvokeWritePrefixA.lean b/proofs/Fl4ProofsA64/P3/TcbInvokeWritePrefixA.lean new file mode 100644 index 000000000..d6e30b113 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbInvokeWritePrefixA.lean @@ -0,0 +1,152 @@ +/- +P3 (AArch64): paired WriteRegisters prefixes over the caller's initial words. +[J] The receive-side argument buffer, when present, belongs to the tracked +buffer pairs. The concrete VCPU link agrees with the guest observation. +-/ +import Fl4ProofsA64.P3.TcbInvokeWriteStepA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4SpecA64 Endp + +/-- The exact TCB after the first `n` register writes from a fixed argument +snapshot. `offset` is the argument index of the first register in `rs`. -/ +def tcbWritePrefixA (guest : Bool) (rs : List Register) (offset : Nat) + (arg : Nat → Word) (x : Tcb) : Nat → Tcb + | 0 => x + | n + 1 => + let r := rs.getD n .faultIP + Notif.setReg (tcbWritePrefixA guest rs offset arg x n) r (fl4SanitiseRegister guest r (arg (n + offset))) + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +theorem tcbVcpuLinkRegFrameA (s : S) {t : U64} (ht : TcbAddr t) (r : Register) (v : U64) : + vcpu.bound st (L.wr s (Endp.regA t (regIdxA r)) v) t = vcpu.bound st s t := by + rw [eq_of_spec_fd (vcpu.bound.spec (L := L) _ t), eq_of_spec_fd (vcpu.bound.spec (L := L) s t)] + let link := core.num.U64.wrapping_add t 3664#u64 + have hal : Aligned link := field_aligned ht (K := 3664#u64) (by decide) (by decide) + have hAddr : link.val = t.val + 3664 := field_val ht (K := 3664#u64) (by decide) + have hn : (Endp.regA t (regIdxA r)).val ≠ link.val := by + intro he + rw [regA_val ht (regIdxA_lt r), hAddr] at he + have := regIdxA_lt r + omega + have hm : L.mem (L.wr s (Endp.regA t (regIdxA r)) v) link = L.mem s link := + L.mem_wr_ne (a_reg ht r).1 hal hn + change ok (L.mem (L.wr s (Endp.regA t (regIdxA r)) v) link) = ok (L.mem s link) + rw [hm] + +private theorem tcbArgBufferViewA {T E R : Set U64} {Bf : Set (U64 × Word)} + {s : S} {σ : State Unit} {buf : U64} {sbo : Option ObjRef} + (hE : EpStateRel T E R Bf (L.mem s) σ) + (hBuf : match sbo with | none => buf = 0#u64 | some sb => (buf, sb) ∈ Bf ∧ buf ≠ 0#u64) : + BufViewA (L.mem s) σ.machineState.underlyingMemory buf sbo := by + cases sbo with + | none => subst buf; exact bufViewA_none _ _ + | some sb => exact hE.bufView hBuf.1 hBuf.2 + +private theorem tcbWritePrefixHeapA (σ : State Unit) (dest caller : U64) (x src : Tcb) + (hne : caller ≠ dest) (hSrc : σ.kheap (specPtr caller) = some (.tcb src)) : + let σ' := Notif.updK σ (specPtr dest) (.tcb x) + σ'.kheap (specPtr dest) = some (.tcb x) ∧ σ'.kheap (specPtr caller) = some (.tcb src) := by + dsimp only + rw [Notif.updK_kheap, Function.update_self, Function.update_of_ne (fun he => hne (specPtr_inj he))] + exact ⟨rfl, hSrc⟩ + +variable {T E R : Set U64} {Bf : Set (U64 × Word)} {SR : S → State Unit → Prop} +variable {dest caller buf p : U64} {sbo : Option ObjRef} {src : Tcb} {um : Word → Word8} + +private def WritePrefixInv (rs : List Register) (offset : Nat) (x : Tcb) (n : Nat) (s : S) : Prop := + ∃ σ : State Unit, + EpStateRel T E R Bf (L.mem s) σ ∧ SR s σ ∧ + σ.kheap (specPtr caller) = some (.tcb src) ∧ + σ.kheap (specPtr dest) = some (.tcb (tcbWritePrefixA (p != 0#u64) rs offset + (specArgA src.tcbArch.tcbContext.userRegs sbo um) x n)) ∧ + σ.machineState.underlyingMemory = um ∧ vcpu.bound st s dest = ok p + +set_option trace.profiler true in +private theorem tcbWritePrefixStepA (O : SchedOpsEA st T E R Bf SR) (P : EPlaced T E R Bf) + (ht : dest ∈ T) (hc : caller ∈ T) (hne : caller ≠ dest) + (hBuf : match sbo with | none => buf = 0#u64 | some sb => (buf, sb) ∈ Bf ∧ buf ≠ 0#u64) + (rs : List Register) (offset : Nat) (x : Tcb) (s : S) (j arg : U64) + (hIndex : j.val < rs.length) (hArg : arg.val = j.val + offset) (hSmall : arg.val < 127) + (hI : WritePrefixInv (st := st) (T := T) (E := E) (R := R) (Bf := Bf) (SR := SR) + (dest := dest) (caller := caller) (p := p) (sbo := sbo) (src := src) (um := um) rs offset x j.val s) : + (do + let raw ← msg.syscall_arg st s caller buf arg + let reg := regIdxA rs[j.val] + let sanitised ← regs.sanitise_for_tcb st s dest reg raw + obj.Tcb.set_reg st dest s reg sanitised) ⦃ s' => + WritePrefixInv (st := st) (T := T) (E := E) (R := R) (Bf := Bf) (SR := SR) + (dest := dest) (caller := caller) (p := p) (sbo := sbo) (src := src) (um := um) rs offset x (j.val + 1) s' ⦄ := by + obtain ⟨σ, hE, hS, hSrc, hDest, hMem, hVcpu⟩ := hI + let r := rs[j.val] + let old := tcbWritePrefixA (p != 0#u64) rs offset (specArgA src.tcbArch.tcbContext.userRegs sbo um) x j.val + let v : U64 := ⟨fl4SanitiseRegister (p != 0#u64) r + (specArgA src.tcbArch.tcbContext.userRegs sbo σ.machineState.underlyingMemory arg.val)⟩ + let s' := L.wr s (Endp.regA dest (regIdxA r)) v + let x' := Notif.setReg old r v.bv + let σ' := Notif.updK σ (specPtr dest) (.tcb x') + have hStep := tcbWriteRegister_argumentA O P hE hS ht hc hDest hSrc (tcbArgBufferViewA hE hBuf) hSmall + (r := r) (guest := (p != 0#u64)) (fun _ => ⟨p, hVcpu, rfl⟩) + change _ = ok s' ∧ _ ∧ EpStateRel T E R Bf (L.mem s') σ' ∧ SR s' σ' at hStep + obtain ⟨hRun, _, hE', hS'⟩ := hStep + rw [hRun, WP.spec_ok] + have hDst : x' = tcbWritePrefixA (p != 0#u64) rs offset + (specArgA src.tcbArch.tcbContext.userRegs sbo um) x (j.val + 1) := by + change Notif.setReg old r (fl4SanitiseRegister (p != 0#u64) r + (specArgA src.tcbArch.tcbContext.userRegs sbo σ.machineState.underlyingMemory arg.val)) = _ + rw [hMem, hArg, tcbWritePrefixA] + simp only [List.getD_eq_getElem _ _ hIndex] + rfl + have hk := tcbWritePrefixHeapA σ dest caller x' src hne hSrc + have hLink : vcpu.bound st s' dest = ok p := (tcbVcpuLinkRegFrameA s (P.tcb dest ht) r v).trans hVcpu + refine ⟨σ', hE', hS', hk.2, ?_, hMem, hLink⟩ + rw [← hDst] + exact hk.1 + +set_option trace.profiler true in +/-- Both extracted loops preserve the caller's argument snapshot and the + VCPU link while refining the exact frame prefix followed by the GP prefix. -/ +theorem tcbWriteRegisters_prefixA (O : SchedOpsEA st T E R Bf SR) (P : EPlaced T E R Bf) + {s : S} {σ : State Unit} {x : Tcb} {count : U64} + (hE : EpStateRel T E R Bf (L.mem s) σ) (hS : SR s σ) + (ht : dest ∈ T) (hc : caller ∈ T) (hne : caller ≠ dest) + (hDest : σ.kheap (specPtr dest) = some (.tcb x)) (hSrc : σ.kheap (specPtr caller) = some (.tcb src)) + (hBuf : match sbo with | none => buf = 0#u64 | some sb => (buf, sb) ∈ Bf ∧ buf ≠ 0#u64) + (hVcpu : vcpu.bound st s dest = ok p) : + let args := specArgA src.tcbArch.tcbContext.userRegs sbo σ.machineState.underlyingMemory + let frame := tcbWritePrefixA (p != 0#u64) frameRegisters 2 args x (min frameRegisters.length count.val) + let gp := tcbWritePrefixA (p != 0#u64) gpRegisters 19 args frame (min gpRegisters.length (count.val - 17)) + (do + let sFrame ← tcb.write_registers_loop0 st s dest buf caller count 0#u64 + tcb.write_registers_loop1 st sFrame dest buf caller count 0#u64) ⦃ sFinal => + ∃ σFinal : State Unit, + EpStateRel T E R Bf (L.mem sFinal) σFinal ∧ SR sFinal σFinal ∧ + σFinal.kheap (specPtr caller) = some (.tcb src) ∧ σFinal.kheap (specPtr dest) = some (.tcb gp) ∧ + σFinal.machineState.underlyingMemory = σ.machineState.underlyingMemory ∧ vcpu.bound st sFinal dest = ok p ⦄ := by + dsimp only + let I := WritePrefixInv (st := st) (T := T) (E := E) (R := R) (Bf := Bf) (SR := SR) + (dest := dest) (caller := caller) (p := p) (sbo := sbo) (src := src) (um := σ.machineState.underlyingMemory) + have hStart : I frameRegisters 2 x 0 s := ⟨σ, hE, hS, hSrc, hDest, rfl, hVcpu⟩ + have hFrame := tcbWriteRegisters_frameLoopA (count := count) (dest := dest) (buf := buf) + (caller := caller) hStart (fun s' j arg hj _ ha hI => + tcbWritePrefixStepA O P ht hc hne hBuf frameRegisters 2 x s' j arg hj ha (by + have hLen : frameRegisters.length = 17 := rfl + omega) hI) + refine WP.spec_bind hFrame ?_ + intro sFrame hI + let frame := tcbWritePrefixA (p != 0#u64) frameRegisters 2 + (specArgA src.tcbArch.tcbContext.userRegs sbo σ.machineState.underlyingMemory) x + (min frameRegisters.length count.val) + have hGpStart : I gpRegisters 19 frame 0 sFrame := hI + exact tcbWriteRegisters_gpLoopA (count := count) (dest := dest) (buf := buf) + (caller := caller) hGpStart (fun s' j arg hj _ ha hJ => + tcbWritePrefixStepA O P ht hc hne hBuf gpRegisters 19 frame s' j arg hj ha (by + have hLen : gpRegisters.length = 19 := rfl + omega) hJ) + +end +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/TcbInvokeWriteSpecA.lean b/proofs/Fl4ProofsA64/P3/TcbInvokeWriteSpecA.lean new file mode 100644 index 000000000..91c2b6b23 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbInvokeWriteSpecA.lean @@ -0,0 +1,85 @@ +/- +P3 (AArch64): the abstract WriteRegisters register prefix and NextIP. +[J] The destination is a typed TCB in the named post-Restart state. +-/ +import Fl4ProofsA64.P3.TcbInvokeRegOrderA +import Fl4ProofsA64.P3.DelSpecA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open Fl4SpecA64 + +noncomputable section + +/-- Apply each supplied word to the corresponding register, retaining the +FPU image and sanitising only the registers that need it. -/ +def tcbWrittenContextA (guest : Bool) (rs : List Register) (values : List Word) (uc : UserContext) : + UserContext := + (rs.zip values).foldl (fun c rv => Notif.setRegUc c rv.1 (fl4SanitiseRegister guest rv.1 rv.2)) uc + +set_option trace.profiler true in +private theorem tcbZipWrite_eqA (guest : Bool) (rs : List Register) (values : List Word) (uc : UserContext) : + Fl4Spec.NonDet.zipWithM (fun r v => setRegister r (fl4SanitiseRegister guest r v)) rs values uc = + ({((rs.zip values).map (fun _ => ()), tcbWrittenContextA guest rs values uc)}, False) := by + induction rs generalizing values uc with + | nil => cases values <;> rfl + | cons r rs ih => + cases values with + | nil => rfl + | cons v vs => + let after := Notif.setRegUc uc r (fl4SanitiseRegister guest r v) + have hSet : setRegister r (fl4SanitiseRegister guest r v) uc = ({((), after)}, False) := rfl + change (setRegister r (fl4SanitiseRegister guest r v) >>= fun u => + Fl4Spec.NonDet.zipWithM (fun r v => setRegister r (fl4SanitiseRegister guest r v)) rs vs >>= fun us => + Fl4Spec.NonDet.pure (u :: us)) uc = _ + rw [nd_bind_oneD hSet, nd_bind_oneD (ih vs after)] + rfl + +set_option trace.profiler true in +private theorem tcbWriteUser_eqA (guest : Bool) (values : List Word) (uc : UserContext) : + let written := tcbWrittenContextA guest (frameRegisters ++ gpRegisters) values uc + (do + let _ ← Fl4Spec.NonDet.zipWithM (fun r v => setRegister r (fl4SanitiseRegister guest r v)) + (frameRegisters ++ gpRegisters) values + let pc ← getRestartPC + setNextPC pc : UserMonad Unit) uc = + ({((), Notif.setRegUc written .nextIP (written.userRegs .faultIP))}, False) := by + dsimp only + rw [nd_bind_oneD (tcbZipWrite_eqA guest _ values uc)] + let written := tcbWrittenContextA guest (frameRegisters ++ gpRegisters) values uc + have hPc : getRestartPC written = ({(written.userRegs .faultIP, written)}, False) := rfl + rw [nd_bind_oneD hPc] + rfl + +set_option trace.profiler true in +/-- The abstract register body writes the sanitised prefix, then assigns +NextIP from the resulting FaultIP and returns the exact updated heap. -/ +theorem tcbWriteRegisters_userA {σ : State Unit} {dest : ObjRef} {x : Tcb} + (hk : σ.kheap dest = some (.tcb x)) (guest : Bool) (values : List Word) : + let written := tcbWrittenContextA guest (frameRegisters ++ gpRegisters) values + (archTcbContextGet x.tcbArch) + let final := Notif.setRegUc written .nextIP (written.userRegs .faultIP) + asUser dest (do + let _ ← Fl4Spec.NonDet.zipWithM (fun r v => setRegister r (fl4SanitiseRegister guest r v)) + (frameRegisters ++ gpRegisters) values + let pc ← getRestartPC + setNextPC pc) σ = + ({((), Notif.updK σ dest (.tcb { x with tcbArch := archTcbContextSet final x.tcbArch }))}, False) := by + dsimp only + unfold asUser + rw [nd_bind_oneD (Notif.getTcb_eq hk), Notif.nd_pure_bind'] + rw [tcbWriteUser_eqA, nd_bind_oneD (selectF_oneA _ _)] + dsimp only + rw [Notif.nd_pure_bind'] + let written := tcbWrittenContextA guest (frameRegisters ++ gpRegisters) values + (archTcbContextGet x.tcbArch) + let final := Notif.setRegUc written .nextIP (written.userRegs .faultIP) + let xFinal : Tcb := { x with tcbArch := archTcbContextSet final x.tcbArch } + change (setObject dest (.tcb xFinal) >>= fun _ => Fl4Spec.NonDet.pure ()) σ = + ({((), Notif.updK σ dest (.tcb xFinal))}, False) + rw [nd_bind_oneD (Notif.setObject_eq (o' := .tcb xFinal) hk rfl)] + rfl + +end +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/TcbInvokeWriteStepA.lean b/proofs/Fl4ProofsA64/P3/TcbInvokeWriteStepA.lean new file mode 100644 index 000000000..b7f18ea2b --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbInvokeWriteStepA.lean @@ -0,0 +1,57 @@ +/- +P3 (AArch64): one paired WriteRegisters loop iteration. +[J] The caller's register and buffer views hold at the actual argument +read. The VCPU observation is needed only at the SPSR write. +-/ +import Fl4ProofsA64.P3.TcbInvokeWriteLoopsA +import Fl4ProofsA64.P3.SchedOpsA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4SpecA64 Endp + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +set_option trace.profiler true in +/-- Evaluate the syscall argument, perform the sanitised Store write and +advance the paired endpoint and scheduler relations in the same step. -/ +theorem tcbWriteRegister_argumentA {T E R : Set U64} {Bf : Set (U64 × Word)} + {SR : S → State Unit → Prop} (O : SchedOpsEA st T E R Bf SR) + {s : S} {σ : State Unit} {t caller buf arg : U64} {sbo : Option ObjRef} + {x src : Tcb} {r : Register} {guest : Bool} + (P : EPlaced T E R Bf) (hE : EpStateRel T E R Bf (L.mem s) σ) (hS : SR s σ) + (ht : t ∈ T) (hc : caller ∈ T) + (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hSrc : σ.kheap (specPtr caller) = some (.tcb src)) + (hBuf : BufViewA (L.mem s) σ.machineState.underlyingMemory buf sbo) (hArg : arg.val < 127) + (hGuest : r = .spsrEl1 → ∃ p, vcpu.bound st s t = ok p ∧ guest = (p != 0#u64)) : + let v : U64 := ⟨fl4SanitiseRegister guest r + (specArgA src.tcbArch.tcbContext.userRegs sbo σ.machineState.underlyingMemory arg.val)⟩ + let sFinal := L.wr s (Endp.regA t (regIdxA r)) v + let xFinal := Notif.setReg x r v.bv + let σFinal := Notif.updK σ (specPtr t) (.tcb xFinal) + (do let raw ← msg.syscall_arg st s caller buf arg + let clean ← regs.sanitise_for_tcb st s t (regIdxA r) raw + obj.Tcb.set_reg st t s (regIdxA r) clean) = ok sFinal ∧ + asUser (specPtr t) (setRegister r v.bv) σ = ({((), σFinal)}, False) ∧ + EpStateRel T E R Bf (L.mem sFinal) σFinal ∧ SR sFinal σFinal := by + let v : U64 := ⟨fl4SanitiseRegister guest r + (specArgA src.tcbArch.tcbContext.userRegs sbo σ.machineState.underlyingMemory arg.val)⟩ + let sFinal := L.wr s (Endp.regA t (regIdxA r)) v + let xFinal := Notif.setReg x r v.bv + let σFinal := Notif.updK σ (specPtr t) (.tcb xFinal) + obtain ⟨raw, hRaw, hV⟩ := syscall_arg_relA (hE.regView hc src hSrc).2 hBuf arg hArg + have hConcrete : (do + let w ← msg.syscall_arg st s caller buf arg + let clean ← regs.sanitise_for_tcb st s t (regIdxA r) w + obj.Tcb.set_reg st t s (regIdxA r) clean) = ok sFinal := by + rw [hRaw, bind_ok, tcbWriteSanitisedReg_eqA s t r raw guest hGuest, hV] + obtain ⟨hEFinal, hWrite, hStep⟩ := write_reg_rel P s σ hE ht hk r v + have hSFinal : SR sFinal σFinal := O.stable _ _ _ _ hS hWrite hStep (NoBlockE.tcbK hk xFinal rfl rfl) + have hSpec := Notif.asUser_setRegister_eq r v.bv hk + exact ⟨hConcrete, hSpec, hEFinal, hSFinal⟩ + +end +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/TcbReadJointA.lean b/proofs/Fl4ProofsA64/P3/TcbReadJointA.lean new file mode 100644 index 000000000..e282a5008 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbReadJointA.lean @@ -0,0 +1,371 @@ +/- +P3 (AArch64): TCB Stage-3 integration, ReadRegisters Call without source suspension. + +* **`restartCaller_jointA`**: FL4's `tcb.restart_caller` (the caller's `TS_RESTART` write and + `schedule_tcb`) refines `setThreadState cur .restart`, keeping `SchedSRA` at the same `Z`, + `StEqA`, `EpStateRel`, the typed-heap/slot facts, `BufCover` and `BufNativeA`, the current + thread, and the caller's TCB `{x with tcbState := .restart}`. The writes are the state word + and the current core's action word, so the IRQ-node region is unchanged. +* **`tcbReadRegisters_callPerformJA`**: `tcbReadRegisters_callPerformA` with the endpoint + scheduler interface over the merged relation `IpcSchedRelA` (`schedOpsEAOfPlacementA`) in + place of the TCB line's `O : SchedOpsEA … (SchedSRA st Z m κ)`: the restart/reschedule tails + of that interface move the queue ghosts, so the instance exists over `IpcSchedRelA`. +* **`tcbReadRegisters_callJointA`**: the two composed from the pre-Restart joint state. + +[J] (TCB obligations 1–9, now discharged here from JOIN + MCS): the interface, the placement, +`EpStateRel`/`SchedSRA`/`StEqA`/`StateRelA`/`BufCover`/`BufNativeA` after Restart, the source +distinct from the current thread after Restart, the typed caller. Remaining caller premises: +`hu` (the caller's receive-buffer frames, S3 user-memory relation), `hoff` (slot words off the +endpoint writes, as the Reply producer). +-/ +import Fl4ProofsA64.P3.TcbInvokeReadCallPerformA +import Fl4ProofsA64.P3.ReplyXferFactsA +import Fl4ProofsA64.P3.ReplyRunExA +import Fl4ProofsA64.P3.IpcScPushTopA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4SpecA64 Endp Sched + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S A : Type} {st : machine.Store S} [L : LawfulStore st] {ar : machine.Arch A} + +private theorem restartStepNbA {σ : State Unit} {p : ObjRef} {x : Tcb} + (hk : σ.kheap p = some (.tcb x)) {ts : ThreadState} + (hr : runnable x.tcbState = true) (hn : runnable ts = true) : + NoBlockESc σ (updKSc σ p (.tcb { x with tcbState := ts })) := by + have hh : (updKSc σ p (.tcb { x with tcbState := ts })).kheap p = some (.tcb { x with tcbState := ts }) := by + simp [updKSc] + have far : ∀ q, q ≠ p → (updKSc σ p (.tcb { x with tcbState := ts })).kheap q = σ.kheap q := + fun q hq => by simp [updKSc, hq] + intro q y z h1 h2 + by_cases e : q = p + · subst q + rw [hk] at h1 + rw [hh] at h2 + cases h1 + cases h2 + have hni : x.tcbState ≠ .idleThreadState := by + intro e + rw [e] at hr + cases hr + exact Or.inr (Or.inl ⟨hn, hni, Or.inl (Or.inl hr)⟩) + · rw [far _ e, h1] at h2 + cases h2 + exact Or.inl ⟨rfl, id⟩ + +private theorem stsActA_kheapRA (σ : State Unit) (p : ObjRef) : (stsActA σ p).kheap = σ.kheap := by + unfold stsActA + split <;> rfl + +private theorem stsActA_curRA (σ : State Unit) (p : ObjRef) : (stsActA σ p).curThread = σ.curThread := by + unfold stsActA + split <;> rfl + +private theorem stsActA_memRA (σ : State Unit) (p : ObjRef) : + (stsActA σ p).machineState.underlyingMemory = σ.machineState.underlyingMemory := by + unfold stsActA + split <;> rfl + +private theorem runnableNotWaitingRA {ts : ThreadState} (hr : runnable ts = true) : + Notif.tsCode ts ≠ 3 ∧ Notif.tsCode ts ≠ 4 ∧ Notif.tsCode ts ≠ 6 := by + cases ts <;> simp [runnable, Notif.tsCode] at hr ⊢ + +private theorem updK_eqRA (σ : State Unit) (p : ObjRef) (o : KernelObject) : + Notif.updK σ p o = updKSc σ p o := rfl + +private theorem stsAct_eqRA (σ : State Unit) (p : ObjRef) : Notif.stsAct σ p = stsActA σ p := rfl + +/-- The scheduler relation after the caller's state word. -/ +private theorem restartSchedRA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {σ : State Unit} + (H : SchedSRA st Z m κ s σ) (hst : StEqA Z.T (L.mem s) σ) + {x : Tcb} (hx : σ.kheap (specPtr (L.curW s)) = some (.tcb x)) (hr : runnable x.tcbState = true) : + SchedSRA st Z m κ (L.wr s (stA (L.curW s)) sched.TS_RESTART) + (updKSc σ (specPtr (L.curW s)) (.tcb { x with tcbState := .restart })) ∧ + StEqA Z.T (L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART)) + (updKSc σ (specPtr (L.curW s)) (.tcb { x with tcbState := .restart })) := by + have ht : L.curW s ∈ Z.T := H.curT + have hal : Aligned (stA (L.curW s)) := by + rw [stA_eq] + exact field_aligned (H.core.tcbs _ ht) (by simp) (by simp) + have code : sched.TS_RESTART.val = tsCodeA .restart := by rw [ts_vals.2.1]; rfl + have w1 : WritesIn st s (L.wr s (stA (L.curW s)) sched.TS_RESTART) (· = stA (L.curW s)) := writesIn_wr hal + obtain ⟨hS, -, -⟩ := setStateStepExA hx (ts := .restart) rfl H.run H.rd + refine ⟨H.stable w1 (fun _ e => e ▸ stateOffExA H.toSchedSRN ht) hS (restartStepNbA hx hr rfl), ?_⟩ + intro u hu y hy + by_cases e : u = L.curW s + · subst u + rw [updKSc_kheap, Function.update_self] at hy + cases hy + rw [L.mem_wr_same hal] + exact code + · have hne : specPtr u ≠ specPtr (L.curW s) := fun eq => e (specPtr_inj eq) + rw [updKSc_kheap, Function.update_of_ne hne] at hy + have hne' : stA u ≠ stA (L.curW s) := by + rw [stA_eq, stA_eq] + exact field_ne (H.core.tcbs u hu) (H.core.tcbs _ ht) (by simp) (by simp) (fun e' => absurd e' e) + have ha : Aligned (stA u) := by + rw [stA_eq] + exact field_aligned (H.core.tcbs u hu) (by simp) (by simp) + rw [w1.mem_eq ha hne'] + exact hst u hu y hy + +/-- A runnable TCB is in no endpoint queue. -/ +private theorem runnableNotQueuedRA {T E R : Set U64} {Bf : Set (U64 × Word)} {m : U64 → U64} + {σ : State Unit} (HE : EpStateRel T E R Bf m σ) {t : U64} (ht : t ∈ T) {x : Tcb} + (hx : σ.kheap (specPtr t) = some (.tcb x)) (hr : runnable x.tcbState = true) : + ∀ e ∈ E, ∀ z, σ.kheap (specPtr e) = some (.endpoint z) → specPtr t ∉ z.epQueue := by + obtain ⟨notRecv, notSend, -⟩ := runnableNotWaitingRA hr + have epTcb := HE.at_tcb ht hx + intro e he z hz hqm + obtain ⟨z', hz', rel⟩ := HE.ep e he + rw [hz] at hz' + obtain rfl := KernelObject.endpoint.inj (Option.some.inj hz') + obtain ⟨qs, _, hqs, members⟩ := rel.queue + rw [hqs, List.mem_map] at hqm + obtain ⟨u, hu, hEq⟩ := hqm + have eu : u = t := specPtr_inj hEq + subst u + have state := (members t hu).2 + rw [epTcb.st] at state + exact state.elim notRecv notSend + +/-- The IRQ-node slot region is outside the caller's state word and the action word. -/ +private theorem restartIrqRA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s s' : S} {σ : State Unit} + (H : SchedSRA st Z m κ s σ) + (w : WritesIn st s s' (fun b => b = stA (L.curW s) ∨ b = L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION)) : + IrqSlotsKeepA (st := st) s s' := by + have ht : L.curW s ∈ Z.T := H.curT + have hfit := (H.core.tcbs _ ht).fits + have hstv : (stA (L.curW s)).val = (L.curW s).val + 3328 := by + rw [stA_eq, wa_valm] + have hk : (3328#u64 : U64).val = 3328 := by simp + omega + intro b hb hlo hhi + refine w.mem_eq hb fun h => ?_ + rcases h with rfl | rfl + · rcases H.tcb_ks ht with h | h + · omega + · unfold ksSizeA at h + omega + · obtain ⟨n, -, hn, hcs⟩ := H.csN + have hc := (hcs _).1 H.sch.cpuCs + rw [nodeAt_valSc (o := layout.node.SCHED_ACTION) H.ksFit (by omega) + (by simp [layout.node.SCHED_ACTION])] at hhi + omega + +/-- The caller's state word is an endpoint-written TCB field. -/ +private theorem restartFootRA {Z : XSetsA} {m : McState} {κ : U64 → U64} {E R : Set U64} + {Bf : Set (U64 × Word)} {s s' : S} {σ : State Unit} (H : SchedSRA st Z m κ s σ) + (w : WritesIn st s s' (fun b => b = stA (L.curW s) ∨ b = L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION)) : + WritesIn st s s' (ReplyFootW (st := st) Z m σ s E R Bf) := by + have ht : L.curW s ∈ Z.T := H.curT + have hfit := (H.core.tcbs _ ht).fits + have hstv : (stA (L.curW s)).val = (L.curW s).val + 3328 := by + rw [stA_eq, wa_valm] + have hk : (3328#u64 : U64).val = 3328 := by simp + omega + refine w.mono fun b h => ?_ + rcases h with rfl | rfl + · exact Or.inl (Or.inr (Or.inr (Or.inl ⟨_, ht, Or.inl ⟨by omega, by unfold TWOffN; left; omega⟩⟩))) + · exact Or.inr (Or.inl (swAction H)) + +/-- **The caller's Restart at the joint state** (module docstring). -/ +theorem restartCaller_jointA {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} + {Bf : Set (U64 × Word)} {s : S} {σ : State Unit} + (H : SchedSRA st Z m κ s σ) (hst : StEqA Z.T (L.mem s) σ) + (P : IpcSchedPlacementA Z.th N E R Bf) (PE : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + (hoff : ∀ y, IsSlotA Z.th y → ¬ EWrite Z.T E R Bf y ∧ ¬ EWrite Z.T E R Bf (wa y 8#u64)) + (hSt : StateRelA Z.th (L.mem s) σ) (hV : SlotsValidA Z.th (L.mem s)) + (hC : BufCover st Z.T Bf s σ) (hNative : BufNativeA Z.T (L.mem s)) + {x : Tcb} (hx : σ.kheap (specPtr (L.curW s)) = some (.tcb x)) (hr : runnable x.tcbState = true) : + tcb.restart_caller st s ⦃ s1 => ∃ σ1, + setThreadState (Z := Unit) (specPtr (L.curW s)) .restart σ = ({((), σ1)}, False) ∧ + SchedSRA st Z m κ s1 σ1 ∧ StEqA Z.T (L.mem s1) σ1 ∧ EpStateRel Z.T E R Bf (L.mem s1) σ1 ∧ + StateRelA Z.th (L.mem s1) σ1 ∧ SlotsValidA Z.th (L.mem s1) ∧ BufCover st Z.T Bf s1 σ1 ∧ + BufNativeA Z.T (L.mem s1) ∧ L.curW s1 = L.curW s ∧ + σ1.kheap (specPtr (L.curW s)) = some (.tcb { x with tcbState := .restart }) ∧ + (∀ q, q ≠ specPtr (L.curW s) → σ1.kheap q = σ.kheap q) ⦄ := by + have ht : L.curW s ∈ Z.T := H.curT + have code' : sched.TS_RESTART.val = Notif.tsCode .restart := by rw [ts_vals.2.1]; rfl + obtain ⟨H1, hst1⟩ := restartSchedRA H hst hx hr + have hq := runnableNotQueuedRA HE ht hx hr + obtain ⟨E1, -, -⟩ := write_state_rel PE s σ HE ht hx hq .restart (by decide) sched.TS_RESTART code' + rw [updK_eqRA] at E1 + unfold tcb.restart_caller sched.set_thread_state + refine WP.spec_bind (sched.cur_thread.spec s) ?_ + rintro t1 ht1 + rw [show t1 = L.curW s from ht1] + refine WP.spec_bind (obj.Tcb.set_state.spec s (L.curW s) sched.TS_RESTART) ?_ + intro s₂ hs₂ + rw [hs₂] + refine WP.spec_mono (WP.spec_and (schedule_tcb_refinesA H1 (L.curW s) hst1) + ((schedOpsEAOfPlacementA (st := st) (m := m) (κ := κ) P).scheduleTcb _ _ (L.curW s) E1 + ⟨Z.Q, Z.R, H1⟩ ht)) ?_ + rintro s' ⟨⟨w2, H2⟩, FE, -⟩ + have E2 := E1.frame PE FE (Endp.KView.stsAct' _ (specPtr (L.curW s))) + rw [stsAct_eqRA] at E2 + have hSA : L.nodeAt (L.wr s (stA (L.curW s)) sched.TS_RESTART) + (L.cpu (L.wr s (stA (L.curW s)) sched.TS_RESTART)) layout.node.SCHED_ACTION = + L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION := by + rw [nodeAt_of_ksb (L.ksb_wr _ _ _), L.cpu_wr] + have hal : Aligned (stA (L.curW s)) := by + rw [stA_eq] + exact field_aligned (H.core.tcbs _ ht) (by simp) (by simp) + have w : WritesIn st s s' (fun b => b = stA (L.curW s) ∨ b = L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) := + ((writesIn_wr (v := sched.TS_RESTART) hal).trans w2).mono fun b h => h.elim Or.inl (fun e => Or.inr (e.trans hSA)) + have keep : RemKeepA σ (stsActA (updKSc σ (specPtr (L.curW s)) (.tcb { x with tcbState := .restart })) + (specPtr (L.curW s))) := + (RemKeepA.updTcb (σ' := updKSc σ (specPtr (L.curW s)) (.tcb { x with tcbState := .restart })) + (st := .restart) (sc := x.tcbSchedContext) hx rfl rfl).trans + (RemKeepA.of_kheap (stsActA_memRA _ _) (stsActA_kheapRA _ _)) + have hTcb : ∀ u ∈ Z.T, ∃ x0, σ.kheap (specPtr u) = some (.tcb x0) := fun u hu => by + obtain ⟨y, hy, -⟩ := HE.tcb u hu + exact ⟨y, hy⟩ + have X := replyXferFactsA H.toSchedSRN PE P hoff (restartFootRA (E := E) (R := R) (Bf := Bf) H w) keep + (restartIrqRA H w) hSt hV hC hNative hTcb + have hcur : L.curW s' = L.curW s := + specPtr_inj (H2.sch.curThread.symm.trans ((stsActA_curRA _ _).trans H.sch.curThread)) + refine ⟨_, setThreadState_stsActExA hx _, H2, fun u hu y hy => (E2.at_tcb hu hy).st, E2, + X.1, X.2.1, X.2.2.1, X.2.2.2, hcur, ?_, ?_⟩ + · rw [stsActA_kheapRA, updKSc_kheap, Function.update_self] + · intro q hq + rw [stsActA_kheapRA, updKSc_kheap, Function.update_of_ne hq] + +/-- **`tcbReadRegisters_callPerformA` over the merged relation** (module docstring). -/ +theorem tcbReadRegisters_callPerformJA + {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + {th : TypedHeapA} {s sRestart : S} {a : A} {σRestart : State Unit} + {t flags count : U64} {ctx : tcb.Ctx} {src caller : Tcb} {block donate : Bool} + (hst : smp.remote_tcb_stall st ar s a t = ok (s, a)) + (hlen : ¬ctx.length < 2#u64) + (hf : tcb.arg st s ctx 0#u64 = ok flags) (hn : tcb.arg st s ctx 1#u64 = ok count) + (hlo : ¬count < 1#u64) (hhi : ¬36#u64 < count) (hself : t ≠ L.curW s) + (hr : tcb.restart_caller st s = ok sRestart) + (hcall : ctx.call = true) (hsuspend : (flags &&& 1#u64) = 0#u64) + (P' : IpcSchedPlacementA Z.th N E R Bf) (P : EPlaced Z.T E R Bf) + (H : SchedSRA st Z m κ sRestart σRestart) + (hE : EpStateRel Z.T E R Bf (L.mem sRestart) σRestart) + (hSt : StateRelA th (L.mem sRestart) σRestart) (hC : BufCover st Z.T Bf sRestart σRestart) + (hNative : BufNativeA Z.T (L.mem sRestart)) + (ht : t ∈ Z.T) (hne : t ≠ L.curW sRestart) (htc : th (L.curW sRestart) = some .tcb) + (hsrc : σRestart.kheap (specPtr t) = some (.tcb src)) + (hcaller : σRestart.kheap (specPtr (L.curW sRestart)) = some (.tcb caller)) + (hu : ∀ rp, ipcBufOfA true caller = some rp → ∀ k, k < 128 → + inUserFrame (bufAddr rp k) σRestart) + (hrun : runnable caller.tcbState = true) : + tcb.decode st ar s a fl4_abi.label.TCBReadRegisters t ctx ⦃ r => + r.1 = .Ok () ∧ r.2.2 = a ∧ ∃ σFinal, + (.ok [], σFinal) ∈ ((fl4PerformInvocation block true donate + (.invokeTCB (.readRegisters (specPtr t) false count.bv ArchDefaultExtraRegisters))).run σRestart).1 ∧ + IpcSchedRelA st Z m κ r.2.1 σFinal ∧ StEqA Z.T (L.mem r.2.1) σFinal ∧ + WritesIn st sRestart r.2.1 (fun b => SWrA (st := st) Z sRestart b ∨ + EWrite Z.T E R Bf b ∨ b = stA (L.curW sRestart)) ⦄ := by + have hN : count.val ≤ 36 := by + have h : ¬ 36 < count.val := hhi + omega + rw [tcbReadRegisters_acceptedA hst hlen hf hn hlo hhi hself hr, hcall, hsuspend] + refine WP.spec_bind (Pₘ := fun r : S × A => r.2 = a ∧ ∃ σFinal, + (.ok [], σFinal) ∈ ((fl4PerformInvocation block true donate + (.invokeTCB (.readRegisters (specPtr t) false count.bv ArchDefaultExtraRegisters))).run σRestart).1 ∧ + IpcSchedRelA st Z m κ r.1 σFinal ∧ StEqA Z.T (L.mem r.1) σFinal ∧ + WritesIn st sRestart r.1 (fun b => SWrA (st := st) Z sRestart b ∨ + EWrite Z.T E R Bf b ∨ b = stA (L.curW sRestart))) ?_ ?_ + swap + · rintro ⟨sFinal, aFinal⟩ ⟨h1, h2⟩ + exact (WP.spec_ok _).2 ⟨rfl, h1, h2⟩ + rw [tcbReadRegisters_callProgramA] + simp only [bne_self_eq_false, Bool.false_eq_true, ite_false, bind_ok] + have hc : L.curW sRestart ∈ Z.T := H.curT + refine tcbReadRegisters_callReplyKA (schedOpsEAOfPlacementA (st := st) (m := m) (κ := κ) P') P + sRestart σRestart hE ⟨Z.Q, Z.R, H⟩ hSt hC hNative ht hc hne htc hsrc hcaller hu hN _ ?_ + rintro sTag ⟨σT, hReply, hET, ⟨Q, R', hST⟩, hWT, ⟨xT, hkT, hxT⟩, hOth⟩ + have hStT : StEqA Z.T (L.mem sTag) σT := fun _ ht' _ hx => (hET.at_tcb ht' hx).st + have hstate : xT.tcbState = caller.tcbState := by + have h := congrArg Tcb.tcbState hxT + exact h + have hrunT : runnable xT.tcbState = true := by rw [hstate]; exact hrun + have hv : sched.TS_RUNNING.val = tsCodeA .running := by rw [ts_vals.1]; rfl + refine WP.spec_bind (sc_set_runnable_stateA hST hStT hc hkT hrunT (ts := .running) rfl hv) ?_ + rintro sFinal ⟨σFinal, hrunSpec, HFinal, hStateFinal, _, hFrame⟩ + refine (WP.spec_ok _).2 ⟨rfl, σFinal, ?_, ⟨Q, R', HFinal⟩, hStateFinal, ?_⟩ + · have hFinish : fl4FinishTcbReply true (((frame_registers ++ gp_registers).take count.bv.toNat).map + src.tcbArch.tcbContext.userRegs) σRestart = ({((), σFinal)}, False) := by + unfold fl4FinishTcbReply + rw [nd_bind_oneSc (show (Fl4Spec.NonDet.gets fun s : State Unit => s.curThread) σRestart = + ({(σRestart.curThread, σRestart)}, False) from rfl)] + rw [H.sch.curThread] + rw [nd_bind_oneSc (show (Fl4Spec.NonDet.when true (replyFromKernel (Z := Unit) (specPtr (L.curW sRestart)) + (0, ((frame_registers ++ gp_registers).take count.bv.toNat).map src.tcbArch.tcbContext.userRegs))) σRestart = + ({((), σT)}, False) from hReply)] + exact hrunSpec + unfold fl4PerformInvocation + rw [run_bind_okD (readRegsSpec_noSuspendA hsrc count.bv), run_bind_okD (run_liftE_stD hFinish)] + rfl + · exact (hWT.trans hFrame).mono fun b hb => hb.elim (fun h => Or.inr (Or.inl h)) + (fun h => h.elim (fun h => Or.inl (swr_ksb rfl hWT.1 h)) fun h => Or.inr (Or.inr h)) + +/-- **ReadRegisters Call without source suspension, from the pre-Restart joint state** +(module docstring). -/ +theorem tcbReadRegisters_callJointA + {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + {s sRestart : S} {a : A} {σ : State Unit} + {t flags count : U64} {ctx : tcb.Ctx} {src x : Tcb} {block donate : Bool} + (hst0 : smp.remote_tcb_stall st ar s a t = ok (s, a)) + (hlen : ¬ctx.length < 2#u64) + (hf : tcb.arg st s ctx 0#u64 = ok flags) (hn : tcb.arg st s ctx 1#u64 = ok count) + (hlo : ¬count < 1#u64) (hhi : ¬36#u64 < count) (hself : t ≠ L.curW s) + (hr : tcb.restart_caller st s = ok sRestart) + (hcall : ctx.call = true) (hsuspend : (flags &&& 1#u64) = 0#u64) + (H : SchedSRA st Z m κ s σ) (hst : StEqA Z.T (L.mem s) σ) + (P' : IpcSchedPlacementA Z.th N E R Bf) (P : EPlaced Z.T E R Bf) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + (hoff : ∀ y, IsSlotA Z.th y → ¬ EWrite Z.T E R Bf y ∧ ¬ EWrite Z.T E R Bf (wa y 8#u64)) + (hSt : StateRelA Z.th (L.mem s) σ) (hV : SlotsValidA Z.th (L.mem s)) + (hC : BufCover st Z.T Bf s σ) (hNative : BufNativeA Z.T (L.mem s)) + (ht : t ∈ Z.T) (hsrc : σ.kheap (specPtr t) = some (.tcb src)) + (hx : σ.kheap (specPtr (L.curW s)) = some (.tcb x)) (hrun : runnable x.tcbState = true) + (hu : ∀ rp, ipcBufOfA true x = some rp → ∀ k, k < 128 → inUserFrame (bufAddr rp k) σ) : + tcb.decode st ar s a fl4_abi.label.TCBReadRegisters t ctx ⦃ r => + r.1 = .Ok () ∧ r.2.2 = a ∧ ∃ σRestart σFinal, + setThreadState (Z := Unit) (specPtr (L.curW s)) .restart σ = ({((), σRestart)}, False) ∧ + (.ok [], σFinal) ∈ ((fl4PerformInvocation block true donate + (.invokeTCB (.readRegisters (specPtr t) false count.bv ArchDefaultExtraRegisters))).run σRestart).1 ∧ + IpcSchedRelA st Z m κ r.2.1 σFinal ∧ StEqA Z.T (L.mem r.2.1) σFinal ∧ + WritesIn st sRestart r.2.1 (fun b => SWrA (st := st) Z sRestart b ∨ + EWrite Z.T E R Bf b ∨ b = stA (L.curW sRestart)) ⦄ := by + have hR := restartCaller_jointA H hst P' P HE hoff hSt hV hC hNative hx hrun + rw [hr, WP.spec_ok] at hR + obtain ⟨σR, hspec, HR, -, ER, StR, -, CR, NR, hcur, hkc, hfar⟩ := hR + have hne : t ≠ L.curW sRestart := by rw [hcur]; exact hself + have hpt : specPtr t ≠ specPtr (L.curW s) := fun e => hself (specPtr_inj e) + have hsrcR : σR.kheap (specPtr t) = some (.tcb src) := by rw [hfar _ hpt]; exact hsrc + have hcR : σR.kheap (specPtr (L.curW sRestart)) = some (.tcb { x with tcbState := .restart }) := by + rw [hcur]; exact hkc + have huR : ∀ rp, ipcBufOfA true { x with tcbState := .restart } = some rp → ∀ k, k < 128 → + inUserFrame (bufAddr rp k) σR := by + intro rp hrp k hk + have h := hu rp hrp k hk + unfold inUserFrame at h ⊢ + obtain ⟨sz, hsz⟩ := h + refine ⟨sz, ?_⟩ + have hq : (bufAddr rp k &&& ~~~(mask (pageBitsForSize sz))) ≠ specPtr (L.curW s) := by + intro e + rw [e, hx] at hsz + cases hsz + rw [hfar _ hq] + exact hsz + refine WP.spec_mono (tcbReadRegisters_callPerformJA (block := block) (donate := donate) hst0 hlen hf hn + hlo hhi hself hr hcall hsuspend P' P HR ER StR CR NR ht hne ((HR.thT _).2 HR.curT) hsrcR hcR huR rfl) ?_ + rintro r ⟨h1, h2, σF, hm, hS, hE, hW⟩ + exact ⟨h1, h2, σR, σF, hspec, hm, hS, hE, hW⟩ + +end +end +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/TcbRegViewA.lean b/proofs/Fl4ProofsA64/P3/TcbRegViewA.lean new file mode 100644 index 000000000..8ebff1f88 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbRegViewA.lean @@ -0,0 +1,159 @@ +/- +P3 (AArch64): a register write of a tracked TCB keeps every merged view. + +* **`tcbRegWrite_viewsA`**: writing register `r` of the TCB at `t` (and the abstract user context + at the same register) keeps `TcbIpcViewsA` with the same reply stacks. + +No named premise. +-/ +import Fl4ProofsA64.P3.TcbRegsContA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4SpecA64 Fl4Spec Sched + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + +/-- **A register write keeps the merged views** (module docstring). -/ +theorem tcbRegWrite_viewsA (hP : IPlacedA N Z.T E R Bf) (P : IpcSchedPlacementA Z.th N E R Bf) + {s : S} {σ : State Unit} {stack : U64 → List U64} + (V : TcbIpcViewsA st Z m κ N E R Bf stack s σ) {t : U64} (ht : t ∈ Z.T) {x : Tcb} + (hk : σ.kheap (specPtr t) = some (.tcb x)) (r : Register) (v : U64) : + TcbIpcViewsA st Z m κ N E R Bf stack (L.wr s (regA t (regIdxA r)) v) + (Notif.updK σ (specPtr t) (.tcb (Notif.setReg x r v.bv))) := by + have PE := hP.e + obtain ⟨hE, hW, hStep⟩ := write_reg_rel PE s σ V.ep ht hk r v + have O := schedOpsEAOfPlacementA (st := st) (m := m) (κ := κ) P + have hRel := O.stable _ _ _ _ V.rel hW hStep (NoBlockE.tcbK hk (Notif.setReg x r v.bv) rfl rfl) + have hta := hP.n.tcb t ht + have hal : Aligned (regA t (regIdxA r)) := (a_reg hta r).1 + have hrv : (regA t (regIdxA r)).val = t.val + 256 + 8 * (regIdxA r).val := regA_val hta (regIdxA_lt r) + have hlt := regIdxA_lt r + have hag : ∀ b, Aligned b → b.val ≠ (regA t (regIdxA r)).val → + L.mem (L.wr s (regA t (regIdxA r)) v) b = L.mem s b := + fun b hb hne => agree_wr (L := L) (s := s) (v := v) hal b hb hne + -- words of any tracked TCB other than the written register + have far : ∀ y ∈ Z.T, ∀ K : Nat, K < 4096 → (y = t → 552 ≤ K) → + y.val + K ≠ (regA t (regIdxA r)).val := by + intro y hy K hK hself + rw [hrv] + by_cases e : y = t + · have := hself e + subst e + omega + · have hne : y.val ≠ t.val := fun h => e (UScalar.eq_of_val_eq h) + have h1 := (hP.n.tcb y hy).align + have h2 := hta.align + omega + have tst : ∀ y ∈ Z.T, L.mem (L.wr s (regA t (regIdxA r)) v) (stA y) = L.mem s (stA y) := fun y hy => + hag _ (foot_st hP.n hy).1 (by rw [val_st (hP.n.tcb y hy)]; exact far y hy _ (by omega) (fun _ => by omega)) + have tblk : ∀ y ∈ Z.T, L.mem (L.wr s (regA t (regIdxA r)) v) (tBlockA y) = L.mem s (tBlockA y) := fun y hy => + hag _ (foot_blk hP.n hy).1 (by rw [val_blk (hP.n.tcb y hy)]; exact far y hy _ (by omega) (fun _ => by omega)) + have tprio : ∀ y ∈ Z.T, L.mem (L.wr s (regA t (regIdxA r)) v) (prioA y) = L.mem s (prioA y) := fun y hy => + hag _ (foot_prio hP.n hy).1 (by rw [val_prio (hP.n.tcb y hy)]; exact far y hy _ (by omega) (fun _ => by omega)) + have tprev : ∀ y ∈ Z.T, L.mem (L.wr s (regA t (regIdxA r)) v) (prevA .Ep y) = L.mem s (prevA .Ep y) := + fun y hy => hag _ (foot_prev hP.n hy).1 + (by rw [val_prev (hP.n.tcb y hy)]; exact far y hy _ (by omega) (fun _ => by omega)) + have tnext : ∀ y ∈ Z.T, L.mem (L.wr s (regA t (regIdxA r)) v) (nextA .Ep y) = L.mem s (nextA .Ep y) := + fun y hy => hag _ (foot_next hP.n hy).1 + (by rw [val_next (hP.n.tcb y hy)]; exact far y hy _ (by omega) (fun _ => by omega)) + have tbadge : ∀ y ∈ Z.T, y ≠ t → + L.mem (L.wr s (regA t (regIdxA r)) v) (badgeRegA y) = L.mem s (badgeRegA y) := by + intro y hy hne + refine hag _ (foot_breg hP.n hy).1 ?_ + rw [badgeRegA_eq, tw_val (hP.n.tcb y hy) (by simp)] + exact far y hy _ (by simp) (fun e => absurd e hne) + have nfar : ∀ n ∈ N, ∀ K : U64, K.val < 64 → + (wa n K).val ≠ (regA t (regIdxA r)).val := by + intro n hn K hK + rw [nfield_val (hP.n.ntfn n hn) hK, hrv] + have := hP.n.disj n hn t ht + omega + have RN : Notif.NtfnStateRel N Z.T (L.mem (L.wr s (regA t (regIdxA r)) v)) + (Notif.updK σ (specPtr t) (.tcb (Notif.setReg x r v.bv))) := by + have old := V.ntfn.at_tcb ht hk + refine V.ntfn.upd_tcb ht (Notif.setReg x r v.bv) (fun n hn => ?_) + (fun y hy hne => ⟨tst y hy, tblk y hy, tbadge y hy hne, tprio y hy⟩) + (fun y hy => ⟨tprev y hy, tnext y hy⟩) (Or.inr ⟨tst t ht, tblk t ht⟩) ?_ + · refine ⟨hag _ (foot_head hP.n hn).1 ?_, hag _ (foot_ts hP.n hn).1 ?_, + hag _ (foot_badge hP.n hn).1 ?_, hag _ (foot_bound hP.n hn).1 ?_, hag _ (foot_sc hP.n hn).1 ?_⟩ + · rw [Notif.epHeadA_eq]; exact nfar n hn _ (by simp) + · rw [Notif.epTSA_eq]; exact nfar n hn _ (by simp) + · rw [Notif.nBadgeA_eq]; exact nfar n hn _ (by simp) + · rw [Notif.nBoundA_eq]; exact nfar n hn _ (by simp) + · rw [Notif.nScA_eq]; exact nfar n hn _ (by simp) + · refine ⟨?_, fun n hn => ?_, ?_, ?_⟩ + · rw [tst t ht]; exact old.st + · rw [tblk t ht]; exact old.blk n hn + · show _ = (Notif.setReg x r v.bv).tcbArch.tcbContext.userRegs badge_register + rw [setReg_regs, badgeRegA_eq_regA] + by_cases e : r = badge_register + · subst e + rw [L.mem_wr_same hal, Function.update_self] + · rw [hag _ (a_reg hta badge_register).1 (regA_ne hta (Ne.symm e)), + Function.update_of_ne (Ne.symm e), ← badgeRegA_eq_regA] + exact old.badge + · rw [tprio t ht]; exact old.prio + have tcbKeep : ∀ q y, σ.kheap q = some (.tcb y) → + ∃ y', (Notif.updK σ (specPtr t) (.tcb (Notif.setReg x r v.bv))).kheap q = some (.tcb y') ∧ + y'.tcbMcpriority = y.tcbMcpriority ∧ y'.tcbYieldTo = y.tcbYieldTo ∧ y'.tcbFlags = y.tcbFlags := by + intro q y hy + by_cases e : q = specPtr t + · subst e + rw [hk] at hy + cases hy + exact ⟨Notif.setReg x r v.bv, by rw [Notif.updK_kheap, Function.update_self], rfl, rfl, rfl⟩ + · exact ⟨y, by rw [Notif.updK_kheap, Function.update_of_ne e]; exact hy, rfl, rfl, rfl⟩ + have other : ∀ q o, σ.kheap q = some o → (∀ y, o ≠ .tcb y) → + (Notif.updK σ (specPtr t) (.tcb (Notif.setReg x r v.bv))).kheap q = some o := by + intro q o ho hne + have e : q ≠ specPtr t := by + intro e + rw [e, hk] at ho + exact hne x (Option.some.inj ho).symm + rw [Notif.updK_kheap, Function.update_of_ne e] + exact ho + have HS : ReplyStackRelA (L.mem (L.wr s (regA t (regIdxA r)) v)) R Z.C + (Notif.updK σ (specPtr t) (.tcb (Notif.setReg x r v.bv))) (scNameA m) stack := by + refine V.stk.frame (fun q hq => ?_) (fun c _ y n hy => ?_) (fun q _ y hy => ?_) + · obtain ⟨na, nv⟩ := replyNextPlaceA (Z := Z) PE hq + obtain ⟨pa, pv⟩ := replyPrevPlaceA (Z := Z) PE hq + have := PE.rt q hq t ht + exact ⟨hag _ na (by rw [nv, hrv]; omega), hag _ pa (by rw [pv, hrv]; omega)⟩ + · exact ⟨y, n, other _ _ hy (by intro _ e; cases e), rfl⟩ + · have e : specPtr q ≠ specPtr t := by + intro e + rw [e, Notif.updK_kheap, Function.update_self] at hy + cases hy + rw [Notif.updK_kheap, Function.update_of_ne e] at hy + exact ⟨y, hy, rfl⟩ + have word : ∀ u ∈ Z.T, ∀ K : U64, (K = 3440#u64 ∨ K = 3472#u64 ∨ K = 3456#u64) → + L.mem (L.wr s (regA t (regIdxA r)) v) (wa u K) = L.mem s (wa u K) := by + intro u hu K hK + have hK4 : K.val < 4096 := by rcases hK with rfl | rfl | rfl <;> simp + have hK8 : K.val % 8 = 0 := by rcases hK with rfl | rfl | rfl <;> simp + have hK5 : 552 ≤ K.val := by rcases hK with rfl | rfl | rfl <;> simp + refine hag _ (field_aligned (hP.n.tcb u hu) hK4 hK8) ?_ + rw [tw_val (hP.n.tcb u hu) hK4] + exact far u hu _ hK4 (fun _ => hK5) + obtain ⟨Q, RR, H⟩ := id V.rel + obtain ⟨M, Y⟩ := yieldFrameA V.mcp V.yld + (mem' := L.mem (L.wr s (regA t (regIdxA r)) v)) + (σ' := Notif.updK σ (specPtr t) (.tcb (Notif.setReg x r v.bv))) + (fun u hu => ⟨word u hu 3440#u64 (Or.inl rfl), word u hu 3456#u64 (Or.inr (Or.inr rfl))⟩) + (fun u hu => word u hu 3472#u64 (Or.inr (Or.inl rfl))) + (fun u _ y hy => tcbKeep _ y hy) (fun c hc => by + obtain ⟨y, n, hy, -⟩ := H.toSchedSRN.scr.sc c hc + exact ⟨y, n, y, hy, other _ _ hy (by intro _ e; cases e), rfl⟩) + exact ⟨hRel, RN, hE, HS, M, Y⟩ + +end +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/TcbRegsContA.lean b/proofs/Fl4ProofsA64/P3/TcbRegsContA.lean new file mode 100644 index 000000000..70646adf0 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbRegsContA.lean @@ -0,0 +1,336 @@ +/- +P3 (AArch64): the scheduler continuations of the register invocations (WriteRegisters, +CopyRegisters) over the merged relation. + +* **`TcbRestartReadyA`** / **`TcbSuspendReadyA`**: the caller-side facts of `restart_refinesA` / + `suspend_refinesA` at one state. +* **`tcbRestartIf_contA`**, **`tcbSuspendIf_contA`**: the optional restart / suspend. +* **`tcbRescheduleIf_contA`**: the current-destination reschedule. + +[S]: the facts in `TcbRestartReadyA`, `hidle`. [P]: the platform premises of `restart_refinesA`. +No [J]. The user-memory frame of `rescheduleRequired` is a local copy of `IpcScGlueA`'s private one. +-/ +import Fl4ProofsA64.P3.TcbResumeLocalA +import Fl4ProofsA64.P3.ScSwitchA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4SpecA64 Sched + +noncomputable section + +section UserMemory +open Fl4Spec + +/-- All successful results of an operation keep the specification's user memory. -/ +private def KeepsUserMemory {α : Type} (op : SMonad Unit α) : Prop := + ∀ σ σ' a, (a, σ') ∈ (op σ).1 → σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory + +private theorem KeepsUserMemory.bind {α β : Type} {op : SMonad Unit α} {next : α → SMonad Unit β} + (h : KeepsUserMemory op) (hn : ∀ a, KeepsUserMemory (next a)) : KeepsUserMemory (op >>= next) := by + intro σ σ' b hb + obtain ⟨⟨a, σ₁⟩, ha, hnext⟩ := hb + exact (hn a σ₁ σ' b hnext).trans (h σ σ₁ a ha) + +private theorem KeepsUserMemory.singleton {α : Type} {op : SMonad Unit α} (f : State Unit → α × State Unit) + (he : ∀ σ, (op σ).1 = {f σ}) + (hm : ∀ σ, (f σ).2.machineState.underlyingMemory = σ.machineState.underlyingMemory) : + KeepsUserMemory op := by + intro σ σ' a ha + rw [he, Set.mem_singleton_iff] at ha + exact (congrArg (fun p : α × State Unit => p.2.machineState.underlyingMemory) ha).trans (hm σ) + +private theorem KeepsUserMemory.pure {α : Type} (a : α) : KeepsUserMemory (pure a) := + KeepsUserMemory.singleton (fun σ => (a, σ)) (fun _ => rfl) fun _ => rfl + +private theorem KeepsUserMemory.gets {α : Type} (f : State Unit → α) : KeepsUserMemory (NonDet.gets f) := + KeepsUserMemory.singleton (fun σ => (f σ, σ)) (fun _ => rfl) fun _ => rfl + +private theorem KeepsUserMemory.modify {f : State Unit → State Unit} + (h : ∀ σ, (f σ).machineState.underlyingMemory = σ.machineState.underlyingMemory) : + KeepsUserMemory (NonDet.modify f) := + KeepsUserMemory.singleton (fun σ => ((), f σ)) (fun _ => rfl) h + +private theorem KeepsUserMemory.fail {α : Type} : KeepsUserMemory (NonDet.fail : SMonad Unit α) := by + intro σ σ' a h + exact (Set.notMem_empty _ h).elim + +private theorem KeepsUserMemory.assert (P : Prop) : KeepsUserMemory (NonDet.assert P) := by + intro σ σ' a h + have e : (a, σ') = ((), σ) := h.2 + cases e + rfl + +private theorem KeepsUserMemory.assertOpt {α : Type} (x : Option α) : KeepsUserMemory (NonDet.assertOpt x) := by + cases x with + | none => exact KeepsUserMemory.fail + | some a => exact KeepsUserMemory.pure a + +private theorem KeepsUserMemory.getsThe {α : Type} (f : State Unit → Option α) : + KeepsUserMemory (NonDet.getsThe f) := + fun σ => KeepsUserMemory.assertOpt (f σ) σ + +private theorem KeepsUserMemory.when {P : Prop} {d : Decidable P} {op : SMonad Unit Unit} + (h : KeepsUserMemory op) : KeepsUserMemory (@NonDet.when _ P d op) := by + unfold NonDet.when + split_ifs + · exact h + · exact KeepsUserMemory.pure () + +private theorem KeepsUserMemory.maybeM {α : Type} {f : α → SMonad Unit Unit} (hf : ∀ x, KeepsUserMemory (f x)) + (o : Option α) : KeepsUserMemory (NonDet.maybeM f o) := by + cases o with + | none => exact KeepsUserMemory.pure () + | some x => exact hf x + +private theorem KeepsUserMemory.loop {α : Type} {cond : α → State Unit → Prop} {body : α → SMonad Unit α} + (h : ∀ a, KeepsUserMemory (body a)) (a : α) : KeepsUserMemory (NonDet.whileLoop cond body a) := by + intro σ σ' b hb + change NonDet.WhileResults cond body a σ (b, σ') at hb + suffices aux : ∀ a σ res, NonDet.WhileResults cond body a σ res → + res.2.machineState.underlyingMemory = σ.machineState.underlyingMemory from aux a σ _ hb + intro a σ res hw + induction hw with + | done _ => rfl + | step _ hm _ ih => exact ih.trans (h _ _ _ _ hm) + +private theorem KeepsUserMemory.mapM {α β : Type} {f : α → SMonad Unit β} (h : ∀ a, KeepsUserMemory (f a)) + (xs : List α) : KeepsUserMemory (NonDet.mapM f xs) := by + induction xs with + | nil => exact KeepsUserMemory.pure [] + | cons x xs hi => exact KeepsUserMemory.bind (h x) fun y => + KeepsUserMemory.bind hi fun ys => KeepsUserMemory.pure (y :: ys) + +private theorem KeepsUserMemory.setObject (p : ObjRef) (obj : KernelObject) : + KeepsUserMemory (setObject (Z := Unit) p obj) := by + unfold Fl4SpecA64.setObject getObject + refine KeepsUserMemory.bind (KeepsUserMemory.getsThe _) fun old => ?_ + refine KeepsUserMemory.bind (KeepsUserMemory.assert _) fun _ => ?_ + refine KeepsUserMemory.singleton (fun σ => ((), updKSc σ p obj)) (fun σ => ?_) fun _ => rfl + rw [Notif.nd_bind_one (show NonDet.get σ = ({(σ, σ)}, False) from rfl)] + rfl + +private theorem KeepsUserMemory.updateSc (p : ObjRef) (f : SchedContext → SchedContext) : + KeepsUserMemory (updateSchedContext (Z := Unit) p f) := by + unfold updateSchedContext getObject + refine KeepsUserMemory.bind (KeepsUserMemory.getsThe _) fun old => ?_ + cases old <;> first | exact KeepsUserMemory.setObject _ _ | exact KeepsUserMemory.fail + +private theorem KeepsUserMemory.getSc (p : ObjRef) : KeepsUserMemory (getSchedContext (Z := Unit) p) := by + unfold getSchedContext getObject + refine KeepsUserMemory.bind (KeepsUserMemory.getsThe _) fun old => ?_ + cases old <;> first | exact KeepsUserMemory.pure _ | exact KeepsUserMemory.fail + +private theorem KeepsUserMemory.getSimpleKo {α : Type} [Nonempty α] (tag : α → KernelObject) (p : ObjRef) : + KeepsUserMemory (Fl4SpecA64.getSimpleKo (Z := Unit) tag p) := by + unfold Fl4SpecA64.getSimpleKo getObject + refine KeepsUserMemory.bind (KeepsUserMemory.getsThe _) fun old => + KeepsUserMemory.bind (KeepsUserMemory.assert _) fun _ => ?_ + split + · apply KeepsUserMemory.pure + · exact KeepsUserMemory.fail + +-- Apply the frame rules to successive commands in an unfolded spec operation. +local macro "keep_user_memory" : tactic => `(tactic| + repeat' first + | assumption + | apply KeepsUserMemory.pure + | apply KeepsUserMemory.getsThe + | apply KeepsUserMemory.gets + | apply KeepsUserMemory.assertOpt + | apply KeepsUserMemory.assert + | apply KeepsUserMemory.fail + | exact KeepsUserMemory.modify (fun _ => rfl) + | apply KeepsUserMemory.setObject + | apply KeepsUserMemory.updateSc + | apply KeepsUserMemory.getSc + | apply KeepsUserMemory.getSimpleKo + | apply KeepsUserMemory.when + | (apply KeepsUserMemory.maybeM; intro) + | (apply KeepsUserMemory.loop; intro) + | (apply KeepsUserMemory.mapM; intro) + | refine KeepsUserMemory.bind ?_ (fun _ => ?_)) + +private theorem rescheduleKeepsUserMemory : KeepsUserMemory (rescheduleRequired (Z := Unit)) := by + unfold rescheduleRequired + refine KeepsUserMemory.bind (KeepsUserMemory.gets _) fun act => ?_ + cases act <;> + simp only [tcbSchedAction, getTcbQueue, setTcbQueue, threadGet, getScRefillSufficient, getScRefillReady, + setSchedulerAction] <;> + keep_user_memory + + +/-- `rescheduleRequired` keeps the user memory. -/ +theorem rescheduleUserMemoryA {σ σ' : State Unit} (h : ((), σ') ∈ (rescheduleRequired (Z := Unit) σ).1) : + σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory := + rescheduleKeepsUserMemory σ σ' () h + +end UserMemory + +section +open Fl4Spec +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + +/-- The caller-side facts of `restart_refinesA` at `(s, σ)`. -/ +structure TcbRestartReadyA (st : machine.Store S) [L : LawfulStore st] (Z : XSetsA) (m : McState) + (κ : U64 → U64) (N E R : Set U64) (s : S) (σ : State Unit) (t : U64) (x : Tcb) : Prop where + tcb : σ.kheap (specPtr t) = some (.tcb x) + reply : ∀ r', x.tcbState = .blockedOnReply r' → ∃ r ∈ R, specPtr r = r' ∧ + ∃ xr, σ.kheap r' = some (.reply xr) ∧ xr.replyTcb = some (specPtr t) ∧ ScReplyCovA Z.C m σ r' + ep : ∀ e', epBlocked x.tcbState = some e' → L.mem s (tBlockA t) ∈ E ∧ + ∃ z, σ.kheap e' = some (.endpoint z) ∧ specPtr t ∈ z.epQueue + ntfn : ∀ n', x.tcbState = .blockedOnNotification n' → L.mem s (tBlockA t) ∈ N ∧ + ∃ z, σ.kheap n' = some (.notification z) ∧ specPtr t ∈ z.ntfnObj.ntfnQueue + rep : RepOKA σ (specPtr t) + on : L.mem s (affA t) = L.cpu s + tail : ∀ (s2 : S) (σ2 : State Unit) (Q : U64 → U64 → List U64) (RR : U64 → List U64), + SchedSRB st { Z with Q := Q, R := RR } m κ s2 σ2 (specPtr t) → + (∃ y, σ2.kheap (specPtr t) = some (.tcb y) ∧ y.tcbState = .restart) → + (∀ sc y n, L.mem s2 (scA t) = sc → sc ≠ 0#u64 → + σ2.kheap (scNameA m sc) = some (.schedContext y n) → scActive y = true) ∧ + (L.mem s2 (scA t) ≠ 0#u64 → L.mem s2 (scCoreA (L.mem s2 (scA t))) = L.cpu s2) + +/-- The caller-side facts of `suspend_refinesA` at `(s, σ)`. -/ +structure TcbSuspendReadyA (st : machine.Store S) [L : LawfulStore st] (Z : XSetsA) (m : McState) + (N E R : Set U64) (s : S) (σ : State Unit) (t : U64) (x : Tcb) : Prop where + tcb : σ.kheap (specPtr t) = some (.tcb x) + reply : ∀ r', x.tcbState = .blockedOnReply r' → ∃ r ∈ R, specPtr r = r' ∧ + ∃ xr, σ.kheap r' = some (.reply xr) ∧ xr.replyTcb = some (specPtr t) ∧ ScReplyCovA Z.C m σ r' + ep : ∀ e', epBlocked x.tcbState = some e' → L.mem s (tBlockA t) ∈ E ∧ + ∃ z, σ.kheap e' = some (.endpoint z) ∧ specPtr t ∈ z.epQueue + ntfn : ∀ n', x.tcbState = .blockedOnNotification n' → L.mem s (tBlockA t) ∈ N ∧ + ∃ z, σ.kheap n' = some (.notification z) ∧ specPtr t ∈ z.ntfnObj.ntfnQueue + rep : RepOKA σ (specPtr t) + runW : L.mem s (runVcpuA t) = 0#u64 + runS : x.tcbExt.tcbRunVcpu = none + on : L.mem s (affA t) = L.cpu s + +/-- The relations at two states of one `Z` have the same current thread. -/ +theorem IpcSchedRelA.curThread_eq {s s' : S} {σ σ' : State Unit} (H : IpcSchedRelA st Z m κ s σ) + (H' : IpcSchedRelA st Z m κ s' σ') : σ'.curThread = σ.curThread := by + obtain ⟨Q, RR, H⟩ := H + obtain ⟨Q', RR', H'⟩ := H' + rw [← H.sch.cur, ← H'.sch.cur] + +theorem IpcSchedRelA.curThread_curW {s : S} {σ : State Unit} (H : IpcSchedRelA st Z m κ s σ) : + σ.curThread = specPtr (L.curW s) := by + obtain ⟨Q, RR, H⟩ := H + exact H.sch.curThread + +/-- The optional restart of a register invocation. -/ +theorem tcbRestartIf_contA (hP : IPlacedA N Z.T E R Bf) (P : IpcSchedPlacementA Z.th N E R Bf) + (hc : m.curCore < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hmbF : ∀ a, Fl4MinBudgetAgreeA ar a) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {s : S} {a : A} {σ : State Unit} {stack : U64 → List U64} + (V : TcbIpcViewsA st Z m κ N E R Bf stack s σ) (hC0 : ∀ c ∈ Z.C, c ≠ 0#u64) + {t : U64} (ht : t ∈ Z.T) (resume : Bool) + (hidle : ∀ c < fl4A64Platform.numNodes, specPtr t ≠ m.idleThreadOf c) + (ready : resume = true → ∃ x, TcbRestartReadyA st Z m κ N E R s σ t x) : + (if resume then sched.restart st ar s a t else ok (s, a)) ⦃ r => r.2 = a ∧ ∃ σ' stack', + (Fl4Spec.NonDet.when resume (fl4TcbRestart (Z := Unit) (specPtr t))) σ = ({((), σ')}, False) ∧ + TcbIpcViewsA st Z m κ N E R Bf stack' r.1 σ' ⦄ := by + cases resume with + | false => + rw [if_neg (by simp), WP.spec_ok] + refine ⟨rfl, σ, stack, ?_, V⟩ + unfold Fl4Spec.NonDet.when + rw [if_neg (by simp)] + rfl + | true => + obtain ⟨x, X⟩ := ready rfl + rw [if_pos rfl] + refine WP.spec_mono (restart_refinesA hP P hc hmb hmbF hmax htu s a σ V.rel V.ntfn V.ep V.stk V.mcp V.yld + hC0 ht X.tcb X.reply X.ep X.ntfn X.rep X.on hidle X.tail) ?_ + rintro r ⟨ha, σ', stk', hspec, RN, H, HE, HS, M, Y, -, -⟩ + refine ⟨ha, σ', stk', ?_, ⟨H, RN, HE, HS, M, Y⟩⟩ + unfold Fl4Spec.NonDet.when + rw [if_pos rfl] + exact hspec + +/-- The optional suspension of a register invocation (`fl4CancelRun; suspend`). -/ +theorem tcbSuspendIf_contA (hP : IPlacedA N Z.T E R Bf) (P : IpcSchedPlacementA Z.th N E R Bf) + (hc : m.curCore < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {s : S} {a : A} {σ : State Unit} {stack : U64 → List U64} + (V : TcbIpcViewsA st Z m κ N E R Bf stack s σ) (hC0 : ∀ c ∈ Z.C, c ≠ 0#u64) + {t : U64} (ht : t ∈ Z.T) (susp : Bool) + (ready : susp = true → ∃ x, TcbSuspendReadyA st Z m N E R s σ t x) : + (if susp then sched.suspend st ar s a t else ok (s, a)) ⦃ r => r.2 = a ∧ ∃ σ' stack', + (Fl4Spec.NonDet.when susp (fl4CancelRun (Z := Unit) (specPtr t) >>= fun _ => + Fl4SpecA64.suspend (specPtr t))) σ = ({((), σ')}, False) ∧ + TcbIpcViewsA st Z m κ N E R Bf stack' r.1 σ' ⦄ := by + cases susp with + | false => + rw [if_neg (by simp), WP.spec_ok] + refine ⟨rfl, σ, stack, ?_, V⟩ + unfold Fl4Spec.NonDet.when + rw [if_neg (by simp)] + rfl + | true => + obtain ⟨x, X⟩ := ready rfl + rw [if_pos rfl] + refine WP.spec_mono (suspend_refinesA hP P hc hmb hmax htu s a σ V.rel V.ntfn V.ep V.stk V.mcp V.yld + hC0 ht X.tcb X.reply X.ep X.ntfn X.rep X.runW X.runS X.on) ?_ + rintro r ⟨ha, σ', stk', hspec, RN, H, HE, HS, M, Y, -, -⟩ + refine ⟨ha, σ', stk', ?_, ⟨H, RN, HE, HS, M, Y⟩⟩ + unfold Fl4Spec.NonDet.when + rw [if_pos rfl] + exact hspec + +/-- `sched.reschedule_required` keeps every merged view. -/ +theorem tcbReschedule_viewsA (hP : IPlacedA N Z.T E R Bf) (P : IpcSchedPlacementA Z.th N E R Bf) + {s : S} {σ : State Unit} {stack : U64 → List U64} + (V : TcbIpcViewsA st Z m κ N E R Bf stack s σ) : + sched.reschedule_required st s ⦃ s' => ∃ σ', + rescheduleRequired (Z := Unit) σ = ({((), σ')}, False) ∧ + TcbIpcViewsA st Z m κ N E R Bf stack s' σ' ⦄ := by + obtain ⟨Q0, R0, H0⟩ := id V.rel + have T0 : StEqA Z.T (L.mem s) σ := fun u hu y hy => (V.ep.at_tcb hu hy).st + refine WP.spec_mono (reschedule_required_refinesA (Z := { Z with Q := Q0, R := R0 }) H0 T0) ?_ + rintro s' ⟨σ', Q', hspec, hk, -, hw, H'⟩ + have hmem : σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory := + rescheduleUserMemoryA (by rw [hspec]; exact Set.mem_singleton _) + obtain ⟨RN, HE, HS, M, Y, -⟩ := mcsStepFramesA (Z := { Z with Q := Q0, R := R0 }) hP P H0.toSchedSRN + (MFrA.of_sw hk hw) hmem (fun q y n h => ⟨y, by rw [hk]; exact h, rfl, rfl⟩) V.ntfn V.ep V.stk V.mcp V.yld + exact ⟨σ', hspec, ⟨⟨Q', R0, H'⟩, RN, HE, HS, M, Y⟩⟩ + +/-- The current-destination test and reschedule that end a register invocation. -/ +theorem tcbRescheduleIf_contA (hP : IPlacedA N Z.T E R Bf) (P : IpcSchedPlacementA Z.th N E R Bf) + {s : S} {a : A} {σ : State Unit} {stack : U64 → List U64} + (V : TcbIpcViewsA st Z m κ N E R Bf stack s σ) (dest : U64) : + (if dest = L.curW s then do + let s' ← sched.reschedule_required st s + ok (s', a) + else ok (s, a)) ⦃ r => r.2 = a ∧ ∃ σ', + (Fl4Spec.NonDet.when (specPtr dest = σ.curThread) (rescheduleRequired (Z := Unit))) σ = + ({((), σ')}, False) ∧ + TcbIpcViewsA st Z m κ N E R Bf stack r.1 σ' ⦄ := by + have hcur := V.rel.curThread_curW + by_cases h : dest = L.curW s + · have hs : specPtr dest = σ.curThread := by rw [hcur, h] + rw [if_pos h] + refine WP.spec_bind (tcbReschedule_viewsA hP P V) ?_ + rintro s' ⟨σ', hspec, V'⟩ + rw [WP.spec_ok] + refine ⟨rfl, σ', ?_, V'⟩ + unfold Fl4Spec.NonDet.when + rw [if_pos hs] + exact hspec + · have hs : specPtr dest ≠ σ.curThread := by + rw [hcur]; exact fun e => h (specPtr_inj e) + rw [if_neg h, WP.spec_ok] + refine ⟨rfl, σ, ?_, V⟩ + unfold Fl4Spec.NonDet.when + rw [if_neg hs] + rfl + +end +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/TcbRestartA.lean b/proofs/Fl4ProofsA64/P3/TcbRestartA.lean new file mode 100644 index 000000000..f10cb6853 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbRestartA.lean @@ -0,0 +1,385 @@ +/- +P3 (AArch64): `sched.restart` refines l4v `fl4TcbRestart` over the merged relation. + +Rust order: `is_stopped`; if stopped: `ipc::cancel_ipc`, `set_thread_state Restart`, the sporadic +unblock, `schedcontext::resume`, `is_schedulable`, `possible_switch_to`. + +* **`restartOffInqA`**: the cancellation's writes miss every TCB's queue bits. +* **`SchedSRN.notReady_bitA`**: a clear `Q_QUEUED` bit is non-membership of every spec ready queue. +* **`restart_progA`**: Rust's `restart` as test, cancel, write and `restartTailProgA`. +* **`restart_refinesA`**: the composition. + +[S] premises: `hon` (`t` on the current core), `hidle` (`t` is no core's idle thread), `tailS` +(at the post-`Restart` state a bound SC of `t` is on the current core; for `restart_refinesA` also active). [P]: the +platform premises of `ipcSchedOpsA` (`hc`, `hmb`, `hmax`, `htu`) and `hmbF`. The other premises +are the cancellation's caller-side tracking facts (`suspendCancelA`). No [J]. +-/ +import Fl4ProofsA64.P3.TcbRestartFrameA +import Fl4ProofsA64.P3.TcbRestartTailInactA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4SpecA64 Fl4Spec Sched + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + +/-- The cancellation misses the TCBs' queue bits. -/ +theorem restartOffInqA {s : S} {σ : State Unit} (H : SchedSRN st Z m κ s σ) + (hP : IPlacedA N Z.T E R Bf) {u K : U64} (hu : u ∈ Z.T) + (hK : K = 3592#u64) : + ¬ CancelReplyWA N Z.T Z.C E R Bf (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) (wa u K) := by + have PE := hP.e + have hua := H.core.tcbs u hu + have hKv : K.val = 3592 := by + subst hK; simp + have hv : (wa u K).val = u.val + K.val := Notif.tfield_val hua (by omega) + have hty := (H.thT u).2 hu + -- a word of a TCB of `T` at one of these offsets + have tcb_off : ∀ t ∈ Z.T, ∀ k : Nat, k < 4096 → (wa u K).val = t.val + k → k = K.val := by + intro t ht k hk e + rw [hv] at e + by_cases htu : t = u + · subst t; omega + · have h := H.place.apart t u .tcb .tcb ((H.thT t).2 ht) hty htu + change t.val + 4096 ≤ u.val ∨ u.val + 4096 ≤ t.val at h + omega + have hin : u.val ≤ (wa u K).val ∧ (wa u K).val < u.val + 4096 := by omega + rintro ((h | h | h) | ⟨q, hq, h⟩ | ⟨sc, hc, h⟩) + · rcases h with ⟨e, he, h⟩ | ⟨r, hr, h⟩ | ⟨t, ht, h | h⟩ | h + · have := PE.et e he u hu; omega + · have := PE.rt r hr u hu; omega + · obtain ⟨h1, h2⟩ := h + have hk := tcb_off t ht ((wa u K).val - t.val) (by unfold TWOffN at h2; omega) (by omega) + rw [hk] at h2 + unfold TWOffN at h2 + omega + · rcases h with h | h + · have := tcb_off t ht 3496 (by omega) h; omega + · have := tcb_off t ht 3504 (by omega) h; omega + · have := PE.bt _ h u hu; omega + · rcases h with ⟨n, hn, h1, h2⟩ | ⟨t, ht, h⟩ + · have hpn : Z.th n = some (.other 64) → False := fun hp => by + have hne : n ≠ u := by + rintro rfl + rw [hty] at hp + cases hp + have h := H.place.apart n u (.other 64) .tcb hp hty hne + change n.val + 64 ≤ u.val ∨ u.val + 4096 ≤ n.val at h + omega + have := hP.n.disj n hn u hu; omega + · have hta := PE.tcb t ht + have hval : (wa u K).val = t.val + 3328 ∨ (wa u K).val = t.val + 3336 ∨ (wa u K).val = t.val + 256 ∨ + (wa u K).val = t.val + 3496 ∨ (wa u K).val = t.val + 3504 ∨ (wa u K).val = t.val + 3376 := by + rcases h with h | h | h | h | h | h <;> + rw [h] <;> + (try simp only [stA_eq, Notif.tBlockA_eq, Notif.badgeRegA_eq, Notif.nextEp_eq, Notif.prevEp_eq]) <;> + rw [Notif.tfield_val hta] <;> simp + rcases hval with h | h | h | h | h | h <;> + · have := tcb_off t ht _ (by omega) h; omega + · have e : wa u K = L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION := h + have hK' : (wa u K).val < (L.ksb s).val ∨ (L.ksb s).val + ksSizeA ≤ (wa u K).val := by + have h := H.place.ks u _ hty (fun irq => by + have := H.place.irqK hty irq + cases this) + change u.val + 4096 ≤ (L.ksb s).val ∨ _ at h + omega + have hn := H.node_in H.sch.cpuCs (o := layout.node.SCHED_ACTION) (by simp [layout.node.SCHED_ACTION]) + rw [e] at hK' + omega + · have PEq := replyNextPlaceA (Z := Z) PE hq + have PEp := replyPrevPlaceA (Z := Z) PE hq + have := PE.rt q hq u hu + rcases h with h | h + · rw [h] at hin; omega + · rw [h] at hin; omega + · have hfoot : ScFootA Z.C m σ (wa u K) := by + obtain ⟨y, k, hk, hx⟩ := H.scr.sc sc hc + refine ⟨sc, hc, y, k, hk, ?_⟩ + have h32 := hx.hdr (K := 32#u64) (by simp) (by simp) + have ee : scReplyA sc = scWA sc 32#u64 := by unfold scReplyA scWA layout.sc.REPLY; rfl + rw [h, ee] + exact hx.in_obj h32.2 + have hd := H.scp.th_out hfoot hty + change (wa u K).val < u.val ∨ u.val + 4096 ≤ (wa u K).val at hd + omega + +/-- A clear `Q_QUEUED` bit is non-membership of every spec ready queue. -/ +theorem _root_.Fl4ProofsA64.P3.Sched.SchedSRN.notReady_bitA {s : S} {σ : State Unit} (H : SchedSRN st Z m κ s σ) {t : U64} + (ht : t ∈ Z.T) (h0 : (L.mem s (inqA t) &&& sched.Q_QUEUED) = 0#u64) : + ∀ d p, specPtr t ∉ σ.readyQueues d p := by + intro d p + by_cases hd : d = 0 + · subst d + let p' : U64 := UScalar.mk (BitVec.ofNat 64 p.toNat) + have hp' : p'.val = p.toNat := by + change p.toNat % 2^64 = p.toNat + exact Nat.mod_eq_of_lt (by have := p.isLt; omega) + have e : BitVec.ofNat 8 p'.val = p := by + apply BitVec.eq_of_toNat_eq + rw [BitVec.toNat_ofNat, hp'] + exact Nat.mod_eq_of_lt p.isLt + have hq := H.rest.rq p' (by rw [hp']; exact p.isLt) + rw [← e, hq, mem_map_specPtrSc] + exact H.core.notQ ht h0 H.sch.cpuCs (by rw [hp']; exact p.isLt) + · rw [H.rest.rqDom _ _ hd] + exact List.not_mem_nil + +variable (st ar) in +/-- Rust's `restart` after the inlining of the sporadic unblock (module docstring). -/ +theorem restart_progA (s : S) (a : A) (t : U64) : + sched.restart st ar s a t = (do + let b ← sched.is_stopped st s t + if b then + let r ← ipc.cancel_ipc st ar s a t + let s2 ← sched.set_thread_state st r.1 t sched.TS_RESTART + restartTailProgA st ar s2 r.2 t + else ok (s, a)) := by + have hsc : ∀ s', obj.Tcb.sc st t s' = ok (L.mem s' (scA t)) := + fun s' => eq_of_spec_fd (obj.Tcb.sc.spec (L := L) s' t) + unfold sched.restart restartTailProgA notification.unblock_sporadic + simp only [hsc, bind_ok, Std.bind_assoc] + rfl + +/-- `restart_refinesA` with the two spec steps that reach the post-`Restart` state handed to `tailS`. -/ +theorem restart_refinesAtA (hP : IPlacedA N Z.T E R Bf) (P : IpcSchedPlacementA Z.th N E R Bf) + (hc : m.curCore < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hmbF : ∀ a, Fl4MinBudgetAgreeA ar a) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (s : S) (a : A) (σ : State Unit) {stack : U64 → List U64} + (H : IpcSchedRelA st Z m κ s σ) (RN : NtfnStateRel N Z.T (L.mem s) σ) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + (M : McpRelA Z.T m (L.mem s) σ) (Y : YieldRelA Z.T Z.C m (L.mem s) σ) (hC0 : ∀ c ∈ Z.C, c ≠ 0#u64) + {t : U64} (ht : t ∈ Z.T) {x : Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hReply : ∀ r', x.tcbState = .blockedOnReply r' → ∃ r ∈ R, specPtr r = r' ∧ + ∃ xr, σ.kheap r' = some (.reply xr) ∧ xr.replyTcb = some (specPtr t) ∧ ScReplyCovA Z.C m σ r') + (hE : ∀ e', epBlocked x.tcbState = some e' → L.mem s (tBlockA t) ∈ E ∧ + ∃ z, σ.kheap e' = some (.endpoint z) ∧ specPtr t ∈ z.epQueue) + (hN : ∀ n', x.tcbState = .blockedOnNotification n' → L.mem s (tBlockA t) ∈ N ∧ + ∃ z, σ.kheap n' = some (.notification z) ∧ specPtr t ∈ z.ntfnObj.ntfnQueue) + (hrep : RepOKA σ (specPtr t)) + (hon : L.mem s (affA t) = L.cpu s) + (hidle : ∀ c < fl4A64Platform.numNodes, specPtr t ≠ m.idleThreadOf c) + (tailS : ∀ (s2 : S) (σ2 : State Unit) (Q : U64 → U64 → List U64) (RR : U64 → List U64) (σc : State Unit), + SchedSRB st { Z with Q := Q, R := RR } m κ s2 σ2 (specPtr t) → + (∃ y, σ2.kheap (specPtr t) = some (.tcb y) ∧ y.tcbState = .restart) → + cancelIpc (Z := Unit) (specPtr t) σ = ({((), σc)}, False) → + setThreadState (Z := Unit) (specPtr t) .restart σc = ({((), σ2)}, False) → + (L.mem s2 (scA t) ≠ 0#u64 → L.mem s2 (scCoreA (L.mem s2 (scA t))) = L.cpu s2)) : + sched.restart st ar s a t ⦃ out => out.2 = a ∧ ∃ σ' stack', + fl4TcbRestart (Z := Unit) (specPtr t) σ = ({((), σ')}, False) ∧ + NtfnStateRel N Z.T (L.mem out.1) σ' ∧ IpcSchedRelA st Z m κ out.1 σ' ∧ + EpStateRel Z.T E R Bf (L.mem out.1) σ' ∧ + ReplyStackRelA (L.mem out.1) R Z.C σ' (scNameA m) stack' ∧ + McpRelA Z.T m (L.mem out.1) σ' ∧ YieldRelA Z.T Z.C m (L.mem out.1) σ' ∧ + StEqA Z.T (L.mem out.1) σ' ∧ + (¬runnable x.tcbState = true ∧ ¬idle x.tcbState → + ∃ y, σ'.kheap (specPtr t) = some (.tcb y) ∧ y.tcbState = .restart) ⦄ := by + have ops := ipcSchedOpsA (st := st) (ar := ar) (κ := κ) hP P hc hmb hmax htu + have hSched : CancelScheduleA st N Z.T (IpcSchedRelA st Z m κ) := cancelScheduleA_of_placement P + have PE := hP.e + have T0 : StEqA Z.T (L.mem s) σ := fun u hu y hy => (HE.at_tcb hu hy).st + obtain ⟨b, hb, hbiff⟩ := isStopped_eqA (st := st) (s := s) T0 ht hk + rw [restart_progA st ar s a t, hb, bind_ok] + have hspec0 : fl4TcbRestart (Z := Unit) (specPtr t) σ = + (Fl4Spec.NonDet.when (¬runnable x.tcbState ∧ ¬idle x.tcbState) (do + cancelIpc (specPtr t) + setThreadState (specPtr t) .restart + let sc ← getTcbObjRef Tcb.tcbSchedContext (specPtr t) + ifSporadicCurScTestRefillUnblockCheck sc + NonDet.maybeM fl4SchedContextResume sc + testPossibleSwitchTo (specPtr t)) : SMonad Unit Unit) σ := by + unfold fl4TcbRestart + rw [nd_bind_oneSc (Notif.getThreadState_eq hk)] + cases b with + | false => + rw [if_neg (by simp), WP.spec_ok] + have hns : ¬(¬runnable x.tcbState = true ∧ ¬idle x.tcbState) := fun h => by + have := hbiff.2 h + cases this + refine ⟨rfl, σ, stack, ?_, RN, H, HE, HS, M, Y, T0, fun h => absurd h hns⟩ + rw [hspec0] + unfold Fl4Spec.NonDet.when + rw [if_neg hns] + rfl + | true => + have hstop := hbiff.1 rfl + rw [if_pos rfl] + obtain ⟨Q0, R0, H0⟩ := id H + -- the thread is in no queue before the cancellation + have hnqs0 : ∀ d p, specPtr t ∉ σ.readyQueues d p := by + intro d p hm + obtain ⟨x', hk', hr⟩ := H0.run.1 d p _ hm + rw [hk] at hk' + cases hk' + exact hstop.1 hr + have hnrel0 : specPtr t ∉ σ.releaseQueue := by + intro hm + obtain ⟨x', hk', hr⟩ := H0.run.2 _ hm + rw [hk] at hk' + cases hk' + exact hstop.1 hr + have hq0 : (L.mem s (inqA t) &&& sched.Q_QUEUED) = 0#u64 := H0.toSchedSRN.qBit0 ht hon hnqs0 + have hr0 : (L.mem s (inqA t) &&& sched.Q_RELEASE) = 0#u64 := (H0.toSchedSRN.relOn ht hon).2 hnrel0 + -- cancel_ipc + refine WP.spec_bind (suspendCancelA ops.1 hSched ops.2.2.1 hP P s a σ H RN HE HS ht hk hReply hE hN hrep hC0) ?_ + rintro r ⟨ha, σ1, stk1, hc1, RN1, H1, HE1, HS1, w1, K1, x1, hk1, hfree⟩ + have hoff : ∀ u ∈ Z.T, ∀ K : U64, (K = 3440#u64 ∨ K = 3472#u64 ∨ K = 3520#u64) → + L.mem r.1 (wa u K) = L.mem s (wa u K) := by + intro u hu K hK + refine w1.mem_eq (field_aligned (H0.core.tcbs u hu) ?_ ?_) (suspendOffTcbA H0.toSchedSRN hP hu hK) + · rcases hK with rfl | rfl | rfl <;> simp + · rcases hK with rfl | rfl | rfl <;> simp + have hoffF : ∀ u ∈ Z.T, L.mem r.1 (flagsA u) = L.mem s (flagsA u) := fun u hu => + w1.mem_eq (field_aligned (H0.core.tcbs u hu) (K := 3456#u64) (by decide) (by decide)) + (suspendOffFlagsA H0.toSchedSRN hP hu) + have hoffQ : L.mem r.1 (inqA t) = L.mem s (inqA t) := + w1.mem_eq (field_aligned (K := 3592#u64) (H0.core.tcbs t ht) (by simp) (by simp)) + (restartOffInqA H0.toSchedSRN hP ht rfl) + have htcb1 : ∀ u ∈ Z.T, ∀ y, σ.kheap (specPtr u) = some (.tcb y) → + ∃ y', σ1.kheap (specPtr u) = some (.tcb y') ∧ y'.tcbMcpriority = y.tcbMcpriority ∧ + y'.tcbYieldTo = y.tcbYieldTo ∧ y'.tcbFlags = y.tcbFlags := by + intro u _ y hy + obtain ⟨y1, hy1, e1⟩ := K1.tcb _ _ hy + exact ⟨y1, hy1, by rw [e1], by rw [e1], by rw [e1]⟩ + have hsc1 : ∀ c ∈ Z.C, ∃ y n y', σ.kheap (scNameA m c) = some (.schedContext y n) ∧ + σ1.kheap (scNameA m c) = some (.schedContext y' n) ∧ y'.scYieldFrom = y.scYieldFrom := by + intro c hc' + obtain ⟨y, n, hy, -⟩ := H0.scr.sc c hc' + obtain ⟨y', hy', e⟩ := K1.sc _ _ _ hy + exact ⟨y, n, y', hy, hy', by rw [e]⟩ + obtain ⟨M1, Y1⟩ := yieldFrameA (mem' := L.mem r.1) (σ' := σ1) M Y + (fun u hu => ⟨hoff u hu 3440#u64 (Or.inl rfl), hoffF u hu⟩) + (fun u hu => hoff u hu 3472#u64 (Or.inr (Or.inl rfl))) + htcb1 hsc1 + -- set Restart + obtain ⟨Q1, R1, H1'⟩ := H1 + have T1 : StEqA Z.T (L.mem r.1) σ1 := fun u hu y hy => (HE1.at_tcb hu hy).st + have hv : sched.TS_RESTART.val = tsCodeA .restart := by unfold sched.TS_RESTART; rfl + refine WP.spec_bind (WP.spec_and + (set_thread_state_exAnyA (Z := { Z with Q := Q1, R := R1 }) H1' T1 ht hk1 (ts := .restart) rfl hv) + (restartStateIpcA (Z := Z) hP P (Q := Q1) (RR := R1) (H1'.toB (specPtr t)) T1 RN1 HE1 HS1 ht hk1 hfree)) ?_ + rintro s2 ⟨⟨σ2, hsts, hkh2, H2, T2, w2⟩, RN2, E2, S2⟩ + have hσ2 : σ2 = stsActA (updKSc σ1 (specPtr t) (.tcb { x1 with tcbState := .restart })) (specPtr t) := + congrArg Prod.snd (Set.singleton_eq_singleton_iff.1 + (congrArg Prod.fst (hsts.symm.trans (setThreadState_stsActExA hk1 _)))) + rw [← hσ2] at RN2 E2 S2 + have h2t : σ2.kheap (specPtr t) = some (.tcb { x1 with tcbState := .restart }) := by + rw [hkh2, updKSc_same] + have h2far : ∀ q, q ≠ specPtr t → σ2.kheap q = σ1.kheap q := fun q hq => by + rw [hkh2, updKSc_kheap, Function.update_of_ne hq] + have off2 : ∀ u ∈ Z.T, ∀ K : U64, (K = 3440#u64 ∨ K = 3472#u64 ∨ K = 3520#u64 ∨ K = 3592#u64 ∨ K = 3456#u64) → + L.mem s2 (wa u K) = L.mem r.1 (wa u K) := by + intro u hu K hK + have hua := PE.tcb u hu + have hKv : K.val = 3440 ∨ K.val = 3472 ∨ K.val = 3520 ∨ K.val = 3592 ∨ K.val = 3456 := by + rcases hK with rfl | rfl | rfl | rfl | rfl <;> simp + have hv' : (wa u K).val = u.val + K.val := Notif.tfield_val hua (by omega) + refine w2.mem_eq (field_aligned (H1'.core.tcbs u hu) (by omega) + (by rcases hK with rfl | rfl | rfl | rfl | rfl <;> simp)) (fun h => ?_) + rcases h with h | h + · have h' := congrArg UScalar.val h + rw [hv', val_st (hP.n.tcb t ht)] at h' + by_cases e : u = t + · subst u; omega + · have := tcb_disj PE ht hu e; omega + · have hty := (H1'.thT u).2 hu + have hK' : (wa u K).val < (L.ksb r.1).val ∨ (L.ksb r.1).val + ksSizeA ≤ (wa u K).val := by + have g := H1'.place.ks u _ hty (fun irq => by + have := H1'.place.irqK hty irq + cases this) + change u.val + 4096 ≤ (L.ksb r.1).val ∨ _ at g + omega + have hn := H1'.node_in H1'.sch.cpuCs (o := layout.node.SCHED_ACTION) (by simp [layout.node.SCHED_ACTION]) + rw [h] at hK' + omega + obtain ⟨M2, Y2⟩ := yieldFrameA (mem' := L.mem s2) (σ' := σ2) M1 Y1 + (fun u hu => ⟨off2 u hu 3440#u64 (Or.inl rfl), off2 u hu 3456#u64 (Or.inr (Or.inr (Or.inr (Or.inr rfl))))⟩) + (fun u hu => off2 u hu 3472#u64 (Or.inr (Or.inl rfl))) + (fun u _ y hy => by + by_cases e : specPtr u = specPtr t + · rw [e, hk1] at hy + cases hy + exact ⟨{ x1 with tcbState := .restart }, by rw [e]; exact h2t, rfl, rfl, rfl⟩ + · exact ⟨y, by rw [h2far _ e]; exact hy, rfl, rfl, rfl⟩) + (fun c hc' => by + obtain ⟨y, n, hy, -⟩ := H1'.scr.sc c hc' + exact ⟨y, n, y, hy, by rw [h2far _ (fun h => by rw [h, hk1] at hy; cases hy)]; exact hy, rfl⟩) + -- the SC tail + have hon2 : L.mem s2 (affA t) = L.cpu s2 := by + have e1 : L.mem s2 (affA t) = L.mem s (affA t) := + (off2 t ht 3520#u64 (Or.inr (Or.inr (Or.inl rfl)))).trans (hoff t ht 3520#u64 (Or.inr (Or.inr rfl))) + rw [e1, w2.2.1, w1.2.1] + exact hon + have hq2 : L.mem s2 (inqA t) = L.mem s (inqA t) := + (off2 t ht 3592#u64 (Or.inr (Or.inr (Or.inr (Or.inl rfl))))).trans hoffQ + have hnqs2 : ∀ d p, specPtr t ∉ σ2.readyQueues d p := + H2.toSchedSRN.notReady_bitA ht (by rw [hq2]; exact hq0) + have hnrel2 : specPtr t ∉ σ2.releaseQueue := + (H2.toSchedSRN.relOn ht hon2).1 (by rw [hq2]; exact hr0) + have hcpu := tailS s2 σ2 Q1 R1 σ1 H2 ⟨_, h2t, rfl⟩ hc1 hsts + rw [ha] + refine WP.spec_mono (restartTailAnyA (ar := ar) (a1 := a) H2 T2 ht h2t rfl hon2 (hmbF a) hidle hnqs2 hnrel2 + hcpu) ?_ + rintro out ⟨ho, σ3, Q', R', htail, H3, T3, F⟩ + obtain ⟨RN3, E3, S3, M3, Y3, -⟩ := mcsStepFramesA (Z := { Z with Q := Q1, R := R1 }) hP P + H2.toSchedSRN F.fr F.mem F.sc RN2 E2 S2 M2 Y2 + refine ⟨ho, σ3, stk1, ?_, RN3, ⟨Q', R', H3⟩, E3, S3, M3, Y3, T3, fun _ => ?_⟩ + · rw [hspec0] + unfold Fl4Spec.NonDet.when + rw [if_pos hstop, nd_bind_oneSc hc1, nd_bind_oneSc hsts] + change (threadGet Tcb.tcbSchedContext (specPtr t) >>= _) σ2 = _ + rw [nd_bind_oneSc (threadGet_eqSc _ h2t)] + exact htail + · obtain ⟨y, hy, e⟩ := F.tcb _ h2t + exact ⟨y, hy, e⟩ + +/-- **`sched.restart` refines `fl4TcbRestart`** (module docstring). -/ +theorem restart_refinesA (hP : IPlacedA N Z.T E R Bf) (P : IpcSchedPlacementA Z.th N E R Bf) + (hc : m.curCore < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hmbF : ∀ a, Fl4MinBudgetAgreeA ar a) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (s : S) (a : A) (σ : State Unit) {stack : U64 → List U64} + (H : IpcSchedRelA st Z m κ s σ) (RN : NtfnStateRel N Z.T (L.mem s) σ) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + (M : McpRelA Z.T m (L.mem s) σ) (Y : YieldRelA Z.T Z.C m (L.mem s) σ) (hC0 : ∀ c ∈ Z.C, c ≠ 0#u64) + {t : U64} (ht : t ∈ Z.T) {x : Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hReply : ∀ r', x.tcbState = .blockedOnReply r' → ∃ r ∈ R, specPtr r = r' ∧ + ∃ xr, σ.kheap r' = some (.reply xr) ∧ xr.replyTcb = some (specPtr t) ∧ ScReplyCovA Z.C m σ r') + (hE : ∀ e', epBlocked x.tcbState = some e' → L.mem s (tBlockA t) ∈ E ∧ + ∃ z, σ.kheap e' = some (.endpoint z) ∧ specPtr t ∈ z.epQueue) + (hN : ∀ n', x.tcbState = .blockedOnNotification n' → L.mem s (tBlockA t) ∈ N ∧ + ∃ z, σ.kheap n' = some (.notification z) ∧ specPtr t ∈ z.ntfnObj.ntfnQueue) + (hrep : RepOKA σ (specPtr t)) + (hon : L.mem s (affA t) = L.cpu s) + (hidle : ∀ c < fl4A64Platform.numNodes, specPtr t ≠ m.idleThreadOf c) + (tailS : ∀ (s2 : S) (σ2 : State Unit) (Q : U64 → U64 → List U64) (RR : U64 → List U64), + SchedSRB st { Z with Q := Q, R := RR } m κ s2 σ2 (specPtr t) → + (∃ y, σ2.kheap (specPtr t) = some (.tcb y) ∧ y.tcbState = .restart) → + (∀ sc y n, L.mem s2 (scA t) = sc → sc ≠ 0#u64 → + σ2.kheap (scNameA m sc) = some (.schedContext y n) → scActive y = true) ∧ + (L.mem s2 (scA t) ≠ 0#u64 → L.mem s2 (scCoreA (L.mem s2 (scA t))) = L.cpu s2)) : + sched.restart st ar s a t ⦃ out => out.2 = a ∧ ∃ σ' stack', + fl4TcbRestart (Z := Unit) (specPtr t) σ = ({((), σ')}, False) ∧ + NtfnStateRel N Z.T (L.mem out.1) σ' ∧ IpcSchedRelA st Z m κ out.1 σ' ∧ + EpStateRel Z.T E R Bf (L.mem out.1) σ' ∧ + ReplyStackRelA (L.mem out.1) R Z.C σ' (scNameA m) stack' ∧ + McpRelA Z.T m (L.mem out.1) σ' ∧ YieldRelA Z.T Z.C m (L.mem out.1) σ' ∧ + StEqA Z.T (L.mem out.1) σ' ∧ + (¬runnable x.tcbState = true ∧ ¬idle x.tcbState → + ∃ y, σ'.kheap (specPtr t) = some (.tcb y) ∧ y.tcbState = .restart) ⦄ := + restart_refinesAtA (st := st) (ar := ar) (κ := κ) hP P hc hmb hmbF hmax htu s a σ H RN HE HS M Y hC0 ht hk hReply hE hN hrep hon hidle (fun s2 σ2 Q RR _ h h' _ _ => (tailS s2 σ2 Q RR h h').2) + +end +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/TcbRestartFrameA.lean b/proofs/Fl4ProofsA64/P3/TcbRestartFrameA.lean new file mode 100644 index 000000000..27953bd0b --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbRestartFrameA.lean @@ -0,0 +1,79 @@ +/- +P3 (AArch64): the IPC views across one MCS scheduler step. + +**`mcsStepFramesA`**: a step with the frame bundle `MFrA` (FL4 writes in the scheduler words and +SC footprint; the spec changes only TCB SC links and SC objects), the same user memory and the +same `scReplies` / `scYieldFrom` of every SC, keeps the notification and endpoint relations, the +reply stack, `McpRelA`, `YieldRelA` and `StEqA`. It is the common frame of the `sched.restart` +tail (sporadic unblock, resume or postpone, possible switch). + +No named premise. +-/ +import Fl4ProofsA64.P3.TcbRestartViewsA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4SpecA64 Fl4Spec Sched + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + +/-- **The IPC views across one MCS step** (module docstring). -/ +theorem mcsStepFramesA {s s' : S} {σ σ' : State Unit} {stack : U64 → List U64} + (hP : IPlacedA N Z.T E R Bf) (P : IpcSchedPlacementA Z.th N E R Bf) + (H : SchedSRN st Z m κ s σ) (F : MFrA (st := st) Z m s s' σ σ') + (hmem : σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory) + (hsc : ∀ q y n, σ.kheap q = some (.schedContext y n) → + ∃ y', σ'.kheap q = some (.schedContext y' n) ∧ y'.scReplies = y.scReplies ∧ + y'.scYieldFrom = y.scYieldFrom) + (RN : NtfnStateRel N Z.T (L.mem s) σ) (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + (M : McpRelA Z.T m (L.mem s) σ) (Y : YieldRelA Z.T Z.C m (L.mem s) σ) : + NtfnStateRel N Z.T (L.mem s') σ' ∧ EpStateRel Z.T E R Bf (L.mem s') σ' ∧ + ReplyStackRelA (L.mem s') R Z.C σ' (scNameA m) stack ∧ McpRelA Z.T m (L.mem s') σ' ∧ + YieldRelA Z.T Z.C m (L.mem s') σ' ∧ StEqA Z.T (L.mem s') σ' := by + have PE := hP.e + have fn : Notif.FootEq N Z.T (L.mem s) (L.mem s') := fun b ha hb => + F.wr.mem_eq ha (notifFootOffMWrA H P.notification hb) + have KN : Notif.KView σ σ' := by + refine ⟨fun p x hx => F.keep.obj p _ hx (by intro _ _ e; cases e) (by intro _ e; cases e), ?_⟩ + intro p x hx + obtain ⟨y, hy, o, rfl⟩ := F.keep.tcb p x hx + exact ⟨_, hy, rfl, rfl, rfl⟩ + have RN' := RN.frame hP.n fn KN + obtain ⟨FE, VIEW⟩ := epFrameOfMcsA H P F hmem + have HE' := HE.frame PE FE VIEW + have HS' := mcsReplyFrameA H P PE HE HS F (fun p x n h => + let ⟨y, hy, e, _⟩ := hsc p x n h + ⟨y, hy, e⟩) + have off : ∀ u ∈ Z.T, ∀ K : U64, (K = 3328#u64 ∨ K = 3440#u64 ∨ K = 3472#u64) → + L.mem s' (wa u K) = L.mem s (wa u K) := by + intro u hu K hK + refine F.wr.mem_eq (field_aligned (H.core.tcbs u hu) ?_ ?_) (yieldTcbNotMWrA H hu hK) + · rcases hK with rfl | rfl | rfl <;> simp + · rcases hK with rfl | rfl | rfl <;> simp + have htcb : ∀ u ∈ Z.T, ∀ x, σ.kheap (specPtr u) = some (.tcb x) → + ∃ x', σ'.kheap (specPtr u) = some (.tcb x') ∧ x'.tcbMcpriority = x.tcbMcpriority ∧ + x'.tcbYieldTo = x.tcbYieldTo ∧ x'.tcbFlags = x.tcbFlags := by + intro u _ x hx + obtain ⟨y, hy, o, rfl⟩ := F.keep.tcb _ x hx + exact ⟨_, hy, rfl, rfl, rfl⟩ + have offF : ∀ u ∈ Z.T, L.mem s' (flagsA u) = L.mem s (flagsA u) := fun u hu => + F.wr.mem_eq (field_aligned (H.core.tcbs u hu) (K := 3456#u64) (by decide) (by decide)) (yieldFlagsNotMWrA H hu) + obtain ⟨M', Y'⟩ := yieldFrameA M Y + (fun u hu => ⟨off u hu 3440#u64 (Or.inr (Or.inl rfl)), offF u hu⟩) + (fun u hu => off u hu 3472#u64 (Or.inr (Or.inr rfl))) htcb (fun c hc => by + obtain ⟨y, n, hy, -⟩ := H.scr.sc c hc + obtain ⟨y', hy', -, e⟩ := hsc _ y n hy + exact ⟨y, n, y', hy, hy', e⟩) + exact ⟨RN', HE', HS', M', Y', fun u hu y hy => (HE'.at_tcb hu hy).st⟩ + +end +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/TcbRestartResumeA.lean b/proofs/Fl4ProofsA64/P3/TcbRestartResumeA.lean new file mode 100644 index 000000000..82ab04f78 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbRestartResumeA.lean @@ -0,0 +1,148 @@ +/- +P3 (AArch64): the SC resume tail of `sched.restart`, with the branch exposed. + +`restartScResume_fl4A` / `restartScResume_fromTargetA` are `tcbScResume_fl4A` / +`tcbResumeSc_fromTargetA` (TcbInvokeResumeA, TcbInvokeScBacklinkA) with the same proofs, except +that the unchanged outcome is reported as `r.1 = s ∧ σ' = σ` together with the fact that a +schedulable bound thread then has a ready and FL4-sufficient head (the original returns only a +caller relation `R`, which cannot carry that), and that `R` is required only after a postpone. + +No named premise. +-/ +import Fl4ProofsA64.P3.TcbInvokeScBacklinkA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4Spec Fl4SpecA64 Sched + +noncomputable section +variable {S A : Type} {st : machine.Store S} [L : LawfulStore st] {ar : machine.Arch A} + +theorem restartScResume_fl4A {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {a : A} {σ : State Unit} + (H : SchedSRN st Z m κ s σ) {q t : U64} (hq : q ≠ 0#u64) (ht : t ≠ 0#u64) + (hc : q ∈ Z.C) {sc : SchedContext} {n : Nat} + (hk : σ.kheap (scNameA m q) = some (.schedContext sc n)) + (hbt : sc.scTcb = some (specPtr t)) (htcb : obj.Sc.tcb st q s = ok t) + (hsched : sched.is_schedulable st s t = ok (schedulable (specPtr t) σ)) + {x : Tcb} (hkt : σ.kheap (specPtr t) = some (.tcb x)) + (hact : schedulable (specPtr t) σ = true → scActive sc = true) + (hnq : specPtr t ∉ σ.readyQueues x.tcbDomain x.tcbPriority) + (hm : Fl4MinBudgetAgreeA ar a) + (R : S → State Unit → Prop) + (hp : sched.postpone st s q ⦃ s' => ∃ σ', postpone (scNameA m q) σ = ({((), σ')}, False) ∧ R s' σ' ⦄) : + schedcontext.resume st ar s a q ⦃ r => r.2 = a ∧ ∃ σ', + fl4SchedContextResume (scNameA m q) σ = ({((), σ')}, False) ∧ ((r.1 = s ∧ σ' = σ ∧ (schedulable (specPtr t) σ = true → refillReady σ.curTime (refillHd sc) = true ∧ fl4RefillSufficient 0 (refillHd sc) = true)) ∨ R r.1 σ') ⦄ := by + have hspec := tcbResumeSc_boundA hk hbt hkt (H.inv.ne hk) hact hnq + unfold schedcontext.resume + simp only [eq_of_spec_fd (obj.Sc.is_null.spec _), hq, decide_false, Bool.false_eq_true, ite_false, + bind_ok, htcb, eq_of_spec_fd (obj.Tcb.is_null.spec _), ht, hsched] + by_cases hs : schedulable (specPtr t) σ = true + swap + · rw [if_neg hs, WP.spec_ok] + exact ⟨rfl, σ, hspec.trans (if_neg (fun h => hs h.1)), Or.inl ⟨rfl, rfl, fun h => absurd h hs⟩⟩ + rw [if_pos hs] + have ha := hact hs + have hne := H.inv.ne hk ha + have late (hb : (refillReady σ.curTime (refillHd sc) && + fl4RefillSufficient 0 (refillHd sc)) = false) : + (do let s' ← sched.postpone st s q; ok (s', a)) ⦃ r => r.2 = a ∧ ∃ σ', + fl4SchedContextResume (scNameA m q) σ = ({((), σ')}, False) ∧ ((r.1 = s ∧ σ' = σ ∧ (schedulable (specPtr t) σ = true → refillReady σ.curTime (refillHd sc) = true ∧ fl4RefillSufficient 0 (refillHd sc) = true)) ∨ R r.1 σ') ⦄ := by + refine WP.spec_bind hp fun s' ⟨σ', hσ, hR'⟩ => ?_ + rw [WP.spec_ok] + exact ⟨rfl, σ', hspec.trans ((if_pos ⟨hs, hb⟩).trans hσ), Or.inr hR'⟩ + refine WP.spec_bind (ready_refinesA H.scr H.sch.time hc hk ha) ?_ + intro b hb + rw [getScRefillReady_eqA hk hne] at hb + have he : refillReady σ.curTime (refillHd sc) = b := + (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst hb))).1 + cases b + · simp only [Bool.false_eq_true, ite_false] + exact late (by rw [he, Bool.false_and]) + · simp only [ite_true] + refine WP.spec_bind (fl4_sufficient_refinesA a H.scr hc hk ha 0#u64 hm) ?_ + intro enough henough + cases enough + · simp only [Bool.false_eq_true, ite_false] + exact late (by rw [he, Bool.true_and]; exact henough) + · change fl4RefillSufficient (0 : Data) (refillHd sc) = true at henough + simp only [ite_true, WP.spec_ok] + exact ⟨trivial, σ, hspec.trans (if_neg (by rw [he, Bool.true_and, henough]; simp)), Or.inl ⟨trivial, rfl, fun _ => ⟨he, henough⟩⟩⟩ + +/-- At the restart or bind resume site, the live TCB link identifies a + reciprocal SC. The decoded queue exclusion discharges the MCS assert. -/ +theorem restartScResume_fromTargetA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {a : A} {σ : State Unit} + (H : SchedSRN st Z m κ s σ) (hst : StEqA Z.T (L.mem s) σ) {t : U64} + (ht : t ∈ Z.T) (hon : L.mem s (affA t) = L.cpu s) + (hQ : (L.mem s (inqA t) &&& sched.Q_QUEUED) = 0#u64) (hm : Fl4MinBudgetAgreeA ar a) + (R : S → State Unit → Prop) + (hp : + let q := L.mem s (scA t) + q ≠ 0#u64 → sched.postpone st s q ⦃ s' => ∃ σ', + postpone (scNameA m q) σ = ({((), σ')}, False) ∧ R s' σ' ⦄) : + schedcontext.resume st ar s a (L.mem s (scA t)) ⦃ r => r.2 = a ∧ ∃ σ', + (do + let p ← getTcbObjRef Tcb.tcbSchedContext (specPtr t) + NonDet.maybeM fl4SchedContextResume p) σ = ({((), σ')}, False) ∧ ((r.1 = s ∧ σ' = σ ∧ (L.mem s (scA t) ≠ 0#u64 → ∀ y n, σ.kheap (scNameA m (L.mem s (scA t))) = some (.schedContext y n) → schedulable (specPtr t) σ = true → refillReady σ.curTime (refillHd y) = true ∧ fl4RefillSufficient 0 (refillHd y) = true)) ∨ R r.1 σ') ⦄ := by + obtain ⟨x, hkt, hx⟩ := H.sch.tcb t ht + let q := L.mem s (scA t) + have hx' : x.tcbSchedContext = optScA m q := hx + have hRead : getTcbObjRef Tcb.tcbSchedContext (specPtr t) σ = ({(x.tcbSchedContext, σ)}, False) := + threadGet_eqSc _ hkt + by_cases h0 : q = 0#u64 + · have hn : x.tcbSchedContext = none := by rw [hx', h0, optScA, if_pos rfl] + change schedcontext.resume st ar s a q ⦃ _ ⦄ + rw [h0, tcbScResume_nullA, WP.spec_ok] + refine ⟨rfl, σ, ?_, Or.inl ⟨rfl, rfl, fun h => absurd h0 h⟩⟩ + rw [nd_bind_oneSc hRead, hn] + rfl + have ht0 : t ≠ 0#u64 := (tcb_ne01Sc (H.core.tcbs t ht)).1 + have hc : q ∈ Z.C := H.rest.scMem t ht h0 + have hxp : x.tcbSchedContext = some (scNameA m q) := by + rw [hx', optScA, if_neg h0, scNameA_eq] + obtain ⟨y, ny, hky, hym⟩ := H.scr.sc q hc + obtain ⟨z, nz, hkz, hz⟩ := H.inv.back _ _ _ hkt hxp + rw [hky] at hkz + cases hkz + have hTcbWord : L.mem s (scTcbA q) = t := by + have he := hym.tcb + rw [← scTcbA_eqSc, hz] at he + by_cases h : L.mem s (scTcbA q) = 0#u64 + · rw [h, optRefS_zero] at he + cases he + · rw [optRefS_ne h] at he + exact specPtr_inj (Option.some.inj he).symm + have hScRead : obj.Sc.tcb st q s = ok t := + (eq_of_spec_fd (obj.Sc.tcb.spec (L := L) s q)).trans (congrArg ok hTcbWord) + have hnq : specPtr t ∉ σ.readyQueues x.tcbDomain x.tcbPriority := by + obtain ⟨w, hkw, hd, _, hPriority⟩ := H.rest.prio t ht + rw [hkt] at hkw + cases hkw + have hRp : BitVec.ofNat 8 (L.mem s (prioA t)).val = x.tcbPriority := by + apply BitVec.eq_of_toNat_eq + rw [BitVec.toNat_ofNat, hPriority] + exact Nat.mod_eq_of_lt x.tcbPriority.isLt + rw [hd, ← hRp, H.rest.rq _ (H.core.prio t ht), mem_map_specPtrSc, ← hon] + exact fun h => (H.core.queued t ht).2 h hQ + have hSchedEq := schedulable_eqA H ht (H.relOn ht hon) hst + have hSched : sched.is_schedulable st s t = ok (schedulable (specPtr t) σ) := by + rw [is_schedulable_eqA, hSchedEq] + have hAct : schedulable (specPtr t) σ = true → scActive y = true := by + intro h + have hp0 := (of_decide_eq_true (hSchedEq.trans h)).2.2.1 + change L.rmax s q > 0#u64 at hp0 + rw [gt_iff_valSc, hym.max] at hp0 + change 0 < y.scRefillMax at hp0 + simpa only [scActive, activeSc, decide_eq_true_eq] using hp0 + change schedcontext.resume st ar s a q ⦃ _ ⦄ + refine WP.spec_mono (restartScResume_fl4A H h0 ht0 hc hky hz hScRead hSched hkt hAct hnq hm R (hp h0)) ?_ + rintro r ⟨ha, σ', hr, hD⟩ + refine ⟨ha, σ', ?_, ?_⟩ + · rw [nd_bind_oneSc hRead, hxp] + exact hr + · rcases hD with ⟨e1, e2, hc'⟩ | hR' + · exact Or.inl ⟨e1, e2, fun _ y n hy hs' => by rw [hky] at hy; cases hy; exact hc' hs'⟩ + · exact Or.inr hR' + +end +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/TcbRestartTailA.lean b/proofs/Fl4ProofsA64/P3/TcbRestartTailA.lean new file mode 100644 index 000000000..fea9ee417 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbRestartTailA.lean @@ -0,0 +1,329 @@ +/- +P3 (AArch64): the SC tail of `sched.restart` from the relaxed relation after the `Restart` write. + +After `set_thread_state t TS_RESTART` only `released_ipc_queues` except at `t` holds +(`SchedSRB … (specPtr t)`). **`restartTailA`** runs Rust's `notification::unblock_sporadic t`, +`schedcontext::resume`, `is_schedulable` and `possible_switch_to` and restores `SchedSRA`: + +* the sporadic unblock is `unblock_sporadic_SRB` (`t` is in no release queue, so its release + hypothesis is vacuous); +* the resume is `restartScResume_fromTargetA`: either the heap is unchanged and a schedulable + bound thread has a ready, FL4-sufficient head (`ScInvA.suff` gives `releasedSc`, so `SchedSRA` + holds), or `postponeTime_refinesA` puts `t` in the release queue; +* the final test and switch is `switch_if_refinesA`. + +`RTPostA` collects what the caller's frames need: the frame bundle `MFrA`, the user memory, the +SC replies and yield links, and the state of `t`'s TCB. + +[S] premises, at the start state: `hon` (`t` on the current core), `hidle` (`t` is no core's idle +thread), `active` and `hcpu` (a bound SC of `t` is active and on the current core). No [J]. +-/ +import Fl4ProofsA64.P3.TcbRestartResumeA +import Fl4ProofsA64.P3.ScRestartUnbA +import Fl4ProofsA64.P3.ScDonA +import Fl4ProofsA64.P3.ScWakeDonA +import Fl4ProofsA64.P3.IpcScGlueA +import Fl4ProofsA64.P3.IpcScEndpointRefillA +import Fl4ProofsA64.P3.ScYieldFrameA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4Spec Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S A : Type} {st : machine.Store S} [L : LawfulStore st] {ar : machine.Arch A} + +variable (st ar) in +/-- The Rust tail of `sched.restart` after the `Restart` write (the sporadic unblock is +`notification::unblock_sporadic`). -/ +def restartTailProgA (s2 : S) (a1 : A) (t : U64) : Result (S × A) := do + let s3 ← notification.unblock_sporadic st s2 t + let (s4, a2) ← schedcontext.resume st ar s3 a1 (L.mem s2 (scA t)) + let b ← sched.is_schedulable st s4 t + if b then + let s5 ← sched.possible_switch_to st s4 t + ok (s5, a2) + else ok (s4, a2) + +/-- The spec tail of `fl4TcbRestart` after the `Restart` write and the SC read. -/ +def restartTailSpecA (t : ObjRef) (sc : Option ObjRef) : SMonad Unit Unit := do + ifSporadicCurScTestRefillUnblockCheck sc + NonDet.maybeM fl4SchedContextResume sc + testPossibleSwitchTo t + +/-- What the caller's frames need after the tail (module docstring). -/ +structure RTPostA (Z : XSetsA) (m : McState) (t : U64) (s2 s : S) (σ2 σ : State Unit) : Prop where + fr : MFrA (st := st) Z m s2 s σ2 σ + mem : σ.machineState.underlyingMemory = σ2.machineState.underlyingMemory + sc : ∀ q y n, σ2.kheap q = some (.schedContext y n) → + ∃ y', σ.kheap q = some (.schedContext y' n) ∧ y'.scReplies = y.scReplies ∧ y'.scYieldFrom = y.scYieldFrom + tcb : ∀ x', σ2.kheap (specPtr t) = some (.tcb x') → + ∃ x3, σ.kheap (specPtr t) = some (.tcb x3) ∧ x3.tcbState = x'.tcbState + +theorem RTPostA.refl (Z : XSetsA) (m : McState) (t : U64) (s : S) (σ : State Unit) : + RTPostA (st := st) Z m t s s σ σ := + ⟨MFrA.refl Z m s σ, rfl, fun _ y _ h => ⟨y, h, rfl, rfl⟩, fun x' h => ⟨x', h, rfl⟩⟩ + +theorem RTPostA.trans {Z Z' : XSetsA} {m : McState} {t : U64} {s1 s2 s3 : S} {σ1 σ2 σ3 : State Unit} + (F1 : RTPostA (st := st) Z m t s1 s2 σ1 σ2) (F2 : RTPostA (st := st) Z' m t s2 s3 σ2 σ3) + (hC : Z'.C = Z.C) (hT : Z'.T = Z.T) : RTPostA (st := st) Z m t s1 s3 σ1 σ3 := + ⟨F1.fr.trans F2.fr hC hT, F2.mem.trans F1.mem, fun q y n h => by + obtain ⟨y1, h1, e1, e2⟩ := F1.sc q y n h + obtain ⟨y2, h2, e3, e4⟩ := F2.sc q y1 n h1 + exact ⟨y2, h2, e3.trans e1, e4.trans e2⟩, fun x' h => by + obtain ⟨x1, h1, e1⟩ := F1.tcb x' h + obtain ⟨x2, h2, e2⟩ := F2.tcb x1 h1 + exact ⟨x2, h2, e2.trans e1⟩⟩ + +theorem RTPostA.of_sw {Z : XSetsA} {m : McState} {t : U64} {s s' : S} {σ σ' : State Unit} + (hk : σ'.kheap = σ.kheap) (hw : WritesIn st s s' (SWrA (st := st) Z s)) + (hm : σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory) : + RTPostA (st := st) Z m t s s' σ σ' := + ⟨MFrA.of_sw hk hw, hm, fun _ y _ h => ⟨y, by rw [hk]; exact h, rfl, rfl⟩, + fun x' h => ⟨x', by rw [hk]; exact h, rfl⟩⟩ + +/-- Two `SchedSRN` states over the same `m` agree on a thread's affinity. -/ +theorem SchedSRN.aff_eqN {Z Z' : XSetsA} {m : McState} {κ : U64 → U64} {s s' : S} {σ σ' : State Unit} + (H : SchedSRN st Z m κ s σ) (H' : SchedSRN st Z' m κ s' σ') {t : U64} (ht : t ∈ Z.T) (ht' : t ∈ Z'.T) : + L.mem s' (affA t) = L.mem s (affA t) := by + have h1 := H.sch.aff t ht + have h2 := H'.sch.aff t ht' + rw [h1] at h2 + injection h2 with h2 + exact UScalar.eq_of_val_eq h2.symm + +theorem SchedSRN.cpu_eqN {Z Z' : XSetsA} {m : McState} {κ : U64 → U64} {s s' : S} {σ σ' : State Unit} + (H : SchedSRN st Z m κ s σ) (H' : SchedSRN st Z' m κ s' σ') : L.cpu s' = L.cpu s := + UScalar.eq_of_val_eq (H'.sch.core.symm.trans H.sch.core) + +/-- The sporadic unblock keeps the thread's affinity and SC words and its state word's relation. -/ +theorem UnbFrA.restartPre {Z : XSetsA} {m : McState} {κ : U64 → U64} {s s' : S} {σ σ' : State Unit} + (H : SchedSRN st Z m κ s σ) (F : UnbFrA (st := st) Z m s s' σ σ') {t : U64} (ht : t ∈ Z.T) + (hon : L.mem s (affA t) = L.cpu s) (hst : StEqA Z.T (L.mem s) σ) : + L.mem s' (affA t) = L.cpu s' ∧ StEqA Z.T (L.mem s') σ' ∧ L.mem s' (scA t) = L.mem s (scA t) ∧ + (∀ x, σ.kheap (specPtr t) = some (.tcb x) → σ'.kheap (specPtr t) = some (.tcb x)) := by + have hr56 : layout.node.REPROGRAM.val = 56 := by unfold layout.node.REPROGRAM; rfl + obtain ⟨-, -, tK⟩ := H.keepW F.wr (o0 := layout.node.REPROGRAM) (by rw [hr56]; omega) (fun b hb => hb) + have back : ∀ u ∈ Z.T, ∀ y, σ'.kheap (specPtr u) = some (.tcb y) → σ.kheap (specPtr u) = some (.tcb y) := by + intro u hu y hy + obtain ⟨x0, hx0, -⟩ := H.sch.tcb u hu + rw [F.obj _ _ hx0 (fun _ _ h => by cases h)] at hy + rw [hx0, hy] + refine ⟨?_, fun u hu y hy => ?_, tK t ht 3464#u64 (by simp) (by simp), + fun x hx => F.obj _ _ hx (fun _ _ h => by cases h)⟩ + · rw [F.wr.2.1, show affA t = wa t 3520#u64 from rfl, tK t ht 3520#u64 (by simp) (by simp)]; exact hon + · rw [stA_eq, tK u hu 3328#u64 (by simp) (by simp), ← stA_eq]; exact hst u hu y (back u hu y hy) + +/-- **The SC tail of `sched.restart`** (module docstring). -/ +theorem restartTailA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s2 : S} {a1 : A} {σ2 : State Unit} {t : U64} + (H : SchedSRB st Z m κ s2 σ2 (specPtr t)) (hst : StEqA Z.T (L.mem s2) σ2) (ht : t ∈ Z.T) + {x : Tcb} (hkx : σ2.kheap (specPtr t) = some (.tcb x)) (hrs : x.tcbState = .restart) + (hon : L.mem s2 (affA t) = L.cpu s2) (hmb : Fl4MinBudgetAgreeA ar a1) + (hidle : ∀ c < fl4A64Platform.numNodes, specPtr t ≠ m.idleThreadOf c) + (hnqs : ∀ d p, specPtr t ∉ σ2.readyQueues d p) (hnrel : specPtr t ∉ σ2.releaseQueue) + (active : ∀ sc y n, L.mem s2 (scA t) = sc → sc ≠ 0#u64 → + σ2.kheap (scNameA m sc) = some (.schedContext y n) → scActive y = true) + (hcpu : L.mem s2 (scA t) ≠ 0#u64 → L.mem s2 (scCoreA (L.mem s2 (scA t))) = L.cpu s2) : + restartTailProgA st ar s2 a1 t ⦃ out => out.2 = a1 ∧ ∃ σ3 Q' R', + restartTailSpecA (specPtr t) x.tcbSchedContext σ2 = ({((), σ3)}, False) ∧ + SchedSRA st { Z with Q := Q', R := R' } m κ out.1 σ3 ∧ StEqA Z.T (L.mem out.1) σ3 ∧ + RTPostA (st := st) Z m t s2 out.1 σ2 σ3 ⦄ := by + obtain ⟨x0, hx0, hxs⟩ := H.sch.tcb t ht + have e0 : x0 = x := by + rw [hkx] at hx0 + exact (KernelObject.tcb.inj (Option.some.inj hx0)).symm + rw [e0] at hxs + unfold restartTailProgA + -- the sporadic unblock + refine WP.spec_bind (unblock_sporadic_SRB H ht false (fun h => absurd h hnrel)) ?_ + rintro s3 ⟨σ3, h3, H3, F, G⟩ + obtain ⟨hon3, hst3, hqw, hk3f⟩ := UnbFrA.restartPre H.toSchedSRN F ht hon hst + have hkx3 := hk3f x hkx + have hnqs3 : ∀ d p, specPtr t ∉ σ3.readyQueues d p := by rw [F.rq]; exact hnqs + have hnrel3 : specPtr t ∉ σ3.releaseQueue := by rw [F.rel]; exact hnrel + have hpU : ∀ p, optScA m (L.mem s2 (scA t)) = some p → ∃ y n, σ2.kheap p = some (.schedContext y n) := by + intro p hp + by_cases h0 : L.mem s2 (scA t) = 0#u64 + · rw [optScA, if_pos h0] at hp + cases hp + · rw [optScA, if_neg h0] at hp + obtain ⟨y, n, hk, -⟩ := H.scr.sc _ (H.rest.scMem t ht h0) + exact ⟨y, n, by rw [← Option.some.inj hp]; exact hk⟩ + have hmem3 : σ3.machineState.underlyingMemory = σ2.machineState.underlyingMemory := + (Endp.condUnblockReplyListsA false hpU (by rw [h3]; exact Set.mem_singleton _)).mem + have RT1 : RTPostA (st := st) Z m t s2 s3 σ2 σ3 := + ⟨F.mfr H.toSchedSRN, hmem3, fun q y n h => by + obtain ⟨y', h', e⟩ := G q y n h + exact ⟨y', h', by rw [e], by rw [e]⟩, + fun x' h => ⟨x', F.obj _ _ h (fun _ _ e => by cases e), rfl⟩⟩ + have hq0 : (L.mem s3 (inqA t) &&& sched.Q_QUEUED) = 0#u64 := H3.toSchedSRN.qBit0 ht hon3 hnqs3 + have hr0 : (L.mem s3 (inqA t) &&& sched.Q_RELEASE) = 0#u64 := (H3.toSchedSRN.relOn ht hon3).2 hnrel3 + have hxs3 : x.tcbSchedContext = optScA m (L.mem s3 (scA t)) := by rw [hqw]; exact hxs + have hact3 : ∀ y n, σ3.kheap (scNameA m (L.mem s3 (scA t))) = some (.schedContext y n) → + L.mem s3 (scA t) ≠ 0#u64 → scActive y = true := by + intro y n hy h0 + rw [hqw] at hy h0 + obtain ⟨y2, n2, hy2, -⟩ := H.scr.sc _ (H.rest.scMem t ht h0) + obtain ⟨y3, hy3, ey⟩ := G _ y2 n2 hy2 + have hyy : KernelObject.schedContext y3 n2 = .schedContext y n := Option.some.inj (hy3.symm.trans hy) + injection hyy with e1 _ + have e3 : scActive y3 = scActive y2 := by rw [ey] + rw [← e1, e3] + exact active _ y2 n2 rfl h0 hy2 + have hpost : L.mem s3 (scA t) ≠ 0#u64 → sched.postpone st s3 (L.mem s3 (scA t)) ⦃ s' => ∃ σ', + postpone (scNameA m (L.mem s3 (scA t))) σ3 = ({((), σ')}, False) ∧ + (∃ R', SchedSRA st { Z with R := R' } m κ s' σ' ∧ StEqA Z.T (L.mem s') σ' ∧ + RTPostA (st := st) Z m t s3 s' σ3 σ' ∧ specPtr t ∈ σ'.releaseQueue) ⦄ := by + intro h0 + have hc : L.mem s3 (scA t) ∈ Z.C := H3.rest.scMem t ht h0 + obtain ⟨y, n, hky, hym⟩ := H3.scr.sc _ hc + have hxs3' : x.tcbSchedContext = some (scNameA m (L.mem s3 (scA t))) := by + rw [hxs3, optScA, if_neg h0] + rfl + have htw : L.mem s3 (scTcbA (L.mem s3 (scA t))) = t := by + obtain ⟨y', n', hy', hb'⟩ := H3.inv.back _ x _ hkx3 hxs3' + rw [hky] at hy' + cases hy' + have e := hym.tcb + rw [hb', ← scTcbA_eqSc] at e + by_cases hw0 : L.mem s3 (scTcbA (L.mem s3 (scA t))) = 0#u64 + · rw [hw0, optRefS_zero] at e + cases e + · rw [optRefS_ne hw0] at e + exact specPtr_inj (Option.some.inj e).symm + have hta : (L.rmax s3 (L.mem s3 (scA t))).val ≠ 0 := by + have hya := hact3 y n hky h0 + rw [hym.max] + simp [scActive, activeSc] at hya + omega + have hcpu3 : L.mem s3 (scCoreA (L.mem s3 (scA t))) = L.cpu s3 := by + have h02 : L.mem s2 (scA t) ≠ 0#u64 := by rw [← hqw]; exact h0 + have c3 := H3.scCore _ hc + have c2 := H.scCore _ (H.rest.scMem t ht h02) + exact c3.trans ((congrArg κ hqw).trans (c2.symm.trans ((hcpu h02).trans F.wr.2.1.symm))) + have hrun3 : ∀ x', σ3.kheap (specPtr t) = some (.tcb x') → runnable x'.tcbState = true := by + intro x' h + rw [hkx3] at h + cases h + rw [hrs] + rfl + refine WP.spec_mono (postponeTime_refinesA (st := st) H3 hc ht hon3 htw rfl h0 hcpu3 hq0 hr0 hta hnqs3 hrun3) ?_ + rintro s' ⟨σ', R', e, hk, ha, hct, hq, hr, hsub, hs, w, HS, hms, hcons, hidl⟩ + refine ⟨σ', e, R', HS, ?_, ?_, hr⟩ + · intro u hu y hy + rw [hs u hu] + exact hst3 u hu y (by rw [← hk]; exact hy) + · refine RTPostA.of_sw hk (w.mono fun b hb => hb.elim (fun hz => ?_) (fun e => ?_)) (by rw [hms]) + · exact H3.toSchedSRN.qfoot_swr (fun v hv => (List.mem_cons.1 hv).elim (· ▸ ht) + (fun e => (H3.relAct v e).1)) H3.sch.cpuCs ht hz + · rw [e] + exact H3.toSchedSRN.repr_swr + have hresume := restartScResume_fromTargetA (ar := ar) (a := a1) H3.toSchedSRN hst3 ht hon3 hq0 hmb + (fun s' σ' => ∃ R', SchedSRA st { Z with R := R' } m κ s' σ' ∧ StEqA Z.T (L.mem s') σ' ∧ + RTPostA (st := st) Z m t s3 s' σ3 σ' ∧ specPtr t ∈ σ'.releaseQueue) hpost + rw [← hqw] + refine WP.spec_bind hresume ?_ + rintro ⟨s4, a2⟩ ⟨ha2, σ4, hres4, hcase⟩ + have ha2' : a2 = a1 := ha2 + obtain ⟨R4, H4, hst4, RT2⟩ : ∃ R4, SchedSRA st { Z with R := R4 } m κ s4 σ4 ∧ StEqA Z.T (L.mem s4) σ4 ∧ + RTPostA (st := st) Z m t s3 s4 σ3 σ4 := by + rcases hcase with ⟨e1, e2, hcond⟩ | ⟨R', HS, hstS, RT, -⟩ + · have e1' : s4 = s3 := e1 + rw [e1', e2] + have hrel : RelIfBoundSc σ3 (specPtr t) := by + intro x' scp y' n' h1 h2 h3' h4 h5 + rw [hkx3] at h1 + obtain rfl := KernelObject.tcb.inj (Option.some.inj h1) + have h0 : L.mem s3 (scA t) ≠ 0#u64 := by + intro h0 + rw [hxs3, h0, optScA, if_pos rfl] at h2 + cases h2 + have h2' : x.tcbSchedContext = some (scNameA m (L.mem s3 (scA t))) := by + rw [hxs3, optScA, if_neg h0] + rfl + have hscp : scp = scNameA m (L.mem s3 (scA t)) := Option.some.inj (h2.symm.trans h2') + subst hscp + have hc : L.mem s3 (scA t) ∈ Z.C := H3.rest.scMem t ht h0 + obtain ⟨y, n, hky, hym⟩ := H3.scr.sc _ hc + have hyy : y = y' := (KernelObject.schedContext.inj (Option.some.inj (hky.symm.trans h3'))).1 + subst hyy + have hSE := schedulable_eqA H3.toSchedSRN ht (H3.toSchedSRN.relOn ht hon3) hst3 + have hsch : Fl4SpecA64.schedulable (specPtr t) σ3 = true := by + rw [← hSE] + apply decide_eq_true + refine ⟨(runnable_iffA (hst3 t ht x hkx3)).2 (by rw [hrs]; rfl), h0, ?_, + (H3.toSchedSRN.relOn ht hon3).2 hnrel3⟩ + rw [gt_iff_valSc, hym.max] + change 0 < y.scRefillMax + simpa only [scActive, activeSc, decide_eq_true_eq] using h4 + have hb := hcond h0 y n hky hsch + exact ⟨hb.1, ScInvA.suff H3.inv hky h4 (H3.inv.tcbScNotIdle _ x _ hkx3 h2' hidle)⟩ + exact ⟨Z.R, H3.toA hrel, hst3, RTPostA.refl Z m t s3 σ3⟩ + · exact ⟨R', HS, hstS, RT⟩ + have hon4 : L.mem s4 (affA t) = L.cpu s4 := by + rw [SchedSRN.aff_eqN H3.toSchedSRN H4.toSchedSRN ht ht, hon3] + exact (SchedSRN.cpu_eqN H3.toSchedSRN H4.toSchedSRN).symm + have hSE4 := schedulable_eqA H4.toSchedSRN ht (H4.toSchedSRN.relOn ht hon4) hst4 + have fin : ∀ (s5 : S) (σ5 : State Unit) (Q' : U64 → U64 → List U64), + testPossibleSwitchTo (Z := Unit) (specPtr t) σ4 = ({((), σ5)}, False) → σ5.kheap = σ4.kheap → + WritesIn st s4 s5 (SWrA (st := st) Z s4) → + σ5.machineState.underlyingMemory = σ4.machineState.underlyingMemory → + SchedSRA st { Z with Q := Q', R := R4 } m κ s5 σ5 → + ((s5, a2).2 = a1 ∧ ∃ σ3 Q' R', + restartTailSpecA (specPtr t) x.tcbSchedContext σ2 = ({((), σ3)}, False) ∧ + SchedSRA st { Z with Q := Q', R := R' } m κ (s5, a2).1 σ3 ∧ StEqA Z.T (L.mem (s5, a2).1) σ3 ∧ + RTPostA (st := st) Z m t s2 (s5, a2).1 σ2 σ3) := by + intro s5 σ5 Q' hSw hk5 hw5 hmem5 H5 + have RT3 : RTPostA (st := st) Z m t s4 s5 σ4 σ5 := RTPostA.of_sw hk5 hw5 hmem5 + have hst5 : StEqA Z.T (L.mem s5) σ5 := fun u hu y hy => by + have e : L.mem s5 (wa u 3328#u64) = L.mem s4 (wa u 3328#u64) := + hw5.mem_eq (field_aligned (H4.core.tcbs u hu) (by simp) (by simp)) + (fun hb => yieldTcbNotMWrA H4.toSchedSRN hu (Or.inl rfl) (Or.inr hb)) + rw [stA_eq, e, ← stA_eq] + exact hst4 u hu y (by rw [← hk5]; exact hy) + refine ⟨ha2', σ5, Q', R4, ?_, H5, hst5, (RT1.trans RT2 rfl rfl).trans RT3 rfl rfl⟩ + have hU : ifSporadicCurScTestRefillUnblockCheck (Z := Unit) x.tcbSchedContext σ2 = ({((), σ3)}, False) := by + rw [hxs] + exact h3 + have hres4' := hres4 + rw [nd_bind_oneSc (show getTcbObjRef Tcb.tcbSchedContext (specPtr t) σ3 = ({(x.tcbSchedContext, σ3)}, False) + from threadGet_eqSc _ hkx3)] at hres4' + unfold restartTailSpecA + rw [nd_bind_oneSc hU, nd_bind_oneSc hres4'] + exact hSw + refine let_pairSc s4 a2 (fun s4 a2 => do + let b ← sched.is_schedulable st s4 t + if b then + let s5 ← sched.possible_switch_to st s4 t + ok (s5, a2) + else ok (s4, a2)) _ ?_ + beta_reduce + rw [is_schedulable_eqA, bind_ok, hSE4] + by_cases hs : Fl4SpecA64.schedulable (specPtr t) σ4 = true + · rw [if_pos hs] + refine WP.spec_bind (possible_switch_to_refinesA H4 t ht hon4 (schedulable_partsA hs).1 hst4) ?_ + rintro s5 ⟨σ5, Q', hspec, hk5, hw5, H5⟩ + rw [WP.spec_ok] + refine fin s5 σ5 Q' ?_ hk5 hw5 ?_ H5 + · unfold testPossibleSwitchTo + rw [nd_gets_bindSc] + unfold Fl4Spec.NonDet.when + rw [if_pos hs] + exact hspec + · exact Endp.switchKeepsUserMemory (specPtr t) σ4 σ5 () (by rw [hspec]; exact Set.mem_singleton _) + · rw [if_neg hs, WP.spec_ok] + refine fin s4 σ4 Z.Q ?_ rfl (WritesIn.refl _ _) rfl H4 + unfold testPossibleSwitchTo + rw [nd_gets_bindSc] + unfold Fl4Spec.NonDet.when + rw [if_neg hs] + rfl + +end +end +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/TcbRestartTailInactA.lean b/proofs/Fl4ProofsA64/P3/TcbRestartTailInactA.lean new file mode 100644 index 000000000..9e7a8e4ae --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbRestartTailInactA.lean @@ -0,0 +1,199 @@ +/- +P3 (AArch64): the SC tail of `sched.restart` when the target's bound SC is **inactive**. + +`SchedContextBind` may attach an inactive SC to a stopped thread (`schedcontext.rs` bind of a `TS_INACTIVE` +thread), so `restartTailA`'s `active` premise is not an invariant. With an inactive bound SC the thread is not +schedulable after the `Restart` write: the sporadic unblock runs as usual, `schedcontext::resume` tests +`is_schedulable` and does nothing, and `possible_switch_to` is skipped; the spec's `fl4SchedContextResume` and +`testPossibleSwitchTo` test `schedulable` and do nothing either. + +* `restartScResume_notSchedA`: `schedcontext.resume` of a non-schedulable target is a no-op on both sides. +* `restartTailInactA`: the tail with an inactive bound SC. +* **`restartTailAnyA`**: `restartTailA` without `active` (case split). + +[S] premises as `restartTailA`'s, without `active`. No [J]. +-/ +import Fl4ProofsA64.P3.TcbRestartTailA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Sched +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4Spec Fl4SpecA64 + +noncomputable section + +section +variable {S A : Type} {st : machine.Store S} [L : LawfulStore st] {ar : machine.Arch A} + +/-- `schedcontext.resume` of a target that is not schedulable is a no-op on both sides. -/ +theorem restartScResume_notSchedA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s : S} {a : A} {σ : State Unit} + (H : SchedSRN st Z m κ s σ) (hst : StEqA Z.T (L.mem s) σ) {t : U64} + (ht : t ∈ Z.T) (hon : L.mem s (affA t) = L.cpu s) + (hQ : (L.mem s (inqA t) &&& sched.Q_QUEUED) = 0#u64) (hns : schedulable (specPtr t) σ = false) : + schedcontext.resume st ar s a (L.mem s (scA t)) = ok (s, a) ∧ + (do + let p ← getTcbObjRef Tcb.tcbSchedContext (specPtr t) + NonDet.maybeM fl4SchedContextResume p) σ = ({((), σ)}, False) := by + obtain ⟨x, hkt, hx⟩ := H.sch.tcb t ht + have hx' : x.tcbSchedContext = optScA m (L.mem s (scA t)) := hx + have hRead : getTcbObjRef Tcb.tcbSchedContext (specPtr t) σ = ({(x.tcbSchedContext, σ)}, False) := + threadGet_eqSc _ hkt + by_cases h0 : L.mem s (scA t) = 0#u64 + · have hn : x.tcbSchedContext = none := by rw [hx', h0, optScA, if_pos rfl] + refine ⟨by rw [h0, tcbScResume_nullA], ?_⟩ + rw [nd_bind_oneSc hRead, hn] + rfl + have ht0 : t ≠ 0#u64 := (tcb_ne01Sc (H.core.tcbs t ht)).1 + have hc : L.mem s (scA t) ∈ Z.C := H.rest.scMem t ht h0 + have hxp : x.tcbSchedContext = some (scNameA m (L.mem s (scA t))) := by + rw [hx', optScA, if_neg h0, scNameA_eq] + obtain ⟨y, ny, hky, hym⟩ := H.scr.sc _ hc + obtain ⟨z, nz, hkz, hz⟩ := H.inv.back _ _ _ hkt hxp + rw [hky] at hkz + cases hkz + have hTcbWord : L.mem s (scTcbA (L.mem s (scA t))) = t := by + have he := hym.tcb + rw [← scTcbA_eqSc, hz] at he + by_cases h : L.mem s (scTcbA (L.mem s (scA t))) = 0#u64 + · rw [h, optRefS_zero] at he + cases he + · rw [optRefS_ne h] at he + exact specPtr_inj (Option.some.inj he).symm + have hScRead : obj.Sc.tcb st (L.mem s (scA t)) s = ok t := + (eq_of_spec_fd (obj.Sc.tcb.spec (L := L) s _)).trans (congrArg ok hTcbWord) + have hnq : specPtr t ∉ σ.readyQueues x.tcbDomain x.tcbPriority := by + obtain ⟨w, hkw, hd, _, hPriority⟩ := H.rest.prio t ht + rw [hkt] at hkw + cases hkw + have hRp : BitVec.ofNat 8 (L.mem s (prioA t)).val = x.tcbPriority := by + apply BitVec.eq_of_toNat_eq + rw [BitVec.toNat_ofNat, hPriority] + exact Nat.mod_eq_of_lt x.tcbPriority.isLt + rw [hd, ← hRp, H.rest.rq _ (H.core.prio t ht), mem_map_specPtrSc, ← hon] + exact fun h => (H.core.queued t ht).2 h hQ + have hSched : sched.is_schedulable st s t = ok false := by + rw [is_schedulable_eqA, schedulable_eqA H ht (H.relOn ht hon) hst, hns] + refine ⟨?_, ?_⟩ + · unfold schedcontext.resume + simp only [eq_of_spec_fd (obj.Sc.is_null.spec _), h0, decide_false, Bool.false_eq_true, ite_false, + bind_ok, hScRead, eq_of_spec_fd (obj.Tcb.is_null.spec _), ht0, hSched] + · rw [nd_bind_oneSc hRead, hxp] + show fl4SchedContextResume (scNameA m (L.mem s (scA t))) σ = _ + rw [tcbResumeSc_boundA hky hz hkt (H.inv.ne hky) (fun h => by rw [hns] at h; cases h) hnq, + if_neg (fun h => by rw [hns] at h; cases h.1)] + +/-- **The SC tail of `sched.restart` with an inactive bound SC** (module docstring). -/ +theorem restartTailInactA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s2 : S} {a1 : A} {σ2 : State Unit} {t : U64} + (H : SchedSRB st Z m κ s2 σ2 (specPtr t)) (hst : StEqA Z.T (L.mem s2) σ2) (ht : t ∈ Z.T) + {x : Tcb} (hkx : σ2.kheap (specPtr t) = some (.tcb x)) + (hon : L.mem s2 (affA t) = L.cpu s2) + (hnqs : ∀ d p, specPtr t ∉ σ2.readyQueues d p) (hnrel : specPtr t ∉ σ2.releaseQueue) + (h0 : L.mem s2 (scA t) ≠ 0#u64) {y : SchedContext} {n : Nat} + (hky : σ2.kheap (scNameA m (L.mem s2 (scA t))) = some (.schedContext y n)) (hia : scActive y = false) : + restartTailProgA st ar s2 a1 t ⦃ out => out.2 = a1 ∧ ∃ σ3 Q' R', + restartTailSpecA (specPtr t) x.tcbSchedContext σ2 = ({((), σ3)}, False) ∧ + SchedSRA st { Z with Q := Q', R := R' } m κ out.1 σ3 ∧ StEqA Z.T (L.mem out.1) σ3 ∧ + RTPostA (st := st) Z m t s2 out.1 σ2 σ3 ⦄ := by + obtain ⟨x0, hx0, hxs⟩ := H.sch.tcb t ht + have e0 : x0 = x := by + rw [hkx] at hx0 + exact (KernelObject.tcb.inj (Option.some.inj hx0)).symm + rw [e0] at hxs + unfold restartTailProgA + refine WP.spec_bind (unblock_sporadic_SRB H ht false (fun h => absurd h hnrel)) ?_ + rintro s3 ⟨σ3, h3, H3, F, G⟩ + obtain ⟨hon3, hst3, hqw, hk3f⟩ := UnbFrA.restartPre H.toSchedSRN F ht hon hst + have hkx3 := hk3f x hkx + have hnqs3 : ∀ d p, specPtr t ∉ σ3.readyQueues d p := by rw [F.rq]; exact hnqs + have hnrel3 : specPtr t ∉ σ3.releaseQueue := by rw [F.rel]; exact hnrel + have hpU : ∀ p, optScA m (L.mem s2 (scA t)) = some p → ∃ y n, σ2.kheap p = some (.schedContext y n) := by + intro p hp + by_cases h0 : L.mem s2 (scA t) = 0#u64 + · rw [optScA, if_pos h0] at hp + cases hp + · rw [optScA, if_neg h0] at hp + obtain ⟨y, n, hk, -⟩ := H.scr.sc _ (H.rest.scMem t ht h0) + exact ⟨y, n, by rw [← Option.some.inj hp]; exact hk⟩ + have hmem3 : σ3.machineState.underlyingMemory = σ2.machineState.underlyingMemory := + (Endp.condUnblockReplyListsA false hpU (by rw [h3]; exact Set.mem_singleton _)).mem + have RT1 : RTPostA (st := st) Z m t s2 s3 σ2 σ3 := + ⟨F.mfr H.toSchedSRN, hmem3, fun q y n h => by + obtain ⟨y', h', e⟩ := G q y n h + exact ⟨y', h', by rw [e], by rw [e]⟩, + fun x' h => ⟨x', F.obj _ _ h (fun _ _ e => by cases e), rfl⟩⟩ + have hq0 : (L.mem s3 (inqA t) &&& sched.Q_QUEUED) = 0#u64 := H3.toSchedSRN.qBit0 ht hon3 hnqs3 + -- the bound SC is still inactive, so `t` is not schedulable + have hxp : x.tcbSchedContext = some (scNameA m (L.mem s2 (scA t))) := by + rw [hxs, optScA, if_neg h0] + rfl + have hia3 : ∀ y' n', σ3.kheap (scNameA m (L.mem s2 (scA t))) = some (.schedContext y' n') → + scActive y' = false := by + intro y' n' hy' + obtain ⟨y3, hy3, ey⟩ := G _ y n hky + have hyy : KernelObject.schedContext y3 n = .schedContext y' n' := Option.some.inj (hy3.symm.trans hy') + injection hyy with e1 _ + have e3 : scActive y3 = scActive y := by rw [ey] + rw [← e1, e3] + exact hia + have hns : schedulable (specPtr t) σ3 = false := by + obtain ⟨y3, hy3, -⟩ := G _ y n hky + have ia3 := hia3 y3 n hy3 + unfold schedulable + simp [optPred, optMapSome, optMap, tcbsOf, tcbsOfKh, tcbOf, scsOf, scsOfKh, scOf, hkx3, hxp, hy3, ia3] + obtain ⟨hres, hspecR⟩ := restartScResume_notSchedA (ar := ar) (a := a1) H3.toSchedSRN hst3 ht hon3 hq0 hns + rw [← hqw, hres, bind_ok] + refine let_pairSc s3 a1 (fun s4 a2 => do + let b ← sched.is_schedulable st s4 t + if b then + let s5 ← sched.possible_switch_to st s4 t + ok (s5, a2) + else ok (s4, a2)) _ ?_ + beta_reduce + rw [is_schedulable_eqA, bind_ok, schedulable_eqA H3.toSchedSRN ht (H3.toSchedSRN.relOn ht hon3) hst3, hns, + if_neg Bool.false_ne_true, WP.spec_ok] + have hrel : RelIfBoundSc σ3 (specPtr t) := by + intro x' scp y' n' h1 h2 h3' h4 _ + rw [hkx3] at h1 + cases h1 + rw [hxp] at h2 + cases h2 + rw [hia3 y' n' h3'] at h4 + cases h4 + refine ⟨rfl, σ3, Z.Q, Z.R, ?_, H3.toA hrel, hst3, RT1⟩ + have hU : ifSporadicCurScTestRefillUnblockCheck (Z := Unit) x.tcbSchedContext σ2 = ({((), σ3)}, False) := by + rw [hxs] + exact h3 + rw [nd_bind_oneSc (show getTcbObjRef Tcb.tcbSchedContext (specPtr t) σ3 = ({(x.tcbSchedContext, σ3)}, False) + from threadGet_eqSc _ hkx3)] at hspecR + unfold restartTailSpecA + rw [nd_bind_oneSc hU, nd_bind_oneSc hspecR] + unfold testPossibleSwitchTo + rw [nd_gets_bindSc] + unfold Fl4Spec.NonDet.when + rw [if_neg (by rw [hns]; exact Bool.false_ne_true)] + rfl + +/-- **`restartTailA` without the `active` premise** (module docstring): an inactive bound SC takes +`restartTailInactA`. -/ +theorem restartTailAnyA {Z : XSetsA} {m : McState} {κ : U64 → U64} {s2 : S} {a1 : A} {σ2 : State Unit} {t : U64} + (H : SchedSRB st Z m κ s2 σ2 (specPtr t)) (hst : StEqA Z.T (L.mem s2) σ2) (ht : t ∈ Z.T) + {x : Tcb} (hkx : σ2.kheap (specPtr t) = some (.tcb x)) (hrs : x.tcbState = .restart) + (hon : L.mem s2 (affA t) = L.cpu s2) (hmb : Fl4MinBudgetAgreeA ar a1) + (hidle : ∀ c < fl4A64Platform.numNodes, specPtr t ≠ m.idleThreadOf c) + (hnqs : ∀ d p, specPtr t ∉ σ2.readyQueues d p) (hnrel : specPtr t ∉ σ2.releaseQueue) + (hcpu : L.mem s2 (scA t) ≠ 0#u64 → L.mem s2 (scCoreA (L.mem s2 (scA t))) = L.cpu s2) : + restartTailProgA st ar s2 a1 t ⦃ out => out.2 = a1 ∧ ∃ σ3 Q' R', + restartTailSpecA (specPtr t) x.tcbSchedContext σ2 = ({((), σ3)}, False) ∧ + SchedSRA st { Z with Q := Q', R := R' } m κ out.1 σ3 ∧ StEqA Z.T (L.mem out.1) σ3 ∧ + RTPostA (st := st) Z m t s2 out.1 σ2 σ3 ⦄ := by + by_cases hA : ∀ sc y n, L.mem s2 (scA t) = sc → sc ≠ 0#u64 → + σ2.kheap (scNameA m sc) = some (.schedContext y n) → scActive y = true + · exact restartTailA H hst ht hkx hrs hon hmb hidle hnqs hnrel hA hcpu + · push Not at hA + obtain ⟨sc, y, n, e, h0, hky, hia⟩ := hA + subst e + exact restartTailInactA H hst ht hkx hon hnqs hnrel h0 hky (by simpa using hia) + +end +end +end Fl4ProofsA64.P3.Sched diff --git a/proofs/Fl4ProofsA64/P3/TcbRestartViewsA.lean b/proofs/Fl4ProofsA64/P3/TcbRestartViewsA.lean new file mode 100644 index 000000000..272db0960 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbRestartViewsA.lean @@ -0,0 +1,194 @@ +/- +P3 (AArch64): the views of `sched.restart`'s first steps. + +* **`isStopped_eqA`**: `sched.is_stopped` is the spec's `¬runnable ∧ ¬idle` test, from `StEqA`. +* **`restartStateIpcA`**: `set_thread_state t TS_RESTART` of a thread with no pending IPC keeps the + endpoint and notification relations and the reply stack (the `Restart` twin of + `suspendInactiveIpcA`). + +No named premise. +-/ +import Fl4ProofsA64.P3.TcbSuspendA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4SpecA64 Fl4Spec Sched + +noncomputable section + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + +/-- **`is_stopped` is the spec's stopped test** (module docstring). -/ +theorem isStopped_eqA {s : S} {σ : State Unit} {T : Set U64} (hst : StEqA T (L.mem s) σ) {t : U64} + (ht : t ∈ T) {x : Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) : + ∃ b, sched.is_stopped st s t = ok b ∧ + (b = true ↔ (¬runnable x.tcbState = true ∧ ¬idle x.tcbState)) := by + have hw : (L.mem s (stA t)).val = tsCodeA x.tcbState := hst t ht x hk + have hread := eq_of_spec_fd (P1.obj.Tcb.state.spec (st := st) s t) + have c0 : sched.TS_INACTIVE.val = 0 := by unfold sched.TS_INACTIVE; rfl + have c3 : sched.TS_BLOCKED_ON_RECV.val = 3 := by unfold sched.TS_BLOCKED_ON_RECV; rfl + have c4 : sched.TS_BLOCKED_ON_SEND.val = 4 := by unfold sched.TS_BLOCKED_ON_SEND; rfl + have c5 : sched.TS_BLOCKED_ON_REPLY.val = 5 := by unfold sched.TS_BLOCKED_ON_REPLY; rfl + have c6 : sched.TS_BLOCKED_ON_NTFN.val = 6 := by unfold sched.TS_BLOCKED_ON_NTFN; rfl + have v : ∀ w k : U64, w = k ↔ w.val = k.val := fun w k => ⟨fun h => h ▸ rfl, UScalar.eq_of_val_eq⟩ + unfold sched.is_stopped sched.is_blocked + rw [hread] + simp only [bind_ok] + generalize L.mem s (stA t) = w at hw ⊢ + simp only [v w, c0, c3, c4, c5, c6, hw] + rcases hts : x.tcbState with _ | _ | _ | _ | _ | _ | _ | _ | ⟨e⟩ <;> + first + | (simp [tsCodeA, runnable, idle]; done) + | (cases e; simp [tsCodeA, runnable, idle, ExtThreadState.runnable]; done) + +end + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + +/-- `set_thread_state t Restart` keeps the endpoint and notification relations and the reply +stack, for a thread with no pending IPC. -/ +theorem restartStateIpcA (hP : IPlacedA N Z.T E R Bf) (P : IpcSchedPlacementA Z.th N E R Bf) + {s : S} {σ : State Unit} {Q : U64 → U64 → List U64} {RR : U64 → List U64} {p0 : ObjRef} + {stack : U64 → List U64} (H : SchedSRB st { Z with Q := Q, R := RR } m κ s σ p0) + (hst : StEqA Z.T (L.mem s) σ) + (RN : NtfnStateRel N Z.T (L.mem s) σ) (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {t : U64} (ht : t ∈ Z.T) {x : Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hfree : IpcFreeA x.tcbState) : + sched.set_thread_state st s t sched.TS_RESTART ⦃ s' => + NtfnStateRel N Z.T (L.mem s') (stsActA (updKSc σ (specPtr t) (.tcb { x with tcbState := .restart })) (specPtr t)) ∧ + EpStateRel Z.T E R Bf (L.mem s') (stsActA (updKSc σ (specPtr t) (.tcb { x with tcbState := .restart })) (specPtr t)) ∧ + ReplyStackRelA (L.mem s') R Z.C + (stsActA (updKSc σ (specPtr t) (.tcb { x with tcbState := .restart })) (specPtr t)) (scNameA m) stack ⦄ := by + have PE := hP.e + have hta := PE.tcb t ht + have hcode : (L.mem s (stA t)).val = tsCode x.tcbState := (HE.at_tcb ht hk).st + obtain ⟨f1, f2, f3, f4⟩ := hfree + have h3 : (L.mem s (stA t)).val ≠ 3 := by + rw [hcode]; intro h + obtain ⟨e, r, d, h⟩ := tsCode_three h + exact f2 _ _ _ h + have h4 : (L.mem s (stA t)).val ≠ 4 := by + rw [hcode]; intro h + obtain ⟨e, d, h⟩ := tsCode_four h + exact f1 _ _ h + have h6 : (L.mem s (stA t)).val ≠ 6 := by + rw [hcode]; intro h + obtain ⟨n, h⟩ := tsCode_six' h + exact f3 _ h + have hqE := notqE_of_code HE h3 h4 + have hqN := notqN_of_code RN h6 + obtain ⟨E1, -, -⟩ := write_state_rel PE s σ HE ht hk hqE .restart (by decide) sched.TS_RESTART + (by unfold sched.TS_RESTART; rfl) + obtain ⟨hw, hoth⟩ := twr_wr (st := st) hP.n ht (K := 3328#u64) (Or.inl rfl) s sched.TS_RESTART + rw [← stA_eq] at hw hoth + have hx := RN.at_tcb ht hk + have RN1 : NtfnStateRel N Z.T (L.mem (L.wr s (stA t) sched.TS_RESTART)) + (updK σ (specPtr t) (.tcb { x with tcbState := .restart })) := by + refine RN.upd_tcb ht _ hw.ntfn hw.tcb hw.links (Or.inl hqN) ⟨?_, ?_, ?_, ?_⟩ + · rw [rd_wr_eq (foot_st hP.n ht).1]; unfold sched.TS_RESTART; rfl + · intro n hn; cases hn + · show (L.mem _ (badgeRegA t)).bv = badgeOf x + rw [badgeRegA_eq, hoth _ (by simp) (by simp) (by simp), ← badgeRegA_eq]; exact hx.badge + · show (L.mem _ (prioA t)).val = x.tcbPriority.toNat + rw [prioA_eq', hoth _ (by simp) (by simp) (by simp), ← prioA_eq']; exact hx.prio + have hal : Aligned (stA t) := (foot_st hP.n ht).1 + have w1 : WritesIn st s (L.wr s (stA t) sched.TS_RESTART) (· = stA t) := writesIn_wr hal + have hsv : (stA t).val = t.val + 3328 := val_st (hP.n.tcb t ht) + have HS1 : ReplyStackRelA (L.mem (L.wr s (stA t) sched.TS_RESTART)) R Z.C + (updK σ (specPtr t) (.tcb { x with tcbState := .restart })) (scNameA m) stack := by + refine HS.frame (fun d hd => ?_) (fun c _ y k hy => ⟨y, k, ?_, rfl⟩) (fun d _ y hy => ⟨y, ?_, rfl⟩) + · have n := replyNextPlaceA (Z := Z) PE hd + have p := replyPrevPlaceA (Z := Z) PE hd + have := PE.rt d hd t ht + exact ⟨w1.mem_eq n.1 (fun e => by have := congrArg UScalar.val e; omega), + w1.mem_eq p.1 (fun e => by have := congrArg UScalar.val e; omega)⟩ + · rw [updK_kheap, Function.update_of_ne (fun e => by rw [e, hk] at hy; cases hy)]; exact hy + · by_cases e : specPtr d = specPtr t + · rw [e, updK_kheap, Function.update_self] at hy; cases hy + · rw [updK_kheap, Function.update_of_ne e] at hy; exact hy + unfold sched.set_thread_state + refine WP.spec_bind (obj.Tcb.set_state.spec s t sched.TS_RESTART) ?_ + intro s1 hs1 + subst hs1 + refine WP.spec_mono (schedule_tcb_act (st := st) (L.wr s (stA t) sched.TS_RESTART) t) ?_ + intro s' h + have hσk : (stsActA (updKSc σ (specPtr t) (.tcb { x with tcbState := .restart })) (specPtr t)).kheap = + (updK σ (specPtr t) (.tcb { x with tcbState := .restart })).kheap := by + unfold stsActA + split <;> rfl + have KN : Notif.KView (updK σ (specPtr t) (.tcb { x with tcbState := .restart })) + (stsActA (updKSc σ (specPtr t) (.tcb { x with tcbState := .restart })) (specPtr t)) := + Notif.KView.stsAct _ _ + have KE : Endp.KView (updK σ (specPtr t) (.tcb { x with tcbState := .restart })) + (stsActA (updKSc σ (specPtr t) (.tcb { x with tcbState := .restart })) (specPtr t)) := + KView.stsAct' _ _ + have HS2 : ReplyStackRelA (L.mem (L.wr s (stA t) sched.TS_RESTART)) R Z.C + (stsActA (updKSc σ (specPtr t) (.tcb { x with tcbState := .restart })) (specPtr t)) (scNameA m) stack := + HS1.frame (fun _ _ => ⟨rfl, rfl⟩) (fun c _ y k hy => ⟨y, k, by rw [hσk]; exact hy, rfl⟩) + (fun d _ y hy => ⟨y, by rw [hσk] at hy; exact hy, rfl⟩) + rcases h with rfl | rfl + · exact ⟨RN1.frame hP.n (fun _ _ _ => rfl) KN, E1.frame PE (fun _ _ _ => rfl) KE, HS2⟩ + · obtain ⟨s2, hs2⟩ : ∃ s2, s2 = L.wr s (stA t) sched.TS_RESTART := ⟨_, rfl⟩ + have hA : actA (L := L) s2 = L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION := by + show L.nodeAt s2 (L.cpu s2) layout.node.SCHED_ACTION = _ + rw [hs2, nodeAt_of_ksb w1.1, w1.2.1] + have hAal : Aligned (actA (L := L) s2) := by + rw [hA] + exact nodeAt_aligned (H.core.nodes _ H.sch.cpuCs) (by simp [layout.node.SCHED_ACTION]) + (by simp [layout.node.SCHED_ACTION]) + rw [← hs2] at RN1 E1 HS2 ⊢ + have w2 := writesIn_wr (st := st) (s := s2) (v := sched.ACTION_CHOOSE_NEW) hAal + have hn := H.node_in H.sch.cpuCs (o := layout.node.SCHED_ACTION) (by simp [layout.node.SCHED_ACTION]) + -- the action word is outside every object + have off : ∀ (p b : U64) (k : ObjKindA), Z.th p = some k → k ≠ .cnode 0 → p.val ≤ b.val → + b.val < p.val + k.size → b ≠ actA (L := L) s2 := by + intro p b k hp hne hlo hhi e + have hK := H.place.ks p k hp (fun irq => hne (H.place.irqK hp irq)) + rw [e, hA] at hlo hhi + omega + refine ⟨RN1.frame hP.n (fun b hb hf => w2.mem_eq hb fun e => ?_) KN, + E1.frame PE (fun b hb hf => w2.mem_eq hb fun e => ?_) KE, + HS2.frame (fun d hd => ?_) (fun c _ y k hy => ⟨y, k, hy, rfl⟩) (fun d _ y hy => ⟨y, hy, rfl⟩)⟩ + · rcases hf with ⟨n, hnN, g1, g2⟩ | ⟨u, hu, hf⟩ + · exact off n b _ (P.notification n hnN) (by intro h; cases h) g1 (by change b.val < n.val + 64; omega) e + · have hua := hP.n.tcb u hu + have hval : b.val = u.val + 3328 ∨ b.val = u.val + 3336 ∨ b.val = u.val + 256 ∨ + b.val = u.val + 3448 ∨ b.val = u.val + 3496 ∨ b.val = u.val + 3504 := by + rcases hf with h | h | h | h | h | h <;> + rw [h] <;> + simp only [stA_eq, Notif.tBlockA_eq, Notif.badgeRegA_eq, Notif.prioA_eq', Notif.nextEp_eq, + Notif.prevEp_eq] <;> + rw [Notif.tfield_val hua] <;> simp + exact off u b _ ((H.thT u).2 hu) (by intro h; cases h) (by omega) (by change b.val < u.val + 4096; omega) e + · rcases hf with ⟨ep, hep, g⟩ | ⟨r, hr, g⟩ | ⟨u, hu, g | g⟩ | g + · exact off ep b _ (P.endpoint ep hep) (by intro h; cases h) (by omega) + (by change b.val < ep.val + 16; omega) e + · exact off r b _ (P.reply r hr) (by intro h; cases h) (by omega) + (by change b.val < r.val + 32; omega) e + · have g1 := g.1 + have g2 := g.2 + unfold TOffN at g2 + exact off u b _ ((H.thT u).2 hu) (by intro h; cases h) g1 (by change b.val < u.val + 4096; omega) e + · unfold TL at g + exact off u b _ ((H.thT u).2 hu) (by intro h; cases h) (by omega) + (by change b.val < u.val + 4096; omega) e + · obtain ⟨p, len, hp, g1, g2⟩ := P.buffer b g + exact off p b _ hp (by intro h; cases h) g1 g2 e + · have n := replyNextPlaceA (Z := Z) PE hd + have p := replyPrevPlaceA (Z := Z) PE hd + exact ⟨w2.mem_eq n.1 (off d _ _ (P.reply d hd) (by intro h; cases h) (by omega) + (by change _ < d.val + 32; omega)), + w2.mem_eq p.1 (off d _ _ (P.reply d hd) (by intro h; cases h) (by omega) + (by change _ < d.val + 32; omega))⟩ + +end + +end + +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/TcbResumeLocalA.lean b/proofs/Fl4ProofsA64/P3/TcbResumeLocalA.lean new file mode 100644 index 000000000..932e49fb8 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbResumeLocalA.lean @@ -0,0 +1,209 @@ +/- +P3 (AArch64): the Resume (13) and Suspend (12) invocations over the merged relation. + +Rust: `tcb.restart_caller` (the caller's `TS_RESTART` write), then `sched.restart` / +`sched.suspend`, answering `Ok`. + +* **`restartCaller_mcsA`**: the caller's `Restart` keeps every merged view. +* **`TcbIpcViewsA`**: the merged views a TCB invocation hands on. +* **`tcbResume_localA`**: label 13 against `fl4PerformInvocation` of `invokeTCB (resume t)`. +* **`tcbSuspend_localA`**: label 12 against `fl4PerformInvocation` of `invokeTCB (suspend t)`. + +[S] premises: a quiet `remote_tcb_stall` (`hstall`) and the caller-side facts of +`restart_refinesA` / `suspend_refinesA` at the post-`Restart` state (`tgt`). [P]: their platform +premises. No [J]. +-/ +import Fl4ProofsA64.P3.TcbRestartA +import Fl4ProofsA64.P3.LocalInvokePostA +import Fl4ProofsA64.P3.TcbInvokeDispatchA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4SpecA64 Fl4Spec Sched + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + +/-- The merged views a TCB invocation hands on. -/ +structure TcbIpcViewsA (st : machine.Store S) [L : LawfulStore st] (Z : XSetsA) (m : McState) + (κ : U64 → U64) (N E R : Set U64) (Bf : Set (U64 × Word)) (stack : U64 → List U64) (s : S) + (σ : State Unit) : Prop where + rel : IpcSchedRelA st Z m κ s σ + ntfn : NtfnStateRel N Z.T (L.mem s) σ + ep : EpStateRel Z.T E R Bf (L.mem s) σ + stk : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack + mcp : McpRelA Z.T m (L.mem s) σ + yld : YieldRelA Z.T Z.C m (L.mem s) σ + +/-- **The caller's `Restart` keeps every merged view.** -/ +theorem restartCaller_mcsA (hP : IPlacedA N Z.T E R Bf) (P : IpcSchedPlacementA Z.th N E R Bf) + {s : S} {σ : State Unit} {stack : U64 → List U64} + (V : TcbIpcViewsA st Z m κ N E R Bf stack s σ) + {x : Tcb} (hx : σ.kheap (specPtr (L.curW s)) = some (.tcb x)) (hr : runnable x.tcbState = true) : + tcb.restart_caller st s ⦃ s1 => ∃ σ1, + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + TcbIpcViewsA st Z m κ N E R Bf stack s1 σ1 ⦄ := by + have PE := hP.e + obtain ⟨Q0, R0, H0⟩ := id V.rel + have hc : L.curW s ∈ Z.T := H0.curT + have T0 : StEqA Z.T (L.mem s) σ := fun u hu y hy => (V.ep.at_tcb hu hy).st + have hv : sched.TS_RESTART.val = tsCodeA .restart := by unfold sched.TS_RESTART; rfl + have hfree : IpcFreeA x.tcbState := by + refine ⟨?_, ?_, ?_, ?_⟩ <;> intros <;> intro e <;> rw [e] at hr <;> cases hr + have hcur : σ.curThread = specPtr (L.curW s) := H0.sch.curThread + unfold tcb.restart_caller + refine WP.spec_bind (sched.cur_thread.spec s) ?_ + rintro t1 ht1 + rw [show t1 = L.curW s from ht1] + refine WP.spec_mono (WP.spec_and (WP.spec_and + (sc_set_runnable_stateA (Z := { Z with Q := Q0, R := R0 }) H0 T0 hc hx hr (ts := .restart) rfl hv) + (set_thread_state_exAnyA (Z := { Z with Q := Q0, R := R0 }) H0 T0 hc hx (ts := .restart) rfl hv)) + (restartStateIpcA (Z := Z) hP P (Q := Q0) (RR := R0) (H0.toB (specPtr (L.curW s))) T0 V.ntfn V.ep + V.stk hc hx hfree)) ?_ + rintro s1 ⟨⟨⟨σ1, hsts, H1, T1, hkh, -⟩, ⟨-, -, -, -, -, w⟩⟩, RN1, E1, S1⟩ + have hσ1 : σ1 = stsActA (updKSc σ (specPtr (L.curW s)) (.tcb { x with tcbState := .restart })) + (specPtr (L.curW s)) := + congrArg Prod.snd (Set.singleton_eq_singleton_iff.1 + (congrArg Prod.fst (hsts.symm.trans (setThreadState_stsActExA hx _)))) + rw [← hσ1] at RN1 E1 S1 + have h1t : σ1.kheap (specPtr (L.curW s)) = some (.tcb { x with tcbState := .restart }) := by + rw [hkh, updKSc_same] + have h1far : ∀ q, q ≠ specPtr (L.curW s) → σ1.kheap q = σ.kheap q := fun q hq => by + rw [hkh, updKSc_kheap, Function.update_of_ne hq] + have off : ∀ u ∈ Z.T, ∀ K : U64, (K = 3440#u64 ∨ K = 3472#u64 ∨ K = 3456#u64) → + L.mem s1 (wa u K) = L.mem s (wa u K) := by + intro u hu K hK + have hua := PE.tcb u hu + have hKv : K.val = 3440 ∨ K.val = 3472 ∨ K.val = 3456 := by rcases hK with rfl | rfl | rfl <;> simp + have hv' : (wa u K).val = u.val + K.val := Notif.tfield_val hua (by omega) + refine w.mem_eq (field_aligned (H0.core.tcbs u hu) (by omega) (by rcases hK with rfl | rfl | rfl <;> simp)) + (fun h => ?_) + rcases h with h | h + · have h' := congrArg UScalar.val h + rw [hv', val_st (hP.n.tcb _ hc)] at h' + by_cases e : u = L.curW s + · subst u; omega + · have := tcb_disj PE hc hu e; omega + · have hty := (H0.thT u).2 hu + have hK' : (wa u K).val < (L.ksb s).val ∨ (L.ksb s).val + ksSizeA ≤ (wa u K).val := by + have g := H0.place.ks u _ hty (fun irq => by + have := H0.place.irqK hty irq + cases this) + change u.val + 4096 ≤ (L.ksb s).val ∨ _ at g + omega + have hn := H0.node_in H0.sch.cpuCs (o := layout.node.SCHED_ACTION) (by simp [layout.node.SCHED_ACTION]) + rw [h] at hK' + omega + obtain ⟨M1, Y1⟩ := yieldFrameA (mem' := L.mem s1) (σ' := σ1) V.mcp V.yld + (fun u hu => ⟨off u hu 3440#u64 (Or.inl rfl), off u hu 3456#u64 (Or.inr (Or.inr rfl))⟩) + (fun u hu => off u hu 3472#u64 (Or.inr (Or.inl rfl))) + (fun u _ y hy => by + by_cases e : specPtr u = specPtr (L.curW s) + · rw [e, hx] at hy + cases hy + exact ⟨{ x with tcbState := .restart }, by rw [e]; exact h1t, rfl, rfl, rfl⟩ + · exact ⟨y, by rw [h1far _ e]; exact hy, rfl, rfl, rfl⟩) + (fun c hc' => by + obtain ⟨y, n, hy, -⟩ := H0.scr.sc c hc' + exact ⟨y, n, y, hy, by rw [h1far _ (fun h => by rw [h, hx] at hy; cases hy)]; exact hy, rfl⟩) + exact ⟨σ1, by rw [hcur]; exact hsts, ⟨⟨Q0, R0, H1⟩, RN1, E1, S1, M1, Y1⟩⟩ + +/-- **Resume (label 13)** (module docstring). -/ +theorem tcbResume_localA (hP : IPlacedA N Z.T E R Bf) (P : IpcSchedPlacementA Z.th N E R Bf) + (hc : m.curCore < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hmbF : ∀ a, Fl4MinBudgetAgreeA ar a) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {s : S} {a : A} {σ : State Unit} {stack : U64 → List U64} + (V : TcbIpcViewsA st Z m κ N E R Bf stack s σ) (hC0 : ∀ c ∈ Z.C, c ≠ 0#u64) + {t : U64} (ht : t ∈ Z.T) {ctx : tcb.Ctx} {block call donate : Bool} + (hstall : smp.remote_tcb_stall st ar s a t = ok (s, a)) + {xc : Tcb} (hxc : σ.kheap (specPtr (L.curW s)) = some (.tcb xc)) (hrc : runnable xc.tcbState = true) + (hidle : ∀ c < fl4A64Platform.numNodes, specPtr t ≠ m.idleThreadOf c) + (tgt : ∀ (s1 : S) (σ1 : State Unit), + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) → + TcbIpcViewsA st Z m κ N E R Bf stack s1 σ1 → ∃ x, σ1.kheap (specPtr t) = some (.tcb x) ∧ + (∀ r', x.tcbState = .blockedOnReply r' → ∃ r ∈ R, specPtr r = r' ∧ + ∃ xr, σ1.kheap r' = some (.reply xr) ∧ xr.replyTcb = some (specPtr t) ∧ ScReplyCovA Z.C m σ1 r') ∧ + (∀ e', epBlocked x.tcbState = some e' → L.mem s1 (tBlockA t) ∈ E ∧ + ∃ z, σ1.kheap e' = some (.endpoint z) ∧ specPtr t ∈ z.epQueue) ∧ + (∀ n', x.tcbState = .blockedOnNotification n' → L.mem s1 (tBlockA t) ∈ N ∧ + ∃ z, σ1.kheap n' = some (.notification z) ∧ specPtr t ∈ z.ntfnObj.ntfnQueue) ∧ + RepOKA σ1 (specPtr t) ∧ L.mem s1 (affA t) = L.cpu s1 ∧ + (∀ (s2 : S) (σ2 : State Unit) (Q : U64 → U64 → List U64) (RR : U64 → List U64), + SchedSRB st { Z with Q := Q, R := RR } m κ s2 σ2 (specPtr t) → + (∃ y, σ2.kheap (specPtr t) = some (.tcb y) ∧ y.tcbState = .restart) → + (∀ sc y n, L.mem s2 (scA t) = sc → sc ≠ 0#u64 → + σ2.kheap (scNameA m sc) = some (.schedContext y n) → scActive y = true) ∧ + (L.mem s2 (scA t) ≠ 0#u64 → L.mem s2 (scCoreA (L.mem s2 (scA t))) = L.cpu s2))) : + tcb.decode st ar s a fl4_abi.label.TCBResume t ctx ⦃ LocalInvokePostA + (fun s' a' σ' => a' = a ∧ ∃ stack', TcbIpcViewsA st Z m κ N E R Bf stack' s' σ') + block call donate (.invokeTCB (.resume (specPtr t))) σ ⦄ := by + rw [tcbResume_programA hstall] + refine WP.spec_bind (restartCaller_mcsA hP P V hxc hrc) ?_ + rintro s1 ⟨σ1, hsts, V1⟩ + obtain ⟨x, hk, hReply, hE, hN, hrep, hon, tailS⟩ := tgt s1 σ1 hsts V1 + refine WP.spec_bind (restart_refinesA hP P hc hmb hmbF hmax htu s1 a σ1 V1.rel V1.ntfn V1.ep V1.stk + V1.mcp V1.yld hC0 ht hk hReply hE hN hrep hon hidle tailS) ?_ + rintro ⟨s2, a2⟩ ⟨ha, σ2, stk2, hspec, RN2, H2, E2, S2, M2, Y2, -, -⟩ + refine (WP.spec_ok (core.result.Result.Ok (), s2, a2)).2 ?_ + refine ⟨rfl, σ1, σ2, [], hsts, ?_, ha, stk2, ⟨H2, RN2, E2, S2, M2, Y2⟩⟩ + unfold fl4PerformInvocation fl4InvokeTcb + apply mem_liftED + refine mem_bind_ndD (x := ((), σ2)) ?_ rfl + rw [hspec] + rfl + +/-- **Suspend (label 12)** (module docstring). -/ +theorem tcbSuspend_localA (hP : IPlacedA N Z.T E R Bf) (P : IpcSchedPlacementA Z.th N E R Bf) + (hc : m.curCore < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {s : S} {a : A} {σ : State Unit} {stack : U64 → List U64} + (V : TcbIpcViewsA st Z m κ N E R Bf stack s σ) (hC0 : ∀ c ∈ Z.C, c ≠ 0#u64) + {t : U64} (ht : t ∈ Z.T) {ctx : tcb.Ctx} {block call donate : Bool} + (hstall : smp.remote_tcb_stall st ar s a t = ok (s, a)) + {xc : Tcb} (hxc : σ.kheap (specPtr (L.curW s)) = some (.tcb xc)) (hrc : runnable xc.tcbState = true) + (tgt : ∀ (s1 : S) (σ1 : State Unit), + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) → + TcbIpcViewsA st Z m κ N E R Bf stack s1 σ1 → ∃ x, σ1.kheap (specPtr t) = some (.tcb x) ∧ + (∀ r', x.tcbState = .blockedOnReply r' → ∃ r ∈ R, specPtr r = r' ∧ + ∃ xr, σ1.kheap r' = some (.reply xr) ∧ xr.replyTcb = some (specPtr t) ∧ ScReplyCovA Z.C m σ1 r') ∧ + (∀ e', epBlocked x.tcbState = some e' → L.mem s1 (tBlockA t) ∈ E ∧ + ∃ z, σ1.kheap e' = some (.endpoint z) ∧ specPtr t ∈ z.epQueue) ∧ + (∀ n', x.tcbState = .blockedOnNotification n' → L.mem s1 (tBlockA t) ∈ N ∧ + ∃ z, σ1.kheap n' = some (.notification z) ∧ specPtr t ∈ z.ntfnObj.ntfnQueue) ∧ + RepOKA σ1 (specPtr t) ∧ L.mem s1 (runVcpuA t) = 0#u64 ∧ x.tcbExt.tcbRunVcpu = none ∧ + L.mem s1 (affA t) = L.cpu s1) : + tcb.decode st ar s a fl4_abi.label.TCBSuspend t ctx ⦃ LocalInvokePostA + (fun s' a' σ' => a' = a ∧ ∃ stack', TcbIpcViewsA st Z m κ N E R Bf stack' s' σ') + block call donate (.invokeTCB (.suspend (specPtr t))) σ ⦄ := by + rw [tcbSuspend_programA hstall] + refine WP.spec_bind (restartCaller_mcsA hP P V hxc hrc) ?_ + rintro s1 ⟨σ1, hsts, V1⟩ + obtain ⟨x, hk, hReply, hE, hN, hrep, hrun0, hrunS, hon⟩ := tgt s1 σ1 hsts V1 + refine WP.spec_bind (suspend_refinesA hP P hc hmb hmax htu s1 a σ1 V1.rel V1.ntfn V1.ep V1.stk + V1.mcp V1.yld hC0 ht hk hReply hE hN hrep hrun0 hrunS hon) ?_ + rintro ⟨s2, a2⟩ ⟨ha, σ2, stk2, hspec, RN2, H2, E2, S2, M2, Y2, -, -⟩ + refine (WP.spec_ok (core.result.Result.Ok (), s2, a2)).2 ?_ + refine ⟨rfl, σ1, σ2, [], hsts, ?_, ha, stk2, ⟨H2, RN2, E2, S2, M2, Y2⟩⟩ + have s0 := fl4CancelRun_quietA hk hrunS + rw [nd_bind_oneSc s0] at hspec + unfold fl4PerformInvocation fl4InvokeTcb invokeTcb + refine mem_bindE_okD (a := ()) (s' := σ1) (mem_liftED (by rw [s0]; rfl)) ?_ + apply mem_liftED + refine mem_bind_ndD (x := ((), σ2)) ?_ rfl + rw [hspec] + rfl + +end +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/TcbSuspendA.lean b/proofs/Fl4ProofsA64/P3/TcbSuspendA.lean new file mode 100644 index 000000000..a35383804 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbSuspendA.lean @@ -0,0 +1,697 @@ +/- +P3 (AArch64): `sched.suspend` refines l4v `fl4CancelRun; suspend` over the merged relation. + +Rust order: `vcpu::cancel_run`, `ipc::cancel_ipc`, `FAULT_IP := NEXT_IP` if still Running, +`dequeue`, `release_remove`, `cancel_yield_to`, `set_thread_state Inactive`. + +* **`suspendRestartPcA`**: the `FAULT_IP := NEXT_IP` step against l4v `update_restart_pc`. +* **`suspend_refinesA`**: the composition. After `release_remove` only `released_ipc_queues` + except at `t` holds (`SchedSRB … t`); the `Inactive` write closes it. + +[S] premises: `t` has no live VCPU run link (`RUN_VCPU = 0`, `tcbRunVcpu = none`); `t`'s +affinity is the current core. [P]: the platform premises of `ipcSchedOpsA` (`hc`, `hmb`, +`hmax`, `htu`). The other premises are the cancellation's caller-side tracking facts +(`suspendCancelA`). No [J]. +-/ +import Fl4ProofsA64.P3.TcbSuspendCancelA +import Fl4ProofsA64.P3.TcbCancelRunA +import Fl4ProofsA64.P3.ScCancelYieldBA +import Fl4ProofsA64.P3.TcbInactiveExA +import Fl4ProofsA64.P3.ScDeqA +import Fl4ProofsA64.P3.ScRelRmA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4SpecA64 Fl4Spec Sched + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +theorem suspendCopyPcA {σ : State Unit} {p : ObjRef} {x : Tcb} + (hk : σ.kheap p = some (.tcb x)) : + updateRestartPc (Z := Unit) p σ = + ({((), updKSc σ p (.tcb (yieldRegA x .faultIP (x.tcbArch.tcbContext.userRegs .nextIP))))}, False) := by + let ip := x.tcbArch.tcbContext.userRegs .nextIP + rw [← yieldAsUserRegA hk .faultIP ip] + unfold updateRestartPc asUser + rw [nd_bind_oneSc (getTcb_oneA hk), nd_pure_bindSc] + rw [nd_bind_oneSc (getTcb_oneA hk), nd_pure_bindSc] + have e : (Fl4Spec.NonDet.bind (getRegister nextInstructionRegister) (setRegister faultRegister)) + (archTcbContextGet x.tcbArch) = setRegister .faultIP ip (archTcbContextGet x.tcbArch) := by + change (getRegister nextInstructionRegister >>= setRegister faultRegister) _ = _ + unfold getRegister nextInstructionRegister faultRegister + rw [nd_gets_bindSc] + rfl + rw [e] + +theorem tsCode_runningA {ts : ThreadState} (h : tsCode ts = tsCode .running) : ts = .running := by + rcases ts with _ | _ | _ | _ | _ | _ | _ | _ | ⟨⟨⟩⟩ <;> simp [tsCode] at h ⊢ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + +/-- The FL4 `FAULT_IP := NEXT_IP` fragment of `suspend`. -/ +def suspendPcProgA (st : machine.Store S) (s : S) (t : U64) : Result S := do + let i ← obj.Tcb.state st t s + if i = sched.TS_RUNNING then do + let ip ← obj.Tcb.reg st t s layout.reg.NEXT_IP + obj.Tcb.set_reg st t s layout.reg.FAULT_IP ip + else ok s + +/-- What the register step keeps (module docstring). -/ +structure SuspendPcPostA (st : machine.Store S) [L : LawfulStore st] (Z : XSetsA) (m : McState) + (κ : U64 → U64) (N E R : Set U64) (Bf : Set (U64 × Word)) (stack : U64 → List U64) (s : S) + (σ σ' : State Unit) (t : U64) (x : Tcb) (s' : S) : Prop where + ntfn : NtfnStateRel N Z.T (L.mem s') σ' + sched : IpcSchedRelA st Z m κ s' σ' + ep : EpStateRel Z.T E R Bf (L.mem s') σ' + stk : ReplyStackRelA (L.mem s') R Z.C σ' (scNameA m) stack + wr : WritesIn st s s' (· = regA t layout.reg.FAULT_IP) + tcb : ∃ y, σ'.kheap (specPtr t) = some (.tcb y) ∧ y = { x with tcbArch := y.tcbArch } + far : ∀ q, q ≠ specPtr t → σ'.kheap q = σ.kheap q + +theorem suspendRestartPcA (hP : IPlacedA N Z.T E R Bf) (P : IpcSchedPlacementA Z.th N E R Bf) + {s : S} {σ : State Unit} {stack : U64 → List U64} + (RN : NtfnStateRel N Z.T (L.mem s) σ) (H : IpcSchedRelA st Z m κ s σ) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {t : U64} (ht : t ∈ Z.T) {x : Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) : + suspendPcProgA st s t ⦃ s' => ∃ σ', + (∀ (β : Type) (f : Unit → Fl4Spec.NonDet (State Unit) β), + (getThreadState (Z := Unit) (specPtr t) >>= fun ts => + Fl4Spec.NonDet.when (ts = .running) (updateRestartPc (specPtr t)) >>= f) σ = f () σ') ∧ + SuspendPcPostA st Z m κ N E R Bf stack s σ σ' t x s' ⦄ := by + have PE := hP.e + have hta := PE.tcb t ht + have hx := HE.at_tcb ht hk + have hgs : getThreadState (Z := Unit) (specPtr t) σ = ({(x.tcbState, σ)}, False) := getThreadState_eq hk + unfold suspendPcProgA + step with obj.Tcb.state.spec as ⟨w, hw⟩ + have hw' : w.val = tsCode x.tcbState := by rw [hw]; exact hx.st + have hrun : w = sched.TS_RUNNING ↔ x.tcbState = .running := by + constructor + · intro e + exact tsCode_runningA (by rw [← hw', e]; unfold sched.TS_RUNNING; rfl) + · intro e + exact UScalar.eq_of_val_eq (by rw [hw', e]; unfold sched.TS_RUNNING; rfl) + by_cases h : w = sched.TS_RUNNING + swap + · rw [if_neg h, WP.spec_ok] + refine ⟨σ, fun β f => ?_, RN, H, HE, HS, WritesIn.refl _ _, ⟨x, hk, by cases x; rfl⟩, fun _ _ => rfl⟩ + rw [nd_bind_oneSc hgs] + unfold Fl4Spec.NonDet.when + rw [if_neg (fun e => h (hrun.2 e))] + exact nd_bind_oneSc (show Fl4Spec.NonDet.pure () σ = ({((), σ)}, False) from rfl) _ + · rw [if_pos h] + have hr := hrun.1 h + rw [tcb_reg_eqA, bind_ok] + obtain ⟨v, hv⟩ : ∃ v, v = L.mem s (regWA t layout.reg.NEXT_IP) := ⟨_, rfl⟩ + rw [← hv] + refine WP.spec_mono (obj.Tcb.set_reg.spec s t layout.reg.FAULT_IP v) ?_ + intro s' hs' + subst hs' + have hvv : v.bv = x.tcbArch.tcbContext.userRegs .nextIP := by + have h1 := hx.regs .nextIP + rw [regIdxA_nextIP] at h1 + rw [hv] + exact h1 + have hW := write_reg_rel PE s σ HE ht hk .faultIP v + rw [regIdxA_faultIP] at hW + obtain ⟨E1, w1, S1⟩ := hW + have hz : setReg x .faultIP v.bv = yieldRegA x .faultIP (x.tcbArch.tcbContext.userRegs .nextIP) := by + rw [← hvv] + rfl + have hal : Aligned (regA t layout.reg.FAULT_IP) := by + rw [← regIdxA_faultIP] + exact (a_reg hta .faultIP).1 + have hrv : (regA t layout.reg.FAULT_IP).val = t.val + 528 := by + rw [← regIdxA_faultIP, regA_val hta (regIdxA_lt _)] + rfl + have wx : WritesIn st s (L.wr s (regA t layout.reg.FAULT_IP) v) (· = regA t layout.reg.FAULT_IP) := + writesIn_wr hal + set z := setReg x .faultIP v.bv with hzdef + have kz : (updK σ (specPtr t) (.tcb z)).kheap (specPtr t) = some (.tcb z) := by + rw [updK_kheap, Function.update_self] + have far : ∀ q, q ≠ specPtr t → (updK σ (specPtr t) (.tcb z)).kheap q = σ.kheap q := fun q hq => by + rw [updK_kheap, Function.update_of_ne hq] + have H' : IpcSchedRelA st Z m κ (L.wr s (regA t layout.reg.FAULT_IP) v) (updK σ (specPtr t) (.tcb z)) := by + obtain ⟨Q, RR, H2⟩ := H + have step := yieldTcbStepA (σ := σ) (p := specPtr t) (x := x) (z := z) hk rfl rfl rfl rfl rfl + exact ⟨Q, RR, H2.stable w1 (fun _ hb => endpWriteOffRead H2.toSchedSRN P hb) step.1 step.2⟩ + have hag := agree_wr (L := L) (s := s) (v := v) hal + have RN' : NtfnStateRel N Z.T (L.mem (L.wr s (regA t layout.reg.FAULT_IP) v)) + (updK σ (specPtr t) (.tcb z)) := by + refine RN.frameX hP.n (hag.mono fun b hb e => ?_) (nkview_upd hk (fun _ h => by cases h) + fun y hy => by + cases hy + refine ⟨z, rfl, rfl, ?_, rfl⟩ + rw [hzdef] + show Function.update x.tcbArch.tcbContext.userRegs .faultIP v.bv badge_register = + x.tcbArch.tcbContext.userRegs badge_register + exact Function.update_of_ne (by decide) _ _) + rw [hrv] at e + rcases NKeep.vals hP.n hb with ⟨n, hn, h1, h2⟩ | ⟨y, hy, h1⟩ | ⟨y, hy, -, h1⟩ + · have := hP.n.disj n hn t ht; omega + · by_cases ey : y = t + · subst y; omega + · have := tcb_disj PE ht hy ey; omega + · by_cases ey : y = t + · subst y; omega + · have := tcb_disj PE ht hy ey; omega + have HS' : ReplyStackRelA (L.mem (L.wr s (regA t layout.reg.FAULT_IP) v)) R Z.C + (updK σ (specPtr t) (.tcb z)) (scNameA m) stack := by + refine HS.frame (fun d hd => ?_) (fun c _ y k hy => ⟨y, k, ?_, rfl⟩) (fun d _ y hy => ⟨y, ?_, rfl⟩) + · have n := replyNextPlaceA (Z := Z) PE hd + have p := replyPrevPlaceA (Z := Z) PE hd + have := PE.rt d hd t ht + exact ⟨wx.mem_eq n.1 (fun e => by have := congrArg UScalar.val e; omega), + wx.mem_eq p.1 (fun e => by have := congrArg UScalar.val e; omega)⟩ + · rw [far _ (fun e => by rw [e, hk] at hy; cases hy)]; exact hy + · by_cases e : specPtr d = specPtr t + · rw [e, kz] at hy; cases hy + · rw [far _ e] at hy; exact hy + refine ⟨updK σ (specPtr t) (.tcb z), fun β f => ?_, RN', H', E1, HS', wx, ⟨z, kz, rfl⟩, far⟩ + rw [nd_bind_oneSc hgs] + unfold Fl4Spec.NonDet.when + rw [if_pos hr] + refine nd_bind_oneSc ?_ _ + rw [suspendCopyPcA hk, ← hz] + rfl + +end + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + +/-- `set_thread_state t Inactive` keeps the endpoint and notification relations and the reply +stack, for a thread with no pending IPC. -/ +theorem suspendInactiveIpcA (hP : IPlacedA N Z.T E R Bf) (P : IpcSchedPlacementA Z.th N E R Bf) + {s : S} {σ : State Unit} {Q : U64 → U64 → List U64} {RR : U64 → List U64} {p0 : ObjRef} + {stack : U64 → List U64} (H : SchedSRB st { Z with Q := Q, R := RR } m κ s σ p0) + (hst : StEqA Z.T (L.mem s) σ) + (RN : NtfnStateRel N Z.T (L.mem s) σ) (HE : EpStateRel Z.T E R Bf (L.mem s) σ) + (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {t : U64} (ht : t ∈ Z.T) {x : Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hfree : IpcFreeA x.tcbState) : + sched.set_thread_state st s t sched.TS_INACTIVE ⦃ s' => + NtfnStateRel N Z.T (L.mem s') (stsActA (updKSc σ (specPtr t) (.tcb { x with tcbState := .inactive })) (specPtr t)) ∧ + EpStateRel Z.T E R Bf (L.mem s') (stsActA (updKSc σ (specPtr t) (.tcb { x with tcbState := .inactive })) (specPtr t)) ∧ + ReplyStackRelA (L.mem s') R Z.C + (stsActA (updKSc σ (specPtr t) (.tcb { x with tcbState := .inactive })) (specPtr t)) (scNameA m) stack ⦄ := by + have PE := hP.e + have hta := PE.tcb t ht + have hcode : (L.mem s (stA t)).val = tsCode x.tcbState := (HE.at_tcb ht hk).st + obtain ⟨f1, f2, f3, f4⟩ := hfree + have h3 : (L.mem s (stA t)).val ≠ 3 := by + rw [hcode]; intro h + obtain ⟨e, r, d, h⟩ := tsCode_three h + exact f2 _ _ _ h + have h4 : (L.mem s (stA t)).val ≠ 4 := by + rw [hcode]; intro h + obtain ⟨e, d, h⟩ := tsCode_four h + exact f1 _ _ h + have h6 : (L.mem s (stA t)).val ≠ 6 := by + rw [hcode]; intro h + obtain ⟨n, h⟩ := tsCode_six' h + exact f3 _ h + have hqE := notqE_of_code HE h3 h4 + have hqN := notqN_of_code RN h6 + obtain ⟨E1, -, -⟩ := write_state_rel PE s σ HE ht hk hqE .inactive (by decide) sched.TS_INACTIVE + (by unfold sched.TS_INACTIVE; rfl) + obtain ⟨hw, hoth⟩ := twr_wr (st := st) hP.n ht (K := 3328#u64) (Or.inl rfl) s sched.TS_INACTIVE + rw [← stA_eq] at hw hoth + have hx := RN.at_tcb ht hk + have RN1 : NtfnStateRel N Z.T (L.mem (L.wr s (stA t) sched.TS_INACTIVE)) + (updK σ (specPtr t) (.tcb { x with tcbState := .inactive })) := by + refine RN.upd_tcb ht _ hw.ntfn hw.tcb hw.links (Or.inl hqN) ⟨?_, ?_, ?_, ?_⟩ + · rw [rd_wr_eq (foot_st hP.n ht).1]; unfold sched.TS_INACTIVE; rfl + · intro n hn; cases hn + · show (L.mem _ (badgeRegA t)).bv = badgeOf x + rw [badgeRegA_eq, hoth _ (by simp) (by simp) (by simp), ← badgeRegA_eq]; exact hx.badge + · show (L.mem _ (prioA t)).val = x.tcbPriority.toNat + rw [prioA_eq', hoth _ (by simp) (by simp) (by simp), ← prioA_eq']; exact hx.prio + have hal : Aligned (stA t) := (foot_st hP.n ht).1 + have w1 : WritesIn st s (L.wr s (stA t) sched.TS_INACTIVE) (· = stA t) := writesIn_wr hal + have hsv : (stA t).val = t.val + 3328 := val_st (hP.n.tcb t ht) + have HS1 : ReplyStackRelA (L.mem (L.wr s (stA t) sched.TS_INACTIVE)) R Z.C + (updK σ (specPtr t) (.tcb { x with tcbState := .inactive })) (scNameA m) stack := by + refine HS.frame (fun d hd => ?_) (fun c _ y k hy => ⟨y, k, ?_, rfl⟩) (fun d _ y hy => ⟨y, ?_, rfl⟩) + · have n := replyNextPlaceA (Z := Z) PE hd + have p := replyPrevPlaceA (Z := Z) PE hd + have := PE.rt d hd t ht + exact ⟨w1.mem_eq n.1 (fun e => by have := congrArg UScalar.val e; omega), + w1.mem_eq p.1 (fun e => by have := congrArg UScalar.val e; omega)⟩ + · rw [updK_kheap, Function.update_of_ne (fun e => by rw [e, hk] at hy; cases hy)]; exact hy + · by_cases e : specPtr d = specPtr t + · rw [e, updK_kheap, Function.update_self] at hy; cases hy + · rw [updK_kheap, Function.update_of_ne e] at hy; exact hy + unfold sched.set_thread_state + refine WP.spec_bind (obj.Tcb.set_state.spec s t sched.TS_INACTIVE) ?_ + intro s1 hs1 + subst hs1 + refine WP.spec_mono (schedule_tcb_act (st := st) (L.wr s (stA t) sched.TS_INACTIVE) t) ?_ + intro s' h + have hσk : (stsActA (updKSc σ (specPtr t) (.tcb { x with tcbState := .inactive })) (specPtr t)).kheap = + (updK σ (specPtr t) (.tcb { x with tcbState := .inactive })).kheap := by + unfold stsActA + split <;> rfl + have KN : Notif.KView (updK σ (specPtr t) (.tcb { x with tcbState := .inactive })) + (stsActA (updKSc σ (specPtr t) (.tcb { x with tcbState := .inactive })) (specPtr t)) := + Notif.KView.stsAct _ _ + have KE : Endp.KView (updK σ (specPtr t) (.tcb { x with tcbState := .inactive })) + (stsActA (updKSc σ (specPtr t) (.tcb { x with tcbState := .inactive })) (specPtr t)) := + KView.stsAct' _ _ + have HS2 : ReplyStackRelA (L.mem (L.wr s (stA t) sched.TS_INACTIVE)) R Z.C + (stsActA (updKSc σ (specPtr t) (.tcb { x with tcbState := .inactive })) (specPtr t)) (scNameA m) stack := + HS1.frame (fun _ _ => ⟨rfl, rfl⟩) (fun c _ y k hy => ⟨y, k, by rw [hσk]; exact hy, rfl⟩) + (fun d _ y hy => ⟨y, by rw [hσk] at hy; exact hy, rfl⟩) + rcases h with rfl | rfl + · exact ⟨RN1.frame hP.n (fun _ _ _ => rfl) KN, E1.frame PE (fun _ _ _ => rfl) KE, HS2⟩ + · obtain ⟨s2, hs2⟩ : ∃ s2, s2 = L.wr s (stA t) sched.TS_INACTIVE := ⟨_, rfl⟩ + have hA : actA (L := L) s2 = L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION := by + show L.nodeAt s2 (L.cpu s2) layout.node.SCHED_ACTION = _ + rw [hs2, nodeAt_of_ksb w1.1, w1.2.1] + have hAal : Aligned (actA (L := L) s2) := by + rw [hA] + exact nodeAt_aligned (H.core.nodes _ H.sch.cpuCs) (by simp [layout.node.SCHED_ACTION]) + (by simp [layout.node.SCHED_ACTION]) + rw [← hs2] at RN1 E1 HS2 ⊢ + have w2 := writesIn_wr (st := st) (s := s2) (v := sched.ACTION_CHOOSE_NEW) hAal + have hn := H.node_in H.sch.cpuCs (o := layout.node.SCHED_ACTION) (by simp [layout.node.SCHED_ACTION]) + -- the action word is outside every object + have off : ∀ (p b : U64) (k : ObjKindA), Z.th p = some k → k ≠ .cnode 0 → p.val ≤ b.val → + b.val < p.val + k.size → b ≠ actA (L := L) s2 := by + intro p b k hp hne hlo hhi e + have hK := H.place.ks p k hp (fun irq => hne (H.place.irqK hp irq)) + rw [e, hA] at hlo hhi + omega + refine ⟨RN1.frame hP.n (fun b hb hf => w2.mem_eq hb fun e => ?_) KN, + E1.frame PE (fun b hb hf => w2.mem_eq hb fun e => ?_) KE, + HS2.frame (fun d hd => ?_) (fun c _ y k hy => ⟨y, k, hy, rfl⟩) (fun d _ y hy => ⟨y, hy, rfl⟩)⟩ + · rcases hf with ⟨n, hnN, g1, g2⟩ | ⟨u, hu, hf⟩ + · exact off n b _ (P.notification n hnN) (by intro h; cases h) g1 (by change b.val < n.val + 64; omega) e + · have hua := hP.n.tcb u hu + have hval : b.val = u.val + 3328 ∨ b.val = u.val + 3336 ∨ b.val = u.val + 256 ∨ + b.val = u.val + 3448 ∨ b.val = u.val + 3496 ∨ b.val = u.val + 3504 := by + rcases hf with h | h | h | h | h | h <;> + rw [h] <;> + simp only [stA_eq, Notif.tBlockA_eq, Notif.badgeRegA_eq, Notif.prioA_eq', Notif.nextEp_eq, + Notif.prevEp_eq] <;> + rw [Notif.tfield_val hua] <;> simp + exact off u b _ ((H.thT u).2 hu) (by intro h; cases h) (by omega) (by change b.val < u.val + 4096; omega) e + · rcases hf with ⟨ep, hep, g⟩ | ⟨r, hr, g⟩ | ⟨u, hu, g | g⟩ | g + · exact off ep b _ (P.endpoint ep hep) (by intro h; cases h) (by omega) + (by change b.val < ep.val + 16; omega) e + · exact off r b _ (P.reply r hr) (by intro h; cases h) (by omega) + (by change b.val < r.val + 32; omega) e + · have g1 := g.1 + have g2 := g.2 + unfold TOffN at g2 + exact off u b _ ((H.thT u).2 hu) (by intro h; cases h) g1 (by change b.val < u.val + 4096; omega) e + · unfold TL at g + exact off u b _ ((H.thT u).2 hu) (by intro h; cases h) (by omega) + (by change b.val < u.val + 4096; omega) e + · obtain ⟨p, len, hp, g1, g2⟩ := P.buffer b g + exact off p b _ hp (by intro h; cases h) g1 g2 e + · have n := replyNextPlaceA (Z := Z) PE hd + have p := replyPrevPlaceA (Z := Z) PE hd + exact ⟨w2.mem_eq n.1 (off d _ _ (P.reply d hd) (by intro h; cases h) (by omega) + (by change _ < d.val + 32; omega)), + w2.mem_eq p.1 (off d _ _ (P.reply d hd) (by intro h; cases h) (by omega) + (by change _ < d.val + 32; omega))⟩ + +/-- **`sched.suspend` refines `fl4CancelRun; suspend`** (module docstring). -/ +theorem suspend_refinesA (hP : IPlacedA N Z.T E R Bf) (P : IpcSchedPlacementA Z.th N E R Bf) + (hc : m.curCore < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (s : S) (a : A) (σ : State Unit) {stack : U64 → List U64} + (H : IpcSchedRelA st Z m κ s σ) (RN : NtfnStateRel N Z.T (L.mem s) σ) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + (M : McpRelA Z.T m (L.mem s) σ) (Y : YieldRelA Z.T Z.C m (L.mem s) σ) (hC0 : ∀ c ∈ Z.C, c ≠ 0#u64) + {t : U64} (ht : t ∈ Z.T) {x : Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hReply : ∀ r', x.tcbState = .blockedOnReply r' → ∃ r ∈ R, specPtr r = r' ∧ + ∃ xr, σ.kheap r' = some (.reply xr) ∧ xr.replyTcb = some (specPtr t) ∧ ScReplyCovA Z.C m σ r') + (hE : ∀ e', epBlocked x.tcbState = some e' → L.mem s (tBlockA t) ∈ E ∧ + ∃ z, σ.kheap e' = some (.endpoint z) ∧ specPtr t ∈ z.epQueue) + (hN : ∀ n', x.tcbState = .blockedOnNotification n' → L.mem s (tBlockA t) ∈ N ∧ + ∃ z, σ.kheap n' = some (.notification z) ∧ specPtr t ∈ z.ntfnObj.ntfnQueue) + (hrep : RepOKA σ (specPtr t)) + (hrun0 : L.mem s (runVcpuA t) = 0#u64) (hrunS : x.tcbExt.tcbRunVcpu = none) + (hon : L.mem s (affA t) = L.cpu s) : + sched.suspend st ar s a t ⦃ out => out.2 = a ∧ ∃ σ' stack', + (fl4CancelRun (Z := Unit) (specPtr t) >>= fun _ => Fl4SpecA64.suspend (specPtr t)) σ = + ({((), σ')}, False) ∧ + NtfnStateRel N Z.T (L.mem out.1) σ' ∧ IpcSchedRelA st Z m κ out.1 σ' ∧ + EpStateRel Z.T E R Bf (L.mem out.1) σ' ∧ + ReplyStackRelA (L.mem out.1) R Z.C σ' (scNameA m) stack' ∧ + McpRelA Z.T m (L.mem out.1) σ' ∧ YieldRelA Z.T Z.C m (L.mem out.1) σ' ∧ + StEqA Z.T (L.mem out.1) σ' ∧ + ∃ y, σ'.kheap (specPtr t) = some (.tcb y) ∧ y.tcbState = .inactive ⦄ := by + have ops := ipcSchedOpsA (st := st) (ar := ar) (κ := κ) hP P hc hmb hmax htu + have hSched : CancelScheduleA st N Z.T (IpcSchedRelA st Z m κ) := cancelScheduleA_of_placement P + have PE := hP.e + have h1 := cancel_run_quietA (st := st) (ar := ar) s a t hrun0 + have s1 := fl4CancelRun_quietA hk hrunS + have hsusp : (fl4CancelRun (Z := Unit) (specPtr t) >>= fun _ => Fl4SpecA64.suspend (specPtr t)) σ = + Fl4SpecA64.suspend (specPtr t) σ := nd_bind_oneSc s1 _ + have hprog : sched.suspend st ar s a t = (do + let r ← ipc.cancel_ipc st ar s a t + let s3 ← suspendPcProgA st r.1 t + let s4 ← sched.dequeue st s3 t + let s5 ← sched.release_remove st s4 t + let s6 ← schedcontext.cancel_yield_to st s5 t + let s7 ← sched.set_thread_state st s6 t sched.TS_INACTIVE + ok (s7, r.2)) := by + unfold sched.suspend suspendPcProgA + rw [h1] + simp only [bind_ok, Std.bind_assoc] + rfl + rw [hprog] + -- cancel_ipc + refine WP.spec_bind (suspendCancelA ops.1 hSched ops.2.2.1 hP P s a σ H RN HE HS ht hk hReply hE hN hrep hC0) ?_ + rintro r ⟨ha, σ1, stk1, hc1, RN1, H1, HE1, HS1, w1, K1, x1, hk1, hfree⟩ + obtain ⟨Q0, R0, H0⟩ := id H + have hoff : ∀ u ∈ Z.T, ∀ K : U64, (K = 3440#u64 ∨ K = 3472#u64 ∨ K = 3520#u64) → + L.mem r.1 (wa u K) = L.mem s (wa u K) := by + intro u hu K hK + refine w1.mem_eq (field_aligned (H0.core.tcbs u hu) ?_ ?_) (suspendOffTcbA H0.toSchedSRN hP hu hK) + · rcases hK with rfl | rfl | rfl <;> simp + · rcases hK with rfl | rfl | rfl <;> simp + have hoffF : ∀ u ∈ Z.T, L.mem r.1 (flagsA u) = L.mem s (flagsA u) := fun u hu => + w1.mem_eq (field_aligned (H0.core.tcbs u hu) (K := 3456#u64) (by decide) (by decide)) + (suspendOffFlagsA H0.toSchedSRN hP hu) + -- FAULT_IP := NEXT_IP + refine WP.spec_bind (suspendRestartPcA hP P RN1 H1 HE1 HS1 ht hk1) ?_ + rintro s3 ⟨σ3, hpc, Hpc⟩ + obtain ⟨x3, hk3, hx3⟩ := Hpc.tcb + have hst3 : x3.tcbState = x1.tcbState := by rw [hx3] + have w13 : WritesIn st s s3 (fun b => + CancelReplyWA N Z.T Z.C E R Bf (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) b ∨ + b = regA t layout.reg.FAULT_IP) := w1.trans Hpc.wr + have hrv : (regA t layout.reg.FAULT_IP).val = t.val + 528 := by + rw [← regIdxA_faultIP, regA_val (PE.tcb t ht) (regIdxA_lt _)] + rfl + have hoff3 : ∀ u ∈ Z.T, ∀ K : U64, (K = 3440#u64 ∨ K = 3472#u64 ∨ K = 3520#u64) → + L.mem s3 (wa u K) = L.mem s (wa u K) := by + intro u hu K hK + have hua := H0.core.tcbs u hu + have hKv : K.val = 3440 ∨ K.val = 3472 ∨ K.val = 3520 := by rcases hK with rfl | rfl | rfl <;> simp + have hv : (wa u K).val = u.val + K.val := Notif.tfield_val hua (by omega) + rw [← hoff u hu K hK] + refine Hpc.wr.mem_eq (field_aligned hua (by omega) (by rcases hK with rfl | rfl | rfl <;> simp)) (fun e => ?_) + have e' := congrArg UScalar.val e + rw [hv, hrv] at e' + by_cases h : u = t + · subst u; omega + · have := tcb_disj PE ht hu h; omega + have hoff3F : ∀ u ∈ Z.T, L.mem s3 (flagsA u) = L.mem s (flagsA u) := fun u hu => by + have hua := H0.core.tcbs u hu + have hv : (flagsA u).val = u.val + 3456 := by + rw [show flagsA u = wa u 3456#u64 from rfl, Notif.tfield_val hua (by decide)]; rfl + rw [← hoffF u hu] + refine Hpc.wr.mem_eq (field_aligned hua (K := 3456#u64) (by decide) (by decide)) (fun e => ?_) + have e' := congrArg UScalar.val e + rw [hv, hrv] at e' + by_cases h : u = t + · subst u; omega + · have := tcb_disj PE ht hu h; omega + -- MCP and yield links + have htcb3 : ∀ u ∈ Z.T, ∀ y, σ.kheap (specPtr u) = some (.tcb y) → + ∃ y', σ3.kheap (specPtr u) = some (.tcb y') ∧ y'.tcbMcpriority = y.tcbMcpriority ∧ + y'.tcbYieldTo = y.tcbYieldTo ∧ y'.tcbFlags = y.tcbFlags := by + intro u _ y hy + obtain ⟨y1, hy1, e1⟩ := K1.tcb _ _ hy + by_cases e : specPtr u = specPtr t + · rw [e, hk1] at hy1 + cases hy1 + exact ⟨x3, by rw [e]; exact hk3, by rw [hx3, e1], by rw [hx3, e1], by rw [hx3, e1]⟩ + · exact ⟨y1, by rw [Hpc.far _ e]; exact hy1, by rw [e1], by rw [e1], by rw [e1]⟩ + have hsc3 : ∀ c ∈ Z.C, ∃ y n y', σ.kheap (scNameA m c) = some (.schedContext y n) ∧ + σ3.kheap (scNameA m c) = some (.schedContext y' n) ∧ y'.scYieldFrom = y.scYieldFrom := by + intro c hc' + obtain ⟨y, n, hy, -⟩ := H0.scr.sc c hc' + obtain ⟨y', hy', e⟩ := K1.sc _ _ _ hy + refine ⟨y, n, y', hy, ?_, by rw [e]⟩ + rw [Hpc.far _ (fun h => by rw [h, hk1] at hy'; cases hy')] + exact hy' + obtain ⟨M3, Y3⟩ := yieldFrameA (mem' := L.mem s3) (σ' := σ3) M Y + (fun u hu => ⟨hoff3 u hu 3440#u64 (Or.inl rfl), hoff3F u hu⟩) + (fun u hu => hoff3 u hu 3472#u64 (Or.inr (Or.inl rfl))) + htcb3 hsc3 + have hon3 : L.mem s3 (affA t) = L.cpu s3 := by + rw [w13.2.1, ← hon] + exact hoff3 t ht 3520#u64 (Or.inr (Or.inr rfl)) + -- dequeue + obtain ⟨Q, RR, H3⟩ := Hpc.sched + have ht' : t ∈ ({ Z with Q := Q, R := RR } : XSetsA).T := ht + refine WP.spec_bind (dequeue_refines_nodeA H3.toSchedSRN ht' hon3) ?_ + rintro s4 ⟨σ4, hdq, hk4h, hrel4, hfix4, -, -, H4N, F4⟩ + have H4 : SchedSRA st { Z with Q := deqQA (st := st) s3 t Q, R := RR } m κ s4 σ4 := + ⟨H4N, RdA.of_eq H3.rd hk4h hfix4.curTime hrel4⟩ + have hready : ∀ d p, specPtr t ∉ σ4.readyQueues d p := H3.toSchedSRN.ready_after_dequeueA ht' H4N + have hms4 : σ4.machineState = σ3.machineState := by + obtain ⟨y, hy, hd, -, -⟩ := H3.rest.prio t ht + have h := (tcbSchedAction_eqA hy hd Fl4SpecA64.tcbSchedDequeue).symm.trans hdq + have e := congrArg (fun p : Unit × State Unit => p.2.machineState) + (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst h)) + exact e.symm + -- frames across a scheduler step + have frame : ∀ (s0 s9 : S) (σ0 σ9 : State Unit) (Q9 : U64 → U64 → List U64) (R9 : U64 → List U64) + (H9 : SchedSRN st { Z with Q := Q9, R := R9 } m κ s0 σ0), + WritesIn st s0 s9 (MWrA (st := st) { Z with Q := Q9, R := R9 } m σ0 s0) → + σ9.kheap = σ0.kheap → σ9.machineState = σ0.machineState → + NtfnStateRel N Z.T (L.mem s0) σ0 → EpStateRel Z.T E R Bf (L.mem s0) σ0 → + ReplyStackRelA (L.mem s0) R Z.C σ0 (scNameA m) stk1 → StEqA Z.T (L.mem s0) σ0 → + NtfnStateRel N Z.T (L.mem s9) σ9 ∧ EpStateRel Z.T E R Bf (L.mem s9) σ9 ∧ + ReplyStackRelA (L.mem s9) R Z.C σ9 (scNameA m) stk1 ∧ StEqA Z.T (L.mem s9) σ9 ∧ + (∀ u ∈ Z.T, ∀ K : U64, (K = 3440#u64 ∨ K = 3472#u64 ∨ K = 3520#u64) → + L.mem s9 (wa u K) = L.mem s0 (wa u K)) ∧ + (∀ u ∈ Z.T, L.mem s9 (flagsA u) = L.mem s0 (flagsA u)) := by + intro s0 s9 σ0 σ9 Q9 R9 H9 w hkh hms RN0 E0 S0 T0 + have KV : Endp.KView σ0 σ9 := ⟨fun _ _ h => by rw [hkh]; exact h, fun _ z h => ⟨z, by rw [hkh]; exact h, rfl⟩, + fun _ z h => ⟨z, by rw [hkh]; exact h, rfl, rfl, rfl, rfl, rfl⟩, by rw [hms]⟩ + have off : ∀ u ∈ Z.T, ∀ K : U64, (K = 3328#u64 ∨ K = 3440#u64 ∨ K = 3472#u64) → + L.mem s9 (wa u K) = L.mem s0 (wa u K) := by + intro u hu K hK + refine w.mem_eq (field_aligned (H9.core.tcbs u hu) ?_ ?_) (yieldTcbNotMWrA H9 hu hK) + · rcases hK with rfl | rfl | rfl <;> simp + · rcases hK with rfl | rfl | rfl <;> simp + refine ⟨RN0.frame hP.n (fun b hb hf => w.mem_eq hb fun h => notifFootOffMWrA H9 P.notification hf h) + ⟨fun _ _ h => by rw [hkh]; exact h, fun _ z h => ⟨z, by rw [hkh]; exact h, rfl, rfl, rfl⟩⟩, + E0.frame PE (epFrameOfMcsA H9 P (σ := σ0) (σ' := σ0) ⟨w, SKeepA.refl _, SSzA.refl _⟩ rfl).1 KV, + S0.frame (fun d hd => ?_) (fun c _ y k hy => ⟨y, k, by rw [hkh]; exact hy, rfl⟩) + (fun d _ y hy => ⟨y, by rw [hkh] at hy; exact hy, rfl⟩), fun u hu y hy => ?_, fun u hu K hK => ?_, + fun u hu => w.mem_eq (field_aligned (H9.core.tcbs u hu) (K := 3456#u64) (by decide) (by decide)) + (yieldFlagsNotMWrA H9 hu)⟩ + · have n := replyNextPlaceA (Z := Z) PE hd + have p := replyPrevPlaceA (Z := Z) PE hd + exact ⟨w.mem_eq n.1 (replyBlockOffMWrA H9 P hd (by omega) (by omega)), + w.mem_eq p.1 (replyBlockOffMWrA H9 P hd (by omega) (by omega))⟩ + · rw [hkh] at hy + rw [stA_eq, off u hu 3328#u64 (Or.inl rfl), ← stA_eq] + exact T0 u hu y hy + · rcases hK with h | h | h + · exact off u hu K (Or.inr (Or.inl h)) + · exact off u hu K (Or.inr (Or.inr h)) + · subst h + have hua := H9.core.tcbs u hu + have hv : (wa u 3520#u64).val = u.val + 3520 := by rw [Notif.tfield_val hua] <;> simp + refine w.mem_eq (field_aligned hua (by simp) (by simp)) (fun h => ?_) + have hty := (H9.thT u).2 hu + have hK' : (wa u 3520#u64).val < (L.ksb s0).val ∨ (L.ksb s0).val + ksSizeA ≤ (wa u 3520#u64).val := by + have h := H9.place.ks u _ hty (fun irq => by + have := H9.place.irqK hty irq + cases this) + change u.val + 4096 ≤ (L.ksb s0).val ∨ _ at h + omega + have hS : ¬ ScFootA Z.C m σ0 (wa u 3520#u64) := by + intro hf + have hd := H9.scp.th_out hf hty + change (wa u 3520#u64).val < u.val ∨ u.val + 4096 ≤ (wa u 3520#u64).val at hd + omega + refine not_mwrA hS (fun v hv' => ?_) hK' h + have hva := H9.core.tcbs v hv' + refine ⟨field_ne hua hva (by decide) (by decide) (fun _ => by decide), + field_ne hua hva (by decide) (by decide) (fun _ => by decide), + field_ne hua hva (by decide) (by decide) (fun _ => by decide), + field_ne hua hva (by decide) (by decide) (fun _ => by decide)⟩ + have T3 : StEqA Z.T (L.mem s3) σ3 := fun u hu y hy => (Hpc.ep.at_tcb hu hy).st + obtain ⟨RN4, E4, S4, T4, off4, offF4⟩ := frame s3 s4 σ3 σ4 Q RR H3.toSchedSRN F4.wr hk4h hms4 Hpc.ntfn Hpc.ep + Hpc.stk T3 + -- release_remove + have hon4 : L.mem s4 (affA t) = L.cpu s4 := by + rw [F4.wr.2.1, ← hon3] + exact off4 t ht 3520#u64 (Or.inr (Or.inr rfl)) + refine WP.spec_bind (release_remove_refines_localA H4 (t := t) ht hon4) ?_ + rintro s5 ⟨R5, -, H5, F5⟩ + obtain ⟨σ5, hσ5⟩ : ∃ σ5, σ5 = releaseRemoveStA σ4 (specPtr t) := ⟨_, rfl⟩ + rw [← hσ5] at H5 F5 + have hk5h : σ5.kheap = σ4.kheap := by rw [hσ5]; rfl + have hms5 : σ5.machineState = σ4.machineState := by rw [hσ5]; rfl + have hrq5 : σ5.readyQueues = σ4.readyQueues := by rw [hσ5]; rfl + have hnr5 : specPtr t ∉ σ5.releaseQueue := by rw [hσ5]; exact releaseRemoveStA_notMem _ _ + obtain ⟨RN5, E5, S5, T5, off5, offF5⟩ := frame s4 s5 σ4 σ5 _ RR H4N F5.wr hk5h hms5 RN4 E4 S4 T4 + have hmy : ∀ u ∈ Z.T, ∀ K : U64, (K = 3440#u64 ∨ K = 3472#u64) → L.mem s5 (wa u K) = L.mem s3 (wa u K) := + fun u hu K hK => by + have hK' : K = 3440#u64 ∨ K = 3472#u64 ∨ K = 3520#u64 := hK.imp id Or.inl + rw [off5 u hu K hK', off4 u hu K hK'] + have hk53 : σ5.kheap = σ3.kheap := hk5h.trans hk4h + obtain ⟨M5, Y5⟩ := yieldFrameA (mem' := L.mem s5) (σ' := σ5) M3 Y3 + (fun u hu => ⟨hmy u hu 3440#u64 (Or.inl rfl), by rw [offF5 u hu, offF4 u hu]⟩) + (fun u hu => hmy u hu 3472#u64 (Or.inr rfl)) + (fun u _ y hy => ⟨y, by rw [hk53]; exact hy, rfl, rfl, rfl⟩) + (fun c hc' => by + obtain ⟨y, n, hy, -⟩ := H3.scr.sc c hc' + exact ⟨y, n, y, hy, by rw [hk53]; exact hy, rfl⟩) + -- cancel_yield_to + refine WP.spec_bind (cancel_yield_to_refinesB H5 T5 M5 Y5 hC0 (t := t) ht) ?_ + rintro s6 ⟨σ6, hy6, H6, T6, M6, Y6, -, -, hrel6, hrq6, -, htcb6, hobj6, w6, hms6, hsc6⟩ + have hk5 : σ5.kheap (specPtr t) = some (.tcb x3) := by rw [hk53]; exact hk3 + obtain ⟨x6, hk6, hx6⟩ := htcb6 _ _ hk5 + -- the IPC relations across the yield-link writes + have hyv : (yieldA t).val = t.val + 3472 := by + change (wa t 3472#u64).val = _ + rw [Notif.tfield_val (PE.tcb t ht)] <;> simp + have tk : ∀ q z, σ5.kheap q = some (.tcb z) → ∃ z', σ6.kheap q = some (.tcb z') ∧ + z' = { z with tcbYieldTo := z'.tcbYieldTo } := htcb6 + have RN6 : NtfnStateRel N Z.T (L.mem s6) σ6 := by + refine RN5.frame hP.n (fun b hb hf => w6.mem_eq hb fun h => ?_) ⟨fun q z h => ?_, fun q z h => ?_⟩ + · rcases h with h | h + · exact notifFootOffMWrA H5.toSchedSRN P.notification hf h + · rw [h] at hf + rcases hf with ⟨n, hn, g1, g2⟩ | ⟨u, hu, hf⟩ + · have := hP.n.disj n hn t ht + omega + · have hua := PE.tcb u hu + have hval : (yieldA t).val = u.val + 3328 ∨ (yieldA t).val = u.val + 3336 ∨ + (yieldA t).val = u.val + 256 ∨ (yieldA t).val = u.val + 3448 ∨ + (yieldA t).val = u.val + 3496 ∨ (yieldA t).val = u.val + 3504 := by + rcases hf with h | h | h | h | h | h <;> + rw [h] <;> + (try simp only [stA_eq, Notif.tBlockA_eq, Notif.badgeRegA_eq, Notif.prioA_eq', Notif.nextEp_eq, + Notif.prevEp_eq]) <;> + rw [Notif.tfield_val hua] <;> simp + by_cases e : u = t + · subst u; omega + · have := tcb_disj PE ht hu e; omega + · exact hobj6 q _ h (fun _ _ e => by cases e) (fun _ e => by cases e) + · obtain ⟨z', hz', e⟩ := tk q z h + exact ⟨z', hz', by rw [e], by rw [e]; rfl, by rw [e]⟩ + have E6 : EpStateRel Z.T E R Bf (L.mem s6) σ6 := by + refine E5.frame PE (fun b hb hf => w6.mem_eq hb fun h => ?_) + ⟨fun q z h => ?_, fun q z h => ⟨z, ?_, rfl⟩, fun q z h => ?_, by rw [hms6]⟩ + · rcases h with h | h + · exact endpFootOffWrite H5.toSchedSRN P hf h + · rw [h] at hf + rcases hf with ⟨ep, hep, g⟩ | ⟨r', hr', g⟩ | ⟨u, hu, g | g⟩ | g + · have := PE.et ep hep t ht + omega + · have := PE.rt r' hr' t ht + omega + · obtain ⟨g1, g2⟩ := g + unfold TOffN at g2 + by_cases e : u = t + · subst u; omega + · have := tcb_disj PE ht hu e; omega + · unfold TL at g + by_cases e : u = t + · subst u; omega + · have := tcb_disj PE ht hu e; omega + · have := PE.bt _ g t ht + omega + · exact hobj6 q _ h (fun _ _ e => by cases e) (fun _ e => by cases e) + · exact hobj6 q _ h (fun _ _ e => by cases e) (fun _ e => by cases e) + · obtain ⟨z', hz', e⟩ := tk q z h + exact ⟨z', hz', by rw [e], by rw [e], by rw [e], by rw [e], by rw [e]⟩ + have S6 : ReplyStackRelA (L.mem s6) R Z.C σ6 (scNameA m) stk1 := by + refine S5.frame (fun d hd => ?_) (fun c _ y n hy => ?_) (fun d hd y hy => ?_) + · have nx := replyNextPlaceA (Z := Z) PE hd + have pv := replyPrevPlaceA (Z := Z) PE hd + have off : ∀ b : U64, d.val ≤ b.val → b.val < d.val + 32 → b ≠ yieldA t := by + intro b g1 g2 e + have := PE.rt d hd t ht + rw [e, hyv] at g1 g2 + omega + exact ⟨w6.mem_eq nx.1 (fun h => h.elim (replyBlockOffMWrA H5.toSchedSRN P hd (by omega) (by omega)) + (off _ (by omega) (by omega))), + w6.mem_eq pv.1 (fun h => h.elim (replyBlockOffMWrA H5.toSchedSRN P hd (by omega) (by omega)) + (off _ (by omega) (by omega)))⟩ + · obtain ⟨y', hy', e⟩ := hsc6 _ _ _ hy + exact ⟨y', n, hy', by rw [e]⟩ + · obtain ⟨z, hz, -⟩ := E5.reply d hd + have h6 := hobj6 _ _ hz (fun _ _ e => by cases e) (fun _ e => by cases e) + rw [h6] at hy + cases hy + exact ⟨_, hz, rfl⟩ + -- set Inactive + have hnq : (∀ d p, specPtr t ∉ σ6.readyQueues d p) ∧ specPtr t ∉ σ6.releaseQueue := by + rw [hrq6, hrq5, hrel6] + exact ⟨hready, hnr5⟩ + have hfree6 : IpcFreeA x6.tcbState := by + rw [hx6] + change IpcFreeA x3.tcbState + rw [hst3] + exact hfree + refine WP.spec_bind (WP.spec_and (set_thread_state_inactiveExA H6 T6 ht hk6 hnq) + (suspendInactiveIpcA hP P H6 T6 RN6 E6 S6 ht hk6 hfree6)) ?_ + rintro s7 ⟨⟨σ7, hsts, hkh7, H7, T7, w7⟩, RN7, E7, S7⟩ + have hσ7 : σ7 = stsActA (updKSc σ6 (specPtr t) (.tcb { x6 with tcbState := .inactive })) (specPtr t) := + congrArg Prod.snd (Set.singleton_eq_singleton_iff.1 + (congrArg Prod.fst (hsts.symm.trans (setThreadState_stsActExA hk6 _)))) + rw [← hσ7] at RN7 E7 S7 + have h7t : σ7.kheap (specPtr t) = some (.tcb { x6 with tcbState := .inactive }) := by + rw [hkh7, updKSc_same] + have h7far : ∀ q, q ≠ specPtr t → σ7.kheap q = σ6.kheap q := fun q hq => by + rw [hkh7, updKSc_kheap, Function.update_of_ne hq] + have off7 : ∀ u ∈ Z.T, ∀ K : U64, (K = 3440#u64 ∨ K = 3472#u64 ∨ K = 3456#u64) → + L.mem s7 (wa u K) = L.mem s6 (wa u K) := by + intro u hu K hK + have hua := PE.tcb u hu + have hKv : K.val = 3440 ∨ K.val = 3472 ∨ K.val = 3456 := by rcases hK with rfl | rfl | rfl <;> simp + have hv : (wa u K).val = u.val + K.val := Notif.tfield_val hua (by omega) + refine w7.mem_eq (field_aligned (H6.core.tcbs u hu) (by omega) (by rcases hK with rfl | rfl | rfl <;> simp)) + (fun h => ?_) + rcases h with h | h + · have h' := congrArg UScalar.val h + rw [hv, val_st (hP.n.tcb t ht)] at h' + by_cases e : u = t + · subst u; omega + · have := tcb_disj PE ht hu e; omega + · have hty := (H6.thT u).2 hu + have hK' : (wa u K).val < (L.ksb s6).val ∨ (L.ksb s6).val + ksSizeA ≤ (wa u K).val := by + have g := H6.place.ks u _ hty (fun irq => by + have := H6.place.irqK hty irq + cases this) + change u.val + 4096 ≤ (L.ksb s6).val ∨ _ at g + omega + have hn := H6.node_in H6.sch.cpuCs (o := layout.node.SCHED_ACTION) (by simp [layout.node.SCHED_ACTION]) + rw [h] at hK' + omega + obtain ⟨M7, Y7⟩ := yieldFrameA (mem' := L.mem s7) (σ' := σ7) M6 Y6 + (fun u hu => ⟨off7 u hu 3440#u64 (Or.inl rfl), off7 u hu 3456#u64 (Or.inr (Or.inr rfl))⟩) + (fun u hu => off7 u hu 3472#u64 (Or.inr (Or.inl rfl))) + (fun u _ y hy => by + by_cases e : specPtr u = specPtr t + · rw [e, hk6] at hy + cases hy + exact ⟨{ x6 with tcbState := .inactive }, by rw [e]; exact h7t, rfl, rfl, rfl⟩ + · exact ⟨y, by rw [h7far _ e]; exact hy, rfl, rfl, rfl⟩) + (fun c hc' => by + obtain ⟨y, n, hy, -⟩ := H6.scr.sc c hc' + exact ⟨y, n, y, hy, by rw [h7far _ (fun h => by rw [h, hk6] at hy; cases hy)]; exact hy, rfl⟩) + rw [WP.spec_ok] + refine ⟨ha, σ7, stk1, ?_, RN7, ⟨_, _, H7⟩, E7, S7, M7, Y7, T7, _, h7t, rfl⟩ + rw [hsusp] + unfold Fl4SpecA64.suspend + rw [nd_bind_oneSc hc1, hpc, nd_bind_oneSc hdq, nd_bind_oneSc (tcbReleaseRemove_eqA σ4 _), ← hσ5, + nd_bind_oneSc hy6] + exact hsts + +end +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/TcbSuspendCancelA.lean b/proofs/Fl4ProofsA64/P3/TcbSuspendCancelA.lean new file mode 100644 index 000000000..58e2ebfe4 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbSuspendCancelA.lean @@ -0,0 +1,245 @@ +/- +P3 (AArch64): the cancellation prefix of `sched.suspend` (every IPC state), and the frames the +rest of the composition needs. + +* **`suspendCancelA`**: `ipc.cancel_ipc` refines l4v `cancel_ipc` in every thread state: the + `BlockedOnReply` arm is `cancel_ipc_replyA`, the others `cancel_ipc_rel`, `cancel_ipc_footA` + and `cancelIpc_keepA`. Result `SuspendCancelPostA`. +* **`suspendOffTcbA`**: the writes of the cancellation miss the TCBs' MCP, yield-target and + affinity words; **`replyBlockOffMWrA`**: the scheduler's writes miss every reply word. + +Premises on the caller side: the object the thread is blocked on is tracked and holds it +(endpoint / notification), a receiver's reply points back (`RepOKA`), the reply of a +`BlockedOnReply` thread is tracked, points back and its SCs are covered. + +No named premise. +-/ +import Fl4ProofsA64.P3.CancelIpcReplyA +import Fl4ProofsA64.P3.CancelIpcKeepA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4SpecA64 Sched + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + +/-- A reply object's words are outside the scheduler write footprint. -/ +theorem replyBlockOffMWrA {s : S} {σ : State Unit} (H : SchedSRN st Z m κ s σ) + (P : IpcSchedPlacementA Z.th N E R Bf) {r b : U64} (hr : r ∈ R) (hlo : r.val ≤ b.val) + (hhi : b.val < r.val + 32) : ¬ MWrA (st := st) Z m σ s b := by + have hp := P.reply r hr + have hK : b.val < (L.ksb s).val ∨ (L.ksb s).val + ksSizeA ≤ b.val := by + have h := H.place.ks r _ hp (fun irq => by + have := H.place.irqK hp irq + cases this) + change r.val + 32 ≤ (L.ksb s).val ∨ _ at h + omega + have hS : ¬ ScFootA Z.C m σ b := by + intro h + have hd := H.scp.th_out h hp + change b.val < r.val ∨ r.val + 32 ≤ b.val at hd + omega + refine H.not_mwr hS (fun t ht => ?_) hK + have hty := (H.thT t).2 ht + have hne : r ≠ t := by + rintro rfl + rw [hty] at hp + cases hp + have h := H.place.apart r t (.other 32) .tcb hp hty hne + change r.val + 32 ≤ t.val ∨ t.val + 4096 ≤ r.val at h + omega + +/-- The cancellation misses the TCBs' MCP, yield-target and affinity words. -/ +theorem suspendOffTcbNatA {s : S} {σ : State Unit} (H : SchedSRN st Z m κ s σ) + (hP : IPlacedA N Z.T E R Bf) {u K : U64} (hu : u ∈ Z.T) + (hKv : K.val = 3440 ∨ K.val = 3472 ∨ K.val = 3520 ∨ K.val = 3456) : + ¬ CancelReplyWA N Z.T Z.C E R Bf (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) (wa u K) := by + have PE := hP.e + have hua := H.core.tcbs u hu + have hv : (wa u K).val = u.val + K.val := Notif.tfield_val hua (by omega) + have hty := (H.thT u).2 hu + -- a word of a TCB of `T` at one of these offsets + have tcb_off : ∀ t ∈ Z.T, ∀ k : Nat, k < 4096 → (wa u K).val = t.val + k → k = K.val := by + intro t ht k hk e + rw [hv] at e + by_cases htu : t = u + · subst t; omega + · have h := H.place.apart t u .tcb .tcb ((H.thT t).2 ht) hty htu + change t.val + 4096 ≤ u.val ∨ u.val + 4096 ≤ t.val at h + omega + have hin : u.val ≤ (wa u K).val ∧ (wa u K).val < u.val + 4096 := by omega + rintro ((h | h | h) | ⟨q, hq, h⟩ | ⟨sc, hc, h⟩) + · rcases h with ⟨e, he, h⟩ | ⟨r, hr, h⟩ | ⟨t, ht, h | h⟩ | h + · have := PE.et e he u hu; omega + · have := PE.rt r hr u hu; omega + · obtain ⟨h1, h2⟩ := h + have hk := tcb_off t ht ((wa u K).val - t.val) (by unfold TWOffN at h2; omega) (by omega) + rw [hk] at h2 + unfold TWOffN at h2 + omega + · rcases h with h | h + · have := tcb_off t ht 3496 (by omega) h; omega + · have := tcb_off t ht 3504 (by omega) h; omega + · have := PE.bt _ h u hu; omega + · rcases h with ⟨n, hn, h1, h2⟩ | ⟨t, ht, h⟩ + · have hpn : Z.th n = some (.other 64) → False := fun hp => by + have hne : n ≠ u := by + rintro rfl + rw [hty] at hp + cases hp + have h := H.place.apart n u (.other 64) .tcb hp hty hne + change n.val + 64 ≤ u.val ∨ u.val + 4096 ≤ n.val at h + omega + have := hP.n.disj n hn u hu; omega + · have hta := PE.tcb t ht + have hval : (wa u K).val = t.val + 3328 ∨ (wa u K).val = t.val + 3336 ∨ (wa u K).val = t.val + 256 ∨ + (wa u K).val = t.val + 3496 ∨ (wa u K).val = t.val + 3504 ∨ (wa u K).val = t.val + 3376 := by + rcases h with h | h | h | h | h | h <;> + rw [h] <;> + (try simp only [stA_eq, Notif.tBlockA_eq, Notif.badgeRegA_eq, Notif.nextEp_eq, Notif.prevEp_eq]) <;> + rw [Notif.tfield_val hta] <;> simp + rcases hval with h | h | h | h | h | h <;> + · have := tcb_off t ht _ (by omega) h; omega + · have e : wa u K = L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION := h + have hK' : (wa u K).val < (L.ksb s).val ∨ (L.ksb s).val + ksSizeA ≤ (wa u K).val := by + have h := H.place.ks u _ hty (fun irq => by + have := H.place.irqK hty irq + cases this) + change u.val + 4096 ≤ (L.ksb s).val ∨ _ at h + omega + have hn := H.node_in H.sch.cpuCs (o := layout.node.SCHED_ACTION) (by simp [layout.node.SCHED_ACTION]) + rw [e] at hK' + omega + · have PEq := replyNextPlaceA (Z := Z) PE hq + have PEp := replyPrevPlaceA (Z := Z) PE hq + have := PE.rt q hq u hu + rcases h with h | h + · rw [h] at hin; omega + · rw [h] at hin; omega + · have hfoot : ScFootA Z.C m σ (wa u K) := by + obtain ⟨y, k, hk, hx⟩ := H.scr.sc sc hc + refine ⟨sc, hc, y, k, hk, ?_⟩ + have h32 := hx.hdr (K := 32#u64) (by simp) (by simp) + have ee : scReplyA sc = scWA sc 32#u64 := by unfold scReplyA scWA layout.sc.REPLY; rfl + rw [h, ee] + exact hx.in_obj h32.2 + have hd := H.scp.th_out hfoot hty + change (wa u K).val < u.val ∨ u.val + 4096 ≤ (wa u K).val at hd + omega + +theorem suspendOffTcbA {s : S} {σ : State Unit} (H : SchedSRN st Z m κ s σ) + (hP : IPlacedA N Z.T E R Bf) {u K : U64} (hu : u ∈ Z.T) + (hK : K = 3440#u64 ∨ K = 3472#u64 ∨ K = 3520#u64) : + ¬ CancelReplyWA N Z.T Z.C E R Bf (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) (wa u K) := + suspendOffTcbNatA H hP hu (by rcases hK with rfl | rfl | rfl <;> simp) + +/-- (item 9) The cancellation misses the TCBs' flags words. -/ +theorem suspendOffFlagsA {s : S} {σ : State Unit} (H : SchedSRN st Z m κ s σ) + (hP : IPlacedA N Z.T E R Bf) {u : U64} (hu : u ∈ Z.T) : + ¬ CancelReplyWA N Z.T Z.C E R Bf (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) (wa u 3456#u64) := + suspendOffTcbNatA (K := 3456#u64) H hP hu (Or.inr (Or.inr (Or.inr (by decide)))) + +/-- What the cancellation prefix of `suspend` establishes. -/ +def SuspendCancelPostA (st : machine.Store S) [L : LawfulStore st] (Z : XSetsA) (m : McState) + (κ : U64 → U64) (N E R : Set U64) (Bf : Set (U64 × Word)) (s : S) (σ : State Unit) (t : U64) (s' : S) : + Prop := + ∃ σ' stack', cancelIpc (Z := Unit) (specPtr t) σ = ({((), σ')}, False) ∧ + NtfnStateRel N Z.T (L.mem s') σ' ∧ IpcSchedRelA st Z m κ s' σ' ∧ EpStateRel Z.T E R Bf (L.mem s') σ' ∧ + ReplyStackRelA (L.mem s') R Z.C σ' (scNameA m) stack' ∧ + WritesIn st s s' (CancelReplyWA N Z.T Z.C E R Bf (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION)) ∧ + CancelKeepA σ σ' ∧ ∃ y, σ'.kheap (specPtr t) = some (.tcb y) ∧ IpcFreeA y.tcbState + +/-- **The cancellation in every thread state** (module docstring). -/ +theorem suspendCancelA (hH : SchedHypsA st N Z.T (IpcSchedRelA st Z m κ)) + (hSched : CancelScheduleA st N Z.T (IpcSchedRelA st Z m κ)) + (hOE : SchedOpsEA st Z.T E R Bf (IpcSchedRelA st Z m κ)) (hP : IPlacedA N Z.T E R Bf) + (P : IpcSchedPlacementA Z.th N E R Bf) (s : S) (a : A) (σ : State Unit) {stack : U64 → List U64} + (H : IpcSchedRelA st Z m κ s σ) (RN : NtfnStateRel N Z.T (L.mem s) σ) + (HE : EpStateRel Z.T E R Bf (L.mem s) σ) (HS : ReplyStackRelA (L.mem s) R Z.C σ (scNameA m) stack) + {t : U64} (ht : t ∈ Z.T) {x : Tcb} (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hReply : ∀ r', x.tcbState = .blockedOnReply r' → ∃ r ∈ R, specPtr r = r' ∧ + ∃ xr, σ.kheap r' = some (.reply xr) ∧ xr.replyTcb = some (specPtr t) ∧ ScReplyCovA Z.C m σ r') + (hE : ∀ e', epBlocked x.tcbState = some e' → L.mem s (tBlockA t) ∈ E ∧ + ∃ z, σ.kheap e' = some (.endpoint z) ∧ specPtr t ∈ z.epQueue) + (hN : ∀ n', x.tcbState = .blockedOnNotification n' → L.mem s (tBlockA t) ∈ N ∧ + ∃ z, σ.kheap n' = some (.notification z) ∧ specPtr t ∈ z.ntfnObj.ntfnQueue) + (hrep : RepOKA σ (specPtr t)) (hC0 : ∀ c ∈ Z.C, c ≠ 0#u64) : + ipc.cancel_ipc st ar s a t ⦃ r => r.2 = a ∧ SuspendCancelPostA st Z m κ N E R Bf s σ t r.1 ⦄ := by + have hAct : Aligned (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) := by + obtain ⟨Q, RR, H'⟩ := H + exact nodeAt_aligned (H'.core.nodes _ H'.sch.cpuCs) (by simp [layout.node.SCHED_ACTION]) + (by simp [layout.node.SCHED_ACTION]) + by_cases hr : ∃ r', x.tcbState = .blockedOnReply r' + · obtain ⟨r', hst⟩ := hr + obtain ⟨r, hrR, he, xr, hkr, hback, cov⟩ := hReply r' hst + subst he + refine WP.spec_mono (cancel_ipc_replyA hSched hOE hP P s a σ H RN HE HS ht hrR hk hst hkr hback cov + hC0 hAct) ?_ + rintro out ⟨h1, σ', stack', hc, hN', hS', hE', HS', -, hw, hk', y, hy, hin⟩ + exact ⟨h1, σ', stack', hc, hN', hS', hE', HS', hw, hk', y, hy, by rw [hin]; exact IpcFreeA.inactive⟩ + have hnr : ∀ r, x.tcbState ≠ .blockedOnReply r := fun r h => hr ⟨r, h⟩ + obtain ⟨σk, hk1, keep, skeep, y, hy, hfree⟩ := cancelIpc_keepA hk hnr + (fun e' hb => (hE e' hb).2) (fun n' h => (hN n' h).2) hrep + refine WP.spec_mono (WP.spec_and (cancel_ipc_rel hH hSched hOE hP s a σ RN HE H ht hk hnr hE hN hrep) + (cancel_ipc_footA hP s a σ RN HE ht hk hnr hE hN hAct)) ?_ + rintro out ⟨⟨h1, σ', hc, hN', hE', hS'⟩, hw⟩ + have hσ : σ' = σk := congrArg Prod.snd (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst (hc.symm.trans hk1))) + subst hσ + have hlink : ∀ d ∈ R, L.mem out.1 (rNextA d) = L.mem s (rNextA d) ∧ + L.mem out.1 (rPrevA d) = L.mem s (rPrevA d) := by + intro d hd + have off : ∀ b : U64, d.val + 8 ≤ b.val → b.val < d.val + 32 → + ¬ CancelWA N Z.T E R Bf (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) b := by + intro b h1 h2 h + have PE := hP.e + rcases h with h | h | h + · rcases h with ⟨e, he, h⟩ | ⟨r, hr, h⟩ | ⟨u, hu, h | h⟩ | h + · have := PE.er e he d hd; omega + · have hrr := PE.rep r hr + have hdd := PE.rep d hd + omega + · have := PE.rt d hd u hu + obtain ⟨g1, g2⟩ := h + unfold TWOffN at g2 + omega + · have := PE.rt d hd u hu + unfold TL at h + omega + · have := PE.br _ h d hd; omega + · refine replyWordOffNFootA hP hd (b := b) (by omega) h2 ?_ + rcases h with ⟨n, hn, g1, g2⟩ | ⟨u, hu, h⟩ + · exact Or.inl ⟨n, hn, g1, g2⟩ + rcases h with h | h | h | h | h | h + · exact Or.inr ⟨u, hu, Or.inl h⟩ + · exact Or.inr ⟨u, hu, Or.inr (Or.inl h)⟩ + · exact Or.inr ⟨u, hu, Or.inr (Or.inr (Or.inl h))⟩ + · exact Or.inr ⟨u, hu, Or.inr (Or.inr (Or.inr (Or.inr (Or.inl h))))⟩ + · exact Or.inr ⟨u, hu, Or.inr (Or.inr (Or.inr (Or.inr (Or.inr h))))⟩ + · exfalso + have hta := PE.tcb u hu + have hv : (wa u 3376#u64).val = u.val + 3376 := by rw [Notif.tfield_val hta] <;> simp + have := PE.rt d hd u hu + rw [h, hv] at h1 h2 + omega + · obtain ⟨Q, RR, H'⟩ := H + refine replyBlockOffMWrA H'.toSchedSRN P hd (b := b) (by omega) h2 ?_ + rw [h] + exact swAction H' + have n := replyNextPlaceA (Z := Z) hP.e hd + have p := replyPrevPlaceA (Z := Z) hP.e hd + exact ⟨hw.mem_eq n.1 (off _ (by omega) (by omega)), hw.mem_eq p.1 (off _ (by omega) (by omega))⟩ + have HS' : ReplyStackRelA (L.mem out.1) R Z.C σ' (scNameA m) stack := + HS.frame hlink (fun c _ y k hy => ⟨y, k, skeep.sc _ _ _ hy, rfl⟩) + (fun d _ y hy => skeep.reply _ _ hy) + exact ⟨h1, σ', stack, hc, hN', hS', hE', HS', hw.mono fun _ hb => Or.inl hb, keep, y, hy, hfree⟩ + +end +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/TcbWriteContA.lean b/proofs/Fl4ProofsA64/P3/TcbWriteContA.lean new file mode 100644 index 000000000..6dbfbe626 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbWriteContA.lean @@ -0,0 +1,52 @@ +/- +P3 (AArch64): the abstract WriteRegisters invocation from its phases. + +* **`tcbWriteRegisters_specOutcomeA`**: the abstract WriteRegisters invocation from its phases. + +The register write is the `asUser` equation `hWrite` (`tcbWriteRegisters_userA` produces it). +No named premise. +-/ +import Fl4ProofsA64.P3.TcbRegsContA +import Fl4ProofsA64.P3.TcbInvokeWriteSpecA +import Fl4ProofsA64.P3.DelSpecA +import Fl4ProofsA64.P3.TcbInvokeCopySpecA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4SpecA64 Sched + +noncomputable section + +section +open Fl4Spec +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + +/-- The abstract WriteRegisters invocation takes its restart and reschedule transitions after +the typed write and the `FaultIP -> NextIP` update. -/ +theorem tcbWriteRegisters_specOutcomeA {σ σIp σRestart σFinal : State Unit} {t : ObjRef} {x : Tcb} + {resume : Bool} {values : List Word} {guest : Bool} + (hk : σ.kheap t = some (.tcb x)) (hGuest : decide (x.tcbArch.tcbVcpu ≠ none) = guest) + (hWrite : asUser t (do + let _ ← Fl4Spec.NonDet.zipWithM (fun r v => setRegister r (fl4SanitiseRegister guest r v)) + (frameRegisters ++ gpRegisters) values + let pc ← getRestartPC + setNextPC pc) σ = ({((), σIp)}, False)) + (hRestart : ((), σRestart) ∈ ((Fl4Spec.NonDet.when resume (fl4TcbRestart (Z := Unit) t)) σIp).1) + (hReschedule : ((), σFinal) ∈ + ((Fl4Spec.NonDet.when (t = σ.curThread) (rescheduleRequired (Z := Unit))) σRestart).1) : + (.ok [], σFinal) ∈ ((fl4InvokeTcb (Z := Unit) (.writeRegisters t resume values ArchDefaultExtraRegisters)).run σ).1 := by + have hInfo : archGetSanitiseRegisterInfo (Z := Unit) t σ = ({(guest, σ)}, False) := by + rw [tcbCopyRegisters_guestA hk, hGuest] + unfold fl4InvokeTcb + apply mem_liftED + refine mem_bind_ndD (x := (σ.curThread, σ)) rfl ?_ + refine mem_bind_ndD (x := (guest, σ)) (by rw [hInfo]; rfl) ?_ + refine mem_bind_ndD (x := ((), σIp)) (by rw [hWrite]; rfl) ?_ + refine mem_bind_ndD (x := ((), σIp)) rfl ?_ + exact mem_bind_ndD hRestart (mem_bind_ndD hReschedule rfl) + +end +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/TcbWriteSchedA.lean b/proofs/Fl4ProofsA64/P3/TcbWriteSchedA.lean new file mode 100644 index 000000000..e2c859af0 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbWriteSchedA.lean @@ -0,0 +1,68 @@ +/- +P3 (AArch64): the WriteRegisters scheduler continuation over the merged relation. + +**`tcbWriteRegisters_contA`**: from the paired state after the register writes and the +`FaultIP -> NextIP` copy, the optional restart refines `fl4TcbRestart` and the current-destination +reschedule is skipped on both sides (the target is not the current thread). The two abstract +equations are the `hRestart` / `hReschedule` inputs of `tcbWriteRegisters_specOutcomeA`. + +[S]: `TcbRestartReadyA`, `hidle`. [P]: the platform premises of `restart_refinesA`. No [J]. +-/ +import Fl4ProofsA64.P3.TcbRegsContA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4SpecA64 Sched + +noncomputable section + +section +open Fl4Spec +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + +/-- **The WriteRegisters scheduler continuation** (module docstring). -/ +theorem tcbWriteRegisters_contA (hP : IPlacedA N Z.T E R Bf) (P : IpcSchedPlacementA Z.th N E R Bf) + (hc : m.curCore < fl4A64Platform.numNodes) + (hmb : ∀ a, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hmbF : ∀ a, Fl4MinBudgetAgreeA ar a) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, ar.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {sIp : S} {a : A} {σ σIp : State Unit} {stack : U64 → List U64} + (V : TcbIpcViewsA st Z m κ N E R Bf stack sIp σIp) (hC0 : ∀ c ∈ Z.C, c ≠ 0#u64) + {dest : U64} (ht : dest ∈ Z.T) (hOther : dest ≠ L.curW sIp) (resume : Bool) + (hidle : ∀ c < fl4A64Platform.numNodes, specPtr dest ≠ m.idleThreadOf c) + (ready : resume = true → ∃ x, TcbRestartReadyA st Z m κ N E R sIp σIp dest x) : + (do + let r ← if resume then sched.restart st ar sIp a dest else ok (sIp, a) + let t ← sched.cur_thread st r.1 + let b ← obj.Tcb.Insts.CoreCmpPartialEqTcb.eq dest t + if b then + let s' ← sched.reschedule_required st r.1 + ok (s', r.2) + else ok (r.1, r.2)) ⦃ r => r.2 = a ∧ ∃ σ1 stack', + (Fl4Spec.NonDet.when resume (fl4TcbRestart (Z := Unit) (specPtr dest))) σIp = ({((), σ1)}, False) ∧ + (Fl4Spec.NonDet.when (specPtr dest = σIp.curThread) (rescheduleRequired (Z := Unit))) σ1 = + ({((), σ1)}, False) ∧ + TcbIpcViewsA st Z m κ N E R Bf stack' r.1 σ1 ⦄ := by + refine WP.spec_bind (tcbRestartIf_contA (a := a) hP P hc hmb hmbF hmax htu V hC0 ht resume hidle ready) ?_ + rintro ⟨s1, a1⟩ ⟨ha, σ1, stk1, hR, V1⟩ + have hcur : L.curW s1 = L.curW sIp := + specPtr_inj (V1.rel.curThread_curW.symm.trans ((V.rel.curThread_eq V1.rel).trans V.rel.curThread_curW)) + have hne : dest ≠ L.curW s1 := by rw [hcur]; exact hOther + have hspecNe : specPtr dest ≠ σIp.curThread := by + rw [V.rel.curThread_curW] + exact fun e => hOther (specPtr_inj e) + have hskip : (Fl4Spec.NonDet.when (specPtr dest = σIp.curThread) (rescheduleRequired (Z := Unit))) σ1 = + ({((), σ1)}, False) := by + unfold Fl4Spec.NonDet.when + rw [if_neg hspecNe] + rfl + simp only [cur_thread_ok, bind_ok, obj.Tcb.Insts.CoreCmpPartialEqTcb.eq, decide_eq_true_eq, if_neg hne, + WP.spec_ok] + exact ⟨ha, σ1, stk1, hR, hskip, V1⟩ + +end +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/TcbWriteViewA.lean b/proofs/Fl4ProofsA64/P3/TcbWriteViewA.lean new file mode 100644 index 000000000..b4a13aca4 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/TcbWriteViewA.lean @@ -0,0 +1,146 @@ +/- +P3 (AArch64): the WriteRegisters loops over the merged views. + +* **`tcbWriteRegisters_viewsLoopA`**: both register loops keep every merged view, and the abstract + state is the exact register prefix of the destination. + +No named premise. +-/ +import Fl4ProofsA64.P3.TcbRegViewA +import Fl4ProofsA64.P3.TcbInvokeWritePrefixA +import Fl4ProofsA64.P3.TcbInvokeWriteSpecA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Endp +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P3.Notif Fl4SpecA64 Fl4Spec Sched + +noncomputable section + +section +attribute [local irreducible] tcbWritePrefixA +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {Z : XSetsA} {m : McState} {κ : U64 → U64} {N E R : Set U64} {Bf : Set (U64 × Word)} + +private def WriteViewInv (st : machine.Store S) [L : LawfulStore st] (Z : XSetsA) (m : McState) + (κ : U64 → U64) (N E R : Set U64) (Bf : Set (U64 × Word)) (stack : U64 → List U64) (σ0 : State Unit) + (dest caller p : U64) (src : Tcb) (y : Tcb) (s : S) : Prop := + TcbIpcViewsA st Z m κ N E R Bf stack s (Notif.updK σ0 (specPtr dest) (.tcb y)) ∧ + (Notif.updK σ0 (specPtr dest) (.tcb y)).kheap (specPtr caller) = some (.tcb src) ∧ + vcpu.bound st s dest = ok p + +private theorem tcbWriteViewStepA (hP : IPlacedA N Z.T E R Bf) (P : IpcSchedPlacementA Z.th N E R Bf) + {stack : U64 → List U64} {σ0 : State Unit} {dest caller buf p : U64} {sbo : Option ObjRef} + {src : Tcb} (ht : dest ∈ Z.T) (hc : caller ∈ Z.T) (hne : caller ≠ dest) + (hBuf : match sbo with | none => buf = 0#u64 | some sb => (buf, sb) ∈ Bf ∧ buf ≠ 0#u64) + (rs : List Register) (offset : Nat) (x : Tcb) (s : S) (j arg : U64) + (hIndex : j.val < rs.length) (hArg : arg.val = j.val + offset) (hSmall : arg.val < 127) + (hI : WriteViewInv st Z m κ N E R Bf stack σ0 dest caller p src + (tcbWritePrefixA (p != 0#u64) rs offset + (specArgA src.tcbArch.tcbContext.userRegs sbo σ0.machineState.underlyingMemory) x j.val) s) : + (do + let raw ← msg.syscall_arg st s caller buf arg + let reg := regIdxA rs[j.val] + let sanitised ← regs.sanitise_for_tcb st s dest reg raw + obj.Tcb.set_reg st dest s reg sanitised) ⦃ s' => + WriteViewInv st Z m κ N E R Bf stack σ0 dest caller p src + (tcbWritePrefixA (p != 0#u64) rs offset + (specArgA src.tcbArch.tcbContext.userRegs sbo σ0.machineState.underlyingMemory) x (j.val + 1)) s' ⦄ := by + obtain ⟨V, hSrc, hVcpu⟩ := hI + let args := specArgA src.tcbArch.tcbContext.userRegs sbo σ0.machineState.underlyingMemory + let old := tcbWritePrefixA (p != 0#u64) rs offset args x j.val + let σ := Notif.updK σ0 (specPtr dest) (.tcb old) + have hDest : σ.kheap (specPtr dest) = some (.tcb old) := by + show (Notif.updK σ0 (specPtr dest) (.tcb old)).kheap (specPtr dest) = _ + rw [Notif.updK_kheap, Function.update_self] + have hBufV : BufViewA (L.mem s) σ.machineState.underlyingMemory buf sbo := by + cases sbo with + | none => subst buf; exact bufViewA_none _ _ + | some sb => exact V.ep.bufView hBuf.1 hBuf.2 + have O := schedOpsEAOfPlacementA (st := st) (m := m) (κ := κ) P + have hStep := tcbWriteRegister_argumentA O hP.e V.ep V.rel ht hc hDest hSrc hBufV hSmall + (r := rs[j.val]) (guest := (p != 0#u64)) (fun _ => ⟨p, hVcpu, rfl⟩) + let v : U64 := ⟨fl4SanitiseRegister (p != 0#u64) rs[j.val] + (specArgA src.tcbArch.tcbContext.userRegs sbo σ.machineState.underlyingMemory arg.val)⟩ + have hRun := hStep.1 + rw [hRun, WP.spec_ok] + have V' := tcbRegWrite_viewsA hP P V ht hDest rs[j.val] v + have hNew : Notif.setReg old rs[j.val] v.bv = + tcbWritePrefixA (p != 0#u64) rs offset args x (j.val + 1) := by + show Notif.setReg old rs[j.val] (fl4SanitiseRegister (p != 0#u64) rs[j.val] + (args arg.val)) = _ + rw [hArg, tcbWritePrefixA] + simp only [List.getD_eq_getElem _ _ hIndex] + rfl + have hUpd : Notif.updK σ (specPtr dest) (.tcb (Notif.setReg old rs[j.val] v.bv)) = + Notif.updK σ0 (specPtr dest) (.tcb (tcbWritePrefixA (p != 0#u64) rs offset args x (j.val + 1))) := by + rw [hNew] + simp only [σ, Notif.updK, Function.update_idem] + rw [hUpd] at V' + have hne' : specPtr caller ≠ specPtr dest := fun e => hne (specPtr_inj e) + refine ⟨V', ?_, (tcbVcpuLinkRegFrameA s (hP.e.tcb dest ht) rs[j.val] v).trans hVcpu⟩ + rw [Notif.updK_kheap, Function.update_of_ne hne'] + rw [Notif.updK_kheap, Function.update_of_ne hne'] at hSrc + exact hSrc + +private def WriteViewPrefixInv (st : machine.Store S) [L : LawfulStore st] (Z : XSetsA) (m : McState) + (κ : U64 → U64) (N E R : Set U64) (Bf : Set (U64 × Word)) (stack : U64 → List U64) (σ0 : State Unit) + (dest caller p : U64) (src : Tcb) (args : Nat → Word) (rs : List Register) (offset : Nat) (x : Tcb) + (n : Nat) (s : S) : Prop := + WriteViewInv st Z m κ N E R Bf stack σ0 dest caller p src + (tcbWritePrefixA (p != 0#u64) rs offset args x n) s + +/-- **Both WriteRegisters loops keep the merged views.** -/ +theorem tcbWriteRegisters_viewsLoopA (hP : IPlacedA N Z.T E R Bf) (P : IpcSchedPlacementA Z.th N E R Bf) + {s : S} {σ : State Unit} {stack : U64 → List U64} + (V : TcbIpcViewsA st Z m κ N E R Bf stack s σ) {dest caller buf p count : U64} {sbo : Option ObjRef} + {src x : Tcb} (ht : dest ∈ Z.T) (hc : caller ∈ Z.T) (hne : caller ≠ dest) + (hDest : σ.kheap (specPtr dest) = some (.tcb x)) (hSrc : σ.kheap (specPtr caller) = some (.tcb src)) + (hBuf : match sbo with | none => buf = 0#u64 | some sb => (buf, sb) ∈ Bf ∧ buf ≠ 0#u64) + (hVcpu : vcpu.bound st s dest = ok p) : + let args := specArgA src.tcbArch.tcbContext.userRegs sbo σ.machineState.underlyingMemory + let frame := tcbWritePrefixA (p != 0#u64) frameRegisters 2 args x (min frameRegisters.length count.val) + let gp := tcbWritePrefixA (p != 0#u64) gpRegisters 19 args frame (min gpRegisters.length (count.val - 17)) + (do + let sFrame ← tcb.write_registers_loop0 st s dest buf caller count 0#u64 + tcb.write_registers_loop1 st sFrame dest buf caller count 0#u64) ⦃ sFinal => + TcbIpcViewsA st Z m κ N E R Bf stack sFinal (Notif.updK σ (specPtr dest) (.tcb gp)) ⦄ := by + dsimp only + have h0 : Notif.updK σ (specPtr dest) (.tcb x) = σ := updK_selfA hDest + let args := specArgA src.tcbArch.tcbContext.userRegs sbo σ.machineState.underlyingMemory + have hBase : WriteViewInv st Z m κ N E R Bf stack σ dest caller p src x s := by + refine ⟨?_, ?_, hVcpu⟩ + · rw [h0]; exact V + · rw [h0]; exact hSrc + have hStart : WriteViewPrefixInv st Z m κ N E R Bf stack σ dest caller p src args frameRegisters 2 x 0 s := by + unfold WriteViewPrefixInv + rw [tcbWritePrefixA] + exact hBase + have hFrame := tcbWriteRegisters_frameLoopA (count := count) (dest := dest) (buf := buf) + (caller := caller) + (I := WriteViewPrefixInv st Z m κ N E R Bf stack σ dest caller p src args frameRegisters 2 x) + hStart (fun s' j arg hj _ ha hI => + tcbWriteViewStepA hP P ht hc hne hBuf frameRegisters 2 x s' j arg hj ha (by + have hLen : frameRegisters.length = 17 := rfl + omega) hI) + refine WP.spec_bind hFrame ?_ + intro sFrame hI + let frame := tcbWritePrefixA (p != 0#u64) frameRegisters 2 args x (min frameRegisters.length count.val) + have hGpStart : WriteViewPrefixInv st Z m κ N E R Bf stack σ dest caller p src args gpRegisters 19 frame 0 + sFrame := by + unfold WriteViewPrefixInv + rw [tcbWritePrefixA] + exact hI + refine WP.spec_mono (tcbWriteRegisters_gpLoopA (count := count) (dest := dest) (buf := buf) + (caller := caller) + (I := WriteViewPrefixInv st Z m κ N E R Bf stack σ dest caller p src args gpRegisters 19 frame) + hGpStart (fun s' j arg hj _ ha hJ => + tcbWriteViewStepA hP P ht hc hne hBuf gpRegisters 19 frame s' j arg hj ha (by + have hLen : gpRegisters.length = 19 := rfl + omega) hJ)) ?_ + intro sGp h + exact h.1 + +end +end +end Fl4ProofsA64.P3.Endp diff --git a/proofs/Fl4ProofsA64/P3/UnmapDecJA.lean b/proofs/Fl4ProofsA64/P3/UnmapDecJA.lean new file mode 100644 index 000000000..bef698365 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UnmapDecJA.lean @@ -0,0 +1,543 @@ +/- +P3 (aarch64, A64-P3 S3 gen 26, ruling 22:56Z items 1–3): **the `ARMPageUnmap` / `ARMPageTableUnmap` tops at the +decoder** — `P3/UnmapTopJA`'s `page_unmap_jA` / `pt_unmap_jA` with their cap facts discharged from the entry relation, +the spec's decoder (`Ext/Invocation`'s `fl4ArchDecodeInvocation`) matched, and the decode-site facts at `w0` / `w1`. +The x86 counterparts are `EptCore.frameUnmapF` / `P3/EptTableUnmapDec` (`VmEptState.tableUnmapDec`, +`zv_eD_unmapF`). + +* **Item 1 (cap facts).** The unmapped caps FL4 writes (`Frame fp sz rt dev false false 0 0`, `PageTable ptr false 0 + 0`) get `EncWf` from the invoked cap's (`CapRt.decodeF_encWf`), `CapRelA` from its `CapWfA` (`StateRelA.getAbsA`; + `extUnmappedFrame` / `updateMapData … none` of the invoked cap's abstract cap is `absCapA` of the unmapped one), + `UA` from the shape (`UA.of_shapeA`), `CapObjOkA` by construction; `asid < 2^16` from `CapWfA`, the plain frame's + `VaOkE` from `JAtZA.vaOkE`. **`VsPgOffOSA.of_apA`**: `VsPgOffOSA` (gen 25's lemma hypothesis of the tops) follows + from `JEntA.ap` (`SlotApA.vs` / `.ext`: the Stage-1 tables' and windows' level-1 words are `VsFootA`, the Stage-2 + roots' and tables' are `ExtFootE`), so it is no longer a hypothesis of the decoder-level tops. + **Not discharged: `hp`** (the table's ghost membership, `Z.S1` at depth ≥ 1 or `Z.T2`): no field of `JAtZAX` / + `CapValidA` / `CapObjOkA` ties a mapped `Cap::PageTable` to the relation's tables (x86 carries it in + `EptCore.capTbl`), so it stays a hypothesis (open question for the Architect). +* **Item 2 (the decoder).** **`dec_pgUnmapA`**: l4v+FL4's `fl4ArchDecodeInvocation` at `ARMPageUnmap` of a mapped + frame cap (`absFrameE`: `frameCap` whose ASID names an FL4 root of kind `k`, `extKindOfAsid`; or `stage2FrameCap`, + `k = stage2`) has the single outcome `.extVSpace (.pageUnmap k cap cte)`; **`dec_ptUnmapA`**: at + `ARMPageTableUnmap` of a mapped page-table cap whose ASID names an FL4 root of kind `k`, final (`isFinalCap'`), the + single outcome `.extVSpace (.tableUnmap k cap cte)`. **`asidRootKind_stUpdA`**: the restart's TCB update keeps + `asidRootKind`, so the decoder's `k` (at the decode state) is the tops' (at the restart state). + **`page_unmap_decJA`** / **`pt_unmap_decJA`**: the decoder's outcome and, for that `k`, FL4's `decode_frame` / + `decode_page_table` against the restart and `performExtVSpace`, with `JAtZAX` at FL4's returned arch state (ruling + Q4) and the tree. Finality: FL4's `is_final_capability = ok true` and the spec's `isFinalCap'` are both hypotheses + of `pt_unmap_decJA` (the A64 twin of x86's `fin_iff_of` — `FinInv`, `finF_iff`, `final_iff` — is not ported yet). +* **Item 3 (`w0` / `w1`).** `ExtTabOffA` is vacuous at both boot witnesses (no Stage-2 root, window or Stage-2 table: + `ExtTabOffA.of_emptyA`, **`extTabOff_w0A`**, **`extTabOff_w1A`**); `VsPgOffOSA` at `w0` is vacuous + (**`vsPgOffOS_w0A`**) and at `w1` follows from `w1_jEntA_ex`'s `SlotApA` (**`vsPgOffOS_w1A`**). + +**S3 gen 27 (ruling 23:23Z items 1–3).** `hp` stays a hypothesis of `pt_unmap_decJA` (no `CapObjOkA` change). +**Changed statement**: `pt_unmap_decJA`'s two finality hypotheses (FL4's `hf` and the spec's `hfS`) are joined into +the carried invariant `hFI : FinInvA Z.th (L.mem s) σ Z.G` (`P3/FinDefsA`) and FL4's `hf`; the spec's finality is +derived (`fin_iff_ofA`). Item 3, a **non-final** page-table cap: **`dec_ptUnmap_nfA`** (the spec's decoder is +`RevokeFirst` at the unchanged state, any mapping, any root kind; `dec_ptUnmapSpec_nfA` for l4v's arm), +**`decode_pt_nfA`** (FL4's `decode_page_table` returns `RevokeFirst`, nothing written) and **`pt_unmap_nfJA`** (both +with `FinInvA`, the error relation and `JAtZAX` as at the entry). + +**S3 gen 29 (ruling 00:45Z items 3–4). Changed statements**: `pt_unmap_decJA` takes `hPC : PtCapInA Z (L.mem s)` +(`P3/FinKeepA`) in place of `hp` (derived, `PtCapInA.hpA`; `hp` retired); `page_unmap_decJA` / `pt_unmap_decJA` (as +`P3/UnmapTopJA`'s tops) also conclude `CarryA Z (L.mem s) σ → CarryA Z (L.mem s3) σ3` (`P3/CarryA`). + +Named premises: `ArchKeepsA` [P] and `S2IpaE` [P] (as `P3/UnmapTopJA`). Decode-site hypotheses `TabOffA`, +`ExtTabOffA`, `RsSiteA` (standing). No new named premise. New module; no existing statement changed. +-/ +import Fl4ProofsA64.P3.UnmapTopJA +import Fl4ProofsA64.P3.CnodeFrontA +import Fl4ProofsA64.P3.JEntW1A +import Fl4ProofsA64.P3.FinDefsA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## The restart keeps the ASID's root kind -/ + +section kind +open Fl4SpecA64 + +theorem vspaceForAsid_congrA {σ σ' : State Unit} (ha : σ'.archState = σ.archState) + (hp : asidPoolsOf σ' = asidPoolsOf σ) (a : Asid) : vspaceForAsid a σ' = vspaceForAsid a σ := by + by_cases h : 0 < a + · rw [vspaceForAsid_posA h, vspaceForAsid_posA h] + unfold specVsA + rw [ha, hp] + · unfold vspaceForAsid Fl4Spec.Lookup.oassert + simp only [if_neg h] + rfl + +variable {σ : State Unit} {t : ObjRef} {x : Tcb} {ts : ThreadState} + +theorem aobjsOf_stUpdA (hk : σ.kheap t = some (.tcb x)) : aobjsOf (stUpdA σ t x ts) = aobjsOf σ := by + funext p + by_cases e : p = t + · subst e + simp only [aobjsOf, stUpdA, setObjA, Fl4Spec.optMap, Function.update_self, hk, aobjOf] + · simp only [aobjsOf, stUpdA, setObjA, Fl4Spec.optMap, Function.update_of_ne e] + +theorem extObjAt_stUpdA (hk : σ.kheap t = some (.tcb x)) (r : ObjRef) : + extObjAt (stUpdA σ t x ts) r = extObjAt σ r := by + by_cases e : r = t + · subst e + simp only [extObjAt, stUpdA, setObjA, Function.update_self, hk] + · simp only [extObjAt, stUpdA, setObjA, Function.update_of_ne e] + +/-- **The restart keeps the ASID's root kind** (module docstring, item 2). -/ +theorem asidRootKind_stUpdA (hk : σ.kheap t = some (.tcb x)) (a : Asid) : + asidRootKind (stUpdA σ t x ts) a = asidRootKind σ a := by + have hv : vspaceForAsid a (stUpdA σ t x ts) = vspaceForAsid a σ := + vspaceForAsid_congrA (σ := σ) (σ' := stUpdA σ t x ts) rfl + (by show Fl4Spec.optMap (aobjsOf (stUpdA σ t x ts)) asidPoolOf = Fl4Spec.optMap (aobjsOf σ) asidPoolOf + rw [aobjsOf_stUpdA hk]) a + have hr : rootKindAt (stUpdA σ t x ts) = rootKindAt σ := by + funext r; unfold rootKindAt; rw [extObjAt_stUpdA hk] + unfold asidRootKind + rw [hv, hr] + +end kind + +/-! ## The spec's decoder (item 2) -/ + +section dec +open Fl4SpecA64 + +theorem flushLbl_unmapA : + isPageFlushLabel (invocationType (specLabelWord (archL .armPageUnmap))) = false := by + show isPageFlushLabel (invocationType (BitVec.ofNat 64 + (InvocationLabel.archInvocationLabel .armPageUnmap).fromEnum)) = false + rw [invocationType_fromEnumA]; rfl + +variable (calling : Bool) (args : List Data) (xSlot : CapRef) (cte : CslotPtr) (exs : List (Cap × CslotPtr)) + {σ : State Unit} + +/-- The spec's `ARMPageUnmap` of a mapped plain frame cap whose ASID names an FL4 root of kind `k`. -/ +theorem dec_pgUnmapFrA (p : ObjRef) (R : CapRights) (sz : VMPageSize) (dev : Bool) (asid : Asid) (va : VSpaceRef) + {k : ExtRootKind} (hk : asidRootKind σ asid = some k) (hk1 : k ≠ .stage1) : + (fl4ArchDecodeInvocation (Z := Unit) calling (archL .armPageUnmap) args xSlot cte + (.frameCap p R sz dev (some (asid, va))) exs).run σ = + ({(.ok (.extVSpace (.pageUnmap k (.frameCap p R sz dev (some (asid, va))) cte)), σ)}, False) := by + have h1 : ¬ (archL .armPageUnmap = Fl4Label.ext .armPageDeriveSubframe) := fun h => by cases h + have h2 : ¬ (archL .armPageUnmap = Fl4Label.ext .armPageTestAndClearAccessed) := fun h => by cases h + have h4 : ¬ (archL .armPageUnmap = archL .armPageMap ∧ args.length > 2 ∧ exs.length > 0) := + fun h => by cases h.1 + unfold fl4ArchDecodeInvocation + dsimp only + rw [if_neg h1, if_neg h2, flushLbl_unmapA, if_neg Bool.false_ne_true, if_neg h4, if_pos rfl] + refine (run_bind_okD (run_liftE_stD (extKindOfAsid_someA hk hk1)) _).trans ?_ + rfl + +/-- The spec's `ARMPageUnmap` of a Stage-2 frame cap: always `.pageUnmap .stage2`. -/ +theorem dec_pgUnmapS2A (p : ObjRef) (R : CapRights) (sz : VMPageSize) (dev : Bool) + (mp : Option (Asid × VSpaceRef)) : + (fl4ArchDecodeInvocation (Z := Unit) calling (archL .armPageUnmap) args xSlot cte + (.ext (.stage2FrameCap p R sz dev mp)) exs).run σ = + ({(.ok (.extVSpace (.pageUnmap .stage2 (.ext (.stage2FrameCap p R sz dev mp)) cte)), σ)}, False) := by + have h1 : ¬ (archL .armPageUnmap = Fl4Label.ext .armPageDeriveSubframe) := fun h => by cases h + have h2 : ¬ (archL .armPageUnmap = Fl4Label.ext .armPageTestAndClearAccessed) := fun h => by cases h + have h4 : ¬ (archL .armPageUnmap = archL .armPageMap ∧ args.length > 2 ∧ exs.length > 0) := + fun h => by cases h.1 + unfold fl4ArchDecodeInvocation + dsimp only + rw [if_neg h1, if_neg h2, flushLbl_unmapA, if_neg Bool.false_ne_true, if_neg h4, if_pos rfl] + rfl + +/-- **The spec's `ARMPageUnmap` of a mapped FL4-root frame cap** (module docstring, item 2). -/ +theorem dec_pgUnmapA (isEpt : Bool) (p : ObjRef) (R : CapRights) (sz : VMPageSize) (dev : Bool) (asid : Asid) + (va : VSpaceRef) {k : ExtRootKind} (hk : isEpt = false → asidRootKind σ asid = some k ∧ k ≠ .stage1) + (he : isEpt = true → k = .stage2) : + (fl4ArchDecodeInvocation (Z := Unit) calling (archL .armPageUnmap) args xSlot cte + (absFrameE isEpt p R sz dev (some (asid, va))) exs).run σ = + ({(.ok (.extVSpace (.pageUnmap k (absFrameE isEpt p R sz dev (some (asid, va))) cte)), σ)}, False) := by + cases isEpt with + | true => + obtain rfl := he rfl + exact dec_pgUnmapS2A calling args xSlot cte exs p R sz dev _ + | false => + obtain ⟨h1, h2⟩ := hk rfl + exact dec_pgUnmapFrA calling args xSlot cte exs p R sz dev asid va h1 h2 + +/-- **The spec's `ARMPageTableUnmap` of a final mapped page-table cap on an FL4 root** (module docstring, item 2; +x86 `zv_eD_unmapF`). -/ +theorem dec_ptUnmapA (p : ObjRef) (asid : Asid) (va : VSpaceRef) {k : ExtRootKind} + (hk : asidRootKind σ asid = some k) (hk1 : k ≠ .stage1) + (hf : isFinalCap' (Z := Unit) (.archObjectCap (.pageTableCap p .normalPtT (some (asid, va)))) σ) : + (fl4ArchDecodeInvocation (Z := Unit) calling (archL .armPageTableUnmap) args xSlot cte + (.pageTableCap p .normalPtT (some (asid, va))) exs).run σ = + ({(.ok (.extVSpace (.tableUnmap k (.pageTableCap p .normalPtT (some (asid, va))) cte)), σ)}, False) := by + have hF : isFinalCap (Z := Unit) (.archObjectCap (.pageTableCap p .normalPtT (some (asid, va)))) σ = + ({(true, σ)}, False) := by + show ({(decide (isFinalCap' (Z := Unit) (.archObjectCap (.pageTableCap p .normalPtT (some (asid, va)))) σ), σ)}, + False) = _ + rw [decide_eq_true hf] + have h4 : ¬ (archL .armPageTableUnmap = archL .armPageTableMap ∧ args.length > 1 ∧ exs.length > 0) := + fun h => by cases h.1 + unfold fl4ArchDecodeInvocation + dsimp only + rw [if_neg h4, if_pos rfl] + refine (run_bind_okD (run_liftE_stD (extKindOfAsid_someA hk hk1)) _).trans ?_ + dsimp only + refine (run_bind_okD (run_liftE_stD hF) _).trans ?_ + simp only [Fl4Spec.NonDetE.unlessE, eq_self_iff_true, if_true, ndE_pure_bindD] + rfl + +/-! ### Item 3 (gen 27): a non-final page-table cap, on any root and any mapping -/ + +theorem invType_ptUnmapA : + invocationType (specLabelWord (archL .armPageTableUnmap)) = .archInvocationLabel .armPageTableUnmap := by + show invocationType (BitVec.ofNat 64 (InvocationLabel.archInvocationLabel .armPageTableUnmap).fromEnum) = _ + exact invocationType_fromEnumA _ + +theorem isFinalCap_falseA {c : Cap} (h : ¬ isFinalCap' (Z := Unit) c σ) : + isFinalCap (Z := Unit) c σ = ({(false, σ)}, False) := by + show ({(decide (isFinalCap' (Z := Unit) c σ), σ)}, False) = _ + rw [decide_eq_false h] + +/-- l4v's `decode_page_table_invocation` at `ARMPageTableUnmap` of a non-final cap: `RevokeFirst`. -/ +theorem dec_ptUnmapSpec_nfA (p : ObjRef) (mp : Option (Asid × VSpaceRef)) + (hnf : ¬ isFinalCap' (Z := Unit) (.archObjectCap (.pageTableCap p .normalPtT mp)) σ) : + (Fl4Spec.NonDetE.liftME Fl4ArchInvocation.spec (archDecodeInvocation (Z := Unit) (specLabelWord (archL .armPageTableUnmap)) + args xSlot cte (.pageTableCap p .normalPtT mp) exs)).run σ = ({(.error .revokeFirst, σ)}, False) := by + unfold Fl4Spec.NonDetE.liftME + refine run_bind_errorD ?_ _ + unfold archDecodeInvocation + dsimp only + unfold decodePageTableInvocation + try dsimp only + rw [invType_ptUnmapA, if_neg (fun h => by cases h), if_pos rfl] + refine (run_bind_okD (run_liftE_stD (isFinalCap_falseA hnf)) _).trans ?_ + exact unlessE_neg_runX (by simp) _ _ _ + +/-- **The spec's `ARMPageTableUnmap` of a non-final page-table cap** (item 3): `RevokeFirst`, the state unchanged, +whatever the cap's mapping and root kind. -/ +theorem dec_ptUnmap_nfA (p : ObjRef) (mp : Option (Asid × VSpaceRef)) + (hnf : ¬ isFinalCap' (Z := Unit) (.archObjectCap (.pageTableCap p .normalPtT mp)) σ) : + (fl4ArchDecodeInvocation (Z := Unit) calling (archL .armPageTableUnmap) args xSlot cte + (.pageTableCap p .normalPtT mp) exs).run σ = ({(.error .revokeFirst, σ)}, False) := by + have hsp := dec_ptUnmapSpec_nfA args xSlot cte exs p mp hnf + have h4 : ¬ (archL .armPageTableUnmap = archL .armPageTableMap ∧ args.length > 1 ∧ exs.length > 0) := + fun h => by cases h.1 + unfold fl4ArchDecodeInvocation + dsimp only + rw [if_neg h4, if_pos rfl] + rcases mp with _ | ⟨asid, va⟩ + · exact hsp + dsimp only + rcases hk : asidRootKind σ asid with _ | k + · exact (run_bind_okD (run_liftE_stD (extKindOfAsid_noneA (Or.inl hk))) _).trans hsp + by_cases hk1 : k = .stage1 + · subst hk1 + exact (run_bind_okD (run_liftE_stD (extKindOfAsid_noneA (Or.inr hk))) _).trans hsp + refine (run_bind_okD (run_liftE_stD (extKindOfAsid_someA hk hk1)) _).trans ?_ + dsimp only + refine (run_bind_okD (run_liftE_stD (isFinalCap_falseA hnf)) _).trans ?_ + exact unlessE_neg_runX (by simp) _ _ _ + +/-! ### Item 3 (gen 27): the l4v arms (unmapped caps, caps on a Stage-1 root or on no root) -/ + +/-- The cap's mapping names no FL4 root: unmapped, or mapped on a Stage-1 root or on none. -/ +def NoExtMapA (σ : State Unit) : Option (Asid × VSpaceRef) → Prop + | none => True + | some (asid, _) => asidRootKind σ asid = none ∨ asidRootKind σ asid = some .stage1 + +theorem invType_pgUnmapA : + invocationType (specLabelWord (archL .armPageUnmap)) = .archInvocationLabel .armPageUnmap := by + show invocationType (BitVec.ofNat 64 (InvocationLabel.archInvocationLabel .armPageUnmap).fromEnum) = _ + exact invocationType_fromEnumA _ + +/-- **The spec's `ARMPageUnmap` of a plain frame cap on no FL4 root** (item 3): l4v's `PageUnmap` invocation. -/ +theorem dec_pgUnmap_specA (p : ObjRef) (R : CapRights) (sz : VMPageSize) (dev : Bool) + (mp : Option (Asid × VSpaceRef)) (hk : NoExtMapA σ mp) : + (fl4ArchDecodeInvocation (Z := Unit) calling (archL .armPageUnmap) args xSlot cte + (.frameCap p R sz dev mp) exs).run σ = + ({(.ok (.spec (.invokePage (.pageUnmap (.frameCap p R sz dev mp) cte))), σ)}, False) := by + have hsp : (Fl4Spec.NonDetE.liftME Fl4ArchInvocation.spec (archDecodeInvocation (Z := Unit) + (specLabelWord (archL .armPageUnmap)) args xSlot cte (.frameCap p R sz dev mp) exs)).run σ = + ({(.ok (.spec (.invokePage (.pageUnmap (.frameCap p R sz dev mp) cte))), σ)}, False) := by + unfold Fl4Spec.NonDetE.liftME + refine (run_bind_okD ?_ _).trans rfl + unfold archDecodeInvocation + dsimp only + unfold decodeFrameInvocation + try dsimp only + rw [invType_pgUnmapA, if_neg (fun h => by cases h), if_pos rfl] + rfl + have h1 : ¬ (archL .armPageUnmap = Fl4Label.ext .armPageDeriveSubframe) := fun h => by cases h + have h2 : ¬ (archL .armPageUnmap = Fl4Label.ext .armPageTestAndClearAccessed) := fun h => by cases h + have h4 : ¬ (archL .armPageUnmap = archL .armPageMap ∧ args.length > 2 ∧ exs.length > 0) := + fun h => by cases h.1 + unfold fl4ArchDecodeInvocation + dsimp only + rw [if_neg h1, if_neg h2, flushLbl_unmapA, if_neg Bool.false_ne_true, if_neg h4, if_pos rfl] + rcases mp with _ | ⟨asid, va⟩ + · exact hsp + · exact (run_bind_okD (run_liftE_stD (extKindOfAsid_noneA hk)) _).trans hsp + +theorem isFinalCap_trueA {c : Cap} (h : isFinalCap' (Z := Unit) c σ) : + isFinalCap (Z := Unit) c σ = ({(true, σ)}, False) := by + show ({(decide (isFinalCap' (Z := Unit) c σ), σ)}, False) = _ + rw [decide_eq_true h] + +/-- **The spec's `ARMPageTableUnmap` of a final page-table cap on no FL4 root** (item 3): l4v's +`PageTableUnmap` invocation. -/ +theorem dec_ptUnmap_specA (p : ObjRef) (mp : Option (Asid × VSpaceRef)) (hk : NoExtMapA σ mp) + (hf : isFinalCap' (Z := Unit) (.archObjectCap (.pageTableCap p .normalPtT mp)) σ) : + (fl4ArchDecodeInvocation (Z := Unit) calling (archL .armPageTableUnmap) args xSlot cte + (.pageTableCap p .normalPtT mp) exs).run σ = + ({(.ok (.spec (.invokePageTable (.pageTableUnmap (.pageTableCap p .normalPtT mp) cte))), σ)}, False) := by + have hsp : (Fl4Spec.NonDetE.liftME Fl4ArchInvocation.spec (archDecodeInvocation (Z := Unit) + (specLabelWord (archL .armPageTableUnmap)) args xSlot cte (.pageTableCap p .normalPtT mp) exs)).run σ = + ({(.ok (.spec (.invokePageTable (.pageTableUnmap (.pageTableCap p .normalPtT mp) cte))), σ)}, False) := by + unfold Fl4Spec.NonDetE.liftME + refine (run_bind_okD ?_ _).trans rfl + unfold archDecodeInvocation + dsimp only + unfold decodePageTableInvocation + try dsimp only + rw [invType_ptUnmapA, if_neg (fun h => by cases h), if_pos rfl] + refine (run_bind_okD (run_liftE_stD (isFinalCap_trueA hf)) _).trans ?_ + simp only [Fl4Spec.NonDetE.unlessE, eq_self_iff_true, if_true, ndE_pure_bindD] + rfl + have h4 : ¬ (archL .armPageTableUnmap = archL .armPageTableMap ∧ args.length > 1 ∧ exs.length > 0) := + fun h => by cases h.1 + unfold fl4ArchDecodeInvocation + dsimp only + rw [if_neg h4, if_pos rfl] + rcases mp with _ | ⟨asid, va⟩ + · exact hsp + · exact (run_bind_okD (run_liftE_stD (extKindOfAsid_noneA hk)) _).trans hsp + +end dec + +/-! ## The decode-site facts (items 1 and 3) -/ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-- **`VsPgOffOSA` from the entry's slot apartness** (module docstring, item 1). -/ +theorem VsPgOffOSA.of_apA {Z : XSetsA} {H : Heap} {OS : Set U64} {kb : U64} (h : SlotApA Z H OS kb) : + VsPgOffOSA Z OS := by + intro q j hj hq + rcases hq with (hT | ⟨r, hr, rfl | rfl⟩) | ⟨y, hy, rfl⟩ | hS + · exact (h.ext _ (.s2pt q j hT hj)).1 + · exact (h.ext _ (.s2lo q j hr hj)).1 + · exact (h.ext _ (.s2hi r j hr hj)).1 + · exact (h.vs _ (.high y j hy hj)).1 + · exact (h.vs _ (.pt q j hS hj)).1 + +/-- No Stage-2 root, window or Stage-2 table: `ExtTabOffA` is vacuous (module docstring, item 3). -/ +theorem ExtTabOffA.of_emptyA {Z : XSetsA} (hX : Z.X = ∅) (hY : Z.Y = ∅) (hT : Z.T2 = ∅) + (m : Fl4SpecA64.McState) (s : S) : ExtTabOffA (L := L) Z m s := by + refine ⟨fun q hq => ?_, fun p hp => ?_⟩ + · exfalso + rcases hq with (h | ⟨r, hr, -⟩) | ⟨y, hy, -⟩ + · rw [hT] at h; exact Set.notMem_empty _ h + · rw [hX] at hr; exact Set.notMem_empty _ hr + · rw [hY] at hy; exact Set.notMem_empty _ hy + · exfalso + rcases hp with h | h | h + · rw [hX] at h; exact Set.notMem_empty _ h + · rw [hY] at h; exact Set.notMem_empty _ h + · rw [hT] at h; exact Set.notMem_empty _ h + +/-- No VSpace page at all: `VsPgOffOSA` is vacuous. -/ +theorem VsPgOffOSA.of_emptyA {Z : XSetsA} (hX : Z.X = ∅) (hY : Z.Y = ∅) (hT : Z.T2 = ∅) (hS : Z.S1 = ∅) + (OS : Set U64) : VsPgOffOSA Z OS := by + intro q j _ hq + exfalso + rcases hq with (h | ⟨r, hr, -⟩) | ⟨y, hy, -⟩ | h + · rw [hT] at h; exact Set.notMem_empty _ h + · rw [hX] at hr; exact Set.notMem_empty _ hr + · rw [hY] at hy; exact Set.notMem_empty _ hy + · rw [hS] at h; exact Set.notMem_empty _ h + +theorem extTabOff_w0A (kb ib : U64) (nc : Nat) (m : Fl4SpecA64.McState) (s : S) : + ExtTabOffA (L := L) (bootZA kb ib nc) m s := ExtTabOffA.of_emptyA rfl rfl rfl m s + +theorem vsPgOffOS_w0A (kb ib : U64) (nc : Nat) (OS : Set U64) : VsPgOffOSA (bootZA kb ib nc) OS := + VsPgOffOSA.of_emptyA rfl rfl rfl rfl OS + +theorem extTabOff_w1A (kb ib : U64) (nc : Nat) (m : Fl4SpecA64.McState) (s : S) : + ExtTabOffA (L := L) (w1ZA kb ib nc) m s := ExtTabOffA.of_emptyA rfl rfl rfl m s + +end + +open Fl4ProofsA64.P1.W7.R.ST.BootWit in +/-- `VsPgOffOSA` at A64-Boot's concrete `w1` (its `SlotApA`, `w1_jEntA_ex`). -/ +theorem vsPgOffOS_w1A : + VsPgOffOSA (w1ZA 18446603337294938112#u64 18446603338368679936#u64 128) (w1OS 18446603337294938112#u64 18446603338368679936#u64 128) := + VsPgOffOSA.of_apA (w1_jEntA_ex (A := Unit) (a := ()) (fun _ _ => True) 0#u64 0#u64 0#u64 0#u64 (fun _ => 0#u64) + none trivial).1.ap + +/-! ## The tops at the decoder (items 1 and 2) -/ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} + +set_option maxHeartbeats 2000000 in +/-- **`ARMPageUnmap` on an FL4 root, from the decoder** (module docstring): the spec decoder's single outcome +`.pageUnmap k`, and for it the restart, FL4's `decode_frame` against `performExtVSpace` with `JAtZAX` at FL4's returned +arch state and the tree — `page_unmap_jA` with its cap facts, `h16`, `hVa` and `VsPgOffOSA` discharged. -/ +theorem page_unmap_decJA {ar : machine.Arch A} (K : ArchKeepsA ar AR) {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} + (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) + (hTO : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s q) (hEO : ExtTabOffA (L := L) Z m s) + (hK : Aligned (L.ksb s)) (hT : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s)) (hIpa : S2IpaE ar a) + (length buf : U64) (call calling : Bool) (ex : Aeneas.Std.Array obj.Slot 3#usize) (args : List Fl4SpecA64.Data) + (xSlot : Fl4SpecA64.CapRef) (exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) + (cte : Fl4SpecA64.CslotPtr) (hab : AbsSlotA Z.th cte) + (fp rt asid va : U64) (sz : capability.FrameSize) (dev isEpt : Bool) (h0 : asid ≠ 0#u64) + (hcap : capA st s (cslotAddrA cte) = .Frame fp sz rt dev true isEpt asid va) (k : Fl4SpecA64.ExtRootKind) + (hk : isEpt = false → Fl4SpecA64.asidRootKind σ (BitVec.ofNat 16 asid.val) = some k ∧ k ≠ .stage1) + (he : isEpt = true → k = .stage2) : + (Fl4SpecA64.fl4ArchDecodeInvocation (Z := Unit) calling (Fl4SpecA64.archL .armPageUnmap) args xSlot cte + (absFrameE isEpt (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev (some (BitVec.ofNat 16 asid.val, va.bv))) + exs).run σ = + ({(.ok (.extVSpace (.pageUnmap k (absFrameE isEpt (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev + (some (BitVec.ofNat 16 asid.val, va.bv))) cte)), σ)}, False) ∧ + ∃ x, σ.kheap σ.curThread = some (.tcb x) ∧ + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + (TreeA st OS Z.G.chains Z.G.par s → (∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) → + ∃ (s3 : S) (σ3 : Fl4SpecA64.State Unit) (fa : Result A), + vspace.decode_frame st ar s a fl4_abi.label.ARMPageUnmap length (cslotAddrA cte) + (.Frame fp sz rt dev true isEpt asid va) call buf ex = + (do let a1 ← fa; ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s3, a1)) ∧ + (([] : List Fl4SpecA64.Data), σ3) ∈ (Fl4SpecA64.performExtVSpace (Z := Unit) (.pageUnmap k + (absFrameE isEpt (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev + (some (BitVec.ofNat 16 asid.val, va.bv))) cte) (stUpdA σ σ.curThread x .restart)).1 ∧ + fa ⦃ a' => JAtZAX (L := L) AR Z s3 a' σ3 m ⦄ ∧ TreeA st OS Z.G.chains Z.G.par s3 ∧ + (CarryA Z (L.mem s) σ → CarryA Z (L.mem s3) σ3)) := by + have hsl : IsSlotA Z.th (cslotAddrA cte) := hab.isSlot E.j.1.place + have hcap' : capAtA (L.mem s) (cslotAddrA cte) = .Frame fp sz rt dev true isEpt asid va := hcap + obtain ⟨c0, -, hrel0⟩ := StateRelA.getAbsA E.j.1.st hab + rw [hcap'] at hrel0 + obtain ⟨⟨hr1, hr3, ⟨h16a, -⟩, hov⟩, -⟩ := hrel0 + have hEnc0 : CapRt.EncWf (capAtA (L.mem s) (cslotAddrA cte)) := CapRt.decodeF_encWf _ _ + rw [hcap'] at hEnc0 + have hcan : CapRt.Canonical fp ∧ rt.val < 4 := by + cases isEpt <;> (simp only [CapRt.EncWf] at hEnc0; exact ⟨hEnc0.1, hEnc0.2.1⟩) + have hVa : isEpt = false → VaOkE va := fun he' => by subst he'; exact E.j.1.vaOkE hsl hcap' + have hEnc : CapRt.EncWf (.Frame fp sz rt dev false false 0#u64 0#u64) := by + simp only [CapRt.EncWf]; exact ⟨hcan.1, hcan.2, by simp, Or.inl fun j hj _ => by simp⟩ + have hca : CapRelA (.Frame fp sz rt dev false false 0#u64 0#u64) (.archObjectCap (Fl4SpecA64.extUnmappedFrame + (absFrameE isEpt (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev + (some (BitVec.ofNat 16 asid.val, va.bv))))) := + ⟨⟨hr1, hr3, by simp, hov⟩, by cases isEpt <;> rfl⟩ + have hok : CapObjOkA Z (.Frame fp sz rt dev false false 0#u64 0#u64) := + ⟨fun _ h => (by cases h), fun _ _ _ _ _ h => (by cases h), fun _ _ _ h => (by cases h), fun _ _ _ h => (by cases h), (by intro h; cases h)⟩ + obtain ⟨x, hkx, hset, hrest⟩ := page_unmap_jA K E RS hTO hEO (VsPgOffOSA.of_apA E.ap) hK hT hIpa length buf call + ex cte hab fp rt asid va sz dev isEpt h0 (lt_of_lt_of_eq h16a (by norm_num)) hVa (vmRightsOfA rt) hcap hEnc hca + (UA.of_shapeA rfl fun h => by cases h) hok + exact ⟨dec_pgUnmapA calling args xSlot cte exs isEpt _ _ _ dev _ _ hk he, x, hkx, hset, fun hTr hnul => + hrest k (fun he' => by rw [asidRootKind_stUpdA hkx]; exact hk he') hTr hnul⟩ + +set_option maxHeartbeats 2000000 in +/-- **`ARMPageTableUnmap` on an FL4 root, from the decoder** (module docstring): the spec decoder's single outcome +`.tableUnmap k` (finality given on both sides), and for it the restart, FL4's `decode_page_table` against +`performExtVSpace` with `JAtZAX` at FL4's returned arch state and the tree — `pt_unmap_jA` with its cap facts, `h16` +and `VsPgOffOSA` discharged; the table's membership `hp` from `PtCapInA` (S3 gen 29). -/ +theorem pt_unmap_decJA {ar : machine.Arch A} (K : ArchKeepsA ar AR) {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} + (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) + (hTO : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s q) (hEO : ExtTabOffA (L := L) Z m s) + (hK : Aligned (L.ksb s)) (hT : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s)) (hIpa : S2IpaE ar a) + (length buf : U64) (calling : Bool) (ex : Aeneas.Std.Array obj.Slot 3#usize) (args : List Fl4SpecA64.Data) + (xSlot : Fl4SpecA64.CapRef) (exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) + (cte : Fl4SpecA64.CslotPtr) (hab : AbsSlotA Z.th cte) + (ptr asid va : U64) (hPC : PtCapInA Z (L.mem s)) + (hcap : capA st s (cslotAddrA cte) = .PageTable ptr true asid va) + (hFI : FinInvA Z.th (L.mem s) σ Z.G) (hf : cdt.is_final_capability st s (cslotAddrA cte) = ok true) + (k : Fl4SpecA64.ExtRootKind) (hk : Fl4SpecA64.asidRootKind σ (BitVec.ofNat 16 asid.val) = some k) + (hk1 : k ≠ .stage1) : + (Fl4SpecA64.fl4ArchDecodeInvocation (Z := Unit) calling (Fl4SpecA64.archL .armPageTableUnmap) args xSlot cte + (.pageTableCap (specPtr ptr) .normalPtT (some (BitVec.ofNat 16 asid.val, va.bv))) exs).run σ = + ({(.ok (.extVSpace (.tableUnmap k (.pageTableCap (specPtr ptr) .normalPtT + (some (BitVec.ofNat 16 asid.val, va.bv))) cte)), σ)}, False) ∧ + ∃ x, σ.kheap σ.curThread = some (.tcb x) ∧ + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + (TreeA st OS Z.G.chains Z.G.par s → (∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) → + ∃ (s3 : S) (σ3 : Fl4SpecA64.State Unit) (fa : Result A), + vspace.decode_page_table st ar s a fl4_abi.label.ARMPageTableUnmap length (cslotAddrA cte) + (.PageTable ptr true asid va) buf ex = + (do let a1 ← fa; ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s3, a1)) ∧ + (([] : List Fl4SpecA64.Data), σ3) ∈ (Fl4SpecA64.performExtVSpace (Z := Unit) (.tableUnmap k + (.pageTableCap (specPtr ptr) .normalPtT (some (BitVec.ofNat 16 asid.val, va.bv))) cte) + (stUpdA σ σ.curThread x .restart)).1 ∧ + fa ⦃ a' => JAtZAX (L := L) AR Z s3 a' σ3 m ⦄ ∧ TreeA st OS Z.G.chains Z.G.par s3 ∧ + (CarryA Z (L.mem s) σ → CarryA Z (L.mem s3) σ3)) := by + have hcap' : capAtA (L.mem s) (cslotAddrA cte) = .PageTable ptr true asid va := hcap + have hp := hPC.hpA E.j.1.place hab hcap' + have hfS : Fl4SpecA64.isFinalCap' (Z := Unit) (.archObjectCap (.pageTableCap (specPtr ptr) .normalPtT + (some (BitVec.ofNat 16 asid.val, va.bv)))) σ := by + have := (fin_iff_ofA E.j.1.place E.j.1.st E.j.1.cdt.tree.lk hFI hab + (by rw [hcap']; exact pt_needsFinA _ _ _ _) (by rw [hcap']; rfl)).1 hf + rw [hcap'] at this; exact this + obtain ⟨c0, -, hrel0⟩ := StateRelA.getAbsA E.j.1.st hab + rw [hcap'] at hrel0 + obtain ⟨h16a, -⟩ := hrel0 + have hEnc0 : CapRt.EncWf (capAtA (L.mem s) (cslotAddrA cte)) := CapRt.decodeF_encWf _ _ + rw [hcap'] at hEnc0 + simp only [CapRt.EncWf] at hEnc0 + have hEnc : CapRt.EncWf (.PageTable ptr false 0#u64 0#u64) := by + simp only [CapRt.EncWf]; exact ⟨hEnc0.1, by simp, Or.inl fun j hj _ => by simp⟩ + have hca : CapRelA (.PageTable ptr false 0#u64 0#u64) (.archObjectCap (Fl4SpecA64.updateMapData + (.pageTableCap (specPtr ptr) .normalPtT (some (BitVec.ofNat 16 asid.val, va.bv))) none)) := + ⟨show (0#u64 : U64).val < 2 ^ 16 by simp, rfl⟩ + have hok : CapObjOkA Z (.PageTable ptr false 0#u64 0#u64) := + ⟨fun _ h => (by cases h), fun _ _ _ _ _ h => (by cases h), fun _ _ _ h => (by cases h), fun _ _ _ h => (by cases h), trivial⟩ + obtain ⟨x, hkx, hset, hrest⟩ := pt_unmap_jA K E RS hTO hEO (VsPgOffOSA.of_apA E.ap) hK hT hIpa length buf ex cte + hab ptr asid va (lt_of_lt_of_eq h16a (by norm_num)) hp hf hcap hEnc hca (UA.of_shapeA rfl fun h => by cases h) hok + exact ⟨dec_ptUnmapA calling args xSlot cte exs _ _ _ hk hk1 hfS, x, hkx, hset, fun hTr hnul => + hrest k (by rw [asidRootKind_stUpdA hkx]; exact hk) hk1 hTr hnul⟩ + + +/-- FL4's `decode_page_table` at `ARMPageTableUnmap` of a non-final cap: `RevokeFirst`, nothing written. -/ +theorem decode_pt_nfA {ar : machine.Arch A} (lbl0 length buf : U64) (slot : U64) (ex : Aeneas.Std.Array obj.Slot 3#usize) + (ptr asid va : U64) (mp : Bool) (hl : lbl0 = fl4_abi.label.ARMPageTableUnmap) + (hf : cdt.is_final_capability st s slot = ok false) : + vspace.decode_page_table st ar s a lbl0 length slot (.PageTable ptr mp asid va) buf ex = + ok (.Err (.Sys .RevokeFirst), s, a) := by + subst hl + have hct : sched.cur_thread st s = ok (L.curW s) := cur_thread_ok s + unfold vspace.decode_page_table + simp only [hct, hf, bind_ok, ↓reduceIte, Bool.false_eq_true] + +/-- **`ARMPageTableUnmap` of a non-final page-table cap** (item 3): with `FinInvA`, FL4's `is_final_capability = +false` makes the spec's decoder `RevokeFirst` at the unchanged state (any mapping, any root kind), FL4 returns +`RevokeFirst` with its state and arch state unchanged, the two errors are related and `JAtZAX` holds as at the entry. -/ +theorem pt_unmap_nfJA {ar : machine.Arch A} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} {ia ie : Nat} + (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) (hFI : FinInvA Z.th (L.mem s) σ Z.G) + (length buf : U64) (calling : Bool) (ex : Aeneas.Std.Array obj.Slot 3#usize) (args : List Fl4SpecA64.Data) + (xSlot : Fl4SpecA64.CapRef) (exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) + (cte : Fl4SpecA64.CslotPtr) (hab : AbsSlotA Z.th cte) (ptr asid va : U64) (mp : Bool) + (hcap : capA st s (cslotAddrA cte) = .PageTable ptr mp asid va) + (hf : cdt.is_final_capability st s (cslotAddrA cte) = ok false) : + (Fl4SpecA64.fl4ArchDecodeInvocation (Z := Unit) calling (Fl4SpecA64.archL .armPageTableUnmap) args xSlot cte + (.pageTableCap (specPtr ptr) .normalPtT (mapDataA mp asid va)) exs).run σ = + ({(.error .revokeFirst, σ)}, False) ∧ + vspace.decode_page_table st ar s a fl4_abi.label.ARMPageTableUnmap length (cslotAddrA cte) + (.PageTable ptr mp asid va) buf ex = ok (.Err (.Sys .RevokeFirst), s, a) ∧ + SysErrRelA (.Sys .RevokeFirst) .revokeFirst ∧ JAtZAX (L := L) AR Z s a σ m := by + have hcap' : capAtA (L.mem s) (cslotAddrA cte) = .PageTable ptr mp asid va := hcap + have hnf : ¬ Fl4SpecA64.isFinalCap' (Z := Unit) (.archObjectCap (.pageTableCap (specPtr ptr) .normalPtT + (mapDataA mp asid va))) σ := fun h => by + have := (fin_iff_ofA E.j.1.place E.j.1.st E.j.1.cdt.tree.lk hFI hab + (by rw [hcap']; exact pt_needsFinA _ _ _ _) (by rw [hcap']; rfl)).2 (by rw [hcap']; exact h) + exact Bool.false_ne_true (okinj (hf.symm.trans this)) + exact ⟨dec_ptUnmap_nfA calling args xSlot cte exs _ _ hnf, decode_pt_nfA _ length buf _ ex ptr asid va mp rfl hf, + trivial, E.j⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UnmapL4vJA.lean b/proofs/Fl4ProofsA64/P3/UnmapL4vJA.lean new file mode 100644 index 000000000..ea9203f1a --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UnmapL4vJA.lean @@ -0,0 +1,321 @@ +/- +P3 (aarch64, A64-P3 S3 gen 28, ruling 23:55Z items 1–3): **the tops of the l4v arms of `ARMPageUnmap` / +`ARMPageTableUnmap`** (the caps whose mapping names no FL4 root) **and of the unmapped Stage-2 frame**, on +`P3/UnmapS1JA`'s spec runs and helpers. Each top: the spec decoder's single outcome, the caller's restart, FL4's decode +against the spec's perform at the restart state, `JAtZAX` after (at FL4's returned arch state) and the MDB tree. With +gen 26–27's FL4-root and non-final arms (`P3/UnmapDecJA`), the `ARMPageUnmap` / `ARMPageTableUnmap` decode and +perform are total except the mapped Stage-1 table (below). The x86 counterparts are `P3/VsUnmapDec`'s native unmap arms. + +* **`page_unmapU_jA`** (an unmapped frame, any `is_ept`: the restart, FL4's decode, the spec's write of the unmapped + plain frame cap, `JAtZAX`; for a plain frame that write is the slot's own cap, `setCap_selfA`), with + **`page_unmapU_decJA`** (item 1: l4v `PageUnmap`; the port's `perform_pg_inv_unmap` of an unmapped cap is the + assertion only, so the spec state is the restart state) and **`page_unmapS2U_decJA`** (item 3: the Ext + `.pageUnmap .stage2`, the cap left a plain unmapped frame). +* **`pt_unmapU_decJA`** (item 1: a final unmapped table, l4v `PageTableUnmap`, finality from `FinInvA`). +* **`page_unmapS1_decJA`** (item 2: a frame mapped with ASID ≠ 0 on a Stage-1 root or on none — FL4 `unmap_page` + against l4v `unmap_page`, `unmap_postS1JA`, then the cap write; `ArchKeepsA.invPage`). +* **Not here: the mapped Stage-1 table** (`pt_unmapS1_decJA`): its proof elaborates up to the cap write, but the final + application of `P3/UnmapS1JA`'s `perfPtUnmapM_memA` runs away in elaboration (> 18 GiB, bisected to that one + application); it is gen-29 work (the pieces — `zero_postA`, `JAtZAX.of_msA`, `setcap_coreA`, + `perfPtUnmapM_memA` — are in place). + +Named premises: `ArchKeepsA` [P]. Decode-site hypotheses: `TabOffA`, `ExtTabOffA`, `RsSiteA`. Lemma hypotheses: +`FinInvA`, the root-kind branch `hS1`. No new named premise. + +**S3 gen 29 (ruling 00:45Z items 3–4). Changed statements**: every top here also concludes +`CarryA Z (L.mem s) σ → CarryA Z (L.mem s3) σ3` (`P3/CarryA.of_setcapA`; the spec side by `setCap_tyA` / +`perfPgUnmap_tyA`). The mapped Stage-1 table arm is `P3/UnmapPtS1JA`'s `pt_unmapS1_decJA` (gen 29). +-/ +import Fl4ProofsA64.P3.UnmapS1JA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Classical + +noncomputable section + +/-! ## The tops -/ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} +variable {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} {ia ie : Nat} + +set_option maxHeartbeats 2000000 in +/-- **An unmapped frame cap's `ARMPageUnmap`, FL4 side** (items 1 and 3): the restart, FL4's decode (restart, cap +write), the slot's abstract cap at the restart state, the spec's write of the unmapped plain frame cap, `JAtZAX` and +the tree after. -/ +theorem page_unmapU_jA (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) {ar : machine.Arch A} (length buf : U64) (call : Bool) + (ex : Aeneas.Std.Array obj.Slot 3#usize) (cte : Fl4SpecA64.CslotPtr) (hab : AbsSlotA Z.th cte) + (fp rt asid va : U64) (sz : capability.FrameSize) (dev isEpt : Bool) + (hcap : capA st s (cslotAddrA cte) = .Frame fp sz rt dev false isEpt asid va) : + ∃ x, σ.kheap σ.curThread = some (.tcb x) ∧ + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + (TreeA st OS Z.G.chains Z.G.par s → (∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) → + ∃ (s3 : S) (σ3 : Fl4SpecA64.State Unit), + vspace.decode_frame st ar s a fl4_abi.label.ARMPageUnmap length (cslotAddrA cte) + (.Frame fp sz rt dev false isEpt asid va) call buf ex = + ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s3, a) ∧ + ((), σ3) ∈ (Fl4SpecA64.setCap (Z := Unit) (.archObjectCap (.frameCap (specPtr fp) (vmRightsOfA rt) + (frameSizeOfA sz) dev none)) cte (stUpdA σ σ.curThread x .restart)).1 ∧ + (isEpt = false → σ3 = stUpdA σ σ.curThread x .restart) ∧ + (isEpt = false → (.archObjectCap (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev none), + stUpdA σ σ.curThread x .restart) ∈ (Fl4SpecA64.getCap (Z := Unit) cte (stUpdA σ σ.curThread x .restart)).1) ∧ + JAtZAX (L := L) AR Z s3 a σ3 m ∧ TreeA st OS Z.G.chains Z.G.par s3 ∧ + (CarryA Z (L.mem s) σ → CarryA Z (L.mem s3) σ3)) := by + have hcap' : capAtA (L.mem s) (cslotAddrA cte) = .Frame fp sz rt dev false isEpt asid va := hcap + obtain ⟨c0, -, hrel0⟩ := StateRelA.getAbsA E.j.1.st hab + rw [hcap'] at hrel0 + obtain ⟨⟨hr1, hr3, -, hov⟩, -⟩ := hrel0 + have hEnc0 : CapRt.EncWf (capAtA (L.mem s) (cslotAddrA cte)) := CapRt.decodeF_encWf _ _ + rw [hcap'] at hEnc0 + have hcan : CapRt.Canonical fp ∧ rt.val < 4 := by + cases isEpt <;> (simp only [CapRt.EncWf] at hEnc0; exact ⟨hEnc0.1, hEnc0.2.1⟩) + have hEnc : CapRt.EncWf (.Frame fp sz rt dev false false 0#u64 0#u64) := by + simp only [CapRt.EncWf]; exact ⟨hcan.1, hcan.2, by simp, Or.inl fun j hj _ => by simp⟩ + have hca : CapRelA (.Frame fp sz rt dev false false 0#u64 0#u64) + (.archObjectCap (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev none)) := + ⟨⟨hr1, hr3, by simp, hov⟩, rfl⟩ + have hok : CapObjOkA Z (.Frame fp sz rt dev false false 0#u64 0#u64) := + ⟨fun _ h => (by cases h), fun _ _ _ _ _ h => (by cases h), fun _ _ _ h => (by cases h), fun _ _ _ h => (by cases h), (by intro h; cases h)⟩ + obtain ⟨Rp, Nt, extra, sp, P⟩ := RS.pl + obtain ⟨x, hkx, hst, hset, E1, -, hOK, -, -⟩ := E.restartA P RS.sym RS.run RS.cur RS.vs RS.vc + refine ⟨x, hkx, hset, fun hTr hnul => ?_⟩ + obtain ⟨s3, σ3, hsc, ⟨c', heq, hgc⟩, hσ3, J3, T3⟩ := setcap_coreA E RS E1.j hOK (L.ksb_wr _ _ _) (L.cpu_wr _ _ _) cte hab + hcap hEnc hca (UA.of_shapeA rfl fun h => by cases h) hok (fun h => by cases h) (fun h => by cases h) hTr hnul + obtain ⟨cCur, hcCur, hcur1, hcur2⟩ := StateRelA.curA E1.j.1.st hab + have e0 : capAtA (L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART)) (cslotAddrA cte) = + .Frame fp sz rt dev false isEpt asid va := (hOK.capA (RS.sub _ (hab.isSlot E.j.1.place))).trans hcap + rw [e0] at hcCur + have hcur : (∀ sz' cn, (stUpdA σ σ.curThread x .restart).kheap cte.1 = some (.cnode sz' cn) → + cn cte.2 = some (absCapA (.Frame fp sz rt dev false isEpt asid va))) ∧ + (∀ t, (stUpdA σ σ.curThread x .restart).kheap cte.1 = some (.tcb t) → + Fl4SpecA64.tcbCnodeMap t cte.2 = some (absCapA (.Frame fp sz rt dev false isEpt asid va))) := by + rw [hcCur]; exact ⟨hcur1, hcur2⟩ + refine ⟨s3, σ3, ?_, hσ3, fun he => ?_, fun he => ?_, J3, T3, fun C => C.of_setcapA E1.sys.ent.os RS.sub hTr hOK + (RS.sub _ (hab.isSlot E.j.1.place)) hEnc hsc (fun _ _ _ h => by cases h) + (by show CapFinEqA _ (capA st s _); rw [hcap]; exact CapFinEqA.frameA _ _ _ _ _ _ _ _) + ((TyKeepA.stUpdA hkx _).trans (setCap_tyA _ _ _ _ hσ3))⟩ + · rw [page_unmapU_eqA ar s a length (cslotAddrA cte) fp rt asid va sz dev isEpt call buf ex, hst, bind_ok, hsc, + bind_ok] + · subst he + exact setCap_selfA hcur.1 hcur.2 hσ3 + · subst he + have h' : c' = .archObjectCap (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev none) := heq.symm + rw [← h']; exact hgc + +/-- **`ARMPageUnmap` of an unmapped plain frame cap** (item 1): l4v's `PageUnmap` at the decoder, FL4's decode +against l4v's `perform_pg_inv_unmap`, `JAtZAX` and the tree after. -/ +theorem page_unmapU_decJA (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) {ar : machine.Arch A} (length buf : U64) (call calling : Bool) + (ex : Aeneas.Std.Array obj.Slot 3#usize) (args : List Fl4SpecA64.Data) (xSlot : Fl4SpecA64.CapRef) + (exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) (cte : Fl4SpecA64.CslotPtr) (hab : AbsSlotA Z.th cte) + (fp rt asid va : U64) (sz : capability.FrameSize) (dev : Bool) + (hcap : capA st s (cslotAddrA cte) = .Frame fp sz rt dev false false asid va) : + (Fl4SpecA64.fl4ArchDecodeInvocation (Z := Unit) calling (Fl4SpecA64.archL .armPageUnmap) args xSlot cte + (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev none) exs).run σ = + ({(.ok (.spec (.invokePage (.pageUnmap (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev none) + cte))), σ)}, False) ∧ + ∃ x, σ.kheap σ.curThread = some (.tcb x) ∧ + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + (TreeA st OS Z.G.chains Z.G.par s → (∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) → + ∃ (s3 : S) (σ3 : Fl4SpecA64.State Unit), + vspace.decode_frame st ar s a fl4_abi.label.ARMPageUnmap length (cslotAddrA cte) + (.Frame fp sz rt dev false false asid va) call buf ex = + ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s3, a) ∧ + (([] : List Fl4SpecA64.Data), σ3) ∈ (Fl4SpecA64.performPageInvocation (Z := Unit) + (.pageUnmap (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev none) cte) + (stUpdA σ σ.curThread x .restart)).1 ∧ + JAtZAX (L := L) AR Z s3 a σ3 m ∧ TreeA st OS Z.G.chains Z.G.par s3 ∧ + (CarryA Z (L.mem s) σ → CarryA Z (L.mem s3) σ3)) := by + obtain ⟨x, hkx, hset, hrest⟩ := page_unmapU_jA (ar := ar) E RS length buf call ex cte hab fp rt asid va sz dev + false hcap + refine ⟨dec_pgUnmap_specA calling args xSlot cte exs _ _ _ dev none trivial, x, hkx, hset, fun hTr hnul => ?_⟩ + obtain ⟨s3, σ3, hd, hσ3, e3, hg3, J3, T3, C3⟩ := hrest hTr hnul + obtain rfl := e3 rfl + exact ⟨s3, _, hd, perfPgUnmapU_memA (hg3 rfl) hσ3, J3, T3, C3⟩ + +/-- **`ARMPageUnmap` of an unmapped Stage-2 frame cap** (item 3): the Ext `.pageUnmap .stage2` at the decoder, FL4's +decode against `performExtVSpace` (the cap left a plain unmapped frame), `JAtZAX` and the tree after. -/ +theorem page_unmapS2U_decJA (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) {ar : machine.Arch A} (length buf : U64) (call calling : Bool) + (ex : Aeneas.Std.Array obj.Slot 3#usize) (args : List Fl4SpecA64.Data) (xSlot : Fl4SpecA64.CapRef) + (exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) (cte : Fl4SpecA64.CslotPtr) (hab : AbsSlotA Z.th cte) + (fp rt asid va : U64) (sz : capability.FrameSize) (dev : Bool) + (hcap : capA st s (cslotAddrA cte) = .Frame fp sz rt dev false true asid va) : + (Fl4SpecA64.fl4ArchDecodeInvocation (Z := Unit) calling (Fl4SpecA64.archL .armPageUnmap) args xSlot cte + (.ext (.stage2FrameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev none)) exs).run σ = + ({(.ok (.extVSpace (.pageUnmap .stage2 (.ext (.stage2FrameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) + dev none)) cte)), σ)}, False) ∧ + ∃ x, σ.kheap σ.curThread = some (.tcb x) ∧ + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + (TreeA st OS Z.G.chains Z.G.par s → (∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) → + ∃ (s3 : S) (σ3 : Fl4SpecA64.State Unit), + vspace.decode_frame st ar s a fl4_abi.label.ARMPageUnmap length (cslotAddrA cte) + (.Frame fp sz rt dev false true asid va) call buf ex = + ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s3, a) ∧ + (([] : List Fl4SpecA64.Data), σ3) ∈ (Fl4SpecA64.performExtVSpace (Z := Unit) + (.pageUnmap .stage2 (.ext (.stage2FrameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev none)) + cte) (stUpdA σ σ.curThread x .restart)).1 ∧ + JAtZAX (L := L) AR Z s3 a σ3 m ∧ TreeA st OS Z.G.chains Z.G.par s3 ∧ + (CarryA Z (L.mem s) σ → CarryA Z (L.mem s3) σ3)) := by + obtain ⟨x, hkx, hset, hrest⟩ := page_unmapU_jA (ar := ar) E RS length buf call ex cte hab fp rt asid va sz dev + true hcap + refine ⟨dec_pgUnmapS2A calling args xSlot cte exs _ _ _ dev none, x, hkx, hset, fun hTr hnul => ?_⟩ + obtain ⟨s3, σ3, hd, hσ3, -, -, J3, T3, C3⟩ := hrest hTr hnul + exact ⟨s3, σ3, hd, perfS2UnmapU_memA hσ3, J3, T3, C3⟩ + +set_option maxHeartbeats 2000000 in +/-- **`ARMPageTableUnmap` of a final unmapped page-table cap** (item 1): l4v's `PageTableUnmap` at the decoder (the +spec's finality from `FinInvA`), FL4's decode against l4v's `perform_pt_inv_unmap`, `JAtZAX` and the tree after. -/ +theorem pt_unmapU_decJA (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) {ar : machine.Arch A} (length buf : U64) (calling : Bool) + (ex : Aeneas.Std.Array obj.Slot 3#usize) (args : List Fl4SpecA64.Data) (xSlot : Fl4SpecA64.CapRef) + (exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) (cte : Fl4SpecA64.CslotPtr) (hab : AbsSlotA Z.th cte) + (ptr asid va : U64) (hcap : capA st s (cslotAddrA cte) = .PageTable ptr false asid va) + (hFI : FinInvA Z.th (L.mem s) σ Z.G) (hf : cdt.is_final_capability st s (cslotAddrA cte) = ok true) : + (Fl4SpecA64.fl4ArchDecodeInvocation (Z := Unit) calling (Fl4SpecA64.archL .armPageTableUnmap) args xSlot cte + (.pageTableCap (specPtr ptr) .normalPtT none) exs).run σ = + ({(.ok (.spec (.invokePageTable (.pageTableUnmap (.pageTableCap (specPtr ptr) .normalPtT none) cte))), σ)}, + False) ∧ + ∃ x, σ.kheap σ.curThread = some (.tcb x) ∧ + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + (TreeA st OS Z.G.chains Z.G.par s → (∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) → + ∃ (s3 : S) (σ3 : Fl4SpecA64.State Unit), + vspace.decode_page_table st ar s a fl4_abi.label.ARMPageTableUnmap length (cslotAddrA cte) + (.PageTable ptr false asid va) buf ex = + ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s3, a) ∧ + ((), σ3) ∈ (Fl4SpecA64.performPageTableInvocation (Z := Unit) + (.pageTableUnmap (.pageTableCap (specPtr ptr) .normalPtT none) cte) (stUpdA σ σ.curThread x .restart)).1 ∧ + JAtZAX (L := L) AR Z s3 a σ3 m ∧ TreeA st OS Z.G.chains Z.G.par s3 ∧ + (CarryA Z (L.mem s) σ → CarryA Z (L.mem s3) σ3)) := by + have hcap' : capAtA (L.mem s) (cslotAddrA cte) = .PageTable ptr false asid va := hcap + have hfS : Fl4SpecA64.isFinalCap' (Z := Unit) (.archObjectCap (.pageTableCap (specPtr ptr) .normalPtT none)) σ := by + have := (fin_iff_ofA E.j.1.place E.j.1.st E.j.1.cdt.tree.lk hFI hab + (by rw [hcap']; exact pt_needsFinA _ _ _ _) (by rw [hcap']; rfl)).1 hf + rw [hcap'] at this; exact this + have hEnc0 : CapRt.EncWf (capAtA (L.mem s) (cslotAddrA cte)) := CapRt.decodeF_encWf _ _ + rw [hcap'] at hEnc0 + simp only [CapRt.EncWf] at hEnc0 + have hEnc : CapRt.EncWf (.PageTable ptr false 0#u64 0#u64) := by + simp only [CapRt.EncWf]; exact ⟨hEnc0.1, by simp, Or.inl fun j hj _ => by simp⟩ + have hca : CapRelA (.PageTable ptr false 0#u64 0#u64) + (.archObjectCap (.pageTableCap (specPtr ptr) .normalPtT none)) := + ⟨show (0#u64 : U64).val < 2 ^ 16 by simp, rfl⟩ + have hok : CapObjOkA Z (.PageTable ptr false 0#u64 0#u64) := + ⟨fun _ h => (by cases h), fun _ _ _ _ _ h => (by cases h), fun _ _ _ h => (by cases h), fun _ _ _ h => (by cases h), trivial⟩ + obtain ⟨Rp, Nt, extra, sp, P⟩ := RS.pl + obtain ⟨x, hkx, hst, hset, E1, -, hOK, -, -⟩ := E.restartA P RS.sym RS.run RS.cur RS.vs RS.vc + refine ⟨dec_ptUnmap_specA calling args xSlot cte exs _ none trivial hfS, x, hkx, hset, fun hTr hnul => ?_⟩ + obtain ⟨s3, σ3, hsc, ⟨c', heq, hgc⟩, hσ3, J3, T3⟩ := setcap_coreA E RS E1.j hOK (L.ksb_wr _ _ _) (L.cpu_wr _ _ _) cte hab + hcap hEnc hca (UA.of_shapeA rfl fun h => by cases h) hok (fun h => by cases h) (fun h => by cases h) hTr hnul + obtain ⟨cCur, hcCur, hcur1, hcur2⟩ := StateRelA.curA E1.j.1.st hab + have e0 : capAtA (L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART)) (cslotAddrA cte) = + .PageTable ptr false asid va := (hOK.capA (RS.sub _ (hab.isSlot E.j.1.place))).trans hcap + rw [e0] at hcCur + have hcur : (∀ sz cn, (stUpdA σ σ.curThread x .restart).kheap cte.1 = some (.cnode sz cn) → + cn cte.2 = some (absCapA (.PageTable ptr false asid va))) ∧ + (∀ t, (stUpdA σ σ.curThread x .restart).kheap cte.1 = some (.tcb t) → + Fl4SpecA64.tcbCnodeMap t cte.2 = some (absCapA (.PageTable ptr false asid va))) := by + rw [hcCur]; exact ⟨hcur1, hcur2⟩ + have hC : CarryA Z (L.mem s) σ → CarryA Z (L.mem s3) σ3 := fun C => C.of_setcapA E1.sys.ent.os RS.sub hTr hOK + (RS.sub _ (hab.isSlot E.j.1.place)) hEnc hsc (fun _ _ _ h => by cases h) + (by show CapFinEqA _ (capA st s _); rw [hcap]; exact CapFinEqA.ptA _ _ _ _) + ((TyKeepA.stUpdA hkx _).trans (setCap_tyA _ _ _ _ hσ3)) + obtain rfl : σ3 = stUpdA σ σ.curThread x .restart := setCap_selfA hcur.1 hcur.2 hσ3 + have h' : c' = .archObjectCap (.pageTableCap (specPtr ptr) .normalPtT none) := heq.symm + rw [h'] at hgc + refine ⟨s3, _, ?_, perfPtUnmapU_memA hgc hσ3, J3, T3, hC⟩ + rw [pt_unmapU_eqA ar s a length (cslotAddrA cte) ptr asid va buf ex hf, hst, bind_ok, hsc, bind_ok] + +set_option maxHeartbeats 4000000 in +/-- **`ARMPageUnmap` of a plain frame cap mapped on a Stage-1 root or on none** (item 2): l4v's `PageUnmap` at the +decoder; FL4's `unmap_page` (`unmap_pageA`, `unmap_postS1JA`) and the cap write against l4v's `perform_pg_inv_unmap` +(`unmap_page`, the cap read, the write); `JAtZAX` at FL4's returned arch state (`ArchKeepsA.invPage`) and the tree. -/ +theorem page_unmapS1_decJA {ar : machine.Arch A} (K : ArchKeepsA ar AR) + (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) + (hTO : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s q) + (hK : Aligned (L.ksb s)) (hT : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s)) + (length buf : U64) (call calling : Bool) (ex : Aeneas.Std.Array obj.Slot 3#usize) (args : List Fl4SpecA64.Data) + (xSlot : Fl4SpecA64.CapRef) (exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) + (cte : Fl4SpecA64.CslotPtr) (hab : AbsSlotA Z.th cte) + (fp rt asid va : U64) (sz : capability.FrameSize) (dev : Bool) (h0 : asid ≠ 0#u64) + (hcap : capA st s (cslotAddrA cte) = .Frame fp sz rt dev true false asid va) + (hS1 : Fl4SpecA64.asidRootKind σ (BitVec.ofNat 16 asid.val) = none ∨ + Fl4SpecA64.asidRootKind σ (BitVec.ofNat 16 asid.val) = some .stage1) : + (Fl4SpecA64.fl4ArchDecodeInvocation (Z := Unit) calling (Fl4SpecA64.archL .armPageUnmap) args xSlot cte + (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev (some (BitVec.ofNat 16 asid.val, va.bv))) + exs).run σ = + ({(.ok (.spec (.invokePage (.pageUnmap (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev + (some (BitVec.ofNat 16 asid.val, va.bv))) cte))), σ)}, False) ∧ + ∃ x, σ.kheap σ.curThread = some (.tcb x) ∧ + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + (TreeA st OS Z.G.chains Z.G.par s → (∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) → + ∃ (s3 : S) (σ3 : Fl4SpecA64.State Unit) (fa : Result A), + vspace.decode_frame st ar s a fl4_abi.label.ARMPageUnmap length (cslotAddrA cte) + (.Frame fp sz rt dev true false asid va) call buf ex = + (do let a1 ← fa; ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s3, a1)) ∧ + (([] : List Fl4SpecA64.Data), σ3) ∈ (Fl4SpecA64.performPageInvocation (Z := Unit) + (.pageUnmap (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev + (some (BitVec.ofNat 16 asid.val, va.bv))) cte) (stUpdA σ σ.curThread x .restart)).1 ∧ + fa ⦃ a' => JAtZAX (L := L) AR Z s3 a' σ3 m ⦄ ∧ TreeA st OS Z.G.chains Z.G.par s3 ∧ + (CarryA Z (L.mem s) σ → CarryA Z (L.mem s3) σ3)) := by + have hcap' : capAtA (L.mem s) (cslotAddrA cte) = .Frame fp sz rt dev true false asid va := hcap + obtain ⟨c0, -, hrel0⟩ := StateRelA.getAbsA E.j.1.st hab + rw [hcap'] at hrel0 + obtain ⟨⟨hr1, hr3, ⟨h16a, -⟩, hov⟩, -⟩ := hrel0 + have h16 : asid.val < 65536 := lt_of_lt_of_eq h16a (by norm_num) + have hEnc0 : CapRt.EncWf (capAtA (L.mem s) (cslotAddrA cte)) := CapRt.decodeF_encWf _ _ + rw [hcap'] at hEnc0 + simp only [CapRt.EncWf] at hEnc0 + have hEnc : CapRt.EncWf (.Frame fp sz rt dev false false 0#u64 0#u64) := by + simp only [CapRt.EncWf]; exact ⟨hEnc0.1, hEnc0.2.1, by simp, Or.inl fun j hj _ => by simp⟩ + have hca : CapRelA (.Frame fp sz rt dev false false 0#u64 0#u64) + (.archObjectCap (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev none)) := + ⟨⟨hr1, hr3, by simp, hov⟩, rfl⟩ + have hok : CapObjOkA Z (.Frame fp sz rt dev false false 0#u64 0#u64) := + ⟨fun _ h => (by cases h), fun _ _ _ _ _ h => (by cases h), fun _ _ _ h => (by cases h), fun _ _ _ h => (by cases h), (by intro h; cases h)⟩ + have hpOS := VsPgOffOSA.of_apA E.ap + obtain ⟨Rp, Nt, extra, sp, P⟩ := RS.pl + obtain ⟨x, hkx, hst, hset, E1, -, hOK, -, -⟩ := E.restartA P RS.sym RS.run RS.cur RS.vs RS.vc + refine ⟨dec_pgUnmap_specA calling args xSlot cte exs _ _ _ dev _ hS1, x, hkx, hset, fun hTr hnul => ?_⟩ + have hS1' : Fl4SpecA64.asidRootKind (stUpdA σ σ.curThread x .restart) (BitVec.ofNat 16 asid.val) = none ∨ + Fl4SpecA64.asidRootKind (stUpdA σ σ.curThread x .restart) (BitVec.ofNat 16 asid.val) = some .stage1 := by + rw [asidRootKind_stUpdA hkx]; exact hS1 + have hks : L.ksb (L.wr s (stA (L.curW s)) sched.TS_RESTART) = L.ksb s := L.ksb_wr _ _ _ + have hcpu : L.cpu (L.wr s (stA (L.curW s)) sched.TS_RESTART) = L.cpu s := L.cpu_wr _ _ _ + generalize L.wr s (stA (L.curW s)) sched.TS_RESTART = s1 at hst E1 hOK hks hcpu + have hN := bxi_nodes E1.sys.ent.bxi + have hU := unmap_pageA (ar := ar) (a := a) E1.j.1.vs (by rw [hks]; exact hK) + (fun p hp i hi => by rw [hks]; exact hT p i hi (Or.inr (Or.inr (Or.inl hp)))) E1.j.2.2.1.obj sz h16 + (hs1_ofE hS1') va fp + obtain ⟨s2, fa, σu, hfa, hu, hσu, J2, hk2, hks2, hcpu2⟩ := unmap_postS1JA (unmapPage_kmA _ _ _ _) E1.j + (FootA.aligned E1.j.1 hN E1.scAl E1.ap.vcAl) (fun q hq => (hTO q hq).of_ksA hks hcpu) E1.sys.ent.os hpOS hU + obtain ⟨s3, σ3, hsc, ⟨_, heq, hg⟩, hσ3, J3, T3⟩ := setcap_coreA E RS J2 (hOK.trans' hk2) (hks2.trans hks) + (hcpu2.trans hcpu) cte hab hcap hEnc hca (UA.of_shapeA rfl fun h => by cases h) hok (fun h => by cases h) + (fun h => by cases h) hTr hnul + rw [← heq] at hg + have hP := perfPgUnmapM_memA (R := vmRightsOfA rt) (dev := dev) hσu hg hσ3 + refine ⟨s3, σ3, fa, ?_, hP, ?_, T3, fun C => C.of_setcapA E1.sys.ent.os RS.sub hTr (hOK.trans' hk2) + (RS.sub _ (hab.isSlot E.j.1.place)) hEnc hsc (fun _ _ _ h => by cases h) + (by show CapFinEqA _ (capA st s _); rw [hcap]; exact CapFinEqA.frameA _ _ _ _ _ _ _ _) + ((TyKeepA.stUpdA hkx _).trans (perfPgUnmap_tyA _ _ _ _ hP))⟩ + · rw [page_unmap_eqA ar s a length (cslotAddrA cte) fp rt asid va sz dev false call buf ex h0, hst, bind_ok, + if_neg Bool.false_ne_true] + exact then_setcapA hu hsc + · rcases hfa with rfl | rfl + · exact (WP.spec_ok _).2 J3 + · exact WP.spec_mono (K.invPage a _ asid va J3.1.ar) fun _ h => J3.of_arA h + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UnmapPtS1JA.lean b/proofs/Fl4ProofsA64/P3/UnmapPtS1JA.lean new file mode 100644 index 000000000..88324a425 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UnmapPtS1JA.lean @@ -0,0 +1,273 @@ +/- +P3 (aarch64, A64-P3 S3 gen 29, ruling 00:45Z items 1–2): **the mapped Stage-1 page-table arm of +`ARMPageTableUnmap`** (`pt_unmapS1_decJA`), the one arm `P3/UnmapL4vJA` left open, and **the mapped ASID-0 frame arm +of `ARMPageUnmap`** (`page_unmapZ_decJA`). The x86 counterparts are `P3/VsUnmapDec`'s native unmap arms. + +* **`perfPtUnmapN_memA`**: l4v's `perform_pt_inv_unmap` of a mapped `normalPtT` cap as a concrete chain — + `unmap_page_table`, the 512 `store_pte`s given as **an equation** (the form `P3/TabZeroA`'s `zero_relA` / + `P3/UnmapTopJA`'s `zero_postA` produce), the cache clean, the cap read, the write of the unmapped cap. Gen 28's + generic `perfPtUnmapM_memA` (any `PtType`, the `store_pte` loop as a membership) ran away at every full application + (> 12–18 GiB); this concrete statement is applied with its hypotheses in the exact syntactic form of the top's facts. +* **`pt_unmapS1_decJA`**: a final page-table cap mapped on a Stage-1 root or on none — l4v's `PageTableUnmap` at the + decoder (finality from `FinInvA`), FL4's `unmap_page_table` (`unmap_page_tableA`, `unmap_postS1JA`), `zero` + (`zero_postA`) and the cap write against the spec chain above (`JAtZAX.of_msA` for the cache clean, + `setcap_coreA`); `JAtZAX` at FL4's returned arch state (`ArchKeepsA.invAsid`) and the tree. +* **`unmapPage_zeroA`** (l4v's `unmap_page` of ASID 0: `find_vspace_for_asid` throws, the handler returns), + **`page_unmap0_eqA`** (FL4's arm: `f.asid != 0` fails, the unmap is skipped, the cap write) and + **`page_unmapZ_decJA`**: a plain frame cap mapped with ASID 0 — the decoder (no root names ASID 0), the restart, both + sides' cap writes, `JAtZAX` and the tree; strictly stronger than excluding ASID 0 through `FrPlA.mz`. + +Named premises: `ArchKeepsA` [P]. Decode-site hypotheses: `TabOffA`, `ExtTabOffA`, `RsSiteA`. Lemma hypotheses: +`FinInvA`, the root-kind branch `hS1`, `PtCapInA` (as `pt_unmap_decJA`). Both tops also conclude +`CarryA Z (L.mem s) σ → CarryA Z (L.mem s3) σ3` (`P3/CarryA`). No new named premise. New +module; no existing statement changed. +-/ +import Fl4ProofsA64.P3.UnmapL4vJA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Classical + +noncomputable section + +section spec +open Fl4Spec + +set_option maxHeartbeats 400000 in +/-- **l4v's `perform_pt_inv_unmap` of a mapped `normalPtT` cap** as a concrete chain (module docstring). -/ +theorem perfPtUnmapN_memA {σ1 σu σz σ5 σ3 : Fl4SpecA64.State Unit} {p : Fl4SpecA64.ObjRef} + {asid : Fl4SpecA64.Asid} {v : Fl4SpecA64.VSpaceRef} {cte : Fl4SpecA64.CslotPtr} {ac : Fl4SpecA64.ArchCap} + (hu : ((), σu) ∈ (Fl4SpecA64.unmapPageTable (Z := Unit) asid v p σ1).1) + (hz : NonDet.mapMx (fun sl => Fl4SpecA64.storePte (Z := Unit) .normalPtT sl .invalidPTE) + (Fl4SpecA64.uptoEnumStep p (p + ((1 : Fl4SpecA64.Word) <<< Fl4SpecA64.pteBits)) + (p + Fl4SpecA64.mask (Fl4SpecA64.ptBits .normalPtT))) σu = ({((), σz)}, False)) + (hm : ((), σ5) ∈ (Fl4SpecA64.doMachineOp (Z := Unit) (Fl4SpecA64.cleanCacheRange_PoU p + (p + Fl4SpecA64.mask (Fl4SpecA64.ptBits .normalPtT)) (Fl4SpecA64.addrFromPPtr p)) σz).1) + (hg : (.archObjectCap ac, σ5) ∈ (Fl4SpecA64.getCap (Z := Unit) cte σ5).1) + (hac : Fl4SpecA64.updateMapData ac none = .pageTableCap p .normalPtT none) + (hs : ((), σ3) ∈ (Fl4SpecA64.setCap (Z := Unit) (.archObjectCap (.pageTableCap p .normalPtT none)) cte σ5).1) : + ((), σ3) ∈ (Fl4SpecA64.performPageTableInvocation (Z := Unit) + (.pageTableUnmap (.pageTableCap p .normalPtT (some (asid, v))) cte) σ1).1 := by + unfold Fl4SpecA64.performPageTableInvocation Fl4SpecA64.performPtInvUnmap + dsimp only [Fl4SpecA64.ArchCap.acapMapData, Fl4SpecA64.ArchCap.acapObj, Fl4SpecA64.ArchCap.acapPtType] + refine bind_memA (x := ((), σ1)) (assert_memA rfl σ1) ?_ + refine bind_memA (x := (p, σ1)) (Set.mem_singleton _) ?_ + refine bind_memA hu ?_ + refine bind_memA (x := (Fl4SpecA64.uptoEnumStep p (p + ((1 : Fl4SpecA64.Word) <<< Fl4SpecA64.pteBits)) + (p + Fl4SpecA64.mask (Fl4SpecA64.ptBits .normalPtT)), σu)) (Set.mem_singleton _) ?_ + refine bind_memA (x := ((), σz)) ?_ ?_ + · dsimp only + rw [hz]; exact Set.mem_singleton _ + refine bind_memA hm ?_ + refine bind_memA (liftArch_memA hg) ?_ + dsimp only + rw [hac]; exact hs + +/-- **l4v's `unmap_page` of ASID 0 is a no-op**: `find_vspace_for_asid` throws and the handler returns. -/ +theorem unmapPage_zeroA (sz : Fl4SpecA64.VMPageSize) (v : Fl4SpecA64.VSpaceRef) (p : Fl4SpecA64.ObjRef) + (σ : Fl4SpecA64.State Unit) : ((), σ) ∈ (Fl4SpecA64.unmapPage (Z := Unit) sz (BitVec.ofNat 16 0) v p σ).1 := by + unfold Fl4SpecA64.unmapPage + rw [catch_bind_errA (findVs_noneA (vspaceForAsid_zeroA σ))] + exact Set.mem_singleton _ + +end spec + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} +variable {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} {ia ie : Nat} + +set_option maxHeartbeats 4000000 in +/-- **`ARMPageTableUnmap` of a final page-table cap mapped on a Stage-1 root or on none** (item 2): l4v's +`PageTableUnmap` at the decoder (finality from `FinInvA`); FL4's `unmap_page_table` (`unmap_page_tableA`, +`unmap_postS1JA`), `zero` (`zero_relA`, `JAtZAX.of_zeroA`) and the cap write against l4v's `perform_pt_inv_unmap` +(`unmap_page_table`, the 512 `store_pte`s, the cache clean, `JAtZAX.of_msA`, the cap read, the write); `JAtZAX` at +FL4's returned arch state (`ArchKeepsA.invAsid`) and the tree; the table's membership from `PtCapInA` (as +`pt_unmap_decJA`); `CarryA` kept. -/ +theorem pt_unmapS1_decJA {ar : machine.Arch A} (K : ArchKeepsA ar AR) + (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) + (hTO : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s q) (hEO : ExtTabOffA (L := L) Z m s) + (hK : Aligned (L.ksb s)) (hT : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s)) + (length buf : U64) (calling : Bool) (ex : Aeneas.Std.Array obj.Slot 3#usize) (args : List Fl4SpecA64.Data) + (xSlot : Fl4SpecA64.CapRef) (exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) + (cte : Fl4SpecA64.CslotPtr) (hab : AbsSlotA Z.th cte) + (ptr asid va : U64) (hPC : PtCapInA Z (L.mem s)) + (hcap : capA st s (cslotAddrA cte) = .PageTable ptr true asid va) + (hFI : FinInvA Z.th (L.mem s) σ Z.G) (hf : cdt.is_final_capability st s (cslotAddrA cte) = ok true) + (hS1 : Fl4SpecA64.asidRootKind σ (BitVec.ofNat 16 asid.val) = none ∨ + Fl4SpecA64.asidRootKind σ (BitVec.ofNat 16 asid.val) = some .stage1) : + (Fl4SpecA64.fl4ArchDecodeInvocation (Z := Unit) calling (Fl4SpecA64.archL .armPageTableUnmap) args xSlot cte + (.pageTableCap (specPtr ptr) .normalPtT (some (BitVec.ofNat 16 asid.val, va.bv))) exs).run σ = + ({(.ok (.spec (.invokePageTable (.pageTableUnmap (.pageTableCap (specPtr ptr) .normalPtT + (some (BitVec.ofNat 16 asid.val, va.bv))) cte))), σ)}, False) ∧ + ∃ x, σ.kheap σ.curThread = some (.tcb x) ∧ + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + (TreeA st OS Z.G.chains Z.G.par s → (∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) → + ∃ (s3 : S) (σ3 : Fl4SpecA64.State Unit) (fa : Result A), + vspace.decode_page_table st ar s a fl4_abi.label.ARMPageTableUnmap length (cslotAddrA cte) + (.PageTable ptr true asid va) buf ex = + (do let a1 ← fa; ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s3, a1)) ∧ + ((), σ3) ∈ (Fl4SpecA64.performPageTableInvocation (Z := Unit) (.pageTableUnmap (.pageTableCap (specPtr ptr) + .normalPtT (some (BitVec.ofNat 16 asid.val, va.bv))) cte) (stUpdA σ σ.curThread x .restart)).1 ∧ + fa ⦃ a' => JAtZAX (L := L) AR Z s3 a' σ3 m ⦄ ∧ TreeA st OS Z.G.chains Z.G.par s3 ∧ + (CarryA Z (L.mem s) σ → CarryA Z (L.mem s3) σ3)) := by + have hcap' : capAtA (L.mem s) (cslotAddrA cte) = .PageTable ptr true asid va := hcap + have hp := hPC.hpA E.j.1.place hab hcap' + have hfS : Fl4SpecA64.isFinalCap' (Z := Unit) (.archObjectCap (.pageTableCap (specPtr ptr) .normalPtT + (some (BitVec.ofNat 16 asid.val, va.bv)))) σ := by + have := (fin_iff_ofA E.j.1.place E.j.1.st E.j.1.cdt.tree.lk hFI hab + (by rw [hcap']; exact pt_needsFinA _ _ _ _) (by rw [hcap']; rfl)).1 hf + rw [hcap'] at this; exact this + obtain ⟨c0, -, hrel0⟩ := StateRelA.getAbsA E.j.1.st hab + rw [hcap'] at hrel0 + obtain ⟨h16a, -⟩ := hrel0 + have h16 : asid.val < 65536 := lt_of_lt_of_eq h16a (by norm_num) + have hEnc0 : CapRt.EncWf (capAtA (L.mem s) (cslotAddrA cte)) := CapRt.decodeF_encWf _ _ + rw [hcap'] at hEnc0 + simp only [CapRt.EncWf] at hEnc0 + have hEnc : CapRt.EncWf (.PageTable ptr false 0#u64 0#u64) := by + simp only [CapRt.EncWf]; exact ⟨hEnc0.1, by simp, Or.inl fun j hj _ => by simp⟩ + have hca : CapRelA (.PageTable ptr false 0#u64 0#u64) + (.archObjectCap (.pageTableCap (specPtr ptr) .normalPtT none)) := + ⟨show (0#u64 : U64).val < 2 ^ 16 by simp, rfl⟩ + have hok : CapObjOkA Z (.PageTable ptr false 0#u64 0#u64) := + ⟨fun _ h => (by cases h), fun _ _ _ _ _ h => (by cases h), fun _ _ _ h => (by cases h), fun _ _ _ h => (by cases h), trivial⟩ + have hpOS := VsPgOffOSA.of_apA E.ap + obtain ⟨Rp, Nt, extra, sp, P⟩ := RS.pl + obtain ⟨x, hkx, hst, hset, E1, -, hOK, -, -⟩ := E.restartA P RS.sym RS.run RS.cur RS.vs RS.vc + refine ⟨dec_ptUnmap_specA calling args xSlot cte exs _ _ hS1 hfS, x, hkx, hset, fun hTr hnul => ?_⟩ + have hS1' : Fl4SpecA64.asidRootKind (stUpdA σ σ.curThread x .restart) (BitVec.ofNat 16 asid.val) = none ∨ + Fl4SpecA64.asidRootKind (stUpdA σ σ.curThread x .restart) (BitVec.ofNat 16 asid.val) = some .stage1 := by + rw [asidRootKind_stUpdA hkx]; exact hS1 + have hks : L.ksb (L.wr s (stA (L.curW s)) sched.TS_RESTART) = L.ksb s := L.ksb_wr _ _ _ + have hcpu : L.cpu (L.wr s (stA (L.curW s)) sched.TS_RESTART) = L.cpu s := L.cpu_wr _ _ _ + generalize L.wr s (stA (L.curW s)) sched.TS_RESTART = s1 at hst E1 hOK hks hcpu + have hN := bxi_nodes E1.sys.ent.bxi + have hO := E1.sys.ent.os + have hU := unmap_page_tableA (ar := ar) (a := a) E1.j.1.vs (by rw [hks]; exact hK) + (fun p hp i hi => by rw [hks]; exact hT p i hi (Or.inr (Or.inr (Or.inl hp)))) E1.j.2.2.1.obj h16 + (hs1_ofE hS1') va ptr + obtain ⟨s2, fa, σu, hfa, hu, hσu, J2, hk2, hks2, hcpu2⟩ := unmap_postS1JA (unmapPageTable_kmA _ _ _) E1.j + (FootA.aligned E1.j.1 hN E1.scAl E1.ap.vcAl) (fun q hq => (hTO q hq).of_ksA hks hcpu) hO hpOS hU + have hks2' : L.ksb s2 = L.ksb s := hks2.trans hks + have hcpu2' : L.cpu s2 = L.cpu s := hcpu2.trans hcpu + have hN2 : ∀ c ∈ Z.Cs, ∀ o : U64, o.val < 4288 → o.val % 8 = 0 → Aligned (L.nodeAt s2 c o) := + fun c hc o h1 h2 => by rw [nodeAt_ksb hks2]; exact hN c hc o h1 h2 + obtain ⟨t', hrun0, J4, hk3, hks3, hcpu3⟩ := zero_postA J2 (FootA.aligned J2.1 hN2 E1.scAl E1.ap.vcAl) + (fun q hq => (hTO q hq).of_ksA hks2' hcpu2') (hEO.of_ksA hks2' hcpu2') (by rw [hks2']; exact hK) + (by rw [hks2']; exact hT) hO hpOS hp + obtain ⟨-, ⟨⟨⟨⟩, σ5⟩, h5⟩, h5m⟩ := doMachineOp_okA (mop := Fl4SpecA64.cleanCacheRange_PoU (specPtr ptr) + (specPtr ptr + Fl4SpecA64.mask (Fl4SpecA64.ptBits .normalPtT)) (Fl4SpecA64.addrFromPPtr (specPtr ptr))) + (machineOpLift_okA _) (ptSetA σu (specPtr ptr) t') + have J5 := J4.of_msA (σ' := σ5) (h5m _ h5) + obtain ⟨s5, σ6, hsc, ⟨c', hc', hg⟩, hσ6, J6, T6⟩ := setcap_coreA E RS J5 ((hOK.trans' hk2).trans' hk3) (hks3.trans hks2') + (hcpu3.trans hcpu2') cte hab + hcap hEnc hca (UA.of_shapeA rfl fun h => by cases h) hok (fun h => by cases h) (fun h => by cases h) hTr hnul + obtain ⟨asid', va', rfl, _, _⟩ := CapEqA.pt_mapped_iff.mp hc' + have hP := perfPtUnmapN_memA (p := specPtr ptr) (asid := BitVec.ofNat 16 asid.val) (v := va.bv) (cte := cte) + (σz := ptSetA σu (specPtr ptr) t') + (ac := .pageTableCap (specPtr ptr) .normalPtT (some (asid', va'))) hσu hrun0 h5 hg rfl hσ6 + refine ⟨s5, σ6, fa, ?_, hP, ?_, T6, fun C => C.of_setcapA hO RS.sub hTr ((hOK.trans' hk2).trans' hk3) + (RS.sub _ (hab.isSlot E.j.1.place)) hEnc hsc (fun _ _ _ h => by cases h) + (by show CapFinEqA _ (capA st s _); rw [hcap]; exact CapFinEqA.ptA _ _ _ _) + ((TyKeepA.stUpdA hkx _).trans (perfPtUnmap_tyA _ _ _ _ hP))⟩ + · rw [pt_unmap_eqA ar s a length (cslotAddrA cte) ptr asid va buf ex hf, hst, bind_ok] + exact then_setcapA (then_zeroA hu ptr) hsc + · rcases hfa with rfl | rfl + · exact (WP.spec_ok _).2 J6 + · exact WP.spec_mono (K.invAsid a _ asid J6.1.ar) fun _ h => J6.of_arA h + + +/-- **FL4's `ARMPageUnmap` of a plain frame mapped with ASID 0**: the restart and the cap write (the unmap is +skipped). -/ +theorem page_unmap0_eqA (ar : machine.Arch A) (s : S) (a : A) (length : U64) (slot : obj.Slot) + (fp rt va : U64) (sz : capability.FrameSize) (dev call : Bool) (buf : U64) + (ex : Aeneas.Std.Array obj.Slot 3#usize) : + vspace.decode_frame st ar s a fl4_abi.label.ARMPageUnmap length slot (.Frame fp sz rt dev true false 0#u64 va) + call buf ex = + (do let s1 ← sched.set_thread_state st s (L.curW s) sched.TS_RESTART + let s3 ← obj.Slot.set_cap st slot s1 (.Frame fp sz rt dev false false 0#u64 0#u64) + ok (core.result.Result.Ok (), s3, a)) := by + have hct : sched.cur_thread st s = ok (L.curW s) := cur_thread_ok s + have hne : ¬ fl4_abi.label.ARMPageUnmap = fl4_abi.label.ARMPageMap := by + unfold fl4_abi.label.ARMPageUnmap fl4_abi.label.ARMPageMap; decide + have hb : ((0#u64 : U64) != 0#u64) = false := by decide + unfold vspace.decode_frame + simp only [vspace.as_frame, hct, bind_ok, hne, hb, eq_self_iff_true, if_true, ↓reduceIte, vspace.unmapped_frame, + Bool.false_eq_true] + rfl + +set_option maxHeartbeats 2000000 in +/-- **`ARMPageUnmap` of a plain frame cap mapped with ASID 0** (ruling 00:45Z item 2): l4v's `PageUnmap` at the +decoder (no root names ASID 0); FL4 skips the unmap (`f.asid != 0`) and writes the unmapped cap, l4v's `unmap_page` +fails `find_vspace_for_asid` and catches (`unmapPage_zeroA`), then reads and rewrites the cap; `JAtZAX` and the tree +after. Strictly stronger than excluding ASID 0 (`FrPlA.mz`): no hypothesis on callers. -/ +theorem page_unmapZ_decJA {ar : machine.Arch A} + (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) + (length buf : U64) (call calling : Bool) (ex : Aeneas.Std.Array obj.Slot 3#usize) (args : List Fl4SpecA64.Data) + (xSlot : Fl4SpecA64.CapRef) (exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) + (cte : Fl4SpecA64.CslotPtr) (hab : AbsSlotA Z.th cte) + (fp rt va : U64) (sz : capability.FrameSize) (dev : Bool) + (hcap : capA st s (cslotAddrA cte) = .Frame fp sz rt dev true false 0#u64 va) : + (Fl4SpecA64.fl4ArchDecodeInvocation (Z := Unit) calling (Fl4SpecA64.archL .armPageUnmap) args xSlot cte + (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev (some (BitVec.ofNat 16 (0#u64 : U64).val, va.bv))) + exs).run σ = + ({(.ok (.spec (.invokePage (.pageUnmap (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev + (some (BitVec.ofNat 16 (0#u64 : U64).val, va.bv))) cte))), σ)}, False) ∧ + ∃ x, σ.kheap σ.curThread = some (.tcb x) ∧ + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + (TreeA st OS Z.G.chains Z.G.par s → (∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) → + ∃ (s3 : S) (σ3 : Fl4SpecA64.State Unit), + vspace.decode_frame st ar s a fl4_abi.label.ARMPageUnmap length (cslotAddrA cte) + (.Frame fp sz rt dev true false 0#u64 va) call buf ex = + ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s3, a) ∧ + (([] : List Fl4SpecA64.Data), σ3) ∈ (Fl4SpecA64.performPageInvocation (Z := Unit) + (.pageUnmap (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev + (some (BitVec.ofNat 16 (0#u64 : U64).val, va.bv))) cte) (stUpdA σ σ.curThread x .restart)).1 ∧ + JAtZAX (L := L) AR Z s3 a σ3 m ∧ TreeA st OS Z.G.chains Z.G.par s3 ∧ + (CarryA Z (L.mem s) σ → CarryA Z (L.mem s3) σ3)) := by + have hcap' : capAtA (L.mem s) (cslotAddrA cte) = .Frame fp sz rt dev true false 0#u64 va := hcap + have e0 : (0#u64 : U64).val = 0 := rfl + obtain ⟨c0, -, hrel0⟩ := StateRelA.getAbsA E.j.1.st hab + rw [hcap'] at hrel0 + obtain ⟨⟨hr1, hr3, -, hov⟩, -⟩ := hrel0 + have hEnc0 : CapRt.EncWf (capAtA (L.mem s) (cslotAddrA cte)) := CapRt.decodeF_encWf _ _ + rw [hcap'] at hEnc0 + simp only [CapRt.EncWf] at hEnc0 + have hEnc : CapRt.EncWf (.Frame fp sz rt dev false false 0#u64 0#u64) := by + simp only [CapRt.EncWf]; exact ⟨hEnc0.1, hEnc0.2.1, by simp, Or.inl fun j hj _ => by simp⟩ + have hca : CapRelA (.Frame fp sz rt dev false false 0#u64 0#u64) + (.archObjectCap (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev none)) := + ⟨⟨hr1, hr3, by simp, hov⟩, rfl⟩ + have hok : CapObjOkA Z (.Frame fp sz rt dev false false 0#u64 0#u64) := + ⟨fun _ h => (by cases h), fun _ _ _ _ _ h => (by cases h), fun _ _ _ h => (by cases h), fun _ _ _ h => (by cases h), (by intro h; cases h)⟩ + have hk0 : Fl4SpecA64.asidRootKind σ (BitVec.ofNat 16 (0#u64 : U64).val) = none ∨ + Fl4SpecA64.asidRootKind σ (BitVec.ofNat 16 (0#u64 : U64).val) = some .stage1 := by + left; unfold Fl4SpecA64.asidRootKind; rw [e0, vspaceForAsid_zeroA]; rfl + obtain ⟨Rp, Nt, extra, sp, P⟩ := RS.pl + obtain ⟨x, hkx, hst, hset, E1, -, hOK, -, -⟩ := E.restartA P RS.sym RS.run RS.cur RS.vs RS.vc + refine ⟨dec_pgUnmap_specA calling args xSlot cte exs _ _ _ dev _ hk0, x, hkx, hset, fun hTr hnul => ?_⟩ + have hks : L.ksb (L.wr s (stA (L.curW s)) sched.TS_RESTART) = L.ksb s := L.ksb_wr _ _ _ + have hcpu : L.cpu (L.wr s (stA (L.curW s)) sched.TS_RESTART) = L.cpu s := L.cpu_wr _ _ _ + generalize L.wr s (stA (L.curW s)) sched.TS_RESTART = s1 at hst E1 hOK hks hcpu + obtain ⟨s3, σ3, hsc, ⟨_, heq, hg⟩, hσ3, J3, T3⟩ := setcap_coreA E RS E1.j hOK hks hcpu cte hab hcap hEnc hca + (UA.of_shapeA rfl fun h => by cases h) hok (fun h => by cases h) (fun h => by cases h) hTr hnul + rw [← heq] at hg + have hu0 : ((), stUpdA σ σ.curThread x .restart) ∈ (Fl4SpecA64.unmapPage (Z := Unit) (frameSizeOfA sz) + (BitVec.ofNat 16 (0#u64 : U64).val) va.bv (specPtr fp) (stUpdA σ σ.curThread x .restart)).1 := by + rw [e0]; exact unmapPage_zeroA _ _ _ _ + have hP := perfPgUnmapM_memA (R := vmRightsOfA rt) (dev := dev) hu0 hg hσ3 + refine ⟨s3, σ3, ?_, hP, J3, T3, fun C => C.of_setcapA E1.sys.ent.os RS.sub hTr hOK + (RS.sub _ (hab.isSlot E.j.1.place)) hEnc hsc (fun _ _ _ h => by cases h) + (by show CapFinEqA _ (capA st s _); rw [hcap]; exact CapFinEqA.frameA _ _ _ _ _ _ _ _) + ((TyKeepA.stUpdA hkx _).trans (perfPgUnmap_tyA _ _ _ _ hP))⟩ + rw [page_unmap0_eqA ar s a length (cslotAddrA cte) fp rt va sz dev call buf ex, hst, bind_ok, hsc, bind_ok] + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UnmapS1JA.lean b/proofs/Fl4ProofsA64/P3/UnmapS1JA.lean new file mode 100644 index 000000000..100284d5d --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UnmapS1JA.lean @@ -0,0 +1,300 @@ +/- +P3 (aarch64, A64-P3 S3 gen 28, ruling 23:55Z items 1–3): **the spec runs and helpers of the l4v unmap arms** (the +caps whose mapping names no FL4 root, and the unmapped Stage-2 frame); the tops are in `P3/UnmapL4vJA`. The x86 +counterparts are `P3/VsUnmapDec`'s native unmap arms. + +* **The spec runs**: `bind_memA` / `assert_memA` (one outcome of a bind / an assertion); **`perfPgUnmapU_memA`** / + **`perfPtUnmapU_memA`** (l4v's `perform_pg_inv_unmap` / `perform_pt_inv_unmap` of an unmapped cap: the `None` arm + does nothing, then the slot's cap is read and rewritten (`getCap` / `setCap` hypotheses; the rewrite is of the + slot's own cap, which **`setCap_selfA`** shows changes nothing). Statements changed with the `return ()` → `pure ()` + spec fix (`spec-return-audit.md` rows 1–2): they used to prove only the assertion); **`perfPgUnmapM_memA`** / + **`perfPtUnmapM_memA`** (of a mapped cap: l4v's `unmap_page` / `unmap_page_table`, for the table the 512 + `store_pte`s and the cache clean, then the read and the write; `liftArch_memA`); **`perfS2UnmapU_memA`** (the Ext + `.pageUnmap .stage2` of an unmapped Stage-2 frame: the cap write only). +* **`setCap_selfA`** (every outcome of `set_cap` of the cap a slot holds is the start state) and + **`StateRelA.curA`** (a related slot's spec cap is FL4's abstract cap). +* **`JAtZAX.of_msA`**: a machine-state-only spec step keeps `JAtZAX`. **`setcap_coreA`**: after a step keeping the `OS` + slots, `ksb` and `cpu`, the slot's abstract cap is `getCap`'s value and the in-place write keeps `JAtZAX` and the + tree (`JAtZAX.of_setcapA`). **`unmap_postS1JA`**: the Stage-1 twin of `P3/UnmapTopJA`'s `unmap_postJA` — after an + `UnmapRelA` unmap whose spec side changes only heap and machine state (`OutKmA`), FL4 is `ok a` or the unmap's + flush, and some spec outcome is `JAtZAX`-related to the written state (`JAtZAX.of_s1ClearA`). +* **FL4's arms**: `page_unmapU_eqA` (an unmapped frame: restart, cap write), `pt_unmapU_eqA` (an unmapped final + table). + +No named premise. New module; no existing statement changed. +-/ +import Fl4ProofsA64.P3.UnmapDecJA +import Fl4ProofsA64.P3.FinVsJA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## The spec runs -/ + +section spec +open Fl4Spec + +/-- One outcome of a bind. -/ +theorem bind_memA {σ α β : Type} {m : NonDet σ α} {f : α → NonDet σ β} {s : σ} {x : α × σ} + (hx : x ∈ (m s).1) {r : β × σ} (hr : r ∈ (f x.1 x.2).1) : r ∈ ((m >>= f) s).1 := by + rw [nd_bind_applyA]; exact ⟨x, hx, hr⟩ + +theorem assert_memA {σ : Type} {P : Prop} (h : P) (s : σ) : ((), s) ∈ (NonDet.assert P s).1 := ⟨h, rfl⟩ + +variable {σ1 σu σz σ5 σ3 : Fl4SpecA64.State Unit} {p : Fl4SpecA64.ObjRef} {R : Fl4SpecA64.CapRights} + {sz : Fl4SpecA64.VMPageSize} {dev : Bool} {cte : Fl4SpecA64.CslotPtr} {ac : Fl4SpecA64.ArchCap} + {asid : Fl4SpecA64.Asid} {v : Fl4SpecA64.VSpaceRef} {t : Fl4SpecA64.PtType} + +/-- l4v's `perform_pg_inv_unmap` of an unmapped frame cap: the `None` arm does nothing, then the slot's cap is read +and rewritten with the map data cleared (a no-op on a slot holding the invoked cap: `setCap_selfA`). -/ +theorem perfPgUnmapU_memA (hg : (.archObjectCap ac, σ1) ∈ (Fl4SpecA64.getCap (Z := Unit) cte σ1).1) + (hs : ((), σ3) ∈ (Fl4SpecA64.setCap (Z := Unit) (.archObjectCap (Fl4SpecA64.updateMapData ac none)) cte σ1).1) : + (([] : List Fl4SpecA64.Data), σ3) ∈ + (Fl4SpecA64.performPageInvocation (Z := Unit) (.pageUnmap (.frameCap p R sz dev none) cte) σ1).1 := by + unfold Fl4SpecA64.performPageInvocation Fl4SpecA64.performPgInvUnmap + dsimp only [Fl4SpecA64.ArchCap.acapMapData] + refine bind_memA (x := ((), σ3)) ?_ (Set.mem_singleton _) + refine bind_memA (x := ((), σ1)) (assert_memA rfl σ1) ?_ + exact bind_memA hg hs + +/-- l4v's `perform_pg_inv_unmap` of a mapped frame cap: `unmap_page`, the slot's cap read, the cap write. -/ +theorem perfPgUnmapM_memA (hu : ((), σu) ∈ (Fl4SpecA64.unmapPage (Z := Unit) sz asid v p σ1).1) + (hg : (.archObjectCap ac, σu) ∈ (Fl4SpecA64.getCap (Z := Unit) cte σu).1) + (hs : ((), σ3) ∈ (Fl4SpecA64.setCap (Z := Unit) (.archObjectCap (Fl4SpecA64.updateMapData ac none)) cte σu).1) : + (([] : List Fl4SpecA64.Data), σ3) ∈ + (Fl4SpecA64.performPageInvocation (Z := Unit) (.pageUnmap (.frameCap p R sz dev (some (asid, v))) cte) σ1).1 := by + unfold Fl4SpecA64.performPageInvocation Fl4SpecA64.performPgInvUnmap + dsimp only [Fl4SpecA64.ArchCap.acapMapData, Fl4SpecA64.ArchCap.acapFsize, Fl4SpecA64.ArchCap.acapObj] + refine bind_memA (x := ((), σ3)) ?_ (Set.mem_singleton _) + refine bind_memA (x := ((), σ1)) (assert_memA rfl σ1) ?_ + exact bind_memA hu (bind_memA hg hs) + +/-- The cap read of `perform_pt_inv_unmap` (`liftM the_arch_cap (get_cap …)`). -/ +theorem liftArch_memA (hg : (.archObjectCap ac, σ1) ∈ (Fl4SpecA64.getCap (Z := Unit) cte σ1).1) : + (ac, σ1) ∈ ((NonDet.liftM Fl4SpecA64.Cap.theArchCap (Fl4SpecA64.getCap (Z := Unit) cte)) σ1).1 := by + unfold NonDet.liftM + exact ⟨(.archObjectCap ac, σ1), hg, Set.mem_singleton _⟩ + +/-- l4v's `perform_pt_inv_unmap` of an unmapped table cap: the `None` arm does nothing, then the slot's cap is read +and rewritten with the map data cleared (as `perfPgUnmapU_memA`). -/ +theorem perfPtUnmapU_memA (hg : (.archObjectCap ac, σ1) ∈ (Fl4SpecA64.getCap (Z := Unit) cte σ1).1) + (hs : ((), σ3) ∈ (Fl4SpecA64.setCap (Z := Unit) (.archObjectCap (Fl4SpecA64.updateMapData ac none)) cte σ1).1) : + ((), σ3) ∈ + (Fl4SpecA64.performPageTableInvocation (Z := Unit) (.pageTableUnmap (.pageTableCap p t none) cte) σ1).1 := by + have hl := liftArch_memA hg + unfold Fl4SpecA64.performPageTableInvocation Fl4SpecA64.performPtInvUnmap + dsimp only [Fl4SpecA64.ArchCap.acapMapData] + refine bind_memA (x := ((), σ1)) (assert_memA rfl σ1) ?_ + exact bind_memA hl hs + +/-- l4v's `perform_pt_inv_unmap` of a mapped table cap: `unmap_page_table`, the 512 `store_pte`s, the cache clean, +the slot's cap read, the cap write. -/ +theorem perfPtUnmapM_memA (hu : ((), σu) ∈ (Fl4SpecA64.unmapPageTable (Z := Unit) asid v p σ1).1) + (hz : ((), σz) ∈ (NonDet.mapMx (fun sl => Fl4SpecA64.storePte (Z := Unit) t sl .invalidPTE) + (Fl4SpecA64.uptoEnumStep p (p + ((1 : Fl4SpecA64.Word) <<< Fl4SpecA64.pteBits)) + (p + Fl4SpecA64.mask (Fl4SpecA64.ptBits t))) σu).1) + (hm : ((), σ5) ∈ (Fl4SpecA64.doMachineOp (Z := Unit) (Fl4SpecA64.cleanCacheRange_PoU p + (p + Fl4SpecA64.mask (Fl4SpecA64.ptBits t)) (Fl4SpecA64.addrFromPPtr p)) σz).1) + (hg : (.archObjectCap ac, σ5) ∈ (Fl4SpecA64.getCap (Z := Unit) cte σ5).1) {sc : Fl4SpecA64.Cap} + (hs : ((), σ3) ∈ (Fl4SpecA64.setCap (Z := Unit) sc cte σ5).1) + (hsc : sc = .archObjectCap (Fl4SpecA64.updateMapData ac none)) : + ((), σ3) ∈ (Fl4SpecA64.performPageTableInvocation (Z := Unit) + (.pageTableUnmap (.pageTableCap p t (some (asid, v))) cte) σ1).1 := by + subst hsc + have hl := liftArch_memA hg + unfold Fl4SpecA64.performPageTableInvocation Fl4SpecA64.performPtInvUnmap + dsimp only [Fl4SpecA64.ArchCap.acapMapData, Fl4SpecA64.ArchCap.acapObj, Fl4SpecA64.ArchCap.acapPtType] + refine bind_memA (x := ((), σ1)) (assert_memA rfl σ1) ?_ + exact bind_memA (x := (p, σ1)) (Set.mem_singleton _) (bind_memA hu (bind_memA (Set.mem_singleton _) + (bind_memA hz (bind_memA hm (bind_memA hl hs))))) + +/-- The Ext `.pageUnmap .stage2` of an unmapped Stage-2 frame cap: only the cap write (`extUnmappedFrame`). -/ +theorem perfS2UnmapU_memA + (hs : ((), σ3) ∈ (Fl4SpecA64.setCap (Z := Unit) (.archObjectCap (.frameCap p R sz dev none)) cte σ1).1) : + (([] : List Fl4SpecA64.Data), σ3) ∈ (Fl4SpecA64.performExtVSpace (Z := Unit) + (.pageUnmap .stage2 (.ext (.stage2FrameCap p R sz dev none)) cte) σ1).1 := by + unfold Fl4SpecA64.performExtVSpace + dsimp only + refine bind_memA hs ?_ + exact Set.mem_singleton _ + +/-- **`set_cap` of the cap a slot already holds changes nothing**: every outcome is the start state. -/ +theorem setCap_selfA {a : Fl4SpecA64.Cap} {sl : Fl4SpecA64.CslotPtr} {σ : Fl4SpecA64.State Unit} + {r : Unit × Fl4SpecA64.State Unit} + (hcn : ∀ sz cn, σ.kheap sl.1 = some (.cnode sz cn) → cn sl.2 = some a) + (htc : ∀ t, σ.kheap sl.1 = some (.tcb t) → Fl4SpecA64.tcbCnodeMap t sl.2 = some a) + (hr : r ∈ (Fl4SpecA64.setCap (Z := Unit) a sl σ).1) : r.2 = σ := by + obtain ⟨oref, cref⟩ := sl + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + obtain ⟨e1, e2⟩ := getObject_mem hx + obtain ⟨obj, σ1⟩ := x + simp only at e1 e2 hr hcn htc + have key : ∀ o, o = obj → + r ∈ ((Pure.pure o >>= fun o' => Fl4SpecA64.setObject (Z := Unit) oref o') σ1).1 → r.2 = σ := by + intro o ho h + obtain ⟨y, hy, h⟩ := mem_bindA.1 h + obtain rfl : y = (o, σ1) := hy + obtain ⟨o0, -, hr2⟩ := setObject_mem h + rw [hr2, e1, ho, ← e2, Function.update_eq_self] + have kfail : ∀ f : Fl4SpecA64.KernelObject → NonDet (Fl4SpecA64.State Unit) Unit, + r ∈ ((NonDet.fail >>= f) σ1).1 → False := by + intro f h + obtain ⟨y, hy, -⟩ := mem_bindA.1 h + exact absurd hy (Set.notMem_empty _) + cases obj with + | cnode sz cn => + have ha := hcn sz cn e2 + simp only at hr + split_ifs at hr + · exact key _ (by rw [Function.update_eq_self_iff.2 ha.symm]) hr + · exact (kfail _ hr).elim + | tcb t => + have ha := htc t e2 + simp only at hr + split_ifs at hr <;> + first + | exact (kfail _ hr).elim + | (refine key _ ?_ hr + subst_vars + simp [Fl4SpecA64.tcbCnodeMap, tcbIdx_eq, Fl4SpecA64.tcbStage2Slot, Fl4SpecA64.tcbHighVSpaceSlot] at ha + subst_vars + rfl) + | _ => + try simp only at hr + exact (kfail _ hr).elim + +/-- The spec slot of a related slot holds the abstract cap of FL4's (as `getCap` reads it, for `setCap_selfA`). -/ +theorem StateRelA.curA {th : TypedHeapA} {mem : U64 → U64} {σ : Fl4SpecA64.State Unit} + (hR : StateRelA th mem σ) {sl : Fl4SpecA64.CslotPtr} (hs : AbsSlotA th sl) : + ∃ c, CapEqA (capAtA mem (cslotAddrA sl)) c ∧ + (∀ sz cn, σ.kheap sl.1 = some (.cnode sz cn) → cn sl.2 = some c) ∧ + (∀ t, σ.kheap sl.1 = some (.tcb t) → Fl4SpecA64.tcbCnodeMap t sl.2 = some c) := by + obtain ⟨o, idx⟩ := sl + obtain ⟨p, e1, hc | ⟨ht, i, hi, e2⟩⟩ := hs + · simp only at e1 hc; subst e1 + obtain ⟨cs, hk, -, hr⟩ := hR.cnode p _ hc + obtain ⟨c, hc', hrel⟩ := hr idx rfl + refine ⟨c, hrel.2, fun sz cn h => ?_, fun t h => ?_⟩ + · rw [hk] at h; cases h; exact hc' + · rw [hk] at h; cases h + · simp only at e1 e2; subst e1 e2 + obtain ⟨x, hk, hr⟩ := hR.tcb p ht + obtain ⟨c, hc', hrel⟩ := hr i hi + have hrel' : CapRelA (capAtA mem (cslotAddrA (specPtr p, Fl4SpecA64.tcbCnodeIndex i))) c := by + unfold cslotAddrA; rw [ofBl_tcbIdx i (by unfold tcbSlotsA at hi; omega)]; exact hrel + refine ⟨c, hrel'.2, fun sz cn h => (by rw [hk] at h; cases h), fun t h => ?_⟩ + rw [hk] at h; cases h; exact hc' + +end spec + +/-! ## `JAtZAX` across a machine step, the cap write, a Stage-1 unmap -/ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} + +/-- **A machine-state-only spec step keeps `JAtZAX`.** -/ +theorem JAtZAX.of_msA (J : JAtZAX (L := L) AR Z s a σ m) {σ' : Fl4SpecA64.State Unit} (h : MsA σ σ') : + JAtZAX (L := L) AR Z s a σ' m := by + obtain ⟨ms, rfl⟩ := h + obtain ⟨HG, hc, ho⟩ := KmA.globA (σ := σ) (σ' := { σ with machineState := ms }) ⟨σ.kheap, ms, rfl⟩ + exact J.of_spec { toSpecGlobA := HG, kh := fun _ _ => rfl, ext := fun _ _ e _ => e, cdt := hc, orig := ho } + +variable {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} {ia ie : Nat} + +set_option maxHeartbeats 1000000 in +/-- **The cap write after a step that keeps the slots** (module docstring): the invoked slot's abstract cap is +`getCap`'s value, and the in-place write keeps `JAtZAX` and the tree. -/ +theorem setcap_coreA (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) {s2 : S} {σ2 : Fl4SpecA64.State Unit} + (J2 : JAtZAX (L := L) AR Z s2 a σ2 m) (hk : OSKeep st OS s s2) (hks : L.ksb s2 = L.ksb s) + (hcpu : L.cpu s2 = L.cpu s) (cte : Fl4SpecA64.CslotPtr) (hab : AbsSlotA Z.th cte) {c0 c : capability.Cap} + {sc : Fl4SpecA64.Cap} (hcap : capA st s (cslotAddrA cte) = c0) (hEnc : CapRt.EncWf c) (hca : CapRelA c sc) + (hua : UA c c0) (hok : CapObjOkA Z c) (hcn : c ≠ .Null) (hc0 : c0 ≠ .Null) + (hT : TreeA st OS Z.G.chains Z.G.par s) (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) : + ∃ s3 σ3, obj.Slot.set_cap st (cslotAddrA cte) s2 c = ok s3 ∧ + (∃ c', CapEqA c0 c' ∧ (c', σ2) ∈ (Fl4SpecA64.getCap (Z := Unit) cte σ2).1) ∧ + ((), σ3) ∈ (Fl4SpecA64.setCap (Z := Unit) sc cte σ2).1 ∧ + JAtZAX (L := L) AR Z s3 a σ3 m ∧ TreeA st OS Z.G.chains Z.G.par s3 := by + have hos : cslotAddrA cte ∈ OS := RS.sub _ (hab.isSlot E.j.1.place) + have hN := bxi_nodes E.sys.ent.bxi + have e0 : capA st s2 (cslotAddrA cte) = c0 := (hk.capA hos).trans hcap + obtain ⟨c', hg, -, hc'⟩ := StateRelA.getAbsA J2.1.st hab + have e1 : capAtA (L.mem s2) (cslotAddrA cte) = c0 := e0 + rw [e1] at hc' + obtain ⟨s3, σ3, hsc, hσ3, -, -, J3, T3⟩ := J2.of_setcapA E.sys.ent.os RS.sub + (footNOff_ksA hks hcpu (footN_offA E)) (footNAl_ksA hks hcpu (FootNA.aligned E.j.1 hN E.scAl E.ap.vcAl)) + cte hab hEnc hca (hT.keepO hk) (fun y hy hn => by rw [hk.capA hy]; exact hnul y hy hn) hcn + (by rw [e0]; exact hc0) (by rw [e0]; exact hua) hok + exact ⟨s3, σ3, hsc, ⟨c', hc', hg⟩, hσ3, J3, T3⟩ + +/-- **After a Stage-1 unmap** (module docstring; the Stage-1 twin of `unmap_postJA`). -/ +theorem unmap_postS1JA {fl : Result A} {u : Result (S × A)} {mm : Fl4SpecA64.SMonad Unit Unit} (hmm : OutKmA mm) + (J : JAtZAX (L := L) AR Z s a σ m) (hfa : ∀ w, FootA (L := L) Z s w → Aligned w) + (hTO : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s q) {T Ω Sg OS : Set U64} (hO : OSOK T Ω Sg OS) + (hpOS : VsPgOffOSA Z OS) (h : UnmapRelA Z.Pp Z.S1 Z.Lv Z.X Z.Y σ st s a fl u (mm σ)) : + ∃ (s2 : S) (fa : Result A) (σu : Fl4SpecA64.State Unit), (fa = ok a ∨ fa = fl) ∧ + u = (do let a1 ← fa; ok (s2, a1)) ∧ ((), σu) ∈ (mm σ).1 ∧ JAtZAX (L := L) AR Z s2 a σu m ∧ + OSKeep st OS s s2 ∧ L.ksb s2 = L.ksb s ∧ L.cpu s2 = L.cpu s := by + rcases UnmapRelA.extE J.1.vs J.1.ext h with ⟨hu, hW⟩ | + ⟨slot, ⟨p, hp, i, hi, rfl⟩, hu, ⟨-, ⟨⟨⟨⟩, σu⟩, hx0⟩, h3⟩, h4⟩ + · exact ⟨s, ok a, σ, Or.inl rfl, by rw [hu, bind_ok], by rw [hW]; exact Set.mem_singleton _, J, + fun y _ => ⟨rfl, rfl, rfl, rfl⟩, rfl, rfl⟩ + · have hal : Aligned (peAF p i) := peAF_alignedA (J.1.vs.al_mod hp) hi + exact ⟨_, fl, σu, Or.inr rfl, hu, hx0, + J.of_s1ClearA hfa hTO hp hi (hmm σ _ hx0) (h3 _ hx0).2 (h3 _ hx0).1 (h4 _ hx0), + osKeep_ofA hO fun w hw hin => by + rw [L.mem_wr _ _ _ _ hal hw, if_neg fun (e : peAF p i = w) => hpOS p i hi (Or.inr (Or.inr hp)) (e ▸ hin)], + L.ksb_wr _ _ _, L.cpu_wr _ _ _⟩ + +end + +/-! ## FL4's arms -/ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +set_option maxHeartbeats 400000 in +/-- FL4's `ARMPageUnmap` of an unmapped frame: the restart and the cap write. -/ +theorem page_unmapU_eqA {A : Type} (ar : machine.Arch A) (s : S) (a : A) (length : U64) (slot : obj.Slot) + (fp rt asid va : U64) (sz : capability.FrameSize) (dev isEpt call : Bool) (buf : U64) + (ex : Aeneas.Std.Array obj.Slot 3#usize) : + vspace.decode_frame st ar s a fl4_abi.label.ARMPageUnmap length slot (.Frame fp sz rt dev false isEpt asid va) + call buf ex = + (do let s1 ← sched.set_thread_state st s (L.curW s) sched.TS_RESTART + let s3 ← obj.Slot.set_cap st slot s1 (.Frame fp sz rt dev false false 0#u64 0#u64) + ok (core.result.Result.Ok (), s3, a)) := by + have hct : sched.cur_thread st s = ok (L.curW s) := cur_thread_ok s + have hne : ¬ fl4_abi.label.ARMPageUnmap = fl4_abi.label.ARMPageMap := by + unfold fl4_abi.label.ARMPageUnmap fl4_abi.label.ARMPageMap; decide + unfold vspace.decode_frame + simp only [vspace.as_frame, hct, bind_ok, hne, eq_self_iff_true, if_true, ↓reduceIte, vspace.unmapped_frame, + Bool.false_eq_true] + rfl + +set_option maxHeartbeats 400000 in +/-- FL4's `ARMPageTableUnmap` of a final unmapped table: the restart and the cap write. -/ +theorem pt_unmapU_eqA {A : Type} (ar : machine.Arch A) (s : S) (a : A) (length : U64) (slot : obj.Slot) + (ptr asid va buf : U64) (ex : Aeneas.Std.Array obj.Slot 3#usize) + (hf : cdt.is_final_capability st s slot = ok true) : + vspace.decode_page_table st ar s a fl4_abi.label.ARMPageTableUnmap length slot (.PageTable ptr false asid va) + buf ex = + (do let s1 ← sched.set_thread_state st s (L.curW s) sched.TS_RESTART + let s3 ← obj.Slot.set_cap st slot s1 (.PageTable ptr false 0#u64 0#u64) + ok (core.result.Result.Ok (), s3, a)) := by + have hct : sched.cur_thread st s = ok (L.curW s) := cur_thread_ok s + unfold vspace.decode_page_table + simp only [hct, hf, bind_ok, ↓reduceIte, Bool.false_eq_true] + rfl + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UnmapTopJA.lean b/proofs/Fl4ProofsA64/P3/UnmapTopJA.lean new file mode 100644 index 000000000..25d120b40 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UnmapTopJA.lean @@ -0,0 +1,279 @@ +/- +P3 (aarch64, A64-P3 S3 gen 25, ruling 22:14Z item 3 and Q3): **the whole-step `JAtZAX` tops of `ARMPageUnmap` and +`ARMPageTableUnmap` on an FL4 root** (`performExtVSpace` `.pageUnmap` / `.tableUnmap`, `P3/ExtVsPerfA`): the caller's +restart (`JEntA.restartA`), the Ext unmap lifted as `P3/FinVsXA`'s `JAtZAX.of_extClearA`, for a table the zeroing +(`P3/TabZeroA`'s `zero_relA` and `JAtZAX.of_zeroA`), then the cap write as `P3/SetCapJA`'s `JAtZAX.of_setcapA` — with +`JAtZAX` and the MDB tree after the whole step. The x86 counterparts are `EptCore.frameUnmapF` (`P3/EptFrameUnmap`) and +`P3/EptTableUnmapDec`. + +* **`unmap_postJA`**: after an `ExtUnmapRelE` unmap, FL4 is an arch step to a state with `JAtZAX` (the entry's, or + `JAtZAX.of_extClearA`'s), the `OS` slots and `ksb` / `cpu` kept (`osKeep_ofA`); `then_setcapA` the FL4 run shape. +* **`page_unmap_jA`** (the `ARMPageUnmap` top), **`pt_unmap_jA`** (the `ARMPageTableUnmap` top, with the zeroing). + +Lemma hypotheses (not premises): `VsPgOffOSA Z OS` — the words of the VSpace pages an Ext unmap or the zeroing writes +(Stage-2 pages, windows' level-1 pages, Stage-1 tables) are in no `OS` slot (A64 P1's `PtW` / `HeapOK` at integration, +as `SlotApA.vs` for `VsR` words); the new cap's `EncWf` / `CapRelA` / `UA` / `CapObjOkA` (as gen 22's +`restart_setcapJA`), the table's membership (`Z.T2`, or `Z.S1` at depth ≥ 1). Decode-site hypotheses: `TabOffA`, +`ExtTabOffA`. No named premise beyond `S2IpaE` [P] (read by the unmaps). No existing statement changed. + +**S3 gen 26 (ruling Q4): the arch state after the tops is FL4's returned one.** The unmaps' continuation is a flush of +the entry's arch state (`P3/ExtVsTopA`'s `FlushE` / `ArchFlE`), `FlushE.arA` keeps `AR` across it under `ArchKeepsA` +[P] (`P3/VsMapJA`, extended by the four Ext flushes), so **changed statements (forced by Q4)**: `page_unmap_jA` / +`pt_unmap_jA` take `K : ArchKeepsA ar AR` and conclude `fa ⦃ a' => JAtZAX AR Z s3 a' σ3 m ⦄` (was `JAtZAX` at the +entry's `a`); `unmap_postJA` takes `ArchFlE ar a fk` and also gives `FlushE ar a fa`. + +**S3 gen 28**: the table zeroing after an unmap is factored out as **`zero_postA`** (`zero_relA`, `JAtZAX.of_zeroA`, +the `OS` slots kept), shared by `pt_unmap_jA` (proof only, statement unchanged) and the gen-29 mapped Stage-1 table top. + +**S3 gen 29 (ruling 00:45Z items 3–4). Changed statements**: `page_unmap_jA` / `pt_unmap_jA` also conclude +`CarryA Z (L.mem s) σ → CarryA Z (L.mem s3) σ3` (`P3/CarryA`: `PtCapInA` and `FinInvA` kept — the `OS` slots kept, +the slot's write `CapFinEqA` to the old cap and no mapped table cap, the spec side `TyKeepA` by +`perfExtPgUnmap_tyA` / `perfExtPtUnmap_tyA`). +-/ +import Fl4ProofsA64.P3.TabZeroA +import Fl4ProofsA64.P3.SetCapJA +import Fl4ProofsA64.P3.VsMapJA +import Fl4ProofsA64.P3.CarryA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-- **The VSpace pages' words are in no slot** (lemma hypothesis, module docstring). -/ +def VsPgOffOSA (Z : XSetsA) (OS : Set U64) : Prop := + ∀ q j : U64, j.val < 512 → (q ∈ S2PgE Z.X Z.T2 ∨ (∃ y ∈ Z.Y, q = wa y 4096#u64) ∨ q ∈ Z.S1) → + ¬ InOS OS (peAF q j) + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} + +/-- A step keeping every aligned word of the `OS` slots keeps them (`OSKeep`). -/ +theorem osKeep_ofA {T Ω Sg OS : Set U64} (hO : OSOK T Ω Sg OS) {s' : S} + (hm : ∀ w, Aligned w → InOS OS w → L.mem s' w = L.mem s w) : OSKeep st OS s s' := fun y hy => by + obtain ⟨v8, v16, v24⟩ := vals hO hy + exact ⟨hm _ (hO.al0 hy) ⟨y, hy, Nat.le_refl _, by omega⟩, + hm _ (hO.al hy (k := 8#u64) (by simp) (by simp)) ⟨y, hy, by omega, by omega⟩, + hm _ (hO.al hy (k := 16#u64) (by simp) (by simp)) ⟨y, hy, by omega, by omega⟩, + hm _ (hO.al hy (k := 24#u64) (by simp) (by simp)) ⟨y, hy, by omega, by omega⟩⟩ + +/-- (gen 26, ruling Q4) **A flush keeps the arch relation** (`ArchKeepsA` [P]). -/ +theorem FlushE.arA {ar : machine.Arch A} (K : ArchKeepsA ar AR) {fa : Result A} (h : FlushE ar a fa) {hw : HwRootA} + (ha : AR a hw) : fa ⦃ a' => AR a' hw ⦄ := by + unfold FlushE at h + rcases h with rfl | ⟨x, y, rfl⟩ | ⟨x, rfl⟩ | ⟨x, rfl⟩ | ⟨x, rfl⟩ + · exact (WP.spec_ok _).2 ha + · exact K.invS2Page a hw x y ha + · exact K.invS2Vmid a hw x ha + · exact K.invHiPage a hw x ha + · exact K.invHiVs a hw x ha + +/-- **After an Ext unmap** (module docstring): FL4's unmap is an arch step `fa` (gen 26: a flush of `a`, `FlushE`) +returning a state `s2` (the entry's, or one zero write into a VSpace page), with `JAtZAX` at `(s2, σu)`, the `OS` +slots and `ksb` / `cpu` kept. -/ +theorem unmap_postJA {ar : machine.Arch A} {fk : S → Result (S × A)} (hfk : ArchFlE ar a fk) {u : Result (S × A)} + {σu : Fl4SpecA64.State Unit} (J : JAtZAX (L := L) AR Z s a σ m) + (hfa : ∀ w, FootA (L := L) Z s w → Aligned w) (hTO : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s q) + (hEO : ExtTabOffA (L := L) Z m s) {T Ω Sg OS : Set U64} (hO : OSOK T Ω Sg OS) (hpOS : VsPgOffOSA Z OS) + (h : ExtUnmapRelE Z.Pp Z.S1 Z.Lv Z.X Z.Y Z.T2 Z.Sh2 σ st s a fk u ({((), σu)}, False)) : + ∃ (s2 : S) (fa : Result A), FlushE ar a fa ∧ u = (do let a1 ← fa; ok (s2, a1)) ∧ JAtZAX (L := L) AR Z s2 a σu m ∧ + OSKeep st OS s s2 ∧ L.ksb s2 = L.ksb s ∧ L.cpu s2 = L.cpu s := by + rcases h with ⟨hu, hW⟩ | ⟨w, σ', hu, hW, hR, hE, hw⟩ + · obtain rfl := sing_stA hW + exact ⟨s, ok a, Or.inl rfl, by rw [hu, bind_ok], J, fun y _ => ⟨rfl, rfl, rfl, rfl⟩, rfl, rfl⟩ + · obtain rfl := sing_stA hW + have J1 := J.of_extClearA hfa hTO hEO hw hR hE + obtain ⟨⟨q, j, hj, rfl, hq4, hq⟩, -⟩ := hw + have hal' : Aligned (peAF q j) := peAF_alignedA (Nat.mod_eq_zero_of_dvd hq4) hj + obtain ⟨fa, hfl, hfa'⟩ := hfk (L.wr s (peAF q j) 0#u64) + exact ⟨_, fa, hfl, hu.trans hfa', J1, osKeep_ofA hO fun w' hw' hin => by + rw [L.mem_wr _ _ _ _ hal' hw', if_neg fun (e : peAF q j = w') => hpOS q j hj hq (e ▸ hin)], + L.ksb_wr _ _ _, L.cpu_wr _ _ _⟩ + +/-- FL4's unmap, then the cap write. -/ +theorem then_setcapA {u : Result (S × A)} {fa : Result A} {s2 s3 : S} (hu : u = (do let a1 ← fa; ok (s2, a1))) + {x : U64} {c : capability.Cap} (hsc : obj.Slot.set_cap st x s2 c = ok s3) : + (do let (s2', a1) ← u + let s3' ← obj.Slot.set_cap st x s2' c + ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s3', a1)) = + (do let a1 ← fa; ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s3, a1)) := by + rw [hu] + simp only [bind_tc_eq, Std.bind_assoc, bind_ok, hsc] + +set_option maxHeartbeats 2000000 in +/-- **The `ARMPageUnmap` top on an FL4 root** (module docstring; x86 `EptCore.frameUnmapF`): the restart, the Ext +unmap (`P3/ExtVsPerfA`'s `perform_pgUnmapXA`) and the cap write of the unmapped frame, with `JAtZAX` and the tree +after (for every root kind `k` the decoder's `extKindOfAsid` gives, at the restart's state). -/ +theorem page_unmap_jA {ar : machine.Arch A} (K : ArchKeepsA ar AR) {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} {ia ie : Nat} + (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) + (hTO : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s q) (hEO : ExtTabOffA (L := L) Z m s) (hpOS : VsPgOffOSA Z OS) + (hK : Aligned (L.ksb s)) (hT : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s)) (hIpa : S2IpaE ar a) + (length buf : U64) (call : Bool) (ex : Aeneas.Std.Array obj.Slot 3#usize) + (cte : Fl4SpecA64.CslotPtr) (hab : AbsSlotA Z.th cte) + (fp rt asid va : U64) (sz : capability.FrameSize) (dev isEpt : Bool) (h0 : asid ≠ 0#u64) + (h16 : asid.val < 65536) (hVa : isEpt = false → VaOkE va) (Rt : Fl4SpecA64.CapRights) + (hcap : capA st s (cslotAddrA cte) = .Frame fp sz rt dev true isEpt asid va) + (hEnc : CapRt.EncWf (.Frame fp sz rt dev false false 0#u64 0#u64)) + (hca : CapRelA (.Frame fp sz rt dev false false 0#u64 0#u64) (.archObjectCap (Fl4SpecA64.extUnmappedFrame + (absFrameE isEpt (specPtr fp) Rt (frameSizeOfA sz) dev (some (BitVec.ofNat 16 asid.val, va.bv)))))) + (hua : UA (.Frame fp sz rt dev false false 0#u64 0#u64) (.Frame fp sz rt dev true isEpt asid va)) + (hok : CapObjOkA Z (.Frame fp sz rt dev false false 0#u64 0#u64)) : + ∃ x, σ.kheap σ.curThread = some (.tcb x) ∧ + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + ∀ k : Fl4SpecA64.ExtRootKind, (isEpt = false → Fl4SpecA64.asidRootKind (stUpdA σ σ.curThread x .restart) + (BitVec.ofNat 16 asid.val) = some k ∧ k ≠ .stage1) → + TreeA st OS Z.G.chains Z.G.par s → (∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) → + ∃ (s3 : S) (σ3 : Fl4SpecA64.State Unit) (fa : Result A), + vspace.decode_frame st ar s a fl4_abi.label.ARMPageUnmap length (cslotAddrA cte) + (.Frame fp sz rt dev true isEpt asid va) call buf ex = + (do let a1 ← fa; ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s3, a1)) ∧ + (([] : List Fl4SpecA64.Data), σ3) ∈ (Fl4SpecA64.performExtVSpace (Z := Unit) (.pageUnmap k + (absFrameE isEpt (specPtr fp) Rt (frameSizeOfA sz) dev (some (BitVec.ofNat 16 asid.val, va.bv))) cte) + (stUpdA σ σ.curThread x .restart)).1 ∧ + fa ⦃ a' => JAtZAX (L := L) AR Z s3 a' σ3 m ⦄ ∧ TreeA st OS Z.G.chains Z.G.par s3 ∧ + (CarryA Z (L.mem s) σ → CarryA Z (L.mem s3) σ3) := by + have hos : cslotAddrA cte ∈ OS := RS.sub _ (hab.isSlot E.j.1.place) + obtain ⟨⟨Rp, Nt, extra, sp, P⟩, Y, hrun, hcur, hvs, hvc, hsub⟩ := RS + obtain ⟨x, hkx, hst, hset, E1, -, hOK, -, -⟩ := E.restartA P Y hrun hcur hvs hvc + refine ⟨x, hkx, hset, fun k hk hTr hnul => ?_⟩ + have hks : L.ksb (L.wr s (stA (L.curW s)) sched.TS_RESTART) = L.ksb s := L.ksb_wr _ _ _ + have hcpu : L.cpu (L.wr s (stA (L.curW s)) sched.TS_RESTART) = L.cpu s := L.cpu_wr _ _ _ + generalize L.wr s (stA (L.curW s)) sched.TS_RESTART = s1 at hst E1 hOK hks hcpu + have hN := bxi_nodes E1.sys.ent.bxi + obtain ⟨σu, fk, hfk, hperf, hU⟩ := perform_pgUnmapXA (ar := ar) (a := a) E1.j.1.vs E1.j.1.ext + (by rw [hks]; exact hK) (by rw [hks]; exact hT) E1.j.2.2.1.obj hIpa h16 sz va fp isEpt hVa Rt dev k hk cte + obtain ⟨s2, fa, hfl, hu, J2, hk2, hks2, hcpu2⟩ := unmap_postJA hfk E1.j (FootA.aligned E1.j.1 hN E1.scAl E1.ap.vcAl) + (fun q hq => (hTO q hq).of_ksA hks hcpu) (hEO.of_ksA hks hcpu) E1.sys.ent.os hpOS hU + have hk12 := hOK.trans' hk2 + obtain ⟨s3, σ3, hsc, hσ3, -, -, J3, T3⟩ := J2.of_setcapA E1.sys.ent.os hsub + (footNOff_ksA hks2 hcpu2 (footN_offA E1)) (footNAl_ksA hks2 hcpu2 (FootNA.aligned E1.j.1 hN E1.scAl E1.ap.vcAl)) + cte hab hEnc hca (hTr.keepO hk12) (fun y hy hn => by rw [hk12.capA hy]; exact hnul y hy hn) + (fun h => by cases h) (by rw [hk12.capA hos, hcap]; exact fun h => by cases h) (by rw [hk12.capA hos, hcap]; exact hua) + hok + refine (fun hP => ⟨s3, σ3, fa, ?_, hP, WP.spec_mono (hfl.arA K J3.1.ar) fun _ h => J3.of_arA h, T3, + fun C => C.of_setcapA E1.sys.ent.os hsub hTr hk12 hos hEnc hsc (fun _ _ _ h => by cases h) + (by show CapFinEqA _ (capA st s _); rw [hcap]; exact CapFinEqA.frameA _ _ _ _ _ _ _ _) + ((TyKeepA.stUpdA hkx _).trans (perfExtPgUnmap_tyA _ _ _ _ _ hP))⟩) ?_ + all_goals first + | (rw [page_unmap_eqA ar s a length (cslotAddrA cte) fp rt asid va sz dev isEpt call buf ex h0, hst, bind_ok] + exact then_setcapA hu hsc) + | (rw [hperf] + exact ⟨((), σ3), hσ3, rfl⟩) + +/-- FL4's unmap then the zeroing of the table `p`. -/ +theorem then_zeroA {u : Result (S × A)} {fa : Result A} {s2 : S} (hu : u = (do let a1 ← fa; ok (s2, a1))) (p : U64) : + (do let (s3, a2) ← u + let s4 ← st.zero s3 p 4096#u64 + ok (s4, a2)) = (do let a1 ← fa; ok (L.zr s2 p 4096#u64, a1)) := by + rw [hu] + simp only [bind_tc_eq, Std.bind_assoc, bind_ok, L.zero_eq] + +/-- (S3 gen 28) **The table zeroing after an unmap**: FL4's `zero(p, 4096)` against the spec's 512 `store_pte`s +(`zero_relA`), `JAtZAX` after (`JAtZAX.of_zeroA`), the `OS` slots, `ksb` and `cpu` kept. Shared by `pt_unmap_jA` and +`P3/UnmapS1JA`'s `pt_unmapS1_decJA`. -/ +theorem zero_postA {s2 : S} {σu : Fl4SpecA64.State Unit} (J2 : JAtZAX (L := L) AR Z s2 a σu m) + (hfa : ∀ w, FootA (L := L) Z s2 w → Aligned w) (hTO : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s2 q) + (hEO : ExtTabOffA (L := L) Z m s2) (hK : Aligned (L.ksb s2)) (hT : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s2)) + {T Ω Sg OS : Set U64} (hO : OSOK T Ω Sg OS) (hpOS : VsPgOffOSA Z OS) {ptr : U64} + (hp : (ptr ∈ Z.S1 ∧ 1 ≤ Z.Lv ptr) ∨ ptr ∈ Z.T2) : + ∃ t', Fl4Spec.NonDet.mapMx (fun sl => Fl4SpecA64.storePte (Z := Unit) .normalPtT sl .invalidPTE) + (Fl4SpecA64.uptoEnumStep (specPtr ptr) (specPtr ptr + ((1 : Fl4SpecA64.Word) <<< Fl4SpecA64.pteBits)) + (specPtr ptr + Fl4SpecA64.mask (Fl4SpecA64.ptBits .normalPtT))) σu = + ({((), ptSetA σu (specPtr ptr) t')}, False) ∧ + JAtZAX (L := L) AR Z (L.zr s2 ptr 4096#u64) a (ptSetA σu (specPtr ptr) t') m ∧ + OSKeep st OS s2 (L.zr s2 ptr 4096#u64) ∧ L.ksb (L.zr s2 ptr 4096#u64) = L.ksb s2 ∧ + L.cpu (L.zr s2 ptr 4096#u64) = L.cpu s2 := by + have hp4 : ptr.val % 4096 = 0 := by + rcases hp with ⟨h, -⟩ | h + · exact J2.1.vs.al_mod h + · exact Nat.mod_eq_zero_of_dvd (J2.1.ext.tal ptr h) + obtain ⟨t', -, -, hrun0, -, VR, ER⟩ := zero_relA J2.1.vs J2.1.ext hK hT hp + refine ⟨t', hrun0, J2.of_zeroA hfa hTO hEO (by rcases hp with ⟨h, -⟩ | h; exact Or.inl h; exact Or.inr h) hp4 VR ER, + osKeep_ofA hO fun w hw hin => ?_, L.ksb_zr _ _ _, L.cpu_zr _ _ _⟩ + rw [zr_wrsA hp4 s2 hw, mem_wrsA hp4 hw _ (fun j h => List.mem_range.1 h), if_neg] + rintro ⟨j, hj, e⟩ + have hj' := List.mem_range.1 hj + exact hpOS ptr (ujA j) (by rw [ujA_val hj']; exact hj') + (by rcases hp with ⟨h, -⟩ | h; exact Or.inr (Or.inr h); exact Or.inl (Or.inl h)) (e ▸ hin) + +set_option maxHeartbeats 4000000 in +/-- **The `ARMPageTableUnmap` top on an FL4 root** (module docstring; x86 `P3/EptTableUnmapDec`): the restart, the Ext +unmap (`P3/ExtVsPerfA`'s `perform_tabUnmapXA`), the zeroing of the table (`zero_relA`, `JAtZAX.of_zeroA`) and the +cap write of the unmapped table cap, with `JAtZAX` and the tree after. -/ +theorem pt_unmap_jA {ar : machine.Arch A} (K : ArchKeepsA ar AR) {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} {ia ie : Nat} + (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) + (hTO : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s q) (hEO : ExtTabOffA (L := L) Z m s) (hpOS : VsPgOffOSA Z OS) + (hK : Aligned (L.ksb s)) (hT : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s)) (hIpa : S2IpaE ar a) + (length buf : U64) (ex : Aeneas.Std.Array obj.Slot 3#usize) (cte : Fl4SpecA64.CslotPtr) (hab : AbsSlotA Z.th cte) + (ptr asid va : U64) (h16 : asid.val < 65536) (hp : (ptr ∈ Z.S1 ∧ 1 ≤ Z.Lv ptr) ∨ ptr ∈ Z.T2) + (hf : cdt.is_final_capability st s (cslotAddrA cte) = ok true) + (hcap : capA st s (cslotAddrA cte) = .PageTable ptr true asid va) + (hEnc : CapRt.EncWf (.PageTable ptr false 0#u64 0#u64)) + (hca : CapRelA (.PageTable ptr false 0#u64 0#u64) (.archObjectCap (Fl4SpecA64.updateMapData + (.pageTableCap (specPtr ptr) .normalPtT (some (BitVec.ofNat 16 asid.val, va.bv))) none))) + (hua : UA (.PageTable ptr false 0#u64 0#u64) (.PageTable ptr true asid va)) + (hok : CapObjOkA Z (.PageTable ptr false 0#u64 0#u64)) : + ∃ x, σ.kheap σ.curThread = some (.tcb x) ∧ + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + ∀ k : Fl4SpecA64.ExtRootKind, + Fl4SpecA64.asidRootKind (stUpdA σ σ.curThread x .restart) (BitVec.ofNat 16 asid.val) = some k → k ≠ .stage1 → + TreeA st OS Z.G.chains Z.G.par s → (∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) → + ∃ (s3 : S) (σ3 : Fl4SpecA64.State Unit) (fa : Result A), + vspace.decode_page_table st ar s a fl4_abi.label.ARMPageTableUnmap length (cslotAddrA cte) + (.PageTable ptr true asid va) buf ex = + (do let a1 ← fa; ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s3, a1)) ∧ + (([] : List Fl4SpecA64.Data), σ3) ∈ (Fl4SpecA64.performExtVSpace (Z := Unit) (.tableUnmap k + (.pageTableCap (specPtr ptr) .normalPtT (some (BitVec.ofNat 16 asid.val, va.bv))) cte) + (stUpdA σ σ.curThread x .restart)).1 ∧ + fa ⦃ a' => JAtZAX (L := L) AR Z s3 a' σ3 m ⦄ ∧ TreeA st OS Z.G.chains Z.G.par s3 ∧ + (CarryA Z (L.mem s) σ → CarryA Z (L.mem s3) σ3) := by + have hos : cslotAddrA cte ∈ OS := RS.sub _ (hab.isSlot E.j.1.place) + obtain ⟨⟨Rp, Nt, extra, sp, P⟩, Y, hrun, hcur, hvs, hvc, hsub⟩ := RS + obtain ⟨x, hkx, hst, hset, E1, -, hOK, -, -⟩ := E.restartA P Y hrun hcur hvs hvc + refine ⟨x, hkx, hset, fun k hk hk1 hTr hnul => ?_⟩ + have hks : L.ksb (L.wr s (stA (L.curW s)) sched.TS_RESTART) = L.ksb s := L.ksb_wr _ _ _ + have hcpu : L.cpu (L.wr s (stA (L.curW s)) sched.TS_RESTART) = L.cpu s := L.cpu_wr _ _ _ + generalize L.wr s (stA (L.curW s)) sched.TS_RESTART = s1 at hst E1 hOK hks hcpu + have hN := bxi_nodes E1.sys.ent.bxi + have hO := E1.sys.ent.os + obtain ⟨σu, fk, hfk, hperf, hU⟩ := perform_tabUnmapXA (ar := ar) (a := a) E1.j.1.vs E1.j.1.ext + (by rw [hks]; exact hK) (by rw [hks]; exact hT) E1.j.2.2.1.obj hIpa h16 va ptr k hk hk1 cte + obtain ⟨s2, fa, hfl, hu, J2, hk2, hks2, hcpu2⟩ := unmap_postJA hfk E1.j (FootA.aligned E1.j.1 hN E1.scAl E1.ap.vcAl) + (fun q hq => (hTO q hq).of_ksA hks hcpu) (hEO.of_ksA hks hcpu) hO hpOS hU + have hks2' : L.ksb s2 = L.ksb s := hks2.trans hks + have hcpu2' : L.cpu s2 = L.cpu s := hcpu2.trans hcpu + have hN2 : ∀ c ∈ Z.Cs, ∀ o : U64, o.val < 4288 → o.val % 8 = 0 → Aligned (L.nodeAt s2 c o) := + fun c hc o h1 h2 => by rw [nodeAt_ksb hks2]; exact hN c hc o h1 h2 + obtain ⟨t', hrun0, J4, hk3, -, -⟩ := zero_postA J2 (FootA.aligned J2.1 hN2 E1.scAl E1.ap.vcAl) + (fun q hq => (hTO q hq).of_ksA hks2' hcpu2') (hEO.of_ksA hks2' hcpu2') (by rw [hks2']; exact hK) + (by rw [hks2']; exact hT) hO hpOS hp + have hks4 : L.ksb (L.zr s2 ptr 4096#u64) = L.ksb s1 := (L.ksb_zr _ _ _).trans hks2 + have hcpu4 : L.cpu (L.zr s2 ptr 4096#u64) = L.cpu s1 := (L.cpu_zr _ _ _).trans hcpu2 + have hk14 := (hOK.trans' hk2).trans' hk3 + obtain ⟨s5, σ5, hsc, hσ5, -, -, J5, T5⟩ := J4.of_setcapA hO hsub + (footNOff_ksA hks4 hcpu4 (footN_offA E1)) (footNAl_ksA hks4 hcpu4 (FootNA.aligned E1.j.1 hN E1.scAl E1.ap.vcAl)) + cte hab hEnc hca (hTr.keepO hk14) (fun y hy hn => by rw [hk14.capA hy]; exact hnul y hy hn) + (fun h => by cases h) (by rw [hk14.capA hos, hcap]; exact fun h => by cases h) + (by rw [hk14.capA hos, hcap]; exact hua) hok + refine (fun hP => ⟨s5, σ5, fa, ?_, hP, WP.spec_mono (hfl.arA K J5.1.ar) fun _ h => J5.of_arA h, T5, + fun C => C.of_setcapA hO hsub hTr hk14 hos hEnc hsc (fun _ _ _ h => by cases h) + (by show CapFinEqA _ (capA st s _); rw [hcap]; exact CapFinEqA.ptA _ _ _ _) + ((TyKeepA.stUpdA hkx _).trans (perfExtPtUnmap_tyA _ _ _ _ _ hP))⟩) ?_ + all_goals first + | (rw [pt_unmap_eqA ar s a length (cslotAddrA cte) ptr asid va buf ex hf, hst, bind_ok] + exact then_setcapA (then_zeroA hu ptr) hsc) + | (rw [hperf, nd_bind_oneD hrun0] + exact ⟨((), σ5), hσ5, rfl⟩) + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtApA.lean b/proofs/Fl4ProofsA64/P3/UtApA.lean new file mode 100644 index 000000000..75e03e3a3 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtApA.lean @@ -0,0 +1,316 @@ +/- +P3 (aarch64, A64-P3 Untyped, full-record post, discharge): **`UserOK` and the slot apartness `ap` after a +creation**, from A64 P1's creation end (`S1aRtIn`) and the record before it. + +* `utRt_userA`: no record clause places the ghost user words `U` against the untyped regions (at `w1`, `U` is a + whole untyped), so the record after a creation takes the words of `U` still off its objects (`utUserA`). `U` has no + lower bound in the record. +* `utRt_apA`: `sca` from the grown `SlotPlaced` (the creation column of `utRt_j7TopA`), `irqSt` / `vcAl` / `vc` + from the creation; `vs` and `ext` from a split of + their words into old ones off the hole and new ones in the window. + +`hH1 : H1 = H`: the heap the creation starts from is the record's (no reset, or the record on the dropped heap). + +No named premise. New theorems only. +-/ +import Fl4ProofsA64.P3.UtFullJ7A +import Fl4ProofsA64.P3.Stage3A +import Fl4ProofsA64.P1.BootScAp +import Fl4ProofsA64.P1.S1aRtCol +import Fl4ProofsA64.P1.S1aTcbN + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Classical + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- The user words that are still off the grown heap's objects. -/ +def utUserA (H' : Heap) (U : Set U64) : Set U64 := + {b | b ∈ U ∧ (∀ t ∈ H'.tcbs, b.val < t.val ∨ t.val + 2 ^ 12 ≤ b.val) ∧ b ∉ H'.omega ∧ + ∀ e, (H' e = some .ep ∨ H' e = some .ntfn) → b.val + 8 ≤ e.val ∨ e.val + 16 ≤ b.val} + +theorem utRt_userA {H H' : Heap} {Cs U : Set U64} {s s' : S} + (h : Fl4ProofsA64.P1.UserOK st H.tcbs Cs H.omega H U s) (hk : L.ksb s' = L.ksb s) : + Fl4ProofsA64.P1.UserOK st H'.tcbs Cs H'.omega H' (utUserA H' U) s' := by + intro b hb + have o := (h b hb.1).1.ksb (s' := s') hk + exact ⟨⟨o.1, hb.2.1, o.2.2⟩, hb.2.2.1, hb.2.2.2⟩ + +variable {H H1 H' : Heap} {Cs OS OS1 U0 Ac : Set U64} {s s1 q : S} {ia ie lo hi : Nat} + {slot p sb fi aligned nt us dest off win : U64} {dev : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (E : S1aRtIn st H Cs OS U0 Ac s ia ie slot p sb fi dev extra H1 H' OS1 s1 q lo hi aligned nt us dest off win) +include E + +/-- A word that is off the old slots, old TCBs and the hole, or a word of a new object that is no TCB and no CNode, +is off the grown slots and TCBs. -/ +theorem utRt_wordApA (hH1 : H1 = H) (SP : SlotPlaced H OS) {b : U64} + (hb : ((¬ InOS OS b ∧ (∀ t ∈ H.tcbs, b.val < t.val ∨ t.val + 4096 ≤ b.val)) ∧ + (dev = false → ¬ (aligned.val ≤ b.val ∧ b.val < aligned.val + win.val * 2 ^ (zA nt.val us).val))) ∨ + (dev = false ∧ aligned.val ≤ b.val ∧ b.val < aligned.val + win.val * 2 ^ (zA nt.val us).val ∧ + nt.val ≠ 1 ∧ nt.val ≠ 4)) : + ¬ InOS (s1aRtSlots OS1 dev nt us aligned win) b ∧ + ∀ t ∈ H'.tcbs, b.val < t.val ∨ t.val + 4096 ≤ b.val := by + have U := E.creation + have hfit : aligned.val + win.val * 2 ^ (zA nt.val us).val ≤ 2 ^ 64 := le_trans U.A.aw U.A.pf + -- every object of the record's heap is off the hole + have hole : ∀ r k, H r = some k → r.val + 2 ^ k.bits ≤ lo ∨ hi ≤ r.val := fun r k h => + U.hole r k (by rw [hH1]; exact h) + -- a new TCB lies in the window + have newT : ∀ t, H' t = some .tcb → H t = some .tcb ∨ + (dev = false ∧ nt.val = 1 ∧ aligned.val ≤ t.val ∧ + t.val + 4096 ≤ aligned.val + win.val * 2 ^ (zA nt.val us).val) := by + intro t ht + rcases U.ex t _ ht with old | ⟨hd, j, hj, e, hk⟩ + · exact Or.inl (by rw [← hH1]; exact old) + · have hbits := newKind_bitsA hk U.A.hn E.kept.us64 + have h1 : nt.val = 1 := newKind_tcb hk + obtain ⟨hv, hle⟩ := bj_facts hj hfit + have z12 : 2 ^ (zA nt.val us).val = 4096 := by rw [← hbits]; rfl + refine Or.inr ⟨hd, h1, ?_, ?_⟩ + · rw [e, hv]; omega + · rw [e]; rw [z12] at hle ⊢; exact hle + rcases hb with ⟨⟨nos, ntcb⟩, out⟩ | ⟨hd, b1, b2, n1, n4⟩ + · constructor + · rintro ⟨y, (hy | hy), y1, y2⟩ + · exact nos ⟨y, U.os y hy, y1, y2⟩ + · obtain ⟨hd, j, hj, i, -, hyv, -, hend, h32⟩ := newSl_in E.kept hfit hy + exact out hd ⟨by omega, by omega⟩ + · intro t ht + rcases newT t ht with old | ⟨hd, -, t1, t2⟩ + · exact ntcb t old + · have := out hd + omega + · have w := U.whR hd + constructor + · rintro ⟨y, (hy | hy), y1, y2⟩ + · obtain ⟨-, r, k, hr, r1, r2, -, -⟩ := SP y (U.os y hy) + rcases hole r k hr with g | g <;> omega + · obtain ⟨-, j, -, i, hi, -⟩ := newSl_in E.kept hfit hy + rcases hi with ⟨h, -⟩ | ⟨h, -⟩ + · exact n1 h + · exact n4 h + · intro t ht + rcases newT t ht with old | ⟨-, h, -, -⟩ + · have := hole t _ old + have e : HKind.bits .tcb = 12 := rfl + rw [e] at this + omega + · exact absurd h n1 + +/-- **`SlotApA` after a creation.** `F`: the set split of `utRt_j7SplitA`; `ram12`: only a RAM untyped makes VCPUs. -/ +theorem utRt_apA {Z Z' : XSetsA} (hH1 : H1 = H) (J : SlotApA Z H OS (L.ksb s)) (SP : SlotPlaced H OS) + (SP' : SlotPlaced H' (s1aRtSlots OS1 dev nt us aligned win)) (hH : HeapOK H) (vh : VcH H Z.V) + (ptH : PtHA H (Z.Pp ∪ Z.S1)) (sets : SetsInHA Z H) (ksw : (L.ksb s).val + 788736 ≤ 2 ^ 64) + (F : UtJ7SplitA st Z Z' (s1aRtSlots OS1 dev nt us aligned win) s q slot p sb nt us aligned win.val) + (ram12 : nt.val = 12 → 0 < win.val → dev = false) : + SlotApA Z' H' (s1aRtSlots OS1 dev nt us aligned win) (L.ksb s) := by + have U := E.creation + have hH' := E.heapOK + have hfit : aligned.val + win.val * 2 ^ (zA nt.val us).val ≤ 2 ^ 64 := le_trans U.A.aw U.A.pf + have hole : ∀ r k, H r = some k → r.val + 2 ^ k.bits ≤ lo ∨ hi ≤ r.val := fun r k h => + U.hole r k (by rw [hH1]; exact h) + have b12 : HKind.bits (.other 12) = 12 := rfl + -- a word of an object of the record's heap is off the window + have objOff : ∀ (r : U64) k, H r = some k → ∀ b : U64, r.val ≤ b.val → b.val < r.val + 2 ^ k.bits → + dev = false → ¬ (aligned.val ≤ b.val ∧ b.val < aligned.val + win.val * 2 ^ (zA nt.val us).val) := by + intro r k hr b b1 b2 hd + have w := U.whR hd + rcases hole r k hr with g | g <;> omega + have vsp : ∀ v ∈ Z'.V, v ∈ Z.V ∨ (dev = false ∧ nt.val = 12 ∧ ∃ j < win.val, v = bjA aligned (zA nt.val us) j) := by + intro v hv + rcases F.v v hv with ⟨old, -⟩ | ⟨h12, j, hj, e⟩ + · exact Or.inl old + · exact Or.inr ⟨ram12 h12 (by omega), h12, j, hj, e⟩ + -- a word of a new table page + have newW : ∀ x ∈ UtSchedNewA nt us aligned win.val, ∀ b : U64, x.val ≤ b.val → b.val < x.val + 4096 → + dev = false ∧ aligned.val ≤ b.val ∧ b.val < aligned.val + win.val * 2 ^ (zA nt.val us).val ∧ + nt.val ≠ 1 ∧ nt.val ≠ 4 := by + intro x hx b b1 b2 + obtain ⟨hd, a1, a2, -⟩ := utEnd_newPageA E hx + have ty := (utSchedNew_typeA hx).1 + exact ⟨hd, by omega, by omega, by omega, by omega⟩ + have newX : ∀ x ∈ UtSchedNewA nt us aligned win.val, x.val + 4096 ≤ 2 ^ 64 := fun x hx => by + obtain ⟨-, -, a2, -⟩ := utEnd_newPageA E hx + omega + -- a word of an old 4 KiB page or 8 KiB root + have pageW : ∀ r : U64, (∃ (q : U64) (z : Nat), H q = some (.other z) ∧ q.val ≤ r.val ∧ r.val + 4096 ≤ q.val + 2 ^ z) → + r.val + 4096 ≤ 2 ^ 64 ∧ ∀ b : U64, r.val ≤ b.val → b.val < r.val + 4096 → dev = false → + ¬ (aligned.val ≤ b.val ∧ b.val < aligned.val + win.val * 2 ^ (zA nt.val us).val) := by + rintro r ⟨o, z, ho, h1, h2⟩ + have f := hH.fits o _ ho + have e : HKind.bits (.other z) = z := rfl + rw [e] at f + exact ⟨by omega, fun b b1 b2 => objOff o _ ho b (by omega) (by rw [e]; omega)⟩ + have rootW : ∀ r ∈ Z.X ∪ Z.Y, r.val + 8192 ≤ 2 ^ 64 ∧ ∀ b : U64, r.val ≤ b.val → b.val < r.val + 8192 → + dev = false → ¬ (aligned.val ≤ b.val ∧ b.val < aligned.val + win.val * 2 ^ (zA nt.val us).val) := by + intro r hr + obtain ⟨o, z, ho, h1, h2⟩ := sets.roots r hr + have f := hH.fits o _ ho + have e : HKind.bits (.other z) = z := rfl + rw [e] at f + exact ⟨by omega, fun b b1 b2 => objOff o _ ho b (by omega) (by rw [e]; omega)⟩ + have hiV : ∀ r : U64, r.val + 8192 ≤ 2 ^ 64 → (wa r 4096#u64).val = r.val + 4096 := fun r h => by + rw [wa_val] + exact Nat.mod_eq_of_lt (by simp; omega) + have vsW : ∀ b, VsFootA Z'.Pp Z'.S1 Z'.Y (L.ksb s) b → + (VsFootA Z.Pp Z.S1 Z.Y (L.ksb s) b ∧ + (dev = false → ¬ (aligned.val ≤ b.val ∧ b.val < aligned.val + win.val * 2 ^ (zA nt.val us).val))) ∨ + (dev = false ∧ aligned.val ≤ b.val ∧ b.val < aligned.val + win.val * 2 ^ (zA nt.val us).val ∧ + nt.val ≠ 1 ∧ nt.val ≠ 4) := by + intro b hb + cases hb with + | asid i hi => + refine Or.inl ⟨.asid i hi, fun hd => ?_⟩ + have v := atAF_valA (kb := L.ksb s) ksw hi + have ks := utEnd_ksRegionA E hd + have pa := U.A.pa + have aw := U.A.aw + rcases ks with g | g <;> omega + | pool r l hr hl => + have old := (F.pp r hr).1 + obtain ⟨fit, off⟩ := pageW r (ptH r (Or.inl old)) + have v := peAF_valA fit hl + exact Or.inl ⟨.pool r l old hl, off _ (by omega) (by omega)⟩ + | pt r i hr hi => + rcases F.s1 r hr with ⟨old, -⟩ | new + · obtain ⟨fit, off⟩ := pageW r (ptH r (Or.inr old)) + have v := peAF_valA fit hi + exact Or.inl ⟨.pt r i old hi, off _ (by omega) (by omega)⟩ + · have v := peAF_valA (newX r new) hi + exact Or.inr (newW r new _ (by omega) (by omega)) + | high y i hy hi => + rcases F.y y hy with ⟨old, -⟩ | ⟨-, new⟩ + · obtain ⟨fit, off⟩ := rootW y (Or.inr old) + have e := hiV y fit + have v := peAF_valA (p := wa y 4096#u64) (by omega) hi + exact Or.inl ⟨.high y i old hi, off _ (by omega) (by omega)⟩ + · have v := peAF_valA (newX _ new) hi + exact Or.inr (newW _ new _ (by omega) (by omega)) + have exW : ∀ b, ExtFootE Z'.X Z'.Y Z'.T2 b → + (ExtFootE Z.X Z.Y Z.T2 b ∧ + (dev = false → ¬ (aligned.val ≤ b.val ∧ b.val < aligned.val + win.val * 2 ^ (zA nt.val us).val))) ∨ + (dev = false ∧ aligned.val ≤ b.val ∧ b.val < aligned.val + win.val * 2 ^ (zA nt.val us).val ∧ + nt.val ≠ 1 ∧ nt.val ≠ 4) := by + intro b hb + cases hb with + | s2lo r i hr hi => + rcases F.x r hr with ⟨old, -⟩ | ⟨new, -⟩ + · obtain ⟨fit, off⟩ := rootW r (Or.inl old) + have v := peAF_valA (p := r) (by omega) hi + exact Or.inl ⟨.s2lo r i old hi, off _ (by omega) (by omega)⟩ + · have v := peAF_valA (newX r new) hi + exact Or.inr (newW r new _ (by omega) (by omega)) + | s2hi r i hr hi => + rcases F.x r hr with ⟨old, -⟩ | ⟨-, new⟩ + · obtain ⟨fit, off⟩ := rootW r (Or.inl old) + have e := hiV r fit + have v := peAF_valA (p := wa r 4096#u64) (by omega) hi + exact Or.inl ⟨.s2hi r i old hi, off _ (by omega) (by omega)⟩ + · have v := peAF_valA (newX _ new) hi + exact Or.inr (newW _ new _ (by omega) (by omega)) + | s2pt t i ht hi => + rcases F.t2 t ht with ⟨old, -⟩ | new + · obtain ⟨fit, off⟩ := pageW t (sets.pages t (Or.inl (Or.inr old))) + have v := peAF_valA fit hi + exact Or.inl ⟨.s2pt t i old hi, off _ (by omega) (by omega)⟩ + · have v := peAF_valA (newX t new) hi + exact Or.inr (newW t new _ (by omega) (by omega)) + | wl0 y hy => + have h511 : (511#u64 : U64).val < 512 := by simp + have e511 : (511#u64 : U64).val = 511 := rfl + rcases F.y y hy with ⟨old, -⟩ | ⟨new, -⟩ + · obtain ⟨fit, off⟩ := rootW y (Or.inr old) + have v := peAF_valA (p := y) (by omega) h511 + exact Or.inl ⟨.wl0 y old, off _ (by omega) (by omega)⟩ + · have v := peAF_valA (newX y new) h511 + exact Or.inr (newW y new _ (by omega) (by omega)) + | wlo y i hy hi => + have hlt : i.val < 512 := by omega + rcases F.y y hy with ⟨old, -⟩ | ⟨new, -⟩ + · obtain ⟨fit, off⟩ := rootW y (Or.inr old) + have v := peAF_valA (p := y) (by omega) hlt + exact Or.inl ⟨.wlo y i old hi, off _ (by omega) (by omega)⟩ + · have v := peAF_valA (newX y new) hlt + exact Or.inr (newW y new _ (by omega) (by omega)) + -- every VCPU of the grown set is a 4 KiB object of the grown heap + have obj : ∀ v ∈ Z'.V, H' v = some (.other 12) := by + intro v hv + rcases vsp v hv with old | ⟨hd, h12, j, hj, e⟩ + · exact U.grow v _ (by rw [hH1]; exact vh v old) + · rw [e] + exact U.hn hd j hj _ (by rw [h12]; rfl) + have al : ∀ v ∈ Z'.V, v.val % 4096 = 0 ∧ v.val + 4096 ≤ 2 ^ 64 := fun v hv => by + have a := hH'.align v _ (obj v hv) + have f := hH'.fits v _ (obj v hv) + rw [b12] at a f + exact ⟨a, f⟩ + have word : ∀ v ∈ Z'.V, ∀ b, (b = v ∨ b = wa v 24#u64) → + ¬ InOS (s1aRtSlots OS1 dev nt us aligned win) b ∧ ∀ t ∈ H'.tcbs, b.val < t.val ∨ t.val + 4096 ≤ b.val := by + intro v hv b hb + have bv : v.val ≤ b.val ∧ b.val < v.val + 4096 := by + rcases hb with e | e + · rw [e]; omega + · have : b.val = v.val + 24 := by + rw [e, wa_val] + exact Nat.mod_eq_of_lt (by have := (al v hv).2; simp; omega) + omega + refine utRt_wordApA E hH1 SP ?_ + rcases vsp v hv with old | ⟨hd, h12, j, hj, e⟩ + · refine Or.inl ⟨⟨(J.vc v old b hb).1, fun t ht => ?_⟩, + objOff v _ (vh v old) b bv.1 (by rw [b12]; exact bv.2)⟩ + · have ne : t ≠ v := fun e => by + have h : H t = some .tcb := ht + rw [e, vh v old] at h + cases h + have d := hH.disj t _ v _ ht (vh v old) ne + have e1 : HKind.bits .tcb = 12 := rfl + rw [e1, b12] at d + omega + · obtain ⟨jv, hle⟩ := bj_facts hj hfit + have hk : newKind nt.val us.val = some (.other 12) := by rw [h12]; rfl + have z12 : 2 ^ (zA nt.val us).val = 4096 := by + rw [← newKind_bitsA hk U.A.hn E.kept.us64, b12]; rfl + rw [← e] at jv hle + rw [z12] at hle jv + exact Or.inr ⟨hd, by omega, by rw [z12]; omega, by omega, by omega⟩ + refine + { sca := ScApart.ofPlaced hH' SP' + irqSt := fun o o1 o2 y hy => ?irq + vcAl := fun v hv => by + have := (al v hv).1 + show v.val % 8 = 0 + omega + vc := fun v hv b hb => ?vc + vs := fun b hb => utRt_wordApA E hH1 SP ((vsW b hb).imp (fun g => ⟨J.vs b g.1, g.2⟩) id) + ext := fun b hb => utRt_wordApA E hH1 SP ((exW b hb).imp (fun g => ⟨J.ext b g.1, g.2⟩) id) } + case irq => + rcases hy with hy | hy + · exact J.irqSt o o1 o2 y (U.os y hy) + · obtain ⟨hd, j, hj, i, -, hyv, -, hend, h32⟩ := newSl_in E.kept hfit hy + have ks := utEnd_ksRegionA E hd + have pa := U.A.pa + have aw := U.A.aw + have ov : (wa (L.ksb s) o).val = (L.ksb s).val + o.val := by + rw [wa_val] + exact Nat.mod_eq_of_lt (by omega) + rintro ⟨a, c⟩ + rcases ks with g | g <;> omega + case vc => + obtain ⟨h1, h2⟩ := word v hv b hb + refine ⟨h1, fun ⟨t, ht, K, hK, e⟩ => ?_⟩ + have f := hH'.fits t _ ht + have e1 : HKind.bits .tcb = 12 := rfl + rw [e1] at f + have : b.val = t.val + K.val := by + rw [e, wa_val] + exact Nat.mod_eq_of_lt (by omega) + rcases h2 t ht with g | g <;> omega + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtApCaps2A.lean b/proofs/Fl4ProofsA64/P3/UtApCaps2A.lean new file mode 100644 index 000000000..6817079b6 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtApCaps2A.lean @@ -0,0 +1,273 @@ +/- +P3 (aarch64, A64-P3 Untyped, full-record post, discharge): **the cap-split bundle `UtAsmCapsInA` at the creation +start**, with no side hypothesis. `utRt_capsInA` states it from A64 P1's creation end at the decode's state; here it +comes from the record at the creation's start, the creation's end facts there (`CrtE`, the sharp write frame, the new +free index) and the grown heap's facts, so it is stated at the same state as the IPC, scheduler and spec bundles. + +* `utRt_objNewA`: a joint object of the grown record is an old one, or lies in the creation's window. +* `utRt_capsAllA`: the bundle, with the slot set `OS ∪ NewSl`. + +No named premise. New theorems only. +-/ +import Fl4ProofsA64.P3.UtAsmCapsA +import Fl4ProofsA64.P3.UtApSpec2A +import Fl4ProofsA64.P3.UtFullJ5A +import Fl4ProofsA64.P3.UtFullCapTyA +import Fl4ProofsA64.P1.MdbUtW + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +set_option maxRecDepth 100000 + +/-- A new object of a table, root or VCPU type has `2 ^ zA` bytes. -/ +theorem utZa_pageA {nt us : U64} (hn : nt.val < 15) (hus : us.val < 64) : + (nt.val = 8 ∨ nt.val = 11 ∨ nt.val = 12 → (zA nt.val us).val = 12) ∧ + (nt.val = 13 ∨ nt.val = 14 → (zA nt.val us).val = 13) := by + rw [zA_valA nt us hn hus] + refine ⟨fun h => ?_, fun h => ?_⟩ + · rcases h with h | h | h <;> simp [zOfA, h] + · rcases h with h | h <;> simp [zOfA, h] + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H H' : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s s' : S} {a a' : A} {σ σ' : State Unit} {m m' : McState} {x p sb fi nfi : U64} + {nt us aligned dest off win : U64} {G' : MdbGhostA} {Tn Cn : Set U64} + +/-- **The joint objects of the grown record** (module docstring). -/ +theorem utRt_objNewA (hn : nt.val < 15) (hus : us.val < 64) + (hfit : aligned.val + win.val * 2 ^ (zA nt.val us).val ≤ 2 ^ 64) + (hex : ∀ q k, H' q = some k → H q = some k ∨ + ∃ j < win.val, q = bjA aligned (zA nt.val us) j ∧ newKind nt.val us.val = some k) + (q : U64) (bytes : Nat) + (h : JointObjectA (utRtZA Z G' nt us aligned win.val Tn Cn (UtOfTyA 12 nt us aligned win.val)) H' q bytes) : + JointObjectA Z H q bytes ∨ + (nt.val ≠ 0 ∧ aligned.val ≤ q.val ∧ q.val + bytes ≤ aligned.val + win.val * 2 ^ (zA nt.val us).val) := by + have inW : ∀ j < win.val, aligned.val ≤ (bjA aligned (zA nt.val us) j).val ∧ + (bjA aligned (zA nt.val us) j).val + 2 ^ (zA nt.val us).val ≤ + aligned.val + win.val * 2 ^ (zA nt.val us).val := by + intro j hj + have h1' : (j + 1) * 2 ^ (zA nt.val us).val ≤ win.val * 2 ^ (zA nt.val us).val := Nat.mul_le_mul_right _ hj + rw [Nat.add_mul, Nat.one_mul] at h1' + have e : (bjA aligned (zA nt.val us) j).val = aligned.val + j * 2 ^ (zA nt.val us).val := bjA_val (by + have := Nat.two_pow_pos (zA nt.val us).val + omega) + omega + have new : nt.val ≠ 0 → q ∈ utBasesA aligned (zA nt.val us) win.val → bytes = 2 ^ (zA nt.val us).val → + nt.val ≠ 0 ∧ aligned.val ≤ q.val ∧ q.val + bytes ≤ aligned.val + win.val * 2 ^ (zA nt.val us).val := by + rintro h0 ⟨j, hj, rfl⟩ rfl + exact ⟨h0, inW j hj⟩ + have Z4 := (utZa_pageA hn hus).1 + have Z8 := (utZa_pageA hn hus).2 + rcases h with ⟨eb, h⟩ | ⟨eb, h⟩ | ⟨kind, h, eb⟩ | ⟨k, h, eb⟩ + · rcases h with h | h | h | h | h + · exact Or.inl (Or.inl ⟨eb, Or.inl h⟩) + · rcases h with h | ⟨ty, g⟩ + · exact Or.inl (Or.inl ⟨eb, Or.inr (Or.inl h)⟩) + refine Or.inr (new (by omega) g ?_) + rw [eb, Z4 (by omega)] + norm_num + · exact Or.inl (Or.inl ⟨eb, Or.inr (Or.inr (Or.inl h))⟩) + · rcases h with h | ⟨ty, g⟩ + · exact Or.inl (Or.inl ⟨eb, Or.inr (Or.inr (Or.inr (Or.inl h)))⟩) + refine Or.inr (new (by omega) g ?_) + rw [eb, Z4 (by omega)] + norm_num + · exact Or.inl (Or.inl ⟨eb, Or.inr (Or.inr (Or.inr (Or.inr h)))⟩) + · rcases h with h | h + · rcases h with h | ⟨ty, g⟩ + · exact Or.inl (Or.inr (Or.inl ⟨eb, Or.inl h⟩)) + refine Or.inr (new (by omega) g ?_) + rw [eb, Z8 (Or.inl ty)] + norm_num + · rcases h with h | ⟨ty, g⟩ + · exact Or.inl (Or.inr (Or.inl ⟨eb, Or.inr h⟩)) + refine Or.inr (new (by omega) g ?_) + rw [eb, Z8 (Or.inr ty)] + norm_num + · rcases utRtTh_splitA h with ⟨-, o⟩ | ⟨g, rfl⟩ + · exact Or.inl (Or.inr (Or.inr (Or.inl ⟨kind, o, eb⟩))) + obtain ⟨-, g0, -, j, hj, e⟩ := g + exact Or.inr (new g0 ⟨j, hj, e⟩ (eb.trans (utKind_sizeA hn g0 hus))) + · rcases hex q k h with o | ⟨j, hj, e, hk⟩ + · exact Or.inl (Or.inr (Or.inr (Or.inr ⟨k, o, eb⟩))) + have g0 : nt.val ≠ 0 := fun z => by + rw [z] at hk + simp [newKind] at hk + exact Or.inr (new g0 ⟨j, hj, e⟩ (by rw [eb, newKind_bitsA hk hn hus])) + +/-- **The cap-split bundle of a RAM creation at its start** (module docstring). -/ +theorem utRt_capsAllA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (hx : x ∈ OS) (hc : capA st s x = .Untyped p sb fi false) + (W : UtWinZA nt aligned (zA nt.val us) win.val) + (h1 : p.val + fi.val ≤ aligned.val) + (h2 : aligned.val + win.val * 2 ^ (zA nt.val us).val ≤ p.val + 2 ^ sb.val) + (pos : 0 < win.val) (us39 : nt.val = 0 → us.val ≤ 39) + (hn : nt.val < 15) (hus : us.val < 64) + (Ec : CrtE st H H.frame OS s s' x p sb false aligned nt us dest off win) + (w : WritesIn st s s' (UtCrtWA st H OS s nt aligned (zA nt.val us) win.val)) + -- the new free index + (hnv : nfi.val = aligned.val + win.val * 2 ^ (zA nt.val us).val - p.val) + (cap' : capA st s' x = .Untyped p sb nfi false) + -- the grown heap + (hgrow : ∀ q k, H q = some k → H' q = some k) + (hex : ∀ q k, H' q = some k → H q = some k ∨ + ∃ j < win.val, q = bjA aligned (zA nt.val us) j ∧ newKind nt.val us.val = some k) + (hnew : ∀ j < win.val, ∀ k, newKind nt.val us.val = some k → H' (bjA aligned (zA nt.val us) j) = some k) + -- the core post + (K : JAtZAX (L := L) AR (utRtZA Z G' nt us aligned win.val Tn Cn (UtOfTyA 12 nt us aligned win.val)) + s' a' σ' m') + -- the new objects are default objects of the spec heap + (newNull : ∀ y ∈ NewSl false nt us aligned win, y ∉ OS → capA st s' y = .Null) : + UtAsmCapsInA st Z (utRtZA Z G' nt us aligned win.val Tn Cn (UtOfTyA 12 nt us aligned win.val)) H H' OS + (OS ∪ NewSl false nt us aligned win) s s' x p sb fi nfi nt us aligned win.val := by + have _ := K + refine + { slots := fun u hu => ?_ + W := W + src := hx + cap := hc + cap' := cap' + lo := h1 + hi := by omega + top := h2 + ne := Nat.mul_pos pos (Nat.two_pow_pos _) + us39 := us39 + heap := hgrow + tcbs := fun t ht => hgrow t _ ht + vcpuW := fun t ht => utRt_footKeptA J hx hc h1 h2 w + (.tcb t _ (by rw [← J.tcbs]; exact ht) (Or.inr (Or.inr (Or.inr (Or.inl rfl))))) + obj := fun q bytes h => utRt_objNewA hn hus W.fit hex q bytes h + nodes := fun lo hi h => by + refine ⟨fun c hc' => ?_, ?_⟩ + · have e : L.nodeBase s' c = L.nodeBase s c := by + unfold LawfulStore.nodeBase + rw [w.1] + rw [e] + exact h.1 c hc' + · rw [w.1] + exact h.2 + newKind := fun j hj => by + refine utNewCap_kindA (fun g => hnew j hj _ ?_) (fun g => hnew j hj _ ?_) (fun g => hnew j hj _ ?_) + (fun g => hnew j hj _ ?_) <;> (rw [g]; rfl) } + by_cases ex : u = x + · exact Or.inr (Or.inl ⟨ex, nfi, by rw [ex]; exact cap'⟩) + by_cases ed : ∃ j < win.val, u = djA dest off j + · obtain ⟨j, hj, e⟩ := ed + exact Or.inr (Or.inr (Or.inl ⟨j, hj, by rw [e]; exact Ec.new j hj⟩)) + by_cases uo : u ∈ OS + · exact Or.inl ⟨uo, Ec.keep u uo ex (fun j hj e => ed ⟨j, hj, e⟩)⟩ + rcases hu with hu | hu + · exact absurd hu uo + exact Or.inr (Or.inr (Or.inr (newNull u hu uo))) + +/-- **The new slots are null after the creation**: they are slots of default objects, which the core post relates. -/ +theorem utRt_newNullA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (hx : x ∈ OS) (hc : capA st s x = .Untyped p sb fi false) + (W : UtWinZA nt aligned (zA nt.val us) win.val) + (h1 : p.val + fi.val ≤ aligned.val) + (h2 : aligned.val + win.val * 2 ^ (zA nt.val us).val ≤ p.val + 2 ^ sb.val) + (hn : nt.val < 15) (hus : us.val < 64) + {t : ApiObjectType} (htt : objTypeOfA nt.val = some t) + (K : JAtZAX (L := L) AR (utRtZA Z G' nt us aligned win.val Tn Cn (UtOfTyA 12 nt us aligned win.val)) + s' a' σ' m') + (O : ∀ j < win.val, t ≠ .untyped → + σ'.kheap (specPtr (bjA aligned (zA nt.val us) j)) = some (defaultObject t false us.val σ.curDomain)) : + ∀ y ∈ NewSl false nt us aligned win, capA st s' y = .Null := by + rintro y ⟨-, j, hj, i, g, yv⟩ + have thFree : ∀ q, UtNewA false nt us aligned win.val q → Z.th q = none := utRt_ipcFreeA J hx hc W h1 h2 + have bv : (bjA aligned (zA nt.val us) j).val = aligned.val + j * 2 ^ (zA nt.val us).val ∧ + aligned.val + j * 2 ^ (zA nt.val us).val + 2 ^ (zA nt.val us).val ≤ + aligned.val + win.val * 2 ^ (zA nt.val us).val := by + have h1' : (j + 1) * 2 ^ (zA nt.val us).val ≤ win.val * 2 ^ (zA nt.val us).val := Nat.mul_le_mul_right _ hj + rw [Nat.add_mul, Nat.one_mul] at h1' + have fit := W.fit + exact ⟨bjA_val (by have := Nat.two_pow_pos (zA nt.val us).val; omega), by omega⟩ + have fit := W.fit + have tn : t ≠ .untyped := fun z => by + have n0 : nt.val = 0 := by + rw [z] at htt + revert htt + unfold objTypeOfA archDataToObjType + intro htt + interval_cases hv : nt.val <;> simp at htt <;> rfl + omega + have obj := O j hj tn + -- the slot's address + have addr : 32 * i + 32 ≤ 2 ^ (zA nt.val us).val → + y = slotAddrA (specPtr (bjA aligned (zA nt.val us) j)) (BitVec.ofNat 64 i) := by + intro hi + apply UScalar.eq_of_val_eq + rw [slotAddrA_valN (by omega)] + exact yv + rcases g with ⟨n1, i7⟩ | ⟨n4, ir⟩ + · have ez : (zA nt.val us).val = 12 := by rw [n1]; exact zA_1 us + have ey := addr (by rw [ez]; omega) + have th : utRtThA Z.th false nt us aligned win.val (bjA aligned (zA nt.val us) j) = some .tcb := by + rw [utRtTh_newA ⟨rfl, by omega, by omega, j, hj, rfl⟩] + unfold utKindA + rw [ite_eq_left n1] + obtain ⟨z, hz, sl⟩ := K.1.st.tcb _ th + have et : t = .tcbObject := by + rw [n1] at htt + simpa [objTypeOfA, eq_comm] using htt + rw [obj, et] at hz + obtain rfl : defaultTcb σ.curDomain = z := KernelObject.tcb.inj (Option.some.inj hz) + obtain ⟨ac, e, rel⟩ := sl i (by unfold tcbSlotsA; omega) + have en : ac = .nullCap := by + refine utDefaultObject_capA (t := .tcbObject) (by simp) (dev := false) (n := us.val) (d := σ.curDomain) + (b := tcbCnodeIndex i) ?_ + exact e + rw [en] at rel + rw [ey] + exact utCapRel_nullA rel + · have ez : (zA nt.val us).val = us.val + 5 := by + rw [zA_valA nt us hn hus]; simp [zOfA, n4, Nat.add_comm] + have hi : 32 * i + 32 ≤ 2 ^ (zA nt.val us).val := by rw [ez, pow_add]; omega + have ey := addr hi + have th : utRtThA Z.th false nt us aligned win.val (bjA aligned (zA nt.val us) j) = some (.cnode us.val) := by + rw [utRtTh_newA ⟨rfl, by omega, by omega, j, hj, rfl⟩] + unfold utKindA + rw [ite_eq_right (by omega), ite_eq_left n4] + obtain ⟨cs, hz, -, sl⟩ := K.1.st.cnode _ _ th + have et : t = .capTableObject := by + rw [n4] at htt + simpa [objTypeOfA, eq_comm] using htt + rw [obj, et] at hz + have ecs : emptyCnode us.val = cs := by + injection (Option.some.inj hz) + have hr : us.val < 64 := hus + have i64 : i < 2 ^ 64 := by + have : 2 ^ us.val ≤ 2 ^ 64 := Nat.pow_le_pow_right (by norm_num) (by omega) + omega + have hl : (natToCref us.val i).length = us.val := natToCref_length_utA us.val i (by omega) + obtain ⟨ac, e, rel⟩ := sl (natToCref us.val i) hl + have en : ac = .nullCap := by + rw [← ecs] at e + unfold emptyCnode at e + rw [ite_eq_left hl] at e + exact (Option.some.inj e).symm + rw [en] at rel + have ea : slotAddrA (specPtr (bjA aligned (zA nt.val us) j)) (ofBl (natToCref us.val i)) = + slotAddrA (specPtr (bjA aligned (zA nt.val us) j)) (BitVec.ofNat 64 i) := by + have h := cslotAddr_windowA (bjA aligned (zA nt.val us) j) us.val (by omega) i (by omega) + unfold cslotAddrA at h + exact h + rw [ea, ← ey] at rel + exact utCapRel_nullA rel + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtApCapsA.lean b/proofs/Fl4ProofsA64/P3/UtApCapsA.lean new file mode 100644 index 000000000..7c27a3fa3 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtApCapsA.lean @@ -0,0 +1,112 @@ +/- +P3 (aarch64, A64-P3 Untyped, full-record post, discharge): **the cap-split bundle `UtAsmCapsInA`** of a RAM +creation, from A64 P1's creation end (`S1aRtIn`). Discharged here: the slot split (new CNode / TCB slots are null), +the source's cap before and after, the window's top, the heap growth and the new caps' heap kinds. Left as +hypotheses: the window arithmetic (`W`, `lo`, `hi`, `ne`, `us39`), the old TCBs' VCPU words, the joint objects and +the nodes. + +No named premise. New theorems only. +-/ +import Fl4ProofsA64.P3.UtAsmCapsA +import Fl4ProofsA64.P3.UtFullJ7A +import Fl4ProofsA64.P1.MdbGrowN + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Classical + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {Z Z' : XSetsA} {H H1 H' : Heap} {Cs OS OS1 U0 Ac : Set U64} {s s1 q : S} {ia ie lo hi : Nat} + {slot p sb fi nfi aligned nt us dest off win : U64} {dev : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + +theorem utRt_capsInA + (E : S1aRtIn st H Cs OS U0 Ac s ia ie slot p sb fi dev extra H1 H' OS1 s1 q lo hi aligned nt us dest off win) + (hH1 : H1 = H) (ram : dev = false) (ksw : (L.ksb s).val + 788736 ≤ 2 ^ 64) + (cap' : capA st q slot = .Untyped p sb nfi false) + (W : UtWinZA nt aligned (zA nt.val us) win.val) + (hlo : p.val + fi.val ≤ aligned.val) + (hhi : aligned.val + win.val * 2 ^ (zA nt.val us).val ≤ p.val + nfi.val) + (ne : 0 < win.val * 2 ^ (zA nt.val us).val) (us39 : nt.val = 0 → us.val ≤ 39) + (vcpuW : ∀ t ∈ H.tcbs, L.mem q (wa t 3664#u64) = L.mem s (wa t 3664#u64)) + (obj : ∀ r bytes, JointObjectA Z' H' r bytes → JointObjectA Z H r bytes ∨ + (nt.val ≠ 0 ∧ aligned.val ≤ r.val ∧ r.val + bytes ≤ aligned.val + win.val * 2 ^ (zA nt.val us).val)) + (nodes : ∀ lo hi, RegionOffNodes st Z.Cs s lo hi → RegionOffNodes st Z'.Cs q lo hi) : + UtAsmCapsInA st Z Z' H H' OS (s1aRtSlots OS1 dev nt us aligned win) s q slot p sb fi nfi nt us aligned + win.val := by + have U := E.creation + have kb : L.ksb q = L.ksb s := E.ksb + have F := U.freshN E.kept (by rw [kb]; exact ksw) + have hk : ∀ j < win.val, CapKindA H' (newCapA nt.val (bjA aligned (zA nt.val us) j) us false) := by + intro j hj + have key : ∀ k, newKind nt.val us.val = some k → H' (bjA aligned (zA nt.val us) j) = some k := + U.hn ram j hj + refine utNewCap_kindA (fun h => key _ ?_) (fun h => key _ ?_) (fun h => key _ ?_) (fun h => key _ ?_) <;> + (rw [h]; rfl) + refine + { slots := fun u hu => ?_ + W := W + src := E.place.src + cap := by rw [← ram]; exact E.sourceCap + cap' := cap' + lo := hlo + hi := hhi + top := U.A.aw + ne := ne + us39 := us39 + heap := fun r k h => U.grow r k (by rw [hH1]; exact h) + tcbs := fun t ht => U.grow t _ (by rw [hH1]; exact ht) + vcpuW := vcpuW + obj := obj + nodes := nodes + newKind := hk } + rcases hu with hu | hu + · by_cases es : u = slot + · exact Or.inr (Or.inl ⟨es, nfi, by rw [es]; exact cap'⟩) + by_cases ed : ∃ j, j < win.val ∧ u = djA dest off j + · obtain ⟨j, hj, e⟩ := ed + refine Or.inr (Or.inr (Or.inl ⟨j, hj, ?_⟩)) + rw [e, U.E.new j hj, ram] + · refine Or.inl ⟨U.os u hu, ?_⟩ + rw [U.E.keep u hu es (fun j hj e => ed ⟨j, hj, e⟩), U.k1 u hu es] + · exact Or.inr (Or.inr (Or.inr (F.null u hu))) + +/-- **The window arithmetic** of a creation with `reset = false` (decode arguments `UtArgs`). -/ +theorem utRt_capsWinA + (E : S1aRtIn st H Cs OS U0 Ac s ia ie slot p sb fi dev extra H1 H' OS1 s1 q lo hi aligned nt us dest off win) + (hU : UtArgs st s extra slot p sb fi dev false aligned nt us dest off win) (pos : 0 < win.val) + (sb39 : sb.val ≤ 39) : + UtWinZA nt aligned (zA nt.val us) win.val ∧ p.val + fi.val ≤ aligned.val ∧ + 0 < win.val * 2 ^ (zA nt.val us).val ∧ (nt.val = 0 → us.val ≤ 39) := by + have R := UtDecP.of_live E.pre E.sys.sys E.place E.source E.sys.ln E.sys.vr E.sys.ly + have U := E.creation + obtain ⟨-, -, -, hA0, hAw, -, -, -⟩ := UtArgs.arithA R.ty R.ext R.cap R.utv hU + obtain ⟨hal, h47⟩ := UtArgs.zalA R.ty R.ext R.cap R.utv hU + have hus : us.val < 64 := E.kept.us64 + have hfit : aligned.val + win.val * 2 ^ (zA nt.val us).val ≤ 2 ^ 64 := le_trans U.A.aw U.A.pf + have hz := zA_valA nt us U.A.hn hus + have lo : p.val + fi.val ≤ aligned.val := by simpa using hA0 + have ne : 0 < win.val * 2 ^ (zA nt.val us).val := Nat.mul_pos pos (Nat.two_pow_pos _) + refine ⟨⟨by omega, hfit, hal, fun h => ?_, fun h => ?_⟩, lo, ne, fun h0 => ?_⟩ + · rw [hz] + unfold zOfA + rcases h with h | h <;> rw [h] <;> simp + · rw [hz] + unfold zOfA + rcases h with h | h <;> rw [h] <;> simp + · have e : (zA nt.val us).val = us.val := by + rw [hz, h0] + rfl + have le : 2 ^ (zA nt.val us).val ≤ win.val * 2 ^ (zA nt.val us).val := Nat.le_mul_of_pos_left _ pos + have pa := U.A.pa + have aw := U.A.aw + have : 2 ^ us.val ≤ 2 ^ sb.val := by rw [← e]; omega + have := (Nat.pow_le_pow_iff_right (by norm_num : 1 < 2)).1 this + omega + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtApHeap2A.lean b/proofs/Fl4ProofsA64/P3/UtApHeap2A.lean new file mode 100644 index 000000000..d9014cf02 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtApHeap2A.lean @@ -0,0 +1,1174 @@ +/- +P3 (aarch64, A64-P3 Untyped, full-record post, discharge): **the table and heap bundle `UtAsmHeapInA`** of a RAM +creation without a reset, at the creation start, over the slot set `OS ∪ NewSl` (the same state and slot set as the +cap-split, scheduler and spec bundles). + +* the creation-window facts: every old object, table page, footprint word and IPC-buffer word is off the window. +* `utRt_relNewA`: a spec object the grown relation names is an old one, or a new object of a type that has one. +* `utRt_heapAllA`: the bundle. + +Inputs beyond the creation's end facts: the destination slots were null (`dstNull`), the grown heap's facts, the +new `HighVSpace`s' words (`UtHighZW`) and the scheduler bundle (`UtAsmSchedInA`, for the spec side). + +No named premise. New theorems only. +-/ +import Fl4ProofsA64.P3.UtApHeapA +import Fl4ProofsA64.P3.UtApCaps2A +import Fl4ProofsA64.P3.UtApSchedA +import Fl4ProofsA64.P3.UtRtAsidA +import Fl4ProofsA64.P3.UtRtHighWordsA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +set_option maxRecDepth 100000 + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H H' : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s s' : S} {a : A} {σ : State Unit} {m : McState} {x p sb fi nfi : U64} + {nt us aligned dest off win : U64} {G' : MdbGhostA} + +/-- A table index's word offset in an 8 KiB root. -/ +theorem utPe_val2A {q i : U64} (hq : q.val + 8192 ≤ 2 ^ 64) (hi : i.val < 1024) : + (peAF q i).val = q.val + 8 * i.val := by + have e : (core.num.U64.wrapping_mul i 8#u64).val = 8 * i.val := by + have sz : UScalar.size UScalarTy.U64 = 2 ^ 64 := by simp [U64.size_def, UScalar.size, UScalarTy.numBits, U64.size, U64.numBits] + rw [core.num.U64.wrapping_mul_val_eq, sz, show (8#u64 : U64).val = 8 from rfl, + Nat.mod_eq_of_lt (by omega)] + omega + unfold peAF + rw [wa_valP, e] + exact Nat.mod_eq_of_lt (by omega) + +/-- `wa` of an in-bounds offset. -/ +theorem utWa_valA {q K : U64} (h : q.val + K.val < 2 ^ 64) : (wa q K).val = q.val + K.val := by + rw [wa_valP]; exact Nat.mod_eq_of_lt h + +/-- **An `OS` object cap's region is off the source's free part**, and does not wrap. -/ +theorem utRt_capExtA {par : U64 → Option U64} (Ut : UtInvA st OS par s) + (hx : x ∈ OS) (hc : capA st s x = .Untyped p sb fi false) {u : U64} (hu : u ∈ OS) {c : capability.Cap} + (hcu : capA st s u = c) (phys : physA c = true) (nut : isUtA c = false) {q : U64} (hq : ptrA c = q) {b : Nat} + (hb : (sizeA c).val = b) (b64 : b < 64) : + q.val + 2 ^ b ≤ 2 ^ 64 ∧ (q.val + 2 ^ b ≤ p.val + fi.val ∨ p.val + 2 ^ sb.val ≤ q.val) := by + have phys' : physA (capA st s u) = true := by rw [hcu]; exact phys + have reg := Ut.reg u hu phys' + have no := Ut.noov x hx p sb fi false hc u hu phys' + rw [hcu] at reg no + have mv : (mskA (sizeA c)).val = 2 ^ b - 1 := by + rw [mskA_val (by omega), hb] + have two : 0 < 2 ^ b := Nat.two_pow_pos _ + have le : 2 ^ b ≤ 2 ^ 64 := Nat.pow_le_pow_right (by norm_num) (by omega) + have qlt : q.val < 2 ^ 64 := by + have := q.hBounds + simpa using this + have tv : topA c = (q.val + (2 ^ b - 1)) % 2 ^ 64 := by + unfold topA + rw [wa_valP, hq, mv] + have bv : baseA c = q.val := by unfold baseA; rw [hq] + have fit : q.val + 2 ^ b ≤ 2 ^ 64 := by + by_contra g + have e : (q.val + (2 ^ b - 1)) % 2 ^ 64 = q.val + (2 ^ b - 1) - 2 ^ 64 := by + rw [Nat.mod_eq_sub_mod (by omega)] + exact Nat.mod_eq_of_lt (by omega) + rw [tv, bv, e] at reg + omega + have tv' : topA c = q.val + (2 ^ b - 1) := by + rw [tv]; exact Nat.mod_eq_of_lt (by omega) + refine ⟨fit, ?_⟩ + rcases no with (g | g) | ⟨g, -⟩ + · exact Or.inl (by rw [tv'] at g; omega) + · exact Or.inr (by rw [bv] at g; exact g) + · rw [nut] at g; cases g + +/-- A word of an `omega` heap object is neither the debug head nor a TCB's debug link. -/ +theorem utRt_omegaNeA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {b : U64} (h : b ∈ H.omega) : b ≠ nwA st s ∧ ∀ t, H t = some .tcb → b ≠ dpA t := by + obtain ⟨-, q, k, hq, kind, g1, g2⟩ := h + have hH : HeapOK H := J.sys.ent.heap + have h0 : (0#u64 : U64) ∈ Z.Cs := by + obtain ⟨k, hk, -, h⟩ := J.j.1.csN + exact (h 0#u64).2 (by simpa using hk) + have n0 := J.sq.nodes 0#u64 h0 + have d88 : layout.node.DEBUG_TCBS.val < 4288 := by rw [debug_val]; omega + have hv0 : (nwA st s).val = (L.ksb s).val + 223576 := by + rw [nodeAt_val n0 d88, debug_val] + unfold LawfulStore.nodeBase + simp + have off : b.val < (L.ksb s).val + 223488 ∨ (L.ksb s).val + 772352 ≤ b.val := by + by_cases ho : ∃ y, k = .other y + · obtain ⟨y, rfl⟩ := ho + simp only [HKind.bits] at g2 + rcases J.ko.2 q y hq with g | g | g | g <;> omega + · rcases J.ko.1 q k hq (fun y e => ho ⟨y, e⟩) with g | g <;> omega + refine ⟨fun e => ?_, fun t ht e => ?_⟩ + · rw [e, hv0] at off + omega + have f := hH.fits t _ ht + simp only [HKind.bits] at f + have dv : (dpA t).val = t.val + 3584 := by + unfold dpA + rw [wa_valP, show (3584#u64 : U64).val = 3584 from rfl] + exact Nat.mod_eq_of_lt (by omega) + have ne : q ≠ t := fun e' => by + rw [e', ht] at hq + have ek := (Option.some.inj hq).symm + rw [ek] at kind + rcases kind with g | ⟨_, g⟩ | ⟨_, g⟩ | ⟨g, -⟩ <;> cases g + have d := hH.disj q k t .tcb hq ht ne + rw [show HKind.tcb.bits = 12 from rfl] at d + have := congrArg UScalar.val e + rw [dv] at this + omega + +/-- **A creation keeps the words of an old table page off its window.** -/ +theorem utRt_pgKeptA {Rj : Set U64 → JSlotA S} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7A st Rj) s a σ m) + (w : WritesIn st s s' (UtCrtWA st H OS s nt aligned (zA nt.val us) win.val)) + {q : U64} (hq : SchedPageA st Z OS s q) (fit : q.val + 4096 ≤ 2 ^ 64) + (off : q.val + 4096 ≤ aligned.val ∨ aligned.val + win.val * 2 ^ (zA nt.val us).val ≤ q.val) + (i : U64) (hi : i.val < 512) : L.mem s' (peAF q i) = L.mem s (peAF q i) := by + obtain ⟨Pt, Ub, vr, -, -, -, -, -, -, link, -⟩ := J.j7 + have hv : VsR (L.ksb s) Pt (peAF q i) := by + refine Or.inl ⟨q, link q hq, core.num.U64.wrapping_mul i 8#u64, ?_, ?_, rfl⟩ + · rw [wm8_valA hi]; omega + · rw [wm8_valA hi]; omega + refine w.2.2 _ (vr.al _ hv) fun h => ?_ + rcases h with h | ⟨-, g1, g2⟩ | h | ⟨t, ht, h⟩ + · exact vr.os _ hv h + · rw [peAF_valA fit hi] at g1 g2 + omega + · exact (utRt_omegaNeA J (vr.om _ hv)).1 h + · exact (utRt_omegaNeA J (vr.om _ hv)).2 t ht h + +/-- **A spec object the grown relation names is old, or a new object** (module docstring). `Zx` is `utRtZA` with some +sets emptied; `K` says which types still name their new objects. -/ +theorem utRt_relNewA {Zx Z0 : XSetsA} {Tn Cn Vn : Set U64} {K : Prop} + (eth : Zx.th = utRtThA Z.th false nt us aligned win.val) (th0 : Z0.th = Z.th) + (eT : Zx.T = Z.T ∪ Tn) (T0 : Z0.T = Z.T) (eC : Zx.C = Z.C ∪ Cn) (C0 : Z0.C = Z.C) + (eV : Zx.V = Z.V ∪ Vn) (V0 : Z0.V = Z.V) + (hX : ∀ r ∈ Zx.X, r ∈ Z0.X ∨ (K ∧ r ∈ utBasesA aligned (zA nt.val us) win.val)) + (hY : ∀ r ∈ Zx.Y, r ∈ Z0.Y ∨ (K ∧ r ∈ utBasesA aligned (zA nt.val us) win.val)) + (ePp : Zx.Pp = Z0.Pp) (eS1 : Zx.S1 = Z0.S1) (eT2 : Zx.T2 = Z0.T2) + (kth : ∀ q, UtNewA false nt us aligned win.val q → K) + (kT : ∀ t ∈ Tn, K ∧ t ∈ utBasesA aligned (zA nt.val us) win.val) + (kC : ∀ c ∈ Cn, K ∧ c ∈ utBasesA aligned (zA nt.val us) win.val ∧ scNameA m c = specPtr c) + (kV : ∀ v ∈ Vn, K ∧ v ∈ utBasesA aligned (zA nt.val us) win.val) + {Tm : Set U64} {r : ObjRef} (h : RelPA Zx (utRtMA m Tm) r) : + RelPA Z0 m r ∨ (K ∧ UtNewSpA aligned (zA nt.val us) win.val r) := by + have new : ∀ q ∈ utBasesA aligned (zA nt.val us) win.val, UtNewSpA aligned (zA nt.val us) win.val (specPtr q) := by + rintro q ⟨j, hj, rfl⟩ + exact ⟨j, hj, rfl⟩ + unfold RelPA at h ⊢ + rcases h with ⟨q, hq, rfl⟩ | ⟨t, ht, rfl⟩ | ⟨c, hc, rfl⟩ | ⟨v, hv, rfl⟩ | ⟨q, hq, rfl⟩ | ⟨q, hq, rfl⟩ | + ⟨q, hq, rfl⟩ | ⟨q, hq, rfl⟩ | ⟨q, hq, rfl⟩ + · by_cases g : UtNewA false nt us aligned win.val q + · have g' := g + obtain ⟨-, -, -, j, hj, e⟩ := g' + exact Or.inr ⟨kth q g, new q ⟨j, hj, e⟩⟩ + · rw [eth, utRtTh_oldA g] at hq + exact Or.inl (Or.inl ⟨q, by rw [th0]; exact hq, rfl⟩) + · rw [eT] at ht + rcases ht with ht | ht + · exact Or.inl (Or.inr (Or.inl ⟨t, by rw [T0]; exact ht, rfl⟩)) + · exact Or.inr ⟨(kT t ht).1, new t (kT t ht).2⟩ + · rw [utRtMA_scRen] + rw [eC] at hc + rcases hc with hc | hc + · exact Or.inl (Or.inr (Or.inr (Or.inl ⟨c, by rw [C0]; exact hc, rfl⟩))) + · obtain ⟨k, g, e⟩ := kC c hc + refine Or.inr ⟨k, ?_⟩ + rw [show scRenA m c = scNameA m c from rfl, e] + exact new c g + · rw [eV] at hv + rcases hv with hv | hv + · exact Or.inl (Or.inr (Or.inr (Or.inr (Or.inl ⟨v, by rw [V0]; exact hv, rfl⟩)))) + · exact Or.inr ⟨(kV v hv).1, new v (kV v hv).2⟩ + · rcases hX q hq with o | ⟨k, g⟩ + · exact Or.inl (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨q, o, rfl⟩))))) + · exact Or.inr ⟨k, new q g⟩ + · rcases hY q hq with o | ⟨k, g⟩ + · exact Or.inl (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨q, o, rfl⟩)))))) + · exact Or.inr ⟨k, new q g⟩ + · rw [ePp] at hq + exact Or.inl (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨q, hq, rfl⟩))))))) + · rw [eS1] at hq + exact Or.inl (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨q, hq, rfl⟩)))))))) + · rw [eT2] at hq + exact Or.inl (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ⟨q, hq, rfl⟩)))))))) + +/-- Emptying the Stage-1 tables names no more objects. -/ +theorem utRelS1_monoA {r : ObjRef} (h : RelPA { Z with S1 := ∅ } m r) : RelPA Z m r := by + unfold RelPA at h ⊢ + rcases h with h | h | h | h | h | h | h | ⟨q, hq, -⟩ | h + · exact Or.inl h + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr (Or.inl h)) + · exact Or.inr (Or.inr (Or.inr (Or.inl h))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inl h)))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl h))))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl h)))))) + · exact absurd hq (Set.notMem_empty _) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr h))))))) + +/-- A type whose new objects are not in the typed heap. -/ +def UtTabNtA (nt : U64) : Prop := nt.val = 8 ∨ nt.val = 11 ∨ nt.val = 13 ∨ nt.val = 14 + +set_option maxHeartbeats 4000000 in +/-- **The table and heap bundle of a RAM creation at its start** (module docstring). -/ +theorem utRt_heapAllA {Rj : Set U64 → JSlotA S} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7A st Rj) s a σ m) + (hx : x ∈ OS) (hc : capA st s x = .Untyped p sb fi false) + (W : UtWinZA nt aligned (zA nt.val us) win.val) + (h1 : p.val + fi.val ≤ aligned.val) + (h2 : aligned.val + win.val * 2 ^ (zA nt.val us).val ≤ p.val + 2 ^ sb.val) + (Ec : CrtE st H H.frame OS s s' x p sb false aligned nt us dest off win) + (w : WritesIn st s s' (UtCrtWA st H OS s nt aligned (zA nt.val us) win.val)) + -- the destination slots were empty + (dstNull : ∀ j < win.val, capA st s (djA dest off j) = .Null) + -- the grown heap + (hgrow : ∀ q k, H q = some k → H' q = some k) + (hex : ∀ q k, H' q = some k → H q = some k ∨ + ∃ j < win.val, q = bjA aligned (zA nt.val us) j ∧ newKind nt.val us.val = some k) + (hnew : ∀ j < win.val, ∀ k, newKind nt.val us.val = some k → H' (bjA aligned (zA nt.val us) j) = some k) + -- the new `HighVSpace`s' words + (HW : nt.val = 14 → UtHighZW st aligned (zA nt.val us) win.val s') + -- the spec body + {t : ApiObjectType} (htt : objTypeOfA nt.val = some t) (hus : us.val < 64) (hsc : nt.val = 5 → 7 ≤ us.val) + {srcSlot : CslotPtr} {slots : List CslotPtr} {r : Unit × State Unit} + (hr : r ∈ (utRtBodyA srcSlot (specPtr p) (specPtr aligned) t us.val slots false σ).1) + -- the cap-split and scheduler bundles + (I : UtAsmCapsInA st Z (utRtZA Z G' nt us aligned win.val (UtOfTyA 1 nt us aligned win.val) + (UtOfTyA 5 nt us aligned win.val) (UtOfTyA 12 nt us aligned win.val)) H H' OS + (OS ∪ NewSl false nt us aligned win) s s' x p sb fi nfi nt us aligned win.val) + (Sch : UtAsmSchedInA st Z (utRtZA Z G' nt us aligned win.val (UtOfTyA 1 nt us aligned win.val) + (UtOfTyA 5 nt us aligned win.val) (UtOfTyA 12 nt us aligned win.val)) s s' σ r.2 m + (utRtMA m (UtOfTyA 1 nt us aligned win.val)) (UtOfTyA 1 nt us aligned win.val) + (UtOfTyA 5 nt us aligned win.val) + (fun q => t ≠ .untyped ∧ UtNewSpA aligned (zA nt.val us) win.val q) + (utNewDA t us.val σ.curDomain) n) : + UtAsmHeapInA st Z (utRtZA Z G' nt us aligned win.val (UtOfTyA 1 nt us aligned win.val) + (UtOfTyA 5 nt us aligned win.val) (UtOfTyA 12 nt us aligned win.val)) H H' OS + (OS ∪ NewSl false nt us aligned win) Rp (UtOfTyA 3 nt us aligned win.val) (UtOfTyA 2 nt us aligned win.val) + (UtOfTyA 6 nt us aligned win.val) Bf stack + (fun c => if c ∈ UtOfTyA 5 nt us aligned win.val then [] else stack c) s s' σ r.2 m + (utRtMA m (UtOfTyA 1 nt us aligned win.val)) x p sb fi nt us aligned win.val := by + have hn : nt.val < 15 := (objTypeOfA_some htt us.val).1 + have Cl : UtClrA st Z H s aligned.val (aligned.val + win.val * 2 ^ (zA nt.val us).val) := + utRt_winClrA J hx hc h1 h2 + have hH : HeapOK H := J.sys.ent.heap + obtain ⟨G, par, hT⟩ := J.sys.sysl.sys.stz.1.1.tu + have Ut := hT.ut + have fit := W.fit + -- the new objects + have bj : ∀ j < win.val, aligned.val ≤ (bjA aligned (zA nt.val us) j).val ∧ + (bjA aligned (zA nt.val us) j).val + 2 ^ (zA nt.val us).val ≤ + aligned.val + win.val * 2 ^ (zA nt.val us).val := by + intro j hj + have h1' : (j + 1) * 2 ^ (zA nt.val us).val ≤ win.val * 2 ^ (zA nt.val us).val := Nat.mul_le_mul_right _ hj + rw [Nat.add_mul, Nat.one_mul] at h1' + have e : (bjA aligned (zA nt.val us) j).val = aligned.val + j * 2 ^ (zA nt.val us).val := bjA_val (by + have := Nat.two_pow_pos (zA nt.val us).val + omega) + omega + have base : ∀ q ∈ utBasesA aligned (zA nt.val us) win.val, aligned.val ≤ q.val ∧ + q.val + 2 ^ (zA nt.val us).val ≤ aligned.val + win.val * 2 ^ (zA nt.val us).val := by + rintro q ⟨j, hj, rfl⟩ + exact bj j hj + have z12 : nt.val = 8 ∨ nt.val = 11 ∨ nt.val = 12 → 2 ^ (zA nt.val us).val = 4096 := fun h => by + rw [(utZa_pageA hn hus).1 h]; norm_num + have z13 : nt.val = 13 ∨ nt.val = 14 → 2 ^ (zA nt.val us).val = 8192 := fun h => by + rw [(utZa_pageA hn hus).2 h]; norm_num + have z1 : nt.val = 1 → 2 ^ (zA nt.val us).val = 4096 := fun h => by + have e : (zA nt.val us).val = 12 := by rw [h]; exact zA_1 us + rw [e]; norm_num + -- old pages and roots are off the window + have pgFit : ∀ q, q ∈ Z.Pp ∨ q ∈ Z.S1 ∨ q ∈ Z.T2 ∨ q ∈ Z.V → q.val + 4096 ≤ 2 ^ 64 ∧ + (q.val + 4096 ≤ aligned.val ∨ aligned.val + win.val * 2 ^ (zA nt.val us).val ≤ q.val) := by + intro q hq + have hq' : q ∈ Z.Pp ∪ Z.S1 ∪ Z.T2 ∪ Z.V := by + rcases hq with h | h | h | h + · exact Or.inl (Or.inl (Or.inl h)) + · exact Or.inl (Or.inl (Or.inr h)) + · exact Or.inl (Or.inr h) + · exact Or.inr h + obtain ⟨o, y, ho, k1, k2⟩ := J.setsInH.pages q hq' + have f := hH.fits o _ ho + simp only [HKind.bits] at f + refine ⟨by omega, Cl.obj q 4096 (Or.inl ⟨rfl, ?_⟩)⟩ + rcases hq with h | h | h | h + · exact Or.inl h + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr (Or.inl h)) + · exact Or.inr (Or.inr (Or.inr (Or.inl h))) + have rtFit : ∀ q, q ∈ Z.X ∨ q ∈ Z.Y → q.val + 8192 ≤ 2 ^ 64 ∧ + (q.val + 8192 ≤ aligned.val ∨ aligned.val + win.val * 2 ^ (zA nt.val us).val ≤ q.val) := by + intro q hq + obtain ⟨o, y, ho, k1, k2⟩ := J.setsInH.roots q hq + have f := hH.fits o _ ho + simp only [HKind.bits] at f + exact ⟨by omega, Cl.obj q 8192 (Or.inr (Or.inl ⟨rfl, hq⟩))⟩ + -- a word of a page in or off the window + have peOff : ∀ q : U64, q.val + 4096 ≤ 2 ^ 64 → + (q.val + 4096 ≤ aligned.val ∨ aligned.val + win.val * 2 ^ (zA nt.val us).val ≤ q.val) → ∀ i : U64, + i.val < 512 → ¬ (aligned.val ≤ (peAF q i).val ∧ + (peAF q i).val < aligned.val + win.val * 2 ^ (zA nt.val us).val) := by + intro q f o i hi g + rw [peAF_valA f hi] at g + omega + have peIn : ∀ q : U64, aligned.val ≤ q.val → q.val + 4096 ≤ aligned.val + win.val * 2 ^ (zA nt.val us).val → + ∀ i : U64, i.val < 512 → aligned.val ≤ (peAF q i).val ∧ + (peAF q i).val < aligned.val + win.val * 2 ^ (zA nt.val us).val := by + intro q g1 g2 i hi + rw [peAF_valA (by omega) hi] + omega + -- the second page of a root + have wa4 : ∀ q : U64, q.val + 8192 ≤ 2 ^ 64 → (wa q 4096#u64).val = q.val + 4096 := by + intro q f + have e : (4096#u64 : U64).val = 4096 := rfl + rw [utWa_valA (by rw [e]; omega), e] + -- the old footprint is off the window + have footOff : ∀ b, FootA (L := L) Z s b → + ¬ (aligned.val ≤ b.val ∧ b.val < aligned.val + win.val * 2 ^ (zA nt.val us).val) := + fun b h => ut_foot_regionA J Cl h + have footS1 : ∀ b, FootA (L := L) { Z with S1 := ∅ } s b → + ¬ (aligned.val ≤ b.val ∧ b.val < aligned.val + win.val * 2 ^ (zA nt.val us).val) := + fun b h => footOff b (FootA.s1_monoA h) + -- IPC buffers are off the window + have bufOff : ∀ b, BufW Bf b → + ¬ (aligned.val ≤ b.val ∧ b.val < aligned.val + win.val * 2 ^ (zA nt.val us).val) := fun b hb => + ut_where_regionA J Cl (ut_rd_whereA J (N := N) (E := E) (Rp := Rp) (Bf := Bf) + (Or.inr (Or.inl (Or.inr (Or.inr (Or.inr hb)))))) + -- the new sets by type + have ofT : ∀ k q, q ∈ UtOfTyA k nt us aligned win.val → + nt.val = k ∧ q ∈ utBasesA aligned (zA nt.val us) win.val := fun _ _ h => h + have anchor : ∀ j, j < win.val → specPtr (bjA aligned (zA nt.val us) j) ≠ Fl4SpecA64.idleScPtr := by + intro j hj + have v := utRt_newValA W hj + exact utRet_anchorA (st := st) J.idleAnchor hx hc (by omega) (by omega) + -- the spec side + have N' := Sch.spec + have specNew : ∀ j < win.val, t ≠ .untyped → + r.2.kheap (specPtr (bjA aligned (zA nt.val us) j)) = some (defaultObject t false us.val σ.curDomain) := by + intro j hj tn + have o := N'.new (specPtr (bjA aligned (zA nt.val us) j)) ⟨tn, j, hj, rfl⟩ + rw [utNewDA_ne tn] at o + exact o + have specKeep : ∀ q ko, σ.kheap q = some ko → (∀ rr cs, ko ≠ .cnode rr cs) → r.2.kheap q = some ko := N'.keep + have ty8 : nt.val = 8 → t = .archObject .vspaceObj := fun e => by + rw [e] at htt; simpa [objTypeOfA, archDataToObjType, eq_comm] using htt + have ty11 : nt.val = 11 → t = .archObject .pageTableObj := fun e => by + rw [e] at htt; simpa [objTypeOfA, archDataToObjType, eq_comm] using htt + have ty12 : nt.val = 12 → t = .archObject .vcpuObj := fun e => by + rw [e] at htt; simpa [objTypeOfA, archDataToObjType, eq_comm] using htt + have ty13 : nt.val = 13 → t = .archObject (.ext .stage2VSpaceObj) := fun e => by + rw [e] at htt; simpa [objTypeOfA, eq_comm] using htt + have ty14 : nt.val = 14 → t = .archObject (.ext .highVSpaceObj) := fun e => by + rw [e] at htt; simpa [objTypeOfA, eq_comm] using htt + -- the grown sets by name + generalize hZ' : utRtZA Z G' nt us aligned win.val (UtOfTyA 1 nt us aligned win.val) + (UtOfTyA 5 nt us aligned win.val) (UtOfTyA 12 nt us aligned win.val) = Z' at I Sch ⊢ + have eth : Z'.th = utRtThA Z.th false nt us aligned win.val := by rw [← hZ']; rfl + have eT : Z'.T = Z.T ∪ UtOfTyA 1 nt us aligned win.val := by rw [← hZ']; rfl + have eC : Z'.C = Z.C ∪ UtOfTyA 5 nt us aligned win.val := by rw [← hZ']; rfl + have eV : Z'.V = Z.V ∪ UtOfTyA 12 nt us aligned win.val := by rw [← hZ']; rfl + have eX : Z'.X = Z.X ∪ utXNewA nt aligned (zA nt.val us) win.val := by rw [← hZ']; rfl + have eY : Z'.Y = Z.Y ∪ utYNewA nt aligned (zA nt.val us) win.val := by rw [← hZ']; rfl + have eS : Z'.S1 = Z.S1 ∪ utS1NewA nt aligned (zA nt.val us) win.val := by rw [← hZ']; rfl + have ePp : Z'.Pp = Z.Pp := by rw [← hZ']; rfl + have eT2 : Z'.T2 = Z.T2 := by rw [← hZ']; rfl + have eCs : Z'.Cs = Z.Cs := by rw [← hZ']; rfl + have ksb' : L.ksb s' = L.ksb s := w.1 + have cpu' : L.cpu s' = L.cpu s := w.2.1 + -- a new slot word of the typed heap + have slotNew : ∀ q, UtNewA false nt us aligned win.val q → ∀ y : U64, ∀ i : Nat, + 32 * i + 32 ≤ 2 ^ (zA nt.val us).val → y = slotAddrA (specPtr q) (BitVec.ofNat 64 i) → + ∀ v : U64, (v = y ∨ v = wa y 8#u64 ∨ v = wa y 16#u64 ∨ v = wa y 24#u64) → + aligned.val ≤ v.val ∧ v.val < aligned.val + win.val * 2 ^ (zA nt.val us).val := by + rintro q ⟨-, -, -, j, hj, rfl⟩ y i hi ey v hv + have g := bj j hj + have yv : y.val = (bjA aligned (zA nt.val us) j).val + 32 * i := by + rw [ey]; exact slotAddrA_valN (by omega) + have o : ∀ K : U64, K.val ≤ 24 → (wa y K).val = y.val + K.val := fun K hK => utWa_valA (by omega) + rcases hv with rfl | rfl | rfl | rfl + · omega + · have := o 8#u64 (by simp) + have e : (8#u64 : U64).val = 8 := rfl + omega + · have := o 16#u64 (by simp) + have e : (16#u64 : U64).val = 16 := rfl + omega + · have := o 24#u64 (by simp) + have e : (24#u64 : U64).val = 24 := rfl + omega + have slotF : ∀ y, SlotWA Z'.th y → SlotWA Z.th y ∨ (¬ UtTabNtA nt ∧ + ∀ v : U64, (v = y ∨ v = wa y 8#u64 ∨ v = wa y 16#u64 ∨ v = wa y 24#u64) → + aligned.val ≤ v.val ∧ v.val < aligned.val + win.val * 2 ^ (zA nt.val us).val) := by + intro y hy + rw [eth] at hy + have notab : ∀ q, UtNewA false nt us aligned win.val q → ¬ UtTabNtA nt := fun q g h => by + have := g.2.2.1 + unfold UtTabNtA at h + omega + have cnz : ∀ q rr, UtNewA false nt us aligned win.val q → ObjKindA.cnode rr = utKindA nt us → + rr = us.val ∧ (zA nt.val us).val = us.val + 5 := by + intro q rr _ ek + unfold utKindA at ek + split_ifs at ek with e1 e4 + cases ek + refine ⟨rfl, ?_⟩ + rw [zA_valA nt us hn hus]; simp [zOfA, e4, Nat.add_comm] + have cn : ∀ q rr, utRtThA Z.th false nt us aligned win.val q = some (.cnode rr) → ∀ i < 2 ^ rr, + y = slotAddrA (specPtr q) (BitVec.ofNat 64 i) → Z.th q = some (.cnode rr) ∨ (¬ UtTabNtA nt ∧ + ∀ v : U64, (v = y ∨ v = wa y 8#u64 ∨ v = wa y 16#u64 ∨ v = wa y 24#u64) → + aligned.val ≤ v.val ∧ v.val < aligned.val + win.val * 2 ^ (zA nt.val us).val) := by + intro q rr hq i hi ey + rcases utRtTh_splitA hq with ⟨-, o⟩ | ⟨g, ek⟩ + · exact Or.inl o + obtain ⟨er, ez⟩ := cnz q rr g ek + refine Or.inr ⟨notab q g, slotNew q g y i ?_ ey⟩ + rw [ez, pow_add, ← er] + omega + rcases hy with ⟨q, ⟨rr, hq, i, hi, ey⟩ | ⟨hq, i, hi, ey⟩⟩ | ⟨q, rr, hq, idx, hl, ey⟩ + · exact (cn q rr hq i hi ey).imp (fun o => Or.inl ⟨q, Or.inl ⟨rr, o, i, hi, ey⟩⟩) id + · rcases utRtTh_splitA hq with ⟨-, o⟩ | ⟨g, ek⟩ + · exact Or.inl (Or.inl ⟨q, Or.inr ⟨o, i, hi, ey⟩⟩) + have e1 : nt.val = 1 := by + unfold utKindA at ek + split_ifs at ek with e1 e4 + exact e1 + refine Or.inr ⟨notab q g, slotNew q g y i ?_ ey⟩ + rw [z1 e1] + unfold tcbSlotsA at hi + omega + · have l64 : idx.length ≤ 64 := by + rcases utRtTh_splitA hq with ⟨-, o⟩ | ⟨g, ek⟩ + · have h := J.j.1.place.fit q _ o + simp only [ObjKindA.size] at h + have : 2 ^ (rr + 5) ≤ 2 ^ 64 := by omega + have := (Nat.pow_le_pow_iff_right (by norm_num : 1 < 2)).1 this + omega + · have := (cnz q rr g ek).1 + omega + have ey' : y = slotAddrA (specPtr q) (BitVec.ofNat 64 (blvA idx)) := by + rw [ey, ofBlA_eq l64] + exact (cn q rr hq (blvA idx) (hl ▸ blvA_lt idx) ey').imp + (fun o => Or.inr ⟨q, rr, o, idx, hl, ey⟩) id + -- the new TCB, SC and VCPU words + have tcbF : ∀ u ∈ Z'.T, u ∈ Z.T ∨ (¬ UtTabNtA nt ∧ ∀ v : U64, (v = stA u ∨ v = scA u ∨ v = affA u ∨ + v = wa u 3664#u64 ∨ v = runVcpuA u ∨ v = prioA u ∨ v = domA u) → + aligned.val ≤ v.val ∧ v.val < aligned.val + win.val * 2 ^ (zA nt.val us).val) := by + intro u hu + rw [eT] at hu + rcases hu with hu | hu + · exact Or.inl hu + obtain ⟨e1, g⟩ := ofT _ _ hu + have gu := base u g + have ezgu := z1 e1 + have o : ∀ K : U64, K.val < 4096 → aligned.val ≤ (wa u K).val ∧ + (wa u K).val < aligned.val + win.val * 2 ^ (zA nt.val us).val := by + intro K hK + rw [utWa_valA (by omega)] + omega + refine Or.inr ⟨fun h => by unfold UtTabNtA at h; omega, fun v hv => ?_⟩ + rcases hv with rfl | rfl | rfl | rfl | rfl | rfl | rfl + · rw [stA_eq]; exact o _ (by simp) + · exact o 3464#u64 (by simp) + · exact o 3520#u64 (by simp) + · exact o 3664#u64 (by simp) + · exact o 3672#u64 (by simp) + · exact o 3448#u64 (by simp) + · exact o 3432#u64 (by simp) + have scF : ∀ c ∈ Z'.C, c ∈ Z.C ∨ (¬ UtTabNtA nt ∧ ∀ v : U64, (v = wa c 24#u64 ∨ v = wa c 72#u64) → + aligned.val ≤ v.val ∧ v.val < aligned.val + win.val * 2 ^ (zA nt.val us).val) := by + intro c hc' + rw [eC] at hc' + rcases hc' with hc' | hc' + · exact Or.inl hc' + obtain ⟨e5, g⟩ := ofT _ _ hc' + have gu := base c g + have big : 128 ≤ 2 ^ (zA nt.val us).val := by + have ez : (zA nt.val us).val = us.val := by rw [zA_valA nt us hn hus, e5]; unfold zOfA; simp + rw [ez, show (128 : Nat) = 2 ^ 7 by norm_num] + exact Nat.pow_le_pow_right (by norm_num) (hsc e5) + have o : ∀ K : U64, K.val < 128 → aligned.val ≤ (wa c K).val ∧ + (wa c K).val < aligned.val + win.val * 2 ^ (zA nt.val us).val := by + intro K hK + rw [utWa_valA (by omega)] + omega + refine Or.inr ⟨fun h => by unfold UtTabNtA at h; omega, fun v hv => ?_⟩ + rcases hv with rfl | rfl + · exact o 24#u64 (by simp) + · exact o 72#u64 (by simp) + have vcF : ∀ v ∈ Z'.V, v ∈ Z.V ∨ (¬ UtTabNtA nt ∧ ∀ b : U64, (b = v ∨ b = wa v 24#u64) → + aligned.val ≤ b.val ∧ b.val < aligned.val + win.val * 2 ^ (zA nt.val us).val) := by + intro v hv + rw [eV] at hv + rcases hv with hv | hv + · exact Or.inl hv + obtain ⟨e12, g⟩ := ofT _ _ hv + have gu := base v g + have ezgu := z12 (by omega) + have e24 : (24#u64 : U64).val = 24 := rfl + refine Or.inr ⟨fun h => by unfold UtTabNtA at h; omega, fun b hb => ?_⟩ + rcases hb with rfl | rfl + · omega + · rw [utWa_valA (by rw [e24]; omega), e24] + omega + -- the footprint splits + have F : UtFootNewA { Z with S1 := ∅ } { Z' with S1 := ∅ } + (fun v => (nt.val ≠ 8 ∧ nt.val ≠ 11) ∧ + aligned.val ≤ v.val ∧ v.val < aligned.val + win.val * 2 ^ (zA nt.val us).val) := by + have nt' : ¬ UtTabNtA nt → nt.val ≠ 8 ∧ nt.val ≠ 11 := fun h => by unfold UtTabNtA at h; omega + refine + { slot := fun y hy => (slotF y hy).imp id (fun g v hv => ⟨nt' g.1, g.2 v hv⟩) + cs := eCs + pp := ePp + t2 := eT2 + tcb := fun u hu => (tcbF u hu).imp id (fun g v hv => ⟨nt' g.1, g.2 v hv⟩) + sc := fun c hc' => (scF c hc').imp id (fun g v hv => ⟨nt' g.1, g.2 v hv⟩) + vcpu := fun v hv => (vcF v hv).imp id (fun g b hb => ⟨nt' g.1, g.2 b hb⟩) + s1 := fun q hq => absurd hq (Set.notMem_empty _) + y := fun y hy => ?_ + x := fun q hq => ?_ } + · change y ∈ Z'.Y at hy + rw [eY] at hy + rcases hy with hy | ⟨e14, g⟩ + · exact Or.inl hy + have gu := base y g + have ezgu := z13 (Or.inr e14) + have e4 := wa4 y (by omega) + have k : nt.val ≠ 8 ∧ nt.val ≠ 11 := by omega + refine Or.inr ⟨fun i hi => ⟨k, peIn _ (by omega) (by omega) i hi⟩, ⟨k, peIn y (by omega) (by omega) _ (by simp)⟩, + fun i hi => ⟨k, peIn y (by omega) (by omega) i (by omega)⟩⟩ + · change q ∈ Z'.X at hq + rw [eX] at hq + rcases hq with hq | ⟨e13, g⟩ + · exact Or.inl hq + have gu := base q g + have ezgu := z13 (Or.inl e13) + have e4 := wa4 q (by omega) + have k : nt.val ≠ 8 ∧ nt.val ≠ 11 := by omega + exact Or.inr fun i hi => ⟨⟨k, peIn q (by omega) (by omega) i hi⟩, ⟨k, peIn _ (by omega) (by omega) i hi⟩⟩ + have Fe : UtFootNewA (extZA Z) (extZA Z') + (fun v => ¬ UtTabNtA nt ∧ + aligned.val ≤ v.val ∧ v.val < aligned.val + win.val * 2 ^ (zA nt.val us).val) := + { slot := fun y hy => (slotF y hy).imp id (fun g v hv => ⟨g.1, g.2 v hv⟩) + cs := eCs + pp := ePp + t2 := rfl + tcb := fun u hu => (tcbF u hu).imp id (fun g v hv => ⟨g.1, g.2 v hv⟩) + sc := fun c hc' => (scF c hc').imp id (fun g v hv => ⟨g.1, g.2 v hv⟩) + vcpu := fun v hv => (vcF v hv).imp id (fun g b hb => ⟨g.1, g.2 b hb⟩) + s1 := fun q hq => absurd hq (Set.notMem_empty _) + y := fun y hy => absurd hy (Set.notMem_empty _) + x := fun q hq => absurd hq (Set.notMem_empty _) } + -- the new objects the relation names + have kC : ∀ (K : Prop), (nt.val = 5 → K) → ∀ c ∈ UtOfTyA 5 nt us aligned win.val, + K ∧ c ∈ utBasesA aligned (zA nt.val us) win.val ∧ scNameA m c = specPtr c := fun K k c hc' => + ⟨k (ofT _ _ hc').1, (ofT _ _ hc').2, Sch.newSc.nameEq c hc'⟩ + have kN : ∀ (K : Prop) (i : Nat), (nt.val = i → K) → ∀ q ∈ UtOfTyA i nt us aligned win.val, + K ∧ q ∈ utBasesA aligned (zA nt.val us) win.val := fun K i k q hq => ⟨k (ofT _ _ hq).1, (ofT _ _ hq).2⟩ + have relS1 : ∀ q, RelPA { Z' with S1 := ∅ } (utRtMA m (UtOfTyA 1 nt us aligned win.val)) q → + RelPA { Z with S1 := ∅ } m q ∨ + ((nt.val ≠ 8 ∧ nt.val ≠ 11) ∧ UtNewSpA aligned (zA nt.val us) win.val q) := by + intro q h + refine utRt_relNewA (Z := Z) (Zx := { Z' with S1 := ∅ }) (Z0 := { Z with S1 := ∅ }) + (K := nt.val ≠ 8 ∧ nt.val ≠ 11) (nt := nt) (us := us) (aligned := aligned) (win := win) eth rfl eT rfl eC rfl + eV rfl (fun v hv => ?_) (fun v hv => ?_) ePp rfl eT2 (fun v g => ?_) (kN _ 1 (by omega)) (kC _ (by omega)) + (kN _ 12 (by omega)) h + · change v ∈ Z'.X at hv + rw [eX] at hv + exact hv.imp id fun g => ⟨by have := g.1; omega, g.2⟩ + · change v ∈ Z'.Y at hv + rw [eY] at hv + exact hv.imp id fun g => ⟨by have := g.1; omega, g.2⟩ + · have := g.2.2.1 + omega + have relExt : ∀ q, RelPA (extZA Z') (utRtMA m (UtOfTyA 1 nt us aligned win.val)) q → + RelPA (extZA Z) m q ∨ (¬ UtTabNtA nt ∧ UtNewSpA aligned (zA nt.val us) win.val q) := by + intro q h + have k : ∀ i : Nat, i ≠ 8 → i ≠ 11 → i ≠ 13 → i ≠ 14 → nt.val = i → ¬ UtTabNtA nt := by + intro i a b c d e g + unfold UtTabNtA at g + omega + refine utRt_relNewA (Z := Z) (Zx := extZA Z') (Z0 := extZA Z) (K := ¬ UtTabNtA nt) (nt := nt) (us := us) + (aligned := aligned) (win := win) eth rfl eT rfl eC rfl eV rfl + (fun v hv => absurd hv (Set.notMem_empty _)) (fun v hv => absurd hv (Set.notMem_empty _)) ePp rfl rfl + (fun v g => ?_) (kN _ 1 (k 1 (by omega) (by omega) (by omega) (by omega))) + (kC _ (k 5 (by omega) (by omega) (by omega) (by omega))) + (kN _ 12 (k 12 (by omega) (by omega) (by omega) (by omega))) h + have := g.2.2.1 + unfold UtTabNtA + omega + -- an object the old relation names is at no new address + have oldRel : ∀ q, RelPA Z m q → ¬ UtNewSpA aligned (zA nt.val us) win.val q := fun q h => + utRt_relOffA (m := m) J Cl W rfl rfl anchor h + have newAt : ∀ q : U64, UtNewSpA aligned (zA nt.val us) win.val (specPtr q) → + q ∈ utBasesA aligned (zA nt.val us) win.val := by + rintro q ⟨j, hj, e⟩ + exact ⟨j, hj, ut_specPtr_injA e⟩ + -- the new Stage-1 tables are off the grown footprint + have newTab : nt.val = 8 ∨ nt.val = 11 → ∀ q ∈ utBasesA aligned (zA nt.val us) win.val, + TabOffA (L := L) Z' (utRtMA m (UtOfTyA 1 nt us aligned win.val)) s' q := by + intro ty q hq + have gq := base q hq + have ezgq := z12 (by omega) + refine ⟨fun i hi k => ?_, fun v hv e => ?_⟩ + · rcases utRt_footSplitA F ksb' cpu' k with o | g + · exact footS1 _ o (peIn q (by omega) (by omega) i hi) + · have := g.1; omega + · rcases relS1 v hv with o | ⟨g, -⟩ + · refine oldRel v (utRelS1_monoA o) ?_ + obtain ⟨j, hj, ej⟩ := hq + exact ⟨j, hj, e.trans (congrArg specPtr ej)⟩ + · omega + -- an old table page off the window stays off the grown footprint + have oldTab : ∀ q : U64, q.val + 4096 ≤ 2 ^ 64 → + (q.val + 4096 ≤ aligned.val ∨ aligned.val + win.val * 2 ^ (zA nt.val us).val ≤ q.val) → + TabOffA (L := L) Z m s q → + TabOffA (L := L) Z' (utRtMA m (UtOfTyA 1 nt us aligned win.val)) s' q := by + intro q f off O + refine ⟨fun i hi k => ?_, fun v hv e => ?_⟩ + · rcases utRt_footSplitA F ksb' cpu' k with o | g + · exact O.mem i hi o + · exact peOff q f off i hi g.2 + · rcases relS1 v hv with o | ⟨-, g⟩ + · exact O.obj v o e + · rw [e] at g + have := base q (newAt q g) + have := Nat.two_pow_pos (zA nt.val us).val + omega + -- an unmapped table of a kept cap + have unmKept : ∀ ty q, SchedPageA st Z OS s q → q.val + 4096 ≤ 2 ^ 64 → + (q.val + 4096 ≤ aligned.val ∨ aligned.val + win.val * 2 ^ (zA nt.val us).val ≤ q.val) → + UnmTabA st Z m s σ ty q → UnmTabA st Z' (utRtMA m (UtOfTyA 1 nt us aligned win.val)) s' r.2 ty q := by + intro ty q pg f off O + obtain ⟨tb, hk, hty, he⟩ := O.spec + refine ⟨fun i hi => ?_, ⟨tb, specKeep _ _ hk (fun _ _ e => by cases e), hty, he⟩, oldTab q f off O.off⟩ + rw [utRt_pgKeptA J w pg f off i hi] + exact O.inv i hi + have capExt : ∀ u ∈ OS, ∀ c, capA st s u = c → physA c = true → isUtA c = false → ∀ q : U64, ptrA c = q → + ∀ b : Nat, (sizeA c).val = b → b < 64 → q.val + 2 ^ b ≤ 2 ^ 64 ∧ + (q.val + 2 ^ b ≤ aligned.val ∨ aligned.val + win.val * 2 ^ (zA nt.val us).val ≤ q.val) := by + intro u hu c hcu phys nut q hq b hb b64 + obtain ⟨f, o⟩ := utRt_capExtA Ut hx hc hu hcu phys nut hq hb b64 + refine ⟨f, ?_⟩ + rcases o with o | o + · exact Or.inl (by omega) + · exact Or.inr (by omega) + have pw12 : (2 : Nat) ^ 12 = 4096 := by norm_num + have pw13 : (2 : Nat) ^ 13 = 8192 := by norm_num + have peZero : ¬ WKA nt → ∀ j < win.val, ∀ i : U64, i.val < 1024 → 8 * i.val + 8 ≤ 2 ^ (zA nt.val us).val → + 3 ≤ (zA nt.val us).val → 4096 ≤ 2 ^ (zA nt.val us).val → i.val < 512 ∨ 8192 ≤ 2 ^ (zA nt.val us).val → + L.mem s' (peAF (bjA aligned (zA nt.val us) j) i) = 0#u64 := by + intro wk j hj i hi ib h3 h4 f + have g := bj j hj + have ev : (peAF (bjA aligned (zA nt.val us) j) i).val = (bjA aligned (zA nt.val us) j).val + 8 * i.val := by + rcases f with f | f + · exact peAF_valA (by omega) f + · exact utPe_val2A (by omega) hi + exact utRt_newZeroA J hx hc W h1 h2 Ec wk hj h3 (by omega) ib ev + have dbit0 : dBitA (0#u64) 0 = false := by decide + have newS1 : ∀ q ∈ utS1NewA nt aligned (zA nt.val us) win.val, + nt.val = 8 ∧ q ∈ utBasesA aligned (zA nt.val us) win.val := fun _ h => h + refine + { nt15 := hn + ksb := ksb' + cpu := cpu' + src' := Or.inl hx + kept := fun u hu ne nn => ⟨Or.inl hu, Ec.keep u hu ne (fun j hj e => nn (by rw [e]; exact dstNull j hj))⟩ + tcbSub := fun t h => by rw [eT]; exact Or.inl h + heapN := fun q hq => by + obtain ⟨e, j, hj, rfl⟩ := ofT _ _ hq + exact hnew j hj _ (by rw [e]; rfl) + heapE := fun q hq => by + obtain ⟨e, j, hj, rfl⟩ := ofT _ _ hq + exact hnew j hj _ (by rw [e]; rfl) + heapR := fun q hq => by + obtain ⟨e, j, hj, rfl⟩ := ofT _ _ hq + exact hnew j hj _ (by rw [e]; rfl) + scName := fun _ => rfl + idleSc := fun _ => rfl + scSplit := fun c hc' => ?_ + thSplit := fun q z h => Or.inr (utRtTh_posA J.thPos q z (by rw [eth] at h; exact h)) + ptNew := fun q hq => ?_ + pagesNew := fun q hq => ?_ + rootsNew := fun q hq => ?_ + vsVcSplit := fun b hb hv => ?_ + unmT := fun u hu q asid va e _ O => by + have g := capExt u hu _ e (physA_of rfl) rfl q (ptrA_of rfl) 12 (by rw [sizeA_of rfl]; rfl) (by omega) + rw [pw12] at g + exact unmKept _ q (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inl ⟨_, _, _, e⟩⟩))))))) g.1 g.2 O + unmP := fun u hu q asid e _ O => by + have g := capExt u hu _ e (physA_of rfl) rfl q (ptrA_of rfl) 12 (by rw [sizeA_of rfl]; rfl) (by omega) + rw [pw12] at g + exact unmKept _ q (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inl ⟨_, _, e⟩)⟩))))))) g.1 g.2 O + unmS := fun u hu q asid va e _ O => by + have g := capExt u hu _ e (physA_of rfl) rfl q (ptrA_of rfl) 13 (by rw [sizeA_of rfl]; simp [fl4_abi.consts.bits.STAGE2_VSPACE, fl4_abi.consts.bits.HIGH_VSPACE]) (by omega) + rw [pw13] at g + have e4 := wa4 q g.1 + have pg0 : SchedPageA st Z OS s q := Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inr (Or.inl ⟨_, _, e⟩))⟩)))))) + have pg1 : SchedPageA st Z OS s (wa q 4096#u64) := Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inr (Or.inr (Or.inl ⟨q, _, _, e, rfl⟩)))⟩)))))) + obtain ⟨es, hk, he⟩ := O.spec + refine ⟨fun i hi => ?_, es, specKeep _ _ hk (fun _ _ e => by cases e), he⟩ + by_cases lo : i.val < 512 + · rw [utRt_pgKeptA J w pg0 (by omega) (by omega) i lo] + exact O.inv i hi + · obtain ⟨j, hj, ej⟩ := peAF_hiA q i (by omega) hi + rw [ej, utRt_pgKeptA J w pg1 (by omega) (by omega) j hj, ← ej] + exact O.inv i hi + unmH := fun u hu q asid e _ O => by + have g := capExt u hu _ e (physA_of rfl) rfl q (ptrA_of rfl) 13 (by rw [sizeA_of rfl]; simp [fl4_abi.consts.bits.STAGE2_VSPACE, fl4_abi.consts.bits.HIGH_VSPACE]) (by omega) + rw [pw13] at g + have e4 := wa4 q g.1 + have pg1 : SchedPageA st Z OS s (wa q 4096#u64) := Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + ⟨u, hu, Or.inr (Or.inr (Or.inr (Or.inr ⟨q, _, e, rfl⟩)))⟩)))))) + obtain ⟨l1, hk, he⟩ := O.spec + refine ⟨fun i hi => ?_, l1, specKeep _ _ hk (fun _ _ e => by cases e), he⟩ + rw [utRt_pgKeptA J w pg1 (by omega) (by omega) i hi] + exact O.inv i hi + newT := fun e j hj => by + have et := ty11 e + have hq : bjA aligned (zA nt.val us) j ∈ utBasesA aligned (zA nt.val us) win.val := ⟨j, hj, rfl⟩ + have ez : (zA nt.val us).val = 12 := (utZa_pageA hn hus).1 (Or.inr (Or.inl e)) + refine ⟨fun i hi => ?_, ?_, newTab (Or.inr e) _ hq⟩ + · rw [peZero (by unfold WKA; omega) j hj i (by omega) (by rw [ez, pw12]; omega) (by omega) (by rw [ez, pw12]) (Or.inl hi)] + exact dbit0 + · have o := specNew j hj (by rw [et]; simp) + rw [et] at o + exact ⟨_, o, rfl, fun _ _ => rfl⟩ + newP := fun e j hj => by + have et := ty8 e + have hq : bjA aligned (zA nt.val us) j ∈ utBasesA aligned (zA nt.val us) win.val := ⟨j, hj, rfl⟩ + have ez : (zA nt.val us).val = 12 := (utZa_pageA hn hus).1 (Or.inl e) + refine ⟨fun i hi => ?_, ?_, newTab (Or.inl e) _ hq⟩ + · rw [peZero (by unfold WKA; omega) j hj i (by omega) (by rw [ez, pw12]; omega) (by omega) (by rw [ez, pw12]) (Or.inl hi)] + exact dbit0 + · have o := specNew j hj (by rw [et]; simp) + rw [et] at o + exact ⟨_, o, rfl, fun _ _ => rfl⟩ + newS := fun e j hj => by + have et := ty13 e + have ez : (zA nt.val us).val = 13 := (utZa_pageA hn hus).2 (Or.inl e) + have g := bj j hj + have ezg := z13 (Or.inl e) + refine ⟨fun i hi => ?_, ?_⟩ + · rw [peZero (by unfold WKA; omega) j hj i hi (by rw [ez, pw13]; omega) (by omega) (by rw [ez, pw13]; omega) (Or.inr (by rw [ez, pw13]))] + exact dbit0 + · have o := specNew j hj (by rw [et]; simp) + rw [et] at o + exact ⟨_, o, fun _ => rfl⟩ + newH := fun e j hj => by + have et := ty14 e + refine ⟨fun i hi => ?_, ?_⟩ + · rw [((HW e) j hj).2.2 i hi] + exact dbit0 + · have o := specNew j hj (by rw [et]; simp) + rw [et] at o + exact ⟨_, o, fun _ => rfl⟩ + tabFoot := ⟨_, F, fun q hq i hi g => ?_, fun q hq => ?_⟩ + tabObj := fun q hq v hv e => ?_ + extFoot := ⟨_, Fe, fun q hq i hi g => ?_, fun q hq => ?_⟩ + extObj := fun q hq h => ?_ + bufOs := fun b hb h => ?_ + bufVs := fun b hb h => ?_ + bufPg := fun b hb q hq o ho o8 e => ?_ + bufVc := fun b hb v hv o ho ho8 e => ?_ + deadV := fun v hv => ?_ + deadR := fun q hq c hc' hs => ?_ + deadY := fun q hq y e => by + rcases hq with hq | hq + · obtain ⟨x, hk, -⟩ := J.ep.reply q hq + have := (specKeep _ _ hk (fun _ _ e => by cases e)).symm.trans e + cases this + exact Or.inl ⟨hq, hk⟩ + · obtain ⟨e6, j, hj, rfl⟩ := ofT _ _ hq + have et : t = .replyObject := by + rw [e6] at htt; simpa [objTypeOfA, eq_comm] using htt + have o := specNew j hj (by rw [et]; simp) + rw [e, et] at o + simp only [defaultObject, Option.some.injEq, KernelObject.reply.injEq] at o + exact Or.inr (by rw [o]; rfl) + asid := utRtBody_asidA hr + deadT := fun u hu => ?_ + deadW := Sch.vcw + frSplit := ⟨fun y => ∃ j < win.val, capA st s' y = newCapA nt.val (bjA aligned (zA nt.val us) j) us false, + fun y hy hk => ?_, fun y z ⟨j, _, ey⟩ ⟨l, _, ez⟩ fy kz => ?_⟩ + devSplit := fun y hy q size rights mapped ept asid va h => ?_ + devKeep := fun q hq => ?_ } + · -- scSplit + rw [eC] at hc' + rcases hc' with hc' | hc' + · obtain ⟨y, k, hk, -⟩ := J.j.1.sch.sc c hc' + refine Or.inl ⟨hc', ?_⟩ + show r.2.kheap (scRenA m c) = σ.kheap (scRenA m c) + rw [hk] + exact specKeep _ _ hk (fun _ _ e => by cases e) + · obtain ⟨e5, j, hj, ej⟩ := ofT _ _ hc' + have et : t = .schedContextObject := (objTypeOfA_some htt us.val).2.2.2.2.2.2 e5 + refine Or.inr ⟨us.val, ?_, hsc e5, ?_⟩ + · rw [ej]; exact hnew j hj _ (by rw [e5]; rfl) + · show r.2.kheap (scNameA m c) = _ + rw [Sch.newSc.nameEq c hc', ej] + have o := specNew j hj (by rw [et]; simp) + rw [et] at o + exact o + · -- ptNew + rw [ePp, eS] at hq + rcases hq with hq | hq | hq + · exact Or.inl (Or.inl hq) + · exact Or.inl (Or.inr hq) + obtain ⟨ty, j, hj, rfl⟩ := newS1 q hq + refine Or.inr ⟨_, 12, hnew j hj _ (by rw [ty]; rfl), le_rfl, by norm_num⟩ + · -- pagesNew + rw [ePp, eS, eT2, eV] at hq + rcases hq with ((hq | hq | hq) | hq) | hq | hq + · exact Or.inl (Or.inl (Or.inl (Or.inl hq))) + · exact Or.inl (Or.inl (Or.inl (Or.inr hq))) + · obtain ⟨ty, j, hj, rfl⟩ := newS1 q hq + exact Or.inr ⟨_, 12, hnew j hj _ (by rw [ty]; rfl), le_rfl, by norm_num⟩ + · exact Or.inl (Or.inl (Or.inr hq)) + · exact Or.inl (Or.inr hq) + · obtain ⟨e12, j, hj, rfl⟩ := ofT _ _ hq + exact Or.inr ⟨_, 12, hnew j hj _ (by rw [e12]; rfl), le_rfl, by norm_num⟩ + · -- rootsNew + rw [eX, eY] at hq + rcases hq with (hq | ⟨e, j, hj, rfl⟩) | (hq | ⟨e, j, hj, rfl⟩) + · exact Or.inl (Or.inl hq) + · exact Or.inr ⟨_, 13, hnew j hj _ (by rw [e]; rfl), le_rfl, by norm_num⟩ + · exact Or.inl (Or.inr hq) + · exact Or.inr ⟨_, 13, hnew j hj _ (by rw [e]; rfl), le_rfl, by norm_num⟩ + · -- vsVcSplit + rw [ePp, eS, eY] at hb + rw [eV] at hv + have vs : VsFootA Z.Pp Z.S1 Z.Y (L.ksb s) b ∨ ((nt.val = 8 ∨ nt.val = 11 ∨ nt.val = 14) ∧ + aligned.val ≤ b.val ∧ b.val < aligned.val + win.val * 2 ^ (zA nt.val us).val) := by + cases hb with + | asid hi h => exact Or.inl (.asid hi h) + | pool q lo hq h => exact Or.inl (.pool q lo hq h) + | pt q i hq h => + rcases hq with hq | hq + · exact Or.inl (.pt q i hq h) + obtain ⟨ty, g⟩ := newS1 q hq + have gq := base q g + have ezgq := z12 (by omega) + exact Or.inr ⟨by omega, peIn q (by omega) (by omega) i h⟩ + | high y i hy h => + rcases hy with hy | ⟨e14, g⟩ + · exact Or.inl (.high y i hy h) + have gq := base y g + have ezgq := z13 (Or.inr e14) + have e4 := wa4 y (by omega) + exact Or.inr ⟨by omega, peIn _ (by omega) (by omega) i h⟩ + have vc : VcW Z.V b ∨ (nt.val = 12 ∧ + aligned.val ≤ b.val ∧ b.val < aligned.val + win.val * 2 ^ (zA nt.val us).val) := by + obtain ⟨v, hv, o, o1, o8, e⟩ := hv + rcases hv with hv | hv + · exact Or.inl ⟨v, hv, o, o1, o8, e⟩ + obtain ⟨e12, g⟩ := ofT _ _ hv + have gq := base v g + have ezgq := z12 (by omega) + have ev : b.val = v.val + o.val := by rw [e]; exact utWa_valA (by omega) + exact Or.inr ⟨e12, by omega, by omega⟩ + rcases vs with vs | ⟨ty, vs⟩ <;> rcases vc with vc | ⟨ty', vc⟩ + · exact ⟨vs, vc⟩ + · exact absurd vc (footOff b (.nslot b (.vs b vs))) + · obtain ⟨v, hv, o, o1, o8, e⟩ := vc + have g := pgFit v (Or.inr (Or.inr (Or.inr hv))) + have ev : b.val = v.val + o.val := by rw [e]; exact utWa_valA (by omega) + omega + · omega + · -- tabFoot: no table meets the new words + change q ∈ Z'.S1 at hq + rw [eS] at hq + rcases hq with hq | hq + · have f := pgFit q (Or.inr (Or.inl hq)) + exact peOff q f.1 f.2 i hi g.2 + · have := (newS1 q hq).1 + omega + · change q ∈ Z'.S1 at hq + rw [eS] at hq + rcases hq with hq | hq + · exact Or.inl hq + obtain ⟨ty, g⟩ := newS1 q hq + have gq := base q g + have ezgq := z12 (by omega) + exact Or.inr fun i hi k => footS1 _ k (peIn q (by omega) (by omega) i hi) + · -- tabObj + rw [eS] at hq + rcases hq with hq | hq + · rcases relS1 v hv with o | ⟨-, g⟩ + · exact (J.tabS1 q hq).obj v o e + · rw [e] at g + have := base q (newAt q g) + have := pgFit q (Or.inr (Or.inl hq)) + have := Nat.two_pow_pos (zA nt.val us).val + omega + · obtain ⟨ty, g⟩ := newS1 q hq + rcases relS1 v hv with o | ⟨k, -⟩ + · refine oldRel v (utRelS1_monoA o) ?_ + obtain ⟨j, hj, ej⟩ := g + exact ⟨j, hj, e.trans (congrArg specPtr ej)⟩ + · omega + · -- extFoot: no ext table meets the new words + have old : q ∈ S2PgE Z.X Z.T2 ∨ (∃ y ∈ Z.Y, q = wa y 4096#u64) → q.val + 4096 ≤ 2 ^ 64 ∧ + (q.val + 4096 ≤ aligned.val ∨ aligned.val + win.val * 2 ^ (zA nt.val us).val ≤ q.val) := by + rintro (h | ⟨y, hy, e⟩) + · rcases h with h | ⟨v, hv, e⟩ + · exact pgFit q (Or.inr (Or.inr (Or.inl h))) + have f := rtFit v (Or.inl hv) + rcases e with e | e + · rw [e]; exact ⟨by omega, by omega⟩ + · have e4 := wa4 v f.1 + rw [e]; exact ⟨by omega, by omega⟩ + · have f := rtFit y (Or.inr hy) + have e4 := wa4 y f.1 + rw [e]; exact ⟨by omega, by omega⟩ + have split : (q ∈ S2PgE Z.X Z.T2 ∨ ∃ y ∈ Z.Y, q = wa y 4096#u64) ∨ UtTabNtA nt := by + rcases hq with h | ⟨y, hy, e⟩ + · rcases h with h | ⟨v, hv, e⟩ + · rw [eT2] at h; exact Or.inl (Or.inl (Or.inl h)) + rw [eX] at hv + rcases hv with hv | ⟨e13, -⟩ + · exact Or.inl (Or.inl (Or.inr ⟨v, hv, e⟩)) + · exact Or.inr (Or.inr (Or.inr (Or.inl e13))) + · rw [eY] at hy + rcases hy with hy | ⟨e14, -⟩ + · exact Or.inl (Or.inr ⟨y, hy, e⟩) + · exact Or.inr (Or.inr (Or.inr (Or.inr e14))) + rcases split with o | nt' + · have f := old o + exact peOff q f.1 f.2 i hi g.2 + · exact g.1 nt' + · have new : ∀ v ∈ utBasesA aligned (zA nt.val us) win.val, nt.val = 13 ∨ nt.val = 14 → + (q = v ∨ q = wa v 4096#u64) → ∀ i : U64, i.val < 512 → ¬ FootA (L := L) (extZA Z) s (peAF q i) := by + intro v hv ty e i hi k + have gq := base v hv + have ezgq := z13 ty + have e4 := wa4 v (by omega) + refine footS1 _ (FootA.extMonoA k) (peIn q ?_ ?_ i hi) <;> rcases e with e | e <;> rw [e] <;> omega + rcases hq with h | ⟨y, hy, e⟩ + · rcases h with h | ⟨v, hv, e⟩ + · rw [eT2] at h; exact Or.inl (Or.inl (Or.inl h)) + rw [eX] at hv + rcases hv with hv | ⟨e13, g⟩ + · exact Or.inl (Or.inl (Or.inr ⟨v, hv, e⟩)) + · exact Or.inr (new v g (Or.inl e13) e) + · rw [eY] at hy + rcases hy with hy | ⟨e14, g⟩ + · exact Or.inl (Or.inr ⟨y, hy, e⟩) + · exact Or.inr (new y g (Or.inr e14) (Or.inr e)) + · -- extObj + have split : (q ∈ Z.X ∨ q ∈ Z.Y ∨ q ∈ Z.T2) ∨ + ((nt.val = 13 ∨ nt.val = 14) ∧ q ∈ utBasesA aligned (zA nt.val us) win.val) := by + rcases hq with hq | hq | hq + · rw [eX] at hq + rcases hq with hq | ⟨e, g⟩ + · exact Or.inl (Or.inl hq) + · exact Or.inr ⟨Or.inl e, g⟩ + · rw [eY] at hq + rcases hq with hq | ⟨e, g⟩ + · exact Or.inl (Or.inr (Or.inl hq)) + · exact Or.inr ⟨Or.inr e, g⟩ + · rw [eT2] at hq + exact Or.inl (Or.inr (Or.inr hq)) + rcases split with o | ⟨ty, g⟩ + · rcases relExt _ h with k | ⟨-, k⟩ + · exact J.tabExt.obj q o k + · have gb := base q (newAt q k) + have := Nat.two_pow_pos (zA nt.val us).val + rcases o with o | o | o + · have := rtFit q (Or.inl o); omega + · have := rtFit q (Or.inr o); omega + · have := pgFit q (Or.inr (Or.inr (Or.inl o))); omega + · rcases relExt _ h with k | ⟨k, -⟩ + · refine oldRel _ ?_ (by obtain ⟨j, hj, ej⟩ := g; exact ⟨j, hj, congrArg specPtr ej⟩) + exact RelPA.extMonoA k |> utRelS1_monoA + · exact k (by unfold UtTabNtA; omega) + · -- bufOs + obtain ⟨y, hy, g1, g2⟩ := h + rcases hy with hy | ⟨-, j, hj, i, kind, yv⟩ + · exact ⟨y, hy, g1, g2⟩ + have gj := bj j hj + have le : 32 * i + 32 ≤ 2 ^ (zA nt.val us).val := by + rcases kind with ⟨e1, hi⟩ | ⟨e4, hi⟩ + · rw [z1 e1]; omega + · have ez : (zA nt.val us).val = us.val + 5 := by + rw [zA_valA nt us hn hus]; simp [zOfA, e4, Nat.add_comm] + rw [ez, pow_add] + omega + exact absurd ⟨by omega, by omega⟩ (bufOff b hb) + · -- bufVs + rw [ksb', ePp, eS, eY] at h + cases h with + | asid hi h => exact .asid hi h + | pool q lo hq h => exact .pool q lo hq h + | pt q i hq h => + rcases hq with hq | hq + · exact .pt q i hq h + obtain ⟨ty, g⟩ := newS1 q hq + have gq := base q g + have ezgq := z12 (by omega) + exact absurd (peIn q (by omega) (by omega) i h) (bufOff _ hb) + | high y i hy h => + rcases hy with hy | ⟨e14, g⟩ + · exact .high y i hy h + have gq := base y g + have ezgq := z13 (Or.inr e14) + have e4 := wa4 y (by omega) + exact absurd (peIn _ (by omega) (by omega) i h) (bufOff _ hb) + · -- bufPg + have hq' : SchedPageA st Z' {u | u ∈ OS ∪ NewSl false nt us aligned win ∧ capA st s' u ≠ .Null} s' q := by + unfold SchedPageA at hq ⊢ + rcases hq with h | h | h | h | h | h | h | ⟨u, hu, k⟩ + · exact Or.inl h + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr (Or.inl h)) + · exact Or.inr (Or.inr (Or.inr (Or.inl h))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inl h)))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl h))))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl h)))))) + · refine Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ⟨u, ⟨hu, fun z => ?_⟩, k⟩)))))) + rcases k with ⟨_, _, _, k⟩ | ⟨_, _, k⟩ | ⟨_, _, k⟩ | ⟨_, _, _, k, -⟩ | ⟨_, _, k, -⟩ <;> rw [z] at k <;> cases k + have newPg : ∀ p ∈ UtSchedNewA nt us aligned win.val, + aligned.val ≤ p.val ∧ p.val + 4096 ≤ aligned.val + win.val * 2 ^ (zA nt.val us).val := by + rintro p ⟨j, hj, h⟩ + have gj := bj j hj + rcases h with ⟨ty, e⟩ | ⟨ty, e⟩ | ⟨ty, e⟩ + · have ezgj := z12 (by omega) + rw [e]; exact ⟨gj.1, by omega⟩ + all_goals + have ezgj := z13 (by omega) + have e4 := wa4 (bjA aligned (zA nt.val us) j) (by omega) + rcases e with e | e <;> rw [e] <;> omega + rcases utRt_schedPageA (Z := Z) (OS := OS) (s := s) (nt := nt) (us := us) (aligned := aligned) (win := win.val) + (dev := false) hn ⟨fun p h => by rw [ePp] at h; exact h, fun p h => by + rw [eS] at h + rcases h with h | h + · exact Or.inl h + obtain ⟨ty, j, hj, e⟩ := newS1 p h |>.imp id id + exact Or.inr ⟨j, hj, Or.inl ⟨Or.inl ty, e⟩⟩, + fun p h => by rw [eT2] at h; exact Or.inl h, + fun v h => by + rw [eX] at h + rcases h with h | ⟨e13, j, hj, e⟩ + · exact Or.inl h + exact Or.inr ⟨⟨j, hj, Or.inr (Or.inl ⟨e13, Or.inl e⟩)⟩, ⟨j, hj, Or.inr (Or.inl ⟨e13, Or.inr (by rw [e])⟩)⟩⟩, + fun v h => by + rw [eY] at h + rcases h with h | ⟨e14, j, hj, e⟩ + · exact Or.inl h + exact Or.inr ⟨⟨j, hj, Or.inr (Or.inr ⟨e14, Or.inl e⟩)⟩, ⟨j, hj, Or.inr (Or.inr ⟨e14, Or.inr (by rw [e])⟩)⟩⟩⟩ + (fun u hu => by + obtain ⟨hu, nn⟩ := hu + rcases I.slots u hu with ⟨uo, k⟩ | ⟨-, f, k⟩ | ⟨j, hj, k⟩ | k + · exact Or.inr (Or.inl ⟨uo, k⟩) + · exact Or.inl ⟨_, _, _, _, k⟩ + · exact Or.inr (Or.inr ⟨j, hj, k⟩) + · exact absurd k nn) q hq' with old | new + · exact old + · have g := newPg q new + have ev : b.val = q.val + o.val := by rw [e]; exact utWa_valA (by omega) + exact absurd ⟨by omega, by omega⟩ (bufOff b hb) + · -- bufVc + rw [eV] at hv + rcases hv with hv | hv + · exact hv + obtain ⟨e12, g⟩ := ofT _ _ hv + have gq := base v g + have ezgq := z12 (by omega) + have ev : b.val = v.val + o.val := by + rw [e]; exact utWa_valA (by omega) + exact absurd ⟨by omega, by omega⟩ (bufOff b hb) + · -- deadV + rw [eV] at hv + rcases hv with hv | hv + · obtain ⟨y, hk, -⟩ := J.j.2.1.obj v hv + exact Or.inl ⟨hv, (specKeep _ _ hk (fun _ _ e => by cases e)).trans hk.symm⟩ + obtain ⟨e12, j, hj, ej⟩ := ofT _ _ hv + refine Or.inr fun y hy => ?_ + have et := ty12 e12 + have o := specNew j hj (by rw [et]; simp) + rw [← ej, hy, et] at o + have e : y = defaultVcpu := by + simp only [defaultObject, defaultArchObject, Option.some.injEq, KernelObject.archObj.injEq, + ArchKernelObj.vcpu.injEq] at o + exact o + rw [e] + rfl + · -- deadR + by_cases g : c ∈ UtOfTyA 5 nt us aligned win.val + · rw [ite_eq_left g] at hs + cases hs + rw [ite_eq_right g] at hs + rw [eC] at hc' + have co : c ∈ Z.C := hc'.resolve_right g + exact ⟨(J.reply.stackRel c co).2.1 q hs, co, hs⟩ + · -- deadT + rw [eT] at hu + rcases hu with hu | hu + · exact Or.inl ⟨hu, Sch.tvk u hu⟩ + · exact Or.inr (Sch.tvn u hu) + · -- frSplit: the slots + rcases I.slots y hy with ⟨old, k⟩ | ⟨-, f, k⟩ | ⟨j, hj, k⟩ | k + · exact Or.inl ⟨y, old, k.symm⟩ + · rw [k] at hk + rcases hk with hk | hk + · cases hk + · have := hk.2.1; simp [isUtA] at this + · refine Or.inr ⟨⟨j, hj, k⟩, ?_⟩ + rw [k, newCapA_ptr] + have gj := bj j hj + have := Nat.two_pow_pos (zA nt.val us).val + exact ⟨by omega, by omega⟩ + · rw [k] at hk + rcases hk with hk | hk + · cases hk + · have h1 := hk.1 + have e := physA_eq capability.Cap.Null + simp only [objects.cap_is_physical] at e + rw [← okinj e] at h1 + cases h1 + · rw [ez] at kz + rw [ey] at fy + have := kz.2.2 + rw [utNewCap_frA nt.val (bjA aligned (zA nt.val us) l) (bjA aligned (zA nt.val us) j), fy] at this + cases this + · -- devSplit + rcases I.slots y hy with ⟨old, k⟩ | ⟨-, f, k⟩ | ⟨j, hj, k⟩ | k + · exact Or.inl ⟨y, old, k.symm⟩ + · rw [k] at h; cases h + · rw [k] at h; exact absurd h (utNewCap_notDevFrA _ _ _ _ _ _ _ _ _ _) + · rw [k] at h; cases h + · -- devKeep + refine ⟨?_, fun dev pg h => specKeep _ _ h (fun _ _ e => by cases e)⟩ + cases h : H q with + | some k => exact hgrow q k h + | none => + cases h' : H' q with + | none => rfl + | some k => + rcases hex q k h' with old | ⟨j, hj, e, -⟩ + · rw [h] at old; cases old + · have gj := bj j hj + have := Nat.two_pow_pos (zA nt.val us).val + exact absurd (by rw [e]; exact ⟨by omega, by omega⟩) hq + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtApHeapA.lean b/proofs/Fl4ProofsA64/P3/UtApHeapA.lean new file mode 100644 index 000000000..c142893f7 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtApHeapA.lean @@ -0,0 +1,217 @@ +/- +P3 (aarch64, A64-P3 Untyped, full-record post, discharge): **parts of the table / heap bundle `UtAsmHeapInA`** of a +RAM creation, from A64 P1's creation end (`S1aRtIn`) and the set split of `utRt_j7SplitA`. Discharged here: the +type bound, block base and core, the kept slots, the new IPC objects' heap kinds, the new pages and roots in heap +objects, the new frame caps (none is a device cap, all lie in the source's free part) and the kept heap below the +free part. + +No named premise. New theorems only. +-/ +import Fl4ProofsA64.P3.UtAsmHeapA +import Fl4ProofsA64.P3.UtFullJ7A + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Classical + +local notation "wa" => core.num.U64.wrapping_add + +private theorem ut_iteDevA {α : Type} {c : Prop} [Decidable c] {x y z : α} (h : (if c then x else y) = z) : + (c ∧ x = z) ∨ (¬ c ∧ y = z) := by + by_cases g : c + · exact Or.inl ⟨g, (ite_eq_left g).symm.trans h⟩ + · exact Or.inr ⟨g, (ite_eq_right g).symm.trans h⟩ + +/-- A new cap of a RAM creation is no device frame. -/ +theorem utNewCap_notDevFrA (n : Nat) (b u p : U64) (sz : _) (rights : _) (mapped ept : _) (asid va : _) : + newCapA n b u false ≠ .Frame p sz rights true mapped ept asid va := by + intro h + unfold newCapA at h + repeat + rcases ut_iteDevA h with ⟨-, h⟩ | ⟨-, h⟩ + · cases h + cases h + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {Z Z' : XSetsA} {H H1 H' : Heap} {Cs OS OS1 U0 Ac : Set U64} {s s1 q : S} {ia ie lo hi : Nat} + {slot p sb fi aligned nt us dest off win : U64} {dev : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + +structure UtApHeapOutA (st : machine.Store S) [L : LawfulStore st] (Z Z' : XSetsA) (H H' : Heap) (OS OS' : Set U64) + (s s' : S) (x p sb fi nt us aligned : U64) (win : Nat) : Prop where + nt15 : nt.val < 15 + ksb : L.ksb s' = L.ksb s + cpu : L.cpu s' = L.cpu s + src' : x ∈ OS' + kept : ∀ u ∈ OS, u ≠ x → capA st s u ≠ .Null → u ∈ OS' ∧ capA st s' u = capA st s u + heapN : ∀ r, (nt.val = 3 ∧ ∃ j < win, r = bjA aligned (zA nt.val us) j) → H' r = some .ntfn + heapE : ∀ r, (nt.val = 2 ∧ ∃ j < win, r = bjA aligned (zA nt.val us) j) → H' r = some .ep + heapR : ∀ r, (nt.val = 6 ∧ ∃ j < win, r = bjA aligned (zA nt.val us) j) → H' r = some .reply + ptNew : ∀ r ∈ Z'.Pp ∪ Z'.S1, r ∈ Z.Pp ∪ Z.S1 ∨ + ∃ o z, H' o = some (.other z) ∧ o.val ≤ r.val ∧ r.val + 4096 ≤ o.val + 2 ^ z + pagesNew : ∀ r ∈ Z'.Pp ∪ Z'.S1 ∪ Z'.T2 ∪ Z'.V, r ∈ Z.Pp ∪ Z.S1 ∪ Z.T2 ∪ Z.V ∨ + ∃ o z, H' o = some (.other z) ∧ o.val ≤ r.val ∧ r.val + 4096 ≤ o.val + 2 ^ z + rootsNew : ∀ r ∈ Z'.X ∪ Z'.Y, r ∈ Z.X ∪ Z.Y ∨ + ∃ o z, H' o = some (.other z) ∧ o.val ≤ r.val ∧ r.val + 8192 ≤ o.val + 2 ^ z + frSplit : ∃ New : U64 → Prop, + (∀ y ∈ OS', isFrA (capA st s' y) = true ∨ RngKeyA (capA st s' y) → + (∃ y0 ∈ OS, capA st s y0 = capA st s' y) ∨ + (New y ∧ p.val + fi.val ≤ (ptrA (capA st s' y)).val ∧ (ptrA (capA st s' y)).val < p.val + 2 ^ sb.val)) ∧ + ∀ y z, New y → New z → isFrA (capA st s' y) = true → ¬ RngKeyA (capA st s' z) + devSplit : ∀ y ∈ OS', ∀ o size rights mapped ept asid va, + capA st s' y = .Frame o size rights true mapped ept asid va → + (∃ y0 ∈ OS, capA st s y0 = capA st s' y) ∨ + ∀ k, H' o = some k → (∃ bits, k = .other bits) ∧ False + heapKeep : ∀ r : U64, ¬ (p.val + fi.val ≤ r.val ∧ r.val < p.val + 2 ^ sb.val) → H' r = H r + +theorem utRt_heapInA + (E : S1aRtIn st H Cs OS U0 Ac s ia ie slot p sb fi dev extra H1 H' OS1 s1 q lo hi aligned nt us dest off win) + (hH1 : H1 = H) (ram : dev = false) (ksw : (L.ksb s).val + 788736 ≤ 2 ^ 64) + (cap' : ∃ nfi, capA st q slot = .Untyped p sb nfi false) + (hlo : p.val + fi.val ≤ aligned.val) + (F : UtJ7SplitA st Z Z' (s1aRtSlots OS1 dev nt us aligned win) s q slot p sb nt us aligned win.val) + (roots : ∀ r ∈ Z'.X ∪ Z'.Y, r ∈ Z.X ∪ Z.Y ∨ + ((nt.val = 13 ∨ nt.val = 14) ∧ ∃ j < win.val, r = bjA aligned (zA nt.val us) j)) : + UtApHeapOutA st Z Z' H H' OS (s1aRtSlots OS1 dev nt us aligned win) s q slot p sb fi nt us aligned win.val := by + have U := E.creation + have kb : L.ksb q = L.ksb s := E.ksb + have FN := U.freshN E.kept (by rw [kb]; exact ksw) + have hfit : aligned.val + win.val * 2 ^ (zA nt.val us).val ≤ 2 ^ 64 := le_trans U.A.aw U.A.pf + have inWin : ∀ j < win.val, p.val + fi.val ≤ (bjA aligned (zA nt.val us) j).val ∧ + (bjA aligned (zA nt.val us) j).val < p.val + 2 ^ sb.val := by + intro j hj + obtain ⟨v, le⟩ := bj_facts hj hfit + have := Nat.two_pow_pos (zA nt.val us).val + have aw := U.A.aw + exact ⟨by omega, by omega⟩ + have newK : ∀ j < win.val, ∀ k, newKind nt.val us.val = some k → H' (bjA aligned (zA nt.val us) j) = some k := + fun j hj => U.hn ram j hj + -- the slots of the grown set + have split : ∀ y ∈ s1aRtSlots OS1 dev nt us aligned win, + (y ∈ OS ∧ capA st q y = capA st s y) ∨ (∃ nfi, capA st q y = .Untyped p sb nfi false) ∨ + (∃ j < win.val, capA st q y = newCapA nt.val (bjA aligned (zA nt.val us) j) us false) ∨ + capA st q y = .Null := by + rintro y (hy | hy) + · by_cases es : y = slot + · rw [es] + exact Or.inr (Or.inl cap') + by_cases ed : ∃ j, j < win.val ∧ y = djA dest off j + · obtain ⟨j, hj, e⟩ := ed + refine Or.inr (Or.inr (Or.inl ⟨j, hj, ?_⟩)) + rw [e, U.E.new j hj, ram] + · refine Or.inl ⟨U.os y hy, ?_⟩ + rw [U.E.keep y hy es (fun j hj e => ed ⟨j, hj, e⟩), U.k1 y hy es] + · exact Or.inr (Or.inr (Or.inr (FN.null y hy))) + -- a new table page lies in a new object + have newPage : ∀ r ∈ UtSchedNewA nt us aligned win.val, + ∃ o z, H' o = some (.other z) ∧ o.val ≤ r.val ∧ r.val + 4096 ≤ o.val + 2 ^ z := by + intro r ⟨j, hj, h⟩ + have kind : nt.val = 8 ∨ nt.val = 11 ∨ nt.val = 13 ∨ nt.val = 14 := by + rcases h with ⟨h, -⟩ | ⟨h, -⟩ | ⟨h, -⟩ <;> omega + obtain ⟨-, size, typed, -, fit, -⟩ := E.newObj hj kind + refine ⟨_, _, typed, ?_⟩ + have p12 : 4096 ≤ 2 ^ (zA nt.val us).val := by + rcases size with e | e <;> rw [e] <;> norm_num + have two : nt.val = 13 ∨ nt.val = 14 → 2 ^ (zA nt.val us).val = 8192 := by + intro g + have e : (zA nt.val us).val = 13 := by rcases g with g | g <;> simp [g, zA] + rw [e] + norm_num + have hiV : nt.val = 13 ∨ nt.val = 14 → + (wa (bjA aligned (zA nt.val us) j) 4096#u64).val = (bjA aligned (zA nt.val us) j).val + 4096 := by + intro g + have := two g + rw [wa_val] + exact Nat.mod_eq_of_lt (by simp; omega) + rcases h with ⟨-, e⟩ | ⟨g, e | e⟩ | ⟨g, e | e⟩ + · rw [e]; exact ⟨le_rfl, by omega⟩ + · rw [e]; exact ⟨le_rfl, by omega⟩ + · rw [e, hiV (Or.inl g), two (Or.inl g)]; exact ⟨by omega, by omega⟩ + · rw [e]; exact ⟨le_rfl, by omega⟩ + · rw [e, hiV (Or.inr g), two (Or.inr g)]; exact ⟨by omega, by omega⟩ + have newV : nt.val = 12 → ∀ j < win.val, + ∃ o z, H' o = some (.other z) ∧ o.val ≤ (bjA aligned (zA nt.val us) j).val ∧ + (bjA aligned (zA nt.val us) j).val + 4096 ≤ o.val + 2 ^ z := by + intro h12 j hj + exact ⟨_, 12, newK j hj _ (by rw [h12]; rfl), le_rfl, by norm_num⟩ + refine + { nt15 := U.A.hn + ksb := kb + cpu := U.E.fr.2.1.trans U.w1.2.1 + src' := Or.inl U.src + kept := fun u hu ne nn => E.capKeep hu nn ne + heapN := fun r ⟨h, j, hj, e⟩ => by rw [e]; exact newK j hj _ (by rw [h]; rfl) + heapE := fun r ⟨h, j, hj, e⟩ => by rw [e]; exact newK j hj _ (by rw [h]; rfl) + heapR := fun r ⟨h, j, hj, e⟩ => by rw [e]; exact newK j hj _ (by rw [h]; rfl) + ptNew := fun r hr => ?_ + pagesNew := fun r hr => ?_ + rootsNew := fun r hr => ?_ + frSplit := ⟨fun y => ∃ j < win.val, capA st q y = newCapA nt.val (bjA aligned (zA nt.val us) j) us false, + fun y hy hk => ?_, fun y z ⟨j, _, ey⟩ ⟨l, _, ez⟩ fy kz => ?_⟩ + devSplit := fun y hy o size rights mapped ept asid va hc => ?_ + heapKeep := fun r hr => ?_ } + · rcases hr with h | h + · exact Or.inl (Or.inl (F.pp r h).1) + · rcases F.s1 r h with ⟨old, -⟩ | new + · exact Or.inl (Or.inr old) + · exact Or.inr (newPage r new) + · rcases hr with ((h | h) | h) | h + · exact Or.inl (Or.inl (Or.inl (Or.inl (F.pp r h).1))) + · rcases F.s1 r h with ⟨old, -⟩ | new + · exact Or.inl (Or.inl (Or.inl (Or.inr old))) + · exact Or.inr (newPage r new) + · rcases F.t2 r h with ⟨old, -⟩ | new + · exact Or.inl (Or.inl (Or.inr old)) + · exact Or.inr (newPage r new) + · rcases F.v r h with ⟨old, -⟩ | ⟨h12, j, hj, e⟩ + · exact Or.inl (Or.inr old) + · rw [e]; exact Or.inr (newV h12 j hj) + · rcases roots r hr with old | ⟨g, j, hj, e⟩ + · exact Or.inl old + have kind : nt.val = 8 ∨ nt.val = 11 ∨ nt.val = 13 ∨ nt.val = 14 := by omega + obtain ⟨-, -, typed, -⟩ := E.newObj hj kind + have e13 : (zA nt.val us).val = 13 := by rcases g with g | g <;> simp [g, zA] + refine Or.inr ⟨_, _, typed, by rw [e], ?_⟩ + rw [e, e13] + norm_num + · rcases split y hy with ⟨old, k⟩ | ⟨nfi, k⟩ | ⟨j, hj, k⟩ | k + · exact Or.inl ⟨y, old, k.symm⟩ + · rw [k] at hk + rcases hk with hk | hk + · cases hk + · have := hk.2.1; simp [isUtA] at this + · refine Or.inr ⟨⟨j, hj, k⟩, ?_⟩ + rw [k, newCapA_ptr] + exact inWin j hj + · rw [k] at hk + rcases hk with hk | hk + · cases hk + · have h1 := hk.1 + have e := physA_eq capability.Cap.Null + simp only [objects.cap_is_physical] at e + rw [← okinj e] at h1 + cases h1 + · rw [ez] at kz + rw [ey] at fy + have := kz.2.2 + rw [utNewCap_frA nt.val (bjA aligned (zA nt.val us) l) (bjA aligned (zA nt.val us) j), fy] at this + cases this + · rcases split y hy with ⟨old, k⟩ | ⟨nfi, k⟩ | ⟨j, hj, k⟩ | k + · exact Or.inl ⟨y, old, k.symm⟩ + · rw [k] at hc; cases hc + · rw [k] at hc; exact absurd hc (utNewCap_notDevFrA _ _ _ _ _ _ _ _ _ _) + · rw [k] at hc; cases hc + · cases h : H r with + | some k => exact U.grow r k (by rw [hH1]; exact h) + | none => + cases h' : H' r with + | none => rfl + | some k => + rcases U.ex r k h' with old | ⟨-, j, hj, e, -⟩ + · rw [hH1, h] at old; cases old + · exact absurd (by rw [e]; exact inWin j hj) hr + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtApIpcA.lean b/proofs/Fl4ProofsA64/P3/UtApIpcA.lean new file mode 100644 index 000000000..416b46293 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtApIpcA.lean @@ -0,0 +1,757 @@ +/- +P3 (aarch64, A64-P3 Untyped, full-record post, discharge): **the IPC bundle `UtAsmIpcInA` of a RAM creation +without a reset**, from the record before the creation (`JStage3A`), the creation's end facts (`CrtE`, the sharp +write frame), the core post `JAtZAX` at the grown sets, any outcome of the spec body and the new objects' words +(zero but for a new TCB's registers). + +Hypotheses: the window arithmetic (`W`, `h1`, `h2`), the creation's facts (`Ec`, `w`, `K`, `hex`: the grown heap's +new entries are the new objects), the new TCB words (`WT`, `Rg`, both from `utRetype_tcbCarryA` / +`liveRWH`), the slot sites of the body (`hsrcS`, `hdst`: CNode slots of `th`, a destination CNode of nonzero +radix), the global frame (`curDom`) and +* `nodeHi` (record edit #5, Architect-approved 2026-12-04, to be projected from `IrqRelA.nodeHi` once it lands): + the spec's IRQ node of an IRQ `≥ 5376` is FL4's IRQ-node slot. + +New theorems only. +-/ +import Fl4ProofsA64.P3.UtAsmIpcA +import Fl4ProofsA64.P3.UtApSpecObjA +import Fl4ProofsA64.P3.UtRtRdA +import Fl4ProofsA64.P3.UtRtPostA +import Fl4ProofsA64.P3.UtRtZeroPostA +import Fl4ProofsA64.P3.UtRtTcbWordsA +import Fl4ProofsA64.P3.UtTcbScA +import Fl4ProofsA64.P3.UtObjA +import Fl4ProofsA64.P3.JBootA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +set_option maxRecDepth 100000 + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H H' : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s s' : S} {a : A} {σ : State Unit} {m : McState} {x p sb fi : U64} + {nt us aligned dest off win : U64} + +/-- A slot of a CNode of the typed heap is no TCB's buffer slot. -/ +theorem utRt_sbNeCnA {th : TypedHeapA} {kb u q : U64} (hP : PlaceA th kb) (hu : th u = some .tcb) + {sl : CslotPtr} (hs : sl.1 = specPtr q) (hq : th q = some (.cnode sl.2.length)) : sbWordA u ≠ cslotAddrA sl := by + obtain ⟨e, hb, -⟩ := cn_slotV hP hq rfl + have fq := hP.fit q _ hq + have fu := hP.fit u _ hu + simp only [ObjKindA.size] at fq fu + have e5 : 2 ^ (sl.2.length + 5) = 2 ^ sl.2.length * 32 := by rw [pow_add]; norm_num + have v : (cslotAddrA sl).val = q.val + 32 * blvA sl.2 := by + unfold cslotAddrA + rw [hs, e] + exact slotAddrA_valN (by omega) + have sbv : (sbWordA u).val = u.val + 64 := by + rw [← bufSlot_eqA] + exact slotAddrA_valN (by omega) + have ne : u ≠ q := fun h => by + rw [h, hq] at hu + cases hu + have d := hP.apart u q .tcb _ hu hq ne + simp only [ObjKindA.size] at d + intro h + have g := congrArg UScalar.val h + rw [sbv, v] at g + rcases d with d | d <;> omega + +/-- The default object of a creation, an endpoint where nothing is created. -/ +def utNewDA (t : ApiObjectType) (n : Nat) (d : Domain) : KernelObject := + if t = .untyped then .endpoint defaultEp else defaultObject t false n d + +theorem utNewDA_ne {t : ApiObjectType} (h : t ≠ .untyped) (n : Nat) (d : Domain) : + utNewDA t n d = defaultObject t false n d := ite_eq_right h + +/-- A new default TCB is no idle TCB. -/ +theorem utNewDA_tcbA {t : ApiObjectType} {n : Nat} {d : Domain} {x : Tcb} (h : utNewDA t n d = .tcb x) : + x = defaultTcb d := by + unfold utNewDA at h + split_ifs at h with g + cases t with + | untyped => exact absurd rfl g + | tcbObject => exact (KernelObject.tcb.inj h).symm + | capTableObject => exact Fl4SpecA64.KernelObject.noConfusion h + | endpointObject => exact Fl4SpecA64.KernelObject.noConfusion h + | notificationObject => exact Fl4SpecA64.KernelObject.noConfusion h + | schedContextObject => exact Fl4SpecA64.KernelObject.noConfusion h + | replyObject => exact Fl4SpecA64.KernelObject.noConfusion h + | archObject o => exact Fl4SpecA64.KernelObject.noConfusion h + +/-- A new default SC is the default SC, and its type the SC type. -/ +theorem utNewDA_scA {t : ApiObjectType} {n : Nat} {d : Domain} {y : SchedContext} {k : Nat} + (h : utNewDA t n d = .schedContext y k) : t = .schedContextObject ∧ y = defaultSchedContext := by + unfold utNewDA at h + split_ifs at h with g + cases t with + | untyped => exact absurd rfl g + | schedContextObject => exact ⟨rfl, (KernelObject.schedContext.inj h).1.symm⟩ + | capTableObject => exact Fl4SpecA64.KernelObject.noConfusion h + | endpointObject => exact Fl4SpecA64.KernelObject.noConfusion h + | notificationObject => exact Fl4SpecA64.KernelObject.noConfusion h + | tcbObject => exact Fl4SpecA64.KernelObject.noConfusion h + | replyObject => exact Fl4SpecA64.KernelObject.noConfusion h + | archObject o => exact Fl4SpecA64.KernelObject.noConfusion h + +/-- A new default notification is bound to no SC. -/ +theorem utNewDA_ntfnA {t : ApiObjectType} {n : Nat} {d : Domain} {y : Fl4SpecA64.Notification} + (h : utNewDA t n d = .notification y) : y = defaultNotification := by + unfold utNewDA at h + split_ifs at h with g + cases t with + | untyped => exact absurd rfl g + | notificationObject => exact (KernelObject.notification.inj h).symm + | capTableObject => exact Fl4SpecA64.KernelObject.noConfusion h + | endpointObject => exact Fl4SpecA64.KernelObject.noConfusion h + | schedContextObject => exact Fl4SpecA64.KernelObject.noConfusion h + | tcbObject => exact Fl4SpecA64.KernelObject.noConfusion h + | replyObject => exact Fl4SpecA64.KernelObject.noConfusion h + | archObject o => exact Fl4SpecA64.KernelObject.noConfusion h + +/-- A null spec cap relates to a null FL4 cap only. -/ +theorem utCapRel_nullA {c : capability.Cap} (h : CapRelA c .nullCap) : c = .Null := by + have w := h.1 + rcases h.2.eq_or_pt with e | ⟨_, _, _, e⟩ + · cases c <;> first | rfl | (simp [CapWfA] at w; done) | (simp [absCapA] at e; done) + · exact Fl4SpecA64.Cap.noConfusion e + +/-- The bases of the new objects of type number `k`. -/ +def UtOfTyA (k : Nat) (nt us aligned : U64) (win : Nat) : Set U64 := + {q | nt.val = k ∧ q ∈ utBasesA aligned (zA nt.val us) win} + +variable (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) +include J + +/-- **No word the endpoint or notification relation reads is a TCB's buffer slot.** -/ +theorem utRt_sbOffA {b : U64} (h : EFoot Z.T E Rp Bf b ∨ NFoot N Z.T b) : + ∀ t ∈ Z.T, b ≠ sbWordA t ∧ b ≠ wa (sbWordA t) 8#u64 := by + intro t ht + have tf : ∀ u ∈ Z.T, u.val + 4096 ≤ 2 ^ 64 := fun u hu => (ut_tcbThA J hu).2 + have sbv : (sbWordA t).val = t.val + 64 := by + rw [← bufSlot_eqA] + exact slotAddrA_valN (by have := tf t ht; omega) + have sbv8 : (wa (sbWordA t) 8#u64).val = t.val + 72 := by + rw [wa_valP, sbv, show (8#u64 : U64).val = 8 from rfl] + exact Nat.mod_eq_of_lt (by have := tf t ht; omega) + suffices g : b.val < t.val + 64 ∨ t.val + 80 ≤ b.val by + refine ⟨fun e => ?_, fun e => ?_⟩ <;> (rw [e] at g; omega) + -- two TCBs are apart + have apart : ∀ u ∈ Z.T, u ≠ t → u.val + 4096 ≤ t.val ∨ t.val + 4096 ≤ u.val := fun u hu ne => by + have := J.j.1.place.apart u t .tcb .tcb (ut_tcbThA J hu).1 (ut_tcbThA J ht).1 ne + simpa [ObjKindA.size] using this + have field : ∀ u ∈ Z.T, ∀ k : Nat, b.val = u.val + k → 256 ≤ k → k + 8 ≤ 4096 → + b.val < t.val + 64 ∨ t.val + 80 ≤ b.val := by + intro u hu k e h1 h2 + by_cases eu : u = t + · subst eu; right; omega + · rcases apart u hu eu with g | g <;> omega + have fieldK : ∀ u ∈ Z.T, ∀ K : U64, 256 ≤ K.val → K.val + 8 ≤ 4096 → b = wa u K → + b.val < t.val + 64 ∨ t.val + 80 ≤ b.val := by + intro u hu K h1 h2 e + refine field u hu K.val ?_ h1 h2 + rw [e, wa_valP] + exact Nat.mod_eq_of_lt (by have := tf u hu; omega) + rcases h with h | h + · rcases h with ⟨e, he, hv⟩ | ⟨r, hr, hv⟩ | ⟨u, hu, hv⟩ | hv + · rcases J.iplaced.e.et e he t ht with g | g <;> omega + · rcases J.iplaced.e.rt r hr t ht with g | g <;> omega + · rcases hv with ⟨h1, h2⟩ | h2 + · have : b.val - u.val + u.val = b.val := Nat.sub_add_cancel h1 + refine field u hu (b.val - u.val) (by omega) ?_ ?_ <;> (unfold TOffN at h2; omega) + · rcases h2 with h2 | h2 + · exact field u hu 3496 h2 (by omega) (by omega) + · exact field u hu 3504 h2 (by omega) (by omega) + · rcases J.iplaced.e.bt b hv t ht with g | g <;> omega + · rcases h with ⟨c, hc, h1, h2⟩ | ⟨u, hu, e⟩ + · rcases J.iplaced.n.disj c hc t ht with g | g <;> omega + · rcases e with e | e | e | e | e | e + · rw [stA_eq] at e; exact fieldK u hu _ (by simp) (by simp) e + · rw [tBlockA_eq] at e; exact fieldK u hu _ (by simp) (by simp) e + · rw [badgeRegA_eq] at e; exact fieldK u hu _ (by simp) (by simp) e + · rw [prioA_eq'] at e; exact fieldK u hu _ (by simp) (by simp) e + · have ta : TcbAddr u := J.sq.tcbs u (by rw [J.tcbs]; exact hu) + refine field u hu 3496 ?_ (by omega) (by omega) + rw [e, nextA_val ta]; rfl + · have ta : TcbAddr u := J.sq.tcbs u (by rw [J.tcbs]; exact hu) + refine field u hu 3504 ?_ (by omega) (by omega) + rw [e, prevA_val ta]; rfl + +variable (hx : x ∈ OS) (hc : capA st s x = .Untyped p sb fi false) + (W : UtWinZA nt aligned (zA nt.val us) win.val) + (h1 : p.val + fi.val ≤ aligned.val) + (h2 : aligned.val + win.val * 2 ^ (zA nt.val us).val ≤ p.val + 2 ^ sb.val) + (Ec : CrtE st H H.frame OS s s' x p sb false aligned nt us dest off win) + (w : WritesIn st s s' (UtCrtWA st H OS s nt aligned (zA nt.val us) win.val)) + +include hx hc W h1 h2 + +/-- The new objects are no objects of the old typed heap. -/ +theorem utRt_ipcFreeA : ∀ q, UtNewA false nt us aligned win.val q → Z.th q = none := by + have Cl : UtClrA st Z H s aligned.val (aligned.val + win.val * 2 ^ (zA nt.val us).val) := + utRt_winClrA J hx hc h1 h2 + have anchor : ∀ j, j < win.val → specPtr (bjA aligned (zA nt.val us) j) ≠ Fl4SpecA64.idleScPtr := by + intro j hj + have v := utRt_newValA W hj + exact utRet_anchorA (st := st) J.idleAnchor hx hc (by omega) (by omega) + rintro q ⟨-, -, -, j, hj, rfl⟩ + by_contra h + exact utRt_relOffA (m := m) J Cl W rfl rfl anchor (Or.inl ⟨_, h, rfl⟩) ⟨j, hj, rfl⟩ + +/-- A new object's address is free in the spec. -/ +theorem utRt_ipcSpecFreeA {q : U64} (hq : q ∈ utBasesA aligned (zA nt.val us) win.val) : + σ.kheap (specPtr q) = none := by + obtain ⟨j, hj, rfl⟩ := hq + have v := utRt_newValA W hj + exact J.specFree.heap _ ⟨x, hx, p, sb, fi, false, hc, trivial, by omega, by omega⟩ + +include Ec + +/-- A word of a new object of a kind writing nothing is zero. -/ +theorem utRt_newZeroA (wk : ¬ WKA nt) {j : Nat} (hj : j < win.val) (h3 : 3 ≤ (zA nt.val us).val) {b : U64} + {o : Nat} (o8 : o % 8 = 0) (ob : o + 8 ≤ 2 ^ (zA nt.val us).val) + (e : b.val = (bjA aligned (zA nt.val us) j).val + o) : L.mem s' b = 0#u64 := by + have _ := hx + have _ := hc + have _ := h1 + have _ := h2 + have h1' : (j + 1) * 2 ^ (zA nt.val us).val ≤ win.val * 2 ^ (zA nt.val us).val := Nat.mul_le_mul_right _ hj + rw [Nat.add_mul, Nat.one_mul] at h1' + have fit := W.fit + have ev : (bjA aligned (zA nt.val us) j).val = aligned.val + j * 2 ^ (zA nt.val us).val := bjA_val (by + have := Nat.two_pow_pos (zA nt.val us).val + omega) + have al : (bjA aligned (zA nt.val us) j).val % 8 = 0 := by + have d8 : 8 ∣ 2 ^ (zA nt.val us).val := Nat.pow_dvd_pow 2 h3 + have da : 8 ∣ aligned.val := d8.trans (Nat.dvd_of_mod_eq_zero W.al) + rw [ev] + exact Nat.mod_eq_zero_of_dvd (Nat.dvd_add da (Dvd.dvd.mul_left d8 j)) + refine Ec.zero rfl wk b ?_ (by omega) (by omega) + show _ % 8 = 0 + omega + +omit Ec in +/-- The value of a word at a fixed offset of a new object. -/ +theorem utRt_newWordA {j : Nat} (hj : j < win.val) (K : U64) {k : Nat} (ek : K.val = k) + (hk : k < 2 ^ (zA nt.val us).val) : + (wa (bjA aligned (zA nt.val us) j) K).val = (bjA aligned (zA nt.val us) j).val + k := by + have _ := hx + have _ := hc + have _ := h1 + have _ := h2 + have h1' : (j + 1) * 2 ^ (zA nt.val us).val ≤ win.val * 2 ^ (zA nt.val us).val := Nat.mul_le_mul_right _ hj + rw [Nat.add_mul, Nat.one_mul] at h1' + have fit := W.fit + have ev : (bjA aligned (zA nt.val us) j).val = aligned.val + j * 2 ^ (zA nt.val us).val := bjA_val (by + have := Nat.two_pow_pos (zA nt.val us).val + omega) + rw [wa_valP, ek] + exact Nat.mod_eq_of_lt (by omega) + +end + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H H' : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s s' : S} {a a' : A} {σ : State Unit} {m : McState} {x p sb fi : U64} + {nt us aligned dest off win : U64} {G' : MdbGhostA} {Tn0 Cn0 Vn0 : Set U64} + +set_option maxHeartbeats 4000000 in +/-- **The IPC bundle of a RAM creation without a reset** (module docstring). -/ +theorem utRt_ipcInA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (hx : x ∈ OS) (hc : capA st s x = .Untyped p sb fi false) + (W : UtWinZA nt aligned (zA nt.val us) win.val) + (h1 : p.val + fi.val ≤ aligned.val) + (h2 : aligned.val + win.val * 2 ^ (zA nt.val us).val ≤ p.val + 2 ^ sb.val) + (Ec : CrtE st H H.frame OS s s' x p sb false aligned nt us dest off win) + (w : WritesIn st s s' (UtCrtWA st H OS s nt aligned (zA nt.val us) win.val)) + (hex : ∀ q k, H' q = some k → H q = some k ∨ + ∃ j < win.val, q = bjA aligned (zA nt.val us) j ∧ newKind nt.val us.val = some k) + -- the new TCBs' words + (WT : nt.val = 1 → WinT st s' aligned (zA nt.val us) win.val) + (Rg : nt.val = 1 → UtTcbRegW st aligned (zA nt.val us) win.val s') + -- the spec body + {t : ApiObjectType} (htt : objTypeOfA nt.val = some t) (hus : us.val < 64) + {srcSlot : CslotPtr} (hxc : cslotAddrA srcSlot = x) + (hsrcS : ∃ q, srcSlot.1 = specPtr q ∧ Z.th q = some (.cnode srcSlot.2.length)) + {slots : List CslotPtr} (hlen : slots.length = win.val) + (hdst : ∀ d ∈ slots, ∃ q, d.1 = specPtr q ∧ Z.th q = some (.cnode d.2.length) ∧ d.2.length ≠ 0) + (hdstA : ∀ j < win.val, ∃ d ∈ slots, djA dest off j = cslotAddrA d) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + {r : Unit × State Unit} + (hr : r ∈ (utRtBodyA srcSlot (specPtr p) (specPtr aligned) t us.val slots false σ).1) + -- the core post + (K : JAtZAX (L := L) AR (utRtZA Z G' nt us aligned win.val Tn0 Cn0 Vn0) s' a' r.2 + (utRtMA m (UtOfTyA 1 nt us aligned win.val))) + -- record edit #5 + (nodeHi : ∀ i : Fl4SpecA64.Irq, 5376 ≤ i.toNat → σ.interruptIrqNode i = irqNodeOfA (L.ksb s) i) : + UtAsmIpcInA st Z (utRtThA Z.th false nt us aligned win.val) H H' N E Rp Bf + (UtOfTyA 3 nt us aligned win.val) (UtOfTyA 2 nt us aligned win.val) (UtOfTyA 6 nt us aligned win.val) + (UtOfTyA 1 nt us aligned win.val) (UtOfTyA 5 nt us aligned win.val) (UtNewA false nt us aligned win.val) + s s' σ r.2 m (utRtMA m (UtOfTyA 1 nt us aligned win.val)) + (fun q => t ≠ .untyped ∧ UtNewSpA aligned (zA nt.val us) win.val q) + (utNewDA t us.val σ.curDomain) := by + have hn : nt.val < 15 := (objTypeOfA_some htt us.val).1 + have Cl : UtClrA st Z H s aligned.val (aligned.val + win.val * 2 ^ (zA nt.val us).val) := + utRt_winClrA J hx hc h1 h2 + have anchor : ∀ j, j < win.val → specPtr (bjA aligned (zA nt.val us) j) ≠ Fl4SpecA64.idleScPtr := by + intro j hj + have v := utRt_newValA W hj + exact utRet_anchorA (st := st) J.idleAnchor hx hc (by omega) (by omega) + have thFree : ∀ q, UtNewA false nt us aligned win.val q → Z.th q = none := utRt_ipcFreeA J hx hc W h1 h2 + have memA : ∀ q, q ∈ fl4RetypeAddrs fl4ObjBitsApi (specPtr aligned) slots.length us.val t ↔ + UtNewSpA aligned (zA nt.val us) win.val q := fun q => by + rw [hlen, utRet_addrs_memA htt hus] + rfl + have thOff : ∀ q k, Z.th q = some k → ¬ UtNewSpA aligned (zA nt.val us) win.val (specPtr q) := fun q k hq => + utRt_relOffA (m := m) J Cl W rfl rfl anchor (Or.inl ⟨q, by rw [hq]; simp, rfl⟩) + have specFree : ∀ q, UtNewSpA aligned (zA nt.val us) win.val q → σ.kheap q = none := by + rintro q ⟨j, hj, rfl⟩ + exact utRt_ipcSpecFreeA J hx hc W h1 h2 ⟨j, hj, rfl⟩ + have newTh : ∀ k j, j < win.val → nt.val = k → k ≠ 0 → k ≠ 5 ∧ k ≠ 8 ∧ k ≠ 11 ∧ k ≠ 13 ∧ k ≠ 14 → + UtNewA false nt us aligned win.val (bjA aligned (zA nt.val us) j) := fun k j hj e k0 k5 => + ⟨rfl, by omega, by omega, j, hj, rfl⟩ + -- the spec outcome + obtain ⟨qS, eqS, hqS⟩ := hsrcS + obtain ⟨csS, hkS, hSwf, hsS⟩ := J.j.1.st.cnode qS _ hqS + rw [← eqS] at hkS + obtain ⟨capS, hcapS, hrelS⟩ := hsS srcSlot.2 rfl + have eS : ∃ bb ii : Nat, capS = .untypedCap false (specPtr p) bb ii := by + have ea : slotAddrA (specPtr qS) (Fl4SpecA64.ofBl srcSlot.2) = x := by rw [← hxc]; unfold cslotAddrA; rw [eqS] + rw [ea, capAtA_eq, hc] at hrelS + exact ⟨_, _, (show absCapA (.Untyped p sb fi false) = capS from hrelS.2).symm⟩ + obtain ⟨bS, iS, eS⟩ := eS + have hdc : ∀ d ∈ slots, (∃ sz cn, σ.kheap d.1 = some (.cnode sz cn)) ∧ + d.1 ∉ fl4RetypeAddrs fl4ObjBitsApi (specPtr aligned) slots.length us.val t := by + intro d hd + obtain ⟨q, eq, hq, -⟩ := hdst d hd + obtain ⟨cs, hk, -⟩ := J.j.1.st.cnode q _ hq + rw [eq, memA] + exact ⟨⟨_, cs, hk⟩, thOff q _ hq⟩ + have hSa : srcSlot.1 ∉ fl4RetypeAddrs fl4ObjBitsApi (specPtr aligned) slots.length us.val t := by + rw [eqS, memA] + exact thOff qS _ hqS + have O0 := utRtBody_newObjA hr hkS hcapS hSa hdc (fun q hq _ => specFree q ((memA q).1 hq)) + have O : UtNewObjA σ r.2 (fun q => t ≠ .untyped ∧ UtNewSpA aligned (zA nt.val us) win.val q) + (utNewDA t us.val σ.curDomain) := + { fresh := fun q g => O0.fresh q ⟨g.1, (memA q).2 g.2⟩ + new := fun q g => by + rw [utNewDA_ne g.1] + exact O0.new q ⟨g.1, (memA q).2 g.2⟩ + keep := O0.keep + back := fun q ko h ne => (O0.back q ko h ne).imp id fun g => by + rw [utNewDA_ne g.1.1] + exact ⟨⟨g.1.1, (memA q).1 g.1.2⟩, g.2⟩ + glob := O0.glob + um := O0.um } + -- a new object of the spec heap + have newObj : t ≠ .untyped → ∀ j, j < win.val → + r.2.kheap (specPtr (bjA aligned (zA nt.val us) j)) = some (defaultObject t false us.val σ.curDomain) := by + intro g j hj + exact O0.new _ ⟨g, (memA _).2 ⟨j, hj, rfl⟩⟩ + have tyOf : ∀ k, nt.val = k → objTypeOfA k = some t := fun k e => e ▸ htt + -- the words the relation reads are kept + have rdK : ∀ b : U64, Aligned b → UtRdBA st Z N E Rp Bf b → + (∀ u ∈ Z.T, b ≠ sbWordA u ∧ b ≠ wa (sbWordA u) 8#u64) → L.mem s' b = L.mem s b := + fun b al h g => utRt_rdKeptA J hx hc h1 h2 w al (Or.inl ⟨h, g⟩) + have footE : Endp.FootEq Z.T E Rp Bf (L.mem s) (L.mem s') := fun b al h => + rdK b al (Or.inl h) (utRt_sbOffA J (Or.inl h)) + have footN : Notif.FootEq N Z.T (L.mem s) (L.mem s') := fun b al h => + rdK b al (Or.inr (Or.inl h)) (utRt_sbOffA J (Or.inr h)) + have tf : ∀ u ∈ Z.T, u.val + 4096 ≤ 2 ^ 64 := fun u hu => (ut_tcbThA J hu).2 + have sbv : ∀ u ∈ Z.T, (sbWordA u).val = u.val + 64 := by + intro u hu + rw [← bufSlot_eqA] + exact slotAddrA_valN (by have := tf u hu; omega) + -- a word of an old object away from every buffer slot is kept + have keptW : ∀ b : U64, Aligned b → UtRdBA st Z N E Rp Bf b → + (∀ u ∈ Z.T, b.val < u.val + 64 ∨ u.val + 80 ≤ b.val) → L.mem s' b = L.mem s b := by + intro b al h g + refine rdK b al h (fun u hu => ?_) + have v := sbv u hu + have v8 : (wa (sbWordA u) 8#u64).val = u.val + 72 := by + rw [wa_valP, v, show (8#u64 : U64).val = 8 from rfl] + exact Nat.mod_eq_of_lt (by have := tf u hu; omega) + have g' := g u hu + refine ⟨fun e => ?_, fun e => ?_⟩ <;> (rw [e] at g'; omega) + have tcbW : ∀ u ∈ Z.T, ∀ K : U64, 224 ≤ K.val → K.val < 4096 → K.val % 8 = 0 → + UtRdBA st Z N E Rp Bf (wa u K) → L.mem s' (wa u K) = L.mem s (wa u K) := by + intro u hu K k1 k2 k8 g + have fu := tf u hu + have v : (wa u K).val = u.val + K.val := by rw [wa_valP]; exact Nat.mod_eq_of_lt (by omega) + have al : Aligned (wa u K) := by + have := J.j.1.place.align u _ (ut_tcbThA J hu).1 + simp only [ObjKindA.size] at this + show _ % 8 = 0 + omega + refine keptW _ al g (fun t' ht' => ?_) + by_cases e : u = t' + · subst e; right; omega + · have := J.j.1.place.apart u t' .tcb .tcb (ut_tcbThA J hu).1 (ut_tcbThA J ht').1 e + simp only [ObjKindA.size] at this + rcases this with q | q <;> omega + have linkW : ∀ rp ∈ Rp, ∀ K : U64, K = 8#u64 ∨ K = 16#u64 → + (wa rp K = rNextA rp ∨ wa rp K = rPrevA rp) → L.mem s' (wa rp K) = L.mem s (wa rp K) := by + intro rp hrp K hK hw + have R := J.iplaced.e.rep rp hrp + have K8 : K.val = 8 ∨ K.val = 16 := by rcases hK with rfl | rfl <;> simp + have v : (wa rp K).val = rp.val + K.val := by rw [wa_valP]; exact Nat.mod_eq_of_lt (by omega) + have al : Aligned (wa rp K) := by show _ % 8 = 0; omega + refine keptW _ al (Or.inr (Or.inr (Or.inl ⟨rp, hrp, hw⟩))) (fun u hu => ?_) + rcases J.iplaced.e.rt rp hrp u hu with g | g <;> omega + -- the buffer slot's cap is kept + have sbSlot : ∀ u ∈ Z.T, IsSlotA Z.th (sbWordA u) := fun u hu => + ⟨u, Or.inr ⟨(ut_tcbThA J hu).1, 2, by simp [tcbSlotsA], (bufSlot_eqA u).symm⟩⟩ + have sbCap : ∀ u ∈ Z.T, capA st s' (sbWordA u) = capA st s (sbWordA u) := by + intro u hu + refine Ec.keep _ (hsub _ (sbSlot u hu)) ?_ ?_ + · rw [← hxc] + exact utRt_sbNeCnA J.j.1.place (ut_tcbThA J hu).1 eqS hqS + · intro j hj + obtain ⟨d, hd, ed⟩ := hdstA j hj + obtain ⟨q, eq, hq, -⟩ := hdst d hd + rw [ed] + exact utRt_sbNeCnA J.j.1.place (ut_tcbThA J hu).1 eq hq + -- the new words are zero + have zA2 : nt.val = 2 → (zA nt.val us).val = 4 := fun h => by rw [zA_valA nt us hn hus, h]; rfl + have zA3 : nt.val = 3 → (zA nt.val us).val = 6 := fun h => by rw [zA_valA nt us hn hus, h]; rfl + have zA6 : nt.val = 6 → (zA nt.val us).val = 5 := fun h => by rw [zA_valA nt us hn hus, h]; rfl + have zw : ¬ WKA nt → ∀ j, j < win.val → 3 ≤ (zA nt.val us).val → ∀ (K : U64) (k : Nat), K.val = k → + k % 8 = 0 → k + 8 ≤ 2 ^ (zA nt.val us).val → L.mem s' (wa (bjA aligned (zA nt.val us) j) K) = 0#u64 := by + intro wk j hj h3 K k ek k8 kb + exact utRt_newZeroA J hx hc W h1 h2 Ec wk hj h3 k8 kb (utRt_newWordA J hx hc W h1 h2 hj K ek (by omega)) + have nk : ∀ k, nt.val = k → k ≠ 1 → k ≠ 12 → k ≠ 14 → ¬ WKA nt := fun k e a b c => by + unfold WKA + omega + have newN : ∀ rr ∈ UtOfTyA 3 nt us aligned win.val, r.2.kheap (specPtr rr) = some (.notification defaultNotification) ∧ + L.mem s' (epHeadA rr) = 0#u64 ∧ L.mem s' (epTSA rr) = 0#u64 ∧ L.mem s' (nBoundA rr) = 0#u64 ∧ + L.mem s' (nScA rr) = 0#u64 := by + rintro rr ⟨e3, j, hj, rfl⟩ + have z := zA3 e3 + have et : t = .notificationObject := by + have h := tyOf 3 e3 + simp [objTypeOfA, eq_comm] at h + exact h + have o := newObj (by rw [et]; simp) j hj + rw [et] at o + have wk := nk 3 e3 (by omega) (by omega) (by omega) + have zz : ∀ (K : U64) (k : Nat), K.val = k → k % 8 = 0 → k + 8 ≤ 64 → + L.mem s' (wa (bjA aligned (zA nt.val us) j) K) = 0#u64 := fun K k ek k8 kb => + zw wk j hj (by omega) K k ek k8 (by rw [z]; omega) + refine ⟨o, ?_, ?_, ?_, ?_⟩ + · rw [Notif.epHeadA_eq]; exact zz _ 0 rfl (by omega) (by omega) + · rw [Notif.epTSA_eq]; exact zz _ 8 rfl (by omega) (by omega) + · rw [nBoundA_eq]; exact zz _ 24 rfl (by omega) (by omega) + · rw [nScA_eq]; exact zz _ 32 rfl (by omega) (by omega) + have newE : ∀ e ∈ UtOfTyA 2 nt us aligned win.val, r.2.kheap (specPtr e) = some (.endpoint defaultEp) ∧ + L.mem s' (epHeadA e) = 0#u64 ∧ L.mem s' (epTSA e) = 0#u64 := by + rintro rr ⟨e2, j, hj, rfl⟩ + have z := zA2 e2 + have et : t = .endpointObject := by + have h := tyOf 2 e2 + simp [objTypeOfA, eq_comm] at h + exact h + have o := newObj (by rw [et]; simp) j hj + rw [et] at o + have wk := nk 2 e2 (by omega) (by omega) (by omega) + have zz : ∀ (K : U64) (k : Nat), K.val = k → k % 8 = 0 → k + 8 ≤ 16 → + L.mem s' (wa (bjA aligned (zA nt.val us) j) K) = 0#u64 := fun K k ek k8 kb => + zw wk j hj (by omega) K k ek k8 (by rw [z]; omega) + refine ⟨o, ?_, ?_⟩ + · rw [Notif.epHeadA_eq]; exact zz _ 0 rfl (by omega) (by omega) + · rw [Notif.epTSA_eq]; exact zz _ 8 rfl (by omega) (by omega) + have newR : ∀ rr ∈ UtOfTyA 6 nt us aligned win.val, r.2.kheap (specPtr rr) = some (.reply defaultReply) ∧ + L.mem s' (Endp.rTcbA rr) = 0#u64 ∧ L.mem s' (rNextA rr) = 0#u64 ∧ L.mem s' (rPrevA rr) = 0#u64 := by + rintro rr ⟨e6, j, hj, rfl⟩ + have z := zA6 e6 + have et : t = .replyObject := by + have h := tyOf 6 e6 + simp [objTypeOfA, eq_comm] at h + exact h + have o := newObj (by rw [et]; simp) j hj + rw [et] at o + have wk := nk 6 e6 (by omega) (by omega) (by omega) + have zz : ∀ (K : U64) (k : Nat), K.val = k → k % 8 = 0 → k + 8 ≤ 32 → + L.mem s' (wa (bjA aligned (zA nt.val us) j) K) = 0#u64 := fun K k ek k8 kb => + zw wk j hj (by omega) K k ek k8 (by rw [z]; omega) + refine ⟨o, ?_, ?_, ?_⟩ + · have e0 : Endp.rTcbA (bjA aligned (zA nt.val us) j) = wa (bjA aligned (zA nt.val us) j) 0#u64 := by + unfold Endp.rTcbA layout.reply.TCB; rfl + rw [e0]; exact zz _ 0 rfl (by omega) (by omega) + · have e0 : rNextA (bjA aligned (zA nt.val us) j) = wa (bjA aligned (zA nt.val us) j) 16#u64 := by + unfold rNextA layout.reply.NEXT; rfl + rw [e0]; exact zz _ 16 rfl (by omega) (by omega) + · have e0 : rPrevA (bjA aligned (zA nt.val us) j) = wa (bjA aligned (zA nt.val us) j) 8#u64 := by + unfold rPrevA layout.reply.PREV; rfl + rw [e0]; exact zz _ 8 rfl (by omega) (by omega) + -- the new TCBs + have newT : ∀ tt ∈ UtOfTyA 1 nt us aligned win.val, ∃ y, r.2.kheap (specPtr tt) = some (.tcb y) ∧ + TcbRelE (Rp ∪ UtOfTyA 6 nt us aligned win.val) (L.mem s') tt y ∧ TcbRel1 (L.mem s') tt y ∧ + y.tcbMcpriority = 0 ∧ L.mem s' (mcpA tt) = 0#u64 ∧ y.tcbYieldTo = none ∧ L.mem s' (yieldA tt) = 0#u64 ∧ + y.tcbIpcframe = .nullCap ∧ y.tcbIpcBuffer = 0 ∧ L.mem s' (flagsA tt) = 0#u64 ∧ y.tcbFlags = ∅ := by + rintro tt ⟨e1, j, hj, rfl⟩ + have et : t = .tcbObject := by + have h := tyOf 1 e1 + simp [objTypeOfA, eq_comm] at h + exact h + have o := newObj (by rw [et]; simp) j hj + rw [et] at o + have ez : (zA nt.val us).val = 12 := by rw [e1]; exact zA_1 us + have wz : ∀ (K : U64) (k : Nat), K.val = k → k < 4096 → k % 8 = 0 → ¬ TcbInitK k → + L.mem s' (wa (bjA aligned (zA nt.val us) j) K) = 0#u64 := by + intro K k ek hk k8 ni + have wv := utRt_newWordA J hx hc W h1 h2 hj K ek (by rw [ez]; omega) + have bv := utRt_newValA W hj + have h1' : (j + 1) * 2 ^ (zA nt.val us).val ≤ win.val * 2 ^ (zA nt.val us).val := Nat.mul_le_mul_right _ hj + rw [Nat.add_mul, Nat.one_mul] at h1' + have fit := W.fit + have ev : (bjA aligned (zA nt.val us) j).val = aligned.val + j * 2 ^ (zA nt.val us).val := bjA_val (by + have := Nat.two_pow_pos (zA nt.val us).val + omega) + have al : aligned.val % 4096 = 0 := by + have g := W.al + rwa [ez] at g + rw [ez] at h1' ev + have X : (2 : Nat) ^ 12 = 4096 := by norm_num + rw [X] at h1' ev + have hal : Aligned (wa (bjA aligned (zA nt.val us) j) K) := by + show _ % 8 = 0 + rw [wv, ev] + omega + have h2' : aligned.val + win.val * 2 ^ (zA nt.val us).val = aligned.val + win.val * 4096 := by rw [ez]; norm_num + rcases WT e1 _ hal (by rw [wv, ev]; omega) (by rw [wv, ev, h2']; omega) with g | ⟨i, -, K', hK, e⟩ + · exact g + · rw [wv, ev, ez, X] at e + have hK4 : K' < 4096 := hK.2 + have : K' = k := by omega + exact absurd (this ▸ hK) ni + have hst : L.mem s' (stA (bjA aligned (zA nt.val us) j)) = 0#u64 := by + rw [stA_eq]; exact wz _ 3328 rfl (by omega) (by omega) (by unfold TcbInitK; omega) + have hprio : L.mem s' (prioA (bjA aligned (zA nt.val us) j)) = 0#u64 := by + rw [prioA_eq']; exact wz _ 3448 rfl (by omega) (by omega) (by unfold TcbInitK; omega) + have hfault : L.mem s' (tFaultA (bjA aligned (zA nt.val us) j)) = 0#u64 := + wz 3376#u64 3376 rfl (by omega) (by omega) (by unfold TcbInitK; omega) + have hbound : L.mem s' (tBoundA (bjA aligned (zA nt.val us) j)) = 0#u64 := + wz 3368#u64 3368 rfl (by omega) (by omega) (by unfold TcbInitK; omega) + have regs : UtRegsA (L.mem s') (bjA aligned (zA nt.val us) j) := Rg e1 j hj + have hbadge : L.mem s' (badgeRegA (bjA aligned (zA nt.val us) j)) = 0#u64 := by + have e0 : badgeRegA (bjA aligned (zA nt.val us) j) = Endp.regA (bjA aligned (zA nt.val us) j) (regIdxA .x0) := by + unfold badgeRegA Endp.regA regIdxA layout.reg.BADGE layout.reg.X0; rfl + rw [e0, regs .x0] + exact ite_eq_right (by decide) + exact ⟨_, o, ut_tcbRelA _ _ _ _ hst regs hprio hfault hbound, ut_tcbNtfnRelA _ _ _ hst hbadge hprio, + rfl, wz 3440#u64 3440 rfl (by omega) (by omega) (by unfold TcbInitK; omega), rfl, + wz 3472#u64 3472 rfl (by omega) (by omega) (by unfold TcbInitK; omega), rfl, rfl, + wz 3456#u64 3456 rfl (by omega) (by omega) (by unfold TcbInitK; omega), rfl⟩ + have newTslot : ∀ tt ∈ UtOfTyA 1 nt us aligned win.val, + capAtA (L.mem s') (slotAddrA (specPtr tt) (BitVec.ofNat 64 2)) = .Null := by + rintro tt htt1 + obtain ⟨y, o, -, -, -, -, -, -, e8, -⟩ := newT tt htt1 + obtain ⟨e1, j, hj, rfl⟩ := htt1 + have th : utRtThA Z.th false nt us aligned win.val (bjA aligned (zA nt.val us) j) = some .tcb := by + rw [utRtTh_newA (newTh 1 j hj e1 (by omega) (by omega))] + unfold utKindA + rw [ite_eq_left e1] + obtain ⟨y', o', sl⟩ := K.1.st.tcb _ th + rw [o] at o' + obtain rfl : y = y' := KernelObject.tcb.inj (Option.some.inj o') + obtain ⟨ac, e, rel⟩ := sl 2 (by simp [tcbSlotsA]) + have ec : tcbCnodeMap y (tcbCnodeIndex 2) = some y.tcbIpcframe := by + unfold tcbCnodeMap + simp [Function.update_apply, tcbIdx_eq, tcbStage2Slot, tcbHighVSpaceSlot] + rw [ec, e8] at e + rw [← Option.some.inj e] at rel + exact utCapRel_nullA rel + -- the new SCs + have idleC : specPtr (L.mem s (L.nodeAt s (L.cpu s) layout.node.IDLE_SC)) = m.idleScOf m.curCore := by + rw [J.j.1.sch.core] + exact (J.j.1.sch.idleScOf _ J.j.1.sch.cpuCs).symm + have scOffOld : ∀ c ∈ Z.C, ¬ UtNewSpA aligned (zA nt.val us) win.val (specPtr c) := by + intro c hcc + have hcs : c ∈ H.scs := by rw [J.scs]; exact hcc + exact utRt_objOffA Cl W rfl rfl (JointObjectA.of_sc hcs) (Nat.two_pow_pos _) + have newC : ∀ c ∈ UtOfTyA 5 nt us aligned win.val, ∃ k, r.2.kheap (scNameA m c) = some (.schedContext defaultSchedContext k) := by + rintro c ⟨e5, j, hj, rfl⟩ + have ne : specPtr (bjA aligned (zA nt.val us) j) ≠ m.idleScOf m.curCore := fun e => by + have ic := J.j.1.idleC _ J.j.1.sch.cpuCs + refine scOffOld _ ic ⟨j, hj, ?_⟩ + rw [idleC, e] + have en : scNameA m (bjA aligned (zA nt.val us) j) = specPtr (bjA aligned (zA nt.val us) j) := ite_eq_right ne + have et : t = .schedContextObject := (objTypeOfA_some htt us.val).2.2.2.2.2.2 e5 + have o := newObj (by rw [et]; simp) j hj + rw [et] at o + exact ⟨_, by rw [en]; exact o⟩ + have newRfresh : ∀ rr ∈ UtOfTyA 6 nt us aligned win.val, rr ∉ Rp := by + rintro rr ⟨e6, j, hj, rfl⟩ g + have := J.ipcPlace.reply _ g + rw [thFree _ (newTh 6 j hj e6 (by omega) (by omega))] at this + cases this + have newCfresh : ∀ c ∈ UtOfTyA 5 nt us aligned win.val, c ∉ Z.C := by + rintro c ⟨-, j, hj, rfl⟩ g + exact scOffOld _ g ⟨j, hj, rfl⟩ + have irq : SpecIrqA r.2 := by + refine utRtBody_irqA hr hkS J.sinv.irqN (d0 := false) (sbS := bS) (i0 := iS) (by rw [← eS]; exact hcapS) hSwf + hSa hdc rfl (fun _ i mem => ?_) (fun i hi d hd e => ?_) + · rw [memA] at mem + by_cases hi : i.toNat < 5376 + · obtain ⟨i64, ei⟩ : ∃ i64 : U64, i64.val = i.toNat := + ⟨⟨BitVec.ofNat 64 i.toNat⟩, by rw [irqU_val]⟩ + have hnode := J.j.1.irq.node i64 (by omega) + have e13 : BitVec.ofNat 13 i64.val = i := by + apply BitVec.eq_of_toNat_eq + rw [BitVec.toNat_ofNat, ei] + exact Nat.mod_eq_of_lt i.isLt + rw [e13] at hnode + rw [hnode] at mem + exact thOff _ _ (J.j.1.place.irq i64 (by omega)) mem + · rw [nodeHi i (by omega)] at mem + unfold irqNodeOfA at mem + obtain ⟨j, hj, e⟩ := mem + have fit : (L.ksb s).val + ksSizeA ≤ 2 ^ 64 := J.sys.ent.ksw + have lt : (⟨BitVec.ofNat 64 i.toNat⟩ : U64).val < 8192 := by rw [irqU_val]; exact irqU_lt i + have v := irqSlotA_val13 (kb := L.ksb s) (i := ⟨BitVec.ofNat 64 i.toNat⟩) fit lt + rw [irqU_val] at v + have eq : irqSlotA (L.ksb s) ⟨BitVec.ofNat 64 i.toNat⟩ = bjA aligned (zA nt.val us) j := ut_specPtr_injA e + rw [eq] at v + have bv := utRt_newValA W hj + have ltk := irqU_lt i + have := Cl.ks + omega + · obtain ⟨q, eq, hq, ne⟩ := hdst d hd + obtain ⟨i64, ei⟩ : ∃ i64 : U64, i64.val = i.toNat := + ⟨⟨BitVec.ofNat 64 i.toNat⟩, by rw [irqU_val]⟩ + have lt : i64.val < 5376 := by rw [ei]; exact hi + have hnode := J.j.1.irq.node i64 lt + have e13 : BitVec.ofNat 13 i64.val = i := by + apply BitVec.eq_of_toNat_eq + rw [BitVec.toNat_ofNat, ei] + exact Nat.mod_eq_of_lt i.isLt + rw [e13] at hnode + rw [eq, hnode] at e + have := J.j.1.place.irq i64 lt + rw [← ut_specPtr_injA e, hq] at this + exact ne (ObjKindA.cnode.inj (Option.some.inj this)) + exact + { spec := O + grow := + { old := fun q k h => utRtTh_growA thFree h + fresh := thFree + newN := fun q ⟨e, j, hj, eq⟩ => by + subst eq + have g := newTh 3 j hj e (by omega) (by omega) + exact ⟨g, (utRtTh_ipcA g).1 e⟩ + newE := fun q ⟨e, j, hj, eq⟩ => by + subst eq + have g := newTh 2 j hj e (by omega) (by omega) + exact ⟨g, (utRtTh_ipcA g).2.1 e⟩ + newR := fun q ⟨e, j, hj, eq⟩ => by + subst eq + have g := newTh 6 j hj e (by omega) (by omega) + exact ⟨g, (utRtTh_ipcA g).2.2 e⟩ + newT := fun q ⟨e, j, hj, eq⟩ => by + subst eq + have g := newTh 1 j hj e (by omega) (by omega) + refine ⟨g, ?_⟩ + rw [utRtTh_newA g] + unfold utKindA + rw [ite_eq_left e] + oldT := fun q h => (ut_tcbThA J h).1 } + place := w.1 ▸ K.1.place + footE := footE + footN := footN + newN := newN + newE := newE + newR := newR + newT := newT + newTslot := newTslot + newC := newC + newRfresh := newRfresh + newCfresh := newCfresh + oldReply := fun rr h => by + obtain ⟨y, e, -⟩ := J.ep.reply rr h + exact ⟨y, e⟩ + link := fun rr h => by + have e16 : rNextA rr = wa rr 16#u64 := by unfold rNextA layout.reply.NEXT; rfl + have e8 : rPrevA rr = wa rr 8#u64 := by unfold rPrevA layout.reply.PREV; rfl + rw [e16, e8] + exact ⟨linkW rr h _ (Or.inr rfl) (Or.inl e16.symm), linkW rr h _ (Or.inl rfl) (Or.inr e8.symm)⟩ + mcpW := fun u h => ⟨tcbW u h 3440#u64 (by simp) (by simp) (by simp) + (Or.inr (Or.inr (Or.inr ⟨u, h, Or.inl rfl⟩))), tcbW u h 3456#u64 (by simp) (by simp) (by simp) + (Or.inr (Or.inr (Or.inr ⟨u, h, Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr rfl)))))⟩)))⟩ + yieldW := fun u h => tcbW u h 3472#u64 (by simp) (by simp) (by simp) + (Or.inr (Or.inr (Or.inr ⟨u, h, Or.inr (Or.inl rfl)⟩))) + bufW := fun u h => ⟨tcbW u h 3512#u64 (by simp) (by simp) (by simp) + (Or.inr (Or.inr (Or.inr ⟨u, h, Or.inr (Or.inr (Or.inr (Or.inr (Or.inl rfl))))⟩))), sbCap u h⟩ + bufSlot := fun u h => by + rw [bufSlot_eqA, capAtA_eq, capAtA_eq] + exact sbCap u h + scOld := fun c h => by + obtain ⟨y, k, e, -⟩ := J.j.1.sch.sc c h + exact ⟨y, k, e⟩ + optSc := fun q => utRtMA_optSc m _ q + scName := fun _ => rfl + curCore := rfl + idleThreadOf := rfl + idleScOf := rfl + affinity := fun q c h => by + unfold utRtMA at h + simp only at h + split_ifs at h + · exact Or.inr (Option.some.inj h).symm + · exact Or.inl h + newTcbState := fun y e => by + rw [utNewDA_tcbA e] + exact fun h => by cases h + newTcbFault := fun y e => by + rw [utNewDA_tcbA e] + rfl + newScInactive := fun y k e => by + rw [(utNewDA_scA e).2] + rfl + newScNoRep := fun y k e => by + rw [(utNewDA_scA e).2] + rfl + newScName := fun q g y k e => by + obtain ⟨-, j, hj, rfl⟩ := g + have et := (utNewDA_scA e).1 + have e5 : nt.val = 5 := by + rw [et] at htt + revert htt + unfold objTypeOfA archDataToObjType + intro htt + interval_cases hv : nt.val <;> simp at htt <;> rfl + have ne : specPtr (bjA aligned (zA nt.val us) j) ≠ m.idleScOf m.curCore := fun e => by + have ic := J.j.1.idleC _ J.j.1.sch.cpuCs + refine scOffOld _ ic ⟨j, hj, ?_⟩ + rw [idleC, e] + exact ⟨_, ⟨e5, j, hj, rfl⟩, (ite_eq_right ne).symm⟩ + irq := irq + heapN := fun q h => (hex q _ h).imp id fun ⟨j, hj, e, k⟩ => ⟨by + revert k + unfold newKind + split_ifs <;> simp <;> omega, j, hj, e⟩ + heapE := fun q h => (hex q _ h).imp id fun ⟨j, hj, e, k⟩ => ⟨by + revert k + unfold newKind + split_ifs <;> simp <;> omega, j, hj, e⟩ + heapR := fun q h => (hex q _ h).imp id fun ⟨j, hj, e, k⟩ => ⟨by + revert k + unfold newKind + split_ifs <;> simp <;> omega, j, hj, e⟩ } + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtApRec5A.lean b/proofs/Fl4ProofsA64/P3/UtApRec5A.lean new file mode 100644 index 000000000..5900fb8eb --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtApRec5A.lean @@ -0,0 +1,219 @@ +/- +P3 (aarch64, A64-P3 Untyped, full-record post): **the record-edit-#5 fields after a RAM creation** +(`UtRec5OutA`, `utRt_rec5A`), from the record at the creation start, A64 P1's creation end and the spec body's +object view. + +* `heapWin`: a new object lies in the window, inside the untyped's region ([J] `UtWinCapA`, record edit #6). +* `ptCapIn`, `fhOK`, `zwf`: the new slots are null; an old typed slot keeps its cap, takes the untyped's free-index + write, or (a destination) a new cap, which is no mapped page table and no zombie. +* `symL`: A64 P1's creation top. `coresAll`: the cores are kept. +* `schedValid`: the body keeps the scheduler globals and the old non-CNode objects; a new TCB is inactive. + +Premises: [J] `UtWinCapA`. New definitions and theorems only. +-/ +import Fl4ProofsA64.P3.UtApCaps2A +import Fl4ProofsA64.P3.UtApIpcA +import Fl4ProofsA64.P3.Stage3J5A +import Fl4ProofsA64.P3.Stage3Rec5A + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +set_option maxRecDepth 100000 + +/-- The record-edit-#5 fields of the grown record. -/ +structure UtRec5OutA {S : Type} (st : machine.Store S) [L : LawfulStore st] (Z' : XSetsA) (H' : Heap) (s' : S) + (σ' : State Unit) : Prop where + heapWin : ∀ p k, H' p = some k → + layout.PPTR_BASE.val ≤ p.val ∧ p.val + 2 ^ k.bits ≤ layout.PPTR_BASE.val + 2 ^ 44 + coresAll : ∀ k < Fl4SpecA64.fl4A64Platform.numNodes, ∃ c ∈ Z'.Cs, c.val = k + ptCapIn : PtCapInA Z' (L.mem s') + symL : SymL st H' Z'.Cs s' + fhOK : ∀ t ∈ Z'.T, NullOrEp H' (capA st s' (slotIdx t layout.tcb.SLOT_FAULT_HANDLER)) + zwf : DelInvA st Z'.th s' + schedValid : SchedValidA σ' + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H H' : Heap} {OS U Ac Pp Tb Cn Vn : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N Ep Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J7 : JSlotA S} {s s' : S} {a : A} {σ σ' : State Unit} {m : McState} {G' : MdbGhostA} + {slot p sb fi aligned nt us dest off win : U64} {P : ObjRef → Prop} {d : KernelObject} + +private theorem ut_ite_neA {c : Prop} [Decidable c] {x y z : capability.Cap} (hx : x ≠ z) (hy : y ≠ z) : + (if c then x else y) ≠ z := by + by_cases h : c + · rw [ite_eq_left h]; exact hx + · rw [ite_eq_right h]; exact hy + +theorem utNewCap_noZombieA {k : Nat} {b u p zt c : U64} {dv : Bool} : newCapA k b u dv ≠ .Zombie p zt c := by + unfold newCapA + repeat' refine ut_ite_neA ?_ ?_ + all_goals exact fun h => by cases h + +theorem utNewCap_noMappedPtA {k : Nat} {b u ptr asid va : U64} {dv : Bool} : + newCapA k b u dv ≠ .PageTable ptr true asid va := by + unfold newCapA + repeat' refine ut_ite_neA ?_ ?_ + all_goals exact fun h => by cases h + +set_option maxHeartbeats 2000000 in +/-- **The record-edit-#5 fields after a RAM creation** (module docstring). -/ +theorem utRt_rec5A + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N Ep Rp Bf stack κ n (J5A st) J7 s a σ m) + (hx : slot ∈ OS) (hc : capA st s slot = .Untyped p sb fi false) + (W : UtWinZA nt aligned (zA nt.val us) win.val) + (h1 : p.val + fi.val ≤ aligned.val) + (h2 : aligned.val + win.val * 2 ^ (zA nt.val us).val ≤ p.val + 2 ^ sb.val) + (uw : UtWinCapA st OS s) + (hn : nt.val < 15) (hus : us.val < 64) (hc47 : nt.val = 4 → us.val ≤ 47) + (Ec : CrtE st H H.frame OS s s' slot p sb false aligned nt us dest off win) + (hdc : ∀ j < win.val, ∃ d : CslotPtr, (∃ q, d.1 = specPtr q ∧ Z.th q = some (.cnode d.2.length)) ∧ + djA dest off j = cslotAddrA d) + (hfree : ∀ q, UtNewA false nt us aligned win.val q → Z.th q = none) + (newNull : ∀ y ∈ NewSl false nt us aligned win, capA st s' y = .Null) + (hgrow : ∀ q k, H q = some k → H' q = some k) + (hex : ∀ q k, H' q = some k → H q = some k ∨ + ∃ j < win.val, q = bjA aligned (zA nt.val us) j ∧ newKind nt.val us.val = some k) + (symL : SymL st H' Z.Cs s') + (O : UtNewObjA σ σ' P d) (hdi : ∀ x, d = .tcb x → x.tcbState = .inactive) : + UtRec5OutA st (utRtZA Z G' nt us aligned win.val (UtOfTyA 1 nt us aligned win.val) Cn Vn) H' s' σ' := by + have hpf : aligned.val + win.val * 2 ^ (zA nt.val us).val ≤ 2 ^ 64 := W.fit + have bjv : ∀ j < win.val, (bjA aligned (zA nt.val us) j).val = aligned.val + j * 2 ^ (zA nt.val us).val ∧ + j * 2 ^ (zA nt.val us).val + 2 ^ (zA nt.val us).val ≤ win.val * 2 ^ (zA nt.val us).val := fun j hj => by + have g : (j + 1) * 2 ^ (zA nt.val us).val ≤ win.val * 2 ^ (zA nt.val us).val := Nat.mul_le_mul_right _ hj + rw [Nat.add_mul, Nat.one_mul] at g + have := Nat.two_pow_pos (zA nt.val us).val + exact ⟨bjA_val (by omega), g⟩ + have hP := J.j.1.place + have thOld : ∀ q k, Z.th q = some k → utRtThA Z.th false nt us aligned win.val q = some k := fun q k h => by + rw [utRtTh_oldA (fun g => by rw [hfree q g] at h; cases h)] + exact h + -- an old typed slot: its cap after + have slots : ∀ y, IsSlotA (utRtThA Z.th false nt us aligned win.val) y → + capA st s' y = .Null ∨ (∃ k b u dv, capA st s' y = newCapA k b u dv) ∨ + (∃ i, capA st s' y = .Untyped p sb i false) ∨ (IsSlotA Z.th y ∧ capA st s' y = capA st s y) := by + intro y hy + rcases (utRt_newSlotsA (th := Z.th) hn hus hc47 hpf hfree y).1 hy with old | new + · have yO : y ∈ OS := J.j5.2.1 y old + by_cases e : y = slot + · rw [e] + exact Or.inr (Or.inr (Or.inl Ec.src)) + by_cases g : ∃ j < win.val, y = djA dest off j + · obtain ⟨j, hj, rfl⟩ := g + exact Or.inr (Or.inl ⟨_, _, _, _, Ec.new j hj⟩) + exact Or.inr (Or.inr (Or.inr ⟨old, Ec.keep y yO e fun j hj h => g ⟨j, hj, h⟩⟩)) + · exact Or.inl (newNull y new) + refine + { heapWin := ?win + coresAll := J.coresAll + ptCapIn := ?pt + symL := symL + fhOK := ?fh + zwf := ?zw + schedValid := ?sv } + case win => + intro q k hq + rcases hex q k hq with old | ⟨j, hj, rfl, hk⟩ + · exact J.heapWin q k old + obtain ⟨v, le⟩ := bjv j hj + obtain ⟨w1, w2⟩ := uw slot hx p sb fi hc + rw [newKind_bitsA hk hn hus, v] + omega + case pt => + intro y hy ptr asid va h + change capA st s' y = _ at h + rcases slots y hy with g | ⟨k, b, u, dv, g⟩ | ⟨i, g⟩ | ⟨old, g⟩ + · rw [g] at h; cases h + · rw [g] at h; exact absurd h utNewCap_noMappedPtA + · rw [g] at h; cases h + rw [g] at h + rcases J.ptCapIn y old ptr asid va h with ⟨m1, lv⟩ | t2 + · refine Or.inl ⟨Or.inl m1, ?_⟩ + have off : ptr ∉ utS1NewA nt aligned (zA nt.val us) win.val := by + rintro ⟨-, j, hj, e⟩ + have C := utRt_winClrA J hx hc h1 h2 + have g := C.1 ptr 4096 (Or.inl ⟨rfl, Or.inr (Or.inl m1)⟩) + obtain ⟨v, le⟩ := bjv j hj + rw [← e] at v + have := Nat.two_pow_pos (zA nt.val us).val + omega + show 1 ≤ utLvNewA Z.Lv nt aligned (zA nt.val us) win.val ptr + unfold utLvNewA + rw [ite_eq_right off] + exact lv + · exact Or.inr t2 + case fh => + intro t ht + rcases ht with old | ⟨e1, j, hj, rfl⟩ + · have tth : Z.th t = some .tcb := (J.sr.thT t).2 old + have isS := isSlot_fhA tth + have yO := J.j5.2.1 _ isS + have was := J.fhOK t old + have ne : slotIdx t layout.tcb.SLOT_FAULT_HANDLER ≠ slot := fun e => by + rw [e, hc] at was + rcases was with g | ⟨_, _, _, _, _, _, g, -⟩ <;> cases g + have nd : ∀ j < win.val, slotIdx t layout.tcb.SLOT_FAULT_HANDLER ≠ djA dest off j := fun j hj e => by + obtain ⟨d, ⟨q, eq, hq⟩, ed⟩ := hdc j hj + have a1 : AbsSlotA Z.th (specPtr t, Fl4SpecA64.tcbCnodeIndex 3) := + ⟨t, rfl, Or.inr ⟨tth, 3, by unfold tcbSlotsA; omega, rfl⟩⟩ + have a2 : AbsSlotA Z.th d := ⟨q, eq, Or.inl hq⟩ + have ea : cslotAddrA (specPtr t, Fl4SpecA64.tcbCnodeIndex 3) = cslotAddrA d := by + rw [← ed, ← e, slotIdx_fhA] + unfold cslotAddrA + rw [ofBl_tcbIdx 3 (by omega)] + have := cslotAddrA_inj hP a1 a2 ea + rw [← this] at eq + have e2 := specPtr_inj eq + rw [← e2, tth] at hq + cases hq + rw [Ec.keep _ yO ne nd] + exact was.imp id fun ⟨ep, b0, b1, b2, b3, b4, e, he⟩ => ⟨ep, b0, b1, b2, b3, b4, e, hgrow _ _ he⟩ + · refine Or.inl (newNull _ ?_) + obtain ⟨v, le⟩ := bjv j hj + have z12 : 2 ^ (zA nt.val us).val = 4096 := by rw [e1, zA_1 us]; rfl + refine ⟨rfl, j, hj, 3, Or.inl ⟨e1, by omega⟩, ?_⟩ + have e3 : layout.tcb.SLOT_FAULT_HANDLER.val = 3 := by simp [layout.tcb.SLOT_FAULT_HANDLER] + rw [slotIdx_valm _ _ (by rw [e3]; omega), e3] + try omega + case zw => + intro y hy q zt k h + rcases slots y hy with g | ⟨k', b, u, dv, g⟩ | ⟨i, g⟩ | ⟨old, g⟩ + · rw [g] at h; cases h + · rw [g] at h; exact absurd h utNewCap_noZombieA + · rw [g] at h; cases h + rw [g] at h + exact (J.zwf y old q zt k h).imp (fun z => ⟨z.1, thOld _ _ z.2.1, z.2.2⟩) + (fun z => ⟨z.1, thOld _ _ z.2.1, z.2.2⟩) + case sv => + have keep : ∀ q ko, σ.kheap q = some ko → (∀ r cs, ko ≠ .cnode r cs) → σ'.kheap q = some ko := O.keep + have act : ∀ q, ScActiveAtA σ q → ScActiveAtA σ' q := fun q ⟨y, k, e, g⟩ => + ⟨y, k, keep q _ e (fun _ _ h => by cases h), g⟩ + refine ⟨fun dm pr t hm => ?_, fun e1 e2 => ?_, fun t x sc e1 e2 e3 => ?_, fun t hm => ?_⟩ + · rw [O.glob.rq] at hm + obtain ⟨⟨x, sc, e, es, g⟩, nr⟩ := J.schedValid.rq dm pr t hm + exact ⟨⟨x, sc, keep t _ e (fun _ _ h => by cases h), es, act _ g⟩, by rw [O.glob.rls]; exact nr⟩ + · rw [O.glob.act] at e1 + rw [O.glob.cur, O.glob.idle] at e2 + obtain ⟨⟨x, sc, e, es, g⟩, nr⟩ := J.schedValid.act e1 e2 + rw [O.glob.cur, O.glob.rls] + exact ⟨⟨x, sc, keep _ _ e (fun _ _ h => by cases h), es, act _ g⟩, nr⟩ + · rcases O.back t _ e1 (fun _ _ h => by cases h) with old | ⟨-, e⟩ + · exact act _ (J.schedValid.blk t x sc old e2 e3) + · rw [hdi x e.symm] at e2 + cases e2 + · rw [O.glob.rls] at hm + obtain ⟨x, sc, e, es, g⟩ := J.schedValid.rls t hm + exact ⟨x, sc, keep t _ e (fun _ _ h => by cases h), es, act _ g⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtApRec6A.lean b/proofs/Fl4ProofsA64/P3/UtApRec6A.lean new file mode 100644 index 000000000..94f6fb59b --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtApRec6A.lean @@ -0,0 +1,488 @@ +/- +P3 (aarch64, A64-P3 Untyped, full-record post): **the record-edit-#6 fields after a RAM creation**, from the record at +the creation start, A64 P1's creation end and the spec body's object view. + +* `utRt_osFreeA`: a word of the source untyped's free part is in no `OS` slot. +* `utNewFrame_objA`: the spec object of a new non-device frame cap is a data page of the cap's size. +* `UtRec6OutA`, **`utRt_rec6A`**: `utWin` (new RAM untypeds lie inside the parent), `vcBack` / `runBack` (new VCPUs and + TCBs have zero binding words), `vcOS`, `userFr`. +* **`utRt_vcSnapA`**: `vcSnap` after FL4's `vcpu::init` step of the spec (`utVcInitA`): a new VCPU's words are those + of `fl4_vcpu_init default_vcpu` (`UtVcpuZW`), an old VCPU's words and spec object are kept. + +No named premise. New definitions and theorems only. +-/ +import Fl4ProofsA64.P3.UtRec6KeepA +import Fl4ProofsA64.P3.UtApRec5A +import Fl4ProofsA64.P3.UtRtVcpuWordsA +import Fl4ProofsA64.P3.UtRtTcbWordsA +import Fl4ProofsA64.P3.UtVcTrA +import Fl4ProofsA64.P3.UtRtFrontA +import Fl4ProofsA64.P3.UtRtSpecA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +set_option maxRecDepth 100000 + +/-- The spec object of a new non-device frame cap. -/ +theorem utNewFrame_objA {nt us b p rights x3 x4 : U64} {size : capability.FrameSize} {x1 x2 : Bool} + (hn : nt.val < 15) (h : newCapA nt.val b us false = .Frame p size rights false x1 x2 x3 x4) + {t : ApiObjectType} (htt : objTypeOfA nt.val = some t) (d : Domain) : + t ≠ .untyped ∧ defaultObject t false us.val d = .archObj (.dataPage false (frameSizeOfA size)) := by + generalize nt.val = k at h htt hn + interval_cases k <;> simp [newCapA] at h <;> obtain ⟨-, rfl, -⟩ := h <;> + simp [objTypeOfA, archDataToObjType] at htt <;> subst htt <;> exact ⟨by simp, rfl⟩ + +/-- A new frame cap's object kind: `utKindA` is its `other` object of the cap's full size, for a type `UtNewA` creates +objects of. -/ +theorem utKind_frameA {nt us b p rights x3 x4 bits : U64} {size : capability.FrameSize} {x1 x2 : Bool} + (hn : nt.val < 15) (h : newCapA nt.val b us false = .Frame p size rights false x1 x2 x3 x4) + (hb : size.bits = ok bits) : + nt.val ≠ 0 ∧ (nt.val ≠ 5 ∧ nt.val ≠ 8 ∧ nt.val ≠ 11 ∧ nt.val ≠ 13 ∧ nt.val ≠ 14) ∧ + utKindA nt us = .other (2 ^ bits.val) := by + unfold utKindA + generalize nt.val = k at h hn ⊢ + interval_cases k <;> simp [newCapA] at h <;> obtain ⟨-, rfl, -⟩ := h <;> + simp only [capability.FrameSize.bits] at hb <;> rw [← okinj hb] <;> simp [zA] + +/-- The record-edit-#6 fields of the grown record, but the VCPU snapshot. -/ +structure UtRec6OutA {S : Type} (st : machine.Store S) [L : LawfulStore st] (Z' : XSetsA) (OS' : Set U64) (s' : S) + (σ' : State Unit) : Prop where + utWin : UtWinCapA st OS' s' + devWin : DevWinCapA st OS' s' + vcBack : ∀ v ∈ Z'.V, L.mem s' v ≠ 0#u64 → L.mem s' (wa (L.mem s' v) 3664#u64) = v + runBack : ∀ t ∈ Z'.T, L.mem s' (runVcpuA t) ≠ 0#u64 → L.mem s' (wa (L.mem s' (runVcpuA t)) 24#u64) = t + vcOS : ∀ b, VcW Z'.V b → ¬ InOS OS' b + userFr : UserFrSpecA st OS' s' σ' + frTh : FrThA st Z'.th OS' s' + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb Cn : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N Ep Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s s' : S} {a : A} {σ σ' : State Unit} {m : McState} {G' : MdbGhostA} + {slot p sb fi aligned nt us dest off win : U64} + +/-- The value of a window base. -/ +theorem utRt_bjvA {nt aligned z : U64} {win j : Nat} (W : UtWinZA nt aligned z win) (hj : j < win) : + (bjA aligned z j).val = aligned.val + j * 2 ^ z.val := by + have g : (j + 1) * 2 ^ z.val ≤ win * 2 ^ z.val := Nat.mul_le_mul_right _ hj + rw [Nat.add_mul, Nat.one_mul] at g + have := Nat.two_pow_pos z.val + have := W.fit + exact bjA_val (by omega) + +/-- A word of the source's free part is in no `OS` slot. -/ +theorem utRt_osFreeA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N Ep Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (hx : slot ∈ OS) (hc : capA st s slot = .Untyped p sb fi false) {b : U64} (al : Aligned b) + (h1 : p.val + fi.val ≤ b.val) (h2 : b.val < p.val + 2 ^ sb.val) : ¬ InOS OS b := by + rintro ⟨y, hy, g1, g2⟩ + have hO := J.sys.ent.os + have al0 := hO.al0 hy + unfold Aligned at al0 al + obtain ⟨d, hd⟩ : ∃ d : U64, d.val = b.val - y.val := + ⟨⟨BitVec.ofNat 64 (b.val - y.val)⟩, by + show (BitVec.ofNat 64 _).toNat = _ + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by have := b.hBounds; simp at this; omega)⟩ + have d8 : d.val % 8 = 0 := by omega + have ey : wa y d = b := by + apply UScalar.eq_of_val_eq + rw [wa_valP, hd] + rw [Nat.mod_eq_of_lt (by have := b.hBounds; simp at this; omega)] + omega + exact in_free_not_capw J.sys.ent.heap J.sys.sysl.sys.stz.1.2.2.1 hx hc h1 h2 + (ey ▸ hO.cw hy (k := d) (by rw [hd]; omega) d8) + +set_option maxHeartbeats 2000000 in +/-- **The record-edit-#6 fields after a RAM creation** (module docstring). -/ +theorem utRt_rec6A + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N Ep Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (hx : slot ∈ OS) (hc : capA st s slot = .Untyped p sb fi false) + (W : UtWinZA nt aligned (zA nt.val us) win.val) + (h1 : p.val + fi.val ≤ aligned.val) + (h2 : aligned.val + win.val * 2 ^ (zA nt.val us).val ≤ p.val + 2 ^ sb.val) + (hn : nt.val < 15) (hus : us.val < 64) + (Ec : CrtE st H H.frame OS s s' slot p sb false aligned nt us dest off win) + (w : WritesIn st s s' (UtCrtWA st H OS s nt aligned (zA nt.val us) win.val)) + (newNull : ∀ y ∈ NewSl false nt us aligned win, capA st s' y = .Null) + (TW : nt.val = 1 → UtTcbZW st aligned (zA nt.val us) win.val s') + (VW : nt.val = 12 → UtVcpuZW st aligned (zA nt.val us) win.val s') + {t : ApiObjectType} (htt : objTypeOfA nt.val = some t) + (O : UtNewObjA σ σ' (fun q => t ≠ .untyped ∧ UtNewSpA aligned (zA nt.val us) win.val q) + (utNewDA t us.val σ.curDomain)) : + UtRec6OutA st (utRtZA Z G' nt us aligned win.val (UtOfTyA 1 nt us aligned win.val) Cn + (UtOfTyA 12 nt us aligned win.val)) (OS ∪ NewSl false nt us aligned win) s' σ' := by + have Cl : UtClrA st Z H s aligned.val (aligned.val + win.val * 2 ^ (zA nt.val us).val) := + utRt_winClrA J hx hc h1 h2 + have footK : ∀ b : U64, FootNA (L := L) Z s b → L.mem s' b = L.mem s b := fun b h => + utRt_footKeptA J hx hc h1 h2 w h + have bjv : ∀ j, j < win.val → (bjA aligned (zA nt.val us) j).val = aligned.val + j * 2 ^ (zA nt.val us).val ∧ + aligned.val + j * 2 ^ (zA nt.val us).val + 2 ^ (zA nt.val us).val ≤ + aligned.val + win.val * 2 ^ (zA nt.val us).val := fun j hj => by + have g : (j + 1) * 2 ^ (zA nt.val us).val ≤ win.val * 2 ^ (zA nt.val us).val := Nat.mul_le_mul_right _ hj + rw [Nat.add_mul, Nat.one_mul] at g + have := Nat.two_pow_pos (zA nt.val us).val + have := W.fit + exact ⟨bjA_val (by omega), by omega⟩ + -- a new slot lies in the window + have newIn : ∀ y ∈ NewSl false nt us aligned win, (nt.val = 1 ∨ nt.val = 4) ∧ aligned.val ≤ y.val ∧ + y.val + 32 ≤ aligned.val + win.val * 2 ^ (zA nt.val us).val := by + rintro y ⟨-, j, hj, i, hi, e⟩ + have v := bjv j hj + have inobj : 32 * i + 32 ≤ 2 ^ (zA nt.val us).val := by + rcases hi with ⟨h1', hi⟩ | ⟨h4, hi⟩ + · have z12 : (zA nt.val us).val = 12 := by rw [zA_valA nt us hn hus]; simp [zOfA, h1'] + rw [z12]; omega + · have z5 : (zA nt.val us).val = us.val + 5 := by rw [zA_valA nt us hn hus]; simp [zOfA, h4, Nat.add_comm] + rw [z5, Nat.pow_add]; omega + refine ⟨hi.imp And.left And.left, ?_, ?_⟩ <;> omega + -- a cap of a grown slot + have caps : ∀ y ∈ OS ∪ NewSl false nt us aligned win, + capA st s' y = .Null ∨ (∃ j < win.val, capA st s' y = newCapA nt.val (bjA aligned (zA nt.val us) j) us false) ∨ + (y = slot ∧ ∃ i, capA st s' y = .Untyped p sb i false) ∨ (y ∈ OS ∧ capA st s' y = capA st s y) := by + intro y hy + by_cases hN : y ∈ NewSl false nt us aligned win + · exact Or.inl (newNull y hN) + have yO : y ∈ OS := hy.resolve_right hN + by_cases e : y = slot + · exact Or.inr (Or.inr (Or.inl ⟨e, by rw [e]; exact Ec.src⟩)) + by_cases g : ∃ j < win.val, y = djA dest off j + · obtain ⟨j, hj, rfl⟩ := g + exact Or.inr (Or.inl ⟨j, hj, Ec.new j hj⟩) + exact Or.inr (Or.inr (Or.inr ⟨yO, Ec.keep y yO e fun j hj h => g ⟨j, hj, h⟩⟩)) + have vOld : ∀ v ∈ Z.V, H v = some (.other 12) := fun v hv => + (utVc_wordA J hv (o := 0#u64) (by simp) (by simp)).1 + -- an old VCPU is outside the window + have vOut : ∀ v ∈ Z.V, v.val + 4096 ≤ aligned.val ∨ aligned.val + win.val * 2 ^ (zA nt.val us).val ≤ v.val := + fun v hv => by + have := Cl.obj v _ (Or.inr (Or.inr (Or.inr ⟨_, vOld v hv, rfl⟩))) + simpa only [show (HKind.other 12).bits = 12 from rfl, Nat.reducePow] using this + have vNew : ∀ v ∈ UtOfTyA 12 nt us aligned win.val, nt.val = 12 ∧ ∃ j < win.val, + v = bjA aligned (zA nt.val us) j ∧ v.val = aligned.val + j * 4096 ∧ + v.val + 4096 ≤ aligned.val + win.val * 2 ^ (zA nt.val us).val := by + rintro v ⟨e12, j, hj, rfl⟩ + have z12 : 2 ^ (zA nt.val us).val = 4096 := by + rw [show (zA nt.val us).val = 12 by rw [zA_valA nt us hn hus]; simp [zOfA, e12]] + norm_num + have b := bjv j hj + rw [z12] at b ⊢ + exact ⟨e12, j, hj, rfl, b.1, by omega⟩ + have thFree : ∀ q, UtNewA false nt us aligned win.val q → Z.th q = none := utRt_ipcFreeA J hx hc W h1 h2 + refine ⟨?win, ?dev, ?back, ?run, ?os, ?fr, ?th⟩ + case win => + intro u hu q b f hcu + rcases caps u hu with g | ⟨j, hj, g⟩ | ⟨-, i, g⟩ | ⟨uO, g⟩ + · rw [g] at hcu; cases hcu + · rw [g] at hcu + obtain ⟨e0, rfl, rfl, -⟩ := newCapA_utE hcu + have zb : (zA nt.val b).val = b.val := by rw [zA_valA nt b hn hus]; simp [zOfA, e0] + have v := bjv j hj + rw [zb] at v h2 + have w0 := J.utWin slot hx p sb fi hc + omega + · rw [g] at hcu + obtain ⟨e1, e2, -, -⟩ := capability.Cap.Untyped.inj hcu + rw [← e1, ← e2] + exact J.utWin slot hx p sb fi hc + · rw [g] at hcu + exact J.utWin u uO q b f hcu + case dev => + intro u hu + refine ⟨fun q b f hcu => ?_, fun p' sz r m e' a' v' hcu bits hb => ?_⟩ + · rcases caps u hu with g | ⟨j, hj, g⟩ | ⟨-, i, g⟩ | ⟨uO, g⟩ + · rw [g] at hcu; cases hcu + · rw [g] at hcu + obtain ⟨-, -, -, -, ed⟩ := utNewCap_untypedA hcu + cases ed + · rw [g] at hcu + obtain ⟨-, -, -, e4⟩ := capability.Cap.Untyped.inj hcu + cases e4 + · rw [g] at hcu + exact (J.devWin u uO).1 q b f hcu + · rcases caps u hu with g | ⟨j, hj, g⟩ | ⟨-, i, g⟩ | ⟨uO, g⟩ + · rw [g] at hcu; cases hcu + · rw [g] at hcu + generalize nt.val = k at hcu hn + interval_cases k <;> simp [newCapA] at hcu + · rw [g] at hcu; cases hcu + · rw [g] at hcu + exact (J.devWin u uO).2 p' sz r m e' a' v' hcu bits hb + case back => + intro v hv hne + rcases hv with old | new + · have e0 : L.mem s' v = L.mem s v := footK _ (.vcpu v v old (Or.inl rfl)) + rw [e0] at hne ⊢ + rcases J.vcRun.vtcb v old with g | g + · exact absurd g hne + rw [footK _ (.tcb _ _ g (Or.inr (Or.inr (Or.inr (Or.inl rfl)))))] + exact J.vcBack v old hne + · obtain ⟨e12, j, hj, rfl, -⟩ := vNew v new + have z := (VW e12 j hj).1 0#u64 (Or.inl rfl) + rw [wa_zeroVcA] at z + exact absurd z hne + case run => + intro u hu hne + rcases hu with old | ⟨e1, j, hj, rfl⟩ + · have e0 : L.mem s' (runVcpuA u) = L.mem s (runVcpuA u) := + footK _ (.tcb u _ old (Or.inr (Or.inr (Or.inr (Or.inr rfl))))) + rw [e0] at hne ⊢ + rcases J.vcRun.run u old with g | g + · exact absurd g hne + rw [footK _ (.vcpu _ _ g (Or.inr rfl))] + exact J.runBack u old hne + · exact absurd (TW e1 j hj _ (Or.inr (Or.inr rfl))) hne + case os => + rintro b ⟨v, hv, o, o1, o8, rfl⟩ ⟨y, hy, g1, g2⟩ + rcases hv with old | new + · obtain ⟨-, ev, -, -, -⟩ := utVc_wordA J old o1 o8 + by_cases hN : y ∈ NewSl false nt us aligned win + · obtain ⟨-, a1, a2⟩ := newIn y hN + have := vOut v old + omega + exact J.vcOS _ ⟨v, old, o, o1, o8, rfl⟩ ⟨y, hy.resolve_right hN, g1, g2⟩ + · obtain ⟨e12, j, hj, -, vv, vle⟩ := vNew v new + have hv4 : v.val % 4096 = 0 := by + have al := W.al + rw [show (zA nt.val us).val = 12 by rw [zA_valA nt us hn hus]; simp [zOfA, e12]] at al + norm_num at al + omega + have ev : (wa v o).val = v.val + o.val := by + rw [wa_valP]; exact Nat.mod_eq_of_lt (by have := W.fit; omega) + by_cases hN : y ∈ NewSl false nt us aligned win + · have := (newIn y hN).1 + omega + have al : Aligned (wa v o) := by show _ % 8 = 0; omega + exact utRt_osFreeA J hx hc al (by omega) (by omega) ⟨y, hy.resolve_right hN, g1, g2⟩ + case fr => + intro y hy q size rights mapped ept asid va hcy + rcases caps y hy with g | ⟨j, hj, g⟩ | ⟨-, i, g⟩ | ⟨yO, g⟩ + · rw [g] at hcy; cases hcy + · rw [g] at hcy + obtain ⟨eq, -⟩ := newCapA_frame hn hcy + obtain ⟨tn, d⟩ := utNewFrame_objA hn hcy htt σ.curDomain + have o := O.new (specPtr q) ⟨tn, j, hj, by rw [eq]⟩ + rw [utNewDA_ne tn, d] at o + exact o + · rw [g] at hcy; cases hcy + · rw [g] at hcy + exact O.keep _ _ (J.userFr y yO q size rights mapped ept asid va hcy) (fun _ _ e => by cases e) + case th => + intro y hy q size rights x1 x2 x3 x4 bits hcy hb + rcases caps y hy with g | ⟨j, hj, g⟩ | ⟨-, i, g⟩ | ⟨yO, g⟩ + · rw [g] at hcy; cases hcy + · rw [g] at hcy + obtain ⟨eq, -⟩ := newCapA_frame hn hcy + obtain ⟨n0, n5, uk⟩ := utKind_frameA hn hcy hb + have nw : UtNewA false nt us aligned win.val q := ⟨rfl, n0, n5, j, hj, eq⟩ + show utRtThA Z.th false nt us aligned win.val q = _ + rw [utRtTh_newA nw, uk] + · rw [g] at hcy; cases hcy + · rw [g] at hcy + exact utRtTh_growA thFree (J.frTh y yO q size rights x1 x2 x3 x4 bits hcy hb) + +set_option maxHeartbeats 1000000 in +/-- **`vcSnap` after the spec's `vcpu::init` step** (module docstring). -/ +theorem utRt_vcSnapA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N Ep Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (hx : slot ∈ OS) (hc : capA st s slot = .Untyped p sb fi false) + (W : UtWinZA nt aligned (zA nt.val us) win.val) + (h1 : p.val + fi.val ≤ aligned.val) + (h2 : aligned.val + win.val * 2 ^ (zA nt.val us).val ≤ p.val + 2 ^ sb.val) + (hn : nt.val < 15) (hus : us.val < 64) + (w : WritesIn st s s' (UtCrtWA st H OS s nt aligned (zA nt.val us) win.val)) + (VW : nt.val = 12 → UtVcpuZW st aligned (zA nt.val us) win.val s') + {t : ApiObjectType} (htt : objTypeOfA nt.val = some t) + (O : UtNewObjA σ σ' (fun q => t ≠ .untyped ∧ UtNewSpA aligned (zA nt.val us) win.val q) + (utNewDA t us.val σ.curDomain)) : + VcpuStateSnapA (Z.V ∪ UtOfTyA 12 nt us aligned win.val) (L.mem s') + (utVcInitA (UtVcSpA (UtOfTyA 12 nt us aligned win.val)) σ') := by + have Cl : UtClrA st Z H s aligned.val (aligned.val + win.val * 2 ^ (zA nt.val us).val) := + utRt_winClrA J hx hc h1 h2 + have hH : HeapOK H := J.sys.ent.heap + have thFree : ∀ q, UtNewA false nt us aligned win.val q → Z.th q = none := utRt_ipcFreeA J hx hc W h1 h2 + refine ⟨fun v hv x hk => ?_, fun v hv x hk => ?_⟩ + rotate_left + · by_cases new : v ∈ UtOfTyA 12 nt us aligned win.val + · obtain ⟨e12, j, hj, rfl⟩ := new + have et : t = .archObject .vcpuObj := by + have h := e12 ▸ htt + simp [objTypeOfA, archDataToObjType, eq_comm] at h + exact h + have tn : t ≠ .untyped := by rw [et]; simp + have o := O.new (specPtr (bjA aligned (zA nt.val us) j)) ⟨tn, j, hj, rfl⟩ + rw [utNewDA_ne tn, et] at o + change σ'.kheap _ = some (.archObj (.vcpu defaultVcpu)) at o + have hq : UtVcSpA (UtOfTyA 12 nt us aligned win.val) (specPtr (bjA aligned (zA nt.val us) j)) := + ⟨_, ⟨e12, j, hj, rfl⟩, rfl⟩ + rw [utVcInit_vcpuA hq o] at hk + obtain rfl : fl4VcpuInit defaultVcpu = x := by + injection hk with hk; injection hk with hk; injection hk + obtain ⟨-, -, -, -, -, hmk⟩ := utVcpu_headerA + rw [hmk]; simp + · have old : v ∈ Z.V := hv.resolve_right new + have nq : ¬ UtVcSpA (UtOfTyA 12 nt us aligned win.val) (specPtr v) := by + rintro ⟨u, hu, e⟩ + exact new (specPtr_inj e ▸ hu) + rw [utVcInit_offA nq] at hk + obtain ⟨y, hy, -⟩ := J.j.2.1.obj v old + have kp := O.keep _ _ hy (fun _ _ e => by cases e) + have exy : σ.kheap (specPtr v) = some (.archObj (.vcpu x)) := by rw [hy, ← kp, hk] + exact J.vcSnap.mask v old x exy + by_cases new : v ∈ UtOfTyA 12 nt us aligned win.val + · obtain ⟨e12, j, hj, rfl⟩ := new + have et : t = .archObject .vcpuObj := by + have h := e12 ▸ htt + simp [objTypeOfA, archDataToObjType, eq_comm] at h + exact h + have tn : t ≠ .untyped := by rw [et]; simp + have o := O.new (specPtr (bjA aligned (zA nt.val us) j)) ⟨tn, j, hj, rfl⟩ + rw [utNewDA_ne tn, et] at o + change σ'.kheap _ = some (.archObj (.vcpu defaultVcpu)) at o + have hq : UtVcSpA (UtOfTyA 12 nt us aligned win.val) (specPtr (bjA aligned (zA nt.val us) j)) := + ⟨_, ⟨e12, j, hj, rfl⟩, rfl⟩ + rw [utVcInit_vcpuA hq o] at hk + obtain rfl : fl4VcpuInit defaultVcpu = x := by + injection hk with hk; injection hk with hk; injection hk + obtain ⟨-, rz, rr⟩ := VW e12 j hj + obtain ⟨-, -, hm, -, ho, hmk⟩ := utVcpu_headerA + refine ⟨rr, ?_, ?_, ?_, ?_⟩ + · rw [rz _ (Or.inl rfl), hm]; rfl + · rw [rz _ (Or.inr (Or.inl rfl)), ho]; rfl + · rw [rz _ (Or.inr (Or.inr (Or.inl rfl))), hmk]; rfl + · rw [rz _ (Or.inr (Or.inr (Or.inr rfl)))]; simp + · have old : v ∈ Z.V := hv.resolve_right new + have nq : ¬ UtVcSpA (UtOfTyA 12 nt us aligned win.val) (specPtr v) := by + rintro ⟨u, hu, e⟩ + exact new (specPtr_inj e ▸ hu) + rw [utVcInit_offA nq] at hk + obtain ⟨y, hy, -⟩ := J.j.2.1.obj v old + have kp := O.keep _ _ hy (fun _ _ e => by cases e) + have exy : σ.kheap (specPtr v) = some (.archObj (.vcpu x)) := by rw [hy, ← kp, hk] + have words : ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → L.mem s' (wa v o) = L.mem s (wa v o) := by + intro o o1 o8 + obtain ⟨hv12, ev, al, out, -⟩ := utVc_wordA J old o1 o8 + refine w.mem_eq al ?_ + rintro (h | ⟨-, g1, g2⟩ | h | ⟨u, hu, h⟩) + · exact J.vcOS _ ⟨v, old, o, o1, o8, rfl⟩ h + · exact Cl.inObj (Or.inr (Or.inr (Or.inr ⟨_, hv12, rfl⟩))) (a := wa v o) (by omega) + (by simp only [show (HKind.other 12).bits = 12 from rfl, Nat.reducePow]; omega) ⟨g1, g2⟩ + · have h0 : (0#u64 : U64) ∈ Z.Cs := by + obtain ⟨k, hk, -, h⟩ := J.j.1.csN + exact (h 0#u64).2 (by simpa using hk) + have n0 := J.sq.nodes 0#u64 h0 + have d88 : layout.node.DEBUG_TCBS.val < 4288 := by rw [debug_val]; omega + have hv0 : (nwA st s).val = (L.ksb s).val + 223576 := by + rw [nodeAt_val n0 d88, debug_val] + unfold LawfulStore.nodeBase + simp + rw [h, hv0] at out + omega + · have f := hH.fits u .tcb hu + simp only [show HKind.tcb.bits = 12 from rfl, Nat.reducePow] at f + have eu : (dpA u).val = u.val + 3584 := by + unfold dpA + rw [wa_valP, show (3584#u64 : U64).val = 3584 from rfl] + exact Nat.mod_eq_of_lt (by omega) + have ne : v ≠ u := fun e => by + rw [← e, hv12] at hu + cases hu + have d := hH.disj v _ u _ hv12 hu ne + simp only [show (HKind.other 12).bits = 12 from rfl, show HKind.tcb.bits = 12 from rfl, Nat.reducePow] at d + rw [h, eu] at ev + omega + obtain ⟨g1, g2, g3, g4, g5⟩ := J.vcSnap.snap v old x exy + refine ⟨fun i hi => ?_, ?_, ?_, ?_, ?_⟩ + · obtain ⟨o1, o2⟩ := snapOffA i hi + rw [words _ o1 o2]; exact g1 i hi + · rw [words 16#u64 (by simp) (by simp)]; exact g2 + · rw [words 32#u64 (by simp) (by simp)]; exact g3 + · rw [words 40#u64 (by simp) (by simp)]; exact g4 + · rw [words 8#u64 (by simp) (by simp)]; exact g5 + +/-- Every outcome of the generic invocation ending in `σ'` extends to one of `fl4InvokeUntypedA64` ending in +`utInvStA` of `σ'`. -/ +def UtInvVcA (t : ApiObjectType) (retypeBase : Word) (us : Nat) (slots : List CslotPtr) (σ' : State Unit) : Prop := + ∀ (srcSlot : CslotPtr) (reset : Bool) (base : Word) (dev : Bool) (σa : State Unit), + (.ok (), σ') ∈ ((fl4InvokeUntyped (Z := Unit) + (.retype srcSlot reset base retypeBase t us slots dev)).run σa).1 → + (.ok (), utInvStA t retypeBase us slots σ') ∈ ((fl4InvokeUntypedA64 (Z := Unit) + (.retype srcSlot reset base retypeBase t us slots dev)).run σa).1 + +/-- **The `vcpu::init` step at a RAM creation's outcome**: the spec step relation of `utInvStA` (`P3/UtVcTrA`), its +membership in the A64 invocation, and `utInvStA` as the init at the new VCPUs. -/ +theorem utRt_vcStepA (W : UtWinZA nt aligned (zA nt.val us) win.val) (hn : nt.val < 15) (hus : us.val < 64) + {t : ApiObjectType} (htt : objTypeOfA nt.val = some t) {slots : List CslotPtr} (hlen : slots.length = win.val) + (O : UtNewObjA σ σ' (fun q => t ≠ .untyped ∧ UtNewSpA aligned (zA nt.val us) win.val q) + (utNewDA t us.val σ.curDomain)) : + utInvStA t (specPtr aligned) us.val slots σ' = utVcInitA (UtVcSpA (UtOfTyA 12 nt us aligned win.val)) σ' ∧ + UtVcStepA σ' (utInvStA t (specPtr aligned) us.val slots σ') ∧ + UtInvVcA t (specPtr aligned) us.val slots σ' := by + have memA : ∀ q, q ∈ fl4RetypeAddrs fl4ObjBitsApi (specPtr aligned) slots.length us.val t ↔ + UtNewSpA aligned (zA nt.val us) win.val q := fun q => by + rw [hlen, utRet_addrs_memA htt hus] + rfl + have t12 : t = .archObject .vcpuObj ↔ nt.val = 12 := by + have hh := htt + generalize nt.val = k at hh hn ⊢ + interval_cases k <;> simp [objTypeOfA, archDataToObjType] at hh <;> subst hh <;> simp + have isV : ∀ q, utVcRetQA t (specPtr aligned) us.val slots q → + σ'.kheap q = some (.archObj (.vcpu defaultVcpu)) := by + rintro q ⟨et, hq⟩ + have tn : t ≠ .untyped := by rw [et]; simp + have o := O.new q ⟨tn, (memA q).1 hq⟩ + rw [utNewDA_ne tn, et] at o + exact o + have eQ : utVcRetQA t (specPtr aligned) us.val slots = UtVcSpA (UtOfTyA 12 nt us aligned win.val) := by + funext q + refine propext ⟨fun h => ?_, fun h => ?_⟩ + · obtain ⟨j, hj, rfl⟩ := (memA q).1 h.2 + exact ⟨_, ⟨t12.1 h.1, j, hj, rfl⟩, rfl⟩ + · obtain ⟨v, ⟨e12, j, hj, rfl⟩, rfl⟩ := h + exact ⟨t12.2 e12, (memA _).2 ⟨j, hj, rfl⟩⟩ + have nd : (fl4RetypeAddrs fl4ObjBitsApi (specPtr aligned) slots.length us.val t).Nodup := by + unfold fl4RetypeAddrs + rw [utRet_bitsA htt hus, hlen] + refine List.Nodup.map_on (fun i hi j hj e => ?_) List.nodup_range + rw [utRet_ptrA, utRet_ptrA] at e + have e' := congrArg UScalar.val (ut_specPtr_injA e) + have li := List.mem_range.1 hi + have lj := List.mem_range.1 hj + rw [(utRt_bjvA W li), (utRt_bjvA W lj)] at e' + exact Nat.eq_of_mul_eq_mul_right (Nat.two_pow_pos (zA nt.val us).val) (by omega) + refine ⟨by unfold utInvStA; rw [eQ], utVcStepA_of isV, fun srcSlot reset base dev σa h => ?_⟩ + unfold fl4InvokeUntypedA64 + refine mem_bindE_okD (a := ()) (s' := σ') h (mem_liftED ?_) + unfold Fl4Spec.NonDet.when + by_cases et : t = .archObject .vcpuObj + · rw [ite_eq_left et] + have e : utInvStA t (specPtr aligned) us.val slots σ' = + utVcInitA (· ∈ fl4RetypeAddrs fl4ObjBitsApi (specPtr aligned) slots.length us.val t) σ' := by + unfold utInvStA + congr 1 + funext q + exact propext ⟨And.right, fun g => ⟨et, g⟩⟩ + rw [e] + exact utVcInit_mapMxA _ σ' nd fun q hq => isV q ⟨et, hq⟩ + · rw [ite_eq_right et] + have e : utInvStA t (specPtr aligned) us.val slots σ' = σ' := by + unfold utInvStA utVcInitA + have k : (fun q => if utVcRetQA t (specPtr aligned) us.val slots q then (σ'.kheap q).map utVcObjA + else σ'.kheap q) = σ'.kheap := by + funext q + exact if_neg fun g => et g.1 + rw [k] + rw [e] + exact Set.mem_singleton _ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtApRstA.lean b/proofs/Fl4ProofsA64/P3/UtApRstA.lean new file mode 100644 index 000000000..225053004 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtApRstA.lean @@ -0,0 +1,418 @@ +/- +P3 (aarch64, A64-P3 Untyped, full-record post, reset path): **the entry-invariant bundle of a RAM creation after a +reset** (no `H1 = H`): the creation starts from the record on the dropped sets, A64 P1's creation top is over the +record at the decode. + +* `utRt_wordApRstA`, `utRt_apRstA`: `utRt_apA` over the creation-start heap and slots `H1` / `OS1`. +* **`utRt_sysRstA`**: `UtAsmSys0InA` with the decode-state heap and slots, from A64 P1's creation end, A64 P1's creation + top (`arm`, `ko`, `j7at`), the creation-start record on the dropped sets and the scheduler bundle there. + +No named premise. New theorems only. +-/ +import Fl4ProofsA64.P3.UtAsmAll0A +import Fl4ProofsA64.P3.UtApSys2A + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +set_option maxRecDepth 100000 + +section ap +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {H H1 H' : Heap} {Cs OS OS1 U0 Ac : Set U64} {s s1 q : S} {ia ie lo hi : Nat} + {slot p sb fi aligned nt us dest off win : U64} {dev : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (E : S1aRtIn st H Cs OS U0 Ac s ia ie slot p sb fi dev extra H1 H' OS1 s1 q lo hi aligned nt us dest off win) +include E + +/-- A word that is off the old slots, old TCBs and the hole, or a word of a new object that is no TCB and no CNode, +is off the grown slots and TCBs. -/ +theorem utRt_wordApRstA (SP : SlotPlaced H1 OS1) {b : U64} + (hb : ((¬ InOS OS1 b ∧ (∀ t ∈ H1.tcbs, b.val < t.val ∨ t.val + 4096 ≤ b.val)) ∧ + (dev = false → ¬ (aligned.val ≤ b.val ∧ b.val < aligned.val + win.val * 2 ^ (zA nt.val us).val))) ∨ + (dev = false ∧ aligned.val ≤ b.val ∧ b.val < aligned.val + win.val * 2 ^ (zA nt.val us).val ∧ + nt.val ≠ 1 ∧ nt.val ≠ 4)) : + ¬ InOS (s1aRtSlots OS1 dev nt us aligned win) b ∧ + ∀ t ∈ H'.tcbs, b.val < t.val ∨ t.val + 4096 ≤ b.val := by + have U := E.creation + have hfit : aligned.val + win.val * 2 ^ (zA nt.val us).val ≤ 2 ^ 64 := le_trans U.A.aw U.A.pf + -- every object of the record's heap is off the hole + have hole : ∀ r k, H1 r = some k → r.val + 2 ^ k.bits ≤ lo ∨ hi ≤ r.val := fun r k h => + U.hole r k h + -- a new TCB lies in the window + have newT : ∀ t, H' t = some .tcb → H1 t = some .tcb ∨ + (dev = false ∧ nt.val = 1 ∧ aligned.val ≤ t.val ∧ + t.val + 4096 ≤ aligned.val + win.val * 2 ^ (zA nt.val us).val) := by + intro t ht + rcases U.ex t _ ht with old | ⟨hd, j, hj, e, hk⟩ + · exact Or.inl old + · have hbits := newKind_bitsA hk U.A.hn E.kept.us64 + have h1 : nt.val = 1 := newKind_tcb hk + obtain ⟨hv, hle⟩ := bj_facts hj hfit + have z12 : 2 ^ (zA nt.val us).val = 4096 := by rw [← hbits]; rfl + refine Or.inr ⟨hd, h1, ?_, ?_⟩ + · rw [e, hv]; omega + · rw [e]; rw [z12] at hle ⊢; exact hle + rcases hb with ⟨⟨nos, ntcb⟩, out⟩ | ⟨hd, b1, b2, n1, n4⟩ + · constructor + · rintro ⟨y, (hy | hy), y1, y2⟩ + · exact nos ⟨y, hy, y1, y2⟩ + · obtain ⟨hd, j, hj, i, -, hyv, -, hend, h32⟩ := newSl_in E.kept hfit hy + exact out hd ⟨by omega, by omega⟩ + · intro t ht + rcases newT t ht with old | ⟨hd, -, t1, t2⟩ + · exact ntcb t old + · have := out hd + omega + · have w := U.whR hd + constructor + · rintro ⟨y, (hy | hy), y1, y2⟩ + · obtain ⟨-, r, k, hr, r1, r2, -, -⟩ := SP y hy + rcases hole r k hr with g | g <;> omega + · obtain ⟨-, j, -, i, hi, -⟩ := newSl_in E.kept hfit hy + rcases hi with ⟨h, -⟩ | ⟨h, -⟩ + · exact n1 h + · exact n4 h + · intro t ht + rcases newT t ht with old | ⟨-, h, -, -⟩ + · have := hole t _ old + have e : HKind.bits .tcb = 12 := rfl + rw [e] at this + omega + · exact absurd h n1 + +/-- **`SlotApA` after a creation.** `F`: the set split of `utRt_j7SplitA`; `ram12`: only a RAM untyped makes VCPUs. -/ +theorem utRt_apRstA {Z Z' : XSetsA} (J : SlotApA Z H1 OS1 (L.ksb s)) (SP : SlotPlaced H1 OS1) + (SP' : SlotPlaced H' (s1aRtSlots OS1 dev nt us aligned win)) (hH : HeapOK H1) (vh : VcH H1 Z.V) + (ptH : PtHA H1 (Z.Pp ∪ Z.S1)) (sets : SetsInHA Z H1) (ksw : (L.ksb s).val + 788736 ≤ 2 ^ 64) + (F : UtJ7SplitA st Z Z' (s1aRtSlots OS1 dev nt us aligned win) s q slot p sb nt us aligned win.val) + (ram12 : nt.val = 12 → 0 < win.val → dev = false) : + SlotApA Z' H' (s1aRtSlots OS1 dev nt us aligned win) (L.ksb s) := by + have U := E.creation + have hH' := E.heapOK + have hfit : aligned.val + win.val * 2 ^ (zA nt.val us).val ≤ 2 ^ 64 := le_trans U.A.aw U.A.pf + have hole : ∀ r k, H1 r = some k → r.val + 2 ^ k.bits ≤ lo ∨ hi ≤ r.val := fun r k h => + U.hole r k h + have b12 : HKind.bits (.other 12) = 12 := rfl + -- a word of an object of the record's heap is off the window + have objOff : ∀ (r : U64) k, H1 r = some k → ∀ b : U64, r.val ≤ b.val → b.val < r.val + 2 ^ k.bits → + dev = false → ¬ (aligned.val ≤ b.val ∧ b.val < aligned.val + win.val * 2 ^ (zA nt.val us).val) := by + intro r k hr b b1 b2 hd + have w := U.whR hd + rcases hole r k hr with g | g <;> omega + have vsp : ∀ v ∈ Z'.V, v ∈ Z.V ∨ (dev = false ∧ nt.val = 12 ∧ ∃ j < win.val, v = bjA aligned (zA nt.val us) j) := by + intro v hv + rcases F.v v hv with ⟨old, -⟩ | ⟨h12, j, hj, e⟩ + · exact Or.inl old + · exact Or.inr ⟨ram12 h12 (by omega), h12, j, hj, e⟩ + -- a word of a new table page + have newW : ∀ x ∈ UtSchedNewA nt us aligned win.val, ∀ b : U64, x.val ≤ b.val → b.val < x.val + 4096 → + dev = false ∧ aligned.val ≤ b.val ∧ b.val < aligned.val + win.val * 2 ^ (zA nt.val us).val ∧ + nt.val ≠ 1 ∧ nt.val ≠ 4 := by + intro x hx b b1 b2 + obtain ⟨hd, a1, a2, -⟩ := utEnd_newPageA E hx + have ty := (utSchedNew_typeA hx).1 + exact ⟨hd, by omega, by omega, by omega, by omega⟩ + have newX : ∀ x ∈ UtSchedNewA nt us aligned win.val, x.val + 4096 ≤ 2 ^ 64 := fun x hx => by + obtain ⟨-, -, a2, -⟩ := utEnd_newPageA E hx + omega + -- a word of an old 4 KiB page or 8 KiB root + have pageW : ∀ r : U64, (∃ (q : U64) (z : Nat), H1 q = some (.other z) ∧ q.val ≤ r.val ∧ r.val + 4096 ≤ q.val + 2 ^ z) → + r.val + 4096 ≤ 2 ^ 64 ∧ ∀ b : U64, r.val ≤ b.val → b.val < r.val + 4096 → dev = false → + ¬ (aligned.val ≤ b.val ∧ b.val < aligned.val + win.val * 2 ^ (zA nt.val us).val) := by + rintro r ⟨o, z, ho, h1, h2⟩ + have f := hH.fits o _ ho + have e : HKind.bits (.other z) = z := rfl + rw [e] at f + exact ⟨by omega, fun b b1 b2 => objOff o _ ho b (by omega) (by rw [e]; omega)⟩ + have rootW : ∀ r ∈ Z.X ∪ Z.Y, r.val + 8192 ≤ 2 ^ 64 ∧ ∀ b : U64, r.val ≤ b.val → b.val < r.val + 8192 → + dev = false → ¬ (aligned.val ≤ b.val ∧ b.val < aligned.val + win.val * 2 ^ (zA nt.val us).val) := by + intro r hr + obtain ⟨o, z, ho, h1, h2⟩ := sets.roots r hr + have f := hH.fits o _ ho + have e : HKind.bits (.other z) = z := rfl + rw [e] at f + exact ⟨by omega, fun b b1 b2 => objOff o _ ho b (by omega) (by rw [e]; omega)⟩ + have hiV : ∀ r : U64, r.val + 8192 ≤ 2 ^ 64 → (wa r 4096#u64).val = r.val + 4096 := fun r h => by + rw [wa_val] + exact Nat.mod_eq_of_lt (by simp; omega) + have vsW : ∀ b, VsFootA Z'.Pp Z'.S1 Z'.Y (L.ksb s) b → + (VsFootA Z.Pp Z.S1 Z.Y (L.ksb s) b ∧ + (dev = false → ¬ (aligned.val ≤ b.val ∧ b.val < aligned.val + win.val * 2 ^ (zA nt.val us).val))) ∨ + (dev = false ∧ aligned.val ≤ b.val ∧ b.val < aligned.val + win.val * 2 ^ (zA nt.val us).val ∧ + nt.val ≠ 1 ∧ nt.val ≠ 4) := by + intro b hb + cases hb with + | asid i hi => + refine Or.inl ⟨.asid i hi, fun hd => ?_⟩ + have v := atAF_valA (kb := L.ksb s) ksw hi + have ks := utEnd_ksRegionA E hd + have pa := U.A.pa + have aw := U.A.aw + rcases ks with g | g <;> omega + | pool r l hr hl => + have old := (F.pp r hr).1 + obtain ⟨fit, off⟩ := pageW r (ptH r (Or.inl old)) + have v := peAF_valA fit hl + exact Or.inl ⟨.pool r l old hl, off _ (by omega) (by omega)⟩ + | pt r i hr hi => + rcases F.s1 r hr with ⟨old, -⟩ | new + · obtain ⟨fit, off⟩ := pageW r (ptH r (Or.inr old)) + have v := peAF_valA fit hi + exact Or.inl ⟨.pt r i old hi, off _ (by omega) (by omega)⟩ + · have v := peAF_valA (newX r new) hi + exact Or.inr (newW r new _ (by omega) (by omega)) + | high y i hy hi => + rcases F.y y hy with ⟨old, -⟩ | ⟨-, new⟩ + · obtain ⟨fit, off⟩ := rootW y (Or.inr old) + have e := hiV y fit + have v := peAF_valA (p := wa y 4096#u64) (by omega) hi + exact Or.inl ⟨.high y i old hi, off _ (by omega) (by omega)⟩ + · have v := peAF_valA (newX _ new) hi + exact Or.inr (newW _ new _ (by omega) (by omega)) + have exW : ∀ b, ExtFootE Z'.X Z'.Y Z'.T2 b → + (ExtFootE Z.X Z.Y Z.T2 b ∧ + (dev = false → ¬ (aligned.val ≤ b.val ∧ b.val < aligned.val + win.val * 2 ^ (zA nt.val us).val))) ∨ + (dev = false ∧ aligned.val ≤ b.val ∧ b.val < aligned.val + win.val * 2 ^ (zA nt.val us).val ∧ + nt.val ≠ 1 ∧ nt.val ≠ 4) := by + intro b hb + cases hb with + | s2lo r i hr hi => + rcases F.x r hr with ⟨old, -⟩ | ⟨new, -⟩ + · obtain ⟨fit, off⟩ := rootW r (Or.inl old) + have v := peAF_valA (p := r) (by omega) hi + exact Or.inl ⟨.s2lo r i old hi, off _ (by omega) (by omega)⟩ + · have v := peAF_valA (newX r new) hi + exact Or.inr (newW r new _ (by omega) (by omega)) + | s2hi r i hr hi => + rcases F.x r hr with ⟨old, -⟩ | ⟨-, new⟩ + · obtain ⟨fit, off⟩ := rootW r (Or.inl old) + have e := hiV r fit + have v := peAF_valA (p := wa r 4096#u64) (by omega) hi + exact Or.inl ⟨.s2hi r i old hi, off _ (by omega) (by omega)⟩ + · have v := peAF_valA (newX _ new) hi + exact Or.inr (newW _ new _ (by omega) (by omega)) + | s2pt t i ht hi => + rcases F.t2 t ht with ⟨old, -⟩ | new + · obtain ⟨fit, off⟩ := pageW t (sets.pages t (Or.inl (Or.inr old))) + have v := peAF_valA fit hi + exact Or.inl ⟨.s2pt t i old hi, off _ (by omega) (by omega)⟩ + · have v := peAF_valA (newX t new) hi + exact Or.inr (newW t new _ (by omega) (by omega)) + | wl0 y hy => + have h511 : (511#u64 : U64).val < 512 := by simp + have e511 : (511#u64 : U64).val = 511 := rfl + rcases F.y y hy with ⟨old, -⟩ | ⟨new, -⟩ + · obtain ⟨fit, off⟩ := rootW y (Or.inr old) + have v := peAF_valA (p := y) (by omega) h511 + exact Or.inl ⟨.wl0 y old, off _ (by omega) (by omega)⟩ + · have v := peAF_valA (newX y new) h511 + exact Or.inr (newW y new _ (by omega) (by omega)) + | wlo y i hy hi => + have hlt : i.val < 512 := by omega + rcases F.y y hy with ⟨old, -⟩ | ⟨new, -⟩ + · obtain ⟨fit, off⟩ := rootW y (Or.inr old) + have v := peAF_valA (p := y) (by omega) hlt + exact Or.inl ⟨.wlo y i old hi, off _ (by omega) (by omega)⟩ + · have v := peAF_valA (newX y new) hlt + exact Or.inr (newW y new _ (by omega) (by omega)) + -- every VCPU of the grown set is a 4 KiB object of the grown heap + have obj : ∀ v ∈ Z'.V, H' v = some (.other 12) := by + intro v hv + rcases vsp v hv with old | ⟨hd, h12, j, hj, e⟩ + · exact U.grow v _ (vh v old) + · rw [e] + exact U.hn hd j hj _ (by rw [h12]; rfl) + have al : ∀ v ∈ Z'.V, v.val % 4096 = 0 ∧ v.val + 4096 ≤ 2 ^ 64 := fun v hv => by + have a := hH'.align v _ (obj v hv) + have f := hH'.fits v _ (obj v hv) + rw [b12] at a f + exact ⟨a, f⟩ + have word : ∀ v ∈ Z'.V, ∀ b, (b = v ∨ b = wa v 24#u64) → + ¬ InOS (s1aRtSlots OS1 dev nt us aligned win) b ∧ ∀ t ∈ H'.tcbs, b.val < t.val ∨ t.val + 4096 ≤ b.val := by + intro v hv b hb + have bv : v.val ≤ b.val ∧ b.val < v.val + 4096 := by + rcases hb with e | e + · rw [e]; omega + · have : b.val = v.val + 24 := by + rw [e, wa_val] + exact Nat.mod_eq_of_lt (by have := (al v hv).2; simp; omega) + omega + refine utRt_wordApRstA E SP ?_ + rcases vsp v hv with old | ⟨hd, h12, j, hj, e⟩ + · refine Or.inl ⟨⟨(J.vc v old b hb).1, fun t ht => ?_⟩, + objOff v _ (vh v old) b bv.1 (by rw [b12]; exact bv.2)⟩ + · have ne : t ≠ v := fun e => by + have h : H1 t = some .tcb := ht + rw [e, vh v old] at h + cases h + have d := hH.disj t _ v _ ht (vh v old) ne + have e1 : HKind.bits .tcb = 12 := rfl + rw [e1, b12] at d + omega + · obtain ⟨jv, hle⟩ := bj_facts hj hfit + have hk : newKind nt.val us.val = some (.other 12) := by rw [h12]; rfl + have z12 : 2 ^ (zA nt.val us).val = 4096 := by + rw [← newKind_bitsA hk U.A.hn E.kept.us64, b12]; rfl + rw [← e] at jv hle + rw [z12] at hle jv + exact Or.inr ⟨hd, by omega, by rw [z12]; omega, by omega, by omega⟩ + refine + { sca := ScApart.ofPlaced hH' SP' + irqSt := fun o o1 o2 y hy => ?irq + vcAl := fun v hv => by + have := (al v hv).1 + show v.val % 8 = 0 + omega + vc := fun v hv b hb => ?vc + vs := fun b hb => utRt_wordApRstA E SP ((vsW b hb).imp (fun g => ⟨J.vs b g.1, g.2⟩) id) + ext := fun b hb => utRt_wordApRstA E SP ((exW b hb).imp (fun g => ⟨J.ext b g.1, g.2⟩) id) } + case irq => + rcases hy with hy | hy + · exact J.irqSt o o1 o2 y hy + · obtain ⟨hd, j, hj, i, -, hyv, -, hend, h32⟩ := newSl_in E.kept hfit hy + have ks := utEnd_ksRegionA E hd + have pa := U.A.pa + have aw := U.A.aw + have ov : (wa (L.ksb s) o).val = (L.ksb s).val + o.val := by + rw [wa_val] + exact Nat.mod_eq_of_lt (by omega) + rintro ⟨a, c⟩ + rcases ks with g | g <;> omega + case vc => + obtain ⟨h1, h2⟩ := word v hv b hb + refine ⟨h1, fun ⟨t, ht, K, hK, e⟩ => ?_⟩ + have f := hH'.fits t _ ht + have e1 : HKind.bits .tcb = 12 := rfl + rw [e1] at f + have : b.val = t.val + K.val := by + rw [e, wa_val] + exact Nat.mod_eq_of_lt (by omega) + rcases h2 t ht with g | g <;> omega + +end ap + +section sys +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z Z0 : XSetsA} {H H1 H' : Heap} {OS OS1 U U0 Ac0 Ud Ac Ac1 Vn Pp Tb Pp0 Tb0 : Set U64} + {Lv Lv0 : U64 → Nat} {N E0 Rp N1 E1 Rp1 : Set U64} {Bf Bf1 : Set (U64 × Fl4SpecA64.Word)} + {stack stack1 : U64 → List U64} {κ κ1 : U64 → U64} {n n' : Nat} {nA : U64} {J5 J5' : JSlotA S} + {s s2 q : S} {a a2 : A} {σ σ2 σ' : Fl4SpecA64.State Unit} {m m2 m' : Fl4SpecA64.McState} {ia ie lo hi : Nat} + {slot p sb fi aligned nt us dest off win : U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} {G' : MdbGhostA} + {P : ObjRef → Prop} {d : KernelObject} + +set_option maxHeartbeats 2000000 in +/-- **The entry-invariant bundle of a RAM creation after a reset** (module docstring). `Z0`, `H1`, `OS1`: the dropped +sets, heap and slots of the creation-start record `J2`. -/ +theorem utRt_sysRstA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E0 Rp Bf stack κ n J5 (J7A st (J7R2A st ar nA)) s a σ m) + (E : S1aRtIn st H Z.Cs OS U0 Ac0 s ia ie slot p sb fi false extra H1 H' OS1 s2 q lo hi aligned nt us dest off + win) + (hp : 0 < aligned.val) + -- A64 P1's creation top + (arm : ArmQIVAt st H Z.Cs OS ia ie s H' (s1aRtSlots OS1 false nt us aligned win) Vn q) + (ko : HeapKsOff H' (L.ksb s)) + (j7at : ∀ (Z'' : XSetsA) (σ'' : Fl4SpecA64.State Unit) (m'' : Fl4SpecA64.McState), + UtJ7SplitA st Z Z'' (s1aRtSlots OS1 false nt us aligned win) s q slot p sb nt us aligned win.val → + J7A st (J7R2A st ar nA) Z'' H' (s1aRtSlots OS1 false nt us aligned win) q σ'' m'') + -- the dropped sets, inside the record's + (cs : Z0.Cs = Z.Cs) (pp : Z0.Pp ⊆ Z.Pp) (s1' : Z0.S1 ⊆ Z.S1) (t2 : Z0.T2 ⊆ Z.T2) (x : Z0.X ⊆ Z.X) + (y : Z0.Y ⊆ Z.Y) (v : Z0.V ⊆ Z.V) + (offr : ∀ r, (r ∈ Z0.Pp ∨ r ∈ Z0.S1 ∨ r ∈ Z0.T2 ∨ r ∈ Z0.V) → UtOffRgA st s slot p sb r) + (offr2 : ∀ r, (r ∈ Z0.X ∨ r ∈ Z0.Y) → UtOffRgA st s slot p sb r ∧ UtOffRgA st s slot p sb (wa r 4096#u64)) + -- no cap at the decode's start names a dropped VCPU (the source had no children) + (vc0 : VcCap st Z0.V OS s) + -- the record and the creation at its start, on the dropped sets + (J2 : JStage3A st AR Z0 H1 OS1 Ud Ac1 Pp0 Tb0 Lv0 ia ie N1 E1 Rp1 Bf1 stack1 κ1 n J5' (J7A st (J7R2A st ar nA)) s2 a2 σ2 m2) + (hx : slot ∈ OS1) (hc : capA st s2 slot = .Untyped p sb 0#u64 false) + (h1 : p.val ≤ aligned.val) + (h2 : aligned.val + win.val * 2 ^ (zA nt.val us).val ≤ p.val + 2 ^ sb.val) + (w : WritesIn st s2 q (UtCrtWA st H1 OS1 s2 nt aligned (zA nt.val us) win.val)) + -- the grown heap + (hgrow : ∀ q k, H1 q = some k → H' q = some k) + (hex : ∀ q k, H' q = some k → H1 q = some k ∨ + ∃ j < win.val, q = bjA aligned (zA nt.val us) j ∧ newKind nt.val us.val = some k) + (hnew : ∀ j < win.val, ∀ k, newKind nt.val us.val = some k → H' (bjA aligned (zA nt.val us) j) = some k) + -- the scheduler bundle at the creation start + (Sch : UtAsmSchedInA st Z0 (utRtZA Z0 G' nt us aligned win.val (UtOfTyA 1 nt us aligned win.val) + (UtOfTyA 5 nt us aligned win.val) (UtOfTyA 12 nt us aligned win.val)) s2 q σ2 σ' m2 m' + (UtOfTyA 1 nt us aligned win.val) (UtOfTyA 5 nt us aligned win.val) P d n') : + (UtAsmSys0InA st ar nA Z (utRtZA Z0 G' nt us aligned win.val (UtOfTyA 1 nt us aligned win.val) + (UtOfTyA 5 nt us aligned win.val) (UtOfTyA 12 nt us aligned win.val)) H H' OS + (s1aRtSlots OS1 false nt us aligned win) Vn (utUserA H' U) Pp0 + (Tb0 ∪ UtSchedNewA nt us aligned win.val) Lv0 ia ie s q σ' m' slot p sb nt us aligned win.val) ∧ + utVsPtA st Pp0 (Tb0 ∪ UtSchedNewA nt us aligned win.val) Lv0 q ⊆ utVsPtA st Pp0 Tb0 Lv0 s2 := by + have ksq : L.ksb q = L.ksb s := E.ksb + have ks2 : L.ksb s2 = L.ksb s := E.creation.w1.1 + have ksq2 : L.ksb q = L.ksb s2 := by rw [ksq, ks2] + have split : UtJ7SplitA st Z (utRtZA Z0 G' nt us aligned win.val (UtOfTyA 1 nt us aligned win.val) + (UtOfTyA 5 nt us aligned win.val) (UtOfTyA 12 nt us aligned win.val)) + (s1aRtSlots OS1 false nt us aligned win) s q slot p sb nt us aligned win.val := + utRt_j7SplitA E cs pp s1' t2 x y v offr offr2 vc0 + (fun r h => h) (fun e r h => ⟨e, h⟩) + have kept := utRt_vsCrtKeptA (nt := nt) (us := us) (aligned := aligned) (win := win.val) J2 hx hc + (by simpa using h1) h2 w + have vsk : VsZ st (L.ksb s2) (utVsPtA st Pp0 Tb0 Lv0 s2) s2 q := VsZ.of_keep kept + have hps : PoolSet st Pp0 q = PoolSet st Pp0 s2 := poolSet_keepA ksq2 kept + have vs : VsTyR st (L.ksb q) (PoolSet st Pp0 q) ∅ (Tb0 ∪ UtSchedNewA nt us aligned win.val) Lv0 q := + (utVsTyR_keepA J2.sys.ent.vs ksq2 vsk hps).mono (Set.empty_subset _) Set.subset_union_left + refine ⟨?_, utVsPtA_retypeA J2.sys.ent.vs ksq2 vsk Set.subset_union_left⟩ + have eT : H'.tcbs = Z0.T ∪ UtOfTyA 1 nt us aligned win.val := utRt_tcbsNewA J2.tcbs hgrow hex hnew + have eC : H'.scs = Z0.C ∪ UtOfTyA 5 nt us aligned win.val := utRt_scsNewA J2.scs hgrow hex hnew + have h0 : (0#u64 : U64) ∈ Z0.Cs := by + obtain ⟨k, hk, -, h⟩ := J2.j.1.csN + exact (h 0#u64).2 (by simpa using hk) + have core : SchedCoreA st Z0.T Z0.Cs q Z0.Q Z0.R := utRt_coreKeptA J2.sr Sch.hks Sch.hm Sch.hnode + have core' : SchedCoreA st H'.tcbs Z0.Cs q Z0.Q Z0.R := by + rw [eT] + exact utRt_schedCoreA core h0 Sch.hTd Sch.hTa Sch.hTnode Sch.hTw + obtain ⟨Pt, Ub, -, -, armc2, -⟩ := J2.j7 + obtain ⟨Pt', Ub', -, -, armc', -⟩ := j7at _ σ' m' split + have ksw2 := J2.sys.ent.ksw + have split2 : UtJ7SplitA st Z0 (utRtZA Z0 G' nt us aligned win.val (UtOfTyA 1 nt us aligned win.val) + (UtOfTyA 5 nt us aligned win.val) (UtOfTyA 12 nt us aligned win.val)) + (s1aRtSlots OS1 false nt us aligned win) s q slot p sb nt us aligned win.val := + utRt_j7SplitA E (Z := Z0) rfl subset_rfl subset_rfl subset_rfl subset_rfl subset_rfl subset_rfl + offr offr2 vc0 (fun r h => h) (fun e r h => ⟨e, h⟩) + have A : SlotApA (utRtZA Z0 G' nt us aligned win.val (UtOfTyA 1 nt us aligned win.val) + (UtOfTyA 5 nt us aligned win.val) (UtOfTyA 12 nt us aligned win.val)) H' + (s1aRtSlots OS1 false nt us aligned win) (L.ksb s) := by + have g := J2.ap + rw [ks2] at g ksw2 + exact utRt_apRstA E g armc2.case.slot armc'.case.slot J2.sys.ent.heap armc2.vh J2.ptH J2.setsInH ksw2 split2 + (fun _ _ => rfl) + exact + { arm := arm + ko := ko + j7at := j7at + split := split + ksb := ksq + heapOK := E.heapOK + os := E.creation.osok E.kept hp + user := by + show Fl4ProofsA64.P1.UserOK st H'.tcbs Z0.Cs H'.omega H' (utUserA H' U) q + rw [cs]; exact utRt_userA J.sys.ent.user ksq + vs := vs + cs := cs + tcbs := eT + scs := eC + pp := J2.pp + s1 := fun r hr => (split2.s1 r hr).elim (fun g => Or.inl (J2.s1 g.1)) Or.inr + core := core' + sca := A.sca + irqSt := by rw [ksq]; exact A.irqSt + vcAl := A.vcAl + vc := A.vc + vsAp := by rw [ksq]; exact A.vs + ext := A.ext } + +end sys + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtApSchedA.lean b/proofs/Fl4ProofsA64/P3/UtApSchedA.lean new file mode 100644 index 000000000..abf4ec21c --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtApSchedA.lean @@ -0,0 +1,578 @@ +/- +P3 (aarch64, A64-P3 Untyped, full-record post, discharge): **the scheduler bundle `UtAsmSchedInA` of a RAM creation +without a reset**, from the record before the creation, the creation's end facts, the core post `JAtZAX` at the grown +sets, the spec side of the IPC bundle (`UtAsmIpcInA`) and the new objects' words. + +* `utRtBody_capsNewA`: after the spec body every slot holds an old cap of some slot, or a cap to no idle object. +* `utRt_schedInA`: the bundle. + +Hypothesis beyond the creation's facts: +* `coresAll` (record edit #5, Architect-approved 2026-12-04, to be projected from `JStage3A.coresAll` once it lands): + every platform core has a node. + +New theorems only. +-/ +import Fl4ProofsA64.P3.UtAsmSchedA +import Fl4ProofsA64.P3.UtApIpcA +import Fl4ProofsA64.P3.UtRtVcpuWordsA +import Fl4ProofsA64.P3.UtRtKeptA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +set_option maxRecDepth 100000 + +section spec +variable {σ : State Unit} {r : Unit × State Unit} {srcSlot : CslotPtr} {base retypeBase : Word} + {t : ApiObjectType} {objSz : Nat} {slots : List CslotPtr} {dev : Bool} + +/-- A default object holds null caps only. -/ +theorem utDefaultObject_specCapA (ht : t ≠ .untyped) {d : Domain} {q : ObjRef} {σ' : State Unit} + (h : σ'.kheap q = some (defaultObject t dev objSz d)) {sl : CslotPtr} (e : sl.1 = q) {c : Fl4SpecA64.Cap} + (hc : specCapAtA σ' sl = some c) : c = .nullCap := by + unfold specCapAtA at hc + rw [e, h] at hc + cases hd : defaultObject t dev objSz d with + | cnode sz cn => + rw [hd] at hc + simp only at hc + split_ifs at hc + exact utDefaultObject_capA ht (dev := dev) (n := objSz) (d := d) (b := sl.2) (by rw [hd]; exact hc) + | tcb x => + rw [hd] at hc + simp only at hc + exact utDefaultObject_capA ht (dev := dev) (n := objSz) (d := d) (b := sl.2) (by rw [hd]; exact hc) + | _ => rw [hd] at hc; cases hc + +/-- **The caps after the spec body** (module docstring). `idle`: no new address is an idle object. -/ +theorem utRtBody_capsNewA + (hr : r ∈ (utRtBodyA srcSlot base retypeBase t objSz slots dev σ).1) + {rS : Nat} {csS : CnodeContents} (hS : σ.kheap srcSlot.1 = some (.cnode rS csS)) {capS : Fl4SpecA64.Cap} + (hcapS : csS srcSlot.2 = some capS) + (hSa : srcSlot.1 ∉ fl4RetypeAddrs fl4ObjBitsApi retypeBase slots.length objSz t) + (hd : ∀ d ∈ slots, (∃ sz cn, σ.kheap d.1 = some (.cnode sz cn)) ∧ + d.1 ∉ fl4RetypeAddrs fl4ObjBitsApi retypeBase slots.length objSz t) + {m : McState} {n : Nat} + (idle : t ≠ .untyped → ∀ o ∈ fl4RetypeAddrs fl4ObjBitsApi retypeBase slots.length objSz t, + (∀ c < n, o ≠ m.idleThreadOf c) ∧ o ≠ idleScPtr) : + ∀ sl, (∃ sl0, specCapAtA r.2 sl = specCapAtA σ sl0) ∨ ∀ c, specCapAtA r.2 sl = some c → NoIdleCapV m n c := by + obtain ⟨-, hCn, hNew, hK, -, -, -⟩ := utRtBody_invA hr hS hcapS hSa hd + obtain ⟨idx', hidx⟩ : ∃ i' : Nat, i' = unat (retypeBase + BitVec.ofNat 64 slots.length <<< fl4ObjBitsApi t objSz - + base) := ⟨_, rfl⟩ + rw [← hidx] at hCn hK + intro sl + have nullOk : NoIdleCapV m n .nullCap := ⟨fun c _ h => (by cases h), fun b h => (by cases h), fun _ _ _ h => (by cases h)⟩ + by_cases g : t ≠ .untyped ∧ sl.1 ∈ fl4RetypeAddrs fl4ObjBitsApi retypeBase slots.length objSz t + · refine Or.inr (fun c hc => ?_) + rw [utDefaultObject_specCapA g.1 (hNew g.1 _ g.2) rfl hc] + exact nullOk + have og : t = .untyped ∨ sl.1 ∉ fl4RetypeAddrs fl4ObjBitsApi retypeBase slots.length objSz t := by + by_cases e : t = .untyped + · exact Or.inl e + exact Or.inr fun mem => g ⟨e, mem⟩ + have eqA : ∀ q, q ≠ srcSlot.1 → ∀ u : Fl4SpecA64.Cap, + (setObjA σ srcSlot.1 (.cnode rS (Function.update csS srcSlot.2 (some u)))).kheap q = σ.kheap q := + fun q h u => Function.update_of_ne h .. + by_cases c : ∃ sz cn, (setObjA σ srcSlot.1 (.cnode rS (Function.update csS srcSlot.2 + (some (.untypedCap dev base (bitsOf capS) idx'))))).kheap sl.1 = some (.cnode sz cn) + swap + · have ne : sl.1 ≠ srcSlot.1 := fun e => c ⟨rS, _, by rw [e]; exact Function.update_self ..⟩ + refine Or.inl ⟨sl, ?_⟩ + unfold specCapAtA + rw [hK _ og (fun sz cn k => c ⟨sz, cn, k⟩), eqA _ ne] + obtain ⟨sz, cn, k⟩ := c + obtain ⟨e, dm⟩ := hCn _ sz cn og k + have wf : wellFormedCnodeN sz (utWritesA t objSz dev + (slots.zip (fl4RetypeAddrs fl4ObjBitsApi retypeBase slots.length objSz t)) sl.1 cn) ↔ + wellFormedCnodeN sz cn := by + unfold wellFormedCnodeN + have : dom (utWritesA t objSz dev (slots.zip (fl4RetypeAddrs fl4ObjBitsApi retypeBase slots.length objSz t)) + sl.1 cn) = dom cn := by + ext j + exact not_congr (dm j) + rw [this] + have eR : specCapAtA r.2 sl = if wellFormedCnodeN sz cn then + utWritesA t objSz dev (slots.zip (fl4RetypeAddrs fl4ObjBitsApi retypeBase slots.length objSz t)) sl.1 cn sl.2 + else none := by + unfold specCapAtA + rw [e] + simp only [wf] + by_cases w : wellFormedCnodeN sz cn + swap + · refine Or.inr (fun c hc => ?_) + rw [eR, ite_eq_right w] at hc + cases hc + rw [ite_eq_left w] at eR + rcases utWritesA_casesA (t := t) (bits := objSz) (dev := dev) (q := sl.1) (idx := sl.2) + (slots.zip (fl4RetypeAddrs fl4ObjBitsApi retypeBase slots.length objSz t)) cn with h | ⟨y, hy, -, h⟩ + swap + · refine Or.inr (fun c hc => ?_) + rw [eR, h] at hc + obtain rfl := Option.some.inj hc + have m2 : y.2 ∈ fl4RetypeAddrs fl4ObjBitsApi retypeBase slots.length objSz t := + (List.of_mem_zip (show (y.1, y.2) ∈ _ from hy)).2 + refine ⟨fun c hc' e' => ?_, fun b e' => ?_, fun p' a' v' e' => ?_⟩ + · have : t = .tcbObject ∧ y.2 = m.idleThreadOf c := by + cases t with + | tcbObject => injection e' with e1; exact ⟨rfl, e1⟩ + | _ => exact Fl4SpecA64.Cap.noConfusion e' + exact (idle (by rw [this.1]; simp) _ m2).1 c hc' this.2 + · have : t = .schedContextObject ∧ y.2 = idleScPtr := by + cases t with + | schedContextObject => injection e' with e1; exact ⟨rfl, e1⟩ + | _ => exact Fl4SpecA64.Cap.noConfusion e' + exact (idle (by rw [this.1]; simp) _ m2).2 this.2 + · cases t with + | archObject aobj => cases aobj <;> cases e' + | _ => exact Fl4SpecA64.Cap.noConfusion e' + rw [h] at eR + by_cases es : sl.1 = srcSlot.1 + · have k' : (setObjA σ srcSlot.1 (.cnode rS (Function.update csS srcSlot.2 + (some (.untypedCap dev base (bitsOf capS) idx'))))).kheap sl.1 = some (.cnode rS (Function.update csS + srcSlot.2 (some (.untypedCap dev base (bitsOf capS) idx')))) := by rw [es]; exact Function.update_self .. + rw [k'] at k + injection (Option.some.inj k) with k1 k2 + subst k1 k2 + by_cases ei : sl.2 = srcSlot.2 + · refine Or.inr (fun c hc => ?_) + rw [eR, ei, Function.update_self] at hc + obtain rfl := Option.some.inj hc + exact ⟨fun c _ h => (by cases h), fun b h => (by cases h), fun _ _ _ h => (by cases h)⟩ + · rw [Function.update_of_ne ei] at eR + refine Or.inl ⟨sl, ?_⟩ + have dm' : dom (Function.update csS srcSlot.2 (some (.untypedCap dev base (bitsOf capS) idx'))) = dom csS := by + ext j + by_cases j1 : j = srcSlot.2 + · subst j1 + show _ ≠ none ↔ _ ≠ none + rw [Function.update_self, hcapS] + simp + · show _ ≠ none ↔ _ ≠ none + rw [Function.update_of_ne j1] + have w0 : wellFormedCnodeN rS csS := by + have := w + unfold wellFormedCnodeN at this ⊢ + rwa [← dm'] + rw [eR] + unfold specCapAtA + rw [es, hS] + simp only + rw [ite_eq_left w0] + · rw [eqA _ es] at k + refine Or.inl ⟨sl, ?_⟩ + rw [eR] + unfold specCapAtA + rw [k] + simp only + rw [ite_eq_left w] + +end spec + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H H' : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s s' : S} {a a' : A} {σ : State Unit} {m : McState} {x p sb fi : U64} + {nt us aligned dest off win : U64} {G' : MdbGhostA} + +/-- A node word other than node 0's debug head is not the debug head. -/ +theorem utRt_nodeNeNwA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {c o : U64} (hc : c ∈ Z.Cs) (ho : o.val < 4288) (h88 : o.val ≠ 88) : L.nodeAt s c o ≠ nwA st s := by + intro e + have h0 : (0#u64 : U64) ∈ Z.Cs := by + obtain ⟨k, hk, -, h⟩ := J.j.1.csN + exact (h 0#u64).2 (by simpa using hk) + have g := congrArg UScalar.val e + have e0 : nwA st s = L.nodeAt s 0#u64 layout.node.DEBUG_TCBS := rfl + rw [e0, nodeAt_val (J.sq.nodes c hc) ho, nodeAt_val (J.sq.nodes 0#u64 h0) (by rw [debug_val]; omega), + debug_val] at g + have b : L.nodeBase s c = L.nodeBase s 0#u64 + c.val * 4288 := by + unfold LawfulStore.nodeBase + simp + omega + +/-- **The scheduler bundle of a RAM creation without a reset** (module docstring). -/ +theorem utRt_schedInA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (hx : x ∈ OS) (hc : capA st s x = .Untyped p sb fi false) + (W : UtWinZA nt aligned (zA nt.val us) win.val) + (h1 : p.val + fi.val ≤ aligned.val) + (h2 : aligned.val + win.val * 2 ^ (zA nt.val us).val ≤ p.val + 2 ^ sb.val) + (hb0 : 0 < aligned.val) + (Ec : CrtE st H H.frame OS s s' x p sb false aligned nt us dest off win) + (w : WritesIn st s s' (UtCrtWA st H OS s nt aligned (zA nt.val us) win.val)) + -- the new TCBs' and VCPUs' words + (X : DbgX st H s nt aligned (zA nt.val us) s' win.val) + (WT : nt.val = 1 → WinT st s' aligned (zA nt.val us) win.val) + (TW : nt.val = 1 → UtTcbZW st aligned (zA nt.val us) win.val s') + (VW : nt.val = 12 → UtVcpuZW st aligned (zA nt.val us) win.val s') + -- the spec body + {t : ApiObjectType} (htt : objTypeOfA nt.val = some t) (hus : us.val < 64) (hsc : nt.val = 5 → 7 ≤ us.val) + {srcSlot : CslotPtr} (hsrcS : ∃ q, srcSlot.1 = specPtr q ∧ Z.th q = some (.cnode srcSlot.2.length)) + {slots : List CslotPtr} (hlen : slots.length = win.val) + (hdst : ∀ d ∈ slots, ∃ q, d.1 = specPtr q ∧ Z.th q = some (.cnode d.2.length)) + {r : Unit × State Unit} + (hr : r ∈ (utRtBodyA srcSlot (specPtr p) (specPtr aligned) t us.val slots false σ).1) + (K : JAtZAX (L := L) AR (utRtZA Z G' nt us aligned win.val (UtOfTyA 1 nt us aligned win.val) + (UtOfTyA 5 nt us aligned win.val) (UtOfTyA 12 nt us aligned win.val)) s' a' r.2 + (utRtMA m (UtOfTyA 1 nt us aligned win.val))) + -- the IPC bundle + (I : UtAsmIpcInA st Z (utRtThA Z.th false nt us aligned win.val) H H' N E Rp Bf + (UtOfTyA 3 nt us aligned win.val) (UtOfTyA 2 nt us aligned win.val) (UtOfTyA 6 nt us aligned win.val) + (UtOfTyA 1 nt us aligned win.val) (UtOfTyA 5 nt us aligned win.val) (UtNewA false nt us aligned win.val) + s s' σ r.2 m (utRtMA m (UtOfTyA 1 nt us aligned win.val)) + (fun q => t ≠ .untyped ∧ UtNewSpA aligned (zA nt.val us) win.val q) + (utNewDA t us.val σ.curDomain)) + -- record edit #5 + (coresAll : ∀ k < Fl4SpecA64.fl4A64Platform.numNodes, ∃ c ∈ Z.Cs, c.val = k) : + UtAsmSchedInA st Z (utRtZA Z G' nt us aligned win.val (UtOfTyA 1 nt us aligned win.val) + (UtOfTyA 5 nt us aligned win.val) (UtOfTyA 12 nt us aligned win.val)) s s' σ r.2 m + (utRtMA m (UtOfTyA 1 nt us aligned win.val)) (UtOfTyA 1 nt us aligned win.val) + (UtOfTyA 5 nt us aligned win.val) + (fun q => t ≠ .untyped ∧ UtNewSpA aligned (zA nt.val us) win.val q) + (utNewDA t us.val σ.curDomain) n := by + have hn : nt.val < 15 := (objTypeOfA_some htt us.val).1 + have Cl : UtClrA st Z H s aligned.val (aligned.val + win.val * 2 ^ (zA nt.val us).val) := + utRt_winClrA J hx hc h1 h2 + have anchor : ∀ j, j < win.val → specPtr (bjA aligned (zA nt.val us) j) ≠ Fl4SpecA64.idleScPtr := by + intro j hj + have v := utRt_newValA W hj + exact utRet_anchorA (st := st) J.idleAnchor hx hc (by omega) (by omega) + have thFree : ∀ q, UtNewA false nt us aligned win.val q → Z.th q = none := utRt_ipcFreeA J hx hc W h1 h2 + have memA : ∀ q, q ∈ fl4RetypeAddrs fl4ObjBitsApi (specPtr aligned) slots.length us.val t ↔ + UtNewSpA aligned (zA nt.val us) win.val q := fun q => by + rw [hlen, utRet_addrs_memA htt hus] + rfl + have thOff : ∀ q k, Z.th q = some k → ¬ UtNewSpA aligned (zA nt.val us) win.val (specPtr q) := fun q k hq => + utRt_relOffA (m := m) J Cl W rfl rfl anchor (Or.inl ⟨q, by rw [hq]; simp, rfl⟩) + have specFree : ∀ q, UtNewSpA aligned (zA nt.val us) win.val q → σ.kheap q = none := by + rintro q ⟨j, hj, rfl⟩ + exact utRt_ipcSpecFreeA J hx hc W h1 h2 ⟨j, hj, rfl⟩ + have footK : ∀ b : U64, FootNA (L := L) Z s b → L.mem s' b = L.mem s b := fun b h => + utRt_footKeptA J hx hc h1 h2 w h + -- a new object + have bjv : ∀ j, j < win.val → (bjA aligned (zA nt.val us) j).val = aligned.val + j * 2 ^ (zA nt.val us).val ∧ + aligned.val + j * 2 ^ (zA nt.val us).val + 2 ^ (zA nt.val us).val ≤ + aligned.val + win.val * 2 ^ (zA nt.val us).val := by + intro j hj + have h1' : (j + 1) * 2 ^ (zA nt.val us).val ≤ win.val * 2 ^ (zA nt.val us).val := Nat.mul_le_mul_right _ hj + rw [Nat.add_mul, Nat.one_mul] at h1' + have fit := W.fit + exact ⟨bjA_val (by have := Nat.two_pow_pos (zA nt.val us).val; omega), by omega⟩ + have bjal : ∀ j, j < win.val → (bjA aligned (zA nt.val us) j).val % 2 ^ (zA nt.val us).val = 0 := by + intro j hj + rw [(bjv j hj).1, Nat.add_mul_mod_self_right] + exact W.al + have tcbZ : nt.val = 1 → (zA nt.val us).val = 12 := fun h => by rw [h]; exact zA_1 us + have tcbAddr : ∀ u ∈ UtOfTyA 1 nt us aligned win.val, TcbAddr u := by + rintro u ⟨e1, j, hj, rfl⟩ + have z := tcbZ e1 + have v := bjv j hj + have al := bjal j hj + have fit := W.fit + rw [z] at v al fit + exact ⟨by omega, al, by omega⟩ + -- a word of a new TCB outside `TcbInitK` + have tcbWz : nt.val = 1 → ∀ j < win.val, ∀ (K : U64) (k : Nat), K.val = k → k < 4096 → k % 8 = 0 → + ¬ TcbInitK k → L.mem s' (wa (bjA aligned (zA nt.val us) j) K) = 0#u64 := by + intro e1 j hj K k ek hk k8 ni + have z := tcbZ e1 + have v := bjv j hj + have fit := W.fit + have al : aligned.val % 4096 = 0 := by + have g := W.al + rwa [z] at g + have X12 : (2 : Nat) ^ 12 = 4096 := by norm_num + rw [z, X12] at v fit + have wv : (wa (bjA aligned (zA nt.val us) j) K).val = aligned.val + j * 4096 + k := by + rw [wa_valP, ek, v.1] + exact Nat.mod_eq_of_lt (by omega) + have hal : Aligned (wa (bjA aligned (zA nt.val us) j) K) := by + show _ % 8 = 0 + rw [wv] + omega + have h2' : aligned.val + win.val * 2 ^ (zA nt.val us).val = aligned.val + win.val * 4096 := by rw [z]; norm_num + rcases WT e1 _ hal (by rw [wv]; omega) (by rw [wv, h2']; omega) with g | ⟨i, -, K', hK, e⟩ + · exact g + · rw [wv, z, X12] at e + have hK4 : K' < 4096 := hK.2 + have : K' = k := by omega + exact absurd (this ▸ hK) ni + -- the new SCs + have scT : ∀ c ∈ UtOfTyA 5 nt us aligned win.val, t = .schedContextObject ∧ (zA nt.val us).val = us.val ∧ + 7 ≤ us.val := by + rintro c ⟨e5, -⟩ + refine ⟨(objTypeOfA_some htt us.val).2.2.2.2.2.2 e5, ?_, hsc e5⟩ + rw [zA_valA nt us hn hus, e5] + unfold zOfA + simp + have scSp : ∀ c ∈ UtOfTyA 5 nt us aligned win.val, + r.2.kheap (specPtr c) = some (.schedContext defaultSchedContext (us.val - minSchedContextBits)) := by + intro c hc' + obtain ⟨et, -, -⟩ := scT c hc' + obtain ⟨-, j, hj, rfl⟩ := hc' + have o := I.spec.new (specPtr (bjA aligned (zA nt.val us) j)) ⟨by rw [et]; simp, j, hj, rfl⟩ + rw [utNewDA_ne (by rw [et]; simp), et] at o + exact o + have scK : ∀ c ∈ UtOfTyA 5 nt us aligned win.val, ∀ k, + r.2.kheap (specPtr c) = some (.schedContext defaultSchedContext k) → + k + minSchedContextBits = (zA nt.val us).val := by + intro c hc' k hk + obtain ⟨-, ez, h7⟩ := scT c hc' + rw [scSp c hc'] at hk + injection (Option.some.inj hk) with _ e + have : minSchedContextBits = 7 := rfl + omega + have scIn : ∀ c ∈ UtOfTyA 5 nt us aligned win.val, ∀ k, + r.2.kheap (specPtr c) = some (.schedContext defaultSchedContext k) → ∀ b, ScObjA c k b → + aligned.val ≤ b.val ∧ b.val < aligned.val + win.val * 2 ^ (zA nt.val us).val := by + intro c hc' k hk b hb + have e := scK c hc' k hk + obtain ⟨-, j, hj, rfl⟩ := hc' + have v := bjv j hj + unfold ScObjA at hb + rw [e] at hb + omega + have scOld : ∀ c' ∈ Z.C, ∀ y k, σ.kheap (scNameA m c') = some (.schedContext y k) → ∀ b, ScObjA c' k b → + b.val < aligned.val ∨ aligned.val + win.val * 2 ^ (zA nt.val us).val ≤ b.val := by + intro c' hc' y k hk b hb + have sz := (J.scSize c' hc' y k hk).1 + have hcs : c' ∈ H.scs := by rw [J.scs]; exact hc' + have d := Cl.obj c' _ (JointObjectA.of_sc hcs) + have : 2 ^ (k + minSchedContextBits) ≤ 2 ^ H.scBits c' := Nat.pow_le_pow_right (by norm_num) sz + unfold ScObjA at hb + omega + have idleC : specPtr (L.mem s (L.nodeAt s (L.cpu s) layout.node.IDLE_SC)) = m.idleScOf m.curCore := by + rw [J.j.1.sch.core] + exact (J.j.1.sch.idleScOf _ J.j.1.sch.cpuCs).symm + have newSc : UtNewScA st (utRtThA Z.th false nt us aligned win.val) Z.C (UtOfTyA 5 nt us aligned win.val) m s s' σ r.2 := by + have nIdle : ∀ c ∈ UtOfTyA 5 nt us aligned win.val, ¬ IdleScA m c := by + rintro c hc' ⟨k, hk, e⟩ + obtain ⟨cw, hcw, ev⟩ := coresAll k hk + have g := J.j.1.sch.idleScOf cw hcw + rw [ev, e] at g + have ic := J.j.1.idleC cw hcw + rw [← ut_specPtr_injA g] at ic + exact I.newCfresh c hc' ic + refine + { fresh := I.newCfresh + nameEq := fun c hc' => ite_eq_right (fun e => by + have ic := J.j.1.idleC _ J.j.1.sch.cpuCs + rw [← idleC] at e + rw [← ut_specPtr_injA e] at ic + exact I.newCfresh c hc' ic) + notIdle := nIdle + notIdlePtr := fun c ⟨_, j, hj, e⟩ => by rw [e]; exact anchor j hj + obj := fun c hc' => ?_ + core := fun c hc' => ?_ + apart := fun c hc' c' hc'' ne k k' hk hk' b hb hb' => ?_ + off := fun c hc' k hk b hb => ?_ + oldOff := fun c hc' y k hk b hb q kd hq => ?_ } + · refine ⟨_, scSp c hc', ?_⟩ + obtain ⟨et, ez, h7⟩ := scT c hc' + have e5 := hc'.1 + obtain ⟨-, j, hj, rfl⟩ := hc' + have v := bjv j hj + have al := bjal j hj + have fit := W.fit + have ek : us.val - minSchedContextBits + minSchedContextBits = us.val := by + have : minSchedContextBits = 7 := rfl + omega + have wk : ¬ WKA nt := by unfold WKA; omega + have big : 128 ≤ 2 ^ (zA nt.val us).val := by + rw [ez, show (128 : Nat) = 2 ^ 7 by norm_num] + exact Nat.pow_le_pow_right (by norm_num) h7 + have zz : ∀ K : U64, K.val < 128 → K.val % 8 = 0 → L.mem s' (wa (bjA aligned (zA nt.val us) j) K) = 0#u64 := by + intro K k1 k8 + exact utRt_newZeroA J hx hc W h1 h2 Ec wk hj (by omega) k8 (by omega) + (utRt_newWordA J hx hc W h1 h2 hj K rfl (by omega)) + refine ut_scRelA (st := st) _ (nIdle _ ⟨e5, j, hj, rfl⟩) s' _ _ ?_ ?_ ?_ ?_ + · rw [ek] + refine ⟨hus, ?_, ?_⟩ + · rw [← ez]; exact al + · rw [← ez]; omega + · rw [periodA_eqS] + exact zz _ (by simp) (by simp) + · rw [maxA_eqS] + exact zz _ (by simp) (by simp) + · intro K hK + refine zz K ?_ ?_ <;> rcases hK with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> simp + · have e5 := hc'.1 + obtain ⟨-, ez, h7⟩ := scT c hc' + obtain ⟨-, j, hj, rfl⟩ := hc' + have wk : ¬ WKA nt := by unfold WKA; omega + have big : 128 ≤ 2 ^ (zA nt.val us).val := by + rw [ez, show (128 : Nat) = 2 ^ 7 by norm_num] + exact Nat.pow_le_pow_right (by norm_num) h7 + have kv : layout.sc.CORE.val < 128 ∧ layout.sc.CORE.val % 8 = 0 := by + unfold layout.sc.CORE; decide + exact utRt_newZeroA J hx hc W h1 h2 Ec wk hj (by omega) kv.2 (by omega) + (utRt_newWordA J hx hc W h1 h2 hj _ rfl (by omega)) + · have e := scK c hc' k hk + have e' := scK c' hc'' k' hk' + obtain ⟨-, j, hj, rfl⟩ := hc' + obtain ⟨-, l, hl, rfl⟩ := hc'' + have jl : j ≠ l := fun h => ne (by rw [h]) + have v := bjv j hj + have v' := bjv l hl + unfold ScObjA at hb hb' + rw [e] at hb + rw [e'] at hb' + rcases Nat.lt_or_ge j l with g | g + · have := Nat.mul_le_mul_right (2 ^ (zA nt.val us).val) (Nat.succ_le_of_lt g) + rw [Nat.succ_mul] at this + omega + · have := Nat.mul_le_mul_right (2 ^ (zA nt.val us).val) (Nat.succ_le_of_lt (Nat.lt_of_le_of_ne g jl.symm)) + rw [Nat.succ_mul] at this + omega + · have bw := scIn c hc' k hk b hb + refine ⟨fun q kd hq => ?_, ?_, fun c' hcc y k' hk' hb' => ?_⟩ + · rcases utRtTh_splitA hq with ⟨-, o⟩ | ⟨g, -⟩ + · rcases utEnd_thDisjA (Z := Z) J.objAlloc hx hc h1 h2 q kd o with g | g <;> omega + · exact absurd hc'.1 g.2.2.1.1 + · have := Cl.ks + unfold ksSizeA + omega + · rcases scOld c' hcc y k' hk' b hb' with g | g <;> omega + · have off := scOld c hc' y k hk b hb + rcases utRtTh_splitA hq with ⟨-, o⟩ | ⟨g, rfl⟩ + · exact (J.sr.scp.out c hc' y k hk b hb).1 q kd o + · obtain ⟨-, g0, -, j, hj, rfl⟩ := g + have v := bjv j hj + rw [utKind_sizeA hn g0 hus] + omega + obtain ⟨qS, eqS, hqS⟩ := hsrcS + obtain ⟨csS, hkS, -, hsS⟩ := J.j.1.st.cnode qS _ hqS + rw [← eqS] at hkS + obtain ⟨capS, hcapS, -⟩ := hsS srcSlot.2 rfl + have hdc : ∀ d ∈ slots, (∃ sz cn, σ.kheap d.1 = some (.cnode sz cn)) ∧ + d.1 ∉ fl4RetypeAddrs fl4ObjBitsApi (specPtr aligned) slots.length us.val t := by + intro d hd + obtain ⟨q, eq, hq⟩ := hdst d hd + obtain ⟨cs, hk, -⟩ := J.j.1.st.cnode q _ hq + rw [eq, memA] + exact ⟨⟨_, cs, hk⟩, thOff q _ hq⟩ + have hSa : srcSlot.1 ∉ fl4RetypeAddrs fl4ObjBitsApi (specPtr aligned) slots.length us.val t := by + rw [eqS, memA] + exact thOff qS _ hqS + have caps := utRtBody_capsNewA (m := m) (n := n) hr hkS hcapS hSa hdc (fun _ o ho => by + rw [memA] at ho + refine ⟨fun c hc' e => ?_, ?_⟩ + · obtain ⟨y, hy, -⟩ := J.sidle.f3 c hc' + rw [← e, specFree o ho] at hy + cases hy + · obtain ⟨j, hj, rfl⟩ := ho + exact anchor j hj) + refine + { spec := I.spec + eT := rfl + eC := rfl + eCs := rfl + eQ := rfl + eR := rfl + subV := Set.subset_union_left + hks := w.1 + hcpu := w.2.1 + hids := rfl + hidt := rfl + hcc := rfl + sch := K.1.sch + rest := K.1.rest + thT := K.1.thT + place := K.1.place + hm := fun b al h ne => utRt_rdKeptA J hx hc h1 h2 w al (Or.inr ⟨h, ne⟩) + hnode := fun c hc' o ho o8 o88 => by + have nc := J.sq.nodes c hc' + have al : Aligned (L.nodeAt s c o) := by + show _ % 8 = 0 + rw [nodeAt_val nc ho] + have := nc.align + omega + have O := utRt_nodeOffA J hc' ho rfl al (utRt_nodeNeNwA J hc' ho o88) + refine w.mem_eq al ?_ + rintro (h | ⟨-, g1, g2⟩ | h | ⟨u, hu, h⟩) + · exact O.1 h + · rw [nodeAt_val nc ho] at g1 g2 + rcases Cl.nodes c hc' with g | g <;> omega + · exact O.2.2 h + · exact O.2.1 u hu h + hTd := fun u hu h => by + have g := I.grow.newT u hu + have := I.grow.oldT u h + rw [I.grow.fresh u g.1] at this + cases this + hTa := tcbAddr + hTnode := fun c hc' u hu => by + obtain ⟨e1, j, hj, rfl⟩ := hu + have z := tcbZ e1 + have v := bjv j hj + have eb : L.nodeBase s' c = L.nodeBase s c := by + unfold LawfulStore.nodeBase + rw [w.1] + rw [eb] + have X12 : 2 ^ (zA nt.val us).val = 2 ^ 12 := by rw [z] + rcases Cl.nodes c hc' with g | g <;> omega + hTw := fun u hu => by + obtain ⟨e1, j, hj, rfl⟩ := hu + refine ⟨(X.2.2 j hj e1).2.2.1, ?_, ?_⟩ + · rw [prioA_eq']; exact tcbWz e1 j hj _ 3448 rfl (by omega) (by omega) (by unfold TcbInitK; omega) + · exact tcbWz e1 j hj 3592#u64 3592 rfl (by omega) (by omega) (by unfold TcbInitK; omega) + newSc := newSc + hdt := fun y e => by + rw [utNewDA_tcbA e] + exact ⟨rfl, rfl, rfl⟩ + hds := fun y k e => by + rw [(utNewDA_scA e).2] + exact ⟨rfl, rfl⟩ + hdn := fun y e => by + rw [utNewDA_ntfnA e] + rfl + scNe := fun c ⟨_, j, hj, e⟩ h => by + have v := bjv j hj + rw [← e, h] at v + have : (0#u64 : U64).val = 0 := rfl + omega + tvk := fun u hu => footK _ (.tcb u _ hu (Or.inr (Or.inr (Or.inr (Or.inl rfl))))) + tvn := fun u hu => by + obtain ⟨e1, j, hj, rfl⟩ := hu + exact TW e1 j hj _ (Or.inr (Or.inl rfl)) + vcw := fun v hv => by + rcases hv with old | ⟨e12, j, hj, rfl⟩ + · exact Or.inl ⟨old, footK _ (.vcpu v _ old (Or.inr rfl))⟩ + · exact Or.inr ((VW e12 j hj).1 _ (Or.inr rfl)) + vcw0 := fun v hv => by + rcases hv with old | ⟨e12, j, hj, rfl⟩ + · exact Or.inl old + · exact Or.inr (by simpa only [wa_zeroVcA] using (VW e12 j hj).1 0#u64 (Or.inl rfl)) + idle := I.spec.glob.idle + idleSc := fun ⟨_, j, hj, e⟩ => anchor j hj e.symm + newCap := caps + vk := fun v hv => footK _ (.vcpu v _ hv (Or.inl rfl)) + runk := fun u hu => footK _ (.tcb u _ hu (Or.inr (Or.inr (Or.inr (Or.inr rfl))))) + runn := fun u hu => by + obtain ⟨e1, j, hj, rfl⟩ := hu + exact TW e1 j hj _ (Or.inr (Or.inr rfl)) + stk := fun u hu => footK _ (.tcb u _ hu (Or.inl rfl)) + tbk := fun u hu => by + have f := foot_blk J.iplaced.n hu + exact I.footN _ f.1 f.2 + newT := fun u hu => by + obtain ⟨e1, j, hj, rfl⟩ := hu + have et : t = .tcbObject := by + have h := e1 ▸ htt + simp [objTypeOfA, eq_comm] at h + exact h + exact ⟨by rw [et]; simp, j, hj, rfl⟩ } + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtApSpec2A.lean b/proofs/Fl4ProofsA64/P3/UtApSpec2A.lean new file mode 100644 index 000000000..4ff2a6e69 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtApSpec2A.lean @@ -0,0 +1,449 @@ +/- +P3 (aarch64, A64-P3 Untyped, full-record post, discharge): **the rest of the spec-side bundle**. `utRt_specInA` +left the idle node words, the new original caps, the CNodes and the body's preconditions; here they come from the +record and the body's outcome, so `UtAsmSpecInA` of a RAM creation has no side hypothesis. + +* `utWritesA_hitA`: a slot written by some pair of the list holds a default cap after the fold. +* `utRtBody_origA`: every original cap after the body is an old one at an unchanged slot, or sits on an object that + existed before (the source or a destination) and is not null. +* `utRt_specAllA`: the bundle. + +No named premise. New theorems only. +-/ +import Fl4ProofsA64.P3.UtApSpecA +import Fl4ProofsA64.P3.UtApSchedA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Sched Fl4SpecA64 Fl4Spec Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +set_option maxRecDepth 100000 + +/-- A slot written by some pair of the list holds a default cap after the fold of writes. -/ +theorem utWritesA_hitA {t : ApiObjectType} {bits : Nat} {dev : Bool} {q : ObjRef} {idx : CnodeIndex} + (l : List (CslotPtr × ObjRef)) (cn : CnodeContents) (h : ∃ x ∈ l, x.1 = (q, idx)) : + ∃ o, utWritesA t bits dev l q cn idx = some (defaultCap t o bits dev) := by + induction l generalizing cn with + | nil => + obtain ⟨x, hx, -⟩ := h + cases hx + | cons a l ih => + have step : utWritesA t bits dev (a :: l) q cn = utWritesA t bits dev l q + (if a.1.1 = q then Function.update cn a.1.2 (some (defaultCap t a.2 bits dev)) else cn) := by + unfold utWritesA + rw [List.foldl_cons] + rw [step] + by_cases later : ∃ x ∈ l, x.1 = (q, idx) + · exact ih _ later + · obtain ⟨x, hx, e⟩ := h + rcases List.mem_cons.1 hx with rfl | hx' + swap + · exact absurd ⟨x, hx', e⟩ later + have off : ∀ y ∈ l, y.1 ≠ (q, idx) := fun y hy e' => later ⟨y, hy, e'⟩ + have e1 : x.1.1 = q := by rw [e] + have e2 : x.1.2 = idx := by rw [e] + refine ⟨x.2, ?_⟩ + rw [utWritesA_off l _ off, ite_eq_left e1, e2, Function.update_self] + +/-- A default cap is never null. -/ +theorem utDefaultCap_neNullA (t : ApiObjectType) (o : ObjRef) (bits : Nat) (dev : Bool) : + defaultCap t o bits dev ≠ .nullCap := by + cases t with + | archObject a => + intro h + exact Fl4SpecA64.Cap.noConfusion h + | _ => exact fun h => Fl4SpecA64.Cap.noConfusion h + +section +variable {σ : State Unit} {r : Unit × State Unit} {srcSlot : CslotPtr} {base retypeBase : Word} + {t : ApiObjectType} {objSz : Nat} {slots : List CslotPtr} {dev : Bool} + +/-- **The original caps after the body** (module docstring). `horig`: an old original slot is a slot of an object +that is a CNode or a TCB of the old spec heap. -/ +theorem utRtBody_origA + (hr : r ∈ (utRtBodyA srcSlot base retypeBase t objSz slots dev σ).1) + {rS : Nat} {csS : CnodeContents} (hS : σ.kheap srcSlot.1 = some (.cnode rS csS)) + (hSwf : wellFormedCnodeN rS csS) {d0 : Bool} {w : Word} {sbS iS : Nat} + (hcapS : csS srcSlot.2 = some (.untypedCap d0 w sbS iS)) + (hSa : srcSlot.1 ∉ fl4RetypeAddrs fl4ObjBitsApi retypeBase slots.length objSz t) + (hd : ∀ d ∈ slots, (∃ cn, σ.kheap d.1 = some (.cnode d.2.length cn) ∧ wellFormedCnodeN d.2.length cn) ∧ + d.1 ∉ fl4RetypeAddrs fl4ObjBitsApi retypeBase slots.length objSz t) + (hold : ∀ sl, σ.isOriginalCap sl = true → sl.1 ∉ fl4RetypeAddrs fl4ObjBitsApi retypeBase slots.length objSz t) + (hlen : slots.length ≤ (fl4RetypeAddrs fl4ObjBitsApi retypeBase slots.length objSz t).length) : + ∀ sl, r.2.isOriginalCap sl = true → + (σ.isOriginalCap sl = true ∧ specCapAtA r.2 sl = specCapAtA σ sl) ∨ + ((sl = srcSlot ∨ sl ∈ slots) ∧ specCapAtA r.2 sl ≠ some .nullCap) ∨ + (σ.isOriginalCap sl = true ∧ specCapAtA σ sl = none ∧ specCapAtA r.2 sl ≠ some .nullCap) := by + have hd' : ∀ d ∈ slots, (∃ sz cn, σ.kheap d.1 = some (.cnode sz cn)) ∧ + d.1 ∉ fl4RetypeAddrs fl4ObjBitsApi retypeBase slots.length objSz t := fun d h => by + obtain ⟨⟨cn, k, -⟩, g⟩ := hd d h + exact ⟨⟨_, cn, k⟩, g⟩ + obtain ⟨-, hCn, -, hK, -, horig, -⟩ := utRtBody_invA hr hS hcapS hSa hd' + have eb : bitsOf (.untypedCap d0 w sbS iS) = sbS := rfl + rw [eb] at hCn hK + obtain ⟨idx', hidx⟩ : ∃ i' : Nat, i' = unat (retypeBase + BitVec.ofNat 64 slots.length <<< fl4ObjBitsApi t objSz - + base) := ⟨_, rfl⟩ + rw [← hidx] at hCn hK + -- the source after the body, when it is no destination + have srcA : (setObjA σ srcSlot.1 (.cnode rS (Function.update csS srcSlot.2 + (some (.untypedCap dev base sbS idx'))))).kheap srcSlot.1 = some (.cnode rS (Function.update csS srcSlot.2 + (some (.untypedCap dev base sbS idx')))) := Function.update_self .. + have eqA : ∀ q, q ≠ srcSlot.1 → (setObjA σ srcSlot.1 (.cnode rS (Function.update csS srcSlot.2 + (some (.untypedCap dev base sbS idx'))))).kheap q = σ.kheap q := fun q h => Function.update_of_ne h .. + have domS : dom (Function.update csS srcSlot.2 (some (.untypedCap dev base sbS idx'))) = dom csS := by + ext j + by_cases j1 : j = srcSlot.2 + · subst j1 + show _ ≠ none ↔ _ ≠ none + rw [Function.update_self, hcapS] + simp + · show _ ≠ none ↔ _ ≠ none + rw [Function.update_of_ne j1] + have wfS : wellFormedCnodeN rS (Function.update csS srcSlot.2 (some (.untypedCap dev base sbS idx'))) := by + unfold wellFormedCnodeN at hSwf ⊢ + rw [domS] + exact hSwf + -- a CNode of the state after the source rewrite, off the new objects + have cnodeR : ∀ q sz cn, q ∉ fl4RetypeAddrs fl4ObjBitsApi retypeBase slots.length objSz t → + (setObjA σ srcSlot.1 (.cnode rS (Function.update csS srcSlot.2 (some (.untypedCap dev base sbS idx'))))).kheap + q = some (.cnode sz cn) → wellFormedCnodeN sz cn → ∀ idx, + specCapAtA r.2 (q, idx) = utWritesA t objSz dev + (slots.zip (fl4RetypeAddrs fl4ObjBitsApi retypeBase slots.length objSz t)) q cn idx := by + intro q sz cn off k wf idx + obtain ⟨e, dm⟩ := hCn q sz cn (Or.inr off) k + have wf' : wellFormedCnodeN sz (utWritesA t objSz dev + (slots.zip (fl4RetypeAddrs fl4ObjBitsApi retypeBase slots.length objSz t)) q cn) := by + unfold wellFormedCnodeN at wf ⊢ + rw [← wf] + ext j + exact not_congr (dm j) + unfold specCapAtA + simp only [e] + rw [ite_eq_left wf'] + intro sl h + rw [horig sl] at h + by_cases mem : sl ∈ slots + · refine Or.inr (Or.inl ⟨Or.inr mem, ?_⟩) + obtain ⟨⟨cn, k, wf⟩, off⟩ := hd sl mem + have hit : ∃ x ∈ slots.zip (fl4RetypeAddrs fl4ObjBitsApi retypeBase slots.length objSz t), x.1 = (sl.1, sl.2) := by + have : sl ∈ (slots.zip (fl4RetypeAddrs fl4ObjBitsApi retypeBase slots.length objSz t)).map Prod.fst := by + rw [List.map_fst_zip hlen] + exact mem + obtain ⟨x, hx, e⟩ := List.mem_map.1 this + exact ⟨x, hx, e⟩ + by_cases es : sl.1 = srcSlot.1 + · have k' : (setObjA σ srcSlot.1 (.cnode rS (Function.update csS srcSlot.2 + (some (.untypedCap dev base sbS idx'))))).kheap sl.1 = some (.cnode rS (Function.update csS srcSlot.2 + (some (.untypedCap dev base sbS idx')))) := by rw [es]; exact srcA + have kk := k + rw [es, hS] at kk + injection (Option.some.inj kk) with k1 k2 + have wf' : wellFormedCnodeN rS (Function.update csS srcSlot.2 (some (.untypedCap dev base sbS idx'))) := wfS + have e := cnodeR sl.1 rS _ off k' wf' sl.2 + obtain ⟨o, g⟩ := utWritesA_hitA (t := t) (bits := objSz) (dev := dev) _ + (Function.update csS srcSlot.2 (some (.untypedCap dev base sbS idx'))) hit + rw [show specCapAtA r.2 sl = specCapAtA r.2 (sl.1, sl.2) from rfl, e, g] + exact fun c => utDefaultCap_neNullA _ _ _ _ (Option.some.inj c) + · have k' := (eqA sl.1 es).trans k + have e := cnodeR sl.1 _ cn off k' wf sl.2 + obtain ⟨o, g⟩ := utWritesA_hitA (t := t) (bits := objSz) (dev := dev) _ cn hit + rw [show specCapAtA r.2 sl = specCapAtA r.2 (sl.1, sl.2) from rfl, e, g] + exact fun c => utDefaultCap_neNullA _ _ _ _ (Option.some.inj c) + rw [ite_eq_right mem] at h + have off := hold sl h + -- no destination writes this slot + have noW : ∀ q idx, (q, idx) = sl → ∀ x ∈ slots.zip (fl4RetypeAddrs fl4ObjBitsApi retypeBase slots.length objSz t), + x.1 ≠ (q, idx) := by + intro q idx e x hx e' + have m : x.1 ∈ slots := (List.of_mem_zip (show (x.1, x.2) ∈ _ from hx)).1 + rw [e', e] at m + exact mem m + by_cases es : sl = srcSlot + · refine Or.inr (Or.inl ⟨Or.inl es, ?_⟩) + have off' : srcSlot.1 ∉ fl4RetypeAddrs fl4ObjBitsApi retypeBase slots.length objSz t := by rw [← es]; exact off + have e := cnodeR srcSlot.1 rS _ off' srcA wfS srcSlot.2 + have ep : specCapAtA r.2 sl = specCapAtA r.2 (srcSlot.1, srcSlot.2) := by rw [es] + rw [ep, e, utWritesA_off _ _ (noW _ _ es.symm), Function.update_self] + exact fun c => Fl4SpecA64.Cap.noConfusion (Option.some.inj c) + refine Or.inl ⟨h, ?_⟩ + by_cases c : ∃ sz cn, (setObjA σ srcSlot.1 (.cnode rS (Function.update csS srcSlot.2 + (some (.untypedCap dev base sbS idx'))))).kheap sl.1 = some (.cnode sz cn) + swap + · have ne : sl.1 ≠ srcSlot.1 := fun e => c ⟨rS, _, by rw [e]; exact srcA⟩ + unfold specCapAtA + rw [hK _ (Or.inr off) (fun sz cn k => c ⟨sz, cn, k⟩), eqA _ ne] + obtain ⟨sz, cn, k⟩ := c + obtain ⟨e, dm⟩ := hCn sl.1 sz cn (Or.inr off) k + have wfi : wellFormedCnodeN sz (utWritesA t objSz dev + (slots.zip (fl4RetypeAddrs fl4ObjBitsApi retypeBase slots.length objSz t)) sl.1 cn) ↔ + wellFormedCnodeN sz cn := by + unfold wellFormedCnodeN + have : dom (utWritesA t objSz dev (slots.zip (fl4RetypeAddrs fl4ObjBitsApi retypeBase slots.length objSz t)) + sl.1 cn) = dom cn := by + ext j + exact not_congr (dm j) + rw [this] + have wv : utWritesA t objSz dev (slots.zip (fl4RetypeAddrs fl4ObjBitsApi retypeBase slots.length objSz t)) + sl.1 cn sl.2 = cn sl.2 := utWritesA_off _ _ (noW _ _ rfl) + have eR : specCapAtA r.2 sl = if wellFormedCnodeN sz cn then cn sl.2 else none := by + unfold specCapAtA + simp only [e, wfi, wv] + by_cases es1 : sl.1 = srcSlot.1 + · rw [es1, srcA] at k + injection (Option.some.inj k) with k1 k2 + have ei : sl.2 ≠ srcSlot.2 := fun c => es (Prod.ext es1 c) + rw [eR, ← k1, ← k2, ite_eq_left wfS, Function.update_of_ne ei] + unfold specCapAtA + rw [es1, hS] + simp only + rw [ite_eq_left hSwf] + · rw [eqA _ es1] at k + rw [eR] + unfold specCapAtA + rw [k] + +end + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s s' : S} {a : A} {σ : State Unit} {m : McState} {x p sb fi : U64} + {nt us aligned win : U64} {G' : MdbGhostA} {Tn Cn Vn : Set U64} + +/-- **The spec-side bundle of a RAM creation** (module docstring). -/ +theorem utRt_specAllA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (hx : x ∈ OS) (hc : capA st s x = .Untyped p sb fi false) + (W : UtWinZA nt aligned (zA nt.val us) win.val) + (h1 : p.val + fi.val ≤ aligned.val) + (h2 : aligned.val + win.val * 2 ^ (zA nt.val us).val ≤ p.val + 2 ^ sb.val) + (w : WritesIn st s s' (UtCrtWA st H OS s nt aligned (zA nt.val us) win.val)) + {t : ApiObjectType} (htt : objTypeOfA nt.val = some t) (hus : us.val < 64) + {srcSlot : CslotPtr} (hxc : cslotAddrA srcSlot = x) + (hsrcS : ∃ q, srcSlot.1 = specPtr q ∧ Z.th q = some (.cnode srcSlot.2.length)) + {slots : List CslotPtr} (hlen : slots.length = win.val) + (hdst : ∀ d ∈ slots, ∃ q, d.1 = specPtr q ∧ Z.th q = some (.cnode d.2.length)) + {r : Unit × State Unit} + (hr : r ∈ (utRtBodyA srcSlot (specPtr p) (specPtr aligned) t us.val slots false σ).1) : + UtAsmSpecInA st Z (utRtZA Z G' nt us aligned win.val Tn Cn Vn) OS s s' σ r.2 m (utRtMA m Tn) nt us aligned + win.val := by + have hn : nt.val < 15 := (objTypeOfA_some htt us.val).1 + have Cl : UtClrA st Z H s aligned.val (aligned.val + win.val * 2 ^ (zA nt.val us).val) := + utRt_winClrA J hx hc h1 h2 + have anchor : ∀ j, j < win.val → specPtr (bjA aligned (zA nt.val us) j) ≠ Fl4SpecA64.idleScPtr := by + intro j hj + have v := utRt_newValA W hj + exact utRet_anchorA (st := st) J.idleAnchor hx hc (by omega) (by omega) + have thFree : ∀ q, UtNewA false nt us aligned win.val q → Z.th q = none := utRt_ipcFreeA J hx hc W h1 h2 + have memA : ∀ q, q ∈ fl4RetypeAddrs fl4ObjBitsApi (specPtr aligned) slots.length us.val t ↔ + UtNewSpA aligned (zA nt.val us) win.val q := fun q => by + rw [hlen, utRet_addrs_memA htt hus] + rfl + have thOff : ∀ q k, Z.th q = some k → ¬ UtNewSpA aligned (zA nt.val us) win.val (specPtr q) := fun q k hq => + utRt_relOffA (m := m) J Cl W rfl rfl anchor (Or.inl ⟨q, by rw [hq]; simp, rfl⟩) + have specFree : ∀ q, UtNewSpA aligned (zA nt.val us) win.val q → σ.kheap q = none := by + rintro q ⟨j, hj, rfl⟩ + exact utRt_ipcSpecFreeA J hx hc W h1 h2 ⟨j, hj, rfl⟩ + obtain ⟨qS, eqS, hqS⟩ := hsrcS + obtain ⟨csS, hkS, hSwf, hsS⟩ := J.j.1.st.cnode qS _ hqS + rw [← eqS] at hkS + obtain ⟨capS, hcapS, hrelS⟩ := hsS srcSlot.2 rfl + have eS : ∃ bb ii : Nat, capS = .untypedCap false (specPtr p) bb ii := by + have ea : slotAddrA (specPtr qS) (Fl4SpecA64.ofBl srcSlot.2) = x := by rw [← hxc]; unfold cslotAddrA; rw [eqS] + rw [ea, capAtA_eq, hc] at hrelS + exact ⟨_, _, (show absCapA (.Untyped p sb fi false) = capS from hrelS.2).symm⟩ + obtain ⟨bS, iS, eS⟩ := eS + rw [eS] at hcapS + have dstCn : ∀ d ∈ slots, (∃ cn, σ.kheap d.1 = some (.cnode d.2.length cn) ∧ wellFormedCnodeN d.2.length cn) ∧ + d.1 ∉ fl4RetypeAddrs fl4ObjBitsApi (specPtr aligned) slots.length us.val t := by + intro d hd + obtain ⟨q, eq, hq⟩ := hdst d hd + obtain ⟨cs, hk, wf, -⟩ := J.j.1.st.cnode q _ hq + rw [eq, memA] + exact ⟨⟨cs, hk, wf⟩, thOff q _ hq⟩ + have hSa : srcSlot.1 ∉ fl4RetypeAddrs fl4ObjBitsApi (specPtr aligned) slots.length us.val t := by + rw [eqS, memA] + exact thOff qS _ hqS + have hd : ∀ d ∈ slots, (∃ sz cn, σ.kheap d.1 = some (.cnode sz cn)) ∧ + d.1 ∉ fl4RetypeAddrs fl4ObjBitsApi (specPtr aligned) slots.length us.val t := fun d h => by + obtain ⟨⟨cn, k, -⟩, g⟩ := dstCn d h + exact ⟨⟨_, cn, k⟩, g⟩ + have grow : ∀ q k, Z.th q = some k → utRtThA Z.th false nt us aligned win.val q = some k := fun q k h => + utRtTh_growA thFree h + -- an original slot of the old state is a slot of `th`, off the new addresses + have origOff : ∀ sl, σ.isOriginalCap sl = true → + AbsSlotA Z.th sl ∧ sl.1 ∉ fl4RetypeAddrs fl4ObjBitsApi (specPtr aligned) slots.length us.val t := by + intro sl h + have A := J.origLive sl h + obtain ⟨q, e, g⟩ := id A + refine ⟨A, ?_⟩ + rw [memA, e] + rcases g with g | ⟨g, -⟩ <;> exact thOff q _ g + have notFree : ∀ q kd, Z.th q = some kd → ∀ y : U64, UtFreeA st OS s (fun _ => True) y.val → + specPtr q ≠ specPtr y := by + intro q kd hq y hy e + have eq := ut_specPtr_injA e + subst eq + have none := J.specFree.heap q hy + cases kd with + | cnode rr => + obtain ⟨cs, hk, -⟩ := J.j.1.st.cnode q _ hq + rw [hk] at none + cases none + | tcb => + obtain ⟨y, hk, -⟩ := J.j.1.st.tcb q hq + rw [hk] at none + cases none + | other z => + obtain ⟨u, hu, q0, b0, f0, d0, hc0, -, l1, l2⟩ := hy + have d := J.objAlloc q z (Or.inr (Or.inr (Or.inl ⟨_, hq, rfl⟩))) u hu q0 b0 f0 d0 hc0 + have := J.thPos q z hq + rcases d with d | d | ⟨-, d⟩ <;> omega + have keepAbs : ∀ sl, AbsSlotA Z.th sl → AbsSlotA (utRtThA Z.th false nt us aligned win.val) sl := by + rintro sl ⟨q, e, g | ⟨g, i⟩⟩ + · exact ⟨q, e, Or.inl (grow q _ g)⟩ + · exact ⟨q, e, Or.inr ⟨grow q _ g, i⟩⟩ + have addrsLen : slots.length ≤ + (fl4RetypeAddrs fl4ObjBitsApi (specPtr aligned) slots.length us.val t).length := by + unfold fl4RetypeAddrs + rw [List.length_map, List.length_range] + have O := utRtBody_origA hr hkS hSwf hcapS hSa dstCn (fun sl h => (origOff sl h).2) addrsLen + refine utRt_specInA J hr hkS hcapS hSa hd (fun q h g => ?_) thFree (fun q h g y => ?_) (fun c hc' o ho => ?_) + (fun sl h => ?_) (fun q rr cs h wf => ?_) + · rw [memA] at h + obtain ⟨j, hj, e⟩ := h + have eq := ut_specPtr_injA e + subst eq + have v := utRt_newValA W hj + refine ⟨v.1, v.2, fun z => ?_⟩ + rw [z] at htt + simp [objTypeOfA, eq_comm] at htt + exact g htt + · obtain ⟨y, hy⟩ := y + have et : t = .tcbObject := by + cases t with + | untyped => exact absurd rfl g + | tcbObject => rfl + | capTableObject => exact Fl4SpecA64.KernelObject.noConfusion hy + | endpointObject => exact Fl4SpecA64.KernelObject.noConfusion hy + | notificationObject => exact Fl4SpecA64.KernelObject.noConfusion hy + | schedContextObject => exact Fl4SpecA64.KernelObject.noConfusion hy + | replyObject => exact Fl4SpecA64.KernelObject.noConfusion hy + | archObject o => exact Fl4SpecA64.KernelObject.noConfusion hy + have n1 : nt.val = 1 := by + rw [et] at htt + revert htt + unfold objTypeOfA archDataToObjType + intro htt + interval_cases hv : nt.val <;> simp at htt <;> rfl + rw [memA] at h + obtain ⟨j, hj, e⟩ := h + rw [e, fl4Ptr_specPtr, utRtTh_newA ⟨rfl, by omega, by omega, j, hj, rfl⟩] + unfold utKindA + rw [ite_eq_left n1] + · have eb : L.nodeAt s' c o = L.nodeAt s c o := by + unfold LawfulStore.nodeAt + rw [w.1] + rw [eb] + have o1 : o.val < 4288 ∧ o.val % 8 = 0 ∧ o.val ≠ 88 := by + rcases ho with rfl | rfl <;> simp [layout.node.IDLE_THREAD, layout.node.IDLE_SC] + have nc := J.sq.nodes c hc' + have al : Aligned (L.nodeAt s c o) := by + show _ % 8 = 0 + rw [nodeAt_val nc o1.1] + have := nc.align + omega + have Of := utRt_nodeOffA J hc' o1.1 rfl al (utRt_nodeNeNwA J hc' o1.1 o1.2.2) + refine w.mem_eq al ?_ + rintro (h | ⟨-, g1, g2⟩ | h | ⟨u, hu, h⟩) + · exact Of.1 h + · rw [nodeAt_val nc o1.1] at g1 g2 + rcases Cl.nodes c hc' with g | g <;> omega + · exact Of.2.2 h + · exact Of.2.1 u hu h + · rcases O sl h with ⟨g, e⟩ | ⟨g, ne⟩ | ⟨g, e, ne⟩ + · exact Or.inl ⟨g, e⟩ + · have A : AbsSlotA Z.th sl := by + rcases g with rfl | g + · exact ⟨qS, eqS, Or.inl hqS⟩ + · obtain ⟨q, eq, hq⟩ := hdst sl g + exact ⟨q, eq, Or.inl hq⟩ + refine Or.inr ⟨fun y hy => ?_, keepAbs sl A, ne⟩ + obtain ⟨q, eq, hq | ⟨hq, -⟩⟩ := A <;> (rw [eq]; exact notFree q _ hq y hy) + · obtain ⟨A, -⟩ := origOff sl g + refine Or.inr ⟨fun y hy => ?_, keepAbs sl A, ne⟩ + obtain ⟨q, eq, hq | ⟨hq, -⟩⟩ := A <;> (rw [eq]; exact notFree q _ hq y hy) + · by_cases g : t ≠ .untyped ∧ q ∈ fl4RetypeAddrs fl4ObjBitsApi (specPtr aligned) slots.length us.val t + · have hNew := (utRtBody_invA hr hkS hcapS hSa hd).2.2.1 + rw [hNew g.1 q g.2] at h + obtain ⟨e1, e2⟩ := utDefaultObject_cnodeA g.1 (Option.some.inj h) + have et : t = .capTableObject := by + have h' := Option.some.inj h + cases t with + | untyped => exact absurd rfl g.1 + | capTableObject => rfl + | tcbObject => exact Fl4SpecA64.KernelObject.noConfusion h' + | endpointObject => exact Fl4SpecA64.KernelObject.noConfusion h' + | notificationObject => exact Fl4SpecA64.KernelObject.noConfusion h' + | schedContextObject => exact Fl4SpecA64.KernelObject.noConfusion h' + | replyObject => exact Fl4SpecA64.KernelObject.noConfusion h' + | archObject o => exact Fl4SpecA64.KernelObject.noConfusion h' + have n4 : nt.val = 4 := by + rw [et] at htt + revert htt + unfold objTypeOfA archDataToObjType + intro htt + interval_cases hv : nt.val <;> simp at htt <;> rfl + have m' := g.2 + rw [memA] at m' + obtain ⟨j, hj, e⟩ := m' + refine Or.inr ?_ + rw [e, fl4Ptr_specPtr, utRtTh_newA ⟨rfl, by omega, by omega, j, hj, rfl⟩, e1] + unfold utKindA + rw [ite_eq_right (by omega), ite_eq_left n4] + refine Or.inl ?_ + have og : t = .untyped ∨ q ∉ fl4RetypeAddrs fl4ObjBitsApi (specPtr aligned) slots.length us.val t := by + by_cases e : t = .untyped + · exact Or.inl e + · exact Or.inr fun mem => g ⟨e, mem⟩ + obtain ⟨-, hCn, -, hK, -, -, -⟩ := utRtBody_invA hr hkS hcapS hSa hd + by_cases c : ∃ sz cn, (setObjA σ srcSlot.1 (.cnode srcSlot.2.length (Function.update csS srcSlot.2 (some + (.untypedCap false (specPtr p) (bitsOf (.untypedCap false (specPtr p) bS iS)) (unat (specPtr aligned + + BitVec.ofNat 64 slots.length <<< fl4ObjBitsApi t us.val - specPtr p))))))).kheap q = some (.cnode sz cn) + swap + · rw [hK q og (fun sz cn k => c ⟨sz, cn, k⟩)] at h + exact absurd ⟨rr, cs, h⟩ c + obtain ⟨sz, cn, k⟩ := c + obtain ⟨e, dm⟩ := hCn q sz cn og k + rw [e] at h + injection (Option.some.inj h) with k1 k2 + have wfc : wellFormedCnodeN rr cn := by + unfold wellFormedCnodeN at wf ⊢ + rw [← wf, ← k2] + ext j + exact (not_congr (dm j)).symm + by_cases es : q = srcSlot.1 + · have k' := k + rw [es] at k' + unfold setObjA at k' + simp only [Function.update_self, Option.some.injEq] at k' + injection k' with a1 a2 + refine ⟨csS, ?_, ?_⟩ + · rw [es, hkS, a1, k1] + · rw [← k1, ← a1] + exact hSwf + · refine ⟨cn, ?_, wfc⟩ + have k' : σ.kheap q = some (.cnode sz cn) := (Function.update_of_ne es _ _).symm.trans k + rw [k', k1] + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtApSpecA.lean b/proofs/Fl4ProofsA64/P3/UtApSpecA.lean new file mode 100644 index 000000000..2302cb9ae --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtApSpecA.lean @@ -0,0 +1,135 @@ +/- +P3 (aarch64, A64-P3 Untyped, full-record post, discharge): **the spec-side bundle `UtAsmSpecInA`** at an outcome of +the spec's retype body, from the record before it (`utRtBody_invA`, `utRtBody_globA`, `utRtBody_umA`). Discharged +here: `th`, `cs`, `idleThreadOf`, `kheapNone`, `um`, `cdt`, `tcb`, `asid`. Left as hypotheses: the idle node words, +the new original caps and the CNodes. + +No named premise. New theorems only. +-/ +import Fl4ProofsA64.P3.UtAsmSpecA +import Fl4ProofsA64.P3.UtRtCapsA +import Fl4ProofsA64.P3.UtRtGlobA +import Fl4ProofsA64.P3.UtRtUmA +import Fl4ProofsA64.P3.UtRtSchedA +import Fl4ProofsA64.P3.UtRtPostA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Sched Fl4SpecA64 Fl4Spec Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s s' : S} {a : A} {σ : State Unit} {m : McState} + {nt us aligned : U64} {win : Nat} {G' : MdbGhostA} {Tn Cn Vn : Set U64} + {srcSlot : CslotPtr} {base retypeBase : Word} {t : ApiObjectType} {objSz : Nat} {slots : List CslotPtr} + {dev : Bool} {r : Unit × State Unit} + +theorem utRt_specInA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (hr : r ∈ (utRtBodyA srcSlot base retypeBase t objSz slots dev σ).1) + {rS : Nat} {csS : CnodeContents} (hS : σ.kheap srcSlot.1 = some (.cnode rS csS)) {capS : Cap} + (hcapS : csS srcSlot.2 = some capS) + (hSa : srcSlot.1 ∉ fl4RetypeAddrs fl4ObjBitsApi retypeBase slots.length objSz t) + (hd : ∀ d ∈ slots, (∃ sz cn, σ.kheap d.1 = some (.cnode sz cn)) ∧ + d.1 ∉ fl4RetypeAddrs fl4ObjBitsApi retypeBase slots.length objSz t) + -- the new addresses lie in the window and are off the old typed heap + (hwin : ∀ q : U64, specPtr q ∈ fl4RetypeAddrs fl4ObjBitsApi retypeBase slots.length objSz t → t ≠ .untyped → + aligned.val ≤ q.val ∧ q.val < aligned.val + win * 2 ^ (zA nt.val us).val ∧ nt.val ≠ 0) + (hfree : ∀ q, UtNewA false nt us aligned win q → Z.th q = none) + (hnewTcb : ∀ q ∈ fl4RetypeAddrs fl4ObjBitsApi retypeBase slots.length objSz t, t ≠ .untyped → + (∃ y, defaultObject t dev objSz σ.curDomain = .tcb y) → + utRtThA Z.th false nt us aligned win (fl4Ptr q) = some .tcb) + -- not discharged here + (idleW : ∀ c ∈ Z.Cs, ∀ o : U64, (o = layout.node.IDLE_THREAD ∨ o = layout.node.IDLE_SC) → + L.mem s' (L.nodeAt s' c o) = L.mem s (L.nodeAt s c o)) + (orig : ∀ sl, r.2.isOriginalCap sl = true → + (σ.isOriginalCap sl = true ∧ specCapAtA r.2 sl = specCapAtA σ sl) ∨ + ((∀ q : U64, UtFreeA st OS s (fun _ => True) q.val → sl.1 ≠ specPtr q) ∧ + AbsSlotA (utRtThA Z.th false nt us aligned win) sl ∧ specCapAtA r.2 sl ≠ some .nullCap)) + (cnode : ∀ q rr cs, r.2.kheap q = some (.cnode rr cs) → wellFormedCnodeN rr cs → + (∃ cs0, σ.kheap q = some (.cnode rr cs0) ∧ wellFormedCnodeN rr cs0) ∨ + utRtThA Z.th false nt us aligned win (fl4Ptr q) = some (.cnode rr)) : + UtAsmSpecInA st Z (utRtZA Z G' nt us aligned win Tn Cn Vn) OS s s' σ r.2 m (utRtMA m Tn) nt us aligned win := by + obtain ⟨-, hcn, hnew, hoth, hcdt, -, -⟩ := utRtBody_invA hr hS hcapS hSa hd + have G := utRtBody_globA hr + have um := utRtBody_umA hr + -- a free address holds no object, so no CNode + have notFree : ∀ (o : ObjRef) (sz : Nat) (cn : CnodeContents), σ.kheap o = some (.cnode sz cn) → + ∀ q : U64, UtFreeA st OS s (fun _ => True) q.val → o ≠ specPtr q := by + intro o sz cn h q hq e + rw [e, J.specFree.heap q hq] at h + cases h + -- off the new objects an outcome's non-CNode object, and its absence, are the old ones + have srcNe : ∀ o, o ≠ srcSlot.1 → ∀ u : Cap, + (setObjA σ srcSlot.1 (.cnode rS (Function.update csS srcSlot.2 (some u)))).kheap o = σ.kheap o := by + intro o h u + exact Function.update_of_ne h _ _ + refine + { th := fun q k h => ?_ + cs := rfl + idleThreadOf := rfl + idleW := idleW + kheapNone := fun q h hw => ?_ + um := fun w h => by rw [um]; exact h + cdt := fun c d h => ?_ + orig := orig + cnode := cnode + tcb := fun q y h => ?_ + asid := G.asid + vcur := G.vcur } + · show utRtThA Z.th false nt us aligned win q = some k + rw [utRtTh_oldA (fun c => by rw [hfree q c] at h; cases h)] + exact h + · have off : t = .untyped ∨ specPtr q ∉ fl4RetypeAddrs fl4ObjBitsApi retypeBase slots.length objSz t := by + by_cases g : t = .untyped + · exact Or.inl g + refine Or.inr (fun mem => ?_) + obtain ⟨a1, a2, a3⟩ := hwin q mem g + exact a3 (hw a1 a2) + have ne : specPtr q ≠ srcSlot.1 := fun e => by + rw [e, hS] at h + cases h + rw [hoth _ off (fun sz cn k => by rw [srcNe _ ne, h] at k; cases k), srcNe _ ne] + exact h + · rw [hcdt c] at h + split at h + · rename_i mem + obtain rfl := Option.some.inj h + obtain ⟨⟨sz, cn, k⟩, -⟩ := hd c mem + exact Or.inr (fun q hq => ⟨notFree _ _ _ k q hq, notFree _ _ _ hS q hq⟩) + · exact Or.inl h + · by_cases g : t ≠ .untyped ∧ q ∈ fl4RetypeAddrs fl4ObjBitsApi retypeBase slots.length objSz t + · rw [hnew g.1 q g.2] at h + exact Or.inr (hnewTcb q g.2 g.1 ⟨y, Option.some.inj h⟩) + have off : t = .untyped ∨ q ∉ fl4RetypeAddrs fl4ObjBitsApi retypeBase slots.length objSz t := by + by_cases e : t = .untyped + · exact Or.inl e + · exact Or.inr fun mem => g ⟨e, mem⟩ + refine Or.inl ?_ + by_cases c : ∃ sz cn, (setObjA σ srcSlot.1 (.cnode rS (Function.update csS srcSlot.2 + (some (.untypedCap dev base (bitsOf capS) + (unat (retypeBase + BitVec.ofNat 64 slots.length <<< fl4ObjBitsApi t objSz - base))))))).kheap q = + some (.cnode sz cn) + · obtain ⟨sz, cn, k⟩ := c + rw [(hcn q sz cn off k).1] at h + cases h + have e := hoth q off (fun sz cn k => c ⟨sz, cn, k⟩) + rw [e] at h + have ne : q ≠ srcSlot.1 := fun eq => c ⟨rS, _, by + rw [eq] + exact Function.update_self _ _ _⟩ + rw [srcNe _ ne] at h + exact ⟨y, h⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtApSpecObjA.lean b/proofs/Fl4ProofsA64/P3/UtApSpecObjA.lean new file mode 100644 index 000000000..4766b879b --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtApSpecObjA.lean @@ -0,0 +1,263 @@ +/- +P3 (aarch64, A64-P3 Untyped, full-record post, discharge): **the spec side of the IPC bundle** at an outcome of the +spec's retype body. + +* `utRtBody_newObjA`: the outcome is a `UtNewObjA`: the new addresses were free and hold the default object, every + other object that is no CNode is kept. +* `utRtBody_irqA`: the outcome keeps `SpecIrqA`. The new addresses are no IRQ-node address (`haddr`: for the IRQs of + FL4 the node objects are occupied; for the others it is the record's `IrqRelA.nodeHi`) and no destination CNode is + an IRQ node (`hdst`: destination CNodes have a nonzero radix). + +No named premise. New theorems only. +-/ +import Fl4ProofsA64.P3.UtRtCapsA +import Fl4ProofsA64.P3.UtRtGlobA +import Fl4ProofsA64.P3.UtRtUmA +import Fl4ProofsA64.P3.UtFullIpcA +import Fl4ProofsA64.P3.UtChunkA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4SpecA64 Fl4Spec Classical + +noncomputable section + +section +variable {σ : State Unit} {r : Unit × State Unit} {srcSlot : CslotPtr} {base retypeBase : Word} + {t : ApiObjectType} {objSz : Nat} {slots : List CslotPtr} {dev : Bool} + +/-- A slot of a fold of default-cap writes holds the old cap or a default cap of a pair of the list. -/ +theorem utWritesA_casesA {t : ApiObjectType} {bits : Nat} {dev : Bool} {q : ObjRef} {idx : CnodeIndex} + (l : List (CslotPtr × ObjRef)) (cn : CnodeContents) : + utWritesA t bits dev l q cn idx = cn idx ∨ + ∃ x ∈ l, x.1 = (q, idx) ∧ utWritesA t bits dev l q cn idx = some (defaultCap t x.2 bits dev) := by + induction l generalizing cn with + | nil => exact Or.inl rfl + | cons a l ih => + have step : utWritesA t bits dev (a :: l) q cn = utWritesA t bits dev l q + (if a.1.1 = q then Function.update cn a.1.2 (some (defaultCap t a.2 bits dev)) else cn) := by + unfold utWritesA + rw [List.foldl_cons] + rw [step] + rcases ih (if a.1.1 = q then Function.update cn a.1.2 (some (defaultCap t a.2 bits dev)) else cn) with h | h + · by_cases ha : a.1.1 = q + · rw [ite_eq_left ha] at h ⊢ + by_cases hi : idx = a.1.2 + · refine Or.inr ⟨a, List.mem_cons_self .., Prod.ext ha hi.symm, ?_⟩ + rw [h, hi, Function.update_self] + · refine Or.inl ?_ + rw [h, Function.update_of_ne hi] + · rw [ite_eq_right ha] at h ⊢ + exact Or.inl h + · obtain ⟨x, hx, e1, e2⟩ := h + exact Or.inr ⟨x, List.mem_cons_of_mem _ hx, e1, e2⟩ + +/-- Only a capability-table default cap is a CNode cap. -/ +theorem utDefaultCap_cnodeA {t : ApiObjectType} {o p : ObjRef} {bits n : Nat} {dev : Bool} {g : List Bool} + (h : defaultCap t o bits dev = .cnodeCap p n g) : t = .capTableObject ∧ o = p := by + cases t with + | capTableObject => + injection h with h1 + exact ⟨rfl, h1⟩ + | _ => exact Fl4SpecA64.Cap.noConfusion h + +/-- A default object holds no CNode cap. -/ +theorem utDefaultObject_capA {t : ApiObjectType} (ht : t ≠ .untyped) {dev : Bool} {n : Nat} {d : Domain} + {b : CnodeIndex} {a : Fl4SpecA64.Cap} (h : capOf (defaultObject t dev n d) b = some a) : a = .nullCap := by + cases t with + | untyped => exact absurd rfl ht + | capTableObject => + change emptyCnode n b = some a at h + unfold emptyCnode at h + split_ifs at h + exact (Option.some.inj h).symm + | tcbObject => + change tcbCnodeMap (defaultTcb d) b = some a at h + unfold tcbCnodeMap at h + simp only [Function.update_apply] at h + split_ifs at h <;> (cases h <;> rfl) + | _ => cases h + +/-- A default object that is a CNode is the empty CNode of a capability table. -/ +theorem utDefaultObject_cnodeA {t : ApiObjectType} (ht : t ≠ .untyped) {dev : Bool} {n sz : Nat} {d : Domain} + {cs : CnodeContents} (h : defaultObject t dev n d = .cnode sz cs) : sz = n ∧ cs = emptyCnode n := by + cases t with + | untyped => exact absurd rfl ht + | capTableObject => + change KernelObject.cnode n (emptyCnode n) = .cnode sz cs at h + injection h with h1 h2 + exact ⟨h1.symm, h2.symm⟩ + | tcbObject => exact Fl4SpecA64.KernelObject.noConfusion h + | endpointObject => exact Fl4SpecA64.KernelObject.noConfusion h + | notificationObject => exact Fl4SpecA64.KernelObject.noConfusion h + | schedContextObject => exact Fl4SpecA64.KernelObject.noConfusion h + | replyObject => exact Fl4SpecA64.KernelObject.noConfusion h + | archObject aobj => exact Fl4SpecA64.KernelObject.noConfusion h + +variable (hr : r ∈ (utRtBodyA srcSlot base retypeBase t objSz slots dev σ).1) + {rS : Nat} {csS : CnodeContents} (hS : σ.kheap srcSlot.1 = some (.cnode rS csS)) +include hr hS + +/-- **The outcome of the body is a `UtNewObjA`** (module docstring). -/ +theorem utRtBody_newObjA {capS : Fl4SpecA64.Cap} (hcapS : csS srcSlot.2 = some capS) + (hSa : srcSlot.1 ∉ fl4RetypeAddrs fl4ObjBitsApi retypeBase slots.length objSz t) + (hd : ∀ d ∈ slots, (∃ sz cn, σ.kheap d.1 = some (.cnode sz cn)) ∧ + d.1 ∉ fl4RetypeAddrs fl4ObjBitsApi retypeBase slots.length objSz t) + (hfree : ∀ q ∈ fl4RetypeAddrs fl4ObjBitsApi retypeBase slots.length objSz t, t ≠ .untyped → σ.kheap q = none) : + UtNewObjA σ r.2 (fun q => t ≠ .untyped ∧ q ∈ fl4RetypeAddrs fl4ObjBitsApi retypeBase slots.length objSz t) + (defaultObject t dev objSz σ.curDomain) := by + obtain ⟨-, hCn, hNew, hK, -, -, -⟩ := utRtBody_invA hr hS hcapS hSa hd + -- an old object that is no CNode is at no destination or source, so the body keeps it + have srcNe : ∀ q ko, (∀ sz cn, ko ≠ .cnode sz cn) → σ.kheap q = some ko → q ≠ srcSlot.1 := by + intro q ko ne h e + rw [e, hS] at h + exact ne _ _ (Option.some.inj h).symm + have eqA : ∀ q, q ≠ srcSlot.1 → ∀ u : Fl4SpecA64.Cap, + (setObjA σ srcSlot.1 (.cnode rS (Function.update csS srcSlot.2 (some u)))).kheap q = σ.kheap q := + fun q h u => Function.update_of_ne h .. + refine + { fresh := fun p g => hfree p g.2 g.1 + new := fun p g => hNew g.1 p g.2 + keep := fun p ko h ne => ?_ + back := fun p ko h ne => ?_ + glob := utRtBody_globA hr + um := utRtBody_umA hr } + · have off : t = .untyped ∨ p ∉ fl4RetypeAddrs fl4ObjBitsApi retypeBase slots.length objSz t := by + by_cases g : t = .untyped + · exact Or.inl g + exact Or.inr fun m => by rw [hfree p m g] at h; cases h + have e := srcNe p ko ne h + rw [hK p off (fun sz cn k => by + rw [eqA p e, h] at k + exact ne _ _ (Option.some.inj k)), eqA p e] + exact h + · by_cases g : t = .untyped ∨ p ∉ fl4RetypeAddrs fl4ObjBitsApi retypeBase slots.length objSz t + · refine Or.inl ?_ + by_cases c : ∃ sz cn, (setObjA σ srcSlot.1 (.cnode rS (Function.update csS srcSlot.2 (some + (.untypedCap dev base (bitsOf capS) (unat (retypeBase + + BitVec.ofNat 64 slots.length <<< fl4ObjBitsApi t objSz - base))))))).kheap p = some (.cnode sz cn) + · obtain ⟨sz, cn, k⟩ := c + rw [(hCn p sz cn g k).1] at h + exact absurd (Option.some.inj h).symm (ne _ _) + · rw [hK p g (fun sz cn k => c ⟨sz, cn, k⟩)] at h + by_cases e : p = srcSlot.1 + · exact absurd ⟨rS, _, by rw [e]; exact Function.update_self ..⟩ c + · rwa [eqA p e] at h + · have gt : t ≠ .untyped := fun e => g (Or.inl e) + have gp : p ∈ fl4RetypeAddrs fl4ObjBitsApi retypeBase slots.length objSz t := by + by_contra k + exact g (Or.inr k) + refine Or.inr ⟨⟨gt, gp⟩, ?_⟩ + rw [hNew gt p gp] at h + exact (Option.some.inj h).symm + +/-- **The outcome of the body keeps `SpecIrqA`** (module docstring). -/ +theorem utRtBody_irqA (hI : SpecIrqA σ) {d0 : Bool} {sbS i0 : Nat} + (hcapS : csS srcSlot.2 = some (.untypedCap d0 base sbS i0)) (hSwf : wellFormedCnodeN rS csS) + (hSa : srcSlot.1 ∉ fl4RetypeAddrs fl4ObjBitsApi retypeBase slots.length objSz t) + (hd : ∀ d ∈ slots, (∃ sz cn, σ.kheap d.1 = some (.cnode sz cn)) ∧ + d.1 ∉ fl4RetypeAddrs fl4ObjBitsApi retypeBase slots.length objSz t) + (hdb : d0 = dev) + (haddr : t ≠ .untyped → ∀ i : Irq, σ.interruptIrqNode i ∉ fl4RetypeAddrs fl4ObjBitsApi retypeBase + slots.length objSz t) + (hdst : ∀ irq : Irq, irq.toNat < fl4IrqCount → ∀ d ∈ slots, d.1 ≠ σ.interruptIrqNode irq) : + SpecIrqA r.2 := by + obtain ⟨-, hCn, hNew, hK, -, -, -⟩ := utRtBody_invA hr hS hcapS hSa hd + have G := utRtBody_globA hr + subst hdb + have eb : bitsOf (.untypedCap d0 base sbS i0) = sbS := rfl + rw [eb] at hCn hK + obtain ⟨idx', hidx⟩ : ∃ i' : Nat, i' = unat (retypeBase + BitVec.ofNat 64 slots.length <<< fl4ObjBitsApi t objSz - + base) := ⟨_, rfl⟩ + rw [← hidx] at hCn hK + have Ha : SpecIrqA (setObjA σ srcSlot.1 (.cnode rS (Function.update csS srcSlot.2 + (some (.untypedCap d0 base sbS idx'))))) := ut_specIrq_setA (d' := d0) (i' := idx') hI hS hSwf hcapS + have nd : (setObjA σ srcSlot.1 (.cnode rS (Function.update csS srcSlot.2 + (some (.untypedCap d0 base sbS idx'))))).interruptIrqNode = σ.interruptIrqNode := rfl + have off : ∀ i : Irq, t = .untyped ∨ σ.interruptIrqNode i ∉ fl4RetypeAddrs fl4ObjBitsApi retypeBase + slots.length objSz t := fun i => by + by_cases g : t = .untyped + · exact Or.inl g + exact Or.inr (haddr g i) + -- the IRQ nodes of FL4's IRQs are as before the objects are created + have keepLow : ∀ i : Irq, i.toNat < fl4IrqCount → + r.2.kheap (σ.interruptIrqNode i) = (setObjA σ srcSlot.1 (.cnode rS (Function.update csS srcSlot.2 + (some (.untypedCap d0 base sbS idx'))))).kheap (σ.interruptIrqNode i) := by + intro i hi + obtain ⟨cs, k, -⟩ := Ha.node.1.2 i hi + rw [nd] at k + rw [(hCn _ 0 cs (off i) k).1, k] + congr 2 + funext b + refine utWritesA_off _ cs fun x hx e => ?_ + have m : x.1 ∈ slots := (List.of_mem_zip (show (x.1, x.2) ∈ _ from hx)).1 + exact hdst i hi x.1 m (congrArg Prod.fst e) + have nr : r.2.interruptIrqNode = σ.interruptIrqNode := G.irqN + refine ⟨⟨⟨?_, fun i hi => ?_⟩, fun i hi n h => ?_⟩, fun i hi o b a ho hb => ?_, fun p o b a ho hb => ?_⟩ + · rw [nr] + exact Ha.node.1.1 + · obtain ⟨cs, k, w⟩ := Ha.node.1.2 i hi + rw [nd] at k + refine ⟨cs, ?_, w⟩ + rw [nr, keepLow i hi] + exact k + · obtain ⟨cs, k, w⟩ := h + rw [nr] at k + refine Ha.node.2 i hi n ?_ + by_cases c : ∃ sz cn, (setObjA σ srcSlot.1 (.cnode rS (Function.update csS srcSlot.2 + (some (.untypedCap d0 base sbS idx'))))).kheap (σ.interruptIrqNode i) = some (.cnode sz cn) + · obtain ⟨sz, cn, g⟩ := c + obtain ⟨e, dm⟩ := hCn _ sz cn (off i) g + rw [e] at k + injection (Option.some.inj k) with e1 e2 + subst e1 + refine ⟨cn, by rw [nd]; exact g, ?_⟩ + unfold wellFormedCnodeN at w ⊢ + rw [← w, ← e2] + ext j + show cn j ≠ none ↔ utWritesA t objSz d0 _ _ cn j ≠ none + exact (not_congr (dm j)).symm + · rw [hK _ (off i) (fun sz cn g => c ⟨sz, cn, g⟩)] at k + exact absurd ⟨n, cs, k⟩ c + · rw [nr, keepLow i hi] at ho + exact Ha.slot i hi o b a (by rw [nd]; exact ho) hb + · rw [nr] + by_cases g : t ≠ .untyped ∧ p ∈ fl4RetypeAddrs fl4ObjBitsApi retypeBase slots.length objSz t + · rw [hNew g.1 p g.2] at ho + rw [← Option.some.inj ho] at hb + rw [utDefaultObject_capA g.1 hb] + rintro ⟨_, _, _, _, e⟩ + cases e + have og : t = .untyped ∨ p ∉ fl4RetypeAddrs fl4ObjBitsApi retypeBase slots.length objSz t := by + by_cases e : t = .untyped + · exact Or.inl e + exact Or.inr fun m => g ⟨e, m⟩ + by_cases c : ∃ sz cn, (setObjA σ srcSlot.1 (.cnode rS (Function.update csS srcSlot.2 + (some (.untypedCap d0 base sbS idx'))))).kheap p = some (.cnode sz cn) + · obtain ⟨sz, cn, k⟩ := c + rw [(hCn p sz cn og k).1] at ho + rw [← Option.some.inj ho] at hb + change utWritesA t objSz d0 _ p cn b = some a at hb + rcases utWritesA_casesA (t := t) (bits := objSz) (dev := d0) (q := p) (idx := b) + (slots.zip (fl4RetypeAddrs fl4ObjBitsApi retypeBase slots.length objSz t)) cn with h | ⟨x, hx, -, h⟩ + · rw [h] at hb + have := Ha.ref p _ b a k hb + rwa [nd] at this + · rw [h] at hb + rintro ⟨irq, hirq, n, gd, e⟩ + rw [← Option.some.inj hb] at e + obtain ⟨et, eo⟩ := utDefaultCap_cnodeA e + have m : x.2 ∈ fl4RetypeAddrs fl4ObjBitsApi retypeBase slots.length objSz t := + (List.of_mem_zip (show (x.1, x.2) ∈ _ from hx)).2 + rw [eo] at m + exact haddr (by rw [et]; simp) irq m + · rw [hK p og (fun sz cn k => c ⟨sz, cn, k⟩)] at ho + have := Ha.ref p o b a ho hb + rwa [nd] at this + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtApSys2A.lean b/proofs/Fl4ProofsA64/P3/UtApSys2A.lean new file mode 100644 index 000000000..2e73df56a --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtApSys2A.lean @@ -0,0 +1,255 @@ +/- +P3 (aarch64, A64-P3 Untyped, full-record post, discharge): **the entry-invariant bundle `UtAsmSysInA` of a RAM +creation without a reset**, with only A64 P1's creation top left as input (`arm`, `ko`, `j7at`: the post of +`utRt_j7TopA`). The bundle stays at the decode's state (`ArmQIVAt` is P1's statement there); what `utRt_sysInA` left +open comes from the record at the creation start (after the caller's restart write) and the scheduler bundle there. + +* `utArgs_noChA`: a decode without a reset has a source with children, so every member is off-region (`UtOffRgA`). +* `utRt_tcbsNewA`, `utRt_scsNewA`: the grown heap's TCBs and SCs are the old ones and the new objects of type 1 / 5. +* `utRt_vsCrtKeptA`: a creation keeps every reachable VSpace word (they lie in `omega` objects, off the window). +* `utRt_vsRestartA`: the same across the caller's restart write, then both steps: `VsZ` and the pool set. +* `utRt_coreKeptA`: the old queue core at the creation end (`SchedSRA` at the start, the kept words). +* **`utRt_sysAllA`**: the bundle. + +No named premise. New theorems only. +-/ +import Fl4ProofsA64.P3.UtApSysA +import Fl4ProofsA64.P3.UtApHeap2A +import Fl4ProofsA64.P3.UtFullJ7A +import Fl4ProofsA64.P3.DelVsKeepA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +set_option maxRecDepth 100000 + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + +/-- A decode's arguments without a reset: `ensure_no_children` did not succeed. -/ +theorem utArgs_noChA {s0 : S} {extra : Aeneas.Std.Array obj.Slot 3#usize} {slot p sb fi : U64} {dev : Bool} + {aligned nt us dest off win : U64} + (h : UtArgs st s0 extra slot p sb fi dev false aligned nt us dest off win) : + ¬ ∃ v, cdt.ensure_no_children st s0 slot = ok (.Ok v) := fun ch => by + obtain ⟨-, -, -, -, -, -, -, -, -, -, iff, -⟩ := h + exact absurd (iff.mpr ch) (by simp) + +/-- Without a successful `ensure_no_children`, everything is off the source's region. -/ +theorem utOffRg_noChA {s0 : S} {slot p sb : U64} (h : ¬ ∃ v, cdt.ensure_no_children st s0 slot = ok (.Ok v)) + (x : U64) : UtOffRgA st s0 slot p sb x := fun ch => absurd ch h + +/-- The kind a creation gives a TCB. -/ +theorem utNewKind_tcbA {n us : Nat} (h : newKind n us = some .tcb) : n = 1 := by + unfold newKind at h + split_ifs at h <;> simp_all + +/-- The kind a creation gives an SC. -/ +theorem utNewKind_scA {n us z : Nat} (h : newKind n us = some (.sc z)) : n = 5 ∧ z = us := by + unfold newKind at h + split_ifs at h <;> simp_all + +/-- **The grown heap's TCBs.** -/ +theorem utRt_tcbsNewA {H H' : Heap} {T : Set U64} {nt us aligned : U64} {win : Nat} (eT : H.tcbs = T) + (hgrow : ∀ q k, H q = some k → H' q = some k) + (hex : ∀ q k, H' q = some k → H q = some k ∨ + ∃ j < win, q = bjA aligned (zA nt.val us) j ∧ newKind nt.val us.val = some k) + (hnew : ∀ j < win, ∀ k, newKind nt.val us.val = some k → H' (bjA aligned (zA nt.val us) j) = some k) : + H'.tcbs = T ∪ UtOfTyA 1 nt us aligned win := by + ext t + constructor + · intro ht + change H' t = some .tcb at ht + rcases hex t _ ht with o | ⟨j, hj, e, hk⟩ + · exact Or.inl (eT ▸ o) + · exact Or.inr ⟨utNewKind_tcbA hk, j, hj, e⟩ + · rintro (ht | ⟨e1, j, hj, rfl⟩) + · rw [← eT] at ht + exact hgrow t _ ht + · exact hnew j hj _ (by unfold newKind; simp [e1]) + +/-- **The grown heap's SCs.** -/ +theorem utRt_scsNewA {H H' : Heap} {C : Set U64} {nt us aligned : U64} {win : Nat} (eC : H.scs = C) + (hgrow : ∀ q k, H q = some k → H' q = some k) + (hex : ∀ q k, H' q = some k → H q = some k ∨ + ∃ j < win, q = bjA aligned (zA nt.val us) j ∧ newKind nt.val us.val = some k) + (hnew : ∀ j < win, ∀ k, newKind nt.val us.val = some k → H' (bjA aligned (zA nt.val us) j) = some k) : + H'.scs = C ∪ UtOfTyA 5 nt us aligned win := by + ext t + constructor + · rintro ⟨z, hz⟩ + rcases hex t _ hz with o | ⟨j, hj, e, hk⟩ + · exact Or.inl (eC ▸ ⟨z, o⟩) + · exact Or.inr ⟨(utNewKind_scA hk).1, j, hj, e⟩ + · rintro (ht | ⟨e5, j, hj, rfl⟩) + · rw [← eC] at ht + obtain ⟨z, hz⟩ := ht + exact ⟨z, hgrow t _ hz⟩ + · exact ⟨us.val, hnew j hj _ (by unfold newKind; simp [e5])⟩ + +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H H' : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s s' : S} {a : A} {σ : State Unit} {m : McState} {x p sb fi : U64} + {nt us aligned : U64} {win : Nat} + +/-- **A RAM creation keeps the reachable VSpace words** (module docstring). -/ +theorem utRt_vsCrtKeptA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (hx : x ∈ OS) (hc : capA st s x = .Untyped p sb fi false) + (h1 : p.val + fi.val ≤ aligned.val) + (h2 : aligned.val + win * 2 ^ (zA nt.val us).val ≤ p.val + 2 ^ sb.val) + (w : WritesIn st s s' (UtCrtWA st H OS s nt aligned (zA nt.val us) win)) : + ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → L.mem s' b = L.mem s b := by + intro b hb + have vr : VrOK H.omega H.frame H.scs H.scBits OS Z.V ∅ (L.ksb s) (utVsPtA st Pp Tb Lv s) := J.vsOk + have Cl : UtClrA st Z H s aligned.val (aligned.val + win * 2 ^ (zA nt.val us).val) := + utRt_winClrA J hx hc h1 h2 + refine w.mem_eq (vr.al b hb) fun h => ?_ + rcases h with h | ⟨-, g1, g2⟩ | h | ⟨t, ht, h⟩ + · exact vr.os b hb h + · obtain ⟨-, q, k, hq, -, i1, i2⟩ := vr.om b hb + rcases Cl.obj q (2 ^ k.bits) (Or.inr (Or.inr (Or.inr ⟨k, hq, rfl⟩))) with g | g <;> omega + · exact (utRt_omegaNeA J (vr.om b hb)).1 h + · exact (utRt_omegaNeA J (vr.om b hb)).2 t ht h + +/-- A reachable VSpace word is a word of `Pp ∪ Tb`. -/ +theorem utVsR_subA {kb : U64} {s0 : S} {b : U64} (h : VsR kb (utVsPtA st Pp Tb Lv s0) b) : VsR kb (Pp ∪ Tb) b := + h.imp (fun ⟨q, hq, o, ho⟩ => ⟨q, utVsPtA_sub hq, o, ho⟩) id + +/-- **The reachable VSpace words across the restart write and the creation** (module docstring): every reachable word +of the decode's state is kept, the typing holds at the creation start and the reachable set is unchanged there. -/ +theorem utRt_vsRestartA {s0 s2 q : S} + (ty : VsTyR st (L.ksb s0) (PoolSet st Pp s0) ∅ Tb Lv s0) + (hvs : ∀ b, VsR (L.ksb s0) (utVsPtA st Pp Tb Lv s0) b → Aligned b ∧ b ≠ stA (L.curW s0)) + (hks2 : L.ksb s2 = L.ksb s0) (hr : ∀ b, Aligned b → b ≠ stA (L.curW s0) → L.mem s2 b = L.mem s0 b) + (k2 : ∀ b, VsR (L.ksb s2) (utVsPtA st Pp Tb Lv s2) b → L.mem q b = L.mem s2 b) + (hksq : L.ksb q = L.ksb s0) : + VsZ st (L.ksb s0) (utVsPtA st Pp Tb Lv s0) s0 q ∧ PoolSet st Pp q = PoolSet st Pp s0 := by + have k1 : ∀ b, VsR (L.ksb s0) (utVsPtA st Pp Tb Lv s0) b → L.mem s2 b = L.mem s0 b := fun b hb => by + obtain ⟨al, ne⟩ := hvs b hb + exact hr b al ne + obtain ⟨-, ept⟩ := utVsPt_keepA ty hks2 k1 + have k : ∀ b, VsR (L.ksb s0) (utVsPtA st Pp Tb Lv s0) b → L.mem q b = L.mem s0 b := fun b hb => by + have hb2 : VsR (L.ksb s2) (utVsPtA st Pp Tb Lv s2) b := by rw [hks2, ept]; exact hb + rw [k2 b hb2]; exact k1 b hb + exact ⟨VsZ.of_keep k, poolSet_keepA hksq k⟩ + +/-- **The old queue core at the creation end** (`utRt_fullSchedSRA`'s first step, on its own). -/ +theorem utRt_coreKeptA {κ : U64 → U64} {s s' : S} (Hs : SchedSRA st Z m κ s σ) (hks : L.ksb s' = L.ksb s) + (hm : ∀ a, Aligned a → SReadA st Z m s σ a → a ≠ nwA st s → L.mem s' a = L.mem s a) + (hnode : ∀ c ∈ Z.Cs, ∀ o : U64, o.val < 4288 → o.val % 8 = 0 → o.val ≠ 88 → + L.mem s' (L.nodeAt s c o) = L.mem s (L.nodeAt s c o)) : + SchedCoreA st Z.T Z.Cs s' Z.Q Z.R := by + have hT := Hs.core.tcbs + have h0 : (0#u64 : U64) ∈ Z.Cs := by + obtain ⟨k0, hk, -, h⟩ := Hs.csN + exact (h 0#u64).2 (by simpa using hk) + have tK : ∀ t ∈ Z.T, ∀ k : U64, (k = 3520#u64 ∨ k = 3448#u64 ∨ k = 3432#u64 ∨ k = 3464#u64 ∨ k = 3592#u64 ∨ + k = 3480#u64 ∨ k = 3488#u64) → L.mem s' (wa t k) = L.mem s (wa t k) := by + intro t ht k hk + have al : Aligned (wa t k) := by + rcases hk with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> exact field_aligned (hT t ht) (by simp) (by simp) + have hr : SReadA st Z m s σ (wa t k) := Or.inr (Or.inr (Or.inr (Or.inl ⟨t, ht, k, hk, rfl⟩))) + refine hm _ al hr (fun e => ?_) + have nd := Hs.core.nodes 0#u64 h0 + have f := (hT t ht).fits + have kv : k.val < 4096 := by rcases hk with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> simp + have v : (wa t k).val = t.val + k.val := by + rw [wa_valP]; exact Nat.mod_eq_of_lt (by omega) + have w := congrArg UScalar.val e + rw [v, nodeAt_val nd (by rw [debug_val]; omega), debug_val] at w + rcases nd.disj t ht with g | g <;> omega + exact Hs.core.agree hks (fun y hy => ⟨tK y hy 3480#u64 (by simp), tK y hy 3488#u64 (by simp)⟩) + (fun c hc o h1 h2 h3 => hnode c hc o h1 h2 (by omega)) (fun y hy => tK y hy 3592#u64 (by simp)) + (fun y hy => tK y hy 3520#u64 (by simp)) (fun y hy => tK y hy 3448#u64 (by simp)) + +end + +section all +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H H' : Heap} {OS OS1 U0 Ac0 Vn U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {N E0 Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n n' : Nat} {nA : U64} {J5 J5' J7' : JSlotA S} {s s2 q : S} {a a2 : A} {σ σ2 σ' : Fl4SpecA64.State Unit} + {m m' : Fl4SpecA64.McState} {ia ie lo hi : Nat} {slot p sb fi aligned nt us dest off win : U64} + {extra : Aeneas.Std.Array obj.Slot 3#usize} {G' : MdbGhostA} {P : ObjRef → Prop} {d : KernelObject} + +/-- (record edit #8) A creation that keeps or zeroes the reachable VSpace words and grows `Tb` shrinks the reachable +set. -/ +theorem utVsPtA_retypeA {Pp Tb Tb' : Set U64} {Lv : U64 → Nat} {s q : S} + (h : VsTyR st (L.ksb s) (PoolSet st Pp s) ∅ Tb Lv s) (hk : L.ksb q = L.ksb s) + (k : VsZ st (L.ksb s) (utVsPtA st Pp Tb Lv s) s q) (sub : Tb ⊆ Tb') : + utVsPtA st Pp Tb' Lv q ⊆ utVsPtA st Pp Tb Lv s := by + obtain ⟨ty, le⟩ := utVsZero_keepA h hk k + rw [utVsPtA_growA ty sub] + exact le + +set_option maxHeartbeats 2000000 in +/-- **The entry-invariant bundle of a RAM creation without a reset** (module docstring). -/ +theorem utRt_sysAllA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E0 Rp Bf stack κ n J5 (J7A st (J7R2A st ar nA)) s a σ m) + (E : S1aRtIn st H Z.Cs OS U0 Ac0 s ia ie slot p sb fi false extra H H' OS1 s2 q lo hi aligned nt us dest off + win) + (noch : ¬ ∃ v, cdt.ensure_no_children st s slot = ok (.Ok v)) (hp : 0 < aligned.val) + -- A64 P1's creation top (`utRt_j7TopA`) + (arm : ArmQIVAt st H Z.Cs OS ia ie s H' (s1aRtSlots OS1 false nt us aligned win) Vn q) + (ko : HeapKsOff H' (L.ksb s)) + (j7at : ∀ (Z'' : XSetsA) (σ'' : Fl4SpecA64.State Unit) (m'' : Fl4SpecA64.McState), + UtJ7SplitA st Z Z'' (s1aRtSlots OS1 false nt us aligned win) s q slot p sb nt us aligned win.val → + J7A st (J7R2A st ar nA) Z'' H' (s1aRtSlots OS1 false nt us aligned win) q σ'' m'') + -- the caller's restart write + (hvs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → Aligned b ∧ b ≠ stA (L.curW s)) + (hr : ∀ b, Aligned b → b ≠ stA (L.curW s) → L.mem s2 b = L.mem s b) + -- the record and the creation at its start + (J2 : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E0 Rp Bf stack κ n J5' J7' s2 a2 σ2 m) + (hx : slot ∈ OS) (hc : capA st s2 slot = .Untyped p sb fi false) + (h1 : p.val + fi.val ≤ aligned.val) + (h2 : aligned.val + win.val * 2 ^ (zA nt.val us).val ≤ p.val + 2 ^ sb.val) + (w : WritesIn st s2 q (UtCrtWA st H OS s2 nt aligned (zA nt.val us) win.val)) + -- the grown heap + (hgrow : ∀ q k, H q = some k → H' q = some k) + (hex : ∀ q k, H' q = some k → H q = some k ∨ + ∃ j < win.val, q = bjA aligned (zA nt.val us) j ∧ newKind nt.val us.val = some k) + (hnew : ∀ j < win.val, ∀ k, newKind nt.val us.val = some k → H' (bjA aligned (zA nt.val us) j) = some k) + -- the scheduler bundle at the creation start + (Sch : UtAsmSchedInA st Z (utRtZA Z G' nt us aligned win.val (UtOfTyA 1 nt us aligned win.val) + (UtOfTyA 5 nt us aligned win.val) (UtOfTyA 12 nt us aligned win.val)) s2 q σ2 σ' m m' + (UtOfTyA 1 nt us aligned win.val) (UtOfTyA 5 nt us aligned win.val) P d n') : + (UtAsmSysInA st ar nA Z (utRtZA Z G' nt us aligned win.val (UtOfTyA 1 nt us aligned win.val) + (UtOfTyA 5 nt us aligned win.val) (UtOfTyA 12 nt us aligned win.val)) H H' OS + (s1aRtSlots OS1 false nt us aligned win) Vn (utUserA H' U) Pp + (Tb ∪ UtSchedNewA nt us aligned win.val) Lv ia ie s q σ' m' slot p sb nt us aligned win.val + (Tb := Tb)) ∧ + utVsPtA st Pp (Tb ∪ UtSchedNewA nt us aligned win.val) Lv q ⊆ utVsPtA st Pp Tb Lv s := by + have ksq : L.ksb q = L.ksb s := E.ksb + have ks2 : L.ksb s2 = L.ksb s := by rw [← Sch.hks]; exact ksq + -- the split + obtain ⟨-, -, -, -, -, -, vc0, -⟩ := J.j7 + have split : UtJ7SplitA st Z (utRtZA Z G' nt us aligned win.val (UtOfTyA 1 nt us aligned win.val) + (UtOfTyA 5 nt us aligned win.val) (UtOfTyA 12 nt us aligned win.val)) + (s1aRtSlots OS1 false nt us aligned win) s q slot p sb nt us aligned win.val := + utRt_j7SplitA E rfl subset_rfl subset_rfl subset_rfl subset_rfl subset_rfl subset_rfl + (fun r _ => utOffRg_noChA noch r) (fun r _ => ⟨utOffRg_noChA noch r, utOffRg_noChA noch _⟩) vc0 + (fun r h => h) (fun e r h => ⟨e, h⟩) + -- the reachable VSpace words + obtain ⟨vsk, hps⟩ := utRt_vsRestartA J.sys.ent.vs hvs ks2 hr (utRt_vsCrtKeptA J2 hx hc h1 h2 w) ksq + refine ⟨?_, utVsPtA_retypeA J.sys.ent.vs ksq vsk Set.subset_union_left⟩ + -- the grown heap's TCBs + have eT : H'.tcbs = Z.T ∪ UtOfTyA 1 nt us aligned win.val := utRt_tcbsNewA J.tcbs hgrow hex hnew + have eC : H'.scs = Z.C ∪ UtOfTyA 5 nt us aligned win.val := utRt_scsNewA J.scs hgrow hex hnew + have h0 : (0#u64 : U64) ∈ Z.Cs := by + obtain ⟨k, hk, -, h⟩ := J.j.1.csN + exact (h 0#u64).2 (by simpa using hk) + have core : SchedCoreA st Z.T Z.Cs q Z.Q Z.R := utRt_coreKeptA J2.sr Sch.hks Sch.hm Sch.hnode + exact utRt_sysInA J E rfl hp (fun _ _ => rfl) arm ko j7at split rfl eT eC rfl rfl rfl eT h0 Sch.hTd Sch.hTa + Sch.hTnode Sch.hTw core vsk hps + +end all + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtApSysA.lean b/proofs/Fl4ProofsA64/P3/UtApSysA.lean new file mode 100644 index 000000000..93e5143f7 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtApSysA.lean @@ -0,0 +1,92 @@ +/- +P3 (aarch64, A64-P3 Untyped, full-record post, discharge): **the entry-invariant bundle `UtAsmSysInA`** from A64 +P1's creation end (`S1aRtIn`) and the record before it. Discharged here: `HeapOK`, `OSOK` (`UtEnd.osok`), `UserOK` +(at `utUserA`), `VsTyR` (the table set grows by the new table pages, `VsTyR.mono`), the old-queue core and the six +`ap` fields. Left as hypotheses: A64 P1's top (`utRt_j7TopA`), the shapes of the grown sets, and that the creation +keeps or zeroes the reachable VSpace words (`vsk`, `hps`). + +No named premise. New theorems only. +-/ +import Fl4ProofsA64.P3.UtApA +import Fl4ProofsA64.P3.UtAsmSysA +import Fl4ProofsA64.P3.UtFullSchedA +import Fl4ProofsA64.P1.MdbGrowO + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Sched Classical + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z Z' : XSetsA} {H H1 H' : Heap} {Cs OS OS1 U0 Ac0 Vn Tn U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {N E0 Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {J5 : JSlotA S} {s s1 q : S} {a : A} {σ σ' : Fl4SpecA64.State Unit} + {m m' : Fl4SpecA64.McState} {ia ie lo hi : Nat} {slot p sb fi aligned nt us dest off win : U64} {dev : Bool} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + +theorem utRt_sysInA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E0 Rp Bf stack κ n J5 (J7A st (J7R2A st ar nA)) s a σ m) + (E : S1aRtIn st H Z.Cs OS U0 Ac0 s ia ie slot p sb fi dev extra H1 H' OS1 s1 q lo hi aligned nt us dest off win) + (hH1 : H1 = H) (hp : 0 < aligned.val) (ram12 : nt.val = 12 → 0 < win.val → dev = false) + (arm : ArmQIVAt st H Z.Cs OS ia ie s H' (s1aRtSlots OS1 dev nt us aligned win) Vn q) + (ko : HeapKsOff H' (L.ksb s)) + (j7at : ∀ (Z'' : XSetsA) (σ'' : Fl4SpecA64.State Unit) (m'' : Fl4SpecA64.McState), + UtJ7SplitA st Z Z'' (s1aRtSlots OS1 dev nt us aligned win) s q slot p sb nt us aligned win.val → + J7A st (J7R2A st ar nA) Z'' H' (s1aRtSlots OS1 dev nt us aligned win) q σ'' m'') + (split : UtJ7SplitA st Z Z' (s1aRtSlots OS1 dev nt us aligned win) s q slot p sb nt us aligned win.val) + -- the grown sets + (cs : Z'.Cs = Z.Cs) (tcbs : H'.tcbs = Z'.T) (scs : H'.scs = Z'.C) (pp : Z'.Pp = Z.Pp) (eQ : Z'.Q = Z.Q) + (eR : Z'.R = Z.R) + -- the new TCBs (for the old queue lists) + (eT : H'.tcbs = Z.T ∪ Tn) (h0 : (0#u64 : U64) ∈ Z.Cs) (hd : ∀ t ∈ Tn, t ∉ Z.T) (ha : ∀ t ∈ Tn, TcbAddr t) + (hn : ∀ c ∈ Z.Cs, ∀ t ∈ Tn, t.val + 2 ^ 12 ≤ L.nodeBase q c ∨ L.nodeBase q c + 4288 ≤ t.val) + (hw : ∀ t ∈ Tn, L.mem q (affA t) = 0#u64 ∧ L.mem q (prioA t) = 0#u64 ∧ L.mem q (inqA t) = 0#u64) + (core : SchedCoreA st Z.T Z.Cs q Z.Q Z.R) + -- the reachable VSpace words + (vsk : VsZ st (L.ksb s) (utVsPtA st Pp Tb Lv s) s q) (hps : PoolSet st Pp q = PoolSet st Pp s) : + UtAsmSysInA st ar nA Z Z' H H' OS (s1aRtSlots OS1 dev nt us aligned win) Vn (utUserA H' U) Pp + (Tb ∪ UtSchedNewA nt us aligned win.val) Lv ia ie s q σ' m' slot p sb nt us aligned win.val + (Tb := Tb) := by + have kb : L.ksb q = L.ksb s := E.ksb + have hH' := E.heapOK + have hcol := J.j7 + obtain ⟨Pt, Ub, -, -, armc, -⟩ := hcol + obtain ⟨Pt', Ub', -, -, armc', -⟩ := j7at Z' σ' m' split + have A : SlotApA Z' H' (s1aRtSlots OS1 dev nt us aligned win) (L.ksb s) := + utRt_apA E hH1 J.ap armc.case.slot armc'.case.slot J.sys.ent.heap armc.vh J.ptH J.setsInH J.sys.ent.ksw split + ram12 + have vs : VsTyR st (L.ksb q) (PoolSet st Pp q) ∅ (Tb ∪ UtSchedNewA nt us aligned win.val) Lv q := + (utVsTyR_keepA J.sys.ent.vs kb vsk hps).mono (Set.empty_subset _) Set.subset_union_left + have core' : SchedCoreA st H'.tcbs Z'.Cs q Z'.Q Z'.R := by + rw [eT, cs, eQ, eR] + exact utRt_schedCoreA core h0 hd ha hn hw + exact + { arm := arm + ko := ko + j7at := j7at + split := split + ksb := kb + heapOK := hH' + os := E.creation.osok E.kept hp + user := by rw [cs]; exact utRt_userA J.sys.ent.user kb + vs := vs + cs := cs + tcbs := tcbs + scs := scs + pp := pp + tb := Set.subset_union_left + s1New := fun x _ h => Or.inr h + core := core' + sca := A.sca + irqSt := by rw [kb]; exact A.irqSt + vcAl := A.vcAl + vc := A.vc + vsAp := by rw [kb]; exact A.vs + ext := A.ext } + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtAsmAll0A.lean b/proofs/Fl4ProofsA64/P3/UtAsmAll0A.lean new file mode 100644 index 000000000..506932b7d --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtAsmAll0A.lean @@ -0,0 +1,259 @@ +/- +P3 (aarch64, A64-P3 Untyped, full-record post, assembly): **the record after a creation, with the decode-state record +over other sets than the creation-start record** (the reset path: A64 P1's creation top is over the record's sets at +the decode, the creation starts from the record on the dropped sets). + +* `UtAsmSys0InA`: `UtAsmSysInA` with the grown record's pools and Stage-1 tables given directly (`pp : Z'.Pp = Pp'`, + `s1 : Z'.S1 ⊆ Tb'`) instead of through the decode-state record (a reset drops pools and tables). +* `utRt_sys0A`: the bundle of the non-reset path is one (`Pp' = Pp`). +* `utRt_asmSys0A`: the entry-invariant side of the grown record from it; the decode-state record gives only the + kernel-state block bound and `PoolOff`. +* **`utRt_asmAll0A`**: `JStage3A` after a creation from the decode-state record `Jd` (any sets) for the + entry-invariant bundle and the creation-start record `J` for the other five. + +No named premise. New definitions and theorems only. +-/ +import Fl4ProofsA64.P3.UtApRec6A +import Fl4ProofsA64.P3.UtApRec5A +import Fl4ProofsA64.P3.UtAsmAllA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + +/-- The creation's side of the entry-invariant fields, with the grown record's pools and Stage-1 tables given. -/ +structure UtAsmSys0InA (st : machine.Store S) [L : LawfulStore st] (ar : machine.Arch A) (nA : U64) + (Z Z' : XSetsA) (H H' : Heap) (OS OS' Vn U Pp' Tb' : Set U64) (Lv' : U64 → Nat) (ia ie : Nat) (s s' : S) + (σ' : State Unit) (m' : McState) (slot p sb nt us aligned : U64) (win : Nat) : Prop where + /- A64 P1's creation top -/ + arm : ArmQIVAt st H Z.Cs OS ia ie s H' OS' Vn s' + ko : HeapKsOff H' (L.ksb s) + j7at : ∀ (Z'' : XSetsA) (σ'' : State Unit) (m'' : McState), + UtJ7SplitA st Z Z'' OS' s s' slot p sb nt us aligned win → J7A st (J7R2A st ar nA) Z'' H' OS' s' σ'' m'' + split : UtJ7SplitA st Z Z' OS' s s' slot p sb nt us aligned win + ksb : L.ksb s' = L.ksb s + /- the rest of the entry invariant -/ + heapOK : HeapOK H' + os : OSOK H'.tcbs H'.omega H'.frame OS' + user : Fl4ProofsA64.P1.UserOK st H'.tcbs Z'.Cs H'.omega H' U s' + vs : VsTyR st (L.ksb s') (PoolSet st Pp' s') ∅ Tb' Lv' s' + /- the grown sets -/ + cs : Z'.Cs = Z.Cs + tcbs : H'.tcbs = Z'.T + scs : H'.scs = Z'.C + pp : Z'.Pp = Pp' + s1 : Z'.S1 ⊆ Tb' + core : SchedCoreA st H'.tcbs Z'.Cs s' Z'.Q Z'.R + /- slot apartness -/ + sca : ScApart H'.scs H'.scBits OS' + irqSt : IrqStApart OS' (L.ksb s') + vcAl : ∀ v ∈ Z'.V, Aligned v + vc : ∀ v ∈ Z'.V, ∀ b, (b = v ∨ b = wa v 24#u64) → ¬ InOS OS' b ∧ ¬ W7.TLow H'.tcbs b + vsAp : ∀ b, VsFootA Z'.Pp Z'.S1 Z'.Y (L.ksb s') b → ¬ InOS OS' b ∧ ∀ t ∈ H'.tcbs, b.val < t.val ∨ t.val + 4096 ≤ b.val + ext : ∀ b, ExtFootE Z'.X Z'.Y Z'.T2 b → ¬ InOS OS' b ∧ ∀ t ∈ H'.tcbs, b.val < t.val ∨ t.val + 4096 ≤ b.val + +variable {AR : A → HwRootA → Prop} {Z Z' Zd : XSetsA} {H H' Hd : Heap} + {OS OS' OSd Vn U U' Ud Ac Acd Pp Ppd Tb Tb' Tbd : Set U64} {Lv Lv' Lvd : U64 → Nat} {ia ie : Nat} + {N E Rp Nd Ed Rpd Nn En Rn Tn Cn : Set U64} {Bf Bfd : Set (U64 × Fl4SpecA64.Word)} + {stack stackd : U64 → List U64} {κ κd : U64 → U64} {n : Nat} {nA : U64} {J5 J5d J7 : JSlotA S} {s0 s s' : S} + {a0 a a' : A} {σ0 σ σ' : State Unit} {m0 m m' : McState} {x p sb fi nfi nt us aligned : U64} {win : Nat} + {slot0 p0 sb0 nt0 us0 aligned0 : U64} {win0 : Nat} {th' : TypedHeapA} {Q : U64 → Prop} + {P : ObjRef → Prop} {d : KernelObject} + +/-- **The non-reset bundle is one** (module docstring). -/ +theorem utRt_sys0A + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (K : UtAsmSysInA st ar nA Z Z' H H' OS OS' Vn U' Pp Tb' Lv' ia ie s s' σ' m' slot0 p0 sb0 nt0 us0 aligned0 win0 + (Tb := Tb)) : + UtAsmSys0InA st ar nA Z Z' H H' OS OS' Vn U' Pp Tb' Lv' ia ie s s' σ' m' slot0 p0 sb0 nt0 us0 aligned0 win0 where + arm := K.arm + ko := K.ko + j7at := K.j7at + split := K.split + ksb := K.ksb + heapOK := K.heapOK + os := K.os + user := K.user + vs := K.vs + cs := K.cs + tcbs := K.tcbs + scs := K.scs + pp := K.pp.trans J.pp + s1 := fun x hx => by + rcases K.split.s1 x hx with ⟨old, -⟩ | new + · exact K.tb (J.s1 old) + · exact K.s1New x hx new + core := K.core + sca := K.sca + irqSt := K.irqSt + vcAl := K.vcAl + vc := K.vc + vsAp := K.vsAp + ext := K.ext + +/-- **The entry-invariant side of the grown record** (module docstring). -/ +theorem utRt_asmSys0A + (Jd : JStage3A st AR Zd Hd OSd Ud Acd Ppd Tbd Lvd ia ie Nd Ed Rpd Bfd stackd κd n J5d J7 s0 a0 σ0 m0) + (K : UtAsmSys0InA st ar nA Zd Z' Hd H' OSd OS' Vn U Pp' Tb' Lv' ia ie s0 s' σ' m' slot0 p0 sb0 nt0 us0 + aligned0 win0) : + ∃ Ac', UtAsmSysOutA st ar nA Z' H' OS' U Ac' Pp' Tb' Lv' ia ie s' σ' m' := by + obtain ⟨sysl, symL, ⟨Ac', blk⟩, -, -, poolOff, -⟩ := K.arm + obtain ⟨⟨Q0, R0, h0⟩, bound, xinv, scsok, sg⟩ := blk + have j7 := K.j7at Z' σ' m' K.split + have sq : SchedQueuesInv st H'.tcbs Z'.Cs H'.omega H' s' Z'.Q Z'.R := by + rw [← K.cs] at h0 + exact utRt_sqA K.core h0 + refine ⟨Ac', ?_⟩ + exact + { sys := + { ent := ⟨K.heapOK, ⟨⟨_, _, sq⟩, bound, K.cs.symm ▸ xinv, sg, scsok⟩, K.os, K.user, K.vs, + K.ksb.symm ▸ Jd.sys.ent.ksw⟩ + sysl := K.cs.symm ▸ sysl } + symL := K.cs.symm ▸ symL + poolOff := K.cs.symm ▸ poolOff Jd.poolOff + sq := sq + tcbs := K.tcbs + scs := K.scs + ap := ⟨K.sca, K.irqSt, K.vcAl, K.vc, K.vsAp, K.ext⟩ + pp := K.pp + s1 := K.s1 + ko := K.ksb.symm ▸ K.ko + j7 := j7 + vsOk := utVsOk_of_J7A j7 K.vs } + +set_option maxHeartbeats 2000000 in +/-- **The record after a creation, decode-state record over any sets** (module docstring). -/ +theorem utRt_asmAll0A + (Jd : JStage3A st AR Zd Hd OSd Ud Acd Ppd Tbd Lvd ia ie Nd Ed Rpd Bfd stackd κd n J5d (J7A st (J7R2A st ar nA)) + s0 a0 σ0 m0) + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (IC : UtAsmCapsInA st Z Z' H H' OS OS' s s' x p sb fi nfi nt us aligned win) + (KSp : UtAsmSpecInA st Z Z' OS s s' σ σ' m m' nt us aligned win) + (KI : UtAsmIpcInA st Z th' H H' N E Rp Bf Nn En Rn Tn Cn Q s s' σ σ' m m' P d) + (KH : UtAsmHeapInA st Z Z' H H' OS OS' Rp Nn En Rn Bf stack (fun c => if c ∈ Cn then [] else stack c) s s' + σ σ' m m' x p sb fi nt us aligned win) + (KSc : UtAsmSchedInA st Z Z' s s' σ σ' m m' Tn Cn P d n) + (KS : UtAsmSys0InA st ar nA Zd Z' Hd H' OSd OS' Vn U' Pp' Tb' Lv' ia ie s0 s' σ' m' slot0 p0 sb0 nt0 us0 + aligned0 win0) + (hth : th' = Z'.th) + (K : JAtZAX (L := L) AR Z' s' a' σ' m') + (j5 : J5A st Z' H' OS' s' σ' m') + (R5 : UtRec5OutA st Z' H' s' σ') + -- the spec's `vcpu::init` step and the record-edit-#6 fields + {τ : State Unit} (Hτ : UtVcStepA σ' τ) (R6 : UtRec6OutA st Z' OS' s' σ') + (vcS : VcpuStateSnapA Z'.V (L.mem s') τ) + (hdom : Sched.NdOKA (st := st) s' τ) (hipi : Sched.SilentScheduleIpiA (st := st) s') + (hbv : ∀ b, Endp.BufW Bf b → ¬ VsR (L.ksb s') (utVsPtA st Pp' Tb' Lv' s') b) : + ∃ Ac', JStage3A st AR Z' H' OS' U' Ac' Pp' Tb' Lv' ia ie (N ∪ Nn) (E ∪ En) (Rp ∪ Rn) Bf + (fun c => if c ∈ Cn then [] else stack c) (fun c => if c ∈ Cn then 0#u64 else κ c) n (J5A st) + (J7A st (J7R2A st ar nA)) s' a' τ m' := by + obtain ⟨Ac', SO⟩ := utRt_asmSys0A Jd KS + have CO := utRt_asmCapsA J IC + have SpO := utRt_asmSpecA J IC KSp + have IO := utRt_asmIpcA J KI + have HO := utRt_asmHeapA J IC KH + obtain ⟨ScO, blk⟩ := utRt_asmSchedA (En := En) (Nn := Nn) J KSc + rw [← KSc.eT, ← KSc.eC, hth] at IO + refine ⟨Ac', ?_⟩ + exact + { sys := SO.sys + j := utVc_jatA Hτ K + tcbs := SO.tcbs + scs := SO.scs + sq := SO.sq + ap := SO.ap + pp := SO.pp + s1 := SO.s1 + ko := SO.ko + ptH := HO.ptH + tabInj := by rw [Hτ.arch]; exact SpO.tabInj + poolOff := SO.poolOff + objAlloc := CO.objAlloc + idleAnchor := CO.idleAnchor + vsVc := HO.vsVc + sr := utVc_schedSRA Hτ ScO.sr + scCov := utVc_scCovA Hτ IO.scCov + scSize := utVc_scSizeA Hτ HO.scSize + ep := utVc_epA Hτ IO.ep IO.iplaced.e + ntfn := utVc_ntfnA Hτ IO.ntfn IO.iplaced.n + reply := utVc_replyA Hτ IO.reply + bufCover := utVc_bufCoverA Hτ IO.bufCover + bufNative := IO.bufNative + iplaced := IO.iplaced + ipcPlace := IO.ipcPlace + idleNoFault := utVc_idleNoFaultA Hτ IO.idleNoFault + mcp := utVc_mcpA Hτ IO.mcp + yieldRel := utVc_yieldA Hτ IO.yieldRel + sinv := utVc_sinvA Hτ IO.sinv + setsInH := HO.setsInH + utIpc := HO.utIpc + ipcTh := IO.ipcTh + deadExt := utVc_deadExtA Hτ HO.deadExt + specFree := utVc_specFreeA Hτ SpO.specFree + utBlk := CO.utBlk + unmPt := utVc_unmPtA Hτ HO.unmPt + tabS1 := HO.tabS1 + tabExt := HO.tabExt + specZero := utVc_specZeroA Hτ SpO.specZero + origLive := utVc_origLiveA Hτ SpO.origLive + nullOrig := utVc_nullOrigA Hτ SpO.nullOrig + thPos := HO.thPos + frBase := HO.frBase + devFr := utVc_devFrA Hτ HO.devFr + finCov := utVc_finCovA Hτ SpO.finCov + idleAll := SpO.idleAll + ipcHeapCov := IO.ipcHeapCov + zombieNoVcpu := CO.zombieNoVcpu + zombieTcb := CO.zombieTcb + bufOff := HO.bufOff + vsOk := SO.vsOk + bufAl := utVc_bufAlA Hτ IO.bufAl + zombieBnd := CO.zombieBnd + devUtOff := CO.devUtOff + sidle := utVc_sidleA Hτ ScO.sidle + capTy := CO.capTy + scCpu := ScO.scCpu + scNZ := ScO.scNZ + vcRun := ScO.vcRun + vcSym1 := ScO.vcSym1 + runVcpu := ScO.runVcpu + vcSym2 := ScO.vcSym2 + blkQ := utVc_blkQA Hτ blk + heapWin := R5.heapWin + coresAll := R5.coresAll + ptCapIn := R5.ptCapIn + symL := R5.symL + fhOK := R5.fhOK + zwf := R5.zwf + schedValid := utVc_schedValidA Hτ R5.schedValid + utWin := R6.utWin + devWin := R6.devWin + vcBack := R6.vcBack + runBack := R6.runBack + vcSnap := vcS + vcOS := R6.vcOS + userFr := utVc_userFrA Hτ R6.userFr + frTh := R6.frTh + bfSpec := J.bfSpec + curVcpuNone := Hτ.vcur.trans (KSp.vcur.trans J.curVcpuNone) + ipcSorted := IO.ipcSorted.of_q Hτ.qkeep + activeReplyScs := IO.activeReplyScs.of_q Hτ.qkeep + domOK := hdom + ipiZero := hipi + bufVs := hbv + j5 := j5 + j7 := SO.j7 } + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtAsmAllA.lean b/proofs/Fl4ProofsA64/P3/UtAsmAllA.lean new file mode 100644 index 000000000..daf1a52f6 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtAsmAllA.lean @@ -0,0 +1,164 @@ +/- +P3 (aarch64, A64-P3 Untyped, full-record post, assembly): **the whole record `JStage3A` after a creation**, from the +six input bundles (`UtAsmCapsInA`, `UtAsmSpecInA`, `UtAsmIpcInA`, `UtAsmHeapInA`, `UtAsmSchedInA`, `UtAsmSysInA`), +the creation's core post `JAtZAX` and `J5A` at the grown sets. + +Every field of `JStage3A` (and of `JEntA`) is one of the six assemblies' outputs (`utRt_asm*A`), the core post or +`J5A`. The entry-invariant bundle is stated against the record at the decode's state (`Jd`, A64 P1's creation top is +there); the other five against the record at the creation start (`J`, after the caller's restart write). The two +records share every set; the grown record's IPC sets, reply stacks and SC cores are the IPC and scheduler bundles'. + +No named premise. New theorems only. +-/ +import Fl4ProofsA64.P3.UtApRec6A +import Fl4ProofsA64.P3.UtApRec5A +import Fl4ProofsA64.P3.UtAsmCapsA +import Fl4ProofsA64.P3.UtAsmSpecA +import Fl4ProofsA64.P3.UtAsmIpcA +import Fl4ProofsA64.P3.UtAsmHeapA +import Fl4ProofsA64.P3.UtAsmSchedA +import Fl4ProofsA64.P3.UtAsmSysA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z Z' : XSetsA} {H H' : Heap} {OS OS' U U' Ac Pp Tb Tb' Vn : Set U64} + {Lv Lv' : U64 → Nat} {ia ie : Nat} {N E Rp Nn En Rn Tn Cn : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {nA : U64} {J5 J5' J7' : JSlotA S} {s0 s s' : S} + {a0 a a' : A} {σ0 σ σ' : State Unit} {m0 m m' : McState} {x p sb fi nfi nt us aligned : U64} {win : Nat} + {slot0 p0 sb0 nt0 us0 aligned0 : U64} {win0 : Nat} {th' : TypedHeapA} {Q : U64 → Prop} + {P : ObjRef → Prop} {d : KernelObject} + +set_option maxHeartbeats 2000000 in +/-- **The record after a creation** (module docstring). -/ +theorem utRt_asmAllA + (Jd : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7A st (J7R2A st ar nA)) s0 a0 σ0 m0) + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5' J7' s a σ m) + (IC : UtAsmCapsInA st Z Z' H H' OS OS' s s' x p sb fi nfi nt us aligned win) + (KSp : UtAsmSpecInA st Z Z' OS s s' σ σ' m m' nt us aligned win) + (KI : UtAsmIpcInA st Z th' H H' N E Rp Bf Nn En Rn Tn Cn Q s s' σ σ' m m' P d) + (KH : UtAsmHeapInA st Z Z' H H' OS OS' Rp Nn En Rn Bf stack (fun c => if c ∈ Cn then [] else stack c) s s' + σ σ' m m' x p sb fi nt us aligned win) + (KSc : UtAsmSchedInA st Z Z' s s' σ σ' m m' Tn Cn P d n) + (KS : UtAsmSysInA st ar nA Z Z' H H' OS OS' Vn U' Pp Tb' Lv' ia ie s0 s' σ' m' slot0 p0 sb0 nt0 us0 aligned0 + win0 (Tb := Tb)) + (hth : th' = Z'.th) + (K : JAtZAX (L := L) AR Z' s' a' σ' m') + (j5 : J5A st Z' H' OS' s' σ' m') + (R5 : UtRec5OutA st Z' H' s' σ') + -- the spec's `vcpu::init` step and the record-edit-#6 fields + {τ : State Unit} (Hτ : UtVcStepA σ' τ) (R6 : UtRec6OutA st Z' OS' s' σ') + (vcS : VcpuStateSnapA Z'.V (L.mem s') τ) + (hdom : Sched.NdOKA (st := st) s' τ) (hipi : Sched.SilentScheduleIpiA (st := st) s') + (hbv : ∀ b, Endp.BufW Bf b → ¬ VsR (L.ksb s') (utVsPtA st Pp Tb' Lv' s') b) : + ∃ Ac', JStage3A st AR Z' H' OS' U' Ac' Pp Tb' Lv' ia ie (N ∪ Nn) (E ∪ En) (Rp ∪ Rn) Bf + (fun c => if c ∈ Cn then [] else stack c) (fun c => if c ∈ Cn then 0#u64 else κ c) n (J5A st) + (J7A st (J7R2A st ar nA)) s' a' τ m' := by + obtain ⟨Ac', SO⟩ := utRt_asmSysA Jd KS + have CO := utRt_asmCapsA J IC + have SpO := utRt_asmSpecA J IC KSp + have IO := utRt_asmIpcA J KI + have HO := utRt_asmHeapA J IC KH + obtain ⟨ScO, blk⟩ := utRt_asmSchedA (En := En) (Nn := Nn) J KSc + rw [← KSc.eT, ← KSc.eC, hth] at IO + refine ⟨Ac', ?_⟩ + exact + { sys := SO.sys + j := utVc_jatA Hτ K + tcbs := SO.tcbs + scs := SO.scs + sq := SO.sq + ap := SO.ap + pp := SO.pp + s1 := SO.s1 + ko := SO.ko + ptH := HO.ptH + tabInj := by rw [Hτ.arch]; exact SpO.tabInj + poolOff := SO.poolOff + objAlloc := CO.objAlloc + idleAnchor := CO.idleAnchor + vsVc := HO.vsVc + sr := utVc_schedSRA Hτ ScO.sr + scCov := utVc_scCovA Hτ IO.scCov + scSize := utVc_scSizeA Hτ HO.scSize + ep := utVc_epA Hτ IO.ep IO.iplaced.e + ntfn := utVc_ntfnA Hτ IO.ntfn IO.iplaced.n + reply := utVc_replyA Hτ IO.reply + bufCover := utVc_bufCoverA Hτ IO.bufCover + bufNative := IO.bufNative + iplaced := IO.iplaced + ipcPlace := IO.ipcPlace + idleNoFault := utVc_idleNoFaultA Hτ IO.idleNoFault + mcp := utVc_mcpA Hτ IO.mcp + yieldRel := utVc_yieldA Hτ IO.yieldRel + sinv := utVc_sinvA Hτ IO.sinv + setsInH := HO.setsInH + utIpc := HO.utIpc + ipcTh := IO.ipcTh + deadExt := utVc_deadExtA Hτ HO.deadExt + specFree := utVc_specFreeA Hτ SpO.specFree + utBlk := CO.utBlk + unmPt := utVc_unmPtA Hτ HO.unmPt + tabS1 := HO.tabS1 + tabExt := HO.tabExt + specZero := utVc_specZeroA Hτ SpO.specZero + origLive := utVc_origLiveA Hτ SpO.origLive + nullOrig := utVc_nullOrigA Hτ SpO.nullOrig + thPos := HO.thPos + frBase := HO.frBase + devFr := utVc_devFrA Hτ HO.devFr + finCov := utVc_finCovA Hτ SpO.finCov + idleAll := SpO.idleAll + ipcHeapCov := IO.ipcHeapCov + zombieNoVcpu := CO.zombieNoVcpu + zombieTcb := CO.zombieTcb + bufOff := HO.bufOff + vsOk := SO.vsOk + bufAl := utVc_bufAlA Hτ IO.bufAl + zombieBnd := CO.zombieBnd + devUtOff := CO.devUtOff + sidle := utVc_sidleA Hτ ScO.sidle + capTy := CO.capTy + scCpu := ScO.scCpu + scNZ := ScO.scNZ + vcRun := ScO.vcRun + vcSym1 := ScO.vcSym1 + runVcpu := ScO.runVcpu + vcSym2 := ScO.vcSym2 + blkQ := utVc_blkQA Hτ blk + heapWin := R5.heapWin + coresAll := R5.coresAll + ptCapIn := R5.ptCapIn + symL := R5.symL + fhOK := R5.fhOK + zwf := R5.zwf + schedValid := utVc_schedValidA Hτ R5.schedValid + utWin := R6.utWin + devWin := R6.devWin + vcBack := R6.vcBack + runBack := R6.runBack + vcSnap := vcS + vcOS := R6.vcOS + userFr := utVc_userFrA Hτ R6.userFr + frTh := R6.frTh + bfSpec := J.bfSpec + curVcpuNone := Hτ.vcur.trans (KSp.vcur.trans J.curVcpuNone) + ipcSorted := IO.ipcSorted.of_q Hτ.qkeep + activeReplyScs := IO.activeReplyScs.of_q Hτ.qkeep + domOK := hdom + ipiZero := hipi + bufVs := hbv + j5 := j5 + j7 := SO.j7 } + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtAsmCapsA.lean b/proofs/Fl4ProofsA64/P3/UtAsmCapsA.lean new file mode 100644 index 000000000..5de2c45bf --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtAsmCapsA.lean @@ -0,0 +1,123 @@ +/- +P3 (aarch64, A64-P3 Untyped, full-record post, assembly): **the fields that follow from the cap split**: `utBlk`, +`zombieBnd`, `zombieTcb`, `zombieNoVcpu`, `capTy`, `devUtOff`, `idleAnchor`, `objAlloc`, from the record before the +creation (on the reset path: the record on the dropped sets). + +**`utRt_hothA`** discharges the field lemmas' `hoth` from the record (P1 `UtInvA.noov` / `excl`). What is left in `UtAsmCapsInA` is about the creation itself; the assembly supplies it. + +No named premise. New theorems only. +-/ +import Fl4ProofsA64.P3.UtFullAllocA +import Fl4ProofsA64.P3.UtFullCapTyA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4SpecA64 Classical + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z Z' : XSetsA} {H H' : Heap} {OS OS' U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s s' : S} {a : A} {σ : State Unit} {m : McState} + {x p sb fi nfi nt us aligned : U64} {win : Nat} + +/-- The creation's side of these fields. -/ +structure UtAsmCapsInA (st : machine.Store S) [L : LawfulStore st] (Z Z' : XSetsA) (H H' : Heap) (OS OS' : Set U64) + (s s' : S) (x p sb fi nfi nt us aligned : U64) (win : Nat) : Prop where + slots : UtSlotsA st OS OS' s s' x p sb nt us aligned win + W : UtWinZA nt aligned (zA nt.val us) win + src : x ∈ OS + cap : capA st s x = .Untyped p sb fi false + cap' : capA st s' x = .Untyped p sb nfi false + lo : p.val + fi.val ≤ aligned.val + hi : aligned.val + win * 2 ^ (zA nt.val us).val ≤ p.val + nfi.val + top : aligned.val + win * 2 ^ (zA nt.val us).val ≤ p.val + 2 ^ sb.val + /-- the decode creates at least one object. -/ + ne : 0 < win * 2 ^ (zA nt.val us).val + /-- the decode's size check on a new untyped. -/ + us39 : nt.val = 0 → us.val ≤ 39 + heap : ∀ q k, H q = some k → H' q = some k + tcbs : ∀ t ∈ H.tcbs, t ∈ H'.tcbs + vcpuW : ∀ t ∈ H.tcbs, L.mem s' (wa t 3664#u64) = L.mem s (wa t 3664#u64) + obj : ∀ q bytes, JointObjectA Z' H' q bytes → JointObjectA Z H q bytes ∨ + (nt.val ≠ 0 ∧ aligned.val ≤ q.val ∧ q.val + bytes ≤ aligned.val + win * 2 ^ (zA nt.val us).val) + nodes : ∀ lo hi, RegionOffNodes st Z.Cs s lo hi → RegionOffNodes st Z'.Cs s' lo hi + newKind : ∀ j < win, CapKindA H' (newCapA nt.val (bjA aligned (zA nt.val us) j) us false) + +/-- **Every other untyped is off the window, or holds the source's region below its watermark** (`UtInvA.noov` / +`excl`), with or without children of the source. `hne`: the window is not empty. -/ +theorem utRt_hothA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (hx : x ∈ OS) (hc : capA st s x = .Untyped p sb fi false) {lo W : Nat} (h1 : p.val + fi.val ≤ lo) + (h3 : lo + W ≤ p.val + 2 ^ sb.val) (hne : 0 < W) : + ∀ u ∈ OS, u ≠ x → ∀ q b f d, capA st s u = .Untyped q b f d → + (q.val + 2 ^ b.val ≤ lo ∨ lo + W ≤ q.val) ∨ (q.val ≤ p.val ∧ p.val + 2 ^ sb.val ≤ q.val + f.val) := by + intro u hu ne q b f d e + obtain ⟨G, par, hT⟩ := J.sys.sysl.sys.stz.1.1.tu + have hU := hT.ut + have R : ∀ z ∈ OS, ∀ p b i d, capA st s z = .Untyped p b i d → + baseA (capA st s z) = p.val ∧ topA (capA st s z) = p.val + 2 ^ b.val - 1 ∧ p.val + 2 ^ b.val ≤ 2 ^ 64 := + fun z hz p b i d h => by + rw [h] + exact ut_rangeA (capA_ut_b h) (by + have := hU.reg z hz (by rw [h]; exact physA_ut _ _ _ _); rwa [h] at this) + obtain ⟨xb, xt, -⟩ := R x hx p sb fi false hc + obtain ⟨ub, ut, -⟩ := R u hu q b f d e + have hb1 : 1 ≤ 2 ^ b.val := Nat.one_le_two_pow + have hs1 : 1 ≤ 2 ^ sb.val := Nat.one_le_two_pow + rcases hU.noov x hx p sb fi false hc u hu (by rw [e]; exact physA_ut _ _ _ _) with g | ⟨-, w⟩ + · unfold DisjRA at g + rw [ub, ut] at g + left; omega + · unfold WithinA at w + rw [xb, xt, ub, ut] at w + rcases hU.noov u hu q b f d e x hx (by rw [hc]; exact physA_ut _ _ _ _) with g | ⟨-, w'⟩ + · unfold DisjRA at g + rw [xb, xt] at g + right; omega + · unfold WithinA at w' + rw [xb, xt, ub, ut] at w' + have eq : q = p := UScalar.eq_of_val_eq (by omega) + have eb : b = sb := UScalar.eq_of_val_eq (Nat.pow_right_injective (le_refl 2) (by + show 2 ^ b.val = 2 ^ sb.val + omega)) + subst eq eb + rcases hU.excl x hx u hu (fun c => ne c.symm) _ _ fi f false d hc e with g | g + · omega + · right; omega + +/-- The cap-split fields of the grown record. -/ +structure UtAsmCapsOutA (st : machine.Store S) [L : LawfulStore st] (Z' : XSetsA) (H' : Heap) (OS' : Set U64) + (ia ie : Nat) (s' : S) : Prop where + utBlk : UtBlk39A st OS' s' + zombieBnd : UtZombieA st OS' s' + zombieTcb : ZombieTcbTA st H' OS' s' + zombieNoVcpu : ZombieTcbNoVcpuA st OS' s' + capTy : CapTyA st H' OS' s' + devUtOff : DevUtOffA st Z' H' OS' ia ie s' + idleAnchor : IdleAnchorOffA (st := st) OS' s' + objAlloc : ObjAllocA (st := st) Z' H' OS' s' + +theorem utRt_asmCapsA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (I : UtAsmCapsInA st Z Z' H H' OS OS' s s' x p sb fi nfi nt us aligned win) : + UtAsmCapsOutA st Z' H' OS' ia ie s' := by + have lop : p.val ≤ aligned.val := by have := I.lo; omega + exact + { utBlk := utRt_utBlkA J.utBlk I.slots I.src I.cap I.us39 + zombieBnd := utRt_zombieBndA J.zombieBnd I.slots + zombieTcb := utRt_zombieTcbA J.zombieTcb I.slots I.tcbs + zombieNoVcpu := utRt_zombieNoVcpuA J.zombieNoVcpu J.zombieTcb I.slots I.vcpuW + capTy := utRt_capTyA J.capTy I.slots I.heap I.newKind + devUtOff := utRt_devUtOffA J.devUtOff I.slots I.W I.src I.cap lop I.top + (fun q bytes h => (I.obj q bytes h).imp id fun g => g.2) I.nodes + idleAnchor := utRt_idleAnchorA J.idleAnchor I.slots I.W I.src I.cap lop I.top + objAlloc := utRt_objAllocA J.objAlloc I.slots I.W I.src I.cap I.cap' I.lo I.hi I.top I.obj + (utRt_hothA J I.src I.cap I.lo I.top I.ne) } + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtAsmHeapA.lean b/proofs/Fl4ProofsA64/P3/UtAsmHeapA.lean new file mode 100644 index 000000000..83e59070c --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtAsmHeapA.lean @@ -0,0 +1,176 @@ +/- +P3 (aarch64, A64-P3 Untyped, full-record post, assembly): **the table and heap fields**: `unmPt`, `tabS1`, `tabExt`, +`vsVc`, `ptH`, `setsInH`, `bufOff`, `deadExt`, `utIpc`, `scSize`, `frBase`, `devFr`, `thPos`, from the record before +the creation. What is left in `UtAsmHeapInA` is about the creation itself; the assembly supplies it. `vsOk` is not +here: it needs `J7A` and the VSpace typing after the creation (`utVsOk_of_J7A`). + +`frBase` and `devFr` use the record's `UtInvA` (`sys.sysl`), so nothing about it is left in the bundle. + +No named premise. New theorems only. +-/ +import Fl4ProofsA64.P3.UtAsmCapsA +import Fl4ProofsA64.P3.UtFullAllocA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z Z' : XSetsA} {H H' : Heap} {OS OS' U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp Nn En Rn : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack stack' : U64 → List U64} + {κ : U64 → U64} {n : Nat} {J5 J7 : JSlotA S} {s s' : S} {a : A} {σ σ' : State Unit} {m m' : McState} + {x p sb fi nfi nt us aligned : U64} {win : Nat} + +/-- The creation's side of these fields, beyond the cap split (`UtAsmCapsInA`). -/ +structure UtAsmHeapInA (st : machine.Store S) [L : LawfulStore st] (Z Z' : XSetsA) (H H' : Heap) (OS OS' : Set U64) + (Rp Nn En Rn : Set U64) (Bf : Set (U64 × Fl4SpecA64.Word)) (stack stack' : U64 → List U64) (s s' : S) + (σ σ' : State Unit) (m m' : McState) (x p sb fi nt us aligned : U64) (win : Nat) : Prop where + nt15 : nt.val < 15 + ksb : L.ksb s' = L.ksb s + cpu : L.cpu s' = L.cpu s + /-- the source stays, and every other non-null slot keeps its cap. -/ + src' : x ∈ OS' + kept : ∀ u ∈ OS, u ≠ x → capA st s u ≠ .Null → u ∈ OS' ∧ capA st s' u = capA st s u + /-- (12′) the old TCBs stay. -/ + tcbSub : ∀ t ∈ Z.T, t ∈ Z'.T + /- new IPC objects and SCs in the grown heap -/ + heapN : ∀ q ∈ Nn, H' q = some .ntfn + heapE : ∀ q ∈ En, H' q = some .ep + heapR : ∀ q ∈ Rn, H' q = some .reply + scName : ∀ c, scNameA m' c = scNameA m c + /-- (record edit #8b-D) the idle SCs are kept (for `scSize`'s non-idle equality). -/ + idleSc : ∀ c, m'.idleScOf c = m.idleScOf c + scSplit : ∀ c ∈ Z'.C, (c ∈ Z.C ∧ σ'.kheap (scNameA m' c) = σ.kheap (scNameA m c)) ∨ + ∃ u, H' c = some (.sc u) ∧ Fl4SpecA64.minSchedContextBits ≤ u ∧ + σ'.kheap (scNameA m' c) = + some (.schedContext Fl4SpecA64.defaultSchedContext (u - Fl4SpecA64.minSchedContextBits)) + thSplit : ∀ q z, Z'.th q = some (.other z) → Z.th q = some (.other z) ∨ 0 < z + /- the VSpace sets: new pages lie in new `other` objects -/ + ptNew : ∀ r ∈ Z'.Pp ∪ Z'.S1, r ∈ Z.Pp ∪ Z.S1 ∨ + ∃ q z, H' q = some (.other z) ∧ q.val ≤ r.val ∧ r.val + 4096 ≤ q.val + 2 ^ z + pagesNew : ∀ r ∈ Z'.Pp ∪ Z'.S1 ∪ Z'.T2 ∪ Z'.V, r ∈ Z.Pp ∪ Z.S1 ∪ Z.T2 ∪ Z.V ∨ + ∃ q z, H' q = some (.other z) ∧ q.val ≤ r.val ∧ r.val + 4096 ≤ q.val + 2 ^ z + rootsNew : ∀ r ∈ Z'.X ∪ Z'.Y, r ∈ Z.X ∪ Z.Y ∨ + ∃ q z, H' q = some (.other z) ∧ q.val ≤ r.val ∧ r.val + 8192 ≤ q.val + 2 ^ z + vsVcSplit : ∀ b, VsFootA Z'.Pp Z'.S1 Z'.Y (L.ksb s) b → VcW Z'.V b → + VsFootA Z.Pp Z.S1 Z.Y (L.ksb s) b ∧ VcW Z.V b + /- unmapped tables: old ones stay, new ones are unmapped -/ + unmT : ∀ u ∈ OS, ∀ q asid va, capA st s u = .PageTable q false asid va → capA st s' u = capA st s u → + UnmTabA st Z m s σ .normalPtT q → UnmTabA st Z' m' s' σ' .normalPtT q + unmP : ∀ u ∈ OS, ∀ q asid, capA st s u = .Pml4 q false asid → capA st s' u = capA st s u → + UnmTabA st Z m s σ .vsRootPtT q → UnmTabA st Z' m' s' σ' .vsRootPtT q + unmS : ∀ u ∈ OS, ∀ q asid va, capA st s u = .Ept q 0#u64 false asid va → capA st s' u = capA st s u → + UnmS2RootA st s σ q → UnmS2RootA st s' σ' q + unmH : ∀ u ∈ OS, ∀ q asid, capA st s u = .HighVSpace q false asid → capA st s' u = capA st s u → + UnmHiRootA st s σ q → UnmHiRootA st s' σ' q + newT : nt.val = 11 → ∀ j < win, UnmTabA st Z' m' s' σ' .normalPtT (bjA aligned (zA nt.val us) j) + newP : nt.val = 8 → ∀ j < win, UnmTabA st Z' m' s' σ' .vsRootPtT (bjA aligned (zA nt.val us) j) + newS : nt.val = 13 → ∀ j < win, UnmS2RootA st s' σ' (bjA aligned (zA nt.val us) j) + newH : nt.val = 14 → ∀ j < win, UnmHiRootA st s' σ' (bjA aligned (zA nt.val us) j) + /- the footprint split, for the Stage-1 tables and for the Stage-2 / window tables -/ + tabFoot : ∃ Nw, UtFootNewA { Z with S1 := ∅ } { Z' with S1 := ∅ } Nw ∧ + (∀ q ∈ Z'.S1, ∀ i : U64, i.val < 512 → ¬ Nw (peAF q i)) ∧ + ∀ q ∈ Z'.S1, q ∈ Z.S1 ∨ ∀ i : U64, i.val < 512 → ¬ FootA (L := L) { Z with S1 := ∅ } s (peAF q i) + tabObj : ∀ q ∈ Z'.S1, ∀ r, RelPA { Z' with S1 := ∅ } m' r → r ≠ specPtr q + extFoot : ∃ Nw, UtFootNewA (extZA Z) (extZA Z') Nw ∧ + (∀ q, (q ∈ S2PgE Z'.X Z'.T2 ∨ ∃ y ∈ Z'.Y, q = wa y 4096#u64) → ∀ i : U64, i.val < 512 → ¬ Nw (peAF q i)) ∧ + ∀ q, (q ∈ S2PgE Z'.X Z'.T2 ∨ ∃ y ∈ Z'.Y, q = wa y 4096#u64) → + (q ∈ S2PgE Z.X Z.T2 ∨ ∃ y ∈ Z.Y, q = wa y 4096#u64) ∨ + ∀ i : U64, i.val < 512 → ¬ FootA (L := L) (extZA Z) s (peAF q i) + extObj : ∀ q, (q ∈ Z'.X ∨ q ∈ Z'.Y ∨ q ∈ Z'.T2) → ¬ RelPA (extZA Z') m' (specPtr q) + /- IPC buffers lie off the creation -/ + bufOs : ∀ b, BufW Bf b → InOS OS' b → InOS OS b + bufVs : ∀ b, BufW Bf b → VsFootA Z'.Pp Z'.S1 Z'.Y (L.ksb s') b → VsFootA Z.Pp Z.S1 Z.Y (L.ksb s) b + bufPg : ∀ b, BufW Bf b → ∀ q, SchedPageA st Z' OS' s' q → ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → b = wa q o → + SchedPageA st Z OS s q + bufVc : ∀ b, BufW Bf b → ∀ v ∈ Z'.V, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → b = wa v o → v ∈ Z.V + /- dead objects -/ + deadV : ∀ v ∈ Z'.V, (v ∈ Z.V ∧ σ'.kheap (specPtr v) = σ.kheap (specPtr v)) ∨ + ∀ y, σ'.kheap (specPtr v) = some (.archObj (.vcpu y)) → y.vcpuTcb = none + deadR : ∀ r ∈ Rp ∪ Rn, ∀ c ∈ Z'.C, r ∈ stack' c → r ∈ Rp ∧ c ∈ Z.C ∧ r ∈ stack c + asid : σ'.archState.armAsidTable = σ.archState.armAsidTable + deadY : ∀ r ∈ Rp ∪ Rn, ∀ y, σ'.kheap (specPtr r) = some (.reply y) → + (r ∈ Rp ∧ σ.kheap (specPtr r) = some (.reply y)) ∨ y.replyTcb = none + deadT : ∀ t ∈ Z'.T, (t ∈ Z.T ∧ L.mem s' (wa t 3664#u64) = L.mem s (wa t 3664#u64)) ∨ + L.mem s' (wa t 3664#u64) = 0#u64 + deadW : ∀ v ∈ Z'.V, (v ∈ Z.V ∧ L.mem s' (wa v 24#u64) = L.mem s (wa v 24#u64)) ∨ L.mem s' (wa v 24#u64) = 0#u64 + /- frame caps: the new ones lie in the source's free part -/ + frSplit : ∃ New : U64 → Prop, + (∀ y ∈ OS', isFrA (capA st s' y) = true ∨ RngKeyA (capA st s' y) → + (∃ y0 ∈ OS, capA st s y0 = capA st s' y) ∨ + (New y ∧ p.val + fi.val ≤ (ptrA (capA st s' y)).val ∧ (ptrA (capA st s' y)).val < p.val + 2 ^ sb.val)) ∧ + ∀ y z, New y → New z → isFrA (capA st s' y) = true → ¬ RngKeyA (capA st s' z) + devSplit : ∀ y ∈ OS', ∀ q size rights mapped ept asid va, + capA st s' y = .Frame q size rights true mapped ept asid va → + (∃ y0 ∈ OS, capA st s y0 = capA st s' y) ∨ + ∀ k, H' q = some k → (∃ bits, k = .other bits) ∧ ∃ dev pg, σ'.kheap (specPtr q) = some (.archObj (.dataPage dev pg)) + devKeep : ∀ q : U64, ¬ (p.val + fi.val ≤ q.val ∧ q.val < p.val + 2 ^ sb.val) → + H' q = H q ∧ ∀ dev pg, σ.kheap (specPtr q) = some (.archObj (.dataPage dev pg)) → + σ'.kheap (specPtr q) = some (.archObj (.dataPage dev pg)) + +/-- The table and heap fields of the grown record. -/ +structure UtAsmHeapOutA (st : machine.Store S) [L : LawfulStore st] (Z' : XSetsA) (H' : Heap) (OS' : Set U64) + (N E Rp : Set U64) (Bf : Set (U64 × Fl4SpecA64.Word)) (stack' : U64 → List U64) (s' : S) (σ' : State Unit) + (m' : McState) : Prop where + unmPt : UnmPtSpecA st Z' m' OS' s' σ' + tabS1 : ∀ q ∈ Z'.S1, TabOffA (L := L) Z' m' s' q + tabExt : ExtTabOffA (L := L) Z' m' s' + vsVc : ∀ b, VsFootA Z'.Pp Z'.S1 Z'.Y (L.ksb s') b → ¬ VcW Z'.V b + ptH : PtHA H' (Z'.Pp ∪ Z'.S1) + setsInH : SetsInHA Z' H' + bufOff : BufOffA st Z' OS' s' Bf + deadExt : DeadExtA st Z' OS' Rp stack' s' σ' + utIpc : UtIpcHeapA H' N E Rp + scSize : ∀ c ∈ Z'.C, ∀ y k, σ'.kheap (scNameA m' c) = some (.schedContext y k) → + k + Fl4SpecA64.minSchedContextBits ≤ H'.scBits c ∧ + (NotIdleScA m' (scNameA m' c) → k + Fl4SpecA64.minSchedContextBits = H'.scBits c) + frBase : FrBaseApartA st OS' s' + devFr : DevFrDisjA st H' OS' s' σ' + thPos : ∀ q z, Z'.th q = some (.other z) → 0 < z + +theorem utRt_asmHeapA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (I : UtAsmCapsInA st Z Z' H H' OS OS' s s' x p sb fi nfi nt us aligned win) + (K : UtAsmHeapInA st Z Z' H H' OS OS' Rp Nn En Rn Bf stack stack' s s' σ σ' m m' x p sb fi nt us aligned win) : + UtAsmHeapOutA st Z' H' OS' (N ∪ Nn) (E ∪ En) (Rp ∪ Rn) Bf stack' s' σ' m' := by + obtain ⟨G, par, hT⟩ := J.sys.sysl.sys.stz.1.1.tu + have hU := hT.ut + have mono : ∀ k q, DeadA st OS' k s' q → DeadA st OS k s q := fun _ _ h => + utRt_deadMonoA I.cap I.cap' K.src' K.kept h + obtain ⟨Nw, F, hN, hold⟩ := K.tabFoot + obtain ⟨Ne, Fe, hNe, holde⟩ := K.extFoot + obtain ⟨New, hsplit, huni⟩ := K.frSplit + exact + { unmPt := utRt_unmPtA J.unmPt I.slots K.nt15 K.unmT K.unmP K.unmS K.unmH K.newT K.newP K.newS K.newH + tabS1 := fun q hq => utRt_tabOffA F K.ksb K.cpu + (fun i hi => (hold q hq).elim (fun o => (J.tabS1 q o).mem i hi) fun g => g i hi) (hN q hq) (K.tabObj q hq) + tabExt := utRt_extTabOffA Fe K.ksb K.cpu + (fun q hq i hi => (holde q hq).elim (fun o => J.tabExt.mem q o i hi) fun g => g i hi) hNe K.extObj + vsVc := utRt_vsVcA J.vsVc K.ksb K.vsVcSplit + ptH := utRt_ptHA J.ptH I.heap K.ptNew + setsInH := utRt_setsInHA J.setsInH I.heap K.pagesNew K.rootsNew + bufOff := utRt_bufOffA J.bufOff K.bufOs K.bufVs K.bufPg K.bufVc fun bp hbp => + (J.bufOff.pin bp hbp).keepF fun t ht hO _ _ r x1 x2 x3 x4 e => by + obtain ⟨h1, h2⟩ := K.kept _ hO (fun e' => by rw [e', I.cap] at e; cases e) (by rw [e]; exact fun h => by cases h) + exact ⟨K.tcbSub t ht, h1, r, x1, x2, x3, x4, h2.trans e⟩ + deadExt := utRt_deadExtA J.deadExt mono K.deadV K.deadR K.asid K.deadT K.deadW K.deadY + utIpc := utRt_ipcHeapA J.utIpc I.heap (fun q h => h.imp id (K.heapN q)) (fun q h => h.imp id (K.heapE q)) + (fun q h => h.imp id (K.heapR q)) + scSize := utRt_scSizeA (C := Z.C) (m := m') (σ := σ) (H := H) + (fun c hc x n hk => by + rw [K.scName c] at hk ⊢ + obtain ⟨h1, h2⟩ := J.scSize c hc x n hk + exact ⟨h1, fun hn => h2 ⟨hn.1, fun i hi => by rw [← K.idleSc i]; exact hn.2 i hi⟩⟩) + (fun c hc => J.toJEntA.scs.symm ▸ hc) I.heap + (fun c hc => (K.scSplit c hc).imp (fun g => ⟨g.1, by rw [g.2, K.scName c]⟩) id) + frBase := utRt_frBaseA J.frBase hU I.src I.cap New hsplit huni + devFr := utRt_devFrDisjA J.devFr hU I.src I.cap K.devSplit K.devKeep + thPos := fun q z h => (K.thSplit q z h).elim (J.thPos q z) id } + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtAsmIpcA.lean b/proofs/Fl4ProofsA64/P3/UtAsmIpcA.lean new file mode 100644 index 000000000..5943aa8cd --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtAsmIpcA.lean @@ -0,0 +1,224 @@ +/- +P3 (aarch64, A64-P3 Untyped, full-record post, assembly): **the IPC, buffer and linkage fields**: `ep`, `ntfn`, +`reply`, `bufCover`, `bufNative`, `bufAl`, `iplaced`, `ipcPlace`, `ipcTh`, `ipcHeapCov`, `scCov`, `idleNoFault`, +`mcp`, `yieldRel`, `sinv`, from the record before the creation. `UtAsmIpcInA` is the creation's side: the new IPC +objects, TCBs and SCs (`Nn`, `En`, `Rn`, `Tn`, `Cn`), the spec outcome and the kept words. + +No named premise. New theorems only. +-/ +import Fl4ProofsA64.P3.UtFullPlaceA +import Fl4ProofsA64.P3.Stage3A + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H H' : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s s' : S} {a : A} {σ σ' : State Unit} {m m' : McState} + +/-- The creation's side of these fields. -/ +structure UtAsmIpcInA (st : machine.Store S) [L : LawfulStore st] (Z : XSetsA) (th' : TypedHeapA) (H H' : Heap) + (N E Rp : Set U64) (Bf : Set (U64 × Word)) (Nn En Rn Tn Cn : Set U64) (Q : U64 → Prop) (s s' : S) + (σ σ' : State Unit) (m m' : McState) (P : ObjRef → Prop) (d : KernelObject) : Prop where + spec : UtNewObjA σ σ' P d + grow : UtIpcGrowA Z.th th' N E Rp Z.T Nn En Rn Tn Q + place : PlaceA th' (L.ksb s) + footE : Endp.FootEq Z.T E Rp Bf (L.mem s) (L.mem s') + footN : Notif.FootEq N Z.T (L.mem s) (L.mem s') + newN : ∀ r ∈ Nn, σ'.kheap (specPtr r) = some (.notification defaultNotification) ∧ + L.mem s' (epHeadA r) = 0#u64 ∧ L.mem s' (epTSA r) = 0#u64 ∧ L.mem s' (nBoundA r) = 0#u64 ∧ + L.mem s' (nScA r) = 0#u64 + newE : ∀ e ∈ En, σ'.kheap (specPtr e) = some (.endpoint defaultEp) ∧ L.mem s' (epHeadA e) = 0#u64 ∧ + L.mem s' (epTSA e) = 0#u64 + newR : ∀ r ∈ Rn, σ'.kheap (specPtr r) = some (.reply defaultReply) ∧ L.mem s' (Endp.rTcbA r) = 0#u64 ∧ + L.mem s' (rNextA r) = 0#u64 ∧ L.mem s' (rPrevA r) = 0#u64 + newT : ∀ t ∈ Tn, ∃ y, σ'.kheap (specPtr t) = some (.tcb y) ∧ TcbRelE (Rp ∪ Rn) (L.mem s') t y ∧ + TcbRel1 (L.mem s') t y ∧ y.tcbMcpriority = 0 ∧ L.mem s' (mcpA t) = 0#u64 ∧ y.tcbYieldTo = none ∧ + L.mem s' (yieldA t) = 0#u64 ∧ y.tcbIpcframe = .nullCap ∧ y.tcbIpcBuffer = 0 ∧ + L.mem s' (flagsA t) = 0#u64 ∧ y.tcbFlags = ∅ + newTslot : ∀ t ∈ Tn, capAtA (L.mem s') (slotAddrA (specPtr t) (BitVec.ofNat 64 2)) = .Null + newC : ∀ c ∈ Cn, ∃ k, σ'.kheap (scNameA m c) = some (.schedContext defaultSchedContext k) + newRfresh : ∀ r ∈ Rn, r ∉ Rp + newCfresh : ∀ c ∈ Cn, c ∉ Z.C + /-- old reply and old words. -/ + oldReply : ∀ r ∈ Rp, ∃ x, σ.kheap (specPtr r) = some (.reply x) + link : ∀ r ∈ Rp, L.mem s' (rNextA r) = L.mem s (rNextA r) ∧ L.mem s' (rPrevA r) = L.mem s (rPrevA r) + mcpW : ∀ t ∈ Z.T, L.mem s' (mcpA t) = L.mem s (mcpA t) ∧ L.mem s' (flagsA t) = L.mem s (flagsA t) + yieldW : ∀ t ∈ Z.T, L.mem s' (yieldA t) = L.mem s (yieldA t) + bufW : ∀ t ∈ Z.T, L.mem s' (wa t 3512#u64) = L.mem s (wa t 3512#u64) ∧ capA st s' (sbWordA t) = capA st s (sbWordA t) + bufSlot : ∀ t ∈ Z.T, capAtA (L.mem s') (slotAddrA (specPtr t) (BitVec.ofNat 64 2)) = + capAtA (L.mem s) (slotAddrA (specPtr t) (BitVec.ofNat 64 2)) + scOld : ∀ c ∈ Z.C, ∃ y k, σ.kheap (scNameA m c) = some (.schedContext y k) + optSc : ∀ w, optScA m' w = optScA m w + scName : ∀ c, scNameA m' c = scNameA m c + curCore : m'.curCore = m.curCore + idleThreadOf : m'.idleThreadOf = m.idleThreadOf + idleScOf : m'.idleScOf = m.idleScOf + affinity : ∀ t c, m'.tcbAffinity t = some c → m.tcbAffinity t = some c ∨ c = 0 + newTcbState : ∀ x, d = .tcb x → x.tcbState ≠ .idleThreadState + /-- (record edit #8b-D (b)) a new TCB has no fault. -/ + newTcbFault : ∀ x, d = .tcb x → x.tcbFault = none + newScInactive : ∀ y k, d = .schedContext y k → Fl4SpecA64.scActive y = false + newScNoRep : ∀ y k, d = .schedContext y k → y.scReplies = [] + newScName : ∀ p, P p → ∀ x k, d = .schedContext x k → ∃ c ∈ Cn, p = scNameA m c + irq : SpecIrqA σ' + heapN : ∀ q, H' q = some .ntfn → H q = some .ntfn ∨ q ∈ Nn + heapE : ∀ q, H' q = some .ep → H q = some .ep ∨ q ∈ En + heapR : ∀ q, H' q = some .reply → H q = some .reply ∨ q ∈ Rn + +/-- The IPC, buffer and linkage fields of the grown record. -/ +structure UtAsmIpcOutA (st : machine.Store S) [L : LawfulStore st] (T C : Set U64) (th' : TypedHeapA) (H' : Heap) + (N E Rp : Set U64) (Bf : Set (U64 × Word)) (stack : U64 → List U64) (n : Nat) (s' : S) (σ' : State Unit) + (m' : McState) : Prop where + ep : EpStateRel T E Rp Bf (L.mem s') σ' + ntfn : NtfnStateRel N T (L.mem s') σ' + reply : ReplyStackRelA (L.mem s') Rp C σ' (scNameA m') stack + bufCover : BufCover st T Bf s' σ' + bufNative : BufNativeA T (L.mem s') + bufAl : UtBufAlA T σ' + iplaced : IPlacedA N T E Rp Bf + ipcPlace : IpcSchedPlacementA th' N E Rp Bf + ipcTh : IpcThA th' N E Rp + ipcHeapCov : IpcHeapCovA H' N E Rp + scCov : ScCovA C m' σ' + idleNoFault : IdleNoFaultA m' σ' + mcp : McpRelA T m' (L.mem s') σ' + yieldRel : YieldRelA T C m' (L.mem s') σ' + sinv : SInvXA σ' m' n + ipcSorted : IpcSortedA (specPtr '' E) (specPtr '' N) σ' + activeReplyScs : ActRepA σ' + +/-- **`ipcSorted` across a creation**: the old queues hold old threads, the new endpoints and notifications are +idle. -/ +theorem utRt_ipcSortedA {T : Set U64} (O : UtNewObjA σ σ' P d) (h : IpcSortedA (specPtr '' E) (specPtr '' N) σ) + (He : EpStateRel T E Rp Bf (L.mem s) σ) (Hn : NtfnStateRel N T (L.mem s) σ) {En Nn : Set U64} + (nE : ∀ e ∈ En, σ'.kheap (specPtr e) = some (.endpoint defaultEp)) + (nN : ∀ r ∈ Nn, σ'.kheap (specPtr r) = some (.notification defaultNotification)) : + IpcSortedA (specPtr '' (E ∪ En)) (specPtr '' (N ∪ Nn)) σ' := by + have mono : ∀ q : List ObjRef, (∀ a ∈ q, ∃ t ∈ T, a = specPtr t) → PrioSortedA σ q → PrioSortedA σ' q := by + intro q hm hq + refine hq.imp_of_mem fun {a b} ha hb hab x y hx hy => ?_ + have old : ∀ c ∈ q, ∀ w, σ'.kheap c = some (.tcb w) → σ.kheap c = some (.tcb w) := fun c hc w hw => by + obtain ⟨t, ht, rfl⟩ := hm c hc + obtain ⟨z, hz, -⟩ := He.tcb t ht + rw [(O.keep _ _ hz (fun _ _ e => by cases e)).symm.trans hw] at hz + exact hz + exact hab x y (old a ha x hx) (old b hb y hy) + have mm : ∀ xs : List U64, (∀ t ∈ xs, t ∈ T) → ∀ a ∈ xs.map specPtr, ∃ t ∈ T, a = specPtr t := fun xs h a ha => by + obtain ⟨t, ht, rfl⟩ := List.mem_map.1 ha + exact ⟨t, h t ht, rfl⟩ + refine ⟨?_, ?_⟩ + · rintro p ⟨e, he | he, rfl⟩ x' hx' + · obtain ⟨x, hx, rel⟩ := He.ep e he + have := (O.keep _ _ hx (fun _ _ e => by cases e)).symm.trans hx' + cases this + refine mono _ ?_ (h.1 _ ⟨e, he, rfl⟩ _ hx) + cases x' with + | idleEP => exact fun _ ha => absurd ha (by simp [Endpoint.epQueue]) + | sendEP q => + obtain ⟨-, xs, -, rfl, hq, -⟩ := rel + exact mm xs hq.mem + | recvEP q => + obtain ⟨-, xs, -, rfl, hq, -⟩ := rel + exact mm xs hq.mem + · rw [nE e he] at hx' + cases hx' + exact List.Pairwise.nil + · rintro p ⟨r, hr | hr, rfl⟩ x' hx' + · obtain ⟨x, hx, rel⟩ := Hn.ntfn r hr + have := (O.keep _ _ hx (fun _ _ e => by cases e)).symm.trans hx' + cases this + refine mono _ ?_ (h.2 _ ⟨r, hr, rfl⟩ _ hx) + have o := rel.obj + cases ho : x'.ntfnObj with + | idleNtfn => exact fun _ ha => absurd ha (by simp [Ntfn.ntfnQueue]) + | activeNtfn b => exact fun _ ha => absurd ha (by simp [Ntfn.ntfnQueue]) + | waitingNtfn q => + rw [ho] at o + obtain ⟨-, xs, -, rfl, hq, -⟩ := o + exact mm xs hq.mem + · rw [nN r hr] at hx' + cases hx' + exact List.Pairwise.nil + +theorem utRt_asmIpcA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {th' : TypedHeapA} {Nn En Rn Tn Cn : Set U64} {Q : U64 → Prop} {P : ObjRef → Prop} {d : KernelObject} + (K : UtAsmIpcInA st Z th' H H' N E Rp Bf Nn En Rn Tn Cn Q s s' σ σ' m m' P d) : + UtAsmIpcOutA st (Z.T ∪ Tn) (Z.C ∪ Cn) th' H' (N ∪ Nn) (E ∪ En) (Rp ∪ Rn) Bf + (fun c => if c ∈ Cn then [] else stack c) n s' σ' m' := by + have O := K.spec + have kt : ∀ p x, σ.kheap p = some (.tcb x) → σ'.kheap p = some (.tcb x) := fun p x g => + O.keep p _ g (fun _ _ e => by cases e) + have ks : ∀ p y k, σ.kheap p = some (.schedContext y k) → σ'.kheap p = some (.schedContext y k) := fun p y k g => + O.keep p _ g (fun _ _ e => by cases e) + have kr : ∀ p x, σ.kheap p = some (.reply x) → σ'.kheap p = some (.reply x) := fun p x g => + O.keep p _ g (fun _ _ e => by cases e) + have tb : ∀ t ∈ Z.T, ∀ x, σ'.kheap (specPtr t) = some (.tcb x) → σ.kheap (specPtr t) = some (.tcb x) := by + intro t ht x g + obtain ⟨y, e, -⟩ := J.j.1.sch.tcb t ht + have := (kt _ _ e).symm.trans g + cases this + exact e + have hname : scNameA m' = scNameA m := funext K.scName + have yR : YieldRelA (Z.T ∪ Tn) (Z.C ∪ Cn) m' (L.mem s') σ' := + utRt_yieldA J.yieldRel K.optSc K.scName K.yieldW kt ks K.scOld + (fun t ht => by + obtain ⟨y, g, -, -, -, -, a1, a2, -⟩ := K.newT t ht + exact ⟨y, g, a1, a2⟩) K.newC + have rep : ReplyStackRelA (L.mem s') (Rp ∪ Rn) (Z.C ∪ Cn) σ' (scNameA m) + (fun c => if c ∈ Cn then [] else stack c) := + utRt_replyStackA J.reply K.link (fun c _ x k g => ks _ _ _ g) (fun r _ x g => kr _ _ g) K.newRfresh K.newCfresh + K.oldReply (fun r hr => by + obtain ⟨a, -, b, c⟩ := K.newR r hr + exact ⟨a, b, c⟩) K.newC + exact + { ep := utRt_epStateA J.ep J.iplaced.e K.footE O.epView K.newE + (fun r hr => ⟨(K.newR r hr).1, (K.newR r hr).2.1⟩) + (fun t ht => by + obtain ⟨y, g, a, -⟩ := K.newT t ht + exact ⟨y, g, a⟩) + ntfn := utRt_ntfnStateA J.ntfn J.iplaced.n K.footN O.ntfnView K.newN + (fun t ht => by + obtain ⟨y, g, -, a, -⟩ := K.newT t ht + exact ⟨y, g, a⟩) + reply := by rw [hname]; exact rep + bufCover := utRt_bufCoverA J.bufCover tb (fun t ht => (K.bufW t ht).1) (fun t ht => (K.bufW t ht).2) + (fun t ht => by + obtain ⟨y, g, -, -, -, -, -, -, a, -⟩ := K.newT t ht + exact ⟨y, g, a⟩) + bufNative := utRt_bufNativeA J.bufNative K.bufSlot K.newTslot + bufAl := utRt_bufAlA J.bufAl tb (fun t ht => by + obtain ⟨y, g, -, -, -, -, -, -, -, a, -⟩ := K.newT t ht + exact ⟨y, g, a⟩) + iplaced := utRt_iplacedA K.grow J.ipcPlace K.place J.iplaced + ipcPlace := utRt_ipcPlaceA K.grow J.ipcPlace + ipcTh := utRt_ipcThA K.grow J.ipcPlace + ipcHeapCov := utRt_ipcHeapCovA J.ipcHeapCov K.heapN K.heapE K.heapR + scCov := utRt_scCovA J.scCov O K.scName K.newScName + idleNoFault := utRt_idleNoFaultA J.idleNoFault (fun c => by rw [K.idleThreadOf]) kt + mcp := utRt_mcpA J.mcp K.idleThreadOf K.mcpW O.epView kt (fun t ht => by + obtain ⟨y, g, -, -, a, b, -, -, -, -, c, d⟩ := K.newT t ht + exact ⟨y, g, a, b, c, d⟩) + yieldRel := yR + sinv := utRt_sinvA J.sinv O K.curCore K.idleThreadOf K.idleScOf K.affinity K.newTcbState K.newScInactive K.newTcbFault K.irq + ipcSorted := utRt_ipcSortedA O J.ipcSorted J.ep J.ntfn (fun e he => (K.newE e he).1) + (fun r hr => (K.newN r hr).1) + activeReplyScs := fun p y k h hr => by + rcases O.back p _ h (fun _ _ e => by cases e) with g | ⟨-, e⟩ + · exact J.activeReplyScs p y k g hr + · exact absurd (K.newScNoRep y k e.symm) hr } + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtAsmSchedA.lean b/proofs/Fl4ProofsA64/P3/UtAsmSchedA.lean new file mode 100644 index 000000000..a3be9f0b1 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtAsmSchedA.lean @@ -0,0 +1,246 @@ +/- +P3 (aarch64, A64-P3 Untyped, full-record post, assembly): **the scheduler record after a creation**: `sr`, `scCpu`, +`scNZ`, `vcRun`, `vcSym1`, `vcSym2`, `runVcpu`, `blkQ`, `sidle`, from the record before it. What is left in `UtAsmSchedInA` is about the creation itself; +the assembly supplies it. + +No named premise. New theorems only. +-/ +import Fl4ProofsA64.P3.UtFullSchedA +import Fl4ProofsA64.P3.Stage3A + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +set_option maxRecDepth 100000 + +/-- **F3–F6 after a creation**: new objects are no idle objects, a new SC is bound to no thread, a new spec cap is +no cap to an idle thread or to the idle SC. -/ +theorem utRt_sidleA {σ σ' : State Unit} {m m' : McState} {n : Nat} {P : ObjRef → Prop} {d : KernelObject} + (h : SIdleA σ m n) (O : UtNewObjA σ σ' P d) (hidt : m'.idleThreadOf = m.idleThreadOf) + (hid : σ'.idleThread = σ.idleThread) (hP : ¬ P idleScPtr) + (hds : ∀ y k, d = .schedContext y k → y.scTcb = none) + (hcap : ∀ sl, (∃ sl0, specCapAtA σ' sl = specCapAtA σ sl0) ∨ ∀ a, specCapAtA σ' sl = some a → NoIdleCapV m n a) : + SIdleA σ' m' n := by + have vt : ∀ c < n, vtCapA σ' (m'.idleThreadOf c) = some .nullCap ∧ vcpuOfA σ' (m'.idleThreadOf c) = none := by + intro c hc + obtain ⟨x, hx, -⟩ := h.f3 c hc + rw [hidt] + exact idleVtSameA (by rw [hx]; exact O.keep _ _ hx (fun _ _ e => by cases e)) (h.idleSlot c hc) (h.idleBound c hc) + refine ⟨fun c hc => ?_, fun y k hy => ?_, fun p y k hy he => ?_, fun sl => ?_, fun y k hy => ?_, + fun c hc => (vt c hc).1, fun c hc => (vt c hc).2⟩ + rotate_left 4 + · rw [O.glob.time] + rcases O.back _ _ hy (fun _ _ e => by cases e) with g | ⟨g, -⟩ + · exact h.f7 y k g + · exact absurd g hP + · obtain ⟨x, hx, hs⟩ := h.f3 c hc + exact ⟨x, by rw [hidt]; exact O.keep _ _ hx (fun _ _ e => by cases e), hs⟩ + · rcases O.back _ _ hy (fun _ _ e => by cases e) with g | ⟨g, -⟩ + · exact h.f4 y k g + · exact absurd g hP + · rcases O.back _ _ hy (fun _ _ e => by cases e) with g | ⟨-, e⟩ + · exact h.f5 p y k g (by rw [← hid]; exact he) + · rw [hds y k e.symm] at he + cases he + · rcases hcap sl with ⟨sl0, e⟩ | g + · rw [e, hidt] + exact h.f6 sl0 + · rw [hidt] + exact ⟨fun c hc e => (g _ e).1 c hc rfl, fun b e => (g _ e).2.1 b rfl, fun a e => (g _ e).2.2⟩ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z Z' : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s s' : S} {a : A} {σ σ' : State Unit} {m m' : McState} + {Tn Cn En Nn : Set U64} {P : ObjRef → Prop} {d : KernelObject} + +/-- The creation's side of these fields. -/ +structure UtAsmSchedInA (st : machine.Store S) [L : LawfulStore st] (Z Z' : XSetsA) (s s' : S) (σ σ' : State Unit) + (m m' : McState) (Tn Cn : Set U64) (P : ObjRef → Prop) (d : KernelObject) (n : Nat) : Prop where + spec : UtNewObjA σ σ' P d + eT : Z'.T = Z.T ∪ Tn + eC : Z'.C = Z.C ∪ Cn + eCs : Z'.Cs = Z.Cs + eQ : Z'.Q = Z.Q + eR : Z'.R = Z.R + subV : Z.V ⊆ Z'.V + hks : L.ksb s' = L.ksb s + hcpu : L.cpu s' = L.cpu s + hids : m'.idleScOf = m.idleScOf + hidt : m'.idleThreadOf = m.idleThreadOf + hcc : m'.curCore = m.curCore + sch : SchedRelA (L := L) Z'.T Z'.C Z'.Cs Z'.curOf Z'.curScOf s' σ' m' + rest : SchedRestA (L := L) Z' s' σ' m' + thT : ∀ t, Z'.th t = some .tcb ↔ t ∈ Z'.T + place : PlaceA Z'.th (L.ksb s') + /- kept words -/ + hm : ∀ w, Aligned w → SReadA st Z m s σ w → w ≠ nwA st s → L.mem s' w = L.mem s w + hnode : ∀ c ∈ Z.Cs, ∀ o : U64, o.val < 4288 → o.val % 8 = 0 → o.val ≠ 88 → + L.mem s' (L.nodeAt s c o) = L.mem s (L.nodeAt s c o) + /- new TCBs and SCs -/ + hTd : ∀ t ∈ Tn, t ∉ Z.T + hTa : ∀ t ∈ Tn, TcbAddr t + hTnode : ∀ c ∈ Z.Cs, ∀ t ∈ Tn, t.val + 2 ^ 12 ≤ L.nodeBase s' c ∨ L.nodeBase s' c + 4288 ≤ t.val + hTw : ∀ t ∈ Tn, L.mem s' (affA t) = 0#u64 ∧ L.mem s' (prioA t) = 0#u64 ∧ L.mem s' (inqA t) = 0#u64 + newSc : UtNewScA st Z'.th Z.C Cn m s s' σ σ' + hdt : ∀ x, d = .tcb x → x.tcbSchedContext = none ∧ x.tcbState = .inactive ∧ x.tcbFault = none + hds : ∀ y k, d = .schedContext y k → scActive y = false ∧ y.scTcb = none + hdn : ∀ x, d = .notification x → x.ntfnSc = none + /-- a new SC is at a nonzero address. -/ + scNe : ∀ c ∈ Cn, c ≠ 0#u64 + /- the VCPU binding words: old ones are kept, new ones are zero -/ + tvk : ∀ t ∈ Z.T, L.mem s' (wa t 3664#u64) = L.mem s (wa t 3664#u64) + tvn : ∀ t ∈ Tn, L.mem s' (wa t 3664#u64) = 0#u64 + vcw : ∀ v ∈ Z'.V, (v ∈ Z.V ∧ L.mem s' (wa v 24#u64) = L.mem s (wa v 24#u64)) ∨ L.mem s' (wa v 24#u64) = 0#u64 + vcw0 : ∀ v ∈ Z'.V, v ∈ Z.V ∨ L.mem s' v = 0#u64 + /- the idle objects and the new spec caps -/ + idle : σ'.idleThread = σ.idleThread + idleSc : ¬ P idleScPtr + newCap : ∀ sl, (∃ sl0, specCapAtA σ' sl = specCapAtA σ sl0) ∨ ∀ c, specCapAtA σ' sl = some c → NoIdleCapV m n c + /- the other binding words and the block words: old ones are kept, a new TCB's `RUN_VCPU` is zero -/ + vk : ∀ v ∈ Z.V, L.mem s' v = L.mem s v + runk : ∀ t ∈ Z.T, L.mem s' (runVcpuA t) = L.mem s (runVcpuA t) + runn : ∀ t ∈ Tn, L.mem s' (runVcpuA t) = 0#u64 + stk : ∀ t ∈ Z.T, L.mem s' (stA t) = L.mem s (stA t) + tbk : ∀ t ∈ Z.T, L.mem s' (Notif.tBlockA t) = L.mem s (Notif.tBlockA t) + newT : ∀ t ∈ Tn, P (specPtr t) + +/-- The scheduler fields of the grown record. -/ +structure UtAsmSchedOutA (st : machine.Store S) [L : LawfulStore st] (Z' : XSetsA) (κ' : U64 → U64) (s' : S) + (σ' : State Unit) (m' : McState) (n : Nat) : Prop where + sr : SchedSRA st Z' m' κ' s' σ' + scCpu : ∀ c ∈ Z'.C, (κ' c).val < Fl4SpecA64.fl4A64Platform.numNodes + scNZ : ∀ c ∈ Z'.C, c ≠ 0#u64 + vcRun : VcRunA st Z'.T Z'.V s' + sidle : SIdleA σ' m' n + vcSym1 : VcSym1A st Z'.T s' + vcSym2 : VcSym2A st Z'.V s' + runVcpu : RunVcpuInv st Z'.T s' + +theorem utRt_asmSchedA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (K : UtAsmSchedInA st Z Z' s s' σ σ' m m' Tn Cn P d n) : + UtAsmSchedOutA st Z' (fun c => if c ∈ Cn then 0#u64 else κ c) s' σ' m' n ∧ + BlkQueuedA Z'.T (E ∪ En) (N ∪ Nn) (L.mem s') σ' := by + refine ⟨?_, ?_, ?_⟩ + rotate_left + · intro t ht x hx e hb + rw [K.eT] at ht + rcases ht with old | new + · obtain ⟨y0, hy0, -⟩ := J.ep.tcb t old + have back : σ.kheap (specPtr t) = some (.tcb x) := by + rcases K.spec.back _ _ hx (fun _ _ g => by cases g) with g | ⟨g, -⟩ + · exact g + · rw [K.spec.fresh _ g] at hy0; cases hy0 + obtain ⟨a0, y, hy, mem⟩ := J.blkQ.ep t old x back e hb + exact ⟨by rw [K.tbk t old]; exact Or.inl a0, y, K.spec.keep _ _ hy (fun _ _ g => by cases g), mem⟩ + · have g := K.spec.new _ (K.newT t new) + rw [hx] at g + have st := (K.hdt x (Option.some.inj g).symm).2.1 + rw [st] at hb + simp [Fl4SpecA64.epBlocked] at hb + · intro t ht x hx w hb + rw [K.eT] at ht + rcases ht with old | new + · obtain ⟨y0, hy0, -⟩ := J.ntfn.tcb t old + have back : σ.kheap (specPtr t) = some (.tcb x) := by + rcases K.spec.back _ _ hx (fun _ _ g => by cases g) with g | ⟨g, -⟩ + · exact g + · rw [K.spec.fresh _ g] at hy0; cases hy0 + obtain ⟨a0, y, l, hy, ho, mem⟩ := J.blkQ.ntfn t old x back w hb + exact ⟨by rw [K.tbk t old]; exact Or.inl a0, y, l, K.spec.keep _ _ hy (fun _ _ g => by cases g), ho, mem⟩ + · have g := K.spec.new _ (K.newT t new) + rw [hx] at g + have st := (K.hdt x (Option.some.inj g).symm).2.1 + rw [st] at hb + cases hb + refine + { sr := ?sr, scCpu := ?scCpu, scNZ := ?scNZ, vcRun := ?vcRun, sidle := ?sidle, vcSym1 := ?s1, vcSym2 := ?s2, + runVcpu := ?rv } + case s1 => + intro t ht h0 + rw [K.eT] at ht + rcases ht with old | new + · rw [K.tvk t old] at h0 ⊢ + rcases J.vcRun.tcb t old with e | mem + · exact absurd e h0 + · rw [K.vk _ mem] + exact J.vcSym1 t old h0 + · exact absurd (K.tvn t new) h0 + case s2 => + intro v hv h0 + rcases K.vcw v hv with ⟨old, e⟩ | z + · rw [e] at h0 ⊢ + rcases J.vcRun.vcpu v old with z | mem + · exact absurd z h0 + · rw [K.runk _ mem] + exact J.vcSym2 v old h0 + · exact absurd z h0 + case rv => + intro t ht h + rw [K.eT] at ht + rcases ht with old | new + · rw [K.runk t old] at h + rw [K.stk t old] + exact J.runVcpu t old h + · exact absurd (K.runn t new) h + case sr => + exact utRt_fullSchedSRA J.sr K.spec K.eT K.eC K.eCs K.eQ K.eR K.hks K.hcpu K.hids K.hidt K.hcc K.sch K.rest K.thT + K.place K.hm K.hnode K.hTd K.hTa K.hTnode K.hTw K.newSc K.hdt (fun y k e => (K.hds y k e).1) K.hdn + case scCpu => + intro c hc + by_cases g : c ∈ Cn + · simp only [g, ↓reduceIte] + exact Fl4SpecA64.fl4A64Platform.numNodes_pos + · simp only [g, ↓reduceIte] + rw [K.eC] at hc + exact J.scCpu c (hc.resolve_right g) + case scNZ => + intro c hc + rw [K.eC] at hc + exact hc.elim (J.scNZ c) (K.scNe c) + case vcRun => + constructor + · intro t ht + rw [K.eT] at ht + rcases ht with old | new + · rw [K.tvk t old] + exact (J.vcRun.tcb t old).imp id (fun g => K.subV g) + · exact Or.inl (K.tvn t new) + · intro v hv + rcases K.vcw v hv with ⟨old, e⟩ | z + · rw [e, K.eT] + exact (J.vcRun.vcpu v old).imp id Or.inl + · exact Or.inl z + · intro v hv + rcases K.vcw0 v hv with old | z + · rw [K.vk v old, K.eT] + exact (J.vcRun.vtcb v old).imp id Or.inl + · exact Or.inl z + · intro t ht + rw [K.eT] at ht + rcases ht with old | new + · rw [K.runk t old] + exact (J.vcRun.run t old).imp id (fun g => K.subV g) + · exact Or.inl (K.runn t new) + · intro t ht h0 + rw [K.eT] at ht + rcases ht with old | new + · rw [K.tvk t old] at h0 + rw [K.runk t old] + exact J.vcRun.disj t old h0 + · exact absurd (K.tvn t new) h0 + case sidle => exact utRt_sidleA J.sidle K.spec K.hidt K.idle K.idleSc (fun y k e => (K.hds y k e).2) K.newCap + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtAsmSpecA.lean b/proofs/Fl4ProofsA64/P3/UtAsmSpecA.lean new file mode 100644 index 000000000..49230ab68 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtAsmSpecA.lean @@ -0,0 +1,90 @@ +/- +P3 (aarch64, A64-P3 Untyped, full-record post, assembly): **the spec-side fields**: `specFree`, `specZero`, +`origLive`, `nullOrig`, `finCov`, `tabInj` and `idleAll`, from the record before the creation. The free parts shrink +(`utRt_freeShrinkA`, with `hoth` from `utRt_hothA`). What is left in `UtAsmSpecInA` is the spec outcome and the node +words of the creation; the assembly supplies it. + +No named premise. New theorems only. +-/ +import Fl4ProofsA64.P3.UtAsmCapsA +import Fl4ProofsA64.P3.UtFullSlotA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4SpecA64 Classical + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z Z' : XSetsA} {H H' : Heap} {OS OS' U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s s' : S} {a : A} {σ σ' : State Unit} {m m' : McState} + {x p sb fi nfi nt us aligned : U64} {win : Nat} + +/-- The creation's side of these fields, beyond the cap split (`UtAsmCapsInA`). -/ +structure UtAsmSpecInA (st : machine.Store S) [L : LawfulStore st] (Z Z' : XSetsA) (OS : Set U64) (s s' : S) + (σ σ' : State Unit) (m m' : McState) (nt us aligned : U64) (win : Nat) : Prop where + th : ∀ q k, Z.th q = some k → Z'.th q = some k + cs : Z'.Cs = Z.Cs + idleThreadOf : m'.idleThreadOf = m.idleThreadOf + idleW : ∀ c ∈ Z.Cs, ∀ o : U64, (o = layout.node.IDLE_THREAD ∨ o = layout.node.IDLE_SC) → + L.mem s' (L.nodeAt s' c o) = L.mem s (L.nodeAt s c o) + /-- the spec creates no object outside the window, and none at all for new untypeds. -/ + kheapNone : ∀ q : U64, σ.kheap (specPtr q) = none → + (aligned.val ≤ q.val → q.val < aligned.val + win * 2 ^ (zA nt.val us).val → nt.val = 0) → + σ'.kheap (specPtr q) = none + um : ∀ w, σ.machineState.underlyingMemory w = 0 → σ'.machineState.underlyingMemory w = 0 + cdt : ∀ c d, σ'.cdt c = some d → σ.cdt c = some d ∨ + ∀ q : U64, UtFreeA st OS s (fun _ => True) q.val → c.1 ≠ specPtr q ∧ d.1 ≠ specPtr q + /-- a new original cap names no free address, is a slot of the grown heap and is not null. -/ + orig : ∀ sl, σ'.isOriginalCap sl = true → + (σ.isOriginalCap sl = true ∧ specCapAtA σ' sl = specCapAtA σ sl) ∨ + ((∀ q : U64, UtFreeA st OS s (fun _ => True) q.val → sl.1 ≠ specPtr q) ∧ AbsSlotA Z'.th sl ∧ + specCapAtA σ' sl ≠ some .nullCap) + cnode : ∀ q r cs, σ'.kheap q = some (.cnode r cs) → wellFormedCnodeN r cs → + (∃ cs0, σ.kheap q = some (.cnode r cs0) ∧ wellFormedCnodeN r cs0) ∨ Z'.th (fl4Ptr q) = some (.cnode r) + tcb : ∀ q y, σ'.kheap q = some (.tcb y) → (∃ y0, σ.kheap q = some (.tcb y0)) ∨ Z'.th (fl4Ptr q) = some .tcb + asid : σ'.archState.armAsidTable = σ.archState.armAsidTable + /-- (record edit #7) -/ + vcur : σ'.archState.armCurrentVcpu = σ.archState.armCurrentVcpu + +/-- The spec-side fields of the grown record. -/ +structure UtAsmSpecOutA (st : machine.Store S) [L : LawfulStore st] (Z' : XSetsA) (OS' : Set U64) (ia ie : Nat) + (s' : S) (σ' : State Unit) (m' : McState) : Prop where + specFree : SpecFreeA st OS' s' σ' + specZero : SpecZeroFreeA st OS' s' σ' + origLive : ∀ sl, σ'.isOriginalCap sl = true → AbsSlotA Z'.th sl + nullOrig : ∀ sl, specCapAtA σ' sl = some .nullCap → σ'.isOriginalCap sl = false + finCov : FinCovA Z'.th σ' + tabInj : ∀ i j r, σ'.archState.armAsidTable i = some r → σ'.archState.armAsidTable j = some r → i = j + idleAll : IdleAllA st ia ie Z'.Cs m' s' + +theorem utRt_asmSpecA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (I : UtAsmCapsInA st Z Z' H H' OS OS' s s' x p sb fi nfi nt us aligned win) + (K : UtAsmSpecInA st Z Z' OS s s' σ σ' m m' nt us aligned win) : + UtAsmSpecOutA st Z' OS' ia ie s' σ' m' := by + have hoth : ∀ u ∈ OS, u ≠ x → ∀ q b f d, capA st s u = .Untyped q b f d → ∀ w, q.val + f.val ≤ w → + w < q.val + 2 ^ b.val → ¬ (aligned.val ≤ w ∧ w < aligned.val + win * 2 ^ (zA nt.val us).val) := by + intro u hu ne q b f d e w w1 w2 ⟨w3, w4⟩ + have h1 := I.lo + have h3 := I.top + rcases utRt_hothA J I.src I.cap I.lo I.top I.ne u hu ne q b f d e with (g | g) | g <;> omega + have shrink : ∀ (dev : Bool → Prop) (w : Nat), UtFreeA st OS' s' dev w → UtFreeA st OS s dev w ∧ + (aligned.val ≤ w → w < aligned.val + win * 2 ^ (zA nt.val us).val → nt.val = 0) := fun _ _ h => + utRt_freeShrinkA I.slots I.W I.src I.cap I.cap' I.lo I.hi I.top hoth h + have orig' : ∀ sl, σ'.isOriginalCap sl = true → σ.isOriginalCap sl = true ∨ AbsSlotA Z'.th sl := + fun sl h => (K.orig sl h).imp And.left fun g => g.2.1 + exact + { specFree := utRt_specFreeA J.specFree (fun w h => shrink _ w h) K.kheapNone K.cdt + (fun sl h => (K.orig sl h).imp And.left fun g => g.1) + specZero := utRt_specZeroA J.specZero (fun w h => (shrink _ w h).1) K.um + origLive := utRt_origLiveA J.origLive K.th orig' + nullOrig := utRt_nullOrigA J.nullOrig fun sl h => (K.orig sl h).imp id fun g => g.2.2 + finCov := utRt_finCovA J.finCov K.th K.cnode K.tcb + tabInj := utRt_tabInjA J.tabInj K.asid + idleAll := utRt_idleAllA J.idleAll K.cs K.idleThreadOf K.idleW } + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtAsmSysA.lean b/proofs/Fl4ProofsA64/P3/UtAsmSysA.lean new file mode 100644 index 000000000..2b60c68ee --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtAsmSysA.lean @@ -0,0 +1,112 @@ +/- +P3 (aarch64, A64-P3 Untyped, full-record post, assembly): **the entry-invariant side of the record after a +creation**: `sys.ent`, `sys.sysl`, `SymL`, `poolOff`, `sq`, `tcbs`, `scs`, `ap`, `pp`, `s1`, `ko`, `j7`, `vsOk`. A64 +P1's creation top (`utRt_j7TopA`: `ArmQIVAt`, `HeapKsOff`, the `J7A` hook) supplies the invariants; what is left in +`UtAsmSysInA` the assembly supplies. `Ac` and the table set `Tb` of the grown record are new ones. `j5` is +`utRt_j5A`. + +No named premise. New theorems only. +-/ +import Fl4ProofsA64.P3.UtFullJ7A +import Fl4ProofsA64.P3.UtFullSqA +import Fl4ProofsA64.P3.UtVsOkJA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z Z' : XSetsA} {H H' : Heap} {OS OS' Vn U Ac Pp Tb Tb' : Set U64} + {Lv Lv' : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {nA : U64} {J5 : JSlotA S} {s s' : S} {a : A} + {σ σ' : State Unit} {m m' : McState} {slot p sb nt us aligned : U64} {win : Nat} + +/-- The creation's side of these fields. -/ +structure UtAsmSysInA (st : machine.Store S) [L : LawfulStore st] (ar : machine.Arch A) (nA : U64) + (Z Z' : XSetsA) (H H' : Heap) (OS OS' Vn U Pp Tb' : Set U64) (Lv' : U64 → Nat) (ia ie : Nat) (s s' : S) + (σ' : State Unit) (m' : McState) (slot p sb nt us aligned : U64) (win : Nat) : Prop where + /- A64 P1's creation top -/ + arm : ArmQIVAt st H Z.Cs OS ia ie s H' OS' Vn s' + ko : HeapKsOff H' (L.ksb s) + j7at : ∀ (Z'' : XSetsA) (σ'' : State Unit) (m'' : McState), + UtJ7SplitA st Z Z'' OS' s s' slot p sb nt us aligned win → J7A st (J7R2A st ar nA) Z'' H' OS' s' σ'' m'' + split : UtJ7SplitA st Z Z' OS' s s' slot p sb nt us aligned win + ksb : L.ksb s' = L.ksb s + /- the rest of the entry invariant -/ + heapOK : HeapOK H' + os : OSOK H'.tcbs H'.omega H'.frame OS' + user : Fl4ProofsA64.P1.UserOK st H'.tcbs Z'.Cs H'.omega H' U s' + vs : VsTyR st (L.ksb s') (PoolSet st Pp s') ∅ Tb' Lv' s' + /- the grown sets -/ + cs : Z'.Cs = Z.Cs + tcbs : H'.tcbs = Z'.T + scs : H'.scs = Z'.C + pp : Z'.Pp = Z.Pp + tb : Tb ⊆ Tb' + s1New : ∀ x ∈ Z'.S1, x ∈ UtSchedNewA nt us aligned win → x ∈ Tb' + /-- the old queue lists over the grown TCBs (`utRt_schedCoreA`). -/ + core : SchedCoreA st H'.tcbs Z'.Cs s' Z'.Q Z'.R + /- slot apartness -/ + sca : ScApart H'.scs H'.scBits OS' + irqSt : IrqStApart OS' (L.ksb s') + vcAl : ∀ v ∈ Z'.V, Aligned v + vc : ∀ v ∈ Z'.V, ∀ b, (b = v ∨ b = wa v 24#u64) → ¬ InOS OS' b ∧ ¬ W7.TLow H'.tcbs b + vsAp : ∀ b, VsFootA Z'.Pp Z'.S1 Z'.Y (L.ksb s') b → ¬ InOS OS' b ∧ ∀ t ∈ H'.tcbs, b.val < t.val ∨ t.val + 4096 ≤ b.val + ext : ∀ b, ExtFootE Z'.X Z'.Y Z'.T2 b → ¬ InOS OS' b ∧ ∀ t ∈ H'.tcbs, b.val < t.val ∨ t.val + 4096 ≤ b.val + +/-- The entry-invariant side of the grown record, at the new block set `Ac'`. -/ +structure UtAsmSysOutA (st : machine.Store S) [L : LawfulStore st] (ar : machine.Arch A) (nA : U64) (Z' : XSetsA) + (H' : Heap) (OS' U Ac' Pp Tb' : Set U64) (Lv' : U64 → Nat) (ia ie : Nat) (s' : S) (σ' : State Unit) + (m' : McState) : Prop where + sys : SysInvA st H' Z'.Cs OS' U Ac' Pp Tb' Lv' ia ie s' + symL : SymL st H' Z'.Cs s' + poolOff : PoolOff st H' Z'.Cs OS' s' + sq : SchedQueuesInv st H'.tcbs Z'.Cs H'.omega H' s' Z'.Q Z'.R + tcbs : H'.tcbs = Z'.T + scs : H'.scs = Z'.C + ap : SlotApA Z' H' OS' (L.ksb s') + pp : Z'.Pp = Pp + s1 : Z'.S1 ⊆ Tb' + ko : HeapKsOff H' (L.ksb s') + j7 : J7A st (J7R2A st ar nA) Z' H' OS' s' σ' m' + vsOk : UtVsOkA st H' OS' Z'.V Pp Tb' Lv' s' + +theorem utRt_asmSysA {U0 : Set U64} + (J : JStage3A st AR Z H OS U0 Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7A st (J7R2A st ar nA)) s a σ m) + (K : UtAsmSysInA st ar nA Z Z' H H' OS OS' Vn U Pp Tb' Lv' ia ie s s' σ' m' slot p sb nt us aligned win + (Tb := Tb)) : + ∃ Ac', UtAsmSysOutA st ar nA Z' H' OS' U Ac' Pp Tb' Lv' ia ie s' σ' m' := by + obtain ⟨sysl, symL, ⟨Ac', blk⟩, -, -, poolOff, -⟩ := K.arm + obtain ⟨⟨Q0, R0, h0⟩, bound, xinv, scsok, sg⟩ := blk + have j7 := K.j7at Z' σ' m' K.split + have sq : SchedQueuesInv st H'.tcbs Z'.Cs H'.omega H' s' Z'.Q Z'.R := by + rw [← K.cs] at h0 + exact utRt_sqA K.core h0 + refine ⟨Ac', ?_⟩ + exact + { sys := + { ent := ⟨K.heapOK, ⟨⟨_, _, sq⟩, bound, K.cs.symm ▸ xinv, sg, scsok⟩, K.os, K.user, K.vs, + K.ksb.symm ▸ J.sys.ent.ksw⟩ + sysl := K.cs.symm ▸ sysl } + symL := K.cs.symm ▸ symL + poolOff := K.cs.symm ▸ poolOff J.poolOff + sq := sq + tcbs := K.tcbs + scs := K.scs + ap := ⟨K.sca, K.irqSt, K.vcAl, K.vc, K.vsAp, K.ext⟩ + pp := K.pp.trans J.pp + s1 := fun x hx => by + rcases K.split.s1 x hx with ⟨old, -⟩ | new + · exact K.tb (J.s1 old) + · exact K.s1New x hx new + ko := K.ksb.symm ▸ K.ko + j7 := j7 + vsOk := utVsOk_of_J7A j7 K.vs } + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtChunkA.lean b/proofs/Fl4ProofsA64/P3/UtChunkA.lean new file mode 100644 index 000000000..7e6d2b463 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtChunkA.lean @@ -0,0 +1,1227 @@ +/- +P3 (aarch64, A64-P3 Untyped, `UtResetA`; lead 04:55Z): **the whole record across one chunk step of the reset**. + +`utChunk_coreA`: the free-index write itself, RAM or device. `utChunk_stepA`: zero a chunk `[p + off, p + off + n)` of a clear region and rewrite the untyped's free index to +`off`. FL4's `set_cap` succeeds, spec `set_cap` has the outcome, and `JStage3A` (concrete J5, J7 at `J7R2A`), `SymL`, +`UtClrA`, `SysPlace`, `ensure_no_children` and the spec facts about the region hold again, so the step can be iterated. +The preemption point is not covered (contract J8, `PPHypUtA`). + +Named premises: +* [J] `symL`: A64 P1's `SymL`. +-/ +import Fl4ProofsA64.P3.Stage3Rec5A +import Fl4ProofsA64.P3.UtClrA +import Fl4ProofsA64.P3.Stage3SchedWrA +import Fl4ProofsA64.P3.UtResetSpecA +import Fl4ProofsA64.P3.SetCapJA +import Fl4ProofsA64.P3.VsTabJA +import Fl4ProofsA64.P3.TabZeroA +import Fl4ProofsA64.P1.MdbRstZ +import Fl4ProofsA64.P1.S1aR2Transfer +import Fl4ProofsA64.P1.MdbInvA +import Fl4ProofsA64.P1.MdbIrqInvA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {ar : machine.Arch A} + +/-! ## The slot's rewrite with a lower free index -/ + +/-- **`JAtZAX` across an in-place CNode slot write that keeps the tree** (`JAtZAX.of_setcapA` with the tree after the +write as a hypothesis: the reset lowers the free index, so `UA` does not hold). -/ +theorem JAtZAX.of_lowerA {AR : A → HwRootA → Prop} {Z : XSetsA} {s : S} {a : A} {σ : State Unit} {m : McState} + (J : JAtZAX (L := L) AR Z s a σ m) {T Ω Sg OS : Set U64} (hO : OSOK T Ω Sg OS) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hoff : ∀ b, FootNA (L := L) Z s b → ¬ W7.CapW T {b | InOS OS b} ∅ b) + (hal : ∀ b, FootNA (L := L) Z s b → Aligned b) + (cte : CslotPtr) {q : U64} (hq : cte.1 = specPtr q) (hth : Z.th q = some (.cnode cte.2.length)) + {c : capability.Cap} {sc : Cap} (hEnc : CapRt.EncWf c) (hca : CapRelA c sc) + (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) (hok : CapObjOkA Z c) {s2 : S} + (hset : obj.Slot.set_cap st (cslotAddrA cte) s c = ok s2) + (hT2 : TreeA st OS Z.G.chains Z.G.par s2) : + ∃ cs, σ.kheap cte.1 = some (.cnode cte.2.length cs) ∧ wellFormedCnodeN cte.2.length cs ∧ + ((), setObjA σ cte.1 (.cnode cte.2.length (Function.update cs cte.2 (some sc)))) ∈ + (setCap (Z := Unit) sc cte σ).1 ∧ + WritesIn st s s2 (W7.CapW T {b | InOS OS b} ∅) ∧ + JAtZAX (L := L) AR Z s2 a (setObjA σ cte.1 (.cnode cte.2.length (Function.update cs cte.2 (some sc)))) m := by + have hP1 := J.1.place + have hab : AbsSlotA Z.th cte := ⟨q, hq, Or.inl hth⟩ + have hcs : IsSlotA Z.th (cslotAddrA cte) := hab.isSlot hP1 + have hos := hsub _ hcs + obtain ⟨w0, w1, hen, hd⟩ := CapRt.decode_encode _ hEnc + have a0x := hO.al0 hos + have a8x := hO.al hos (k := 8#u64) (by simp) (by simp) + obtain ⟨v8, -, -⟩ := vals hO hos + have hw2 : WritesIn st s (L.wr (L.wr s (cslotAddrA cte) w0) (wa (cslotAddrA cte) 8#u64) w1) + (W7.CapW T {b | InOS OS b} ∅) := by + have := W7.obj.Slot.set_cap.capw (st := st) (s := s) (c := c) (osw hO hos) + rw [set_cap_eq hen, WP.spec_ok] at this + exact this + have es2 : s2 = L.wr (L.wr s (cslotAddrA cte) w0) (wa (cslotAddrA cte) 8#u64) w1 := + okinj (hset.symm.trans (set_cap_eq hen)) + subst es2 + have hcx : capAtA (L.mem (L.wr (L.wr s (cslotAddrA cte) w0) (wa (cslotAddrA cte) 8#u64) w1)) (cslotAddrA cte) = c := by + have hd' := hd + unfold capAtA + rw [L.mem_wr_ne a8x a0x (by omega), L.mem_wr_same a0x, L.mem_wr_same a8x] + rw [CapRt.decode_eq] at hd' + exact okinj hd' + have hcapo : ∀ y, IsSlotA Z.th y → y ≠ cslotAddrA cte → + capAtA (L.mem (L.wr (L.wr s (cslotAddrA cte) w0) (wa (cslotAddrA cte) 8#u64) w1)) y = capAtA (L.mem s) y := by + intro y hy hne + rw [capAtA_eq, capAtA_eq] + exact capA_wr2_other hO hos (hsub y hy) hne + obtain ⟨cs, hk, hwf, -⟩ := J.1.st.cnode q _ hth + have hk' : σ.kheap cte.1 = some (.cnode cte.2.length cs) := by rw [hq]; exact hk + have hmem : ((), setObjA σ cte.1 (.cnode cte.2.length (Function.update cs cte.2 (some sc)))) ∈ + (setCap (Z := Unit) sc cte σ).1 := setCap_cnode_memA sc hk' hwf rfl + have hca' : CapRelA (capAtA (L.mem (L.wr (L.wr s (cslotAddrA cte) w0) (wa (cslotAddrA cte) 8#u64) w1)) + (cslotAddrA cte)) sc := by rw [hcx]; exact hca + obtain ⟨o, idx⟩ := cte + simp only at hq hth hk hk' hmem hca' hcapo hcs hcx ⊢ + subst hq + have hSR := StateRelA.setCn (σ' := setObjA σ (specPtr q) (.cnode idx.length (Function.update cs idx (some sc)))) + J.1.st hP1 hth hk rfl hca' hcapo rfl + have HS : SpecSlotA σ (setObjA σ (specPtr q) (.cnode idx.length (Function.update cs idx (some sc)))) := + SpecSlotA.setCn hk rfl ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, ⟨rfl, rfl, rfl, rfl⟩⟩ + have C1 := J.1.cdt + have hC : CdtRelA st Z.th (L.wr (L.wr s (cslotAddrA (specPtr q, idx)) w0) (wa (cslotAddrA (specPtr q, idx)) 8#u64) w1) + (setObjA σ (specPtr q) (.cnode idx.length (Function.update cs idx (some sc)))) Z.G := + CdtRelA.mkA (cdtTree_of hT2 hsub fun y hy hn => by + rw [capA_wr2_other hO hos hy (fun e => hn (e ▸ hcs))]; exact hnul y hy hn) + C1.cdt C1.cdt_slot + (fun sl hsl => by rw [revA_wr2A hO hos (hsub _ (hsl.isSlot hP1))]; exact C1.orig sl hsl) + C1.par_slot C1.po + refine ⟨cs, hk', hwf, hmem, hw2, + J.of_slotWrites hw2 (fun b hb hf => hoff b hf hb) hal Z.G HS hSR hC fun y hy => ?_⟩ + by_cases hne : y = cslotAddrA (specPtr q, idx) + · subst hne; rw [hcx]; exact hok + · rw [hcapo y hy hne]; exact J.capObj hy + +/-! ## Spec -/ + +/-- A CNode slot write is a step that changes at most one CNode. -/ +theorem ut_cnStep_setA {σ : State Unit} {p : ObjRef} {r : Nat} {cs cs' : CnodeContents} + (h : σ.kheap p = some (.cnode r cs)) : UtCnStepA σ (setObjA σ p (.cnode r cs')) := UtCnStepA.set h + +/-- **The spec IRQ node across the untyped's slot rewrite**: the CNode is no IRQ node, as its slot held an untyped. -/ +theorem ut_specIrq_setA {σ : State Unit} (h : SpecIrqA σ) {p : ObjRef} {r : Nat} {cs : CnodeContents} + {idx : CnodeIndex} {d d' : Bool} {w : Word} {b i i' : Nat} (hk : σ.kheap p = some (.cnode r cs)) + (hwf : wellFormedCnodeN r cs) (hc : cs idx = some (.untypedCap d w b i)) : + SpecIrqA (setObjA σ p (.cnode r (Function.update cs idx (some (.untypedCap d' w b i'))))) := by + have ne : ∀ irq : Irq, σ.interruptIrqNode irq ≠ p := by + intro irq e + by_cases hlt : irq.toNat < fl4IrqCount + · have := h.slot irq hlt _ idx _ (by rw [e]; exact hk) (show capOf (.cnode r cs) idx = _ from hc) + rcases this with g | ⟨_, _, _, g⟩ <;> cases g + · exact h.node.2 irq (by omega) r ⟨cs, by rw [e]; exact hk, hwf⟩ + have keep : ∀ irq : Irq, + (setObjA σ p (.cnode r (Function.update cs idx (some (.untypedCap d' w b i'))))).kheap (σ.interruptIrqNode irq) + = σ.kheap (σ.interruptIrqNode irq) := fun irq => Function.update_of_ne (ne irq) .. + refine ⟨⟨⟨h.node.1.1, fun irq hlt => ?_⟩, fun irq hge k => ?_⟩, fun irq hlt o b' a' ho hcap => ?_, + fun q o b' a' ho hcap => ?_⟩ + · obtain ⟨cs0, h1, h2⟩ := h.node.1.2 irq hlt + exact ⟨cs0, (keep irq).trans h1, h2⟩ + · rintro ⟨cs0, h1, h2⟩ + exact h.node.2 irq hge k ⟨cs0, (keep irq).symm.trans h1, h2⟩ + · exact h.slot irq hlt o b' a' ((keep irq).symm.trans ho) hcap + · by_cases e : q = p + · subst e + have eo : o = .cnode r (Function.update cs idx (some (.untypedCap d' w b i'))) := by + have : (setObjA σ q (.cnode r (Function.update cs idx (some (.untypedCap d' w b i'))))).kheap q + = some (.cnode r (Function.update cs idx (some (.untypedCap d' w b i')))) := Function.update_self .. + exact Option.some.inj (ho.symm.trans this) + subst eo + change Function.update cs idx (some (.untypedCap d' w b i')) b' = some a' at hcap + by_cases ei : b' = idx + · subst ei + rw [Function.update_self] at hcap + cases hcap + rintro ⟨_, _, _, _, g⟩ + cases g + · rw [Function.update_of_ne ei] at hcap + exact h.ref q _ b' a' hk hcap + · have : (setObjA σ p (.cnode r (Function.update cs idx (some (.untypedCap d' w b i'))))).kheap q = σ.kheap q := + Function.update_of_ne e .. + exact h.ref q o b' a' (this.symm.trans ho) hcap + +/-! ## The step -/ + +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s : S} {a : A} {σ : State Unit} {m : McState} + +set_option maxHeartbeats 4000000 in +/-- F1–F2 across a step that rewrites CNode contents only. -/ +theorem utCn_sactA {σ σ' : State Unit} (h : SActA σ) (H : UtCnStepA σ σ') : SActA σ' := by + refine ⟨fun u hu => ?_, ?_⟩ + · rw [H.cur, H.idle] + exact h.f1 u (by rw [← H.act]; exact hu) + · obtain ⟨x, hx, hs⟩ := h.f2 + exact ⟨x, by rw [H.cur]; exact H.keep hx (fun _ _ e => by cases e), hs⟩ + +/-- F3–F6 across a step that rewrites CNode contents only; `hc`: a cap after is an old one or no idle cap. -/ +theorem utCn_sidleA {σ σ' : State Unit} {m : McState} {n : Nat} (h : SIdleA σ m n) (H : UtCnStepA σ σ') + (hc : ∀ sl a, specCapAtA σ' sl = some a → specCapAtA σ sl = some a ∨ NoIdleCapV m n a) : SIdleA σ' m n := by + have back : ∀ p o, (∀ r cs, o ≠ .cnode r cs) → σ'.kheap p = some o → σ.kheap p = some o := by + intro p o ne g + rcases H.kh p with e | ⟨r, cs, cs', -, e2⟩ + · rw [← e]; exact g + · rw [g] at e2 + exact absurd (Option.some.inj e2) (ne _ _) + have vt : ∀ c < n, vtCapA σ' (m.idleThreadOf c) = some .nullCap ∧ vcpuOfA σ' (m.idleThreadOf c) = none := by + intro c hc' + obtain ⟨x, hx, -⟩ := h.f3 c hc' + exact idleVtSameA (by rw [hx]; exact H.keep hx (fun _ _ e => by cases e)) (h.idleSlot c hc') (h.idleBound c hc') + refine ⟨fun c hc' => ?_, fun y k hy => h.f4 y k (back _ _ (fun _ _ e => by cases e) hy), + fun p y k hy he => h.f5 p y k (back _ _ (fun _ _ e => by cases e) hy) (by rw [← H.idle]; exact he), + h.f6.of_new hc, fun y k hy => by rw [H.time]; exact h.f7 y k (back _ _ (fun _ _ e => by cases e) hy), + fun c hc' => (vt c hc').1, fun c hc' => (vt c hc').2⟩ + obtain ⟨x, hx, hs⟩ := h.f3 c hc' + exact ⟨x, H.keep hx (fun _ _ e => by cases e), hs⟩ + +/-- **The untyped's free-index write keeps the record**, RAM or device (module docstring): from the FL4 step's facts +(`s1` the state after the clear, `s2` after `set_cap`) and `SysL ∧ SymL ∧ BlkOK` at `s2`. -/ +theorem utChunk_coreA [Fact (n ≤ Fl4SpecA64.fl4A64Platform.numNodes)] {dev : Bool} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {p sb i off len c x : U64} + (C : UtClrA st Z H s p.val (p.val + 2 ^ sb.val)) + {Rq Nt : Set U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (P : SysPlace st H Z.Cs H.frame Rq Nt OS s extra x) + (hrun : Runnable (L.mem s (stA (L.curW s)))) + (hcx : capA st s x = .Untyped p sb i dev) + (he : cdt.ensure_no_children st s x = ok (.Ok ())) + (cte : CslotPtr) {qc : U64} (hq : cte.1 = specPtr qc) (hth : Z.th qc = some (.cnode cte.2.length)) + (hxc : cslotAddrA cte = x) + (hslots : ∀ y ∈ OS, y.val + 32 ≤ p.val ∨ p.val + 2 ^ sb.val ≤ y.val) + (hcv : c.val = p.val + off.val) + (hon : off.val + len.val ≤ 2 ^ sb.val) + (hheap : ∀ q : U64, p.val ≤ q.val → q.val < p.val + 2 ^ sb.val → σ.kheap (specPtr q) = none) + (hlink : ∀ a' c', σ.cdt a' = some c' → ∀ q : U64, p.val ≤ q.val → q.val < p.val + 2 ^ sb.val → + a'.1 ≠ specPtr q ∧ c'.1 ≠ specPtr q) + (horig : ∀ a', σ.isOriginalCap a' = true → ∀ q : U64, p.val ≤ q.val → q.val < p.val + 2 ^ sb.val → + a'.1 ≠ specPtr q) + (hz : ∀ w : Word, p.val ≤ (fl4Ptr w).val → (fl4Ptr w).val < p.val + 2 ^ sb.val → + σ.machineState.underlyingMemory w = 0) + (s1 s2 : S) (hw1 : WritesIn st s s1 (fun b => c.val ≤ b.val ∧ b.val < c.val + len.val)) + (hset : obj.Slot.set_cap st x s1 (.Untyped p sb off dev) = ok s2) + (hks : L.ksb s2 = L.ksb s) (hcpu : L.cpu s2 = L.cpu s) (kc : ∀ y ∈ OS, y ≠ x → capA st s2 y = capA st s y) + (hl : ∀ y ∈ OS, L.mem s2 (wa y 16#u64) = L.mem s (wa y 16#u64) ∧ + L.mem s2 (wa y 24#u64) = L.mem s (wa y 24#u64)) + (hcx' : capA st s2 x = .Untyped p sb off dev) + (hsys : SysL st H Z.Cs OS ia ie s2 ∧ SymL st H Z.Cs s2 ∧ + BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s2) : + ∃ σ2, ((), σ2) ∈ (setCap (Z := Unit) (absCapA (.Untyped p sb off dev)) cte σ).1 ∧ + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s2 a σ2 m ∧ + SymL st H Z.Cs s2 ∧ UtClrA st Z H s2 p.val (p.val + 2 ^ sb.val) ∧ + SysPlace st H Z.Cs H.frame Rq Nt OS s2 extra x ∧ Runnable (L.mem s2 (stA (L.curW s2))) ∧ + capA st s2 x = .Untyped p sb off dev ∧ cdt.ensure_no_children st s2 x = ok (.Ok ()) ∧ + UtCnStepA σ σ2 ∧ σ2.cdt = σ.cdt ∧ σ2.isOriginalCap = σ.isOriginalCap ∧ + (∀ q : U64, p.val ≤ q.val → q.val < p.val + 2 ^ sb.val → σ2.kheap (specPtr q) = none) := by + have hH := J.sys.ent.heap + have hO := J.sys.ent.os + have hx : x ∈ OS := P.src + have hMdb := J.sys.sysl.sys.stz.1.1 + have hRev := J.sys.sysl.sys.stz.1.2.1 + have hV := J.sys.sysl.sys.stz.1.2.2.1 + obtain ⟨hpal, h47, hifull⟩ := J.sys.sysl.sys.ur.utv hMdb hx hcx + have hfit : p.val + 2 ^ sb.val ≤ 2 ^ 64 := al_fit hpal (by omega) p.bv.isLt + have hoffle : off.val ≤ 2 ^ sb.val := by omega + have P3 : PlaceA Z.th (L.ksb s) := J.j.1.place + have hsub : ∀ y, IsSlotA Z.th y → y ∈ OS := J.j5.2.1 + have hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null := J.j5.2.2 + have hab : AbsSlotA Z.th cte := ⟨qc, hq, Or.inl hth⟩ + have hxs : IsSlotA Z.th x := hxc ▸ hab.isSlot P3 + have htr : TreeA st OS Z.G.chains Z.G.par s := J.j.1.cdt.tree.grow hsub fun y hy hn => hnul y hy hn + have hr : obj.Slot.revocable st x s = ok true := hRev x hx (by rw [hcx]; rfl) + have hnd : ∀ y, ¬ Fl4Proofs.P3.Mdb.Desc Z.G.par y x := cdt.ensure_no_children.noDesc hO htr hx hcx hr he + have eO : OffR p.val (p.val + 2 ^ sb.val) OS = OS := + Set.ext fun y => ⟨fun h => h.1, fun h => ⟨h, hslots y h⟩⟩ + have chunkR : ∀ b : U64, c.val ≤ b.val ∧ b.val < c.val + len.val → + p.val ≤ b.val ∧ b.val < p.val + 2 ^ sb.val := fun b h => by omega + have osk1 : OSKeep st OS s s1 := fun y hy => by + have hf' := hO.fit y hy + have v : ∀ o : U64, o.val < 32 → (wa y o).val = y.val + o.val := fun o ho => by + rw [wa_valP]; exact Nat.mod_eq_of_lt (by omega) + have out : ∀ b : U64, y.val ≤ b.val → b.val < y.val + 32 → ¬ (c.val ≤ b.val ∧ b.val < c.val + len.val) := + fun b h1 h2 h => by + have := chunkR b h + rcases hslots y hy with g | g <;> omega + have k : ∀ o : U64, o.val < 32 → o.val % 8 = 0 → L.mem s1 (wa y o) = L.mem s (wa y o) := + fun o ho h8 => hw1.mem_eq (hO.al hy ho h8) (out _ (by rw [v o ho]; omega) (by rw [v o ho]; omega)) + exact ⟨hw1.mem_eq (hO.al0 hy) (out _ (le_refl _) (by omega)), k 8#u64 (by simp) (by simp), + k 16#u64 (by simp) (by simp), k 24#u64 (by simp) (by simp)⟩ + -- the record at the cleared state + have hJ1 : JAtZAX (L := L) AR Z s1 a σ m := + J.toJEntA.writesIn hw1 (fun b hb hfoot => ut_foot_regionA J C hfoot (chunkR b hb)) J.ap.vcAl + have hks1 : L.ksb s1 = L.ksb s := hw1.1 + have hcpu1 : L.cpu s1 = L.cpu s := hw1.2.1 + have hfn := footN_offA J.toJEntA + have hfal := FootNA.aligned J.j.1 (bxi_nodes J.sys.ent.bxi) J.toJEntA.scAl J.ap.vcAl + have hEnc : CapRt.EncWf (.Untyped p sb off dev) := by + have w := CapRt.decodeF_encWf (L.mem s x).bv (L.mem s (wa x 8#u64)).bv + have e' : CapRt.decodeF (L.mem s x).bv (L.mem s (wa x 8#u64)).bv = .Untyped p sb i dev := hcx + rw [e'] at w; exact w + -- the old slot relation + obtain ⟨acx, hacx, hrelx⟩ := ut_absRelA J.j.1.st hab + have eold : capAtA (L.mem s) (cslotAddrA cte) = .Untyped p sb i dev := by rw [hxc]; exact hcx + rw [eold] at hrelx + have eacx : acx = .untypedCap dev (specPtr p) sb.val i.val := (show absCapA (.Untyped p sb i dev) = acx from hrelx.2).symm + have hca : CapRelA (.Untyped p sb off dev) (absCapA (.Untyped p sb off dev)) := + ⟨hrelx.1, CapEqA.of_abs rfl⟩ + have hT2 : TreeA st OS Z.G.chains Z.G.par s2 := + htr.lower hx hcx hcx' hoffle hnd kc hl + obtain ⟨cs, hk, hwf, hmem, -, hJ2⟩ := JAtZAX.of_lowerA hJ1 hO hsub (footNOff_ksA hks1 hcpu1 hfn) + (footNAl_ksA hks1 hcpu1 hfal) cte hq hth hEnc hca + (fun y hy hn => (osk1.capA hy).trans (hnul y hy hn)) + ⟨(fun _ h => by cases h), (fun _ _ _ _ _ h => by cases h), (fun _ _ _ h => by cases h), (fun _ _ _ h => by cases h), + trivial⟩ (s2 := s2) (by rw [hxc]; exact hset) hT2 + have hcsidx : cs cte.2 = some (.untypedCap dev (specPtr p) sb.val i.val) := by + unfold specCapAtA at hacx + rw [hk] at hacx + simp only [hwf, ↓reduceIte] at hacx + rw [hacx, eacx] + have eabs : absCapA (.Untyped p sb off dev) = .untypedCap dev (specPtr p) sb.val off.val := rfl + set σ2 := setObjA σ cte.1 (.cnode cte.2.length (Function.update cs cte.2 (some (absCapA (.Untyped p sb off dev))))) + with hσ2 + have H2 : UtCnStepA σ σ2 := ut_cnStep_setA hk + have hcdt : σ2.cdt = σ.cdt := rfl + have hor : σ2.isOriginalCap = σ.isOriginalCap := rfl + -- the whole write set + have a0x := hO.al0 hx + have a8x := hO.al hx (k := 8#u64) (by simp) (by simp) + have osd : ∀ b : U64, x.val ≤ b.val → b.val < x.val + 32 → DomW (L.ksb s) b → False := + fun b h1 h2 hd => by + obtain ⟨o, ho, eo⟩ := hd.wa_exists J.sr.ksFit + exact J.sys.sysl.ly.kslo o ho x hx (by rw [eo]; exact ⟨h1, h2⟩) + have hw : WritesIn st s s2 (fun b => (p.val ≤ b.val ∧ b.val < p.val + 2 ^ sb.val) ∨ b = x ∨ b = wa x 8#u64) := by + obtain ⟨w0, w1, hen, -⟩ := CapRt.decode_encode _ hEnc + have es2 : s2 = L.wr (L.wr s1 x w0) (wa x 8#u64) w1 := okinj (hset.symm.trans (set_cap_eq hen)) + refine ⟨hks, hcpu, fun b hb hn => ?_⟩ + rw [es2, L.mem_wr_ne a8x hb (fun e => hn (Or.inr (Or.inr (UScalar.eq_of_val_eq e.symm)))), + L.mem_wr_ne a0x hb (fun e => hn (Or.inr (Or.inl (UScalar.eq_of_val_eq e.symm))))] + exact hw1.mem_eq hb fun h => hn (Or.inl (chunkR b h)) + have hmem1 : ∀ b, Aligned b → b ≠ x → b ≠ wa x 8#u64 → L.mem s2 b = L.mem s1 b := by + obtain ⟨w0, w1, hen, -⟩ := CapRt.decode_encode _ hEnc + have es2 : s2 = L.wr (L.wr s1 x w0) (wa x 8#u64) w1 := okinj (hset.symm.trans (set_cap_eq hen)) + intro b hb h1 h2 + rw [es2, L.mem_wr_ne a8x hb (fun e => h2 (UScalar.eq_of_val_eq e.symm)), + L.mem_wr_ne a0x hb (fun e => h1 (UScalar.eq_of_val_eq e.symm))] + have hW : ∀ b, ((p.val ≤ b.val ∧ b.val < p.val + 2 ^ sb.val) ∨ b = x ∨ b = wa x 8#u64) → + ¬ UtRdA st Z N E Rp Bf m s σ b := by + rintro b (h | h) hrd + · exact ut_rd_regionA J C hrd h + · exact ut_rd_slotA J hx hcx hxs h hrd + -- the slot's object, and the words off it + have f32 := hO.fit x hx + have v8x : (wa x 8#u64).val = x.val + 8 := by rw [wa_valP]; exact Nat.mod_eq_of_lt (by simp; omega) + have inOSx : ∀ b : U64, (b = x ∨ b = wa x 8#u64) → InOS OS b := fun b h => + ⟨x, hx, by rcases h with e | e <;> (have := congrArg UScalar.val e; omega), + by rcases h with e | e <;> (have := congrArg UScalar.val e; omega)⟩ + have xregion : ∀ b : U64, (b = x ∨ b = wa x 8#u64) → ¬ (p.val ≤ b.val ∧ b.val < p.val + 2 ^ sb.val) := by + intro b h hb + have : x.val ≤ b.val ∧ b.val < x.val + 16 := by rcases h with e | e <;> rw [e] <;> omega + rcases hslots x hx with g | g <;> omega + have footN : ∀ b, FootNA (L := L) Z s b → L.mem s2 b = L.mem s b := by + intro b hb + refine hw.mem_eq (hfal b hb) ?_ + rintro (h | h) + · exact ut_foot_regionA J C (.nslot _ hb) h + · refine hfn b hb ?_ + rcases h with e | e <;> rw [e] + · exact Or.inr ⟨inOSx _ (Or.inl rfl), id⟩ + · exact Or.inr ⟨inOSx _ (Or.inr rfl), id⟩ + have hcap2 : ∀ y ∈ OS, y ≠ x → capA st s2 y = capA st s y := kc + -- J7's words + obtain ⟨Pt, Ub, vr, pool, armc, capc, vcc, frd, hic, sched, apart, frub, alub, vcub, Pp7, Tb7, Hr, Te, Kt, Lv7, Re, bits, r2, kt, hkl⟩ := + J.j7 + have vsk : VsKeep st (L.ksb s) Pt s s2 := by + intro b hb + refine hw.mem_eq (vr.al b hb) ?_ + rintro (h | h) + · rcases hb with ⟨t, ht, o, ho, -, e⟩ | ⟨o, h1, h2, -, e⟩ + · obtain ⟨-, r, z, hr', g1, g2⟩ := r2.footprint t ht + have hrf := hH.fits r _ hr' + simp only [HKind.bits] at hrf + have v : b.val = t.val + o.val := by rw [e, wa_valP]; exact Nat.mod_eq_of_lt (by omega) + exact C.inObj (Or.inr (Or.inr (Or.inr ⟨_, hr', rfl⟩))) (by omega) (by simp only [HKind.bits]; omega) h + · have hk' := J.sys.ent.ksw + have v : b.val = (L.ksb s).val + o.val := by rw [e, wa_valP]; exact Nat.mod_eq_of_lt (by omega) + rcases C.ks with g | g <;> omega + · exact vr.os b hb (inOSx b h) + have tabW : ∀ j, L.mem s2 (wa (L.ksb s) (AtOff j)) = L.mem s (wa (L.ksb s) (AtOff j)) := fun j => + vsk _ (Or.inr (asW_at _ j)) + have nodeE : ∀ c' o, L.nodeAt s2 c' o = L.nodeAt s c' o := fun c' o => nodeAt_ksb hks c' o + -- the current thread + have hcpuCs : L.cpu s ∈ Z.Cs := J.j.1.sch.cpuCs + have ecur : L.curW s2 = L.curW s := by + unfold LawfulStore.curW + rw [hcpu, nodeE] + exact footN _ (.node _ _ hcpuCs (Or.inl rfl)) + have hcurT : L.curW s ∈ Z.T := J.sr.curT + have hrun2 : Runnable (L.mem s2 (stA (L.curW s2))) := by + rw [ecur, footN _ (.tcb _ _ hcurT (Or.inl rfl))] + exact hrun + -- P1's invariants + have hcurH : H (L.curW s) ≠ none := by + have : L.curW s ∈ H.tcbs := by rw [J.tcbs]; exact hcurT + rw [show H (L.curW s) = some .tcb from this]; simp + have hcscH : H (L.cscW s) ≠ none := by + have : L.cscW s ∈ H.scs := by rw [J.scs]; exact J.j.1.cscC _ hcpuCs + obtain ⟨z, hz'⟩ := this + rw [hz']; simp + have hclrH : ∀ q k, H q = some k → ¬ RegionD H p.val (p.val + 2 ^ sb.val) q := by + intro q k h hR + have g := C.obj q _ (Or.inr (Or.inr (Or.inr ⟨k, h, rfl⟩))) + unfold RegionD MeetsR at hR + rw [h] at hR + simp only [Option.elim] at hR + omega + obtain ⟨sysl2, sym2, blk2⟩ := hsys + -- `ensure_no_children` + have hxlinks := hl x hx + have hsh : ∀ y ∈ OS, Fl4ProofsA64.P1.W7.R.ST.shapeA (capA st s2 y) = + Fl4ProofsA64.P1.W7.R.ST.shapeA (capA st s y) := fun y hy => by + by_cases e : y = x + · rw [e, hcx, hcx']; rfl + · rw [kc y hy e] + have hbd : ∀ y ∈ OS, Fl4ProofsA64.P1.W7.R.ST.badgeA (capA st s2 y) = + Fl4ProofsA64.P1.W7.R.ST.badgeA (capA st s y) := fun y hy => by + by_cases e : y = x + · rw [e, hcx, hcx']; rfl + · rw [kc y hy e] + have hnx : ∀ y ∈ OS, NxA st s2 y = NxA st s y := fun y hy => by unfold NxA; rw [(hl y hy).2] + have hrv : ∀ y ∈ OS, Fl4ProofsA64.P1.W7.R.ST.revA st s2 y = Fl4ProofsA64.P1.W7.R.ST.revA st s y := fun y hy => revA_congr (hl y hy).2 + have hfb : ∀ y ∈ OS, fbA st s2 y = fbA st s y := fun y hy => fbA_congr (hl y hy).2 + have hGO : ∀ y ∈ Z.G.chains.flatten, y ∈ OS := fun y hy => ((htr.lk.members y).1 hy).1 + have hxG : x ∈ Z.G.chains.flatten := (htr.lk.members x).2 ⟨hx, by rw [hcx]; exact fun e => by cases e⟩ + have he2 : cdt.ensure_no_children st s2 x = ok (.Ok ()) := by + have he' := he + unfold cdt.ensure_no_children at he' ⊢ + rw [next_eq] at he' ⊢ + rw [hnx x hx] + simp only [bind_ok, obj.Slot.is_null] at he' ⊢ + by_cases h0 : NxA st s x = 0#u64 + · simp [h0] + · simp only [h0, decide_false, Bool.false_eq_true, ↓reduceIte] at he' ⊢ + have hmO := hGO _ (htr.lk.next_memA hxG h0) + have : isPMA st s2 x (NxA st s x) = isPMA st s x (NxA st s x) := by + unfold isPMA + rw [hrv x hx, hfb _ hmO] + exact isPFA_congr (hsh x hx) (hbd x hx) (hsh _ hmO) (hbd _ hmO) + rw [is_mdb_parent_of_eqA] at he' ⊢ + rw [this] + exact he' + -- caps by kind + have same : ∀ y ∈ OS, ∀ cc : capability.Cap, isUtA cc = false → (capA st s2 y = cc ↔ capA st s y = cc) := by + intro y hy cc hcc + by_cases e : y = x + · rw [e, hcx, hcx'] + constructor <;> (intro h; rw [← h] at hcc; cases hcc) + · rw [kc y hy e] + have kept : ∀ (P' : capability.Cap → Prop), P' (.Untyped p sb off dev) → (∀ y ∈ OS, P' (capA st s y)) → + ∀ y ∈ OS, P' (capA st s2 y) := by + intro P' hu h y hy + by_cases e : y = x + · rw [e, hcx']; exact hu + · rw [kc y hy e]; exact h y hy + -- words of the pages of `Pt` + have ptw : ∀ t ∈ Pt, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → L.mem s2 (wa t o) = L.mem s (wa t o) := + fun t ht o ho h8 => vsk _ (Or.inl ⟨t, ht, o, ho, h8, rfl⟩) + have pte : ∀ t ∈ Pt, ∀ j : U64, j.val < 512 → L.mem s2 (peAF t j) = L.mem s (peAF t j) := by + intro t ht j hj + exact ptw t ht _ (by rw [wm8_valA hj]; omega) (by rw [wm8_valA hj]; omega) + have image : S1aSlotImage st OS s s2 := by + have nr : ∀ y ∈ OS, S1aR2Reads (capA st s2 y) → y ≠ x := fun y _ h e => by + rw [e, hcx'] at h; exact h + refine ⟨fun y hy h => ⟨y, hy, (kc y hy (nr y hy h)).symm⟩, fun y hy h => ?_, fun y hy z hz' k hky hkz hr0 => ?_⟩ + · have ne : y ≠ x := fun e => by rw [e, hcx] at h; exact h + exact ⟨y, hy, kc y hy ne⟩ + · have ney : y ≠ x := fun e => by rw [e, hcx'] at hky; cases hky + have nez : z ≠ x := fun e => by rw [e, hcx'] at hkz; cases hkz + rw [kc y hy ney] at hky hr0 + rw [kc z hz' nez] at hkz + exact r2.capRefs.unique y hy z hz' k hky hkz hr0 + have hvsK : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → L.mem s2 b = L.mem s b := fun b hb => by + refine hw.mem_eq (J.vsOk.al b hb) ?_ + rintro (h | h) + · obtain ⟨-, q, k, hq, -, hin⟩ := J.vsOk.om b hb + exact C.inObj (Or.inr (Or.inr (Or.inr ⟨k, hq, rfl⟩))) hin.1 hin.2 h + · exact J.vsOk.os b hb (inOSx b h) + have hvsP := utVsPt_keepA J.sys.ent.vs hks hvsK + -- J5 + obtain ⟨⟨mdb, irqr, hact⟩, -, -⟩ := J.j5 + have j5' : J5A st Z H OS s2 σ2 m := by + refine ⟨⟨?_, ?_, ?_⟩, hsub, fun y hy hn => ?_⟩ + · exact mdb.recap_alike htr.lk htr.tree (fun y hy => hsh y (hGO y hy)) (fun y hy => hbd y (hGO y hy)) + (fun y hy => hnx y (hGO y hy)) (fun y hy => hrv y (hGO y hy)) (fun y hy => hfb y (hGO y hy)) + · exact irqr.keepIrq fun y hy irq => same y (hGO y hy) _ rfl + · refine hact.keepIrq (fun y hy irq h => (same y (hGO y hy) _ rfl).1 h) hks fun irq hirq => ?_ + exact footN _ (.irq irq hirq) + · have ne : y ≠ x := fun e => hn (e ▸ hxs) + exact (kc y hy ne).trans (hnul y hy hn) + -- J7 + have schedSub : ∀ q, SchedPageA st Z OS s2 q → SchedPageA st Z OS s q := by + intro q h + rcases h with h | h | h | h | h | h | h | ⟨u, hu, h⟩ + · exact Or.inl h + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr (Or.inl h)) + · exact Or.inr (Or.inr (Or.inr (Or.inl h))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inl h)))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl h))))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl h)))))) + · refine Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ⟨u, hu, ?_⟩)))))) + have ne : u ≠ x := fun e => by + rw [e, hcx'] at h + rcases h with ⟨_, _, _, h⟩ | ⟨_, _, h⟩ | ⟨_, _, h⟩ | ⟨_, _, _, h, -⟩ | ⟨_, _, h, -⟩ <;> cases h + rw [kc u hu ne] at h + exact h + have frameW : FrameWords st OS s2 = FrameWords st OS s := by + ext b + constructor <;> rintro ⟨al, y, hy, ptr, size, rights, x1, x2, x3, x4, bts, hcc, rest⟩ + · exact ⟨al, y, hy, ptr, size, rights, x1, x2, x3, x4, bts, (same y hy _ rfl).1 hcc, rest⟩ + · exact ⟨al, y, hy, ptr, size, rights, x1, x2, x3, x4, bts, (same y hy _ rfl).2 hcc, rest⟩ + have capc2 : S1aCapCol st H OS Ub s2 := by + refine ⟨fun b hb => ?_, ?_, fun y hy ptr size rights x1 x2 x3 x4 h => ?_⟩ + · obtain ⟨y, hy, ptr, size, rights, x1, x2, x3, x4, bts, hcc, rest⟩ := capc.uf b hb + exact ⟨y, hy, ptr, size, rights, x1, x2, x3, x4, bts, (same y hy _ rfl).2 hcc, rest⟩ + · have u : UserOffUt st OS (FrameWords st OS s) s := capc.uo + have off : dev = false → ∀ w ∈ FrameWords st OS s, ¬ (p.val ≤ w.val ∧ w.val < p.val + 2 ^ sb.val) := fun hd => + UserOffUt.noCh hO u hRev hx (by rw [← hd]; exact hcx) he + show UserOffUt st OS (FrameWords st OS s2) s2 + rw [frameW] + refine ⟨fun y hy pp bb ii h w hw' => ?_, fun y hy pp bb ii h hn w hw' => ?_⟩ + · by_cases e : y = x + · rw [e, hcx'] at h + injection h with e1 e2 e3 e4 + subst e1 e2 + intro g + exact off e4 w hw' ⟨by omega, g.2⟩ + · rw [kc y hy e] at h + exact u.1 y hy pp bb ii h w hw' + · by_cases e : y = x + · rw [e, hcx'] at h + injection h with e1 e2 e3 e4 + subst e1 e2 + exact off e4 w hw' + · have h' := h + rw [kc y hy e] at h' + refine u.2 y hy pp bb ii h' (fun ⟨hz', hsra⟩ => hn ?_) w hw' + rw [hnx y hy] + refine ⟨hz', ?_⟩ + rw [sraA_congr (show Fl4ProofsA64.P1.W7.R.ST.shapeA (.Untyped pp bb ii false) = + Fl4ProofsA64.P1.W7.R.ST.shapeA (.Untyped pp bb ii false) from rfl) + (hsh _ hz')] + exact hsra + · exact capc.fo y hy ptr size rights x1 x2 x3 x4 ((same y hy _ rfl).1 h) + have hic2 : HiCap511 st OS s2 := by + intro y hy pp mm aa h + have h' := (same y hy _ rfl).1 h + have hp : pp ∈ Pt := by + refine r2.highFootprint ⟨pp, ?_, Or.inl rfl⟩ + rw [r2.highRoots] + exact ⟨y, hy, mm, aa, h'⟩ + rw [ptw pp hp 4088#u64 (by simp) (by simp)] + exact hic y hy pp mm aa h' + have r22 : S1aR2 st H OS Pt Pp7 Tb7 Hr Te Kt Lv7 Re bits nA s2 := r2.ofImage hks vsk image + have kt2 : S1aKtUt st OS Kt s2 := by + intro k hk' y hy pp bb ii d h + by_cases e : y = x + · rw [e, hcx'] at h + injection h with e1 e2 e3 e4 + subst e1 e2 + exact kt k hk' x hx _ _ i _ hcx + · rw [kc y hy e] at h + exact kt k hk' y hy pp bb ii d h + have hik2 : ∃ KLv : U64 → Nat, KTab ar Kt KLv ∧ S1aHiK st OS Kt KLv s2 := by + obtain ⟨KLv, ktab2, hik⟩ := hkl + refine ⟨KLv, ktab2, fun t ht hl o ho h8 e => ?_, fun y hy R mm aa h o ho h8 e => ?_⟩ + · rw [ptw t (r2.place.tables (r2.kernelTables.1 ht)) o ho h8] at e ⊢ + exact hik.kt t ht hl o ho h8 e + · have h' := (same y hy _ rfl).1 h + have hp : R ∈ Pt := by + refine r2.highFootprint ⟨R, ?_, Or.inl rfl⟩ + rw [r2.highRoots] + exact ⟨y, hy, mm, aa, h'⟩ + rw [ptw R hp o (by omega) h8] at e ⊢ + exact hik.hdr y hy R mm aa h' o ho h8 e + have j72 : J7A st (J7R2A st ar nA) Z H OS s2 σ2 m := by + refine ⟨Pt, Ub, ?_, ?_, ?_, capc2, ?_, ?_, hic2, fun q h => sched q (schedSub q h), ?_, + kept (FrUbP Ub) (fun _ _ _ _ _ _ _ h => by cases h) frub, alub, vcub, + Pp7, Tb7, Hr, Te, Kt, Lv7, Re, bits, r22, kt2, hik2⟩ + · rw [hks]; exact vr + · intro j + have h0 := pool j + rw [vspace.asid_table.eq] at h0 ⊢ + refine (WP.spec_ok _).2 fun hne => ?_ + rw [hks, tabW j] at hne ⊢ + obtain ⟨y, hy, bb, h⟩ := (WP.spec_ok _).1 h0 hne + exact ⟨y, hy, bb, (same y hy _ rfl).2 h⟩ + · exact armc.ofTab ⟨hks, fun j => by rw [hks]; exact tabW j⟩ + · exact kept (VcCapP Z.V) (fun _ h => by cases h) vcc + · exact kept DevFrP (fun _ _ _ _ _ _ _ h => by cases h) frd + · rw [hks]; exact apart + -- the remaining record fields + have F : UtFiA st OS x p sb i off dev s s2 := ⟨kc, hcx, hcx'⟩ + have tcbAl : ∀ t ∈ Z.T, t.val % 4096 = 0 ∧ t.val + 4096 ≤ 2 ^ 64 := fun t ht => + ⟨P3.align t _ (ut_tcbThA J ht).1, (ut_tcbThA J ht).2⟩ + have fAl : ∀ t ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → Aligned (wa t K) := by + intro t ht K h1 h2 + obtain ⟨g1, g2⟩ := tcbAl t ht + have v : (wa t K).val = t.val + K.val := by rw [wa_valP]; exact Nat.mod_eq_of_lt (by omega) + show _ % 8 = 0 + omega + have sbAl : ∀ t ∈ Z.T, Aligned (wa t 3512#u64) ∧ Aligned (sbWordA t) ∧ Aligned (wa (sbWordA t) 8#u64) := by + intro t ht + obtain ⟨g1, g2⟩ := tcbAl t ht + have v := ut_sbValA J ht + refine ⟨fAl t ht _ (by simp) (by simp), ?_, ?_⟩ <;> show _ % 8 = 0 <;> omega + have rAl : ∀ r ∈ Rp, Aligned (rNextA r) ∧ Aligned (rPrevA r) := by + intro r hr' + obtain ⟨g1, -, g3⟩ := J.iplaced.e.rep r hr' + have e8 : (rPrevA r).val = r.val + 8 := by + unfold rPrevA layout.reply.PREV + rw [wa_valP]; exact Nat.mod_eq_of_lt (by simp; omega) + have e16 : (rNextA r).val = r.val + 16 := by + unfold rNextA layout.reply.NEXT + rw [wa_valP]; exact Nat.mod_eq_of_lt (by simp; omega) + constructor <;> show _ % 8 = 0 <;> omega + have xobj : ∀ w : U64, ∀ q k, H q = some k → (k ≠ .tcb ∧ ∀ z, k ≠ .other z) → InObj q k w → + (w = x ∨ w = wa x 8#u64) → False := by + intro w q k hq' hk' hin hwx + obtain ⟨-, q', k', hq'', g1, g2, -, g4⟩ := armc.case.slot x hx + have ne : q ≠ q' := fun e => by + rw [e, hq''] at hq' + cases hq' + rcases g4 with h | ⟨z, h⟩ + · exact hk'.1 h + · exact hk'.2 z h + have := hH.disj q k q' k' hq' hq'' ne + unfold InObj at hin + have : x.val ≤ w.val ∧ w.val < x.val + 16 := by rcases hwx with e | e <;> rw [e] <;> omega + omega + have sq2 : SchedQueuesInv st H.tcbs Z.Cs H.omega H s2 Z.Q Z.R := by + refine utFr_sqA (Sg := ∅) J.sq hw (fun y hy K h1 h2 h3 => ?_) (fun c' hc' o ho h8 => ?_) (fun _ hb => hb.elim) + (fun e hee => ?_) + · have hyT : y ∈ Z.T := by rw [← J.tcbs]; exact hy + refine ⟨fAl y hyT K h2 h3, ?_⟩ + obtain ⟨g1, g2⟩ := tcbAl y hyT + have v : (wa y K).val = y.val + K.val := by rw [wa_valP]; exact Nat.mod_eq_of_lt (by omega) + have hth' := (ut_tcbThA J hyT).1 + rintro (h | h) + · exact C.inObj (Or.inr (Or.inr (Or.inl ⟨_, hth', rfl⟩))) (by omega) (by simp only [ObjKindA.size]; omega) h + · have hb : x.val ≤ (wa y K).val ∧ (wa y K).val < x.val + 16 := by rcases h with e | e <;> rw [e] <;> omega + obtain ⟨k, hk', hsz, hv⟩ := slotVA_of P3 hth (j := blvA cte.2) (by + have := blvA_lt cte.2 + rw [cnode_size]; omega) + have ek : k = .cnode cte.2.length := Option.some.inj (hk'.symm.trans hth) + have ex : slotAddrA (specPtr qc) (BitVec.ofNat 64 (blvA cte.2)) = x := by + rw [← hxc] + unfold cslotAddrA + rw [hq] + exact (cn_slotV P3 hth rfl).1.symm + rw [ex] at hv + have ne : y ≠ qc := fun e => by rw [e, hth] at hth'; cases hth' + have := P3.apart y qc _ _ hth' hth ne + rw [ek] at hsz + simp only [ObjKindA.size] at this + rw [show 2 ^ (cte.2.length + 5) = (ObjKindA.cnode cte.2.length).size from rfl] at this + omega + · refine ⟨nodeAt_aligned (J.sq.nodes c' hc') ho h8, ?_⟩ + have v : (L.nodeAt s c' o).val = L.nodeBase s c' + o.val := nodeAt_val (J.sq.nodes c' hc') ho + rintro (h | h) + · rcases C.nodes c' hc' with g | g <;> omega + · have cw : W7.CapW H.tcbs H.omega H.frame (L.nodeAt s c' o) := by + rcases h with g | g <;> rw [g] + · exact hO.cw0 hx + · exact hO.cw hx (by simp) (by simp) + obtain ⟨⟨Q, R, qi⟩, -⟩ := J.sys.ent.bxi + exact capw_ne_node qi cw hc' ho rfl + · have hk' : ∃ k, H e = some k ∧ (k = .ep ∨ k = .ntfn) := by + rcases hee with h | h + · exact ⟨_, h, Or.inl rfl⟩ + · exact ⟨_, h, Or.inr rfl⟩ + obtain ⟨k, hk, hkk⟩ := hk' + have hk'' : k ≠ .tcb ∧ ∀ z, k ≠ .other z := by + rcases hkk with e' | e' <;> rw [e'] <;> exact ⟨by simp, fun _ => by simp⟩ + have hb4 : 4 ≤ k.bits := by rcases hkk with e' | e' <;> rw [e'] <;> simp [HKind.bits] + have h16 : 2 ^ 4 ≤ 2 ^ k.bits := Nat.pow_le_pow_right (by decide : 0 < 2) hb4 + have hal := hH.align e k hk + have hf := hH.fits e k hk + have h8 : 8 ∣ e.val := by + have : 2 ^ 3 ∣ 2 ^ k.bits := Nat.pow_dvd_pow 2 (by omega) + exact (show (8 : Nat) ∣ 2 ^ k.bits from this).trans (Nat.dvd_of_mod_eq_zero hal) + have word : ∀ b : U64, (b.val = e.val ∨ b.val = e.val + 8) → + Aligned b ∧ ¬ ((p.val ≤ b.val ∧ b.val < p.val + 2 ^ sb.val) ∨ b = x ∨ b = wa x 8#u64) := by + intro b hb + refine ⟨?_, ?_⟩ + · show b.val % 8 = 0 + omega + · have hin : InObj e k b := by unfold InObj; omega + rintro (h | h) + · exact C.inObj (Or.inr (Or.inr (Or.inr ⟨k, hk, rfl⟩))) hin.1 hin.2 h + · exact xobj b e k hk hk'' hin h + have eh : (epHeadA e).val = e.val := by simp [epHeadA] + have et : (epTSA e).val = e.val + 8 := by + unfold epTSA + have e8 : (layout.ep.TAIL_STATE : U64).val = 8 := by unfold layout.ep.TAIL_STATE; rfl + rw [wa_valP, e8] + exact Nat.mod_eq_of_lt (by omega) + exact ⟨(word _ (Or.inl eh)).1, (word _ (Or.inl eh)).2, (word _ (Or.inr et)).1, (word _ (Or.inr et)).2⟩ + have tab8 : PoolOff st H Z.Cs OS s2 := by + intro j + have h0 := J.poolOff j + rw [vspace.asid_table.eq] at h0 ⊢ + refine (WP.spec_ok _).2 fun hne asid => ?_ + rw [hks, tabW j] at hne ⊢ + have W := (WP.spec_ok _).1 h0 hne asid + refine { al := W.al, om := W.om, sg := W.sg, os := W.os, ks := ?_ } + have := W.ks + unfold RegionOffNodes LawfulStore.nodeBase at this ⊢ + rw [hks]; exact this + have sinv2 : SInvXA σ2 m n := by + refine utCn_sinvA J.sinv H2 ?_ + rw [hσ2, eabs] + exact ut_specIrq_setA J.sinv.irqN hk hwf hcsidx + have hheap2 : ∀ q : U64, p.val ≤ q.val → q.val < p.val + 2 ^ sb.val → σ2.kheap (specPtr q) = none := by + intro q h1 h2 + rcases H2.kh (specPtr q) with e | ⟨r, cs0, cs1, e1, -⟩ + · rw [e]; exact hheap q h1 h2 + · rw [hheap q h1 h2] at e1; cases e1 + have tabs : ∀ b, VsR (L.ksb s) Pt b → L.mem s2 b = L.mem s b := vsk + have unm : UnmPtSpecA st Z m OS s2 σ2 := by + have tab : ∀ ty pp, pp ∈ Pt → UnmTabA st Z m s σ ty pp → UnmTabA st Z m s2 σ2 ty pp := by + intro ty pp hp h + refine ⟨fun j hj => by rw [pte pp hp j hj]; exact h.inv j hj, ?_, h.off.of_ksA hks hcpu⟩ + obtain ⟨t, h1, h2, h3⟩ := h.spec + exact ⟨t, H2.keep h1 (fun _ _ e => by cases e), h2, h3⟩ + refine ⟨fun u hu pp asid va h => ?_, fun u hu pp asid h => ?_, fun u hu pp asid va h => ?_, + fun u hu pp asid h => ?_⟩ + · have h' := (same u hu _ rfl).1 h + exact tab _ _ (r2.pageTables u hu pp false asid va h') (J.unmPt.pt u hu pp asid va h') + · have h' := (same u hu _ rfl).1 h + exact tab _ _ (r2.nativeRoots u hu pp false asid h').2 (J.unmPt.vs u hu pp asid h') + · have h' := (same u hu _ rfl).1 h + have g := (r2.eptRoots u hu pp 0#u64 false asid va h').2 + have lo : pp ∈ Pt := g ⟨pp, rfl, Or.inl rfl⟩ + have hi : wa pp 4096#u64 ∈ Pt := g ⟨pp, rfl, Or.inr ⟨rfl, rfl⟩⟩ + have u0 := J.unmPt.s2 u hu pp asid va h' + refine ⟨fun j hj => ?_, ?_⟩ + · by_cases hj' : j.val < 512 + · rw [pte pp lo j hj']; exact u0.inv j hj + · obtain ⟨j', hj2, ej⟩ := peAF_hiA pp j (by omega) hj + rw [ej, pte _ hi j' hj2, ← ej]; exact u0.inv j hj + · obtain ⟨es, h1, h2⟩ := u0.spec + exact ⟨es, H2.keep h1 (fun _ _ e => by cases e), h2⟩ + · have h' := (same u hu _ rfl).1 h + have hr' : pp ∈ Hr := by rw [r2.highRoots]; exact ⟨u, hu, false, asid, h'⟩ + have hi : wa pp 4096#u64 ∈ Pt := r2.highFootprint ⟨pp, hr', Or.inr ⟨rfl, rfl⟩⟩ + have u0 := J.unmPt.hv u hu pp asid h' + refine ⟨fun j hj => by rw [pte _ hi j hj]; exact u0.inv j hj, ?_⟩ + obtain ⟨l1, h1, h2⟩ := u0.spec + exact ⟨l1, H2.keep h1 (fun _ _ e => by cases e), h2⟩ + have nullOrig2 : ∀ sl, specCapAtA σ2 sl = some .nullCap → σ2.isOriginalCap sl = false := by + intro sl h + rw [hor] + by_contra ho + have ho' : σ.isOriginalCap sl = true := by simpa using ho + have hsl := J.origLive sl ho' + obtain ⟨ac2, e2, hr2⟩ := ut_absRelA hJ2.1.st hsl + rw [h] at e2 + cases e2 + by_cases ex : cslotAddrA sl = x + · have es : sl = cte := cslotAddrA_inj P3 hsl hab (ex.trans hxc.symm) + rw [es] at h + unfold specCapAtA at h + rw [show σ2.kheap cte.1 = some (.cnode cte.2.length (Function.update cs cte.2 + (some (absCapA (.Untyped p sb off dev))))) from Function.update_self ..] at h + simp only [wf_updA _ hwf rfl, ↓reduceIte, Function.update_self] at h + rw [eabs] at h + cases h + · obtain ⟨ac, e1, hr1⟩ := ut_absRelA J.j.1.st hsl + have hsO := hsub _ (hsl.isSlot P3) + have ecap : capAtA (L.mem s2) (cslotAddrA sl) = capAtA (L.mem s) (cslotAddrA sl) := kc _ hsO ex + rw [ecap] at hr2 + have : ac = .nullCap := by + have h1 := hr1.2 + have h2 := hr2.2 + revert h1 h2 + cases capAtA (L.mem s) (cslotAddrA sl) with + | PageTable pp mm asid va => + cases mm + · intro h1 h2; exact h1.symm.trans h2 + · intro _ h2 + rcases CapEqA.eq_or_pt h2 with g | ⟨_, _, _, g⟩ <;> cases g + | _ => intro h1 h2; exact h1.symm.trans h2 + rw [this] at e1 + rw [J.nullOrig sl e1] at ho' + cases ho' + have tv : ∀ t ∈ Z.T, L.mem s2 (wa t 3664#u64) = L.mem s (wa t 3664#u64) := fun t ht => + ut_rd_tcbA hw hW H2 ht (fAl t ht _ (by simp) (by simp)) (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl rfl)))))) + -- D1's clauses: only slot `x`'s cap changed, from the untyped to the untyped + have capK : ∀ u ∈ OS, u ≠ x → capA st s2 u = capA st s u := fun u hu ne => kc _ hu ne + have capNU : ∀ u ∈ OS, ∀ cc : capability.Cap, isUtA cc = false → capA st s2 u = cc → u ≠ x ∧ capA st s u = cc := by + intro u hu cc hcc h + have ne : u ≠ x := fun e => by + rw [e, hcx'] at h + rw [← h] at hcc + simp [isUtA] at hcc + exact ⟨ne, (capK u hu ne).symm.trans h⟩ + have nodeK : ∀ c ∈ Z.Cs, ∀ o : U64, o.val < 4288 → o.val % 8 = 0 → + L.mem s2 (L.nodeAt s2 c o) = L.mem s (L.nodeAt s c o) := by + intro c hc o ho h8 + have e : L.nodeAt s2 c o = L.nodeAt s c o := by + unfold LawfulStore.nodeAt + rw [hks] + rw [e] + exact hw.mem_eq (nodeAt_aligned (J.sq.nodes c hc) ho h8) fun h => + hW _ h (Or.inl (Or.inl ⟨c, hc, o, ho, rfl⟩)) + have frBase2 : FrBaseApartA st OS s2 := by + intro y hy z hz fy kz + have ny : y ≠ x := fun e => by rw [e, hcx'] at fy; simp [isFrA] at fy + have nz : z ≠ x := fun e => by rw [e, hcx'] at kz; exact absurd kz.2.1 (by simp [isUtA]) + rw [capK y hy ny] at fy + rw [capK z hz nz] at kz + rw [capK y hy ny, capK z hz nz] + exact J.frBase y hy z hz fy kz + have devFr2 : DevFrDisjA st H OS s2 σ2 := by + intro y hy q size rights mapped ept asid va hc k hk + obtain ⟨-, hc0⟩ := capNU y hy _ (by simp [isUtA]) hc + obtain ⟨h1, dev, pg, h2⟩ := J.devFr y hy q size rights mapped ept asid va hc0 k hk + exact ⟨h1, dev, pg, H2.keep h2 (fun _ _ e => by cases e)⟩ + have finCov2 : FinCovA Z.th σ2 := by + have new : σ2.kheap cte.1 = some (.cnode cte.2.length (Function.update cs cte.2 + (some (absCapA (.Untyped p sb off dev))))) := Function.update_self .. + have old : ∀ q, q ≠ cte.1 → σ2.kheap q = σ.kheap q := fun q e => Function.update_of_ne e .. + refine ⟨fun q r cs1 hk1 w => ?_, fun q y hk1 => ?_⟩ + · by_cases e : q = cte.1 + · rw [e, new] at hk1 + cases hk1 + rw [e] + exact J.finCov.cnode _ _ _ hk hwf + · rw [old q e] at hk1 + exact J.finCov.cnode q r cs1 hk1 w + · by_cases e : q = cte.1 + · rw [e, new] at hk1 + cases hk1 + · rw [old q e] at hk1 + exact J.finCov.tcb q y hk1 + have idleAll2 : IdleAllA st ia ie Z.Cs m s2 := + J.idleAll.keepA (fun c hc => nodeK c hc _ (by simp [layout.node.IDLE_THREAD]) (by simp [layout.node.IDLE_THREAD])) + (fun c hc => nodeK c hc _ (by simp [layout.node.IDLE_SC]) (by simp [layout.node.IDLE_SC])) + have zTcb2 : ZombieTcbTA st H OS s2 := fun u hu q k hc => + J.zombieTcb u hu q k (capNU u hu _ (by simp [isUtA]) hc).2 + have zVc2 : ZombieTcbNoVcpuA st OS s2 := fun u hu q k hc => by + have hc0 := (capNU u hu _ (by simp [isUtA]) hc).2 + have hq : q ∈ Z.T := by rw [← J.tcbs]; exact J.zombieTcb u hu q k hc0 + rw [tv q hq] + exact J.zombieNoVcpu u hu q k hc0 + have bufOff2 : BufOffA st Z OS s2 Bf := by + refine ⟨J.bufOff.os, fun b hb => by rw [hks]; exact J.bufOff.vs b hb, fun b hb q hq => ?_, J.bufOff.vcw, + fun bp hbp => (J.bufOff.pin bp hbp).keepF fun t ht hO _ _ r x1 x2 x3 x4 e => ⟨ht, hO, r, x1, x2, x3, x4, by + rw [capK _ hO (fun e1 => by rw [e1, hcx] at e; cases e)]; exact e⟩⟩ + refine J.bufOff.page b hb q ?_ + unfold SchedPageA at hq ⊢ + rcases hq with hq | hq | hq | hq | hq | hq | hq | ⟨u, hu, hq⟩ + · exact Or.inl hq + · exact Or.inr (Or.inl hq) + · exact Or.inr (Or.inr (Or.inl hq)) + · exact Or.inr (Or.inr (Or.inr (Or.inl hq))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inl hq)))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl hq))))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl hq)))))) + · have ne : u ≠ x := fun e => by + rw [e, hcx'] at hq + simp at hq + rw [capK u hu ne] at hq + exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ⟨u, hu, hq⟩)))))) + have sidle2 : SIdleA σ2 m n := utCn_sidleA J.sidle H2 fun sl a h => by + by_cases es : sl = cte + · right + rw [es] at h + unfold specCapAtA at h + rw [show σ2.kheap cte.1 = some (.cnode cte.2.length (Function.update cs cte.2 + (some (absCapA (.Untyped p sb off dev))))) from Function.update_self ..] at h + simp only [wf_updA _ hwf rfl, ↓reduceIte, Function.update_self] at h + rw [eabs] at h + cases h + exact ⟨(fun _ _ e => by cases e), (fun _ e => by cases e), (fun _ _ _ e => by cases e)⟩ + · left + rw [← h] + unfold specCapAtA + by_cases e1 : sl.1 = cte.1 + · rw [e1, hk, show σ2.kheap cte.1 = some (.cnode cte.2.length (Function.update cs cte.2 + (some (absCapA (.Untyped p sb off dev))))) from Function.update_self ..] + have e2 : sl.2 ≠ cte.2 := fun e => es (Prod.ext e1 e) + simp only [hwf, wf_updA _ hwf rfl, ↓reduceIte, Function.update_of_ne e2] + · rw [show σ2.kheap sl.1 = σ.kheap sl.1 from Function.update_of_ne e1 ..] + have rec2 : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s2 a σ2 + m := + { sys := ⟨utFr_entA J.sys.ent blk2 hks hvsP.1, sysl2⟩ + j := hJ2 + tcbs := J.tcbs + scs := J.scs + sq := sq2 + ap := by rw [hks]; exact J.ap + pp := J.pp + ipcTh := J.ipcTh + s1 := J.s1 + ko := by rw [hks]; exact J.ko + ptH := J.ptH + tabInj := J.tabInj + poolOff := tab8 + objAlloc := utFi_objAllocA F J.objAlloc C.obj + idleAnchor := utFi_idleAnchorA F J.idleAnchor + vsVc := by rw [hks]; exact J.vsVc + sr := utFr_schedSRA hw hW H2 J.sr + scCov := utCn_scCovA J.scCov H2 + scSize := utCn_scSizeA J.scSize H2 + ep := utFr_epA hw hW H2 J.ep J.iplaced.e + ntfn := utFr_ntfnA hw hW H2 J.ntfn J.iplaced.n + reply := utFr_replyA hw hW H2 J.reply rAl + bufCover := utFr_bufCoverA hw hW H2 J.bufCover sbAl + bufNative := utFr_bufNativeA hw hW H2 J.bufNative fun t ht => (sbAl t ht).2 + iplaced := J.iplaced + ipcPlace := J.ipcPlace + idleNoFault := utCn_idleNoFaultA J.idleNoFault H2 + mcp := utFr_mcpA hw hW H2 J.mcp fun t ht => ⟨fAl t ht _ (by simp) (by simp), fAl t ht _ (by simp) (by simp)⟩ + yieldRel := utFr_yieldA hw hW H2 J.yieldRel fun t ht => fAl t ht _ (by simp) (by simp) + sinv := sinv2 + sidle := sidle2 + setsInH := J.setsInH + utIpc := J.utIpc + deadExt := utFi_deadExtA F hx J.deadExt H2 tv fun v hv => footN _ (.vcpu v _ hv (Or.inr rfl)) + specFree := utFi_specFreeA F J.specFree H2 hcdt hor hheap hlink horig + utBlk := utFi_utBlkA F hx J.utBlk + unmPt := unm + tabS1 := fun q h => (J.tabS1 q h).of_ksA hks hcpu + tabExt := J.tabExt.of_ksA hks hcpu + specZero := utFi_specZeroA F J.specZero H2 fun _ => hz + thPos := J.thPos + origLive := J.origLive + nullOrig := nullOrig2 + frBase := frBase2 + devFr := devFr2 + finCov := finCov2 + idleAll := idleAll2 + ipcHeapCov := J.ipcHeapCov + zombieNoVcpu := zVc2 + zombieTcb := zTcb2 + capTy := fun u hu => + ⟨fun q b x y z w hc => (J.capTy u hu).1 q b x y z w (capNU u hu _ (by simp [isUtA]) hc).2, + fun q b x y hc => (J.capTy u hu).2.1 q b x y (capNU u hu _ (by simp [isUtA]) hc).2, + fun q g hc => (J.capTy u hu).2.2.1 q g (capNU u hu _ (by simp [isUtA]) hc).2, + fun q sz hc => (J.capTy u hu).2.2.2.1 q sz (capNU u hu _ (by simp [isUtA]) hc).2, + fun cpu hc => (J.capTy u hu).2.2.2.2 cpu (capNU u hu _ (by simp [isUtA]) hc).2⟩ + scCpu := J.scCpu + scNZ := J.scNZ + vcRun := J.vcRun.keepA tv (fun v hv => footN _ (.vcpu v _ hv (Or.inr rfl))) + (fun v hv => footN _ (.vcpu v v hv (Or.inl rfl))) + (fun t ht => footN _ (.tcb t _ ht (Or.inr (Or.inr (Or.inr (Or.inr rfl)))))) + heapWin := J.heapWin + coresAll := J.coresAll + symL := sym2 + ptCapIn := fun y hy ptr asid va h => by + change capA st s2 y = _ at h + obtain ⟨-, h0⟩ := capNU y (hsub y hy) _ (by simp [isUtA]) h + exact J.ptCapIn y hy ptr asid va h0 + zwf := fun y hy q zt k h => J.zwf y hy q zt k (capNU y (hsub y hy) _ (by simp [isUtA]) h).2 + fhOK := fun t ht => by + have isS := isSlot_fhA ((J.sr.thT t).2 ht) + have was := J.fhOK t ht + have ne : slotIdx t layout.tcb.SLOT_FAULT_HANDLER ≠ x := fun e => by + rw [e, hcx] at was + rcases was with g | ⟨_, _, _, _, _, _, g, -⟩ <;> cases g + rw [capK _ (hsub _ isS) ne] + exact was + utWin := fun u hu q b f e => by + by_cases h : u = x + · rw [h, hcx'] at e + obtain ⟨e1, e2, -, e4⟩ := capability.Cap.Untyped.inj e + rw [← e1, ← e2] + exact J.utWin x hx p sb _ (e4 ▸ hcx) + · rw [kc u hu h] at e + exact J.utWin u hu q b f e + devWin := fun u hu => by + by_cases h : u = x + · subst h + refine ⟨fun q b f e => ?_, fun p' sz r m e a v c => by rw [hcx'] at c; cases c⟩ + rw [hcx'] at e + obtain ⟨e1, e2, -, e4⟩ := capability.Cap.Untyped.inj e + rw [← e1, ← e2] + exact (J.devWin u hx).1 p sb _ (e4 ▸ hcx) + · rw [kc u hu h] + exact J.devWin u hu + vcBack := vcBack_keepA J.vcRun.vtcb (fun v hv => footN _ (.vcpu v v hv (Or.inl rfl))) tv J.vcBack + runBack := runBack_keepA J.vcRun.run + (fun t ht => footN _ (.tcb t _ ht (Or.inr (Or.inr (Or.inr (Or.inr rfl)))))) + (fun v hv => footN _ (.vcpu v _ hv (Or.inr rfl))) J.runBack + vcSnap := J.vcSnap.keepA (fun v hv o ho h8 => by + have hH12 : H v = some (.other 12) := armc.vh v hv + have al := (vcw_notCapWA hH armc.vh J.vcOS hv ho h8).1 + have hrf := hH.fits v _ hH12 + simp only [show (HKind.other 12).bits = 12 from rfl, Nat.reducePow] at hrf + have ev : (wa v o).val = v.val + o.val := by rw [wa_valP]; exact Nat.mod_eq_of_lt (by omega) + refine hw.mem_eq al ?_ + rintro (h | h) + · exact C.inObj (Or.inr (Or.inr (Or.inr ⟨_, hH12, rfl⟩))) (a := wa v o) (by omega) + (by simp only [show (HKind.other 12).bits = 12 from rfl, Nat.reducePow]; omega) h + · exact J.vcOS _ ⟨v, hv, o, ho, h8, rfl⟩ (inOSx _ h)) + (fun v _ y hy => H2.back hy (fun _ _ e => by cases e)) + vcOS := J.vcOS + userFr := fun y hy q size rights mapped ept asid va hc => + H2.keep (J.userFr y hy q size rights mapped ept asid va ((same y hy _ rfl).1 hc)) (fun _ _ e => by cases e) + frTh := fun y hy q size rights x1 x2 x3 x4 bits hc hb => + J.frTh y hy q size rights x1 x2 x3 x4 bits ((same y hy _ rfl).1 hc) hb + bfSpec := J.bfSpec + schedValid := by + have tcbs : ∀ q y, σ2.kheap q = some (.tcb y) → σ.kheap q = some (.tcb y) := by + intro q y h + by_cases e : q = cte.1 + · rw [hσ2, e] at h + have := Option.some.inj ((Function.update_self (β := fun _ => Option KernelObject) _ _ _).symm.trans h) + cases this + · rw [hσ2] at h + exact (Function.update_of_ne e _ _).symm.trans h + refine J.schedValid.congr rfl rfl rfl rfl rfl rfl + (fun q y k h => H2.keep h (fun _ _ e => by cases e)) + (fun t y h => ⟨y, H2.keep h (fun _ _ e => by cases e), rfl⟩) + (fun t y h => ⟨y, tcbs t y h, rfl, rfl⟩) + vcSym1 := J.vcSym1.keepA J.vcRun tv fun v hv => footN _ (.vcpu v v hv (Or.inl rfl)) + vcSym2 := J.vcSym2.keepA J.vcRun (fun v hv => footN _ (.vcpu v _ hv (Or.inr rfl))) + (fun t ht => footN _ (.tcb t _ ht (Or.inr (Or.inr (Or.inr (Or.inr rfl)))))) + runVcpu := fun t ht h => by + rw [footN _ (.tcb t _ ht (Or.inr (Or.inr (Or.inr (Or.inr rfl)))))] at h + rw [footN _ (.tcb t _ ht (Or.inl rfl))] + exact J.runVcpu t ht h + blkQ := by + have obj : ∀ q o, (∀ sz cn, o ≠ .cnode sz cn) → σ.kheap q = some o → σ2.kheap q = some o := by + intro q o ne h + have e : q ≠ cte.1 := fun e => by + rw [e, hk] at h + exact ne _ _ (Option.some.inj h).symm + rw [hσ2] + exact (Function.update_of_ne e _ _).trans h + have tcbs : ∀ q y, σ2.kheap q = some (.tcb y) → σ.kheap q = some (.tcb y) := by + intro q y h + by_cases e : q = cte.1 + · rw [hσ2, e] at h + have := Option.some.inj ((Function.update_self (β := fun _ => Option KernelObject) _ _ _).symm.trans h) + cases this + · rw [hσ2] at h + exact (Function.update_of_ne e _ _).symm.trans h + refine J.blkQ.keepA (fun t ht => ?_) (fun t _ y h => Or.inr ⟨y, tcbs _ _ h, rfl⟩) + (fun q y h => obj q _ (fun _ _ g => by cases g) h) (fun q y h => obj q _ (fun _ _ g => by cases g) h) + refine ut_rd_keepA hw hW ?_ (Or.inr (Or.inr (Or.inl (Or.inr ⟨t, ht, Or.inr (Or.inl rfl)⟩)))) + have : Notif.tBlockA t = wa t 3336#u64 := rfl + rw [this] + exact fAl t ht _ (by simp) (by simp) + bufOff := bufOff2 + curVcpuNone := H2.vcur.trans J.curVcpuNone + domOK := J.domOK.keepS J.sr.toSchedSRN H2.domk hks hcpu (fun b hb hd => hw.mem_eq hb (fun h => by + rcases h with h | e | e + · unfold DomW at hd; rcases C.ks with g | g <;> omega + · rw [e] at hd; exact osd x (le_refl _) (by omega) hd + · rw [e] at hd + have v : (wa x 8#u64).val = x.val + (8#u64 : U64).val := by + rw [wa_valP]; exact Nat.mod_eq_of_lt (by have := hO.fit x hx; have e8 : (8#u64 : U64).val = 8 := rfl; omega) + have e8 : (8#u64 : U64).val = 8 := rfl + exact osd _ (by omega) (by omega) hd)) + ipiZero := J.ipiKeepOf hw (fun o ho h => by + obtain ⟨hv, hcl⟩ := J.sr.toSchedSRN.ipi_val ho + have nc := C.nodes _ J.sr.sch.cpuCs + unfold LawfulStore.nodeBase at nc + have osi : x.val ≤ (L.nodeAt s (L.cpu s) o).val → (L.nodeAt s (L.cpu s) o).val < x.val + 32 → False := + fun h1 h2 => J.ipi_capwA ho (Or.inr ⟨⟨x, hx, h1, h2⟩, id⟩) + rcases h with ⟨g1, g2⟩ | e | e + · omega + · have := congrArg UScalar.val e + exact osi (by omega) (by omega) + · have v : (wa x 8#u64).val = x.val + (8#u64 : U64).val := by + rw [wa_valP]; exact Nat.mod_eq_of_lt (by have := hO.fit x hx; have e8 : (8#u64 : U64).val = 8 := rfl; omega) + have e8 : (8#u64 : U64).val = 8 := rfl + have := congrArg UScalar.val e + exact osi (by omega) (by omega)) + ipcSorted := J.ipcSorted.of_q H2.qkeep + activeReplyScs := J.activeReplyScs.of_q H2.qkeep + bufVs := by + rw [hks, hvsP.2] + exact J.bufVs + j5 := j5' + j7 := j72 + vsOk := by + unfold UtVsOkA + rw [hks, hvsP.2] + exact J.vsOk + bufAl := fun t ht y hk => by + rcases H2.kh (specPtr t) with e | ⟨r, cs0, cs1, -, e2⟩ + · exact J.bufAl t ht y (e ▸ hk) + · rw [e2] at hk + cases hk + zombieBnd := fun y hy q t n hc => J.zombieBnd y hy q t n (capNU y hy _ (by simp [isUtA]) hc).2 + devUtOff := by + have old : ∀ u ∈ OS, ∀ q b fi' d, capA st s2 u = .Untyped q b fi' d → ∃ fi0, capA st s u = .Untyped q b fi0 d := by + intro u hu q b fi' d h + by_cases e : u = x + · rw [e, hcx'] at h + injection h with e1 e2 e3 e4 + subst e1 e2 e4 + exact ⟨i, by rw [e]; exact hcx⟩ + · exact ⟨fi', (capK u hu e).symm.trans h⟩ + refine ⟨fun u hu q b fi' h p' bytes hj => ?_, fun u hu q b fi' h => ?_, fun u hu q b fi' h => ?_, + fun u hu q b fi' h v hv q' b' fi'' h' => ?_⟩ + · obtain ⟨fi0, g⟩ := old u hu q b fi' true h + exact J.devUtOff.obj u hu q b fi0 g p' bytes hj + · obtain ⟨fi0, g⟩ := old u hu q b fi' true h + have r := J.devUtOff.ks u hu q b fi0 g + unfold RegionOffNodes LawfulStore.nodeBase at r ⊢ + rw [hks] + exact r + · obtain ⟨fi0, g⟩ := old u hu q b fi' true h + exact J.devUtOff.idle u hu q b fi0 g + · obtain ⟨fi0, g⟩ := old u hu q b fi' true h + obtain ⟨fi1, g'⟩ := old v hv q' b' fi'' false h' + exact J.devUtOff.sep u hu q b fi0 g v hv q' b' fi1 g' } + have hty : SlotTy st H.tcbs H.omega H.frame H Rq Nt OS s2 := by + refine P.ty.frame (x := x) (fun y hy ne => ?_) ?_ + · have f := hO.fit y hy + have hd := hO.disj y hy x hx ne + have v : ∀ o : U64, o.val < 32 → (wa y o).val = y.val + o.val := fun o ho => by + rw [wa_valP]; exact Nat.mod_eq_of_lt (by omega) + have out : ∀ b : U64, y.val ≤ b.val → b.val < y.val + 32 → + ¬ ((p.val ≤ b.val ∧ b.val < p.val + 2 ^ sb.val) ∨ b = x ∨ b = wa x 8#u64) := by + rintro b h1 h2 (h | h) + · rcases hslots y hy with g | g <;> omega + · have : x.val ≤ b.val ∧ b.val < x.val + 16 := by rcases h with e | e <;> rw [e] <;> omega + omega + have k : ∀ o : U64, o.val < 32 → o.val % 8 = 0 → L.mem s2 (wa y o) = L.mem s (wa y o) := fun o ho h8 => + hw.mem_eq (hO.al hy ho h8) (out _ (by rw [v o ho]; omega) (by rw [v o ho]; omega)) + exact ⟨hw.mem_eq (hO.al0 hy) (out _ (le_refl _) (by omega)), k 8#u64 (by simp) (by simp), + k 16#u64 (by simp) (by simp), k 24#u64 (by simp) (by simp)⟩ + · obtain ⟨h1, h2, h3⟩ := P.ty x hx + refine ⟨fun c0 hc0 => ?_, by rw [(hl x hx).1]; exact h2, by rw [(hl x hx).2]; exact h3⟩ + have enew : c0 = .Untyped p sb off dev := by + rw [CapRt.decode_eq] at hc0 + exact (okinj hc0).symm.trans hcx' + rw [enew] + refine ⟨⟨⟨fun _ _ _ _ _ _ h => ?_, fun _ _ _ _ h => ?_, fun _ _ h => ?_⟩, fun _ h => ?_, + fun _ _ _ h => ?_, fun _ _ _ _ h => ?_, fun _ _ _ h => ?_, fun _ h => ?_⟩, + fun _ _ _ _ h => ?_, fun _ _ _ h => ?_⟩ <;> cases h + have P2 : SysPlace st H Z.Cs H.frame Rq Nt OS s2 extra x := + { heap := P.heap, os := P.os, win := P.win, ty := hty, core0 := P.core0, ext := P.ext, src := P.src, + slotw := P.slotw, wu := by rw [hks]; exact P.wu } + have C2 : UtClrA st Z H s2 p.val (p.val + 2 ^ sb.val) := by + refine ⟨C.obj, fun c' hc' => ?_, by rw [hks]; exact C.ks⟩ + have := C.nodes c' hc' + unfold LawfulStore.nodeBase at this ⊢ + rw [hks]; exact this + refine ⟨σ2, ?_, rec2, sym2, C2, P2, hrun2, hcx', he2, H2, hcdt, hor, hheap2⟩ + exact hmem + +set_option maxHeartbeats 4000000 in +/-- **One chunk step of the reset keeps the record** (module docstring). -/ +theorem utChunk_stepA [Fact (n ≤ Fl4SpecA64.fl4A64Platform.numNodes)] + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {p sb i off len c x : U64} + (C : UtClrA st Z H s p.val (p.val + 2 ^ sb.val)) + {Rq Nt : Set U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (P : SysPlace st H Z.Cs H.frame Rq Nt OS s extra x) + (symL : SymL st H Z.Cs s) + (hrun : Runnable (L.mem s (stA (L.curW s)))) + (hcx : capA st s x = .Untyped p sb i false) + (he : cdt.ensure_no_children st s x = ok (.Ok ())) + (cte : CslotPtr) {qc : U64} (hq : cte.1 = specPtr qc) (hth : Z.th qc = some (.cnode cte.2.length)) + (hxc : cslotAddrA cte = x) + (hslots : ∀ y ∈ OS, y.val + 32 ≤ p.val ∨ p.val + 2 ^ sb.val ≤ y.val) + (hc : Aligned c) (hcv : c.val = p.val + off.val) (hlen : len.val % 8 = 0) + (hon : off.val + len.val ≤ 2 ^ sb.val) (hil : i.val ≤ off.val + len.val) + (hheap : ∀ q : U64, p.val ≤ q.val → q.val < p.val + 2 ^ sb.val → σ.kheap (specPtr q) = none) + (hlink : ∀ a' c', σ.cdt a' = some c' → ∀ q : U64, p.val ≤ q.val → q.val < p.val + 2 ^ sb.val → + a'.1 ≠ specPtr q ∧ c'.1 ≠ specPtr q) + (horig : ∀ a', σ.isOriginalCap a' = true → ∀ q : U64, p.val ≤ q.val → q.val < p.val + 2 ^ sb.val → + a'.1 ≠ specPtr q) + (hz : ∀ w : Word, p.val ≤ (fl4Ptr w).val → (fl4Ptr w).val < p.val + 2 ^ sb.val → + σ.machineState.underlyingMemory w = 0) : + ∃ s2 σ2, obj.Slot.set_cap st x (L.zr s c len) (.Untyped p sb off false) = ok s2 ∧ + ((), σ2) ∈ (setCap (Z := Unit) (absCapA (.Untyped p sb off false)) cte σ).1 ∧ + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s2 a σ2 m ∧ + SymL st H Z.Cs s2 ∧ UtClrA st Z H s2 p.val (p.val + 2 ^ sb.val) ∧ + SysPlace st H Z.Cs H.frame Rq Nt OS s2 extra x ∧ Runnable (L.mem s2 (stA (L.curW s2))) ∧ + capA st s2 x = .Untyped p sb off false ∧ cdt.ensure_no_children st s2 x = ok (.Ok ()) ∧ + UtCnStepA σ σ2 ∧ σ2.cdt = σ.cdt ∧ σ2.isOriginalCap = σ.isOriginalCap ∧ + (∀ q : U64, p.val ≤ q.val → q.val < p.val + 2 ^ sb.val → σ2.kheap (specPtr q) = none) := by + have hH := J.sys.ent.heap + have hO := J.sys.ent.os + have hx : x ∈ OS := P.src + have hMdb := J.sys.sysl.sys.stz.1.1 + have hRev := J.sys.sysl.sys.stz.1.2.1 + have hV := J.sys.sysl.sys.stz.1.2.2.1 + have hUZ := J.sys.sysl.sys.stz.1.2.2.2.1 + obtain ⟨hpal, h47, -⟩ := J.sys.sysl.sys.ur.utv hMdb hx hcx + have hfit : p.val + 2 ^ sb.val ≤ 2 ^ 64 := al_fit hpal (by omega) p.bv.isLt + have hoffle : off.val ≤ 2 ^ sb.val := by omega + have P3 : PlaceA Z.th (L.ksb s) := J.j.1.place + have hsub : ∀ y, IsSlotA Z.th y → y ∈ OS := J.j5.2.1 + have hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null := J.j5.2.2 + have htr : TreeA st OS Z.G.chains Z.G.par s := J.j.1.cdt.tree.grow hsub fun y hy hn => hnul y hy hn + have eO : OffR p.val (p.val + 2 ^ sb.val) OS = OS := + Set.ext fun y => ⟨fun h => h.1, fun h => ⟨h, hslots y h⟩⟩ + have hstep := W7.R.ST.rst_clrset (st := st) (Sg := H.frame) (OS := OS) (x := x) (p := p) (sb := sb) (i := i) + (off := off) (c := c) (n := len) hH hO hV (by rw [eO]; exact hx) (RstF.refl (st := st) s) hcx + (by rw [eO]; exact hUZ) hc hcv hlen hon hil hfit + obtain ⟨s2, hset, hf, hcx', hUZ'⟩ := spec_exists_dut hstep + rw [eO] at hf hUZ' + obtain ⟨hks, hcpu, kc, hl⟩ := hf + have hw1 : WritesIn st s (L.zr s c len) (fun b => c.val ≤ b.val ∧ b.val < c.val + len.val) := + writesIn_zr' hc hlen (by omega) + have hEnc : CapRt.EncWf (.Untyped p sb off false) := by + have w := CapRt.decodeF_encWf (L.mem s x).bv (L.mem s (wa x 8#u64)).bv + have e' : CapRt.decodeF (L.mem s x).bv (L.mem s (wa x 8#u64)).bv = .Untyped p sb i false := hcx + rw [e'] at w; exact w + have hw : WritesIn st s s2 (fun b => (p.val ≤ b.val ∧ b.val < p.val + 2 ^ sb.val) ∨ b = x ∨ b = wa x 8#u64) := by + obtain ⟨w0, w1, hen, -⟩ := CapRt.decode_encode _ hEnc + have es2 : s2 = L.wr (L.wr (L.zr s c len) x w0) (wa x 8#u64) w1 := okinj (hset.symm.trans (set_cap_eq hen)) + refine ⟨hks, hcpu, fun b hb hn => ?_⟩ + have a0x := hO.al0 hx + have a8x := hO.al hx (k := 8#u64) (by simp) (by simp) + rw [es2, L.mem_wr_ne a8x hb (fun e => hn (Or.inr (Or.inr (UScalar.eq_of_val_eq e.symm)))), + L.mem_wr_ne a0x hb (fun e => hn (Or.inr (Or.inl (UScalar.eq_of_val_eq e.symm))))] + exact hw1.mem_eq hb fun h => hn (Or.inl (by omega)) + have hcpuCs : L.cpu s ∈ Z.Cs := J.j.1.sch.cpuCs + have hcurH : H (L.curW s) ≠ none := by + have : L.curW s ∈ H.tcbs := by rw [J.tcbs]; exact J.sr.curT + rw [show H (L.curW s) = some .tcb from this]; simp + have hcscH : H (L.cscW s) ≠ none := by + have : L.cscW s ∈ H.scs := by rw [J.scs]; exact J.j.1.cscC _ hcpuCs + obtain ⟨z, hz'⟩ := this + rw [hz']; simp + have hclrH : ∀ q k, H q = some k → ¬ RegionD H p.val (p.val + 2 ^ sb.val) q := by + intro q k h hR + have g := C.obj q _ (Or.inr (Or.inr (Or.inr ⟨k, h, rfl⟩))) + unfold RegionD MeetsR at hR + rw [h] at hR + simp only [Option.elim] at hR + omega + have hr : obj.Slot.revocable st x s = ok true := hRev x hx (by rw [hcx]; rfl) + have hndA : ∀ G par, TreeA st OS G par s → ∀ y, ¬ Fl4Proofs.P3.Mdb.Desc par y x := fun G par h y => + cdt.ensure_no_children.noDesc hO h hx hcx hr he y + have hsl := utFr_syslA (s' := s2) (i := off) P J.sys.ent.bxi.blk J.sys.sysl symL J.sys.sysl.ly.h0 hclrH hslots + hcurH hcscH hrun hcx hndA hw kc hcx' hoffle hUZ' + obtain ⟨σ2, h⟩ := utChunk_coreA J C P hrun hcx he cte hq hth hxc hslots hcv hon hheap hlink horig hz + (L.zr s c len) s2 hw1 hset hks hcpu kc hl hcx' hsl + exact ⟨s2, σ2, hset, h⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtClosA.lean b/proofs/Fl4ProofsA64/P3/UtClosA.lean new file mode 100644 index 000000000..d1314236c --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtClosA.lean @@ -0,0 +1,535 @@ +/- +P3 (aarch64, A64-P3 Untyped `UtDropA`, closure producers; Architect 00:06Z freeze): **the closure premises of +`UtDropA`'s field lemmas from A64 P1's `DropOK`** (QueueHeapDrop) and the unified record's facts. + +* `utD_memA`: the removed spec addresses of a region `[p, p + 2^sb)` are exactly the region's. +* `utD_bridgeA`: on the heap's objects, "removed" is P1's `RegionD`. +* The producers: SC links, queues, replies, yields, endpoints / notifications, the current core's objects, the + interrupt CNodes. + +Premises of this module: +* **[J] `UtIpcHeapA`** (`P3/UtIpcHeapA`, a record field): the IPC ghosts' endpoints, notifications and replies are + P1 heap objects of those kinds. + The record has no such field (`JStage3A` places them only in the typed heap). +* The kind-`tcb` / `sc` sets of the record: `Z.T = H.tcbs`, `Z.C = H.scs` (`JEntA`). +Other cores' current and idle objects are not produced here (closure items B [S] and C). New lemmas only. +-/ +import Fl4ProofsA64.P3.UtDropA +import Fl4ProofsA64.P1.MdbLive +import Fl4ProofsA64.P1.MdbFrC +import Fl4ProofsA64.P1.QueueDecUt +import Fl4ProofsA64.P3.UtIpcHeapA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## The removed spec addresses of a region -/ + +/-- The spec addresses spec `deleteObjects` removes for the region at `p` of size `2^sb`. -/ +def utDA (p : U64) (sb : Nat) : Set Fl4SpecA64.ObjRef := {x | specPtr p ≤ x ∧ x ≤ specPtr p + 2 ^ sb - 1} + +theorem utD_memA {p : U64} {sb : Nat} (hsb : sb < 64) (hp : p.val + 2 ^ sb ≤ 2 ^ 64) + (hsp : (specPtr p).toNat + 2 ^ sb ≤ 2 ^ 64) (q : U64) : + specPtr q ∈ utDA p sb ↔ p.val ≤ q.val ∧ q.val < p.val + 2 ^ sb := by + have h1 : 1 ≤ 2 ^ sb := Nat.one_le_two_pow + have hlt : 2 ^ sb < 2 ^ 64 := Nat.pow_lt_pow_right (by omega) hsb + have hpow : (2 ^ sb : BitVec 64).toNat = 2 ^ sb := by + rw [BitVec.toNat_pow, show (2 : BitVec 64).toNat = 2 from rfl, Nat.mod_eq_of_lt hlt] + have htop : (specPtr p + 2 ^ sb - 1).toNat = (specPtr p).toNat + 2 ^ sb - 1 := by + have e : specPtr p + 2 ^ sb - 1 = specPtr p + (2 ^ sb - 1) := by bv_omega + have h2 : (2 ^ sb - 1 : BitVec 64).toNat = 2 ^ sb - 1 := by + rw [BitVec.toNat_sub_of_le (by rw [BitVec.le_def, hpow]; exact h1), hpow] + rfl + rw [e, BitVec.toNat_add, h2, Nat.mod_eq_of_lt (by omega)] + omega + have hq : q.val < 2 ^ 64 := q.bv.isLt + have hpl : p.val < 2 ^ 64 := p.bv.isLt + have hsq : (specPtr q).toNat = ((specPtr p).toNat + (q.val + 2 ^ 64 - p.val)) % 2 ^ 64 := by + have e : specPtr q = specPtr p + (q.bv - p.bv) := by unfold specPtr; bv_omega + rw [e, BitVec.toNat_add, BitVec.toNat_sub] + have e1 : q.bv.toNat = q.val := rfl + have e2 : p.bv.toNat = p.val := rfl + rw [e1, e2, Nat.add_mod_mod] + congr 2 + omega + have hs : (specPtr p).toNat < 2 ^ 64 := (specPtr p).isLt + show specPtr p ≤ specPtr q ∧ specPtr q ≤ specPtr p + 2 ^ sb - 1 ↔ _ + rw [BitVec.le_def, BitVec.le_def, htop, hsq] + by_cases h : p.val ≤ q.val + · have e : ((specPtr p).toNat + (q.val + 2 ^ 64 - p.val)) % 2 ^ 64 = + ((specPtr p).toNat + (q.val - p.val)) % 2 ^ 64 := by + rw [show (specPtr p).toNat + (q.val + 2 ^ 64 - p.val) = (specPtr p).toNat + (q.val - p.val) + 2 ^ 64 by omega, + Nat.add_mod_right] + rw [e] + by_cases hw : (specPtr p).toNat + (q.val - p.val) < 2 ^ 64 + · rw [Nat.mod_eq_of_lt hw] + omega + · rw [Nat.mod_eq_sub_mod (by omega), Nat.mod_eq_of_lt (by omega)] + omega + · by_cases hw : (specPtr p).toNat + (q.val + 2 ^ 64 - p.val) < 2 ^ 64 + · rw [Nat.mod_eq_of_lt hw] + omega + · rw [Nat.mod_eq_sub_mod (by omega), Nat.mod_eq_of_lt (by omega)] + omega + +/-- **On heap objects, removed is `RegionD`.** `hin`: objects meeting the region lie in it (`UtRV.rin`). -/ +theorem utD_bridgeA {H : Heap} {p : U64} {sb : Nat} (hsb : sb < 64) (hp : p.val + 2 ^ sb ≤ 2 ^ 64) + (hsp : (specPtr p).toNat + 2 ^ sb ≤ 2 ^ 64) (hin : RegionIn H p.val (p.val + 2 ^ sb)) : + ∀ q k, H q = some k → (specPtr q ∈ utDA p sb ↔ RegionD H p.val (p.val + 2 ^ sb) q) := by + intro q k hq + have hk : 1 ≤ 2 ^ k.bits := Nat.one_le_two_pow + have hR : RegionD H p.val (p.val + 2 ^ sb) q ↔ MeetsR p.val (p.val + 2 ^ sb) q.val (q.val + 2 ^ k.bits) := by + unfold RegionD + rw [hq] + rfl + rw [utD_memA hsb hp hsp q, hR] + constructor + · intro h + unfold MeetsR + omega + · intro h + obtain ⟨h1, h2⟩ := hin q k hq h + omega + +/-! ## The IPC ghosts in the heap -/ + +theorem ut_even_maskA {w : U64} (h : w.val % 2 = 0) : w &&& ~~~1#u64 = w ∧ headBit w = false := by + have h0 : w.bv &&& 1#64 = 0#64 := by + apply BitVec.eq_of_toNat_eq + rw [BitVec.toNat_and, show (1#64).toNat = 1 from rfl, Nat.and_one_is_mod] + exact h + constructor + · apply UScalar.eq_of_val_eq + have : (w &&& ~~~1#u64).bv = w.bv := by + change w.bv &&& ~~~(1#64) = w.bv + apply BitVec.eq_of_getLsbD_eq + intro i hi + have hb := congrArg (fun z : BitVec 64 => z.getLsbD i) h0 + simp only [BitVec.getLsbD_and, BitVec.getLsbD_zero] at hb + rw [BitVec.getLsbD_and, BitVec.getLsbD_not] + by_cases h1 : (1#64).getLsbD i = true + · rw [h1] at hb ⊢ + simp only [Bool.and_true] at hb + simp [hb] + · simp only [Bool.not_eq_true] at h1 + rw [h1] + simp [hi] + exact congrArg BitVec.toNat this + · unfold headBit + have : (w &&& 1#u64) = 0#u64 := by + apply UScalar.eq_of_val_eq + have : (w &&& 1#u64).bv = 0#64 := by + rw [UScalar.bv_and] + exact h0 + exact congrArg BitVec.toNat this + rw [this] + rfl + +/-! ## The producers -/ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {H : Heap} {R : U64 → Prop} {Cs : Set U64} {s : S} {D : Set Fl4SpecA64.ObjRef} + +/-- A heap object not removed in the spec is not dropped from the heap, and conversely. -/ +abbrev UtBrA (H : Heap) (R : U64 → Prop) (D : Set Fl4SpecA64.ObjRef) : Prop := + ∀ q k, H q = some k → (specPtr q ∈ D ↔ R q) + +theorem ut_keptA {q : U64} {k : HKind} (hbr : UtBrA H R D) (hq : H q = some k) (h : ¬ R q) : specPtr q ∉ D := + fun hd => h ((hbr q k hq).1 hd) + +theorem ut_keptRefA {x : U64} {k : HKind} (hbr : UtBrA H R D) (hk : KeptRef H R x) (hx : H x = some k) : + specPtr x ∉ D := ut_keptA hbr hx (hk (by rw [hx]; simp)) + +/-- A TCB that is not inactive is retained. -/ +theorem utClos_liveA (DK : DropOK st H R Cs s) (hbr : UtBrA H R D) : + ∀ t ∈ H.tcbs, (L.mem s (stA t)).val ≠ 0 → specPtr t ∉ D := fun t ht h => + ut_keptA hbr ht (DK.keepT ht (fun e => h (by rw [e]; simp [sched.TS_INACTIVE]))) + +/-- A retained TCB's SC word names a retained SC (`utDrop_schedRestA`'s `hsc`). -/ +theorem utClos_scA (DK : DropOK st H R Cs s) (hbr : UtBrA H R D) {Ω Sg : Set U64} {Ac : Set U64} + (hB : BXI st H.tcbs Cs Ω H Sg H.replies H.scs H.ntfns H.scBits Ac s) : + ∀ t ∈ H.tcbs, L.mem s (scA t) ≠ 0#u64 → specPtr (L.mem s (scA t)) ∉ D := by + obtain ⟨-, -, hX, -, -⟩ := hB + intro t ht h0 + obtain ⟨h1, h2, -⟩ := hX.2.2.bound t ht h0 + obtain ⟨z, hz⟩ := h1 + have htH : H t = some .tcb := ht + exact ut_keptA hbr hz (DK.keepSc ⟨z, hz⟩ (Or.inl (by rw [h2]; exact DK.ne0 (by rw [htH]; simp)))) + +/-- A retained receiver's reply is retained (`utDrop_epStateA`'s `hr`). -/ +theorem utClos_recvA (DK : DropOK st H R Cs s) (hbr : UtBrA H R D) {Ω Sg : Set U64} {Ac : Set U64} + {Z : U64 → Nat} {Sc Nt : Set U64} + (hB : BXI st H.tcbs Cs Ω H Sg H.replies Sc Nt Z Ac s) : + ∀ t ∈ H.tcbs, (L.mem s (stA t)).val = 3 → L.mem s (tReplyA t) ≠ 0#u64 → + specPtr (L.mem s (tReplyA t)) ∉ D := by + obtain ⟨-, -, hX, -, -⟩ := hB + intro t ht h3 h0 + have hst : L.mem s (stA t) = sched.TS_BLOCKED_ON_RECV := + UScalar.eq_of_val_eq (by rw [h3]; simp [sched.TS_BLOCKED_ON_RECV]) + have e : tReplyA t = replyA t := rfl + rw [e] at h0 ⊢ + obtain ⟨hr, hcall⟩ := hX.2.1.recv t ht hst h0 + have htH : H t = some .tcb := ht + exact ut_keptA hbr hr (DK.keepR hr (by rw [hcall]; exact DK.ne0 (by rw [htH]; simp))) + +/-- The FL4 ready and release queues hold retained TCBs (`utDrop_schedCoreA`'s `hQ` / `hR`; closure item `DeadQ`). -/ +theorem utClos_queuesA (DK : DropOK st H R Cs s) (hbr : UtBrA H R D) {Ω : Set U64} {Q : U64 → U64 → List U64} + {Rq : U64 → List U64} (h : SchedQueuesInv st H.tcbs Cs Ω H s Q Rq) : + (∀ c ∈ Cs, ∀ p : U64, p.val < 256 → ∀ t ∈ Q c p, specPtr t ∉ D) ∧ (∀ c ∈ Cs, ∀ t ∈ Rq c, specPtr t ∉ D) := by + have run : ∀ t ∈ H.tcbs, Runnable (L.mem s (stA t)) → specPtr t ∉ D := by + intro t ht hr + refine utClos_liveA DK hbr t ht ?_ + rcases hr with e | e | e <;> rw [e] <;> + simp [sched.TS_RUNNING, sched.TS_RESTART, sched.TS_RUNNING_VM] + refine ⟨fun c hc p hp t ht => ?_, fun c hc t ht => ?_⟩ + · exact run t (((h.ready c hc).1.rel p hp).mem t ht) (h.runQ c hc p hp t ht) + · exact run t ((h.release c hc).mem t ht) (h.runR c hc t ht) + +/-- The current thread, the current SC and a pending switch target are retained (the current core). -/ +theorem utClos_curA (DK : DropOK st H R Cs s) (hbr : UtBrA H R D) {Ω : Set U64} {Q : U64 → U64 → List U64} + {Rq : U64 → List U64} (h : SchedQueuesInv st H.tcbs Cs Ω H s Q Rq) (hcsc : L.cscW s ∈ H.scs) : + specPtr (L.curW s) ∉ D ∧ specPtr (L.cscW s) ∉ D ∧ specPtr (L.idleW s) ∉ D ∧ + (∀ u ∈ H.tcbs, L.actionW s = u → u ≠ 0#u64 → specPtr u ∉ D) := by + have htc : H (L.curW s) = some .tcb := h.curT + have hid : H (L.idleW s) = some .tcb := h.idleT + obtain ⟨z, hz⟩ := hcsc + refine ⟨ut_keptA hbr htc DK.cur, ut_keptA hbr hz DK.csc, ?_, fun u hu e _ => ?_⟩ + · refine ut_keptA hbr hid fun hD => ?_ + exact ((DK.deadOf (by rw [hid]; simp) hD).1 hid).2.2.2.2.1 rfl + · have huH : H u = some .tcb := hu + refine ut_keptA hbr huH fun hD => ?_ + exact ((DK.deadOf (by rw [huH]; simp) hD).1 huH).2.2.2.1 e.symm + +/-- The yield links (`utDrop_yieldA`'s `hto`). -/ +theorem utClos_yieldToA (DK : DropOK st H R Cs s) (hbr : UtBrA H R D) : + ∀ t ∈ H.tcbs, specPtr t ∉ D → L.mem s (yieldA t) ∈ H.scs → specPtr (L.mem s (yieldA t)) ∉ D := by + intro t ht hk ⟨z, hz⟩ + have htH : H t = some .tcb := ht + exact ut_keptRefA hbr (DK.yto t ht (fun hr => hk ((hbr t _ htH).2 hr))) hz + +/-- A retained SC's yielder is retained (`utDrop_yieldA`'s `hfrom`). -/ +theorem utClos_yieldFromA (DK : DropOK st H R Cs s) (hbr : UtBrA H R D) {m : Fl4SpecA64.McState} + {σ : Fl4SpecA64.State Unit} (h : YieldRelA H.tcbs H.scs m (L.mem s) σ) : + ∀ c ∈ H.scs, specPtr c ∉ D → ∀ y n, σ.kheap (scNameA m c) = some (.schedContext y n) → + ∀ p, y.scYieldFrom = some p → p ∉ D := by + intro c hc _ y n hy p hp + obtain ⟨t, ht, rfl, hm⟩ := h.frm c hc y n hy p hp + have htH : H t = some .tcb := ht + refine ut_keptA hbr htH fun hD => ?_ + have h0 := ((DK.deadOf (by rw [htH]; simp) hD).1 htH).2.2.2.2.2 + obtain ⟨z, hz⟩ := hc + rw [h0] at hm + rw [← hm, DK.h0] at hz + cases hz + +/-- A retained notification's bound TCB is retained (`utDrop_ntfnStateA`'s `hb`). -/ +theorem utClos_ntfnBoundA (DK : DropOK st H R Cs s) (hbr : UtBrA H R D) : + ∀ n ∈ H.ntfns, specPtr n ∉ D → L.mem s (nBoundA n) ∈ H.tcbs → specPtr (L.mem s (nBoundA n)) ∉ D := by + intro n hn hk ht + have hnH : H n = some .ntfn := hn + have e : nBoundA n = ntfnBtA n := rfl + rw [e] at ht ⊢ + exact ut_keptRefA hbr (DK.ntb n hn (fun hr => hk ((hbr n _ hnH).2 hr))) (show H _ = some .tcb from ht) + +/-- A retained reply's links are retained (`utDrop_replyStackA`'s `hlk`). -/ +theorem utClos_linksA (DK : DropOK st H R Cs s) (hbr : UtBrA H R D) : + ∀ r ∈ H.replies, specPtr r ∉ D → ∀ w, (w = L.mem s (rNextA r) ∨ w = L.mem s (rPrevA r)) → w ∈ H.replies → + specPtr w ∉ D := by + intro r hr hk w hw hwr + have hrH : H r = some .reply := hr + have hwH : H w = some .reply := hwr + have hnR : ¬ R r := fun h => hk ((hbr r _ hrH).2 h) + have hal : w.val % 2 = 0 := by + have := DK.heap.align w _ hwH + simp only [HKind.bits] at this + omega + obtain ⟨e1, e2⟩ := ut_even_maskA hal + rcases hw with h | h + · have k := DK.rnext r hr hnR (by rw [← h]; exact e2) + rw [← h, e1] at k + exact ut_keptRefA hbr k hwH + · have k := DK.rprev r hr hnR + rw [← h, e1] at k + exact ut_keptRefA hbr k hwH + +/-- **Every reply on an SC's stack is retained, and so is the SC** (`utDrop_replyStackA`'s `hstk`): the stack's +replies are blocked replies (`ValidReplies`), and a removed SC has an empty stack (`DeadQ`). -/ +theorem utClos_stackA (DK : DropOK st H R Cs s) (hbr : UtBrA H R D) (VR : ValidReplies st H s) + {Rp : Set U64} {m : Fl4SpecA64.McState} {σ : Fl4SpecA64.State Unit} {stack : U64 → List U64} + (hRp : ∀ r ∈ Rp, H r = some .reply) (h : ReplyStackRelA (L.mem s) Rp H.scs σ (scNameA m) stack) + (hsc : ScStateRelA st H.scs m s σ) : + ∀ sc ∈ H.scs, ∀ r ∈ stack sc, (specPtr sc ∉ D ↔ specPtr r ∉ D) := by + intro sc hsc' r hr + obtain ⟨⟨x, n, hx, hxr⟩, hmem, -, hw⟩ := h.stackRel sc hsc' + obtain ⟨y, k, hy, hrel⟩ := hsc.sc sc hsc' + rw [hx] at hy + cases hy + have kept : ∀ q ∈ H.replies, RBlk st s q → specPtr q ∉ D := fun q hq hb => + ut_keptA hbr hq (DK.keepR hq hb.tcb) + -- the head is the SC's reply word + have hne : stack sc ≠ [] := fun e => by rw [e] at hr; exact List.not_mem_nil hr + have hpos : 0 < (stack sc).length := List.length_pos_iff.2 hne + have hhead : L.mem s (scWA sc 32#u64) = (stack sc)[0] := by + have h1 := hrel.reply + rw [hxr, List.head?_map, List.head?_eq_getElem?, List.getElem?_eq_getElem hpos] at h1 + unfold optRefS at h1 + split at h1 + · cases h1 + · exact (specPtr_inj (Option.some.inj h1)).symm + have hr0 : H ((stack sc)[0]) = some .reply := hRp _ (hmem _ (List.getElem_mem hpos)) + have h0ne : (stack sc)[0] ≠ 0#u64 := fun e => by rw [e, DK.h0] at hr0; cases hr0 + have eA : scReplyA sc = scWA sc 32#u64 := by + unfold scReplyA scWA + simp [layout.sc.REPLY] + have all : ∀ i (hi : i < (stack sc).length), RBlk st s ((stack sc)[i]) := by + intro i + induction i with + | zero => + intro _ + have := VR.screp sc hsc' (by rw [eA, hhead]; exact h0ne) + rwa [eA, hhead] at this + | succ i ih => + intro hi + have hi' : i < (stack sc).length := by omega + have hri : H ((stack sc)[i]) = some .reply := hRp _ (hmem _ (List.getElem_mem hi')) + have hrj : H ((stack sc)[i + 1]) = some .reply := hRp _ (hmem _ (List.getElem_mem hi)) + have hp := (hw i hi').1 + rw [List.getD_eq_getElem?_getD, List.getElem?_eq_getElem hi, Option.getD_some] at hp + have hal : ((stack sc)[i + 1]).val % 2 = 0 := by + have := DK.heap.align _ _ hrj + simp only [HKind.bits] at this + omega + have e1 := (ut_even_maskA hal).1 + have hne' : (stack sc)[i + 1] ≠ 0#u64 := fun e => by rw [e, DK.h0] at hrj; cases hrj + have := VR.rprev _ hri (by rw [hp, e1]; exact hne') + rwa [hp, e1] at this + obtain ⟨i, hi, rfl⟩ := List.mem_iff_getElem.1 hr + have h1 : specPtr (stack sc)[i] ∉ D := kept _ (hRp _ (hmem _ hr)) (all i hi) + have h2 : specPtr sc ∉ D := by + obtain ⟨z, hz⟩ := hsc' + refine ut_keptA hbr hz fun hD => ?_ + have d := (DK.deadOf (by rw [hz]; simp) hD).2.2.2.2 z hz + exact h0ne (by rw [← hhead, ← eA]; exact d.2.1) + exact iff_of_true h2 h1 + +/-- The spec's references of retained objects (`UtSpecClosedA`). -/ +theorem utClos_specA (DK : DropOK st H R Cs s) (hbr : UtBrA H R D) {Z : XSetsA} {m : Fl4SpecA64.McState} + {κ : U64 → U64} {σ : Fl4SpecA64.State Unit} (_ : Z.T = H.tcbs) (hC : Z.C = H.scs) (_ : Z.Cs = Cs) + {Ω : Set U64} (hq : SchedQueuesInv st H.tcbs Cs Ω H s Z.Q Z.R) (h : SchedSRA st Z m κ s σ) + (hcov : ScCovA Z.C m σ) (hA : Fl4SpecA64.idleScPtr ∉ D) : UtSpecClosedA D σ := by + obtain ⟨hQ, hR⟩ := utClos_queuesA DK hbr hq + have hcpu : L.cpu s ∈ Cs := hq.cur + refine ⟨fun u x p _ hu hsc => ?_, fun u hu => ?_, fun d p u hu => ?_⟩ + · obtain ⟨y, n, hp, hy⟩ := h.inv.back u x p hu hsc + obtain ⟨c, hc, rfl⟩ := hcov p y n hp + obtain ⟨y', n', hp', hy'⟩ := h.sch.sc c hc + have e : scRenA m c = scNameA m c := rfl + rw [e, hp] at hp' + cases hp' + rw [hy] at hy' + have h0 : L.mem s (wa c 24#u64) ≠ 0#u64 := fun e0 => by + rw [optRefA, ite_eq_left e0] at hy' + cases hy' + have hcH : c ∈ H.scs := hC ▸ hc + have eA : scTcbA c = wa c 24#u64 := by + unfold scTcbA + simp [layout.sc.TCB] + obtain ⟨z, hz⟩ := hcH + exact scName_keptA hA (ut_keptA hbr hz (DK.keepSc ⟨z, hz⟩ (Or.inl (by rw [eA]; exact h0)))) + · rw [h.rest.rel] at hu + obtain ⟨t, ht, rfl⟩ := List.mem_map.1 hu + exact hR _ hcpu t ht + · by_cases hd : d = 0 + · subst hd + obtain ⟨p', hp'⟩ : ∃ p' : U64, p' = ⟨BitVec.ofNat 64 p.toNat⟩ := ⟨_, rfl⟩ + have hv : p'.val = p.toNat := by + rw [hp'] + show (BitVec.ofNat 64 p.toNat).toNat = _ + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by have := p.isLt; omega)] + have hlt : p'.val < 256 := by rw [hv]; exact p.isLt + have e : BitVec.ofNat 8 p'.val = p := by rw [hv]; simp + have := h.rest.rq p' hlt + rw [e] at this + rw [this] at hu + obtain ⟨t, ht, rfl⟩ := List.mem_map.1 hu + exact hQ _ hcpu p' hlt t ht + · rw [h.rest.rqDom d p hd] at hu + exact absurd hu List.not_mem_nil + +/-- The interrupt CNodes are retained: they lie in the state block, which the region misses (`KsOffA`). -/ +theorem utClos_irqA {p : U64} {sb : Nat} (hsb : sb < 64) (hp : p.val + 2 ^ sb ≤ 2 ^ 64) + (hsp : (specPtr p).toNat + 2 ^ sb ≤ 2 ^ 64) {kb : U64} (hfit : kb.val + ksSizeA ≤ 2 ^ 64) + (hoff : p.val + 2 ^ sb ≤ kb.val ∨ kb.val + 223488 ≤ p.val) : + ∀ i : U64, i.val < 5376 → specPtr (Fl4ProofsA64.P1.irqSlotA kb i) ∉ utDA p sb := by + intro i hi h + have := (utD_memA hsb hp hsp _).1 h + rw [irqSlotA_val hfit hi] at this + omega + +/-! ## Caps, slots and the CDT -/ + +/-- **The slots inside a childless untyped's region are null** (l4v `if_unsafe_then_cap` with childlessness): +a non-null slot is covered by a cap (`CoverZ`) that is no descendant of the untyped, so lies off its region +(`reset_offCov`). Interrupt slots lie in the state block, which the region misses. -/ +theorem utClos_goneNullA {OS : Set U64} {par : U64 → Option U64} (hU : UtInvA st OS par s) + (cz : CoverZ st OS s) {u p b i : U64} {d : Bool} (hu : u ∈ OS) (hcu : capA st s u = .Untyped p b i d) + (hnd : ∀ y, ¬ Fl4Proofs.P3.Mdb.Desc par y u) (hfit : (L.ksb s).val + ksSizeA ≤ 2 ^ 64) + (hoff : p.val + 2 ^ b.val ≤ (L.ksb s).val ∨ (L.ksb s).val + 223488 ≤ p.val) : + ∀ x ∈ OS, p.val ≤ x.val → x.val < p.val + 2 ^ b.val → capA st s x = .Null := by + intro x hx h1 h2 + by_contra hne + have hirq : ¬ IslN (L.ksb s) x := by + rintro ⟨j, hj, rfl⟩ + rw [irqSlotA_val hfit hj] at h1 h2 + omega + obtain ⟨z, hz, hc⟩ := cz x hx hirq hne + have := reset_offCov hU hu hcu hnd hz hc.1 + omega + +/-- **No retained slot names a removed CNode, TCB or VCPU** (`UtNoCapA`): they are range keys, and a childless +untyped's region holds none (`noKey`). `hsub`: closure item A. -/ +theorem utClos_noCapA {OS : Set U64} {th : TypedHeapA} {p : U64} {sb : Nat} (hsb : sb < 64) + (hp : p.val + 2 ^ sb ≤ 2 ^ 64) (hsp : (specPtr p).toNat + 2 ^ sb ≤ 2 ^ 64) + (hsub : ∀ a, IsSlotA th a → a ∈ OS) + (hnk : ∀ c ∈ OS, RngKeyA (capA st s c) → + ¬ (p.val ≤ (ptrA (capA st s c)).val ∧ (ptrA (capA st s c)).val < p.val + 2 ^ sb)) : + UtNoCapA (utDA p sb) th (L.mem s) := by + intro y hy + have hyO : y ∈ OS := hsub y (isSlot_dropA hy) + have key : ∀ q : U64, RngKeyA (capA st s y) → ptrA (capA st s y) = q → specPtr q ∉ utDA p sb := by + intro q hk hq h + exact hnk y hyO hk (hq ▸ (utD_memA hsb hp hsp q).1 h) + have e : capAtA (L.mem s) y = capA st s y := by rfl + rw [e] + cases hc : capA st s y <;> first + | trivial + | (refine key _ ?_ ?_ + · rw [hc] + exact ⟨okinj ((physA_eq _).symm.trans rfl), rfl, rfl⟩ + · rw [hc] + exact ptrA_of rfl) + +/-- **`DeadA` is not changed by removing the region's (null) slots.** -/ +theorem utClos_deadA {OS : Set U64} (hgone : ∀ x ∈ OS, specPtr x ∈ D → capA st s x = .Null) : + ∀ k p, specPtr p ∉ D → DeadA st (dropSetA D OS) k s p → DeadA st OS k s p := by + intro k p _ h u hu hn + refine h u ⟨hu, fun hd => ?_⟩ hn + rw [hgone u hu hd] at hn + exact absurd hn (not_namesD_nullA k p) + +/-- **No CDT edge or MDB link touches a slot of a removed object** (`utDrop_cdtRelA`'s `hcdt` / `hpar`): both ends +of a link are non-null, and the region's slots are null (`hgone`). `hslot`: a removed object's slots lie in the +region (the typed heap's placement). -/ +theorem utClos_linksCdtA {th : TypedHeapA} {σ : Fl4SpecA64.State Unit} {G : MdbGhostA} {lo hi : Nat} + (h : CdtRelA (L := L) _ th s σ G) + (hgone : ∀ x, IsSlotA th x → lo ≤ x.val → x.val < hi → capA st s x = .Null) + (hslot : ∀ sl, AbsSlotA th sl → sl.1 ∈ D → lo ≤ (cslotAddrA sl).val ∧ (cslotAddrA sl).val < hi) : + (∀ sl q, σ.cdt sl = some q → sl.1 ∉ D ∧ q.1 ∉ D) ∧ + (∀ a b, G.par a = some b → ∃ sl, AbsSlotA th sl ∧ cslotAddrA sl = a ∧ sl.1 ∉ D) := by + have out : ∀ sl, AbsSlotA th sl → cslotAddrA sl ∈ G.chains.flatten → sl.1 ∉ D := by + intro sl hsl hm hd + obtain ⟨hs, hne⟩ := (h.tree.lk.members _).1 hm + obtain ⟨h1, h2⟩ := hslot sl hsl hd + exact hne (hgone _ hs h1 h2) + refine ⟨fun sl q hq => ?_, fun a b hab => ?_⟩ + · obtain ⟨h1, h2⟩ := h.cdt_slot sl q hq + have hp : G.par (cslotAddrA sl) = some (cslotAddrA q) := by + rw [← h.cdt sl h1, hq] + rfl + obtain ⟨m1, m2⟩ := h.tree.tree.par_mem _ _ hp + exact ⟨out sl h1 m1, out q h2 m2⟩ + · obtain ⟨sl, h1, rfl⟩ := h.par_slot a b hab + exact ⟨sl, h1, rfl, out sl h1 (h.tree.tree.par_mem _ _ hab).1⟩ + +/-! ## IPC buffers -/ + +/-- A retained thread's buffer word is outside a childless untyped's region: its buffer slot holds a RAM frame cap +or `Null` (`BufHold`), and the frame's heap object is kept by A64 P1's `FrameOK` over the dropped heap. -/ +theorem utClos_bufA {H : Heap} {OS : Set U64} {par : U64 → Option U64} (hH : HeapOK H) (hF : FrameOK st H OS s) + (hU : UtInvA st OS par s) {u p b i : U64} {d : Bool} (hu : u ∈ OS) (hcu : capA st s u = .Untyped p b i d) + (hnd : ∀ y, ¬ Fl4Proofs.P3.Mdb.Desc par y u) (hsb : b.val < 64) (hp : p.val + 2 ^ b.val ≤ 2 ^ 64) + (hsp : (specPtr p).toNat + 2 ^ b.val ≤ 2 ^ 64) {t w : U64} {rcv : Bool} + (hx : sbWordA t ∈ OffR p.val (p.val + 2 ^ b.val) OS) (hbc : BufCap (capA st s (sbWordA t))) + (hl : msg.lookup_ipc_buffer st s rcv t = ok w) (hw : w ≠ 0#u64) : specPtr w ∉ utDA p b.val := by + have hs : obj.Tcb.slot t layout.tcb.SLOT_BUFFER = ok (sbWordA t) := by simp [obj.Tcb.slot, lift, sbWordA] + unfold msg.lookup_ipc_buffer at hl + rw [ipc_buffer_word, bind_ok, hs, bind_ok, capA_eq, bind_ok] at hl + rcases hbc with hn | ⟨ptr, size, rights, x1, x2, x3, x4, hc⟩ + · rw [hn] at hl + exact absurd (okinj hl).symm hw + have hF' := FrameOK.reset hF hU hu hcu hnd (OS' := OffR p.val (p.val + 2 ^ b.val) OS) (fun y hy => hy.1) + (s' := s) (i' := i) hcu (fun _ _ _ => rfl) + rw [hc] at hl + simp only [Bool.false_eq_true, ↓reduceIte] at hl + obtain ⟨bits, hbits⟩ : ∃ bits, size.bits = ok bits := by + cases size <;> exact ⟨_, rfl⟩ + have hobj := hF' _ hx ptr size rights x1 x2 x3 x4 hc bits hbits + obtain ⟨hnR, hqH⟩ := Heap.drop_eq_some.1 hobj + have hfit := hH.fits ptr _ hqH + simp only [HKind.bits] at hfit + have h30 : bits.val < 31 := by + cases size <;> simp only [capability.FrameSize.bits] at hbits <;> rw [← okinj hbits] <;> simp + -- the buffer address lies in the frame + have addr : ∀ r : Result U64, (do + let bits ← capability.FrameSize.bits size + let i ← lift (bits &&& 63#u64) + let i1 ← 1#u64 <<< i + let i2 ← lift (core.num.U64.wrapping_sub i1 1#u64) + let i3 ← lift (L.mem s (ibWordA t) &&& i2) + ok (core.num.U64.wrapping_add ptr i3)) = r → r = ok w → ptr.val ≤ w.val ∧ w.val < ptr.val + 2 ^ bits.val := by + intro r hr hrw + rw [hrw] at hr + have h63 : (bits &&& 63#u64).val = bits.val := by + rw [UScalar.val_and, show (63#u64 : U64).val = 2 ^ 6 - 1 from rfl, Nat.and_two_pow_sub_one_eq_mod] + exact Nat.mod_eq_of_lt (by omega) + obtain ⟨i1, e1, h1⟩ : ∃ i1 : U64, (1#u64 <<< (bits &&& 63#u64) : Result U64) = ok i1 ∧ i1.val = 2 ^ bits.val := by + have : (1#u64 <<< (bits &&& 63#u64) : Result U64) ⦃ z => z.val = 2 ^ bits.val ⦄ := by + step as ⟨z, hz1, hz2⟩ + rw [h63] at hz1 + have hlt : 2 ^ bits.val < 2 ^ 64 := Nat.pow_lt_pow_right (by decide) (by omega) + simpa [Nat.shiftLeft_eq, U64.size_def, U64.numBits, Nat.mod_eq_of_lt hlt] using hz1 + exact spec_exists_dut this + rw [hbits, bind_ok] at hr + simp only [lift, bind_ok] at hr + rw [e1, bind_ok] at hr + have e := congrArg UScalar.val (okinj hr) + have hpos : 1 ≤ 2 ^ bits.val := Nat.one_le_two_pow + have hm : (core.num.U64.wrapping_sub i1 1#u64).val = 2 ^ bits.val - 1 := by + have hlt : 2 ^ bits.val < 2 ^ 64 := Nat.pow_lt_pow_right (by decide) (by omega) + have h1v : (1#u64 : U64).val = 1 := rfl + have hsz : UScalar.size UScalarTy.U64 = 2 ^ 64 := by simp [U64.size_def, U64.numBits] + rw [core.num.U64.wrapping_sub_val_eq, h1, h1v, hsz] + omega + have ha : (L.mem s (ibWordA t) &&& core.num.U64.wrapping_sub i1 1#u64).val < 2 ^ bits.val := by + rw [UScalar.val_and, hm, Nat.and_two_pow_sub_one_eq_mod] + exact Nat.mod_lt _ hpos + rw [wa_valP, Nat.mod_eq_of_lt (by omega)] at e + omega + have hin : ptr.val ≤ w.val ∧ w.val < ptr.val + 2 ^ bits.val := by + split at hl + · exact addr _ rfl hl + · split at hl + · exact absurd (okinj hl).symm hw + · split at hl + · exact addr _ rfl hl + · exact absurd (okinj hl).symm hw + intro hd + have hr := (utD_memA hsb hp hsp w).1 hd + apply hnR + unfold RegionD MeetsR + rw [hqH] + simp only [Option.elim, HKind.bits] + omega + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtClrA.lean b/proofs/Fl4ProofsA64/P3/UtClrA.lean new file mode 100644 index 000000000..482dd3257 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtClrA.lean @@ -0,0 +1,440 @@ +/- +P3 (aarch64, A64-P3 Untyped, chunk steps): **where the record reads, and that a clear region holds none of it**. + +* `UtClrA`: a region `[lo, hi)` holding no joint object, off the cores' nodes and the kernel-state block. +* `utDrop_clrA`: the record after the dead-object drop is clear on the retyped untyped. +* `ut_rd_regionA`, `ut_foot_regionA`: no word of `UtRdA` or `FootA` lies in a clear region. +* `ut_rd_slotA`: neither word of an untyped's cap is a word of `UtRdA`. + +No named premise. New lemmas only. +-/ +import Fl4ProofsA64.P3.UtDropCA +import Fl4ProofsA64.P3.UtFrA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-- **A clear region** (module docstring). -/ +structure UtClrA (st : machine.Store S) [L : LawfulStore st] (Z : XSetsA) (H : Heap) (s : S) (lo hi : Nat) : + Prop where + obj : ∀ q bytes, JointObjectA Z H q bytes → q.val + bytes ≤ lo ∨ hi ≤ q.val + nodes : ∀ c ∈ Z.Cs, L.nodeBase s c + 4288 ≤ lo ∨ hi ≤ L.nodeBase s c + ks : (L.ksb s).val + 788736 ≤ lo ∨ hi ≤ (L.ksb s).val + +/-- A word inside a joint object is not in a clear region. -/ +theorem UtClrA.inObj {Z : XSetsA} {H : Heap} {s : S} {lo hi : Nat} (C : UtClrA st Z H s lo hi) {q a : U64} + {bytes : Nat} (h : JointObjectA Z H q bytes) (h1 : q.val ≤ a.val) (h2 : a.val < q.val + bytes) : + ¬ (lo ≤ a.val ∧ a.val < hi) := by + rcases C.obj q bytes h with g | g <;> omega + +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {nA : U64} {s : S} {a : A} {σ : State Unit} {m : McState} {p sb fi : U64} + +/-- **The dropped record is clear on the retyped untyped.** -/ +theorem utDrop_clrA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {x : U64} (hx : x ∈ OS) (hc : capA st s x = .Untyped p sb fi false) (hfi : fi.val ≤ 2 ^ sb.val) + {D : Set ObjRef} (hD : ∀ z : U64, specPtr z ∈ D ↔ p.val ≤ z.val ∧ z.val < p.val + 2 ^ sb.val) + (hnodes : ∀ c ∈ Z.Cs, L.nodeBase s c + 4288 ≤ p.val ∨ p.val + 2 ^ sb.val ≤ L.nodeBase s c) + (hks : (L.ksb s).val + 788736 ≤ p.val ∨ p.val + 2 ^ sb.val ≤ (L.ksb s).val) : + UtClrA st (dropZA D Z) (dropHA D H) s p.val (p.val + 2 ^ sb.val) := by + refine ⟨fun q bytes h => ?_, hnodes, hks⟩ + have h' : JointObjectA Z H q bytes ∧ specPtr q ∉ D := by + rcases h with ⟨e, h⟩ | ⟨e, h⟩ | ⟨kind, h, e⟩ | ⟨k, h, e⟩ + · rcases h with h | h | h | h | h + · exact ⟨Or.inl ⟨e, Or.inl h.1⟩, h.2⟩ + · exact ⟨Or.inl ⟨e, Or.inr (Or.inl h.1)⟩, h.2⟩ + · exact ⟨Or.inl ⟨e, Or.inr (Or.inr (Or.inl h.1))⟩, h.2⟩ + · exact ⟨Or.inl ⟨e, Or.inr (Or.inr (Or.inr (Or.inl h.1)))⟩, h.2⟩ + · refine ⟨Or.inl ⟨e, Or.inr (Or.inr (Or.inr (Or.inr h)))⟩, fun hd => ?_⟩ + have h1 := (hD q).1 hd + rw [h] at h1 + rcases J.idleAnchor x hx p sb fi false hc with g | g <;> omega + · rcases h with h | h + · exact ⟨Or.inr (Or.inl ⟨e, Or.inl h.1⟩), h.2⟩ + · exact ⟨Or.inr (Or.inl ⟨e, Or.inr h.1⟩), h.2⟩ + · obtain ⟨k1, k2⟩ := dropThA_someA.1 h + exact ⟨Or.inr (Or.inr (Or.inl ⟨kind, k2, e⟩)), k1⟩ + · obtain ⟨k1, k2⟩ := dropHA_someA.1 h + exact ⟨Or.inr (Or.inr (Or.inr ⟨k, k2, e⟩)), k1⟩ + have out : ¬ (p.val ≤ q.val ∧ q.val < p.val + 2 ^ sb.val) := fun g => h'.2 ((hD q).2 g) + rcases J.objAlloc q bytes h'.1 x hx p sb fi false hc with g | g | g + · exact Or.inl g + · exact Or.inr g + · by_cases hb : bytes = 0 + · omega + · exact absurd ⟨g.1, by omega⟩ out + +/-! ## Where the record reads -/ + +/-- The kinds of word the record reads outside the cap slots. -/ +inductive UtWhereA (st : machine.Store S) [L : LawfulStore st] (Z : XSetsA) (H : Heap) (m : McState) (s : S) + (σ : State Unit) (a : U64) : Prop + | tcb (t : U64) : t ∈ Z.T → t.val + 224 ≤ a.val → a.val < t.val + 4096 → UtWhereA st Z H m s σ a + | ibuf (t : U64) : t ∈ Z.T → a = sbWordA t ∨ a = wa (sbWordA t) 8#u64 → UtWhereA st Z H m s σ a + | other (q : U64) (len : Nat) : Z.th q = some (.other len) → q.val ≤ a.val → a.val < q.val + len → + UtWhereA st Z H m s σ a + | sc (c : U64) (x : SchedContext) (k : Nat) : c ∈ Z.C → σ.kheap (scNameA m c) = some (.schedContext x k) → + ScObjA c k a → UtWhereA st Z H m s σ a + | node (c o : U64) : c ∈ Z.Cs → o.val < 4288 → a = L.nodeAt s c o → UtWhereA st Z H m s σ a + | glob (o : U64) : o.val = 8 ∨ o.val = 16 → a = wa (L.ksb s) o → UtWhereA st Z H m s σ a + +variable (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) +include J + +theorem ut_tcbThA {t : U64} (ht : t ∈ Z.T) : Z.th t = some .tcb ∧ t.val + 4096 ≤ 2 ^ 64 := by + have h := (J.j.1.thT t).2 ht + exact ⟨h, J.j.1.place.fit t _ h⟩ + +theorem ut_tcbFieldA {t K : U64} (ht : t ∈ Z.T) (h1 : 224 ≤ K.val) (h2 : K.val < 4096) : + UtWhereA st Z H m s σ (wa t K) := by + have hf := (ut_tcbThA J ht).2 + have e : (wa t K).val = t.val + K.val := by + rw [wa_valP]; exact Nat.mod_eq_of_lt (by omega) + exact .tcb t ht (by omega) (by omega) + +/-- **Every word of `UtRdA` is of one of these kinds.** -/ +theorem ut_rd_whereA {b : U64} (h : UtRdA st Z N E Rp Bf m s σ b) : UtWhereA st Z H m s σ b := by + have tf : ∀ t ∈ Z.T, ∀ K : U64, 224 ≤ K.val → K.val < 4096 → UtWhereA st Z H m s σ (wa t K) := + fun t ht K h1 h2 => ut_tcbFieldA J ht h1 h2 + have other : ∀ q len, Z.th q = some (.other len) → ∀ c : U64, q.val ≤ c.val → c.val < q.val + len → + UtWhereA st Z H m s σ c := fun q len h c h1 h2 => .other q len h h1 h2 + rcases h with h | h | h | ⟨r, hr, h⟩ | ⟨t, ht, h⟩ + · rcases h with ⟨c, hc, o, ho, e⟩ | e | e | ⟨t, ht, K, hK, e⟩ | h + · exact .node c o hc ho e + · exact .glob 8#u64 (Or.inl rfl) e + · exact .glob 16#u64 (Or.inr rfl) e + · rw [e] + rcases hK with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> exact tf t ht _ (by simp) (by simp) + · obtain ⟨c, hc, x, k, hk, ha⟩ := h + exact .sc c x k hc hk ha + · rcases h with ⟨e, he, hv⟩ | ⟨r, hr, hv⟩ | ⟨t, ht, hv⟩ | hv + · exact other e _ (J.ipcPlace.endpoint e he) b (by omega) (by omega) + · exact other r _ (J.ipcPlace.reply r hr) b (by omega) (by omega) + · have hf := (ut_tcbThA J ht).2 + rcases hv with ⟨h1, h2⟩ | h2 + · refine .tcb t ht ?_ ?_ <;> (unfold TOffN at h2; omega) + · unfold TL at h2 + refine .tcb t ht ?_ ?_ <;> omega + · obtain ⟨q, len, h1, h2, h3⟩ := J.ipcPlace.buffer b hv + exact other q len h1 b h2 h3 + · rcases h with ⟨c, hc, h1, h2⟩ | ⟨t, ht, e⟩ + · exact other c _ (J.ipcPlace.notification c hc) b h1 (by omega) + · rcases e with e | e | e | e | e | e <;> rw [e] + · rw [stA_eq]; exact tf t ht _ (by simp) (by simp) + · exact tf t ht 3336#u64 (by simp) (by simp) + · have eb : badgeRegA t = wa t 256#u64 := by + unfold badgeRegA layout.tcb.REGS layout.reg.BADGE layout.reg.X0 + apply UScalar.eq_of_val_eq + simp [core.num.U64.wrapping_add_val_eq, core.num.U64.wrapping_mul_val_eq, U64.size_def, U64.numBits] + rw [eb]; exact tf t ht _ (by simp) (by simp) + · exact tf t ht 3448#u64 (by simp) (by simp) + · exact tf t ht 3496#u64 (by simp) (by simp) + · exact tf t ht 3504#u64 (by simp) (by simp) + · have hth := J.ipcPlace.reply r hr + have hf := J.j.1.place.fit r _ hth + simp only [ObjKindA.size] at hf + have e8 : (rPrevA r).val = r.val + 8 := by + unfold rPrevA layout.reply.PREV + rw [wa_valP]; exact Nat.mod_eq_of_lt (by simp; omega) + have e16 : (rNextA r).val = r.val + 16 := by + unfold rNextA layout.reply.NEXT + rw [wa_valP]; exact Nat.mod_eq_of_lt (by simp; omega) + rcases h with e | e <;> rw [e] + · exact other r _ hth _ (by omega) (by omega) + · exact other r _ hth _ (by omega) (by omega) + · rcases h with e | e | e | e | e | e | e + · rw [e]; exact tf t ht 3440#u64 (by simp) (by simp) + · rw [e]; exact tf t ht 3472#u64 (by simp) (by simp) + · exact .ibuf t ht (Or.inl e) + · exact .ibuf t ht (Or.inr e) + · rw [e]; exact tf t ht 3512#u64 (by simp) (by simp) + · rw [e]; exact tf t ht 3664#u64 (by simp) (by simp) + · rw [e]; exact tf t ht 3456#u64 (by simp) (by simp) + +theorem ut_sbValA {t : U64} (ht : t ∈ Z.T) : + (sbWordA t).val = t.val + 64 ∧ (wa (sbWordA t) 8#u64).val = t.val + 72 := by + have hf := (ut_tcbThA J ht).2 + have e : sbWordA t = wa t 64#u64 := by + unfold sbWordA layout.tcb.SLOT_BUFFER layout.cte.SIZE + apply UScalar.eq_of_val_eq + simp [core.num.U64.wrapping_add_val_eq, core.num.U64.wrapping_mul_val_eq, U64.size_def, U64.numBits] + have e1 : (sbWordA t).val = t.val + 64 := by + rw [e, wa_valP]; exact Nat.mod_eq_of_lt (by simp; omega) + refine ⟨e1, ?_⟩ + rw [wa_valP, e1]; exact Nat.mod_eq_of_lt (by simp; omega) + +/-- A word of one of these kinds is in no clear region. -/ +theorem ut_where_regionA {lo hi : Nat} (C : UtClrA st Z H s lo hi) {b : U64} (h : UtWhereA st Z H m s σ b) : + ¬ (lo ≤ b.val ∧ b.val < hi) := by + have tj : ∀ q k, Z.th q = some k → JointObjectA Z H q k.size := fun q k h => + Or.inr (Or.inr (Or.inl ⟨k, h, rfl⟩)) + cases h with + | tcb t ht h1 h2 => + exact C.inObj (tj t _ (ut_tcbThA J ht).1) (by omega) (by simp only [ObjKindA.size]; omega) + | ibuf t ht e => + have v := ut_sbValA J ht + refine C.inObj (tj t _ (ut_tcbThA J ht).1) ?_ ?_ + · rcases e with e | e <;> rw [e] <;> omega + · simp only [ObjKindA.size]; rcases e with e | e <;> rw [e] <;> omega + | other q len hq h1 h2 => exact C.inObj (tj q _ hq) h1 h2 + | sc c x k hc hk ha => + have hsz := (J.scSize c hc x k hk).1 + have hcH : c ∈ H.scs := by rw [J.scs]; exact hc + obtain ⟨h1, h2⟩ := ha + refine C.inObj (JointObjectA.of_sc hcH) h1 (lt_of_lt_of_le h2 ?_) + exact Nat.add_le_add_left (Nat.pow_le_pow_right (by norm_num) hsz) _ + | node c o hc ho e => + have v : b.val = L.nodeBase s c + o.val := by rw [e]; exact nodeAt_val (J.sq.nodes c hc) ho + rcases C.nodes c hc with g | g <;> omega + | glob o ho e => + have hk : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.sys.ent.ksw + have v : b.val = (L.ksb s).val + o.val := by + rw [e, wa_valP]; exact Nat.mod_eq_of_lt (by omega) + rcases C.ks with g | g <;> omega + +theorem ut_rd_regionA {lo hi : Nat} (C : UtClrA st Z H s lo hi) {b : U64} (h : UtRdA st Z N E Rp Bf m s σ b) : + ¬ (lo ≤ b.val ∧ b.val < hi) := + ut_where_regionA J C (ut_rd_whereA J h) + +/-- **Neither word of an untyped's cap is a word of `UtRdA`.** -/ +theorem ut_rd_slotA {x p sb fi : U64} {d : Bool} (hx : x ∈ OS) (hcx : capA st s x = .Untyped p sb fi d) + (hxs : IsSlotA Z.th x) {b : U64} (hb : b = x ∨ b = wa x 8#u64) : ¬ UtRdA st Z N E Rp Bf m s σ b := by + intro h + have P := J.j.1.place + obtain ⟨a32, f32⟩ := hxs.al32 P + have v8 : (wa x 8#u64).val = x.val + 8 := by + rw [wa_valP]; exact Nat.mod_eq_of_lt (by simp; omega) + have hbv : x.val ≤ b.val ∧ b.val < x.val + 16 := by rcases hb with e | e <;> rw [e] <;> omega + -- the slot's object + have obj : ∃ q k, Z.th q = some k ∧ q.val ≤ x.val ∧ x.val + 32 ≤ q.val + k.size ∧ + (k = .tcb → x.val + 32 ≤ q.val + 224) ∧ ∀ len, k ≠ .other len := by + obtain ⟨q, ⟨r, hr, i, hi, e⟩ | ⟨ht, i, hi, e⟩⟩ := hxs + · obtain ⟨k, hk, hs, hv⟩ := slotVA_of P hr (j := i) (by rw [cnode_size]; omega) + have ek : k = .cnode r := Option.some.inj (hk.symm.trans hr) + subst ek + rw [← e] at hv + exact ⟨q, _, hr, by omega, by omega, (fun h => by cases h), fun _ h => by cases h⟩ + · obtain ⟨k, hk, hs, hv⟩ := tcb_slotV P ht hi + have ek : k = .tcb := Option.some.inj (hk.symm.trans ht) + subst ek + rw [← e] at hv + have : i < 7 := hi + exact ⟨q, _, ht, by omega, by omega, fun _ => by omega, fun _ h => by cases h⟩ + obtain ⟨q, k, hq, q1, q2, qt, qo⟩ := obj + have apart : ∀ q' k', Z.th q' = some k' → q' ≠ q → q'.val ≤ b.val → b.val < q'.val + k'.size → False := by + intro q' k' hq' ne h1 h2 + rcases P.apart q' q k' k hq' hq ne with g | g <;> omega + have hw := ut_rd_whereA J h + cases hw with + | tcb t ht h1 h2 => + have htt := (ut_tcbThA J ht).1 + by_cases e : t = q + · subst e + have ek : k = .tcb := Option.some.inj (hq.symm.trans htt) + have := qt ek + omega + · exact apart t _ htt e (by omega) (by simp only [ObjKindA.size]; omega) + | ibuf t ht e => + have v := ut_sbValA J ht + have htt := (ut_tcbThA J ht).1 + have hxsb : x = sbWordA t := by + have ta := P.align t _ htt + simp only [ObjKindA.size] at ta + apply UScalar.eq_of_val_eq + rcases e with e | e + · have := congrArg UScalar.val e + rcases hb with g | g <;> (have := congrArg UScalar.val g; omega) + · have := congrArg UScalar.val e + rcases hb with g | g <;> (have := congrArg UScalar.val g; omega) + have htH : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + have hbc := J.sys.sysl.bh x hx ⟨t, htH, hxsb.symm⟩ + rw [hcx] at hbc + rcases hbc with h | ⟨ptr, size, rights, x1, x2, x3, x4, h⟩ <;> cases h + | other q' len hq' h1 h2 => + refine apart q' _ hq' (fun e => ?_) h1 (by simpa only [ObjKindA.size] using h2) + rw [e] at hq' + exact qo len (Option.some.inj (hq.symm.trans hq')) + | sc c y k' hc hk ha => + rcases (J.sr.scp.out c hc y k' hk b ha).1 q k hq with g | g <;> omega + | node c o hc ho e => + obtain ⟨⟨Q, R, qi⟩, -⟩ := J.sys.ent.bxi + have hO := J.sys.ent.os + have cw : W7.CapW H.tcbs H.omega H.frame b := by + rcases hb with g | g <;> rw [g] + · exact hO.cw0 hx + · exact hO.cw hx (by simp) (by simp) + exact capw_ne_node qi cw hc ho e + | glob o ho e => + have hk : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.sys.ent.ksw + have v : b.val = (L.ksb s).val + o.val := by + rw [e, wa_valP]; exact Nat.mod_eq_of_lt (by omega) + refine J.sys.sysl.ly.kslo o (by omega) x hx ⟨?_, ?_⟩ + · rw [← e]; omega + · rw [← e]; omega + +/-! ## `JAtZAX`'s footprint -/ + +/-- **No word of `FootA` lies in a clear region.** -/ +theorem ut_foot_regionA {lo hi : Nat} (C : UtClrA st Z H s lo hi) {b : U64} (h : FootA (L := L) Z s b) : + ¬ (lo ≤ b.val ∧ b.val < hi) := by + have P := J.j.1.place + have tj : ∀ q k, Z.th q = some k → JointObjectA Z H q k.size := fun q k h => + Or.inr (Or.inr (Or.inl ⟨k, h, rfl⟩)) + have hk : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.sys.ent.ksw + have ksw : ∀ c : U64, (L.ksb s).val ≤ c.val → c.val < (L.ksb s).val + 788736 → ¬ (lo ≤ c.val ∧ c.val < hi) := + fun c h1 h2 => by rcases C.ks with g | g <;> omega + have tf : ∀ t ∈ Z.T, ∀ K : U64, 224 ≤ K.val → K.val < 4096 → ¬ (lo ≤ (wa t K).val ∧ (wa t K).val < hi) := + fun t ht K h1 h2 => ut_where_regionA J C (ut_tcbFieldA J ht h1 h2) + have node : ∀ c ∈ Z.Cs, ∀ o : U64, o.val < 4288 → ¬ (lo ≤ (L.nodeAt s c o).val ∧ (L.nodeAt s c o).val < hi) := + fun c hc o ho => ut_where_regionA J C (.node c o hc ho rfl) + have scw : ∀ c ∈ Z.C, ∀ K : U64, K.val < 128 → ¬ (lo ≤ (wa c K).val ∧ (wa c K).val < hi) := by + intro c hc K hK + have hcH : c ∈ H.scs := by rw [J.scs]; exact hc + obtain ⟨z, hz⟩ := id hcH + have hz7 := J.sys.ent.heap.scmin c z hz + have hf := J.sys.ent.heap.fits c _ hz + simp only [HKind.bits] at hf + have h128 : 2 ^ 7 ≤ 2 ^ z := Nat.pow_le_pow_right (by decide : 0 < 2) hz7 + have v : (wa c K).val = c.val + K.val := by + rw [wa_valP]; exact Nat.mod_eq_of_lt (by omega) + have e : H.scBits c = z := by unfold Heap.scBits; rw [hz] + exact C.inObj (JointObjectA.of_sc hcH) (by omega) (by rw [e]; omega) + -- a word of a 4 KiB page that is a joint object + have page : ∀ (q i : U64), JointObjectA Z H q 4096 → q.val + 4096 ≤ 2 ^ 64 → i.val < 512 → + ¬ (lo ≤ (peAF q i).val ∧ (peAF q i).val < hi) := by + intro q i hj hf hi' + have v : (peAF q i).val = q.val + 8 * i.val := by + unfold peAF + rw [wa_valP, wm8_valA hi']; exact Nat.mod_eq_of_lt (by omega) + exact C.inObj hj (by omega) (by omega) + have hpg : ∀ q, q ∈ Z.Pp ∨ q ∈ Z.S1 ∨ q ∈ Z.T2 → JointObjectA Z H q 4096 ∧ q.val + 4096 ≤ 2 ^ 64 := by + intro q hq + refine ⟨Or.inl ⟨rfl, ?_⟩, ?_⟩ + · rcases hq with h | h | h + · exact Or.inl h + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr (Or.inl h)) + · obtain ⟨r, z, hr, h1, h2⟩ := J.setsInH.pages q (by + rcases hq with h | h | h + · exact Or.inl (Or.inl (Or.inl h)) + · exact Or.inl (Or.inl (Or.inr h)) + · exact Or.inl (Or.inr h)) + have := J.sys.ent.heap.fits r _ hr + simp only [HKind.bits] at this + omega + -- a word of a root pair + have pair : ∀ (q i : U64), q ∈ Z.X ∨ q ∈ Z.Y → i.val < 512 → + (¬ (lo ≤ (peAF q i).val ∧ (peAF q i).val < hi)) ∧ + ¬ (lo ≤ (peAF (wa q 4096#u64) i).val ∧ (peAF (wa q 4096#u64) i).val < hi) := by + intro q i hq hi' + have hj : JointObjectA Z H q 8192 := Or.inr (Or.inl ⟨rfl, hq⟩) + obtain ⟨r, z, hr, h1, h2⟩ := J.setsInH.roots q hq + have := J.sys.ent.heap.fits r _ hr + simp only [HKind.bits] at this + have e4 : (wa q 4096#u64).val = q.val + 4096 := by + rw [wa_valP]; exact Nat.mod_eq_of_lt (by simp; omega) + have v : (peAF q i).val = q.val + 8 * i.val := by + unfold peAF + rw [wa_valP, wm8_valA hi']; exact Nat.mod_eq_of_lt (by omega) + have v' : (peAF (wa q 4096#u64) i).val = q.val + 4096 + 8 * i.val := by + unfold peAF + rw [wa_valP, wm8_valA hi', e4]; exact Nat.mod_eq_of_lt (by omega) + exact ⟨C.inObj hj (by omega) (by omega), C.inObj hj (by omega) (by omega)⟩ + cases h with + | slot y w hy hw => + have hy' : IsSlotA Z.th y := by + rcases hy with h | ⟨q, r, hq, idx, hl, rfl⟩ + · exact h + · obtain ⟨e, hb, -⟩ := cn_slotV P hq hl + exact ⟨q, Or.inl ⟨r, hq, blvA idx, hb, e⟩⟩ + obtain ⟨q, k, hq, q1, q2⟩ := ut_ownerA P (th' := Z.th) (fun _ _ h => h) hy' + obtain ⟨a32, f32⟩ := hy'.al32 P + have v : ∀ o : U64, o.val < 32 → (wa y o).val = y.val + o.val := fun o ho => by + rw [wa_valP]; exact Nat.mod_eq_of_lt (by omega) + have key : ∀ c : U64, (c = y ∨ c = wa y 8#u64 ∨ c = wa y 16#u64 ∨ c = wa y 24#u64) → + y.val ≤ c.val ∧ c.val < y.val + 32 := by + intro c hc + rcases hc with rfl | rfl | rfl | rfl + · omega + all_goals rw [v _ (by simp)]; simp + have hb := key _ hw + exact C.inObj (tj q _ hq) (by omega) (by omega) + | nslot _ hn => + cases hn with + | node c _ hc ho => + rcases ho with rfl | rfl | rfl | rfl <;> + exact node c hc _ (by simp [layout.node.CUR_THREAD, layout.node.IDLE_THREAD, layout.node.CUR_SC, + layout.node.IDLE_SC]) + | cur _ ho => + rcases ho with rfl | rfl <;> + exact node _ J.j.1.sch.cpuCs _ (by simp [layout.node.SCHED_ACTION, layout.node.CUR_TIME]) + | tcb t _ ht ho => + rcases ho with rfl | rfl | rfl | rfl | rfl + · rw [stA_eq]; exact tf t ht _ (by simp) (by simp) + · exact tf t ht 3464#u64 (by simp) (by simp) + · exact tf t ht 3520#u64 (by simp) (by simp) + · exact tf t ht 3664#u64 (by simp) (by simp) + · exact tf t ht 3672#u64 (by simp) (by simp) + | sc c hc => exact scw c hc 24#u64 (by simp) + | vcpu v _ hv hw => + have hj : JointObjectA Z H v 4096 := Or.inl ⟨rfl, Or.inr (Or.inr (Or.inr (Or.inl hv)))⟩ + obtain ⟨r, z, hr, h1, h2⟩ := J.setsInH.pages v (Or.inr hv) + have := J.sys.ent.heap.fits r _ hr + simp only [HKind.bits] at this + have e24 : (wa v 24#u64).val = v.val + 24 := by + rw [wa_valP]; exact Nat.mod_eq_of_lt (by simp; omega) + rcases hw with rfl | rfl + · exact C.inObj hj (le_refl _) (by omega) + · exact C.inObj hj (by omega) (by omega) + | irq i hi' => + have v := irqWordA_val (show (L.ksb s).val + 788736 ≤ 2 ^ 64 from hk) hi' + exact ksw _ (by omega) (by omega) + | cur2 _ ho => + rcases ho with rfl | rfl <;> + exact node _ J.j.1.sch.cpuCs _ (by simp [layout.node.CONSUMED, layout.node.REPROGRAM]) + | glob _ ho => + rcases ho with rfl | rfl + · exact ut_where_regionA J C (.glob 8#u64 (Or.inl rfl) rfl) + · exact ut_where_regionA J C (.glob 16#u64 (Or.inr rfl) rfl) + | tcb2 t _ ht ho => + rcases ho with rfl | rfl + · exact tf t ht 3448#u64 (by simp) (by simp) + · exact tf t ht 3432#u64 (by simp) (by simp) + | sc2 c hc => exact scw c hc 72#u64 (by simp) + | vs _ hv => + cases hv with + | asid h hlt => + have v : (atAF (L.ksb s) h).val = (L.ksb s).val + (772352 + 8 * h.val) := by + unfold atAF + have e8 : (8#u64 : U64).val = 8 := rfl + have e7 : (772352#u64 : U64).val = 772352 := rfl + have hsz : UScalar.size UScalarTy.U64 = 2 ^ 64 := by simp [U64.size_def, U64.numBits] + rw [wa_valP, wa_valP, core.num.U64.wrapping_mul_val_eq, e7, e8, hsz] + omega + exact ksw _ (by omega) (by omega) + | pool q i hq hi' => exact page q i (hpg q (Or.inl hq)).1 (hpg q (Or.inl hq)).2 hi' + | pt q i hq hi' => exact page q i (hpg q (Or.inr (Or.inl hq))).1 (hpg q (Or.inr (Or.inl hq))).2 hi' + | high y i hy hi' => exact (pair y i (Or.inr hy) hi').2 + | ext _ he => + cases he with + | s2lo r i hr hi' => exact (pair r i (Or.inl hr) hi').1 + | s2hi r i hr hi' => exact (pair r i (Or.inl hr) hi').2 + | s2pt t i ht hi' => exact page t i (hpg t (Or.inr (Or.inr ht))).1 (hpg t (Or.inr (Or.inr ht))).2 hi' + | wl0 y hy => exact (pair y 511#u64 (Or.inr hy) (by simp)).1 + | wlo y i hy hi' => exact (pair y i (Or.inr hy) (by omega)).1 + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtCrtResetA.lean b/proofs/Fl4ProofsA64/P3/UtCrtResetA.lean new file mode 100644 index 000000000..8db4b9e18 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtCrtResetA.lean @@ -0,0 +1,186 @@ +/- +P3 (aarch64, A64-P3 Untyped, reset + creation): **A64 P1's creation-start and creation-end facts at a reset's exit**, +for any successful `reset_untyped` from the restart state. + +A64 P1 builds `CrtP` and `UtEnd` at the reset's exit inside its own proofs (`untyped.retype.stZFCRUGB`, and the RAM +branch of `untyped.decode.sysLBH`). These are the same derivations as standalone lemmas, so the record's reset proofs +(which fix the exit state) can compose with the creation. No P1 statement is changed. + +* **`utCrtP_resetA`**: `CrtP` over the dropped heap and slot set at the exit, and the retained caps are those of the + decode's start. +* **`utEnd_resetA`**: `UtEnd` of the creation that follows. + +No named premise. New theorems only. +-/ +import Fl4ProofsA64.P1.MdbLiveU +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P1 +open fl4_core_a64 Fl4ProofsA64.P0.Consts Fl4Proofs.P3 + +section +variable {S : Type _} {st : machine.Store S} [L : LawfulStore st] +variable {H : Heap} {Cs Rp Nt : Set U64} + +local notation "wa" => core.num.U64.wrapping_add + +namespace W7 +namespace R +namespace ST + +variable {OS : Set U64} +variable {A : Type _} {ar : machine.Arch A} + +set_option maxHeartbeats 4000000 in +/-- **`CrtP` at the reset's exit** (module docstring). -/ +theorem utCrtP_resetA (hA : P0.ArchTotal ar) {s0 s2 s3 : S} {extra : Aeneas.Std.Array obj.Slot 3#usize} + {slot p sb fi : U64} (R : UtDecP st H Cs H.frame Rp Nt OS s0 extra slot p sb fi false) + (e : sched.set_thread_state st s0 (L.curW s0) sched.TS_RESTART = ok s2) (a a3 : A) + {aligned nt us dest off win : U64} (hU : UtArgs st s0 extra slot p sb fi false true aligned nt us dest off win) + (eres : untyped.reset_untyped st ar s2 a slot = ok (core.result.Result.Ok (), s3, a3)) : + CrtP st (H.drop (RegionD H p.val (p.val + 2 ^ sb.val))) Cs H.frame (OffR p.val (p.val + 2 ^ sb.val) OS) s3 slot p + sb 0#u64 false aligned nt us dest off win ∧ + (∀ y ∈ OffR p.val (p.val + 2 ^ sb.val) OS, y ≠ slot → capA st s3 y = capA st s0 y) ∧ + WritesIn st s2 s3 (RstW st s2 slot p sb) := by + have hH := R.heap + have hO := R.os + have hsrc := R.src + have hcs := R.cap + obtain ⟨x, hxm, hx0, idx, dep, val, ers, hv64, erd, ees, hrs, -⟩ := id hU + have he := hrs.1 rfl + obtain ⟨Ac, Q, Rq, q, k, w, hact, q2, hDb2, hSt2, hF2, hCZ2, hb2, hcs2, hW2, hnd0, hnd2⟩ := R.restart e he + obtain ⟨hcap0, hpal, hfit, P0, hnode0, hsz⟩ := R.rst rfl he + have hRin := R.ur.rin R.src R.cap + have P2 := DropOK.restart P0 q w hact + have hnode2 : RegionOffNodes st Cs s2 p.val (p.val + 2 ^ sb.val) := by + unfold RegionOffNodes LawfulStore.nodeBase at hnode0 ⊢; rw [w.1]; exact hnode0 + have hRk2 : ResetOK st H Cs s2 slot p sb fi false := ⟨by rw [capA_eq, hcs2], hpal, hfit, P2, hnode2, hsz⟩ + obtain ⟨hxO, -, -, -, -, -, hnull⟩ := reset_startS hSt2.1 hsrc hcs2 hnd2 (not_islN_region q2 hnode2) + obtain ⟨hn, hus, hval⟩ := untyped.retype_size.okA ers + have hz : (zA nt.val us).val < 64 := by rw [← hval]; exact hv64 + have hAr : UtAr (H.drop (RegionD H p.val (p.val + 2 ^ sb.val))) p sb 0#u64 false aligned nt us dest off win := + R.arith _ _ _ _ _ _ _ hU + have hS0 := R.stz.1.1 + obtain ⟨G0, par0, hM0, -, -, -, -, hTr0, hU0⟩ := id hS0 + have hdc' := untyped.retype_dest.covC hO R.ty hS0 (R.ext x hxm hx0) erd + have hdn0 := UtArgs.hdn R.ty R.ext hU + have hrun := WP.spec_and (untyped.reset_untyped.stZFC hA hH hO hb2 R.slotw hRk2 hSt2 hF2 hCZ2 hsrc hnd2 + hW2 a) (WP.spec_and (untyped.reset_untyped.wr hA hb2 R.slotw hRk2 a) + (resetP1Step_holds (st := st) hA H Cs Ac s2 a slot p sb fi false hb2 hRk2.1 R.slotw hpal hfit P2 hnode2 hsz)) + rw [eres, WP.spec_ok] at hrun + obtain ⟨⟨⟨hS1, hF1, hC1⟩, i, hci, hi0⟩, hw1, H2, Ac2, hbR, hsh, -, -⟩ := hrun + dsimp only at hS1 hF1 hC1 hci hi0 hw1 hbR + have e2 := reset_heap_eq hsh + subst e2 + have keep : ∀ y ∈ OffR p.val (p.val + 2 ^ sb.val) OS, y ≠ slot → capA st s3 y = capA st s0 y := + fun y hy hyx => (capA_keepRst hO q2 hW2 hsrc hw1 hy hyx).trans (k.capA hy.1) + have hi := hi0 rfl + subst hi + have hHd : HeapOK (H.drop (RegionD H p.val (p.val + 2 ^ sb.val))) := hH.drop + have hOd := OSOK.dropR hH hO hRin + have hN2 := q2.nodes _ R.core0 + have hN0 : NodePlaced st (H.drop (RegionD H p.val (p.val + 2 ^ sb.val))).tcbs s3 0#u64 := by + have h := hN2.ksb hw1.1 + exact ⟨h.align, h.fits, fun t ht => h.disj t (Heap.mem_tcbs_drop.1 ht).1⟩ + have enw : nwA st s3 = nwA st s2 := nodeAt_of_ksb hw1.1 _ _ + have hnC : ¬ W7.CapW (H.drop (RegionD H p.val (p.val + 2 ^ sb.val))).tcbs + (H.drop (RegionD H p.val (p.val + 2 ^ sb.val))).omega H.frame (nwA st s3) := fun hb => by + rw [enw] at hb + have hb' : W7.CapW H.tcbs H.omega H.frame (nwA st s2) := by + rcases hb with ⟨t, ht, K, hK, e'⟩ | ⟨h1, h2⟩ + · exact Or.inl ⟨t, (Heap.mem_tcbs_drop.1 ht).1, K, hK, e'⟩ + · exact Or.inr ⟨Heap.omega_drop_sub h1, h2⟩ + exact capw_ne_node q2 hb' R.core0 (by rw [debug_val]; omega) rfl + have hnv : (nwA st s2).val = L.nodeBase s2 0#u64 + 88 := by + rw [nodeAt_val hN2 (off := layout.node.DEBUG_TCBS) (by rw [debug_val]; omega), debug_val] + have hmn : L.mem s3 (nwA st s3) = L.mem s2 (nwA st s2) := by + rw [enw] + refine hw1.mem_eq (nodeAt_aligned hN2 (by rw [debug_val]; omega) (by rw [debug_val])) ?_ + rintro (⟨h1, h2⟩ | (e' | e') | hp) + · rcases hnode2.1 _ R.core0 with g | g <;> omega + · exact capw_ne_node q2 (hO.cw0 hsrc) R.core0 (o := layout.node.DEBUG_TCBS) (by rw [debug_val]; omega) e'.symm + · exact capw_ne_node q2 (hO.cw hsrc (k := 8#u64) (by simp) (by simp)) R.core0 (o := layout.node.DEBUG_TCBS) + (by rw [debug_val]; omega) e'.symm + · rcases hp with e' | e' | e' + · have h3 := congrArg UScalar.val e' + rw [ks_val q2 (o := layout.ks.WORK_UNITS) (by simp [layout.ks.WORK_UNITS])] at h3 + have : layout.ks.WORK_UNITS.val = 40 := by simp [layout.ks.WORK_UNITS] + simp only [LawfulStore.nodeBase] at hnv + omega + · unfold LawfulStore.nodeA at e' + exact node_ne_node hN2 (q2.nodes _ q2.cur) (o := layout.node.DEBUG_TCBS) (o' := layout.node.CUR_TIME) + (by rw [debug_val]; omega) (by simp [layout.node.CUR_TIME]) + (fun _ => by rw [debug_val]; simp [layout.node.CUR_TIME]) (congrArg UScalar.val e') + · unfold LawfulStore.nodeA at e' + exact node_ne_node hN2 (q2.nodes _ q2.cur) (o := layout.node.DEBUG_TCBS) (o' := layout.node.CONSUMED) + (by rw [debug_val]; omega) (by simp [layout.node.CONSUMED]) + (fun _ => by rw [debug_val]; simp [layout.node.CONSUMED]) (congrArg UScalar.val e') + have hhd0 : L.mem s3 (nwA st s3) = 0#u64 ∨ + (H.drop (RegionD H p.val (p.val + 2 ^ sb.val))) (L.mem s3 (nwA st s3)) = some .tcb := by + rw [hmn]; exact nw_kept P2 R.core0 hDb2 + have hdO : ∀ j, j < win.val → djA dest off j ∈ OffR p.val (p.val + 2 ^ sb.val) OS := fun j hj => by + obtain ⟨hdj, z, hz, p', r', g', b', ez, hcv⟩ := hdc' j hj + exact ⟨hdj, reset_offCov hU0 hsrc hcs (hnd0 G0 par0 ⟨hM0, hTr0, hU0⟩) hz (by rw [ez]; exact hcv)⟩ + have hzO : ∀ j, j < win.val → ∃ z ∈ OffR p.val (p.val + 2 ^ sb.val) OS, ∃ p' r' g' b', + capA st s3 z = .CNode p' r' g' b' ∧ CovBy (.CNode p' r' g' b') (djA dest off j) := fun j hj => by + obtain ⟨-, z, hz, p', r', g', b', ez, hcv⟩ := hdc' j hj + have hz' : z ∈ OffR p.val (p.val + 2 ^ sb.val) OS := by + by_contra hn' + have := hnull z hz hn' + rw [k.capA hz, ez] at this; cases this + have hzx : z ≠ slot := fun e' => by rw [e', hcs] at ez; cases ez + exact ⟨z, hz', p', r', g', b', by rw [keep z hz' hzx, ez], hcv⟩ + have hdn : ∀ j, j < win.val → capA st s3 (djA dest off j) = .Null := fun j hj => by + have hdx : djA dest off j ≠ slot := fun e' => by have := hdn0 j hj; rw [e', hcs] at this; cases this + rw [keep _ (hdO j hj) hdx]; exact hdn0 j hj + have hdc : ∀ j, j < win.val → obj.sext48 (djA dest off j) = ok (djA dest off j) := + fun j hj => hO.canon R.win (hdO j hj).1 + exact ⟨⟨hHd, hOd, hS1, hF1, hC1, hxO, hci, hn, hus, hz, hAr, R.core0, hN0, hnC, hhd0, hdO, hdn, hdc, hzO⟩, keep, hw1⟩ + +set_option maxHeartbeats 4000000 in +/-- **`UtEnd` of the creation after a reset** (module docstring). -/ +theorem utEnd_resetA (hA : P0.ArchTotal ar) {s0 s2 s3 : S} {extra : Aeneas.Std.Array obj.Slot 3#usize} + {slot p sb fi : U64} {ia ie : Nat} (R : UtDecP st H Cs H.frame Rp Nt OS s0 extra slot p sb fi false) + (LY : A64Layout st H OS ia ie s0) + (e : sched.set_thread_state st s0 (L.curW s0) sched.TS_RESTART = ok s2) (a a3 : A) + {aligned nt us dest off win : U64} (hU : UtArgs st s0 extra slot p sb fi false true aligned nt us dest off win) + (eres : untyped.reset_untyped st ar s2 a slot = ok (core.result.Result.Ok (), s3, a3)) + {H' : Heap} + (hHH' : ∀ q k, (H.drop (RegionD H p.val (p.val + 2 ^ sb.val))) q = some k → H' q = some k) + (hEx : ∀ q k, H' q = some k → (H.drop (RegionD H p.val (p.val + 2 ^ sb.val))) q = some k ∨ + (false = false ∧ ∃ j, j < win.val ∧ q = bjA aligned (zA nt.val us) j ∧ newKind nt.val us.val = some k)) + (hHn : false = false → + ∀ j, j < win.val → ∀ k, newKind nt.val us.val = some k → H' (bjA aligned (zA nt.val us) j) = some k) : + untyped.retype st ar s3 a3 slot false aligned nt us dest off win false ⦃ r => + UtEnd st H Cs OS s0 slot p sb false (H.drop (RegionD H p.val (p.val + 2 ^ sb.val))) H' + (OffR p.val (p.val + 2 ^ sb.val) OS) s3 r.2.1 p.val (p.val + 2 ^ sb.val) aligned nt us dest off win ⦄ := by + obtain ⟨C, keep, hw1⟩ := utCrtP_resetA hA R e a a3 hU eres + obtain ⟨x, hxm, hx0, idx, dep, val, ers, hv64, erd, ees, hrs, -⟩ := id hU + have he := hrs.1 rfl + obtain ⟨Ac', hb⟩ := R.blk + obtain ⟨Q, Rq, q⟩ := hb.1 + obtain ⟨k, w⟩ := restart_frame (OS := OS) R.os q e + have hsw : ∀ b, (b = slot ∨ b = wa slot 8#u64) → W7.CapW H.tcbs H.omega H.frame b := fun b hb' => by + rcases hb' with e' | e' + · subst e'; exact R.os.cw0 R.src + · subst e'; exact R.os.cw R.src (by simp) (by simp) + obtain ⟨-, -, -, -, -, -, -, -, -, -, -, -, -, -, -, hnd0, -⟩ := R.restart e he + have F := DropF.of R (R.drop rfl he) hnd0 + obtain ⟨hsrc1, hnn, hRT, hBT, hKT, hIT, hhole, hH1, -, -, hkp, hkps, hoff⟩ := id F + have hb0 : 0 < aligned.val := + lt_of_lt_of_le (LY.ut0 slot R.src p sb fi R.cap) (CrtA.of_args R hU).pa + refine WP.spec_mono (untyped.retype.liveRWH hA C hb0 a3) fun r ⟨E, X, _, _⟩ => ?_ + exact ⟨CrtA.of_args R hU, hH1, hhole, R.stz.2.2 slot R.src p sb fi R.cap, hRT, hKT, fun _ h => h.1, hnn, hsrc1, + rst_words w hsw hw1, keep, C.dO, E, X, fun _ => ⟨(CrtA.of_args R hU).pa, (CrtA.of_args R hU).aw⟩, + hHH', hEx, UtArgs.dcv R.ty R.ext hU, + fun j hj h4 => ⟨rfl, hHn rfl j hj _ (by rw [h4]; simp [newKind])⟩, hoff, hkp, hIT, hHn, + fun _ => ⟨(CrtA.of_args R hU).pa, (CrtA.of_args R hU).aw⟩, fun _ => C.fr, C.os, C.heap, + fun x hx h => ⟨hx, h⟩⟩ + +end ST +end R +end W7 + +end + +end Fl4ProofsA64.P1 diff --git a/proofs/Fl4ProofsA64/P3/UtD1bA.lean b/proofs/Fl4ProofsA64/P3/UtD1bA.lean new file mode 100644 index 000000000..652093585 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtD1bA.lean @@ -0,0 +1,35 @@ +/- +P3 (aarch64, A64-P3 Untyped): **the record's `vsOk` gives the reset's `hvs`** (the clause shapes are in +`P3/Stage3D1bA`). + +* `utHvs_ofA`: with `UtVsOkA`, no reachable VSpace word or ASID-table word is a word of a slot of `OS`. + +No named premise. +-/ +import Fl4ProofsA64.P3.Stage3D1bA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4SpecA64 Classical + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {H : Heap} {OS V Pp Tb : Set U64} {Lv : U64 → Nat} {s : S} + +/-- **`hvs`** from the placement clause. -/ +theorem utHvs_ofA (h : UtVsOkA st H OS V Pp Tb Lv s) {x : U64} (hx : x ∈ OS) (hfit : x.val + 32 ≤ 2 ^ 64) : + ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → Aligned b ∧ b ≠ x ∧ b ≠ wa x 8#u64 := by + intro b hb + have e8 : (wa x 8#u64).val = x.val + 8 := by + rw [wa_valP, show (8#u64 : U64).val = 8 from rfl] + exact Nat.mod_eq_of_lt (by omega) + refine ⟨h.al b hb, fun e => h.os b hb ?_, fun e => h.os b hb ?_⟩ + · exact ⟨x, hx, by rw [e], by rw [e]; omega⟩ + · exact ⟨x, hx, by rw [e, e8]; omega, by rw [e, e8]; omega⟩ + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtDecRecA.lean b/proofs/Fl4ProofsA64/P3/UtDecRecA.lean new file mode 100644 index 000000000..b0fca4ff3 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtDecRecA.lean @@ -0,0 +1,260 @@ +/- +P3 (aarch64, A64-P3 Untyped, Architect ruling (c)): **the decode-level top theorem of a RAM retype without a +reset**: `untyped::decode` refines the spec and, for every outcome of the spec's `invoke_untyped` body, the whole +record `JStage3A` holds after it. + +* **`utRt_j7RetA`**: `utRt_j7TopA` at the retype level, with explicit creation parameters (A64 P1's + `untyped.retype.s1aTopRam`): the creation end `S1aRtIn`, `ArmQIVAt`, `HeapKsOff` and the `J7A` hook. +* **`utDecode_recNoResetA`**: `utDecode_postA`'s proof with `utRt_j7RetA` and `utCreate_recA` at the one creation. + +For an outcome `r` of the body the record holds at `utInvStA … r.2`, the outcome of `fl4InvokeUntypedA64` (the body +followed by `vcpu::init` on new VCPUs; membership `UtInvVcA`, `P3/UtApRec6A`). + +Premises: those of `utDecode_postA`. New theorems only. +-/ +import Fl4ProofsA64.P1.S1aKtRetype +import Fl4ProofsA64.P3.UtFullJ7A +import Fl4ProofsA64.P3.UtRtTopA +import Fl4ProofsA64.P3.UtRtRecTopA +import Fl4ProofsA64.P1.S1aRtTopK + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +set_option maxRecDepth 100000 + +section top +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + +set_option maxHeartbeats 4000000 in +/-- **P1's creation top at the retype level with `J7A`** (module docstring). -/ +theorem utRt_j7RetA (hA : P0.ArchTotal ar) {H : Heap} {Z : XSetsA} + {OS U Ac Pp0 Tb0 Ud Acd : Set U64} {Lv0 : U64 → Nat} {ia ie : Nat} {s s2 : S} {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} {n : U64} + (I : SysInvA st H Z.Cs OS U Ac Pp0 Tb0 Lv0 ia ie s) {extra : Aeneas.Std.Array obj.Slot 3#usize} + {slot p sb fi : U64} + (D : DecPre st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ud Acd s (.Untyped p sb fi false) + extra) + (P : SysPlace st H Z.Cs H.frame H.replies H.ntfns OS s extra slot) (Y : SymL st H Z.Cs s) + (hcap : obj.Slot.cap st slot s = ok (.Untyped p sb fi false)) + (J : J7A st (J7R2A st ar n) Z H OS s σ m) (ko : HeapKsOff H (L.ksb s)) + (e : sched.set_thread_state st s (L.curW s) sched.TS_RESTART = ok s2) (a : A) + {aligned nt us dest off win : U64} + (hU : UtArgs st s extra slot p sb fi false false aligned nt us dest off win) : + ∃ H' : Heap, + untyped.retype st ar s2 a slot false aligned nt us dest off win false ⦃ r => + ∃ lo hi : Nat, UtEnd st H Z.Cs OS s slot p sb false H H' OS s2 r.2.1 lo hi aligned nt us dest off win ⦄ ∧ + untyped.retype st ar s2 a slot false aligned nt us dest off win false ⦃ r => + ∃ (Vn : Set U64) (lo hi : Nat), + S1aRtIn st H Z.Cs OS Ud Acd s ia ie slot p sb fi false extra H H' OS s2 r.2.1 lo hi aligned nt us dest off + win ∧ + ArmQIVAt st H Z.Cs OS ia ie s H' (OS ∪ NewSl false nt us aligned win) Vn r.2.1 ∧ + HeapKsOff H' (L.ksb s) ∧ + ∀ (Z' : XSetsA) (σ' : Fl4SpecA64.State Unit) (m' : Fl4SpecA64.McState), + UtJ7SplitA st Z Z' (OS ∪ NewSl false nt us aligned win) s r.2.1 slot p sb nt us aligned win.val → + J7A st (J7R2A st ar n) Z' H' (OS ∪ NewSl false nt us aligned win) r.2.1 σ' m' ⦄ := by + obtain ⟨Pt, Ub0, vr0, pools, C0, K0, vc, frame, hic0, sched, apart, frub, alub, vcub, Pp, Tb, Hr, Te, Kt, Lv, Re, bits, + h, sep, KLv, ktab, hik⟩ := J + have vr := utAlUb_vrA vr0 + have C := utAlUb_colA C0 + have K := utAlUb_capA K0 + have al := utAlUb_alA Ub0 + have B := I.sysl + have R := UtDecP.of_live D B.sys P hcap B.ln B.vr B.ly + have offState : (false : Bool) = false → + (L.ksb s).val + 788736 ≤ p.val ∨ p.val + 2 ^ sb.val ≤ (L.ksb s).val := fun _ => + (R.stz.2.2 slot R.src p sb fi R.cap).2 + obtain ⟨-, -, -, -, -, -, -, H', hNew, hHH', hHn, hEx⟩ : + UtAr H p sb fi false aligned nt us dest off win := R.arith _ _ _ _ _ _ _ hU + have hUe := utEnd_noResetA (ar := ar) hA R B.ly e a hU hHH' hEx hHn + refine ⟨H', hUe, ?_⟩ + have fresh1 : untyped.retype st ar s2 a slot false aligned nt us dest off win false ⦃ r => + ∀ x ∈ OS, capA st s x = .Null → + (cdt.ensure_no_children st s slot = ok (.Ok ()) → x.val + 32 ≤ p.val ∨ p.val + 2 ^ sb.val ≤ x.val) → + ∀ t m asid, capA st r.2.1 x = .HighVSpace t m asid → S1aHighInit st (Kt1 ar) r.2.1 t ⦄ := by + have pos : 0 < aligned.val := + lt_of_lt_of_le (B.ly.ut0 slot R.src p sb fi R.cap) (CrtA.of_args R hU).pa + obtain ⟨_, hb⟩ := R.blk + obtain ⟨_, _, q0⟩ := hb.1 + have kp := (restart_frame (OS := OS) R.os q0 e).1 + refine WP.spec_mono (untyped.retype.newHighInit hA (Kt1.ktab ktab) (CrtP.of0 R e hU) pos + (UtArgs.zalA R.ty R.ext R.cap R.utv hU).1 a) ?_ + intro _ new x hx empty _ + exact new x hx ((kp.capA hx).trans empty) + refine WP.spec_mono (WP.spec_and (untyped.retype.s1aTopRam D P B hcap e hU hA Y vr pools C K al vc h sep ktab a hNew + hHH' hEx hHn hUe) fresh1) ?_ + rintro ⟨r, q, a'⟩ ⟨⟨Vn, Vc'', lo, hi, rt, arm, tab, hsub, h4, h5, h6, words, roots, paths, fresh, keep, init⟩, fr1⟩ + have E := rt.creation + have X := rt.kept + have after : SysL st H' Z.Cs (s1aRtSlots OS false nt us aligned win) ia ie q := arm.1 + obtain ⟨_, _, _, kinds, _, _⟩ := after.sys.stz.1.1.mdbS + have ks' := E.heapKsOff X offState ko + have devs := UtEnd.devFrN D P B hcap E X rt.window frame + have hic := E.hiCap511 X B.ly.ksw init keep hic0 + have tab' := tab + have tables := UtEnd.ptCapHG D P B hcap rt.creation rt.kept rt.window rt.reset h.pageTables h.footprint + have R2 := rt.r2 h sep words tab roots paths fresh kinds after.sys.fr hic ks' tables + refine ⟨Vn, lo, hi, rt, arm, ks', fun Z' σ' m' F => ?_⟩ + exact utRt_j7MadeA rt vr C K al sched apart h sep (arm.2.2.2.2.2.2.1 pools) h5 h6 devs hic R2 rfl F + (utAlUb_frA frub) (fun b hb m => vcub b hb m.1) ktab hik words fr1 + +end top + +section site2 +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rq : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s : S} {a : A} {σ : State Unit} {m : McState} + +set_option maxHeartbeats 4000000 in +/-- **The decode-level top, no reset** (module docstring). -/ +theorem utDecode_recNoResetA (hA : P0.ArchTotal ar) + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rq Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {p sb fi : U64} (ex : Aeneas.Std.Array obj.Slot 3#usize) + (sl : CslotPtr) (hsrc : ∃ q, sl.1 = specPtr q ∧ Z.th q = some (.cnode sl.2.length)) + {Ud Acd : Set U64} + (D : DecPre st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ud Acd s (.Untyped p sb fi false) ex) + (P : SysPlace st H Z.Cs H.frame H.replies H.ntfns OS s ex (cslotAddrA sl)) + (hcapF : obj.Slot.cap st (cslotAddrA sl) s = ok (.Untyped p sb fi false)) + (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) + (hfm : A64FrameMasks st s) + (vi : U64) (hvi : ar.virtualization a = ok vi) (hvirt : vi = 0#u64 ↔ fl4A64Virt = false) + (hfan : Fl4SpecA64.retypeFanOutLimit = 256#64) + (lbl : U64) (slbl : Fl4SpecA64.Data) + (hlbl : Fl4SpecA64.genInvocationType slbl = .untypedRetype ↔ lbl = fl4_abi.label.UntypedRetype) + (length buf : U64) (args : List U64) (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) + (exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) (hex : ExtraOKA Z.th (L.mem s) ex exs) + (hchild : ∃ x, σ.cdt x = some sl) + (hnew : ∀ aligned nt us dest off win : U64, + UtArgs st s ex (cslotAddrA sl) p sb fi false false aligned nt us dest off win → + PlaceA (utRtThA Z.th false nt us aligned win.val) (L.ksb s) ∧ + ∀ j, j < win.val → utRangedA nt.val → SpNoOvA (bjA aligned (zA nt.val us) j) (zOfA nt.val us.val)) + -- the arch facts of the creation + (hVP : ∀ a1 : A, UtVcpuPlatA ar a1) {k : U64 → U64} + (hKt : ∀ (a1 : A) (i : U64), i.val < 511 → ar.kernel_ttbr1_entry a1 0#u64 i = ok (k i)) + (hK0 : ∀ i : U64, i.val < 256 → k i = 0#u64) : + untyped.decode st ar s a lbl length (cslotAddrA sl) (.Untyped p sb fi false) buf ex ⦃ res => + UPostA σ (fl4DecodeUntypedInvocationVirt (Z := Unit) slbl (args.map UScalar.bv) sl + (absCapA (.Untyped p sb fi false)) (exs.map Prod.fst)) s a + (fun inv res => ∃ (base retypeBase : Word) (t : ApiObjectType) (us : Nat) (slots : List CslotPtr), + inv = .retype sl false base retypeBase t us slots false ∧ res.1 = .Ok () ∧ + ∃ xT, σ.kheap σ.curThread = some (.tcb xT) ∧ + ∃ aligned nt usF dest off win : U64, + UtArgs st s ex (cslotAddrA sl) p sb fi false false aligned nt usF dest off win ∧ + ∀ r ∈ (utRtBodyA sl base retypeBase t us slots false (stUpdA σ σ.curThread xT .restart)).1, + UtInvVcA t retypeBase us slots r.2 ∧ ∃ (H'' : Heap) (Z'' : XSetsA) (OS'' U'' Ac' Pp'' Tb'' N'' E'' Rp'' : Set U64) + (stack'' : U64 → List U64) (κ'' : U64 → U64) (m'' : McState), + JStage3A st AR Z'' H'' OS'' U'' Ac' Pp'' Tb'' Lv ia ie N'' E'' Rp'' Bf stack'' κ'' n (J5A st) + (J7A st (J7R2A st ar nA)) res.2.1 res.2.2 (utInvStA t retypeBase us slots r.2) m'' ∧ + Z'' = utRecZA Z (cslotAddrA sl) nt usF aligned dest off win ∧ + OS'' = OS ∪ NewSl false nt usF aligned win) res ⦄ := by + have Je := J.toJEntA + have hV : SlotsValidA Z.th (L.mem s) := Je.j.1.cv + have hpos : ∀ p k, Z.th p = some k → 0 < k.size := fun p k h => by + cases k with + | cnode r => exact Nat.two_pow_pos _ + | tcb => exact Nat.succ_pos _ + | other z => exact J.thPos p z h + have hsub : ∀ y, IsSlotA Z.th y → y ∈ OS := J.j5.2.1 + have hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null := J.j5.2.2 + have hR := Je.j.1.st + have hC := Je.j.1.cdt + have hP := Je.j.1.place + have hO := Je.sys.ent.os + have hTr : TreeA st OS Z.G.chains Z.G.par s := + hC.tree.grow hsub fun x hx hn => hnul x hx hn + have hAs : AbsSlotA Z.th sl := by + obtain ⟨q, e, hq⟩ := hsrc + exact ⟨q, e, Or.inl hq⟩ + have hsO : cslotAddrA sl ∈ OS := hsub _ (hAs.isSlot hP) + have R := UtDecP.of_live D Je.sys.sysl.sys P hcapF Je.sys.sysl.ln Je.sys.sysl.vr Je.sys.sysl.ly + have hcap : capA st s (cslotAddrA sl) = .Untyped p sb fi false := R.cap + have hrv : obj.Slot.revocable st (cslotAddrA sl) s = ok true := + Je.sys.sysl.sys.stz.1.2.1 _ hsO (by rw [hcap]; rfl) + have hsb : sb.val ≤ 39 := J.utBlk _ hsO p sb fi false hcap + refine untyped_decode_refinesA ar Z.th s a σ Z.G hR hV hP hO hTr hC hsub hfm vi hvi hvirt hfan lbl slbl hlbl + length buf args hlen hargs sl hAs hsO p sb fi false hcap hrv hsb R.utv.2.2 ex exs hex _ ?_ + intro nt us dest off win t rd reset fiv _ X hU + have hres : reset = false := by + rw [X.resetEq] + exact decide_eq_false (fun h => h hchild) + subst hres + generalize (⟨Fl4SpecA64.alignUp (p.bv + fiv.bv) (zOfA nt.val us.val)⟩ : U64) = aligned at hU ⊢ + -- the caller's restart + obtain ⟨Rp', Nt', extra', sp', hPpl⟩ := RS.pl + obtain ⟨xT, hkT, hst, -, J2, Y2, hOS1, -⟩ := J.of_restartCurA hPpl RS.sym RS.run D.nvm RS.cur RS.vs RS.vc + rw [hst, bind_ok] + set s2 := L.wr s (stA (L.curW s)) sched.TS_RESTART with hs2 + set σ2 := stUpdA σ σ.curThread xT .restart with hσ2 + have C : CrtP st H Z.Cs H.frame OS s2 (cslotAddrA sl) p sb fi false aligned nt us dest off win := + CrtP.of0 R hst hU + obtain ⟨-, hal8, -, hA0, hAw, -, -, H', -, hHH', hHn, hEx⟩ : + UtAr H p sb fi false aligned nt us dest off win := R.arith _ _ _ _ _ _ _ hU + have hTr2 : TreeA st OS Z.G.chains Z.G.par s2 := treeA_of_mem hTr hOS1 + have hS2 : StateRelA Z.th (L.mem s2) σ2 := J2.j.1.st + have hC2 : CdtRelA st Z.th s2 σ2 Z.G := J2.j.1.cdt + have hks : L.ksb s2 = L.ksb s := L.ksb_wr _ _ _ + have hrev : revA (L.mem s2) (cslotAddrA sl) = true := by + have e24 := (hOS1 _ hsO).2.2.2 + have h0 := revA_of_revocableA hrv + unfold revA at h0 ⊢ + rw [e24]; exact h0 + have hc2 : capA st s2 (cslotAddrA sl) = .Untyped p sb fi false := by rw [hOS1.capA hsO]; exact hcap + obtain ⟨hP', hno⟩ := hnew aligned nt us dest off win hU + have hb0 : 0 < aligned.val := by + have hp0 : 0 < p.val := J.sys.sysl.ly.ut0 _ hsO p sb fi hcap + have g := (CrtA.of_args R hU).pa + omega + -- the window is aligned to the object size + have CA := CrtA.of_args R hU + have h1 : p.val + fi.val ≤ aligned.val := C.ar.2.2.2.1 + have h2 : aligned.val + win.val * 2 ^ (zA nt.val us).val ≤ p.val + 2 ^ sb.val := C.ar.2.2.2.2.1 + have hal2 : aligned.val % 2 ^ (zA nt.val us).val = 0 := by + obtain ⟨y, -, -, idx, dep, val, ers, hv64, -, -, -, eal, -, -⟩ := id hU + obtain ⟨-, -, ev⟩ := untyped.retype_size.okA ers + rw [ev] at eal hv64 + simp only [Bool.false_eq_true, ↓reduceIte] at eal + have fw : (core.num.U64.wrapping_add p fi).val = p.val + fi.val := by + rw [wa_valP] + refine Nat.mod_eq_of_lt ?_ + have := CA.pf + have := C.ar.2.2.1 + omega + have hw1 : 2 ^ (zA nt.val us).val ≤ win.val * 2 ^ (zA nt.val us).val := + Nat.le_mul_of_pos_left _ (by have := X.window.1; omega) + have g := align_up_valA hv64 (by rw [fw]; have := CA.pf; omega) eal + rw [g] + exact Nat.mul_mod_left _ _ + have hpf : aligned.val + win.val * 2 ^ (zA nt.val us).val ≤ 2 ^ 64 := le_trans h2 CA.pf + have hks : L.ksb s2 = L.ksb s := L.ksb_wr _ _ _ + have sA : Aligned (stA (L.curW s)) := by + rw [stA_eq]; exact field_aligned (J.sq.tcbs _ J.sq.curT) (by simp) (by simp) + have noch : ¬ ∃ v, cdt.ensure_no_children st s (cslotAddrA sl) = ok (.Ok v) := by + obtain ⟨-, -, -, -, -, -, -, -, -, -, hres, -⟩ := id hU + exact fun g => absurd (hres.2 g) (by simp) + -- a new `HighVSpace` window is in the kernel window: its untyped is ([J] `utWin`) + have hkw : nt.val = 14 → 18446603336221196288 ≤ aligned.val := fun _ => by + have w := (J.utWin _ hsO p sb fi hcap).1 + have e : layout.PPTR_BASE.val = 18446603336221196288 := by simp [layout.PPTR_BASE] + omega + obtain ⟨H', hUe, hJ7⟩ := utRt_j7RetA hA J.sys D P RS.sym hcapF J.j7 J.ko hst a hU + refine WP.spec_mono (WP.spec_and + (utCreate_recA hA J J2 C X hcap hsrc hUe hb0 hal8 hpf hal2 (by rw [hks]; exact hP') hno hVP hKt hK0 + hkw noch RS.vs (fun b hb hne => mem_wr_ne' sA hb hne.symm)) hJ7) fun res g => ?_ + obtain ⟨⟨hok, K⟩, Vn, lo, hi, rt, arm, ko', j7at⟩ := g + refine ⟨_, _, _, _, _, rfl, hok, xT, hkT, aligned, nt, us, dest, off, win, hU, fun r hr => ?_⟩ + obtain ⟨vm, Ac', g⟩ := K r hr H' Vn _ _ lo hi _ rt arm ko' j7at + exact ⟨vm, _, _, _, _, _, _, _, _, _, _, _, _, _, g, rfl, rfl⟩ + +end site2 + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtDecRecRstA.lean b/proofs/Fl4ProofsA64/P3/UtDecRecRstA.lean new file mode 100644 index 000000000..001bff763 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtDecRecRstA.lean @@ -0,0 +1,386 @@ +/- +P3 (aarch64, A64-P3 Untyped, reset path): **the decode-level top theorem of a RAM retype that resets**: +`untyped::decode` refines the spec, and the whole record `JStage3A` holds after every outcome: after every outcome of +the spec's `invoke_untyped` body that follows a successful `reset_untyped_cap`, and after a preempted reset (over the +dropped sets). + +* **`utRt_j7RstA`**: P1's creation top after a reset at the retype level (`untyped.retype.s1aTopRst`) with the `J7A` + hook (`utRt_j7RetA` for the reset path). +* **`utDecode_recResetA`**: `utDecode_resetPostA`'s proof with `utRt_j7RstA` and `utCreate_recRstA` at each of the + three creations (unused untyped, small reset, chunked reset). + +For an outcome `r` of the body the record holds at `utInvStA … r.2` (membership `UtInvVcA`, `P3/UtApRec6A`). + +Premises: those of `utDecode_resetPostA`. New theorems only. +-/ +import Fl4ProofsA64.P1.S1aKtRetype +import Fl4ProofsA64.P3.UtRtRecRstA +import Fl4ProofsA64.P3.UtRtResetCrtA +import Fl4ProofsA64.P3.UtFullJ7A +import Fl4ProofsA64.P1.S1aRtTopRst + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4SpecA64 Fl4Spec Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +set_option maxRecDepth 100000 + +section top +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + +set_option maxHeartbeats 4000000 in +/-- **P1's creation top after a reset, at the retype level, with `J7A`** (module docstring). -/ +theorem utRt_j7RstA (hA : P0.ArchTotal ar) {H : Heap} {Z : XSetsA} + {OS U Ac Pp0 Tb0 Ud Acd : Set U64} {Lv0 : U64 → Nat} {ia ie : Nat} {s s2 s3 : S} {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} {n : U64} + (I : SysInvA st H Z.Cs OS U Ac Pp0 Tb0 Lv0 ia ie s) {extra : Aeneas.Std.Array obj.Slot 3#usize} + {slot p sb fi : U64} + (D : DecPre st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ud Acd s (.Untyped p sb fi false) + extra) + (P : SysPlace st H Z.Cs H.frame H.replies H.ntfns OS s extra slot) (Y : SymL st H Z.Cs s) + (hcap : obj.Slot.cap st slot s = ok (.Untyped p sb fi false)) + (J : J7A st (J7R2A st ar n) Z H OS s σ m) (ko : HeapKsOff H (L.ksb s)) + (e : sched.set_thread_state st s (L.curW s) sched.TS_RESTART = ok s2) {a a3 : A} + {aligned nt us dest off win : U64} + (hU : UtArgs st s extra slot p sb fi false true aligned nt us dest off win) + (eres : untyped.reset_untyped st ar s2 a slot = ok (core.result.Result.Ok (), s3, a3)) : + ∃ H' : Heap, + untyped.retype st ar s3 a3 slot false aligned nt us dest off win false ⦃ r => + ∃ lo hi : Nat, UtEnd st H Z.Cs OS s slot p sb false (H.drop (RegionD H p.val (p.val + 2 ^ sb.val))) H' + (OffR p.val (p.val + 2 ^ sb.val) OS) s3 r.2.1 lo hi aligned nt us dest off win ⦄ ∧ + untyped.retype st ar s3 a3 slot false aligned nt us dest off win false ⦃ r => + ∃ (Vn : Set U64) (lo hi : Nat), + S1aRtIn st H Z.Cs OS Ud Acd s ia ie slot p sb fi false extra (H.drop (RegionD H p.val (p.val + 2 ^ sb.val))) + H' (OffR p.val (p.val + 2 ^ sb.val) OS) s3 r.2.1 lo hi aligned nt us dest off win ∧ + ArmQIVAt st H Z.Cs OS ia ie s H' + (s1aRtSlots (OffR p.val (p.val + 2 ^ sb.val) OS) false nt us aligned win) Vn r.2.1 ∧ + HeapKsOff H' (L.ksb s) ∧ + ∀ (Z' : XSetsA) (σ' : Fl4SpecA64.State Unit) (m' : Fl4SpecA64.McState), + UtJ7SplitA st Z Z' (s1aRtSlots (OffR p.val (p.val + 2 ^ sb.val) OS) false nt us aligned win) s r.2.1 slot p + sb nt us aligned win.val → + J7A st (J7R2A st ar n) Z' H' + (s1aRtSlots (OffR p.val (p.val + 2 ^ sb.val) OS) false nt us aligned win) r.2.1 σ' m' ⦄ := by + obtain ⟨Pt, Ub0, vr0, pools, C0, K0, vc, frame, hic0, sched, apart, frub, alub, vcub, Pp, Tb, Hr, Te, Kt, Lv, Re, bits, + h, sep, KLv, ktab, hik⟩ := J + have vr := utAlUb_vrA vr0 + have C := utAlUb_colA C0 + have K := utAlUb_capA K0 + have al := utAlUb_alA Ub0 + have B := I.sysl + have R := UtDecP.of_live D B.sys P hcap B.ln B.vr B.ly + have offState : (false : Bool) = false → + (L.ksb s).val + 788736 ≤ p.val ∨ p.val + 2 ^ sb.val ≤ (L.ksb s).val := fun _ => + (R.stz.2.2 slot R.src p sb fi R.cap).2 + obtain ⟨-, -, -, -, -, -, -, H', hNew, hHH', hHn, hEx⟩ : + UtAr (H.drop (RegionD H p.val (p.val + 2 ^ sb.val))) p sb 0#u64 false aligned nt us dest off win := + R.arith _ _ _ _ _ _ _ hU + refine ⟨H', WP.spec_mono (utEnd_resetA hA R B.ly e a a3 hU eres hHH' hEx hHn) fun _ h => ⟨_, _, h⟩, ?_⟩ + refine WP.spec_mono (WP.spec_and (untyped.retype.s1aTopRst D P B hcap e hU eres hA Y vr pools C K al vc h sep ktab + hNew hHH' hEx hHn) (untyped.retype.newHighInitRst D P B hcap e hU eres hA (Kt1.ktab ktab))) ?_ + rintro ⟨r, q, a'⟩ ⟨⟨Vn, Vc'', rt, arm, tab, hsub, h4, h5, h6, words, roots, paths, fresh, keep, init⟩, fr1⟩ + have E := rt.creation + have X := rt.kept + have after : SysL st H' Z.Cs (s1aRtSlots (OffR p.val (p.val + 2 ^ sb.val) OS) false nt us aligned win) ia ie q := + arm.1 + obtain ⟨_, _, _, kinds, _, _⟩ := after.sys.stz.1.1.mdbS + have ks' := E.heapKsOff X offState ko + have devs := UtEnd.devFrN D P B hcap E X rt.window frame + have hic := E.hiCap511 X B.ly.ksw init keep hic0 + have tables := UtEnd.ptCapHG D P B hcap rt.creation rt.kept rt.window rt.reset h.pageTables h.footprint + have R2 := rt.r2 h sep words tab roots paths fresh kinds after.sys.fr hic ks' tables + refine ⟨Vn, _, _, rt, arm, ks', fun Z' σ' m' F => ?_⟩ + exact utRt_j7MadeA rt vr C K al sched apart h sep (arm.2.2.2.2.2.2.1 pools) h5 h6 devs hic R2 rfl F + (utAlUb_frA frub) (fun b hb m => vcub b hb m.1) ktab hik words fr1 + +end top + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rq : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s : S} {a : A} {σ : State Unit} {m : McState} + +/-- The record after a creation over the sets `Z0` and view `m0`: for every outcome of the spec body from `σ3`. -/ +def UtRstRecA (AR : A → HwRootA → Prop) (Z0 : XSetsA) (m0 : McState) (sl : CslotPtr) (p aligned nt us dest off + win : U64) (t : ApiObjectType) (rd : Nat) (σ3 : State Unit) (s' : S) (a' : A) (Lv : U64 → Nat) (ia ie n : Nat) + (ar : machine.Arch A) (nA : U64) (st' : machine.Store S) [LawfulStore st'] : Prop := + ∀ r ∈ (utRtBodyA sl (specPtr p) (specPtr aligned) t us.val (windowSlotsA dest rd off.val win.val) false σ3).1, + UtInvVcA t (specPtr aligned) us.val (windowSlotsA dest rd off.val win.val) r.2 ∧ + ∃ (H'' : Heap) (OS'' U'' Ac' Pp'' Tb'' N'' E'' Rp'' : Set U64) (Bf'' : Set (U64 × Fl4SpecA64.Word)) + (stack'' : U64 → List U64) (κ'' : U64 → U64), + JStage3A (S := S) st' AR (utRecZA Z0 (cslotAddrA sl) nt us aligned dest off win) H'' OS'' U'' Ac' Pp'' Tb'' Lv ia + ie N'' E'' Rp'' Bf'' stack'' κ'' n (J5A st') (J7A st' (J7R2A st' ar nA)) s' a' + (utInvStA t (specPtr aligned) us.val (windowSlotsA dest rd off.val win.val) r.2) + (utRtMA m0 (UtOfTyA 1 nt us aligned win.val)) + +set_option maxHeartbeats 4000000 in +/-- **The decode-level top, with a reset** (module docstring). -/ +theorem utDecode_recResetA [Fact (n ≤ Fl4SpecA64.fl4A64Platform.numNodes)] (hA : P0.ArchTotal ar) + (sact : SActA σ) + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rq Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {p sb fi : U64} (ex : Aeneas.Std.Array obj.Slot 3#usize) + (sl : CslotPtr) (hsrc : ∃ q, sl.1 = specPtr q ∧ Z.th q = some (.cnode sl.2.length)) + {Ud Acd : Set U64} + (D : DecPre st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ud Acd s (.Untyped p sb fi false) ex) + (P : SysPlace st H Z.Cs H.frame H.replies H.ntfns OS s ex (cslotAddrA sl)) + (hcapF : obj.Slot.cap st (cslotAddrA sl) s = ok (.Untyped p sb fi false)) + (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) + (hfm : A64FrameMasks st s) + (vi : U64) (hvi : ar.virtualization a = ok vi) (hvirt : vi = 0#u64 ↔ fl4A64Virt = false) + (hfan : Fl4SpecA64.retypeFanOutLimit = 256#64) + (lbl : U64) (slbl : Fl4SpecA64.Data) + (hlbl : Fl4SpecA64.genInvocationType slbl = .untypedRetype ↔ lbl = fl4_abi.label.UntypedRetype) + (length buf : U64) (args : List U64) (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) + (exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) (hex : ExtraOKA Z.th (L.mem s) ex exs) + (hnochild : ¬ ∃ x, σ.cdt x = some sl) + (hnew : ∀ aligned nt us dest off win : U64, + UtArgs st s ex (cslotAddrA sl) p sb fi false true aligned nt us dest off win → + PlaceA (utRtThA (dropThA (utDA p sb.val) Z.th) false nt us aligned win.val) (L.ksb s) ∧ + PlaceA (utRtThA Z.th false nt us aligned win.val) (L.ksb s) ∧ + ∀ j, j < win.val → utRangedA nt.val → SpNoOvA (bjA aligned (zA nt.val us) j) (zOfA nt.val us.val)) + (othT : ∀ c ∈ Z.Cs, c ≠ L.cpu s → specPtr (L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART) + (L.nodeAt (L.wr s (stA (L.curW s)) sched.TS_RESTART) c layout.node.CUR_THREAD)) ∉ utDA p sb.val) + (othC : ∀ c ∈ Z.Cs, c ≠ L.cpu s → specPtr (L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART) + (L.nodeAt (L.wr s (stA (L.curW s)) sched.TS_RESTART) c layout.node.CUR_SC)) ∉ utDA p sb.val) + -- the decode-site's own state has no other core's current thread or SC in the region either + (othT0 : ∀ c ∈ Z.Cs, c ≠ L.cpu s → specPtr (L.mem s (L.nodeAt s c layout.node.CUR_THREAD)) ∉ utDA p sb.val) + (othC0 : ∀ c ∈ Z.Cs, c ≠ L.cpu s → specPtr (L.mem s (L.nodeAt s c layout.node.CUR_SC)) ∉ utDA p sb.val) + -- the reset + (hn : nA.val = fl4A64Platform.s2IpaBits) + (hPost : ∀ σ' : State Unit, ((), σ') ∈ (archPostResetUntyped (Z := Unit) false sb.val σ').1) + (hchunk : resetChunkBits = 16) + (hnd : numDomains = 1) {Clock : A → MachineState → Prop} (Pc : NowAgreeA ar Clock) + (hvp : ∀ (s : S) (a : A) (σ : State Unit), + JStage3A st AR (dropZA (utDA p sb.val) Z) (dropHA (utDA p sb.val) H) (dropSetA (utDA p sb.val) OS) U Ac + (dropSetA (utDA p sb.val) Pp) Tb Lv ia ie (dropSetA (utDA p sb.val) N) (dropSetA (utDA p sb.val) E) + (dropSetA (utDA p sb.val) Rq) (utBfOffA (utDA p sb.val) (dropBfA (utDA p sb.val) Bf)) stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s a σ (dropMA (utDA p sb.val) m) → + ∀ b, VsR (L.ksb s) (dropSetA (utDA p sb.val) Pp ∪ Tb) b → Aligned b ∧ ¬ PPW st s b) + (hC : ∀ i, fl4RawCounterOracle i ≤ fl4RawCounterOracle (i + 1)) + (hNow : ∀ σ : State Unit, fl4ReadRawCounter σ.machineState = getCurrentTime σ.machineState) + (hAr : ∀ (s : S) (a : A), AR a Z.hw → sched.update_timestamp st ar s a ⦃ r => AR r.2 Z.hw ⦄) + (hirq : ∀ (a : A) (σ : State Unit), AR a Z.hw → Clock a σ.machineState → + ar.irq_pending a ⦃ r => ∃ o ms, (o, { σ with machineState := ms }) ∈ + (doMachineOp (getActiveIRQ true) σ).1 ∧ (r.1 = true ↔ ¬ o = none) ∧ + ms.underlyingMemory = σ.machineState.underlyingMemory ∧ + ms.lastMachineTime = σ.machineState.lastMachineTime ∧ ms.timeState = σ.machineState.timeState ∧ + Clock r.2 ms ∧ AR r.2 Z.hw ⦄) + (hmb : ∀ a : A, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hT0 : ∀ xT, σ.kheap σ.curThread = some (.tcb xT) → + UtPpTimeA Clock a (utDelStA (specPtr p) sb.val (stUpdA σ σ.curThread xT .restart))) + -- the arch facts of the creation + (hVP : ∀ a1 : A, UtVcpuPlatA ar a1) {k : U64 → U64} + (hKt : ∀ (a1 : A) (i : U64), i.val < 511 → ar.kernel_ttbr1_entry a1 0#u64 i = ok (k i)) + (hK0 : ∀ i : U64, i.val < 256 → k i = 0#u64) : + untyped.decode st ar s a lbl length (cslotAddrA sl) (.Untyped p sb fi false) buf ex ⦃ res => + UPostA σ (fl4DecodeUntypedInvocationVirt (Z := Unit) slbl (args.map UScalar.bv) sl + (absCapA (.Untyped p sb fi false)) (exs.map Prod.fst)) s a + (fun inv res => ∃ (base retypeBase : Word) (t : ApiObjectType) (us : Nat) (slots : List CslotPtr), + inv = .retype sl true base retypeBase t us slots false ∧ + ∃ xT, σ.kheap σ.curThread = some (.tcb xT) ∧ + ∃ (aligned nt usF dest off win : U64) (rd : Nat), + UtArgs st s ex (cslotAddrA sl) p sb fi false true aligned nt usF dest off win ∧ + base = specPtr p ∧ retypeBase = specPtr aligned ∧ us = usF.val ∧ + slots = windowSlotsA dest rd off.val win.val ∧ + ((res.1 = .Ok () ∧ ∃ σ3, + (.ok (), σ3) ∈ ((resetUntypedCap (Z := Unit) sl).run (stUpdA σ σ.curThread xT .restart)).1 ∧ + ∃ (Z0 : XSetsA) (m0 : McState), + (Z0 = Z ∧ m0 = m ∨ Z0 = dropZA (utDA p sb.val) Z ∧ m0 = dropMA (utDA p sb.val) m) ∧ + UtRstRecA AR Z0 m0 sl p aligned nt usF dest off win t rd σ3 res.2.1 res.2.2 Lv ia ie n ar nA st) + ∨ (res.1 = .Err .Preempted ∧ ∃ σ3, + (.error (), σ3) ∈ ((resetUntypedCap (Z := Unit) sl).run (stUpdA σ σ.curThread xT .restart)).1 ∧ + JStage3A st AR (dropZA (utDA p sb.val) Z) (dropHA (utDA p sb.val) H) (dropSetA (utDA p sb.val) OS) + U Ac (dropSetA (utDA p sb.val) Pp) Tb Lv ia ie (dropSetA (utDA p sb.val) N) + (dropSetA (utDA p sb.val) E) (dropSetA (utDA p sb.val) Rq) + (utBfOffA (utDA p sb.val) (dropBfA (utDA p sb.val) Bf)) stack κ n (J5A st) (J7A st (J7R2A st ar nA)) + res.2.1 res.2.2 σ3 (dropMA (utDA p sb.val) m)))) res ⦄ := by + have hsub : ∀ y, IsSlotA Z.th y → y ∈ OS := J.j5.2.1 + have hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null := J.j5.2.2 + obtain ⟨qc, hq, hth⟩ := hsrc + have hAs : AbsSlotA Z.th sl := ⟨qc, hq, Or.inl hth⟩ + have hsO : cslotAddrA sl ∈ OS := hsub _ (hAs.isSlot J.j.1.place) + have R := UtDecP.of_live D J.sys.sysl.sys P hcapF J.sys.sysl.ln J.sys.sysl.vr J.sys.sysl.ly + have hcap : capA st s (cslotAddrA sl) = .Untyped p sb fi false := R.cap + refine utDecode_resetA J ex sl ⟨qc, hq, hth⟩ D P hcapF RS hfm vi hvi hvirt hfan lbl slbl hlbl length buf args hlen + hargs exs hex hnochild _ ?_ + intro aligned nt us dest off win t rd xT fiv X hU hkT _ _ _ + obtain ⟨x0, hxm, hx0, idx, dep, val, ers, hv64, erd, ees, hrs, eal, -⟩ := id hU + have he := hrs.1 rfl + obtain ⟨⟨⟩, he⟩ := he + obtain ⟨-, -, -, -, -, -, -, -, -, -, hrs0, -⟩ := id hU + have he0 := hrs0.1 rfl + -- the record and the reset's inputs at the restarted state + obtain ⟨y, hk, hst, -, Y⟩ := utRstReady_restartA sact J D P hcapF he RS.sym RS.run RS.cur RS.vs RS.vc othT othC + obtain rfl : y = xT := by + have h := hk.symm.trans hkT + injection h with h + injection h + set s2 := L.wr s (stA (L.curW s)) sched.TS_RESTART with hs2 + set σ2 := stUpdA σ σ.curThread y .restart with hσ2 + have J2 := Y.rec_ + have e : sched.set_thread_state st s (L.curW s) sched.TS_RESTART = ok s2 := hst + -- the drop's facts at the decode's state + have C0 := utDropC_of J D P hcapF he RS.sym othT0 othC0 hn + -- the new base and the window + have CA := CrtA.of_args R hU + have hus : us.val < 64 := X.size.1 + have hnn : nt.val < 15 := CA.hn + have hp0 : 0 < p.val := J.sys.sysl.ly.ut0 _ hsO p sb fi hcap + have hpfit : p.val + 2 ^ sb.val ≤ 2 ^ 64 := al_fit R.utv.1 (by have := R.utv.2.1; omega) p.bv.isLt + have hArU : UtAr (H.drop (RegionD H p.val (p.val + 2 ^ sb.val))) p sb 0#u64 false aligned nt us dest off win := + R.arith _ _ _ _ _ _ _ hU + have hA0 : p.val + (0#u64 : U64).val ≤ aligned.val := hArU.2.2.2.1 + have hAw : aligned.val + win.val * 2 ^ (zA nt.val us).val ≤ p.val + 2 ^ sb.val := hArU.2.2.2.2.1 + have hb0 : 0 < aligned.val := by omega + have hal8 : aligned.val % 8 = 0 := hArU.2.1 + have hpf : aligned.val + win.val * 2 ^ (zA nt.val us).val ≤ 2 ^ 64 := le_trans hAw hpfit + have hal2 : aligned.val % 2 ^ (zA nt.val us).val = 0 := by + obtain ⟨-, -, ev⟩ := untyped.retype_size.okA ers + rw [ev] at eal hv64 + simp only [↓reduceIte] at eal + have fw : (core.num.U64.wrapping_add p 0#u64).val = p.val := by + rw [wa_valP, show (0#u64 : U64).val = 0 from rfl] + refine Nat.mod_eq_of_lt ?_ + have := p.bv.isLt + simpa using this + have hw1 : 2 ^ (zA nt.val us).val ≤ win.val * 2 ^ (zA nt.val us).val := + Nat.le_mul_of_pos_left _ (by have := X.window.1; omega) + have g := align_up_valA hv64 (by rw [fw]; omega) eal + rw [g] + exact Nat.mul_mod_left _ _ + obtain ⟨hPd, hPk, hno⟩ := hnew aligned nt us dest off win hU + -- a new `HighVSpace` window is in the kernel window: its untyped is ([J] `utWin`) + have hkw' : nt.val = 14 → 18446603336221196288 ≤ aligned.val := fun _ => by + have w := (J.utWin _ hsO p sb fi hcap).1 + have e : layout.PPTR_BASE.val = 18446603336221196288 := by simp [layout.PPTR_BASE] + have : p.val ≤ aligned.val := by simpa using hA0 + omega + have kb2 : L.ksb s2 = L.ksb s := L.ksb_wr _ _ _ + -- the arithmetic's grown heap + obtain ⟨-, -, -, -, -, -, -, H', -, hHH', hHn, hEx⟩ := id hArU + -- the drop's slot facts + obtain ⟨-, -, -, -, -, -, -, -, -, -, -, -, -, -, -, hnd0, -⟩ := R.restart e he0 + have F := DropF.of R (R.drop rfl he0) hnd0 + obtain ⟨-, -, -, -, -, -, vc, -⟩ := id J.j7 + have vcD : VcCap st (dropSetA (utDA p sb.val) Z.V) OS s := fun y hy v hc => by + have yD : y ∈ dropSetA (utDA p sb.val) OS := by + rw [← C0.slotsB] + exact F.nn y hy (by rw [hc]; exact fun h => by cases h) + exact ⟨vc y hy v hc, C0.vcpuN y yD v hc⟩ + have holeD : ∀ q k, dropHA (utDA p sb.val) H q = some k → q.val + 2 ^ k.bits ≤ p.val ∨ p.val + 2 ^ sb.val ≤ q.val := by + rw [← C0.heapB]; exact F.hole + -- the creation over the dropped sets, after a used untyped's reset + have drop : ∀ (s3 : S) (a3 : A) (σ3 : State Unit), + untyped.reset_untyped st ar s2 a (cslotAddrA sl) = ok (core.result.Result.Ok (), s3, a3) → + JStage3A st AR (dropZA (utDA p sb.val) Z) (dropHA (utDA p sb.val) H) (dropSetA (utDA p sb.val) OS) U Ac + (dropSetA (utDA p sb.val) Pp) Tb Lv ia ie (dropSetA (utDA p sb.val) N) (dropSetA (utDA p sb.val) E) + (dropSetA (utDA p sb.val) Rq) (utBfOffA (utDA p sb.val) (dropBfA (utDA p sb.val) Bf)) stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s3 a3 σ3 (dropMA (utDA p sb.val) m) → + untyped.retype st ar s3 a3 (cslotAddrA sl) false aligned nt us dest off win false ⦃ res => + res.1 = .Ok () ∧ UtRstRecA AR (dropZA (utDA p sb.val) Z) (dropMA (utDA p sb.val) m) sl p aligned nt us dest off + win t rd σ3 res.2.1 res.2.2 Lv ia ie n ar nA st ⦄ := by + intro s3 a3 σ3 eres J3 + obtain ⟨C1, -, hw1⟩ := utCrtP_resetA hA R e a a3 hU eres + have kb3 : L.ksb s3 = L.ksb s := hw1.1.trans kb2 + rw [C0.heapB, C0.slotsB] at C1 + have C : CrtP st (dropHA (utDA p sb.val) H) Z.Cs (dropHA (utDA p sb.val) H).frame (dropSetA (utDA p sb.val) OS) s3 + (cslotAddrA sl) p sb 0#u64 false aligned nt us dest off win := + { heap := C1.heap, os := J3.sys.ent.os, stz := C1.stz, fr := C1.fr, cz := C1.cz, src := C1.src, cap := C1.cap, + hn := C1.hn, hus := C1.hus, hz := C1.hz, ar := C1.ar, core0 := C1.core0, n0 := C1.n0, + nc := fun hb => capw_ne_node J3.sq hb C1.core0 (o := layout.node.DEBUG_TCBS) (by rw [debug_val]; omega) rfl, + hd := C1.hd, dO := C1.dO, dn := C1.dn, dc := C1.dc, zO := C1.zO } + obtain ⟨hdest, hrd, hrd0, yc, rr, gs, g, ySlot, ycap⟩ := X.cnode + have yS' : IsSlotA (dropThA (utDA p sb.val) Z.th) yc := by + by_contra h + have := C0.gone yc (hsub yc ySlot) ySlot h + rw [this] at ycap + cases ycap + have destK : specPtr dest ∉ utDA p sb.val := by + have := C0.noCap yc yS' + rw [show capAtA (L.mem s) yc = capA st s yc from rfl, ycap] at this + exact this + have X' : UtCtxA st (dropZA (utDA p sb.val) Z).th s σ sl p sb fi false nt us dest off win t rd true fiv := + { X with cnode := ⟨dropThA_someA.2 ⟨destK, hdest⟩, hrd, hrd0, yc, rr, gs, g, yS', ycap⟩ } + obtain ⟨-, -, -, -, hth', -⟩ := utReset_startA J D P hcapF he RS.sym othT0 othC0 hn sl hq hth rfl + obtain ⟨H', hUe, hJ7⟩ := utRt_j7RstA hA J.sys D P RS.sym hcapF J.j7 J.ko e hU eres + rw [C0.heapB, C0.slotsB] at hUe hJ7 + obtain ⟨offr, offr2⟩ := utRt_offSetsA (st := st) (s := s) (slot := cslotAddrA sl) (p := p) (sb := sb) J3.setsInH + J3.sys.ent.heap holeD + refine WP.spec_mono (WP.spec_and (utCreate_recRstA hA J J3 C X' hcap ⟨qc, hq, hth'⟩ hUe hb0 hal8 hpf hal2 + (by rw [kb3]; exact hPd) hno hVP hKt hK0 hkw' (Z0 := dropZA (utDA p sb.val) Z) rfl (fun _ h => h.1) + (fun _ h => h.1) (fun _ h => h.1) (fun _ h => h.1) (fun _ h => h.1) (fun _ h => h.1) offr offr2 vcD) hJ7) fun res g => ?_ + obtain ⟨⟨hok, K⟩, Vn, lo, hi, rt, arm, ko', j7at⟩ := g + refine ⟨hok, fun r hr => ?_⟩ + obtain ⟨vm, Ac', g⟩ := K r hr _ Vn _ _ lo hi _ rt arm ko' j7at + exact ⟨vm, _, _, _, _, _, _, _, _, _, _, _, _, g⟩ + refine utRetype_resetPpA ar sl hq hth Y hn hPost hchunk hnd Pc hvp hC hNow hAr hirq hmb (hT0 y hkT) aligned nt us dest + off win _ ?hnoop ?hsmall ?hbig ?hpre + case hpre => + intro s3 a3 σ3 hm J3 + exact ⟨_, _, _, _, _, rfl, y, hkT, aligned, nt, us, dest, off, win, rd, hU, rfl, rfl, rfl, rfl, + Or.inr ⟨rfl, σ3, hm, J3⟩⟩ + case hsmall => + intro _ _ s3 σ3 eres hm J3 _ _ + refine WP.spec_mono (drop s3 a σ3 eres J3) fun res h => ?_ + exact ⟨_, _, _, _, _, rfl, y, hkT, aligned, nt, us, dest, off, win, rd, hU, rfl, rfl, rfl, rfl, + Or.inl ⟨h.1, σ3, hm, _, _, Or.inr ⟨rfl, rfl⟩, h.2⟩⟩ + case hbig => + intro _ _ s3 a3 σ3 _ _ eres hm I _ + refine WP.spec_mono (drop s3 a3 σ3 eres I.rec_) fun res h => ?_ + exact ⟨_, _, _, _, _, rfl, y, hkT, aligned, nt, us, dest, off, win, rd, hU, rfl, rfl, rfl, rfl, + Or.inl ⟨h.1, σ3, hm, _, _, Or.inr ⟨rfl, rfl⟩, h.2⟩⟩ + case hnoop => + intro h0 eres hm + obtain ⟨C1, -, -⟩ := utCrtP_resetA hA R e a a hU eres + have hc2 : capA st s2 (cslotAddrA sl) = .Untyped p sb 0#u64 false := by + rw [← h0]; exact okinj ((capA_eq s2 _).symm.trans Y.cap) + have offH : ∀ q k, H q = some k → q.val + 2 ^ k.bits ≤ p.val ∨ p.val + 2 ^ sb.val ≤ q.val := by + intro q k hk + have g := J2.sys.sysl.sys.stz.1.2.2.1 (cslotAddrA sl) hsO p sb 0#u64 hc2 q k hk + simpa using g + have eH : H.drop (RegionD H p.val (p.val + 2 ^ sb.val)) = H := by + funext q + unfold Heap.drop + cases hk : H q with + | none => split_ifs <;> rfl + | some k => + have nR : ¬ RegionD H p.val (p.val + 2 ^ sb.val) q := fun hR => by + unfold RegionD MeetsR at hR + rw [hk] at hR + simp only [Option.elim] at hR + have := offH q k hk + omega + rw [ite_eq_right nR] + have eO : OffR p.val (p.val + 2 ^ sb.val) OS = OS := by + refine Set.ext fun z => ⟨fun h => h.1, fun h => ⟨h, ?_⟩⟩ + obtain ⟨Pt, Ub7, vr, pool, armc, capc, vcc, frd, hic, sched, apart, frub, alub, vcub, Pp7, Tb7, Hr, Te, Kt, Lv7, Re, bits, r2, + kt, hkl⟩ := J2.j7 + obtain ⟨-, q, k, hk, h1, h2, -, -⟩ := armc.case.slot z h + have := offH q k hk + have := Nat.two_pow_pos sb.val + omega + rw [eH, eO] at C1 + obtain ⟨H', hUe, hJ7⟩ := utRt_j7RstA hA J.sys D P RS.sym hcapF J.j7 J.ko e hU eres + rw [eH, eO] at hUe hJ7 + obtain ⟨offr, offr2⟩ := utRt_offSetsA (st := st) (s := s) (slot := cslotAddrA sl) (p := p) (sb := sb) J2.setsInH + J2.sys.ent.heap offH + refine WP.spec_mono (WP.spec_and (utCreate_recRstA hA J J2 C1 X hcap ⟨qc, hq, hth⟩ hUe hb0 hal8 hpf hal2 + (by rw [kb2]; exact hPk) hno hVP hKt hK0 hkw' (Z0 := Z) rfl subset_rfl subset_rfl subset_rfl subset_rfl + subset_rfl subset_rfl offr offr2 vc) hJ7) fun res g => ?_ + obtain ⟨⟨hok, K⟩, Vn, lo, hi, rt, arm, ko', j7at⟩ := g + refine ⟨_, _, _, _, _, rfl, y, hkT, aligned, nt, us, dest, off, win, rd, hU, rfl, rfl, rfl, rfl, + Or.inl ⟨hok, σ2, hm, Z, m, Or.inl ⟨rfl, rfl⟩, fun r hr => ?_⟩⟩ + obtain ⟨vm, Ac', g⟩ := K r hr _ Vn _ _ lo hi _ rt arm ko' j7at + exact ⟨vm, _, _, _, _, _, _, _, _, _, _, _, _, g⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtDecodeA.lean b/proofs/Fl4ProofsA64/P3/UtDecodeA.lean new file mode 100644 index 000000000..8c172e180 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtDecodeA.lean @@ -0,0 +1,1072 @@ +/- +P3 (aarch64, A64-P3 Untyped W1b): **`untyped::decode` against `fl4DecodeUntypedInvocationVirt`** +(`crates/fl4-core/src/untyped.rs`; x86 `P3/UntypedDecode`). + +* **`retype_size_casesA`**, **`retype_dest_*A`**, **`ensure_empty_slots_eqA`**, **`align_up_eqA`**: FL4's helpers in + closed form. +* **`window_empty_runA`**: the spec's `ensure_empty` over the retype window, against FL4's slots. +* **`ut_labelA`**: `hlbl` holds at the label word `fl4DecodeInvocation` passes (`specLabelWord (fl4LabelOf lbl)`). +* **`UtCtxA`**: what the decode has established when it calls `retype`. +* **`untyped_decode_refinesA`**, in continuation form: an error leaves both states unchanged with the related spec + error; otherwise the spec decodes an invocation and the hypothesis `Q` speaks of `set_thread_state Restart` followed + by `untyped::retype`. + +Premises: `A64FrameMasks` [P] (no shared frames, so `UntypedRetypeShared` is `IllegalOperation`), the virtualization +agreement [P] (`hvirt`), `retypeFanOutLimit = 256` [P] (`hfan`), `size_bits ≤ 39` [J] (`hsb`, from J4 `UtBlk39A`). +`hfi` (the free index is within the block) is A64 P1's `UtRV.utv` at the entry. The rest are decode-site facts, as `decInv_asidControl_all_jA` reads them. New module; no existing statement changed. +-/ +import Fl4ProofsA64.P3.UtSpecA +import Fl4ProofsA64.P3.VsTopPtA +import Fl4ProofsA64.P3.NoChildA +import Fl4ProofsA64.P3.CnodeCopyA +import Fl4ProofsA64.P3.FrontEndExtraA +import Fl4ProofsA64.P1.MdbSysZ +import Fl4ProofsA64.P1.MdbTreeL +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## Pure helpers -/ + +theorem zA_valA (nt us : U64) (hn : nt.val < 15) (hus : us.val < 64) : (zA nt.val us).val = zOfA nt.val us.val := by + have h4 : (wa us 5#u64).val = 5 + us.val := by + rw [wa_valP] + have : (5#u64 : U64).val = 5 := rfl + omega + unfold zA zOfA + generalize nt.val = n at hn ⊢ + interval_cases n <;> simp [h4] + +/-- `retype_size` in closed form. -/ +theorem retype_size_casesA (nt us : U64) : + untyped.retype_size nt us = ok ( + if 15 ≤ nt.val then .Err (.Sys (.InvalidArgument 0#u64)) + else if 64 ≤ us.val ∨ 47 < zOfA nt.val us.val then .Err (.Sys (.RangeError 0#u64 47#u64)) + else if nt.val = 4 ∧ us.val = 0 then .Err (.Sys (.InvalidArgument 1#u64)) + else if nt.val = 0 ∧ us.val < 4 then .Err (.Sys (.InvalidArgument 1#u64)) + else if nt.val = 5 ∧ us.val < 7 then .Err (.Sys (.InvalidArgument 1#u64)) + else .Ok (zA nt.val us)) := by + have hC : (fl4_abi.consts.object.Count).val = 15 := by simp [fl4_abi.consts.object.Count] + unfold untyped.retype_size + by_cases h15 : 15 ≤ nt.val + · have : nt >= fl4_abi.consts.object.Count := by + show (fl4_abi.consts.object.Count).val ≤ nt.val + omega + rw [ite_eq_left this, ite_eq_left h15]; rfl + have hge : ¬ nt >= fl4_abi.consts.object.Count := by + show ¬ (fl4_abi.consts.object.Count).val ≤ nt.val + omega + have hn : nt.val < 15 := by omega + rw [ite_eq_right hge, ite_eq_right h15, object_size_eqA nt us hn, bind_ok] + dsimp only + have hM : fl4_abi.consts.bits.MAX_UNTYPED = 47#u64 := by simp [fl4_abi.consts.bits.MAX_UNTYPED] + by_cases hu : 64 ≤ us.val + · have : us >= 64#u64 := by scalar_tac + rw [ite_eq_left this, ite_eq_left (Or.inl hu), hM]; rfl + have hu' : ¬ us >= 64#u64 := by scalar_tac + have hz := zA_valA nt us hn (by omega) + rw [ite_eq_right hu'] + by_cases hz47 : 47 < zOfA nt.val us.val + · have : zA nt.val us > fl4_abi.consts.bits.MAX_UNTYPED := by rw [hM]; scalar_tac + rw [ite_eq_left this, ite_eq_left (Or.inr hz47), hM]; rfl + have hz' : ¬ zA nt.val us > fl4_abi.consts.bits.MAX_UNTYPED := by rw [hM]; scalar_tac + rw [ite_eq_right hz', ite_eq_right (not_or.2 ⟨hu, hz47⟩)] + have eC : nt = fl4_abi.consts.object.CapTable ↔ nt.val = 4 := by + rw [u64_eq_iffA]; simp [fl4_abi.consts.object.CapTable] + have eU : nt = fl4_abi.consts.object.Untyped ↔ nt.val = 0 := by + rw [u64_eq_iffA]; simp [fl4_abi.consts.object.Untyped] + have eS : nt = fl4_abi.consts.object.SchedContext ↔ nt.val = 5 := by + rw [u64_eq_iffA]; simp [fl4_abi.consts.object.SchedContext] + have u0 : us = 0#u64 ↔ us.val = 0 := by rw [u64_eq_iffA]; simp + have u4 : us < fl4_abi.consts.bits.MIN_UNTYPED ↔ us.val < 4 := by + show us.val < (fl4_abi.consts.bits.MIN_UNTYPED).val ↔ _ + simp [fl4_abi.consts.bits.MIN_UNTYPED] + have u7 : us < fl4_abi.consts.bits.MIN_SCHED_CONTEXT ↔ us.val < 7 := by + show us.val < (fl4_abi.consts.bits.MIN_SCHED_CONTEXT).val ↔ _ + simp [fl4_abi.consts.bits.MIN_SCHED_CONTEXT] + simp only [eC, eU, eS, u0, u4, u7, error.invalid_arg] + by_cases c4 : nt.val = 4 + · have c0 : ¬ nt.val = 0 := by omega + have c5 : ¬ nt.val = 5 := by omega + by_cases hus0 : us.val = 0 + · simp [c4, hus0] + · simp [c4, hus0] + by_cases c0 : nt.val = 0 + · have c5 : ¬ nt.val = 5 := by omega + by_cases hus4 : us.val < 4 + · simp [c0, hus4] + · simp [c0, hus4] + by_cases c5 : nt.val = 5 + · by_cases hus7 : us.val < 7 + · simp [c5, hus7] + · simp [c5, hus7] + simp [c4, c0, c5] + +theorem align_up_eqA (v z : U64) (hz : z.val < 64) : + untyped.align_up v z = ok ⟨Fl4SpecA64.alignUp v.bv z.val⟩ := by + obtain ⟨i1, e1, hi1⟩ := spec_exists_dut (shl1_val hz) + have h63 : (z &&& 63#u64).val = z.val := by + have : (z &&& 63#u64).val = z.val % 64 := by simp; exact Nat.and_two_pow_sub_one_eq_mod z.val 6 + omega + have e1' : i1.bv = BitVec.ofNat 64 (2 ^ z.val) := by + apply BitVec.eq_of_toNat_eq + show i1.val = _ + rw [hi1] + have : 2 ^ z.val < 2 ^ 64 := Nat.pow_lt_pow_right (by decide) hz + exact (Nat.mod_eq_of_lt this).symm + unfold untyped.align_up + simp only [lift, bind_ok] + rw [e1, bind_ok] + congr 1 + rw [UScalar.eq_equiv_bv_eq] + simp only [UScalar.bv_and, UScalar.bv_not, core.num.U64.wrapping_add_bv_eq, core.num.U64.wrapping_sub_bv_eq, e1', + Fl4SpecA64.alignUp] + congr 1 + simp only [BitVec.sub_eq_add_neg, BitVec.add_assoc] + rfl + +/-- `retype_dest`'s range checks on a CNode of radix `r` (below 64). -/ +def destResA (ptr : U64) (r : Nat) (off win : U64) : core.result.Result U64 error.Exc := + if off.val > 2 ^ r - 1 then .Err (.Sys (.RangeError 0#u64 ⟨BitVec.ofNat 64 (2 ^ r - 1)⟩)) + else if win.val < 1 ∨ win.val > 256 then .Err (.Sys (.RangeError 1#u64 256#u64)) + else if win.val > 2 ^ r - off.val then .Err (.Sys (.RangeError 1#u64 ⟨BitVec.ofNat 64 (2 ^ r - off.val)⟩)) + else .Ok ptr + +/-- `retype_dest` after reading the node cap. -/ +def tailOfA (dep off win : U64) : capability.Cap → core.result.Result U64 error.Exc + | .CNode ptr r _ _ => destResA ptr r.val off win + | _ => .Err (.Sys (.FailedLookup false (.MissingCapability dep))) + +theorem u64_ofNat_eqA (x : U64) (n : Nat) (h : x.val = n) : x = ⟨BitVec.ofNat 64 n⟩ := by + apply UScalar.eq_of_val_eq + have : x.val < 2 ^ 64 := by have := x.hBounds; simpa [U64.size_def, UScalarTy.U64_numBits_eq, U64.numBits] using this + show x.val = (BitVec.ofNat 64 n).toNat + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)]; omega + +theorem cnode_tailA (dep off win : U64) (c : capability.Cap) (hc : CapWfA c) : + (do + let o ← untyped.cnode_of c + match o with + | none => + ok (core.result.Result.Err (error.Exc.Sys (error.SysError.FailedLookup + false (error.LookupFault.MissingCapability dep)))) + | some x => + let (dest, radix) := x + let i ← lift (radix &&& 63#u64) + let node_size ← 1#u64 <<< i + let i1 ← lift (core.num.U64.wrapping_sub node_size 1#u64) + if off > i1 + then + let i2 ← lift (core.num.U64.wrapping_sub node_size 1#u64) + error.range Std.U64 0#u64 i2 + else + if win < 1#u64 + then error.range Std.U64 1#u64 untyped.RETYPE_FAN_OUT_LIMIT + else + if win > untyped.RETYPE_FAN_OUT_LIMIT + then error.range Std.U64 1#u64 untyped.RETYPE_FAN_OUT_LIMIT + else + let i2 ← lift (core.num.U64.wrapping_sub node_size off) + if win > i2 + then + let i3 ← lift (core.num.U64.wrapping_sub node_size off) + error.range Std.U64 1#u64 i3 + else ok (core.result.Result.Ok dest) : Result (core.result.Result U64 error.Exc)) = + ok (tailOfA dep off win c) := by + cases c <;> try (simp only [untyped.cnode_of, bind_ok]; rfl) + case CNode ptr r gs g => + have hr : r.val < 64 := hc.1 + have h63 : (r &&& 63#u64).val = r.val := by + have : (r &&& 63#u64).val = r.val % 64 := by simp; exact Nat.and_two_pow_sub_one_eq_mod r.val 6 + omega + simp only [untyped.cnode_of, lift, bind_ok, uncurry_apply_pair] + have hp : 2 ^ r.val < 2 ^ 64 := Nat.pow_lt_pow_right (by decide) hr + have hp1 : 1 ≤ 2 ^ r.val := Nat.one_le_two_pow + obtain ⟨ns, ens, hns⟩ := spec_exists_dut (shl1_val (x := r) hr) + rw [ens, bind_ok] + have hs1 : (core.num.U64.wrapping_sub ns 1#u64).val = 2 ^ r.val - 1 := by + simp [core.num.U64.wrapping_sub_val_eq, U64.size_def, UScalarTy.U64_numBits_eq, U64.numBits, hns]; omega + have hso : off.val ≤ 2 ^ r.val → (core.num.U64.wrapping_sub ns off).val = 2 ^ r.val - off.val := by + intro h + simp [core.num.U64.wrapping_sub_val_eq, U64.size_def, UScalarTy.U64_numBits_eq, U64.numBits, hns]; omega + simp only [tailOfA, destResA, untyped.RETYPE_FAN_OUT_LIMIT, error.range] + by_cases c1 : off.val > 2 ^ r.val - 1 + · have : off > core.num.U64.wrapping_sub ns 1#u64 := by scalar_tac + simp only [this, ↓reduceIte, c1] + rw [← u64_ofNat_eqA _ _ hs1] + have : ¬ off > core.num.U64.wrapping_sub ns 1#u64 := by scalar_tac + simp only [this, ↓reduceIte, c1] + have hso' := hso (by omega) + by_cases c2 : win.val < 1 + · have : win < 1#u64 := by scalar_tac + simp [this, c2] + have : ¬ win < 1#u64 := by scalar_tac + simp only [this, ↓reduceIte, c2, false_or] + by_cases c3 : win.val > 256 + · have : win > 256#u64 := by scalar_tac + simp [this, c3] + have : ¬ win > 256#u64 := by scalar_tac + simp only [this, ↓reduceIte, c3] + by_cases c4 : win.val > 2 ^ r.val - off.val + · have : win > core.num.U64.wrapping_sub ns off := by scalar_tac + simp only [this, ↓reduceIte, c4] + rw [← u64_ofNat_eqA _ _ hso'] + have : ¬ win > core.num.U64.wrapping_sub ns off := by scalar_tac + simp only [this, ↓reduceIte, c4] + +/-! ## The label -/ + +set_option maxRecDepth 8000 in +theorem labNA_retypeA : ∀ n, n < 74 → + (labNA n = .spec (.genInvocationLabel .untypedRetype) ↔ n = 1) := by + decide + +theorem genInvocationType_retype_utA (l : Fl4SpecA64.Fl4Label) : + Fl4SpecA64.genInvocationType (Fl4SpecA64.specLabelWord l) = .untypedRetype ↔ + l = .spec (.genInvocationLabel .untypedRetype) := by + cases l with + | spec y => + have e : Fl4SpecA64.invocationType (Fl4SpecA64.specLabelWord (.spec y)) = y := invocationType_fromEnumA y + unfold Fl4SpecA64.genInvocationType + rw [e] + cases y with + | genInvocationLabel g => simp + | archInvocationLabel x => simp + | ext x => + have h0 : Fl4SpecA64.specLabelWord (.ext x) = + BitVec.ofNat 64 (Fl4SpecA64.InvocationLabel.genInvocationLabel .invalidInvocation).fromEnum := by + show (0 : Fl4SpecA64.Data) = _; decide + unfold Fl4SpecA64.genInvocationType + rw [h0, invocationType_fromEnumA] + simp + +/-- **The decode's label hypothesis** at the word `fl4DecodeInvocation` passes on. -/ +theorem ut_labelA (lbl : U64) : + Fl4SpecA64.genInvocationType (Fl4SpecA64.specLabelWord (Fl4SpecA64.fl4LabelOf lbl.bv)) = .untypedRetype ↔ + lbl = fl4_abi.label.UntypedRetype := by + rw [genInvocationType_retype_utA, fl4LabelOf_bvA, u64_eq_iffA, + Fl4ProofsA64.P0.Consts.fl4_abi.label.UntypedRetype.val_eq] + by_cases h : lbl.val < 74 + · exact labNA_retypeA _ h + · rw [labNA_geA (by omega)] + constructor + · intro e; cases e + · intro e; omega + +/-! ## The window -/ + +/-- The slots of a retype window, as the spec lists them. -/ +def windowSlotsA (dest : U64) (r off win : Nat) : List Fl4SpecA64.CslotPtr := + (List.range' off win).map fun i => (specPtr dest, Fl4SpecA64.natToCref r i) + +theorem natToCref_utA (r n : Nat) (hr : r ≤ 64) : Fl4SpecA64.natToCref r n = bitsOfA (n % 2 ^ 64) r := by + unfold Fl4SpecA64.natToCref Fl4SpecA64.wordBits + rw [toBl_dropA _ r hr, BitVec.toNat_ofNat] + +theorem natToCref_length_utA (r n : Nat) (hr : r ≤ 64) : (Fl4SpecA64.natToCref r n).length = r := by + rw [natToCref_utA r n hr]; simp + +/-- Slot `i` of a CNode of radix `r` is at FL4's `Slot::index`. -/ +theorem cslotAddr_windowA (dest : U64) (r : Nat) (hr : r ≤ 64) (i : Nat) (hi : i < 2 ^ r) : + cslotAddrA (specPtr dest, Fl4SpecA64.natToCref r i) = slotAddrA (specPtr dest) (BitVec.ofNat 64 i) := by + have h64 : i < 2 ^ 64 := Nat.lt_of_lt_of_le hi (Nat.pow_le_pow_right (by omega) hr) + have hof : Fl4SpecA64.ofBl (Fl4SpecA64.natToCref r i) = BitVec.ofNat 64 i := by + rw [natToCref_utA r i hr, ofBl_bitsOfA _ r hr, Nat.mod_eq_of_lt h64, Nat.mod_eq_of_lt hi] + unfold cslotAddrA; dsimp only; rw [hof] + +/-- A related cap is null exactly when the spec's is. -/ +theorem capRel_null_utA {c : capability.Cap} {a : Fl4SpecA64.Cap} (h : CapRelA c a) : + c = .Null ↔ a = .nullCap := by + obtain ⟨hwf, he⟩ := h + rcases CapEqA.eq_or_pt he with h | ⟨p', asid', va', h⟩ + · rw [h]; exact (absA_null_iff hwf).symm + · constructor + · rintro rfl + have : Fl4SpecA64.Cap.nullCap = a := he + exact this.symm + · intro hn; rw [hn] at h; cases h + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- `retype_dest`, depth 0: the checks on the root cap. -/ +theorem retype_dest_zeroA (s : S) (x0 idx off win : U64) (hw : CapWfA (capA st s x0)) : + untyped.retype_dest st s x0 idx 0#u64 off win = ok (tailOfA 0#u64 off win (capA st s x0)) := by + unfold untyped.retype_dest + simp only [↓reduceIte, capA_eq, bind_ok] + exact cnode_tailA _ _ _ _ hw + +/-- `retype_dest`, depth not 0, the lookup finds slot `v`. -/ +theorem retype_dest_okA (s : S) (x0 idx dep off win : U64) (h0 : dep ≠ 0#u64) (v : U64) + (hl : cspace.lookup_target_slot st s (capA st s x0) idx dep = ok (.Ok v)) (hw : CapWfA (capA st s v)) : + untyped.retype_dest st s x0 idx dep off win = ok (tailOfA dep off win (capA st s v)) := by + unfold untyped.retype_dest + simp only [h0, ↓reduceIte, capA_eq, hl, bind_ok, core.result.Result.Insts.CoreOpsTry.branch] + exact cnode_tailA _ _ _ _ hw + +/-- `retype_dest`, depth not 0, the lookup fails with `e`. -/ +theorem retype_dest_errA (s : S) (x0 idx dep off win : U64) (h0 : dep ≠ 0#u64) (e : error.Exc) + (hl : cspace.lookup_target_slot st s (capA st s x0) idx dep = ok (.Err e)) : + untyped.retype_dest st s x0 idx dep off win = ok (.Err e) := by + unfold untyped.retype_dest + simp only [h0, ↓reduceIte, capA_eq, hl, bind_ok, core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, bind_tc_ok] + +/-- `ensure_empty_slots cnode from to`: `Ok` iff every slot of the range is null, else `DeleteFirst`. -/ +theorem ensure_empty_slots_eqA (s : S) (cnode fr tp : U64) (hle : fr.val ≤ tp.val) : + untyped.ensure_empty_slots st s cnode fr tp = ok + (if ∀ i : Nat, fr.val ≤ i → i < tp.val → capA st s (slotAddrA (specPtr cnode) (BitVec.ofNat 64 i)) = .Null + then .Ok () else .Err (.Sys .DeleteFirst)) := by + unfold untyped.ensure_empty_slots untyped.ensure_empty_slots_loop + let P : Nat → Prop := fun i => ∀ j : Nat, fr.val ≤ j → j < i → + capA st s (slotAddrA (specPtr cnode) (BitVec.ofNat 64 j)) = .Null + let rv : Nat → core.result.Result Unit error.Exc := fun i => if P i then .Ok () else .Err (.Sys .DeleteFirst) + have goal : loop (fun x => untyped.ensure_empty_slots_loop.body st s cnode tp x.1 x.2) + ((core.result.Result.Ok () : core.result.Result Unit error.Exc), fr) ⦃ r => r = rv tp.val ⦄ := by + refine loop.spec_decr_nat (measure := fun x => tp.val - x.2.val) + (inv := fun x => fr.val ≤ x.2.val ∧ x.2.val ≤ tp.val ∧ x.1 = rv x.2.val) _ _ _ ?_ ?_ + · rintro ⟨r, i⟩ ⟨h1, h2, hr⟩ + unfold untyped.ensure_empty_slots_loop.body + simp only at h1 h2 hr ⊢ + by_cases hit : i < tp + · rw [ite_eq_left hit] + have hit' : i.val < tp.val := hit + by_cases hP : P i.val + · have hro : r = .Ok () := by rw [hr]; simp [rv, hP] + subst hro + simp only [core.result.Result.is_ok, bind_ok, ↓reduceIte] + obtain ⟨x, ex, hx⟩ := spec_exists_dut (slot_index_specA cnode i) + rw [ex, bind_ok] + have hiv : i.bv = BitVec.ofNat 64 i.val := by simp + rw [hiv] at hx + unfold cdt.ensure_empty_slot + rw [capA_eq, bind_ok] + obtain ⟨i1, hi1, hv⟩ := spec_exists_dut (U64.add_spec (x := i) (y := 1#u64) (by scalar_tac)) + have hi1v : i1.val = i.val + 1 := by simpa using hv + have hPs : P i1.val ↔ capA st s x = .Null := by + rw [hi1v, hx] + constructor + · intro h; exact h i.val h1 (by omega) + · intro h j hj1 hj2 + by_cases hj : j = i.val + · rw [hj]; exact h + · exact hP j hj1 (by omega) + by_cases hn : capA st s x = .Null + · rw [hn, cap_is_null_nullA] + simp only [bind_ok, ↓reduceIte, hi1, WP.spec_ok] + exact ⟨⟨by omega, by omega, (ite_eq_left (hPs.2 hn)).symm⟩, by omega⟩ + · rw [cap_is_null_neA hn] + simp only [bind_ok, Bool.false_eq_true, ↓reduceIte, hi1, WP.spec_ok] + exact ⟨⟨by omega, by omega, (ite_eq_right (fun h => hn (hPs.1 h))).symm⟩, by omega⟩ + · have hre : r = .Err (.Sys .DeleteFirst) := by rw [hr]; simp [rv, hP] + subst hre + simp only [core.result.Result.is_ok, Bool.false_eq_true, ↓reduceIte, bind_ok, WP.spec_ok] + exact (ite_eq_right (fun h => hP (fun j a b => h j a (by omega)))).symm + · rw [ite_eq_right hit, WP.spec_ok] + have : i.val = tp.val := by have : ¬ i.val < tp.val := hit; omega + rw [hr, this] + · exact ⟨le_refl _, hle, (ite_eq_left (fun j h1 h2 => absurd h2 (Nat.not_lt.2 h1))).symm⟩ + obtain ⟨r, er, hr⟩ := spec_exists_dut goal + exact er.trans (by rw [hr]) + +/-- The spec's `ensure_empty` over a retype window, against FL4's slots. -/ +theorem window_empty_runA (th : TypedHeapA) (s : S) (σ : Fl4SpecA64.State Unit) (hR : StateRelA th (L.mem s) σ) + (ptr : U64) (r : Nat) (hr : r < 64) (hth : th ptr = some (.cnode r)) (off win : Nat) (hw : off + win ≤ 2 ^ r) : + (NonDetE.mapMEx (Fl4SpecA64.ensureEmpty (Z := Unit)) (windowSlotsA ptr r off win)).run σ = + ({(if ∀ i : Nat, off ≤ i → i < off + win → capA st s (slotAddrA (specPtr ptr) (BitVec.ofNat 64 i)) = .Null + then .ok () else .error .deleteFirst, σ)}, False) := by + let capsOf : Fl4SpecA64.CslotPtr → Fl4SpecA64.Cap := fun sl => + if h : ∃ a, Fl4SpecA64.getCap (Z := Unit) sl σ = ({(a, σ)}, False) then h.choose else .nullCap + have key : ∀ i, off ≤ i → i < off + win → + CapRelA (capA st s (slotAddrA (specPtr ptr) (BitVec.ofNat 64 i))) + (capsOf (specPtr ptr, Fl4SpecA64.natToCref r i)) ∧ + Fl4SpecA64.getCap (Z := Unit) (specPtr ptr, Fl4SpecA64.natToCref r i) σ = + ({(capsOf (specPtr ptr, Fl4SpecA64.natToCref r i), σ)}, False) := by + intro i h1 h2 + obtain ⟨c, hc, hg⟩ := hR.slot_capA (sl := (specPtr ptr, Fl4SpecA64.natToCref r i)) (p := ptr) rfl + (by rw [natToCref_length_utA _ _ (by omega)]; exact hth) + have hex : ∃ a, Fl4SpecA64.getCap (Z := Unit) (specPtr ptr, Fl4SpecA64.natToCref r i) σ = ({(a, σ)}, False) := + ⟨c, hg⟩ + have hcs : capsOf (specPtr ptr, Fl4SpecA64.natToCref r i) = c := by + simp only [capsOf, dite_eq_left hex] + have h := hex.choose_spec.symm.trans hg + simp only [Prod.mk.injEq, Set.singleton_eq_singleton_iff] at h + exact h.1.1 + rw [hcs] + refine ⟨?_, hg⟩ + rw [← cslotAddr_windowA ptr r (by omega) i (by omega)]; exact hc + have hiff : (∀ sl ∈ windowSlotsA ptr r off win, capsOf sl = .nullCap) ↔ + (∀ i : Nat, off ≤ i → i < off + win → capA st s (slotAddrA (specPtr ptr) (BitVec.ofNat 64 i)) = .Null) := by + simp only [windowSlotsA, List.mem_map, List.mem_range'_1, forall_exists_index, and_imp] + constructor + · intro h i h1 h2 + exact (capRel_null_utA (key i h1 h2).1).2 (h _ i h1 h2 rfl) + · rintro h _ i h1 h2 rfl + exact (capRel_null_utA (key i h1 h2).1).1 (h i h1 h2) + rw [mapMEx_ensureEmpty_runA σ capsOf] + · by_cases hb : ∀ i : Nat, off ≤ i → i < off + win → + capA st s (slotAddrA (specPtr ptr) (BitVec.ofNat 64 i)) = .Null + · rw [ite_eq_left hb, ite_eq_left (hiff.2 hb)] + · rw [ite_eq_right hb, ite_eq_right (fun h => hb (hiff.1 h))] + · simp only [windowSlotsA, List.mem_map, List.mem_range'_1, forall_exists_index, and_imp] + rintro _ i h1 h2 rfl + exact (key i h1 h2).2 + +end + +/-! ## The decode -/ + +/-- After `decode`, either FL4 returned the spec's error with nothing changed, or the spec decodes an invocation and +`Q` relates it to FL4's result. -/ +def UPostA {S A : Type} (σ : Fl4SpecA64.State Unit) (m : Fl4SpecA64.SEMonad Unit Fl4SpecA64.UntypedInvocation) + (s : S) (a : A) (Q : Fl4SpecA64.UntypedInvocation → core.result.Result Unit error.Exc × S × A → Prop) + (res : core.result.Result Unit error.Exc × S × A) : Prop := + (∃ e e', res = (.Err e, s, a) ∧ m.run σ = ({(.error e', σ)}, False) ∧ SysErrRelA e e') ∨ + (∃ inv, m.run σ = ({(.ok inv, σ)}, False) ∧ Q inv res) + +theorem UPostA.congr {S A : Type} {σ : Fl4SpecA64.State Unit} + {m m' : Fl4SpecA64.SEMonad Unit Fl4SpecA64.UntypedInvocation} (h : m.run σ = m'.run σ) {s : S} {a : A} {Q} + {prog : Result (core.result.Result Unit error.Exc × S × A)} + (hp : prog ⦃ res => UPostA σ m' s a Q res ⦄) : prog ⦃ res => UPostA σ m s a Q res ⦄ := by + refine WP.spec_mono hp ?_ + rintro res (⟨e, e', h1, h2, h3⟩ | ⟨inv, h2, h3⟩) + · exact Or.inl ⟨e, e', h1, h.trans h2, h3⟩ + · exact Or.inr ⟨inv, h.trans h2, h3⟩ + +theorem UPostA.err {S A : Type} {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.SEMonad Unit Fl4SpecA64.UntypedInvocation} {s : S} {a : A} {Q} {e : error.Exc} + {e' : Fl4SpecA64.SyscallError} (h : m.run σ = ({(.error e', σ)}, False)) (he : SysErrRelA e e') : + (ok ((.Err e, s, a) : core.result.Result Unit error.Exc × S × A)) ⦃ res => UPostA σ m s a Q res ⦄ := + (WP.spec_ok _).2 (Or.inl ⟨e, e', rfl, h, he⟩) + +/-- What `decode` has established when it calls `retype`: type `nt`, size `us`, destination CNode `dest` of radix +`r`, window `off`..`off + win`, `reset`, and the free index `fiv` it starts from. -/ +structure UtCtxA {S : Type} (st : machine.Store S) [L : LawfulStore st] (th : TypedHeapA) (s : S) + (σ : Fl4SpecA64.State Unit) (sl : Fl4SpecA64.CslotPtr) (p sb fi : U64) (dev : Bool) + (nt us dest off win : U64) (t : Fl4SpecA64.ApiObjectType) (r : Nat) (reset : Bool) (fiv : U64) : Prop where + ty : objTypeOfA nt.val = some t + size : us.val < 64 ∧ zOfA nt.val us.val ≤ 47 ∧ ¬ (nt.val = 4 ∧ us.val = 0) ∧ ¬ (nt.val = 0 ∧ us.val < 4) ∧ + ¬ (nt.val = 5 ∧ us.val < 7) + /-- The destination CNode, of radix `r`, named by a CNode cap held in a slot of `th`. -/ + cnode : th dest = some (.cnode r) ∧ r < 64 ∧ r ≠ 0 ∧ + ∃ y rr gs g, IsSlotA th y ∧ capA st s y = .CNode dest rr gs g + window : 1 ≤ win.val ∧ win.val ≤ 256 ∧ off.val + win.val ≤ 2 ^ r + empty : ∀ i : Nat, off.val ≤ i → i < off.val + win.val → + capA st s (slotAddrA (specPtr dest) (BitVec.ofNat 64 i)) = .Null + resetEq : reset = decide (¬ ∃ x, σ.cdt x = some sl) + fidx : fiv = if reset then 0#u64 else fi + sizeBits : sb.val ≤ 39 + /-- The object size does not exceed the block's. -/ + zle : zOfA nt.val us.val ≤ sb.val + memory : win.val ≤ ((1#64 <<< sb.val : BitVec 64) - fiv.bv).toNat >>> zOfA nt.val us.val + device : dev = true → Fl4SpecA64.isFrameType t = true ∨ t = .untyped + /-- Virtualization is present for the types that need it. -/ + virt : (nt.val = 12 ∨ nt.val = 13) → fl4A64Virt = true + +/-- The common tail of FL4's decode after its virtualization check (the extracted code repeats it per branch). -/ +def utTailA {S : Type} {A : Type} (st : machine.Store S) (ar : machine.Arch A) (s : S) (a : A) (slot : obj.Slot) + (cap : capability.Cap) (s1 : obj.Slot) (ct new_type user_size node_index node_depth node_offset + node_window : U64) : Result ((core.result.Result Unit error.Exc) × S × A) := do + let r ← untyped.retype_size new_type user_size + let cf ← core.result.Result.Insts.CoreOpsTry.branch r + match cf with + | core.ops.control_flow.ControlFlow.Continue val => + let r1 ← untyped.retype_dest st s s1 node_index node_depth node_offset node_window + let cf1 ← core.result.Result.Insts.CoreOpsTry.branch r1 + match cf1 with + | core.ops.control_flow.ControlFlow.Continue val1 => + let i ← lift (core.num.U64.wrapping_add node_offset node_window) + let r2 ← untyped.ensure_empty_slots st s val1 node_offset i + let cf2 ← core.result.Result.Insts.CoreOpsTry.branch r2 + match cf2 with + | core.ops.control_flow.ControlFlow.Continue _ => + let o ← untyped.as_untyped cap + match o with + | none => let r3 ← error.illegal Unit + ok (r3, s, a) + | some u => + let r3 ← cdt.ensure_no_children st s slot + let (free_index, reset) ← + match r3 with + | core.result.Result.Ok _ => ok (0#u64, true) + | core.result.Result.Err _ => ok (u.free_index, false) + let free_ref ← lift (core.num.U64.wrapping_add u.ptr free_index) + let i1 ← lift (u.size_bits &&& 63#u64) + let i2 ← 1#u64 <<< i1 + let free_bytes ← lift (core.num.U64.wrapping_sub i2 free_index) + let i3 ← lift (val &&& 63#u64) + let i4 ← free_bytes >>> i3 + if i4 < node_window + then + ok (core.result.Result.Err (error.Exc.Sys (error.SysError.NotEnoughMemory free_bytes)), s, a) + else + if val >= 64#u64 + then + ok (core.result.Result.Err (error.Exc.Sys (error.SysError.NotEnoughMemory free_bytes)), s, a) + else + if u.is_device + then + let b1 ← objects.is_frame_type new_type + if b1 + then + let aligned ← untyped.align_up free_ref val + let s2 ← sched.set_thread_state st s ct sched.TS_RESTART + untyped.retype st ar s2 a slot reset aligned new_type user_size val1 node_offset node_window + u.is_device + else + if new_type != fl4_abi.consts.object.Untyped + then + let r4 ← error.invalid_arg Unit 1#u64 + ok (r4, s, a) + else + let aligned ← untyped.align_up free_ref val + let s2 ← sched.set_thread_state st s ct sched.TS_RESTART + untyped.retype st ar s2 a slot reset aligned new_type user_size val1 node_offset node_window + u.is_device + else + let aligned ← untyped.align_up free_ref val + let s2 ← sched.set_thread_state st s ct sched.TS_RESTART + untyped.retype st ar s2 a slot reset aligned new_type user_size val1 node_offset node_window + u.is_device + | core.ops.control_flow.ControlFlow.Break residual => + let r3 ← + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual + Unit (core.convert.FromSame error.Exc) residual + ok (r3, s, a) + | core.ops.control_flow.ControlFlow.Break residual => + let r2 ← + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual + Unit (core.convert.FromSame error.Exc) residual + ok (r2, s, a) + | core.ops.control_flow.ControlFlow.Break residual => + let r1 ← + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual + Unit (core.convert.FromSame error.Exc) residual + ok (r1, s, a) + +theorem is_frame_type_eqA (nt : U64) : + objects.is_frame_type nt = ok (decide (nt.val = 7 ∨ nt.val = 9 ∨ nt.val = 10)) := by + have h9 : (fl4_abi.consts.object.ArmSmallPage).val = 9 := by simp [fl4_abi.consts.object.ArmSmallPage] + have h10 : (fl4_abi.consts.object.ArmLargePage).val = 10 := by simp [fl4_abi.consts.object.ArmLargePage] + have h7 : (fl4_abi.consts.object.ArmHugePage).val = 7 := by simp [fl4_abi.consts.object.ArmHugePage] + unfold objects.is_frame_type + simp only [u64_eq_iffA, h9, h10, h7] + split_ifs <;> simp_all + +/-- Simplify FL4's `?` on an error. -/ +macro "ut_break" : tactic => `(tactic| simp only [↓reduceIte, core.result.Result.Insts.CoreOpsTry.branch, bind_ok, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame, + core.convert.FromSame.from, bind_tc_ok]) + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +set_option maxHeartbeats 4000000 in +/-- **FL4's `untyped::decode` refines FL4's untyped decode** (`fl4DecodeUntypedInvocationVirt`), in continuation +form (module docstring). -/ +theorem untyped_decode_refinesA {A : Type} (ar : machine.Arch A) (th : TypedHeapA) (s : S) (a : A) + (σ : Fl4SpecA64.State Unit) (G : MdbGhostA) {T Ω Sg OS : Set U64} + (hR : StateRelA th (L.mem s) σ) (hV : SlotsValidA th (L.mem s)) (hP : PlaceA th (L.ksb s)) + (hO : OSOK T Ω Sg OS) (hTr : TreeA st OS G.chains G.par s) (hC : CdtRelA st th s σ G) + (hsub : ∀ y, IsSlotA th y → y ∈ OS) + (hfm : A64FrameMasks st s) + (vi : U64) (hvi : ar.virtualization a = ok vi) (hvirt : vi = 0#u64 ↔ fl4A64Virt = false) + (hfan : Fl4SpecA64.retypeFanOutLimit = 256#64) + (lbl : U64) (slbl : Fl4SpecA64.Data) + (hlbl : Fl4SpecA64.genInvocationType slbl = .untypedRetype ↔ lbl = fl4_abi.label.UntypedRetype) + (length buf : U64) (args : List U64) (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) + (sl : Fl4SpecA64.CslotPtr) (hAs : AbsSlotA th sl) (hsO : cslotAddrA sl ∈ OS) + (p sb fi : U64) (dev : Bool) (hcap : capA st s (cslotAddrA sl) = .Untyped p sb fi dev) + (hrv : obj.Slot.revocable st (cslotAddrA sl) s = ok true) (hsb : sb.val ≤ 39) (hfi : fi.val ≤ 2 ^ sb.val) + (ex : Aeneas.Std.Array obj.Slot 3#usize) (exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) + (hex : ExtraOKA th (L.mem s) ex exs) + (Q : Fl4SpecA64.UntypedInvocation → core.result.Result Unit error.Exc × S × A → Prop) + (hQ : ∀ (nt us dest off win : U64) (t : Fl4SpecA64.ApiObjectType) (r : Nat) (reset : Bool) (fiv : U64), + args[0]? = some nt → UtCtxA st th s σ sl p sb fi dev nt us dest off win t r reset fiv → + UtArgs st s ex (cslotAddrA sl) p sb fi dev reset + ⟨Fl4SpecA64.alignUp (p.bv + fiv.bv) (zOfA nt.val us.val)⟩ nt us dest off win → + (do + let s2 ← sched.set_thread_state st s (L.curW s) sched.TS_RESTART + untyped.retype st ar s2 a (cslotAddrA sl) reset ⟨Fl4SpecA64.alignUp (p.bv + fiv.bv) (zOfA nt.val us.val)⟩ + nt us dest off win dev) ⦃ res => + Q (.retype sl reset (specPtr p) + (specPtr ⟨Fl4SpecA64.alignUp (p.bv + fiv.bv) (zOfA nt.val us.val)⟩) t us.val + (windowSlotsA dest r off.val win.val) dev) res ⦄) : + untyped.decode st ar s a lbl length (cslotAddrA sl) (.Untyped p sb fi dev) buf ex ⦃ res => + UPostA σ (fl4DecodeUntypedInvocationVirt (Z := Unit) slbl (args.map UScalar.bv) sl + (absCapA (.Untyped p sb fi dev)) (exs.map Prod.fst)) s a Q res ⦄ := by + rw [show absCapA (.Untyped p sb fi dev) = .untypedCap dev (specPtr p) sb.val fi.val from rfl] + have hx0 : Aeneas.Std.Array.index_usize ex 0#usize = ok (exAtA ex 0) := index_exAtA ex 0#usize (by simp) + -- the spec's decode when the request is not refused for virtualization + have other : ¬ fl4UntypedVirtRefused slbl (args.map UScalar.bv) (exs.map Prod.fst) → + fl4DecodeUntypedInvocationVirt (Z := Unit) slbl (args.map UScalar.bv) sl + (.untypedCap dev (specPtr p) sb.val fi.val) (exs.map Prod.fst) = + decodeUntypedInvocationWith (Z := Unit) fl4ObjBitsApi fl4DataToObjType slbl (args.map UScalar.bv) sl + (.untypedCap dev (specPtr p) sb.val fi.val) (exs.map Prod.fst) := + fun h => fl4DecodeUntypedInvocationVirt_other h _ _ + unfold untyped.decode + by_cases hl : lbl = fl4_abi.label.UntypedRetype + swap + · have hne : (lbl != fl4_abi.label.UntypedRetype) = true := by simpa using hl + have hg : ¬ Fl4SpecA64.genInvocationType slbl = .untypedRetype := fun h => hl (hlbl.1 h) + rw [other (fun h => hg h.2.1)] + simp only [hne, ↓reduceIte, hfm.sharing, bind_ok, Bool.false_eq_true, ite_self, error.illegal] + refine UPostA.err (e' := .illegalOperation) ?_ trivial + unfold decodeUntypedInvocationWith + simp only [NonDetE.unlessE, hg, ↓reduceIte] + simp only [NonDetE.throwError, ndE_throw_bindA]; rfl + have hne : (lbl != fl4_abi.label.UntypedRetype) = false := by simpa using hl + have hg : Fl4SpecA64.genInvocationType slbl = .untypedRetype := hlbl.2 hl + simp only [hne, Bool.false_eq_true, ↓reduceIte] + by_cases h6 : length < 6#u64 + · simp only [h6, ↓reduceIte, error.truncated, bind_ok] + have h6s : (args.map UScalar.bv).length < 6 := by simp; scalar_tac + rw [other (fun h => by have := h.2.2.1; omega)] + refine UPostA.err (e' := .truncatedMessage) ?_ trivial + unfold decodeUntypedInvocationWith + simp only [NonDetE.unlessE, hg, ↓reduceIte, ndE_pure_bindA, NonDetE.whenE, h6s, NonDetE.throwError, + ndE_throw_bindA]; rfl + have h6' : ¬ (args.map UScalar.bv).length < 6 := by simp; scalar_tac + simp only [h6, ↓reduceIte, hx0, bind_ok, obj.Slot.is_null] + obtain ⟨hex3, hexS, hexN⟩ := hex + cases exs with + | nil => + have hz : exAtA ex 0 = 0#u64 := hexN 0 (by simp) (by omega) + simp only [hz, decide_true, ↓reduceIte, error.truncated, bind_ok] + rw [other (fun h => h.2.2.2.1 rfl)] + refine UPostA.err (e' := .truncatedMessage) ?_ trivial + unfold decodeUntypedInvocationWith + simp only [NonDetE.unlessE, hg, ↓reduceIte, ndE_pure_bindA, NonDetE.whenE, h6', List.map_nil, List.length_nil, + NonDetE.throwError, ndE_throw_bindA]; rfl + | cons e0 erest => + obtain ⟨ps, hths, hy1, haddr, hrel0⟩ := hexS 0 (by simp) + simp only [List.getElem_cons_zero] at hths hy1 haddr hrel0 + have hAx : AbsSlotA th e0.2 := ⟨ps, hy1, Or.inl hths⟩ + have hxS : IsSlotA th (exAtA ex 0) := haddr ▸ hAx.isSlot hP + have hx0n : ¬ exAtA ex 0 = 0#u64 := hO.nz _ (hsub _ hxS) + have hcv0 : CapValidA th (capA st s (exAtA ex 0)) := hV _ hxS + have hrel0' : CapRelA (capA st s (exAtA ex 0)) e0.1 := hrel0 + simp only [hx0n, decide_false, Bool.false_eq_true, ↓reduceIte] + obtain ⟨nt, us, idx, dep, off, win, rest, rfl⟩ : ∃ nt us idx dep off win rest, + args = nt :: us :: idx :: dep :: off :: win :: rest := by + match args, h6' with + | nt :: us :: idx :: dep :: off :: win :: rest, _ => exact ⟨nt, us, idx, dep, off, win, rest, rfl⟩ + | [], h | [_], h | [_, _], h | [_, _, _], h | [_, _, _, _], h | [_, _, _, _, _], h => simp at h + have hct : sched.cur_thread st s = ok (L.curW s) := cur_thread_ok s + simp only [hct, hargs 0#u64 nt (by simp), hargs 1#u64 us (by simp), hargs 2#u64 idx (by simp), + hargs 3#u64 dep (by simp), hargs 4#u64 off (by simp), hargs 5#u64 win (by simp), bind_ok] + have hV12 : (fl4_abi.consts.object.ArmVcpu).val = 12 := by simp [fl4_abi.consts.object.ArmVcpu] + have hV13 : (fl4_abi.consts.object.ArmStage2VSpace).val = 13 := by + simp [fl4_abi.consts.object.ArmStage2VSpace] + have eV : nt = fl4_abi.consts.object.ArmVcpu ↔ nt.val = 12 := by rw [u64_eq_iffA, hV12] + have eS2 : nt = fl4_abi.consts.object.ArmStage2VSpace ↔ nt.val = 13 := by rw [u64_eq_iffA, hV13] + -- the virtualization check + by_cases hgate : (nt.val = 12 ∨ nt.val = 13) ∧ vi = 0#u64 + · have href : fl4UntypedVirtRefused slbl ((nt :: us :: idx :: dep :: off :: win :: rest).map UScalar.bv) + ((e0 :: erest).map Prod.fst) := by + refine ⟨hvirt.1 hgate.2, hg, by omega, by simp, ?_⟩ + simpa [Fl4SpecA64.dataToNat, Fl4SpecA64.the] using hgate.1 + rw [fl4DecodeUntypedInvocationVirt_refused href] + have hgoal : (ok ((core.result.Result.Err (error.Exc.Sys (error.SysError.InvalidArgument 0#u64)), s, a) : + core.result.Result Unit error.Exc × S × A)) ⦃ res => + UPostA σ (Fl4Spec.NonDetE.throwError (.invalidArgument 0)) s a Q res ⦄ := + UPostA.err (e' := .invalidArgument 0) rfl (by simp [SysErrRelA]) + rcases hgate.1 with h | h + · rw [ite_eq_left (eV.2 h), hvi, bind_ok, ite_eq_left hgate.2] + simp only [error.invalid_arg, bind_ok] + exact hgoal + · have : ¬ nt = fl4_abi.consts.object.ArmVcpu := fun e => by have := eV.1 e; omega + rw [ite_eq_right this, ite_eq_left (eS2.2 h), hvi, bind_ok, ite_eq_left hgate.2] + simp only [error.invalid_arg, bind_ok] + exact hgoal + have hnref : ¬ fl4UntypedVirtRefused slbl ((nt :: us :: idx :: dep :: off :: win :: rest).map UScalar.bv) + ((e0 :: erest).map Prod.fst) := by + intro h + refine hgate ⟨?_, hvirt.2 h.1⟩ + simpa [Fl4SpecA64.dataToNat, Fl4SpecA64.the] using h.2.2.2.2 + have hvt : (nt.val = 12 ∨ nt.val = 13) → fl4A64Virt = true := by + intro h + cases hb : fl4A64Virt with + | true => rfl + | false => exact absurd ⟨h, hvirt.2 hb⟩ hgate + suffices htail : utTailA st ar s a (cslotAddrA sl) (.Untyped p sb fi dev) (exAtA ex 0) (L.curW s) nt us idx dep off + win ⦃ res => UPostA σ (fl4DecodeUntypedInvocationVirt (Z := Unit) slbl + ((nt :: us :: idx :: dep :: off :: win :: rest).map UScalar.bv) sl + (.untypedCap dev (specPtr p) sb.val fi.val) ((e0 :: erest).map Prod.fst)) s a Q res ⦄ by + by_cases h12 : nt = fl4_abi.consts.object.ArmVcpu + · have hv0 : ¬ vi = 0#u64 := fun h => hgate ⟨Or.inl (eV.1 h12), h⟩ + rw [ite_eq_left h12, hvi, bind_ok, ite_eq_right hv0] + exact htail + rw [ite_eq_right h12] + by_cases h13 : nt = fl4_abi.consts.object.ArmStage2VSpace + · have hv0 : ¬ vi = 0#u64 := fun h => hgate ⟨Or.inr (eS2.1 h13), h⟩ + rw [ite_eq_left h13, hvi, bind_ok, ite_eq_right hv0] + exact htail + rw [ite_eq_right h13] + exact htail + rw [other hnref] + unfold utTailA + -- the type and size checks + unfold decodeUntypedInvocationWith + have h6'' : ¬ (List.map UScalar.bv rest).length + 1 + 1 + 1 + 1 + 1 + 1 < 6 := by omega + simp only [NonDetE.unlessE, hg, ↓reduceIte, ndE_pure_bindA, NonDetE.whenE, h6'', List.map_cons, List.length_cons, + Nat.add_one_ne_zero, List.getElem?_cons_zero, List.getElem?_cons_succ, Fl4SpecA64.the, NonDetE.returnOk, + Fl4SpecA64.dataToNat, Fl4SpecA64.unat, UScalar.bv_toNat] + have hd := fl4DataToObjType_runA (Z := Unit) nt.bv σ + rw [retype_size_casesA, bind_ok] + by_cases h15 : 15 ≤ nt.val + · have hn : objTypeOfA nt.bv.toNat = none := (objTypeOfA_none _).2 (by simpa using h15) + rw [hn] at hd + rw [ite_eq_left h15]; ut_break + exact UPostA.err (e' := .invalidArgument 0) (run_bind_errorA hd _) (by simp [SysErrRelA]) + obtain ⟨t, ht⟩ : ∃ t, objTypeOfA nt.val = some t := by + cases h : objTypeOfA nt.val with + | none => exact absurd ((objTypeOfA_none _).1 h) h15 + | some t => exact ⟨t, rfl⟩ + obtain ⟨hn15, hbits, hfr, hut, hct', hsc⟩ := objTypeOfA_some ht us.val + rw [UScalar.bv_toNat, ht] at hd + rw [ite_eq_right h15] + refine UPostA.congr (run_bind_okA hd _) ?_ + simp only [hbits] + by_cases hsA : 64 ≤ us.val ∨ 47 < zOfA nt.val us.val + · rw [ite_eq_left hsA]; ut_break + refine UPostA.err (e' := .rangeError 0 (BitVec.ofNat 64 47)) ?_ ⟨rfl, rfl⟩ + have : ¬ (us.val < Fl4SpecA64.wordBits ∧ zOfA nt.val us.val ≤ Fl4SpecA64.untypedMaxBits) := by + simp only [Fl4SpecA64.wordBits, Fl4SpecA64.untypedMaxBits]; omega + simp only [this, ↓reduceIte, NonDetE.throwError, ndE_throw_bindA]; rfl + have hsA' : us.val < Fl4SpecA64.wordBits ∧ zOfA nt.val us.val ≤ Fl4SpecA64.untypedMaxBits := by + simp only [Fl4SpecA64.wordBits, Fl4SpecA64.untypedMaxBits]; omega + simp only [hsA', and_self, ↓reduceIte, ndE_pure_bindA] + rw [ite_eq_right hsA] + have hus : us.val < 64 := by omega + have hz47 : zOfA nt.val us.val ≤ 47 := by omega + by_cases hsB : nt.val = 4 ∧ us.val = 0 + · rw [ite_eq_left hsB]; ut_break + refine UPostA.err (e' := .invalidArgument 1) ?_ (by simp [SysErrRelA]) + have : t = .capTableObject ∧ us.val = 0 := ⟨hct'.2 hsB.1, hsB.2⟩ + simp only [this, and_self, ↓reduceIte, NonDetE.throwError, ndE_throw_bindA]; rfl + have hsB' : ¬ (t = .capTableObject ∧ us.val = 0) := fun h => hsB ⟨hct'.1 h.1, h.2⟩ + simp only [hsB', ↓reduceIte, ndE_pure_bindA] + rw [ite_eq_right hsB] + by_cases hsC : nt.val = 0 ∧ us.val < 4 + · rw [ite_eq_left hsC]; ut_break + refine UPostA.err (e' := .invalidArgument 1) ?_ (by simp [SysErrRelA]) + have : t = .untyped ∧ us.val < Fl4SpecA64.untypedMinBits := ⟨hut.2 hsC.1, hsC.2⟩ + simp only [this, and_self, ↓reduceIte, NonDetE.throwError, ndE_throw_bindA]; rfl + have hsC' : ¬ (t = .untyped ∧ us.val < Fl4SpecA64.untypedMinBits) := fun h => hsC ⟨hut.1 h.1, h.2⟩ + simp only [hsC', ↓reduceIte, ndE_pure_bindA] + rw [ite_eq_right hsC] + by_cases hsD : nt.val = 5 ∧ us.val < 7 + · rw [ite_eq_left hsD]; ut_break + refine UPostA.err (e' := .invalidArgument 1) ?_ (by simp [SysErrRelA]) + have : t = .schedContextObject ∧ us.val < Fl4SpecA64.minSchedContextBits := ⟨hsc.2 hsD.1, hsD.2⟩ + simp only [this, and_self, ↓reduceIte, NonDetE.throwError, ndE_throw_bindA]; rfl + have hsD' : ¬ (t = .schedContextObject ∧ us.val < Fl4SpecA64.minSchedContextBits) := + fun h => hsD ⟨hsc.1 h.1, h.2⟩ + simp only [hsD', ↓reduceIte, ndE_pure_bindA] + rw [ite_eq_right hsD] + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok] + -- the node cap + have hwf0 : CapWfA (capA st s (exAtA ex 0)) := hrel0'.1 + have hnode : (∃ e e', untyped.retype_dest st s (exAtA ex 0) idx dep off win = ok (.Err e) ∧ + (if dep.val = 0 then pure e0.1 else do + let nodeSlot ← Fl4SpecA64.lookupTargetSlot e0.1 (Fl4SpecA64.dataToCptr idx.bv) dep.val + NonDetE.liftE (Fl4SpecA64.getCap nodeSlot) : Fl4SpecA64.SEMonad Unit Fl4SpecA64.Cap).run σ = + ({(.error e', σ)}, False) ∧ SysErrRelA e e') ∨ + (∃ y, IsSlotA th y ∧ untyped.retype_dest st s (exAtA ex 0) idx dep off win = + ok (tailOfA dep off win (capA st s y)) ∧ + ∃ nc', CapRelA (capA st s y) nc' ∧ + (if dep.val = 0 then pure e0.1 else do + let nodeSlot ← Fl4SpecA64.lookupTargetSlot e0.1 (Fl4SpecA64.dataToCptr idx.bv) dep.val + NonDetE.liftE (Fl4SpecA64.getCap nodeSlot) : Fl4SpecA64.SEMonad Unit Fl4SpecA64.Cap).run σ = + ({(.ok nc', σ)}, False)) := by + by_cases h0 : dep = 0#u64 + · subst h0 + refine Or.inr ⟨_, hxS, retype_dest_zeroA s _ idx off win hwf0, e0.1, hrel0', ?_⟩ + simp only [show (0#u64 : U64).val = 0 from rfl, ↓reduceIte]; rfl + have h0' : dep.val ≠ 0 := fun h => h0 (UScalar.eq_of_val_eq h) + simp only [h0', ↓reduceIte] + obtain ⟨r, hr, e1, he1, hrel⟩ := spec_ex_fd + (lookup_slot_for_cnode_op_refinesA th s σ hR hV false _ e0.1 hrel0' hcv0 idx dep) + have hl' : cspace.lookup_target_slot st s (capA st s (exAtA ex 0)) idx dep = ok r := hr + have hlt : (Fl4SpecA64.lookupTargetSlot (Z := Unit) e0.1 (Fl4SpecA64.dataToCptr idx.bv) dep.val).run σ = + ({(e1, σ)}, False) := he1 + cases r with + | Err e => + cases e1 with + | ok _ => exact hrel.elim + | error e' => + exact Or.inl ⟨e, e', retype_dest_errA s _ idx dep off win h0 e hl', run_bind_errorA hlt _, hrel⟩ + | Ok v => + cases e1 with + | error _ => exact hrel.elim + | ok sl' => + obtain ⟨rfl, pp, hpp1, hpp, -⟩ := hrel + obtain ⟨c', hc', hg'⟩ := hR.slot_capA hpp1 hpp + have hS' : IsSlotA th (cslotAddrA sl') := (show AbsSlotA th sl' from ⟨pp, hpp1, Or.inl hpp⟩).isSlot hP + have hc'' : CapRelA (capA st s (cslotAddrA sl')) c' := hc' + refine Or.inr ⟨_, hS', retype_dest_okA s _ idx dep off win h0 _ hl' hc''.1, c', hc'', ?_⟩ + rw [run_bind_okA hlt, run_liftEA hg'] + rcases hnode with ⟨e, e', hrd, hsn, hrel⟩ | ⟨y, hyS, hrd, nc', hcr, hsn⟩ + · rw [hrd]; ut_break + exact UPostA.err (run_bind_errorA hsn _) hrel + rw [hrd] + refine UPostA.congr (run_bind_okA hsn _) ?_ + simp only [bind_ok] + have hncv : CapValidA th (capA st s y) := hV y hyS + by_cases hcn : ∃ ptr r gs g, capA st s y = .CNode ptr r gs g + swap + · have ht' : tailOfA dep off win (capA st s y) = .Err (.Sys (.FailedLookup false (.MissingCapability dep))) := by + cases hc : capA st s y <;> first | rfl | exact absurd ⟨_, _, _, _, hc⟩ hcn + have hi : Fl4SpecA64.isCnodeCap nc' = false := by + rcases CapEqA.eq_or_pt hcr.2 with h | ⟨_, _, _, h⟩ + · rw [h] + cases hc : capA st s y <;> first | rfl | exact absurd ⟨_, _, _, _, hc⟩ hcn + · rw [h]; rfl + rw [ht']; ut_break + refine UPostA.err (e' := .failedLookup false (.missingCapability dep.val)) ?_ ⟨rfl, rfl⟩ + simp only [hi, Bool.false_eq_true, ↓reduceIte, NonDetE.throwError, ndE_throw_bindA]; rfl + obtain ⟨ptr, r, gs, g, hcy⟩ := hcn + have hnc' : nc' = .cnodeCap (specPtr ptr) r.val (bitsOfA g.val gs.val) := by + have h := hcr.2 + rw [hcy] at h + exact h.symm + have hr64 : r.val < 64 := by have h := hcr.1; rw [hcy] at h; exact h.1 + obtain ⟨hthp, hr0⟩ : th ptr = some (.cnode r.val) ∧ r.val ≠ 0 := by rw [hcy] at hncv; exact hncv + have hpow : 2 ^ r.val < 2 ^ 64 := Nat.pow_lt_pow_right (by decide) hr64 + have hpow1 : 1 ≤ 2 ^ r.val := Nat.one_le_two_pow + rw [hnc', hcy] + simp only [Fl4SpecA64.isCnodeCap, ↓reduceIte, Fl4SpecA64.bitsOf, Fl4SpecA64.objRefOf, tailOfA, destResA, + hfan] + by_cases e1 : off.val > 2 ^ r.val - 1 + · rw [ite_eq_left e1]; ut_break + refine UPostA.err (e' := .rangeError 0 (BitVec.ofNat 64 (2 ^ r.val - 1))) ?_ ⟨rfl, rfl⟩ + simp only [e1, or_true, ↓reduceIte, NonDetE.throwError, ndE_throw_bindA]; rfl + have e1' : ¬ (off.val < 0 ∨ off.val > 2 ^ r.val - 1) := by omega + simp only [e1', ↓reduceIte, ndE_pure_bindA]; rw [ite_eq_right e1] + have h256 : Fl4SpecA64.unat (256#64 : BitVec 64) = 256 := rfl + by_cases e2 : win.val < 1 ∨ win.val > 256 + · rw [ite_eq_left e2]; ut_break + refine UPostA.err (e' := .rangeError 1 256#64) ?_ ⟨rfl, rfl⟩ + simp only [h256, e2, ↓reduceIte, NonDetE.throwError, ndE_throw_bindA]; rfl + simp only [h256, e2, ↓reduceIte, ndE_pure_bindA] + by_cases e3 : win.val > 2 ^ r.val - off.val + · rw [ite_eq_left e3]; ut_break + refine UPostA.err (e' := .rangeError 1 (BitVec.ofNat 64 (2 ^ r.val - off.val))) ?_ ⟨rfl, rfl⟩ + simp only [e3, or_true, ↓reduceIte, NonDetE.throwError, ndE_throw_bindA]; rfl + have e3' : ¬ (win.val < 1 ∨ win.val > 2 ^ r.val - off.val) := by omega + simp only [e3', ↓reduceIte, ndE_pure_bindA]; rw [ite_eq_right e3]; simp only [bind_ok] + -- the window is empty + have hw1 : 1 ≤ win.val ∧ win.val ≤ 256 := by omega + have hwin : off.val + win.val ≤ 2 ^ r.val := by omega + have hsum : (core.num.U64.wrapping_add off win).val = off.val + win.val := by + rw [wa_valP]; omega + simp only [lift, bind_ok] + simp only [ensure_empty_slots_eqA s ptr off _ (show off.val ≤ (core.num.U64.wrapping_add off win).val by omega), + hsum, bind_ok] + have hsl2 : List.map (fun cref => (specPtr ptr, cref)) (List.map (Fl4SpecA64.natToCref r.val) + (List.range' off.val (off.val + win.val - off.val))) = windowSlotsA ptr r.val off.val win.val := by + simp [windowSlotsA, List.map_map, Function.comp_def] + simp only [hsl2] + have hwe := window_empty_runA th s σ hR ptr r.val hr64 hthp off.val win.val hwin + by_cases hemp : ∀ i : Nat, off.val ≤ i → i < off.val + win.val → + capA st s (slotAddrA (specPtr ptr) (BitVec.ofNat 64 i)) = .Null + swap + · rw [ite_eq_right hemp] at hwe; rw [ite_eq_right hemp]; ut_break + exact UPostA.err (e' := .deleteFirst) (run_bind_errorA hwe _) (by simp [SysErrRelA]) + rw [ite_eq_left hemp] at hwe; rw [ite_eq_left hemp] + refine UPostA.congr (run_bind_okA hwe _) ?_ + simp only [bind_ok, untyped.as_untyped] + -- reset: no children + have hnc := noChildren_of_adjA hP hO hTr hC hAs hsO hcap hrv + unfold NoChildrenRelA at hnc + have hres := constOnFailure_runA (Z := Unit) σ sl + simp only [NonDetE.returnOk] at hres + refine UPostA.congr (run_bind_okA (run_liftEA hres) _) ?_ + rw [hnc]; simp only [bind_ok] + obtain ⟨R, hR'⟩ : ∃ R : Bool, R = decide (¬ ∃ x, σ.cdt x = some sl) := ⟨_, rfl⟩ + rw [← hR'] + obtain ⟨fiv, hfiv⟩ : ∃ fiv : U64, fiv = if R then 0#u64 else fi := ⟨_, rfl⟩ + have hfr' : (match (if ∃ x, σ.cdt x = some sl then + (core.result.Result.Err (error.Exc.Sys error.SysError.RevokeFirst) : core.result.Result Unit error.Exc) + else core.result.Result.Ok ()) with + | core.result.Result.Ok _ => (ok (0#u64, true) : Result (U64 × Bool)) + | core.result.Result.Err _ => ok (fi, false)) = ok (fiv, R) := by + by_cases hch : ∃ x, σ.cdt x = some sl + · rw [ite_eq_left hch] + have : R = false := by rw [hR']; simp [hch] + rw [hfiv, this]; rfl + · rw [ite_eq_right hch] + have : R = true := by rw [hR']; simp [hch] + rw [hfiv, this]; rfl + rw [hfr'] + simp only [bind_ok, uncurry_apply_pair] + have hfivv : fiv.val = if R then 0 else fi.val := by rw [hfiv]; cases R <;> rfl + have hspec_fi : (if R = true then 0 else Fl4SpecA64.freeIndexOf + (Fl4SpecA64.Cap.untypedCap dev (specPtr p) sb.val fi.val)) = fiv.val := by + rw [hfivv]; cases R <;> rfl + have hfb : BitVec.ofNat 64 fiv.val = fiv.bv := by + apply BitVec.eq_of_toNat_eq + rw [BitVec.toNat_ofNat] + exact Nat.mod_eq_of_lt fiv.bv.isLt + have hobj : Fl4SpecA64.objSize (.untypedCap dev (specPtr p) sb.val fi.val) = (1#64 <<< sb.val : BitVec 64) := by + simp only [Fl4SpecA64.objSize] + apply BitVec.eq_of_toNat_eq + simp [Nat.shiftLeft_eq] + simp only [hspec_fi, hobj, Fl4SpecA64.getFreeRef, hfb] + have hsb64 : sb.val < 64 := by omega + have h63 : (sb &&& 63#u64).val = sb.val := by + have : (sb &&& 63#u64).val = sb.val % 64 := by simp; exact Nat.and_two_pow_sub_one_eq_mod sb.val 6 + omega + have hzv : (zA nt.val us).val = zOfA nt.val us.val := zA_valA nt us hn15 hus + have hz63 : (zA nt.val us &&& 63#u64).val = zOfA nt.val us.val := by + have : (zA nt.val us &&& 63#u64).val = (zA nt.val us).val % 64 := by + simp; exact Nat.and_two_pow_sub_one_eq_mod _ 6 + omega + obtain ⟨i2, ei2, hi2v⟩ := spec_exists_dut (shl1_val (x := sb) hsb64) + have hp64 : 2 ^ sb.val < 2 ^ 64 := Nat.pow_lt_pow_right (by decide) hsb64 + have hi2 : i2.bv = 1#64 <<< sb.val := by + apply BitVec.eq_of_toNat_eq + show i2.val = _ + rw [hi2v]; simp [Nat.shiftLeft_eq]; omega + rw [ei2, bind_ok] + have hFB : (core.num.U64.wrapping_sub i2 fiv).bv = 1#64 <<< sb.val - fiv.bv := by + rw [core.num.U64.wrapping_sub_bv_eq] + exact congrArg (fun x => x - fiv.bv) hi2 + have hshr : (core.num.U64.wrapping_sub i2 fiv >>> (zA nt.val us &&& 63#u64)) ⦃ w => + w.val = (1#64 <<< sb.val - fiv.bv).toNat >>> zOfA nt.val us.val ⦄ := by + have hlt : (zA nt.val us &&& 63#u64).val < 64 := by omega + step as ⟨w, hw, hwb⟩ + rw [hw, hz63, ← hFB]; rfl + obtain ⟨i4, ei4, hi4⟩ := spec_exists_dut hshr + rw [ei4, bind_ok] + by_cases hm : i4 < win + · rw [ite_eq_left hm] + refine UPostA.err (e' := .notEnoughMemory (1#64 <<< sb.val - fiv.bv)) ?_ + (show (1#64 <<< sb.val - fiv.bv) = (core.num.U64.wrapping_sub i2 fiv).bv from hFB.symm) + have : ((1#64 <<< sb.val - fiv.bv) >>> zOfA nt.val us.val).toNat < win.val := by + simp only [BitVec.toNat_ushiftRight]; rw [← hi4]; exact hm + simp only [this, ↓reduceIte, NonDetE.throwError, ndE_throw_bindA]; rfl + rw [ite_eq_right hm] + have hm' : ¬ ((1#64 <<< sb.val - fiv.bv) >>> zOfA nt.val us.val).toNat < win.val := by + simp only [BitVec.toNat_ushiftRight]; rw [← hi4]; exact hm + simp only [hm', ↓reduceIte, ndE_pure_bindA] + have h64 : ¬ zA nt.val us >= 64#u64 := by + intro h + have : (64#u64 : U64).val ≤ (zA nt.val us).val := h + simp at this; omega + rw [ite_eq_right h64] + have hmem : win.val ≤ ((1#64 <<< sb.val : BitVec 64) - fiv.bv).toNat >>> zOfA nt.val us.val := by + rw [← hi4]; have : ¬ i4.val < win.val := hm; omega + have hzle : zOfA nt.val us.val ≤ sb.val := by + by_contra hc + have hlt : ((1#64 <<< sb.val : BitVec 64) - fiv.bv).toNat < 2 ^ zOfA nt.val us.val := by + refine Nat.lt_of_lt_of_le ?_ (Nat.pow_le_pow_right (by omega) (show sb.val + 1 ≤ zOfA nt.val us.val by omega)) + rw [← hFB] + show (core.num.U64.wrapping_sub i2 fiv).val < 2 ^ (sb.val + 1) + have hfle : fiv.val ≤ 2 ^ sb.val := by rw [hfivv]; split; exacts [Nat.zero_le _, hfi] + have hw : (core.num.U64.wrapping_sub i2 fiv).val = 2 ^ sb.val - fiv.val := by + simp [core.num.U64.wrapping_sub_val_eq, U64.size_def, UScalarTy.U64_numBits_eq, U64.numBits, hi2v] + omega + rw [hw, Nat.pow_succ] + have : 1 ≤ 2 ^ sb.val := Nat.one_le_two_pow + omega + rw [Nat.shiftRight_eq_div_pow, Nat.div_eq_of_lt hlt] at hmem + omega + have hal : untyped.align_up (core.num.U64.wrapping_add p fiv) (zA nt.val us) = + ok ⟨Fl4SpecA64.alignUp (p.bv + fiv.bv) (zOfA nt.val us.val)⟩ := by + rw [align_up_eqA _ _ (by omega), hzv, core.num.U64.wrapping_add_bv_eq] + have hspec_al : Fl4SpecA64.alignUp (specPtr p + fiv.bv) (zOfA nt.val us.val) = + specPtr ⟨Fl4SpecA64.alignUp (p.bv + fiv.bv) (zOfA nt.val us.val)⟩ := by + rw [specPtr_def, specPtr_def, + show p.bv + pptrShiftA + fiv.bv = (p.bv + fiv.bv) + pptrShiftA from bv_add_swapA _ _ _, + alignUp_shiftA _ _ (by omega)] + have hctx : (dev = true → Fl4SpecA64.isFrameType t = true ∨ t = .untyped) → + UtCtxA st th s σ sl p sb fi dev nt us ptr off win t r.val R fiv := + fun hdev => ⟨ht, ⟨hus, hz47, hsB, hsC, hsD⟩, ⟨hthp, hr64, hr0, y, r, gs, g, hyS, hcy⟩, + ⟨hw1.1, hw1.2, hwin⟩, hemp, hR', hfiv, hsb, hzle, hmem, hdev, hvt⟩ + have hsucc : (dev = true → Fl4SpecA64.isFrameType t = true ∨ t = .untyped) → + (do + let aligned ← untyped.align_up (core.num.U64.wrapping_add p fiv) (zA nt.val us) + let s2 ← sched.set_thread_state st s (L.curW s) sched.TS_RESTART + untyped.retype st ar s2 a (cslotAddrA sl) R aligned nt us ptr off win dev) ⦃ res => + UPostA σ (pure (Fl4SpecA64.UntypedInvocation.retype sl R (specPtr p) + (Fl4SpecA64.alignUp (specPtr p + fiv.bv) (zOfA nt.val us.val)) t us.val + (windowSlotsA ptr r.val off.val win.val) dev)) s a Q res ⦄ := by + intro hdev + -- what the decode has checked, in A64 P1's form + have hU : UtArgs st s ex (cslotAddrA sl) p sb fi dev R + ⟨Fl4SpecA64.alignUp (p.bv + fiv.bv) (zOfA nt.val us.val)⟩ nt us ptr off win := by + have hxm : exAtA ex 0 ∈ ex.val := by + have hl : ex.val.length = 3 := by simp + unfold exAtA + rw [List.getD_eq_getElem _ _ (by omega)] + exact List.getElem_mem _ + refine ⟨exAtA ex 0, hxm, hx0n, idx, dep, zA nt us, ?_, by omega, ?_, ?_, ?_, ?_, ?_, ?_⟩ + · rw [retype_size_casesA, ite_eq_right h15, ite_eq_right hsA, ite_eq_right hsB, ite_eq_right hsC, + ite_eq_right hsD] + · rw [hrd, hcy] + simp only [tailOfA, destResA, ite_eq_right e1, ite_eq_right e2, ite_eq_right e3] + · rw [ensure_empty_slots_eqA s ptr off _ (show off.val ≤ (core.num.U64.wrapping_add off win).val by omega), + hsum, ite_eq_left hemp] + · have hnc' : cdt.ensure_no_children st s (cslotAddrA sl) = + ok (if ∃ x, σ.cdt x = some sl then .Err (.Sys .RevokeFirst) else .Ok ()) := hnc + rw [hnc', hR'] + by_cases hch : ∃ x, σ.cdt x = some sl + · rw [ite_eq_left hch] + refine ⟨fun h => absurd hch (of_decide_eq_true h), fun ⟨v, hv⟩ => ?_⟩ + cases okinj hv + · exact ⟨fun _ => ⟨(), by rw [ite_eq_right hch]⟩, fun _ => decide_eq_true hch⟩ + · rw [← hfiv]; exact hal + · refine ⟨i2, i4, ?_, ?_, ?_⟩ + · exact ei2 + · rw [← hfiv]; exact ei4 + · exact Nat.le_of_not_lt (show ¬ i4.val < win.val from hm) + · intro hd + rcases hdev hd with h | h + · exact Or.inl (by rw [is_frame_type_eqA, decide_eq_true (hfr.1 h)]) + · exact Or.inr (UScalar.eq_of_val_eq (by rw [hut.1 h]; simp [fl4_abi.consts.object.Untyped])) + rw [hal, bind_ok, hspec_al] + exact WP.spec_mono (hQ nt us ptr off win t r.val R fiv rfl (hctx hdev) hU) + (fun res h => Or.inr ⟨_, rfl, h⟩) + cases dev + · rw [ite_eq_right (by simp)] + refine UPostA.congr ?_ (hsucc (by simp)) + simp only [Bool.false_eq_true, false_and, ↓reduceIte, ndE_pure_bindA] + rw [ite_eq_left rfl, is_frame_type_eqA, bind_ok] + by_cases hfrm : nt.val = 7 ∨ nt.val = 9 ∨ nt.val = 10 + · rw [ite_eq_left (by simpa using hfrm)] + refine UPostA.congr ?_ (hsucc (fun _ => Or.inl (hfr.2 hfrm))) + simp only [hfr.2 hfrm, not_true_eq_false, false_and, and_false, ↓reduceIte, ndE_pure_bindA] + rw [ite_eq_right (by simpa using hfrm)] + have hnf : ¬ Fl4SpecA64.isFrameType t = true := fun h => hfrm (hfr.1 h) + have hU0 : (fl4_abi.consts.object.Untyped).val = 0 := by simp [fl4_abi.consts.object.Untyped] + by_cases hu : nt.val = 0 + · have : (nt != fl4_abi.consts.object.Untyped) = false := by + simp only [bne_eq_false_iff_eq]; exact UScalar.eq_of_val_eq (by rw [hU0]; exact hu) + rw [this, ite_eq_right (by simp)] + refine UPostA.congr ?_ (hsucc (fun _ => Or.inr (hut.2 hu))) + simp only [hut.2 hu, ne_eq, not_true_eq_false, and_false, ↓reduceIte, ndE_pure_bindA] + have : (nt != fl4_abi.consts.object.Untyped) = true := by + simp only [bne_iff_ne, ne_eq] + intro h; exact hu (by rw [h]; exact hU0) + rw [this, ite_eq_left rfl] + simp only [error.invalid_arg, bind_ok] + refine UPostA.err (e' := .invalidArgument 1) ?_ (by simp [SysErrRelA]) + have : ¬ t = .untyped := fun h => hu (hut.1 h) + simp only [hnf, this, ne_eq, Bool.false_eq_true, not_false_eq_true, and_self, ↓reduceIte, + NonDetE.throwError, ndE_throw_bindA] + rfl + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtDropA.lean b/proofs/Fl4ProofsA64/P3/UtDropA.lean new file mode 100644 index 000000000..0bee00180 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtDropA.lean @@ -0,0 +1,1013 @@ +/- +P3 (aarch64, A64-P3 Untyped `UtDropA`; Architect 23:14Z, blocker 2): **dropping the dead objects of a region from the +unified record**. Spec `deleteObjects` removes every `kheap` object of the region; the drop step moves the record from +`(Z, H, σ)` to `(dropZA D Z, dropHA D H, detype D σ)` at the same FL4 state, `D` being the spec addresses removed. + +This module has one lemma per record field or field group, for the fields that do not depend on the VSpace relation's +shape. **Not here, and no top lemma yet**: `VsRelA`, `ExtVsRelA`, the `Z.S1` / `Z.X` / `Z.Y` / `Z.Pp` membership +clauses (`pml`, `Stage2CoreA`, `HighVSpaceCoreA`, `pp`, `s1`, `ptH`, `tabInj`, `poolOff`, `vsVc`, `SlotApA.vs` / +`.ext`, `SetsInHA`, `DeadExtA.asid`, `UnmPtSpecA`, `tabS1`, `tabExt`): they follow the `VsRelA` freeze. + +Each lemma takes only the closure facts it needs, "no retained object refers into `D`"; their producers are the +childless untyped and A64 P1's invariants. `utDrop_deadDropA` gives the former `DeadDrop4KA` statement on the dropped +record. `P3/UtClosA` produces the closure facts from A64 P1's `DropOK`. `utDrop_syslA` is P1's `SysL.dropR`; +`utDrop_treeA` the CDT tree; `utDrop_deviceA` the device case. New definitions and lemmas only. +-/ +import Fl4ProofsA64.P3.Stage3A +import Fl4ProofsA64.P1.MdbDropR +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## The dropped record -/ + +/-- The retained members of a set of FL4 addresses. -/ +def dropSetA (D : Set Fl4SpecA64.ObjRef) (X : Set U64) : Set U64 := {p | p ∈ X ∧ specPtr p ∉ D} + +def dropThA (D : Set Fl4SpecA64.ObjRef) (th : TypedHeapA) : TypedHeapA := + fun p => if specPtr p ∈ D then none else th p + +def dropHA (D : Set Fl4SpecA64.ObjRef) (H : Heap) : Heap := fun p => if specPtr p ∈ D then none else H p + +/-- The new TCBs' map of affinities loses the dropped threads. -/ +def dropMA (D : Set Fl4SpecA64.ObjRef) (m : Fl4SpecA64.McState) : Fl4SpecA64.McState := + { m with tcbAffinity := fun p => if p ∈ D then none else m.tcbAffinity p } + +def dropZA (D : Set Fl4SpecA64.ObjRef) (Z : XSetsA) : XSetsA := + { Z with th := dropThA D Z.th, T := dropSetA D Z.T, C := dropSetA D Z.C, V := dropSetA D Z.V, + X := dropSetA D Z.X, Y := dropSetA D Z.Y, Pp := dropSetA D Z.Pp, S1 := dropSetA D Z.S1, + T2 := dropSetA D Z.T2 } + +variable {D : Set Fl4SpecA64.ObjRef} + +theorem dropThA_someA {th : TypedHeapA} {p : U64} {k : ObjKindA} : + dropThA D th p = some k ↔ specPtr p ∉ D ∧ th p = some k := by + unfold dropThA + by_cases h : specPtr p ∈ D <;> simp [h] + +theorem dropHA_someA {H : Heap} {p : U64} {k : HKind} : dropHA D H p = some k ↔ specPtr p ∉ D ∧ H p = some k := by + unfold dropHA + by_cases h : specPtr p ∈ D <;> simp [h] + +theorem detype_keptA (σ : Fl4SpecA64.State Unit) {q : Fl4SpecA64.ObjRef} (h : q ∉ D) : + (Fl4SpecA64.detype D σ).kheap q = σ.kheap q := by + show (if q ∈ D then none else σ.kheap q) = σ.kheap q + rw [ite_eq_right h] + +theorem detype_someA {σ : Fl4SpecA64.State Unit} {q : Fl4SpecA64.ObjRef} {o} : + (Fl4SpecA64.detype D σ).kheap q = some o ↔ q ∉ D ∧ σ.kheap q = some o := by + show (if q ∈ D then none else σ.kheap q) = some o ↔ _ + by_cases h : q ∈ D <;> simp [h] + +theorem dropHA_tcbsA (H : Heap) : (dropHA D H).tcbs = dropSetA D H.tcbs := by + ext p + show dropHA D H p = some .tcb ↔ H p = some .tcb ∧ specPtr p ∉ D + rw [dropHA_someA, and_comm] + +theorem dropHA_scsA (H : Heap) : (dropHA D H).scs = dropSetA D H.scs := by + ext p + show (∃ z, dropHA D H p = some (.sc z)) ↔ (∃ z, H p = some (.sc z)) ∧ specPtr p ∉ D + simp only [dropHA_someA] + constructor + · rintro ⟨z, h, e⟩ + exact ⟨⟨z, e⟩, h⟩ + · rintro ⟨⟨z, e⟩, h⟩ + exact ⟨z, h, e⟩ + +theorem dropHA_scBitsA (H : Heap) {c : U64} (h : specPtr c ∉ D) : (dropHA D H).scBits c = H.scBits c := by + unfold Heap.scBits dropHA + rw [ite_eq_right h] + +theorem isSlot_dropA {th : TypedHeapA} {a : U64} (h : IsSlotA (dropThA D th) a) : IsSlotA th a := by + obtain ⟨p, ⟨r, hr, hi⟩ | ⟨ht, hi⟩⟩ := h + · exact ⟨p, Or.inl ⟨r, (dropThA_someA.1 hr).2, hi⟩⟩ + · exact ⟨p, Or.inr ⟨(dropThA_someA.1 ht).2, hi⟩⟩ + +theorem absSlot_dropA {th : TypedHeapA} {sl : Fl4SpecA64.CslotPtr} : + AbsSlotA (dropThA D th) sl ↔ AbsSlotA th sl ∧ sl.1 ∉ D := by + constructor + · rintro ⟨p, hp, h | ⟨h, hi⟩⟩ + · have := dropThA_someA.1 h + exact ⟨⟨p, hp, Or.inl this.2⟩, hp ▸ this.1⟩ + · have := dropThA_someA.1 h + exact ⟨⟨p, hp, Or.inr ⟨this.2, hi⟩⟩, hp ▸ this.1⟩ + · rintro ⟨⟨p, hp, h | ⟨h, hi⟩⟩, hk⟩ + · exact ⟨p, hp, Or.inl (dropThA_someA.2 ⟨hp ▸ hk, h⟩)⟩ + · exact ⟨p, hp, Or.inr ⟨dropThA_someA.2 ⟨hp ▸ hk, h⟩, hi⟩⟩ + +/-- A retained SC keeps its (possibly renamed) spec object; `hA`: the idle-SC anchor is not removed. -/ +theorem scRen_keptA {m : Fl4SpecA64.McState} {c : U64} (hA : Fl4SpecA64.idleScPtr ∉ D) (hc : specPtr c ∉ D) : + scRenA m c ∉ D := by + unfold scRenA + split + · exact hA + · exact hc + +theorem scName_keptA {m : Fl4SpecA64.McState} {c : U64} (hA : Fl4SpecA64.idleScPtr ∉ D) (hc : specPtr c ∉ D) : + scNameA m c ∉ D := by + unfold scNameA + split + · exact hA + · exact hc + +/-! ## Heap placement and the untyped fields (J1) -/ + +theorem jointObject_dropA {Z : XSetsA} {H : Heap} {p : U64} {bytes : Nat} + (h : JointObjectA (dropZA D Z) (dropHA D H) p bytes) : JointObjectA Z H p bytes := by + rcases h with ⟨hb, h | h | h | h | h⟩ | ⟨hb, h | h⟩ | ⟨k, hk, hb⟩ | ⟨k, hk, hb⟩ + · exact Or.inl ⟨hb, Or.inl h.1⟩ + · exact Or.inl ⟨hb, Or.inr (Or.inl h.1)⟩ + · exact Or.inl ⟨hb, Or.inr (Or.inr (Or.inl h.1))⟩ + · exact Or.inl ⟨hb, Or.inr (Or.inr (Or.inr (Or.inl h.1)))⟩ + · exact Or.inl ⟨hb, Or.inr (Or.inr (Or.inr (Or.inr h)))⟩ + · exact Or.inr (Or.inl ⟨hb, Or.inl h.1⟩) + · exact Or.inr (Or.inl ⟨hb, Or.inr h.1⟩) + · exact Or.inr (Or.inr (Or.inl ⟨k, (dropThA_someA.1 hk).2, hb⟩)) + · exact Or.inr (Or.inr (Or.inr ⟨k, (dropHA_someA.1 hk).2, hb⟩)) + +/-- Every retained joint object but the idle-SC anchor starts outside `D`. -/ +theorem jointObject_keptA {Z : XSetsA} {H : Heap} {p : U64} {bytes : Nat} + (h : JointObjectA (dropZA D Z) (dropHA D H) p bytes) : + specPtr p ∉ D ∨ (p = fl4Ptr Fl4SpecA64.idleScPtr ∧ bytes = 4096) := by + rcases h with ⟨hb, h | h | h | h | h⟩ | ⟨-, h | h⟩ | ⟨k, hk, -⟩ | ⟨k, hk, -⟩ + · exact Or.inl h.2 + · exact Or.inl h.2 + · exact Or.inl h.2 + · exact Or.inl h.2 + · exact Or.inr ⟨h, hb⟩ + · exact Or.inl h.2 + · exact Or.inl h.2 + · exact Or.inl (dropThA_someA.1 hk).1 + · exact Or.inl (dropHA_someA.1 hk).1 + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-- J1, `ObjAllocA`. -/ +theorem utDrop_objAllocA {Z : XSetsA} {H : Heap} {OS : Set U64} {s : S} (h : ObjAllocA (st := st) Z H OS s) : + ObjAllocA (st := st) (dropZA D Z) (dropHA D H) (dropSetA D OS) s := + fun p bytes hp u hu q b fi d hc => h p bytes (jointObject_dropA hp) u hu.1 q b fi d hc + +/-- J1, `IdleAnchorOffA`. -/ +theorem utDrop_idleAnchorA {OS : Set U64} {s : S} (h : IdleAnchorOffA (st := st) OS s) : + IdleAnchorOffA (st := st) (dropSetA D OS) s := fun u hu q b fi d hc => h u hu.1 q b fi d hc + +/-- **The former `DeadDrop4KA` [J], on the dropped record**: no retained joint object lies in `[frame, frame + fi)`. +`hD`: `D` holds the spec address of every FL4 address of the used prefix. `hidle`: the idle-SC anchor is outside it. +`hpos`: no joint object is empty. -/ +theorem utDrop_deadDropA {Z : XSetsA} {H : Heap} {frame fi : U64} + (hD : ∀ p : U64, frame.val ≤ p.val → p.val < frame.val + fi.val → specPtr p ∈ D) + (hidle : (fl4Ptr Fl4SpecA64.idleScPtr).val + 4096 ≤ frame.val ∨ + frame.val + fi.val ≤ (fl4Ptr Fl4SpecA64.idleScPtr).val) + (hpos : ∀ p bytes, JointObjectA Z H p bytes → 0 < bytes) : + DeadDrop4KA (dropZA D Z) (dropHA D H) frame fi := by + intro p bytes hp h1 h2 + have hb := hpos p bytes (jointObject_dropA hp) + rcases jointObject_keptA hp with h | ⟨rfl, rfl⟩ + · exact h (hD p h1 (by omega)) + · omega + +/-- `SpecFreeA` (J3). -/ +theorem utDrop_specFreeA {OS : Set U64} {s : S} {σ : Fl4SpecA64.State Unit} (h : SpecFreeA st OS s σ) : + SpecFreeA st (dropSetA D OS) s (Fl4SpecA64.detype D σ) := by + have hf : ∀ a, UtFreeA st (dropSetA D OS) s (fun _ => True) a → UtFreeA st OS s (fun _ => True) a := + fun a ⟨u, hu, rest⟩ => ⟨u, hu.1, rest⟩ + refine ⟨fun p hp => ?_, fun x y hxy p hp => h.cdt x y hxy p (hf _ hp), fun x hx p hp => h.orig x hx p (hf _ hp)⟩ + show (if specPtr p ∈ D then none else σ.kheap (specPtr p)) = none + rw [h.heap p (hf _ hp), ite_self] + +/-- `UtBlk39A` (J4). -/ +theorem utDrop_utBlkA {OS : Set U64} {s : S} (h : UtBlk39A st OS s) : UtBlk39A st (dropSetA D OS) s := + fun u hu q b fi d hc => h u hu.1 q b fi d hc + +/-- `SpecZeroFreeA` (J10): `detype` keeps user memory. -/ +theorem utDrop_specZeroA {OS : Set U64} {s : S} {σ : Fl4SpecA64.State Unit} (h : SpecZeroFreeA st OS s σ) : + SpecZeroFreeA st (dropSetA D OS) s (Fl4SpecA64.detype D σ) := + fun a ⟨u, hu, rest⟩ => h a ⟨u, hu.1, rest⟩ + +/-- `SlotApA` without its two VSpace clauses (`vs`, `ext`: after the freeze). -/ +theorem utDrop_slotApA {Z : XSetsA} {H : Heap} {OS : Set U64} {kb : U64} (h : SlotApA Z H OS kb) : + ScApart (dropHA D H).scs (dropHA D H).scBits (dropSetA D OS) ∧ IrqStApart (dropSetA D OS) kb ∧ + (∀ v ∈ (dropZA D Z).V, Aligned v) ∧ + (∀ v ∈ (dropZA D Z).V, ∀ b, (b = v ∨ b = wa v 24#u64) → ¬ InOS (dropSetA D OS) b ∧ + ¬ W7.TLow (dropHA D H).tcbs b) := by + refine ⟨?_, fun o h1 h2 y hy => h.irqSt o h1 h2 y hy.1, fun v hv => h.vcAl v hv.1, fun v hv b hb => ?_⟩ + · intro x hx c hc + rw [dropHA_scsA] at hc + rw [dropHA_scBitsA H hc.2] + exact h.sca x hx.1 c hc.1 + · obtain ⟨h1, h2⟩ := h.vc v hv.1 b hb + refine ⟨fun ⟨y, hy, r⟩ => h1 ⟨y, hy.1, r⟩, fun ⟨t, ht, r⟩ => h2 ⟨t, ?_, r⟩⟩ + rw [dropHA_tcbsA] at ht + exact ht.1 + +/-- `HeapKsOff`. -/ +theorem utDrop_koA {H : Heap} {kb : U64} (h : HeapKsOff H kb) : HeapKsOff (dropHA D H) kb := + ⟨fun p k hk => h.1 p k (dropHA_someA.1 hk).2, fun p z hz => h.2 p z (dropHA_someA.1 hz).2⟩ + +/-- `JEntA.tcbs` and `.scs`. -/ +theorem utDrop_heapSetsA {Z : XSetsA} {H : Heap} (ht : H.tcbs = Z.T) (hc : H.scs = Z.C) : + (dropHA D H).tcbs = (dropZA D Z).T ∧ (dropHA D H).scs = (dropZA D Z).C := by + rw [dropHA_tcbsA, dropHA_scsA, ht, hc] + exact ⟨rfl, rfl⟩ + +/-! ## The state, CDT and scheduler relations -/ + +/-- `StateRelA`: no closure fact is needed. -/ +theorem utDrop_stateRelA {th : TypedHeapA} {mem : U64 → U64} {σ : Fl4SpecA64.State Unit} (h : StateRelA th mem σ) : + StateRelA (dropThA D th) mem (Fl4SpecA64.detype D σ) := by + constructor + · intro p r hp + obtain ⟨hk, hp⟩ := dropThA_someA.1 hp + rw [detype_keptA σ hk] + exact h.cnode p r hp + · intro t ht + obtain ⟨hk, ht⟩ := dropThA_someA.1 ht + rw [detype_keptA σ hk] + exact h.tcb t ht + +/-- `CdtRelA`. `hT` **(A64 P1 dependency)**: the MDB tree invariant over the retained slots. Closure: the removed +objects' slots have no CDT edge in the spec (`hcdt`) and none in FL4's tree (`hpar`). -/ +theorem utDrop_cdtRelA {th : TypedHeapA} {s : S} {σ : Fl4SpecA64.State Unit} {G : MdbGhostA} + (h : CdtRelA (L := L) _ th s σ G) + (hT : Fl4ProofsA64.P1.W7.R.ST.TreeA st {a | IsSlotA (dropThA D th) a} G.chains G.par s) + (hcdt : ∀ sl q, σ.cdt sl = some q → sl.1 ∉ D ∧ q.1 ∉ D) + (hpar : ∀ a b, G.par a = some b → ∃ sl, AbsSlotA th sl ∧ cslotAddrA sl = a ∧ sl.1 ∉ D) : + CdtRelA (L := L) _ (dropThA D th) s (Fl4SpecA64.detype D σ) G where + tree := hT + cdt := fun sl hsl => h.cdt sl (absSlot_dropA.1 hsl).1 + cdt_slot := fun sl q hq => by + have h1 := h.cdt_slot sl q hq + have h2 := hcdt sl q hq + exact ⟨absSlot_dropA.2 ⟨h1.1, h2.1⟩, absSlot_dropA.2 ⟨h1.2, h2.2⟩⟩ + orig := fun sl hsl => h.orig sl (absSlot_dropA.1 hsl).1 + par_slot := fun a b hab => by + obtain ⟨sl, h1, h2, h3⟩ := hpar a b hab + exact ⟨sl, absSlot_dropA.2 ⟨h1, h3⟩, h2⟩ + prev := h.prev + +/-- `SchedRelA`. Closure: the idle-SC anchor is retained (`hA`). -/ +theorem utDrop_schedRelA {T C Cs : Set U64} {curOf curScOf : Fl4SpecA64.CoreId → Fl4SpecA64.ObjRef} {s : S} + {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (h : SchedRelA (L := L) T C Cs curOf curScOf s σ m) (hA : Fl4SpecA64.idleScPtr ∉ D) : + SchedRelA (L := L) (dropSetA D T) (dropSetA D C) Cs curOf curScOf s (Fl4SpecA64.detype D σ) (dropMA D m) where + core := h.core + cpuCs := h.cpuCs + curOfW := h.curOfW + curScOfW := h.curScOfW + cur := h.cur + csc := h.csc + idle := h.idle + idleOf := h.idleOf + idleScOf := h.idleScOf + act := h.act + time := h.time + aff := fun t ht => by + show (if specPtr t ∈ D then none else m.tcbAffinity (specPtr t)) = _ + rw [ite_eq_right ht.2] + exact h.aff t ht.1 + affT := fun p c hp => by + change (if p ∈ D then none else m.tcbAffinity p) = some c at hp + by_cases hk : p ∈ D + · rw [ite_eq_left hk] at hp + cases hp + rw [ite_eq_right hk] at hp + obtain ⟨t, ht, e⟩ := h.affT p c hp + exact ⟨t, ⟨ht, e ▸ hk⟩, e⟩ + tcb := fun t ht => by + rw [detype_keptA σ ht.2] + exact h.tcb t ht.1 + sc := fun c hc => by + show ∃ y n, (Fl4SpecA64.detype D σ).kheap (scRenA m c) = some (.schedContext y n) ∧ _ + rw [detype_keptA σ (scRen_keptA hA hc.2)] + exact h.sc c hc.1 + scOff := fun c hc => h.scOff c hc.1 + +/-- `PlaceA`. Closure: the interrupt CNodes are retained (`hirq`). -/ +theorem utDrop_placeA {th : TypedHeapA} {ksb : U64} (h : PlaceA th ksb) + (hirq : ∀ i : U64, i.val < 5376 → specPtr (Fl4ProofsA64.P1.irqSlotA ksb i) ∉ D) : + PlaceA (dropThA D th) ksb where + align := fun p k hp => h.align p k (dropThA_someA.1 hp).2 + fit := fun p k hp => h.fit p k (dropThA_someA.1 hp).2 + apart := fun p q kp kq hp hq => h.apart p q kp kq (dropThA_someA.1 hp).2 (dropThA_someA.1 hq).2 + ks := fun p k hp => h.ks p k (dropThA_someA.1 hp).2 + irq := fun i hi => dropThA_someA.2 ⟨hirq i hi, h.irq i hi⟩ + nz := fun p k hp => h.nz p k (dropThA_someA.1 hp).2 + +/-- `IrqRelA`: `detype` keeps the interrupt state. -/ +theorem utDrop_irqRelA {mem : U64 → U64} {ksb : U64} {σ : Fl4SpecA64.State Unit} (h : IrqRelA mem ksb σ) : + IrqRelA mem ksb (Fl4SpecA64.detype D σ) := ⟨h.st, h.node, h.nodeHi⟩ + +/-- `SchedRestA`. Closure: the idle-SC anchor (`hA`) and a retained TCB's SC (`hsc`) are retained. -/ +theorem utDrop_schedRestA {Z : XSetsA} {s : S} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (h : SchedRestA (L := L) Z s σ m) (hA : Fl4SpecA64.idleScPtr ∉ D) + (hsc : ∀ t ∈ Z.T, specPtr t ∉ D → L.mem s (scA t) ≠ 0#u64 → specPtr (L.mem s (scA t)) ∉ D) : + SchedRestA (L := L) (dropZA D Z) s (Fl4SpecA64.detype D σ) (dropMA D m) where + rq := h.rq + rqDom := h.rqDom + rel := h.rel + consumed := h.consumed + repr := h.repr + curDom := h.curDom + domTime := h.domTime + prio := fun t ht => by + rw [detype_keptA σ ht.2] + exact h.prio t ht.1 + scMem := fun t ht h0 => ⟨h.scMem t ht.1 h0, hsc t ht.1 ht.2 h0⟩ + refillMax := fun c hc => by + show ∃ y n, (Fl4SpecA64.detype D σ).kheap (scRenA m c) = some (.schedContext y n) ∧ _ + rw [detype_keptA σ (scRen_keptA hA hc.2)] + exact h.refillMax c hc.1 + +/-! ## Cap closure and the VCPU record -/ + +/-- **No retained slot names a removed object** (the closure fact childlessness and A64 P1's MDB invariants give). -/ +def UtNoCapA (D : Set Fl4SpecA64.ObjRef) (th : TypedHeapA) (mem : U64 → U64) : Prop := + ∀ y, IsSlotA (dropThA D th) y → match capAtA mem y with + | .CNode p _ _ _ => specPtr p ∉ D + | .Thread p => specPtr p ∉ D + | .Vcpu p => specPtr p ∉ D + | _ => True + +/-- `JAtZA.cv`. -/ +theorem utDrop_capValidA {th : TypedHeapA} {mem : U64 → U64} + (h : ∀ y, IsSlotA th y → CapValidA th (capAtA mem y)) (hn : UtNoCapA D th mem) : + ∀ y, IsSlotA (dropThA D th) y → CapValidA (dropThA D th) (capAtA mem y) := by + intro y hy + have h1 := h y (isSlot_dropA hy) + have h2 := hn y hy + revert h1 h2 + cases capAtA mem y <;> intro h1 h2 <;> first + | exact h1 + | exact ⟨dropThA_someA.2 ⟨h2, h1.1⟩, h1.2⟩ + | exact dropThA_someA.2 ⟨h2, h1⟩ + +/-- `JAtZA.thT`, `.scOffD`. -/ +theorem utDrop_thTA {Z : XSetsA} {m : Fl4SpecA64.McState} (ht : ∀ t, Z.th t = some .tcb ↔ t ∈ Z.T) + (hs : ∀ p, InDomA Z p → specPtr p ≠ m.idleScOf m.curCore → specPtr p ≠ Fl4SpecA64.idleScPtr) : + (∀ t, (dropZA D Z).th t = some .tcb ↔ t ∈ (dropZA D Z).T) ∧ + (∀ p, InDomA (dropZA D Z) p → specPtr p ≠ (dropMA D m).idleScOf (dropMA D m).curCore → + specPtr p ≠ Fl4SpecA64.idleScPtr) := by + refine ⟨fun t => ?_, fun p hp => hs p ?_⟩ + · show dropThA D Z.th t = some .tcb ↔ t ∈ Z.T ∧ specPtr t ∉ D + rw [dropThA_someA, ht t, and_comm] + · rcases hp with h | h | h | h + · refine Or.inl fun e => h ?_ + show dropThA D Z.th p = none + unfold dropThA + rw [e, ite_self] + · exact Or.inr (Or.inl h.1) + · exact Or.inr (Or.inr (Or.inl h.1)) + · exact Or.inr (Or.inr (Or.inr h.1)) + +/-- `JAtZA.idleT` / `.idleC` / `.curT` / `.cscC`. Closure: no node names a removed idle or current object. -/ +theorem utDrop_nodeRefsA {Z : XSetsA} {s : S} (off : U64) {X : Set U64} + (h : ∀ c ∈ Z.Cs, L.mem s (L.nodeAt s c off) ∈ X) + (hk : ∀ c ∈ Z.Cs, specPtr (L.mem s (L.nodeAt s c off)) ∉ D) : + ∀ c ∈ (dropZA D Z).Cs, L.mem s (L.nodeAt s c off) ∈ dropSetA D X := fun c hc => ⟨h c hc, hk c hc⟩ + +/-- `VcpuCoreA`. -/ +theorem utDrop_vcpuCoreA {Z : XSetsA} {s : S} {σ : Fl4SpecA64.State Unit} (h : VcpuCoreA (L := L) Z s σ) + (hn : UtNoCapA D Z.th (L.mem s)) : VcpuCoreA (L := L) (dropZA D Z) s (Fl4SpecA64.detype D σ) where + obj := fun v hv => by + rw [detype_keptA σ hv.2] + exact h.obj v hv.1 + back := fun p x hp => by + obtain ⟨hk, hp⟩ := detype_someA.1 hp + obtain ⟨v, hv, e⟩ := h.back p x hp + exact ⟨v, ⟨hv, e ▸ hk⟩, e⟩ + tcb := fun t ht => by + rw [detype_keptA σ ht.2] + exact h.tcb t ht.1 + cap := fun y hy p hc => by + have h2 := hn y hy + rw [hc] at h2 + exact ⟨h.cap y (isSlot_dropA hy) p hc, h2⟩ + +/-! ## The IPC / MCS fields that read one retained object at a time -/ + +/-- `ScCovA`. Closure: the idle-SC renaming does not change (`dropMA` keeps the idle SCs). -/ +theorem utDrop_scCovA {C : Set U64} {m : Fl4SpecA64.McState} {σ : Fl4SpecA64.State Unit} (h : ScCovA C m σ) + (hren : ∀ c ∈ C, scNameA m c ∉ D → specPtr c ∉ D) : + ScCovA (dropSetA D C) (dropMA D m) (Fl4SpecA64.detype D σ) := by + intro p x n hp + obtain ⟨hk, hp⟩ := detype_someA.1 hp + obtain ⟨c, hc, e⟩ := h p x n hp + exact ⟨c, ⟨hc, hren c hc (e ▸ hk)⟩, e⟩ + +/-- `JStage3A.scSize`. -/ +theorem utDrop_scSizeA {H : Heap} {C : Set U64} {m : Fl4SpecA64.McState} {σ : Fl4SpecA64.State Unit} + (h : ∀ c ∈ C, ∀ x n, σ.kheap (scNameA m c) = some (.schedContext x n) → + n + Fl4SpecA64.minSchedContextBits ≤ H.scBits c ∧ + (NotIdleScA m (scNameA m c) → n + Fl4SpecA64.minSchedContextBits = H.scBits c)) : + ∀ c ∈ dropSetA D C, ∀ x n, (Fl4SpecA64.detype D σ).kheap (scNameA (dropMA D m) c) = some (.schedContext x n) → + n + Fl4SpecA64.minSchedContextBits ≤ (dropHA D H).scBits c ∧ + (NotIdleScA (dropMA D m) (scNameA (dropMA D m) c) → + n + Fl4SpecA64.minSchedContextBits = (dropHA D H).scBits c) := fun c hc x n hk => by + rw [dropHA_scBitsA H hc.2] + exact h c hc.1 x n (detype_someA.1 hk).2 + +/-- `McpRelA`. -/ +theorem utDrop_mcpA {T : Set U64} {m : Fl4SpecA64.McState} {mem : U64 → U64} {σ : Fl4SpecA64.State Unit} (h : McpRelA T m mem σ) : + McpRelA (dropSetA D T) (dropMA D m) mem (Fl4SpecA64.detype D σ) := fun t ht => by + obtain ⟨x, hx, e, hf⟩ := h t ht.1 + exact ⟨x, by rw [detype_keptA σ ht.2]; exact hx, e, hf⟩ + +/-- `BufNativeA`. -/ +theorem utDrop_bufNativeA {T : Set U64} {mem : U64 → U64} (h : BufNativeA T mem) : BufNativeA (dropSetA D T) mem := + fun t ht => h t ht.1 + +/-- `IdleNoFaultA`. Closure: the idle threads are retained. -/ +theorem utDrop_idleNoFaultA {m : Fl4SpecA64.McState} {σ : Fl4SpecA64.State Unit} (h : IdleNoFaultA m σ) + (hk : ∀ c < Fl4SpecA64.fl4A64Platform.numNodes, m.idleThreadOf c ∉ D) : + IdleNoFaultA (dropMA D m) (Fl4SpecA64.detype D σ) := fun c hc => by + show ∃ x, (Fl4SpecA64.detype D σ).kheap (m.idleThreadOf c) = some (.tcb x) ∧ x.tcbFault = none + rw [detype_keptA σ (hk c hc)] + exact h c hc + +/-- `DeadExtA`'s dead-TCB clause (closure item E). -/ +theorem utDrop_deadTVcpuA {Z : XSetsA} {OS : Set U64} {s : S} + (h : ∀ t ∈ Z.T, DeadA st OS .tcb s t → L.mem s (wa t 3664#u64) = 0#u64) + (hdead : ∀ k p, specPtr p ∉ D → DeadA st (dropSetA D OS) k s p → DeadA st OS k s p) : + ∀ t ∈ (dropZA D Z).T, DeadA st (dropSetA D OS) .tcb s t → L.mem s (wa t 3664#u64) = 0#u64 := + fun t ht hd => h t ht.1 (hdead _ t ht.2 hd) + +/-- `JStage3A.thPos` (closure item D). -/ +theorem utDrop_thPosA {th : TypedHeapA} (h : ∀ p z, th p = some (.other z) → 0 < z) : + ∀ p z, dropThA D th p = some (.other z) → 0 < z := fun p z hp => h p z (dropThA_someA.1 hp).2 + +/-- `DeadExtA`'s VCPU and reply-stack clauses (the ASID clause: after the freeze). `hdead`: a retained object dead +over the retained slots was dead before (no removed slot held its only cap). -/ +theorem utDrop_deadExtA {Z : XSetsA} {OS Rp : Set U64} {stack : U64 → List U64} {s : S} + {σ : Fl4SpecA64.State Unit} (h : DeadExtA st Z OS Rp stack s σ) + (hdead : ∀ k p, specPtr p ∉ D → DeadA st (dropSetA D OS) k s p → DeadA st OS k s p) : + (∀ v ∈ (dropZA D Z).V, DeadA st (dropSetA D OS) .vcpu s v → ∀ x, + (Fl4SpecA64.detype D σ).kheap (specPtr v) = some (.archObj (.vcpu x)) → x.vcpuTcb = none) ∧ + (∀ r ∈ dropSetA D Rp, DeadA st (dropSetA D OS) .reply s r → ∀ c ∈ (dropZA D Z).C, r ∉ stack c) ∧ + (∀ r ∈ dropSetA D Rp, DeadA st (dropSetA D OS) .reply s r → ∀ x, + (Fl4SpecA64.detype D σ).kheap (specPtr r) = some (.reply x) → x.replyTcb = none) := + ⟨fun v hv hd x hx => h.vcpu v hv.1 (hdead _ v hv.2 hd) x (detype_someA.1 hx).2, + fun r hr hd c hc => h.reply r hr.1 (hdead _ r hr.2 hd) c hc.1, + fun r hr hd x hx => h.replyTcb r hr.1 (hdead _ r hr.2 hd) x (detype_someA.1 hx).2⟩ + +/-! ## The scheduler -/ + +/-- **The spec's own references of retained objects are retained** (closure at `σ`): a retained TCB's SC, and the +members of the release queue and ready queues. -/ +structure UtSpecClosedA (D : Set Fl4SpecA64.ObjRef) (σ : Fl4SpecA64.State Unit) : Prop where + tcbSc : ∀ u x p, u ∉ D → σ.kheap u = some (.tcb x) → x.tcbSchedContext = some p → p ∉ D + rls : ∀ u ∈ σ.releaseQueue, u ∉ D + rq : ∀ d p u, u ∈ σ.readyQueues d p → u ∉ D + +/-- `ScStateRelA`. -/ +theorem utDrop_scStateA {C : Set U64} {m : Fl4SpecA64.McState} {s : S} {σ : Fl4SpecA64.State Unit} + (h : ScStateRelA st C m s σ) (hA : Fl4SpecA64.idleScPtr ∉ D) : + ScStateRelA st (dropSetA D C) (dropMA D m) s (Fl4SpecA64.detype D σ) := by + refine ⟨fun c hc => ?_⟩ + show ∃ x n, (Fl4SpecA64.detype D σ).kheap (scNameA m c) = some (.schedContext x n) ∧ _ + rw [detype_keptA σ (scName_keptA hA hc.2)] + exact h.sc c hc.1 + +/-- `ScPlacedA`. -/ +theorem utDrop_scPlacedA {th : TypedHeapA} {ksb : U64} {C : Set U64} {m : Fl4SpecA64.McState} + {σ : Fl4SpecA64.State Unit} (h : ScPlacedA th ksb C m σ) : + ScPlacedA (dropThA D th) ksb (dropSetA D C) (dropMA D m) (Fl4SpecA64.detype D σ) where + inj := fun c hc c' hc' e => h.inj c hc.1 c' hc'.1 e + disj := fun c hc c' hc' hne x n x' n' hx hx' a ha => + h.disj c hc.1 c' hc'.1 hne x n x' n' (detype_someA.1 hx).2 (detype_someA.1 hx').2 a ha + out := fun c hc x n hx a ha => + have h1 := h.out c hc.1 x n (detype_someA.1 hx).2 a ha + ⟨fun p k hp => h1.1 p k (dropThA_someA.1 hp).2, h1.2⟩ + +/-- `ScCovA`'s producer premise from `ScPlacedA`'s naming: an SC whose name is retained is retained, when the +idle SC's own address is. -/ +theorem utDrop_scRenKeptA {C : Set U64} {m : Fl4SpecA64.McState} + (h : ∀ c ∈ C, specPtr c = m.idleScOf m.curCore → specPtr c ∉ D) : + ∀ c ∈ C, scNameA m c ∉ D → specPtr c ∉ D := by + intro c hc hn + unfold scNameA at hn + split at hn + · rename_i e + exact h c hc e + · exact hn + +/-- `RunQA`. -/ +theorem utDrop_runQA {σ : Fl4SpecA64.State Unit} (h : RunQA σ) (hc : UtSpecClosedA D σ) : + RunQA (Fl4SpecA64.detype D σ) := by + refine ⟨fun d p t ht => ?_, fun t ht => ?_⟩ + · rw [detype_keptA σ (hc.rq d p t ht)] + exact h.1 d p t ht + · rw [detype_keptA σ (hc.rls t ht)] + exact h.2 t ht + +/-- `RdA`. -/ +theorem utDrop_rdA {σ : Fl4SpecA64.State Unit} (h : RdA σ) : RdA (Fl4SpecA64.detype D σ) := by + intro u x hu hcl y scp z n hx hsc hy hact hnr + exact h u x (detype_someA.1 hu).2 hcl y scp z n (detype_someA.1 hx).2 hsc (detype_someA.1 hy).2 hact hnr + +/-- `ScInvA`. `hpos`: an active SC has a refill (`SInvXA.rpos`). -/ +theorem utDrop_scInvA {m : Fl4SpecA64.McState} {σ : Fl4SpecA64.State Unit} (h : ScInvA m σ) + (hc : UtSpecClosedA D σ) + (hpos : ∀ p y k, σ.kheap p = some (.schedContext y k) → Fl4SpecA64.scActive y = true → y.scRefills ≠ []) : + ScInvA (dropMA D m) (Fl4SpecA64.detype D σ) := by + have hrel : ∀ u ∈ σ.releaseQueue, ∃ x p y n, σ.kheap u = some (.tcb x) ∧ x.tcbSchedContext = some p ∧ + σ.kheap p = some (.schedContext y n) ∧ Fl4SpecA64.scActive y = true ∧ u ∉ D ∧ p ∉ D := by + intro u hu + obtain ⟨x, p, y, n, h1, h2, h3, h4⟩ := h.relSc u hu + exact ⟨x, p, y, n, h1, h2, h3, h4, hc.rls u hu, hc.tcbSc u x p (hc.rls u hu) h1 h2⟩ + have hrt : ∀ u ∈ σ.releaseQueue, specRtA (Fl4SpecA64.detype D σ) u = specRtA σ u := by + intro u hu + obtain ⟨x, p, y, n, h1, h2, h3, h4, hu', hp'⟩ := hrel u hu + have hne := hpos p y n h3 h4 + rw [specRtA_of h1 h2 h3 hne, + specRtA_of (σ := Fl4SpecA64.detype D σ) (by rw [detype_keptA σ hu']; exact h1) h2 + (by rw [detype_keptA σ hp']; exact h3) hne] + exact + { refills := fun p y n hp => h.refills p y n (detype_someA.1 hp).2 + rr := fun p y n hp => h.rr p y n (detype_someA.1 hp).2 + ntfnIdle := fun q x hq => h.ntfnIdle q x (detype_someA.1 hq).2 + ntfnIdleOf := fun q x hq => h.ntfnIdleOf q x (detype_someA.1 hq).2 + relSc := fun u hu => by + obtain ⟨x, p, y, n, h1, h2, h3, h4, hu', hp'⟩ := hrel u hu + exact ⟨x, p, y, n, by rw [detype_keptA σ hu']; exact h1, h2, by rw [detype_keptA σ hp']; exact h3, h4⟩ + sorted := h.sorted.imp_of_mem fun {a b} ha hb hab => by + rw [hrt a ha, hrt b hb] + exact hab + back := fun u x p hu hsc => by + obtain ⟨hk, hu⟩ := detype_someA.1 hu + rw [detype_keptA σ (hc.tcbSc u x p hk hu hsc)] + exact h.back u x p hu hsc + tcbScNotIdle := fun u x p hu hsc => h.tcbScNotIdle u x p (detype_someA.1 hu).2 hsc } + +/-- `QueueRel` over a smaller set that still holds the queue's members. -/ +theorem utDrop_queueRelA {mem : U64 → U64} {T : Set U64} {l : tcbqueue.Link} {q : tcbqueue.Queue} {xs : List U64} + (h : QueueRel mem T l q xs) (hk : ∀ t ∈ xs, specPtr t ∉ D) : QueueRel mem (dropSetA D T) l q xs := + ⟨h.nodup, fun t ht => ⟨h.mem t ht, hk t ht⟩, h.head, h.last, h.seg⟩ + +/-- `SchedCoreA`. **(A64 P1 dependency, `DeadQ`)** `hQ` / `hR`: no removed TCB is in a ready or release queue. -/ +theorem utDrop_schedCoreA {T Cs : Set U64} {s : S} {Q : U64 → U64 → List U64} {R : U64 → List U64} + (h : SchedCoreA st T Cs s Q R) + (hQ : ∀ c ∈ Cs, ∀ p : U64, p.val < 256 → ∀ t ∈ Q c p, specPtr t ∉ D) + (hR : ∀ c ∈ Cs, ∀ t ∈ R c, specPtr t ∉ D) : SchedCoreA st (dropSetA D T) Cs s Q R where + tcbs := fun t ht => h.tcbs t ht.1 + nodes := fun c hc => + have n := h.nodes c hc + ⟨n.align, n.fits, fun t ht => n.disj t ht.1⟩ + aff := fun t ht => h.aff t ht.1 + prio := fun t ht => h.prio t ht.1 + ready := fun c hc => + have r := h.ready c hc + ⟨⟨fun p hp => utDrop_queueRelA (r.1.rel p hp) (hQ c hc p hp), r.1.disj⟩, r.2⟩ + release := fun c hc => utDrop_queueRelA (h.release c hc) (hR c hc) + inQ := h.inQ + inR := h.inR + queued := fun t ht => h.queued t ht.1 + released := fun t ht => h.released t ht.1 + excl := fun t ht => h.excl t ht.1 + +/-- `SchedRelNoStA`. -/ +theorem utDrop_schedNoStA {T C Cs : Set U64} {curOf curScOf : Fl4SpecA64.CoreId → Fl4SpecA64.ObjRef} {s : S} + {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (h : SchedRelNoStA (L := L) T C Cs curOf curScOf s σ m) (hA : Fl4SpecA64.idleScPtr ∉ D) : + SchedRelNoStA (L := L) (dropSetA D T) (dropSetA D C) Cs curOf curScOf s (Fl4SpecA64.detype D σ) (dropMA D m) where + core := h.core + cpuCs := h.cpuCs + curOfW := h.curOfW + curScOfW := h.curScOfW + cur := h.cur + csc := h.csc + idle := h.idle + idleOf := h.idleOf + idleScOf := h.idleScOf + act := h.act + time := h.time + aff := fun t ht => by + show (if specPtr t ∈ D then none else m.tcbAffinity (specPtr t)) = _ + rw [ite_eq_right ht.2] + exact h.aff t ht.1 + affT := fun p c hp => by + change (if p ∈ D then none else m.tcbAffinity p) = some c at hp + by_cases hk : p ∈ D + · rw [ite_eq_left hk] at hp + cases hp + rw [ite_eq_right hk] at hp + obtain ⟨t, ht, e⟩ := h.affT p c hp + exact ⟨t, ⟨ht, e ▸ hk⟩, e⟩ + tcb := fun t ht => by + rw [detype_keptA σ ht.2] + exact h.tcb t ht.1 + sc := fun c hc => by + show ∃ y n, (Fl4SpecA64.detype D σ).kheap (scRenA m c) = some (.schedContext y n) ∧ _ + rw [detype_keptA σ (scRen_keptA hA hc.2)] + exact h.sc c hc.1 + scOff := fun c hc => h.scOff c hc.1 + +/-- `JStage3A.sr`. `hQ` / `hR` **(A64 P1 dependency, `DeadQ`)**; closure: the current thread, every core's current SC (item B) and a +pending switch target are retained. -/ +theorem utDrop_schedSRA {Z : XSetsA} {m : Fl4SpecA64.McState} {κ : U64 → U64} {s : S} {σ : Fl4SpecA64.State Unit} + (h : SchedSRA st Z m κ s σ) (hA : Fl4SpecA64.idleScPtr ∉ D) (hc : UtSpecClosedA D σ) + (hsc : ∀ t ∈ Z.T, specPtr t ∉ D → L.mem s (scA t) ≠ 0#u64 → specPtr (L.mem s (scA t)) ∉ D) + (hirq : ∀ i : U64, i.val < 5376 → specPtr (Fl4ProofsA64.P1.irqSlotA (L.ksb s) i) ∉ D) + (hQ : ∀ c ∈ Z.Cs, ∀ p : U64, p.val < 256 → ∀ t ∈ Z.Q c p, specPtr t ∉ D) + (hR : ∀ c ∈ Z.Cs, ∀ t ∈ Z.R c, specPtr t ∉ D) + (hcur : specPtr (L.curW s) ∉ D) + (hcsc : ∀ c ∈ Z.Cs, specPtr (L.mem s (L.nodeAt s c layout.node.CUR_SC)) ∉ D) + (hact : ∀ u, σ.schedulerAction = .switchThread (specPtr u) → u ∈ Z.T → specPtr u ∉ D) + (hpos : ∀ p y k, σ.kheap p = some (.schedContext y k) → Fl4SpecA64.scActive y = true → y.scRefills ≠ []) : + SchedSRA st (dropZA D Z) (dropMA D m) κ s (Fl4SpecA64.detype D σ) := + { sch := utDrop_schedNoStA h.sch hA + rest := utDrop_schedRestA h.rest hA hsc + scr := utDrop_scStateA h.scr hA + scp := utDrop_scPlacedA h.scp + thT := fun t => by + show dropThA D Z.th t = some .tcb ↔ t ∈ Z.T ∧ specPtr t ∉ D + rw [dropThA_someA, h.thT t, and_comm] + place := utDrop_placeA h.place hirq + csN := h.csN + ksFit := h.ksFit + curT := ⟨h.curT, hcur⟩ + core := utDrop_schedCoreA h.core hQ hR + curOn := h.curOn + run := utDrop_runQA h.run hc + actOn := fun u hu => ⟨⟨(h.actOn u hu).1, hact u hu (h.actOn u hu).1⟩, (h.actOn u hu).2⟩ + cscC := ⟨h.cscC, hcsc _ h.sch.cpuCs⟩ + inv := utDrop_scInvA h.inv hc hpos + scCore := fun c hc' => h.scCore c hc'.1 + cscAll := fun c hc' => ⟨h.cscAll c hc', hcsc c hc'⟩ + rd := utDrop_rdA h.rd } + +/-! ## Endpoints, notifications, replies -/ + +/-- `EpStateRel`. Closure: a TCB that is not inactive (`hlive`, so every queue member) and a retained receiver's +reply (`hr`) are retained. `Bf'` is any subset of the buffers. -/ +theorem utDrop_epStateA {T E R : Set U64} {Bf Bf' : Set (U64 × Fl4SpecA64.Word)} {mem : U64 → U64} + {σ : Fl4SpecA64.State Unit} (h : EpStateRel T E R Bf mem σ) (hBf : Bf' ⊆ Bf) + (hlive : ∀ t ∈ T, (mem (stA t)).val ≠ 0 → specPtr t ∉ D) + (hr : ∀ t ∈ T, specPtr t ∉ D → (mem (stA t)).val = 3 → mem (tReplyA t) ≠ 0#u64 → + specPtr (mem (tReplyA t)) ∉ D) : + EpStateRel (dropSetA D T) (dropSetA D E) (dropSetA D R) Bf' mem (Fl4SpecA64.detype D σ) where + ep := fun e he => by + rw [detype_keptA σ he.2] + obtain ⟨x, hx, ho⟩ := h.ep e he.1 + refine ⟨x, hx, ?_⟩ + cases x with + | idleEP => exact ⟨ho.1, utDrop_queueRelA ho.2 (fun _ h => absurd h List.not_mem_nil)⟩ + | sendEP q => + obtain ⟨h1, xs, h2, h3, h4, h5⟩ := ho + exact ⟨h1, xs, h2, h3, utDrop_queueRelA h4 (fun y hy => hlive y (h4.mem y hy) (by rw [(h5 y hy).1]; decide)), h5⟩ + | recvEP q => + obtain ⟨h1, xs, h2, h3, h4, h5⟩ := ho + exact ⟨h1, xs, h2, h3, utDrop_queueRelA h4 (fun y hy => hlive y (h4.mem y hy) (by rw [(h5 y hy).1]; decide)), h5⟩ + reply := fun r hr => by + rw [detype_keptA σ hr.2] + exact h.reply r hr.1 + tcb := fun t ht => by + rw [detype_keptA σ ht.2] + obtain ⟨x, hx, ho⟩ := h.tcb t ht.1 + refine ⟨x, hx, { ho with recv := fun e r sd hs => ?_ }⟩ + obtain ⟨h1, h2⟩ := ho.recv e r sd hs + have h3 : (mem (stA t)).val = 3 := by rw [ho.st, hs]; rfl + refine ⟨h1, fun hn => ⟨h2 hn, hr t ht.1 ht.2 h3 (fun h0 => hn ?_)⟩⟩ + rw [← h1, optRef, ite_eq_left h0] + buf := fun bp hbp => h.buf bp (hBf hbp) + +/-- `NtfnStateRel`. Closure: a TCB that is not inactive (`hlive`, so every waiter) and a retained notification's +bound TCB (`hb`) are retained. -/ +theorem utDrop_ntfnStateA {N T : Set U64} {mem : U64 → U64} {σ : Fl4SpecA64.State Unit} + (h : NtfnStateRel N T mem σ) + (hlive : ∀ t ∈ T, (mem (stA t)).val ≠ 0 → specPtr t ∉ D) + (hb : ∀ n ∈ N, specPtr n ∉ D → mem (nBoundA n) ∈ T → specPtr (mem (nBoundA n)) ∉ D) : + NtfnStateRel (dropSetA D N) (dropSetA D T) mem (Fl4SpecA64.detype D σ) where + ntfn := fun n hn => by + rw [detype_keptA σ hn.2] + obtain ⟨x, hx, ho⟩ := h.ntfn n hn.1 + refine ⟨x, hx, { ho with obj := ?_, bound_mem := fun h0 => ⟨ho.bound_mem h0, hb n hn.1 hn.2 (ho.bound_mem h0)⟩ }⟩ + have o := ho.obj + revert o + cases x.ntfnObj with + | idleNtfn => exact fun o => ⟨o.1, utDrop_queueRelA o.2 (fun _ h => absurd h List.not_mem_nil)⟩ + | activeNtfn b => exact fun o => ⟨o.1, utDrop_queueRelA o.2.1 (fun _ h => absurd h List.not_mem_nil), o.2.2⟩ + | waitingNtfn q => + intro o + obtain ⟨h1, xs, h2, h3, h4, h5⟩ := o + exact ⟨h1, xs, h2, h3, utDrop_queueRelA h4 (fun y hy => hlive y (h4.mem y hy) (by rw [(h5 y hy).1]; decide)), h5⟩ + tcb := fun t ht => by + rw [detype_keptA σ ht.2] + exact h.tcb t ht.1 + +/-- `ReplyStackRelA`. Closure: an SC and the replies of its stack are retained or removed together (`hstk`), and a +retained reply's non-zero links are retained (`hlk`). -/ +theorem utDrop_replyStackA {mem : U64 → U64} {R C : Set U64} {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} {stack : U64 → List U64} + (h : ReplyStackRelA mem R C σ (scNameA m) stack) (hA : Fl4SpecA64.idleScPtr ∉ D) + (hstk : ∀ sc ∈ C, ∀ r ∈ stack sc, (specPtr sc ∉ D ↔ specPtr r ∉ D)) + (hlk : ∀ r ∈ R, specPtr r ∉ D → ∀ w, (w = mem (rNextA r) ∨ w = mem (rPrevA r)) → w ∈ R → specPtr w ∉ D) : + ReplyStackRelA mem (dropSetA D R) (dropSetA D C) (Fl4SpecA64.detype D σ) (scNameA (dropMA D m)) stack where + stackRel := fun sc hsc => by + obtain ⟨⟨x, n, hx, hr⟩, h2, h3, h4⟩ := h.stackRel sc hsc.1 + refine ⟨⟨x, n, ?_, hr⟩, fun r hr => ⟨h2 r hr, (hstk sc hsc.1 r hr).1 hsc.2⟩, h3, h4⟩ + show (Fl4SpecA64.detype D σ).kheap (scNameA m sc) = _ + rw [detype_keptA σ (scName_keptA hA hsc.2)] + exact hx + unique := fun sc hsc sc' hsc' => h.unique sc hsc.1 sc' hsc'.1 + owner := fun r hr x hx p => by + have h1 := h.owner r hr.1 x (detype_someA.1 hx).2 p + refine h1.trans ⟨fun ⟨sc, hsc, e, hd⟩ => ?_, fun ⟨sc, hsc, e, hd⟩ => ⟨sc, hsc.1, e, hd⟩⟩ + exact ⟨sc, ⟨hsc, (hstk sc hsc r (List.mem_of_mem_head? hd)).2 hr.2⟩, e, hd⟩ + detached := fun r hr hno => by + have hno' : ∀ sc ∈ C, r ∉ stack sc := fun sc hsc hm => + hno sc ⟨hsc, (hstk sc hsc r hm).2 hr.2⟩ hm + obtain ⟨h1, h2, h3⟩ := h.detached r hr.1 hno' + refine ⟨h1, fun h0 => ?_, fun h0 => ?_⟩ + · obtain ⟨a, b, c⟩ := h2 h0 + exact ⟨⟨a, hlk r hr.1 hr.2 _ (Or.inl rfl) a⟩, fun sc hsc => b sc hsc.1, c⟩ + · obtain ⟨a, b, c⟩ := h3 h0 + exact ⟨⟨a, hlk r hr.1 hr.2 _ (Or.inr rfl) a⟩, fun sc hsc => b sc hsc.1, c⟩ + +/-- `YieldRelA`. Closure: a retained TCB's yield target (`hto`) and a retained SC's yielder (`hfrom`) are +retained. -/ +theorem utDrop_yieldA {T C : Set U64} {m : Fl4SpecA64.McState} {mem : U64 → U64} {σ : Fl4SpecA64.State Unit} + (h : YieldRelA T C m mem σ) (hA : Fl4SpecA64.idleScPtr ∉ D) + (hto : ∀ t ∈ T, specPtr t ∉ D → mem (yieldA t) ∈ C → specPtr (mem (yieldA t)) ∉ D) + (hfrom : ∀ c ∈ C, specPtr c ∉ D → ∀ y n, σ.kheap (scNameA m c) = some (.schedContext y n) → + ∀ p, y.scYieldFrom = some p → p ∉ D) : + YieldRelA (dropSetA D T) (dropSetA D C) (dropMA D m) mem (Fl4SpecA64.detype D σ) where + tcb := fun t ht => by + rw [detype_keptA σ ht.2] + exact h.tcb t ht.1 + toSc := fun t ht h0 => by + obtain ⟨hc, y, n, hy, hf⟩ := h.toSc t ht.1 h0 + have hk := hto t ht.1 ht.2 hc + refine ⟨⟨hc, hk⟩, y, n, ?_, hf⟩ + show (Fl4SpecA64.detype D σ).kheap (scNameA m _) = _ + rw [detype_keptA σ (scName_keptA hA hk)] + exact hy + frm := fun c hc y n hy p hp => by + have hy' := (detype_someA.1 hy).2 + obtain ⟨t, ht, e, hm⟩ := h.frm c hc.1 y n hy' p hp + exact ⟨t, ⟨ht, e ▸ hfrom c hc.1 hc.2 y n hy' p hp⟩, e, hm⟩ + +/-- `IPlacedA`. -/ +theorem utDrop_iplacedA {N T E R : Set U64} {Bf Bf' : Set (U64 × Fl4SpecA64.Word)} (h : IPlacedA N T E R Bf) + (hBf : Bf' ⊆ Bf) : IPlacedA (dropSetA D N) (dropSetA D T) (dropSetA D E) (dropSetA D R) Bf' := by + have hw : ∀ a, BufW Bf' a → BufW Bf a := fun a ⟨bp, hbp, rest⟩ => ⟨bp, hBf hbp, rest⟩ + exact + { n := ⟨fun t ht => h.n.tcb t ht.1, fun n hn => h.n.ntfn n hn.1, fun n hn t ht => h.n.disj n hn.1 t ht.1⟩ + e := + { tcb := fun t ht => h.e.tcb t ht.1 + ep := fun e he => h.e.ep e he.1 + rep := fun r hr => h.e.rep r hr.1 + et := fun e he t ht => h.e.et e he.1 t ht.1 + rt := fun r hr t ht => h.e.rt r hr.1 t ht.1 + er := fun e he r hr => h.e.er e he.1 r hr.1 + balign := fun a ha => h.e.balign a (hw a ha) + bt := fun a ha t ht => h.e.bt a (hw a ha) t ht.1 + be := fun a ha e he => h.e.be a (hw a ha) e he.1 + br := fun a ha r hr => h.e.br a (hw a ha) r hr.1 + binj := fun bp hbp bq hbq => h.e.binj bp (hBf hbp) bq (hBf hbq) } + ne := fun n hn e he => h.ne n hn.1 e he.1 + nr := fun n hn r hr => h.nr n hn.1 r hr.1 + nb := fun n hn a ha => h.nb n hn.1 a (hw a ha) } + +/-- The IPC buffers all of whose 128 words are retained. -/ +def dropBfA (D : Set Fl4SpecA64.ObjRef) (Bf : Set (U64 × Fl4SpecA64.Word)) : Set (U64 × Fl4SpecA64.Word) := + {bp | bp ∈ Bf ∧ ∀ k : U64, k.val < 128 → specPtr (bufW bp.1 k) ∉ D} + +/-- `IpcSchedPlacementA`. `hbuf`: a retained buffer's container is retained (no object straddles `D`). -/ +theorem utDrop_ipcPlaceA {th : TypedHeapA} {N E R : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + (h : IpcSchedPlacementA th N E R Bf) + (hbuf : ∀ b, BufW (dropBfA D Bf) b → ∀ p len, th p = some (.other len) → p.val ≤ b.val → b.val < p.val + len → + specPtr p ∉ D) : + IpcSchedPlacementA (dropThA D th) (dropSetA D N) (dropSetA D E) (dropSetA D R) (dropBfA D Bf) where + notification := fun n hn => dropThA_someA.2 ⟨hn.2, h.notification n hn.1⟩ + endpoint := fun e he => dropThA_someA.2 ⟨he.2, h.endpoint e he.1⟩ + reply := fun r hr => dropThA_someA.2 ⟨hr.2, h.reply r hr.1⟩ + buffer := fun b hb => by + obtain ⟨bp, hbp, rest⟩ := hb + obtain ⟨p, len, h1, h2, h3⟩ := h.buffer b ⟨bp, hbp.1, rest⟩ + exact ⟨p, len, dropThA_someA.2 ⟨hbuf b ⟨bp, hbp, rest⟩ p len h1 h2 h3, h1⟩, h2, h3⟩ + +set_option linter.unusedSectionVars false in +/-- `BufCover`. `hw`: a retained thread's IPC buffer is retained, all of its words. -/ +theorem utDrop_bufCoverA {T : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {s : S} {σ : Fl4SpecA64.State Unit} + (h : BufCover st T Bf s σ) + (hw : ∀ t ∈ T, specPtr t ∉ D → ∀ w, msg.lookup_ipc_buffer st s false t = ok w ∨ + msg.lookup_ipc_buffer st s true t = ok w → w ≠ 0#u64 → ∀ k : U64, k.val < 128 → specPtr (bufW w k) ∉ D) : + BufCover st (dropSetA D T) (dropBfA D Bf) s (Fl4SpecA64.detype D σ) := by + intro t ht x b sb w hx hsb hl + obtain ⟨h1, h2⟩ := h t ht.1 x b sb w (detype_someA.1 hx).2 hsb hl + refine ⟨⟨h1, hw t ht.1 ht.2 w ?_ h2⟩, h2⟩ + cases b + · exact Or.inl hl + · exact Or.inr hl + +/-! ## `SInvXA` -/ + +/-- `SInvXA`. Closure: the current thread and SC, the idle objects (`hcur`, `hidle`), the interrupt CNodes (`hirq`) +and the queue members are retained. -/ +theorem utDrop_sinvA {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} {n : Nat} (h : SInvXA σ m n) + (hc : UtSpecClosedA D σ) (hA : Fl4SpecA64.idleScPtr ∉ D) (hcur : σ.curThread ∉ D) (hcsc : σ.curSc ∉ D) + (hidle : σ.idleThread ∉ D) + (hirq : ∀ irq : Fl4SpecA64.Irq, irq.toNat < Fl4SpecA64.fl4IrqCount → σ.interruptIrqNode irq ∉ D) : + SInvXA (Fl4SpecA64.detype D σ) (dropMA D m) n where + mc := + { curCore_lt := h.mc.curCore_lt + idleThread_inj := h.mc.idleThread_inj + idleSc_inj := h.mc.idleSc_inj + affinity_lt := fun t c ht => by + change (if t ∈ D then none else m.tcbAffinity t) = some c at ht + by_cases hk : t ∈ D + · rw [ite_eq_left hk] at ht + cases ht + · rw [ite_eq_right hk] at ht + exact h.mc.affinity_lt t c ht } + view := h.view + curTcb := by + show ∃ x, (Fl4SpecA64.detype D σ).kheap σ.curThread = some (.tcb x) + rw [detype_keptA σ hcur] + exact h.curTcb + idleTcb := by + show ∃ x, (Fl4SpecA64.detype D σ).kheap σ.idleThread = some (.tcb x) ∧ _ + rw [detype_keptA σ hidle] + exact h.idleTcb + onlyIdle := fun t x ht => h.onlyIdle t x (detype_someA.1 ht).2 + idleSc := by + show ∃ x y k, (Fl4SpecA64.detype D σ).kheap σ.idleThread = some (.tcb x) ∧ _ ∧ + (Fl4SpecA64.detype D σ).kheap Fl4SpecA64.idleScPtr = some (.schedContext y k) ∧ y.scTcb = some σ.idleThread + rw [detype_keptA σ hidle, detype_keptA σ hA] + exact h.idleSc + curSc := by + show ∃ y k, (Fl4SpecA64.detype D σ).kheap σ.curSc = some (.schedContext y k) + rw [detype_keptA σ hcsc] + exact h.curSc + ctQ := h.ctQ + idleQ := h.idleQ + rq := fun d p t ht => by + rw [detype_keptA σ (hc.rq d p t ht)] + exact h.rq d p t ht + rls := fun t ht => by + rw [detype_keptA σ (hc.rls t ht)] + exact h.rls t ht + rpos := fun p y k hp => h.rpos p y k (detype_someA.1 hp).2 + zc := (CurFactsA.back (σ := σ) (σ' := Fl4SpecA64.detype D σ) ⟨h.zc, h.rlsRun, h.curScReady⟩ (fun _ hq => hq) rfl rfl rfl + (fun t x hx => ⟨x, (detype_someA.1 hx).2, rfl⟩) + (fun t x hx hm => ⟨x, by rw [detype_keptA σ (hc.rls t hm)]; exact hx, id⟩) + (fun y k e => ⟨y, (detype_someA.1 e).2, fun a => ⟨a, id⟩⟩)).1 + rlsRun := (CurFactsA.back (σ := σ) (σ' := Fl4SpecA64.detype D σ) ⟨h.zc, h.rlsRun, h.curScReady⟩ (fun _ hq => hq) rfl rfl rfl + (fun t x hx => ⟨x, (detype_someA.1 hx).2, rfl⟩) + (fun t x hx hm => ⟨x, by rw [detype_keptA σ (hc.rls t hm)]; exact hx, id⟩) + (fun y k e => ⟨y, (detype_someA.1 e).2, fun a => ⟨a, id⟩⟩)).2.1 + curScReady := (CurFactsA.back (σ := σ) (σ' := Fl4SpecA64.detype D σ) ⟨h.zc, h.rlsRun, h.curScReady⟩ (fun _ hq => hq) rfl rfl rfl + (fun t x hx => ⟨x, (detype_someA.1 hx).2, rfl⟩) + (fun t x hx hm => ⟨x, by rw [detype_keptA σ (hc.rls t hm)]; exact hx, id⟩) + (fun y k e => ⟨y, (detype_someA.1 e).2, fun a => ⟨a, id⟩⟩)).2.2 + faultSt := fun t x hx f => h.faultSt t x (detype_someA.1 hx).2 f + irqN := + { node := by + refine ⟨⟨h.irqN.node.1.1, fun irq hi => ?_⟩, fun irq hi n' => ?_⟩ + · obtain ⟨cs, h1, h2⟩ := h.irqN.node.1.2 irq hi + refine ⟨cs, ?_, h2⟩ + show (Fl4SpecA64.detype D σ).kheap (σ.interruptIrqNode irq) = _ + rw [detype_keptA σ (hirq irq hi)] + exact h1 + · rintro ⟨cs, h1, h2⟩ + exact h.irqN.node.2 irq hi n' ⟨cs, (detype_someA.1 h1).2, h2⟩ + slot := fun irq hi o b a ho => h.irqN.slot irq hi o b a (detype_someA.1 ho).2 + ref := fun p o b a ho => h.irqN.ref p o b a (detype_someA.1 ho).2 } + +/-! ## `sys`, the CDT tree, and the device case -/ + +/-- **The slots off the region** are those whose spec address is retained, when every slot lies in a heap object. -/ +theorem utD_offRA {H : Heap} {OS : Set U64} {p : U64} {lo hi : Nat} (hlo : lo = p.val) + (hmem : ∀ q : U64, specPtr q ∈ D ↔ lo ≤ q.val ∧ q.val < hi) (hin : RegionIn H lo hi) + (hs : ∀ x ∈ OS, ∃ q k, H q = some k ∧ q.val ≤ x.val ∧ x.val + 32 ≤ q.val + 2 ^ k.bits) : + dropSetA D OS = OffR lo hi OS := by + have _ := hlo + ext x + show x ∈ OS ∧ specPtr x ∉ D ↔ x ∈ OS ∧ (x.val + 32 ≤ lo ∨ hi ≤ x.val) + refine and_congr_right fun hx => ?_ + rw [hmem x] + obtain ⟨q, k, hq, h1, h2⟩ := hs x hx + by_cases hm : MeetsR lo hi q.val (q.val + 2 ^ k.bits) + · obtain ⟨a, b⟩ := hin q k hq hm + omega + · unfold MeetsR at hm + omega + +/-- **`CdtRelA.tree`** over the retained slots, from A64 P1's tree. `hsub` / `hnul`: closure item A (the J5 +instance's `FinStA.sub` / `.nul`); `hgone`: a removed object's slots are null. -/ +theorem utDrop_treeA {th : TypedHeapA} {OS : Set U64} {s : S} {G : List (List U64)} {par : U64 → Option U64} + (h : TreeA st OS G par s) (hsub : ∀ a, IsSlotA th a → a ∈ OS) (hnul : ∀ x ∈ OS, ¬ IsSlotA th x → capA st s x = .Null) + (hgone : ∀ x ∈ OS, IsSlotA th x → ¬ IsSlotA (dropThA D th) x → capA st s x = .Null) : + TreeA st {a | IsSlotA (dropThA D th) a} G par s := + cdtTree_of h (fun a ha => hsub a (isSlot_dropA ha)) fun x hx hn => by + by_cases hs : IsSlotA th x + · exact hgone x hx hs hn + · exact hnul x hx hs + +/-- **`sys.sysl`** (with `SymL` and `BlkOK`) over the dropped heap, A64 P1's `SysL.dropR` as a step that writes +nothing. `Y`: `SymL`, which the unified record does not carry. -/ +theorem utDrop_syslA {H : Heap} {Cs Rp Nt OS : Set U64} {s : S} {extra : Aeneas.Std.Array obj.Slot 3#usize} + {x p sb fi : U64} {ia ie : Nat} {Ac : Set U64} (P : SysPlace st H Cs H.frame Rp Nt OS s extra x) + (hb : BlkOK st H.tcbs Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s) + (B : SysL st H Cs OS ia ie s) (Y : SymL st H Cs s) + (DK : DropOK st H (RegionD H p.val (p.val + 2 ^ sb.val)) Cs s) + (hrun : Runnable (L.mem s (stA (L.curW s)))) (hcx : capA st s x = .Untyped p sb fi false) + (hnd : ∀ G par, TreeA st OS G par s → ∀ y, ¬ Fl4Proofs.P3.Mdb.Desc par y x) (hfi : fi.val ≤ 2 ^ sb.val) + (hUZ : UtZeroA st OS s) : + SysL st (H.drop (RegionD H p.val (p.val + 2 ^ sb.val))) Cs (OffR p.val (p.val + 2 ^ sb.val) OS) ia ie s ∧ + SymL st (H.drop (RegionD H p.val (p.val + 2 ^ sb.val))) Cs s ∧ + BlkOK st (H.drop (RegionD H p.val (p.val + 2 ^ sb.val))).tcbs Cs + (H.drop (RegionD H p.val (p.val + 2 ^ sb.val))).omega (H.drop (RegionD H p.val (p.val + 2 ^ sb.val))) + (H.drop (RegionD H p.val (p.val + 2 ^ sb.val))).frame (H.drop (RegionD H p.val (p.val + 2 ^ sb.val))).replies + (H.drop (RegionD H p.val (p.val + 2 ^ sb.val))).scs (H.drop (RegionD H p.val (p.val + 2 ^ sb.val))).ntfns + (H.drop (RegionD H p.val (p.val + 2 ^ sb.val))).scBits Ac s := + SysL.dropR P hb B Y DK hrun hcx hnd (WritesIn.refl _ _) (fun _ _ _ => rfl) hcx hfi + (fun u hu => hUZ u hu.1) + +/-- **`sys.ent` but its VSpace clause** (`vs : VsTy`, after the freeze): heap, block invariants, slot set and user +words over the dropped heap. -/ +theorem utDrop_entA {H : Heap} {Cs OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} {s : S} {p : U64} {sb : Nat} + (h : EntryInvA3 st H Cs OS U Ac Pp Tb Lv s) + (DK : DropOK st H (RegionD H p.val (p.val + 2 ^ sb)) Cs s) (hin : RegionIn H p.val (p.val + 2 ^ sb)) : + HeapOK (H.drop (RegionD H p.val (p.val + 2 ^ sb))) ∧ + BXI st (H.drop (RegionD H p.val (p.val + 2 ^ sb))).tcbs Cs (H.drop (RegionD H p.val (p.val + 2 ^ sb))).omega + (H.drop (RegionD H p.val (p.val + 2 ^ sb))) (H.drop (RegionD H p.val (p.val + 2 ^ sb))).frame + (H.drop (RegionD H p.val (p.val + 2 ^ sb))).replies (H.drop (RegionD H p.val (p.val + 2 ^ sb))).scs + (H.drop (RegionD H p.val (p.val + 2 ^ sb))).ntfns (H.drop (RegionD H p.val (p.val + 2 ^ sb))).scBits Ac s ∧ + OSOK (H.drop (RegionD H p.val (p.val + 2 ^ sb))).tcbs (H.drop (RegionD H p.val (p.val + 2 ^ sb))).omega + (H.drop (RegionD H p.val (p.val + 2 ^ sb))).frame (OffR p.val (p.val + 2 ^ sb) OS) ∧ + Fl4ProofsA64.P1.UserOK st (H.drop (RegionD H p.val (p.val + 2 ^ sb))).tcbs Cs + (H.drop (RegionD H p.val (p.val + 2 ^ sb))).omega (H.drop (RegionD H p.val (p.val + 2 ^ sb))) U s ∧ + (L.ksb s).val + 788736 ≤ 2 ^ 64 := by + obtain ⟨hQ, hB, hX, hSg, hS⟩ := h.bxi + have blk := BlkOK.drop DK (Ac := Ac) ⟨hQ, hB, hX, hS, hSg⟩ + obtain ⟨b1, b2, b3, b4, b5⟩ := blk + have os := OSOK.dropR (lo := p.val) (hi := p.val + 2 ^ sb) h.heap h.os hin + -- the signature words only shrink, so the slots stay outside them + have os' : OSOK (H.drop (RegionD H p.val (p.val + 2 ^ sb))).tcbs (H.drop (RegionD H p.val (p.val + 2 ^ sb))).omega + (H.drop (RegionD H p.val (p.val + 2 ^ sb))).frame (OffR p.val (p.val + 2 ^ sb) OS) := + ⟨fun x hx => ⟨(os.slot x hx).1, fun o ho h8 => ((os.slot x hx).2 o ho h8).imp id + fun w => ⟨w.1, fun hf => w.2 (Heap.frame_drop_sub hf)⟩⟩, os.fit, os.disj, os.nz⟩ + refine ⟨HeapOK.drop h.heap, ⟨b1, b2, b3, b5, b4⟩, os', fun b hb => ?_, h.ksw⟩ + obtain ⟨u1, u2, u3⟩ := h.user b hb + refine ⟨u1.sub Heap.tcbs_drop_sub, fun hm => u2 (Heap.omega_drop_sub hm), fun e he => u3 e ?_⟩ + exact he.imp (fun x => (Heap.drop_eq_some.1 x).2) (fun x => (Heap.drop_eq_some.1 x).2) + +/-- **A region that holds nothing of the record** (a device untyped, Architect 00:06Z): the drop changes nothing. -/ +theorem utDrop_deviceA {Z : XSetsA} {H : Heap} {OS : Set U64} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (hth : ∀ p, Z.th p ≠ none → specPtr p ∉ D) + (hsets : ∀ p, p ∈ Z.T ∨ p ∈ Z.C ∨ p ∈ Z.V ∨ p ∈ Z.X ∨ p ∈ Z.Y ∨ p ∈ Z.Pp ∨ p ∈ Z.S1 ∨ p ∈ Z.T2 → + specPtr p ∉ D) + (hH : ∀ p, H p ≠ none → specPtr p ∉ D) (hOS : ∀ x ∈ OS, specPtr x ∉ D) + (hk : ∀ q ∈ D, σ.kheap q = none) (ha : ∀ q ∈ D, m.tcbAffinity q = none) : + dropZA D Z = Z ∧ dropHA D H = H ∧ dropSetA D OS = OS ∧ Fl4SpecA64.detype D σ = σ ∧ dropMA D m = m := by + have set : ∀ X : Set U64, (∀ p ∈ X, specPtr p ∉ D) → dropSetA D X = X := fun X h => + Set.ext fun p => ⟨fun hp => hp.1, fun hp => ⟨hp, h p hp⟩⟩ + have fn : ∀ {β : Type} (f : U64 → Option β), (∀ p, f p ≠ none → specPtr p ∉ D) → + (fun p => if specPtr p ∈ D then none else f p) = f := by + intro β f h + funext p + by_cases hd : specPtr p ∈ D + · rw [ite_eq_left hd] + by_contra hne + exact h p (Ne.symm hne) hd + · rw [ite_eq_right hd] + refine ⟨?_, fn H hH, set OS hOS, ?_, ?_⟩ + · have e1 : dropThA D Z.th = Z.th := fn Z.th hth + have eT := set Z.T fun p h => hsets p (Or.inl h) + have eC := set Z.C fun p h => hsets p (Or.inr (Or.inl h)) + have eV := set Z.V fun p h => hsets p (Or.inr (Or.inr (Or.inl h))) + have eX := set Z.X fun p h => hsets p (Or.inr (Or.inr (Or.inr (Or.inl h)))) + have eY := set Z.Y fun p h => hsets p (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl h))))) + have eP := set Z.Pp fun p h => hsets p (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl h)))))) + have eS := set Z.S1 fun p h => hsets p (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl h))))))) + have e2 := set Z.T2 fun p h => hsets p (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr h))))))) + unfold dropZA + rw [e1, eT, eC, eV, eX, eY, eP, eS, e2] + · have : (fun x => if x ∈ D then none else σ.kheap x) = σ.kheap := by + funext q + by_cases hd : q ∈ D + · rw [ite_eq_left hd, hk q hd] + · rw [ite_eq_right hd] + unfold Fl4SpecA64.detype + rw [this] + · have : (fun p => if p ∈ D then none else m.tcbAffinity p) = m.tcbAffinity := by + funext q + by_cases hd : q ∈ D + · rw [ite_eq_left hd, ha q hd] + · rw [ite_eq_right hd] + unfold dropMA + rw [this] + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtDropCA.lean b/proofs/Fl4ProofsA64/P3/UtDropCA.lean new file mode 100644 index 000000000..9c8d6b5df --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtDropCA.lean @@ -0,0 +1,914 @@ +/- +P3 (aarch64, A64-P3 Untyped; lead 04:55Z): **`UtDropG` derived** for a childless untyped (`utDropG_of`, RAM or device) and `UtDropC` for the childless RAM untyped being retyped (`utDropC_of`), with +the removed set `D = utDA p sb` (spec `delete_objects`' set). + +The one placement fact behind most of it is `ut_jointA`: by `ObjAllocA` (J1) every joint object (P1 heap object, +typed-heap object, relation page, root pair, the idle-SC anchor) is off the untyped's block or inside its used +prefix, so all of its addresses are removed together. + +Inputs: the unified record at concrete J5 / J7, A64 P1's decode facts (`DecPre`, `SysPlace`, the slot's cap), the +successful `ensure_no_children`, and the platform's IPA width `hn`. + +Named premises: +* [J] `symL`: A64 P1's `SymL` (producer: the P1 completion wave). +* [S] `othT`, `othC`: B, another core's current thread and current SC are not in the retyped block. +-/ +import Fl4ProofsA64.P3.UtDropTopA +import Fl4ProofsA64.P3.UtVsConvA +import Fl4ProofsA64.P3.UtIdleAllA +import Fl4ProofsA64.P3.CnodeCopyA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {ar : machine.Arch A} + +/-! ## Placement -/ + +/-- **A joint object is removed or kept as a whole** (module docstring). -/ +theorem ut_jointA {Z : XSetsA} {H : Heap} {OS : Set U64} {s : S} (hA : ObjAllocA (st := st) Z H OS s) + {x p sb fi : U64} {dev : Bool} (hx : x ∈ OS) (hc : capA st s x = .Untyped p sb fi dev) (hfi : fi.val ≤ 2 ^ sb.val) + {D : Set ObjRef} (hD : ∀ z : U64, specPtr z ∈ D ↔ p.val ≤ z.val ∧ z.val < p.val + 2 ^ sb.val) + {q : U64} {bytes : Nat} (hj : JointObjectA Z H q bytes) {a b : U64} + (a1 : q.val ≤ a.val) (a2 : a.val < q.val + bytes) (b1 : q.val ≤ b.val) (b2 : b.val < q.val + bytes) + (ka : specPtr a ∉ D) : specPtr b ∉ D := fun hb => by + refine ka ((hD a).2 ?_) + have hb' := (hD b).1 hb + rcases hA q bytes hj x hx p sb fi dev hc with o | o | o <;> omega + +/-- A retained slot address is a slot of the dropped typed heap, when its owner is kept. -/ +theorem ut_slotKeepA {th : TypedHeapA} {D : Set ObjRef} {y : U64} (h : IsSlotA th y) + (keep : ∀ q k, th q = some k → q.val ≤ y.val → y.val + 32 ≤ q.val + k.size → specPtr q ∉ D) + {kb : U64} (P : PlaceA th kb) : IsSlotA (dropThA D th) y := by + obtain ⟨q, ⟨r, hr, i, hi, rfl⟩ | ⟨ht, i, hi, rfl⟩⟩ := h + · obtain ⟨k, hk, hj, hv⟩ := slotVA_of P hr (j := i) (by rw [cnode_size]; omega) + have e : k = .cnode r := Option.some.inj (hk.symm.trans hr) + subst e + exact ⟨q, Or.inl ⟨r, dropThA_someA.2 ⟨keep q _ hr (by omega) (by omega), hr⟩, i, hi, rfl⟩⟩ + · obtain ⟨k, hk, hj, hv⟩ := tcb_slotV P ht hi + have e : k = .tcb := Option.some.inj (hk.symm.trans ht) + subst e + exact ⟨q, Or.inr ⟨dropThA_someA.2 ⟨keep q _ ht (by omega) (by omega), ht⟩, i, hi, rfl⟩⟩ + +/-- An abstract slot's cap in the spec is related to the FL4 slot's. -/ +theorem ut_absRelA {th : TypedHeapA} {mem : U64 → U64} {σ : State Unit} (hR : StateRelA th mem σ) {sl : CslotPtr} + (h : AbsSlotA th sl) : ∃ a, specCapAtA σ sl = some a ∧ CapRelA (capAtA mem (cslotAddrA sl)) a := by + obtain ⟨q, e, hc | ⟨ht, i, hi, e2⟩⟩ := h + · obtain ⟨cs, hk, hwf, hs⟩ := hR.cnode q _ hc + obtain ⟨a, ha, hr⟩ := hs sl.2 rfl + refine ⟨a, ?_, ?_⟩ + · unfold specCapAtA + rw [e, hk] + simp only [hwf, ↓reduceIte] + exact ha + · unfold cslotAddrA + rw [e] + exact hr + · obtain ⟨x, hk, hs⟩ := hR.tcb q ht + obtain ⟨a, ha, hr⟩ := hs i hi + refine ⟨a, ?_, ?_⟩ + · unfold specCapAtA + rw [e, hk, e2] + exact ha + · unfold cslotAddrA + rw [e, e2, ofBl_tcbIdx i (by unfold tcbSlotsA at hi; omega)] + exact hr + +/-- A slot of a sub-typed heap is a slot of the typed heap. -/ +theorem utSlot_subA {th th' : TypedHeapA} {kb : U64} (sub : ∀ q k, th' q = some k → th q = some k) + (_ : PlaceA th kb) {y : U64} (h : IsSlotA th' y) : IsSlotA th y := by + obtain ⟨q, ⟨r, hr, rest⟩ | ⟨ht, rest⟩⟩ := h + · exact ⟨q, Or.inl ⟨r, sub _ _ hr, rest⟩⟩ + · exact ⟨q, Or.inr ⟨sub _ _ ht, rest⟩⟩ + +/-- An abstract slot whose address is retained belongs to a retained object. -/ +theorem utAbs_keptA {Z : XSetsA} {H : Heap} {D : Set ObjRef} {kb : U64} + (tj : ∀ q k, Z.th q = some k → JointObjectA Z H q k.size) + (joint : ∀ (q : U64) (bytes : Nat), JointObjectA Z H q bytes → ∀ a b : U64, q.val ≤ a.val → + a.val < q.val + bytes → q.val ≤ b.val → b.val < q.val + bytes → specPtr a ∉ D → specPtr b ∉ D) + (P : PlaceA Z.th kb) {sl : CslotPtr} (h : AbsSlotA Z.th sl) (k : specPtr (cslotAddrA sl) ∉ D) : sl.1 ∉ D := by + obtain ⟨q, e, hv⟩ := AbsSlotA.v P h + obtain ⟨j, kd, hk, hj, hval⟩ : ∃ j, SlotVA Z.th (cslotAddrA sl) q j := by + rcases hv with ⟨-, hv⟩ | ⟨-, i, -, -, hv⟩ + · exact ⟨_, hv⟩ + · exact ⟨_, hv⟩ + rw [e] + exact joint q _ (tj q kd hk) (cslotAddrA sl) q (by omega) (by omega) (le_refl _) (by omega) k + +/-- `SpNoOvA` as a bound. -/ +theorem ut_spNoOvA {p : U64} {n : Nat} (hn : n < 64) (h : SpNoOvA p n) : (specPtr p).toNat + 2 ^ n ≤ 2 ^ 64 := by + unfold SpNoOvA at h + have hk : 2 ^ n < 2 ^ 64 := Nat.pow_lt_pow_right (by decide) hn + have h1 : (1 : Nat) ≤ 2 ^ n := Nat.one_le_two_pow + have e1 : ((1 : BitVec 64) <<< n).toNat = 2 ^ n := by + rw [BitVec.toNat_shiftLeft, show (1 : BitVec 64).toNat = 1 from rfl, Nat.shiftLeft_eq, Nat.one_mul, + Nat.mod_eq_of_lt hk] + have ha := (specPtr p).isLt + generalize (1 : BitVec 64) <<< n = y at h e1 + by_contra hc + have e2 : (specPtr p + y).toNat = (specPtr p).toNat + 2 ^ n - 2 ^ 64 := by + rw [BitVec.toNat_add, e1, Nat.mod_eq_sub_mod (by omega), Nat.mod_eq_of_lt (by omega)] + have e3 : (specPtr p + y - 1).toNat = (specPtr p + y).toNat - 1 := + BitVec.toNat_sub_of_le (by + rw [BitVec.le_def, e2] + show 1 ≤ (specPtr p).toNat + 2 ^ n - 2 ^ 64 + omega) + rw [BitVec.le_def, e3, e2] at h + omega + +/-- **A thread's IPC buffer lies, 1024 bytes of it, in one frame object**. -/ +theorem utBuf_addrA {H : Heap} {s : S} (hH : HeapOK H) {t w : U64} {rcv : Bool} + (hF : ∀ ptr size rights x1 x2 x3 x4, capA st s (sbWordA t) = .Frame ptr size rights false x1 x2 x3 x4 → + FrObj H ptr size) + (hbc : BufCap (capA st s (sbWordA t))) (hl : msg.lookup_ipc_buffer st s rcv t = ok w) (hw : w ≠ 0#u64) + (hal : (L.mem s (ibWordA t)).val % 1024 = 0) : + ∃ ptr bits, H ptr = some (.other bits) ∧ ptr.val ≤ w.val ∧ w.val + 1024 ≤ ptr.val + 2 ^ bits ∧ + ptr.val + 2 ^ bits ≤ 2 ^ 64 := by + have hs : obj.Tcb.slot t layout.tcb.SLOT_BUFFER = ok (sbWordA t) := by simp [obj.Tcb.slot, lift, sbWordA] + unfold msg.lookup_ipc_buffer at hl + rw [ipc_buffer_word, bind_ok, hs, bind_ok, capA_eq, bind_ok] at hl + rcases hbc with hn | ⟨ptr, size, rights, x1, x2, x3, x4, hc⟩ + · rw [hn] at hl + exact absurd (okinj hl).symm hw + have hobj := hF ptr size rights x1 x2 x3 x4 hc + rw [hc] at hl + simp only [Bool.false_eq_true, ↓reduceIte] at hl + obtain ⟨bits, hbits⟩ : ∃ bits, size.bits = ok bits := by + cases size <;> exact ⟨_, rfl⟩ + have hqH := hobj bits hbits + have hfit := hH.fits ptr _ hqH + simp only [HKind.bits] at hfit + have h30 : bits.val < 31 ∧ 10 ≤ bits.val := by + cases size <;> simp only [capability.FrameSize.bits] at hbits <;> rw [← okinj hbits] <;> simp + have addr : ∀ r : Result U64, (do + let bits ← capability.FrameSize.bits size + let i ← lift (bits &&& 63#u64) + let i1 ← 1#u64 <<< i + let i2 ← lift (core.num.U64.wrapping_sub i1 1#u64) + let i3 ← lift (L.mem s (ibWordA t) &&& i2) + ok (core.num.U64.wrapping_add ptr i3)) = r → r = ok w → + ptr.val ≤ w.val ∧ w.val + 1024 ≤ ptr.val + 2 ^ bits.val := by + intro r hr hrw + rw [hrw] at hr + have h63 : (bits &&& 63#u64).val = bits.val := by + rw [UScalar.val_and, show (63#u64 : U64).val = 2 ^ 6 - 1 from rfl, Nat.and_two_pow_sub_one_eq_mod] + exact Nat.mod_eq_of_lt (by omega) + obtain ⟨i1, e1, h1⟩ : ∃ i1 : U64, (1#u64 <<< (bits &&& 63#u64) : Result U64) = ok i1 ∧ i1.val = 2 ^ bits.val := by + have : (1#u64 <<< (bits &&& 63#u64) : Result U64) ⦃ z => z.val = 2 ^ bits.val ⦄ := by + step as ⟨z, hz1, hz2⟩ + rw [h63] at hz1 + have hlt : 2 ^ bits.val < 2 ^ 64 := Nat.pow_lt_pow_right (by decide) (by omega) + simpa [Nat.shiftLeft_eq, U64.size_def, U64.numBits, Nat.mod_eq_of_lt hlt] using hz1 + exact spec_exists_dut this + rw [hbits, bind_ok] at hr + simp only [lift, bind_ok] at hr + rw [e1, bind_ok] at hr + have e := congrArg UScalar.val (okinj hr) + have hpos : 1 ≤ 2 ^ bits.val := Nat.one_le_two_pow + have hm : (core.num.U64.wrapping_sub i1 1#u64).val = 2 ^ bits.val - 1 := by + have hlt : 2 ^ bits.val < 2 ^ 64 := Nat.pow_lt_pow_right (by decide) (by omega) + have h1v : (1#u64 : U64).val = 1 := rfl + have hsz : UScalar.size UScalarTy.U64 = 2 ^ 64 := by simp [U64.size_def, U64.numBits] + rw [core.num.U64.wrapping_sub_val_eq, h1, h1v, hsz] + omega + have ha : (L.mem s (ibWordA t) &&& core.num.U64.wrapping_sub i1 1#u64).val = + (L.mem s (ibWordA t)).val % 2 ^ bits.val := by + rw [UScalar.val_and, hm, Nat.and_two_pow_sub_one_eq_mod] + have hdvd : 1024 ∣ 2 ^ bits.val := by + rw [show (1024 : Nat) = 2 ^ 10 by norm_num] + exact Nat.pow_dvd_pow 2 h30.2 + have hmod : (L.mem s (ibWordA t)).val % 2 ^ bits.val % 1024 = 0 := by + rw [Nat.mod_mod_of_dvd _ hdvd, hal] + have hlt : (L.mem s (ibWordA t)).val % 2 ^ bits.val < 2 ^ bits.val := Nat.mod_lt _ hpos + have hle : (L.mem s (ibWordA t)).val % 2 ^ bits.val + 1024 ≤ 2 ^ bits.val := by + obtain ⟨c, hc'⟩ := hdvd + have g := Nat.div_add_mod ((L.mem s (ibWordA t)).val % 2 ^ bits.val) 1024 + rw [hmod] at g + omega + rw [wa_valP, ha, Nat.mod_eq_of_lt (by omega)] at e + omega + have hin : ptr.val ≤ w.val ∧ w.val + 1024 ≤ ptr.val + 2 ^ bits.val := by + split at hl + · exact addr _ rfl hl + · split at hl + · exact absurd (okinj hl).symm hw + · split at hl + · exact addr _ rfl hl + · exact absurd (okinj hl).symm hw + exact ⟨ptr, bits.val, hqH, hin.1, hin.2, hfit⟩ + +/-! ## The derivation -/ + +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s : S} {a : A} {σ : State Unit} {m : McState} {p sb fi : U64} + +set_option maxHeartbeats 2000000 in +/-- **`UtDropG` at a childless untyped**, RAM or device: from the region's `DropOK`, `RegionIn`, and its distance +from the state block and the idle area. -/ +theorem utDropG_of {dev : Bool} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {Rq Nt : Set U64} {ex : Aeneas.Std.Array obj.Slot 3#usize} {x : U64} + (R : UtDecP st H Z.Cs H.frame Rq Nt OS s ex x p sb fi dev) + (hcx : capA st s x = .Untyped p sb fi dev) + (he : cdt.ensure_no_children st s x = ok (.Ok ())) + (DK : DropOK st H (RegionD H p.val (p.val + 2 ^ sb.val)) Z.Cs s) + (hRin : RegionIn H p.val (p.val + 2 ^ sb.val)) + (hks : (L.ksb s).val + 788736 ≤ p.val ∨ p.val + 2 ^ sb.val ≤ (L.ksb s).val) + (hio : ie ≤ p.val ∨ p.val + 2 ^ sb.val ≤ ia) + (hact : L.actionW s = 0#u64) + (othT : ∀ c ∈ Z.Cs, c ≠ L.cpu s → specPtr (L.mem s (L.nodeAt s c layout.node.CUR_THREAD)) ∉ utDA p sb.val) + (othC : ∀ c ∈ Z.Cs, c ≠ L.cpu s → specPtr (L.mem s (L.nodeAt s c layout.node.CUR_SC)) ∉ utDA p sb.val) + (hn : nA.val = Fl4SpecA64.fl4A64Platform.s2IpaBits) : + UtDropG st (utDA p sb.val) Z H OS Ac N E Rp Bf stack nA s σ m p sb fi := by + obtain ⟨-, h47, hfi⟩ := R.utv + have hsb : sb.val < 64 := by omega + have hH : HeapOK H := R.heap + have hsub : ∀ y, IsSlotA Z.th y → y ∈ OS := J.j5.2.1 + have hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null := J.j5.2.2 + have htr : TreeA st OS Z.G.chains Z.G.par s := J.j.1.cdt.tree.grow hsub fun y hy hn => hnul y hy hn + have hrev : obj.Slot.revocable st x s = ok true := R.stz.1.2.1 x R.src (by rw [hcx]; rfl) + have hnd : ∀ y, ¬ Fl4Proofs.P3.Mdb.Desc Z.G.par y x := cdt.ensure_no_children.noDesc R.os htr R.src hcx hrev he + have hnk : ∀ c ∈ OS, RngKeyA (capA st s c) → + ¬ (p.val ≤ (ptrA (capA st s c)).val ∧ (ptrA (capA st s c)).val < p.val + 2 ^ sb.val) := + cdt.ensure_no_children.noKey R.os htr.utRangeOrd R.stz.1.2.1 R.src hcx he + have P3 : PlaceA Z.th (L.ksb s) := J.j.1.place + -- the slot's block under `specPtr` + have hxs : IsSlotA Z.th x := by + by_contra h + rw [hnul x R.src h] at hcx + cases hcx + have hwf : SpNoOvA p sb.val := by + obtain ⟨sl, habs, esl⟩ := IsSlotA.absA P3 hxs + obtain ⟨ac, -, hr⟩ := ut_absRelA J.j.1.st habs + have e : capAtA (L.mem s) (cslotAddrA sl) = capA st s x := by rw [esl]; rfl + have h1 := hr.1 + rw [e, hcx] at h1 + exact h1 + have hsp : (specPtr p).toNat + 2 ^ sb.val ≤ 2 ^ 64 := ut_spNoOvA hsb hwf + have hpfit : p.val + 2 ^ sb.val ≤ 2 ^ 64 := + al_fit R.utv.1 (by omega) p.bv.isLt + have hD : ∀ z : U64, specPtr z ∈ utDA p sb.val ↔ p.val ≤ z.val ∧ z.val < p.val + 2 ^ sb.val := + utD_memA hsb hpfit hsp + have hbr := utD_bridgeA (H := H) hsb hpfit hsp hRin + -- the state block is off the untyped + have ksKeep : ∀ b : U64, (L.ksb s).val ≤ b.val → b.val < (L.ksb s).val + 788736 → specPtr b ∉ utDA p sb.val := by + intro b h1 h2 hb + have := (hD b).1 hb + omega + -- objects removed or kept as a whole + have joint : ∀ (q : U64) (bytes : Nat), JointObjectA Z H q bytes → ∀ a b : U64, q.val ≤ a.val → + a.val < q.val + bytes → q.val ≤ b.val → b.val < q.val + bytes → specPtr a ∉ utDA p sb.val → + specPtr b ∉ utDA p sb.val := fun q bytes hj a b a1 a2 b1 b2 ka => + ut_jointA J.objAlloc R.src hcx hfi hD hj a1 a2 b1 b2 ka + have hj : ∀ q k, H q = some k → JointObjectA Z H q (2 ^ k.bits) := fun q k h => + Or.inr (Or.inr (Or.inr ⟨k, h, rfl⟩)) + have tj : ∀ q k, Z.th q = some k → JointObjectA Z H q k.size := fun q k h => + Or.inr (Or.inr (Or.inl ⟨k, h, rfl⟩)) + have keepH : ∀ q k, H q = some k → ∀ b : U64, q.val ≤ b.val → b.val < q.val + 2 ^ k.bits → + specPtr b ∉ utDA p sb.val → dropHA (utDA p sb.val) H q = some k := by + intro q k h b h1 h2 kb + have hpos : 0 < 2 ^ k.bits := Nat.two_pow_pos _ + exact dropHA_someA.2 ⟨joint q _ (hj q k h) b q h1 h2 (le_refl _) (by omega) kb, h⟩ + have omegaKeep : ∀ b : U64, b ∈ H.omega → specPtr b ∉ utDA p sb.val → b ∈ (dropHA (utDA p sb.val) H).omega := by + rintro b ⟨al, q, k, h, hk, hin⟩ kb + exact ⟨al, q, k, keepH q k h b hin.1 hin.2 kb, hk, hin⟩ + have frameSub : ∀ b, b ∈ (dropHA (utDA p sb.val) H).frame → b ∈ H.frame := by + rintro b ⟨al, q, k, h, rest⟩ + exact ⟨al, q, k, (dropHA_someA.1 h).2, rest⟩ + -- slots + have thSub : ∀ q k, dropThA (utDA p sb.val) Z.th q = some k → Z.th q = some k := + fun q k h => (dropThA_someA.1 h).2 + have slotOff : ∀ y, IsSlotA (dropThA (utDA p sb.val) Z.th) y → specPtr y ∉ utDA p sb.val := by + intro y hy + obtain ⟨q, k, hq, h1, h2⟩ := ut_ownerA P3 thSub hy + obtain ⟨kq, hq'⟩ := dropThA_someA.1 hq + exact joint q _ (tj q k hq') q y (le_refl _) (by omega) h1 (by omega) kq + have slotKeep : ∀ y, IsSlotA Z.th y → specPtr y ∉ utDA p sb.val → IsSlotA (dropThA (utDA p sb.val) Z.th) y := + fun y hy ky => ut_slotKeepA hy (fun q k hq h1 h2 => + joint q _ (tj q k hq) y q h1 (by omega) (le_refl _) (by omega) ky) P3 + have hfit : (L.ksb s).val + ksSizeA ≤ 2 ^ 64 := J.j.1.ksFit + have hoff : p.val + 2 ^ sb.val ≤ (L.ksb s).val ∨ (L.ksb s).val + 223488 ≤ p.val := by + rcases hks with g | g + · exact Or.inr (by omega) + · exact Or.inl g + have goneR : ∀ y ∈ OS, p.val ≤ y.val → y.val < p.val + 2 ^ sb.val → capA st s y = .Null := + utClos_goneNullA htr.ut R.cz R.src hcx hnd hfit hoff + have goneNull : ∀ y ∈ OS, specPtr y ∈ utDA p sb.val → capA st s y = .Null := fun y hy h => + goneR y hy ((hD y).1 h).1 ((hD y).1 h).2 + have nonnull : ∀ y ∈ OS, capA st s y ≠ .Null → y ∈ dropSetA (utDA p sb.val) OS := fun y hy h => + ⟨hy, fun hd => h (goneNull y hy hd)⟩ + have nkD : ∀ c ∈ OS, RngKeyA (capA st s c) → specPtr (ptrA (capA st s c)) ∉ utDA p sb.val := fun c hc k hd => + hnk c hc k ((hD _).1 hd) + have key : ∀ y ∈ OS, ∀ (c : capability.Cap) (t : U64), capA st s y = c → RngKeyA c → ptrA c = t → + specPtr t ∉ utDA p sb.val := by + intro y hy c t hc k e + have := nkD y hy (by rw [hc]; exact k) + rwa [hc, e] at this + have kPool : ∀ y ∈ OS, ∀ t b, capA st s y = .AsidPool t b → specPtr t ∉ utDA p sb.val := fun y hy t b hc => + key y hy _ t hc (RngKeyA.ap _ _) (ptrA_ap _ _) + have kPT : ∀ y ∈ OS, ∀ t mp asid va, capA st s y = .PageTable t mp asid va → specPtr t ∉ utDA p sb.val := + fun y hy t mp asid va hc => key y hy _ t hc (RngKeyA.pt _ _ _ _) (ptrA_pt _ _ _ _) + have kPml : ∀ y ∈ OS, ∀ t mp asid, capA st s y = .Pml4 t mp asid → specPtr t ∉ utDA p sb.val := + fun y hy t mp asid hc => key y hy _ t hc (RngKeyA.pml4 _ _ _) (ptrA_pml4 _ _ _) + have kEpt : ∀ y ∈ OS, ∀ t l mp asid va, capA st s y = .Ept t l mp asid va → specPtr t ∉ utDA p sb.val := + fun y hy t l mp asid va hc => key y hy _ t hc (RngKeyA.ept _ _ _ _ _) (ptrA_ept _ _ _ _ _) + have kHv : ∀ y ∈ OS, ∀ t mp asid, capA st s y = .HighVSpace t mp asid → specPtr t ∉ utDA p sb.val := + fun y hy t mp asid hc => key y hy _ t hc (RngKeyA.hv _ _ _) (ptrA_hv _ _ _) + have kVc : ∀ y ∈ OS, ∀ v, capA st s y = .Vcpu v → specPtr v ∉ utDA p sb.val := fun y hy v hc => + key y hy _ v hc (RngKeyA.vcpu _) (ptrA_vcpu _) + -- J7 + obtain ⟨Pt, Ub7, vr, pool, armc, capc, vcc, frd, hic, sched, apart, frub, alub, vcub, Pp7, Tb7, Hr, Te, Kt, Lv7, Re, bits, r2, kt, hkl⟩ := + J.j7 + have hVs : UtVsClosA (utDA p sb.val) Z.Pp Z.S1 Z.Y Z.X Z.T2 Z.Sh2 (L.mem s) (L.ksb s) σ := by + refine utVsClos_of_r2A r2 J.j.1.vs J.j.1.ext hn ?_ + intro y hy t h + rcases h with ⟨b, hc⟩ | ⟨mp, asd, v, hc⟩ | ⟨mp, asd, hc⟩ | ⟨l, mp, asd, v, hc⟩ | ⟨mp, asd, hc⟩ + · exact kPool y hy _ _ hc + · exact kPT y hy _ _ _ _ hc + · exact kPml y hy _ _ _ hc + · exact kEpt y hy _ _ _ _ _ hc + · exact kHv y hy _ _ _ hc + -- current objects + have hcpu : L.cpu s ∈ Z.Cs := J.j.1.sch.cpuCs + have hcsc : L.cscW s ∈ H.scs := by + rw [J.scs] + exact J.j.1.cscC _ hcpu + have cur := utClos_curA DK hbr J.sq hcsc + have curT : ∀ c ∈ Z.Cs, specPtr (L.mem s (L.nodeAt s c layout.node.CUR_THREAD)) ∉ utDA p sb.val := by + intro c hc + by_cases e : c = L.cpu s + · rw [e]; exact cur.1 + · exact othT c hc e + have curC : ∀ c ∈ Z.Cs, specPtr (L.mem s (L.nodeAt s c layout.node.CUR_SC)) ∉ utDA p sb.val := by + intro c hc + by_cases e : c = L.cpu s + · rw [e]; exact cur.2.1 + · exact othC c hc e + have idl := utIdleAll_dropA J.idleAll (D := utDA p sb.val) (p := p) (sb := sb) + hio (fun z hz => (hD z).1 hz) + have anchor : idleScPtr ∉ utDA p sb.val := by + intro h + have e : specPtr (fl4Ptr idleScPtr) = idleScPtr := specPtr_fl4Ptr _ + rw [← e] at h + have h' := (hD _).1 h + rcases J.idleAnchor x R.src p sb fi dev hcx with g | g <;> omega + have irqSlot := utClos_irqA (p := p) hsb hpfit hsp hfit hoff + have hTc : Z.T = H.tcbs := J.tcbs.symm + have hCc : Z.C = H.scs := J.scs.symm + have hrpH : ∀ r ∈ Rp, H r = some .reply := J.utIpc.reply + have queues := utClos_queuesA DK hbr J.sq + -- the ASID table + have tpool := hVs.tpool + have poolK : ∀ i, L.mem s (wa (L.ksb s) (AtOff i)) ≠ 0#u64 → + specPtr (L.mem s (wa (L.ksb s) (AtOff i))) ∉ utDA p sb.val := by + intro i hne + have h0 := pool i + rw [vspace.asid_table.eq] at h0 + obtain ⟨y, hy, b, hc⟩ := (WP.spec_ok _).1 h0 hne + exact kPool y hy _ _ hc + refine + { dropOK := DK + regionIn := hRin + heapB := ?heapB + slotsB := ?slotsB + vsClos := hVs + tbl := ?tbl + gone := ?gone + cdt := ?cdt + par := ?par + anchor := anchor + tcbSc := ?tcbSc + irqSlot := irqSlot + idleT := idl.1 + idleC := idl.2.1 + curT := curT + curC := curC + noCap := utClos_noCapA hsb hpfit hsp hsub hnk + nkey := nkD + pmlN := fun y hy q mp asd hc => kPml y (hsub y (utSlot_subA thSub P3 hy)) _ _ _ hc + eptN := fun y hy q l mp asd va hc => kEpt y (hsub y (utSlot_subA thSub P3 hy)) _ _ _ _ _ hc + hvN := fun y hy q mp asd hc => kHv y (hsub y (utSlot_subA thSub P3 hy)) _ _ _ hc + spec := utClos_specA DK hbr hTc hCc rfl J.sq J.sr J.scCov anchor + queues := queues.1 + releases := queues.2 + curW := cur.1 + act := ?act + refills := J.sinv.rpos + ren := ?ren + live := ?live + recvReply := ?recvReply + bound := ?bound + stackKept := ?stackKept + links := ?links + yieldTo := ?yieldTo + yieldFrom := ?yieldFrom + bufPlace := ?bufPlace + bufCover := ?bufCover + curThread := ?curThread + curSc := ?curSc + idleThread := ?idleThread + irqNode := ?irqNode + idleOf := idl.2.2 + whole := ?whole + dead := utClos_deadA goneNull + asidPools := ?asidPools + unmObj := ?unmObj + poolWords := ?poolWords + nullSlots := ?nullSlots + slotOff := slotOff + own := fun y _ ky hy => slotKeep y hy ky + restr := ?restr + kinds := ?kinds + frames := ?frames + arm := ?arm + omega := ?omega + vcpuN := fun y hy v hc => kVc y hy.1 v hc + pages := ?pages } + case heapB => + funext q + unfold Heap.drop dropHA + cases hq : H q with + | none => split_ifs <;> rfl + | some k => + have h := hbr q k hq + by_cases hR : RegionD H p.val (p.val + 2 ^ sb.val) q + · rw [ite_eq_left hR, ite_eq_left (h.2 hR)] + · rw [ite_eq_right hR, ite_eq_right (fun d => hR (h.1 d))] + case slotsB => + have SP := armc.case.slot + ext y + refine and_congr_right fun hy => ?_ + obtain ⟨-, q, k, hq, h1, h2, -, -⟩ := SP y hy + constructor + · intro h hd + have := (hD y).1 hd + omega + · intro ky + by_contra hc + have hky := (not_congr (hD y)).1 ky + have kq := keepH q k hq y h1 (by omega) ky + have hlt : y.val < p.val := by omega + have kp : specPtr p ∈ utDA p sb.val := (hD p).2 ⟨le_refl _, by have := Nat.two_pow_pos sb.val; omega⟩ + have hp1 : p.val < q.val + 2 ^ k.bits := by omega + exact joint q _ (hj q k hq) y p h1 (by omega) (by omega) hp1 ky kp + case tbl => + intro o h1 h2 h8 hne + have hi128 : (o.val - 772352) / 8 < 128 := by omega + let hi : U64 := ⟨BitVec.ofNat 64 ((o.val - 772352) / 8)⟩ + have hiv : hi.val = (o.val - 772352) / 8 := by + show (BitVec.ofNat 64 ((o.val - 772352) / 8)).toNat = _ + rw [BitVec.toNat_ofNat] + exact Nat.mod_eq_of_lt (by omega) + have e : atAF (L.ksb s) hi = wa (L.ksb s) o := by + unfold atAF + congr 1 + apply UScalar.eq_of_val_eq + have e8 : (8#u64 : U64).val = 8 := rfl + have e7 : (772352#u64 : U64).val = 772352 := rfl + rw [wa_valP, core.num.U64.wrapping_mul_val_eq, hiv, e7, e8] + have hsz : UScalar.size UScalarTy.U64 = 2 ^ 64 := by simp [U64.size_def, U64.numBits] + rw [hsz] + omega + rw [← e] at hne ⊢ + exact tpool hi (by omega) hne + case gone => + intro y hy h1 h2 + by_contra hc + exact h2 (slotKeep y h1 (nonnull y hy hc).2) + case cdt => + refine (utClos_linksCdtA (lo := p.val) (hi := p.val + 2 ^ sb.val) J.j.1.cdt ?_ ?_).1 + · exact fun y hy h1 h2 => goneR y (hsub y hy) h1 h2 + · intro sl h hd + exact (hD _).1 (by_contra fun k => (utAbs_keptA tj joint P3 h k) hd) + case par => + refine (utClos_linksCdtA (lo := p.val) (hi := p.val + 2 ^ sb.val) J.j.1.cdt ?_ ?_).2 + · exact fun y hy h1 h2 => goneR y (hsub y hy) h1 h2 + · intro sl h hd + exact (hD _).1 (by_contra fun k => (utAbs_keptA tj joint P3 h k) hd) + case tcbSc => + intro t ht + rw [hTc] at ht + intro _ + exact utClos_scA DK hbr J.sys.ent.bxi t ht + case act => + intro u hu _ + have h := J.j.1.sch.act + rw [hu, hact] at h + simp [actOfA] at h + case ren => + intro c _ e + have h : m.idleScOf m.curCore = specPtr (L.mem s (L.nodeAt s (L.cpu s) layout.node.IDLE_SC)) := by + rw [J.j.1.sch.core] + exact J.j.1.sch.idleScOf _ hcpu + rw [e, h] + exact idl.2.1 _ hcpu + case live => + intro t ht h + rw [hTc] at ht + exact utClos_liveA DK hbr t ht h + case recvReply => + intro t ht _ + rw [hTc] at ht + exact utClos_recvA DK hbr J.sys.ent.bxi t ht + case bound => + intro q hq kq h + rw [hTc] at h + exact utClos_ntfnBoundA DK hbr q (J.utIpc.ntfn q hq) kq h + case stackKept => + have h := J.reply + have h' := J.sr.scr + rw [hCc] at h h' ⊢ + exact utClos_stackA DK hbr J.sys.sysl.vr hrpH h h' + case links => + intro r hr kr w hw hwR + exact utClos_linksA DK hbr r (hrpH r hr) kr w hw (hrpH w hwR) + case yieldTo => + intro t ht kt' h + rw [hTc] at ht + rw [hCc] at h + exact utClos_yieldToA DK hbr t ht kt' h + case yieldFrom => + have h := J.yieldRel + rw [hTc, hCc] at h + rw [hCc] + exact utClos_yieldFromA DK hbr h + case bufPlace => + rintro b ⟨bp, hbp, k, hk, rfl⟩ q len hq h1 h2 + exact joint q _ (tj q _ hq) _ q h1 h2 (le_refl _) (by show q.val < q.val + len; omega) (hbp.2 k hk) + case bufCover => + intro t ht kt' w hl hw k hk + rw [hTc] at ht + have htH : H t = some .tcb := ht + have htf := hH.fits t _ htH + have hsbS : sbWordA t = slotIdx t layout.tcb.SLOT_BUFFER := by + simp [sbWordA, slotIdx, layout.tcb.SLOT_BUFFER, layout.cte.SIZE] + have hsbi : (layout.tcb.SLOT_BUFFER : U64).val < 7 := by simp [layout.tcb.SLOT_BUFFER] + have hsbO : sbWordA t ∈ OS := by + rw [hsbS] + exact armc.case.ts t ht _ hsbi + have hbc : BufCap (capA st s (sbWordA t)) := J.sys.sysl.bh _ hsbO ⟨t, ht, rfl⟩ + have hsbK : sbWordA t ∈ OffR p.val (p.val + 2 ^ sb.val) OS := by + refine ⟨hsbO, ?_⟩ + obtain ⟨-, q, kd, hq, h1, h2, -, -⟩ := armc.case.slot _ hsbO + have hnR : ¬ RegionD H p.val (p.val + 2 ^ sb.val) t := fun h => kt' ((hbr t _ htH).2 h) + have hout : t.val + 2 ^ HKind.tcb.bits ≤ p.val ∨ p.val + 2 ^ sb.val ≤ t.val := by + unfold RegionD MeetsR at hnR + rw [htH] at hnR + simp only [Option.elim] at hnR + have hpos : 0 < 2 ^ sb.val := Nat.two_pow_pos _ + have hpos' : 0 < 2 ^ HKind.tcb.bits := Nat.two_pow_pos _ + omega + have hbits : 224 ≤ 2 ^ HKind.tcb.bits := by simp [HKind.bits] + have hv : (sbWordA t).val = t.val + 32 * (layout.tcb.SLOT_BUFFER : U64).val := by + rw [hsbS] + unfold slotIdx + have e32 : (32#u64 : U64).val = 32 := rfl + have hsz : UScalar.size UScalarTy.U64 = 2 ^ 64 := by simp [U64.size_def, U64.numBits] + rw [wa_valP, core.num.U64.wrapping_mul_val_eq, e32, hsz] + omega + omega + have main : ∀ rcv, msg.lookup_ipc_buffer st s rcv t = ok w → specPtr (bufW w k) ∉ utDA p sb.val := by + intro rcv hl' + have kw : specPtr w ∉ utDA p sb.val := + utClos_bufA hH R.fr htr.ut R.src hcx hnd hsb hpfit hsp hsbK hbc hl' hw + obtain ⟨ptr, b, hq, h1, h2, h3⟩ := utBuf_addrA hH (fun ptr size rights x1 x2 x3 x4 hc => + R.fr _ hsbO ptr size rights x1 x2 x3 x4 hc) hbc hl' hw (J.sys.sysl.ipc t ht) + have hv : (bufW w k).val = w.val + 8 * k.val := by + unfold bufW + have e8 : (8#u64 : U64).val = 8 := rfl + have hsz : UScalar.size UScalarTy.U64 = 2 ^ 64 := by simp [U64.size_def, U64.numBits] + rw [wa_valP, core.num.U64.wrapping_mul_val_eq, e8, hsz] + omega + exact joint ptr _ (hj ptr _ hq) w _ h1 (by simp only [HKind.bits]; omega) (by omega) + (by simp only [HKind.bits]; omega) kw + rcases hl with h | h + · exact main _ h + · exact main _ h + case curThread => + have h := J.j.1.sch + rw [← h.cur, h.core, h.curOfW _ hcpu] + exact curT _ hcpu + case curSc => + have h := J.j.1.sch + rw [← h.csc, h.core, h.curScOfW _ hcpu] + unfold scRenA + split_ifs + · exact anchor + · exact curC _ hcpu + case idleThread => + rw [J.j.1.sch.idle] + exact cur.2.2.1 + case irqNode => + intro irq hirq + have h5 : irq.toNat < 5376 := hirq + let i : U64 := ⟨BitVec.ofNat 64 irq.toNat⟩ + have hiv : i.val = irq.toNat := by + show (BitVec.ofNat 64 irq.toNat).toNat = _ + rw [BitVec.toNat_ofNat] + exact Nat.mod_eq_of_lt (by omega) + have e : BitVec.ofNat 13 i.val = irq := by + rw [hiv] + refine BitVec.eq_of_toNat_eq (n := 13) ?_ + rw [BitVec.toNat_ofNat] + exact Nat.mod_eq_of_lt (BitVec.isLt (w := 13) irq) + rw [← e, J.j.1.irq.node i (by omega)] + exact irqSlot i (by omega) + case whole => + intro q z hq hd b h1 h2 + by_contra kb + have hpos : 0 < 2 ^ z := Nat.two_pow_pos _ + exact joint q _ (hj q _ hq) b q h1 h2 (le_refl _) (by simp only [HKind.bits]; omega) kb hd + case asidPools => + intro i q hq + let hi : U64 := ⟨BitVec.ofNat 64 i.toNat⟩ + have hlt : i.toNat < 128 := i.isLt + have hiv : hi.val = i.toNat := by + show (BitVec.ofNat 64 i.toNat).toNat = _ + rw [BitVec.toNat_ofNat] + exact Nat.mod_eq_of_lt (by omega) + have e : BitVec.ofNat 7 hi.val = i := by + rw [hiv] + rw [BitVec.ofNat_toNat, BitVec.setWidth_eq] + have h := J.j.1.vs.table hi (by omega) + rw [e, hq] at h + unfold optPtrA at h + split_ifs at h with h0 + rw [specPtr_inj (Option.some.inj h)] + exact tpool hi (by omega) h0 + case unmObj => + intro u hu q h + rcases h with ⟨asd, va, hc⟩ | ⟨asd, hc⟩ | ⟨asd, va, hc⟩ | ⟨asd, hc⟩ + · exact kPT u hu.1 _ _ _ _ hc + · exact kPml u hu.1 _ _ _ hc + · exact kEpt u hu.1 _ _ _ _ _ hc + · exact kHv u hu.1 _ _ _ hc + case poolWords => + intro i + rw [vspace.asid_table.eq] + refine (WP.spec_ok _).2 fun hne asid q k hq hin => ?_ + have h0 := armc.case.pobj i + rw [vspace.asid_table.eq] at h0 + have hp12 := (WP.spec_ok _).1 h0 hne + have kp := poolK i hne + have hfit' := hH.fits _ _ hp12 + simp only [HKind.bits] at hfit' + have h4 : (2 : Nat) ^ 12 = 4096 := by norm_num + have hw : (wa (L.mem s (wa (L.ksb s) (AtOff i))) (core.num.U64.wrapping_mul (asid &&& 511#u64) 8#u64)).val = + (L.mem s (wa (L.ksb s) (AtOff i))).val + 8 * (asid &&& 511#u64).val := by + have h511 : (asid &&& 511#u64).val < 512 := by + rw [UScalar.val_and, show (511#u64 : U64).val = 2 ^ 9 - 1 from rfl, Nat.and_two_pow_sub_one_eq_mod] + exact Nat.mod_lt _ (by norm_num) + have e8 : (8#u64 : U64).val = 8 := rfl + have hsz : UScalar.size UScalarTy.U64 = 2 ^ 64 := by simp [U64.size_def, U64.numBits] + rw [wa_valP, core.num.U64.wrapping_mul_val_eq, e8, hsz] + omega + have h511 : (asid &&& 511#u64).val < 512 := by + rw [UScalar.val_and, show (511#u64 : U64).val = 2 ^ 9 - 1 from rfl, Nat.and_two_pow_sub_one_eq_mod] + exact Nat.mod_lt _ (by norm_num) + have kw := joint _ _ (hj _ _ hp12) (L.mem s (wa (L.ksb s) (AtOff i))) + (wa (L.mem s (wa (L.ksb s) (AtOff i))) (core.num.U64.wrapping_mul (asid &&& 511#u64) 8#u64)) + (le_refl _) (by simp only [HKind.bits]; omega) (by omega) (by simp only [HKind.bits]; omega) kp + exact joint q _ (hj q k hq) _ q hin.1 hin.2 (le_refl _) (by have := Nat.two_pow_pos k.bits; omega) kw + case nullSlots => + intro sl h hd + obtain ⟨ac, e, hr⟩ := ut_absRelA J.j.1.st h + have hs := AbsSlotA.isSlot P3 h + have kd : specPtr (cslotAddrA sl) ∈ utDA p sb.val := by_contra fun k => utAbs_keptA tj joint P3 h k hd + have hnull : capAtA (L.mem s) (cslotAddrA sl) = .Null := goneNull _ (hsub _ hs) kd + have h2 := hr.2 + rw [hnull] at h2 + rw [e, ← show absCapA .Null = ac from h2] + rfl + case restr => + intro Kt' hkt + refine + { heap := hH + sub := fun t k h => (dropHA_someA.1 h).2 + slots := fun y hy => hy.1 + nonnull := nonnull + survive := fun t k ht h => ?_ } + have kt' : specPtr t ∉ utDA p sb.val := by + rcases h with h | ⟨y, hy, hk, e⟩ + · intro hd + have h1 := (hD t).1 hd + rcases hkt t h x R.src p sb fi dev hcx with g | g <;> omega + · rw [← e]; exact nkD y hy hk + exact dropHA_someA.2 ⟨kt', ht⟩ + case kinds => + obtain ⟨_, _, -, -, k, -⟩ := R.stz.1.1 + exact k + case frames => + have h := FrameOK.reset R.fr htr.ut R.src hcx hnd (OS' := OffR p.val (p.val + 2 ^ sb.val) OS) + (fun y hy => hy.1) (s' := s) (i' := fi) hcx (fun _ _ _ => rfl) + have eH : H.drop (RegionD H p.val (p.val + 2 ^ sb.val)) = dropHA (utDA p sb.val) H := by + funext q + unfold Heap.drop dropHA + cases hq : H q with + | none => split_ifs <;> rfl + | some k => + have h := hbr q k hq + by_cases hR : RegionD H p.val (p.val + 2 ^ sb.val) q + · rw [ite_eq_left hR, ite_eq_left (h.2 hR)] + · rw [ite_eq_right hR, ite_eq_right (fun d => hR (h.1 d))] + rw [eH] at h + intro y hy ptr size rights x1 x2 x3 x4 hc + refine h y ⟨hy.1, ?_⟩ ptr size rights x1 x2 x3 x4 hc + obtain ⟨-, q, k, hq, h1, h2, -, -⟩ := armc.case.slot y hy.1 + have kq := (dropHA_someA.1 (keepH q k hq y h1 (by omega) hy.2)).1 + have hnR : ¬ RegionD H p.val (p.val + 2 ^ sb.val) q := fun h => kq ((hbr q k hq).2 h) + have hy' := (not_congr (hD y)).1 hy.2 + unfold RegionD MeetsR at hnR + rw [hq] at hnR + simp only [Option.elim] at hnR + have hpos : 0 < 2 ^ sb.val := Nat.two_pow_pos _ + omega + case arm => + intro Ub C + have slotK : ∀ y ∈ OS, specPtr y ∉ utDA p sb.val → ∃ q k, dropHA (utDA p sb.val) H q = some k ∧ + q.val ≤ y.val ∧ y.val + 32 ≤ q.val + 2 ^ k.bits ∧ (k = .tcb → y.val + 32 ≤ q.val + 224) ∧ + (k = .tcb ∨ ∃ z, k = .other z) := by + intro y hy ky + obtain ⟨-, q, k, hq, h1, h2, h3, h4⟩ := C.case.slot y hy + exact ⟨q, k, keepH q k hq y h1 (by omega) ky, h1, h2, h3, h4⟩ + refine + { case := { slot := ?_, ts := ?_, po := ?_, pobj := ?_ } + kb := { ks := ?_, ksIn := ?_, slotKs := ?_, ksU := C.kb.ksU, hT := ?_ } + nt := fun b hb q hq => C.nt b hb q (dropHA_someA.1 hq).2 + pool := C.pool + vh := fun v hv => dropHA_someA.2 ⟨hv.2, C.vh v hv.1⟩ } + · intro y hy + exact ⟨(C.case.slot y hy.1).1, slotK y hy.1 hy.2⟩ + · intro t ht i hi + have ht' := dropHA_someA.1 (show dropHA (utDA p sb.val) H t = some .tcb from ht) + have hmem := C.case.ts t ht'.2 i hi + refine ⟨hmem, ?_⟩ + have htf := hH.fits t _ ht'.2 + have hbits : 224 ≤ 2 ^ HKind.tcb.bits := by simp [HKind.bits] + have hv : (slotIdx t i).val = t.val + 32 * i.val := by + unfold slotIdx + have e32 : (32#u64 : U64).val = 32 := rfl + have hsz : UScalar.size UScalarTy.U64 = 2 ^ 64 := by simp [U64.size_def, U64.numBits] + rw [wa_valP, core.num.U64.wrapping_mul_val_eq, e32, hsz] + omega + exact joint t _ (hj t _ ht'.2) t _ (le_refl _) (by omega) (by omega) (by omega) ht'.1 + · intro i + have h0 := C.case.po i + have h1 := C.case.pobj i + rw [vspace.asid_table.eq] at h0 h1 ⊢ + refine (WP.spec_ok _).2 fun hne asid => ?_ + have W := (WP.spec_ok _).1 h0 hne asid + have hp12 := (WP.spec_ok _).1 h1 hne + have kp := poolK i hne + have hfit' := hH.fits _ _ hp12 + simp only [HKind.bits] at hfit' + have h4 : (2 : Nat) ^ 12 = 4096 := by norm_num + have h511 : (asid &&& 511#u64).val < 512 := by + rw [UScalar.val_and, show (511#u64 : U64).val = 2 ^ 9 - 1 from rfl, Nat.and_two_pow_sub_one_eq_mod] + exact Nat.mod_lt _ (by norm_num) + have hw : (wa (L.mem s (wa (L.ksb s) (AtOff i))) (core.num.U64.wrapping_mul (asid &&& 511#u64) 8#u64)).val = + (L.mem s (wa (L.ksb s) (AtOff i))).val + 8 * (asid &&& 511#u64).val := by + have e8 : (8#u64 : U64).val = 8 := rfl + have hsz : UScalar.size UScalarTy.U64 = 2 ^ 64 := by simp [U64.size_def, U64.numBits] + rw [wa_valP, core.num.U64.wrapping_mul_val_eq, e8, hsz] + omega + have kw := joint _ _ (hj _ _ hp12) (L.mem s (wa (L.ksb s) (AtOff i))) + (wa (L.mem s (wa (L.ksb s) (AtOff i))) (core.num.U64.wrapping_mul (asid &&& 511#u64) 8#u64)) + (le_refl _) (by simp only [HKind.bits]; omega) (by omega) (by simp only [HKind.bits]; omega) kp + exact { al := W.al, om := omegaKeep _ W.om kw, sg := fun h => W.sg (frameSub _ h), + os := fun y hy => W.os y hy.1, ks := W.ks } + · intro i + have h1 := C.case.pobj i + rw [vspace.asid_table.eq] at h1 ⊢ + refine (WP.spec_ok _).2 fun hne => ?_ + exact dropHA_someA.2 ⟨poolK i hne, (WP.spec_ok _).1 h1 hne⟩ + · exact fun q k h hk => C.kb.ks q k (dropHA_someA.1 h).2 hk + · exact fun q z h => C.kb.ksIn q z (dropHA_someA.1 h).2 + · exact fun y hy => C.kb.slotKs y hy.1 + · intro hi hlt + have h := C.kb.hT hi hlt + have hv : (atAF (L.ksb s) hi).val = (L.ksb s).val + (772352 + 8 * hi.val) := by + unfold atAF + have e8 : (8#u64 : U64).val = 8 := rfl + have e7 : (772352#u64 : U64).val = 772352 := rfl + have hsz : UScalar.size UScalarTy.U64 = 2 ^ 64 := by simp [U64.size_def, U64.numBits] + have := J.sys.ent.ksw + rw [wa_valP, wa_valP, core.num.U64.wrapping_mul_val_eq, e7, e8, hsz] + omega + exact omegaKeep _ h (ksKeep _ (by omega) (by omega)) + case omega => + intro Pt' b hb hom + refine omegaKeep b hom ?_ + rcases hb with ⟨t, ht, o, ho, -, rfl⟩ | ⟨o, h1, h2, -, rfl⟩ + · obtain ⟨-, q, z, hq, g1, g2⟩ := ht.2 t rfl + obtain ⟨kq, hq'⟩ := dropHA_someA.1 hq + have hqf := hH.fits q _ hq' + simp only [HKind.bits] at hqf + have hv : (wa t o).val = t.val + o.val := by + rw [wa_valP] + exact Nat.mod_eq_of_lt (by omega) + exact joint q _ (hj q _ hq') q _ (le_refl _) (by simp only [HKind.bits]; omega) (by omega) + (by simp only [HKind.bits]; omega) kq + · have := J.sys.ent.ksw + have hv : (wa (L.ksb s) o).val = (L.ksb s).val + o.val := by + rw [wa_valP] + exact Nat.mod_eq_of_lt (by omega) + exact ksKeep _ (by omega) (by omega) + case pages => + intro q hq t ht + have e : t = q := ht + subst e + have hq0 : SchedPageA st Z OS s t := by + rcases hq with h | h | h | h | ⟨r, hr, e⟩ | h | ⟨y, hy, e⟩ | ⟨u, hu, h⟩ + · exact Or.inl h.1 + · exact Or.inr (Or.inl h.1) + · exact Or.inr (Or.inr (Or.inl h.1)) + · exact Or.inr (Or.inr (Or.inr (Or.inl h.1))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨r, hr.1, e⟩)))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl h.1))))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨y, hy.1, e⟩)))))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ⟨u, hu.1, h⟩)))))) + obtain ⟨hal, r, z, hr, g1, g2⟩ := r2.footprint t (sched t hq0) + have hrf := hH.fits r _ hr + simp only [HKind.bits] at hrf + -- the page itself is retained + have second : ∀ (b : U64) (bytes : Nat), JointObjectA Z H b bytes → bytes = 8192 → b.val + 8192 ≤ 2 ^ 64 → + specPtr b ∉ utDA p sb.val → t = wa b 4096#u64 → specPtr t ∉ utDA p sb.val := by + intro b bytes hjb eb hf kb et + have e4 : (4096#u64 : U64).val = 4096 := rfl + have hv : t.val = b.val + 4096 := by + rw [et, wa_valP, e4] + exact Nat.mod_eq_of_lt (by omega) + exact joint b _ hjb b t (le_refl _) (by omega) (by omega) (by omega) kb + have rootPair : ∀ b ∈ Z.X ∪ Z.Y, specPtr b ∉ utDA p sb.val → t = wa b 4096#u64 → + specPtr t ∉ utDA p sb.val := by + intro b hb kb et + obtain ⟨r', z', hr', k1, k2⟩ := J.setsInH.roots b hb + have := hH.fits r' _ hr' + simp only [HKind.bits] at this + exact second b 8192 (Or.inr (Or.inl ⟨rfl, hb⟩)) rfl (by omega) kb et + have obj13 : ∀ b, H b = some (.other 13) → specPtr b ∉ utDA p sb.val → t = wa b 4096#u64 → + specPtr t ∉ utDA p sb.val := by + intro b hb kb et + have := hH.fits b _ hb + simp only [HKind.bits] at this + exact second b _ (hj b _ hb) (by simp [HKind.bits]) (by omega) kb et + have kt' : specPtr t ∉ utDA p sb.val := by + rcases hq with h | h | h | h | ⟨b, hb, et⟩ | h | ⟨b, hb, et⟩ | ⟨u, hu, h⟩ + · exact h.2 + · exact h.2 + · exact h.2 + · exact h.2 + · exact rootPair b (Or.inl hb.1) hb.2 et + · exact h.2 + · exact rootPair b (Or.inr hb.1) hb.2 et + · rcases h with ⟨mp, asd, v, hc⟩ | ⟨mp, asd, hc⟩ | ⟨asd, v, hc⟩ | ⟨b, asd, v, hc, et⟩ | ⟨b, asd, hc, et⟩ + · exact kPT u hu.1 _ _ _ _ hc + · exact kPml u hu.1 _ _ _ hc + · exact kEpt u hu.1 _ _ _ _ _ hc + · exact obj13 b (r2.eptRoots u hu.1 _ _ _ _ _ (by rw [hc])).1 (kEpt u hu.1 _ _ _ _ _ hc) et + · exact obj13 b (r2.highObjects u hu.1 _ _ _ (by rw [hc])) (kHv u hu.1 _ _ _ hc) et + refine ⟨hal, r, z, ?_, g1, g2⟩ + exact keepH r _ hr t g1 (by simp only [HKind.bits]; omega) kt' + +/-- **`UtDropC` at the retyped childless RAM untyped** (module docstring). -/ +theorem utDropC_of + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {Ud Acd Rq Nt : Set U64} {ex : Aeneas.Std.Array obj.Slot 3#usize} {x : U64} + (Dp : DecPre st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ud Acd s (.Untyped p sb fi false) ex) + (P : SysPlace st H Z.Cs H.frame Rq Nt OS s ex x) + (hcap : obj.Slot.cap st x s = ok (.Untyped p sb fi false)) + (he : cdt.ensure_no_children st s x = ok (.Ok ())) + (symL : SymL st H Z.Cs s) + (othT : ∀ c ∈ Z.Cs, c ≠ L.cpu s → specPtr (L.mem s (L.nodeAt s c layout.node.CUR_THREAD)) ∉ utDA p sb.val) + (othC : ∀ c ∈ Z.Cs, c ≠ L.cpu s → specPtr (L.mem s (L.nodeAt s c layout.node.CUR_SC)) ∉ utDA p sb.val) + (hn : nA.val = Fl4SpecA64.fl4A64Platform.s2IpaBits) : + UtDropC st (utDA p sb.val) Z H OS Ac N E Rp Bf stack nA s σ m p sb fi := by + have hcx : capA st s x = .Untyped p sb fi false := okinj ((capA_eq s x).symm.trans hcap) + have R : UtDecP st H Z.Cs H.frame Rq Nt OS s ex x p sb fi false := + UtDecP.of_live Dp J.sys.sysl.sys P hcap J.sys.sysl.ln J.sys.sysl.vr J.sys.sysl.ly + have G := utDropG_of J R hcx he (R.drop rfl ⟨(), he⟩) (R.ur.rin R.src R.cap) (R.stz.2.2 x R.src p sb fi hcx).2 + (J.sys.sysl.ly.idleOff x R.src p sb fi hcx) Dp.act othT othC hn + obtain ⟨-, -, hfi⟩ := R.utv + exact + { toUtDropG := G + place := ⟨Rq, Nt, ex, x, P, hcx, fun G par h y => + cdt.ensure_no_children.noDesc R.os h R.src hcx (R.stz.1.2.1 x R.src (by rw [hcx]; rfl)) he y⟩ + blk := J.sys.ent.bxi.blk + symL := symL + run := Dp.run + fiLe := hfi + utZero := R.stz.1.2.2.2.1 } + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtDropDevA.lean b/proofs/Fl4ProofsA64/P3/UtDropDevA.lean new file mode 100644 index 000000000..cebd02965 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtDropDevA.lean @@ -0,0 +1,215 @@ +/- +P3 (aarch64, A64-P3 Untyped; lead 10:53:55Z item 1): **the drop of a childless device untyped.** + +A device untyped's reset runs the spec's `delete_objects` over its region while FL4 only zeroes the free index. +By the record's `devUtOff` (D1b-4) nothing the record relates lies in the region, so the record is unchanged on `Z`, `H` +and `OS` and holds after the spec's detype: + +* `utDropG_devA`: the generic drop bundle `UtDropG` at a childless device untyped. +* `utDrop_devIdA`: dropping the region changes none of `Z.th`, the object sets, `H` and `OS`. +* **`utDrop_devA`**: `JStage3A` and `SymL` at `(Z, H, OS)` with the spec state `detype D σ`. + +Named premises: +* [J] `symL`: A64 P1's `SymL`. +* [S] `othT`, `othC`: another core's current thread and current SC are not in the region. (They also follow from + `devOff`; kept in the RAM drop's shape.) +-/ +import Fl4ProofsA64.P3.UtDropCA +import Fl4ProofsA64.P1.MdbRtBlk +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s : S} {a : A} {σ : State Unit} {m : McState} {p sb fi : U64} + +/-- No heap object meets a childless device untyped's region. -/ +theorem utDev_heapOffA (dO : DevUtOffA st Z H OS ia ie s) {x : U64} (hx : x ∈ OS) + (hcx : capA st s x = .Untyped p sb fi true) : + ∀ q, H q ≠ none → ¬ RegionD H p.val (p.val + 2 ^ sb.val) q := by + intro q hq hd + cases hk : H q with + | none => exact hq hk + | some k => + have o := dO.obj x hx p sb fi hcx q _ (Or.inr (Or.inr (Or.inr ⟨k, hk, rfl⟩))) + have h1 : 0 < 2 ^ k.bits := Nat.two_pow_pos _ + have h2 : 0 < 2 ^ sb.val := Nat.two_pow_pos _ + unfold RegionD MeetsR at hd + rw [hk] at hd + simp only [Option.elim] at hd + omega + +/-- **`UtDropG` at a childless device untyped.** -/ +theorem utDropG_devA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {Ud Acd Rq Nt : Set U64} {ex : Aeneas.Std.Array obj.Slot 3#usize} {x : U64} + (Dp : DecPre st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ud Acd s (.Untyped p sb fi true) ex) + (P : SysPlace st H Z.Cs H.frame Rq Nt OS s ex x) + (hcap : obj.Slot.cap st x s = ok (.Untyped p sb fi true)) + (he : cdt.ensure_no_children st s x = ok (.Ok ())) + (othT : ∀ c ∈ Z.Cs, c ≠ L.cpu s → specPtr (L.mem s (L.nodeAt s c layout.node.CUR_THREAD)) ∉ utDA p sb.val) + (othC : ∀ c ∈ Z.Cs, c ≠ L.cpu s → specPtr (L.mem s (L.nodeAt s c layout.node.CUR_SC)) ∉ utDA p sb.val) + (hn : nA.val = Fl4SpecA64.fl4A64Platform.s2IpaBits) : + UtDropG st (utDA p sb.val) Z H OS Ac N E Rp Bf stack nA s σ m p sb fi := by + have dO := J.devUtOff + have hcx : capA st s x = .Untyped p sb fi true := okinj ((capA_eq s x).symm.trans hcap) + have R : UtDecP st H Z.Cs H.frame Rq Nt OS s ex x p sb fi true := + UtDecP.of_live Dp J.sys.sysl.sys P hcap J.sys.sysl.ln J.sys.sysl.vr J.sys.sysl.ly + have off := utDev_heapOffA dO R.src hcx + have hcpu : L.cpu s ∈ Z.Cs := J.j.1.sch.cpuCs + have hcsc : L.cscW s ∈ H.scs := by + rw [J.scs] + exact J.j.1.cscC _ hcpu + have DK : DropOK st H (RegionD H p.val (p.val + 2 ^ sb.val)) Z.Cs s := + DropOK.ofNo R.heap J.sys.sysl.ly.h0 off J.sq.curT hcsc + have hRin : RegionIn H p.val (p.val + 2 ^ sb.val) := fun q k hq hm => by + refine absurd ?_ (off q (by rw [hq]; simp)) + unfold RegionD + rw [hq] + exact hm + exact utDropG_of J R hcx he DK hRin (dO.ks x R.src p sb fi hcx).2 (dO.idle x R.src p sb fi hcx) Dp.act + othT othC hn + +/-- **Dropping a childless device untyped's region changes nothing the record keeps**: the typed heap, the object +sets, `H` and `OS`. -/ +theorem utDrop_devIdA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {x : U64} (hx : x ∈ OS) (hcx : capA st s x = .Untyped p sb fi true) + (hsb : sb.val < 64) (hp : p.val + 2 ^ sb.val ≤ 2 ^ 64) (hsp : (specPtr p).toNat + 2 ^ sb.val ≤ 2 ^ 64) : + dropZA (utDA p sb.val) Z = Z ∧ dropHA (utDA p sb.val) H = H ∧ dropSetA (utDA p sb.val) OS = OS ∧ + H.drop (RegionD H p.val (p.val + 2 ^ sb.val)) = H ∧ OffR p.val (p.val + 2 ^ sb.val) OS = OS ∧ + dropSetA (utDA p sb.val) N = N ∧ dropSetA (utDA p sb.val) E = E ∧ dropSetA (utDA p sb.val) Rp = Rp ∧ + dropSetA (utDA p sb.val) Pp = Pp := by + have dO := J.devUtOff + have joint : ∀ (q : U64) (bytes : Nat), 0 < bytes → JointObjectA Z H q bytes → specPtr q ∉ utDA p sb.val := by + intro q bytes h0 hj hd + have h1 := (utD_memA hsb hp hsp q).1 hd + have o := dO.obj x hx p sb fi hcx q bytes hj + omega + have inH : ∀ q k, H q = some k → specPtr q ∉ utDA p sb.val := fun q k h => + joint q _ (Nat.two_pow_pos _) (Or.inr (Or.inr (Or.inr ⟨k, h, rfl⟩))) + have set : ∀ X : Set U64, (∀ q ∈ X, specPtr q ∉ utDA p sb.val) → dropSetA (utDA p sb.val) X = X := fun X h => + Set.ext fun q => ⟨fun hq => hq.1, fun hq => ⟨hq, h q hq⟩⟩ + have fn : ∀ {β : Type} (f : U64 → Option β), (∀ q, f q ≠ none → specPtr q ∉ utDA p sb.val) → + (fun q => if specPtr q ∈ utDA p sb.val then none else f q) = f := by + intro β f h + funext q + by_cases hd : specPtr q ∈ utDA p sb.val + · rw [ite_eq_left hd] + by_contra hne + exact h q (Ne.symm hne) hd + · rw [ite_eq_right hd] + have page : ∀ q, q ∈ Z.Pp ∨ q ∈ Z.S1 ∨ q ∈ Z.T2 ∨ q ∈ Z.V → specPtr q ∉ utDA p sb.val := by + intro q h + refine joint q 4096 (by omega) (Or.inl ⟨rfl, ?_⟩) + rcases h with h | h | h | h + · exact Or.inl h + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr (Or.inl h)) + · exact Or.inr (Or.inr (Or.inr (Or.inl h))) + have root : ∀ q, q ∈ Z.X ∨ q ∈ Z.Y → specPtr q ∉ utDA p sb.val := fun q h => + joint q 8192 (by omega) (Or.inr (Or.inl ⟨rfl, h⟩)) + have eth : dropThA (utDA p sb.val) Z.th = Z.th := fn Z.th fun q h => by + cases hk : Z.th q with + | none => exact absurd hk h + | some k => + refine joint q k.size ?_ (Or.inr (Or.inr (Or.inl ⟨k, hk, rfl⟩))) + cases k with + | cnode r => exact Nat.two_pow_pos _ + | tcb => exact (by decide : (0 : Nat) < 4096) + | other z => exact J.thPos q z hk + have eT : dropSetA (utDA p sb.val) Z.T = Z.T := set _ fun q h => by + rw [← J.tcbs] at h + exact inH q _ h + have eC : dropSetA (utDA p sb.val) Z.C = Z.C := set _ fun q h => by + rw [← J.scs] at h + obtain ⟨z, hz⟩ := h + exact inH q _ hz + have eH : dropHA (utDA p sb.val) H = H := fn H fun q h => by + cases hk : H q with + | none => exact absurd hk h + | some k => exact inH q k hk + have off := utDev_heapOffA (H := H) dO hx hcx + have eHd : H.drop (RegionD H p.val (p.val + 2 ^ sb.val)) = H := by + funext q + unfold Heap.drop + cases hq : H q with + | none => split_ifs <;> rfl + | some k => rw [ite_eq_right (off q (by rw [hq]; simp))] + have SP : ∀ y ∈ OS, y.val + 32 ≤ p.val ∨ p.val + 2 ^ sb.val ≤ y.val := by + intro y hy + obtain ⟨Pt, Ub7, vr, pool, armc, capc, vcc, frd, hic, sched, apart, frub, alub, vcub, Pp7, Tb7, Hr, Te, Kt, Lv7, Re, bits, r2, + kt, hkl⟩ := J.j7 + obtain ⟨-, q, k, hq, h1, h2, -, -⟩ := armc.case.slot y hy + have o := dO.obj x hx p sb fi hcx q _ (Or.inr (Or.inr (Or.inr ⟨k, hq, rfl⟩))) + have := Nat.two_pow_pos sb.val + omega + have eO : OffR p.val (p.val + 2 ^ sb.val) OS = OS := + Set.ext fun y => ⟨fun h => h.1, fun h => ⟨h, SP y h⟩⟩ + have eOS : dropSetA (utDA p sb.val) OS = OS := set _ fun y hy hd => by + have := (utD_memA hsb hp hsp y).1 hd + rcases SP y hy with g | g <;> omega + refine ⟨?_, eH, eOS, eHd, eO, set _ fun q h => inH q _ (J.utIpc.ntfn q h), set _ fun q h => inH q _ (J.utIpc.ep q h), + set _ fun q h => inH q _ (J.utIpc.reply q h), ?_⟩ + · unfold dropZA + rw [eth, eT, eC, set Z.V fun q h => page q (Or.inr (Or.inr (Or.inr h))), set Z.X fun q h => root q (Or.inl h), + set Z.Y fun q h => root q (Or.inr h), set Z.Pp fun q h => page q (Or.inl h), + set Z.S1 fun q h => page q (Or.inr (Or.inl h)), set Z.T2 fun q h => page q (Or.inr (Or.inr (Or.inl h)))] + · exact set _ fun q h => page q (Or.inl (J.pp ▸ h)) + +/-- **The drop of a childless device untyped** (module docstring): the record is unchanged on `Z`, `H`, `OS` and +holds after the spec's detype. -/ +theorem utDrop_devA [Fact (n ≤ Fl4SpecA64.fl4A64Platform.numNodes)] + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {Ud Acd Rq Nt : Set U64} {ex : Aeneas.Std.Array obj.Slot 3#usize} {x : U64} + (Dp : DecPre st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ud Acd s (.Untyped p sb fi true) ex) + (P : SysPlace st H Z.Cs H.frame Rq Nt OS s ex x) + (hcap : obj.Slot.cap st x s = ok (.Untyped p sb fi true)) + (he : cdt.ensure_no_children st s x = ok (.Ok ())) + (symL : SymL st H Z.Cs s) + (othT : ∀ c ∈ Z.Cs, c ≠ L.cpu s → specPtr (L.mem s (L.nodeAt s c layout.node.CUR_THREAD)) ∉ utDA p sb.val) + (othC : ∀ c ∈ Z.Cs, c ≠ L.cpu s → specPtr (L.mem s (L.nodeAt s c layout.node.CUR_SC)) ∉ utDA p sb.val) + (hn : nA.val = Fl4SpecA64.fl4A64Platform.s2IpaBits) : + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp (dropBfA (utDA p sb.val) Bf) stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s a (detype (utDA p sb.val) σ) (dropMA (utDA p sb.val) m) ∧ + SymL st H Z.Cs s := by + have dO := J.devUtOff + have hcx : capA st s x = .Untyped p sb fi true := okinj ((capA_eq s x).symm.trans hcap) + have C := utDropG_devA J Dp P hcap he othT othC hn + have R : UtDecP st H Z.Cs H.frame Rq Nt OS s ex x p sb fi true := + UtDecP.of_live Dp J.sys.sysl.sys P hcap J.sys.sysl.ln J.sys.sysl.vr J.sys.sysl.ly + obtain ⟨hal, h47, -⟩ := R.utv + have hsb : sb.val < 64 := by omega + have hp : p.val + 2 ^ sb.val ≤ 2 ^ 64 := al_fit hal (by omega) p.bv.isLt + obtain ⟨sl, habs, esl⟩ := IsSlotA.absA J.j.1.place (show IsSlotA Z.th x by + by_contra h + rw [J.j5.2.2 x R.src h] at hcx + cases hcx) + have hwf : SpNoOvA p sb.val := by + obtain ⟨ac, -, hr⟩ := ut_absRelA J.j.1.st habs + have e : capAtA (L.mem s) (cslotAddrA sl) = capA st s x := by rw [esl]; rfl + have h1 := hr.1 + rw [e, hcx] at h1 + exact h1 + have hsp : (specPtr p).toNat + 2 ^ sb.val ≤ 2 ^ 64 := ut_spNoOvA hsb hwf + obtain ⟨eZ, eH, eOS, eHd, eO, eN, eE, eRp, ePp⟩ := utDrop_devIdA J R.src hcx hsb hp hsp + have hsys : SysL st (H.drop (RegionD H p.val (p.val + 2 ^ sb.val))) Z.Cs (OffR p.val (p.val + 2 ^ sb.val) OS) ia ie s ∧ + SymL st (H.drop (RegionD H p.val (p.val + 2 ^ sb.val))) Z.Cs s := by + rw [eHd, eO] + exact ⟨J.sys.sysl, symL⟩ + have top := utDrop_genA J C hsys + rw [eZ, eH, eOS, eN, eE, eRp, ePp] at top + exact top + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtDropJ57A.lean b/proofs/Fl4ProofsA64/P3/UtDropJ57A.lean new file mode 100644 index 000000000..fdaebdf45 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtDropJ57A.lean @@ -0,0 +1,177 @@ +/- +P3 (aarch64, A64-P3 Untyped W2; lead 04:45Z item 1): **the J5 and J7 slot contents after the dead-object drop.** +FL4's memory and the CDT ghost are unchanged; the record moves to the filtered sets and `dropHA D H`. + +* **`utDrop_J5A`**: the concrete J5. `MdbInvA`, `IrqRunA` and `HandlersActiveA` read only memory and the chains, so + they are unchanged; the slot facts need the removed set to cut no object (`own`, `slotOff`). +* **`utDrop_J7A`**: the concrete J7 at `J7R2A`, the `S1aR2` / `S1aKtUt` part by A64 P1's `S1aR2.restrict` at the + kept pages `s1aKeptPt H' Pt`. + +`utDrop_J7A`'s hypotheses beyond the closure facts, **not produced here**: `arm` (P1's `S1aArmCol` column at the +dropped heap) and `omega` (the page-table and ASID-table words stay in `Ω`). No named premise. +-/ +import Fl4ProofsA64.P3.UtDropA +import Fl4ProofsA64.P3.Stage3J5A +import Fl4ProofsA64.P1.S1aR2Restrict +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Classical + +noncomputable section + +variable {D : Set Fl4SpecA64.ObjRef} {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {A : Type} {ar : machine.Arch A} + +theorem utDrop_J5A {Z : XSetsA} {H : Heap} {OS : Set U64} {s : S} {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} (h : J5A st Z H OS s σ m) + (slotOff : ∀ y, IsSlotA (dropThA D Z.th) y → specPtr y ∉ D) + (own : ∀ y ∈ OS, specPtr y ∉ D → IsSlotA Z.th y → IsSlotA (dropThA D Z.th) y) + (σ' : Fl4SpecA64.State Unit) (m' : Fl4SpecA64.McState) : + J5A st (dropZA D Z) (dropHA D H) (dropSetA D OS) s σ' m' := by + obtain ⟨mdb, sub, nul⟩ := h + refine ⟨mdb, fun y hy => ⟨sub y ?_, slotOff y hy⟩, fun y hy hn => nul y hy.1 fun hs => hn (own y hy.1 hy.2 hs)⟩ + obtain ⟨p, hp⟩ := hy + refine ⟨p, ?_⟩ + rcases hp with ⟨r, hr, rest⟩ | ⟨ht, rest⟩ + · exact Or.inl ⟨r, (dropThA_someA.1 hr).2, rest⟩ + · exact Or.inr ⟨(dropThA_someA.1 ht).2, rest⟩ + +/-- The `S1aCapCol` column over the retained slots: every non-null slot is retained. -/ +theorem utDrop_capColA {H H' : Heap} {OS O' Ub : Set U64} {s : S} (h : S1aCapCol st H OS Ub s) + (sub : O' ⊆ OS) (nonnull : ∀ y ∈ OS, capA st s y ≠ .Null → y ∈ O') (frames : FrameOK st H' O' s) : + S1aCapCol st H' O' Ub s := by + have words : ∀ w ∈ FrameWords st O' s, w ∈ FrameWords st OS s := by + rintro w ⟨al, y, hy, rest⟩ + exact ⟨al, y, sub hy, rest⟩ + refine ⟨fun b hb => ?_, ?_, frames⟩ + · obtain ⟨y, hy, ptr, size, rights, x1, x2, x3, x4, bits, hc, rest⟩ := h.uf b hb + exact ⟨y, nonnull y hy (by rw [hc]; exact fun e => by cases e), ptr, size, rights, x1, x2, x3, x4, bits, hc, rest⟩ + · have u : UserOffUt st OS (FrameWords st O' s) s := UserOffUt.mono words h.uo + refine ⟨fun x hx p b i hc => u.1 x (sub hx) p b i hc, fun x hx p b i hc hn => ?_⟩ + refine u.2 x (sub hx) p b i hc fun ⟨hz, hsra⟩ => hn ⟨?_, hsra⟩ + refine nonnull _ hz fun e => ?_ + rw [e, sraA_null] at hsra + cases hsra + +theorem utDrop_J7A {Z : XSetsA} {H H' : Heap} {OS O' : Set U64} {s : S} {σ σ' : Fl4SpecA64.State Unit} + {m m' : Fl4SpecA64.McState} {nA : U64} (h : J7A st (J7R2A st ar nA) Z H OS s σ m) + (hH' : H' = dropHA D H) + (restr : ∀ Kt, S1aKtUt st OS Kt s → S1aRestrict st H H' OS O' Kt s) + (kinds : KindA st OS s) (frames : FrameOK st H' O' s) + (arm : ∀ Ub, S1aArmCol st H Z.Cs OS Z.V Ub s → S1aArmCol st H' Z.Cs O' (dropSetA D Z.V) Ub s) + (omega : ∀ Pt b, VsR (L.ksb s) (s1aKeptPt H' Pt) b → b ∈ H.omega → b ∈ H'.omega) + (vcpu : ∀ x ∈ O', ∀ v, capA st s x = .Vcpu v → specPtr v ∉ D) + (pages : ∀ p, SchedPageA st (dropZA D Z) O' s p → PtHG H' {p}) : + J7A st (J7R2A st ar nA) (dropZA D Z) H' O' s σ' m' := by + obtain ⟨Pt, Ub, vr, pool, armc, capc, vc, fr, hi, sched, apart, frub, alub, vcub, Pp, Tb, Hr, Te, Kt, Lv, Re, bits, r2, kt, hkl⟩ := h + obtain ⟨KLv, ktab2, hik⟩ := hkl + have E := restr Kt kt + have sub : O' ⊆ OS := E.slots + have kept : ∀ p, p ∈ s1aKeptPt H' Pt → p ∈ Pt := fun p hp => hp.1 + have vsr : ∀ b, VsR (L.ksb s) (s1aKeptPt H' Pt) b → VsR (L.ksb s) Pt b := by + intro b hb + rcases hb with ⟨p, hp, rest⟩ | a + · exact Or.inl ⟨p, kept p hp, rest⟩ + · exact Or.inr a + have frameSub : ∀ b, b ∈ H'.frame → b ∈ H.frame := by + rintro b ⟨al, p, k, hp, rest⟩ + exact ⟨al, p, k, E.sub p k hp, rest⟩ + have vr' : VrOK H'.omega H'.frame H'.scs H'.scBits O' (dropSetA D Z.V) Ub (L.ksb s) (s1aKeptPt H' Pt) := by + refine + { al := fun b hb => vr.al b (vsr b hb) + om := fun b hb => omega Pt b hb (vr.om b (vsr b hb)) + sg := fun b hb m => vr.sg b (vsr b hb) (frameSub b m) + sc := fun b hb c hc => ?_ + os := fun b hb ⟨y, hy, r⟩ => vr.os b (vsr b hb) ⟨y, sub hy, r⟩ + vc := fun b hb hv => ?_ + ub := fun b hb => vr.ub b (vsr b hb) + ks := fun b hb => vr.ks b (vsr b hb) } + · subst hH' + rw [dropHA_scsA] at hc + rw [dropHA_scBitsA H hc.2] + exact vr.sc b (vsr b hb) c hc.1 + · obtain ⟨v, hv, rest⟩ := hv + exact vr.vc b (vsr b hb) ⟨v, hv.1, rest⟩ + have pool' : PoolNamed st O' s := fun i => WP.spec_mono (pool i) fun p hp nz => by + obtain ⟨y, hy, b, hc⟩ := hp nz + exact ⟨y, E.nonnull y hy (by rw [hc]; exact fun e => by cases e), b, hc⟩ + have vc' : VcCap st (dropSetA D Z.V) O' s := fun y hy v hc => ⟨vc y (sub hy) v hc, vcpu y hy v hc⟩ + have fr' : DevFrAl st O' s := fun y hy => fr y (sub hy) + have hi' : HiCap511 st O' s := fun y hy => hi y (sub hy) + have sched' : ∀ p, SchedPageA st (dropZA D Z) O' s p → p ∈ s1aKeptPt H' Pt := by + intro p hp + refine ⟨sched p ?_, pages p hp⟩ + unfold SchedPageA at hp ⊢ + rcases hp with a | a | a | a | ⟨r, a, e⟩ | a | ⟨y, a, e⟩ | ⟨u, hu, a⟩ + · exact Or.inl a.1 + · exact Or.inr (Or.inl a.1) + · exact Or.inr (Or.inr (Or.inl a.1)) + · exact Or.inr (Or.inr (Or.inr (Or.inl a.1))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨r, a.1, e⟩)))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl a.1))))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨y, a.1, e⟩)))))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ⟨u, sub hu, a⟩)))))) + have apart' : ∀ b, VcW (dropSetA D Z.V) b → ∀ o : U64, o.val < 223488 → + core.num.U64.wrapping_add (L.ksb s) o ≠ b := by + rintro b ⟨v, hv, rest⟩ + exact apart b ⟨v, hv.1, rest⟩ + have r2' := r2.restrict E kinds frames + have kt' : S1aKtUt st O' Kt s := fun k hk x hx => kt k hk x (sub hx) + exact ⟨s1aKeptPt H' Pt, Ub, vr', pool', arm Ub armc, utDrop_capColA capc sub E.nonnull frames, vc', fr', hi', + sched', apart', CapInv.sub frub sub, alub, fun b ⟨v, hv, rest⟩ => vcub b ⟨v, hv.1, rest⟩, + Pp, _, _, _, Kt, Lv, Re, bits, r2', kt', KLv, ktab2, hik.kt, fun y hy => hik.hdr y (sub hy)⟩ + +end + +end Fl4ProofsA64.P3 + +/-! ## The VSpace core records (lead 04:45Z item 1) -/ + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Classical + +variable {D : Set Fl4SpecA64.ObjRef} {S : Type} {st : machine.Store S} [L : LawfulStore st] + +theorem utIsSlot_dropA {th : TypedHeapA} {y : U64} (h : IsSlotA (dropThA D th) y) : IsSlotA th y := by + obtain ⟨p, hp⟩ := h + refine ⟨p, ?_⟩ + rcases hp with ⟨r, hr, rest⟩ | ⟨ht, rest⟩ + · exact Or.inl ⟨r, (dropThA_someA.1 hr).2, rest⟩ + · exact Or.inr ⟨(dropThA_someA.1 ht).2, rest⟩ + +/-- `JAtZA.pml`. `named`: the root a retained slot's cap names is retained. -/ +theorem utDrop_pmlA {Z : XSetsA} {s : S} + (h : ∀ y, IsSlotA Z.th y → ∀ p mp asid, capAtA (L.mem s) y = .Pml4 p mp asid → p ∈ Z.S1 ∧ Z.Lv p = 0) + (named : ∀ y, IsSlotA (dropThA D Z.th) y → ∀ p mp asid, capAtA (L.mem s) y = .Pml4 p mp asid → specPtr p ∉ D) : + ∀ y, IsSlotA (dropZA D Z).th y → ∀ p mp asid, capAtA (L.mem s) y = .Pml4 p mp asid → + p ∈ (dropZA D Z).S1 ∧ (dropZA D Z).Lv p = 0 := fun y hy p mp asid hc => by + obtain ⟨a, b⟩ := h y (utIsSlot_dropA hy) p mp asid hc + exact ⟨⟨a, named y hy p mp asid hc⟩, b⟩ + +theorem utDrop_stage2CoreA {Z : XSetsA} {s : S} {σ : Fl4SpecA64.State Unit} (h : Stage2CoreA (L := L) Z s σ) + (named : ∀ y, IsSlotA (dropThA D Z.th) y → ∀ p l mp asid va, capAtA (L.mem s) y = .Ept p l mp asid va → + specPtr p ∉ D) : Stage2CoreA (L := L) (dropZA D Z) s (Fl4SpecA64.detype D σ) where + obj := fun r hr => by + rw [detype_keptA σ hr.2] + exact h.obj r hr.1 + back := fun p es hp => by + obtain ⟨hk, hp⟩ := detype_someA.1 hp + obtain ⟨r, hr, e⟩ := h.back p es hp + exact ⟨r, ⟨hr, e ▸ hk⟩, e⟩ + cap := fun y hy p l mp asid va hc => + ⟨h.cap y (utIsSlot_dropA hy) p l mp asid va hc, named y hy p l mp asid va hc⟩ + +theorem utDrop_highCoreA {Z : XSetsA} {s : S} {σ : Fl4SpecA64.State Unit} (h : HighVSpaceCoreA (L := L) Z s σ) + (named : ∀ y, IsSlotA (dropThA D Z.th) y → ∀ p mp asid, capAtA (L.mem s) y = .HighVSpace p mp asid → + specPtr p ∉ D) : HighVSpaceCoreA (L := L) (dropZA D Z) s (Fl4SpecA64.detype D σ) where + obj := fun r hr => by + rw [detype_keptA σ hr.2] + exact h.obj r hr.1 + back := fun p l1 hp => by + obtain ⟨hk, hp⟩ := detype_someA.1 hp + obtain ⟨r, hr, e⟩ := h.back p l1 hp + exact ⟨r, ⟨hr, e ▸ hk⟩, e⟩ + cap := fun y hy p mp asid hc => ⟨h.cap y (utIsSlot_dropA hy) p mp asid hc, named y hy p mp asid hc⟩ + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtDropTopA.lean b/proofs/Fl4ProofsA64/P3/UtDropTopA.lean new file mode 100644 index 000000000..ea1a99a86 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtDropTopA.lean @@ -0,0 +1,655 @@ +/- +P3 (aarch64, A64-P3 Untyped W2; lead 04:45Z item 1): **the top dead-object drop of the unified record.** + +**`utDrop_genA`** (any removed region) and **`utDrop_topA`** (a RAM untyped): `JStage3A` (concrete J5, J7 at `J7R2A`) at `(Z, H, OS, σ, m)` gives `JStage3A` at the dropped +`(dropZA D Z, dropHA D H, dropSetA D OS, detype D σ, dropMA D m)` and the same FL4 state, with `SymL`. The ASID-pool +set is filtered; A64 P1's table set `Tb` is kept (FL4's memory is unchanged, so `VsTy` holds as it is). + +Everything it needs beyond the record is in **`UtDropC`** (derived at the retyped untyped by `P3/UtDropCA`): the facts of the retyped childless untyped for A64 P1's +`SysL` / `EntryInvA` / queue drops, the two bridges between P1's removed-region form and `D`, and the closure facts "no +retained object refers into `D`". None of `UtDropC` is derived in this module. `symL` is the named [J] `SymL`. +-/ +import Fl4ProofsA64.P3.Stage3Rec5A +import Fl4ProofsA64.P3.UtDropJ57A +import Fl4ProofsA64.P3.UtDropVs2A +import Fl4ProofsA64.P3.UtClosA +import Fl4ProofsA64.P1.QueueHeapDrop +import Fl4ProofsA64.P3.UtVsOkJA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {ar : machine.Arch A} + +/-- **What the drop needs beyond the record at any removed region** (module docstring): the part a RAM and a device +untyped share. -/ +structure UtDropG (st : machine.Store S) [L : LawfulStore st] (D : Set ObjRef) (Z : XSetsA) (H : Heap) + (OS Ac : Set U64) (N E Rp : Set U64) (Bf : Set (U64 × Word)) (stack : U64 → List U64) (nA : U64) (s : S) + (σ : State Unit) (m : McState) (p sb fi : U64) : Prop where + -- A64 P1's drop of the region + dropOK : DropOK st H (RegionD H p.val (p.val + 2 ^ sb.val)) Z.Cs s + regionIn : RegionIn H p.val (p.val + 2 ^ sb.val) + -- the bridges + heapB : H.drop (RegionD H p.val (p.val + 2 ^ sb.val)) = dropHA D H + slotsB : OffR p.val (p.val + 2 ^ sb.val) OS = dropSetA D OS + -- closure: state, CDT, slots + vsClos : UtVsClosA D Z.Pp Z.S1 Z.Y Z.X Z.T2 Z.Sh2 (L.mem s) (L.ksb s) σ + tbl : ∀ o : U64, 772352 ≤ o.val → o.val < 773376 → o.val % 8 = 0 → L.mem s (wa (L.ksb s) o) ≠ 0#u64 → + specPtr (L.mem s (wa (L.ksb s) o)) ∉ D + gone : ∀ x ∈ OS, IsSlotA Z.th x → ¬ IsSlotA (dropThA D Z.th) x → capA st s x = .Null + cdt : ∀ sl q, σ.cdt sl = some q → sl.1 ∉ D ∧ q.1 ∉ D + par : ∀ a b, Z.G.par a = some b → ∃ sl, AbsSlotA Z.th sl ∧ cslotAddrA sl = a ∧ sl.1 ∉ D + anchor : idleScPtr ∉ D + tcbSc : ∀ t ∈ Z.T, specPtr t ∉ D → L.mem s (scA t) ≠ 0#u64 → specPtr (L.mem s (scA t)) ∉ D + irqSlot : ∀ i : U64, i.val < 5376 → specPtr (irqSlotA (L.ksb s) i) ∉ D + idleT : ∀ c ∈ Z.Cs, specPtr (L.mem s (L.nodeAt s c layout.node.IDLE_THREAD)) ∉ D + idleC : ∀ c ∈ Z.Cs, specPtr (L.mem s (L.nodeAt s c layout.node.IDLE_SC)) ∉ D + curT : ∀ c ∈ Z.Cs, specPtr (L.mem s (L.nodeAt s c layout.node.CUR_THREAD)) ∉ D + curC : ∀ c ∈ Z.Cs, specPtr (L.mem s (L.nodeAt s c layout.node.CUR_SC)) ∉ D + noCap : UtNoCapA D Z.th (L.mem s) + -- no cap's range key object is removed (childlessness) + nkey : ∀ c ∈ OS, RngKeyA (capA st s c) → specPtr (ptrA (capA st s c)) ∉ D + pmlN : ∀ y, IsSlotA (dropThA D Z.th) y → ∀ q mp asid, capAtA (L.mem s) y = .Pml4 q mp asid → specPtr q ∉ D + eptN : ∀ y, IsSlotA (dropThA D Z.th) y → ∀ q l mp asid va, capAtA (L.mem s) y = .Ept q l mp asid va → + specPtr q ∉ D + hvN : ∀ y, IsSlotA (dropThA D Z.th) y → ∀ q mp asid, capAtA (L.mem s) y = .HighVSpace q mp asid → specPtr q ∉ D + -- closure: scheduler and IPC + spec : UtSpecClosedA D σ + queues : ∀ c ∈ Z.Cs, ∀ q : U64, q.val < 256 → ∀ t ∈ Z.Q c q, specPtr t ∉ D + releases : ∀ c ∈ Z.Cs, ∀ t ∈ Z.R c, specPtr t ∉ D + curW : specPtr (L.curW s) ∉ D + act : ∀ u, σ.schedulerAction = .switchThread (specPtr u) → u ∈ Z.T → specPtr u ∉ D + refills : ∀ q y k, σ.kheap q = some (.schedContext y k) → scActive y = true → y.scRefills ≠ [] + ren : ∀ c ∈ Z.C, specPtr c = m.idleScOf m.curCore → specPtr c ∉ D + live : ∀ t ∈ Z.T, (L.mem s (stA t)).val ≠ 0 → specPtr t ∉ D + recvReply : ∀ t ∈ Z.T, specPtr t ∉ D → (L.mem s (stA t)).val = 3 → L.mem s (tReplyA t) ≠ 0#u64 → + specPtr (L.mem s (tReplyA t)) ∉ D + bound : ∀ n ∈ N, specPtr n ∉ D → L.mem s (nBoundA n) ∈ Z.T → specPtr (L.mem s (nBoundA n)) ∉ D + stackKept : ∀ sc ∈ Z.C, ∀ r ∈ stack sc, (specPtr sc ∉ D ↔ specPtr r ∉ D) + links : ∀ r ∈ Rp, specPtr r ∉ D → ∀ w, (w = L.mem s (rNextA r) ∨ w = L.mem s (rPrevA r)) → w ∈ Rp → + specPtr w ∉ D + yieldTo : ∀ t ∈ Z.T, specPtr t ∉ D → L.mem s (yieldA t) ∈ Z.C → specPtr (L.mem s (yieldA t)) ∉ D + yieldFrom : ∀ c ∈ Z.C, specPtr c ∉ D → ∀ y n, σ.kheap (scNameA m c) = some (.schedContext y n) → + ∀ q, y.scYieldFrom = some q → q ∉ D + bufPlace : ∀ b, BufW (dropBfA D Bf) b → ∀ q len, Z.th q = some (.other len) → q.val ≤ b.val → + b.val < q.val + len → specPtr q ∉ D + bufCover : ∀ t ∈ Z.T, specPtr t ∉ D → ∀ w, + msg.lookup_ipc_buffer st s false t = ok w ∨ msg.lookup_ipc_buffer st s true t = ok w → w ≠ 0#u64 → + ∀ k : U64, k.val < 128 → specPtr (bufW w k) ∉ D + curThread : σ.curThread ∉ D + curSc : σ.curSc ∉ D + idleThread : σ.idleThread ∉ D + irqNode : ∀ irq : Irq, irq.toNat < fl4IrqCount → σ.interruptIrqNode irq ∉ D + idleOf : ∀ c < fl4A64Platform.numNodes, m.idleThreadOf c ∉ D + -- closure: placement and dead objects + whole : ∀ q z, H q = some (.other z) → specPtr q ∈ D → ∀ a : U64, q.val ≤ a.val → a.val < q.val + 2 ^ z → + specPtr a ∈ D + dead : ∀ k q, specPtr q ∉ D → DeadA st (dropSetA D OS) k s q → DeadA st OS k s q + asidPools : ∀ i q, σ.archState.armAsidTable i = some (specPtr q) → specPtr q ∉ D + unmObj : ∀ u ∈ dropSetA D OS, ∀ q, + (∃ asid va, capA st s u = .PageTable q false asid va) ∨ (∃ asid, capA st s u = .Pml4 q false asid) ∨ + (∃ asid va, capA st s u = .Ept q 0#u64 false asid va) ∨ (∃ asid, capA st s u = .HighVSpace q false asid) → + specPtr q ∉ D + poolWords : ∀ i, vspace.asid_table st s i ⦃ pool => pool ≠ 0#u64 → ∀ asid : U64, ∀ q k, H q = some k → + InObj q k (wa pool (core.num.U64.wrapping_mul (asid &&& 511#u64) 8#u64)) → specPtr q ∉ D ⦄ + nullSlots : ∀ sl, AbsSlotA Z.th sl → sl.1 ∈ D → specCapAtA σ sl = some .nullCap + slotOff : ∀ y, IsSlotA (dropThA D Z.th) y → specPtr y ∉ D + own : ∀ y ∈ OS, specPtr y ∉ D → IsSlotA Z.th y → IsSlotA (dropThA D Z.th) y + -- J7's restriction + restr : ∀ Kt, S1aKtUt st OS Kt s → S1aRestrict st H (dropHA D H) OS (dropSetA D OS) Kt s + kinds : KindA st OS s + frames : FrameOK st (dropHA D H) (dropSetA D OS) s + arm : ∀ Ub, S1aArmCol st H Z.Cs OS Z.V Ub s → S1aArmCol st (dropHA D H) Z.Cs (dropSetA D OS) (dropSetA D Z.V) Ub s + omega : ∀ Pt b, VsR (L.ksb s) (s1aKeptPt (dropHA D H) Pt) b → b ∈ H.omega → b ∈ (dropHA D H).omega + vcpuN : ∀ x ∈ dropSetA D OS, ∀ v, capA st s x = .Vcpu v → specPtr v ∉ D + pages : ∀ q, SchedPageA st (dropZA D Z) (dropSetA D OS) s q → PtHG (dropHA D H) {q} + +/-- **What the drop of a RAM untyped needs beyond the record** (module docstring). -/ +structure UtDropC (st : machine.Store S) [L : LawfulStore st] (D : Set ObjRef) (Z : XSetsA) (H : Heap) + (OS Ac : Set U64) (N E Rp : Set U64) (Bf : Set (U64 × Word)) (stack : U64 → List U64) (nA : U64) (s : S) + (σ : State Unit) (m : McState) (p sb fi : U64) : Prop + extends UtDropG st D Z H OS Ac N E Rp Bf stack nA s σ m p sb fi where + -- the retyped untyped, for A64 P1's drops + place : ∃ (Rq Nt : Set U64) (extra : Aeneas.Std.Array obj.Slot 3#usize) (x : U64), + SysPlace st H Z.Cs H.frame Rq Nt OS s extra x ∧ capA st s x = .Untyped p sb fi false ∧ + (∀ G par, TreeA st OS G par s → ∀ y, ¬ Fl4Proofs.P3.Mdb.Desc par y x) + blk : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s + symL : SymL st H Z.Cs s + run : Runnable (L.mem s (stA (L.curW s))) + fiLe : fi.val ≤ 2 ^ sb.val + utZero : UtZeroA st OS s + +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s : S} {a : A} {σ : State Unit} {m : McState} {D : Set ObjRef} {p sb fi : U64} + +theorem utVsFoot_subA {Pp Pp' Tb Tb' Y Y' : Set U64} (e1 : Pp' ⊆ Pp) (e2 : Tb' ⊆ Tb) (e3 : Y' ⊆ Y) {kb w : U64} + (h : VsFootA Pp' Tb' Y' kb w) : VsFootA Pp Tb Y kb w := by + cases h with + | asid hi h => exact .asid hi h + | pool q lo hq h => exact .pool q lo (e1 hq) h + | pt q i hq h => exact .pt q i (e2 hq) h + | high y i hy h => exact .high y i (e3 hy) h + +theorem utSchedPage_subA {Z Z' : XSetsA} {OS OS' : Set U64} {s : S} (e1 : Z'.Pp ⊆ Z.Pp) (e2 : Z'.S1 ⊆ Z.S1) + (e3 : Z'.T2 ⊆ Z.T2) (e4 : Z'.X ⊆ Z.X) (e5 : Z'.Y ⊆ Z.Y) (eo : OS' ⊆ OS) {p : U64} + (h : SchedPageA st Z' OS' s p) : SchedPageA st Z OS s p := by + unfold SchedPageA at h ⊢ + rcases h with h | h | h | h | ⟨r, hr, h⟩ | h | ⟨y, hy, h⟩ | ⟨u, hu, h⟩ + · exact Or.inl (e1 h) + · exact Or.inr (Or.inl (e2 h)) + · exact Or.inr (Or.inr (Or.inl (e3 h))) + · exact Or.inr (Or.inr (Or.inr (Or.inl (e4 h)))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨r, e4 hr, h⟩)))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (e5 h)))))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨y, e5 hy, h⟩)))))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ⟨u, eo hu, h⟩)))))) + +/-- F3–F6 across the drop. `hi`: the removed region holds no idle thread of the `n` cores. -/ +theorem utDrop_sidleA {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} {n : Nat} {D : Set Fl4SpecA64.ObjRef} + (h : SIdleA σ m n) (hi : ∀ c < n, m.idleThreadOf c ∉ D) : + SIdleA (Fl4SpecA64.detype D σ) (dropMA D m) n := by + have kept : ∀ q o, (Fl4SpecA64.detype D σ).kheap q = some o → σ.kheap q = some o := by + intro q o g + change (if q ∈ D then none else σ.kheap q) = some o at g + split at g + case isTrue => cases g + case isFalse => exact g + have vt : ∀ c < n, vtCapA (Fl4SpecA64.detype D σ) (m.idleThreadOf c) = some .nullCap ∧ + vcpuOfA (Fl4SpecA64.detype D σ) (m.idleThreadOf c) = none := fun c hc => + idleVtSameA (detype_keptA σ (hi c hc)) (h.idleSlot c hc) (h.idleBound c hc) + refine ⟨fun c hc => ?_, fun y k hy => h.f4 y k (kept _ _ hy), fun p y k hy he => h.f5 p y k (kept _ _ hy) he, + fun sl => ?_, fun y k hy => h.f7 y k (kept _ _ hy), fun c hc => (vt c hc).1, fun c hc => (vt c hc).2⟩ + · show ∃ x, (Fl4SpecA64.detype D σ).kheap (m.idleThreadOf c) = some (.tcb x) ∧ x.tcbState = .idleThreadState + rw [detype_keptA σ (hi c hc)] + exact h.f3 c hc + · exact ⟨fun c hc e => (h.f6 sl).1 c hc (utSpecCap_dropA e).2, + fun b e => (h.f6 sl).2.1 b (utSpecCap_dropA e).2, + fun a e => (h.f6 sl).2.2 a (utSpecCap_dropA e).2⟩ + +/-- **`vcRun` across the drop** (record edit #4): a retained TCB's VCPU is retained (else it is dead, so unbound, +against `vcSym1`), and a retained VCPU's `RUN_CALLER` is retained (`vcSym2` and `runVcpu` put it in +`TS_RUNNING_VM`, and a thread that is not inactive is retained). -/ +theorem utDrop_vcRunA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (C : UtDropG st D Z H OS Ac N E Rp Bf stack nA s σ m p sb fi) : + VcRunA st (dropZA D Z).T (dropZA D Z).V s := by + -- a dropped object of the record is named by no cap + have dead : ∀ (k : DKindA) (q : U64), (k = .tcb ∨ k = .vcpu) → specPtr q ∈ D → DeadA st OS k s q := by + intro k q hk hD u hu hn + have key : RngKeyA (capA st s u) ∧ ptrA (capA st s u) = q := by + rcases hk with rfl | rfl + · rcases hn with e | ⟨n, e⟩ + · rw [e]; exact ⟨RngKeyA.thread _, okinj ((ptrA_eq _).symm.trans (by unfold capability.Cap.ptr; rfl))⟩ + · rw [e]; exact ⟨RngKeyA.zombie _ _ _, ptrA_zombie _ _ _⟩ + · rw [show capA st s u = .Vcpu q from hn]; exact ⟨RngKeyA.vcpu _, ptrA_vcpu _⟩ + exact (key.2 ▸ C.nkey u hu key.1) hD + have h0 := J.sys.sysl.ly.h0 + have vh : ∀ v ∈ Z.V, H v = some (.other 12) := by + obtain ⟨Pt, Ub, -, -, armc, -⟩ := J.j7 + exact armc.vh + refine ⟨fun t ht => ?_, fun v hv => ?_, fun v hv => ?_, fun t ht => ?_, fun t ht h0 => J.vcRun.disj t ht.1 h0⟩ + · rcases J.vcRun.tcb t ht.1 with e | mem + · exact Or.inl e + by_cases hw : L.mem s (wa t 3664#u64) = 0#u64 + · exact Or.inl hw + refine Or.inr ⟨mem, fun hD => ?_⟩ + have dead : DeadA st OS .vcpu s (L.mem s (wa t 3664#u64)) := by + intro u hu hn + change capA st s u = .Vcpu (L.mem s (wa t 3664#u64)) at hn + by_cases ku : specPtr u ∈ D + · have null : capA st s u = .Null := by + by_cases hs : IsSlotA Z.th u + · exact C.gone u hu hs (fun h => C.slotOff u h ku) + · exact J.j5.2.2 u hu hs + rw [null] at hn + cases hn + · exact C.vcpuN u ⟨hu, ku⟩ _ hn hD + obtain ⟨x, hx, htcb, -⟩ := J.j.2.1.obj _ mem + have none := J.deadExt.vcpu _ mem dead x hx + rw [htcb] at none + have z : L.mem s (L.mem s (wa t 3664#u64)) = 0#u64 := by + unfold optRefA at none + split at none + case isTrue g => exact g + case isFalse => cases none + have e := J.vcSym1 t ht.1 hw + rw [z] at e + have hT : H t = some .tcb := by + have : t ∈ H.tcbs := by rw [J.tcbs]; exact ht.1 + exact this + exact ne0_of_heap h0 hT e.symm + · rcases J.vcRun.vcpu v hv.1 with e | mem + · exact Or.inl e + by_cases hw : L.mem s (wa v 24#u64) = 0#u64 + · exact Or.inl hw + have back := J.vcSym2 v hv.1 hw + have vne : v ≠ 0#u64 := ne0_of_heap h0 (vh v hv.1) + have run := J.runVcpu _ mem (by rw [back]; exact vne) + refine Or.inr ⟨mem, C.live _ mem ?_⟩ + rw [run] + simp [sched.TS_RUNNING_VM] + · rcases J.vcRun.vtcb v hv.1 with e | mem + · exact Or.inl e + by_cases hw : L.mem s v = 0#u64 + · exact Or.inl hw + refine Or.inr ⟨mem, fun hD => ?_⟩ + have z := J.deadExt.tvcpu _ mem (dead .tcb _ (Or.inl rfl) hD) + rw [J.vcBack v hv.1 hw] at z + exact ne0_of_heap h0 (vh v hv.1) z + · rcases J.vcRun.run t ht.1 with e | mem + · exact Or.inl e + by_cases hw : L.mem s (runVcpuA t) = 0#u64 + · exact Or.inl hw + refine Or.inr ⟨mem, fun hD => ?_⟩ + have z := J.deadExt.vcDeadRun _ mem (dead .vcpu _ (Or.inr rfl) hD) + rw [J.runBack t ht.1 hw] at z + have hT : H t = some .tcb := by + have : t ∈ H.tcbs := by rw [J.tcbs]; exact ht.1 + exact this + exact ne0_of_heap h0 hT z + +/-- **`blkQ` across the drop**: the endpoint or notification a retained thread is blocked on is retained (a dead one +has an empty queue, `DeadQ`). -/ +theorem utDrop_blkQA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (C : UtDropG st D Z H OS Ac N E Rp Bf stack nA s σ m p sb fi) : + BlkQueuedA (dropZA D Z).T (dropSetA D E) (dropSetA D N) (L.mem s) (detype D σ) := by + -- a dropped heap object of the record is dead + have gone : ∀ b k, H b = some k → specPtr b ∈ D → DeadQ st H s b := by + intro b k hb hD + have hR : RegionD H p.val (p.val + 2 ^ sb.val) b := by + by_contra g + have e : (H.drop (RegionD H p.val (p.val + 2 ^ sb.val))) b = some k := Heap.drop_eq_some.2 ⟨g, hb⟩ + rw [C.heapB] at e + exact (dropHA_someA.1 e).1 hD + exact C.dropOK.dead b k hb hR + have back : ∀ q o, (detype D σ).kheap q = some o → σ.kheap q = some o := by + intro q o g + exact (detype_someA.1 g).2 + refine ⟨fun t ht x hx e hb => ?_, fun t ht x hx q hb => ?_⟩ + · have hx0 := back _ _ hx + obtain ⟨a, y, hy, mem⟩ := J.blkQ.ep t ht.1 x hx0 e hb + obtain ⟨x', hx', rel⟩ := J.ep.tcb t ht.1 + rw [hx0] at hx' + obtain rfl : x = x' := by + injection hx' with h + injection h + have ee := rel.blk e hb + obtain ⟨x0, hx0e, obj⟩ := J.ep.ep _ a + rw [← ee] at hy + rw [hy] at hx0e + obtain rfl : y = x0 := by + injection hx0e with h + injection h + have kb : specPtr (L.mem s (Notif.tBlockA t)) ∉ D := by + intro hD + have hH := J.utIpc.ep _ a + have z := (gone _ _ hH hD).2.1 hH + rw [epState_eq] at z + cases y with + | idleEP => exact absurd mem List.not_mem_nil + | sendEP l => + have one := obj.1 + rw [z] at one + exact absurd (congrArg UScalar.val one) (by simp) + | recvEP l => + have two := obj.1 + rw [z] at two + exact absurd (congrArg UScalar.val two) (by simp) + refine ⟨⟨a, kb⟩, y, ?_, mem⟩ + rw [← ee, detype_keptA σ kb] + exact hy + · have hx0 := back _ _ hx + obtain ⟨a, y, l, hy, ho, mem⟩ := J.blkQ.ntfn t ht.1 x hx0 q hb + obtain ⟨x', hx', rel⟩ := J.ntfn.tcb t ht.1 + rw [hx0] at hx' + obtain rfl : x = x' := by + injection hx' with h + injection h + have ee := rel.blk q hb + obtain ⟨x0, hx0e, rel0⟩ := J.ntfn.ntfn _ a + rw [← ee] at hy + rw [hy] at hx0e + obtain rfl : y = x0 := by + injection hx0e with h + injection h + have kb : specPtr (L.mem s (Notif.tBlockA t)) ∉ D := by + intro hD + have hH := J.utIpc.ntfn _ a + have z := (gone _ _ hH hD).2.2.1 hH + rw [epState_eq] at z + have obj := rel0.obj + rw [ho] at obj + exact z.1 obj.1 + refine ⟨⟨a, kb⟩, y, l, ?_, ho, mem⟩ + rw [← ee, detype_keptA σ kb] + exact hy + +/-- **The top drop at any removed region** (module docstring): `UtDropG` and A64 P1's `SysL ∧ SymL` over the dropped +heap and slot set. -/ +theorem utDrop_genA [Fact (n ≤ Fl4SpecA64.fl4A64Platform.numNodes)] + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (C : UtDropG st D Z H OS Ac N E Rp Bf stack nA s σ m p sb fi) + (hsys : SysL st (H.drop (RegionD H p.val (p.val + 2 ^ sb.val))) Z.Cs (OffR p.val (p.val + 2 ^ sb.val) OS) ia ie s ∧ + SymL st (H.drop (RegionD H p.val (p.val + 2 ^ sb.val))) Z.Cs s) + : + JStage3A st AR (dropZA D Z) (dropHA D H) (dropSetA D OS) U Ac (dropSetA D Pp) Tb Lv ia ie (dropSetA D N) + (dropSetA D E) (dropSetA D Rp) (dropBfA D Bf) stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a (detype D σ) + (dropMA D m) ∧ + SymL st (dropHA D H) Z.Cs s := by + have hent := utDrop_entA J.sys.ent C.dropOK C.regionIn + have hsq := SchedQueuesInv.drop C.dropOK J.sq + rw [C.heapB, C.slotsB] at hent hsys + rw [C.heapB] at hsq + obtain ⟨eh, ebxi, eos, euser, eksw⟩ := hent + obtain ⟨ssys, ssym⟩ := hsys + have hsub : ∀ y, IsSlotA Z.th y → y ∈ OS := J.j5.2.1 + have hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null := J.j5.2.2 + have htr : TreeA st OS Z.G.chains Z.G.par s := J.j.1.cdt.tree.grow hsub fun y hy hn => hnul y hy hn + have htree := utDrop_treeA (D := D) htr hsub hnul C.gone + have hps : PoolSet st (dropSetA D Pp) s = PoolSet st Pp s := by + ext p + constructor + · rintro ⟨hp, rest⟩ + exact ⟨hp.1, rest⟩ + · rintro ⟨hp, o, h1, h2, h8, hm, hn⟩ + refine ⟨⟨hp, ?_⟩, o, h1, h2, h8, hm, hn⟩ + have := C.tbl o h1 h2 h8 (hm ▸ hn) + rwa [hm] at this + have hvs : VsTyR st (L.ksb s) (PoolSet st (dropSetA D Pp) s) ∅ Tb Lv s := by + rw [hps] + exact J.sys.ent.vs + have hsa := utDrop_slotApA (D := D) J.ap + have hsv := utDrop_slotApVsA (D := D) J.ap + have hth := utDrop_thTA (D := D) (m := m) J.j.1.thT J.j.1.scOffD + have hsets := utDrop_heapSetsA (D := D) J.tcbs J.scs + have hde := utDrop_deadExtA J.deadExt C.dead + have hat : JAtZA (L := L) AR (dropZA D Z) s a (detype D σ) (dropMA D m) := + { st := utDrop_stateRelA J.j.1.st + cdt := utDrop_cdtRelA J.j.1.cdt htree C.cdt C.par + sch := utDrop_schedRelA J.j.1.sch C.anchor + ar := J.j.1.ar + place := utDrop_placeA J.j.1.place C.irqSlot + irq := utDrop_irqRelA J.j.1.irq + rest := utDrop_schedRestA J.j.1.rest C.anchor C.tcbSc + thT := hth.1 + csN := J.j.1.csN + idleT := utDrop_nodeRefsA _ J.j.1.idleT C.idleT + idleC := utDrop_nodeRefsA _ J.j.1.idleC C.idleC + curT := utDrop_nodeRefsA _ J.j.1.curT C.curT + cscC := utDrop_nodeRefsA _ J.j.1.cscC C.curC + ksFit := J.j.1.ksFit + scOffD := hth.2 + cv := utDrop_capValidA J.j.1.cv C.noCap + pml := utDrop_pmlA J.j.1.pml C.pmlN + vs := utDrop_vsRelA J.j.1.vs C.vsClos + ext := utDrop_extVsRelA J.j.1.ext C.vsClos } + have j7' : J7A st (J7R2A st ar nA) (dropZA D Z) (dropHA D H) (dropSetA D OS) s (detype D σ) (dropMA D m) := + utDrop_J7A J.j7 rfl C.restr C.kinds C.frames C.arm C.omega C.vcpuN C.pages + refine ⟨?_, ssym⟩ + exact + { sys := ⟨⟨eh, ebxi, eos, euser, hvs, eksw⟩, ssys⟩ + j := ⟨hat, utDrop_vcpuCoreA J.j.2.1 C.noCap, utDrop_stage2CoreA J.j.2.2.1 C.eptN, + utDrop_highCoreA J.j.2.2.2 C.hvN⟩ + tcbs := hsets.1 + scs := hsets.2 + sq := hsq + ap := { sca := hsa.1, irqSt := hsa.2.1, vcAl := hsa.2.2.1, vc := hsa.2.2.2, vs := hsv.1, ext := hsv.2 } + pp := by rw [← J.pp]; rfl + s1 := fun q hq => J.s1 hq.1 + ko := utDrop_koA J.ko + ptH := by + have h := utDrop_ptHA J.ptH C.whole + refine fun q hq => h q ?_ + rcases hq with m' | m' + · exact ⟨Or.inl m'.1, m'.2⟩ + · exact ⟨Or.inr m'.1, m'.2⟩ + tabInj := utDrop_tabInjA J.tabInj + poolOff := utDrop_poolOffA J.poolOff C.poolWords + objAlloc := utDrop_objAllocA J.objAlloc + idleAnchor := utDrop_idleAnchorA J.idleAnchor + vsVc := utDrop_vsVcA J.vsVc + sr := utDrop_schedSRA J.sr C.anchor C.spec C.tcbSc C.irqSlot C.queues C.releases C.curW C.curC C.act C.refills + scCov := utDrop_scCovA J.scCov (utDrop_scRenKeptA C.ren) + scSize := utDrop_scSizeA J.scSize + ep := utDrop_epStateA J.ep (fun _ h => h.1) C.live C.recvReply + ntfn := utDrop_ntfnStateA J.ntfn C.live C.bound + reply := utDrop_replyStackA J.reply C.anchor C.stackKept C.links + bufCover := utDrop_bufCoverA J.bufCover C.bufCover + bufNative := utDrop_bufNativeA J.bufNative + iplaced := utDrop_iplacedA J.iplaced (fun _ h => h.1) + ipcPlace := utDrop_ipcPlaceA J.ipcPlace C.bufPlace + idleNoFault := utDrop_idleNoFaultA J.idleNoFault C.idleOf + mcp := utDrop_mcpA J.mcp + yieldRel := utDrop_yieldA J.yieldRel C.anchor C.yieldTo C.yieldFrom + sinv := utDrop_sinvA J.sinv C.spec C.anchor C.curThread C.curSc C.idleThread C.irqNode + setsInH := utDrop_setsInHA J.setsInH C.whole + utIpc := utDrop_utIpcA J.utIpc + frBase := fun y hy z hz => J.frBase y hy.1 z hz.1 + devFr := fun y hy q size rights mapped ept asid va hc k hk => by + obtain ⟨kq, hq⟩ := dropHA_someA.1 hk + obtain ⟨h1, dev, pg, h2⟩ := J.devFr y hy.1 q size rights mapped ept asid va hc k hq + refine ⟨h1, dev, pg, ?_⟩ + show (if specPtr q ∈ D then none else σ.kheap (specPtr q)) = _ + rw [ite_eq_right kq] + exact h2 + finCov := by + have kept : ∀ q o, (detype D σ).kheap q = some o → q ∉ D ∧ σ.kheap q = some o := by + intro q o h + change (if q ∈ D then none else σ.kheap q) = some o at h + split at h + case isTrue => cases h + case isFalse g => exact ⟨g, h⟩ + have th : ∀ q k, q ∉ D → Z.th (fl4Ptr q) = some k → dropThA D Z.th (fl4Ptr q) = some k := + fun q k g h => dropThA_someA.2 ⟨by rw [specPtr_fl4Ptr]; exact g, h⟩ + refine ⟨fun q r cs h w => ?_, fun q y h => ?_⟩ + · obtain ⟨g, h'⟩ := kept q _ h + exact th q _ g (J.finCov.cnode q r cs h' w) + · obtain ⟨g, h'⟩ := kept q _ h + exact th q _ g (J.finCov.tcb q y h') + idleAll := ⟨J.idleAll.tcb, J.idleAll.sc, J.idleAll.spec⟩ + ipcHeapCov := + { ntfn := fun q h => let ⟨k, h'⟩ := dropHA_someA.1 h; ⟨J.ipcHeapCov.ntfn q h', k⟩ + ep := fun q h => let ⟨k, h'⟩ := dropHA_someA.1 h; ⟨J.ipcHeapCov.ep q h', k⟩ + reply := fun q h => let ⟨k, h'⟩ := dropHA_someA.1 h; ⟨J.ipcHeapCov.reply q h', k⟩ } + zombieNoVcpu := fun u hu => J.zombieNoVcpu u hu.1 + scCpu := fun c hc => J.scCpu c hc.1 + scNZ := fun c hc => J.scNZ c hc.1 + vcRun := utDrop_vcRunA J C + heapWin := fun q k h => J.heapWin q k (dropHA_someA.1 h).2 + coresAll := J.coresAll + ptCapIn := fun y hy q asid va hc => by + have yO := hsub y (utIsSlot_dropA hy) + have kq : specPtr q ∉ D := by + have g := C.nkey y yO (by rw [show capA st s y = .PageTable q true asid va from hc]; exact RngKeyA.pt _ _ _ _) + rwa [show capA st s y = .PageTable q true asid va from hc, + show ptrA (.PageTable q true asid va) = q from + okinj ((ptrA_eq _).symm.trans (by unfold capability.Cap.ptr; rfl))] at g + exact (J.ptCapIn y (utIsSlot_dropA hy) q asid va hc).imp (fun g => ⟨⟨g.1, kq⟩, g.2⟩) (fun g => ⟨g, kq⟩) + symL := ssym + fhOK := fun t ht => by + have yO := hsub _ (isSlot_fhA ((J.sr.thT t).2 ht.1)) + refine (J.fhOK t ht.1).imp id fun ⟨ep, b0, b1, b2, b3, b4, e, he⟩ => ⟨ep, b0, b1, b2, b3, b4, e, ?_⟩ + have rk : RngKeyA (.Endpoint ep b0 b1 b2 b3 b4) := + ⟨okinj ((physA_eq _).symm.trans (by unfold objects.cap_is_physical; rfl)), rfl, rfl⟩ + have g := C.nkey _ yO (by rw [e]; exact rk) + rw [e, show ptrA (.Endpoint ep b0 b1 b2 b3 b4) = ep from + okinj ((ptrA_eq _).symm.trans (by unfold capability.Cap.ptr; rfl))] at g + exact dropHA_someA.2 ⟨g, he⟩ + zwf := fun y hy q zt k hc => by + have kq : specPtr q ∉ D := by + have g := C.nkey y (hsub y (utIsSlot_dropA hy)) (by rw [hc]; exact RngKeyA.zombie _ _ _) + rwa [hc, ptrA_zombie] at g + exact (J.zwf y (utIsSlot_dropA hy) q zt k hc).imp + (fun g => ⟨g.1, dropThA_someA.2 ⟨kq, g.2.1⟩, g.2.2⟩) (fun g => ⟨g.1, dropThA_someA.2 ⟨kq, g.2.1⟩, g.2.2⟩) + schedValid := by + have act : ∀ q, q ∉ D → ScActiveAtA σ q → ScActiveAtA (detype D σ) q := fun q h ⟨y, k, e, g⟩ => + ⟨y, k, by rw [detype_keptA σ h]; exact e, g⟩ + refine ⟨fun d q t hm => ?_, fun e1 e2 => ?_, fun t x sc e1 e2 e3 => ?_, fun t hm => ?_⟩ + · have tD := C.spec.rq d q t hm + obtain ⟨⟨x, sc, e, es, g⟩, nr⟩ := J.schedValid.rq d q t hm + exact ⟨⟨x, sc, by rw [detype_keptA σ tD]; exact e, es, act _ (C.spec.tcbSc t x sc tD e es) g⟩, nr⟩ + · obtain ⟨⟨x, sc, e, es, g⟩, nr⟩ := J.schedValid.act e1 e2 + exact ⟨⟨x, sc, (detype_keptA σ C.curThread).trans e, es, + act _ (C.spec.tcbSc _ x sc C.curThread e es) g⟩, nr⟩ + · obtain ⟨tD, e⟩ := detype_someA.1 e1 + exact act _ (C.spec.tcbSc t x sc tD e e3) (J.schedValid.blk t x sc e e2 e3) + · have tD := C.spec.rls t hm + obtain ⟨x, sc, e, es, g⟩ := J.schedValid.rls t hm + exact ⟨x, sc, by rw [detype_keptA σ tD]; exact e, es, act _ (C.spec.tcbSc t x sc tD e es) g⟩ + utWin := fun u hu => J.utWin u hu.1 + devWin := fun u hu => J.devWin u hu.1 + vcBack := fun v hv h => J.vcBack v hv.1 h + runBack := fun t ht h => J.runBack t ht.1 h + vcSnap := ⟨fun v hv x hx => J.vcSnap.snap v hv.1 x (detype_someA.1 hx).2, + fun v hv x hx => J.vcSnap.mask v hv.1 x (detype_someA.1 hx).2⟩ + vcOS := fun b ⟨v, hv, r⟩ ⟨y, hy, g⟩ => J.vcOS b ⟨v, hv.1, r⟩ ⟨y, hy.1, g⟩ + userFr := fun y hy p size rights mapped ept asid va hc => by + obtain ⟨bits, hbits⟩ : ∃ bits, size.bits = ok bits := by cases size <;> exact ⟨_, rfl⟩ + have kp := (dropHA_someA.1 (ssys.sys.fr y hy p size rights mapped ept asid va hc bits hbits)).1 + exact (detype_keptA σ kp).trans (J.userFr y hy.1 p size rights mapped ept asid va hc) + frTh := fun y hy p size rights x1 x2 x3 x4 bits hc hb => by + have kp := (dropHA_someA.1 (ssys.sys.fr y hy p size rights x1 x2 x3 x4 hc bits hb)).1 + exact dropThA_someA.2 ⟨kp, J.frTh y hy.1 p size rights x1 x2 x3 x4 bits hc hb⟩ + bfSpec := fun bp h => J.bfSpec bp h.1 + vcSym1 := fun t ht h => J.vcSym1 t ht.1 h + runVcpu := fun t ht h => J.runVcpu t ht.1 h + vcSym2 := fun v hv h => J.vcSym2 v hv.1 h + blkQ := utDrop_blkQA J C + capTy := fun u hu => by + have ph : ∀ c : capability.Cap, objects.cap_is_physical c = ok true → isUtA c = false → isFrA c = false → + RngKeyA c := fun c h a b => ⟨okinj ((physA_eq c).symm.trans h), a, b⟩ + have kept : ∀ (c : capability.Cap), capA st s u = c → RngKeyA c → ∀ q, capability.Cap.ptr c = ok q → + ∀ k, H q = some k → dropHA D H q = some k := fun c e r q hq k h => by + have eq : ptrA c = q := okinj ((ptrA_eq c).symm.trans hq) + have kq := C.nkey u hu.1 (by rw [e]; exact r) + rw [e, eq] at kq + exact dropHA_someA.2 ⟨kq, h⟩ + refine ⟨fun q b x y z w hc => ?_, fun q b x y hc => ?_, fun q g hc => ?_, fun q sz hc => ?_, + fun cpu hc => (J.capTy u hu.1).2.2.2.2 cpu hc⟩ + · exact kept _ hc (ph _ (by unfold objects.cap_is_physical; rfl) rfl rfl) _ (by unfold capability.Cap.ptr; rfl) _ ((J.capTy u hu.1).1 q b x y z w hc) + · exact kept _ hc (ph _ (by unfold objects.cap_is_physical; rfl) rfl rfl) _ (by unfold capability.Cap.ptr; rfl) _ ((J.capTy u hu.1).2.1 q b x y hc) + · exact kept _ hc (ph _ (by unfold objects.cap_is_physical; rfl) rfl rfl) _ (by unfold capability.Cap.ptr; rfl) _ ((J.capTy u hu.1).2.2.1 q g hc) + · exact kept _ hc (RngKeyA.sc _ _) _ (by unfold capability.Cap.ptr; rfl) _ ((J.capTy u hu.1).2.2.2.1 q sz hc) + zombieTcb := fun u hu q k hc => by + have h := J.zombieTcb u hu.1 q k hc + have kq : specPtr q ∉ D := by + have := C.nkey u hu.1 (by rw [hc]; exact RngKeyA.zombie _ _ _) + rwa [hc, ptrA_zombie] at this + exact (show dropHA D H q = some .tcb from dropHA_someA.2 ⟨kq, h⟩) + bufOff := + { os := fun b ⟨bp, hbp, e⟩ ⟨y, hy, g⟩ => J.bufOff.os b ⟨bp, hbp.1, e⟩ ⟨y, hy.1, g⟩ + vs := fun b ⟨bp, hbp, e⟩ h => J.bufOff.vs b ⟨bp, hbp.1, e⟩ + (utVsFoot_subA (Pp := Z.Pp) (Tb := Z.S1) (Y := Z.Y) (utDropSet_subA _) (utDropSet_subA _) + (utDropSet_subA _) h) + page := fun b ⟨bp, hbp, e⟩ q hq => J.bufOff.page b ⟨bp, hbp.1, e⟩ q + (utSchedPage_subA (st := st) (Z := Z) (Z' := dropZA D Z) (OS := OS) (OS' := dropSetA D OS) (s := s) + (utDropSet_subA _) (utDropSet_subA _) (utDropSet_subA _) (utDropSet_subA _) (utDropSet_subA _) + (utDropSet_subA _) hq) + vcw := fun b ⟨bp, hbp, e⟩ v hv => J.bufOff.vcw b ⟨bp, hbp.1, e⟩ v hv.1 + pin := fun bp hbp => (J.bufOff.pin bp hbp.1).keepF fun t ht hO _ _ r x1 x2 x3 x4 e => by + have hs : IsSlotA Z.th (sbWordA t) := + ⟨t, Or.inr ⟨(J.j.1.thT t).2 ht, 2, by simp [tcbSlotsA], (bufSlot_eqA t).symm⟩⟩ + have hs' : IsSlotA (dropThA D Z.th) (sbWordA t) := by + by_contra h + have := C.gone _ hO hs h + rw [e] at this + cases this + have kt : specPtr t ∉ D := by + obtain ⟨q, k, hq, h1, h2⟩ := ut_ownerA J.j.1.place (fun q k h => (dropThA_someA.1 h).2) hs' + have ht' : Z.th t = some .tcb := (J.j.1.thT t).2 ht + have tfit := J.j.1.place.fit t _ ht' + have : q = t := by + by_contra ne + have a := J.j.1.place.apart q t k .tcb (dropThA_someA.1 hq).2 ht' ne + have v : (sbWordA t).val = t.val + 64 := by + rw [sbWordA_eq]; exact (wa_valP _ _).trans (Nat.mod_eq_of_lt (by + simp only [ObjKindA.size] at tfit; simp; omega)) + have z : (ObjKindA.tcb).size = 4096 := rfl + omega + exact this ▸ (dropThA_someA.1 hq).1 + exact ⟨⟨ht, kt⟩, ⟨hO, C.slotOff _ hs'⟩, r, x1, x2, x3, x4, e⟩ } + deadExt := + { vcpu := hde.1 + reply := hde.2.1 + replyTcb := hde.2.2 + asid := utDrop_deadAsidA J.deadExt.asid C.asidPools C.dead + tvcpu := utDrop_deadTVcpuA J.deadExt.tvcpu C.dead + vcDeadRun := fun v hv hd => J.deadExt.vcDeadRun v hv.1 (C.dead _ v hv.2 hd) } + specFree := utDrop_specFreeA J.specFree + utBlk := utDrop_utBlkA J.utBlk + unmPt := utDrop_unmPtA J.unmPt C.unmObj + tabS1 := fun q hq => utDrop_tabOffA (J.tabS1 q hq.1) + tabExt := utDrop_extTabOffA J.tabExt + specZero := utDrop_specZeroA J.specZero + thPos := utDrop_thPosA J.thPos + origLive := utDrop_origLiveA J.origLive J.nullOrig C.nullSlots + nullOrig := utDrop_nullOrigA J.nullOrig + sidle := utDrop_sidleA J.sidle (fun c hc => C.idleOf c (lt_of_lt_of_le hc (Fact.out))) + curVcpuNone := J.curVcpuNone + domOK := { J.domOK with } + ipiZero := J.ipiZero + ipcSorted := (J.ipcSorted.mono (Set.image_mono (show dropSetA D E ⊆ E from fun _ h => h.1)) + (Set.image_mono (show dropSetA D N ⊆ N from fun _ h => h.1))).of_q + (QKeepA.of_back (fun _ _ h => (detype_someA.1 h).2) ⟨rfl, rfl, rfl, rfl⟩) + activeReplyScs := J.activeReplyScs.of_q (QKeepA.of_back (fun _ _ h => (detype_someA.1 h).2) ⟨rfl, rfl, rfl, rfl⟩) + bufVs := fun b ⟨bp, hbp, e⟩ => by + have eP : utVsPtA st (dropSetA D Pp) Tb Lv s = utVsPtA st Pp Tb Lv s := by + unfold utVsPtA + rw [hps] + rw [eP] + exact J.bufVs b ⟨bp, hbp.1, e⟩ + j5 := utDrop_J5A J.j5 C.slotOff C.own _ _ + j7 := j7' + ipcTh := ⟨fun q h => dropThA_someA.2 ⟨h.2, J.ipcTh.1 q h.1⟩, + fun q h => dropThA_someA.2 ⟨h.2, J.ipcTh.2.1 q h.1⟩, fun q h => dropThA_someA.2 ⟨h.2, J.ipcTh.2.2 q h.1⟩⟩ + vsOk := utVsOk_of_J7A j7' hvs + bufAl := fun t ht y hk => by + change (if specPtr t ∈ D then none else σ.kheap (specPtr t)) = some (.tcb y) at hk + split at hk + · cases hk + exact J.bufAl t ht.1 y hk + zombieBnd := fun y hy => J.zombieBnd y hy.1 + devUtOff := by + have eJ : ∀ q bytes, JointObjectA (dropZA D Z) (dropHA D H) q bytes → JointObjectA Z H q bytes := by + rintro q bytes (⟨e, h⟩ | ⟨e, h⟩ | ⟨kind, h, e⟩ | ⟨k, h, e⟩) + · refine Or.inl ⟨e, ?_⟩ + rcases h with h | h | h | h | h + · exact Or.inl h.1 + · exact Or.inr (Or.inl h.1) + · exact Or.inr (Or.inr (Or.inl h.1)) + · exact Or.inr (Or.inr (Or.inr (Or.inl h.1))) + · exact Or.inr (Or.inr (Or.inr (Or.inr h))) + · exact Or.inr (Or.inl ⟨e, h.imp (fun h => h.1) (fun h => h.1)⟩) + · exact Or.inr (Or.inr (Or.inl ⟨kind, (dropThA_someA.1 h).2, e⟩)) + · exact Or.inr (Or.inr (Or.inr ⟨k, (dropHA_someA.1 h).2, e⟩)) + exact ⟨fun u hu q b fi hc p bytes hj => J.devUtOff.obj u hu.1 q b fi hc p bytes (eJ p bytes hj), + fun u hu q b fi hc => J.devUtOff.ks u hu.1 q b fi hc, + fun u hu q b fi hc => J.devUtOff.idle u hu.1 q b fi hc, + fun u hu q b fi hc v hv q' b' fi' hc' => J.devUtOff.sep u hu.1 q b fi hc v hv.1 q' b' fi' hc'⟩ } + +/-- **The top drop of a RAM untyped** (module docstring). -/ +theorem utDrop_topA [Fact (n ≤ Fl4SpecA64.fl4A64Platform.numNodes)] + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (C : UtDropC st D Z H OS Ac N E Rp Bf stack nA s σ m p sb fi) : + JStage3A st AR (dropZA D Z) (dropHA D H) (dropSetA D OS) U Ac (dropSetA D Pp) Tb Lv ia ie (dropSetA D N) + (dropSetA D E) (dropSetA D Rp) (dropBfA D Bf) stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a (detype D σ) + (dropMA D m) ∧ + SymL st (dropHA D H) Z.Cs s := by + obtain ⟨Rq, Nt, extra, x, P, hcx, hnd⟩ := C.place + have hsys := utDrop_syslA P C.blk J.sys.sysl C.symL C.dropOK C.run hcx hnd C.fiLe C.utZero + exact utDrop_genA J C.toUtDropG ⟨hsys.1, hsys.2.1⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtDropVs2A.lean b/proofs/Fl4ProofsA64/P3/UtDropVs2A.lean new file mode 100644 index 000000000..8c28b728c --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtDropVs2A.lean @@ -0,0 +1,250 @@ +/- +P3 (aarch64, A64-P3 Untyped W2): **the dead-object drop for the remaining VSpace-side and J12 fields** of the +unified record: `pp`, `s1`, `ptH`, `tabInj`, `poolOff`, `vsVc`, `SlotApA.vs` / `.ext`, `DeadExtA.asid`, +`UnmPtSpecA`, `tabS1`, `tabExt`, `utIpc`, `origLive`, `nullOrig`. + +FL4's memory is unchanged; the record moves to the filtered sets, `dropHA D H`, `detype D σ`, `dropMA D m`. Each lemma +takes only the closure facts it needs, "no retained object refers into `D`"; their producers are not part of this +module. `origLive` uses `nullOrig` and "the removed objects' slots hold the null cap". No named premise. +-/ +import Fl4ProofsA64.P3.UtDropVsA +import Fl4ProofsA64.P3.VsTabJA +import Fl4ProofsA64.P3.FinVsXA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +variable {D : Set Fl4SpecA64.ObjRef} {S : Type} {st : machine.Store S} [L : LawfulStore st] + +theorem utSlotWA_dropA {th : TypedHeapA} {y : U64} (h : SlotWA (dropThA D th) y) : SlotWA th y := by + rcases h with ⟨p, h⟩ | ⟨p, r, hp, rest⟩ + · refine Or.inl ⟨p, ?_⟩ + rcases h with ⟨r, hr, rest⟩ | ⟨ht, rest⟩ + · exact Or.inl ⟨r, (dropThA_someA.1 hr).2, rest⟩ + · exact Or.inr ⟨(dropThA_someA.1 ht).2, rest⟩ + · exact Or.inr ⟨p, r, (dropThA_someA.1 hp).2, rest⟩ + +theorem utVsFoot_dropA {Pp Tb Y : Set U64} {ksb b : U64} + (h : VsFootA (dropSetA D Pp) (dropSetA D Tb) (dropSetA D Y) ksb b) : VsFootA Pp Tb Y ksb b := by + cases h with + | asid hi h => exact .asid hi h + | pool p lo hp h => exact .pool p lo hp.1 h + | pt p i hp h => exact .pt p i hp.1 h + | high y i hy h => exact .high y i hy.1 h + +theorem utExtFoot_dropA {X Y T2 : Set U64} {b : U64} + (h : ExtFootE (dropSetA D X) (dropSetA D Y) (dropSetA D T2) b) : ExtFootE X Y T2 b := by + cases h with + | s2lo r i hr hi => exact .s2lo r i hr.1 hi + | s2hi r i hr hi => exact .s2hi r i hr.1 hi + | s2pt t i ht hi => exact .s2pt t i ht.1 hi + | wl0 y hy => exact .wl0 y hy.1 + | wlo y i hy hi => exact .wlo y i hy.1 hi + +/-- The footprint over the retained sets is part of the old one. -/ +theorem utFootA_dropA {Z : XSetsA} {s : S} {w : U64} (h : FootA (L := L) (dropZA D Z) s w) : + FootA (L := L) Z s w := by + cases h with + | slot y w hy hw => exact .slot y w (utSlotWA_dropA hy) hw + | nslot w h => + refine .nslot w ?_ + cases h with + | node c w hc hw => exact .node c w hc hw + | cur w hw => exact .cur w hw + | tcb t w ht hw => exact .tcb t w ht.1 hw + | sc c hc => exact .sc c hc.1 + | vcpu v w hv hw => exact .vcpu v w hv.1 hw + | irq i hi => exact .irq i hi + | cur2 w hw => exact .cur2 w hw + | glob w hw => exact .glob w hw + | tcb2 t w ht hw => exact .tcb2 t w ht.1 hw + | sc2 c hc => exact .sc2 c hc.1 + | vs w hw => exact .vs w (utVsFoot_dropA hw) + | ext w hw => exact .ext w (utExtFoot_dropA hw) + +theorem utRelPA_dropA {Z : XSetsA} {m : Fl4SpecA64.McState} {p : Fl4SpecA64.ObjRef} + (h : RelPA (dropZA D Z) (dropMA D m) p) : RelPA Z m p := by + rcases h with ⟨q, h, e⟩ | ⟨t, h, e⟩ | ⟨c, h, e⟩ | ⟨v, h, e⟩ | ⟨r, h, e⟩ | ⟨r, h, e⟩ | ⟨q, h, e⟩ | ⟨q, h, e⟩ | + ⟨q, h, e⟩ + · refine Or.inl ⟨q, ?_, e⟩ + intro hn + apply h + show dropThA D Z.th q = none + unfold dropThA + split_ifs + · rfl + · exact hn + · exact Or.inr (Or.inl ⟨t, h.1, e⟩) + · exact Or.inr (Or.inr (Or.inl ⟨c, h.1, e⟩)) + · exact Or.inr (Or.inr (Or.inr (Or.inl ⟨v, h.1, e⟩))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨r, h.1, e⟩)))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨r, h.1, e⟩))))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨q, h.1, e⟩)))))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨q, h.1, e⟩))))))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ⟨q, h.1, e⟩))))))) + +/-- `pp` and `s1`. -/ +theorem utDrop_ppS1A {Z : XSetsA} {Pp Tb : Set U64} (hp : Z.Pp = Pp) (hs : Z.S1 ⊆ Tb) : + (dropZA D Z).Pp = dropSetA D Pp ∧ (dropZA D Z).S1 ⊆ dropSetA D Tb := + ⟨by rw [← hp]; rfl, fun _ h => ⟨hs h.1, h.2⟩⟩ + +theorem utDrop_ptHA {H : Heap} {P : Set U64} (h : PtHA H P) + (whole : ∀ q z, H q = some (.other z) → specPtr q ∈ D → ∀ p : U64, q.val ≤ p.val → p.val < q.val + 2 ^ z → + specPtr p ∈ D) : PtHA (dropHA D H) (dropSetA D P) := by + intro p hp + obtain ⟨q, z, hq, h1, h2⟩ := h p hp.1 + exact ⟨q, z, dropHA_someA.2 ⟨fun hd => hp.2 (whole q z hq hd p h1 (by omega)), hq⟩, h1, h2⟩ + +theorem utDrop_tabInjA {σ : Fl4SpecA64.State Unit} + (h : ∀ i j p, σ.archState.armAsidTable i = some p → σ.archState.armAsidTable j = some p → i = j) : + ∀ i j p, (Fl4SpecA64.detype D σ).archState.armAsidTable i = some p → + (Fl4SpecA64.detype D σ).archState.armAsidTable j = some p → i = j := h + +/-- `poolOff`. `hw`: the objects holding a live pool's words are retained. -/ +theorem utDrop_poolOffA {H : Heap} {Cs OS : Set U64} {s : S} (h : PoolOff st H Cs OS s) + (hw : ∀ i, vspace.asid_table st s i ⦃ pool => pool ≠ 0#u64 → ∀ asid : U64, ∀ p k, H p = some k → + InObj p k (wa pool (core.num.U64.wrapping_mul (asid &&& 511#u64) 8#u64)) → specPtr p ∉ D ⦄) : + PoolOff st (dropHA D H) Cs (dropSetA D OS) s := by + intro i + refine WP.spec_mono (WP.spec_and (h i) (hw i)) fun pool ⟨a, b⟩ hn asid => ?_ + obtain ⟨al, om, sg, os, ks⟩ := a hn asid + obtain ⟨-, p, k, hk, hkind, hin⟩ := om + refine ⟨al, ⟨al, p, k, dropHA_someA.2 ⟨b hn asid p k hk hin, hk⟩, hkind, hin⟩, fun m => sg ?_, + fun y hy => os y hy.1, ks⟩ + obtain ⟨a', p', k', hk', rest⟩ := m + exact ⟨a', p', k', (dropHA_someA.1 hk').2, rest⟩ + +theorem utDrop_vsVcA {Z : XSetsA} {kb : U64} (h : ∀ b, VsFootA Z.Pp Z.S1 Z.Y kb b → ¬ VcW Z.V b) : + ∀ b, VsFootA (dropZA D Z).Pp (dropZA D Z).S1 (dropZA D Z).Y kb b → ¬ VcW (dropZA D Z).V b := by + intro b hb hv + obtain ⟨v, hv, rest⟩ := hv + exact h b (utVsFoot_dropA hb) ⟨v, hv.1, rest⟩ + +/-- `SlotApA`'s two VSpace fields. -/ +theorem utDrop_slotApVsA {Z : XSetsA} {H : Heap} {OS : Set U64} {kb : U64} (h : SlotApA Z H OS kb) : + (∀ b, VsFootA (dropZA D Z).Pp (dropZA D Z).S1 (dropZA D Z).Y kb b → + ¬ InOS (dropSetA D OS) b ∧ ∀ t ∈ (dropHA D H).tcbs, b.val < t.val ∨ t.val + 4096 ≤ b.val) ∧ + ∀ b, ExtFootE (dropZA D Z).X (dropZA D Z).Y (dropZA D Z).T2 b → + ¬ InOS (dropSetA D OS) b ∧ ∀ t ∈ (dropHA D H).tcbs, b.val < t.val ∨ t.val + 4096 ≤ b.val := by + have tc : ∀ t ∈ (dropHA D H).tcbs, t ∈ H.tcbs := fun t ht => by + rw [dropHA_tcbsA] at ht + exact ht.1 + refine ⟨fun b hb => ?_, fun b hb => ?_⟩ + · obtain ⟨a, c⟩ := h.vs b (utVsFoot_dropA hb) + exact ⟨fun ⟨y, hy, r⟩ => a ⟨y, hy.1, r⟩, fun t ht => c t (tc t ht)⟩ + · obtain ⟨a, c⟩ := h.ext b (utExtFoot_dropA hb) + exact ⟨fun ⟨y, hy, r⟩ => a ⟨y, hy.1, r⟩, fun t ht => c t (tc t ht)⟩ + +/-- `DeadExtA.asid`. `hdead`: a retained object dead after the drop was dead before. -/ +theorem utDrop_deadAsidA {OS : Set U64} {s : S} {σ : Fl4SpecA64.State Unit} + (h : ∀ i p, σ.archState.armAsidTable i = some (specPtr p) → ¬ DeadA st OS .pool s p) + (hpool : ∀ i p, σ.archState.armAsidTable i = some (specPtr p) → specPtr p ∉ D) + (hdead : ∀ k p, specPtr p ∉ D → DeadA st (dropSetA D OS) k s p → DeadA st OS k s p) : + ∀ i p, (Fl4SpecA64.detype D σ).archState.armAsidTable i = some (specPtr p) → + ¬ DeadA st (dropSetA D OS) .pool s p := + fun i p ht hd => h i p ht (hdead _ p (hpool i p ht) hd) + +theorem utDrop_tabOffA {Z : XSetsA} {m : Fl4SpecA64.McState} {s : S} {p : U64} (T : TabOffA (L := L) Z m s p) : + TabOffA (L := L) (dropZA D Z) (dropMA D m) s p := by + refine ⟨fun i hi h => T.mem i hi ?_, fun q hq => T.obj q ?_⟩ + · have e : ({ dropZA D Z with S1 := ∅ } : XSetsA) = dropZA D { Z with S1 := ∅ } := by + have : dropSetA D (∅ : Set U64) = ∅ := Set.eq_empty_of_forall_notMem fun _ h => h.1 + simp [dropZA, this] + rw [e] at h + exact utFootA_dropA h + · have e : ({ dropZA D Z with S1 := ∅ } : XSetsA) = dropZA D { Z with S1 := ∅ } := by + have : dropSetA D (∅ : Set U64) = ∅ := Set.eq_empty_of_forall_notMem fun _ h => h.1 + simp [dropZA, this] + rw [e] at hq + exact utRelPA_dropA hq + +theorem utDrop_extTabOffA {Z : XSetsA} {m : Fl4SpecA64.McState} {s : S} (T : ExtTabOffA (L := L) Z m s) : + ExtTabOffA (L := L) (dropZA D Z) (dropMA D m) s := by + have e : extZA (dropZA D Z) = dropZA D (extZA Z) := by + have : dropSetA D (∅ : Set U64) = ∅ := Set.eq_empty_of_forall_notMem fun _ h => h.1 + simp [extZA, dropZA, this] + refine ⟨fun q hq i hi h => ?_, fun p hp h => ?_⟩ + · rw [e] at h + refine T.mem q ?_ i hi (utFootA_dropA h) + rcases hq with hq | ⟨y, hy, ey⟩ + · refine Or.inl ?_ + rcases hq with m | ⟨r, hr, er⟩ + · exact Or.inl m.1 + · exact Or.inr ⟨r, hr.1, er⟩ + · exact Or.inr ⟨y, hy.1, ey⟩ + · rw [e] at h + refine T.obj p ?_ (utRelPA_dropA h) + exact hp.imp And.left (Or.imp And.left And.left) + +/-- `UnmPtSpecA`. `hobj`: the object a retained unmapped cap names is retained. -/ +theorem utDrop_unmPtA {Z : XSetsA} {m : Fl4SpecA64.McState} {OS : Set U64} {s : S} {σ : Fl4SpecA64.State Unit} + (h : UnmPtSpecA st Z m OS s σ) + (hobj : ∀ u ∈ dropSetA D OS, ∀ p, + (∃ asid va, capA st s u = .PageTable p false asid va) ∨ (∃ asid, capA st s u = .Pml4 p false asid) ∨ + (∃ asid va, capA st s u = .Ept p 0#u64 false asid va) ∨ + (∃ asid, capA st s u = .HighVSpace p false asid) → specPtr p ∉ D) : + UnmPtSpecA st (dropZA D Z) (dropMA D m) (dropSetA D OS) s (Fl4SpecA64.detype D σ) := by + have tab : ∀ ty p, specPtr p ∉ D → UnmTabA st Z m s σ ty p → + UnmTabA st (dropZA D Z) (dropMA D m) s (Fl4SpecA64.detype D σ) ty p := by + intro ty p hp U + obtain ⟨t, h1, h2, h3⟩ := U.spec + exact ⟨U.inv, ⟨t, (detype_keptA σ hp).trans h1, h2, h3⟩, utDrop_tabOffA U.off⟩ + refine ⟨fun u hu p asid va hc => ?_, fun u hu p asid hc => ?_, fun u hu p asid va hc => ?_, + fun u hu p asid hc => ?_⟩ + · exact tab _ _ (hobj u hu p (Or.inl ⟨_, _, hc⟩)) (h.pt u hu.1 p asid va hc) + · exact tab _ _ (hobj u hu p (Or.inr (Or.inl ⟨_, hc⟩))) (h.vs u hu.1 p asid hc) + · have U := h.s2 u hu.1 p asid va hc + obtain ⟨es, h1, h2⟩ := U.spec + exact ⟨U.inv, es, (detype_keptA σ (hobj u hu p (Or.inr (Or.inr (Or.inl ⟨_, _, hc⟩))))).trans h1, h2⟩ + · have U := h.hv u hu.1 p asid hc + obtain ⟨l1, h1, h2⟩ := U.spec + exact ⟨U.inv, l1, (detype_keptA σ (hobj u hu p (Or.inr (Or.inr (Or.inr ⟨_, hc⟩))))).trans h1, h2⟩ + +theorem utDrop_utIpcA {H : Heap} {N E Rp : Set U64} (h : UtIpcHeapA H N E Rp) : + UtIpcHeapA (dropHA D H) (dropSetA D N) (dropSetA D E) (dropSetA D Rp) := + ⟨fun n hn => dropHA_someA.2 ⟨hn.2, h.ntfn n hn.1⟩, fun e he => dropHA_someA.2 ⟨he.2, h.ep e he.1⟩, + fun r hr => dropHA_someA.2 ⟨hr.2, h.reply r hr.1⟩⟩ + +theorem utSpecCap_dropA {σ : Fl4SpecA64.State Unit} {sl : Fl4SpecA64.CslotPtr} {c : Fl4SpecA64.Cap} + (h : specCapAtA (Fl4SpecA64.detype D σ) sl = some c) : sl.1 ∉ D ∧ specCapAtA σ sl = some c := by + unfold specCapAtA at h ⊢ + by_cases hd : sl.1 ∈ D + · have e : (Fl4SpecA64.detype D σ).kheap sl.1 = none := by + show (if sl.1 ∈ D then none else σ.kheap sl.1) = none + rw [ite_eq_left hd] + rw [e] at h + cases h + · rw [detype_keptA σ hd] at h + exact ⟨hd, h⟩ + +theorem utDrop_nullOrigA {σ : Fl4SpecA64.State Unit} + (h : ∀ sl, specCapAtA σ sl = some .nullCap → σ.isOriginalCap sl = false) : + ∀ sl, specCapAtA (Fl4SpecA64.detype D σ) sl = some .nullCap → + (Fl4SpecA64.detype D σ).isOriginalCap sl = false := + fun sl hs => h sl (utSpecCap_dropA hs).2 + +/-- `origLive`. `hnull`: the slots of removed objects hold the null cap. -/ +theorem utDrop_origLiveA {th : TypedHeapA} {σ : Fl4SpecA64.State Unit} + (h : ∀ sl, σ.isOriginalCap sl = true → AbsSlotA th sl) + (hn : ∀ sl, specCapAtA σ sl = some .nullCap → σ.isOriginalCap sl = false) + (hnull : ∀ sl, AbsSlotA th sl → sl.1 ∈ D → specCapAtA σ sl = some .nullCap) : + ∀ sl, (Fl4SpecA64.detype D σ).isOriginalCap sl = true → AbsSlotA (dropThA D th) sl := by + intro sl ho + have ho' : σ.isOriginalCap sl = true := ho + have a := h sl ho' + have nd : sl.1 ∉ D := fun hd => by + rw [hn sl (hnull sl a hd)] at ho' + cases ho' + obtain ⟨p, e, k⟩ := a + have keep : ∀ k', th p = some k' → dropThA D th p = some k' := fun k' hk => + dropThA_someA.2 ⟨e ▸ nd, hk⟩ + exact ⟨p, e, k.imp (keep _) fun ⟨ht, rest⟩ => ⟨keep _ ht, rest⟩⟩ + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtDropVsA.lean b/proofs/Fl4ProofsA64/P3/UtDropVsA.lean new file mode 100644 index 000000000..2b319aaef --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtDropVsA.lean @@ -0,0 +1,197 @@ +/- +P3 (aarch64, A64-P3 Untyped W2): **the dead-object drop for the VSpace relations** (Design 1A), the parts of +`UtDropA` that waited for `vsrel-1a`. + +* `utVsPath_dropA`, `utS2Reach_dropA`: reachability over the retained sets is reachability over the old ones. +* **`utDrop_vsRelA`**, **`utDrop_extVsRelA`**: `VsRelA` / `ExtVsRelA` at the filtered sets and `detype D σ`, FL4's + memory unchanged. +* **`utDrop_setsInHA`**: `SetsInHA` at the dropped heap. + +Each takes only **`UtVsClosA`**, "nothing reachable from the ASID table refers into `D`" (Design 1A asks nothing of +entries in unreachable pools and tables), and for `SetsInHA` that `D` does not cut an object of the heap. Their +producers (A64 P1 / S1A reachability) are not part of this module. No named premise. +-/ +import Fl4ProofsA64.P3.UtDropA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Classical + +local notation "wa" => core.num.U64.wrapping_add + +variable {D : Set Fl4SpecA64.ObjRef} + +theorem utDropSet_memA {X : Set U64} {p : U64} : p ∈ dropSetA D X ↔ p ∈ X ∧ specPtr p ∉ D := Iff.rfl + +theorem utDropSet_subA (X : Set U64) : dropSetA D X ⊆ X := fun _ h => h.1 + +/-- **[closure]** Nothing reachable from the ASID table refers into `D`: the table's pools, the roots held by an +active pool, and the targets of the table descriptors of every reachable Stage-1 / high-window and Stage-2 table. -/ +structure UtVsClosA (D : Set Fl4SpecA64.ObjRef) (Pp Tb Y X T2 : Set U64) (Sh2 : U64 → Nat) (mem : U64 → U64) + (ksb : U64) (σ : Fl4SpecA64.State Unit) : Prop where + tpool : ∀ hi : U64, hi.val < 128 → mem (atAF ksb hi) ≠ 0#u64 → specPtr (mem (atAF ksb hi)) ∉ D + ent : ∀ p ∈ Pp, specPtr p ∉ D → ActivePoolA mem ksb p → ∀ lo : U64, lo.val < 512 → mem (peAF p lo) ≠ 0#u64 → + specPtr (untagA (mem (peAF p lo))) ∉ D + tab : ∀ tag sh q, (tag = 0#u64 ∨ tag = 2#u64) → VsPathA Pp Tb Y mem ksb tag sh q → 12 < sh → + (q ∈ Tb ∨ ∃ y ∈ Y, q = wa y 4096#u64) → ∀ i : U64, i.val < 512 → dBitA (mem (peAF q i)) 0 = true → + dBitA (mem (peAF q i)) 1 = true → dTabA (mem (peAF q i)) ∈ Tb → specPtr (dTabA (mem (peAF q i))) ∉ D + s2root : ∀ r, ActiveS2RootA Pp X σ r → specPtr r ∉ D → ∀ i : U64, i.val < 1024 → + i.val < Fl4SpecA64.s2Geom.topMask + 1 → + dBitA (mem (peAF r i)) 0 = true → dBitA (mem (peAF r i)) 1 = true → dTabA (mem (peAF r i)) ∈ T2 → + specPtr (dTabA (mem (peAF r i))) ∉ D + s2pt : ∀ t, S2ReachA Pp X T2 Sh2 mem σ t → t ∈ T2 → 12 < Sh2 t → ∀ i : U64, i.val < 512 → dBitA (mem (peAF t i)) 0 = true → + dBitA (mem (peAF t i)) 1 = true → dTabA (mem (peAF t i)) ∈ T2 → specPtr (dTabA (mem (peAF t i))) ∉ D + +variable {Pp Tb Y X T2 : Set U64} {Sh2 Lv : U64 → Nat} {mem : U64 → U64} {ksb : U64} {σ : Fl4SpecA64.State Unit} + +theorem utActiveAsid_dropA {asid raw : U64} (h : ActiveAsidRootA mem ksb (dropSetA D Pp) asid raw) : + ActiveAsidRootA mem ksb Pp asid raw := by + obtain ⟨hi, lo, h1, h2, h3, pool, hp, rest⟩ := h + exact ⟨hi, lo, h1, h2, h3, pool, hp.1, rest⟩ + +theorem utVsPath_dropA {tag : U64} {sh : Nat} {q : U64} + (h : VsPathA (dropSetA D Pp) (dropSetA D Tb) (dropSetA D Y) mem ksb tag sh q) : + VsPathA Pp Tb Y mem ksb tag sh q := by + induction h with + | native ha ht hm => exact .native (utActiveAsid_dropA ha) ht hm.1 + | high ha ht hm => exact .high (utActiveAsid_dropA ha) ht hm.1 + | child _ hm h12 hi h3 ih => + exact .child ih (hm.imp And.left fun ⟨y, hy, e⟩ => ⟨y, hy.1, e⟩) h12 hi h3 + +theorem utVsReach_dropA {q : U64} (h : VsReachA (dropSetA D Pp) (dropSetA D Tb) (dropSetA D Y) mem ksb q) : + VsReachA Pp Tb Y mem ksb q := by + obtain ⟨tag, sh, ht, hp⟩ := h + exact ⟨tag, sh, ht, utVsPath_dropA hp⟩ + +/-- A descriptor relation over `Tb` is one over the retained tables when its table target is retained. -/ +theorem utPteRel_dropA {k : Nat} {w : U64} {pte : Fl4SpecA64.PTE} (h : PteRelA Tb Lv k w pte) + (hk : k < 3 → dBitA w 0 = true → dBitA w 1 = true → dTabA w ∈ Tb → specPtr (dTabA w) ∉ D) : + PteRelA (dropSetA D Tb) Lv k w pte := by + cases pte with + | invalidPTE => exact h + | pageTablePTE ppn => + obtain ⟨h1, h2, h3, h4, h5, h6, h7⟩ := h + exact ⟨h1, h2, h3, h4, h5, ⟨h6, hk h1 h2 h3 h6⟩, h7⟩ + | pagePTE _ _ _ _ => exact h + +theorem utDrop_vsRelA (h : VsRelA Pp Tb Lv X Y mem ksb σ) (C : UtVsClosA D Pp Tb Y X T2 Sh2 mem ksb σ) : + VsRelA (dropSetA D Pp) (dropSetA D Tb) Lv (dropSetA D X) (dropSetA D Y) mem ksb (Fl4SpecA64.detype D σ) := by + refine + { table := fun hi hh => h.table hi hh + tmem := fun hi hh hn => ⟨h.tmem hi hh hn, C.tpool hi hh hn⟩ + pool := fun p hp => ?_ + kind := fun p hp ha lo hl => ?_ + asid0 := h.asid0 + pt := fun p hp => ?_ + high := fun y hy => ?_ + lv := fun p hp => h.lv p hp.1 + al := fun p hp => h.al p hp.1 + pal := fun p hp => h.pal p hp.1 + yal := fun y hy => h.yal y hy.1 + ytb := fun y hy m => h.ytb y hy.1 m.1 } + · obtain ⟨f, hk, hf⟩ := h.pool p hp.1 + exact ⟨f, (detype_keptA σ hp.2).trans hk, hf⟩ + · have k := h.kind p hp.1 ha lo hl + have e := C.ent p hp.1 hp.2 ha lo hl + by_cases h0 : mem (peAF p lo) = 0#u64 + · exact Or.inl h0 + rcases k with k | ⟨a, b, c⟩ | ⟨a, b⟩ | ⟨a, b⟩ + · exact Or.inl k + · exact Or.inr (Or.inl ⟨a, ⟨b, e h0⟩, c⟩) + · exact Or.inr (Or.inr (Or.inl ⟨a, b, e h0⟩)) + · exact Or.inr (Or.inr (Or.inr ⟨a, b, e h0⟩)) + · obtain ⟨t, hk, ht, he⟩ := h.pt p hp.1 + refine ⟨t, (detype_keptA σ hp.2).trans hk, ht, fun hr i hi => ?_⟩ + have hr' := utVsReach_dropA hr + refine utPteRel_dropA (he hr' i hi) fun hk3 b0 b1 m => ?_ + obtain ⟨tag, sh, htag, path⟩ := hr' + rcases (h.path_props path).2 with ⟨-, -, hsh⟩ | ⟨-, hsh, -⟩ + · exact C.tab tag sh p htag path (by omega) (Or.inl hp.1) i hi b0 b1 m + · exact C.tab tag sh p htag path (by omega) (Or.inl hp.1) i hi b0 b1 m + · obtain ⟨l1, hk, he⟩ := h.high y hy.1 + refine ⟨l1, (detype_keptA σ hy.2).trans hk, fun ha i hi => ?_⟩ + obtain ⟨asid, raw, h1, h2, h3⟩ := ha + have h1' := utActiveAsid_dropA h1 + have hr : VsPathA Pp Tb Y mem ksb 2#u64 30 (wa y 4096#u64) := by + have p : VsPathA Pp Tb Y mem ksb 2#u64 30 (wa (untagA raw) 4096#u64) := + .high h1' h2 (by rw [h3]; exact hy.1) + rw [h3] at p + exact p + exact utPteRel_dropA (he ⟨asid, raw, h1', h2, h3⟩ i hi) fun _ b0 b1 m => + C.tab _ _ _ (Or.inr rfl) hr (by decide) (Or.inr ⟨y, hy.1, rfl⟩) i hi b0 b1 m + +theorem utActiveS2_dropA {r : U64} + (h : ActiveS2RootA (dropSetA D Pp) (dropSetA D X) (Fl4SpecA64.detype D σ) r) : ActiveS2RootA Pp X σ r := by + obtain ⟨hr, asid, p, f, ha, hp, htab, hk, hf⟩ := h + exact ⟨hr.1, asid, p, f, ha, hp.1, htab, (detype_someA.1 hk).2, hf⟩ + +theorem utS2Reach_dropA {t : U64} + (h : S2ReachA (dropSetA D Pp) (dropSetA D X) (dropSetA D T2) Sh2 mem (Fl4SpecA64.detype D σ) t) : + S2ReachA Pp X T2 Sh2 mem σ t := by + induction h with + | root ha => exact .root (utActiveS2_dropA ha) + | stepRoot ha hi h3 hm => exact .stepRoot (utActiveS2_dropA ha) hi h3 hm.1 + | stepPt _ ht h12 hi h3 hm ih => exact .stepPt ih ht.1 h12 hi h3 hm.1 + +theorem utDrop_extVsRelA (h : ExtVsRelA Pp Tb X Y T2 Sh2 mem σ) (C : UtVsClosA D Pp Tb Y X T2 Sh2 mem ksb σ) : + ExtVsRelA (dropSetA D Pp) (dropSetA D Tb) (dropSetA D X) (dropSetA D Y) (dropSetA D T2) Sh2 mem + (Fl4SpecA64.detype D σ) := by + have ptSub : ∀ q, q ∈ VsPtA (dropSetA D Pp) (dropSetA D Tb) (dropSetA D Y) → q ∈ VsPtA Pp Tb Y := fun q m => by + rcases m with m | m | ⟨y, hy, e⟩ + · exact Or.inl m.1 + · exact Or.inr (Or.inl m.1) + · exact Or.inr (Or.inr ⟨y, hy.1, e⟩) + refine + { s2root := fun r hr => ?_ + s2pt := fun t ht => ?_ + tsh := fun t ht => h.tsh t ht.1 + wl0 := fun y hy => h.wl0 y hy.1 + wlo := fun y hy => h.wlo y hy.1 + xal := fun r hr => h.xal r hr.1 + tal := fun t ht => h.tal t ht.1 + pgd := fun q hq => ?_ + t2x := fun t ht r hr => h.t2x t ht.1 r hr.1 + ypg := fun y hy m => h.ypg y hy.1 (ptSub _ m) + ypp := fun y hy m => h.ypp y hy.1 m.1 } + · obtain ⟨es, hk, he⟩ := h.s2root r hr.1 + refine ⟨es, (detype_keptA σ hr.2).trans hk, fun ha i hi htop => ?_⟩ + have ha' := utActiveS2_dropA ha + obtain ⟨d, at'⟩ := he ha' i hi htop + exact ⟨d.mono fun b0 b1 _ g => ⟨⟨g.1, C.s2root r ha' hr.2 i hi htop b0 b1 g.1⟩, g.2⟩, at'⟩ + · obtain ⟨pt, hk, hty, he⟩ := h.s2pt t ht.1 + refine ⟨pt, (detype_keptA σ ht.2).trans hk, hty, fun hr i hi => ?_⟩ + have hr' := utS2Reach_dropA hr + obtain ⟨d, at'⟩ := he hr' i hi + refine ⟨d.mono fun b0 b1 h12 g => ⟨⟨g.1, C.s2pt t hr' ht.1 h12 i hi b0 b1 g.1⟩, g.2⟩, at'⟩ + · have hq' : q ∈ S2PgE X T2 := by + rcases hq with m | ⟨r, hr, e⟩ + · exact Or.inl m.1 + · exact Or.inr ⟨r, hr.1, e⟩ + obtain ⟨a, b⟩ := h.pgd q hq' + exact ⟨fun m => a (ptSub _ m), fun m => b m.1⟩ + +/-- **`SetsInHA` after the drop**, when `D` does not cut a heap object. -/ +theorem utDrop_setsInHA {Z : XSetsA} {H : Heap} (h : SetsInHA Z H) + (whole : ∀ q z, H q = some (.other z) → specPtr q ∈ D → ∀ p : U64, q.val ≤ p.val → p.val < q.val + 2 ^ z → + specPtr p ∈ D) : + SetsInHA (dropZA D Z) (dropHA D H) := by + have keep : ∀ q z (p : U64), H q = some (.other z) → q.val ≤ p.val → p.val < q.val + 2 ^ z → specPtr p ∉ D → + dropHA D H q = some (.other z) := fun q z p hq h1 h2 hp => + dropHA_someA.2 ⟨fun hd => hp (whole q z hq hd p h1 h2), hq⟩ + refine { pages := fun p hp => ?_, roots := fun p hp => ?_ } + · have hp' : p ∈ Z.Pp ∪ Z.S1 ∪ Z.T2 ∪ Z.V ∧ specPtr p ∉ D := by + rcases hp with ((m | m) | m) | m + · exact ⟨Or.inl (Or.inl (Or.inl m.1)), m.2⟩ + · exact ⟨Or.inl (Or.inl (Or.inr m.1)), m.2⟩ + · exact ⟨Or.inl (Or.inr m.1), m.2⟩ + · exact ⟨Or.inr m.1, m.2⟩ + obtain ⟨q, z, hq, h1, h2⟩ := h.pages p hp'.1 + exact ⟨q, z, keep q z p hq h1 (by omega) hp'.2, h1, h2⟩ + · have hp' : p ∈ Z.X ∪ Z.Y ∧ specPtr p ∉ D := by + rcases hp with m | m + · exact ⟨Or.inl m.1, m.2⟩ + · exact ⟨Or.inr m.1, m.2⟩ + obtain ⟨q, z, hq, h1, h2⟩ := h.roots p hp'.1 + exact ⟨q, z, keep q z p hq h1 (by omega) hp'.2, h1, h2⟩ + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtFrA.lean b/proofs/Fl4ProofsA64/P3/UtFrA.lean new file mode 100644 index 000000000..a55d3aecf --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtFrA.lean @@ -0,0 +1,435 @@ +/- +P3 (aarch64, A64-P3 Untyped W2, item 1, non-VSpace part): **the record across the reset's chunk steps** — zeroing a +chunk of the region, lowering the untyped's free index and the preemption point. + +After the drop (`UtDropA`) nothing of the record is left in the region, so a chunk step is: FL4 writes that miss the +record's words, and a spec step that changes at most one CNode (`UtCnStepA`: reflexive for the chunk clear, whose +bytes are already zero; the cap rewrite otherwise). + +* `UtRdA`: the words the record reads beyond `JAtZAX`'s footprint (`utFr_*`: one lemma per memory-reading field, + given writes off `UtRdA`). +* `utCn_*`: the spec-only fields across `UtCnStepA`. +* `utFi_*`: the fields that read caps, across a free-index change (`UtFiA`), with the region empty. +* `PPHypUtA`: the preemption point's loop hook (contract J8, x86 `PPHypDW`'s form). + +* `utFr_syslA`, `utFr_entA`, `utFr_sqA`: the P1 invariants (`sys.sysl` by A64 P1's `SysL.dropR` with nothing left to + drop). + +`JAtZAX` itself is `JEntA.writesIn` (chunk clear) and `JAtZAX.of_setcapA` (cap rewrite). The VSpace fields +(`sys.ent.vs` included, a hypothesis of `utFr_entA`) wait for `vsrel-1a`. + +Premises (hypotheses of the lemmas that need them): +* [J] `PPHypUtA`: the preemption point keeps the loop invariant, with a spec outcome of the same kind (producer + MCS-18/19, contract J8). +* [J] `EPlaced`, `NPlaced`: the IPC ghosts' placement. +* [J] `hI : SpecIrqA σ'`: the rewritten CNode is not an IRQ node. +* [J] `SymL` (closure item G, as `utDrop_syslA`). +-/ +import Fl4ProofsA64.P3.Stage3SchedPresA +import Fl4ProofsA64.P3.Stage3A +import Fl4ProofsA64.P3.IpcScGlueA +import Fl4ProofsA64.P3.ScYieldRelA +import Fl4ProofsA64.P3.BufNativeFrA +import Fl4ProofsA64.P1.MdbDropR +import Fl4ProofsA64.P3.ScStableA +import Fl4ProofsA64.P3.EpSendRecvA +import Fl4ProofsA64.P3.DelSpecA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +open Fl4SpecA64 Fl4Spec + +/-- **A spec step that changes at most CNodes**: globals, IRQ and ASID tables, user memory and every non-CNode object +kept. -/ +structure UtCnStepA (σ σ' : State Unit) : Prop extends SpecGlobA σ σ' where + kh : ∀ p, σ'.kheap p = σ.kheap p ∨ + ∃ r cs cs', σ.kheap p = some (.cnode r cs) ∧ σ'.kheap p = some (.cnode r cs') + um : σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory + +theorem UtCnStepA.refl (σ : State Unit) : UtCnStepA σ σ := ⟨SpecGlobA.refl σ, fun _ => Or.inl rfl, rfl⟩ + +/-- Rewriting one CNode's slots. -/ +theorem UtCnStepA.set {σ : State Unit} {p : ObjRef} {r : Nat} {cs cs' : CnodeContents} + (h : σ.kheap p = some (.cnode r cs)) : UtCnStepA σ (setObjA σ p (.cnode r cs')) := by + refine ⟨⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, ⟨rfl, rfl, rfl, rfl⟩⟩, fun q => ?_, rfl⟩ + by_cases e : q = p + · subst e; exact Or.inr ⟨r, cs, cs', h, Function.update_self ..⟩ + · exact Or.inl (Function.update_of_ne e ..) + +theorem UtCnStepA.keep {σ σ' : State Unit} (H : UtCnStepA σ σ') {p : ObjRef} {ko : KernelObject} + (h : σ.kheap p = some ko) (hk : ∀ r cs, ko ≠ .cnode r cs) : σ'.kheap p = some ko := by + rcases H.kh p with e | ⟨r, cs, cs', e1, -⟩ + · rw [e]; exact h + · rw [h] at e1; exact absurd (Option.some.inj e1) (hk r cs) + +theorem UtCnStepA.back {σ σ' : State Unit} (H : UtCnStepA σ σ') {p : ObjRef} {ko : KernelObject} + (h : σ'.kheap p = some ko) (hk : ∀ r cs, ko ≠ .cnode r cs) : σ.kheap p = some ko := by + rcases H.kh p with e | ⟨r, cs, cs', -, e2⟩ + · rw [← e]; exact h + · rw [h] at e2; exact absurd (Option.some.inj e2) (hk r cs') + +/-- (record edit #8) A CNode-only step keeps the queue invariants' ingredients. -/ +theorem UtCnStepA.qkeep {σ σ' : State Unit} (H : UtCnStepA σ σ') : QKeepA σ σ' := by + have hb : ∀ p o, (∀ r cs, o ≠ .cnode r cs) → σ'.kheap p = some o → σ.kheap p = some o := fun p o ho h => by + rcases H.kh p with e | ⟨r, cs, cs', e1, e2⟩ + · rw [← e]; exact h + · rw [h] at e2; exact absurd (Option.some.inj e2) (ho r cs') + exact ⟨fun t y hy => ⟨y, hb t _ (fun _ _ e => by cases e) hy, rfl⟩, + fun p x hp => ⟨x, hb p _ (fun _ _ e => by cases e) hp, List.Sublist.refl _⟩, + fun p x hp => ⟨x, hb p _ (fun _ _ e => by cases e) hp, List.Sublist.refl _⟩, + fun p y k hp => ⟨y, k, hb p _ (fun _ _ e => by cases e) hp, fun r => ⟨r, id⟩⟩, + fun p y hp => ⟨y, hb p _ (fun _ _ e => by cases e) hp, id⟩, H.domk⟩ + +theorem UtCnStepA.sched {σ σ' : State Unit} (H : UtCnStepA σ σ') : SchedSameA σ σ' := by + refine ⟨fun p ko hko => ?_, H.cur, H.idle, H.csc, H.rq, H.rls, H.time, H.cons⟩ + have hk : ∀ r cs, ko ≠ .cnode r cs := fun r cs e => by + rcases hko with ⟨x, e'⟩ | ⟨y, k, e'⟩ <;> rw [e'] at e <;> cases e + exact ⟨fun h => H.back h hk, fun h => H.keep h hk⟩ + +theorem UtCnStepA.sstep {σ σ' : State Unit} (H : UtCnStepA σ σ') : SStepA σ σ' := by + refine ⟨⟨H.cur, H.csc, H.idle, H.time, H.cons, H.repr, H.dom, H.domT⟩, H.act, H.rq, H.rls, ?_, ?_, ?_, ?_, ?_⟩ + · intro p x n h; rw [h]; exact H.keep h (fun _ _ e => by cases e) + · intro p x h; exact ⟨x, H.keep h (fun _ _ e => by cases e), rfl, rfl, rfl⟩ + · intro p x n h; rw [h]; exact H.back h (fun _ _ e => by cases e) + · intro p y h; exact ⟨y, H.back h (fun _ _ e => by cases e)⟩ + · intro p y h; exact ⟨y, H.back h (fun _ _ e => by cases e), rfl⟩ + +theorem UtCnStepA.noBlock {σ σ' : State Unit} (H : UtCnStepA σ σ') : NoBlockESc σ σ' := by + intro p x y hx hy + have := (H.keep hx (fun _ _ e => by cases e)).symm.trans hy + cases this + exact Or.inl ⟨rfl, id⟩ + +/-! ## Spec-only fields -/ + +theorem utCn_scCovA {C : Set U64} {m : McState} {σ σ' : State Unit} (h : ScCovA C m σ) (H : UtCnStepA σ σ') : + ScCovA C m σ' := fun p x n hk => h p x n (H.back hk (fun _ _ e => by cases e)) + +theorem utCn_scSizeA {C : Set U64} {m : McState} {σ σ' : State Unit} {f : U64 → Nat} + (h : ∀ c ∈ C, ∀ x n, σ.kheap (scNameA m c) = some (.schedContext x n) → n + minSchedContextBits ≤ f c ∧ + (NotIdleScA m (scNameA m c) → n + minSchedContextBits = f c)) + (H : UtCnStepA σ σ') : + ∀ c ∈ C, ∀ x n, σ'.kheap (scNameA m c) = some (.schedContext x n) → n + minSchedContextBits ≤ f c ∧ + (NotIdleScA m (scNameA m c) → n + minSchedContextBits = f c) := + fun c hc x n hk => h c hc x n (H.back hk (fun _ _ e => by cases e)) + +theorem utCn_idleNoFaultA {m : McState} {σ σ' : State Unit} (h : IdleNoFaultA m σ) (H : UtCnStepA σ σ') : + IdleNoFaultA m σ' := fun c hc => by + obtain ⟨x, hx, hf⟩ := h c hc + exact ⟨x, H.keep hx (fun _ _ e => by cases e), hf⟩ + +theorem utCn_sinvA {m : McState} {n : Nat} {σ σ' : State Unit} (h : SInvXA σ m n) + (H : UtCnStepA σ σ') (hI : SpecIrqA σ') : SInvXA σ' m n := + h.of_spec H.sched H.act hI + +/-! ## Memory-reading fields -/ + +/-- **The words the record reads beyond `JAtZAX`'s footprint.** -/ +def UtRdA (st : machine.Store S) [L : LawfulStore st] (Z : XSetsA) (N E Rp : Set U64) (Bf : Set (U64 × Word)) + (m : McState) (s : S) (σ : State Unit) (a : U64) : Prop := + SReadA st Z m s σ a ∨ EFoot Z.T E Rp Bf a ∨ NFoot N Z.T a ∨ (∃ r ∈ Rp, a = rNextA r ∨ a = rPrevA r) ∨ + ∃ t ∈ Z.T, a = mcpA t ∨ a = yieldA t ∨ a = sbWordA t ∨ a = wa (sbWordA t) 8#u64 ∨ a = wa t 3512#u64 ∨ + a = wa t 3664#u64 ∨ a = flagsA t + +/-- Writes off the record's words keep each of them. -/ +theorem ut_rd_keepA {Z : XSetsA} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {m : McState} {s s' : S} + {σ : State Unit} {W : U64 → Prop} (hw : WritesIn st s s' W) + (hW : ∀ a, W a → ¬ UtRdA st Z N E Rp Bf m s σ a) {a : U64} (hal : Aligned a) + (hr : UtRdA st Z N E Rp Bf m s σ a) : L.mem s' a = L.mem s a := + hw.mem_eq hal (fun h => hW a h hr) + +section fr +variable {Z : XSetsA} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {m : McState} {s s' : S} {σ σ' : State Unit} + {W : U64 → Prop} (hw : WritesIn st s s' W) (hW : ∀ a, W a → ¬ UtRdA st Z N E Rp Bf m s σ a) + (H : UtCnStepA σ σ') +include hw hW H + +theorem utFr_schedSRA {κ : U64 → U64} (h : SchedSRA st Z m κ s σ) : SchedSRA st Z m κ s' σ' := + h.stable hw (fun a ha hr => hW a ha (Or.inl hr)) H.sstep H.noBlock + +theorem utFr_epA (h : EpStateRel Z.T E Rp Bf (L.mem s) σ) (hP : EPlaced Z.T E Rp Bf) : + EpStateRel Z.T E Rp Bf (L.mem s') σ' := by + refine h.frame hP (fun a ha hf => ut_rd_keepA hw hW ha (Or.inr (Or.inl hf))) + ⟨fun p x hk => H.keep hk (fun _ _ e => by cases e), + fun p x hk => ⟨x, H.keep hk (fun _ _ e => by cases e), rfl⟩, + fun p x hk => ⟨x, H.keep hk (fun _ _ e => by cases e), rfl, rfl, rfl, rfl, rfl⟩, H.um⟩ + +theorem utFr_ntfnA (h : NtfnStateRel N Z.T (L.mem s) σ) (hP : NPlaced Z.T N) : + NtfnStateRel N Z.T (L.mem s') σ' := by + refine h.frame hP (fun a ha hf => ut_rd_keepA hw hW ha (Or.inr (Or.inr (Or.inl hf)))) + ⟨fun p x hk => H.keep hk (fun _ _ e => by cases e), + fun p x hk => ⟨x, H.keep hk (fun _ _ e => by cases e), rfl, rfl, rfl⟩⟩ + +theorem utFr_replyA {C : Set U64} {scRef : U64 → ObjRef} {stack : U64 → List U64} + (h : ReplyStackRelA (L.mem s) Rp C σ scRef stack) (hal : ∀ r ∈ Rp, Aligned (rNextA r) ∧ Aligned (rPrevA r)) : + ReplyStackRelA (L.mem s') Rp C σ' scRef stack := by + refine h.frame (fun r hr => ?_) (fun c _ x n hk => ⟨x, n, H.keep hk (fun _ _ e => by cases e), rfl⟩) + (fun r _ x hk => ⟨x, H.back hk (fun _ _ e => by cases e), rfl⟩) + have k : ∀ a, (a = rNextA r ∨ a = rPrevA r) → UtRdA st Z N E Rp Bf m s σ a := + fun a ha => Or.inr (Or.inr (Or.inr (Or.inl ⟨r, hr, ha⟩))) + exact ⟨ut_rd_keepA hw hW (hal r hr).1 (k _ (Or.inl rfl)), ut_rd_keepA hw hW (hal r hr).2 (k _ (Or.inr rfl))⟩ + +set_option linter.unusedSectionVars false in +/-- A TCB word of `UtRdA`'s last disjunct is kept. -/ +theorem ut_rd_tcbA {t a : U64} (ht : t ∈ Z.T) (hal : Aligned a) + (ha : a = mcpA t ∨ a = yieldA t ∨ a = sbWordA t ∨ a = wa (sbWordA t) 8#u64 ∨ a = wa t 3512#u64 ∨ + a = wa t 3664#u64 ∨ a = flagsA t) : L.mem s' a = L.mem s a := + ut_rd_keepA hw hW hal (Or.inr (Or.inr (Or.inr (Or.inr ⟨t, ht, ha⟩)))) + +theorem ut_ibSameA {t : U64} (ht : t ∈ Z.T) (h1 : Aligned (wa t 3512#u64)) (h2 : Aligned (sbWordA t)) + (h3 : Aligned (wa (sbWordA t) 8#u64)) : IbSame (L := L) s s' t := + ⟨ut_rd_tcbA hw hW H ht h1 (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl rfl))))), + ut_rd_tcbA hw hW H ht h2 (Or.inr (Or.inr (Or.inl rfl))), + ut_rd_tcbA hw hW H ht h3 (Or.inr (Or.inr (Or.inr (Or.inl rfl))))⟩ + +theorem utFr_bufCoverA (h : BufCover st Z.T Bf s σ) + (hal : ∀ t ∈ Z.T, Aligned (wa t 3512#u64) ∧ Aligned (sbWordA t) ∧ Aligned (wa (sbWordA t) 8#u64)) : + BufCover st Z.T Bf s' σ' := by + intro t ht x b sb w hx hb hl + obtain ⟨h1, h2, h3⟩ := hal t ht + rw [lookup_ipc_buffer_sameA (ut_ibSameA hw hW H ht h1 h2 h3)] at hl + exact h t ht x b sb w (H.back hx (fun _ _ e => by cases e)) hb hl + +theorem utFr_bufNativeA (h : BufNativeA Z.T (L.mem s)) + (hal : ∀ t ∈ Z.T, Aligned (sbWordA t) ∧ Aligned (wa (sbWordA t) 8#u64)) : BufNativeA Z.T (L.mem s') := + h.frame fun t ht => ⟨ut_rd_tcbA hw hW H ht (hal t ht).1 (Or.inr (Or.inr (Or.inl rfl))), + ut_rd_tcbA hw hW H ht (hal t ht).2 (Or.inr (Or.inr (Or.inr (Or.inl rfl))))⟩ + +theorem utFr_mcpA (h : McpRelA Z.T m (L.mem s) σ) (hal : ∀ t ∈ Z.T, Aligned (mcpA t) ∧ Aligned (flagsA t)) : + McpRelA Z.T m (L.mem s') σ' := fun t ht => by + obtain ⟨x, hx, he, hf⟩ := h t ht + refine ⟨x, H.keep hx (fun _ _ e => by cases e), ?_, fun g => (hf g).keep ?_ rfl⟩ + · rw [ut_rd_tcbA hw hW H ht (hal t ht).1 (Or.inl rfl)]; exact he + · exact ut_rd_tcbA hw hW H ht (hal t ht).2 (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr rfl)))))) + +theorem utFr_yieldA {C : Set U64} (h : YieldRelA Z.T C m (L.mem s) σ) (hal : ∀ t ∈ Z.T, Aligned (yieldA t)) : + YieldRelA Z.T C m (L.mem s') σ' := by + have k : ∀ t ∈ Z.T, L.mem s' (yieldA t) = L.mem s (yieldA t) := + fun t ht => ut_rd_tcbA hw hW H ht (hal t ht) (Or.inr (Or.inl rfl)) + refine ⟨fun t ht => ?_, fun t ht hnz => ?_, fun c hc y n hy p hp => ?_⟩ + · obtain ⟨x, hx, he⟩ := h.tcb t ht + exact ⟨x, H.keep hx (fun _ _ e => by cases e), by rw [k t ht]; exact he⟩ + · rw [k t ht] at hnz ⊢ + obtain ⟨hc, y, n, hy, hf⟩ := h.toSc t ht hnz + exact ⟨hc, y, n, H.keep hy (fun _ _ e => by cases e), hf⟩ + · obtain ⟨t, ht, e1, e2⟩ := h.frm c hc y n (H.back hy (fun _ _ e => by cases e)) p hp + exact ⟨t, ht, e1, by rw [k t ht]; exact e2⟩ + +/-- Closure item E's reading: a TCB's VCPU word is kept. -/ +theorem utFr_tvcpuA {t : U64} (ht : t ∈ Z.T) (hal : Aligned (wa t 3664#u64)) (h : L.mem s (wa t 3664#u64) = 0#u64) : + L.mem s' (wa t 3664#u64) = 0#u64 := by rw [ut_rd_tcbA hw hW H ht hal (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl rfl))))))]; exact h + +end fr + +/-! ## Across a free-index change -/ + +/-- **FL4's caps across the reset's cap rewrite** (`i' = i`: across a chunk clear): the untyped at `x` keeps its +base, size and device bit; every other slot of `OS` keeps its cap. -/ +structure UtFiA (st : machine.Store S) [L : LawfulStore st] (OS : Set U64) (x p b i i' : U64) (d : Bool) + (s s' : S) : Prop where + keep : ∀ y ∈ OS, y ≠ x → capA st s' y = capA st s y + old : capA st s x = .Untyped p b i d + new : capA st s' x = .Untyped p b i' d + +section fi +variable {OS : Set U64} {x p b i i' : U64} {d : Bool} {s s' : S} (F : UtFiA st OS x p b i i' d s s') +include F + +/-- Every `Untyped` cap after was one before, with the same base, size and device bit. -/ +theorem UtFiA.ut {u : U64} (hu : u ∈ OS) {q c fi : U64} {e : Bool} (h : capA st s' u = .Untyped q c fi e) : + ∃ fi0, capA st s u = .Untyped q c fi0 e ∧ (u ≠ x → fi0 = fi) ∧ (u = x → q = p ∧ c = b ∧ e = d ∧ fi = i') := by + by_cases ex : u = x + · subst ex; rw [F.new] at h; cases h + exact ⟨i, F.old, fun n => absurd rfl n, fun _ => ⟨rfl, rfl, rfl, rfl⟩⟩ + · rw [F.keep u hu ex] at h + exact ⟨fi, h, fun _ => rfl, fun e => absurd e ex⟩ + +theorem utFi_deadA (hx : x ∈ OS) {k : DKindA} {q : U64} : DeadA st OS k s' q ↔ DeadA st OS k s q := by + refine ⟨fun h u hu hp => ?_, fun h u hu hp => ?_⟩ + · by_cases ex : u = x + · rw [ex, F.old] at hp; exact not_namesD_untypedA _ _ _ _ _ _ hp + · exact h u hu (by rw [F.keep u hu ex]; exact hp) + · by_cases ex : u = x + · rw [ex, F.new] at hp; exact not_namesD_untypedA _ _ _ _ _ _ hp + · rw [F.keep u hu ex] at hp; exact h u hu hp + +theorem utFi_utBlkA (_ : x ∈ OS) (h : UtBlk39A st OS s) : UtBlk39A st OS s' := fun u hu q c fi e hc => by + obtain ⟨fi0, h0, -, -⟩ := F.ut hu hc + exact h u hu q c fi0 e h0 + +theorem utFi_idleAnchorA (h : IdleAnchorOffA (st := st) OS s) : IdleAnchorOffA (st := st) OS s' := + fun u hu q c fi e hc => by + obtain ⟨fi0, h0, -, -⟩ := F.ut hu hc + exact h u hu q c fi0 e h0 + +/-- `ObjAllocA`: no object of the record meets the region (after the drop). -/ +theorem utFi_objAllocA {Z : XSetsA} {H : Heap} (h : ObjAllocA (st := st) Z H OS s) + (hclr : ∀ q bytes, JointObjectA Z H q bytes → q.val + bytes ≤ p.val ∨ p.val + 2 ^ b.val ≤ q.val) : + ObjAllocA (st := st) Z H OS s' := fun q bytes hq u hu q' c fi e hc => by + obtain ⟨fi0, h0, same, atx⟩ := F.ut hu hc + by_cases ex : u = x + · obtain ⟨rfl, rfl, -, -⟩ := atx ex + rcases hclr q bytes hq with k | k + · exact Or.inl k + · exact Or.inr (Or.inl k) + · rw [← same ex]; exact h q bytes hq u hu q' c fi0 e h0 + +/-- A free address after was free before, or lies in the region. -/ +theorem utFi_freeA {dev : Bool → Prop} {n : Nat} (h : UtFreeA st OS s' dev n) : + UtFreeA st OS s dev n ∨ (dev d ∧ p.val ≤ n ∧ n < p.val + 2 ^ b.val) := by + obtain ⟨u, hu, q, c, fi, e, hc, hd, h1, h2⟩ := h + obtain ⟨fi0, h0, same, atx⟩ := F.ut hu hc + by_cases ex : u = x + · obtain ⟨rfl, rfl, rfl, -⟩ := atx ex + exact Or.inr ⟨hd, by omega, h2⟩ + · rw [← same ex] at h1; exact Or.inl ⟨u, hu, q, c, fi0, e, h0, hd, h1, h2⟩ + +/-- `SpecFreeA`: the spec holds nothing in the region (after the drop), and the CNode step keeps the CDT. -/ +theorem utFi_specFreeA {σ σ' : State Unit} (h : SpecFreeA st OS s σ) (H : UtCnStepA σ σ') + (hcdt : σ'.cdt = σ.cdt) (horig : σ'.isOriginalCap = σ.isOriginalCap) + (hheap : ∀ q : U64, p.val ≤ q.val → q.val < p.val + 2 ^ b.val → σ.kheap (specPtr q) = none) + (hlink : ∀ a c, σ.cdt a = some c → ∀ q : U64, p.val ≤ q.val → q.val < p.val + 2 ^ b.val → + a.1 ≠ specPtr q ∧ c.1 ≠ specPtr q) + (hor : ∀ a, σ.isOriginalCap a = true → ∀ q : U64, p.val ≤ q.val → q.val < p.val + 2 ^ b.val → + a.1 ≠ specPtr q) : SpecFreeA st OS s' σ' := by + refine ⟨fun q hq => ?_, fun a c hc q hq => ?_, fun a ha q hq => ?_⟩ + · have old : σ.kheap (specPtr q) = none := by + rcases utFi_freeA F hq with k | ⟨-, k1, k2⟩ + · exact h.heap q k + · exact hheap q k1 k2 + rcases H.kh (specPtr q) with e | ⟨r, cs, cs', e1, -⟩ + · rw [e]; exact old + · rw [old] at e1; cases e1 + · rw [hcdt] at hc + rcases utFi_freeA F hq with k | ⟨-, k1, k2⟩ + · exact h.cdt a c hc q k + · exact hlink a c hc q k1 k2 + · rw [horig] at ha + rcases utFi_freeA F hq with k | ⟨-, k1, k2⟩ + · exact h.orig a ha q k + · exact hor a ha q k1 k2 + +/-- `SpecZeroFreeA`: the spec's bytes of a RAM region are zero after `delete_objects`. -/ +theorem utFi_specZeroA {σ σ' : State Unit} (h : SpecZeroFreeA st OS s σ) (H : UtCnStepA σ σ') + (hz : d = false → ∀ a : Word, p.val ≤ (fl4Ptr a).val → (fl4Ptr a).val < p.val + 2 ^ b.val → + σ.machineState.underlyingMemory a = 0) : SpecZeroFreeA st OS s' σ' := fun a ha => by + rw [H.um] + rcases utFi_freeA F ha with k | ⟨hd, k1, k2⟩ + · exact h a k + · exact hz hd a k1 k2 + +/-- J2's references across the cap rewrite. -/ +theorem utFi_deadExtA (hx : x ∈ OS) {Z : XSetsA} {Rp : Set U64} {stack : U64 → List U64} {σ σ' : State Unit} + (h : DeadExtA st Z OS Rp stack s σ) (H : UtCnStepA σ σ') + (ht : ∀ t ∈ Z.T, L.mem s' (wa t 3664#u64) = L.mem s (wa t 3664#u64)) + (hv24 : ∀ v ∈ Z.V, L.mem s' (wa v 24#u64) = L.mem s (wa v 24#u64)) : + DeadExtA st Z OS Rp stack s' σ' := by + refine ⟨fun v hv hd y hy => ?_, fun r hr hd => h.reply r hr ((utFi_deadA F hx).1 hd), fun j q hq hd => ?_, + fun t htt hd => ?_, fun v hv hd => by rw [hv24 v hv]; exact h.vcDeadRun v hv ((utFi_deadA F hx).1 hd), + fun r hr hd y hy => h.replyTcb r hr ((utFi_deadA F hx).1 hd) y (H.back hy (fun _ _ e => by cases e))⟩ + · exact h.vcpu v hv ((utFi_deadA F hx).1 hd) y (H.back hy (fun _ _ e => by cases e)) + · rw [H.asid] at hq + exact h.asid j q hq ((utFi_deadA F hx).1 hd) + · rw [ht t htt]; exact h.tvcpu t htt ((utFi_deadA F hx).1 hd) + +end fi + +/-! ## The P1 invariants (`sys` but `ent.vs`, and `sq`) -/ + +/-- No heap object meets the region: nothing is dropped. -/ +theorem ut_dropOK_noneA {H : Heap} {Cs : Set U64} {s : S} {lo hi : Nat} (hH : HeapOK H) (h0 : H 0#u64 = none) + (hclr : ∀ q k, H q = some k → ¬ RegionD H lo hi q) (hcur : H (L.curW s) ≠ none) (hcsc : H (L.cscW s) ≠ none) : + DropOK st H (RegionD H lo hi) Cs s := by + have K : ∀ x, KeptRef H (RegionD H lo hi) x := fun x hx => by + obtain ⟨k, hk⟩ := Option.ne_none_iff_exists'.1 hx + exact hclr x k hk + have O : ∀ b, OffDrop H (RegionD H lo hi) b := fun _ q k hk hD _ => hclr q k hk hD + exact ⟨hH, h0, fun q k hk hD => absurd hD (hclr q k hk), K _ hcur, K _ hcsc, fun _ _ _ => O _, + fun _ _ _ _ _ => O _, fun _ _ _ _ => K _, fun _ _ _ => K _, fun _ _ _ => K _, fun _ _ _ => K _, + fun _ _ _ => K _, fun _ _ _ => K _, fun _ _ _ => K _, fun _ _ _ => K _, + fun _ _ _ => ⟨K _, K _, K _, K _, K _⟩, fun _ _ => ⟨K _, K _, K _⟩⟩ + +theorem ut_drop_noneA {H : Heap} {lo hi : Nat} (hclr : ∀ q k, H q = some k → ¬ RegionD H lo hi q) : + H.drop (RegionD H lo hi) = H := by + funext q + by_cases h : RegionD H lo hi q + · cases e : H q with + | none => unfold Heap.drop; simp [h] + | some k => exact absurd h (hclr q k e) + · exact Heap.drop_of_not h + +/-- **`sys.sysl`, `SymL` and the block invariants across a chunk step** (A64 P1's `SysL.dropR` with nothing left to +drop): FL4 writes the region and the untyped slot's two cap words only. -/ +theorem utFr_syslA {H : Heap} {Cs Ac Rp Nt OS : Set U64} {s s' : S} {extra : Aeneas.Std.Array obj.Slot 3#usize} + {x p sb fi i : U64} {ia ie : Nat} (P : SysPlace st H Cs H.frame Rp Nt OS s extra x) + (hb : BlkOK st H.tcbs Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s) + (B : SysL st H Cs OS ia ie s) (Y : SymL st H Cs s) (h0 : H 0#u64 = none) + (hclr : ∀ q k, H q = some k → ¬ RegionD H p.val (p.val + 2 ^ sb.val) q) + (hoff : ∀ y ∈ OS, y.val + 32 ≤ p.val ∨ p.val + 2 ^ sb.val ≤ y.val) + (hcur : H (L.curW s) ≠ none) (hcsc : H (L.cscW s) ≠ none) + (hrun : Runnable (L.mem s (stA (L.curW s)))) (hcx : capA st s x = .Untyped p sb fi false) + (hnd : ∀ G par, TreeA st OS G par s → ∀ y, ¬ Fl4Proofs.P3.Mdb.Desc par y x) + (w : WritesIn st s s' (fun b => (p.val ≤ b.val ∧ b.val < p.val + 2 ^ sb.val) ∨ b = x ∨ b = wa x 8#u64)) + (kc : ∀ y ∈ OS, y ≠ x → capA st s' y = capA st s y) + (hcx' : capA st s' x = .Untyped p sb i false) (hi : i.val ≤ 2 ^ sb.val) (hUZ : UtZeroA st OS s') : + SysL st H Cs OS ia ie s' ∧ SymL st H Cs s' ∧ + BlkOK st H.tcbs Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s' := by + have DK := ut_dropOK_noneA (st := st) (Cs := Cs) (s := s) (lo := p.val) (hi := p.val + 2 ^ sb.val) P.heap h0 + hclr hcur hcsc + have eH := ut_drop_noneA hclr + have eO : OffR p.val (p.val + 2 ^ sb.val) OS = OS := Set.ext fun y => ⟨fun h => h.1, fun h => ⟨h, hoff y h⟩⟩ + have := SysL.dropR P hb B Y DK hrun hcx hnd w (fun y hy => kc y hy.1) hcx' hi (by rw [eO]; exact hUZ) + rwa [eH, eO] at this + +/-- **`sys.ent` but `vs` across a chunk step**, from the block invariants after (`utFr_syslA`). -/ +theorem utFr_entA {H : Heap} {Cs OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} {s s' : S} + (h : EntryInvA3 st H Cs OS U Ac Pp Tb Lv s) + (hb : BlkOK st H.tcbs Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s') (hk : L.ksb s' = L.ksb s) + (hvs : VsTyR st (L.ksb s') (PoolSet st Pp s') ∅ Tb Lv s') : EntryInvA3 st H Cs OS U Ac Pp Tb Lv s' := by + obtain ⟨b1, b2, b3, b4, b5⟩ := hb + exact ⟨h.heap, ⟨b1, b2, b3, b5, b4⟩, h.os, h.user.ksb hk, hvs, by rw [hk]; exact h.ksw⟩ + +/-- **`sq` across a chunk step** (A64 P1's `SchedQueuesInv.agreeD`). -/ +theorem utFr_sqA {T Cs Ω Sg : Set U64} {H : Heap} {s s' : S} {Q : U64 → U64 → List U64} {R : U64 → List U64} + (h : SchedQueuesInv st T Cs Ω H s Q R) {W : U64 → Prop} (hw : WritesIn st s s' W) + (htcb : ∀ y ∈ T, ∀ K : U64, 192 ≤ K.val → K.val < 4096 → K.val % 8 = 0 → Aligned (wa y K) ∧ ¬ W (wa y K)) + (hnode : ∀ c ∈ Cs, ∀ o : U64, o.val < 4288 → o.val % 8 = 0 → Aligned (L.nodeAt s c o) ∧ ¬ W (L.nodeAt s c o)) + (hsig : ∀ b ∈ Sg, Aligned b ∧ ¬ W b) + (hep : ∀ e, (H e = some .ep ∨ H e = some .ntfn) → + Aligned (epHeadA e) ∧ ¬ W (epHeadA e) ∧ Aligned (epTSA e) ∧ ¬ W (epTSA e)) : + SchedQueuesInv st T Cs Ω H s' Q R := by + have G : AgreeD st T Cs H Sg s s' := + ⟨hw.1, hw.2.1, + fun y hy K h1 h2 h3 _ _ => hw.mem_eq (htcb y hy K h1 h2 h3).1 (htcb y hy K h1 h2 h3).2, + fun c hc o h1 h2 _ => hw.mem_eq (hnode c hc o h1 h2).1 (hnode c hc o h1 h2).2, + fun b hb => hw.mem_eq (hsig b hb).1 (hsig b hb).2, + fun e he => ⟨hw.mem_eq (hep e he).1 (hep e he).2.1, hw.mem_eq (hep e he).2.2.1 (hep e he).2.2.2⟩⟩ + exact h.agreeD G + +/-! ## The preemption point -/ + +/-- [J] (contract J8) **The preemption point's loop hook** (x86 `PPHypDW`'s form; producer MCS-18/19): from the +loop invariant `R`, FL4's caps in `OS` are kept and the spec takes an outcome of the same kind, `R` after `Ok`, `Rp` +after a preemption. -/ +def PPHypUtA (st : machine.Store S) [L : LawfulStore st] (ar : machine.Arch A) (OS : Set U64) + (R Rp : S → A → State Unit → Prop) : Prop := + ∀ (s : S) (a : A) (σ : State Unit), R s a σ → + sched.preemption_point st ar s a ⦃ r => + (∀ y ∈ OS, capA st r.2.1 y = capA st s y) ∧ ∃ σ', + ((r.1 = .Ok () ∧ (.ok (), σ') ∈ ((preemptionPoint (Z := Unit)).run σ).1 ∧ R r.2.1 r.2.2 σ') ∨ + (r.1 = .Err .Preempted ∧ (.error (), σ') ∈ ((preemptionPoint (Z := Unit)).run σ).1 ∧ + Rp r.2.1 r.2.2 σ')) ⦄ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtFullAllocA.lean b/proofs/Fl4ProofsA64/P3/UtFullAllocA.lean new file mode 100644 index 000000000..c444eb874 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtFullAllocA.lean @@ -0,0 +1,138 @@ +/- +P3 (aarch64, A64-P3 Untyped, full-record post, part A): **`objAlloc` and `idleAnchor` after a RAM creation**, and +`ptH` / `tabInj`. The split of the joint objects into old ones and new ones in the window, and `hoth` (every other +untyped is off the window or holds the source's region below its watermark: `utRt_hothA`) are hypotheses, discharged at the +assembly. + +No named premise. New theorems only. +-/ +import Fl4ProofsA64.P3.UtFullTabA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4SpecA64 Classical + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {Z Z' : XSetsA} {H H' : Heap} {OS OS' : Set U64} {s s' : S} {x p sb nt us aligned : U64} {win : Nat} + +theorem utRt_objAllocA (h : ObjAllocA (st := st) Z H OS s) + (F : UtSlotsA st OS OS' s s' x p sb nt us aligned win) (W : UtWinZA nt aligned (zA nt.val us) win) + {fi nfi : U64} (hx : x ∈ OS) (hc : capA st s x = .Untyped p sb fi false) + (hc' : capA st s' x = .Untyped p sb nfi false) (h1 : p.val + fi.val ≤ aligned.val) + (h2 : aligned.val + win * 2 ^ (zA nt.val us).val ≤ p.val + nfi.val) + (h3 : aligned.val + win * 2 ^ (zA nt.val us).val ≤ p.val + 2 ^ sb.val) + -- old joint objects, or new ones in the window (none for new untypeds) + (hobj : ∀ q bytes, JointObjectA Z' H' q bytes → JointObjectA Z H q bytes ∨ + (nt.val ≠ 0 ∧ aligned.val ≤ q.val ∧ q.val + bytes ≤ aligned.val + win * 2 ^ (zA nt.val us).val)) + (hoth : ∀ u ∈ OS, u ≠ x → ∀ q b f d, capA st s u = .Untyped q b f d → + (q.val + 2 ^ b.val ≤ aligned.val ∨ aligned.val + win * 2 ^ (zA nt.val us).val ≤ q.val) ∨ + (q.val ≤ p.val ∧ p.val + 2 ^ sb.val ≤ q.val + f.val)) : + ObjAllocA (st := st) Z' H' OS' s' := by + intro o bytes ho u hu q b f d e + rcases F u hu with ⟨uo, k⟩ | ⟨ux, f', k⟩ | ⟨j, hj, k⟩ | k + · have e0 := k.symm.trans e + by_cases uxe : u = x + · subst uxe + have g1 := hc.symm.trans e0 + have g2 := hc'.symm.trans e + simp only [capability.Cap.Untyped.injEq] at g1 g2 + have v1 := congrArg UScalar.val g1.2.2.1 + have v2 := congrArg UScalar.val g2.2.2.1 + have v3 := congrArg UScalar.val (hc.symm.trans (k.symm.trans hc') |> (capability.Cap.Untyped.injEq ..).mp).2.2.1 + rw [← g2.1, ← g2.2.1] + rcases hobj o bytes ho with old | ⟨-, a1, a2⟩ + · have := h o bytes old u uo p sb fi false hc + omega + · omega + · rcases hobj o bytes ho with old | ⟨-, a1, a2⟩ + · exact h o bytes old u uo q b f d e0 + · rcases hoth u uo uxe q b f d e0 with (g | g) | g <;> omega + · subst ux + have g := hc'.symm.trans e + simp only [capability.Cap.Untyped.injEq] at g + obtain ⟨g1, g2, g3, -⟩ := g + rw [← g1, ← g2, ← g3] + rcases hobj o bytes ho with old | ⟨-, a1, a2⟩ + · have := h o bytes old u hx p sb fi false hc + omega + · omega + · obtain ⟨h0, eq, eb, -, -⟩ := utNewCap_untypedA (k.symm.trans e) + have inw := utBase_inA W (b := bjA aligned (zA nt.val us) j) ⟨j, hj, rfl⟩ + have ez : zA nt.val us = us := by rw [h0]; rfl + simp only [ez] at inw eq h3 + rw [eq, eb] + rcases hobj o bytes ho with old | ⟨ne, -, -⟩ + · have := h o bytes old x hx p sb fi false hc + have f0 : fi.val ≤ aligned.val - p.val := by omega + rcases this with g | g | ⟨g1, g2⟩ + · omega + · omega + · omega + · exact absurd h0 ne + · rw [k] at e; cases e + +theorem utRt_idleAnchorA (h : IdleAnchorOffA (st := st) OS s) + (F : UtSlotsA st OS OS' s s' x p sb nt us aligned win) (W : UtWinZA nt aligned (zA nt.val us) win) + {fi : U64} (hx : x ∈ OS) (hc : capA st s x = .Untyped p sb fi false) (hlo : p.val ≤ aligned.val) + (h3 : aligned.val + win * 2 ^ (zA nt.val us).val ≤ p.val + 2 ^ sb.val) : + IdleAnchorOffA (st := st) OS' s' := by + intro u hu q b f d e + rcases F u hu with ⟨uo, k⟩ | ⟨-, f', k⟩ | ⟨j, hj, k⟩ | k + · exact h u uo q b f d (k.symm.trans e) + · have g := k.symm.trans e + simp only [capability.Cap.Untyped.injEq] at g + rw [← g.1, ← g.2.1] + exact h x hx p sb fi false hc + · obtain ⟨h0, eq, eb, -, -⟩ := utNewCap_untypedA (k.symm.trans e) + have inw := utBase_inA W (b := bjA aligned (zA nt.val us) j) ⟨j, hj, rfl⟩ + have ez : zA nt.val us = us := by rw [h0]; rfl + simp only [ez] at inw eq h3 + rw [eq, eb] + rcases h x hx p sb fi false hc with g | g <;> omega + · rw [k] at e; cases e + +/-- `hn`: a new pool or Stage-1 page lies in an `other` object of the grown heap. -/ +theorem utRt_ptHA {P P' : Set U64} (h : PtHA H P) (hH : ∀ q k, H q = some k → H' q = some k) + (hn : ∀ r ∈ P', r ∈ P ∨ ∃ q z, H' q = some (.other z) ∧ q.val ≤ r.val ∧ r.val + 4096 ≤ q.val + 2 ^ z) : + PtHA H' P' := by + intro r hr + rcases hn r hr with old | new + · obtain ⟨q, z, e, a, b⟩ := h r old + exact ⟨q, z, hH _ _ e, a, b⟩ + · exact new + +theorem utRt_tabInjA {σ σ' : State Unit} + (h : ∀ i j r, σ.archState.armAsidTable i = some r → σ.archState.armAsidTable j = some r → i = j) + (hA : σ'.archState.armAsidTable = σ.archState.armAsidTable) : + ∀ i j r, σ'.archState.armAsidTable i = some r → σ'.archState.armAsidTable j = some r → i = j := by + rw [hA]; exact h + +/-- `hp` / `hr`: a new table page, VCPU or two-page root lies in an `other` object of the grown heap. -/ +theorem utRt_setsInHA (h : SetsInHA Z H) (hH : ∀ q k, H q = some k → H' q = some k) + (hp : ∀ r ∈ Z'.Pp ∪ Z'.S1 ∪ Z'.T2 ∪ Z'.V, r ∈ Z.Pp ∪ Z.S1 ∪ Z.T2 ∪ Z.V ∨ + ∃ q z, H' q = some (.other z) ∧ q.val ≤ r.val ∧ r.val + 4096 ≤ q.val + 2 ^ z) + (hr : ∀ r ∈ Z'.X ∪ Z'.Y, r ∈ Z.X ∪ Z.Y ∨ + ∃ q z, H' q = some (.other z) ∧ q.val ≤ r.val ∧ r.val + 8192 ≤ q.val + 2 ^ z) : + SetsInHA Z' H' := by + refine ⟨utRt_ptHA h.pages hH hp, fun r m => ?_⟩ + rcases hr r m with old | new + · obtain ⟨q, z, e, a, b⟩ := h.roots r old + exact ⟨q, z, hH _ _ e, a, b⟩ + · exact new + +/-- `vsVc`. `hsplit`: a VSpace word of the grown sets in a VCPU of the grown sets is an old one of both (one Retype +makes one type, and the new objects are off the old ones). -/ +theorem utRt_vsVcA (h : ∀ b, VsFootA Z.Pp Z.S1 Z.Y (L.ksb s) b → ¬ VcW Z.V b) (hks : L.ksb s' = L.ksb s) + (hsplit : ∀ b, VsFootA Z'.Pp Z'.S1 Z'.Y (L.ksb s) b → VcW Z'.V b → + VsFootA Z.Pp Z.S1 Z.Y (L.ksb s) b ∧ VcW Z.V b) : + ∀ b, VsFootA Z'.Pp Z'.S1 Z'.Y (L.ksb s') b → ¬ VcW Z'.V b := by + intro b hb hv + rw [hks] at hb + obtain ⟨a, c⟩ := hsplit b hb hv + exact h b a c + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtFullCapA.lean b/proofs/Fl4ProofsA64/P3/UtFullCapA.lean new file mode 100644 index 000000000..619027df9 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtFullCapA.lean @@ -0,0 +1,177 @@ +/- +P3 (aarch64, A64-P3 Untyped, full-record post, part C): **the cap-form fields after a creation** whose clauses only +read `OS` caps: `utBlk`, `zombieBnd`, `zombieTcb`, `zombieNoVcpu`; and the free-part clauses `specFree`, `specZero` (the free parts only +shrink, `utRt_freeShrinkA`). + +`UtSlotsA` says where each cap of the grown slot set comes from. The old TCBs' VCPU words and the heap's growth are +hypotheses here, discharged at the assembly. + +No named premise. New theorems only. +-/ +import Fl4ProofsA64.P3.UtFullSchedA +import Fl4ProofsA64.P3.UtRtVsA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4SpecA64 Classical + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **Where a cap of the grown slot set comes from**: an old slot kept, the source untyped, a new object's cap, or +null. -/ +def UtSlotsA (st : machine.Store S) [LawfulStore st] (OS OS' : Set U64) (s s' : S) (x p sb nt us aligned : U64) + (win : Nat) : Prop := + ∀ u ∈ OS', (u ∈ OS ∧ capA st s' u = capA st s u) ∨ (u = x ∧ ∃ fi, capA st s' u = .Untyped p sb fi false) ∨ + (∃ j < win, capA st s' u = newCapA nt.val (bjA aligned (zA nt.val us) j) us false) ∨ capA st s' u = .Null + +private theorem ut_iteCapA {α : Type} {c : Prop} [Decidable c] {x y z : α} (h : (if c then x else y) = z) : + (c ∧ x = z) ∨ (¬ c ∧ y = z) := by + by_cases g : c + · exact Or.inl ⟨g, (ite_eq_left g).symm.trans h⟩ + · exact Or.inr ⟨g, (ite_eq_right g).symm.trans h⟩ + +theorem utNewCap_notZombieA (n : Nat) (b us : U64) (d : Bool) (p t k : U64) : + newCapA n b us d ≠ .Zombie p t k := by + intro h + unfold newCapA at h + repeat + rcases ut_iteCapA h with ⟨-, h⟩ | ⟨-, h⟩ + · cases h + cases h + +theorem utNewCap_untypedA {n : Nat} {b us q bits fi : U64} {d d' : Bool} + (h : newCapA n b us d = .Untyped q bits fi d') : n = 0 ∧ q = b ∧ bits = us ∧ fi = 0#u64 ∧ d' = d := by + unfold newCapA at h + repeat + rcases ut_iteCapA h with ⟨e, h⟩ | ⟨-, h⟩ + · first + | (cases h; exact ⟨e, rfl, rfl, rfl, rfl⟩) + | cases h + cases h + +variable {OS OS' : Set U64} {s s' : S} {x p sb nt us aligned : U64} {win : Nat} + +/-- A zombie of the grown slot set is an old one. -/ +theorem utRt_oldZombieA (F : UtSlotsA st OS OS' s s' x p sb nt us aligned win) {u q t k : U64} (hu : u ∈ OS') + (h : capA st s' u = .Zombie q t k) : u ∈ OS ∧ capA st s u = .Zombie q t k := by + rcases F u hu with ⟨a, e⟩ | ⟨-, fi, e⟩ | ⟨j, -, e⟩ | e + · exact ⟨a, e.symm.trans h⟩ + · rw [e] at h; cases h + · exact absurd (e.symm.trans h) (utNewCap_notZombieA _ _ _ _ _ _ _) + · rw [e] at h; cases h + +theorem utRt_zombieBndA (h : UtZombieA st OS s) (F : UtSlotsA st OS OS' s s' x p sb nt us aligned win) : + UtZombieA st OS' s' := by + intro y hy q t k e + obtain ⟨a, b⟩ := utRt_oldZombieA F hy e + exact h y a q t k b + +theorem utRt_zombieTcbA {H H' : Heap} (h : ZombieTcbTA st H OS s) + (F : UtSlotsA st OS OS' s s' x p sb nt us aligned win) (hH : ∀ t ∈ H.tcbs, t ∈ H'.tcbs) : + ZombieTcbTA st H' OS' s' := by + intro u hu q k e + obtain ⟨a, b⟩ := utRt_oldZombieA F hu e + exact hH q (h u a q k b) + +theorem utRt_zombieNoVcpuA {H : Heap} (h : ZombieTcbNoVcpuA st OS s) (hz : ZombieTcbTA st H OS s) + (F : UtSlotsA st OS OS' s s' x p sb nt us aligned win) + (hm : ∀ t ∈ H.tcbs, L.mem s' (wa t 3664#u64) = L.mem s (wa t 3664#u64)) : ZombieTcbNoVcpuA st OS' s' := by + intro u hu q k e + obtain ⟨a, b⟩ := utRt_oldZombieA F hu e + rw [hm q (hz u a q k b)] + exact h u a q k b + +/-- `hs`: the source is an `OS` untyped; `hus`: the decode's size check on a new untyped. -/ +theorem utRt_utBlkA (h : UtBlk39A st OS s) (F : UtSlotsA st OS OS' s s' x p sb nt us aligned win) + {fi : U64} (hx : x ∈ OS) (hs : capA st s x = .Untyped p sb fi false) (hus : nt.val = 0 → us.val ≤ 39) : + UtBlk39A st OS' s' := by + intro u hu q b f d e + rcases F u hu with ⟨a, k⟩ | ⟨-, f', k⟩ | ⟨j, -, k⟩ | k + · exact h u a q b f d (k.symm.trans e) + · rw [k] at e + cases e + exact h x hx _ _ _ _ hs + · obtain ⟨h0, -, rfl, -, -⟩ := utNewCap_untypedA (k.symm.trans e) + exact hus h0 + · rw [k] at e; cases e + +/-- **The free parts only shrink**: a free address after the creation was free before, and lies in the window only if +the new objects are untypeds. `hoth`: no other untyped's free part meets the window (`pool_utOther`). -/ +theorem utRt_freeShrinkA (F : UtSlotsA st OS OS' s s' x p sb nt us aligned win) + (W : UtWinZA nt aligned (zA nt.val us) win) {fi nfi : U64} (hx : x ∈ OS) + (hc : capA st s x = .Untyped p sb fi false) (hc' : capA st s' x = .Untyped p sb nfi false) + (h1 : p.val + fi.val ≤ aligned.val) (h2 : aligned.val + win * 2 ^ (zA nt.val us).val ≤ p.val + nfi.val) + (h3 : aligned.val + win * 2 ^ (zA nt.val us).val ≤ p.val + 2 ^ sb.val) + (hoth : ∀ u ∈ OS, u ≠ x → ∀ q b f d, capA st s u = .Untyped q b f d → ∀ a, q.val + f.val ≤ a → + a < q.val + 2 ^ b.val → ¬ (aligned.val ≤ a ∧ a < aligned.val + win * 2 ^ (zA nt.val us).val)) + {dev : Bool → Prop} {a : Nat} (h : UtFreeA st OS' s' dev a) : + UtFreeA st OS s dev a ∧ + (aligned.val ≤ a → a < aligned.val + win * 2 ^ (zA nt.val us).val → nt.val = 0) := by + obtain ⟨u, hu, q, b, f, d, e, hd, a1, a2⟩ := h + rcases F u hu with ⟨uo, k⟩ | ⟨ux, f', k⟩ | ⟨j, hj, k⟩ | k + · have e0 := k.symm.trans e + refine ⟨⟨u, uo, q, b, f, d, e0, hd, a1, a2⟩, fun w1 w2 => ?_⟩ + by_cases ux : u = x + · subst ux + have g := hc.symm.trans (k.symm.trans hc') + simp only [capability.Cap.Untyped.injEq] at g + have := congrArg UScalar.val g.2.2.1 + omega + · exact absurd ⟨w1, w2⟩ (hoth u uo ux q b f d e0 a a1 a2) + · subst ux + have g := hc'.symm.trans e + simp only [capability.Cap.Untyped.injEq] at g + obtain ⟨g1, g2, g3, g4⟩ := g + rw [← g1, ← g3] at a1 + rw [← g1, ← g2] at a2 + rw [← g4] at hd + refine ⟨⟨u, hx, p, sb, fi, false, hc, hd, by omega, a2⟩, fun w1 w2 => ?_⟩ + omega + · obtain ⟨h0, eq, eb, ef, ed⟩ := utNewCap_untypedA (k.symm.trans e) + have inw := utBase_inA W (b := bjA aligned (zA nt.val us) j) ⟨j, hj, rfl⟩ + have ez : zA nt.val us = us := by rw [h0]; rfl + have f0 : f.val = 0 := by rw [ef]; rfl + rw [eq, f0] at a1 + rw [eq, eb] at a2 + rw [ed] at hd + simp only [ez] at inw h1 h2 h3 a1 a2 ⊢ + exact ⟨⟨x, hx, p, sb, fi, false, hc, hd, by omega, by omega⟩, fun _ _ => h0⟩ + · rw [k] at e; cases e + +/-- `hum`: the creation keeps zero bytes zero. -/ +theorem utRt_specZeroA {σ σ' : State Unit} (h : SpecZeroFreeA st OS s σ) + (shrink : ∀ a, UtFreeA st OS' s' (fun d => d = false) a → UtFreeA st OS s (fun d => d = false) a) + (hum : ∀ a, σ.machineState.underlyingMemory a = 0 → σ'.machineState.underlyingMemory a = 0) : + SpecZeroFreeA st OS' s' σ' := + fun a ha => hum a (h a (shrink _ ha)) + +/-- `hk`: the spec creates no object outside the window, and none at all for new untypeds; `hcdt` / `horig`: a new CDT +link or original bit names no free address. -/ +theorem utRt_specFreeA {σ σ' : State Unit} (h : SpecFreeA st OS s σ) + (shrink : ∀ a, UtFreeA st OS' s' (fun _ => True) a → UtFreeA st OS s (fun _ => True) a ∧ + (aligned.val ≤ a → a < aligned.val + win * 2 ^ (zA nt.val us).val → nt.val = 0)) + (hk : ∀ q : U64, σ.kheap (specPtr q) = none → + (aligned.val ≤ q.val → q.val < aligned.val + win * 2 ^ (zA nt.val us).val → nt.val = 0) → + σ'.kheap (specPtr q) = none) + (hcdt : ∀ c d, σ'.cdt c = some d → σ.cdt c = some d ∨ + ∀ q : U64, UtFreeA st OS s (fun _ => True) q.val → c.1 ≠ specPtr q ∧ d.1 ≠ specPtr q) + (horig : ∀ c, σ'.isOriginalCap c = true → σ.isOriginalCap c = true ∨ + ∀ q : U64, UtFreeA st OS s (fun _ => True) q.val → c.1 ≠ specPtr q) : + SpecFreeA st OS' s' σ' := by + refine ⟨fun q hq => ?_, fun c d e q hq => ?_, fun c e q hq => ?_⟩ + · obtain ⟨old, w⟩ := shrink _ hq + exact hk q (h.heap q old) w + · rcases hcdt c d e with g | g + · exact h.cdt c d g q (shrink _ hq).1 + · exact g q (shrink _ hq).1 + · rcases horig c e with g | g + · exact h.orig c g q (shrink _ hq).1 + · exact g q (shrink _ hq).1 + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtFullCapTyA.lean b/proofs/Fl4ProofsA64/P3/UtFullCapTyA.lean new file mode 100644 index 000000000..d29d88b5a --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtFullCapTyA.lean @@ -0,0 +1,81 @@ +/- +P3 (aarch64, A64-P3 Untyped, full-record post): **`capTy` after a creation.** Old caps keep their heap object, the +source stays an untyped, and a new Endpoint, Notification, Reply or SchedContext cap points at a fresh heap object of +that kind (`hnew`, from the grown heap at the assembly). + +No named premise. New theorems only. +-/ +import Fl4ProofsA64.P3.UtFullCapA +import Fl4ProofsA64.P3.Stage3FieldsA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Classical + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +private theorem ut_iteKindA {α : Type} {c : Prop} [Decidable c] {x y z : α} (h : (if c then x else y) = z) : + (c ∧ x = z) ∨ (¬ c ∧ y = z) := by + by_cases g : c + · exact Or.inl ⟨g, (ite_eq_left g).symm.trans h⟩ + · exact Or.inr ⟨g, (ite_eq_right g).symm.trans h⟩ + +/-- The heap object a new cap of type `n` must find at its base. -/ +theorem utNewCap_kindA {H' : Heap} {n : Nat} {base us : U64} {dev : Bool} + (h2 : n = 2 → H' base = some .ep) (h3 : n = 3 → H' base = some .ntfn) (h6 : n = 6 → H' base = some .reply) + (h5 : n = 5 → H' base = some (.sc us.val)) : CapKindA H' (newCapA n base us dev) := by + have key : ∀ c, newCapA n base us dev = c → + (∀ q b x y z w, c = .Endpoint q b x y z w → n = 2 ∧ q = base) ∧ + (∀ q b x y, c = .Notification q b x y → n = 3 ∧ q = base) ∧ + (∀ q g, c = .Reply q g → n = 6 ∧ q = base) ∧ + (∀ q sz, c = .SchedContext q sz → n = 5 ∧ q = base ∧ sz = us) ∧ + (∀ cpu, c ≠ .SchedControl cpu) := by + intro c h + unfold newCapA at h + repeat + rcases ut_iteKindA h with ⟨e, h⟩ | ⟨-, h⟩ + · subst h + refine ⟨fun _ _ _ _ _ _ k => ?_, fun _ _ _ _ k => ?_, fun _ _ k => ?_, fun _ _ k => ?_, fun _ k => ?_⟩ + all_goals first + | (cases k; done) + | (cases k; exact ⟨e, rfl⟩) + | (cases k; exact ⟨e, rfl, rfl⟩) + subst h + refine ⟨fun _ _ _ _ _ _ k => ?_, fun _ _ _ _ k => ?_, fun _ _ k => ?_, fun _ _ k => ?_, fun _ k => ?_⟩ <;> cases k + obtain ⟨k2, k3, k6, k5, kc⟩ := key _ rfl + refine ⟨fun q b x y z w e => ?_, fun q b x y e => ?_, fun q g e => ?_, fun q sz e => ?_, + fun cpu e => absurd e (kc cpu)⟩ + · obtain ⟨a, rfl⟩ := k2 q b x y z w e + exact h2 a + · obtain ⟨a, rfl⟩ := k3 q b x y e + exact h3 a + · obtain ⟨a, rfl⟩ := k6 q g e + exact h6 a + · obtain ⟨a, rfl, rfl⟩ := k5 q sz e + exact h5 a + +theorem utRt_capTyA {H H' : Heap} {OS OS' : Set U64} {s s' : S} {x p sb nt us aligned : U64} {win : Nat} + (h : CapTyA st H OS s) (F : UtSlotsA st OS OS' s s' x p sb nt us aligned win) + (hH : ∀ q k, H q = some k → H' q = some k) + (hnew : ∀ j < win, CapKindA H' (newCapA nt.val (bjA aligned (zA nt.val us) j) us false)) : + CapTyA st H' OS' s' := by + intro u hu + rcases F u hu with ⟨uo, k⟩ | ⟨-, f, k⟩ | ⟨j, hj, k⟩ | k + · rw [k] + obtain ⟨a, b, c, d, f⟩ := h u uo + exact ⟨fun q b' x' y z w e => hH _ _ (a q b' x' y z w e), fun q b' x' y e => hH _ _ (b q b' x' y e), + fun q g e => hH _ _ (c q g e), fun q sz e => hH _ _ (d q sz e), f⟩ + · rw [k] + exact ⟨(fun _ _ _ _ _ _ e => by cases e), (fun _ _ _ _ e => by cases e), (fun _ _ e => by cases e), + (fun _ _ e => by cases e), (fun _ e => by cases e)⟩ + · rw [k] + exact hnew j hj + · rw [k] + exact ⟨(fun _ _ _ _ _ _ e => by cases e), (fun _ _ _ _ e => by cases e), (fun _ _ e => by cases e), + (fun _ _ e => by cases e), (fun _ e => by cases e)⟩ + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtFullDeadA.lean b/proofs/Fl4ProofsA64/P3/UtFullDeadA.lean new file mode 100644 index 000000000..eaba2b4e8 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtFullDeadA.lean @@ -0,0 +1,73 @@ +/- +P3 (aarch64, A64-P3 Untyped, full-record post, part C): **`deadExt` and `bufOff` after a creation.** New VCPUs have no TCB, new +replies and SCs are on no stack, and a thread, VCPU, reply or pool dead after was dead before (`utRt_deadMonoA`). The +splits are hypotheses here, discharged at the assembly. + +No named premise. New theorems only. +-/ +import Fl4ProofsA64.P3.UtFullSlotA +import Fl4ProofsA64.P3.UtRtFrA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4SpecA64 Classical + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +theorem utRt_deadExtA {Z Z' : XSetsA} {OS OS' Rp Rp' : Set U64} {stack stack' : U64 → List U64} {s s' : S} + {σ σ' : State Unit} (h : DeadExtA st Z OS Rp stack s σ) + (mono : ∀ k q, DeadA st OS' k s' q → DeadA st OS k s q) + -- old VCPUs keep their object; new ones have no TCB + (hV : ∀ v ∈ Z'.V, (v ∈ Z.V ∧ σ'.kheap (specPtr v) = σ.kheap (specPtr v)) ∨ + ∀ y, σ'.kheap (specPtr v) = some (.archObj (.vcpu y)) → y.vcpuTcb = none) + -- old replies are on old stacks only; new ones on none + (hR : ∀ r ∈ Rp', ∀ c ∈ Z'.C, r ∈ stack' c → r ∈ Rp ∧ c ∈ Z.C ∧ r ∈ stack c) + (hA : σ'.archState.armAsidTable = σ.archState.armAsidTable) + -- old threads keep their VCPU word; new ones have none + (hT : ∀ t ∈ Z'.T, (t ∈ Z.T ∧ L.mem s' (wa t 3664#u64) = L.mem s (wa t 3664#u64)) ∨ + L.mem s' (wa t 3664#u64) = 0#u64) + -- old VCPUs keep their run-caller word; new ones have none + (hW : ∀ v ∈ Z'.V, (v ∈ Z.V ∧ L.mem s' (wa v 24#u64) = L.mem s (wa v 24#u64)) ∨ + L.mem s' (wa v 24#u64) = 0#u64) + -- old replies keep their object; new ones have no caller + (hY : ∀ r ∈ Rp', ∀ y, σ'.kheap (specPtr r) = some (.reply y) → + (r ∈ Rp ∧ σ.kheap (specPtr r) = some (.reply y)) ∨ y.replyTcb = none) : + DeadExtA st Z' OS' Rp' stack' s' σ' := by + refine ⟨fun v hv d y e => ?_, fun r hr d c hc g => ?_, fun i q e d => ?_, + utRt_tvcpuA h.tvcpu (fun t _ => mono .tcb t) hT, fun v hv d => ?_, fun r hr d y e => ?_⟩ + · rcases hV v hv with ⟨old, k⟩ | k + · exact h.vcpu v old (mono _ _ d) y (k.symm.trans e) + · exact k y e + · obtain ⟨old, oc, os⟩ := hR r hr c hc g + exact h.reply r old (mono _ _ d) c oc os + · rw [hA] at e + exact h.asid i q e (mono _ _ d) + · rcases hW v hv with ⟨old, k⟩ | k + · rw [k]; exact h.vcDeadRun v old (mono _ _ d) + · exact k + · rcases hY r hr y e with ⟨old, k⟩ | k + · exact h.replyTcb r old (mono _ _ d) y k + · exact k + +/-- `bufOff`: no new buffer words. `hbw`: a tracked buffer word is off every new `OS` slot, VSpace word, table page +and VCPU (the buffers lie in old frames, off the creation's window). -/ +theorem utRt_bufOffA {Z Z' : XSetsA} {OS OS' : Set U64} {s s' : S} {Bf : Set (U64 × Word)} + (h : BufOffA st Z OS s Bf) + (hos : ∀ b, BufW Bf b → InOS OS' b → InOS OS b) + (hvs : ∀ b, BufW Bf b → VsFootA Z'.Pp Z'.S1 Z'.Y (L.ksb s') b → VsFootA Z.Pp Z.S1 Z.Y (L.ksb s) b) + (hpg : ∀ b, BufW Bf b → ∀ p, SchedPageA st Z' OS' s' p → ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → b = wa p o → + SchedPageA st Z OS s p) + (hvc : ∀ b, BufW Bf b → ∀ v ∈ Z'.V, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → b = wa v o → v ∈ Z.V) + (hpin : ∀ bp ∈ Bf, BufPinA st Z'.T OS' s' bp.1) : + BufOffA st Z' OS' s' Bf := + ⟨fun b hb c => h.os b hb (hos b hb c), fun b hb c => h.vs b hb (hvs b hb c), + fun b hb p hp o o1 o2 e => h.page b hb p (hpg b hb p hp o o1 o2 e) o o1 o2 e, + fun b hb v hv o o1 o2 e => h.vcw b hb v (hvc b hb v hv o o1 o2 e) o o1 o2 e, hpin⟩ + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtFullIpcA.lean b/proofs/Fl4ProofsA64/P3/UtFullIpcA.lean new file mode 100644 index 000000000..8a6c1cddd --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtFullIpcA.lean @@ -0,0 +1,388 @@ +/- +P3 (aarch64, A64-P3 Untyped, full-record post, part B): **the record's IPC, reply and SC-linkage fields when new +objects join the sets**. + +The old objects are carried by the merged frame lemmas (words and spec objects kept); the new ones hold zero words and +the spec's default objects. Each lemma takes those two facts per field; the assembly discharges them from the +creation (`UtRtRdA`, `CrtE`, the per-type word lemmas) and the spec outcome (`UtNewObjA`). + +No named premise. New theorems only. +-/ +import Fl4ProofsA64.P3.UtRtRdA +import Fl4ProofsA64.P3.UtRtSchedA +import Fl4ProofsA64.P3.UtObjA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +set_option maxRecDepth 100000 + +/-- **The spec heap after a creation**: the new addresses `P` were free and hold `d`; every other object that is no +CNode is as before. -/ +structure UtNewObjA (σ σ' : State Unit) (P : ObjRef → Prop) (d : KernelObject) : Prop where + fresh : ∀ p, P p → σ.kheap p = none + new : ∀ p, P p → σ'.kheap p = some d + keep : ∀ p ko, σ.kheap p = some ko → (∀ r cs, ko ≠ .cnode r cs) → σ'.kheap p = some ko + back : ∀ p ko, σ'.kheap p = some ko → (∀ r cs, ko ≠ .cnode r cs) → σ.kheap p = some ko ∨ (P p ∧ ko = d) + glob : SpecGlobA σ σ' + um : σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory + +theorem UtNewObjA.epView {σ σ' : State Unit} {P : ObjRef → Prop} {d : KernelObject} (N : UtNewObjA σ σ' P d) : + Endp.KView σ σ' := + ⟨fun p x h => N.keep p _ h (fun _ _ e => by cases e), + fun p x h => ⟨x, N.keep p _ h (fun _ _ e => by cases e), rfl⟩, + fun p x h => ⟨x, N.keep p _ h (fun _ _ e => by cases e), rfl, rfl, rfl, rfl, rfl⟩, N.um⟩ + +theorem UtNewObjA.ntfnView {σ σ' : State Unit} {P : ObjRef → Prop} {d : KernelObject} (N : UtNewObjA σ σ' P d) : + Notif.KView σ σ' := + ⟨fun p x h => N.keep p _ h (fun _ _ e => by cases e), + fun p x h => ⟨x, N.keep p _ h (fun _ _ e => by cases e), rfl, rfl, rfl⟩⟩ + +theorem utQueue_monoA {m : U64 → U64} {T T' : Set U64} {l : tcbqueue.Link} {q : tcbqueue.Queue} {xs : List U64} + (h : QueueRel m T l q xs) (sub : T ⊆ T') : QueueRel m T' l q xs := + ⟨h.nodup, fun t ht => sub (h.mem t ht), h.head, h.last, h.seg⟩ + +theorem utEpObj_monoA {m : U64 → U64} {T T' : Set U64} {e : U64} {x : Endpoint} (h : EpObjRel m T e x) + (sub : T ⊆ T') : EpObjRel m T' e x := by + cases x with + | idleEP => exact ⟨h.1, utQueue_monoA h.2 sub⟩ + | sendEP q => + obtain ⟨h0, xs, h1, h2, h3, h4⟩ := h + exact ⟨h0, xs, h1, h2, utQueue_monoA h3 sub, h4⟩ + | recvEP q => + obtain ⟨h0, xs, h1, h2, h3, h4⟩ := h + exact ⟨h0, xs, h1, h2, utQueue_monoA h3 sub, h4⟩ + +theorem utTcbRelE_monoA {m : U64 → U64} {R R' : Set U64} {t : U64} {x : Tcb} (h : TcbRelE R m t x) + (sub : R ⊆ R') : TcbRelE R' m t x := + { h with recv := fun e r d hs => ⟨(h.recv e r d hs).1, fun hr => sub ((h.recv e r d hs).2 hr)⟩ } + +theorem utNtfnRel1_monoA {m : U64 → U64} {T T' : Set U64} {n : U64} {x : Notification} (h : NtfnRel1 m T n x) + (sub : T ⊆ T') : NtfnRel1 m T' n x := by + refine ⟨?_, h.bound, fun g => sub (h.bound_mem g), h.sc⟩ + have o := h.obj + cases hx : x.ntfnObj with + | idleNtfn => + rw [hx] at o + exact ⟨o.1, utQueue_monoA o.2 sub⟩ + | activeNtfn b => + rw [hx] at o + exact ⟨o.1, utQueue_monoA o.2.1 sub, o.2.2⟩ + | waitingNtfn q => + rw [hx] at o + obtain ⟨h0, xs, h1, h2, h3, h4⟩ := o + exact ⟨h0, xs, h1, h2, utQueue_monoA h3 sub, h4⟩ + +/-- **`EpStateRel` with new endpoints, replies and TCBs.** -/ +theorem utRt_epStateA {T E R Tn En Rn : Set U64} {Bf : Set (U64 × Word)} {mem mem' : U64 → U64} {σ σ' : State Unit} + (h : EpStateRel T E R Bf mem σ) (hP : EPlaced T E R Bf) (hm : Endp.FootEq T E R Bf mem mem') + (hk : Endp.KView σ σ') + (he : ∀ e ∈ En, σ'.kheap (specPtr e) = some (.endpoint defaultEp) ∧ mem' (epHeadA e) = 0#u64 ∧ + mem' (epTSA e) = 0#u64) + (hr : ∀ r ∈ Rn, σ'.kheap (specPtr r) = some (.reply defaultReply) ∧ mem' (Endp.rTcbA r) = 0#u64) + (ht : ∀ t ∈ Tn, ∃ y, σ'.kheap (specPtr t) = some (.tcb y) ∧ TcbRelE (R ∪ Rn) mem' t y) : + EpStateRel (T ∪ Tn) (E ∪ En) (R ∪ Rn) Bf mem' σ' := by + have h' := h.frame hP hm hk + refine ⟨fun e g => ?_, fun r g => ?_, fun t g => ?_, h'.buf⟩ + · rcases g with g | g + · obtain ⟨x, hx, hr'⟩ := h'.ep e g + exact ⟨x, hx, utEpObj_monoA hr' Set.subset_union_left⟩ + · obtain ⟨k, h0, h8⟩ := he e g + exact ⟨_, k, ut_epRelA mem' _ e h0 h8⟩ + · rcases g with g | g + · exact h'.reply r g + · obtain ⟨k, h0⟩ := hr r g + exact ⟨_, k, (ut_replyRelA mem' r h0).1⟩ + · rcases g with g | g + · obtain ⟨x, hx, hr'⟩ := h'.tcb t g + exact ⟨x, hx, utTcbRelE_monoA hr' Set.subset_union_left⟩ + · exact ht t g + +/-- **`NtfnStateRel` with new notifications and TCBs.** -/ +theorem utRt_ntfnStateA {N T Nn Tn : Set U64} {mem mem' : U64 → U64} {σ σ' : State Unit} + (h : NtfnStateRel N T mem σ) (hP : NPlaced T N) (hm : Notif.FootEq N T mem mem') (hk : Notif.KView σ σ') + (hn : ∀ n ∈ Nn, σ'.kheap (specPtr n) = some (.notification defaultNotification) ∧ mem' (epHeadA n) = 0#u64 ∧ + mem' (epTSA n) = 0#u64 ∧ mem' (nBoundA n) = 0#u64 ∧ mem' (nScA n) = 0#u64) + (ht : ∀ t ∈ Tn, ∃ y, σ'.kheap (specPtr t) = some (.tcb y) ∧ TcbRel1 mem' t y) : + NtfnStateRel (N ∪ Nn) (T ∪ Tn) mem' σ' := by + have h' := h.frame hP hm hk + refine ⟨fun n g => ?_, fun t g => ?_⟩ + · rcases g with g | g + · obtain ⟨x, hx, hr⟩ := h'.ntfn n g + exact ⟨x, hx, utNtfnRel1_monoA hr Set.subset_union_left⟩ + · obtain ⟨k, h0, h8, hb, hs⟩ := hn n g + exact ⟨_, k, ut_ntfnRelA mem' _ n h0 h8 hb hs⟩ + · rcases g with g | g + · exact h'.tcb t g + · exact ht t g + +/-- **`ReplyStackRelA` with new replies (on no stack) and new SCs (with empty stacks).** -/ +theorem utRt_replyStackA {R C Rn Cn : Set U64} {mem mem' : U64 → U64} {σ σ' : State Unit} + {scRef : U64 → ObjRef} {stack : U64 → List U64} (h : ReplyStackRelA mem R C σ scRef stack) + (hlink : ∀ r ∈ R, mem' (rNextA r) = mem (rNextA r) ∧ mem' (rPrevA r) = mem (rPrevA r)) + (hs : ∀ c ∈ C, ∀ x n, σ.kheap (scRef c) = some (.schedContext x n) → σ'.kheap (scRef c) = some (.schedContext x n)) + (hr : ∀ r ∈ R, ∀ x, σ.kheap (specPtr r) = some (.reply x) → σ'.kheap (specPtr r) = some (.reply x)) + (hRd : ∀ r ∈ Rn, r ∉ R) (hCd : ∀ c ∈ Cn, c ∉ C) + (hRx : ∀ r ∈ R, ∃ x, σ.kheap (specPtr r) = some (.reply x)) + (hrn : ∀ r ∈ Rn, σ'.kheap (specPtr r) = some (.reply defaultReply) ∧ mem' (rNextA r) = 0#u64 ∧ + mem' (rPrevA r) = 0#u64) + (hcn : ∀ c ∈ Cn, ∃ k, σ'.kheap (scRef c) = some (.schedContext defaultSchedContext k)) : + ReplyStackRelA mem' (R ∪ Rn) (C ∪ Cn) σ' scRef (fun c => if c ∈ Cn then [] else stack c) := by + have h' : ReplyStackRelA mem' R C σ' scRef stack := by + refine h.frame hlink (fun c hc x n hk => ⟨x, n, hs c hc x n hk, rfl⟩) (fun r hrR x' hk => ?_) + obtain ⟨x, hx⟩ := hRx r hrR + have := (hr r hrR x hx).symm.trans hk + cases this + exact ⟨x', hx, rfl⟩ + have old : ∀ c ∈ C, (if c ∈ Cn then ([] : List U64) else stack c) = stack c := fun c hc => + ite_eq_right (fun g => hCd c g hc) + have neu : ∀ c ∈ Cn, (if c ∈ Cn then ([] : List U64) else stack c) = [] := fun c hc => ite_eq_left hc + have split : ∀ c, c ∈ C ∪ Cn → ∀ r, r ∈ (if c ∈ Cn then ([] : List U64) else stack c) → + c ∈ C ∧ r ∈ stack c := by + rintro c (hc | hc) r g + · rw [old c hc] at g; exact ⟨hc, g⟩ + · rw [neu c hc] at g; cases g + have inR : ∀ c ∈ C, ∀ r ∈ stack c, r ∈ R := fun c hc => (h'.stackRel c hc).2.1 + refine ⟨fun c hc => ?_, fun c hc c' hc' r g g' => ?_, fun r hrR x hk p => ?_, fun r hrR hdet => ?_⟩ + · rcases hc with hc | hc + · rw [old c hc] + obtain ⟨a, b, c1, d⟩ := h'.stackRel c hc + exact ⟨a, fun r g => Or.inl (b r g), c1, d⟩ + · rw [neu c hc] + obtain ⟨k, hk⟩ := hcn c hc + exact ⟨⟨_, k, hk, rfl⟩, fun r g => (by cases g), List.nodup_nil, fun i hi => absurd hi (Nat.not_lt_zero i)⟩ + · obtain ⟨a, b⟩ := split c hc r g + obtain ⟨a', b'⟩ := split c' hc' r g' + exact h'.unique c a c' a' r b b' + · rcases hrR with hrR | hrR + · refine (h'.owner r hrR x hk p).trans ⟨?_, ?_⟩ + · rintro ⟨c, hc, e, hh⟩ + exact ⟨c, Or.inl hc, e, by rw [old c hc]; exact hh⟩ + · rintro ⟨c, hc | hc, e, hh⟩ + · rw [old c hc] at hh; exact ⟨c, hc, e, hh⟩ + · rw [neu c hc] at hh; cases hh + · rw [(hrn r hrR).1] at hk + cases hk + constructor + · intro g; cases g + · rintro ⟨c, hc, -, hh⟩ + have g := List.mem_of_head? hh + obtain ⟨a, b⟩ := split c hc r g + exact absurd (inR c a r b) (hRd r hrR) + · rcases hrR with hrR | hrR + · have d0 : ∀ c ∈ C, r ∉ stack c := fun c hc g => hdet c (Or.inl hc) (by rw [old c hc]; exact g) + obtain ⟨a, b, c1⟩ := h'.detached r hrR d0 + have lift : ∀ q ∈ R, (∀ c ∈ C, q ∉ stack c) → ∀ c ∈ C ∪ Cn, + q ∉ (if c ∈ Cn then ([] : List U64) else stack c) := fun q _ hq c hc g => by + obtain ⟨a', b'⟩ := split c hc q g + exact hq c a' b' + refine ⟨a, fun g => ?_, fun g => ?_⟩ + · obtain ⟨q1, q2, q3⟩ := b g + exact ⟨Or.inl q1, lift _ q1 q2, q3⟩ + · obtain ⟨q1, q2, q3⟩ := c1 g + exact ⟨Or.inl q1, lift _ q1 q2, q3⟩ + · obtain ⟨-, hnx, hpv⟩ := hrn r hrR + refine ⟨?_, fun g => absurd hnx g, fun g => absurd hpv g⟩ + rw [hnx]; decide + +theorem utRt_mcpA {T Tn : Set U64} {m m' : McState} {mem mem' : U64 → U64} {σ σ' : State Unit} (h : McpRelA T m mem σ) + (hid : m'.idleThreadOf = m.idleThreadOf) + (hm : ∀ t ∈ T, mem' (mcpA t) = mem (mcpA t) ∧ mem' (flagsA t) = mem (flagsA t)) (_hk : Endp.KView σ σ') + (hkeep : ∀ p x, σ.kheap p = some (.tcb x) → σ'.kheap p = some (.tcb x)) + (hn : ∀ t ∈ Tn, ∃ y, σ'.kheap (specPtr t) = some (.tcb y) ∧ y.tcbMcpriority = 0 ∧ mem' (mcpA t) = 0#u64 ∧ + mem' (flagsA t) = 0#u64 ∧ y.tcbFlags = ∅) : + McpRelA (T ∪ Tn) m' mem' σ' := by + rintro t (g | g) + · obtain ⟨x, hx, e, hf⟩ := h t g + exact ⟨x, hkeep _ _ hx, by rw [(hm t g).1]; exact e, + fun h' => by rw [hid] at h'; exact (hf h').keep (hm t g).2 rfl⟩ + · obtain ⟨y, hy, e, e0, f0, fe⟩ := hn t g + refine ⟨y, hy, by rw [e0, e]; rfl, fun _ => ?_⟩ + unfold FlagsOkA + rw [f0, fe] + ext f; simp [wordToTcbFlags] + +theorem utRt_yieldA {T C Tn Cn : Set U64} {m m' : McState} {mem mem' : U64 → U64} {σ σ' : State Unit} + (h : YieldRelA T C m mem σ) (hopt : ∀ w, optScA m' w = optScA m w) (hname : ∀ c, scNameA m' c = scNameA m c) + (hm : ∀ t ∈ T, mem' (yieldA t) = mem (yieldA t)) + (hkt : ∀ p x, σ.kheap p = some (.tcb x) → σ'.kheap p = some (.tcb x)) + (hks : ∀ p x n, σ.kheap p = some (.schedContext x n) → σ'.kheap p = some (.schedContext x n)) + (hsx : ∀ c ∈ C, ∃ y n, σ.kheap (scNameA m c) = some (.schedContext y n)) + (htn : ∀ t ∈ Tn, ∃ y, σ'.kheap (specPtr t) = some (.tcb y) ∧ y.tcbYieldTo = none ∧ mem' (yieldA t) = 0#u64) + (hcn : ∀ c ∈ Cn, ∃ k, σ'.kheap (scNameA m c) = some (.schedContext defaultSchedContext k)) : + YieldRelA (T ∪ Tn) (C ∪ Cn) m' mem' σ' := by + refine ⟨fun t g => ?_, fun t g hnz => ?_, fun c g y n hy p hp => ?_⟩ + · rcases g with g | g + · obtain ⟨x, hx, e⟩ := h.tcb t g + exact ⟨x, hkt _ _ hx, by rw [hopt, hm t g]; exact e⟩ + · obtain ⟨y, hy, e, e0⟩ := htn t g + exact ⟨y, hy, by rw [hopt, e0, e]; rfl⟩ + · rcases g with g | g + · rw [hm t g] at hnz ⊢ + obtain ⟨hc, y, n, hy, hf⟩ := h.toSc t g hnz + exact ⟨Or.inl hc, y, n, by rw [hname]; exact hks _ _ _ hy, hf⟩ + · obtain ⟨-, -, -, e0⟩ := htn t g + exact absurd e0 hnz + · rw [hname] at hy + rcases g with g | g + · obtain ⟨y0, n0, h0⟩ := hsx c g + have := (hks _ _ _ h0).symm.trans hy + cases this + obtain ⟨t, ht, e1, e2⟩ := h.frm c g _ _ h0 p hp + exact ⟨t, Or.inl ht, e1, by rw [hm t ht]; exact e2⟩ + · obtain ⟨k, hk⟩ := hcn c g + rw [hk] at hy + cases hy + cases hp + +theorem utRt_idleNoFaultA {m m' : McState} {σ σ' : State Unit} (h : IdleNoFaultA m σ) + (hi : ∀ c, m'.idleThreadOf c = m.idleThreadOf c) + (hkt : ∀ p x, σ.kheap p = some (.tcb x) → σ'.kheap p = some (.tcb x)) : IdleNoFaultA m' σ' := fun c hc => by + obtain ⟨x, hx, hf⟩ := h c hc + exact ⟨x, by rw [hi]; exact hkt _ _ hx, hf⟩ + +/-- **`BufNativeA` with new TCBs, whose buffer slots hold the null cap.** -/ +theorem utRt_bufNativeA {T Tn : Set U64} {mem mem' : U64 → U64} (h : BufNativeA T mem) + (hc : ∀ t ∈ T, capAtA mem' (slotAddrA (specPtr t) (BitVec.ofNat 64 2)) = + capAtA mem (slotAddrA (specPtr t) (BitVec.ofNat 64 2))) + (hn : ∀ t ∈ Tn, capAtA mem' (slotAddrA (specPtr t) (BitVec.ofNat 64 2)) = .Null) : + BufNativeA (T ∪ Tn) mem' := by + rintro t (g | g) + · rw [hc t g]; exact h t g + · rw [hn t g]; trivial + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- `lookup_ipc_buffer` reads the TCB's buffer word and its buffer slot's cap. -/ +theorem utRt_lookupBufA {s s' : S} {t : U64} (hw : L.mem s' (wa t 3512#u64) = L.mem s (wa t 3512#u64)) + (hc : capA st s' (sbWordA t) = capA st s (sbWordA t)) (b : Bool) : + msg.lookup_ipc_buffer st s' b t = msg.lookup_ipc_buffer st s b t := by + have hw' : L.mem s' (ibWordA t) = L.mem s (ibWordA t) := hw + unfold msg.lookup_ipc_buffer + rw [ipc_buffer_word, ipc_buffer_word, hw'] + have hs : obj.Tcb.slot t layout.tcb.SLOT_BUFFER = ok (sbWordA t) := by simp [obj.Tcb.slot, lift, sbWordA] + rw [hs]; simp only [bind_ok] + have e : obj.Slot.cap st (sbWordA t) s' = obj.Slot.cap st (sbWordA t) s := by + rw [capA_eq, capA_eq, hc] + rw [e] + +/-- **`BufCover` with new TCBs, which have no IPC frame.** -/ +theorem utRt_bufCoverA {T Tn : Set U64} {Bf : Set (U64 × Word)} {s s' : S} {σ σ' : State Unit} + (h : BufCover st T Bf s σ) + (hk : ∀ t ∈ T, ∀ x, σ'.kheap (specPtr t) = some (.tcb x) → σ.kheap (specPtr t) = some (.tcb x)) + (hw : ∀ t ∈ T, L.mem s' (wa t 3512#u64) = L.mem s (wa t 3512#u64)) + (hc : ∀ t ∈ T, capA st s' (sbWordA t) = capA st s (sbWordA t)) + (hn : ∀ t ∈ Tn, ∃ y, σ'.kheap (specPtr t) = some (.tcb y) ∧ y.tcbIpcframe = .nullCap) : + BufCover st (T ∪ Tn) Bf s' σ' := by + rintro t (g | g) x b sb w hx hb hl + · rw [utRt_lookupBufA (hw t g) (hc t g)] at hl + exact h t g x b sb w (hk t g x hx) hb hl + · obtain ⟨y, hy, e⟩ := hn t g + rw [hy] at hx + cases hx + unfold ipcBufOfA at hb + rw [e] at hb + cases hb + +end + +theorem utRt_bufAlA {T Tn : Set U64} {σ σ' : State Unit} (h : UtBufAlA T σ) + (hk : ∀ t ∈ T, ∀ x, σ'.kheap (specPtr t) = some (.tcb x) → σ.kheap (specPtr t) = some (.tcb x)) + (hn : ∀ t ∈ Tn, ∃ y, σ'.kheap (specPtr t) = some (.tcb y) ∧ y.tcbIpcBuffer = 0) : UtBufAlA (T ∪ Tn) σ' := by + rintro t (g | g) y hy + · exact h t g y (hk t g y hy) + · obtain ⟨z, hz, e⟩ := hn t g + rw [hz] at hy + cases hy + rw [e]; rfl + +/-- **`SInvXA` after a creation**: the new TCBs are inactive, the new SCs have no refills, and the new threads join +core 0. -/ +theorem utRt_sinvA {m m' : McState} {n : Nat} {σ σ' : State Unit} {P : ObjRef → Prop} {d : KernelObject} + (h : SInvXA σ m n) (O : UtNewObjA σ σ' P d) + (hcore : m'.curCore = m.curCore) (hidt : m'.idleThreadOf = m.idleThreadOf) (hids : m'.idleScOf = m.idleScOf) + (haff : ∀ t c, m'.tcbAffinity t = some c → m.tcbAffinity t = some c ∨ c = 0) + (hdt : ∀ x, d = .tcb x → x.tcbState ≠ .idleThreadState) + (hds : ∀ y k, d = .schedContext y k → Fl4SpecA64.scActive y = false) + (hdf : ∀ x, d = .tcb x → x.tcbFault = none) + (hI : SpecIrqA σ') : SInvXA σ' m' n := by + have kt : ∀ p x, σ.kheap p = some (.tcb x) → σ'.kheap p = some (.tcb x) := fun p x g => + O.keep p _ g (fun _ _ e => by cases e) + have ks : ∀ p y k, σ.kheap p = some (.schedContext y k) → σ'.kheap p = some (.schedContext y k) := fun p y k g => + O.keep p _ g (fun _ _ e => by cases e) + have n0 : 0 < n := Nat.lt_of_le_of_lt (Nat.zero_le _) h.mc.curCore_lt + refine ⟨⟨?_, ?_, ?_, ?_⟩, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, hI, ?_, ?_, ?_, ?_⟩ + · rw [hcore]; exact h.mc.curCore_lt + · rw [hidt]; exact h.mc.idleThread_inj + · rw [hids]; exact h.mc.idleSc_inj + · intro t c g + rcases haff t c g with g' | g' + · exact h.mc.affinity_lt t c g' + · rw [g']; exact n0 + · show σ'.idleThread = m'.idleThreadOf m'.curCore + rw [O.glob.idle, hidt, hcore]; exact h.view + · obtain ⟨x, hx⟩ := h.curTcb + exact ⟨x, by rw [O.glob.cur]; exact kt _ _ hx⟩ + · obtain ⟨x, hx, hs⟩ := h.idleTcb + exact ⟨x, by rw [O.glob.idle]; exact kt _ _ hx, hs⟩ + · intro t x g hs + rcases O.back t _ g (fun _ _ e => by cases e) with g' | ⟨-, e⟩ + · rw [hidt]; exact h.onlyIdle t x g' hs + · exact absurd hs (hdt x e.symm) + · obtain ⟨x, y, k, h1, h2, h3, h4⟩ := h.idleSc + exact ⟨x, y, k, by rw [O.glob.idle]; exact kt _ _ h1, h2, ks _ _ _ h3, by rw [O.glob.idle]; exact h4⟩ + · obtain ⟨y, k, g⟩ := h.curSc + exact ⟨y, k, by rw [O.glob.csc]; exact ks _ _ _ g⟩ + · intro hres dd p + rw [O.glob.cur, O.glob.rq]; exact h.ctQ (O.glob.act.symm.trans hres) dd p + · intro dd p c hc + rw [hidt, O.glob.rq]; exact h.idleQ dd p c hc + · intro dd p t g + rw [O.glob.rq] at g + obtain ⟨x, hx, r⟩ := h.rq dd p t g + exact ⟨x, kt _ _ hx, r⟩ + · intro t g + rw [O.glob.rls] at g + obtain ⟨x, hx⟩ := h.rls t g + exact ⟨x, kt _ _ hx⟩ + · intro p y k g ha + rcases O.back p _ g (fun _ _ e => by cases e) with g' | ⟨-, e⟩ + · exact h.rpos p y k g' ha + · rw [hds y k e.symm] at ha + cases ha + · intro t x' hx e hr + rw [O.glob.rls] at hr + obtain ⟨x, hx0⟩ := h.rls t hr + rcases O.back t _ hx (fun _ _ e => by cases e) with g | ⟨hP, -⟩ + · have := KernelObject.tcb.inj (Option.some.inj (hx0.symm.trans g)) + subst this + rw [O.glob.cons] + rw [O.glob.csc] at e + exact h.zc t x hx0 e hr + · exact absurd (O.fresh t hP) (by rw [hx0]; simp) + · intro t ht + rw [O.glob.rls] at ht + obtain ⟨x, hx, hr⟩ := h.rlsRun t ht + exact ⟨x, kt _ _ hx, hr⟩ + · intro hc y k hy a + rw [O.glob.cons] at hc + rw [O.glob.csc] at hy + rcases O.back _ _ hy (fun _ _ e => by cases e) with g | ⟨-, e⟩ + · rw [O.glob.time]; exact h.curScReady hc y k g a + · rw [hds y k e.symm] at a + cases a + · intro t x' hx f + rcases O.back t _ hx (fun _ _ e => by cases e) with g | ⟨-, e⟩ + · exact h.faultSt t x' g f + · exact absurd (hdf x' e.symm) f + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtFullJ5A.lean b/proofs/Fl4ProofsA64/P3/UtFullJ5A.lean new file mode 100644 index 000000000..c8b5e9520 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtFullJ5A.lean @@ -0,0 +1,201 @@ +/- +P3 (aarch64, A64-P3 Untyped, full-record post, part A): **`J5A` after a creation**, from `J5A` at the creation's +start (`s1`, after a restart or a reset). + +* **`utRt_j5LoopA`**: `utRetype_j5A` at P1's `CrtP`: the MDB facts of the grown ghost at the end of the creation. +* **`utRt_irqKeptA`**: a RAM creation (`utRetype_frameA`'s write frame) keeps the IRQ-state words. +* **`utRt_j5A`**: `J5A` at the new typed heap, the grown slots `s1aRtSlots` and the grown ghost, from the loop's + facts, the unchanged IRQ-state words and the creation's end (`UtEnd`). + +Hypotheses of `utRt_j5A`: the old `J5A`, `utRt_j5LoopA`'s outcome, `UtEnd`, the new bases free of the old typed +heap, destination slots old typed slots, the source non-null, `hirq` (RAM: `utRt_irqKeptA`). No named premise. New theorems only. +-/ +import Fl4ProofsA64.P3.UtJ5LoopA +import Fl4ProofsA64.P3.Stage3J5A +import Fl4ProofsA64.P3.UtRtKeptA +import Fl4ProofsA64.P3.UtRtSiteA +import Fl4ProofsA64.P1.S1aR2RetypeEnd + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + +/-- **The loop's facts at P1's `CrtP`** (module docstring). -/ +theorem utRt_j5LoopA (hA : P0.ArchTotal ar) {H1 : Heap} {Cs Sg OS1 : Set U64} {s1 : S} + {slot p sb fi1 : U64} {dev : Bool} {aligned nt us dest off win : U64} + (C : CrtP st H1 Cs Sg OS1 s1 slot p sb fi1 dev aligned nt us dest off win) {G : List (List U64)} + {par : U64 → Option U64} (hT : TreeA st OS1 G par s1) (hI : MdbInvA st G par s1) (hR : Fl4ProofsA64.P1.W7.R.ST.IrqRunA st G s1) + (hrev : Fl4ProofsA64.P1.W7.R.ST.revA st s1 slot = true) (a1 : A) : + untyped.retype st ar s1 a1 slot false aligned nt us dest off win dev ⦃ r => + MdbInvA st (utGA slot (djA dest off) G win.val) (utPA slot (djA dest off) par win.val) r.2.1 ∧ + Fl4ProofsA64.P1.W7.R.ST.IrqRunA st (utGA slot (djA dest off) G win.val) r.2.1 ∧ + (∀ y ∈ (utGA slot (djA dest off) G win.val).flatten, ∀ k, capA st r.2.1 y = .IrqHandler k → + y ∈ G.flatten ∧ capA st s1 y = .IrqHandler k) ⦄ := by + obtain ⟨hdev, hal, hfi, hA0, hAw, hinj, hcan, -⟩ := C.ar + obtain ⟨hSA, -, hK1⟩ := C.stz + obtain ⟨-, -, hV1, hUZ1, -⟩ := hSA + have hnA := nodeAt_aligned C.n0 (by rw [debug_val]; omega) (by rw [debug_val]) + have hnF := KsOffA.node hK1 C.core0 C.n0 (by rw [debug_val]; omega) + obtain ⟨-, -, hpf⟩ := ut_rangeA (capA_ut_b C.cap) (by + have := hT.ut.reg slot C.src (by rw [C.cap]; exact physA_ut _ _ _ _); rwa [C.cap] at this) + cases dev + · exact utRetype_j5A C.os hT hI hR a1 C.src C.cap hrev nt us dest off win aligned false C.hn C.hz hfi hA0 hAw + (Zq := fun s' j => ZTc st H1 s1 aligned (zA nt.val us) win.val s' j) + (fun s' a' j hj hq => create_object.hCOZ hA C.heap C.os hV1 C.src C.cap nt us win aligned false C.hn + (fun h => by cases h) hal hA0 hAw hpf hnA C.nc (hnF slot C.src p sb fi1 C.cap) s' a' j hj hq) + (fun _ _ _ h w => ZTc.capw C.heap C.os hV1 C.src C.cap hA0 hAw hnA C.nc h w) + (fun _ w => ZTc.start C.heap C.os hV1 C.src C.cap hA0 hAw hnA C.nc hUZ1 C.hd w) + C.dO C.dn C.dc hinj hcan C.hus + · obtain ⟨n1, n12, n14⟩ := hdev rfl + exact utRetype_j5A C.os hT hI hR a1 C.src C.cap hrev nt us dest off win aligned true C.hn C.hz hfi hA0 hAw + (Zq := fun _ _ => True) + (fun s' a' j hj _ => by + rw [create_object.pureA ar s' a' nt _ us true C.hn n1 n12 n14, WP.spec_ok] + exact ⟨rfl, fun _ _ => ⟨rfl, rfl, rfl, rfl⟩, trivial⟩) + (fun _ _ _ _ _ => trivial) (fun _ _ => trivial) C.dO C.dn C.dc hinj hcan C.hus + +/-- The byte address of slot `i` of the object at `b`, as a number. -/ +theorem utRt_slotValA {b : U64} {i : Nat} (h : b.val + 32 * i < 2 ^ 64) : + (slotAddrA (specPtr b) (BitVec.ofNat 64 i)).val = b.val + 32 * i := by + rw [slotAddrA_sp] + show (b.bv + BitVec.ofNat 64 i * 32#64).toNat = _ + have hb : b.bv.toNat = b.val := rfl + have e1 : (BitVec.ofNat 64 i).toNat = i := by rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + have e3 : (32#64 : BitVec 64).toNat = 32 := rfl + rw [BitVec.toNat_add, BitVec.toNat_mul, hb, e1, e3, Nat.mod_eq_of_lt (show i * 32 < 2 ^ 64 by omega), + Nat.mod_eq_of_lt (by omega)] + omega + +/-- **The new typed slots are P1's new slots** (module docstring). -/ +theorem utRt_newSlotsA {th : TypedHeapA} {dev : Bool} {nt us aligned win : U64} + (hn : nt.val < 15) (hus : us.val < 64) (hc47 : nt.val = 4 → us.val ≤ 47) + (hfit : aligned.val + win.val * 2 ^ (zA nt.val us).val ≤ 2 ^ 64) + (hfree : ∀ q, UtNewA dev nt us aligned win.val q → th q = none) (y : U64) : + (IsSlotA (utRtThA th dev nt us aligned win.val) y → IsSlotA th y ∨ y ∈ NewSl dev nt us aligned win) ∧ + (y ∈ NewSl dev nt us aligned win → IsSlotA (utRtThA th dev nt us aligned win.val) y) := by + have off : ∀ j, j < win.val → ∀ i : Nat, 32 * i + 32 ≤ 2 ^ (zA nt.val us).val → + (slotAddrA (specPtr (bjA aligned (zA nt.val us) j)) (BitVec.ofNat 64 i)).val = + (bjA aligned (zA nt.val us) j).val + 32 * i := fun j hj i hi => by + have h1 : (j + 1) * 2 ^ (zA nt.val us).val ≤ win.val * 2 ^ (zA nt.val us).val := Nat.mul_le_mul_right _ hj + rw [Nat.add_mul, Nat.one_mul] at h1 + have hz : 0 < 2 ^ (zA nt.val us).val := Nat.two_pow_pos _ + have e : (bjA aligned (zA nt.val us) j).val = aligned.val + j * 2 ^ (zA nt.val us).val := bjA_val (by omega) + exact utRt_slotValA (by omega) + have s4 : nt.val = 4 → ∀ i : Nat, i < 2 ^ us.val → 32 * i + 32 ≤ 2 ^ (zA nt.val us).val := fun h4 i hi => by + have ez : (zA nt.val us).val = us.val + 5 := by + rw [zA_valA nt us hn hus]; simp [zOfA, h4, Nat.add_comm] + rw [ez, pow_add]; omega + have s1 : nt.val = 1 → ∀ i : Nat, i < 7 → 32 * i + 32 ≤ 2 ^ (zA nt.val us).val := fun h1 i hi => by + have ez : (zA nt.val us).val = 12 := by rw [h1]; exact zA_1 us + rw [ez]; omega + constructor + · rintro ⟨q, hq⟩ + have isnew : ∀ k, utRtThA th dev nt us aligned win.val q = some k → th q ≠ some k → + UtNewA dev nt us aligned win.val q ∧ k = utKindA nt us := fun k h ho => by + rcases utRtTh_splitA h with ⟨-, o⟩ | n + · exact absurd o ho + · exact n + rcases hq with ⟨r, hk, i, hi, ey⟩ | ⟨hk, i, hi, ey⟩ + · by_cases ho : th q = some (.cnode r) + · exact Or.inl ⟨q, Or.inl ⟨r, ho, i, hi, ey⟩⟩ + obtain ⟨⟨hd, -, -, j, hj, eq⟩, ek⟩ := isnew _ hk ho + unfold utKindA at ek + split_ifs at ek with h1 h4 + cases ek + refine Or.inr ⟨hd, j, hj, i, Or.inr ⟨h4, hi⟩, ?_⟩ + rw [ey, eq] + exact off j hj i (s4 h4 i hi) + · by_cases ho : th q = some .tcb + · exact Or.inl ⟨q, Or.inr ⟨ho, i, hi, ey⟩⟩ + obtain ⟨⟨hd, -, -, j, hj, eq⟩, ek⟩ := isnew _ hk ho + have h1 : nt.val = 1 := by + unfold utKindA at ek + split_ifs at ek with h1 h4 + exact h1 + have hi7 : i < 7 := hi + refine Or.inr ⟨hd, j, hj, i, Or.inl ⟨h1, hi7⟩, ?_⟩ + rw [ey, eq] + exact off j hj i (s1 h1 i hi7) + · rintro ⟨hd, j, hj, i, ty, e⟩ + have hq : UtNewA dev nt us aligned win.val (bjA aligned (zA nt.val us) j) := + ⟨hd, by omega, by omega, j, hj, rfl⟩ + have ek : utRtThA th dev nt us aligned win.val (bjA aligned (zA nt.val us) j) = some (utKindA nt us) := + ite_eq_left hq + have ey : 32 * i + 32 ≤ 2 ^ (zA nt.val us).val → + y = slotAddrA (specPtr (bjA aligned (zA nt.val us) j)) (BitVec.ofNat 64 i) := fun h => + UScalar.eq_of_val_eq (by rw [off j hj i h]; exact e) + refine ⟨bjA aligned (zA nt.val us) j, ?_⟩ + rcases ty with ⟨h1, hi⟩ | ⟨h4, hi⟩ + · refine Or.inr ⟨?_, i, hi, ey (s1 h1 i hi)⟩ + rw [ek]; unfold utKindA; rw [ite_eq_left h1] + · refine Or.inl ⟨us.val, ?_, i, hi, ey (s4 h4 i hi)⟩ + rw [ek]; unfold utKindA; rw [ite_eq_right (by omega), ite_eq_left h4] + +/-- **`J5A` after a creation** (module docstring). -/ +theorem utRt_j5A {Z Z' : XSetsA} {H H1 H' : Heap} {Cs OS OS1 : Set U64} {s0 s1 q : S} {slot p sb : U64} + {dev : Bool} {lo hi : Nat} {aligned nt us dest off win : U64} {σ σ' : Fl4SpecA64.State Unit} + {m m' : Fl4SpecA64.McState} + (J : J5A st Z H1 OS1 s1 σ m) + (U : UtEnd st H Cs OS s0 slot p sb dev H1 H' OS1 s1 q lo hi aligned nt us dest off win) + (hth : Z'.th = utRtThA Z.th dev nt us aligned win.val) + (hG : Z'.G.chains = utGA slot (djA dest off) Z.G.chains win.val) + (hP : Z'.G.par = utPA slot (djA dest off) Z.G.par win.val) + (hus : us.val < 64) (hc47 : nt.val = 4 → us.val ≤ 47) (hsrc1 : capA st s1 slot ≠ .Null) + (hfree : ∀ q, UtNewA dev nt us aligned win.val q → Z.th q = none) + (hdst : ∀ j, j < win.val → IsSlotA Z.th (djA dest off j)) + (L5 : MdbInvA st (utGA slot (djA dest off) Z.G.chains win.val) (utPA slot (djA dest off) Z.G.par win.val) q ∧ + Fl4ProofsA64.P1.W7.R.ST.IrqRunA st (utGA slot (djA dest off) Z.G.chains win.val) q ∧ + (∀ y ∈ (utGA slot (djA dest off) Z.G.chains win.val).flatten, ∀ k, capA st q y = .IrqHandler k → + y ∈ Z.G.chains.flatten ∧ capA st s1 y = .IrqHandler k)) + (hirq : ∀ i : U64, i.val < 5376 → L.mem q (irqStAddrA (L.ksb s1) i) = L.mem s1 (irqStAddrA (L.ksb s1) i)) : + J5A st Z' H' (s1aRtSlots OS1 dev nt us aligned win) q σ' m' := by + obtain ⟨⟨-, -, hH⟩, sub, nul⟩ := J + obtain ⟨hI, hR, hh⟩ := L5 + have ns := utRt_newSlotsA (th := Z.th) U.A.hn hus hc47 (le_trans U.A.aw U.A.pf) hfree + refine ⟨?_, fun y hy => ?_, fun y hy hn => ?_⟩ + · rw [hG, hP] + refine ⟨hI, hR, fun y hy k e => ?_⟩ + obtain ⟨y0, e0⟩ := hh y hy k e + obtain ⟨h1, h2⟩ := hH y y0 k e0 + refine ⟨h1, ?_⟩ + rw [U.E.fr.1, hirq k h1]; exact h2 + · rw [hth] at hy + rcases (ns y).1 hy with o | n + · exact Or.inl (sub y o) + · exact Or.inr n + · rw [hth] at hn + have y1 : y ∈ OS1 := hy.resolve_right fun n => hn ((ns y).2 n) + have no : ¬ IsSlotA Z.th y := fun h => hn (utRt_isSlot_growA hfree h) + have c0 := nul y y1 no + have ys : y ≠ slot := fun e => hsrc1 (e ▸ c0) + have yd : ∀ j, j < win.val → y ≠ djA dest off j := fun j hj e => no (e ▸ hdst j hj) + rw [U.E.keep y y1 ys yd]; exact c0 + +/-- **A RAM creation keeps the IRQ-state words** (module docstring). -/ +theorem utRt_irqKeptA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} + {κ : U64 → U64} {n : Nat} {J5 J7 : JSlotA S} {s s' : S} {a : A} {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} {x p sb fi : U64} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (hx : x ∈ OS) (hc : capA st s x = .Untyped p sb fi false) {nt base z : U64} {len : Nat} + (h1 : p.val + fi.val ≤ base.val) (h2 : base.val + len * 2 ^ z.val ≤ p.val + 2 ^ sb.val) + (w : WritesIn st s s' (UtCrtWA st H OS s nt base z len)) : + ∀ i : U64, i.val < 5376 → L.mem s' (irqStAddrA (L.ksb s) i) = L.mem s (irqStAddrA (L.ksb s) i) := + fun i hi => by + have eI : irqStAddrA (L.ksb s) i = irqWordA (L.ksb s) i := rfl + rw [eI] + exact utRt_footKeptA J hx hc h1 h2 w (.irq i hi) + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtFullJ7A.lean b/proofs/Fl4ProofsA64/P3/UtFullJ7A.lean new file mode 100644 index 000000000..339c47203 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtFullJ7A.lean @@ -0,0 +1,722 @@ +/- +P3 (aarch64, A64-P3 Untyped, full-record post): **`J7A` after a creation**, for every object type (`HighVSpace` with +`KTab` [P]). + +The page-table set of the new `J7A` is A64 P1's `s1aRtPt`: the old pages off the creation's hole plus the new table +pages (`UtSchedNewA`, `utRtPt_memA`). `S1aR2` at it is P1's `S1aRtIn.r2`; `VrOK` is proved word by word: an old word's object survives off the hole, a new +word lies in the creation window. + +* `UtOffRgA`: at a childless source (the reset's case) `x`'s page is off the source's region. +* `UtJ7SplitA`: the new sets `Z'`, split into old members (off the region if childless) and the new objects. +* **`utRt_j7MadeA`**: `J7A (J7R2A st ar n)` at a creation end (P1's `S1aRtIn`) for every such `Z'`. +* **`utRt_j7SplitA`**: the split holds for `utRtZA` over any old sets `Z0 ⊆ Z` (`Z` itself, or `Z` after a drop) whose + members are off the region if the source is childless. +* **`utRt_j7TopA`**: P1's creation top at the record's facts, with `HeapKsOff` and either an unchanged state + (`H`, `OS`, `s`: the old `J7A` holds as it is) or a creation end with `J7A` for every split. + +Hypotheses: the decode-site facts of `utRt_p1TopA` (`DecPre`, `SysPlace`, `SymL` [J], `A64FrameMasks` [P], +`ArchTotal` [P]), the old `J7A`, and `HeapKsOff` at the old heap (`JEntA.ko`). No new named premise (`KTab` [P] is +`J7R2A`'s). New definitions and theorems only. +-/ +import Fl4ProofsA64.P1.S1aKtRetype +import Fl4ProofsA64.P3.UtFullUbA +import Fl4ProofsA64.P3.UtRtJ7A +import Fl4ProofsA64.P3.UtRtEndA +import Fl4ProofsA64.P3.UtRtPostA +import Fl4ProofsA64.P1.S1aKtRetype +import Fl4ProofsA64.P1.S1aUbW + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Classical + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **The page at `x` is off the source's region if the source is childless** (module docstring). -/ +def UtOffRgA (st : machine.Store S) [LawfulStore st] (s : S) (slot p sb x : U64) : Prop := + (∃ v, cdt.ensure_no_children st s slot = ok (.Ok v)) → x.val + 4096 ≤ p.val ∨ p.val + 2 ^ sb.val ≤ x.val + +/-- **The sets after a creation, split into old and new members** (module docstring). -/ +structure UtJ7SplitA (st : machine.Store S) [LawfulStore st] (Z Z' : XSetsA) (OS' : Set U64) (s q : S) + (slot p sb nt us aligned : U64) (win : Nat) : Prop where + cs : Z'.Cs = Z.Cs + pp : ∀ x ∈ Z'.Pp, x ∈ Z.Pp ∧ UtOffRgA st s slot p sb x + s1 : ∀ x ∈ Z'.S1, (x ∈ Z.S1 ∧ UtOffRgA st s slot p sb x) ∨ x ∈ UtSchedNewA nt us aligned win + t2 : ∀ x ∈ Z'.T2, (x ∈ Z.T2 ∧ UtOffRgA st s slot p sb x) ∨ x ∈ UtSchedNewA nt us aligned win + x : ∀ r ∈ Z'.X, (r ∈ Z.X ∧ UtOffRgA st s slot p sb r ∧ UtOffRgA st s slot p sb (wa r 4096#u64)) ∨ + (r ∈ UtSchedNewA nt us aligned win ∧ wa r 4096#u64 ∈ UtSchedNewA nt us aligned win) + y : ∀ r ∈ Z'.Y, (r ∈ Z.Y ∧ UtOffRgA st s slot p sb r ∧ UtOffRgA st s slot p sb (wa r 4096#u64)) ∨ + (r ∈ UtSchedNewA nt us aligned win ∧ wa r 4096#u64 ∈ UtSchedNewA nt us aligned win) + v : ∀ v ∈ Z'.V, (v ∈ Z.V ∧ UtOffRgA st s slot p sb v) ∨ + (nt.val = 12 ∧ ∃ j < win, v = bjA aligned (zA nt.val us) j) + vcc : VcCap st Z'.V OS' q + +/-- A new table page names its type. -/ +theorem utSchedNew_typeA {nt us aligned : U64} {win : Nat} {x : U64} (h : x ∈ UtSchedNewA nt us aligned win) : + (nt.val = 8 ∨ nt.val = 11 ∨ nt.val = 13 ∨ nt.val = 14) ∧ 0 < win := by + obtain ⟨j, hj, h⟩ := h + refine ⟨?_, by omega⟩ + rcases h with ⟨h, -⟩ | ⟨h, -⟩ | ⟨h, -⟩ <;> omega + +/-- **P1's footprint after a Retype**: the old pages off the hole and the new table pages. -/ +theorem utRtPt_memA {Pt : Set U64} {lo hi : Nat} {aligned nt us win x : U64} : + x ∈ s1aRtPt Pt lo hi aligned nt us win ↔ x ∈ PtOffR Pt lo hi ∨ x ∈ UtSchedNewA nt us aligned win.val := by + constructor + · rintro ((((h | h) | h) | h) | h) + · exact Or.inl h + · obtain ⟨t, j, hj, e⟩ := h + refine Or.inr ⟨j, hj, ?_⟩ + rcases t with t | t + · exact Or.inl ⟨Or.inr t, e⟩ + · exact Or.inr (Or.inr ⟨t, Or.inl e⟩) + · obtain ⟨b, ⟨t, j, hj, e⟩, h⟩ := h + refine Or.inr ⟨j, hj, Or.inr (Or.inl ⟨t, ?_⟩)⟩ + rcases h with h | ⟨-, h⟩ + · exact Or.inl (h.trans e) + · exact Or.inr (e ▸ h) + · obtain ⟨t, j, hj, e⟩ := h + exact Or.inr ⟨j, hj, Or.inl ⟨Or.inl t, e⟩⟩ + · obtain ⟨b, ⟨t, j, hj, e⟩, h⟩ := h + refine Or.inr ⟨j, hj, Or.inr (Or.inr ⟨t, ?_⟩)⟩ + rcases h with h | ⟨-, h⟩ + · exact Or.inl (h.trans e) + · exact Or.inr (e ▸ h) + · rintro (h | ⟨j, hj, h⟩) + · exact Or.inl (Or.inl (Or.inl (Or.inl h))) + rcases h with ⟨t | t, e⟩ | ⟨t, e⟩ | ⟨t, e⟩ + · exact Or.inl (Or.inr ⟨t, j, hj, e⟩) + · exact Or.inl (Or.inl (Or.inl (Or.inr ⟨Or.inl t, j, hj, e⟩))) + · refine Or.inl (Or.inl (Or.inr ⟨_, ⟨t, j, hj, rfl⟩, ?_⟩)) + exact e.imp id fun e => ⟨rfl, e⟩ + · refine Or.inr ⟨_, ⟨t, j, hj, rfl⟩, ?_⟩ + exact e.imp id fun e => ⟨rfl, e⟩ + +/-- Two heap objects that hold one word are one. -/ +theorem utObj_uniqA {H : Heap} (hH : HeapOK H) {q r b : U64} {k k' : HKind} (h : H q = some k) (h' : H r = some k') + (a : InObj q k b) (c : InObj r k' b) : r = q ∧ k' = k := by + have e : r = q := by + by_contra ne + have d := hH.disj r k' q k h' h ne + unfold InObj at a c + generalize (2 : Nat) ^ k.bits = A at d a + generalize (2 : Nat) ^ k'.bits = B at d c + omega + subst e + exact ⟨rfl, Option.some.inj (h'.symm.trans h)⟩ + +end + +section made +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + {H H1 H' : Heap} {Cs OS OS1 U0 Ac : Set U64} {s s1 q : S} {ia ie lo hi : Nat} + {slot p sb fi aligned nt us dest off win : U64} {dev : Bool} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (E : S1aRtIn st H Cs OS U0 Ac s ia ie slot p sb fi dev extra H1 H' OS1 s1 q lo hi aligned nt us dest off win) +include E + +/-- An old object's word survives, off the hole. -/ +theorem utEnd_wordKeptA {r b : U64} {k : HKind} (h : H r = some k) (hin : InObj r k b) + (res : lo ≤ b.val → b.val < hi → H1 = H) : H' r = some k ∧ ¬ (lo ≤ b.val ∧ b.val < hi) := by + have k1 : H1 r = some k := by + by_cases g : lo ≤ b.val ∧ b.val < hi + · rw [res g.1 g.2]; exact h + · rcases E.kept.kept r k h with a | ⟨a, c⟩ + · exact a + · unfold InObj at hin + exact absurd (show lo ≤ b.val ∧ b.val < hi by omega) g + refine ⟨E.creation.grow r k k1, fun g => ?_⟩ + unfold InObj at hin + rcases E.creation.hole r k k1 with o | o <;> omega + +/-- An old page off the source's region survives, off the hole. -/ +theorem utEnd_pageKeptA {Pt : Set U64} (ph : PtHG H Pt) {x : U64} (hx : x ∈ Pt) (off : UtOffRgA st s slot p sb x) : + x ∈ PtOffR Pt lo hi := by + obtain ⟨-, r, z, h, a, c⟩ := ph x hx + have k1 : H1 r = some (.other z) := by + rcases E.reset with e | ⟨-, v, el, eh⟩ + · rw [e]; exact h + · rcases E.kept.kept r _ h with g | ⟨g1, g2⟩ + · exact g + · simp only [HKind.bits] at g2 + rcases off v with o | o <;> omega + have o := E.creation.hole r _ k1 + simp only [HKind.bits] at o + exact ⟨hx, by omega⟩ + +/-- **An old word keeps `om`, `sg` and `sc`** in the grown heap. -/ +theorem utEnd_oldWordA {b : U64} (om : b ∈ H.omega) (res : lo ≤ b.val → b.val < hi → H1 = H) : + b ∈ H'.omega ∧ (b ∉ H.frame → b ∉ H'.frame) ∧ + ((∀ c ∈ H.scs, ¬ InSC c (H.scBits c) b) → ∀ c ∈ H'.scs, ¬ InSC c (H'.scBits c) b) ∧ + ¬ (lo ≤ b.val ∧ b.val < hi) := by + obtain ⟨al, r, k, h, kk, hin⟩ := om + obtain ⟨h', out⟩ := utEnd_wordKeptA E h hin res + have hH' := E.heapOK + refine ⟨⟨al, r, k, h', kk, hin⟩, fun nf f => nf ?_, fun nsc c hc ic => ?_, out⟩ + · obtain ⟨-, r2, k2, h2, kk2, hin2⟩ := f + obtain ⟨e1, e2⟩ := utObj_uniqA hH' h' h2 hin hin2 + subst e1 e2 + exact ⟨al, r2, k2, h, kk2, hin⟩ + · obtain ⟨z, hz⟩ := hc + rw [Heap.scBits_eq hz] at ic + have ic' : InObj c (.sc z) b := ic + obtain ⟨e1, e2⟩ := utObj_uniqA hH' h' hz hin ic' + subst e1 e2 + refine nsc c ⟨z, h⟩ ?_ + rw [Heap.scBits_eq h] + exact ic + +/-- A new table page lies in the window, hence in the hole and in the source's region; the source is RAM. -/ +theorem utEnd_newPageA {x : U64} (hx : x ∈ UtSchedNewA nt us aligned win.val) : + dev = false ∧ aligned.val ≤ x.val ∧ x.val + 4096 ≤ aligned.val + win.val * 2 ^ (zA nt.val us).val ∧ + lo ≤ aligned.val ∧ aligned.val + win.val * 2 ^ (zA nt.val us).val ≤ hi ∧ p.val ≤ aligned.val ∧ + aligned.val + win.val * 2 ^ (zA nt.val us).val ≤ p.val + 2 ^ sb.val := by + obtain ⟨ty, pos⟩ := utSchedNew_typeA hx + have ram := (E.newObj pos ty).1 + have fit : aligned.val + win.val * 2 ^ (zA nt.val us).val ≤ 2 ^ 64 := E.creation.A.aw.trans E.creation.A.pf + obtain ⟨a, b⟩ := utSchedNew_inWinA fit x hx + obtain ⟨c, d⟩ := E.creation.whR ram + exact ⟨ram, a, b, c, d, E.creation.A.pa, E.creation.A.aw⟩ + +/-- A RAM source's region is off the kernel-state block. -/ +theorem utEnd_ksRegionA (ram : dev = false) : + (L.ksb s).val + 788736 ≤ p.val ∨ p.val + 2 ^ sb.val ≤ (L.ksb s).val := by + have R := UtDecP.of_live E.pre E.sys.sys E.place E.source E.sys.ln E.sys.vr E.sys.ly + subst ram + exact (R.stz.2.2 slot R.src p sb fi R.cap).2 + +/-- **The user words are off a creation window** (childless: off the whole region; else the window is above the free +index). -/ +theorem utEnd_ubWinA {Ub : Set U64} (K : S1aCapCol st H OS Ub s) (al : ∀ b ∈ Ub, Aligned b) (ram : dev = false) + (pos : 0 < win.val) : ∀ b ∈ Ub, ¬ (aligned.val ≤ b.val ∧ b.val < aligned.val + win.val * 2 ^ (zA nt.val us).val) := by + intro b hb g + have R := UtDecP.of_live E.pre E.sys.sys E.place E.source E.sys.ln E.sys.vr E.sys.ly + obtain ⟨reset, args⟩ := E.window.resolve_left (by omega) + have ar := UtArgs.arithA R.ty R.ext R.cap R.utv args + have cap : capA st s slot = .Untyped p sb fi false := ram ▸ E.sourceCap + have aw := E.creation.A.aw + have pa := E.creation.A.pa + by_cases ch : ∃ v, cdt.ensure_no_children st s slot = ok (.Ok v) + · obtain ⟨⟨⟩, ch⟩ := ch + exact E.sys.offNoCh E.place.os K.uf al E.place.src cap ch b hb (by omega) + · have nr : reset = false := by + cases reset + · rfl + obtain ⟨x, -, -, idx, dep, val, -, -, -, -, iff, -⟩ := args + exact absurd (iff.mp rfl) ch + subst nr + have lo' := ar.2.2.2.1 + simp only [Bool.false_eq_true, ite_false] at lo' + exact (E.sys.userOffFr.toUb K.uf al).1 slot E.place.src p sb fi cap b hb (by omega) + +/-- **`J7A` at a creation end** (module docstring). -/ +theorem utRt_j7MadeA {A : Type} {ar : machine.Arch A} {Z Z' : XSetsA} {Ub Pt Pp Tb Hr Te Kt Vc'' : Set U64} + {Lv Re bits : U64 → Nat} {n : U64} {σ' : Fl4SpecA64.State Unit} {m' : Fl4SpecA64.McState} + (vr : VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt) + (C : S1aArmCol st H Cs OS Z.V Ub s) (K : S1aCapCol st H OS Ub s) (al : ∀ b ∈ Ub, Aligned b) + (sched : ∀ x, SchedPageA st Z OS s x → x ∈ Pt) + (apart : ∀ b, VcW Z.V b → ∀ o : U64, o.val < 223488 → wa (L.ksb s) o ≠ b) + (h : S1aR2 st H OS Pt Pp Tb Hr Te Kt Lv Re bits n s) (sep : S1aKtUt st OS Kt s) + (pools : PoolNamed st (s1aRtSlots OS1 dev nt us aligned win) q) + (C' : S1aArmCol st H' Cs (s1aRtSlots OS1 dev nt us aligned win) Vc'' Ub q) + (K' : S1aCapCol st H' (s1aRtSlots OS1 dev nt us aligned win) Ub q) + (devs : DevFrAl st (s1aRtSlots OS1 dev nt us aligned win) q) + (hic : HiCap511 st (s1aRtSlots OS1 dev nt us aligned win) q) + (R2 : S1aR2 st H' (s1aRtSlots OS1 dev nt us aligned win) (s1aRtPt Pt lo hi aligned nt us win) Pp + (s1aRtTb Tb Pt lo hi aligned nt us win) (HvRt st (s1aRtSlots OS1 dev nt us aligned win) q) + (s1aRtTe H' Te bits lo hi) Kt (s1aRtLv Lv aligned nt us win) Re bits n q) + (hcs : Cs = Z.Cs) (F : UtJ7SplitA st Z Z' (s1aRtSlots OS1 dev nt us aligned win) s q slot p sb nt us aligned win.val) + -- record edit #5: the user words, and the kernel-table levels + (frub : FrUb st Ub OS s) (vcub : ∀ b, VcW Z.V b → b ∉ Ub) {KLv : U64 → Nat} (ktab : KTab ar Kt KLv) + (hik : S1aHiK st OS Kt KLv s) + (words : ∀ b, VsR (L.ksb s) Pt b → S1aRetypeWordOwner st H OS Kt s b → L.mem q b = L.mem s b) + (init1 : ∀ x ∈ OS, capA st s x = .Null → + (cdt.ensure_no_children st s slot = ok (.Ok ()) → x.val + 32 ≤ p.val ∨ p.val + 2 ^ sb.val ≤ x.val) → + ∀ t m asid, capA st q x = .HighVSpace t m asid → S1aHighInit st (Kt1 ar) q t) : + J7A st (J7R2A st ar n) Z' H' (s1aRtSlots OS1 dev nt us aligned win) q σ' m' := by + have hH' := E.heapOK + have ksb : L.ksb q = L.ksb s := E.ksb + have ksw := E.sys.ly.ksw + -- an old VCPU of the new set + have oldV {v : U64} (hv : v ∈ Z.V) (off : UtOffRgA st s slot p sb v) : + H' v = some (.other 12) ∧ ∀ b : U64, v.val ≤ b.val → b.val < v.val + 4096 → ¬ (lo ≤ b.val ∧ b.val < hi) := by + have hv12 := C.vh v hv + have k1 : H1 v = some (.other 12) := by + rcases E.reset with e | ⟨-, w, el, eh⟩ + · rw [e]; exact hv12 + · rcases E.kept.kept v _ hv12 with g | ⟨g1, g2⟩ + · exact g + · simp only [HKind.bits] at g2 + rcases off w with o | o <;> omega + refine ⟨E.creation.grow v _ k1, fun b a c => ?_⟩ + have o := E.creation.hole v _ k1 + simp only [HKind.bits] at o + omega + -- a new VCPU + have newV {v : U64} (hv : nt.val = 12 ∧ ∃ j < win.val, v = bjA aligned (zA nt.val us) j) : + dev = false ∧ H' v = some (.other 12) ∧ + ∀ b : U64, v.val ≤ b.val → b.val < v.val + 4096 → + (lo ≤ b.val ∧ b.val < hi) ∧ (p.val ≤ b.val ∧ b.val < p.val + 2 ^ sb.val) := by + obtain ⟨e12, j, hj, rfl⟩ := hv + have ram : dev = false := by + obtain ⟨reset, args⟩ := E.window.resolve_left (show ¬ win.val = 0 by omega) + cases h : dev + · rfl + · rw [h] at args + rcases UtArgs.devK args with g | g | g | g <;> omega + have ez : (zA nt.val us).val = 12 := by rw [e12]; simp [zA] + have kd : newKind nt.val us.val = some (.other 12) := by rw [e12]; simp [newKind] + have obj := E.creation.hn ram j hj _ kd + have pos := (utEnd_bjA E.creation hj).1 + have step : (j + 1) * 2 ^ (zA nt.val us).val ≤ win.val * 2 ^ (zA nt.val us).val := Nat.mul_le_mul_right _ hj + rw [Nat.add_mul, Nat.one_mul] at step + rw [ez] at step pos + obtain ⟨c, d⟩ := E.creation.whR ram + have aw := E.creation.A.aw + have pa := E.creation.A.pa + rw [ez] at d aw + exact ⟨ram, obj, fun b x y => by omega⟩ + -- a frame word of the new slots is an old user word, or a word of a new frame in the window + have newFr : ∀ b ∈ FrameWords st (s1aRtSlots OS1 dev nt us aligned win) q, b ∈ Ub ∨ + (dev = false ∧ aligned.val ≤ b.val ∧ b.val < aligned.val + win.val * 2 ^ (zA nt.val us).val ∧ + (nt.val ≠ 8 ∧ nt.val ≠ 11 ∧ nt.val ≠ 12 ∧ nt.val ≠ 13 ∧ nt.val ≠ 14) ∧ + ∃ r z, H' r = some (.other z) ∧ r.val ≤ b.val ∧ b.val < r.val + 2 ^ z) := by + rintro b ⟨bal, y, hy, ptr, size, rights, x1, x2, x3, x4, bits, hc, hb, b1, b2⟩ + rcases E.capAt hy hc (fun e => by cases e) (fun _ _ _ _ e => by cases e) with ⟨yo, -, was⟩ | ⟨j, hj, -, new⟩ + · exact Or.inl (frub y yo ptr size rights x1 x2 x3 x4 was bits hb b bal b1 b2) + have h := new.symm + have ram := newCapA_frame_dev E.creation.A.hn h + obtain ⟨-, kind⟩ := newCapA_frame E.creation.A.hn h + obtain ⟨eptr, ebits⟩ := newCapA_frame_z E.creation.A.hn h hb + have obj := E.creation.hn ram j hj _ (kind bits hb) + have pos := (utEnd_bjA E.creation hj).1 + have step : (j + 1) * 2 ^ (zA nt.val us).val ≤ win.val * 2 ^ (zA nt.val us).val := Nat.mul_le_mul_right _ hj + rw [Nat.add_mul, Nat.one_mul] at step + rw [eptr] at b1 b2 + have b2' := b2 + rw [ebits] at b2' + have ty : nt.val ≠ 8 ∧ nt.val ≠ 11 ∧ nt.val ≠ 12 ∧ nt.val ≠ 13 ∧ nt.val ≠ 14 := by + refine ⟨?_, ?_, ?_, ?_, ?_⟩ <;> intro e <;> rw [e] at h <;> simp [newCapA] at h + exact Or.inr ⟨ram, by omega, by omega, ty, _, _, obj, b1, b2⟩ + -- the words of an old table page or of the ASID table + have old {b : U64} (hb : VsR (L.ksb s) Pt b) (res : lo ≤ b.val → b.val < hi → H1 = H) : + Aligned b ∧ b ∈ H'.omega ∧ b ∉ H'.frame ∧ (∀ c ∈ H'.scs, ¬ InSC c (H'.scBits c) b) ∧ + ¬ InOS (s1aRtSlots OS1 dev nt us aligned win) b ∧ ¬ VcW Z'.V b ∧ + b ∉ Ub ∪ FrameWords st (s1aRtSlots OS1 dev nt us aligned win) q ∧ + ∀ o : U64, o.val < 223488 → wa (L.ksb s) o ≠ b := by + obtain ⟨om, sg, sc, out⟩ := utEnd_oldWordA E (vr.om b hb) res + have nub : b ∉ Ub ∪ FrameWords st (s1aRtSlots OS1 dev nt us aligned win) q := by + rintro (u | fw) + · exact vr.ub b hb u + rcases newFr b fw with u | ⟨ram, a1, a2, -⟩ + · exact vr.ub b hb u + obtain ⟨c1, c2⟩ := E.creation.whR ram + exact out (by omega) + refine ⟨vr.al b hb, om, sg (vr.sg b hb), sc (vr.sc b hb), ?_, ?_, nub, vr.ks b hb⟩ + · rintro ⟨y, (hy | hy), a, c⟩ + · exact vr.os b hb ⟨y, E.creation.os y hy, a, c⟩ + · obtain ⟨ram, j, hj, i, ty, e⟩ := hy + have posj := (utEnd_bjA E.creation hj).1 + have step : (j + 1) * 2 ^ (zA nt.val us).val ≤ win.val * 2 ^ (zA nt.val us).val := Nat.mul_le_mul_right _ hj + rw [Nat.add_mul, Nat.one_mul] at step + have size : 32 * i + 32 ≤ 2 ^ (zA nt.val us).val := by + rcases ty with ⟨t, hi7⟩ | ⟨t, hin⟩ + · have : (zA nt.val us).val = 12 := by rw [t]; simp [zA] + rw [this]; omega + · have : (zA nt.val us).val = us.val + 5 := by + have u := E.kept.us64 + rw [t] + simp [zA] + scalar_tac + rw [this, Nat.pow_add]; omega + obtain ⟨c1, c2⟩ := E.creation.whR ram + exact out (by omega) + · rintro ⟨v, hv, o, ho, h8, e⟩ + rcases F.v v hv with ⟨vo, off⟩ | nv + · exact vr.vc b hb ⟨v, vo, o, ho, h8, e⟩ + · obtain ⟨-, obj, inw⟩ := newV nv + have fit := hH'.fits v _ obj + simp only [HKind.bits] at fit + have bv : b.val = v.val + o.val := by rw [e]; exact wadd_val (by omega) + exact out (inw b (by omega) (by omega)).1 + have kept : ∀ b, VsR (L.ksb s) (PtOffR Pt lo hi) b → VsR (L.ksb s) Pt b ∧ (lo ≤ b.val → b.val < hi → H1 = H) := by + rintro b (⟨x, hx, o, ho, h8, e⟩ | a) + · refine ⟨Or.inl ⟨x, hx.1, o, ho, h8, e⟩, fun c d => ?_⟩ + have fit := h.footprint.fit E.place.heap x hx.1 + have bv : b.val = x.val + o.val := by rw [e]; exact wadd_val (by omega) + rcases hx.2 with g | g <;> omega + · refine ⟨Or.inr a, fun c d => ?_⟩ + rcases E.reset with e | ⟨ram, -, el, eh⟩ + · exact e + obtain ⟨o, o1, o2, -, e⟩ := a + have bv : b.val = (L.ksb s).val + o.val := by rw [e]; exact wadd_val (by omega) + rcases utEnd_ksRegionA E ram with r | r <;> omega + have all : ∀ b, VsR (L.ksb s) (s1aRtPt Pt lo hi aligned nt us win) b → + Aligned b ∧ b ∈ H'.omega ∧ b ∉ H'.frame ∧ (∀ c ∈ H'.scs, ¬ InSC c (H'.scBits c) b) ∧ + ¬ InOS (s1aRtSlots OS1 dev nt us aligned win) b ∧ ¬ VcW Z'.V b ∧ + b ∉ Ub ∪ FrameWords st (s1aRtSlots OS1 dev nt us aligned win) q ∧ + ∀ o : U64, o.val < 223488 → wa (L.ksb s) o ≠ b := by + intro b hb + have split : VsR (L.ksb s) (PtOffR Pt lo hi) b ∨ + ∃ x ∈ UtSchedNewA nt us aligned win.val, ∃ o : U64, o.val < 4096 ∧ o.val % 8 = 0 ∧ b = wa x o := by + rcases hb with ⟨x, hx, o, ho⟩ | a + · rcases utRtPt_memA.mp hx with g | g + · exact Or.inl (Or.inl ⟨x, g, o, ho⟩) + · exact Or.inr ⟨x, g, o, ho⟩ + · exact Or.inl (Or.inr a) + rcases split with g | ⟨x, hx, o, ho, h8, e⟩ + · obtain ⟨a, b'⟩ := kept b g + exact old a b' + obtain ⟨ram, a1, a2, l1, l2, r1, r2⟩ := utEnd_newPageA E hx + obtain ⟨xal, r, z, obj, rl, rh⟩ := R2.footprint x (utRtPt_memA.mpr (Or.inr hx)) + have fit := hH'.fits r _ obj + simp only [HKind.bits] at fit + have bv : b.val = x.val + o.val := by rw [e]; exact wadd_val (by omega) + have bal : Aligned b := by + show b.val % 8 = 0 + omega + have hin : InObj r (.other z) b := ⟨by omega, by simp only [HKind.bits]; omega⟩ + obtain ⟨om, sg⟩ := HeapOK.otherW hH' obj bal hin.1 hin.2 + have win' : aligned.val ≤ b.val ∧ b.val < aligned.val + win.val * 2 ^ (zA nt.val us).val := by omega + obtain ⟨ty, pos⟩ := utSchedNew_typeA hx + have nub : b ∉ Ub ∪ FrameWords st (s1aRtSlots OS1 dev nt us aligned win) q := by + rintro (u | fw) + · exact utEnd_ubWinA E K al ram pos b u win' + rcases newFr b fw with u | ⟨-, -, -, ty', -⟩ + · exact utEnd_ubWinA E K al ram pos b u win' + omega + refine ⟨bal, om, sg, fun c hc ic => ?_, ?_, ?_, nub, fun o' ho' e' => ?_⟩ + · obtain ⟨z', hz⟩ := hc + rw [Heap.scBits_eq hz] at ic + have ic' : InObj c (.sc z') b := ic + cases (utObj_uniqA hH' obj hz hin ic').2 + · rintro ⟨y, (hy | hy), c, d⟩ + · have slotw := E.creation.os1.slot y hy + have yfit := E.creation.os1.fit y hy + have yal : y.val % 8 = 0 := slotw.1 + have b8 : b.val % 8 = 0 := bal + have ov : (⟨BitVec.ofNat 64 (b.val - y.val)⟩ : U64).val = b.val - y.val := by + show (BitVec.ofNat 64 (b.val - y.val)).toNat = _ + rw [BitVec.toNat_ofNat] + exact Nat.mod_eq_of_lt (by omega) + have w := slotw.2 ⟨BitVec.ofNat 64 (b.val - y.val)⟩ (by rw [ov]; omega) (by rw [ov]; omega) + have eb : wa y ⟨BitVec.ofNat 64 (b.val - y.val)⟩ = b := by + apply UScalar.eq_of_val_eq + rw [wadd_val (by rw [ov]; have := b.hBounds; scalar_tac), ov] + omega + rw [eb] at w + have off1 {r' : U64} {k : HKind} (h1 : H1 r' = some k) (i : InObj r' k b) : False := by + unfold InObj at i + rcases E.creation.hole r' k h1 with g | g <;> omega + rcases w with ⟨t, ht, K', hK, e'⟩ | ⟨⟨-, r', k, h1, -, i⟩, -⟩ + · have tf := E.creation.hp1.fits t _ ht + have tbits : 224 ≤ 2 ^ (HKind.tcb).bits := by simp [HKind.bits] + have : b.val = t.val + K'.val := by rw [e']; exact wadd_val (by omega) + exact off1 ht ⟨by omega, by omega⟩ + · exact off1 h1 i + · obtain ⟨-, j, -, i, t, -⟩ := hy + rcases t with ⟨t, -⟩ | ⟨t, -⟩ <;> omega + · rintro ⟨v, hv, o', ho', h8', e'⟩ + rcases F.v v hv with ⟨vo, off⟩ | ⟨e12, -⟩ + · obtain ⟨obj', out⟩ := oldV vo off + have vf := hH'.fits v _ obj' + simp only [HKind.bits] at vf + have : b.val = v.val + o'.val := by rw [e']; exact wadd_val (by omega) + exact out b (by omega) (by omega) (by omega) + · omega + · have : b.val = (L.ksb s).val + o'.val := by rw [← e']; exact wadd_val (by omega) + rcases utEnd_ksRegionA E ram with k | k <;> omega + have vr' : VrOK H'.omega H'.frame H'.scs H'.scBits (s1aRtSlots OS1 dev nt us aligned win) Z'.V + (Ub ∪ FrameWords st (s1aRtSlots OS1 dev nt us aligned win) q) (L.ksb q) (s1aRtPt Pt lo hi aligned nt us win) := by + rw [ksb] + exact ⟨fun b hb => (all b hb).1, fun b hb => (all b hb).2.1, fun b hb => (all b hb).2.2.1, + fun b hb => (all b hb).2.2.2.1, fun b hb => (all b hb).2.2.2.2.1, fun b hb => (all b hb).2.2.2.2.2.1, + fun b hb => (all b hb).2.2.2.2.2.2.1, fun b hb => (all b hb).2.2.2.2.2.2.2⟩ + have vh : VcH H' Z'.V := fun v hv => + (F.v v hv).elim (fun g => (oldV g.1 g.2).1) (fun g => (newV g).2.1) + have apart' : ∀ b, VcW Z'.V b → ∀ o : U64, o.val < 223488 → wa (L.ksb q) o ≠ b := by + rintro b ⟨v, hv, o', ho', h8', e'⟩ o ho + rw [ksb] + rcases F.v v hv with ⟨vo, -⟩ | nv + · exact apart b ⟨v, vo, o', ho', h8', e'⟩ o ho + · obtain ⟨ram, obj, inw⟩ := newV nv + have vf := hH'.fits v _ obj + simp only [HKind.bits] at vf + have bv : b.val = v.val + o'.val := by rw [e']; exact wadd_val (by omega) + intro e + have : b.val = (L.ksb s).val + o.val := by rw [← e]; exact wadd_val (by omega) + have g := (inw b (by omega) (by omega)).2 + rcases utEnd_ksRegionA E ram with k | k <;> omega + -- the page inclusion + have inOld {x : U64} (g : SchedPageA st Z OS s x) (off : UtOffRgA st s slot p sb x) : + x ∈ s1aRtPt Pt lo hi aligned nt us win := + utRtPt_memA.mpr (Or.inl (utEnd_pageKeptA E h.footprint (sched x g) off)) + have inNew {x : U64} (g : x ∈ UtSchedNewA nt us aligned win.val) : x ∈ s1aRtPt Pt lo hi aligned nt us win := + utRtPt_memA.mpr (Or.inr g) + have sched' : ∀ x, SchedPageA st Z' (s1aRtSlots OS1 dev nt us aligned win) q x → + x ∈ s1aRtPt Pt lo hi aligned nt us win := by + intro x hx + unfold SchedPageA at hx + rcases hx with m | m | m | m | ⟨r, m, e⟩ | m | ⟨r, m, e⟩ | ⟨u, hu, c⟩ + · exact inOld (Or.inl (F.pp x m).1) (F.pp x m).2 + · exact (F.s1 x m).elim (fun g => inOld (Or.inr (Or.inl g.1)) g.2) inNew + · exact (F.t2 x m).elim (fun g => inOld (Or.inr (Or.inr (Or.inl g.1))) g.2) inNew + · exact (F.x x m).elim (fun g => inOld (Or.inr (Or.inr (Or.inr (Or.inl g.1)))) g.2.1) (fun g => inNew g.1) + · rcases F.x r m with g | g + · exact inOld (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨r, g.1, e⟩))))) (e ▸ g.2.2) + · exact inNew (e ▸ g.2) + · exact (F.y x m).elim (fun g => inOld (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl g.1)))))) g.2.1) + (fun g => inNew g.1) + · rcases F.y r m with g | g + · exact inOld (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨r, g.1, e⟩))))))) (e ▸ g.2.2) + · exact inNew (e ▸ g.2) + have shape : capA st q u ≠ .Null ∧ ∀ a b d e, capA st q u ≠ .Untyped a b d e := by + rcases c with ⟨_, _, _, c⟩ | ⟨_, _, c⟩ | ⟨_, _, c⟩ | ⟨_, _, _, c, -⟩ | ⟨_, _, c, -⟩ <;> rw [c] <;> + exact ⟨(fun e => by cases e), (fun _ _ _ _ e => by cases e)⟩ + rcases E.capAt hu rfl shape.1 shape.2 with ⟨uo, -, same⟩ | ⟨j, hj, -, new⟩ + · rw [← same] at c + have g : SchedPageA st Z OS s x := Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ⟨u, uo, c⟩)))))) + have xp := sched x g + have fit := h.footprint.fit E.place.heap x xp + refine utRtPt_memA.mpr (Or.inl ⟨xp, ?_⟩) + rcases c with ⟨m, a, v, c⟩ | ⟨m, a, c⟩ | ⟨a, v, c⟩ | ⟨r, a, v, c, e⟩ | ⟨r, a, c, e⟩ + · exact E.namedOff h uo (Or.inl ⟨m, a, v, c⟩) + · exact E.namedOff h uo (Or.inr (Or.inl ⟨m, a, c⟩)) + · exact E.namedOff h uo (Or.inr (Or.inr (Or.inr (Or.inr ⟨_, _, _, _, c⟩)))) + · obtain ⟨obj, gr⟩ := h.eptRoots u uo _ _ _ _ _ c + obtain ⟨z, -, ho, -, out⟩ := E.tableObj h uo (k := (2, r)) (by rw [c]; rfl) + rw [obj] at ho + cases ho + have rf := E.place.heap.fits r _ obj + simp only [HKind.bits] at rf + have xv : x.val = r.val + 4096 := by rw [e]; exact wadd_val (by change r.val + 4096 < 2 ^ 64; omega) + simp only at out + omega + · exact E.namedOff h uo (Or.inr (Or.inr (Or.inr (Or.inl ⟨r, _, _, c, e⟩)))) + · rw [new] at c + obtain ⟨c1, c2, c3, c4⟩ := utNewCap_schedA E.creation.A.hn (bjA aligned (zA nt.val us) j) us dev + refine inNew ⟨j, hj, ?_⟩ + rcases c with ⟨_, _, _, c⟩ | ⟨_, _, c⟩ | ⟨_, _, c⟩ | ⟨r, _, _, c, e⟩ | ⟨r, _, c, e⟩ + · obtain ⟨a, b⟩ := c1 _ _ _ _ c + exact Or.inl ⟨Or.inr a, b⟩ + · obtain ⟨a, b⟩ := c2 _ _ _ c + exact Or.inl ⟨Or.inl a, b⟩ + · obtain ⟨a, b⟩ := c3 _ _ _ _ _ c + exact Or.inr (Or.inl ⟨a, Or.inl b⟩) + · obtain ⟨a, b⟩ := c3 _ _ _ _ _ c + exact Or.inr (Or.inl ⟨a, Or.inr (b ▸ e)⟩) + · obtain ⟨a, b⟩ := c4 _ _ _ c + exact Or.inr (Or.inr ⟨a, Or.inr (b ▸ e)⟩) + have sep' : S1aKtUt st (s1aRtSlots OS1 dev nt us aligned win) Kt q := + E.creation.s1aKtUt E.kept E.sys.ly.ksw E.sourceCap sep + -- the grown user words + have ksuN : ∀ b ∈ Ub ∪ FrameWords st (s1aRtSlots OS1 dev nt us aligned win) q, + b.val < (L.ksb q).val ∨ (L.ksb q).val + 788736 ≤ b.val := by + rintro b (u | fw) + · exact C'.kb.ksU b u + rcases newFr b fw with u | ⟨ram, a1, a2, -⟩ + · exact C'.kb.ksU b u + have aw := E.creation.A.aw + have pa := E.creation.A.pa + rw [ksb] + rcases utEnd_ksRegionA E ram with k | k <;> omega + have ntN : NtOffU H' (Ub ∪ FrameWords st (s1aRtSlots OS1 dev nt us aligned win) q) := by + rintro b (u | fw) n0 hn ⟨c1, c2⟩ + · exact C'.nt b u n0 hn ⟨c1, c2⟩ + rcases newFr b fw with u | ⟨-, -, -, -, r, z, obj, r1, r2⟩ + · exact C'.nt b u n0 hn ⟨c1, c2⟩ + have i1 : InObj r (.other z) b := ⟨r1, by simpa only [HKind.bits] using r2⟩ + have i2 : InObj n0 .ntfn b := ⟨c1, by simpa [HKind.bits] using c2⟩ + cases (utObj_uniqA hH' obj hn i1 i2).2 + have poolN : UbPoolT st (Ub ∪ FrameWords st (s1aRtSlots OS1 dev nt us aligned win) q) q := by + intro i nz b hb ⟨c1, c2⟩ + have v := AtOff.v i + have inPp := R2.core.stage1.tbl (AtOff i) v.1 v.2.1 v.2.2 nz + have inPt := R2.place.pools inPp + have obj := pool_of_poolObj C'.case.pobj nz + have fit := hH'.fits _ _ obj + have al12 := hH'.align _ _ obj + simp only [HKind.bits] at fit al12 + have bal : b.val % 8 = 0 := by + rcases hb with u | fw + · exact al b u + · exact fw.1 + generalize he : L.mem q (wa (L.ksb q) (AtOff i)) = e at c1 c2 inPt fit al12 + have ov : (⟨BitVec.ofNat 64 (b.val - e.val)⟩ : U64).val = b.val - e.val := by + show (BitVec.ofNat 64 (b.val - e.val)).toNat = _ + rw [BitVec.toNat_ofNat] + exact Nat.mod_eq_of_lt (by have := b.hBounds; scalar_tac) + have eb : b = wa e ⟨BitVec.ofNat 64 (b.val - e.val)⟩ := by + apply UScalar.eq_of_val_eq + rw [wadd_val (by rw [ov]; have := b.hBounds; scalar_tac), ov] + omega + have vsr : VsR (L.ksb s) (s1aRtPt Pt lo hi aligned nt us win) b := + Or.inl ⟨e, inPt, _, by rw [ov]; omega, by rw [ov]; omega, eb⟩ + exact (all b vsr).2.2.2.2.2.2.1 hb + have C'' : S1aArmCol st H' Z'.Cs (s1aRtSlots OS1 dev nt us aligned win) Z'.V + (Ub ∪ FrameWords st (s1aRtSlots OS1 dev nt us aligned win) q) q := by + rw [F.cs, ← hcs] + exact ⟨C'.case, ⟨C'.kb.ks, C'.kb.ksIn, C'.kb.slotKs, ksuN, C'.kb.hT⟩, ntN, poolN, vh⟩ + have K'' : S1aCapCol st H' (s1aRtSlots OS1 dev nt us aligned win) + (Ub ∪ FrameWords st (s1aRtSlots OS1 dev nt us aligned win) q) q := + ⟨fun b hb => hb.elim (K'.uf b) (fun fw => fw.2), K'.uo, K'.fo⟩ + have frub' : FrUb st (Ub ∪ FrameWords st (s1aRtSlots OS1 dev nt us aligned win) q) + (s1aRtSlots OS1 dev nt us aligned win) q := + fun y hy ptr size rights x1 x2 x3 x4 hc bits hb b bal b1 b2 => + Or.inr ⟨bal, y, hy, ptr, size, rights, x1, x2, x3, x4, bits, hc, hb, b1, b2⟩ + have al' : ∀ b ∈ Ub ∪ FrameWords st (s1aRtSlots OS1 dev nt us aligned win) q, Aligned b := + fun b hb => hb.elim (al b) (fun fw => fw.1) + have vcub' : ∀ b, VcW Z'.V b → b ∉ Ub ∪ FrameWords st (s1aRtSlots OS1 dev nt us aligned win) q := by + rintro b ⟨v, hv, o', ho', h8', e'⟩ hb + rcases F.v v hv with ⟨vo, off⟩ | nv + · obtain ⟨obj', out⟩ := oldV vo off + have vf := hH'.fits v _ obj' + simp only [HKind.bits] at vf + have bv : b.val = v.val + o'.val := by rw [e']; exact wadd_val (by omega) + have nu : b ∉ Ub := vcub b ⟨v, vo, o', ho', h8', e'⟩ + rcases hb with u | fw + · exact nu u + rcases newFr b fw with u | ⟨ram, a1, a2, -⟩ + · exact nu u + obtain ⟨c1, c2⟩ := E.creation.whR ram + exact out b (by omega) (by omega) (by omega) + · obtain ⟨ram, obj, -⟩ := newV nv + obtain ⟨e12, j, hj, ev⟩ := nv + have vf := hH'.fits v _ obj + simp only [HKind.bits] at vf + have bv : b.val = v.val + o'.val := by rw [e']; exact wadd_val (by omega) + have ez : (zA nt.val us).val = 12 := by rw [e12]; simp [zA] + have pos := (utEnd_bjA E.creation hj).1 + have step : (j + 1) * 2 ^ (zA nt.val us).val ≤ win.val * 2 ^ (zA nt.val us).val := Nat.mul_le_mul_right _ hj + rw [Nat.add_mul, Nat.one_mul] at step + rw [← ev] at pos + rw [ez] at step pos + have win' : aligned.val ≤ b.val ∧ b.val < aligned.val + win.val * 2 ^ (zA nt.val us).val := by + rw [ez]; omega + rcases hb with u | fw + · exact utEnd_ubWinA E K al ram (by omega) b u win' + rcases newFr b fw with u | ⟨-, -, -, ty', -⟩ + · exact utEnd_ubWinA E K al ram (by omega) b u win' + omega + have hiK' : S1aHiK st (s1aRtSlots OS1 dev nt us aligned win) Kt KLv q := + E.hiK h hik words init1 (Kt1.sub ktab) + exact ⟨_, _, vr', pools, C'', K'', F.vcc, devs, hic, sched', apart', frub', al', vcub', + _, _, _, _, _, _, _, _, R2, sep', KLv, ktab, hiK'⟩ + +/-- **The split for the assembly's sets** (`utRtZA` over old sets `Z0` inside `Z`; module docstring). `vc0`: the +caps at the decode's start name VCPUs of `Z0`. -/ +theorem utRt_j7SplitA {Z Z0 : XSetsA} {G' : MdbGhostA} {Tn Cn Vn : Set U64} + (cs : Z0.Cs = Z.Cs) (pp : Z0.Pp ⊆ Z.Pp) (s1' : Z0.S1 ⊆ Z.S1) (t2 : Z0.T2 ⊆ Z.T2) (x : Z0.X ⊆ Z.X) + (y : Z0.Y ⊆ Z.Y) (v : Z0.V ⊆ Z.V) + (off : ∀ r, (r ∈ Z0.Pp ∨ r ∈ Z0.S1 ∨ r ∈ Z0.T2 ∨ r ∈ Z0.V) → UtOffRgA st s slot p sb r) + (off2 : ∀ r, (r ∈ Z0.X ∨ r ∈ Z0.Y) → UtOffRgA st s slot p sb r ∧ UtOffRgA st s slot p sb (wa r 4096#u64)) + (vc0 : VcCap st Z0.V OS s) + (hVn : ∀ r ∈ Vn, nt.val = 12 ∧ r ∈ utBasesA aligned (zA nt.val us) win.val) + (hVn12 : nt.val = 12 → ∀ r ∈ utBasesA aligned (zA nt.val us) win.val, r ∈ Vn) : + UtJ7SplitA st Z (utRtZA Z0 G' nt us aligned win.val Tn Cn Vn) (s1aRtSlots OS1 dev nt us aligned win) s q + slot p sb nt us aligned win.val where + cs := cs + pp r h := ⟨pp h, off r (Or.inl h)⟩ + s1 r h := by + rcases h with h | ⟨t, j, hj, e⟩ + · exact Or.inl ⟨s1' h, off r (Or.inr (Or.inl h))⟩ + · exact Or.inr ⟨j, hj, Or.inl ⟨Or.inl t, e⟩⟩ + t2 r h := Or.inl ⟨t2 h, off r (Or.inr (Or.inr (Or.inl h)))⟩ + x r h := by + rcases h with h | ⟨t, j, hj, e⟩ + · exact Or.inl ⟨x h, off2 r (Or.inl h)⟩ + · exact Or.inr ⟨⟨j, hj, Or.inr (Or.inl ⟨t, Or.inl e⟩)⟩, ⟨j, hj, Or.inr (Or.inl ⟨t, Or.inr (e ▸ rfl)⟩)⟩⟩ + y r h := by + rcases h with h | ⟨t, j, hj, e⟩ + · exact Or.inl ⟨y h, off2 r (Or.inr h)⟩ + · exact Or.inr ⟨⟨j, hj, Or.inr (Or.inr ⟨t, Or.inl e⟩)⟩, ⟨j, hj, Or.inr (Or.inr ⟨t, Or.inr (e ▸ rfl)⟩)⟩⟩ + v r h := by + rcases h with h | h + · exact Or.inl ⟨v h, off r (Or.inr (Or.inr (Or.inr h)))⟩ + · exact Or.inr (hVn r h) + vcc := by + intro u hu r hc + rcases E.capAt hu hc (fun e => by cases e) (fun _ _ _ _ e => by cases e) with ⟨uo, -, old⟩ | ⟨j, hj, -, new⟩ + · exact Or.inl (vc0 u uo r old) + · obtain ⟨e12, e⟩ := utNewCap_vcpuA new.symm + exact Or.inr (hVn12 e12 r ⟨j, hj, e⟩) + +end made + +section top +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **P1's creation top at the record's facts, with `J7A`** (module docstring). -/ +theorem utRt_j7TopA {A : Type} {ar : machine.Arch A} (hA : P0.ArchTotal ar) {H : Heap} {Z : XSetsA} + {OS U Ac Pp0 Tb0 : Set U64} {Lv0 : U64 → Nat} {ia ie : Nat} {s : S} {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} {n : U64} + (I : SysInvA st H Z.Cs OS U Ac Pp0 Tb0 Lv0 ia ie s) {extra : Aeneas.Std.Array obj.Slot 3#usize} + {slot p sb fi : U64} {dev : Bool} + (D : DecPre st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits U Ac s (.Untyped p sb fi dev) extra) + (P : SysPlace st H Z.Cs H.frame H.replies H.ntfns OS s extra slot) (Y : SymL st H Z.Cs s) + (hcap : obj.Slot.cap st slot s = ok (.Untyped p sb fi dev)) (hfm : A64FrameMasks st s) + (J : J7A st (J7R2A st ar n) Z H OS s σ m) (ko : HeapKsOff H (L.ksb s)) (a : A) (lbl len buf : U64) : + untyped.decode st ar s a lbl len slot (.Untyped p sb fi dev) buf extra ⦃ _ q _ => + ∃ (H' : Heap) (OS' Vn Vc'' Ub : Set U64), ArmQIVAt st H Z.Cs OS ia ie s H' OS' Vn q ∧ TabKeep st s q ∧ + Vc'' ⊆ Z.V ∪ Vn ∧ VcCap st Vc'' OS' q ∧ S1aArmCol st H' Z.Cs OS' Vc'' Ub q ∧ S1aCapCol st H' OS' Ub q ∧ + HeapKsOff H' (L.ksb s) ∧ DevFrAl st OS' q ∧ HiCap511 st OS' q ∧ + ((H' = H ∧ OS' = OS ∧ q = s) ∨ + ∃ (H1 : Heap) (OS1 : Set U64) (s1 : S) (lo hi : Nat) (aligned nt us dest off win : U64), + S1aRtIn st H Z.Cs OS U Ac s ia ie slot p sb fi dev extra H1 H' OS1 s1 q lo hi aligned nt us dest off win ∧ + OS' = s1aRtSlots OS1 dev nt us aligned win ∧ + ∀ (Z' : XSetsA) (σ' : Fl4SpecA64.State Unit) (m' : Fl4SpecA64.McState), + UtJ7SplitA st Z Z' OS' s q slot p sb nt us aligned win.val → + J7A st (J7R2A st ar n) Z' H' OS' q σ' m') ⦄ := by + obtain ⟨Pt, Ub0, vr0, pools, C0, K0, vc, frame, hic0, sched, apart, frub, alub, vcub, Pp, Tb, Hr, Te, Kt, Lv, Re, bits, + h, sep, KLv, ktab, hik⟩ := J + have vr := utAlUb_vrA vr0 + have C := utAlUb_colA C0 + have K := utAlUb_capA K0 + have al := utAlUb_alA Ub0 + have B := I.sysl + have R := UtDecP.of_live D B.sys P hcap B.ln B.vr B.ly + have offState : dev = false → + (L.ksb s).val + 788736 ≤ p.val ∨ p.val + 2 ^ sb.val ≤ (L.ksb s).val := fun ram => by + subst ram + exact (R.stz.2.2 slot R.src p sb fi R.cap).2 + refine WP.spec_mono (WP.spec_and + (untyped.decode.s1aRtVJH hA D P B Y hcap hfm vr pools C K al vc a lbl len buf + (fun H' O q => (H' = H ∧ O = OS ∧ q = s ∧ HeapKsOff H' (L.ksb s) ∧ DevFrAl st O q ∧ + (HrW st (HvRt st OS s) s q → HiCap511 st O q)) ∨ + ∃ (H1 : Heap) (OS1 : Set U64) (s1 : S) (lo hi : Nat) (aligned nt us dest off win : U64), + S1aRtIn st H Z.Cs OS U Ac s ia ie slot p sb fi dev extra H1 H' OS1 s1 q lo hi aligned nt us dest off win ∧ + O = s1aRtSlots OS1 dev nt us aligned win ∧ HeapKsOff H' (L.ksb s) ∧ DevFrAl st O q ∧ + (HrW st (HvRt st OS s) s q → HiCap511 st O q)) + (Or.inl ⟨rfl, rfl, rfl, ko, frame, fun _ => hic0⟩) ?_) + (WP.spec_and (untyped.decode.keepOwnedVsWords hA D P B hcap hfm vr sep a lbl len buf) + (WP.spec_and (untyped.decode.tabK hA D P B hcap hfm vr a lbl len buf) + (WP.spec_and (untyped.decode.keepActivePaths hA D P B hcap hfm vr h sep a lbl len buf) + (WP.spec_and (untyped.decode.newHighInit hA ktab D P B hcap hfm a lbl len buf) + (WP.spec_and (untyped.decode.keepOldHi hA D P B hcap hfm vr h.footprint + (show HvPt st Pt OS s from fun y hy t mp asid cap => + h.highFootprint ⟨t, h.highRoots ▸ ⟨y, hy, mp, asid, cap⟩, Or.inl rfl⟩) + a lbl len buf) (untyped.decode.newHighInit hA (Kt1.ktab ktab) D P B hcap hfm a lbl len buf))))))) ?_ + · intro H1 H' OS1 s1 q lo hi aligned nt us dest off win E X window reset init + have rt : S1aRtIn st H Z.Cs OS U Ac s ia ie slot p sb fi dev extra H1 H' OS1 s1 q lo hi aligned nt us dest off win := + ⟨D, P, B, hcap, E, X, window, reset⟩ + exact Or.inr ⟨H1, OS1, s1, lo, hi, aligned, nt, us, dest, off, win, rt, rfl, E.heapKsOff X offState ko, + UtEnd.devFrN D P B hcap E X window frame, fun keep => E.hiCap511 X B.ly.ksw init keep hic0⟩ + · rintro ⟨_, q, _⟩ ⟨⟨H', OS', Vn, Vc', ⟨h1, h2, h3, h4, h5, h6⟩, made⟩, words, writes, ⟨roots, paths⟩, fresh, keep, fresh1⟩ + have after : SysL st H' Z.Cs OS' ia ie q := h1.1 + obtain ⟨_, _, _, kinds, _, _⟩ := after.sys.stz.1.1.mdbS + rcases made with ⟨eH, eO, eq, ks', devs, hic⟩ | ⟨H1, OS1, s1, lo, hi, aligned, nt, us, dest, off, win, rt, eO, ks', devs, hic⟩ + · exact ⟨H', OS', Vn, Vc', _, h1, h2, h3, h4, h5, h6, ks', devs, hic keep, Or.inl ⟨eH, eO, eq⟩⟩ + refine ⟨H', OS', Vn, Vc', _, h1, h2, h3, h4, h5, h6, ks', devs, hic keep, + Or.inr ⟨H1, OS1, s1, lo, hi, aligned, nt, us, dest, off, win, rt, eO, fun Z' σ' m' F => ?_⟩⟩ + subst eO + have tab : TabKeep st s q := TabKeep.ofW vr writes fun _ h => h + have tables := UtEnd.ptCapHG D P B hcap rt.creation rt.kept rt.window rt.reset h.pageTables h.footprint + have R2 := rt.r2 h sep words tab roots paths fresh kinds after.sys.fr (hic keep) ks' tables + exact utRt_j7MadeA rt vr C K al sched apart h sep (h1.2.2.2.2.2.2.1 pools) h5 h6 devs (hic keep) R2 rfl F + (utAlUb_frA frub) (fun b hb m => vcub b hb m.1) ktab hik words fresh1 + +end top + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtFullPlaceA.lean b/proofs/Fl4ProofsA64/P3/UtFullPlaceA.lean new file mode 100644 index 000000000..4e6e443ae --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtFullPlaceA.lean @@ -0,0 +1,183 @@ +/- +P3 (aarch64, A64-P3 Untyped, full-record post, part B): **the record's IPC placement and coverage fields when new +objects join the sets**: `iplaced`, `ipcPlace`, `ipcTh`, `ipcHeapCov`, `scCov`. + +The new objects are free in the old typed heap and have their kind in the new one (`PlaceA`); the tracked buffers are +words of old objects. + +No named premise. New theorems only. +-/ +import Fl4ProofsA64.P3.UtFullIpcA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-- **The IPC sets of the new typed heap**: `Q` holds the addresses new in `th'`. -/ +structure UtIpcGrowA (th th' : TypedHeapA) (N E Rp T Nn En Rn Tn : Set U64) (Q : U64 → Prop) : Prop where + old : ∀ p k, th p = some k → th' p = some k + fresh : ∀ p, Q p → th p = none + newN : ∀ p ∈ Nn, Q p ∧ th' p = some (.other 64) + newE : ∀ p ∈ En, Q p ∧ th' p = some (.other 16) + newR : ∀ p ∈ Rn, Q p ∧ th' p = some (.other 32) + newT : ∀ p ∈ Tn, Q p ∧ th' p = some .tcb + oldT : ∀ t ∈ T, th t = some .tcb + +section +variable {th th' : TypedHeapA} {N E Rp T Nn En Rn Tn : Set U64} {Q : U64 → Prop} {Bf : Set (U64 × Word)} + (G : UtIpcGrowA th th' N E Rp T Nn En Rn Tn Q) (I : IpcSchedPlacementA th N E Rp Bf) +include G I + +theorem utRt_thN : ∀ n ∈ N ∪ Nn, th' n = some (.other 64) := by + rintro n (h | h) + · exact G.old _ _ (I.notification n h) + · exact (G.newN n h).2 + +theorem utRt_thE : ∀ e ∈ E ∪ En, th' e = some (.other 16) := by + rintro n (h | h) + · exact G.old _ _ (I.endpoint n h) + · exact (G.newE n h).2 + +theorem utRt_thR : ∀ r ∈ Rp ∪ Rn, th' r = some (.other 32) := by + rintro n (h | h) + · exact G.old _ _ (I.reply n h) + · exact (G.newR n h).2 + +omit I in +theorem utRt_thTc : ∀ t ∈ T ∪ Tn, th' t = some .tcb := by + rintro n (h | h) + · exact G.old _ _ (G.oldT n h) + · exact (G.newT n h).2 + +theorem utRt_ipcPlaceA : IpcSchedPlacementA th' (N ∪ Nn) (E ∪ En) (Rp ∪ Rn) Bf := + ⟨utRt_thN G I, utRt_thE G I, utRt_thR G I, fun b hb => by + obtain ⟨p, len, hp, h⟩ := I.buffer b hb + exact ⟨p, len, G.old _ _ hp, h⟩⟩ + +theorem utRt_ipcThA : IpcThA th' (N ∪ Nn) (E ∪ En) (Rp ∪ Rn) := + ⟨utRt_thN G I, utRt_thE G I, utRt_thR G I⟩ + +set_option maxHeartbeats 2000000 in +/-- **`IPlacedA` with new IPC objects and TCBs** (module docstring). -/ +theorem utRt_iplacedA {ksb : U64} (P : PlaceA th' ksb) (J : IPlacedA N T E Rp Bf) : + IPlacedA (N ∪ Nn) (T ∪ Tn) (E ∪ En) (Rp ∪ Rn) Bf := by + have hN := utRt_thN G I + have hE := utRt_thE G I + have hR := utRt_thR G I + have hT := utRt_thTc G + -- two typed objects of `th'` are apart + have ap : ∀ (p q : U64) (kp kq : ObjKindA), th' p = some kp → th' q = some kq → p ≠ q → + p.val + kp.size ≤ q.val ∨ q.val + kq.size ≤ p.val := fun p q kp kq h1 h2 ne => P.apart p q kp kq h1 h2 ne + have kne : ∀ (p q : U64) (kp kq : ObjKindA), th' p = some kp → th' q = some kq → kp ≠ kq → p ≠ q := by + intro p q kp kq h1 h2 ne e + rw [e, h2] at h1 + exact ne (Option.some.inj h1).symm + have tcbP : TcbPlaced (T ∪ Tn) := fun t ht => + ⟨fun h => P.nz t _ (hT t ht) (UScalar.eq_of_val_eq (by rw [h]; rfl)), P.align t _ (hT t ht), + P.fit t _ (hT t ht)⟩ + have tn : ∀ n ∈ N ∪ Nn, ∀ t ∈ T ∪ Tn, t.val + 4096 ≤ n.val ∨ n.val + 64 ≤ t.val := fun n hn t ht => + ap t n _ _ (hT t ht) (hN n hn) (kne t n _ _ (hT t ht) (hN n hn) (fun h => by cases h)) + have nz : ∀ p k, th' p = some k → p.val ≠ 0 := fun p k h g => + P.nz p k h (UScalar.eq_of_val_eq (by rw [g]; rfl)) + have et : ∀ e ∈ E ∪ En, ∀ t ∈ T ∪ Tn, t.val + 4096 ≤ e.val ∨ e.val + 16 ≤ t.val := fun e he t ht => + ap t e _ _ (hT t ht) (hE e he) (kne t e _ _ (hT t ht) (hE e he) (fun h => by cases h)) + have rt : ∀ r ∈ Rp ∪ Rn, ∀ t ∈ T ∪ Tn, t.val + 4096 ≤ r.val ∨ r.val + 32 ≤ t.val := fun r hr t ht => + ap t r _ _ (hT t ht) (hR r hr) (kne t r _ _ (hT t ht) (hR r hr) (fun h => by cases h)) + have er : ∀ e ∈ E ∪ En, ∀ r ∈ Rp ∪ Rn, r.val + 32 ≤ e.val ∨ e.val + 16 ≤ r.val := fun e he r hr => + ap r e _ _ (hR r hr) (hE e he) (kne r e _ _ (hR r hr) (hE e he) (fun h => by cases h)) + -- a tracked buffer word is off every new object + have bnew : ∀ a, BufW Bf a → ∀ (q : U64) (k : ObjKindA), Q q → th' q = some k → + a.val < q.val ∨ q.val + k.size ≤ a.val := by + intro a ha q k hq hk + obtain ⟨p, len, hp, g1, g2⟩ := I.buffer a ha + have ne : p ≠ q := fun e => by + rw [e, G.fresh q hq] at hp; cases hp + rcases ap p q _ _ (G.old _ _ hp) hk ne with g | g <;> simp only [ObjKindA.size] at g ⊢ <;> omega + have bt : ∀ a, BufW Bf a → ∀ t ∈ T ∪ Tn, a.val < t.val ∨ t.val + 4096 ≤ a.val := by + rintro a ha t (h | h) + · exact J.e.bt a ha t h + · exact bnew a ha t _ (G.newT t h).1 (G.newT t h).2 + have be : ∀ a, BufW Bf a → ∀ e ∈ E ∪ En, a.val < e.val ∨ e.val + 16 ≤ a.val := by + rintro a ha e (h | h) + · exact J.e.be a ha e h + · exact bnew a ha e _ (G.newE e h).1 (G.newE e h).2 + have br : ∀ a, BufW Bf a → ∀ r ∈ Rp ∪ Rn, a.val < r.val ∨ r.val + 32 ≤ a.val := by + rintro a ha r (h | h) + · exact J.e.br a ha r h + · exact bnew a ha r _ (G.newR r h).1 (G.newR r h).2 + refine ⟨⟨tcbP, fun n hn => ⟨P.align n _ (hN n hn), P.fit n _ (hN n hn)⟩, tn⟩, + ⟨tcbP, fun e he => ⟨P.align e _ (hE e he), nz e _ (hE e he), P.fit e _ (hE e he)⟩, + fun r hr => ⟨P.align r _ (hR r hr), nz r _ (hR r hr), P.fit r _ (hR r hr)⟩, et, rt, er, J.e.balign, bt, be, + br, J.e.binj⟩, fun n hn e he => ?_, fun n hn r hr => ?_, ?_⟩ + · exact ap n e _ _ (hN n hn) (hE e he) (kne n e _ _ (hN n hn) (hE e he) (fun h => by cases h)) + · exact ap n r _ _ (hN n hn) (hR r hr) (kne n r _ _ (hN n hn) (hR r hr) (fun h => by cases h)) + · rintro n (h | h) a ha + · exact J.nb n h a ha + · exact bnew a ha n _ (G.newN n h).1 (G.newN n h).2 + +end + +/-- **`IpcHeapCovA` over the grown heap.** -/ +theorem utRt_ipcHeapCovA {H H' : Heap} {N E Rp Nn En Rn : Set U64} (h : IpcHeapCovA H N E Rp) + (hN : ∀ q, H' q = some .ntfn → H q = some .ntfn ∨ q ∈ Nn) + (hE : ∀ q, H' q = some .ep → H q = some .ep ∨ q ∈ En) + (hR : ∀ q, H' q = some .reply → H q = some .reply ∨ q ∈ Rn) : + IpcHeapCovA H' (N ∪ Nn) (E ∪ En) (Rp ∪ Rn) := + ⟨fun p g => (hN p g).imp (h.ntfn p) id, fun p g => (hE p g).imp (h.ep p) id, + fun p g => (hR p g).imp (h.reply p) id⟩ + +/-- Only type 3 makes notifications, type 2 endpoints, type 6 replies. -/ +theorem utNewKind_ipcA {n us : Nat} {k : HKind} (h : newKind n us = some k) : + (k = .ntfn → n = 3) ∧ (k = .ep → n = 2) ∧ (k = .reply → n = 6) := by + have key : ∀ k' : HKind, some k' = some k → (k' = .ntfn → n = 3) → (k' = .ep → n = 2) → + (k' = .reply → n = 6) → (k = .ntfn → n = 3) ∧ (k = .ep → n = 2) ∧ (k = .reply → n = 6) := by + intro k' e a b c + cases e + exact ⟨a, b, c⟩ + unfold newKind at h + by_cases h1 : n = 1 + · rw [ite_eq_left h1] at h + exact key _ h (fun e => by cases e) (fun e => by cases e) (fun e => by cases e) + rw [ite_eq_right h1] at h + by_cases h2 : n = 2 + · rw [ite_eq_left h2] at h + exact key _ h (fun e => by cases e) (fun _ => h2) (fun e => by cases e) + rw [ite_eq_right h2] at h + by_cases h3 : n = 3 + · rw [ite_eq_left h3] at h + exact key _ h (fun _ => h3) (fun e => by cases e) (fun e => by cases e) + rw [ite_eq_right h3] at h + by_cases h4 : n = 4 + · rw [ite_eq_left h4] at h + exact key _ h (fun e => by cases e) (fun e => by cases e) (fun e => by cases e) + rw [ite_eq_right h4] at h + by_cases h5 : n = 5 + · rw [ite_eq_left h5] at h + exact key _ h (fun e => by cases e) (fun e => by cases e) (fun e => by cases e) + rw [ite_eq_right h5] at h + by_cases h6 : n = 6 + · rw [ite_eq_left h6] at h + exact key _ h (fun e => by cases e) (fun e => by cases e) (fun _ => h6) + rw [ite_eq_right h6] at h + split_ifs at h + all_goals exact key _ h (fun e => by cases e) (fun e => by cases e) (fun e => by cases e) + +/-- **`ScCovA` with new SCs.** -/ +theorem utRt_scCovA {C Cn : Set U64} {m m' : McState} {σ σ' : State Unit} {P : ObjRef → Prop} {d : KernelObject} + (h : ScCovA C m σ) (O : UtNewObjA σ σ' P d) (hname : ∀ c, scNameA m' c = scNameA m c) + (hn : ∀ p, P p → ∀ x n, d = .schedContext x n → ∃ c ∈ Cn, p = scNameA m c) : ScCovA (C ∪ Cn) m' σ' := by + intro p x n hk + rcases O.back p _ hk (fun _ _ e => by cases e) with g | ⟨hp, e⟩ + · obtain ⟨c, hc, e⟩ := h p x n g + exact ⟨c, Or.inl hc, by rw [hname]; exact e⟩ + · obtain ⟨c, hc, e'⟩ := hn p hp x n e.symm + exact ⟨c, Or.inr hc, by rw [hname]; exact e'⟩ + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtFullSchedA.lean b/proofs/Fl4ProofsA64/P3/UtFullSchedA.lean new file mode 100644 index 000000000..dac47957d --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtFullSchedA.lean @@ -0,0 +1,362 @@ +/- +P3 (aarch64, A64-P3 Untyped, full-record post, part B): **the scheduler relation `SchedSRA` after a creation** +(`JStage3A.sr`), with the new TCBs (inactive, unqueued, core 0) and SCs (zero, no refills, `CORE` word 0). + +`sch`, `rest`, `thT`, the placement and the current thread come from the `JAtZAX` post (hypotheses here); the old +queues, SC objects and scheduler words are kept (`UtRtRdA`), the old spec objects too (`UtNewObjA`). + +No named premise. New theorems only. +-/ +import Fl4ProofsA64.P3.UtFullPlaceA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +set_option maxRecDepth 100000 + +/-- **`ScInvA` after a creation.** -/ +theorem utRt_scInvA {m m' : McState} {σ σ' : State Unit} {P : ObjRef → Prop} {d : KernelObject} + (h : ScInvA m σ) (O : UtNewObjA σ σ' P d) (hidt : m'.idleThreadOf = m.idleThreadOf) + (hids : m'.idleScOf = m.idleScOf) + (hdt : ∀ x, d = .tcb x → x.tcbSchedContext = none) + (hds : ∀ y k, d = .schedContext y k → scActive y = false) + (hdn : ∀ x, d = .notification x → x.ntfnSc = none) : ScInvA m' σ' := by + have kt : ∀ p x, σ.kheap p = some (.tcb x) → σ'.kheap p = some (.tcb x) := fun p x g => + O.keep p _ g (fun _ _ e => by cases e) + have ks : ∀ p y k, σ.kheap p = some (.schedContext y k) → σ'.kheap p = some (.schedContext y k) := fun p y k g => + O.keep p _ g (fun _ _ e => by cases e) + have hR := h.relKeep (σ' := σ') O.glob.rls (fun u _ x hx => ⟨x, kt _ _ hx, rfl⟩) (fun u _ x p hx hp => by + obtain ⟨y, n, hy, -⟩ := h.back u x p hx hp + rw [hy]; exact ks _ _ _ hy) + have nis : ∀ p, NotIdleScA m p → NotIdleScA m' p := fun p g => + ⟨g.1, fun c hc => by rw [hids]; exact g.2 c hc⟩ + have nis' : ∀ p, NotIdleScA m' p → NotIdleScA m p := fun p g => + ⟨g.1, fun c hc => by rw [← hids]; exact g.2 c hc⟩ + have bsc : ∀ p y n, σ'.kheap p = some (.schedContext y n) → scActive y = true → + σ.kheap p = some (.schedContext y n) := fun p y n g ha => by + rcases O.back p _ g (fun _ _ e => by cases e) with g' | ⟨-, e⟩ + · exact g' + · rw [hds y n e.symm] at ha; cases ha + have btcb : ∀ u x p, σ'.kheap u = some (.tcb x) → x.tcbSchedContext = some p → σ.kheap u = some (.tcb x) := + fun u x p g hp => by + rcases O.back u _ g (fun _ _ e => by cases e) with g' | ⟨-, e⟩ + · exact g' + · rw [hdt x e.symm] at hp; cases hp + have bn : ∀ q x, σ'.kheap q = some (.notification x) → x.ntfnSc ≠ none → σ.kheap q = some (.notification x) := + fun q x g ne => by + rcases O.back q _ g (fun _ _ e => by cases e) with g' | ⟨-, e⟩ + · exact g' + · exact absurd (hdn x e.symm) ne + refine ⟨fun p y n g ha hp => h.refills p y n (bsc p y n g ha) ha hp, fun p y n g ha hp => ?_, + fun q x g e => h.ntfnIdle q x (bn q x g (by rw [e]; exact fun c => by cases c)) e, + fun q x g c hc e => h.ntfnIdleOf q x (bn q x g (by rw [e]; exact fun c => by cases c)) c hc + (by rw [← hids]; exact e), hR.1, hR.2, fun u x p g hp => ?_, fun u x p g hp hni => ?_⟩ + · obtain ⟨a, b⟩ := h.rr p y n (bsc p y n g ha) ha hp + exact ⟨a, fun c => b (nis' p c)⟩ + · obtain ⟨y, n, hy, e⟩ := h.back u x p (btcb u x p g hp) hp + exact ⟨y, n, ks _ _ _ hy, e⟩ + · exact nis p (h.tcbScNotIdle u x p (btcb u x p g hp) hp (fun c hc => by rw [← hidt]; exact hni c hc)) + +/-- **`RdA` after a creation.** -/ +theorem utRt_rdA {σ σ' : State Unit} {P : ObjRef → Prop} {d : KernelObject} (h : RdA σ) (O : UtNewObjA σ σ' P d) + (hback : ∀ u x p, σ.kheap u = some (.tcb x) → x.tcbSchedContext = some p → + ∃ y n, σ.kheap p = some (.schedContext y n)) + (hdt : ∀ x, d = .tcb x → ¬ inClassRSc x.tcbState x.tcbFault) : RdA σ' := by + intro u x hx hc x' scp y n h1 h2 h3 h4 h5 + have ex : x' = x := by + rw [hx] at h1 + cases h1 + rfl + subst ex + rcases O.back u _ hx (fun _ _ e => by cases e) with g | ⟨-, e⟩ + · obtain ⟨y0, n0, hy⟩ := hback u _ scp g h2 + have := (O.keep scp _ hy (fun _ _ e => by cases e)).symm.trans h3 + cases this + rw [O.glob.rls] at h5 + have := h u _ g hc _ scp y n g h2 hy h4 h5 + unfold releasedSc at this ⊢ + rw [O.glob.time]; exact this + · exact absurd hc (hdt _ e.symm) + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **`SchedCoreA` with new TCBs**: unqueued, of core 0 and priority 0. -/ +theorem utRt_schedCoreA {T Tn Cs : Set U64} {s : S} {Q : U64 → U64 → List U64} {R : U64 → List U64} + (h : SchedCoreA st T Cs s Q R) (h0 : (0#u64 : U64) ∈ Cs) (hd : ∀ t ∈ Tn, t ∉ T) + (ha : ∀ t ∈ Tn, TcbAddr t) + (hn : ∀ c ∈ Cs, ∀ t ∈ Tn, t.val + 2 ^ 12 ≤ L.nodeBase s c ∨ L.nodeBase s c + 4288 ≤ t.val) + (hw : ∀ t ∈ Tn, L.mem s (affA t) = 0#u64 ∧ L.mem s (prioA t) = 0#u64 ∧ L.mem s (inqA t) = 0#u64) : + SchedCoreA st (T ∪ Tn) Cs s Q R := by + have qm : ∀ c ∈ Cs, ∀ p : U64, p.val < 256 → ∀ t ∈ Q c p, t ∈ T := fun c hc p hp t ht => + ((h.ready c hc).1.rel p hp).mem t ht + have rm : ∀ c ∈ Cs, ∀ t ∈ R c, t ∈ T := fun c hc t ht => (h.release c hc).mem t ht + have z256 : (0#u64 : U64).val < 256 := by simp + have notQ : ∀ t ∈ Tn, t ∉ Q (L.mem s (affA t)) (L.mem s (prioA t)) := fun t ht g => by + rw [(hw t ht).1, (hw t ht).2.1] at g + exact hd t ht (qm _ h0 _ z256 t g) + have notR : ∀ t ∈ Tn, t ∉ R (L.mem s (affA t)) := fun t ht g => by + rw [(hw t ht).1] at g + exact hd t ht (rm _ h0 t g) + refine ⟨fun t ht => ht.elim (h.tcbs t) (ha t), fun c hc => ?_, fun t ht => ?_, fun t ht => ?_, fun c hc => ?_, + fun c hc => utQueue_monoA (h.release c hc) Set.subset_union_left, h.inQ, h.inR, fun t ht => ?_, + fun t ht => ?_, fun t ht => ?_⟩ + · have n := h.nodes c hc + exact ⟨n.align, n.fits, fun t ht => ht.elim (n.disj t) (hn c hc t)⟩ + · rcases ht with g | g + · exact h.aff t g + · rw [(hw t g).1]; exact h0 + · rcases ht with g | g + · exact h.prio t g + · rw [(hw t g).2.1]; simp + · obtain ⟨⟨r, dj⟩, bm⟩ := h.ready c hc + exact ⟨⟨fun p hp => utQueue_monoA (r p hp) Set.subset_union_left, dj⟩, bm⟩ + · rcases ht with g | g + · exact h.queued t g + · rw [(hw t g).2.2] + exact ⟨fun c => absurd (by simp) c, fun c => absurd c (notQ t g)⟩ + · rcases ht with g | g + · exact h.released t g + · rw [(hw t g).2.2] + exact ⟨fun c => absurd (by simp) c, fun c => absurd c (notR t g)⟩ + · rcases ht with g | g + · exact h.excl t g + · exact fun c => absurd c (notQ t g) + +/-- **The new SCs of a creation**: their placement and zeroed words. -/ +structure UtNewScA (st : machine.Store S) [L : LawfulStore st] (th' : TypedHeapA) (C Cn : Set U64) (m : McState) + (s s' : S) (σ σ' : State Unit) : Prop where + fresh : ∀ c ∈ Cn, c ∉ C + nameEq : ∀ c ∈ Cn, scNameA m c = specPtr c + notIdle : ∀ c ∈ Cn, ¬ IdleScA m c + notIdlePtr : ∀ c ∈ Cn, specPtr c ≠ idleScPtr + obj : ∀ c ∈ Cn, ∃ k, σ'.kheap (specPtr c) = some (.schedContext defaultSchedContext k) ∧ + ScRelWA st (IdleScA m c) s' c defaultSchedContext k + core : ∀ c ∈ Cn, L.mem s' (scCoreA c) = 0#u64 + apart : ∀ c ∈ Cn, ∀ c' ∈ Cn, c ≠ c' → ∀ k k', σ'.kheap (specPtr c) = some (.schedContext defaultSchedContext k) → + σ'.kheap (specPtr c') = some (.schedContext defaultSchedContext k') → ∀ a, ScObjA c k a → ¬ ScObjA c' k' a + off : ∀ c ∈ Cn, ∀ k, σ'.kheap (specPtr c) = some (.schedContext defaultSchedContext k) → ∀ a, ScObjA c k a → + (∀ p kd, th' p = some kd → a.val < p.val ∨ p.val + kd.size ≤ a.val) ∧ + (a.val < (L.ksb s).val ∨ (L.ksb s).val + ksSizeA ≤ a.val) ∧ + (∀ c' ∈ C, ∀ x n, σ.kheap (scNameA m c') = some (.schedContext x n) → ¬ ScObjA c' n a) + /-- The old SC objects are off the new typed heap. -/ + oldOff : ∀ c ∈ C, ∀ x n, σ.kheap (scNameA m c) = some (.schedContext x n) → ∀ a, ScObjA c n a → + ∀ p kd, th' p = some kd → a.val < p.val ∨ p.val + kd.size ≤ a.val + +/-- **`SchedSRA` after a creation** (module docstring). -/ +theorem utRt_fullSchedSRA {Z Z' : XSetsA} {Tn Cn : Set U64} {m m' : McState} {κ : U64 → U64} {s s' : S} + {σ σ' : State Unit} {P : ObjRef → Prop} {d : KernelObject} + (H : SchedSRA st Z m κ s σ) (O : UtNewObjA σ σ' P d) + (eT : Z'.T = Z.T ∪ Tn) (eC : Z'.C = Z.C ∪ Cn) (eCs : Z'.Cs = Z.Cs) (eQ : Z'.Q = Z.Q) (eR : Z'.R = Z.R) + (hks : L.ksb s' = L.ksb s) (hcpu : L.cpu s' = L.cpu s) + (hids : m'.idleScOf = m.idleScOf) (hidt : m'.idleThreadOf = m.idleThreadOf) (hcc : m'.curCore = m.curCore) + (sch : SchedRelA (L := L) Z'.T Z'.C Z'.Cs Z'.curOf Z'.curScOf s' σ' m') + (rest : SchedRestA (L := L) Z' s' σ' m') + (thT : ∀ t, Z'.th t = some .tcb ↔ t ∈ Z'.T) (place : PlaceA Z'.th (L.ksb s')) + -- the kept words + (hm : ∀ a, Aligned a → SReadA st Z m s σ a → a ≠ nwA st s → L.mem s' a = L.mem s a) + (hnode : ∀ c ∈ Z.Cs, ∀ o : U64, o.val < 4288 → o.val % 8 = 0 → o.val ≠ 88 → + L.mem s' (L.nodeAt s c o) = L.mem s (L.nodeAt s c o)) + -- the new TCBs and SCs + (hTd : ∀ t ∈ Tn, t ∉ Z.T) (hTa : ∀ t ∈ Tn, TcbAddr t) + (hTnode : ∀ c ∈ Z.Cs, ∀ t ∈ Tn, t.val + 2 ^ 12 ≤ L.nodeBase s' c ∨ L.nodeBase s' c + 4288 ≤ t.val) + (hTw : ∀ t ∈ Tn, L.mem s' (affA t) = 0#u64 ∧ L.mem s' (prioA t) = 0#u64 ∧ L.mem s' (inqA t) = 0#u64) + (K : UtNewScA st Z'.th Z.C Cn m s s' σ σ') + (hdt : ∀ x, d = .tcb x → x.tcbSchedContext = none ∧ x.tcbState = .inactive ∧ x.tcbFault = none) + (hds : ∀ y k, d = .schedContext y k → scActive y = false) + (hdn : ∀ x, d = .notification x → x.ntfnSc = none) : + SchedSRA st Z' m' (fun c => if c ∈ Cn then 0#u64 else κ c) s' σ' := by + have hname : ∀ c, scNameA m' c = scNameA m c := fun c => by unfold scNameA; rw [hids, hcc] + have hidle : ∀ c, IdleScA m' c ↔ IdleScA m c := fun c => by unfold IdleScA; rw [hids] + have hT := H.core.tcbs + have kt : ∀ p x, σ.kheap p = some (.tcb x) → σ'.kheap p = some (.tcb x) := fun p x g => + O.keep p _ g (fun _ _ e => by cases e) + have ks : ∀ p y k, σ.kheap p = some (.schedContext y k) → σ'.kheap p = some (.schedContext y k) := fun p y k g => + O.keep p _ g (fun _ _ e => by cases e) + have tK : ∀ t ∈ Z.T, ∀ k : U64, (k = 3520#u64 ∨ k = 3448#u64 ∨ k = 3432#u64 ∨ k = 3464#u64 ∨ k = 3592#u64 ∨ + k = 3480#u64 ∨ k = 3488#u64) → L.mem s' (wa t k) = L.mem s (wa t k) := by + intro t ht k hk + have al : Aligned (wa t k) := by + rcases hk with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> exact field_aligned (hT t ht) (by simp) (by simp) + have hr : SReadA st Z m s σ (wa t k) := Or.inr (Or.inr (Or.inr (Or.inl ⟨t, ht, k, hk, rfl⟩))) + refine hm _ al hr (fun e => ?_) + have n := H.core.nodes 0#u64 (by + obtain ⟨k0, h0, -, h⟩ := H.csN + exact (h 0#u64).2 (by simpa using h0)) + have f := (hT t ht).fits + have kv : k.val < 4096 := by rcases hk with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> simp + have v : (wa t k).val = t.val + k.val := by + rw [wa_valP]; exact Nat.mod_eq_of_lt (by omega) + have w := congrArg UScalar.val e + rw [v, nodeAt_val n (by rw [debug_val]; omega), debug_val] at w + rcases n.disj t ht with g | g <;> omega + have nK : ∀ c ∈ Z.Cs, ∀ o : U64, o.val < 4288 → o.val % 8 = 0 → o.val ≠ 88 → + L.mem s' (L.nodeAt s' c o) = L.mem s (L.nodeAt s c o) := fun c hc o h1 h2 h3 => by + rw [nodeAt_of_ksb hks]; exact hnode c hc o h1 h2 h3 + have h0 : (0#u64 : U64) ∈ Z.Cs := by + obtain ⟨k0, hk, -, h⟩ := H.csN + exact (h 0#u64).2 (by simpa using hk) + have core0 : SchedCoreA st Z.T Z.Cs s' Z.Q Z.R := + H.core.agree hks (fun y hy => ⟨tK y hy 3480#u64 (by simp), tK y hy 3488#u64 (by simp)⟩) + (fun c hc o h1 h2 h3 => hnode c hc o h1 h2 (by omega)) (fun y hy => tK y hy 3592#u64 (by simp)) + (fun y hy => tK y hy 3520#u64 (by simp)) (fun y hy => tK y hy 3448#u64 (by simp)) + have core : SchedCoreA st Z'.T Z'.Cs s' Z'.Q Z'.R := by + rw [eT, eCs, eQ, eR] + exact utRt_schedCoreA core0 h0 hTd hTa hTnode hTw + have curT : L.curW s' = L.curW s := by + have := nK _ H.sch.cpuCs layout.node.CUR_THREAD (by simp [layout.node.CUR_THREAD]) + (by simp [layout.node.CUR_THREAD]) (by simp [layout.node.CUR_THREAD]) + unfold LawfulStore.curW + rw [hcpu]; exact this + -- the SC relation + have mfoot : ∀ a, Aligned a → ScFootA Z.C m σ a → L.mem s' a = L.mem s a := by + intro a al hf + refine hm a al (Or.inr (Or.inr (Or.inr (Or.inr hf)))) (fun e => ?_) + obtain ⟨c, hc, x, n, hk, ha⟩ := hf + have out := (H.scp.out c hc x n hk a ha).2 + have nd := H.core.nodes 0#u64 h0 + have w := congrArg UScalar.val e + rw [nodeAt_val nd (by rw [debug_val]; omega), debug_val] at w + unfold LawfulStore.nodeBase at w + unfold ksSizeA at out + simp at w + omega + have hkc : ∀ c ∈ Z.C, σ'.kheap (scNameA m c) = σ.kheap (scNameA m c) := fun c hc => by + obtain ⟨x, n, hk, -⟩ := H.scr.sc c hc + rw [hk]; exact ks _ _ _ hk + have scr0 : ScStateRelA st Z.C m s' σ' := H.scr.frame mfoot hkc + have scr : ScStateRelA st Z'.C m' s' σ' := by + refine ⟨fun c hc => ?_⟩ + rw [eC] at hc + rcases hc with hc | hc + · obtain ⟨x, n, hk, r⟩ := scr0.sc c hc + refine ⟨x, n, by rw [hname]; exact hk, ?_⟩ + rw [show IdleScA m' c = IdleScA m c from propext (hidle c)] + exact r + · obtain ⟨k, hk, r⟩ := K.obj c hc + refine ⟨_, k, by rw [hname, K.nameEq c hc]; exact hk, ?_⟩ + rw [show IdleScA m' c = IdleScA m c from propext (hidle c)] + exact r + -- the SC placement + have oldK : ∀ c ∈ Z.C, ∀ x n, σ'.kheap (scNameA m c) = some (.schedContext x n) → + σ.kheap (scNameA m c) = some (.schedContext x n) := fun c hc x n g => by rwa [hkc c hc] at g + have newK : ∀ c ∈ Cn, ∀ x n, σ'.kheap (scNameA m c) = some (.schedContext x n) → + σ'.kheap (specPtr c) = some (.schedContext defaultSchedContext n) := fun c hc x n g => by + rw [K.nameEq c hc] at g + obtain ⟨k, hk, -⟩ := K.obj c hc + have := g.symm.trans hk + cases this + exact hk + have scp : ScPlacedA Z'.th (L.ksb s') Z'.C m' σ' := by + refine ⟨fun c hc c' hc' e => ?_, fun c hc c' hc' ne x n x' n' g g' a ha ha' => ?_, fun c hc x n g a ha => ?_⟩ + · rw [hname, hname] at e + rw [eC] at hc hc' + rcases hc with hc | hc <;> rcases hc' with hc' | hc' + · exact H.scp.inj c hc c' hc' e + · rw [K.nameEq c' hc'] at e + unfold scNameA at e + split_ifs at e with g + · exact absurd e.symm (K.notIdlePtr c' hc') + · exact absurd (specPtr_inj e ▸ hc) (K.fresh c' hc') + · rw [K.nameEq c hc] at e + unfold scNameA at e + split_ifs at e with g + · exact absurd e (K.notIdlePtr c hc) + · exact absurd (specPtr_inj e.symm ▸ hc') (K.fresh c hc) + · rw [K.nameEq c hc, K.nameEq c' hc'] at e + exact specPtr_inj e + · rw [hname] at g g' + rw [eC] at hc hc' + rcases hc with hc | hc <;> rcases hc' with hc' | hc' + · exact H.scp.disj c hc c' hc' ne x n x' n' (oldK c hc x n g) (oldK c' hc' x' n' g') a ha ha' + · exact (K.off c' hc' n' (newK c' hc' x' n' g') a ha').2.2 c hc x n (oldK c hc x n g) ha + · exact (K.off c hc n (newK c hc x n g) a ha).2.2 c' hc' x' n' (oldK c' hc' x' n' g') ha' + · exact K.apart c hc c' hc' ne n n' (newK c hc x n g) (newK c' hc' x' n' g') a ha ha' + · rw [hname] at g + rw [eC] at hc + rw [hks] + rcases hc with hc | hc + · have g0 := oldK c hc x n g + exact ⟨K.oldOff c hc x n g0 a ha, (H.scp.out c hc x n g0 a ha).2⟩ + · obtain ⟨a1, a2, -⟩ := K.off c hc n (newK c hc x n g) a ha + exact ⟨a1, a2⟩ + -- the node words naming SCs + have csc : ∀ c ∈ Z.Cs, L.mem s' (L.nodeAt s' c layout.node.CUR_SC) = L.mem s (L.nodeAt s c layout.node.CUR_SC) := + fun c hc => nK c hc _ (by simp [layout.node.CUR_SC]) (by simp [layout.node.CUR_SC]) (by simp [layout.node.CUR_SC]) + have coreW : ∀ c ∈ Z.C, L.mem s' (scCoreA c) = L.mem s (scCoreA c) := by + intro c hc + obtain ⟨x, n, hk, r⟩ := H.scr.sc c hc + have pl := r.pl + have h7 : 2 ^ 7 ≤ 2 ^ (n + minSchedContextBits) := + Nat.pow_le_pow_right (by norm_num) (by unfold minSchedContextBits; omega) + have f := pl.fits + have e : (scCoreA c).val = c.val + 16 := by + unfold scCoreA + rw [wa_valP, show layout.sc.CORE.val = 16 by simp [layout.sc.CORE]] + exact Nat.mod_eq_of_lt (by omega) + have al : Aligned (scCoreA c) := by + show _ % 8 = 0 + have := pl.align + have d : 8 ∣ 2 ^ (n + minSchedContextBits) := Nat.dvd_trans (by norm_num) (Nat.pow_dvd_pow 2 + (show 3 ≤ n + minSchedContextBits by unfold minSchedContextBits; omega)) + have : 8 ∣ c.val := Nat.dvd_trans d (Nat.dvd_of_mod_eq_zero this) + omega + exact mfoot _ al ⟨c, hc, x, n, hk, by omega, by omega⟩ + refine + { sch := ?_, rest := rest, scr := scr, scp := scp, thT := thT, place := place, csN := by rw [eCs]; exact H.csN, + ksFit := by rw [hks]; exact H.ksFit, curT := by rw [curT, eT]; exact Or.inl H.curT, core := core, + curOn := ?_, run := ?_, actOn := ?_, cscC := ?_, + inv := utRt_scInvA H.inv O hidt hids (fun x e => (hdt x e).1) hds hdn, scCore := ?_, cscAll := ?_, + rd := utRt_rdA H.rd O (fun u x p hx hp => by + obtain ⟨y, n, hy, -⟩ := H.inv.back u x p hx hp + exact ⟨y, n, hy⟩) (fun x e => by + obtain ⟨-, e1, e2⟩ := hdt x e + rw [e1, e2] + rintro (g | ⟨_, g⟩ | ⟨_, _, g, -⟩ | ⟨_, _, _, g⟩) <;> simp [runnable] at g) } + · exact + { core := sch.core, cpuCs := sch.cpuCs, curOfW := sch.curOfW, curScOfW := sch.curScOfW, cur := sch.cur, + csc := sch.csc, idle := sch.idle, idleOf := sch.idleOf, idleScOf := sch.idleScOf, act := sch.act, + time := sch.time, aff := sch.aff, affT := sch.affT, + tcb := fun t ht => by + obtain ⟨x, hx, -, e⟩ := sch.tcb t ht + exact ⟨x, hx, e⟩, + sc := sch.sc, scOff := sch.scOff } + · rw [curT, hcpu] + exact (tK _ H.curT 3520#u64 (by simp)).trans H.curOn + · refine ⟨fun dd p t g => ?_, fun t g => ?_⟩ + · rw [O.glob.rq] at g + obtain ⟨x, hx, r⟩ := H.run.1 dd p t g + exact ⟨x, kt _ _ hx, r⟩ + · rw [O.glob.rls] at g + obtain ⟨x, hx, r⟩ := H.run.2 t g + exact ⟨x, kt _ _ hx, r⟩ + · intro u hu + rw [O.glob.act] at hu + obtain ⟨a, b⟩ := H.actOn u hu + exact ⟨by rw [eT]; exact Or.inl a, (tK u a 3520#u64 (by simp)).trans (b.trans hcpu.symm)⟩ + · rw [hcpu, csc _ H.sch.cpuCs, eC] + exact Or.inl H.cscC + · intro c hc + rw [eC] at hc + show L.mem s' (scCoreA c) = if c ∈ Cn then 0#u64 else κ c + rcases hc with hc | hc + · rw [ite_eq_right (fun g => K.fresh c g hc), coreW c hc] + exact H.scCore c hc + · rw [ite_eq_left hc] + exact K.core c hc + · intro c hc + rw [eCs] at hc + rw [csc c hc, eC] + exact Or.inl (H.cscAll c hc) + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtFullSlotA.lean b/proofs/Fl4ProofsA64/P3/UtFullSlotA.lean new file mode 100644 index 000000000..f0cbf0846 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtFullSlotA.lean @@ -0,0 +1,119 @@ +/- +P3 (aarch64, A64-P3 Untyped, full-record post, part C): **`devUtOff`, `idleAll`, `origLive`, `nullOrig` and `finCov` +after a RAM creation.** The splits of the post-state's objects, node words and spec slots into old and new are +hypotheses here, discharged at the assembly. + +No named premise. New theorems only. +-/ +import Fl4ProofsA64.P3.UtFullCapA +import Fl4ProofsA64.P3.UtDropDevA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4SpecA64 Classical + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {Z Z' : XSetsA} {H H' : Heap} {OS OS' : Set U64} {s s' : S} {x p sb nt us aligned : U64} {win ia ie : Nat} + +/-- New device untypeds do not exist (a RAM creation), and the new objects lie in the source's RAM region +(`hlo`, `hhi`, `hobj`). `hks`: the nodes do not move. -/ +theorem utRt_devUtOffA (h : DevUtOffA st Z H OS ia ie s) + (F : UtSlotsA st OS OS' s s' x p sb nt us aligned win) (W : UtWinZA nt aligned (zA nt.val us) win) + {fi : U64} (hx : x ∈ OS) (hc : capA st s x = .Untyped p sb fi false) (hlo : p.val ≤ aligned.val) + (hhi : aligned.val + win * 2 ^ (zA nt.val us).val ≤ p.val + 2 ^ sb.val) + (hobj : ∀ q bytes, JointObjectA Z' H' q bytes → JointObjectA Z H q bytes ∨ + (aligned.val ≤ q.val ∧ q.val + bytes ≤ aligned.val + win * 2 ^ (zA nt.val us).val)) + (hks : ∀ lo hi, RegionOffNodes st Z.Cs s lo hi → RegionOffNodes st Z'.Cs s' lo hi) : + DevUtOffA st Z' H' OS' ia ie s' := by + -- a device untyped of the new slots is an old one + have dv : ∀ u ∈ OS', ∀ q b f, capA st s' u = .Untyped q b f true → u ∈ OS ∧ capA st s u = .Untyped q b f true := by + intro u hu q b f e + rcases F u hu with ⟨uo, k⟩ | ⟨-, f', k⟩ | ⟨j, -, k⟩ | k + · exact ⟨uo, k.symm.trans e⟩ + · have g := k.symm.trans e + simp only [capability.Cap.Untyped.injEq] at g + exact absurd g.2.2.2 (by simp) + · obtain ⟨-, -, -, -, ed⟩ := utNewCap_untypedA (k.symm.trans e) + cases ed + · rw [k] at e; cases e + refine ⟨fun u hu q b f e o bytes ho => ?_, fun u hu q b f e => ?_, fun u hu q b f e => ?_, + fun u hu q b f e v hv q' b' f' e' => ?_⟩ + · obtain ⟨uo, k⟩ := dv u hu q b f e + rcases hobj o bytes ho with g | ⟨g1, g2⟩ + · exact h.obj u uo q b f k o bytes g + · rcases h.sep u uo q b f k x hx p sb fi hc with d | d <;> omega + · obtain ⟨uo, k⟩ := dv u hu q b f e + exact hks _ _ (h.ks u uo q b f k) + · obtain ⟨uo, k⟩ := dv u hu q b f e + exact h.idle u uo q b f k + · obtain ⟨uo, k⟩ := dv u hu q b f e + rcases F v hv with ⟨vo, kv⟩ | ⟨-, f2, kv⟩ | ⟨j, hj, kv⟩ | kv + · exact h.sep u uo q b f k v vo q' b' f' (kv.symm.trans e') + · have g := kv.symm.trans e' + simp only [capability.Cap.Untyped.injEq] at g + rw [← g.1, ← g.2.1] + exact h.sep u uo q b f k x hx p sb fi hc + · obtain ⟨h0, eq, eb, -, -⟩ := utNewCap_untypedA (kv.symm.trans e') + have inw := utBase_inA W (b := bjA aligned (zA nt.val us) j) ⟨j, hj, rfl⟩ + have ez : zA nt.val us = us := by rw [h0]; rfl + simp only [ez] at inw eq hhi + rw [eq, eb] + rcases h.sep u uo q b f k x hx p sb fi hc with d | d <;> omega + · rw [kv] at e'; cases e' + +theorem utRt_idleAllA {m m' : McState} (h : IdleAllA st ia ie Z.Cs m s) (hCs : Z'.Cs = Z.Cs) + (hidt : m'.idleThreadOf = m.idleThreadOf) + (hn : ∀ c ∈ Z.Cs, ∀ o : U64, (o = layout.node.IDLE_THREAD ∨ o = layout.node.IDLE_SC) → + L.mem s' (L.nodeAt s' c o) = L.mem s (L.nodeAt s c o)) : + IdleAllA st ia ie Z'.Cs m' s' := by + rw [hCs] + refine ⟨fun c hc => ?_, fun c hc => ?_, fun c hc => ?_⟩ + · rw [hn c hc _ (Or.inl rfl)]; exact h.tcb c hc + · rw [hn c hc _ (Or.inr rfl)]; exact h.sc c hc + · rw [hidt]; exact h.spec c hc + +theorem utRt_absSlot_growA {th th' : TypedHeapA} (g : ∀ q k, th q = some k → th' q = some k) {sl : CslotPtr} + (h : AbsSlotA th sl) : AbsSlotA th' sl := by + obtain ⟨q, e, k⟩ := h + exact ⟨q, e, k.imp (g _ _) fun ⟨t, rest⟩ => ⟨g _ _ t, rest⟩⟩ + +/-- `ho`: an original slot after was original before, or is a new slot of the grown heap. -/ +theorem utRt_origLiveA {th th' : TypedHeapA} {σ σ' : State Unit} + (h : ∀ sl, σ.isOriginalCap sl = true → AbsSlotA th sl) (g : ∀ q k, th q = some k → th' q = some k) + (ho : ∀ sl, σ'.isOriginalCap sl = true → σ.isOriginalCap sl = true ∨ AbsSlotA th' sl) : + ∀ sl, σ'.isOriginalCap sl = true → AbsSlotA th' sl := fun sl e => + (ho sl e).elim (fun k => utRt_absSlot_growA g (h sl k)) id + +/-- `ho`: an original slot after is an old one with its cap, or holds a non-null cap. -/ +theorem utRt_nullOrigA {σ σ' : State Unit} + (h : ∀ sl, specCapAtA σ sl = some .nullCap → σ.isOriginalCap sl = false) + (ho : ∀ sl, σ'.isOriginalCap sl = true → + (σ.isOriginalCap sl = true ∧ specCapAtA σ' sl = specCapAtA σ sl) ∨ specCapAtA σ' sl ≠ some .nullCap) : + ∀ sl, specCapAtA σ' sl = some .nullCap → σ'.isOriginalCap sl = false := by + intro sl e + by_contra c + rcases ho sl (by simpa using c) with ⟨o, k⟩ | k + · rw [h sl (k.symm.trans e)] at o + cases o + · exact k e + +/-- `hc` / `ht`: a spec CNode or TCB after was one before, or is in the grown typed heap. -/ +theorem utRt_finCovA {th th' : TypedHeapA} {σ σ' : State Unit} (h : FinCovA th σ) + (g : ∀ q k, th q = some k → th' q = some k) + (hc : ∀ q r cs, σ'.kheap q = some (.cnode r cs) → wellFormedCnodeN r cs → + (∃ cs0, σ.kheap q = some (.cnode r cs0) ∧ wellFormedCnodeN r cs0) ∨ th' (fl4Ptr q) = some (.cnode r)) + (ht : ∀ q y, σ'.kheap q = some (.tcb y) → (∃ y0, σ.kheap q = some (.tcb y0)) ∨ th' (fl4Ptr q) = some .tcb) : + FinCovA th' σ' := by + refine ⟨fun q r cs e w => ?_, fun q y e => ?_⟩ + · rcases hc q r cs e w with ⟨cs0, e0, w0⟩ | k + · exact g _ _ (h.cnode q r cs0 e0 w0) + · exact k + · rcases ht q y e with ⟨y0, e0⟩ | k + · exact g _ _ (h.tcb q y0 e0) + · exact k + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtFullSqA.lean b/proofs/Fl4ProofsA64/P3/UtFullSqA.lean new file mode 100644 index 000000000..5601175ec --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtFullSqA.lean @@ -0,0 +1,46 @@ +/- +P3 (aarch64, A64-P3 Untyped, full-record post, part A): **`sq` after a creation.** `JEntA.sq` is P1's +`SchedQueuesInv` at the relation's queue lists. The creation's P1 post gives it at some lists `Q0` / `R0` (inside +`BlkOK`), and P3's `SchedCoreA` holds at the record's `Q` / `R` (`utRt_fullSchedSRA`). Membership of both is fixed by +the TCBs' queued bits, so the other fields carry over to `Q` / `R`. + +No named premise. New theorems only. +-/ +import Fl4ProofsA64.P3.ScCoreA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Sched Classical + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +theorem utRt_sqA {T Cs Ω : Set U64} {H : Heap} {s : S} {Q Q0 : U64 → U64 → List U64} {R R0 : U64 → List U64} + (h : SchedCoreA st T Cs s Q R) (h0 : SchedQueuesInv st T Cs Ω H s Q0 R0) : + SchedQueuesInv st T Cs Ω H s Q R := by + -- a member of a ready list of `Q` is a member of the same list of `Q0` + have mq : ∀ c ∈ Cs, ∀ p : U64, p.val < 256 → ∀ t ∈ Q c p, t ∈ Q0 c p := fun c hc p hp t ht => by + have tT : t ∈ T := ((h.ready c hc).1.rel p hp).mem t ht + obtain ⟨ea, ep⟩ := h.inQ c hc p hp t ht + have bit := (h.queued t tT).2 (by rw [ea, ep]; exact ht) + have := (h0.queued t tT).1 bit + rwa [ea, ep] at this + have mr : ∀ c ∈ Cs, ∀ t ∈ R c, t ∈ R0 c := fun c hc t ht => by + have tT : t ∈ T := (h.release c hc).mem t ht + have ea := h.inR c hc t ht + have bit := (h.released t tT).2 (by rw [ea]; exact ht) + have := (h0.released t tT).1 bit + rwa [ea] at this + exact + { tcbs := h.tcbs, nodes := h.nodes, cur := h0.cur, aff := h.aff, prio := h.prio, ready := h.ready + release := h.release, inQ := h.inQ, inR := h.inR, queued := h.queued, released := h.released, excl := h.excl + action := h0.action + runQ := fun c hc p hp t ht => h0.runQ c hc p hp t (mq c hc p hp t ht) + runR := fun c hc t ht => h0.runR c hc t (mr c hc t ht) + actOK := fun a => ⟨(h0.actOK a).1, fun c hc k => (h0.actOK a).2 c hc (mr c hc _ k)⟩ + eps := h0.eps, curT := h0.curT, idleT := h0.idleT, ksOut := h0.ksOut, omega := h0.omega } + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtFullTabA.lean b/proofs/Fl4ProofsA64/P3/UtFullTabA.lean new file mode 100644 index 000000000..13166d6c2 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtFullTabA.lean @@ -0,0 +1,189 @@ +/- +P3 (aarch64, A64-P3 Untyped, full-record post, part C): **the relation footprint after a creation, and the table +clauses `tabS1` / `tabExt`, and `unmPt`.** A footprint word of the grown sets is an old footprint word or one of the new objects' +words `N` (`utRt_footSplitA`); a table then stays off the footprint if it is off both. Which words are new is a +hypothesis, discharged at the assembly. + +No named premise. New theorems only. +-/ +import Fl4ProofsA64.P3.UtFullDeadA +import Fl4ProofsA64.P3.UtRtJ7A + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4SpecA64 Classical + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **The new objects' footprint words** `N` against the grown sets `Z'`. -/ +structure UtFootNewA (Z Z' : XSetsA) (N : U64 → Prop) : Prop where + slot : ∀ y, SlotWA Z'.th y → SlotWA Z.th y ∨ ∀ w, (w = y ∨ w = wa y 8#u64 ∨ w = wa y 16#u64 ∨ w = wa y 24#u64) → N w + cs : Z'.Cs = Z.Cs + pp : Z'.Pp = Z.Pp + t2 : Z'.T2 = Z.T2 + tcb : ∀ t ∈ Z'.T, t ∈ Z.T ∨ ∀ w, (w = stA t ∨ w = scA t ∨ w = affA t ∨ w = wa t 3664#u64 ∨ w = runVcpuA t ∨ + w = prioA t ∨ w = domA t) → N w + sc : ∀ c ∈ Z'.C, c ∈ Z.C ∨ ∀ w, (w = wa c 24#u64 ∨ w = wa c 72#u64) → N w + vcpu : ∀ v ∈ Z'.V, v ∈ Z.V ∨ ∀ w, (w = v ∨ w = wa v 24#u64) → N w + s1 : ∀ p ∈ Z'.S1, p ∈ Z.S1 ∨ ∀ i : U64, i.val < 512 → N (peAF p i) + y : ∀ y ∈ Z'.Y, y ∈ Z.Y ∨ ((∀ i : U64, i.val < 512 → N (peAF (wa y 4096#u64) i)) ∧ N (peAF y 511#u64) ∧ + ∀ i : U64, i.val < 256 → N (peAF y i)) + x : ∀ r ∈ Z'.X, r ∈ Z.X ∨ ∀ i : U64, i.val < 512 → N (peAF r i) ∧ N (peAF (wa r 4096#u64) i) + +theorem utRt_footSplitA {Z Z' : XSetsA} {N : U64 → Prop} {s s' : S} (F : UtFootNewA Z Z' N) + (hks : L.ksb s' = L.ksb s) (hcpu : L.cpu s' = L.cpu s) {w : U64} (h : FootA (L := L) Z' s' w) : + FootA (L := L) Z s w ∨ N w := by + have na : ∀ c o, L.nodeAt s' c o = L.nodeAt s c o := fun c o => nodeAt_ksb hks c o + cases h with + | slot y w hy hw => + rcases F.slot y hy with old | new + · exact Or.inl (.slot y w old hw) + · exact Or.inr (new w hw) + | nslot w h => + have O : FootNA (L := L) Z s w → FootA (L := L) Z s w ∨ N w := fun k => Or.inl (.nslot w k) + cases h with + | node c w hc hw => + simp only [na] at hw + exact O (.node c w (F.cs ▸ hc) hw) + | cur w hw => + simp only [na, hcpu] at hw + exact O (.cur w hw) + | tcb t w ht hw => + rcases F.tcb t ht with old | new + · exact O (.tcb t w old hw) + · refine Or.inr (new w ?_) + rcases hw with e | e | e | e | e <;> simp [e] + | sc c hc => + rcases F.sc c hc with old | new + · exact O (.sc c old) + · exact Or.inr (new _ (Or.inl rfl)) + | vcpu v w hv hw => + rcases F.vcpu v hv with old | new + · exact O (.vcpu v w old hw) + · exact Or.inr (new w hw) + | irq i hi => + rw [hks] + exact Or.inl (.nslot _ (.irq i hi)) + | cur2 w hw => + simp only [na, hcpu] at hw + exact O (.cur2 w hw) + | glob w hw => + rw [hks] at hw + exact O (.glob w hw) + | tcb2 t w ht hw => + rcases F.tcb t ht with old | new + · exact O (.tcb2 t w old hw) + · refine Or.inr (new w ?_) + rcases hw with e | e <;> simp [e] + | sc2 c hc => + rcases F.sc c hc with old | new + · exact O (.sc2 c old) + · exact Or.inr (new _ (Or.inr rfl)) + | vs w hw => + rw [hks, F.pp] at hw + cases hw with + | asid hi h => exact O (.vs _ (.asid hi h)) + | pool p lo hp h => exact O (.vs _ (.pool p lo hp h)) + | pt p i hp h => + rcases F.s1 p hp with old | new + · exact O (.vs _ (.pt p i old h)) + · exact Or.inr (new i h) + | high y i hy h => + rcases F.y y hy with old | new + · exact O (.vs _ (.high y i old h)) + · exact Or.inr (new.1 i h) + | ext w hw => + rw [F.t2] at hw + cases hw with + | s2lo r i hr hi => + rcases F.x r hr with old | new + · exact O (.ext _ (.s2lo r i old hi)) + · exact Or.inr (new i hi).1 + | s2hi r i hr hi => + rcases F.x r hr with old | new + · exact O (.ext _ (.s2hi r i old hi)) + · exact Or.inr (new i hi).2 + | s2pt t i ht hi => exact O (.ext _ (.s2pt t i ht hi)) + | wl0 y hy => + rcases F.y y hy with old | new + · exact O (.ext _ (.wl0 y old)) + · exact Or.inr new.2.1 + | wlo y i hy hi => + rcases F.y y hy with old | new + · exact O (.ext _ (.wlo y i old hi)) + · exact Or.inr (new.2.2 i hi) + +/-- A Stage-1 table of the grown sets is off their footprint: it is off the old one (`hold`: old tables by `tabS1`, +new ones by the clear window), off the new words (`hN`) and names no related object (`hobj`). -/ +theorem utRt_tabOffA {Z Z' : XSetsA} {N : U64 → Prop} {m' : McState} {s s' : S} {p : U64} + (F : UtFootNewA { Z with S1 := ∅ } { Z' with S1 := ∅ } N) (hks : L.ksb s' = L.ksb s) (hcpu : L.cpu s' = L.cpu s) + (hold : ∀ i : U64, i.val < 512 → ¬ FootA (L := L) { Z with S1 := ∅ } s (peAF p i)) + (hN : ∀ i : U64, i.val < 512 → ¬ N (peAF p i)) + (hobj : ∀ q, RelPA { Z' with S1 := ∅ } m' q → q ≠ specPtr p) : TabOffA (L := L) Z' m' s' p := + ⟨fun i hi k => (utRt_footSplitA F hks hcpu k).elim (hold i hi) (hN i hi), hobj⟩ + +theorem utRt_extTabOffA {Z Z' : XSetsA} {N : U64 → Prop} {m' : McState} {s s' : S} + (F : UtFootNewA (extZA Z) (extZA Z') N) (hks : L.ksb s' = L.ksb s) (hcpu : L.cpu s' = L.cpu s) + (hold : ∀ q, (q ∈ S2PgE Z'.X Z'.T2 ∨ ∃ y ∈ Z'.Y, q = wa y 4096#u64) → ∀ i : U64, i.val < 512 → + ¬ FootA (L := L) (extZA Z) s (peAF q i)) + (hN : ∀ q, (q ∈ S2PgE Z'.X Z'.T2 ∨ ∃ y ∈ Z'.Y, q = wa y 4096#u64) → ∀ i : U64, i.val < 512 → ¬ N (peAF q i)) + (hobj : ∀ p, (p ∈ Z'.X ∨ p ∈ Z'.Y ∨ p ∈ Z'.T2) → ¬ RelPA (extZA Z') m' (specPtr p)) : + ExtTabOffA (L := L) Z' m' s' := + ⟨fun q hq i hi k => (utRt_footSplitA F hks hcpu k).elim (hold q hq i hi) (hN q hq i hi), hobj⟩ + +/-- `unmPt`. `hk…`: an old unmapped table stays unmapped under the creation (its words, spec object and footprint +placement are kept); `hn…`: a new one is unmapped (zeroed words, fresh spec object, off the grown footprint). -/ +theorem utRt_unmPtA {Z Z' : XSetsA} {OS OS' : Set U64} {s s' : S} {m m' : McState} {σ σ' : State Unit} + {x p sb nt us aligned : U64} {win : Nat} (h : UnmPtSpecA st Z m OS s σ) + (F : UtSlotsA st OS OS' s s' x p sb nt us aligned win) (hn : nt.val < 15) + (hkT : ∀ u ∈ OS, ∀ q asid va, capA st s u = .PageTable q false asid va → capA st s' u = capA st s u → + UnmTabA st Z m s σ .normalPtT q → UnmTabA st Z' m' s' σ' .normalPtT q) + (hkP : ∀ u ∈ OS, ∀ q asid, capA st s u = .Pml4 q false asid → capA st s' u = capA st s u → + UnmTabA st Z m s σ .vsRootPtT q → UnmTabA st Z' m' s' σ' .vsRootPtT q) + (hkS : ∀ u ∈ OS, ∀ q asid va, capA st s u = .Ept q 0#u64 false asid va → capA st s' u = capA st s u → + UnmS2RootA st s σ q → UnmS2RootA st s' σ' q) + (hkH : ∀ u ∈ OS, ∀ q asid, capA st s u = .HighVSpace q false asid → capA st s' u = capA st s u → + UnmHiRootA st s σ q → UnmHiRootA st s' σ' q) + (hnT : nt.val = 11 → ∀ j < win, UnmTabA st Z' m' s' σ' .normalPtT (bjA aligned (zA nt.val us) j)) + (hnP : nt.val = 8 → ∀ j < win, UnmTabA st Z' m' s' σ' .vsRootPtT (bjA aligned (zA nt.val us) j)) + (hnS : nt.val = 13 → ∀ j < win, UnmS2RootA st s' σ' (bjA aligned (zA nt.val us) j)) + (hnH : nt.val = 14 → ∀ j < win, UnmHiRootA st s' σ' (bjA aligned (zA nt.val us) j)) : + UnmPtSpecA st Z' m' OS' s' σ' := by + refine ⟨fun u hu q asid va e => ?_, fun u hu q asid e => ?_, fun u hu q asid va e => ?_, + fun u hu q asid e => ?_⟩ + · rcases F u hu with ⟨uo, k⟩ | ⟨-, f, k⟩ | ⟨j, hj, k⟩ | k + · have e0 := k.symm.trans e + exact hkT u uo q asid va e0 k (h.pt u uo q asid va e0) + · rw [k] at e; cases e + · obtain ⟨h0, rfl⟩ := (utNewCap_schedA hn _ _ _).1 _ _ _ _ (k.symm.trans e) + exact hnT h0 j hj + · rw [k] at e; cases e + · rcases F u hu with ⟨uo, k⟩ | ⟨-, f, k⟩ | ⟨j, hj, k⟩ | k + · have e0 := k.symm.trans e + exact hkP u uo q asid e0 k (h.vs u uo q asid e0) + · rw [k] at e; cases e + · obtain ⟨h0, rfl⟩ := (utNewCap_schedA hn _ _ _).2.1 _ _ _ (k.symm.trans e) + exact hnP h0 j hj + · rw [k] at e; cases e + · rcases F u hu with ⟨uo, k⟩ | ⟨-, f, k⟩ | ⟨j, hj, k⟩ | k + · have e0 := k.symm.trans e + exact hkS u uo q asid va e0 k (h.s2 u uo q asid va e0) + · rw [k] at e; cases e + · obtain ⟨h0, rfl⟩ := (utNewCap_schedA hn _ _ _).2.2.1 _ _ _ _ _ (k.symm.trans e) + exact hnS h0 j hj + · rw [k] at e; cases e + · rcases F u hu with ⟨uo, k⟩ | ⟨-, f, k⟩ | ⟨j, hj, k⟩ | k + · have e0 := k.symm.trans e + exact hkH u uo q asid e0 k (h.hv u uo q asid e0) + · rw [k] at e; cases e + · obtain ⟨h0, rfl⟩ := (utNewCap_schedA hn _ _ _).2.2.2 _ _ _ (k.symm.trans e) + exact hnH h0 j hj + · rw [k] at e; cases e + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtFullUbA.lean b/proofs/Fl4ProofsA64/P3/UtFullUbA.lean new file mode 100644 index 000000000..d2aaf1763 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtFullUbA.lean @@ -0,0 +1,88 @@ +/- +P3 (aarch64, A64-P3 Untyped, full-record post, part A): **`J7A`'s user-frame set shrunk to its aligned words, and P1's creation top from +the record** (`utRt_p1TopA`: `untyped.decode.s1aRtV` at the record's `SysInvA` and `J7A`'s facts). A64 +P1's creation tops take `hal : ∀ b ∈ Ub, Aligned b`, which `J7A`'s existential `Ub` does not give. Every clause that +mentions `Ub` is a statement about its members, so it holds at `{b ∈ Ub | Aligned b}`: no record edit is needed. + +The decode-site facts (`DecPre`, `SysPlace`, `SymL` [J], `A64FrameMasks` [P], `ArchTotal` [P]) are hypotheses. +New theorems only. +-/ +import Fl4ProofsA64.P3.Stage3J7A +import Fl4ProofsA64.P1.S1aRtV +import Fl4ProofsA64.P1.S1aRetype + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Classical + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- The aligned words of a set. -/ +def utAlUbA (Ub : Set U64) : Set U64 := {b | b ∈ Ub ∧ Aligned b} + +theorem utAlUb_alA (Ub : Set U64) : ∀ b ∈ utAlUbA Ub, Aligned b := fun _ h => h.2 + +theorem utAlUb_vrA {Ω Sg Scs OS Vc Ub Pt : Set U64} {Z : U64 → Nat} {kb : U64} + (h : VrOK Ω Sg Scs Z OS Vc Ub kb Pt) : VrOK Ω Sg Scs Z OS Vc (utAlUbA Ub) kb Pt := + h.ofUb fun b hb v => h.ub b v hb.1 + +theorem utAlUb_colA {H : Heap} {Cs OS Vc Ub : Set U64} {s : S} (h : S1aArmCol st H Cs OS Vc Ub s) : + S1aArmCol st H Cs OS Vc (utAlUbA Ub) s := + { case := h.case + kb := ⟨h.kb.ks, h.kb.ksIn, h.kb.slotKs, fun b hb => h.kb.ksU b hb.1, h.kb.hT⟩ + nt := fun b hb => h.nt b hb.1 + pool := fun i ne b hb => h.pool i ne b hb.1 + vh := h.vh } + +theorem utAlUb_frA {S : Type} {st : machine.Store S} [L : LawfulStore st] {OS Ub : Set U64} {s : S} + (h : FrUb st Ub OS s) : FrUb st (utAlUbA Ub) OS s := + fun y hy ptr size rights x1 x2 x3 x4 hc bits hb b al b1 b2 => + ⟨h y hy ptr size rights x1 x2 x3 x4 hc bits hb b al b1 b2, al⟩ + +theorem utAlUb_capA {H : Heap} {OS Ub : Set U64} {s : S} (h : S1aCapCol st H OS Ub s) : + S1aCapCol st H OS (utAlUbA Ub) s := + ⟨fun b hb => h.uf b hb.1, h.uo, h.fo⟩ + +/-- **The creation's P1 post from the record**: at one grown `(H', OS')`, `SysL`, `SymL`, `PoolOff`, `VcCap` and the +arm and cap columns. -/ +theorem utRt_p1TopA {A : Type} {ar : machine.Arch A} (hA : P0.ArchTotal ar) {H : Heap} + {Cs Rp Nt OS U Ac Pp Tb Vc Ub Pt : Set U64} {Lv : U64 → Nat} {ia ie : Nat} {s : S} + (I : SysInvA st H Cs OS U Ac Pp Tb Lv ia ie s) {extra : Aeneas.Std.Array obj.Slot 3#usize} + {slot p sb fi : U64} {dev : Bool} + (D : DecPre st H.tcbs Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits U Ac s (.Untyped p sb fi dev) extra) + (P : SysPlace st H Cs H.frame Rp Nt OS s extra slot) (Y : SymL st H Cs s) + (hcap : obj.Slot.cap st slot s = ok (.Untyped p sb fi dev)) (hfm : A64FrameMasks st s) + (vr : VrOK H.omega H.frame H.scs H.scBits OS Vc Ub (L.ksb s) Pt) (pools : PoolNamed st OS s) + (C : S1aArmCol st H Cs OS Vc Ub s) (K : S1aCapCol st H OS Ub s) (vc : VcCap st Vc OS s) + (a : A) (lbl len buf : U64) : + untyped.decode st ar s a lbl len slot (.Untyped p sb fi dev) buf extra ⦃ _ s' _ => + S1aRtVAt st H Cs OS Vc (utAlUbA Ub) ia ie s s' ⦄ := + untyped.decode.s1aRtV hA D P I.sysl Y hcap hfm (utAlUb_vrA vr) pools (utAlUb_colA C) (utAlUb_capA K) + (utAlUb_alA Ub) vc a lbl len buf + +/-- **The same post with `HeapKsOff` at the grown heap** (`JEntA.ko`). `hks`: the source's region is off the +kernel-state block (part of `SysL` at the source slot). -/ +theorem utRt_p1KoA {A : Type} {ar : machine.Arch A} (hA : P0.ArchTotal ar) {H : Heap} + {Cs Rp Nt OS U Ac Pp Tb Vc Ub Pt : Set U64} {Lv : U64 → Nat} {ia ie : Nat} {s : S} + (I : SysInvA st H Cs OS U Ac Pp Tb Lv ia ie s) {extra : Aeneas.Std.Array obj.Slot 3#usize} + {slot p sb fi : U64} {dev : Bool} + (D : DecPre st H.tcbs Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits U Ac s (.Untyped p sb fi dev) extra) + (P : SysPlace st H Cs H.frame Rp Nt OS s extra slot) (Y : SymL st H Cs s) + (hcap : obj.Slot.cap st slot s = ok (.Untyped p sb fi dev)) (hfm : A64FrameMasks st s) + (vr : VrOK H.omega H.frame H.scs H.scBits OS Vc Ub (L.ksb s) Pt) (pools : PoolNamed st OS s) + (C : S1aArmCol st H Cs OS Vc Ub s) (K : S1aCapCol st H OS Ub s) (vc : VcCap st Vc OS s) + (hks : dev = false → (L.ksb s).val + 788736 ≤ p.val ∨ p.val + 2 ^ sb.val ≤ (L.ksb s).val) + (ko : HeapKsOff H (L.ksb s)) (a : A) (lbl len buf : U64) : + untyped.decode st ar s a lbl len slot (.Untyped p sb fi dev) buf extra ⦃ _ s' _ => + ∃ (H' : Heap) (OS' Vn Vc'' : Set U64), (ArmQIVAt st H Cs OS ia ie s H' OS' Vn s' ∧ TabKeep st s s' ∧ + Vc'' ⊆ Vc ∪ Vn ∧ VcCap st Vc'' OS' s' ∧ S1aArmCol st H' Cs OS' Vc'' (utAlUbA Ub) s' ∧ + S1aCapCol st H' OS' (utAlUbA Ub) s') ∧ HeapKsOff H' (L.ksb s) ⦄ := + untyped.decode.s1aRtVJH hA D P I.sysl Y hcap hfm (utAlUb_vrA vr) pools (utAlUb_colA C) (utAlUb_capA K) + (utAlUb_alA Ub) vc a lbl len buf (fun H' _ _ => HeapKsOff H' (L.ksb s)) ko + fun _ _ _ _ _ _ _ _ _ _ _ _ _ E X _ _ _ => E.heapKsOff X hks ko + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtIdleAllA.lean b/proofs/Fl4ProofsA64/P3/UtIdleAllA.lean new file mode 100644 index 000000000..930e3c733 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtIdleAllA.lean @@ -0,0 +1,35 @@ +/- +P3 (aarch64, A64-P3 Untyped, closure item C; Architect 04:38Z, J13): the drop premises about every core's idle +objects, from the record's `idleAll` (`IdleAllA`, `P3/Stage3FieldsA`). + +* **`utIdleAll_dropA`**: with a removed set inside a RAM untyped of `OS` (which `A64Layout.idleOff` keeps off the idle + area), no core's idle thread or idle SC and no platform idle thread is removed. +-/ +import Fl4ProofsA64.P3.UtDropA +import Fl4ProofsA64.P1.MdbLive +import Fl4ProofsA64.P3.Stage3FieldsA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST + +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **The drop's idle premises** (module docstring). -/ +theorem utIdleAll_dropA {ia ie : Nat} {Cs : Set U64} {m : Fl4SpecA64.McState} {s : S} + (h : IdleAllA st ia ie Cs m s) {D : Set Fl4SpecA64.ObjRef} {p sb : U64} + (off : ie ≤ p.val ∨ p.val + 2 ^ sb.val ≤ ia) + (hD : ∀ a : U64, specPtr a ∈ D → p.val ≤ a.val ∧ a.val < p.val + 2 ^ sb.val) : + (∀ c ∈ Cs, specPtr (L.mem s (L.nodeAt s c layout.node.IDLE_THREAD)) ∉ D) ∧ + (∀ c ∈ Cs, specPtr (L.mem s (L.nodeAt s c layout.node.IDLE_SC)) ∉ D) ∧ + ∀ c < Fl4SpecA64.fl4A64Platform.numNodes, m.idleThreadOf c ∉ D := by + have out : ∀ a : U64, ia ≤ a.val → a.val < ie → specPtr a ∉ D := fun a h1 h2 hd => by + obtain ⟨g1, g2⟩ := hD a hd + rcases off with o | o <;> omega + refine ⟨fun c hc => out _ (h.tcb c hc).1 (h.tcb c hc).2, fun c hc => out _ (h.sc c hc).1 (h.sc c hc).2, + fun c hc => ?_⟩ + obtain ⟨t, e, h1, h2⟩ := h.spec c hc + rw [e] + exact out t h1 h2 + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtIpcHeapA.lean b/proofs/Fl4ProofsA64/P3/UtIpcHeapA.lean new file mode 100644 index 000000000..814fdc5b3 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtIpcHeapA.lean @@ -0,0 +1,19 @@ +/- +P3 (aarch64, A64-P3 Stage-3 integration; Architect 00:54Z): **`UtIpcHeapA`**, the IPC ghosts' objects in the heap. +Defined here (a leaf module) so that both `JStage3A` and the Untyped line (`UtClosA`) import the one definition. + +* **[J] `UtIpcHeapA`**: the IPC ghost sets' notifications, endpoints and replies are P1 heap objects of those kinds. +-/ +import Fl4ProofsA64.P1.QueueSchedInv +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open Fl4ProofsA64.P1 + +/-- **[J]** The IPC ghosts' objects are P1 heap objects of their kinds. -/ +structure UtIpcHeapA (H : Heap) (N E Rp : Set U64) : Prop where + ntfn : ∀ n ∈ N, H n = some .ntfn + ep : ∀ e ∈ E, H e = some .ep + reply : ∀ r ∈ Rp, H r = some .reply + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtJ5CtxA.lean b/proofs/Fl4ProofsA64/P3/UtJ5CtxA.lean new file mode 100644 index 000000000..02d555392 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtJ5CtxA.lean @@ -0,0 +1,207 @@ +/- +P3 (aarch64, A64-P3 Untyped, full-record post, part A): **`MdbInvA` after a fresh child that may be of a badged +kind.** A64 P1's `FreshChildCtxA` asks `hc_nb : isBadgedA c = false`, which a new endpoint or notification cap does +not satisfy, so it cannot serve Retype for those types. `UtFreshCtxA` is that context with `hc_nb` weakened to +`hc_bdg` (a badged-kind new cap names another region than its successor); the proof of `inv` is P1's +(`P1/MdbInvInsA`) except in the `badges` case at the new slot. No P1 statement changed. + +No named premise. +-/ +import Fl4ProofsA64.P1.MdbInvInsA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P0.Consts Fl4Proofs.P3 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST + +section +variable {S : Type _} {st : machine.Store S} [L : LawfulStore st] + +noncomputable section + +/-- Inserting a child `dest` right after a revocable, non-badged `src` (`MakePool`, `DeriveSubframe`, +`insert_new_cap`), where `src` may have its `Untyped` free index updated (`c1` same `shapeA` as `src`). -/ +structure UtFreshCtxA (st : machine.Store S) [L : LawfulStore st] (OS : Set U64) + (G : List (List U64)) (par : U64 → Option U64) (s s' : S) (src dest : U64) + (c c1 : capability.Cap) (rv fb : Bool) : Prop where + hM : MdbLk st OS G s + hOk : Mdb.MdbOK par G + hI : MdbInvA st G par s + hsrc : src ∈ G.flatten + hdest : dest ∉ G.flatten + hsrc_rev : revA st s src = true + hsrc_nb : isBadgedA (capA st s src) = false + hsh1 : shapeA c1 = shapeA (capA st s src) + hbd1 : badgeA c1 = badgeA (capA st s src) + /-- (weaker than P1's `hc_nb`) a badged-kind new cap names another region than its successor. -/ + hc_bdg : isBadgedA c = true → NxA st s src ≠ 0#u64 → sraA c (capA st s (NxA st s src)) = false + hc_ut : isUtA c = true → rv = true + hc_self : isZbA c = false → sraA c c = true + hc_in_src : sraA (capA st s src) c = true + hsr_left : ∀ a ∈ G.flatten, sraA (capA st s a) c = srMA st s a src + hanc_nb : ∀ a ∈ G.flatten, Mdb.Desc par src a → isBadgedA (capA st s a) = false + hdest_after : NxA st s src ≠ 0#u64 → isPFA rv c (capA st s (NxA st s src)) (fbA st s (NxA st s src)) = false + hdest_ca : ∀ ch ∈ G, ∀ A B, ch = A ++ src :: B → Mdb.CA (srFA st s') dest B + hnext : ∀ a, (a = dest ∨ a ∈ G.flatten) → NxA st s' a = + if a = dest then NxA st s src else if a = src then dest else NxA st s a + hcap : ∀ a, (a = dest ∨ a ∈ G.flatten) → capA st s' a = + if a = dest then c else if a = src then c1 else capA st s a + hrev : ∀ a, (a = dest ∨ a ∈ G.flatten) → revA st s' a = + if a = dest then rv else revA st s a + hfb : ∀ a, (a = dest ∨ a ∈ G.flatten) → fbA st s' a = + if a = dest then fb else fbA st s a + +namespace UtFreshCtxA +variable {OS : Set U64} {G : List (List U64)} {par : U64 → Option U64} {s s' : S} {src dest : U64} + {c c1 : capability.Cap} {rv fb : Bool} (h : UtFreshCtxA st OS G par s s' src dest c c1 rv fb) +include h + +theorem dne {z : U64} (hz : z ∈ G.flatten) : z ≠ dest := fun e => h.hdest (e ▸ hz) +theorem sd : src ≠ dest := h.dne h.hsrc +theorem cz {z : U64} (hz : z ∈ G.flatten) : + shapeA (capA st s' z) = shapeA (capA st s z) ∧ badgeA (capA st s' z) = badgeA (capA st s z) := by + rw [h.hcap z (Or.inr hz), ite_eq_right (h.dne hz)] + by_cases hzs : z = src + · subst hzs; simp [h.hsh1, h.hbd1] + · simp [hzs] +theorem rz {z : U64} (hz : z ∈ G.flatten) : revA st s' z = revA st s z := by + rw [h.hrev z (Or.inr hz), ite_eq_right (h.dne hz)] +theorem fz {z : U64} (hz : z ∈ G.flatten) : fbA st s' z = fbA st s z := by + rw [h.hfb z (Or.inr hz), ite_eq_right (h.dne hz)] +theorem nz {z : U64} (hz : z ∈ G.flatten) (hzs : z ≠ src) : NxA st s' z = NxA st s z := by + rw [h.hnext z (Or.inr hz), ite_eq_right (h.dne hz), ite_eq_right hzs] +theorem ns : NxA st s' src = dest := by rw [h.hnext src (Or.inr h.hsrc), ite_eq_right h.sd, ite_eq_left rfl] +theorem nd : NxA st s' dest = NxA st s src := by rw [h.hnext dest (Or.inl rfl), ite_eq_left rfl] +theorem cd : capA st s' dest = c := by rw [h.hcap dest (Or.inl rfl), ite_eq_left rfl] +theorem cs : capA st s' src = c1 := by rw [h.hcap src (Or.inr h.hsrc), ite_eq_right h.sd, ite_eq_left rfl] +theorem rd : revA st s' dest = rv := by rw [h.hrev dest (Or.inl rfl), ite_eq_left rfl] +theorem fd : fbA st s' dest = fb := by rw [h.hfb dest (Or.inl rfl), ite_eq_left rfl] + +theorem pm {x y : U64} (hx : x ∈ G.flatten) (hy : y ∈ G.flatten) : isPMA st s' x y = isPMA st s x y := by + unfold isPMA; rw [h.rz hx, h.fz hy] + exact isPFA_congr (h.cz hx).1 (h.cz hx).2 (h.cz hy).1 (h.cz hy).2 +theorem sm {x y : U64} (hx : x ∈ G.flatten) (hy : y ∈ G.flatten) : srMA st s' x y = srMA st s x y := by + unfold srMA; exact sraA_congr (h.cz hx).1 (h.cz hy).1 +theorem sf {x y : U64} (hx : x ∈ G.flatten) (hy : y ∈ G.flatten) : srFA st s' x y = srFA st s x y := + srFA_congr (h.sm hx hy) (isFrA_of_shape (h.cz hx).1) (h.rz hx) + +theorem sr_left {a : U64} (ha : a ∈ G.flatten) : srMA st s' a dest = srMA st s a src := by + unfold srMA; rw [h.cd, sraA_congr (h.cz ha).1 rfl]; exact h.hsr_left a ha + +theorem pd (z : U64) : par z ≠ some dest := fun e => h.hdest (h.hOk.par_mem z dest e).2 +theorem dn : par dest = none := by + cases e : par dest with + | none => rfl + | some p => exact absurd (h.hOk.par_mem dest p e).1 h.hdest + +theorem desc (y x : U64) : Mdb.Desc (insParA par src dest true) y x ↔ + Mdb.Desc par y x ∨ (y = dest ∧ (x = src ∨ Mdb.Desc par src x)) := by + have hnp : (if true then some src else par src) ≠ some dest := by simp [h.sd] + unfold insParA + rw [Mdb.desc_update_iff h.pd h.dn hnp, np_desc_iffA] + simp + +theorem dest_no_desc (x : U64) : ¬ Mdb.Desc par dest x := fun e => h.hdest (desc_memA h.hOk e).1 +theorem memI (z : U64) : z ∈ (insG src dest G).flatten ↔ z = dest ∨ z ∈ G.flatten := mem_insG_flat h.hsrc + +/-- **`MdbInvA` after inserting a fresh child**, badged kinds included. -/ +theorem inv : MdbInvA st (insG src dest G) (insParA par src dest true) s' := by + refine ⟨fun z p hz => ?_, fun x y hd => ?_, fun y hy hn x hx hnd => ?_, fun ch' hch' => ?_, + fun y hy hn hbg hsr hbne hb0 => ?_, fun x hx hu => ?_, fun x hx hz => ?_⟩ + · unfold insParA at hz + by_cases hzd : z = dest + · subst hzd; rw [Function.update_self] at hz + have hp : p = src := by simpa using hz.symm + rw [hp, h.rz h.hsrc]; exact h.hsrc_rev + · rw [Function.update_of_ne hzd] at hz + rw [h.rz (h.hOk.par_mem _ _ hz).2]; exact h.hI.orig _ _ hz + · rcases (h.desc y x).1 hd with hd | ⟨hyd, hxs | hd⟩ + · obtain ⟨h1, h2⟩ := desc_memA h.hOk hd + rw [h.pm h2 h1]; exact h.hI.desc x y hd + · rw [hyd, hxs] + unfold isPMA isPFA + rw [h.rz h.hsrc, h.hsrc_rev, h.cs, h.cd, sraA_congr h.hsh1 rfl, h.hc_in_src, + isBadgedA_of_shape h.hsh1, h.hsrc_nb] + rfl + · rw [hyd] + have hx := (desc_memA h.hOk hd).2 + have h0 := h.hI.desc x src hd + unfold isPMA isPFA at h0 ⊢ + simp only [Bool.and_eq_true] at h0 + rw [h.rz hx, h0.1.1, isBadgedA_of_shape (h.cz hx).1, h.hanc_nb x hx hd] + have hsr := h.sr_left hx + unfold srMA at hsr; rw [hsr, h0.1.2]; rfl + · rcases (h.memI y).1 hy with hyd | hy + · rw [hyd] at hn hnd hx ⊢ + rw [h.nd] at hn hnd ⊢ + have hN := h.hM.next_memA h.hsrc hn + rcases hx with hxd | hx + · rw [hxd]; unfold isPMA + rw [h.rd, h.cd, h.fz hN, isPFA_congr rfl rfl (h.cz hN).1 (h.cz hN).2] + exact h.hdest_after hn + · have hx' : x = src ∨ Mdb.Desc par src x := by + rcases (h.desc dest x).1 hx with e | ⟨-, e⟩ + · exact absurd e (h.dest_no_desc x) + · exact e + have hnd' : ¬ Mdb.Desc par (NxA st s src) x := fun e => hnd ((h.desc _ x).2 (Or.inl e)) + have hxm : x ∈ G.flatten := by rcases hx' with rfl | e; exact h.hsrc; exact (desc_memA h.hOk e).2 + rw [h.pm hxm hN]; exact h.hI.after src h.hsrc hn x hx' hnd' + · by_cases hys : y = src + · rw [hys] at hn hnd hx hy ⊢ + rw [h.ns] at hn hnd ⊢ + rcases hx with hxs | hx + · rw [hxs] at hnd; exact absurd ((h.desc dest src).2 (Or.inr ⟨rfl, Or.inl rfl⟩)) hnd + · have hx' : Mdb.Desc par src x := ((h.desc src x).1 hx).resolve_right (fun e => h.sd e.1) + exact absurd ((h.desc dest x).2 (Or.inr ⟨rfl, Or.inr hx'⟩)) hnd + · rw [h.nz hy hys] at hn hnd ⊢ + have hw := h.hM.next_memA hy hn + have hx' : x = y ∨ Mdb.Desc par y x := by + rcases hx with rfl | e + · exact Or.inl rfl + · exact Or.inr (((h.desc y x).1 e).resolve_right (fun e => h.dne hy e.1)) + have hnd' : ¬ Mdb.Desc par (NxA st s y) x := fun e => hnd ((h.desc _ x).2 (Or.inl e)) + have hxm : x ∈ G.flatten := by rcases hx' with rfl | e; exact hy; exact (desc_memA h.hOk e).2 + rw [h.pm hxm hw]; exact h.hI.after y hy hn x hx' hnd' + · rw [insG_eq] at hch' + obtain ⟨ch, hch, rfl⟩ := List.mem_map.1 hch' + have hchm : ∀ z, z ∈ ch → z ∈ G.flatten := fun z hz => List.mem_flatten.2 ⟨_, hch, hz⟩ + by_cases hsc : src ∈ ch + · obtain ⟨A, B, rfl⟩ := List.append_of_mem hsc + have hnd0 : (A ++ src :: B).Nodup := (List.nodup_flatten.1 h.hOk.nodup).1 _ hch + rw [Mdb.insAfter_split (Mdb.nodup_mid hnd0).1] + refine Mdb.chunkL_ins2 (h.hI.chunk _ hch) (fun u hu v hv => h.sf (hchm u hu) (hchm v hv)) + (fun u hu => srFA_congr (h.sr_left (hchm u hu)) (isFrA_of_shape (h.cz (hchm u hu)).1) (h.rz (hchm u hu))) + (h.hdest_ca _ hch A B rfl) (srFA_of_srMA ?_) + unfold srMA; rw [h.cs, h.cd, sraA_congr h.hsh1 rfl]; exact h.hc_in_src + · rw [Mdb.insAfter_of_not_mem hsc] + exact Mdb.chunkL_congr (h.hI.chunk _ hch) (fun u hu v hv => h.sf (hchm u hu) (hchm v hv)) + · rcases (h.memI y).1 hy with rfl | hy + · rw [h.nd] at hn hsr + have hw := h.hM.next_memA h.hsrc hn + rw [h.cd] at hbg + unfold srMA at hsr + rw [h.cd, sraA_congr rfl (h.cz hw).1, h.hc_bdg hbg hn] at hsr + cases hsr + · by_cases hys : y = src + · subst hys; rw [h.cs, isBadgedA_of_shape h.hsh1, h.hsrc_nb] at hbg; cases hbg + · rw [h.nz hy hys] at hn hsr hbne hb0 ⊢ + have hw := h.hM.next_memA hy hn + rw [h.fz hw] + rw [(h.cz hy).2, (h.cz hw).2] at hbne + rw [(h.cz hw).2] at hb0 + rw [isBadgedA_of_shape (h.cz hy).1] at hbg + exact h.hI.badges y hy hn hbg (by rw [← h.sm hy hw]; exact hsr) hbne hb0 + · rcases (h.memI x).1 hx with rfl | hx + · rw [h.cd] at hu; rw [h.rd]; exact h.hc_ut hu + · rw [h.rz hx]; rw [isUtA_of_shape (h.cz hx).1] at hu; exact h.hI.utrev x hx hu + · rcases (h.memI x).1 hx with rfl | hx + · rw [h.cd] at hz; unfold srMA; rw [h.cd]; exact h.hc_self hz + · rw [isZbA_of_shape (h.cz hx).1] at hz; rw [h.sm hx hx]; exact h.hI.self x hx hz + +end UtFreshCtxA + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtJ5InsA.lean b/proofs/Fl4ProofsA64/P3/UtJ5InsA.lean new file mode 100644 index 000000000..b083cb805 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtJ5InsA.lean @@ -0,0 +1,203 @@ +/- +P3 (aarch64, A64-P3 Untyped, full-record post, part A): **one `cdt.insert_new_cap` keeps `MdbInvA`** (the step of the +`J5A` loop). + +* **`cdt.insert_new_cap.fbb`**: every `OS` slot but `dest` keeps its first-badged bit, and `dest`'s is set (the + sibling of P1's `cdt.insert_new_cap.rvb`). +* **`utJ5_insA`**: the insert's writes as `UtFreshCtxA` (`P3/UtJ5CtxA`), hence `MdbInvA` at the ghost + `insG src dest G` / `update par dest src`. The facts about the new cap against the old tree (`hsr_left`, + `hdest_after`, `hdest_ca`, the class facts) are hypotheses, discharged along the loop. + +* **`utJ5_irqInsA`**: the same insert keeps `IrqRunA` and `HandlersActiveA`: neither the source (an untyped) nor the + new cap is an IRQ handler. + +* **`utJ5_inSrcA`**: `hc_in_src`: a new cap in the source's region is in the source untyped's region. + +No named premise. New theorems only. +-/ +import Fl4ProofsA64.P3.UtJ5CtxA +import Fl4ProofsA64.P1.MdbTreeL +import Fl4ProofsA64.P1.MdbTreeK +import Fl4ProofsA64.P1.MdbIrqInvA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P0.Consts Fl4ProofsA64.P1 Fl4Proofs.P3 Fl4ProofsA64.P1.W7.R.ST + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {T Ω Sg : Set U64} + +private theorem utFbb_or (a : U64) : ((a ||| RVB) ||| Fl4ProofsA64.P1.W7.R.ST.FB) &&& Fl4ProofsA64.P1.W7.R.ST.FB = Fl4ProofsA64.P1.W7.R.ST.FB := by + rw [UScalar.eq_equiv_bv_eq] + simp only [UScalar.bv_and, UScalar.bv_or] + apply BitVec.eq_of_getLsbD_eq; intro i + simp only [BitVec.getLsbD_and, BitVec.getLsbD_or] + cases a.bv.getLsbD i <;> cases (RVB : U64).bv.getLsbD i <;> cases (Fl4ProofsA64.P1.W7.R.ST.FB : U64).bv.getLsbD i <;> simp + +private theorem utFbb_ptr (a n : U64) : + ((a &&& ~~~ obj.MDB_PTR_MASK) ||| (n &&& obj.MDB_PTR_MASK)) &&& Fl4ProofsA64.P1.W7.R.ST.FB = a &&& Fl4ProofsA64.P1.W7.R.ST.FB := + ptr_bits a n _ _ (by unfold obj.MDB_PTR_MASK; rfl) + +private theorem utSetNf_fbb (s : S) (x n : U64) : + obj.Slot.set_next_flags st x s n true true ⦃ s' => ∃ w, s' = L.wr s (wa x 24#u64) w ∧ w &&& Fl4ProofsA64.P1.W7.R.ST.FB = Fl4ProofsA64.P1.W7.R.ST.FB ⦄ := by + unfold obj.Slot.set_next_flags obj.Slot.next_word + simp only [lift, bind_ok, cte_next, L.read_eq, fb_val, rvb_val] + simp only [↓reduceIte, bind_ok, L.write_eq, WP.spec_ok] + exact ⟨_, rfl, utFbb_or _⟩ + +/-- **`insert_new_cap` and the first-badged bit.** -/ +theorem cdt.insert_new_cap.fbb {OS : Set U64} (hO : OSOK T Ω Sg OS) {s : S} {src dest : U64} {nc : capability.Cap} + (hsrc : src ∈ OS) (hd : dest ∈ OS) (hsd : src ≠ dest) (hw : CapRt.EncWf nc) + (hnN : NxA st s src ≠ 0#u64 → NxA st s src ∈ OS) : + cdt.insert_new_cap st s src dest nc ⦃ s' => + (∀ y ∈ OS, y ≠ dest → L.mem s' (wa y 24#u64) &&& Fl4ProofsA64.P1.W7.R.ST.FB = L.mem s (wa y 24#u64) &&& Fl4ProofsA64.P1.W7.R.ST.FB) ∧ + L.mem s' (wa dest 24#u64) &&& Fl4ProofsA64.P1.W7.R.ST.FB = Fl4ProofsA64.P1.W7.R.ST.FB ⦄ := by + unfold cdt.insert_new_cap + rw [next_eq, bind_ok] + obtain ⟨w0, w1, he, -⟩ := CapRt.decode_encode nc hw + rw [set_cap_eq he, bind_ok] + have K2 : ∀ y ∈ OS, L.mem (L.wr (L.wr s dest w0) (wa dest 8#u64) w1) (wa y 24#u64) = L.mem s (wa y 24#u64) := + fun y hy => (set_cap_links hO hd y hy).2 + generalize L.wr (L.wr s dest w0) (wa dest 8#u64) w1 = s2 at K2 ⊢ + rw [set_prev_eq, bind_ok] + have K3 : ∀ y ∈ OS, L.mem (L.wr s2 (wa dest 16#u64) src) (wa y 24#u64) = L.mem s (wa y 24#u64) := + fun y hy => (w24_wr16 hO s2 hd hy src).trans (K2 y hy) + generalize L.wr s2 (wa dest 16#u64) src = s3 at K3 ⊢ + refine WP.spec_bind (utSetNf_fbb s3 dest _) ?_ + rintro s4 ⟨w4, rfl, hw4⟩ + have K4 : ∀ y ∈ OS, y ≠ dest → L.mem (L.wr s3 (wa dest 24#u64) w4) (wa y 24#u64) = L.mem s (wa y 24#u64) := + fun y hy e => (w24_wr24 hO s3 hd hy e w4).trans (K3 y hy) + have D4 : L.mem (L.wr s3 (wa dest 24#u64) w4) (wa dest 24#u64) &&& Fl4ProofsA64.P1.W7.R.ST.FB = Fl4ProofsA64.P1.W7.R.ST.FB := by rw [w24_self hO s3 hd]; exact hw4 + generalize L.wr s3 (wa dest 24#u64) w4 = s5 at K4 D4 ⊢ + have fin : ∀ s6 : S, (∀ y ∈ OS, L.mem s6 (wa y 24#u64) = L.mem s5 (wa y 24#u64)) → + obj.Slot.set_next st src s6 dest ⦃ s' => + (∀ y ∈ OS, y ≠ dest → L.mem s' (wa y 24#u64) &&& Fl4ProofsA64.P1.W7.R.ST.FB = L.mem s (wa y 24#u64) &&& Fl4ProofsA64.P1.W7.R.ST.FB) ∧ + L.mem s' (wa dest 24#u64) &&& Fl4ProofsA64.P1.W7.R.ST.FB = Fl4ProofsA64.P1.W7.R.ST.FB ⦄ := by + intro s6 h6 + rw [set_next_eq, WP.spec_ok] + refine ⟨fun y hy e => ?_, ?_⟩ + · by_cases ys : y = src + · subst ys + rw [w24_self hO s6 hy, utFbb_ptr, h6 y hy, K4 y hy e] + · rw [w24_wr24 hO s6 hsrc hy ys, h6 y hy, K4 y hy e] + · rw [w24_wr24 hO s6 hsrc hd hsd.symm, h6 dest hd]; exact D4 + simp only [obj.Slot.is_null, bind_ok] + split + · simp only [bind_ok] + exact fin s5 fun _ _ => rfl + · rename_i hn0 + have hN0 : NxA st s src ≠ 0#u64 := by simpa using hn0 + rw [set_prev_eq, bind_ok] + exact fin _ fun y hy => w24_wr16 hO s5 (hnN hN0) hy dest + +/-- **`MdbInvA` across one `insert_new_cap`.** -/ +theorem utJ5_insA {OS : Set U64} (hO : OSOK T Ω Sg OS) {G : List (List U64)} {par : U64 → Option U64} {s : S} + (hM : MdbLk st OS G s) (hT : Mdb.MdbOK par G) (hI : MdbInvA st G par s) + {src dest : U64} {nc : capability.Cap} (hsrc : src ∈ G.flatten) (hd : dest ∈ OS) (hd0 : capA st s dest = .Null) + (hdc : obj.sext48 dest = ok dest) (hw : CapRt.EncWf nc) + (hsrc_rev : revA st s src = true) (hsrc_nb : isBadgedA (capA st s src) = false) + (hc_bdg : isBadgedA nc = true → NxA st s src ≠ 0#u64 → sraA nc (capA st s (NxA st s src)) = false) + (hc_self : isZbA nc = false → sraA nc nc = true) + (hc_in_src : sraA (capA st s src) nc = true) + (hsr_left : ∀ a ∈ G.flatten, sraA (capA st s a) nc = srMA st s a src) + (hanc_nb : ∀ a ∈ G.flatten, Mdb.Desc par src a → isBadgedA (capA st s a) = false) + (hdest_after : NxA st s src ≠ 0#u64 → + isPFA true nc (capA st s (NxA st s src)) (fbA st s (NxA st s src)) = false) + (hdest_ca : ∀ s' : S, (∀ a ∈ G.flatten, capA st s' a = capA st s a ∧ revA st s' a = revA st s a) → + capA st s' dest = nc → ∀ ch ∈ G, ∀ A B, ch = A ++ src :: B → Mdb.CA (srFA st s') dest B) : + cdt.insert_new_cap st s src dest nc ⦃ s' => + MdbInvA st (insG src dest G) (Function.update par dest (some src)) s' ⦄ := by + have inOS : ∀ y ∈ G.flatten, y ∈ OS := fun y hy => ((hM.members y).1 hy).1 + have sOS := inOS src hsrc + have dG : dest ∉ G.flatten := fun hm => ((hM.members dest).1 hm).2 hd0 + have hsd : src ≠ dest := fun e => dG (e ▸ hsrc) + have hN := (MdbLk.nxG hO hM hsrc).2 + refine WP.spec_mono (spec_andL (cdt.insert_new_cap.lr hO (LR.self s) sOS hd hsd hdc hw (fun h => hN h)) + (spec_andL (cdt.insert_new_cap.rvb hO sOS hd hsd hw hN) (cdt.insert_new_cap.fbb hO sOS hd hsd hw hN))) ?_ + rintro s' ⟨⟨hL, ed, ek⟩, ⟨rk, rd⟩, fk, fd⟩ + have old : ∀ a ∈ G.flatten, a ≠ dest := fun a ha e => dG (e ▸ ha) + have capK : ∀ a ∈ G.flatten, capA st s' a = capA st s a := fun a ha => ek a (inOS a ha) (old a ha) + have revK : ∀ a ∈ G.flatten, revA st s' a = revA st s a := fun a ha => by + unfold revA + rw [show (281474976710656#u64 : U64) = RVB from rfl, rk a (inOS a ha) (old a ha)] + have C : UtFreshCtxA st OS G par s s' src dest nc (capA st s src) true true := + { hM := hM, hOk := hT, hI := hI, hsrc := hsrc, hdest := dG, hsrc_rev := hsrc_rev, hsrc_nb := hsrc_nb + hsh1 := rfl, hbd1 := rfl, hc_bdg := hc_bdg, hc_ut := fun _ => rfl, hc_self := hc_self + hc_in_src := hc_in_src, hsr_left := hsr_left, hanc_nb := hanc_nb, hdest_after := hdest_after + hdest_ca := hdest_ca s' (fun a ha => ⟨capK a ha, revK a ha⟩) ed + hnext := fun a ha => by + have aOS : a ∈ OS := ha.elim (fun e => e ▸ hd) (inOS a) + rw [show NxA st s' a = _ from (hL a aOS).2] + by_cases ad : a = dest + · subst ad; simp [hsd.symm] + · simp [ad] + hcap := fun a ha => by + by_cases ad : a = dest + · subst ad; simp [ed] + · have am := ha.resolve_left ad + rw [ite_eq_right ad, capK a am] + by_cases asr : a = src + · rw [ite_eq_left asr, asr] + · rw [ite_eq_right asr] + hrev := fun a ha => by + by_cases ad : a = dest + · subst ad + rw [ite_eq_left rfl] + unfold revA + rw [show (281474976710656#u64 : U64) = RVB from rfl, rd] + decide + · rw [ite_eq_right ad, revK a (ha.resolve_left ad)] + hfb := fun a ha => by + by_cases ad : a = dest + · subst ad + rw [ite_eq_left rfl] + unfold fbA + rw [show (562949953421312#u64 : U64) = Fl4ProofsA64.P1.W7.R.ST.FB from rfl, fd] + decide + · have am := ha.resolve_left ad + rw [ite_eq_right ad] + unfold fbA + rw [show (562949953421312#u64 : U64) = Fl4ProofsA64.P1.W7.R.ST.FB from rfl, fk a (inOS a am) ad] } + exact C.inv + +/-- **`IrqRunA` / `HandlersActiveA` across one insert** of a cap that is no IRQ handler under a source that is none. +`hks` / `hst`: the insert writes no IRQ-state word. -/ +theorem utJ5_irqInsA {G : List (List U64)} {s s' : S} (hR : IrqRunA st G s) (hH : HandlersActiveA st G s) + (hnd : G.flatten.Nodup) {src dest : U64} (hsrc : src ∈ G.flatten) (hdest : dest ∉ G.flatten) + (hk : ∀ y ∈ G.flatten, capA st s' y = capA st s y) (hs : ∀ i, capA st s src ≠ .IrqHandler i) + (hd : ∀ i, capA st s' dest ≠ .IrqHandler i) (hks : L.ksb s' = L.ksb s) + (hst : ∀ i : U64, i.val < 5376 → L.mem s' (irqStAddrA (L.ksb s) i) = L.mem s (irqStAddrA (L.ksb s) i)) : + IrqRunA st (insG src dest G) s' ∧ HandlersActiveA st (insG src dest G) s' := + ⟨hR.ins hnd hsrc hdest (fun y hy i => by rw [hk y hy]) (fun i h => absurd h (hd i)) + (fun i h => absurd h (hs i)), + hH.ins_derived hsrc (fun y hy i h => (hk y hy).symm.trans h) (fun i h => absurd h (hd i)) hks hst⟩ + +/-- **`hc_in_src`**: a cap `objects::create_object` returns for a window inside the source's region names a region +inside it. -/ +theorem utJ5_inSrcA {n : Nat} (hn : n < 15) {p sb fi base us : U64} {d dev : Bool} (hsb : sb.val < 64) + (hz : (zA n us).val < 64) (hlo : p.val ≤ base.val) (hhi : base.val + 2 ^ (zA n us).val ≤ p.val + 2 ^ sb.val) + (hfit : p.val + 2 ^ sb.val ≤ 2 ^ 64) : sraA (.Untyped p sb fi d) (newCapA n base us dev) = true := by + rw [sraA_ut, newCapA_phys, newCapA_ptr, newCapA_size hn] + have h1 : 1 ≤ 2 ^ (zA n us).val := Nat.one_le_two_pow + have h2 : 1 ≤ 2 ^ sb.val := Nat.one_le_two_pow + have e1 : (core.num.U64.wrapping_add base (mskA (zA n us))).val = base.val + (2 ^ (zA n us).val - 1) := by + rw [wa_val, mskA_val hz] + exact Nat.mod_eq_of_lt (by omega) + have e2 : (core.num.U64.wrapping_add p (mskA sb)).val = p.val + (2 ^ sb.val - 1) := by + rw [wa_val, mskA_val hsb] + exact Nat.mod_eq_of_lt (by omega) + have a : p ≤ base := hlo + have b : core.num.U64.wrapping_add base (mskA (zA n us)) ≤ core.num.U64.wrapping_add p (mskA sb) := by + show (core.num.U64.wrapping_add base (mskA (zA n us))).val ≤ (core.num.U64.wrapping_add p (mskA sb)).val + omega + have c : base ≤ core.num.U64.wrapping_add base (mskA (zA n us)) := by + show base.val ≤ (core.num.U64.wrapping_add base (mskA (zA n us))).val + omega + simp [a, b, c] + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtJ5LoopA.lean b/proofs/Fl4ProofsA64/P3/UtJ5LoopA.lean new file mode 100644 index 000000000..51b0ead25 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtJ5LoopA.lean @@ -0,0 +1,614 @@ +/- +P3 (aarch64, A64-P3 Untyped, full-record post, part A): **the `J5A` window loop of a creation** (`cdt.insert`s). + +* **`utJ5_sraPhysA`**, **`utJ5_sraMeetA`**: caps that are `same_region_as` agree on being physical, and the second + starts where the first does or inside it. +* **`utJ5_afterSrcA`**: no cap after the source in its chain is in a new cap's region (`hc_bdg`, `hdest_after`, + `hdest_ca`). +* **`utRetype_j5A`**: after a non-reset Retype, `MdbInvA` at the grown ghost (`utGA` / `utPA`), `IrqRunA`, and every + `IrqHandler` cap in the grown chains was there before. Per insert: `utJ5_insA`. + +Facts about the old caps come from the old tree (`TreeA`: `UtInvA.noov` / `drange` / `inc` / `anc`). No named +premise. New theorems only. +-/ +import Fl4ProofsA64.P3.UtJ5InsA +import Fl4ProofsA64.P3.UtJ5SraA +import Fl4ProofsA64.P3.UtRtCdtA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P0.Consts Fl4ProofsA64.P1 Fl4Proofs.P3 Fl4ProofsA64.P1.W7.R.ST Classical + +local notation "wa" => core.num.U64.wrapping_add + +set_option maxHeartbeats 4000000 in +theorem utJ5_sraPhysA {a b : capability.Cap} (h : sraA a b = true) : physA a = physA b := by + have e : objects.cap_is_physical a = objects.cap_is_physical b := by + cases a <;> cases b + all_goals try rfl + all_goals try (rw [sraA_of_eq (v := false) rfl] at h; exact Bool.noConfusion h) + all_goals + exfalso + rw [sraA_of_eq (v := false) (by simp [objects.same_region_as, objects.cap_is_physical])] at h + exact Bool.noConfusion h + rw [physA_eq, physA_eq] at e + exact okinj e + +theorem utJ5_capPtrA {a b : capability.Cap} (h : capability.Cap.ptr a = capability.Cap.ptr b) : ptrA a = ptrA b := by + rw [ptrA_eq, ptrA_eq] at h + exact okinj h + +set_option maxHeartbeats 4000000 in +theorem utJ5_sraMeetA {a b : capability.Cap} (h : sraA a b = true) (hb : physA b = true) : + baseA b = baseA a ∨ (baseA a ≤ baseA b ∧ baseA b ≤ topA a) := by + by_cases hu : isUtA a = true + · cases a <;> simp [isUtA] at hu + case Untyped p sb fi d => + rw [sraA_ut] at h + simp only [Bool.and_eq_true, decide_eq_true_eq] at h + obtain ⟨-, h2, h3, h4⟩ := h + refine Or.inr ⟨?_, ?_⟩ + · show (ptrA (capability.Cap.Untyped p sb fi d)).val ≤ (ptrA b).val + rw [ptrA_ut]; exact h2 + · show (ptrA b).val ≤ (wa (ptrA (capability.Cap.Untyped p sb fi d)) (mskA (sizeA (.Untyped p sb fi d)))).val + rw [ptrA_ut, sizeA_ut] + exact le_trans (show (ptrA b).val ≤ (wa (ptrA b) (mskA (sizeA b))).val from h4) h3 + · have hpb : objects.cap_is_physical b = ok true := by rw [physA_eq, hb] + rcases utJ5_sraNonUtA (by simpa using hu) hpb h with e | ⟨fa, fb⟩ + · refine Or.inl ?_ + unfold baseA; rw [utJ5_capPtrA e] + · cases a <;> simp [isFrA] at fa + cases b <;> simp [isFrA] at fb + rename_i pa za c1 c2 c3 c4 c5 c6 pb zb d1 d2 d3 d4 d5 d6 + obtain ⟨ia, hia⟩ : ∃ i, capability.FrameSize.bits za = ok i := by + cases za <;> exact ⟨_, rfl⟩ + obtain ⟨ib, hib⟩ : ∃ i, capability.FrameSize.bits zb = ok i := by + cases zb <;> exact ⟨_, rfl⟩ + have ea : sizeA (.Frame pa za c1 c2 c3 c4 c5 c6) = ia := sizeA_of (by simp [objects.cap_size_bits, hia]) + have p1 : ptrA (.Frame pa za c1 c2 c3 c4 c5 c6) = pa := ptrA_of rfl + have p2 : ptrA (.Frame pb zb d1 d2 d3 d4 d5 d6) = pb := ptrA_of rfl + have key : pa ≤ pb ∧ wa pa (mskA ia) ≥ wa pb (mskA ib) ∧ pb ≤ wa pb (mskA ib) := by + have e := sraA_eq (.Frame pa za c1 c2 c3 c4 c5 c6) (.Frame pb zb d1 d2 d3 d4 d5 d6) + rw [h] at e + simp only [objects.same_region_as, hia, hib, mskA_eq, bind_ok, lift] at e + split_ifs at e with g1 g2 + · refine ⟨g1, g2, ?_⟩ + simpa using okinj e + all_goals exact absurd (okinj e) (by simp) + refine Or.inr ⟨?_, ?_⟩ + · unfold baseA + rw [p1, p2] + exact key.1 + · unfold baseA topA + rw [ea, p1, p2] + exact le_trans (show pb.val ≤ (wa pb (mskA ib)).val from key.2.2) key.2.1 + +/-- An untyped that is `same_region_as` a cap spans it. -/ +theorem utJ5_sraUtSpanA {a b : capability.Cap} (hu : isUtA a = true) (h : sraA a b = true) : + baseA a ≤ baseA b ∧ topA b ≤ topA a := by + cases a <;> simp [isUtA] at hu + case Untyped p sb fi d => + rw [sraA_ut] at h + simp only [Bool.and_eq_true, decide_eq_true_eq] at h + obtain ⟨-, h2, h3, -⟩ := h + refine ⟨?_, ?_⟩ + · show (ptrA (capability.Cap.Untyped p sb fi d)).val ≤ (ptrA b).val + rw [ptrA_ut]; exact h2 + · show (wa (ptrA b) (mskA (sizeA b))).val ≤ + (wa (ptrA (capability.Cap.Untyped p sb fi d)) (mskA (sizeA (.Untyped p sb fi d)))).val + rw [ptrA_ut, sizeA_ut] + exact h3 + +/-- ... and an untyped spanning a physical, non-wrapping cap is `same_region_as` it. -/ +theorem utJ5_sraUtOfA {p sb fi : U64} {d : Bool} {c : capability.Cap} (hp : physA c = true) + (h1 : p.val ≤ baseA c) (h2 : topA c ≤ topA (.Untyped p sb fi d)) (h3 : baseA c ≤ topA c) : + sraA (.Untyped p sb fi d) c = true := by + rw [sraA_ut, hp, Bool.true_and] + simp only [Bool.and_eq_true, decide_eq_true_eq] + have e : topA (.Untyped p sb fi d) = (wa p (mskA sb)).val := by unfold topA; rw [ptrA_ut, sizeA_ut] + rw [e] at h2 + exact ⟨h1, h2, h3⟩ + +set_option maxHeartbeats 1000000 in +/-- A new cap of any type but a zombie is in its own region. -/ +theorem utJ5_selfA {n : Nat} (hn : n < 15) (base us : U64) (dev : Bool) + (hb : baseA (newCapA n base us dev) ≤ topA (newCapA n base us dev)) : + sraA (newCapA n base us dev) (newCapA n base us dev) = true := by + by_cases hu : isUtA (newCapA n base us dev) = true + · have e := (newCapA_ut hu).2 + rw [e] at hb ⊢ + refine utJ5_sraUtOfA (physA_ut _ _ _ _) ?_ (le_refl _) hb + unfold baseA; rw [ptrA_ut] + · have fr : ∀ (p : U64) (z : capability.FrameSize) (r : U64) (d x y : Bool) (m a : U64), + baseA (.Frame p z r d x y m a) ≤ topA (.Frame p z r d x y m a) → + sraA (.Frame p z r d x y m a) (.Frame p z r d x y m a) = true := by + intro p z r d x y m a h + obtain ⟨i, hi⟩ : ∃ i, capability.FrameSize.bits z = ok i := by cases z <;> exact ⟨_, rfl⟩ + have es : sizeA (.Frame p z r d x y m a) = i := sizeA_of (by simp [objects.cap_size_bits, hi]) + have ep : ptrA (.Frame p z r d x y m a) = p := ptrA_of rfl + unfold baseA topA at h + rw [es, ep] at h + apply sraA_of_eq + simp only [objects.same_region_as, hi, mskA_eq, bind_ok, lift] + have h' : p ≤ wa p (mskA i) := h + simp [h'] + revert hu hb + apply newCapA_ind (fun c => baseA c ≤ topA c → ¬ isUtA c = true → sraA c c = true) + all_goals intros + all_goals rename_i hb hu + all_goals first + | exact absurd rfl hu + | (with_reducible exact fr _ _ _ _ _ _ _ _ hb) + | (apply sraA_of_eq; simp [objects.same_region_as, objects.cap_is_physical]) + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {T Ω Sg OS : Set U64} + +/-- The old tree's facts a creation window `[A, A + w·k)` in the free part of `src` needs, and the new caps. -/ +structure UtWinA (st : machine.Store S) [L : LawfulStore st] (OS : Set U64) (par : U64 → Option U64) (s : S) + (src p sb fi : U64) (dev : Bool) (A k w : Nat) (cj : Nat → capability.Cap) : Prop where + U : UtInvA st OS par s + hP : ParIn OS par + hsrc : src ∈ OS + hcs : capA st s src = .Untyped p sb fi dev + hfi : fi.val < 2 ^ sb.val + hA : p.val + fi.val ≤ A + hAw : A + w * k ≤ p.val + 2 ^ sb.val + hk : 1 ≤ k + cphys : ∀ j, j < w → physA (cj j) = true + cbase : ∀ j, j < w → baseA (cj j) = A + j * k + ctop : ∀ j, j < w → topA (cj j) = A + j * k + k - 1 + +namespace UtWinA +variable {par : U64 → Option U64} {s : S} {src p sb fi : U64} {dev : Bool} {A k w : Nat} + {cj : Nat → capability.Cap} (h : UtWinA st OS par s src p sb fi dev A k w cj) +include h + +theorem srcR : baseA (capability.Cap.Untyped p sb fi dev) = p.val ∧ + topA (capability.Cap.Untyped p sb fi dev) = p.val + 2 ^ sb.val - 1 ∧ p.val + 2 ^ sb.val ≤ 2 ^ 64 := by + have hr := h.U.reg src h.hsrc (by rw [h.hcs]; exact physA_ut _ _ _ _) + rw [h.hcs] at hr + exact ut_rangeA (capA_ut_b h.hcs) hr + +theorem inW {j : Nat} (hj : j < w) : p.val + fi.val ≤ A + j * k ∧ A + j * k + k ≤ p.val + 2 ^ sb.val := by + have h1 : (j + 1) * k ≤ w * k := Nat.mul_le_mul_right _ hj + rw [Nat.add_mul, Nat.one_mul] at h1 + have := h.hA + have := h.hAw + omega + +theorem apart {j l : Nat} (hj : j < w) (hl : l < w) (ne : j ≠ l) : + ¬ (A + j * k ≤ A + l * k ∧ A + l * k ≤ A + j * k + k - 1) := by + rintro ⟨h1, h2⟩ + have hk := h.hk + rcases Nat.lt_or_gt_of_ne ne with g | g + · have : (j + 1) * k ≤ l * k := Nat.mul_le_mul_right _ g + rw [Nat.add_mul, Nat.one_mul] at this + omega + · have : (l + 1) * k ≤ j * k := Nat.mul_le_mul_right _ g + rw [Nat.add_mul, Nat.one_mul] at this + omega + +/-- Two new caps are in different regions. -/ +theorem newNew {j l : Nat} (hj : j < w) (hl : l < w) (ne : j ≠ l) : sraA (cj j) (cj l) = false := by + cases e : sraA (cj j) (cj l) + · rfl + have hk := h.hk + refine absurd ?_ (h.apart hj hl ne) + rcases utJ5_sraMeetA e (h.cphys l hl) with g | g + · rw [h.cbase l hl, h.cbase j hj] at g + omega + · rw [h.cbase l hl, h.cbase j hj, h.ctop j hj] at g + exact g + +/-- A new cap is not `same_region_as` the source (the window holds two). -/ +theorem newSrc {j l : Nat} (hj : j < w) (hl : l < w) (ne : j ≠ l) (i : U64) : + sraA (cj j) (.Untyped p sb i dev) = false := by + cases hu : isUtA (cj j) + · exact utJ5_sraNonUt_utA hu _ _ _ _ + cases e : sraA (cj j) (.Untyped p sb i dev) + · rfl + have s1 := utJ5_sraUtSpanA hu e + have r : baseA (capability.Cap.Untyped p sb i dev) = p.val ∧ + topA (capability.Cap.Untyped p sb i dev) = p.val + 2 ^ sb.val - 1 := by + obtain ⟨e1, e2, -⟩ := ut_rng p sb i fi dev dev + exact ⟨e1.trans h.srcR.1, e2.trans h.srcR.2.1⟩ + rw [r.1, r.2, h.cbase j hj, h.ctop j hj] at s1 + have a := h.inW hj + have b := h.inW hl + have hk := h.hk + refine absurd ?_ (h.apart hj hl ne) + omega + +/-- **An old physical cap against the window**: apart from the free part, or an untyped spanning the source. -/ +theorem old {y : U64} (hy : y ∈ OS) (hp : physA (capA st s y) = true) : + (DisjRA (capA st s y) (p.val + fi.val) (p.val + 2 ^ sb.val) ∨ + (isUtA (capA st s y) = true ∧ WithinA (capA st s src) (capA st s y))) ∧ + baseA (capA st s y) ≤ topA (capA st s y) := + ⟨h.U.noov src h.hsrc p sb fi dev h.hcs y hy hp, h.U.reg y hy hp⟩ + +/-- **An old cap that is `same_region_as` the source is, against a new cap, as against the source.** -/ +theorem oldLeft {y : U64} (hy : y ∈ OS) {j : Nat} (hj : j < w) (i : U64) + (hsub : sraA (.Untyped p sb i dev) (cj j) = true) : + sraA (capA st s y) (cj j) = sraA (capA st s y) (.Untyped p sb i dev) := by + have r : baseA (capability.Cap.Untyped p sb i dev) = p.val ∧ + topA (capability.Cap.Untyped p sb i dev) = p.val + 2 ^ sb.val - 1 := by + obtain ⟨e1, e2, -⟩ := ut_rng p sb i fi dev dev + exact ⟨e1.trans h.srcR.1, e2.trans h.srcR.2.1⟩ + have a := h.inW hj + have hk := h.hk + have hfi := h.hfi + have e0 : p ≤ p := le_refl _ + by_cases hp : physA (capA st s y) = true + swap + · have f : ∀ b, physA b = true → sraA (capA st s y) b = false := fun b hb => by + cases e : sraA (capA st s y) b + · rfl + exact absurd ((utJ5_sraPhysA e).trans hb) hp + rw [f _ (h.cphys j hj), f _ (physA_ut _ _ _ _)] + obtain ⟨c, reg⟩ := h.old hy hp + rcases c with dis | ⟨hu, wi⟩ + · have f1 : sraA (capA st s y) (cj j) = false := by + cases e : sraA (capA st s y) (cj j) + · rfl + have m := utJ5_sraMeetA e (h.cphys j hj) + rw [h.cbase j hj] at m + rcases dis with d | d <;> rcases m with m | m <;> omega + have f2 : sraA (capA st s y) (.Untyped p sb i dev) = false := by + cases hu : isUtA (capA st s y) + · exact utJ5_sraNonUt_utA hu _ _ _ _ + cases e : sraA (capA st s y) (.Untyped p sb i dev) + · rfl + have sp := utJ5_sraUtSpanA hu e + rw [r.1, r.2] at sp + rcases dis with d | d <;> omega + rw [f1, f2] + · obtain ⟨w1, w2⟩ := wi + rw [h.hcs, h.srcR.1] at w1 + rw [h.hcs, h.srcR.2.1] at w2 + obtain ⟨q, qb, qi, qd, e⟩ : ∃ q qb qi qd, capA st s y = .Untyped q qb qi qd := by + revert hu + cases capA st s y <;> simp [isUtA] + rw [e] at w1 w2 reg ⊢ + have bq : baseA (capability.Cap.Untyped q qb qi qd) = q.val := by unfold baseA; rw [ptrA_ut] + have f1 : sraA (.Untyped q qb qi qd) (cj j) = true := by + refine utJ5_sraUtOfA (h.cphys j hj) ?_ ?_ ?_ + · rw [h.cbase j hj]; omega + · rw [h.ctop j hj]; omega + · rw [h.cbase j hj, h.ctop j hj]; omega + have f2 : sraA (.Untyped q qb qi qd) (.Untyped p sb i dev) = true := by + refine utJ5_sraUtOfA (physA_ut _ _ _ _) ?_ ?_ ?_ + · rw [r.1]; omega + · rw [r.2]; omega + · rw [r.1, r.2]; omega + rw [f1, f2] + +/-- **An old cap other than the source that is no ancestor of it is in no new cap's region** (`hc_bdg`, +`hdest_after`, `hdest_ca`). -/ +theorem newOld {y : U64} (hy : y ∈ OS) (ne : y ≠ src) (hanc : ¬ Mdb.Desc par src y) {j : Nat} (hj : j < w) : + sraA (cj j) (capA st s y) = false := by + cases e : sraA (cj j) (capA st s y) + · rfl + exfalso + have hp : physA (capA st s y) = true := (utJ5_sraPhysA e).symm.trans (h.cphys j hj) + have a := h.inW hj + have hk := h.hk + obtain ⟨c, reg⟩ := h.old hy hp + have m := utJ5_sraMeetA e hp + rw [h.cbase j hj, h.ctop j hj] at m + have m' : A + j * k ≤ baseA (capA st s y) ∧ baseA (capA st s y) ≤ A + j * k + k - 1 := by + rcases m with m | m + · omega + · exact m + rcases c with dis | ⟨hu, wi⟩ + · rcases dis with d | d <;> omega + obtain ⟨w1, w2⟩ := wi + rw [h.hcs, h.srcR.1] at w1 + rw [h.hcs, h.srcR.2.1] at w2 + have fi0 : fi.val = 0 := by omega + have by0 : baseA (capA st s y) = p.val := by omega + have cu : isUtA (cj j) = true := by + cases cu : isUtA (cj j) + · obtain ⟨q, qb, qi, qd, eq⟩ : ∃ q qb qi qd, capA st s y = .Untyped q qb qi qd := by + revert hu + cases capA st s y <;> simp [isUtA] + rw [eq, utJ5_sraNonUt_utA cu] at e + exact Bool.noConfusion e + · rfl + have ndis : ¬ DisjA (capA st s y) (capA st s src) := by + unfold DisjA + rw [h.hcs, h.srcR.1, h.srcR.2.1] + omega + rcases h.U.drange src h.hsrc p sb fi dev h.hcs y hy ne hp ndis with d | d + · have i := (h.U.inc src h.hsrc p sb fi dev h.hcs y d).2.2 + omega + · exact hanc d + +end UtWinA + +/-- A cap after `src` in its chain is no ancestor of `src`. -/ +theorem utJ5_afterNotAncA {par : U64 → Option U64} {G : List (List U64)} (hOk : Mdb.MdbOK par G) + {ch A B : List U64} (hch : ch ∈ G) {src v : U64} (e : ch = A ++ src :: B) (hv : v ∈ B) : + v ≠ src ∧ ¬ Mdb.Desc par src v := by + have nd := (List.nodup_flatten.1 hOk.nodup).1 ch hch + obtain ⟨B1, B2, rfl⟩ := List.append_of_mem hv + have nd' := nd + rw [e] at nd' + have x := List.nodup_append.1 nd' + have x2 := List.nodup_cons.1 x.2.1 + refine ⟨fun q => x2.1 (by rw [← q]; simp), fun d => ?_⟩ + have e' : ch = (A ++ src :: B1) ++ v :: B2 := by rw [e]; simp + have m := Mdb.desc_after (hOk.seg ch hch) e' d + rw [e'] at nd + exact (List.nodup_append.1 nd).2.2 src (by simp) src (List.mem_cons_of_mem _ m) rfl + +variable {A : Type} {ar : machine.Arch A} + +/-- **One insert of the window loop keeps `MdbInvA`** (module docstring). -/ +theorem utJ5_stepA (hO : OSOK T Ω Sg OS) {G : List (List U64)} {par : U64 → Option U64} {s s1 s' : S} + {src p sb fi : U64} {dev : Bool} (hT : TreeA st OS G par s) (hsrc : src ∈ OS) + (hcs : capA st s src = .Untyped p sb fi dev) (hrev : Fl4ProofsA64.P1.W7.R.ST.revA st s src = true) + {base z : U64} {w i : Nat} {dslot : Nat → U64} {cj : Nat → capability.Cap} + (W : UtWinA st OS par s src p sb fi dev base.val (2 ^ z.val) w cj) + {nfi : U64} (h1c : capA st s1 src = .Untyped p sb nfi dev) + (h1k : ∀ x ∈ OS, x ≠ src → capA st s1 x = capA st s x) + (h1r : ∀ y ∈ OS, L.mem s1 (wa y 24#u64) = L.mem s (wa y 24#u64)) + (hi : i < w) (h : UtLoopA st OS G par s1 src dslot cj s' i) + (hI : MdbInvA st (utGA src dslot G i) (utPA src dslot par i) s') + (hdO : ∀ j, j < w → dslot j ∈ OS) (hdn : ∀ j, j < w → capA st s (dslot j) = .Null) + (hdc : ∀ j, j < w → obj.sext48 (dslot j) = ok (dslot j)) + (hinj : ∀ j l, j < w → l < w → dslot j = dslot l → j = l) (hsn : ∀ j, j < w → src ≠ dslot j) + (hwf : ∀ j, j < w → CapRt.EncWf (cj j)) + (hself : isZbA (cj i) = false → sraA (cj i) (cj i) = true) + (hin : sraA (.Untyped p sb nfi dev) (cj i) = true) : + cdt.insert_new_cap st s' src (dslot i) (cj i) ⦃ s'' => + MdbInvA st (utGA src dslot G (i + 1)) (utPA src dslot par (i + 1)) s'' ⦄ := by + obtain ⟨hM, hOk, hsG, h1, h2, h3, h4, -⟩ := h + have hs1n : ∀ j, j < w → capA st s1 (dslot j) = .Null := fun j hj => by + rw [h1k _ (hdO j hj) (fun e => hsn j hj e.symm)]; exact hdn j hj + have hne : ∀ j, j < i → dslot i ≠ dslot j := fun j hj e => by + have := hinj i j hi (by omega) e; omega + have hd0 : capA st s' (dslot i) = .Null := by rw [h1 _ (hdO i hi) hne]; exact hs1n i hi + -- the source at the loop state + have hsrc1 : capA st s' src = .Untyped p sb nfi dev := + (h1 src hsrc fun j hj => hsn j (by omega)).trans h1c + have hrv' : Fl4ProofsA64.P1.W7.R.ST.revA st s' src = true := by + unfold Fl4ProofsA64.P1.W7.R.ST.revA at hrev ⊢ + rw [show (281474976710656#u64 : U64) = RVB from rfl] at hrev ⊢ + rw [h4 src hsrc fun j hj => hsn j (by omega), h1r src hsrc] + exact hrev + -- a member is a new cap or an old one + have mem : ∀ a ∈ (utGA src dslot G i).flatten, (∃ j, j < i ∧ a = dslot j ∧ capA st s' a = cj j) ∨ + (a ∈ OS ∧ (∀ j, j < i → a ≠ dslot j) ∧ (a ≠ src → capA st s' a = capA st s a)) := by + intro a ha + obtain ⟨aOS, an⟩ := (hM.members a).1 ha + by_cases ex : ∃ j, j < i ∧ a = dslot j + · obtain ⟨j, hj, e⟩ := ex + exact Or.inl ⟨j, hj, e, e ▸ h2 j hj⟩ + · have nd : ∀ j, j < i → a ≠ dslot j := fun j hj e => ex ⟨j, hj, e⟩ + exact Or.inr ⟨aOS, nd, fun ns => (h1 a aOS nd).trans (h1k a aOS ns)⟩ + have hdesc : ∀ x, Mdb.Desc (utPA src dslot par i) src x ↔ Mdb.Desc par src x := fun x => by + rw [h3] + constructor + · rintro (g | ⟨j, hj, e, -⟩) + · exact g + · exact absurd e (hsn j (by omega)) + · exact Or.inl + -- no cap after the source is in the new cap's region + have after : ∀ ch ∈ utGA src dslot G i, ∀ P B : List U64, ch = P ++ src :: B → ∀ v ∈ B, + sraA (cj i) (capA st s' v) = false := by + intro ch hch P B e v hv + obtain ⟨vs, va⟩ := utJ5_afterNotAncA hOk hch e hv + have vm : v ∈ (utGA src dslot G i).flatten := + List.mem_flatten.2 ⟨ch, hch, by rw [e]; exact List.mem_append_right _ (List.mem_cons_of_mem _ hv)⟩ + rcases mem v vm with ⟨j, hj, -, c⟩ | ⟨vOS, -, c⟩ + · rw [c]; exact W.newNew hi (by omega) (by omega) + · rw [c vs] + exact W.newOld vOS vs (fun d => va ((hdesc v).2 d)) hi + have nxt : NxA st s' src ≠ 0#u64 → sraA (cj i) (capA st s' (NxA st s' src)) = false := by + intro hn + obtain ⟨ch, hch, hs⟩ := List.mem_flatten.1 hsG + obtain ⟨P, B, e⟩ := List.append_of_mem hs + cases B with + | nil => exact absurd (hM.last ch hch src (by rw [e]; simp)) hn + | cons b B => + rw [nx_split hM hch e] + exact after ch hch P (b :: B) e b List.mem_cons_self + have R := utJ5_insA hO hM hOk hI (src := src) (dest := dslot i) (nc := cj i) hsG (hdO i hi) hd0 (hdc i hi) + (hwf i hi) hrv' (by rw [hsrc1]; rfl) (fun _ hn => nxt hn) hself (by rw [hsrc1]; exact hin) + (fun a ha => by + unfold srMA + rw [hsrc1] + rcases mem a ha with ⟨j, hj, -, c⟩ | ⟨aOS, -, c⟩ + · rw [c, W.newNew (by omega) hi (by omega), W.newSrc (by omega) hi (by omega) nfi] + · by_cases asr : a = src + · rw [asr, hsrc1, hin, sraA_self hin] + · rw [c asr] + exact W.oldLeft aOS hi nfi hin) + (fun a ha d => by + rw [hdesc] at d + have aOS := (desc_in W.hP d).2 + have ut : isUtA (capA st s a) = true := W.U.anc a src d (by rw [hcs]; rfl) + have e : isUtA (capA st s' a) = true := by + rcases mem a ha with ⟨j, hj, e, -⟩ | ⟨-, -, c⟩ + · rw [e, hdn j (by omega)] at ut + exact absurd ut (by simp [isUtA]) + · by_cases asr : a = src + · rw [asr, hsrc1]; rfl + · rw [c asr]; exact ut + revert e + cases capA st s' a <;> simp [isUtA, isBadgedA]) + (fun hn => by + unfold isPFA + rw [nxt hn] + simp) + (fun s'' kp ec ch hch P B e => by + have nm : ∀ v ∈ B, srMA st s'' (dslot i) v = false := fun v hv => by + have vm : v ∈ (utGA src dslot G i).flatten := + List.mem_flatten.2 ⟨ch, hch, by rw [e]; exact List.mem_append_right _ (List.mem_cons_of_mem _ hv)⟩ + unfold srMA + rw [ec, (kp v vm).1] + exact after ch hch P B e v hv + have ca : ∀ l : List U64, (∀ v ∈ l, v ∈ B) → Mdb.CA (srFA st s'') (dslot i) l := by + intro l + induction l with + | nil => intro _; trivial + | cons v l ih => + intro sub + refine ⟨fun h0 x hx => ?_, ih fun y hy => sub y (List.mem_cons_of_mem _ hy)⟩ + unfold srFA at h0 ⊢ + rw [nm v (sub v List.mem_cons_self)] at h0 + rw [nm x (sub x (List.mem_cons_of_mem _ hx))] + exact h0 + exact ca B fun _ h => h) + rw [utPA_succ] at R + exact R + +/-- **The window loop of a creation keeps J5's MDB facts** (module docstring). -/ +theorem utRetype_j5A (hO : OSOK T Ω Sg OS) {G : List (List U64)} {par : U64 → Option U64} {s : S} + (hT : TreeA st OS G par s) (hI : MdbInvA st G par s) (hR : Fl4ProofsA64.P1.W7.R.ST.IrqRunA st G s) (a : A) {src p sb fi : U64} + {dev : Bool} (hsrc : src ∈ OS) (hcs : capA st s src = .Untyped p sb fi dev) (hrev : Fl4ProofsA64.P1.W7.R.ST.revA st s src = true) + (nt us dcn off len base : U64) (device : Bool) (hn : nt.val < 15) + (hz : (zA nt.val us).val < 64) (hfi : fi.val < 2 ^ sb.val) (hA : p.val + fi.val ≤ base.val) + (hAw : base.val + len.val * 2 ^ (zA nt.val us).val ≤ p.val + 2 ^ sb.val) + {Zq : S → Nat → Prop} + (hCOZ : ∀ (s' : S) (a' : A) (j : Nat), j < len.val → Zq s' j → + objects.create_object st ar s' a' nt (bjA base (zA nt.val us) j) us device ⦃ r => + r.1 = newCapA nt.val (bjA base (zA nt.val us) j) us device ∧ OSKeep st OS s' r.2.1 ∧ Zq r.2.1 (j + 1) ⦄) + (hZW : ∀ (s1 s2 : S) (j : Nat), Zq s1 j → WritesIn st s1 s2 (W7.CapW T Ω Sg) → Zq s2 j) + (hZ0 : ∀ s1 : S, WritesIn st s s1 (fun b => b = src ∨ b = wa src 8#u64) → Zq s1 0) + (hdO : ∀ j, j < len.val → djA dcn off j ∈ OS) + (hdn : ∀ j, j < len.val → capA st s (djA dcn off j) = .Null) + (hdc : ∀ j, j < len.val → obj.sext48 (djA dcn off j) = ok (djA dcn off j)) + (hinj : ∀ j l, j < len.val → l < len.val → djA dcn off j = djA dcn off l → j = l) + (hcan : ∀ j, j < len.val → CapRt.Canonical (bjA base (zA nt.val us) j)) (hus : us.val < 64) : + untyped.retype st ar s a src false base nt us dcn off len device ⦃ r => + MdbInvA st (utGA src (djA dcn off) G len.val) (utPA src (djA dcn off) par len.val) r.2.1 ∧ + Fl4ProofsA64.P1.W7.R.ST.IrqRunA st (utGA src (djA dcn off) G len.val) r.2.1 ∧ + (∀ y ∈ (utGA src (djA dcn off) G len.val).flatten, ∀ k, capA st r.2.1 y = .IrqHandler k → + y ∈ G.flatten ∧ capA st s y = .IrqHandler k) ⦄ := by + have hU := hT.ut + have hM := hT.lk + set z := zA nt.val us with hzdef + have hk1 : 1 ≤ 2 ^ z.val := Nat.one_le_two_pow + obtain ⟨-, -, hpfit⟩ := ut_rangeA (capA_ut_b hcs) (by + have := hU.reg src hsrc (by rw [hcs]; exact physA_ut _ _ _ _); rwa [hcs] at this) + have hsb : 2 ^ sb.val < 2 ^ 64 := Nat.pow_lt_pow_right (by decide) (capA_ut_b hcs) + have hs0 : capA st s src ≠ .Null := by rw [hcs]; exact fun h => by cases h + have hsG : src ∈ G.flatten := (hM.members src).2 ⟨hsrc, hs0⟩ + have hsn : ∀ j, j < len.val → src ≠ djA dcn off j := fun j hj e => hs0 (by rw [e]; exact hdn j hj) + have hwf : ∀ j, j < len.val → CapRt.EncWf (newCapA nt.val (bjA base z j) us device) := fun j hj => + newCapA_encWf _ _ _ _ (hcan j hj) hus + have hfit : base.val + len.val * 2 ^ z.val ≤ 2 ^ 64 := by omega + have hb : ∀ j, j < len.val → (bjA base z j).val = base.val + j * 2 ^ z.val ∧ + (bjA base z j).val + 2 ^ z.val ≤ p.val + 2 ^ sb.val := fun j hj => by + have h1 : (j + 1) * 2 ^ z.val ≤ len.val * 2 ^ z.val := Nat.mul_le_mul_right _ hj + rw [Nat.add_mul, Nat.one_mul] at h1 + have e : (bjA base z j).val = base.val + j * 2 ^ z.val := bjA_val (by omega) + exact ⟨e, by omega⟩ + have rng : ∀ j, j < len.val → baseA (newCapA nt.val (bjA base z j) us device) = base.val + j * 2 ^ z.val ∧ + topA (newCapA nt.val (bjA base z j) us device) = base.val + j * 2 ^ z.val + 2 ^ z.val - 1 := fun j hj => by + have r := newCapA_range hn (bjA base z j) us device hz (by rw [← hzdef]; have := (hb j hj).2; omega) + rw [← hzdef, (hb j hj).1] at r + exact r + have W : UtWinA st OS par s src p sb fi dev base.val (2 ^ z.val) len.val + (fun j => newCapA nt.val (bjA base z j) us device) := + ⟨hU, hT.parIn, hsrc, hcs, hfi, hA, hAw, hk1, fun j _ => newCapA_phys _ _ _ _, fun j hj => (rng j hj).1, + fun j hj => (rng j hj).2⟩ + have hdn1 : ∀ s1, RtSrc st s src p sb dev base.val (base.val + len.val * 2 ^ z.val) s1 → + ∀ j, j < len.val → capA st s1 (djA dcn off j) = .Null := fun s1 h1 j hj => by + obtain ⟨-, -, -, hc1, -, -⟩ := h1.facts hO hsrc + rw [hc1 _ (hdO j hj) (fun e => hsn j hj e.symm)]; exact hdn j hj + have noirq : ∀ j k, newCapA nt.val (bjA base z j) us device ≠ .IrqHandler k := fun j k e => by + have := newCapA_phys nt.val (bjA base z j) us device + have f : physA (.IrqHandler k) = false := physA_of rfl + rw [e, f] at this + exact Bool.noConfusion this + refine WP.spec_mono (untyped.retype.genJK ar a hcs nt us dcn off len base device hn hz hfit + (J := fun s1 s' i => UtLoopA st OS G par s1 src (djA dcn off) + (fun j => newCapA nt.val (bjA base z j) us device) s' i ∧ + MdbInvA st (utGA src (djA dcn off) G i) (utPA src (djA dcn off) par i) s' ∧ + Fl4ProofsA64.P1.W7.R.ST.IrqRunA st (utGA src (djA dcn off) G i) s' ∧ Zq s' i) + (K := fun s1 s' i => UtLoopA st OS G par s1 src (djA dcn off) + (fun j => newCapA nt.val (bjA base z j) us device) s' i ∧ + MdbInvA st (utGA src (djA dcn off) G i) (utPA src (djA dcn off) par i) s' ∧ + Fl4ProofsA64.P1.W7.R.ST.IrqRunA st (utGA src (djA dcn off) G i) s' ∧ Zq s' (i + 1)) + (fun _ _ s' a' j hj h => WP.spec_mono (hCOZ s' a' j hj h.2.2.2) fun r hr => + ⟨hr.1, h.1.keep hO (fun l hl => hdO l (by omega)) hr.2.1, + MdbInvA.keepO h.1.1 h.1.2.1 h.2.1 hr.2.1, Fl4ProofsA64.P1.W7.R.ST.IrqRunA.keepO h.1.1 h.2.2.1 hr.2.1, hr.2.2⟩) + (fun s1 h1 s' i hi h => ?_) (fun s1 h1 => ?_)) ?_ + · obtain ⟨hL, hI', hR', hZ⟩ := h + obtain ⟨nfi, -, h1c, h1k, h1l, -⟩ := h1.facts hO hsrc + have hin : sraA (.Untyped p sb nfi dev) (newCapA nt.val (bjA base z i) us device) = true := by + refine utJ5_inSrcA hn (capA_ut_b hcs) hz ?_ (hb i hi).2 hpfit + rw [(hb i hi).1]; omega + have step := utJ5_stepA hO hT hsrc hcs hrev W h1c h1k (fun y hy => (h1l y hy).2) hi hL hI' hdO hdn hdc hinj hsn + hwf (fun _ => utJ5_selfA hn _ _ _ (by rw [(rng i hi).1, (rng i hi).2]; omega)) hin + have loop := UtLoopA.step hO hi hL hdO (hdn1 s1 h1) hdc hinj hsn hwf (fun j _ => newCapA_ne _ _ _ _) + have zq := cdt.insert_new_cap.zqW hO hZW (src := src) (nc := newCapA nt.val (bjA base z i) us device) + (hdO i hi) (by exact MdbLk.nxG hO hL.1 hL.2.2.1) hZ + refine WP.spec_mono (spec_andL loop (spec_andL step zq)) ?_ + rintro s'' ⟨hL', hI'', hZ'⟩ + refine ⟨hL', hI'', ?_, hZ'⟩ + obtain ⟨hMi, -, hsGi, k1, k2, -⟩ := hL + obtain ⟨-, -, -, k1', k2', -⟩ := hL' + have dG : djA dcn off i ∉ (utGA src (djA dcn off) G i).flatten := fun hm => by + refine ((hMi.members _).1 hm).2 ?_ + rw [k1 _ (hdO i hi) fun j hj e => by have := hinj i j hi (by omega) e; omega] + exact hdn1 s1 h1 i hi + have same : ∀ y ∈ (utGA src (djA dcn off) G i).flatten, capA st s'' y = capA st s' y := fun y hy => by + obtain ⟨yOS, yn⟩ := (hMi.members y).1 hy + by_cases ex : ∃ j, j < i ∧ y = djA dcn off j + · obtain ⟨j, hj, e⟩ := ex + rw [e, k2' j (by omega), k2 j hj] + · have ne : y ≠ djA dcn off i := fun e => dG (e ▸ hy) + have nd : ∀ j, j < i → y ≠ djA dcn off j := fun j hj e => ex ⟨j, hj, e⟩ + rw [k1' y yOS fun j hj => by + rcases Nat.lt_succ_iff_lt_or_eq.1 hj with g | rfl + exacts [nd j g, ne], k1 y yOS nd] + have srcI : ∀ k, capA st s' src ≠ .IrqHandler k := fun k e => by + rw [k1 src hsrc fun j hj => hsn j (by omega), h1c] at e + cases e + refine Fl4ProofsA64.P1.W7.R.ST.IrqRunA.ins hR' hMi.nodup hsGi dG (fun y hy k => by rw [same y hy]) (fun k e => ?_) + (fun k e => absurd e (srcI k)) + rw [k2' i (by omega)] at e + exact absurd e (noirq i k) + · obtain ⟨nfi, -, h1c, h1k, h1l, -⟩ := h1.facts hO hsrc + have L0 := h1.lk hO hsrc hM hs0 + have inOS : ∀ y ∈ G.flatten, y ∈ OS := fun y hy => ((hM.members y).1 hy).1 + have alike : ∀ y ∈ G.flatten, Fl4ProofsA64.P1.W7.R.ST.shapeA (capA st s1 y) = Fl4ProofsA64.P1.W7.R.ST.shapeA (capA st s y) ∧ + Fl4ProofsA64.P1.W7.R.ST.badgeA (capA st s1 y) = Fl4ProofsA64.P1.W7.R.ST.badgeA (capA st s y) := fun y hy => by + by_cases e : y = src + · rw [e, h1c, hcs]; exact ⟨rfl, rfl⟩ + · rw [h1k y (inOS y hy) e]; exact ⟨rfl, rfl⟩ + have I1 : MdbInvA st G par s1 := MdbInvA.recap_alike hM hT.tree hI (fun y hy => (alike y hy).1) + (fun y hy => (alike y hy).2) (fun y hy => by unfold NxA; rw [(h1l y (inOS y hy)).2]) + (fun y hy => Fl4ProofsA64.P1.W7.R.ST.revA_congr (h1l y (inOS y hy)).2) (fun y hy => fbA_congr (h1l y (inOS y hy)).2) + have R1 : Fl4ProofsA64.P1.W7.R.ST.IrqRunA st G s1 := hR.keepIrq fun y hy k => by + by_cases e : y = src + · rw [e, h1c, hcs]; exact ⟨(fun h => by cases h), (fun h => by cases h)⟩ + · rw [h1k y (inOS y hy) e] + refine ⟨⟨L0, by rw [utPA_zero]; exact hT.tree, hsG, fun _ _ _ => rfl, + fun j hj => absurd hj (Nat.not_lt_zero j), fun y x => ?_, fun _ _ _ => rfl, + fun j hj => absurd hj (Nat.not_lt_zero j)⟩, by rw [utPA_zero]; exact I1, R1, + hZ0 s1 (by obtain ⟨-, -, -, -, -, hW⟩ := h1.facts hO hsrc; exact hW)⟩ + rw [utPA_zero] + exact ⟨Or.inl, fun h => h.elim id fun ⟨j, hj, _⟩ => absurd hj (Nat.not_lt_zero j)⟩ + rintro r ⟨-, s1, h1, ⟨hM', -, -, k1, k2, -⟩, hI', hR', -⟩ + refine ⟨hI', hR', fun y hy k e => ?_⟩ + obtain ⟨nfi, -, h1c, h1k, -, -⟩ := h1.facts hO hsrc + obtain ⟨yOS, -⟩ := (hM'.members y).1 hy + by_cases ex : ∃ j, j < len.val ∧ y = djA dcn off j + · obtain ⟨j, hj, ey⟩ := ex + rw [ey, k2 j hj] at e + exact absurd e (noirq j k) + have nd : ∀ j, j < len.val → y ≠ djA dcn off j := fun j hj ey => ex ⟨j, hj, ey⟩ + rw [k1 y yOS nd] at e + have ys : y ≠ src := fun q => by rw [q, h1c] at e; cases e + rw [h1k y yOS ys] at e + exact ⟨(hM.members y).2 ⟨yOS, by rw [e]; exact fun h => by cases h⟩, e⟩ + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtJ5SraA.lean b/proofs/Fl4ProofsA64/P3/UtJ5SraA.lean new file mode 100644 index 000000000..52f347bab --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtJ5SraA.lean @@ -0,0 +1,57 @@ +/- +P3 (aarch64, A64-P3 Untyped, full-record post, part A): **which old caps share a region with a new cap** +(`hsr_left`'s input). A cap that is no untyped and is `same_region_as` a physical cap points where it does, unless +both are frames. + +* **`utJ5_sraNonUt_utA`**: no cap that is no untyped is `same_region_as` an untyped. +* **`utJ5_hsrLeftA`**: `hsr_left`, from each old cap's placement against the source's region and the new object. + +No named premise. New theorems only. +-/ +import Fl4ProofsA64.P1.MdbTreeL +import Fl4ProofsA64.P1.MdbSemA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P0.Consts Fl4ProofsA64.P1 Fl4Proofs.P3 Fl4ProofsA64.P1.W7.R.ST + +set_option maxHeartbeats 4000000 in +theorem utJ5_sraNonUtA {a b : capability.Cap} (hut : isUtA a = false) (hp : objects.cap_is_physical b = ok true) + (h : sraA a b = true) : capability.Cap.ptr a = capability.Cap.ptr b ∨ (isFrA a = true ∧ isFrA b = true) := by + cases a <;> cases b + all_goals try (rw [sraA_of_eq (v := false) rfl] at h; exact Bool.noConfusion h) + all_goals try (exact Bool.noConfusion hut) + all_goals try (exact Or.inr ⟨rfl, rfl⟩) + all_goals try (simp [objects.cap_is_physical] at hp; done) + all_goals + refine Or.inl ?_ + unfold capability.Cap.ptr + by_contra ne + rw [sraA_of_eq (v := false) (by + unfold objects.same_region_as + dsimp only + split_ifs <;> first + | rfl + | (subst_vars; exact absurd rfl ne))] at h + exact Bool.noConfusion h + +set_option maxHeartbeats 1000000 in +theorem utJ5_sraNonUt_utA {a : capability.Cap} (hut : isUtA a = false) (p sb fi : U64) (d : Bool) : + sraA a (.Untyped p sb fi d) = false := by + apply sraA_of_eq + cases a <;> first + | rfl + | exact Bool.noConfusion hut + +/-- **`hsr_left`**: against the new cap `c` an old cap `a` reads as it does against the source untyped `u`. `hU`: an +old untyped's two readings agree (it holds the whole source region, or meets neither: P1 `UtInvA.noov`); `hN`: an old +cap that is no untyped does not share the new object's region. -/ +theorem utJ5_hsrLeftA {a c : capability.Cap} {p sb fi : U64} {d : Bool} + (hU : isUtA a = true → sraA a c = sraA a (.Untyped p sb fi d)) + (hN : isUtA a = false → sraA a c = false) : sraA a c = sraA a (.Untyped p sb fi d) := by + cases h : isUtA a + · rw [hN h, utJ5_sraNonUt_utA h] + · exact hU h + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtMakePoolA.lean b/proofs/Fl4ProofsA64/P3/UtMakePoolA.lean new file mode 100644 index 000000000..033573490 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtMakePoolA.lean @@ -0,0 +1,313 @@ +/- +P3 (aarch64, A64-P3 Untyped; lead 04:55Z item 2): **MakePool's perform with the dead-object drop**, a sibling of +`makePool_perform_jA` (`P3/AsidCtrlTopA`, unchanged). + +`makePool_perform_jA` needs `DeadDrop4KA` of the record it starts from. Here the record may still hold dead objects of +the childless parent untyped: they are dropped first (`utDrop_topA` with `utDropC_of`), `makePool_perform_jA` runs on +the dropped record, and its spec outcome is one of the spec perform from the undropped state, because `perform` +starts with `delete_objects`, which drops the same objects. + +Named premises: +* [J] `zb` (`UtZombieA`): a `Zombie`'s count fits its object (`P3/UtSysPlaceA`, which derives from it the `SysPlace` + over the dropped heap and slot set). +* [S] `othT`, `othC`: B, another core's current thread and current SC are not in the retyped page. +* [W] decode site: `RS` (with `SymL` [J]), `Dp` (`DecPre` re-read at the parent untyped), `P`, and `hdc` (the + destination CNode is one a CNode cap names). +-/ +import Fl4ProofsA64.P3.UtDropCA +import Fl4ProofsA64.P3.AsidCtrlTopA +import Fl4ProofsA64.P3.UtSysPlaceA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4SpecA64 Fl4Spec Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## Spec: `delete_objects` after a drop -/ + +private theorem ut_nd_congrA {σ' α β : Type} (m : NonDet σ' α) (f : α → NonDet σ' β) {s t : σ'} (h : m s = m t) : + NonDet.bind m f s = NonDet.bind m f t := by + unfold NonDet.bind + rw [h] + +/-- Dropping a region's objects first does not change `delete_objects` of that region. -/ +theorem ut_deleteObjects_dropA (ptr : Word) (bits : Nat) (σ : State Unit) : + deleteObjects (Z := Unit) ptr bits (detype {x | ptr ≤ x ∧ x ≤ ptr + 2 ^ bits - 1} σ) = + deleteObjects (Z := Unit) ptr bits σ := by + have key : ∀ ms : MachineState, + detype {x | ptr ≤ x ∧ x ≤ ptr + 2 ^ bits - 1} + { detype {x | ptr ≤ x ∧ x ≤ ptr + 2 ^ bits - 1} σ with machineState := ms } = + detype {x | ptr ≤ x ∧ x ≤ ptr + 2 ^ bits - 1} { σ with machineState := ms } := by + intro ms + have k : (detype {x | ptr ≤ x ∧ x ≤ ptr + 2 ^ bits - 1} + (detype {x | ptr ≤ x ∧ x ≤ ptr + 2 ^ bits - 1} σ)).kheap = + (detype {x | ptr ≤ x ∧ x ≤ ptr + 2 ^ bits - 1} σ).kheap := by + funext x + unfold detype + dsimp only + split_ifs <;> rfl + exact congrArg (fun h => ({ σ with kheap := h, machineState := ms } : State Unit)) k + have run : ∀ t : State Unit, deleteObjects (Z := Unit) ptr bits t = + ({r | ∃ ms ∈ (freeMemory ptr bits t.machineState).1, + r = ((), detype {x | ptr ≤ x ∧ x ≤ ptr + 2 ^ bits - 1} { t with machineState := ms.2 })}, + (freeMemory ptr bits t.machineState).2) := by + intro t + have e : deleteObjects (Z := Unit) ptr bits t = + NonDet.bind (NonDet.bind (NonDet.gets fun s : State Unit => s.machineState) fun ms => + NonDet.bind (NonDet.selectF (freeMemory ptr bits ms)) fun r => + NonDet.bind (NonDet.modify fun st : State Unit => { st with machineState := r.2 }) fun _ => NonDet.pure r.1) + (fun _ => NonDet.modify (detype {x | ptr ≤ x ∧ x ≤ ptr + 2 ^ bits - 1})) t := by + unfold deleteObjects doMachineOp + rfl + rw [e] + apply Prod.ext + · apply Set.ext + intro r + constructor + · intro h + simp only [NonDet.bind, NonDet.gets, NonDet.selectF, NonDet.modify, NonDet.pure, Set.mem_singleton_iff, + Set.mem_ofPred_eq] at h + obtain ⟨x, ⟨y, hy, z, ⟨hz, ez⟩, w, ew, hx⟩, hr⟩ := h + subst hy + subst ew + subst hx + refine ⟨z.1, hz, ?_⟩ + rw [hr, ez] + · rintro ⟨ms, hms, rfl⟩ + simp only [NonDet.bind, NonDet.gets, NonDet.selectF, NonDet.modify, NonDet.pure, Set.mem_singleton_iff, + Set.mem_ofPred_eq] + exact ⟨(ms.1, { t with machineState := ms.2 }), + ⟨_, rfl, (ms, t), ⟨hms, rfl⟩, _, rfl, rfl⟩, rfl⟩ + · apply propext + simp [NonDet.bind, NonDet.gets, NonDet.selectF, NonDet.modify, NonDet.pure] + rw [run, run] + have em : (detype {x | ptr ≤ x ∧ x ≤ ptr + 2 ^ bits - 1} σ).machineState = σ.machineState := rfl + rw [em] + simp only [key] + +/-- The spec's MakePool perform does not see a drop of its page's objects. -/ +theorem ut_makePool_dropA (frame : Word) (dest parent : CslotPtr) (base : Asid) (σ : State Unit) : + (fl4ArchPerformInvocation (Z := Unit) + (.spec (.invokeASIDControl (.makePool frame dest parent base)))).run + (detype {x | frame ≤ x ∧ x ≤ frame + 2 ^ pageBits - 1} σ) = + (fl4ArchPerformInvocation (Z := Unit) + (.spec (.invokeASIDControl (.makePool frame dest parent base)))).run σ := by + have h : performAsidControlInvocation (Z := Unit) (.makePool frame dest parent base) + (detype {x | frame ≤ x ∧ x ≤ frame + 2 ^ pageBits - 1} σ) = + performAsidControlInvocation (Z := Unit) (.makePool frame dest parent base) σ := by + unfold performAsidControlInvocation + exact ut_nd_congrA _ _ (ut_deleteObjects_dropA frame pageBits σ) + unfold fl4ArchPerformInvocation archPerformInvocation archNoReturn + exact ut_nd_congrA _ _ (ut_nd_congrA _ _ h) + +/-- A thread update commutes with a drop that keeps the thread. -/ +theorem ut_detype_updA {D : Set ObjRef} {σ : State Unit} {t : ObjRef} {x : Tcb} (ht : t ∉ D) (st' : ThreadState) : + stUpdA (detype D σ) t x st' = detype D (stUpdA σ t x st') := by + unfold stUpdA setObjA detype + refine congrArg (fun k => ({ σ with kheap := k } : State Unit)) (funext fun q => ?_) + by_cases e : q = t + · subst e + simp [ht] + · simp [Function.update_of_ne e] + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s : S} {a : A} {σ : State Unit} {m : McState} + +/-- The reachable VSpace pages grow with the pool candidates. -/ +theorem utVsPtA_monoA {P Q Tb : Set U64} {Lv : U64 → Nat} {s : S} (h : P ⊆ Q) : + utVsPtA st P Tb Lv s ⊆ utVsPtA st Q Tb Lv s := by + have hp : PoolSet st P s ⊆ PoolSet st Q s := fun _ x => ⟨h x.1, x.2⟩ + have hr : ∀ p, VsReach st (PoolSet st P s) ∅ Lv s p → VsReach st (PoolSet st Q s) ∅ Lv s p := fun p r => by + induction r with + | hr hR => exact .hr hR + | root0 hq ho h8 hn h0 => exact .root0 (hp hq) ho h8 hn h0 + | root2 hq ho h8 h2 => exact .root2 (hp hq) ho h8 h2 + | step _ hl ho h8 h3 ih => exact .step ih hl ho h8 h3 + exact Set.union_subset_union hp fun _ x => ⟨x.1, hr _ x.2⟩ + +set_option maxHeartbeats 2000000 in +/-- **MakePool's perform with the drop** (module docstring). -/ +theorem makePool_perform_dropA [Fact (n ≤ Fl4SpecA64.fl4A64Platform.numNodes)] + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) + (hT : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s)) + (hYAs : ∀ y ∈ Z.Y, ∀ j : U64, j.val < 512 → ¬ AsW (L.ksb s) (peAF y j)) + (hcO : ∀ y ∈ OS, obj.sext48 y = ok y) + {parent dest : CslotPtr} {frame fi i base : U64} + (hAp : AbsSlotA Z.th parent) (hAd : AbsSlotA Z.th dest) + (hcp : capA st s (cslotAddrA parent) = .Untyped frame 12#u64 fi false) + (hnc : cdt.ensure_no_children st s (cslotAddrA parent) = ok (core.result.Result.Ok ())) + (hcd : capA st s (cslotAddrA dest) = .Null) + (hi : i.val < 128) (hfree : L.mem s (atAF (L.ksb s) i) = 0#u64) (hbase : base.val = i.val * 512) + (hdc : ∃ y ∈ OS, ∃ q r gs g : U64, capA st s y = .CNode q r gs g ∧ dest.1 = specPtr q) + {Ud Acd Rq Nt : Set U64} {ex : Aeneas.Std.Array obj.Slot 3#usize} + (Dp : DecPre st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ud Acd s + (.Untyped frame 12#u64 fi false) ex) + (P : SysPlace st H Z.Cs H.frame Rq Nt OS s ex (cslotAddrA parent)) + (othT : ∀ c ∈ Z.Cs, c ≠ L.cpu s → + specPtr (L.mem s (L.nodeAt s c layout.node.CUR_THREAD)) ∉ utDA frame 12) + (othC : ∀ c ∈ Z.Cs, c ≠ L.cpu s → specPtr (L.mem s (L.nodeAt s c layout.node.CUR_SC)) ∉ utDA frame 12) + (hn : nA.val = fl4A64Platform.s2IpaBits) : + (do + let s1 ← sched.set_thread_state st s (L.curW s) sched.TS_RESTART + let s2 ← obj.Slot.set_cap st (cslotAddrA parent) s1 (.Untyped frame 12#u64 4096#u64 false) + let s3 ← st.clear_free s2 frame 4096#u64 + let s4 ← cdt.insert st s3 (.AsidPool frame base) (cslotAddrA parent) (cslotAddrA dest) + let s5 ← vspace.set_asid_table st s4 i frame + ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s5, a)) ⦃ res => + ∃ σ1, setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ∃ σ' rv, (.ok rv, σ') ∈ ((fl4ArchPerformInvocation (Z := Unit) + (.spec (.invokeASIDControl (.makePool (specPtr frame) dest parent (asidOfA base))))).run σ1).1 ∧ + ∃ Z' : XSetsA, + res = (.Ok (), res.2.1, a) ∧ + Z'.Pp = insert frame (dropSetA (utDA frame 12) Z.Pp) ∧ + JAtZAX (L := L) AR Z' res.2.1 a σ' (dropMA (utDA frame 12) m) ∧ + TreeA st (dropSetA (utDA frame 12) OS) Z'.G.chains Z'.G.par res.2.1 ∧ + (CarryA (dropZA (utDA frame 12) Z) (L.mem s) (detype (utDA frame 12) σ) → + CarryA Z' (L.mem res.2.1) σ') ⦄ := by + have e12 : (12#u64 : U64).val = 12 := rfl + have hcap : obj.Slot.cap st (cslotAddrA parent) s = ok (.Untyped frame 12#u64 fi false) := by rw [capA_eq, hcp] + have C := utDropC_of J Dp P hcap hnc RS.sym (by rw [e12]; exact othT) (by rw [e12]; exact othC) hn + have top := utDrop_topA J C + rw [e12] at C top + obtain ⟨J', Y'⟩ := top + have hxO : cslotAddrA parent ∈ OS := P.src + have P3 : PlaceA Z.th (L.ksb s) := J.j.1.place + have hsub : ∀ y, IsSlotA Z.th y → y ∈ OS := J.j5.2.1 + -- the parent and destination slots are kept + have xS : IsSlotA Z.th (cslotAddrA parent) := hAp.isSlot P3 + have xS' : IsSlotA (dropThA (utDA frame 12) Z.th) (cslotAddrA parent) := by + by_contra h + have := C.gone _ hxO xS h + rw [hcp] at this + cases this + have xK : specPtr (cslotAddrA parent) ∉ utDA frame 12 := C.slotOff _ xS' + obtain ⟨-, -, hfi⟩ := J.sys.sysl.sys.ur.utv J.sys.sysl.sys.stz.1.1 hxO hcp + rw [e12] at hfi + have hpar : parent.1 ∉ utDA frame 12 := by + obtain ⟨q, k, hk, h1, h2⟩ := ut_ownerA P3 (th' := dropThA (utDA frame 12) Z.th) + (fun _ _ h => (dropThA_someA.1 h).2) xS' + obtain ⟨kq, hq⟩ := dropThA_someA.1 hk + obtain ⟨q', e, hv⟩ := AbsSlotA.v P3 hAp + obtain ⟨j, kd, hkd, hj, hval⟩ : ∃ j, SlotVA Z.th (cslotAddrA parent) q' j := by + rcases hv with ⟨-, hv⟩ | ⟨-, i', -, -, hv⟩ + · exact ⟨_, hv⟩ + · exact ⟨_, hv⟩ + have eq : q' = q := by + by_contra ne + rcases P3.apart q' q kd k hkd hq ne with g | g <;> omega + rw [e, eq] + exact kq + have hdest : dest.1 ∉ utDA frame 12 := by + obtain ⟨y, hy, q, r, gs, g, hc, e⟩ := hdc + have yS : IsSlotA Z.th y := by + by_contra h + rw [J.j5.2.2 y hy h] at hc + cases hc + have yS' : IsSlotA (dropThA (utDA frame 12) Z.th) y := by + by_contra h + have := C.gone y hy yS h + rw [hc] at this + cases this + have := C.noCap y yS' + rw [show capAtA (L.mem s) y = capA st s y from rfl, hc] at this + rw [e] + exact this + have hAp' : AbsSlotA (dropThA (utDA frame 12) Z.th) parent := absSlot_dropA.2 ⟨hAp, hpar⟩ + have hAd' : AbsSlotA (dropThA (utDA frame 12) Z.th) dest := absSlot_dropA.2 ⟨hAd, hdest⟩ + have E' := J'.toJEntA + have RS' : RsSiteA (L := L) (dropZA (utDA frame 12) Z) (dropHA (utDA frame 12) H) (dropSetA (utDA frame 12) OS) Ac + (dropSetA (utDA frame 12) Pp) Tb Lv s := + { pl := by + obtain ⟨Rq', Nt', h⟩ := utSysPlace_recA J Dp P hcap hnc J.zombieBnd (D := utDA frame 12) (by rw [e12]; exact C.heapB) + (by rw [e12]; exact C.slotsB) + exact ⟨Rq', Nt', _, _, h⟩ + sym := Y' + run := RS.run + cur := RS.cur + vs := fun b hb => RS.vs b (hb.imp (fun h => ptw_mono (utVsPtA_monoA + (show dropSetA (utDA frame 12) Pp ⊆ Pp from fun _ h => h.1)) h) id) + vc := fun v hv => RS.vc v hv.1 + sub := J'.j5.2.1 } + have hT' : OffAsE (dropZA (utDA frame 12) Z).Pp (dropZA (utDA frame 12) Z).S1 (dropZA (utDA frame 12) Z).X + (dropZA (utDA frame 12) Z).Y (dropZA (utDA frame 12) Z).T2 (L.ksb s) := by + intro q j hj h + refine hT q j hj ?_ + rcases h with h | h + · rcases h with h | ⟨r, hr, e⟩ + · exact Or.inl (Or.inl h.1) + · exact Or.inl (Or.inr ⟨r, hr.1, e⟩) + · rcases h with h | h | ⟨y, hy, e⟩ + · exact Or.inr (Or.inl h.1) + · exact Or.inr (Or.inr (Or.inl h.1)) + · exact Or.inr (Or.inr (Or.inr ⟨y, hy.1, e⟩)) + have hTr' : TreeA st (dropSetA (utDA frame 12) OS) (dropZA (utDA frame 12) Z).G.chains + (dropZA (utDA frame 12) Z).G.par s := + J'.j.1.cdt.tree.grow J'.j5.2.1 fun y hy h => J'.j5.2.2 y hy h + have hD : DeadDrop4KA (dropZA (utDA frame 12) Z) (dropHA (utDA frame 12) H) frame fi := by + have hRin : RegionIn H frame.val (frame.val + 2 ^ 12) := C.regionIn + have mem : ∀ z : U64, frame.val ≤ z.val → z.val < frame.val + fi.val → specPtr z ∈ utDA frame 12 := by + intro z h1 h2 + have hpal := (J.sys.sysl.sys.ur.utv J.sys.sysl.sys.stz.1.1 hxO hcp).1 + rw [e12] at hpal + have hpfit : frame.val + 2 ^ 12 ≤ 2 ^ 64 := al_fit hpal (by omega) frame.bv.isLt + have hwf : SpNoOvA frame 12 := by + obtain ⟨ac, -, hr⟩ := ut_absRelA J.j.1.st hAp + have h := hr.1 + rw [show capAtA (L.mem s) (cslotAddrA parent) = capA st s (cslotAddrA parent) from rfl, hcp] at h + exact h + exact (utD_memA (by omega) hpfit (ut_spNoOvA (by omega) hwf) z).2 ⟨h1, by omega⟩ + refine utDrop_deadDropA mem ?_ ?_ + · rcases J.idleAnchor _ hxO frame 12#u64 fi false hcp with g | g + · exact Or.inl g + · exact Or.inr (by rw [e12] at g; omega) + · rintro q bytes (⟨e, -⟩ | ⟨e, -⟩ | ⟨kind, hk, e⟩ | ⟨k, hk, e⟩) + · omega + · omega + · rw [e] + cases kind with + | cnode r => exact Nat.two_pow_pos _ + | tcb => decide + | other z => exact J.thPos q z hk + · rw [e]; exact Nat.two_pow_pos _ + have main := makePool_perform_jA (L := L) E' RS' hT' (fun y hy j hj => hYAs y hy.1 j hj) hTr' J'.j5.2.2 + (fun y hy => hcO y hy.1) (parent := parent) (dest := dest) (frame := frame) (fi := fi) (i := i) (base := base) + hAp' hAd' hcp hnc hcd hi hfree hbase hD + refine WP.spec_mono main fun res h => ?_ + obtain ⟨σ1d, hst, σ', rv, hrun, Z', hres, hPp, hJ, hTree, hC⟩ := h + -- the spec's restart on both records + have curK : σ.curThread ∉ utDA frame 12 := C.curThread + obtain ⟨Rp0, Nt0, extra0, sp0, hP0⟩ := RS.pl + obtain ⟨xT, hkT, -, hst0, -⟩ := J.toJEntA.restartA hP0 RS.sym RS.run RS.cur RS.vs RS.vc + obtain ⟨Rp1, Nt1, extra1, sp1, hP1⟩ := RS'.pl + obtain ⟨xT', hkT', -, hst1, -⟩ := E'.restartA hP1 RS'.sym RS'.run RS'.cur RS'.vs RS'.vc + have exT : xT' = xT := by + change (if σ.curThread ∈ utDA frame 12 then none else σ.kheap σ.curThread) = some (.tcb xT') at hkT' + rw [ite_eq_right curK, hkT] at hkT' + injection hkT' with h1 + injection h1 with h2 + exact h2.symm + have eσ : σ1d = detype (utDA frame 12) (stUpdA σ σ.curThread xT .restart) := by + have h := hst.symm.trans hst1 + have h1 := (Prod.mk.inj h).1 + have h3 := congrArg Prod.snd (Set.singleton_eq_singleton_iff.1 h1) + simp only at h3 + rw [h3, exT] + exact ut_detype_updA curK _ + have eD : utDA frame 12 = {x | specPtr frame ≤ x ∧ x ≤ specPtr frame + 2 ^ pageBits - 1} := rfl + refine ⟨_, hst0, σ', rv, ?_, Z', hres, hPp, hJ, hTree, hC⟩ + rw [eσ, eD, ut_makePool_dropA] at hrun + exact hrun + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtObjA.lean b/proofs/Fl4ProofsA64/P3/UtObjA.lean new file mode 100644 index 000000000..520afa135 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtObjA.lean @@ -0,0 +1,192 @@ +/- +P3 (aarch64, A64-P3 Untyped W3/W4 leaves): **the witnesses of a retyped object**, each from FL4's words alone (zeroed +memory) against the spec's default object, with no dependency on the joint record or the ghost heap. + +* **`create_object_pureA`**: for every type but the TCB, the VCPU and the HighVSpace, `objects::create_object` writes + nothing and returns `newCapA`. +* **`utNewCap_relA`**: FL4's new cap represents l4v's `default_cap`, for all fifteen types (`UtDefault` below). +* **`ut_epRelA`**, **`ut_ntfnRelA`**, **`ut_replyRelA`**, **`ut_cnodeSlotsA`**: zeroed endpoint, notification, reply + and CNode words against the default objects. +* **`ut_ptEntriesA`**, **`ut_s2EntriesA`**: zeroed table words against all-invalid Stage-1 / Stage-2 entries. + +No named premise. Hypotheses: the words are zero; an untyped's or a frame's range does not wrap under `specPtr` +(`SpNoOvA`, from placement). New definitions and lemmas only. +-/ +import Fl4ProofsA64.P3.UtSpecA +import Fl4ProofsA64.P3.NtfnRelA +import Fl4ProofsA64.P3.EpRelA +import Fl4ProofsA64.P3.ExtVsDefA +import Fl4ProofsA64.P1.MdbTreeL +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4Spec Fl4SpecA64 + Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## Caps -/ + +/-- The types whose new cap's range must not wrap under `specPtr`: untyped and the three frames. -/ +def utRangedA (n : Nat) : Prop := n = 0 ∨ n = 7 ∨ n = 9 ∨ n = 10 + +/-- **FL4's new cap represents l4v's `default_cap`**, for every type the decode lets through. -/ +theorem utNewCap_relA {n : Nat} {t : Fl4SpecA64.ApiObjectType} (ht : objTypeOfA n = some t) (base us : U64) + (dev : Bool) (hus : us.val < 64) (hcn : n = 4 → us.val ≤ 47) (hsc : n = 5 → 7 ≤ us.val) + (hno : utRangedA n → SpNoOvA base (zOfA n us.val)) : + CapRelA (newCapA n base us dev) (Fl4SpecA64.defaultCap t (specPtr base) us.val dev) := by + have hn : n < 15 := (objTypeOfA_some ht us.val).1 + have hb : bitsOfA (0#u64 : U64).val (0#u64 : U64).val = [] := bitsOfA_eq_nil.2 rfl + have huniv : rightsOfA true true true true = Set.univ := by + ext x; cases x <;> simp [rightsOfA] + have hrw : rightsOfA true true false false = {.allowRead, .allowWrite} := by + ext x; cases x <;> simp [rightsOfA] + have hvm : vmRightsOfA 3#u64 = Fl4SpecA64.vmReadWrite := by simp [vmRightsOfA] + unfold objTypeOfA archDataToObjType at ht + interval_cases n <;> simp at ht <;> subst ht + · refine ⟨hno (Or.inl rfl), CapEqA.of_abs ?_⟩; rfl + · exact ⟨trivial, CapEqA.of_abs rfl⟩ + · refine ⟨trivial, CapEqA.of_abs ?_⟩ + show Fl4SpecA64.Cap.endpointCap (specPtr base) (0#u64 : U64).bv (rightsOfA true true true true) = _ + rw [huniv]; rfl + · refine ⟨trivial, CapEqA.of_abs ?_⟩ + show Fl4SpecA64.Cap.notificationCap (specPtr base) (0#u64 : U64).bv (rightsOfA true true false false) = _ + rw [hrw]; rfl + · have h47 := hcn rfl + refine ⟨?_, CapEqA.of_abs ?_⟩ + · show us.val < 64 ∧ (0#u64 : U64).val < 64 ∧ (0#u64 : U64).val < 2 ^ (0#u64 : U64).val + exact ⟨hus, by decide, by decide⟩ + · show Fl4SpecA64.Cap.cnodeCap (specPtr base) us.val (bitsOfA (0#u64 : U64).val (0#u64 : U64).val) = _ + rw [hb]; rfl + · refine ⟨?_, CapEqA.of_abs rfl⟩ + show Fl4SpecA64.minSchedContextBits ≤ us.val + exact hsc rfl + · refine ⟨trivial, CapEqA.of_abs ?_⟩ + show Fl4SpecA64.Cap.replyCap (specPtr base) (replyRightsA true) = _ + rfl + · refine ⟨?_, CapEqA.of_abs ?_⟩ + · exact ⟨by decide, by decide, ⟨by decide, fun _ => rfl, (fun h => by cases h), (fun h => by cases h)⟩, + hno (Or.inr (Or.inl rfl))⟩ + · show Fl4SpecA64.Cap.archObjectCap (absFrameE false (specPtr base) (vmRightsOfA 3#u64) _ dev _) = _ + rw [hvm]; rfl + · refine ⟨?_, CapEqA.of_abs rfl⟩ + show (0#u64 : U64).val < 2 ^ 16 + decide + · refine ⟨?_, CapEqA.of_abs ?_⟩ + · exact ⟨by decide, by decide, ⟨by decide, fun _ => rfl, (fun h => by cases h), (fun h => by cases h)⟩, + hno (Or.inr (Or.inr (Or.inl rfl)))⟩ + · show Fl4SpecA64.Cap.archObjectCap (absFrameE false (specPtr base) (vmRightsOfA 3#u64) _ dev _) = _ + rw [hvm]; rfl + · refine ⟨?_, CapEqA.of_abs ?_⟩ + · exact ⟨by decide, by decide, ⟨by decide, fun _ => rfl, (fun h => by cases h), (fun h => by cases h)⟩, + hno (Or.inr (Or.inr (Or.inr rfl)))⟩ + · show Fl4SpecA64.Cap.archObjectCap (absFrameE false (specPtr base) (vmRightsOfA 3#u64) _ dev _) = _ + rw [hvm]; rfl + · refine ⟨?_, CapEqA.of_abs rfl⟩ + show (0#u64 : U64).val < 2 ^ 16 + decide + · exact ⟨trivial, CapEqA.of_abs rfl⟩ + · refine ⟨?_, CapEqA.of_abs rfl⟩ + show (0#u64 : U64).val = 0 ∧ (0#u64 : U64).val < 2 ^ 16 + exact ⟨rfl, by decide⟩ + · refine ⟨?_, CapEqA.of_abs rfl⟩ + show (0#u64 : U64).val < 2 ^ 16 + decide + +section +variable {S : Type} {A : Type} + +/-- **`create_object` writes nothing** for every type but the TCB (1), the VCPU (12) and the HighVSpace (14). -/ +theorem create_object_pureA (st : machine.Store S) (ar : machine.Arch A) (s : S) (a : A) (nt base us : U64) + (dev : Bool) (hn : nt.val < 15) (h1 : nt.val ≠ 1) (h12 : nt.val ≠ 12) (h14 : nt.val ≠ 14) : + objects.create_object st ar s a nt base us dev = ok (newCapA nt.val base us dev, s, a) := by + have e : ∀ c : U64, nt = c ↔ nt.val = c.val := fun c => ⟨congrArg _, UScalar.eq_of_val_eq⟩ + unfold objects.create_object objects.create_arch_object newCapA + simp only [e, fl4_abi.consts.object.Tcb, fl4_abi.consts.object.Endpoint, fl4_abi.consts.object.Notification, + fl4_abi.consts.object.CapTable, fl4_abi.consts.object.Untyped, fl4_abi.consts.object.SchedContext, + fl4_abi.consts.object.Reply, fl4_abi.consts.object.ArmPageTable, fl4_abi.consts.object.ArmVSpace, + fl4_abi.consts.object.ArmVcpu, fl4_abi.consts.object.ArmStage2VSpace, fl4_abi.consts.object.ArmHighVSpace, + fl4_abi.consts.object.ArmSmallPage, fl4_abi.consts.object.ArmLargePage] + generalize nt.val = n at hn h1 h12 h14 ⊢ + interval_cases n <;> first | omega | rfl | (simp [capability.VM_READ_WRITE]) + +end + +/-! ## Objects from zeroed words -/ + +/-- A zeroed endpoint is the spec's default (idle) endpoint. -/ +theorem ut_epRelA (m : U64 → U64) (T : Set U64) (e : U64) (h0 : m (epHeadA e) = 0#u64) (h8 : m (epTSA e) = 0#u64) : + EpObjRel m T e Fl4SpecA64.defaultEp := by + have hq : qOf m e = ⟨0#u64, 0#u64⟩ := by + unfold qOf; rw [h0, h8] + congr 1 + have hs : stOf m e = 0#u64 := by unfold stOf; rw [h8]; rfl + show stOf m e = 0#u64 ∧ QueueRel m T .Ep (qOf m e) [] + exact ⟨hs, hq ▸ QueueRel.nil⟩ + +/-- A zeroed notification is the spec's default notification. -/ +theorem ut_ntfnRelA (m : U64 → U64) (T : Set U64) (n : U64) (h0 : m (epHeadA n) = 0#u64) + (h8 : m (epTSA n) = 0#u64) (hb : m (nBoundA n) = 0#u64) (hsc : m (nScA n) = 0#u64) : + NtfnRel1 m T n Fl4SpecA64.defaultNotification := by + have hq : qOf m n = ⟨0#u64, 0#u64⟩ := by + unfold qOf; rw [h0, h8] + congr 1 + have hs : stOf m n = 0#u64 := by unfold stOf; rw [h8]; rfl + refine ⟨?_, ?_, fun h => absurd hb h, ?_⟩ + · show stOf m n = 0#u64 ∧ QueueRel m T .Ep (qOf m n) [] + exact ⟨hs, hq ▸ QueueRel.nil⟩ + · rw [optRef, ite_eq_left hb]; rfl + · rw [optRef, ite_eq_left hsc]; rfl + +/-- A zeroed reply is the spec's default reply: no TCB, on no SC. -/ +theorem ut_replyRelA (m : U64 → U64) (r : U64) (h : m (Endp.rTcbA r) = 0#u64) : + Fl4SpecA64.defaultReply.replyTcb = optRef (m (Endp.rTcbA r)) ∧ Fl4SpecA64.defaultReply.replySc = none := by + rw [optRef, ite_eq_left h]; exact ⟨rfl, rfl⟩ + +/-- A slot of two zero words holds the null cap. -/ +theorem ut_capAt_zeroA (m : U64 → U64) (a : U64) (h0 : m a = 0#u64) (h8 : m (wa a 8#u64) = 0#u64) : + capAtA m a = .Null := by + unfold capAtA; rw [h0, h8]; rfl + +/-- **A zeroed CNode of radix `r`** holds the spec's empty CNode, well formed. -/ +theorem ut_cnodeSlotsA (m : U64 → U64) (base : U64) (r : Nat) + (hz : ∀ idx : List Bool, idx.length = r → + m (slotAddrA (specPtr base) (Fl4SpecA64.ofBl idx)) = 0#u64 ∧ + m (wa (slotAddrA (specPtr base) (Fl4SpecA64.ofBl idx)) 8#u64) = 0#u64) : + Fl4SpecA64.wellFormedCnodeN r (Fl4SpecA64.emptyCnode r) ∧ + ∀ idx : List Bool, idx.length = r → + SlotRelA m (slotAddrA (specPtr base) (Fl4SpecA64.ofBl idx)) (Fl4SpecA64.emptyCnode r idx) := by + constructor + · unfold Fl4SpecA64.wellFormedCnodeN + ext x + show (Fl4SpecA64.emptyCnode r x ≠ none) ↔ x.length = r + unfold Fl4SpecA64.emptyCnode + split <;> simp [*] + · intro idx h + refine ⟨.nullCap, by unfold Fl4SpecA64.emptyCnode; rw [ite_eq_left h], ?_⟩ + rw [ut_capAt_zeroA m _ (hz idx h).1 (hz idx h).2] + exact CapRelA.null + +/-! ## Tables -/ + +/-- **A zeroed Stage-1 table** matches the all-invalid entries of a new `PageTable` or `VSpace`, and of a new +`ArmHighVSpace`'s level-1 table. -/ +theorem ut_ptEntriesA (m : U64 → U64) (Tb : Set U64) (Lv : U64 → Nat) (k : Nat) (base : U64) + (hz : ∀ i : U64, i.val < 512 → m (peAF base i) = 0#u64) : + (∀ i : U64, i.val < 512 → PteRelA Tb Lv k (m (peAF base i)) + (Fl4SpecA64.ptApply (.normalPT fun _ => .invalidPTE) i.bv)) ∧ + (∀ i : U64, i.val < 512 → PteRelA Tb Lv k (m (peAF base i)) + (Fl4SpecA64.ptApply (.vsRootPT fun _ => .invalidPTE) i.bv)) := by + constructor <;> intro i hi <;> rw [hz i hi] <;> exact pteRelA_zero_inv Tb Lv k + +/-- **A zeroed Stage-2 root** matches the all-invalid entries of a new Stage-2 VSpace. -/ +theorem ut_s2EntriesA (m : U64 → U64) (G : U64 → Nat → Prop) (sh : Nat) (base : U64) + (hz : ∀ i : U64, i.val < 1024 → m (peAF base i) = 0#u64) : + ∀ i : U64, i.val < 1024 → DescRelE G sh (m (peAF base i)) .invalidPTE ∧ S2AttrE sh (m (peAF base i)) .invalidPTE := + fun i hi => by rw [hz i hi]; exact ⟨descRelE_zero G sh, trivial⟩ + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtPpHookA.lean b/proofs/Fl4ProofsA64/P3/UtPpHookA.lean new file mode 100644 index 000000000..7b3a156de --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtPpHookA.lean @@ -0,0 +1,173 @@ +/- +P3 (aarch64, A64-P3 Untyped, J8): **the preemption point's loop hook for the reset**, from +`preemption_point_refinesA` and the four `UtRstInvA` steps. + +* `UtPpTimeA`: what the invariant carries beside `UtRstInvA` for the preemption point: the timing facts, the clock + relation and an active current SC. +* `UtPpTimeA.cnStep`: it survives a chunk step (`utReset_loopA`'s `hT` for `T := UtPpTimeA Clock`). +* **`utPp_hookA`**: `PPHypUtA` for `R := UtRstInvA ∧ UtPpTimeA`. + +Named premises (each over every state of `R`): +* [J] `hvp`: no VSpace word of `Pp ∪ Tb`, and no ASID-table word, is a preemption-point word. +* [P] `hC` (executor 49, ruling 13:24Z: the raw counter oracle is monotone; `CounterStepA` itself is carried in + `UtPpTimeA.cs`, it was an over-quantified `∀ σ, ValidMachineTimeA σ → CounterStepA σ`), `hNow` (the raw counter read is `getCurrentTime`), `hAr` (the arch relation after the + timestamp), `hirq` (`irq_pending` is an outcome of `getActiveIRQ true`; it keeps the spec memory, the last machine + time, the clock relation and the arch relation), `hmb` (the minimum budget), `P` (`NowAgreeA`), `hrc` (the clock + relation across the interrupt poll's `getActiveIRQ`, inside `hirq`), `hnd` (one domain). + +New definitions and theorems only. +-/ +import Fl4ProofsA64.P3.UtPpWuA +import Fl4ProofsA64.P3.UtPpScA +import Fl4ProofsA64.P3.UtPpTsA +import Fl4ProofsA64.P3.Stage3PreemptA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +set_option maxRecDepth 100000 + +/-- **What the preemption point needs beside the reset's invariant** (module docstring). -/ +structure UtPpTimeA {A : Type} (Clock : A → MachineState → Prop) (a : A) (σ : State Unit) : Prop where + valid : ValidMachineTimeA σ + cur : CurInvA σ + off : OffReadyA σ + act : ActIdleA σ + clk : Clock a σ.machineState + scAct : ∀ y k, σ.kheap σ.curSc = some (.schedContext y k) → scActive y = true + /-- (executor 49) The raw counter's next sample is not below the last one (`CounterStepA`). -/ + cs : CounterStepA σ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {m : McState} {Rq Nt : Set U64} {ex : Aeneas.Std.Array obj.Slot 3#usize} {p sb x i : U64} + +/-- `UtPpTimeA` survives a CNode write that keeps the machine state (`utReset_loopA`'s `hT`). -/ +theorem UtPpTimeA.cnStep {B : Type} {Clock : B → MachineState → Prop} {a : B} {σ σ' : State Unit} + (T : UtPpTimeA Clock a σ) (H : UtCnStepA σ σ') (hms : σ'.machineState = σ.machineState) : + UtPpTimeA Clock a σ' := by + have kT : ∀ p y, σ.kheap p = some (.tcb y) → σ'.kheap p = some (.tcb y) := fun p y h => + H.keep h (fun _ _ e => by cases e) + have bS : ∀ p y k, σ'.kheap p = some (.schedContext y k) → σ.kheap p = some (.schedContext y k) := + fun p y k h => H.back h (fun _ _ e => by cases e) + have et : tcbsOf σ' = tcbsOf σ := by + funext p + show Fl4Spec.optMap σ'.kheap tcbOf p = Fl4Spec.optMap σ.kheap tcbOf p + unfold Fl4Spec.optMap + rcases H.kh p with h | ⟨r, cs, cs', h1, h2⟩ + · rw [h] + · rw [h1, h2]; rfl + have es : scsOf σ' = scsOf σ := by + funext p + show Fl4Spec.optMap σ'.kheap scOf p = Fl4Spec.optMap σ.kheap scOf p + unfold Fl4Spec.optMap + rcases H.kh p with h | ⟨r, cs, cs', h1, h2⟩ + · rw [h] + · rw [h1, h2]; rfl + have sch : ∀ t, schedulable t σ' = schedulable t σ := fun t => by + unfold schedulable + rw [et, es, H.rls] + refine + { valid := ?_ + cur := ?_ + off := fun y k h => ?_ + act := ?_ + clk := by rw [hms]; exact T.clk + scAct := fun y k h => ?_ + cs := by unfold CounterStepA; rw [hms]; exact T.cs } + · show σ'.curTime ≤ σ'.machineState.lastMachineTime + rw [H.time, hms] + exact T.valid + · obtain ⟨y, h1, h2, h3⟩ := T.cur.tcb + obtain ⟨c, k, hc⟩ := T.cur.sc + refine ⟨⟨y, by rw [H.cur]; exact kT _ _ h1, by rw [H.csc]; exact h2, h3⟩, + ⟨c, k, by rw [H.csc]; exact H.keep hc (fun _ _ e => by cases e)⟩, fun ne c' k' h => ?_, + by rw [H.cur, H.rls]; exact T.cur.notRel⟩ + rw [H.csc] at ne h + exact T.cur.active ne c' k' (bS _ _ _ h) + · rw [H.cons, H.time] + rw [H.csc] at h + exact T.off y k (bS _ _ _ h) + · obtain ⟨h1, y, h2, h3, h4, h5⟩ := T.act + refine ⟨fun t ht => ?_, y, by rw [H.idle]; exact kT _ _ h2, h3, h4, by rw [H.idle, H.rls]; exact h5⟩ + rw [H.act] at ht + rw [sch] + exact h1 t ht + · rw [H.csc] at h + exact T.scAct y k (bS _ _ _ h) + +/-- **`PPHypUtA` for the reset loop** (module docstring). -/ +theorem utPp_hookA (ar : machine.Arch A) (hnd : numDomains = 1) {Clock : A → MachineState → Prop} + (P : NowAgreeA ar Clock) + (hvp : ∀ (s : S) (a : A) (σ : State Unit), + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m → + ∀ b, VsR (L.ksb s) (Pp ∪ Tb) b → Aligned b ∧ ¬ PPW st s b) + (hC : ∀ i, fl4RawCounterOracle i ≤ fl4RawCounterOracle (i + 1)) + (hNow : ∀ σ : State Unit, fl4ReadRawCounter σ.machineState = getCurrentTime σ.machineState) + (hAr : ∀ (s : S) (a : A), AR a Z.hw → sched.update_timestamp st ar s a ⦃ r => AR r.2 Z.hw ⦄) + (hirq : ∀ (a : A) (σ : State Unit), AR a Z.hw → Clock a σ.machineState → + ar.irq_pending a ⦃ r => ∃ o ms, (o, { σ with machineState := ms }) ∈ + (doMachineOp (getActiveIRQ true) σ).1 ∧ (r.1 = true ↔ ¬ o = none) ∧ + ms.underlyingMemory = σ.machineState.underlyingMemory ∧ + ms.lastMachineTime = σ.machineState.lastMachineTime ∧ ms.timeState = σ.machineState.timeState ∧ + Clock r.2 ms ∧ AR r.2 Z.hw ⦄) + (hmb : ∀ a : A, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) : + PPHypUtA st ar OS + (fun s a σ => UtRstInvA (ar := ar) st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n nA m p sb x Rq Nt ex s a σ i ∧ + UtPpTimeA Clock a σ) + (fun s a σ => UtRstInvA (ar := ar) st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n nA m p sb x Rq Nt ex s a σ i ∧ + UtPpTimeA Clock a σ) := by + refine preemption_point_refinesA OS _ hnd ?_ ?_ ?_ ?_ ?_ + · rintro s a σ v s1 ⟨I, T⟩ h + have hv : ∀ b, VsR (L.ksb s) (Pp ∪ Tb) b → Aligned b ∧ b ≠ wa (L.ksb s) layout.ks.WORK_UNITS := fun b hb => + ⟨(hvp s a σ I.rec_ b hb).1, fun e => (hvp s a σ I.rec_ b hb).2 (Or.inl e)⟩ + obtain ⟨I1, k⟩ := I.workUnitsA hv h + exact ⟨⟨I1, T⟩, k⟩ + · rintro s a σ ⟨I, T⟩ + have har : AR a Z.hw := I.rec_.j.1.ar + refine WP.spec_mono (I.timestampA (hvp s a σ I.rec_) P T.clk hnd T.valid T.cs T.cur T.off T.act (hNow σ) + (hAr s a har)) ?_ + rintro r ⟨σ1, h1, I1, k, v, c, o, ac, cl⟩ + obtain ⟨now, -, e⟩ : ∃ now : Ticks, True ∧ + σ1.kheap = σ.kheap ∧ σ1.curSc = σ.curSc ∧ σ1.machineState = rawStateA σ.machineState := by + have h2 := h1 + rw [← fl4UpdateTimestamp_eq_l4vA σ hnd (hNow σ), fl4UpdateTimestamp_eqA σ] at h2 + have e2 := (Prod.mk.inj (Set.mem_singleton_iff.1 h2)).2 + exact ⟨0, trivial, by rw [e2]; rfl, by rw [e2]; rfl, by rw [e2]; rfl⟩ + refine ⟨σ1, h1, ⟨I1, v, c, o, ac, cl, fun y k hk => ?_, ?_⟩, k⟩ + · rw [e.1, e.2.1] at hk + exact T.scAct y k hk + · unfold CounterStepA rawNextA + rw [e.2.2] + exact hC _ + · rintro s a σ ⟨I, T⟩ + have har : AR a Z.hw := I.rec_.j.1.ar + refine WP.spec_mono (hirq a σ har T.clk) ?_ + rintro r ⟨o, ms, h1, h2, h3, h4, h4t, h5, h6⟩ + refine ⟨o, _, h1, h2, I.machineA h3 h6, ?_⟩ + refine ⟨?_, ⟨T.cur.tcb, T.cur.sc, T.cur.active, T.cur.notRel⟩, T.off, T.act, h5, T.scAct, ?_⟩ + · show σ.curTime ≤ ms.lastMachineTime + rw [h4] + exact T.valid + · show ms.lastMachineTime ≤ fl4RawCounterOracle (ms.timeState + 1) + rw [h4, h4t] + exact T.cs + · rintro s a σ ⟨I, -⟩ + exact I.scActiveA + · rintro s a σ ⟨I, T⟩ + exact I.sufficientA (hmb a) T.scAct + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtPpScA.lean b/proofs/Fl4ProofsA64/P3/UtPpScA.lean new file mode 100644 index 000000000..fdd7e255d --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtPpScA.lean @@ -0,0 +1,151 @@ +/- +P3 (aarch64, A64-P3 Untyped, J8 `hIrq` / `hAct` / `hSuf`): **the preemption point's reads and interrupt poll under +the reset's invariant**. + +* `UtRstInvA.machineA`: the invariant across a change of the spec machine state that keeps the memory (and of the + arch state, `har`). +* **`UtRstInvA.irqA`**: `preemption_point_refinesA`'s `hIrq`. +* **`UtRstInvA.scActiveA`**: its `hAct`. +* **`UtRstInvA.sufficientA`**: its `hSuf`, **for an active current SC only** (`hact`). + +Named premises: +* [P] `hirq` (`irqA`): FL4's `irq_pending` is an outcome of the spec's `getActiveIRQ true`, keeps the spec memory and + the arch relation. +* [P] `hmb` (`sufficientA`): FL4's minimum budget is the spec's. +* [J] `hact` (`sufficientA`): the current SC is active. The producer asks `hSuf` at every state; it uses it only after + `sc_active` returned true. + +New theorems only. +-/ +import Fl4ProofsA64.P3.Stage3Rec5A +import Fl4ProofsA64.P3.UtResetA +import Fl4ProofsA64.P3.ScRefillA +import Fl4ProofsA64.P3.ScSpecA +import Fl4ProofsA64.P3.Stage3RestartA +import Fl4ProofsA64.P3.Stage3PreemptTA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +set_option maxRecDepth 100000 + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s : S} {a a2 : A} {σ : State Unit} {m : McState} {Rq Nt : Set U64} + {ex : Aeneas.Std.Array obj.Slot 3#usize} {p sb x i : U64} + +set_option maxHeartbeats 1000000 in +/-- **The invariant across a machine-state change that keeps the memory** (module docstring). -/ +theorem UtRstInvA.machineA + (I : UtRstInvA (ar := ar) st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n nA m p sb x Rq Nt ex s a σ i) + {ms : MachineState} (hm : ms.underlyingMemory = σ.machineState.underlyingMemory) (har : AR a2 Z.hw) : + UtRstInvA (ar := ar) st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n nA m p sb x Rq Nt ex s a2 + { σ with machineState := ms } i := by + have J := I.rec_ + have J2 := ((J.toTA I.sact I.noSwitch).machineA hm har).toJA J.sys.sysl J.vcSym1 J.vcSym2 J.heapWin + J.coresAll J.ptCapIn J.symL J.fhOK J.zwf ⟨J.schedValid.rq, J.schedValid.act, J.schedValid.blk, J.schedValid.rls⟩ + J.utWin J.devWin J.vcBack J.runBack ⟨J.vcSnap.snap, J.vcSnap.mask⟩ J.vcOS J.userFr J.frTh J.bfSpec + obtain ⟨σ2, e2⟩ : ∃ σ2 : State Unit, σ2 = { σ with machineState := ms } := ⟨_, rfl⟩ + rw [← e2] at J2 ⊢ + have kh : σ2.kheap = σ.kheap := by rw [e2] + have um : σ2.machineState.underlyingMemory = σ.machineState.underlyingMemory := by rw [e2]; exact hm + exact + { rec_ := J2 + sact := by + rw [e2] + exact ⟨I.sact.f1, I.sact.f2⟩ + noSwitch := by + rw [e2] + exact I.noSwitch + symL := I.symL + clr := I.clr + place := I.place + run := I.run + cap := I.cap + noch := I.noch + heap := by rw [kh]; exact I.heap + link := by rw [e2]; exact I.link + orig := by rw [e2]; exact I.orig + zero := by rw [um]; exact I.zero } + +/-- **`preemption_point_refinesA`'s `hIrq`** (module docstring). -/ +theorem UtRstInvA.irqA {ar : machine.Arch A} + (I : UtRstInvA (ar := ar) st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n nA m p sb x Rq Nt ex s a σ i) + (hirq : ar.irq_pending a ⦃ r => ∃ o ms, (o, { σ with machineState := ms }) ∈ + (doMachineOp (getActiveIRQ true) σ).1 ∧ (r.1 = true ↔ ¬ o = none) ∧ + ms.underlyingMemory = σ.machineState.underlyingMemory ∧ AR r.2 Z.hw ⦄) : + ar.irq_pending a ⦃ r => ∃ o σ2, (o, σ2) ∈ (doMachineOp (getActiveIRQ true) σ).1 ∧ (r.1 = true ↔ ¬ o = none) ∧ + UtRstInvA (ar := ar) st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n nA m p sb x Rq Nt ex s r.2 σ2 i ⦄ := by + refine WP.spec_mono hirq ?_ + rintro r ⟨o, ms, h1, h2, h3, h4⟩ + exact ⟨o, _, h1, h2, I.machineA h3 h4⟩ + +/-- The spec's current SC is the relation's name of FL4's, a related SC. -/ +theorem UtRstInvA.curScA + (I : UtRstInvA (ar := ar) st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n nA m p sb x Rq Nt ex s a σ i) : + L.cscW s ∈ Z.C ∧ σ.curSc = scNameA m (L.cscW s) := by + have J := I.rec_ + refine ⟨J.sr.cscC, ?_⟩ + have h := J.j.1.sch + rw [← h.csc, h.core, h.curScOfW _ h.cpuCs] + rfl + +/-- **`preemption_point_refinesA`'s `hAct`**. -/ +theorem UtRstInvA.scActiveA + (I : UtRstInvA (ar := ar) st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n nA m p sb x Rq Nt ex s a σ i) : + sporadic.sc_active st s (L.cscW s) ⦃ b => readScActive (Z := Unit) σ.curSc σ = some b ⦄ := by + obtain ⟨hc, ec⟩ := I.curScA + have scr := I.rec_.sr.scr + obtain ⟨y, k, hk, -⟩ := scr.sc _ hc + refine WP.spec_mono (sc_active_refinesA scr hc hk) fun b hb => ?_ + rw [getScActive_eqA hk] at hb + have eb : b = scActive y := by + have := congrArg Prod.fst hb + exact (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 this)).1.symm + rw [ec, eb] + show Option.bind (readSchedContext (scNameA m (L.cscW s)) σ) (fun sc => Fl4Spec.Lookup.oreturn (scActive sc) σ) = _ + rw [readSchedContext_eqA hk]; rfl + +/-- **`preemption_point_refinesA`'s `hSuf`, for an active current SC** (module docstring). -/ +theorem UtRstInvA.sufficientA {ar : machine.Arch A} + (I : UtRstInvA (ar := ar) st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n nA m p sb x Rq Nt ex s a σ i) + (hmb : sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hact : ∀ y k, σ.kheap σ.curSc = some (.schedContext y k) → scActive y = true) : + sporadic.sufficient st ar s a (L.cscW s) (L.mem s (L.nodeA s layout.node.CONSUMED)) ⦃ ok1 => + (ok1 = true ↔ readScRefillSufficient (Z := Unit) σ.curSc σ.consumedTime σ = some true) ⦄ := by + obtain ⟨hc, ec⟩ := I.curScA + have scr := I.rec_.sr.scr + obtain ⟨y, k, hk, hr⟩ := scr.sc _ hc + have ha : scActive y = true := hact y k (by rw [ec]; exact hk) + have hne : y.scRefills ≠ [] := by + have R' := hr.strong ha + rw [R'.refills] + exact refills_neA R'.wf + have cons : σ.consumedTime = (L.mem s (L.nodeA s layout.node.CONSUMED)).bv := I.rec_.j.1.rest.consumed + refine WP.spec_mono (sufficient_refinesA a scr hc hk ha _ hmb) fun b hb => ?_ + rw [getScRefillSufficient_eqA hk hne] at hb + have eb : b = refillSufficient (L.mem s (L.nodeA s layout.node.CONSUMED)).bv (refillHd y) := by + have := congrArg Prod.fst hb + exact (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 this)).1.symm + have rd : readScRefillSufficient (Z := Unit) σ.curSc σ.consumedTime σ = some b := by + rw [ec, cons, eb] + show Option.bind (readRefillHead (scNameA m (L.cscW s)) σ) + (fun h => Fl4Spec.Lookup.oreturn (refillSufficient _ h) σ) = _ + rw [readRefillHead_eqA hk hne]; rfl + rw [rd] + exact ⟨fun h => by rw [h], fun h => Option.some.inj h⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtPpTsA.lean b/proofs/Fl4ProofsA64/P3/UtPpTsA.lean new file mode 100644 index 000000000..db19ee5d3 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtPpTsA.lean @@ -0,0 +1,292 @@ +/- +P3 (aarch64, A64-P3 Untyped, J8 `hTs`): **the reset's invariant across the preemption point's timestamp**. + +* The record without `SysL` is the shared `JStage3TA.timestampA` (`P3/Stage3PreemptTA`); `SysL` and the timing + facts are proved here. +* **`UtRstInvA.timestampA`**: `preemption_point_refinesA`'s `hTs`, with the timing facts carried beside the invariant + (they are re-established). + +Named premises: +* [J] `V`, `CI`, `R`, `Act`: the entry's timing facts (`ValidMachineTimeA`, `CurInvA`, `OffReadyA`, `ActIdleA`); + returned again after the step. +* [P] `P`, `hClock` (the clock relation), `C` (`CounterStepA`), `hNow` (the raw counter read is the spec's + `getCurrentTime`), `hAr` (the arch relation after the timestamp), `hnd` (one domain). +* [J] `hvp`: no VSpace word of `Pp ∪ Tb`, and no ASID-table word, is a preemption-point word. + +New theorems only. +-/ +import Fl4ProofsA64.P3.UtRec6KeepA +import Fl4ProofsA64.P3.Stage3Rec5A +import Fl4ProofsA64.P3.UtResetA +import Fl4ProofsA64.P3.UtRstReadyA +import Fl4ProofsA64.P3.Stage3RestartA +import Fl4ProofsA64.P3.Stage3PreemptTA +import Fl4ProofsA64.P3.Stage3EntryProjA +import Fl4ProofsA64.P3.ScTimeA +import Fl4ProofsA64.P1.QueueKeeps + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +set_option maxRecDepth 100000 + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s s' : S} {a : A} {σ σ' : State Unit} {m : McState} {Rq Nt : Set U64} + {ex : Aeneas.Std.Array obj.Slot 3#usize} {x : U64} + +set_option maxHeartbeats 2000000 in +/-- **`preemption_point_refinesA`'s `hTs`** (module docstring). -/ +theorem UtRstInvA.timestampA {ar : machine.Arch A} {p sb x i : U64} + (I : UtRstInvA (ar := ar) st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n nA m p sb x Rq Nt ex s a σ i) + (hvp : ∀ b, VsR (L.ksb s) (Pp ∪ Tb) b → Aligned b ∧ ¬ PPW st s b) + {Clock : A → MachineState → Prop} (P : NowAgreeA ar Clock) (hClock : Clock a σ.machineState) + (hnd : numDomains = 1) (V : ValidMachineTimeA σ) (C : CounterStepA σ) (CI : CurInvA σ) (R : OffReadyA σ) + (Act : ActIdleA σ) (hNow : fl4ReadRawCounter σ.machineState = getCurrentTime σ.machineState) + (hAr : sched.update_timestamp st ar s a ⦃ r => AR r.2 Z.hw ⦄) : + sched.update_timestamp st ar s a ⦃ r => ∃ σ1, ((), σ1) ∈ (updateTimeStamp (Z := Unit) σ).1 ∧ + UtRstInvA (ar := ar) st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n nA m p sb x Rq Nt ex r.1 r.2 σ1 i ∧ + (∀ y ∈ OS, capA st r.1 y = capA st s y) ∧ ValidMachineTimeA σ1 ∧ CurInvA σ1 ∧ OffReadyA σ1 ∧ + ActIdleA σ1 ∧ Clock r.2 σ1.machineState ⦄ := by + have J := I.rec_ + have E0 := J.yieldQueueA + refine WP.spec_mono (WP.spec_and (WP.spec_and (WP.spec_and (update_timestamp_refinesA E0 a P hClock hnd V C CI R Act) + (update_timestamp_wordsA E0.sched.toSchedSRN a P hClock)) hAr) + ((J.toTA I.sact I.noSwitch).timestampA (fun b hb => hvp b (hb.imp (ptw_mono utVsPtA_sub) id)) P hClock hnd V C hNow + (CurEntryGA.of_cur CI R J.sr.inv) hAr)) ?_ + rintro ⟨s', a'⟩ ⟨⟨⟨⟨τ, hτ, E1, hCl, V1, CI1, R1, Act1, -, -, F⟩, now, -, -, es⟩, har⟩, σT, memT, JT, -, -, -⟩ + dsimp only at hτ E1 hCl F es har memT JT ⊢ + have eτ := fl4UpdateTimestamp_eqA σ + rw [hτ] at eτ + have eτ' : τ = fl4TimestampState (rawNextA σ.machineState) { σ with machineState := rawStateA σ.machineState } := + (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst eτ))).2 + have mem : ((), τ) ∈ (updateTimeStamp (Z := Unit) σ).1 := by + rw [← fl4UpdateTimestamp_eq_l4vA σ hnd hNow, hτ] + exact Set.mem_singleton _ + have kh : τ.kheap = σ.kheap := by rw [eτ']; rfl + have um : τ.machineState.underlyingMemory = σ.machineState.underlyingMemory := by rw [eτ']; rfl + have irqN : τ.interruptIrqNode = σ.interruptIrqNode := by rw [eτ']; rfl + have cdt : τ.cdt = σ.cdt := by rw [eτ']; rfl + have orig : τ.isOriginalCap = σ.isOriginalCap := by rw [eτ']; rfl + have HS : SpecSchedA σ τ := + { kh := fun q => Or.inl (by rw [kh]) + irqS := by rw [eτ']; rfl + irqN := irqN + asid := by rw [eτ']; rfl + vcur := by rw [eτ']; rfl + cdt := cdt + orig := orig } + have SSm : SchedSameA σ { τ with curTime := σ.curTime, consumedTime := σ.consumedTime } := + { kh := fun q ko _ => by show τ.kheap q = some ko ↔ σ.kheap q = some ko; rw [kh] + cur := by rw [eτ']; rfl + idle := by rw [eτ']; rfl + csc := by rw [eτ']; rfl + rq := by rw [eτ']; rfl + rls := by rw [eτ']; rfl + time := rfl + cons := rfl } + have SSz : SSzA σ τ := fun q y k h => ⟨y, by rw [← kh]; exact h⟩ + -- the two node words + have hc0 := J.sq.cur + have nd := J.sq.nodes _ hc0 + have d48 : layout.node.CUR_TIME.val < 4288 := by simp [layout.node.CUR_TIME] + have d40 : layout.node.CONSUMED.val < 4288 := by simp [layout.node.CONSUMED] + have v48 : layout.node.CUR_TIME.val = 48 := by unfold layout.node.CUR_TIME; rfl + have v40 : layout.node.CONSUMED.val = 40 := by unfold layout.node.CONSUMED; rfl + have a48 := nodeAt_aligned nd d48 (by rw [v48]) + have a40 := nodeAt_aligned nd d40 (by rw [v40]) + obtain ⟨tA, etA⟩ : ∃ b, b = L.nodeAt s (L.cpu s) layout.node.CUR_TIME := ⟨_, rfl⟩ + obtain ⟨cA, ecA⟩ : ∃ b, b = L.nodeAt s (L.cpu s) layout.node.CONSUMED := ⟨_, rfl⟩ + rw [← etA] at a48 + rw [← ecA] at a40 + obtain ⟨used, es'⟩ : ∃ used, s' = L.wr (L.wr s tA now) cA used := by + have es2 : s' = L.wr (L.wr s tA now) cA (wa (L.mem s (L.nodeA s layout.node.CONSUMED)) + (core.num.U64.wrapping_sub now (L.curTime s))) := by + rw [etA, ecA]; exact es + exact ⟨_, es2⟩ + have hw : WritesIn st s s' (fun b => b = tA ∨ b = cA) := by + rw [es'] + exact (writesIn_wr (st := st) (s := s) (v := now) a48).trans (writesIn_wr (st := st) (v := used) a40) + have kb : L.ksb s' = L.ksb s := hw.1 + have cpu : L.cpu s' = L.cpu s := hw.2.1 + have tv : tA.val = L.nodeBase s (L.cpu s) + 48 := by rw [etA, nodeAt_val nd d48, v48] + have cv : cA.val = L.nodeBase s (L.cpu s) + 40 := by rw [ecA, nodeAt_val nd d40, v40] + have hnb : L.nodeBase s (L.cpu s) = (L.ksb s).val + 223488 + (L.cpu s).val * 4288 := rfl + have memk : ∀ b : U64, Aligned b → b.val ≠ tA.val → b.val ≠ cA.val → L.mem s' b = L.mem s b := fun b hb n1 n2 => + hw.mem_eq hb (fun h => h.elim (fun e => n1 (by rw [e])) (fun e => n2 (by rw [e]))) + have hcpu128 : (L.cpu s).val < 128 := by + obtain ⟨k, -, hk, h⟩ := J.j.1.csN + have := (h _).1 hc0 + omega + -- the words written are node words, in no object + have hblk := BXI.blk J.sys.ent.bxi + have Lc := LvC.of_blk hblk + have inKs : ∀ b : U64, (b = tA ∨ b = cA) → (L.ksb s).val + 223488 ≤ b.val ∧ b.val < (L.ksb s).val + 772352 := by + rintro b (e | e) <;> rw [e] + · rw [tv, hnb]; omega + · rw [cv, hnb]; omega + have inKsS : ∀ b : U64, (b = tA ∨ b = cA) → (L.ksb s).val + 223488 ≤ b.val ∧ b.val < (L.ksb s).val + 772352 ∧ + (b.val - (L.ksb s).val - 223488) % 4288 ≠ 120 ∧ (b.val - (L.ksb s).val - 223488) % 4288 ≠ 128 := by + rintro b (e | e) <;> rw [e] + · rw [tv, hnb]; omega + · rw [cv, hnb]; omega + have offT : ∀ b : U64, (b = tA ∨ b = cA) → ∀ t ∈ H.tcbs, b.val < t.val ∨ t.val + 4096 ≤ b.val := by + intro b hb t ht + have d := nd.disj t ht + have g := inKs b hb + rcases hb with e | e <;> rw [e] + · rw [tv]; unfold LawfulStore.nodeBase at d ⊢; omega + · rw [cv]; unfold LawfulStore.nodeBase at d ⊢; omega + have offK : ∀ q k, H q = some k → (∀ y, k ≠ .other y) → ∀ b : U64, q.val ≤ b.val → b.val < q.val + 2 ^ k.bits → + ¬ (b = tA ∨ b = cA) := by + intro q k hq ne b h1 h2 hb + have g := inKs b hb + rcases J.ko.1 q k hq ne with g' | g' <;> omega + have offW : ∀ b : U64, Aligned b → ¬ (b = tA ∨ b = cA) → L.mem s' b = L.mem s b := fun b hb n => hw.mem_eq hb n + have wS : WritesIn st s s' (SchedStepWA (st := st) Z m σ s) := + hw.mono fun b hb => Or.inl (Or.inr (Or.inl (Or.inr (Or.inr (Or.inr (inKsS b hb)))))) + have osk : OSKeep st OS s s' := J.osKeep_stepA wS + have tcbK : ∀ t ∈ Z.T, ∀ b : U64, Aligned b → t.val ≤ b.val → b.val < t.val + 4096 → L.mem s' b = L.mem s b := by + intro t ht b hb h1 h2 + have htH : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + refine offW b hb (fun h => ?_) + rcases offT b h t htH with g | g <;> omega + have tAl : ∀ t ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → Aligned (wa t K) := fun t ht K h1 h2 => + field_aligned (J.sr.core.tcbs t ht) h1 h2 + have tK : ∀ t ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → L.mem s' (wa t K) = L.mem s (wa t K) := by + intro t ht K h1 h2 + have f := (J.sr.core.tcbs t ht).fits + have ev : (wa t K).val = t.val + K.val := by rw [wa_valP]; exact Nat.mod_eq_of_lt (by omega) + exact tcbK t ht _ (tAl t ht K h1 h2) (by omega) (by omega) + -- the system invariants + have hN1 : L.wr s tA now = L.wr s (L.nodeAt s (L.cpu s) layout.node.CUR_TIME) now := by rw [etA] + have e2 : s' = L.wr (L.wr s tA now) (L.nodeAt (L.wr s tA now) (L.cpu (L.wr s tA now)) layout.node.CONSUMED) + used := by + rw [es', L.cpu_wr, nodeAt_of_ksb (L.ksb_wr _ _ _), ← ecA] + have lf48 : LowFree layout.node.CUR_TIME.val := by rw [v48]; unfold LowFree; omega + have lf40 : LowFree layout.node.CONSUMED.val := by rw [v40]; unfold LowFree; omega + have bx1 := nodew_bxc (o := layout.node.CUR_TIME) (v := now) J.sys.ent.bxi lf48 (by rw [v48]; omega) + (by rw [v48]; omega) + rw [← hN1] at bx1 + have bx2 := nodew_bxc (o := layout.node.CONSUMED) (v := used) bx1.1.1 lf40 (by rw [v40]; omega) + (by rw [v40]; omega) + rw [← e2] at bx2 + have blk2 := BXI.blk bx2.1.1 + have sq2 : SchedQueuesInv st H.tcbs Z.Cs H.omega H s' Z.Q Z.R := by + obtain ⟨-, hB, hX, hSg, -⟩ := J.sys.ent.bxi + have n1 := node_bx (o := layout.node.CUR_TIME) (v := now) J.sq hB hX hSg lf48 (by rw [v48]; omega) + (by rw [v48]; omega) + rw [← hN1] at n1 + obtain ⟨q1, hB1, hX1, hSg1, -⟩ := n1 + have n2 := node_bx (o := layout.node.CONSUMED) (v := used) q1 hB1 hX1 hSg1 lf40 (by rw [v40]; omega) + (by rw [v40]; omega) + rw [← e2] at n2 + exact n2.1 + have hKo : ∀ o : U64, o.val ∈ (∅ : Set Nat) → o.val < 224 → ∀ y ∈ OS, + ¬ (y.val ≤ (wa (L.ksb s) o).val ∧ (wa (L.ksb s) o).val < y.val + 32) := + fun _ h => absurd h (Set.notMem_empty _) + have dfw : ∀ b : U64, (b = tA ∨ b = cA) → + ∃ c ∈ Z.Cs, ∃ o : U64, o.val < 4288 ∧ o.val ≠ 88 ∧ o.val ≠ 8 ∧ b = L.nodeAt s c o := by + rintro b (e | e) + · exact ⟨_, hc0, _, d48, by rw [v48]; omega, by rw [v48]; omega, e.trans etA⟩ + · exact ⟨_, hc0, _, d40, by rw [v40]; omega, by rw [v40]; omega, e.trans ecA⟩ + have d : DF st H Z.Cs (L.curW s) ∅ s s' := by + refine ⟨hw.mono fun b hb => ?_, (LvF.ofCwN Lc hw fun b _ hb => ?_).mono (LvF.none_le _)⟩ + · obtain ⟨c, hc, o, o1, o2, -, e⟩ := dfw b hb + exact Or.inl (Or.inr (Or.inl ⟨c, hc, o, o1, o2, e⟩)) + · obtain ⟨c, hc, o, o1, o2, o3, e⟩ := dfw b hb + exact Or.inr ⟨c, hc, o, o1, o2, o3, s, rfl, e⟩ + obtain ⟨sysl2, Y2⟩ := SysL.dfB (Bw := fun _ => False) I.place J.sys.ent.bxi I.run hKo (fun _ h => h.elim) + (fun _ _ _ _ _ h => h) J.sys.sysl I.symL (DFB.ofDF d) + -- the record without `SysL` is the shared module's, at the same spec outcome + have eT : σT = τ := by + have e : (updateTimeStamp (Z := Unit) σ).1 = {((), τ)} := by + rw [← fl4UpdateTimestamp_eq_l4vA σ hnd hNow, hτ] + rw [e] at memT + exact (Prod.mk.inj (Set.mem_singleton_iff.1 memT)).2 + rw [eT] at JT + have X := utTA_extraA J JT kh + have sv : SchedValidA τ := J.schedValid.congr SSm.rq SSm.rls (by rw [eτ']; rfl) SSm.cur SSm.idle SSm.csc + (fun q y k h => by rw [kh]; exact h) (fun t y h => ⟨y, by rw [kh]; exact h, rfl⟩) + (fun t y h => ⟨y, by rw [← kh]; exact h, rfl, rfl⟩) + have K6 := utRec6_keepA J (s' := s') (σ' := τ) (fun u hu => osk.capA hu) kh + (fun v hv o ho h8 => by + obtain ⟨-, -, al, out, -⟩ := utVc_wordA J hv ho h8 + refine offW _ al (fun h => ?_) + have := inKs _ h + omega) + (fun t ht K hK => by + rcases hK with rfl | rfl <;> exact tK t ht _ (by simp) (by simp)) + have J2 := JT.toJA sysl2 X.1 X.2.1 (heapWin := J.heapWin) (coresAll := J.coresAll) + (utWin := K6.1) (devWin := J.devWin.keepA (fun u hu => osk.capA hu)) (vcBack := K6.2.1) (runBack := K6.2.2.1) (vcSnap := K6.2.2.2.1) (vcOS := J.vcOS) + (userFr := K6.2.2.2.2) (frTh := J.frTh.keepA (fun u hu => osk.capA hu) fun _ _ e => e) (bfSpec := J.bfSpec) + (ptCapIn := fun y hy q asid va h => J.ptCapIn y hy q asid va ((osk.capA (J.j5.2.1 y hy)).symm.trans h)) + (symL := Y2) + (fhOK := fun t ht => by + rw [osk.capA (J.j5.2.1 _ (isSlot_fhA ((J.sr.thT t).2 ht)))] + exact J.fhOK t ht) + (zwf := J.zwf.keepW fun y hy => osk.capA (J.j5.2.1 y hy)) + (schedValid := sv) + have curW : L.curW s' = L.curW s := by + have d2 : layout.node.CUR_THREAD.val < 4288 := by simp [layout.node.CUR_THREAD] + have al := nodeAt_aligned nd d2 (by simp [layout.node.CUR_THREAD]) + have g : (L.nodeAt s (L.cpu s) layout.node.CUR_THREAD).val = + L.nodeBase s (L.cpu s) + layout.node.CUR_THREAD.val := nodeAt_val nd d2 + have n48 : layout.node.CUR_THREAD.val ≠ 48 ∧ layout.node.CUR_THREAD.val ≠ 40 := by + simp [layout.node.CUR_THREAD] + unfold LawfulStore.curW + rw [cpu, nodeAt_of_ksb kb] + exact memk _ al (by omega) (by omega) + have run : Runnable (L.mem s' (stA (L.curW s'))) := by + have ct := I.rec_.sq.curT + have ctZ : L.curW s ∈ Z.T := J.sr.curT + rw [curW, stA_eq, tK _ ctZ _ (by simp) (by simp), ← stA_eq] + exact I.run + refine ⟨τ, mem, ?_, fun y hy => osk.capA hy, V1, CI1, R1, Act1, hCl⟩ + exact + { sact := JT.sact + noSwitch := JT.noSwitch + rec_ := J2 + symL := Y2 + clr := ⟨I.clr.obj, fun c hc => by + have := I.clr.nodes c hc + unfold LawfulStore.nodeBase at this ⊢ + rw [kb]; exact this, by rw [kb]; exact I.clr.ks⟩ + place := SysPlace.keepO I.place osk kb + run := run + cap := by rw [osk.capA I.place.src]; exact I.cap + noch := by + have htr : TreeA st OS Z.G.chains Z.G.par s := + J.j.1.cdt.tree.grow J.j5.2.1 (fun y hy hn => J.j5.2.2 y hy hn) + have hx : x ∈ OS := I.place.src + have hxG : x ∈ Z.G.chains.flatten := + (htr.lk.members x).2 ⟨hx, by rw [I.cap]; exact fun e => by cases e⟩ + have x0 : x ≠ 0#u64 := I.place.os.nz x hx + rw [utNoChild_congrA (s := s) (s' := s') (x := x) (fun y hy h0 => ?_) x0] + · exact I.noch + · have hyO : y ∈ OS := by + rcases hy with e | e + · rw [e]; exact hx + · rw [e] + exact ((htr.lk.members _).1 (htr.lk.next_memA hxG (e ▸ h0))).1 + have k := osk y hyO + exact ⟨k.1, k.2.1, k.2.2.2⟩ + heap := by rw [kh]; exact I.heap + link := by rw [cdt]; exact I.link + orig := by rw [orig]; exact I.orig + zero := by rw [um]; exact I.zero } + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtPpWuA.lean b/proofs/Fl4ProofsA64/P3/UtPpWuA.lean new file mode 100644 index 000000000..7e3375fdf --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtPpWuA.lean @@ -0,0 +1,189 @@ +/- +P3 (aarch64, A64-P3 Untyped, J8 `hW`): **the record across the preemption point's `WORK_UNITS` write**. + +* `JStage3A.of_workUnitsA`: `JStage3A` (concrete J5, J7 at `J7R2A`) and `SymL` after FL4's `ks_set WORK_UNITS`; the + spec state is unchanged. The record without `SysL` is the shared `JStage3TA.workUnitsA` (`P3/Stage3PreemptTA`); + only `SysL`'s preservation is proved here. +* **`UtRstInvA.workUnitsA`**: the reset loop's invariant after the write, and the `OS` caps — `preemption_point_refinesA`'s + `hW` for `R := UtRstInvA`. + +Named premises: +* [J] `hvw`: no word of `sys.ent.vs`'s pools and tables, and no ASID-table word, is `WORK_UNITS`. + +New theorems only. +-/ +import Fl4ProofsA64.P3.UtRec6KeepA +import Fl4ProofsA64.P3.Stage3Rec5A +import Fl4ProofsA64.P3.UtResetA +import Fl4ProofsA64.P3.UtRstReadyA +import Fl4ProofsA64.P3.Stage3RestartA +import Fl4ProofsA64.P3.Stage3PreemptTA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +set_option maxRecDepth 100000 + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s : S} {a : A} {σ : State Unit} {m : McState} {Rq Nt : Set U64} + {ex : Aeneas.Std.Array obj.Slot 3#usize} {x : U64} + +set_option maxHeartbeats 2000000 in +/-- **`JStage3A` across the `WORK_UNITS` write** (module docstring). -/ +theorem JStage3A.of_workUnitsA + (sact : SActA σ) (noSwitch : ∀ u, σ.schedulerAction ≠ .switchThread u) + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (P : SysPlace st H Z.Cs H.frame Rq Nt OS s ex x) (Y : SymL st H Z.Cs s) + (hrun : Runnable (L.mem s (stA (L.curW s)))) + (hvw : ∀ b, VsR (L.ksb s) (Pp ∪ Tb) b → Aligned b ∧ b ≠ wa (L.ksb s) layout.ks.WORK_UNITS) (v : U64) : + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) + (L.wr s (wa (L.ksb s) layout.ks.WORK_UNITS) v) a σ m ∧ + SymL st H Z.Cs (L.wr s (wa (L.ksb s) layout.ks.WORK_UNITS) v) ∧ + OSKeep st OS s (L.wr s (wa (L.ksb s) layout.ks.WORK_UNITS) v) := by + obtain ⟨wu, ewu⟩ : ∃ wu, wu = wa (L.ksb s) layout.ks.WORK_UNITS := ⟨_, rfl⟩ + rw [← ewu] + have hblk := BXI.blk J.sys.ent.bxi + obtain ⟨ha, hΩ, hnS⟩ := ks_work_units hblk + rw [← ewu] at ha hΩ hnS + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.sys.ent.ksw + have e40 : (layout.ks.WORK_UNITS : U64).val = 40 := by unfold layout.ks.WORK_UNITS; rfl + have wv : wu.val = (L.ksb s).val + 40 := by + rw [ewu, wa_valP, e40]; exact Nat.mod_eq_of_lt (by omega) + have kb : L.ksb (L.wr s wu v) = L.ksb s := L.ksb_wr _ _ _ + have hw : WritesIn st s (L.wr s wu v) (· = wu) := writesIn_wr ha + have memk : ∀ b, Aligned b → b ≠ wu → L.mem (L.wr s wu v) b = L.mem s b := fun b hb ne => hw.mem_eq hb ne + have C := LvC.of_blk hblk + -- the system invariants + have blk2 := blk_capw_wr (st := st) (v := v) hblk ha (Or.inr ⟨hΩ, hnS⟩) + have d : DFB st H Z.Cs (L.curW s) ∅ (· = wu) s (L.wr s wu v) := + DFB.ofBw C hw fun b e => by rw [e]; exact ⟨hΩ, hnS⟩ + have hKo : ∀ o : U64, o.val ∈ (∅ : Set Nat) → o.val < 224 → ∀ y ∈ OS, + ¬ (y.val ≤ (wa (L.ksb s) o).val ∧ (wa (L.ksb s) o).val < y.val + 32) := + fun _ h => absurd h (Set.notMem_empty _) + have hBo : ∀ y ∈ OS, ∀ b : U64, y.val ≤ b.val → b.val < y.val + 32 → ¬ b = wu := by + intro y hy b h1 h2 e + refine J.sys.sysl.ly.kslo layout.ks.WORK_UNITS (by rw [e40]; omega) y hy ?_ + rw [← ewu, ← e] + exact ⟨h1, h2⟩ + obtain ⟨sysl2, Y2⟩ := SysL.dfB P J.sys.ent.bxi hrun hKo (fun b e => by rw [e]; exact ⟨hΩ, hnS⟩) hBo J.sys.sysl Y d + -- the record without `SysL` across the write is the shared module's + have T := (J.toTA sact noSwitch).workUnitsA (fun b hb => hvw b (hb.imp (ptw_mono utVsPtA_sub) id)) v + rw [← ewu] at T + have X := utTA_extraA J T.1 rfl + have K6 := utRec6_keepA J (s' := L.wr s wu v) (σ' := σ) (fun u hu => T.2.capA hu) rfl + (fun u hu o ho h8 => by + obtain ⟨-, -, al, -, ap⟩ := utVc_wordA J hu ho h8 + refine memk _ al (fun e => ap layout.ks.WORK_UNITS (by rw [e40]; omega) ?_) + rw [← ewu, e]) + (fun t ht K hK => by + have k : K.val < 4096 ∧ K.val % 8 = 0 := by rcases hK with rfl | rfl <;> simp + obtain ⟨al, out⟩ := utT_wordA J ht k.1 k.2 + refine memk _ al (fun e => ?_) + rw [e, wv] at out + omega) + refine ⟨T.1.toJA sysl2 X.1 X.2.1 (heapWin := J.heapWin) (coresAll := J.coresAll) + (utWin := K6.1) (devWin := J.devWin.keepA (fun u hu => T.2.capA hu)) (vcBack := K6.2.1) (runBack := K6.2.2.1) (vcSnap := K6.2.2.2.1) (vcOS := J.vcOS) + (userFr := K6.2.2.2.2) (frTh := J.frTh.keepA (fun u hu => T.2.capA hu) fun _ _ e => e) (bfSpec := J.bfSpec) + (ptCapIn := fun y hy q asid va h => J.ptCapIn y hy q asid va ((T.2.capA (J.j5.2.1 y hy)).symm.trans h)) + (symL := Y2) + (fhOK := fun t ht => by + rw [T.2.capA (J.j5.2.1 _ (isSlot_fhA ((J.sr.thT t).2 ht)))] + exact J.fhOK t ht) + (zwf := J.zwf.keepW fun y hy => T.2.capA (J.j5.2.1 y hy)) + (schedValid := J.schedValid), Y2, T.2⟩ + +/-- **The reset loop's invariant across the `WORK_UNITS` write** (`preemption_point_refinesA`'s `hW`). -/ +theorem UtRstInvA.workUnitsA {p sb x : U64} {i : U64} {s s1 : S} {a : A} {σ : State Unit} + (I : UtRstInvA (ar := ar) st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n nA m p sb x Rq Nt ex s a σ i) + (hvw : ∀ b, VsR (L.ksb s) (Pp ∪ Tb) b → Aligned b ∧ b ≠ wa (L.ksb s) layout.ks.WORK_UNITS) {v : U64} + (h : obj.ks_set st s layout.ks.WORK_UNITS v = ok s1) : + UtRstInvA (ar := ar) st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n nA m p sb x Rq Nt ex s1 a σ i ∧ + ∀ y ∈ OS, capA st s1 y = capA st s y := by + have e1 : s1 = L.wr s (wa (L.ksb s) layout.ks.WORK_UNITS) v := by + have g := eq_of_spec_fd (obj.ks_set.spec (st := st) s layout.ks.WORK_UNITS v) + exact okinj (h.symm.trans g) + obtain ⟨J2, Y2, osk⟩ := JStage3A.of_workUnitsA I.sact I.noSwitch I.rec_ I.place I.symL I.run hvw v + rw [← e1] at J2 Y2 osk + have kb : L.ksb s1 = L.ksb s := by rw [e1]; exact L.ksb_wr _ _ _ + have cpu : L.cpu s1 = L.cpu s := by rw [e1]; exact L.cpu_wr _ _ _ + have hblk := BXI.blk I.rec_.sys.ent.bxi + obtain ⟨ha, hΩ, -⟩ := ks_work_units hblk + have C := LvC.of_blk hblk + have hom := C.om _ hΩ + obtain ⟨wu, ewu⟩ : ∃ wu, wu = wa (L.ksb s) layout.ks.WORK_UNITS := ⟨_, rfl⟩ + rw [← ewu] at ha hom + have memk : ∀ b : U64, Aligned b → b.val ≠ wu.val → L.mem s1 b = L.mem s b := fun b hb ne => by + rw [e1, ← ewu]; exact L.mem_wr_ne ha hb (fun e => ne e.symm) + have curW : L.curW s1 = L.curW s := by + have hc := I.rec_.sq.cur + have nd := I.rec_.sq.nodes _ hc + have d : layout.node.CUR_THREAD.val < 4288 := by simp [layout.node.CUR_THREAD] + have al := nodeAt_aligned nd d (by simp [layout.node.CUR_THREAD]) + have g : (L.nodeAt s (L.cpu s) layout.node.CUR_THREAD).val = + L.nodeBase s (L.cpu s) + layout.node.CUR_THREAD.val := nodeAt_val nd d + unfold LawfulStore.curW + rw [cpu, nodeAt_of_ksb kb] + refine memk _ al (fun e => ?_) + have k := hom.2.2 _ hc + omega + have run : Runnable (L.mem s1 (stA (L.curW s1))) := by + have ct := I.rec_.sq.curT + have hta := I.rec_.sq.tcbs _ ct + have al : Aligned (stA (L.curW s)) := by rw [stA_eq]; exact field_aligned hta (by simp) (by simp) + have f := hta.fits + have ev : (stA (L.curW s)).val = (L.curW s).val + 3328 := by + rw [stA_eq]; exact Notif.tfield_val hta (by decide : (3328#u64 : U64).val < 4096) + have k : wu.val < (L.curW s).val ∨ (L.curW s).val + 2 ^ 12 ≤ wu.val := hom.2.1 _ ct + have ne : (stA (L.curW s)).val ≠ wu.val := fun e => by omega + rw [curW, memk _ al ne] + exact I.run + refine ⟨?_, fun y hy => osk.capA hy⟩ + exact + { sact := I.sact + noSwitch := I.noSwitch + rec_ := J2 + symL := Y2 + clr := ⟨I.clr.obj, fun c hc => by + have := I.clr.nodes c hc + unfold LawfulStore.nodeBase at this ⊢ + rw [kb]; exact this, by rw [kb]; exact I.clr.ks⟩ + place := SysPlace.keepO I.place osk kb + run := run + cap := by rw [osk.capA I.place.src]; exact I.cap + noch := by + have J := I.rec_ + have htr : TreeA st OS Z.G.chains Z.G.par s := + J.j.1.cdt.tree.grow J.j5.2.1 (fun y hy hn => J.j5.2.2 y hy hn) + have hx : x ∈ OS := I.place.src + have hxG : x ∈ Z.G.chains.flatten := + (htr.lk.members x).2 ⟨hx, by rw [I.cap]; exact fun e => by cases e⟩ + have x0 : x ≠ 0#u64 := I.place.os.nz x hx + rw [utNoChild_congrA (s := s) (s' := s1) (x := x) (fun y hy h0 => ?_) x0] + · exact I.noch + · have hyO : y ∈ OS := by + rcases hy with e | e + · rw [e]; exact hx + · rw [e] + exact ((htr.lk.members _).1 (htr.lk.next_memA hxG (e ▸ h0))).1 + have k := osk y hyO + exact ⟨k.1, k.2.1, k.2.2.2⟩ + heap := I.heap + link := I.link + orig := I.orig + zero := I.zero } + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtRec6KeepA.lean b/proofs/Fl4ProofsA64/P3/UtRec6KeepA.lean new file mode 100644 index 000000000..80ef706cd --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtRec6KeepA.lean @@ -0,0 +1,86 @@ +/- +P3 (aarch64, A64-P3 Untyped): **keeping the record-edit-#6 fields**. + +* `utVc_wordA`: a word of a record VCPU is aligned, lies in no node word and in none of the state block's first + 223488 bytes (J7's `apart`, `HeapKsOff`). +* `utT_wordA`: a word of a record TCB is aligned and off the state block. +* `utRec6_keepA`: `utWin`, `vcBack`, `runBack`, `vcSnap`, `userFr` across a step keeping the `OS` caps, the spec heap, + every VCPU word and the TCBs' two VCPU words (the preemption point's writes). + +No named premise. New theorems only. +-/ +import Fl4ProofsA64.P3.Stage3A +import Fl4ProofsA64.P3.Stage3J5A +import Fl4ProofsA64.P3.Stage3J7A + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4SpecA64 Classical + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s s' : S} {a : A} {σ σ' : State Unit} {m : McState} + +theorem utVc_wordA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {v o : U64} (hv : v ∈ Z.V) (ho : o.val < 4096) (h8 : o.val % 8 = 0) : + H v = some (.other 12) ∧ (wa v o).val = v.val + o.val ∧ Aligned (wa v o) ∧ + ((wa v o).val < (L.ksb s).val + 223488 ∨ (L.ksb s).val + 772352 ≤ (wa v o).val) ∧ + ∀ k : U64, k.val < 223488 → wa (L.ksb s) k ≠ wa v o := by + obtain ⟨Pt, Ub, -, -, armc, -, -, -, -, -, apart, -⟩ := J.j7 + have hH12 : H v = some (.other 12) := armc.vh v hv + have hf := J.sys.ent.heap.fits v _ hH12 + have hal := J.sys.ent.heap.align v _ hH12 + simp only [show (HKind.other 12).bits = 12 from rfl, Nat.reducePow] at hf hal + have ev : (wa v o).val = v.val + o.val := by rw [wa_valP]; exact Nat.mod_eq_of_lt (by omega) + refine ⟨hH12, ev, by show _ % 8 = 0; rw [ev]; omega, ?_, fun k hk => apart _ ⟨v, hv, o, ho, h8, rfl⟩ k hk⟩ + rw [ev] + have := J.ko.2 v 12 hH12 + simp only [Nat.reducePow] at this + omega + +theorem utT_wordA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {t K : U64} (ht : t ∈ Z.T) (hK : K.val < 4096) (h8 : K.val % 8 = 0) : + Aligned (wa t K) ∧ ((wa t K).val < (L.ksb s).val ∨ (L.ksb s).val + 788736 ≤ (wa t K).val) := by + have htH : H t = some .tcb := by + have h : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + exact h + have hf := J.sys.ent.heap.fits t _ htH + have hal := J.sys.ent.heap.align t _ htH + simp only [show HKind.tcb.bits = 12 from rfl, Nat.reducePow] at hf hal + have ev : (wa t K).val = t.val + K.val := by rw [wa_valP]; exact Nat.mod_eq_of_lt (by omega) + refine ⟨by show _ % 8 = 0; rw [ev]; omega, ?_⟩ + rw [ev] + have := J.ko.1 t _ htH (fun _ e => by cases e) + simp only [show HKind.tcb.bits = 12 from rfl, Nat.reducePow] at this + omega + +theorem utRec6_keepA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (capE : ∀ u ∈ OS, capA st s' u = capA st s u) (kh : σ'.kheap = σ.kheap) + (vm : ∀ v ∈ Z.V, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → L.mem s' (wa v o) = L.mem s (wa v o)) + (tm : ∀ t ∈ Z.T, ∀ K : U64, K = 3664#u64 ∨ K = 3672#u64 → L.mem s' (wa t K) = L.mem s (wa t K)) : + UtWinCapA st OS s' ∧ + (∀ v ∈ Z.V, L.mem s' v ≠ 0#u64 → L.mem s' (wa (L.mem s' v) 3664#u64) = v) ∧ + (∀ t ∈ Z.T, L.mem s' (runVcpuA t) ≠ 0#u64 → L.mem s' (wa (L.mem s' (runVcpuA t)) 24#u64) = t) ∧ + VcpuStateSnapA Z.V (L.mem s') σ' ∧ UserFrSpecA st OS s' σ' := by + have v0 : ∀ v ∈ Z.V, L.mem s' v = L.mem s v := fun v hv => by + have := vm v hv 0#u64 (by simp) (by simp) + rwa [wa_zeroVcA] at this + have tw : ∀ t ∈ Z.T, L.mem s' (wa t 3664#u64) = L.mem s (wa t 3664#u64) := fun t ht => tm t ht _ (Or.inl rfl) + have tr : ∀ t ∈ Z.T, L.mem s' (runVcpuA t) = L.mem s (runVcpuA t) := fun t ht => tm t ht _ (Or.inr rfl) + refine ⟨J.utWin.keepA capE, + vcBack_keepA J.vcRun.vtcb v0 tw J.vcBack, + runBack_keepA J.vcRun.run tr (fun v hv => vm v hv _ (by simp) (by simp)) J.runBack, + J.vcSnap.keepA vm (fun v _ x h => by rw [← kh]; exact h), + J.userFr.keepA capE (fun p dev pg h => by rw [kh]; exact h)⟩ + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtResetA.lean b/proofs/Fl4ProofsA64/P3/UtResetA.lean new file mode 100644 index 000000000..d2365eed7 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtResetA.lean @@ -0,0 +1,762 @@ +/- +P3 (aarch64, A64-P3 Untyped, `UtResetA`): **FL4's `reset_untyped` of a used RAM untyped below the chunk size, +refining spec `reset_untyped_cap`, with the whole record**. + +* `utDelStA_withUmA`: spec `delete_objects`' state is the detyped state with the region's bytes zeroed. +* `utReset_startA`: the record after the drop and spec `delete_objects`, FL4's state unchanged, with the facts a chunk + step needs. +* `UtRstInvA`, `utReset_loopA`: the chunk loop, `Ok` or preempted, with the spec's chunk outcomes. +* `utReset_bigA`: a used RAM untyped of at least a chunk, `Ok` or preempted. +* `utReset_smallA`: FL4 returns `Ok` after one clear and the free-index write; the spec has the matching outcome; the + record holds on the dropped sets. + +Named premises: +* [J] `symL`; [S] `othT`, `othC` (B). +* [J] `zb` (`UtZombieA`): a `Zombie`'s count fits its object (`P3/UtSysPlaceA`, which derives from it the `SysPlace` + over the dropped heap). +* [P] `hPost`: spec `arch_post_reset_untyped`'s cache maintenance has an outcome that keeps the state. +* [P] `hchunk`: the spec's `CONFIG_RESET_CHUNK_BITS` (opaque in the port) is FL4's 16. +* [J] `hpp` (contract J8, `PPHypUtA`): the preemption point keeps the loop invariant and the carried predicate `T` + (`UtPpHookA.utPp_hookA` gives it for `T := UtPpTimeA`). +* [J] `hT`, `hT0`: `T` survives a chunk's CNode write, and holds after `delete_objects`. +* [W] decode site: `Dp`, `P`, the slot's `ensure_no_children`, and that the untyped cap is in a CNode. +-/ +import Fl4ProofsA64.P3.Stage3PreemptA +import Fl4ProofsA64.P3.Stage3TA +import Fl4ProofsA64.P3.UtChunkA +import Fl4ProofsA64.P3.UtZeroSpecA +import Fl4ProofsA64.P3.UtSbufA +import Fl4ProofsA64.P3.UtSysPlaceA +import Fl4ProofsA64.P3.UtResetRunA +import Fl4ProofsA64.P3.UtResetLoopA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4SpecA64 Fl4Spec Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {ar : machine.Arch A} + +/-- The spec's bytes with the region `D` zeroed. -/ +def utUm0A (D : Set ObjRef) (σ : State Unit) : Word → Word8 := + fun w => if w ∈ D then 0 else σ.machineState.underlyingMemory w + +theorem utDelStA_withUmA {pu : Word} {sz : Nat} (hsz : sz < 64) (hfit : pu.toNat + 2 ^ sz ≤ 2 ^ 64) (σ : State Unit) : + utDelStA pu sz σ = + withUmA (detype {x | pu ≤ x ∧ x ≤ pu + 2 ^ sz - 1} σ) (utUm0A {x | pu ≤ x ∧ x ≤ pu + 2 ^ sz - 1} σ) := by + have hk : 0 < 2 ^ sz := Nat.two_pow_pos _ + have hk' : 2 ^ sz < 2 ^ 64 := Nat.pow_lt_pow_right (by decide) hsz + have htop : (pu + 2 ^ sz - 1 : Word).toNat = pu.toNat + 2 ^ sz - 1 := by + rw [pow2_word hsz] + exact toNat_add_n_sub1 hk hfit + have e : (fun x : Word => if pu.toNat ≤ x.toNat ∧ x.toNat < pu.toNat + 2 ^ sz then (0 : Word8) + else σ.machineState.underlyingMemory x) = utUm0A {x | pu ≤ x ∧ x ≤ pu + 2 ^ sz - 1} σ := by + funext x + unfold utUm0A + have iff : (pu.toNat ≤ x.toNat ∧ x.toNat < pu.toNat + 2 ^ sz) ↔ + x ∈ {x : Word | pu ≤ x ∧ x ≤ pu + 2 ^ sz - 1} := by + show _ ↔ pu ≤ x ∧ x ≤ pu + 2 ^ sz - 1 + rw [BitVec.le_def, BitVec.le_def, htop] + omega + by_cases h : x ∈ {x : Word | pu ≤ x ∧ x ≤ pu + 2 ^ sz - 1} + · rw [ite_eq_left (iff.2 h), ite_eq_left h] + · rw [ite_eq_right (fun g => h (iff.1 g)), ite_eq_right h] + unfold utDelStA + rw [e] + rfl + +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s : S} {a : A} {σ : State Unit} {m : McState} + +set_option maxHeartbeats 1000000 in +/-- **The record after the drop and spec `delete_objects`** (module docstring). -/ +theorem utReset_startA [Fact (n ≤ Fl4SpecA64.fl4A64Platform.numNodes)] + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {p sb fi : U64} {Ud Acd Rq Nt : Set U64} {ex : Aeneas.Std.Array obj.Slot 3#usize} {x : U64} + (Dp : DecPre st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ud Acd s (.Untyped p sb fi false) ex) + (P : SysPlace st H Z.Cs H.frame Rq Nt OS s ex x) + (hcap : obj.Slot.cap st x s = ok (.Untyped p sb fi false)) + (he : cdt.ensure_no_children st s x = ok (.Ok ())) + (symL : SymL st H Z.Cs s) + (othT : ∀ c ∈ Z.Cs, c ≠ L.cpu s → specPtr (L.mem s (L.nodeAt s c layout.node.CUR_THREAD)) ∉ utDA p sb.val) + (othC : ∀ c ∈ Z.Cs, c ≠ L.cpu s → specPtr (L.mem s (L.nodeAt s c layout.node.CUR_SC)) ∉ utDA p sb.val) + (hn : nA.val = fl4A64Platform.s2IpaBits) + (cte : CslotPtr) {qc : U64} (hq : cte.1 = specPtr qc) (hth : Z.th qc = some (.cnode cte.2.length)) + (hxc : cslotAddrA cte = x) : + let D := utDA p sb.val + let σ1 := utDelStA (specPtr p) sb.val σ + JStage3A st AR (dropZA D Z) (dropHA D H) (dropSetA D OS) U Ac (dropSetA D Pp) Tb Lv ia ie (dropSetA D N) + (dropSetA D E) (dropSetA D Rp) (utBfOffA D (dropBfA D Bf)) stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ1 + (dropMA D m) ∧ + SymL st (dropHA D H) Z.Cs s ∧ + UtClrA st (dropZA D Z) (dropHA D H) s p.val (p.val + 2 ^ sb.val) ∧ + (∀ y ∈ dropSetA D OS, y.val + 32 ≤ p.val ∨ p.val + 2 ^ sb.val ≤ y.val) ∧ + (dropZA D Z).th qc = some (.cnode cte.2.length) ∧ x ∈ dropSetA D OS ∧ + (∀ q : U64, p.val ≤ q.val → q.val < p.val + 2 ^ sb.val → σ1.kheap (specPtr q) = none) ∧ + (∀ a' c', σ1.cdt a' = some c' → ∀ q : U64, p.val ≤ q.val → q.val < p.val + 2 ^ sb.val → + a'.1 ≠ specPtr q ∧ c'.1 ≠ specPtr q) ∧ + (∀ a', σ1.isOriginalCap a' = true → ∀ q : U64, p.val ≤ q.val → q.val < p.val + 2 ^ sb.val → a'.1 ≠ specPtr q) ∧ + (∀ w : Word, p.val ≤ (fl4Ptr w).val → (fl4Ptr w).val < p.val + 2 ^ sb.val → + σ1.machineState.underlyingMemory w = 0) ∧ + (specPtr p).toNat + 2 ^ sb.val ≤ 2 ^ 64 ∧ + (∀ w : Word, (specPtr p).toNat ≤ w.toNat → w.toNat < (specPtr p).toNat + 2 ^ sb.val → + σ1.machineState.underlyingMemory w = 0) ∧ + (∃ Rq' Nt' : Set U64, SysPlace st (dropHA D H) Z.Cs (dropHA D H).frame Rq' Nt' (dropSetA D OS) s + (Array.repeat 3#usize 0#u64) x) := by + have hsal := J.bufAl + have zb := J.zombieBnd + intro D σ1 + have hcx : capA st s x = .Untyped p sb fi false := okinj ((capA_eq s x).symm.trans hcap) + have C := utDropC_of J Dp P hcap he symL othT othC hn + obtain ⟨J1, Y1⟩ := utDrop_topA J C + have hx : x ∈ OS := P.src + have hMdb := J.sys.sysl.sys.stz.1.1 + obtain ⟨hpal, h47, hfi⟩ := J.sys.sysl.sys.ur.utv hMdb hx hcx + have hsb : sb.val < 64 := by omega + have hpfit : p.val + 2 ^ sb.val ≤ 2 ^ 64 := al_fit hpal (by omega) p.bv.isLt + have P3 : PlaceA Z.th (L.ksb s) := J.j.1.place + have hab : AbsSlotA Z.th cte := ⟨qc, hq, Or.inl hth⟩ + have hwf : SpNoOvA p sb.val := by + obtain ⟨ac, -, hr⟩ := ut_absRelA J.j.1.st hab + have h := hr.1 + rw [hxc, show capAtA (L.mem s) x = capA st s x from rfl, hcx] at h + exact h + have hsp : (specPtr p).toNat + 2 ^ sb.val ≤ 2 ^ 64 := ut_spNoOvA hsb hwf + have hD : ∀ z : U64, specPtr z ∈ D ↔ p.val ≤ z.val ∧ z.val < p.val + 2 ^ sb.val := utD_memA hsb hpfit hsp + have eσ : σ1 = withUmA (detype D σ) (utUm0A D σ) := utDelStA_withUmA hsb hsp σ + have J2 := utZeroSpec_stageA J1 D (utUm0A D σ) (fun w => rfl) (fun t ht y b sbf hk hb => by + change (if specPtr t ∈ D then none else σ.kheap (specPtr t)) = some (.tcb y) at hk + rw [ite_eq_right ht.2] at hk + exact utSbuf_keptA J Dp P hcap he hsal t ht.1 ht.2 y b sbf hk hb) + rw [← eσ] at J2 + have hks : (L.ksb s).val + 788736 ≤ p.val ∨ p.val + 2 ^ sb.val ≤ (L.ksb s).val := + (J.sys.sysl.sys.stz.2.2 x hx p sb fi hcx).2 + have Cl : UtClrA st (dropZA D Z) (dropHA D H) s p.val (p.val + 2 ^ sb.val) := + utDrop_clrA J hx hcx hfi hD (J.sys.sysl.sys.stz.2.2 x hx p sb fi hcx).1 hks + have hslots : ∀ y ∈ dropSetA D OS, y.val + 32 ≤ p.val ∨ p.val + 2 ^ sb.val ≤ y.val := by + intro y hy + have h : y ∈ OffR p.val (p.val + 2 ^ sb.val) OS := by rw [C.slotsB]; exact hy + exact h.2 + have xS : IsSlotA Z.th x := hxc ▸ hab.isSlot P3 + have xS' : IsSlotA (dropThA D Z.th) x := by + by_contra h + have := C.gone _ hx xS h + rw [hcx] at this + cases this + have xK : specPtr x ∉ D := C.slotOff _ xS' + have qcK : specPtr qc ∉ D := by + obtain ⟨q, k, hk, h1, h2⟩ := ut_ownerA P3 (th' := dropThA D Z.th) (fun _ _ h => (dropThA_someA.1 h).2) xS' + obtain ⟨kq, hq'⟩ := dropThA_someA.1 hk + obtain ⟨k0, hk0, hsz, hv⟩ := slotVA_of P3 hth (j := blvA cte.2) (by + have := blvA_lt cte.2 + rw [cnode_size]; omega) + have ek : k0 = .cnode cte.2.length := Option.some.inj (hk0.symm.trans hth) + have ex' : slotAddrA (specPtr qc) (BitVec.ofNat 64 (blvA cte.2)) = x := by + rw [← hxc] + unfold cslotAddrA + rw [hq] + exact (cn_slotV P3 hth rfl).1.symm + rw [ex'] at hv + have eq : qc = q := by + by_contra ne + have := P3.apart qc q _ _ hth hq' ne + rw [ek] at hsz + omega + rw [eq]; exact kq + have hheap : ∀ q : U64, p.val ≤ q.val → q.val < p.val + 2 ^ sb.val → σ1.kheap (specPtr q) = none := by + intro q h1 h2 + rw [eσ] + show (if specPtr q ∈ D then none else σ.kheap (specPtr q)) = none + rw [ite_eq_left ((hD q).2 ⟨h1, h2⟩)] + have hlink : ∀ a' c', σ1.cdt a' = some c' → ∀ q : U64, p.val ≤ q.val → q.val < p.val + 2 ^ sb.val → + a'.1 ≠ specPtr q ∧ c'.1 ≠ specPtr q := by + intro a' c' h q h1 h2 + rw [eσ] at h + have g := C.cdt a' c' h + have mq := (hD q).2 ⟨h1, h2⟩ + exact ⟨fun e => g.1 (e ▸ mq), fun e => g.2 (e ▸ mq)⟩ + have horig : ∀ a', σ1.isOriginalCap a' = true → ∀ q : U64, p.val ≤ q.val → q.val < p.val + 2 ^ sb.val → + a'.1 ≠ specPtr q := by + intro a' h q h1 h2 e + have g := (absSlot_dropA.1 (J2.origLive a' h)).2 + exact g (e ▸ (hD q).2 ⟨h1, h2⟩) + have hz : ∀ w : Word, p.val ≤ (fl4Ptr w).val → (fl4Ptr w).val < p.val + 2 ^ sb.val → + σ1.machineState.underlyingMemory w = 0 := by + intro w h1 h2 + rw [eσ] + show (if w ∈ D then (0 : Word8) else σ.machineState.underlyingMemory w) = 0 + have mw : specPtr (fl4Ptr w) ∈ D := (hD _).2 ⟨h1, h2⟩ + rw [specPtr_fl4Ptr] at mw + rw [ite_eq_left mw] + have hzn : ∀ w : Word, (specPtr p).toNat ≤ w.toNat → w.toNat < (specPtr p).toNat + 2 ^ sb.val → + σ1.machineState.underlyingMemory w = 0 := by + intro w h1 h2 + show (if (specPtr p).toNat ≤ w.toNat ∧ w.toNat < (specPtr p).toNat + 2 ^ sb.val then (0 : Word8) + else σ.machineState.underlyingMemory w) = 0 + rw [ite_eq_left ⟨h1, h2⟩] + exact ⟨J2, Y1, Cl, hslots, dropThA_someA.2 ⟨qcK, hth⟩, ⟨hx, xK⟩, hheap, hlink, horig, hz, hsp, hzn, + utSysPlace_recA J Dp P hcap he zb C.heapB C.slotsB⟩ + +set_option maxHeartbeats 1000000 in +/-- **A used RAM untyped below the chunk size** (module docstring). -/ +theorem utReset_smallA [Fact (n ≤ Fl4SpecA64.fl4A64Platform.numNodes)] (ar : machine.Arch A) + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {p sb fi : U64} {Ud Acd Rq Nt : Set U64} {ex : Aeneas.Std.Array obj.Slot 3#usize} {x : U64} + (Dp : DecPre st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ud Acd s (.Untyped p sb fi false) ex) + (P : SysPlace st H Z.Cs H.frame Rq Nt OS s ex x) + (hcap : obj.Slot.cap st x s = ok (.Untyped p sb fi false)) + (he : cdt.ensure_no_children st s x = ok (.Ok ())) + (symL : SymL st H Z.Cs s) + (othT : ∀ c ∈ Z.Cs, c ≠ L.cpu s → specPtr (L.mem s (L.nodeAt s c layout.node.CUR_THREAD)) ∉ utDA p sb.val) + (othC : ∀ c ∈ Z.Cs, c ≠ L.cpu s → specPtr (L.mem s (L.nodeAt s c layout.node.CUR_SC)) ∉ utDA p sb.val) + (hn : nA.val = fl4A64Platform.s2IpaBits) + (cte : CslotPtr) {qc : U64} (hq : cte.1 = specPtr qc) (hth : Z.th qc = some (.cnode cte.2.length)) + (hxc : cslotAddrA cte = x) + (hPost : ∀ σ' : State Unit, ((), σ') ∈ (archPostResetUntyped (Z := Unit) false sb.val σ').1) + (hfi0 : fi ≠ 0#u64) (hsm : sb.val < 16) (hchunk : resetChunkBits = 16) : + ∃ s2 σ2, untyped.reset_untyped st ar s a x = ok (core.result.Result.Ok (), s2, a) ∧ + (.ok (), σ2) ∈ ((resetUntypedCap (Z := Unit) cte).run σ).1 ∧ + JStage3A st AR (dropZA (utDA p sb.val) Z) (dropHA (utDA p sb.val) H) (dropSetA (utDA p sb.val) OS) U Ac + (dropSetA (utDA p sb.val) Pp) Tb Lv ia ie (dropSetA (utDA p sb.val) N) (dropSetA (utDA p sb.val) E) + (dropSetA (utDA p sb.val) Rp) (utBfOffA (utDA p sb.val) (dropBfA (utDA p sb.val) Bf)) stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s2 a σ2 (dropMA (utDA p sb.val) m) ∧ + SymL st (dropHA (utDA p sb.val) H) Z.Cs s2 ∧ capA st s2 x = .Untyped p sb 0#u64 false := by + have hsal := J.bufAl + have zb := J.zombieBnd + have hcx : capA st s x = .Untyped p sb fi false := okinj ((capA_eq s x).symm.trans hcap) + obtain ⟨J2, Y1, Cl, hslots, hth', hx', hheap, hlink, horig, hz, hsp, hzn, Rq', Nt', P'⟩ := + utReset_startA J Dp P hcap he symL othT othC hn cte hq hth hxc + have hx : x ∈ OS := P.src + obtain ⟨hpal, h47, hfi⟩ := J.sys.sysl.sys.ur.utv J.sys.sysl.sys.stz.1.1 hx hcx + have h4 : 4 ≤ sb.val := J.sys.sysl.sys.ur.ge4 hx hcx + have hsb : sb.val < 64 := by omega + have hpfit : p.val + 2 ^ sb.val ≤ 2 ^ 64 := al_fit hpal (by omega) p.bv.isLt + have h2pow : 2 ^ sb.val < 2 ^ 64 := Nat.pow_lt_pow_right (by decide) hsb + -- FL4's length + obtain ⟨len, hlen, hlv⟩ : ∃ len : U64, (1#u64 <<< (sb &&& 63#u64) : Result U64) = ok len ∧ len.val = 2 ^ sb.val := by + have h63 : (sb &&& 63#u64).val = sb.val := by + rw [UScalar.val_and, show (63#u64 : U64).val = 2 ^ 6 - 1 from rfl, Nat.and_two_pow_sub_one_eq_mod] + exact Nat.mod_eq_of_lt (by omega) + have : (1#u64 <<< (sb &&& 63#u64) : Result U64) ⦃ z => z.val = 2 ^ sb.val ⦄ := by + step as ⟨z, hz1, hz2⟩ + rw [h63] at hz1 + simpa [Nat.shiftLeft_eq, U64.size_def, U64.numBits, Nat.mod_eq_of_lt h2pow] using hz1 + exact spec_exists_dut this + have h8 : 8 ∣ 2 ^ sb.val := by + have : 2 ^ 3 ∣ 2 ^ sb.val := Nat.pow_dvd_pow 2 (by omega) + simpa using this + have hal : Aligned p := by + show p.val % 8 = 0 + exact Nat.mod_eq_zero_of_dvd (h8.trans (Nat.dvd_of_mod_eq_zero hpal)) + have e0 : (0#u64 : U64).val = 0 := rfl + have step := utChunk_stepA (c := p) (off := 0#u64) (len := len) J2 (p := p) (sb := sb) (i := fi) (x := x) Cl P' Y1 + Dp.run hcx he cte hq hth' hxc hslots hal (by rw [e0]; omega) (by rw [hlv]; exact Nat.mod_eq_zero_of_dvd h8) + (by rw [e0, hlv]; omega) (by rw [e0, hlv]; omega) hheap hlink horig hz + obtain ⟨s2, σ2, hset, hsc, J3, Y3, -, -, -, hc2, -⟩ := step + have hsbchunk : sb < untyped.RESET_CHUNK_BITS := by + unfold untyped.RESET_CHUNK_BITS + show sb.val < 16 + exact hsm + refine ⟨s2, σ2, ut_reset_smallA ar hcx hfi0 hsbchunk hlen hset, ?_, J3, Y3, hc2⟩ + -- the spec + have hab : AbsSlotA Z.th cte := ⟨qc, hq, Or.inl hth⟩ + obtain ⟨ac, hg, hr⟩ := J.j.1.st.getAbsA hab + have eac : ac = .untypedCap false (specPtr p) sb.val fi.val := by + have h := hr.2 + rw [hxc, show capAtA (L.mem s) x = capA st s x from rfl, hcx] at h + exact (show absCapA (.Untyped p sb fi false) = ac from h).symm + rw [eac] at hg + have hal8 : (specPtr p).toNat % 8 = 0 := by + have := specPtr_mod p (n := 3) (by omega) + have hp8 : p.val % 8 = 0 := hal + simpa [show 2 ^ 3 = 8 by decide, hp8] using this + have hD := ut_delete_exA (sz := sb.val) hal8 (by omega) hsb hsp σ + obtain ⟨ms', hC⟩ := ut_clear_exA hal8 (2 ^ sb.val) (utDelStA (specPtr p) sb.val σ) + have eC := clear_id hal8 (Nat.mod_eq_zero_of_dvd h8) (Nat.two_pow_pos _) hsp hzn _ hC + simp only at eC + rw [eC] at hC + refine ut_reset_small_memA (σD := utDelStA (specPtr p) sb.val σ) (σC := utDelStA (specPtr p) sb.val σ) + (σS := σ2) (dev := false) (pu := specPtr p) (sz := sb.val) (fi := fi.val) hg ?_ (Or.inr (by rw [hchunk]; exact hsm)) hD ?_ hsc (hPost σ2) + · intro h + exact hfi0 (UScalar.eq_of_val_eq (by rw [h]; rfl)) + · unfold NonDet.unless + simp only [Bool.false_eq_true, ↓reduceIte] + exact hC + +/-! ## The chunked case -/ + +/-- FL4's `set_cap` of the untyped keeps the state block's base. -/ +theorem ut_setCap_ksbA {s1 s2 : S} {x p sb off : U64} {d : Bool} (hw : CapRt.EncWf (.Untyped p sb off d)) + (h : obj.Slot.set_cap st x s1 (.Untyped p sb off d) = ok s2) : L.ksb s2 = L.ksb s1 := by + obtain ⟨w0, w1, hen, -⟩ := CapRt.decode_encode _ hw + have e : s2 = L.wr (L.wr s1 x w0) (wa x 8#u64) w1 := okinj (h.symm.trans (set_cap_eq hen)) + rw [e, L.ksb_wr, L.ksb_wr] + +/-- `set_cap` keeps the spec machine state. -/ +theorem ut_setCap_msA {cap : Cap} {sl : CslotPtr} {σ : State Unit} {r : Unit × State Unit} + (hr : r ∈ (setCap (Z := Unit) cap sl σ).1) : r.2.machineState = σ.machineState := by + obtain ⟨oref, cref⟩ := sl + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + obtain ⟨e1, -⟩ := getObject_mem hx + obtain ⟨obj, σ1⟩ := x + simp only at e1 hr + subst e1 + have key : ∀ o : KernelObject, r ∈ ((Pure.pure o >>= fun o' => setObject (Z := Unit) oref o') σ1).1 → + r.2.machineState = σ1.machineState := by + intro o h + obtain ⟨y, hy, h⟩ := mem_bindA.1 h + obtain rfl : y = (o, σ1) := hy + obtain ⟨o0, -, hr2⟩ := setObject_mem h + rw [hr2] + have kfail : ∀ f : KernelObject → NonDet (State Unit) Unit, r ∈ ((NonDet.fail >>= f) σ1).1 → False := by + intro f h + obtain ⟨y, hy, -⟩ := mem_bindA.1 h + exact absurd hy (Set.notMem_empty _) + cases obj with + | cnode sz cn => + simp only at hr + split_ifs at hr + · exact key _ hr + · exact (kfail _ hr).elim + | tcb t => + simp only at hr + split_ifs at hr <;> + first + | exact (kfail _ hr).elim + | exact key _ hr + | _ => + try simp only at hr + exact (kfail _ hr).elim + +theorem utDelSt_actA (σ : State Unit) (pu : Fl4SpecA64.Word) (sz : Nat) : + (utDelStA pu sz σ).schedulerAction = σ.schedulerAction := rfl + +/-- F1–F2 across the reset's `delete_objects`, given that the current thread survives it (`hc`, from the record at +the dropped sets). -/ +theorem utDelSt_sactA {σ : State Unit} {pu : Fl4SpecA64.Word} {sz : Nat} (h : SActA σ) + (hc : ∃ y, (utDelStA pu sz σ).kheap (utDelStA pu sz σ).curThread = some (.tcb y)) : + SActA (utDelStA pu sz σ) := by + have kept : ∀ (D : Set ObjRef) (τ : State Unit) q o, (detype D τ).kheap q = some o → τ.kheap q = some o := by + intro D τ q o g + change (if q ∈ D then none else τ.kheap q) = some o at g + split at g + case isTrue => cases g + case isFalse => exact g + obtain ⟨y, hy⟩ := hc + have back : σ.kheap σ.curThread = some (KernelObject.tcb y) := kept _ _ _ _ hy + obtain ⟨z, hz, hs⟩ := h.f2 + rw [hz] at back + cases back + exact ⟨fun u hu => h.f1 u hu, y, hy, hs⟩ + +/-- **The clauses `JStage3TA` does not carry** (record edit #4: `vcSym1`, `vcSym2`, `blkQ`) after a step that keeps +the spec heap: the record before and after tie the same spec fields to the same words. -/ +theorem utTA_extraA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} + {J5 J7 : JSlotA S} {s s' : S} {a a' : A} {σ σ' : State Unit} {m m' : McState} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (J' : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s' a' σ' m') + (kh : σ'.kheap = σ.kheap) : + VcSym1A st Z.T s' ∧ VcSym2A st Z.V s' ∧ BlkQueuedA Z.T E N (L.mem s') σ' := by + have inj : ∀ x y : U64, optRefA x = optRefA y → x = y := by + intro x y h + unfold optRefA at h + split_ifs at h with h1 h2 h2 + · rw [h1, h2] + · exact specPtr_inj (Option.some.inj h) + have same : ∀ q o o', σ.kheap q = some o → σ'.kheap q = some o' → o = o' := by + intro q o o' h h' + rw [kh, h] at h' + exact Option.some.inj h' + have vobj : ∀ v ∈ Z.V, L.mem s' v = L.mem s v ∧ L.mem s' (wa v 24#u64) = L.mem s (wa v 24#u64) := by + intro v hv + obtain ⟨x, hx, h1, h2⟩ := J.j.2.1.obj v hv + obtain ⟨y, hy, g1, g2⟩ := J'.j.2.1.obj v hv + have e := same _ _ _ hx hy + injection e with e + injection e with e + subst e + exact ⟨inj _ _ (g1.symm.trans h1), inj _ _ (g2.symm.trans h2)⟩ + have tobj : ∀ t ∈ Z.T, L.mem s' (wa t 3664#u64) = L.mem s (wa t 3664#u64) ∧ + L.mem s' (runVcpuA t) = L.mem s (runVcpuA t) := by + intro t ht + obtain ⟨x, hx, h1, h2⟩ := J.j.2.1.tcb t ht + obtain ⟨y, hy, g1, g2⟩ := J'.j.2.1.tcb t ht + have e := same _ _ _ hx hy + injection e with e + subst e + exact ⟨inj _ _ (g1.symm.trans h1), inj _ _ (g2.symm.trans h2)⟩ + refine ⟨J.vcSym1.keepA J.vcRun (fun t ht => (tobj t ht).1) (fun v hv => (vobj v hv).1), + J.vcSym2.keepA J.vcRun (fun v hv => (vobj v hv).2) (fun t ht => (tobj t ht).2), ?_, ?_⟩ + · intro t ht x hx e hb + have hx0 : σ.kheap (specPtr t) = some (.tcb x) := by rw [← kh]; exact hx + obtain ⟨a0, y, hy, mem⟩ := J.blkQ.ep t ht x hx0 e hb + obtain ⟨x1, h1, r1⟩ := J.ep.tcb t ht + obtain ⟨x2, h2, r2⟩ := J'.ep.tcb t ht + rw [hx0] at h1 + rw [hx] at h2 + injection h1 with h1 + injection h1 with h1 + injection h2 with h2 + injection h2 with h2 + subst h1 h2 + have bw : L.mem s' (Notif.tBlockA t) = L.mem s (Notif.tBlockA t) := + specPtr_inj ((r2.blk e hb).trans (r1.blk e hb).symm) + exact ⟨by rw [bw]; exact a0, y, by rw [kh]; exact hy, mem⟩ + · intro t ht x hx q hb + have hx0 : σ.kheap (specPtr t) = some (.tcb x) := by rw [← kh]; exact hx + obtain ⟨a0, y, l, hy, ho, mem⟩ := J.blkQ.ntfn t ht x hx0 q hb + obtain ⟨x1, h1, r1⟩ := J.ntfn.tcb t ht + obtain ⟨x2, h2, r2⟩ := J'.ntfn.tcb t ht + rw [hx0] at h1 + rw [hx] at h2 + injection h1 with h1 + injection h1 with h1 + injection h2 with h2 + injection h2 with h2 + subst h1 h2 + have bw : L.mem s' (Notif.tBlockA t) = L.mem s (Notif.tBlockA t) := + specPtr_inj ((r2.blk q hb).trans (r1.blk q hb).symm) + exact ⟨by rw [bw]; exact a0, y, l, by rw [kh]; exact hy, ho, mem⟩ + +/-- **The reset loop's invariant** at free index `i`: the record on the dropped sets and what a chunk step needs. -/ +structure UtRstInvA (st : machine.Store S) [L : LawfulStore st] (AR : A → HwRootA → Prop) (Z : XSetsA) (H : Heap) + (OS U Ac Pp Tb : Set U64) (Lv : U64 → Nat) (ia ie : Nat) (N E Rp : Set U64) (Bf : Set (U64 × Word)) + (stack : U64 → List U64) (κ : U64 → U64) (n : Nat) (nA : U64) (m : McState) (p sb x : U64) + (Rq Nt : Set U64) (ex : Aeneas.Std.Array obj.Slot 3#usize) (s : S) (a : A) (σ : State Unit) (i : U64) : + Prop where + rec_ : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m + /-- (F1–F2) the pending switch and the current thread's state. -/ + sact : SActA σ + /-- no thread switch is pending (from the decode site's action word `0`). -/ + noSwitch : ∀ u, σ.schedulerAction ≠ .switchThread u + symL : SymL st H Z.Cs s + clr : UtClrA st Z H s p.val (p.val + 2 ^ sb.val) + place : SysPlace st H Z.Cs H.frame Rq Nt OS s ex x + run : Runnable (L.mem s (stA (L.curW s))) + cap : capA st s x = .Untyped p sb i false + noch : cdt.ensure_no_children st s x = ok (.Ok ()) + heap : ∀ q : U64, p.val ≤ q.val → q.val < p.val + 2 ^ sb.val → σ.kheap (specPtr q) = none + link : ∀ a' c', σ.cdt a' = some c' → ∀ q : U64, p.val ≤ q.val → q.val < p.val + 2 ^ sb.val → + a'.1 ≠ specPtr q ∧ c'.1 ≠ specPtr q + orig : ∀ a', σ.isOriginalCap a' = true → ∀ q : U64, p.val ≤ q.val → q.val < p.val + 2 ^ sb.val → a'.1 ≠ specPtr q + zero : ∀ w : Word, p.val ≤ (fl4Ptr w).val → (fl4Ptr w).val < p.val + 2 ^ sb.val → + σ.machineState.underlyingMemory w = 0 + +set_option maxHeartbeats 2000000 in +set_option maxRecDepth 100000 in +/-- **The chunk loop** from a state satisfying the invariant (module docstring): `Ok` with the spec's chunks all `Ok`, +or the preemption point's error with the spec's matching error. -/ +theorem utReset_loopA [Fact (n ≤ Fl4SpecA64.fl4A64Platform.numNodes)] (ar : machine.Arch A) {p sb x : U64} {Rq Nt : Set U64} + {ex : Aeneas.Std.Array obj.Slot 3#usize} {cte : CslotPtr} {qc : U64} + (hq : cte.1 = specPtr qc) (hth : Z.th qc = some (.cnode cte.2.length)) (hxc : cslotAddrA cte = x) + (hslots : ∀ y ∈ OS, y.val + 32 ≤ p.val ∨ p.val + 2 ^ sb.val ≤ y.val) + (hpal : p.val % 2 ^ sb.val = 0) (hsb16 : 16 ≤ sb.val) (hsb47 : sb.val ≤ 47) + (hsp : (specPtr p).toNat + 2 ^ sb.val ≤ 2 ^ 64) (hchunk : resetChunkBits = 16) + (Rpre : U64 → S → A → State Unit → Prop) + (T : A → State Unit → Prop) + (hT : ∀ (a : A) (σ σ' : State Unit), T a σ → UtCnStepA σ σ' → σ'.machineState = σ.machineState → T a σ') + (hpp : ∀ off : U64, off ≠ 0#u64 → PPHypUtA st ar OS + (fun s a σ => UtRstInvA (ar := ar) st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n nA m p sb x Rq Nt ex s a σ off ∧ + T a σ) + (Rpre off)) + (σD : State Unit) (k : Nat) {s : S} {a : A} {σ : State Unit} {off i : U64} + (I : UtRstInvA (ar := ar) st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n nA m p sb x Rq Nt ex s a σ i) + (hoff : off.val % 65536 = 0) (hon : off.val + 65536 ≤ 2 ^ sb.val) (hil : i.val ≤ off.val + 65536) + (hT0 : T a σ) + (hspec : ∀ y, y ∈ ((NonDetE.mapMEx (utChunkA false (specPtr p) sb.val cte) + (List.range (off.val / 65536 + 1)).reverse).run σ).1 → + y ∈ ((NonDetE.mapMEx (utChunkA false (specPtr p) sb.val cte) (List.range (k + 1)).reverse).run σD).1) : + untyped.reset_untyped_loop st ar s a x p sb false off ⦃ r => + (r.1 = .Ok () ∧ ∃ σ', (.ok (), σ') ∈ ((NonDetE.mapMEx (utChunkA false (specPtr p) sb.val cte) + (List.range (k + 1)).reverse).run σD).1 ∧ + UtRstInvA (ar := ar) st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n nA m p sb x Rq Nt ex r.2.1 r.2.2 σ' 0#u64 ∧ + T r.2.2 σ') ∨ + (r.1 = .Err .Preempted ∧ ∃ σ' off', (.error (), σ') ∈ ((NonDetE.mapMEx (utChunkA false (specPtr p) sb.val cte) + (List.range (k + 1)).reverse).run σD).1 ∧ Rpre off' r.2.1 r.2.2 σ') ⦄ := by + have hsb : sb.val < 64 := by omega + have hpfit : p.val + 2 ^ sb.val ≤ 2 ^ 64 := al_fit hpal (by omega) p.bv.isLt + have hD : ∀ z : U64, specPtr z ∈ utDA p sb.val ↔ p.val ≤ z.val ∧ z.val < p.val + 2 ^ sb.val := + utD_memA hsb hpfit hsp + have h8p : 8 ∣ p.val := by + have : 8 ∣ 2 ^ sb.val := by + have : 2 ^ 3 ∣ 2 ^ sb.val := Nat.pow_dvd_pow 2 (by omega) + simpa using this + exact this.trans (Nat.dvd_of_mod_eq_zero hpal) + have e64k : (untyped.RESET_CHUNK : U64).val = 65536 := by unfold untyped.RESET_CHUNK; rfl + unfold untyped.reset_untyped_loop + apply loop.spec_decr_nat (measure := fun y : S × A × U64 => y.2.2.val) + (inv := fun y => y.2.2.val % 65536 = 0 ∧ y.2.2.val + 65536 ≤ 2 ^ sb.val ∧ ∃ (σ0 : State Unit) (i0 : U64), + UtRstInvA (ar := ar) st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n nA m p sb x Rq Nt ex y.1 y.2.1 σ0 i0 ∧ + i0.val ≤ y.2.2.val + 65536 ∧ T y.2.1 σ0 ∧ + ∀ z, z ∈ ((NonDetE.mapMEx (utChunkA false (specPtr p) sb.val cte) + (List.range (y.2.2.val / 65536 + 1)).reverse).run σ0).1 → + z ∈ ((NonDetE.mapMEx (utChunkA false (specPtr p) sb.val cte) (List.range (k + 1)).reverse).run σD).1) + · rintro ⟨s1, a1, o⟩ ⟨ho, hoN, σ0, i0, I0, hi0, T0, hsp0⟩ + simp only at ho hoN I0 hi0 T0 hsp0 + dsimp only + unfold untyped.reset_untyped_loop.body + simp only [lift, bind_ok] + refine WP.spec_bind (LawfulStore.clear_free_spec s1 _ _) fun s3 e3 => ?_ + subst e3 + -- one chunk + have hcv : (wa p o).val = p.val + o.val := by + rw [wa_valP]; exact Nat.mod_eq_of_lt (by omega) + have hcal : Aligned (wa p o) := by + show (wa p o).val % 8 = 0 + rw [hcv]; omega + have hEnc : CapRt.EncWf (.Untyped p sb o false) := by + have w := CapRt.decodeF_encWf (L.mem s1 x).bv (L.mem s1 (wa x 8#u64)).bv + have e' : CapRt.decodeF (L.mem s1 x).bv (L.mem s1 (wa x 8#u64)).bv = .Untyped p sb i0 false := I0.cap + rw [e'] at w; exact w + obtain ⟨s2, σ2, hset, hsc, J2, Y2, Cl2, P2, run2, cap2, noch2, H2, cdt2, or2, heap2⟩ := + utChunk_stepA (c := wa p o) (off := o) (len := untyped.RESET_CHUNK) I0.rec_ (p := p) (sb := sb) (i := i0) + (x := x) I0.clr I0.place I0.symL I0.run I0.cap I0.noch cte hq hth hxc hslots hcal hcv + (by rw [e64k]) (by rw [e64k]; omega) (by rw [e64k]; omega) I0.heap I0.link I0.orig I0.zero + have ek : L.ksb s2 = L.ksb s1 := (ut_setCap_ksbA hEnc hset).trans (L.ksb_zr _ _ _) + have K : UtRstInvA (ar := ar) st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n nA m p sb x Rq Nt ex s2 a1 σ2 o := + { rec_ := J2, sact := utCn_sactA I0.sact H2, + noSwitch := fun u => by rw [H2.act]; exact I0.noSwitch u, symL := Y2, clr := Cl2, place := P2, run := run2, cap := cap2, noch := noch2 + heap := heap2 + link := by rw [cdt2]; exact I0.link + orig := by rw [or2]; exact I0.orig + zero := fun w h1 h2 => by rw [H2.um]; exact I0.zero w h1 h2 } + -- the spec's clear is the identity + have hshift : (specPtr p + (BitVec.ofNat 64 (o.val / 65536) <<< resetChunkBits)).toNat = + (specPtr p).toNat + o.val := by + have e1 : (BitVec.ofNat 64 (o.val / 65536) <<< resetChunkBits).toNat = o.val := by + rw [hchunk, BitVec.toNat_shiftLeft, BitVec.toNat_ofNat, Nat.shiftLeft_eq, + Nat.mod_eq_of_lt (show o.val / 65536 < 2 ^ 64 by omega)] + have : o.val / 65536 * 2 ^ 16 = o.val := by omega + rw [this] + exact Nat.mod_eq_of_lt (by omega) + rw [BitVec.toNat_add, e1] + have : 0 < 2 ^ sb.val := Nat.two_pow_pos _ + exact Nat.mod_eq_of_lt (by omega) + have hal8 : (specPtr p + (BitVec.ofNat 64 (o.val / 65536) <<< resetChunkBits)).toNat % 8 = 0 := by + rw [hshift] + have := specPtr_mod p (n := 3) (by omega) + have : (specPtr p).toNat % 8 = 0 := by + simpa [show 2 ^ 3 = 8 by decide, Nat.mod_eq_zero_of_dvd h8p] using this + omega + have h2c : (2 : Nat) ^ resetChunkBits = 65536 := by rw [hchunk]; rfl + have htop : (specPtr p + 2 ^ sb.val - 1 : Word).toNat = (specPtr p).toNat + 2 ^ sb.val - 1 := by + rw [pow2_word hsb] + exact toNat_add_n_sub1 (Nat.two_pow_pos _) hsp + have hzc : ∀ w : Word, (specPtr p + (BitVec.ofNat 64 (o.val / 65536) <<< resetChunkBits)).toNat ≤ w.toNat → + w.toNat < (specPtr p + (BitVec.ofNat 64 (o.val / 65536) <<< resetChunkBits)).toNat + 2 ^ resetChunkBits → + σ0.machineState.underlyingMemory w = 0 := by + intro w h1 h2 + rw [hshift] at h1 h2 + rw [h2c] at h2 + have mw : w ∈ utDA p sb.val := by + refine ⟨?_, ?_⟩ + · rw [BitVec.le_def]; omega + · rw [BitVec.le_def, htop]; omega + rw [← specPtr_fl4Ptr w] at mw + have g := (hD _).1 mw + exact I0.zero w g.1 g.2 + have hfitc : (specPtr p + (BitVec.ofNat 64 (o.val / 65536) <<< resetChunkBits)).toNat + 2 ^ resetChunkBits ≤ + 2 ^ 64 := by rw [hshift, h2c]; omega + obtain ⟨ms', hC⟩ := ut_clear_exA hal8 (2 ^ resetChunkBits) σ0 + have eC := clear_id hal8 (by rw [h2c]) (by rw [h2c]; omega) hfitc hzc _ hC + simp only at eC + rw [eC] at hC + have hS : ((), σ2) ∈ (setCap (Z := Unit) (.untypedCap false (specPtr p) sb.val + (o.val / 65536 * 2 ^ resetChunkBits)) cte σ0).1 := by + have e : o.val / 65536 * 2 ^ resetChunkBits = o.val := by rw [h2c]; omega + rw [e] + exact hsc + have TK : T a1 σ2 := hT a1 σ0 σ2 T0 H2 (ut_setCap_msA hsc) + have erng : (List.range (o.val / 65536 + 1)).reverse = + (o.val / 65536) :: (List.range (o.val / 65536)).reverse := by + rw [List.range_succ, List.reverse_append] + rfl + refine WP.spec_bind (show obj.Slot.set_cap st x (L.zr s1 (wa p o) untyped.RESET_CHUNK) + (.Untyped p sb o false) ⦃ s' => s' = s2 ⦄ by rw [hset]; exact (WP.spec_ok _).2 rfl) fun s4 e4 => ?_ + subst e4 + split + · rename_i h0 + have hpre0 := pp_spec_shortA σ2 + refine (WP.spec_ok _).2 (Or.inl ⟨rfl, σ2, hsp0 _ ?_, h0 ▸ K, TK⟩) + have e0 : o.val / 65536 = 0 := by rw [h0]; rfl + rw [erng] + refine ut_chunks_okA (ut_chunk_memA (i := o.val / 65536) hC hS hpre0) ?_ + rw [e0] + exact ut_chunks_nilA false (specPtr p) sb.val cte σ2 + rename_i hne + have ecap : obj.Slot.cap st x s4 = ok (.Untyped p sb o false) := ut_slot_capA cap2 + rw [ecap, bind_ok] + unfold untyped.as_untyped + simp only [bind_ok, core.option.Option.is_none, Option.isNone, Bool.false_eq_true, ↓reduceIte] + refine WP.spec_bind (hpp o hne s4 a1 σ2 ⟨K, TK⟩) ?_ + rintro ⟨r, s5, a5⟩ ⟨-, σ3, h⟩ + simp only at h + try simp only [uncurry_apply_pair] + rcases h with ⟨hr, hpre, K3, T3⟩ | ⟨hr, hpre, K3⟩ + · subst hr + have hov : 65536 ≤ o.val := by + have : o.val ≠ 0 := fun h => hne (UScalar.eq_of_val_eq (by rw [h]; rfl)) + omega + have hnext : (core.num.U64.saturating_sub o untyped.RESET_CHUNK).val = o.val - 65536 := by + simp only [Fl4ProofsA64.P0.saturating_sub_u64_val, e64k] + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok] + refine (WP.spec_ok _).2 ?_ + refine ⟨⟨by rw [hnext]; omega, by rw [hnext]; omega, σ3, o, K3, by rw [hnext]; omega, T3, fun z hz => ?_⟩, + by simp only [hnext]; omega⟩ + refine hsp0 z ?_ + rw [erng] + refine ut_chunks_okA (ut_chunk_memA (i := o.val / 65536) hC hS hpre) ?_ + have e : (core.num.U64.saturating_sub o untyped.RESET_CHUNK).val / 65536 + 1 = o.val / 65536 := by + rw [hnext]; omega + simp only at hz + rwa [e] at hz + · subst hr + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame.from, + bind_tc_ok] + refine (WP.spec_ok _).2 (Or.inr ⟨rfl, σ3, o, hsp0 _ ?_, K3⟩) + rw [erng] + exact ut_chunks_errA (ut_chunk_memA (i := o.val / 65536) hC hS hpre) + · exact ⟨hoff, hon, σ, i, I, hil, hT0, hspec⟩ + +set_option maxHeartbeats 2000000 in +set_option maxRecDepth 100000 in +/-- **A used RAM untyped of at least a chunk** (module docstring). -/ +theorem utReset_bigA [Fact (n ≤ Fl4SpecA64.fl4A64Platform.numNodes)] (ar : machine.Arch A) + (sact : SActA σ) + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {p sb fi : U64} {Ud Acd Rq Nt : Set U64} {ex : Aeneas.Std.Array obj.Slot 3#usize} {x : U64} + (Dp : DecPre st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ud Acd s (.Untyped p sb fi false) ex) + (P : SysPlace st H Z.Cs H.frame Rq Nt OS s ex x) + (hcap : obj.Slot.cap st x s = ok (.Untyped p sb fi false)) + (he : cdt.ensure_no_children st s x = ok (.Ok ())) + (symL : SymL st H Z.Cs s) + (othT : ∀ c ∈ Z.Cs, c ≠ L.cpu s → specPtr (L.mem s (L.nodeAt s c layout.node.CUR_THREAD)) ∉ utDA p sb.val) + (othC : ∀ c ∈ Z.Cs, c ≠ L.cpu s → specPtr (L.mem s (L.nodeAt s c layout.node.CUR_SC)) ∉ utDA p sb.val) + (hn : nA.val = fl4A64Platform.s2IpaBits) + (cte : CslotPtr) {qc : U64} (hq : cte.1 = specPtr qc) (hth : Z.th qc = some (.cnode cte.2.length)) + (hxc : cslotAddrA cte = x) + (hPost : ∀ σ' : State Unit, ((), σ') ∈ (archPostResetUntyped (Z := Unit) false sb.val σ').1) + (Rpre : U64 → S → A → State Unit → Prop) + (T : A → State Unit → Prop) + (hT : ∀ (a : A) (σ σ' : State Unit), T a σ → UtCnStepA σ σ' → σ'.machineState = σ.machineState → T a σ') + (hT0 : T a (utDelStA (specPtr p) sb.val σ)) + (hpp : ∀ (Rq' Nt' : Set U64) (off : U64), off ≠ 0#u64 → PPHypUtA st ar (dropSetA (utDA p sb.val) OS) + (fun s a σ => UtRstInvA (ar := ar) st AR (dropZA (utDA p sb.val) Z) (dropHA (utDA p sb.val) H) + (dropSetA (utDA p sb.val) OS) U Ac (dropSetA (utDA p sb.val) Pp) Tb Lv ia ie (dropSetA (utDA p sb.val) N) + (dropSetA (utDA p sb.val) E) (dropSetA (utDA p sb.val) Rp) + (utBfOffA (utDA p sb.val) (dropBfA (utDA p sb.val) Bf)) stack κ n nA (dropMA (utDA p sb.val) m) p sb x Rq' Nt' + (Array.repeat 3#usize 0#u64) s a σ off ∧ T a σ) + (Rpre off)) + (hfi0 : fi ≠ 0#u64) (hbig : 16 ≤ sb.val) (hchunk : resetChunkBits = 16) : + untyped.reset_untyped st ar s a x ⦃ r => + (r.1 = .Ok () ∧ ∃ (σ' : State Unit) (Rq' Nt' : Set U64), (.ok (), σ') ∈ ((resetUntypedCap (Z := Unit) cte).run σ).1 ∧ + UtRstInvA (ar := ar) st AR (dropZA (utDA p sb.val) Z) (dropHA (utDA p sb.val) H) + (dropSetA (utDA p sb.val) OS) U Ac (dropSetA (utDA p sb.val) Pp) Tb Lv ia ie (dropSetA (utDA p sb.val) N) + (dropSetA (utDA p sb.val) E) (dropSetA (utDA p sb.val) Rp) + (utBfOffA (utDA p sb.val) (dropBfA (utDA p sb.val) Bf)) stack κ n nA (dropMA (utDA p sb.val) m) p sb x Rq' + Nt' (Array.repeat 3#usize 0#u64) r.2.1 r.2.2 σ' 0#u64 ∧ T r.2.2 σ') ∨ + (r.1 = .Err .Preempted ∧ ∃ σ' off', (.error (), σ') ∈ ((resetUntypedCap (Z := Unit) cte).run σ).1 ∧ + Rpre off' r.2.1 r.2.2 σ') ⦄ := by + have hsal := J.bufAl + have zb := J.zombieBnd + have hcx : capA st s x = .Untyped p sb fi false := okinj ((capA_eq s x).symm.trans hcap) + obtain ⟨J2, Y1, Cl, hslots, hth', hx', hheap, hlink, horig, hz, hsp, -, Rq', Nt', P'⟩ := + utReset_startA J Dp P hcap he symL othT othC hn cte hq hth hxc + have hx : x ∈ OS := P.src + obtain ⟨hpal, h47, hfi⟩ := J.sys.sysl.sys.ur.utv J.sys.sysl.sys.stz.1.1 hx hcx + have hsb : sb.val < 64 := by omega + have sactD : SActA (utDelStA (specPtr p) sb.val σ) := utDelSt_sactA sact J2.sinv.curTcb + have I : UtRstInvA (ar := ar) st AR (dropZA (utDA p sb.val) Z) (dropHA (utDA p sb.val) H) + (dropSetA (utDA p sb.val) OS) U Ac (dropSetA (utDA p sb.val) Pp) Tb Lv ia ie (dropSetA (utDA p sb.val) N) + (dropSetA (utDA p sb.val) E) (dropSetA (utDA p sb.val) Rp) + (utBfOffA (utDA p sb.val) (dropBfA (utDA p sb.val) Bf)) stack κ n nA (dropMA (utDA p sb.val) m) p sb x Rq' Nt' + (Array.repeat 3#usize 0#u64) s a (utDelStA (specPtr p) sb.val σ) fi := + { rec_ := J2, sact := sactD, + noSwitch := fun u => by + rw [utDelSt_actA, J.j.1.sch.act, Dp.act] + simp [actOfA], + symL := Y1, clr := Cl, place := P', run := Dp.run, cap := hcx, noch := he, heap := hheap + link := hlink, orig := horig, zero := hz } + -- FL4's first offset + have hfipos : 0 < fi.val := by + rcases Nat.eq_zero_or_pos fi.val with h | h + · exact absurd (UScalar.eq_of_val_eq (by rw [h]; rfl)) hfi0 + · exact h + have e1 : (1#u64 : U64).val = 1 := rfl + obtain ⟨i1, hi1, hi1v⟩ : ∃ i : U64, (fi - 1#u64 : Result U64) = ok i ∧ i.val = fi.val - 1 := by + have : (fi - 1#u64 : Result U64) ⦃ z => z.val = fi.val - 1 ⦄ := by + step as ⟨z, hz'⟩ + omega + exact spec_exists_dut this + have h65535 : (untyped.RESET_CHUNK - 1#u64 : Result U64) = ok 65535#u64 := by + have : (untyped.RESET_CHUNK - 1#u64 : Result U64) ⦃ z => z = 65535#u64 ⦄ := by + unfold untyped.RESET_CHUNK + step as ⟨z, hz'⟩ + apply UScalar.eq_of_val_eq + have : (65535#u64 : U64).val = 65535 := rfl + have : (65536#u64 : U64).val = 65536 := rfl + omega + obtain ⟨z, hz1, hz2⟩ := spec_exists_dut this + rw [hz1, hz2] + have hoffv : (i1 &&& ~~~65535#u64).val = (fi.val - 1) / 65536 * 65536 := by + rw [mask_chunk_div, hi1v] + have hsbn : ¬ sb < untyped.RESET_CHUNK_BITS := by + unfold untyped.RESET_CHUNK_BITS + show ¬ sb.val < 16 + omega + rw [ut_reset_chunkedA ar hcx hfi0 hsbn hi1 h65535] + have hdvd : 65536 ∣ 2 ^ sb.val := by + have : 2 ^ 16 ∣ 2 ^ sb.val := Nat.pow_dvd_pow 2 hbig + simpa using this + have hon : (i1 &&& ~~~65535#u64).val + 65536 ≤ 2 ^ sb.val := by + rw [hoffv] + obtain ⟨c, hc⟩ := hdvd + omega + have ek : (i1 &&& ~~~65535#u64).val / 65536 = (fi.val - 1) / 65536 := by rw [hoffv]; omega + have loop := utReset_loopA (st := st) (ar := ar) (x := x) (cte := cte) (qc := qc) hq hth' hxc hslots hpal hbig + (by omega) hsp hchunk Rpre T hT (hpp Rq' Nt') (utDelStA (specPtr p) sb.val σ) ((fi.val - 1) / 65536) I + (off := i1 &&& ~~~65535#u64) (by rw [hoffv]; omega) hon (by rw [hoffv]; omega) hT0 + (fun y hy => by rw [ek] at hy; exact hy) + refine WP.spec_mono loop fun r h => ?_ + -- the spec + have hab : AbsSlotA Z.th cte := ⟨qc, hq, Or.inl hth⟩ + obtain ⟨ac, hg, hr⟩ := J.j.1.st.getAbsA hab + have eac : ac = .untypedCap false (specPtr p) sb.val fi.val := by + have h := hr.2 + rw [hxc, show capAtA (L.mem s) x = capA st s x from rfl, hcx] at h + exact (show absCapA (.Untyped p sb fi false) = ac from h).symm + rw [eac] at hg + have h4 : 4 ≤ sb.val := by omega + have hal8 : (specPtr p).toNat % 8 = 0 := by + have h8 : 8 ∣ p.val := by + have : 2 ^ 3 ∣ 2 ^ sb.val := Nat.pow_dvd_pow 2 (by omega) + exact (show (8 : Nat) ∣ 2 ^ sb.val by simpa using this).trans (Nat.dvd_of_mod_eq_zero hpal) + have := specPtr_mod p (n := 3) (by omega) + simpa [show 2 ^ 3 = 8 by decide, Nat.mod_eq_zero_of_dvd h8] using this + have hD := ut_delete_exA (sz := sb.val) hal8 (by omega) hsb hsp σ + have eidx : (fi.val - 1) / 2 ^ resetChunkBits + 1 = (fi.val - 1) / 65536 + 1 := by + rw [hchunk, show (2 : Nat) ^ 16 = 65536 by norm_num] + have hbig' : resetChunkBits ≤ sb.val := by rw [hchunk]; exact hbig + rcases h with ⟨hok, σ', hm, K⟩ | ⟨herr, σ', off', hm, K⟩ + · refine Or.inl ⟨hok, σ', Rq', Nt', ?_, K⟩ + refine ut_reset_loop_okA (σD := utDelStA (specPtr p) sb.val σ) (σm := σ') (pu := specPtr p) (sz := sb.val) + (fi := fi.val) hg hfipos hfi hbig' hD ?_ (hPost σ') + rw [eidx]; exact hm + · refine Or.inr ⟨herr, σ', off', ?_, K⟩ + refine ut_reset_loop_errA (σD := utDelStA (specPtr p) sb.val σ) (pu := specPtr p) (sz := sb.val) + (fi := fi.val) hg hfipos hfi hbig' hD ?_ + rw [eidx]; exact hm + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtResetDevA.lean b/proofs/Fl4ProofsA64/P3/UtResetDevA.lean new file mode 100644 index 000000000..f8280e329 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtResetDevA.lean @@ -0,0 +1,280 @@ +/- +P3 (aarch64, A64-P3 Untyped; lead 10:53:55Z item 1): **the reset of a used device untyped.** + +FL4 rewrites the cap with free index 0 and nothing else; the spec deletes the region's objects, then writes free index +0. + +* `SysL.devRstA`: A64 P1's `SysL ∧ SymL ∧ BlkOK` across the free-index write of a childless device untyped. +* **`utReset_devA`**: FL4's `reset_untyped` succeeds, the spec's `resetUntypedCap` has the matching outcome, and + `JStage3A` holds on the same `Z`, `H`, `OS`, with `SymL` and the new cap. + +Named premises: +* [J] `symL`: A64 P1's `SymL`. +* [S] `othT`, `othC`: another core's current thread and current SC are not in the region. +* [P] `hPost`: the spec's `arch_post_reset_untyped` has the no-change outcome. +-/ +import Fl4ProofsA64.P3.UtDropDevA +import Fl4ProofsA64.P3.UtResetA +import Fl4ProofsA64.P3.UtSbufA +import Fl4ProofsA64.P1.MdbRstD +import Fl4ProofsA64.P1.MdbSetCapG +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P1 +open fl4_core_a64 Fl4ProofsA64.P0.Consts Fl4Proofs.P3 + +section +variable {S : Type _} {st : machine.Store S} [L : LawfulStore st] {H : Heap} +variable {Cs Ac : Set U64} + +local notation "wa" => core.num.U64.wrapping_add + +namespace W7 +namespace R +namespace ST + +variable {OS : Set U64} {ia ie : Nat} + +set_option maxHeartbeats 2000000 in +/-- **The free-index write of a childless device untyped keeps `SysL ∧ SymL` and `BlkOK`** (MdbSetCapG's `sysLG` +with the untyped's own cap facts, and MdbRstD's `StZ.devRst` for the untyped bundle). -/ +theorem SysL.devRstA (hH : HeapOK H) (hO : OSOK H.tcbs H.omega H.frame OS) {s : S} + (hb : BXI st H.tcbs Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s) + (B : SysL st H Cs OS ia ie s) (Y : SymL st H Cs s) {x p sb fi : U64} (hx : x ∈ OS) + (hc : capA st s x = .Untyped p sb fi true) + (hnd : ∀ G par, TreeA st OS G par s → ∀ y, ¬ Mdb.Desc par y x) {w0 w1 : U64} + (hd : capability.Cap.decode w0 w1 = ok (.Untyped p sb 0#u64 true)) + (w : WritesIn st s (L.wr (L.wr s x w0) (wa x 8#u64) w1) (W7.CapW H.tcbs H.omega H.frame)) : + SysL st H Cs OS ia ie (L.wr (L.wr s x w0) (wa x 8#u64) w1) ∧ + SymL st H Cs (L.wr (L.wr s x w0) (wa x 8#u64) w1) ∧ + BlkOK st H.tcbs Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac + (L.wr (L.wr s x w0) (wa x 8#u64) w1) := by + obtain ⟨Q, Rq, q⟩ := hb.1 + generalize hs' : L.wr (L.wr s x w0) (wa x 8#u64) w1 = s' at w ⊢ + have ed : capA st s' x = .Untyped p sb 0#u64 true := by rw [← hs']; exact capA_wr2_self hO hx hd + have hk : ∀ y ∈ OS, y ≠ x → capA st s' y = capA st s y := fun y hy e => by + rw [← hs']; exact capA_wr2_other hO hx hy e + have u : UBm st OS s s' := fun y hy => by + by_cases e : y = x + · subst e; exact ⟨y, hy, by rw [ed]; exact UB.of_not fun _ _ _ h => by cases h⟩ + · exact ⟨y, hy, by rw [hk y hy e]; exact UB.refl _⟩ + have g : LiveAg st H Cs s s' := LiveAg.ofCwN hb.blk w fun _ _ hb' => Or.inl hb' + obtain ⟨y', vr', ly'⟩ := SVL.cw hb.blk ⟨Y, B.vr, B.ly⟩ w (fun _ _ hb' => Or.inl hb') u + have ksk : L.ksb s' = L.ksb s := w.1 + obtain ⟨hStZ, hFr, hCz⟩ := StZ.devRst hH hO B.sys.stz B.sys.fr B.sys.cz hx hc hnd hd + rw [hs'] at hStZ hFr hCz + have e0 : capA st s' x = .Untyped p sb 0#u64 true ∧ ∀ y ∈ OS, y ≠ x → capA st s' y = capA st s y := ⟨ed, hk⟩ + have hP : ∀ {P : capability.Cap → Prop}, P (.Untyped p sb 0#u64 true) → CapInv st P OS s → CapInv st P OS s' := + fun hp h => CapInv.ins h hp e0.1 e0.2 + have hur : UtCP H (.Untyped p sb 0#u64 true) := by + have h := B.sys.ur x hx + rw [hc] at h + intro p' sb' fi' dev' e + injection e with e1 e2 e3 e4 + subst e1 e2 e4 + exact ⟨(h p sb fi true rfl).1, fun h => by cases h⟩ + have xB : x ∉ BufS H.tcbs := fun h => by + have := B.bh x hx h + rw [hc] at this + rcases this with g | ⟨_, _, _, _, _, _, _, g⟩ <;> cases g + have bi : BufInv st H OS s' := fun y hy => by + by_cases e : y = x + · subst e; rw [ed]; exact ⟨⟨(fun _ _ _ _ h => by cases h), (fun _ _ _ h => by cases h)⟩, fun h => absurd h xB⟩ + · rw [hk y hy e]; exact BufInv.of B.cn B.bh y hy + have ep' : EptSlot st H OS s' := fun y hy hE => by + by_cases e : y = x + · subst e + have h := B.ep y hy hE + rw [hc] at h + rcases h with g | ⟨_, _, _, _, _, g⟩ <;> cases g + · rw [hk y hy e]; exact B.ep y hy hE + have al : NamesKept st OS s s' := NamesKept.of_alike fun z hzO _ => ⟨z, hzO, by + by_cases e : z = x + · subst e; rw [ed, hc]; exact ⟨⟨fun h => h, fun h => h⟩, fun _ => ⟨rfl, rfl⟩⟩ + · rw [hk z hzO e]; exact AlikeA.refl _⟩ + refine ⟨⟨⟨hStZ, hFr, hCz, hP hur B.sys.ur⟩, B.ln.ag g al, vr', ly', bi.cn, bi.bh, + (hP ⟨(fun h => by cases h), (fun _ h => by cases h), (fun _ _ _ h => by cases h)⟩ B.ci).idleMv g.idle (g.idleSc q.idleT), hP (fun _ _ h => by cases h) B.sc, CtOff.ks ksk (hP (CtOffP.of_not (fun _ _ _ _ h => by cases h) (fun _ _ _ h => by cases h)) B.ct), + FrOff.ks ksk (hP (FrOffP.of_nf fun _ _ _ _ _ _ _ h => by cases h) B.fo), ep', B.ipc.keep (IbK.of_w q.tcbs w fun y hy => ib_not_cw q hy)⟩, y', + (blk_of_capw hb.blk w fun _ h => h).1⟩ + +end ST +end R +end W7 + +end + +end Fl4ProofsA64.P1 + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s : S} {a : A} {σ : State Unit} {m : McState} + +set_option maxHeartbeats 2000000 in +/-- **The reset of a used childless device untyped** (module docstring). -/ +theorem utReset_devA [Fact (n ≤ Fl4SpecA64.fl4A64Platform.numNodes)] (ar : machine.Arch A) + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {p sb fi x : U64} {Ud Acd Rq Nt : Set U64} {ex : Aeneas.Std.Array obj.Slot 3#usize} + (Dp : DecPre st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ud Acd s (.Untyped p sb fi true) ex) + (P : SysPlace st H Z.Cs H.frame Rq Nt OS s ex x) + (hcap : obj.Slot.cap st x s = ok (.Untyped p sb fi true)) + (he : cdt.ensure_no_children st s x = ok (.Ok ())) + (symL : SymL st H Z.Cs s) + (othT : ∀ c ∈ Z.Cs, c ≠ L.cpu s → specPtr (L.mem s (L.nodeAt s c layout.node.CUR_THREAD)) ∉ utDA p sb.val) + (othC : ∀ c ∈ Z.Cs, c ≠ L.cpu s → specPtr (L.mem s (L.nodeAt s c layout.node.CUR_SC)) ∉ utDA p sb.val) + (hn : nA.val = fl4A64Platform.s2IpaBits) + (cte : CslotPtr) {qc : U64} (hq : cte.1 = specPtr qc) (hth : Z.th qc = some (.cnode cte.2.length)) + (hxc : cslotAddrA cte = x) + (hPost : ∀ σ' : State Unit, ((), σ') ∈ (archPostResetUntyped (Z := Unit) true sb.val σ').1) + (hfi0 : fi ≠ 0#u64) : + ∃ s2 σ2, untyped.reset_untyped st ar s a x = ok (core.result.Result.Ok (), s2, a) ∧ + (.ok (), σ2) ∈ ((resetUntypedCap (Z := Unit) cte).run σ).1 ∧ + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp + (utBfOffA (utDA p sb.val) (dropBfA (utDA p sb.val) Bf)) stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s2 a σ2 + (dropMA (utDA p sb.val) m) ∧ + SymL st H Z.Cs s2 ∧ capA st s2 x = .Untyped p sb 0#u64 true := by + have hsal := J.bufAl + have dO := J.devUtOff + have hcx : capA st s x = .Untyped p sb fi true := okinj ((capA_eq s x).symm.trans hcap) + have hx : x ∈ OS := P.src + have hH := J.sys.ent.heap + have hO := J.sys.ent.os + obtain ⟨hpal, h47, -⟩ := J.sys.sysl.sys.ur.utv J.sys.sysl.sys.stz.1.1 hx hcx + have h4 : 4 ≤ sb.val := J.sys.sysl.sys.ur.ge4 hx hcx + have hsb : sb.val < 64 := by omega + have hpfit : p.val + 2 ^ sb.val ≤ 2 ^ 64 := al_fit hpal (by omega) p.bv.isLt + have hab : AbsSlotA Z.th cte := ⟨qc, hq, Or.inl hth⟩ + have hwf : SpNoOvA p sb.val := by + obtain ⟨ac, -, hr⟩ := ut_absRelA J.j.1.st hab + have h := hr.1 + rw [hxc, show capAtA (L.mem s) x = capA st s x from rfl, hcx] at h + exact h + have hsp : (specPtr p).toNat + 2 ^ sb.val ≤ 2 ^ 64 := ut_spNoOvA hsb hwf + have hD : ∀ z : U64, specPtr z ∈ utDA p sb.val ↔ p.val ≤ z.val ∧ z.val < p.val + 2 ^ sb.val := + utD_memA hsb hpfit hsp + -- the drop + obtain ⟨-, -, -, -, eO, -, -, -, -⟩ := utDrop_devIdA J hx hcx hsb hpfit hsp + have G := utDropG_devA J Dp P hcap he othT othC hn + obtain ⟨J1, -⟩ := utDrop_devA J Dp P hcap he symL othT othC hn + have hslots : ∀ y ∈ OS, y.val + 32 ≤ p.val ∨ p.val + 2 ^ sb.val ≤ y.val := by + intro y hy + have h : y ∈ OffR p.val (p.val + 2 ^ sb.val) OS := by rw [eO]; exact hy + exact h.2 + -- spec `delete_objects` + have eσ : utDelStA (specPtr p) sb.val σ = + withUmA (detype (utDA p sb.val) σ) (utUm0A (utDA p sb.val) σ) := utDelStA_withUmA hsb hsp σ + have J2 := utZeroSpec_stageA J1 (utDA p sb.val) (utUm0A (utDA p sb.val) σ) (fun w => rfl) + (fun t ht y b sbf hk hb => by + change (if specPtr t ∈ utDA p sb.val then none else σ.kheap (specPtr t)) = some (.tcb y) at hk + split at hk + · cases hk + rename_i kt + have R : UtDecP st H Z.Cs H.frame Rq Nt OS s ex x p sb fi true := + UtDecP.of_live Dp J.sys.sysl.sys P hcap J.sys.sysl.ln J.sys.sysl.vr J.sys.sysl.ly + have hRin : RegionIn H p.val (p.val + 2 ^ sb.val) := fun q k hq hm => by + refine absurd ?_ (utDev_heapOffA dO hx hcx q (by rw [hq]; simp)) + unfold RegionD + rw [hq] + exact hm + exact utSbufG_keptA J R hcx hRin he hsal t ht kt y b sbf hk hb) + rw [← eσ] at J2 + generalize hσ1 : utDelStA (specPtr p) sb.val σ = σ1 at J2 + have C : UtClrA st Z H s p.val (p.val + 2 ^ sb.val) := + ⟨fun q bytes h => dO.obj x hx p sb fi hcx q bytes h, (dO.ks x hx p sb fi hcx).1, (dO.ks x hx p sb fi hcx).2⟩ + have hheap : ∀ q : U64, p.val ≤ q.val → q.val < p.val + 2 ^ sb.val → σ1.kheap (specPtr q) = none := by + intro q h1 h2 + rw [← hσ1, eσ] + show (if specPtr q ∈ utDA p sb.val then none else σ.kheap (specPtr q)) = none + rw [ite_eq_left ((hD q).2 ⟨h1, h2⟩)] + have hlink : ∀ a' c', σ1.cdt a' = some c' → ∀ q : U64, p.val ≤ q.val → q.val < p.val + 2 ^ sb.val → + a'.1 ≠ specPtr q ∧ c'.1 ≠ specPtr q := by + intro a' c' h q h1 h2 + rw [← hσ1, eσ] at h + have g := G.cdt a' c' h + have mq := (hD q).2 ⟨h1, h2⟩ + exact ⟨fun e => g.1 (e ▸ mq), fun e => g.2 (e ▸ mq)⟩ + have horig : ∀ a', σ1.isOriginalCap a' = true → ∀ q : U64, p.val ≤ q.val → q.val < p.val + 2 ^ sb.val → + a'.1 ≠ specPtr q := by + intro a' h q h1 h2 e + obtain ⟨qa, ha, hk⟩ := J2.origLive a' h + have off : specPtr qa ∉ utDA p sb.val := by + intro hd + have h3 := (hD qa).1 hd + rcases hk with hk | ⟨hk, -⟩ + · have := dO.obj x hx p sb fi hcx qa _ (Or.inr (Or.inr (Or.inl ⟨_, hk, rfl⟩))) + simp only [ObjKindA.size] at this + have := Nat.two_pow_pos (a'.2.length + 5) + omega + · have := dO.obj x hx p sb fi hcx qa _ (Or.inr (Or.inr (Or.inl ⟨_, hk, rfl⟩))) + simp only [ObjKindA.size] at this + omega + exact off (by rw [← ha, e]; exact (hD q).2 ⟨h1, h2⟩) + have hz : ∀ w : Word, p.val ≤ (fl4Ptr w).val → (fl4Ptr w).val < p.val + 2 ^ sb.val → + σ1.machineState.underlyingMemory w = 0 := by + intro w h1 h2 + rw [← hσ1, eσ] + show (if w ∈ utDA p sb.val then (0 : Word8) else σ.machineState.underlyingMemory w) = 0 + have mw : specPtr (fl4Ptr w) ∈ utDA p sb.val := (hD _).2 ⟨h1, h2⟩ + rw [specPtr_fl4Ptr] at mw + rw [ite_eq_left mw] + -- the FL4 write + have hEnc : CapRt.EncWf (.Untyped p sb 0#u64 true) := by + have w := CapRt.decodeF_encWf (L.mem s x).bv (L.mem s (wa x 8#u64)).bv + have e' : CapRt.decodeF (L.mem s x).bv (L.mem s (wa x 8#u64)).bv = .Untyped p sb fi true := hcx + rw [e'] at w; exact w + obtain ⟨w0, w1, hen, hd⟩ := CapRt.decode_encode _ hEnc + have hset : obj.Slot.set_cap st x s (.Untyped p sb 0#u64 true) = ok (L.wr (L.wr s x w0) (wa x 8#u64) w1) := + set_cap_eq hen + have hcapw : WritesIn st s (L.wr (L.wr s x w0) (wa x 8#u64) w1) (W7.CapW H.tcbs H.omega H.frame) := by + have := W7.obj.Slot.set_cap.capw (st := st) (s := s) (c := .Untyped p sb 0#u64 true) (hO.slot x hx) + rw [hset, WP.spec_ok] at this + exact this + have hnd : ∀ G par, TreeA st OS G par s → ∀ y, ¬ Fl4Proofs.P3.Mdb.Desc par y x := fun G par h y => + cdt.ensure_no_children.noDesc hO h hx hcx (J.sys.sysl.sys.stz.1.2.1 x hx (by rw [hcx]; rfl)) he y + have hsys := SysL.devRstA hH hO J.sys.ent.bxi J.sys.sysl symL hx hcx hnd hd hcapw + have hcx2 : capA st (L.wr (L.wr s x w0) (wa x 8#u64) w1) x = .Untyped p sb 0#u64 true := capA_wr2_self hO hx hd + have e0 : (0#u64 : U64).val = 0 := rfl + have P' : SysPlace st H Z.Cs H.frame Rq Nt OS s ex x := P + obtain ⟨σ2, hsc, J3, Y3, -, -, -, hc2, -⟩ := utChunk_coreA (c := p) (off := 0#u64) (len := 0#u64) J2 (p := p) + (sb := sb) (i := fi) (x := x) C P' Dp.run hcx he cte hq hth hxc hslots (by rw [e0]; omega) + (by rw [e0]; omega) hheap hlink horig hz s (L.wr (L.wr s x w0) (wa x 8#u64) w1) (WritesIn.refl _ _) hset hcapw.1 hcapw.2.1 + (fun y hy ne => capA_wr2_other hO hx hy ne) (set_cap_links hO hx) hcx2 hsys + refine ⟨_, σ2, ut_reset_devA ar hcx hfi0 hset, ?_, J3, Y3, hc2⟩ + -- the spec + obtain ⟨ac, hg, hr⟩ := J.j.1.st.getAbsA hab + have eac : ac = .untypedCap true (specPtr p) sb.val fi.val := by + have h := hr.2 + rw [hxc, show capAtA (L.mem s) x = capA st s x from rfl, hcx] at h + exact (show absCapA (.Untyped p sb fi true) = ac from h).symm + rw [eac] at hg + have hal8 : (specPtr p).toNat % 8 = 0 := by + have := specPtr_mod p (n := 3) (by omega) + have h8 : 8 ∣ 2 ^ sb.val := by + have : 2 ^ 3 ∣ 2 ^ sb.val := Nat.pow_dvd_pow 2 (by omega) + simpa using this + have hp8 : p.val % 8 = 0 := Nat.mod_eq_zero_of_dvd (h8.trans (Nat.dvd_of_mod_eq_zero hpal)) + simpa [show 2 ^ 3 = 8 by decide, hp8] using this + have hDl := ut_delete_exA (sz := sb.val) hal8 (by omega) hsb hsp σ + rw [hσ1] at hDl + refine ut_reset_small_memA (σD := σ1) (σC := σ1) (σS := σ2) (dev := true) (pu := specPtr p) (sz := sb.val) + (fi := fi.val) hg ?_ (Or.inl rfl) hDl ?_ hsc (hPost σ2) + · intro h + exact hfi0 (UScalar.eq_of_val_eq (by rw [h]; rfl)) + · unfold Fl4Spec.NonDet.unless + simp only [↓reduceIte] + exact rfl + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtResetLoopA.lean b/proofs/Fl4ProofsA64/P3/UtResetLoopA.lean new file mode 100644 index 000000000..7dadd5e71 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtResetLoopA.lean @@ -0,0 +1,222 @@ +/- +P3 (aarch64, A64-P3 Untyped W2, spec side): **the outcomes of l4v `reset_untyped_cap`** (`resetUntypedCap`), as +memberships, including the preempted exit. + +* `utChunkA` is one chunk of the loop (`resetUntypedCap_chunksA`: the chunked branch is `mapMEx utChunkA` over the + chunk indices); `ut_chunk_memA`: a clear, a cap write and a preemption-point outcome give a chunk outcome. +* `ut_chunk_idxA`: for a used untyped (`0 < fi ≤ 2^sz`) the indices are `k, k-1, .., 0` with `k = (fi - 1) / chunk`, + FL4's descending offsets. +* `ut_chunks_okA` / `ut_chunks_errA` / `ut_chunks_nilA`: a chunk's `Ok` continues the loop; its preemption ends it. +* Tops: `ut_reset_noop_memA` (unused), `ut_reset_small_memA` (device or below a chunk), `ut_reset_loop_okA`, + `ut_reset_loop_errA` (preempted). +* `ut_clear_exA`, `ut_delete_exA`: `clear_memory` and `delete_objects` have an outcome on aligned regions; with + `deleteObjects_outA` the latter's is `utDelStA`. + +The cap writes, the preemption points and `arch_post_reset_untyped` are premises of each outcome: their producers are +the record's relations (`UtResetA`). No named premise. New definitions and lemmas only. +-/ +import Fl4ProofsA64.P3.UtResetSpecA +import Fl4ProofsA64.P3.DelSpecA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Classical + +noncomputable section + +section +open Fl4SpecA64 Fl4Spec + +private theorem ut_mapMx_store_exA (xs : List Word) (hal : ∀ p ∈ xs, p &&& mask 3 = 0) (ms : MachineState) : + ∃ ms', ((), ms') ∈ (NonDet.mapMx (fun p => storeWord p 0) xs ms).1 := by + induction xs generalizing ms with + | nil => exact ⟨ms, Set.mem_singleton _⟩ + | cons p ps ih => + unfold NonDet.mapMx + have hp : p &&& mask 3 = 0 := hal p (List.mem_cons_self ..) + have hsw : ∃ ms1, ((), ms1) ∈ (storeWord p 0 ms).1 := by + unfold storeWord NonDet.assert NonDet.modify + simp [hp] + exact ⟨_, ⟨((), ms), Set.mem_singleton _, Set.mem_singleton _⟩⟩ + obtain ⟨ms1, hms1⟩ := hsw + obtain ⟨ms2, hms2⟩ := ih (fun q hq => hal q (List.mem_cons_of_mem p hq)) ms1 + exact ⟨ms2, ⟨((), ms1), hms1, hms2⟩⟩ + +private theorem ut_step_alA {ptr hi : Word} (hal : ptr.toNat % 8 = 0) : + ∀ p ∈ uptoEnumStep ptr (ptr + BitVec.ofNat 64 wordSize) hi, p &&& mask 3 = 0 := by + intro p hp + unfold uptoEnumStep at hp + split_ifs at hp with hlt + · cases hp + · rw [List.mem_map] at hp + obtain ⟨x, _, rfl⟩ := hp + have hws : BitVec.ofNat 64 wordSize = 8#64 := by decide + have hm3 : (mask 3 : BitVec 64) = 7#64 := by decide + rw [hws, hm3] + have hsub : ptr + 8#64 - ptr = 8#64 := by bv_omega + rw [hsub] + apply BitVec.eq_of_toNat_eq + rw [BitVec.toNat_and, show (7#64 : BitVec 64).toNat = 7 from rfl, Nat.and_two_pow_sub_one_eq_mod _ 3, + BitVec.toNat_add, BitVec.toNat_mul, show (8#64 : BitVec 64).toNat = 8 from rfl, + show (0 : BitVec 64).toNat = 0 from rfl] + omega + +/-- `clear_memory` of an aligned region has an outcome. -/ +theorem ut_clear_exA {ptr : Word} (hal : ptr.toNat % 8 = 0) (n : Nat) (σ : State Unit) : + ∃ ms', ((), { σ with machineState := ms' }) ∈ (doMachineOp (Z := Unit) (clearMemory ptr n) σ).1 := by + obtain ⟨ms', h⟩ := ut_mapMx_store_exA _ (ut_step_alA (hi := ptr + BitVec.ofNat 64 n - 1) hal) σ.machineState + exact ⟨ms', dmo_has h⟩ + +/-- `delete_objects` of an aligned region has an outcome (`utDelStA`, by `deleteObjects_outA`). -/ +theorem ut_delete_exA {ptr : Word} {sz : Nat} (hal : ptr.toNat % 8 = 0) (hsz : 3 ≤ sz) (hsz' : sz < 64) + (hfit : ptr.toNat + 2 ^ sz ≤ 2 ^ 64) (σ : State Unit) : + ((), utDelStA ptr sz σ) ∈ (deleteObjects (Z := Unit) ptr sz σ).1 := by + obtain ⟨ms', h⟩ := ut_mapMx_store_exA _ (ut_step_alA (hi := ptr + 2 ^ sz - 1) hal) σ.machineState + have hm : ((), { σ with machineState := ms' }) ∈ (doMachineOp (Z := Unit) (freeMemory ptr sz) σ).1 := dmo_has h + have : ((), detype {x | ptr ≤ x ∧ x ≤ ptr + 2 ^ sz - 1} { σ with machineState := ms' }) ∈ + (deleteObjects (Z := Unit) ptr sz σ).1 := by + unfold deleteObjects + exact mem_bind_ndD hm rfl + have e := deleteObjects_outA hal hsz hsz' hfit σ this + simp only at e + rwa [e] at this + +/-! ## One chunk and the loop -/ + +/-- One chunk of `reset_untyped_cap`'s loop. -/ +def utChunkA (dev : Bool) (pu : Word) (sz : Nat) (sl : CslotPtr) (i : Nat) : PMonad Unit Unit := do + NonDetE.liftE (doMachineOp + (clearMemory (pu + (BitVec.ofNat 64 i <<< resetChunkBits)) (2 ^ resetChunkBits))) + NonDetE.liftE (setCap (.untypedCap dev pu sz (i * 2 ^ resetChunkBits)) sl) + preemptionPoint + +theorem ut_chunk_memA {dev : Bool} {pu : Word} {sz i : Nat} {sl : CslotPtr} {σ σ1 σ2 σ3 : State Unit} + {e : Except Unit Unit} + (hc : ((), σ1) ∈ (doMachineOp (Z := Unit) + (clearMemory (pu + (BitVec.ofNat 64 i <<< resetChunkBits)) (2 ^ resetChunkBits)) σ).1) + (hs : ((), σ2) ∈ (setCap (Z := Unit) (.untypedCap dev pu sz (i * 2 ^ resetChunkBits)) sl σ1).1) + (hp : (e, σ3) ∈ ((preemptionPoint (Z := Unit)).run σ2).1) : + (e, σ3) ∈ ((utChunkA dev pu sz sl i).run σ).1 := by + unfold utChunkA + exact mem_bindE_okD (mem_liftED hc) (mem_bindE_okD (mem_liftED hs) hp) + +theorem ut_chunks_nilA (dev : Bool) (pu : Word) (sz : Nat) (sl : CslotPtr) (σ : State Unit) : + (.ok (), σ) ∈ ((NonDetE.mapMEx (utChunkA dev pu sz sl) []).run σ).1 := mem_of_runD (run_returnOkD _ σ) + +theorem ut_chunks_okA {dev : Bool} {pu : Word} {sz i : Nat} {is : List Nat} {sl : CslotPtr} {σ σ' : State Unit} + {y : Except Unit Unit × State Unit} (h : (.ok (), σ') ∈ ((utChunkA dev pu sz sl i).run σ).1) + (hr : y ∈ ((NonDetE.mapMEx (utChunkA dev pu sz sl) is).run σ').1) : + y ∈ ((NonDetE.mapMEx (utChunkA dev pu sz sl) (i :: is)).run σ).1 := + mem_bindE_okD (f := fun _ => NonDetE.mapMEx (utChunkA dev pu sz sl) is) h hr + +theorem ut_chunks_errA {dev : Bool} {pu : Word} {sz i : Nat} {is : List Nat} {sl : CslotPtr} {σ σ' : State Unit} + (h : (.error (), σ') ∈ ((utChunkA dev pu sz sl i).run σ).1) : + (.error (), σ') ∈ ((NonDetE.mapMEx (utChunkA dev pu sz sl) (i :: is)).run σ).1 := + mem_bindE_errD (f := fun _ => NonDetE.mapMEx (utChunkA dev pu sz sl) is) h + +/-- The chunk indices below the free index are `0 .. (fi - 1) / c`. -/ +theorem ut_chunk_idxA {c fi N : Nat} (hc : 0 < c) (hfi : 0 < fi) (hle : fi ≤ N * c) : + (List.range N).filter (fun i => decide (i * c < fi)) = List.range ((fi - 1) / c + 1) := by + have hk : (fi - 1) / c < N := by + rw [Nat.div_lt_iff_lt_mul hc] + omega + have hiff : ∀ i, i * c < fi ↔ i < (fi - 1) / c + 1 := by + intro i + rw [Nat.lt_succ_iff, Nat.le_div_iff_mul_le hc] + omega + obtain ⟨d, hd⟩ : ∃ d, N = (fi - 1) / c + 1 + d := ⟨N - ((fi - 1) / c + 1), by omega⟩ + generalize hm : (fi - 1) / c + 1 = m at hiff hd ⊢ + subst hd + rw [List.range_add, List.filter_append, List.filter_eq_self.2 (fun i hi => by + simpa using (hiff i).2 (List.mem_range.1 hi)), List.filter_eq_nil_iff.2 (fun i hi => ?_), List.append_nil] + obtain ⟨j, -, rfl⟩ := List.mem_map.1 hi + simp only [decide_eq_true_eq, hiff] + omega + +/-! ## The whole reset -/ + +theorem ut_reset_noop_memA {sl : CslotPtr} {σ : State Unit} {cap : Cap} + (hg : (cap, σ) ∈ (getCap (Z := Unit) sl σ).1) (h0 : freeIndexOf cap = 0) : + (.ok (), σ) ∈ ((resetUntypedCap (Z := Unit) sl).run σ).1 := by + unfold resetUntypedCap + refine mem_bindE_okD (mem_liftED hg) ?_ + refine mem_bindE_okD (mem_of_runD (run_returnOkD _ σ)) ?_ + refine mem_bindE_okD (mem_of_runD (run_returnOkD _ σ)) ?_ + rw [ite_eq_left h0] + exact mem_of_runD (run_returnOkD _ σ) + +/-- Device, or below a chunk: clear (RAM only), set the free index to 0, `arch_post_reset_untyped`. -/ +theorem ut_reset_small_memA {sl : CslotPtr} {σ σD σC σS σ' : State Unit} {dev : Bool} {pu : Word} {sz fi : Nat} + (hg : (.untypedCap dev pu sz fi, σ) ∈ (getCap (Z := Unit) sl σ).1) (hfi : fi ≠ 0) + (hsm : dev = true ∨ sz < resetChunkBits) + (hD : ((), σD) ∈ (deleteObjects (Z := Unit) pu sz σ).1) + (hC : ((), σC) ∈ (NonDet.unless (dev = true) (doMachineOp (Z := Unit) (clearMemory pu (2 ^ sz))) σD).1) + (hS : ((), σS) ∈ (setCap (Z := Unit) (.untypedCap dev pu sz 0) sl σC).1) + (hA : ((), σ') ∈ (archPostResetUntyped (Z := Unit) dev sz σS).1) : + (.ok (), σ') ∈ ((resetUntypedCap (Z := Unit) sl).run σ).1 := by + unfold resetUntypedCap + refine mem_bindE_okD (mem_liftED hg) ?_ + refine mem_bindE_okD (a := sz) (mem_of_runD (run_returnOkD _ σ)) ?_ + refine mem_bindE_okD (a := pu) (mem_of_runD (run_returnOkD _ σ)) ?_ + rw [ite_eq_right (show ¬ freeIndexOf (.untypedCap dev pu sz fi) = 0 from hfi)] + refine mem_bindE_okD (mem_liftED hD) ?_ + refine mem_bindE_okD (a := dev) (mem_of_runD (run_returnOkD _ σD)) ?_ + rw [ite_eq_left hsm] + exact mem_bindE_okD (mem_liftED (mem_bind_ndD hC hS)) (mem_liftED hA) + +/-- The chunked reset that runs to the end. -/ +theorem ut_reset_loop_okA {sl : CslotPtr} {σ σD σm σ' : State Unit} {pu : Word} {sz fi : Nat} + (hg : (.untypedCap false pu sz fi, σ) ∈ (getCap (Z := Unit) sl σ).1) (hfi : 0 < fi) (hle : fi ≤ 2 ^ sz) + (hbig : resetChunkBits ≤ sz) (hD : ((), σD) ∈ (deleteObjects (Z := Unit) pu sz σ).1) + (hm : (.ok (), σm) ∈ ((NonDetE.mapMEx (utChunkA false pu sz sl) + (List.range ((fi - 1) / 2 ^ resetChunkBits + 1)).reverse).run σD).1) + (hA : ((), σ') ∈ (archPostResetUntyped (Z := Unit) false sz σm).1) : + (.ok (), σ') ∈ ((resetUntypedCap (Z := Unit) sl).run σ).1 := by + have hidx := ut_chunk_idxA (c := 2 ^ resetChunkBits) (fi := fi) (N := 2 ^ (sz - resetChunkBits)) + (Nat.two_pow_pos _) hfi (by rw [← Nat.pow_add, Nat.sub_add_cancel hbig]; exact hle) + unfold resetUntypedCap + refine mem_bindE_okD (mem_liftED hg) ?_ + refine mem_bindE_okD (a := sz) (mem_of_runD (run_returnOkD _ σ)) ?_ + refine mem_bindE_okD (a := pu) (mem_of_runD (run_returnOkD _ σ)) ?_ + rw [ite_eq_right (show ¬ freeIndexOf (.untypedCap false pu sz fi) = 0 from Nat.pos_iff_ne_zero.1 hfi)] + refine mem_bindE_okD (mem_liftED hD) ?_ + refine mem_bindE_okD (a := false) (mem_of_runD (run_returnOkD _ σD)) ?_ + rw [ite_eq_right (show ¬ (false = true ∨ sz < resetChunkBits) by simp; omega)] + refine mem_bindE_okD (a := ()) (s' := σm) ?_ (mem_liftED hA) + simp only [freeIndexOf] + change (.ok (), σm) ∈ ((NonDetE.mapMEx (utChunkA false pu sz sl) + ((List.range (2 ^ (sz - resetChunkBits))).filter + (fun i => decide (i * 2 ^ resetChunkBits < fi))).reverse).run σD).1 + rw [hidx] + exact hm + +/-- The chunked reset that is preempted. -/ +theorem ut_reset_loop_errA {sl : CslotPtr} {σ σD σm : State Unit} {pu : Word} {sz fi : Nat} + (hg : (.untypedCap false pu sz fi, σ) ∈ (getCap (Z := Unit) sl σ).1) (hfi : 0 < fi) (hle : fi ≤ 2 ^ sz) + (hbig : resetChunkBits ≤ sz) (hD : ((), σD) ∈ (deleteObjects (Z := Unit) pu sz σ).1) + (hm : (.error (), σm) ∈ ((NonDetE.mapMEx (utChunkA false pu sz sl) + (List.range ((fi - 1) / 2 ^ resetChunkBits + 1)).reverse).run σD).1) : + (.error (), σm) ∈ ((resetUntypedCap (Z := Unit) sl).run σ).1 := by + have hidx := ut_chunk_idxA (c := 2 ^ resetChunkBits) (fi := fi) (N := 2 ^ (sz - resetChunkBits)) + (Nat.two_pow_pos _) hfi (by rw [← Nat.pow_add, Nat.sub_add_cancel hbig]; exact hle) + unfold resetUntypedCap + refine mem_bindE_okD (mem_liftED hg) ?_ + refine mem_bindE_okD (a := sz) (mem_of_runD (run_returnOkD _ σ)) ?_ + refine mem_bindE_okD (a := pu) (mem_of_runD (run_returnOkD _ σ)) ?_ + rw [ite_eq_right (show ¬ freeIndexOf (.untypedCap false pu sz fi) = 0 from Nat.pos_iff_ne_zero.1 hfi)] + refine mem_bindE_okD (mem_liftED hD) ?_ + refine mem_bindE_okD (a := false) (mem_of_runD (run_returnOkD _ σD)) ?_ + rw [ite_eq_right (show ¬ (false = true ∨ sz < resetChunkBits) by simp; omega)] + refine mem_bindE_errD ?_ + simp only [freeIndexOf] + change (.error (), σm) ∈ ((NonDetE.mapMEx (utChunkA false pu sz sl) + ((List.range (2 ^ (sz - resetChunkBits))).filter + (fun i => decide (i * 2 ^ resetChunkBits < fi))).reverse).run σD).1 + rw [hidx] + exact hm + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtResetRunA.lean b/proofs/Fl4ProofsA64/P3/UtResetRunA.lean new file mode 100644 index 000000000..dc8513983 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtResetRunA.lean @@ -0,0 +1,88 @@ +/- +P3 (aarch64, A64-P3 Untyped W2, FL4 side): **FL4's `reset_untyped`, case by case**, as equations over the law-abiding +store. + +* `ut_reset_noopA`: a slot with an unused untyped (`free_index = 0`) is left as it is. +* `ut_reset_devA`: a used device untyped only gets free index 0. +* `ut_reset_smallA`: a used RAM untyped below the chunk size is cleared whole, then gets free index 0. +* `ut_reset_chunkedA`: a used RAM untyped of at least a chunk runs the chunk loop from `(free_index - 1)` rounded + down to a chunk. +* `ut_reset_stepA`: one chunk clears `[ptr + offset, + 64 KiB)` and writes free index `offset`; at offset 0 the loop + ends `Ok`, otherwise it reaches the preemption point, then continues one chunk lower or ends with its error. + +No named premise. New lemmas only. +-/ +import Fl4ProofsA64.P1.MdbRstD +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} (ar : machine.Arch A) + +theorem ut_slot_capA {s : S} {x : U64} {c : capability.Cap} (h : capA st s x = c) : + obj.Slot.cap st x s = ok c := by + rw [capA_eq, h] + +theorem ut_reset_noopA {s : S} {a : A} {x p sb : U64} {dev : Bool} (hc : capA st s x = .Untyped p sb 0#u64 dev) : + untyped.reset_untyped st ar s a x = ok (core.result.Result.Ok (), s, a) := by + unfold untyped.reset_untyped + rw [ut_slot_capA hc, bind_ok] + unfold untyped.as_untyped + simp only [bind_ok, ↓reduceIte] + +theorem ut_reset_devA {s s2 : S} {a : A} {x p sb fi : U64} (hc : capA st s x = .Untyped p sb fi true) + (hfi : fi ≠ 0#u64) (hset : obj.Slot.set_cap st x s (.Untyped p sb 0#u64 true) = ok s2) : + untyped.reset_untyped st ar s a x = ok (core.result.Result.Ok (), s2, a) := by + unfold untyped.reset_untyped + rw [ut_slot_capA hc, bind_ok] + unfold untyped.as_untyped + simp only [bind_ok, ite_eq_right hfi, ↓reduceIte, hset] + +theorem ut_reset_smallA {s s2 : S} {a : A} {x p sb fi n : U64} (hc : capA st s x = .Untyped p sb fi false) + (hfi : fi ≠ 0#u64) (hsb : sb < untyped.RESET_CHUNK_BITS) (hn : (1#u64 <<< (sb &&& 63#u64) : Result U64) = ok n) + (hset : obj.Slot.set_cap st x (L.zr s p n) (.Untyped p sb 0#u64 false) = ok s2) : + untyped.reset_untyped st ar s a x = ok (core.result.Result.Ok (), s2, a) := by + unfold untyped.reset_untyped + rw [ut_slot_capA hc, bind_ok] + unfold untyped.as_untyped + simp only [bind_ok, ite_eq_right hfi, Bool.false_eq_true, ↓reduceIte, ite_eq_left hsb, lift, hn, L.clear_free_eq, hset] + +theorem ut_reset_chunkedA {s : S} {a : A} {x p sb fi i i1 : U64} (hc : capA st s x = .Untyped p sb fi false) + (hfi : fi ≠ 0#u64) (hsb : ¬ sb < untyped.RESET_CHUNK_BITS) (hi : (fi - 1#u64 : Result U64) = ok i) + (h1 : (untyped.RESET_CHUNK - 1#u64 : Result U64) = ok i1) : + untyped.reset_untyped st ar s a x = + untyped.reset_untyped_loop st ar s a x p sb false (i &&& ~~~i1) := by + unfold untyped.reset_untyped + rw [ut_slot_capA hc, bind_ok] + unfold untyped.as_untyped + simp only [bind_ok, ite_eq_right hfi, Bool.false_eq_true, ↓reduceIte, ite_eq_right hsb, hi, h1, lift] + +/-- The state after one chunk: the chunk zeroed and the free index written. -/ +theorem ut_reset_stepA {s s2 : S} {a : A} {x p sb off : U64} {dev : Bool} + (hset : obj.Slot.set_cap st x (L.zr s (core.num.U64.wrapping_add p off) untyped.RESET_CHUNK) + (.Untyped p sb off dev) = ok s2) (hcap : capA st s2 x = .Untyped p sb off dev) : + untyped.reset_untyped_loop.body st ar x p sb dev s a off = + (if off = 0#u64 then ok (ControlFlow.done (core.result.Result.Ok (), s2, a)) else do + let (r, s3, a1) ← sched.preemption_point st ar s2 a + let cf ← core.result.Result.Insts.CoreOpsTry.branch r + match cf with + | core.ops.control_flow.ControlFlow.Continue _ => + ok (ControlFlow.cont (s3, a1, core.num.U64.saturating_sub off untyped.RESET_CHUNK)) + | core.ops.control_flow.ControlFlow.Break residual => + let r1 ← core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual + Unit (core.convert.FromSame error.Exc) residual + ok (ControlFlow.done (r1, s3, a1))) := by + unfold untyped.reset_untyped_loop.body + simp only [lift, bind_ok, L.clear_free_eq, hset] + split + · rfl + rw [ut_slot_capA hcap, bind_ok] + unfold untyped.as_untyped + simp only [bind_ok, core.option.Option.is_none, Option.isNone, Bool.false_eq_true, ↓reduceIte] + rfl + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtResetSpecA.lean b/proofs/Fl4ProofsA64/P3/UtResetSpecA.lean new file mode 100644 index 000000000..b2f381c54 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtResetSpecA.lean @@ -0,0 +1,269 @@ +/- +P3 (aarch64, A64-P3 Untyped W2, spec side): **what spec `clear_memory` and `delete_objects` do exactly** (port of +`Fl4Proofs/P3/UtResetSpecJ`'s memory part). + +* `storeWord0_out`, `mapStore0_out`: storing zeros over a list of words sets their bytes to zero and keeps every other + part of the machine state. +* `upto8_bytes`: the bytes of the words of `uptoEnumStep a (a+8) c` are the range `[a, c]`. +* `clear_id`: `clear_memory` over bytes already zero is the identity. +* **`deleteObjects_outA`**: every outcome of `delete_objects` is `utDelStA`: the detype of the region with the region's + bytes zeroed. + +No named premise. New definitions and lemmas only. +-/ +import Fl4SpecA64 +import Fl4ProofsA64.P3.UtClosA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Classical + +noncomputable section + +section Mach +open Fl4SpecA64 Fl4Spec + +/-- The bytes of the 8-byte word at `q`. -/ +def B8 (q y : Word) : Prop := ∃ k : Nat, k < 8 ∧ y = q + BitVec.ofNat 64 k + +theorem fold_upd0 (q : Word) (v : Int → Word8) (hv : ∀ i, v i = 0) : + ∀ (l : List Int) (m : Word → Word8), + l.foldl (fun m i => Function.update m (q + BitVec.ofInt 64 i) (v i)) m = + fun y => if ∃ i ∈ l, y = q + BitVec.ofInt 64 i then 0 else m y + | [], m => by funext y; simp + | i :: l, m => by + simp only [List.foldl] + rw [fold_upd0 q v hv l] + funext y + have hiff : (∃ j ∈ i :: l, y = q + BitVec.ofInt 64 j) ↔ y = q + BitVec.ofInt 64 i ∨ + ∃ j ∈ l, y = q + BitVec.ofInt 64 j := by + constructor + · rintro ⟨j, hj, e⟩ + rcases List.mem_cons.1 hj with rfl | hj + · exact Or.inl e + · exact Or.inr ⟨j, hj, e⟩ + · rintro (e | ⟨j, hj, e⟩) + · exact ⟨i, List.mem_cons.2 (Or.inl rfl), e⟩ + · exact ⟨j, List.mem_cons.2 (Or.inr hj), e⟩ + by_cases h1 : ∃ j ∈ l, y = q + BitVec.ofInt 64 j + · rw [ite_eq_left h1, ite_eq_left (hiff.2 (Or.inr h1))] + · rw [ite_eq_right h1] + by_cases h2 : y = q + BitVec.ofInt 64 i + · rw [ite_eq_left (hiff.2 (Or.inl h2)), h2, Function.update_self, hv] + · rw [ite_eq_right (fun h => (hiff.1 h).elim h2 h1), Function.update_of_ne h2] + +theorem intUpto_bytes (q y : Word) : (∃ i ∈ intUpto 0 7, y = q + BitVec.ofInt 64 i) ↔ B8 q y := by + have h8 : ((7 : Int) - 0 + 1).toNat = 8 := rfl + unfold intUpto + rw [h8] + constructor + · rintro ⟨i, hi, e⟩ + obtain ⟨k, hk, rfl⟩ := List.mem_map.1 hi + refine ⟨k, List.mem_range.1 hk, ?_⟩ + rw [e]; congr 1; simp + · rintro ⟨k, hk, e⟩ + refine ⟨(0 : Int) + (k : Int), List.mem_map.2 ⟨k, List.mem_range.2 hk, rfl⟩, ?_⟩ + rw [e]; congr 1; simp + +theorem getD_zero8 : ∀ (l : List Word8) (k : Nat), (∀ a ∈ l, a = 0) → l.getD k 0 = 0 + | [], _, _ => rfl + | a :: _, 0, h => h a (List.mem_cons.2 (Or.inl rfl)) + | _ :: l, k + 1, h => getD_zero8 l k (fun b hb => h b (List.mem_cons.2 (Or.inr hb))) + +theorem wordRsplit0_getD (k : Nat) : ((wordRsplit (0 : Word) : List Word8)).getD k 0 = 0 := by + refine getD_zero8 _ k (fun a ha => ?_) + unfold wordRsplit at ha + obtain ⟨j, -, rfl⟩ := List.mem_map.1 ha + simp + +/-- **Storing a zero word** (module docstring). -/ +theorem storeWord0_out {q : Word} {ms : MachineState} {x : Unit × MachineState} + (hx : x ∈ (storeWord q 0 ms).1) : + x.2 = { ms with underlyingMemory := fun y => if B8 q y then 0 else ms.underlyingMemory y } := by + unfold storeWord at hx + obtain ⟨z, hz, hx⟩ := hx + obtain ⟨-, rfl⟩ := hz + rw [Set.mem_singleton_iff.1 hx] + dsimp only + rw [fold_upd0 q _ (fun i => wordRsplit0_getD _)] + refine congrArg (fun u => ({ ms with underlyingMemory := u } : MachineState)) (funext fun y => ?_) + by_cases h : B8 q y + · rw [ite_eq_left ((intUpto_bytes q y).2 h), ite_eq_left h] + · rw [ite_eq_right (fun h' => h ((intUpto_bytes q y).1 h')), ite_eq_right h] + +/-- **Storing zeros over a list of words** (module docstring). -/ +theorem mapStore0_out : ∀ (l : List Word) (ms : MachineState) (x : Unit × MachineState), + x ∈ (NonDet.mapMx (fun p => storeWord p 0) l ms).1 → + x.2 = { ms with underlyingMemory := fun y => if ∃ q ∈ l, B8 q y then 0 else ms.underlyingMemory y } + | [], ms, x, hx => by + rw [Set.mem_singleton_iff.1 hx] + have e : (fun y => if ∃ q ∈ ([] : List Word), B8 q y then (0 : Word8) else ms.underlyingMemory y) = + ms.underlyingMemory := funext fun y => by simp + rw [e] + | a :: l, ms, x, hx => by + obtain ⟨z, hz, hx⟩ := hx + rw [mapStore0_out l z.2 x hx, storeWord0_out hz] + refine congrArg (fun u => ({ ms with underlyingMemory := u } : MachineState)) (funext fun y => ?_) + show (if ∃ q ∈ l, B8 q y then (0 : Word8) else (if B8 a y then 0 else ms.underlyingMemory y)) = _ + have hiff : (∃ q ∈ a :: l, B8 q y) ↔ B8 a y ∨ ∃ q ∈ l, B8 q y := by + constructor + · rintro ⟨q, hq, e⟩ + rcases List.mem_cons.1 hq with rfl | hq + · exact Or.inl e + · exact Or.inr ⟨q, hq, e⟩ + · rintro (e | ⟨q, hq, e⟩) + · exact ⟨a, List.mem_cons.2 (Or.inl rfl), e⟩ + · exact ⟨q, List.mem_cons.2 (Or.inr hq), e⟩ + by_cases h1 : ∃ q ∈ l, B8 q y + · rw [ite_eq_left h1, ite_eq_left (hiff.2 (Or.inr h1))] + · rw [ite_eq_right h1] + by_cases h2 : B8 a y + · rw [ite_eq_left h2, ite_eq_left (hiff.2 (Or.inl h2))] + · rw [ite_eq_right h2, ite_eq_right (fun h => (hiff.1 h).elim h2 h1)] + +theorem toNat_add_n_sub1 {a : Word} {n : Nat} (hn0 : 0 < n) (hfit : a.toNat + n ≤ 2 ^ 64) : + (a + BitVec.ofNat 64 n - 1).toNat = a.toNat + n - 1 := by + have hn : n ≤ 2 ^ 64 := by omega + rcases Nat.lt_or_ge n (2 ^ 64) with hlt | hge + · rw [BitVec.toNat_sub, BitVec.toNat_add, BitVec.toNat_ofNat, Nat.mod_eq_of_lt hlt] + have h1 : (1 : Word).toNat = 1 := rfl + rw [h1] + rcases Nat.lt_or_ge (a.toNat + n) (2 ^ 64) with h | h + · rw [Nat.mod_eq_of_lt h]; omega + · have : a.toNat + n = 2 ^ 64 := by omega + rw [this, Nat.mod_self]; omega + · have hn' : n = 2 ^ 64 := by omega + have ha : a.toNat = 0 := by omega + subst hn' + have : a = 0 := BitVec.eq_of_toNat_eq (by rw [ha]; rfl) + subst this + decide + +/-- **The bytes of the words of `uptoEnumStep a (a + 8) c`** are `[a, c]` +(`c = a + n - 1`, `8 ∣ a`, `8 ∣ n`, no wrap). -/ +theorem upto8_bytes {a c : Word} {n : Nat} (ha : a.toNat % 8 = 0) (hn : n % 8 = 0) (hn0 : 0 < n) + (hfit : a.toNat + n ≤ 2 ^ 64) (hc : c.toNat = a.toNat + n - 1) (y : Word) : + (∃ q ∈ uptoEnumStep a (a + BitVec.ofNat 64 wordSize) c, B8 q y) ↔ + a.toNat ≤ y.toNat ∧ y.toNat < a.toNat + n := by + have h64 : (2 : Nat) ^ 64 = 18446744073709551616 := by norm_num + have hca : ¬ c < a := by rw [BitVec.lt_def, hc]; omega + have hb : a + BitVec.ofNat 64 wordSize - a = 8#64 := by + rw [add_sub_cancel_left]; rfl + have hd : ((c - a) / 8#64).toNat = (n - 1) / 8 := by + rw [BitVec.toNat_udiv, BitVec.toNat_sub_of_le (by rw [BitVec.le_def, hc]; omega), hc] + have : (8#64 : Word).toNat = 8 := rfl + rw [this]; congr 1; omega + unfold uptoEnumStep uptoEnum + rw [ite_eq_right hca, hb, hd] + have h0 : (0 : Word).toNat = 0 := rfl + rw [h0] + have hy := y.isLt + constructor + · rintro ⟨q, hq, k, hk, rfl⟩ + obtain ⟨x, hx, rfl⟩ := List.mem_map.1 hq + obtain ⟨j, hj, rfl⟩ := List.mem_map.1 hx + rw [List.mem_range'_1] at hj + have hj8 : 8 * j + 8 ≤ n := by omega + have e : (a + BitVec.ofNat 64 j * 8#64 + BitVec.ofNat 64 k).toNat = a.toNat + 8 * j + k := by + simp only [BitVec.toNat_add, BitVec.toNat_mul, BitVec.toNat_ofNat, h64, Nat.reduceMod] + rw [h64] at hfit + omega + rw [e]; omega + · rintro ⟨h1, h2⟩ + set d := y.toNat - a.toNat with hd' + refine ⟨a + BitVec.ofNat 64 (d / 8) * 8#64, List.mem_map.2 ⟨BitVec.ofNat 64 (d / 8), + List.mem_map.2 ⟨d / 8, by rw [List.mem_range'_1]; omega, rfl⟩, rfl⟩, d % 8, Nat.mod_lt _ (by decide), ?_⟩ + apply BitVec.eq_of_toNat_eq + simp only [BitVec.toNat_add, BitVec.toNat_mul, BitVec.toNat_ofNat, h64, Nat.reduceMod] + rw [h64] at hfit hy + omega + +/-- `doMachineOp`'s outcomes. -/ +theorem dmo_mem {α : Type} {mop : NonDet MachineState α} {σ : State Unit} {z : α × State Unit} + (hz : z ∈ (doMachineOp (Z := Unit) mop σ).1) : + ∃ ms', (z.1, ms') ∈ (mop σ.machineState).1 ∧ z.2 = { σ with machineState := ms' } := by + unfold doMachineOp at hz + obtain ⟨a, ha, hz⟩ := hz + rw [Set.mem_singleton_iff.1 ha] at hz + obtain ⟨⟨⟨r, ms'⟩, s'⟩, ⟨hb1, hb2⟩, hz⟩ := hz + simp only at hb2 + subst hb2 + obtain ⟨c, hc, hz⟩ := hz + rw [Set.mem_singleton_iff.1 hc] at hz + rw [Set.mem_singleton_iff.1 hz] + exact ⟨ms', hb1, rfl⟩ + +theorem dmo_has {α : Type} {mop : NonDet MachineState α} {σ : State Unit} {r : α} {ms' : MachineState} + (h : (r, ms') ∈ (mop σ.machineState).1) : + (r, { σ with machineState := ms' }) ∈ (doMachineOp (Z := Unit) mop σ).1 := + ⟨(σ.machineState, σ), Set.mem_singleton _, ((r, ms'), σ), ⟨h, rfl⟩, ((), { σ with machineState := ms' }), + Set.mem_singleton _, Set.mem_singleton _⟩ + +/-- **Clearing bytes already zero** is the identity (module docstring). -/ +theorem clear_id {a : Word} {n : Nat} (ha : a.toNat % 8 = 0) (hn : n % 8 = 0) (hn0 : 0 < n) + (hfit : a.toNat + n ≤ 2 ^ 64) {σ : State Unit} + (hz : ∀ y : Word, a.toNat ≤ y.toNat → y.toNat < a.toNat + n → σ.machineState.underlyingMemory y = 0) : + ∀ x ∈ (doMachineOp (Z := Unit) (clearMemory a n) σ).1, x.2 = σ := by + intro x hx + obtain ⟨ms', hms, e⟩ := dmo_mem hx + rw [e] + unfold clearMemory at hms + have h1 := mapStore0_out _ _ _ hms + simp only at h1 + rw [h1] + have hfun : (fun y => if ∃ q ∈ uptoEnumStep a (a + BitVec.ofNat 64 wordSize) (a + BitVec.ofNat 64 n - 1), B8 q y + then (0 : Word8) else σ.machineState.underlyingMemory y) = σ.machineState.underlyingMemory := by + funext y + split_ifs with h + · rw [upto8_bytes ha hn hn0 hfit (toNat_add_n_sub1 hn0 hfit) y] at h + exact (hz y h.1 h.2).symm + · rfl + rw [hfun] + +theorem pow2_word {sz : Nat} (_ : sz < 64) : ((2 : Word) ^ sz) = BitVec.ofNat 64 (2 ^ sz) := by + apply BitVec.eq_of_toNat_eq + rw [BitVec.toNat_pow, BitVec.toNat_ofNat] + have : (2 : BitVec 64).toNat = 2 := rfl + rw [this] + +/-- The state after `delete_objects` (`deleteObjects_outA`). -/ +def utDelStA (pu : Word) (sz : Nat) (σ : State Unit) : State Unit := + detype {x | pu ≤ x ∧ x ≤ pu + 2 ^ sz - 1} { σ with machineState := { σ.machineState with underlyingMemory := + fun x => if pu.toNat ≤ x.toNat ∧ x.toNat < pu.toNat + 2 ^ sz then 0 else σ.machineState.underlyingMemory x } } + +/-- **`delete_objects`' outcome** (module docstring). -/ +theorem deleteObjects_outA {pu : Word} {sz : Nat} (hp : pu.toNat % 8 = 0) (hsz : 3 ≤ sz) (hsz' : sz < 64) + (hfit : pu.toNat + 2 ^ sz ≤ 2 ^ 64) (σ : State Unit) {y : Unit × State Unit} + (hy : y ∈ (deleteObjects (Z := Unit) pu sz σ).1) : + y.2 = utDelStA pu sz σ := by + unfold deleteObjects at hy + obtain ⟨z, hz, hy⟩ := hy + rw [Set.mem_singleton_iff.1 hy] + obtain ⟨ms', hms, e⟩ := dmo_mem hz + rw [e] + unfold freeMemory at hms + have h1 := mapStore0_out _ _ _ hms + simp only at h1 + rw [h1] + have hn8 : 2 ^ sz % 8 = 0 := by + have : 8 ∣ 2 ^ sz := by + have := Nat.pow_dvd_pow 2 hsz; simpa using this + omega + have hc : (pu + 2 ^ sz - 1).toNat = pu.toNat + 2 ^ sz - 1 := by + rw [pow2_word hsz']; exact toNat_add_n_sub1 (Nat.two_pow_pos _) hfit + have hfun : (fun x => if ∃ q ∈ uptoEnumStep pu (pu + BitVec.ofNat 64 wordSize) (pu + 2 ^ sz - 1), + B8 q x then (0 : Word8) else σ.machineState.underlyingMemory x) = fun x => + if pu.toNat ≤ x.toNat ∧ x.toNat < pu.toNat + 2 ^ sz then 0 else σ.machineState.underlyingMemory x := by + funext x + have hb := upto8_bytes hp hn8 (Nat.two_pow_pos _) hfit hc x + by_cases h : ∃ q ∈ uptoEnumStep pu (pu + BitVec.ofNat 64 wordSize) (pu + 2 ^ sz - 1), B8 q x + · rw [ite_eq_left h, ite_eq_left (hb.1 h)] + · rw [ite_eq_right h, ite_eq_right (fun h' => h (hb.2 h'))] + rw [hfun] + rfl + +end Mach + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtRetDecVcpuA.lean b/proofs/Fl4ProofsA64/P3/UtRetDecVcpuA.lean new file mode 100644 index 000000000..db3ff20e8 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtRetDecVcpuA.lean @@ -0,0 +1,196 @@ +/- +P3 (aarch64, A64-P3 Untyped W3, closure item E): **a TCB made by the untyped decode has no VCPU**, through the reset +path too, and paired with A64 P1's `UtEnd` (lead 01:35Z). + +* `utRetype_vcpu0_crtA`: `utRetype_vcpu0A` from P1's creation-start record `CrtP`. +* `utCrt_threadA`: a thread cap in a slot that was null at the creation's start is a new TCB (`CrtE`). +* **`utDecode_vcpuA`**: after `untyped::decode` (with or without reset, at every exit), every thread cap in a slot of + `OS` that was null at the decode's start names a TCB whose VCPU word is 0. Under a reset the slot is asked to lie off + the untyped's region. +* **`utEnd_vcpu0A`**: with `UtEnd`'s destinations, every new TCB's VCPU word is 0. + +`utDecode_vcpuA`'s premises are those of P1's `untyped.decode.sysLBH`; it is a second postcondition of that program. +No named premise. +-/ +import Fl4ProofsA64.P3.UtRetVcpuA +import Fl4ProofsA64.P3.UtDecodeA +import Fl4ProofsA64.P1.MdbLiveU +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +theorem ut_alignUp12A (y : BitVec 64) : (Fl4SpecA64.alignUp y 12).toNat % 4096 = 0 := by + have h : Fl4SpecA64.alignUp y 12 &&& (4095#64) = 0#64 := by + unfold Fl4SpecA64.alignUp + have e : BitVec.ofNat 64 (2 ^ 12) - 1 = 4095#64 := by decide + rw [e, BitVec.and_assoc, show ~~~(4095#64) &&& 4095#64 = 0#64 by decide, BitVec.and_zero] + have := congrArg BitVec.toNat h + rw [BitVec.toNat_and] at this + have e1 : (4095#64).toNat = 2 ^ 12 - 1 := by decide + rw [e1, Nat.and_two_pow_sub_one_eq_mod] at this + simpa using this + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + +/-- `utRetype_vcpu0A` from the creation-start record. -/ +theorem utRetype_vcpu0_crtA {H1 : Heap} {Cs Sg OS1 : Set U64} (hA : P0.ArchTotal ar) {s1 : S} + {slot p sb fi1 aligned nt us dest off win : U64} + (C : CrtP st H1 Cs Sg OS1 s1 slot p sb fi1 false aligned nt us dest off win) (hn1 : nt.val = 1) + (hal4 : aligned.val % 4096 = 0) (hb0 : 0 < aligned.val) (a : A) : + untyped.retype st ar s1 a slot false aligned nt us dest off win false ⦃ r => r.1 = .Ok () ∧ + ∀ l, l < win.val → L.mem r.2.1 (wa (bjA aligned (zA nt.val us) l) 3664#u64) = 0#u64 ⦄ := by + obtain ⟨hSA, -, hK⟩ := C.stz + obtain ⟨hS, hR, hV, hUZ, -⟩ := hSA + obtain ⟨-, -, hfi, hA0, hAw, hinj, hcan, -⟩ := C.ar + have hz : 2 ^ (zA nt.val us).val = 4096 := by rw [hn1, zA_1 us]; norm_num + rw [hz] at hAw + have hnode := hK slot C.src p sb fi1 C.cap + have hn0 := C.n0 + have hnA : Aligned (nwA st s1) := nodeAt_aligned hn0 (by rw [debug_val]; omega) (by rw [debug_val]) + have hnv : (nwA st s1).val = L.nodeBase s1 0#u64 + 88 := by + rw [nodeAt_val hn0 (off := layout.node.DEBUG_TCBS) (by rw [debug_val]; omega), debug_val] + have hnF : (nwA st s1).val < p.val + fi1.val ∨ p.val + 2 ^ sb.val ≤ (nwA st s1).val := by + rcases hnode.1 _ C.core0 with g | g + · left; omega + · right; omega + have hbase : L.nodeBase s1 0#u64 = (L.ksb s1).val + 223488 := by + have := hn0.fits + unfold LawfulStore.nodeBase + simp + have hks8 : (L.ksb s1).val % 8 = 0 := by have := hn0.align; omega + have hksw : (L.ksb s1).val + 16 ≤ 2 ^ 64 := by have := hn0.fits; omega + have hksF : (L.ksb s1).val + 8 < p.val + fi1.val ∨ p.val + 2 ^ sb.val ≤ (L.ksb s1).val + 8 := by + rcases hnode.2 with g | g + · left; omega + · right; omega + exact utRetype_vcpu0A hA C.heap C.os hS hR hV hUZ a C.src C.cap nt us dest off win aligned hn1 hal4 hb0 hfi hA0 + hAw hnA C.nc hnF C.hd hks8 hksw hksF C.dO C.dn C.dc hinj hcan C.hus + +/-- A thread cap in a slot that was null at the creation's start is a new TCB. -/ +theorem utCrt_threadA {H1 : Heap} {Sg OS1 : Set U64} {s1 s' : S} {slot p sb : U64} {dev : Bool} + {aligned nt us dest off win : U64} (E : CrtE st H1 Sg OS1 s1 s' slot p sb dev aligned nt us dest off win) + (hn : nt.val < 15) {y t : U64} (hy : y ∈ OS1) (h0 : capA st s1 y = .Null) (ht : capA st s' y = .Thread t) : + nt.val = 1 ∧ ∃ j, j < win.val ∧ t = bjA aligned (zA nt.val us) j := by + have ys : y ≠ slot := fun e => by + obtain ⟨nfi, h⟩ := E.src + rw [e, h] at ht; cases ht + by_cases hd : ∃ j, j < win.val ∧ y = djA dest off j + · obtain ⟨j, hj, rfl⟩ := hd + rw [E.new j hj] at ht + have key : ∀ n, n < 15 → ∀ b : U64, newCapA n b us dev = .Thread t → n = 1 ∧ t = b := by + intro n hn' b h + interval_cases n <;> simp [newCapA] at h; exact ⟨rfl, h.symm⟩ + obtain ⟨h1, e⟩ := key _ hn _ ht + exact ⟨h1, j, hj, e⟩ + · rw [E.keep y hy ys (fun j hj e => hd ⟨j, hj, e⟩), h0] at ht + cases ht + +/-- **After the untyped decode, a thread cap in a formerly null slot names a TCB with no VCPU.** -/ +theorem utDecode_vcpuA {H : Heap} {Cs Rp Nt OS : Set U64} (hA : P0.ArchTotal ar) {U Ac : Set U64} {s0 : S} + {extra : Aeneas.Std.Array obj.Slot 3#usize} {slot p sb fi : U64} {dev : Bool} {ia ie : Nat} + (D : DecPre st H.tcbs Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits U Ac s0 (.Untyped p sb fi dev) extra) + (P : SysPlace st H Cs H.frame Rp Nt OS s0 extra slot) (B : SysL st H Cs OS ia ie s0) + (hcap : obj.Slot.cap st slot s0 = ok (.Untyped p sb fi dev)) (hfm : A64FrameMasks st s0) (a : A) + (lbl len buf : U64) : + untyped.decode st ar s0 a lbl len slot (.Untyped p sb fi dev) buf extra ⦃ _ s' _ => + ∀ y ∈ OS, ((∃ v, cdt.ensure_no_children st s0 slot = ok (.Ok v)) → + y ∈ OffR p.val (p.val + 2 ^ sb.val) OS) → + ∀ t, capA st s0 y = .Null → capA st s' y = .Thread t → L.mem s' (wa t 3664#u64) = 0#u64 ⦄ := by + have R := UtDecP.of_live D B.sys P hcap B.ln B.vr B.ly + have hslot : ∀ y, capA st s0 y = .Null → y ≠ slot := fun y h e => by + rw [e, R.cap] at h; cases h + refine untyped.decode.decG + (P := fun _ s' => ∀ y ∈ OS, ((∃ v, cdt.ensure_no_children st s0 slot = ok (.Ok v)) → + y ∈ OffR p.val (p.val + 2 ^ sb.val) OS) → + ∀ t, capA st s0 y = .Null → capA st s' y = .Thread t → L.mem s' (wa t 3664#u64) = 0#u64) hA a lbl + len slot buf p sb fi dev (fun _ y _ _ t h0 ht => by rw [h0] at ht; cases ht) (fun h => (hfm.noShare h).elim) + fun s2 reset aligned nt us dest off win e hU => ?_ + have hUA := hU + obtain ⟨x, hxm, hx0, idx, dep, val, ers, hv64, erd, ees, hrs, eal, -⟩ := hUA + obtain ⟨hn, hus, hval⟩ := untyped.retype_size.okA ers + have hal4 : nt.val = 1 → aligned.val % 4096 = 0 := by + intro h1 + have e12 : val.val = 12 := by rw [hval, h1]; exact zA_1 us + rw [align_up_eqA _ val hv64] at eal + have := congrArg UScalar.val (okinj eal) + rw [← this, e12] + exact ut_alignUp12A _ + have hb0 : dev = false → 0 < aligned.val := fun hd => by + subst hd + exact lt_of_lt_of_le (B.ly.ut0 slot R.src p sb fi R.cap) (CrtA.of_args R hU).pa + -- the creation from a start record: both reset cases + have create : ∀ (H1 : Heap) (OS1 : Set U64) (s1 : S) (a1 : A) (fi1 : U64) (d : Bool), d = dev → + CrtP st H1 Cs H.frame OS1 s1 slot p sb fi1 d aligned nt us dest off win → + untyped.retype st ar s1 a1 slot false aligned nt us dest off win d ⦃ r => + ∀ y ∈ OS1, ∀ t, capA st s1 y = .Null → capA st r.2.1 y = .Thread t → + L.mem r.2.1 (wa t 3664#u64) = 0#u64 ⦄ := by + intro H1 OS1 s1 a1 fi1 d hd C + cases d + · have hb := hb0 hd.symm + by_cases h1 : nt.val = 1 + · refine WP.spec_mono (WP.spec_and (utRetype_vcpu0_crtA hA C h1 (hal4 h1) hb a1) + (untyped.retype.liveRWH hA C hb a1)) ?_ + rintro r ⟨⟨-, hv⟩, E, -⟩ y hy t h0 ht + obtain ⟨-, j, hj, rfl⟩ := utCrt_threadA E hn hy h0 ht + exact hv j hj + · refine WP.spec_mono (untyped.retype.liveRWH hA C hb a1) ?_ + rintro r ⟨E, -⟩ y hy t h0 ht + exact absurd (utCrt_threadA E hn hy h0 ht).1 h1 + · refine WP.spec_mono (untyped.retype.liveD C a1) ?_ + rintro r ⟨E, -⟩ y hy t h0 ht + exact absurd (utCrt_threadA E hn hy h0 ht).1 (C.ar.1 rfl).1 + obtain ⟨Ac', hb⟩ := R.blk + obtain ⟨Q, Rq, q⟩ := hb.1 + obtain ⟨k, w⟩ := restart_frame (OS := OS) R.os q e + cases reset + · have C := CrtP.of0 R e hU + refine WP.spec_mono (create H OS s2 a fi dev rfl C) ?_ + rintro r h y hy - t h0 ht + exact h y hy t ((k.capA hy).trans h0) ht + · have he := hrs.1 rfl + cases dev + · refine WP.spec_mono (untyped.retype.stZFCRUG hA R e a hU + (fun _ _ s' => ∀ y ∈ OS, ((∃ v, cdt.ensure_no_children st s0 slot = ok (.Ok v)) → + y ∈ OffR p.val (p.val + 2 ^ sb.val) OS) → + ∀ t, capA st s0 y = .Null → capA st s' y = .Thread t → L.mem s' (wa t 3664#u64) = 0#u64) + (fun s1 i _ _ keep y hy hoff t h0 ht => by + rw [keep y (hoff he) (hslot y h0), h0] at ht; cases ht) + (fun s1 a1 _ keep C H' _ _ _ _ => WP.spec_mono (create _ _ s1 a1 _ false rfl C) fun r h y _ hoff t h0 ht => + h y (hoff he) t ((keep y (hoff he) (hslot y h0)).trans h0) ht)) ?_ + rintro ⟨r, s', a'⟩ ⟨H', OS', -, -, -, -, hK⟩ + exact hK + · refine WP.spec_mono (untyped.retype.stZFCRdUG hA R e a hU + (fun _ _ s' => ∀ y ∈ OS, ((∃ v, cdt.ensure_no_children st s0 slot = ok (.Ok v)) → + y ∈ OffR p.val (p.val + 2 ^ sb.val) OS) → + ∀ t, capA st s0 y = .Null → capA st s' y = .Thread t → L.mem s' (wa t 3664#u64) = 0#u64) + (fun s1 a1 _ keep C H' _ _ _ _ => WP.spec_mono (create _ _ s1 a1 _ true rfl C) fun r h y hy _ t h0 ht => + h y hy t ((keep y hy (hslot y h0)).trans h0) ht)) ?_ + rintro ⟨r, s', a'⟩ ⟨H', OS', -, -, -, -, hK⟩ + exact hK + +/-- **Paired with `UtEnd`**: every new TCB of the decode has no VCPU. `hpost` is `utDecode_vcpuA`'s post. -/ +theorem utEnd_vcpu0A {H H1 H' : Heap} {Cs OS OS1 : Set U64} {s0 s1 s' : S} {slot p sb : U64} {dev : Bool} + {lo hi : Nat} {aligned nt us dest off win : U64} + (U : UtEnd st H Cs OS s0 slot p sb dev H1 H' OS1 s1 s' lo hi aligned nt us dest off win) (h1 : nt.val = 1) + (hoff : (∃ v, cdt.ensure_no_children st s0 slot = ok (.Ok v)) → ∀ j, j < win.val → + djA dest off j ∈ OffR p.val (p.val + 2 ^ sb.val) OS) + (hpost : ∀ y ∈ OS, ((∃ v, cdt.ensure_no_children st s0 slot = ok (.Ok v)) → + y ∈ OffR p.val (p.val + 2 ^ sb.val) OS) → + ∀ t, capA st s0 y = .Null → capA st s' y = .Thread t → L.mem s' (wa t 3664#u64) = 0#u64) : + ∀ j, j < win.val → L.mem s' (wa (bjA aligned (zA nt.val us) j) 3664#u64) = 0#u64 := by + intro j hj + refine hpost _ (U.os _ (U.dO j hj)) (fun he => hoff he j hj) _ (U.A.dn0 j hj) ?_ + rw [U.E.new j hj, h1] + simp [newCapA] + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtRetVcpuA.lean b/proofs/Fl4ProofsA64/P3/UtRetVcpuA.lean new file mode 100644 index 000000000..b5034a801 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtRetVcpuA.lean @@ -0,0 +1,214 @@ +/- +P3 (aarch64, A64-P3 Untyped W3, closure item E): **every TCB a Retype makes has no VCPU**, carried along FL4's +creation loop (lead 01:24Z). + +A64 P1's creation invariants do not fix a new TCB's VCPU word (offset 3664 is among `TcbInitK`'s words). The loop is +run with `ZTc` (P1: the uncreated part is zero, the debug head is 0, an old TCB or an earlier new one) strengthened by +"every window's VCPU word is zero": `ut_createTcbA`'s write set misses all of them. + +`utRetype_vcpu0A`: after a non-reset Retype of TCBs, `mem (bj l + 3664) = 0` for every new TCB. It is a second +postcondition of the same program `untyped.retype`, to be paired with A64 P1's `UtEnd` at the decode. + +Case hypotheses: those of P1's `untyped.retype.mdbSTVU`, with the objects' 4 KiB alignment and nonzero base (the +decode's `align_up`) and the global `CUR_DOMAIN` word off the untyped's free part. No named premise. +-/ +import Fl4ProofsA64.P3.UtRtFrA +import Fl4ProofsA64.P1.MdbUZT +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-- A word at offset `k` of window `j` of `n` 4 KiB windows. -/ +theorem ut_inWinA (b j n k : Nat) (h : j < n) (hk : k < 4096) : + b + j * 4096 ≤ b + j * 4096 + k ∧ b + j * 4096 + k < b + n * 4096 := by + have : (j + 1) * 4096 ≤ n * 4096 := Nat.mul_le_mul_right _ h + omega + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + +theorem utRetype_vcpu0A {OS : Set U64} {H : Heap} {Sg : Set U64} (hA : P0.ArchTotal ar) (hH : HeapOK H) + (hO : OSOK H.tcbs H.omega Sg OS) {s : S} (hS : MdbST st OS s) (hR : UtRev st OS s) + (hV : ValidUntypedA st H OS s) (hUZ : UtZeroA st OS s) (a : A) {src p sb fi : U64} (hsrc : src ∈ OS) + (hcs : capA st s src = .Untyped p sb fi false) (nt us dcn off len base : U64) (hn1 : nt.val = 1) + (hal4 : base.val % 4096 = 0) (hb0 : 0 < base.val) (hfi : fi.val < 2 ^ sb.val) + (hA0 : p.val + fi.val ≤ base.val) (hAw : base.val + len.val * 4096 ≤ p.val + 2 ^ sb.val) + (hnA : Aligned (nwA st s)) (hnC : ¬ W7.CapW H.tcbs H.omega Sg (nwA st s)) + (hnF : (nwA st s).val < p.val + fi.val ∨ p.val + 2 ^ sb.val ≤ (nwA st s).val) + (hhd0 : L.mem s (nwA st s) = 0#u64 ∨ H (L.mem s (nwA st s)) = some .tcb) + (hks8 : (L.ksb s).val % 8 = 0) (hksw : (L.ksb s).val + 16 ≤ 2 ^ 64) + (hksF : (L.ksb s).val + 8 < p.val + fi.val ∨ p.val + 2 ^ sb.val ≤ (L.ksb s).val + 8) + (hdO : ∀ j, j < len.val → djA dcn off j ∈ OS) + (hdn : ∀ j, j < len.val → capA st s (djA dcn off j) = .Null) + (hdc : ∀ j, j < len.val → obj.sext48 (djA dcn off j) = ok (djA dcn off j)) + (hinj : ∀ j l, j < len.val → l < len.val → djA dcn off j = djA dcn off l → j = l) + (hcan : ∀ j, j < len.val → CapRt.Canonical (bjA base (zA nt.val us) j)) (hus : us.val < 64) : + untyped.retype st ar s a src false base nt us dcn off len false ⦃ r => r.1 = .Ok () ∧ + ∀ l, l < len.val → L.mem r.2.1 (wa (bjA base (zA nt.val us) l) 3664#u64) = 0#u64 ⦄ := by + have hz12 : (zA nt.val us).val = 12 := by rw [hn1]; exact zA_1 us + have hX : 2 ^ (zA nt.val us).val = 4096 := by rw [hz12]; norm_num + obtain ⟨G, par, hM, -, -, -, -, -, hU⟩ := id hS + obtain ⟨-, -, hpf⟩ := ut_rangeA (capA_ut_b hcs) (by + have := hU.reg src hsrc (by rw [hcs]; exact physA_ut _ _ _ _); rwa [hcs] at this) + -- the new TCBs and their VCPU words + have hbj : ∀ l, l < len.val → (bjA base (zA nt.val us) l).val = base.val + l * 4096 ∧ + base.val + l * 4096 + 4096 ≤ p.val + 2 ^ sb.val := by + intro l hl + have h1 : (l + 1) * 4096 ≤ len.val * 4096 := Nat.mul_le_mul_right _ hl + have := bjA_val (base := base) (z := zA nt.val us) (j := l) (by rw [hX]; omega) + rw [hX] at this + exact ⟨this, by omega⟩ + have htcb : ∀ l, l < len.val → TcbAddr (bjA base (zA nt.val us) l) := by + intro l hl + obtain ⟨hv, hle⟩ := hbj l hl + refine ⟨by omega, ?_, by omega⟩ + rw [hv]; omega + have hvc : ∀ l, l < len.val → (wa (bjA base (zA nt.val us) l) 3664#u64).val = base.val + l * 4096 + 3664 := by + intro l hl + rw [ut_tcbWordA (htcb l hl) (by simp), (hbj l hl).1]; simp + have hvcA : ∀ l, l < len.val → Aligned (wa (bjA base (zA nt.val us) l) 3664#u64) := by + intro l hl + show _ % 8 = 0 + rw [hvc l hl]; omega + have hvcC : ∀ l, l < len.val → ¬ W7.CapW H.tcbs H.omega Sg (wa (bjA base (zA nt.val us) l) 3664#u64) := by + intro l hl + have := (hbj l hl).2 + exact in_free_not_capw hH hV hsrc hcs (by rw [hvc l hl]; omega) (by rw [hvc l hl]; omega) + let V : S → Prop := fun s' => ∀ l, l < len.val → L.mem s' (wa (bjA base (zA nt.val us) l) 3664#u64) = 0#u64 + let Zq : S → Nat → Prop := fun s' j => ZTc st H s base (zA nt.val us) len.val s' j ∧ V s' + have hAw' : base.val + len.val * 2 ^ (zA nt.val us).val ≤ p.val + 2 ^ sb.val := by rw [hX]; exact hAw + have step : ∀ (s' : S) (a' : A) (j : Nat), j < len.val → Zq s' j → + objects.create_object st ar s' a' nt (bjA base (zA nt.val us) j) us false ⦃ r => + r.1 = newCapA nt.val (bjA base (zA nt.val us) j) us false ∧ OSKeep st OS s' r.2.1 ∧ Zq r.2.1 (j + 1) ⦄ := by + intro s' a' j hj ⟨hZ, hVs⟩ + have P1s := create_object.hCOZ (ar := ar) hA hH hO hV hsrc hcs nt us len base false (by omega) + (fun h => by cases h) (by omega) hA0 hAw' hpf hnA hnC hnF s' a' j hj hZ + obtain ⟨hk, hzero, hhd⟩ := hZ + have hnw : nwA st s' = nwA st s := nodeAt_of_ksb hk _ _ + obtain ⟨hv, hle⟩ := hbj j hj + have ht := htcb j hj + have haff : L.mem s' (wa (bjA base (zA nt.val us) j) 3520#u64) = 0#u64 := by + have e : (wa (bjA base (zA nt.val us) j) 3520#u64).val = base.val + j * 4096 + 3520 := by + rw [ut_tcbWordA ht (by simp), hv]; simp + have h1 : (j + 1) * 4096 ≤ len.val * 4096 := Nat.mul_le_mul_right _ hj + generalize wa (bjA base (zA nt.val us) j) 3520#u64 = w at e ⊢ + have al : Aligned w := by show _ % 8 = 0; omega + generalize 2 ^ (zA nt.val us).val = X at hzero hX + subst hX + have g := ut_inWinA base.val j len.val 3520 hj (by norm_num) + rw [← e] at g + exact hzero w al g.1 g.2 + -- node 0's base: its word 88 is the debug head + have hnb : obj.node_base st s' 0#u64 = ok (core.num.U64.wrapping_add + (core.num.U64.wrapping_add (L.ksb s') 223488#u64) (core.num.U64.wrapping_mul 0#u64 4288#u64)) := by + unfold obj.node_base + rw [L.ks_base_eq, eq_of_spec_fd Fl4ProofsA64.P0.Consts.layout.ks.NODES.spec, + eq_of_spec_fd Fl4ProofsA64.P0.Consts.layout.node.SIZE.spec] + simp only [bind_ok, lift] + generalize hnbv : core.num.U64.wrapping_add (core.num.U64.wrapping_add (L.ksb s') 223488#u64) + (core.num.U64.wrapping_mul 0#u64 4288#u64) = nb at hnb + have h88 : wa nb 88#u64 = nwA st s := by + rw [← hnw, ← hnbv] + have hD : layout.node.DEBUG_TCBS = 88#u64 := UScalar.eq_of_val_eq (by rw [debug_val]; simp) + unfold nwA LawfulStore.nodeAt + rw [hD] + have offW : ∀ b : U64, (b.val < p.val + fi.val ∨ p.val + 2 ^ sb.val ≤ b.val) → + utOffTcbA (bjA base (zA nt.val us) j) b := fun b h => by + unfold utOffTcbA + rw [hv]; omega + have hd8 : (wa (L.ksb s') 8#u64).val = (L.ksb s).val + 8 := by + rw [hk, wa_val] + have : (8#u64 : U64).val = 8 := by simp + rw [this]; exact Nat.mod_eq_of_lt (by omega) + -- the old head: 0, an old TCB (off the free part) or an earlier new one + have head : L.mem s' (nwA st s) ≠ 0#u64 → + Aligned (wa (L.mem s' (nwA st s)) 3584#u64) ∧ + utOffTcbA (bjA base (zA nt.val us) j) (wa (L.mem s' (nwA st s)) 3584#u64) ∧ + ∀ l, l < len.val → wa (bjA base (zA nt.val us) l) 3664#u64 ≠ wa (L.mem s' (nwA st s)) 3584#u64 := by + intro h0 + rcases hhd with e | e | ⟨j', hj', e⟩ + · exact absurd e h0 + · generalize L.mem s' (nwA st s) = h at e + have hf := hH.fits _ _ e + have ha := hH.align _ _ e + simp only [HKind.bits] at hf ha + have e3 : (wa h 3584#u64).val = h.val + 3584 := by + rw [wa_val] + have : (3584#u64 : U64).val = 3584 := by simp + rw [this]; exact Nat.mod_eq_of_lt (by omega) + have g := hV src hsrc p sb fi hcs _ .tcb e + simp only [HKind.bits] at g + have off : (wa h 3584#u64).val < p.val + fi.val ∨ p.val + 2 ^ sb.val ≤ (wa h 3584#u64).val := by + rw [e3]; rcases g with g | g <;> omega + refine ⟨by show _ % 8 = 0; rw [e3]; omega, offW _ off, fun l hl heq => ?_⟩ + have := congrArg UScalar.val heq + have := (hbj l hl).2 + rw [hvc l hl] at * + omega + · obtain ⟨hv', hle'⟩ := hbj j' (by omega) + have e3 : (wa (bjA base (zA nt.val us) j') 3584#u64).val = base.val + j' * 4096 + 3584 := by + rw [ut_tcbWordA (htcb j' (by omega)) (by simp), hv']; simp + have hjj : (j' + 1) * 4096 ≤ j * 4096 := Nat.mul_le_mul_right _ hj' + rw [e] + refine ⟨by show _ % 8 = 0; rw [e3]; omega, by unfold utOffTcbA; rw [e3, hv]; omega, fun l hl heq => ?_⟩ + have := congrArg UScalar.val heq + rw [hvc l hl, e3] at this + omega + have M := ut_createTcbA (st := st) ar s' a' nt (bjA base (zA nt.val us) j) us nb false hn1 ht haff hnb + (by rw [h88]; exact hnA) (by rw [h88]; exact offW _ hnF) + (by show _ % 8 = 0; rw [hd8]; omega) (offW _ (by rw [hd8]; exact hksF)) + (by rw [h88]; exact fun h0 => ⟨(head h0).1, (head h0).2.1⟩) + refine WP.spec_mono (spec_andA P1s M) ?_ + rintro r ⟨⟨hc, hk', hZ'⟩, -, -, hw, -, -⟩ + refine ⟨hc, hk', hZ', fun l hl => ?_⟩ + rw [h88] at hw + by_cases hlj : l = j + · subst hlj + exact ut_createTcb_vcpuA (nb := nb) (hd := L.mem s' (nwA st s)) ht (by rw [h88]; exact hw) + (by rw [h88]; exact offW _ hnF) (fun h0 => (head h0).2.1) (hVs l hl) + · rw [hw.mem_eq (hvcA l hl) ?_] + · exact hVs l hl + have := (hbj l hl).2 + rintro (hi | e | ⟨h0, e⟩) + · apply utTcbInit_inA ht hi + unfold utOffTcbA + rw [hvc l hl, hv] + rcases Nat.lt_or_ge l j with g | g + · have : (l + 1) * 4096 ≤ j * 4096 := Nat.mul_le_mul_right _ g + omega + · have : (j + 1) * 4096 ≤ l * 4096 := Nat.mul_le_mul_right _ (by omega) + omega + · have e' := congrArg UScalar.val e + rw [hvc l hl] at e' + rcases hnF with g | g <;> omega + · exact (head h0).2.2 l hl e + have kW : ∀ (s1 s2 : S) (j : Nat), Zq s1 j → WritesIn st s1 s2 (W7.CapW H.tcbs H.omega Sg) → Zq s2 j := by + intro s1 s2 j ⟨hZ, hVs⟩ w + refine ⟨ZTc.capw hH hO hV hsrc hcs hA0 hAw' hnA hnC hZ w, fun l hl => ?_⟩ + rw [w.mem_eq (hvcA l hl) (hvcC l hl)] + exact hVs l hl + have k0 : ∀ s1 : S, WritesIn st s s1 (fun b => b = src ∨ b = wa src 8#u64) → Zq s1 0 := by + intro s1 w + refine ⟨ZTc.start hH hO hV hsrc hcs hA0 hAw' hnA hnC hUZ hhd0 w, fun l hl => ?_⟩ + have hW : WritesIn st s s1 (W7.CapW H.tcbs H.omega Sg) := w.mono fun b hb => by + rcases hb with e | e + · subst e; exact hO.cw0 hsrc + · subst e; exact hO.cw hsrc (by simp) (by simp) + have := (hbj l hl).2 + rw [hW.mem_eq (hvcA l hl) (hvcC l hl)] + exact hUZ src hsrc p sb fi hcs _ (hvcA l hl) (by rw [hvc l hl]; omega) (by rw [hvc l hl]; omega) + refine WP.spec_mono (untyped.retype.mdbSTVZ ar hO (H' := H) hS hR hV a hsrc hcs nt us dcn off len base false rfl + (by omega) (by omega) hfi hA0 hAw' (fun _ _ h => Or.inl h) (Zq := Zq) step kW k0 hdO hdn hdc hinj hcan hus) ?_ + rintro r ⟨h1, -, -, -, -, hVf⟩ + exact ⟨h1, hVf⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtRstReadyA.lean b/proofs/Fl4ProofsA64/P3/UtRstReadyA.lean new file mode 100644 index 000000000..7f29ef5c2 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtRstReadyA.lean @@ -0,0 +1,242 @@ +/- +P3 (aarch64, A64-P3 Untyped): **the reset's inputs after the caller's restart** (`UtRstReadyA`, the premise +`hready` of `utRetype_resetA`), from the same facts at the decode's start and `JStage3A.of_restartCurA`. + +* `utNoChild_congrA`: `ensure_no_children` reads the slot's words and its successor's. +* **`utRstReady_restartA`**: FL4's and the spec's `set_thread_state Restart`, and `UtRstReadyA` at the post-state. + The extra caps of the new `DecPre` / `SysPlace` are all null. + +Named premises: +* [J] `symL` (as `UtRstReadyA`). +* [S] `othT`, `othC` (B), at the post-restart state. +* [J] `hvs`, `hvc`, `hrun`, `hcur`: `of_restartCurA`'s restart-site facts. +-/ +import Fl4ProofsA64.P3.UtRtResetA +import Fl4ProofsA64.P3.Stage3RestartA +import Fl4ProofsA64.P1.FastRREnt + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4SpecA64 Fl4Spec Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {ar : machine.Arch A} + +theorem utNextWord_eqA (s : S) (x : U64) : obj.Slot.next_word st x s = ok (L.mem s (wa x 24#u64)) := by + unfold obj.Slot.next_word + simp only [lift, bind_ok, cte_next, L.read_eq] + +/-- **`ensure_no_children` reads the slot's words and its successor's.** -/ +theorem utNoChild_congrA {s s' : S} {x : U64} + (e : ∀ y, y = x ∨ y = NxA st s x → y ≠ 0#u64 → + L.mem s' y = L.mem s y ∧ L.mem s' (wa y 8#u64) = L.mem s (wa y 8#u64) ∧ + L.mem s' (wa y 24#u64) = L.mem s (wa y 24#u64)) + (hx : x ≠ 0#u64) : + cdt.ensure_no_children st s' x = cdt.ensure_no_children st s x := by + obtain ⟨-, -, x24⟩ := e x (Or.inl rfl) hx + have en : NxA st s' x = NxA st s x := by unfold NxA; rw [x24] + unfold cdt.ensure_no_children + rw [next_eq, next_eq, en, bind_ok, bind_ok] + unfold obj.Slot.is_null + simp only [bind_ok] + by_cases h0 : NxA st s x = 0#u64 + · simp only [h0, decide_true, ↓reduceIte] + simp only [h0, decide_false, Bool.false_eq_true, ↓reduceIte] + obtain ⟨n0, n8, n24⟩ := e (NxA st s x) (Or.inr rfl) h0 + obtain ⟨x0, x8, -⟩ := e x (Or.inl rfl) hx + have cx : obj.Slot.cap st x s' = obj.Slot.cap st x s := by + rw [capA_eq, capA_eq]; unfold capA; rw [x0, x8] + have cn : obj.Slot.cap st (NxA st s x) s' = obj.Slot.cap st (NxA st s x) s := by + rw [capA_eq, capA_eq]; unfold capA; rw [n0, n8] + have rx : obj.Slot.revocable st x s' = obj.Slot.revocable st x s := by + unfold obj.Slot.revocable; rw [utNextWord_eqA, utNextWord_eqA, x24] + have fn : obj.Slot.first_badged st (NxA st s x) s' = obj.Slot.first_badged st (NxA st s x) s := by + unfold obj.Slot.first_badged; rw [utNextWord_eqA, utNextWord_eqA, n24] + unfold cdt.is_mdb_parent_of + rw [rx, cx, cn, fn] + +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s : S} {a : A} {σ : State Unit} {m : McState} {Ud Acd Rq Nt : Set U64} + {ex : Aeneas.Std.Array obj.Slot 3#usize} {x p sb fi : U64} + +set_option maxHeartbeats 1000000 in +/-- **`UtRstReadyA` after the caller's restart** (module docstring). -/ +theorem utRstReady_restartA + (sact : SActA σ) + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (Dp : DecPre st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ud Acd s (.Untyped p sb fi false) ex) + (P : SysPlace st H Z.Cs H.frame Rq Nt OS s ex x) + (hcap : obj.Slot.cap st x s = ok (.Untyped p sb fi false)) + (he : cdt.ensure_no_children st s x = ok (.Ok ())) + (symL : SymL st H Z.Cs s) + (hrun : Runnable (L.mem s (stA (L.curW s)))) + (hcur : (L.mem s (inqA (L.curW s)) &&& sched.Q_RELEASE) = 0#u64 ∧ L.mem s (scA (L.curW s)) ≠ 0#u64 ∧ + L.mem s (scA (L.curW s)) ∈ Ac) + (hvs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → Aligned b ∧ b ≠ stA (L.curW s)) + (hvc : ∀ v ∈ Z.V, stA (L.curW s) ≠ v ∧ stA (L.curW s) ≠ wa v 24#u64) + (othT : ∀ c ∈ Z.Cs, c ≠ L.cpu s → specPtr (L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART) + (L.nodeAt (L.wr s (stA (L.curW s)) sched.TS_RESTART) c layout.node.CUR_THREAD)) ∉ utDA p sb.val) + (othC : ∀ c ∈ Z.Cs, c ≠ L.cpu s → specPtr (L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART) + (L.nodeAt (L.wr s (stA (L.curW s)) sched.TS_RESTART) c layout.node.CUR_SC)) ∉ utDA p sb.val) : + ∃ y, σ.kheap σ.curThread = some (.tcb y) ∧ + sched.set_thread_state st s (L.curW s) sched.TS_RESTART = ok (L.wr s (stA (L.curW s)) sched.TS_RESTART) ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), stUpdA σ σ.curThread y .restart)}, False) ∧ + UtRstReadyA (ar := ar) st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n nA m U Ac Rq Nt (Array.repeat 3#usize 0#u64) + x p sb fi (L.wr s (stA (L.curW s)) sched.TS_RESTART) a (stUpdA σ σ.curThread y .restart) := by + obtain ⟨y, hk, hst, hsp, J2, Y2, hOS, -⟩ := JStage3A.of_restartCurA J P symL hrun Dp.nvm hcur hvs hvc + refine ⟨y, hk, hst, hsp, ?_⟩ + set t := L.curW s with ht + set s2 := L.wr s (stA t) sched.TS_RESTART with hs2 + have hct : t ∈ H.tcbs := J.sq.curT + have hta : TcbAddr t := J.sq.tcbs t hct + have sA : Aligned (stA t) := by rw [stA_eq]; exact field_aligned hta (by simp) (by simp) + have O := J.toJEntA.stOffA hvc + have htZ : ∀ u ∈ H.tcbs, u ∈ Z.T := fun u hu => by rw [← J.tcbs]; exact hu + -- a TCB field other than the current thread's `STATE` is kept + have fld : ∀ u ∈ H.tcbs, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → (u ≠ t ∨ K.val ≠ 3328) → + L.mem s2 (wa u K) = L.mem s (wa u K) := by + intro u hu K h1 h2 h3 + exact mem_wr_ne' sA (field_aligned (J.sq.tcbs u hu) h1 h2) + (O.fld u (htZ u hu) K h1 (fun e => h3.resolve_left (fun g => g e))).symm + have kksb : L.ksb s2 = L.ksb s := by rw [hs2, L.ksb_wr] + have ecpu : L.cpu s2 = L.cpu s := by rw [hs2, L.cpu_wr] + have nodeK : ∀ c ∈ Z.Cs, ∀ o : U64, o.val < 4288 → o.val % 8 = 0 → + L.mem s2 (L.nodeAt s2 c o) = L.mem s (L.nodeAt s c o) := by + intro c hc o h1 h2 + have e : L.nodeAt s2 c o = L.nodeAt s c o := by + unfold LawfulStore.nodeAt + rw [kksb] + rw [e] + exact mem_wr_ne' sA (nodeAt_aligned (J.sq.nodes c hc) h1 h2) (O.node c hc o h1).symm + have hcpu : L.cpu s ∈ Z.Cs := J.j.1.sch.cpuCs + have kc : L.curW s2 = t := by + rw [ht] + unfold LawfulStore.curW + rw [ecpu] + exact nodeK _ hcpu _ (by simp [layout.node.CUR_THREAD]) (by simp [layout.node.CUR_THREAD]) + have kidle : L.idleW s2 = L.idleW s := by + unfold LawfulStore.idleW + rw [ecpu] + exact nodeK _ hcpu _ (by simp [layout.node.IDLE_THREAD]) (by simp [layout.node.IDLE_THREAD]) + have kact : L.actionW s2 = L.actionW s := by + unfold LawfulStore.actionW + rw [ecpu] + exact nodeK _ hcpu _ (by simp [layout.node.SCHED_ACTION]) (by simp [layout.node.SCHED_ACTION]) + have kcsc : L.cscW s2 = L.cscW s := by + unfold LawfulStore.cscW LawfulStore.nodeA + rw [ecpu] + exact nodeK _ hcpu _ (by simp [layout.node.CUR_SC]) (by simp [layout.node.CUR_SC]) + have ksc : L.mem s2 (scA t) = L.mem s (scA t) := + fld t hct 3464#u64 (by simp) (by simp) (Or.inr (by simp)) + have kinq : L.mem s2 (inqA t) = L.mem s (inqA t) := + fld t hct 3592#u64 (by simp) (by simp) (Or.inr (by simp)) + have kst : L.mem s2 (stA t) = sched.TS_RESTART := L.mem_wr_same sA + have hcscA : L.cscW s ∈ Ac := by + rw [Dp.hcsc hcur.2.1] + exact hcur.2.2 + have rvz : L.mem s (runVcpuA t) = 0#u64 := Dp.rv.zero hct Dp.nvm + have rv2 : RunVcpuInv st H.tcbs s2 := by + intro u hu h0 + have e : L.mem s2 (runVcpuA u) = L.mem s (runVcpuA u) := + fld u hu 3672#u64 (by simp) (by simp) (Or.inr (by simp)) + rw [e] at h0 + by_cases eu : u = t + · rw [eu] at h0 + exact absurd rvz h0 + · have es : L.mem s2 (stA u) = L.mem s (stA u) := by + rw [stA_eq] + exact fld u hu 3328#u64 (by simp) (by simp) (Or.inl eu) + rw [es] + exact Dp.rv u hu h0 + have run2 : Runnable (L.mem s2 (stA (L.curW s2))) := by + rw [kc, kst] + exact restart_runnable + have Dp2 : DecPre st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits U Ac s2 + (.Untyped p sb fi false) (Array.repeat 3#usize 0#u64) := + { bxi := J2.sys.ent.bxi + csc := by rw [kcsc]; exact hcscA + cap := ⟨⟨(fun _ _ _ _ _ _ h => by cases h), (fun _ _ _ _ h => by cases h), (fun _ _ h => by cases h)⟩, + (fun _ h => by cases h), (fun _ _ h => by cases h)⟩ + extra := fun _ _ sl hsl h0 => by + simp [Array.repeat] at hsl + exact absurd hsl h0 + run := run2 + act := by rw [kact]; exact Dp.act + hcsc := by + rw [kc, ksc, kcsc] + exact Dp.hcsc + cb := + { mem := by rw [kc]; exact hct + nb := by + rw [kc, kst] + exact fun h => not_runnable_of_blocked h restart_runnable + nrp := by + rw [kc, kst] + intro h + have := congrArg UScalar.val h + simp [sched.TS_RESTART, sched.TS_BLOCKED_ON_REPLY] at this + nq := by rw [kc, kinq]; exact Dp.cb.nq + nr := by rw [kc, kinq]; exact Dp.cb.nr + na := by rw [kc, kact]; exact Dp.cb.na + ni := by rw [kc, kidle]; exact Dp.cb.ni + ac := by + rw [kc, ksc] + exact fun _ => hcur.2.2 } + user := J2.sys.ent.user + rv := rv2 + nvm := by + rw [kc, kst] + intro h + have := congrArg UScalar.val h + simp [sched.TS_RESTART, sched.TS_RUNNING_VM] at this } + have hx : x ∈ OS := P.src + have P2 : SysPlace st H Z.Cs H.frame Rq Nt OS s2 (Array.repeat 3#usize 0#u64) x := + { heap := P.heap + os := P.os + win := P.win + ty := SlotTy.keep P.ty hOS + core0 := P.core0 + ext := fun z hz h0 => by + simp [Array.repeat] at hz + exact absurd hz h0 + src := hx + slotw := P.slotw + wu := by rw [kksb]; exact P.wu } + have cap2 : obj.Slot.cap st x s2 = ok (.Untyped p sb fi false) := by + rw [capA_eq, OSKeep.capA hOS hx, ← capA_eq (st := st), hcap] + have noch2 : cdt.ensure_no_children st s2 x = ok (.Ok ()) := by + rw [utNoChild_congrA (s := s) ?_ (P.os.nz x hx)] + · exact he + intro z hz h0 + have hz' : z ∈ OS := by + rcases hz with rfl | rfl + · exact hx + · have h := (P.ty x hx).2.2 _ (sext48_eq _) + exact h.resolve_left h0 + obtain ⟨k0, k8, -, k24⟩ := hOS z hz' + exact ⟨k0, k8, k24⟩ + exact + { rec_ := J2 + sact := by + refine ⟨fun u hu => sact.f1 u hu, ⟨{ y with tcbState := .restart }, ?_, Or.inr (Or.inl rfl)⟩⟩ + exact Function.update_self .. + dec := Dp2 + place := P2 + cap := cap2 + noch := noch2 + symL := Y2 + othT := fun c hc ne => othT c hc (by rwa [ecpu] at ne) + othC := fun c hc ne => othC c hc (by rwa [ecpu] at ne) } + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtRtArchA.lean b/proofs/Fl4ProofsA64/P3/UtRtArchA.lean new file mode 100644 index 000000000..8a8a66d68 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtRtArchA.lean @@ -0,0 +1,155 @@ +/- +P3 (aarch64, A64-P3 Untyped; lead 10:53:55Z item 2, the `hAR` audit): **FL4's `retype` without a reset returns the +arch state it was given.** + +On that path the only arch operations are reads (`vcpu_vgic_vtr`, `vcpu_gic_version`, `vcpu_midr`, +`kernel_ttbr1_entry`), none of which returns an arch state. Partial correctness over the extracted code: + +* `ut_initHigh_archA`, `ut_createObject_archA`, `ut_createNew_archA`; +* **`utRetype_archA`**: `retype … reset = false … = ok r → r.2.2 = a`; +* `utRetype_keepArA`: a total spec of that call may add `AR r.2.2 hw` from `AR a hw`, for any arch relation `AR`. + +With a reset, FL4's preemption point updates the arch state (`now`, `irq_pending`); that is not covered here. + +No named premise. New theorems only. +-/ +import Fl4ProofsA64.P3.UtRtCdtA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST + +section +variable {S : Type} {st : machine.Store S} {A : Type} {ar : machine.Arch A} + +theorem ut_bind_okA {α β : Type} {m : Result α} {f : α → Result β} {y : β} (h : (m >>= f) = ok y) : + ∃ x, m = ok x ∧ f x = ok y := by + obtain ⟨x, e, hx⟩ := spec_bind_inv (spec_of_eq_fd (show Std.bind m f = ok y from h)) + exact ⟨x, e, eq_of_spec_fd hx⟩ + +theorem ut_initHigh_archA {s : S} {a : A} {base : U64} {r : S × A} + (h : vspace.init_high_vspace st ar s a base = ok r) : r.2 = a := by + unfold vspace.init_high_vspace at h + repeat obtain ⟨_, _, h⟩ := ut_bind_okA h + rw [← okinj h] + +theorem ut_createArchObject_archA {s : S} {a : A} {t base : U64} {dev : Bool} {r : capability.Cap × S × A} + (h : objects.create_arch_object st ar s a t base dev = ok r) : r.2.2 = a := by + unfold objects.create_arch_object at h + split at h + · rw [← okinj h] + split at h + · rw [← okinj h] + split at h + · obtain ⟨_, _, h⟩ := ut_bind_okA h + rw [← okinj h] + split at h + · rw [← okinj h] + split at h + · obtain ⟨x, hx, h⟩ := ut_bind_okA h + obtain ⟨s1, a1⟩ := x + have e := ut_initHigh_archA hx + rw [← okinj h] + exact e + split at h + · rw [← okinj h] + split at h + · rw [← okinj h] + · rw [← okinj h] + +theorem ut_createObject_archA {s : S} {a : A} {t base us : U64} {dev : Bool} {r : capability.Cap × S × A} + (h : objects.create_object st ar s a t base us dev = ok r) : r.2.2 = a := by + unfold objects.create_object at h + split at h + · repeat obtain ⟨_, _, h⟩ := ut_bind_okA h + rw [← okinj h] + split at h + · rw [← okinj h] + split at h + · rw [← okinj h] + split at h + · rw [← okinj h] + split at h + · rw [← okinj h] + split at h + · rw [← okinj h] + split at h + · rw [← okinj h] + · exact ut_createArchObject_archA h + +theorem ut_createNewLoop_archA {a : A} {t src dcn off len base us size : U64} {dev : Bool} : + ∀ (k : Nat) (s : S) (i : U64) (r : S × A), len.val - i.val = k → + objects.create_new_objects_loop st ar s a t src dcn off len base us dev size i = ok r → r.2 = a := by + intro k + induction k using Nat.strong_induction_on with + | _ k ih => + intro s i r hk h + unfold objects.create_new_objects_loop at h + rw [loop.eq_def] at h + obtain ⟨cf, hb, hn⟩ := ut_bind_okA h + change objects.create_new_objects_loop.body st ar t src dcn off len base us dev size s a i = ok cf at hb + unfold objects.create_new_objects_loop.body at hb + split at hb + case isTrue hlt => + obtain ⟨_, -, hb⟩ := ut_bind_okA hb + obtain ⟨_, -, hb⟩ := ut_bind_okA hb + obtain ⟨_, -, hb⟩ := ut_bind_okA hb + obtain ⟨x, hx, hb⟩ := ut_bind_okA hb + obtain ⟨cap, s1, a1⟩ := x + have ea : a1 = a := ut_createObject_archA hx + obtain ⟨_, -, hb⟩ := ut_bind_okA hb + obtain ⟨_, -, hb⟩ := ut_bind_okA hb + obtain ⟨s3, -, hb⟩ := ut_bind_okA hb + obtain ⟨i5, hi5, hb⟩ := ut_bind_okA hb + obtain rfl := okinj hb + subst ea + have hv : i5.val = i.val + 1 := by + have g := UScalar.add_equiv i 1#u64 + rw [hi5] at g + exact g.2.1 + have hlt' : i.val < len.val := hlt + exact ih (len.val - i5.val) (by omega) s3 i5 r rfl (by + unfold objects.create_new_objects_loop + exact hn) + case isFalse => + obtain rfl := okinj hb + rw [← okinj (show (ok (s, a) : Result (S × A)) = ok r from hn)] + +theorem ut_createNew_archA {s : S} {a : A} {t src dcn off len base us : U64} {dev : Bool} {r : S × A} + (h : objects.create_new_objects st ar s a t src dcn off len base us dev = ok r) : r.2 = a := by + unfold objects.create_new_objects at h + obtain ⟨o, -, h⟩ := ut_bind_okA h + cases o with + | none => rw [← okinj (show (ok (s, a) : Result (S × A)) = ok r from h)] + | some z => exact ut_createNewLoop_archA _ s _ r rfl h + +/-- **`retype` without a reset returns its arch state** (module docstring). -/ +theorem utRetype_archA {s : S} {a : A} {slot base nt us dest off len : U64} {dev : Bool} + {r : core.result.Result Unit error.Exc × S × A} + (h : untyped.retype st ar s a slot false base nt us dest off len dev = ok r) : r.2.2 = a := by + unfold untyped.retype at h + simp only [Bool.false_eq_true, ↓reduceIte] at h + obtain ⟨_, -, h⟩ := ut_bind_okA h + obtain ⟨_, -, h⟩ := ut_bind_okA h + obtain ⟨_, -, h⟩ := ut_bind_okA h + obtain ⟨_, -, h⟩ := ut_bind_okA h + obtain ⟨_, -, h⟩ := ut_bind_okA h + obtain ⟨_, -, h⟩ := ut_bind_okA h + obtain ⟨x, hx, h⟩ := ut_bind_okA h + obtain ⟨s3, a2⟩ := x + have e := ut_createNew_archA hx + rw [← okinj h] + exact e + +/-- A total spec of `retype` without a reset keeps any arch relation. -/ +theorem utRetype_keepArA {s : S} {a : A} {slot base nt us dest off len : U64} {dev : Bool} + {Q : core.result.Result Unit error.Exc × S × A → Prop} {R : A → Prop} (hR : R a) + (h : WP.spec (untyped.retype st ar s a slot false base nt us dest off len dev) Q) : + WP.spec (untyped.retype st ar s a slot false base nt us dest off len dev) (fun r => R r.2.2) := by + obtain ⟨y, hy, -⟩ := spec_exists_dut h + rw [hy, WP.spec_ok, utRetype_archA hy] + exact hR + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtRtAsidA.lean b/proofs/Fl4ProofsA64/P3/UtRtAsidA.lean new file mode 100644 index 000000000..34e4f6a71 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtRtAsidA.lean @@ -0,0 +1,163 @@ +/- +P3 (aarch64, A64-P3 Untyped, full-record post, discharge): the spec body of a creation keeps the ASID table (the +heap bundle's `asid`; the same walk as `utRtBody_umA`). + +No named premise. New theorems only. +-/ +import Fl4ProofsA64.P3.UtRtUmA + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Sched Classical + +noncomputable section + +open Fl4SpecA64 Fl4Spec + +/-- Every outcome of `m` keeps the user memory. -/ +def UtAsA {α : Type} (m : NonDet (State Unit) α) : Prop := ∀ σ r, r ∈ (m σ).1 → r.2.archState.armAsidTable = σ.archState.armAsidTable + +theorem UtAsA.bind {α β : Type} {m : NonDet (State Unit) α} {f : α → NonDet (State Unit) β} (hm : UtAsA m) + (hf : ∀ a, UtAsA (f a)) : UtAsA (m >>= f) := fun σ r hr => by + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + exact (hf x.1 x.2 r hr).trans (hm σ x hx) + +theorem UtAsA.state {α : Type} {m : NonDet (State Unit) α} (h : ∀ σ r, r ∈ (m σ).1 → r.2 = σ) : UtAsA m := + fun σ r hr => by rw [h σ r hr] + +theorem UtAsA.pure {α : Type} (a : α) : UtAsA (Pure.pure a : NonDet (State Unit) α) := + .state fun σ r h => by obtain rfl : r = (a, σ) := h; rfl + +theorem UtAsA.gets {α : Type} (g : State Unit → α) : UtAsA (NonDet.gets g) := + .state fun σ r h => by obtain rfl : r = (g σ, σ) := h; rfl + +theorem UtAsA.fail {α : Type} : UtAsA (NonDet.fail : NonDet (State Unit) α) := + fun _ _ h => absurd h (Set.notMem_empty _) + +theorem UtAsA.assert (P : Prop) : UtAsA (NonDet.assert P : NonDet (State Unit) Unit) := + .state fun _ _ h => by obtain ⟨-, rfl⟩ := h; rfl + +theorem UtAsA.assertOpt {α : Type} (o : Option α) : UtAsA (NonDet.assertOpt o : NonDet (State Unit) α) := by + cases o + · exact UtAsA.fail + · exact .state fun σ r h => by obtain rfl : r = (_, σ) := h; rfl + +theorem ut_getCap_asA (sl : CslotPtr) : UtAsA (getCap (Z := Unit) sl) := by + obtain ⟨oref, cref⟩ := sl + have go : UtAsA (getObject (Z := Unit) oref) := .state fun _ _ h => (getObject_mem h).1 + refine UtAsA.bind go fun obj => ?_ + cases obj <;> dsimp only <;> first + | exact UtAsA.bind UtAsA.fail fun _ => UtAsA.assertOpt _ + | exact UtAsA.bind (UtAsA.pure _) fun _ => UtAsA.assertOpt _ + | exact UtAsA.bind (UtAsA.assert _) fun _ => UtAsA.bind (UtAsA.pure _) fun _ => UtAsA.assertOpt _ + +theorem ut_setCap_asA (cap : Cap) (sl : CslotPtr) : UtAsA (setCap (Z := Unit) cap sl) := fun σ r hr => by + obtain ⟨oref, cref⟩ := sl + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + obtain ⟨e1, -⟩ := getObject_mem hx + obtain ⟨obj, σ1⟩ := x + simp only at e1 hr + subst e1 + have key : ∀ o : KernelObject, r ∈ ((Pure.pure o >>= fun o' => setObject (Z := Unit) oref o') σ1).1 → + r.2.archState.armAsidTable = σ1.archState.armAsidTable := by + intro o h + obtain ⟨y, hy, h⟩ := mem_bindA.1 h + obtain rfl : y = (o, σ1) := hy + obtain ⟨o0, -, hr2⟩ := setObject_mem h + rw [hr2] + have kfail : ∀ f : KernelObject → NonDet (State Unit) Unit, r ∈ ((NonDet.fail >>= f) σ1).1 → False := by + intro f h + obtain ⟨y, hy, -⟩ := mem_bindA.1 h + exact absurd hy (Set.notMem_empty _) + cases obj with + | cnode sz cn => + simp only at hr + split_ifs at hr + · exact key _ hr + · exact (kfail _ hr).elim + | tcb t => + simp only at hr + split_ifs at hr <;> + first + | exact (kfail _ hr).elim + | exact key _ hr + | _ => + try simp only at hr + exact (kfail _ hr).elim + +theorem ut_setCdt_asA (t : Cdt) : UtAsA (setCdt (Z := Unit) t) := fun σ r hr => by + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + obtain rfl : x = (σ, σ) := hx + obtain rfl : r = ((), { σ with cdt := t }) := hr + exact rfl + +theorem ut_modify_asA {g : State Unit → State Unit} (h : ∀ σ, (g σ).archState.armAsidTable = σ.archState.armAsidTable) : + UtAsA (NonDet.modify g) := fun σ r hr => by + obtain rfl : r = ((), g σ) := hr + exact h σ + +theorem ut_setOriginal_asA (sl : CslotPtr) (v : Bool) : UtAsA (setOriginal (Z := Unit) sl v) := + UtAsA.bind (UtAsA.gets _) fun _ => ut_modify_asA fun _ => + rfl + +theorem ut_doExtendedOp_asA (eop : DetExtMonad Unit) : UtAsA (doExtendedOp (Z := Unit) eop) := by + unfold doExtendedOp + refine UtAsA.bind (.state fun σ r h => by obtain rfl : r = (σ, σ) := h; rfl) fun _ => ?_ + refine UtAsA.bind (.state fun σ r h => by rw [h.2]) fun _ => ut_modify_asA fun _ => + rfl + +theorem ut_createCap_asA (t : ApiObjectType) (bits : Nat) (src : CslotPtr) (dev : Bool) (x : CslotPtr × ObjRef) : + UtAsA (createCap (Z := Unit) t bits src dev x) := by + obtain ⟨dest, oref⟩ := x + unfold createCap + exact UtAsA.bind (UtAsA.gets _) fun _ => UtAsA.bind (UtAsA.gets _) fun _ => UtAsA.bind (ut_setCdt_asA _) fun _ => + UtAsA.bind (ut_doExtendedOp_asA _) fun _ => UtAsA.bind (ut_setOriginal_asA _ _) fun _ => ut_setCap_asA _ _ + +theorem ut_mapMx_asA {α : Type} {f : α → NonDet (State Unit) Unit} (h : ∀ a, UtAsA (f a)) (l : List α) : + UtAsA (NonDet.mapMx f l) := by + induction l with + | nil => unfold NonDet.mapMx; exact UtAsA.pure _ + | cons a l ih => unfold NonDet.mapMx; exact UtAsA.bind (h a) fun _ => ih + +/-- A machine operation keeps the ASID table. -/ +theorem ut_doMachineOp_asA {α : Type} (mop : NonDet MachineState α) : UtAsA (doMachineOp (Z := Unit) mop) := fun σ r hr => by + unfold doMachineOp at hr + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + obtain rfl : x = (σ.machineState, σ) := hx + obtain ⟨y, hy, hr⟩ := mem_bindA.1 hr + have ey : y.2 = σ := hy.2 + obtain ⟨z, hz, hr⟩ := mem_bindA.1 hr + obtain rfl : z = ((), { y.2 with machineState := y.1.2 }) := hz + obtain rfl : r = (y.1.1, { y.2 with machineState := y.1.2 }) := hr + show y.2.archState.armAsidTable = _ + rw [ey] + +theorem ut_initArch_asA (t : ApiObjectType) (dev : Bool) (ptr : ObjRef) (n sz : Nat) (refs : List ObjRef) : + UtAsA (initArchObjects (Z := Unit) t dev ptr n sz refs) := by + unfold initArchObjects + split_ifs + · exact ut_mapMx_asA (fun _ => ut_doMachineOp_asA _) _ + · exact ut_mapMx_asA (fun _ => ut_doMachineOp_asA _) _ + · exact UtAsA.pure _ + +theorem ut_retRegion_asA (t : ApiObjectType) (ptr : ObjRef) (n bits : Nat) (dev : Bool) : + UtAsA (retypeRegionWith (Z := Unit) fl4ObjBitsApi ptr n bits t dev) := fun σ r hr => by + rw [(utRetRegion_invA _ _ _ _ _ _ hr).2] + split_ifs + · rfl + · exact rfl + +/-- **The body keeps the ASID table.** -/ +theorem utRtBody_asidA {srcSlot : CslotPtr} {base retypeBase : Word} {t : ApiObjectType} {objSz : Nat} + {slots : List CslotPtr} {dev : Bool} {σ : State Unit} {r : Unit × State Unit} + (hr : r ∈ (utRtBodyA srcSlot base retypeBase t objSz slots dev σ).1) : + r.2.archState.armAsidTable = σ.archState.armAsidTable := by + have h : UtAsA (utRtBodyA srcSlot base retypeBase t objSz slots dev) := by + unfold utRtBodyA + exact UtAsA.bind (ut_getCap_asA _) fun _ => UtAsA.bind (UtAsA.pure _) fun _ => UtAsA.bind (UtAsA.pure _) fun _ => + UtAsA.bind (ut_setCap_asA _ _) fun _ => UtAsA.bind (ut_retRegion_asA _ _ _ _ _) fun _ => + UtAsA.bind (ut_initArch_asA _ _ _ _ _ _) fun _ => ut_mapMx_asA (fun x => ut_createCap_asA _ _ _ _ x) _ + exact h σ r hr + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtRtCapsA.lean b/proofs/Fl4ProofsA64/P3/UtRtCapsA.lean new file mode 100644 index 000000000..a45bc375a --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtRtCapsA.lean @@ -0,0 +1,632 @@ +/- +P3 (aarch64, A64-P3 Untyped W5): **the post-creation cap writes in `StateRelA`**, wired to A64 P1's `UtEnd` +(lead 01:47Z, items 1–2). + +* `utArgs_dstOffA`: under a reset (a childless untyped) the destination slots lie off its region: each is covered + by a CNode cap (`retype_dest.covC`), which is not a descendant (`reset_offCov`). +* `utCreateCap_invA`, `utMapCreate_invA`, `utRtBody_invA`: every outcome of the spec body after the reset — which + CNode slots hold what, the `cdt` and original-cap updates. +* **`utEnd_stateRelA`**: `StateRelA` at the typed heap after the Retype, at FL4's final state and the spec's + outcome. Old slots keep their cap (`CrtE.keep`), the untyped slot holds the lowered untyped, each destination the + new object's cap (`CrtE.new`); new CNode words are zero (`CrtE.zero`), new TCB slot words (`DbgX`). + +Hypotheses: `hsub` (the record's slots are in `OS1`, closure item A), the spec / FL4 slot correspondence (`hsrcS`, +`hdst`), `hsrcRel` (the lowered untyped's cap relation: FL4's new free index against the spec's), `hnewRel` +(`utNewCap_relA`). No named premise. +-/ +import Fl4ProofsA64.P3.UtRtWitA +import Fl4ProofsA64.P3.UtRtInvA +import Fl4ProofsA64.P1.MdbLiveU +import Fl4ProofsA64.P1.MdbUtR +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Sched Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## Item 1: the destinations under a reset -/ + +theorem utArgs_dstOffA {S : Type} {st : machine.Store S} [L : LawfulStore st] {H : Heap} + {Cs Sg Rp Nt OS : Set U64} {s0 : S} {extra : Aeneas.Std.Array obj.Slot 3#usize} {slot p sb fi : U64} {dev : Bool} + (R : UtDecP st H Cs Sg Rp Nt OS s0 extra slot p sb fi dev) {reset : Bool} {aligned nt us dest off win : U64} + (hU : UtArgs st s0 extra slot p sb fi dev reset aligned nt us dest off win) + (he : ∃ v, cdt.ensure_no_children st s0 slot = ok (.Ok v)) : + ∀ j, j < win.val → djA dest off j ∈ OffR p.val (p.val + 2 ^ sb.val) OS := by + obtain ⟨x, hxm, hx0, idx, dep, val, ers, hv64, erd, -⟩ := id hU + have hm := R.stz.1.1 + have hdc := untyped.retype_dest.covC R.os R.ty hm (R.ext x hxm hx0) erd + have hR := R.stz.1.2.1 + obtain ⟨G, par, hM, -, -, -, -, hT, hU'⟩ := id hm + have hTA : TreeA st OS G par s0 := ⟨hM, hT, hU'⟩ + obtain ⟨v, hv⟩ := he + have hnd := cdt.ensure_no_children.noDesc R.os hTA R.src R.cap (hR slot R.src (by rw [R.cap]; rfl)) hv + intro j hj + obtain ⟨hdO, z, hz, p', r', g', b', ez, hcv⟩ := hdc j hj + refine ⟨hdO, reset_offCov hU' R.src R.cap hnd hz ?_⟩ + rw [ez]; exact hcv + +/-- The destinations are distinct (their new caps have distinct bases). -/ +theorem utEnd_dstInjA {S : Type} {st : machine.Store S} [L : LawfulStore st] {H H1 H' : Heap} {Cs OS OS1 : Set U64} + {s0 s1 s' : S} {slot p sb : U64} {dev : Bool} {lo hi : Nat} {aligned nt us dest off win : U64} + (U : UtEnd st H Cs OS s0 slot p sb dev H1 H' OS1 s1 s' lo hi aligned nt us dest off win) {j l : Nat} + (hj : j < win.val) (hl : l < win.val) (e : djA dest off j = djA dest off l) : j = l := by + have h := U.E.new j hj + rw [e, U.E.new l hl] at h + have := congrArg ptrA h + rw [newCapA_ptr, newCapA_ptr] at this + have v := congrArg UScalar.val this + rw [(utEnd_bjA U hl).1, (utEnd_bjA U hj).1] at v + have hX : 0 < 2 ^ (zA nt.val us).val := Nat.two_pow_pos _ + exact (Nat.eq_of_mul_eq_mul_right hX (by omega : l * 2 ^ (zA nt.val us).val = j * 2 ^ (zA nt.val us).val)).symm + +/-! ## The spec's cap writes -/ + +open Fl4SpecA64 Fl4Spec + +/-- `create_cap` into a CNode slot. -/ +theorem utCreateCap_invA {t : ApiObjectType} {bits : Nat} {src dest : CslotPtr} {dev : Bool} {oref : ObjRef} + {σ : State Unit} {r : Unit × State Unit} (hr : r ∈ (createCap (Z := Unit) t bits src dev (dest, oref) σ).1) + {sz : Nat} {cn : CnodeContents} (hk : σ.kheap dest.1 = some (.cnode sz cn)) : + dest.2 ∈ dom cn ∧ r.2.kheap = Function.update σ.kheap dest.1 + (some (.cnode sz (Function.update cn dest.2 (some (defaultCap t oref bits dev))))) ∧ + r.2.cdt = Function.update σ.cdt dest (some src) ∧ + r.2.isOriginalCap = Function.update σ.isOriginalCap dest true ∧ r.2.curDomain = σ.curDomain := by + unfold createCap at hr + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + obtain rfl : x = (σ.cdt dest, σ) := hx + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + obtain rfl : x = (σ.cdt, σ) := hx + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + obtain ⟨y, hy, hx⟩ := mem_bindA.1 hx + obtain rfl : y = (σ, σ) := hy + obtain rfl : x = ((), { σ with cdt := Function.update σ.cdt dest (some src) }) := hx + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + have hx' : x.2.kheap = σ.kheap ∧ x.2.cdt = Function.update σ.cdt dest (some src) ∧ + x.2.isOriginalCap = σ.isOriginalCap ∧ x.2.curDomain = σ.curDomain := by + unfold doExtendedOp at hx + obtain ⟨y, hy, hx⟩ := mem_bindA.1 hx + obtain rfl : y = (_, _) := hy + obtain ⟨y, hy, hx⟩ := mem_bindA.1 hx + have ey := hy.2 + obtain ⟨⟨u, es⟩, σy⟩ := y + simp only at ey hx + subst ey + obtain rfl : x = ((), _) := hx + exact ⟨rfl, rfl, rfl, rfl⟩ + obtain ⟨u0, σ1⟩ := x + simp only at hx' hr + obtain ⟨k1, c1, o1, d1⟩ := hx' + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + obtain ⟨y, hy, hx⟩ := mem_bindA.1 hx + obtain rfl : y = (σ1.isOriginalCap, σ1) := hy + obtain rfl : x = ((), { σ1 with isOriginalCap := Function.update σ1.isOriginalCap dest true }) := hx + -- `set_cap` on the CNode + obtain ⟨oref', cref⟩ := dest + obtain ⟨z, hz, hr⟩ := mem_bindA.1 hr + obtain ⟨e1, e2⟩ := getObject_mem hz + obtain ⟨obj, σ2⟩ := z + simp only at e1 e2 hr hk + subst e1 + have ho : obj = .cnode sz cn := by + have : σ1.kheap oref' = some (.cnode sz cn) := by rw [k1]; exact hk + exact Option.some.inj (e2.symm.trans this) + subst ho + simp only at hr + split_ifs at hr with hc + · obtain ⟨w, hw, hr⟩ := mem_bindA.1 hr + obtain rfl : w = (KernelObject.cnode sz (Function.update cn cref (some (defaultCap t oref bits dev))), _) := hw + obtain ⟨o0, -, h2⟩ := setObject_mem hr + refine ⟨hc.1, ?_, ?_, ?_, ?_⟩ <;> rw [h2] + · simp only [k1] + · exact c1 + · simp only [o1] + · exact d1 + · obtain ⟨w, hw, -⟩ := mem_bindA.1 hr + exact absurd hw (Set.notMem_empty _) + +/-- The contents of CNode `q` after the writes `l`. -/ +def utWritesA (t : ApiObjectType) (bits : Nat) (dev : Bool) (l : List (CslotPtr × ObjRef)) (q : ObjRef) + (cn : CnodeContents) : CnodeContents := + l.foldl (fun cn x => if x.1.1 = q then Function.update cn x.1.2 (some (defaultCap t x.2 bits dev)) else cn) cn + +theorem utWritesA_off {t : ApiObjectType} {bits : Nat} {dev : Bool} {q : ObjRef} {idx : CnodeIndex} + (l : List (CslotPtr × ObjRef)) (cn : CnodeContents) (h : ∀ x ∈ l, x.1 ≠ (q, idx)) : + utWritesA t bits dev l q cn idx = cn idx := by + induction l generalizing cn with + | nil => rfl + | cons a l ih => + unfold utWritesA at ih ⊢ + rw [List.foldl_cons, ih _ (fun x hx => h x (List.mem_cons_of_mem _ hx))] + split_ifs with ha + · exact Function.update_of_ne (fun e => h a (List.mem_cons_self ..) (Prod.ext ha e.symm)) .. + · rfl + +theorem utWritesA_on {t : ApiObjectType} {bits : Nat} {dev : Bool} {q : ObjRef} {idx : CnodeIndex} + (l : List (CslotPtr × ObjRef)) (cn : CnodeContents) (hnd : (l.map Prod.fst).Nodup) {o : ObjRef} + (h : ((q, idx), o) ∈ l) : utWritesA t bits dev l q cn idx = some (defaultCap t o bits dev) := by + induction l generalizing cn with + | nil => cases h + | cons a l ih => + rw [List.map_cons, List.nodup_cons] at hnd + rcases List.mem_cons.1 h with e | h' + · subst e + have off : ∀ x ∈ l, x.1 ≠ (q, idx) := fun x hx e => hnd.1 (by + have := List.mem_map_of_mem (f := Prod.fst) hx + rw [e] at this; exact this) + have e1 : utWritesA t bits dev (((q, idx), o) :: l) q cn = + utWritesA t bits dev l q (Function.update cn idx (some (defaultCap t o bits dev))) := by + unfold utWritesA + rw [List.foldl_cons, ite_eq_left rfl] + rw [e1, utWritesA_off l _ off] + exact Function.update_self .. + · exact ih _ hnd.2 h' + +/-- Every outcome of the `create_cap` loop over CNode slots. -/ +theorem utMapCreate_invA {t : ApiObjectType} {bits : Nat} {src : CslotPtr} {dev : Bool} + (l : List (CslotPtr × ObjRef)) (σ : State Unit) + (hc : ∀ x ∈ l, ∃ sz cn, σ.kheap x.1.1 = some (.cnode sz cn)) {r : Unit × State Unit} + (hr : r ∈ (NonDet.mapMx (createCap (Z := Unit) t bits src dev) l σ).1) : + (∀ q sz cn, σ.kheap q = some (.cnode sz cn) → + r.2.kheap q = some (.cnode sz (utWritesA t bits dev l q cn)) ∧ + ∀ idx, (utWritesA t bits dev l q cn idx = none ↔ cn idx = none)) ∧ + (∀ q, (∀ sz cn, σ.kheap q ≠ some (.cnode sz cn)) → r.2.kheap q = σ.kheap q) ∧ + (∀ x, r.2.cdt x = if x ∈ l.map Prod.fst then some src else σ.cdt x) ∧ + (∀ x, r.2.isOriginalCap x = if x ∈ l.map Prod.fst then true else σ.isOriginalCap x) ∧ + r.2.curDomain = σ.curDomain := by + induction l generalizing σ with + | nil => + unfold NonDet.mapMx at hr + obtain rfl : r = ((), σ) := hr + exact ⟨fun q sz cn h => ⟨h, fun _ => Iff.rfl⟩, fun _ _ => rfl, fun x => by simp, fun x => by simp, rfl⟩ + | cons a l ih => + obtain ⟨dest, oref⟩ := a + unfold NonDet.mapMx at hr + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + obtain ⟨sz0, cn0, h0⟩ := hc _ (List.mem_cons_self ..) + obtain ⟨hdom, k1, c1, o1, d1⟩ := utCreateCap_invA hx h0 + -- the CNodes stay CNodes + have kh : ∀ q sz cn, σ.kheap q = some (.cnode sz cn) → x.2.kheap q = some (.cnode sz + (if dest.1 = q then Function.update cn dest.2 (some (defaultCap t oref bits dev)) else cn)) := by + intro q sz cn h + rw [k1] + by_cases e : dest.1 = q + · subst e + rw [h0] at h + cases h + rw [ite_eq_left rfl, Function.update_self] + · rw [ite_eq_right e, Function.update_of_ne (Ne.symm e)]; exact h + have knon : ∀ q, (∀ sz cn, σ.kheap q ≠ some (.cnode sz cn)) → x.2.kheap q = σ.kheap q := by + intro q h + rw [k1] + exact Function.update_of_ne (fun e => h sz0 cn0 (by rw [e]; exact h0)) .. + obtain ⟨A, B, C, D, E⟩ := ih x.2 (fun y hy => by + obtain ⟨sz, cn, h⟩ := hc y (List.mem_cons_of_mem _ hy) + exact ⟨sz, _, kh _ _ _ h⟩) hr + refine ⟨fun q sz cn h => ?_, fun q h => ?_, fun y => ?_, fun y => ?_, E.trans d1⟩ + · obtain ⟨a1, a2⟩ := A q sz _ (kh q sz cn h) + refine ⟨a1, fun idx => (a2 idx).trans ?_⟩ + split_ifs with e + · subst e + rw [h0] at h + cases h + by_cases ei : idx = dest.2 + · subst ei + rw [Function.update_self] + have : cn0 dest.2 ≠ none := hdom + exact ⟨fun h => by simp at h, fun h => absurd h this⟩ + · rw [Function.update_of_ne ei] + · exact Iff.rfl + · have h' : ∀ sz cn, x.2.kheap q ≠ some (.cnode sz cn) := fun sz cn => by rw [knon q h]; exact h sz cn + exact (B q h').trans (knon q h) + · rw [C y, c1] + by_cases e : y ∈ l.map Prod.fst + · rw [ite_eq_left e, ite_eq_left (show y ∈ List.map Prod.fst ((dest, oref) :: l) from List.mem_cons_of_mem _ e)] + · rw [ite_eq_right e] + by_cases e' : y = dest + · subst e'; rw [Function.update_self, ite_eq_left (by simp)] + · rw [Function.update_of_ne e', ite_eq_right (by simp only [List.map_cons, List.mem_cons, not_or]; exact ⟨e', e⟩)] + · rw [D y, o1] + by_cases e : y ∈ l.map Prod.fst + · rw [ite_eq_left e, ite_eq_left (show y ∈ List.map Prod.fst ((dest, oref) :: l) from List.mem_cons_of_mem _ e)] + · rw [ite_eq_right e] + by_cases e' : y = dest + · subst e'; rw [Function.update_self, ite_eq_left (by simp)] + · rw [Function.update_of_ne e', ite_eq_right (by simp only [List.map_cons, List.mem_cons, not_or]; exact ⟨e', e⟩)] + +/-- A machine operation keeps the heap, the CDT, the original-cap map and the current domain. -/ +theorem ut_mop_frameA {α : Type} (mop : NonDet MachineState α) (s : State Unit) (q : α × State Unit) + (hq : q ∈ (doMachineOp (Z := Unit) mop s).1) : q.2.kheap = s.kheap ∧ q.2.cdt = s.cdt ∧ + q.2.isOriginalCap = s.isOriginalCap ∧ q.2.curDomain = s.curDomain := by + let F : State Unit → State Unit → Prop := fun a b => b.kheap = a.kheap ∧ b.cdt = a.cdt ∧ + b.isOriginalCap = a.isOriginalCap ∧ b.curDomain = a.curDomain + have tr : ∀ a b c, F a b → F b c → F a c := fun a b c h1 h2 => + ⟨h2.1.trans h1.1, h2.2.1.trans h1.2.1, h2.2.2.1.trans h1.2.2.1, h2.2.2.2.trans h1.2.2.2⟩ + have refl : ∀ a, F a a := fun _ => ⟨rfl, rfl, rfl, rfl⟩ + have bind : ∀ {β γ : Type} (m : NonDet (State Unit) β) (f : β → NonDet (State Unit) γ), + (∀ a r, r ∈ (m a).1 → F a r.2) → (∀ x a r, r ∈ (f x a).1 → F a r.2) → + ∀ a r, r ∈ ((m >>= f) a).1 → F a r.2 := by + intro β γ m f hm hf a r hr + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + exact tr _ _ _ (hm a x hx) (hf x.1 x.2 r hr) + have : ∀ a r, r ∈ (doMachineOp (Z := Unit) mop a).1 → F a r.2 := by + unfold doMachineOp + refine bind _ _ (fun a r h => by obtain rfl : r = (a.machineState, a) := h; exact refl _) fun _ => ?_ + refine bind _ _ (fun a r h => by rw [h.2]; exact refl _) fun _ => ?_ + refine bind _ _ (fun a r h => by obtain rfl : r = ((), _) := h; exact ⟨rfl, rfl, rfl, rfl⟩) fun _ => ?_ + exact fun a r h => by obtain rfl : r = (_, a) := h; exact refl _ + exact this s q hq + +/-- **Every outcome of the body** over CNode source and destination slots off the new addresses. -/ +theorem utRtBody_invA {srcSlot : CslotPtr} {base retypeBase : Word} {t : ApiObjectType} + {objSz : Nat} {slots : List CslotPtr} {dev : Bool} {σ : State Unit} {r : Unit × State Unit} + (hr : r ∈ (utRtBodyA srcSlot base retypeBase t objSz slots dev σ).1) + {rS : Nat} {csS : CnodeContents} (hS : σ.kheap srcSlot.1 = some (.cnode rS csS)) {capS : Cap} + (hcapS : csS srcSlot.2 = some capS) + (hSa : srcSlot.1 ∉ fl4RetypeAddrs fl4ObjBitsApi retypeBase slots.length objSz t) + (hd : ∀ d ∈ slots, (∃ sz cn, σ.kheap d.1 = some (.cnode sz cn)) ∧ + d.1 ∉ fl4RetypeAddrs fl4ObjBitsApi retypeBase slots.length objSz t) : + let addrs := fl4RetypeAddrs fl4ObjBitsApi retypeBase slots.length objSz t + let l := slots.zip addrs + let u : Cap := .untypedCap dev base (bitsOf capS) + (unat (retypeBase + BitVec.ofNat 64 slots.length <<< fl4ObjBitsApi t objSz - base)) + let σa := setObjA σ srcSlot.1 (.cnode rS (Function.update csS srcSlot.2 (some u))) + srcSlot.2 ∈ dom csS ∧ + (∀ q sz cn, t = .untyped ∨ q ∉ addrs → σa.kheap q = some (.cnode sz cn) → + r.2.kheap q = some (.cnode sz (utWritesA t objSz dev l q cn)) ∧ + ∀ idx, (utWritesA t objSz dev l q cn idx = none ↔ cn idx = none)) ∧ + (t ≠ .untyped → ∀ q ∈ addrs, r.2.kheap q = some (defaultObject t dev objSz σ.curDomain)) ∧ + (∀ q, t = .untyped ∨ q ∉ addrs → (∀ sz cn, σa.kheap q ≠ some (.cnode sz cn)) → r.2.kheap q = σa.kheap q) ∧ + (∀ x, r.2.cdt x = if x ∈ slots then some srcSlot else σ.cdt x) ∧ + (∀ x, r.2.isOriginalCap x = if x ∈ slots then true else σ.isOriginalCap x) ∧ + r.2.curDomain = σ.curDomain := by + intro addrs l u σa + unfold utRtBodyA at hr + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + have hxv : x = (capS, σ) := by + obtain ⟨e2, o, ho, hc⟩ := getCap_memA hx + rw [hS] at ho + cases ho + exact Prod.ext (Option.some.inj (hc.symm.trans (show capOf (.cnode rS csS) srcSlot.2 = some capS from hcapS))) e2 + subst hxv + simp only at hr + obtain ⟨y, hy, hr⟩ := mem_bindA.1 hr + obtain rfl : y = (BitVec.ofNat 64 slots.length <<< fl4ObjBitsApi t objSz, σ) := hy + obtain ⟨y, hy, hr⟩ := mem_bindA.1 hr + obtain rfl : y = (retypeBase + BitVec.ofNat 64 slots.length <<< fl4ObjBitsApi t objSz, σ) := hy + obtain ⟨y, hy, hr⟩ := mem_bindA.1 hr + have hsc : srcSlot.2 ∈ dom csS ∧ y.2 = σa := by + obtain ⟨o, c⟩ := srcSlot + obtain ⟨z, hz, hy⟩ := mem_bindA.1 hy + obtain ⟨e1, e2⟩ := getObject_mem hz + obtain ⟨obj, σ1⟩ := z + simp only at e1 e2 hy hS + subst e1 + obtain rfl : obj = .cnode rS csS := Option.some.inj (e2.symm.trans hS) + simp only at hy + split_ifs at hy with hc + · obtain ⟨w, hw, hy⟩ := mem_bindA.1 hy + obtain rfl : w = (KernelObject.cnode rS (Function.update csS c (some u)), σ1) := hw + obtain ⟨o0, -, h2⟩ := setObject_mem hy + exact ⟨hc.1, h2⟩ + · obtain ⟨w, hw, -⟩ := mem_bindA.1 hy + exact absurd hw (Set.notMem_empty _) + obtain ⟨hdomS, ey⟩ := hsc + obtain ⟨u0, σy⟩ := y + simp only at ey hr + subst ey + obtain ⟨z, hz, hr⟩ := mem_bindA.1 hr + obtain ⟨e1, e2⟩ := utRetRegion_invA _ _ _ _ _ _ hz + obtain ⟨orefs, σb⟩ := z + simp only at e1 e2 hr + subst e1 + obtain ⟨w, hw, hr⟩ := mem_bindA.1 hr + have wk : w.2.kheap = σb.kheap ∧ w.2.cdt = σb.cdt ∧ w.2.isOriginalCap = σb.isOriginalCap ∧ + w.2.curDomain = σb.curDomain := by + have mm : ∀ (f : ObjRef → NonDet (State Unit) Unit), + (∀ a s q, q ∈ (f a s).1 → q.2.kheap = s.kheap ∧ q.2.cdt = s.cdt ∧ q.2.isOriginalCap = s.isOriginalCap ∧ + q.2.curDomain = s.curDomain) → ∀ (l : List ObjRef) (s : State Unit) (q : Unit × State Unit), + q ∈ (NonDet.mapMx f l s).1 → q.2.kheap = s.kheap ∧ q.2.cdt = s.cdt ∧ + q.2.isOriginalCap = s.isOriginalCap ∧ q.2.curDomain = s.curDomain := by + intro f hf l + induction l with + | nil => + intro s q hq + unfold NonDet.mapMx at hq + obtain rfl : q = ((), s) := hq + exact ⟨rfl, rfl, rfl, rfl⟩ + | cons a l ih => + intro s q hq + unfold NonDet.mapMx at hq + obtain ⟨b, hb, hq⟩ := mem_bindA.1 hq + obtain ⟨a1, a2, a3, a4⟩ := hf a s b hb + obtain ⟨b1, b2, b3, b4⟩ := ih b.2 q hq + exact ⟨b1.trans a1, b2.trans a2, b3.trans a3, b4.trans a4⟩ + unfold initArchObjects at hw + split_ifs at hw + · exact mm _ (fun a s q hq => ut_mop_frameA _ s q hq) _ _ _ hw + · exact mm _ (fun a s q hq => ut_mop_frameA _ s q hq) _ _ _ hw + · obtain rfl : w = ((), σb) := hw + exact ⟨rfl, rfl, rfl, rfl⟩ + obtain ⟨wk1, wk2, wk3, wk4⟩ := wk + -- the heap the `create_cap` loop starts from + have kb : ∀ q, w.2.kheap q = + if t ≠ .untyped ∧ q ∈ addrs then some (defaultObject t dev objSz σ.curDomain) else σa.kheap q := by + intro q + rw [wk1, e2] + by_cases h : t = .untyped + · rw [ite_eq_left h, ite_eq_right (fun g => g.1 h)] + · rw [ite_eq_right h, utRetSt_kheapA] + by_cases hq : q ∈ addrs + · rw [ite_eq_left (show t ≠ .untyped ∧ q ∈ addrs from ⟨h, hq⟩)] + exact ite_eq_left hq + · rw [ite_eq_right (show ¬ (t ≠ .untyped ∧ q ∈ addrs) from fun g => hq g.2)] + exact ite_eq_right hq + have eb : σb.cdt = σ.cdt ∧ σb.isOriginalCap = σ.isOriginalCap ∧ σb.curDomain = σ.curDomain := by + rw [e2] + split_ifs <;> exact ⟨rfl, rfl, rfl⟩ + have zipfst : l.map Prod.fst = slots := by + refine List.map_fst_zip ?_ + simp [addrs, fl4RetypeAddrs] + have hcn : ∀ x ∈ l, ∃ sz cn, w.2.kheap x.1.1 = some (.cnode sz cn) := by + intro x hx + have hx1 : x.1 ∈ slots := by rw [← zipfst]; exact List.mem_map_of_mem hx + obtain ⟨⟨sz, cn, h⟩, ha⟩ := hd _ hx1 + rw [kb, ite_eq_right (fun g => ha g.2)] + by_cases e : x.1.1 = srcSlot.1 + · exact ⟨rS, _, by rw [e]; exact Function.update_self ..⟩ + · exact ⟨sz, cn, (Function.update_of_ne e ..).trans h⟩ + obtain ⟨A, B, C, D, E⟩ := utMapCreate_invA l w.2 hcn hr + refine ⟨hdomS, fun q sz cn hq h => ?_, fun ht q hq => ?_, fun q hq h => ?_, fun x => ?_, fun x => ?_, ?_⟩ + · exact A q sz cn (by rw [kb, ite_eq_right (fun g => hq.elim g.1 (fun n => n g.2))]; exact h) + · have nc : ∀ sz cn, w.2.kheap q ≠ some (defaultObject t dev objSz σ.curDomain) → + w.2.kheap q ≠ some (.cnode sz cn) := fun sz cn h => + absurd (by rw [kb, ite_eq_left (show t ≠ .untyped ∧ q ∈ addrs from ⟨ht, hq⟩)]) h + by_cases hcnode : ∃ sz cn, w.2.kheap q = some (.cnode sz cn) + · obtain ⟨sz, cn, h⟩ := hcnode + have off : ∀ idx, utWritesA t objSz dev l q cn idx = cn idx := fun idx => + utWritesA_off l cn fun x hx e => by + have hx1 : x.1 ∈ slots := by rw [← zipfst]; exact List.mem_map_of_mem hx + exact (hd _ hx1).2 (by rw [e]; exact hq) + rw [(A q sz cn h).1, funext off, ← h, kb, ite_eq_left (show t ≠ .untyped ∧ q ∈ addrs from ⟨ht, hq⟩)] + · rw [B q (fun sz cn h => hcnode ⟨sz, cn, h⟩), kb, ite_eq_left (show t ≠ .untyped ∧ q ∈ addrs from ⟨ht, hq⟩)] + · have : w.2.kheap q = σa.kheap q := by rw [kb, ite_eq_right (fun g => hq.elim g.1 (fun n => n g.2))] + rw [B q (fun sz cn => by rw [this]; exact h sz cn), this] + · rw [C x, zipfst, wk2, eb.1] + · rw [D x, zipfst, wk3, eb.2.1] + · rw [E, wk4, eb.2.2] + +/-- The old objects' part of `utEnd_stateRelA`, over an abstract lowered-untyped cap `u`, write list `l` and set of +new addresses `New`. -/ +theorem utStateOldA {S : Type} {st : machine.Store S} [L : LawfulStore st] {H1 : Heap} {Sg OS1 : Set U64} + {s1 s' : S} {slot p sb : U64} {dev : Bool} {aligned nt us dest off win : U64} + (E : CrtE st H1 Sg OS1 s1 s' slot p sb dev aligned nt us dest off win) + {th : TypedHeapA} {ksb : U64} (P : PlaceA th ksb) (hsub : ∀ y, IsSlotA th y → y ∈ OS1) + {σ : State Unit} (hS : StateRelA th (L.mem s1) σ) {t : ApiObjectType} {srcSlot : CslotPtr} + {slots : List CslotPtr} (hlen : slots.length = win.val) {r : State Unit} {u : Cap} + {l : List (CslotPtr × ObjRef)} {New : ObjRef → Prop} + {qS : U64} (eqS : srcSlot.1 = specPtr qS) {csS : CnodeContents} + (hkS : σ.kheap srcSlot.1 = some (.cnode srcSlot.2.length csS)) {capS : Cap} (hcapS : csS srcSlot.2 = some capS) + (newOff : ∀ q k, th q = some k → ¬ New (specPtr q)) + (hC : ∀ q sz cn, ¬ New q → (setObjA σ srcSlot.1 (.cnode srcSlot.2.length + (Function.update csS srcSlot.2 (some u)))).kheap q = some (.cnode sz cn) → + r.kheap q = some (.cnode sz (utWritesA t us.val dev l q cn)) ∧ + ∀ idx, (utWritesA t us.val dev l q cn idx = none ↔ cn idx = none)) + (hK : ∀ q, ¬ New q → (∀ sz cn, (setObjA σ srcSlot.1 (.cnode srcSlot.2.length + (Function.update csS srcSlot.2 (some u)))).kheap q ≠ some (.cnode sz cn)) → + r.kheap q = (setObjA σ srcSlot.1 (.cnode srcSlot.2.length (Function.update csS srcSlot.2 (some u)))).kheap q) + (hsa : cslotAddrA srcSlot = slot) (absS : AbsSlotA th srcSlot) + (hdst : ∀ j (h : j < slots.length), cslotAddrA (slots[j]'h) = djA dest off j ∧ + ∃ q, (slots[j]'h).1 = specPtr q ∧ th q = some (.cnode (slots[j]'h).2.length)) + (zipfst : l.map Prod.fst = slots) (nd : slots.Nodup) + (hzip : ∀ j (h : j < slots.length), (slots[j]'h, specPtr (bjA aligned (zA nt.val us) j)) ∈ l) + (hsrcRel : CapRelA (capA st s' slot) u) + (hnewRel : ∀ j, j < win.val → CapRelA (newCapA nt.val (bjA aligned (zA nt.val us) j) us dev) + (defaultCap t (specPtr (bjA aligned (zA nt.val us) j)) us.val dev)) : + StateRelA th (L.mem s') r := by + have absD : ∀ j (h : j < slots.length), AbsSlotA th (slots[j]'h) := fun j h => by + obtain ⟨-, q, eq, hq⟩ := hdst j h + exact ⟨q, eq, Or.inl hq⟩ + have cA : ∀ y, capAtA (L.mem s') y = capA st s' y ∧ capAtA (L.mem s1) y = capA st s1 y := + fun y => ⟨capAtA_eq .., capAtA_eq ..⟩ + have keep : ∀ sl, AbsSlotA th sl → sl ≠ srcSlot → sl ∉ slots → ∀ oc, + SlotRelA (L.mem s1) (cslotAddrA sl) oc → SlotRelA (L.mem s') (cslotAddrA sl) oc := by + intro sl habs hne hnd oc ⟨ac, e, hrel⟩ + refine ⟨ac, e, ?_⟩ + have hos := hsub _ (habs.isSlot P) + have ys : cslotAddrA sl ≠ slot := fun e => hne (cslotAddrA_inj P habs absS (e.trans hsa.symm)) + have yd : ∀ j, j < win.val → cslotAddrA sl ≠ djA dest off j := fun j hj e => by + have hj' : j < slots.length := by omega + have := cslotAddrA_inj P habs (absD j hj') (e.trans (hdst j hj').1.symm) + exact hnd (this ▸ List.getElem_mem hj') + rw [(cA _).1, E.keep _ hos ys yd, ← (cA _).2]; exact hrel + refine ⟨fun q rd hq => ?_, fun q hq => ?_⟩ + · obtain ⟨cs, hk, hwf, hs⟩ := hS.cnode q rd hq + obtain ⟨cs', hk', hdom', hsl⟩ : ∃ cs', (setObjA σ srcSlot.1 (.cnode srcSlot.2.length + (Function.update csS srcSlot.2 (some u)))).kheap (specPtr q) = some (.cnode rd cs') ∧ + (∀ idx, cs' idx = none ↔ cs idx = none) ∧ + ∀ idx : List Bool, idx.length = rd → ((specPtr q, idx) = srcSlot → cs' idx = some u) + ∧ ((specPtr q, idx) ≠ srcSlot → cs' idx = cs idx) := by + by_cases e : specPtr q = srcSlot.1 + · have eq : q = qS := ut_specPtr_injA (e.trans eqS) + subst eq + rw [← e] at hkS + rw [hk] at hkS + injection hkS with h1 + injection h1 with h2 h3 + subst h3 + refine ⟨_, by rw [e, h2]; exact Function.update_self .., fun idx => ?_, fun idx _ => ⟨fun es => ?_, + fun es => ?_⟩⟩ + · by_cases ei : idx = srcSlot.2 + · subst ei + rw [Function.update_self] + exact ⟨fun h => by simp at h, fun h => by rw [hcapS] at h; cases h⟩ + · rw [Function.update_of_ne ei] + · rw [← es]; exact Function.update_self .. + · exact Function.update_of_ne (fun ei => es (Prod.ext e ei)) .. + · refine ⟨cs, (Function.update_of_ne e ..).trans hk, fun _ => Iff.rfl, fun idx _ => ⟨fun es => ?_, + fun _ => rfl⟩⟩ + exact absurd (congrArg Prod.fst es) e + obtain ⟨hf, hdm⟩ := hC (specPtr q) rd cs' (newOff q _ hq) hk' + refine ⟨_, hf, ?_, fun idx hl => ?_⟩ + · unfold wellFormedCnodeN at hwf ⊢ + rw [← hwf] + ext i + show utWritesA t us.val dev _ (specPtr q) cs' i ≠ none ↔ cs i ≠ none + rw [Ne, Ne, hdm i, hdom' i] + have habs : AbsSlotA th (specPtr q, idx) := ⟨q, rfl, Or.inl (hl ▸ hq)⟩ + have ea : slotAddrA (specPtr q) (ofBl idx) = cslotAddrA (specPtr q, idx) := rfl + rw [ea] + by_cases hd : (specPtr q, idx) ∈ slots + · obtain ⟨j, hj, ej⟩ := List.getElem_of_mem hd + have hj' : j < win.val := by omega + have hz := hzip j hj + rw [ej] at hz + refine ⟨_, utWritesA_on _ _ (by rw [zipfst]; exact nd) hz, ?_⟩ + rw [(cA _).1, ← ej, (hdst j hj).1, E.new j hj'] + exact hnewRel j hj' + · rw [utWritesA_off _ _ (fun x hx e => hd (by rw [← e, ← zipfst]; exact List.mem_map_of_mem hx))] + obtain ⟨a, b⟩ := hsl idx hl + by_cases es : (specPtr q, idx) = srcSlot + · refine ⟨_, a es, ?_⟩ + rw [(cA _).1, es, hsa] + exact hsrcRel + · rw [b es] + exact keep _ habs es hd _ (hs idx hl) + · obtain ⟨x, hk, hs⟩ := hS.tcb q hq + have ne : specPtr q ≠ srcSlot.1 := fun e => by rw [e, hkS] at hk; cases hk + have ka : (setObjA σ srcSlot.1 (.cnode srcSlot.2.length (Function.update csS srcSlot.2 (some u)))).kheap + (specPtr q) = some (.tcb x) := (Function.update_of_ne ne ..).trans hk + refine ⟨x, (hK _ (newOff q _ hq) (fun sz cn h => by rw [ka] at h; cases h)).trans ka, fun i hi => ?_⟩ + have habs : AbsSlotA th (specPtr q, tcbCnodeIndex i) := ⟨q, rfl, Or.inr ⟨hq, i, hi, rfl⟩⟩ + have ea : slotAddrA (specPtr q) (BitVec.ofNat 64 i) = cslotAddrA (specPtr q, tcbCnodeIndex i) := by + have hi7 : i < 7 := hi + unfold cslotAddrA + rw [ofBl_tcbIdx i (by omega)] + rw [ea] + refine keep _ habs (fun e => ne (congrArg Prod.fst e)) (fun m => ?_) _ (ea ▸ hs i hi) + obtain ⟨j, hj, e⟩ := List.getElem_of_mem m + obtain ⟨-, q', eq', hq'⟩ := hdst j hj + rw [e] at eq' + rw [ut_specPtr_injA eq', hq'] at hq + cases hq + +/-! ## `StateRelA` after the cap writes -/ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **`StateRelA` after the Retype**: FL4's final state against the spec's outcome, at the new typed heap. -/ +theorem utEnd_stateRelA {H H1 H' : Heap} {Cs OS OS1 : Set U64} {s0 s1 s' : S} {slot p sb : U64} {dev : Bool} + {lo hi : Nat} {aligned nt us dest off win : U64} + (U : UtEnd st H Cs OS s0 slot p sb dev H1 H' OS1 s1 s' lo hi aligned nt us dest off win) + {th : TypedHeapA} {ksb : U64} (P : PlaceA th ksb) (hsub : ∀ y, IsSlotA th y → y ∈ OS1) + {σ : State Unit} (hS : StateRelA th (L.mem s1) σ) + {t : ApiObjectType} (htt : objTypeOfA nt.val = some t) (hus : us.val < 64) + {srcSlot : CslotPtr} {base : Word} {slots : List CslotPtr} (hlen : slots.length = win.val) + {r : Unit × State Unit} (hr : r ∈ (utRtBodyA srcSlot base (specPtr aligned) t us.val slots dev σ).1) + (hsrcS : cslotAddrA srcSlot = slot ∧ ∃ q, srcSlot.1 = specPtr q ∧ th q = some (.cnode srcSlot.2.length)) + (hdst : ∀ j (h : j < slots.length), cslotAddrA (slots[j]'h) = djA dest off j ∧ + ∃ q, (slots[j]'h).1 = specPtr q ∧ th q = some (.cnode (slots[j]'h).2.length)) + (hsn : ∀ j, j < win.val → djA dest off j ≠ slot) + (hfree : ∀ j, j < win.val → th (bjA aligned (zA nt.val us) j) = none) + (hal : aligned.val % 8 = 0) (hc47 : nt.val = 4 → us.val ≤ 47) + (hsrcRel : ∀ capS, σ.kheap srcSlot.1 ≠ none → (∃ rS cs, σ.kheap srcSlot.1 = some (.cnode rS cs) ∧ + cs srcSlot.2 = some capS) → + CapRelA (capA st s' slot) (.untypedCap dev base (bitsOf capS) + (unat (specPtr aligned + BitVec.ofNat 64 slots.length <<< fl4ObjBitsApi t us.val - base)))) + (hnewRel : ∀ j, j < win.val → CapRelA (newCapA nt.val (bjA aligned (zA nt.val us) j) us dev) + (defaultCap t (specPtr (bjA aligned (zA nt.val us) j)) us.val dev)) : + StateRelA (utRtThA th dev nt us aligned win.val) (L.mem s') r.2 := by + have hn : nt.val < 15 := (objTypeOfA_some htt us.val).1 + obtain ⟨hsa, qS, eqS, hqS⟩ := hsrcS + obtain ⟨csS, hkS, hwfS, hsS⟩ := hS.cnode qS _ hqS + rw [← eqS] at hkS + obtain ⟨capS, hcapS, -⟩ := hsS srcSlot.2 rfl + have newOff : ∀ q k, th q = some k → + specPtr q ∉ fl4RetypeAddrs fl4ObjBitsApi (specPtr aligned) slots.length us.val t := by + intro q k hq m + rw [hlen, utRet_addrs_memA htt hus] at m + obtain ⟨j, hj, e⟩ := m + rw [ut_specPtr_injA e, hfree j hj] at hq + cases hq + have hdc : ∀ d ∈ slots, (∃ sz cn, σ.kheap d.1 = some (.cnode sz cn)) ∧ + d.1 ∉ fl4RetypeAddrs fl4ObjBitsApi (specPtr aligned) slots.length us.val t := by + intro d hd + obtain ⟨j, hj, rfl⟩ := List.getElem_of_mem hd + obtain ⟨-, q, eq, hq⟩ := hdst j hj + obtain ⟨cs, hk, -⟩ := hS.cnode q _ hq + rw [eq] + exact ⟨⟨_, cs, hk⟩, newOff q _ hq⟩ + obtain ⟨hdomS, hC, hN, hK, -, -, hcd⟩ := utRtBody_invA hr hkS hcapS (eqS ▸ newOff qS _ hqS) hdc + have absS : AbsSlotA th srcSlot := ⟨qS, eqS, Or.inl hqS⟩ + have absD : ∀ j (h : j < slots.length), AbsSlotA th (slots[j]'h) := fun j h => by + obtain ⟨-, q, eq, hq⟩ := hdst j h + exact ⟨q, eq, Or.inl hq⟩ + have snd : srcSlot ∉ slots := fun m => by + obtain ⟨j, hj, e⟩ := List.getElem_of_mem m + exact hsn j (by omega) (by rw [← (hdst j hj).1, e, hsa]) + have nd : slots.Nodup := by + refine List.nodup_iff_injective_get.2 fun a b e => ?_ + have ea := (hdst a.1 a.2).1 + have eb := (hdst b.1 b.2).1 + have e' : slots[a.1]'a.2 = slots[b.1]'b.2 := e + rw [e', eb] at ea + exact Fin.ext (utEnd_dstInjA U (by omega) (by omega) ea.symm) + have zipfst : (slots.zip (fl4RetypeAddrs fl4ObjBitsApi (specPtr aligned) slots.length us.val t)).map Prod.fst + = slots := by + refine List.map_fst_zip ?_ + simp [fl4RetypeAddrs] + have old : StateRelA th (L.mem s') r.2 := by + refine utStateOldA U.E P hsub hS hlen (u := .untypedCap dev base (bitsOf capS) + (unat (specPtr aligned + BitVec.ofNat 64 slots.length <<< fl4ObjBitsApi t us.val - base))) + (l := slots.zip (fl4RetypeAddrs fl4ObjBitsApi (specPtr aligned) slots.length us.val t)) + (New := fun q => q ∈ fl4RetypeAddrs fl4ObjBitsApi (specPtr aligned) slots.length us.val t) + eqS hkS hcapS newOff (fun q sz cn hq h => hC q sz cn (Or.inr hq) h) (fun q hq h => hK q (Or.inr hq) h) hsa absS hdst zipfst nd (fun j hj => ?_) + (hsrcRel capS (by rw [hkS]; simp) ⟨_, csS, hkS, hcapS⟩) hnewRel + have ha : j < (fl4RetypeAddrs fl4ObjBitsApi (specPtr aligned) slots.length us.val t).length := by + simpa [fl4RetypeAddrs] using hj + have eaddr : (fl4RetypeAddrs fl4ObjBitsApi (specPtr aligned) slots.length us.val t)[j]'ha = + specPtr (bjA aligned (zA nt.val us) j) := by + simp only [fl4RetypeAddrs, List.getElem_map, List.getElem_range, utRet_bitsA htt hus] + exact utRet_ptrA _ _ _ + have hjz : j < (slots.zip (fl4RetypeAddrs fl4ObjBitsApi (specPtr aligned) slots.length us.val t)).length := by + rw [List.length_zip]; exact lt_min hj ha + have := List.getElem_mem hjz + rwa [List.getElem_zip, eaddr] at this + have hbj : ∀ j, j < win.val → (bjA aligned (zA nt.val us) j).val = aligned.val + j * 2 ^ (zA nt.val us).val ∧ + aligned.val + j * 2 ^ (zA nt.val us).val + 2 ^ (zA nt.val us).val ≤ 2 ^ 64 := by + intro j hj + have h1 : (j + 1) * 2 ^ (zA nt.val us).val ≤ win.val * 2 ^ (zA nt.val us).val := Nat.mul_le_mul_right _ hj + rw [Nat.add_mul, Nat.one_mul] at h1 + have := U.A.aw + have := U.A.pf + exact ⟨(utEnd_bjA U hj).1, by omega⟩ + -- an untyped retype makes no object: the typed heap is unchanged + by_cases ht : t = .untyped + · have n0 : nt.val = 0 := (objTypeOfA_some htt us.val).2.2.2.1.1 ht + have e : utRtThA th dev nt us aligned win.val = th := by + funext q + exact ite_eq_right (fun h => h.2.1 n0) + rw [e] + exact old + have res := utRt_stateRelA (th := th) (dev := dev) (aligned := aligned) (len := win.val) htt hus old P hfree hbj hal + hc47 (fun _ _ => ⟨rfl, rfl⟩) + (fun h4 hd w ha h1 h2 => U.E.zero hd (by unfold WKA; omega) w ha h1 h2) + (fun h1 _ j hj K hK hK8 => (U.X.2.2 j hj h1).2.2.2.2.2 K hK hK8) + have ek : (utRetStA t dev us.val (fl4RetypeAddrs fl4ObjBitsApi (specPtr aligned) win.val us.val t) r.2).kheap = + r.2.kheap := by + funext q + rw [utRetSt_kheapA] + split_ifs with h + · rw [hN ht q (by rw [hlen]; exact h), hcd] + · rfl + exact ⟨fun q rd hq => by rw [← ek]; exact res.cnode q rd hq, fun q hq => by rw [← ek]; exact res.tcb q hq⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtRtCdtA.lean b/proofs/Fl4ProofsA64/P3/UtRtCdtA.lean new file mode 100644 index 000000000..06d74bace --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtRtCdtA.lean @@ -0,0 +1,497 @@ +/- +P3 (aarch64, A64-P3 Untyped W5): **the derivation tree after a Retype, and `CdtRelA`** (lead 01:47Z, item 2). + +* `utPA` / `utGA`: the ghost after the first `i` inserts — each destination's parent is the untyped slot, inserted + into its chain (A64 P1's `insG`). +* **`utRetype_treeXA`**: after a non-reset Retype, FL4's tree is `TreeA` at exactly that ghost (P1's `treeG`, which + hides it), and the revocable bits: set on every destination, kept elsewhere. +* `utEnd_newSlotZeroA`: a new CNode's or TCB's slot words are zero (`CrtE.zero`, `DbgX`). +* **`utEnd_cdtRelA`**: `CdtRelA` at the new typed heap, at FL4's final state against the spec's `cdt` and + original-cap updates (`utRtBody_invA`). + +Hypotheses: the record's `hsub` / `hnul` (closure item A) at the creation's start, the spec / FL4 slot +correspondence, `hcdtfree` / `horigfree` (no spec CDT or original bit at a new address, `SpecFreeA`'s shape), and +the untyped slot revocable (`UtRev`). No named premise. +-/ +import Fl4ProofsA64.P3.UtRtCapsA +import Fl4ProofsA64.P1.MdbUZL +import Fl4ProofsA64.P1.MdbTreeR +import Fl4ProofsA64.P3.EntryA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-- The parent map after the first `i` inserts. -/ +def utPA (src : U64) (dslot : Nat → U64) (par0 : U64 → Option U64) (i : Nat) : U64 → Option U64 := fun x => + if ∃ j < i, x = dslot j then some src else par0 x + +/-- The chains after the first `i` inserts. -/ +def utGA (src : U64) (dslot : Nat → U64) (G0 : List (List U64)) : Nat → List (List U64) + | 0 => G0 + | i + 1 => insG src (dslot i) (utGA src dslot G0 i) + +theorem utPA_zero (src : U64) (dslot : Nat → U64) (par0 : U64 → Option U64) : utPA src dslot par0 0 = par0 := by + funext x + unfold utPA + rw [ite_eq_right] + rintro ⟨j, hj, -⟩ + exact Nat.not_lt_zero j hj + +theorem utPA_succ (src : U64) (dslot : Nat → U64) (par0 : U64 → Option U64) (i : Nat) : + Function.update (utPA src dslot par0 i) (dslot i) (some src) = utPA src dslot par0 (i + 1) := by + funext x + by_cases h : x = dslot i + · subst h + rw [Function.update_self] + unfold utPA + rw [ite_eq_left ⟨i, by omega, rfl⟩] + · rw [Function.update_of_ne h] + unfold utPA + refine if_congr ⟨fun ⟨j, hj, e⟩ => ⟨j, by omega, e⟩, fun ⟨j, hj, e⟩ => ⟨j, ?_, e⟩⟩ rfl rfl + rcases Nat.lt_succ_iff_lt_or_eq.1 hj with g | rfl + · exact g + · exact absurd e h + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {T Ω Sg : Set U64} + +/-- The loop invariant at the explicit ghost (P1's `LoopA` with its ghosts named, and the revocable bits). -/ +def UtLoopA (st : machine.Store S) [L : LawfulStore st] (OS : Set U64) (G0 : List (List U64)) + (par0 : U64 → Option U64) (s1 : S) (src : U64) (dslot : Nat → U64) (cj : Nat → capability.Cap) (s : S) + (i : Nat) : Prop := + MdbLk st OS (utGA src dslot G0 i) s ∧ Fl4Proofs.P3.Mdb.MdbOK (utPA src dslot par0 i) (utGA src dslot G0 i) ∧ + src ∈ (utGA src dslot G0 i).flatten ∧ + (∀ x ∈ OS, (∀ j, j < i → x ≠ dslot j) → capA st s x = capA st s1 x) ∧ + (∀ j, j < i → capA st s (dslot j) = cj j) ∧ + (∀ y x, Fl4Proofs.P3.Mdb.Desc (utPA src dslot par0 i) y x ↔ Fl4Proofs.P3.Mdb.Desc par0 y x ∨ + ∃ j, j < i ∧ y = dslot j ∧ (x = src ∨ Fl4Proofs.P3.Mdb.Desc par0 src x)) ∧ + (∀ y ∈ OS, (∀ j, j < i → y ≠ dslot j) → L.mem s (wa y 24#u64) &&& RVB = L.mem s1 (wa y 24#u64) &&& RVB) ∧ + (∀ j, j < i → L.mem s (wa (dslot j) 24#u64) &&& RVB = RVB) + +variable {OS : Set U64} {G0 : List (List U64)} {par0 : U64 → Option U64} {s1 : S} {src : U64} {dslot : Nat → U64} + {cj : Nat → capability.Cap} + +/-- A step writing no `OS` word keeps the invariant. -/ +theorem UtLoopA.keep (_ : OSOK T Ω Sg OS) {s s' : S} {i : Nat} (h : UtLoopA st OS G0 par0 s1 src dslot cj s i) + (hdO : ∀ j, j < i → dslot j ∈ OS) (k : OSKeep st OS s s') : UtLoopA st OS G0 par0 s1 src dslot cj s' i := by + have hw : ∀ y ∈ OS, L.mem s' (wa y 24#u64) = L.mem s (wa y 24#u64) := fun y hy => (k y hy).2.2.2 + obtain ⟨hM, hT, hs, h1, h2, h3, h4, h5⟩ := h + refine ⟨hM.keep (fun x hx => ⟨(k x hx).2.2.1, (k x hx).2.2.2⟩) (fun x hx => by rw [k.capA hx]), hT, hs, fun x hx hn => (k.capA hx).trans (h1 x hx hn), + fun j hj => (k.capA (hdO j hj)).trans (h2 j hj), h3, fun y hy hn => by rw [hw y hy]; exact h4 y hy hn, + fun j hj => by rw [hw _ (hdO j hj)]; exact h5 j hj⟩ + +theorem UtLoopA.step (hO : OSOK T Ω Sg OS) {s : S} {i w : Nat} (hi : i < w) + (h : UtLoopA st OS G0 par0 s1 src dslot cj s i) (hdO : ∀ j, j < w → dslot j ∈ OS) + (hdn : ∀ j, j < w → capA st s1 (dslot j) = .Null) (hdc : ∀ j, j < w → obj.sext48 (dslot j) = ok (dslot j)) + (hinj : ∀ j l, j < w → l < w → dslot j = dslot l → j = l) (hsn : ∀ j, j < w → src ≠ dslot j) + (hwf : ∀ j, j < w → CapRt.EncWf (cj j)) (hcn : ∀ j, j < w → cj j ≠ .Null) : + cdt.insert_new_cap st s src (dslot i) (cj i) ⦃ s' => UtLoopA st OS G0 par0 s1 src dslot cj s' (i + 1) ⦄ := by + obtain ⟨hM, hT, hs, h1, h2, h3, h4, h5⟩ := h + have hne : ∀ j, j < i → dslot i ≠ dslot j := fun j hj e => by + have := hinj i j hi (by omega) e; omega + have hd0 : capA st s (dslot i) = .Null := by rw [h1 _ (hdO i hi) hne]; exact hdn i hi + have dG : dslot i ∉ (utGA src dslot G0 i).flatten := fun hm => ((hM.members _).1 hm).2 hd0 + have hsOS : src ∈ OS := ((hM.members src).1 hs).1 + have hN := (MdbLk.nxG hO hM hs).2 + refine WP.spec_mono (spec_andL (cdt.insert_new_cap.lkT hO hM hT hs (hdO i hi) hd0 (hdc i hi) (hwf i hi) (hcn i hi)) + (cdt.insert_new_cap.rvb hO hsOS (hdO i hi) (hsn i hi) (hwf i hi) hN)) ?_ + rintro s' ⟨⟨hM', hT', ed, ek⟩, rk, rd⟩ + rw [utPA_succ] at hT' + refine ⟨hM', hT', ?_, fun x hx hn => ?_, fun j hj => ?_, fun y x => ?_, fun y hy hn => ?_, fun j hj => ?_⟩ + · refine (hM'.members src).2 ⟨hsOS, ?_⟩ + rw [ek src hsOS (hsn i hi)]; exact ((hM.members src).1 hs).2 + · rw [ek x hx (hn i (by omega))]; exact h1 x hx fun j hj => hn j (by omega) + · by_cases hji : j = i + · subst hji; exact ed + · rw [ek _ (hdO j (by omega)) (fun e => hji (hinj j i (by omega) hi e))]; exact h2 j (by omega) + · have hpd : ∀ z, utPA src dslot par0 i z ≠ some (dslot i) := fun z e => dG (hT.par_mem z _ e).2 + have hdn' : utPA src dslot par0 i (dslot i) = none := by + cases e : utPA src dslot par0 i (dslot i) with + | none => rfl + | some q => exact absurd (hT.par_mem _ q e).1 dG + have hsrcd : ∀ x, Fl4Proofs.P3.Mdb.Desc (utPA src dslot par0 i) src x ↔ + Fl4Proofs.P3.Mdb.Desc par0 src x := fun x => by + rw [h3] + constructor + · rintro (h | ⟨j, hj, e, -⟩) + · exact h + · exact absurd e (hsn j (by omega)) + · exact Or.inl + rw [← utPA_succ, desc_upd_src hpd hdn' (hsn i hi), h3, hsrcd] + constructor + · rintro ((h | ⟨j, hj, e, hx⟩) | ⟨e, hx⟩) + · exact Or.inl h + · exact Or.inr ⟨j, by omega, e, hx⟩ + · exact Or.inr ⟨i, by omega, e, hx⟩ + · rintro (h | ⟨j, hj, e, hx⟩) + · exact Or.inl (Or.inl h) + · by_cases hji : j = i + · subst hji; exact Or.inr ⟨e, hx⟩ + · exact Or.inl (Or.inr ⟨j, by omega, e, hx⟩) + · rw [rk y hy (hn i (by omega))] + exact h4 y hy fun j hj => hn j (by omega) + · by_cases hji : j = i + · subst hji; exact rd + · rw [rk _ (hdO j (by omega)) (fun e => hji (hinj j i (by omega) hi e))]; exact h5 j (by omega) + +/-- **FL4's tree after a non-reset Retype, at the explicit ghost.** -/ +theorem utRetype_treeXA (hO : OSOK T Ω Sg OS) {G : List (List U64)} {par : U64 → Option U64} {s : S} + (hT : TreeA st OS G par s) (a : A) {src p sb fi : U64} {dev : Bool} (hsrc : src ∈ OS) + (hcs : capA st s src = .Untyped p sb fi dev) (nt us dcn off len base : U64) (device : Bool) (hn : nt.val < 15) + (hz : (zA nt.val us).val < 64) (hfi : fi.val < 2 ^ sb.val) (hA : p.val + fi.val ≤ base.val) + (hAw : base.val + len.val * 2 ^ (zA nt.val us).val ≤ p.val + 2 ^ sb.val) + {Zq : S → Nat → Prop} + (hCOZ : ∀ (s' : S) (a' : A) (j : Nat), j < len.val → Zq s' j → + objects.create_object st ar s' a' nt (bjA base (zA nt.val us) j) us device ⦃ r => + r.1 = newCapA nt.val (bjA base (zA nt.val us) j) us device ∧ OSKeep st OS s' r.2.1 ∧ Zq r.2.1 (j + 1) ⦄) + (hZW : ∀ (s1 s2 : S) (j : Nat), Zq s1 j → WritesIn st s1 s2 (W7.CapW T Ω Sg) → Zq s2 j) + (hZ0 : ∀ s1 : S, WritesIn st s s1 (fun b => b = src ∨ b = wa src 8#u64) → Zq s1 0) + (hdO : ∀ j, j < len.val → djA dcn off j ∈ OS) + (hdn : ∀ j, j < len.val → capA st s (djA dcn off j) = .Null) + (hdc : ∀ j, j < len.val → obj.sext48 (djA dcn off j) = ok (djA dcn off j)) + (hinj : ∀ j l, j < len.val → l < len.val → djA dcn off j = djA dcn off l → j = l) + (hcan : ∀ j, j < len.val → CapRt.Canonical (bjA base (zA nt.val us) j)) (hus : us.val < 64) : + untyped.retype st ar s a src false base nt us dcn off len device ⦃ r => r.1 = .Ok () ∧ + TreeA st OS (utGA src (djA dcn off) G len.val) (utPA src (djA dcn off) par len.val) r.2.1 ∧ + (∀ y ∈ OS, (∀ j, j < len.val → y ≠ djA dcn off j) → revA (L.mem r.2.1) y = revA (L.mem s) y) ∧ + (∀ j, j < len.val → revA (L.mem r.2.1) (djA dcn off j) = true) ⦄ := by + have hU := hT.ut + have hM := hT.lk + set z := zA nt.val us with hzdef + have hk1 : 1 ≤ 2 ^ z.val := Nat.one_le_two_pow + obtain ⟨-, -, hpfit⟩ := ut_rangeA (capA_ut_b hcs) (by + have := hU.reg src hsrc (by rw [hcs]; exact physA_ut _ _ _ _); rwa [hcs] at this) + have hsb : 2 ^ sb.val < 2 ^ 64 := Nat.pow_lt_pow_right (by decide) (capA_ut_b hcs) + have hs0 : capA st s src ≠ .Null := by rw [hcs]; exact fun h => by cases h + have hsG : src ∈ G.flatten := (hM.members src).2 ⟨hsrc, hs0⟩ + have hsn : ∀ j, j < len.val → src ≠ djA dcn off j := fun j hj e => hs0 (by rw [e]; exact hdn j hj) + have hwf : ∀ j, j < len.val → CapRt.EncWf (newCapA nt.val (bjA base z j) us device) := fun j hj => + newCapA_encWf _ _ _ _ (hcan j hj) hus + have hfit : base.val + len.val * 2 ^ z.val ≤ 2 ^ 64 := by omega + have hdn1 : ∀ s1, RtSrc st s src p sb dev base.val (base.val + len.val * 2 ^ z.val) s1 → + ∀ j, j < len.val → capA st s1 (djA dcn off j) = .Null := fun s1 h1 j hj => by + obtain ⟨-, -, -, hc1, -, -⟩ := h1.facts hO hsrc + rw [hc1 _ (hdO j hj) (fun e => hsn j hj e.symm)]; exact hdn j hj + refine WP.spec_mono (untyped.retype.genJK ar a hcs nt us dcn off len base device hn hz hfit + (J := fun s1 s' i => UtLoopA st OS G par s1 src (djA dcn off) + (fun j => newCapA nt.val (bjA base z j) us device) s' i ∧ Zq s' i) + (K := fun s1 s' i => UtLoopA st OS G par s1 src (djA dcn off) + (fun j => newCapA nt.val (bjA base z j) us device) s' i ∧ Zq s' (i + 1)) + (fun _ _ s' a' j hj h => WP.spec_mono (hCOZ s' a' j hj h.2) fun r hr => + ⟨hr.1, h.1.keep hO (fun l hl => hdO l (by omega)) hr.2.1, hr.2.2⟩) + (fun s1 h1 s' i hi h => spec_andL + (UtLoopA.step hO hi h.1 hdO (hdn1 s1 h1) hdc hinj hsn hwf (fun j _ => newCapA_ne _ _ _ _)) + (cdt.insert_new_cap.zqW hO hZW (hdO i hi) + (by exact MdbLk.nxG hO h.1.1 h.1.2.2.1) h.2)) + (fun s1 h1 => ?_)) ?_ + · have L0 := h1.lk hO hsrc hM hs0 + refine ⟨⟨L0, by rw [utPA_zero]; exact hT.tree, hsG, fun _ _ _ => rfl, + fun j hj => absurd hj (Nat.not_lt_zero j), fun y x => ?_, fun _ _ _ => rfl, + fun j hj => absurd hj (Nat.not_lt_zero j)⟩, + hZ0 s1 (by obtain ⟨-, -, -, -, -, hW⟩ := h1.facts hO hsrc; exact hW)⟩ + rw [utPA_zero] + exact ⟨Or.inl, fun h => h.elim id fun ⟨j, hj, _⟩ => absurd hj (Nat.not_lt_zero j)⟩ + rintro r ⟨hok, s1, h1, ⟨hM', hT', -, hF2b, hF2a, hF3, hr1, hr2⟩, -⟩ + obtain ⟨nfi, hnv0, hcs1, hc1, hl, -⟩ := h1.facts hO hsrc + have hnv : nfi.val = base.val + len.val * 2 ^ z.val - p.val := hnv0 (by omega) (by first | omega | (rw [← hzdef]; omega)) + refine ⟨hok, ⟨hM', hT', ?_⟩, fun y hy hne => ?_, fun j hj => ?_⟩ + swap + · unfold revA + rw [show (281474976710656#u64 : U64) = RVB from rfl, hr1 y hy hne, (hl y hy).2] + swap + · unfold revA + rw [show (281474976710656#u64 : U64) = RVB from rfl, hr2 j hj] + simp [RVB] + have hbj : ∀ j, j < len.val → (bjA base z j).val = base.val + j * 2 ^ z.val := fun j hj => by + have : (j + 1) * 2 ^ z.val ≤ len.val * 2 ^ z.val := Nat.mul_le_mul_right _ hj + rw [Nat.add_mul, Nat.one_mul] at this + exact bjA_val (by omega) + have dG : ∀ j, j < len.val → djA dcn off j ∉ G.flatten := fun j hj hm => ((hM.members _).1 hm).2 (hdn j hj) + have hR : RetypeDeltaA st OS par (utPA src (djA dcn off) par len.val) s s1 r.2.1 src p sb fi nfi dev base.val + (2 ^ z.val) len.val (djA dcn off) (fun j => newCapA nt.val (bjA base z j) us device) := by + refine ⟨hT.parIn, hsrc, hcs, hcs1, hc1, hfi, hA, by rw [hnv]; omega, by omega, hk1, + fun j _ => newCapA_phys _ _ _ _, fun j hj => ?_, fun j hj => ?_, fun j hj hu => ?_, hdn, hinj, + fun j hj y => ⟨Fl4Proofs.P3.Mdb.not_desc_of_none ?_, + Fl4Proofs.P3.Mdb.not_desc_of_no_child fun q e => dG j hj (hT.tree.par_mem q _ e).2⟩, + hF2a, hF2b, hF3⟩ + · show baseA _ = _ + unfold baseA; rw [newCapA_ptr, hbj j hj] + · have hjk : (j + 1) * 2 ^ z.val ≤ len.val * 2 ^ z.val := Nat.mul_le_mul_right _ hj + rw [Nat.add_mul, Nat.one_mul] at hjk + have e1 := (newCapA_range hn (bjA base z j) us device hz + (by rw [hbj j hj, ← hzdef]; omega)).2 + rw [← hzdef, hbj j hj] at e1 + rw [e1] + · obtain ⟨h0, e⟩ := newCapA_ut hu + refine ⟨us, device, e, ?_⟩ + rw [hzdef, h0]; simp [zA] + · cases e : par (djA dcn off j) with + | none => rfl + | some q => exact absurd (hT.tree.par_mem _ q e).1 (dG j hj) + exact hR.utinv hU + +end + +/-! ## `CdtRelA` -/ + +open Fl4SpecA64 Fl4Spec + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {H H1 H' : Heap} {Cs OS OS1 : Set U64} {s0 s1 s' : S} {slot p sb : U64} {dev : Bool} {lo hi : Nat} + {aligned nt us dest off win : U64} + +/-- **A new CNode's or TCB's slot words are zero** (`CrtE.zero`, `DbgX`). -/ +theorem utEnd_newSlotZeroA + (U : UtEnd st H Cs OS s0 slot p sb dev H1 H' OS1 s1 s' lo hi aligned nt us dest off win) + {th : TypedHeapA} (hn : nt.val < 15) (hus : us.val < 64) (hc47 : nt.val = 4 → us.val ≤ 47) + (hal : aligned.val % 8 = 0) {y : U64} (hy : IsSlotA (utRtThA th dev nt us aligned win.val) y) + (hold : ¬ IsSlotA th y) : + L.mem s' y = 0#u64 ∧ L.mem s' (wa y 8#u64) = 0#u64 ∧ L.mem s' (wa y 24#u64) = 0#u64 := by + obtain ⟨q, hq⟩ := hy + -- the slot's object is new + have isnew : ∀ k, utRtThA th dev nt us aligned win.val q = some k → th q ≠ some k → + UtNewA dev nt us aligned win.val q ∧ k = utKindA nt us := fun k h ho => by + rcases utRtTh_splitA h with ⟨-, o⟩ | n + · exact absurd o ho + · exact n + -- a word of window `j` at offset `o` + have word : ∀ j, j < win.val → ∀ i o : Nat, o = 32 * i ∨ o = 32 * i + 8 ∨ o = 32 * i + 24 → + 32 * i + 32 ≤ 2 ^ (zA nt.val us).val → (zA nt.val us).val ≤ 52 → + ∀ w : U64, w.val = (bjA aligned (zA nt.val us) j).val + o → Aligned w ∧ + aligned.val ≤ w.val ∧ w.val < aligned.val + win.val * 2 ^ (zA nt.val us).val := by + intro j hj i o ho hle hz w hw + obtain ⟨hv, -⟩ := utEnd_bjA U hj + have h1 : (j + 1) * 2 ^ (zA nt.val us).val ≤ win.val * 2 ^ (zA nt.val us).val := Nat.mul_le_mul_right _ hj + rw [Nat.add_mul, Nat.one_mul] at h1 + have h8 : j * 2 ^ (zA nt.val us).val % 8 = 0 := by + have : 8 ∣ 2 ^ (zA nt.val us).val := by + have h32 : 3 ≤ (zA nt.val us).val := by + have := U.A.z4 + omega + exact (Nat.pow_dvd_pow 2 h32) + exact Nat.mod_eq_zero_of_dvd (Dvd.dvd.mul_left this _) + rw [hv] at hw + exact ⟨by show _ % 8 = 0; omega, by omega, by omega⟩ + -- the slot's address and its three words + have addr : ∀ j, j < win.val → ∀ i : Nat, 32 * i + 32 ≤ 2 ^ (zA nt.val us).val → (zA nt.val us).val ≤ 52 → + y = slotAddrA (specPtr (bjA aligned (zA nt.val us) j)) (BitVec.ofNat 64 i) → + y.val = (bjA aligned (zA nt.val us) j).val + 32 * i ∧ + (wa y 8#u64).val = (bjA aligned (zA nt.val us) j).val + (32 * i + 8) ∧ + (wa y 24#u64).val = (bjA aligned (zA nt.val us) j).val + (32 * i + 24) := by + intro j hj i hle hz ey + obtain ⟨hv, -⟩ := utEnd_bjA U hj + have h1 : (j + 1) * 2 ^ (zA nt.val us).val ≤ win.val * 2 ^ (zA nt.val us).val := Nat.mul_le_mul_right _ hj + rw [Nat.add_mul, Nat.one_mul] at h1 + have h2 := U.A.aw + have h3 := U.A.pf + have hX : 2 ^ (zA nt.val us).val ≤ 2 ^ 52 := Nat.pow_le_pow_right (by norm_num) hz + have hfit : (bjA aligned (zA nt.val us) j).val + 2 ^ (zA nt.val us).val ≤ 2 ^ 64 := by omega + have e0 : y.val = (bjA aligned (zA nt.val us) j).val + 32 * i := by + rw [ey, slotAddrA_sp] + show ((bjA aligned (zA nt.val us) j).bv + BitVec.ofNat 64 i * 32#64).toNat = _ + have hb : (bjA aligned (zA nt.val us) j).bv.toNat = (bjA aligned (zA nt.val us) j).val := rfl + have e1 : (BitVec.ofNat 64 i).toNat = i := by rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + have e3 : (32#64 : BitVec 64).toNat = 32 := rfl + rw [BitVec.toNat_add, BitVec.toNat_mul, hb, e1, e3, Nat.mod_eq_of_lt (show i * 32 < 2 ^ 64 by omega), + Nat.mod_eq_of_lt (by omega)] + omega + have ewa : ∀ k : U64, k.val < 32 → (wa y k).val = y.val + k.val := fun k hk => by + rw [wa_val]; exact Nat.mod_eq_of_lt (by omega) + have k8 : (8#u64 : U64).val = 8 := by simp + have k24 : (24#u64 : U64).val = 24 := by simp + exact ⟨e0, by rw [ewa _ (by omega), e0, k8]; omega, by rw [ewa _ (by omega), e0, k24]; omega⟩ + rcases hq with ⟨r, hk, i, hi, ey⟩ | ⟨hk, i, hi, ey⟩ + · by_cases ho : th q = some (.cnode r) + · exact absurd ⟨q, Or.inl ⟨r, ho, i, hi, ey⟩⟩ hold + obtain ⟨⟨hd, h0, -, j, hj, eq⟩, ek⟩ := isnew _ hk ho + unfold utKindA at ek + split_ifs at ek with h1 h4 + cases ek + have h47 := hc47 h4 + have ez : (zA nt.val us).val = us.val + 5 := by + rw [zA_valA nt us hn hus]; simp [zOfA, h4, Nat.add_comm] + have hle : 32 * i + 32 ≤ 2 ^ (zA nt.val us).val := by + rw [ez, pow_add]; omega + rw [eq] at ey + obtain ⟨a0, a8, a24⟩ := addr j hj i hle (by omega) ey + have zr : ∀ (w : U64) (o : Nat), o = 32 * i ∨ o = 32 * i + 8 ∨ o = 32 * i + 24 → + w.val = (bjA aligned (zA nt.val us) j).val + o → L.mem s' w = 0#u64 := fun w o ho hw => by + obtain ⟨b1, b2, b3⟩ := word j hj i o ho hle (by omega) w hw + exact U.E.zero hd (by unfold WKA; omega) w b1 b2 b3 + exact ⟨zr y _ (Or.inl rfl) a0, zr _ _ (Or.inr (Or.inl rfl)) a8, zr _ _ (Or.inr (Or.inr rfl)) a24⟩ + · by_cases ho : th q = some .tcb + · exact absurd ⟨q, Or.inr ⟨ho, i, hi, ey⟩⟩ hold + obtain ⟨⟨hd, h0, -, j, hj, eq⟩, ek⟩ := isnew _ hk ho + have h1 : nt.val = 1 := by + unfold utKindA at ek + split_ifs at ek with h1 h4 + exact h1 + have ez : (zA nt.val us).val = 12 := by rw [h1]; exact zA_1 us + have hi7 : i < 7 := hi + have hle : 32 * i + 32 ≤ 2 ^ (zA nt.val us).val := by + rw [ez]; omega + rw [eq] at ey + obtain ⟨a0, a8, a24⟩ := addr j hj i hle (by omega) ey + have zr : ∀ (w : U64) (o : Nat), o < 224 → o % 8 = 0 → w.val = (bjA aligned (zA nt.val us) j).val + o → + L.mem s' w = 0#u64 := fun w o ho h8 hw => by + have ev : (⟨BitVec.ofNat 64 o⟩ : U64).val = o := by + show (BitVec.ofNat 64 o).toNat = _ + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)] + have ew : w = wa (bjA aligned (zA nt.val us) j) ⟨BitVec.ofNat 64 o⟩ := by + apply UScalar.eq_of_val_eq + rw [wa_val, ev, ← hw] + exact (Nat.mod_eq_of_lt (by have := w.hBounds; simpa using this)).symm + rw [ew] + exact (U.X.2.2 j hj h1).2.2.2.2.2 _ (by rw [ev]; exact ho) (by rw [ev]; exact h8) + exact ⟨zr y _ (by omega) (by omega) a0, zr _ _ (by omega) (by omega) a8, zr _ _ (by omega) (by omega) a24⟩ + +/-- **`CdtRelA` after the Retype.** -/ +theorem utEnd_cdtRelA + (U : UtEnd st H Cs OS s0 slot p sb dev H1 H' OS1 s1 s' lo hi aligned nt us dest off win) + {th : TypedHeapA} {ksb : U64} (P' : PlaceA (utRtThA th dev nt us aligned win.val) ksb) + (P : PlaceA th ksb) (hfree : ∀ q, UtNewA dev nt us aligned win.val q → th q = none) + (hn : nt.val < 15) (hus : us.val < 64) (hc47 : nt.val = 4 → us.val ≤ 47) (hal : aligned.val % 8 = 0) + (hsub : ∀ y, IsSlotA th y → y ∈ OS1) (hsn : ∀ j, j < win.val → djA dest off j ≠ slot) + (hnul : ∀ y ∈ OS1, ¬ IsSlotA th y → capA st s1 y = .Null) + {σ : State Unit} {G : MdbGhostA} (C : CdtRelA st th s1 σ G) + (hrev : revA (L.mem s1) slot = true) + (htree : TreeA st OS1 (utGA slot (djA dest off) G.chains win.val) (utPA slot (djA dest off) G.par win.val) s') + (hr1 : ∀ y ∈ OS1, (∀ j, j < win.val → y ≠ djA dest off j) → revA (L.mem s') y = revA (L.mem s1) y) + (hr2 : ∀ j, j < win.val → revA (L.mem s') (djA dest off j) = true) + {srcSlot : CslotPtr} {slots : List CslotPtr} (hlen : slots.length = win.val) + (hsa : cslotAddrA srcSlot = slot) (habsS : AbsSlotA th srcSlot) + (habsD : ∀ j (h : j < slots.length), cslotAddrA (slots[j]'h) = djA dest off j ∧ AbsSlotA th (slots[j]'h)) + {σ' : State Unit} (hcdt : ∀ x, σ'.cdt x = if x ∈ slots then some srcSlot else σ.cdt x) + (horig : ∀ x, σ'.isOriginalCap x = if x ∈ slots then true else σ.isOriginalCap x) + (hcdtfree : ∀ a, ¬ AbsSlotA th a → AbsSlotA (utRtThA th dev nt us aligned win.val) a → σ.cdt a = none) + (horigfree : ∀ a, ¬ AbsSlotA th a → AbsSlotA (utRtThA th dev nt us aligned win.val) a → + σ.isOriginalCap a = false) : + CdtRelA st (utRtThA th dev nt us aligned win.val) s' σ' + ⟨utGA slot (djA dest off) G.chains win.val, utPA slot (djA dest off) G.par win.val⟩ := by + set th' := utRtThA th dev nt us aligned win.val with hth' + have grow : ∀ q k, th q = some k → th' q = some k := fun q k h => utRtTh_growA hfree h + have growS : ∀ y, IsSlotA th y → IsSlotA th' y := fun y h => utRt_isSlot_growA hfree h + have growA : ∀ a, AbsSlotA th a → AbsSlotA th' a := fun a ⟨q, e, h⟩ => + ⟨q, e, h.imp (grow q _) (fun ⟨h1, h2⟩ => ⟨grow q _ h1, h2⟩)⟩ + have dstS : ∀ j, j < win.val → IsSlotA th (djA dest off j) := fun j hj => by + have hj' : j < slots.length := by omega + obtain ⟨e, ha⟩ := habsD j hj' + rw [← e]; exact ha.isSlot P + have dstN : ∀ j, j < win.val → capA st s1 (djA dest off j) = .Null := fun j hj => + (U.k1 _ (U.dO j hj) (hsn j hj)).trans (U.A.dn0 j hj) + -- a slot of the new heap that is no old one is no destination, no old tree member + have newPar : ∀ a, ¬ AbsSlotA th a → AbsSlotA th' a → G.par (cslotAddrA a) = none ∧ + (∀ j, j < win.val → cslotAddrA a ≠ djA dest off j) ∧ a ∉ slots := by + intro a ho hn' + have ns : a ∉ slots := fun m => by + obtain ⟨j, hj, e⟩ := List.getElem_of_mem m + exact ho (e ▸ (habsD j hj).2) + refine ⟨?_, fun j hj e => ?_, ns⟩ + · cases h : G.par (cslotAddrA a) with + | none => rfl + | some b => + obtain ⟨sl, hsl, e⟩ := C.par_slot _ _ h + exact absurd (cslotAddrA_inj P' (growA _ hsl) hn' e ▸ hsl) ho + · have hj' : j < slots.length := by omega + obtain ⟨ed, ha⟩ := habsD j hj' + exact ho (cslotAddrA_inj P' (growA _ ha) hn' (ed.trans e.symm) ▸ ha) + -- membership of the destinations + have memD : ∀ a, AbsSlotA th' a → (a ∈ slots ↔ ∃ j < win.val, cslotAddrA a = djA dest off j) := by + intro a ha' + constructor + · intro m + obtain ⟨j, hj, e⟩ := List.getElem_of_mem m + exact ⟨j, by omega, e ▸ (habsD j hj).1⟩ + · rintro ⟨j, hj, e⟩ + have hj' : j < slots.length := by omega + obtain ⟨ed, ha⟩ := habsD j hj' + have := cslotAddrA_inj P' ha' (growA _ ha) (e.trans ed.symm) + exact this ▸ List.getElem_mem hj' + have oldOS : ∀ a, AbsSlotA th a → cslotAddrA a ∈ OS1 := fun a h => hsub _ (h.isSlot P) + refine ⟨?_, ?_, ?_, ?_, ?_, ?_⟩ + · -- the tree over the new typed heap's slots + have g := htree.grow (OS' := OS1 ∪ {y | IsSlotA th' y}) Set.subset_union_left fun y hy hn1 => by + have hs : IsSlotA th' y := hy.resolve_left hn1 + have ho : ¬ IsSlotA th y := fun h => hn1 (hsub y h) + obtain ⟨z0, z8, -⟩ := utEnd_newSlotZeroA U hn hus hc47 hal hs ho + rw [← capAtA_eq]; exact ut_capAt_zeroA _ _ z0 z8 + refine g.sub (fun y hy => Or.inr hy) fun y hy hno => ?_ + have h1 : y ∈ OS1 := hy.resolve_right hno + have ho : ¬ IsSlotA th y := fun h => hno (growS y h) + have ys : y ≠ slot := fun e => ho (by rw [e, ← hsa]; exact habsS.isSlot P) + have yd : ∀ j, j < win.val → y ≠ djA dest off j := fun j hj e => ho (e ▸ dstS j hj) + rw [U.E.keep y h1 ys yd]; exact hnul y h1 ho + · -- `cdt` + intro a ha' + show (σ'.cdt a).map cslotAddrA = utPA slot (djA dest off) G.par win.val (cslotAddrA a) + rw [hcdt] + unfold utPA + by_cases m : a ∈ slots + · rw [ite_eq_left m, ite_eq_left ((memD a ha').1 m), Option.map_some, hsa] + · rw [ite_eq_right m, ite_eq_right (fun h => m ((memD a ha').2 h))] + by_cases o : AbsSlotA th a + · exact C.cdt a o + · rw [hcdtfree a o ha', (newPar a o ha').1]; rfl + · -- `cdt_slot` + intro a q h + rw [hcdt] at h + split_ifs at h with m + · cases h + obtain ⟨j, hj, e⟩ := List.getElem_of_mem m + exact ⟨growA _ (e ▸ (habsD j hj).2), growA _ habsS⟩ + · obtain ⟨h1, h2⟩ := C.cdt_slot a q h + exact ⟨growA _ h1, growA _ h2⟩ + · -- `orig` + intro a ha' + show σ'.isOriginalCap a = revA (L.mem s') (cslotAddrA a) + rw [horig] + by_cases m : a ∈ slots + · rw [ite_eq_left m] + obtain ⟨j, hj, e⟩ := (memD a ha').1 m + rw [e, hr2 j hj] + · rw [ite_eq_right m] + by_cases o : AbsSlotA th a + · rw [C.orig a o, hr1 _ (oldOS a o) fun j hj e => m ((memD a ha').2 ⟨j, hj, e⟩)] + · have hs : IsSlotA th' (cslotAddrA a) := ha'.isSlot P' + have ho : ¬ IsSlotA th (cslotAddrA a) := fun h => by + obtain ⟨sl, hsl, e⟩ := h.absA P + exact o (cslotAddrA_inj P' (growA _ hsl) ha' e ▸ hsl) + obtain ⟨-, -, z24⟩ := utEnd_newSlotZeroA U hn hus hc47 hal hs ho + rw [horigfree a o ha'] + unfold revA + rw [z24] + simp + · -- `par_slot` + intro a b h + change utPA slot (djA dest off) G.par win.val a = some b at h + unfold utPA at h + split_ifs at h with m + · obtain ⟨j, hj, e⟩ := m + have hj' : j < slots.length := by omega + exact ⟨slots[j]'hj', growA _ (habsD j hj').2, (habsD j hj').1.trans e.symm⟩ + · obtain ⟨sl, hsl, e⟩ := C.par_slot a b h + exact ⟨sl, growA _ hsl, e⟩ + · -- `prev` + intro a q h + change utPA slot (djA dest off) G.par win.val a = some q at h + unfold utPA at h + split_ifs at h with m + · cases h + rw [hr1 slot U.src fun j hj e => hsn j hj e.symm] + exact hrev + · have hq := (C.tree.lk.members q).1 (C.tree.tree.par_mem a q h).2 + rw [hr1 q (hsub q hq.1) fun j hj e => hq.2 (by rw [e]; exact dstN j hj)] + exact C.prev a q h + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtRtCreateA.lean b/proofs/Fl4ProofsA64/P3/UtRtCreateA.lean new file mode 100644 index 000000000..49b99c10d --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtRtCreateA.lean @@ -0,0 +1,210 @@ +/- +P3 (aarch64, A64-P3 Untyped): **the creation of a RAM retype from any creation-start state**, relation level: shared by +the decode without a reset (`P3/UtRtTopA`) and by the creation after a reset (`P3/UtRtResetCrtA`). + +* **`utCreate_postA`**: from the record at the creation start, A64 P1's `CrtP` there, the decode's context (`UtCtxA`, + read at the decode's state) and the creation's `UtEnd`, `retype` (without reset) succeeds and every outcome of the + spec body keeps `JAtZAX` at the grown typed heap, CDT ghost, sets and view. + +Premises: +* [P] `hVP` (the platform's VCPU registers), `hKt` / `hK0` (the kernel TTBR1 entries, zero below 256). +* [J] `hkw`: a new `HighVSpace` window is in the kernel window. + +New theorems only. +-/ +import Fl4ProofsA64.P3.UtRtDecSiteA +import Fl4ProofsA64.P3.UtRtArchA +import Fl4ProofsA64.P3.Stage3RestartA +import Fl4ProofsA64.P3.UtRtZeroPostA +import Fl4ProofsA64.P3.UtRtTcbPostA +import Fl4ProofsA64.P3.UtRtVcpuPostA +import Fl4ProofsA64.P3.UtRtHighPostA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +set_option maxRecDepth 100000 + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J7 : JSlotA S} {s2 : S} {a : A} {σ2 : State Unit} {m : McState} + +set_option maxHeartbeats 4000000 in +/-- **The creation from a creation-start state** (module docstring). -/ +theorem utCreate_postA (hA : P0.ArchTotal ar) + (J2 : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s2 a σ2 m) + {p sb fi fi1 : U64} {aligned nt us dest off win fiv : U64} {t : ApiObjectType} {rd : Nat} {reset : Bool} + {sd s0 : S} {σ0 : State Unit} {sl : CslotPtr} {H0 H' : Heap} {OS0 : Set U64} + (C : CrtP st H Z.Cs H.frame OS s2 (cslotAddrA sl) p sb fi1 false aligned nt us dest off win) + (X : UtCtxA st Z.th sd σ0 sl p sb fi false nt us dest off win t rd reset fiv) + (hcap : capA st sd (cslotAddrA sl) = .Untyped p sb fi false) + (hsrc : ∃ q, sl.1 = specPtr q ∧ Z.th q = some (.cnode sl.2.length)) + (hUe : untyped.retype st ar s2 a (cslotAddrA sl) false aligned nt us dest off win false ⦃ r => + ∃ lo hi : Nat, UtEnd st H0 Z.Cs OS0 s0 (cslotAddrA sl) p sb false H H' OS s2 r.2.1 lo hi aligned nt us dest off + win ⦄) + (hb0 : 0 < aligned.val) (hal8 : aligned.val % 8 = 0) + (hpf : aligned.val + win.val * 2 ^ (zA nt.val us).val ≤ 2 ^ 64) + (hal2 : aligned.val % 2 ^ (zA nt.val us).val = 0) + (hP' : PlaceA (utRtThA Z.th false nt us aligned win.val) (L.ksb s2)) + (hno : ∀ j, j < win.val → utRangedA nt.val → SpNoOvA (bjA aligned (zA nt.val us) j) (zOfA nt.val us.val)) + (hVP : ∀ a1 : A, UtVcpuPlatA ar a1) {k : U64 → U64} + (hKt : ∀ (a1 : A) (i : U64), i.val < 511 → ar.kernel_ttbr1_entry a1 0#u64 i = ok (k i)) + (hK0 : ∀ i : U64, i.val < 256 → k i = 0#u64) + (hkw : nt.val = 14 → 18446603336221196288 ≤ aligned.val) : + untyped.retype st ar s2 a (cslotAddrA sl) false aligned nt us dest off win false ⦃ res => + res.1 = .Ok () ∧ + ∀ r ∈ (utRtBodyA sl (specPtr p) (specPtr aligned) t us.val (windowSlotsA dest rd off.val win.val) false σ2).1, + JAtZAX (L := L) AR + (utRtZA Z ⟨utGA (cslotAddrA sl) (djA dest off) Z.G.chains win.val, + utPA (cslotAddrA sl) (djA dest off) Z.G.par win.val⟩ nt us aligned win.val + {t | nt.val = 1 ∧ t ∈ utBasesA aligned (zA nt.val us) win.val} + (utCNewA nt aligned (zA nt.val us) win.val) + {v | nt.val = 12 ∧ v ∈ utBasesA aligned (zA nt.val us) win.val}) + res.2.1 res.2.2 r.2 (utRtMA m {t | nt.val = 1 ∧ t ∈ utBasesA aligned (zA nt.val us) win.val}) ⦄ := by + have hpos : ∀ p k, Z.th p = some k → 0 < k.size := fun p k h => by + cases k with + | cnode r => exact Nat.two_pow_pos _ + | tcb => exact Nat.succ_pos _ + | other z => exact J2.thPos p z h + have hsub : ∀ y, IsSlotA Z.th y → y ∈ OS := J2.j5.2.1 + have hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s2 y = .Null := J2.j5.2.2 + have hsO : cslotAddrA sl ∈ OS := C.src + have hc2 : capA st s2 (cslotAddrA sl) = .Untyped p sb fi1 false := C.cap + have hTr2 : TreeA st OS Z.G.chains Z.G.par s2 := J2.j.1.cdt.tree.grow hsub fun y hy hn => hnul y hy hn + have hS2 : StateRelA Z.th (L.mem s2) σ2 := J2.j.1.st + have hC2 : CdtRelA st Z.th s2 σ2 Z.G := J2.j.1.cdt + have hrev : revA (L.mem s2) (cslotAddrA sl) = true := + revA_of_revocableA (J2.sys.sysl.sys.stz.1.2.1 _ hsO (by rw [hc2]; rfl)) + obtain ⟨-, -, -, h1, h2, -⟩ := id C.ar + have hbj : ∀ j, j < win.val → + (bjA aligned (zA nt.val us) j).val = aligned.val + j * 2 ^ (zA nt.val us).val := fun j hj => by + have h1 : (j + 1) * 2 ^ (zA nt.val us).val ≤ win.val * 2 ^ (zA nt.val us).val := Nat.mul_le_mul_right _ hj + rw [Nat.add_mul, Nat.one_mul] at h1 + have hz : 0 < 2 ^ (zA nt.val us).val := Nat.two_pow_pos _ + exact bjA_val (by omega) + have hfree : ∀ j, j < win.val → Z.th (bjA aligned (zA nt.val us) j) = none := + utThFree_ofA hpos (utEnd_thDisjA (Z := Z) J2.objAlloc hsO hc2 h1 h2) hbj + have hus : us.val < 64 := X.size.1 + have hn : nt.val < 15 := C.hn + have hc47 : nt.val = 4 → us.val ≤ 47 := fun h4 => by + have h := X.size.2.1 + rw [h4] at h + simp [zOfA] at h + omega + have hnewRel : ∀ j, j < win.val → CapRelA (newCapA nt.val (bjA aligned (zA nt.val us) j) us false) + (defaultCap t (specPtr (bjA aligned (zA nt.val us) j)) us.val false) := fun j hj => + utNewCap_relA X.ty _ us false hus hc47 (fun h5 => by have := X.size.2.2.2.2; omega) (hno j hj) + have horig : ∀ x, ¬ AbsSlotA Z.th x → AbsSlotA (utRtThA Z.th false nt us aligned win.val) x → + σ2.isOriginalCap x = false := fun x hx _ => by + cases h : σ2.isOriginalCap x with + | false => rfl + | true => exact absurd (J2.origLive x h) hx + have W : UtWinZA nt aligned (zA nt.val us) win.val := by + refine ⟨C.hz, hpf, hal2, fun h => ?_, fun h => ?_⟩ + · rw [zA_valA nt us hn hus] + unfold zOfA + rcases h with h | h <;> rw [h] <;> simp + · rw [zA_valA nt us hn hus] + unfold zOfA + rcases h with h | h <;> rw [h] <;> simp + have hr4 : nt.val = 4 → us.val ≠ 0 := fun h4 h0 => X.size.2.2.1 ⟨h4, h0⟩ + have hsc : nt.val = 5 → 7 ≤ us.val := fun h5 => by have := X.size.2.2.2.2; omega + have a2 : AR a Z.hw := J2.j.1.ar + -- the per-type words + have tcbW : WP.spec (untyped.retype st ar s2 a (cslotAddrA sl) false aligned nt us dest off win false) + (fun res => nt.val = 1 → UtTcbZW st aligned (zA nt.val us) win.val res.2.1) := by + by_cases h : nt.val = 1 + · have al4 : aligned.val % 4096 = 0 := by + have g := hal2 + rwa [h, zA_1 us] at g + have kv : (L.ksb s2).val + 788736 ≤ 2 ^ 64 := J2.sys.ent.ksw + refine WP.spec_mono (utRetype_tcbWordsA (ar := ar) hA C hTr2 h al4 hb0 J2.j.1.rest.curDom.2 + (by + have h := (J2.sq.nodes _ J2.sq.cur).align + simp only [LawfulStore.nodeBase] at h + omega) (by omega) ?_ ?_ ?_ a) (fun _ g _ => g) + · have g := (J2.sys.sysl.sys.stz.2.2 _ hsO p sb fi1 hc2).2 + rcases g with g | g <;> omega + · rintro ⟨y, hy, g1, g2⟩ + exact J2.sys.sysl.ly.kslo 8#u64 (by simp) y hy ⟨g1, g2⟩ + · intro y hy + have g := J2.ko.1 y .tcb hy (fun _ e => by cases e) + rw [show HKind.tcb.bits = 12 from rfl] at g + rcases g with g | g <;> omega + · exact WP.spec_mono (utRetype_nfiA (ar := ar) hA C a) (fun _ _ g => absurd g h) + have vcW : WP.spec (untyped.retype st ar s2 a (cslotAddrA sl) false aligned nt us dest off win false) + (fun res => nt.val = 12 → UtVcpuZW st aligned (zA nt.val us) win.val res.2.1) := by + by_cases h : nt.val = 12 + · have al4 : aligned.val % 4096 = 0 := by + have g := hal2 + have ez : (zA nt.val us).val = 12 := by + rw [zA_valA nt us hn hus, h]; rfl + rwa [ez] at g + exact WP.spec_mono (utRetype_vcpuWordsA (ar := ar) hA C hTr2 h al4 hVP a) (fun _ g _ => g) + · exact WP.spec_mono (utRetype_nfiA (ar := ar) hA C a) (fun _ _ g => absurd g h) + have hiW : WP.spec (untyped.retype st ar s2 a (cslotAddrA sl) false aligned nt us dest off win false) + (fun res => nt.val = 14 → UtHighZW st aligned (zA nt.val us) win.val res.2.1) := by + by_cases h : nt.val = 14 + · have al4 : aligned.val % 4096 = 0 := by + have g := hal2 + rw [h, zA_14 us] at g + change aligned.val % 8192 = 0 at g + omega + exact WP.spec_mono (utRetype_highWordsA (ar := ar) hA C hTr2 h al4 (hkw h) hKt hK0 a) + (fun _ g _ => g) + · exact WP.spec_mono (utRetype_nfiA (ar := ar) hA C a) (fun _ _ g => absurd g h) + -- the creation + refine WP.spec_mono (WP.spec_and (WP.spec_and (WP.spec_and (utRetype_nfiA (ar := ar) hA C a) + (utRetype_treeCrtA (ar := ar) hA C hTr2 a)) hUe) + (WP.spec_and (WP.spec_and (WP.spec_and (untyped.retype.liveRWH (ar := ar) hA C hb0 a) + (utRetype_frameA (ar := ar) hA C hTr2 a)) (utRetype_keepArA (R := fun b => AR b Z.hw) a2 (utRetype_nfiA (ar := ar) hA C a))) + (WP.spec_and tcbW (WP.spec_and vcW hiW)))) ?_ + rintro res ⟨⟨⟨⟨nfi, hnv, hnc⟩, hok, htree, hr1, hr2⟩, lo, hi, Ue⟩, ⟨⟨⟨Ec, Xd, WT, -⟩, wfr⟩, har⟩, + TW, VW, HW⟩ + refine ⟨hok, fun r hr => ?_⟩ + have rel := utSite_relsA Ue X hcap hsrc hc2 hnv hnc (ksb := L.ksb s2) hP' J2.j.1.place hsub hnul hS2 hC2 hr hfree + hal8 hc47 hnewRel hrev htree hr1 hr2 horig + have P2 : PlaceA (utRtThA Z.th false nt us aligned win.val) (L.ksb s2) := hP' + have hdst : ∀ d ∈ windowSlotsA dest rd off.val win.val, + ∃ q, d.1 = specPtr q ∧ Z.th q = some (.cnode d.2.length) := by + intro d hd + obtain ⟨j, hj, rfl⟩ := List.getElem_of_mem hd + exact (utCtx_dstA X j hj).2 + have hlen := utWindow_lengthA dest rd off.val win.val + have noC : nt.val ≠ 5 → utCNewA nt aligned (zA nt.val us) win.val = ∅ := fun h => + Set.eq_empty_of_forall_notMem fun _ g => h g.1 + have noS : ∀ k : Nat, nt.val ≠ k → + {t : U64 | nt.val = k ∧ t ∈ utBasesA aligned (zA nt.val us) win.val} = ∅ := fun k h => + Set.eq_empty_of_forall_notMem fun _ g => h g.1 + have yesS : ∀ k : Nat, nt.val = k → + {t : U64 | nt.val = k ∧ t ∈ utBasesA aligned (zA nt.val us) win.val} = + utBasesA aligned (zA nt.val us) win.val := fun k h => + Set.ext fun _ => ⟨fun g => g.2, fun g => ⟨h, g⟩⟩ + by_cases n1 : nt.val = 1 + · rw [yesS 1 n1, noS 12 (by omega), noC (by omega)] + exact utRt_tcbPostA J2 hsO hc2 W h1 h2 n1 hsub Ec wfr Xd (WT n1) (TW n1) X.ty hus hlen hsrc hdst hr rel.1 rel.2 + P2 har + rw [noS 1 n1, utRtMA_empty] + by_cases n12 : nt.val = 12 + · rw [yesS 12 n12, noC (by omega)] + exact utRt_vcpuPostA J2 hsO hc2 W h1 h2 n12 hsub Ec wfr (VW n12) X.ty hus hlen hsrc hdst hr rel.1 rel.2 P2 har + rw [noS 12 n12] + by_cases n14 : nt.val = 14 + · rw [noC (by omega)] + exact utRt_highPostA J2 hsO hc2 W h1 h2 n14 hsub Ec wfr (HW n14) X.ty hus hlen hsrc hdst hr rel.1 rel.2 P2 har + exact utRt_zeroPostA J2 hsO hc2 W h1 h2 n1 n12 n14 hr4 hsc hsub Ec wfr X.ty hus hlen hsrc hdst hr rel.1 rel.2 + P2 har + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtRtDecSiteA.lean b/proofs/Fl4ProofsA64/P3/UtRtDecSiteA.lean new file mode 100644 index 000000000..030b700ab --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtRtDecSiteA.lean @@ -0,0 +1,270 @@ +/- +P3 (aarch64, A64-P3 Untyped W5): **the post-creation relations inside `untyped::decode`, no-reset path** (lead +03:21Z). + +* `utEnd_noResetA`: A64 P1's `UtEnd` with the creation's start named — the state after the caller's restart (P1's + `untyped.retype.live0IBH` builds the same record behind a quantified start). +* `utRetype_treeCrtA`: the FL4 tree and revocable bits after the creation (`utRetype_treeXA`) from P1's `CrtP`. +* `utThFree_ofA`: no new base is in the typed heap (`ObjAllocA`, `thPos`). +* **`utDecode_noResetA`**: when the untyped has a CDT child (so the decode does not reset), `untyped::decode` refines + `fl4DecodeUntypedInvocationVirt` and at its end `StateRelA` and `CdtRelA` hold, at the new typed heap and CDT + ghost, for every outcome of the spec's `invoke_untyped` body run from the restarted spec state. + +Premises from the unified record `JStage3A` with the concrete J5: the relations, `SlotsValidA`, `hsub` / `hnul` (J5), `thPos`, +`origLive` (hence `is_original_cap` is false at the new objects' slots), `utBlk` (`size_bits ≤ 39`). Case hypotheses: +the decode-site facts of A64 P1 (`DecPre`, `SysPlace`) and of the restart (`RsSiteA`, which includes +`SymL`); the invoked slot is a CNode slot; and, per the arguments the decode accepts, `hnew` (the new typed heap is +placed; a new untyped or frame range does not wrap under `specPtr`). [P]: `A64FrameMasks`, the virtualization +agreement, fan-out 256. No new named premise. +-/ +import Fl4ProofsA64.P3.UtRtSiteA +import Fl4ProofsA64.P3.UtRetDecVcpuA +import Fl4ProofsA64.P3.Stage3J5A +import Fl4ProofsA64.P3.VsTabJA +import Fl4ProofsA64.P3.InsDisA +import Fl4ProofsA64.P1.MdbUZR +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section run +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + +/-- **`UtEnd` at the named creation start** (module docstring). -/ +theorem utEnd_noResetA (hA : P0.ArchTotal ar) {H H' : Heap} {Cs Rp Nt OS : Set U64} {s0 s2 : S} + {extra : Aeneas.Std.Array obj.Slot 3#usize} {slot p sb fi : U64} {dev : Bool} {ia ie : Nat} + (R : UtDecP st H Cs H.frame Rp Nt OS s0 extra slot p sb fi dev) (LY : A64Layout st H OS ia ie s0) + (e : sched.set_thread_state st s0 (L.curW s0) sched.TS_RESTART = ok s2) (a : A) + {aligned nt us dest off win : U64} (hU : UtArgs st s0 extra slot p sb fi dev false aligned nt us dest off win) + (hHH' : ∀ q k, H q = some k → H' q = some k) + (hEx : ∀ q k, H' q = some k → H q = some k ∨ + (dev = false ∧ ∃ j, j < win.val ∧ q = bjA aligned (zA nt.val us) j ∧ newKind nt.val us.val = some k)) + (hHn : dev = false → + ∀ j, j < win.val → ∀ k, newKind nt.val us.val = some k → H' (bjA aligned (zA nt.val us) j) = some k) : + untyped.retype st ar s2 a slot false aligned nt us dest off win dev ⦃ r => + ∃ lo hi : Nat, UtEnd st H Cs OS s0 slot p sb dev H H' OS s2 r.2.1 lo hi aligned nt us dest off win ⦄ := by + obtain ⟨Ac, hb⟩ := R.blk + obtain ⟨Q, Rq, q⟩ := hb.1 + obtain ⟨k, w⟩ := restart_frame (OS := OS) R.os q e + have hAr : UtAr H p sb fi dev aligned nt us dest off win := R.arith _ _ _ _ _ _ _ hU + obtain ⟨hdev, -, -, hA0, hAw, -⟩ := id hAr + have C : CrtP st H Cs H.frame OS s2 slot p sb fi dev aligned nt us dest off win := CrtP.of0 R e hU + have hw1 : ∀ lo hi : Nat, WritesIn st s0 s2 (fun b => CnW st H.tcbs H.omega H.frame s0 b ∨ + (lo ≤ b.val ∧ b.val < hi) ∨ PPW st s0 b) := fun _ _ => w.mono fun b hb => Or.inl (Or.inr hb) + have hRT := RTgt.of_inv hb.2.2.1.2.1 + obtain ⟨Ac0, hB0⟩ := R.bxi + have hKT : KTgt st H Cs s0 := KTgt.of_inv (XInv.dbg hB0.2.2.1) hB0.2.2.1.2.2.yt + cases dev + · have hKs := R.stz.2.2 slot R.src p sb fi R.cap + have hb0 : 0 < aligned.val := + lt_of_lt_of_le (LY.ut0 slot R.src p sb fi R.cap) (CrtA.of_args R hU).pa + refine WP.spec_mono (untyped.retype.liveRWH hA C hb0 a) fun r ⟨E, X, _, _⟩ => ?_ + exact ⟨p.val + fi.val, p.val + 2 ^ sb.val, ⟨CrtA.of_args R hU, fun _ _ h => h, + R.stz.1.2.2.1 slot R.src p sb fi R.cap, + ⟨fun c hc => (hKs.1 c hc).imp (fun g => by omega) id, hKs.2.imp (fun g => by omega) id⟩, hRT, hKT, + fun _ h => h, fun x hx _ => hx, R.src, hw1 (p.val + fi.val) (p.val + 2 ^ sb.val), fun y hy _ => k.capA hy, + C.dO, E, X, fun _ => ⟨hA0, hAw⟩, hHH', hEx, UtArgs.dcv R.ty R.ext hU, + fun j hj h4 => ⟨rfl, hHn rfl j hj _ (by rw [h4]; simp [newKind])⟩, + OSOK.off_hole R.heap R.os (R.stz.1.2.2.1 slot R.src p sb fi R.cap), fun _ _ _ h => h, q.idleT, hHn, + fun _ => ⟨hA0, hAw⟩, fun _ => C.fr, C.os, C.heap, + fun x hx _ => hx⟩⟩ + · refine WP.spec_mono (untyped.retype.liveD C a) fun r ⟨E, X⟩ => ?_ + obtain ⟨a1, a2, a3⟩ := hdev rfl + exact ⟨0, 0, ⟨CrtA.of_args R hU, fun _ _ h => h, + fun _ _ _ => Or.inr (Nat.zero_le _), ⟨fun _ _ => Or.inr (Nat.zero_le _), Or.inr (Nat.zero_le _)⟩, hRT, hKT, + fun _ h => h, fun x hx _ => hx, R.src, hw1 0 0, fun y hy _ => k.capA hy, C.dO, E, X, + fun h => absurd h (by unfold WKA; omega), hHH', hEx, UtArgs.dcv R.ty R.ext hU, + fun _ _ h4 => absurd (UtArgs.nt4 hU h4) (by simp), fun _ _ h => by omega, fun _ _ _ h => h, q.idleT, + fun h => absurd h (by simp), fun h => absurd h (by simp), fun _ => C.fr, C.os, C.heap, + fun x hx _ => hx⟩⟩ + +/-- **The tree and revocable bits after the creation, from `CrtP`** (module docstring). -/ +theorem utRetype_treeCrtA (hA : P0.ArchTotal ar) {H1 : Heap} {Cs Sg OS1 : Set U64} {s1 : S} + {slot p sb fi1 : U64} {dev : Bool} {aligned nt us dest off win : U64} + (C : CrtP st H1 Cs Sg OS1 s1 slot p sb fi1 dev aligned nt us dest off win) {G : List (List U64)} + {par : U64 → Option U64} (hT : TreeA st OS1 G par s1) (a1 : A) : + untyped.retype st ar s1 a1 slot false aligned nt us dest off win dev ⦃ r => r.1 = .Ok () ∧ + TreeA st OS1 (utGA slot (djA dest off) G win.val) (utPA slot (djA dest off) par win.val) r.2.1 ∧ + (∀ y ∈ OS1, (∀ j, j < win.val → y ≠ djA dest off j) → revA (L.mem r.2.1) y = revA (L.mem s1) y) ∧ + (∀ j, j < win.val → revA (L.mem r.2.1) (djA dest off j) = true) ⦄ := by + obtain ⟨hdev, hal, hfi, hA0, hAw, hinj, hcan, -⟩ := C.ar + obtain ⟨hSA, -, hK1⟩ := C.stz + obtain ⟨-, -, hV1, hUZ1, -⟩ := hSA + have hnA := nodeAt_aligned C.n0 (by rw [debug_val]; omega) (by rw [debug_val]) + have hnF := KsOffA.node hK1 C.core0 C.n0 (by rw [debug_val]; omega) + obtain ⟨-, -, hpf⟩ := ut_rangeA (capA_ut_b C.cap) (by + have := hT.ut.reg slot C.src (by rw [C.cap]; exact physA_ut _ _ _ _); rwa [C.cap] at this) + cases dev + · refine WP.spec_mono (utRetype_treeXA C.os hT a1 C.src C.cap nt us dest off win aligned false C.hn C.hz hfi hA0 hAw + (Zq := fun s' j => ZTc st H1 s1 aligned (zA nt.val us) win.val s' j) + (fun s' a' j hj hq => create_object.hCOZ hA C.heap C.os hV1 C.src C.cap nt us win aligned false C.hn + (fun h => by cases h) hal hA0 hAw hpf hnA C.nc (hnF slot C.src p sb fi1 C.cap) s' a' j hj hq) + (fun _ _ _ h w => ZTc.capw C.heap C.os hV1 C.src C.cap hA0 hAw hnA C.nc h w) + (fun _ w => ZTc.start C.heap C.os hV1 C.src C.cap hA0 hAw hnA C.nc hUZ1 C.hd w) + C.dO C.dn C.dc hinj hcan C.hus) fun _ h => h + · obtain ⟨n1, n12, n14⟩ := hdev rfl + refine WP.spec_mono (utRetype_treeXA C.os hT a1 C.src C.cap nt us dest off win aligned true C.hn C.hz hfi hA0 hAw + (Zq := fun _ _ => True) + (fun s' a' j hj _ => by + rw [create_object.pureA ar s' a' nt _ us true C.hn n1 n12 n14, WP.spec_ok] + exact ⟨rfl, fun _ _ => ⟨rfl, rfl, rfl, rfl⟩, trivial⟩) + (fun _ _ _ _ _ => trivial) (fun _ _ => trivial) C.dO C.dn C.dc hinj hcan C.hus) fun _ h => h + +end run + +/-- **No new base is in the typed heap** (module docstring). -/ +theorem utThFree_ofA {th : TypedHeapA} (hpos : ∀ p k, th p = some k → 0 < k.size) {aligned z : U64} {win : Nat} + (hdisj : ∀ q k, th q = some k → + q.val + k.size ≤ aligned.val ∨ aligned.val + win * 2 ^ z.val ≤ q.val) + (hbj : ∀ j, j < win → (bjA aligned z j).val = aligned.val + j * 2 ^ z.val) : + ∀ j, j < win → th (bjA aligned z j) = none := by + intro j hj + cases h : th (bjA aligned z j) with + | none => rfl + | some k => + have h1 : (j + 1) * 2 ^ z.val ≤ win * 2 ^ z.val := Nat.mul_le_mul_right _ hj + rw [Nat.add_mul, Nat.one_mul] at h1 + have hz : 0 < 2 ^ z.val := Nat.two_pow_pos _ + have := hpos _ _ h + rcases hdisj _ k h with g | g <;> rw [hbj j hj] at g <;> omega + +section site +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rq : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J7 : JSlotA S} {s : S} {a : A} {σ : State Unit} {m : McState} + +/-- **The no-reset decode, with the relations after all cap writes** (module docstring). -/ +theorem utDecode_noResetA (hA : P0.ArchTotal ar) + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rq Bf stack κ n (J5A st) J7 s a σ m) + {p sb fi : U64} {dev : Bool} (ex : Aeneas.Std.Array obj.Slot 3#usize) + (sl : CslotPtr) (hsrc : ∃ q, sl.1 = specPtr q ∧ Z.th q = some (.cnode sl.2.length)) + {Rp Nt Ud Acd : Set U64} + (D : DecPre st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ud Acd s (.Untyped p sb fi dev) ex) + (P : SysPlace st H Z.Cs H.frame Rp Nt OS s ex (cslotAddrA sl)) + (hcapF : obj.Slot.cap st (cslotAddrA sl) s = ok (.Untyped p sb fi dev)) + (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) + (hfm : A64FrameMasks st s) + (vi : U64) (hvi : ar.virtualization a = ok vi) (hvirt : vi = 0#u64 ↔ fl4A64Virt = false) + (hfan : Fl4SpecA64.retypeFanOutLimit = 256#64) + (lbl : U64) (slbl : Fl4SpecA64.Data) + (hlbl : Fl4SpecA64.genInvocationType slbl = .untypedRetype ↔ lbl = fl4_abi.label.UntypedRetype) + (length buf : U64) (args : List U64) (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) + (exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) (hex : ExtraOKA Z.th (L.mem s) ex exs) + (hchild : ∃ x, σ.cdt x = some sl) + (hnew : ∀ aligned nt us dest off win : U64, + UtArgs st s ex (cslotAddrA sl) p sb fi dev false aligned nt us dest off win → + PlaceA (utRtThA Z.th dev nt us aligned win.val) (L.ksb s) ∧ + ∀ j, j < win.val → utRangedA nt.val → SpNoOvA (bjA aligned (zA nt.val us) j) (zOfA nt.val us.val)) : + untyped.decode st ar s a lbl length (cslotAddrA sl) (.Untyped p sb fi dev) buf ex ⦃ res => + UPostA σ (fl4DecodeUntypedInvocationVirt (Z := Unit) slbl (args.map UScalar.bv) sl + (absCapA (.Untyped p sb fi dev)) (exs.map Prod.fst)) s a + (fun inv res => ∃ (base retypeBase : Word) (t : ApiObjectType) (us : Nat) (slots : List CslotPtr), + inv = .retype sl false base retypeBase t us slots dev ∧ res.1 = .Ok () ∧ + ∃ xT, σ.kheap σ.curThread = some (.tcb xT) ∧ + ∃ aligned nt usF dest off win : U64, + UtArgs st s ex (cslotAddrA sl) p sb fi dev false aligned nt usF dest off win ∧ + ∀ r ∈ (utRtBodyA sl base retypeBase t us slots dev (stUpdA σ σ.curThread xT .restart)).1, + StateRelA (utRtThA Z.th dev nt usF aligned win.val) (L.mem res.2.1) r.2 ∧ + CdtRelA st (utRtThA Z.th dev nt usF aligned win.val) res.2.1 r.2 + ⟨utGA (cslotAddrA sl) (djA dest off) Z.G.chains win.val, + utPA (cslotAddrA sl) (djA dest off) Z.G.par win.val⟩) res ⦄ := by + have Je := J.toJEntA + have hV : SlotsValidA Z.th (L.mem s) := Je.j.1.cv + have hpos : ∀ p k, Z.th p = some k → 0 < k.size := fun p k h => by + cases k with + | cnode r => exact Nat.two_pow_pos _ + | tcb => exact Nat.succ_pos _ + | other z => exact J.thPos p z h + have hsub : ∀ y, IsSlotA Z.th y → y ∈ OS := J.j5.2.1 + have hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null := J.j5.2.2 + have hR := Je.j.1.st + have hC := Je.j.1.cdt + have hP := Je.j.1.place + have hO := Je.sys.ent.os + have hTr : TreeA st OS Z.G.chains Z.G.par s := + hC.tree.grow hsub fun x hx hn => hnul x hx hn + have hAs : AbsSlotA Z.th sl := by + obtain ⟨q, e, hq⟩ := hsrc + exact ⟨q, e, Or.inl hq⟩ + have hsO : cslotAddrA sl ∈ OS := hsub _ (hAs.isSlot hP) + have R := UtDecP.of_live D Je.sys.sysl.sys P hcapF Je.sys.sysl.ln Je.sys.sysl.vr Je.sys.sysl.ly + have hcap : capA st s (cslotAddrA sl) = .Untyped p sb fi dev := R.cap + have hrv : obj.Slot.revocable st (cslotAddrA sl) s = ok true := + Je.sys.sysl.sys.stz.1.2.1 _ hsO (by rw [hcap]; rfl) + have hsb : sb.val ≤ 39 := J.utBlk _ hsO p sb fi dev hcap + refine untyped_decode_refinesA ar Z.th s a σ Z.G hR hV hP hO hTr hC hsub hfm vi hvi hvirt hfan lbl slbl hlbl + length buf args hlen hargs sl hAs hsO p sb fi dev hcap hrv hsb R.utv.2.2 ex exs hex _ ?_ + intro nt us dest off win t rd reset fiv _ X hU + have hres : reset = false := by + rw [X.resetEq] + exact decide_eq_false (fun h => h hchild) + subst hres + generalize (⟨Fl4SpecA64.alignUp (p.bv + fiv.bv) (zOfA nt.val us.val)⟩ : U64) = aligned at hU ⊢ + -- the caller's restart + obtain ⟨Rp', Nt', extra', sp', hPpl⟩ := RS.pl + obtain ⟨xT, hkT, hst, -, E1, -, hOS1, -, -⟩ := Je.restartA hPpl RS.sym RS.run RS.cur RS.vs RS.vc + rw [hst, bind_ok] + set s2 := L.wr s (stA (L.curW s)) sched.TS_RESTART with hs2 + set σ2 := stUpdA σ σ.curThread xT .restart with hσ2 + have C : CrtP st H Z.Cs H.frame OS s2 (cslotAddrA sl) p sb fi dev aligned nt us dest off win := + CrtP.of0 R hst hU + obtain ⟨-, hal8, -, hA0, hAw, -, -, H', -, hHH', hHn, hEx⟩ : + UtAr H p sb fi dev aligned nt us dest off win := R.arith _ _ _ _ _ _ _ hU + have hTr2 : TreeA st OS Z.G.chains Z.G.par s2 := treeA_of_mem hTr hOS1 + have hS2 : StateRelA Z.th (L.mem s2) σ2 := E1.j.1.st + have hC2 : CdtRelA st Z.th s2 σ2 Z.G := E1.j.1.cdt + have hks : L.ksb s2 = L.ksb s := L.ksb_wr _ _ _ + have hrev : revA (L.mem s2) (cslotAddrA sl) = true := by + have e24 := (hOS1 _ hsO).2.2.2 + have h0 := revA_of_revocableA hrv + unfold revA at h0 ⊢ + rw [e24]; exact h0 + have hc2 : capA st s2 (cslotAddrA sl) = .Untyped p sb fi dev := by rw [hOS1.capA hsO]; exact hcap + -- the new bases are free of the typed heap + have hbj : ∀ j, j < win.val → + (bjA aligned (zA nt.val us) j).val = aligned.val + j * 2 ^ (zA nt.val us).val := fun j hj => by + have h1 : (j + 1) * 2 ^ (zA nt.val us).val ≤ win.val * 2 ^ (zA nt.val us).val := Nat.mul_le_mul_right _ hj + rw [Nat.add_mul, Nat.one_mul] at h1 + have hz : 0 < 2 ^ (zA nt.val us).val := Nat.two_pow_pos _ + have := (CrtA.of_args R hU).pf + exact bjA_val (by omega) + have hfree : ∀ j, j < win.val → Z.th (bjA aligned (zA nt.val us) j) = none := + utThFree_ofA hpos (utEnd_thDisjA (Z := Z) E1.objAlloc hsO hc2 hA0 hAw) hbj + obtain ⟨hP', hno⟩ := hnew aligned nt us dest off win hU + have hus : us.val < 64 := X.size.1 + have hc47 : nt.val = 4 → us.val ≤ 47 := fun h4 => by + have h := X.size.2.1 + rw [h4] at h + simp [zOfA] at h + omega + have hnewRel : ∀ j, j < win.val → CapRelA (newCapA nt.val (bjA aligned (zA nt.val us) j) us dev) + (defaultCap t (specPtr (bjA aligned (zA nt.val us) j)) us.val dev) := fun j hj => + utNewCap_relA X.ty _ us dev hus hc47 (fun h5 => by have := X.size.2.2.2.2; omega) (hno j hj) + have horig : ∀ x, ¬ AbsSlotA Z.th x → AbsSlotA (utRtThA Z.th dev nt us aligned win.val) x → + σ2.isOriginalCap x = false := fun x hx _ => by + show σ.isOriginalCap x = false + cases h : σ.isOriginalCap x with + | false => rfl + | true => exact absurd (J.origLive x h) hx + -- the creation + refine WP.spec_mono (WP.spec_and (WP.spec_and (utRetype_nfiA (ar := ar) hA C a) + (utRetype_treeCrtA (ar := ar) hA C hTr2 a)) + (utEnd_noResetA (ar := ar) hA R Je.sys.sysl.ly hst a hU hHH' hEx hHn)) ?_ + rintro res ⟨⟨⟨nfi, hnv, hnc⟩, hok, htree, hr1, hr2⟩, lo, hi, Ue⟩ + refine ⟨_, _, _, _, _, rfl, hok, xT, hkT, aligned, nt, us, dest, off, win, hU, fun r hr => ?_⟩ + exact utSite_relsA Ue X hcap hsrc hc2 hnv hnc (ksb := L.ksb s) hP' (hks ▸ E1.j.1.place) hsub + (fun y hy hn => by rw [hOS1.capA hy]; exact hnul y hy hn) hS2 hC2 hr hfree hal8 hc47 hnewRel hrev htree hr1 hr2 + horig + +end site + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtRtDomA.lean b/proofs/Fl4ProofsA64/P3/UtRtDomA.lean new file mode 100644 index 000000000..765645268 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtRtDomA.lean @@ -0,0 +1,107 @@ +/- +P3 (aarch64, A64-P3 Stage 3, record edit #8b-B1, Untyped line): **`NdOKA` across a RAM creation**. A creation's write +set `UtCrtWA` (`OS` slot words, the window of new objects, the debug word of node 0, the TCB debug-pointer words) misses +the domain-schedule words `DomW`: the window lies in the source untyped, whose region misses the kernel-state block +(`KsOffA`), and the other three kinds are slot words, a node word and TCB words. The spec step keeps the schedule. + +No named premise. New module. +-/ +import Fl4ProofsA64.P3.UtRtFrameA +import Fl4ProofsA64.P3.Stage3SchedWrA +import Fl4ProofsA64.P3.UtRtKeptA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Sched Fl4SpecA64 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-- **`NdOKA` after a RAM creation** (module docstring). -/ +theorem utRt_domA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} + {κ : U64 → U64} {n : Nat} {J5 : JSlotA S} {Rj : Set U64 → JSlotA S} {s2 q : S} {a : A} + {σ σ' : State Unit} {m : McState} {nt aligned z : U64} {len : Nat} {slot p sb fi : U64} + (J2 : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7A st Rj) s2 a σ m) + (hx : slot ∈ OS) (hc : capA st s2 slot = .Untyped p sb fi false) (h1 : p.val ≤ aligned.val) + (h2 : aligned.val + len * 2 ^ z.val ≤ p.val + 2 ^ sb.val) + (w : WritesIn st s2 q (UtCrtWA st H OS s2 nt aligned z len)) (D : DomKeepA σ σ') : + NdOKA (st := st) q σ' := by + obtain ⟨-, -, hK1⟩ := J2.sys.sysl.sys.stz + have ks := (hK1 slot hx p sb fi hc).2 + refine J2.domOK.keepS J2.sr.toSchedSRN D w.1 w.2.1 (fun b hb hd => w.mem_eq hb (fun h => ?_)) + rcases h with ⟨y, hy, y1, y2⟩ | ⟨-, h3, h4⟩ | e | ⟨t, ht, e⟩ + · exact J2.os_domA hy y1 y2 hd + · unfold DomW at hd + omega + · have c0 : (0#u64 : U64).val = 0 := rfl + have v := nodeAt_valSc (s := s2) J2.sr.ksFit (c := 0#u64) (o := layout.node.DEBUG_TCBS) (by decide) + (by simp [layout.node.DEBUG_TCBS]) + have o : (layout.node.DEBUG_TCBS).val < 4288 := by simp [layout.node.DEBUG_TCBS] + have e' : b = L.nodeAt s2 0#u64 layout.node.DEBUG_TCBS := e + rw [e'] at hd + unfold DomW at hd + omega + · have htZ : t ∈ Z.T := by rw [← J2.tcbs]; exact ht + have hfit := (J2.sr.core.tcbs t htZ).fits + have kk := J2.sr.tcb_ks htZ + have v : (wa t 3584#u64).val = t.val + 3584 := by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by simp; omega) + unfold dpA at e + rw [e] at hd + unfold DomW at hd + unfold ksSizeA at kk + omega + +/-- (record edit B2) **The IPI pair after a RAM creation** (module docstring). -/ +theorem utRt_ipiA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} + {κ : U64 → U64} {n : Nat} {J5 : JSlotA S} {Rj : Set U64 → JSlotA S} {s2 q : S} {a : A} + {σ : State Unit} {m : McState} {nt aligned z : U64} {len : Nat} {slot p sb fi : U64} + (J2 : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 (J7A st Rj) s2 a σ m) + (hx : slot ∈ OS) (hc : capA st s2 slot = .Untyped p sb fi false) (h1 : p.val ≤ aligned.val) + (h2 : aligned.val + len * 2 ^ z.val ≤ p.val + 2 ^ sb.val) + (w : WritesIn st s2 q (UtCrtWA st H OS s2 nt aligned z len)) : + Sched.SilentScheduleIpiA (st := st) q := by + obtain ⟨hnodes, -⟩ := J2.sys.sysl.sys.stz.2.2 slot hx p sb fi hc + have hH : HeapOK H := J2.sys.ent.heap + refine J2.ipiKeepOf w (fun o ho h => ?_) + obtain ⟨hv, hcl⟩ := J2.sr.toSchedSRN.ipi_val ho + have hcpu := J2.sr.sch.cpuCs + have nc := J2.sq.nodes _ hcpu + have hnb := hnodes _ hcpu + unfold LawfulStore.nodeBase at hnb + have h0 : (0#u64 : U64) ∈ Z.Cs := by + obtain ⟨k, hk, -, h⟩ := J2.j.1.csN + exact (h 0#u64).2 (by simpa using hk) + have n0 := J2.sq.nodes 0#u64 h0 + have d88 : layout.node.DEBUG_TCBS.val < 4288 := by rw [debug_val]; omega + have hv0 : (nwA st s2).val = (L.ksb s2).val + 223576 := by + rw [nodeAt_val n0 d88, debug_val] + unfold LawfulStore.nodeBase + simp + rcases h with h | ⟨-, g1, g2⟩ | h | ⟨u, hu, h⟩ + · exact J2.ipi_capwA ho (Or.inr ⟨h, id⟩) + · omega + · have := congrArg UScalar.val h + omega + · have hfit := hH.fits u .tcb hu + simp only [HKind.bits] at hfit + have hdpv : (dpA u).val = u.val + 3584 := by + unfold dpA + rw [wa_valP, show (3584#u64 : U64).val = 3584 from rfl] + exact Nat.mod_eq_of_lt (by omega) + have ev := congrArg UScalar.val h + have d := nc.disj u hu + unfold LawfulStore.nodeBase at d + omega + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtRtEndA.lean b/proofs/Fl4ProofsA64/P3/UtRtEndA.lean new file mode 100644 index 000000000..13d840aab --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtRtEndA.lean @@ -0,0 +1,216 @@ +/- +P3 (aarch64, A64-P3 Untyped W3): **`UtRtFrA`'s old/new splits from A64 P1's `UtEnd`** (Architect/lead 01:19Z). + +The splits are those of the creation step, from the creation's start `s1` (kept heap `H1`, slots `OS1`; after the +reset if any) to its end `s'` (grown heap `H'`): `UtEnd.grow` / `ex` / `hn` for objects, `UtEnd.E` (`CrtE`) for +caps. `s0` is the decode's start. + +* `utEnd_capsA`: each slot of `OS1` is the untyped (new free index), a destination holding a new cap, or unchanged. +* `utEnd_dstNullA`, `utEnd_deadMonoA`: destinations were null at `s1`; a thread dead after was dead before. +* `utEnd_ipcHeapA`: `UtIpcHeapA` at `H'` with the new endpoint, notification and reply ghosts. +* `utEnd_scSizeA`: `scSize` at `H'` (the heap split from `UtEnd`; the spec objects' split is a hypothesis). +* `utEnd_frBaseA`: `FrBaseApartA` at `s'`. +* `utEnd_newDevFrA` (**the new device frame half of `DevFrDisjA`**): a new device frame comes from a device + untyped, and no heap object is at its base. `utEnd_devFrDisjA`: `DevFrDisjA` at `s'`. + +Not given by `UtEnd`: +* a new TCB's VCPU word (closure item E): `ut_createTcb_vcpuA` per object, carried along the creation loop; +* the spec side (`σ'`): the spec's `retype_region` outcome; +* the VSpace ghosts (`Z.Pp`, `Z.S1`, `Z.X`, `Z.Y`, `Z.T2`, `SetsInHA`, `VsRelA`, `ExtVsRelA`): wait for `vsrel-1a`. + +Hypotheses beyond P1's records: `hcx1` (`CrtP.cap`), `hfa` (`UtAr`: the objects start at or above the free index), +`UtInvA` and `ObjAllocA` at `s1`. No named premise; new theorems only. +-/ +import Fl4ProofsA64.P3.UtRtFrA +import Fl4ProofsA64.P1.MdbLiveU +import Fl4ProofsA64.P1.S1aDevFr +import Fl4ProofsA64.P3.EntryA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-- `isFrA` of a new cap depends on the type only. -/ +theorem utNewCap_frA (n : Nat) (b b' u : U64) (d : Bool) : isFrA (newCapA n b u d) = isFrA (newCapA n b' u d) := by + unfold newCapA + simp only [apply_ite isFrA] + rfl + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {H H1 H' : Heap} {Cs OS OS1 : Set U64} {s0 s1 s' : S} {slot p sb : U64} {dev : Bool} {lo hi : Nat} + {aligned nt us dest off win : U64} + (U : UtEnd st H Cs OS s0 slot p sb dev H1 H' OS1 s1 s' lo hi aligned nt us dest off win) +include U + +/-- The new objects' addresses. -/ +theorem utEnd_bjA {j : Nat} (hj : j < win.val) : + (bjA aligned (zA nt.val us) j).val = aligned.val + j * 2 ^ (zA nt.val us).val ∧ + aligned.val + j * 2 ^ (zA nt.val us).val < p.val + 2 ^ sb.val := by + have hpos : 0 < 2 ^ (zA nt.val us).val := Nat.two_pow_pos _ + have h1 : j * 2 ^ (zA nt.val us).val + 2 ^ (zA nt.val us).val ≤ win.val * 2 ^ (zA nt.val us).val := by + have := Nat.mul_le_mul_right (2 ^ (zA nt.val us).val) (Nat.succ_le_of_lt hj) + rwa [Nat.succ_mul] at this + have h2 := U.A.aw + have h3 := U.A.pf + exact ⟨bjA_val (by omega), by omega⟩ + +/-- **The caps' split** (`CrtE`). -/ +theorem utEnd_capsA {y : U64} : + (y = slot ∧ ∃ nfi, capA st s' y = .Untyped p sb nfi dev) ∨ + (∃ j, j < win.val ∧ y = djA dest off j ∧ + capA st s' y = newCapA nt.val (bjA aligned (zA nt.val us) j) us dev) ∨ + (y ≠ slot ∧ (∀ j, j < win.val → y ≠ djA dest off j) ∧ (y ∈ OS1 → capA st s' y = capA st s1 y)) := by + by_cases e : y = slot + · exact Or.inl ⟨e, e ▸ U.E.src⟩ + by_cases hd : ∃ j, j < win.val ∧ y = djA dest off j + · obtain ⟨j, hj, rfl⟩ := hd + exact Or.inr (Or.inl ⟨j, hj, rfl, U.E.new j hj⟩) + · have hn : ∀ j, j < win.val → y ≠ djA dest off j := fun j hj e' => hd ⟨j, hj, e'⟩ + exact Or.inr (Or.inr ⟨e, hn, fun hy => U.E.keep y hy e hn⟩) + +/-- The destinations were null at the creation's start. -/ +theorem utEnd_dstNullA (hnn : capA st s0 slot ≠ .Null) {j : Nat} (hj : j < win.val) : + djA dest off j ≠ slot ∧ capA st s1 (djA dest off j) = .Null := by + have n0 := U.A.dn0 j hj + have ne : djA dest off j ≠ slot := fun e => hnn (e ▸ n0) + exact ⟨ne, (U.k1 _ (U.dO j hj) ne).trans n0⟩ + +/-- A thread dead after the creation was dead at its start. -/ +theorem utEnd_deadMonoA (hnn : capA st s0 slot ≠ .Null) {fi1 : U64} + (hcx1 : capA st s1 slot = .Untyped p sb fi1 dev) {k : DKindA} {q : U64} (h : DeadA st OS1 k s' q) : + DeadA st OS1 k s1 q := by + obtain ⟨nfi, e⟩ := U.E.src + refine utRt_deadMonoA hcx1 e U.src (fun u hu ne hnull => ⟨hu, U.E.keep u hu ne fun j hj ej => ?_⟩) h + exact hnull (ej ▸ (utEnd_dstNullA U hnn hj).2) + +/-- **`UtIpcHeapA` at the grown heap**, the ghosts extended by a RAM retype's new objects. -/ +theorem utEnd_ipcHeapA {N E Rp : Set U64} (h : UtIpcHeapA H1 N E Rp) : + UtIpcHeapA H' (N ∪ {q | nt.val = 3 ∧ ∃ j < (if dev then 0 else win.val), q = bjA aligned (zA nt.val us) j}) + (E ∪ {q | nt.val = 2 ∧ ∃ j < (if dev then 0 else win.val), q = bjA aligned (zA nt.val us) j}) + (Rp ∪ {q | nt.val = 6 ∧ ∃ j < (if dev then 0 else win.val), q = bjA aligned (zA nt.val us) j}) := by + refine utRt_ipcHeap_newA h U.grow (nt := nt.val) (us := us.val) (win := if dev then 0 else win.val) + (at_ := fun j => bjA aligned (zA nt.val us) j) (fun j hj k hk => ?_) + cases hd : dev + · rw [hd] at hj; exact U.hn hd j (by simpa using hj) k hk + · rw [hd] at hj; simp at hj + +/-- A grown-heap object is an old one or a new one at a destination's base (`UtEnd.ex`). -/ +theorem utEnd_heapA {q : U64} {k : HKind} (h : H' q = some k) : + H1 q = some k ∨ (dev = false ∧ ∃ j, j < win.val ∧ q = bjA aligned (zA nt.val us) j ∧ + newKind nt.val us.val = some k) := U.ex q k h + +/-- **`scSize` at the grown heap.** `hold` / `hnew` are the spec objects' split. -/ +theorem utEnd_scSizeA {m : Fl4SpecA64.McState} {σ σ' : Fl4SpecA64.State Unit} + (h : ∀ c ∈ H1.scs, ∀ x n, σ.kheap (scNameA m c) = some (.schedContext x n) → + n + Fl4SpecA64.minSchedContextBits ≤ H1.scBits c ∧ + (NotIdleScA m (scNameA m c) → n + Fl4SpecA64.minSchedContextBits = H1.scBits c)) + (hold : ∀ c ∈ H1.scs, σ'.kheap (scNameA m c) = σ.kheap (scNameA m c)) + (hnew : dev = false → nt.val = 5 → ∀ j, j < win.val → + σ'.kheap (scNameA m (bjA aligned (zA nt.val us) j)) = + some (.schedContext Fl4SpecA64.defaultSchedContext (us.val - Fl4SpecA64.minSchedContextBits))) : + ∀ c ∈ H'.scs, ∀ x n, σ'.kheap (scNameA m c) = some (.schedContext x n) → + n + Fl4SpecA64.minSchedContextBits ≤ H'.scBits c ∧ + (NotIdleScA m (scNameA m c) → n + Fl4SpecA64.minSchedContextBits = H'.scBits c) := by + refine utRt_scSizeA h (C := H1.scs) (subset_refl _) U.grow (fun c hc => ?_) + obtain ⟨z, hz⟩ := hc + rcases U.ex c _ hz with old | ⟨hd, j, hj, e, hk⟩ + · exact Or.inl ⟨⟨z, old⟩, hold c ⟨z, old⟩⟩ + · obtain ⟨h5, rfl⟩ := newKind_sc hk + refine Or.inr ⟨us.val, hz, ?_, e ▸ hnew hd h5 j hj⟩ + exact U.A.sc h5 + +/-- **`FrBaseApartA` at the creation's end.** -/ +theorem utEnd_frBaseA {par : U64 → Option U64} (h : FrBaseApartA st OS1 s1) (I : UtInvA st OS1 par s1) {fi1 : U64} + (hcx1 : capA st s1 slot = .Untyped p sb fi1 dev) (hfa : p.val + fi1.val ≤ aligned.val) : + FrBaseApartA st OS1 s' := by + refine utRt_frBaseA h I U.src hcx1 (fun y => ∃ j, j < win.val ∧ y = djA dest off j) (fun y hy hk => ?_) + (fun y z hy hz fy kz => ?_) + · rcases utEnd_capsA U (y := y) with ⟨-, nfi, e⟩ | ⟨j, hj, ey, e⟩ | ⟨-, -, k⟩ + · rw [e] at hk + rcases hk with hk | hk + · cases hk + · have := hk.2.1; simp [isUtA] at this + · refine Or.inr ⟨⟨j, hj, ey⟩, ?_⟩ + obtain ⟨hv, hlt⟩ := utEnd_bjA U hj + rw [e, newCapA_ptr, hv] + exact ⟨by omega, hlt⟩ + · exact Or.inl ⟨y, hy, (k hy).symm⟩ + · obtain ⟨j, hj, rfl⟩ := hy + obtain ⟨l, hl, rfl⟩ := hz + rw [U.E.new j hj] at fy + rw [U.E.new l hl] at kz + have := kz.2.2 + rw [utNewCap_frA nt.val (bjA aligned (zA nt.val us) l) (bjA aligned (zA nt.val us) j), fy] at this + cases this + +/-- **The new device frame half of `DevFrDisjA`**: a new device frame comes from a device untyped (which adds no heap +object), and no old heap object starts at its base (`ObjAllocA`: that base is in the untyped's free part). -/ +theorem utEnd_newDevFrA {Z : XSetsA} (ha : ObjAllocA (st := st) Z H1 OS1 s1) {fi1 : U64} + (hcx1 : capA st s1 slot = .Untyped p sb fi1 dev) (hfa : p.val + fi1.val ≤ aligned.val) (hn : nt.val < 15) + {j : Nat} (hj : j < win.val) {q rights asid va : U64} {size : capability.FrameSize} {mapped ept : Bool} + (hc : capA st s' (djA dest off j) = .Frame q size rights true mapped ept asid va) : + dev = true ∧ H' q = none := by + rw [U.E.new j hj] at hc + have hd : dev = true := by + revert hc + cases dev + · intro hc + exfalso + generalize nt.val = n at hc hn + interval_cases n <;> simp [newCapA] at hc + · intro _; rfl + refine ⟨hd, ?_⟩ + obtain ⟨e, -⟩ := newCapA_devFr hn hc + obtain ⟨hv, hlt⟩ := utEnd_bjA U hj + cases hq : H' q with + | none => rfl + | some k => + exfalso + rcases U.ex q k hq with old | ⟨hdf, -⟩ + · have hpos : 0 < 2 ^ k.bits := Nat.two_pow_pos _ + have hqv : q.val = aligned.val + j * 2 ^ (zA nt.val us).val := by rw [e, hv] + rcases ha q (2 ^ k.bits) (Or.inr (Or.inr (Or.inr ⟨k, old, rfl⟩))) slot U.src p sb fi1 dev hcx1 with + h | h | ⟨-, h⟩ <;> omega + · rw [hd] at hdf; cases hdf + +/-- **`DevFrDisjA` at the creation's end.** `hσ`: the spec's data pages off the untyped's free part are kept. -/ +theorem utEnd_devFrDisjA {Z : XSetsA} {σ σ' : Fl4SpecA64.State Unit} (h : DevFrDisjA st H1 OS1 s1 σ) + {par : U64 → Option U64} (I : UtInvA st OS1 par s1) (ha : ObjAllocA (st := st) Z H1 OS1 s1) {fi1 : U64} + (hcx1 : capA st s1 slot = .Untyped p sb fi1 dev) (hfa : p.val + fi1.val ≤ aligned.val) (hn : nt.val < 15) + (hσ : ∀ q : U64, ¬ (p.val + fi1.val ≤ q.val ∧ q.val < p.val + 2 ^ sb.val) → + ∀ d pg, σ.kheap (specPtr q) = some (.archObj (.dataPage d pg)) → + σ'.kheap (specPtr q) = some (.archObj (.dataPage d pg))) : + DevFrDisjA st H' OS1 s' σ' := by + refine utRt_devFrDisjA h I U.src hcx1 (fun y hy q size rights mapped ept asid va hc => ?_) + (fun q hq => ⟨?_, hσ q hq⟩) + · rcases utEnd_capsA U (y := y) with ⟨-, nfi, e⟩ | ⟨j, hj, ey, -⟩ | ⟨-, -, k⟩ + · rw [e] at hc; cases hc + · refine Or.inr (fun kd hk => ?_) + rw [ey] at hc + rw [(utEnd_newDevFrA U ha hcx1 hfa hn hj hc).2] at hk + cases hk + · exact Or.inl ⟨y, hy, (k hy).symm⟩ + · cases h' : H' q with + | none => + cases h1 : H1 q with + | none => rfl + | some k => rw [U.grow q k h1] at h'; cases h' + | some k => + rcases U.ex q k h' with old | ⟨-, j, hj, e, -⟩ + · exact old.symm + · exfalso + obtain ⟨hv, hlt⟩ := utEnd_bjA U hj + apply hq + rw [e, hv] + exact ⟨by omega, hlt⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtRtFrA.lean b/proofs/Fl4ProofsA64/P3/UtRtFrA.lean new file mode 100644 index 000000000..5bb2f7bf7 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtRtFrA.lean @@ -0,0 +1,247 @@ +/- +P3 (aarch64, A64-P3 Untyped W3): **record fields Retype keeps, outside VSpace** (Architect 01:13Z): `scSize`, +`utIpc` (`UtIpcHeapA`), `FrBaseApartA`, `DevFrDisjA`, closure item D (`thPos`) and the Retype side of item E +(`DeadExtA.tvcpu`). + +Each lemma takes the old fact at the pre-state (heap `H`, slots `OS`) and a split of the post-state's objects or caps +into old ones, kept, and new ones. The splits are what A64 P1's `UtEnd` (`grow`, `ex`, `hn`) and the cap writes give; +they are hypotheses here, discharged at composition. + +* `utRt_scSizeA`, `utRt_scSize_newA`: old SCs keep their spec object and heap size; a new one is `ut_scSizeA`. +* `utRt_ipcHeapA`, `utRt_ipcHeap_newA`: old IPC objects are kept; the new ones are in the grown heap at their kind. +* `utRt_frBaseA`: new frame and range-key caps lie in the untyped's free part, where no old one does + (`UtInvA.noov`); one Retype makes one type, so no new frame shares its base with a new range key. +* `utRt_devFrDisjA`: old frames lie off the free part; new device frames have no heap object or a data page. +* `utNewKind_other_posA`, `utRt_thPosA`: every new `other` object is nonempty. +* `utRt_deadMonoA`, `utRt_tvcpuA`, `ut_createTcb_vcpuA`: a thread dead after Retype was dead before, and a new + TCB's VCPU word is the zero it was created from. + +No named premise. New theorems only. +-/ +import Fl4ProofsA64.P3.UtClosA +import Fl4ProofsA64.P3.UtTcbScA +import Fl4ProofsA64.P3.UtTcbCreateA +import Fl4ProofsA64.P3.CnodeFinalA +import Fl4ProofsA64.P3.DevFrDisjA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## `scSize` -/ + +/-- **`JStage3A.scSize` after Retype**: old SCs keep their spec object, new ones have `default_sched_context`'s size +in a heap object of the user size. -/ +theorem utRt_scSizeA {C C' : Set U64} {m : Fl4SpecA64.McState} {σ σ' : Fl4SpecA64.State Unit} {H H' : Heap} + (h : ∀ c ∈ C, ∀ x n, σ.kheap (scNameA m c) = some (.schedContext x n) → + n + Fl4SpecA64.minSchedContextBits ≤ H.scBits c ∧ + (NotIdleScA m (scNameA m c) → n + Fl4SpecA64.minSchedContextBits = H.scBits c)) + (hC : C ⊆ H.scs) (hg : ∀ c k, H c = some k → H' c = some k) + (hsplit : ∀ c ∈ C', (c ∈ C ∧ σ'.kheap (scNameA m c) = σ.kheap (scNameA m c)) ∨ + ∃ us, H' c = some (.sc us) ∧ Fl4SpecA64.minSchedContextBits ≤ us ∧ + σ'.kheap (scNameA m c) = + some (.schedContext Fl4SpecA64.defaultSchedContext (us - Fl4SpecA64.minSchedContextBits))) : + ∀ c ∈ C', ∀ x n, σ'.kheap (scNameA m c) = some (.schedContext x n) → + n + Fl4SpecA64.minSchedContextBits ≤ H'.scBits c ∧ + (NotIdleScA m (scNameA m c) → n + Fl4SpecA64.minSchedContextBits = H'.scBits c) := by + intro c hc + rcases hsplit c hc with ⟨hold, hk⟩ | ⟨us, hH, hus, hk⟩ + · obtain ⟨z, hz⟩ := hC hold + have e : H'.scBits c = H.scBits c := by unfold Heap.scBits; rw [hz, hg c _ hz] + intro x n hx + rw [hk] at hx + rw [e]; exact h c hold x n hx + · intro x n hx + have e := ut_scSizeA hH hus hk x n hx + exact ⟨e.le, fun _ => e⟩ + +/-- A new SC of a RAM Retype is in the grown heap at the user size (`UtEnd.hn`'s shape). -/ +theorem utRt_scSize_newA {H' : Heap} {nt us : Nat} {win : Nat} {at_ : Nat → U64} + (hn : ∀ j, j < win → ∀ k, newKind nt us = some k → H' (at_ j) = some k) (h5 : nt = 5) {j : Nat} (hj : j < win) : + H' (at_ j) = some (.sc us) := hn j hj _ (by subst h5; rfl) + +/-! ## `UtIpcHeapA` -/ + +theorem utRt_ipcHeapA {H H' : Heap} {N E Rp N' E' Rp' : Set U64} (h : UtIpcHeapA H N E Rp) + (hg : ∀ c k, H c = some k → H' c = some k) + (hN : ∀ n ∈ N', n ∈ N ∨ H' n = some .ntfn) (hE : ∀ e ∈ E', e ∈ E ∨ H' e = some .ep) + (hR : ∀ r ∈ Rp', r ∈ Rp ∨ H' r = some .reply) : UtIpcHeapA H' N' E' Rp' := + ⟨fun n hn => (hN n hn).elim (fun o => hg n _ (h.ntfn n o)) id, + fun e he => (hE e he).elim (fun o => hg e _ (h.ep e o)) id, + fun r hr => (hR r hr).elim (fun o => hg r _ (h.reply r o)) id⟩ + +/-- The ghosts after a RAM Retype of type `nt`: the old ones and, at the matching type, the new objects. -/ +theorem utRt_ipcHeap_newA {H H' : Heap} {N E Rp : Set U64} (h : UtIpcHeapA H N E Rp) + (hg : ∀ c k, H c = some k → H' c = some k) {nt us win : Nat} {at_ : Nat → U64} + (hn : ∀ j, j < win → ∀ k, newKind nt us = some k → H' (at_ j) = some k) : + UtIpcHeapA H' (N ∪ {q | nt = 3 ∧ ∃ j < win, q = at_ j}) (E ∪ {q | nt = 2 ∧ ∃ j < win, q = at_ j}) + (Rp ∪ {q | nt = 6 ∧ ∃ j < win, q = at_ j}) := by + refine utRt_ipcHeapA h hg (fun q hq => ?_) (fun q hq => ?_) (fun q hq => ?_) + all_goals + rcases hq with hq | ⟨rfl, j, hj, rfl⟩ + · exact Or.inl hq + · exact Or.inr (hn j hj _ rfl) + +/-! ## `FrBaseApartA`, `DevFrDisjA` -/ + +section caps +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +theorem utFr_physA {c : capability.Cap} (h : isFrA c = true) : physA c = true ∧ isUtA c = false := by + cases c <;> try (simp [isFrA] at h; done) + rename_i q sz r dev m ept a v + have e := physA_eq (.Frame q sz r dev m ept a v) + simp only [objects.cap_is_physical] at e + exact ⟨(okinj e).symm, rfl⟩ + +/-- **An old non-untyped physical cap's base is not in an untyped's free part** (`UtInvA.noov`, `reg`). -/ +theorem utRt_baseOffA {OS : Set U64} {par : U64 → Option U64} {s : S} (U : UtInvA st OS par s) {x p b i : U64} + {d : Bool} (hx : x ∈ OS) (hcx : capA st s x = .Untyped p b i d) {y : U64} (hy : y ∈ OS) + (hp : physA (capA st s y) = true) (hu : isUtA (capA st s y) = false) : + ¬ (p.val + i.val ≤ (ptrA (capA st s y)).val ∧ (ptrA (capA st s y)).val < p.val + 2 ^ b.val) := by + rintro ⟨h1, h2⟩ + rcases U.noov x hx p b i d hcx y hy hp with (h | h) | ⟨h, -⟩ + · have := U.reg y hy hp + unfold baseA at this + omega + · unfold baseA at h; omega + · rw [hu] at h; cases h + +/-- **`FrBaseApartA` after Retype.** -/ +theorem utRt_frBaseA {OS OS' : Set U64} {s s' : S} (h : FrBaseApartA st OS s) {par : U64 → Option U64} + (U : UtInvA st OS par s) {x p b i : U64} {d : Bool} (hx : x ∈ OS) (hcx : capA st s x = .Untyped p b i d) + (New : U64 → Prop) + (hsplit : ∀ y ∈ OS', isFrA (capA st s' y) = true ∨ RngKeyA (capA st s' y) → + (∃ y0 ∈ OS, capA st s y0 = capA st s' y) ∨ + (New y ∧ p.val + i.val ≤ (ptrA (capA st s' y)).val ∧ (ptrA (capA st s' y)).val < p.val + 2 ^ b.val)) + (huni : ∀ y z, New y → New z → isFrA (capA st s' y) = true → ¬ RngKeyA (capA st s' z)) : + FrBaseApartA st OS' s' := by + intro y hy z hz fy kz e + rcases hsplit y hy (Or.inl fy) with ⟨y0, hy0, ey⟩ | ⟨ny, y1, y2⟩ <;> + rcases hsplit z hz (Or.inr kz) with ⟨z0, hz0, ez⟩ | ⟨nz, z1, z2⟩ + · exact h y0 hy0 z0 hz0 (ey ▸ fy) (ez ▸ kz) (by rw [ey, ez]; exact e) + · have f0 : isFrA (capA st s y0) = true := ey ▸ fy + have := utRt_baseOffA U hx hcx hy0 (utFr_physA f0).1 (utFr_physA f0).2 + rw [ey, ← e] at this + exact this ⟨z1, z2⟩ + · have k0 : RngKeyA (capA st s z0) := ez ▸ kz + have := utRt_baseOffA U hx hcx hz0 k0.1 k0.2.1 + rw [ez, e] at this + exact this ⟨y1, y2⟩ + · exact huni y z ny nz fy kz + +/-- **`DevFrDisjA` after Retype.** -/ +theorem utRt_devFrDisjA {H H' : Heap} {OS OS' : Set U64} {s s' : S} {σ σ' : Fl4SpecA64.State Unit} + (h : DevFrDisjA st H OS s σ) {par : U64 → Option U64} (U : UtInvA st OS par s) {x p b i : U64} {d : Bool} + (hx : x ∈ OS) (hcx : capA st s x = .Untyped p b i d) + (hsplit : ∀ y ∈ OS', ∀ q size rights mapped ept asid va, + capA st s' y = .Frame q size rights true mapped ept asid va → + (∃ y0 ∈ OS, capA st s y0 = capA st s' y) ∨ + ∀ k, H' q = some k → (∃ bits, k = .other bits) ∧ + ∃ dev pg, σ'.kheap (specPtr q) = some (.archObj (.dataPage dev pg))) + (hkeep : ∀ q : U64, ¬ (p.val + i.val ≤ q.val ∧ q.val < p.val + 2 ^ b.val) → + H' q = H q ∧ ∀ dev pg, σ.kheap (specPtr q) = some (.archObj (.dataPage dev pg)) → + σ'.kheap (specPtr q) = some (.archObj (.dataPage dev pg))) : + DevFrDisjA st H' OS' s' σ' := by + intro y hy q size rights mapped ept asid va hc k hk + rcases hsplit y hy q size rights mapped ept asid va hc with ⟨y0, hy0, e⟩ | hnew + · have hc0 : capA st s y0 = .Frame q size rights true mapped ept asid va := e.trans hc + have f0 : isFrA (capA st s y0) = true := by rw [hc0]; rfl + have off := utRt_baseOffA U hx hcx hy0 (utFr_physA f0).1 (utFr_physA f0).2 + have eq : ptrA (capA st s y0) = q := by + have := ptrA_eq (capA st s y0) + rw [hc0] at this ⊢ + simpa [capability.Cap.ptr] using this.symm + rw [eq] at off + obtain ⟨eH, ks⟩ := hkeep q off + obtain ⟨hb, dev, pg, hp⟩ := h y0 hy0 q size rights mapped ept asid va hc0 k (eH ▸ hk) + exact ⟨hb, dev, pg, ks dev pg hp⟩ + · exact hnew k hk + +end caps + +/-! ## Closure item D -/ + +/-- Every `other` object a Retype adds is nonempty. -/ +theorem utNewKind_other_posA {n us z : Nat} (h : newKind n us = some (.other z)) : 0 < z := by + unfold newKind at h + split_ifs at h <;> simp at h <;> omega + +theorem utRt_thPosA {th th' : TypedHeapA} (h : ∀ p z, th p = some (.other z) → 0 < z) + (hsplit : ∀ p z, th' p = some (.other z) → th p = some (.other z) ∨ ∃ n us, newKind n us = some (.other z)) : + ∀ p z, th' p = some (.other z) → 0 < z := fun p z hz => by + rcases hsplit p z hz with k | ⟨n, us, k⟩ + · exact h p z k + · exact utNewKind_other_posA k + +/-! ## Closure item E, Retype side -/ + +section dead +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-- Retype keeps every non-null cap's object named: a slot of `OS` stays in `OS'` with the same cap, or is the +untyped itself (whose base is unchanged). So an object dead after was dead before. -/ +theorem utRt_deadMonoA {OS OS' : Set U64} {s s' : S} {x p b i i' : U64} {d : Bool} + (hcx : capA st s x = .Untyped p b i d) (hcx' : capA st s' x = .Untyped p b i' d) + (hx : x ∈ OS') + (hk : ∀ u ∈ OS, u ≠ x → capA st s u ≠ .Null → u ∈ OS' ∧ capA st s' u = capA st s u) {k : DKindA} {q : U64} + (h : DeadA st OS' k s' q) : DeadA st OS k s q := by + intro u hu hp + by_cases e : u = x + · rw [e, hcx] at hp; exact not_namesD_untypedA _ _ _ _ _ _ hp + · have hnn : capA st s u ≠ .Null := fun hn => by + rw [hn] at hp; exact not_namesD_nullA _ _ hp + obtain ⟨hu', ec⟩ := hk u hu e hnn + exact h u hu' (ec ▸ hp) + +theorem utRt_tvcpuA {T T' OS OS' : Set U64} {s s' : S} + (h : ∀ t ∈ T, DeadA st OS .tcb s t → L.mem s (wa t 3664#u64) = 0#u64) + (mono : ∀ t ∈ T, DeadA st OS' .tcb s' t → DeadA st OS .tcb s t) + (hsplit : ∀ t ∈ T', (t ∈ T ∧ L.mem s' (wa t 3664#u64) = L.mem s (wa t 3664#u64)) ∨ + L.mem s' (wa t 3664#u64) = 0#u64) : + ∀ t ∈ T', DeadA st OS' .tcb s' t → L.mem s' (wa t 3664#u64) = 0#u64 := fun t ht hd => by + rcases hsplit t ht with ⟨old, e⟩ | z + · rw [e]; exact h t old (mono t old hd) + · exact z + +/-- **A new TCB has no VCPU**: `create_object` does not write the VCPU word (`ut_createTcbA`'s write set). -/ +theorem ut_createTcb_vcpuA {s s' : S} {t nb hd : U64} (ht : TcbAddr t) + (hw : WritesIn st s s' (fun b => utTcbInitA t b ∨ b = wa nb 88#u64 ∨ (hd ≠ 0#u64 ∧ b = wa hd 3584#u64))) + (hN : utOffTcbA t (wa nb 88#u64)) (hH : hd ≠ 0#u64 → utOffTcbA t (wa hd 3584#u64)) + (h0 : L.mem s (wa t 3664#u64) = 0#u64) : L.mem s' (wa t 3664#u64) = 0#u64 := by + have hv : (wa t 3664#u64).val = t.val + 3664 := ut_tcbWordA ht (by decide) + have hal : Aligned (wa t 3664#u64) := by + have := ht.align + show _ % 8 = 0 + rw [hv]; omega + have inT : ¬ utOffTcbA t (wa t 3664#u64) := fun h => by unfold utOffTcbA at h; omega + rw [hw.mem_eq hal ?_] + · exact h0 + rintro (hi | e | ⟨hz, e⟩) + · have ne : ∀ c : U64, c.val < 4096 → c.val ≠ 3664 → wa t 3664#u64 ≠ wa t c := fun c hc hne e => by + have := congrArg UScalar.val e + rw [hv, ut_tcbWordA ht hc] at this + omega + rcases hi with ⟨j, hj, e⟩ | e | ⟨w, hw8, e⟩ | e | e + · have := congrArg UScalar.val e + rw [hv, regA_val ht hj] at this + omega + · exact ne _ (by simp) (by simp) e + · have hk : (⟨BitVec.ofNat 64 (3600 + 8 * w.val)⟩ : U64).val = 3600 + 8 * w.val := by + show (BitVec.ofNat 64 (3600 + 8 * w.val)).toNat = _ + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)] + exact ne _ (by rw [hk]; omega) (by rw [hk]; omega) e + · exact ne _ (by simp) (by simp) e + · exact ne _ (by simp) (by simp) e + · exact inT (by rw [e]; exact hN) + · exact inT (by rw [e]; exact hH hz) + +end dead + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtRtFrameA.lean b/proofs/Fl4ProofsA64/P3/UtRtFrameA.lean new file mode 100644 index 000000000..39e2efc20 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtRtFrameA.lean @@ -0,0 +1,132 @@ +/- +P3 (aarch64, A64-P3 Untyped W5): **a sharp write frame of FL4's creation**. + +A64 P1's frame of a creation (`RFw`) allows every cap-slot-eligible word, which covers old page tables, pools and IPC +buffers. The creation writes less: slot words of `OS` (the untyped's free index and `insert_new_cap`'s links), the new +window (TCB, VCPU and HighVSpace objects), the debug-list head and an old TCB's debug link. + +* `utInsert_inOSA`: `insert_new_cap` writes slot words of `OS` only (P1's `capwG` at the slot words). +* **`utRetype_frameA`**: the frame of a non-reset RAM `untyped::retype`. + +Hypotheses: A64 P1's creation start `CrtP` and the tree. No named premise. New theorems only. +-/ +import Fl4ProofsA64.P3.UtRtDecSiteA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Sched Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {T Ω Sg OS : Set U64} + +/-- The slot set read as a typed heap whose only words are the slots'. -/ +theorem utOSOK_inOSA (hO : OSOK T Ω Sg OS) : OSOK ∅ {b | InOS OS b} ∅ OS := by + refine ⟨fun x hx => ⟨(hO.slot x hx).1, fun o ho _ => Or.inr ⟨?_, id⟩⟩, hO.fit, hO.disj, hO.nz⟩ + have hv : (wa x o).val = x.val + o.val := wv (hO.fit x hx) ho + exact ⟨x, hx, by omega, by omega⟩ + +/-- **`insert_new_cap` writes slot words of `OS` only.** -/ +theorem utInsert_inOSA (hO : OSOK T Ω Sg OS) {s : S} {src dest : U64} {nc : capability.Cap} + (hsrc : src ∈ OS) (hd : dest ∈ OS) (hnN : NxA st s src ≠ 0#u64 → NxA st s src ∈ OS) : + cdt.insert_new_cap st s src dest nc ⦃ s' => WritesIn st s s' (InOS OS) ⦄ := by + refine WP.spec_mono (cdt.insert_new_cap.capwG (utOSOK_inOSA hO) hsrc hd hnN) fun _ w => w.mono fun b hb => ?_ + rcases hb with ⟨t, ht, -⟩ | ⟨h, -⟩ + · exact ht.elim + · exact h + +/-- The words a RAM creation may write. -/ +def UtCrtWA (st : machine.Store S) [L : LawfulStore st] (H : Heap) (OS : Set U64) (s0 : S) (nt base z : U64) + (len : Nat) (b : U64) : Prop := + InOS OS b ∨ (WKA nt ∧ base.val ≤ b.val ∧ b.val < base.val + len * 2 ^ z.val) ∨ b = nwA st s0 ∨ + ∃ t, H t = some .tcb ∧ b = dpA t + +set_option maxHeartbeats 2000000 in +/-- **The write frame of a non-reset RAM creation** (module docstring). -/ +theorem utRetype_frameA (hA : P0.ArchTotal ar) {H1 : Heap} {Cs Sg OS1 : Set U64} {s1 : S} + {slot p sb fi1 : U64} {aligned nt us dest off win : U64} + (C : CrtP st H1 Cs Sg OS1 s1 slot p sb fi1 false aligned nt us dest off win) {G : List (List U64)} + {par : U64 → Option U64} (hT : TreeA st OS1 G par s1) (a1 : A) : + untyped.retype st ar s1 a1 slot false aligned nt us dest off win false ⦃ r => + WritesIn st s1 r.2.1 (UtCrtWA st H1 OS1 s1 nt aligned (zA nt.val us) win.val) ⦄ := by + obtain ⟨-, hal, hfi, hA0, hAw, hinj, hcan, -⟩ := C.ar + obtain ⟨hSA, -, hK1⟩ := C.stz + obtain ⟨-, -, hV1, hUZ1, -⟩ := hSA + have hnA := nodeAt_aligned C.n0 (by rw [debug_val]; omega) (by rw [debug_val]) + have hnF := KsOffA.node hK1 C.core0 C.n0 (by rw [debug_val]; omega) + have hM := hT.lk + obtain ⟨-, -, hpf⟩ := ut_rangeA (capA_ut_b C.cap) (by + have := hT.ut.reg slot C.src (by rw [C.cap]; exact physA_ut _ _ _ _); rwa [C.cap] at this) + have hsb : 2 ^ sb.val < 2 ^ 64 := Nat.pow_lt_pow_right (by decide) (capA_ut_b C.cap) + have hs0 : capA st s1 slot ≠ .Null := by rw [C.cap]; exact fun h => by cases h + have hsn : ∀ j, j < win.val → slot ≠ djA dest off j := fun j hj e => hs0 (by rw [e]; exact C.dn j hj) + set z := zA nt.val us with hzdef + have hfit : aligned.val + win.val * 2 ^ z.val ≤ 2 ^ 64 := by omega + have hwf : ∀ j, j < win.val → CapRt.EncWf (newCapA nt.val (bjA aligned z j) us false) := fun j hj => + newCapA_encWf _ _ _ _ (hcan j hj) C.hus + have hdn1 : ∀ s', RtSrc st s1 slot p sb false aligned.val (aligned.val + win.val * 2 ^ z.val) s' → + ∀ j, j < win.val → capA st s' (djA dest off j) = .Null := fun s' h1 j hj => by + obtain ⟨-, -, -, hc1, -, -⟩ := h1.facts C.os C.src + rw [hc1 _ (C.dO j hj) (fun e => hsn j hj e.symm)]; exact C.dn j hj + refine WP.spec_mono (untyped.retype.genJK ar a1 C.cap nt us dest off win aligned false C.hn C.hz hfit + (J := fun s' s'' i => UtLoopA st OS1 G par s' slot (djA dest off) + (fun j => newCapA nt.val (bjA aligned z j) us false) s'' i ∧ + ZTc st H1 s1 aligned z win.val s'' i ∧ WritesIn st s1 s'' (UtCrtWA st H1 OS1 s1 nt aligned z win.val)) + (K := fun s' s'' i => UtLoopA st OS1 G par s' slot (djA dest off) + (fun j => newCapA nt.val (bjA aligned z j) us false) s'' i ∧ + ZTc st H1 s1 aligned z win.val s'' (i + 1) ∧ WritesIn st s1 s'' (UtCrtWA st H1 OS1 s1 nt aligned z win.val)) + (fun _ _ s' a' j hj h => ?_) (fun s' h1 s'' i hi h => ?_) (fun s' h1 => ?_)) ?_ + · -- one object + refine WP.spec_mono (WP.spec_and + (create_object.hCOZ hA C.heap C.os hV1 C.src C.cap nt us win aligned false C.hn (fun h => by cases h) hal hA0 + hAw hpf hnA C.nc (hnF slot C.src p sb fi1 C.cap) s' a' j hj h.2.1) + (create_object.frZ hA C.heap (s0 := s1) (p := p) (sb := sb) (fi := fi1) nt us win aligned false C.hn hal hA0 + hAw hpf hnA (hnF slot C.src p sb fi1 C.cap) s' a' j hj h.2.1)) ?_ + rintro r ⟨⟨hc, hk, hz⟩, hw⟩ + refine ⟨hc, h.1.keep C.os (fun l hl => C.dO l (by omega)) hk, hz, h.2.2.trans1 (hw.mono fun b hb => ?_)⟩ + rcases hb with ⟨w, g1, g2⟩ | g | g + · have h1 := Nat.mul_le_mul_right (2 ^ z.val) (show j + 1 ≤ win.val from hj) + refine Or.inr (Or.inl ⟨w, g1, lt_of_lt_of_le g2 (Nat.add_le_add_left ?_ _)⟩) + exact h1 + · exact Or.inr (Or.inr (Or.inl g)) + · exact Or.inr (Or.inr (Or.inr g)) + · -- one insert + have hnx : NxA st s'' slot ≠ 0#u64 → NxA st s'' slot ∈ OS1 := (MdbLk.nxG C.os h.1.1 h.1.2.2.1).2 + refine WP.spec_mono (WP.spec_and + (UtLoopA.step C.os hi h.1 C.dO (hdn1 s' h1) C.dc hinj hsn hwf (fun j _ => newCapA_ne _ _ _ _)) + (WP.spec_and + (cdt.insert_new_cap.zqW (Zq := fun s j => ZTc st H1 s1 aligned z win.val s j) C.os + (fun _ _ _ h w => ZTc.capw C.heap C.os hV1 C.src C.cap hA0 hAw hnA C.nc h w) + (C.dO i hi) (MdbLk.nxG C.os h.1.1 h.1.2.2.1) h.2.1) + (utInsert_inOSA C.os C.src (C.dO i hi) hnx))) ?_ + rintro r ⟨hl, hz, hw⟩ + exact ⟨hl, hz, h.2.2.trans1 (hw.mono fun _ g => Or.inl g)⟩ + · -- the source rewritten + obtain ⟨-, -, -, -, -, hW⟩ := h1.facts C.os C.src + have L0 := h1.lk C.os C.src hM hs0 + have hsG : slot ∈ G.flatten := (hM.members slot).2 ⟨C.src, hs0⟩ + refine ⟨⟨L0, by rw [utPA_zero]; exact hT.tree, hsG, fun _ _ _ => rfl, + fun j hj => absurd hj (Nat.not_lt_zero j), fun y x => ?_, fun _ _ _ => rfl, + fun j hj => absurd hj (Nat.not_lt_zero j)⟩, + ZTc.start C.heap C.os hV1 C.src C.cap hA0 hAw hnA C.nc hUZ1 C.hd hW, hW.mono fun b hb => ?_⟩ + · rw [utPA_zero] + exact ⟨Or.inl, fun h => h.elim id fun ⟨j, hj, _⟩ => absurd hj (Nat.not_lt_zero j)⟩ + have f := C.os.fit slot C.src + refine Or.inl ⟨slot, C.src, ?_, ?_⟩ <;> rcases hb with e | e + · rw [e] + · rw [e, wv f (by simp)]; simp + · rw [e]; omega + · rw [e, wv f (by simp)]; simp + rintro r ⟨-, s', -, -, -, hw⟩ + exact hw + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtRtFrontA.lean b/proofs/Fl4ProofsA64/P3/UtRtFrontA.lean new file mode 100644 index 000000000..ecdb87690 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtRtFrontA.lean @@ -0,0 +1,98 @@ +/- +P3 (aarch64, A64-P3 Untyped W5, spec side): **`reset_untyped_cap` in front of `invoke_untyped`** (lead 01:47Z, +item 3). + +FL4's `invoke_untyped` is the reset (when the invocation resets) then the body `utRtBodyA` +(`utInvoke_eqA`); `fl4InvokeUntypedA64` appends `vcpu::init` for a VCPU retype. + +* Outcomes, as memberships (what a refinement post asks): `utInvoke_noReset_memA`, `utInvoke_reset_memA` (the reset + returned `Ok`, then the body), `utInvoke_preempted_memA` (the reset was preempted: nothing is created), and + `utInvokeA64_memA` for a type other than the VCPU. +* `utInvoke_reset_invA`: every outcome of a resetting invocation is one of those two shapes. + +The reset's own outcomes are `UtResetLoopA`'s. That the body has an outcome is a premise here. No named premise. +-/ +import Fl4ProofsA64.P3.UtRtInvA +import Fl4ProofsA64.P3.UtResetLoopA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 + +noncomputable section + +open Fl4SpecA64 Fl4Spec + +section +variable {srcSlot : CslotPtr} {base retypeBase : Word} {t : ApiObjectType} {objSz : Nat} {slots : List CslotPtr} + {dev : Bool} + +theorem utInvoke_noReset_memA {σ σ' : State Unit} + (h : ((), σ') ∈ (utRtBodyA srcSlot base retypeBase t objSz slots dev σ).1) : + (.ok (), σ') ∈ ((fl4InvokeUntyped (Z := Unit) + (.retype srcSlot false base retypeBase t objSz slots dev)).run σ).1 := by + rw [utInvoke_eqA] + refine mem_bindE_okD (a := ()) (s' := σ) ?_ (mem_liftED h) + exact mem_of_runD (run_returnOkD _ σ) + +theorem utInvoke_reset_memA {σ σr σ' : State Unit} + (hr : (.ok (), σr) ∈ ((resetUntypedCap (Z := Unit) srcSlot).run σ).1) + (h : ((), σ') ∈ (utRtBodyA srcSlot base retypeBase t objSz slots dev σr).1) : + (.ok (), σ') ∈ ((fl4InvokeUntyped (Z := Unit) + (.retype srcSlot true base retypeBase t objSz slots dev)).run σ).1 := by + rw [utInvoke_eqA] + exact mem_bindE_okD (a := ()) (s' := σr) hr (mem_liftED h) + +theorem utInvoke_preempted_memA {σ σr : State Unit} + (hr : (.error (), σr) ∈ ((resetUntypedCap (Z := Unit) srcSlot).run σ).1) : + (.error (), σr) ∈ ((fl4InvokeUntyped (Z := Unit) + (.retype srcSlot true base retypeBase t objSz slots dev)).run σ).1 := by + rw [utInvoke_eqA] + exact mem_bindE_errD hr + +/-- Every outcome of a resetting invocation. -/ +theorem utInvoke_reset_invA {σ : State Unit} {y : Except Unit Unit × State Unit} + (hy : y ∈ ((fl4InvokeUntyped (Z := Unit) + (.retype srcSlot true base retypeBase t objSz slots dev)).run σ).1) : + (∃ σr, (.error (), σr) ∈ ((resetUntypedCap (Z := Unit) srcSlot).run σ).1 ∧ y = (.error (), σr)) ∨ + ∃ σr σ', (.ok (), σr) ∈ ((resetUntypedCap (Z := Unit) srcSlot).run σ).1 ∧ + ((), σ') ∈ (utRtBodyA srcSlot base retypeBase t objSz slots dev σr).1 ∧ y = (.ok (), σ') := by + rw [utInvoke_eqA] at hy + rw [ExceptT.run_bind] at hy + obtain ⟨x, hx, hy⟩ := hy + obtain ⟨e, σr⟩ := x + cases e with + | error u => + cases u + change y = (.error (), σr) at hy + exact Or.inl ⟨σr, hx, hy⟩ + | ok u => + cases u + change y ∈ ((NonDetE.liftE (ε := Unit) (utRtBodyA srcSlot base retypeBase t objSz slots dev)).run σr).1 at hy + rw [liftE_runD] at hy + obtain ⟨z, hz, hy⟩ := hy + obtain ⟨⟨⟩, σ'⟩ := z + change y = (.ok (), σ') at hy + exact Or.inr ⟨σr, σ', hx, hz, hy⟩ + +/-- FL4's `invoke_untyped` with `vcpu::init`, for a type other than the VCPU: the same outcomes. -/ +theorem utInvokeA64_memA {reset : Bool} (ht : t ≠ .archObject .vcpuObj) {σ : State Unit} + {y : Except Unit Unit × State Unit} + (h : y ∈ ((fl4InvokeUntyped (Z := Unit) + (.retype srcSlot reset base retypeBase t objSz slots dev)).run σ).1) : + y ∈ ((fl4InvokeUntypedA64 (Z := Unit) (.retype srcSlot reset base retypeBase t objSz slots dev)).run σ).1 := by + unfold fl4InvokeUntypedA64 + obtain ⟨e, σ'⟩ := y + cases e with + | error u => cases u; exact mem_bindE_errD h + | ok u => + cases u + refine mem_bindE_okD (a := ()) (s' := σ') h (mem_liftED ?_) + unfold NonDet.when + rw [ite_eq_right ht] + exact Set.mem_singleton _ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtRtGlobA.lean b/proofs/Fl4ProofsA64/P3/UtRtGlobA.lean new file mode 100644 index 000000000..656caeacf --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtRtGlobA.lean @@ -0,0 +1,161 @@ +/- +P3 (aarch64, A64-P3 Untyped W5, spec side): **what the body of `invoke_untyped` keeps of the spec state the relation +reads**. + +* `utRtBody_globA`: every outcome of `utRtBodyA` keeps the globals (`SpecGlobA`). + +No named premise. New definitions and theorems only. +-/ +import Fl4ProofsA64.P3.UtRtCapsA +import Fl4ProofsA64.P3.FrameA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Sched Classical + +noncomputable section + +open Fl4SpecA64 Fl4Spec + +/-- Every outcome of `m` keeps the globals. -/ +def UtGA {α : Type} (m : NonDet (State Unit) α) : Prop := ∀ σ r, r ∈ (m σ).1 → SpecGlobA σ r.2 + +theorem UtGA.bind {α β : Type} {m : NonDet (State Unit) α} {f : α → NonDet (State Unit) β} (hm : UtGA m) + (hf : ∀ a, UtGA (f a)) : UtGA (m >>= f) := fun σ r hr => by + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + exact (hm σ x hx).trans (hf x.1 x.2 r hr) + +theorem UtGA.state {α : Type} {m : NonDet (State Unit) α} (h : ∀ σ r, r ∈ (m σ).1 → r.2 = σ) : UtGA m := + fun σ r hr => by rw [h σ r hr]; exact SpecGlobA.refl σ + +theorem UtGA.pure {α : Type} (a : α) : UtGA (Pure.pure a : NonDet (State Unit) α) := + .state fun σ r h => by obtain rfl : r = (a, σ) := h; rfl + +theorem UtGA.gets {α : Type} (g : State Unit → α) : UtGA (NonDet.gets g) := + .state fun σ r h => by obtain rfl : r = (g σ, σ) := h; rfl + +theorem UtGA.fail {α : Type} : UtGA (NonDet.fail : NonDet (State Unit) α) := + fun _ _ h => absurd h (Set.notMem_empty _) + +theorem UtGA.assert (P : Prop) : UtGA (NonDet.assert P : NonDet (State Unit) Unit) := + .state fun _ _ h => by obtain ⟨-, rfl⟩ := h; rfl + +theorem UtGA.assertOpt {α : Type} (o : Option α) : UtGA (NonDet.assertOpt o : NonDet (State Unit) α) := by + cases o + · exact UtGA.fail + · exact .state fun σ r h => by obtain rfl : r = (_, σ) := h; rfl + +theorem ut_getCap_gA (sl : CslotPtr) : UtGA (getCap (Z := Unit) sl) := by + obtain ⟨oref, cref⟩ := sl + have go : UtGA (getObject (Z := Unit) oref) := .state fun _ _ h => (getObject_mem h).1 + refine UtGA.bind go fun obj => ?_ + cases obj <;> dsimp only <;> first + | exact UtGA.bind UtGA.fail fun _ => UtGA.assertOpt _ + | exact UtGA.bind (UtGA.pure _) fun _ => UtGA.assertOpt _ + | exact UtGA.bind (UtGA.assert _) fun _ => UtGA.bind (UtGA.pure _) fun _ => UtGA.assertOpt _ + +theorem ut_setCap_gA (cap : Cap) (sl : CslotPtr) : UtGA (setCap (Z := Unit) cap sl) := fun σ r hr => by + obtain ⟨oref, cref⟩ := sl + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + obtain ⟨e1, -⟩ := getObject_mem hx + obtain ⟨obj, σ1⟩ := x + simp only at e1 hr + subst e1 + have key : ∀ o : KernelObject, r ∈ ((Pure.pure o >>= fun o' => setObject (Z := Unit) oref o') σ1).1 → + SpecGlobA σ1 r.2 := by + intro o h + obtain ⟨y, hy, h⟩ := mem_bindA.1 h + obtain rfl : y = (o, σ1) := hy + obtain ⟨o0, -, hr2⟩ := setObject_mem h + rw [hr2] + exact ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, ⟨rfl, rfl, rfl, rfl⟩⟩ + have kfail : ∀ f : KernelObject → NonDet (State Unit) Unit, r ∈ ((NonDet.fail >>= f) σ1).1 → False := by + intro f h + obtain ⟨y, hy, -⟩ := mem_bindA.1 h + exact absurd hy (Set.notMem_empty _) + cases obj with + | cnode sz cn => + simp only at hr + split_ifs at hr + · exact key _ hr + · exact (kfail _ hr).elim + | tcb t => + simp only at hr + split_ifs at hr <;> + first + | exact (kfail _ hr).elim + | exact key _ hr + | _ => + try simp only at hr + exact (kfail _ hr).elim + +theorem ut_setCdt_gA (t : Cdt) : UtGA (setCdt (Z := Unit) t) := fun σ r hr => by + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + obtain rfl : x = (σ, σ) := hx + obtain rfl : r = ((), { σ with cdt := t }) := hr + exact ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, ⟨rfl, rfl, rfl, rfl⟩⟩ + +theorem ut_modify_gA {g : State Unit → State Unit} (h : ∀ σ, SpecGlobA σ (g σ)) : + UtGA (NonDet.modify g) := fun σ r hr => by + obtain rfl : r = ((), g σ) := hr + exact h σ + +theorem ut_setOriginal_gA (sl : CslotPtr) (v : Bool) : UtGA (setOriginal (Z := Unit) sl v) := + UtGA.bind (UtGA.gets _) fun _ => ut_modify_gA fun _ => + ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, ⟨rfl, rfl, rfl, rfl⟩⟩ + +theorem ut_doExtendedOp_gA (eop : DetExtMonad Unit) : UtGA (doExtendedOp (Z := Unit) eop) := by + unfold doExtendedOp + refine UtGA.bind (.state fun σ r h => by obtain rfl : r = (σ, σ) := h; rfl) fun _ => ?_ + refine UtGA.bind (.state fun σ r h => by rw [h.2]) fun _ => ut_modify_gA fun _ => + ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, ⟨rfl, rfl, rfl, rfl⟩⟩ + +theorem ut_createCap_gA (t : ApiObjectType) (bits : Nat) (src : CslotPtr) (dev : Bool) (x : CslotPtr × ObjRef) : + UtGA (createCap (Z := Unit) t bits src dev x) := by + obtain ⟨dest, oref⟩ := x + unfold createCap + exact UtGA.bind (UtGA.gets _) fun _ => UtGA.bind (UtGA.gets _) fun _ => UtGA.bind (ut_setCdt_gA _) fun _ => + UtGA.bind (ut_doExtendedOp_gA _) fun _ => UtGA.bind (ut_setOriginal_gA _ _) fun _ => ut_setCap_gA _ _ + +theorem ut_mapMx_gA {α : Type} {f : α → NonDet (State Unit) Unit} (h : ∀ a, UtGA (f a)) (l : List α) : + UtGA (NonDet.mapMx f l) := by + induction l with + | nil => unfold NonDet.mapMx; exact UtGA.pure _ + | cons a l ih => unfold NonDet.mapMx; exact UtGA.bind (h a) fun _ => ih + +theorem ut_doMachineOp_gA {α : Type} (mop : NonDet MachineState α) : UtGA (doMachineOp (Z := Unit) mop) := by + unfold doMachineOp + exact UtGA.bind (UtGA.gets _) fun _ => UtGA.bind (.state fun σ r h => by rw [h.2]) fun _ => + UtGA.bind (ut_modify_gA fun _ => ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, ⟨rfl, rfl, rfl, rfl⟩⟩) fun _ => + UtGA.pure _ + +theorem ut_initArch_gA (t : ApiObjectType) (dev : Bool) (ptr : ObjRef) (n sz : Nat) (refs : List ObjRef) : + UtGA (initArchObjects (Z := Unit) t dev ptr n sz refs) := by + unfold initArchObjects + split_ifs + · exact ut_mapMx_gA (fun _ => ut_doMachineOp_gA _) _ + · exact ut_mapMx_gA (fun _ => ut_doMachineOp_gA _) _ + · exact UtGA.pure _ + +theorem ut_retRegion_gA (t : ApiObjectType) (ptr : ObjRef) (n bits : Nat) (dev : Bool) : + UtGA (retypeRegionWith (Z := Unit) fl4ObjBitsApi ptr n bits t dev) := fun σ r hr => by + rw [(utRetRegion_invA _ _ _ _ _ _ hr).2] + split_ifs + · exact SpecGlobA.refl σ + · exact ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, ⟨rfl, rfl, rfl, rfl⟩⟩ + +/-- **The body keeps the globals.** -/ +theorem utRtBody_globA {srcSlot : CslotPtr} {base retypeBase : Word} {t : ApiObjectType} {objSz : Nat} + {slots : List CslotPtr} {dev : Bool} {σ : State Unit} {r : Unit × State Unit} + (hr : r ∈ (utRtBodyA srcSlot base retypeBase t objSz slots dev σ).1) : SpecGlobA σ r.2 := by + have h : UtGA (utRtBodyA srcSlot base retypeBase t objSz slots dev) := by + unfold utRtBodyA + exact UtGA.bind (ut_getCap_gA _) fun _ => UtGA.bind (UtGA.pure _) fun _ => UtGA.bind (UtGA.pure _) fun _ => + UtGA.bind (ut_setCap_gA _ _) fun _ => UtGA.bind (ut_retRegion_gA _ _ _ _ _) fun _ => + UtGA.bind (ut_initArch_gA _ _ _ _ _ _) fun _ => ut_mapMx_gA (fun x => ut_createCap_gA _ _ _ _ x) _ + exact h σ r hr + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtRtHighPostA.lean b/proofs/Fl4ProofsA64/P3/UtRtHighPostA.lean new file mode 100644 index 000000000..ca29f81e7 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtRtHighPostA.lean @@ -0,0 +1,201 @@ +/- +P3 (aarch64, A64-P3 Untyped W5): **`JAtZAX` after a RAM creation of `HighVSpace`s**. + +* **`utRt_highPostA`**: from the record at the creation's start, FL4's creation (`CrtE`, the new level-0 words + `UtHighZW`, the sharp write frame), `StateRelA` / `CdtRelA` after it, and any outcome of the spec body, `JAtZAX` at + the grown sets: the new windows join `Y`. + +Hypotheses: `hsub` (closure item A), the creation's window (`W`, `h1`, `h2`), `CrtE`, `HW` +(`utRetype_highWordsA`), `w`, `hS` / `hC` (`utSite_relsA`), `P'`, `har`, the invoked and destination slots' CNodes. +No named premise. New theorems only. +-/ +import Fl4ProofsA64.P3.UtRtPostA +import Fl4ProofsA64.P3.UtRtGlobA +import Fl4ProofsA64.P3.UtRtCapsA +import Fl4ProofsA64.P3.UtRtZeroPostA +import Fl4ProofsA64.P3.UtRtHighWordsA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +set_option maxRecDepth 100000 + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s s' : S} {a a' : A} {σ : State Unit} {m : McState} {x p sb fi : U64} + {nt us aligned dest off win : U64} + +/-- **`JAtZAX` after a RAM creation of `HighVSpace`s** (module docstring). -/ +theorem utRt_highPostA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (hx : x ∈ OS) (hc : capA st s x = .Untyped p sb fi false) + (W : UtWinZA nt aligned (zA nt.val us) win.val) + (h1 : p.val + fi.val ≤ aligned.val) + (h2 : aligned.val + win.val * 2 ^ (zA nt.val us).val ≤ p.val + 2 ^ sb.val) + (n14 : nt.val = 14) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (Ec : CrtE st H H.frame OS s s' x p sb false aligned nt us dest off win) + (w : WritesIn st s s' (UtCrtWA st H OS s nt aligned (zA nt.val us) win.val)) + (HW : UtHighZW st aligned (zA nt.val us) win.val s') + {t : ApiObjectType} (htt : objTypeOfA nt.val = some t) (hus : us.val < 64) + {srcSlot : CslotPtr} {base : Word} {slots : List CslotPtr} (hlen : slots.length = win.val) + (hsrcS : ∃ q, srcSlot.1 = specPtr q ∧ Z.th q = some (.cnode srcSlot.2.length)) + (hdst : ∀ d ∈ slots, ∃ q, d.1 = specPtr q ∧ Z.th q = some (.cnode d.2.length)) + {r : Unit × State Unit} + (hr : r ∈ (utRtBodyA srcSlot base (specPtr aligned) t us.val slots false σ).1) + {G' : MdbGhostA} (hS : StateRelA (utRtThA Z.th false nt us aligned win.val) (L.mem s') r.2) + (hC : CdtRelA st (utRtThA Z.th false nt us aligned win.val) s' r.2 G') + (P' : PlaceA (utRtThA Z.th false nt us aligned win.val) (L.ksb s)) (har : AR a' Z.hw) : + JAtZAX (L := L) AR + (utRtZA Z G' nt us aligned win.val ∅ ∅ ∅) s' a' r.2 m := by + have hn : nt.val < 15 := (objTypeOfA_some htt us.val).1 + have z1 : nt.val ≠ 1 := by omega + have z12 : nt.val ≠ 12 := by omega + have Cl : UtClrA st Z H s aligned.val (aligned.val + win.val * 2 ^ (zA nt.val us).val) := + utRt_winClrA J hx hc h1 h2 + have anchor : ∀ j, j < win.val → specPtr (bjA aligned (zA nt.val us) j) ≠ Fl4SpecA64.idleScPtr := by + intro j hj + have v := utRt_newValA W hj + exact utRet_anchorA (st := st) J.idleAnchor hx hc (by omega) (by omega) + have memA : ∀ q, q ∈ fl4RetypeAddrs fl4ObjBitsApi (specPtr aligned) slots.length us.val t ↔ + UtNewSpA aligned (zA nt.val us) win.val q := fun q => by + rw [hlen, utRet_addrs_memA htt hus] + rfl + have thOff : ∀ q k, Z.th q = some k → ¬ UtNewSpA aligned (zA nt.val us) win.val (specPtr q) := fun q k hq => + utRt_relOffA (m := m) J Cl W rfl rfl anchor (Or.inl ⟨q, by rw [hq]; simp, rfl⟩) + -- the spec outcome + obtain ⟨qS, eqS, hqS⟩ := hsrcS + obtain ⟨csS, hkS, -, hsS⟩ := J.j.1.st.cnode qS _ hqS + rw [← eqS] at hkS + obtain ⟨capS, hcapS, -⟩ := hsS srcSlot.2 rfl + have hdc : ∀ d ∈ slots, (∃ sz cn, σ.kheap d.1 = some (.cnode sz cn)) ∧ + d.1 ∉ fl4RetypeAddrs fl4ObjBitsApi (specPtr aligned) slots.length us.val t := by + intro d hd + obtain ⟨q, eq, hq⟩ := hdst d hd + obtain ⟨cs, hk, -⟩ := J.j.1.st.cnode q _ hq + rw [eq, memA] + exact ⟨⟨_, cs, hk⟩, thOff q _ hq⟩ + have hSa : srcSlot.1 ∉ fl4RetypeAddrs fl4ObjBitsApi (specPtr aligned) slots.length us.val t := by + rw [eqS, memA] + exact thOff qS _ hqS + obtain ⟨-, hCn, hNew, hK, -, -, -⟩ := utRtBody_invA hr hkS hcapS hSa hdc + -- an old object that is no CNode is kept + have keep : ∀ q, t = .untyped ∨ ¬ UtNewSpA aligned (zA nt.val us) win.val q → + ∀ ko, (∀ sz cn, ko ≠ .cnode sz cn) → ∀ u : Cap, + let σa := setObjA σ srcSlot.1 (.cnode srcSlot.2.length (Function.update csS srcSlot.2 (some u))) + σa.kheap q = some ko → σ.kheap q = some ko := by + intro q _ ko ne u σa h + by_cases e : q = srcSlot.1 + · have : σa.kheap q = some (.cnode srcSlot.2.length (Function.update csS srcSlot.2 (some u))) := by + rw [e]; exact Function.update_self .. + rw [this] at h + exact absurd (Option.some.inj h).symm (ne _ _) + · exact (Function.update_of_ne e ..).symm.trans h + have hkeep : ∀ q, ¬ UtNewSpA aligned (zA nt.val us) win.val q → (∀ sz cn, σ.kheap q ≠ some (.cnode sz cn)) → + r.2.kheap q = σ.kheap q := by + intro q hq nc + have e : q ≠ srcSlot.1 := fun e => nc _ _ (e ▸ hkS) + have ea : ∀ u : Cap, (setObjA σ srcSlot.1 (.cnode srcSlot.2.length (Function.update csS srcSlot.2 (some u)))).kheap q = + σ.kheap q := fun u => Function.update_of_ne e .. + rw [hK q (Or.inr (fun h => hq ((memA q).1 h))) (fun sz cn => by rw [ea]; exact nc sz cn), ea] + have newObj : t ≠ .untyped → ∀ j, j < win.val → + r.2.kheap (specPtr (bjA aligned (zA nt.val us) j)) = some (defaultObject t false us.val σ.curDomain) := + fun ht j hj => hNew ht _ ((memA _).2 ⟨j, hj, rfl⟩) + have back : ∀ q ko, (∀ sz cn, ko ≠ .cnode sz cn) → r.2.kheap q = some ko → σ.kheap q = some ko ∨ + (t ≠ .untyped ∧ UtNewSpA aligned (zA nt.val us) win.val q ∧ ko = defaultObject t false us.val σ.curDomain) := by + intro q ko ne h + by_cases g : t = .untyped ∨ ¬ UtNewSpA aligned (zA nt.val us) win.val q + · have g' : t = .untyped ∨ q ∉ fl4RetypeAddrs fl4ObjBitsApi (specPtr aligned) slots.length us.val t := + g.imp id (fun k m' => k ((memA q).1 m')) + obtain ⟨u0, eu⟩ : ∃ u0 : Cap, u0 = .untypedCap false base (bitsOf capS) (unat (specPtr aligned + + BitVec.ofNat 64 slots.length <<< fl4ObjBitsApi t us.val - base)) := ⟨_, rfl⟩ + refine Or.inl (keep q g ko ne u0 ?_) + rw [eu] + by_cases cn : ∃ sz c, (setObjA σ srcSlot.1 (.cnode srcSlot.2.length (Function.update csS srcSlot.2 (some + (.untypedCap false base (bitsOf capS) (unat (specPtr aligned + + BitVec.ofNat 64 slots.length <<< fl4ObjBitsApi t us.val - base))))))).kheap q = some (.cnode sz c) + · obtain ⟨sz, c, k⟩ := cn + rw [(hCn q sz c g' k).1] at h + exact absurd (Option.some.inj h).symm (ne _ _) + · rw [hK q g' (fun sz c k => cn ⟨sz, c, k⟩)] at h + exact h + · have gt : t ≠ .untyped := fun e => g (Or.inl e) + have gq : UtNewSpA aligned (zA nt.val us) win.val q := by + by_contra k + exact g (Or.inr k) + refine Or.inr ⟨gt, gq, ?_⟩ + rw [hNew gt q ((memA q).2 gq)] at h + exact (Option.some.inj h).symm + have hcap : ∀ y, IsSlotA (utRtThA Z.th false nt us aligned win.val) y → + UtCapAfterA Z.th (L.mem s) (L.mem s') nt us aligned win.val y := by + intro y hy + by_cases old : IsSlotA Z.th y + · by_cases ex : y = x + · obtain ⟨nfi, e⟩ := Ec.src + exact Or.inr (Or.inr (Or.inl ⟨p, sb, nfi, false, by rw [ex]; exact e⟩)) + · by_cases ed : ∃ j < win.val, y = djA dest off j + · obtain ⟨j, hj, e⟩ := ed + exact Or.inr (Or.inl ⟨j, hj, by rw [e]; exact Ec.new j hj⟩) + · exact Or.inl ⟨old, Ec.keep y (hsub y old) ex (fun j hj e => ed ⟨j, hj, e⟩)⟩ + · refine Or.inr (Or.inr (Or.inr ?_)) + obtain ⟨q, h⟩ := hy + have isNew : ∀ k, utRtThA Z.th false nt us aligned win.val q = some k → + (∀ k', Z.th q = some k' → k' ≠ k) → UtNewA false nt us aligned win.val q ∧ k = utKindA nt us := by + intro k hk ne + unfold utRtThA at hk + split_ifs at hk with g + · exact ⟨g, (Option.some.inj hk).symm⟩ + · exact absurd rfl (ne k hk) + have ko : utKindA nt us = .other (2 ^ (zA nt.val us).val) := by + unfold utKindA + rw [ite_eq_right z1, ite_eq_right (by omega)] + rcases h with ⟨rd, hq, i, hi, e⟩ | ⟨hq, i, hi, e⟩ + · have g := isNew _ hq (fun k' hk' ek => old ⟨q, Or.inl ⟨rd, by rw [hk', ek], i, hi, e⟩⟩) + have ek := g.2 + rw [ko] at ek + cases ek + · have g := isNew _ hq (fun k' hk' ek => old ⟨q, Or.inr ⟨by rw [hk', ek], i, hi, e⟩⟩) + have ek := g.2 + rw [ko] at ek + cases ek + have et : t = .archObject (.ext .highVSpaceObj) := by + rw [n14] at htt + simpa [objTypeOfA, eq_comm] using htt + rw [← utRtMA_empty m] + refine utRt_postA (Tn := ∅) (Cn := ∅) (Vn := ∅) J hx hc W h1 h2 hn + (fun h => by omega) w hS hC P' har (utRtBody_globA hr) hkeep (fun q => ⟨fun h => h.elim, fun h => absurd h.2 z1⟩) + (fun c h => h.elim) (fun v h => h.elim) (fun h => absurd h z12) hcap (fun q h => h.elim) (fun c h => h.elim) + (fun v h => h.elim) (fun q h => ?_) (fun q h => ?_) (fun y h => ?_) (fun q y h => ?_) + (fun q es h => ?_) (fun q l1 h => ?_) + · exact absurd h.1 (by omega) + · exact absurd h.1 (by omega) + · obtain ⟨-, j, hj, rfl⟩ := h + have o := newObj (by rw [et]; simp) j hj + rw [et] at o + exact ⟨⟨_, o⟩, (HW j hj).1, (HW j hj).2.1⟩ + · rcases back q _ (fun _ _ e => by cases e) h with k | ⟨-, -, e⟩ + · exact Or.inl k + · rw [et] at e + cases e + · rcases back q _ (fun _ _ e => by cases e) h with k | ⟨-, -, e⟩ + · exact Or.inl k + · rw [et] at e + cases e + · rcases back q _ (fun _ _ e => by cases e) h with k | ⟨-, gq, -⟩ + · exact Or.inl k + · obtain ⟨j, hj, e⟩ := gq + exact Or.inr ⟨_, ⟨n14, j, hj, rfl⟩, e.symm⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtRtHighWordsA.lean b/proofs/Fl4ProofsA64/P3/UtRtHighWordsA.lean new file mode 100644 index 000000000..0ea570efa --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtRtHighWordsA.lean @@ -0,0 +1,270 @@ +/- +P3 (aarch64, A64-P3 Untyped W5): **the level-0 words of a Retype's new `HighVSpace`s**: the table descriptor at 511, +zeros below 256, and the empty level-1 table. + +* `UtHighZW`: those words of every window. +* **`utRetype_highWordsA`**: after a non-reset RAM Retype of `HighVSpace`s from `CrtP`, they hold in every new object + (`utRetype_frameA`'s creation loop with the words carried; one object by `ut_highVSpaceA`). + +Named premises: +* [P] `hK`, `hK0`: FL4's `kernel_ttbr1_entry` at every arch state, zero below 256. +* [J] `hk`: the window starts in the kernel window. + +New definitions and theorems only. +-/ +import Fl4ProofsA64.P3.UtRtFrameA +import Fl4ProofsA64.P3.UtRetVcpuA +import Fl4ProofsA64.P3.UtVsObjA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +set_option maxRecDepth 100000 + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + +/-- The level-0 words and the (zero) level-1 words of every window. -/ +def UtHighZW (st : machine.Store S) [L : LawfulStore st] (aligned z : U64) (len : Nat) (s : S) : Prop := + ∀ l, l < len → + (dBitA (L.mem s (peAF (bjA aligned z l) 511#u64)) 0 = true ∧ + dBitA (L.mem s (peAF (bjA aligned z l) 511#u64)) 1 = true ∧ + dTabA (L.mem s (peAF (bjA aligned z l) 511#u64)) = wa (bjA aligned z l) 4096#u64) ∧ + (∀ i : U64, i.val < 256 → L.mem s (peAF (bjA aligned z l) i) = 0#u64) ∧ + ∀ i : U64, i.val < 512 → L.mem s (peAF (wa (bjA aligned z l) 4096#u64) i) = 0#u64 + +set_option maxHeartbeats 4000000 in +/-- **A new `HighVSpace`'s level-0 words** (module docstring). -/ +theorem utRetype_highWordsA (hA : P0.ArchTotal ar) {H1 : Heap} {Cs Sg OS1 : Set U64} {s1 : S} + {slot p sb fi1 : U64} {aligned nt us dest off win : U64} + (C : CrtP st H1 Cs Sg OS1 s1 slot p sb fi1 false aligned nt us dest off win) {G : List (List U64)} + {par : U64 → Option U64} (hT : TreeA st OS1 G par s1) (hn1 : nt.val = 14) (hal4 : aligned.val % 4096 = 0) + (hk : 18446603336221196288 ≤ aligned.val) {k : U64 → U64} + (hK : ∀ (a : A) (i : U64), i.val < 511 → ar.kernel_ttbr1_entry a 0#u64 i = ok (k i)) + (hK0 : ∀ i : U64, i.val < 256 → k i = 0#u64) (a1 : A) : + untyped.retype st ar s1 a1 slot false aligned nt us dest off win false ⦃ r => + UtHighZW st aligned (zA nt.val us) win.val r.2.1 ⦄ := by + obtain ⟨-, hal, hfi, hA0, hAw, hinj, hcan, -⟩ := C.ar + obtain ⟨hSA, -, hK1⟩ := C.stz + obtain ⟨-, -, hV1, hUZ1, -⟩ := hSA + have hnA := nodeAt_aligned C.n0 (by rw [debug_val]; omega) (by rw [debug_val]) + have hnF := KsOffA.node hK1 C.core0 C.n0 (by rw [debug_val]; omega) + have hM := hT.lk + obtain ⟨-, -, hpf⟩ := ut_rangeA (capA_ut_b C.cap) (by + have := hT.ut.reg slot C.src (by rw [C.cap]; exact physA_ut _ _ _ _); rwa [C.cap] at this) + have hs0 : capA st s1 slot ≠ .Null := by rw [C.cap]; exact fun h => by cases h + have hsn : ∀ j, j < win.val → slot ≠ djA dest off j := fun j hj e => hs0 (by rw [e]; exact C.dn j hj) + have hz13 : (zA nt.val us).val = 13 := by rw [hn1]; exact zA_14 us + have hX : 2 ^ (zA nt.val us).val = 8192 := by rw [hz13]; norm_num + have hfit : aligned.val + win.val * 2 ^ (zA nt.val us).val ≤ 2 ^ 64 := by omega + have hwf : ∀ j, j < win.val → CapRt.EncWf (newCapA nt.val (bjA aligned (zA nt.val us) j) us false) := fun j hj => + newCapA_encWf _ _ _ _ (hcan j hj) C.hus + have hdn1 : ∀ s', RtSrc st s1 slot p sb false aligned.val (aligned.val + win.val * 2 ^ (zA nt.val us).val) s' → + ∀ j, j < win.val → capA st s' (djA dest off j) = .Null := fun s' h1 j hj => by + obtain ⟨-, -, -, hc1, -, -⟩ := h1.facts C.os C.src + rw [hc1 _ (C.dO j hj) (fun e => hsn j hj e.symm)]; exact C.dn j hj + have hbj : ∀ l, l < win.val → (bjA aligned (zA nt.val us) l).val = aligned.val + l * 8192 ∧ + aligned.val + l * 8192 + 8192 ≤ p.val + 2 ^ sb.val ∧ p.val + fi1.val ≤ aligned.val + l * 8192 := by + intro l hl + have h1 : (l + 1) * 8192 ≤ win.val * 8192 := Nat.mul_le_mul_right _ hl + have hAw' := hAw + rw [hX] at hAw' + have hfit' := hfit + rw [hX] at hfit' + have := bjA_val (base := aligned) (z := zA nt.val us) (j := l) (by rw [hX]; omega) + rw [hX] at this + exact ⟨this, by omega, by omega⟩ + have hf8 : ∀ l, l < win.val → (bjA aligned (zA nt.val us) l).val + 8192 ≤ 2 ^ 64 := by + intro l hl + have h1 : (l + 1) * 8192 ≤ win.val * 8192 := Nat.mul_le_mul_right _ hl + have := hfit + rw [hX] at this + rw [(hbj l hl).1]; omega + have pv : ∀ l, l < win.val → ∀ i : U64, i.val < 512 → + (peAF (bjA aligned (zA nt.val us) l) i).val = aligned.val + l * 8192 + 8 * i.val := by + intro l hl i hi + have := hf8 l hl + rw [peAF_valA (by omega) hi, (hbj l hl).1] + -- a write of `OS` slot words keeps the carried words + have inOS : ∀ (l : Nat), l < win.val → ∀ i : U64, i.val < 512 → + Aligned (peAF (bjA aligned (zA nt.val us) l) i) ∧ ¬ InOS OS1 (peAF (bjA aligned (zA nt.val us) l) i) := by + intro l hl i hi + have e := pv l hl i hi + refine ⟨by show _ % 8 = 0; rw [e]; omega, fun ⟨y, hy, g1, g2⟩ => ?_⟩ + have := (hbj l hl).2.1 + have := (hbj l hl).2.2 + have nw : ¬ W7.CapW H1.tcbs H1.omega Sg (peAF (bjA aligned (zA nt.val us) l) i) := + in_free_not_capw C.heap hV1 C.src C.cap (by rw [e]; omega) (by rw [e]; omega) + have al0 := C.os.al0 hy + unfold Aligned at al0 + obtain ⟨d, hd⟩ : ∃ d : U64, d.val = (peAF (bjA aligned (zA nt.val us) l) i).val - y.val := + ⟨⟨BitVec.ofNat 64 ((peAF (bjA aligned (zA nt.val us) l) i).val - y.val)⟩, by + show (BitVec.ofNat 64 _).toNat = _ + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega)⟩ + have d8 : d.val % 8 = 0 := by + rw [e] at g1 hd + omega + have ey : wa y d = peAF (bjA aligned (zA nt.val us) l) i := by + apply UScalar.eq_of_val_eq + rw [wa_valP, hd] + rw [Nat.mod_eq_of_lt (by have := (peAF (bjA aligned (zA nt.val us) l) i).hBounds; simp at this; omega)] + omega + exact nw (ey ▸ C.os.cw hy (k := d) (by rw [hd]; omega) d8) + have pv1 : ∀ l, l < win.val → ∀ i : U64, i.val < 512 → + (peAF (wa (bjA aligned (zA nt.val us) l) 4096#u64) i).val = aligned.val + l * 8192 + 4096 + 8 * i.val := by + intro l hl i hi + have hf := hf8 l hl + have l1 : (wa (bjA aligned (zA nt.val us) l) 4096#u64).val = aligned.val + l * 8192 + 4096 := by + rw [wa_valP, show (4096#u64 : U64).val = 4096 from rfl, (hbj l hl).1] + refine Nat.mod_eq_of_lt ?_ + rw [(hbj l hl).1] at hf + omega + rw [peAF_valA (by rw [l1]; rw [(hbj l hl).1] at hf; omega) hi, l1] + have inOS1 : ∀ (l : Nat), l < win.val → ∀ i : U64, i.val < 512 → + Aligned (peAF (wa (bjA aligned (zA nt.val us) l) 4096#u64) i) ∧ + ¬ InOS OS1 (peAF (wa (bjA aligned (zA nt.val us) l) 4096#u64) i) := by + intro l hl i hi + have e := pv1 l hl i hi + refine ⟨by show _ % 8 = 0; rw [e]; omega, fun ⟨y, hy, g1, g2⟩ => ?_⟩ + have := (hbj l hl).2.1 + have := (hbj l hl).2.2 + have nw : ¬ W7.CapW H1.tcbs H1.omega Sg (peAF (wa (bjA aligned (zA nt.val us) l) 4096#u64) i) := + in_free_not_capw C.heap hV1 C.src C.cap (by rw [e]; omega) (by rw [e]; omega) + have al0 := C.os.al0 hy + unfold Aligned at al0 + obtain ⟨d, hd⟩ : ∃ d : U64, d.val = (peAF (wa (bjA aligned (zA nt.val us) l) 4096#u64) i).val - y.val := + ⟨⟨BitVec.ofNat 64 ((peAF (wa (bjA aligned (zA nt.val us) l) 4096#u64) i).val - y.val)⟩, by + show (BitVec.ofNat 64 _).toNat = _ + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega)⟩ + have d8 : d.val % 8 = 0 := by + rw [e] at g1 hd + omega + have ey : wa y d = peAF (wa (bjA aligned (zA nt.val us) l) 4096#u64) i := by + apply UScalar.eq_of_val_eq + rw [wa_valP, hd] + rw [Nat.mod_eq_of_lt (by + have := (peAF (wa (bjA aligned (zA nt.val us) l) 4096#u64) i).hBounds; simp at this; omega)] + omega + exact nw (ey ▸ C.os.cw hy (k := d) (by rw [hd]; omega) d8) + let V : S → Nat → Prop := fun s' j => j ≤ win.val ∧ UtHighZW st aligned (zA nt.val us) j s' + have keepOS : ∀ (s s' : S) (j : Nat), WritesIn st s s' (InOS OS1) → V s j → V s' j := by + intro s s' j w h + refine ⟨h.1, fun l hlj => ?_⟩ + have hl : l < win.val := lt_of_lt_of_le hlj h.1 + have m : ∀ i : U64, i.val < 512 → L.mem s' (peAF (bjA aligned (zA nt.val us) l) i) = + L.mem s (peAF (bjA aligned (zA nt.val us) l) i) := fun i hi => + w.mem_eq (inOS l hl i hi).1 (inOS l hl i hi).2 + obtain ⟨g1, g2, g3⟩ := h.2 l hlj + refine ⟨?_, fun i hi => by rw [m i (by omega)]; exact g2 i hi, + fun i hi => by rw [w.mem_eq (inOS1 l hl i hi).1 (inOS1 l hl i hi).2]; exact g3 i hi⟩ + rw [m 511#u64 (by simp)] + exact g1 + refine WP.spec_mono (untyped.retype.genJK ar a1 C.cap nt us dest off win aligned false C.hn C.hz hfit + (J := fun s' s'' i => UtLoopA st OS1 G par s' slot (djA dest off) + (fun j => newCapA nt.val (bjA aligned (zA nt.val us) j) us false) s'' i ∧ + ZTc st H1 s1 aligned (zA nt.val us) win.val s'' i ∧ V s'' i) + (K := fun s' s'' i => UtLoopA st OS1 G par s' slot (djA dest off) + (fun j => newCapA nt.val (bjA aligned (zA nt.val us) j) us false) s'' i ∧ + ZTc st H1 s1 aligned (zA nt.val us) win.val s'' (i + 1) ∧ V s'' (i + 1)) + (fun _ _ s' a' j hj h => ?_) (fun s' h1 s'' i hi h => ?_) (fun s' h1 => ?_)) ?_ + · obtain ⟨hL, hZ, hVs⟩ := h + have P1s := create_object.hCOZ (ar := ar) hA C.heap C.os hV1 C.src C.cap nt us win aligned false C.hn + (fun h => by cases h) hal hA0 hAw hpf hnA C.nc (hnF slot C.src p sb fi1 C.cap) s' a' j hj hZ + obtain ⟨-, hzero, -⟩ := id hZ + have hb := hbj j hj + have al : (bjA aligned (zA nt.val us) j).val % 4096 = 0 := by rw [hb.1]; omega + have hf := hf8 j hj + -- a word of window `j` is zero before its creation + have wz : ∀ w : U64, Aligned w → aligned.val + j * 8192 ≤ w.val → w.val < aligned.val + j * 8192 + 8192 → + L.mem s' w = 0#u64 := by + intro w al g1 g2 + have h1 : (j + 1) * 8192 ≤ win.val * 8192 := Nat.mul_le_mul_right _ hj + generalize 2 ^ (zA nt.val us).val = X at hzero hX + subst hX + exact hzero w al g1 (by omega) + have l1 : (wa (bjA aligned (zA nt.val us) j) 4096#u64).val = aligned.val + j * 8192 + 4096 := by + rw [wa_valP, show (4096#u64 : U64).val = 4096 from rfl, hb.1] + refine Nat.mod_eq_of_lt ?_ + rw [hb.1] at hf + omega + have z0 : ∀ i : U64, i.val < 512 → L.mem s' (peAF (bjA aligned (zA nt.val us) j) i) = 0#u64 := by + intro i hi + have e := pv j hj i hi + exact wz _ (inOS j hj i hi).1 (by omega) (by omega) + have z1 : ∀ i : U64, i.val < 512 → + L.mem s' (peAF (wa (bjA aligned (zA nt.val us) j) 4096#u64) i) = 0#u64 := by + intro i hi + have e : (peAF (wa (bjA aligned (zA nt.val us) j) 4096#u64) i).val = aligned.val + j * 8192 + 4096 + 8 * i.val := by + rw [peAF_valA (by rw [l1]; rw [hb.1] at hf; omega) hi, l1] + exact wz _ (by show _ % 8 = 0; rw [e]; omega) (by omega) (by omega) + have M := ut_highVSpaceA (st := st) ar s' a' nt (bjA aligned (zA nt.val us) j) us false k hn1 al + (by rw [hb.1]; omega) hf z0 z1 (hK a') hK0 + refine WP.spec_mono (spec_andA P1s M) ?_ + rintro r ⟨⟨hc, hk', hZ'⟩, -, -, hw, -, q0, q511, q1, -⟩ + refine ⟨hc, hL.keep C.os (fun l hl => C.dO l (by omega)) hk', hZ', hj, fun l hlj => ?_⟩ + have hl : l < win.val := lt_of_lt_of_le hlj hj + by_cases e : l = j + · subst e + exact ⟨q511, q0, q1⟩ + · have m : ∀ i : U64, i.val < 512 → L.mem r.2.1 (peAF (bjA aligned (zA nt.val us) l) i) = + L.mem s' (peAF (bjA aligned (zA nt.val us) l) i) := by + intro i hi + refine hw.mem_eq (inOS l hl i hi).1 ?_ + unfold W7.R.ST.InWin + rw [pv l hl i hi, hb.1] + rcases Nat.lt_or_ge l j with g | g + · have : (l + 1) * 8192 ≤ j * 8192 := Nat.mul_le_mul_right _ g + omega + · have : (j + 1) * 8192 ≤ l * 8192 := Nat.mul_le_mul_right _ (by omega) + omega + have m1 : ∀ i : U64, i.val < 512 → L.mem r.2.1 (peAF (wa (bjA aligned (zA nt.val us) l) 4096#u64) i) = + L.mem s' (peAF (wa (bjA aligned (zA nt.val us) l) 4096#u64) i) := by + intro i hi + refine hw.mem_eq (inOS1 l hl i hi).1 ?_ + unfold W7.R.ST.InWin + rw [pv1 l hl i hi, hb.1] + rcases Nat.lt_or_ge l j with g | g + · have : (l + 1) * 8192 ≤ j * 8192 := Nat.mul_le_mul_right _ g + omega + · have : (j + 1) * 8192 ≤ l * 8192 := Nat.mul_le_mul_right _ (by omega) + omega + obtain ⟨g1, g2, g3⟩ := hVs.2 l (by omega) + refine ⟨?_, fun i hi => by rw [m i (by omega)]; exact g2 i hi, fun i hi => by rw [m1 i hi]; exact g3 i hi⟩ + rw [m 511#u64 (by simp)] + exact g1 + · -- one insert + have hnx : NxA st s'' slot ≠ 0#u64 → NxA st s'' slot ∈ OS1 := (MdbLk.nxG C.os h.1.1 h.1.2.2.1).2 + refine WP.spec_mono (WP.spec_and + (UtLoopA.step C.os hi h.1 C.dO (hdn1 s' h1) C.dc hinj hsn hwf (fun j _ => newCapA_ne _ _ _ _)) + (WP.spec_and + (cdt.insert_new_cap.zqW (Zq := fun s j => ZTc st H1 s1 aligned (zA nt.val us) win.val s j) C.os + (fun _ _ _ h w => ZTc.capw C.heap C.os hV1 C.src C.cap hA0 hAw hnA C.nc h w) + (C.dO i hi) (MdbLk.nxG C.os h.1.1 h.1.2.2.1) h.2.1) + (utInsert_inOSA C.os C.src (C.dO i hi) hnx))) ?_ + rintro r ⟨hl, hz, hw⟩ + exact ⟨hl, hz, keepOS _ _ _ hw h.2.2⟩ + · -- the source rewritten + obtain ⟨-, -, -, -, -, hW⟩ := h1.facts C.os C.src + have L0 := h1.lk C.os C.src hM hs0 + have hsG : slot ∈ G.flatten := (hM.members slot).2 ⟨C.src, hs0⟩ + refine ⟨⟨L0, by rw [utPA_zero]; exact hT.tree, hsG, fun _ _ _ => rfl, + fun j hj => absurd hj (Nat.not_lt_zero j), fun y x => ?_, fun _ _ _ => rfl, + fun j hj => absurd hj (Nat.not_lt_zero j)⟩, + ZTc.start C.heap C.os hV1 C.src C.cap hA0 hAw hnA C.nc hUZ1 C.hd hW, + Nat.zero_le _, fun l hl => absurd hl (Nat.not_lt_zero l)⟩ + rw [utPA_zero] + exact ⟨Or.inl, fun h => h.elim id fun ⟨j, hj, _⟩ => absurd hj (Nat.not_lt_zero j)⟩ + rintro r ⟨-, s', -, -, -, hV⟩ + exact hV.2 + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtRtInvA.lean b/proofs/Fl4ProofsA64/P3/UtRtInvA.lean new file mode 100644 index 000000000..b57b8e814 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtRtInvA.lean @@ -0,0 +1,394 @@ +/- +P3 (aarch64, A64-P3 Untyped W3, spec side): **the whole of FL4's `invoke_untyped` keeps SC objects and data pages** +off the new addresses, and has `retype_region`'s defaults at them (lead 01:35Z). + +* `UtLeafKeepA σ σ'`: each heap entry is unchanged, or is neither before nor after an SC or a data page. +* `set_cap`, `create_cap`, `init_arch_objects` (cache maintenance only, for every type, VSpace ones included) and + FL4's `vcpu::init` step all satisfy it, for every outcome. +* `utRetRegion_invA`: every outcome of `retype_region`. +* `utRtBodyA`: the body of `invoke_untyped` after its reset (`utInvoke_eqA`, by definition). + **`utRtBody_leafA`**: every outcome is `σ →(leaf-keeping) σ1 →(retype_region) →(leaf-keeping)`. +* **`utInvoke_scSizeA`**, **`utInvoke_dataPageA`**: `scSize` after the body, and `utEnd_devFrDisjA`'s `hσ`. + +`resetUntypedCap` (when the invocation resets) is `UtResetLoopA`'s and is not covered here. No named premise. +-/ +import Fl4ProofsA64.P3.UtRtSpecA +import Fl4ProofsA64.P3.SpecFrA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Sched Classical + +noncomputable section + +open Fl4SpecA64 Fl4Spec + +/-- An SC object or a data page. -/ +def UtLeafA (o : Option KernelObject) : Prop := + (∃ y k, o = some (.schedContext y k)) ∨ ∃ d pg, o = some (.archObj (.dataPage d pg)) + +/-- Every heap entry is kept, or is no SC or data page before and after. -/ +def UtLeafKeepA (σ σ' : State Unit) : Prop := + ∀ q, σ'.kheap q = σ.kheap q ∨ (¬ UtLeafA (σ.kheap q) ∧ ¬ UtLeafA (σ'.kheap q)) + +theorem UtLeafKeepA.of_kheap {σ σ' : State Unit} (h : σ'.kheap = σ.kheap) : UtLeafKeepA σ σ' := + fun q => Or.inl (by rw [h]) + +theorem UtLeafKeepA.trans {σ1 σ2 σ3 : State Unit} (h1 : UtLeafKeepA σ1 σ2) (h2 : UtLeafKeepA σ2 σ3) : + UtLeafKeepA σ1 σ3 := fun q => by + rcases h1 q with a | ⟨a, a'⟩ <;> rcases h2 q with b | ⟨b, b'⟩ + · exact Or.inl (b.trans a) + · exact Or.inr ⟨a ▸ b, b'⟩ + · exact Or.inr ⟨a, b ▸ a'⟩ + · exact Or.inr ⟨a, b'⟩ + +/-- A leaf entry is the same before and after. -/ +theorem UtLeafKeepA.iff {σ σ' : State Unit} (h : UtLeafKeepA σ σ') {q : ObjRef} {o : KernelObject} + (hl : UtLeafA (some o)) : σ'.kheap q = some o ↔ σ.kheap q = some o := by + rcases h q with e | ⟨a, b⟩ + · rw [e] + · exact ⟨fun e => absurd (e ▸ hl) b, fun e => absurd (e ▸ hl) a⟩ + +/-- Every outcome of `m` keeps the leaves. -/ +def UtKA {α : Type} (m : NonDet (State Unit) α) : Prop := ∀ σ r, r ∈ (m σ).1 → UtLeafKeepA σ r.2 + +theorem UtKA.bind {α β : Type} {m : NonDet (State Unit) α} {f : α → NonDet (State Unit) β} (hm : UtKA m) + (hf : ∀ a, UtKA (f a)) : UtKA (m >>= f) := fun σ r hr => by + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + exact (hm σ x hx).trans (hf x.1 x.2 r hr) + +/-- A computation that does not change the heap. -/ +theorem UtKA.heap {α : Type} {m : NonDet (State Unit) α} (h : ∀ σ r, r ∈ (m σ).1 → r.2.kheap = σ.kheap) : + UtKA m := fun σ r hr => .of_kheap (h σ r hr) + +theorem UtKA.pure {α : Type} (a : α) : UtKA (Pure.pure a : NonDet (State Unit) α) := + .heap fun σ r h => by obtain rfl : r = (a, σ) := h; rfl + +theorem UtKA.gets {α : Type} (g : State Unit → α) : UtKA (NonDet.gets g) := + .heap fun σ r h => by obtain rfl : r = (g σ, σ) := h; rfl + +theorem UtKA.modify {g : State Unit → State Unit} (h : ∀ σ, (g σ).kheap = σ.kheap) : UtKA (NonDet.modify g) := + .heap fun σ r hr => by obtain rfl : r = ((), g σ) := hr; exact h σ + +theorem UtKA.fail {α : Type} : UtKA (NonDet.fail : NonDet (State Unit) α) := + fun _ _ h => absurd h (Set.notMem_empty _) + +theorem UtKA.assert (P : Prop) : UtKA (NonDet.assert P : NonDet (State Unit) Unit) := + .heap fun _ _ h => by obtain ⟨-, rfl⟩ := h; rfl + +theorem UtKA.assertOpt {α : Type} (o : Option α) : UtKA (NonDet.assertOpt o : NonDet (State Unit) α) := by + cases o + · exact UtKA.fail + · exact .heap fun σ r h => by obtain rfl : r = (_, σ) := h; rfl + +theorem ut_getCap_kA (sl : CslotPtr) : UtKA (getCap (Z := Unit) sl) := by + obtain ⟨oref, cref⟩ := sl + have go : UtKA (getObject (Z := Unit) oref) := .heap fun _ _ h => by rw [(getObject_mem h).1] + refine UtKA.bind go fun obj => ?_ + cases obj <;> dsimp only <;> first + | exact UtKA.bind UtKA.fail fun _ => UtKA.assertOpt _ + | exact UtKA.bind (UtKA.pure _) fun _ => UtKA.assertOpt _ + | exact UtKA.bind (UtKA.assert _) fun _ => UtKA.bind (UtKA.pure _) fun _ => UtKA.assertOpt _ + +theorem ut_setCap_kA (cap : Cap) (sl : CslotPtr) : UtKA (setCap (Z := Unit) cap sl) := fun σ r hr => by + obtain ⟨oref, cref⟩ := sl + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + obtain ⟨e1, e2⟩ := getObject_mem hx + obtain ⟨obj, σ1⟩ := x + simp only at e1 e2 hr + subst e1 + have nl : ∀ o : KernelObject, ((∃ r cs, o = .cnode r cs) ∨ ∃ t, o = .tcb t) → ¬ UtLeafA (some o) := by + rintro o (⟨r, cs, rfl⟩ | ⟨t, rfl⟩) (⟨y, k, e⟩ | ⟨d, pg, e⟩) <;> cases e + have key : ∀ o, ((∃ r cs, o = .cnode r cs) ∨ ∃ t, o = .tcb t) → ((∃ r cs, obj = .cnode r cs) ∨ ∃ t, obj = .tcb t) → + r ∈ ((Pure.pure o >>= fun o' => setObject (Z := Unit) oref o') σ1).1 → UtLeafKeepA σ1 r.2 := by + intro o ho hobj h + obtain ⟨y, hy, h⟩ := mem_bindA.1 h + obtain rfl : y = (o, σ1) := hy + obtain ⟨o0, -, hr2⟩ := setObject_mem h + rw [hr2] + intro q + by_cases hq : q = oref + · subst hq + refine Or.inr ⟨by rw [e2]; exact nl _ hobj, ?_⟩ + simp only [Function.update_self] + exact nl _ ho + · exact Or.inl (Function.update_of_ne hq ..) + have kfail : ∀ f : KernelObject → NonDet (State Unit) Unit, r ∈ ((NonDet.fail >>= f) σ1).1 → False := by + intro f h + obtain ⟨y, hy, -⟩ := mem_bindA.1 h + exact absurd hy (Set.notMem_empty _) + cases obj with + | cnode sz cn => + simp only at hr + split_ifs at hr + · exact key _ (Or.inl ⟨_, _, rfl⟩) (Or.inl ⟨_, _, rfl⟩) hr + · exact (kfail _ hr).elim + | tcb t => + simp only at hr + split_ifs at hr <;> + first + | exact (kfail _ hr).elim + | exact key _ (Or.inr ⟨_, rfl⟩) (Or.inr ⟨_, rfl⟩) hr + | _ => + try simp only at hr + exact (kfail _ hr).elim + +theorem ut_setCdt_kA (t : Cdt) : UtKA (setCdt (Z := Unit) t) := fun σ r hr => by + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + obtain rfl : x = (σ, σ) := hx + obtain rfl : r = ((), { σ with cdt := t }) := hr + exact .of_kheap rfl + +theorem ut_setOriginal_kA (sl : CslotPtr) (v : Bool) : UtKA (setOriginal (Z := Unit) sl v) := + UtKA.bind (UtKA.gets _) fun _ => UtKA.modify fun _ => rfl + +theorem ut_doExtendedOp_kA (eop : DetExtMonad Unit) : UtKA (doExtendedOp (Z := Unit) eop) := by + unfold doExtendedOp + refine UtKA.bind (.heap fun σ r h => by obtain rfl : r = (σ, σ) := h; rfl) fun _ => ?_ + refine UtKA.bind (.heap fun σ r h => by rw [h.2]) fun _ => UtKA.modify fun _ => rfl + +theorem ut_createCap_kA (t : ApiObjectType) (bits : Nat) (src : CslotPtr) (dev : Bool) (x : CslotPtr × ObjRef) : + UtKA (createCap (Z := Unit) t bits src dev x) := by + obtain ⟨dest, oref⟩ := x + unfold createCap + exact UtKA.bind (UtKA.gets _) fun _ => UtKA.bind (UtKA.gets _) fun _ => UtKA.bind (ut_setCdt_kA _) fun _ => + UtKA.bind (ut_doExtendedOp_kA _) fun _ => UtKA.bind (ut_setOriginal_kA _ _) fun _ => ut_setCap_kA _ _ + +theorem ut_mapMx_kA {α : Type} {f : α → NonDet (State Unit) Unit} (h : ∀ a, UtKA (f a)) (l : List α) : + UtKA (NonDet.mapMx f l) := by + induction l with + | nil => unfold NonDet.mapMx; exact UtKA.pure _ + | cons a l ih => unfold NonDet.mapMx; exact UtKA.bind (h a) fun _ => ih + +theorem ut_doMachineOp_kA {α : Type} (mop : NonDet MachineState α) : UtKA (doMachineOp (Z := Unit) mop) := by + unfold doMachineOp + exact UtKA.bind (UtKA.gets _) fun _ => UtKA.bind (.heap fun σ r h => by rw [h.2]) fun _ => + UtKA.bind (UtKA.modify fun _ => rfl) fun _ => UtKA.pure _ + +/-- `init_arch_objects` is cache maintenance only, for every type. -/ +theorem ut_initArch_kA (t : ApiObjectType) (dev : Bool) (ptr : ObjRef) (n sz : Nat) (refs : List ObjRef) : + UtKA (initArchObjects (Z := Unit) t dev ptr n sz refs) := by + unfold initArchObjects + split_ifs + · exact ut_mapMx_kA (fun _ => ut_doMachineOp_kA _) _ + · exact ut_mapMx_kA (fun _ => ut_doMachineOp_kA _) _ + · exact UtKA.pure _ + +/-- FL4's `vcpu::init` step rewrites VCPU objects only. -/ +theorem ut_vcpuInit_kA (refs : List ObjRef) : + UtKA (NonDet.mapMx (fun v => (do + let vcpu ← getVcpu (Z := Unit) v + setVcpu v (fl4VcpuInit vcpu) : NonDet (State Unit) Unit)) refs) := by + refine ut_mapMx_kA (fun v σ r hr => ?_) _ + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + have hv : x.2 = σ ∧ ∃ w, σ.kheap v = some (.archObj (.vcpu w)) := by + change x ∈ ((NonDet.gets (vcpusOf (Z := Unit)) >>= fun m => NonDet.assertOpt (m v)) σ).1 at hx + obtain ⟨y, hy, hx⟩ := mem_bindA.1 hx + obtain rfl : y = (vcpusOf σ, σ) := hy + simp only at hx + cases h : vcpusOf σ v with + | none => rw [h] at hx; exact absurd hx (Set.notMem_empty _) + | some w => + rw [h] at hx + obtain rfl : x = (w, σ) := hx + refine ⟨rfl, w, ?_⟩ + cases hk : σ.kheap v with + | none => simp [vcpusOf, aobjsOf, optMap, hk] at h + | some o => + cases o <;> simp [vcpusOf, aobjsOf, optMap, aobjOf, hk] at h + rename_i ao + cases ao <;> simp [vcpuOf] at h + rw [h] + obtain ⟨e, w, hw⟩ := hv + obtain ⟨o0, -, h2⟩ := setObject_mem hr + rw [h2, e] + intro q + by_cases hq : q = v + · subst hq + refine Or.inr ⟨(by rw [hw]; rintro (⟨_, _, h⟩ | ⟨_, _, h⟩) <;> cases h), ?_⟩ + simp only [Function.update_self] + rintro (⟨_, _, h⟩ | ⟨_, _, h⟩) <;> cases h + · exact Or.inl (Function.update_of_ne hq ..) + +/-- Every outcome of `retype_region`. -/ +theorem utRetRegion_invA (t : ApiObjectType) (ptr : ObjRef) (n bits : Nat) (dev : Bool) (σ : State Unit) + {r : List ObjRef × State Unit} (hr : r ∈ (retypeRegionWith (Z := Unit) fl4ObjBitsApi ptr n bits t dev σ).1) : + r.1 = fl4RetypeAddrs fl4ObjBitsApi ptr n bits t ∧ + r.2 = if t = .untyped then σ else utRetStA t dev bits (fl4RetypeAddrs fl4ObjBitsApi ptr n bits t) σ := by + unfold retypeRegionWith at hr + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + obtain rfl : x = (2 ^ fl4ObjBitsApi t bits, σ) := hx + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + obtain rfl : x = (fl4RetypeAddrs fl4ObjBitsApi ptr n bits t, σ) := hx + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + obtain rfl : r = (fl4RetypeAddrs fl4ObjBitsApi ptr n bits t, x.2) := hr + refine ⟨rfl, ?_⟩ + show x.2 = _ + by_cases h : t = .untyped + · rw [ite_eq_left h] + rw [show NonDet.when (t ≠ .untyped) _ = (NonDet.pure () : NonDet (State Unit) Unit) from + ite_eq_right (fun g => g h)] at hx + obtain rfl : x = ((), σ) := hx + rfl + · rw [ite_eq_right h] + unfold NonDet.when at hx + rw [ite_eq_left (show t ≠ .untyped from h)] at hx + obtain ⟨y, hy, hx⟩ := mem_bindA.1 hx + obtain rfl : y = (σ.kheap, σ) := hy + obtain ⟨y, hy, hx⟩ := mem_bindA.1 hx + obtain rfl : y = (σ.curDomain, σ) := hy + obtain ⟨y, hy, hx⟩ := mem_bindA.1 hx + obtain rfl : y = ((fl4RetypeAddrs fl4ObjBitsApi ptr n bits t).foldr + (fun p kh => Function.update kh p (some (defaultObject t dev bits σ.curDomain))) σ.kheap, σ) := hy + obtain rfl : x = ((), utRetStA t dev bits (fl4RetypeAddrs fl4ObjBitsApi ptr n bits t) σ) := hx + rfl + +/-- The body of FL4's `invoke_untyped` after its reset. -/ +def utRtBodyA (srcSlot : CslotPtr) (base retypeBase : Word) (newType : ApiObjectType) (objSz : Nat) + (slots : List CslotPtr) (isDevice : Bool) : NonDet (State Unit) Unit := do + let cap ← getCap (Z := Unit) srcSlot + let totalObjectSize ← Pure.pure (BitVec.ofNat 64 slots.length <<< fl4ObjBitsApi newType objSz) + let freeRef ← Pure.pure (retypeBase + totalObjectSize) + setCap (.untypedCap isDevice base (bitsOf cap) (unat (freeRef - base))) srcSlot + let orefs ← retypeRegionWith fl4ObjBitsApi retypeBase slots.length objSz newType isDevice + initArchObjects newType isDevice retypeBase slots.length objSz orefs + NonDet.mapMx (createCap newType objSz srcSlot isDevice) (slots.zip orefs) + +theorem utInvoke_eqA (srcSlot : CslotPtr) (reset : Bool) (base retypeBase : Word) (newType : ApiObjectType) + (objSz : Nat) (slots : List CslotPtr) (isDevice : Bool) : + fl4InvokeUntyped (Z := Unit) (.retype srcSlot reset base retypeBase newType objSz slots isDevice) = + (do + NonDetE.whenE reset (resetUntypedCap srcSlot) + NonDetE.liftE (utRtBodyA srcSlot base retypeBase newType objSz slots isDevice)) := rfl + +/-- **Every outcome of the body**: leaf-keeping, `retype_region`, leaf-keeping. -/ +theorem utRtBody_leafA {srcSlot : CslotPtr} {base retypeBase : Word} {t : ApiObjectType} (ht : t ≠ .untyped) + {objSz : Nat} {slots : List CslotPtr} {dev : Bool} {σ : State Unit} {r : Unit × State Unit} + (hr : r ∈ (utRtBodyA srcSlot base retypeBase t objSz slots dev σ).1) : + ∃ σ1, UtLeafKeepA σ σ1 ∧ + UtLeafKeepA (utRetStA t dev objSz (fl4RetypeAddrs fl4ObjBitsApi retypeBase slots.length objSz t) σ1) r.2 := by + unfold utRtBodyA at hr + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + have kget := ut_getCap_kA srcSlot σ x hx + obtain ⟨cap, σ0⟩ := x + simp only at kget hr + obtain ⟨y, hy, hr⟩ := mem_bindA.1 hr + obtain rfl : y = (BitVec.ofNat 64 slots.length <<< fl4ObjBitsApi t objSz, σ0) := hy + obtain ⟨y, hy, hr⟩ := mem_bindA.1 hr + obtain rfl : y = (retypeBase + BitVec.ofNat 64 slots.length <<< fl4ObjBitsApi t objSz, σ0) := hy + obtain ⟨y, hy, hr⟩ := mem_bindA.1 hr + have k1 := ut_setCap_kA _ _ σ0 y hy + obtain ⟨z, hz, hr⟩ := mem_bindA.1 hr + obtain ⟨-, e2⟩ := utRetRegion_invA _ _ _ _ _ _ hz + rw [ite_eq_right ht] at e2 + refine ⟨y.2, kget.trans k1, ?_⟩ + rw [← e2] + refine (UtKA.bind (ut_initArch_kA _ _ _ _ _ _) fun _ => ?_) z.2 r hr + exact ut_mapMx_kA (fun x => ut_createCap_kA _ _ _ _ x) _ + +/-- An untyped retype keeps every leaf. -/ +theorem utRtBody_leaf0A {srcSlot : CslotPtr} {base retypeBase : Word} {t : ApiObjectType} (ht : t = .untyped) + {objSz : Nat} {slots : List CslotPtr} {dev : Bool} {σ : State Unit} {r : Unit × State Unit} + (hr : r ∈ (utRtBodyA srcSlot base retypeBase t objSz slots dev σ).1) : UtLeafKeepA σ r.2 := by + unfold utRtBodyA at hr + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + have kget := ut_getCap_kA srcSlot σ x hx + obtain ⟨cap, σ0⟩ := x + simp only at kget hr + obtain ⟨y, hy, hr⟩ := mem_bindA.1 hr + obtain rfl : y = (BitVec.ofNat 64 slots.length <<< fl4ObjBitsApi t objSz, σ0) := hy + obtain ⟨y, hy, hr⟩ := mem_bindA.1 hr + obtain rfl : y = (retypeBase + BitVec.ofNat 64 slots.length <<< fl4ObjBitsApi t objSz, σ0) := hy + obtain ⟨y, hy, hr⟩ := mem_bindA.1 hr + have k1 := ut_setCap_kA _ _ σ0 y hy + obtain ⟨z, hz, hr⟩ := mem_bindA.1 hr + obtain ⟨-, e2⟩ := utRetRegion_invA _ _ _ _ _ _ hz + rw [ite_eq_left ht] at e2 + refine (kget.trans k1).trans ?_ + rw [← e2] + refine (UtKA.bind (ut_initArch_kA _ _ _ _ _ _) fun _ => ?_) z.2 r hr + exact ut_mapMx_kA (fun x => ut_createCap_kA _ _ _ _ x) _ + +/-- Leaves off the new addresses are kept by the body. -/ +theorem utRtBody_offA {srcSlot : CslotPtr} {base retypeBase : Word} {t : ApiObjectType} + {objSz : Nat} {slots : List CslotPtr} {dev : Bool} {σ : State Unit} {r : Unit × State Unit} + (hr : r ∈ (utRtBodyA srcSlot base retypeBase t objSz slots dev σ).1) {q : ObjRef} + (hq : q ∉ fl4RetypeAddrs fl4ObjBitsApi retypeBase slots.length objSz t) {o : KernelObject} + (hl : UtLeafA (some o)) : r.2.kheap q = some o ↔ σ.kheap q = some o := by + by_cases ht : t = .untyped + · exact (utRtBody_leaf0A ht hr).iff hl + obtain ⟨σ1, k1, k2⟩ := utRtBody_leafA ht hr + rw [k2.iff hl, utRetSt_kheapA, ite_eq_right hq, k1.iff hl] + +/-- **`scSize` after the body** (spec side by `invoke_untyped`'s outcome; heap side by `UtEnd`). -/ +theorem utInvoke_scSizeA {S : Type} {st : machine.Store S} [L : LawfulStore st] {H H1 H' : Heap} + {Cs OS OS1 : Set U64} {s0 s1 s' : S} {slot p sb : U64} {dev : Bool} {lo hi : Nat} + {aligned nt us dest off win : U64} + (U : UtEnd st H Cs OS s0 slot p sb dev H1 H' OS1 s1 s' lo hi aligned nt us dest off win) + {t : ApiObjectType} (htt : objTypeOfA nt.val = some t) (hus : us.val < 64) + {srcSlot : CslotPtr} {base : Word} {slots : List CslotPtr} (hlen : slots.length = win.val) {m : McState} + {σ : State Unit} {r : Unit × State Unit} + (hr : r ∈ (utRtBodyA srcSlot base (specPtr aligned) t us.val slots dev σ).1) + (h : ∀ c ∈ H1.scs, ∀ x n, σ.kheap (scNameA m c) = some (.schedContext x n) → + n + minSchedContextBits ≤ H1.scBits c ∧ + (NotIdleScA m (scNameA m c) → n + minSchedContextBits = H1.scBits c)) + (hfree : ∀ j, j < win.val → H1 (bjA aligned (zA nt.val us) j) = none) + (hanchor : ∀ j, j < win.val → specPtr (bjA aligned (zA nt.val us) j) ≠ idleScPtr) + {c0 : U64} (hid : m.idleScOf m.curCore = specPtr c0) (hc0 : c0 ∈ H1.scs) : + ∀ c ∈ H'.scs, ∀ x n, r.2.kheap (scNameA m c) = some (.schedContext x n) → + n + minSchedContextBits ≤ H'.scBits c ∧ + (NotIdleScA m (scNameA m c) → n + minSchedContextBits = H'.scBits c) := by + have hidle : ∀ j, j < win.val → specPtr (bjA aligned (zA nt.val us) j) ≠ m.idleScOf m.curCore := + fun j hj => utRet_idleScA hid hc0 (hfree j hj) + have leaf : ∀ (x : SchedContext) (n : Nat), UtLeafA (some (.schedContext x n)) := + fun x n => Or.inl ⟨x, n, rfl⟩ + by_cases ht : t = .untyped + · have n0 : nt.val = 0 := (objTypeOfA_some htt us.val).2.2.2.1.1 ht + intro c hc x n hx + rw [(utRtBody_leaf0A ht hr).iff (leaf x n)] at hx + obtain ⟨z, hz⟩ := hc + rcases U.ex c _ hz with old | ⟨-, j, -, -, hk⟩ + · have es : H'.scBits c = H1.scBits c := by unfold Heap.scBits; rw [old, hz] + rw [es]; exact h c ⟨z, old⟩ x n hx + · have := (newKind_sc hk).1 + exfalso; omega + obtain ⟨σ1, k1, k2⟩ := utRtBody_leafA ht hr + rw [hlen] at k2 + obtain ⟨hold, hnew⟩ := utRetSpec_scA htt hus (len := win.val) (H1 := H1) (m := m) σ1 dev hfree hanchor hidle + intro c hc x n hx + rw [k2.iff (leaf x n)] at hx + obtain ⟨z, hz⟩ := hc + rcases U.ex c _ hz with old | ⟨hd, j, hj, e, hk⟩ + · have es : H'.scBits c = H1.scBits c := by unfold Heap.scBits; rw [old, hz] + rw [hold c ⟨z, old⟩] at hx + rw [k1.iff (leaf x n)] at hx + rw [es]; exact h c ⟨z, old⟩ x n hx + · obtain ⟨h5, rfl⟩ := newKind_sc hk + rw [e] at hx hz ⊢ + rw [hnew h5 j hj] at hx + cases hx + have es : H'.scBits (bjA aligned (zA nt.val us) j) = us.val := by unfold Heap.scBits; rw [hz] + have := U.A.sc h5 + rw [es] + have : minSchedContextBits = 7 := rfl + exact ⟨by omega, fun _ => by omega⟩ + +/-- **`utEnd_devFrDisjA`'s spec premise** for the body: data pages off the untyped's free part are kept. -/ +theorem utInvoke_dataPageA {nt us aligned p sb fi1 : U64} {t : ApiObjectType} (htt : objTypeOfA nt.val = some t) + (hus : us.val < 64) {srcSlot : CslotPtr} {base : Word} {slots : List CslotPtr} + {dev : Bool} {σ : State Unit} {r : Unit × State Unit} + (hr : r ∈ (utRtBodyA srcSlot base (specPtr aligned) t us.val slots dev σ).1) + (hin : ∀ j, j < slots.length → p.val + fi1.val ≤ (bjA aligned (zA nt.val us) j).val ∧ + (bjA aligned (zA nt.val us) j).val < p.val + 2 ^ sb.val) : + ∀ q : U64, ¬ (p.val + fi1.val ≤ q.val ∧ q.val < p.val + 2 ^ sb.val) → + ∀ d pg, σ.kheap (specPtr q) = some (.archObj (.dataPage d pg)) → + r.2.kheap (specPtr q) = some (.archObj (.dataPage d pg)) := by + intro q hq d pg h + refine (utRtBody_offA hr (fun m => ?_) (Or.inr ⟨d, pg, rfl⟩)).2 h + rw [utRet_addrs_memA htt hus] at m + obtain ⟨j, hj, e⟩ := m + exact hq (ut_specPtr_injA e ▸ hin j hj) + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtRtJ7A.lean b/proofs/Fl4ProofsA64/P3/UtRtJ7A.lean new file mode 100644 index 000000000..6c100adc4 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtRtJ7A.lean @@ -0,0 +1,152 @@ +/- +P3 (aarch64, A64-P3 Untyped, J7 widening; Architect 03:24Z, lead 04:33Z): **Retype's new page-table pages for J7's +`SchedPageA ⊆ Pt`**. + +* `UtSchedNewA`: the pages `SchedPageA` newly names after a Retype: a new Stage-1 table's or VSpace root's page, + a new Stage-2 root's two pages, a new `HighVSpace`'s private L1 page (`+ 4096`), and a new `HighVSpace`'s root page + when it joins `Z.Y`. +* **`utRt_schedPageA`**: every page `SchedPageA` names after the creation was named before, or is in `UtSchedNewA`. + So J7's inclusion holds at `Pt ∪ UtSchedNewA`. +* **`utSchedNew_inWinA`**: those pages lie inside the creation window, in the untyped's free part; + **`utSchedNew_thOffA`**: hence off every object of the old typed heap. + +Hypotheses: how the new VSpace ghosts split (`hsets`: a member is old or a new object's page), and the cap split of +`UtEnd` (`utEnd_capsA`). `VrOK` and `S1aR2` at `Pt ∪ UtSchedNewA` are A64 P1-S1A's and are not proved here. No named +premise. +-/ +import Fl4ProofsA64.P3.UtRtEndA +import Fl4ProofsA64.P3.Stage3J7A +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-- The pages a Retype of type `nt` adds to what `SchedPageA` names (module docstring). -/ +def UtSchedNewA (nt us aligned : U64) (win : Nat) : Set U64 := + {p | ∃ j < win, + ((nt.val = 8 ∨ nt.val = 11) ∧ p = bjA aligned (zA nt.val us) j) ∨ + (nt.val = 13 ∧ (p = bjA aligned (zA nt.val us) j ∨ p = wa (bjA aligned (zA nt.val us) j) 4096#u64)) ∨ + (nt.val = 14 ∧ (p = bjA aligned (zA nt.val us) j ∨ p = wa (bjA aligned (zA nt.val us) j) 4096#u64))} + +theorem utNewCap_schedA {n : Nat} (hn : n < 15) (base us : U64) (dev : Bool) : + (∀ q m a v, newCapA n base us dev = .PageTable q m a v → n = 11 ∧ q = base) ∧ + (∀ q m a, newCapA n base us dev = .Pml4 q m a → n = 8 ∧ q = base) ∧ + (∀ q l m a v, newCapA n base us dev = .Ept q l m a v → n = 13 ∧ q = base) ∧ + (∀ q m a, newCapA n base us dev = .HighVSpace q m a → n = 14 ∧ q = base) := by + refine ⟨fun q m a v h => ?_, fun q m a h => ?_, fun q l m a v h => ?_, fun q m a h => ?_⟩ <;> + (interval_cases n <;> simp [newCapA] at h; exact ⟨rfl, h.1.symm⟩) + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **J7's inclusion across a Retype** (module docstring). -/ +theorem utRt_schedPageA {Z Z' : XSetsA} {OS OS' : Set U64} {s s' : S} {nt us aligned : U64} {win : Nat} {dev : Bool} + (hn : nt.val < 15) + (hsets : (∀ p ∈ Z'.Pp, p ∈ Z.Pp) ∧ (∀ p ∈ Z'.S1, p ∈ Z.S1 ∨ p ∈ UtSchedNewA nt us aligned win) ∧ + (∀ p ∈ Z'.T2, p ∈ Z.T2 ∨ p ∈ UtSchedNewA nt us aligned win) ∧ + (∀ r ∈ Z'.X, r ∈ Z.X ∨ (r ∈ UtSchedNewA nt us aligned win ∧ + wa r 4096#u64 ∈ UtSchedNewA nt us aligned win)) ∧ + (∀ y ∈ Z'.Y, y ∈ Z.Y ∨ (y ∈ UtSchedNewA nt us aligned win ∧ + wa y 4096#u64 ∈ UtSchedNewA nt us aligned win))) + (hcaps : ∀ u ∈ OS', (∃ q sb i d, capA st s' u = .Untyped q sb i d) ∨ + (u ∈ OS ∧ capA st s' u = capA st s u) ∨ + ∃ j < win, capA st s' u = newCapA nt.val (bjA aligned (zA nt.val us) j) us dev) : + ∀ p, SchedPageA st Z' OS' s' p → SchedPageA st Z OS s p ∨ p ∈ UtSchedNewA nt us aligned win := by + obtain ⟨hPp, hS1, hT2, hX, hY⟩ := hsets + intro p hp + unfold SchedPageA at hp ⊢ + rcases hp with m | m | m | m | ⟨r, m, e⟩ | m | ⟨y, m, e⟩ | ⟨u, hu, h⟩ + · exact Or.inl (Or.inl (hPp p m)) + · rcases hS1 p m with a | a + · exact Or.inl (Or.inr (Or.inl a)) + · exact Or.inr a + · rcases hT2 p m with a | a + · exact Or.inl (Or.inr (Or.inr (Or.inl a))) + · exact Or.inr a + · rcases hX p m with a | ⟨a, -⟩ + · exact Or.inl (Or.inr (Or.inr (Or.inr (Or.inl a)))) + · exact Or.inr a + · rcases hX r m with a | ⟨-, a⟩ + · exact Or.inl (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨r, a, e⟩))))) + · exact Or.inr (e ▸ a) + · rcases hY p m with a | ⟨a, -⟩ + · exact Or.inl (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl a)))))) + · exact Or.inr a + · rcases hY y m with a | ⟨-, a⟩ + · exact Or.inl (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨y, a, e⟩))))))) + · exact Or.inr (e ▸ a) + rcases hcaps u hu with ⟨q, sb, i, d, hc⟩ | ⟨huO, hc⟩ | ⟨j, hj, hc⟩ + · rcases h with ⟨_, _, _, h⟩ | ⟨_, _, h⟩ | ⟨_, _, h⟩ | ⟨_, _, _, h, -⟩ | ⟨_, _, h, -⟩ <;> rw [hc] at h <;> cases h + · rw [hc] at h + exact Or.inl (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ⟨u, huO, h⟩))))))) + · rw [hc] at h + obtain ⟨c1, c2, c3, c4⟩ := utNewCap_schedA hn (bjA aligned (zA nt.val us) j) us dev + refine Or.inr ⟨j, hj, ?_⟩ + rcases h with ⟨_, _, _, h⟩ | ⟨_, _, h⟩ | ⟨_, _, h⟩ | ⟨q, _, _, h, e⟩ | ⟨y, _, h, e⟩ + · obtain ⟨a, b⟩ := c1 _ _ _ _ h + exact Or.inl ⟨Or.inr a, b⟩ + · obtain ⟨a, b⟩ := c2 _ _ _ h + exact Or.inl ⟨Or.inl a, b⟩ + · obtain ⟨a, b⟩ := c3 _ _ _ _ _ h + exact Or.inr (Or.inl ⟨a, Or.inl b⟩) + · obtain ⟨a, b⟩ := c3 _ _ _ _ _ h + exact Or.inr (Or.inl ⟨a, Or.inr (b ▸ e)⟩) + · obtain ⟨a, b⟩ := c4 _ _ _ h + exact Or.inr (Or.inr ⟨a, Or.inr (b ▸ e)⟩) + +end + +/-- **The new pages lie inside the creation window.** -/ +theorem utSchedNew_inWinA {nt us aligned : U64} {win : Nat} + (hfit : aligned.val + win * 2 ^ (zA nt.val us).val ≤ 2 ^ 64) : + ∀ p ∈ UtSchedNewA nt us aligned win, + aligned.val ≤ p.val ∧ p.val + 4096 ≤ aligned.val + win * 2 ^ (zA nt.val us).val := by + rintro p ⟨j, hj, h⟩ + have h1 : (j + 1) * 2 ^ (zA nt.val us).val ≤ win * 2 ^ (zA nt.val us).val := Nat.mul_le_mul_right _ hj + rw [Nat.add_mul, Nat.one_mul] at h1 + have hz : 0 < 2 ^ (zA nt.val us).val := Nat.two_pow_pos _ + have hb : (bjA aligned (zA nt.val us) j).val = aligned.val + j * 2 ^ (zA nt.val us).val := + bjA_val (by omega) + -- a page at `base + off`, inside the object + have at4 : ∀ off : Nat, off + 4096 ≤ 2 ^ (zA nt.val us).val → + p.val = (bjA aligned (zA nt.val us) j).val + off → + aligned.val ≤ p.val ∧ p.val + 4096 ≤ aligned.val + win * 2 ^ (zA nt.val us).val := fun off ho hp => by + omega + have sz : ∀ n, nt.val = n → (n = 8 ∨ n = 11) → 2 ^ (zA nt.val us).val = 4096 := fun n e h => by + rcases h with rfl | rfl <;> rw [e] <;> simp [zA] + have sz2 : (nt.val = 13 ∨ nt.val = 14) → 2 ^ (zA nt.val us).val = 8192 := fun h => by + rcases h with e | e <;> rw [e] <;> simp [zA] + have hi : (nt.val = 13 ∨ nt.val = 14) → p = wa (bjA aligned (zA nt.val us) j) 4096#u64 → + aligned.val ≤ p.val ∧ p.val + 4096 ≤ aligned.val + win * 2 ^ (zA nt.val us).val := fun h e => by + have s := sz2 h + refine at4 4096 (by omega) ?_ + have e4 : (4096#u64 : U64).val = 4096 := rfl + rw [e, wa_valP, e4] + exact Nat.mod_eq_of_lt (by omega) + rcases h with ⟨h, e⟩ | ⟨h, e | e⟩ | ⟨h, e | e⟩ + · exact at4 0 (by rw [sz _ rfl h]) (by rw [e]; rfl) + · exact at4 0 (by rw [sz2 (Or.inl h)]; omega) (by rw [e]; rfl) + · exact hi (Or.inl h) e + · exact at4 0 (by rw [sz2 (Or.inr h)]; omega) (by rw [e]; rfl) + · exact hi (Or.inr h) e + +/-- **The new pages are off the old typed heap's objects**: with the window off them (`utEnd_thDisjA`). -/ +theorem utSchedNew_thOffA {th : TypedHeapA} {nt us aligned : U64} {win : Nat} + (hfit : aligned.val + win * 2 ^ (zA nt.val us).val ≤ 2 ^ 64) + (hdisj : ∀ q k, th q = some k → + q.val + k.size ≤ aligned.val ∨ aligned.val + win * 2 ^ (zA nt.val us).val ≤ q.val) : + ∀ p ∈ UtSchedNewA nt us aligned win, ∀ q k, th q = some k → + p.val + 4096 ≤ q.val ∨ q.val + k.size ≤ p.val := by + intro p hp q k hq + obtain ⟨a, b⟩ := utSchedNew_inWinA hfit p hp + rcases hdisj q k hq with g | g + · right; omega + · left; omega + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtRtKeptA.lean b/proofs/Fl4ProofsA64/P3/UtRtKeptA.lean new file mode 100644 index 000000000..3026941da --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtRtKeptA.lean @@ -0,0 +1,277 @@ +/- +P3 (aarch64, A64-P3 Untyped W5): **a creation keeps the relation's words outside the slots**. + +No word of `FootNA` is one a RAM creation writes (`UtCrtWA`, `utRetype_frameA`): not a slot word of `OS`; not in the +new window, which is in the untyped's free part; not the debug-list head; not a TCB's debug link. + +* `utRt_footOffA`, `utRt_footKeptA`. + +Hypotheses: the record at the creation's start and the window in the invoked untyped's free part. No named premise. +New theorems only. +-/ +import Fl4ProofsA64.P3.UtRtFrameA +import Fl4ProofsA64.P3.UtClrA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s : S} {a : A} {σ : State Unit} {m : McState} {x p sb fi : U64} {dev : Bool} + +/-- **A creation window in the free part is clear of the record's objects, nodes and state block.** -/ +theorem utRt_winClrA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (hx : x ∈ OS) (hc : capA st s x = .Untyped p sb fi false) {lo hi : Nat} (h1 : p.val + fi.val ≤ lo) + (h2 : hi ≤ p.val + 2 ^ sb.val) : UtClrA st Z H s lo hi := by + obtain ⟨hnodes, hks⟩ := J.sys.sysl.sys.stz.2.2 x hx p sb fi hc + refine ⟨fun q bytes h => ?_, fun c hc' => ?_, ?_⟩ + · rcases J.objAlloc q bytes h x hx p sb fi false hc with g | g | g <;> omega + · rcases hnodes c hc' with g | g <;> omega + · rcases hks with g | g <;> omega + +/-- **No relation word outside the slots is written by a creation** (module docstring). -/ +theorem utRt_footOffA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (hx : x ∈ OS) (hc : capA st s x = .Untyped p sb fi false) {nt base z : U64} {len : Nat} + (h1 : p.val + fi.val ≤ base.val) (h2 : base.val + len * 2 ^ z.val ≤ p.val + 2 ^ sb.val) + {b : U64} (hb : FootNA (L := L) Z s b) : ¬ UtCrtWA st H OS s nt base z len b := by + have C := utRt_winClrA J hx hc h1 h2 + have hH : HeapOK H := J.sys.ent.heap + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.sys.ent.ksw + have h0 : (0#u64 : U64) ∈ Z.Cs := by + obtain ⟨k, hk, -, h⟩ := J.j.1.csN + exact (h 0#u64).2 (by simpa using hk) + have n0 := J.sq.nodes 0#u64 h0 + have d88 : layout.node.DEBUG_TCBS.val < 4288 := by rw [debug_val]; omega + have hv0 : (nwA st s).val = (L.ksb s).val + 223576 := by + rw [nodeAt_val n0 d88, debug_val] + unfold LawfulStore.nodeBase + simp + -- a word of a heap object is in no node + have inH : ∀ q k, H q = some k → ∀ w : U64, q.val ≤ w.val → w.val < q.val + 2 ^ k.bits → + w.val < (L.ksb s).val + 223488 ∨ (L.ksb s).val + 772352 ≤ w.val := by + intro q k hq w g1 g2 + by_cases ho : ∃ y, k = .other y + · obtain ⟨y, rfl⟩ := ho + simp only [HKind.bits] at g2 + rcases J.ko.2 q y hq with g | g | g | g <;> omega + · rcases J.ko.1 q k hq (fun y e => ho ⟨y, e⟩) with g | g <;> omega + have hdpv : ∀ t ∈ H.tcbs, (dpA t).val = t.val + 3584 := by + intro t ht + have := hH.fits t .tcb ht + simp only [HKind.bits] at this + unfold dpA + rw [wa_valP, show (3584#u64 : U64).val = 3584 from rfl] + exact Nat.mod_eq_of_lt (by omega) + -- a word off the node area is not the debug head + have offN : ∀ w : U64, w.val < (L.ksb s).val + 223488 ∨ (L.ksb s).val + 772352 ≤ w.val → w ≠ nwA st s := by + intro w g e + rw [e, hv0] at g + omega + -- a node word other than the debug head is no debug head and no TCB word + have node : ∀ c ∈ Z.Cs, ∀ o : U64, o.val < 4288 → o.val ≠ 88 → + L.nodeAt s c o ≠ nwA st s ∧ ∀ t ∈ H.tcbs, L.nodeAt s c o ≠ dpA t := by + intro c hcm o ho ne + have nc := J.sq.nodes c hcm + have hv : (L.nodeAt s c o).val = (L.ksb s).val + 223488 + c.val * 4288 + o.val := by + rw [nodeAt_val nc ho] + rfl + refine ⟨fun e => ?_, fun t ht e => ?_⟩ + · have := congrArg UScalar.val e + rw [hv, hv0] at this + omega + · have ev := congrArg UScalar.val e + rw [hv, hdpv t ht] at ev + have d := nc.disj t ht + unfold LawfulStore.nodeBase at d + omega + -- a TCB field other than the debug link + have tcbF : ∀ t ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val ≠ 3584 → + wa t K ≠ nwA st s ∧ ∀ u ∈ H.tcbs, wa t K ≠ dpA u := by + intro t ht K h4 ne + have htH : H t = some .tcb := by + have h : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + exact h + have f := hH.fits t _ htH + simp only [HKind.bits] at f + have ev : (wa t K).val = t.val + K.val := by + rw [wa_valP]; exact Nat.mod_eq_of_lt (by omega) + refine ⟨offN _ (inH t _ htH _ (by omega) (by simp only [HKind.bits]; omega)), fun u hu e => ?_⟩ + have g := congrArg UScalar.val e + rw [ev, hdpv u hu] at g + by_cases eu : t = u + · subst eu; omega + · have huH : H u = some .tcb := hu + have := hH.disj t _ u _ htH huH eu + simp only [HKind.bits] at this + omega + -- a word of a non-TCB heap object + have obj : ∀ q k, H q = some k → k ≠ .tcb → ∀ w : U64, q.val ≤ w.val → w.val < q.val + 2 ^ k.bits → + w ≠ nwA st s ∧ ∀ u ∈ H.tcbs, w ≠ dpA u := by + intro q k hq ne w g1 g2 + refine ⟨offN _ (inH q k hq w g1 g2), fun u hu e => ?_⟩ + have huH : H u = some .tcb := hu + have g := congrArg UScalar.val e + rw [hdpv u hu] at g + have := hH.disj q k u .tcb hq huH (fun e' => ne (by rw [e', huH] at hq; exact (Option.some.inj hq).symm)) + rw [show HKind.tcb.bits = 12 from rfl] at this + omega + -- a state-block word below the nodes or above them + have kb : ∀ w : U64, (L.ksb s).val ≤ w.val → w.val < (L.ksb s).val + 788736 → + (w.val < (L.ksb s).val + 223488 ∨ (L.ksb s).val + 772352 ≤ w.val) → + w ≠ nwA st s ∧ ∀ u ∈ H.tcbs, w ≠ dpA u := by + intro w g1 g2 g3 + refine ⟨offN _ g3, fun u hu e => ?_⟩ + have huH : H u = some .tcb := hu + have g := congrArg UScalar.val e + rw [hdpv u hu] at g + have d := J.ko.1 u .tcb huH (fun y e => by cases e) + rw [show HKind.tcb.bits = 12 from rfl] at d + omega + -- a word of a 4 KiB relation page + have page : ∀ q ∈ Z.Pp ∪ Z.S1 ∪ Z.T2 ∪ Z.V, ∀ w : U64, q.val ≤ w.val → w.val < q.val + 4096 → + w ≠ nwA st s ∧ ∀ u ∈ H.tcbs, w ≠ dpA u := by + intro q hq w g1 g2 + obtain ⟨o, y, ho, k1, k2⟩ := J.setsInH.pages q hq + exact obj o _ ho (fun e => by cases e) w (by omega) (by simp only [HKind.bits]; omega) + have root : ∀ q ∈ Z.X ∪ Z.Y, ∀ w : U64, q.val ≤ w.val → w.val < q.val + 8192 → + w ≠ nwA st s ∧ ∀ u ∈ H.tcbs, w ≠ dpA u := by + intro q hq w g1 g2 + obtain ⟨o, y, ho, k1, k2⟩ := J.setsInH.roots q hq + exact obj o _ ho (fun e => by cases e) w (by omega) (by simp only [HKind.bits]; omega) + have peP : ∀ q ∈ Z.Pp ∪ Z.S1 ∪ Z.T2 ∪ Z.V, ∀ i : U64, i.val < 512 → + peAF q i ≠ nwA st s ∧ ∀ u ∈ H.tcbs, peAF q i ≠ dpA u := by + intro q hq i hi + obtain ⟨o, y, ho, -, k2⟩ := J.setsInH.pages q hq + have f := hH.fits o _ ho + have e := peAF_valA (p := q) (lo := i) (by simp only [HKind.bits] at f; omega) hi + exact page q hq _ (by omega) (by omega) + have rootW : ∀ q ∈ Z.X ∪ Z.Y, ∀ i : U64, i.val < 512 → + (peAF q i ≠ nwA st s ∧ ∀ u ∈ H.tcbs, peAF q i ≠ dpA u) ∧ + (peAF (wa q 4096#u64) i ≠ nwA st s ∧ ∀ u ∈ H.tcbs, peAF (wa q 4096#u64) i ≠ dpA u) := by + intro q hq i hi + obtain ⟨o, y, ho, -, k2⟩ := J.setsInH.roots q hq + have f := hH.fits o _ ho + simp only [HKind.bits] at f + have e1 : (wa q 4096#u64).val = q.val + 4096 := by + rw [wa_valP, show (4096#u64 : U64).val = 4096 from rfl] + exact Nat.mod_eq_of_lt (by omega) + have a0 := peAF_valA (p := q) (lo := i) (by omega) hi + have a1 := peAF_valA (p := wa q 4096#u64) (lo := i) (by omega) hi + exact ⟨root q hq _ (by omega) (by omega), root q hq _ (by omega) (by omega)⟩ + have ksW : ∀ (o : U64), o.val < 788736 → (o.val < 223488 ∨ 772352 ≤ o.val) → + wa (L.ksb s) o ≠ nwA st s ∧ ∀ u ∈ H.tcbs, wa (L.ksb s) o ≠ dpA u := by + intro o h1 h2 + have e : (wa (L.ksb s) o).val = (L.ksb s).val + o.val := by + rw [wa_valP]; exact Nat.mod_eq_of_lt (by omega) + exact kb _ (by omega) (by omega) (by omega) + have scW : ∀ c ∈ Z.C, ∀ K : U64, K.val < 128 → wa c K ≠ nwA st s ∧ ∀ u ∈ H.tcbs, wa c K ≠ dpA u := by + intro c hcm K hK + have h : c ∈ H.scs := by rw [J.scs]; exact hcm + obtain ⟨y, hy⟩ := h + have f := hH.fits c _ hy + have g := hH.scmin c y hy + have : 2 ^ 7 ≤ 2 ^ y := Nat.pow_le_pow_right (by decide) g + simp only [HKind.bits] at f + have e : (wa c K).val = c.val + K.val := by + rw [wa_valP]; exact Nat.mod_eq_of_lt (by omega) + exact obj c _ hy (fun e => by cases e) _ (by omega) (by simp only [HKind.bits]; omega) + have hbk := hb + have debug : b ≠ nwA st s ∧ ∀ u ∈ H.tcbs, b ≠ dpA u := by + cases hb with + | node c w hcm hw => + rcases hw with e | e | e | e <;> rw [e] <;> + exact node c hcm _ (by simp [layout.node.CUR_THREAD, layout.node.IDLE_THREAD, layout.node.CUR_SC, + layout.node.IDLE_SC]) (by simp [layout.node.CUR_THREAD, layout.node.IDLE_THREAD, layout.node.CUR_SC, + layout.node.IDLE_SC]) + | cur w hw => + rcases hw with e | e <;> rw [e] <;> + exact node _ J.j.1.sch.cpuCs _ (by simp [layout.node.SCHED_ACTION, layout.node.CUR_TIME]) + (by simp [layout.node.SCHED_ACTION, layout.node.CUR_TIME]) + | tcb t w ht hw => + rcases hw with e | e | e | e | e <;> rw [e] + · rw [stA_eq]; exact tcbF t ht _ (by simp) (by simp) + · exact tcbF t ht 3464#u64 (by simp) (by simp) + · exact tcbF t ht 3520#u64 (by simp) (by simp) + · exact tcbF t ht 3664#u64 (by simp) (by simp) + · exact tcbF t ht 3672#u64 (by simp) (by simp) + | sc c hcm => exact scW c hcm 24#u64 (by simp) + | vcpu v w hv hw => + have hv' : v ∈ Z.Pp ∪ Z.S1 ∪ Z.T2 ∪ Z.V := Or.inr hv + obtain ⟨o, y, ho, -, k2⟩ := J.setsInH.pages v hv' + have f := hH.fits o _ ho + simp only [HKind.bits] at f + rcases hw with e | e <;> rw [e] + · exact page v hv' _ (le_refl _) (by omega) + · have e24 : (wa v 24#u64).val = v.val + 24 := by + rw [wa_valP, show (24#u64 : U64).val = 24 from rfl] + exact Nat.mod_eq_of_lt (by omega) + exact page v hv' _ (by omega) (by omega) + | irq i hi => + have e := irqWordA_val (kb := L.ksb s) (i := i) J.j.1.ksFit hi + exact kb _ (by omega) (by omega) (by omega) + | cur2 w hw => + rcases hw with e | e <;> rw [e] <;> + exact node _ J.j.1.sch.cpuCs _ (by simp [layout.node.CONSUMED, layout.node.REPROGRAM]) + (by simp [layout.node.CONSUMED, layout.node.REPROGRAM]) + | glob w hw => + rcases hw with e | e <;> rw [e] <;> exact ksW _ (by simp) (by simp) + | tcb2 t w ht hw => + rcases hw with e | e <;> rw [e] + · exact tcbF t ht 3448#u64 (by simp) (by simp) + · exact tcbF t ht 3432#u64 (by simp) (by simp) + | sc2 c hcm => exact scW c hcm 72#u64 (by simp) + | vs w hw => + cases hw with + | asid hi h => + unfold atAF + have e8 : (8#u64 : U64).val = 8 := rfl + have e7 : (772352#u64 : U64).val = 772352 := rfl + have em : (wa 772352#u64 (core.num.U64.wrapping_mul hi 8#u64)).val = 772352 + 8 * hi.val := by + rw [wa_valP, e7, wm8_valA (by omega)] + exact Nat.mod_eq_of_lt (by omega) + have e : (wa (L.ksb s) (wa 772352#u64 (core.num.U64.wrapping_mul hi 8#u64))).val = + (L.ksb s).val + (772352 + 8 * hi.val) := by + rw [wa_valP, em]; exact Nat.mod_eq_of_lt (by omega) + exact kb _ (by omega) (by omega) (by omega) + | pool q lo hq h => exact peP q (Or.inl (Or.inl (Or.inl hq))) lo h + | pt q i hq h => exact peP q (Or.inl (Or.inl (Or.inr hq))) i h + | high y i hy h => exact (rootW y (Or.inr hy) i h).2 + | ext w hw => + cases hw with + | s2lo r i hr h => exact (rootW r (Or.inl hr) i h).1 + | s2hi r i hr h => exact (rootW r (Or.inl hr) i h).2 + | s2pt q i hq h => exact peP q (Or.inl (Or.inr hq)) i h + | wl0 y hy => exact (rootW y (Or.inr hy) 511#u64 (by simp)).1 + | wlo y i hy h => exact (rootW y (Or.inr hy) i (by omega)).1 + intro h + rcases h with h | ⟨-, g1, g2⟩ | h | ⟨u, hu, h⟩ + · exact footN_offA J.toJEntA b hbk (Or.inr ⟨h, id⟩) + · exact ut_foot_regionA J C (.nslot b hbk) ⟨g1, g2⟩ + · exact debug.1 h + · exact debug.2 u hu h + +/-- **A creation keeps every relation word outside the slots.** -/ +theorem utRt_footKeptA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (hx : x ∈ OS) (hc : capA st s x = .Untyped p sb fi false) {nt base z : U64} {len : Nat} + (h1 : p.val + fi.val ≤ base.val) (h2 : base.val + len * 2 ^ z.val ≤ p.val + 2 ^ sb.val) + {s' : S} (w : WritesIn st s s' (UtCrtWA st H OS s nt base z len)) {b : U64} (hb : FootNA (L := L) Z s b) : + L.mem s' b = L.mem s b := + w.mem_eq (FootNA.aligned J.j.1 (bxi_nodes J.sys.ent.bxi) J.toJEntA.scAl J.ap.vcAl b hb) + (utRt_footOffA J hx hc h1 h2 hb) + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtRtPostA.lean b/proofs/Fl4ProofsA64/P3/UtRtPostA.lean new file mode 100644 index 000000000..f6984e70c --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtRtPostA.lean @@ -0,0 +1,476 @@ +/- +P3 (aarch64, A64-P3 Untyped W5): **`JAtZAX` after a RAM creation** — the contract's relation-level post, over the +typed heap, sets and view the creation grows. + +* `utRtZA`: the sets after the creation: `th` and the CDT ghost of the creation, the new TCBs `Tn`, SCs `Cn` and + VCPUs `Vn`, and the new bases in `S1` / `X` / `Y` by type. +* `utRt_relOffA`: no spec object the relation reads is at a new address. +* **`utRt_postA`**: `JAtZAX` at `utRtZA` and `utRtMA m Tn`, from the record at the creation's start, FL4's sharp + write frame, `StateRelA` / `CdtRelA` after the creation, and facts about the new objects (their words and spec + objects). It is general in the object type. + +Hypotheses of `utRt_postA` about the creation's outcome (supplied per type; `UtRtZeroPostA` for the zeroed types): +`hsub` (closure item A), `w`, `hrel`, `P'`, `har`, `hglob`, `hkeep`, the three `back`s, `hcap`, and the new objects' +facts. No named premise. New definitions and theorems only. +-/ +import Fl4ProofsA64.P3.UtRtKeptA +import Fl4ProofsA64.P3.UtRtVsA +import Fl4ProofsA64.P3.UtRtSchedA +import Fl4ProofsA64.P3.UtRtThA +import Fl4ProofsA64.P3.UtRtWitA +import Fl4ProofsA64.P3.UtRtSpecA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +set_option maxRecDepth 100000 + +/-- A new object's spec address. -/ +def UtNewSpA (aligned z : U64) (win : Nat) (q : Fl4SpecA64.ObjRef) : Prop := ∃ j < win, q = specPtr (bjA aligned z j) + +/-- **The sets after the creation** (module docstring). -/ +def utRtZA (Z : XSetsA) (G' : MdbGhostA) (nt us aligned : U64) (win : Nat) (Tn Cn Vn : Set U64) : XSetsA := + { Z with + th := utRtThA Z.th false nt us aligned win + G := G' + T := Z.T ∪ Tn + C := Z.C ∪ Cn + V := Z.V ∪ Vn + X := Z.X ∪ utXNewA nt aligned (zA nt.val us) win + Y := Z.Y ∪ utYNewA nt aligned (zA nt.val us) win + S1 := Z.S1 ∪ utS1NewA nt aligned (zA nt.val us) win + Lv := utLvNewA Z.Lv nt aligned (zA nt.val us) win } + +/-- What a slot of the new typed heap holds. -/ +def UtCapAfterA (th : TypedHeapA) (mem mem' : U64 → U64) (nt us aligned : U64) (win : Nat) (y : U64) : Prop := + (IsSlotA th y ∧ capAtA mem' y = capAtA mem y) ∨ + (∃ j < win, capAtA mem' y = newCapA nt.val (bjA aligned (zA nt.val us) j) us false) ∨ + (∃ a b c d, capAtA mem' y = .Untyped a b c d) ∨ capAtA mem' y = .Null + +private theorem ut_ite_eqA {α : Type} {c : Prop} [Decidable c] {x y z : α} (h : (if c then x else y) = z) : + (c ∧ x = z) ∨ (¬ c ∧ y = z) := by + by_cases g : c + · exact Or.inl ⟨g, (ite_eq_left g).symm.trans h⟩ + · exact Or.inr ⟨g, (ite_eq_right g).symm.trans h⟩ + +theorem utNewCap_pmlA {n : Nat} {b u p mp asid} {d : Bool} + (h : newCapA n b u d = .Pml4 p mp asid) : n = 8 ∧ p = b := by + unfold newCapA at h + repeat + rcases ut_ite_eqA h with ⟨e, h⟩ | ⟨-, h⟩ + · first + | (cases h; exact ⟨e, rfl⟩) + | cases h + cases h + +theorem utNewCap_vcpuA {n : Nat} {b u p} {d : Bool} + (h : newCapA n b u d = .Vcpu p) : n = 12 ∧ p = b := by + unfold newCapA at h + repeat + rcases ut_ite_eqA h with ⟨e, h⟩ | ⟨-, h⟩ + · first + | (cases h; exact ⟨e, rfl⟩) + | cases h + cases h + +theorem utNewCap_eptA {n : Nat} {b u p l mp asid va} {d : Bool} + (h : newCapA n b u d = .Ept p l mp asid va) : n = 13 ∧ p = b := by + unfold newCapA at h + repeat + rcases ut_ite_eqA h with ⟨e, h⟩ | ⟨-, h⟩ + · first + | (cases h; exact ⟨e, rfl⟩) + | cases h + cases h + +theorem utNewCap_highA {n : Nat} {b u p mp asid} {d : Bool} + (h : newCapA n b u d = .HighVSpace p mp asid) : n = 14 ∧ p = b := by + unfold newCapA at h + repeat + rcases ut_ite_eqA h with ⟨e, h⟩ | ⟨-, h⟩ + · first + | (cases h; exact ⟨e, rfl⟩) + | cases h + cases h + +/-- A new base lies in the window. -/ +theorem utRt_newValA {nt aligned z : U64} {win : Nat} (W : UtWinZA nt aligned z win) {j : Nat} (hj : j < win) : + aligned.val ≤ (bjA aligned z j).val ∧ (bjA aligned z j).val < aligned.val + win * 2 ^ z.val := by + have h1 : (j + 1) * 2 ^ z.val ≤ win * 2 ^ z.val := Nat.mul_le_mul_right _ hj + rw [Nat.add_mul, Nat.one_mul] at h1 + have hz : 0 < 2 ^ z.val := Nat.two_pow_pos _ + have := W.fit + have e : (bjA aligned z j).val = aligned.val + j * 2 ^ z.val := bjA_val (by omega) + omega + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s s' : S} {a a' : A} {σ σ' : State Unit} {m : McState} {x p sb fi : U64} + {nt us aligned : U64} {win : Nat} + +/-- No joint object of positive size is at a new address. -/ +theorem utRt_objOffA {lo hi : Nat} (C : UtClrA st Z H s lo hi) (W : UtWinZA nt aligned (zA nt.val us) win) + (e1 : lo = aligned.val) (e2 : hi = aligned.val + win * 2 ^ (zA nt.val us).val) {q : U64} {bytes : Nat} + (h : JointObjectA Z H q bytes) (hb : 0 < bytes) : ¬ UtNewSpA aligned (zA nt.val us) win (specPtr q) := by + rintro ⟨j, hj, e⟩ + have eq := ut_specPtr_injA e + have v := utRt_newValA W hj + rw [← eq] at v + rcases C.obj q bytes h with g | g <;> omega + +/-- **No spec object the relation reads is at a new address.** -/ +theorem utRt_relOffA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {lo hi : Nat} (C : UtClrA st Z H s lo hi) (W : UtWinZA nt aligned (zA nt.val us) win) + (e1 : lo = aligned.val) (e2 : hi = aligned.val + win * 2 ^ (zA nt.val us).val) + (anchor : ∀ j, j < win → specPtr (bjA aligned (zA nt.val us) j) ≠ Fl4SpecA64.idleScPtr) + {q : Fl4SpecA64.ObjRef} (h : RelPA Z m q) : ¬ UtNewSpA aligned (zA nt.val us) win q := by + have off : ∀ (b : U64) (bytes : Nat), JointObjectA Z H b bytes → 0 < bytes → + ¬ UtNewSpA aligned (zA nt.val us) win (specPtr b) := fun b bytes h hb => utRt_objOffA C W e1 e2 h hb + have thO : ∀ (b : U64) (k : ObjKindA), Z.th b = some k → ¬ UtNewSpA aligned (zA nt.val us) win (specPtr b) := by + intro b k hk + refine off b k.size (Or.inr (Or.inr (Or.inl ⟨k, hk, rfl⟩))) ?_ + cases k with + | cnode r => exact Nat.two_pow_pos _ + | tcb => exact Nat.succ_pos _ + | other z => exact J.thPos b z hk + rcases h with ⟨b, hb, rfl⟩ | ⟨t, ht, rfl⟩ | ⟨c, hc, rfl⟩ | ⟨v, hv, rfl⟩ | ⟨r, hr, rfl⟩ | ⟨r, hr, rfl⟩ | + ⟨b, hb, rfl⟩ | ⟨b, hb, rfl⟩ | ⟨b, hb, rfl⟩ + · obtain ⟨k, hk⟩ := Option.ne_none_iff_exists'.1 hb + exact thO b k hk + · exact thO t _ ((J.j.1.thT t).2 ht) + · unfold scRenA + split_ifs + · rintro ⟨j, hj, e⟩ + exact anchor j hj e.symm + · have hcs : c ∈ H.scs := by rw [J.scs]; exact hc + exact off c _ (JointObjectA.of_sc hcs) (Nat.two_pow_pos _) + · exact off v 4096 (Or.inl ⟨rfl, Or.inr (Or.inr (Or.inr (Or.inl hv)))⟩) (by omega) + · exact off r 8192 (Or.inr (Or.inl ⟨rfl, Or.inl hr⟩)) (by omega) + · exact off r 8192 (Or.inr (Or.inl ⟨rfl, Or.inr hr⟩)) (by omega) + · exact off b 4096 (Or.inl ⟨rfl, Or.inl hb⟩) (by omega) + · exact off b 4096 (Or.inl ⟨rfl, Or.inr (Or.inl hb)⟩) (by omega) + · exact off b 4096 (Or.inl ⟨rfl, Or.inr (Or.inr (Or.inl hb))⟩) (by omega) + +/-- **`JAtZAX` after a RAM creation** (module docstring). -/ +theorem utRt_postA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (hx : x ∈ OS) (hc : capA st s x = .Untyped p sb fi false) (W : UtWinZA nt aligned (zA nt.val us) win) + (h1 : p.val + fi.val ≤ aligned.val) (h2 : aligned.val + win * 2 ^ (zA nt.val us).val ≤ p.val + 2 ^ sb.val) + (hn : nt.val < 15) (hr4 : nt.val = 4 → us.val ≠ 0) + (w : WritesIn st s s' (UtCrtWA st H OS s nt aligned (zA nt.val us) win)) + {G' : MdbGhostA} (hS : StateRelA (utRtThA Z.th false nt us aligned win) (L.mem s') σ') + (hC : CdtRelA st (utRtThA Z.th false nt us aligned win) s' σ' G') + (P' : PlaceA (utRtThA Z.th false nt us aligned win) (L.ksb s)) (har : AR a' Z.hw) + (hglob : SpecGlobA σ σ') + (hkeep : ∀ q, ¬ UtNewSpA aligned (zA nt.val us) win q → (∀ sz cn, σ.kheap q ≠ some (.cnode sz cn)) → + σ'.kheap q = σ.kheap q) + {Tn Cn Vn : Set U64} (hTn : ∀ t, t ∈ Tn ↔ UtNewA false nt us aligned win t ∧ nt.val = 1) + (hCn : ∀ c ∈ Cn, c ∈ utBasesA aligned (zA nt.val us) win) + (hVn : ∀ v ∈ Vn, v ∈ utBasesA aligned (zA nt.val us) win) + (hVn12 : nt.val = 12 → ∀ v ∈ utBasesA aligned (zA nt.val us) win, v ∈ Vn) + (hcap : ∀ y, IsSlotA (utRtThA Z.th false nt us aligned win) y → + UtCapAfterA Z.th (L.mem s) (L.mem s') nt us aligned win y) + -- the new threads + (hnt : ∀ t ∈ Tn, (L.mem s' (affA t)).val = 0 ∧ L.mem s' (stA t) = 0#u64 ∧ L.mem s' (scA t) = 0#u64 ∧ + L.mem s' (domA t) = 0#u64 ∧ (L.mem s' (prioA t)).val = 0 ∧ L.mem s' (wa t 3664#u64) = 0#u64 ∧ + L.mem s' (runVcpuA t) = 0#u64 ∧ + ∃ y, σ'.kheap (specPtr t) = some (.tcb y) ∧ y.tcbState = .inactive ∧ y.tcbSchedContext = none ∧ + y.tcbDomain = 0 ∧ y.tcbPriority = 0 ∧ y.tcbArch.tcbVcpu = none ∧ y.tcbExt.tcbRunVcpu = none) + -- the new SCs + (hnc : ∀ c ∈ Cn, L.mem s' (wa c 24#u64) = 0#u64 ∧ L.mem s' (wa c 72#u64) = 0#u64 ∧ + ∃ y k, σ'.kheap (specPtr c) = some (.schedContext y k) ∧ y.scTcb = none ∧ y.scRefillMax = 0) + -- the new VCPUs + (hnv : ∀ v ∈ Vn, L.mem s' v = 0#u64 ∧ L.mem s' (wa v 24#u64) = 0#u64 ∧ + ∃ y, σ'.kheap (specPtr v) = some (.archObj (.vcpu y)) ∧ y.vcpuTcb = none ∧ y.vcpuExt.runCaller = none) + -- the new VSpace pages + (hpt : ∀ q ∈ utS1NewA nt aligned (zA nt.val us) win, ∃ t, σ'.kheap (specPtr q) = + some (.archObj (.pageTable t)) ∧ Fl4SpecA64.ptType t = ptTyA (if nt.val = 8 then 0 else 1)) + (hs2 : ∀ r ∈ utXNewA nt aligned (zA nt.val us) win, + ∃ es, σ'.kheap (specPtr r) = some (.archObj (.ext (.stage2Root es)))) + (hhv : ∀ y ∈ utYNewA nt aligned (zA nt.val us) win, + (∃ l1, σ'.kheap (specPtr y) = some (.archObj (.ext (.highVSpace l1)))) ∧ + (dBitA (L.mem s' (peAF y 511#u64)) 0 = true ∧ dBitA (L.mem s' (peAF y 511#u64)) 1 = true ∧ + dTabA (L.mem s' (peAF y 511#u64)) = wa y 4096#u64) ∧ + ∀ i : U64, i.val < 256 → L.mem s' (peAF y i) = 0#u64) + -- no other new extension object + (bvc : ∀ q y, σ'.kheap q = some (.archObj (.vcpu y)) → + σ.kheap q = some (.archObj (.vcpu y)) ∨ ∃ v ∈ Vn, specPtr v = q) + (bs2 : ∀ q es, σ'.kheap q = some (.archObj (.ext (.stage2Root es))) → + σ.kheap q = some (.archObj (.ext (.stage2Root es))) ∨ + ∃ r ∈ utXNewA nt aligned (zA nt.val us) win, specPtr r = q) + (bhv : ∀ q l1, σ'.kheap q = some (.archObj (.ext (.highVSpace l1))) → + σ.kheap q = some (.archObj (.ext (.highVSpace l1))) ∨ + ∃ r ∈ utYNewA nt aligned (zA nt.val us) win, specPtr r = q) : + JAtZAX (L := L) AR (utRtZA Z G' nt us aligned win Tn Cn Vn) s' a' σ' (utRtMA m Tn) := by + have H' : SameNA (L := L) Z s s' := + ⟨w.1, w.2.1, fun b hb => utRt_footKeptA J hx hc h1 h2 w hb⟩ + have Cl : UtClrA st Z H s aligned.val (aligned.val + win * 2 ^ (zA nt.val us).val) := utRt_winClrA J hx hc h1 h2 + have newV : ∀ q ∈ utBasesA aligned (zA nt.val us) win, + aligned.val ≤ q.val ∧ q.val < aligned.val + win * 2 ^ (zA nt.val us).val := by + rintro q ⟨j, hj, rfl⟩ + exact utRt_newValA W hj + have anchorB : ∀ q ∈ utBasesA aligned (zA nt.val us) win, specPtr q ≠ Fl4SpecA64.idleScPtr := by + intro q hq + have v := newV q hq + exact utRet_anchorA (st := st) J.idleAnchor hx hc (by omega) (by omega) + have anchor : ∀ j, j < win → specPtr (bjA aligned (zA nt.val us) j) ≠ Fl4SpecA64.idleScPtr := + fun j hj => anchorB _ ⟨j, hj, rfl⟩ + have keepRel : ∀ q ko, RelPA Z m q → σ.kheap q = some ko → (∀ sz cn, ko ≠ .cnode sz cn) → + σ'.kheap q = σ.kheap q := by + intro q ko hq e ne + refine hkeep q (utRt_relOffA J Cl W rfl rfl anchor hq) fun sz cn h => ?_ + rw [e] at h + exact ne sz cn (Option.some.inj h) + have thFree : ∀ q, UtNewA false nt us aligned win q → Z.th q = none := by + rintro q ⟨-, -, -, j, hj, rfl⟩ + by_contra h + exact utRt_relOffA (m := m) J Cl W rfl rfl anchor (Or.inl ⟨_, h, rfl⟩) ⟨j, hj, rfl⟩ + -- the old threads, SCs and VCPUs + have kth : ∀ t ∈ Z.T, σ'.kheap (specPtr t) = σ.kheap (specPtr t) := fun t ht => by + obtain ⟨y, e, -⟩ := J.j.1.sch.tcb t ht + exact keepRel _ _ (Or.inr (Or.inl ⟨t, ht, rfl⟩)) e (fun _ _ h => by cases h) + have kch : ∀ c ∈ Z.C, σ'.kheap (scRenA m c) = σ.kheap (scRenA m c) := fun c hc' => by + obtain ⟨y, k, e, -⟩ := J.j.1.sch.sc c hc' + exact keepRel _ _ (Or.inr (Or.inr (Or.inl ⟨c, hc', rfl⟩))) e (fun _ _ h => by cases h) + have kvh : ∀ v ∈ Z.V, σ'.kheap (specPtr v) = σ.kheap (specPtr v) := fun v hv => by + obtain ⟨y, e, -⟩ := J.j.2.1.obj v hv + exact keepRel _ _ (Or.inr (Or.inr (Or.inr (Or.inl ⟨v, hv, rfl⟩)))) e (fun _ _ h => by cases h) + -- the new SCs are not the idle SC + have idleC : specPtr (L.mem s (L.nodeAt s (L.cpu s) layout.node.IDLE_SC)) = m.idleScOf m.curCore := by + rw [J.j.1.sch.core] + exact (J.j.1.sch.idleScOf _ J.j.1.sch.cpuCs).symm + have newC : ∀ c ∈ Cn, specPtr c ≠ m.idleScOf m.curCore ∧ specPtr c ≠ Fl4SpecA64.idleScPtr := by + intro c hcm + refine ⟨fun e => ?_, anchorB c (hCn c hcm)⟩ + have ic := J.j.1.idleC _ J.j.1.sch.cpuCs + have hcs : L.mem s (L.nodeAt s (L.cpu s) layout.node.IDLE_SC) ∈ H.scs := by rw [J.scs]; exact ic + rw [← idleC] at e + obtain ⟨j, hj, ej⟩ := hCn c hcm + exact utRt_objOffA Cl W rfl rfl (JointObjectA.of_sc hcs) (Nat.two_pow_pos _) + ⟨j, hj, by rw [← e, ej]⟩ + have renC : ∀ c ∈ Cn, scRenA m c = specPtr c := fun c hcm => by + unfold scRenA + rw [ite_eq_right (newC c hcm).1] + -- the old VSpace pages + have hoff : ∀ q, UtOldPgA Z.Pp Z.S1 Z.X Z.Y Z.T2 q → + q.val < aligned.val ∨ aligned.val + win * 2 ^ (zA nt.val us).val ≤ q.val := by + have page : ∀ q, q ∈ Z.Pp ∨ q ∈ Z.S1 ∨ q ∈ Z.T2 → q.val < aligned.val ∨ + aligned.val + win * 2 ^ (zA nt.val us).val ≤ q.val := by + intro q hq + have j : JointObjectA Z H q 4096 := Or.inl ⟨rfl, hq.imp id (fun h => h.imp id Or.inl)⟩ + rcases Cl.obj q _ j with g | g <;> omega + have root : ∀ r, r ∈ Z.X ∨ r ∈ Z.Y → + (r.val < aligned.val ∨ aligned.val + win * 2 ^ (zA nt.val us).val ≤ r.val) ∧ + ((wa r 4096#u64).val < aligned.val ∨ aligned.val + win * 2 ^ (zA nt.val us).val ≤ (wa r 4096#u64).val) := by + intro r hr + have j : JointObjectA Z H r 8192 := Or.inr (Or.inl ⟨rfl, hr⟩) + obtain ⟨o, y, ho, k1, k2⟩ := J.setsInH.roots r hr + have f := J.sys.ent.heap.fits o _ ho + simp only [HKind.bits] at f + have e : (wa r 4096#u64).val = r.val + 4096 := by + rw [wa_valP, show (4096#u64 : U64).val = 4096 from rfl] + exact Nat.mod_eq_of_lt (by omega) + rcases Cl.obj r _ j with g | g <;> omega + rintro q (h | h | h | h | ⟨y, hy, rfl⟩ | h | ⟨r, hr, rfl⟩) + · exact page q (Or.inl h) + · exact page q (Or.inr (Or.inl h)) + · exact page q (Or.inr (Or.inr h)) + · exact (root q (Or.inr h)).1 + · exact (root y (Or.inr hy)).2 + · exact (root q (Or.inl h)).1 + · exact (root r (Or.inl hr)).2 + have V0 := J.j.1.vs + have E0 := J.j.1.ext + have kpg : ∀ q, q ∈ Z.Pp ∨ q ∈ Z.S1 ∨ q ∈ Z.Y ∨ q ∈ Z.X ∨ q ∈ Z.T2 → + σ'.kheap (specPtr q) = σ.kheap (specPtr q) := by + intro q hq + rcases hq with h | h | h | h | h + · obtain ⟨f, e, -⟩ := V0.pool q h + exact keepRel _ _ (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨q, h, rfl⟩))))))) e + (fun _ _ g => by cases g) + · obtain ⟨t, e, -⟩ := V0.pt q h + exact keepRel _ _ (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨q, h, rfl⟩)))))))) e + (fun _ _ g => by cases g) + · obtain ⟨t, e, -⟩ := V0.high q h + exact keepRel _ _ (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨q, h, rfl⟩)))))) e (fun _ _ g => by cases g) + · obtain ⟨t, e, -⟩ := E0.s2root q h + exact keepRel _ _ (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨q, h, rfl⟩))))) e (fun _ _ g => by cases g) + · obtain ⟨t, e, -⟩ := E0.s2pt q h + exact keepRel _ _ (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ⟨q, h, rfl⟩)))))))) e + (fun _ _ g => by cases g) + have G : UtVsGrowA Z.Pp Z.S1 (Z.S1 ∪ utS1NewA nt aligned (zA nt.val us) win) Z.X + (Z.X ∪ utXNewA nt aligned (zA nt.val us) win) Z.Y (Z.Y ∪ utYNewA nt aligned (zA nt.val us) win) Z.T2 Z.Lv + (utLvNewA Z.Lv nt aligned (zA nt.val us) win) (L.mem s) (L.mem s') (L.ksb s) σ σ' := + utRt_vsGrowA W V0 E0 hoff (fun b hb => H'.mem b (.vs b hb)) (fun b hb => H'.mem b (.ext b hb)) hglob.asid kpg + hpt hs2 hhv + -- the caps of the new heap's slots, by constructor + have capOld : ∀ y, IsSlotA (utRtThA Z.th false nt us aligned win) y → ∀ c : capability.Cap, + capAtA (L.mem s') y = c → (∀ a b d e, c ≠ .Untyped a b d e) → c ≠ .Null → + (∃ y0, IsSlotA Z.th y0 ∧ capAtA (L.mem s) y0 = c) ∨ + ∃ j < win, newCapA nt.val (bjA aligned (zA nt.val us) j) us false = c := by + intro y hy c e nu nn + rcases hcap y hy with ⟨h, k⟩ | ⟨j, hj, k⟩ | ⟨a, b, d, f, k⟩ | k + · exact Or.inl ⟨y, h, k.symm.trans e⟩ + · exact Or.inr ⟨j, hj, k.symm.trans e⟩ + · exact absurd (e.symm.trans k) (nu _ _ _ _) + · exact absurd (e.symm.trans k) nn + have inV : ∀ c ∈ Z.C, c ∈ Z.C ∪ Cn := fun c h => Or.inl h + refine ⟨?_, ?_, ?_, ?_⟩ + · refine + { st := hS + cdt := hC + sch := ?_ + ar := har + place := ?_ + irq := ?_ + rest := ?_ + thT := ?_ + csN := J.j.1.csN + idleT := fun c hc' => ?_ + idleC := fun c hc' => ?_ + curT := fun c hc' => ?_ + cscC := fun c hc' => ?_ + ksFit := ?_ + scOffD := ?_ + cv := ?_ + pml := ?_ + vs := ?_ + ext := ?_ } + · refine utRt_schedRelA (Tn := Tn) (Cn := Cn) J.j.1.sch H'.cpu (fun c hc' o ho => ?_) ?_ H'.act H'.time + (fun t ht => ⟨(H'.tcbW ht).2.2.1, (H'.tcbW ht).1, (H'.tcbW ht).2.1⟩) (fun c hc' => H'.scW hc') kth kch + ⟨hglob.cur, hglob.csc, hglob.idle, hglob.act, hglob.time⟩ (fun t ht => ?_) (fun c hcm => ?_) + · have h := H'.node hc' + rcases ho with rfl | rfl | rfl | rfl + · exact h.1 + · exact h.2.2.1 + · exact h.2.1 + · exact h.2.2.2 + · unfold LawfulStore.idleW + rw [H'.cpu] + exact (H'.node J.j.1.sch.cpuCs).2.1 + · obtain ⟨a1, a2, a3, -, -, -, -, y, e, b1, b2, -⟩ := hnt t ht + exact ⟨a1, a2, a3, y, e, b1, b2⟩ + · obtain ⟨a1, -, y, k, e, b1, -⟩ := hnc c hcm + exact ⟨a1, (newC c hcm).1, (newC c hcm).2, y, k, e, b1⟩ + · show PlaceA (utRtThA Z.th false nt us aligned win) (L.ksb s') + rw [H'.ksb] + exact P' + · exact (IrqRelA.of_mem J.j.1.irq H').of_spec hglob + · refine utRt_schedRestA (Z := Z) (Tn := Tn) (Cn := Cn) J.j.1.rest rfl rfl rfl rfl H'.cpu H'.ksb + (fun o ho => ?_) ?_ ⟨hglob.rq, hglob.rls, hglob.cons, hglob.repr, hglob.dom, hglob.domT⟩ + (fun t ht => ⟨(H'.tcb2W ht).2, (H'.tcb2W ht).1, (H'.tcbW ht).2.1⟩) + (fun c hc' => H'.mem _ (.sc2 _ hc')) kth kch (fun t ht => ?_) (fun c hcm => ?_) + · have h := H'.restW + rw [H'.cpu] at h + rcases ho with rfl | rfl + · exact h.1 + · exact h.2.1 + · have h := H'.restW + rw [H'.ksb] at h + exact ⟨h.2.2.1, h.2.2.2⟩ + · obtain ⟨-, -, a3, a4, a5, -, -, y, e, -, -, b3, b4, -⟩ := hnt t ht + exact ⟨a4, a5, a3, y, e, b3, b4⟩ + · obtain ⟨-, a2, y, k, e, -, b2⟩ := hnc c hcm + refine ⟨y, k, by rw [renC c hcm]; exact e, ?_⟩ + rw [a2, b2] + rfl + · intro t + show utRtThA Z.th false nt us aligned win t = some .tcb ↔ t ∈ Z.T ∪ Tn + rw [utRtTh_tcbA J.j.1.thT thFree t, Set.mem_union, hTn] + · show L.mem s' (L.nodeAt s' c layout.node.IDLE_THREAD) ∈ Z.T ∪ Tn + rw [(H'.node hc').2.1] + exact Or.inl (J.j.1.idleT c hc') + · show L.mem s' (L.nodeAt s' c layout.node.IDLE_SC) ∈ Z.C ∪ Cn + rw [(H'.node hc').2.2.2] + exact Or.inl (J.j.1.idleC c hc') + · show L.mem s' (L.nodeAt s' c layout.node.CUR_THREAD) ∈ Z.T ∪ Tn + rw [(H'.node hc').1] + exact Or.inl (J.j.1.curT c hc') + · show L.mem s' (L.nodeAt s' c layout.node.CUR_SC) ∈ Z.C ∪ Cn + rw [(H'.node hc').2.2.1] + exact Or.inl (J.j.1.cscC c hc') + · rw [H'.ksb] + exact J.j.1.ksFit + · intro q hq ne + show specPtr q ≠ Fl4SpecA64.idleScPtr + change specPtr q ≠ m.idleScOf m.curCore at ne + have old : InDomA Z q → specPtr q ≠ Fl4SpecA64.idleScPtr := fun h => J.j.1.scOffD q h ne + rcases hq with h | h | h | h + · change utRtThA Z.th false nt us aligned win q ≠ none at h + unfold utRtThA at h + split_ifs at h with g + · obtain ⟨-, -, -, j, hj, rfl⟩ := g + exact anchor j hj + · exact old (Or.inl h) + · rcases h with h | h + · exact old (Or.inr (Or.inl h)) + · exact anchorB q (hVn q h) + · rcases h with h | h + · exact old (Or.inr (Or.inr (Or.inl h))) + · exact anchorB q h.2 + · rcases h with h | h + · exact old (Or.inr (Or.inr (Or.inr h))) + · exact anchorB q h.2 + · intro y hy + change IsSlotA (utRtThA Z.th false nt us aligned win) y at hy + show CapValidA (utRtThA Z.th false nt us aligned win) (capAtA (L.mem s') y) + rcases hcap y hy with ⟨h, k⟩ | ⟨j, hj, k⟩ | ⟨a, b, d, f, k⟩ | k + · rw [k] + exact utRt_capValid_growA thFree (J.j.1.cv y h) + · rw [k] + exact utRt_capValid_newA hn (fun _ => rfl) hr4 hj + · rw [k] + trivial + · rw [k] + trivial + · refine utRt_pmlA (Z := Z) (Z' := utRtZA Z G' nt us aligned win Tn Cn Vn) (s := s) (s' := s') + (nt := nt) (aligned := aligned) (z := zA nt.val us) (win := win) J.j.1.pml rfl rfl (fun q hq g => ?_) + (fun y hy q mp asid e => ?_) + · have v := newV q g.2 + rcases hoff q (Or.inr (Or.inl hq)) with k | k <;> omega + · rcases capOld y hy _ e (fun _ _ _ _ h => by cases h) (fun h => by cases h) with h | ⟨j, hj, k⟩ + · exact Or.inl h + · obtain ⟨e8, rfl⟩ := utNewCap_pmlA k + exact Or.inr ⟨e8, j, hj, rfl⟩ + · show VsRelA Z.Pp _ _ _ _ (L.mem s') (L.ksb s') σ' + rw [H'.ksb] + exact utVsRel_growA V0 G + · exact utExtVs_growA V0 E0 G + · refine utRt_vcpuCoreA (Z := Z) (Z' := utRtZA Z G' nt us aligned win Tn Cn Vn) (Tn := Tn) (Vn := Vn) J.j.2.1 rfl + rfl (fun v hv => H'.vcW hv) (fun t ht => ⟨(H'.tcbW ht).2.2.2.1, (H'.tcbW ht).2.2.2.2⟩) kvh kth bvc hnv + (fun t ht => ?_) (fun y hy q e => ?_) + · obtain ⟨-, -, -, -, -, a6, a7, y, e, -, -, -, -, b5, b6⟩ := hnt t ht + exact ⟨a6, a7, y, e, b5, b6⟩ + · rcases capOld y hy _ e (fun _ _ _ _ h => by cases h) (fun h => by cases h) with h | ⟨j, hj, k⟩ + · exact Or.inl h + · obtain ⟨e12, rfl⟩ := utNewCap_vcpuA k + exact Or.inr (hVn12 e12 _ ⟨j, hj, rfl⟩) + · refine utRt_stage2CoreA (Z := Z) (Z' := utRtZA Z G' nt us aligned win Tn Cn Vn) (nt := nt) (aligned := aligned) + (z := zA nt.val us) (win := win) J.j.2.2.1 rfl (fun r hr => kpg r (Or.inr (Or.inr (Or.inr (Or.inl hr))))) hs2 + bs2 (fun y hy q l mp asid va e => ?_) + rcases capOld y hy _ e (fun _ _ _ _ h => by cases h) (fun h => by cases h) with h | ⟨j, hj, k⟩ + · exact Or.inl h + · obtain ⟨e13, rfl⟩ := utNewCap_eptA k + exact Or.inr ⟨e13, j, hj, rfl⟩ + · refine utRt_highCoreA (Z := Z) (Z' := utRtZA Z G' nt us aligned win Tn Cn Vn) (nt := nt) (aligned := aligned) + (z := zA nt.val us) (win := win) J.j.2.2.2 rfl (fun r hr => kpg r (Or.inr (Or.inr (Or.inl hr)))) + (fun r hr => (hhv r hr).1) bhv (fun y hy q mp asid e => ?_) + rcases capOld y hy _ e (fun _ _ _ _ h => by cases h) (fun h => by cases h) with h | ⟨j, hj, k⟩ + · exact Or.inl h + · obtain ⟨e14, rfl⟩ := utNewCap_highA k + exact Or.inr ⟨e14, j, hj, rfl⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtRtRdA.lean b/proofs/Fl4ProofsA64/P3/UtRtRdA.lean new file mode 100644 index 000000000..9ad73f7b5 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtRtRdA.lean @@ -0,0 +1,279 @@ +/- +P3 (aarch64, A64-P3 Untyped, full-record post): **a creation keeps every word the record reads beyond `JAtZAX`'s +footprint** (`UtRdA`: scheduler, SC, endpoint, reply, notification and TCB words). + +* **`utRt_rdOffA`**: a word of `UtRdA` is off the `OS` slots, the TCB debug links and node 0's debug head, given + that it is no TCB's buffer-slot word (other words) or not the debug head (scheduler words). +* **`utRt_rdKeptA`**: across the creation's write frame (`utRetype_frameA`) such a word is kept. The buffer-slot + words are read through the slot's cap (`CrtE.keep`). + +No named premise. New theorems only. +-/ +import Fl4ProofsA64.P3.UtRtKeptA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +set_option maxRecDepth 100000 + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s : S} {a : A} {σ : State Unit} {m : McState} + +/-- A word off the `OS` slots, off every TCB's debug link and other than node 0's debug head. -/ +def UtOffSlA (st : machine.Store S) [LawfulStore st] (H : Heap) (OS : Set U64) (s : S) (b : U64) : Prop := + ¬ InOS OS b ∧ (∀ u ∈ H.tcbs, b ≠ dpA u) ∧ b ≠ nwA st s + +/-- The words of `UtRdA` beyond the scheduler's. -/ +def UtRdBA (st : machine.Store S) [LawfulStore st] (Z : XSetsA) (N E Rp : Set U64) (Bf : Set (U64 × Word)) + (a : U64) : Prop := + EFoot Z.T E Rp Bf a ∨ NFoot N Z.T a ∨ (∃ r ∈ Rp, a = rNextA r ∨ a = rPrevA r) ∨ + ∃ t ∈ Z.T, a = mcpA t ∨ a = yieldA t ∨ a = sbWordA t ∨ a = wa (sbWordA t) 8#u64 ∨ a = wa t 3512#u64 ∨ + a = wa t 3664#u64 ∨ a = flagsA t + +variable (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) +include J + +private theorem utRt_dpValA {u : U64} (hu : u ∈ H.tcbs) : (dpA u).val = u.val + 3584 := by + have := J.sys.ent.heap.fits u .tcb hu + simp only [HKind.bits] at this + unfold dpA + rw [wa_valP, show (3584#u64 : U64).val = 3584 from rfl] + exact Nat.mod_eq_of_lt (by omega) + +private theorem utRt_nwValA : (nwA st s).val = (L.ksb s).val + 223576 := by + have h0 : (0#u64 : U64) ∈ Z.Cs := by + obtain ⟨k, hk, -, h⟩ := J.j.1.csN + exact (h 0#u64).2 (by simpa using hk) + rw [nodeAt_val (J.sq.nodes 0#u64 h0) (by rw [debug_val]; omega), debug_val] + unfold LawfulStore.nodeBase + simp + +/-- A word outside the node area is not the debug head. -/ +private theorem utRt_neNwA {b : U64} (h : b.val < (L.ksb s).val + 223488 ∨ (L.ksb s).val + 772352 ≤ b.val) : + b ≠ nwA st s := fun e => by + rw [e, utRt_nwValA J] at h + omega + +/-- A high field word of a related TCB, other than its debug link. -/ +theorem utRt_tcbOffA {t b : U64} (ht : t ∈ Z.T) (hal : Aligned b) (h1 : t.val + 224 ≤ b.val) + (h2 : b.val < t.val + 4096) (h3 : b.val ≠ t.val + 3584) : UtOffSlA st H OS s b := by + have hH := J.sys.ent.heap + have htH : H t = some .tcb := by + have h : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + exact h + have f0 := hH.fits t _ htH + have d0 := J.ko.1 t .tcb htH (fun y e => by cases e) + rw [show HKind.tcb.bits = 12 from rfl] at d0 f0 + refine ⟨fun h => ?_, fun u hu e => ?_, utRt_neNwA J (by rcases d0 with g | g <;> omega)⟩ + · obtain ⟨⟨Q, R, q⟩, -⟩ := J.sys.ent.bxi + obtain ⟨K, hK⟩ : ∃ K : U64, K.val = b.val - t.val := + ⟨⟨BitVec.ofNat 64 (b.val - t.val)⟩, by + show (BitVec.ofNat 64 _).toNat = _ + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega)⟩ + have f := hH.fits t _ htH + simp only [HKind.bits] at f + have e : b = wa t K := by + apply UScalar.eq_of_val_eq + rw [wa_valP, hK, Nat.mod_eq_of_lt (by omega)] + omega + exact capw_ne_hi q (inOS_capw J.sys.ent.os hal h) htH (K := K) (by omega) (by omega) e + · have g := congrArg UScalar.val e + rw [utRt_dpValA J hu] at g + by_cases eu : t = u + · subst eu; omega + · have := hH.disj t _ u _ htH hu eu + simp only [HKind.bits] at this + omega + +/-- A word of an endpoint, notification, reply or SC object. -/ +theorem utRt_objOffSlA {q b : U64} {k : HKind} (hq : H q = some k) + (hk : k = .ep ∨ k = .ntfn ∨ k = .reply ∨ ∃ z, k = .sc z) (hal : Aligned b) (h1 : q.val ≤ b.val) + (h2 : b.val < q.val + 2 ^ k.bits) : UtOffSlA st H OS s b := by + have hH := J.sys.ent.heap + have uniq : ∀ q' k', H q' = some k' → q'.val ≤ b.val → b.val < q'.val + 2 ^ k'.bits → q' = q ∧ k' = k := by + intro q' k' hq' g1 g2 + by_cases e : q' = q + · subst e + exact ⟨rfl, Option.some.inj (hq'.symm.trans hq)⟩ + · rcases hH.disj q' k' q k hq' hq e with g | g <;> omega + have ntcb : k ≠ .tcb := by + rcases hk with rfl | rfl | rfl | ⟨z, rfl⟩ <;> exact fun h => by cases h + have noT : ∀ u ∈ H.tcbs, u.val ≤ b.val → b.val < u.val + 4096 → False := by + intro u hu g1 g2 + exact ntcb (uniq u .tcb hu g1 (by simpa [HKind.bits] using g2)).2.symm + have d0 := J.ko.1 q k hq (fun y e => by + rcases hk with g | g | g | ⟨z, g⟩ <;> rw [g] at e <;> cases e) + refine ⟨fun h => ?_, fun u hu e => ?_, utRt_neNwA J (by rcases d0 with g | g <;> omega)⟩ + · rcases inOS_capw J.sys.ent.os hal h with ⟨u, hu, K, hK, e⟩ | ⟨⟨-, q', k', hq', hc, hin⟩, nf⟩ + · have f := hH.fits u .tcb hu + simp only [HKind.bits] at f + have v : b.val = u.val + K.val := by + rw [e, wa_valP]; exact Nat.mod_eq_of_lt (by omega) + exact noT u hu (by omega) (by omega) + · obtain ⟨rfl, rfl⟩ := uniq q' k' hq' hin.1 hin.2 + refine nf ⟨hal, q', k', hq', ?_, hin⟩ + rcases hc with c | c | ⟨z, c⟩ | c + · exact Or.inl c + · exact Or.inr (Or.inl c) + · subst c + rcases hk with g | g | g | ⟨y, g⟩ <;> cases g + · exact Or.inr (Or.inr c) + · have g := congrArg UScalar.val e + rw [utRt_dpValA J hu] at g + exact noT u hu (by omega) (by omega) + +theorem utRt_nodeOffA {c o b : U64} (hc : c ∈ Z.Cs) (ho : o.val < 4288) (e : b = L.nodeAt s c o) + (hal : Aligned b) (hnw : b ≠ nwA st s) : UtOffSlA st H OS s b := by + obtain ⟨⟨Q, R, q⟩, -⟩ := J.sys.ent.bxi + refine ⟨fun h => capw_ne_node q (inOS_capw J.sys.ent.os hal h) hc ho e, fun u hu e' => ?_, hnw⟩ + have nc := J.sq.nodes c hc + have hv : b.val = L.nodeBase s c + o.val := by rw [e]; exact nodeAt_val nc ho + have g := congrArg UScalar.val e' + rw [utRt_dpValA J hu] at g + have d := nc.disj u hu + omega + +theorem utRt_globOffA {o b : U64} (ho : o = 8#u64 ∨ o = 16#u64) (e : b = wa (L.ksb s) o) (hal : Aligned b) : + UtOffSlA st H OS s b := by + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.sys.ent.ksw + have hb : FootNA (L := L) Z s b := + FootNA.glob b (by rcases ho with rfl | rfl; exacts [Or.inl e, Or.inr e]) + have vo0 : o.val = 8 ∨ o.val = 16 := by rcases ho with rfl | rfl <;> simp + have v0 : b.val = (L.ksb s).val + o.val := by + rw [e, wa_valP]; exact Nat.mod_eq_of_lt (by omega) + refine ⟨fun h => footN_offA J.toJEntA b hb (Or.inr ⟨h, id⟩), fun u hu e' => ?_, utRt_neNwA J (by omega)⟩ + have vo : o.val = 8 ∨ o.val = 16 := by rcases ho with rfl | rfl <;> simp + have v : b.val = (L.ksb s).val + o.val := by + rw [e, wa_valP]; exact Nat.mod_eq_of_lt (by omega) + have g := congrArg UScalar.val e' + rw [utRt_dpValA J hu] at g + have d := J.ko.1 u .tcb hu (fun y e => by cases e) + rw [show HKind.tcb.bits = 12 from rfl] at d + omega + +theorem utRt_bufOffSlA {b : U64} (h : BufW Bf b) : UtOffSlA st H OS s b := by + have off : b.val < (L.ksb s).val + 223488 ∨ (L.ksb s).val + 772352 ≤ b.val := by + obtain ⟨p, len, hp, g1, g2⟩ := J.ipcPlace.buffer b h + have ni : ¬ IrqNodeA (L.ksb s) p := by + rintro ⟨i, hi, e⟩ + have := J.j.1.place.irq i hi + rw [← e, hp] at this + cases this + have := J.j.1.place.ks p _ hp ni + simp only [ObjKindA.size, ksSizeA] at this + omega + refine ⟨J.bufOff.os b h, fun u hu e => ?_, utRt_neNwA J off⟩ + have hu' : u ∈ Z.T := by rw [← J.tcbs]; exact hu + have g := congrArg UScalar.val e + rw [utRt_dpValA J hu] at g + rcases J.iplaced.e.bt b h u hu' with d | d <;> omega + +/-- **The words of `UtRdA` are off the slots and debug links** (module docstring). -/ +theorem utRt_rdOffA {b : U64} (hal : Aligned b) + (h : (UtRdBA st Z N E Rp Bf b ∧ ∀ t ∈ Z.T, b ≠ sbWordA t ∧ b ≠ wa (sbWordA t) 8#u64) ∨ + (SReadA st Z m s σ b ∧ b ≠ nwA st s)) : UtOffSlA st H OS s b := by + have tf : ∀ t ∈ Z.T, ∀ K : U64, 224 ≤ K.val → K.val < 4096 → K.val ≠ 3584 → b = wa t K → + UtOffSlA st H OS s b := by + intro t ht K h1 h2 h3 e + have f := (ut_tcbThA J ht).2 + have v : b.val = t.val + K.val := by + rw [e, wa_valP]; exact Nat.mod_eq_of_lt (by omega) + exact utRt_tcbOffA J ht hal (by omega) (by omega) (by omega) + have ipcH : ∀ q k, H q = some k → (k = .ep ∨ k = .ntfn ∨ k = .reply ∨ ∃ z, k = .sc z) → + q.val ≤ b.val → b.val < q.val + 2 ^ k.bits → UtOffSlA st H OS s b := + fun q k hq hk h1 h2 => utRt_objOffSlA J hq hk hal h1 h2 + have rw8 : ∀ r ∈ Rp, ∀ o : U64, o.val < 32 → b = wa r o → UtOffSlA st H OS s b := by + intro r hr o ho e + have hH := J.utIpc.reply r hr + have f := J.sys.ent.heap.fits r _ hH + simp only [HKind.bits] at f + have v : b.val = r.val + o.val := by + rw [e, wa_valP]; exact Nat.mod_eq_of_lt (by omega) + exact ipcH r _ hH (Or.inr (Or.inr (Or.inl rfl))) (by omega) (by simp only [HKind.bits]; omega) + rcases h with ⟨h | h | ⟨r, hr, h⟩ | ⟨t, ht, h⟩, hsb⟩ | ⟨h, hnw⟩ + rotate_left 4 + · -- the scheduler and SC words + rcases h with ⟨c, hc, o, ho, e⟩ | e | e | ⟨t, ht, K, hK, e⟩ | h + · exact utRt_nodeOffA J hc ho e hal hnw + · exact utRt_globOffA J (Or.inl rfl) e hal + · exact utRt_globOffA J (Or.inr rfl) e hal + · refine tf t ht K ?_ ?_ ?_ e <;> rcases hK with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> simp + · obtain ⟨c, hc, x, k, hk, ha⟩ := h + have hsz := (J.scSize c hc x k hk).1 + have hcH : c ∈ H.scs := by rw [J.scs]; exact hc + obtain ⟨y, hy⟩ := hcH + have ey : H.scBits c = y := by unfold Heap.scBits; rw [hy] + rw [ey] at hsz + obtain ⟨g1, g2⟩ := ha + refine ipcH c _ hy (Or.inr (Or.inr (Or.inr ⟨y, rfl⟩))) g1 (lt_of_lt_of_le g2 ?_) + simp only [HKind.bits] + exact Nat.add_le_add_left (Nat.pow_le_pow_right (by norm_num) hsz) _ + · -- the endpoint side + rcases h with ⟨e, he, hv⟩ | ⟨r, hr, hv⟩ | ⟨t, ht, hv⟩ | hv + · exact ipcH e _ (J.utIpc.ep e he) (Or.inl rfl) (by omega) (by simp only [HKind.bits]; omega) + · exact ipcH r _ (J.utIpc.reply r hr) (Or.inr (Or.inr (Or.inl rfl))) (by omega) + (by simp only [HKind.bits]; omega) + · rcases hv with ⟨h1, h2⟩ | h2 + · refine utRt_tcbOffA J ht hal ?_ ?_ ?_ <;> (unfold TOffN at h2; omega) + · unfold TL at h2 + refine utRt_tcbOffA J ht hal ?_ ?_ ?_ <;> omega + · exact utRt_bufOffSlA J hv + · -- the notification side + rcases h with ⟨c, hc, h1, h2⟩ | ⟨t, ht, e⟩ + · exact ipcH c _ (J.utIpc.ntfn c hc) (Or.inr (Or.inl rfl)) h1 (by simp only [HKind.bits]; omega) + · rcases e with e | e | e | e | e | e + · rw [stA_eq] at e; exact tf t ht _ (by simp) (by simp) (by simp) e + · exact tf t ht 3336#u64 (by simp) (by simp) (by simp) e + · have eb : badgeRegA t = wa t 256#u64 := by + unfold badgeRegA layout.tcb.REGS layout.reg.BADGE layout.reg.X0 + apply UScalar.eq_of_val_eq + simp [core.num.U64.wrapping_add_val_eq, core.num.U64.wrapping_mul_val_eq, U64.size_def, U64.numBits] + rw [eb] at e; exact tf t ht _ (by simp) (by simp) (by simp) e + · exact tf t ht 3448#u64 (by simp) (by simp) (by simp) e + · exact tf t ht 3496#u64 (by simp) (by simp) (by simp) e + · exact tf t ht 3504#u64 (by simp) (by simp) (by simp) e + · -- the reply links + have e8 : rPrevA r = wa r 8#u64 := by unfold rPrevA layout.reply.PREV; rfl + have e16 : rNextA r = wa r 16#u64 := by unfold rNextA layout.reply.NEXT; rfl + rcases h with e | e + · exact rw8 r hr 16#u64 (by simp) (e.trans e16) + · exact rw8 r hr 8#u64 (by simp) (e.trans e8) + · rcases h with e | e | e | e | e | e | e + · exact tf t ht 3440#u64 (by simp) (by simp) (by simp) e + · exact tf t ht 3472#u64 (by simp) (by simp) (by simp) e + · exact absurd e (hsb t ht).1 + · exact absurd e (hsb t ht).2 + · exact tf t ht 3512#u64 (by simp) (by simp) (by simp) e + · exact tf t ht 3664#u64 (by simp) (by simp) (by simp) e + · exact tf t ht 3456#u64 (by simp) (by simp) (by simp) e + +/-- **A creation keeps the words the record reads** (module docstring). -/ +theorem utRt_rdKeptA {x p sb fi : U64} (hx : x ∈ OS) (hc : capA st s x = .Untyped p sb fi false) + {nt base z : U64} {len : Nat} (h1 : p.val + fi.val ≤ base.val) + (h2 : base.val + len * 2 ^ z.val ≤ p.val + 2 ^ sb.val) + {s' : S} (w : WritesIn st s s' (UtCrtWA st H OS s nt base z len)) {b : U64} (hal : Aligned b) + (hr : (UtRdBA st Z N E Rp Bf b ∧ ∀ t ∈ Z.T, b ≠ sbWordA t ∧ b ≠ wa (sbWordA t) 8#u64) ∨ + (SReadA st Z m s σ b ∧ b ≠ nwA st s)) : L.mem s' b = L.mem s b := by + have O := utRt_rdOffA J hal hr + have hrd : UtRdA st Z N E Rp Bf m s σ b := hr.elim (fun h => Or.inr h.1) (fun h => Or.inl h.1) + refine w.mem_eq hal ?_ + rintro (h | ⟨-, g1, g2⟩ | h | ⟨u, hu, h⟩) + · exact O.1 h + · exact ut_rd_regionA J (utRt_winClrA J hx hc h1 h2) hrd ⟨g1, g2⟩ + · exact O.2.2 h + · exact O.2.1 u hu h + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtRtRecA.lean b/proofs/Fl4ProofsA64/P3/UtRtRecA.lean new file mode 100644 index 000000000..7e20bea7e --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtRtRecA.lean @@ -0,0 +1,184 @@ +/- +P3 (aarch64, A64-P3 Untyped, full-record post): **the whole record `JStage3A` after a RAM creation without a reset**, +from the record at the decode's state and at the creation start (after the caller's restart write), A64 P1's creation +end there (`S1aRtIn`, at the kept heap `H` and slots `OS`), A64 P1's creation top (`utRt_j7TopA`'s `ArmQIVAt`, +`HeapKsOff`, `J7A` hook), the creation's end facts (the write frame, the new words, the new free index, the core post +`JAtZAX`, the MDB loop) and one outcome of the spec's body. + +* **`utRt_recNoResetA`**: the six bundles (`utRt_capsAllA`, `utRt_specAllA`, `utRt_ipcInA`, `utRt_schedInA`, + `utRt_heapAllA`, `utRt_sysAllA`), `J5A` (`utRt_j5A`, `utRt_irqKeptA`) and the assembly `utRt_asmAllA`. + +The grown record: typed heap / ghost / sets `utRtZA` (new TCBs, SCs, VCPUs of types 1 / 5 / 12), heap `H'`, slots +`OS ∪ NewSl`, user words `utUserA H' U`, tables `Tb ∪ UtSchedNewA`, notifications / endpoints / replies grown by the +new objects of types 3 / 2 / 6, reply stacks and SC cores empty / `0` at the new SCs. + +Hypotheses beyond the creation's facts: record edit #5's `nodeHi` / `coresAll` (supplied by projection from the +creation-start record at `utCreate_recA`), and `uw` (record edit #6's `utWin`, the same way). + +The record holds at the spec state after `fl4InvokeUntypedA64`'s `vcpu::init` step (`utInvStA`, membership +`UtInvVcA`): a new VCPU's spec registers match FL4's words (`VcpuStateSnapA`) only there. + +No other named premise. New theorems only. +-/ +import Fl4ProofsA64.P3.UtApSys2A +import Fl4ProofsA64.P3.UtAsmAllA +import Fl4ProofsA64.P3.UtFullJ5A +import Fl4ProofsA64.P3.UtRtDomA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +set_option maxRecDepth 100000 + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H H' : Heap} {OS U0 Ac0 Vn U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {N Ep Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} + {κ : U64 → U64} {n : Nat} {nA : U64} {s s2 q : S} {a a' : A} {σ σ2 : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} {ia ie lo hi : Nat} {slot p sb fi nfi aligned nt us dest off win : U64} + {extra : Aeneas.Std.Array obj.Slot 3#usize} + +/-- The grown typed heap, ghost and sets of a RAM creation without a reset. -/ +abbrev utRecZA (Z : XSetsA) (slot nt us aligned dest off win : U64) : XSetsA := + utRtZA Z ⟨utGA slot (djA dest off) Z.G.chains win.val, utPA slot (djA dest off) Z.G.par win.val⟩ nt us aligned + win.val (UtOfTyA 1 nt us aligned win.val) (UtOfTyA 5 nt us aligned win.val) (UtOfTyA 12 nt us aligned win.val) + +set_option maxHeartbeats 4000000 in +/-- **The record after a RAM creation without a reset** (module docstring). -/ +theorem utRt_recNoResetA + -- the record at the decode's state and A64 P1's creation end + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N Ep Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (ER : S1aRtIn st H Z.Cs OS U0 Ac0 s ia ie slot p sb fi false extra H H' OS s2 q lo hi aligned nt us dest off + win) + (noch : ¬ ∃ v, cdt.ensure_no_children st s slot = ok (.Ok v)) + -- A64 P1's creation top + (arm : ArmQIVAt st H Z.Cs OS ia ie s H' (OS ∪ NewSl false nt us aligned win) Vn q) + (ko : HeapKsOff H' (L.ksb s)) + (j7at : ∀ (Z'' : XSetsA) (σ'' : Fl4SpecA64.State Unit) (m'' : Fl4SpecA64.McState), + UtJ7SplitA st Z Z'' (OS ∪ NewSl false nt us aligned win) s q slot p sb nt us aligned win.val → + J7A st (J7R2A st ar nA) Z'' H' (OS ∪ NewSl false nt us aligned win) q σ'' m'') + -- the caller's restart write + (hvs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → Aligned b ∧ b ≠ stA (L.curW s)) + (hr : ∀ b, Aligned b → b ≠ stA (L.curW s) → L.mem s2 b = L.mem s b) + -- the record at the creation start + (J2 : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N Ep Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s2 a + σ2 m) + (hc : capA st s2 slot = .Untyped p sb fi false) + (W : UtWinZA nt aligned (zA nt.val us) win.val) + (h1 : p.val + fi.val ≤ aligned.val) + (h2 : aligned.val + win.val * 2 ^ (zA nt.val us).val ≤ p.val + 2 ^ sb.val) + (pos : 0 < win.val) (us39 : nt.val = 0 → us.val ≤ 39) (hb0 : 0 < aligned.val) + (hc47 : nt.val = 4 → us.val ≤ 47) + (dstNull : ∀ j < win.val, capA st s2 (djA dest off j) = .Null) + -- the creation's end facts + (w : WritesIn st s2 q (UtCrtWA st H OS s2 nt aligned (zA nt.val us) win.val)) + (WT : nt.val = 1 → WinT st q aligned (zA nt.val us) win.val) + (TW : nt.val = 1 → UtTcbZW st aligned (zA nt.val us) win.val q) + (Rg : nt.val = 1 → UtTcbRegW st aligned (zA nt.val us) win.val q) + (VW : nt.val = 12 → UtVcpuZW st aligned (zA nt.val us) win.val q) + (HW : nt.val = 14 → UtHighZW st aligned (zA nt.val us) win.val q) + (hnv : nfi.val = aligned.val + win.val * 2 ^ (zA nt.val us).val - p.val) + (cap' : capA st q slot = .Untyped p sb nfi false) + (L5 : MdbInvA st (utGA slot (djA dest off) Z.G.chains win.val) (utPA slot (djA dest off) Z.G.par win.val) q ∧ + Fl4ProofsA64.P1.W7.R.ST.IrqRunA st (utGA slot (djA dest off) Z.G.chains win.val) q ∧ + (∀ y ∈ (utGA slot (djA dest off) Z.G.chains win.val).flatten, ∀ k, capA st q y = .IrqHandler k → + y ∈ Z.G.chains.flatten ∧ capA st s2 y = .IrqHandler k)) + -- the spec body + {t : ApiObjectType} (htt : objTypeOfA nt.val = some t) (hus : us.val < 64) (hsc : nt.val = 5 → 7 ≤ us.val) + {srcSlot : CslotPtr} (hxc : cslotAddrA srcSlot = slot) + (hsrcS : ∃ q, srcSlot.1 = specPtr q ∧ Z.th q = some (.cnode srcSlot.2.length)) + {slots : List CslotPtr} (hlen : slots.length = win.val) + (hdst : ∀ d ∈ slots, ∃ q, d.1 = specPtr q ∧ Z.th q = some (.cnode d.2.length) ∧ d.2.length ≠ 0) + (hdstA : ∀ j < win.val, ∃ d ∈ slots, djA dest off j = cslotAddrA d) + {r : Unit × Fl4SpecA64.State Unit} + (hr' : r ∈ (utRtBodyA srcSlot (specPtr p) (specPtr aligned) t us.val slots false σ2).1) + -- the core post + (K : JAtZAX (L := L) AR (utRecZA Z slot nt us aligned dest off win) q a' r.2 + (utRtMA m (UtOfTyA 1 nt us aligned win.val))) + -- record edit #6 + (uw : UtWinCapA st OS s2) + -- record edit #5 + (nodeHi : ∀ i : Fl4SpecA64.Irq, 5376 ≤ i.toNat → σ2.interruptIrqNode i = irqNodeOfA (L.ksb s2) i) + (coresAll : ∀ k < Fl4SpecA64.fl4A64Platform.numNodes, ∃ c ∈ Z.Cs, c.val = k) : + UtInvVcA t (specPtr aligned) us.val slots r.2 ∧ ∃ Ac', JStage3A st AR (utRecZA Z slot nt us aligned dest off win) H' (OS ∪ NewSl false nt us aligned win) + (utUserA H' U) Ac' Pp (Tb ∪ UtSchedNewA nt us aligned win.val) Lv ia ie + (N ∪ UtOfTyA 3 nt us aligned win.val) (Ep ∪ UtOfTyA 2 nt us aligned win.val) + (Rp ∪ UtOfTyA 6 nt us aligned win.val) Bf + (fun c => if c ∈ UtOfTyA 5 nt us aligned win.val then [] else stack c) + (fun c => if c ∈ UtOfTyA 5 nt us aligned win.val then 0#u64 else κ c) n (J5A st) + (J7A st (J7R2A st ar nA)) q a' (utInvStA t (specPtr aligned) us.val slots r.2) (utRtMA m (UtOfTyA 1 nt us aligned win.val)) := by + have U := ER.creation + have hx : slot ∈ OS := U.src + have hn : nt.val < 15 := (objTypeOfA_some htt us.val).1 + have Ec : CrtE st H H.frame OS s2 q slot p sb false aligned nt us dest off win := U.E + have X : DbgX st H s2 nt aligned (zA nt.val us) q win.val := U.X + -- the grown heap + have hgrow : ∀ q k, H q = some k → H' q = some k := U.grow + have hex : ∀ q k, H' q = some k → H q = some k ∨ + ∃ j < win.val, q = bjA aligned (zA nt.val us) j ∧ newKind nt.val us.val = some k := fun q k h => + (U.ex q k h).imp id fun ⟨_, j, hj, e, hk⟩ => ⟨j, hj, e, hk⟩ + have hnew : ∀ j < win.val, ∀ k, newKind nt.val us.val = some k → H' (bjA aligned (zA nt.val us) j) = some k := + U.hn rfl + -- the destination slots + have hdst0 : ∀ d ∈ slots, ∃ q, d.1 = specPtr q ∧ Z.th q = some (.cnode d.2.length) := fun d hd => by + obtain ⟨q, e, h, -⟩ := hdst d hd + exact ⟨q, e, h⟩ + have hdstS : ∀ j, j < win.val → IsSlotA Z.th (djA dest off j) := fun j hj => by + obtain ⟨d, hd, e⟩ := hdstA j hj + obtain ⟨q, e1, hq, -⟩ := hdst d hd + rw [e] + exact AbsSlotA.isSlot J2.j.1.place ⟨q, e1, Or.inl hq⟩ + have hsub : ∀ y, IsSlotA Z.th y → y ∈ OS := J2.j5.2.1 + -- the bundles at the creation start + have KI := utRt_ipcInA J2 hx hc W h1 h2 Ec w hex WT Rg htt hus hxc hsrcS hlen hdst hdstA hsub hr' K nodeHi + have KSc := utRt_schedInA J2 hx hc W h1 h2 hb0 Ec w X WT TW VW htt hus hsc hsrcS hlen hdst0 hr' K KI coresAll + have O : ∀ j < win.val, t ≠ .untyped → + r.2.kheap (specPtr (bjA aligned (zA nt.val us) j)) = some (defaultObject t false us.val σ2.curDomain) := by + intro j hj tn + have o := KI.spec.new (specPtr (bjA aligned (zA nt.val us) j)) ⟨tn, j, hj, rfl⟩ + rw [utNewDA_ne tn] at o + exact o + have newNull := utRt_newNullA J2 hx hc W h1 h2 hn hus htt K O + have IC := utRt_capsAllA J2 hx hc W h1 h2 pos us39 hn hus Ec w hnv cap' hgrow hex hnew K + (fun y hy _ => newNull y hy) + have KSp := utRt_specAllA (G' := ⟨utGA slot (djA dest off) Z.G.chains win.val, + utPA slot (djA dest off) Z.G.par win.val⟩) (Tn := UtOfTyA 1 nt us aligned win.val) + (Cn := UtOfTyA 5 nt us aligned win.val) (Vn := UtOfTyA 12 nt us aligned win.val) (s' := q) + J2 hx hc W h1 h2 w htt hus hxc hsrcS hlen hdst0 hr' + have KH := utRt_heapAllA J2 hx hc W h1 h2 Ec w dstNull hgrow hex hnew HW htt hus hsc hr' IC KSc + obtain ⟨KS, hVs⟩ := utRt_sysAllA J ER noch hb0 arm ko j7at hvs hr J2 hx hc h1 h2 w hgrow hex hnew KSc + -- `J5A` + have hfree : ∀ q, UtNewA false nt us aligned win.val q → Z.th q = none := utRt_ipcFreeA J2 hx hc W h1 h2 + have hirq := utRt_irqKeptA J2 hx hc h1 h2 w + have j5 : J5A st (utRecZA Z slot nt us aligned dest off win) H' (OS ∪ NewSl false nt us aligned win) q r.2 + (utRtMA m (UtOfTyA 1 nt us aligned win.val)) := + utRt_j5A J2.j5 U rfl rfl rfl hus hc47 (by rw [hc]; exact fun h => by cases h) hfree hdstS L5 hirq + have R5 := utRt_rec5A (G' := ⟨utGA slot (djA dest off) Z.G.chains win.val, + utPA slot (djA dest off) Z.G.par win.val⟩) (Cn := UtOfTyA 5 nt us aligned win.val) + (Vn := UtOfTyA 12 nt us aligned win.val) J2 hx hc W h1 h2 uw hn hus hc47 Ec + (fun j hj => by + obtain ⟨d, hd, e⟩ := hdstA j hj + obtain ⟨q, e1, hq, -⟩ := hdst d hd + exact ⟨d, ⟨q, e1, hq⟩, e⟩) hfree newNull hgrow hex arm.2.1 KI.spec (fun x e => (KSc.hdt x e).2.1) + have R6 := utRt_rec6A (G' := ⟨utGA slot (djA dest off) Z.G.chains win.val, + utPA slot (djA dest off) Z.G.par win.val⟩) (Cn := UtOfTyA 5 nt us aligned win.val) + J2 hx hc W h1 h2 hn hus Ec w newNull TW VW htt KI.spec + have vcS := utRt_vcSnapA J2 hx hc W h1 h2 hn hus w VW htt KI.spec + obtain ⟨eτ, Hτ, vmem⟩ := utRt_vcStepA W hn hus htt hlen KI.spec + rw [← eτ] at vcS + exact ⟨vmem, utRt_asmAllA J J2 IC KSp KI KH KSc KS rfl K j5 R5 Hτ R6 vcS + (utRt_domA J2 hx hc (by omega) h2 w ((utRtBody_globA hr').domk.trans Hτ.domk)) + (utRt_ipiA J2 hx hc (by omega) h2 w) + (bufVs_subA J.bufVs KS.ksb hVs)⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtRtRecRstA.lean b/proofs/Fl4ProofsA64/P3/UtRtRecRstA.lean new file mode 100644 index 000000000..80d285a4b --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtRtRecRstA.lean @@ -0,0 +1,349 @@ +/- +P3 (aarch64, A64-P3 Untyped, full-record post, reset path): **the creation of a RAM retype after a reset, with the +whole record after it**, relation level, from the creation start `s3` (the reset's exit; at an unused untyped the +restart state), whose record is over its own sets `Z0` / heap `H1` / slots `OS1`. + +* `utRt_offSetsA`: the record's pools, tables, roots and VCPUs are off a region that holds no object of its heap. +* **`utRt_recRstA`**: the six bundles at the creation start (`utRt_capsAllA`, `utRt_specAllA`, `utRt_ipcInA`, + `utRt_schedInA`, `utRt_heapAllA`), `utRt_sysRstA`, `J5A` and the assembly `utRt_asmAll0A`. +* **`utCreate_recRstA`**: `retype` from the creation start succeeds, and for every outcome of the spec body and every + A64 P1 creation end at the decode's parameters (`S1aRtIn` with `ArmQIVAt`, `HeapKsOff` and the `J7A` hook) the whole + record `JStage3A` holds at the grown heap, slots and sets (`utCreate_recA` over the creation-start sets). + +Premises: those of `utCreate_postA` ([P] `hVP`, `hKt` / `hK0`; [J] `hkw`). New theorems only. +-/ +import Fl4ProofsA64.P3.UtApRstA +import Fl4ProofsA64.P3.UtRtRecTopA +import Fl4ProofsA64.P3.UtRtDomA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +set_option maxRecDepth 100000 + +/-- **The record's page sets are off a region holding none of its heap's objects.** -/ +theorem utRt_offSetsA {S : Type} {st : machine.Store S} [L : LawfulStore st] {Z : XSetsA} {H : Heap} {s : S} + {slot p sb : U64} (K : SetsInHA Z H) (hH : HeapOK H) + (hole : ∀ q k, H q = some k → q.val + 2 ^ k.bits ≤ p.val ∨ p.val + 2 ^ sb.val ≤ q.val) : + (∀ r, (r ∈ Z.Pp ∨ r ∈ Z.S1 ∨ r ∈ Z.T2 ∨ r ∈ Z.V) → UtOffRgA st s slot p sb r) ∧ + (∀ r, (r ∈ Z.X ∨ r ∈ Z.Y) → UtOffRgA st s slot p sb r ∧ UtOffRgA st s slot p sb (wa r 4096#u64)) := by + refine ⟨fun r hr _ => ?_, fun r hr => ?_⟩ + · have m : r ∈ Z.Pp ∪ Z.S1 ∪ Z.T2 ∪ Z.V := by + rcases hr with h | h | h | h + · exact Or.inl (Or.inl (Or.inl h)) + · exact Or.inl (Or.inl (Or.inr h)) + · exact Or.inl (Or.inr h) + · exact Or.inr h + obtain ⟨o, z, ho, h1, h2⟩ := K.pages r m + have g := hole o _ ho + have e : HKind.bits (.other z) = z := rfl + rw [e] at g + omega + · have m : r ∈ Z.X ∪ Z.Y := hr + obtain ⟨o, z, ho, h1, h2⟩ := K.roots r m + have g := hole o _ ho + have f := hH.fits o _ ho + have e : HKind.bits (.other z) = z := rfl + rw [e] at g f + have v : (wa r 4096#u64).val = r.val + 4096 := by + rw [wa_val] + exact Nat.mod_eq_of_lt (by simp; omega) + exact ⟨fun _ => by omega, fun _ => by omega⟩ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z Z0 : XSetsA} {H H1 H' : Heap} {OS OS1 U Ud Ac Ac1 U0 Ac0 Vn Pp Tb Pp0 Tb0 : Set U64} + {Lv Lv0 : U64 → Nat} {N Ep Rp N1 E1 Rp1 : Set U64} {Bf Bf1 : Set (U64 × Fl4SpecA64.Word)} + {stack stack1 : U64 → List U64} {κ κ1 : U64 → U64} {n : Nat} {nA : U64} {s s3 q : S} {a a3 a' : A} + {σ σ3 : Fl4SpecA64.State Unit} {m m3 : Fl4SpecA64.McState} {ia ie lo hi : Nat} + {slot p sb fi nfi aligned nt us dest off win : U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} + +set_option maxHeartbeats 4000000 in +/-- **The record after a RAM creation after a reset** (module docstring). -/ +theorem utRt_recRstA + -- the record at the decode's state and A64 P1's creation end + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N Ep Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (ER : S1aRtIn st H Z.Cs OS U0 Ac0 s ia ie slot p sb fi false extra H1 H' OS1 s3 q lo hi aligned nt us dest off + win) + -- A64 P1's creation top + (arm : ArmQIVAt st H Z.Cs OS ia ie s H' (s1aRtSlots OS1 false nt us aligned win) Vn q) + (ko : HeapKsOff H' (L.ksb s)) + (j7at : ∀ (Z'' : XSetsA) (σ'' : Fl4SpecA64.State Unit) (m'' : Fl4SpecA64.McState), + UtJ7SplitA st Z Z'' (s1aRtSlots OS1 false nt us aligned win) s q slot p sb nt us aligned win.val → + J7A st (J7R2A st ar nA) Z'' H' (s1aRtSlots OS1 false nt us aligned win) q σ'' m'') + -- the creation-start sets, inside the record's + (cs : Z0.Cs = Z.Cs) (pp : Z0.Pp ⊆ Z.Pp) (s1' : Z0.S1 ⊆ Z.S1) (t2 : Z0.T2 ⊆ Z.T2) (x : Z0.X ⊆ Z.X) + (y : Z0.Y ⊆ Z.Y) (v : Z0.V ⊆ Z.V) + (offr : ∀ r, (r ∈ Z0.Pp ∨ r ∈ Z0.S1 ∨ r ∈ Z0.T2 ∨ r ∈ Z0.V) → UtOffRgA st s slot p sb r) + (offr2 : ∀ r, (r ∈ Z0.X ∨ r ∈ Z0.Y) → UtOffRgA st s slot p sb r ∧ UtOffRgA st s slot p sb (wa r 4096#u64)) + (vc0 : VcCap st Z0.V OS s) + -- the record at the creation start + (J2 : JStage3A st AR Z0 H1 OS1 Ud Ac1 Pp0 Tb0 Lv0 ia ie N1 E1 Rp1 Bf1 stack1 κ1 n (J5A st) + (J7A st (J7R2A st ar nA)) s3 a3 σ3 m3) + (hc : capA st s3 slot = .Untyped p sb 0#u64 false) + (W : UtWinZA nt aligned (zA nt.val us) win.val) + (h1 : p.val ≤ aligned.val) + (h2 : aligned.val + win.val * 2 ^ (zA nt.val us).val ≤ p.val + 2 ^ sb.val) + (pos : 0 < win.val) (us39 : nt.val = 0 → us.val ≤ 39) (hb0 : 0 < aligned.val) + (hc47 : nt.val = 4 → us.val ≤ 47) + (dstNull : ∀ j < win.val, capA st s3 (djA dest off j) = .Null) + -- the creation's end facts + (Ec : CrtE st H1 H1.frame OS1 s3 q slot p sb false aligned nt us dest off win) + (X : DbgX st H1 s3 nt aligned (zA nt.val us) q win.val) + (w : WritesIn st s3 q (UtCrtWA st H1 OS1 s3 nt aligned (zA nt.val us) win.val)) + (WT : nt.val = 1 → WinT st q aligned (zA nt.val us) win.val) + (TW : nt.val = 1 → UtTcbZW st aligned (zA nt.val us) win.val q) + (Rg : nt.val = 1 → UtTcbRegW st aligned (zA nt.val us) win.val q) + (VW : nt.val = 12 → UtVcpuZW st aligned (zA nt.val us) win.val q) + (HW : nt.val = 14 → UtHighZW st aligned (zA nt.val us) win.val q) + (hnv : nfi.val = aligned.val + win.val * 2 ^ (zA nt.val us).val - p.val) + (cap' : capA st q slot = .Untyped p sb nfi false) + (L5 : MdbInvA st (utGA slot (djA dest off) Z0.G.chains win.val) (utPA slot (djA dest off) Z0.G.par win.val) q ∧ + Fl4ProofsA64.P1.W7.R.ST.IrqRunA st (utGA slot (djA dest off) Z0.G.chains win.val) q ∧ + (∀ y ∈ (utGA slot (djA dest off) Z0.G.chains win.val).flatten, ∀ k, capA st q y = .IrqHandler k → + y ∈ Z0.G.chains.flatten ∧ capA st s3 y = .IrqHandler k)) + -- the spec body + {t : ApiObjectType} (htt : objTypeOfA nt.val = some t) (hus : us.val < 64) (hsc : nt.val = 5 → 7 ≤ us.val) + {srcSlot : CslotPtr} (hxc : cslotAddrA srcSlot = slot) + (hsrcS : ∃ q, srcSlot.1 = specPtr q ∧ Z0.th q = some (.cnode srcSlot.2.length)) + {slots : List CslotPtr} (hlen : slots.length = win.val) + (hdst : ∀ d ∈ slots, ∃ q, d.1 = specPtr q ∧ Z0.th q = some (.cnode d.2.length) ∧ d.2.length ≠ 0) + (hdstA : ∀ j < win.val, ∃ d ∈ slots, djA dest off j = cslotAddrA d) + {r : Unit × Fl4SpecA64.State Unit} + (hr' : r ∈ (utRtBodyA srcSlot (specPtr p) (specPtr aligned) t us.val slots false σ3).1) + -- the core post + (K : JAtZAX (L := L) AR (utRecZA Z0 slot nt us aligned dest off win) q a' r.2 + (utRtMA m3 (UtOfTyA 1 nt us aligned win.val))) + -- record edit #6 + (uw : UtWinCapA st OS1 s3) + -- record edit #5 + (nodeHi : ∀ i : Fl4SpecA64.Irq, 5376 ≤ i.toNat → σ3.interruptIrqNode i = irqNodeOfA (L.ksb s3) i) + (coresAll : ∀ k < Fl4SpecA64.fl4A64Platform.numNodes, ∃ c ∈ Z0.Cs, c.val = k) : + UtInvVcA t (specPtr aligned) us.val slots r.2 ∧ ∃ Ac', JStage3A st AR (utRecZA Z0 slot nt us aligned dest off win) H' (s1aRtSlots OS1 false nt us aligned win) + (utUserA H' U) Ac' Pp0 (Tb0 ∪ UtSchedNewA nt us aligned win.val) Lv0 ia ie + (N1 ∪ UtOfTyA 3 nt us aligned win.val) (E1 ∪ UtOfTyA 2 nt us aligned win.val) + (Rp1 ∪ UtOfTyA 6 nt us aligned win.val) Bf1 + (fun c => if c ∈ UtOfTyA 5 nt us aligned win.val then [] else stack1 c) + (fun c => if c ∈ UtOfTyA 5 nt us aligned win.val then 0#u64 else κ1 c) n (J5A st) + (J7A st (J7R2A st ar nA)) q a' (utInvStA t (specPtr aligned) us.val slots r.2) (utRtMA m3 (UtOfTyA 1 nt us aligned win.val)) := by + have U' := ER.creation + have hx : slot ∈ OS1 := U'.src + have hn : nt.val < 15 := (objTypeOfA_some htt us.val).1 + have h1' : p.val + (0#u64 : U64).val ≤ aligned.val := by simpa using h1 + -- the grown heap + have hgrow : ∀ q k, H1 q = some k → H' q = some k := U'.grow + have hex : ∀ q k, H' q = some k → H1 q = some k ∨ + ∃ j < win.val, q = bjA aligned (zA nt.val us) j ∧ newKind nt.val us.val = some k := fun q k h => + (U'.ex q k h).imp id fun ⟨_, j, hj, e, hk⟩ => ⟨j, hj, e, hk⟩ + have hnew : ∀ j < win.val, ∀ k, newKind nt.val us.val = some k → H' (bjA aligned (zA nt.val us) j) = some k := + U'.hn rfl + -- the destination slots + have hdst0 : ∀ d ∈ slots, ∃ q, d.1 = specPtr q ∧ Z0.th q = some (.cnode d.2.length) := fun d hd => by + obtain ⟨q, e, h, -⟩ := hdst d hd + exact ⟨q, e, h⟩ + have hdstS : ∀ j, j < win.val → IsSlotA Z0.th (djA dest off j) := fun j hj => by + obtain ⟨d, hd, e⟩ := hdstA j hj + obtain ⟨q, e1, hq, -⟩ := hdst d hd + rw [e] + exact AbsSlotA.isSlot J2.j.1.place ⟨q, e1, Or.inl hq⟩ + have hsub : ∀ y, IsSlotA Z0.th y → y ∈ OS1 := J2.j5.2.1 + -- the bundles at the creation start + have KI := utRt_ipcInA J2 hx hc W h1' h2 Ec w hex WT Rg htt hus hxc hsrcS hlen hdst hdstA hsub hr' K nodeHi + have KSc := utRt_schedInA J2 hx hc W h1' h2 hb0 Ec w X WT TW VW htt hus hsc hsrcS hlen hdst0 hr' K KI coresAll + have O : ∀ j < win.val, t ≠ .untyped → + r.2.kheap (specPtr (bjA aligned (zA nt.val us) j)) = some (defaultObject t false us.val σ3.curDomain) := by + intro j hj tn + have o := KI.spec.new (specPtr (bjA aligned (zA nt.val us) j)) ⟨tn, j, hj, rfl⟩ + rw [utNewDA_ne tn] at o + exact o + have newNull := utRt_newNullA J2 hx hc W h1' h2 hn hus htt K O + have IC := utRt_capsAllA J2 hx hc W h1' h2 pos us39 hn hus Ec w hnv cap' hgrow hex hnew K + (fun y hy _ => newNull y hy) + have KSp := utRt_specAllA (G' := ⟨utGA slot (djA dest off) Z0.G.chains win.val, + utPA slot (djA dest off) Z0.G.par win.val⟩) (Tn := UtOfTyA 1 nt us aligned win.val) + (Cn := UtOfTyA 5 nt us aligned win.val) (Vn := UtOfTyA 12 nt us aligned win.val) (s' := q) + J2 hx hc W h1' h2 w htt hus hxc hsrcS hlen hdst0 hr' + have KH := utRt_heapAllA J2 hx hc W h1' h2 Ec w dstNull hgrow hex hnew HW htt hus hsc hr' IC KSc + obtain ⟨KS, hVs⟩ := utRt_sysRstA J ER hb0 arm ko j7at cs pp s1' t2 x y v offr offr2 vc0 J2 hx hc h1 h2 w hgrow + hex hnew KSc + -- `J5A` + have hfree : ∀ q, UtNewA false nt us aligned win.val q → Z0.th q = none := utRt_ipcFreeA J2 hx hc W h1' h2 + have hirq := utRt_irqKeptA J2 hx hc h1' h2 w + have j5 : J5A st (utRecZA Z0 slot nt us aligned dest off win) H' (s1aRtSlots OS1 false nt us aligned win) q r.2 + (utRtMA m3 (UtOfTyA 1 nt us aligned win.val)) := + utRt_j5A J2.j5 U' rfl rfl rfl hus hc47 (by rw [hc]; exact fun h => by cases h) hfree hdstS L5 hirq + have R5 := utRt_rec5A (G' := ⟨utGA slot (djA dest off) Z0.G.chains win.val, + utPA slot (djA dest off) Z0.G.par win.val⟩) (Cn := UtOfTyA 5 nt us aligned win.val) + (Vn := UtOfTyA 12 nt us aligned win.val) J2 hx hc W h1' h2 uw hn hus hc47 Ec + (fun j hj => by + obtain ⟨d, hd, e⟩ := hdstA j hj + obtain ⟨q, e1, hq, -⟩ := hdst d hd + exact ⟨d, ⟨q, e1, hq⟩, e⟩) hfree newNull hgrow hex (cs.symm ▸ arm.2.1) KI.spec (fun x e => (KSc.hdt x e).2.1) + have R6 := utRt_rec6A (G' := ⟨utGA slot (djA dest off) Z0.G.chains win.val, + utPA slot (djA dest off) Z0.G.par win.val⟩) (Cn := UtOfTyA 5 nt us aligned win.val) + J2 hx hc W h1' h2 hn hus Ec w newNull TW VW htt KI.spec + have vcS := utRt_vcSnapA J2 hx hc W h1' h2 hn hus w VW htt KI.spec + obtain ⟨eτ, Hτ, vmem⟩ := utRt_vcStepA W hn hus htt hlen KI.spec + rw [← eτ] at vcS + exact ⟨vmem, utRt_asmAll0A J J2 IC KSp KI KH KSc KS rfl K j5 R5 Hτ R6 vcS + (utRt_domA J2 hx hc h1' h2 w ((utRtBody_globA hr').domk.trans Hτ.domk)) + (utRt_ipiA J2 hx hc h1' h2 w) + (bufVs_subA J2.bufVs KSc.hks hVs)⟩ + +set_option maxHeartbeats 4000000 in +/-- **The creation after a reset with the whole record after it** (module docstring). -/ +theorem utCreate_recRstA (hA : P0.ArchTotal ar) + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N Ep Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (J2 : JStage3A st AR Z0 H1 OS1 Ud Ac1 Pp0 Tb0 Lv0 ia ie N1 E1 Rp1 Bf1 stack1 κ1 n (J5A st) + (J7A st (J7R2A st ar nA)) s3 a3 σ3 m3) + {aligned nt us dest off win fiv : U64} {t : ApiObjectType} {rd : Nat} {reset : Bool} + {sd : S} {σ0 : State Unit} {sl : CslotPtr} + (C : CrtP st H1 Z0.Cs H1.frame OS1 s3 (cslotAddrA sl) p sb 0#u64 false aligned nt us dest off win) + (X : UtCtxA st Z0.th sd σ0 sl p sb fi false nt us dest off win t rd reset fiv) + (hcap : capA st sd (cslotAddrA sl) = .Untyped p sb fi false) + (hsrc : ∃ q, sl.1 = specPtr q ∧ Z0.th q = some (.cnode sl.2.length)) + {H0 Hx : Heap} {OS0 : Set U64} + (hUe : untyped.retype st ar s3 a3 (cslotAddrA sl) false aligned nt us dest off win false ⦃ r => + ∃ lo hi : Nat, UtEnd st H0 Z0.Cs OS0 s (cslotAddrA sl) p sb false H1 Hx OS1 s3 r.2.1 lo hi aligned nt us dest off + win ⦄) + (hb0 : 0 < aligned.val) (hal8 : aligned.val % 8 = 0) + (hpf : aligned.val + win.val * 2 ^ (zA nt.val us).val ≤ 2 ^ 64) + (hal2 : aligned.val % 2 ^ (zA nt.val us).val = 0) + (hP' : PlaceA (utRtThA Z0.th false nt us aligned win.val) (L.ksb s3)) + (hno : ∀ j, j < win.val → utRangedA nt.val → SpNoOvA (bjA aligned (zA nt.val us) j) (zOfA nt.val us.val)) + (hVP : ∀ a1 : A, UtVcpuPlatA ar a1) {k : U64 → U64} + (hKt : ∀ (a1 : A) (i : U64), i.val < 511 → ar.kernel_ttbr1_entry a1 0#u64 i = ok (k i)) + (hK0 : ∀ i : U64, i.val < 256 → k i = 0#u64) + (hkw : nt.val = 14 → 18446603336221196288 ≤ aligned.val) + -- the creation-start sets, inside the record's + (cs : Z0.Cs = Z.Cs) (pp : Z0.Pp ⊆ Z.Pp) (s1' : Z0.S1 ⊆ Z.S1) (t2 : Z0.T2 ⊆ Z.T2) (x : Z0.X ⊆ Z.X) + (y : Z0.Y ⊆ Z.Y) (v : Z0.V ⊆ Z.V) + (offr : ∀ r, (r ∈ Z0.Pp ∨ r ∈ Z0.S1 ∨ r ∈ Z0.T2 ∨ r ∈ Z0.V) → UtOffRgA st s (cslotAddrA sl) p sb r) + (offr2 : ∀ r, (r ∈ Z0.X ∨ r ∈ Z0.Y) → UtOffRgA st s (cslotAddrA sl) p sb r ∧ + UtOffRgA st s (cslotAddrA sl) p sb (wa r 4096#u64)) + (vc0 : VcCap st Z0.V OS s) : + untyped.retype st ar s3 a3 (cslotAddrA sl) false aligned nt us dest off win false ⦃ res => + res.1 = .Ok () ∧ + ∀ r ∈ (utRtBodyA sl (specPtr p) (specPtr aligned) t us.val (windowSlotsA dest rd off.val win.val) false σ3).1, + ∀ (H'' : Heap) (Vn U0 Ac0 : Set U64) (lo hi : Nat) (extra : Aeneas.Std.Array obj.Slot 3#usize), + S1aRtIn st H Z.Cs OS U0 Ac0 s ia ie (cslotAddrA sl) p sb fi false extra H1 H'' OS1 s3 res.2.1 lo hi aligned + nt us dest off win → + ArmQIVAt st H Z.Cs OS ia ie s H'' (s1aRtSlots OS1 false nt us aligned win) Vn res.2.1 → + HeapKsOff H'' (L.ksb s) → + (∀ (Z'' : XSetsA) (σ'' : State Unit) (m'' : McState), + UtJ7SplitA st Z Z'' (s1aRtSlots OS1 false nt us aligned win) s res.2.1 (cslotAddrA sl) p sb nt us aligned + win.val → + J7A st (J7R2A st ar nA) Z'' H'' (s1aRtSlots OS1 false nt us aligned win) res.2.1 σ'' m'') → + UtInvVcA t (specPtr aligned) us.val (windowSlotsA dest rd off.val win.val) r.2 ∧ + ∃ Ac', JStage3A st AR (utRecZA Z0 (cslotAddrA sl) nt us aligned dest off win) H'' + (s1aRtSlots OS1 false nt us aligned win) (utUserA H'' U) Ac' Pp0 + (Tb0 ∪ UtSchedNewA nt us aligned win.val) Lv0 + ia ie (N1 ∪ UtOfTyA 3 nt us aligned win.val) (E1 ∪ UtOfTyA 2 nt us aligned win.val) + (Rp1 ∪ UtOfTyA 6 nt us aligned win.val) Bf1 + (fun c => if c ∈ UtOfTyA 5 nt us aligned win.val then [] else stack1 c) + (fun c => if c ∈ UtOfTyA 5 nt us aligned win.val then 0#u64 else κ1 c) n (J5A st) + (J7A st (J7R2A st ar nA)) res.2.1 res.2.2 + (utInvStA t (specPtr aligned) us.val (windowSlotsA dest rd off.val win.val) r.2) (utRtMA m3 (UtOfTyA 1 nt us aligned win.val)) ⦄ := by + have hsub : ∀ y, IsSlotA Z0.th y → y ∈ OS1 := J2.j5.2.1 + have hnul : ∀ y ∈ OS1, ¬ IsSlotA Z0.th y → capA st s3 y = .Null := J2.j5.2.2 + have hsO : cslotAddrA sl ∈ OS1 := C.src + have hc2 : capA st s3 (cslotAddrA sl) = .Untyped p sb 0#u64 false := C.cap + have hTr2 : TreeA st OS1 Z0.G.chains Z0.G.par s3 := J2.j.1.cdt.tree.grow hsub fun y hy hn => hnul y hy hn + have hrv : obj.Slot.revocable st (cslotAddrA sl) s3 = ok true := + J2.sys.sysl.sys.stz.1.2.1 _ hsO (by rw [hc2]; rfl) + have hrev : Fl4ProofsA64.P1.W7.R.ST.revA st s3 (cslotAddrA sl) = true := + okinj ((slot_rev_eqA s3 (cslotAddrA sl)).symm.trans hrv) + obtain ⟨-, -, -, h1, h2, -⟩ := id C.ar + have hus : us.val < 64 := X.size.1 + have hn : nt.val < 15 := C.hn + have hc47 : nt.val = 4 → us.val ≤ 47 := fun h4 => by + have h := X.size.2.1 + rw [h4] at h + simp [zOfA] at h + omega + have hsc : nt.val = 5 → 7 ≤ us.val := fun h5 => by have := X.size.2.2.2.2; omega + have us39 : nt.val = 0 → us.val ≤ 39 := fun h0 => by + have g := X.zle + have g2 := X.sizeBits + rw [h0] at g + simp [zOfA] at g + omega + have pos : 0 < win.val := X.window.1 + have W : UtWinZA nt aligned (zA nt.val us) win.val := by + refine ⟨C.hz, hpf, hal2, fun h => ?_, fun h => ?_⟩ + · rw [zA_valA nt us hn hus] + unfold zOfA + rcases h with h | h <;> rw [h] <;> simp + · rw [zA_valA nt us hn hus] + unfold zOfA + rcases h with h | h <;> rw [h] <;> simp + obtain ⟨hdst, hdstA⟩ := utCtx_dstLenA X + have hlen := utWindow_lengthA dest rd off.val win.val + -- the new TCBs' zero and register words + have tcbW : WP.spec (untyped.retype st ar s3 a3 (cslotAddrA sl) false aligned nt us dest off win false) + (fun res => nt.val = 1 → UtTcbZW st aligned (zA nt.val us) win.val res.2.1 ∧ + UtTcbRegW st aligned (zA nt.val us) win.val res.2.1) := by + by_cases h : nt.val = 1 + · have al4 : aligned.val % 4096 = 0 := by + have g := hal2 + rwa [h, zA_1 us] at g + have kv : (L.ksb s3).val + 788736 ≤ 2 ^ 64 := J2.sys.ent.ksw + refine WP.spec_mono (utRetype_tcbCarryA (ar := ar) hA C hTr2 h al4 hb0 J2.j.1.rest.curDom.2 + (by + have h := (J2.sq.nodes _ J2.sq.cur).align + simp only [LawfulStore.nodeBase] at h + omega) (by omega) ?_ ?_ ?_ a3) (fun _ g _ => g) + · have g := (J2.sys.sysl.sys.stz.2.2 _ hsO p sb 0#u64 hc2).2 + rcases g with g | g <;> omega + · rintro ⟨y, hy, g1, g2⟩ + exact J2.sys.sysl.ly.kslo 8#u64 (by simp) y hy ⟨g1, g2⟩ + · intro y hy + have g := J2.ko.1 y .tcb hy (fun _ e => by cases e) + rw [show HKind.tcb.bits = 12 from rfl] at g + rcases g with g | g <;> omega + · exact WP.spec_mono (utRetype_nfiA (ar := ar) hA C a3) (fun _ _ g => absurd g h) + have vcW : WP.spec (untyped.retype st ar s3 a3 (cslotAddrA sl) false aligned nt us dest off win false) + (fun res => nt.val = 12 → UtVcpuZW st aligned (zA nt.val us) win.val res.2.1) := by + by_cases h : nt.val = 12 + · have al4 : aligned.val % 4096 = 0 := by + have g := hal2 + have ez : (zA nt.val us).val = 12 := by + rw [zA_valA nt us hn hus, h]; rfl + rwa [ez] at g + exact WP.spec_mono (utRetype_vcpuWordsA (ar := ar) hA C hTr2 h al4 hVP a3) (fun _ g _ => g) + · exact WP.spec_mono (utRetype_nfiA (ar := ar) hA C a3) (fun _ _ g => absurd g h) + have hiW : WP.spec (untyped.retype st ar s3 a3 (cslotAddrA sl) false aligned nt us dest off win false) + (fun res => nt.val = 14 → UtHighZW st aligned (zA nt.val us) win.val res.2.1) := by + by_cases h : nt.val = 14 + · have al4 : aligned.val % 4096 = 0 := by + have g := hal2 + rw [h, zA_14 us] at g + change aligned.val % 8192 = 0 at g + omega + exact WP.spec_mono (utRetype_highWordsA (ar := ar) hA C hTr2 h al4 (hkw h) hKt hK0 a3) + (fun _ g _ => g) + · exact WP.spec_mono (utRetype_nfiA (ar := ar) hA C a3) (fun _ _ g => absurd g h) + -- the creation + refine WP.spec_mono (WP.spec_and + (utCreate_postA hA J2 C X hcap hsrc hUe hb0 hal8 hpf hal2 hP' hno hVP hKt hK0 hkw) + (WP.spec_and (utRetype_frameA (ar := ar) hA C hTr2 a3) + (WP.spec_and (untyped.retype.liveRWH (ar := ar) hA C hb0 a3) + (WP.spec_and tcbW (WP.spec_and vcW (WP.spec_and hiW (WP.spec_and (utRetype_nfiA (ar := ar) hA C a3) + (utRt_j5LoopA hA C hTr2 J2.j5.1.1 J2.j5.1.2.1 hrev a3)))))))) ?_ + rintro res ⟨⟨hok, Kall⟩, w, ⟨Ec, Xd, WT, -⟩, TR, VW, HW, ⟨nfi, hnv, cap'⟩, L5⟩ + refine ⟨hok, fun r hr H'' Vn U0 Ac0 lo hi extra ER arm ko j7at => ?_⟩ + exact utRt_recRstA J ER arm ko j7at cs pp s1' t2 x y v offr offr2 vc0 J2 hc2 W (by simpa using h1) h2 pos us39 hb0 + hc47 C.dn Ec Xd w WT (fun h => (TR h).1) (fun h => (TR h).2) VW HW hnv cap' L5 X.ty hus hsc rfl hsrc hlen hdst + hdstA hr (Kall r hr) J2.utWin J2.j.1.irq.nodeHi J2.coresAll + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtRtRecTopA.lean b/proofs/Fl4ProofsA64/P3/UtRtRecTopA.lean new file mode 100644 index 000000000..17bba403d --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtRtRecTopA.lean @@ -0,0 +1,206 @@ +/- +P3 (aarch64, A64-P3 Untyped, full-record post): **the creation of a RAM retype without a reset, with the whole record +after it**, relation level, from the creation start (after the caller's restart write). + +* **`utCreate_recA`**: from the record at the decode's state and at the creation start, A64 P1's `CrtP` there and the + decode's context (`UtCtxA`), `retype` succeeds, and for every outcome of the spec body and every A64 P1 creation end + at the decode's parameters (`S1aRtIn` with `utRt_j7TopA`'s `ArmQIVAt`, `HeapKsOff` and `J7A` hook) the whole record + `JStage3A` holds at the grown heap, slots and sets (`utRt_recNoResetA`). + +Every creation fact `utRt_recNoResetA` takes is supplied here from the creation's own posts at the same parameters: +the core post (`utCreate_postA`), the write frame (`utRetype_frameA`), the new TCBs' window words (`liveRWH`), their +zero and register words (`utRetype_tcbCarryA`), the new VCPUs' and `HighVSpace`s' words (`utRetype_vcpuWordsA`, +`utRetype_highWordsA`), the new free index (`utRetype_nfiA`) and the MDB loop (`utRt_j5LoopA`); the destination slots, +the window and the size bounds from `CrtP` and `UtCtxA`. + +Premises: those of `utCreate_postA` ([P] `hVP`, `hKt` / `hK0`; [J] `hkw`). No new named premise. New theorems only. +-/ +import Fl4ProofsA64.P3.UtRtRecA +import Fl4ProofsA64.P3.UtRtCreateA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +set_option maxRecDepth 100000 + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N Ep Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s s2 : S} {a : A} {σ σ2 : State Unit} {m : McState} + +/-- The destination slots of a decode's window: CNode slots of a nonzero radix, one per window index. -/ +theorem utCtx_dstLenA {sd : S} {σ0 : State Unit} {sl : CslotPtr} {p sb fi : U64} {dev : Bool} + {nt us dest off win fiv : U64} {t : ApiObjectType} {rd : Nat} {reset : Bool} + (X : UtCtxA st Z.th sd σ0 sl p sb fi dev nt us dest off win t rd reset fiv) : + (∀ d ∈ windowSlotsA dest rd off.val win.val, + ∃ q, d.1 = specPtr q ∧ Z.th q = some (.cnode d.2.length) ∧ d.2.length ≠ 0) ∧ + (∀ j < win.val, ∃ d ∈ windowSlotsA dest rd off.val win.val, djA dest off j = cslotAddrA d) := by + refine ⟨fun d hd => ?_, fun j hj => ?_⟩ + · obtain ⟨j, hj, rfl⟩ := List.getElem_of_mem hd + obtain ⟨-, q, e, hq⟩ := utCtx_dstA X j hj + refine ⟨q, e, hq, ?_⟩ + have hw : j < win.val := by rwa [utWindow_lengthA] at hj + have hr : rd < 64 := X.cnode.2.1 + have ew := X.window.2.2 + have es : (windowSlotsA dest rd off.val win.val)[j]'hj = (specPtr dest, natToCref rd (off.val + j)) := by + simp [windowSlotsA] + rw [es] + show (natToCref rd (off.val + j)).length ≠ 0 + rw [natToCref_length_utA rd _ (by omega)] + exact X.cnode.2.2.1 + · have h : j < (windowSlotsA dest rd off.val win.val).length := by rwa [utWindow_lengthA] + exact ⟨_, List.getElem_mem h, (utCtx_dstA X j h).1.symm⟩ + +set_option maxHeartbeats 4000000 in +/-- **The creation with the whole record after it** (module docstring). -/ +theorem utCreate_recA (hA : P0.ArchTotal ar) + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N Ep Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (J2 : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N Ep Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s2 a + σ2 m) + {p sb fi : U64} {aligned nt us dest off win fiv : U64} {t : ApiObjectType} {rd : Nat} {reset : Bool} + {sd s0 : S} {σ0 : State Unit} {sl : CslotPtr} {H0 H' : Heap} {OS0 : Set U64} + (C : CrtP st H Z.Cs H.frame OS s2 (cslotAddrA sl) p sb fi false aligned nt us dest off win) + (X : UtCtxA st Z.th sd σ0 sl p sb fi false nt us dest off win t rd reset fiv) + (hcap : capA st sd (cslotAddrA sl) = .Untyped p sb fi false) + (hsrc : ∃ q, sl.1 = specPtr q ∧ Z.th q = some (.cnode sl.2.length)) + (hUe : untyped.retype st ar s2 a (cslotAddrA sl) false aligned nt us dest off win false ⦃ r => + ∃ lo hi : Nat, UtEnd st H0 Z.Cs OS0 s0 (cslotAddrA sl) p sb false H H' OS s2 r.2.1 lo hi aligned nt us dest off + win ⦄) + (hb0 : 0 < aligned.val) (hal8 : aligned.val % 8 = 0) + (hpf : aligned.val + win.val * 2 ^ (zA nt.val us).val ≤ 2 ^ 64) + (hal2 : aligned.val % 2 ^ (zA nt.val us).val = 0) + (hP' : PlaceA (utRtThA Z.th false nt us aligned win.val) (L.ksb s2)) + (hno : ∀ j, j < win.val → utRangedA nt.val → SpNoOvA (bjA aligned (zA nt.val us) j) (zOfA nt.val us.val)) + (hVP : ∀ a1 : A, UtVcpuPlatA ar a1) {k : U64 → U64} + (hKt : ∀ (a1 : A) (i : U64), i.val < 511 → ar.kernel_ttbr1_entry a1 0#u64 i = ok (k i)) + (hK0 : ∀ i : U64, i.val < 256 → k i = 0#u64) + (hkw : nt.val = 14 → 18446603336221196288 ≤ aligned.val) + -- the decode without a reset and the caller's restart write + (noch : ¬ ∃ v, cdt.ensure_no_children st s (cslotAddrA sl) = ok (.Ok v)) + (hvs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → Aligned b ∧ b ≠ stA (L.curW s)) + (hrw : ∀ b, Aligned b → b ≠ stA (L.curW s) → L.mem s2 b = L.mem s b) : + untyped.retype st ar s2 a (cslotAddrA sl) false aligned nt us dest off win false ⦃ res => + res.1 = .Ok () ∧ + ∀ r ∈ (utRtBodyA sl (specPtr p) (specPtr aligned) t us.val (windowSlotsA dest rd off.val win.val) false σ2).1, + ∀ (H'' : Heap) (Vn U0 Ac0 : Set U64) (lo hi : Nat) (extra : Aeneas.Std.Array obj.Slot 3#usize), + S1aRtIn st H Z.Cs OS U0 Ac0 s ia ie (cslotAddrA sl) p sb fi false extra H H'' OS s2 res.2.1 lo hi aligned + nt us dest off win → + ArmQIVAt st H Z.Cs OS ia ie s H'' (OS ∪ NewSl false nt us aligned win) Vn res.2.1 → + HeapKsOff H'' (L.ksb s) → + (∀ (Z'' : XSetsA) (σ'' : State Unit) (m'' : McState), + UtJ7SplitA st Z Z'' (OS ∪ NewSl false nt us aligned win) s res.2.1 (cslotAddrA sl) p sb nt us aligned + win.val → + J7A st (J7R2A st ar nA) Z'' H'' (OS ∪ NewSl false nt us aligned win) res.2.1 σ'' m'') → + UtInvVcA t (specPtr aligned) us.val (windowSlotsA dest rd off.val win.val) r.2 ∧ + ∃ Ac', JStage3A st AR (utRecZA Z (cslotAddrA sl) nt us aligned dest off win) H'' + (OS ∪ NewSl false nt us aligned win) (utUserA H'' U) Ac' Pp (Tb ∪ UtSchedNewA nt us aligned win.val) Lv + ia ie (N ∪ UtOfTyA 3 nt us aligned win.val) (Ep ∪ UtOfTyA 2 nt us aligned win.val) + (Rp ∪ UtOfTyA 6 nt us aligned win.val) Bf + (fun c => if c ∈ UtOfTyA 5 nt us aligned win.val then [] else stack c) + (fun c => if c ∈ UtOfTyA 5 nt us aligned win.val then 0#u64 else κ c) n (J5A st) + (J7A st (J7R2A st ar nA)) res.2.1 res.2.2 + (utInvStA t (specPtr aligned) us.val (windowSlotsA dest rd off.val win.val) r.2) (utRtMA m (UtOfTyA 1 nt us aligned win.val)) ⦄ := by + have hsub : ∀ y, IsSlotA Z.th y → y ∈ OS := J2.j5.2.1 + have hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s2 y = .Null := J2.j5.2.2 + have hsO : cslotAddrA sl ∈ OS := C.src + have hc2 : capA st s2 (cslotAddrA sl) = .Untyped p sb fi false := C.cap + have hTr2 : TreeA st OS Z.G.chains Z.G.par s2 := J2.j.1.cdt.tree.grow hsub fun y hy hn => hnul y hy hn + have hrv : obj.Slot.revocable st (cslotAddrA sl) s2 = ok true := + J2.sys.sysl.sys.stz.1.2.1 _ hsO (by rw [hc2]; rfl) + have hrev : Fl4ProofsA64.P1.W7.R.ST.revA st s2 (cslotAddrA sl) = true := + okinj ((slot_rev_eqA s2 (cslotAddrA sl)).symm.trans hrv) + obtain ⟨-, -, -, h1, h2, -⟩ := id C.ar + have hus : us.val < 64 := X.size.1 + have hn : nt.val < 15 := C.hn + have hc47 : nt.val = 4 → us.val ≤ 47 := fun h4 => by + have h := X.size.2.1 + rw [h4] at h + simp [zOfA] at h + omega + have hsc : nt.val = 5 → 7 ≤ us.val := fun h5 => by have := X.size.2.2.2.2; omega + have us39 : nt.val = 0 → us.val ≤ 39 := fun h0 => by + have g := X.zle + have g2 := X.sizeBits + rw [h0] at g + simp [zOfA] at g + omega + have pos : 0 < win.val := X.window.1 + have W : UtWinZA nt aligned (zA nt.val us) win.val := by + refine ⟨C.hz, hpf, hal2, fun h => ?_, fun h => ?_⟩ + · rw [zA_valA nt us hn hus] + unfold zOfA + rcases h with h | h <;> rw [h] <;> simp + · rw [zA_valA nt us hn hus] + unfold zOfA + rcases h with h | h <;> rw [h] <;> simp + obtain ⟨hdst, hdstA⟩ := utCtx_dstLenA X + have hlen := utWindow_lengthA dest rd off.val win.val + -- the new TCBs' zero and register words + have tcbW : WP.spec (untyped.retype st ar s2 a (cslotAddrA sl) false aligned nt us dest off win false) + (fun res => nt.val = 1 → UtTcbZW st aligned (zA nt.val us) win.val res.2.1 ∧ + UtTcbRegW st aligned (zA nt.val us) win.val res.2.1) := by + by_cases h : nt.val = 1 + · have al4 : aligned.val % 4096 = 0 := by + have g := hal2 + rwa [h, zA_1 us] at g + have kv : (L.ksb s2).val + 788736 ≤ 2 ^ 64 := J2.sys.ent.ksw + refine WP.spec_mono (utRetype_tcbCarryA (ar := ar) hA C hTr2 h al4 hb0 J2.j.1.rest.curDom.2 + (by + have h := (J2.sq.nodes _ J2.sq.cur).align + simp only [LawfulStore.nodeBase] at h + omega) (by omega) ?_ ?_ ?_ a) (fun _ g _ => g) + · have g := (J2.sys.sysl.sys.stz.2.2 _ hsO p sb fi hc2).2 + rcases g with g | g <;> omega + · rintro ⟨y, hy, g1, g2⟩ + exact J2.sys.sysl.ly.kslo 8#u64 (by simp) y hy ⟨g1, g2⟩ + · intro y hy + have g := J2.ko.1 y .tcb hy (fun _ e => by cases e) + rw [show HKind.tcb.bits = 12 from rfl] at g + rcases g with g | g <;> omega + · exact WP.spec_mono (utRetype_nfiA (ar := ar) hA C a) (fun _ _ g => absurd g h) + have vcW : WP.spec (untyped.retype st ar s2 a (cslotAddrA sl) false aligned nt us dest off win false) + (fun res => nt.val = 12 → UtVcpuZW st aligned (zA nt.val us) win.val res.2.1) := by + by_cases h : nt.val = 12 + · have al4 : aligned.val % 4096 = 0 := by + have g := hal2 + have ez : (zA nt.val us).val = 12 := by + rw [zA_valA nt us hn hus, h]; rfl + rwa [ez] at g + exact WP.spec_mono (utRetype_vcpuWordsA (ar := ar) hA C hTr2 h al4 hVP a) (fun _ g _ => g) + · exact WP.spec_mono (utRetype_nfiA (ar := ar) hA C a) (fun _ _ g => absurd g h) + have hiW : WP.spec (untyped.retype st ar s2 a (cslotAddrA sl) false aligned nt us dest off win false) + (fun res => nt.val = 14 → UtHighZW st aligned (zA nt.val us) win.val res.2.1) := by + by_cases h : nt.val = 14 + · have al4 : aligned.val % 4096 = 0 := by + have g := hal2 + rw [h, zA_14 us] at g + change aligned.val % 8192 = 0 at g + omega + exact WP.spec_mono (utRetype_highWordsA (ar := ar) hA C hTr2 h al4 (hkw h) hKt hK0 a) + (fun _ g _ => g) + · exact WP.spec_mono (utRetype_nfiA (ar := ar) hA C a) (fun _ _ g => absurd g h) + -- the creation + refine WP.spec_mono (WP.spec_and + (utCreate_postA hA J2 C X hcap hsrc hUe hb0 hal8 hpf hal2 hP' hno hVP hKt hK0 hkw) + (WP.spec_and (utRetype_frameA (ar := ar) hA C hTr2 a) + (WP.spec_and (untyped.retype.liveRWH (ar := ar) hA C hb0 a) + (WP.spec_and tcbW (WP.spec_and vcW (WP.spec_and hiW (WP.spec_and (utRetype_nfiA (ar := ar) hA C a) + (utRt_j5LoopA hA C hTr2 J2.j5.1.1 J2.j5.1.2.1 hrev a)))))))) ?_ + rintro res ⟨⟨hok, Kall⟩, w, ⟨-, -, WT, -⟩, TR, VW, HW, ⟨nfi, hnv, cap'⟩, L5⟩ + refine ⟨hok, fun r hr H'' Vn U0 Ac0 lo hi extra ER arm ko j7at => ?_⟩ + exact utRt_recNoResetA J ER noch arm ko j7at hvs hrw J2 hc2 W h1 h2 pos us39 hb0 hc47 C.dn w WT + (fun h => (TR h).1) (fun h => (TR h).2) VW HW hnv cap' L5 X.ty hus hsc rfl hsrc hlen hdst hdstA hr + (Kall r hr) J2.utWin J2.j.1.irq.nodeHi J2.coresAll + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtRtResetA.lean b/proofs/Fl4ProofsA64/P3/UtRtResetA.lean new file mode 100644 index 000000000..ddb3aac35 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtRtResetA.lean @@ -0,0 +1,266 @@ +/- +P3 (aarch64, A64-P3 Untyped, the reset wired into `untyped::decode`): **a retype that resets its untyped first**. + +* `utRetype_splitA`: FL4's `retype` with `reset` is `reset_untyped` followed by `retype` without it. +* `utInvoke_resetErrA`, `utInvoke_resetOkA`: the same split of spec `invoke_untyped`. +* `UtRstReadyA`: the record and the decode-site facts the reset reads, at one state. +* `utRetype_resetA`: with `reset`, a RAM untyped. Unused: nothing changes. Used, below a chunk: the record holds on + the dropped sets, the free index is 0. Used, a chunk or more: the same, or preempted. Each `Ok` case goes on with + the creation (`retype` without reset) from the reset state, which the caller proves (continuation form). +* `utDecode_resetA`: when the untyped has no CDT child, `untyped::decode` refines `fl4DecodeUntypedInvocationVirt` + up to `set_thread_state Restart` followed by `retype` with `reset`, in `utRetype_resetA`'s form. + +Not covered here: the creation after a reset, and a device untyped's reset (`utRetype_resetA` is for RAM). + +Named premises: +* [J] `hready`: `UtRstReadyA` holds again after the caller's restart. **The record has no restart step yet** + (producer: the merge line's record-across-steps work). +* [J] inside `UtRstReadyA`: `symL`, + [S] `othT`, `othC` (B). +* [J] `hpp` (J8, `UtResetA`; `UtPpHookA.utPp_hookA` gives it), with the carried predicate `T` (`hT`, `hT0`). +* [P] `hPost`, `hchunk`, `hn`, `hfm`, `hvirt`, `hfan`. +-/ +import Fl4ProofsA64.P3.UtResetA +import Fl4ProofsA64.P3.UtRtDecSiteA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4SpecA64 Fl4Spec Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {ar : machine.Arch A} + +omit L in +/-- **FL4's `retype` with `reset`** is `reset_untyped`, then `retype` without it. -/ +theorem utRetype_splitA (ar : machine.Arch A) (s : S) (a : A) (slot : obj.Slot) (base nt us dest off win : U64) + (dev : Bool) : + untyped.retype st ar s a slot true base nt us dest off win dev = + (do + let (r, s1, a1) ← untyped.reset_untyped st ar s a slot + let cf ← core.result.Result.Insts.CoreOpsTry.branch r + match cf with + | core.ops.control_flow.ControlFlow.Continue _ => + untyped.retype st ar s1 a1 slot false base nt us dest off win dev + | core.ops.control_flow.ControlFlow.Break residual => + let r1 ← core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual Unit + (core.convert.FromSame error.Exc) residual + ok (r1, s1, a1)) := by + unfold untyped.retype + simp only [↓reduceIte, Bool.false_eq_true] + rfl + +/-- A preempted reset preempts the spec's invocation. -/ +theorem utInvoke_resetErrA {sl : CslotPtr} {base rb : Fl4SpecA64.Word} {t : ApiObjectType} {sz : Nat} + {slots : List CslotPtr} {dev : Bool} {σ σ' : State Unit} + (h : (.error (), σ') ∈ ((resetUntypedCap (Z := Unit) sl).run σ).1) : + (.error (), σ') ∈ ((fl4InvokeUntyped (Z := Unit) (.retype sl true base rb t sz slots dev)).run σ).1 := by + rw [utInvoke_eqA] + exact mem_bindE_errD h + +/-- A finished reset leaves the spec's invocation at the body, from the reset state. -/ +theorem utInvoke_resetOkA {sl : CslotPtr} {base rb : Fl4SpecA64.Word} {t : ApiObjectType} {sz : Nat} + {slots : List CslotPtr} {dev : Bool} {σ σ' σ'' : State Unit} + (h : (.ok (), σ') ∈ ((resetUntypedCap (Z := Unit) sl).run σ).1) + (hb : ((), σ'') ∈ (utRtBodyA sl base rb t sz slots dev σ').1) : + (.ok (), σ'') ∈ ((fl4InvokeUntyped (Z := Unit) (.retype sl true base rb t sz slots dev)).run σ).1 := by + rw [utInvoke_eqA] + refine mem_bindE_okD (a := ()) (s' := σ') h ?_ + exact mem_liftED hb + +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {m : McState} + +/-- **The record and the decode-site facts the reset reads** (module docstring). -/ +structure UtRstReadyA (st : machine.Store S) [L : LawfulStore st] (AR : A → HwRootA → Prop) (Z : XSetsA) (H : Heap) + (OS U Ac Pp Tb : Set U64) (Lv : U64 → Nat) (ia ie : Nat) (N E Rp : Set U64) (Bf : Set (U64 × Fl4SpecA64.Word)) + (stack : U64 → List U64) (κ : U64 → U64) (n : Nat) (nA : U64) (m : McState) (Ud Acd Rq Nt : Set U64) + (ex : Aeneas.Std.Array obj.Slot 3#usize) (x p sb fi : U64) (s : S) (a : A) (σ : State Unit) : Prop where + rec_ : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m + /-- (F1–F2) the pending switch and the current thread's state. -/ + sact : SActA σ + dec : DecPre st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ud Acd s (.Untyped p sb fi false) ex + place : SysPlace st H Z.Cs H.frame Rq Nt OS s ex x + cap : obj.Slot.cap st x s = ok (.Untyped p sb fi false) + noch : cdt.ensure_no_children st s x = ok (.Ok ()) + symL : SymL st H Z.Cs s + othT : ∀ c ∈ Z.Cs, c ≠ L.cpu s → specPtr (L.mem s (L.nodeAt s c layout.node.CUR_THREAD)) ∉ utDA p sb.val + othC : ∀ c ∈ Z.Cs, c ≠ L.cpu s → specPtr (L.mem s (L.nodeAt s c layout.node.CUR_SC)) ∉ utDA p sb.val + +variable {Ud Acd Rq Nt : Set U64} {ex : Aeneas.Std.Array obj.Slot 3#usize} {p sb fi : U64} + +set_option maxHeartbeats 1000000 in +/-- **A retype that resets its RAM untyped first** (module docstring). -/ +theorem utRetype_resetA [Fact (n ≤ Fl4SpecA64.fl4A64Platform.numNodes)] (ar : machine.Arch A) {s : S} {a : A} {σ : State Unit} + (cte : CslotPtr) {qc : U64} (hq : cte.1 = specPtr qc) (hth : Z.th qc = some (.cnode cte.2.length)) + (Y : UtRstReadyA (ar := ar) st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n nA m Ud Acd Rq Nt ex (cslotAddrA cte) + p sb fi s a σ) + (hn : nA.val = fl4A64Platform.s2IpaBits) + (hPost : ∀ σ' : State Unit, ((), σ') ∈ (archPostResetUntyped (Z := Unit) false sb.val σ').1) + (hchunk : resetChunkBits = 16) + (Rpre : U64 → S → A → State Unit → Prop) + (T : A → State Unit → Prop) + (hT : ∀ (a : A) (σ σ' : State Unit), T a σ → UtCnStepA σ σ' → σ'.machineState = σ.machineState → T a σ') + (hT0 : T a (utDelStA (specPtr p) sb.val σ)) + (hpp : ∀ (Rq' Nt' : Set U64) (o : U64), o ≠ 0#u64 → PPHypUtA st ar (dropSetA (utDA p sb.val) OS) + (fun s a σ => UtRstInvA (ar := ar) st AR (dropZA (utDA p sb.val) Z) (dropHA (utDA p sb.val) H) + (dropSetA (utDA p sb.val) OS) U Ac (dropSetA (utDA p sb.val) Pp) Tb Lv ia ie (dropSetA (utDA p sb.val) N) + (dropSetA (utDA p sb.val) E) (dropSetA (utDA p sb.val) Rp) + (utBfOffA (utDA p sb.val) (dropBfA (utDA p sb.val) Bf)) stack κ n nA (dropMA (utDA p sb.val) m) p sb + (cslotAddrA cte) Rq' Nt' (Array.repeat 3#usize 0#u64) s a σ o ∧ T a σ) + (Rpre o)) + (base nt us dest off win : U64) + (K : core.result.Result Unit error.Exc × S × A → Prop) + -- an unused untyped: the creation from the same state + (hnoop : fi = 0#u64 → untyped.reset_untyped st ar s a (cslotAddrA cte) = ok (core.result.Result.Ok (), s, a) → + (.ok (), σ) ∈ ((resetUntypedCap (Z := Unit) cte).run σ).1 → + untyped.retype st ar s a (cslotAddrA cte) false base nt us dest off win false ⦃ r => K r ⦄) + -- used, below a chunk + (hsmall : fi ≠ 0#u64 → sb.val < 16 → ∀ (s3 : S) (σ3 : State Unit), + untyped.reset_untyped st ar s a (cslotAddrA cte) = ok (core.result.Result.Ok (), s3, a) → + (.ok (), σ3) ∈ ((resetUntypedCap (Z := Unit) cte).run σ).1 → + JStage3A st AR (dropZA (utDA p sb.val) Z) (dropHA (utDA p sb.val) H) (dropSetA (utDA p sb.val) OS) U Ac + (dropSetA (utDA p sb.val) Pp) Tb Lv ia ie (dropSetA (utDA p sb.val) N) (dropSetA (utDA p sb.val) E) + (dropSetA (utDA p sb.val) Rp) (utBfOffA (utDA p sb.val) (dropBfA (utDA p sb.val) Bf)) stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s3 a σ3 (dropMA (utDA p sb.val) m) → + SymL st (dropHA (utDA p sb.val) H) Z.Cs s3 → capA st s3 (cslotAddrA cte) = .Untyped p sb 0#u64 false → + untyped.retype st ar s3 a (cslotAddrA cte) false base nt us dest off win false ⦃ r => K r ⦄) + -- used, a chunk or more + (hbig : fi ≠ 0#u64 → 16 ≤ sb.val → ∀ (s3 : S) (a3 : A) (σ3 : State Unit) (Rq' Nt' : Set U64), + untyped.reset_untyped st ar s a (cslotAddrA cte) = ok (core.result.Result.Ok (), s3, a3) → + (.ok (), σ3) ∈ ((resetUntypedCap (Z := Unit) cte).run σ).1 → + UtRstInvA (ar := ar) st AR (dropZA (utDA p sb.val) Z) (dropHA (utDA p sb.val) H) + (dropSetA (utDA p sb.val) OS) U Ac (dropSetA (utDA p sb.val) Pp) Tb Lv ia ie (dropSetA (utDA p sb.val) N) + (dropSetA (utDA p sb.val) E) (dropSetA (utDA p sb.val) Rp) + (utBfOffA (utDA p sb.val) (dropBfA (utDA p sb.val) Bf)) stack κ n nA (dropMA (utDA p sb.val) m) p sb + (cslotAddrA cte) Rq' Nt' (Array.repeat 3#usize 0#u64) s3 a3 σ3 0#u64 → T a3 σ3 → + untyped.retype st ar s3 a3 (cslotAddrA cte) false base nt us dest off win false ⦃ r => K r ⦄) + -- preempted + (hpre : ∀ (s3 : S) (a3 : A) (σ3 : State Unit) (o : U64), + (.error (), σ3) ∈ ((resetUntypedCap (Z := Unit) cte).run σ).1 → Rpre o s3 a3 σ3 → + K (.Err .Preempted, s3, a3)) : + untyped.retype st ar s a (cslotAddrA cte) true base nt us dest off win false ⦃ r => K r ⦄ := by + have J := Y.rec_ + have hcx : capA st s (cslotAddrA cte) = .Untyped p sb fi false := okinj ((capA_eq s _).symm.trans Y.cap) + rw [utRetype_splitA] + by_cases h0 : fi = 0#u64 + · have hc0 : capA st s (cslotAddrA cte) = .Untyped p sb 0#u64 false := by rw [hcx, h0] + have e : untyped.reset_untyped st ar s a (cslotAddrA cte) = ok (core.result.Result.Ok (), s, a) := + ut_reset_noopA (st := st) (ar := ar) hc0 + rw [e, bind_ok] + simp only [Aeneas.Std.uncurry_apply_pair, core.result.Result.Insts.CoreOpsTry.branch, bind_ok] + refine hnoop h0 e ?_ + have hab : AbsSlotA Z.th cte := ⟨qc, hq, Or.inl hth⟩ + obtain ⟨ac, hg, hr⟩ := J.j.1.st.getAbsA hab + have eac : ac = .untypedCap false (specPtr p) sb.val fi.val := by + have h := hr.2 + rw [show capAtA (L.mem s) (cslotAddrA cte) = capA st s (cslotAddrA cte) from rfl, hcx] at h + exact (show absCapA (.Untyped p sb fi false) = ac from h).symm + rw [eac] at hg + refine ut_reset_noop_memA (cap := .untypedCap false (specPtr p) sb.val fi.val) hg ?_ + show fi.val = 0 + rw [h0]; rfl + by_cases hs : sb.val < 16 + · obtain ⟨s3, σ3, e, hm, J3, Y3, hc3⟩ := utReset_smallA ar J Y.dec Y.place Y.cap Y.noch Y.symL Y.othT Y.othC hn + cte hq hth rfl hPost h0 hs hchunk + rw [e, bind_ok] + simp only [Aeneas.Std.uncurry_apply_pair, core.result.Result.Insts.CoreOpsTry.branch, bind_ok] + exact hsmall h0 hs s3 σ3 e hm J3 Y3 hc3 + · have hb : 16 ≤ sb.val := by omega + have R := utReset_bigA ar Y.sact J Y.dec Y.place Y.cap Y.noch Y.symL Y.othT Y.othC hn cte hq hth rfl + hPost Rpre T hT hT0 hpp h0 hb hchunk + obtain ⟨⟨r1, s3, a3⟩, eR, h⟩ := spec_exists_dut R + rw [eR, bind_ok] + rcases h with ⟨hok, σ3, Rq', Nt', hm, I, TI⟩ | ⟨herr, σ3, o, hm, hR⟩ + · dsimp only at hok I TI + subst hok + simp only [Aeneas.Std.uncurry_apply_pair, core.result.Result.Insts.CoreOpsTry.branch, bind_ok] + exact hbig h0 hb s3 a3 σ3 Rq' Nt' eR hm I TI + · dsimp only at herr hR + subst herr + simp only [Aeneas.Std.uncurry_apply_pair, core.result.Result.Insts.CoreOpsTry.branch, bind_ok, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, bind_tc_ok] + exact (WP.spec_ok _).2 (hpre s3 a3 σ3 o hm hR) + +/-! ## The decode -/ + +variable {J7 : JSlotA S} {s : S} {a : A} {σ : State Unit} {dev : Bool} + +/-- **The decode of a retype that resets** (module docstring): the CDT has no child of the untyped, so FL4 and the +spec decode a `retype` with `reset`; what is left is the restarted caller's `retype` (`hQ`, discharged by +`utRetype_resetA`), from the state after FL4's `set_thread_state Restart`, with the spec's. -/ +theorem utDecode_resetA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + (ex : Aeneas.Std.Array obj.Slot 3#usize) + (sl : CslotPtr) (hsrc : ∃ q, sl.1 = specPtr q ∧ Z.th q = some (.cnode sl.2.length)) + (D : DecPre st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ud Acd s (.Untyped p sb fi dev) ex) + (P : SysPlace st H Z.Cs H.frame Rq Nt OS s ex (cslotAddrA sl)) + (hcapF : obj.Slot.cap st (cslotAddrA sl) s = ok (.Untyped p sb fi dev)) + (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) + (hfm : A64FrameMasks st s) + (vi : U64) (hvi : ar.virtualization a = ok vi) (hvirt : vi = 0#u64 ↔ fl4A64Virt = false) + (hfan : Fl4SpecA64.retypeFanOutLimit = 256#64) + (lbl : U64) (slbl : Fl4SpecA64.Data) + (hlbl : Fl4SpecA64.genInvocationType slbl = .untypedRetype ↔ lbl = fl4_abi.label.UntypedRetype) + (length buf : U64) (args : List U64) (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) + (exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) (hex : ExtraOKA Z.th (L.mem s) ex exs) + (hnochild : ¬ ∃ x, σ.cdt x = some sl) + (Q : UntypedInvocation → core.result.Result Unit error.Exc × S × A → Prop) + (hQ : ∀ (aligned nt us dest off win : U64) (t : ApiObjectType) (r : Nat) (xT : Tcb) (fiv : U64), + UtCtxA st Z.th s σ sl p sb fi dev nt us dest off win t r true fiv → + UtArgs st s ex (cslotAddrA sl) p sb fi dev true aligned nt us dest off win → + σ.kheap σ.curThread = some (.tcb xT) → + JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie (L.wr s (stA (L.curW s)) sched.TS_RESTART) a + (stUpdA σ σ.curThread xT .restart) m → + SymL st H Z.Cs (L.wr s (stA (L.curW s)) sched.TS_RESTART) → + OSKeep st OS s (L.wr s (stA (L.curW s)) sched.TS_RESTART) → + untyped.retype st ar (L.wr s (stA (L.curW s)) sched.TS_RESTART) a (cslotAddrA sl) true aligned nt us dest off + win dev ⦃ res => + Q (.retype sl true (specPtr p) (specPtr aligned) t us.val (windowSlotsA dest r off.val win.val) dev) res ⦄) : + untyped.decode st ar s a lbl length (cslotAddrA sl) (.Untyped p sb fi dev) buf ex ⦃ res => + UPostA σ (fl4DecodeUntypedInvocationVirt (Z := Unit) slbl (args.map UScalar.bv) sl + (absCapA (.Untyped p sb fi dev)) (exs.map Prod.fst)) s a Q res ⦄ := by + have Je := J.toJEntA + have hV : SlotsValidA Z.th (L.mem s) := Je.j.1.cv + have hsub : ∀ y, IsSlotA Z.th y → y ∈ OS := J.j5.2.1 + have hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null := J.j5.2.2 + have hR := Je.j.1.st + have hC := Je.j.1.cdt + have hP := Je.j.1.place + have hO := Je.sys.ent.os + have hTr : TreeA st OS Z.G.chains Z.G.par s := hC.tree.grow hsub fun x hx hn => hnul x hx hn + have hAs : AbsSlotA Z.th sl := by + obtain ⟨q, e, hq⟩ := hsrc + exact ⟨q, e, Or.inl hq⟩ + have hsO : cslotAddrA sl ∈ OS := hsub _ (hAs.isSlot hP) + have R := UtDecP.of_live D Je.sys.sysl.sys P hcapF Je.sys.sysl.ln Je.sys.sysl.vr Je.sys.sysl.ly + have hcap : capA st s (cslotAddrA sl) = .Untyped p sb fi dev := R.cap + have hrv : obj.Slot.revocable st (cslotAddrA sl) s = ok true := + Je.sys.sysl.sys.stz.1.2.1 _ hsO (by rw [hcap]; rfl) + have hsb : sb.val ≤ 39 := J.utBlk _ hsO p sb fi dev hcap + refine untyped_decode_refinesA ar Z.th s a σ Z.G hR hV hP hO hTr hC hsub hfm vi hvi hvirt hfan lbl slbl hlbl + length buf args hlen hargs sl hAs hsO p sb fi dev hcap hrv hsb R.utv.2.2 ex exs hex Q ?_ + intro nt us dest off win t r reset fiv _ X hU + have hres : reset = true := by + rw [X.resetEq] + exact decide_eq_true hnochild + subst hres + generalize (⟨Fl4SpecA64.alignUp (p.bv + fiv.bv) (zOfA nt.val us.val)⟩ : U64) = aligned at hU ⊢ + obtain ⟨Rp', Nt', extra', sp', hPpl⟩ := RS.pl + obtain ⟨xT, hkT, hst, -, E1, Y1, hOS1, -, -⟩ := Je.restartA hPpl RS.sym RS.run RS.cur RS.vs RS.vc + rw [hst, bind_ok] + exact hQ aligned nt us dest off win t r xT fiv X hU hkT E1 Y1 hOS1 + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtRtResetCrtA.lean b/proofs/Fl4ProofsA64/P3/UtRtResetCrtA.lean new file mode 100644 index 000000000..af2b65a69 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtRtResetCrtA.lean @@ -0,0 +1,298 @@ +/- +P3 (aarch64, A64-P3 Untyped): **`untyped::decode` for a retype that resets its RAM untyped first**, relation level. + +* **`utDecode_resetPostA`**: when the untyped has no CDT child, `untyped::decode` refines + `fl4DecodeUntypedInvocationVirt`. Either FL4's retype finishes and, for a spec `reset_untyped_cap` outcome `σ3`, + every outcome of the spec body from `σ3` keeps `JAtZAX` at the grown sets (over the record's sets, or the dropped + ones when the untyped was used); or it is preempted inside the reset, matching a preempted spec reset, and `JStage3A` + holds on the dropped sets. + +The unused untyped (free index 0) keeps the sets; a used one drops its region (`P3/UtResetA`), then the creation +(`P3/UtRtCreateA`) runs from A64 P1's `CrtP` / `UtEnd` at the reset's exit (`P3/UtCrtResetA`). + +Premises: +* [J] `symL` (inside `RS`). +* [S] `othT`, `othC` (B), at the restarted state. +* [J] `hvp`: no VSpace word of the dropped pools and `Tb`, and no ASID-table word, is a preemption-point word, at a + state satisfying the dropped record (the narrowed form from delete-hooks discharges it once on the entry line). +* [J] `hkw`: a new `HighVSpace` window is in the kernel window. [J] `hT0`: the preemption point's timing facts after + `delete_objects`. +* [P] `hPost`, `hchunk`, `hn`, `hfm`, `hvirt`, `hfan`, `hnd`, the clock and interrupt facts (`P`, `hC`, `hNow`, + `hAr`, `hirq`, `hmb`), `hVP`, `hKt` / `hK0`. + +New theorems only. +-/ +import Fl4ProofsA64.P3.UtRtResetPpA +import Fl4ProofsA64.P3.UtRstReadyA +import Fl4ProofsA64.P3.UtCrtResetA +import Fl4ProofsA64.P3.UtRtCreateA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4SpecA64 Fl4Spec Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +set_option maxRecDepth 100000 + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rq : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s : S} {a : A} {σ : State Unit} {m : McState} + +/-- The creation's relation post over the sets `Z'` and view `m'`. -/ +def UtCrtPostA (AR : A → HwRootA → Prop) (Z' : XSetsA) (m' : McState) (sl : CslotPtr) (p aligned nt us dest off win : U64) + (t : ApiObjectType) (rd : Nat) (σ3 : State Unit) (s' : S) (a' : A) (st' : machine.Store S) [LawfulStore st'] : + Prop := + ∀ r ∈ (utRtBodyA sl (specPtr p) (specPtr aligned) t us.val (windowSlotsA dest rd off.val win.val) false σ3).1, + JAtZAX (S := S) (st := st') AR + (utRtZA Z' ⟨utGA (cslotAddrA sl) (djA dest off) Z'.G.chains win.val, + utPA (cslotAddrA sl) (djA dest off) Z'.G.par win.val⟩ nt us aligned win.val + {t | nt.val = 1 ∧ t ∈ utBasesA aligned (zA nt.val us) win.val} + (utCNewA nt aligned (zA nt.val us) win.val) + {v | nt.val = 12 ∧ v ∈ utBasesA aligned (zA nt.val us) win.val}) + s' a' r.2 (utRtMA m' {t | nt.val = 1 ∧ t ∈ utBasesA aligned (zA nt.val us) win.val}) + +set_option maxHeartbeats 4000000 in +/-- **The decode of a retype that resets, to the end of the creation** (module docstring). -/ +theorem utDecode_resetPostA [Fact (n ≤ Fl4SpecA64.fl4A64Platform.numNodes)] (hA : P0.ArchTotal ar) + (sact : SActA σ) + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rq Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {p sb fi : U64} (ex : Aeneas.Std.Array obj.Slot 3#usize) + (sl : CslotPtr) (hsrc : ∃ q, sl.1 = specPtr q ∧ Z.th q = some (.cnode sl.2.length)) + {Rp Nt Ud Acd : Set U64} + (D : DecPre st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ud Acd s (.Untyped p sb fi false) ex) + (P : SysPlace st H Z.Cs H.frame Rp Nt OS s ex (cslotAddrA sl)) + (hcapF : obj.Slot.cap st (cslotAddrA sl) s = ok (.Untyped p sb fi false)) + (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) + (hfm : A64FrameMasks st s) + (vi : U64) (hvi : ar.virtualization a = ok vi) (hvirt : vi = 0#u64 ↔ fl4A64Virt = false) + (hfan : Fl4SpecA64.retypeFanOutLimit = 256#64) + (lbl : U64) (slbl : Fl4SpecA64.Data) + (hlbl : Fl4SpecA64.genInvocationType slbl = .untypedRetype ↔ lbl = fl4_abi.label.UntypedRetype) + (length buf : U64) (args : List U64) (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) + (exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) (hex : ExtraOKA Z.th (L.mem s) ex exs) + (hnochild : ¬ ∃ x, σ.cdt x = some sl) + (hnew : ∀ aligned nt us dest off win : U64, + UtArgs st s ex (cslotAddrA sl) p sb fi false true aligned nt us dest off win → + PlaceA (utRtThA (dropThA (utDA p sb.val) Z.th) false nt us aligned win.val) (L.ksb s) ∧ + PlaceA (utRtThA Z.th false nt us aligned win.val) (L.ksb s) ∧ + ∀ j, j < win.val → utRangedA nt.val → SpNoOvA (bjA aligned (zA nt.val us) j) (zOfA nt.val us.val)) + (othT : ∀ c ∈ Z.Cs, c ≠ L.cpu s → specPtr (L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART) + (L.nodeAt (L.wr s (stA (L.curW s)) sched.TS_RESTART) c layout.node.CUR_THREAD)) ∉ utDA p sb.val) + (othC : ∀ c ∈ Z.Cs, c ≠ L.cpu s → specPtr (L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART) + (L.nodeAt (L.wr s (stA (L.curW s)) sched.TS_RESTART) c layout.node.CUR_SC)) ∉ utDA p sb.val) + -- the decode-site's own state has no other core's current thread or SC in the region either + (othT0 : ∀ c ∈ Z.Cs, c ≠ L.cpu s → specPtr (L.mem s (L.nodeAt s c layout.node.CUR_THREAD)) ∉ utDA p sb.val) + (othC0 : ∀ c ∈ Z.Cs, c ≠ L.cpu s → specPtr (L.mem s (L.nodeAt s c layout.node.CUR_SC)) ∉ utDA p sb.val) + -- the reset + (hn : nA.val = fl4A64Platform.s2IpaBits) + (hPost : ∀ σ' : State Unit, ((), σ') ∈ (archPostResetUntyped (Z := Unit) false sb.val σ').1) + (hchunk : resetChunkBits = 16) + (hnd : numDomains = 1) {Clock : A → MachineState → Prop} (Pc : NowAgreeA ar Clock) + (hvp : ∀ (s : S) (a : A) (σ : State Unit), + JStage3A st AR (dropZA (utDA p sb.val) Z) (dropHA (utDA p sb.val) H) (dropSetA (utDA p sb.val) OS) U Ac + (dropSetA (utDA p sb.val) Pp) Tb Lv ia ie (dropSetA (utDA p sb.val) N) (dropSetA (utDA p sb.val) E) + (dropSetA (utDA p sb.val) Rq) (utBfOffA (utDA p sb.val) (dropBfA (utDA p sb.val) Bf)) stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s a σ (dropMA (utDA p sb.val) m) → + ∀ b, VsR (L.ksb s) (dropSetA (utDA p sb.val) Pp ∪ Tb) b → Aligned b ∧ ¬ PPW st s b) + (hC : ∀ i, fl4RawCounterOracle i ≤ fl4RawCounterOracle (i + 1)) + (hNow : ∀ σ : State Unit, fl4ReadRawCounter σ.machineState = getCurrentTime σ.machineState) + (hAr : ∀ (s : S) (a : A), AR a Z.hw → sched.update_timestamp st ar s a ⦃ r => AR r.2 Z.hw ⦄) + (hirq : ∀ (a : A) (σ : State Unit), AR a Z.hw → Clock a σ.machineState → + ar.irq_pending a ⦃ r => ∃ o ms, (o, { σ with machineState := ms }) ∈ + (doMachineOp (getActiveIRQ true) σ).1 ∧ (r.1 = true ↔ ¬ o = none) ∧ + ms.underlyingMemory = σ.machineState.underlyingMemory ∧ + ms.lastMachineTime = σ.machineState.lastMachineTime ∧ ms.timeState = σ.machineState.timeState ∧ + Clock r.2 ms ∧ AR r.2 Z.hw ⦄) + (hmb : ∀ a : A, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hT0 : ∀ xT, σ.kheap σ.curThread = some (.tcb xT) → + UtPpTimeA Clock a (utDelStA (specPtr p) sb.val (stUpdA σ σ.curThread xT .restart))) + -- the arch facts of the creation + (hVP : ∀ a1 : A, UtVcpuPlatA ar a1) {k : U64 → U64} + (hKt : ∀ (a1 : A) (i : U64), i.val < 511 → ar.kernel_ttbr1_entry a1 0#u64 i = ok (k i)) + (hK0 : ∀ i : U64, i.val < 256 → k i = 0#u64) + (hkw : ∀ aligned nt us dest off win : U64, + UtArgs st s ex (cslotAddrA sl) p sb fi false true aligned nt us dest off win → nt.val = 14 → + 18446603336221196288 ≤ aligned.val) : + untyped.decode st ar s a lbl length (cslotAddrA sl) (.Untyped p sb fi false) buf ex ⦃ res => + UPostA σ (fl4DecodeUntypedInvocationVirt (Z := Unit) slbl (args.map UScalar.bv) sl + (absCapA (.Untyped p sb fi false)) (exs.map Prod.fst)) s a + (fun inv res => ∃ (base retypeBase : Word) (t : ApiObjectType) (us : Nat) (slots : List CslotPtr), + inv = .retype sl true base retypeBase t us slots false ∧ + ∃ xT, σ.kheap σ.curThread = some (.tcb xT) ∧ + ∃ (aligned nt usF dest off win : U64) (rd : Nat), + UtArgs st s ex (cslotAddrA sl) p sb fi false true aligned nt usF dest off win ∧ + base = specPtr p ∧ retypeBase = specPtr aligned ∧ us = usF.val ∧ + slots = windowSlotsA dest rd off.val win.val ∧ + ((res.1 = .Ok () ∧ ∃ σ3, + (.ok (), σ3) ∈ ((resetUntypedCap (Z := Unit) sl).run (stUpdA σ σ.curThread xT .restart)).1 ∧ + ∃ (Z' : XSetsA) (m' : McState), + (Z' = Z ∧ m' = m ∨ Z' = dropZA (utDA p sb.val) Z ∧ m' = dropMA (utDA p sb.val) m) ∧ + UtCrtPostA AR Z' m' sl p aligned nt usF dest off win t rd σ3 res.2.1 res.2.2 st) + ∨ (res.1 = .Err .Preempted ∧ ∃ σ3, + (.error (), σ3) ∈ ((resetUntypedCap (Z := Unit) sl).run (stUpdA σ σ.curThread xT .restart)).1 ∧ + JStage3A st AR (dropZA (utDA p sb.val) Z) (dropHA (utDA p sb.val) H) (dropSetA (utDA p sb.val) OS) + U Ac (dropSetA (utDA p sb.val) Pp) Tb Lv ia ie (dropSetA (utDA p sb.val) N) + (dropSetA (utDA p sb.val) E) (dropSetA (utDA p sb.val) Rq) + (utBfOffA (utDA p sb.val) (dropBfA (utDA p sb.val) Bf)) stack κ n (J5A st) (J7A st (J7R2A st ar nA)) + res.2.1 res.2.2 σ3 (dropMA (utDA p sb.val) m)))) res ⦄ := by + have hsub : ∀ y, IsSlotA Z.th y → y ∈ OS := J.j5.2.1 + have hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null := J.j5.2.2 + obtain ⟨qc, hq, hth⟩ := hsrc + have hAs : AbsSlotA Z.th sl := ⟨qc, hq, Or.inl hth⟩ + have hsO : cslotAddrA sl ∈ OS := hsub _ (hAs.isSlot J.j.1.place) + have R := UtDecP.of_live D J.sys.sysl.sys P hcapF J.sys.sysl.ln J.sys.sysl.vr J.sys.sysl.ly + have hcap : capA st s (cslotAddrA sl) = .Untyped p sb fi false := R.cap + refine utDecode_resetA J ex sl ⟨qc, hq, hth⟩ D P hcapF RS hfm vi hvi hvirt hfan lbl slbl hlbl length buf args hlen + hargs exs hex hnochild _ ?_ + intro aligned nt us dest off win t rd xT fiv X hU hkT _ _ _ + obtain ⟨x0, hxm, hx0, idx, dep, val, ers, hv64, erd, ees, hrs, eal, -⟩ := id hU + have he := hrs.1 rfl + obtain ⟨⟨⟩, he⟩ := he + -- the record and the reset's inputs at the restarted state + obtain ⟨y, hk, hst, -, Y⟩ := utRstReady_restartA sact J D P hcapF he RS.sym RS.run RS.cur RS.vs RS.vc othT othC + obtain rfl : y = xT := by + have h := hk.symm.trans hkT + injection h with h + injection h + set s2 := L.wr s (stA (L.curW s)) sched.TS_RESTART with hs2 + set σ2 := stUpdA σ σ.curThread y .restart with hσ2 + have J2 := Y.rec_ + have e : sched.set_thread_state st s (L.curW s) sched.TS_RESTART = ok s2 := hst + -- the drop's facts at the decode's state + have C0 := utDropC_of J D P hcapF he RS.sym othT0 othC0 hn + -- the new base and the window + have CA := CrtA.of_args R hU + have hus : us.val < 64 := X.size.1 + have hnn : nt.val < 15 := CA.hn + have hp0 : 0 < p.val := J.sys.sysl.ly.ut0 _ hsO p sb fi hcap + have hpfit : p.val + 2 ^ sb.val ≤ 2 ^ 64 := al_fit R.utv.1 (by have := R.utv.2.1; omega) p.bv.isLt + have hArU : UtAr (H.drop (RegionD H p.val (p.val + 2 ^ sb.val))) p sb 0#u64 false aligned nt us dest off win := + R.arith _ _ _ _ _ _ _ hU + have hA0 : p.val + (0#u64 : U64).val ≤ aligned.val := hArU.2.2.2.1 + have hAw : aligned.val + win.val * 2 ^ (zA nt.val us).val ≤ p.val + 2 ^ sb.val := hArU.2.2.2.2.1 + have hb0 : 0 < aligned.val := by omega + have hal8 : aligned.val % 8 = 0 := hArU.2.1 + have hpf : aligned.val + win.val * 2 ^ (zA nt.val us).val ≤ 2 ^ 64 := le_trans hAw hpfit + have hal2 : aligned.val % 2 ^ (zA nt.val us).val = 0 := by + obtain ⟨-, -, ev⟩ := untyped.retype_size.okA ers + rw [ev] at eal hv64 + simp only [↓reduceIte] at eal + have fw : (core.num.U64.wrapping_add p 0#u64).val = p.val := by + rw [wa_valP, show (0#u64 : U64).val = 0 from rfl] + refine Nat.mod_eq_of_lt ?_ + have := p.bv.isLt + simpa using this + have hw1 : 2 ^ (zA nt.val us).val ≤ win.val * 2 ^ (zA nt.val us).val := + Nat.le_mul_of_pos_left _ (by have := X.window.1; omega) + have g := align_up_valA hv64 (by rw [fw]; omega) eal + rw [g] + exact Nat.mul_mod_left _ _ + obtain ⟨hPd, hPk, hno⟩ := hnew aligned nt us dest off win hU + have hkw' := fun h => hkw aligned nt us dest off win hU h + have kb2 : L.ksb s2 = L.ksb s := L.ksb_wr _ _ _ + -- the arithmetic's grown heap + obtain ⟨-, -, -, -, -, -, -, H', -, hHH', hHn, hEx⟩ := id hArU + have hsrc' : ∃ q, sl.1 = specPtr q ∧ Z.th q = some (.cnode sl.2.length) := ⟨qc, hq, hth⟩ + -- the creation over the dropped sets, after a used untyped's reset + have drop : ∀ (s3 : S) (a3 : A) (σ3 : State Unit), + untyped.reset_untyped st ar s2 a (cslotAddrA sl) = ok (core.result.Result.Ok (), s3, a3) → + JStage3A st AR (dropZA (utDA p sb.val) Z) (dropHA (utDA p sb.val) H) (dropSetA (utDA p sb.val) OS) U Ac + (dropSetA (utDA p sb.val) Pp) Tb Lv ia ie (dropSetA (utDA p sb.val) N) (dropSetA (utDA p sb.val) E) + (dropSetA (utDA p sb.val) Rq) (utBfOffA (utDA p sb.val) (dropBfA (utDA p sb.val) Bf)) stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s3 a3 σ3 (dropMA (utDA p sb.val) m) → + untyped.retype st ar s3 a3 (cslotAddrA sl) false aligned nt us dest off win false ⦃ res => + res.1 = .Ok () ∧ UtCrtPostA AR (dropZA (utDA p sb.val) Z) (dropMA (utDA p sb.val) m) sl p aligned nt us dest off + win t rd σ3 res.2.1 res.2.2 st ⦄ := by + intro s3 a3 σ3 eres J3 + obtain ⟨C1, -, hw1⟩ := utCrtP_resetA hA R e a a3 hU eres + have kb3 : L.ksb s3 = L.ksb s := hw1.1.trans kb2 + rw [C0.heapB, C0.slotsB] at C1 + have C : CrtP st (dropHA (utDA p sb.val) H) Z.Cs (dropHA (utDA p sb.val) H).frame (dropSetA (utDA p sb.val) OS) s3 + (cslotAddrA sl) p sb 0#u64 false aligned nt us dest off win := + { heap := C1.heap, os := J3.sys.ent.os, stz := C1.stz, fr := C1.fr, cz := C1.cz, src := C1.src, cap := C1.cap, + hn := C1.hn, hus := C1.hus, hz := C1.hz, ar := C1.ar, core0 := C1.core0, n0 := C1.n0, + nc := fun hb => capw_ne_node J3.sq hb C1.core0 (o := layout.node.DEBUG_TCBS) (by rw [debug_val]; omega) rfl, + hd := C1.hd, dO := C1.dO, dn := C1.dn, dc := C1.dc, zO := C1.zO } + obtain ⟨hdest, hrd, hrd0, yc, rr, gs, g, ySlot, ycap⟩ := X.cnode + have yS' : IsSlotA (dropThA (utDA p sb.val) Z.th) yc := by + by_contra h + have := C0.gone yc (hsub yc ySlot) ySlot h + rw [this] at ycap + cases ycap + have destK : specPtr dest ∉ utDA p sb.val := by + have := C0.noCap yc yS' + rw [show capAtA (L.mem s) yc = capA st s yc from rfl, ycap] at this + exact this + have X' : UtCtxA st (dropZA (utDA p sb.val) Z).th s σ sl p sb fi false nt us dest off win t rd true fiv := + { X with cnode := ⟨dropThA_someA.2 ⟨destK, hdest⟩, hrd, hrd0, yc, rr, gs, g, yS', ycap⟩ } + obtain ⟨-, -, -, -, hth', -⟩ := utReset_startA J D P hcapF he RS.sym othT0 othC0 hn sl hq hth rfl + have hUe := utEnd_resetA hA R J.sys.sysl.ly e a a3 hU eres hHH' hEx hHn + rw [C0.heapB, C0.slotsB] at hUe + exact utCreate_postA hA J3 C X' hcap ⟨qc, hq, hth'⟩ (WP.spec_mono hUe fun _ h => ⟨_, _, h⟩) hb0 hal8 hpf hal2 + (by rw [kb3]; exact hPd) hno hVP hKt hK0 hkw' + refine utRetype_resetPpA ar sl hq hth Y hn hPost hchunk hnd Pc hvp hC hNow hAr hirq hmb (hT0 y hkT) aligned nt us dest + off win _ ?hnoop ?hsmall ?hbig ?hpre + case hpre => + intro s3 a3 σ3 hm J3 + exact ⟨_, _, _, _, _, rfl, y, hkT, aligned, nt, us, dest, off, win, rd, hU, rfl, rfl, rfl, rfl, + Or.inr ⟨rfl, σ3, hm, J3⟩⟩ + case hsmall => + intro _ _ s3 σ3 eres hm J3 _ _ + refine WP.spec_mono (drop s3 a σ3 eres J3) fun res h => ?_ + exact ⟨_, _, _, _, _, rfl, y, hkT, aligned, nt, us, dest, off, win, rd, hU, rfl, rfl, rfl, rfl, + Or.inl ⟨h.1, σ3, hm, _, _, Or.inr ⟨rfl, rfl⟩, h.2⟩⟩ + case hbig => + intro _ _ s3 a3 σ3 _ _ eres hm I _ + refine WP.spec_mono (drop s3 a3 σ3 eres I.rec_) fun res h => ?_ + exact ⟨_, _, _, _, _, rfl, y, hkT, aligned, nt, us, dest, off, win, rd, hU, rfl, rfl, rfl, rfl, + Or.inl ⟨h.1, σ3, hm, _, _, Or.inr ⟨rfl, rfl⟩, h.2⟩⟩ + case hnoop => + intro h0 eres hm + obtain ⟨C1, -, -⟩ := utCrtP_resetA hA R e a a hU eres + have hc2 : capA st s2 (cslotAddrA sl) = .Untyped p sb 0#u64 false := by + rw [← h0]; exact okinj ((capA_eq s2 _).symm.trans Y.cap) + have offH : ∀ q k, H q = some k → q.val + 2 ^ k.bits ≤ p.val ∨ p.val + 2 ^ sb.val ≤ q.val := by + intro q k hk + have g := J2.sys.sysl.sys.stz.1.2.2.1 (cslotAddrA sl) hsO p sb 0#u64 hc2 q k hk + simpa using g + have eH : H.drop (RegionD H p.val (p.val + 2 ^ sb.val)) = H := by + funext q + unfold Heap.drop + cases hk : H q with + | none => split_ifs <;> rfl + | some k => + have nR : ¬ RegionD H p.val (p.val + 2 ^ sb.val) q := fun hR => by + unfold RegionD MeetsR at hR + rw [hk] at hR + simp only [Option.elim] at hR + have := offH q k hk + omega + rw [ite_eq_right nR] + have eO : OffR p.val (p.val + 2 ^ sb.val) OS = OS := by + refine Set.ext fun z => ⟨fun h => h.1, fun h => ⟨h, ?_⟩⟩ + obtain ⟨Pt, Ub7, vr, pool, armc, capc, vcc, frd, hic, sched, apart, frub, alub, vcub, Pp7, Tb7, Hr, Te, Kt, Lv7, Re, bits, r2, + kt, hkl⟩ := J2.j7 + obtain ⟨-, q, k, hk, h1, h2, -, -⟩ := armc.case.slot z h + have := offH q k hk + have := Nat.two_pow_pos sb.val + omega + rw [eH, eO] at C1 + have hUe := utEnd_resetA hA R J.sys.sysl.ly e a a hU eres hHH' hEx hHn + rw [eH, eO] at hUe + refine WP.spec_mono (utCreate_postA hA J2 C1 X hcap ⟨qc, hq, hth⟩ (WP.spec_mono hUe fun _ h => ⟨_, _, h⟩) hb0 hal8 + hpf hal2 (by rw [kb2]; exact hPk) hno hVP hKt hK0 hkw') fun res h => ?_ + exact ⟨_, _, _, _, _, rfl, y, hkT, aligned, nt, us, dest, off, win, rd, hU, rfl, rfl, rfl, rfl, + Or.inl ⟨h.1, σ2, hm, Z, m, Or.inl ⟨rfl, rfl⟩, h.2⟩⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtRtResetPpA.lean b/proofs/Fl4ProofsA64/P3/UtRtResetPpA.lean new file mode 100644 index 000000000..1178c832e --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtRtResetPpA.lean @@ -0,0 +1,115 @@ +/- +P3 (aarch64, A64-P3 Untyped, J8): **`utRetype_resetA` with the preemption point discharged** (`utPp_hookA`). + +* **`utRetype_resetPpA`**: a retype that resets its RAM untyped first; no preemption-point premise. + +Named premises: `utRetype_resetA`'s other than `hpp`, and `utPp_hookA`'s ([J] `hvp`; [P] `P`, `hC`, `hNow`, `hAr`, +`hirq`, `hmb`, `hnd`); [J] `hT0`: the preemption point's facts (`UtPpTimeA`) after `delete_objects`. + +New theorems only. +-/ +import Fl4ProofsA64.P3.UtRtResetA +import Fl4ProofsA64.P3.UtPpHookA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4SpecA64 Fl4Spec Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +set_option maxRecDepth 100000 + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {m : McState} + +variable {Ud Acd Rq Nt : Set U64} {ex : Aeneas.Std.Array obj.Slot 3#usize} {p sb fi : U64} + +set_option maxHeartbeats 1000000 in +/-- **A retype that resets its RAM untyped first**, preemption point included (module docstring). -/ +theorem utRetype_resetPpA [Fact (n ≤ Fl4SpecA64.fl4A64Platform.numNodes)] (ar : machine.Arch A) {s : S} {a : A} {σ : State Unit} + (cte : CslotPtr) {qc : U64} (hq : cte.1 = specPtr qc) (hth : Z.th qc = some (.cnode cte.2.length)) + (Y : UtRstReadyA (ar := ar) st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n nA m Ud Acd Rq Nt ex (cslotAddrA cte) + p sb fi s a σ) + (hn : nA.val = fl4A64Platform.s2IpaBits) + (hPost : ∀ σ' : State Unit, ((), σ') ∈ (archPostResetUntyped (Z := Unit) false sb.val σ').1) + (hchunk : resetChunkBits = 16) + (hnd : numDomains = 1) {Clock : A → MachineState → Prop} (P : NowAgreeA ar Clock) + (hvp : ∀ (s : S) (a : A) (σ : State Unit), + JStage3A st AR (dropZA (utDA p sb.val) Z) (dropHA (utDA p sb.val) H) (dropSetA (utDA p sb.val) OS) U Ac + (dropSetA (utDA p sb.val) Pp) Tb Lv ia ie (dropSetA (utDA p sb.val) N) (dropSetA (utDA p sb.val) E) + (dropSetA (utDA p sb.val) Rp) (utBfOffA (utDA p sb.val) (dropBfA (utDA p sb.val) Bf)) stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s a σ (dropMA (utDA p sb.val) m) → + ∀ b, VsR (L.ksb s) (dropSetA (utDA p sb.val) Pp ∪ Tb) b → Aligned b ∧ ¬ PPW st s b) + (hC : ∀ i, fl4RawCounterOracle i ≤ fl4RawCounterOracle (i + 1)) + (hNow : ∀ σ : State Unit, fl4ReadRawCounter σ.machineState = getCurrentTime σ.machineState) + (hAr : ∀ (s : S) (a : A), AR a Z.hw → sched.update_timestamp st ar s a ⦃ r => AR r.2 Z.hw ⦄) + (hirq : ∀ (a : A) (σ : State Unit), AR a Z.hw → Clock a σ.machineState → + ar.irq_pending a ⦃ r => ∃ o ms, (o, { σ with machineState := ms }) ∈ + (doMachineOp (getActiveIRQ true) σ).1 ∧ (r.1 = true ↔ ¬ o = none) ∧ + ms.underlyingMemory = σ.machineState.underlyingMemory ∧ + ms.lastMachineTime = σ.machineState.lastMachineTime ∧ ms.timeState = σ.machineState.timeState ∧ + Clock r.2 ms ∧ AR r.2 Z.hw ⦄) + (hmb : ∀ a : A, sporadic.min_budget ar a ⦃ mb => mb.bv = Fl4SpecA64.MIN_BUDGET ⦄) + (hT0 : UtPpTimeA Clock a (utDelStA (specPtr p) sb.val σ)) + (base nt us dest off win : U64) + (K : core.result.Result Unit error.Exc × S × A → Prop) + -- an unused untyped: the creation from the same state + (hnoop : fi = 0#u64 → untyped.reset_untyped st ar s a (cslotAddrA cte) = ok (core.result.Result.Ok (), s, a) → + (.ok (), σ) ∈ ((resetUntypedCap (Z := Unit) cte).run σ).1 → + untyped.retype st ar s a (cslotAddrA cte) false base nt us dest off win false ⦃ r => K r ⦄) + -- used, below a chunk + (hsmall : fi ≠ 0#u64 → sb.val < 16 → ∀ (s3 : S) (σ3 : State Unit), + untyped.reset_untyped st ar s a (cslotAddrA cte) = ok (core.result.Result.Ok (), s3, a) → + (.ok (), σ3) ∈ ((resetUntypedCap (Z := Unit) cte).run σ).1 → + JStage3A st AR (dropZA (utDA p sb.val) Z) (dropHA (utDA p sb.val) H) (dropSetA (utDA p sb.val) OS) U Ac + (dropSetA (utDA p sb.val) Pp) Tb Lv ia ie (dropSetA (utDA p sb.val) N) (dropSetA (utDA p sb.val) E) + (dropSetA (utDA p sb.val) Rp) (utBfOffA (utDA p sb.val) (dropBfA (utDA p sb.val) Bf)) stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s3 a σ3 (dropMA (utDA p sb.val) m) → + SymL st (dropHA (utDA p sb.val) H) Z.Cs s3 → capA st s3 (cslotAddrA cte) = .Untyped p sb 0#u64 false → + untyped.retype st ar s3 a (cslotAddrA cte) false base nt us dest off win false ⦃ r => K r ⦄) + -- used, a chunk or more + (hbig : fi ≠ 0#u64 → 16 ≤ sb.val → ∀ (s3 : S) (a3 : A) (σ3 : State Unit) (Rq' Nt' : Set U64), + untyped.reset_untyped st ar s a (cslotAddrA cte) = ok (core.result.Result.Ok (), s3, a3) → + (.ok (), σ3) ∈ ((resetUntypedCap (Z := Unit) cte).run σ).1 → + UtRstInvA (ar := ar) st AR (dropZA (utDA p sb.val) Z) (dropHA (utDA p sb.val) H) + (dropSetA (utDA p sb.val) OS) U Ac (dropSetA (utDA p sb.val) Pp) Tb Lv ia ie (dropSetA (utDA p sb.val) N) + (dropSetA (utDA p sb.val) E) (dropSetA (utDA p sb.val) Rp) + (utBfOffA (utDA p sb.val) (dropBfA (utDA p sb.val) Bf)) stack κ n nA (dropMA (utDA p sb.val) m) p sb + (cslotAddrA cte) Rq' Nt' (Array.repeat 3#usize 0#u64) s3 a3 σ3 0#u64 → UtPpTimeA Clock a3 σ3 → + untyped.retype st ar s3 a3 (cslotAddrA cte) false base nt us dest off win false ⦃ r => K r ⦄) + -- preempted + (hpre : ∀ (s3 : S) (a3 : A) (σ3 : State Unit), + (.error (), σ3) ∈ ((resetUntypedCap (Z := Unit) cte).run σ).1 → + JStage3A st AR (dropZA (utDA p sb.val) Z) (dropHA (utDA p sb.val) H) (dropSetA (utDA p sb.val) OS) U Ac + (dropSetA (utDA p sb.val) Pp) Tb Lv ia ie (dropSetA (utDA p sb.val) N) (dropSetA (utDA p sb.val) E) + (dropSetA (utDA p sb.val) Rp) (utBfOffA (utDA p sb.val) (dropBfA (utDA p sb.val) Bf)) stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s3 a3 σ3 (dropMA (utDA p sb.val) m) → K (.Err .Preempted, s3, a3)) : + untyped.retype st ar s a (cslotAddrA cte) true base nt us dest off win false ⦃ r => K r ⦄ := by + refine utRetype_resetA ar cte hq hth Y hn hPost hchunk + (fun _ s a σ => JStage3A st AR (dropZA (utDA p sb.val) Z) (dropHA (utDA p sb.val) H) + (dropSetA (utDA p sb.val) OS) U Ac (dropSetA (utDA p sb.val) Pp) Tb Lv ia ie (dropSetA (utDA p sb.val) N) + (dropSetA (utDA p sb.val) E) (dropSetA (utDA p sb.val) Rp) + (utBfOffA (utDA p sb.val) (dropBfA (utDA p sb.val) Bf)) stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ + (dropMA (utDA p sb.val) m)) (UtPpTimeA Clock) (fun a σ σ' T H h => T.cnStep H h) hT0 ?_ base nt us dest off win K + hnoop hsmall hbig (fun s3 a3 σ3 _ hm hR => hpre s3 a3 σ3 hm hR) + intro Rq' Nt' o _ s a σ h + refine WP.spec_mono (utPp_hookA (AR := AR) (Z := dropZA (utDA p sb.val) Z) ar hnd P hvp hC hNow hAr hirq hmb + s a σ h) ?_ + rintro r ⟨k, σ', g⟩ + refine ⟨k, σ', ?_⟩ + rcases g with g | ⟨g1, g2, g3⟩ + · exact Or.inl g + · exact Or.inr ⟨g1, g2, g3.1.rec_⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtRtSchedA.lean b/proofs/Fl4ProofsA64/P3/UtRtSchedA.lean new file mode 100644 index 000000000..c3eaaa712 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtRtSchedA.lean @@ -0,0 +1,222 @@ +/- +P3 (aarch64, A64-P3 Untyped W5): **the scheduler and VCPU records after a retype's creation**. + +A retype adds new TCBs, SCs or VCPUs that nothing refers to: a new TCB is `default_tcb` over zero words (affinity 0 +in the view, D5), a new SC the default SC bound to nothing, a new VCPU unbound. + +* `utRtMA`: the view after the creation (`tcbAffinity` of the new TCBs is `some 0`). +* `utRt_schedRelA`, `utRt_schedRestA`, `utRt_vcpuCoreA`: `SchedRelA`, `SchedRestA`, `VcpuCoreA` over the grown sets. + +Hypotheses: the creation keeps the old objects' words and spec objects and the scheduler globals; each new object's +spec default and words; no new SC is at the view's idle SC or at `idle_sc_ptr`. No named premise. New definitions +and theorems only. +-/ +import Fl4ProofsA64.P3.JointA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-- The view after a creation that made the TCBs `Tn`. -/ +def utRtMA (m : Fl4SpecA64.McState) (Tn : Set U64) : Fl4SpecA64.McState := + { m with tcbAffinity := fun p => if ∃ t ∈ Tn, specPtr t = p then some 0 else m.tcbAffinity p } + +theorem utRtMA_scRen (m : Fl4SpecA64.McState) (Tn : Set U64) (p : U64) : scRenA (utRtMA m Tn) p = scRenA m p := rfl + +theorem utRtMA_optSc (m : Fl4SpecA64.McState) (Tn : Set U64) (w : U64) : optScA (utRtMA m Tn) w = optScA m w := rfl + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **`SchedRelA` after the creation.** -/ +theorem utRt_schedRelA {T Tn C Cn Cs : Set U64} {curOf curScOf : Fl4SpecA64.CoreId → Fl4SpecA64.ObjRef} + {s s' : S} {σ σ' : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (h : SchedRelA (L := L) T C Cs curOf curScOf s σ m) + -- the creation keeps + (kcpu : L.cpu s' = L.cpu s) + (knode : ∀ c ∈ Cs, ∀ o : U64, o = layout.node.CUR_THREAD ∨ o = layout.node.CUR_SC ∨ + o = layout.node.IDLE_THREAD ∨ o = layout.node.IDLE_SC → L.mem s' (L.nodeAt s' c o) = L.mem s (L.nodeAt s c o)) + (kidle : L.idleW s' = L.idleW s) (kact : L.actionW s' = L.actionW s) (ktime : L.curTime s' = L.curTime s) + (kt : ∀ t ∈ T, L.mem s' (affA t) = L.mem s (affA t) ∧ L.mem s' (stA t) = L.mem s (stA t) ∧ + L.mem s' (scA t) = L.mem s (scA t)) + (kc : ∀ c ∈ C, L.mem s' (wa c 24#u64) = L.mem s (wa c 24#u64)) + (kth : ∀ t ∈ T, σ'.kheap (specPtr t) = σ.kheap (specPtr t)) + (kch : ∀ c ∈ C, σ'.kheap (scRenA m c) = σ.kheap (scRenA m c)) + (kσ : σ'.curThread = σ.curThread ∧ σ'.curSc = σ.curSc ∧ σ'.idleThread = σ.idleThread ∧ + σ'.schedulerAction = σ.schedulerAction ∧ σ'.curTime = σ.curTime) + -- the new threads + (hnt : ∀ t ∈ Tn, (L.mem s' (affA t)).val = 0 ∧ L.mem s' (stA t) = 0#u64 ∧ L.mem s' (scA t) = 0#u64 ∧ + ∃ x, σ'.kheap (specPtr t) = some (.tcb x) ∧ x.tcbState = .inactive ∧ x.tcbSchedContext = none) + -- the new SCs + (hnc : ∀ c ∈ Cn, L.mem s' (wa c 24#u64) = 0#u64 ∧ specPtr c ≠ m.idleScOf m.curCore ∧ + specPtr c ≠ Fl4SpecA64.idleScPtr ∧ ∃ y k, σ'.kheap (specPtr c) = some (.schedContext y k) ∧ y.scTcb = none) : + SchedRelA (L := L) (T ∪ Tn) (C ∪ Cn) Cs curOf curScOf s' σ' (utRtMA m Tn) := by + obtain ⟨σ1, σ2, σ3, σ4, σ5⟩ := kσ + have aff0 : ∀ t ∈ Tn, (utRtMA m Tn).tcbAffinity (specPtr t) = some 0 := fun t ht => + ite_eq_left (⟨t, ht, rfl⟩ : ∃ u ∈ Tn, specPtr u = specPtr t) + refine + { core := by rw [kcpu]; exact h.core + cpuCs := by rw [kcpu]; exact h.cpuCs + curOfW := fun c hc => by rw [knode c hc _ (Or.inl rfl)]; exact h.curOfW c hc + curScOfW := fun c hc => by + rw [knode c hc _ (Or.inr (Or.inl rfl)), utRtMA_scRen]; exact h.curScOfW c hc + cur := by rw [σ1]; exact h.cur + csc := by rw [σ2]; exact h.csc + idle := by rw [σ3, kidle]; exact h.idle + idleOf := fun c hc => by rw [knode c hc _ (Or.inr (Or.inr (Or.inl rfl)))]; exact h.idleOf c hc + idleScOf := fun c hc => by rw [knode c hc _ (Or.inr (Or.inr (Or.inr rfl)))]; exact h.idleScOf c hc + act := by rw [σ4, kact]; exact h.act + time := by rw [σ5, ktime]; exact h.time + aff := fun t ht => ?_ + affT := fun p c hp => ?_ + tcb := fun t ht => ?_ + sc := fun c hc => ?_ + scOff := fun c hc ne => ?_ } + · by_cases hn : t ∈ Tn + · rw [aff0 t hn, (hnt t hn).1] + · have ho : t ∈ T := ht.resolve_right hn + have e : (utRtMA m Tn).tcbAffinity (specPtr t) = m.tcbAffinity (specPtr t) := + ite_eq_right (fun ⟨u, hu, e⟩ => hn (specPtr_inj e ▸ hu)) + rw [e, (kt t ho).1] + exact h.aff t ho + · change (if ∃ t ∈ Tn, specPtr t = p then some 0 else m.tcbAffinity p) = some c at hp + split at hp + case isTrue g => + obtain ⟨t, ht, e⟩ := g + exact ⟨t, Or.inr ht, e⟩ + case isFalse => + obtain ⟨t, ht, e⟩ := h.affT p c hp + exact ⟨t, Or.inl ht, e⟩ + · by_cases hn : t ∈ Tn + · obtain ⟨-, h1, h2, x, hx, e1, e2⟩ := hnt t hn + refine ⟨x, hx, ?_, ?_⟩ + · rw [h1, e1]; rfl + · rw [h2, e2]; rfl + · have ho : t ∈ T := ht.resolve_right hn + obtain ⟨x, hx, e1, e2⟩ := h.tcb t ho + refine ⟨x, (kth t ho).trans hx, by rw [(kt t ho).2.1]; exact e1, ?_⟩ + rw [(kt t ho).2.2, utRtMA_optSc] + exact e2 + · rw [utRtMA_scRen] + rcases hc with ho | hn + · obtain ⟨y, k, hy, e⟩ := h.sc c ho + exact ⟨y, k, (kch c ho).trans hy, by rw [kc c ho]; exact e⟩ + · obtain ⟨h1, h2, -, y, k, hy, e⟩ := hnc c hn + have er : scRenA m c = specPtr c := ite_eq_right h2 + refine ⟨y, k, by rw [er]; exact hy, ?_⟩ + rw [h1, e]; rfl + · rcases hc with ho | hn + · exact h.scOff c ho ne + · exact (hnc c hn).2.2.1 + +/-- **`SchedRestA` after the creation.** -/ +theorem utRt_schedRestA {Z Z' : XSetsA} {Tn Cn : Set U64} {s s' : S} {σ σ' : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} (h : SchedRestA (L := L) Z s σ m) + (eT : Z'.T = Z.T ∪ Tn) (eC : Z'.C = Z.C ∪ Cn) (eQ : Z'.Q = Z.Q) (eR : Z'.R = Z.R) + (kcpu : L.cpu s' = L.cpu s) (kksb : L.ksb s' = L.ksb s) + (kn : ∀ o : U64, o = layout.node.CONSUMED ∨ o = layout.node.REPROGRAM → + L.mem s' (L.nodeAt s' (L.cpu s) o) = L.mem s (L.nodeAt s (L.cpu s) o)) + (kg : L.mem s' (wa (L.ksb s) 8#u64) = L.mem s (wa (L.ksb s) 8#u64) ∧ + L.mem s' (wa (L.ksb s) 16#u64) = L.mem s (wa (L.ksb s) 16#u64)) + (kσ : σ'.readyQueues = σ.readyQueues ∧ σ'.releaseQueue = σ.releaseQueue ∧ σ'.consumedTime = σ.consumedTime ∧ + σ'.reprogramTimer = σ.reprogramTimer ∧ σ'.curDomain = σ.curDomain ∧ σ'.domainTime = σ.domainTime) + (kt : ∀ t ∈ Z.T, L.mem s' (domA t) = L.mem s (domA t) ∧ L.mem s' (prioA t) = L.mem s (prioA t) ∧ + L.mem s' (scA t) = L.mem s (scA t)) + (kc : ∀ c ∈ Z.C, L.mem s' (wa c 72#u64) = L.mem s (wa c 72#u64)) + (kth : ∀ t ∈ Z.T, σ'.kheap (specPtr t) = σ.kheap (specPtr t)) + (kch : ∀ c ∈ Z.C, σ'.kheap (scRenA m c) = σ.kheap (scRenA m c)) + (hnt : ∀ t ∈ Tn, L.mem s' (domA t) = 0#u64 ∧ (L.mem s' (prioA t)).val = 0 ∧ L.mem s' (scA t) = 0#u64 ∧ + ∃ x, σ'.kheap (specPtr t) = some (.tcb x) ∧ x.tcbDomain = 0 ∧ x.tcbPriority = 0) + (hnc : ∀ c ∈ Cn, ∃ y k, σ'.kheap (scRenA m c) = some (.schedContext y k) ∧ + y.scRefillMax = (L.mem s' (wa c 72#u64)).val) : + SchedRestA (L := L) Z' s' σ' (utRtMA m Tn) := by + obtain ⟨q1, q2, q3, q4, q5, q6⟩ := kσ + refine + { rq := fun p hp => by rw [q1, eQ, kcpu]; exact h.rq p hp + rqDom := fun d p hd => by rw [q1]; exact h.rqDom d p hd + rel := by rw [q2, eR, kcpu]; exact h.rel + consumed := by rw [q3, kcpu, kn _ (Or.inl rfl)]; exact h.consumed + repr := by rw [q4, kcpu, kn _ (Or.inr rfl)]; exact h.repr + curDom := by rw [q5, kksb, kg.1]; exact h.curDom + domTime := by rw [q6, kksb, kg.2]; exact h.domTime + prio := fun t ht => ?_ + scMem := fun t ht ne => ?_ + refillMax := fun c hc => ?_ } + · rw [eT] at ht + by_cases hn : t ∈ Tn + · obtain ⟨h1, h2, -, x, hx, e1, e2⟩ := hnt t hn + refine ⟨x, hx, e1, h1, ?_⟩ + rw [h2, e2]; rfl + · have ho : t ∈ Z.T := ht.resolve_right hn + obtain ⟨x, hx, e1, e2, e3⟩ := h.prio t ho + exact ⟨x, (kth t ho).trans hx, e1, by rw [(kt t ho).1]; exact e2, by rw [(kt t ho).2.1]; exact e3⟩ + · rw [eT] at ht + rw [eC] + by_cases hn : t ∈ Tn + · exact absurd (hnt t hn).2.2.1 ne + · have ho : t ∈ Z.T := ht.resolve_right hn + rw [(kt t ho).2.2] at ne ⊢ + exact Or.inl (h.scMem t ho ne) + · rw [eC] at hc + rw [utRtMA_scRen] + rcases hc with ho | hn + · obtain ⟨y, k, hy, e⟩ := h.refillMax c ho + exact ⟨y, k, (kch c ho).trans hy, by rw [kc c ho]; exact e⟩ + · exact hnc c hn + +/-- **`VcpuCoreA` after the creation.** -/ +theorem utRt_vcpuCoreA {Z Z' : XSetsA} {Tn Vn : Set U64} {s s' : S} {σ σ' : Fl4SpecA64.State Unit} + (h : VcpuCoreA (L := L) Z s σ) (eT : Z'.T = Z.T ∪ Tn) (eV : Z'.V = Z.V ∪ Vn) + (kv : ∀ v ∈ Z.V, L.mem s' v = L.mem s v ∧ L.mem s' (wa v 24#u64) = L.mem s (wa v 24#u64)) + (kt : ∀ t ∈ Z.T, L.mem s' (wa t 3664#u64) = L.mem s (wa t 3664#u64) ∧ + L.mem s' (runVcpuA t) = L.mem s (runVcpuA t)) + (kvh : ∀ v ∈ Z.V, σ'.kheap (specPtr v) = σ.kheap (specPtr v)) + (kth : ∀ t ∈ Z.T, σ'.kheap (specPtr t) = σ.kheap (specPtr t)) + (back : ∀ p y, σ'.kheap p = some (.archObj (.vcpu y)) → + σ.kheap p = some (.archObj (.vcpu y)) ∨ ∃ v ∈ Vn, specPtr v = p) + (hnv : ∀ v ∈ Vn, L.mem s' v = 0#u64 ∧ L.mem s' (wa v 24#u64) = 0#u64 ∧ + ∃ y, σ'.kheap (specPtr v) = some (.archObj (.vcpu y)) ∧ y.vcpuTcb = none ∧ y.vcpuExt.runCaller = none) + (hnt : ∀ t ∈ Tn, L.mem s' (wa t 3664#u64) = 0#u64 ∧ L.mem s' (runVcpuA t) = 0#u64 ∧ + ∃ x, σ'.kheap (specPtr t) = some (.tcb x) ∧ x.tcbArch.tcbVcpu = none ∧ x.tcbExt.tcbRunVcpu = none) + (hcap : ∀ y, IsSlotA Z'.th y → ∀ p, capAtA (L.mem s') y = .Vcpu p → + (∃ y0, IsSlotA Z.th y0 ∧ capAtA (L.mem s) y0 = .Vcpu p) ∨ p ∈ Vn) : + VcpuCoreA (L := L) Z' s' σ' := by + refine ⟨fun v hv => ?_, fun p y hp => ?_, fun t ht => ?_, fun y hy p hc => ?_⟩ + · rw [eV] at hv + by_cases hn : v ∈ Vn + · obtain ⟨h1, h2, z, hz, e1, e2⟩ := hnv v hn + refine ⟨z, hz, ?_, ?_⟩ + · rw [h1, e1]; rfl + · rw [h2, e2]; rfl + · have ho : v ∈ Z.V := hv.resolve_right hn + obtain ⟨z, hz, e1, e2⟩ := h.obj v ho + exact ⟨z, (kvh v ho).trans hz, by rw [(kv v ho).1]; exact e1, by rw [(kv v ho).2]; exact e2⟩ + · rw [eV] + rcases back p y hp with g | ⟨v, hv, e⟩ + · obtain ⟨v, hv, e⟩ := h.back p y g + exact ⟨v, Or.inl hv, e⟩ + · exact ⟨v, Or.inr hv, e⟩ + · rw [eT] at ht + by_cases hn : t ∈ Tn + · obtain ⟨h1, h2, x, hx, e1, e2⟩ := hnt t hn + refine ⟨x, hx, ?_, ?_⟩ + · rw [h1, e1]; rfl + · rw [h2, e2]; rfl + · have ho : t ∈ Z.T := ht.resolve_right hn + obtain ⟨x, hx, e1, e2⟩ := h.tcb t ho + exact ⟨x, (kth t ho).trans hx, by rw [(kt t ho).1]; exact e1, by rw [(kt t ho).2]; exact e2⟩ + · rw [eV] + rcases hcap y hy p hc with ⟨y0, hy0, h0⟩ | g + · exact Or.inl (h.cap y0 hy0 p h0) + · exact Or.inr g + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtRtSiteA.lean b/proofs/Fl4ProofsA64/P3/UtRtSiteA.lean new file mode 100644 index 000000000..c68047ff6 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtRtSiteA.lean @@ -0,0 +1,284 @@ +/- +P3 (aarch64, A64-P3 Untyped W5): **the decode-site discharges for the post-creation relations** (lead 02:25Z). + +* `utRetype_nfiA`: from A64 P1's creation-start record `CrtP`, FL4's creation leaves the untyped slot with free index + `aligned + win·2^z − p`. A sibling of `CrtE.src`, which only says some free index; no P1 statement changes. +* `utSrc_relA`: hence the lowered untyped's cap relation (`utEnd_stateRelA`'s `hsrcRel`). +* `utCdt_freeA`: the spec has no CDT parent for a slot outside the typed heap (`CdtRelA.cdt_slot`). +* `utWalk_srcA` / `utCtx_dstA` / `utCtx_snA`: the spec / FL4 slot correspondence of the source (from the lookup's + `SlotOkA`) and of the destinations (from `UtCtxA`). +* **`utSite_relsA`**: `StateRelA` and `CdtRelA` after all cap writes, with the above discharged. + +Hypotheses of `utSite_relsA`: A64 P1's `UtEnd`; the untyped cap and `StateRelA` / `CdtRelA` at the creation's start; +`hsub` / `hnul` (closure item A); `hfree`; the new typed heap's placement; `hnewRel`; the FL4 tree and revocable bits +after the creation (`utRetype_treeXA`'s outcome); and `horigfree` (the spec's `is_original_cap` is false at the new +objects' slots — no record field gives it). No named premise. +-/ +import Fl4ProofsA64.P3.UtRtCdtA +import Fl4ProofsA64.P3.UtRtCapsA +import Fl4ProofsA64.P3.UtDecodeA +import Fl4ProofsA64.P1.MdbLiveU +import Fl4ProofsA64.P1.MdbUZR +import Fl4ProofsA64.P1.MdbUZT +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section run +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + +/-- **The creation's free index** (module docstring). -/ +theorem utRetype_nfiA (hA : P0.ArchTotal ar) {H1 : Heap} {Cs Sg OS1 : Set U64} {s1 : S} + {slot p sb fi1 : U64} {dev : Bool} {aligned nt us dest off win : U64} + (C : CrtP st H1 Cs Sg OS1 s1 slot p sb fi1 dev aligned nt us dest off win) (a1 : A) : + untyped.retype st ar s1 a1 slot false aligned nt us dest off win dev ⦃ r => + ∃ nfi : U64, nfi.val = aligned.val + win.val * 2 ^ (zA nt.val us).val - p.val ∧ + capA st r.2.1 slot = .Untyped p sb nfi dev ⦄ := by + obtain ⟨hdev, hal, hfi, hA0, hAw, hinj, hcan, -⟩ := C.ar + obtain ⟨hSA, -, hK1⟩ := C.stz + obtain ⟨hS1', -, hV1, hUZ1, -⟩ := hSA + obtain ⟨G1, par1, hM1, -, -, hNl1, -, hTr1, hUi1⟩ := id hS1' + have hT : TreeA st OS1 G1 par1 s1 := ⟨hM1, hTr1, hUi1⟩ + have hnA := nodeAt_aligned C.n0 (by rw [debug_val]; omega) (by rw [debug_val]) + have hnF := KsOffA.node hK1 C.core0 C.n0 (by rw [debug_val]; omega) + obtain ⟨-, -, hpf⟩ := ut_rangeA (capA_ut_b C.cap) (by + have := hT.ut.reg slot C.src (by rw [C.cap]; exact physA_ut _ _ _ _); rwa [C.cap] at this) + cases dev + · refine WP.spec_mono (untyped.retype.lvRG hA C.heap C.os hT hNl1 hV1 hUZ1 a1 C.src C.cap nt us dest off win aligned + false rfl C.hn C.hz hal hfi hA0 hAw hnA C.nc hnF C.hd C.dO C.dn C.dc hinj hcan C.hus + (Z2 := fun _ _ => True) + (fun s' a' j hj hq _ _ => WP.spec_mono (create_object.hCOZ hA C.heap C.os hV1 C.src C.cap nt us win aligned false + C.hn (fun h => by cases h) hal hA0 hAw hpf hnA C.nc (hnF slot C.src p sb fi1 C.cap) s' a' j hj hq) + fun _ _ => trivial) + (fun _ _ _ _ _ => trivial) (fun _ _ => trivial)) ?_ + rintro r ⟨-, ⟨-, nfi, hv, hc⟩, -, -⟩ + exact ⟨nfi, hv, hc⟩ + · refine WP.spec_mono (untyped.retype.lvDC C.heap C.os hT hNl1 hV1 hUZ1 a1 C.src C.cap nt us dest off win aligned + true rfl C.hn C.hz (hdev rfl) hfi hA0 hAw hnF C.dO C.dn C.dc hinj hcan C.hus) ?_ + rintro r ⟨-, ⟨-, nfi, hv, hc⟩, -⟩ + exact ⟨nfi, hv, hc⟩ + +end run + +/-- The lowered untyped's spec free index is FL4's. -/ +theorem utFreeRef_valA {p aligned nfi : U64} {n z : Nat} (_hz : z < 64) (hp : p.val ≤ aligned.val) + (hn : n < 2 ^ 64) (hv : nfi.val = aligned.val + n * 2 ^ z - p.val) : + unat (specPtr aligned + BitVec.ofNat 64 n <<< z - specPtr p) = nfi.val := by + have e : aligned.bv + BitVec.ofNat 64 n <<< z = p.bv + nfi.bv := by + apply BitVec.eq_of_toNat_eq + have ea : aligned.bv.toNat = aligned.val := rfl + have ep : p.bv.toNat = p.val := rfl + have ev : nfi.bv.toNat = nfi.val := rfl + rw [BitVec.toNat_add, BitVec.toNat_add, BitVec.toNat_shiftLeft, BitVec.toNat_ofNat, Nat.mod_eq_of_lt hn, + Nat.shiftLeft_eq, Nat.add_mod_mod, ea, ep, ev] + congr 1 + omega + have e2 : specPtr aligned + BitVec.ofNat 64 n <<< z - specPtr p = nfi.bv := by + rw [specPtr_def, specPtr_def] + have : aligned.bv + pptrShiftA + BitVec.ofNat 64 n <<< z - (p.bv + pptrShiftA) = + aligned.bv + BitVec.ofNat 64 n <<< z - p.bv := by + generalize BitVec.ofNat 64 n <<< z = x + generalize pptrShiftA = k + ring + rw [this, e] + ring + rw [e2] + rfl + +section src +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **`hsrcRel`** (module docstring). -/ +theorem utSrc_relA {th : TypedHeapA} {s1 s' : S} {σ : State Unit} (hS : StateRelA th (L.mem s1) σ) + {srcSlot : CslotPtr} (hsrc : ∃ q, srcSlot.1 = specPtr q ∧ th q = some (.cnode srcSlot.2.length)) + {p sb fi1 nfi aligned : U64} {dev : Bool} {n z : Nat} + (hc1 : capA st s1 (cslotAddrA srcSlot) = .Untyped p sb fi1 dev) + (hc' : capA st s' (cslotAddrA srcSlot) = .Untyped p sb nfi dev) (hz : z < 64) (hp : p.val ≤ aligned.val) + (hn : n < 2 ^ 64) (hv : nfi.val = aligned.val + n * 2 ^ z - p.val) + {capS : Cap} (hcap : ∃ rS cs, σ.kheap srcSlot.1 = some (.cnode rS cs) ∧ cs srcSlot.2 = some capS) : + CapRelA (capA st s' (cslotAddrA srcSlot)) + (.untypedCap dev (specPtr p) (bitsOf capS) (unat (specPtr aligned + BitVec.ofNat 64 n <<< z - specPtr p))) := by + obtain ⟨q, eq, hq⟩ := hsrc + obtain ⟨cs, hk, -, hs⟩ := hS.cnode q _ hq + obtain ⟨rS, cs2, hk2, hc2⟩ := hcap + rw [eq, hk] at hk2 + injection hk2 with h + injection h with _ h2 + subst h2 + obtain ⟨ac, hac, hrel⟩ := hs srcSlot.2 rfl + rw [hc2] at hac + obtain rfl := Option.some.inj hac + have ea : slotAddrA (specPtr q) (ofBl srcSlot.2) = cslotAddrA srcSlot := by unfold cslotAddrA; rw [eq] + rw [ea, capAtA_eq, hc1] at hrel + obtain ⟨hwf, he⟩ := hrel + have he' : absCapA (.Untyped p sb fi1 dev) = capS := he + subst he' + rw [hc', utFreeRef_valA hz hp hn hv] + exact ⟨hwf, rfl⟩ + +end src + +/-- **`hcdtfree`** (module docstring). -/ +theorem utCdt_freeA {S : Type} {st : machine.Store S} [L : LawfulStore st] {th : TypedHeapA} {s : S} + {σ : State Unit} {G : MdbGhostA} (C : CdtRelA st th s σ G) {a : CslotPtr} (h : ¬ AbsSlotA th a) : + σ.cdt a = none := by + cases e : σ.cdt a with + | none => rfl + | some q => exact absurd (C.cdt_slot a q e).1 h + +/-- **`hsrcS`**: a slot the lookup returns is a slot of a CNode of `th`. -/ +theorem utWalk_srcA {th : TypedHeapA} {mem : U64 → U64} {c0 : capability.Cap} {sl : CslotPtr} {rest : CapRef} + (h : SlotOkA th mem c0 (.ok (sl, rest))) : ∃ q, sl.1 = specPtr q ∧ th q = some (.cnode sl.2.length) := by + obtain ⟨q, e, hq, -⟩ := h + exact ⟨q, e, hq⟩ + +theorem utWindow_lengthA (dest : U64) (r off win : Nat) : (windowSlotsA dest r off win).length = win := by + simp [windowSlotsA] + +section ctx +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {th : TypedHeapA} {s0 : S} {σ0 : State Unit} +variable {sl : CslotPtr} {p sb fi : U64} {dev : Bool} {nt us dest off win fiv : U64} {t : ApiObjectType} {r : Nat} +variable {reset : Bool} + +/-- **`hdst`** (module docstring). -/ +theorem utCtx_dstA (X : UtCtxA st th s0 σ0 sl p sb fi dev nt us dest off win t r reset fiv) : + ∀ j (h : j < (windowSlotsA dest r off.val win.val).length), + cslotAddrA ((windowSlotsA dest r off.val win.val)[j]'h) = djA dest off j ∧ + ∃ q, ((windowSlotsA dest r off.val win.val)[j]'h).1 = specPtr q ∧ + th q = some (.cnode ((windowSlotsA dest r off.val win.val)[j]'h).2.length) := by + intro j h + have hj : j < win.val := by rwa [utWindow_lengthA] at h + have hr : r < 64 := X.cnode.2.1 + have hw := X.window.2.2 + have e : (windowSlotsA dest r off.val win.val)[j]'h = (specPtr dest, natToCref r (off.val + j)) := by + simp [windowSlotsA] + rw [e] + refine ⟨?_, dest, rfl, ?_⟩ + · rw [cslotAddr_windowA dest r (by omega) _ (by omega), slotAddrA_sp] + unfold djA slotIdx + apply (UScalar.eq_equiv_bv_eq _ _).2 + simp only [core.num.U64.wrapping_add_bv_eq, core.num.U64.wrapping_mul_bv_eq] + have e2 : off.bv = BitVec.ofNat 64 off.val := + BitVec.eq_of_toNat_eq (by rw [BitVec.toNat_ofNat]; exact (Nat.mod_eq_of_lt (by simpa using off.bv.isLt)).symm) + show _ = dest.bv + (off.bv + BitVec.ofNat 64 j) * 32#64 + rw [e2, ← BitVec.ofNat_add] + · show th dest = some (.cnode (natToCref r (off.val + j)).length) + rw [natToCref_length_utA r _ (by omega)] + exact X.cnode.1 + +/-- The destinations are not the untyped's slot: they are empty, it is not. -/ +theorem utCtx_snA (X : UtCtxA st th s0 σ0 sl p sb fi dev nt us dest off win t r reset fiv) + (hcap : capA st s0 (cslotAddrA sl) = .Untyped p sb fi dev) : + ∀ j, j < win.val → djA dest off j ≠ cslotAddrA sl := by + intro j hj e + have h : j < (windowSlotsA dest r off.val win.val).length := by rwa [utWindow_lengthA] + have hr : r < 64 := X.cnode.2.1 + have hw := X.window.2.2 + have es : (windowSlotsA dest r off.val win.val)[j]'h = (specPtr dest, natToCref r (off.val + j)) := by + simp [windowSlotsA] + have ea := (utCtx_dstA X j h).1 + rw [es, cslotAddr_windowA dest r (by omega) _ (by omega), e] at ea + have := X.empty (off.val + j) (by omega) (by omega) + rw [ea, hcap] at this + cases this + +end ctx + +section site +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **`StateRelA` and `CdtRelA` after all cap writes, at the decode site** (module docstring). -/ +theorem utSite_relsA {H H1 H' : Heap} {Cs OS OS1 : Set U64} {s0 s1 s' sd : S} {p sb fi fi1 nfi : U64} {dev : Bool} + {lo hi : Nat} {aligned nt us dest off win fiv : U64} {t : ApiObjectType} {rd : Nat} {reset : Bool} + {th : TypedHeapA} {σ0 σ : State Unit} {srcSlot : CslotPtr} + (U : UtEnd st H Cs OS s0 (cslotAddrA srcSlot) p sb dev H1 H' OS1 s1 s' lo hi aligned nt us dest off win) + (X : UtCtxA st th sd σ0 srcSlot p sb fi dev nt us dest off win t rd reset fiv) + (hcapd : capA st sd (cslotAddrA srcSlot) = .Untyped p sb fi dev) + (hsrc : ∃ q, srcSlot.1 = specPtr q ∧ th q = some (.cnode srcSlot.2.length)) + (hc1 : capA st s1 (cslotAddrA srcSlot) = .Untyped p sb fi1 dev) + (hnv : nfi.val = aligned.val + win.val * 2 ^ (zA nt.val us).val - p.val) + (hnc : capA st s' (cslotAddrA srcSlot) = .Untyped p sb nfi dev) + {ksb : U64} (P' : PlaceA (utRtThA th dev nt us aligned win.val) ksb) (P : PlaceA th ksb) + (hsub : ∀ y, IsSlotA th y → y ∈ OS1) (hnul : ∀ y ∈ OS1, ¬ IsSlotA th y → capA st s1 y = .Null) + (hS : StateRelA th (L.mem s1) σ) {G : MdbGhostA} (C : CdtRelA st th s1 σ G) + {r : Unit × State Unit} + (hr : r ∈ (utRtBodyA srcSlot (specPtr p) (specPtr aligned) t us.val + (windowSlotsA dest rd off.val win.val) dev σ).1) + (hfree : ∀ j, j < win.val → th (bjA aligned (zA nt.val us) j) = none) + (hal : aligned.val % 8 = 0) (hc47 : nt.val = 4 → us.val ≤ 47) + (hnewRel : ∀ j, j < win.val → CapRelA (newCapA nt.val (bjA aligned (zA nt.val us) j) us dev) + (defaultCap t (specPtr (bjA aligned (zA nt.val us) j)) us.val dev)) + (hrev : revA (L.mem s1) (cslotAddrA srcSlot) = true) + (htree : TreeA st OS1 (utGA (cslotAddrA srcSlot) (djA dest off) G.chains win.val) + (utPA (cslotAddrA srcSlot) (djA dest off) G.par win.val) s') + (hr1 : ∀ y ∈ OS1, (∀ j, j < win.val → y ≠ djA dest off j) → revA (L.mem s') y = revA (L.mem s1) y) + (hr2 : ∀ j, j < win.val → revA (L.mem s') (djA dest off j) = true) + (horigfree : ∀ a, ¬ AbsSlotA th a → AbsSlotA (utRtThA th dev nt us aligned win.val) a → + σ.isOriginalCap a = false) : + StateRelA (utRtThA th dev nt us aligned win.val) (L.mem s') r.2 ∧ + CdtRelA st (utRtThA th dev nt us aligned win.val) s' r.2 + ⟨utGA (cslotAddrA srcSlot) (djA dest off) G.chains win.val, + utPA (cslotAddrA srcSlot) (djA dest off) G.par win.val⟩ := by + have htt := X.ty + have hus : us.val < 64 := X.size.1 + have hn : nt.val < 15 := (objTypeOfA_some htt us.val).1 + have hlen := utWindow_lengthA dest rd off.val win.val + have hdst := utCtx_dstA X + have hsn := utCtx_snA X hcapd + have hz : (zA nt.val us).val < 64 := by + rw [zA_valA nt us hn hus] + have := X.size.2.1 + omega + have hwin : win.val < 2 ^ 64 := by simpa using win.bv.isLt + have hsrcRel : ∀ capS, σ.kheap srcSlot.1 ≠ none → (∃ rS cs, σ.kheap srcSlot.1 = some (.cnode rS cs) ∧ + cs srcSlot.2 = some capS) → + CapRelA (capA st s' (cslotAddrA srcSlot)) (.untypedCap dev (specPtr p) (bitsOf capS) + (unat (specPtr aligned + BitVec.ofNat 64 (windowSlotsA dest rd off.val win.val).length <<< + fl4ObjBitsApi t us.val - specPtr p))) := fun capS _ h => by + rw [hlen, utRet_bitsA htt hus] + exact utSrc_relA hS hsrc hc1 hnc hz U.A.pa hwin hnv h + have res := utEnd_stateRelA U P hsub hS htt hus hlen hr ⟨rfl, hsrc⟩ hdst hsn hfree hal hc47 hsrcRel hnewRel + refine ⟨res, ?_⟩ + -- the spec's CDT and original-cap updates + obtain ⟨q, eq, hq⟩ := hsrc + obtain ⟨csS, hkS, -, hsS⟩ := hS.cnode q _ hq + rw [← eq] at hkS + obtain ⟨capS, hcapS, -⟩ := hsS srcSlot.2 rfl + have newOff : ∀ q k, th q = some k → + specPtr q ∉ fl4RetypeAddrs fl4ObjBitsApi (specPtr aligned) + (windowSlotsA dest rd off.val win.val).length us.val t := by + intro q' k hq' m + rw [hlen, utRet_addrs_memA htt hus] at m + obtain ⟨j, hj, e⟩ := m + rw [ut_specPtr_injA e, hfree j hj] at hq' + cases hq' + have hdc : ∀ d ∈ windowSlotsA dest rd off.val win.val, (∃ sz cn, σ.kheap d.1 = some (.cnode sz cn)) ∧ + d.1 ∉ fl4RetypeAddrs fl4ObjBitsApi (specPtr aligned) + (windowSlotsA dest rd off.val win.val).length us.val t := by + intro d hd + obtain ⟨j, hj, rfl⟩ := List.getElem_of_mem hd + obtain ⟨-, q', e', hq'⟩ := hdst j hj + obtain ⟨cs, hk, -⟩ := hS.cnode q' _ hq' + rw [e'] + exact ⟨⟨_, cs, hk⟩, newOff q' _ hq'⟩ + obtain ⟨-, -, -, -, hcdt, horig, -⟩ := utRtBody_invA hr hkS hcapS (eq ▸ newOff q _ hq) hdc + have hfree' : ∀ q, UtNewA dev nt us aligned win.val q → th q = none := by + rintro q ⟨-, -, -, j, hj, rfl⟩ + exact hfree j hj + exact utEnd_cdtRelA U P' P hfree' hn hus hc47 hal hsub hsn hnul C hrev htree hr1 hr2 hlen rfl + ⟨q, eq, Or.inl hq⟩ + (fun j h => ⟨(hdst j h).1, by + obtain ⟨-, q', e', hq'⟩ := hdst j h + exact ⟨q', e', Or.inl hq'⟩⟩) + hcdt horig (fun a h _ => utCdt_freeA C h) horigfree + +end site + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtRtSpecA.lean b/proofs/Fl4ProofsA64/P3/UtRtSpecA.lean new file mode 100644 index 000000000..60919d39e --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtRtSpecA.lean @@ -0,0 +1,172 @@ +/- +P3 (aarch64, A64-P3 Untyped W3, spec side): **the outcome of FL4's `retype_region`** (`retypeRegionWith +fl4ObjBitsApi`), and the `σ'` side of `scSize` and `DevFrDisjA` from it (lead 01:24Z). + +* `utRetStA`: the state with the default object at each new address; `utRetRegion_memA`: it is an outcome; + `utRetSt_kheapA`: its heap. +* `utRet_ptrA`: the spec's addresses are `specPtr` of FL4's (`bjA`); `utRet_bitsA`: the sizes agree. +* `utRetSpec_scA`: `utEnd_scSizeA`'s spec split (`hold`, `hnew`). +* `utRetSpec_dataPageA`: `utEnd_devFrDisjA`'s spec premise `hσ`. + +This is `retype_region`'s outcome only. The rest of `invoke_untyped` (the untyped's `set_cap`, `init_arch_objects`, +`create_cap`) and FL4's `vcpu::init` step are not composed here, so that they keep SC objects and data pages is not +yet proved. + +No named premise: that no new address is the idle-SC anchor or the current core's idle SC is proved +(`utRet_anchorA`, from `IdleAnchorOffA`; `utRet_idleScA`, the current idle SC being a kept heap SC). +-/ +import Fl4ProofsA64.P3.UtRtEndA +import Fl4ProofsA64.P3.UtDecodeA +import Fl4ProofsA64.P3.JointA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Sched Classical + +noncomputable section + +open Fl4SpecA64 Fl4Spec + +/-- The state after `retype_region` over `addrs`. -/ +def utRetStA (t : ApiObjectType) (dev : Bool) (bits : Nat) (addrs : List ObjRef) (σ : State Unit) : State Unit := + { σ with kheap := (addrs.foldr (fun p kh => Function.update kh p (some (defaultObject t dev bits σ.curDomain))) + σ.kheap : ObjRef → Option KernelObject) } + +private theorem ut_foldr_updA {α β : Type} [DecidableEq α] (v : β) (kh : α → Option β) (l : List α) (q : α) : + (l.foldr (fun p kh => Function.update kh p (some v)) kh) q = if q ∈ l then some v else kh q := by + induction l with + | nil => simp + | cons a l ih => + simp only [List.foldr_cons, List.mem_cons] + by_cases h : q = a + · subst h; rw [Function.update_self, ite_eq_left (Or.inl rfl)] + · rw [Function.update_of_ne h, ih] + simp [h] + +theorem utRetSt_kheapA (t : ApiObjectType) (dev : Bool) (bits : Nat) (addrs : List ObjRef) (σ : State Unit) + (q : ObjRef) : (utRetStA t dev bits addrs σ).kheap q = + if q ∈ addrs then some (defaultObject t dev bits σ.curDomain) else σ.kheap q := + ut_foldr_updA _ _ _ _ + +/-- `retype_region` of a non-untyped type has the outcome `utRetStA`. -/ +theorem utRetRegion_memA {t : ApiObjectType} (ht : t ≠ .untyped) (ptr : ObjRef) (n bits : Nat) (dev : Bool) + (σ : State Unit) : + (fl4RetypeAddrs fl4ObjBitsApi ptr n bits t, + utRetStA t dev bits (fl4RetypeAddrs fl4ObjBitsApi ptr n bits t) σ) ∈ + (retypeRegionWith (Z := Unit) fl4ObjBitsApi ptr n bits t dev σ).1 := by + unfold retypeRegionWith + refine ⟨(2 ^ fl4ObjBitsApi t bits, σ), Set.mem_singleton _, ?_⟩ + refine ⟨(fl4RetypeAddrs fl4ObjBitsApi ptr n bits t, σ), Set.mem_singleton _, ?_⟩ + refine ⟨((), utRetStA t dev bits (fl4RetypeAddrs fl4ObjBitsApi ptr n bits t) σ), ?_, Set.mem_singleton _⟩ + unfold NonDet.when + simp only [ne_eq, ht, not_false_eq_true, ↓reduceIte] + refine ⟨(σ.kheap, σ), Set.mem_singleton _, ?_⟩ + refine ⟨(σ.curDomain, σ), Set.mem_singleton _, ?_⟩ + exact ⟨((fl4RetypeAddrs fl4ObjBitsApi ptr n bits t).foldr + (fun p kh => Function.update kh p (some (defaultObject t dev bits σ.curDomain))) σ.kheap, σ), + Set.mem_singleton _, Set.mem_singleton _⟩ + +/-- The spec's object size is FL4's. -/ +theorem utRet_bitsA {nt us : U64} {t : ApiObjectType} (h : objTypeOfA nt.val = some t) (hus : us.val < 64) : + fl4ObjBitsApi t us.val = (zA nt.val us).val := by + obtain ⟨hn, hz, -⟩ := objTypeOfA_some h us.val + rw [hz, zA_valA nt us hn hus] + +/-- The spec's `j`-th address is FL4's. -/ +theorem utRet_ptrA (base z : U64) (j : Nat) : ptrAdd (specPtr base) (j * 2 ^ z.val) = specPtr (bjA base z j) := by + have e : (bjA base z j).bv = base.bv + BitVec.ofNat 64 (j * 2 ^ z.val) := by + show BitVec.ofNat 64 (base.val + j * 2 ^ z.val) = _ + rw [BitVec.ofNat_add] + congr 1 + exact BitVec.eq_of_toNat_eq (by rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by simpa using base.hBounds)) + unfold specPtr ptrAdd + rw [e] + ac_rfl + +theorem utRet_addrs_memA {nt us base : U64} {t : ApiObjectType} (h : objTypeOfA nt.val = some t) (hus : us.val < 64) + {len : Nat} {q : ObjRef} : + q ∈ fl4RetypeAddrs fl4ObjBitsApi (specPtr base) len us.val t ↔ + ∃ j < len, q = specPtr (bjA base (zA nt.val us) j) := by + unfold fl4RetypeAddrs + rw [utRet_bitsA h hus, List.mem_map] + constructor + · rintro ⟨j, hj, rfl⟩ + exact ⟨j, List.mem_range.1 hj, utRet_ptrA _ _ _⟩ + · rintro ⟨j, hj, rfl⟩ + exact ⟨j, List.mem_range.2 hj, utRet_ptrA _ _ _⟩ + +theorem ut_specPtr_injA {a b : U64} (h : specPtr a = specPtr b) : a = b := by + have := congrArg fl4Ptr h + rwa [fl4Ptr_specPtr, fl4Ptr_specPtr] at this + +/-- No new base is the idle-SC anchor: `IdleAnchorOffA` keeps its page off every untyped. -/ +theorem utRet_anchorA {S : Type} {st : machine.Store S} [LawfulStore st] {OS1 : Set U64} {s1 : S} + (ha : IdleAnchorOffA (st := st) OS1 s1) {slot p sb fi1 b : U64} {dev : Bool} (hs : slot ∈ OS1) + (hc : capA st s1 slot = .Untyped p sb fi1 dev) (h1 : p.val ≤ b.val) (h2 : b.val < p.val + 2 ^ sb.val) : + specPtr b ≠ idleScPtr := fun e => by + have eb : fl4Ptr idleScPtr = b := by rw [← e, fl4Ptr_specPtr] + have := ha slot hs p sb fi1 dev hc + rw [eb] at this + rcases this with g | g <;> omega + +/-- No new base is the current core's idle SC: that SC is a kept heap SC, and no kept object is at a new base. -/ +theorem utRet_idleScA {H1 : Heap} {m : McState} {c0 b : U64} (hid : m.idleScOf m.curCore = specPtr c0) + (hc0 : c0 ∈ H1.scs) (hfree : H1 b = none) : specPtr b ≠ m.idleScOf m.curCore := fun e => by + rw [hid] at e + obtain ⟨z, hz⟩ := hc0 + rw [← ut_specPtr_injA e, hfree] at hz + cases hz + +/-- `utRet_idleScA`'s two facts from the record's scheduler relation: the current core's `IDLE_SC` word. -/ +theorem utRet_idleScOfA {S : Type} {st : machine.Store S} [L : LawfulStore st] {T C Cs : Set U64} + {curOf curScOf : CoreId → ObjRef} {s : S} {σ : State Unit} {m : McState} + (R : SchedRelA (L := L) T C Cs curOf curScOf s σ m) : + m.idleScOf m.curCore = specPtr (L.mem s (L.nodeAt s (L.cpu s) layout.node.IDLE_SC)) := by + rw [R.core]; exact R.idleScOf _ R.cpuCs + +/-- **`scSize`'s spec split** (`utEnd_scSizeA`'s `hold` and `hnew`). `hfree`: no kept heap object at a new base; +`hanchor` / `hidle`: `utRet_anchorA` / `utRet_idleScA`. -/ +theorem utRetSpec_scA {nt us aligned : U64} {t : ApiObjectType} (h : objTypeOfA nt.val = some t) + (hus : us.val < 64) {len : Nat} {H1 : Heap} {m : McState} (σ : State Unit) (dev : Bool) + (hfree : ∀ j, j < len → H1 (bjA aligned (zA nt.val us) j) = none) + (hanchor : ∀ j, j < len → specPtr (bjA aligned (zA nt.val us) j) ≠ idleScPtr) + (hidle : ∀ j, j < len → specPtr (bjA aligned (zA nt.val us) j) ≠ m.idleScOf m.curCore) : + let σ' := utRetStA t dev us.val (fl4RetypeAddrs fl4ObjBitsApi (specPtr aligned) len us.val t) σ + (∀ c ∈ H1.scs, σ'.kheap (scNameA m c) = σ.kheap (scNameA m c)) ∧ + (nt.val = 5 → ∀ j, j < len → σ'.kheap (scNameA m (bjA aligned (zA nt.val us) j)) = + some (.schedContext defaultSchedContext (us.val - minSchedContextBits))) := by + intro σ' + refine ⟨fun c hc => ?_, fun h5 j hj => ?_⟩ + · rw [utRetSt_kheapA, ite_eq_right] + rw [utRet_addrs_memA h hus] + rintro ⟨j, hj, e⟩ + unfold scNameA at e + split_ifs at e with hi + · exact hanchor j hj e.symm + · have ec := ut_specPtr_injA e + obtain ⟨z, hz⟩ := hc + rw [ec, hfree j hj] at hz + cases hz + · have en : scNameA m (bjA aligned (zA nt.val us) j) = specPtr (bjA aligned (zA nt.val us) j) := + ite_eq_right (hidle j hj) + have et : t = .schedContextObject := (objTypeOfA_some h us.val).2.2.2.2.2.2 h5 + rw [en, utRetSt_kheapA, ite_eq_left ((utRet_addrs_memA h hus).2 ⟨j, hj, rfl⟩), et] + rfl + +/-- **`DevFrDisjA`'s spec premise** (`utEnd_devFrDisjA`'s `hσ`): objects off the untyped's free part are kept. -/ +theorem utRetSpec_dataPageA {nt us aligned p sb fi1 : U64} {t : ApiObjectType} (h : objTypeOfA nt.val = some t) + (hus : us.val < 64) {len : Nat} (σ : State Unit) (dev : Bool) + (hin : ∀ j, j < len → p.val + fi1.val ≤ (bjA aligned (zA nt.val us) j).val ∧ + (bjA aligned (zA nt.val us) j).val < p.val + 2 ^ sb.val) : + ∀ q : U64, ¬ (p.val + fi1.val ≤ q.val ∧ q.val < p.val + 2 ^ sb.val) → + (utRetStA t dev us.val (fl4RetypeAddrs fl4ObjBitsApi (specPtr aligned) len us.val t) σ).kheap (specPtr q) = + σ.kheap (specPtr q) := by + intro q hq + rw [utRetSt_kheapA, ite_eq_right] + rw [utRet_addrs_memA h hus] + rintro ⟨j, hj, e⟩ + exact hq (ut_specPtr_injA e ▸ hin j hj) + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtRtTcbPostA.lean b/proofs/Fl4ProofsA64/P3/UtRtTcbPostA.lean new file mode 100644 index 000000000..bb788ee6c --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtRtTcbPostA.lean @@ -0,0 +1,280 @@ +/- +P3 (aarch64, A64-P3 Untyped W5): **`JAtZAX` after a RAM creation of TCBs**. + +* **`utRt_tcbPostA`**: from the record at the creation's start, FL4's creation (`CrtE`, `DbgX`, `WinT`, the domain + and VCPU words `UtTcbZW`, the sharp write frame), `StateRelA` / `CdtRelA` after it, and any outcome of the spec + body, `JAtZAX` at the grown sets: the new TCBs join `T`, with affinity `some 0` in the view. + +Hypotheses: `hsub` (closure item A), the creation's window (`W`, `h1`, `h2`), `CrtE`, `X`, `WT` +(`untyped.retype.liveRWH`), `TW` (`utRetype_tcbWordsA`), `w`, `hS` / `hC` (`utSite_relsA`), `P'`, `har`, the invoked +and destination slots' CNodes. No named premise. New theorems only. +-/ +import Fl4ProofsA64.P3.UtRtPostA +import Fl4ProofsA64.P3.UtRtGlobA +import Fl4ProofsA64.P3.UtRtCapsA +import Fl4ProofsA64.P3.UtRtZeroPostA +import Fl4ProofsA64.P3.UtRtTcbWordsA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +set_option maxRecDepth 100000 + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s s' : S} {a a' : A} {σ : State Unit} {m : McState} {x p sb fi : U64} + {nt us aligned dest off win : U64} + +/-- **`JAtZAX` after a RAM creation of TCBs** (module docstring). -/ +theorem utRt_tcbPostA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (hx : x ∈ OS) (hc : capA st s x = .Untyped p sb fi false) + (W : UtWinZA nt aligned (zA nt.val us) win.val) + (h1 : p.val + fi.val ≤ aligned.val) + (h2 : aligned.val + win.val * 2 ^ (zA nt.val us).val ≤ p.val + 2 ^ sb.val) + (n1 : nt.val = 1) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (Ec : CrtE st H H.frame OS s s' x p sb false aligned nt us dest off win) + (w : WritesIn st s s' (UtCrtWA st H OS s nt aligned (zA nt.val us) win.val)) + (X : DbgX st H s nt aligned (zA nt.val us) s' win.val) (WT : WinT st s' aligned (zA nt.val us) win.val) + (TW : UtTcbZW st aligned (zA nt.val us) win.val s') + {t : ApiObjectType} (htt : objTypeOfA nt.val = some t) (hus : us.val < 64) + {srcSlot : CslotPtr} {base : Word} {slots : List CslotPtr} (hlen : slots.length = win.val) + (hsrcS : ∃ q, srcSlot.1 = specPtr q ∧ Z.th q = some (.cnode srcSlot.2.length)) + (hdst : ∀ d ∈ slots, ∃ q, d.1 = specPtr q ∧ Z.th q = some (.cnode d.2.length)) + {r : Unit × State Unit} + (hr : r ∈ (utRtBodyA srcSlot base (specPtr aligned) t us.val slots false σ).1) + {G' : MdbGhostA} (hS : StateRelA (utRtThA Z.th false nt us aligned win.val) (L.mem s') r.2) + (hC : CdtRelA st (utRtThA Z.th false nt us aligned win.val) s' r.2 G') + (P' : PlaceA (utRtThA Z.th false nt us aligned win.val) (L.ksb s)) (har : AR a' Z.hw) : + JAtZAX (L := L) AR + (utRtZA Z G' nt us aligned win.val (utBasesA aligned (zA nt.val us) win.val) ∅ ∅) s' a' r.2 + (utRtMA m (utBasesA aligned (zA nt.val us) win.val)) := by + have hn : nt.val < 15 := (objTypeOfA_some htt us.val).1 + have Cl : UtClrA st Z H s aligned.val (aligned.val + win.val * 2 ^ (zA nt.val us).val) := + utRt_winClrA J hx hc h1 h2 + have anchor : ∀ j, j < win.val → specPtr (bjA aligned (zA nt.val us) j) ≠ Fl4SpecA64.idleScPtr := by + intro j hj + have v := utRt_newValA W hj + exact utRet_anchorA (st := st) J.idleAnchor hx hc (by omega) (by omega) + have memA : ∀ q, q ∈ fl4RetypeAddrs fl4ObjBitsApi (specPtr aligned) slots.length us.val t ↔ + UtNewSpA aligned (zA nt.val us) win.val q := fun q => by + rw [hlen, utRet_addrs_memA htt hus] + rfl + have thOff : ∀ q k, Z.th q = some k → ¬ UtNewSpA aligned (zA nt.val us) win.val (specPtr q) := fun q k hq => + utRt_relOffA (m := m) J Cl W rfl rfl anchor (Or.inl ⟨q, by rw [hq]; simp, rfl⟩) + -- the spec outcome + obtain ⟨qS, eqS, hqS⟩ := hsrcS + obtain ⟨csS, hkS, -, hsS⟩ := J.j.1.st.cnode qS _ hqS + rw [← eqS] at hkS + obtain ⟨capS, hcapS, -⟩ := hsS srcSlot.2 rfl + have hdc : ∀ d ∈ slots, (∃ sz cn, σ.kheap d.1 = some (.cnode sz cn)) ∧ + d.1 ∉ fl4RetypeAddrs fl4ObjBitsApi (specPtr aligned) slots.length us.val t := by + intro d hd + obtain ⟨q, eq, hq⟩ := hdst d hd + obtain ⟨cs, hk, -⟩ := J.j.1.st.cnode q _ hq + rw [eq, memA] + exact ⟨⟨_, cs, hk⟩, thOff q _ hq⟩ + have hSa : srcSlot.1 ∉ fl4RetypeAddrs fl4ObjBitsApi (specPtr aligned) slots.length us.val t := by + rw [eqS, memA] + exact thOff qS _ hqS + obtain ⟨-, hCn, hNew, hK, -, -, -⟩ := utRtBody_invA hr hkS hcapS hSa hdc + -- an old object that is no CNode is kept + have keep : ∀ q, t = .untyped ∨ ¬ UtNewSpA aligned (zA nt.val us) win.val q → + ∀ ko, (∀ sz cn, ko ≠ .cnode sz cn) → ∀ u : Cap, + let σa := setObjA σ srcSlot.1 (.cnode srcSlot.2.length (Function.update csS srcSlot.2 (some u))) + σa.kheap q = some ko → σ.kheap q = some ko := by + intro q _ ko ne u σa h + by_cases e : q = srcSlot.1 + · have : σa.kheap q = some (.cnode srcSlot.2.length (Function.update csS srcSlot.2 (some u))) := by + rw [e]; exact Function.update_self .. + rw [this] at h + exact absurd (Option.some.inj h).symm (ne _ _) + · exact (Function.update_of_ne e ..).symm.trans h + have hkeep : ∀ q, ¬ UtNewSpA aligned (zA nt.val us) win.val q → (∀ sz cn, σ.kheap q ≠ some (.cnode sz cn)) → + r.2.kheap q = σ.kheap q := by + intro q hq nc + have e : q ≠ srcSlot.1 := fun e => nc _ _ (e ▸ hkS) + have ea : ∀ u : Cap, (setObjA σ srcSlot.1 (.cnode srcSlot.2.length (Function.update csS srcSlot.2 (some u)))).kheap q = + σ.kheap q := fun u => Function.update_of_ne e .. + rw [hK q (Or.inr (fun h => hq ((memA q).1 h))) (fun sz cn => by rw [ea]; exact nc sz cn), ea] + have newObj : t ≠ .untyped → ∀ j, j < win.val → + r.2.kheap (specPtr (bjA aligned (zA nt.val us) j)) = some (defaultObject t false us.val σ.curDomain) := + fun ht j hj => hNew ht _ ((memA _).2 ⟨j, hj, rfl⟩) + have back : ∀ q ko, (∀ sz cn, ko ≠ .cnode sz cn) → r.2.kheap q = some ko → σ.kheap q = some ko ∨ + (t ≠ .untyped ∧ UtNewSpA aligned (zA nt.val us) win.val q ∧ ko = defaultObject t false us.val σ.curDomain) := by + intro q ko ne h + by_cases g : t = .untyped ∨ ¬ UtNewSpA aligned (zA nt.val us) win.val q + · have g' : t = .untyped ∨ q ∉ fl4RetypeAddrs fl4ObjBitsApi (specPtr aligned) slots.length us.val t := + g.imp id (fun k m' => k ((memA q).1 m')) + obtain ⟨u0, eu⟩ : ∃ u0 : Cap, u0 = .untypedCap false base (bitsOf capS) (unat (specPtr aligned + + BitVec.ofNat 64 slots.length <<< fl4ObjBitsApi t us.val - base)) := ⟨_, rfl⟩ + refine Or.inl (keep q g ko ne u0 ?_) + rw [eu] + by_cases cn : ∃ sz c, (setObjA σ srcSlot.1 (.cnode srcSlot.2.length (Function.update csS srcSlot.2 (some + (.untypedCap false base (bitsOf capS) (unat (specPtr aligned + + BitVec.ofNat 64 slots.length <<< fl4ObjBitsApi t us.val - base))))))).kheap q = some (.cnode sz c) + · obtain ⟨sz, c, k⟩ := cn + rw [(hCn q sz c g' k).1] at h + exact absurd (Option.some.inj h).symm (ne _ _) + · rw [hK q g' (fun sz c k => cn ⟨sz, c, k⟩)] at h + exact h + · have gt : t ≠ .untyped := fun e => g (Or.inl e) + have gq : UtNewSpA aligned (zA nt.val us) win.val q := by + by_contra k + exact g (Or.inr k) + refine Or.inr ⟨gt, gq, ?_⟩ + rw [hNew gt q ((memA q).2 gq)] at h + exact (Option.some.inj h).symm + -- the new TCBs' words + have hz12 : (zA nt.val us).val = 12 := by rw [n1]; exact zA_1 us + have hX : 2 ^ (zA nt.val us).val = 4096 := by rw [hz12]; norm_num + have tb : ∀ j, j < win.val → (bjA aligned (zA nt.val us) j).val = aligned.val + j * 4096 ∧ + aligned.val + j * 4096 + 4096 ≤ aligned.val + win.val * 4096 := by + intro j hj + have h1' : (j + 1) * 4096 ≤ win.val * 4096 := Nat.mul_le_mul_right _ hj + have fit := W.fit + rw [hX] at fit + have e := bjA_val (base := aligned) (z := zA nt.val us) (j := j) (by rw [hX]; omega) + rw [hX] at e + exact ⟨e, by omega⟩ + have al4 : aligned.val % 4096 = 0 := by + have := W.al + rwa [hX] at this + have wo : ∀ j, j < win.val → ∀ (K : U64) (k : Nat), K.val = k → k < 4096 → + (wa (bjA aligned (zA nt.val us) j) K).val = aligned.val + j * 4096 + k := by + intro j hj K k ek hk + have v := tb j hj + have fit := W.fit + rw [hX] at fit + rw [wa_valP, ek, v.1] + exact Nat.mod_eq_of_lt (by omega) + have wt : ∀ j, j < win.val → ∀ (b : U64) (k : Nat), b.val = aligned.val + j * 4096 + k → k < 4096 → k % 8 = 0 → + ¬ TcbInitK k → L.mem s' b = 0#u64 := by + intro j hj b k eb hk h8 ni + have v := tb j hj + have al : Aligned b := by show _ % 8 = 0; omega + rcases WT b al (by omega) (by rw [hX]; omega) with h | ⟨i, -, K, hK, e⟩ + · exact h + · rw [hX] at e + have hK4 : K < 4096 := hK.2 + have eij : i = j := by omega + subst eij + have : K = k := by omega + exact absurd (this ▸ hK) ni + have hcap : ∀ y, IsSlotA (utRtThA Z.th false nt us aligned win.val) y → + UtCapAfterA Z.th (L.mem s) (L.mem s') nt us aligned win.val y := by + intro y hy + by_cases old : IsSlotA Z.th y + · by_cases ex : y = x + · obtain ⟨nfi, e⟩ := Ec.src + exact Or.inr (Or.inr (Or.inl ⟨p, sb, nfi, false, by rw [ex]; exact e⟩)) + · by_cases ed : ∃ j < win.val, y = djA dest off j + · obtain ⟨j, hj, e⟩ := ed + exact Or.inr (Or.inl ⟨j, hj, by rw [e]; exact Ec.new j hj⟩) + · exact Or.inl ⟨old, Ec.keep y (hsub y old) ex (fun j hj e => ed ⟨j, hj, e⟩)⟩ + · refine Or.inr (Or.inr (Or.inr ?_)) + obtain ⟨q, h⟩ := hy + have isNew : ∀ k, utRtThA Z.th false nt us aligned win.val q = some k → + (∀ k', Z.th q = some k' → k' ≠ k) → UtNewA false nt us aligned win.val q ∧ k = utKindA nt us := by + intro k hk ne + unfold utRtThA at hk + split_ifs at hk with g + · exact ⟨g, (Option.some.inj hk).symm⟩ + · exact absurd rfl (ne k hk) + have kt : utKindA nt us = .tcb := by unfold utKindA; rw [ite_eq_left n1] + rcases h with ⟨rd, hq, i, hi, e⟩ | ⟨hq, i, hi, e⟩ + · have g := isNew _ hq (fun k' hk' ek => old ⟨q, Or.inl ⟨rd, by rw [hk', ek], i, hi, e⟩⟩) + have ek := g.2 + rw [kt] at ek + cases ek + · have g := isNew _ hq (fun k' hk' ek => old ⟨q, Or.inr ⟨by rw [hk', ek], i, hi, e⟩⟩) + obtain ⟨⟨-, -, -, j, hj, ej⟩, -⟩ := g + have hi7 : i < 7 := hi + have v := tb j hj + have fit := W.fit + rw [hX] at fit + have yv : y.val = q.val + 32 * i := by + rw [e] + refine slotAddrA_valN ?_ + rw [ej, v.1] + omega + have dz := (X.2.2 j hj n1).2.2.2.2.2 + have word : ∀ (b : U64) (k : Nat), b.val = q.val + k → k < 224 → k % 8 = 0 → L.mem s' b = 0#u64 := by + intro b k eb hk h8 + obtain ⟨K, eK⟩ : ∃ K : U64, K.val = k := ⟨⟨BitVec.ofNat 64 k⟩, by + show (BitVec.ofNat 64 k).toNat = k + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega)⟩ + have eq : b = wa (bjA aligned (zA nt.val us) j) K := by + apply UScalar.eq_of_val_eq + rw [wo j hj K k eK (by omega), eb, ej, v.1] + rw [eq] + exact dz K (by omega) (by omega) + have y8 : (wa y 8#u64).val = y.val + 8 := by + rw [wa_valP, show (8#u64 : U64).val = 8 from rfl] + refine Nat.mod_eq_of_lt ?_ + rw [yv, ej, v.1] + omega + exact ut_capAt_zeroA _ _ (word y (32 * i) yv (by omega) (by omega)) + (word _ (32 * i + 8) (by rw [y8, yv]; omega) (by omega) (by omega)) + have z12 : nt.val ≠ 12 := by omega + have z14 : nt.val ≠ 14 := by omega + have et : t = .tcbObject := by + rw [n1] at htt + simpa [objTypeOfA, eq_comm] using htt + refine utRt_postA (Tn := utBasesA aligned (zA nt.val us) win.val) (Cn := ∅) (Vn := ∅) J hx hc W h1 h2 hn + (fun h => by omega) w hS hC P' har (utRtBody_globA hr) hkeep (fun q => ?_) (fun c h => h.elim) + (fun v h => h.elim) (fun h => absurd h z12) hcap (fun q h => ?_) (fun c h => h.elim) (fun v h => h.elim) + (fun q h => ?_) (fun q h => ?_) (fun y h => absurd h.1 z14) (fun q y h => ?_) (fun q es h => ?_) + (fun q l1 h => ?_) + · refine ⟨fun ⟨j, hj, e⟩ => ⟨⟨rfl, by omega, by omega, j, hj, e⟩, n1⟩, fun h => ?_⟩ + obtain ⟨⟨-, -, -, j, hj, e⟩, -⟩ := h + exact ⟨j, hj, e⟩ + · obtain ⟨j, hj, rfl⟩ := h + have dg := X.2.2 j hj n1 + have a0 : L.mem s' (affA (bjA aligned (zA nt.val us) j)) = 0#u64 := dg.2.2.1 + have wz : ∀ (K : U64) (k : Nat), K.val = k → k < 4096 → k % 8 = 0 → ¬ TcbInitK k → + L.mem s' (wa (bjA aligned (zA nt.val us) j) K) = 0#u64 := fun K k ek hk h8 ni => + wt j hj _ k (wo j hj K k ek hk) hk h8 ni + have tw := TW j hj + have o := newObj (by rw [et]; simp) j hj + rw [et] at o + have d0 : σ.curDomain = 0 := J.j.1.rest.curDom.1 + refine ⟨by rw [a0]; rfl, ?_, ?_, ?_, ?_, tw _ (Or.inr (Or.inl rfl)), tw _ (Or.inr (Or.inr rfl)), _, o, rfl, rfl, + d0, rfl, rfl, rfl⟩ + · rw [stA_eq] + exact wz _ 3328 rfl (by omega) (by omega) (by unfold TcbInitK; omega) + · exact wz 3464#u64 3464 rfl (by omega) (by omega) (by unfold TcbInitK; omega) + · exact tw _ (Or.inl rfl) + · have e0 : L.mem s' (prioA (bjA aligned (zA nt.val us) j)) = 0#u64 := + wz 3448#u64 3448 rfl (by omega) (by omega) (by unfold TcbInitK; omega) + rw [e0] + rfl + · exact absurd h.1 (by omega) + · exact absurd h.1 (by omega) + · rcases back q _ (fun _ _ e => by cases e) h with k | ⟨-, -, e⟩ + · exact Or.inl k + · rw [et] at e + cases e + · rcases back q _ (fun _ _ e => by cases e) h with k | ⟨-, -, e⟩ + · exact Or.inl k + · rw [et] at e + cases e + · rcases back q _ (fun _ _ e => by cases e) h with k | ⟨-, -, e⟩ + · exact Or.inl k + · rw [et] at e + cases e + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtRtTcbWordsA.lean b/proofs/Fl4ProofsA64/P3/UtRtTcbWordsA.lean new file mode 100644 index 000000000..81ab1de78 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtRtTcbWordsA.lean @@ -0,0 +1,413 @@ +/- +P3 (aarch64, A64-P3 Untyped W5): **the words of a Retype's new TCBs that A64 P1 leaves open**: the domain, the VCPU +and the VCPU-run link are zero. + +A64 P1's creation facts fix a new TCB's affinity (`DbgX`) and the words outside `TcbInitK` (`WinT`); offsets 3432 +(domain), 3664 and 3672 (VCPU) are `TcbInitK` words. This is the creation loop with those three words carried +(`utRetype_frameA`'s skeleton: each insert writes slot words only). + +* **`utRetype_tcbCarryA`**: after a non-reset RAM Retype of TCBs from `CrtP`, the three words of every new TCB are + zero and its registers are `init_context`'s (`UtRegsA`; later steps write slot words only). +* **`utRetype_tcbWordsA`**: the zero words alone. + +Case hypotheses: the creation start (`CrtP`, `TreeA`), the window 4 KiB aligned (`align_up`), the global `CUR_DOMAIN` word zero, off the untyped's region, +the old TCBs and the `OS` slots. No named premise. New theorems only. +-/ +import Fl4ProofsA64.P3.UtRtFrameA +import Fl4ProofsA64.P3.UtRetVcpuA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +set_option maxRecDepth 100000 + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + +/-- The three words are zero in every window. -/ +def UtTcbZW (st : machine.Store S) [L : LawfulStore st] (aligned z : U64) (len : Nat) (s : S) : Prop := + ∀ l, l < len → ∀ K : U64, K = 3432#u64 ∨ K = 3664#u64 ∨ K = 3672#u64 → L.mem s (wa (bjA aligned z l) K) = 0#u64 + +/-- Every new TCB holds `init_context`'s registers. -/ +def UtTcbRegW (st : machine.Store S) [L : LawfulStore st] (aligned z : U64) (len : Nat) (s : S) : Prop := + ∀ l, l < len → UtRegsA (L.mem s) (bjA aligned z l) + +set_option maxHeartbeats 4000000 in +/-- **The words of every new TCB**: domain and VCPU words zero, and the registers of `init_context`. -/ +theorem utRetype_tcbCarryA (hA : P0.ArchTotal ar) {H1 : Heap} {Cs Sg OS1 : Set U64} {s1 : S} + {slot p sb fi1 : U64} {aligned nt us dest off win : U64} + (C : CrtP st H1 Cs Sg OS1 s1 slot p sb fi1 false aligned nt us dest off win) {G : List (List U64)} + {par : U64 → Option U64} (hT : TreeA st OS1 G par s1) (hn1 : nt.val = 1) (hal4 : aligned.val % 4096 = 0) + (hb0 : 0 < aligned.val) + (hks0 : L.mem s1 (wa (L.ksb s1) 8#u64) = 0#u64) + (hks8 : (L.ksb s1).val % 8 = 0) (hksw : (L.ksb s1).val + 16 ≤ 2 ^ 64) + (hksF : (L.ksb s1).val + 8 < p.val ∨ p.val + 2 ^ sb.val ≤ (L.ksb s1).val + 8) + (hkso : ¬ InOS OS1 (wa (L.ksb s1) 8#u64)) + (hkT : ∀ y, H1 y = some .tcb → (L.ksb s1).val + 8 < y.val ∨ y.val + 4096 ≤ (L.ksb s1).val + 8) (a1 : A) : + untyped.retype st ar s1 a1 slot false aligned nt us dest off win false ⦃ r => + UtTcbZW st aligned (zA nt.val us) win.val r.2.1 ∧ UtTcbRegW st aligned (zA nt.val us) win.val r.2.1 ⦄ := by + obtain ⟨-, hal, hfi, hA0, hAw, hinj, hcan, -⟩ := C.ar + obtain ⟨hSA, -, hK1⟩ := C.stz + obtain ⟨-, -, hV1, hUZ1, -⟩ := hSA + have hnA := nodeAt_aligned C.n0 (by rw [debug_val]; omega) (by rw [debug_val]) + have hnF := KsOffA.node hK1 C.core0 C.n0 (by rw [debug_val]; omega) + have hM := hT.lk + obtain ⟨-, -, hpf⟩ := ut_rangeA (capA_ut_b C.cap) (by + have := hT.ut.reg slot C.src (by rw [C.cap]; exact physA_ut _ _ _ _); rwa [C.cap] at this) + have hs0 : capA st s1 slot ≠ .Null := by rw [C.cap]; exact fun h => by cases h + have hsn : ∀ j, j < win.val → slot ≠ djA dest off j := fun j hj e => hs0 (by rw [e]; exact C.dn j hj) + have hz12 : (zA nt.val us).val = 12 := by rw [hn1]; exact zA_1 us + have hX : 2 ^ (zA nt.val us).val = 4096 := by rw [hz12]; norm_num + have hfit : aligned.val + win.val * 2 ^ (zA nt.val us).val ≤ 2 ^ 64 := by omega + have hwf : ∀ j, j < win.val → CapRt.EncWf (newCapA nt.val (bjA aligned (zA nt.val us) j) us false) := fun j hj => + newCapA_encWf _ _ _ _ (hcan j hj) C.hus + have hdn1 : ∀ s', RtSrc st s1 slot p sb false aligned.val (aligned.val + win.val * 2 ^ (zA nt.val us).val) s' → + ∀ j, j < win.val → capA st s' (djA dest off j) = .Null := fun s' h1 j hj => by + obtain ⟨-, -, -, hc1, -, -⟩ := h1.facts C.os C.src + rw [hc1 _ (C.dO j hj) (fun e => hsn j hj e.symm)]; exact C.dn j hj + -- the new TCBs + have hbj : ∀ l, l < win.val → (bjA aligned (zA nt.val us) l).val = aligned.val + l * 4096 ∧ + aligned.val + l * 4096 + 4096 ≤ p.val + 2 ^ sb.val ∧ p.val + fi1.val ≤ aligned.val + l * 4096 := by + intro l hl + have h1 : (l + 1) * 4096 ≤ win.val * 4096 := Nat.mul_le_mul_right _ hl + have hAw' := hAw + rw [hX] at hAw' + have hfit' := hfit + rw [hX] at hfit' + have := bjA_val (base := aligned) (z := zA nt.val us) (j := l) (by rw [hX]; omega) + rw [hX] at this + exact ⟨this, by omega, by omega⟩ + have htcb : ∀ l, l < win.val → TcbAddr (bjA aligned (zA nt.val us) l) := by + intro l hl + obtain ⟨hv, hle, -⟩ := hbj l hl + refine ⟨by omega, ?_, by omega⟩ + rw [hv]; omega + have hw3 : ∀ l, l < win.val → ∀ K : U64, K = 3432#u64 ∨ K = 3664#u64 ∨ K = 3672#u64 → + (wa (bjA aligned (zA nt.val us) l) K).val = aligned.val + l * 4096 + K.val ∧ K.val < 4096 ∧ K.val % 8 = 0 := by + intro l hl K hK + have k : K.val < 4096 ∧ K.val % 8 = 0 := by rcases hK with rfl | rfl | rfl <;> simp + exact ⟨by rw [ut_tcbWordA (htcb l hl) k.1, (hbj l hl).1], k⟩ + have kv : (wa (L.ksb s1) 8#u64).val = (L.ksb s1).val + 8 := by + rw [wa_valP] + have : (8#u64 : U64).val = 8 := rfl + rw [this]; exact Nat.mod_eq_of_lt (by omega) + have kal : Aligned (wa (L.ksb s1) 8#u64) := by show _ % 8 = 0; rw [kv]; omega + -- a write of `OS` slot words keeps the words of the new TCBs + have inW : ∀ (l : Nat), l < win.val → ∀ (w : U64) (k : Nat), w.val = aligned.val + l * 4096 + k → k < 4096 → + k % 8 = 0 → Aligned w ∧ ¬ InOS OS1 w := by + intro l hl w k e k1 k2 + refine ⟨by show _ % 8 = 0; rw [e]; omega, fun ⟨y, hy, g1, g2⟩ => ?_⟩ + have := (hbj l hl).2 + have nw : ¬ W7.CapW H1.tcbs H1.omega Sg w := + in_free_not_capw C.heap hV1 C.src C.cap (by rw [e]; omega) (by rw [e]; omega) + have f := C.os.fit y hy + have al0 := C.os.al0 hy + unfold Aligned at al0 + have o : (w.val - y.val) < 32 ∧ (w.val - y.val) % 8 = 0 := by + refine ⟨by omega, ?_⟩ + rw [e] at g1 ⊢ + omega + obtain ⟨d, hd⟩ : ∃ d : U64, d.val = w.val - y.val := + ⟨⟨BitVec.ofNat 64 (w.val - y.val)⟩, by + show (BitVec.ofNat 64 _).toNat = _ + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega)⟩ + have ey : wa y d = w := by + apply UScalar.eq_of_val_eq + rw [wa_valP, hd] + rw [Nat.mod_eq_of_lt (by omega)] + omega + exact nw (ey ▸ C.os.cw hy (k := d) (by rw [hd]; exact o.1) (by rw [hd]; exact o.2)) + have inOS : ∀ (l : Nat), l < win.val → ∀ K : U64, K = 3432#u64 ∨ K = 3664#u64 ∨ K = 3672#u64 → + Aligned (wa (bjA aligned (zA nt.val us) l) K) ∧ ¬ InOS OS1 (wa (bjA aligned (zA nt.val us) l) K) := by + intro l hl K hK + obtain ⟨e, k1, k2⟩ := hw3 l hl K hK + exact inW l hl _ _ e k1 k2 + have rw_val : ∀ l, l < win.val → ∀ i : U64, i.val < 37 → + (Endp.regA (bjA aligned (zA nt.val us) l) i).val = aligned.val + l * 4096 + (256 + 8 * i.val) := by + intro l hl i hi + rw [Endp.regA_val (htcb l hl) hi, (hbj l hl).1]; omega + have regK : ∀ l, l < win.val → ∀ r : Fl4SpecA64.Register, + Aligned (Endp.regA (bjA aligned (zA nt.val us) l) (regIdxA r)) ∧ + ¬ InOS OS1 (Endp.regA (bjA aligned (zA nt.val us) l) (regIdxA r)) ∧ (regIdxA r).val < 37 := by + intro l hl r + have hi : (regIdxA r).val < 37 := by cases r <;> simp [regIdxA] + obtain ⟨a1, a2⟩ := inW l hl _ _ (rw_val l hl _ hi) (by omega) (by omega) + exact ⟨a1, a2, hi⟩ + let V : Nat → S → Prop := fun n s' => (UtTcbZW st aligned (zA nt.val us) win.val s' ∧ + L.mem s' (wa (L.ksb s1) 8#u64) = 0#u64) ∧ + ∀ l, l < n → l < win.val → UtRegsA (L.mem s') (bjA aligned (zA nt.val us) l) + have keepOS : ∀ (n : Nat) (s s' : S), WritesIn st s s' (InOS OS1) → V n s → V n s' := by + intro n s s' w h + refine ⟨⟨fun l hl K hK => ?_, ?_⟩, fun l hn hl r => ?_⟩ + · obtain ⟨a1, a2⟩ := inOS l hl K hK + rw [w.mem_eq a1 a2]; exact h.1.1 l hl K hK + · rw [w.mem_eq kal hkso]; exact h.1.2 + · obtain ⟨a1, a2, -⟩ := regK l hl r + rw [w.mem_eq a1 a2]; exact h.2 l hn hl r + refine WP.spec_mono (untyped.retype.genJK ar a1 C.cap nt us dest off win aligned false C.hn C.hz hfit + (J := fun s' s'' i => UtLoopA st OS1 G par s' slot (djA dest off) + (fun j => newCapA nt.val (bjA aligned (zA nt.val us) j) us false) s'' i ∧ + ZTc st H1 s1 aligned (zA nt.val us) win.val s'' i ∧ V i s'') + (K := fun s' s'' i => UtLoopA st OS1 G par s' slot (djA dest off) + (fun j => newCapA nt.val (bjA aligned (zA nt.val us) j) us false) s'' i ∧ + ZTc st H1 s1 aligned (zA nt.val us) win.val s'' (i + 1) ∧ V (i + 1) s'') + (fun _ _ s' a' j hj h => ?_) (fun s' h1 s'' i hi h => ?_) (fun s' h1 => ?_)) ?_ + · -- one object + obtain ⟨hL, hZ, hVs⟩ := h + have P1s := create_object.hCOZ (ar := ar) hA C.heap C.os hV1 C.src C.cap nt us win aligned false C.hn + (fun h => by cases h) hal hA0 hAw hpf hnA C.nc (hnF slot C.src p sb fi1 C.cap) s' a' j hj hZ + obtain ⟨hk, hzero, hhd⟩ := id hZ + have hnw : nwA st s' = nwA st s1 := nodeAt_of_ksb hk _ _ + obtain ⟨hv, hle, hlo⟩ := hbj j hj + have ht := htcb j hj + have haff : L.mem s' (wa (bjA aligned (zA nt.val us) j) 3520#u64) = 0#u64 := by + have e : (wa (bjA aligned (zA nt.val us) j) 3520#u64).val = aligned.val + j * 4096 + 3520 := by + rw [ut_tcbWordA ht (by simp), hv]; simp + have h1 : (j + 1) * 4096 ≤ win.val * 4096 := Nat.mul_le_mul_right _ hj + generalize wa (bjA aligned (zA nt.val us) j) 3520#u64 = w at e ⊢ + have al : Aligned w := by show _ % 8 = 0; omega + generalize 2 ^ (zA nt.val us).val = X at hzero hX + subst hX + have g := ut_inWinA aligned.val j win.val 3520 hj (by norm_num) + rw [← e] at g + exact hzero w al g.1 g.2 + have hnb : obj.node_base st s' 0#u64 = ok (core.num.U64.wrapping_add + (core.num.U64.wrapping_add (L.ksb s') 223488#u64) (core.num.U64.wrapping_mul 0#u64 4288#u64)) := by + unfold obj.node_base + rw [L.ks_base_eq, eq_of_spec_fd Fl4ProofsA64.P0.Consts.layout.ks.NODES.spec, + eq_of_spec_fd Fl4ProofsA64.P0.Consts.layout.node.SIZE.spec] + simp only [bind_ok, lift] + generalize hnbv : core.num.U64.wrapping_add (core.num.U64.wrapping_add (L.ksb s') 223488#u64) + (core.num.U64.wrapping_mul 0#u64 4288#u64) = nb at hnb + have h88 : wa nb 88#u64 = nwA st s1 := by + rw [← hnw, ← hnbv] + have hD : layout.node.DEBUG_TCBS = 88#u64 := UScalar.eq_of_val_eq (by rw [debug_val]; simp) + unfold nwA LawfulStore.nodeAt + rw [hD] + have offW : ∀ b : U64, (b.val < p.val + fi1.val ∨ p.val + 2 ^ sb.val ≤ b.val) → + utOffTcbA (bjA aligned (zA nt.val us) j) b := fun b h => by + unfold utOffTcbA + rw [hv]; omega + have ek : wa (L.ksb s') 8#u64 = wa (L.ksb s1) 8#u64 := by rw [hk] + have koff : utOffTcbA (bjA aligned (zA nt.val us) j) (wa (L.ksb s1) 8#u64) := by + refine offW _ ?_ + rw [kv]; rcases hksF with g | g <;> omega + have head : L.mem s' (nwA st s1) ≠ 0#u64 → + Aligned (wa (L.mem s' (nwA st s1)) 3584#u64) ∧ + utOffTcbA (bjA aligned (zA nt.val us) j) (wa (L.mem s' (nwA st s1)) 3584#u64) ∧ + (∀ l, l < win.val → ∀ (w : U64) (k : Nat), w.val = aligned.val + l * 4096 + k → k < 4096 → k ≠ 3584 → + w ≠ wa (L.mem s' (nwA st s1)) 3584#u64) ∧ + wa (L.ksb s1) 8#u64 ≠ wa (L.mem s' (nwA st s1)) 3584#u64 := by + intro h0 + rcases hhd with e | e | ⟨j', hj', e⟩ + · exact absurd e h0 + · generalize L.mem s' (nwA st s1) = h at e + have hf := C.heap.fits _ _ e + have ha := C.heap.align _ _ e + simp only [HKind.bits] at hf ha + have e3 : (wa h 3584#u64).val = h.val + 3584 := by + rw [wa_valP] + have : (3584#u64 : U64).val = 3584 := rfl + rw [this]; exact Nat.mod_eq_of_lt (by omega) + have g := hV1 slot C.src p sb fi1 C.cap _ .tcb e + simp only [HKind.bits] at g + have off : (wa h 3584#u64).val < p.val + fi1.val ∨ p.val + 2 ^ sb.val ≤ (wa h 3584#u64).val := by + rw [e3]; rcases g with g | g <;> omega + refine ⟨by show _ % 8 = 0; rw [e3]; omega, offW _ off, fun l hl w k ew k1 _ heq => ?_, fun heq => ?_⟩ + · have q := congrArg UScalar.val heq + have := (hbj l hl).2.1 + have := (hbj l hl).2.2 + rw [ew] at q + rcases off with o | o <;> omega + · have := congrArg UScalar.val heq + rw [kv, e3] at this + rcases hkT h e with k | k <;> omega + · obtain ⟨hv', hle', -⟩ := hbj j' (by omega) + have e3 : (wa (bjA aligned (zA nt.val us) j') 3584#u64).val = aligned.val + j' * 4096 + 3584 := by + rw [ut_tcbWordA (htcb j' (by omega)) (by simp), hv']; simp + have hjj : (j' + 1) * 4096 ≤ j * 4096 := Nat.mul_le_mul_right _ hj' + rw [e] + refine ⟨by show _ % 8 = 0; rw [e3]; omega, by unfold utOffTcbA; rw [e3, hv]; omega, + fun l hl w k ew k1 k3 heq => ?_, fun heq => ?_⟩ + · have := congrArg UScalar.val heq + rw [ew, e3] at this + omega + · have := congrArg UScalar.val heq + rw [kv, e3] at this + have := (hbj j' (by omega)).2 + have := (hbj j' (by omega)).2.2 + rcases hksF with g | g <;> omega + have M := ut_createTcbA (st := st) ar s' a' nt (bjA aligned (zA nt.val us) j) us nb false hn1 ht haff hnb + (by rw [h88]; exact hnA) (by rw [h88]; exact offW _ (hnF slot C.src p sb fi1 C.cap)) + (by rw [ek]; exact kal) (by rw [ek]; exact koff) + (by rw [h88]; exact fun h0 => ⟨(head h0).1, (head h0).2.1⟩) + refine WP.spec_mono (spec_andA P1s M) ?_ + rintro r ⟨⟨hc, hk', hZ'⟩, -, -, hw, hregs, hdom⟩ + refine ⟨hc, hL.keep C.os (fun l hl => C.dO l (by omega)) hk', hZ', ?_⟩ + rw [h88] at hw + have nd := hnF slot C.src p sb fi1 C.cap + have kk : L.mem r.2.1 (wa (L.ksb s1) 8#u64) = 0#u64 := by + rw [hw.mem_eq kal ?_] + · exact hVs.1.2 + rintro (hi | e | ⟨h0, e⟩) + · exact utTcbInit_inA ht hi koff + · have e' := congrArg UScalar.val e + have nv : (nwA st s1).val = (L.ksb s1).val + 223576 := by + unfold nwA + rw [nodeAt_val C.n0 (by rw [debug_val]; omega), debug_val] + unfold LawfulStore.nodeBase + simp + rw [kv, nv] at e' + omega + · exact (head h0).2.2.2 e + refine ⟨⟨fun l hl K hK => ?_, kk⟩, fun l hn hl r => ?_⟩ + swap + · by_cases e : l = j + · subst e; exact hregs r + obtain ⟨wal, -, hi'⟩ := regK l hl r + have ew := rw_val l hl _ hi' + have lt : 256 + 8 * (regIdxA r).val < 4096 := by omega + have ni : ¬ utTcbInitA (bjA aligned (zA nt.val us) j) (Endp.regA (bjA aligned (zA nt.val us) l) (regIdxA r)) := by + refine fun hi => utTcbInit_inA ht hi ?_ + unfold utOffTcbA + rw [ew, hv] + rcases Nat.lt_or_ge l j with g | g + · have : (l + 1) * 4096 ≤ j * 4096 := Nat.mul_le_mul_right _ g + omega + · have : (j + 1) * 4096 ≤ l * 4096 := Nat.mul_le_mul_right _ (by omega) + omega + rw [hw.mem_eq wal ?_] + · exact hVs.2 l (by omega) hl r + rintro (hi | e' | ⟨h0, e'⟩) + · exact ni hi + · have e'' := congrArg UScalar.val e' + rw [ew] at e'' + have := (hbj l hl).2.1 + have := (hbj l hl).2.2 + have nd' : (nwA st s1).val < p.val + fi1.val ∨ p.val + 2 ^ sb.val ≤ (nwA st s1).val := nd + rcases nd' with g | g <;> omega + · exact (head h0).2.2.1 l hl _ _ ew lt (by omega) e' + obtain ⟨ew, k1, k2⟩ := hw3 l hl K hK + have wal := (inOS l hl K hK).1 + have notNode : wa (bjA aligned (zA nt.val us) l) K ≠ nwA st s1 := by + intro e + have e' := congrArg UScalar.val e + rw [ew] at e' + have := (hbj l hl).2.1 + have := (hbj l hl).2.2 + have nd' : (nwA st s1).val < p.val + fi1.val ∨ p.val + 2 ^ sb.val ≤ (nwA st s1).val := nd + rcases nd' with g | g <;> omega + have notHead : L.mem s' (nwA st s1) ≠ 0#u64 → + wa (bjA aligned (zA nt.val us) l) K ≠ wa (L.mem s' (nwA st s1)) 3584#u64 := fun h0 => + (head h0).2.2.1 l hl _ K.val ew k1 (by rcases hK with rfl | rfl | rfl <;> simp) + by_cases hlj : l = j ∧ K = 3432#u64 + · obtain ⟨rfl, rfl⟩ := hlj + rw [hdom, ek] + exact hVs.1.2 + · have ni : ¬ utTcbInitA (bjA aligned (zA nt.val us) j) (wa (bjA aligned (zA nt.val us) l) K) := by + intro hi + by_cases e : l = j + · subst e + have hK' : K = 3664#u64 ∨ K = 3672#u64 := by + rcases hK with h | h | h + · exact absurd ⟨rfl, h⟩ hlj + · exact Or.inl h + · exact Or.inr h + have vK : K.val = 3664 ∨ K.val = 3672 := by rcases hK' with rfl | rfl <;> simp + have ne : ∀ c : U64, c.val < 4096 → c.val ≠ K.val → wa (bjA aligned (zA nt.val us) l) K ≠ + wa (bjA aligned (zA nt.val us) l) c := by + intro c hc h q + have := congrArg UScalar.val q + rw [ew, ut_tcbWordA ht hc, hv] at this + omega + rcases hi with ⟨i, hi, q⟩ | q | ⟨w, hw', q⟩ | q | q + · have := congrArg UScalar.val q + rw [ew, regA_val ht hi, hv] at this + omega + · exact ne 3432#u64 (by simp) (by simp; omega) q + · have hk : (⟨BitVec.ofNat 64 (3600 + 8 * w.val)⟩ : U64).val = 3600 + 8 * w.val := by + show (BitVec.ofNat 64 (3600 + 8 * w.val)).toNat = _ + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)] + exact ne _ (by rw [hk]; omega) (by rw [hk]; omega) q + · exact ne 3576#u64 (by simp) (by simp; omega) q + · exact ne 3584#u64 (by simp) (by simp; omega) q + · refine utTcbInit_inA ht hi ?_ + unfold utOffTcbA + rw [ew, hv] + rcases Nat.lt_or_ge l j with g | g + · have : (l + 1) * 4096 ≤ j * 4096 := Nat.mul_le_mul_right _ g + omega + · have : (j + 1) * 4096 ≤ l * 4096 := Nat.mul_le_mul_right _ (by omega) + omega + rw [hw.mem_eq wal ?_] + · exact hVs.1.1 l hl K hK + rintro (hi | e | ⟨h0, e⟩) + · exact ni hi + · exact notNode e + · exact notHead h0 e + · -- one insert + have hnx : NxA st s'' slot ≠ 0#u64 → NxA st s'' slot ∈ OS1 := (MdbLk.nxG C.os h.1.1 h.1.2.2.1).2 + refine WP.spec_mono (WP.spec_and + (UtLoopA.step C.os hi h.1 C.dO (hdn1 s' h1) C.dc hinj hsn hwf (fun j _ => newCapA_ne _ _ _ _)) + (WP.spec_and + (cdt.insert_new_cap.zqW (Zq := fun s j => ZTc st H1 s1 aligned (zA nt.val us) win.val s j) C.os + (fun _ _ _ h w => ZTc.capw C.heap C.os hV1 C.src C.cap hA0 hAw hnA C.nc h w) + (C.dO i hi) (MdbLk.nxG C.os h.1.1 h.1.2.2.1) h.2.1) + (utInsert_inOSA C.os C.src (C.dO i hi) hnx))) ?_ + rintro r ⟨hl, hz, hw⟩ + exact ⟨hl, hz, keepOS _ _ _ hw h.2.2⟩ + · -- the source rewritten + obtain ⟨-, -, -, -, -, hW⟩ := h1.facts C.os C.src + have L0 := h1.lk C.os C.src hM hs0 + have hsG : slot ∈ G.flatten := (hM.members slot).2 ⟨C.src, hs0⟩ + have wS : WritesIn st s1 s' (InOS OS1) := hW.mono fun b hb => by + have f := C.os.fit slot C.src + refine ⟨slot, C.src, ?_, ?_⟩ <;> rcases hb with e | e + · rw [e] + · rw [e, wv f (by simp)]; simp + · rw [e]; omega + · rw [e, wv f (by simp)]; simp + have V0 : V 0 s1 := by + refine ⟨⟨fun l hl K hK => ?_, hks0⟩, fun l hn => absurd hn (Nat.not_lt_zero l)⟩ + obtain ⟨ew, k1, -⟩ := hw3 l hl K hK + have := (hbj l hl).2 + have := (hbj l hl).2.2 + exact hUZ1 slot C.src p sb fi1 C.cap _ (inOS l hl K hK).1 (by rw [ew]; omega) (by rw [ew]; omega) + refine ⟨⟨L0, by rw [utPA_zero]; exact hT.tree, hsG, fun _ _ _ => rfl, + fun j hj => absurd hj (Nat.not_lt_zero j), fun y x => ?_, fun _ _ _ => rfl, + fun j hj => absurd hj (Nat.not_lt_zero j)⟩, + ZTc.start C.heap C.os hV1 C.src C.cap hA0 hAw hnA C.nc hUZ1 C.hd hW, keepOS _ _ _ wS V0⟩ + rw [utPA_zero] + exact ⟨Or.inl, fun h => h.elim id fun ⟨j, hj, _⟩ => absurd hj (Nat.not_lt_zero j)⟩ + rintro r ⟨-, s', -, -, -, hV⟩ + exact ⟨hV.1.1, fun l hl => hV.2 l hl hl⟩ + +set_option maxHeartbeats 1000000 in +/-- **The domain and VCPU words of every new TCB are zero** (module docstring). -/ +theorem utRetype_tcbWordsA (hA : P0.ArchTotal ar) {H1 : Heap} {Cs Sg OS1 : Set U64} {s1 : S} + {slot p sb fi1 : U64} {aligned nt us dest off win : U64} + (C : CrtP st H1 Cs Sg OS1 s1 slot p sb fi1 false aligned nt us dest off win) {G : List (List U64)} + {par : U64 → Option U64} (hT : TreeA st OS1 G par s1) (hn1 : nt.val = 1) (hal4 : aligned.val % 4096 = 0) + (hb0 : 0 < aligned.val) + (hks0 : L.mem s1 (wa (L.ksb s1) 8#u64) = 0#u64) + (hks8 : (L.ksb s1).val % 8 = 0) (hksw : (L.ksb s1).val + 16 ≤ 2 ^ 64) + (hksF : (L.ksb s1).val + 8 < p.val ∨ p.val + 2 ^ sb.val ≤ (L.ksb s1).val + 8) + (hkso : ¬ InOS OS1 (wa (L.ksb s1) 8#u64)) + (hkT : ∀ y, H1 y = some .tcb → (L.ksb s1).val + 8 < y.val ∨ y.val + 4096 ≤ (L.ksb s1).val + 8) (a1 : A) : + untyped.retype st ar s1 a1 slot false aligned nt us dest off win false ⦃ r => + UtTcbZW st aligned (zA nt.val us) win.val r.2.1 ⦄ := + WP.spec_mono (utRetype_tcbCarryA (ar := ar) hA C hT hn1 hal4 hb0 hks0 hks8 hksw hksF hkso hkT a1) + (fun _ h => h.1) + + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtRtThA.lean b/proofs/Fl4ProofsA64/P3/UtRtThA.lean new file mode 100644 index 000000000..2b15af46a --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtRtThA.lean @@ -0,0 +1,200 @@ +/- +P3 (aarch64, A64-P3 Untyped W3, closure item D): **the typed heap after a Retype** (contract §3: `cnode us`, `tcb`, +`other (2^bits)` for every other RAM object), and its `Z.th` split (lead 01:24Z). + +* `utRtThA`: `th` with the RAM retype's new bases at their kinds (`utKindA`); an untyped or device retype adds none. +* `utRtTh_splitA` (the split), `utRtTh_oldA` / `utRtTh_growA` (old entries kept), `utRtTh_posA` (**D**: + `JStage3A.thPos`). +* `utRtTh_tcbA` (`thT`'s split), `utRtTh_ipcA` (new notifications, endpoints and replies are `other 64 / 16 / 32`). +* `utRtTh_placeA`: `PlaceA` is kept. +* `utEnd_thDisjA`: every old entry is off the new objects' window, from `ObjAllocA` at the creation's start. + +No named premise. New definitions and theorems only. +-/ +import Fl4ProofsA64.P3.UtRtEndA +import Fl4ProofsA64.P3.UtDecodeA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Classical + +noncomputable section + +/-- The typed-heap kind of a new object of A64 type number `nt`. -/ +def utKindA (nt us : U64) : ObjKindA := + if nt.val = 1 then .tcb else if nt.val = 4 then .cnode us.val else .other (2 ^ (zA nt.val us).val) + +/-- The new objects' bases in the typed heap: a RAM retype of a type other than untyped, SchedContext (an SC is no +object of `th`: `ScPlacedA.out` keeps every SC off `th`) and the translation tables and roots (types 8, 11, 13, 14: +`TabOffA.obj` and `ExtTabOffA` keep every table off `th`). -/ +def UtNewA (dev : Bool) (nt us aligned : U64) (win : Nat) (p : U64) : Prop := + dev = false ∧ nt.val ≠ 0 ∧ (nt.val ≠ 5 ∧ nt.val ≠ 8 ∧ nt.val ≠ 11 ∧ nt.val ≠ 13 ∧ nt.val ≠ 14) ∧ + ∃ j < win, p = bjA aligned (zA nt.val us) j + +/-- **The typed heap after the creation.** -/ +def utRtThA (th : TypedHeapA) (dev : Bool) (nt us aligned : U64) (win : Nat) : TypedHeapA := fun p => + if UtNewA dev nt us aligned win p then some (utKindA nt us) else th p + +section +variable {th : TypedHeapA} {dev : Bool} {nt us aligned : U64} {win : Nat} + +theorem utRtTh_splitA {p : U64} {k : ObjKindA} (h : utRtThA th dev nt us aligned win p = some k) : + (¬ UtNewA dev nt us aligned win p ∧ th p = some k) ∨ + (UtNewA dev nt us aligned win p ∧ k = utKindA nt us) := by + unfold utRtThA at h + split_ifs at h with c + · exact Or.inr ⟨c, (Option.some.inj h).symm⟩ + · exact Or.inl ⟨c, h⟩ + +theorem utRtTh_oldA {p : U64} (h : ¬ UtNewA dev nt us aligned win p) : + utRtThA th dev nt us aligned win p = th p := ite_eq_right h + +theorem utRtTh_newA {p : U64} (h : UtNewA dev nt us aligned win p) : + utRtThA th dev nt us aligned win p = some (utKindA nt us) := ite_eq_left h + +theorem utRtTh_growA (hfree : ∀ p, UtNewA dev nt us aligned win p → th p = none) {p : U64} {k : ObjKindA} + (h : th p = some k) : utRtThA th dev nt us aligned win p = some k := by + rw [utRtTh_oldA (fun c => by rw [hfree p c] at h; cases h)]; exact h + +/-- **D**: no `other` object of the new typed heap is empty. -/ +theorem utRtTh_posA (h : ∀ p z, th p = some (.other z) → 0 < z) : + ∀ p z, utRtThA th dev nt us aligned win p = some (.other z) → 0 < z := fun p z hz => by + rcases utRtTh_splitA hz with ⟨-, k⟩ | ⟨-, k⟩ + · exact h p z k + · unfold utKindA at k + split_ifs at k + cases k + exact Nat.two_pow_pos _ + +/-- `thT`'s split: the TCBs are the old ones and, for a RAM TCB retype, the new ones. -/ +theorem utRtTh_tcbA {T : Set U64} (h : ∀ t, th t = some .tcb ↔ t ∈ T) + (hfree : ∀ p, UtNewA dev nt us aligned win p → th p = none) (t : U64) : + utRtThA th dev nt us aligned win t = some .tcb ↔ + t ∈ T ∨ (UtNewA dev nt us aligned win t ∧ nt.val = 1) := by + by_cases c : UtNewA dev nt us aligned win t + · rw [utRtTh_newA c] + have nt' : t ∉ T := fun m => by rw [← h t, hfree t c] at m; cases m + unfold utKindA + split_ifs with h1 h4 + · exact ⟨fun _ => Or.inr ⟨c, h1⟩, fun _ => rfl⟩ + · exact ⟨fun e => (by cases e), fun m => m.elim (fun m => absurd m nt') (fun m => absurd m.2 h1)⟩ + · exact ⟨fun e => (by cases e), fun m => m.elim (fun m => absurd m nt') (fun m => absurd m.2 h1)⟩ + · rw [utRtTh_oldA c, h] + exact ⟨Or.inl, fun m => m.elim id (fun m => absurd m.1 c)⟩ + +/-- The IPC objects' kinds: notification 64, endpoint 16, reply 32 bytes. -/ +theorem utRtTh_ipcA {p : U64} (c : UtNewA dev nt us aligned win p) : + (nt.val = 3 → utRtThA th dev nt us aligned win p = some (.other 64)) ∧ + (nt.val = 2 → utRtThA th dev nt us aligned win p = some (.other 16)) ∧ + (nt.val = 6 → utRtThA th dev nt us aligned win p = some (.other 32)) := by + rw [utRtTh_newA c] + refine ⟨fun h => ?_, fun h => ?_, fun h => ?_⟩ <;> simp [utKindA, zA, h] + +/-- A new object's size is `2 ^ zA`. -/ +theorem utKind_sizeA (hn : nt.val < 15) (_ : nt.val ≠ 0) (hus : us.val < 64) : + (utKindA nt us).size = 2 ^ (zA nt.val us).val := by + unfold utKindA + split_ifs with h1 h4 + · simp [ObjKindA.size, zA, h1] + · have e : (zA nt.val us).val = us.val + 5 := by + rw [zA_valA nt us hn hus]; simp [zOfA, h4, Nat.add_comm] + simp [ObjKindA.size, e] + · rfl + +/-- **`PlaceA` is kept**: the new objects are aligned, fit, are off every old object and the kernel-state block, and +none is at address 0. -/ +theorem utRtTh_placeA {ksb : U64} (P : PlaceA th ksb) (hn : nt.val < 15) (hus : us.val < 64) + (hal : aligned.val % 2 ^ (zA nt.val us).val = 0) (hnz : 0 < aligned.val) + (hfit : aligned.val + win * 2 ^ (zA nt.val us).val ≤ 2 ^ 64) + (hks : ksb.val + ksSizeA ≤ aligned.val ∨ aligned.val + win * 2 ^ (zA nt.val us).val ≤ ksb.val) + (hlt : ∀ j, j < win → aligned.val + j * 2 ^ (zA nt.val us).val < 2 ^ 64) + (hdisj : ∀ p k, th p = some k → + p.val + k.size ≤ aligned.val ∨ aligned.val + win * 2 ^ (zA nt.val us).val ≤ p.val) + (hpos : ∀ p k, th p = some k → 0 < k.size) : + PlaceA (utRtThA th dev nt us aligned win) ksb := by + have hX : 0 < 2 ^ (zA nt.val us).val := Nat.two_pow_pos _ + have nw : ∀ p, UtNewA dev nt us aligned win p → (utKindA nt us).size = 2 ^ (zA nt.val us).val ∧ + ∃ j < win, p.val = aligned.val + j * 2 ^ (zA nt.val us).val ∧ + aligned.val + j * 2 ^ (zA nt.val us).val + 2 ^ (zA nt.val us).val ≤ + aligned.val + win * 2 ^ (zA nt.val us).val := by + rintro p ⟨-, h0, -, j, hj, rfl⟩ + have h1 : j * 2 ^ (zA nt.val us).val + 2 ^ (zA nt.val us).val ≤ win * 2 ^ (zA nt.val us).val := by + have := Nat.mul_le_mul_right (2 ^ (zA nt.val us).val) (Nat.succ_le_of_lt hj) + rwa [Nat.succ_mul] at this + exact ⟨utKind_sizeA hn h0 hus, j, hj, bjA_val (hlt j hj), by omega⟩ + have old : ∀ p k, ¬ UtNewA dev nt us aligned win p → utRtThA th dev nt us aligned win p = some k → + th p = some k := fun p k c h => by rwa [utRtTh_oldA c] at h + refine ⟨fun p k h => ?_, fun p k h => ?_, fun p q kp kq hp hq hne => ?_, fun p k h hirq => ?_, + fun i hi => ?_, fun p k h => ?_⟩ + · rcases utRtTh_splitA h with ⟨-, o⟩ | ⟨c, rfl⟩ + · exact P.align p k o + · obtain ⟨es, j, -, ev, -⟩ := nw p c + rw [es, ev, Nat.add_mul_mod_self_right]; exact hal + · rcases utRtTh_splitA h with ⟨-, o⟩ | ⟨c, rfl⟩ + · exact P.fit p k o + · obtain ⟨es, j, -, ev, hle⟩ := nw p c + rw [es, ev]; omega + · rcases utRtTh_splitA hp with ⟨-, op⟩ | ⟨cp, rfl⟩ <;> rcases utRtTh_splitA hq with ⟨-, oq⟩ | ⟨cq, rfl⟩ + · exact P.apart p q kp kq op oq hne + · obtain ⟨es, j, -, ev, hle⟩ := nw q cq + have := hpos p kp op + rw [es, ev] + rcases hdisj p kp op with d | d <;> omega + · obtain ⟨es, j, -, ev, hle⟩ := nw p cp + have := hpos q kq oq + rw [es, ev] + rcases hdisj q kq oq with d | d <;> omega + · obtain ⟨es, j, -, ev, -⟩ := nw p cp + obtain ⟨-, l, -, ew, -⟩ := nw q cq + have hjl : j ≠ l := fun e => hne (UScalar.eq_of_val_eq (by rw [ev, ew, e])) + rw [es, ev, ew] + rcases Nat.lt_or_ge j l with g | g + · have := Nat.mul_le_mul_right (2 ^ (zA nt.val us).val) (Nat.succ_le_of_lt g) + rw [Nat.succ_mul] at this + omega + · have := Nat.mul_le_mul_right (2 ^ (zA nt.val us).val) (Nat.succ_le_of_lt (Nat.lt_of_le_of_ne g hjl.symm)) + rw [Nat.succ_mul] at this + omega + · rcases utRtTh_splitA h with ⟨-, o⟩ | ⟨c, rfl⟩ + · exact P.ks p k o hirq + · obtain ⟨es, j, -, ev, hle⟩ := nw p c + rw [es, ev] + rcases hks with d | d + · right; omega + · left; omega + · have h := P.irq i hi + have c : ¬ UtNewA dev nt us aligned win (Fl4ProofsA64.P1.irqSlotA ksb i) := fun c => by + obtain ⟨-, j, -, ev, hle⟩ := nw _ c + have := hpos _ _ h + rcases hdisj _ _ h with d | d <;> omega + rw [utRtTh_oldA c]; exact h + · rcases utRtTh_splitA h with ⟨-, o⟩ | ⟨c, rfl⟩ + · exact P.nz p k o + · obtain ⟨-, j, -, ev, -⟩ := nw p c + intro e + rw [e] at ev + have : (0#u64 : U64).val = 0 := rfl + omega + +end + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- Every old typed-heap entry is off the new objects' window (`ObjAllocA` at the creation's start: the window lies +in the untyped's free part). -/ +theorem utEnd_thDisjA {Z : XSetsA} {H1 : Heap} {OS1 : Set U64} {s1 : S} (ha : ObjAllocA (st := st) Z H1 OS1 s1) + {slot p sb fi1 aligned : U64} {dev : Bool} (hs : slot ∈ OS1) (hc : capA st s1 slot = .Untyped p sb fi1 dev) + (hfa : p.val + fi1.val ≤ aligned.val) {w : Nat} (haw : aligned.val + w ≤ p.val + 2 ^ sb.val) : + ∀ q k, Z.th q = some k → q.val + k.size ≤ aligned.val ∨ aligned.val + w ≤ q.val := by + intro q k hq + rcases ha q k.size (Or.inr (Or.inr (Or.inl ⟨k, hq, rfl⟩))) slot hs p sb fi1 dev hc with h | h | ⟨-, h⟩ + · left; omega + · right; omega + · left; omega + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtRtTopA.lean b/proofs/Fl4ProofsA64/P3/UtRtTopA.lean new file mode 100644 index 000000000..e3064cfd0 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtRtTopA.lean @@ -0,0 +1,175 @@ +/- +P3 (aarch64, A64-P3 Untyped W5): **the contract's top post for `untyped::decode` at the relation level**, RAM +untyped, no reset. + +* **`utDecode_postA`**: when the untyped has a CDT child (so the decode does not reset), `untyped::decode` refines + `fl4DecodeUntypedInvocationVirt`, and after it `JAtZAX` holds — at the grown typed heap, CDT ghost, sets and view + — for every outcome of the spec's `invoke_untyped` body run from the restarted spec state. The new TCBs, SCs and + VCPUs join `T`, `C`, `V`; new VSpace pages join `S1` / `X` / `Y` by type (`utRtZA`, `UtVsGrowA` inside + `utRt_postA`). + +Premises: the record `JStage3A` (concrete J5, J7); the decode-site facts (`DecPre`, `SysPlace`, `RsSiteA`) and `hnew` +of `utDecode_noResetA`; and of the creation: +* [P] `hVP` (the platform's VCPU registers), `hKt` / `hK0` (the kernel TTBR1 entries, zero below 256), + `A64FrameMasks`, virtualization, fan-out 256. +* [J] `hkw`: a new `HighVSpace` window is in the kernel window. + +New theorems only. +-/ +import Fl4ProofsA64.P3.UtRtDecSiteA +import Fl4ProofsA64.P3.UtRtCreateA +import Fl4ProofsA64.P3.Stage3RestartA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +set_option maxRecDepth 100000 + +section site +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rq : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s : S} {a : A} {σ : State Unit} {m : McState} + +set_option maxHeartbeats 4000000 in +/-- **The contract's top post for `decode`, relation level** (module docstring). -/ +theorem utDecode_postA (hA : P0.ArchTotal ar) + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rq Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {p sb fi : U64} (ex : Aeneas.Std.Array obj.Slot 3#usize) + (sl : CslotPtr) (hsrc : ∃ q, sl.1 = specPtr q ∧ Z.th q = some (.cnode sl.2.length)) + {Rp Nt Ud Acd : Set U64} + (D : DecPre st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ud Acd s (.Untyped p sb fi false) ex) + (P : SysPlace st H Z.Cs H.frame Rp Nt OS s ex (cslotAddrA sl)) + (hcapF : obj.Slot.cap st (cslotAddrA sl) s = ok (.Untyped p sb fi false)) + (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) + (hfm : A64FrameMasks st s) + (vi : U64) (hvi : ar.virtualization a = ok vi) (hvirt : vi = 0#u64 ↔ fl4A64Virt = false) + (hfan : Fl4SpecA64.retypeFanOutLimit = 256#64) + (lbl : U64) (slbl : Fl4SpecA64.Data) + (hlbl : Fl4SpecA64.genInvocationType slbl = .untypedRetype ↔ lbl = fl4_abi.label.UntypedRetype) + (length buf : U64) (args : List U64) (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) + (exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) (hex : ExtraOKA Z.th (L.mem s) ex exs) + (hchild : ∃ x, σ.cdt x = some sl) + (hnew : ∀ aligned nt us dest off win : U64, + UtArgs st s ex (cslotAddrA sl) p sb fi false false aligned nt us dest off win → + PlaceA (utRtThA Z.th false nt us aligned win.val) (L.ksb s) ∧ + ∀ j, j < win.val → utRangedA nt.val → SpNoOvA (bjA aligned (zA nt.val us) j) (zOfA nt.val us.val)) + -- the arch facts of the creation + (hVP : ∀ a1 : A, UtVcpuPlatA ar a1) {k : U64 → U64} + (hKt : ∀ (a1 : A) (i : U64), i.val < 511 → ar.kernel_ttbr1_entry a1 0#u64 i = ok (k i)) + (hK0 : ∀ i : U64, i.val < 256 → k i = 0#u64) + (hkw : ∀ aligned nt us dest off win : U64, + UtArgs st s ex (cslotAddrA sl) p sb fi false false aligned nt us dest off win → nt.val = 14 → + 18446603336221196288 ≤ aligned.val) : + untyped.decode st ar s a lbl length (cslotAddrA sl) (.Untyped p sb fi false) buf ex ⦃ res => + UPostA σ (fl4DecodeUntypedInvocationVirt (Z := Unit) slbl (args.map UScalar.bv) sl + (absCapA (.Untyped p sb fi false)) (exs.map Prod.fst)) s a + (fun inv res => ∃ (base retypeBase : Word) (t : ApiObjectType) (us : Nat) (slots : List CslotPtr), + inv = .retype sl false base retypeBase t us slots false ∧ res.1 = .Ok () ∧ + ∃ xT, σ.kheap σ.curThread = some (.tcb xT) ∧ + ∃ aligned nt usF dest off win : U64, + UtArgs st s ex (cslotAddrA sl) p sb fi false false aligned nt usF dest off win ∧ + ∀ r ∈ (utRtBodyA sl base retypeBase t us slots false (stUpdA σ σ.curThread xT .restart)).1, + JAtZAX (L := L) AR + (utRtZA Z ⟨utGA (cslotAddrA sl) (djA dest off) Z.G.chains win.val, + utPA (cslotAddrA sl) (djA dest off) Z.G.par win.val⟩ nt usF aligned win.val + {t | nt.val = 1 ∧ t ∈ utBasesA aligned (zA nt.val usF) win.val} + (utCNewA nt aligned (zA nt.val usF) win.val) + {v | nt.val = 12 ∧ v ∈ utBasesA aligned (zA nt.val usF) win.val}) + res.2.1 res.2.2 r.2 + (utRtMA m {t | nt.val = 1 ∧ t ∈ utBasesA aligned (zA nt.val usF) win.val})) res ⦄ := by + have Je := J.toJEntA + have hV : SlotsValidA Z.th (L.mem s) := Je.j.1.cv + have hpos : ∀ p k, Z.th p = some k → 0 < k.size := fun p k h => by + cases k with + | cnode r => exact Nat.two_pow_pos _ + | tcb => exact Nat.succ_pos _ + | other z => exact J.thPos p z h + have hsub : ∀ y, IsSlotA Z.th y → y ∈ OS := J.j5.2.1 + have hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null := J.j5.2.2 + have hR := Je.j.1.st + have hC := Je.j.1.cdt + have hP := Je.j.1.place + have hO := Je.sys.ent.os + have hTr : TreeA st OS Z.G.chains Z.G.par s := + hC.tree.grow hsub fun x hx hn => hnul x hx hn + have hAs : AbsSlotA Z.th sl := by + obtain ⟨q, e, hq⟩ := hsrc + exact ⟨q, e, Or.inl hq⟩ + have hsO : cslotAddrA sl ∈ OS := hsub _ (hAs.isSlot hP) + have R := UtDecP.of_live D Je.sys.sysl.sys P hcapF Je.sys.sysl.ln Je.sys.sysl.vr Je.sys.sysl.ly + have hcap : capA st s (cslotAddrA sl) = .Untyped p sb fi false := R.cap + have hrv : obj.Slot.revocable st (cslotAddrA sl) s = ok true := + Je.sys.sysl.sys.stz.1.2.1 _ hsO (by rw [hcap]; rfl) + have hsb : sb.val ≤ 39 := J.utBlk _ hsO p sb fi false hcap + refine untyped_decode_refinesA ar Z.th s a σ Z.G hR hV hP hO hTr hC hsub hfm vi hvi hvirt hfan lbl slbl hlbl + length buf args hlen hargs sl hAs hsO p sb fi false hcap hrv hsb R.utv.2.2 ex exs hex _ ?_ + intro nt us dest off win t rd reset fiv _ X hU + have hres : reset = false := by + rw [X.resetEq] + exact decide_eq_false (fun h => h hchild) + subst hres + generalize (⟨Fl4SpecA64.alignUp (p.bv + fiv.bv) (zOfA nt.val us.val)⟩ : U64) = aligned at hU ⊢ + -- the caller's restart + obtain ⟨Rp', Nt', extra', sp', hPpl⟩ := RS.pl + obtain ⟨xT, hkT, hst, -, J2, Y2, hOS1, -⟩ := J.of_restartCurA hPpl RS.sym RS.run D.nvm RS.cur RS.vs RS.vc + rw [hst, bind_ok] + set s2 := L.wr s (stA (L.curW s)) sched.TS_RESTART with hs2 + set σ2 := stUpdA σ σ.curThread xT .restart with hσ2 + have C : CrtP st H Z.Cs H.frame OS s2 (cslotAddrA sl) p sb fi false aligned nt us dest off win := + CrtP.of0 R hst hU + obtain ⟨-, hal8, -, hA0, hAw, -, -, H', -, hHH', hHn, hEx⟩ : + UtAr H p sb fi false aligned nt us dest off win := R.arith _ _ _ _ _ _ _ hU + have hTr2 : TreeA st OS Z.G.chains Z.G.par s2 := treeA_of_mem hTr hOS1 + have hS2 : StateRelA Z.th (L.mem s2) σ2 := J2.j.1.st + have hC2 : CdtRelA st Z.th s2 σ2 Z.G := J2.j.1.cdt + have hks : L.ksb s2 = L.ksb s := L.ksb_wr _ _ _ + have hrev : revA (L.mem s2) (cslotAddrA sl) = true := by + have e24 := (hOS1 _ hsO).2.2.2 + have h0 := revA_of_revocableA hrv + unfold revA at h0 ⊢ + rw [e24]; exact h0 + have hc2 : capA st s2 (cslotAddrA sl) = .Untyped p sb fi false := by rw [hOS1.capA hsO]; exact hcap + obtain ⟨hP', hno⟩ := hnew aligned nt us dest off win hU + have hb0 : 0 < aligned.val := by + have hp0 : 0 < p.val := J.sys.sysl.ly.ut0 _ hsO p sb fi hcap + have g := (CrtA.of_args R hU).pa + omega + -- the window is aligned to the object size + have CA := CrtA.of_args R hU + have h1 : p.val + fi.val ≤ aligned.val := C.ar.2.2.2.1 + have h2 : aligned.val + win.val * 2 ^ (zA nt.val us).val ≤ p.val + 2 ^ sb.val := C.ar.2.2.2.2.1 + have hal2 : aligned.val % 2 ^ (zA nt.val us).val = 0 := by + obtain ⟨y, -, -, idx, dep, val, ers, hv64, -, -, -, eal, -, -⟩ := id hU + obtain ⟨-, -, ev⟩ := untyped.retype_size.okA ers + rw [ev] at eal hv64 + simp only [Bool.false_eq_true, ↓reduceIte] at eal + have fw : (core.num.U64.wrapping_add p fi).val = p.val + fi.val := by + rw [wa_valP] + refine Nat.mod_eq_of_lt ?_ + have := CA.pf + have := C.ar.2.2.1 + omega + have hw1 : 2 ^ (zA nt.val us).val ≤ win.val * 2 ^ (zA nt.val us).val := + Nat.le_mul_of_pos_left _ (by have := X.window.1; omega) + have g := align_up_valA hv64 (by rw [fw]; have := CA.pf; omega) eal + rw [g] + exact Nat.mul_mod_left _ _ + have hpf : aligned.val + win.val * 2 ^ (zA nt.val us).val ≤ 2 ^ 64 := le_trans h2 CA.pf + refine WP.spec_mono (utCreate_postA hA J2 C X hcap hsrc + (utEnd_noResetA (ar := ar) hA R J.sys.sysl.ly hst a hU hHH' hEx hHn) hb0 hal8 hpf hal2 + (by rw [show L.ksb s2 = L.ksb s from L.ksb_wr _ _ _]; exact hP') hno hVP hKt hK0 (hkw _ _ _ _ _ _ hU)) fun res g => ?_ + exact ⟨_, _, _, _, _, rfl, g.1, xT, hkT, aligned, nt, us, dest, off, win, hU, g.2⟩ + +end site + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtRtUmA.lean b/proofs/Fl4ProofsA64/P3/UtRtUmA.lean new file mode 100644 index 000000000..cf3a3dbc4 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtRtUmA.lean @@ -0,0 +1,176 @@ +/- +P3 (aarch64, A64-P3 Untyped, full-record post, assembly): **the body of `invoke_untyped` keeps the spec's user +memory** (`UtNewObjA.um`; the sibling of `UtRtGlobA`'s walk). Its only machine operations are cache cleans, which +`machineOpLift` confines to the rest of the machine state. + +* `utRtBody_umA`: every outcome of `utRtBodyA` keeps `underlyingMemory`. + +No named premise. New definitions and theorems only. +-/ +import Fl4ProofsA64.P3.UtRtGlobA +import Fl4ProofsA64.P3.DelAsidSpecA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Sched Classical + +noncomputable section + +open Fl4SpecA64 Fl4Spec + +/-- Every outcome of `m` keeps the user memory. -/ +def UtUA {α : Type} (m : NonDet (State Unit) α) : Prop := ∀ σ r, r ∈ (m σ).1 → r.2.machineState.underlyingMemory = σ.machineState.underlyingMemory + +theorem UtUA.bind {α β : Type} {m : NonDet (State Unit) α} {f : α → NonDet (State Unit) β} (hm : UtUA m) + (hf : ∀ a, UtUA (f a)) : UtUA (m >>= f) := fun σ r hr => by + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + exact (hf x.1 x.2 r hr).trans (hm σ x hx) + +theorem UtUA.state {α : Type} {m : NonDet (State Unit) α} (h : ∀ σ r, r ∈ (m σ).1 → r.2 = σ) : UtUA m := + fun σ r hr => by rw [h σ r hr] + +theorem UtUA.pure {α : Type} (a : α) : UtUA (Pure.pure a : NonDet (State Unit) α) := + .state fun σ r h => by obtain rfl : r = (a, σ) := h; rfl + +theorem UtUA.gets {α : Type} (g : State Unit → α) : UtUA (NonDet.gets g) := + .state fun σ r h => by obtain rfl : r = (g σ, σ) := h; rfl + +theorem UtUA.fail {α : Type} : UtUA (NonDet.fail : NonDet (State Unit) α) := + fun _ _ h => absurd h (Set.notMem_empty _) + +theorem UtUA.assert (P : Prop) : UtUA (NonDet.assert P : NonDet (State Unit) Unit) := + .state fun _ _ h => by obtain ⟨-, rfl⟩ := h; rfl + +theorem UtUA.assertOpt {α : Type} (o : Option α) : UtUA (NonDet.assertOpt o : NonDet (State Unit) α) := by + cases o + · exact UtUA.fail + · exact .state fun σ r h => by obtain rfl : r = (_, σ) := h; rfl + +theorem ut_getCap_umA (sl : CslotPtr) : UtUA (getCap (Z := Unit) sl) := by + obtain ⟨oref, cref⟩ := sl + have go : UtUA (getObject (Z := Unit) oref) := .state fun _ _ h => (getObject_mem h).1 + refine UtUA.bind go fun obj => ?_ + cases obj <;> dsimp only <;> first + | exact UtUA.bind UtUA.fail fun _ => UtUA.assertOpt _ + | exact UtUA.bind (UtUA.pure _) fun _ => UtUA.assertOpt _ + | exact UtUA.bind (UtUA.assert _) fun _ => UtUA.bind (UtUA.pure _) fun _ => UtUA.assertOpt _ + +theorem ut_setCap_umA (cap : Cap) (sl : CslotPtr) : UtUA (setCap (Z := Unit) cap sl) := fun σ r hr => by + obtain ⟨oref, cref⟩ := sl + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + obtain ⟨e1, -⟩ := getObject_mem hx + obtain ⟨obj, σ1⟩ := x + simp only at e1 hr + subst e1 + have key : ∀ o : KernelObject, r ∈ ((Pure.pure o >>= fun o' => setObject (Z := Unit) oref o') σ1).1 → + r.2.machineState.underlyingMemory = σ1.machineState.underlyingMemory := by + intro o h + obtain ⟨y, hy, h⟩ := mem_bindA.1 h + obtain rfl : y = (o, σ1) := hy + obtain ⟨o0, -, hr2⟩ := setObject_mem h + rw [hr2] + have kfail : ∀ f : KernelObject → NonDet (State Unit) Unit, r ∈ ((NonDet.fail >>= f) σ1).1 → False := by + intro f h + obtain ⟨y, hy, -⟩ := mem_bindA.1 h + exact absurd hy (Set.notMem_empty _) + cases obj with + | cnode sz cn => + simp only at hr + split_ifs at hr + · exact key _ hr + · exact (kfail _ hr).elim + | tcb t => + simp only at hr + split_ifs at hr <;> + first + | exact (kfail _ hr).elim + | exact key _ hr + | _ => + try simp only at hr + exact (kfail _ hr).elim + +theorem ut_setCdt_umA (t : Cdt) : UtUA (setCdt (Z := Unit) t) := fun σ r hr => by + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + obtain rfl : x = (σ, σ) := hx + obtain rfl : r = ((), { σ with cdt := t }) := hr + exact rfl + +theorem ut_modify_umA {g : State Unit → State Unit} (h : ∀ σ, (g σ).machineState.underlyingMemory = σ.machineState.underlyingMemory) : + UtUA (NonDet.modify g) := fun σ r hr => by + obtain rfl : r = ((), g σ) := hr + exact h σ + +theorem ut_setOriginal_umA (sl : CslotPtr) (v : Bool) : UtUA (setOriginal (Z := Unit) sl v) := + UtUA.bind (UtUA.gets _) fun _ => ut_modify_umA fun _ => + rfl + +theorem ut_doExtendedOp_umA (eop : DetExtMonad Unit) : UtUA (doExtendedOp (Z := Unit) eop) := by + unfold doExtendedOp + refine UtUA.bind (.state fun σ r h => by obtain rfl : r = (σ, σ) := h; rfl) fun _ => ?_ + refine UtUA.bind (.state fun σ r h => by rw [h.2]) fun _ => ut_modify_umA fun _ => + rfl + +theorem ut_createCap_umA (t : ApiObjectType) (bits : Nat) (src : CslotPtr) (dev : Bool) (x : CslotPtr × ObjRef) : + UtUA (createCap (Z := Unit) t bits src dev x) := by + obtain ⟨dest, oref⟩ := x + unfold createCap + exact UtUA.bind (UtUA.gets _) fun _ => UtUA.bind (UtUA.gets _) fun _ => UtUA.bind (ut_setCdt_umA _) fun _ => + UtUA.bind (ut_doExtendedOp_umA _) fun _ => UtUA.bind (ut_setOriginal_umA _ _) fun _ => ut_setCap_umA _ _ + +theorem ut_mapMx_umA {α : Type} {f : α → NonDet (State Unit) Unit} (h : ∀ a, UtUA (f a)) (l : List α) : + UtUA (NonDet.mapMx f l) := by + induction l with + | nil => unfold NonDet.mapMx; exact UtUA.pure _ + | cons a l ih => unfold NonDet.mapMx; exact UtUA.bind (h a) fun _ => ih + +/-- A machine operation confined to the rest of the machine state keeps the user memory. -/ +def UtMopA {α : Type} (mop : NonDet MachineState α) : Prop := + ∀ ms r, r ∈ (mop ms).1 → r.2.underlyingMemory = ms.underlyingMemory + +theorem ut_machineOpLift_keepA (f : MachineRestMonad Unit) : UtMopA (machineOpLift f) := fun ms r hr => by + obtain ⟨mr, e⟩ := (machineOpLift_restA f ms).2.2 r hr + rw [e] + +theorem ut_doMachineOp_umA {α : Type} {mop : NonDet MachineState α} (h : UtMopA mop) : + UtUA (doMachineOp (Z := Unit) mop) := fun σ r hr => by + unfold doMachineOp at hr + obtain ⟨x, hx, hr⟩ := mem_bindA.1 hr + obtain rfl : x = (σ.machineState, σ) := hx + obtain ⟨y, hy, hr⟩ := mem_bindA.1 hr + have ey : y.2 = σ := hy.2 + have hm : y.1 ∈ (mop σ.machineState).1 := hy.1 + obtain ⟨z, hz, hr⟩ := mem_bindA.1 hr + obtain rfl : z = ((), { y.2 with machineState := y.1.2 }) := hz + obtain rfl : r = (y.1.1, { y.2 with machineState := y.1.2 }) := hr + exact h _ _ hm + +theorem ut_initArch_umA (t : ApiObjectType) (dev : Bool) (ptr : ObjRef) (n sz : Nat) (refs : List ObjRef) : + UtUA (initArchObjects (Z := Unit) t dev ptr n sz refs) := by + unfold initArchObjects + split_ifs + · exact ut_mapMx_umA (fun _ => ut_doMachineOp_umA (ut_machineOpLift_keepA _)) _ + · exact ut_mapMx_umA (fun _ => ut_doMachineOp_umA (ut_machineOpLift_keepA _)) _ + · exact UtUA.pure _ + +theorem ut_retRegion_umA (t : ApiObjectType) (ptr : ObjRef) (n bits : Nat) (dev : Bool) : + UtUA (retypeRegionWith (Z := Unit) fl4ObjBitsApi ptr n bits t dev) := fun σ r hr => by + rw [(utRetRegion_invA _ _ _ _ _ _ hr).2] + split_ifs + · rfl + · exact rfl + +/-- **The body keeps the user memory.** -/ +theorem utRtBody_umA {srcSlot : CslotPtr} {base retypeBase : Word} {t : ApiObjectType} {objSz : Nat} + {slots : List CslotPtr} {dev : Bool} {σ : State Unit} {r : Unit × State Unit} + (hr : r ∈ (utRtBodyA srcSlot base retypeBase t objSz slots dev σ).1) : r.2.machineState.underlyingMemory = σ.machineState.underlyingMemory := by + have h : UtUA (utRtBodyA srcSlot base retypeBase t objSz slots dev) := by + unfold utRtBodyA + exact UtUA.bind (ut_getCap_umA _) fun _ => UtUA.bind (UtUA.pure _) fun _ => UtUA.bind (UtUA.pure _) fun _ => + UtUA.bind (ut_setCap_umA _ _) fun _ => UtUA.bind (ut_retRegion_umA _ _ _ _ _) fun _ => + UtUA.bind (ut_initArch_umA _ _ _ _ _ _) fun _ => ut_mapMx_umA (fun x => ut_createCap_umA _ _ _ _ x) _ + exact h σ r hr + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtRtVcpuPostA.lean b/proofs/Fl4ProofsA64/P3/UtRtVcpuPostA.lean new file mode 100644 index 000000000..d951b0c0e --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtRtVcpuPostA.lean @@ -0,0 +1,208 @@ +/- +P3 (aarch64, A64-P3 Untyped W5): **`JAtZAX` after a RAM creation of VCPUs**. + +* **`utRt_vcpuPostA`**: from the record at the creation's start, FL4's creation (`CrtE`, the new VCPUs' thread and + run-caller words `UtVcpuZW`, the sharp write frame), `StateRelA` / `CdtRelA` after it, and any outcome of the spec + body, `JAtZAX` at the grown sets: the new VCPUs join `V`. + +Hypotheses: `hsub` (closure item A), the creation's window (`W`, `h1`, `h2`), `CrtE`, `VW` +(`utRetype_vcpuWordsA`), `w`, `hS` / `hC` (`utSite_relsA`), `P'`, `har`, the invoked and destination slots' CNodes. +No named premise. New theorems only. +-/ +import Fl4ProofsA64.P3.UtRtPostA +import Fl4ProofsA64.P3.UtRtGlobA +import Fl4ProofsA64.P3.UtRtCapsA +import Fl4ProofsA64.P3.UtRtZeroPostA +import Fl4ProofsA64.P3.UtRtVcpuWordsA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +set_option maxRecDepth 100000 + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s s' : S} {a a' : A} {σ : State Unit} {m : McState} {x p sb fi : U64} + {nt us aligned dest off win : U64} + +/-- **`JAtZAX` after a RAM creation of VCPUs** (module docstring). -/ +theorem utRt_vcpuPostA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (hx : x ∈ OS) (hc : capA st s x = .Untyped p sb fi false) + (W : UtWinZA nt aligned (zA nt.val us) win.val) + (h1 : p.val + fi.val ≤ aligned.val) + (h2 : aligned.val + win.val * 2 ^ (zA nt.val us).val ≤ p.val + 2 ^ sb.val) + (n12 : nt.val = 12) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (Ec : CrtE st H H.frame OS s s' x p sb false aligned nt us dest off win) + (w : WritesIn st s s' (UtCrtWA st H OS s nt aligned (zA nt.val us) win.val)) + (VW : UtVcpuZW st aligned (zA nt.val us) win.val s') + {t : ApiObjectType} (htt : objTypeOfA nt.val = some t) (hus : us.val < 64) + {srcSlot : CslotPtr} {base : Word} {slots : List CslotPtr} (hlen : slots.length = win.val) + (hsrcS : ∃ q, srcSlot.1 = specPtr q ∧ Z.th q = some (.cnode srcSlot.2.length)) + (hdst : ∀ d ∈ slots, ∃ q, d.1 = specPtr q ∧ Z.th q = some (.cnode d.2.length)) + {r : Unit × State Unit} + (hr : r ∈ (utRtBodyA srcSlot base (specPtr aligned) t us.val slots false σ).1) + {G' : MdbGhostA} (hS : StateRelA (utRtThA Z.th false nt us aligned win.val) (L.mem s') r.2) + (hC : CdtRelA st (utRtThA Z.th false nt us aligned win.val) s' r.2 G') + (P' : PlaceA (utRtThA Z.th false nt us aligned win.val) (L.ksb s)) (har : AR a' Z.hw) : + JAtZAX (L := L) AR + (utRtZA Z G' nt us aligned win.val ∅ ∅ (utBasesA aligned (zA nt.val us) win.val)) s' a' r.2 m := by + have hn : nt.val < 15 := (objTypeOfA_some htt us.val).1 + have z1 : nt.val ≠ 1 := by omega + have z14 : nt.val ≠ 14 := by omega + have Cl : UtClrA st Z H s aligned.val (aligned.val + win.val * 2 ^ (zA nt.val us).val) := + utRt_winClrA J hx hc h1 h2 + have anchor : ∀ j, j < win.val → specPtr (bjA aligned (zA nt.val us) j) ≠ Fl4SpecA64.idleScPtr := by + intro j hj + have v := utRt_newValA W hj + exact utRet_anchorA (st := st) J.idleAnchor hx hc (by omega) (by omega) + have memA : ∀ q, q ∈ fl4RetypeAddrs fl4ObjBitsApi (specPtr aligned) slots.length us.val t ↔ + UtNewSpA aligned (zA nt.val us) win.val q := fun q => by + rw [hlen, utRet_addrs_memA htt hus] + rfl + have thOff : ∀ q k, Z.th q = some k → ¬ UtNewSpA aligned (zA nt.val us) win.val (specPtr q) := fun q k hq => + utRt_relOffA (m := m) J Cl W rfl rfl anchor (Or.inl ⟨q, by rw [hq]; simp, rfl⟩) + -- the spec outcome + obtain ⟨qS, eqS, hqS⟩ := hsrcS + obtain ⟨csS, hkS, -, hsS⟩ := J.j.1.st.cnode qS _ hqS + rw [← eqS] at hkS + obtain ⟨capS, hcapS, -⟩ := hsS srcSlot.2 rfl + have hdc : ∀ d ∈ slots, (∃ sz cn, σ.kheap d.1 = some (.cnode sz cn)) ∧ + d.1 ∉ fl4RetypeAddrs fl4ObjBitsApi (specPtr aligned) slots.length us.val t := by + intro d hd + obtain ⟨q, eq, hq⟩ := hdst d hd + obtain ⟨cs, hk, -⟩ := J.j.1.st.cnode q _ hq + rw [eq, memA] + exact ⟨⟨_, cs, hk⟩, thOff q _ hq⟩ + have hSa : srcSlot.1 ∉ fl4RetypeAddrs fl4ObjBitsApi (specPtr aligned) slots.length us.val t := by + rw [eqS, memA] + exact thOff qS _ hqS + obtain ⟨-, hCn, hNew, hK, -, -, -⟩ := utRtBody_invA hr hkS hcapS hSa hdc + -- an old object that is no CNode is kept + have keep : ∀ q, t = .untyped ∨ ¬ UtNewSpA aligned (zA nt.val us) win.val q → + ∀ ko, (∀ sz cn, ko ≠ .cnode sz cn) → ∀ u : Cap, + let σa := setObjA σ srcSlot.1 (.cnode srcSlot.2.length (Function.update csS srcSlot.2 (some u))) + σa.kheap q = some ko → σ.kheap q = some ko := by + intro q _ ko ne u σa h + by_cases e : q = srcSlot.1 + · have : σa.kheap q = some (.cnode srcSlot.2.length (Function.update csS srcSlot.2 (some u))) := by + rw [e]; exact Function.update_self .. + rw [this] at h + exact absurd (Option.some.inj h).symm (ne _ _) + · exact (Function.update_of_ne e ..).symm.trans h + have hkeep : ∀ q, ¬ UtNewSpA aligned (zA nt.val us) win.val q → (∀ sz cn, σ.kheap q ≠ some (.cnode sz cn)) → + r.2.kheap q = σ.kheap q := by + intro q hq nc + have e : q ≠ srcSlot.1 := fun e => nc _ _ (e ▸ hkS) + have ea : ∀ u : Cap, (setObjA σ srcSlot.1 (.cnode srcSlot.2.length (Function.update csS srcSlot.2 (some u)))).kheap q = + σ.kheap q := fun u => Function.update_of_ne e .. + rw [hK q (Or.inr (fun h => hq ((memA q).1 h))) (fun sz cn => by rw [ea]; exact nc sz cn), ea] + have newObj : t ≠ .untyped → ∀ j, j < win.val → + r.2.kheap (specPtr (bjA aligned (zA nt.val us) j)) = some (defaultObject t false us.val σ.curDomain) := + fun ht j hj => hNew ht _ ((memA _).2 ⟨j, hj, rfl⟩) + have back : ∀ q ko, (∀ sz cn, ko ≠ .cnode sz cn) → r.2.kheap q = some ko → σ.kheap q = some ko ∨ + (t ≠ .untyped ∧ UtNewSpA aligned (zA nt.val us) win.val q ∧ ko = defaultObject t false us.val σ.curDomain) := by + intro q ko ne h + by_cases g : t = .untyped ∨ ¬ UtNewSpA aligned (zA nt.val us) win.val q + · have g' : t = .untyped ∨ q ∉ fl4RetypeAddrs fl4ObjBitsApi (specPtr aligned) slots.length us.val t := + g.imp id (fun k m' => k ((memA q).1 m')) + obtain ⟨u0, eu⟩ : ∃ u0 : Cap, u0 = .untypedCap false base (bitsOf capS) (unat (specPtr aligned + + BitVec.ofNat 64 slots.length <<< fl4ObjBitsApi t us.val - base)) := ⟨_, rfl⟩ + refine Or.inl (keep q g ko ne u0 ?_) + rw [eu] + by_cases cn : ∃ sz c, (setObjA σ srcSlot.1 (.cnode srcSlot.2.length (Function.update csS srcSlot.2 (some + (.untypedCap false base (bitsOf capS) (unat (specPtr aligned + + BitVec.ofNat 64 slots.length <<< fl4ObjBitsApi t us.val - base))))))).kheap q = some (.cnode sz c) + · obtain ⟨sz, c, k⟩ := cn + rw [(hCn q sz c g' k).1] at h + exact absurd (Option.some.inj h).symm (ne _ _) + · rw [hK q g' (fun sz c k => cn ⟨sz, c, k⟩)] at h + exact h + · have gt : t ≠ .untyped := fun e => g (Or.inl e) + have gq : UtNewSpA aligned (zA nt.val us) win.val q := by + by_contra k + exact g (Or.inr k) + refine Or.inr ⟨gt, gq, ?_⟩ + rw [hNew gt q ((memA q).2 gq)] at h + exact (Option.some.inj h).symm + have hcap : ∀ y, IsSlotA (utRtThA Z.th false nt us aligned win.val) y → + UtCapAfterA Z.th (L.mem s) (L.mem s') nt us aligned win.val y := by + intro y hy + by_cases old : IsSlotA Z.th y + · by_cases ex : y = x + · obtain ⟨nfi, e⟩ := Ec.src + exact Or.inr (Or.inr (Or.inl ⟨p, sb, nfi, false, by rw [ex]; exact e⟩)) + · by_cases ed : ∃ j < win.val, y = djA dest off j + · obtain ⟨j, hj, e⟩ := ed + exact Or.inr (Or.inl ⟨j, hj, by rw [e]; exact Ec.new j hj⟩) + · exact Or.inl ⟨old, Ec.keep y (hsub y old) ex (fun j hj e => ed ⟨j, hj, e⟩)⟩ + · refine Or.inr (Or.inr (Or.inr ?_)) + obtain ⟨q, h⟩ := hy + have isNew : ∀ k, utRtThA Z.th false nt us aligned win.val q = some k → + (∀ k', Z.th q = some k' → k' ≠ k) → UtNewA false nt us aligned win.val q ∧ k = utKindA nt us := by + intro k hk ne + unfold utRtThA at hk + split_ifs at hk with g + · exact ⟨g, (Option.some.inj hk).symm⟩ + · exact absurd rfl (ne k hk) + have ko : utKindA nt us = .other (2 ^ (zA nt.val us).val) := by + unfold utKindA + rw [ite_eq_right z1, ite_eq_right (by omega)] + rcases h with ⟨rd, hq, i, hi, e⟩ | ⟨hq, i, hi, e⟩ + · have g := isNew _ hq (fun k' hk' ek => old ⟨q, Or.inl ⟨rd, by rw [hk', ek], i, hi, e⟩⟩) + have ek := g.2 + rw [ko] at ek + cases ek + · have g := isNew _ hq (fun k' hk' ek => old ⟨q, Or.inr ⟨by rw [hk', ek], i, hi, e⟩⟩) + have ek := g.2 + rw [ko] at ek + cases ek + have et : t = .archObject .vcpuObj := by + rw [n12] at htt + simpa [objTypeOfA, archDataToObjType, eq_comm] using htt + rw [← utRtMA_empty m] + refine utRt_postA (Tn := ∅) (Cn := ∅) (Vn := utBasesA aligned (zA nt.val us) win.val) J hx hc W h1 h2 hn + (fun h => by omega) w hS hC P' har (utRtBody_globA hr) hkeep (fun q => ⟨fun h => h.elim, fun h => absurd h.2 z1⟩) + (fun c h => h.elim) (fun v h => h) (fun _ v h => h) hcap (fun q h => h.elim) (fun c h => h.elim) + (fun v h => ?_) (fun q h => ?_) (fun q h => ?_) (fun y h => absurd h.1 z14) (fun q y h => ?_) + (fun q es h => ?_) (fun q l1 h => ?_) + · obtain ⟨j, hj, rfl⟩ := h + have o := newObj (by rw [et]; simp) j hj + rw [et] at o + have z0 := (VW j hj).1 0#u64 (Or.inl rfl) + have e0 : wa (bjA aligned (zA nt.val us) j) 0#u64 = bjA aligned (zA nt.val us) j := by + apply UScalar.eq_of_val_eq + rw [wa_valP] + have := (bjA aligned (zA nt.val us) j).hBounds + simp at this ⊢ + rw [e0] at z0 + exact ⟨z0, (VW j hj).1 24#u64 (Or.inr rfl), _, o, rfl, rfl⟩ + · exact absurd h.1 (by omega) + · exact absurd h.1 (by omega) + · rcases back q _ (fun _ _ e => by cases e) h with k | ⟨-, gq, -⟩ + · exact Or.inl k + · obtain ⟨j, hj, e⟩ := gq + exact Or.inr ⟨_, ⟨j, hj, rfl⟩, e.symm⟩ + · rcases back q _ (fun _ _ e => by cases e) h with k | ⟨-, -, e⟩ + · exact Or.inl k + · rw [et] at e + cases e + · rcases back q _ (fun _ _ e => by cases e) h with k | ⟨-, -, e⟩ + · exact Or.inl k + · rw [et] at e + cases e + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtRtVcpuWordsA.lean b/proofs/Fl4ProofsA64/P3/UtRtVcpuWordsA.lean new file mode 100644 index 000000000..208681f7b --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtRtVcpuWordsA.lean @@ -0,0 +1,291 @@ +/- +P3 (aarch64, A64-P3 Untyped W5): **a Retype's new VCPUs have no thread and no run caller**. + +* `ut_createVcpuObjA`: `create_object` of a VCPU on a zeroed page writes only in the page and leaves the `TCB` and + `RUN_CALLER` words zero. +* **`utRetype_vcpuWordsA`**: after a non-reset RAM Retype of VCPUs from `CrtP`, those two words of every new VCPU are + zero, and its snapshot words are those of a new VCPU (`UtVcRegsW`) (`utRetype_frameA`'s creation loop with the + words carried). + +Named premises: [P] `hP`, the platform's VCPU registers (`ut_freshVcpuA`), at every arch state. New theorems only. +-/ +import Fl4ProofsA64.P3.UtRtFrameA +import Fl4ProofsA64.P3.UtRetVcpuA +import Fl4ProofsA64.P3.UtVcpuA +import Fl4ProofsA64.P3.Stage3A + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +set_option maxRecDepth 100000 + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + +/-- The snapshot words of a new VCPU (record edit #6, `VcpuStateSnapA`): `VPPI_MASKED`, `RUN_OFFSET` and +`RUN_OUT_MASK` are zero, and register word `i` is register `i` of `fl4_vcpu_init default_vcpu`. -/ +def UtVcRegsW (m : U64 → U64) (b : U64) : Prop := + (∀ K : U64, K = 16#u64 ∨ K = 32#u64 ∨ K = 40#u64 ∨ K = 8#u64 → m (wa b K) = 0#u64) ∧ + ∀ i < 64, (m (wa b ⟨BitVec.ofNat 64 (64 + 8 * i)⟩)).bv = fl4VcpuRegGet (fl4VcpuInit defaultVcpu) i + +theorem UtVcRegsW.congr {m m' : U64 → U64} {b : U64} + (h : ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → m' (wa b o) = m (wa b o)) (r : UtVcRegsW m b) : + UtVcRegsW m' b := by + refine ⟨fun K hK => ?_, fun i hi => ?_⟩ + · have k : K.val < 4096 ∧ K.val % 8 = 0 := by rcases hK with rfl | rfl | rfl | rfl <;> simp + rw [h K k.1 k.2]; exact r.1 K hK + · obtain ⟨o1, o2⟩ := snapOffA i hi + rw [h _ o1 o2]; exact r.2 i hi + +/-- The platform's VCPU registers, as `ut_freshVcpuA` asks them. -/ +def UtVcpuPlatA (ar : machine.Arch A) (a : A) : Prop := + ar.vcpu_midr a = ok ⟨fl4A64Platform.vcpuMidr⟩ ∧ ar.vcpu_vgic_vtr a = ok ⟨fl4A64Platform.vgicVtr⟩ ∧ + ∃ g : U64, ar.vcpu_gic_version a = ok g ∧ decide (g = 3#u64) = config_ARM_GIC_V3 + +theorem ut_createVcpuObjA (s : S) (a : A) (nt base us : U64) (dev : Bool) (hn : nt.val = 12) + (hal : base.val % 4096 = 0) (hf : base.val + 4096 ≤ 2 ^ 64) + (hz : ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → L.mem s (wa base o) = 0#u64) (hP : UtVcpuPlatA ar a) : + objects.create_object st ar s a nt base us dev ⦃ r => r.1 = .Vcpu base ∧ r.2.2 = a ∧ + WritesIn st s r.2.1 (W7.R.ST.InWin base 4096) ∧ L.mem r.2.1 (wa base 0#u64) = 0#u64 ∧ + L.mem r.2.1 (wa base 24#u64) = 0#u64 ∧ UtVcRegsW (L.mem r.2.1) base ⦄ := by + have e : nt = 12#u64 := UScalar.eq_of_val_eq (by rw [hn]; rfl) + subst e + have hal8 : Aligned base := by show _ % 8 = 0; omega + unfold objects.create_object objects.create_arch_object + simp only [fl4_abi.consts.object.Tcb, fl4_abi.consts.object.Endpoint, fl4_abi.consts.object.Notification, + fl4_abi.consts.object.CapTable, fl4_abi.consts.object.Untyped, fl4_abi.consts.object.SchedContext, + fl4_abi.consts.object.Reply, fl4_abi.consts.object.ArmPageTable, fl4_abi.consts.object.ArmVSpace, + fl4_abi.consts.object.ArmVcpu, show ¬ (12#u64 : U64) = 1#u64 by decide, show ¬ (12#u64 : U64) = 2#u64 by decide, + show ¬ (12#u64 : U64) = 3#u64 by decide, show ¬ (12#u64 : U64) = 4#u64 by decide, + show ¬ (12#u64 : U64) = 0#u64 by decide, show ¬ (12#u64 : U64) = 5#u64 by decide, + show ¬ (12#u64 : U64) = 6#u64 by decide, show ¬ (12#u64 : U64) = 11#u64 by decide, + show ¬ (12#u64 : U64) = 8#u64 by decide, ↓reduceIte] + refine WP.spec_bind (ut_freshVcpuA ar s a base hal8 hf hz hP) fun s' h => ?_ + obtain ⟨w, hh, hregs⟩ := h + have wp : WritesIn st s s' (W7.R.ST.InWin base 4096) := by + refine ⟨w.1, w.2.1, fun b hb hnb => w.2.2 b hb (fun h => ?_)⟩ + obtain ⟨o, o1, -, eb⟩ := h + have ev : b.val = base.val + o.val := by + rw [eb, wa_valP]; exact Nat.mod_eq_of_lt (by omega) + refine hnb ?_ + unfold W7.R.ST.InWin + omega + refine (WP.spec_ok _).2 ⟨rfl, rfl, wp, ?_, ?_, ⟨fun K hK => ?_, hregs⟩⟩ + · exact hh 0 (Or.inl rfl) + · exact hh 24 (Or.inr (Or.inr (Or.inr (Or.inl rfl)))) + · rcases hK with rfl | rfl | rfl | rfl + · exact hh 16 (Or.inr (Or.inr (Or.inl rfl))) + · exact hh 32 (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl rfl))))) + · exact hh 40 (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr rfl))))) + · exact hh 8 (Or.inr (Or.inl rfl)) + +/-- Every window's `TCB` and `RUN_CALLER` words are zero, and its snapshot words are a new VCPU's. -/ +def UtVcpuZW (st : machine.Store S) [L : LawfulStore st] (aligned z : U64) (len : Nat) (s : S) : Prop := + ∀ l, l < len → (∀ K : U64, K = 0#u64 ∨ K = 24#u64 → L.mem s (wa (bjA aligned z l) K) = 0#u64) ∧ + UtVcRegsW (L.mem s) (bjA aligned z l) + +set_option maxHeartbeats 4000000 in +/-- **A new VCPU's thread and run-caller words are zero** (module docstring). -/ +theorem utRetype_vcpuWordsA (hA : P0.ArchTotal ar) {H1 : Heap} {Cs Sg OS1 : Set U64} {s1 : S} + {slot p sb fi1 : U64} {aligned nt us dest off win : U64} + (C : CrtP st H1 Cs Sg OS1 s1 slot p sb fi1 false aligned nt us dest off win) {G : List (List U64)} + {par : U64 → Option U64} (hT : TreeA st OS1 G par s1) (hn1 : nt.val = 12) (hal4 : aligned.val % 4096 = 0) + (hP : ∀ a : A, UtVcpuPlatA ar a) (a1 : A) : + untyped.retype st ar s1 a1 slot false aligned nt us dest off win false ⦃ r => + UtVcpuZW st aligned (zA nt.val us) win.val r.2.1 ⦄ := by + obtain ⟨-, hal, hfi, hA0, hAw, hinj, hcan, -⟩ := C.ar + obtain ⟨hSA, -, hK1⟩ := C.stz + obtain ⟨-, -, hV1, hUZ1, -⟩ := hSA + have hnA := nodeAt_aligned C.n0 (by rw [debug_val]; omega) (by rw [debug_val]) + have hnF := KsOffA.node hK1 C.core0 C.n0 (by rw [debug_val]; omega) + have hM := hT.lk + obtain ⟨-, -, hpf⟩ := ut_rangeA (capA_ut_b C.cap) (by + have := hT.ut.reg slot C.src (by rw [C.cap]; exact physA_ut _ _ _ _); rwa [C.cap] at this) + have hs0 : capA st s1 slot ≠ .Null := by rw [C.cap]; exact fun h => by cases h + have hsn : ∀ j, j < win.val → slot ≠ djA dest off j := fun j hj e => hs0 (by rw [e]; exact C.dn j hj) + have hz12 : (zA nt.val us).val = 12 := by + rw [zA_valA nt us C.hn C.hus, hn1] + rfl + have hX : 2 ^ (zA nt.val us).val = 4096 := by rw [hz12]; norm_num + have hfit : aligned.val + win.val * 2 ^ (zA nt.val us).val ≤ 2 ^ 64 := by omega + have hwf : ∀ j, j < win.val → CapRt.EncWf (newCapA nt.val (bjA aligned (zA nt.val us) j) us false) := fun j hj => + newCapA_encWf _ _ _ _ (hcan j hj) C.hus + have hdn1 : ∀ s', RtSrc st s1 slot p sb false aligned.val (aligned.val + win.val * 2 ^ (zA nt.val us).val) s' → + ∀ j, j < win.val → capA st s' (djA dest off j) = .Null := fun s' h1 j hj => by + obtain ⟨-, -, -, hc1, -, -⟩ := h1.facts C.os C.src + rw [hc1 _ (C.dO j hj) (fun e => hsn j hj e.symm)]; exact C.dn j hj + have hbj : ∀ l, l < win.val → (bjA aligned (zA nt.val us) l).val = aligned.val + l * 4096 ∧ + aligned.val + l * 4096 + 4096 ≤ p.val + 2 ^ sb.val ∧ p.val + fi1.val ≤ aligned.val + l * 4096 := by + intro l hl + have h1 : (l + 1) * 4096 ≤ win.val * 4096 := Nat.mul_le_mul_right _ hl + have hAw' := hAw + rw [hX] at hAw' + have hfit' := hfit + rw [hX] at hfit' + have := bjA_val (base := aligned) (z := zA nt.val us) (j := l) (by rw [hX]; omega) + rw [hX] at this + exact ⟨this, by omega, by omega⟩ + have hw2 : ∀ l, l < win.val → ∀ K : U64, K.val < 4096 ∧ K.val % 8 = 0 → + (wa (bjA aligned (zA nt.val us) l) K).val = aligned.val + l * 4096 + K.val ∧ K.val < 4096 ∧ K.val % 8 = 0 := by + intro l hl K k + have := (hbj l hl).2.1 + have e : (wa (bjA aligned (zA nt.val us) l) K).val = (bjA aligned (zA nt.val us) l).val + K.val := by + rw [wa_valP] + refine Nat.mod_eq_of_lt ?_ + have := hbj l hl + have := hfit + have h1 : (l + 1) * 4096 ≤ win.val * 4096 := Nat.mul_le_mul_right _ hl + rw [hX] at this + omega + exact ⟨by rw [e, (hbj l hl).1], k⟩ + -- a write of `OS` slot words keeps the carried words + have inOS : ∀ (l : Nat), l < win.val → ∀ K : U64, K.val < 4096 ∧ K.val % 8 = 0 → + Aligned (wa (bjA aligned (zA nt.val us) l) K) ∧ ¬ InOS OS1 (wa (bjA aligned (zA nt.val us) l) K) := by + intro l hl K hK + obtain ⟨e, k1, k2⟩ := hw2 l hl K hK + have al4 : aligned.val % 8 = 0 := by omega + refine ⟨by show _ % 8 = 0; rw [e]; omega, fun ⟨y, hy, g1, g2⟩ => ?_⟩ + have := (hbj l hl).2.1 + have := (hbj l hl).2.2 + have nw : ¬ W7.CapW H1.tcbs H1.omega Sg (wa (bjA aligned (zA nt.val us) l) K) := + in_free_not_capw C.heap hV1 C.src C.cap (by rw [e]; omega) (by rw [e]; omega) + have al0 := C.os.al0 hy + unfold Aligned at al0 + obtain ⟨d, hd⟩ : ∃ d : U64, d.val = (wa (bjA aligned (zA nt.val us) l) K).val - y.val := + ⟨⟨BitVec.ofNat 64 ((wa (bjA aligned (zA nt.val us) l) K).val - y.val)⟩, by + show (BitVec.ofNat 64 _).toNat = _ + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega)⟩ + have d8 : d.val % 8 = 0 := by + rw [e] at g1 hd + omega + have ey : wa y d = wa (bjA aligned (zA nt.val us) l) K := by + apply UScalar.eq_of_val_eq + rw [wa_valP, hd] + rw [Nat.mod_eq_of_lt (by have := (wa (bjA aligned (zA nt.val us) l) K).hBounds; simp at this; omega)] + omega + exact nw (ey ▸ C.os.cw hy (k := d) (by rw [hd]; omega) d8) + have k0 : ∀ K : U64, K = 0#u64 ∨ K = 24#u64 → K.val < 4096 ∧ K.val % 8 = 0 := fun K hK => by + rcases hK with rfl | rfl <;> simp + -- the first `i` windows hold new VCPUs; the other windows' two words are still zero + let V : S → Nat → Prop := fun s' i => + (∀ l, l < win.val → ∀ K : U64, K = 0#u64 ∨ K = 24#u64 → L.mem s' (wa (bjA aligned (zA nt.val us) l) K) = 0#u64) ∧ + ∀ l, l < i → l < win.val → UtVcRegsW (L.mem s') (bjA aligned (zA nt.val us) l) + have keepOS : ∀ (s s' : S) (i : Nat), WritesIn st s s' (InOS OS1) → V s i → V s' i := by + intro s s' i w h + have kp : ∀ l, l < win.val → ∀ K : U64, K.val < 4096 ∧ K.val % 8 = 0 → + L.mem s' (wa (bjA aligned (zA nt.val us) l) K) = L.mem s (wa (bjA aligned (zA nt.val us) l) K) := by + intro l hl K hK + obtain ⟨a1, a2⟩ := inOS l hl K hK + exact w.mem_eq a1 a2 + exact ⟨fun l hl K hK => by rw [kp l hl K (k0 K hK)]; exact h.1 l hl K hK, + fun l hi hl => (h.2 l hi hl).congr fun o o1 o8 => kp l hl o ⟨o1, o8⟩⟩ + refine WP.spec_mono (untyped.retype.genJK ar a1 C.cap nt us dest off win aligned false C.hn C.hz hfit + (J := fun s' s'' i => UtLoopA st OS1 G par s' slot (djA dest off) + (fun j => newCapA nt.val (bjA aligned (zA nt.val us) j) us false) s'' i ∧ + ZTc st H1 s1 aligned (zA nt.val us) win.val s'' i ∧ V s'' i) + (K := fun s' s'' i => UtLoopA st OS1 G par s' slot (djA dest off) + (fun j => newCapA nt.val (bjA aligned (zA nt.val us) j) us false) s'' i ∧ + ZTc st H1 s1 aligned (zA nt.val us) win.val s'' (i + 1) ∧ V s'' (i + 1)) + (fun _ _ s' a' j hj h => ?_) (fun s' h1 s'' i hi h => ?_) (fun s' h1 => ?_)) ?_ + · obtain ⟨hL, hZ, hVs⟩ := h + have P1s := create_object.hCOZ (ar := ar) hA C.heap C.os hV1 C.src C.cap nt us win aligned false C.hn + (fun h => by cases h) hal hA0 hAw hpf hnA C.nc (hnF slot C.src p sb fi1 C.cap) s' a' j hj hZ + obtain ⟨-, hzero, -⟩ := id hZ + have hb := hbj j hj + have al : (bjA aligned (zA nt.val us) j).val % 4096 = 0 := by rw [hb.1]; omega + have hf : (bjA aligned (zA nt.val us) j).val + 4096 ≤ 2 ^ 64 := by + have h1 : (j + 1) * 4096 ≤ win.val * 4096 := Nat.mul_le_mul_right _ hj + have := hfit + rw [hX] at this + rw [hb.1]; omega + have pz : ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → L.mem s' (wa (bjA aligned (zA nt.val us) j) o) = 0#u64 := by + intro o o1 o8 + have e : (wa (bjA aligned (zA nt.val us) j) o).val = aligned.val + j * 4096 + o.val := by + rw [wa_valP, hb.1] + refine Nat.mod_eq_of_lt ?_ + rw [hb.1] at hf + omega + have h1 : (j + 1) * 4096 ≤ win.val * 4096 := Nat.mul_le_mul_right _ hj + generalize wa (bjA aligned (zA nt.val us) j) o = w at e ⊢ + have al : Aligned w := by show _ % 8 = 0; omega + generalize 2 ^ (zA nt.val us).val = X at hzero hX + subst hX + have g := ut_inWinA aligned.val j win.val o.val hj o1 + rw [← e] at g + exact hzero w al g.1 g.2 + have M := ut_createVcpuObjA (st := st) (ar := ar) s' a' nt (bjA aligned (zA nt.val us) j) us false hn1 al hf pz + (hP a') + refine WP.spec_mono (spec_andA P1s M) ?_ + rintro r ⟨⟨hc, hk', hZ'⟩, -, -, hw, w0, w24, wr⟩ + have kp : ∀ l, l < win.val → l ≠ j → ∀ K : U64, K.val < 4096 ∧ K.val % 8 = 0 → + L.mem r.2.1 (wa (bjA aligned (zA nt.val us) l) K) = L.mem s' (wa (bjA aligned (zA nt.val us) l) K) := by + intro l hl e K hK + obtain ⟨ew, k1, -⟩ := hw2 l hl K hK + refine hw.mem_eq (inOS l hl K hK).1 ?_ + unfold W7.R.ST.InWin + rw [ew, hb.1] + rcases Nat.lt_or_ge l j with g | g + · have : (l + 1) * 4096 ≤ j * 4096 := Nat.mul_le_mul_right _ g + omega + · have : (j + 1) * 4096 ≤ l * 4096 := Nat.mul_le_mul_right _ (by omega) + omega + refine ⟨hc, hL.keep C.os (fun l hl => C.dO l (by omega)) hk', hZ', fun l hl K hK => ?_, fun l hi hl => ?_⟩ + · by_cases e : l = j + · subst e + rcases hK with rfl | rfl + · exact w0 + · exact w24 + · rw [kp l hl e K (k0 K hK)] + exact hVs.1 l hl K hK + · by_cases e : l = j + · subst e + exact wr + · exact (hVs.2 l (by omega) hl).congr fun o o1 o8 => kp l hl e o ⟨o1, o8⟩ + · -- one insert + have hnx : NxA st s'' slot ≠ 0#u64 → NxA st s'' slot ∈ OS1 := (MdbLk.nxG C.os h.1.1 h.1.2.2.1).2 + refine WP.spec_mono (WP.spec_and + (UtLoopA.step C.os hi h.1 C.dO (hdn1 s' h1) C.dc hinj hsn hwf (fun j _ => newCapA_ne _ _ _ _)) + (WP.spec_and + (cdt.insert_new_cap.zqW (Zq := fun s j => ZTc st H1 s1 aligned (zA nt.val us) win.val s j) C.os + (fun _ _ _ h w => ZTc.capw C.heap C.os hV1 C.src C.cap hA0 hAw hnA C.nc h w) + (C.dO i hi) (MdbLk.nxG C.os h.1.1 h.1.2.2.1) h.2.1) + (utInsert_inOSA C.os C.src (C.dO i hi) hnx))) ?_ + rintro r ⟨hl, hz, hw⟩ + exact ⟨hl, hz, keepOS _ _ _ hw h.2.2⟩ + · -- the source rewritten + obtain ⟨-, -, -, -, -, hW⟩ := h1.facts C.os C.src + have L0 := h1.lk C.os C.src hM hs0 + have hsG : slot ∈ G.flatten := (hM.members slot).2 ⟨C.src, hs0⟩ + have wS : WritesIn st s1 s' (InOS OS1) := hW.mono fun b hb => by + have f := C.os.fit slot C.src + refine ⟨slot, C.src, ?_, ?_⟩ <;> rcases hb with e | e + · rw [e] + · rw [e, wv f (by simp)]; simp + · rw [e]; omega + · rw [e, wv f (by simp)]; simp + have V0 : V s1 0 := by + refine ⟨fun l hl K hK => ?_, fun l hi => absurd hi (Nat.not_lt_zero l)⟩ + obtain ⟨ew, k1, -⟩ := hw2 l hl K (k0 K hK) + have := (hbj l hl).2.1 + have := (hbj l hl).2.2 + exact hUZ1 slot C.src p sb fi1 C.cap _ (inOS l hl K (k0 K hK)).1 (by rw [ew]; omega) (by rw [ew]; omega) + refine ⟨⟨L0, by rw [utPA_zero]; exact hT.tree, hsG, fun _ _ _ => rfl, + fun j hj => absurd hj (Nat.not_lt_zero j), fun y x => ?_, fun _ _ _ => rfl, + fun j hj => absurd hj (Nat.not_lt_zero j)⟩, + ZTc.start C.heap C.os hV1 C.src C.cap hA0 hAw hnA C.nc hUZ1 C.hd hW, keepOS _ _ _ wS V0⟩ + rw [utPA_zero] + exact ⟨Or.inl, fun h => h.elim id fun ⟨j, hj, _⟩ => absurd hj (Nat.not_lt_zero j)⟩ + rintro r ⟨-, s', -, -, -, hV⟩ + exact fun l hl => ⟨hV.1 l hl, hV.2 l hl hl⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtRtVsA.lean b/proofs/Fl4ProofsA64/P3/UtRtVsA.lean new file mode 100644 index 000000000..6bf88fc5c --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtRtVsA.lean @@ -0,0 +1,337 @@ +/- +P3 (aarch64, A64-P3 Untyped W5): **the VSpace and extension records after a retype's creation** (`UtVsGrowA` +instantiated at the new objects). + +A retype of window `[aligned, aligned + win·2^z)` adds its bases to one VSpace ghost set by type: a VSpace root +(type 8, depth 0) or page table (11, depth 1) to `Z.S1`, a Stage-2 root (13) to `Z.X`, a `HighVSpace` (14) to `Z.Y`. + +* `utRt_vsGrowA`: the growth facts (`UtVsGrowA`) from the old pages lying off the window. +* `utRt_vsRelA`, `utRt_extVsA`: `VsRelA` / `ExtVsRelA` at the grown sets. +* `utRt_pmlA`, `utRt_stage2CoreA`, `utRt_highCoreA`: `JAtZA.pml`, `Stage2CoreA`, `HighVSpaceCoreA`. + +Hypotheses: `hoff` (the old VSpace pages are off the window), the memory and spec facts of the creation (old words and +old spec objects kept; each new object's spec default; a new `HighVSpace`'s level-0 words), and the split of the caps +and spec objects into old and new. No named premise. New definitions and theorems only. +-/ +import Fl4ProofsA64.P3.UtVsGrowA +import Fl4ProofsA64.P3.JointA +import Fl4ProofsA64.P1.MdbTreeL + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +set_option maxRecDepth 100000 + +/-- The bases of a creation's window. -/ +def utBasesA (aligned z : U64) (win : Nat) : Set U64 := {p | ∃ j < win, p = bjA aligned z j} + +/-- The new Stage-1 roots. A retyped page table (type 11) is *not* in `Z.S1` until it is mapped +(record edit #8b-C item 15), so `ARMPageTable_Map` applies to it. -/ +def utS1NewA (nt aligned z : U64) (win : Nat) : Set U64 := + {p | nt.val = 8 ∧ p ∈ utBasesA aligned z win} + +/-- The new Stage-2 roots. -/ +def utXNewA (nt aligned z : U64) (win : Nat) : Set U64 := {p | nt.val = 13 ∧ p ∈ utBasesA aligned z win} + +/-- The new `HighVSpace`s. -/ +def utYNewA (nt aligned z : U64) (win : Nat) : Set U64 := {p | nt.val = 14 ∧ p ∈ utBasesA aligned z win} + +/-- The depths after the creation: a new root is at depth 0, a new page table at depth 1. -/ +def utLvNewA (Lv : U64 → Nat) (nt aligned z : U64) (win : Nat) : U64 → Nat := fun p => + if p ∈ utS1NewA nt aligned z win then (if nt.val = 8 then 0 else 1) else Lv p + +/-- A VSpace page of the old sets. -/ +def UtOldPgA (Pp S1 X Y T2 : Set U64) (q : U64) : Prop := + q ∈ Pp ∨ q ∈ S1 ∨ q ∈ T2 ∨ q ∈ Y ∨ (∃ y ∈ Y, q = wa y 4096#u64) ∨ q ∈ X ∨ ∃ r ∈ X, q = wa r 4096#u64 + +/-- The creation window's placement. -/ +structure UtWinZA (nt aligned z : U64) (win : Nat) : Prop where + z64 : z.val < 64 + fit : aligned.val + win * 2 ^ z.val ≤ 2 ^ 64 + al : aligned.val % 2 ^ z.val = 0 + pg : nt.val = 8 ∨ nt.val = 11 → 12 ≤ z.val + pg2 : nt.val = 13 ∨ nt.val = 14 → 13 ≤ z.val + +section +variable {nt aligned z : U64} {win : Nat} + +theorem utBase_inA (W : UtWinZA nt aligned z win) {b : U64} (h : b ∈ utBasesA aligned z win) : + aligned.val ≤ b.val ∧ b.val + 2 ^ z.val ≤ aligned.val + win * 2 ^ z.val ∧ b.val % 2 ^ z.val = 0 := by + obtain ⟨j, hj, rfl⟩ := h + have h1 : (j + 1) * 2 ^ z.val ≤ win * 2 ^ z.val := Nat.mul_le_mul_right _ hj + rw [Nat.add_mul, Nat.one_mul] at h1 + have h2 : 0 < 2 ^ z.val := Nat.two_pow_pos _ + have := W.fit + have e : (bjA aligned z j).val = aligned.val + j * 2 ^ z.val := bjA_val (by omega) + rw [e] + refine ⟨by omega, by omega, ?_⟩ + rw [Nat.add_mul_mod_self_right] + exact W.al + +/-- A base of a 8 KiB-or-larger type: 8 KiB-aligned, with its second page in the window. -/ +theorem utBase_bigA (W : UtWinZA nt aligned z win) (hnt : nt.val = 13 ∨ nt.val = 14) {b : U64} + (h : b ∈ utBasesA aligned z win) : + 8192 ∣ b.val ∧ (wa b 4096#u64).val = b.val + 4096 ∧ aligned.val ≤ b.val + 4096 ∧ + b.val + 4096 < aligned.val + win * 2 ^ z.val := by + obtain ⟨h1, h2, h3⟩ := utBase_inA W h + have h13 := W.pg2 hnt + have d : 8192 ∣ 2 ^ z.val := by + rw [show (8192 : Nat) = 2 ^ 13 by norm_num] + exact Nat.pow_dvd_pow 2 h13 + have hz : 8192 ≤ 2 ^ z.val := by + rw [show (8192 : Nat) = 2 ^ 13 by norm_num] + exact Nat.pow_le_pow_right (by decide) h13 + have := W.fit + have e : (wa b 4096#u64).val = b.val + 4096 := by + rw [wa_valP, show (4096#u64 : U64).val = 4096 from rfl] + exact Nat.mod_eq_of_lt (by omega) + exact ⟨d.trans (Nat.dvd_of_mod_eq_zero h3), e, by omega, by omega⟩ + +variable {Pp S1 X Y T2 : Set U64} {Lv : U64 → Nat} {Sh2 : U64 → Nat} {mem mem' : U64 → U64} {ksb : U64} + {σ σ' : Fl4SpecA64.State Unit} + +/-- **The growth facts of a creation** (module docstring). -/ +theorem utRt_vsGrowA (W : UtWinZA nt aligned z win) + (V : VsRelA Pp S1 Lv X Y mem ksb σ) (E : ExtVsRelA Pp S1 X Y T2 Sh2 mem σ) + (hoff : ∀ q, UtOldPgA Pp S1 X Y T2 q → q.val < aligned.val ∨ aligned.val + win * 2 ^ z.val ≤ q.val) + (hm : ∀ w, VsFootA Pp S1 Y ksb w → mem' w = mem w) (he : ∀ w, ExtFootE X Y T2 w → mem' w = mem w) + (htab : σ'.archState.armAsidTable = σ.archState.armAsidTable) + (hk : ∀ q, q ∈ Pp ∨ q ∈ S1 ∨ q ∈ Y ∨ q ∈ X ∨ q ∈ T2 → σ'.kheap (specPtr q) = σ.kheap (specPtr q)) + (hpt : ∀ p ∈ utS1NewA nt aligned z win, ∃ t, σ'.kheap (specPtr p) = some (.archObj (.pageTable t)) ∧ + Fl4SpecA64.ptType t = ptTyA (if nt.val = 8 then 0 else 1)) + (hs2 : ∀ r ∈ utXNewA nt aligned z win, ∃ es, σ'.kheap (specPtr r) = some (.archObj (.ext (.stage2Root es)))) + (hhv : ∀ y ∈ utYNewA nt aligned z win, (∃ l1, σ'.kheap (specPtr y) = some (.archObj (.ext (.highVSpace l1)))) ∧ + (dBitA (mem' (peAF y 511#u64)) 0 = true ∧ dBitA (mem' (peAF y 511#u64)) 1 = true ∧ + dTabA (mem' (peAF y 511#u64)) = wa y 4096#u64) ∧ + ∀ i : U64, i.val < 256 → mem' (peAF y i) = 0#u64) : + UtVsGrowA Pp S1 (S1 ∪ utS1NewA nt aligned z win) X (X ∪ utXNewA nt aligned z win) Y + (Y ∪ utYNewA nt aligned z win) T2 Lv (utLvNewA Lv nt aligned z win) mem mem' ksb σ σ' := by + -- an old page is no new base and no second page of one + have notB : ∀ q, UtOldPgA Pp S1 X Y T2 q → q ∉ utBasesA aligned z win := by + intro q hq hb + obtain ⟨h1, h2, -⟩ := utBase_inA W hb + have := Nat.two_pow_pos z.val + rcases hoff q hq with g | g <;> omega + have notB2 : ∀ q, UtOldPgA Pp S1 X Y T2 q → ∀ b ∈ utBasesA aligned z win, nt.val = 13 ∨ nt.val = 14 → + q ≠ wa b 4096#u64 := by + intro q hq b hb hnt e + obtain ⟨-, e', h1, h2⟩ := utBase_bigA W hnt hb + have := congrArg UScalar.val e + rcases hoff q hq with g | g <;> omega + have oPp : ∀ q ∈ Pp, UtOldPgA Pp S1 X Y T2 q := fun q h => Or.inl h + have oS1 : ∀ q ∈ S1, UtOldPgA Pp S1 X Y T2 q := fun q h => Or.inr (Or.inl h) + have oT2 : ∀ q ∈ T2, UtOldPgA Pp S1 X Y T2 q := fun q h => Or.inr (Or.inr (Or.inl h)) + have oY : ∀ q ∈ Y, UtOldPgA Pp S1 X Y T2 q := fun q h => Or.inr (Or.inr (Or.inr (Or.inl h))) + have oY2 : ∀ y ∈ Y, UtOldPgA Pp S1 X Y T2 (wa y 4096#u64) := fun y h => + Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨y, h, rfl⟩)))) + have oS2 : ∀ q ∈ S2PgE X T2, UtOldPgA Pp S1 X Y T2 q := by + rintro q (h | ⟨r, hr, rfl | rfl⟩) + · exact oT2 q h + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl hr))))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ⟨r, hr, rfl⟩))))) + have sn : ∀ q, UtOldPgA Pp S1 X Y T2 q → q ∉ utS1NewA nt aligned z win := fun q h g => notB q h g.2 + have xn : ∀ q, UtOldPgA Pp S1 X Y T2 q → q ∉ utXNewA nt aligned z win := fun q h g => notB q h g.2 + have yn : ∀ q, UtOldPgA Pp S1 X Y T2 q → q ∉ utYNewA nt aligned z win := fun q h g => notB q h g.2 + -- a word in the window is no old page + have win_notOld : ∀ q : U64, aligned.val ≤ q.val → q.val < aligned.val + win * 2 ^ z.val → + ¬ UtOldPgA Pp S1 X Y T2 q := fun q h1 h2 h => by + rcases hoff q h with g | g <;> omega + have baseW : ∀ b ∈ utBasesA aligned z win, ¬ UtOldPgA Pp S1 X Y T2 b := fun b hb h => notB b h hb + refine + { tb := Set.subset_union_left + xs := Set.subset_union_left + ys := Set.subset_union_left + lv := fun p hp => ite_eq_right (sn p (oS1 p hp)) + kmem := hm + kext := he + ktab := htab + kheap := hk + newT := fun p hp hnp => ?_ + newY := fun y hy hny => ?_ + newX := fun r hr hnr => ?_ + ytb := fun y hy => ?_ + pgd := fun q hq => ?_ + t2x := fun t ht r hr => ?_ + ypg := fun y hy => ?_ + ypp := fun y hy => ?_ } + · have hn : p ∈ utS1NewA nt aligned z win := hp.resolve_left hnp + obtain ⟨-, -, hal⟩ := utBase_inA W hn.2 + have d : 4096 ∣ 2 ^ z.val := by + rw [show (4096 : Nat) = 2 ^ 12 by norm_num] + exact Nat.pow_dvd_pow 2 (W.pg (Or.inl hn.1)) + have e : utLvNewA Lv nt aligned z win p = if nt.val = 8 then 0 else 1 := ite_eq_left hn + refine ⟨fun y hy e' => baseW p hn.2 (by rw [e']; exact oY2 y hy), by rw [e]; split <;> omega, + d.trans (Nat.dvd_of_mod_eq_zero hal), ?_⟩ + rw [e] + exact hpt p hn + · have hn : y ∈ utYNewA nt aligned z win := hy.resolve_left hny + exact ⟨(utBase_bigA W (Or.inr hn.1) hn.2).1, hhv y hn⟩ + · have hn : r ∈ utXNewA nt aligned z win := hr.resolve_left hnr + exact ⟨(utBase_bigA W (Or.inl hn.1) hn.2).1, hs2 r hn⟩ + · rcases hy with hy | hy + · rintro (g | g) + · exact V.ytb y hy g + · exact sn _ (oY2 y hy) g + · obtain ⟨-, e, h1, h2⟩ := utBase_bigA W (Or.inr hy.1) hy.2 + rintro (g | g) + · exact win_notOld _ (by rw [e]; exact h1) (by rw [e]; exact h2) (oS1 _ g) + · have g' := g.1; rw [hy.1] at g'; omega + · have old : q ∈ S2PgE X T2 → q ∉ VsPtA Pp (S1 ∪ utS1NewA nt aligned z win) (Y ∪ utYNewA nt aligned z win) ∧ + q ∉ Y ∪ utYNewA nt aligned z win := by + intro g + have o := oS2 q g + obtain ⟨g1, g2⟩ := E.pgd q g + have nY : q ∉ Y ∪ utYNewA nt aligned z win := by + rintro (h | h) + · exact g2 h + · exact yn q o h + refine ⟨?_, nY⟩ + rintro (h | (h | h) | ⟨y, (hy | hy), e⟩) + · exact g1 (Or.inl h) + · exact g1 (Or.inr (Or.inl h)) + · exact sn q o h + · exact g1 (Or.inr (Or.inr ⟨y, hy, e⟩)) + · exact notB2 q o y hy.2 (Or.inr hy.1) e + have new : ∀ r ∈ utXNewA nt aligned z win, q = r ∨ q = wa r 4096#u64 → + q ∉ VsPtA Pp (S1 ∪ utS1NewA nt aligned z win) (Y ∪ utYNewA nt aligned z win) ∧ + q ∉ Y ∪ utYNewA nt aligned z win := by + intro r hr e + have nw : ¬ UtOldPgA Pp S1 X Y T2 q := by + rcases e with rfl | rfl + · exact baseW _ hr.2 + · obtain ⟨-, e, h1, h2⟩ := utBase_bigA W (Or.inl hr.1) hr.2 + exact win_notOld _ (by rw [e]; exact h1) (by rw [e]; exact h2) + have noS : q ∉ utS1NewA nt aligned z win := fun g => by + have g' := g.1; rw [hr.1] at g'; omega + have noY : q ∉ utYNewA nt aligned z win := fun g => by + have := g.1; rw [hr.1] at this; omega + refine ⟨?_, ?_⟩ + · rintro (h | (h | h) | ⟨y, (hy | hy), e'⟩) + · exact nw (oPp q h) + · exact nw (oS1 q h) + · exact noS h + · exact nw (by rw [e']; exact oY2 y hy) + · have := hy.1; rw [hr.1] at this; omega + · rintro (h | h) + · exact nw (oY q h) + · exact noY h + rcases hq with h | ⟨r, hr | hr, e⟩ + · exact old (Or.inl h) + · exact old (Or.inr ⟨r, hr, e⟩) + · exact new r hr e + · rcases hr with hr | hr + · exact E.t2x t ht r hr + · obtain ⟨-, e, h1, h2⟩ := utBase_bigA W (Or.inl hr.1) hr.2 + exact ⟨fun g => baseW r hr.2 (by rw [← g]; exact oT2 t ht), fun g => win_notOld _ (by rw [e]; exact h1) (by rw [e]; exact h2) (by rw [← g]; exact oT2 t ht)⟩ + · rcases hy with hy | hy + · rintro (h | (h | h) | ⟨y', (hy' | hy'), e⟩) + · exact E.ypg y hy (Or.inl h) + · exact E.ypg y hy (Or.inr (Or.inl h)) + · exact sn y (oY y hy) h + · exact E.ypg y hy (Or.inr (Or.inr ⟨y', hy', e⟩)) + · exact notB2 y (oY y hy) y' hy'.2 (Or.inr hy'.1) e + · rintro (h | (h | h) | ⟨y', (hy' | hy'), e⟩) + · exact baseW y hy.2 (oPp y h) + · exact baseW y hy.2 (oS1 y h) + · have g := h.1; rw [hy.1] at g; omega + · exact baseW y hy.2 (by rw [e]; exact oY2 y' hy') + · have a := (utBase_bigA W (Or.inr hy.1) hy.2).1 + obtain ⟨b, e', -, -⟩ := utBase_bigA W (Or.inr hy'.1) hy'.2 + have := congrArg UScalar.val e + omega + · rcases hy with hy | hy + · exact E.ypp y hy + · obtain ⟨-, e, h1, h2⟩ := utBase_bigA W (Or.inr hy.1) hy.2 + exact fun g => win_notOld _ (by rw [e]; exact h1) (by rw [e]; exact h2) (oPp _ g) + +end + +/-! ## The extension records -/ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {nt aligned z : U64} {win : Nat} + +/-- **`JAtZA.pml` after the creation**: a root cap is old, or a new VSpace root's (type 8). `hcap`: the root caps of +the new slots. -/ +theorem utRt_pmlA {Z Z' : XSetsA} {s s' : S} + (old : ∀ y, IsSlotA Z.th y → ∀ p mp asid, capAtA (L.mem s) y = .Pml4 p mp asid → p ∈ Z.S1 ∧ Z.Lv p = 0) + (e1 : Z'.S1 = Z.S1 ∪ utS1NewA nt aligned z win) (e2 : Z'.Lv = utLvNewA Z.Lv nt aligned z win) + (hfresh : ∀ p ∈ Z.S1, p ∉ utS1NewA nt aligned z win) + (hcap : ∀ y, IsSlotA Z'.th y → ∀ p mp asid, capAtA (L.mem s') y = .Pml4 p mp asid → + (∃ y0, IsSlotA Z.th y0 ∧ capAtA (L.mem s) y0 = .Pml4 p mp asid) ∨ + (nt.val = 8 ∧ p ∈ utBasesA aligned z win)) : + ∀ y, IsSlotA Z'.th y → ∀ p mp asid, capAtA (L.mem s') y = .Pml4 p mp asid → p ∈ Z'.S1 ∧ Z'.Lv p = 0 := by + intro y hy p mp asid h + rw [e1, e2] + rcases hcap y hy p mp asid h with ⟨y0, hy0, h0⟩ | ⟨h8, hb⟩ + · obtain ⟨a, b⟩ := old y0 hy0 p mp asid h0 + exact ⟨Or.inl a, (ite_eq_right (hfresh p a)).trans b⟩ + · have hn : p ∈ utS1NewA nt aligned z win := ⟨h8, hb⟩ + exact ⟨Or.inr hn, (ite_eq_left hn).trans (ite_eq_left h8)⟩ + +/-- **`Stage2CoreA` after the creation.** -/ +theorem utRt_stage2CoreA {Z Z' : XSetsA} {s s' : S} {σ σ' : Fl4SpecA64.State Unit} + (old : Stage2CoreA (L := L) Z s σ) (e1 : Z'.X = Z.X ∪ utXNewA nt aligned z win) + (hk : ∀ r ∈ Z.X, σ'.kheap (specPtr r) = σ.kheap (specPtr r)) + (hnew : ∀ r ∈ utXNewA nt aligned z win, ∃ es, σ'.kheap (specPtr r) = some (.archObj (.ext (.stage2Root es)))) + (hback : ∀ p es, σ'.kheap p = some (.archObj (.ext (.stage2Root es))) → + σ.kheap p = some (.archObj (.ext (.stage2Root es))) ∨ ∃ r ∈ utXNewA nt aligned z win, specPtr r = p) + (hcap : ∀ y, IsSlotA Z'.th y → ∀ p l mp asid va, capAtA (L.mem s') y = .Ept p l mp asid va → + (∃ y0, IsSlotA Z.th y0 ∧ capAtA (L.mem s) y0 = .Ept p l mp asid va) ∨ + (nt.val = 13 ∧ p ∈ utBasesA aligned z win)) : + Stage2CoreA (L := L) Z' s' σ' := by + refine ⟨fun r hr => ?_, fun p es h => ?_, fun y hy p l mp asid va h => ?_⟩ + · rw [e1] at hr + rcases hr with hr | hr + · obtain ⟨es, h⟩ := old.obj r hr + exact ⟨es, (hk r hr).trans h⟩ + · exact hnew r hr + · rw [e1] + rcases hback p es h with g | ⟨r, hr, e⟩ + · obtain ⟨r, hr, e⟩ := old.back p es g + exact ⟨r, Or.inl hr, e⟩ + · exact ⟨r, Or.inr hr, e⟩ + · rw [e1] + rcases hcap y hy p l mp asid va h with ⟨y0, hy0, h0⟩ | g + · exact Or.inl (old.cap y0 hy0 p l mp asid va h0) + · exact Or.inr g + +/-- **`HighVSpaceCoreA` after the creation.** -/ +theorem utRt_highCoreA {Z Z' : XSetsA} {s s' : S} {σ σ' : Fl4SpecA64.State Unit} + (old : HighVSpaceCoreA (L := L) Z s σ) (e1 : Z'.Y = Z.Y ∪ utYNewA nt aligned z win) + (hk : ∀ r ∈ Z.Y, σ'.kheap (specPtr r) = σ.kheap (specPtr r)) + (hnew : ∀ r ∈ utYNewA nt aligned z win, ∃ l1, σ'.kheap (specPtr r) = some (.archObj (.ext (.highVSpace l1)))) + (hback : ∀ p l1, σ'.kheap p = some (.archObj (.ext (.highVSpace l1))) → + σ.kheap p = some (.archObj (.ext (.highVSpace l1))) ∨ ∃ r ∈ utYNewA nt aligned z win, specPtr r = p) + (hcap : ∀ y, IsSlotA Z'.th y → ∀ p mp asid, capAtA (L.mem s') y = .HighVSpace p mp asid → + (∃ y0, IsSlotA Z.th y0 ∧ capAtA (L.mem s) y0 = .HighVSpace p mp asid) ∨ + (nt.val = 14 ∧ p ∈ utBasesA aligned z win)) : + HighVSpaceCoreA (L := L) Z' s' σ' := by + refine ⟨fun r hr => ?_, fun p l1 h => ?_, fun y hy p mp asid h => ?_⟩ + · rw [e1] at hr + rcases hr with hr | hr + · obtain ⟨l1, h⟩ := old.obj r hr + exact ⟨l1, (hk r hr).trans h⟩ + · exact hnew r hr + · rw [e1] + rcases hback p l1 h with g | ⟨r, hr, e⟩ + · obtain ⟨r, hr, e⟩ := old.back p l1 g + exact ⟨r, Or.inl hr, e⟩ + · exact ⟨r, Or.inr hr, e⟩ + · rw [e1] + rcases hcap y hy p mp asid h with ⟨y0, hy0, h0⟩ | g + · exact Or.inl (old.cap y0 hy0 p mp asid h0) + · exact Or.inr g + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtRtWitA.lean b/proofs/Fl4ProofsA64/P3/UtRtWitA.lean new file mode 100644 index 000000000..cc1f710e4 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtRtWitA.lean @@ -0,0 +1,254 @@ +/- +P3 (aarch64, A64-P3 Untyped W5): **`StateRelA` and `CapValidA` at the typed heap after a Retype** (lead 01:35Z). + +* `utRt_stateRelA`: after `retype_region` (`utRetStA`) and FL4's object creation, the CSpace relation holds at + `utRtThA`, before any cap is written: old CNodes and TCBs keep their spec object and slot words; a new CNode is the + empty CNode over zeroed words; a new TCB is `default_tcb` over zeroed slot words. +* `utRt_capValid_growA`: caps valid before stay valid. `utRt_capValid_newA`: every new object's cap is valid, for + every type (VSpace types have no `CapValidA` clause). +* `utRt_isSlot_growA`: old slots remain slots. + +The other object kinds (endpoint, notification, reply, SC, frame, VCPU) are `other` entries with no `StateRelA` +clause; their own relations are other record fields. The cap writes that follow (`set_cap` on the untyped, +`create_cap` into the destinations) and `CdtRelA` are not composed here. + +Case hypotheses: the zero words are `CrtE.zero` (CNode window) and `DbgX` (a new TCB's first 224 bytes). No named +premise. +-/ +import Fl4ProofsA64.P3.UtRtThA +import Fl4ProofsA64.P3.UtRtSpecA +import Fl4ProofsA64.P3.UtObjA +import Fl4ProofsA64.P3.EmptyA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +open Fl4SpecA64 Fl4Spec + +section +variable {th : TypedHeapA} {dev : Bool} {nt us aligned : U64} {len : Nat} + +/-- Old slots remain slots. -/ +theorem utRt_isSlot_growA (hfree : ∀ p, UtNewA dev nt us aligned len p → th p = none) {y : U64} + (h : IsSlotA th y) : IsSlotA (utRtThA th dev nt us aligned len) y := by + obtain ⟨p, h⟩ := h + refine ⟨p, ?_⟩ + rcases h with ⟨r, hp, rest⟩ | ⟨hp, rest⟩ + · exact Or.inl ⟨r, utRtTh_growA hfree hp, rest⟩ + · exact Or.inr ⟨utRtTh_growA hfree hp, rest⟩ + +/-- Caps valid before stay valid. -/ +theorem utRt_capValid_growA (hfree : ∀ p, UtNewA dev nt us aligned len p → th p = none) {c : capability.Cap} + (h : CapValidA th c) : CapValidA (utRtThA th dev nt us aligned len) c := by + cases c + case CNode p r g gb => + obtain ⟨h1, h2⟩ := h + exact ⟨utRtTh_growA hfree h1, h2⟩ + case Thread p => exact utRtTh_growA hfree h + all_goals exact h + +/-- Every new object's cap is valid. `hram`: CNodes and TCBs are RAM objects; `hr`: no radix-0 CNode (the decode's +checks). -/ +theorem utRt_capValid_newA (hn : nt.val < 15) (hram : nt.val = 1 ∨ nt.val = 4 → dev = false) + (hr : nt.val = 4 → us.val ≠ 0) {j : Nat} (hj : j < len) : + CapValidA (utRtThA th dev nt us aligned len) + (newCapA nt.val (bjA aligned (zA nt.val us) j) us dev) := by + have new : nt.val = 1 ∨ nt.val = 4 → + utRtThA th dev nt us aligned len (bjA aligned (zA nt.val us) j) = some (utKindA nt us) := fun h => + utRtTh_newA ⟨hram h, by omega, by omega, j, hj, rfl⟩ + generalize hb : bjA aligned (zA nt.val us) j = b at new ⊢ + by_cases h1 : nt.val = 1 + · have e := new (Or.inl h1) + simp only [utKindA, h1, ite_eq_left] at e + rw [h1] + simpa [newCapA, CapValidA] using e + by_cases h4 : nt.val = 4 + · have e := new (Or.inr h4) + simp only [utKindA, h4] at e + rw [h4] + simp only [newCapA] + exact ⟨by simpa using e, hr h4⟩ + generalize nt.val = n at hn h1 h4 ⊢ + interval_cases n <;> first + | omega + | (simp [newCapA, CapValidA]) + +/-- A new CNode's slot word offsets are inside its window. -/ +private theorem ut_slot_valA {b : U64} {idx : List Bool} {r : Nat} (hl : idx.length = r) (hr : r ≤ 47) + (hfit : b.val + 2 ^ (r + 5) ≤ 2 ^ 64) : + (slotAddrA (specPtr b) (ofBl idx)).val = b.val + 32 * blvA idx ∧ 32 * blvA idx + 32 ≤ 2 ^ (r + 5) := by + have hlt := blvA_lt idx + rw [hl] at hlt + have h32 : 2 ^ (r + 5) = 32 * 2 ^ r := by rw [pow_add]; omega + have hle : 32 * blvA idx + 32 ≤ 2 ^ (r + 5) := by omega + refine ⟨?_, hle⟩ + rw [slotAddrA_sp, ofBlA_eq (by omega)] + show (b.bv + BitVec.ofNat 64 (blvA idx) * 32#64).toNat = _ + have h64 : 2 ^ (r + 5) ≤ 2 ^ 64 := Nat.pow_le_pow_right (by norm_num) (by omega) + have hv : b.bv.toNat = b.val := rfl + rw [BitVec.toNat_add, BitVec.toNat_mul, BitVec.toNat_ofNat, hv, + show (32#64 : BitVec 64).toNat = 32 from rfl, + Nat.mod_eq_of_lt (show blvA idx < 2 ^ 64 by omega), Nat.mod_eq_of_lt (show blvA idx * 32 < 2 ^ 64 by omega), + Nat.mod_eq_of_lt (by omega)] + omega + +/-- **The CSpace relation at the new typed heap**, after `retype_region` and the object creation. -/ +theorem utRt_stateRelA {t : ApiObjectType} (htt : objTypeOfA nt.val = some t) (hus : us.val < 64) + {mem mem' : U64 → U64} {σ : State Unit} (h : StateRelA th mem σ) {ksb : U64} (P : PlaceA th ksb) + (hfree : ∀ j, j < len → th (bjA aligned (zA nt.val us) j) = none) + (hbj : ∀ j, j < len → (bjA aligned (zA nt.val us) j).val = aligned.val + j * 2 ^ (zA nt.val us).val ∧ + aligned.val + j * 2 ^ (zA nt.val us).val + 2 ^ (zA nt.val us).val ≤ 2 ^ 64) + (hal : aligned.val % 8 = 0) (hc47 : nt.val = 4 → us.val ≤ 47) + (hkeep : ∀ y, IsSlotA th y → mem' y = mem y ∧ mem' (wa y 8#u64) = mem (wa y 8#u64)) + (hzC : nt.val = 4 → dev = false → ∀ w : U64, Aligned w → aligned.val ≤ w.val → + w.val < aligned.val + len * 2 ^ (zA nt.val us).val → mem' w = 0#u64) + (hzT : nt.val = 1 → dev = false → ∀ j, j < len → ∀ K : U64, K.val < 224 → K.val % 8 = 0 → + mem' (wa (bjA aligned (zA nt.val us) j) K) = 0#u64) : + StateRelA (utRtThA th dev nt us aligned len) mem' + (utRetStA t dev us.val (fl4RetypeAddrs fl4ObjBitsApi (specPtr aligned) len us.val t) σ) := by + have hn : nt.val < 15 := (objTypeOfA_some htt us.val).1 + -- old objects are at no new address + have oldH : ∀ p k, th p = some k → + (utRetStA t dev us.val (fl4RetypeAddrs fl4ObjBitsApi (specPtr aligned) len us.val t) σ).kheap (specPtr p) = + σ.kheap (specPtr p) := by + intro p k hp + rw [utRetSt_kheapA, ite_eq_right] + rw [utRet_addrs_memA htt hus] + rintro ⟨j, hj, e⟩ + rw [ut_specPtr_injA e, hfree j hj] at hp + cases hp + have newH : ∀ j, j < len → + (utRetStA t dev us.val (fl4RetypeAddrs fl4ObjBitsApi (specPtr aligned) len us.val t) σ).kheap + (specPtr (bjA aligned (zA nt.val us) j)) = some (defaultObject t dev us.val σ.curDomain) := by + intro j hj + rw [utRetSt_kheapA, ite_eq_left ((utRet_addrs_memA htt hus).2 ⟨j, hj, rfl⟩)] + -- an old slot relation is kept + have slot : ∀ y, IsSlotA th y → ∀ oc, SlotRelA mem y oc → SlotRelA mem' y oc := by + intro y hy oc ⟨ac, e, hr⟩ + obtain ⟨k0, k8⟩ := hkeep y hy + refine ⟨ac, e, ?_⟩ + have : capAtA mem' y = capAtA mem y := by unfold capAtA; rw [k0, k8] + rw [this]; exact hr + refine ⟨fun p r hp => ?_, fun q hq => ?_⟩ + · rcases utRtTh_splitA hp with ⟨-, old⟩ | ⟨⟨hd, h0, -, j, hj, rfl⟩, hk⟩ + · obtain ⟨cs, hk, hwf, hs⟩ := h.cnode p r old + refine ⟨cs, (oldH p _ old).trans hk, hwf, fun idx hl => slot _ ?_ _ (hs idx hl)⟩ + refine ⟨p, Or.inl ⟨r, old, blvA idx, hl ▸ blvA_lt idx, ?_⟩⟩ + have hr64 : idx.length ≤ 64 := by + have h1 := P.fit p _ old + simp only [ObjKindA.size] at h1 + have h2 : 2 ^ (r + 5) ≤ 2 ^ 64 := by omega + have := (Nat.pow_le_pow_iff_right (by norm_num : 1 < 2)).1 h2 + omega + rw [ofBlA_eq hr64] + · unfold utKindA at hk + split_ifs at hk with h1 h4 + cases hk + have ht : t = .capTableObject := (objTypeOfA_some htt us.val).2.2.2.2.1.2 h4 + have h47 := hc47 h4 + obtain ⟨hv, hfit⟩ := hbj j hj + have ez : (zA nt.val us).val = us.val + 5 := by + rw [zA_valA nt us hn hus]; simp [zOfA, h4, Nat.add_comm] + have hz : ∀ idx : List Bool, idx.length = us.val → + mem' (slotAddrA (specPtr (bjA aligned (zA nt.val us) j)) (ofBl idx)) = 0#u64 ∧ + mem' (wa (slotAddrA (specPtr (bjA aligned (zA nt.val us) j)) (ofBl idx)) 8#u64) = 0#u64 := by + intro idx hl + obtain ⟨sv, sle⟩ := ut_slot_valA (b := bjA aligned (zA nt.val us) j) hl h47 + (by rw [hv, ← ez]; exact hfit) + have h1' : (j + 1) * 2 ^ (zA nt.val us).val ≤ len * 2 ^ (zA nt.val us).val := Nat.mul_le_mul_right _ hj + rw [Nat.add_mul, Nat.one_mul] at h1' + rw [← ez] at sle + have h8 : (wa (slotAddrA (specPtr (bjA aligned (zA nt.val us) j)) (ofBl idx)) 8#u64).val = + (slotAddrA (specPtr (bjA aligned (zA nt.val us) j)) (ofBl idx)).val + 8 := by + rw [wa_val] + have : (8#u64 : U64).val = 8 := by simp + rw [this]; exact Nat.mod_eq_of_lt (by omega) + have h32 : 2 ^ (zA nt.val us).val % 32 = 0 := by + rw [ez, pow_add]; omega + have hal8 : (bjA aligned (zA nt.val us) j).val % 8 = 0 := by + have : j * 2 ^ (zA nt.val us).val % 8 = 0 := by + have : 8 ∣ 2 ^ (zA nt.val us).val := Nat.dvd_of_mod_eq_zero (by omega) + exact Nat.mod_eq_zero_of_dvd (Dvd.dvd.mul_left this _) + omega + constructor + · exact hzC h4 hd _ (by show _ % 8 = 0; omega) (by omega) (by omega) + · exact hzC h4 hd _ (by show _ % 8 = 0; omega) (by omega) (by omega) + obtain ⟨hwf, hs⟩ := ut_cnodeSlotsA mem' (bjA aligned (zA nt.val us) j) us.val hz + refine ⟨emptyCnode us.val, ?_, hwf, hs⟩ + rw [newH j hj, ht]; rfl + · rcases utRtTh_splitA hq with ⟨-, old⟩ | ⟨⟨hd, h0, -, j, hj, rfl⟩, hk⟩ + · obtain ⟨x, hk, hs⟩ := h.tcb q old + exact ⟨x, (oldH q _ old).trans hk, fun i hi => slot _ ⟨q, Or.inr ⟨old, i, hi, rfl⟩⟩ _ (hs i hi)⟩ + · have h1 : nt.val = 1 := by + unfold utKindA at hk + split_ifs at hk with h1 h4 + exact h1 + have ht : t = .tcbObject := by + rw [h1] at htt + simpa [objTypeOfA] using htt.symm + refine ⟨defaultTcb σ.curDomain, by rw [newH j hj, ht]; rfl, fun i hi => ?_⟩ + have hi7 : i < 7 := hi + obtain ⟨hv, hfit⟩ := hbj j hj + have ez : (zA nt.val us).val = 12 := by rw [h1]; exact zA_1 us + have e12 : 2 ^ (zA nt.val us).val = 4096 := by rw [ez]; norm_num + have K : ∀ o : Nat, o < 224 → o % 8 = 0 → mem' (wa (bjA aligned (zA nt.val us) j) + ⟨BitVec.ofNat 64 o⟩) = 0#u64 := by + intro o ho h8 + have ev : (⟨BitVec.ofNat 64 o⟩ : U64).val = o := by + show (BitVec.ofNat 64 o).toNat = _ + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)] + exact hzT h1 hd j hj _ (by rw [ev]; exact ho) (by rw [ev]; exact h8) + have ea : ∀ o : Nat, (slotAddrA (specPtr (bjA aligned (zA nt.val us) j)) (BitVec.ofNat 64 i) = + wa (bjA aligned (zA nt.val us) j) ⟨BitVec.ofNat 64 (32 * i)⟩) ∧ + (wa (slotAddrA (specPtr (bjA aligned (zA nt.val us) j)) (BitVec.ofNat 64 i)) 8#u64 = + wa (bjA aligned (zA nt.val us) j) ⟨BitVec.ofNat 64 (32 * i + 8)⟩) := by + intro _ + have base : slotAddrA (specPtr (bjA aligned (zA nt.val us) j)) (BitVec.ofNat 64 i) = + wa (bjA aligned (zA nt.val us) j) ⟨BitVec.ofNat 64 (32 * i)⟩ := by + rw [slotAddrA_sp] + apply UScalar.eq_of_val_eq + rw [wa_val] + show ((bjA aligned (zA nt.val us) j).bv + BitVec.ofNat 64 i * 32#64).toNat = + ((bjA aligned (zA nt.val us) j).val + (BitVec.ofNat 64 (32 * i)).toNat) % 2 ^ 64 + have hb : (bjA aligned (zA nt.val us) j).bv.toNat = (bjA aligned (zA nt.val us) j).val := rfl + rw [BitVec.toNat_add, BitVec.toNat_mul, hb] + have e1 : (BitVec.ofNat 64 i).toNat = i := by + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + have e2 : (BitVec.ofNat 64 (32 * i)).toNat = 32 * i := by + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + have e3 : (32#64 : BitVec 64).toNat = 32 := rfl + rw [e1, e2, e3, Nat.mod_eq_of_lt (show i * 32 < 2 ^ 64 by omega), Nat.mul_comm i 32] + refine ⟨base, ?_⟩ + rw [base] + apply UScalar.eq_of_val_eq + have ev : ∀ o : Nat, o < 4096 → (wa (bjA aligned (zA nt.val us) j) ⟨BitVec.ofNat 64 o⟩).val = + (bjA aligned (zA nt.val us) j).val + o := by + intro o ho + have e1 : (⟨BitVec.ofNat 64 o⟩ : U64).val = o := by + show (BitVec.ofNat 64 o).toNat = _ + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)] + rw [wa_val, e1] + exact Nat.mod_eq_of_lt (by omega) + rw [wa_val, ev _ (by omega), ev _ (by omega)] + have : (8#u64 : U64).val = 8 := by simp + rw [this] + rw [Nat.mod_eq_of_lt (by omega)] + omega + have z0 : mem' (slotAddrA (specPtr (bjA aligned (zA nt.val us) j)) (BitVec.ofNat 64 i)) = 0#u64 := by + rw [(ea 0).1]; exact K _ (by omega) (by omega) + have z8 : mem' (wa (slotAddrA (specPtr (bjA aligned (zA nt.val us) j)) (BitVec.ofNat 64 i)) 8#u64) = + 0#u64 := by rw [(ea 0).2]; exact K _ (by omega) (by omega) + refine ⟨.nullCap, ?_, ?_⟩ + · interval_cases i <;> rfl + · rw [ut_capAt_zeroA mem' _ z0 z8]; exact CapRelA.null + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtRtZeroPostA.lean b/proofs/Fl4ProofsA64/P3/UtRtZeroPostA.lean new file mode 100644 index 000000000..fb7020412 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtRtZeroPostA.lean @@ -0,0 +1,325 @@ +/- +P3 (aarch64, A64-P3 Untyped W5): **`JAtZAX` after a RAM creation of a zeroed type** — every type but TCB, VCPU and +HighVSpace: untyped, endpoint, notification, CNode, SC, reply, frames, VSpace root, page table, Stage-2 root. + +* **`utRt_zeroPostA`**: from the record at the creation's start, FL4's creation (`CrtE`, its sharp write frame), + `StateRelA` / `CdtRelA` after it, and any outcome of the spec body, `JAtZAX` at the grown sets. The new SCs join + `C`; the new VSpace pages `S1` / `X`. + +Hypotheses: `hsub` (closure item A), the creation's window (`W`, `h1`, `h2`), `CrtE`, `w`, `hS` / `hC` (`utSite_relsA`), +`P'`, `har` (FL4's final arch state is related to the same hardware root), the invoked and destination slots' CNodes, +and the two size checks of the decode (`hr4`, `hsc`). No named premise. New theorems only. +-/ +import Fl4ProofsA64.P3.UtRtPostA +import Fl4ProofsA64.P3.UtRtGlobA +import Fl4ProofsA64.P3.UtRtCapsA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +set_option maxRecDepth 100000 + +theorem utRtMA_empty (m : Fl4SpecA64.McState) : utRtMA m ∅ = m := by + have e : (utRtMA m ∅).tcbAffinity = m.tcbAffinity := by + funext p + unfold utRtMA + exact ite_eq_right (fun h => h.elim fun _ g => g.1) + show { m with tcbAffinity := (utRtMA m ∅).tcbAffinity } = m + rw [e] + +/-- The new SCs. -/ +def utCNewA (nt aligned z : U64) (win : Nat) : Set U64 := {c | nt.val = 5 ∧ c ∈ utBasesA aligned z win} + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {J5 J7 : JSlotA S} {s s' : S} {a a' : A} {σ : State Unit} {m : McState} {x p sb fi : U64} + {nt us aligned dest off win : U64} + +/-- **`JAtZAX` after a RAM creation of a zeroed type** (module docstring). -/ +theorem utRt_zeroPostA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (hx : x ∈ OS) (hc : capA st s x = .Untyped p sb fi false) + (W : UtWinZA nt aligned (zA nt.val us) win.val) + (h1 : p.val + fi.val ≤ aligned.val) + (h2 : aligned.val + win.val * 2 ^ (zA nt.val us).val ≤ p.val + 2 ^ sb.val) + (z1 : nt.val ≠ 1) (z12 : nt.val ≠ 12) (z14 : nt.val ≠ 14) + (hr4 : nt.val = 4 → us.val ≠ 0) (hsc : nt.val = 5 → 7 ≤ us.val) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (Ec : CrtE st H H.frame OS s s' x p sb false aligned nt us dest off win) + (w : WritesIn st s s' (UtCrtWA st H OS s nt aligned (zA nt.val us) win.val)) + {t : ApiObjectType} (htt : objTypeOfA nt.val = some t) (hus : us.val < 64) + {srcSlot : CslotPtr} {base : Word} {slots : List CslotPtr} (hlen : slots.length = win.val) + (hsrcS : ∃ q, srcSlot.1 = specPtr q ∧ Z.th q = some (.cnode srcSlot.2.length)) + (hdst : ∀ d ∈ slots, ∃ q, d.1 = specPtr q ∧ Z.th q = some (.cnode d.2.length)) + {r : Unit × State Unit} + (hr : r ∈ (utRtBodyA srcSlot base (specPtr aligned) t us.val slots false σ).1) + {G' : MdbGhostA} (hS : StateRelA (utRtThA Z.th false nt us aligned win.val) (L.mem s') r.2) + (hC : CdtRelA st (utRtThA Z.th false nt us aligned win.val) s' r.2 G') + (P' : PlaceA (utRtThA Z.th false nt us aligned win.val) (L.ksb s)) (har : AR a' Z.hw) : + JAtZAX (L := L) AR + (utRtZA Z G' nt us aligned win.val ∅ (utCNewA nt aligned (zA nt.val us) win.val) ∅) s' a' r.2 m := by + have hn : nt.val < 15 := (objTypeOfA_some htt us.val).1 + have Cl : UtClrA st Z H s aligned.val (aligned.val + win.val * 2 ^ (zA nt.val us).val) := + utRt_winClrA J hx hc h1 h2 + have anchor : ∀ j, j < win.val → specPtr (bjA aligned (zA nt.val us) j) ≠ Fl4SpecA64.idleScPtr := by + intro j hj + have v := utRt_newValA W hj + exact utRet_anchorA (st := st) J.idleAnchor hx hc (by omega) (by omega) + have memA : ∀ q, q ∈ fl4RetypeAddrs fl4ObjBitsApi (specPtr aligned) slots.length us.val t ↔ + UtNewSpA aligned (zA nt.val us) win.val q := fun q => by + rw [hlen, utRet_addrs_memA htt hus] + rfl + have thOff : ∀ q k, Z.th q = some k → ¬ UtNewSpA aligned (zA nt.val us) win.val (specPtr q) := fun q k hq => + utRt_relOffA (m := m) J Cl W rfl rfl anchor (Or.inl ⟨q, by rw [hq]; simp, rfl⟩) + -- the spec outcome + obtain ⟨qS, eqS, hqS⟩ := hsrcS + obtain ⟨csS, hkS, -, hsS⟩ := J.j.1.st.cnode qS _ hqS + rw [← eqS] at hkS + obtain ⟨capS, hcapS, -⟩ := hsS srcSlot.2 rfl + have hdc : ∀ d ∈ slots, (∃ sz cn, σ.kheap d.1 = some (.cnode sz cn)) ∧ + d.1 ∉ fl4RetypeAddrs fl4ObjBitsApi (specPtr aligned) slots.length us.val t := by + intro d hd + obtain ⟨q, eq, hq⟩ := hdst d hd + obtain ⟨cs, hk, -⟩ := J.j.1.st.cnode q _ hq + rw [eq, memA] + exact ⟨⟨_, cs, hk⟩, thOff q _ hq⟩ + have hSa : srcSlot.1 ∉ fl4RetypeAddrs fl4ObjBitsApi (specPtr aligned) slots.length us.val t := by + rw [eqS, memA] + exact thOff qS _ hqS + obtain ⟨-, hCn, hNew, hK, -, -, -⟩ := utRtBody_invA hr hkS hcapS hSa hdc + -- an old object that is no CNode is kept + have keep : ∀ q, t = .untyped ∨ ¬ UtNewSpA aligned (zA nt.val us) win.val q → + ∀ ko, (∀ sz cn, ko ≠ .cnode sz cn) → ∀ u : Cap, + let σa := setObjA σ srcSlot.1 (.cnode srcSlot.2.length (Function.update csS srcSlot.2 (some u))) + σa.kheap q = some ko → σ.kheap q = some ko := by + intro q _ ko ne u σa h + by_cases e : q = srcSlot.1 + · have : σa.kheap q = some (.cnode srcSlot.2.length (Function.update csS srcSlot.2 (some u))) := by + rw [e]; exact Function.update_self .. + rw [this] at h + exact absurd (Option.some.inj h).symm (ne _ _) + · exact (Function.update_of_ne e ..).symm.trans h + have hkeep : ∀ q, ¬ UtNewSpA aligned (zA nt.val us) win.val q → (∀ sz cn, σ.kheap q ≠ some (.cnode sz cn)) → + r.2.kheap q = σ.kheap q := by + intro q hq nc + have e : q ≠ srcSlot.1 := fun e => nc _ _ (e ▸ hkS) + have ea : ∀ u : Cap, (setObjA σ srcSlot.1 (.cnode srcSlot.2.length (Function.update csS srcSlot.2 (some u)))).kheap q = + σ.kheap q := fun u => Function.update_of_ne e .. + rw [hK q (Or.inr (fun h => hq ((memA q).1 h))) (fun sz cn => by rw [ea]; exact nc sz cn), ea] + have newObj : t ≠ .untyped → ∀ j, j < win.val → + r.2.kheap (specPtr (bjA aligned (zA nt.val us) j)) = some (defaultObject t false us.val σ.curDomain) := + fun ht j hj => hNew ht _ ((memA _).2 ⟨j, hj, rfl⟩) + have back : ∀ q ko, (∀ sz cn, ko ≠ .cnode sz cn) → r.2.kheap q = some ko → σ.kheap q = some ko ∨ + (t ≠ .untyped ∧ UtNewSpA aligned (zA nt.val us) win.val q ∧ ko = defaultObject t false us.val σ.curDomain) := by + intro q ko ne h + by_cases g : t = .untyped ∨ ¬ UtNewSpA aligned (zA nt.val us) win.val q + · have g' : t = .untyped ∨ q ∉ fl4RetypeAddrs fl4ObjBitsApi (specPtr aligned) slots.length us.val t := + g.imp id (fun k m' => k ((memA q).1 m')) + obtain ⟨u0, eu⟩ : ∃ u0 : Cap, u0 = .untypedCap false base (bitsOf capS) (unat (specPtr aligned + + BitVec.ofNat 64 slots.length <<< fl4ObjBitsApi t us.val - base)) := ⟨_, rfl⟩ + refine Or.inl (keep q g ko ne u0 ?_) + rw [eu] + by_cases cn : ∃ sz c, (setObjA σ srcSlot.1 (.cnode srcSlot.2.length (Function.update csS srcSlot.2 (some + (.untypedCap false base (bitsOf capS) (unat (specPtr aligned + + BitVec.ofNat 64 slots.length <<< fl4ObjBitsApi t us.val - base))))))).kheap q = some (.cnode sz c) + · obtain ⟨sz, c, k⟩ := cn + rw [(hCn q sz c g' k).1] at h + exact absurd (Option.some.inj h).symm (ne _ _) + · rw [hK q g' (fun sz c k => cn ⟨sz, c, k⟩)] at h + exact h + · have gt : t ≠ .untyped := fun e => g (Or.inl e) + have gq : UtNewSpA aligned (zA nt.val us) win.val q := by + by_contra k + exact g (Or.inr k) + refine Or.inr ⟨gt, gq, ?_⟩ + rw [hNew gt q ((memA q).2 gq)] at h + exact (Option.some.inj h).symm + -- the new words are zero + have wk : ¬ WKA nt := by + unfold WKA + omega + have zw : ∀ j, j < win.val → ∀ b : U64, Aligned b → (bjA aligned (zA nt.val us) j).val ≤ b.val → + b.val < (bjA aligned (zA nt.val us) j).val + 2 ^ (zA nt.val us).val → L.mem s' b = 0#u64 := by + intro j hj b hb g1 g2 + have v := utRt_newValA W hj + have h1' : (j + 1) * 2 ^ (zA nt.val us).val ≤ win.val * 2 ^ (zA nt.val us).val := Nat.mul_le_mul_right _ hj + rw [Nat.add_mul, Nat.one_mul] at h1' + have := W.fit + have e : (bjA aligned (zA nt.val us) j).val = aligned.val + j * 2 ^ (zA nt.val us).val := bjA_val (by + have := Nat.two_pow_pos (zA nt.val us).val + omega) + exact Ec.zero rfl wk b hb (by omega) (by omega) + -- a word at an offset of a new object + have zoff : ∀ j, j < win.val → 3 ≤ (zA nt.val us).val → ∀ (b : U64) (o : Nat), o % 8 = 0 → + o + 8 ≤ 2 ^ (zA nt.val us).val → b.val = (bjA aligned (zA nt.val us) j).val + o → L.mem s' b = 0#u64 := by + intro j hj h3 b o o8 ob e + have al : (bjA aligned (zA nt.val us) j).val % 8 = 0 := by + have v := utRt_newValA W hj + have h1' : (j + 1) * 2 ^ (zA nt.val us).val ≤ win.val * 2 ^ (zA nt.val us).val := Nat.mul_le_mul_right _ hj + rw [Nat.add_mul, Nat.one_mul] at h1' + have := W.fit + have ev : (bjA aligned (zA nt.val us) j).val = aligned.val + j * 2 ^ (zA nt.val us).val := bjA_val (by + have := Nat.two_pow_pos (zA nt.val us).val + omega) + have d8 : 8 ∣ 2 ^ (zA nt.val us).val := Nat.pow_dvd_pow 2 h3 + have da : 8 ∣ aligned.val := d8.trans (Nat.dvd_of_mod_eq_zero W.al) + rw [ev] + exact Nat.mod_eq_zero_of_dvd (Nat.dvd_add da (Dvd.dvd.mul_left d8 j)) + refine zw j hj b ?_ (by omega) (by omega) + unfold Aligned + omega + have hcap : ∀ y, IsSlotA (utRtThA Z.th false nt us aligned win.val) y → + UtCapAfterA Z.th (L.mem s) (L.mem s') nt us aligned win.val y := by + intro y hy + by_cases old : IsSlotA Z.th y + · by_cases ex : y = x + · obtain ⟨nfi, e⟩ := Ec.src + exact Or.inr (Or.inr (Or.inl ⟨p, sb, nfi, false, by rw [ex]; exact e⟩)) + · by_cases ed : ∃ j < win.val, y = djA dest off j + · obtain ⟨j, hj, e⟩ := ed + exact Or.inr (Or.inl ⟨j, hj, by rw [e]; exact Ec.new j hj⟩) + · exact Or.inl ⟨old, Ec.keep y (hsub y old) ex (fun j hj e => ed ⟨j, hj, e⟩)⟩ + · refine Or.inr (Or.inr (Or.inr ?_)) + obtain ⟨q, h⟩ := hy + have isNew : ∀ k, utRtThA Z.th false nt us aligned win.val q = some k → + (∀ k', Z.th q = some k' → k' ≠ k) → UtNewA false nt us aligned win.val q ∧ k = utKindA nt us := by + intro k hk ne + unfold utRtThA at hk + split_ifs at hk with g + · exact ⟨g, (Option.some.inj hk).symm⟩ + · exact absurd rfl (ne k hk) + rcases h with ⟨rd, hq, i, hi, e⟩ | ⟨hq, i, hi, e⟩ + · have g := isNew _ hq (fun k' hk' ek => old ⟨q, Or.inl ⟨rd, by rw [hk', ek], i, hi, e⟩⟩) + obtain ⟨⟨-, -, -, j, hj, ej⟩, ek⟩ := g + unfold utKindA at ek + rw [ite_eq_right z1] at ek + split_ifs at ek with g4 + have erd : rd = us.val := by injection ek + have ez : (zA nt.val us).val = us.val + 5 := by + rw [zA_valA nt us hn hus, g4] + unfold zOfA + simp + omega + have sz : 2 ^ (zA nt.val us).val = 2 ^ rd * 32 := by rw [ez, erd, pow_add]; norm_num + have v := utRt_newValA W hj + have h1' : (j + 1) * 2 ^ (zA nt.val us).val ≤ win.val * 2 ^ (zA nt.val us).val := Nat.mul_le_mul_right _ hj + rw [Nat.add_mul, Nat.one_mul] at h1' + have fit := W.fit + have ev : (bjA aligned (zA nt.val us) j).val = aligned.val + j * 2 ^ (zA nt.val us).val := bjA_val (by + have := Nat.two_pow_pos (zA nt.val us).val + omega) + have yv : y.val = q.val + 32 * i := by + rw [e] + refine slotAddrA_valN ?_ + rw [ej] + have := Nat.two_pow_pos (zA nt.val us).val + omega + have y8 : (wa y 8#u64).val = y.val + 8 := by + rw [wa_valP, show (8#u64 : U64).val = 8 from rfl] + refine Nat.mod_eq_of_lt ?_ + rw [yv, ej] + omega + have h3 : 3 ≤ (zA nt.val us).val := by omega + exact ut_capAt_zeroA _ _ (zoff j hj h3 y (32 * i) (by omega) (by omega) (by rw [yv, ej])) + (zoff j hj h3 _ (32 * i + 8) (by omega) (by omega) (by rw [y8, yv, ej]; omega)) + · have g := isNew _ hq (fun k' hk' ek => old ⟨q, Or.inr ⟨by rw [hk', ek], i, hi, e⟩⟩) + have ek := g.2 + unfold utKindA at ek + rw [ite_eq_right z1] at ek + split_ifs at ek + have hCnb : ∀ c ∈ utCNewA nt aligned (zA nt.val us) win.val, c ∈ utBasesA aligned (zA nt.val us) win.val := + fun c h => h.2 + rw [← utRtMA_empty m] + refine utRt_postA (Tn := ∅) (Cn := utCNewA nt aligned (zA nt.val us) win.val) (Vn := ∅) J hx hc W h1 h2 hn hr4 w + hS hC P' har (utRtBody_globA hr) hkeep (fun q => ⟨fun h => h.elim, fun h => absurd h.2 z1⟩) hCnb + (fun v h => h.elim) (fun h => absurd h z12) hcap (fun q h => h.elim) (fun c h => ?_) (fun v h => h.elim) + (fun q h => ?_) (fun q h => ?_) (fun y h => absurd h.1 z14) (fun q y h => ?_) (fun q es h => ?_) + (fun q l1 h => ?_) + · obtain ⟨n5, j, hj, rfl⟩ := h + have et : t = .schedContextObject := (objTypeOfA_some htt us.val).2.2.2.2.2.2 n5 + have ez : (zA nt.val us).val = us.val := by + rw [zA_valA nt us hn hus, n5] + unfold zOfA + simp + have big : 128 ≤ 2 ^ (zA nt.val us).val := by + rw [ez, show (128 : Nat) = 2 ^ 7 by norm_num] + exact Nat.pow_le_pow_right (by decide) (hsc n5) + have h3 : 3 ≤ (zA nt.val us).val := by + have := hsc n5 + omega + have off : ∀ (K : U64) (k : Nat), K.val = k → k + 8 ≤ 128 → + (wa (bjA aligned (zA nt.val us) j) K).val = (bjA aligned (zA nt.val us) j).val + k := by + intro K k ek hk + have v := utRt_newValA W hj + have h1' : (j + 1) * 2 ^ (zA nt.val us).val ≤ win.val * 2 ^ (zA nt.val us).val := Nat.mul_le_mul_right _ hj + rw [Nat.add_mul, Nat.one_mul] at h1' + have fit := W.fit + have ev : (bjA aligned (zA nt.val us) j).val = aligned.val + j * 2 ^ (zA nt.val us).val := bjA_val (by omega) + rw [wa_valP, ek] + exact Nat.mod_eq_of_lt (by omega) + refine ⟨zoff j hj h3 _ 24 (by omega) (by omega) (off _ 24 rfl (by omega)), + zoff j hj h3 _ 72 (by omega) (by omega) (off _ 72 rfl (by omega)), ?_⟩ + have o := newObj (by rw [et]; simp) j hj + rw [et] at o + exact ⟨_, _, o, rfl, rfl⟩ + · obtain ⟨g, j, hj, rfl⟩ := h + have et : t = .archObject .vspaceObj := by + rw [g] at htt + simpa [objTypeOfA, archDataToObjType, eq_comm] using htt + have o := newObj (by rw [et]; simp) j hj + rw [et] at o + rw [ite_eq_left g] + exact ⟨_, o, rfl⟩ + · obtain ⟨g, j, hj, rfl⟩ := h + have et : t = .archObject (.ext .stage2VSpaceObj) := by + rw [g] at htt + simpa [objTypeOfA, eq_comm] using htt + have o := newObj (by rw [et]; simp) j hj + rw [et] at o + exact ⟨_, o⟩ + · rcases back q _ (fun _ _ e => by cases e) h with k | ⟨gt, -, e⟩ + · exact Or.inl k + · exfalso + unfold objTypeOfA archDataToObjType at htt + interval_cases hv : nt.val <;> simp at htt <;> subst htt <;> + first + | exact z1 rfl + | exact z12 rfl + | exact z14 rfl + | exact absurd rfl gt + | cases e + · rcases back q _ (fun _ _ e => by cases e) h with k | ⟨gt, gq, e⟩ + · exact Or.inl k + · refine Or.inr ?_ + have g13 : nt.val = 13 := by + unfold objTypeOfA archDataToObjType at htt + interval_cases hv : nt.val <;> simp at htt <;> subst htt <;> + first + | rfl + | exact absurd rfl gt + | cases e + obtain ⟨j, hj, eq⟩ := gq + exact ⟨_, ⟨g13, j, hj, rfl⟩, eq.symm⟩ + · rcases back q _ (fun _ _ e => by cases e) h with k | ⟨gt, -, e⟩ + · exact Or.inl k + · exfalso + unfold objTypeOfA archDataToObjType at htt + interval_cases hv : nt.val <;> simp at htt <;> subst htt <;> + first + | exact z14 rfl + | exact absurd rfl gt + | cases e + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtSbufA.lean b/proofs/Fl4ProofsA64/P3/UtSbufA.lean new file mode 100644 index 000000000..e75ea92fd --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtSbufA.lean @@ -0,0 +1,220 @@ +/- +P3 (aarch64, A64-P3 Untyped, `UtResetA`): **a retained thread's spec IPC buffer misses a childless untyped** +(the premise `hsbuf` of `utReset_startA`). + +The thread's slot 2 holds a RAM frame cap or `Null` (`BufHold`), the spec's cap is its image (`StateRelA`), and a +childless untyped keeps the frame's heap object (A64 P1 `FrameOK`), so the whole frame is off the block (`ut_jointA`). +The spec's buffer is the frame's address plus its `tcbIpcBuffer` offset. + +Named premise: +* [J] `hsal`: every TCB's `tcbIpcBuffer` is 1024-aligned (l4v: `is_aligned tcb_ipc_buffer msg_align_bits`; + `msgAlignBits = 10`). **The record cannot supply it**: it relates the spec's `tcbIpcBuffer` to no FL4 word, and + `EpStateRel.buf` aligns the buffer's words to 8 only, so 128 words may run past the frame's end. +-/ +import Fl4ProofsA64.P3.UtDropCA +import Fl4ProofsA64.P3.UtZeroSpecA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {ar : machine.Arch A} + +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s : S} {a : A} {σ : State Unit} {m : McState} {p sb fi : U64} + +/-- A page-offset of a 1024-aligned address leaves room for a whole buffer. -/ +theorem ut_bufOffsetA (v : Word) (hv : v.toNat % 1024 = 0) (b : Nat) (h10 : 10 ≤ b) (h64 : b < 64) : + (v &&& mask b).toNat + 1024 ≤ 2 ^ b := by + have hm : (mask b : Word).toNat = 2 ^ b - 1 := by + have h1 : 2 ^ b < 2 ^ 64 := Nat.pow_lt_pow_right (by decide) h64 + have h2 : 1 ≤ 2 ^ b := Nat.one_le_two_pow + have e1 : (mask b : Word) = BitVec.ofNat 64 (2 ^ b - 1) := by + apply BitVec.eq_of_toNat_eq + have e : ((1 : Word) <<< b).toNat = 2 ^ b := by + rw [BitVec.toNat_shiftLeft, show (1 : Word).toNat = 1 from rfl, Nat.shiftLeft_eq, Nat.one_mul, + Nat.mod_eq_of_lt h1] + show (((1 : Word) <<< b) - 1).toNat = _ + rw [BitVec.toNat_sub_of_le (by rw [BitVec.le_def, e]; exact h2), e, BitVec.toNat_ofNat, + Nat.mod_eq_of_lt (by omega)] + rfl + rw [e1, BitVec.toNat_ofNat] + exact Nat.mod_eq_of_lt (by omega) + have e : (v &&& mask b).toNat = v.toNat % 2 ^ b := by + rw [BitVec.toNat_and, hm, Nat.and_two_pow_sub_one_eq_mod] + have hd : 1024 ∣ 2 ^ b := by + rw [show (1024 : Nat) = 2 ^ 10 by norm_num] + exact Nat.pow_dvd_pow 2 h10 + have h0 : v.toNat % 2 ^ b % 1024 = 0 := by rw [Nat.mod_mod_of_dvd _ hd, hv] + have hlt : v.toNat % 2 ^ b < 2 ^ b := Nat.mod_lt _ (Nat.two_pow_pos b) + obtain ⟨c, hc⟩ := hd + have g := Nat.div_add_mod (v.toNat % 2 ^ b) 1024 + rw [e] + omega + +set_option maxHeartbeats 1000000 in +/-- **A retained thread's spec IPC buffer misses a childless untyped**, RAM or device, whose region holds the heap +objects it meets (module docstring). -/ +theorem utSbufG_keptA {dev : Bool} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {Rq Nt : Set U64} {ex : Aeneas.Std.Array obj.Slot 3#usize} {x : U64} + (R : UtDecP st H Z.Cs H.frame Rq Nt OS s ex x p sb fi dev) + (hcx : capA st s x = .Untyped p sb fi dev) + (hRin : RegionIn H p.val (p.val + 2 ^ sb.val)) + (he : cdt.ensure_no_children st s x = ok (.Ok ())) + (hsal : ∀ t ∈ Z.T, ∀ y, σ.kheap (specPtr t) = some (.tcb y) → y.tcbIpcBuffer.toNat % 1024 = 0) : + ∀ t ∈ Z.T, specPtr t ∉ utDA p sb.val → ∀ y b sbf, σ.kheap (specPtr t) = some (.tcb y) → + ipcBufOfA b y = some sbf → UtBufOffA (utDA p sb.val) sbf := by + intro t ht kt y b sbf hk hb + obtain ⟨-, h47, hfi⟩ := R.utv + have hsb : sb.val < 64 := by omega + have hH : HeapOK H := R.heap + have hsub : ∀ y, IsSlotA Z.th y → y ∈ OS := J.j5.2.1 + have hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null := J.j5.2.2 + have htr : TreeA st OS Z.G.chains Z.G.par s := J.j.1.cdt.tree.grow hsub fun y hy hn => hnul y hy hn + have hrev : obj.Slot.revocable st x s = ok true := R.stz.1.2.1 x R.src (by rw [hcx]; rfl) + have hnd : ∀ y, ¬ Fl4Proofs.P3.Mdb.Desc Z.G.par y x := cdt.ensure_no_children.noDesc R.os htr R.src hcx hrev he + have P3 : PlaceA Z.th (L.ksb s) := J.j.1.place + have hxs : IsSlotA Z.th x := by + by_contra h + rw [hnul x R.src h] at hcx + cases hcx + have hwf : SpNoOvA p sb.val := by + obtain ⟨sl, habs, esl⟩ := IsSlotA.absA P3 hxs + obtain ⟨ac, -, hr⟩ := ut_absRelA J.j.1.st habs + have e : capAtA (L.mem s) (cslotAddrA sl) = capA st s x := by rw [esl]; rfl + have h1 := hr.1 + rw [e, hcx] at h1 + exact h1 + have hsp : (specPtr p).toNat + 2 ^ sb.val ≤ 2 ^ 64 := ut_spNoOvA hsb hwf + have hpfit : p.val + 2 ^ sb.val ≤ 2 ^ 64 := al_fit R.utv.1 (by omega) p.bv.isLt + have hD : ∀ z : U64, specPtr z ∈ utDA p sb.val ↔ p.val ≤ z.val ∧ z.val < p.val + 2 ^ sb.val := + utD_memA hsb hpfit hsp + have hbr := utD_bridgeA (H := H) hsb hpfit hsp hRin + -- the thread's buffer slot + have htH : H t = some .tcb := by + have h : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + exact h + have htf := hH.fits t _ htH + have htth : Z.th t = some .tcb := (J.sr.thT t).2 ht + obtain ⟨Pt, Ub7, vr, pool, armc, capc, vcc, frd, hic, sched, apart, frub, alub, vcub, Pp7, Tb7, Hr, Te, Kt, Lv7, Re, bits, r2, kt7, hkl⟩ := + J.j7 + have hsbS : sbWordA t = slotIdx t layout.tcb.SLOT_BUFFER := by + simp [sbWordA, slotIdx, layout.tcb.SLOT_BUFFER, layout.cte.SIZE] + have hsbi : (layout.tcb.SLOT_BUFFER : U64).val < 7 := by simp [layout.tcb.SLOT_BUFFER] + have hsbO : sbWordA t ∈ OS := by + rw [hsbS] + exact armc.case.ts t htH _ hsbi + have hbc : BufCap (capA st s (sbWordA t)) := J.sys.sysl.bh _ hsbO ⟨t, htH, rfl⟩ + have hsbK : sbWordA t ∈ OffR p.val (p.val + 2 ^ sb.val) OS := by + refine ⟨hsbO, ?_⟩ + have hout : t.val + 2 ^ HKind.tcb.bits ≤ p.val ∨ p.val + 2 ^ sb.val ≤ t.val := by + have hnR : ¬ RegionD H p.val (p.val + 2 ^ sb.val) t := fun h => kt ((hbr t _ htH).2 h) + unfold RegionD MeetsR at hnR + rw [htH] at hnR + simp only [Option.elim] at hnR + have hpos : 0 < 2 ^ sb.val := Nat.two_pow_pos _ + have hpos' : 0 < 2 ^ HKind.tcb.bits := Nat.two_pow_pos _ + omega + have hbits : 224 ≤ 2 ^ HKind.tcb.bits := by simp [HKind.bits] + have hv : (sbWordA t).val = t.val + 32 * (layout.tcb.SLOT_BUFFER : U64).val := by + rw [hsbS] + unfold slotIdx + have e32 : (32#u64 : U64).val = 32 := rfl + have hsz : UScalar.size UScalarTy.U64 = 2 ^ 64 := by simp [U64.size_def, U64.numBits] + rw [wa_valP, core.num.U64.wrapping_mul_val_eq, e32, hsz] + omega + omega + -- the spec's cap is the image of FL4's + obtain ⟨y', hk', hs⟩ := J.j.1.st.tcb t htth + rw [hk] at hk'; cases hk' + obtain ⟨ac, hac, hca⟩ := hs 2 (by decide) + rw [tcbCnodeMap_twoA] at hac + cases hac + obtain ⟨-, hab⟩ := hca + have hNative := J.bufNative t ht + rw [capAtA_eq, bufSlot_eqA] at hNative hab + rcases hbc with hn | ⟨ptr, size, rights, x1, x2, x3, x4, hc⟩ + · rw [hn] at hab + replace hab : (Fl4SpecA64.Cap.nullCap) = y.tcbIpcframe := hab + unfold ipcBufOfA at hb + rw [← hab] at hb + cases hb + have hF' := FrameOK.reset R.fr htr.ut R.src hcx hnd (OS' := OffR p.val (p.val + 2 ^ sb.val) OS) (fun y hy => hy.1) + (s' := s) (i' := fi) hcx (fun _ _ _ => rfl) + rw [hc] at hab hNative + have hept : x2 = false := hNative + subst hept + obtain ⟨bits', hbits⟩ : ∃ bits', size.bits = ok bits' := by + cases size <;> exact ⟨_, rfl⟩ + have hobj := hF' _ hsbK ptr size rights x1 false x3 x4 hc bits' hbits + obtain ⟨hnR, hqH⟩ := Heap.drop_eq_some.1 hobj + have kp : specPtr ptr ∉ utDA p sb.val := fun h => hnR ((hbr ptr _ hqH).1 h) + have hfit := hH.fits ptr _ hqH + simp only [HKind.bits] at hfit + have hpg : pageBitsForSize (frameSizeOfA size) = bits'.val ∧ 10 ≤ bits'.val ∧ bits'.val < 64 := by + cases size <;> simp only [capability.FrameSize.bits] at hbits <;> rw [← okinj hbits] <;> + exact ⟨by decide, by decide, by decide⟩ + replace hab : absCapA (.Frame ptr size rights false x1 false x3 x4) = y.tcbIpcframe := hab + simp only [absCapA, absFrameE, Bool.false_eq_true, ↓reduceIte] at hab + unfold ipcBufOfA at hb + rw [← hab] at hb + simp only at hb + split at hb + case isFalse => cases hb + have esb := Option.some.inj hb + rw [hpg.1] at esb + have hoff := ut_bufOffsetA y.tcbIpcBuffer (hsal t ht y hk) bits'.val hpg.2.1 hpg.2.2 + generalize hg : y.tcbIpcBuffer &&& mask bits'.val = off at esb hoff + intro k hk128 j hj8 + have hlt : off.toNat + k * 8 + j < 2 ^ bits'.val := by omega + have h64 : 2 ^ bits'.val < 2 ^ 64 := Nat.pow_lt_pow_right (by decide) hpg.2.2 + let o : U64 := ⟨BitVec.ofNat 64 (off.toNat + k * 8 + j)⟩ + have ho : o.val = off.toNat + k * 8 + j := by + show (BitVec.ofNat 64 (off.toNat + k * 8 + j)).toNat = _ + rw [BitVec.toNat_ofNat] + exact Nat.mod_eq_of_lt (by omega) + have ea : bufAddr sbf k + BitVec.ofNat 64 j = specPtr (wa ptr o) := by + rw [specPtr_wa, ← esb] + unfold bufAddr + show _ = specPtr ptr + BitVec.ofNat 64 (off.toNat + k * 8 + j) + rw [BitVec.ofNat_add, BitVec.ofNat_add, BitVec.ofNat_toNat, BitVec.setWidth_eq, BitVec.add_assoc, + BitVec.add_assoc, BitVec.add_assoc] + rfl + rw [ea] + have hw : (wa ptr o).val = ptr.val + o.val := by + rw [wa_valP] + exact Nat.mod_eq_of_lt (by omega) + have hpos : 0 < 2 ^ bits'.val := Nat.two_pow_pos _ + exact ut_jointA J.objAlloc R.src hcx hfi hD (Or.inr (Or.inr (Or.inr ⟨_, hqH, rfl⟩))) (a := ptr) (b := wa ptr o) + (le_refl _) (by simp only [HKind.bits]; omega) (by omega) (by simp only [HKind.bits]; omega) kp + +/-- **A retained thread's spec IPC buffer misses a childless RAM untyped** (module docstring). -/ +theorem utSbuf_keptA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {Ud Acd Rq Nt : Set U64} {ex : Aeneas.Std.Array obj.Slot 3#usize} {x : U64} + (Dp : DecPre st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ud Acd s (.Untyped p sb fi false) ex) + (P : SysPlace st H Z.Cs H.frame Rq Nt OS s ex x) + (hcap : obj.Slot.cap st x s = ok (.Untyped p sb fi false)) + (he : cdt.ensure_no_children st s x = ok (.Ok ())) + (hsal : ∀ t ∈ Z.T, ∀ y, σ.kheap (specPtr t) = some (.tcb y) → y.tcbIpcBuffer.toNat % 1024 = 0) : + ∀ t ∈ Z.T, specPtr t ∉ utDA p sb.val → ∀ y b sbf, σ.kheap (specPtr t) = some (.tcb y) → + ipcBufOfA b y = some sbf → UtBufOffA (utDA p sb.val) sbf := by + have hcx : capA st s x = .Untyped p sb fi false := okinj ((capA_eq s x).symm.trans hcap) + have R : UtDecP st H Z.Cs H.frame Rq Nt OS s ex x p sb fi false := + UtDecP.of_live Dp J.sys.sysl.sys P hcap J.sys.sysl.ln J.sys.sysl.vr J.sys.sysl.ly + exact utSbufG_keptA J R hcx (R.ur.rin R.src R.cap) he hsal + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtSpecA.lean b/proofs/Fl4ProofsA64/P3/UtSpecA.lean new file mode 100644 index 000000000..e45e44294 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtSpecA.lean @@ -0,0 +1,200 @@ +/- +P3 (aarch64, A64-P3 Untyped W1a): **spec-side facts of the untyped decode** (`Fl4SpecA64.Decode`, +`Ext/TcbSize`, `Ext/UntypedVirt`; x86 `P3/UntypedSpec`, spec half). + +* **`objTypeOfA` / `zOfA`**: FL4's object type numbers 0..14 (`fl4_abi::consts::object`) as the spec's types, and + the size in bits FL4's `object_size` gives, which is `fl4ObjBitsApi` (`objTypeOfA_some`). +* **`fl4DataToObjType_runA`**, **`mapMEx_ensureEmpty_runA`**, **`constOnFailure_runA`**: the decode's pieces with one, + state-preserving outcome. +* **`alignUp_shiftA`**: `align_up` commutes with the pointer shift `pptrShiftA = 2^47 + 2^39` up to size 39 + (contract finding F2; J4 `UtBlk39A` supplies the bound). + +No named premise. New definitions and lemmas only. +-/ +import Fl4ProofsA64.P3.LookupA +import Fl4ProofsA64.P3.CnodeSpecA + +namespace Fl4ProofsA64.P3 +open Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +/-! ## Object types -/ + +/-- FL4's object type number as the spec's type. -/ +def objTypeOfA (n : Nat) : Option ApiObjectType := + if n = 0 then some .untyped else if n = 1 then some .tcbObject + else if n = 2 then some .endpointObject else if n = 3 then some .notificationObject + else if n = 4 then some .capTableObject else if n = 5 then some .schedContextObject + else if n = 6 then some .replyObject + else if n = 13 then some (.archObject (.ext .stage2VSpaceObj)) + else if n = 14 then some (.archObject (.ext .highVSpaceObj)) + else (archDataToObjType (n - 7)).map .archObject + +/-- The size in bits of an object of type number `n` and user size `us`. -/ +def zOfA (n us : Nat) : Nat := + if n = 0 ∨ n = 5 then us else if n = 1 then 12 else if n = 2 then 4 else if n = 3 then 6 + else if n = 4 then 5 + us else if n = 6 then 5 else if n = 7 then 30 else if n = 10 then 21 + else if n = 13 ∨ n = 14 then 13 else 12 + +theorem objTypeOfA_none (n : Nat) : objTypeOfA n = none ↔ 15 ≤ n := by + constructor + · intro h + by_contra hn + unfold objTypeOfA archDataToObjType at h + interval_cases n <;> simp at h + · intro h + unfold objTypeOfA archDataToObjType + simp only [show ¬ n = 0 by omega, show ¬ n = 1 by omega, show ¬ n = 2 by omega, show ¬ n = 3 by omega, + show ¬ n = 4 by omega, show ¬ n = 5 by omega, show ¬ n = 6 by omega, show ¬ n = 13 by omega, + show ¬ n = 14 by omega, show ¬ n - 7 = 0 by omega, show ¬ n - 7 = 1 by omega, show ¬ n - 7 = 2 by omega, + show ¬ n - 7 = 3 by omega, show ¬ n - 7 = 4 by omega, show ¬ n - 7 = 5 by omega, ↓reduceIte, + Option.map_none] + +/-- The facts the decode reads about a type. -/ +theorem objTypeOfA_some {n : Nat} {t : ApiObjectType} (h : objTypeOfA n = some t) (us : Nat) : + n < 15 ∧ fl4ObjBitsApi t us = zOfA n us ∧ (isFrameType t = true ↔ n = 7 ∨ n = 9 ∨ n = 10) ∧ + (t = .untyped ↔ n = 0) ∧ (t = .capTableObject ↔ n = 4) ∧ (t = .schedContextObject ↔ n = 5) := by + have hn : n < 15 := by + by_contra hc; have := (objTypeOfA_none n).2 (by omega); rw [this] at h; cases h + refine ⟨hn, ?_⟩ + unfold objTypeOfA archDataToObjType at h + interval_cases n <;> simp at h <;> subst h <;> + simp [fl4ObjBitsApi, fl4TcbBits, objBitsApi, zOfA, objBits, archKobjSize, defaultArchObject, + ExtAObjectType.defaultObject, ExtArchObj.objSize, extObjBits, pageBitsForSize, tableSize, ptTranslationBits, config_ARM_PA_SIZE_BITS_40, + pteBits, wordSizeBits, pageBits, vcpuBits, isFrameType, archIsFrameType, slotBits, + endpointBits, ntfnBits, replyBits]; (try omega) + +section +variable {Z : Type} [StateExt Z] + +set_option linter.unusedSectionVars false in +/-- `data_to_obj_type` with FL4's types: one outcome, the state kept. -/ +theorem fl4DataToObjType_runA (x : Data) (σ : State Z) : + (fl4DataToObjType (Z := Z) x).run σ = + ({(match objTypeOfA x.toNat with | some t => .ok t | none => .error (.invalidArgument 0), σ)}, False) := by + unfold fl4DataToObjType dataToObjType objTypeOfA + simp only [ExtAObjectType.num, NonDetE.returnOk, ndE_pure_bindA, dataToNat, unat] + by_cases h13 : x.toNat = 13 + · simp [h13] + by_cases h14 : x.toNat = 14 + · simp [h14] + simp only [h13, h14, ↓reduceIte] + split_ifs <;> try rfl + cases h : archDataToObjType (x.toNat - 7) <;> rfl + +/-! ## The window and the reset flag -/ + +set_option linter.unusedSectionVars false in +/-- `mapME_x ensure_empty` over slots whose caps are known. -/ +theorem mapMEx_ensureEmpty_runA (σ : State Z) (caps : CslotPtr → Cap) : + ∀ (slots : List CslotPtr), (∀ sl ∈ slots, getCap (Z := Z) sl σ = ({(caps sl, σ)}, False)) → + (NonDetE.mapMEx (ensureEmpty (Z := Z)) slots).run σ = + ({(if ∀ sl ∈ slots, caps sl = .nullCap then .ok () else .error .deleteFirst, σ)}, False) + | [], _ => by simp [NonDetE.mapMEx] + | sl :: rest, h => by + unfold NonDetE.mapMEx + have h1 : (ensureEmpty (Z := Z) sl).run σ = + ({(if caps sl = .nullCap then .ok () else .error .deleteFirst, σ)}, False) := by + unfold ensureEmpty + rw [run_bind_okA (run_liftEA (h sl (by simp)))] + by_cases hn : caps sl = .nullCap + · simp only [NonDetE.whenE, hn, ne_eq, not_true_eq_false, ↓reduceIte]; rfl + · simp only [NonDetE.whenE, hn, ne_eq, not_false_eq_true, ↓reduceIte]; rfl + by_cases hn : caps sl = .nullCap + · rw [ite_eq_left hn] at h1 + rw [run_bind_okA h1, mapMEx_ensureEmpty_runA σ caps rest (fun x hx => h x (by simp [hx]))] + simp [hn] + · rw [ite_eq_right hn] at h1 + rw [run_bind_errorA h1] + simp [hn] + +set_option linter.unusedSectionVars false in +theorem ensureNoChildren_run_utA (σ : State Z) (sl : CslotPtr) : + (ensureNoChildren (Z := Z) sl).run σ = + ({(if ∃ c, σ.cdt c = some sl then .error .revokeFirst else .ok (), σ)}, False) := by + unfold ensureNoChildren NonDetE.whenE + rw [run_bind_okA (run_liftEA (m := NonDet.gets _) rfl)] + by_cases h : ∃ c, σ.cdt c = some sl + · rw [ite_eq_left h, ite_eq_left h]; rfl + · rw [ite_eq_right h, ite_eq_right h]; rfl + +/-- The decode's reset flag: whether the untyped has no children. -/ +theorem constOnFailure_runA (σ : State Z) (sl : CslotPtr) : + constOnFailure (Z := Z) false (do ensureNoChildren sl; NonDetE.returnOk true : SEMonad Z Bool) σ = + ({(decide (¬ ∃ x, σ.cdt x = some sl), σ)}, False) := by + unfold constOnFailure NonDetE.catch + have h := ensureNoChildren_run_utA σ sl + by_cases hx : ∃ x, σ.cdt x = some sl + · rw [ite_eq_left hx] at h + have e : (do ensureNoChildren sl; NonDetE.returnOk true : SEMonad Z Bool).run σ = + ({(.error .revokeFirst, σ)}, False) := run_bind_errorA h _ + refine (nd_bind_singleA' e _).trans ?_ + simp [hx]; rfl + · rw [ite_eq_right hx] at h + have e : (do ensureNoChildren sl; NonDetE.returnOk true : SEMonad Z Bool).run σ = + ({(.ok true, σ)}, False) := by rw [run_bind_okA h]; rfl + refine (nd_bind_singleA' e _).trans ?_ + simp [hx]; rfl + +end + +/-! ## Alignment across the pointer shift -/ + +theorem and_lowmaskA (x : BitVec 64) (z : Nat) (hz : z < 64) : + (x &&& (BitVec.ofNat 64 (2 ^ z) - 1)).toNat = x.toNat % 2 ^ z := by + have hp : 2 ^ z < 2 ^ 64 := Nat.pow_lt_pow_right (by decide) hz + have h1 : 1 ≤ 2 ^ z := Nat.one_le_two_pow + have hm : (BitVec.ofNat 64 (2 ^ z) - 1 : BitVec 64).toNat = 2 ^ z - 1 := by + rw [BitVec.toNat_sub, BitVec.toNat_ofNat, Nat.mod_eq_of_lt hp] + show (2 ^ 64 - 1 + 2 ^ z) % 2 ^ 64 = 2 ^ z - 1 + omega + rw [BitVec.toNat_and, hm, Nat.and_two_pow_sub_one_eq_mod] + +theorem and_not_eq_subA (x m : BitVec 64) : x &&& ~~~m = x - (x &&& m) := by + have h : (x &&& ~~~m) + (x &&& m) = x := by + rw [BitVec.add_eq_or_of_and_eq_zero] + · ext i hi + simp only [BitVec.getElem_or, BitVec.getElem_and, BitVec.getElem_not] + cases x[i] <;> cases m[i] <;> rfl + · ext i hi + simp only [BitVec.getElem_and, BitVec.getElem_not, BitVec.getElem_zero] + cases x[i] <;> cases m[i] <;> rfl + exact (BitVec.eq_sub_iff_add_eq).2 h + +theorem bv_add_sub_swapA (a b c : BitVec 64) : a + b - c = a - c + b := by + apply BitVec.eq_of_toNat_eq + have ha := a.isLt + have hb := b.isLt + have hc := c.isLt + simp only [BitVec.toNat_add, BitVec.toNat_sub] + omega + +theorem bv_add_swapA (a b c : BitVec 64) : a + b + c = a + c + b := by + rw [BitVec.add_assoc, BitVec.add_comm b, ← BitVec.add_assoc] + +/-- Rounding down to `2^z` commutes with adding a multiple of `2^z`. -/ +theorem and_not_add_shiftA (y S : BitVec 64) (z : Nat) (hz : z < 64) (hS : S.toNat % 2 ^ z = 0) : + (y + S) &&& ~~~(BitVec.ofNat 64 (2 ^ z) - 1) = (y &&& ~~~(BitVec.ofNat 64 (2 ^ z) - 1)) + S := by + have hm : (y + S) &&& (BitVec.ofNat 64 (2 ^ z) - 1) = y &&& (BitVec.ofNat 64 (2 ^ z) - 1) := by + apply BitVec.eq_of_toNat_eq + have hd : 2 ^ z ∣ 2 ^ 64 := Nat.pow_dvd_pow 2 (Nat.le_of_lt hz) + rw [and_lowmaskA _ z hz, and_lowmaskA _ z hz, BitVec.toNat_add, Nat.mod_mod_of_dvd _ hd, Nat.add_mod, hS, + Nat.add_zero, Nat.mod_mod] + rw [and_not_eq_subA, and_not_eq_subA y, hm] + exact bv_add_sub_swapA _ _ _ + +/-- **`align_up` commutes with the pointer shift up to size 39** (F2). -/ +theorem alignUp_shiftA (x : BitVec 64) (z : Nat) (hz : z ≤ 39) : + Fl4SpecA64.alignUp (x + pptrShiftA) z = Fl4SpecA64.alignUp x z + pptrShiftA := by + unfold Fl4SpecA64.alignUp + have hS : pptrShiftA.toNat % 2 ^ z = 0 := by + have h39 : 2 ^ 39 ∣ pptrShiftA.toNat := by rw [pptrShiftA_eq]; decide + exact Nat.mod_eq_zero_of_dvd (Nat.dvd_trans (Nat.pow_dvd_pow 2 hz) h39) + rw [show x + pptrShiftA + BitVec.ofNat 64 (2 ^ z) - 1 = (x + BitVec.ofNat 64 (2 ^ z) - 1) + pptrShiftA by + rw [bv_add_swapA x, bv_add_sub_swapA]] + exact and_not_add_shiftA _ _ z (by omega) hS + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtSysPlaceA.lean b/proofs/Fl4ProofsA64/P3/UtSysPlaceA.lean new file mode 100644 index 000000000..1d741d2a7 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtSysPlaceA.lean @@ -0,0 +1,408 @@ +/- +P3 (aarch64, A64-P3 Untyped, `UtResetA` / `UtMakePoolA`): **A64 P1's decode-site placement over the dropped heap** +(`SysPlace`, the premises `P'` / `hpl`). + +A childless untyped's block holds no live cap and no cap names an object in it, so every retained slot keeps its P1 +typing (`SlotOK`) over the dropped heap and slot set: its cap's objects are kept (`RngKeyA` caps are off the block), +the slots of a `CNode` / `Zombie` cap lie in the cap's own range, which a childless untyped does not meet +(`reset_offCov`), and its MDB links are null or name members of the tree, which are non-null and so retained. No merged +P1 statement changes. The extra caps of the new placement are all null. + +Named premise: +* [J] `UtZombieA`: a `Zombie`'s count fits its object (l4v `valid_cap`: `zombie_cte_num`). **The record cannot supply + it**: neither `CapWfA` nor `SlotTy` bounds the count, so the counted slots need not lie in the zombie's object. +-/ +import Fl4ProofsA64.P3.UtDropCA +import Fl4ProofsA64.P3.Stage3D1bA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P0.Consts Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {ar : machine.Arch A} + +/-! ## Caps naming an endpoint, notification or reply -/ + +section +variable (p b : U64) + +theorem _root_.Fl4ProofsA64.P1.RngKeyA.endpoint_ut (x y z w) : RngKeyA (.Endpoint p b x y z w) := by + have h := physA_eq (.Endpoint p b x y z w) + simp only [objects.cap_is_physical] at h + exact ⟨(okinj h).symm, rfl, rfl⟩ + +theorem _root_.Fl4ProofsA64.P1.RngKeyA.ntfn_ut (x y) : RngKeyA (.Notification p b x y) := by + have h := physA_eq (.Notification p b x y) + simp only [objects.cap_is_physical] at h + exact ⟨(okinj h).symm, rfl, rfl⟩ + +theorem _root_.Fl4ProofsA64.P1.RngKeyA.reply_ut (g) : RngKeyA (.Reply p g) := by + have h := physA_eq (.Reply p g) + simp only [objects.cap_is_physical] at h + exact ⟨(okinj h).symm, rfl, rfl⟩ + +theorem ptrA_endpoint_ut (x y z w) : ptrA (.Endpoint p b x y z w) = p := by + have h := ptrA_eq (.Endpoint p b x y z w); simp only [capability.Cap.ptr] at h; exact (okinj h).symm + +theorem ptrA_ntfn_ut (x y) : ptrA (.Notification p b x y) = p := by + have h := ptrA_eq (.Notification p b x y); simp only [capability.Cap.ptr] at h; exact (okinj h).symm + +theorem ptrA_reply_ut (g) : ptrA (.Reply p g) = p := by + have h := ptrA_eq (.Reply p g); simp only [capability.Cap.ptr] at h; exact (okinj h).symm + +end + +/-- A `Zombie` cap covers its first slot and its counted slots. -/ +theorem utZombie_covA {p t n i : U64} + (hb : (t = capability.ZOMBIE_TCB → n.val ≤ 7) ∧ (t ≠ capability.ZOMBIE_TCB → t.val < 64 ∧ n.val ≤ 2 ^ t.val)) + (hreg : baseA (.Zombie p t n) ≤ topA (.Zombie p t n)) (hfit : (slotIdx p i).val + 32 ≤ 2 ^ 64) + (hi : i.val < n.val ∨ i.val = 0) : W7.R.ST.CovBy (.Zombie p t n) (slotIdx p i) := by + by_cases ht : t = capability.ZOMBIE_TCB + · have h7 : i.val < 7 := by + rcases hi with h | h + · exact Nat.lt_of_lt_of_le h (hb.1 ht) + · omega + subst ht + refine ⟨W7.R.ST.physA_zombie _ _ _, rfl, ?_⟩ + unfold baseA topA at hreg ⊢ + rw [ptrA_zombie, W7.R.ST.sizeA_zombie65] at hreg ⊢ + have hbits : (fl4_abi.consts.bits.TCB : U64).val = 12 := by simp [fl4_abi.consts.bits.TCB] + have hm : (mskA fl4_abi.consts.bits.TCB).val = 4095 := by + rw [mskA_val (by omega), hbits] + rfl + have htop : (wa p (mskA fl4_abi.consts.bits.TCB)).val = (p.val + 4095) % 2 ^ 64 := by + rw [wa_valP, hm] + have hsl : (slotIdx p i).val = (p.val + (i.val * 32) % 2 ^ 64) % 2 ^ 64 := by + unfold slotIdx + simp [core.num.U64.wrapping_add_val_eq, core.num.U64.wrapping_mul_val_eq, U64.size_def, U64.numBits] + have hpv : p.val < 2 ^ 64 := by have := p.hBounds; simpa [U64.size_def, U64.numBits] using this + rw [htop] at hreg ⊢ + rw [hsl] + omega + · obtain ⟨h64, hn⟩ := hb.2 ht + have hreg' : baseA (.CNode p t 0#u64 0#u64) ≤ topA (.CNode p t 0#u64 0#u64) := by + unfold baseA topA at hreg ⊢ + rw [ptrA_zombie, W7.R.ST.sizeA_zombieT p t n ht] at hreg + rw [ptrA_cnode, sizeA_cnode] + exact hreg + have hi' : i.val < 2 ^ (t.val % 64) := by + rw [Nat.mod_eq_of_lt h64] + rcases hi with h | h + · omega + · rw [h]; exact Nat.two_pow_pos _ + exact W7.R.ST.CovBy.same (c := .CNode p t 0#u64 0#u64) (d := .Zombie p t n) + (by simp only [W7.R.ST.physA_zombie, W7.R.ST.physA_cnode]) rfl (by simp only [ptrA_zombie, ptrA_cnode]) + (by simp only [W7.R.ST.sizeA_zombieT p t n ht, sizeA_cnode]) (W7.R.ST.cn_cov hreg' h64 hi' hfit) + +theorem utSlot0A (p : U64) : slotIdx p 0#u64 = p := by + apply UScalar.eq_of_val_eq + unfold slotIdx + have hpv : p.val < 2 ^ 64 := by have := p.hBounds; simpa [U64.size_def, U64.numBits] using this + simp [core.num.U64.wrapping_add_val_eq, core.num.U64.wrapping_mul_val_eq, U64.size_def, U64.numBits] + +/-! ## Words off the block -/ + +section +variable {H : Heap} {Sg Sg' : Set U64} {lo hi : Nat} + +/-- A cap-slot word of a TCB or of `Ω` that is off the block stays one over the dropped heap. -/ +theorem utWord_keptA (hR : RegionIn H lo hi) {b : U64} (hoff : b.val < lo ∨ hi ≤ b.val) + {q : U64} {k : HKind} (hq : H q = some k) (h1 : q.val ≤ b.val) (h2 : b.val < q.val + 2 ^ k.bits) : + ¬ RegionD H lo hi q := by + intro hD + have hD' : MeetsR lo hi q.val (q.val + 2 ^ k.bits) := by simpa [RegionD, hq, Option.elim] using hD + obtain ⟨g1, g2⟩ := hR q k hq hD' + omega + +theorem utSlotW_keptA (hH : HeapOK H) (hR : RegionIn H lo hi) (hSg : Sg' ⊆ Sg) {x : U64} + (hf : x.val + 32 ≤ 2 ^ 64) (hoff : x.val + 32 ≤ lo ∨ hi ≤ x.val) + (h : W7.SlotW H.tcbs H.omega Sg x) : + W7.SlotW (H.drop (RegionD H lo hi)).tcbs (H.drop (RegionD H lo hi)).omega Sg' x := by + refine ⟨h.1, fun o ho h8 => ?_⟩ + have hv : (wa x o).val = x.val + o.val := wv hf ho + have off : (wa x o).val < lo ∨ hi ≤ (wa x o).val := by omega + rcases h.2 o ho h8 with ⟨t, ht, K, hK, e⟩ | ⟨⟨ha, p, k, hp, hk, hin⟩, hn⟩ + · have ht' : H t = some .tcb := ht + have hf' := hH.fits t .tcb ht' + simp only [HKind.bits] at hf' + have hv' : (wa t K).val = t.val + K.val := by + simp [core.num.U64.wrapping_add_val_eq, U64.size_def, U64.numBits]; omega + refine Or.inl ⟨t, Heap.mem_tcbs_drop.2 ⟨ht, utWord_keptA hR off ht' (by rw [e, hv']; omega) + (by rw [e, hv']; simp only [HKind.bits]; omega)⟩, K, hK, e⟩ + · exact Or.inr ⟨⟨ha, p, k, Heap.drop_eq_some.2 ⟨utWord_keptA hR off hp hin.1 hin.2, hp⟩, hk, hin⟩, + fun g => hn (hSg g)⟩ + +theorem utCapSlotW_keptA (hH : HeapOK H) (hR : RegionIn H lo hi) (hSg : Sg' ⊆ Sg) {x : U64} + (hf : x.val + 32 ≤ 2 ^ 64) (hoff : x.val + 32 ≤ lo ∨ hi ≤ x.val) + (h : CapSlotW H.tcbs H.omega Sg x) : + CapSlotW (H.drop (RegionD H lo hi)).tcbs (H.drop (RegionD H lo hi)).omega Sg' x := by + refine ⟨h.1, fun o ho h8 => ?_⟩ + have hv : (wa x o).val = x.val + o.val := wv hf ho + have off : (wa x o).val < lo ∨ hi ≤ (wa x o).val := by omega + rcases h.2 o ho h8 with ⟨t, ht, K, hK, e⟩ | ⟨⟨ha, p, k, hp, hk, hin⟩, hn⟩ + · have ht' : H t = some .tcb := ht + have hf' := hH.fits t .tcb ht' + simp only [HKind.bits] at hf' + have hv' : (wa t K).val = t.val + K.val := by + simp [core.num.U64.wrapping_add_val_eq, U64.size_def, U64.numBits]; omega + refine Or.inl ⟨t, Heap.mem_tcbs_drop.2 ⟨ht, utWord_keptA hR off ht' (by rw [e, hv']; omega) + (by rw [e, hv']; simp only [HKind.bits]; omega)⟩, K, hK, e⟩ + · exact Or.inr ⟨⟨ha, p, k, Heap.drop_eq_some.2 ⟨utWord_keptA hR off hp hin.1 hin.2, hp⟩, hk, hin⟩, + fun g => hn (hSg g)⟩ + +end + +/-- A cap-slot word list in the kernel window does not wrap: a typed slot fits the address space. -/ +theorem utSlot_fitA {T Ω Sg : Set U64} (W : WinOK T Ω) {p : U64} (h : W7.SlotW T Ω Sg p) : p.val + 32 ≤ 2 ^ 64 := by + by_contra hc + have hpv : p.val < 2 ^ 64 := by have := p.hBounds; simpa [U64.size_def, U64.numBits] using this + have ha : p.val % 8 = 0 := h.1 + have hv : (wa p 24#u64).val = p.val + 24 - 2 ^ 64 := by + have e : (24#u64 : U64).val = 24 := rfl + rw [wa_valP, e] + omega + have hb : (141287244169216 : Nat) ≤ (layout.PPTR_BASE : U64).val := by + simp [layout.PPTR_BASE] + rcases h.2 24#u64 (by decide) (by decide) with ⟨t, ht, K, hK, e⟩ | ⟨hm, -⟩ + · have g := W.tcb t ht + have : (wa p 24#u64).val = t.val + K.val := by + rw [e, wa_valP] + have htop : (layout.PPTR_TOP : U64).val < 2 ^ 64 := by + simp + exact Nat.mod_eq_of_lt (by omega) + omega + · have g := W.omega _ hm + omega + +/-! ## The placement -/ + +variable {H : Heap} {Cs Rq Nt OS : Set U64} {s : S} {ex : Aeneas.Std.Array obj.Slot 3#usize} {x p sb fi : U64} + +set_option maxHeartbeats 2000000 in +/-- **`SysPlace` over the dropped heap and slot set** (module docstring). -/ +theorem utSysPlace_dropA + (R : UtDecP st H Cs H.frame Rq Nt OS s ex x p sb fi false) + (P : SysPlace st H Cs H.frame Rq Nt OS s ex x) + (he : cdt.ensure_no_children st s x = ok (.Ok ())) + {G : List (List U64)} {par : U64 → Option U64} (htr : TreeA st OS G par s) + (hfit : (L.ksb s).val + ksSizeA ≤ 2 ^ 64) + (hoff : p.val + 2 ^ sb.val ≤ (L.ksb s).val ∨ (L.ksb s).val + 223488 ≤ p.val) + (zb : UtZombieA st OS s) : + SysPlace st (H.drop (RegionD H p.val (p.val + 2 ^ sb.val))) Cs + (H.drop (RegionD H p.val (p.val + 2 ^ sb.val))).frame + {r | r ∈ Rq ∧ ¬ (p.val ≤ r.val ∧ r.val < p.val + 2 ^ sb.val)} + {r | r ∈ Nt ∧ ¬ (p.val ≤ r.val ∧ r.val < p.val + 2 ^ sb.val)} + (OffR p.val (p.val + 2 ^ sb.val) OS) s (Array.repeat 3#usize 0#u64) x := by + set lo := p.val with hlo + set hi := p.val + 2 ^ sb.val with hhi + have hcx : capA st s x = .Untyped p sb fi false := R.cap + have hH : HeapOK H := R.heap + have hRin : RegionIn H lo hi := R.ur.rin R.src R.cap + have hrev : obj.Slot.revocable st x s = ok true := R.stz.1.2.1 x R.src (by rw [hcx]; rfl) + have hnd : ∀ y, ¬ Fl4Proofs.P3.Mdb.Desc par y x := cdt.ensure_no_children.noDesc R.os htr R.src hcx hrev he + have hnk : ∀ c ∈ OS, RngKeyA (capA st s c) → + ¬ (lo ≤ (ptrA (capA st s c)).val ∧ (ptrA (capA st s c)).val < hi) := + cdt.ensure_no_children.noKey R.os htr.utRangeOrd R.stz.1.2.1 R.src hcx he + have goneR : ∀ y ∈ OS, lo ≤ y.val → y.val < hi → capA st s y = .Null := + utClos_goneNullA htr.ut R.cz R.src hcx hnd hfit hoff + have hfr : (H.drop (RegionD H lo hi)).frame ⊆ H.frame := by + rintro b ⟨al, q, k, h, rest⟩ + exact ⟨al, q, k, (Heap.drop_eq_some.1 h).2, rest⟩ + have hom : (H.drop (RegionD H lo hi)).omega ⊆ H.omega := by + rintro b ⟨al, q, k, h, rest⟩ + exact ⟨al, q, k, (Heap.drop_eq_some.1 h).2, rest⟩ + -- a covered slot is retained + have covOff : ∀ z ∈ OS, ∀ y ∈ OS, W7.R.ST.CovBy (capA st s z) y → y ∈ OffR lo hi OS := fun z hz y hy hc => + ⟨hy, reset_offCov htr.ut R.src hcx hnd hz hc⟩ + -- non-null slots are retained + have nonnull : ∀ y ∈ OS, capA st s y ≠ .Null → y ∈ OffR lo hi OS := by + intro y hy hne + by_cases hirq : IslN (L.ksb s) y + · obtain ⟨j, hj, rfl⟩ := hirq + refine ⟨hy, ?_⟩ + rw [irqSlotA_val hfit hj] + omega + obtain ⟨z, hz, hc⟩ := R.cz y hy hirq hne + exact covOff z hz y hy hc.1 + -- a kept slot is typed over the dropped heap + have slotT : ∀ y ∈ OffR lo hi OS, W7.SlotW (H.drop (RegionD H lo hi)).tcbs (H.drop (RegionD H lo hi)).omega + (H.drop (RegionD H lo hi)).frame y := fun y hy => + utSlotW_keptA hH hRin hfr (R.os.fit y hy.1) hy.2 (R.os.slot y hy.1) + -- a heap object whose base is off the block is kept + have keptObj : ∀ q k, H q = some k → ¬ (lo ≤ q.val ∧ q.val < hi) → (H.drop (RegionD H lo hi)) q = some k := by + intro q k hq hb + refine Heap.drop_eq_some.2 ⟨fun hD => hb ?_, hq⟩ + have hD' : MeetsR lo hi q.val (q.val + 2 ^ k.bits) := by simpa [RegionD, hq, Option.elim] using hD + obtain ⟨g1, g2⟩ := hRin q k hq hD' + have := Nat.two_pow_pos k.bits + omega + -- the MDB + obtain ⟨G', par', hM, -, -, hN, -⟩ := R.stz.1.1 + have member : ∀ n ∈ G'.flatten, n ∈ OffR lo hi OS := fun n hn => by + obtain ⟨h1, h2⟩ := (hM.members n).1 hn + exact nonnull n h1 h2 + have links : ∀ y ∈ OS, LinkOK (OffR lo hi OS) (L.mem s (wa y 16#u64)) ∧ + LinkOK (OffR lo hi OS) (NxA st s y) := by + intro y hy + by_cases hn : capA st s y = .Null + · obtain ⟨h1, h2⟩ := hN y hy hn + exact ⟨Or.inl h1, Or.inl h2⟩ + have hm : y ∈ G'.flatten := (hM.members y).2 ⟨hy, hn⟩ + obtain ⟨ch, hch, hyc⟩ := List.mem_flatten.1 hm + have inG : ∀ a ∈ ch, a ∈ G'.flatten := fun a ha => List.mem_flatten.2 ⟨ch, hch, ha⟩ + obtain ⟨i, hi', e⟩ := List.mem_iff_getElem.1 hyc + have ei : ch[i]? = some y := by rw [List.getElem?_eq_getElem hi', e] + constructor + · cases i with + | zero => exact Or.inl (hM.head ch hch y (by rw [List.head?_eq_getElem?]; exact ei)) + | succ j => + have ej : ch[j]? = some ch[j] := List.getElem?_eq_getElem (by omega) + rw [(hM.chain ch hch j _ y ej ei).2] + exact Or.inr (member _ (inG _ (List.getElem_mem _))) + · by_cases hl : i + 1 < ch.length + · have ej : ch[i + 1]? = some ch[i + 1] := List.getElem?_eq_getElem hl + rw [(hM.chain ch hch i y _ ei ej).1] + exact Or.inr (member _ (inG _ (List.getElem_mem _))) + · refine Or.inl (hM.last ch hch y ?_) + rw [List.getLast?_eq_getElem?, show ch.length - 1 = i by omega] + exact ei + refine + { heap := hH.drop + os := ?os + win := ⟨fun t ht => P.win.tcb t (Heap.mem_tcbs_drop.1 ht).1, fun b hb => P.win.omega b (hom hb)⟩ + ty := ?ty + core0 := P.core0 + ext := ?ext + src := ?src + slotw := ?slotw + wu := fun y hy => P.wu y hy.1 } + case os => + exact ⟨slotT, fun y hy => R.os.fit y hy.1, fun y hy z hz => R.os.disj y hy.1 z hz.1, fun y hy => R.os.nz y hy.1⟩ + case ext => + intro y hy hne + simp [Array.repeat] at hy + exact absurd hy hne + case src => + exact nonnull x R.src (by rw [hcx]; intro h; cases h) + case slotw => + have h := nonnull x R.src (by rw [hcx]; intro g; cases g) + exact utCapSlotW_keptA hH hRin hfr (R.os.fit x R.src) h.2 P.slotw + case ty => + intro y hy + obtain ⟨hcap, -, -⟩ := P.ty y hy.1 + obtain ⟨l1, l2⟩ := links y hy.1 + refine ⟨fun c hc => ?_, l1, fun n hn => ?_⟩ + swap + · have e : obj.sext48 (L.mem s (wa y 24#u64)) = ok (NxA st s y) := sext48_eq _ + rw [e] at hn + rw [← okinj hn] + exact l2 + have ec : capA st s y = c := by + have h := capA_eq (st := st) s y + rw [DPF.slot_cap_dec] at h + exact (okinj (h.symm.trans hc)) + obtain ⟨⟨⟨hep, hnt, hrp⟩, hirq, hio, hcn, hzm, hth⟩, hin⟩ := hcap c hc + have off : RngKeyA c → ¬ (lo ≤ (ptrA c).val ∧ (ptrA c).val < hi) := fun k => by + have := hnk y hy.1 (ec ▸ k) + rwa [ec] at this + have key : ∀ q, RngKeyA c → ptrA c = q → ¬ (lo ≤ q.val ∧ q.val < hi) := fun q k e => e ▸ off k + have cnS : ∀ q r g b, c = .CNode q r g b → ∀ i : U64, i.val < 2 ^ (r.val % 64) → slotIdx q i ∈ OffR lo hi OS := by + intro q r g b e i hi' + have hm : slotIdx q i ∈ OS := hin.1 q r g b e i hi' + have hr : r.val < 64 := W7.R.ST.decode_rad (e ▸ hc) + have hreg := htr.ut.reg y hy.1 (by rw [ec, e]; exact W7.R.ST.physA_cnode _ _ _ _) + rw [ec, e] at hreg + exact covOff y hy.1 _ hm (by rw [ec, e]; exact W7.R.ST.cn_cov hreg hr hi' (R.os.fit _ hm)) + have zmS : ∀ q t m, c = .Zombie q t m → ∀ i : U64, i.val < m.val ∨ i.val = 0 → slotIdx q i ∈ OS → + (slotIdx q i).val + 32 ≤ 2 ^ 64 → slotIdx q i ∈ OffR lo hi OS := by + intro q t m e i hi' hm hf + have hreg := htr.ut.reg y hy.1 (by rw [ec, e]; exact W7.R.ST.physA_zombie _ _ _) + rw [ec, e] at hreg + exact covOff y hy.1 _ hm (by rw [ec, e]; exact utZombie_covA (zb y hy.1 q t m (ec.trans e)) hreg hf hi') + refine ⟨⟨⟨?_, ?_, ?_⟩, hirq, hio, ?_, ?_, ?_⟩, ?_, ?_⟩ + · intro q b z1 z2 z3 z4 e + exact keptObj q _ (hep q b z1 z2 z3 z4 e) (key q (by rw [e]; exact RngKeyA.endpoint_ut _ _ _ _ _ _) + (by rw [e]; exact ptrA_endpoint_ut _ _ _ _ _ _)) + · intro q b z1 z2 e + have h := hnt q b z1 z2 e + have o := key q (by rw [e]; exact RngKeyA.ntfn_ut _ _ _ _) (by rw [e]; exact ptrA_ntfn_ut _ _ _ _) + exact ⟨⟨h.1, o⟩, keptObj q _ h.2 o⟩ + · intro q g e + exact ⟨hrp q g e, key q (by rw [e]; exact RngKeyA.reply_ut _ _) (by rw [e]; exact ptrA_reply_ut _ _)⟩ + · intro q r g b e + have h0 : (0#u64 : U64).val < 2 ^ (r.val % 64) := by + simp + have s0 := cnS q r g b e 0#u64 h0 + rw [utSlot0A] at s0 + exact ⟨slotT q s0, fun i hi' => slotT _ (cnS q r g b e i hi')⟩ + · intro q t m e + obtain ⟨w0, wi⟩ := hzm q t m e + have f0 : q.val + 32 ≤ 2 ^ 64 := utSlot_fitA P.win w0 + have slots : ∀ i : U64, i.val < m.val → slotIdx q i ∈ OffR lo hi OS := fun i hi' => + have hm := hin.2 q t m e i hi' + zmS q t m e i (Or.inl hi') hm (R.os.fit _ hm) + refine ⟨?_, fun i hi' => slotT _ (slots i hi')⟩ + have hreg := htr.ut.reg y hy.1 (by rw [ec, e]; exact W7.R.ST.physA_zombie _ _ _) + rw [ec, e] at hreg + have c0 := utZombie_covA (i := 0#u64) (zb y hy.1 q t m (ec.trans e)) hreg (by rw [utSlot0A]; exact f0) + (Or.inr rfl) + rw [utSlot0A] at c0 + have o0 := reset_offCov htr.ut R.src hcx hnd hy.1 (y := q) (by rw [ec, e]; exact c0) + exact utSlotW_keptA hH hRin hfr f0 o0 w0 + · intro q e + have h : q ∈ H.tcbs := hth q e + exact keptObj q _ h (key q (by rw [e]; exact RngKeyA.thread _) (by rw [e]; exact ptrA_thread _)) + · intro q r g b e i hi' + exact cnS q r g b e i hi' + · intro q t m e i hi' + have hm := hin.2 q t m e i hi' + exact zmS q t m e i (Or.inl hi') hm (R.os.fit _ hm) + +/-! ## At the record -/ + +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {a : A} {σ : State Unit} {m : McState} {Rq Nt : Set U64} {x : U64} + +/-- **`SysPlace` over the record's dropped heap and slot set** (`utSysPlace_dropA` at the decode site; `heapB` and +`slotsB` are `UtDropC`'s). -/ +theorem utSysPlace_recA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + {Ud Acd : Set U64} + (Dp : DecPre st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ud Acd s (.Untyped p sb fi false) ex) + (P : SysPlace st H Z.Cs H.frame Rq Nt OS s ex x) + (hcap : obj.Slot.cap st x s = ok (.Untyped p sb fi false)) + (he : cdt.ensure_no_children st s x = ok (.Ok ())) + (zb : UtZombieA st OS s) {D : Set ObjRef} + (heapB : H.drop (RegionD H p.val (p.val + 2 ^ sb.val)) = dropHA D H) + (slotsB : OffR p.val (p.val + 2 ^ sb.val) OS = dropSetA D OS) : + ∃ Rq' Nt' : Set U64, + SysPlace st (dropHA D H) Z.Cs (dropHA D H).frame Rq' Nt' (dropSetA D OS) s (Array.repeat 3#usize 0#u64) x := by + have hcx : capA st s x = .Untyped p sb fi false := okinj ((capA_eq s x).symm.trans hcap) + have R : UtDecP st H Z.Cs H.frame Rq Nt OS s ex x p sb fi false := + UtDecP.of_live Dp J.sys.sysl.sys P hcap J.sys.sysl.ln J.sys.sysl.vr J.sys.sysl.ly + have hsub : ∀ y, IsSlotA Z.th y → y ∈ OS := J.j5.2.1 + have hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null := J.j5.2.2 + have htr : TreeA st OS Z.G.chains Z.G.par s := J.j.1.cdt.tree.grow hsub fun y hy hn => hnul y hy hn + have hks : (L.ksb s).val + 788736 ≤ p.val ∨ p.val + 2 ^ sb.val ≤ (L.ksb s).val := + (R.stz.2.2 x R.src p sb fi hcx).2 + have hoff : p.val + 2 ^ sb.val ≤ (L.ksb s).val ∨ (L.ksb s).val + 223488 ≤ p.val := by + rcases hks with g | g + · exact Or.inr (by omega) + · exact Or.inl g + have h := utSysPlace_dropA R P he htr J.j.1.ksFit hoff zb + rw [heapB, slotsB] at h + exact ⟨_, _, h⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtTcbCreateA.lean b/proofs/Fl4ProofsA64/P3/UtTcbCreateA.lean new file mode 100644 index 000000000..ff221149f --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtTcbCreateA.lean @@ -0,0 +1,495 @@ +/- +P3 (aarch64, A64-P3 Untyped W3 leaf): **the words `objects::create_object` leaves in a new TCB**, so that the TCB +witnesses of `UtTcbScA` no longer assume them. + +* **`ut_initContextA`**: `regs::init_context` writes only the 37 register words: all zero but `SPSR_EL1 = 0x140`. +* **`ut_childNameA`**: `set_child_name` writes only the child's eight name words. +* **`ut_createTcbA`**: `create_object` of a TCB writes only the registers, domain, name and debug-list words of the new + TCB, node 0's debug-list head, and the old head's `DEBUG_PREV`; the registers satisfy `UtRegsA` and the domain word + is the kernel's `CUR_DOMAIN`. +* **`ut_createTcb_relA`**: from a zeroed TCB, the new TCB matches `default_tcb` on both sides (`TcbRelE`, `TcbRel1`). + +No named premise. Hypotheses: the TCB's placement; its affinity word zero; node 0's `DEBUG_TCBS` word and the kernel's +`CUR_DOMAIN` word are outside the new TCB, as is the old list head's `DEBUG_PREV`. New lemmas only. +-/ +import Fl4ProofsA64.P3.UtTcbScA +import Fl4ProofsA64.P3.VsRelA +import Fl4ProofsA64.P1.MdbVUC +import Fl4ProofsA64.P1.QueueDecUt +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4Spec Fl4SpecA64 + Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-- A write inside the footprint keeps it. -/ +theorem ut_wr_writesA {s0 s : S} {W : U64 → Prop} (h : WritesIn st s0 s W) (p v : U64) (hp : Aligned p) (hW : W p) : + WritesIn st s0 (L.wr s p v) W := + ⟨by rw [L.ksb_wr, h.1], by rw [L.cpu_wr, h.2.1], fun b hb hnb => by + rw [L.mem_wr s p v b hp hb, ite_eq_right (fun e => hnb (by rw [← e]; exact hW)), h.2.2 b hb hnb]⟩ + +theorem ut_tcbWordA {t c : U64} (ht : TcbAddr t) (hc : c.val < 4096) : (wa t c).val = t.val + c.val := by + have := ht.fits + rw [wa_valP] + exact Nat.mod_eq_of_lt (by omega) + +theorem ut_set_regA (s : S) (t i v : U64) : obj.Tcb.set_reg st t s i v = ok (L.wr s (regA t i) v) := by + unfold obj.Tcb.set_reg regA + simp only [lift, bind_ok, L.write_eq] + +/-- **`regs::init_context`** (module docstring). -/ +theorem ut_initContextA (s : S) (t : U64) (ht : TcbAddr t) : + regs.init_context st s t ⦃ s' => WritesIn st s s' (fun b => ∃ i : U64, i.val < 37 ∧ b = regA t i) ∧ + ∀ i : U64, i.val < 37 → L.mem s' (regA t i) = if i.val = 33 then 320#u64 else 0#u64 ⦄ := by + have hal : ∀ i : U64, i.val < 37 → Aligned (regA t i) := by + intro i hi + have := ht.align + show _ % 8 = 0 + rw [regA_val ht hi] + omega + have hrd : ∀ (s2 : S) (i j v : U64), i.val < 37 → j.val < 37 → + L.mem (L.wr s2 (regA t i) v) (regA t j) = if i.val = j.val then v else L.mem s2 (regA t j) := by + intro s2 i j v hi hj + rw [L.mem_wr s2 _ v _ (hal i hi) (hal j hj)] + have : regA t i = regA t j ↔ i.val = j.val := by + constructor + · intro e + have := congrArg UScalar.val e + rw [regA_val ht hi, regA_val ht hj] at this + omega + · intro e + rw [UScalar.eq_of_val_eq e] + simp only [this] + have hcnt : (layout.reg.COUNT).val = 37 := by simp [layout.reg.COUNT] + have hloop : regs.init_context_loop st s t 0#u64 ⦃ s' => + WritesIn st s s' (fun b => ∃ i : U64, i.val < 37 ∧ b = regA t i) ∧ + ∀ j : U64, j.val < 37 → L.mem s' (regA t j) = 0#u64 ⦄ := by + unfold regs.init_context_loop + refine loop.spec_decr_nat (measure := fun x => 37 - x.2.val) + (inv := fun x => x.2.val ≤ 37 ∧ WritesIn st s x.1 (fun b => ∃ i : U64, i.val < 37 ∧ b = regA t i) ∧ + ∀ j : U64, j.val < x.2.val → L.mem x.1 (regA t j) = 0#u64) _ _ _ ?_ + ⟨by simp, WritesIn.refl _ _, fun j hj => absurd hj (by simp)⟩ + rintro ⟨s2, i⟩ ⟨hi, hw, hm⟩ + simp only at hi hw hm ⊢ + unfold regs.init_context_loop.body + by_cases hit : i < layout.reg.COUNT + · rw [ite_eq_left hit] + have hit' : i.val < 37 := by have : i.val < layout.reg.COUNT.val := hit; omega + obtain ⟨i1, h1e, h1v⟩ := spec_exists_dut (U64.add_spec (x := i) (y := 1#u64) (by scalar_tac)) + have h1 : i1.val = i.val + 1 := by simpa using h1v + rw [ut_set_regA, bind_ok, h1e, bind_ok, WP.spec_ok] + refine ⟨⟨show i1.val ≤ 37 by omega, ut_wr_writesA hw _ _ (hal i hit') ⟨i, hit', rfl⟩, fun j hj => ?_⟩, + show 37 - i1.val < 37 - i.val by omega⟩ + have hj' : j.val < i.val + 1 := h1 ▸ hj + show L.mem (L.wr s2 (regA t i) 0#u64) (regA t j) = 0#u64 + rw [hrd s2 i j _ hit' (by omega)] + by_cases h : i.val = j.val + · rw [ite_eq_left h] + · rw [ite_eq_right h] + exact hm j (by omega) + · rw [ite_eq_right hit, WP.spec_ok] + have : i.val = 37 := by have : ¬ i.val < layout.reg.COUNT.val := hit; omega + exact ⟨hw, fun j hj => hm j (by omega)⟩ + obtain ⟨s1, e1, hw1, hm1⟩ := spec_exists_dut hloop + obtain ⟨p, ep, hp⟩ := spec_exists_dut Fl4ProofsA64.P0.Consts.regs.PSTATE_USER.spec + have h33 : (layout.reg.SPSR_EL1).val = 33 := by simp [layout.reg.SPSR_EL1] + unfold regs.init_context + rw [e1, bind_ok, ep, bind_ok, ut_set_regA, WP.spec_ok] + refine ⟨ut_wr_writesA hw1 _ _ (hal _ (by omega)) ⟨_, by omega, rfl⟩, fun j hj => ?_⟩ + rw [hrd s1 _ j _ (by omega) hj, h33, hm1 j hj, hp] + by_cases h : j.val = 33 + · rw [ite_eq_left h.symm, ite_eq_left h] + · rw [ite_eq_right (Ne.symm h), ite_eq_right h] + +/-- **`set_child_name` writes only the child's name words** (module docstring). -/ +theorem ut_childNameA (s : S) (child parent : U64) (ht : TcbAddr child) : + objects.set_child_name st s child parent ⦃ s' => + WritesIn st s s' (fun b => ∃ w : U64, w.val < 8 ∧ b = wa child ⟨BitVec.ofNat 64 (3600 + 8 * w.val)⟩) ⦄ := by + have hS : Fl4ProofsA64.P0.StoreTotal st := L.total + unfold objects.set_child_name + rw [ite_eq_left (show fl4_abi.config.DEBUG_BUILD = true by simp [fl4_abi.config.DEBUG_BUILD])] + unfold obj.Tcb.is_null + simp only [bind_ok] + split + · exact (WP.spec_ok _).2 (WritesIn.refl _ _) + obtain ⟨len, elen, -⟩ := spec_exists_dut (Fl4ProofsA64.P0.objects.set_child_name_loop0.p0 st s parent 0#u64 hS) + rw [elen, bind_ok] + unfold objects.set_child_name_loop1 + refine loop.spec_decr_nat (measure := fun x => 8 - x.2.val) + (inv := fun x => x.2.val ≤ 8 ∧ WritesIn st s x.1 + (fun b => ∃ w : U64, w.val < 8 ∧ b = wa child ⟨BitVec.ofNat 64 (3600 + 8 * w.val)⟩)) _ _ _ ?_ + ⟨by simp, WritesIn.refl _ _⟩ + rintro ⟨s2, w⟩ ⟨hw8, hw⟩ + simp only at hw8 hw ⊢ + unfold objects.set_child_name_loop1.body + by_cases hlt : w < 8#u64 + · rw [ite_eq_left hlt] + have hlt' : w.val < 8 := hlt + obtain ⟨v, ev, -⟩ := spec_exists_dut + (Fl4ProofsA64.P0.objects.set_child_name_loop1_loop0.p0 st s2 parent len w 0#u64 0#u64 hS) + obtain ⟨na, ena, hna⟩ := spec_exists_dut Fl4ProofsA64.P0.Consts.layout.tcb.NAME.spec + have hk : (⟨BitVec.ofNat 64 (3600 + 8 * w.val)⟩ : U64).val = 3600 + 8 * w.val := by + show (BitVec.ofNat 64 (3600 + 8 * w.val)).toNat = _ + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)] + have hadr : core.num.U64.wrapping_add (core.num.U64.wrapping_add child 3600#u64) + (core.num.U64.wrapping_mul w 8#u64) = wa child ⟨BitVec.ofNat 64 (3600 + 8 * w.val)⟩ := by + have := ht.fits + have h36 : (3600#u64 : U64).val = 3600 := rfl + have hm : (core.num.U64.wrapping_mul w 8#u64).val = 8 * w.val := wm8_valA (by omega) + apply UScalar.eq_of_val_eq + rw [ut_tcbWordA ht (by rw [hk]; omega), hk, wa_valP, ut_tcbWordA ht (by rw [h36]; omega), h36, hm, + Nat.mod_eq_of_lt (by omega)] + omega + have halw : Aligned (wa child ⟨BitVec.ofNat 64 (3600 + 8 * w.val)⟩) := by + have := ht.align + show _ % 8 = 0 + rw [ut_tcbWordA ht (by rw [hk]; omega), hk] + omega + obtain ⟨w1, h1e, h1v⟩ := spec_exists_dut (U64.add_spec (x := w) (y := 1#u64) (by scalar_tac)) + have h1 : w1.val = w.val + 1 := by simpa using h1v + rw [ev, bind_ok] + unfold obj.Tcb.name_addr + rw [ena, bind_ok] + simp only [lift, bind_ok] + rw [hna, hadr, L.write_eq, bind_ok, h1e, bind_ok, WP.spec_ok] + exact ⟨⟨show w1.val ≤ 8 by omega, ut_wr_writesA hw _ _ halw ⟨w, hlt', rfl⟩⟩, + show 8 - w1.val < 8 - w.val by omega⟩ + · rw [ite_eq_right hlt, WP.spec_ok] + exact hw + +/-- The words of the new TCB at `t` that `create_object` writes: registers, domain, name, debug-list links. -/ +def utTcbInitA (t b : U64) : Prop := + (∃ i : U64, i.val < 37 ∧ b = regA t i) ∨ b = wa t 3432#u64 ∨ + (∃ w : U64, w.val < 8 ∧ b = wa t ⟨BitVec.ofNat 64 (3600 + 8 * w.val)⟩) ∨ b = wa t 3576#u64 ∨ b = wa t 3584#u64 + +/-- `b` is outside the 4 KiB of the TCB at `t`. -/ +def utOffTcbA (t b : U64) : Prop := b.val < t.val ∨ t.val + 4096 ≤ b.val + +/-- An initialised word lies in the TCB. -/ +theorem utTcbInit_inA {t b : U64} (ht : TcbAddr t) (h : utTcbInitA t b) : ¬ utOffTcbA t b := by + have hc : ∀ c : U64, c.val < 4096 → ¬ utOffTcbA t (wa t c) := by + intro c hc h + unfold utOffTcbA at h + rw [ut_tcbWordA ht hc] at h + omega + rcases h with ⟨i, hi, rfl⟩ | rfl | ⟨w, hw, rfl⟩ | rfl | rfl + · intro h + unfold utOffTcbA at h + rw [regA_val ht hi] at h + omega + · exact hc _ (by decide) + · refine hc _ ?_ + show (BitVec.ofNat 64 (3600 + 8 * w.val)).toNat < 4096 + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)] + omega + · exact hc _ (by decide) + · exact hc _ (by decide) + +theorem ut_node_baseA (s s' : S) (h : L.ksb s' = L.ksb s) (cpu : U64) : + obj.node_base st s' cpu = obj.node_base st s cpu := by + unfold obj.node_base + rw [L.ks_base_eq, L.ks_base_eq, h] + +/-- **`create_object` of a TCB** (module docstring). `nb` is node 0's base. -/ +theorem ut_createTcbA (ar : machine.Arch A) (s : S) (a : A) (nt t us nb : U64) (dev : Bool) (hn : nt.val = 1) + (ht : TcbAddr t) (haff : L.mem s (wa t 3520#u64) = 0#u64) (hnb : obj.node_base st s 0#u64 = ok nb) + (hN : Aligned (wa nb 88#u64)) (hNo : utOffTcbA t (wa nb 88#u64)) + (hDa : Aligned (wa (L.ksb s) 8#u64)) (hDo : utOffTcbA t (wa (L.ksb s) 8#u64)) + (hH : L.mem s (wa nb 88#u64) ≠ 0#u64 → Aligned (wa (L.mem s (wa nb 88#u64)) 3584#u64) ∧ + utOffTcbA t (wa (L.mem s (wa nb 88#u64)) 3584#u64)) : + objects.create_object st ar s a nt t us dev ⦃ r => + r.1 = .Thread t ∧ r.2.2 = a ∧ + WritesIn st s r.2.1 (fun b => utTcbInitA t b ∨ b = wa nb 88#u64 ∨ + (L.mem s (wa nb 88#u64) ≠ 0#u64 ∧ b = wa (L.mem s (wa nb 88#u64)) 3584#u64)) ∧ + UtRegsA (L.mem r.2.1) t ∧ L.mem r.2.1 (wa t 3432#u64) = L.mem s (wa (L.ksb s) 8#u64) ⦄ := by + have hS : Fl4ProofsA64.P0.StoreTotal st := L.total + generalize hhd : L.mem s (wa nb 88#u64) = hd at hH ⊢ + have hal : ∀ c : U64, c.val < 4096 → c.val % 8 = 0 → Aligned (wa t c) := by + intro c hc h8 + have := ht.align + show _ % 8 = 0 + rw [ut_tcbWordA ht hc] + omega + have hregal : ∀ i : U64, i.val < 37 → Aligned (regA t i) := by + intro i hi + have := ht.align + show _ % 8 = 0 + rw [regA_val ht hi] + omega + -- the affinity word is not an initialised word + have haffI : ¬ utTcbInitA t (wa t 3520#u64) := by + have e : (wa t 3520#u64).val = t.val + 3520 := ut_tcbWordA ht (by decide) + have hc : ∀ c : U64, c.val < 4096 → c.val ≠ 3520 → wa t 3520#u64 ≠ wa t c := by + intro c hc hne h + have := congrArg UScalar.val h + rw [e, ut_tcbWordA ht hc] at this + omega + rintro (⟨i, hi, h⟩ | h | ⟨w, hw, h⟩ | h | h) + · have := congrArg UScalar.val h + rw [e, regA_val ht hi] at this + omega + · exact hc _ (by decide) (by decide) h + · have hk : (⟨BitVec.ofNat 64 (3600 + 8 * w.val)⟩ : U64).val = 3600 + 8 * w.val := by + show (BitVec.ofNat 64 (3600 + 8 * w.val)).toNat = _ + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)] + exact hc _ (by rw [hk]; omega) (by rw [hk]; omega) h + · exact hc _ (by decide) (by decide) h + · exact hc _ (by decide) (by decide) h + -- init_context + obtain ⟨s1, e1, w1, r1⟩ := spec_exists_dut (ut_initContextA (st := st) s t ht) + have W1 : WritesIn st s s1 (utTcbInitA t) := + ⟨w1.1, w1.2.1, fun b hb h => w1.2.2 b hb (fun x => h (Or.inl x))⟩ + -- the domain + obtain ⟨d, hd8⟩ : ∃ d, d = L.mem s (wa (L.ksb s) 8#u64) := ⟨_, rfl⟩ + have ed : sched.cur_domain st s1 = ok d := by + have h8 : layout.ks.CUR_DOMAIN = 8#u64 := by simp [layout.ks.CUR_DOMAIN] + unfold sched.cur_domain obj.ks_get + rw [L.ks_base_eq] + simp only [lift, bind_ok, L.read_eq] + rw [h8, W1.1, W1.2.2 _ hDa (fun h => utTcbInit_inA ht h hDo), ← hd8] + obtain ⟨dm, edm, hdm⟩ := spec_exists_dut Fl4ProofsA64.P0.Consts.layout.tcb.DOMAIN.spec + have e2 : obj.Tcb.set_domain st t s1 d = ok (L.wr s1 (wa t 3432#u64) d) := by + unfold obj.Tcb.set_domain + rw [edm, bind_ok, hdm] + simp only [lift, bind_ok, L.write_eq] + obtain ⟨s2, hs2⟩ : ∃ x, x = L.wr s1 (wa t 3432#u64) d := ⟨_, rfl⟩ + rw [← hs2] at e2 + have W2 : WritesIn st s s2 (utTcbInitA t) := by + rw [hs2] + exact ut_wr_writesA W1 _ _ (hal _ (by decide) (by decide)) (Or.inr (Or.inl rfl)) + have r2 : ∀ i : U64, i.val < 37 → L.mem s2 (regA t i) = L.mem s1 (regA t i) := by + intro i hi + rw [hs2, L.mem_wr s1 _ d _ (hal _ (by decide) (by decide)) (hregal i hi), ite_eq_right] + intro h + have := congrArg UScalar.val h + rw [ut_tcbWordA ht (by decide), regA_val ht hi] at this + have h34 : (3432#u64 : U64).val = 3432 := rfl + omega + -- the parent's name + obtain ⟨ct, ect⟩ : ∃ c, sched.cur_thread st s2 = ok c := by + unfold sched.cur_thread obj.node_get obj.node_base + obtain ⟨n0, en0, -⟩ := spec_exists_dut Fl4ProofsA64.P0.Consts.layout.ks.NODES.spec + obtain ⟨sz, esz, -⟩ := spec_exists_dut Fl4ProofsA64.P0.Consts.layout.node.SIZE.spec + rw [L.cur_cpu_eq, bind_ok, L.ks_base_eq, bind_ok, en0, bind_ok] + simp only [lift, bind_ok] + rw [esz, bind_ok] + simp only [bind_ok, L.read_eq] + exact ⟨_, rfl⟩ + obtain ⟨s3, e3, w3⟩ := spec_exists_dut (ut_childNameA (st := st) s2 t ct ht) + have W3 : WritesIn st s s3 (utTcbInitA t) := + ⟨w3.1.trans W2.1, w3.2.1.trans W2.2.1, fun b hb h => + (w3.2.2 b hb (fun x => h (Or.inr (Or.inr (Or.inl x))))).trans (W2.2.2 b hb h)⟩ + have r3 : ∀ i : U64, i.val < 37 → L.mem s3 (regA t i) = L.mem s1 (regA t i) := by + intro i hi + rw [w3.2.2 _ (hregal i hi) ?_, r2 i hi] + rintro ⟨w, hw, h⟩ + have hk : (⟨BitVec.ofNat 64 (3600 + 8 * w.val)⟩ : U64).val = 3600 + 8 * w.val := by + show (BitVec.ofNat 64 (3600 + 8 * w.val)).toNat = _ + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)] + have := congrArg UScalar.val h + rw [regA_val ht hi, ut_tcbWordA ht (by rw [hk]; omega), hk] at this + omega + -- the debug list + have haff3 : L.mem s3 (wa t 3520#u64) = 0#u64 := by + rw [W3.2.2 _ (hal _ (by decide) (by decide)) haffI, haff] + have hhd3 : L.mem s3 (wa nb 88#u64) = hd := by + rw [W3.2.2 _ hN (fun h => utTcbInit_inA ht h hNo), hhd] + obtain ⟨af, eaf, haf⟩ := spec_exists_dut Fl4ProofsA64.P0.Consts.layout.tcb.AFFINITY.spec + obtain ⟨dp, edp, hdp⟩ := spec_exists_dut Fl4ProofsA64.P0.Consts.layout.tcb.DEBUG_PREV.spec + obtain ⟨dn, edn, hdn⟩ := spec_exists_dut Fl4ProofsA64.P0.Consts.layout.tcb.DEBUG_NEXT.spec + have h88 : layout.node.DEBUG_TCBS = 88#u64 := by simp [layout.node.DEBUG_TCBS] + have hprev : ∀ (s' : S) (x v : U64), obj.Tcb.set_debug_prev st x s' v = ok (L.wr s' (wa x 3584#u64) v) := by + intro s' x v + unfold obj.Tcb.set_debug_prev + rw [edp, bind_ok, hdp] + simp only [lift, bind_ok, L.write_eq] + have hnext : ∀ (s' : S) (x v : U64), obj.Tcb.set_debug_next st x s' v = ok (L.wr s' (wa x 3576#u64) v) := by + intro s' x v + unfold obj.Tcb.set_debug_next + rw [edn, bind_ok, hdn] + simp only [lift, bind_ok, L.write_eq] + obtain ⟨s5, hs5⟩ : ∃ x, x = L.wr (L.wr s3 (wa t 3584#u64) 0#u64) (wa t 3576#u64) hd := ⟨_, rfl⟩ + have W5 : WritesIn st s s5 (utTcbInitA t) := by + rw [hs5] + exact ut_wr_writesA (ut_wr_writesA W3 _ _ (hal _ (by decide) (by decide)) + (Or.inr (Or.inr (Or.inr (Or.inr rfl))))) _ _ (hal _ (by decide) (by decide)) + (Or.inr (Or.inr (Or.inr (Or.inl rfl)))) + have r5 : ∀ i : U64, i.val < 37 → L.mem s5 (regA t i) = L.mem s1 (regA t i) := by + intro i hi + have hne : ∀ c : U64, c.val < 4096 → 552 ≤ c.val → wa t c ≠ regA t i := by + intro c hc h5 h + have := congrArg UScalar.val h + rw [ut_tcbWordA ht hc, regA_val ht hi] at this + omega + rw [hs5, L.mem_wr _ _ _ _ (hal _ (by decide) (by decide)) (hregal i hi), ite_eq_right (hne _ (by decide) (by decide)), + L.mem_wr _ _ _ _ (hal _ (by decide) (by decide)) (hregal i hi), ite_eq_right (hne _ (by decide) (by decide)), + r3 i hi] + -- the final state: the old head's back link, then the head + obtain ⟨s6, hs6⟩ : ∃ x, x = (if hd = 0#u64 then s5 else L.wr s5 (wa hd 3584#u64) t) := ⟨_, rfl⟩ + obtain ⟨s7, hs7⟩ : ∃ x, x = L.wr s6 (wa nb 88#u64) t := ⟨_, rfl⟩ + have hdbg : objects.debug_append st s3 t = ok s7 := by + unfold objects.debug_append obj.Tcb.affinity obj.node_get obj.node_set + rw [eaf, bind_ok, haf] + simp only [lift, bind_ok, L.read_eq, haff3] + rw [ut_node_baseA s s3 W3.1, hnb] + simp only [bind_ok, h88, hhd3, hprev, hnext] + rw [← hs5] + have hnb6 : obj.node_base st s6 0#u64 = ok nb := by + rw [ut_node_baseA s s6, hnb] + rw [hs6] + split + · exact W5.1 + · rw [L.ksb_wr, W5.1] + by_cases h0 : hd = 0#u64 + · have e6 : s6 = s5 := by rw [hs6, ite_eq_left h0] + rw [ite_eq_right (by simp [h0])] + simp only [bind_ok, uncurry_apply_pair] + rw [← e6, hnb6] + simp only [bind_ok, L.write_eq, hs7] + · have e6 : s6 = L.wr s5 (wa hd 3584#u64) t := by rw [hs6, ite_eq_right h0] + rw [ite_eq_left (by simp [h0])] + simp only [bind_ok, uncurry_apply_pair] + rw [← e6, hnb6] + simp only [bind_ok, L.write_eq, hs7] + have ecr : objects.create_object st ar s a nt t us dev = ok (.Thread t, s7, a) := by + obtain rfl : nt = 1#u64 := UScalar.eq_of_val_eq (by simp [hn]) + unfold objects.create_object + rw [ite_eq_left (show (1#u64 : U64) = fl4_abi.consts.object.Tcb by simp [fl4_abi.consts.object.Tcb])] + unfold objects.init_fpu debug.init_context + rw [e1] + simp only [bind_ok] + rw [ed, bind_ok, e2, bind_ok, ect, bind_ok, e3, bind_ok, hdbg, bind_ok] + rw [ecr, WP.spec_ok] + refine ⟨rfl, rfl, ?_, ?_, ?_⟩ + · have W5' : WritesIn st s s5 (fun b => utTcbInitA t b ∨ b = wa nb 88#u64 ∨ + (hd ≠ 0#u64 ∧ b = wa hd 3584#u64)) := + ⟨W5.1, W5.2.1, fun b hb h => W5.2.2 b hb (fun x => h (Or.inl x))⟩ + rw [hs7] + refine ut_wr_writesA ?_ _ _ hN (Or.inr (Or.inl rfl)) + rw [hs6] + split + · exact W5' + · rename_i h0 + exact ut_wr_writesA W5' _ _ (hH h0).1 (Or.inr (Or.inr ⟨h0, rfl⟩)) + · intro r + have hi : (regIdxA r).val < 37 := regIdxA_lt r + have h7 : L.mem s7 (regA t (regIdxA r)) = L.mem s5 (regA t (regIdxA r)) := by + have hin : utTcbInitA t (regA t (regIdxA r)) := Or.inl ⟨_, hi, rfl⟩ + rw [hs7, L.mem_wr _ _ _ _ hN (hregal _ hi), ite_eq_right (fun h => utTcbInit_inA ht hin (by rw [← h]; exact hNo)), hs6] + split + · rfl + · rename_i h0 + rw [L.mem_wr _ _ _ _ (hH h0).1 (hregal _ hi), ite_eq_right (fun h => utTcbInit_inA ht hin (by rw [← h]; exact (hH h0).2))] + rw [h7, r5 _ hi, r1 _ hi] + have : (regIdxA r).val = 33 ↔ r = .spsrEl1 := by cases r <;> decide + simp only [this] + · have hD : (wa t 3432#u64).val = t.val + 3432 := ut_tcbWordA ht (by decide) + have hDal : Aligned (wa t 3432#u64) := hal _ (by decide) (by decide) + have hDin : utTcbInitA t (wa t 3432#u64) := Or.inr (Or.inl rfl) + have hne : ∀ c : U64, c.val < 4096 → c.val ≠ 3432 → wa t c ≠ wa t 3432#u64 := by + intro c hc hne h + have := congrArg UScalar.val h + rw [hD, ut_tcbWordA ht hc] at this + omega + have h7 : L.mem s7 (wa t 3432#u64) = L.mem s5 (wa t 3432#u64) := by + rw [hs7, L.mem_wr _ _ _ _ hN hDal, ite_eq_right (fun h => utTcbInit_inA ht hDin (by rw [← h]; exact hNo)), hs6] + split + · rfl + · rename_i h0 + rw [L.mem_wr _ _ _ _ (hH h0).1 hDal, + ite_eq_right (fun h => utTcbInit_inA ht hDin (by rw [← h]; exact (hH h0).2))] + have h5 : L.mem s5 (wa t 3432#u64) = L.mem s3 (wa t 3432#u64) := by + rw [hs5, L.mem_wr _ _ _ _ (hal _ (by decide) (by decide)) hDal, ite_eq_right (hne _ (by decide) (by decide)), + L.mem_wr _ _ _ _ (hal _ (by decide) (by decide)) hDal, ite_eq_right (hne _ (by decide) (by decide))] + have h3 : L.mem s3 (wa t 3432#u64) = L.mem s2 (wa t 3432#u64) := by + refine w3.2.2 _ hDal ?_ + rintro ⟨w, hw, h⟩ + have hk : (⟨BitVec.ofNat 64 (3600 + 8 * w.val)⟩ : U64).val = 3600 + 8 * w.val := by + show (BitVec.ofNat 64 (3600 + 8 * w.val)).toNat = _ + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)] + exact hne _ (by rw [hk]; omega) (by rw [hk]; omega) h.symm + rw [h7, h5, h3, hs2, L.mem_wr s1 _ d _ hDal hDal, ite_eq_left rfl, hd8] + +/-- **A TCB retyped from zeroed memory matches `default_tcb`** (module docstring). -/ +theorem ut_createTcb_relA (ar : machine.Arch A) (s : S) (a : A) (nt t us nb : U64) (dev : Bool) (hn : nt.val = 1) + (ht : TcbAddr t) (hz : ∀ c : U64, c.val < 4096 → c.val % 8 = 0 → L.mem s (wa t c) = 0#u64) + (hnb : obj.node_base st s 0#u64 = ok nb) + (hN : Aligned (wa nb 88#u64)) (hNo : utOffTcbA t (wa nb 88#u64)) + (hDa : Aligned (wa (L.ksb s) 8#u64)) (hDo : utOffTcbA t (wa (L.ksb s) 8#u64)) + (hH : L.mem s (wa nb 88#u64) ≠ 0#u64 → Aligned (wa (L.mem s (wa nb 88#u64)) 3584#u64) ∧ + utOffTcbA t (wa (L.mem s (wa nb 88#u64)) 3584#u64)) + (R : Set U64) (d : Fl4SpecA64.Domain) : + objects.create_object st ar s a nt t us dev ⦃ r => + r.1 = .Thread t ∧ r.2.2 = a ∧ TcbRelE R (L.mem r.2.1) t (Fl4SpecA64.defaultTcb d) ∧ + TcbRel1 (L.mem r.2.1) t (Fl4SpecA64.defaultTcb d) ⦄ := by + refine WP.spec_mono (ut_createTcbA ar s a nt t us nb dev hn ht (hz _ (by decide) (by decide)) hnb hN hNo hDa hDo hH) + fun r ⟨hc, ha, hW, hR, _⟩ => ?_ + -- a word of the TCB outside the initialised ones keeps its zero + have hkeep : ∀ c : U64, c.val = 3328 ∨ c.val = 3368 ∨ c.val = 3376 ∨ c.val = 3448 → L.mem r.2.1 (wa t c) = 0#u64 := by + intro c hcv + have hc4 : c.val < 4096 := by omega + have h8 : c.val % 8 = 0 := by omega + have e : (wa t c).val = t.val + c.val := ut_tcbWordA ht hc4 + have hal : Aligned (wa t c) := by + have := ht.align + show _ % 8 = 0 + rw [e] + omega + have hin : ¬ utOffTcbA t (wa t c) := by + unfold utOffTcbA + rw [e] + omega + have hne : ∀ x : U64, x.val < 4096 → x.val ≠ c.val → wa t c ≠ wa t x := by + intro x hx hne h + have := congrArg UScalar.val h + rw [e, ut_tcbWordA ht hx] at this + omega + rw [hW.2.2 _ hal ?_, hz c hc4 h8] + rintro (h | h | ⟨h0, h⟩) + · rcases h with ⟨i, hi, h⟩ | h | ⟨w, hw, h⟩ | h | h + · have := congrArg UScalar.val h + rw [e, regA_val ht hi] at this + omega + · have hx : (3432#u64 : U64).val = 3432 := rfl + exact hne _ (by omega) (by omega) h + · have hk : (⟨BitVec.ofNat 64 (3600 + 8 * w.val)⟩ : U64).val = 3600 + 8 * w.val := by + show (BitVec.ofNat 64 (3600 + 8 * w.val)).toNat = _ + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)] + exact hne _ (by rw [hk]; omega) (by rw [hk]; omega) h + · have hx : (3576#u64 : U64).val = 3576 := rfl + exact hne _ (by omega) (by omega) h + · have hx : (3584#u64 : U64).val = 3584 := rfl + exact hne _ (by omega) (by omega) h + · exact hin (by rw [h]; exact hNo) + · exact hin (by rw [h]; exact (hH h0).2) + have hst : L.mem r.2.1 (stA t) = 0#u64 := by + have : stA t = wa t 3328#u64 := by + unfold stA layout.tcb.STATE layout.tcb.F + rfl + rw [this] + exact hkeep _ (Or.inl rfl) + have hbound : L.mem r.2.1 (tBoundA t) = 0#u64 := hkeep 3368#u64 (Or.inr (Or.inl rfl)) + have hfault : L.mem r.2.1 (tFaultA t) = 0#u64 := hkeep 3376#u64 (Or.inr (Or.inr (Or.inl rfl))) + have hprio : L.mem r.2.1 (prioA t) = 0#u64 := hkeep 3448#u64 (Or.inr (Or.inr (Or.inr rfl))) + have hbadge : L.mem r.2.1 (badgeRegA t) = 0#u64 := by + have : badgeRegA t = regA t (regIdxA Fl4SpecA64.badge_register) := by + unfold badgeRegA regA + have : layout.reg.BADGE = regIdxA Fl4SpecA64.badge_register := by + simp [layout.reg.BADGE, layout.reg.X0] + rfl + rw [this] + rw [this, hR, ite_eq_right (by decide)] + exact ⟨hc, ha, ut_tcbRelA R _ t d hst hR hprio hfault hbound, ut_tcbNtfnRelA _ t d hst hbadge hprio⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtTcbScA.lean b/proofs/Fl4ProofsA64/P3/UtTcbScA.lean new file mode 100644 index 000000000..7d853041e --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtTcbScA.lean @@ -0,0 +1,123 @@ +/- +P3 (aarch64, A64-P3 Untyped W3 leaves): **a retyped TCB and scheduling context from FL4's words**, against the spec's +`default_tcb` and `default_sched_context`, with no dependency on the joint record or the ghost heap. + +* **`ut_tcbRelA`** / **`ut_tcbNtfnRelA`**: `TcbRelE` / `TcbRel1` at `defaultTcb d`, from zeroed state, fault, binding + and priority words and the user registers `init_context` leaves (zero but `SPSR_EL1 = 0x140`). +* **`ut_scRelA`**: `ScRelWA` at `defaultSchedContext` (an inactive SC), from zeroed header words. + +No named premise. Hypotheses: the words; the SC's placement; the new SC is no core's idle SC. New lemmas only. +-/ +import Fl4ProofsA64.P3.EpRelA +import Fl4ProofsA64.P3.NtfnRelA +import Fl4ProofsA64.P3.ScRelA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +/-- The user register words of a new TCB (`regs::init_context` on zeroed memory). -/ +def UtRegsA (m : U64 → U64) (t : U64) : Prop := + ∀ r : Fl4SpecA64.Register, m (regA t (regIdxA r)) = if r = .spsrEl1 then 0x140#u64 else 0#u64 + +theorem ut_newContextA (r : Fl4SpecA64.Register) : + (Fl4SpecA64.defaultArchTcb.tcbContext.userRegs r) = if r = .spsrEl1 then 0x140#64 else 0#64 := by + show Function.update (fun _ => (0 : Fl4SpecA64.Word)) .spsrEl1 Fl4SpecA64.pstateUser r = _ + rw [Function.update_apply]; rfl + +/-- **A new TCB matches `default_tcb`** (the endpoint-side record). -/ +theorem ut_tcbRelA (R : Set U64) (m : U64 → U64) (t : U64) (d : Fl4SpecA64.Domain) (hst : m (stA t) = 0#u64) + (hregs : UtRegsA m t) (hprio : m (prioA t) = 0#u64) (hfault : m (tFaultA t) = 0#u64) + (hbound : m (tBoundA t) = 0#u64) : TcbRelE R m t (Fl4SpecA64.defaultTcb d) := by + refine + { st := by rw [hst]; rfl + blk := fun e h => by cases h + send := fun e sd h => by cases h + recv := fun e r sd h => by cases h + reply := fun r h => by cases h + regs := fun r => ?_ + prio := by rw [hprio]; rfl + fault0 := ⟨fun _ => rfl, fun _ => hfault⟩ + faultT := ⟨fun h => ?_, fun ⟨_, h⟩ => by cases h⟩ + bound := by rw [optRef, ite_eq_left hbound]; rfl + faultR := hfault } + · change (m (regA t (regIdxA r))).bv = Fl4SpecA64.defaultArchTcb.tcbContext.userRegs r + rw [hregs r, ut_newContextA] + split <;> rfl + · rw [hfault] at h; exact absurd h (by decide) + +/-- **A new TCB matches `default_tcb`** (the notification-side record). -/ +theorem ut_tcbNtfnRelA (m : U64 → U64) (t : U64) (d : Fl4SpecA64.Domain) (hst : m (stA t) = 0#u64) + (hbadge : m (badgeRegA t) = 0#u64) (hprio : m (prioA t) = 0#u64) : TcbRel1 m t (Fl4SpecA64.defaultTcb d) := by + refine + { st := by rw [hst]; rfl + blk := fun n h => by cases h + badge := ?_ + prio := by rw [hprio]; rfl } + rw [hbadge] + show (0#u64 : U64).bv = badgeOf (Fl4SpecA64.defaultTcb d) + unfold badgeOf + change _ = Fl4SpecA64.defaultArchTcb.tcbContext.userRegs Fl4SpecA64.badge_register + rw [ut_newContextA, ite_eq_right (by decide)] + rfl + +/-- **`JStage3A.scSize` for a retyped SC** (Architect 00:17Z): `default_object`'s size is `us - 7`, within the new heap +object of size `2 ^ us`. -/ +theorem ut_scSizeA {H : Heap} {c : U64} {us : Nat} {m : Fl4SpecA64.McState} {σ : Fl4SpecA64.State Unit} + (hH : H c = some (.sc us)) (hus : Fl4SpecA64.minSchedContextBits ≤ us) + (hk : σ.kheap (scNameA m c) = + some (.schedContext Fl4SpecA64.defaultSchedContext (us - Fl4SpecA64.minSchedContextBits))) : + ∀ x n, σ.kheap (scNameA m c) = some (.schedContext x n) → + n + Fl4SpecA64.minSchedContextBits = H.scBits c := by + intro x n h + rw [hk] at h + cases h + have e : H.scBits c = us := by + unfold Heap.scBits + rw [hH] + rw [e] + omega + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **A new scheduling context matches `default_sched_context`** (inactive, so no ring is related). -/ +theorem ut_scRelA (ii : Prop) (hii : ¬ ii) (s : S) (sc : U64) (n : Nat) + (hpl : Placed sc (n + Fl4SpecA64.minSchedContextBits)) + (hperiod : L.mem s (periodA sc) = 0#u64) (hmax : L.mem s (maxA sc) = 0#u64) + (hw : ∀ K : U64, K = 8#u64 ∨ K = 24#u64 ∨ K = 32#u64 ∨ K = 40#u64 ∨ K = 48#u64 ∨ K = 56#u64 ∨ K = 64#u64 → + L.mem s (scWA sc K) = 0#u64) : + ScRelWA st ii s sc Fl4SpecA64.defaultSchedContext n := by + have h24 := hw 24#u64 (by simp) + have h32 := hw 32#u64 (by simp) + have h40 := hw 40#u64 (by simp) + have h56 := hw 56#u64 (by simp) + have h64 := hw 64#u64 (by simp) + have hm : (L.rmax s sc).val = 0 := by + show (L.mem s (maxA sc)).val = 0 + rw [hmax]; rfl + exact + { pl := hpl + period := by + show (0 : Fl4SpecA64.Ticks) = (L.mem s (periodA sc)).bv + rw [hperiod]; rfl + consumed := by rw [hw 8#u64 (by simp)]; rfl + tcb := by rw [h24, optRefS_zero]; rfl + reply := by rw [h32, optRefS_zero]; rfl + ntfn := by rw [h40, optRefS_zero]; rfl + badge := by rw [hw 48#u64 (by simp)]; rfl + yieldFrom := by rw [h56, optRefS_zero]; rfl + sporadic := by rw [h64]; rfl + spor := fun h => absurd h64 h + idle := fun h => absurd h hii + max := by rw [hm]; rfl + act := fun h => absurd hm h } + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtVcInitA.lean b/proofs/Fl4ProofsA64/P3/UtVcInitA.lean new file mode 100644 index 000000000..428874f93 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtVcInitA.lean @@ -0,0 +1,113 @@ +/- +P3 (aarch64, A64-P3 Untyped, spec side): **the `vcpu::init` step `fl4InvokeUntypedA64` appends to a VCPU retype**. + +* `utVcInitA Q σ`: `σ` with `fl4_vcpu_init` applied to every VCPU object at an address of `Q`. +* `UtVcSpA V`: the spec addresses of the VCPUs of `V`. + +New definitions and lemmas only. No named premise. +-/ +import Fl4ProofsA64.P3.UtVcpuA +import Fl4ProofsA64.P3.ScSpecA +import Fl4ProofsA64.P3.SpecFrA + +namespace Fl4ProofsA64.P3 + +noncomputable section + +open Fl4SpecA64 Fl4Spec Aeneas Aeneas.Std Classical + +/-- The spec addresses of the objects at `V`. -/ +def UtVcSpA (V : Set U64) (q : ObjRef) : Prop := ∃ v ∈ V, q = specPtr v + +/-- `fl4_vcpu_init` on a VCPU object; every other object as it is. -/ +def utVcObjA : KernelObject → KernelObject + | .archObj (.vcpu v) => .archObj (.vcpu (fl4VcpuInit v)) + | o => o + +/-- `σ` with `fl4_vcpu_init` applied to every VCPU object at an address of `Q`. -/ +def utVcInitA (Q : ObjRef → Prop) (σ : State Unit) : State Unit := + { σ with kheap := fun q => if Q q then (σ.kheap q).map utVcObjA else σ.kheap q } + +theorem utVcInit_offA {Q : ObjRef → Prop} {σ : State Unit} {q : ObjRef} (h : ¬ Q q) : + (utVcInitA Q σ).kheap q = σ.kheap q := by + show (if Q q then (σ.kheap q).map utVcObjA else σ.kheap q) = _ + rw [if_neg h] + +theorem utVcInit_onA {Q : ObjRef → Prop} {σ : State Unit} {q : ObjRef} (h : Q q) : + (utVcInitA Q σ).kheap q = (σ.kheap q).map utVcObjA := by + show (if Q q then (σ.kheap q).map utVcObjA else σ.kheap q) = _ + rw [if_pos h] + +theorem utVcInit_vcpuA {Q : ObjRef → Prop} {σ : State Unit} {q : ObjRef} (hq : Q q) {v : Vcpu} + (h : σ.kheap q = some (.archObj (.vcpu v))) : + (utVcInitA Q σ).kheap q = some (.archObj (.vcpu (fl4VcpuInit v))) := by + show (if Q q then (σ.kheap q).map utVcObjA else σ.kheap q) = _ + rw [if_pos hq, h] + rfl + +/-- The addresses `fl4InvokeUntypedA64` runs `vcpu::init` at. -/ +def utVcRetQA (t : ApiObjectType) (retypeBase : Word) (us : Nat) (slots : List CslotPtr) : ObjRef → Prop := + fun q => t = .archObject .vcpuObj ∧ q ∈ fl4RetypeAddrs fl4ObjBitsApi retypeBase slots.length us t + +/-- **The spec state after the whole A64 invocation**: `σ` is the generic body's outcome. -/ +def utInvStA (t : ApiObjectType) (retypeBase : Word) (us : Nat) (slots : List CslotPtr) (σ : State Unit) : + State Unit := + utVcInitA (utVcRetQA t retypeBase us slots) σ + +/-- The `vcpu::init` loop over distinct new VCPUs reaches `utVcInitA`. -/ +theorem utVcInit_mapMxA (l : List ObjRef) : ∀ σ : State Unit, l.Nodup → + (∀ q ∈ l, σ.kheap q = some (.archObj (.vcpu defaultVcpu))) → + ((), utVcInitA (· ∈ l) σ) ∈ (NonDet.mapMx (fun v => (do + let vcpu ← getVcpu (Z := Unit) v + setVcpu v (fl4VcpuInit vcpu) : NonDet (State Unit) Unit)) l σ).1 := by + induction l with + | nil => + intro σ _ _ + have e : utVcInitA (· ∈ ([] : List ObjRef)) σ = σ := by + unfold utVcInitA + simp + rw [e] + exact Set.mem_singleton _ + | cons a l ih => + intro σ nd h + obtain ⟨na, nd'⟩ := List.nodup_cons.1 nd + have ha := h a List.mem_cons_self + obtain ⟨σ1, e1⟩ : ∃ σ1 : State Unit, σ1 = { σ with + kheap := Function.update σ.kheap a (some (.archObj (.vcpu (fl4VcpuInit defaultVcpu)))) } := ⟨_, rfl⟩ + have k1 : ∀ q, q ≠ a → σ1.kheap q = σ.kheap q := fun q ne => by rw [e1]; exact Function.update_of_ne ne .. + have ka : σ1.kheap a = some (.archObj (.vcpu (fl4VcpuInit defaultVcpu))) := by + rw [e1]; exact Function.update_self .. + have step : ((), σ1) ∈ ((do + let vcpu ← getVcpu (Z := Unit) a + setVcpu a (fl4VcpuInit vcpu) : NonDet (State Unit) Unit) σ).1 := by + refine mem_bindA.2 ⟨(defaultVcpu, σ), ?_, ?_⟩ + · change (defaultVcpu, σ) ∈ ((NonDet.gets (vcpusOf (Z := Unit)) >>= fun m => NonDet.assertOpt (m a)) σ).1 + refine mem_bindA.2 ⟨(vcpusOf σ, σ), rfl, ?_⟩ + have v : vcpusOf σ a = some defaultVcpu := by simp [vcpusOf, aobjsOf, optMap, aobjOf, ha, vcpuOf] + simp only [v] + exact Set.mem_singleton _ + · unfold setVcpu + rw [Sched.setObject_eqSc ha (by rfl)] + rw [e1] + exact Set.mem_singleton _ + have rest := ih σ1 nd' fun q hq => by + rw [k1 q (fun e => na (e ▸ hq))] + exact h q (List.mem_cons_of_mem _ hq) + have e : utVcInitA (· ∈ l) σ1 = utVcInitA (· ∈ a :: l) σ := by + have kk : (utVcInitA (· ∈ l) σ1).kheap = (utVcInitA (· ∈ a :: l) σ).kheap := by + funext q + by_cases eq : q = a + · subst eq + rw [utVcInit_offA (Q := (· ∈ l)) na, ka, utVcInit_vcpuA (Q := (· ∈ q :: l)) List.mem_cons_self ha] + · by_cases m : q ∈ l + · rw [utVcInit_onA (Q := (· ∈ l)) m, utVcInit_onA (Q := (· ∈ a :: l)) (List.mem_cons_of_mem _ m), k1 q eq] + · rw [utVcInit_offA (Q := (· ∈ l)) m, utVcInit_offA (Q := (· ∈ a :: l)) (by simp [eq, m]), k1 q eq] + unfold utVcInitA at kk ⊢ + rw [e1] at kk ⊢ + exact congrArg (fun k => { σ with kheap := k }) kk + rw [← e] + exact mem_bindA.2 ⟨((), σ1), step, rest⟩ + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtVcTrA.lean b/proofs/Fl4ProofsA64/P3/UtVcTrA.lean new file mode 100644 index 000000000..224d47c91 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtVcTrA.lean @@ -0,0 +1,325 @@ +/- +P3 (aarch64, A64-P3 Untyped): **the record across the spec's `vcpu::init` step** (`utVcInitA`), field by field. FL4's +state is unchanged: the step rewrites new default VCPU objects to `fl4_vcpu_init default_vcpu`, which keeps their +(absent) thread and run caller. + +* `UtVcStepA σ τ`: every spec object is as in `σ`, or was `default_vcpu` and is `fl4_vcpu_init default_vcpu`; the + globals, the CDT, the original-cap map, the arch state and user memory are kept. `utVcStepA_of`. +* `utVc_*`: the fields of `JStage3A` that read the spec state. + +The relation between FL4's words and the new spec VCPU registers is `VcpuStateSnapA` (`utRt_vcSnapA`, `P3/UtApRec6A`). +No named premise. New definitions and theorems only. +-/ +import Fl4ProofsA64.P3.UtFrA +import Fl4ProofsA64.P3.UtVcInitA +import Fl4ProofsA64.P3.FrameA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-- The spec's `vcpu::init` step (module docstring). -/ +structure UtVcStepA (σ τ : State Unit) : Prop extends SpecGlobA σ τ where + kh : ∀ p, τ.kheap p = σ.kheap p ∨ + (σ.kheap p = some (.archObj (.vcpu defaultVcpu)) ∧ τ.kheap p = some (.archObj (.vcpu (fl4VcpuInit defaultVcpu)))) + um : τ.machineState.underlyingMemory = σ.machineState.underlyingMemory + cdt : τ.cdt = σ.cdt + orig : τ.isOriginalCap = σ.isOriginalCap + arch : τ.archState = σ.archState + +theorem utVcStepA_of {Q : ObjRef → Prop} {σ : State Unit} + (h : ∀ p, Q p → σ.kheap p = some (.archObj (.vcpu defaultVcpu))) : UtVcStepA σ (utVcInitA Q σ) := by + refine ⟨⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, ⟨rfl, rfl, rfl, rfl⟩⟩, fun p => ?_, rfl, rfl, rfl, rfl⟩ + by_cases q : Q p + · exact Or.inr ⟨h p q, utVcInit_vcpuA q (h p q)⟩ + · exact Or.inl (utVcInit_offA q) + +namespace UtVcStepA + +variable {σ τ : State Unit} + +theorem same (H : UtVcStepA σ τ) {p : ObjRef} (h : ∀ x, σ.kheap p ≠ some (.archObj (.vcpu x))) : + τ.kheap p = σ.kheap p := by + rcases H.kh p with e | ⟨e, -⟩ + · exact e + · exact absurd e (h _) + +theorem keep (H : UtVcStepA σ τ) {p : ObjRef} {ko : KernelObject} (h : σ.kheap p = some ko) + (hk : ∀ x, ko ≠ .archObj (.vcpu x)) : τ.kheap p = some ko := by + rw [H.same (fun x e => by rw [h] at e; exact hk x (Option.some.inj e))]; exact h + +theorem back (H : UtVcStepA σ τ) {p : ObjRef} {ko : KernelObject} (h : τ.kheap p = some ko) + (hk : ∀ x, ko ≠ .archObj (.vcpu x)) : σ.kheap p = some ko := by + rcases H.kh p with e | ⟨-, e⟩ + · rw [← e]; exact h + · rw [h] at e; exact absurd (Option.some.inj e) (hk _) + +theorem none (H : UtVcStepA σ τ) {p : ObjRef} (h : σ.kheap p = Option.none) : τ.kheap p = Option.none := by + rw [H.same (fun x e => by rw [h] at e; cases e)]; exact h + +/-- A VCPU object after was one before, with the same thread and run caller. -/ +theorem vcpuBack (H : UtVcStepA σ τ) {p : ObjRef} {y : Vcpu} (h : τ.kheap p = some (.archObj (.vcpu y))) : + ∃ x, σ.kheap p = some (.archObj (.vcpu x)) ∧ y.vcpuTcb = x.vcpuTcb ∧ + y.vcpuExt.runCaller = x.vcpuExt.runCaller := by + rcases H.kh p with e | ⟨e1, e2⟩ + · exact ⟨y, e ▸ h, rfl, rfl⟩ + · rw [h] at e2 + have : y = fl4VcpuInit defaultVcpu := by + injection e2 with e2; injection e2 with e2; injection e2 + subst this + exact ⟨_, e1, rfl, rfl⟩ + +theorem vcpuKeep (H : UtVcStepA σ τ) {p : ObjRef} {x : Vcpu} (h : σ.kheap p = some (.archObj (.vcpu x))) : + ∃ y, τ.kheap p = some (.archObj (.vcpu y)) ∧ y.vcpuTcb = x.vcpuTcb ∧ + y.vcpuExt.runCaller = x.vcpuExt.runCaller := by + rcases H.kh p with e | ⟨e1, e2⟩ + · exact ⟨x, e.trans h, rfl, rfl⟩ + · have : x = defaultVcpu := by + rw [h] at e1 + injection e1 with e1; injection e1 with e1; injection e1 + subst this + exact ⟨_, e2, rfl, rfl⟩ + +theorem sched (H : UtVcStepA σ τ) : SchedSameA σ τ := by + refine ⟨fun p ko hko => ?_, H.cur, H.idle, H.csc, H.rq, H.rls, H.time, H.cons⟩ + have hk : ∀ x, ko ≠ .archObj (.vcpu x) := fun x e => by + rcases hko with ⟨y, e'⟩ | ⟨y, k, e'⟩ <;> rw [e'] at e <;> cases e + exact ⟨fun h => H.back h hk, fun h => H.keep h hk⟩ + +theorem sstep (H : UtVcStepA σ τ) : SStepA σ τ := by + refine ⟨⟨H.cur, H.csc, H.idle, H.time, H.cons, H.repr, H.dom, H.domT⟩, H.act, H.rq, H.rls, ?_, ?_, ?_, ?_, ?_⟩ + · intro p x n h; rw [h]; exact H.keep h (fun _ e => by cases e) + · intro p x h; exact ⟨x, H.keep h (fun _ e => by cases e), rfl, rfl, rfl⟩ + · intro p x n h; rw [h]; exact H.back h (fun _ e => by cases e) + · intro p y h; exact ⟨y, H.back h (fun _ e => by cases e)⟩ + · intro p y h; exact ⟨y, H.back h (fun _ e => by cases e), rfl⟩ + +theorem noBlock (H : UtVcStepA σ τ) : NoBlockESc σ τ := by + intro p x y hx hy + have := (H.keep hx (fun _ e => by cases e)).symm.trans hy + cases this + exact Or.inl ⟨rfl, id⟩ + +theorem specCapAt (H : UtVcStepA σ τ) (sl : CslotPtr) : specCapAtA τ sl = specCapAtA σ sl := by + unfold specCapAtA + rcases H.kh sl.1 with e | ⟨e1, e2⟩ + · rw [e] + · rw [e1, e2] + +theorem qkeep (H : UtVcStepA σ τ) : QKeepA σ τ := + ⟨fun _ y h => ⟨y, H.back h (fun _ e => by cases e), rfl⟩, + fun _ x h => ⟨x, H.back h (fun _ e => by cases e), List.Sublist.refl _⟩, + fun _ x h => ⟨x, H.back h (fun _ e => by cases e), List.Sublist.refl _⟩, + fun _ y k h => ⟨y, k, H.back h (fun _ e => by cases e), fun r => ⟨r, id⟩⟩, + fun _ y h => ⟨y, H.back h (fun _ e => by cases e), id⟩, H.domk⟩ + +end UtVcStepA + +section fields +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {s : S} {a : A} {σ τ : State Unit} {m : McState} + (H : UtVcStepA σ τ) +include H + +theorem utVc_jatA (J : JAtZAX (L := L) AR Z s a σ m) : JAtZAX (L := L) AR Z s a τ m := by + obtain ⟨J, V, X, Y⟩ := J + have hT : TcbKeepA Z.T σ τ := fun t _ x e => ⟨x, H.keep e (fun _ h => by cases h), rfl⟩ + have hC : ∀ c ∈ Z.C, τ.kheap (scRenA m c) = σ.kheap (scRenA m c) := fun c hc => by + obtain ⟨y, n, e, -⟩ := J.sch.sc c hc + rw [e]; exact H.keep e (fun _ h => by cases h) + have J' : JAtZA (L := L) AR Z s a τ m := + { J with + st := + { cnode := fun p r hp => by + obtain ⟨cs, h, rest⟩ := J.st.cnode p r hp + exact ⟨cs, H.keep h (fun _ e => by cases e), rest⟩ + tcb := fun t ht => by + obtain ⟨x, h, rest⟩ := J.st.tcb t ht + exact ⟨x, H.keep h (fun _ e => by cases e), rest⟩ } + cdt := + { J.cdt with + cdt := by rw [H.cdt]; exact J.cdt.cdt + cdt_slot := by rw [H.cdt]; exact J.cdt.cdt_slot + orig := by rw [H.orig]; exact J.cdt.orig } + sch := J.sch.of_spec hT hC H.toSpecGlobA + irq := J.irq.of_spec H.toSpecGlobA + rest := J.rest.of_spec hT hC H.toSpecGlobA + vs := J.vs.of_spec (by rw [H.arch]) fun p hp => H.same fun x e => by + rcases hp with ⟨q, hq, rfl⟩ | ⟨q, hq, rfl⟩ | ⟨q, hq, rfl⟩ + · have g := J.vs.pool q hq; rw [e] at g; simp at g + · have g := J.vs.pt q hq; rw [e] at g; simp at g + · have g := J.vs.high q hq; rw [e] at g; simp at g + ext := J.ext.of_spec (by rw [H.arch]) fun q hq => H.same fun x e => by + rcases hq with hq | hq | hq + · have g := J.vs.pool q hq; rw [e] at g; simp at g + · have g := J.ext.s2root q hq; rw [e] at g; simp at g + · have g := J.ext.s2pt q hq; rw [e] at g; simp at g } + refine ⟨J', ?_, ?_, ?_⟩ + · refine ⟨fun v hv => ?_, fun p y e => ?_, fun t ht => ?_, V.cap⟩ + · obtain ⟨x, h, h1, h2⟩ := V.obj v hv + obtain ⟨y, g, g1, g2⟩ := H.vcpuKeep h + exact ⟨y, g, g1.trans h1, g2.trans h2⟩ + · obtain ⟨x, g, -, -⟩ := H.vcpuBack e + exact V.back p x g + · obtain ⟨x, h, rest⟩ := V.tcb t ht + exact ⟨x, H.keep h (fun _ e => by cases e), rest⟩ + · refine ⟨fun r hr => ?_, fun p es e => X.back p es (H.back e (fun _ h => by cases h)), X.cap⟩ + obtain ⟨es, h⟩ := X.obj r hr + exact ⟨es, H.keep h (fun _ e => by cases e)⟩ + · refine ⟨fun r hr => ?_, fun p l1 e => Y.back p l1 (H.back e (fun _ h => by cases h)), Y.cap⟩ + obtain ⟨l1, h⟩ := Y.obj r hr + exact ⟨l1, H.keep h (fun _ e => by cases e)⟩ + +theorem utVc_schedSRA {κ : U64 → U64} (h : SchedSRA st Z m κ s σ) : SchedSRA st Z m κ s τ := + h.stable (WritesIn.refl (st := st) s (fun _ => False)) (fun _ f _ => f) H.sstep H.noBlock + +theorem utVc_epA {E Rp : Set U64} {Bf : Set (U64 × Word)} (h : EpStateRel Z.T E Rp Bf (L.mem s) σ) + (hP : EPlaced Z.T E Rp Bf) : EpStateRel Z.T E Rp Bf (L.mem s) τ := + h.frame hP (fun _ _ _ => rfl) + ⟨fun p x hk => H.keep hk (fun _ e => by cases e), + fun p x hk => ⟨x, H.keep hk (fun _ e => by cases e), rfl⟩, + fun p x hk => ⟨x, H.keep hk (fun _ e => by cases e), rfl, rfl, rfl, rfl, rfl⟩, H.um⟩ + +theorem utVc_ntfnA {N : Set U64} (h : NtfnStateRel N Z.T (L.mem s) σ) (hP : NPlaced Z.T N) : + NtfnStateRel N Z.T (L.mem s) τ := + h.frame hP (fun _ _ _ => rfl) + ⟨fun p x hk => H.keep hk (fun _ e => by cases e), + fun p x hk => ⟨x, H.keep hk (fun _ e => by cases e), rfl, rfl, rfl⟩⟩ + +theorem utVc_replyA {Rp C : Set U64} {scRef : U64 → ObjRef} {stack : U64 → List U64} + (h : ReplyStackRelA (L.mem s) Rp C σ scRef stack) : ReplyStackRelA (L.mem s) Rp C τ scRef stack := + h.frame (fun _ _ => ⟨rfl, rfl⟩) (fun c _ x n hk => ⟨x, n, H.keep hk (fun _ e => by cases e), rfl⟩) + (fun r _ x hk => ⟨x, H.back hk (fun _ e => by cases e), rfl⟩) + +theorem utVc_scCovA {C : Set U64} (h : ScCovA C m σ) : ScCovA C m τ := + fun p x n hk => h p x n (H.back hk (fun _ e => by cases e)) + +theorem utVc_scSizeA {C : Set U64} {f : U64 → Nat} + (h : ∀ c ∈ C, ∀ x n, σ.kheap (scNameA m c) = some (.schedContext x n) → n + minSchedContextBits ≤ f c ∧ + (NotIdleScA m (scNameA m c) → n + minSchedContextBits = f c)) : + ∀ c ∈ C, ∀ x n, τ.kheap (scNameA m c) = some (.schedContext x n) → n + minSchedContextBits ≤ f c ∧ + (NotIdleScA m (scNameA m c) → n + minSchedContextBits = f c) := + fun c hc x n hk => h c hc x n (H.back hk (fun _ e => by cases e)) + +theorem utVc_bufCoverA {T : Set U64} {Bf : Set (U64 × Word)} (h : BufCover st T Bf s σ) : BufCover st T Bf s τ := + fun t ht x b sb w hx hb hl => h t ht x b sb w (H.back hx (fun _ e => by cases e)) hb hl + +theorem utVc_idleNoFaultA (h : IdleNoFaultA m σ) : IdleNoFaultA m τ := fun c hc => by + obtain ⟨x, hx, hf⟩ := h c hc + exact ⟨x, H.keep hx (fun _ e => by cases e), hf⟩ + +theorem utVc_mcpA {T : Set U64} (h : McpRelA T m (L.mem s) σ) : McpRelA T m (L.mem s) τ := fun t ht => by + obtain ⟨x, hx, he, hf⟩ := h t ht + exact ⟨x, H.keep hx (fun _ e => by cases e), he, hf⟩ + +theorem utVc_yieldA {T C : Set U64} (h : YieldRelA T C m (L.mem s) σ) : YieldRelA T C m (L.mem s) τ := by + refine ⟨fun t ht => ?_, fun t ht hnz => ?_, fun c hc y n hy p hp => ?_⟩ + · obtain ⟨x, hx, he⟩ := h.tcb t ht + exact ⟨x, H.keep hx (fun _ e => by cases e), he⟩ + · obtain ⟨hc, y, n, hy, hf⟩ := h.toSc t ht hnz + exact ⟨hc, y, n, H.keep hy (fun _ e => by cases e), hf⟩ + · exact h.frm c hc y n (H.back hy (fun _ e => by cases e)) p hp + +theorem utVc_irqA (h : SpecIrqA σ) : SpecIrqA τ := by + have nc : ∀ p o b c, τ.kheap p = some o → capOf o b = some c → σ.kheap p = some o := fun p o b c g e => + H.back g (fun x ex => by rw [ex] at e; cases e) + refine ⟨?_, fun irq hi o b c g e => ?_, fun p o b c g e => ?_⟩ + · have ct : ∀ k p, capTableAt k p τ ↔ capTableAt k p σ := fun k p => by + refine ⟨fun ⟨cs, g, w⟩ => ⟨cs, H.back g (fun _ e => by cases e), w⟩, + fun ⟨cs, g, w⟩ => ⟨cs, H.keep g (fun _ e => by cases e), w⟩⟩ + have n := h.node + unfold IrqNodeFl4 validIrqNodeFl4 irqNodeDomFl4 at n ⊢ + simp only [H.irqN, ct] + exact n + · rw [H.irqN] at g + exact h.slot irq hi o b c (nc _ _ _ _ g e) e + · rw [H.irqN]; exact h.ref p o b c (nc _ _ _ _ g e) e + +theorem utVc_sinvA {n : Nat} (h : SInvXA σ m n) : SInvXA τ m n := h.of_spec H.sched H.act (utVc_irqA H h.irqN) + +theorem utVc_deadExtA {OS Rp : Set U64} {stack : U64 → List U64} (h : DeadExtA st Z OS Rp stack s σ) : + DeadExtA st Z OS Rp stack s τ := + { h with + vcpu := fun v hv d y e => by + obtain ⟨x, g, g1, -⟩ := H.vcpuBack e + rw [g1]; exact h.vcpu v hv d x g + asid := by rw [H.arch]; exact h.asid + replyTcb := fun r hr d x e => h.replyTcb r hr d x (H.back e (fun _ e => by cases e)) } + +theorem utVc_specFreeA {OS : Set U64} (h : SpecFreeA st OS s σ) : SpecFreeA st OS s τ := + ⟨fun p hp => H.none (h.heap p hp), by rw [H.cdt]; exact h.cdt, by rw [H.orig]; exact h.orig⟩ + +theorem utVc_unmTabA {ty : PtType} {p : U64} (h : UnmTabA st Z m s σ ty p) : UnmTabA st Z m s τ ty p := by + obtain ⟨t, h1, h2⟩ := h.spec + exact ⟨h.inv, ⟨t, H.keep h1 (fun _ e => by cases e), h2⟩, h.off⟩ + +theorem utVc_unmPtA {OS : Set U64} (h : UnmPtSpecA st Z m OS s σ) : UnmPtSpecA st Z m OS s τ := by + refine ⟨fun u hu p asid va c => utVc_unmTabA H (h.pt u hu p asid va c), + fun u hu p asid c => utVc_unmTabA H (h.vs u hu p asid c), fun u hu p asid va c => ?_, fun u hu p asid c => ?_⟩ + · have g := h.s2 u hu p asid va c + obtain ⟨es, h1, h2⟩ := g.spec + exact ⟨g.inv, es, H.keep h1 (fun _ e => by cases e), h2⟩ + · have g := h.hv u hu p asid c + obtain ⟨l1, h1, h2⟩ := g.spec + exact ⟨g.inv, l1, H.keep h1 (fun _ e => by cases e), h2⟩ + +theorem utVc_specZeroA {OS : Set U64} (h : SpecZeroFreeA st OS s σ) : SpecZeroFreeA st OS s τ := + fun w hw => by rw [H.um]; exact h w hw + +theorem utVc_origLiveA {th : TypedHeapA} (h : ∀ sl, σ.isOriginalCap sl = true → AbsSlotA th sl) : + ∀ sl, τ.isOriginalCap sl = true → AbsSlotA th sl := by rw [H.orig]; exact h + +theorem utVc_nullOrigA (h : ∀ sl, specCapAtA σ sl = some .nullCap → σ.isOriginalCap sl = false) : + ∀ sl, specCapAtA τ sl = some .nullCap → τ.isOriginalCap sl = false := fun sl c => by + rw [H.specCapAt] at c; rw [H.orig]; exact h sl c + +theorem utVc_devFrA {Hp : Heap} {OS : Set U64} (h : DevFrDisjA st Hp OS s σ) : DevFrDisjA st Hp OS s τ := + DevFrDisjA.keepA (fun _ _ => rfl) (fun _ _ _ g => H.keep g (fun _ e => by cases e)) h + +theorem utVc_userFrA {OS : Set U64} (h : UserFrSpecA st OS s σ) : UserFrSpecA st OS s τ := + h.keepA (fun _ _ => rfl) (fun _ _ _ g => H.keep g (fun _ e => by cases e)) + +theorem utVc_finCovA {th : TypedHeapA} (h : FinCovA th σ) : FinCovA th τ := + ⟨fun q r cs g w => h.cnode q r cs (H.back g (fun _ e => by cases e)) w, + fun q x g => h.tcb q x (H.back g (fun _ e => by cases e))⟩ + +theorem utVc_bufAlA {T : Set U64} (h : UtBufAlA T σ) : UtBufAlA T τ := + fun t ht y g => h t ht y (H.back g (fun _ e => by cases e)) + +theorem utVc_sidleA {n : Nat} (h : SIdleA σ m n) : SIdleA τ m n where + f3 := fun c hc => by + obtain ⟨x, hx, g⟩ := h.f3 c hc + exact ⟨x, H.keep hx (fun _ e => by cases e), g⟩ + f4 := fun y k hy => h.f4 y k (H.back hy (fun _ e => by cases e)) + f5 := fun p y k hy he => h.f5 p y k (H.back hy (fun _ e => by cases e)) (by rw [← H.idle]; exact he) + f6 := h.f6.of_eq H.specCapAt + f7 := fun y k hy => by rw [H.time]; exact h.f7 y k (H.back hy (fun _ e => by cases e)) + idleSlot := fun c hc => by + obtain ⟨x, hx, -⟩ := h.f3 c hc + exact (idleVtSameA (by rw [hx]; exact H.keep hx (fun _ e => by cases e)) (h.idleSlot c hc) (h.idleBound c hc)).1 + idleBound := fun c hc => by + obtain ⟨x, hx, -⟩ := h.f3 c hc + exact (idleVtSameA (by rw [hx]; exact H.keep hx (fun _ e => by cases e)) (h.idleSlot c hc) (h.idleBound c hc)).2 + +theorem utVc_blkQA {T E N : Set U64} (h : BlkQueuedA T E N (L.mem s) σ) : BlkQueuedA T E N (L.mem s) τ := by + refine ⟨fun t ht x hx e hb => ?_, fun t ht x hx p hb => ?_⟩ + · obtain ⟨g, y, hy, mem⟩ := h.ep t ht x (H.back hx (fun _ e => by cases e)) e hb + exact ⟨g, y, H.keep hy (fun _ e => by cases e), mem⟩ + · obtain ⟨g, y, q, hy, rest⟩ := h.ntfn t ht x (H.back hx (fun _ e => by cases e)) p hb + exact ⟨g, y, q, H.keep hy (fun _ e => by cases e), rest⟩ + +theorem utVc_schedValidA (h : SchedValidA σ) : SchedValidA τ := + h.congr H.rq H.rls H.act H.cur H.idle H.csc (fun p y k g => H.keep g (fun _ e => by cases e)) + (fun t y g => ⟨y, H.keep g (fun _ e => by cases e), rfl⟩) + (fun t y g => ⟨y, H.back g (fun _ e => by cases e), rfl, rfl⟩) + +end fields + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtVcpuA.lean b/proofs/Fl4ProofsA64/P3/UtVcpuA.lean new file mode 100644 index 000000000..d355e081d --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtVcpuA.lean @@ -0,0 +1,728 @@ +/- +P3 (aarch64, A64-P3 Untyped W4 leaf; Architect 21:51Z D7): **a retyped VCPU**. `vcpu::init` on a zeroed VCPU leaves +exactly the words of `fl4_vcpu_init default_vcpu` (`Ext/ARMVCPURun`). + +* **`utVcpu_specA`**: the spec's 64-register view of `fl4VcpuInit defaultVcpu`: `utVcpuWordA`. +* **`ut_freshVcpuA`**: `vcpu::init` writes only in the VCPU's 4 KiB; after it the header words (`TCB`, `OWNER_CPU`, + `VPPI_MASKED`, `RUN_CALLER`, `RUN_OFFSET`, `RUN_OUT_MASK`) are zero (`utVcpu_headerA`) and register word `i` is the + spec's register `i`: all 70 words (S4's required form, Architect 22:56Z). + +A standalone leaf: it reads no joint record. S4 consumes it when it adds the register-image relation. + +Premise [P] (`hP`, S4's `VcpuPlatAgreeA` at the backend state): `vcpu_midr`, `vcpu_vgic_vtr` and `vcpu_gic_version` +agree with `fl4A64Platform` and `config_ARM_GIC_V3`. Hypotheses: the VCPU's 4 KiB are zero, aligned and do not wrap. +New definitions and lemmas only. +-/ +import Fl4ProofsA64.P3.UtSpecA +import Fl4ProofsA64.P1.QueueReset +import Fl4ProofsA64.P1.MdbA64 +import Fl4ProofsA64.P1.QueueInv +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## The spec side -/ + +/-- A register write truncates the VGIC `HCR` to 32 bits. -/ +def utRegTruncA (j : Nat) (w : Word) : Word := + if j = vrVgicHcr then ucast 64 (ucast 32 w) else w + +theorem utToEnum_injA : ∀ i < 25, ∀ j < 25, VCPUReg.toEnum i = VCPUReg.toEnum j → i = j := by decide + +/-- Reading after writing a VCPU register other than `VGIC_VMCR` (36, whose width S4's Delta 1 changes; +`vcpu::init` does not write it). -/ +theorem utRegGet_setA (v : Vcpu) (i j : Nat) (hi : i < 64) (hj : j < 64) (hj36 : j ≠ 36) (w : Word) : + fl4VcpuRegGet (fl4VcpuRegSet j w v) i = if i = j then utRegTruncA j w else fl4VcpuRegGet v i := by + have e35 : vrVgicHcr = 35 := rfl + have e36 : vrVgicVmcr = 36 := rfl + have e48 : vrVgicLr0 = 48 := rfl + unfold fl4VcpuRegGet fl4VcpuRegSet utRegTruncA + rw [e35, e48] + have hj36' : ¬ j = vrVgicVmcr := by rw [e36]; exact hj36 + by_cases hij : i = j + · subst hij + rw [ite_eq_left rfl] + by_cases h25 : i < 25 + · simp only [h25, ↓reduceIte, show ¬ i = 35 by omega, Function.update_self] + by_cases h35 : i = 35 + · subst h35; simp + have h36 : ¬ i = vrVgicVmcr := by rw [e36]; exact hj36 + by_cases h48 : 48 ≤ i + · simp only [h25, h35, h36, h48, hi, and_self, ↓reduceIte, Function.update_self] + simp only [h25, h35, h36, h48, hi, false_and, ↓reduceIte, ↓reduceDIte, Function.update_self] + rw [ite_eq_right hij] + by_cases hj25 : j < 25 + · simp only [hj25, ↓reduceIte] + by_cases h25 : i < 25 + · have hne : VCPUReg.toEnum i ≠ VCPUReg.toEnum j := fun h => hij (utToEnum_injA i h25 j hj25 h) + simp only [h25, ↓reduceIte, Function.update_of_ne hne] + · simp only [h25, ↓reduceIte] + simp only [hj25, ↓reduceIte] + by_cases hj35 : j = 35 + · subst hj35 + simp only [↓reduceIte, hij] + simp only [hj35, hj36', ↓reduceIte] + by_cases hj48 : 48 ≤ j + · simp only [hj48, hj, and_self, ↓reduceIte] + by_cases h25 : i < 25 + · simp only [h25, ↓reduceIte] + by_cases h35 : i = 35 + · subst h35; simp + by_cases h36 : i = 36 + · subst h36; simp + by_cases h48 : 48 ≤ i + · have hne : i - 48 ≠ j - 48 := by omega + simp only [h25, h35, h36, h48, hi, and_self, ↓reduceIte, Function.update_of_ne hne] + simp only [h25, h35, h36, h48, false_and, ↓reduceIte] + simp only [hj48, false_and, hj, ↓reduceIte, ↓reduceDIte] + by_cases h25 : i < 25 + · simp only [h25, ↓reduceIte] + by_cases h35 : i = 35 + · subst h35; simp + by_cases h36 : i = 36 + · subst h36 + have hne36 : (36 : Fin 64) ≠ ⟨j, hj⟩ := fun h => hij (congrArg Fin.val h) + simp [Function.update_of_ne hne36] + by_cases h48 : 48 ≤ i + · simp only [h25, h35, h36, h48, hi, and_self, ↓reduceIte] + have hne : (⟨i, hi⟩ : Fin 64) ≠ ⟨j, hj⟩ := fun h => hij (congrArg Fin.val h) + simp only [h25, h35, h36, h48, hi, false_and, ↓reduceIte, ↓reduceDIte, Function.update_of_ne hne] + +/-- The registers of `default_vcpu` other than `SCTLR` and the VGIC `HCR` (both overwritten by the init) are zero. -/ +theorem utRegGet_defaultA (i : Nat) (hi : i < 64) (h0 : i ≠ 0) (h35 : i ≠ 35) : + fl4VcpuRegGet defaultVcpu i = 0 := by + interval_cases i <;> first | exact absurd rfl h0 | exact absurd rfl h35 | rfl + +/-- The VCPU after the six register writes of `vcpu::init`, before the status sync. -/ +def utVcpuPreA : Vcpu := + let v := fl4VcpuRegSet vrSctlr sctlrEl1Default defaultVcpu + let v := fl4VcpuRegSet vrVmpidr vmpidrDefault v + let v := fl4VcpuRegSet vrVpidr fl4A64Platform.vcpuMidr v + let v := fl4VcpuRegSet vrHcr hcrForced1 v + let v := fl4VcpuRegSet vrVgicHcr (1 ||| if fl4GicV3 then (1 <<< 11) else 0) v + fl4VcpuRegSet vrVgicVtr abiVgicVtr v + +/-- The VGIC `HCR` word of a new VCPU. -/ +def utVgicHcrA : Word := if fl4GicV3 then 0x801 else 0x1 + +/-- The registers before the sync. -/ +def utVcpuPreWordA (i : Nat) : Word := + if i = 0 then sctlrEl1Default else if i = 15 then vmpidrDefault else if i = 25 then fl4A64Platform.vcpuMidr + else if i = 26 then hcrForced1 else if i = 34 then abiVgicVtr else if i = 35 then utVgicHcrA else 0 + +theorem utVcpuPre_getA (i : Nat) (hi : i < 64) : fl4VcpuRegGet utVcpuPreA i = utVcpuPreWordA i := by + have e0 : vrSctlr = 0 := rfl + have e15 : vrVmpidr = 15 := rfl + have e25 : vrVpidr = 25 := rfl + have e26 : vrHcr = 26 := rfl + have e34 : vrVgicVtr = 34 := rfl + have e35 : vrVgicHcr = 35 := rfl + have hg : ucast 64 (ucast 32 (1 ||| if fl4GicV3 then (1 <<< 11) else 0 : Word)) = utVgicHcrA := by + unfold utVgicHcrA; cases fl4GicV3 <;> rfl + unfold utVcpuPreA utVcpuPreWordA + simp only [e0, e15, e25, e26, e34, e35] + rw [utRegGet_setA _ i 34 hi (by omega) (by omega), utRegGet_setA _ i 35 hi (by omega) (by omega), + utRegGet_setA _ i 26 hi (by omega) (by omega), utRegGet_setA _ i 25 hi (by omega) (by omega), + utRegGet_setA _ i 15 hi (by omega) (by omega), utRegGet_setA _ i 0 hi (by omega) (by omega)] + simp only [utRegTruncA, e35] + by_cases h34 : i = 34 + · subst h34; simp + by_cases h35 : i = 35 + · subst h35 + simp (config := { decide := true }) only [↓reduceIte] + exact hg + by_cases h26 : i = 26 + · subst h26; simp + by_cases h25 : i = 25 + · subst h25; simp + by_cases h15 : i = 15 + · subst h15; simp + by_cases h0 : i = 0 + · subst h0; simp + simp only [h34, h35, h26, h25, h15, h0, ↓reduceIte] + exact utRegGet_defaultA i hi h0 h35 + +/-- `2^n - 1` as a word. -/ +def utOnesA (n : Nat) : Word := BitVec.ofNat 64 (2 ^ n - 1) + +theorem utOnes_stepA : ∀ n < 16, utOnesA n ||| ((1 : Word) <<< n) = utOnesA (n + 1) := by decide + +theorem utVgicHcr_bitsA : + utVgicHcrA &&& 2 = 0 ∧ utVgicHcrA &&& 4 = 0 ∧ utVgicHcrA &&& 8 = 0 ∧ utVgicHcrA &&& 16 = 0 ∧ + utVgicHcrA &&& 32 = 0 ∧ utVgicHcrA &&& 64 = 0 ∧ utVgicHcrA &&& 128 = 0 := by + unfold utVgicHcrA; cases fl4GicV3 <;> decide + +/-- **The register words of a new VCPU** with `lrs` list registers in use. -/ +def utVcpuWordA (lrs i : Nat) : Word := if i = 39 then utOnesA lrs else utVcpuPreWordA i + +theorem fl4NumLrs_leA : fl4NumLrs ≤ 16 := by + unfold fl4NumLrs + dsimp only + split_ifs <;> omega + +theorem utSync_getA (v : Vcpu) (lrs : Nat) (hl : lrs ≤ 16) (g : Bool) + (hv : ∀ i < 64, fl4VcpuRegGet v i = utVcpuPreWordA i) (i : Nat) (hi : i < 64) : + fl4VcpuRegGet (syncVgicStatus v lrs g) i = utVcpuWordA lrs i := by + let step := fun (acc : Word × Word × Nat × Bool) (i : Nat) => + let (elrsr, eisr, valid, pend) := acc + let (state, hw, eoi) := lrFields (fl4VcpuRegGet v (vrVgicLr0 + i)) g + if state = 0 ∧ (hw ≠ 0 ∨ eoi = 0) then (elrsr ||| ((1 <<< i : Nat) : Word), eisr, valid, pend) + else if state = 0 ∧ hw = 0 ∧ eoi ≠ 0 then (elrsr, eisr ||| ((1 <<< i : Nat) : Word), valid, pend) + else (elrsr, eisr, valid + 1, pend || decide (state = 1)) + have hlr : ∀ k < lrs, fl4VcpuRegGet v (vrVgicLr0 + k) = 0 := by + intro k hk + have e48 : vrVgicLr0 = 48 := rfl + rw [e48, hv _ (by omega)] + unfold utVcpuPreWordA + simp only [show ¬ 48 + k = 0 by omega, show ¬ 48 + k = 15 by omega, show ¬ 48 + k = 25 by omega, + show ¬ 48 + k = 26 by omega, show ¬ 48 + k = 34 by omega, show ¬ 48 + k = 35 by omega, ↓reduceIte] + have hf : ∀ n ≤ lrs, (List.range n).foldl step (0, 0, 0, false) = (utOnesA n, 0, 0, false) := by + intro n + induction n with + | zero => intro _; rfl + | succ n ih => + intro hn + rw [List.range_succ, List.foldl_append, ih (by omega)] + have hz : lrFields (0 : Word) g = (0, 0, 0) := by cases g <;> rfl + simp only [List.foldl_cons, List.foldl_nil, step, hlr n (by omega), hz, ne_eq, not_true_eq_false, false_or, + and_self, ↓reduceIte] + rw [(show ∀ m < 16, utOnesA m ||| ((1 <<< m : Nat) : Word) = utOnesA (m + 1) by decide) n (by omega)] + have hh : fl4VcpuRegGet v vrVgicHcr = utVgicHcrA := by + rw [show vrVgicHcr = 35 from rfl, hv 35 (by omega)]; rfl + have hm : fl4VcpuRegGet v vrVgicVmcr = 0 := by + rw [show vrVgicVmcr = 36 from rfl, hv 36 (by omega)]; rfl + obtain ⟨b1, b2, b3, b4, b5, b6, b7⟩ := utVgicHcr_bitsA + have e : syncVgicStatus v lrs g = + fl4VcpuRegSet vrVgicMisr 0 (fl4VcpuRegSet vrVgicEisr 0 (fl4VcpuRegSet vrVgicElrsr (utOnesA lrs) v)) := by + unfold syncVgicStatus + dsimp only + generalize hr : List.foldl _ _ _ = r + obtain rfl : r = (utOnesA lrs, 0, 0, false) := hr.symm.trans (hf lrs (le_refl _)) + simp only [hh, hm, b1, b2, b3, b4, b5, b6, b7, ne_eq, not_true_eq_false, false_and, ↓reduceIte, + decide_false, Bool.false_eq_true, BitVec.or_self] + rw [e, show vrVgicMisr = 37 from rfl, show vrVgicEisr = 38 from rfl, show vrVgicElrsr = 39 from rfl, + utRegGet_setA _ i 37 hi (by omega) (by omega), utRegGet_setA _ i 38 hi (by omega) (by omega), + utRegGet_setA _ i 39 hi (by omega) (by omega), hv i hi] + unfold utVcpuWordA + have t37 : ∀ w : Word, utRegTruncA 37 w = w := fun w => ite_eq_right (by decide) + have t38 : ∀ w : Word, utRegTruncA 38 w = w := fun w => ite_eq_right (by decide) + have t39 : ∀ w : Word, utRegTruncA 39 w = w := fun w => ite_eq_right (by decide) + simp only [t37, t38, t39] + by_cases h37 : i = 37 + · subst h37; rfl + by_cases h38 : i = 38 + · subst h38; rfl + simp only [h37, h38, ↓reduceIte] + +/-- **The spec's register view of a new VCPU.** -/ +theorem utVcpu_specA (i : Nat) (hi : i < 64) : + fl4VcpuRegGet (fl4VcpuInit defaultVcpu) i = utVcpuWordA fl4NumLrs i := by + have e : ∀ j, fl4VcpuRegGet (fl4VcpuInit defaultVcpu) j = + fl4VcpuRegGet (syncVgicStatus + { utVcpuPreA with vcpuExt := + { utVcpuPreA.vcpuExt with ownerCpu := 0, runCaller := none, runOffset := 0, runOutMask := 0 } } + fl4NumLrs fl4GicV3) j := fun _ => rfl + rw [e] + exact utSync_getA _ _ fl4NumLrs_leA _ (fun k hk => (show fl4VcpuRegGet _ k = fl4VcpuRegGet utVcpuPreA k from rfl).trans + (utVcpuPre_getA k hk)) i hi + +/-- The header fields of a new VCPU, which FL4's words `0x00..0x28` encode as zero. -/ +theorem utVcpu_headerA : + (fl4VcpuInit defaultVcpu).vcpuTcb = none ∧ (fl4VcpuInit defaultVcpu).vcpuExt.ownerCpu = 0 ∧ + (fl4VcpuInit defaultVcpu).vcpuVppiMasked = (fun _ => false) ∧ + (fl4VcpuInit defaultVcpu).vcpuExt.runCaller = none ∧ (fl4VcpuInit defaultVcpu).vcpuExt.runOffset = 0 ∧ + (fl4VcpuInit defaultVcpu).vcpuExt.runOutMask = 0 := + ⟨rfl, rfl, rfl, rfl, rfl, rfl⟩ + +/-! ## The FL4 side -/ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-- Byte offset `K` of the VCPU at `vc`. -/ +def utAtA (vc : U64) (K : Nat) : U64 := wa vc ⟨BitVec.ofNat 64 K⟩ + +theorem utAt_valA {vc : U64} {K : Nat} (hf : vc.val + 4096 ≤ 2 ^ 64) (hK : K < 4096) : + (utAtA vc K).val = vc.val + K := by + have hk : (⟨BitVec.ofNat 64 K⟩ : U64).val = K := by + show (BitVec.ofNat 64 K).toNat = K + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)] + unfold utAtA + rw [wa_valP, hk, Nat.mod_eq_of_lt (by omega)] + +theorem utAt_alA {vc : U64} {K : Nat} (hal : vc.val % 8 = 0) (hf : vc.val + 4096 ≤ 2 ^ 64) (hK : K < 4096) + (h8 : K % 8 = 0) : Aligned (utAtA vc K) := by + show (utAtA vc K).val % 8 = 0 + rw [utAt_valA hf hK]; omega + +/-- Read after write, at offsets of one VCPU. -/ +theorem utAt_wrA {vc : U64} (hal : vc.val % 8 = 0) (hf : vc.val + 4096 ≤ 2 ^ 64) (s : S) (v : U64) {K K' : Nat} + (hK : K < 4096) (h8 : K % 8 = 0) (hK' : K' < 4096) (h8' : K' % 8 = 0) : + L.mem (L.wr s (utAtA vc K) v) (utAtA vc K') = if K = K' then v else L.mem s (utAtA vc K') := by + rw [LawfulStore.mem_wr_val (utAt_alA hal hf hK h8) (utAt_alA hal hf hK' h8'), utAt_valA hf hK, utAt_valA hf hK'] + by_cases h : K = K' + · rw [ite_eq_left h, ite_eq_left (by omega)] + · rw [ite_eq_right h, ite_eq_right (by omega)] + +theorem utAt_ofA {vc k : U64} {K : Nat} (h : k.val = K) : wa vc k = utAtA vc K := by + unfold utAtA + congr 1 + apply UScalar.eq_of_val_eq + show k.val = (BitVec.ofNat 64 K).toNat + have : k.val < 2 ^ 64 := k.bv.isLt + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)]; omega + +theorem ut_reg_addrA (vc idx : U64) (hi : idx.val < 64) : + vcpu.reg_addr vc idx = ok (utAtA vc (64 + 8 * idx.val)) := by + have h64 : (vcpu.REGS).val = 64 := by simp [vcpu.REGS] + have hm : (core.num.U64.wrapping_mul idx 8#u64).val = 8 * idx.val := by + simp [core.num.U64.wrapping_mul_val_eq, U64.size_def, U64.numBits]; omega + unfold vcpu.reg_addr utAtA + simp only [lift, bind_ok] + congr 1 + apply UScalar.eq_of_val_eq + simp only [wa_valP, h64, hm] + have : (⟨BitVec.ofNat 64 (64 + 8 * idx.val)⟩ : U64).val = 64 + 8 * idx.val := by + show (BitVec.ofNat 64 (64 + 8 * idx.val)).toNat = _ + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)] + rw [this] + have : vc.val < 2 ^ 64 := vc.bv.isLt + omega + +theorem ut_reg_setA (s : S) (vc idx v : U64) (hi : idx.val < 64) : + vcpu.reg_set st s vc idx v = ok (L.wr s (utAtA vc (64 + 8 * idx.val)) v) := by + unfold vcpu.reg_set + rw [ut_reg_addrA vc idx hi, bind_ok, L.write_eq] + +theorem ut_reg_getA (s : S) (vc idx : U64) (hi : idx.val < 64) : + vcpu.reg_get st s vc idx = ok (L.mem s (utAtA vc (64 + 8 * idx.val))) := by + unfold vcpu.reg_get + rw [ut_reg_addrA vc idx hi, bind_ok, L.read_eq] + +/-- The list-register scan over zero registers: every register in use is empty. -/ +theorem ut_sync_loopA (s : S) (vc lrs : U64) (g : Bool) (hl : lrs.val ≤ 16) + (hz : ∀ k < lrs.val, L.mem s (utAtA vc (64 + 8 * (48 + k))) = 0#u64) : + vcpu.sync_vgic_status_loop st s vc lrs g 0#u64 0#u64 0#u64 false 0#u64 ⦃ r => + r.1.bv = utOnesA lrs.val ∧ r.2.1 = 0#u64 ∧ r.2.2.1 = 0#u64 ∧ r.2.2.2 = false ⦄ := by + unfold vcpu.sync_vgic_status_loop + refine loop.spec_decr_nat (measure := fun x => lrs.val - x.2.2.2.2.val) + (inv := fun x => x.2.2.2.2.val ≤ lrs.val ∧ x.1.bv = utOnesA x.2.2.2.2.val ∧ x.2.1 = 0#u64 ∧ + x.2.2.1 = 0#u64 ∧ x.2.2.2.1 = false) _ _ _ ?_ ?_ + rotate_left + · exact ⟨Nat.zero_le _, rfl, rfl, rfl, rfl⟩ + rintro ⟨e, ei, vl, hp, i⟩ ⟨hi, he, hei, hvl, hhp⟩ + simp only at hi he hei hvl hhp ⊢ + subst hei hvl hhp + unfold vcpu.sync_vgic_status_loop.body + by_cases hlt : i < lrs + · rw [ite_eq_left hlt] + have hlt' : i.val < lrs.val := hlt + have hL : (fl4_abi.consts.vcpu_reg.VGIC_LR0).val = 48 := by simp [fl4_abi.consts.vcpu_reg.VGIC_LR0] + have hidx : (core.num.U64.wrapping_add fl4_abi.consts.vcpu_reg.VGIC_LR0 i).val = 48 + i.val := by + rw [wa_valP, hL]; omega + simp only [lift, bind_ok] + rw [ut_reg_getA s vc _ (by omega), bind_ok, hidx, hz i.val hlt'] + have hs : ∀ n : I32, 0 ≤ n.val → n.val < 64 → ((0#u64 : U64) >>> n : Result U64) = ok 0#u64 := by + intro n h0 h1 + have : ((0#u64 : U64) >>> n : Result U64) ⦃ z => z = 0#u64 ⦄ := by + step as ⟨z, hz1, hz2⟩ + apply UScalar.eq_of_val_eq + simp [hz1] + obtain ⟨z, ez, hz0⟩ := spec_exists_dut this + rw [ez, hz0] + have hf : (if g then (do + let i2 ← (0#u64 : U64) >>> 62#i32 + let i4 ← (0#u64 : U64) >>> 61#i32 + let i6 ← (0#u64 : U64) >>> 41#i32 + ok (i2 &&& 3#u64, i4 &&& 1#u64, i6 &&& 1#u64)) + else (do + let i2 ← (0#u64 : U64) >>> 28#i32 + let i4 ← (0#u64 : U64) >>> 31#i32 + let i6 ← (0#u64 : U64) >>> 19#i32 + ok (i2 &&& 3#u64, i4 &&& 1#u64, i6 &&& 1#u64))) = ok (0#u64, 0#u64, 0#u64) := by + cases g <;> + simp only [Bool.false_eq_true, ↓reduceIte, hs 62#i32 (by decide) (by decide), + hs 61#i32 (by decide) (by decide), hs 41#i32 (by decide) (by decide), hs 28#i32 (by decide) (by decide), + hs 31#i32 (by decide) (by decide), hs 19#i32 (by decide) (by decide), bind_ok] <;> rfl + rw [hf] + simp only [bind_ok, uncurry_apply_pair] + have h64 : i.val < 64 := by omega + obtain ⟨sh, esh, hsh⟩ : ∃ sh : U64, (1#u64 <<< i : Result U64) = ok sh ∧ sh.bv = (1 : Word) <<< i.val := by + have : (1#u64 <<< i : Result U64) ⦃ z => z.bv = (1 : Word) <<< i.val ⦄ := by + step as ⟨z, hz1, hz2⟩ + exact hz2 + exact spec_exists_dut this + obtain ⟨i1, ei1, hi1⟩ := spec_exists_dut (U64.add_spec (x := i) (y := 1#u64) (by scalar_tac)) + have hi1v : i1.val = i.val + 1 := by simpa using hi1 + have h0ne : ((0#u64 : U64) != 0#u64) = false := rfl + simp only [↓reduceIte, h0ne, Bool.false_eq_true, esh, bind_ok, ei1, uncurry_apply_pair, WP.spec_ok] + refine ⟨⟨by omega, ?_, by trivial, by trivial, by trivial⟩, by omega⟩ + rw [hi1v, ← utOnes_stepA i.val (by omega), ← he, ← hsh] + rfl + · rw [ite_eq_right hlt, WP.spec_ok] + have : i.val = lrs.val := by have : ¬ i.val < lrs.val := hlt; omega + exact ⟨by rw [he, this], rfl, rfl, rfl⟩ + +/-- `sync_vgic_status` on a VCPU whose list registers and `VMCR` are zero and whose `HCR` is a new VCPU's. -/ +theorem ut_sync_statusA (s : S) (vc lrs : U64) (g : Bool) (hl : lrs.val ≤ 16) + (hz : ∀ k < lrs.val, L.mem s (utAtA vc (64 + 8 * (48 + k))) = 0#u64) + (hh : (L.mem s (utAtA vc (64 + 8 * 35))).bv = utVgicHcrA) + (hm : L.mem s (utAtA vc (64 + 8 * 36)) = 0#u64) : + ∃ el : U64, el.bv = utOnesA lrs.val ∧ vcpu.sync_vgic_status st s vc lrs g = + ok (L.wr (L.wr (L.wr s (utAtA vc (64 + 8 * 39)) el) (utAtA vc (64 + 8 * 38)) 0#u64) + (utAtA vc (64 + 8 * 37)) 0#u64) := by + obtain ⟨⟨el, ei, vl, hp⟩, eloop, h1, h2, h3, h4⟩ := spec_exists_dut (ut_sync_loopA (st := st) s vc lrs g hl hz) + simp only at h1 h2 h3 h4 + subst h2 h3 h4 + refine ⟨el, h1, ?_⟩ + have hv35 : (fl4_abi.consts.vcpu_reg.VGIC_HCR).val = 35 := by simp [fl4_abi.consts.vcpu_reg.VGIC_HCR] + have hv36 : (fl4_abi.consts.vcpu_reg.VGIC_VMCR).val = 36 := by simp [fl4_abi.consts.vcpu_reg.VGIC_VMCR] + have hv37 : (fl4_abi.consts.vcpu_reg.VGIC_MISR).val = 37 := by simp [fl4_abi.consts.vcpu_reg.VGIC_MISR] + have hv38 : (fl4_abi.consts.vcpu_reg.VGIC_EISR).val = 38 := by simp [fl4_abi.consts.vcpu_reg.VGIC_EISR] + have hv39 : (fl4_abi.consts.vcpu_reg.VGIC_ELRSR).val = 39 := by simp [fl4_abi.consts.vcpu_reg.VGIC_ELRSR] + have hcr : L.mem s (utAtA vc (64 + 8 * 35)) = ⟨utVgicHcrA⟩ := by + rw [UScalar.eq_equiv_bv_eq]; exact hh + unfold vcpu.sync_vgic_status + rw [eloop] + simp only [bind_ok, uncurry_apply_pair] + rw [ut_reg_getA s vc _ (by omega), bind_ok, ut_reg_getA s vc _ (by omega), bind_ok, hv35, hv36, hcr, hm] + have key : ∀ k : Std.U64 → Result S, + (do + let misr ← + if (0#u64 : U64) != 0#u64 then do + let i ← (1#u64 <<< 0#i32 : Result U64) + ok (0#u64 ||| i) + else ok 0#u64 + let i ← (1#u64 <<< 1#i32 : Result U64) + let i1 ← lift ((⟨utVgicHcrA⟩ : U64) &&& i) + let misr1 ← + if i1 != 0#u64 then if (0#u64 : U64) <= 1#u64 then ok (misr ||| i) else ok misr else ok misr + let i2 ← (1#u64 <<< 2#i32 : Result U64) + let i3 ← lift ((⟨utVgicHcrA⟩ : U64) &&& i2) + let misr2 ← + if i3 != 0#u64 then do + let i4 ← ((⟨utVgicHcrA⟩ : U64) >>> 27#i32 : Result U64) + let i5 ← lift (i4 &&& 31#u64) + if i5 != 0#u64 then ok (misr1 ||| i2) else ok misr1 + else ok misr1 + let i4 ← (1#u64 <<< 3#i32 : Result U64) + let i5 ← lift ((⟨utVgicHcrA⟩ : U64) &&& i4) + let misr3 ← + if i5 != 0#u64 then if false then ok misr2 else ok (misr2 ||| i4) else ok misr2 + let i6 ← (1#u64 <<< 4#i32 : Result U64) + let i7 ← lift ((⟨utVgicHcrA⟩ : U64) &&& i6) + let misr4 ← + if i7 != 0#u64 then do + let i8 ← (1#u64 <<< 0#i32 : Result U64) + let i9 ← lift ((0#u64 : U64) &&& i8) + if i9 != 0#u64 then ok (misr3 ||| i6) else ok misr3 + else ok misr3 + let i8 ← (1#u64 <<< 5#i32 : Result U64) + let i9 ← lift ((⟨utVgicHcrA⟩ : U64) &&& i8) + let misr5 ← + if i9 != 0#u64 then do + let i10 ← (1#u64 <<< 0#i32 : Result U64) + let i11 ← lift ((0#u64 : U64) &&& i10) + if i11 = 0#u64 then ok (misr4 ||| i8) else ok misr4 + else ok misr4 + let i10 ← (1#u64 <<< 6#i32 : Result U64) + let i11 ← lift ((⟨utVgicHcrA⟩ : U64) &&& i10) + let misr6 ← + if i11 != 0#u64 then do + let i12 ← lift ((0#u64 : U64) &&& i) + if i12 != 0#u64 then ok (misr5 ||| i10) else ok misr5 + else ok misr5 + let i12 ← (1#u64 <<< 7#i32 : Result U64) + let i13 ← lift ((⟨utVgicHcrA⟩ : U64) &&& i12) + let misr7 ← + if i13 != 0#u64 then do + let i14 ← lift ((0#u64 : U64) &&& i) + if i14 = 0#u64 then ok (misr6 ||| i12) else ok misr6 + else ok misr6 + k misr7) = k 0#u64 := by + intro k + have s0 : (1#u64 <<< 0#i32 : Result U64) = ok 1#u64 := rfl + have s1 : (1#u64 <<< 1#i32 : Result U64) = ok 2#u64 := rfl + have s2 : (1#u64 <<< 2#i32 : Result U64) = ok 4#u64 := rfl + have s3 : (1#u64 <<< 3#i32 : Result U64) = ok 8#u64 := rfl + have s4 : (1#u64 <<< 4#i32 : Result U64) = ok 16#u64 := rfl + have s5 : (1#u64 <<< 5#i32 : Result U64) = ok 32#u64 := rfl + have s6 : (1#u64 <<< 6#i32 : Result U64) = ok 64#u64 := rfl + have s7 : (1#u64 <<< 7#i32 : Result U64) = ok 128#u64 := rfl + have hw : (⟨utVgicHcrA⟩ : U64) = if fl4GicV3 then 2049#u64 else 1#u64 := by + unfold utVgicHcrA; cases fl4GicV3 <;> rfl + have hand : ∀ c : U64, c = 2#u64 ∨ c = 4#u64 ∨ c = 8#u64 ∨ c = 16#u64 ∨ c = 32#u64 ∨ c = 64#u64 ∨ c = 128#u64 → + (⟨utVgicHcrA⟩ : U64) &&& c = 0#u64 := by + intro c hc + rw [hw] + rcases hc with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> cases fl4GicV3 <;> rfl + have a2 := hand 2#u64 (by simp) + have a4 := hand 4#u64 (by simp) + have a8 := hand 8#u64 (by simp) + have a16 := hand 16#u64 (by simp) + have a32 := hand 32#u64 (by simp) + have a64 := hand 64#u64 (by simp) + have a128 := hand 128#u64 (by simp) + have n0 : ((0#u64 : U64) != 0#u64) = false := rfl + simp only [s0, s1, s2, s3, s4, s5, s6, s7, a2, a4, a8, a16, a32, a64, a128, n0, lift, bind_ok, + Bool.false_eq_true, ↓reduceIte] + have e := key (fun misr7 => do + let s1 ← vcpu.reg_set st s vc fl4_abi.consts.vcpu_reg.VGIC_ELRSR el + let s2 ← vcpu.reg_set st s1 vc fl4_abi.consts.vcpu_reg.VGIC_EISR 0#u64 + vcpu.reg_set st s2 vc fl4_abi.consts.vcpu_reg.VGIC_MISR misr7) + refine Eq.trans e ?_ + rw [ut_reg_setA s vc _ _ (by omega), bind_ok, ut_reg_setA _ vc _ _ (by omega), bind_ok, + ut_reg_setA _ vc _ _ (by omega), hv39, hv38, hv37] + +/-- `abi_vgic_vtr` is the spec's `abiVgicVtr`. -/ +theorem ut_abi_vtrA (vtr : U64) (g : Bool) (hv : vtr.bv = fl4A64Platform.vgicVtr) (hg : g = fl4GicV3) : + ∃ x : U64, vcpu.abi_vgic_vtr vtr g = ok x ∧ x.bv = abiVgicVtr := by + have h0 : vtr = 0#u64 ↔ fl4A64Platform.vgicVtr = 0 := by + rw [← hv, UScalar.eq_equiv_bv_eq]; rfl + have hge : ∀ x : U64, (x >= 16#u64) ↔ x.bv ≥ 16 := fun x => by + show (16#u64 : U64).val ≤ x.val ↔ _ + rfl + have hz' : ¬ vtr = 0#u64 → ¬ vtr.bv = 0 := fun hz h => hz (h0.2 (hv ▸ h)) + unfold vcpu.abi_vgic_vtr abiVgicVtr + rw [← hg] + by_cases hz : vtr = 0#u64 + · rw [ite_eq_left hz] + exact ⟨0#u64, rfl, by rw [← hv, ite_eq_left (show vtr.bv = 0 by rw [hz]; rfl)]; rfl⟩ + rw [ite_eq_right hz] + simp only [← hv] + have hnz := hz' hz + cases g + · simp only [Bool.false_eq_true, ↓reduceIte, lift, bind_ok] + by_cases h16 : (vtr &&& 63#u64) >= 16#u64 + · have : vtr.bv &&& 0x3f ≥ 16 := (hge _).1 h16 + rw [ite_eq_left h16] + exact ⟨_, rfl, by rw [ite_eq_right hnz, ite_eq_left this]; rfl⟩ + · have : ¬ vtr.bv &&& 0x3f ≥ 16 := fun h => h16 ((hge _).2 h) + rw [ite_eq_right h16] + exact ⟨_, rfl, by rw [ite_eq_right hnz, ite_eq_right this]⟩ + · simp only [↓reduceIte, lift, bind_ok] + by_cases h16 : (vtr &&& 31#u64) >= 16#u64 + · have : vtr.bv &&& 0x1f ≥ 16 := (hge _).1 h16 + rw [ite_eq_left h16] + exact ⟨_, rfl, by rw [ite_eq_right hnz, ite_eq_left this]; rfl⟩ + · have : ¬ vtr.bv &&& 0x1f ≥ 16 := fun h => h16 ((hge _).2 h) + rw [ite_eq_right h16] + exact ⟨_, rfl, by rw [ite_eq_right hnz, ite_eq_right this]⟩ + +/-- `num_lrs` is the spec's `fl4NumLrs`. -/ +theorem ut_num_lrsA (ar : machine.Arch A) (a : A) (vtr gv : U64) (hvtr : ar.vcpu_vgic_vtr a = ok vtr) + (hv : vtr.bv = fl4A64Platform.vgicVtr) (hgv : ar.vcpu_gic_version a = ok gv) + (hg : decide (gv = 3#u64) = fl4GicV3) : + ∃ n : U64, vcpu.num_lrs ar a = ok n ∧ n.val = fl4NumLrs := by + have h0 : vtr = 0#u64 ↔ fl4A64Platform.vgicVtr = 0 := by + rw [← hv, UScalar.eq_equiv_bv_eq]; rfl + unfold vcpu.num_lrs fl4NumLrs + rw [hvtr, bind_ok] + by_cases hz : vtr = 0#u64 + · rw [ite_eq_left hz] + exact ⟨0#u64, rfl, by rw [← hv, ite_eq_left (show vtr.bv = 0 by rw [hz]; rfl)]; rfl⟩ + rw [ite_eq_right hz, hgv, bind_ok] + have hnz : ¬ vtr.bv = 0 := fun h => hz (h0.2 (hv ▸ h)) + simp only [← hv, ite_eq_right hnz] + have hmin : ∀ (x : U64), x.val ≤ 63 → + ∃ n : U64, (do let i2 ← x + 1#u64; ok (core.cmp.impls.OrdU64.min i2 16#u64)) = ok n ∧ + n.val = min (x.val + 1) 16 := by + intro x hx + obtain ⟨y, ey, hy⟩ := spec_exists_dut (U64.add_spec (x := x) (y := 1#u64) (by scalar_tac)) + have hyv : y.val = x.val + 1 := by simpa using hy + refine ⟨_, by rw [ey, bind_ok], ?_⟩ + unfold core.cmp.impls.OrdU64.min + have h16 : (16#u64 : U64).val = 16 := rfl + split + all_goals (rename_i h; have h' := h; simp only [UScalar.lt_equiv, h16, hyv] at h' ⊢; omega) + by_cases h3 : gv = 3#u64 + · have hg3 : fl4GicV3 = true := by rw [← hg]; simp [h3] + rw [ite_eq_left h3, hg3] + simp only [lift, bind_ok, ↓reduceIte] + have hb : (vtr &&& 31#u64).val = (vtr.bv &&& 0x1f).toNat := rfl + obtain ⟨n, en, hn⟩ := hmin (vtr &&& 31#u64) (by + have : (vtr &&& 31#u64).val ≤ 31 := by rw [UScalar.val_and]; exact Nat.and_le_right + omega) + exact ⟨n, en, by rw [hn, hb]⟩ + · have hg3 : fl4GicV3 = false := by rw [← hg]; simp [h3] + rw [ite_eq_right h3, hg3] + simp only [lift, bind_ok, Bool.false_eq_true, ↓reduceIte] + have hb : (vtr &&& 63#u64).val = (vtr.bv &&& 0x3f).toNat := rfl + obtain ⟨n, en, hn⟩ := hmin (vtr &&& 63#u64) (by + rw [UScalar.val_and]; exact Nat.and_le_right) + exact ⟨n, en, by rw [hn, hb]⟩ + +/-- **A fresh VCPU** (module docstring; S4's required form, `a64s4vcpu-contract.md` §2.3): `vcpu::init` on a zeroed +VCPU writes only in its 4 KiB, leaves the six header words zero and register word `i` the spec's register `i` of +`fl4_vcpu_init default_vcpu`. `hP` is S4's [P] `VcpuPlatAgreeA ar` at `a`. -/ +theorem ut_freshVcpuA (ar : machine.Arch A) (s : S) (a : A) (vc : U64) (hal : Aligned vc) + (hf : vc.val + 4096 ≤ 2 ^ 64) + (hz : ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → L.mem s (wa vc o) = 0#u64) + (hP : ar.vcpu_midr a = ok ⟨fl4A64Platform.vcpuMidr⟩ ∧ ar.vcpu_vgic_vtr a = ok ⟨fl4A64Platform.vgicVtr⟩ ∧ + ∃ g : U64, ar.vcpu_gic_version a = ok g ∧ decide (g = 3#u64) = config_ARM_GIC_V3) : + vcpu.init st ar s a vc ⦃ s' => + WritesIn st s s' (fun b => ∃ o : U64, o.val < 4096 ∧ o.val % 8 = 0 ∧ b = wa vc o) ∧ + (∀ K, K = 0 ∨ K = 8 ∨ K = 16 ∨ K = 24 ∨ K = 32 ∨ K = 40 → + L.mem s' (wa vc ⟨BitVec.ofNat 64 K⟩) = 0#u64) ∧ + (∀ i < 64, (L.mem s' (wa vc ⟨BitVec.ofNat 64 (64 + 8 * i)⟩)).bv = + fl4VcpuRegGet (fl4VcpuInit defaultVcpu) i) ⦄ := by + obtain ⟨hmidr, hvtr, gv, hgv, hg3'⟩ := hP + obtain ⟨midr, hmdef⟩ : ∃ x : U64, x = ⟨fl4A64Platform.vcpuMidr⟩ := ⟨_, rfl⟩ + obtain ⟨vtr, hvdef⟩ : ∃ x : U64, x = ⟨fl4A64Platform.vgicVtr⟩ := ⟨_, rfl⟩ + rw [← hmdef] at hmidr + rw [← hvdef] at hvtr + have hm : midr.bv = fl4A64Platform.vcpuMidr := by rw [hmdef] + have hv : vtr.bv = fl4A64Platform.vgicVtr := by rw [hvdef] + have hg3 : decide (gv = 3#u64) = fl4GicV3 := hg3' + have hal' : vc.val % 8 = 0 := hal + have hzK : ∀ K < 4096, K % 8 = 0 → L.mem s (utAtA vc K) = 0#u64 := by + intro K hK h8 + have hk : (⟨BitVec.ofNat 64 K⟩ : U64).val = K := by + show (BitVec.ofNat 64 K).toNat = K + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)] + exact hz _ (by omega) (by omega) + -- the writes stay in the VCPU's page + have hW : ∀ (s1 s2 : S) (K : Nat) (v : U64), K < 4096 → K % 8 = 0 → + WritesIn st s1 s2 (fun b => ∃ o : U64, o.val < 4096 ∧ o.val % 8 = 0 ∧ b = wa vc o) → + WritesIn st s1 (L.wr s2 (utAtA vc K) v) (fun b => ∃ o : U64, o.val < 4096 ∧ o.val % 8 = 0 ∧ b = wa vc o) := by + intro s1 s2 K v hK h8 ⟨k1, k2, k3⟩ + refine ⟨by rw [L.ksb_wr, k1], by rw [L.cpu_wr, k2], fun b hb hnb => ?_⟩ + have hk : (⟨BitVec.ofNat 64 K⟩ : U64).val = K := by + show (BitVec.ofNat 64 K).toNat = K + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)] + have hne : utAtA vc K ≠ b := fun e => hnb ⟨⟨BitVec.ofNat 64 K⟩, by omega, by omega, e.symm⟩ + rw [L.mem_wr s2 _ v b (utAt_alA hal' hf hK h8) hb, ite_eq_right hne, k3 b hb hnb] + suffices h : vcpu.init st ar s a vc ⦃ s' => + WritesIn st s s' (fun b => ∃ o : U64, o.val < 4096 ∧ o.val % 8 = 0 ∧ b = wa vc o) ∧ + (∀ K, K = 0 ∨ K = 8 ∨ K = 16 ∨ K = 24 ∨ K = 32 ∨ K = 40 → L.mem s' (utAtA vc K) = 0#u64) ∧ + (∀ i < 64, (L.mem s' (utAtA vc (64 + 8 * i))).bv = fl4VcpuRegGet (fl4VcpuInit defaultVcpu) i) ⦄ from h + have o0 : (vcpu.TCB).val = 0 := by simp [vcpu.TCB] + have o1 : (vcpu.OWNER_CPU).val = 8 := by simp [vcpu.OWNER_CPU] + have o2 : (vcpu.VPPI_MASKED).val = 16 := by simp [vcpu.VPPI_MASKED] + have o3 : (vcpu.RUN_CALLER).val = 24 := by simp [vcpu.RUN_CALLER] + have o4 : (vcpu.RUN_OFFSET).val = 32 := by simp [vcpu.RUN_OFFSET] + have o5 : (vcpu.RUN_OUT_MASK).val = 40 := by simp [vcpu.RUN_OUT_MASK] + have r0 : (fl4_abi.consts.vcpu_reg.SCTLR).val = 0 := by simp [fl4_abi.consts.vcpu_reg.SCTLR] + have r15 : (fl4_abi.consts.vcpu_reg.VMPIDR_EL2).val = 15 := by simp [fl4_abi.consts.vcpu_reg.VMPIDR_EL2] + have r25 : (fl4_abi.consts.vcpu_reg.VPIDR_EL2).val = 25 := by simp [fl4_abi.consts.vcpu_reg.VPIDR_EL2] + have r26 : (fl4_abi.consts.vcpu_reg.HCR_EL2).val = 26 := by simp [fl4_abi.consts.vcpu_reg.HCR_EL2] + have r34 : (fl4_abi.consts.vcpu_reg.VGIC_VTR).val = 34 := by simp [fl4_abi.consts.vcpu_reg.VGIC_VTR] + have r35 : (fl4_abi.consts.vcpu_reg.VGIC_HCR).val = 35 := by simp [fl4_abi.consts.vcpu_reg.VGIC_HCR] + obtain ⟨hcr, ehcr, vhcr⟩ : ∃ x : U64, vcpu.HCR_FORCED_1 = ok x ∧ x.bv = hcrForced1 := by + obtain ⟨x, ex, hx⟩ := spec_exists_dut Fl4ProofsA64.P0.Consts.vcpu.HCR_FORCED_1.spec + exact ⟨x, ex, by rw [hx]; rfl⟩ + obtain ⟨vh, evh, vvh⟩ : ∃ x : U64, (if gv = 3#u64 then (1#u64 <<< 11#i32 : Result U64) else ok 0#u64) = ok x ∧ + (1#u64 ||| x).bv = utVgicHcrA := by + unfold utVgicHcrA + by_cases h : gv = 3#u64 + · have hg : fl4GicV3 = true := by rw [← hg3]; simp [h] + rw [ite_eq_left h, hg]; exact ⟨_, rfl, rfl⟩ + · have hg : fl4GicV3 = false := by rw [← hg3]; simp [h] + rw [ite_eq_right h, hg]; exact ⟨_, rfl, rfl⟩ + obtain ⟨vt, evt, vvt⟩ := ut_abi_vtrA vtr (decide (gv = 3#u64)) hv hg3 + obtain ⟨n, en, vn⟩ := ut_num_lrsA ar a vtr gv hvtr hv hgv hg3 + have hn16 : n.val ≤ 16 := by rw [vn]; exact fl4NumLrs_leA + unfold vcpu.init + simp only [lift, bind_ok, L.write_eq] + rw [utAt_ofA o0, utAt_ofA o1, utAt_ofA o2, utAt_ofA o3, utAt_ofA o4, utAt_ofA o5, + ut_reg_setA _ vc _ _ (by omega), bind_ok, ut_reg_setA _ vc _ _ (by omega), bind_ok, hmidr, bind_ok, + ut_reg_setA _ vc _ _ (by omega), bind_ok, ehcr, bind_ok, ut_reg_setA _ vc _ _ (by omega), bind_ok, hgv, bind_ok, + evh, bind_ok, ut_reg_setA _ vc _ _ (by omega), bind_ok, hvtr, bind_ok, evt, bind_ok, + ut_reg_setA _ vc _ _ (by omega), bind_ok, en, bind_ok, r0, r15, r25, r26, r35, r34] + set s12 := L.wr (L.wr (L.wr (L.wr (L.wr (L.wr (L.wr (L.wr (L.wr (L.wr (L.wr (L.wr s + (utAtA vc 0) 0#u64) (utAtA vc 8) 0#u64) (utAtA vc 16) 0#u64) (utAtA vc 24) 0#u64) (utAtA vc 32) 0#u64) + (utAtA vc 40) 0#u64) (utAtA vc (64 + 8 * 0)) vcpu.SCTLR_EL1_DEFAULT) + (utAtA vc (64 + 8 * 15)) vcpu.VMPIDR_DEFAULT) (utAtA vc (64 + 8 * 25)) midr) (utAtA vc (64 + 8 * 26)) hcr) + (utAtA vc (64 + 8 * 35)) (1#u64 ||| vh)) (utAtA vc (64 + 8 * 34)) vt with hs12 + have h12 : ∀ K < 4096, K % 8 = 0 → L.mem s12 (utAtA vc K) = + if K = 64 + 8 * 34 then vt else if K = 64 + 8 * 35 then 1#u64 ||| vh else if K = 64 + 8 * 26 then hcr + else if K = 64 + 8 * 25 then midr else if K = 64 + 8 * 15 then vcpu.VMPIDR_DEFAULT + else if K = 64 + 8 * 0 then vcpu.SCTLR_EL1_DEFAULT else 0#u64 := by + intro K hK h8 + rw [hs12] + simp only [utAt_wrA hal hf _ _ (show 64 + 8 * 34 < 4096 by omega) (by omega) hK h8, + utAt_wrA hal hf _ _ (show 64 + 8 * 35 < 4096 by omega) (by omega) hK h8, + utAt_wrA hal hf _ _ (show 64 + 8 * 26 < 4096 by omega) (by omega) hK h8, + utAt_wrA hal hf _ _ (show 64 + 8 * 25 < 4096 by omega) (by omega) hK h8, + utAt_wrA hal hf _ _ (show 64 + 8 * 15 < 4096 by omega) (by omega) hK h8, + utAt_wrA hal hf _ _ (show 64 + 8 * 0 < 4096 by omega) (by omega) hK h8, + utAt_wrA hal hf _ _ (show 40 < 4096 by omega) (by omega) hK h8, + utAt_wrA hal hf _ _ (show 32 < 4096 by omega) (by omega) hK h8, + utAt_wrA hal hf _ _ (show 24 < 4096 by omega) (by omega) hK h8, + utAt_wrA hal hf _ _ (show 16 < 4096 by omega) (by omega) hK h8, + utAt_wrA hal hf _ _ (show 8 < 4096 by omega) (by omega) hK h8, + utAt_wrA hal hf _ _ (show 0 < 4096 by omega) (by omega) hK h8, hzK K hK h8, + eq_comm (b := K), ite_self] + have hz12 : ∀ k < n.val, L.mem s12 (utAtA vc (64 + 8 * (48 + k))) = 0#u64 := by + intro k hk + rw [h12 _ (by omega) (by omega)] + simp only [show ¬ 64 + 8 * (48 + k) = 64 + 8 * 34 by omega, show ¬ 64 + 8 * (48 + k) = 64 + 8 * 35 by omega, + show ¬ 64 + 8 * (48 + k) = 64 + 8 * 26 by omega, show ¬ 64 + 8 * (48 + k) = 64 + 8 * 25 by omega, + show ¬ 64 + 8 * (48 + k) = 64 + 8 * 15 by omega, show ¬ 64 + 8 * (48 + k) = 64 + 8 * 0 by omega, ↓reduceIte] + have hh12 : (L.mem s12 (utAtA vc (64 + 8 * 35))).bv = utVgicHcrA := by + rw [h12 _ (by omega) (by omega)]; exact vvh + have hm12 : L.mem s12 (utAtA vc (64 + 8 * 36)) = 0#u64 := by + rw [h12 _ (by omega) (by omega)]; rfl + obtain ⟨el, vel, esync⟩ := ut_sync_statusA (st := st) s12 vc n (decide (gv = 3#u64)) hn16 hz12 hh12 hm12 + rw [esync, WP.spec_ok] + have hfin : ∀ K < 4096, K % 8 = 0 → + L.mem (L.wr (L.wr (L.wr s12 (utAtA vc (64 + 8 * 39)) el) (utAtA vc (64 + 8 * 38)) 0#u64) + (utAtA vc (64 + 8 * 37)) 0#u64) (utAtA vc K) = + if K = 64 + 8 * 37 then 0#u64 else if K = 64 + 8 * 38 then 0#u64 else if K = 64 + 8 * 39 then el + else L.mem s12 (utAtA vc K) := by + intro K hK h8 + simp only [utAt_wrA hal hf _ _ (show 64 + 8 * 37 < 4096 by omega) (by omega) hK h8, + utAt_wrA hal hf _ _ (show 64 + 8 * 38 < 4096 by omega) (by omega) hK h8, + utAt_wrA hal hf _ _ (show 64 + 8 * 39 < 4096 by omega) (by omega) hK h8, eq_comm (b := K)] + refine ⟨?_, ?_, ?_⟩ + · rw [hs12] + repeat' (first + | exact WritesIn.refl _ _ + | refine hW _ _ _ _ (by omega) (by omega) ?_) + · intro K hK + have hK' : K < 64 ∧ K % 8 = 0 := by omega + rw [hfin K (by omega) hK'.2, h12 K (by omega) hK'.2] + simp only [show ¬ K = 64 + 8 * 37 by omega, show ¬ K = 64 + 8 * 38 by omega, show ¬ K = 64 + 8 * 39 by omega, + show ¬ K = 64 + 8 * 34 by omega, show ¬ K = 64 + 8 * 35 by omega, show ¬ K = 64 + 8 * 26 by omega, + show ¬ K = 64 + 8 * 25 by omega, show ¬ K = 64 + 8 * 15 by omega, show ¬ K = 64 + 8 * 0 by omega, ↓reduceIte] + · intro i hi + rw [utVcpu_specA i hi, hfin _ (by omega) (by omega), h12 _ (by omega) (by omega)] + have e : ∀ k : Nat, (64 + 8 * i = 64 + 8 * k) = (i = k) := fun k => propext (by omega) + simp only [e] + unfold utVcpuWordA utVcpuPreWordA + by_cases h39 : i = 39 + · subst h39; simpa [vn] using vel + by_cases h38 : i = 38 + · subst h38; rfl + by_cases h37 : i = 37 + · subst h37; rfl + simp only [h37, h38, h39, ↓reduceIte] + by_cases h34 : i = 34 + · subst h34; simpa using vvt + by_cases h35 : i = 35 + · subst h35; simpa using vvh + by_cases h26 : i = 26 + · subst h26; simpa using vhcr + by_cases h25 : i = 25 + · subst h25; simpa using hm + by_cases h15 : i = 15 + · subst h15 + simp (config := { decide := true }) only [↓reduceIte] + unfold vcpu.VMPIDR_DEFAULT; rfl + by_cases h0 : i = 0 + · subst h0 + simp (config := { decide := true }) only [↓reduceIte] + unfold vcpu.SCTLR_EL1_DEFAULT; rfl + simp only [h34, h35, h26, h25, h15, h0, ↓reduceIte] + rfl + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtVsConvA.lean b/proofs/Fl4ProofsA64/P3/UtVsConvA.lean new file mode 100644 index 000000000..57d40e577 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtVsConvA.lean @@ -0,0 +1,428 @@ +/- +P3 (aarch64, A64-P3 Untyped; Architect 04:38Z, ruling 2): **the converse bridge `VsPathA` / `S2ReachA` → `S1aPath` +at R2 states**, the converse of `P3/Stage3A`'s `of_s1aPath`, and from it **`UtVsClosA`**. + +`S1aPath` walks only the user range; `VsPathA` walks all 512 slots. At an R2 state the slots `S1aPath` leaves out are +zero (`S1aRootSlots`): a native root's slots 256..511 (`native`, so slot 511 too), a high window's L1 slots outside +384..495 (`highPrivate`); below the top level every slot is in range. + +* **`utS1aPath_of_vsPathA`**: Stage-1 and high-window walks. +* **`utS1aPath_of_s2ReachA`**: Stage-2 walks (`n` the platform's IPA width). +* **`utVsClos_of_r2A`**: `UtVsClosA` from `S1aR2`, `VsRelA`, `ExtVsRelA` and `named`: no table or root a cap of `OS` + names is removed (a childless untyped). + +No named premise. No existing statement of 1A changes. +-/ +import Fl4ProofsA64.P3.Stage3A +import Fl4ProofsA64.P3.UtDropVsA +import Fl4ProofsA64.P1.S1aR2KtLevel +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {H : Heap} {OS Pt Pp' Tb' Hr Te Kt : Set U64} {Lv' Re bits : U64 → Nat} {n : U64} {s : S} +variable {Pp Tb X Y T2 : Set U64} {Lv Sh2 : U64 → Nat} {σ : Fl4SpecA64.State Unit} + +theorem utAddrTabA (w : U64) : wa (w &&& vspace.ADDR_MASK) layout.PPTR_BASE = dTabA w := by + unfold vspace.ADDR_MASK layout.PPTR_BASE dTabA dAddrA; rfl + +theorem utAnd3A (e : U64) (h0 : dBitA e 0 = true) (h1 : dBitA e 1 = true) : (e &&& 3#u64) = 3#u64 := by + have d0 : e.val % 2 = 1 := by + have h : e.bv.getLsbD 0 = true := h0 + rw [BitVec.getLsbD, Nat.testBit_eq_decide_div_mod_eq] at h + simpa using h + have d1 : e.val / 2 % 2 = 1 := by + have h : e.bv.getLsbD 1 = true := h1 + rw [BitVec.getLsbD, Nat.testBit_eq_decide_div_mod_eq] at h + simpa using h + apply UScalar.eq_of_val_eq + rw [and3_valA] + show e.val % 4 = 3 + omega + +/-- A zero descriptor is no table descriptor. -/ +theorem utZeroNotTabA {e : U64} (h3 : (e &&& 3#u64) = 3#u64) : e ≠ 0#u64 := by + rintro rfl + have := congrArg UScalar.val h3 + rw [and3_valA] at this + simp at this + +/-- A live ASID-table entry of `VsRelA` is one of R2's. -/ +theorem utActiveAsid_p1A (R : S1aR2 st H OS Pt Pp' Tb' Hr Te Kt Lv' Re bits n s) {asid raw : U64} + (h : ActiveAsidRootA (L.mem s) (L.ksb s) Pp asid raw) : ActiveAsidRoot st Pp' s asid raw := by + obtain ⟨hi, lo, hhi, hlo, ha, pool, -, hpne, htab, hw, hrne⟩ := h + have hat : wa (L.ksb s) (AtOff hi) = atAF (L.ksb s) hi := by + unfold AtOff atAF + have h127 : (hi &&& 127#u64) = hi := UScalar.eq_of_val_eq (by + rw [and_maskA hi 7 _ rfl]; exact Nat.mod_eq_of_lt (by omega)) + rw [h127] + have htab' : L.mem s (wa (L.ksb s) (AtOff hi)) = pool := by rw [hat]; exact htab + have hP := (R.poolCaps.present hi hhi pool htab' hpne).1 + have ev := wm8_valA hlo + refine ⟨hi, core.num.U64.wrapping_mul lo 8#u64, hhi, by omega, by omega, by omega, pool, ?_, htab', hw, hrne⟩ + exact ⟨hP, AtOff hi, (AtOff.v hi).1, (AtOff.v hi).2.1, (AtOff.v hi).2.2, htab', hpne⟩ + +/-- The invariant of a Stage-1 / high-window walk. -/ +def UtS1InvA (st : machine.Store S) [L : LawfulStore st] (OS Pp' : Set U64) (n : U64) (s : S) (tag : U64) (sh : Nat) + (q : U64) : Prop := + ∃ asid va, S1aPath st Pp' n s tag asid sh va q ∧ + ((tag = 0#u64 ∧ sh = 39 ∧ va = 0 ∧ ∃ x ∈ OS, ∃ a, capA st s x = .Pml4 q true a) ∨ + (tag = 2#u64 ∧ sh = 30 ∧ va = 2 ^ 64 - 2 ^ 39 ∧ + ∃ x ∈ OS, ∃ r a, capA st s x = .HighVSpace r true a ∧ q = wa r 4096#u64) ∨ + (sh < s1aTopShift tag n ∧ s1aUserRange tag n va (2 ^ (sh + 9)))) + +/-- **Stage-1 and high-window walks** (module docstring). -/ +theorem utS1aPath_of_vsPathA (R : S1aR2 st H OS Pt Pp' Tb' Hr Te Kt Lv' Re bits n s) + (E : ExtVsRelA Pp Tb X Y T2 Sh2 (L.mem s) σ) {tag : U64} {sh : Nat} {q : U64} + (h : VsPathA Pp Tb Y (L.mem s) (L.ksb s) tag sh q) : UtS1InvA st OS Pp' n s tag sh q := by + have hU : fl4_abi.consts.USER_TOP.val = 140737488355327 := by simp [fl4_abi.consts.USER_TOP] + have hB : fl4_abi.consts.HIGH_VSPACE_BASE.val = 18446743936270598144 := by + simp [fl4_abi.consts.HIGH_VSPACE_BASE] + have hT : fl4_abi.consts.HIGH_VSPACE_TOP.val = 18446744056529682432 := by + simp [fl4_abi.consts.HIGH_VSPACE_TOP] + have t0 : s1aTopShift 0#u64 n = 39 := by simp [s1aTopShift] + have h20 : (2#u64 : U64) ≠ 0#u64 := by decide + have h01 : (0#u64 : U64) ≠ 1#u64 := by decide + have h21 : (2#u64 : U64) ≠ 1#u64 := by decide + have t2 : s1aTopShift 2#u64 n = 30 := by simp [s1aTopShift, h20] + induction h with + | @native asid raw hact htag _ => + have hr0 : raw &&& 3#u64 = 0#u64 := (tag_iffA raw).2 htag + have ha := utActiveAsid_p1A R hact + obtain ⟨x, hx, hc⟩ := R.nativeOf ha hr0 + exact ⟨asid, 0, .native ha hr0, Or.inl ⟨rfl, rfl, rfl, x, hx, asid, hc⟩⟩ + | @high asid raw hact htag hy => + have hr2 : raw &&& 3#u64 = 2#u64 := UScalar.eq_of_val_eq (by rw [and3_valA]; exact htag) + have ha := utActiveAsid_p1A R hact + obtain ⟨x, hx, hc⟩ := R.highOf ha hr2 + have hun : (raw &&& ~~~3#u64) = untagA raw := rfl + have h511 : wa (untagA raw) 4088#u64 = peAF (untagA raw) 511#u64 := rfl + obtain ⟨b0, b1, btab⟩ := E.wl0 _ hy + have hd : (L.mem s (wa (raw &&& ~~~3#u64) 4088#u64) &&& 3#u64) = 3#u64 := by + rw [hun, h511]; exact utAnd3A _ b0 b1 + have p := S1aPath.high (st := st) (n := n) ha hr2 hd + rw [utAddrTabA, hun, h511, btab] at p + exact ⟨asid, _, p, Or.inr (Or.inl ⟨rfl, rfl, rfl, x, hx, _, asid, hc, by rw [hun]⟩)⟩ + | @child tag t i sh _ _ h12 hi h3 ih => + obtain ⟨asid, va, path, inv⟩ := ih + have ev := wm8_valA hi + have eo : (core.num.U64.wrapping_mul i 8#u64).val / 8 = i.val := by omega + have e8 : (core.num.U64.wrapping_mul i 8#u64).val % 8 = 0 := by omega + have epe : wa t (core.num.U64.wrapping_mul i 8#u64) = peAF t i := rfl + have step : s1aTableWidth tag n sh > (core.num.U64.wrapping_mul i 8#u64).val ∧ + s1aUserRange tag n (va + i.val * 2 ^ sh) (2 ^ sh) ∧ sh - 9 < s1aTopShift tag n ∧ 9 ≤ sh := by + rcases inv with ⟨rfl, rfl, rfl, x, hx, a, hc⟩ | ⟨rfl, rfl, rfl, x, hx, r, a, hc, rfl⟩ | ⟨hlt, hr⟩ + · -- a native root: slots 256..511 are zero + have hi256 : i.val < 256 := by + by_contra hge + have z := R.rootSlots.native x hx _ _ _ hc (core.num.U64.wrapping_mul i 8#u64) (by omega) e8 + rw [epe] at z + exact utZeroNotTabA h3 z + refine ⟨?_, ?_, by rw [t0]; omega, by omega⟩ + · simp only [s1aTableWidth, t0, h01, ↓reduceIte]; omega + · simp only [s1aUserRange, ↓reduceIte, hU]; omega + · -- a high window's L1: slots outside 384..495 are zero + have hwin : 384 ≤ i.val ∧ i.val < 496 := by + by_contra hge + have z := R.rootSlots.highPrivate x hx _ _ _ hc (core.num.U64.wrapping_mul i 8#u64) + ⟨by omega, by omega⟩ e8 + rw [epe] at z + exact utZeroNotTabA h3 z + refine ⟨?_, ?_, by rw [t2]; omega, by omega⟩ + · simp only [s1aTableWidth, t2, h21, h20, ↓reduceIte]; omega + · simp only [s1aUserRange, h20, ↓reduceIte, hB, hT]; omega + · -- below the top: every slot, inside the parent's range + have hne : sh ≠ s1aTopShift tag n := by omega + refine ⟨?_, ?_, by omega, by omega⟩ + · simp only [s1aTableWidth, hne, ↓reduceIte]; omega + · have hsz : (i.val + 1) * 2 ^ sh ≤ 2 ^ (sh + 9) := by + rw [Nat.pow_add, Nat.mul_comm (2 ^ sh)] + exact Nat.mul_le_mul_right _ (by omega) + rw [Nat.add_mul, Nat.one_mul] at hsz + unfold s1aUserRange at hr ⊢ + split_ifs at hr ⊢ <;> omega + obtain ⟨hw, hu, hlt, h9⟩ := step + have p := S1aPath.child path h12 hw e8 (by rw [eo]; exact hu) (by rw [epe]; exact h3) + rw [utAddrTabA, epe, eo] at p + exact ⟨asid, _, p, Or.inr (Or.inr ⟨hlt, by rw [show sh - 9 + 9 = sh by omega]; exact hu⟩)⟩ + +/-- The invariant of a Stage-2 walk. -/ +def UtS2InvA (st : machine.Store S) [L : LawfulStore st] (Pp' X T2 : Set U64) (Sh2 : U64 → Nat) (n : U64) (s : S) + (q : U64) : Prop := + ∃ asid sh va, S1aPath st Pp' n s 1#u64 asid sh va q ∧ + ((q ∈ X ∧ sh = s1aTopShift 1#u64 n ∧ va = 0) ∨ + (q ∈ T2 ∧ sh = Sh2 q ∧ sh < s1aTopShift 1#u64 n ∧ s1aUserRange 1#u64 n va (2 ^ (sh + 9)))) + +/-- An active Stage-2 root of the spec is a Stage-2 entry of R2. -/ +theorem utS2Root_pathA (R : S1aR2 st H OS Pt Pp' Tb' Hr Te Kt Lv' Re bits n s) + (V : VsRelA Pp Tb Lv X Y (L.mem s) (L.ksb s) σ) (E : ExtVsRelA Pp Tb X Y T2 Sh2 (L.mem s) σ) {r : U64} + (h : ActiveS2RootA Pp X σ r) : + ∃ asid, S1aPath st Pp' n s 1#u64 asid (s1aTopShift 1#u64 n) 0 r := by + obtain ⟨hrX, asid, p, f, -, hp, htab, hk, hf⟩ := h + have hv : asid.toNat < 65536 := by + have := asid.isLt + simpa [Fl4SpecA64.asidRepLen, Fl4SpecA64.asidLen] using this + have ea : asid = BitVec.ofNat 16 asid.toNat := by simp + have hhiv : (⟨BitVec.ofNat 64 (asid.toNat / 512)⟩ : U64).val = asid.toNat / 512 := by + show (BitVec.ofNat 64 _).toNat = _ + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + have hlov : (⟨BitVec.ofNat 64 (asid.toNat % 512)⟩ : U64).val = asid.toNat % 512 := by + show (BitVec.ofNat 64 _).toNat = _ + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + obtain ⟨hi, hhi⟩ : ∃ hi : U64, hi.val = asid.toNat / 512 := ⟨_, hhiv⟩ + obtain ⟨lo, hlo⟩ : ∃ lo : U64, lo.val = asid.toNat % 512 := ⟨_, hlov⟩ + have hi128 : hi.val < 128 := by omega + have lo512 : lo.val < 512 := by omega + have tabS := V.table hi hi128 + rw [ea, asidHi_eqA hv, ← hhi] at htab + rw [htab] at tabS + have hmne : L.mem s (atAF (L.ksb s) hi) ≠ 0#u64 := fun e => by + rw [e, optPtrA_zero] at tabS; cases tabS + rw [optPtrA_ne hmne] at tabS + have hpm : L.mem s (atAF (L.ksb s) hi) = p := (specPtr_inj (Option.some.inj tabS)).symm + obtain ⟨f', hk', hfe⟩ := V.pool p hp + rw [hk] at hk' + cases hk' + have hpne : p ≠ 0#u64 := hpm ▸ hmne + have hent := hfe ⟨hpne, hi, hi128, hpm⟩ lo lo512 + rw [ea, asidLo_eqA hv, ← hlo] at hf + rw [hf] at hent + obtain ⟨raw, hraw⟩ : ∃ raw, raw = L.mem s (peAF p lo) := ⟨_, rfl⟩ + rw [← hraw] at hent + have hrne : raw ≠ 0#u64 := fun e => by rw [e, poolEntA_zero] at hent; cases hent + have hun : untagA raw = r := by + unfold poolEntA at hent + rw [ite_eq_right hrne] at hent + injection hent with h1 + injection h1 with h2 + exact (specPtr_inj h2).symm + have kind := V.kind p hp ⟨hpne, hi, hi128, hpm⟩ lo lo512 + rw [← hraw] at kind + obtain ⟨es, hes, -⟩ := E.s2root r hrX + have htag : tagA raw = 1 := by + rcases kind with h0 | ⟨-, htb, -⟩ | ⟨h1, -⟩ | ⟨-, hy⟩ + · exact absurd h0 hrne + · rw [hun] at htb + obtain ⟨t, ht, -⟩ := V.pt r htb + rw [hes] at ht; cases ht + · exact h1 + · rw [hun] at hy + obtain ⟨l1, hl, -⟩ := V.high r hy + rw [hes] at hl; cases hl + have hr1 : raw &&& 3#u64 = 1#u64 := UScalar.eq_of_val_eq (by rw [and3_valA]; exact htag) + have hA : ActiveAsidRootA (L.mem s) (L.ksb s) Pp (⟨BitVec.ofNat 64 asid.toNat⟩ : U64) raw := by + have hav : (⟨BitVec.ofNat 64 asid.toNat⟩ : U64).val = asid.toNat := by + show (BitVec.ofNat 64 _).toNat = _ + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + exact ⟨hi, lo, hi128, lo512, by rw [hav, hhi, hlo]; omega, p, hp, hpne, hpm, hraw.symm, hrne⟩ + have path := S1aPath.stage2 (st := st) (n := n) (utActiveAsid_p1A R hA) hr1 + rw [show (raw &&& ~~~3#u64) = untagA raw from rfl, hun] at path + exact ⟨_, path⟩ + +/-- A table descriptor's relation names its target at the next shift. -/ +theorem utDescTabA {G : U64 → Nat → Prop} {sh : Nat} {w : U64} {pte : Fl4SpecA64.PTE} (h : DescRelE G sh w pte) + (b0 : dBitA w 0 = true) (b1 : dBitA w 1 = true) (h12 : 12 < sh) : G (dTabA w) (sh - 9) := by + cases pte with + | invalidPTE => + have : dBitA w 0 = false := h + rw [b0] at this; cases this + | pageTablePTE _ => exact h.2.2.2.2.2 + | pagePTE _ _ _ _ => + have := h.2.1.1 b1 + omega + +/-- **Stage-2 walks** (module docstring). -/ +theorem utS1aPath_of_s2ReachA (R : S1aR2 st H OS Pt Pp' Tb' Hr Te Kt Lv' Re bits n s) + (V : VsRelA Pp Tb Lv X Y (L.mem s) (L.ksb s) σ) (E : ExtVsRelA Pp Tb X Y T2 Sh2 (L.mem s) σ) + (hn : n.val = Fl4SpecA64.fl4A64Platform.s2IpaBits) {q : U64} + (h : S2ReachA Pp X T2 Sh2 (L.mem s) σ q) : UtS2InvA st Pp' X T2 Sh2 n s q := by + have h10 : (1#u64 : U64) ≠ 0#u64 := by decide + have h12' : (1#u64 : U64) ≠ 2#u64 := by decide + have top : s1aTopShift 1#u64 n = Fl4SpecA64.s2Geom.topShift ∧ + (∀ i : Nat, i < Fl4SpecA64.s2Geom.topMask + 1 → + i * 8 < s1aTableWidth 1#u64 n (s1aTopShift 1#u64 n) ∧ + (i + 1) * 2 ^ s1aTopShift 1#u64 n ≤ 2 ^ s1aIpaWidth n) ∧ 12 < s1aTopShift 1#u64 n := by + unfold Fl4SpecA64.s2Geom + rw [← hn] + unfold Fl4SpecA64.s2GeomOf + refine ⟨?_, fun i hi => ?_, ?_⟩ + · simp only [s1aTopShift, h10, h12', ↓reduceIte] + split_ifs <;> rfl + · simp only [s1aTableWidth, s1aTopShift, s1aIpaWidth, h10, h12', ↓reduceIte] at hi ⊢ + split_ifs at hi ⊢ <;> dsimp only at hi ⊢ <;> + (try simp only [Nat.reduceSub, Nat.reducePow, Nat.reduceMul] at hi ⊢) <;> omega + · simp only [s1aTopShift, h10, h12', ↓reduceIte] + split_ifs <;> omega + obtain ⟨etop, hslot, h12top⟩ := top + have child : ∀ {t i : U64} {asid : U64} {sh va : Nat}, S1aPath st Pp' n s 1#u64 asid sh va t → 12 < sh → + i.val < 1024 → i.val * 8 < s1aTableWidth 1#u64 n sh → + s1aUserRange 1#u64 n (va + i.val * 2 ^ sh) (2 ^ sh) → (L.mem s (peAF t i) &&& 3#u64) = 3#u64 → + S1aPath st Pp' n s 1#u64 asid (sh - 9) (va + i.val * 2 ^ sh) (dTabA (L.mem s (peAF t i))) := by + intro t i asid sh va path h12 hi hw hu h3 + have ev : (core.num.U64.wrapping_mul i 8#u64).val = 8 * i.val := by + simp [core.num.U64.wrapping_mul_val_eq, U64.size_def, U64.numBits] + omega + have eo : (core.num.U64.wrapping_mul i 8#u64).val / 8 = i.val := by omega + have epe : wa t (core.num.U64.wrapping_mul i 8#u64) = peAF t i := rfl + have p := S1aPath.child path h12 (show (core.num.U64.wrapping_mul i 8#u64).val < s1aTableWidth 1#u64 n sh by rw [ev]; omega) (by omega) + (by rw [eo]; exact hu) (by rw [epe]; exact h3) + rwa [utAddrTabA, epe, eo] at p + have rootStep : ∀ {r i : U64}, ActiveS2RootA Pp X σ r → i.val < Fl4SpecA64.s2Geom.topMask + 1 → + (L.mem s (peAF r i) &&& 3#u64) = 3#u64 → dTabA (L.mem s (peAF r i)) ∈ T2 → + UtS2InvA st Pp' X T2 Sh2 n s (dTabA (L.mem s (peAF r i))) := by + intro r i ha hi h3 hm + obtain ⟨asid, path⟩ := utS2Root_pathA R V E ha + obtain ⟨hw, hrange⟩ := hslot i.val hi + have hi1024 : i.val < 1024 := by + have : Fl4SpecA64.s2Geom.topMask + 1 ≤ 1024 := by + unfold Fl4SpecA64.s2Geom Fl4SpecA64.s2GeomOf; split_ifs <;> decide + omega + have hu : s1aUserRange 1#u64 n (0 + i.val * 2 ^ s1aTopShift 1#u64 n) (2 ^ s1aTopShift 1#u64 n) := by + unfold s1aUserRange + rw [ite_eq_right h10, ite_eq_right h12', Nat.zero_add] + rw [Nat.add_mul, Nat.one_mul] at hrange + exact hrange + have p := child path h12top hi1024 hw hu h3 + obtain ⟨es, -, he⟩ := E.s2root r ha.1 + obtain ⟨b0, b1⟩ : dBitA (L.mem s (peAF r i)) 0 = true ∧ dBitA (L.mem s (peAF r i)) 1 = true := by + have := congrArg UScalar.val h3 + rw [and3_valA] at this + have v3 : (3#u64 : U64).val = 3 := rfl + constructor <;> (show (L.mem s (peAF r i)).bv.getLsbD _ = true) <;> + rw [BitVec.getLsbD, Nat.testBit_eq_decide_div_mod_eq] <;> simp <;> + (have e : (L.mem s (peAF r i)).bv.toNat = (L.mem s (peAF r i)).val := rfl) <;> omega + obtain ⟨d, -⟩ := he ha i hi1024 hi + have g := utDescTabA d b0 b1 (by omega) + refine ⟨asid, _, _, p, Or.inr ⟨hm, by rw [etop]; exact g.2.symm, by omega, ?_⟩⟩ + rw [show s1aTopShift 1#u64 n - 9 + 9 = s1aTopShift 1#u64 n by omega] + exact hu + induction h with + | @root r ha => + obtain ⟨asid, path⟩ := utS2Root_pathA R V E ha + exact ⟨asid, _, 0, path, Or.inl ⟨ha.1, rfl, rfl⟩⟩ + | @stepRoot r i ha hi h3 hm => exact rootStep ha hi h3 hm + | @stepPt t i hreach ht h12 hi h3 hm ih => + obtain ⟨asid, sh, va, path, inv⟩ := ih + -- `t` is a table of `T2`; a node that is a root is no table + have tab : sh = Sh2 t ∧ sh < s1aTopShift 1#u64 n ∧ s1aUserRange 1#u64 n va (2 ^ (sh + 9)) := by + rcases inv with ⟨hX, -, -⟩ | ⟨-, a, b, c⟩ + · exact absurd rfl (E.t2x t ht t hX).1 + · exact ⟨a, b, c⟩ + obtain ⟨esh, hlt, hr⟩ := tab + have hne : sh ≠ s1aTopShift 1#u64 n := by omega + have h12s : 12 < sh := by omega + have hw : i.val * 8 < s1aTableWidth 1#u64 n sh := by + simp only [s1aTableWidth, hne, ↓reduceIte]; omega + have hu : s1aUserRange 1#u64 n (va + i.val * 2 ^ sh) (2 ^ sh) := by + have hsz : (i.val + 1) * 2 ^ sh ≤ 2 ^ (sh + 9) := by + rw [Nat.pow_add, Nat.mul_comm (2 ^ sh)] + exact Nat.mul_le_mul_right _ (by omega) + rw [Nat.add_mul, Nat.one_mul] at hsz + unfold s1aUserRange at hr ⊢ + rw [ite_eq_right h10, ite_eq_right h12'] at hr ⊢ + omega + have p := child path h12s (by omega) hw hu h3 + obtain ⟨pt, -, -, he⟩ := E.s2pt t ht + obtain ⟨b0, b1⟩ : dBitA (L.mem s (peAF t i)) 0 = true ∧ dBitA (L.mem s (peAF t i)) 1 = true := by + have := congrArg UScalar.val h3 + rw [and3_valA] at this + have v3 : (3#u64 : U64).val = 3 := rfl + constructor <;> (show (L.mem s (peAF t i)).bv.getLsbD _ = true) <;> + rw [BitVec.getLsbD, Nat.testBit_eq_decide_div_mod_eq] <;> simp <;> + (have e : (L.mem s (peAF t i)).bv.toNat = (L.mem s (peAF t i)).val := rfl) <;> omega + obtain ⟨d, -⟩ := he hreach i hi + have g := utDescTabA d b0 b1 (by omega) + refine ⟨asid, _, _, p, Or.inr ⟨hm, by rw [esh]; exact g.2.symm, by omega, ?_⟩⟩ + rw [show sh - 9 + 9 = sh by omega] + exact hu + +/-- **`UtVsClosA` at an R2 state** (module docstring). `named`: no table or root that a cap of `OS` names is +removed. -/ +theorem utVsClos_of_r2A (R : S1aR2 st H OS Pt Pp' Tb' Hr Te Kt Lv' Re bits n s) + (V : VsRelA Pp Tb Lv X Y (L.mem s) (L.ksb s) σ) (E : ExtVsRelA Pp Tb X Y T2 Sh2 (L.mem s) σ) + (hn : n.val = Fl4SpecA64.fl4A64Platform.s2IpaBits) {D : Set Fl4SpecA64.ObjRef} + (named : ∀ x ∈ OS, ∀ t, + (∃ b, capA st s x = .AsidPool t b) ∨ (∃ m a v, capA st s x = .PageTable t m a v) ∨ + (∃ m a, capA st s x = .Pml4 t m a) ∨ (∃ l m a v, capA st s x = .Ept t l m a v) ∨ + (∃ m a, capA st s x = .HighVSpace t m a) → specPtr t ∉ D) : + UtVsClosA D Pp Tb Y X T2 Sh2 (L.mem s) (L.ksb s) σ := by + have rootNamed : ∀ asid raw, ActiveAsidRoot st Pp' s asid raw → specPtr (untagA raw) ∉ D := by + intro asid raw h + obtain ⟨x, hx, c⟩ := R.core.root.backed asid raw h + have hun : (raw &&& ~~~3#u64) = untagA raw := rfl + rw [hun] at c + refine named x hx _ ?_ + rcases c with ⟨-, c⟩ | ⟨-, va, c⟩ | ⟨-, c⟩ + · exact Or.inr (Or.inr (Or.inl ⟨_, _, c⟩)) + · exact Or.inr (Or.inr (Or.inr (Or.inl ⟨_, _, _, _, c⟩))) + · exact Or.inr (Or.inr (Or.inr (Or.inr ⟨_, _, c⟩))) + have tabNamed : ∀ tag asid sh va t, S1aPath st Pp' n s tag asid sh va t → sh < s1aTopShift tag n → + specPtr t ∉ D := by + intro tag asid sh va t p hlt + obtain ⟨x, hx, va', c, -⟩ := R.paths.children tag asid sh va t p hlt + exact named x hx t (Or.inr (Or.inl ⟨_, _, _, c⟩)) + refine + { tpool := fun hi hhi hne => ?_ + ent := fun p _ _ ha lo hlo hne => ?_ + tab := fun tag sh q _ path h12 hnode i hi b0 b1 hm => ?_ + s2root := fun r ha _ i hi hitop b0 b1 hm => ?_ + s2pt := fun t hreach ht h12 i hi b0 b1 hm => ?_ } + · have hat : wa (L.ksb s) (AtOff hi) = atAF (L.ksb s) hi := by + unfold AtOff atAF + have h127 : (hi &&& 127#u64) = hi := UScalar.eq_of_val_eq (by + rw [and_maskA hi 7 _ rfl]; exact Nat.mod_eq_of_lt (by omega)) + rw [h127] + obtain ⟨-, -, -, x, hx, base, c, -⟩ := + R.poolCaps.present hi hhi _ (show L.mem s (wa (L.ksb s) (AtOff hi)) = L.mem s (atAF (L.ksb s) hi) by + rw [hat]) hne + exact named x hx _ (Or.inl ⟨_, c⟩) + · obtain ⟨hpne, hi, hhi, htab⟩ := ha + have hlov := hlo + have hA : ActiveAsidRootA (L.mem s) (L.ksb s) {p} + (⟨BitVec.ofNat 64 (hi.val * 512 + lo.val)⟩ : U64) (L.mem s (peAF p lo)) := by + have hav : (⟨BitVec.ofNat 64 (hi.val * 512 + lo.val)⟩ : U64).val = hi.val * 512 + lo.val := by + show (BitVec.ofNat 64 _).toNat = _ + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + exact ⟨hi, lo, hhi, hlo, hav, p, rfl, hpne, htab, rfl, hne⟩ + exact rootNamed _ _ (utActiveAsid_p1A R hA) + · obtain ⟨asid, va, p, inv⟩ := utS1aPath_of_vsPathA R E path + have hch : VsPathA Pp Tb Y (L.mem s) (L.ksb s) tag (sh - 9) (dTabA (L.mem s (peAF q i))) := by + have h3 := utAnd3A _ b0 b1 + exact .child path hnode h12 hi h3 + obtain ⟨asid', va', p', inv'⟩ := utS1aPath_of_vsPathA R E hch + rcases inv' with ⟨-, h39, -⟩ | ⟨ht2, h30, -⟩ | ⟨hlt, -⟩ + · rcases inv with ⟨-, h, -⟩ | ⟨-, h, -⟩ | ⟨hlt, -⟩ + · omega + · omega + · have : s1aTopShift tag n ≤ 39 := by unfold s1aTopShift; split_ifs <;> omega + omega + · rcases inv with ⟨ht0, h, -⟩ | ⟨-, h, -⟩ | ⟨hlt, -⟩ + · rw [ht0] at ht2; exact absurd ht2 (by decide) + · omega + · have : s1aTopShift tag n = 30 := by + rw [ht2]; unfold s1aTopShift + rw [ite_eq_right (by decide), ite_eq_left rfl] + omega + · exact tabNamed _ _ _ _ _ p' hlt + · have h3 := utAnd3A _ b0 b1 + have hch : S2ReachA Pp X T2 Sh2 (L.mem s) σ (dTabA (L.mem s (peAF r i))) := .stepRoot ha hitop h3 hm + obtain ⟨asid, sh, va, p, inv⟩ := utS1aPath_of_s2ReachA R V E hn hch + rcases inv with ⟨hX, -, -⟩ | ⟨-, -, hlt, -⟩ + · exact absurd rfl (E.t2x _ hm _ hX).1 + · exact tabNamed _ _ _ _ _ p hlt + · have h3 := utAnd3A _ b0 b1 + have hch : S2ReachA Pp X T2 Sh2 (L.mem s) σ (dTabA (L.mem s (peAF t i))) := .stepPt hreach ht h12 hi h3 hm + obtain ⟨asid, sh, va, p, inv⟩ := utS1aPath_of_s2ReachA R V E hn hch + rcases inv with ⟨hX, -, -⟩ | ⟨-, -, hlt, -⟩ + · exact absurd rfl (E.t2x _ hm _ hX).1 + · exact tabNamed _ _ _ _ _ p hlt + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtVsGrowA.lean b/proofs/Fl4ProofsA64/P3/UtVsGrowA.lean new file mode 100644 index 000000000..fcaba12ab --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtVsGrowA.lean @@ -0,0 +1,353 @@ +/- +P3 (aarch64, A64-P3 Untyped W4/W5, the VSpace splits of Retype): **`VsRelA`, `ExtVsRelA` and `SetsInHA` when new, +unreferenced objects join the sets**. + +A retyped table or root is named by no pool entry and no table entry, so every walk of the grown relation is a walk of +the old one (`utVsPath_oldA`, `utS2Reach_oldA`): the old objects keep their clauses, and a new object needs only its +spec object, its alignment and, for a high window, its fixed level-0 words. + +* `UtVsGrowA`: what the step keeps (the relation's words, the old spec objects, `armAsidTable`) and what the new + members bring. +* `utVsRel_growA`, `utExtVs_growA`, `utSetsInH_growA`. + +No named premise: hypotheses only. New definitions and lemmas. +-/ +import Fl4ProofsA64.P3.VsRelA +import Fl4ProofsA64.P3.ExtVsDefA +import Fl4ProofsA64.P3.Stage3A + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4SpecA64 Classical + +local notation "wa" => core.num.U64.wrapping_add + +/-- **A growth of the VSpace sets by unreferenced objects** (module docstring). -/ +structure UtVsGrowA (Pp Tb Tb' X X' Y Y' T2 : Set U64) (Lv Lv' : U64 → Nat) (mem mem' : U64 → U64) (ksb : U64) + (σ σ' : Fl4SpecA64.State Unit) : Prop where + tb : Tb ⊆ Tb' + xs : X ⊆ X' + ys : Y ⊆ Y' + lv : ∀ p ∈ Tb, Lv' p = Lv p + kmem : ∀ w, VsFootA Pp Tb Y ksb w → mem' w = mem w + kext : ∀ w, ExtFootE X Y T2 w → mem' w = mem w + ktab : σ'.archState.armAsidTable = σ.archState.armAsidTable + kheap : ∀ q, q ∈ Pp ∨ q ∈ Tb ∨ q ∈ Y ∨ q ∈ X ∨ q ∈ T2 → σ'.kheap (specPtr q) = σ.kheap (specPtr q) + newT : ∀ p ∈ Tb', p ∉ Tb → (∀ y ∈ Y, p ≠ wa y 4096#u64) ∧ Lv' p ≤ 3 ∧ 4096 ∣ p.val ∧ + ∃ t, σ'.kheap (specPtr p) = some (.archObj (.pageTable t)) ∧ Fl4SpecA64.ptType t = ptTyA (Lv' p) + newY : ∀ y ∈ Y', y ∉ Y → 8192 ∣ y.val ∧ (∃ l1, σ'.kheap (specPtr y) = some (.archObj (.ext (.highVSpace l1)))) ∧ + (dBitA (mem' (peAF y 511#u64)) 0 = true ∧ dBitA (mem' (peAF y 511#u64)) 1 = true ∧ + dTabA (mem' (peAF y 511#u64)) = wa y 4096#u64) ∧ + ∀ i : U64, i.val < 256 → mem' (peAF y i) = 0#u64 + newX : ∀ r ∈ X', r ∉ X → 8192 ∣ r.val ∧ ∃ es, σ'.kheap (specPtr r) = some (.archObj (.ext (.stage2Root es))) + ytb : ∀ y ∈ Y', wa y 4096#u64 ∉ Tb' + pgd : ∀ q ∈ S2PgE X' T2, q ∉ VsPtA Pp Tb' Y' ∧ q ∉ Y' + t2x : ∀ t ∈ T2, ∀ r ∈ X', t ≠ r ∧ t ≠ wa r 4096#u64 + ypg : ∀ y ∈ Y', y ∉ VsPtA Pp Tb' Y' + ypp : ∀ y ∈ Y', wa y 4096#u64 ∉ Pp + +section +variable {Pp Tb Tb' X X' Y Y' T2 : Set U64} {Lv Lv' : U64 → Nat} {Sh2 : U64 → Nat} {mem mem' : U64 → U64} + {ksb : U64} {σ σ' : Fl4SpecA64.State Unit} + +private theorem nat_and_hiA_ut (n k : Nat) (hn : n < 2 ^ 64) (hk : k ≤ 64) : + n &&& (2 ^ 64 - 1 - (2 ^ k - 1)) = 2 ^ k * (n / 2 ^ k) := by + have h1 : 1 ≤ 2 ^ k := Nat.one_le_two_pow + have h2 : 2 ^ k * 2 ^ (64 - k) = 2 ^ 64 := by rw [← Nat.pow_add, Nat.add_sub_cancel' hk] + have e : (2 ^ 64 - 1 - (2 ^ k - 1) : Nat) = 2 ^ k * (2 ^ (64 - k) - 1) := by + rw [Nat.mul_sub_one, h2]; omega + apply Nat.eq_of_testBit_eq + intro i + rw [Nat.testBit_and, e, Nat.testBit_two_pow_mul, Nat.testBit_two_pow_mul, Nat.testBit_two_pow_sub_one, + Nat.testBit_div_two_pow] + by_cases h : k ≤ i + · have e2 : i - k + k = i := by omega + rw [e2] + by_cases hlt : i < 64 + · simp [h, show i - k < 64 - k by omega] + · have e3 : n.testBit i = false := + Nat.testBit_lt_two_pow (lt_of_lt_of_le hn (Nat.pow_le_pow_right (by decide) (by omega))) + simp [h, e3] + · simp [h] + +private theorem utUntag_idA {w : U64} (h : tagA w = 0) : untagA w = w := by + apply UScalar.eq_of_val_eq + unfold untagA + rw [UScalar.val_and, show (18446744073709551612#u64 : U64).val = 2 ^ 64 - 1 - (2 ^ 2 - 1) from rfl, + nat_and_hiA_ut w.val 2 w.bv.isLt (by decide)] + unfold tagA at h + have e : (2 : Nat) ^ 2 = 4 := rfl + rw [e]; omega + +theorem UtVsGrowA.asid (G : UtVsGrowA Pp Tb Tb' X X' Y Y' T2 Lv Lv' mem mem' ksb σ σ') {hi : U64} + (h : hi.val < 128) : mem' (atAF ksb hi) = mem (atAF ksb hi) := G.kmem _ (.asid hi h) + +theorem UtVsGrowA.active (G : UtVsGrowA Pp Tb Tb' X X' Y Y' T2 Lv Lv' mem mem' ksb σ σ') {p : U64} + (h : ActivePoolA mem' ksb p) : ActivePoolA mem ksb p := by + obtain ⟨h0, hi, h1, h2⟩ := h + exact ⟨h0, hi, h1, (G.asid h1).symm.trans h2⟩ + +theorem UtVsGrowA.root (G : UtVsGrowA Pp Tb Tb' X X' Y Y' T2 Lv Lv' mem mem' ksb σ σ') {asid raw : U64} + (h : ActiveAsidRootA mem' ksb Pp asid raw) : ActiveAsidRootA mem ksb Pp asid raw := by + obtain ⟨hi, lo, h1, h2, h3, pool, hp, h0, h4, h5, h6⟩ := h + exact ⟨hi, lo, h1, h2, h3, pool, hp, h0, (G.asid h1).symm.trans h4, (G.kmem _ (.pool pool lo hp h2)).symm.trans h5, h6⟩ + +theorem UtVsGrowA.high (G : UtVsGrowA Pp Tb Tb' X X' Y Y' T2 Lv Lv' mem mem' ksb σ σ') {y : U64} + (h : ActiveHighRootA mem' ksb Pp y) : ActiveHighRootA mem ksb Pp y := by + obtain ⟨asid, raw, ha, ht, hu⟩ := h + exact ⟨asid, raw, G.root ha, ht, hu⟩ + +/-- **Every Stage-1 / high-window walk of the grown sets is an old one.** -/ +theorem utVsPath_oldA (V : VsRelA Pp Tb Lv X Y mem ksb σ) + (G : UtVsGrowA Pp Tb Tb' X X' Y Y' T2 Lv Lv' mem mem' ksb σ σ') {tag : U64} {sh : Nat} {q : U64} + (h : VsPathA Pp Tb' Y' mem' ksb tag sh q) : VsPathA Pp Tb Y mem ksb tag sh q := by + -- the kind of an active pool's word + have kind : ∀ asid raw, ActiveAsidRootA mem ksb Pp asid raw → raw ≠ 0#u64 ∧ EntKindA Tb Lv X Y raw := by + intro asid raw ha + obtain ⟨pool, hp, hact, lo, hlo, hw, hne⟩ := ha.activePool + have hk := V.kind pool hp hact lo hlo + rw [hw] at hk + exact ⟨hne, hk⟩ + induction h with + | @native asid raw hact htag _ => + have ha := G.root hact + obtain ⟨hne, hk⟩ := kind _ _ ha + rcases hk with h | ⟨-, h, -⟩ | ⟨h, -⟩ | ⟨h, -⟩ + · exact absurd h hne + · rw [utUntag_idA htag] at h + exact .native ha htag h + · omega + · omega + | @high asid raw hact htag _ => + have ha := G.root hact + obtain ⟨hne, hk⟩ := kind _ _ ha + rcases hk with h | ⟨h, -⟩ | ⟨h, -⟩ | ⟨-, h⟩ + · exact absurd h hne + · omega + · omega + · exact .high ha htag h + | @child tag t i sh _ _ h12 hi h3 ih => + obtain ⟨-, hc⟩ := V.path_props ih + rcases hc with ⟨ht, -, -⟩ | ⟨-, -, y, hy, -, e⟩ + · have em : mem' (peAF t i) = mem (peAF t i) := G.kmem _ (.pt t i ht hi) + rw [em] at h3 ⊢ + exact .child ih (Or.inl ht) h12 hi h3 + · have em : mem' (peAF t i) = mem (peAF t i) := by rw [e]; exact G.kmem _ (.high y i hy hi) + rw [em] at h3 ⊢ + exact .child ih (Or.inr ⟨y, hy, e⟩) h12 hi h3 + +theorem utPteRel_monoA (tb : Tb ⊆ Tb') (lv : ∀ p ∈ Tb, Lv' p = Lv p) {k : Nat} {w : U64} {pte : Fl4SpecA64.PTE} + (h : PteRelA Tb Lv k w pte) : PteRelA Tb' Lv' k w pte := by + cases pte with + | invalidPTE => exact h + | pageTablePTE ppn => + obtain ⟨h1, h2, h3, h4, h5, h6, h7⟩ := h + exact ⟨h1, h2, h3, h4, h5, tb h6, (lv _ h6).trans h7⟩ + | pagePTE _ _ _ _ => exact h + +/-- **`VsRelA` over the grown sets** (module docstring). -/ +theorem utVsRel_growA (V : VsRelA Pp Tb Lv X Y mem ksb σ) + (G : UtVsGrowA Pp Tb Tb' X X' Y Y' T2 Lv Lv' mem mem' ksb σ σ') : VsRelA Pp Tb' Lv' X' Y' mem' ksb σ' := by + have reach : ∀ p, VsReachA Pp Tb' Y' mem' ksb p → VsReachA Pp Tb Y mem ksb p := fun p ⟨tag, sh, ht, hp⟩ => + ⟨tag, sh, ht, utVsPath_oldA V G hp⟩ + have highNew : ∀ y, y ∉ Y → ¬ ActiveHighRootA mem ksb Pp y := by + rintro y hy ⟨asid, raw, ha, htag, hu⟩ + obtain ⟨pool, hp, hact, lo, hlo, hw, hne⟩ := ha.activePool + have hk := V.kind pool hp hact lo hlo + rw [hw] at hk + rcases hk with h | ⟨h, -⟩ | ⟨h, -⟩ | ⟨-, h⟩ + · exact hne h + · omega + · omega + · exact hy (hu ▸ h) + refine + { table := fun hi h => by rw [G.ktab, G.asid h]; exact V.table hi h + tmem := fun hi h hne => by rw [G.asid h] at hne ⊢; exact V.tmem hi h hne + pool := fun p hp => ?_ + kind := fun p hp hact lo hlo => ?_ + asid0 := fun hne => ?_ + pt := fun p hp => ?_ + high := fun y hy => ?_ + lv := fun p hp => ?_ + al := fun p hp => ?_ + pal := V.pal + yal := fun y hy => ?_ + ytb := G.ytb } + · obtain ⟨f, hf, hr⟩ := V.pool p hp + refine ⟨f, (G.kheap p (Or.inl hp)).trans hf, fun hact lo hlo => ?_⟩ + rw [G.kmem _ (.pool p lo hp hlo)] + exact hr (G.active hact) lo hlo + · rw [G.kmem _ (.pool p lo hp hlo)] + rcases V.kind p hp (G.active hact) lo hlo with h | ⟨h1, h2, h3⟩ | ⟨h1, h2⟩ | ⟨h1, h2⟩ + · exact Or.inl h + · exact Or.inr (Or.inl ⟨h1, G.tb h2, (G.lv _ h2).trans h3⟩) + · exact Or.inr (Or.inr (Or.inl ⟨h1, G.xs h2⟩)) + · exact Or.inr (Or.inr (Or.inr ⟨h1, G.ys h2⟩)) + · have h0 : (0#u64 : U64).val < 128 := by simp + rw [G.asid h0] at hne ⊢ + have hp := V.tmem 0#u64 h0 hne + rw [G.kmem _ (.pool _ _ hp (by simp))] + exact V.asid0 hne + · by_cases h : p ∈ Tb + · obtain ⟨t, h1, h2, h3⟩ := V.pt p h + refine ⟨t, (G.kheap p (Or.inr (Or.inl h))).trans h1, by rw [G.lv p h]; exact h2, fun hr i hi => ?_⟩ + rw [G.kmem _ (.pt p i h hi), G.lv p h] + exact utPteRel_monoA G.tb G.lv (h3 (reach p hr) i hi) + · obtain ⟨hw, -, -, t, h1, h2⟩ := G.newT p hp h + refine ⟨t, h1, h2, fun hr => ?_⟩ + obtain ⟨tag, sh, -, hp'⟩ := reach p hr + obtain ⟨-, hc⟩ := V.path_props hp' + rcases hc with ⟨ht, -, -⟩ | ⟨-, -, y, hy, -, e⟩ + · exact absurd ht h + · exact absurd e (hw y hy) + · by_cases h : y ∈ Y + · obtain ⟨l1, h1, h2⟩ := V.high y h + refine ⟨l1, (G.kheap y (Or.inr (Or.inr (Or.inl h)))).trans h1, fun hact i hi => ?_⟩ + rw [G.kmem _ (.high y _ h hi)] + exact utPteRel_monoA G.tb G.lv (h2 (G.high hact) i hi) + · obtain ⟨-, ⟨l1, h1⟩, -, -⟩ := G.newY y hy h + exact ⟨l1, h1, fun hact => absurd (G.high hact) (highNew y h)⟩ + · by_cases h : p ∈ Tb + · rw [G.lv p h]; exact V.lv p h + · exact (G.newT p hp h).2.1 + · by_cases h : p ∈ Tb + · exact V.al p h + · exact (G.newT p hp h).2.2.1 + · by_cases h : y ∈ Y + · exact V.yal y h + · exact (G.newY y hy h).1 + +theorem utS2_topMaskA : Fl4SpecA64.s2Geom.topMask + 1 ≤ 1024 := by + unfold Fl4SpecA64.s2Geom Fl4SpecA64.s2GeomOf + split_ifs <;> decide + +/-- An installed Stage-2 root of the grown sets is an old one. -/ +theorem utS2Root_oldA (V : VsRelA Pp Tb Lv X Y mem ksb σ) + (G : UtVsGrowA Pp Tb Tb' X X' Y Y' T2 Lv Lv' mem mem' ksb σ σ') {r : U64} + (h : ActiveS2RootA Pp X' σ' r) : ActiveS2RootA Pp X σ r := by + obtain ⟨hr, asid, p, f, h0, hp, ht, hk, hf⟩ := h + rw [G.ktab] at ht + rw [G.kheap p (Or.inl hp)] at hk + refine ⟨?_, asid, p, f, h0, hp, ht, hk, hf⟩ + -- the pool is installed in memory + have hhi : (Fl4SpecA64.asidHighBitsOf asid).toNat < 128 := (Fl4SpecA64.asidHighBitsOf asid).isLt + let hi : U64 := ⟨BitVec.ofNat 64 (Fl4SpecA64.asidHighBitsOf asid).toNat⟩ + have ehi : hi.val = (Fl4SpecA64.asidHighBitsOf asid).toNat := by + show (BitVec.ofNat 64 _).toNat = _ + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + have hT := V.table hi (by omega) + have e7 : BitVec.ofNat 7 hi.val = Fl4SpecA64.asidHighBitsOf asid := by + rw [ehi]; exact (BitVec.eq_of_toNat_eq (by rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt hhi)) + rw [e7, ht] at hT + unfold optPtrA at hT + split at hT + case isTrue => cases hT + case isFalse hne => + have em : mem (atAF ksb hi) = p := (specPtr_inj (Option.some.inj hT)).symm + have hact : ActivePoolA mem ksb p := ⟨em ▸ hne, hi, by omega, em⟩ + have hlo : (Fl4SpecA64.asidLowBitsOf asid).toNat < 512 := (Fl4SpecA64.asidLowBitsOf asid).isLt + let lo : U64 := ⟨BitVec.ofNat 64 (Fl4SpecA64.asidLowBitsOf asid).toNat⟩ + have elo : lo.val = (Fl4SpecA64.asidLowBitsOf asid).toNat := by + show (BitVec.ofNat 64 _).toNat = _ + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + have e9 : BitVec.ofNat 9 lo.val = Fl4SpecA64.asidLowBitsOf asid := by + rw [elo]; exact (BitVec.eq_of_toNat_eq (by rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt hlo)) + obtain ⟨f0, hf0, hrel⟩ := V.pool p hp + have ef : f0 = f := by + rw [hk] at hf0 + cases hf0 + rfl + have hw := hrel hact lo (by omega) + rw [e9, ef, hf] at hw + unfold poolEntA at hw + split at hw + case isTrue => cases hw + case isFalse hne' => + have eu : r = untagA (mem (peAF p lo)) := specPtr_inj (by + have := Option.some.inj hw + injection this) + have hnT : r ∉ Tb' ∧ r ∉ Y' := by + have := G.pgd r (Or.inr ⟨r, hr, Or.inl rfl⟩) + exact ⟨fun g => this.1 (Or.inr (Or.inl g)), this.2⟩ + rcases V.kind p hp hact lo (by omega) with g | ⟨-, g, -⟩ | ⟨-, g⟩ | ⟨-, g⟩ + · exact absurd g hne' + · exact absurd (G.tb (eu ▸ g)) hnT.1 + · exact eu ▸ g + · exact absurd (G.ys (eu ▸ g)) hnT.2 + +theorem utS2Reach_oldA (V : VsRelA Pp Tb Lv X Y mem ksb σ) (E : ExtVsRelA Pp Tb X Y T2 Sh2 mem σ) + (G : UtVsGrowA Pp Tb Tb' X X' Y Y' T2 Lv Lv' mem mem' ksb σ σ') {t : U64} + (h : S2ReachA Pp X' T2 Sh2 mem' σ' t) : S2ReachA Pp X T2 Sh2 mem σ t := by + induction h with + | root h => exact .root (utS2Root_oldA V G h) + | @stepRoot r i hr hi h3 hT => + have ha := utS2Root_oldA V G hr + have em : mem' (peAF r i) = mem (peAF r i) := + G.kext _ (rootFootE ha.1 (E.xal r ha.1) (by have := utS2_topMaskA; omega)) + rw [em] at h3 hT ⊢ + exact .stepRoot ha hi h3 hT + | @stepPt t i _ ht h12 hi h3 hT ih => + have em : mem' (peAF t i) = mem (peAF t i) := G.kext _ (.s2pt t i ht hi) + rw [em] at h3 hT ⊢ + exact .stepPt ih ht h12 hi h3 hT + +/-- **`ExtVsRelA` over the grown sets** (module docstring). -/ +theorem utExtVs_growA (V : VsRelA Pp Tb Lv X Y mem ksb σ) (E : ExtVsRelA Pp Tb X Y T2 Sh2 mem σ) + (G : UtVsGrowA Pp Tb Tb' X X' Y Y' T2 Lv Lv' mem mem' ksb σ σ') : + ExtVsRelA Pp Tb' X' Y' T2 Sh2 mem' σ' := by + refine + { s2root := fun r hr => ?_ + s2pt := fun t ht => ?_ + tsh := E.tsh + wl0 := fun y hy => ?_ + wlo := fun y hy i hi => ?_ + xal := fun r hr => ?_ + tal := E.tal + pgd := G.pgd + t2x := G.t2x + ypg := G.ypg + ypp := G.ypp } + · by_cases h : r ∈ X + · obtain ⟨es, h1, h2⟩ := E.s2root r h + refine ⟨es, (G.kheap r (Or.inr (Or.inr (Or.inr (Or.inl h))))).trans h1, fun hact i hi htop => ?_⟩ + rw [G.kext _ (rootFootE h (E.xal r h) hi)] + exact h2 (utS2Root_oldA V G hact) i hi htop + · obtain ⟨-, es, h1⟩ := G.newX r hr h + exact ⟨es, h1, fun hact => absurd (utS2Root_oldA V G hact).1 h⟩ + · obtain ⟨pt, h1, h2, h3⟩ := E.s2pt t ht + refine ⟨pt, (G.kheap t (Or.inr (Or.inr (Or.inr (Or.inr ht))))).trans h1, h2, fun hr i hi => ?_⟩ + rw [G.kext _ (.s2pt t i ht hi)] + exact h3 (utS2Reach_oldA V E G hr) i hi + · by_cases h : y ∈ Y + · rw [G.kext _ (.wl0 y h)]; exact E.wl0 y h + · exact (G.newY y hy h).2.2.1 + · by_cases h : y ∈ Y + · rw [G.kext _ (.wlo y i h hi)]; exact E.wlo y h i hi + · exact (G.newY y hy h).2.2.2 i hi + · by_cases h : r ∈ X + · exact E.xal r h + · exact (G.newX r hr h).1 + +end + +/-- **`SetsInHA` over a grown heap and grown sets**: the old members keep their containing objects, the new members +bring theirs. -/ +theorem utSetsInH_growA {Z Z' : XSetsA} {H H' : Heap} (h : SetsInHA Z H) + (keep : ∀ q z, H q = some (.other z) → H' q = some (.other z)) + (pages : ∀ p ∈ Z'.Pp ∪ Z'.S1 ∪ Z'.T2 ∪ Z'.V, p ∈ Z.Pp ∪ Z.S1 ∪ Z.T2 ∪ Z.V ∨ + ∃ q z, H' q = some (.other z) ∧ q.val ≤ p.val ∧ p.val + 4096 ≤ q.val + 2 ^ z) + (roots : ∀ p ∈ Z'.X ∪ Z'.Y, p ∈ Z.X ∪ Z.Y ∨ + ∃ q z, H' q = some (.other z) ∧ q.val ≤ p.val ∧ p.val + 8192 ≤ q.val + 2 ^ z) : + SetsInHA Z' H' := by + refine ⟨fun p hp => ?_, fun p hp => ?_⟩ + · rcases pages p hp with g | g + · obtain ⟨q, z, h1, h2⟩ := h.pages p g + exact ⟨q, z, keep q z h1, h2⟩ + · exact g + · rcases roots p hp with g | g + · obtain ⟨q, z, h1, h2⟩ := h.roots p g + exact ⟨q, z, keep q z h1, h2⟩ + · exact g + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtVsObjA.lean b/proofs/Fl4ProofsA64/P3/UtVsObjA.lean new file mode 100644 index 000000000..6046f488f --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtVsObjA.lean @@ -0,0 +1,255 @@ +/- +P3 (aarch64, A64-P3 Untyped W4 leaf): **a retyped `ArmHighVSpace` from FL4's words**. On a zeroed 8 KiB object, +`objects::create_object` returns the unmapped `HighVSpace` cap, writes only the level-0 page, leaves level-0 entry `i` +(`i < 511`) the kernel's TTBR1 entry `i` and entry 511 a table descriptor of the level-1 page at `base + 4096`; the +level-1 page stays zero, so it matches the all-invalid entries of the spec's new `highVSpace`. + +Premises [P] (the backend's `kernel_ttbr1_entry` at the retype's state): it reads `k i` at `(0, i)` for `i < 511` +(`hK`), and `k i = 0` for `i < 256`, the kernel mapping nothing in the lower half of its TTBR1 table (`hK0`). +Hypotheses: both pages zero; `base` 4 KiB-aligned, in the kernel window, not wrapping. New lemmas only. +-/ +import Fl4ProofsA64.P3.UtObjA +import Fl4ProofsA64.P3.VsRelA +import Fl4ProofsA64.P1.MdbVUC +import Fl4ProofsA64.P1.QueueDecUt +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-- The level-0 table descriptor `init_high_vspace` writes for the level-1 table at `l1`. -/ +def utHighDescA (l1 : U64) : U64 := + ((core.num.U64.wrapping_sub l1 layout.PPTR_BASE &&& vspace.ADDR_MASK) ||| 576460752303423488#u64 ||| 1#u64) ||| 2#u64 + +/-- The descriptor is valid, a table, and names `l1`, for an aligned `l1` in the kernel window. -/ +theorem utHighDesc_relA (l1 : U64) (hk : 18446603336221196288 ≤ l1.val) (hal : l1.val % 4096 = 0) : + dBitA (utHighDescA l1) 0 = true ∧ dBitA (utHighDescA l1) 1 = true ∧ dTabA (utHighDescA l1) = l1 := by + have hbv : (utHighDescA l1).bv = ((((l1.bv - layout.PPTR_BASE.bv) &&& 281474976706560#64) ||| + 576460752303423488#64) ||| 1#64) ||| 2#64 := by with_unfolding_all rfl + refine ⟨?_, ?_, ?_⟩ + · unfold dBitA + rw [hbv] + simp only [BitVec.getLsbD_or] + rw [show (1#64).getLsbD 0 = true by decide] + simp only [Bool.or_true, Bool.true_or] + · unfold dBitA + rw [hbv] + simp only [BitVec.getLsbD_or] + rw [show (2#64).getLsbD 1 = true by decide] + simp only [Bool.or_true] + · have hP : layout.PPTR_BASE.bv.toNat = 18446603336221196288 := by simp [layout.PPTR_BASE] + have hl : l1.bv.toNat = l1.val := rfl + have hlt : l1.val < 2 ^ 64 := l1.bv.isLt + obtain ⟨x, hx⟩ : ∃ x : BitVec 64, x = l1.bv - layout.PPTR_BASE.bv := ⟨_, rfl⟩ + have hxn : x.toNat = l1.val - 18446603336221196288 := by + rw [hx, BitVec.toNat_sub_of_le (by rw [BitVec.le_def, hP, hl]; exact hk), hP, hl] + have hx48 : x.toNat < 2 ^ 48 := by omega + have hx12 : x.toNat % 2 ^ 12 = 0 := by omega + have hm : ∀ i < 64, (281474976706560#64).getLsbD i = (decide (12 ≤ i) && decide (i < 48)) := by decide + have hxm : x &&& 281474976706560#64 = x := by + apply BitVec.eq_of_getLsbD_eq + intro i hi + rw [BitVec.getLsbD_and, hm i hi] + by_cases h12 : 12 ≤ i + · by_cases h48 : i < 48 + · simp [h12, h48] + · have : x.getLsbD i = false := by + rw [BitVec.getLsbD] + exact Nat.testBit_lt_two_pow (Nat.lt_of_lt_of_le hx48 (Nat.pow_le_pow_right (by omega) (by omega))) + simp [this] + · have : x.getLsbD i = false := by + rw [BitVec.getLsbD] + have h := Nat.testBit_mod_two_pow x.toNat 12 i + rw [hx12, Nat.zero_testBit] at h + simpa [show i < 12 by omega] using h.symm + simp [this] + have hd : (dAddrA (utHighDescA l1)).bv = x := by + show (utHighDescA l1).bv &&& 281474976706560#64 = x + rw [hbv, ← hx, hxm, BitVec.and_or_distrib_right, BitVec.and_or_distrib_right, BitVec.and_or_distrib_right, + hxm, show (576460752303423488#64) &&& 281474976706560#64 = 0#64 by decide, + show (1#64) &&& 281474976706560#64 = 0#64 by decide, show (2#64) &&& 281474976706560#64 = 0#64 by decide] + simp only [BitVec.or_zero] + apply UScalar.eq_of_val_eq + have : (dTabA (utHighDescA l1)).bv = l1.bv := by + have hwa : ∀ p q : U64, (core.num.U64.wrapping_add p q).bv = p.bv + q.bv := by + intro p q + apply BitVec.eq_of_toNat_eq + rw [BitVec.toNat_add] + exact wa_valP p q + unfold dTabA + rw [hwa, hd, hx] + with_unfolding_all exact BitVec.sub_add_cancel l1.bv layout.PPTR_BASE.bv + exact congrArg BitVec.toNat this + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-- **A retyped `ArmHighVSpace`** (module docstring). -/ +theorem ut_highVSpaceA (ar : machine.Arch A) (s : S) (a : A) (nt base us : U64) (dev : Bool) (k : U64 → U64) + (hn : nt.val = 14) (hal : base.val % 4096 = 0) (hk : 18446603336221196288 ≤ base.val) + (hf : base.val + 8192 ≤ 2 ^ 64) + (hz : ∀ i : U64, i.val < 512 → L.mem s (peAF base i) = 0#u64) + (hz1 : ∀ i : U64, i.val < 512 → L.mem s (peAF (wa base 4096#u64) i) = 0#u64) + (hK : ∀ i : U64, i.val < 511 → ar.kernel_ttbr1_entry a 0#u64 i = ok (k i)) + (hK0 : ∀ i : U64, i.val < 256 → k i = 0#u64) : + objects.create_object st ar s a nt base us dev ⦃ r => + r.1 = .HighVSpace base false 0#u64 ∧ r.2.2 = a ∧ WritesIn st s r.2.1 (W7.R.ST.InWin base 4096) ∧ + (∀ i : U64, i.val < 511 → L.mem r.2.1 (peAF base i) = k i) ∧ + (∀ i : U64, i.val < 256 → L.mem r.2.1 (peAF base i) = 0#u64) ∧ + (dBitA (L.mem r.2.1 (peAF base 511#u64)) 0 = true ∧ dBitA (L.mem r.2.1 (peAF base 511#u64)) 1 = true ∧ + dTabA (L.mem r.2.1 (peAF base 511#u64)) = wa base 4096#u64) ∧ + (∀ i : U64, i.val < 512 → L.mem r.2.1 (peAF (wa base 4096#u64) i) = 0#u64) ∧ + (∀ (Tb : Set U64) (Lv : U64 → Nat) (i : U64), i.val < 512 → + PteRelA Tb Lv 1 (L.mem r.2.1 (peAF (wa base 4096#u64) i)) .invalidPTE) ⦄ := by + have hf0 : base.val + 4096 ≤ 2 ^ 64 := by omega + have hal8 : base.val % 8 = 0 := by omega + have hl1 : (wa base 4096#u64).val = base.val + 4096 := by + rw [wa_valP] + have : (4096#u64 : U64).val = 4096 := rfl + rw [this] + exact Nat.mod_eq_of_lt (by omega) + -- the level-0 and level-1 words + have hv : ∀ i : U64, i.val < 512 → (peAF base i).val = base.val + 8 * i.val := fun i hi => peAF_valA hf0 hi + have hv1 : ∀ i : U64, i.val < 512 → (peAF (wa base 4096#u64) i).val = base.val + 4096 + 8 * i.val := by + intro i hi + rw [peAF_valA (by rw [hl1]; omega) hi, hl1] + have hrd : ∀ (s2 : S) (i j : U64) (v : U64), i.val < 512 → j.val < 512 → + L.mem (L.wr s2 (peAF base i) v) (peAF base j) = if i.val = j.val then v else L.mem s2 (peAF base j) := by + intro s2 i j v hi hj + rw [L.mem_wr s2 _ v _ (by show _ % 8 = 0; rw [hv i hi]; omega) (by show _ % 8 = 0; rw [hv j hj]; omega)] + have : peAF base i = peAF base j ↔ i.val = j.val := by + constructor + · intro e + have := congrArg UScalar.val e + rw [hv i hi, hv j hj] at this + omega + · intro e + rw [UScalar.eq_of_val_eq e] + simp only [this] + have hW : ∀ (s2 : S) (i : U64) (v : U64), i.val < 512 → WritesIn st s s2 (W7.R.ST.InWin base 4096) → + WritesIn st s (L.wr s2 (peAF base i) v) (W7.R.ST.InWin base 4096) := by + intro s2 i v hi ⟨k1, k2, k3⟩ + refine ⟨by rw [L.ksb_wr, k1], by rw [L.cpu_wr, k2], fun b hb hnb => ?_⟩ + have hne : peAF base i ≠ b := fun e => hnb (by + subst e + unfold W7.R.ST.InWin + rw [hv i hi] + omega) + rw [L.mem_wr s2 _ v b (by show _ % 8 = 0; rw [hv i hi]; omega) hb, ite_eq_right hne, k3 b hb hnb] + have h511 : vspace.HIGH_L0_INDEX.val = 511 := by simp [vspace.HIGH_L0_INDEX] + have hloop : vspace.init_high_vspace_loop st ar s a base 0#u64 ⦃ s' => WritesIn st s s' (W7.R.ST.InWin base 4096) ∧ + ∀ j : U64, j.val < 512 → L.mem s' (peAF base j) = if j.val < 511 then k j else 0#u64 ⦄ := by + unfold vspace.init_high_vspace_loop + refine loop.spec_decr_nat (measure := fun x => 511 - x.2.val) + (inv := fun x => x.2.val ≤ 511 ∧ WritesIn st s x.1 (W7.R.ST.InWin base 4096) ∧ + ∀ j : U64, j.val < 512 → L.mem x.1 (peAF base j) = if j.val < x.2.val then k j else 0#u64) _ _ _ ?_ + ⟨by simp, WritesIn.refl _ _, fun j hj => by rw [hz j hj, ite_eq_right (by simp)]⟩ + rintro ⟨s2, i⟩ ⟨hi, hw, hm⟩ + simp only at hi hw hm ⊢ + unfold vspace.init_high_vspace_loop.body + by_cases hit : i < vspace.HIGH_L0_INDEX + · rw [ite_eq_left hit] + have hit' : i.val < 511 := by have : i.val < vspace.HIGH_L0_INDEX.val := hit; omega + rw [hK i hit', bind_ok] + have hstep : ∃ s3, (if k i != 0#u64 then (do + let i1 ← i * 8#u64 + let i2 ← lift (core.num.U64.wrapping_add base i1) + st.write s2 i2 (k i)) + else ok s2) = ok s3 ∧ WritesIn st s s3 (W7.R.ST.InWin base 4096) ∧ + ∀ j : U64, j.val < 512 → L.mem s3 (peAF base j) = if j.val < i.val + 1 then k j else 0#u64 := by + by_cases he : k i = 0#u64 + · refine ⟨s2, ite_eq_right (by simp [he]), hw, fun j hj => ?_⟩ + rw [hm j hj] + by_cases hji : j.val = i.val + · obtain rfl : j = i := UScalar.eq_of_val_eq hji + rw [ite_eq_right (by omega), ite_eq_left (by omega), he] + · by_cases h : j.val < i.val + · rw [ite_eq_left h, ite_eq_left (by omega)] + · rw [ite_eq_right h, ite_eq_right (by omega)] + · obtain ⟨i1, h1e, h1v⟩ := spec_exists_dut (U64.mul_spec (x := i) (y := 8#u64) (by scalar_tac)) + have h1 : i1.val = i.val * 8 := by simpa using h1v + have hadr : core.num.U64.wrapping_add base i1 = peAF base i := by + apply UScalar.eq_of_val_eq + rw [hv i (by omega), wa_valP, h1, Nat.mod_eq_of_lt (by omega)] + omega + refine ⟨L.wr s2 (peAF base i) (k i), ?_, hW _ _ _ (by omega) hw, fun j hj => ?_⟩ + · rw [ite_eq_left (by simp [he]), h1e, bind_ok] + simp only [lift, bind_ok] + rw [hadr, L.write_eq] + · rw [hrd s2 i j (k i) (by omega) hj, hm j hj] + by_cases hji : i.val = j.val + · obtain rfl : i = j := UScalar.eq_of_val_eq hji + rw [ite_eq_left rfl, ite_eq_left (by omega)] + · rw [ite_eq_right hji] + by_cases h : j.val < i.val + · rw [ite_eq_left h, ite_eq_left (by omega)] + · rw [ite_eq_right h, ite_eq_right (by omega)] + obtain ⟨s3, e3, hw3, hm3⟩ := hstep + obtain ⟨i5, h5e, h5v⟩ := spec_exists_dut (U64.add_spec (x := i) (y := 1#u64) (by scalar_tac)) + have h5 : i5.val = i.val + 1 := by simpa using h5v + rw [e3, bind_ok, h5e, bind_ok, WP.spec_ok] + refine ⟨⟨show i5.val ≤ 511 by omega, hw3, fun j hj => ?_⟩, show 511 - i5.val < 511 - i.val by omega⟩ + show L.mem s3 (peAF base j) = if j.val < i5.val then k j else 0#u64 + rw [h5] + exact hm3 j hj + · rw [ite_eq_right hit, WP.spec_ok] + have : i.val = 511 := by have : ¬ i.val < vspace.HIGH_L0_INDEX.val := hit; omega + exact ⟨hw, fun j hj => by rw [hm j hj, this]⟩ + obtain ⟨s1, e1, hw1, hm1⟩ := spec_exists_dut hloop + obtain ⟨i, hie, hiv⟩ := spec_exists_dut (U64.mul_spec (x := vspace.HIGH_L0_INDEX) (y := 8#u64) (by + rw [h511]; scalar_tac)) + have hi : i.val = 4088 := by have := hiv; simp only [h511] at this; simpa using this + have h511v : (511#u64 : U64).val = 511 := rfl + have hadr : core.num.U64.wrapping_add base i = peAF base 511#u64 := by + apply UScalar.eq_of_val_eq + rw [hv _ (by omega), wa_valP, hi, h511v, Nat.mod_eq_of_lt (by omega)] + obtain ⟨dp, edp, hdp⟩ := spec_exists_dut Fl4ProofsA64.P0.Consts.vspace.D_PXN_TABLE.spec + obtain ⟨dv, edv, hdv⟩ := spec_exists_dut Fl4ProofsA64.P0.Consts.vspace.D_VALID.spec + obtain ⟨dt, edt, hdt⟩ := spec_exists_dut Fl4ProofsA64.P0.Consts.vspace.D_TABLE_OR_PAGE.spec + have einit : vspace.init_high_vspace st ar s a base = + ok (L.wr s1 (peAF base 511#u64) (utHighDescA (wa base 4096#u64)), a) := by + unfold vspace.init_high_vspace + simp only [lift, bind_ok] + rw [e1, bind_ok, hie, bind_ok] + simp only [layout.paddr, bind_ok] + rw [edp, bind_ok, edv, bind_ok, edt, bind_ok, hadr, L.write_eq, bind_ok, hdp, hdv, hdt] + rfl + have ecr : objects.create_object st ar s a nt base us dev = + ok (.HighVSpace base false 0#u64, L.wr s1 (peAF base 511#u64) (utHighDescA (wa base 4096#u64)), a) := by + obtain rfl : nt = 14#u64 := UScalar.eq_of_val_eq (by simp [hn]) + unfold objects.create_object objects.create_arch_object + simp (config := { decide := true }) only [fl4_abi.consts.object.Tcb, fl4_abi.consts.object.Endpoint, + fl4_abi.consts.object.Notification, fl4_abi.consts.object.CapTable, fl4_abi.consts.object.Untyped, + fl4_abi.consts.object.SchedContext, fl4_abi.consts.object.Reply, fl4_abi.consts.object.ArmPageTable, + fl4_abi.consts.object.ArmVSpace, fl4_abi.consts.object.ArmVcpu, fl4_abi.consts.object.ArmStage2VSpace, + fl4_abi.consts.object.ArmHighVSpace, ↓reduceIte] + rw [einit] + simp only [bind_ok, uncurry_apply_pair] + rw [ecr, WP.spec_ok] + have hw2 := hW s1 511#u64 (utHighDescA (wa base 4096#u64)) (by omega) hw1 + have hlo : ∀ j : U64, j.val < 511 → + L.mem (L.wr s1 (peAF base 511#u64) (utHighDescA (wa base 4096#u64))) (peAF base j) = k j := by + intro j hj + rw [hrd s1 511#u64 j _ (by omega) (by omega), ite_eq_right (by omega), hm1 j (by omega), ite_eq_left hj] + have htop : L.mem (L.wr s1 (peAF base 511#u64) (utHighDescA (wa base 4096#u64))) (peAF base 511#u64) = + utHighDescA (wa base 4096#u64) := by + rw [hrd s1 511#u64 511#u64 _ (by omega) (by omega), ite_eq_left rfl] + have hhi : ∀ i : U64, i.val < 512 → + L.mem (L.wr s1 (peAF base 511#u64) (utHighDescA (wa base 4096#u64))) (peAF (wa base 4096#u64) i) = 0#u64 := by + intro j hj + rw [hw2.2.2 _ (by show _ % 8 = 0; rw [hv1 j hj]; omega) (by unfold W7.R.ST.InWin; rw [hv1 j hj]; omega), hz1 j hj] + refine ⟨rfl, rfl, hw2, hlo, fun j hj => by rw [hlo j (by omega), hK0 j hj], ?_, hhi, fun Tb Lv j hj => ?_⟩ + · rw [htop] + exact utHighDesc_relA _ (by rw [hl1]; omega) (by rw [hl1]; omega) + · rw [hhi j hj] + exact pteRelA_zero_inv Tb Lv 1 + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtVsOkBootA.lean b/proofs/Fl4ProofsA64/P3/UtVsOkBootA.lean new file mode 100644 index 000000000..9bdf212af --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtVsOkBootA.lean @@ -0,0 +1,64 @@ +/- +P3 (aarch64, A64-P3 Untyped; lead 10:53:55Z item 3): **D1b's `vsOk` holds at both boot witnesses.** + +`UtVsOkA` places the words of the installed pools and the tables reachable from them. At `w1` the record's table set +is the root task's four tables (`w1Tb`, no kernel table) and the installed pool is `PLb`, so the reachable set lies in +A64 P1's `w1Pt`, where `w1_vrOK` places every word. At `w0` the pool and table sets are empty (`boot_vrOK`). + +* `ut_vrOK_monoA`: `VrOK` over a smaller table set, with no excluded user word. +* **`w0_vsOkA`**, **`w1_vsOkA`**. + +No named premise. New theorems only. +-/ +import Fl4ProofsA64.P3.UtD1bA +import Fl4ProofsA64.P3.Stage3J7A +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P1.W7.R.ST.BootWit Fl4ProofsA64.P2.BootEpB + +theorem ut_vrOK_monoA {Ω Sg Scs : Set U64} {Z : U64 → Nat} {OS Vc Ub : Set U64} {kb : U64} {Pt Pt' : Set U64} + (h : VrOK Ω Sg Scs Z OS Vc Ub kb Pt) (sub : Pt' ⊆ Pt) : VrOK Ω Sg Scs Z OS Vc ∅ kb Pt' := by + have m : ∀ b, VsR kb Pt' b → VsR kb Pt b := fun b hb => + hb.imp (fun ⟨p, hp, r⟩ => ⟨p, sub hp, r⟩) id + exact + { al := fun b hb => h.al b (m b hb) + om := fun b hb => h.om b (m b hb) + sg := fun b hb => h.sg b (m b hb) + sc := fun b hb => h.sc b (m b hb) + os := fun b hb => h.os b (m b hb) + vc := fun b hb => h.vc b (m b hb) + ub := fun _ _ => Set.notMem_empty _ + ks := fun b hb => h.ks b (m b hb) } + +variable {kb ib r now mt sl : U64} {nc : Nat} {uw : U64 → U64} + +/-- **`vsOk` at `w0`.** -/ +theorem w0_vsOkA (P : BootLay kb ib nc) (Lv : U64 → Nat) : + UtVsOkA bst (bootH kb ib nc) (bootOS kb ib nc) (bootZA kb ib nc).V ∅ ∅ Lv (bootS kb ib nc r now mt) := by + have e : utVsPtA bst ∅ ∅ Lv (bootS kb ib nc r now mt) = ∅ := by + refine Set.eq_empty_of_forall_notMem fun p h => ?_ + rcases h with h | h + · exact h.1 + · exact VsRt_sub h + unfold UtVsOkA + rw [e] + exact boot_vrOK P + +/-- **`vsOk` at `w1`.** -/ +theorem w1_vsOkA (P : BootLay1 kb ib nc) : + UtVsOkA bst (w1H kb ib nc) (w1OS kb ib nc) (w1ZA kb ib nc).V + (Fl4ProofsA64.P2.BootEpB.w1Pp kb ib nc r now mt sl uw) w1Tb w1Lv (w1S kb ib nc r now mt sl uw) := by + have sub : utVsPtA bst (Fl4ProofsA64.P2.BootEpB.w1Pp kb ib nc r now mt sl uw) w1Tb w1Lv + (w1S kb ib nc r now mt sl uw) ⊆ w1Pt := by + rintro p (h | h) + · rw [w1_pp P h.1] + exact Or.inr (Or.inl (bwv_val (by norm_num))) + · rcases VsRt_sub h with g | g | g | g + · exact Or.inl g + · exact Or.inr (Or.inr (Or.inl g)) + · exact Or.inr (Or.inr (Or.inr (Or.inl g))) + · exact Or.inr (Or.inr (Or.inr (Or.inr g))) + exact ut_vrOK_monoA (w1_vrOK P) sub + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtVsOkJA.lean b/proofs/Fl4ProofsA64/P3/UtVsOkJA.lean new file mode 100644 index 000000000..93af9f6ab --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtVsOkJA.lean @@ -0,0 +1,64 @@ +/- +P3 (aarch64, A64-P3 Untyped): **the record's `vsOk` follows from J7 and the narrowed typing.** + +The pools and tables reachable under the record's typing are pages of J7's `Pt`: J7's `S1aR2` types the same installed +pools, and by induction along the walk the two typings give each reachable table the same level. So J7's placement of +`Pt` places them. Steps that re-establish J7 (the drops) get `vsOk` from this. + +* `utVsPt_subR2A`: `utVsPtA ⊆ Pt`. +* **`utVsOk_of_J7A`**. + +No named premise. New theorems only. +-/ +import Fl4ProofsA64.P3.Stage3J7A +import Fl4ProofsA64.P3.Stage3D1bA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Classical + +local notation "wa" => core.num.U64.wrapping_add + +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {A : Type} {ar : machine.Arch A} +variable {H : Heap} {OS Pt Pp7 Tb7 Hr Te Kt Pp Tb : Set U64} {Lv7 Re bits Lv : U64 → Nat} {n : U64} {s : S} + +theorem utVsPt_subR2A (R : S1aR2 st H OS Pt Pp7 Tb7 Hr Te Kt Lv7 Re bits n s) + (vs : VsTyR st (L.ksb s) (PoolSet st Pp s) ∅ Tb Lv s) : utVsPtA st Pp Tb Lv s ⊆ Pt := by + have v7 := R.core.stage1 + have pools : PoolSet st Pp s ⊆ PoolSet st Pp7 s := by + rintro p ⟨-, o, h1, h2, h8, hm, hn⟩ + have h := v7.tbl o h1 h2 h8 (hm ▸ hn) + rwa [hm] at h + have walk : ∀ p, VsReach st (PoolSet st Pp s) ∅ Lv s p → + p ∈ Tb ∧ p ∈ Tb7 ∧ Lv7 p = Lv p ∧ VsReach st (PoolSet st Pp7 s) ∅ Lv7 s p := by + intro p hr + induction hr with + | hr hR => exact absurd hR (Set.notMem_empty _) + | @root0 q o hq ho h8 hne h0 => + have a := (vs.pool q hq o ho h8 hne).1 h0 + have b := (v7.pool q (pools hq) o ho h8 hne).1 h0 + exact ⟨a.1, b.1, b.2.trans a.2.symm, .root0 (pools hq) ho h8 hne h0⟩ + | @root2 q o hq ho h8 h2 => + have hne : L.mem s (wa q o) ≠ 0#u64 := fun e => by rw [e] at h2; exact absurd h2 (by decide) + have a := (vs.pool q hq o ho h8 hne).2 h2 + have b := (v7.pool q (pools hq) o ho h8 hne).2 h2 + exact ⟨a.1, b.1, b.2.trans a.2.symm, .root2 (pools hq) ho h8 h2⟩ + | @step q o hq hl ho h8 h3 ih => + obtain ⟨hT, hT7, hlv, hr7⟩ := ih + have a := vs.desc q hT hq hl o ho h8 h3 + have hl7 : Lv7 q < 3 := by omega + have b := v7.desc q hT7 hr7 hl7 o ho h8 h3 + exact ⟨a.1, b.1, by rw [b.2, a.2, hlv], .step hr7 hl7 ho h8 h3⟩ + rintro p (h | h) + · exact R.place.pools (pools h) + · exact R.place.tables (walk p h.2).2.1 + +/-- **`vsOk` from J7 and the narrowed typing** (module docstring). -/ +theorem utVsOk_of_J7A {Z : XSetsA} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} {nA : U64} + (h : J7A st (J7R2A st ar nA) Z H OS s σ m) (vs : VsTyR st (L.ksb s) (PoolSet st Pp s) ∅ Tb Lv s) : + UtVsOkA st H OS Z.V Pp Tb Lv s := by + obtain ⟨Pt, Ub, vr, -, -, -, -, -, -, -, -, -, -, -, Pp7, Tb7, Hr, Te, Kt, Lv7, Re, bits, r2, -⟩ := h + exact vrOK_monoPtA vr (utVsPt_subR2A r2 vs) + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/UtZeroSpecA.lean b/proofs/Fl4ProofsA64/P3/UtZeroSpecA.lean new file mode 100644 index 000000000..365ec1f30 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/UtZeroSpecA.lean @@ -0,0 +1,172 @@ +/- +P3 (aarch64, A64-P3 Untyped, `UtResetA`): **the record across spec `delete_objects`' memory zeroing**. + +Spec `delete_objects` zeroes the region's bytes as well as dropping its objects (`utDelStA`); FL4's memory is still +unchanged then. Only two record fields read the spec's bytes: `ep` (the IPC buffer pairs of `Bf` hold FL4's words) and +`specZero`. So the record holds at the zeroed state with the buffer pairs that miss the region (`utBfOffA`). + +Named premise: +* [J] `hsbuf`: a retained thread's spec IPC buffer misses the region (derived in `P3/UtSbufA` from the retyped + childless untyped and the 1024-alignment of the spec's `tcbIpcBuffer`). +-/ +import Fl4ProofsA64.P3.UtDropTopA +import Fl4ProofsA64.P3.W1UserBytesA +import Fl4ProofsA64.P3.Stage3BootA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4SpecA64 Classical + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {ar : machine.Arch A} + +/-- The word at `bufAddr p k` misses `R`, all 8 bytes. -/ +def UtBufOffA (R : Set Word) (p : Word) : Prop := + ∀ k : Nat, k < 128 → ∀ j : Nat, j < 8 → bufAddr p k + BitVec.ofNat 64 j ∉ R + +/-- The buffer pairs whose spec buffer misses `R`. -/ +def utBfOffA (R : Set Word) (Bf : Set (U64 × Word)) : Set (U64 × Word) := {bp | bp ∈ Bf ∧ UtBufOffA R bp.2} + +theorem ut_loadWordAt_congrA {um um' : Word → Word8} {p : Word} + (h : ∀ j : Nat, j < 8 → um' (p + BitVec.ofNat 64 j) = um (p + BitVec.ofNat 64 j)) : + loadWordAt um' p = loadWordAt um p := by + unfold loadWordAt + congr 1 + refine List.map_congr_left fun i hi => ?_ + rw [intUpto_07] at hi + simp only [List.mem_cons, List.not_mem_nil, or_false] at hi + have key : ∀ (c : Int) (j : Nat), j < 8 → (7 - BitVec.ofInt 64 c : Word) = BitVec.ofNat 64 j → + um' (p + (7 - BitVec.ofInt 64 c)) = um (p + (7 - BitVec.ofInt 64 c)) := by + intro c j hj e + rw [e]; exact h j hj + rcases hi with rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl + · exact key _ 7 (by omega) (by decide) + · exact key _ 6 (by omega) (by decide) + · exact key _ 5 (by omega) (by decide) + · exact key _ 4 (by omega) (by decide) + · exact key _ 3 (by omega) (by decide) + · exact key _ 2 (by omega) (by decide) + · exact key _ 1 (by omega) (by decide) + · exact key _ 0 (by omega) (by decide) + +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Word)} {stack : U64 → List U64} {κ : U64 → U64} + {n : Nat} {nA : U64} {s : S} {a : A} {σ : State Unit} {m : McState} + +/-- **The record across the zeroing of `R`'s spec bytes** (module docstring). -/ +theorem utZeroSpec_stageA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (R : Set Word) (um : Word → Word8) + (hum : ∀ w, um w = if w ∈ R then 0 else σ.machineState.underlyingMemory w) + (hsbuf : ∀ t ∈ Z.T, ∀ x b sb, σ.kheap (specPtr t) = some (.tcb x) → ipcBufOfA b x = some sb → UtBufOffA R sb) : + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp (utBfOffA R Bf) stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a + (withUmA σ um) m := by + have sub : utBfOffA R Bf ⊆ Bf := fun _ h => h.1 + have bw : ∀ w, BufW (utBfOffA R Bf) w → BufW Bf w := fun w ⟨bp, hbp, rest⟩ => ⟨bp, sub hbp, rest⟩ + have ep : EpStateRel Z.T E Rp (utBfOffA R Bf) (L.mem s) (withUmA σ um) := + { ep := J.ep.ep + reply := J.ep.reply + tcb := J.ep.tcb + buf := fun bp hbp k hk => by + obtain ⟨h1, h2⟩ := J.ep.buf bp hbp.1 k hk + refine ⟨?_, h2⟩ + have e : loadWordAt um (bufAddr bp.2 k.val) = loadWordAt σ.machineState.underlyingMemory (bufAddr bp.2 k.val) := + ut_loadWordAt_congrA fun j hj => by + rw [hum, ite_eq_right (hbp.2 k.val hk j hj)] + show _ = loadWordAt um _ + rw [e]; exact h1 } + have ip : IPlacedA N Z.T E Rp (utBfOffA R Bf) := + { n := J.iplaced.n + e := + { tcb := J.iplaced.e.tcb + ep := J.iplaced.e.ep + rep := J.iplaced.e.rep + et := J.iplaced.e.et + rt := J.iplaced.e.rt + er := J.iplaced.e.er + balign := fun w h => J.iplaced.e.balign w (bw w h) + bt := fun w h => J.iplaced.e.bt w (bw w h) + be := fun w h => J.iplaced.e.be w (bw w h) + br := fun w h => J.iplaced.e.br w (bw w h) + binj := fun bp hbp bq hbq => J.iplaced.e.binj bp (sub hbp) bq (sub hbq) } + ne := J.iplaced.ne + nr := J.iplaced.nr + nb := fun q hq w h => J.iplaced.nb q hq w (bw w h) } + have unm : UnmPtSpecA st Z m OS s (withUmA σ um) := + { pt := fun u hu p asid va h => by + have g := J.unmPt.pt u hu p asid va h + exact ⟨g.inv, g.spec, { g.off with }⟩ + vs := fun u hu p asid h => by + have g := J.unmPt.vs u hu p asid h + exact ⟨g.inv, g.spec, { g.off with }⟩ + s2 := fun u hu p asid va h => by + have g := J.unmPt.s2 u hu p asid va h + exact ⟨g.inv, g.spec⟩ + hv := fun u hu p asid h => by + have g := J.unmPt.hv u hu p asid h + exact ⟨g.inv, g.spec⟩ } + exact + { J with + j := JAtZAX_withUmA J.j um + sidle := J.sidle.congr rfl rfl (BitVec.le_refl _) + schedValid := ⟨J.schedValid.rq, J.schedValid.act, J.schedValid.blk, J.schedValid.rls⟩ + vcSnap := ⟨J.vcSnap.snap, J.vcSnap.mask⟩ + bfSpec := fun bp h => J.bfSpec bp h.1 + tabInj := J.tabInj + sr := J.sr.withUmA um + scCov := J.scCov + blkQ := ⟨J.blkQ.ep, J.blkQ.ntfn⟩ + scSize := J.scSize + ep := ep + ntfn := { J.ntfn with } + reply := { J.reply with } + bufCover := fun t ht x b sb w hx hb hl => by + obtain ⟨h1, h2⟩ := J.bufCover t ht x b sb w hx hb hl + exact ⟨⟨h1, hsbuf t ht x b sb hx hb⟩, h2⟩ + iplaced := ip + ipcPlace := + { notification := J.ipcPlace.notification + endpoint := J.ipcPlace.endpoint + reply := J.ipcPlace.reply + buffer := fun w h => J.ipcPlace.buffer w (bw w h) } + idleNoFault := J.idleNoFault + mcp := J.mcp + yieldRel := { J.yieldRel with } + sinv := SInvXA_withUmA J.sinv um + deadExt := { J.deadExt with } + specFree := { J.specFree with } + unmPt := unm + specZero := fun w hw => by + show um w = 0 + rw [hum] + split + · rfl + · exact J.specZero w hw + origLive := J.origLive + nullOrig := J.nullOrig + finCov := ⟨J.finCov.cnode, J.finCov.tcb⟩ + devFr := J.devFr + bufOff := + { os := fun b hb => J.bufOff.os b (bw b hb) + vs := fun b hb => J.bufOff.vs b (bw b hb) + page := fun b hb => J.bufOff.page b (bw b hb) + vcw := fun b hb => J.bufOff.vcw b (bw b hb) + pin := fun bp h => J.bufOff.pin bp (sub h) } + curVcpuNone := J.curVcpuNone + domOK := { J.domOK with } + ipiZero := J.ipiZero + ipcSorted := J.ipcSorted + activeReplyScs := J.activeReplyScs + bufVs := fun b hb => J.bufVs b (bw b hb) + j5 := J.j5 + j7 := J.j7 + ipcTh := J.ipcTh } + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/VcpuCheckJA.lean b/proofs/Fl4ProofsA64/P3/VcpuCheckJA.lean new file mode 100644 index 000000000..090770faa --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/VcpuCheckJA.lean @@ -0,0 +1,744 @@ +/- +P3 (aarch64, A64-P3 S4 W4): **`vcpu.check` refinement and `ScFinishVcpuA.S4FinishHookA` +discharge**: +- `VcpuXferStateA.ntfn_set_idle`: single-word `obj.Ntfn.set_state ... NTFN_IDLE` framing on + `VcpuXferStateA` and `VcpuRunPresA`. +- `vcpu_check_s4_hookA`: discharges `ScFinishVcpuA.S4FinishHookA` directly from + `complete_run_refinesA`. +- `vcpu_check_refinesA`: `vcpu.check st ar s a ct` refines `fl4VcpuCheck (specPtr ct)` on + `VcpuXferStateA` (all 4 branches: non-`RunningVM`, `RunningVM` + `runVcpu = 0` recovery, + `RunningVM` + `runVcpu ≠ 0` + no active bound notification, and `RunningVM` + `runVcpu ≠ 0` + + active bound notification). + +No new named premise. +-/ +import Fl4ProofsA64.P3.VcpuRunLinkJA +import Fl4ProofsA64.P3.ScFinishVcpuA + +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3.Vcpu + +open Fl4Spec Fl4SpecA64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3 Fl4ProofsA64.P3.Sched +open Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +private theorem runVcpuA_eq (t : U64) : runVcpuA t = wa t 3672#u64 := rfl + +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +/-- Disjointness of a bound notification's state word `epTSA n` from the `VcpuXferStateA` +footprint (all VCPU pages, TCBs, SCs, CNode slots, `OS`, `Ub`, `ksb`, VSpace, and `ExtFootE`). -/ +structure NtfnStateWordDisjA (Z : XSetsA) (OS Ub : Set U64) (ksb n : U64) : Prop where + al : Aligned (epTSA n) + ne_vc : ¬ VcPage Z.V (epTSA n) + ne_ub : epTSA n ∉ Ub + ne_tcb : ∀ t ∈ Z.T, ∀ K : U64, K.val < 4096 → epTSA n ≠ wa t K + ne_sc : ∀ c ∈ Z.C, epTSA n ≠ wa c 24#u64 ∧ epTSA n ≠ wa c 72#u64 ∧ epTSA n ≠ maxA c ∧ + Aligned (wa c 24#u64) ∧ Aligned (wa c 72#u64) ∧ Aligned (maxA c) + ne_ks : ∀ o : U64, o.val < ksSizeA → epTSA n ≠ wa ksb o + ne_slot : ∀ y, SlotWA Z.th y → + epTSA n ≠ y ∧ epTSA n ≠ wa y 8#u64 ∧ epTSA n ≠ wa y 16#u64 ∧ epTSA n ≠ wa y 24#u64 + ne_os : ∀ y ∈ OS, + epTSA n ≠ y ∧ epTSA n ≠ wa y 8#u64 ∧ epTSA n ≠ wa y 16#u64 ∧ epTSA n ≠ wa y 24#u64 + ne_vs : ¬ VsFootA Z.Pp Z.S1 Z.Y ksb (epTSA n) + ne_ext : ¬ ExtFootE Z.X Z.Y Z.T2 (epTSA n) + ext_al : ∀ q, ExtFootE Z.X Z.Y Z.T2 q → Aligned q + vs_al : ∀ q, VsFootA Z.Pp Z.S1 Z.Y ksb q → Aligned q + +private theorem nodeAt_eq_wa_ksb_localA (s : S) (c o : U64) (hc : c.val < 128) (ho : o.val < 4288) : + ∃ k : U64, k.val < ksSizeA ∧ L.nodeAt s c o = wa (L.ksb s) k := + nodeAt_eq_wa_ksbA (L := L) s c o hc ho + +private theorem irqWordA_lt_ksSizeA (ksb i : U64) (hi : i.val < 5376) : + ∃ k : U64, k.val < ksSizeA ∧ irqWordA ksb i = wa ksb k := by + refine ⟨irqOffA i, ?_, rfl⟩ + unfold irqOffA ksSizeA + simp only [core.num.U64.wrapping_add_val_eq, core.num.U64.wrapping_mul_val_eq, + UScalar.size, UScalarTy.numBits] + have h224 : (224#u64).val = 224 := rfl + have h8 : (8#u64).val = 8 := rfl + rw [h224, h8]; omega + +set_option maxHeartbeats 600000 in +/-- Writing `obj.NTFN_IDLE` into a bound notification's state word `epTSA n`, paired with +updating `σ.kheap (specPtr n)` to `.notification { ntfn with ntfnObj := .idleNtfn }`, preserves +`VcpuXferStateA` and `VcpuRunPresA`. -/ +theorem VcpuXferStateA.ntfn_set_idle {AR : A → HwRootA → Prop} {Z : XSetsA} + {OS Ub : Set U64} {m : Fl4SpecA64.McState} {a : A} {s : S} {σ : State Unit} + (X : VcpuXferStateA (L := L) AR Z OS Ub m a s σ) + (hU : UbFootDisjA Z OS Ub) + (hCs : ∀ c ∈ Z.Cs, c.val < 128) (hcpu : (L.cpu s).val < 128) + (hfa : ∀ q, FootA (L := L) Z s q → Aligned q) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hIdleT : ∀ c ∈ Z.Cs, L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) ∈ Z.T) + {n : U64} {ntfn : Fl4SpecA64.Notification} + (hkn : σ.kheap (specPtr n) = some (.notification ntfn)) + (hND : NtfnStateWordDisjA Z OS Ub (L.ksb s) n) : + let s1 := L.wr s (epTSA n) ((L.mem s (epTSA n) &&& ~~~3#u64) ||| (obj.NTFN_IDLE &&& 3#u64)) + let σ1 := updKSc σ (specPtr n) (.notification { ntfn with ntfnObj := .idleNtfn }) + VcpuXferStateA (L := L) AR Z OS Ub m a s1 σ1 ∧ + L.ksb s1 = L.ksb s ∧ L.cpu s1 = L.cpu s ∧ + VcpuRunPresA L Z OS s s1 σ σ1 := by + intro s1 σ1 + have hksb1 : L.ksb s1 = L.ksb s := L.ksb_wr _ _ _ + have hcpu1 : L.cpu s1 = L.cpu s := L.cpu_wr _ _ _ + have hne_t : ∀ t ∈ Z.T, specPtr n ≠ specPtr t := fun t ht e => by + obtain ⟨xt, hkt, -⟩ := X.vcpu.core.tcb t ht; rw [← e, hkn] at hkt; cases hkt + have hne_v : ∀ v ∈ Z.V, specPtr n ≠ specPtr v := fun v hv e => by + obtain ⟨xv, hkv, -⟩ := X.vcpu.core.obj v hv; rw [← e, hkn] at hkv; cases hkv + have hne_c : ∀ c ∈ Z.C, specPtr n ≠ scRenA m c := fun c hc e => by + have hsc := X.j.1.sch.sc c hc + obtain ⟨y, n0, hkc, -⟩ := hsc; rw [← e, hkn] at hkc; cases hkc + have hwr_ne : ∀ {q : U64}, Aligned q → epTSA n ≠ q → L.mem s1 q = L.mem s q := + fun hq hne => mem_wr_neA hND.al hq hne + have hne_tcb_word : ∀ t ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → + L.mem s1 (wa t K) = L.mem s (wa t K) := fun t ht K hK hK8 => + hwr_ne (aligned_wa (by unfold Aligned; have := (hU.tcb_al t ht).align; omega) K hK8) (hND.ne_tcb t ht K hK) + have hne_node : ∀ c : U64, c.val < 128 → ∀ o : U64, o.val < 4288 → o.val % 8 = 0 → + L.mem s1 (L.nodeAt s c o) = L.mem s (L.nodeAt s c o) := fun c hc o ho ho8 => by + obtain ⟨k, hk, heq⟩ := nodeAt_eq_wa_ksb_localA (L := L) s c o hc ho + exact hwr_ne (X.vcpu.place.node_al c o ho8) (heq ▸ hND.ne_ks k hk) + have hos : OSKeep st OS s s1 := fun y hy => by + have hya := hU.al_os y hy + exact ⟨hwr_ne hya (hND.ne_os y hy).1, + hwr_ne (aligned_wa hya 8#u64 (by decide)) (hND.ne_os y hy).2.1, + hwr_ne (aligned_wa hya 16#u64 (by decide)) (hND.ne_os y hy).2.2.1, + hwr_ne (aligned_wa hya 24#u64 (by decide)) (hND.ne_os y hy).2.2.2⟩ + have hslot : ∀ y, SlotWA Z.th y → + L.mem s1 y = L.mem s y ∧ L.mem s1 (wa y 8#u64) = L.mem s (wa y 8#u64) ∧ + L.mem s1 (wa y 16#u64) = L.mem s (wa y 16#u64) ∧ L.mem s1 (wa y 24#u64) = L.mem s (wa y 24#u64) := + fun y hy => by + obtain ⟨h0, h8, h16, h24⟩ := hND.ne_slot y hy + exact ⟨hwr_ne (hfa _ (.slot y _ hy (Or.inl rfl))) h0, + hwr_ne (hfa _ (.slot y _ hy (Or.inr (Or.inl rfl)))) h8, + hwr_ne (hfa _ (.slot y _ hy (Or.inr (Or.inr (Or.inl rfl))))) h16, + hwr_ne (hfa _ (.slot y _ hy (Or.inr (Or.inr (Or.inr rfl))))) h24⟩ + have hcap_os : ∀ y ∈ OS, capAtA (L.mem s1) y = capAtA (L.mem s) y := fun y hy => by + obtain ⟨h0, h8, -, -⟩ := hos y hy; unfold capAtA; rw [h0, h8] + have hcap_sl : ∀ y, SlotWA Z.th y → capAtA (L.mem s1) y = capAtA (L.mem s) y := fun y hy => by + obtain ⟨h0, h8, -, -⟩ := hslot y hy; unfold capAtA; rw [h0, h8] + have hrev_sl : ∀ y, SlotWA Z.th y → revA (L.mem s1) y = revA (L.mem s) y := fun y hy => by + obtain ⟨-, -, -, h24⟩ := hslot y hy; unfold revA; rw [h24] + have hSt : StateRelA Z.th (L.mem s1) σ1 := by + refine ⟨fun p r hp => ?_, fun t ht => ?_⟩ + · obtain ⟨cs, hkp, hwf, hslots⟩ := X.j.1.st.cnode p r hp + have hne : specPtr p ≠ specPtr n := fun e => by rw [e, hkn] at hkp; cases hkp + refine ⟨cs, by rw [updKSc_other _ hne]; exact hkp, hwf, fun idx hidx => ?_⟩ + have hsr := hslots idx hidx + unfold SlotRelA at hsr ⊢ + rw [hcap_sl _ (Or.inr ⟨p, r, hp, idx, hidx, rfl⟩)]; exact hsr + · obtain ⟨xt, hkt, hslots⟩ := X.j.1.st.tcb t ht + have htT : t ∈ Z.T := (X.j.1.thT t).1 ht + refine ⟨xt, by rw [updKSc_other _ (hne_t t htT).symm]; exact hkt, fun i hi => ?_⟩ + have hsr := hslots i hi + unfold SlotRelA at hsr ⊢ + rw [hcap_sl _ (Or.inl ⟨t, Or.inr ⟨ht, i, hi, rfl⟩⟩)]; exact hsr + have hCdt : CdtRelA st Z.th s1 σ1 Z.G := by + have h0 := X.j.1.cdt + refine ⟨h0.tree.keepO (fun y hy => hslot y (Or.inl hy)), h0.cdt, h0.cdt_slot, + fun sl hsl => by rw [hrev_sl _ (absSlot_w hsl)]; exact h0.orig sl hsl, + h0.par_slot, fun a' p' hp' => by + have hp_sl : SlotWA Z.th p' := by + obtain ⟨sl, hsl, hsl_a⟩ := h0.par_slot a' p' hp' + have hcdt := h0.cdt sl hsl; rw [hsl_a, hp'] at hcdt + cases hq : σ.cdt sl with + | none => rw [hq] at hcdt; cases hcdt + | some q => + rw [hq] at hcdt; simp only [Option.map_some, Option.some.injEq] at hcdt + rw [← hcdt]; exact absSlot_w (h0.cdt_slot sl q hq).2 + rw [hrev_sl p' hp_sl]; exact h0.prev a' p' hp'⟩ + have hnode' : ∀ c ∈ Z.Cs, + L.mem s1 (L.nodeAt s1 c layout.node.CUR_THREAD) = L.mem s (L.nodeAt s c layout.node.CUR_THREAD) ∧ + L.mem s1 (L.nodeAt s1 c layout.node.IDLE_THREAD) = L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) ∧ + L.mem s1 (L.nodeAt s1 c layout.node.CUR_SC) = L.mem s (L.nodeAt s c layout.node.CUR_SC) ∧ + L.mem s1 (L.nodeAt s1 c layout.node.IDLE_SC) = L.mem s (L.nodeAt s c layout.node.IDLE_SC) := fun c hc => by + simp only [nodeAt_ksb hksb1] + exact ⟨hne_node c (hCs c hc) layout.node.CUR_THREAD (by unfold layout.node.CUR_THREAD; decide) (by unfold layout.node.CUR_THREAD; decide), + hne_node c (hCs c hc) layout.node.IDLE_THREAD (by unfold layout.node.IDLE_THREAD; decide) (by unfold layout.node.IDLE_THREAD; decide), + hne_node c (hCs c hc) layout.node.CUR_SC (by unfold layout.node.CUR_SC; decide) (by unfold layout.node.CUR_SC; decide), + hne_node c (hCs c hc) layout.node.IDLE_SC (by unfold layout.node.IDLE_SC; decide) (by unfold layout.node.IDLE_SC; decide)⟩ + have hSch : SchedRelA (L := L) Z.T Z.C Z.Cs Z.curOf Z.curScOf s1 σ1 m := by + have h0 := X.j.1.sch + exact { + core := by rw [hcpu1]; exact h0.core + cpuCs := by rw [hcpu1]; exact h0.cpuCs + curOfW := fun c hc => by rw [(hnode' c hc).1]; exact h0.curOfW c hc + curScOfW := fun c hc => by rw [(hnode' c hc).2.2.1]; exact h0.curScOfW c hc + cur := h0.cur + csc := h0.csc + idle := by unfold LawfulStore.idleW; rw [hcpu1, nodeAt_ksb hksb1, hne_node _ hcpu layout.node.IDLE_THREAD (by unfold layout.node.IDLE_THREAD; decide) (by unfold layout.node.IDLE_THREAD; decide)]; exact h0.idle + idleOf := fun c hc => by rw [(hnode' c hc).2.1]; exact h0.idleOf c hc + idleScOf := fun c hc => by rw [(hnode' c hc).2.2.2]; exact h0.idleScOf c hc + act := by unfold LawfulStore.actionW; rw [hcpu1, nodeAt_ksb hksb1, hne_node _ hcpu layout.node.SCHED_ACTION (by unfold layout.node.SCHED_ACTION; decide) (by unfold layout.node.SCHED_ACTION; decide)]; exact h0.act + time := by unfold LawfulStore.curTime LawfulStore.nodeA; rw [hcpu1, nodeAt_ksb hksb1, hne_node _ hcpu layout.node.CUR_TIME (by unfold layout.node.CUR_TIME; decide) (by unfold layout.node.CUR_TIME; decide)]; exact h0.time + aff := fun t ht => by + unfold affA; rw [hne_tcb_word t ht 3520#u64 (by decide) (by decide)] + exact h0.aff t ht + affT := h0.affT + tcb := fun t ht => by + obtain ⟨xt, hkt, hst, hsc0⟩ := h0.tcb t ht + refine ⟨xt, by rw [updKSc_other _ (hne_t t ht).symm]; exact hkt, ?_, ?_⟩ + · rw [stA_eq, hne_tcb_word t ht 3328#u64 (by decide) (by decide), ← stA_eq]; exact hst + · rw [scA_eq, hne_tcb_word t ht 3464#u64 (by decide) (by decide), ← scA_eq]; exact hsc0 + sc := fun c hc => by + have hne_c' : scRenA m c ≠ specPtr n := (hne_c c hc).symm + obtain ⟨y, n0, hkc, hrest⟩ := h0.sc c hc + refine ⟨y, n0, by rw [updKSc_other _ hne_c']; exact hkc, ?_⟩ + rw [hwr_ne (hND.ne_sc c hc).2.2.2.1 (hND.ne_sc c hc).1] + exact hrest + scOff := h0.scOff } + have hIrq : IrqRelA (L.mem s1) (L.ksb s1) σ1 := by + have h0 := X.j.1.irq + refine ⟨fun i hi => ?_, by rw [hksb1]; exact h0.node, by rw [hksb1]; exact h0.nodeHi⟩ + rw [hksb1] + obtain ⟨k, hk, heq⟩ := irqWordA_lt_ksSizeA (L.ksb s) i hi + rw [hwr_ne (hfa _ (.nslot _ (.irq i hi))) (heq ▸ hND.ne_ks k hk)]; exact h0.st i hi + have hRest : SchedRestA (L := L) Z s1 σ1 m := by + have h0 := X.j.1.rest + exact { + rq := fun p hp => by rw [hcpu1]; exact h0.rq p hp + rqDom := h0.rqDom + rel := by rw [hcpu1]; exact h0.rel + consumed := by rw [hcpu1, nodeAt_ksb hksb1, hne_node _ hcpu layout.node.CONSUMED (by unfold layout.node.CONSUMED; decide) (by unfold layout.node.CONSUMED; decide)]; exact h0.consumed + repr := by rw [hcpu1, nodeAt_ksb hksb1, hne_node _ hcpu layout.node.REPROGRAM (by unfold layout.node.REPROGRAM; decide) (by unfold layout.node.REPROGRAM; decide)]; exact h0.repr + curDom := by rw [hksb1, hwr_ne (hfa _ (.nslot _ (.glob _ (Or.inl rfl)))) (hND.ne_ks 8#u64 (by unfold ksSizeA; decide))]; exact h0.curDom + domTime := by rw [hksb1, hwr_ne (hfa _ (.nslot _ (.glob _ (Or.inr rfl)))) (hND.ne_ks 16#u64 (by unfold ksSizeA; decide))]; exact h0.domTime + prio := fun t ht => by + obtain ⟨x, hkt, h1, h2, h3⟩ := h0.prio t ht + refine ⟨x, by rw [updKSc_other _ (hne_t t ht).symm]; exact hkt, h1, ?_, ?_⟩ + · unfold domA at h2 ⊢; rw [hne_tcb_word t ht 3432#u64 (by decide) (by decide)]; exact h2 + · unfold prioA at h3 ⊢; rw [hne_tcb_word t ht 3448#u64 (by decide) (by decide)]; exact h3 + scMem := fun t ht hnz => by + rw [scA_eq, hne_tcb_word t ht 3464#u64 (by decide) (by decide), ← scA_eq] at hnz ⊢ + exact h0.scMem t ht hnz + refillMax := fun c hc => by + obtain ⟨y, n_sc, hks, hrm⟩ := h0.refillMax c hc + refine ⟨y, n_sc, by rw [updKSc_other _ (hne_c c hc).symm]; exact hks, ?_⟩ + rw [hwr_ne (hND.ne_sc c hc).2.2.2.2.1 (hND.ne_sc c hc).2.1]; exact hrm } + have hVs_kh : ∀ p, (∃ q ∈ Z.Pp, specPtr q = p) ∨ (∃ q ∈ Z.S1, specPtr q = p) ∨ (∃ q ∈ Z.Y, specPtr q = p) → + σ1.kheap p = σ.kheap p := fun p hp => by + apply updKSc_other + intro e; subst e + rcases hp with ⟨q, hq, he⟩ | ⟨q, hq, he⟩ | ⟨q, hq, he⟩ + · obtain ⟨ap, hkp, -⟩ := X.j.1.vs.pool q hq; rw [he, hkn] at hkp; cases hkp + · obtain ⟨pt, hkp, -⟩ := X.j.1.vs.pt q hq; rw [he, hkn] at hkp; cases hkp + · obtain ⟨hv0, hkp⟩ := X.j.2.2.2.obj q hq; rw [he, hkn] at hkp; cases hkp + have hVs : VsRelA Z.Pp Z.S1 Z.Lv Z.X Z.Y (L.mem s1) (L.ksb s1) σ1 := by + rw [hksb1] + exact (X.j.1.vs.of_spec (σ' := σ1) rfl hVs_kh).of_mem fun b hb => hwr_ne (hND.vs_al b hb) (fun e => hND.ne_vs (e ▸ hb)) + have hExt_kh : ∀ q, q ∈ Z.Pp ∨ q ∈ Z.X ∨ q ∈ Z.Y ∨ q ∈ Z.T2 → σ1.kheap (specPtr q) = σ.kheap (specPtr q) := fun q hq => by + apply updKSc_other + intro e + rcases hq with hq | hq | hq | hq + · obtain ⟨ap, hkp, -⟩ := X.j.1.vs.pool q hq; rw [e, hkn] at hkp; cases hkp + · obtain ⟨es, hkp⟩ := X.j.2.2.1.obj q hq; rw [e, hkn] at hkp; cases hkp + · obtain ⟨l1, hkp⟩ := X.j.2.2.2.obj q hq; rw [e, hkn] at hkp; cases hkp + · obtain ⟨pt, hkp, -⟩ := X.j.1.ext.s2pt q hq; rw [e, hkn] at hkp; cases hkp + have hExt : ExtVsRelA Z.Pp Z.S1 Z.X Z.Y Z.T2 Z.Sh2 (L.mem s1) σ1 := + (X.j.1.ext.of_spec (σ' := σ1) rfl (fun q hq => hExt_kh q (by rcases hq with h | h | h <;> simp [h]))).of_mem + fun b hb => hwr_ne (hND.ext_al b hb) (fun e => hND.ne_ext (e ▸ hb)) + have hJ : JAtZA (L := L) AR Z s1 a σ1 m := + { st := hSt, cdt := hCdt, sch := hSch, irq := hIrq, rest := hRest, ar := X.j.1.ar, + place := by rw [hksb1]; exact X.j.1.place, + thT := X.j.1.thT, csN := X.j.1.csN, + idleT := fun c hc => by rw [(hnode' c hc).2.1]; exact X.j.1.idleT c hc, + idleC := fun c hc => by rw [(hnode' c hc).2.2.2]; exact X.j.1.idleC c hc, + curT := fun c hc => by rw [(hnode' c hc).1]; exact X.j.1.curT c hc, + cscC := fun c hc => by rw [(hnode' c hc).2.2.1]; exact X.j.1.cscC c hc, + ksFit := by rw [hksb1]; exact X.j.1.ksFit, + scOffD := X.j.1.scOffD, + cv := fun y hy => by rw [hcap_sl y (Or.inl hy)]; exact X.j.1.cv y hy, + pml := fun y hy p mp asid hc => by rw [hcap_sl y (Or.inl hy)] at hc; exact X.j.1.pml y hy p mp asid hc, + vs := hVs, ext := hExt } + have hS2 : Stage2CoreA (L := L) Z s1 σ1 := + ⟨fun r hr => by rw [hExt_kh r (Or.inr (Or.inl hr))]; exact X.j.2.2.1.obj r hr, + fun p es h => by + by_cases hp : p = specPtr n + · subst hp; rw [updKSc_same] at h; cases h + · rw [updKSc_other _ hp] at h; exact X.j.2.2.1.back p es h, + fun y hy p l mp asid va hc => by rw [hcap_sl y (Or.inl hy)] at hc; exact X.j.2.2.1.cap y hy p l mp asid va hc⟩ + have hHV : HighVSpaceCoreA (L := L) Z s1 σ1 := + ⟨fun r hr => by rw [hExt_kh r (Or.inr (Or.inr (Or.inl hr)))]; exact X.j.2.2.2.obj r hr, + fun p l1 h => by + by_cases hp : p = specPtr n + · subst hp; rw [updKSc_same] at h; cases h + · rw [updKSc_other _ hp] at h; exact X.j.2.2.2.back p l1 h, + fun y hy p mp asid hc => by rw [hcap_sl y (Or.inl hy)] at hc; exact X.j.2.2.2.cap y hy p mp asid hc⟩ + have hTr : TreeA st OS Z.G.chains Z.G.par s1 := X.tr.keepO hos + have hty : TyKeepA σ σ1 := + TyKeepA.upd hkn (fun _ _ e => by cases e) (fun _ e => by cases e) + have hCar : CarryA Z (L.mem s1) σ1 := + ⟨X.car.1.of_capsA rfl (fun _ h => h) (fun _ h => h) (fun y hy => Or.inl (hcap_os y (hsub y hy))), + X.car.2.of_keepA hty.1 hty.2 fun y hy => by + rw [hcap_os y ((X.tr.lk.members y).1 hy).1]; exact CapFinEqA.refl _⟩ + have hF : FrameMemRelA Z OS Ub (L.ksb s1) (L.mem s1) σ1.machineState.underlyingMemory := by + refine ⟨X.fmem.aligned, + fun b hb => by rw [hwr_ne (X.fmem.aligned b hb) (fun e => hND.ne_ub (e ▸ hb))]; exact X.fmem.agree b hb, + fun y hy ptr sz r mp s2 asid va hc => by rw [hcap_os y hy] at hc; exact X.fmem.frames y hy ptr sz r mp s2 asid va hc, + X.fmem.disj_os, X.fmem.disj_slots, X.fmem.disj_tcb, X.fmem.disj_sc, by rw [hksb1]; exact X.fmem.disj_ks⟩ + have hrun_eq : ∀ t : U64, runVcpuA t = wa t 3672#u64 := fun _ => rfl + have hv1 : VcpuRelA (L := L) Z OS Ub s1 σ1 := by + have hvc_mem : ∀ v ∈ Z.V, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → + L.mem s1 (wa v o) = L.mem s (wa v o) := fun v hv o ho ho8 => + hwr_ne (aligned_wa (X.vcpu.place.vc_al hv) o ho8) (fun e => hND.ne_vc (e ▸ ⟨v, hv, o, ho, ho8, rfl⟩)) + have hvc_zero : ∀ v ∈ Z.V, L.mem s1 v = L.mem s v := fun v hv => by + have h := hvc_mem v hv 0#u64 (by decide) (by decide) + rwa [show wa v 0#u64 = v by apply UScalar.eq_of_val_eq; rw [wa_valP]; simp [UScalar.hBounds]] at h + exact { + core := ⟨fun v hv => by + obtain ⟨xv, hkv, hvt, hrc⟩ := X.vcpu.core.obj v hv + exact ⟨xv, by rw [updKSc_other _ (hne_v v hv).symm]; exact hkv, + by rw [hvc_zero v hv]; exact hvt, + by rw [hvc_mem v hv 24#u64 (by decide) (by decide)]; exact hrc⟩, + fun p xv h => by + by_cases hp : p = specPtr n + · subst hp; rw [updKSc_same] at h; cases h + · rw [updKSc_other _ hp] at h; exact X.vcpu.core.back p xv h, + fun t ht => by + obtain ⟨xt, hkt, htv, hrv⟩ := X.vcpu.core.tcb t ht + exact ⟨xt, by rw [updKSc_other _ (hne_t t ht).symm]; exact hkt, + by rw [hne_tcb_word t ht 3664#u64 (by decide) (by decide)]; exact htv, + by rw [hrun_eq, hne_tcb_word t ht 3672#u64 (by decide) (by decide), ← hrun_eq]; exact hrv⟩, + fun y hy v hc => by rw [hcap_sl y (Or.inl hy)] at hc; exact X.vcpu.core.cap y hy v hc⟩ + owner := fun v hv => by rw [hvc_mem v hv 8#u64 (by decide) (by decide)]; exact X.vcpu.owner v hv + vppi := fun v hv xv hkv => by + rw [updKSc_other _ (hne_v v hv).symm] at hkv + rw [hvc_mem v hv 16#u64 (by decide) (by decide)]; exact X.vcpu.vppi v hv xv hkv + runOff := fun v hv xv hkv => by + rw [updKSc_other _ (hne_v v hv).symm] at hkv + rw [hvc_mem v hv 32#u64 (by decide) (by decide)]; exact X.vcpu.runOff v hv xv hkv + runOut := fun v hv xv hkv => by + rw [updKSc_other _ (hne_v v hv).symm] at hkv + rw [hvc_mem v hv 40#u64 (by decide) (by decide)]; exact X.vcpu.runOut v hv xv hkv + regs := fun v hv xv hkv i hi => by + rw [updKSc_other _ (hne_v v hv).symm] at hkv + have hval := vcpuRegOffA_val hi + rw [show vcpuRegWA v i = wa v (vcpuRegOffA i) from rfl, + hvc_mem v hv (vcpuRegOffA i) (by omega) (by omega)] + exact X.vcpu.regs v hv xv hkv i hi + bind_v2t := fun v hv t htv hnz => by + rw [hvc_zero v hv] at htv + obtain ⟨htT, hback⟩ := X.vcpu.bind_v2t v hv t htv hnz + exact ⟨htT, by rw [hne_tcb_word t htT 3664#u64 (by decide) (by decide)]; exact hback⟩ + bind_t2v := fun t ht v hvt hnz => by + rw [hne_tcb_word t ht 3664#u64 (by decide) (by decide)] at hvt + obtain ⟨hvV, hback⟩ := X.vcpu.bind_t2v t ht v hvt hnz + exact ⟨hvV, by rw [hvc_zero v hvV]; exact hback⟩ + run_v2c := fun v hv c hrc hnz => by + rw [hvc_mem v hv 24#u64 (by decide) (by decide)] at hrc + obtain ⟨hcT, hback⟩ := X.vcpu.run_v2c v hv c hrc hnz + exact ⟨hcT, by rw [hrun_eq, hne_tcb_word c hcT 3672#u64 (by decide) (by decide), ← hrun_eq]; exact hback⟩ + run_c2v := fun c hc v hrv hnz => by + rw [hrun_eq, hne_tcb_word c hc 3672#u64 (by decide) (by decide), ← hrun_eq] at hrv + obtain ⟨hvV, hback⟩ := X.vcpu.run_c2v c hc v hrv hnz + exact ⟨hvV, by rw [hvc_mem v hvV 24#u64 (by decide) (by decide)]; exact hback⟩ + run_state := fun c hc hnz => by + rw [hrun_eq, hne_tcb_word c hc 3672#u64 (by decide) (by decide), ← hrun_eq] at hnz + rw [stA_eq, hne_tcb_word c hc 3328#u64 (by decide) (by decide), ← stA_eq] + exact X.vcpu.run_state c hc hnz + bind_run_disj := fun t ht hnz => by + rw [hne_tcb_word t ht 3664#u64 (by decide) (by decide)] at hnz + rw [hrun_eq, hne_tcb_word t ht 3672#u64 (by decide) (by decide), ← hrun_eq] + exact X.vcpu.bind_run_disj t ht hnz + idle_no_vcpu := fun c hc => by + rw [(hnode' c hc).2.1] + have hiT := hIdleT c hc + rw [hne_tcb_word _ hiT 3664#u64 (by decide) (by decide), + hrun_eq, hne_tcb_word _ hiT 3672#u64 (by decide) (by decide), ← hrun_eq] + exact X.vcpu.idle_no_vcpu c hc + live := fun v hv hor => by + rw [hvc_zero v hv, hvc_mem v hv 24#u64 (by decide) (by decide)] at hor + obtain ⟨y, hy, hcap⟩ := X.vcpu.live v hv hor + exact ⟨y, hy, by rw [hcap_os y hy]; exact hcap⟩ + arch := X.vcpu.arch + place := by rw [hksb1]; exact X.vcpu.place } + have hX1 : VcpuXferStateA (L := L) AR Z OS Ub m a s1 σ1 := + ⟨⟨hJ, hv1.core, hS2, hHV⟩, hTr, hCar, hv1, hF, + fun t ht x hkt r => by + rw [updKSc_other _ (hne_t t ht).symm] at hkt + have ⟨hKval, hreg_eq⟩ := regA_eq_wa_KA t r + have hlt := regIdxA_lt r + rw [hreg_eq, hne_tcb_word t ht _ (by omega) (by omega), ← hreg_eq] + exact X.regs t ht x hkt r⟩ + have hpres : VcpuRunPresA L Z OS s s1 σ σ1 := + ⟨hos, + fun t ht => ⟨by rw [inqA_eq, hne_tcb_word t ht 3592#u64 (by decide) (by decide), ← inqA_eq], + by rw [scA_eq, hne_tcb_word t ht 3464#u64 (by decide) (by decide), ← scA_eq]⟩, + fun c hc => hwr_ne (hND.ne_sc c hc).2.2.2.2.2 (hND.ne_sc c hc).2.2.1, + fun c hc => (hnode' c hc).2.1, + fun q hq => inUserFrame_updKScA hkn (fun _ h => by cases h) hq, + fun u hu h => by rwa [hrun_eq, hne_tcb_word u hu 3672#u64 (by decide) (by decide), ← hrun_eq] at h⟩ + exact ⟨hX1, hksb1, hcpu1, hpres⟩ + +/-- **S4 finish hook discharge (`ScFinishVcpuA.S4FinishHookA`)**: directly instantiates +`ScFinishVcpuA.S4FinishHookA` via `complete_run_refinesA` for any postcondition `Post` implied +by `VcpuXferStateA` and `VcpuRunPresA`. -/ +theorem vcpu_check_s4_hookA {AR : A → HwRootA → Prop} {Z : XSetsA} + {OS Ub : Set U64} {m : Fl4SpecA64.McState} {a : A} {s' : S} {σ' : State Unit} + (X : VcpuXferStateA (L := L) AR Z OS Ub m a s' σ') + (hP : VcpuPlatAgreeA ar) (hAK : VcpuArchKeepsA ar AR) (hVR : VcpuVmRootA st ar AR) + (hU : UbFootDisjA Z OS Ub) (hLD : TcbLinkDisjA Z (L.ksb s')) + (hUbUser : ∀ q ∈ Ub, inUserFrame (specPtr q) σ') + (hCs : ∀ c ∈ Z.Cs, c.val < 128) (hcpu : (L.cpu s').val < 128) + (hExt : ∀ b, VcPage Z.V b → ¬ ExtFootE Z.X Z.Y Z.T2 b) + (hSw : ∀ b, VcPage Z.V b → ∀ y, SlotWA Z.th y → + b ≠ y ∧ b ≠ wa y 8#u64 ∧ b ≠ wa y 16#u64 ∧ b ≠ wa y 24#u64) + (hfa : ∀ q, FootA (L := L) Z s' q → Aligned q) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hIdleT : ∀ c ∈ Z.Cs, L.mem s' (L.nodeAt s' c layout.node.IDLE_THREAD) ∈ Z.T) + (hksTcb : ∀ t ∈ Z.T, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → + ∀ o : U64, o.val < ksSizeA → wa t K ≠ wa (L.ksb s') o) + (hSlotTcb : ∀ t ∈ Z.T, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → + ∀ y, SlotWA Z.th y → wa t K ≠ y ∧ wa t K ≠ wa y 8#u64 ∧ wa t K ≠ wa y 16#u64 ∧ wa t K ≠ wa y 24#u64) + (hOS_tcb : ∀ t ∈ Z.T, ∀ K : U64, (K = 3664#u64 ∨ K = 3672#u64) → ∀ y ∈ OS, + wa t K ≠ y ∧ wa t K ≠ wa y 8#u64 ∧ wa t K ≠ wa y 16#u64 ∧ wa t K ≠ wa y 24#u64) + {ct : U64} (hct : ct ∈ Z.T) + (hBufNotS2 : ∀ p sz r d mp asid va, + capAtA (L.mem s') (sbWordA ct) ≠ .Frame p sz r d mp true asid va) + (hRunSched : L.mem s' (runVcpuA ct) ≠ 0#u64 → + L.mem s' (scA ct) ≠ 0#u64 ∧ L.rmax s' (L.mem s' (scA ct)) ≠ 0#u64 ∧ + (L.mem s' (inqA ct) &&& sched.Q_RELEASE) = 0#u64 ∧ + specPtr ct ∉ σ'.releaseQueue ∧ + Aligned (maxA (L.mem s' (scA ct))) ∧ stA ct ≠ maxA (L.mem s' (scA ct))) + (hNotRel : specPtr ct ∉ σ'.releaseQueue) + (hSlotSt : ∀ y, SlotWA Z.th y → stA ct ≠ y ∧ stA ct ≠ wa y 8#u64 ∧ stA ct ≠ wa y 16#u64 ∧ stA ct ≠ wa y 24#u64) + (hOS_st : ∀ y ∈ OS, stA ct ≠ y ∧ stA ct ≠ wa y 8#u64 ∧ stA ct ≠ wa y 16#u64 ∧ stA ct ≠ wa y 24#u64) + (hKs_st : ∀ o : U64, o.val < ksSizeA → stA ct ≠ wa (L.ksb s') o) + (hSc_st : ∀ c ∈ Z.C, stA ct ≠ wa c 24#u64 ∧ stA ct ≠ wa c 72#u64) + (hVs_st : ¬ VsFootA Z.Pp Z.S1 Z.Y (L.ksb s') (stA ct)) + (hExt_st : ¬ ExtFootE Z.X Z.Y Z.T2 (stA ct)) + (badge : U64) + (Post : S × A → State Unit → Prop) + (hPost : ∀ hw' r τ, + VcpuXferStateA (L := L) AR { Z with hw := hw' } OS Ub m r.2 r.1 τ → + VcpuRunPresA L Z OS s' r.1 σ' τ → Post r τ) : + S4FinishHookA (st := st) ar s' a σ' ct badge Post := by + unfold S4FinishHookA + refine WP.spec_mono (complete_run_refinesA X hP hAK hVR hU hLD hUbUser hCs hcpu hExt hSw hfa hsub + hIdleT hksTcb hSlotTcb hOS_tcb hct hBufNotS2 hRunSched hNotRel hSlotSt hOS_st hKs_st hSc_st + hVs_st hExt_st (stop := fl4_abi.consts.vcpu_run_stop.INTERRUPT) (stopSpec := .interrupt) + (by unfold fl4_abi.consts.vcpu_run_stop.INTERRUPT RunStop.word; rfl) badge 0#u64 0#u64) ?_ + rintro r ⟨hw', τ, hcr, X', -, -, -, -, -, -, hpres⟩ + exact ⟨τ, hcr, hPost hw' r τ X' hpres⟩ + +set_option maxHeartbeats 800000 in +/-- **`vcpu.check` refines `fl4VcpuCheck`** on `VcpuXferStateA` (covering all 4 branches: +non-`RunningVM`, `RunningVM` with no linked VCPU recovery, `RunningVM` with linked VCPU and +no active bound notification, and `RunningVM` with linked VCPU and active bound notification). -/ +theorem vcpu_check_refinesA {AR : A → HwRootA → Prop} {Z : XSetsA} + {OS Ub : Set U64} {m : Fl4SpecA64.McState} {a : A} {s : S} {σ : State Unit} + (X : VcpuXferStateA (L := L) AR Z OS Ub m a s σ) + (hP : VcpuPlatAgreeA ar) (hAK : VcpuArchKeepsA ar AR) (hVR : VcpuVmRootA st ar AR) + (hU : UbFootDisjA Z OS Ub) (hLD : TcbLinkDisjA Z (L.ksb s)) + (hUbUser : ∀ q ∈ Ub, inUserFrame (specPtr q) σ) + (hCs : ∀ c ∈ Z.Cs, c.val < 128) (hcpu : (L.cpu s).val < 128) + (hExt : ∀ b, VcPage Z.V b → ¬ ExtFootE Z.X Z.Y Z.T2 b) + (hSw : ∀ b, VcPage Z.V b → ∀ y, SlotWA Z.th y → + b ≠ y ∧ b ≠ wa y 8#u64 ∧ b ≠ wa y 16#u64 ∧ b ≠ wa y 24#u64) + (hfa : ∀ q, FootA (L := L) Z s q → Aligned q) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hIdleT : ∀ c ∈ Z.Cs, L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) ∈ Z.T) + (hksTcb : ∀ t ∈ Z.T, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → + ∀ o : U64, o.val < ksSizeA → wa t K ≠ wa (L.ksb s) o) + (hSlotTcb : ∀ t ∈ Z.T, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → + ∀ y, SlotWA Z.th y → wa t K ≠ y ∧ wa t K ≠ wa y 8#u64 ∧ wa t K ≠ wa y 16#u64 ∧ wa t K ≠ wa y 24#u64) + (hOS_tcb : ∀ t ∈ Z.T, ∀ K : U64, (K = 3664#u64 ∨ K = 3672#u64) → ∀ y ∈ OS, + wa t K ≠ y ∧ wa t K ≠ wa y 8#u64 ∧ wa t K ≠ wa y 16#u64 ∧ wa t K ≠ wa y 24#u64) + {ct : U64} (hct : ct ∈ Z.T) + (hBufNotS2 : ∀ p sz r d mp asid va, + capAtA (L.mem s) (sbWordA ct) ≠ .Frame p sz r d mp true asid va) + (hVmSched : L.mem s (stA ct) = sched.TS_RUNNING_VM → + L.mem s (scA ct) ≠ 0#u64 ∧ L.rmax s (L.mem s (scA ct)) ≠ 0#u64 ∧ + (L.mem s (inqA ct) &&& sched.Q_RELEASE) = 0#u64 ∧ + specPtr ct ∉ σ.releaseQueue ∧ + Aligned (maxA (L.mem s (scA ct))) ∧ stA ct ≠ maxA (L.mem s (scA ct))) + (hNotRel : specPtr ct ∉ σ.releaseQueue) + (hSlotSt : ∀ y, SlotWA Z.th y → stA ct ≠ y ∧ stA ct ≠ wa y 8#u64 ∧ stA ct ≠ wa y 16#u64 ∧ stA ct ≠ wa y 24#u64) + (hOS_st : ∀ y ∈ OS, stA ct ≠ y ∧ stA ct ≠ wa y 8#u64 ∧ stA ct ≠ wa y 16#u64 ∧ stA ct ≠ wa y 24#u64) + (hKs_st : ∀ o : U64, o.val < ksSizeA → stA ct ≠ wa (L.ksb s) o) + (hSc_st : ∀ c ∈ Z.C, stA ct ≠ wa c 24#u64 ∧ stA ct ≠ wa c 72#u64 ∧ stA ct ≠ maxA c ∧ Aligned (maxA c)) + (hSc_reg : ∀ c ∈ Z.C, Endp.regA ct layout.reg.FAULT_IP ≠ maxA c) + (hVs_st : ¬ VsFootA Z.Pp Z.S1 Z.Y (L.ksb s) (stA ct)) + (hExt_st : ¬ ExtFootE Z.X Z.Y Z.T2 (stA ct)) + (hNtfn : ∀ xct, σ.kheap (specPtr ct) = some (.tcb xct) → + L.mem s (stA ct) = sched.TS_RUNNING_VM → L.mem s (runVcpuA ct) ≠ 0#u64 → + FinishNotificationA (st := st) s σ ct xct ∧ + (∀ n, L.mem s (boundA ct) = n → n ≠ 0#u64 → NtfnStateWordDisjA Z OS Ub (L.ksb s) n)) : + vcpu.check st ar s a ct ⦃ res => + ∃ hw' σ', + fl4VcpuCheck (Z := Unit) (specPtr ct) σ = ({((), σ')}, False) ∧ + VcpuXferStateA (L := L) AR { Z with hw := hw' } OS Ub m res.2 res.1 σ' ∧ + L.ksb res.1 = L.ksb s ∧ L.cpu res.1 = L.cpu s ∧ + VcpuRunPresA L Z OS s res.1 σ σ' ⦄ := by + unfold vcpu.check + rw [eq_of_spec_fd (obj.Tcb.state.spec s ct), bind_ok] + obtain ⟨xct, hkc, hst_c, -⟩ := X.j.1.sch.tcb ct hct + obtain ⟨xct', hkc', -, hrv_c⟩ := X.vcpu.core.tcb ct hct + rw [hkc] at hkc'; cases hkc' + have hst_iff : L.mem s (stA ct) = sched.TS_RUNNING_VM ↔ xct.tcbState = .ext .runningVM := by + constructor + · intro h + have hv : tsCodeA xct.tcbState = 8 := by + rw [← hst_c, h]; unfold sched.TS_RUNNING_VM; rfl + cases hts : xct.tcbState with + | ext e => cases e; rfl + | _ => rw [hts] at hv; simp [tsCodeA] at hv + · intro h + apply UScalar.eq_of_val_eq + rw [hst_c, h]; unfold sched.TS_RUNNING_VM tsCodeA; rfl + by_cases hvm : L.mem s (stA ct) = sched.TS_RUNNING_VM + · rw [if_neg (by rw [bne_iff_ne]; exact not_not.2 hvm)] + have hvm_s : xct.tcbState = .ext .runningVM := hst_iff.1 hvm + have hrv_const : layout.tcb.RUN_VCPU = ok 3672#u64 := layout_tcb_run_vcpu_eqA + have hrun_eq : wa ct 3672#u64 = runVcpuA ct := by unfold runVcpuA; rfl + rw [hrv_const, bind_ok, lift_ok, bind_ok, hrun_eq, L.read_eq, bind_ok] + obtain ⟨hsc, hrm, hiq, hnr, hma, hmx⟩ := hVmSched hvm + by_cases hrv0 : L.mem s (runVcpuA ct) = 0#u64 + · -- Branch 2: RunningVM with runVcpu = 0 → copy NEXT_IP to FAULT_IP, set TS_RUNNING + rw [if_pos hrv0] + rw [eq_of_spec_fd (obj.Tcb.reg.spec s ct layout.reg.NEXT_IP), bind_ok] + set ip := L.mem s (Endp.regA ct layout.reg.NEXT_IP) + rw [eq_of_spec_fd (obj.Tcb.set_reg.spec s ct layout.reg.FAULT_IP ip), bind_ok] + have hreg_fip : Endp.regA ct layout.reg.FAULT_IP = Endp.regA ct (regIdxA .faultIP) := by + unfold layout.reg.FAULT_IP regIdxA; rfl + have hX1 : VcpuXferStateA (L := L) AR Z OS Ub m a (L.wr s (Endp.regA ct layout.reg.FAULT_IP) ip) + (ctxUpdA σ (specPtr ct) xct { xct.tcbArch.tcbContext with + userRegs := Function.update xct.tcbArch.tcbContext.userRegs .faultIP ip.bv }) := by + rw [hreg_fip] + exact X.tcb_reg_wr hU hCs hcpu hfa hsub hIdleT hksTcb hSlotTcb hct hkc .faultIP ip + set s1 := L.wr s (Endp.regA ct layout.reg.FAULT_IP) ip + set uc1 : UserContext := { xct.tcbArch.tcbContext with + userRegs := Function.update xct.tcbArch.tcbContext.userRegs .faultIP ip.bv } + set xct1 : Tcb := { xct with tcbArch := { xct.tcbArch with tcbContext := uc1 } } + set σ1 := ctxUpdA σ (specPtr ct) xct uc1 + have hksb1 : L.ksb s1 = L.ksb s := L.ksb_wr _ _ _ + have hcpu1 : L.cpu s1 = L.cpu s := L.cpu_wr _ _ _ + have hta := hU.tcb_al ct hct + have hal_t : Aligned ct := by unfold Aligned; have := hta.align; omega + obtain ⟨hKval, hreg_wa⟩ := regA_eq_wa_KA ct .faultIP + have hlt_fip := regIdxA_lt .faultIP + have hal_fip : Aligned (Endp.regA ct layout.reg.FAULT_IP) := by + rw [hreg_fip, hreg_wa]; exact aligned_wa hal_t _ (by omega) + have hne_fip_tcb : ∀ K : U64, 3328 ≤ K.val → K.val < 4096 → + Endp.regA ct layout.reg.FAULT_IP ≠ wa ct K := fun K hK1 hK2 => by + rw [hreg_fip, hreg_wa] + exact field_ne hta hta (by omega) hK2 (fun _ h => by omega) + have hrv1 : L.mem s1 (runVcpuA ct) = 0#u64 := by + rw [runVcpuA_eq, mem_wr_neA hal_fip (aligned_wa hal_t 3672#u64 (by decide)) (hne_fip_tcb 3672#u64 (by decide) (by decide)), ← runVcpuA_eq]; exact hrv0 + have hsc1 : L.mem s1 (scA ct) = L.mem s (scA ct) := by + rw [scA_eq, mem_wr_neA hal_fip (aligned_wa hal_t 3464#u64 (by decide)) (hne_fip_tcb 3464#u64 (by decide) (by decide)), ← scA_eq] + have hiq1 : L.mem s1 (inqA ct) = L.mem s (inqA ct) := by + rw [inqA_eq, mem_wr_neA hal_fip (aligned_wa hal_t 3592#u64 (by decide)) (hne_fip_tcb 3592#u64 (by decide) (by decide)), ← inqA_eq] + have hkc1 : σ1.kheap (specPtr ct) = some (.tcb xct1) := updKSc_same _ _ _ + have hrm1 : L.rmax s1 (L.mem s1 (scA ct)) = L.rmax s (L.mem s (scA ct)) := by + rw [hsc1] + have hscC := X.j.1.rest.scMem ct hct hsc + exact mem_wr_neA hal_fip hma (hSc_reg _ hscC) + have hfa1 : ∀ q, FootA (L := L) Z s1 q → Aligned q := fun q hq => + hfa q (footA_of_ksb_cpuA hksb1 hcpu1 hq) + have hIdleT1 : ∀ c ∈ Z.Cs, L.mem s1 (L.nodeAt s1 c layout.node.IDLE_THREAD) ∈ Z.T := fun c hc => by + obtain ⟨k, hk, heq⟩ := nodeAt_eq_wa_ksb_localA (L := L) s c layout.node.IDLE_THREAD (hCs c hc) (by unfold layout.node.IDLE_THREAD; decide) + rw [nodeAt_ksb hksb1] + rw [mem_wr_neA hal_fip (X.vcpu.place.node_al c layout.node.IDLE_THREAD (by unfold layout.node.IDLE_THREAD; decide)) + (by rw [hreg_fip, hreg_wa, heq]; exact hksTcb ct hct _ (by omega) (by omega) k hk)] + exact hIdleT c hc + refine WP.spec_bind (hX1.set_state_running_from_vm hU hCs (hcpu1 ▸ hcpu) hfa1 hsub hIdleT1 hct hkc1 hvm_s + hrv1 (hsc1 ▸ hsc) (hrm1 ▸ hrm) (hiq1 ▸ hiq) (hsc1 ▸ hma) (hsc1 ▸ hmx) hnr + hSlotSt hOS_st (hksb1 ▸ hKs_st) (fun c hc => ⟨(hSc_st c hc).1, (hSc_st c hc).2.1⟩) (hksb1 ▸ hVs_st) hExt_st) ?_ + rintro s2 ⟨rfl, σ2, hst2, hX2, rfl⟩ + rw [WP.spec_ok] + have hget_ip : ip.bv = xct.tcbArch.tcbContext.userRegs .nextIP := by + have h := X.regs ct hct xct hkc .nextIP + rwa [show regIdxA .nextIP = layout.reg.NEXT_IP by unfold regIdxA layout.reg.NEXT_IP layout.reg.ELR_EL1; rfl] at h + have hsp_chk : fl4VcpuCheck (Z := Unit) (specPtr ct) σ = ({((), stUpdA σ1 (specPtr ct) xct1 .running)}, False) := by + unfold fl4VcpuCheck + rw [nd_bind_oneSc (by unfold getThreadState; exact threadGet_eqSc _ hkc), hvm_s]; unfold NonDet.when; rw [if_pos rfl] + rw [nd_bind_oneSc (getRunVcpu_eqA hkc), hrv_c]; unfold optRefA; rw [if_pos hrv0]; dsimp only + have hnr_reg : nextInstructionRegister = .nextIP := rfl + have hfr_reg : faultRegister = .faultIP := rfl + rw [hnr_reg, hfr_reg] + have hf_eq : (do let ip0 ← getRegister .nextIP; setRegister .faultIP ip0) (archTcbContextGet xct.tcbArch) = + setRegister .faultIP ip.bv (archTcbContextGet xct.tcbArch) := by + rw [nd_bind_oneSc (m := getRegister .nextIP) rfl]; dsimp only [archTcbContextGet]; rw [← hget_ip] + have hfault_eq : asUser (Z := Unit) (specPtr ct) (do let ip0 ← getRegister .nextIP; setRegister .faultIP ip0) σ = + ({((), σ1)}, False) := by + have h1 : asUser (Z := Unit) (specPtr ct) (do let ip0 ← getRegister .nextIP; setRegister .faultIP ip0) σ = + asUser (Z := Unit) (specPtr ct) (setRegister .faultIP ip.bv) σ := by + unfold asUser + have hg : NonDet.getsThe (getTcb (Z := Unit) (specPtr ct)) σ = ({(xct, σ)}, False) := by + simp [NonDet.getsThe, getTcb, readObject, hkc, NonDet.assertOpt]; rfl + rw [nd_bind_oneSc hg, nd_bind_oneSc (show (pure (archTcbContextGet xct.tcbArch) : SMonad Unit UserContext) σ = _ from rfl), + hf_eq, nd_bind_oneSc hg, nd_bind_oneSc (show (pure (archTcbContextGet xct.tcbArch) : SMonad Unit UserContext) σ = _ from rfl)] + exact h1.trans (asUser_setReg_oneA hkc .faultIP ip.bv) + rw [nd_bind_oneSc hfault_eq]; exact hst2 + have hal_st : Aligned (stA ct) := by rw [stA_eq]; exact aligned_wa hal_t 3328#u64 (by decide) + have hpres1 : VcpuRunPresA L Z OS s s1 σ σ1 := by + refine ⟨fun y hy => ?_, fun t ht => ?_, fun c hc => ?_, fun c hc => ?_, + fun q hq => inUserFrame_updKScA hkc (fun _ h => by cases h) hq, fun u hu h => ?_⟩ + · have hya := hU.al_os y hy + obtain ⟨h0, h8, h16, h24⟩ := hU.tcb_offOs ct hct + (wa 256#u64 (core.num.U64.wrapping_mul (regIdxA .faultIP) 8#u64)) (by omega) (by omega) y hy + rw [← hreg_wa, ← hreg_fip] at h0 h8 h16 h24 + exact ⟨mem_wr_neA hal_fip hya h0, + mem_wr_neA hal_fip (aligned_wa hya 8#u64 (by decide)) h8, + mem_wr_neA hal_fip (aligned_wa hya 16#u64 (by decide)) h16, + mem_wr_neA hal_fip (aligned_wa hya 24#u64 (by decide)) h24⟩ + · have ht_al : Aligned t := by unfold Aligned; have := (hU.tcb_al t ht).align; omega + have hne_t : ∀ K : U64, 3328 ≤ K.val → K.val < 4096 → Endp.regA ct layout.reg.FAULT_IP ≠ wa t K := fun K hK1 hK2 => by + by_cases eq : ct = t + · subst eq; exact hne_fip_tcb K hK1 hK2 + · rw [hreg_fip, hreg_wa]; exact field_ne hta (hU.tcb_al t ht) (by omega) hK2 (fun h _ => eq h) + exact ⟨by rw [inqA_eq, mem_wr_neA hal_fip (aligned_wa ht_al 3592#u64 (by decide)) (hne_t 3592#u64 (by decide) (by decide)), ← inqA_eq], + by rw [scA_eq, mem_wr_neA hal_fip (aligned_wa ht_al 3464#u64 (by decide)) (hne_t 3464#u64 (by decide) (by decide)), ← scA_eq]⟩ + · exact mem_wr_neA hal_fip (hSc_st c hc).2.2.2 (hSc_reg c hc) + · obtain ⟨k, hk, heq⟩ := nodeAt_eq_wa_ksb_localA (L := L) s c layout.node.IDLE_THREAD (hCs c hc) (by unfold layout.node.IDLE_THREAD; decide) + rw [nodeAt_ksb hksb1] + exact mem_wr_neA hal_fip (X.vcpu.place.node_al c layout.node.IDLE_THREAD (by unfold layout.node.IDLE_THREAD; decide)) + (by rw [hreg_fip, hreg_wa, heq]; exact hksTcb ct hct _ (by omega) (by omega) k hk) + · have hu_al : Aligned u := by unfold Aligned; have := (hU.tcb_al u hu).align; omega + have hne_u : Endp.regA ct layout.reg.FAULT_IP ≠ wa u 3672#u64 := by + by_cases eq : ct = u + · subst eq; exact hne_fip_tcb 3672#u64 (by decide) (by decide) + · rw [hreg_fip, hreg_wa]; exact field_ne hta (hU.tcb_al u hu) (by omega) (by decide) (fun h _ => eq h) + rwa [runVcpuA_eq, mem_wr_neA hal_fip (aligned_wa hu_al 3672#u64 (by decide)) hne_u, ← runVcpuA_eq] at h + have hpres2 : VcpuRunPresA L Z OS s1 (L.wr s1 (stA ct) sched.TS_RUNNING) σ1 (stUpdA σ1 (specPtr ct) xct1 .running) := by + refine ⟨fun y hy => ?_, fun t ht => ?_, fun c hc => ?_, fun c hc => ?_, + fun q hq => inUserFrame_updKScA hkc1 (fun _ h => by cases h) hq, fun u hu h => ?_⟩ + · have hya := hU.al_os y hy + exact ⟨mem_wr_neA hal_st hya (hOS_st y hy).1, + mem_wr_neA hal_st (aligned_wa hya 8#u64 (by decide)) (hOS_st y hy).2.1, + mem_wr_neA hal_st (aligned_wa hya 16#u64 (by decide)) (hOS_st y hy).2.2.1, + mem_wr_neA hal_st (aligned_wa hya 24#u64 (by decide)) (hOS_st y hy).2.2.2⟩ + · have ht_al : Aligned t := by unfold Aligned; have := (hU.tcb_al t ht).align; omega + have hne_t : ∀ K : U64, 3384 ≤ K.val → K.val < 4096 → stA ct ≠ wa t K := fun K hK1 hK2 => by + rw [stA_eq] + by_cases eq : ct = t + · subst eq; exact field_ne hta hta (by decide) hK2 (fun _ h => by change (3328 : Nat) = K.val at h; omega) + · exact field_ne hta (hU.tcb_al t ht) (by decide) hK2 (fun h _ => eq h) + exact ⟨by rw [inqA_eq, mem_wr_neA hal_st (aligned_wa ht_al 3592#u64 (by decide)) (hne_t 3592#u64 (by decide) (by decide)), ← inqA_eq], + by rw [scA_eq, mem_wr_neA hal_st (aligned_wa ht_al 3464#u64 (by decide)) (hne_t 3464#u64 (by decide) (by decide)), ← scA_eq]⟩ + · exact mem_wr_neA hal_st (hSc_st c hc).2.2.2 (hSc_st c hc).2.2.1 + · obtain ⟨k, hk, heq⟩ := nodeAt_eq_wa_ksb_localA (L := L) s1 c layout.node.IDLE_THREAD (hCs c hc) (by unfold layout.node.IDLE_THREAD; decide) + rw [nodeAt_ksb (L.ksb_wr _ _ _)] + exact mem_wr_neA hal_st (by rw [nodeAt_ksb hksb1]; exact X.vcpu.place.node_al c layout.node.IDLE_THREAD (by unfold layout.node.IDLE_THREAD; decide)) + (by rw [heq, hksb1]; exact hKs_st k hk) + · have hu_al : Aligned u := by unfold Aligned; have := (hU.tcb_al u hu).align; omega + have hne_u : stA ct ≠ wa u 3672#u64 := by + rw [stA_eq] + by_cases eq : ct = u + · subst eq; exact field_ne hta hta (by decide) (by decide) (fun _ h => by change (3328 : Nat) = 3672 at h; omega) + · exact field_ne hta (hU.tcb_al u hu) (by decide) (by decide) (fun h _ => eq h) + rwa [runVcpuA_eq, mem_wr_neA hal_st (aligned_wa hu_al 3672#u64 (by decide)) hne_u, ← runVcpuA_eq] at h + exact ⟨Z.hw, _, hsp_chk, hX2, (L.ksb_wr _ _ _).trans hksb1, (L.cpu_wr _ _ _).trans hcpu1, hpres1.trans hpres2⟩ + · -- Branch 3 & 4: RunningVM with runVcpu ≠ 0 → check bound notification + rw [if_neg hrv0, eq_of_spec_fd (obj.Tcb.bound_ntfn.val s ct), bind_ok] + obtain ⟨hFN, hND_fn⟩ := hNtfn xct hkc hvm hrv0 + set n := L.mem s (boundA ct) + have hsp_pre : fl4VcpuCheck (Z := Unit) (specPtr ct) σ = (do + let nOpt ← getTcbObjRef Tcb.tcbBoundNotification (specPtr ct) + match nOpt with + | some n0 => do + let ntfn ← getNotification n0 + match ntfn.ntfnObj with + | .activeNtfn badge => do + setNotification n0 { ntfn with ntfnObj := .idleNtfn } + fl4CompleteRun (specPtr ct) .interrupt badge 0 0 + | _ => pure () + | none => pure ()) σ := by + unfold fl4VcpuCheck + rw [nd_bind_oneSc (by unfold getThreadState; exact threadGet_eqSc _ hkc), hvm_s]; unfold NonDet.when; rw [if_pos rfl] + rw [nd_bind_oneSc (getRunVcpu_eqA hkc), hrv_c]; unfold optRefA; rw [if_neg hrv0]; rfl + have hbound : getTcbObjRef (Z := Unit) Tcb.tcbBoundNotification (specPtr ct) σ = + ({(optRefA n, σ)}, False) := by + unfold getTcbObjRef; rw [threadGet_eqSc _ hkc, hFN.1] + by_cases hn0 : n = 0#u64 + · rw [if_neg (by rw [bne_iff_ne]; exact not_not.2 hn0), WP.spec_ok] + refine ⟨Z.hw, σ, ?_, X, rfl, rfl, VcpuRunPresA.refl L Z OS s σ⟩ + rw [hsp_pre, nd_bind_oneSc hbound]; unfold optRefA; rw [if_pos hn0]; rfl + · rw [if_pos (bne_iff_ne.2 hn0)] + obtain ⟨ntfn, hkn, hview⟩ := hFN.2 n rfl hn0 + rw [eq_of_spec_fd (obj.Ntfn.state.spec s n), bind_ok] + have hgetN : getNotification (Z := Unit) (specPtr n) σ = ({(ntfn, σ)}, False) := + getNotification_eq hkn + have hsp_n : fl4VcpuCheck (Z := Unit) (specPtr ct) σ = (do + match ntfn.ntfnObj with + | .activeNtfn badge => do + setNotification (specPtr n) { ntfn with ntfnObj := .idleNtfn } + fl4CompleteRun (specPtr ct) .interrupt badge 0 0 + | _ => pure ()) σ := by + rw [hsp_pre, nd_bind_oneSc hbound]; unfold optRefA; rw [if_neg hn0]; dsimp only + change (getNotification (specPtr n) >>= _) σ = _ + rw [nd_bind_oneSc hgetN] + cases hno : ntfn.ntfnObj with + | idleNtfn => + rw [hno] at hview; rw [if_neg hview, WP.spec_ok] + exact ⟨Z.hw, σ, by rw [hsp_n, hno]; rfl, X, rfl, rfl, VcpuRunPresA.refl L Z OS s σ⟩ + | waitingNtfn q => + rw [hno] at hview; rw [if_neg hview, WP.spec_ok] + exact ⟨Z.hw, σ, by rw [hsp_n, hno]; rfl, X, rfl, rfl, VcpuRunPresA.refl L Z OS s σ⟩ + | activeNtfn b => + rw [hno] at hview + rw [if_pos hview.1, eq_of_spec_fd (obj.Ntfn.badge.val s n), bind_ok] + set badge := L.mem s (ntfnBdA n) + rw [eq_of_spec_fd (obj.Ntfn.set_state.val (st := st) s n obj.NTFN_IDLE), bind_ok] + have hND := hND_fn n rfl hn0 + obtain ⟨hX1, hksb1, hcpu1, hpres1⟩ := X.ntfn_set_idle hU hCs hcpu hfa hsub hIdleT hkn hND + set s1 := L.wr s (epTSA n) ((L.mem s (epTSA n) &&& ~~~3#u64) ||| (obj.NTFN_IDLE &&& 3#u64)) + set σ1 := updKSc σ (specPtr n) (.notification { ntfn with ntfnObj := .idleNtfn }) + have hsetN : setNotification (Z := Unit) (specPtr n) { ntfn with ntfnObj := .idleNtfn } σ = + ({((), σ1)}, False) := setNotification_eq _ hkn + have hfa1 : ∀ q, FootA (L := L) Z s1 q → Aligned q := fun q hq => + hfa q (footA_of_ksb_cpuA hksb1 hcpu1 hq) + have hBuf1 : ∀ p sz r d mp asid va, + capAtA (L.mem s1) (sbWordA ct) ≠ .Frame p sz r d mp true asid va := by + have hta := hU.tcb_al ct hct + have hal_t : Aligned ct := by unfold Aligned; have := hta.align; omega + have heq : capAtA (L.mem s1) (sbWordA ct) = capAtA (L.mem s) (sbWordA ct) := by + have hsb64 : sbWordA ct = wa ct 64#u64 := by + unfold sbWordA layout.tcb.SLOT_BUFFER layout.cte.SIZE; rfl + have hsb72 : wa (wa ct 64#u64) 8#u64 = wa ct 72#u64 := by + apply UScalar.eq_of_val_eq + rw [wa_valP, wa_valP, wa_valP, Nat.add_mod, Nat.mod_mod, ← Nat.add_mod, Nat.add_assoc] + rfl + unfold capAtA; rw [hsb64, hsb72] + rw [mem_wr_neA hND.al (aligned_wa hal_t 64#u64 (by decide)) (hND.ne_tcb ct hct 64#u64 (by decide)), + mem_wr_neA hND.al (aligned_wa hal_t 72#u64 (by decide)) (hND.ne_tcb ct hct 72#u64 (by decide))] + rw [heq]; exact hBufNotS2 + have hRun1 : L.mem s1 (runVcpuA ct) ≠ 0#u64 → + L.mem s1 (scA ct) ≠ 0#u64 ∧ L.rmax s1 (L.mem s1 (scA ct)) ≠ 0#u64 ∧ + (L.mem s1 (inqA ct) &&& sched.Q_RELEASE) = 0#u64 ∧ + specPtr ct ∉ σ1.releaseQueue ∧ + Aligned (maxA (L.mem s1 (scA ct))) ∧ stA ct ≠ maxA (L.mem s1 (scA ct)) := fun _ => by + rw [(hpres1.tcb_meta ct hct).1, (hpres1.tcb_meta ct hct).2] + have hscC := X.j.1.rest.scMem ct hct hsc + refine ⟨hsc, ?_, hiq, hnr, hma, hmx⟩ + change L.mem s1 (maxA (L.mem s (scA ct))) ≠ 0#u64 + rw [hpres1.sc_max _ hscC]; exact hrm + refine WP.spec_mono (complete_run_refinesA hX1 hP hAK hVR hU (hksb1 ▸ hLD) + (fun q hq => hpres1.user_frame (specPtr q) (hUbUser q hq)) hCs (hcpu1 ▸ hcpu) + hExt hSw hfa1 hsub (fun c hc => by rw [hpres1.idle c hc]; exact hIdleT c hc) + (hksb1 ▸ hksTcb) hSlotTcb hOS_tcb hct hBuf1 hRun1 hnr hSlotSt hOS_st + (hksb1 ▸ hKs_st) (fun c hc => ⟨(hSc_st c hc).1, (hSc_st c hc).2.1⟩) (hksb1 ▸ hVs_st) hExt_st + (stop := fl4_abi.consts.vcpu_run_stop.INTERRUPT) (stopSpec := .interrupt) + (by unfold fl4_abi.consts.vcpu_run_stop.INTERRUPT RunStop.word; rfl) badge 0#u64 0#u64) ?_ + rintro res ⟨hw', σ2, hcr, hX2, hksb2, hcpu2, -, -, -, -, hpres2⟩ + refine ⟨hw', σ2, ?_, hX2, hksb2.trans hksb1, hcpu2.trans hcpu1, hpres1.trans hpres2⟩ + rw [hsp_n, hno] + change (setNotification (specPtr n) { ntfn with ntfnObj := .idleNtfn } >>= + fun _ => fl4CompleteRun (specPtr ct) .interrupt b 0 0) σ = _ + rw [nd_bind_oneSc hsetN, ← hview.2]; exact hcr + · -- Branch 1: not RunningVM → no-op + rw [if_pos (bne_iff_ne.2 hvm), WP.spec_ok] + refine ⟨Z.hw, σ, ?_, X, rfl, rfl, VcpuRunPresA.refl L Z OS s σ⟩ + unfold fl4VcpuCheck + rw [nd_bind_oneSc (by unfold getThreadState; exact threadGet_eqSc _ hkc)] + unfold NonDet.when + rw [if_neg (fun h => hvm (hst_iff.2 h))]; rfl + +end + +end Fl4ProofsA64.P3.Vcpu diff --git a/proofs/Fl4ProofsA64/P3/VcpuDelCommA.lean b/proofs/Fl4ProofsA64/P3/VcpuDelCommA.lean new file mode 100644 index 000000000..35bc67e75 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/VcpuDelCommA.lean @@ -0,0 +1,1474 @@ +/- +P3 (aarch64, A64-P3 S4 VCPU delete-hooks): **Spec commutation (`fl4Dissociate_comm_tcbFinA`) +and `dissociateVcpuTcb` / `vcpuDisable none` machine-state characterization**. + +Handles all branches of `σ.archState.armCurrentVcpu` (including `some (v, true)` via +`VsUnmapA.machineOpLift_okA`, with no `armCurrentVcpu = none` premise): +- `vcpuDisableNoneMopA_comm`: `vcpuDisable none` is total (`NdOkA`), preserves + `underlyingMemory`, `timeState`, and `lastMachineTime`, and commutes with + `underlyingMemory` updates. +- `dissociateVcpuTcb_mem_iffA`: exact forward existence & inverse characterization of + `((), σ1) ∈ (dissociateVcpuTcb (Z := Unit) v t σ).1`. +- `fl4Dissociate_comm_tcbFinA`: moves `dissociateVcpuTcb v t` from before + `unbindNotification t >> unbindFromSc t >> suspend t` to after it (matching + `fl4FinaliseCap (.threadCap t) true`'s `prepareThreadDelete t` position). +-/ +import Fl4ProofsA64.P3.VsUnmapA +import Fl4ProofsA64.P3.Stage3SchedValidA +import Fl4ProofsA64.P3.VcpuSpecA +import Fl4ProofsA64.P3.VcpuPremisesA + +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3.Vcpu + +open Fl4Spec Fl4SpecA64 Fl4ProofsA64.P3 + +noncomputable section + +/-! ## Section 1: `vcpuDisable none` and `dissociateVcpuTcb` -/ + +private theorem nd_bind_oneV {σ α β : Type} {m : NonDet σ α} {s s' : σ} {a : α} + (h : m s = ({(a, s')}, False)) (f : α → NonDet σ β) : + (NonDet.bind m f) s = f a s' := by + simp [NonDet.bind, h] + +private theorem nd_bind_mem_iffA {σ α β : Type} {m : NonDet σ α} {f : α → NonDet σ β} + {s s' : σ} {b : β} : + (b, s') ∈ ((m >>= f) s).1 ↔ ∃ a sm, (a, sm) ∈ (m s).1 ∧ (b, s') ∈ (f a sm).1 := by + change (b, s') ∈ {r | ∃ x ∈ (m s).1, r ∈ (f x.1 x.2).1} ↔ _ + simp only [Set.mem_setOf_eq, Prod.exists] + +private theorem nd_pure_mem_iffA {σ α : Type} {a b : α} {s s' : σ} : + (b, s') ∈ ((pure a : NonDet σ α) s).1 ↔ b = a ∧ s' = s := by + simp [Pure.pure, NonDet.pure, Set.mem_singleton_iff, Prod.mk.injEq] + +private theorem nd_gets_mem_iffA {σ α : Type} {f : σ → α} {a : α} {s s' : σ} : + (a, s') ∈ (NonDet.gets f s).1 ↔ a = f s ∧ s' = s := by + simp [NonDet.gets, Set.mem_singleton_iff, Prod.mk.injEq] + +private theorem nd_bind_getsV {σ α β : Type} (f : σ → α) (k : α → NonDet σ β) (s : σ) : + (NonDet.gets f >>= k) s = k (f s) s := by + simp [Bind.bind, NonDet.bind, NonDet.gets] + +private theorem nd_get_mem_iffA {σ : Type} {a s s' : σ} : + (a, s') ∈ (NonDet.get s).1 ↔ a = s ∧ s' = s := by + simp [NonDet.get, Set.mem_singleton_iff, Prod.mk.injEq] + +private theorem nd_put_mem_iffA {σ : Type} {s0 s s' : σ} {u : Unit} : + (u, s') ∈ (NonDet.put s0 s).1 ↔ s' = s0 := by + cases u; simp [NonDet.put, Set.mem_singleton_iff, Prod.mk.injEq] + +private theorem nd_modify_mem_iffA {σ : Type} {f : σ → σ} {u : Unit} {s s' : σ} : + (u, s') ∈ (NonDet.modify f s).1 ↔ s' = f s := by + cases u; simp [NonDet.modify, Set.mem_singleton_iff, Prod.mk.injEq] + +private theorem nd_selectF_mem_iffA {σ α : Type} {r : Set α × Prop} {a : α} {s s' : σ} : + (a, s') ∈ (NonDet.selectF r s).1 ↔ a ∈ r.1 ∧ s' = s := by + simp [NonDet.selectF] + +private theorem nd_assert_mem_iffA {σ : Type} {p : Prop} {u : Unit} {s s' : σ} : + (u, s') ∈ (NonDet.assert p s).1 ↔ p ∧ s' = s := by + cases u; simp [NonDet.assert, Prod.mk.injEq] + +private theorem asUser_det_eqV {α : Type} {σ : State Unit} {t : ObjRef} {x : Tcb} + {f : UserMonad α} {a : α} {uc' : UserContext} + (hkt : σ.kheap t = some (.tcb x)) + (hf : f (archTcbContextGet x.tcbArch) = ({(a, uc')}, False)) : + asUser (Z := Unit) t f σ = + ({(a, { σ with kheap := Function.update σ.kheap t (some (.tcb { x with tcbArch := archTcbContextSet uc' x.tcbArch })) })}, False) := by + unfold asUser + have hgt : NonDet.getsThe (getTcb (Z := Unit) t) σ = ({(x, σ)}, False) := by + unfold NonDet.getsThe getTcb readObject; rw [hkt]; rfl + change (NonDet.bind (NonDet.getsThe (getTcb t)) _) σ = _ + rw [nd_bind_oneV hgt] + have hp1 : ∀ {γ : Type} (y : γ) (k : γ → SMonad Unit α), ((pure y : SMonad Unit γ) >>= k) σ = k y σ := + fun y k => by simp [Bind.bind, NonDet.bind, Pure.pure, NonDet.pure] + rw [hp1] + have hsf : NonDet.selectF (f (archTcbContextGet x.tcbArch)) σ = ({((a, uc'), σ)}, False) := by + unfold NonDet.selectF; rw [hf]; ext ⟨p1, p2⟩ + · simp [Prod.mk.injEq] + · rfl + change (NonDet.bind (NonDet.selectF _) _) σ = _ + rw [nd_bind_oneV hsf] + dsimp only + rw [hp1] + have hso : setObject (Z := Unit) t (.tcb { x with tcbArch := archTcbContextSet uc' x.tcbArch }) σ = + ({((), { σ with kheap := Function.update σ.kheap t (some (.tcb { x with tcbArch := archTcbContextSet uc' x.tcbArch })) })}, False) := by + unfold setObject getObject readObject + have hgo : NonDet.getsThe (fun s : State Unit => s.kheap t) σ = ({(.tcb x, σ)}, False) := by + unfold NonDet.getsThe; dsimp only; rw [hkt]; rfl + change (NonDet.bind (NonDet.getsThe _) _) σ = _ + rw [nd_bind_oneV hgo] + have hass : (NonDet.assert (aType (.tcb x) = aType (.tcb { x with tcbArch := archTcbContextSet uc' x.tcbArch })) : SMonad Unit Unit) σ = ({((), σ)}, False) := by + simp [NonDet.assert, aType] + change (NonDet.bind (NonDet.assert _) _) σ = _ + rw [nd_bind_oneV hass] + change (NonDet.bind NonDet.get _) σ = _ + rw [nd_bind_oneV (m := NonDet.get) rfl]; rfl + change (NonDet.bind (setObject _ _) _) σ = _ + rw [nd_bind_oneV hso]; rfl + +/-- Machine-state relation kept by `vcpuDisable none`: `underlyingMemory`, `timeState`, +and `lastMachineTime` are unchanged. -/ +def MsKeepUmTimeA (ms ms' : MachineState) : Prop := + ms'.underlyingMemory = ms.underlyingMemory ∧ + ms'.timeState = ms.timeState ∧ + ms'.lastMachineTime = ms.lastMachineTime + +theorem msKeepUmTimeA_refl (ms : MachineState) : MsKeepUmTimeA ms ms := ⟨rfl, rfl, rfl⟩ + +theorem msKeepUmTimeA_trans (a b c : MachineState) + (h1 : MsKeepUmTimeA a b) (h2 : MsKeepUmTimeA b c) : MsKeepUmTimeA a c := + ⟨h2.1.trans h1.1, h2.2.1.trans h1.2.1, h2.2.2.trans h1.2.2⟩ + +/-- A machine operation that is total, preserves `underlyingMemory`/`timeState`/`lastMachineTime`, +and commutes with arbitrary `underlyingMemory` updates. -/ +structure MopCommUmA (mop : MachineMonad Unit) : Prop where + ok : NdOkA MsKeepUmTimeA mop + comm : ∀ ms ms' um, ((), ms') ∈ (mop ms).1 → + ((), { ms' with underlyingMemory := um }) ∈ (mop { ms with underlyingMemory := um }).1 + +private theorem mopCommUmA_pure : MopCommUmA (pure ()) where + ok := NdOkA.pure msKeepUmTimeA_refl () + comm := fun _ _ _ h => by + have heq := (Prod.mk.inj (Set.mem_singleton_iff.1 h)).2 + subst heq; exact Set.mem_singleton _ + +private theorem mopCommUmA_bind {m1 m2 : MachineMonad Unit} + (h1 : MopCommUmA m1) (h2 : MopCommUmA m2) : MopCommUmA (do m1; m2) where + ok := NdOkA.bind msKeepUmTimeA_trans h1.ok (fun _ => h2.ok) + comm := fun ms ms' um h => by + simp only [Bind.bind, NonDet.bind, Set.mem_iUnion] at h ⊢ + rcases h with ⟨⟨⟨⟩, ms1⟩, hm1, hm2⟩ + exact ⟨((), { ms1 with underlyingMemory := um }), h1.comm ms ms1 um hm1, h2.comm ms1 ms' um hm2⟩ + +private theorem mopCommUmA_lift (g : MachineRestMonad Unit) : MopCommUmA (machineOpLift g) where + ok := by + unfold machineOpLift machineRestLift + refine NdOkA.bind msKeepUmTimeA_trans (NdOkA.gets msKeepUmTimeA_refl _) fun mr => + NdOkA.bind msKeepUmTimeA_trans (NdOkA.selectF msKeepUmTimeA_refl ((ignoreFailure_okA g) mr).1 + ((ignoreFailure_okA g) mr).2.1) fun x => ?_ + rcases x with ⟨r, mr'⟩ + exact NdOkA.bind msKeepUmTimeA_trans (NdOkA.modify fun _ => ⟨rfl, rfl, rfl⟩) fun _ => + NdOkA.pure msKeepUmTimeA_refl r + comm := fun ms ms' um h => by + unfold machineOpLift machineRestLift at h ⊢ + simp only [nd_bind_mem_iffA, nd_gets_mem_iffA, nd_selectF_mem_iffA, nd_modify_mem_iffA, + nd_pure_mem_iffA] at h ⊢ + rcases h with ⟨mr, s1, ⟨rfl, rfl⟩, ⟨⟨⟩, mr'⟩, s2, ⟨hmem, rfl⟩, ⟨⟩, s3, rfl, _, rfl⟩ + exact ⟨s2.machineStateRest, { s2 with underlyingMemory := um }, ⟨rfl, rfl⟩, + ((), mr'), { s2 with underlyingMemory := um }, ⟨hmem, rfl⟩, + (), { s2 with underlyingMemory := um, machineStateRest := mr' }, rfl, trivial, rfl⟩ + +private theorem mopCommUmA_modify_keep (f : MachineState → MachineState) + (hok : ∀ ms, MsKeepUmTimeA ms (f ms)) + (hcomm : ∀ ms um, { f ms with underlyingMemory := um } = f { ms with underlyingMemory := um }) : + MopCommUmA (NonDet.modify f) where + ok := NdOkA.modify hok + comm := fun ms _ um h => by + simp only [NonDet.modify, Set.mem_singleton_iff] at h ⊢ + rcases Prod.mk.inj h with ⟨_, rfl⟩ + exact Prod.ext rfl (hcomm ms um) + +private theorem mopCommUmA_mask (m : Bool) (irq : Irq) : MopCommUmA (maskInterrupt m irq) := + mopCommUmA_modify_keep _ (fun _ => ⟨rfl, rfl, rfl⟩) (fun _ _ => rfl) + +/-- Combined machine operation executed by `vcpuDisable none`. -/ +def vcpuDisableNoneMopA : MachineMonad Unit := do + dsb + setGicVcpuCtrlHcr 0 + dsb + setSCTLR sctlrDefault + writeTCR tcrEL1Native + setHCR hcrNative + isb + enableFpuEL01 + writeVCPUHardwareReg .vcpuRegCNTV_CTL 0 + checkExportArchTimer + maskInterrupt true irqVTimerEvent + +theorem vcpuDisableNoneMopA_comm : MopCommUmA vcpuDisableNoneMopA := by + unfold vcpuDisableNoneMopA dsb setGicVcpuCtrlHcr setSCTLR writeTCR setHCR isb + enableFpuEL01 writeVCPUHardwareReg checkExportArchTimer + refine mopCommUmA_bind (mopCommUmA_lift _) ?_ + refine mopCommUmA_bind (mopCommUmA_bind (mopCommUmA_modify_keep _ (fun _ => ⟨rfl, rfl, rfl⟩) (fun _ _ => rfl)) (mopCommUmA_lift _)) ?_ + refine mopCommUmA_bind (mopCommUmA_lift _) ?_ + refine mopCommUmA_bind (mopCommUmA_bind (mopCommUmA_modify_keep _ (fun _ => ⟨rfl, rfl, rfl⟩) (fun _ _ => rfl)) (mopCommUmA_lift _)) ?_ + refine mopCommUmA_bind (mopCommUmA_bind (mopCommUmA_modify_keep _ (fun _ => ⟨rfl, rfl, rfl⟩) (fun _ _ => rfl)) (mopCommUmA_lift _)) ?_ + refine mopCommUmA_bind (mopCommUmA_lift _) ?_ + refine mopCommUmA_bind (mopCommUmA_lift _) ?_ + refine mopCommUmA_bind (mopCommUmA_bind (mopCommUmA_modify_keep _ (fun _ => ⟨rfl, rfl, rfl⟩) (fun _ _ => rfl)) (mopCommUmA_lift _)) ?_ + refine mopCommUmA_bind (mopCommUmA_bind (mopCommUmA_modify_keep _ (fun _ => ⟨rfl, rfl, rfl⟩) (fun _ _ => rfl)) (mopCommUmA_lift _)) ?_ + refine mopCommUmA_bind (mopCommUmA_bind (mopCommUmA_modify_keep _ (fun _ => ⟨rfl, rfl, rfl⟩) (fun _ _ => rfl)) (mopCommUmA_lift _)) ?_ + exact mopCommUmA_mask true irqVTimerEvent + +theorem doMachineOp_mem_iffA {mop : MachineMonad Unit} {σ τ : State Unit} : + ((), τ) ∈ (doMachineOp (Z := Unit) mop σ).1 ↔ + ∃ ms', ((), ms') ∈ (mop σ.machineState).1 ∧ τ = { σ with machineState := ms' } := by + unfold doMachineOp + simp only [nd_bind_mem_iffA, nd_gets_mem_iffA, nd_selectF_mem_iffA, nd_modify_mem_iffA, + nd_pure_mem_iffA] + constructor + · rintro ⟨ms0, s0, ⟨rfl, rfl⟩, ⟨⟨⟩, ms'⟩, s1, ⟨hm, rfl⟩, ⟨⟩, s2, rfl, _, rfl⟩ + exact ⟨ms', hm, rfl⟩ + · rintro ⟨ms', hm, rfl⟩ + exact ⟨σ.machineState, σ, ⟨rfl, rfl⟩, ((), ms'), σ, ⟨hm, rfl⟩, + (), { σ with machineState := ms' }, rfl, trivial, rfl⟩ + +theorem doMachineOp_fail_iffA {mop : MachineMonad Unit} {σ : State Unit} : + (doMachineOp (Z := Unit) mop σ).2 ↔ (mop σ.machineState).2 := by + unfold doMachineOp + simp only [Bind.bind, NonDet.bind, NonDet.gets, NonDet.selectF, NonDet.modify, NonDet.pure, + Pure.pure, Set.mem_singleton_iff, Prod.mk.injEq, exists_eq_left, false_or, or_false] + constructor + · rintro (h | ⟨⟨_, _⟩, _, h⟩) + · exact h + · exact False.elim h + · intro h; exact Or.inl h + +private theorem doMachineOp_bindA {m1 m2 : MachineMonad Unit} (σ : State Unit) : + (do doMachineOp (Z := Unit) m1; doMachineOp (Z := Unit) m2) σ = + doMachineOp (Z := Unit) (do m1; m2) σ := by + ext x + · rcases x with ⟨⟨⟩, τ⟩ + rw [doMachineOp_mem_iffA] + simp only [Bind.bind, NonDet.bind, Set.mem_iUnion] + constructor + · rintro ⟨⟨⟨⟩, s1⟩, h1, h2⟩ + obtain ⟨ms1, hm1, rfl⟩ := doMachineOp_mem_iffA.1 h1 + obtain ⟨ms2, hm2, rfl⟩ := doMachineOp_mem_iffA.1 h2 + exact ⟨ms2, ⟨((), ms1), hm1, hm2⟩, rfl⟩ + · rintro ⟨ms2, ⟨⟨⟨⟩, ms1⟩, hm1, hm2⟩, rfl⟩ + exact ⟨((), { σ with machineState := ms1 }), + doMachineOp_mem_iffA.2 ⟨ms1, hm1, rfl⟩, + doMachineOp_mem_iffA.2 ⟨ms2, hm2, rfl⟩⟩ + · rw [doMachineOp_fail_iffA] + simp only [Bind.bind, NonDet.bind] + rw [doMachineOp_fail_iffA] + constructor + · rintro (h | ⟨⟨⟨⟩, s1⟩, h1, h2⟩) + · exact Or.inl h + · obtain ⟨ms1, hm1, rfl⟩ := doMachineOp_mem_iffA.1 h1 + exact Or.inr ⟨((), ms1), hm1, doMachineOp_fail_iffA.1 h2⟩ + · rintro (h | ⟨⟨⟨⟩, ms1⟩, hm1, h2⟩) + · exact Or.inl h + · exact Or.inr ⟨((), { σ with machineState := ms1 }), + doMachineOp_mem_iffA.2 ⟨ms1, hm1, rfl⟩, doMachineOp_fail_iffA.2 h2⟩ + +private theorem nd_bind_pure_leftA {σ α β : Type} (x : α) (f : α → NonDet σ β) (s : σ) : + ((pure x : NonDet σ α) >>= f) s = f x s := by + simp [Bind.bind, NonDet.bind, Pure.pure, NonDet.pure] + +private theorem nd_bind_assocA {σ α β γ : Type} (m : NonDet σ α) (f : α → NonDet σ β) + (g : β → NonDet σ γ) (s : σ) : + ((m >>= f) >>= g) s = (m >>= fun a => f a >>= g) s := by + ext + · simp only [Bind.bind, NonDet.bind, Set.mem_iUnion]; constructor + · rintro ⟨⟨b, s2⟩, ⟨⟨a, s1⟩, hm, hf⟩, hg⟩; exact ⟨(a, s1), hm, ⟨(b, s2), hf, hg⟩⟩ + · rintro ⟨⟨a, s1⟩, hm, ⟨⟨b, s2⟩, hf, hg⟩⟩; exact ⟨(b, s2), ⟨(a, s1), hm, hf⟩, hg⟩ + · simp only [Bind.bind, NonDet.bind, Set.mem_iUnion]; constructor + · rintro ((h | ⟨⟨a, s1⟩, hm, hf⟩) | ⟨⟨b, s2⟩, ⟨⟨a, s1⟩, hm, hf⟩, hg⟩) + · exact Or.inl h + · exact Or.inr ⟨(a, s1), hm, Or.inl hf⟩ + · exact Or.inr ⟨(a, s1), hm, Or.inr ⟨(b, s2), hf, hg⟩⟩ + · rintro (h | ⟨⟨a, s1⟩, hm, (hf | ⟨⟨b, s2⟩, hf, hg⟩)⟩) + · exact Or.inl (Or.inl h) + · exact Or.inl (Or.inr ⟨(a, s1), hm, hf⟩) + · exact Or.inr ⟨(b, s2), ⟨(a, s1), hm, hf⟩, hg⟩ + +private theorem nd_bind_assoc_funA {σ α β γ : Type} (m : NonDet σ α) (f : α → NonDet σ β) + (g : β → NonDet σ γ) : + ((m >>= f) >>= g) = (m >>= fun a => f a >>= g) := + funext (nd_bind_assocA m f g) + +private theorem nd_bind_congr_rightA {σ α β : Type} (m : NonDet σ α) (f g : α → NonDet σ β) + (h : ∀ a s, f a s = g a s) (s : σ) : (m >>= f) s = (m >>= g) s := by + have hfg : f = g := funext fun a => funext fun s' => h a s' + rw [hfg] + +private theorem doMachineOp_bind_contA {m1 m2 : MachineMonad Unit} (k : Unit → SMonad Unit Unit) + (s : State Unit) : + (doMachineOp (Z := Unit) m1 >>= fun _ => doMachineOp (Z := Unit) m2 >>= k) s = + (doMachineOp (Z := Unit) (m1 >>= fun _ => m2) >>= k) s := by + rw [← nd_bind_assocA] + have h : (doMachineOp (Z := Unit) m1 >>= fun _ => doMachineOp (Z := Unit) m2) s = + doMachineOp (Z := Unit) (m1 >>= fun _ => m2) s := doMachineOp_bindA s + change NonDet.bind (doMachineOp (Z := Unit) m1 >>= fun _ => doMachineOp (Z := Unit) m2) k s = + NonDet.bind (doMachineOp (Z := Unit) (m1 >>= fun _ => m2)) k s + dsimp only [NonDet.bind] + rw [h] + +theorem vcpuDisable_none_eqA (σ : State Unit) : + vcpuDisable (Z := Unit) none σ = doMachineOp (Z := Unit) vcpuDisableNoneMopA σ := by + unfold vcpuDisable disableVirtTimer vcpuDisableNoneMopA + dsimp only + have hpure : ∀ (k : Unit → SMonad Unit Unit) (s : State Unit), + (NonDet.pure () >>= k) s = k () s := fun k s => by simp [Bind.bind, NonDet.bind, NonDet.pure] + rw [nd_bind_congr_rightA _ _ (fun _ => + doMachineOp (Z := Unit) (do setGicVcpuCtrlHcr 0; dsb; setSCTLR sctlrDefault; writeTCR tcrEL1Native; setHCR hcrNative; isb; enableFpuEL01) >>= fun _ => + doMachineOp (Z := Unit) (do writeVCPUHardwareReg .vcpuRegCNTV_CTL 0; checkExportArchTimer) >>= fun _ => + doMachineOp (Z := Unit) (maskInterrupt true irqVTimerEvent))] + · rw [doMachineOp_bind_contA, doMachineOp_bind_contA, doMachineOp_bindA] + congr 1; simp only [nd_bind_assoc_funA] + · intro _ s + refine nd_bind_congr_rightA _ _ _ (fun _ s1 => ?_) s + rw [nd_bind_assocA, doMachineOp_bind_contA] + +/-- TCB after `dissociateVcpuTcb` (clears `tcbVcpu` and sanitises `.spsrEl1`). -/ +def dissocTcbObjA (x : Tcb) : Tcb := + { x with tcbArch := { x.tcbArch with + tcbVcpu := none, + tcbContext := { x.tcbArch.tcbContext with + userRegs := Function.update x.tcbArch.tcbContext.userRegs .spsrEl1 + (sanitiseRegister false .spsrEl1 (x.tcbArch.tcbContext.userRegs .spsrEl1)) } } } + +/-- Exact state after `dissociateVcpuTcb v t` given post-disable machine state `ms'`. -/ +def dissocStateA (v t : ObjRef) (x : Tcb) (xv : Vcpu) (σ : State Unit) (ms' : MachineState) : State Unit := + { σ with + kheap := Function.update (Function.update σ.kheap t (some (.tcb (dissocTcbObjA x)))) + v (some (.archObj (.vcpu { xv with vcpuTcb := none }))) + archState := if ∃ a, σ.archState.armCurrentVcpu = some (v, a) + then { σ.archState with armCurrentVcpu := none } else σ.archState + machineState := ms' } + +/-- Exact machine-state step of `dissociateVcpuTcb v t`: `vcpuDisableNoneMopA` iff +`σ.archState.armCurrentVcpu = some (v, true)`, otherwise identity. -/ +def dissocMopA (v : ObjRef) (σ : State Unit) : MachineMonad Unit := + if σ.archState.armCurrentVcpu = some (v, true) then vcpuDisableNoneMopA else pure () + +theorem dissocMopA_comm (v : ObjRef) (σ : State Unit) : MopCommUmA (dissocMopA v σ) := by + unfold dissocMopA + split + · exact vcpuDisableNoneMopA_comm + · exact mopCommUmA_pure + +/-- Exact membership characterization of `dissociateVcpuTcb v t` (all `armCurrentVcpu` branches). -/ +theorem dissociateVcpuTcb_mem_iffA {σ σ1 : State Unit} {v t : ObjRef} {xt : Tcb} {xv : Vcpu} + (hkt : σ.kheap t = some (.tcb xt)) (hkv : σ.kheap v = some (.archObj (.vcpu xv))) + (htv : xt.tcbArch.tcbVcpu = some v) (hvt : xv.vcpuTcb = some t) : + ((), σ1) ∈ (dissociateVcpuTcb (Z := Unit) v t σ).1 ↔ + ∃ ms', ((), ms') ∈ (dissocMopA v σ σ.machineState).1 ∧ + σ1 = dissocStateA v t xt xv σ ms' := by + have hne : v ≠ t := fun e => by rw [e, hkt] at hkv; cases hkv + have hatg : archThreadGet (Z := Unit) ArchTcb.tcbVcpu t σ = ({(some v, σ)}, False) := by + rw [archThreadGet_eqA _ hkt, htv] + have hgv : getVcpu (Z := Unit) v σ = ({(xv, σ)}, False) := getVcpu_eqA hkv + have hpref : ∀ (k : Vcpu → Option (ObjRef × Bool) → SMonad Unit Unit), + (do let tVcpu ← archThreadGet ArchTcb.tcbVcpu t + let v0 ← getVcpu v + NonDet.assert (tVcpu = some v ∧ v0.vcpuTcb = some t) + let curV ← NonDet.gets fun s : State Unit => s.archState.armCurrentVcpu + k v0 curV) σ = k xv σ.archState.armCurrentVcpu σ := by + intro k + change (NonDet.bind (archThreadGet ArchTcb.tcbVcpu t) _) σ = _ + rw [nd_bind_oneV hatg] + change (NonDet.bind (getVcpu v) _) σ = _ + rw [nd_bind_oneV hgv] + have hass : (NonDet.assert (some v = some v ∧ xv.vcpuTcb = some t) : SMonad Unit Unit) σ = ({((), σ)}, False) := by + simp [NonDet.assert, hvt] + change (NonDet.bind (NonDet.assert _) _) σ = _ + rw [nd_bind_oneV hass] + exact nd_bind_getsV _ _ σ + let khFinal : KHeap := Function.update (Function.update σ.kheap t (some (.tcb (dissocTcbObjA xt)))) + v (some (.archObj (.vcpu { xv with vcpuTcb := none }))) + have hsuff : ∀ (τ : State Unit), + τ.kheap = σ.kheap → + (do archThreadSet (Z := Unit) (fun x => { x with tcbVcpu := none }) t + setVcpu v { xv with vcpuTcb := none } + asUser t do + let sr ← getRegister .spsrEl1 + setRegister .spsrEl1 (sanitiseRegister false .spsrEl1 sr)) τ = + ({((), { τ with kheap := khFinal })}, False) := by + intro τ hkh + have hkt' : τ.kheap t = some (.tcb xt) := hkh.symm ▸ hkt + let xt1 : Tcb := { xt with tcbArch := { xt.tcbArch with tcbVcpu := none } } + let τ1 : State Unit := { τ with kheap := Function.update τ.kheap t (some (.tcb xt1)) } + have h1 : archThreadSet (Z := Unit) (fun x => { x with tcbVcpu := none }) t τ = ({((), τ1)}, False) := + archThreadSet_eqA _ hkt' + have hkv1 : τ1.kheap v = some (.archObj (.vcpu xv)) := by + dsimp only [τ1]; rw [Function.update_of_ne hne, hkh]; exact hkv + let τ2 : State Unit := { τ1 with kheap := Function.update τ1.kheap v (some (.archObj (.vcpu { xv with vcpuTcb := none }))) } + have h2 : setVcpu (Z := Unit) v { xv with vcpuTcb := none } τ1 = ({((), τ2)}, False) := + setVcpu_eqA hkv1 + have hkt2 : τ2.kheap t = some (.tcb xt1) := by + dsimp only [τ2, τ1]; rw [Function.update_of_ne hne.symm, Function.update_self] + have h3 : asUser (Z := Unit) t (do + let sr ← getRegister .spsrEl1 + setRegister .spsrEl1 (sanitiseRegister false .spsrEl1 sr)) τ2 = + ({((), ({ τ2 with kheap := Function.update τ2.kheap t (some (.tcb (dissocTcbObjA xt))) } : State Unit))}, False) := by + have hf : (do let sr ← getRegister .spsrEl1; setRegister .spsrEl1 (sanitiseRegister false .spsrEl1 sr)) + (archTcbContextGet xt1.tcbArch) = + ({((), ⟨(archTcbContextGet xt1.tcbArch).userFpuState, + Function.update (archTcbContextGet xt1.tcbArch).userRegs .spsrEl1 + (sanitiseRegister false .spsrEl1 ((archTcbContextGet xt1.tcbArch).userRegs .spsrEl1))⟩)}, False) := by + unfold getRegister setRegister + rw [nd_bind_getsV]; rfl + rw [asUser_det_eqV hkt2 hf]; rfl + change (NonDet.bind (archThreadSet _ t) _) τ = _ + rw [nd_bind_oneV h1] + change (NonDet.bind (setVcpu v _) _) τ1 = _ + rw [nd_bind_oneV h2, h3] + have hst : ({ τ2 with kheap := Function.update τ2.kheap t (some (.tcb (dissocTcbObjA xt))) } : State Unit) = + { τ with kheap := khFinal } := by + have hkh2 : Function.update τ2.kheap t (some (.tcb (dissocTcbObjA xt))) = khFinal := by + funext p + dsimp only [τ2, τ1, khFinal] + by_cases hpv : p = v + · subst hpv; simp [Function.update, hne] + · by_cases hpt : p = t + · subst hpt; simp [Function.update, hne.symm] + · simp [Function.update, hpv, hpt, hkh] + cases τ with | mk abs e => + cases abs + dsimp only [τ2, τ1] at hkh2 ⊢ + congr 1; congr 1 + rw [hst] + unfold dissociateVcpuTcb + have hmain := hpref (fun v0 curV => do + NonDet.when (∃ a, curV = some (v, a)) vcpuInvalidateActive + archThreadSet (fun x => { x with tcbVcpu := none }) t + setVcpu v { v0 with vcpuTcb := none } + asUser t do + let sr ← getRegister .spsrEl1 + setRegister .spsrEl1 (sanitiseRegister false .spsrEl1 sr)) + rw [hmain] + unfold dissocMopA dissocStateA + by_cases hcur : ∃ a, σ.archState.armCurrentVcpu = some (v, a) + · rcases hcur with ⟨act, hact⟩ + have hex : ∃ a, σ.archState.armCurrentVcpu = some (v, a) := ⟨act, hact⟩ + rw [NonDet.when, if_pos hex, if_pos hex] + cases act with + | false => + have hntrue : ¬ (σ.archState.armCurrentVcpu = some (v, true)) := by rw [hact]; intro h; cases h + rw [ite_eq_right hntrue] + have hinv : vcpuInvalidateActive (Z := Unit) σ = + ({((), ({ σ with archState := { σ.archState with armCurrentVcpu := none } } : State Unit))}, False) := by + unfold vcpuInvalidateActive + rw [nd_bind_getsV, hact]; rfl + change ((), σ1) ∈ (NonDet.bind _ _ σ).1 ↔ _ + rw [nd_bind_oneV hinv, hsuff { σ with archState := { σ.archState with armCurrentVcpu := none } } rfl] + simp only [Set.mem_singleton_iff, Prod.mk.injEq, true_and, Pure.pure, NonDet.pure, khFinal] + constructor + · intro h; exact ⟨σ.machineState, rfl, h⟩ + · rintro ⟨ms', rfl, rfl⟩; rfl + | true => + rw [if_pos hact] + have hinv_mem : ∀ τ, ((), τ) ∈ (vcpuInvalidateActive (Z := Unit) σ).1 ↔ + ∃ ms', ((), ms') ∈ (vcpuDisableNoneMopA σ.machineState).1 ∧ + τ = { σ with archState := { σ.archState with armCurrentVcpu := none }, machineState := ms' } := by + intro τ + unfold vcpuInvalidateActive + rw [nd_bind_getsV, hact]; dsimp only + rw [nd_bind_mem_iffA] + constructor + · rintro ⟨⟨⟩, s1, h1, h2⟩ + rw [vcpuDisable_none_eqA, doMachineOp_mem_iffA] at h1 + obtain ⟨ms', hm, rfl⟩ := h1 + rw [nd_modify_mem_iffA] at h2 + exact ⟨ms', hm, h2⟩ + · rintro ⟨ms', hm, rfl⟩ + refine ⟨(), { σ with machineState := ms' }, ?_, nd_modify_mem_iffA.2 rfl⟩ + rw [vcpuDisable_none_eqA, doMachineOp_mem_iffA] + exact ⟨ms', hm, rfl⟩ + rw [nd_bind_mem_iffA] + constructor + · rintro ⟨⟨⟩, τ, hτ, h2⟩ + obtain ⟨ms', hm, rfl⟩ := (hinv_mem τ).1 hτ + rw [hsuff { σ with archState := { σ.archState with armCurrentVcpu := none }, machineState := ms' } rfl] at h2 + exact ⟨ms', hm, (Prod.mk.inj (Set.mem_singleton_iff.1 h2)).2⟩ + · rintro ⟨ms', hm, rfl⟩ + refine ⟨(), { σ with archState := { σ.archState with armCurrentVcpu := none }, machineState := ms' }, + (hinv_mem _).2 ⟨ms', hm, rfl⟩, ?_⟩ + rw [hsuff { σ with archState := { σ.archState with armCurrentVcpu := none }, machineState := ms' } rfl] + exact Set.mem_singleton _ + · rw [NonDet.when, if_neg hcur, if_neg hcur, if_neg (fun h => hcur ⟨true, h⟩)] + change ((), σ1) ∈ (NonDet.bind (NonDet.pure ()) _ σ).1 ↔ _ + rw [nd_bind_oneV (m := NonDet.pure ()) rfl, hsuff _ rfl] + simp only [Set.mem_singleton_iff, Prod.mk.injEq, true_and, Pure.pure, NonDet.pure, khFinal] + constructor + · intro h; exact ⟨σ.machineState, rfl, h⟩ + · rintro ⟨ms', rfl, rfl⟩; rfl + +/-! ## Section 2: `DissocRelA`, `DissocCommA`, and `fl4Dissociate_comm_tcbFinA` -/ + +/-- Exact state relation established by `dissociateVcpuTcb v t` and preserved across +every step of `unbindNotification t >> unbindFromSc t >> suspend t`. -/ +structure DissocRelA (v t : ObjRef) (σ0 σ1 : State Unit) : Prop where + ne_vt : v ≠ t + kheap_ne : ∀ p, p ≠ v → p ≠ t → σ1.kheap p = σ0.kheap p + kheap_t : ∃ xt, σ0.kheap t = some (.tcb xt) ∧ xt.tcbArch.tcbVcpu = some v ∧ + σ1.kheap t = some (.tcb (dissocTcbObjA xt)) + kheap_v : ∃ xv, σ0.kheap v = some (.archObj (.vcpu xv)) ∧ xv.vcpuTcb = some t ∧ + σ1.kheap v = some (.archObj (.vcpu { xv with vcpuTcb := none })) + cdt : σ1.cdt = σ0.cdt + isOriginalCap : σ1.isOriginalCap = σ0.isOriginalCap + curThread : σ1.curThread = σ0.curThread + idleThread : σ1.idleThread = σ0.idleThread + consumedTime : σ1.consumedTime = σ0.consumedTime + curTime : σ1.curTime = σ0.curTime + curSc : σ1.curSc = σ0.curSc + reprogramTimer : σ1.reprogramTimer = σ0.reprogramTimer + schedulerAction : σ1.schedulerAction = σ0.schedulerAction + domainList : σ1.domainList = σ0.domainList + domainIndex : σ1.domainIndex = σ0.domainIndex + domainStartIndex : σ1.domainStartIndex = σ0.domainStartIndex + curDomain : σ1.curDomain = σ0.curDomain + domainTime : σ1.domainTime = σ0.domainTime + readyQueues : σ1.readyQueues = σ0.readyQueues + releaseQueue : σ1.releaseQueue = σ0.releaseQueue + interruptIrqNode : σ1.interruptIrqNode = σ0.interruptIrqNode + interruptStates : σ1.interruptStates = σ0.interruptStates + arch_eq : σ1.archState = + if ∃ a, σ0.archState.armCurrentVcpu = some (v, a) + then { σ0.archState with armCurrentVcpu := none } else σ0.archState + machine_mop : ((), σ1.machineState) ∈ (dissocMopA v σ0 σ0.machineState).1 + exst : σ1.exst = σ0.exst + +private theorem dissocRelA_msKeep {v t : ObjRef} {σ0 σ1 : State Unit} (h : DissocRelA v t σ0 σ1) : + MsKeepUmTimeA σ0.machineState σ1.machineState := + ((dissocMopA_comm v σ0).ok σ0.machineState).2.2 ((), σ1.machineState) h.machine_mop + +private theorem dissocRelA_of_mem {v t : ObjRef} {σ0 σ1 : State Unit} {xt : Tcb} {xv : Vcpu} + (hkt : σ0.kheap t = some (.tcb xt)) (hkv : σ0.kheap v = some (.archObj (.vcpu xv))) + (htv : xt.tcbArch.tcbVcpu = some v) (hvt : xv.vcpuTcb = some t) + (hm : ((), σ1) ∈ (dissociateVcpuTcb (Z := Unit) v t σ0).1) : + DissocRelA v t σ0 σ1 := by + have hne : v ≠ t := fun e => by rw [e, hkt] at hkv; cases hkv + obtain ⟨ms', hmop, rfl⟩ := (dissociateVcpuTcb_mem_iffA hkt hkv htv hvt).1 hm + refine ⟨hne, ?_, ⟨xt, hkt, htv, ?_⟩, ⟨xv, hkv, hvt, ?_⟩, + rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, hmop, rfl⟩ + · intro p hpv hpt; simp [dissocStateA, Function.update, hpv, hpt] + · simp [dissocStateA, Function.update, hne.symm] + · simp [dissocStateA, Function.update] + +private theorem dissocRelA_to_mem {v t : ObjRef} {σ0 σ1 : State Unit} (h : DissocRelA v t σ0 σ1) : + ((), σ1) ∈ (dissociateVcpuTcb (Z := Unit) v t σ0).1 := by + obtain ⟨xt, hkt, htv, hkt1⟩ := h.kheap_t + obtain ⟨xv, hkv, hvt, hkv1⟩ := h.kheap_v + rw [dissociateVcpuTcb_mem_iffA hkt hkv htv hvt] + refine ⟨σ1.machineState, h.machine_mop, ?_⟩ + have hkh : σ1.kheap = (dissocStateA v t xt xv σ0 σ1.machineState).kheap := by + funext p + by_cases hpv : p = v + · subst hpv; simp [dissocStateA, Function.update, hkv1] + · by_cases hpt : p = t + · subst hpt; simp [dissocStateA, Function.update, h.ne_vt.symm, hkt1] + · simp [dissocStateA, Function.update, hpv, hpt, h.kheap_ne p hpv hpt] + cases σ0 with | mk abs0 e0 => + cases abs0 + cases σ1 with | mk abs1 e1 => + cases abs1 + simp only [dissocStateA] at hkh ⊢ + rcases h with ⟨_, _, _, _, h1, h2, h3, h4, h5, h6, h7, h8, h9, h10, h11, h12, h13, h14, h15, h16, h17, h18, h19, _, h20⟩ + dsimp only at h1 h2 h3 h4 h5 h6 h7 h8 h9 h10 h11 h12 h13 h14 h15 h16 h17 h18 h19 h20 + subst hkh h1 h2 h3 h4 h5 h6 h7 h8 h9 h10 h11 h12 h13 h14 h15 h16 h17 h18 h19 h20 + rfl + +/-- Commutation predicate: any step of `m` from a dissociated state `σ1` can be matched +from the pre-dissociation state `σ0`, preserving `DissocRelA v t`. -/ +def DissocCommA (v t : ObjRef) {α : Type} (m : SMonad Unit α) : Prop := + ∀ σ0 σ1 a σ1', DissocRelA v t σ0 σ1 → (a, σ1') ∈ (m σ1).1 → + ∃ σ0', (a, σ0') ∈ (m σ0).1 ∧ DissocRelA v t σ0' σ1' + +private theorem dissocCommA_pure {v t : ObjRef} {α : Type} (x : α) : + DissocCommA v t (pure x : SMonad Unit α) := by + intro σ0 σ1 a σ1' hrel hm + rcases Prod.mk.inj (Set.mem_singleton_iff.1 hm) with ⟨rfl, rfl⟩ + exact ⟨σ0, Set.mem_singleton _, hrel⟩ + +private theorem dissocCommA_bind {v t : ObjRef} {α β : Type} {m : SMonad Unit α} {f : α → SMonad Unit β} + (hm : DissocCommA v t m) (hf : ∀ a, DissocCommA v t (f a)) : + DissocCommA v t (m >>= f) := by + intro σ0 σ1 b σ1' hrel h + simp only [Bind.bind, NonDet.bind, Set.mem_iUnion] at h ⊢ + rcases h with ⟨⟨a, σ1m⟩, h1, h2⟩ + obtain ⟨σ0m, h01, hrelm⟩ := hm σ0 σ1 a σ1m hrel h1 + obtain ⟨σ0', h02, hrel'⟩ := hf a σ0m σ1m b σ1' hrelm h2 + exact ⟨σ0', ⟨(a, σ0m), h01, h02⟩, hrel'⟩ + +private theorem dissocCommA_when {v t : ObjRef} (c : Prop) [Decidable c] {m : SMonad Unit Unit} + (hm : DissocCommA v t m) : DissocCommA v t (NonDet.when c m) := by + unfold NonDet.when; split + · exact hm + · exact dissocCommA_pure () + +private theorem dissocCommA_maybeM {v t : ObjRef} {α : Type} {f : α → SMonad Unit Unit} + (hf : ∀ a, DissocCommA v t (f a)) (o : Option α) : + DissocCommA v t (NonDet.maybeM f o) := by + cases o with + | none => exact dissocCommA_pure () + | some a => exact hf a + +private theorem dissocCommA_assert {v t : ObjRef} (p : Prop) : + DissocCommA v t (NonDet.assert p : SMonad Unit Unit) := by + intro σ0 σ1 a σ1' hrel hm + unfold NonDet.assert at hm ⊢ + rcases hm with ⟨hp, hpair⟩ + rcases Prod.mk.inj hpair with ⟨rfl, rfl⟩ + exact ⟨σ0, ⟨hp, rfl⟩, hrel⟩ + +private theorem dissocCommA_assertOpt {v t : ObjRef} {α : Type} (o : Option α) : + DissocCommA v t (NonDet.assertOpt o : SMonad Unit α) := by + cases o with + | none => intro _ _ _ _ _ hm; exact False.elim hm + | some a => exact dissocCommA_pure a + +private theorem getsThe_mem_iffA {σ α : Type} (f : σ → Option α) (s s' : σ) (a : α) : + (a, s') ∈ (NonDet.getsThe f s).1 ↔ f s = some a ∧ s' = s := by + unfold NonDet.getsThe NonDet.assertOpt + cases h : f s with + | none => simp [NonDet.fail] + | some x => simp [NonDet.pure, Set.mem_singleton_iff, Prod.mk.injEq, eq_comm] + +private theorem getObject_mem_iffA (p : ObjRef) (σ σ' : State Unit) (o : KernelObject) : + (o, σ') ∈ (getObject (Z := Unit) p σ).1 ↔ σ.kheap p = some o ∧ σ' = σ := by + unfold getObject readObject + exact getsThe_mem_iffA _ _ _ _ + +private theorem dissocCommA_gets {v t : ObjRef} {α : Type} (f : State Unit → α) + (hf : ∀ σ0 σ1, DissocRelA v t σ0 σ1 → f σ1 = f σ0) : + DissocCommA v t (NonDet.gets f) := by + intro σ0 σ1 a σ1' hrel hm + rcases Prod.mk.inj (Set.mem_singleton_iff.1 hm) with ⟨rfl, rfl⟩ + rw [hf σ0 σ1' hrel] + exact ⟨σ0, Set.mem_singleton _, hrel⟩ + +private theorem dissocCommA_getsThe {v t : ObjRef} {α : Type} (f : State Unit → Option α) + (hf : ∀ σ0 σ1, DissocRelA v t σ0 σ1 → f σ1 = f σ0) : + DissocCommA v t (NonDet.getsThe f) := by + intro σ0 σ1 a σ1' hrel hm + obtain ⟨hfa, rfl⟩ := (getsThe_mem_iffA f σ1 σ1' a).1 hm + rw [hf σ0 σ1' hrel] at hfa + exact ⟨σ0, (getsThe_mem_iffA f σ0 σ0 a).2 ⟨hfa, rfl⟩, hrel⟩ + +private theorem dissocCommA_liftM {v t : ObjRef} {α β : Type} (f : α → β) {m : SMonad Unit α} + (hm : DissocCommA v t m) : DissocCommA v t (NonDet.liftM f m) := + dissocCommA_bind hm fun a => dissocCommA_pure (f a) + +private theorem dissocCommA_modify_sched {v t : ObjRef} (f : State Unit → State Unit) + (hpres : ∀ σ0 σ1, DissocRelA v t σ0 σ1 → DissocRelA v t (f σ0) (f σ1)) : + DissocCommA v t (NonDet.modify f) := by + intro σ0 σ1 a σ1' hrel hm + rcases Prod.mk.inj (Set.mem_singleton_iff.1 hm) with ⟨rfl, rfl⟩ + exact ⟨f σ0, Set.mem_singleton _, hpres σ0 σ1 hrel⟩ + +private theorem dissocCommA_threadGet {v t : ObjRef} {α : Type} (f : Tcb → α) + (hf : ∀ x, f (dissocTcbObjA x) = f x) (p : ObjRef) : + DissocCommA v t (threadGet (Z := Unit) f p) := by + intro σ0 σ1 a σ1' hrel hm + unfold threadGet at hm ⊢ + simp only [nd_bind_mem_iffA, getsThe_mem_iffA, nd_pure_mem_iffA] at hm ⊢ + rcases hm with ⟨xt1, s1, ⟨hgt1, rfl⟩, rfl, rfl⟩ + have hkt1 : σ1'.kheap p = some (.tcb xt1) := by + unfold getTcb readObject at hgt1; cases hk : σ1'.kheap p with + | none => rw [hk] at hgt1; cases hgt1 + | some obj => rw [hk] at hgt1; cases obj <;> cases hgt1; rfl + by_cases hpv : p = v + · subst hpv; obtain ⟨xv, _, _, hkv1⟩ := hrel.kheap_v; rw [hkv1] at hkt1; cases hkt1 + · by_cases hpt : p = t + · subst hpt; obtain ⟨xt0, hkt0, _, hkt1'⟩ := hrel.kheap_t; rw [hkt1'] at hkt1; cases hkt1 + exact ⟨σ0, ⟨xt0, σ0, ⟨by unfold getTcb readObject; rw [hkt0], rfl⟩, hf xt0, rfl⟩, hrel⟩ + · have hkt0 : σ0.kheap p = some (.tcb xt1) := by rw [← hrel.kheap_ne p hpv hpt, hkt1] + exact ⟨σ0, ⟨xt1, σ0, ⟨by unfold getTcb readObject; rw [hkt0], rfl⟩, rfl, rfl⟩, hrel⟩ + +private theorem dissocCommA_threadSet {v t : ObjRef} (f : Tcb → Tcb) + (hcomm : ∀ x, f (dissocTcbObjA x) = dissocTcbObjA (f x)) + (hvcpu : ∀ x, (f x).tcbArch.tcbVcpu = x.tcbArch.tcbVcpu) (p : ObjRef) : + DissocCommA v t (threadSet (Z := Unit) f p) := by + intro σ0 σ1 a σ1' hrel hm + unfold threadSet setObject at hm ⊢ + simp only [nd_bind_mem_iffA, getsThe_mem_iffA, getObject_mem_iffA, nd_assert_mem_iffA, + nd_get_mem_iffA, nd_put_mem_iffA] at hm ⊢ + rcases hm with ⟨xt1, s1, ⟨hgt1, rfl⟩, obj1, s2, ⟨hko1, rfl⟩, ⟨⟩, s3, ⟨hbits, rfl⟩, s4, s5, ⟨rfl, rfl⟩, rfl⟩ + have hkt1 : s5.kheap p = some (.tcb xt1) := by + unfold getTcb readObject at hgt1; cases hk : s5.kheap p with + | none => rw [hk] at hgt1; cases hgt1 + | some obj => rw [hk] at hgt1; cases obj <;> cases hgt1; rfl + rw [hkt1] at hko1; cases hko1 + have hpv : p ≠ v := fun e => by + subst e; obtain ⟨xv, _, _, hkv1⟩ := hrel.kheap_v; rw [hkv1] at hkt1; cases hkt1 + by_cases hpt : p = t + · subst hpt + obtain ⟨xt0, hkt0, htv0, hkt1'⟩ := hrel.kheap_t + rw [hkt1'] at hkt1; cases hkt1 + have hbits0 : aType (.tcb xt0) = aType (.tcb (f xt0)) := rfl + refine ⟨{ σ0 with kheap := Function.update σ0.kheap p (some (.tcb (f xt0))) }, ?_, ?_⟩ + · exact ⟨xt0, σ0, ⟨by unfold getTcb readObject; rw [hkt0], rfl⟩, + .tcb xt0, σ0, ⟨hkt0, rfl⟩, (), σ0, ⟨hbits0, rfl⟩, σ0, σ0, ⟨rfl, rfl⟩, rfl⟩ + · refine { hrel with + kheap_ne := fun q hqv hqt => by + dsimp only; simp only [Function.update_of_ne hqt]; exact hrel.kheap_ne q hqv hqt + kheap_t := ⟨f xt0, by dsimp only; exact Function.update_self _ _ _, (hvcpu xt0).trans htv0, by + dsimp only; rw [Function.update_self, hcomm]⟩ + kheap_v := by + obtain ⟨xv, hkv0, hvt0, hkv1⟩ := hrel.kheap_v + exact ⟨xv, by dsimp only; rw [Function.update_of_ne hrel.ne_vt]; exact hkv0, hvt0, + by dsimp only; rw [Function.update_of_ne hrel.ne_vt]; exact hkv1⟩ } + · have hkt0 : σ0.kheap p = some (.tcb xt1) := by rw [← hrel.kheap_ne p hpv hpt, hkt1] + refine ⟨{ σ0 with kheap := Function.update σ0.kheap p (some (.tcb (f xt1))) }, ?_, ?_⟩ + · exact ⟨xt1, σ0, ⟨by unfold getTcb readObject; rw [hkt0], rfl⟩, + .tcb xt1, σ0, ⟨hkt0, rfl⟩, (), σ0, ⟨hbits, rfl⟩, σ0, σ0, ⟨rfl, rfl⟩, rfl⟩ + · refine { hrel with + kheap_ne := fun q hqv hqt => by + dsimp only + by_cases hqp : q = p + · subst hqp; simp [Function.update] + · simp only [Function.update_of_ne hqp]; exact hrel.kheap_ne q hqv hqt + kheap_t := by + obtain ⟨xt0, hkt0', htv0, hkt1'⟩ := hrel.kheap_t + exact ⟨xt0, by dsimp only; rw [Function.update_of_ne (Ne.symm hpt)]; exact hkt0', htv0, + by dsimp only; rw [Function.update_of_ne (Ne.symm hpt)]; exact hkt1'⟩ + kheap_v := by + obtain ⟨xv, hkv0, hvt0, hkv1⟩ := hrel.kheap_v + exact ⟨xv, by dsimp only; rw [Function.update_of_ne (Ne.symm hpv)]; exact hkv0, hvt0, + by dsimp only; rw [Function.update_of_ne (Ne.symm hpv)]; exact hkv1⟩ } + +private theorem dissocCommA_setObject_ne {v t : ObjRef} {p : ObjRef} (hpv : p ≠ v) (hpt : p ≠ t) + (obj : KernelObject) : + DissocCommA v t (setObject (Z := Unit) p obj) := by + intro σ0 σ1 a σ1' hrel hm + unfold setObject at hm ⊢ + simp only [nd_bind_mem_iffA, getObject_mem_iffA, nd_assert_mem_iffA, + nd_get_mem_iffA, nd_put_mem_iffA] at hm ⊢ + rcases hm with ⟨oldObj, s1, ⟨hk1, rfl⟩, ⟨⟩, s2, ⟨hbits, rfl⟩, s3, s4, ⟨rfl, rfl⟩, rfl⟩ + have hk0 : σ0.kheap p = some oldObj := by rw [← hrel.kheap_ne p hpv hpt, hk1] + refine ⟨{ σ0 with kheap := Function.update σ0.kheap p (some obj) }, ?_, ?_⟩ + · exact ⟨oldObj, σ0, ⟨hk0, rfl⟩, (), σ0, ⟨hbits, rfl⟩, σ0, σ0, ⟨rfl, rfl⟩, rfl⟩ + · refine { hrel with + kheap_ne := fun q hqv hqt => by + dsimp only + by_cases hqp : q = p + · subst hqp; simp [Function.update] + · simp only [Function.update_of_ne hqp]; exact hrel.kheap_ne q hqv hqt + kheap_t := by + obtain ⟨xt0, hkt0, htv0, hkt1⟩ := hrel.kheap_t + exact ⟨xt0, by dsimp only; rw [Function.update_of_ne (Ne.symm hpt)]; exact hkt0, htv0, + by dsimp only; rw [Function.update_of_ne (Ne.symm hpt)]; exact hkt1⟩ + kheap_v := by + obtain ⟨xv, hkv0, hvt0, hkv1⟩ := hrel.kheap_v + exact ⟨xv, by dsimp only; rw [Function.update_of_ne (Ne.symm hpv)]; exact hkv0, hvt0, + by dsimp only; rw [Function.update_of_ne (Ne.symm hpv)]; exact hkv1⟩ } + +/-- Generic helper for `getEndpoint` / `getNotification` / `getSchedContext` / `getReply`: +any `getObject p` followed by matching a non-TCB, non-VCPU constructor satisfies `DissocCommA`. -/ +private theorem dissocCommA_getObjMatch {v t : ObjRef} {α : Type} (p : ObjRef) + (ext : KernelObject → Option α) + (hnt : ∀ x, ext (.tcb x) = none) (hnv : ∀ xv, ext (.archObj (.vcpu xv)) = none) : + DissocCommA v t (do let o ← getObject (Z := Unit) p; NonDet.assertOpt (ext o)) := by + intro σ0 σ1 a σ1' hrel hm + simp only [nd_bind_mem_iffA, getObject_mem_iffA] at hm ⊢ + rcases hm with ⟨obj, s1, ⟨hk1, rfl⟩, h2⟩ + cases hext : ext obj with + | none => rw [hext] at h2; exact False.elim h2 + | some val => + rw [hext] at h2 + rcases Prod.mk.inj (Set.mem_singleton_iff.1 h2) with ⟨rfl, rfl⟩ + have hpv : p ≠ v := fun e => by + subst e; obtain ⟨xv, _, _, hkv1⟩ := hrel.kheap_v; rw [hkv1] at hk1; cases hk1 + rw [hnv] at hext; cases hext + have hpt : p ≠ t := fun e => by + subst e; obtain ⟨xt, _, _, hkt1⟩ := hrel.kheap_t; rw [hkt1] at hk1; cases hk1 + rw [hnt] at hext; cases hext + have hk0 : σ0.kheap p = some obj := by rw [← hrel.kheap_ne p hpv hpt, hk1] + exact ⟨σ0, ⟨obj, σ0, ⟨hk0, rfl⟩, by rw [hext]; exact Set.mem_singleton _⟩, hrel⟩ + +private theorem dissocCommA_setObjChecked {v t : ObjRef} (p : ObjRef) (obj : KernelObject) + (check : KernelObject → Prop) + (hnt : ∀ x, ¬ check (.tcb x)) (hnv : ∀ xv, ¬ check (.archObj (.vcpu xv))) + (m : SMonad Unit Unit) + (hm_eq : ∀ σ, m σ = (do let o ← getObject (Z := Unit) p; NonDet.assert (check o); setObject p obj) σ) : + DissocCommA v t m := by + intro σ0 σ1 a σ1' hrel hm + rw [hm_eq] at hm ⊢ + simp only [nd_bind_mem_iffA, getObject_mem_iffA, nd_assert_mem_iffA] at hm ⊢ + rcases hm with ⟨o, s1, ⟨hk1, rfl⟩, ⟨⟩, s2, ⟨hc, rfl⟩, hset⟩ + have hpv : p ≠ v := fun e => by + subst e; obtain ⟨xv, _, _, hkv1⟩ := hrel.kheap_v; rw [hkv1] at hk1; cases hk1; exact hnv _ hc + have hpt : p ≠ t := fun e => by + subst e; obtain ⟨xt, _, _, hkt1⟩ := hrel.kheap_t; rw [hkt1] at hk1; cases hk1; exact hnt _ hc + obtain ⟨σ0', hset0, hrel'⟩ := dissocCommA_setObject_ne hpv hpt obj σ0 s2 a σ1' hrel hset + have hk0 : σ0.kheap p = some o := by rw [← hrel.kheap_ne p hpv hpt, hk1] + exact ⟨σ0', ⟨o, σ0, ⟨hk0, rfl⟩, (), σ0, ⟨hc, rfl⟩, hset0⟩, hrel'⟩ + +private theorem dissocCommA_asUser_setReg {v t : ObjRef} (p : ObjRef) (r : Register) (hr : r ≠ .spsrEl1) (w : Word) : + DissocCommA v t (asUser (Z := Unit) p (setRegister r w)) := by + intro σ0 σ1 a σ1' hrel hm + have hkt1 : ∃ x1, σ1.kheap p = some (.tcb x1) := by + unfold asUser at hm + simp only [nd_bind_mem_iffA, getsThe_mem_iffA] at hm + rcases hm with ⟨x1, _, ⟨hgt1, _⟩, _⟩ + unfold getTcb readObject at hgt1; cases hk : σ1.kheap p with + | none => rw [hk] at hgt1; cases hgt1 + | some obj => rw [hk] at hgt1; cases obj <;> cases hgt1; exact ⟨_, rfl⟩ + rcases hkt1 with ⟨x1, hk1⟩ + have hpv : p ≠ v := fun e => by + subst e; obtain ⟨xv, _, _, hkv1⟩ := hrel.kheap_v; rw [hkv1] at hk1; cases hk1 + have hset : ∀ (σ : State Unit) (x : Tcb), σ.kheap p = some (.tcb x) → + asUser (Z := Unit) p (setRegister r w) σ = + ({((), { σ with kheap := Function.update σ.kheap p (some (.tcb { x with tcbArch := + { x.tcbArch with tcbContext := { x.tcbArch.tcbContext with userRegs := Function.update x.tcbArch.tcbContext.userRegs r w } } })) })}, False) := + fun σ x hkx => asUser_det_eqV hkx rfl + rw [hset σ1 x1 hk1] at hm + rcases Prod.mk.inj (Set.mem_singleton_iff.1 hm) with ⟨rfl, rfl⟩ + by_cases hpt : p = t + · subst hpt; obtain ⟨xt, hkt0, htv0, hkt1'⟩ := hrel.kheap_t + rw [hkt1'] at hk1; cases hk1 + rw [hset σ0 xt hkt0] + let xt' : Tcb := { xt with tcbArch := { xt.tcbArch with tcbContext := + { xt.tcbArch.tcbContext with userRegs := Function.update xt.tcbArch.tcbContext.userRegs r w } } } + refine ⟨{ σ0 with kheap := Function.update σ0.kheap p (some (.tcb xt')) }, Set.mem_singleton _, ?_⟩ + refine { hrel with + kheap_ne := fun q hqv hqt => by + dsimp only; simp only [Function.update_of_ne hqt]; exact hrel.kheap_ne q hqv hqt + kheap_t := ⟨xt', by dsimp only; exact Function.update_self _ _ _, htv0, by + have hreg_eq : Function.update (dissocTcbObjA xt).tcbArch.tcbContext.userRegs r w = + (dissocTcbObjA xt').tcbArch.tcbContext.userRegs := by + funext reg + by_cases hrr : reg = r + · subst hrr; simp [dissocTcbObjA, xt', Function.update, hr] + · by_cases hrs : reg = .spsrEl1 + · subst hrs; simp [dissocTcbObjA, xt', Function.update, hr.symm] + · simp [dissocTcbObjA, xt', Function.update, hrr, hrs] + dsimp only; rw [Function.update_self] + dsimp only [dissocTcbObjA, xt'] at hreg_eq ⊢ + rw [hreg_eq]⟩ + kheap_v := by + obtain ⟨xv, hkv0, hvt0, hkv1⟩ := hrel.kheap_v + exact ⟨xv, by dsimp only; rw [Function.update_of_ne hrel.ne_vt]; exact hkv0, hvt0, + by dsimp only; rw [Function.update_of_ne hrel.ne_vt]; exact hkv1⟩ } + · have hk0 : σ0.kheap p = some (.tcb x1) := by rw [← hrel.kheap_ne p hpv hpt, hk1] + rw [hset σ0 x1 hk0] + let x' : Tcb := { x1 with tcbArch := { x1.tcbArch with tcbContext := + { x1.tcbArch.tcbContext with userRegs := Function.update x1.tcbArch.tcbContext.userRegs r w } } } + refine ⟨{ σ0 with kheap := Function.update σ0.kheap p (some (.tcb x')) }, Set.mem_singleton _, ?_⟩ + refine { hrel with + kheap_ne := fun q hqv hqt => by + dsimp only + by_cases hqp : q = p + · subst hqp; simp [Function.update, x'] + · simp only [Function.update_of_ne hqp]; exact hrel.kheap_ne q hqv hqt + kheap_t := by + obtain ⟨xt0, hkt0, htv0, hkt1'⟩ := hrel.kheap_t + exact ⟨xt0, by dsimp only; rw [Function.update_of_ne (Ne.symm hpt)]; exact hkt0, htv0, + by dsimp only; rw [Function.update_of_ne (Ne.symm hpt)]; exact hkt1'⟩ + kheap_v := by + obtain ⟨xv, hkv0, hvt0, hkv1⟩ := hrel.kheap_v + exact ⟨xv, by dsimp only; rw [Function.update_of_ne (Ne.symm hpv)]; exact hkv0, hvt0, + by dsimp only; rw [Function.update_of_ne (Ne.symm hpv)]; exact hkv1⟩ } + +private theorem dissocCommA_updateRestartPc {v t : ObjRef} (p : ObjRef) : + DissocCommA v t (updateRestartPc (Z := Unit) p) := by + intro σ0 σ1 a σ1' hrel hm + unfold updateRestartPc at hm ⊢ + have hkt1 : ∃ x1, σ1.kheap p = some (.tcb x1) := by + unfold asUser at hm + simp only [nd_bind_mem_iffA, getsThe_mem_iffA] at hm + rcases hm with ⟨x1, _, ⟨hgt1, _⟩, _⟩ + unfold getTcb readObject at hgt1; cases hk : σ1.kheap p with + | none => rw [hk] at hgt1; cases hgt1 + | some obj => rw [hk] at hgt1; cases obj <;> cases hgt1; exact ⟨_, rfl⟩ + rcases hkt1 with ⟨x1, hk1⟩ + have hpv : p ≠ v := fun e => by + subst e; obtain ⟨xv, _, _, hkv1⟩ := hrel.kheap_v; rw [hkv1] at hk1; cases hk1 + have hupd : ∀ (σ : State Unit) (x : Tcb), σ.kheap p = some (.tcb x) → + asUser (Z := Unit) p (NonDet.bind (getRegister nextInstructionRegister) (setRegister faultRegister)) σ = + ({((), { σ with kheap := Function.update σ.kheap p (some (.tcb { x with tcbArch := + { x.tcbArch with tcbContext := { x.tcbArch.tcbContext with + userRegs := Function.update x.tcbArch.tcbContext.userRegs faultRegister + (x.tcbArch.tcbContext.userRegs nextInstructionRegister) } } })) })}, False) := fun σ x hkx => by + refine asUser_det_eqV hkx ?_ + unfold getRegister setRegister + change (NonDet.gets _ >>= _) _ = _ + rw [nd_bind_getsV]; rfl + rw [hupd σ1 x1 hk1] at hm + rcases Prod.mk.inj (Set.mem_singleton_iff.1 hm) with ⟨rfl, rfl⟩ + by_cases hpt : p = t + · subst hpt; obtain ⟨xt, hkt0, htv0, hkt1'⟩ := hrel.kheap_t + rw [hkt1'] at hk1; cases hk1 + rw [hupd σ0 xt hkt0] + have hnr : nextInstructionRegister ≠ .spsrEl1 := by decide + have hfr : faultRegister ≠ .spsrEl1 := by decide + let ur' := Function.update xt.tcbArch.tcbContext.userRegs faultRegister + (xt.tcbArch.tcbContext.userRegs nextInstructionRegister) + let xt' : Tcb := { xt with tcbArch := { xt.tcbArch with tcbContext := { xt.tcbArch.tcbContext with userRegs := ur' } } } + refine ⟨{ σ0 with kheap := Function.update σ0.kheap p (some (.tcb xt')) }, Set.mem_singleton _, ?_⟩ + refine { hrel with + kheap_ne := fun q hqv hqt => by + dsimp only; simp only [Function.update_of_ne hqt]; exact hrel.kheap_ne q hqv hqt + kheap_t := ⟨xt', by dsimp only; exact Function.update_self _ _ _, htv0, by + have hreg_eq : Function.update (dissocTcbObjA xt).tcbArch.tcbContext.userRegs faultRegister + ((dissocTcbObjA xt).tcbArch.tcbContext.userRegs nextInstructionRegister) = + (dissocTcbObjA xt').tcbArch.tcbContext.userRegs := by + funext reg + by_cases hrr : reg = faultRegister + · subst hrr; simp [dissocTcbObjA, xt', ur', Function.update, hnr, hfr] + · by_cases hrs : reg = .spsrEl1 + · subst hrs; simp [dissocTcbObjA, xt', ur', Function.update, hfr.symm] + · simp [dissocTcbObjA, xt', ur', Function.update, hrr, hrs] + dsimp only; rw [Function.update_self] + dsimp only [dissocTcbObjA, xt', ur'] at hreg_eq ⊢ + rw [hreg_eq]⟩ + kheap_v := by + obtain ⟨xv, hkv0, hvt0, hkv1⟩ := hrel.kheap_v + exact ⟨xv, by dsimp only; rw [Function.update_of_ne hrel.ne_vt]; exact hkv0, hvt0, + by dsimp only; rw [Function.update_of_ne hrel.ne_vt]; exact hkv1⟩ } + · have hk0 : σ0.kheap p = some (.tcb x1) := by rw [← hrel.kheap_ne p hpv hpt, hk1] + rw [hupd σ0 x1 hk0] + let ur' := Function.update x1.tcbArch.tcbContext.userRegs faultRegister + (x1.tcbArch.tcbContext.userRegs nextInstructionRegister) + let x' : Tcb := { x1 with tcbArch := { x1.tcbArch with tcbContext := { x1.tcbArch.tcbContext with userRegs := ur' } } } + refine ⟨{ σ0 with kheap := Function.update σ0.kheap p (some (.tcb x')) }, Set.mem_singleton _, ?_⟩ + refine { hrel with + kheap_ne := fun q hqv hqt => by + dsimp only + by_cases hqp : q = p + · subst hqp; simp [Function.update, x', ur'] + · simp only [Function.update_of_ne hqp]; exact hrel.kheap_ne q hqv hqt + kheap_t := by + obtain ⟨xt0, hkt0, htv0, hkt1'⟩ := hrel.kheap_t + exact ⟨xt0, by dsimp only; rw [Function.update_of_ne (Ne.symm hpt)]; exact hkt0, htv0, + by dsimp only; rw [Function.update_of_ne (Ne.symm hpt)]; exact hkt1'⟩ + kheap_v := by + obtain ⟨xv, hkv0, hvt0, hkv1⟩ := hrel.kheap_v + exact ⟨xv, by dsimp only; rw [Function.update_of_ne (Ne.symm hpv)]; exact hkv0, hvt0, + by dsimp only; rw [Function.update_of_ne (Ne.symm hpv)]; exact hkv1⟩ } + +private theorem dissocCommA_storeWord {v t : ObjRef} (ptr w : Word) : + DissocCommA v t (doMachineOp (Z := Unit) (storeWord ptr w)) := by + intro σ0 σ1 a σ1' hrel hm + obtain ⟨ms1', hsw, rfl⟩ := doMachineOp_mem_iffA.1 hm + unfold storeWord at hsw + simp only [nd_bind_mem_iffA, nd_assert_mem_iffA, nd_modify_mem_iffA] at hsw + rcases hsw with ⟨⟨⟩, s0, ⟨hal, rfl⟩, rfl⟩ + let um1 := (intUpto 0 7).foldl + (fun m i => Function.update m (ptr + BitVec.ofInt 64 i) + ((wordRsplit w : List Word8).getD (7 - i.toNat) 0)) + σ1.machineState.underlyingMemory + have hum_eq : um1 = (intUpto 0 7).foldl + (fun m i => Function.update m (ptr + BitVec.ofInt 64 i) + ((wordRsplit w : List Word8).getD (7 - i.toNat) 0)) + σ0.machineState.underlyingMemory := by + simp only [um1, (dissocRelA_msKeep hrel).1] + refine ⟨{ σ0 with machineState := { σ0.machineState with underlyingMemory := um1 } }, ?_, ?_⟩ + · rw [doMachineOp_mem_iffA] + refine ⟨{ σ0.machineState with underlyingMemory := um1 }, ?_, rfl⟩ + unfold storeWord + simp only [nd_bind_mem_iffA, nd_assert_mem_iffA, nd_modify_mem_iffA] + exact ⟨(), σ0.machineState, ⟨hal, rfl⟩, by simp [hum_eq]⟩ + · refine { hrel with machine_mop := ?_ } + exact (dissocMopA_comm v σ0).comm σ0.machineState σ1.machineState um1 hrel.machine_mop + +private theorem dissocCommA_storeWordOffs {v t : ObjRef} (ptr : ObjRef) (offs : Nat) (w : Word) : + DissocCommA v t (storeWordOffs (Z := Unit) ptr offs w) := by + intro σ0 σ1 a σ1' hrel hm + unfold storeWordOffs at hm ⊢ + simp only [nd_bind_mem_iffA, nd_get_mem_iffA, nd_assert_mem_iffA] at hm ⊢ + rcases hm with ⟨s0, s1, ⟨rfl, rfl⟩, ⟨⟩, s2, ⟨hiuf, rfl⟩, hsw⟩ + have hiuf0 : inUserFrame (ptr + BitVec.ofNat 64 (offs * wordSize)) σ0 := by + unfold inUserFrame at hiuf ⊢ + rcases hiuf with ⟨sz, hk⟩; refine ⟨sz, ?_⟩ + have hpv : (ptr + BitVec.ofNat 64 (offs * wordSize)) &&& ~~~mask (pageBitsForSize sz) ≠ v := by + intro e; rw [e] at hk; obtain ⟨xv, _, _, hkv1⟩ := hrel.kheap_v + rw [hkv1] at hk; cases hk + have hpt : (ptr + BitVec.ofNat 64 (offs * wordSize)) &&& ~~~mask (pageBitsForSize sz) ≠ t := by + intro e; rw [e] at hk; obtain ⟨xt, _, _, hkt1⟩ := hrel.kheap_t + rw [hkt1] at hk; cases hk + rw [← hrel.kheap_ne _ hpv hpt]; exact hk + obtain ⟨σ0', hsw0, hrel'⟩ := dissocCommA_storeWord (ptr + BitVec.ofNat 64 (offs * wordSize)) w σ0 s2 a σ1' hrel hsw + exact ⟨σ0', ⟨σ0, σ0, ⟨rfl, rfl⟩, (), σ0, ⟨hiuf0, rfl⟩, hsw0⟩, hrel'⟩ + +private theorem nd_assertOpt_mem_iffA {σ α : Type} {o : Option α} {a : α} {s s' : σ} : + (a, s') ∈ (NonDet.assertOpt o s).1 ↔ o = some a ∧ s' = s := by + cases o with + | none => simp [NonDet.assertOpt, NonDet.fail] + | some x => simp [NonDet.assertOpt, NonDet.pure, Set.mem_singleton_iff, Prod.mk.injEq, eq_comm] + +private theorem dissocCommA_getCap {v t : ObjRef} (slot : CslotPtr) : + DissocCommA v t (getCap (Z := Unit) slot) := by + rcases slot with ⟨oref, cref⟩ + intro σ0 σ1 a σ1' hrel hm + unfold getCap at hm ⊢ + dsimp only at hm ⊢ + rw [nd_bind_mem_iffA] at hm + rcases hm with ⟨o, s1, hgo, hrest⟩ + obtain ⟨hk1, rfl⟩ := (getObject_mem_iffA oref σ1 s1 o).1 hgo + by_cases hpv : oref = v + · subst hpv; obtain ⟨xv, _, _, hkv1⟩ := hrel.kheap_v; rw [hkv1] at hk1; cases hk1 + rw [nd_bind_mem_iffA] at hrest; rcases hrest with ⟨_, _, hf, _⟩; exact False.elim hf + · by_cases hpt : oref = t + · subst hpt; obtain ⟨xt, hkt0, _, hkt1⟩ := hrel.kheap_t; rw [hkt1] at hk1; cases hk1 + rw [nd_bind_mem_iffA] at hrest + rcases hrest with ⟨caps, s2, hcaps, hopt⟩ + obtain ⟨rfl, rfl⟩ := nd_pure_mem_iffA.1 hcaps + obtain ⟨hcap_eq, rfl⟩ := nd_assertOpt_mem_iffA.1 hopt + have hcm : tcbCnodeMap (dissocTcbObjA xt) = tcbCnodeMap xt := rfl + rw [hcm] at hcap_eq + refine ⟨σ0, nd_bind_mem_iffA.2 ⟨.tcb xt, σ0, (getObject_mem_iffA oref σ0 σ0 _).2 ⟨hkt0, rfl⟩, ?_⟩, hrel⟩ + rw [nd_bind_mem_iffA] + exact ⟨tcbCnodeMap xt, σ0, nd_pure_mem_iffA.2 ⟨rfl, rfl⟩, nd_assertOpt_mem_iffA.2 ⟨hcap_eq, rfl⟩⟩ + · have hk0 : σ0.kheap oref = some o := by rw [← hrel.kheap_ne oref hpv hpt, hk1] + cases o with + | cnode sz cn => + simp only [nd_bind_mem_iffA, nd_assert_mem_iffA, nd_pure_mem_iffA, nd_assertOpt_mem_iffA] at hrest + rcases hrest with ⟨⟨⟩, s1', ⟨hwf', rfl⟩, caps, s2, ⟨rfl, rfl⟩, hcap_eq, rfl⟩ + refine ⟨σ0, nd_bind_mem_iffA.2 ⟨.cnode sz caps, σ0, (getObject_mem_iffA oref σ0 σ0 _).2 ⟨hk0, rfl⟩, ?_⟩, hrel⟩ + simp only [nd_bind_mem_iffA, nd_assert_mem_iffA, nd_pure_mem_iffA, nd_assertOpt_mem_iffA] + exact ⟨(), σ0, ⟨hwf', rfl⟩, caps, σ0, ⟨rfl, rfl⟩, hcap_eq, rfl⟩ + | tcb x => + rw [nd_bind_mem_iffA] at hrest + rcases hrest with ⟨caps, s2, hcaps, hopt⟩ + obtain ⟨rfl, rfl⟩ := nd_pure_mem_iffA.1 hcaps + obtain ⟨hcap_eq, rfl⟩ := nd_assertOpt_mem_iffA.1 hopt + refine ⟨σ0, nd_bind_mem_iffA.2 ⟨.tcb x, σ0, (getObject_mem_iffA oref σ0 σ0 _).2 ⟨hk0, rfl⟩, ?_⟩, hrel⟩ + rw [nd_bind_mem_iffA] + exact ⟨tcbCnodeMap x, σ0, nd_pure_mem_iffA.2 ⟨rfl, rfl⟩, nd_assertOpt_mem_iffA.2 ⟨hcap_eq, rfl⟩⟩ + | _ => + rw [nd_bind_mem_iffA] at hrest; rcases hrest with ⟨_, _, hf, _⟩; exact False.elim hf + +-- Helpers for specific kernel operations used in `unbindNotification`, `unbindFromSc`, `suspend` + +private theorem dissocCommA_getSimpleKo {v t : ObjRef} {α : Type} [Nonempty α] + (f : α → KernelObject) (p : ObjRef) : + DissocCommA v t (getSimpleKo (Z := Unit) f p) := by + intro σ0 σ1 a σ1' hrel hm + unfold getSimpleKo at hm ⊢ + simp only [nd_bind_mem_iffA, getObject_mem_iffA, nd_assert_mem_iffA] at hm ⊢ + rcases hm with ⟨o, s1, ⟨hk1, rfl⟩, ⟨⟩, s2, ⟨hsimp, rfl⟩, hpi⟩ + have hpv : p ≠ v := fun e => by + subst e; obtain ⟨xv, _, _, hkv1⟩ := hrel.kheap_v; rw [hkv1] at hk1; cases hk1 + simp [isSimpleType, aType] at hsimp + have hpt : p ≠ t := fun e => by + subst e; obtain ⟨xt, _, _, hkt1⟩ := hrel.kheap_t; rw [hkt1] at hk1; cases hk1 + simp [isSimpleType, aType] at hsimp + have hk0 : σ0.kheap p = some o := by rw [← hrel.kheap_ne p hpv hpt, hk1] + cases hpf : partialInv f o with + | none => rw [hpf] at hpi; exact False.elim hpi + | some val => + rw [hpf] at hpi + obtain ⟨rfl, rfl⟩ := nd_pure_mem_iffA.1 hpi + exact ⟨σ0, ⟨o, σ0, ⟨hk0, rfl⟩, (), σ0, ⟨hsimp, rfl⟩, by rw [hpf]; exact nd_pure_mem_iffA.2 ⟨rfl, rfl⟩⟩, hrel⟩ + +private theorem dissocCommA_setSimpleKo {v t : ObjRef} {α : Type} [Nonempty α] + (f : α → KernelObject) (p : ObjRef) (x : α) : + DissocCommA v t (setSimpleKo (Z := Unit) f p x) := by + intro σ0 σ1 a σ1' hrel hm + unfold setSimpleKo at hm ⊢ + simp only [nd_bind_mem_iffA, getObject_mem_iffA, nd_assert_mem_iffA] at hm ⊢ + rcases hm with ⟨o, s1, ⟨hk1, rfl⟩, ⟨⟩, s2, ⟨hsimp, rfl⟩, ⟨⟩, s3, ⟨hpi, rfl⟩, hset⟩ + have hpv : p ≠ v := fun e => by + subst e; obtain ⟨xv, _, _, hkv1⟩ := hrel.kheap_v; rw [hkv1] at hk1; cases hk1 + simp [isSimpleType, aType] at hsimp + have hpt : p ≠ t := fun e => by + subst e; obtain ⟨xt, _, _, hkt1⟩ := hrel.kheap_t; rw [hkt1] at hk1; cases hk1 + simp [isSimpleType, aType] at hsimp + have hk0 : σ0.kheap p = some o := by rw [← hrel.kheap_ne p hpv hpt, hk1] + obtain ⟨σ0', hset0, hrel'⟩ := dissocCommA_setObject_ne hpv hpt (f x) σ0 s3 a σ1' hrel hset + exact ⟨σ0', ⟨o, σ0, ⟨hk0, rfl⟩, (), σ0, ⟨hsimp, rfl⟩, (), σ0, ⟨hpi, rfl⟩, hset0⟩, hrel'⟩ + +private theorem dissocCommA_getNotification {v t : ObjRef} (p : ObjRef) : + DissocCommA v t (getNotification (Z := Unit) p) := + dissocCommA_getSimpleKo KernelObject.notification p + +private theorem dissocCommA_getEndpoint {v t : ObjRef} (p : ObjRef) : + DissocCommA v t (getEndpoint (Z := Unit) p) := + dissocCommA_getSimpleKo KernelObject.endpoint p + +private theorem dissocCommA_getReply {v t : ObjRef} (p : ObjRef) : + DissocCommA v t (getReply (Z := Unit) p) := + dissocCommA_getSimpleKo KernelObject.reply p + +private theorem dissocCommA_setNotification {v t : ObjRef} (p : ObjRef) (n : Notification) : + DissocCommA v t (setNotification (Z := Unit) p n) := + dissocCommA_setSimpleKo KernelObject.notification p n + +private theorem dissocCommA_setEndpoint {v t : ObjRef} (p : ObjRef) (e : Endpoint) : + DissocCommA v t (setEndpoint (Z := Unit) p e) := + dissocCommA_setSimpleKo KernelObject.endpoint p e + +private theorem dissocCommA_setReply {v t : ObjRef} (p : ObjRef) (r : Reply) : + DissocCommA v t (setReply (Z := Unit) p r) := + dissocCommA_setSimpleKo KernelObject.reply p r + +private theorem dissocCommA_getSchedContext {v t : ObjRef} (p : ObjRef) : + DissocCommA v t (getSchedContext (Z := Unit) p) := by + intro σ0 σ1 a σ1' hrel hm + unfold getSchedContext at hm ⊢ + simp only [nd_bind_mem_iffA, getObject_mem_iffA] at hm ⊢ + rcases hm with ⟨o, s1, ⟨hk1, rfl⟩, h2⟩ + cases o with + | schedContext sc sz => + obtain ⟨rfl, rfl⟩ := nd_pure_mem_iffA.1 h2 + have hpv : p ≠ v := fun e => by subst e; obtain ⟨xv, _, _, hkv1⟩ := hrel.kheap_v; rw [hkv1] at hk1; cases hk1 + have hpt : p ≠ t := fun e => by subst e; obtain ⟨xt, _, _, hkt1⟩ := hrel.kheap_t; rw [hkt1] at hk1; cases hk1 + have hk0 : σ0.kheap p = some (.schedContext a sz) := by rw [← hrel.kheap_ne p hpv hpt, hk1] + exact ⟨σ0, ⟨.schedContext a sz, σ0, ⟨hk0, rfl⟩, nd_pure_mem_iffA.2 ⟨rfl, rfl⟩⟩, hrel⟩ + | _ => exact False.elim h2 + +private theorem dissocCommA_updateSchedContext {v t : ObjRef} (p : ObjRef) (f : SchedContext → SchedContext) : + DissocCommA v t (updateSchedContext (Z := Unit) p f) := by + intro σ0 σ1 a σ1' hrel hm + unfold updateSchedContext setSchedContext at hm ⊢ + simp only [nd_bind_mem_iffA, getObject_mem_iffA] at hm ⊢ + rcases hm with ⟨o, s1, ⟨hk1, rfl⟩, hset⟩ + cases o with + | schedContext oldSc sz => + have hpv : p ≠ v := fun e => by subst e; obtain ⟨xv, _, _, hkv1⟩ := hrel.kheap_v; rw [hkv1] at hk1; cases hk1 + have hpt : p ≠ t := fun e => by subst e; obtain ⟨xt, _, _, hkt1⟩ := hrel.kheap_t; rw [hkt1] at hk1; cases hk1 + obtain ⟨σ0', hset0, hrel'⟩ := dissocCommA_setObject_ne hpv hpt (.schedContext (f oldSc) sz) σ0 s1 a σ1' hrel hset + have hk0 : σ0.kheap p = some (.schedContext oldSc sz) := by rw [← hrel.kheap_ne p hpv hpt, hk1] + exact ⟨σ0', ⟨.schedContext oldSc sz, σ0, ⟨hk0, rfl⟩, hset0⟩, hrel'⟩ + | _ => exact False.elim hset + +private theorem dissocCommA_tcbSchedAction {v t : ObjRef} (act : ObjRef → List ObjRef → List ObjRef) (p : ObjRef) : + DissocCommA v t (tcbSchedAction (Z := Unit) act p) := by + unfold tcbSchedAction getTcbQueue setTcbQueue + refine dissocCommA_bind (dissocCommA_threadGet Tcb.tcbDomain (fun _ => rfl) p) fun d => + dissocCommA_bind (dissocCommA_threadGet Tcb.tcbPriority (fun _ => rfl) p) fun prio => + dissocCommA_bind (dissocCommA_bind (dissocCommA_gets (v := v) (t := t) (fun s => s.readyQueues) fun _ _ h => h.readyQueues) + fun queues => dissocCommA_pure (queues d prio)) fun q => + dissocCommA_bind (dissocCommA_pure (act p q)) fun q' => + dissocCommA_modify_sched (fun es => { es with readyQueues := fun d' p' => if d' = d ∧ p' = prio then q' else es.readyQueues d' p' }) + fun σ0 σ1 h => { h with readyQueues := by dsimp only; rw [h.readyQueues] } + +private theorem dissocCommA_tcbReleaseRemove {v t : ObjRef} (p : ObjRef) : + DissocCommA v t (tcbReleaseRemove (Z := Unit) p) := by + unfold tcbReleaseRemove setReleaseQueue + refine dissocCommA_bind (dissocCommA_gets (fun s => s.releaseQueue) fun _ _ h => h.releaseQueue) fun qs => + dissocCommA_bind (dissocCommA_when _ (dissocCommA_modify_sched _ fun _ _ h => { h with reprogramTimer := rfl })) fun _ => + dissocCommA_bind (dissocCommA_pure _) fun qs' => + dissocCommA_modify_sched _ fun _ _ h => { h with releaseQueue := rfl } + +private theorem dissocCommA_schedulable_eq {v t : ObjRef} {σ0 σ1 : State Unit} + (h : DissocRelA v t σ0 σ1) (p : ObjRef) : schedulable p σ1 = schedulable p σ0 := by + have hsc : scsOf σ1 = scsOf σ0 := by + funext q; unfold scsOf scsOfKh optMap scOf + by_cases hqv : q = v + · subst hqv; obtain ⟨xv, hkv0, _, hkv1⟩ := h.kheap_v; simp [hkv0, hkv1] + · by_cases hqt : q = t + · subst hqt; obtain ⟨xt, hkt0, _, hkt1⟩ := h.kheap_t; simp [hkt0, hkt1] + · simp [h.kheap_ne q hqv hqt] + unfold schedulable + apply decide_eq_decide.2 + rw [h.releaseQueue, hsc] + unfold optPred optMapSome tcbsOf tcbsOfKh optMap tcbOf + by_cases hpv : p = v + · subst hpv; obtain ⟨xv, hkv0, _, hkv1⟩ := h.kheap_v; simp [hkv0, hkv1] + · by_cases hpt : p = t + · subst hpt; obtain ⟨xt, hkt0, _, hkt1⟩ := h.kheap_t + simp only [hkt0, hkt1, dissocTcbObjA] + · rw [h.kheap_ne p hpv hpt] + +private theorem dissocCommA_rescheduleRequired {v t : ObjRef} : + DissocCommA v t (rescheduleRequired (Z := Unit)) := by + have hset : DissocCommA v t (setSchedulerAction (Z := Unit) .chooseNewThread) := by + unfold setSchedulerAction + exact dissocCommA_modify_sched _ fun _ _ h => { h with schedulerAction := rfl } + unfold rescheduleRequired + refine dissocCommA_bind (dissocCommA_gets (fun s => s.schedulerAction) fun _ _ h => h.schedulerAction) fun act => ?_ + cases act with + | switchThread p => + refine dissocCommA_bind (dissocCommA_gets (schedulable p) fun _ _ h => dissocCommA_schedulable_eq h p) fun sc => + dissocCommA_bind (dissocCommA_when _ <| + dissocCommA_bind (dissocCommA_threadGet Tcb.tcbSchedContext (fun _ => rfl) p) fun scOpt => + dissocCommA_bind (dissocCommA_assertOpt scOpt) fun scp => + dissocCommA_bind ?_ fun suf => + dissocCommA_bind ?_ fun rdy => + dissocCommA_bind (dissocCommA_assert _) fun _ => + dissocCommA_tcbSchedAction tcbSchedEnqueue p) fun _ => hset + · unfold getScRefillSufficient + refine dissocCommA_getsThe _ fun σ0 σ1 hrel => ?_ + have hlb : ∀ {α β : Type} (m : Lookup (State Unit) α) (f : α → Lookup (State Unit) β) (s : State Unit), + (m >>= f) s = Option.bind (m s) (fun a => f a s) := fun _ _ _ => rfl + have hsc_eq : readSchedContext (Z := Unit) scp σ1 = readSchedContext (Z := Unit) scp σ0 := by + unfold readSchedContext readObject + rw [hlb, hlb] + by_cases hpv : scp = v + · subst hpv; obtain ⟨xv, hkv0, _, hkv1⟩ := hrel.kheap_v; rw [hkv0, hkv1]; rfl + · by_cases hpt : scp = t + · subst hpt; obtain ⟨xt, hkt0, _, hkt1⟩ := hrel.kheap_t; rw [hkt0, hkt1]; rfl + · rw [hrel.kheap_ne scp hpv hpt] + cases σ0.kheap scp with + | none => rfl + | some obj => cases obj <;> rfl + have hrh : readRefillHead (Z := Unit) scp σ1 = readRefillHead (Z := Unit) scp σ0 := by + unfold readRefillHead + rw [hlb, hlb, hsc_eq] + cases readSchedContext (Z := Unit) scp σ0 with + | none => rfl + | some sc => + dsimp only [Option.bind, Lookup.oassert] + by_cases hne : sc.scRefills ≠ [] + · simp [hne, hlb, Lookup.oreturn] + · simp [hne, hlb, Lookup.ofail] + unfold readScRefillSufficient + rw [hlb, hlb, hrh] + cases readRefillHead (Z := Unit) scp σ0 <;> rfl + · unfold getScRefillReady + refine dissocCommA_getsThe _ fun σ0 σ1 hrel => ?_ + have hlb : ∀ {α β : Type} (m : Lookup (State Unit) α) (f : α → Lookup (State Unit) β) (s : State Unit), + (m >>= f) s = Option.bind (m s) (fun a => f a s) := fun _ _ _ => rfl + have hsc_eq : readSchedContext (Z := Unit) scp σ1 = readSchedContext (Z := Unit) scp σ0 := by + unfold readSchedContext readObject + rw [hlb, hlb] + by_cases hpv : scp = v + · subst hpv; obtain ⟨xv, hkv0, _, hkv1⟩ := hrel.kheap_v; rw [hkv0, hkv1]; rfl + · by_cases hpt : scp = t + · subst hpt; obtain ⟨xt, hkt0, _, hkt1⟩ := hrel.kheap_t; rw [hkt0, hkt1]; rfl + · rw [hrel.kheap_ne scp hpv hpt] + cases σ0.kheap scp with + | none => rfl + | some obj => cases obj <;> rfl + have hrh : readRefillHead (Z := Unit) scp σ1 = readRefillHead (Z := Unit) scp σ0 := by + unfold readRefillHead + rw [hlb, hlb, hsc_eq] + cases readSchedContext (Z := Unit) scp σ0 with + | none => rfl + | some sc => + dsimp only [Option.bind, Lookup.oassert] + by_cases hne : sc.scRefills ≠ [] + · simp [hne, hlb, Lookup.oreturn] + · simp [hne, hlb, Lookup.ofail] + unfold readScRefillReady + rw [hlb, hlb, hrh] + cases readRefillHead (Z := Unit) scp σ0 with + | none => rfl + | some rh => + dsimp only [Option.bind] + rw [hlb, hlb] + simp [Lookup.asks, Lookup.oreturn, hrel.curTime] + | _ => exact hset + +private theorem dissocCommA_setMrs {v t : ObjRef} (p : ObjRef) (buf : Option ObjRef) (msgs : List Message) : + DissocCommA v t (setMrs (Z := Unit) p buf msgs) := by + have hnot_spsr : Register.spsrEl1 ∉ msg_registers.take msgs.length := by + intro hmem + have hmem' := List.mem_of_mem_take hmem + simp [msg_registers, msgRegisters, Register.x2ToX5] at hmem' + let updMrs : Tcb → Tcb := fun tcb => + let newRegs := fun reg => + if reg ∈ msg_registers.take msgs.length + then the msgs[theIndex msg_registers reg]? + else archTcbGetRegisters tcb.tcbArch reg + { tcb with tcbArch := archTcbSetRegisters newRegs tcb.tcbArch } + have hmain : ∀ (l1 : List Nat) (l2 : List Word) (pptr : ObjRef), + DissocCommA v t (NonDet.zipWithMx (σ := State Unit) (fun x => storeWordOffs pptr x) l1 l2) := by + intro l1; induction l1 with + | nil => intro l2 _; cases l2 <;> exact dissocCommA_pure () + | cons a l1 ih => + intro l2 pptr; cases l2 with + | nil => exact dissocCommA_pure () + | cons b l2 => exact dissocCommA_bind (dissocCommA_storeWordOffs pptr a b) fun _ => ih l2 pptr + have hsplit : setMrs (Z := Unit) p buf msgs = + (threadSet updMrs p >>= fun _ => + match buf with + | none => pure (natToLen (min msg_registers.length msgs.length)) + | some pptr => do + NonDet.zipWithMx (fun x => storeWordOffs pptr x) + (List.range' (msg_registers.length + 1) (msgMaxLength + 1 - (msg_registers.length + 1))) + (msgs.drop msg_registers.length) + pure (natToLen (min msgs.length msgMaxLength))) := by + funext s + unfold setMrs threadSet + simp only [nd_bind_assoc_funA] + refine nd_bind_congr_rightA _ _ _ (fun tcb s1 => ?_) s + rw [nd_bind_pure_leftA, nd_bind_pure_leftA] + refine nd_bind_congr_rightA _ _ _ (fun _ s2 => ?_) s1 + rw [nd_bind_pure_leftA] + cases buf <;> rfl + rw [hsplit] + refine dissocCommA_bind (dissocCommA_threadSet updMrs (fun x => ?_) (fun _ => rfl) p) fun _ => ?_ + · have hreg_eq : (updMrs (dissocTcbObjA x)).tcbArch.tcbContext.userRegs = + (dissocTcbObjA (updMrs x)).tcbArch.tcbContext.userRegs := by + funext reg + by_cases hreg : reg ∈ msg_registers.take msgs.length + · have hne : reg ≠ .spsrEl1 := fun e => hnot_spsr (e ▸ hreg) + simp [updMrs, dissocTcbObjA, archTcbSetRegisters, archTcbGetRegisters, Function.update, hreg, hne] + · by_cases hrs : reg = .spsrEl1 + · subst hrs; simp [updMrs, dissocTcbObjA, archTcbSetRegisters, archTcbGetRegisters, Function.update, hnot_spsr] + · simp [updMrs, dissocTcbObjA, archTcbSetRegisters, archTcbGetRegisters, Function.update, hreg, hrs] + dsimp only [updMrs, dissocTcbObjA, archTcbSetRegisters, archTcbGetRegisters] at hreg_eq ⊢ + rw [hreg_eq] + · cases buf with + | none => exact dissocCommA_pure _ + | some pptr => exact dissocCommA_bind (hmain _ _ pptr) fun _ => dissocCommA_pure _ + +private theorem dissocCommA_unbindNotification {v t : ObjRef} (p : ObjRef) : + DissocCommA v t (unbindNotification (Z := Unit) p) := by + unfold unbindNotification getTcbObjRef + refine dissocCommA_bind (dissocCommA_threadGet Tcb.tcbBoundNotification (fun _ => rfl) p) fun nopt => + dissocCommA_maybeM (fun nptr => ?_) nopt + unfold doUnbindNotification setNtfnObjRef updateSkObjRef setTcbObjRef + exact dissocCommA_bind (dissocCommA_bind (dissocCommA_getNotification nptr) fun _ => + dissocCommA_setNotification nptr _) fun _ => + dissocCommA_threadSet (tcbBoundNotificationUpdate fun _ => none) (fun _ => rfl) (fun _ => rfl) p + +private theorem dissocCommA_schedContextCancelYieldTo {v t : ObjRef} (p : ObjRef) : + DissocCommA v t (schedContextCancelYieldTo (Z := Unit) p) := by + unfold schedContextCancelYieldTo getTcbObjRef + refine dissocCommA_bind (dissocCommA_threadGet Tcb.tcbYieldTo (fun _ => rfl) p) fun yopt => + dissocCommA_maybeM (fun scPtr => ?_) yopt + unfold setScObjRef setTcbObjRef + exact dissocCommA_bind (dissocCommA_updateSchedContext scPtr _) fun _ => + dissocCommA_threadSet (tcbYieldToUpdate fun _ => none) (fun _ => rfl) (fun _ => rfl) p + +private theorem dissocCommA_unbindFromSc {v t : ObjRef} (p : ObjRef) : + DissocCommA v t (unbindFromSc (Z := Unit) p) := by + unfold unbindFromSc getTcbObjRef + refine dissocCommA_bind (dissocCommA_threadGet Tcb.tcbSchedContext (fun _ => rfl) p) fun scOpt => + dissocCommA_bind (dissocCommA_maybeM (fun scPtr => ?_) scOpt) fun _ => + dissocCommA_maybeM (fun scPtr => ?_) scOpt + · unfold schedContextUnbindTcb setTcbObjRef setScObjRef + refine dissocCommA_bind (dissocCommA_getSchedContext scPtr) fun sc => + dissocCommA_bind (dissocCommA_assertOpt sc.scTcb) fun tptr => + dissocCommA_bind (dissocCommA_gets (fun s => s.curThread) fun _ _ h => h.curThread) fun cur => + dissocCommA_bind (dissocCommA_when _ dissocCommA_rescheduleRequired) fun _ => + dissocCommA_bind (dissocCommA_tcbSchedAction tcbSchedDequeue tptr) fun _ => + dissocCommA_bind (dissocCommA_tcbReleaseRemove tptr) fun _ => + dissocCommA_bind (dissocCommA_threadSet (tcbSchedContextUpdate fun _ => none) (fun _ => rfl) (fun _ => rfl) tptr) fun _ => + dissocCommA_updateSchedContext scPtr _ + · refine dissocCommA_bind (dissocCommA_getSchedContext scPtr) fun sc => + dissocCommA_maybeM (fun yt => ?_) sc.scYieldFrom + unfold completeYieldTo getTcbObjRef + refine dissocCommA_bind (dissocCommA_threadGet Tcb.tcbYieldTo (fun _ => rfl) yt) fun yopt => + dissocCommA_maybeM (fun sc2 => ?_) yopt + refine dissocCommA_bind ?_ fun _ => dissocCommA_schedContextCancelYieldTo yt + unfold setConsumed schedContextUpdateConsumed setScObjRef replyFromKernel + lookupIpcBuffer setMessageInfo + refine dissocCommA_bind (dissocCommA_bind (dissocCommA_getSchedContext sc2) fun scObj => + dissocCommA_bind (dissocCommA_pure scObj.scConsumed) fun cons => by + split + · exact dissocCommA_bind (dissocCommA_updateSchedContext sc2 _) fun _ => dissocCommA_pure _ + · exact dissocCommA_bind (dissocCommA_updateSchedContext sc2 _) fun _ => dissocCommA_pure _) fun consumed => + dissocCommA_bind (dissocCommA_pure _) fun _ => + dissocCommA_bind (dissocCommA_bind (dissocCommA_threadGet Tcb.tcbIpcBuffer (fun _ => rfl) yt) fun _ => + dissocCommA_bind (dissocCommA_pure _) fun _ => + dissocCommA_bind (dissocCommA_getCap _) fun cap => by + cases cap with + | archObjectCap ac => cases ac with + | frameCap _ _ _ dev _ => cases dev with + | false => dsimp only; split <;> exact dissocCommA_pure _ + | true => exact dissocCommA_pure _ + | _ => exact dissocCommA_pure _ + | _ => exact dissocCommA_pure _) fun buf => + dissocCommA_bind (dissocCommA_asUser_setReg yt badge_register (by decide) 0) fun _ => + dissocCommA_bind (dissocCommA_setMrs yt buf _) fun len => + dissocCommA_asUser_setReg yt msg_info_register (by decide) _ + +private theorem dissocCommA_setThreadState {v t : ObjRef} (p : ObjRef) (ts : ThreadState) : + DissocCommA v t (setThreadState (Z := Unit) p ts) := by + unfold setThreadState setThreadStateAct setSchedulerAction + exact dissocCommA_bind (dissocCommA_threadSet (fun tcb => { tcb with tcbState := ts }) (fun _ => rfl) (fun _ => rfl) p) fun _ => + dissocCommA_bind (dissocCommA_gets (fun s => s.curThread) fun _ _ h => h.curThread) fun _ => + dissocCommA_bind (dissocCommA_gets (fun s => s.schedulerAction) fun _ _ h => h.schedulerAction) fun _ => + dissocCommA_bind (dissocCommA_gets (schedulable p) fun _ _ h => dissocCommA_schedulable_eq h p) fun _ => + dissocCommA_when _ (dissocCommA_modify_sched _ fun _ _ h => { h with schedulerAction := rfl }) + +private theorem dissocCommA_replyUnlinkTcb {v t : ObjRef} (p r : ObjRef) : + DissocCommA v t (replyUnlinkTcb (Z := Unit) p r) := by + unfold replyUnlinkTcb getThreadState setReplyObjRef updateSkObjRef + refine dissocCommA_bind (dissocCommA_getReply r) fun rep => + dissocCommA_bind (dissocCommA_assert _) fun _ => + dissocCommA_bind (dissocCommA_threadGet Tcb.tcbState (fun _ => rfl) p) fun ts => + dissocCommA_bind (dissocCommA_assert _) fun _ => + dissocCommA_bind (dissocCommA_bind (dissocCommA_getReply r) fun rep2 => + dissocCommA_setReply r _) fun _ => + dissocCommA_setThreadState p .inactive + +private theorem dissocCommA_suspend {v t : ObjRef} (p : ObjRef) : + DissocCommA v t (suspend (Z := Unit) p) := by + unfold suspend cancelIpc getThreadState + refine dissocCommA_bind (dissocCommA_bind (dissocCommA_threadGet Tcb.tcbState (fun _ => rfl) p) fun st => + dissocCommA_bind (dissocCommA_threadSet (fun tcb => { tcb with tcbFault := none }) (fun _ => rfl) (fun _ => rfl) p) fun _ => ?_) fun _ => + dissocCommA_bind (dissocCommA_threadGet Tcb.tcbState (fun _ => rfl) p) fun st2 => + dissocCommA_bind (dissocCommA_when _ ?_) fun _ => + dissocCommA_bind (dissocCommA_tcbSchedAction tcbSchedDequeue p) fun _ => + dissocCommA_bind (dissocCommA_tcbReleaseRemove p) fun _ => + dissocCommA_bind (dissocCommA_schedContextCancelYieldTo p) fun _ => + dissocCommA_setThreadState p .inactive + · cases st with + | blockedOnSend ep _ => + unfold blockedCancelIpc getBlockingObject tcbEpDequeue + refine dissocCommA_bind (dissocCommA_assertOpt _) fun epptr => + dissocCommA_bind (dissocCommA_bind (dissocCommA_getEndpoint epptr) fun e => + dissocCommA_bind (dissocCommA_pure _) fun _ => + dissocCommA_bind (dissocCommA_assert _) fun _ => + dissocCommA_bind (dissocCommA_pure _) fun _ => + dissocCommA_bind (dissocCommA_pure _) fun e' => + dissocCommA_setEndpoint epptr e') fun _ => + dissocCommA_setThreadState p .inactive + | blockedOnReceive ep ropt _ => + unfold blockedCancelIpc getBlockingObject tcbEpDequeue + refine dissocCommA_bind (dissocCommA_assertOpt _) fun epptr => + dissocCommA_bind (dissocCommA_bind (dissocCommA_getEndpoint epptr) fun e => + dissocCommA_bind (dissocCommA_pure _) fun _ => + dissocCommA_bind (dissocCommA_assert _) fun _ => + dissocCommA_bind (dissocCommA_pure _) fun _ => + dissocCommA_bind (dissocCommA_pure _) fun e' => + dissocCommA_setEndpoint epptr e') fun _ => ?_ + cases ropt with + | none => exact dissocCommA_setThreadState p .inactive + | some r => exact dissocCommA_bind (dissocCommA_replyUnlinkTcb p r) fun _ => + dissocCommA_setThreadState p .inactive + | blockedOnNotification nptr => + unfold cancelSignal tcbNtfnDequeue + refine dissocCommA_bind (dissocCommA_bind (dissocCommA_getNotification nptr) fun n => + dissocCommA_bind (dissocCommA_pure _) fun _ => + dissocCommA_bind (dissocCommA_assert _) fun _ => + dissocCommA_bind (dissocCommA_pure _) fun _ => + dissocCommA_bind (dissocCommA_pure _) fun n' => + dissocCommA_setNotification nptr n') fun _ => + dissocCommA_setThreadState p .inactive + | blockedOnReply rptr => + unfold replyRemoveTcb getThreadState getReplyObjRef setReplyObjRef updateSkObjRef + refine dissocCommA_bind (dissocCommA_threadGet Tcb.tcbState (fun _ => rfl) p) fun _ => + dissocCommA_bind (dissocCommA_assert _) fun _ => + dissocCommA_bind (dissocCommA_gets (scWithReply rptr) fun σ0 σ1 h => by + unfold scWithReply; congr 1; funext scPtr + by_cases hpv : scPtr = v + · subst hpv; obtain ⟨xv, hkv0, _, hkv1⟩ := h.kheap_v; simp [hkv0, hkv1] + · by_cases hpt : scPtr = t + · subst hpt; obtain ⟨xt, hkt0, _, hkt1⟩ := h.kheap_t; simp [hkt0, hkt1] + · simp [h.kheap_ne scPtr hpv hpt]) fun scOpt => ?_ + cases scOpt with + | none => exact dissocCommA_replyUnlinkTcb p rptr + | some scPtr => + exact dissocCommA_bind (dissocCommA_liftM _ (dissocCommA_getSchedContext scPtr)) fun _ => + dissocCommA_bind (dissocCommA_liftM _ (dissocCommA_getReply rptr)) fun _ => + dissocCommA_bind (dissocCommA_assert _) fun _ => + dissocCommA_bind (dissocCommA_updateSchedContext scPtr _) fun _ => + dissocCommA_bind (dissocCommA_bind (dissocCommA_getReply rptr) fun _ => + dissocCommA_setReply rptr _) fun _ => + dissocCommA_replyUnlinkTcb p rptr + | _ => exact dissocCommA_pure () + · exact dissocCommA_updateRestartPc p + +/-- Moving `dissociateVcpuTcb v t` (for `t`'s bound VCPU `v`) from before +`unbindNotification t >> unbindFromSc t >> suspend t` to after it (matching +`fl4FinaliseCap (.threadCap t) true`'s `prepareThreadDelete t` position after +`af8c696e2`, which already places `fl4CancelRun t` and `fl4VcpuEndRun v` before +`finaliseCap (.threadCap t) true`). -/ +theorem fl4Dissociate_comm_tcbFinA {σ σ1 σ2 : Fl4SpecA64.State Unit} {t v : ObjRef} + (htv : ∃ xt, σ.kheap t = some (.tcb xt) ∧ xt.tcbArch.tcbVcpu = some v) + (hvt : ∃ xv, σ.kheap v = some (.archObj (.vcpu xv)) ∧ xv.vcpuTcb = some t) + (h1 : ((), σ1) ∈ (dissociateVcpuTcb (Z := Unit) v t σ).1) + (h2 : ((), σ2) ∈ (((do unbindNotification t; unbindFromSc t; suspend t) : SMonad Unit Unit) σ1).1) : + ((), σ2) ∈ (((do unbindNotification t; unbindFromSc t; suspend t; dissociateVcpuTcb v t) : SMonad Unit Unit) σ).1 := by + rcases htv with ⟨xt, hkt, htv'⟩ + rcases hvt with ⟨xv, hkv, hvt'⟩ + have hrel := dissocRelA_of_mem hkt hkv htv' hvt' h1 + have hcomm : DissocCommA v t (do unbindNotification (Z := Unit) t; unbindFromSc t; suspend t) := + dissocCommA_bind (dissocCommA_unbindNotification t) fun _ => + dissocCommA_bind (dissocCommA_unbindFromSc t) fun _ => + dissocCommA_suspend t + obtain ⟨σ0', h02, hrel'⟩ := hcomm σ σ1 () σ2 hrel h2 + have hdiss := dissocRelA_to_mem hrel' + have hassoc : ((do unbindNotification (Z := Unit) t; unbindFromSc t; suspend t; dissociateVcpuTcb v t) σ) = + (((do unbindNotification (Z := Unit) t; unbindFromSc t; suspend t) >>= fun _ => dissociateVcpuTcb v t) σ) := by + simp only [nd_bind_assoc_funA] + rw [hassoc, nd_bind_mem_iffA] + exact ⟨(), σ0', h02, hdiss⟩ + +end + +end Fl4ProofsA64.P3.Vcpu diff --git a/proofs/Fl4ProofsA64/P3/VcpuDelFinHookA.lean b/proofs/Fl4ProofsA64/P3/VcpuDelFinHookA.lean new file mode 100644 index 000000000..643cfd636 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/VcpuDelFinHookA.lean @@ -0,0 +1,286 @@ +/- +P3 (AArch64, S4 VCPU delete-hooks plug-in): **the VCPU finaliser hook (`FinHookKA .vcpu`)**, the `vcpuHook` +field of `Stage3DelAsmA`'s `DelS4A` (built against `par/a64-delhooks` `ba9ddb574`). + +* **`vcpuHookA`**: `FinHookKA st .vcpu ar Z.th OS fl4FinaliseCap (DelWTopA …) (DelWxTopA …) + (DelRdXA … (RvVcA st OS fun _ _ _ _ _ => True)) (DelRzA st)`, discharging `vc ≠ 0#u64` directly from + `Std6GA.heapWin` (`layout.PPTR_BASE.val ≤ vc.val`). + +Named premises: `DelPlatA` ([P] + `numNodes1` [S]) and `hCs` (`∀ c ∈ Z.Cs, c.val < numNodes`). New module. +-/ +import Fl4ProofsA64.P3.VcpuDelCommA +import Fl4ProofsA64.P3.VcpuDelHookA +import Fl4ProofsA64.P3.Stage3DelAsmA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3.Vcpu +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3 Fl4ProofsA64.P3.Endp + Fl4ProofsA64.P3.Notif Fl4ProofsA64.P3.Sched Fl4Spec Fl4SpecA64 Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + +/-! ### Small spec helpers for `fl4FinaliseCap (.archObjectCap (.vcpuCap _))` and `curTime` -/ + +private theorem rngKey_vcpuA (p : U64) : RngKeyA (.Vcpu p) := by + have h := physA_eq (.Vcpu p) + simp only [objects.cap_is_physical] at h + exact ⟨(okinj h).symm, rfl, rfl⟩ + +private theorem ptrA_vcpuA (p : U64) : ptrA (.Vcpu p) = p := by + have h := ptrA_eq (.Vcpu p) + simp only [capability.Cap.ptr] at h + exact (okinj h).symm + +private theorem of_kindA_vcpuA {c : capability.Cap} (h : kindA c = kindA (.Vcpu 0#u64)) : + ∃ p, c = .Vcpu p := by + cases c <;> simp only [kindA] at h <;> first | exact ⟨_, rfl⟩ | omega + +private theorem dissociateVcpuTcb_curTimeA {v t : ObjRef} {σ σ' : State Unit} + (h : ((), σ') ∈ (dissociateVcpuTcb (Z := Unit) v t σ).1) : σ'.curTime = σ.curTime := by + have h1 := h + unfold dissociateVcpuTcb at h1 + obtain ⟨⟨tv, s0⟩, ha, hb⟩ := mem_bindA.1 h1 + have hkt : ∃ xt, σ.kheap t = some (.tcb xt) ∧ tv = xt.tcbArch.tcbVcpu ∧ s0 = σ := by + unfold archThreadGet NonDet.getsThe getTcb readObject at ha + obtain ⟨⟨xt, sa⟩, hc, hd⟩ := mem_bindA.1 ha + unfold NonDet.assertOpt at hc + cases hk : σ.kheap t with + | none => rw [hk] at hc; simp [NonDet.fail] at hc + | some ko => + cases ko with + | tcb xt0 => + rw [hk] at hc; simp [NonDet.pure] at hc; rcases hc with ⟨rfl, rfl⟩ + simp [Pure.pure, NonDet.pure] at hd; rcases hd with ⟨rfl, rfl⟩ + exact ⟨xt, rfl, rfl, rfl⟩ + | _ => rw [hk] at hc; simp [NonDet.fail] at hc + obtain ⟨xt, hkt, htv0, hs0⟩ := hkt + rw [htv0, hs0] at hb + obtain ⟨⟨xv, s1⟩, hc, hd⟩ := mem_bindA.1 hb + have hkv : σ.kheap v = some (.archObj (.vcpu xv)) ∧ s1 = σ := by + unfold getVcpu NonDet.getsMap at hc + obtain ⟨⟨_, sa⟩, he, hf⟩ := mem_bindA.1 hc + simp [NonDet.gets] at he; rcases he with ⟨rfl, hsa⟩ + rw [hsa] at hf; dsimp only at hf + simp only [NonDet.assertOpt, vcpusOf, aobjsOf, optMap, vcpuOf, aobjOf] at hf + cases hk : σ.kheap v with + | none => rw [hk] at hf; simp [NonDet.fail] at hf + | some ko => + cases ko with + | archObj ao => + cases ao with + | vcpu xv0 => + rw [hk] at hf; simp [NonDet.pure] at hf; rcases hf with ⟨rfl, rfl⟩ + exact ⟨rfl, rfl⟩ + | _ => rw [hk] at hf; simp [NonDet.fail] at hf + | _ => rw [hk] at hf; simp [NonDet.fail] at hf + obtain ⟨hkv, hs1⟩ := hkv + rw [hs1] at hd + obtain ⟨⟨_, s2⟩, he, _⟩ := mem_bindA.1 hd + unfold NonDet.assert at he + dsimp only at he + obtain ⟨ms', -, rfl⟩ := (dissociateVcpuTcb_mem_iffA hkt hkv he.1.1 he.1.2).1 h + rfl + +private theorem fl4VcpuUnbind_curTimeA {v : ObjRef} {σ σ' : State Unit} + (h : fl4VcpuUnbind v σ = ({((), σ')}, False)) : σ'.curTime = σ.curTime := by + have hmem : ((), σ') ∈ (fl4VcpuUnbind v σ).1 := by rw [h]; exact Set.mem_singleton _ + unfold fl4VcpuUnbind at hmem + obtain ⟨⟨_, s1⟩, h1, h2⟩ := mem_bindA.1 hmem + have e1 := fl4VcpuEndRun_curTimeA h1 + obtain ⟨⟨xv, s2⟩, h3, h4⟩ := mem_bindA.1 h2 + have e2 : s2 = s1 := by + unfold getVcpu NonDet.getsMap at h3 + obtain ⟨⟨_, s0'⟩, ha, hb⟩ := mem_bindA.1 h3 + simp [NonDet.gets] at ha; rcases ha with ⟨rfl, hsa⟩ + rw [hsa] at hb; dsimp only at hb + unfold NonDet.assertOpt at hb + cases hvp : vcpusOf s1 v with + | none => rw [hvp] at hb; simp [NonDet.fail] at hb + | some _ => rw [hvp] at hb; simp [NonDet.pure] at hb; exact hb.2 + subst e2 + cases hv : xv.vcpuTcb with + | none => rw [hv] at h4; simp [Pure.pure, NonDet.pure] at h4; rw [h4, e1] + | some t => rw [hv] at h4; exact (dissociateVcpuTcb_curTimeA h4).trans e1 + +set_option maxHeartbeats 1600000 in +/-- (integration round 2) `fl4VcpuUnbind` keeps `TmA` (no SC binding or SC is written). -/ +theorem fl4VcpuUnbind_tmA (v : ObjRef) : NdTmA (fl4VcpuUnbind v) := by + unfold fl4VcpuUnbind + tm + +private theorem fl4FinaliseCap_vcpu_trueA {v : ObjRef} {σ σ' : State Unit} + (h : fl4VcpuUnbind v σ = ({((), σ')}, False)) : + ((Fl4SpecA64.Cap.nullCap, Fl4SpecA64.Cap.nullCap), σ') ∈ + (fl4FinaliseCap (Z := Unit) (.archObjectCap (.vcpuCap v)) true σ).1 := by + unfold fl4FinaliseCap + dsimp only + unfold NonDet.when + simp only [↓reduceIte] + have hmem : ((), σ') ∈ (fl4VcpuUnbind v σ).1 := by rw [h]; exact Set.mem_singleton _ + unfold fl4VcpuUnbind at hmem + obtain ⟨⟨_, s1⟩, h1, h2⟩ := mem_bindA.1 hmem + refine mem_bindA.2 ⟨((), s1), h1, ?_⟩ + show ((Fl4SpecA64.Cap.nullCap, Fl4SpecA64.Cap.nullCap), σ') ∈ + (archFinaliseCap (Z := Unit) (.vcpuCap v) true s1).1 + unfold archFinaliseCap + exact mem_bindA.2 ⟨((), σ'), h2, rfl⟩ + +private theorem fl4FinaliseCap_vcpu_falseA (v : ObjRef) (σ : State Unit) : + ((Fl4SpecA64.Cap.nullCap, Fl4SpecA64.Cap.nullCap), σ) ∈ + (fl4FinaliseCap (Z := Unit) (.archObjectCap (.vcpuCap v)) false σ).1 := by + unfold fl4FinaliseCap + dsimp only + unfold NonDet.when + simp only [Bool.false_eq_true, ↓reduceIte] + refine mem_bindA.2 ⟨((), σ), rfl, ?_⟩ + rfl + +/-! ### The VCPU finaliser hook (`FinHookKA .vcpu`) and `DelS4A` -/ + +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {κ : U64 → U64} {n : Nat} {m : McState} + {Clock : A → Fl4SpecA64.MachineState → Prop} + +variable {Ag : Set U64} + +/-- **The VCPU finaliser hook** (`DelS4A.vcpuHook` in `Stage3DelAsmA`). -/ +theorem vcpuHookA {ipa : U64} (s0 : S) + (P : DelPlatA st ar AR Clock) + (hCs : ∀ c ∈ Z.Cs, c.val < fl4A64Platform.numNodes) : + FinHookKA st .vcpu ar Z.th OS fl4FinaliseCap + (DelWTopGA st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m ar ipa Clock s0) + (DelWxTopGA st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m Clock s0) + (DelRdXA st ar Z H OS ipa (RvVcA st OS fun _ _ _ _ _ => True)) (DelRzA st) := by + intro s a σ G sl c ac f D hW hsl hc hk hg hac hf + obtain ⟨vc, rfl⟩ : ∃ vc, c = .Vcpu vc := by + cases c <;> simp [finKindA] at hk + exact ⟨_, rfl⟩ + have eac : ac = .archObjectCap (.vcpuCap (specPtr vc)) := hac.symm + obtain ⟨⟨hcore, ⟨⟨Q, R, hw, Ac, Bf, stack, J, X⟩, hPA, hZT, hBH, hCT, hFx⟩, hSV⟩, hVT, hCl, hCnt, hCE⟩ := hW + have hOSx : cslotAddrA sl ∈ OS := D.mem_slot hsl + have unread : ¬ S1aR2Reads (capability.Cap.Vcpu vc) := fun h => h + have hFinalIff : f = true ↔ isFinalCap' ac σ := hf trivial + rw [fin_cap_eq_obj (by simp)] + cases f + · -- Non-final VCPU cap: no-op; another OS slot holds `.Vcpu vc` for `RvVcA`. + have e : objects.finalise_cap_object st ar s a (.Vcpu vc) false = + ok ((capability.Cap.Null, capability.Cap.Null), s, a) := rfl + rw [e, WP.spec_ok] + have hnf : ¬ isFinalCap' (absCapA (.Vcpu vc)) σ := by + intro hfin + have : false = true := hFinalIff.2 (eac.symm ▸ hfin) + cases this + obtain ⟨w, hwOS, hne, -, pw, kw⟩ := other_of_nonfinalA D.toFinStA hsl hc (rngKey_vcpuA vc) hnf + obtain ⟨vc', hcapw⟩ := of_kindA_vcpuA kw + have hvc'_eq : vc' = vc := by + have := ptrA_vcpuA vc' + rw [← hcapw, pw, ptrA_vcpuA] at this + exact this.symm + rcases hvc'_eq.symm with rfl + have hRv : RvVcA st OS (fun _ _ _ _ _ => True) s a σ G (cslotAddrA sl) := + ⟨fun v hv => (by rw [hc] at hv; cases hv; exact Or.inr ⟨w, hwOS, hne, hcapw⟩), trivial⟩ + have hRd : DelRdA st (RvVcA st OS fun _ _ _ _ _ => True) s a σ G (cslotAddrA sl) := + ⟨fun _ _ h => (by rw [hc] at h; cases h), + fun _ _ h => (by rw [hc] at h; cases h), + fun _ _ => hRv⟩ + refine ⟨rfl, trivial, trivial, σ, G, + ⟨false, hFinalIff, eac ▸ fl4FinaliseCap_vcpu_falseA (specPtr vc) σ⟩, + D, hc, fun _ _ _ => Or.inl rfl, ?_, ?_, fun _ _ _ h => by cases h⟩ + · exact ⟨fun h => absurd (Or.inl rfl) h, fun _ => + ⟨⟨hcore, ⟨⟨Q, R, hw, Ac, Bf, stack, J.mapJ7A fun _ _ => trivial, X⟩, + hPA, hZT, hBH, hCT, hFx⟩, hSV⟩, hVT, hCl, hCnt, hCE⟩⟩ + · exact fun _ => ⟨⟨hRd, rdThA_of_ne fun _ h => by rw [hc] at h; cases h⟩, + m, J.j7.pinUnreadA (by rw [hc]; exact unread)⟩ + · -- Final VCPU cap: run `vcpu.finalise` via `vcpu_finalise_stage3TA`. + have e : objects.finalise_cap_object st ar s a (.Vcpu vc) true = (do + let (s1, a1) ← vcpu.finalise st ar s a vc + ok ((capability.Cap.Null, capability.Cap.Null), s1, a1)) := rfl + rw [e] + -- `vc ∈ Z.V` and `H vc = some (.other 12)` (hence `vc ≠ 0#u64` from `X.heapWin`). + obtain ⟨_, _, -, -, arm, -, vcCap, -, -, -, -, -, -, -, -⟩ := J.j7 + have hvcV : vc ∈ Z.V := vcCap _ hOSx vc hc + have hvc_nz : vc ≠ 0#u64 := by + have hvH := arm.vh vc hvcV + have hwin := (X.heapWin vc _ hvH).1 + intro h0 + rw [h0] at hwin + have h0v : (0#u64 : U64).val = 0 := rfl + have hpv : layout.PPTR_BASE.val = 18446603336221196288 := by unfold layout.PPTR_BASE; rfl + rw [h0v, hpv] at hwin + omega + have R6 : VcpuRec6A st { Z with G := G, Q := Q, R := R, hw := hw } OS Bf s σ := + { snap := X.vcSnap, vcSym1 := X.vcSym1, vcSym2 := X.vcSym2, + vcBack := X.vcBack, runBack := X.runBack, vcOS := X.vcOS, + frOff := X.frOff, userFr := X.userFr, frTh := X.frTh, bfSpec := X.bfSpec } + refine WP.spec_bind (vcpu_finalise_stage3TA J hSV R6 hCl + P.del.clockVm P.del.clockVcpuFlush P.del.clockVcpuFlushState P.del.clockFr + P.vcpuAgree P.vcpuKeeps P.vcpuVmRoot hCs P.numNodes1 hvcV hvc_nz) ?_ + rintro ⟨s', a'⟩ ⟨hw', σ', hsp, J', hSV', R6', hCl', hts, hlmt, hvc0, hvc24, hksb, hcpu, osk, fk⟩ + change WP.spec (ok ((capability.Cap.Null, capability.Cap.Null), s', a')) _ + rw [WP.spec_ok] + have caps : ∀ y ∈ OS, capA st s' y = capA st s y := fun y hy => osk.capA hy + have hc' : capA st s' (cslotAddrA sl) = .Vcpu vc := (caps _ hOSx).trans hc + have hD' : DStateA st Z.th OS s' σ' G := + DStateA.keepOSA (Z := { Z with G := G, Q := Q, R := R, hw := hw' }) D J' osk + have hX' : Std6GA st Ag Z H OS Bf s' σ' := + { utWin := X.utWin.keepA caps + devWin := X.devWin.keepA caps + vcBack := R6'.vcBack + runBack := R6'.runBack + vcSnap := R6'.snap + vcOS := X.vcOS + userFr := R6'.userFr + frTh := R6'.frTh + bfSpec := X.bfSpec + vcSym1 := R6'.vcSym1 + vcSym2 := R6'.vcSym2 + heapWin := X.heapWin + coresAll := X.coresAll + zwf := fun y hy p zt k e0 => X.zwf y hy p zt k ((caps y (D.sub y hy)).symm.trans e0) + frOff := FrOff.ks hksb (CapInv.keepO X.frOff osk) + frAg := FrAgA.keepP X.frAg fk } + have T : TmA σ σ' := fl4VcpuUnbind_tmA _ σ ((), σ') (by rw [hsp]; exact Set.mem_singleton _) + have hVT' : ValidMachineTimeA σ' := by + unfold ValidMachineTimeA at hVT ⊢ + rw [fl4VcpuUnbind_curTimeA hsp, hlmt] + exact hVT + have hvtNone : ∀ y, σ'.kheap (specPtr vc) = some (.archObj (.vcpu y)) → y.vcpuTcb = none := by + intro y hy + obtain ⟨y0, hy0, hvt, -⟩ := J'.j.2.1.obj vc hvcV + rw [hy0] at hy; cases hy + rw [hvt, hvc0]; rfl + have hRv : RvVcA st OS (fun _ _ _ _ _ => True) s' a' σ' G (cslotAddrA sl) := + ⟨fun v hv => (by rw [hc'] at hv; cases hv; exact Or.inl ⟨hvtNone, hvc24⟩), trivial⟩ + have hRd : DelRdA st (RvVcA st OS fun _ _ _ _ _ => True) s' a' σ' G (cslotAddrA sl) := + ⟨fun _ _ h => (by rw [hc'] at h; cases h), + fun _ _ h => (by rw [hc'] at h; cases h), + fun _ _ => hRv⟩ + have hWx' : DelWxTopGA st Ag AR Z H OS U Pp Tb Lv ia ie N E Rp κ n m Clock s0 s' a' σ' G := + ⟨⟨⟨hksb.trans hcore.1, hcpu.trans hcore.2⟩, + ⟨⟨Q, R, hw', Ac, Bf, stack, J'.mapJ7A fun _ _ => trivial, hX'⟩, + ParentAdjA.keepOS D.lk osk hPA, + J'.zombieNoVcpu, + bufHold_sameA st hBH caps, + ctOff_sameA st hCT hksb caps, + hFx.keepA st D.sub caps⟩, hSV'⟩, hVT', hCl', + by unfold CounterStepA rawNextA at hCnt ⊢; rw [T.2.1, T.2.2.1]; exact hCnt, + hCE.keepA T.2.2.2 (by rw [T.1]; exact BitVec.le_refl _)⟩ + refine ⟨rfl, trivial, trivial, σ', G, + ⟨true, hFinalIff, eac ▸ fl4FinaliseCap_vcpu_trueA hsp⟩, + hD', hc', fun y hy _ => Or.inl (caps y hy), + ⟨fun h => absurd (Or.inl rfl) h, fun _ => hWx'⟩, + fun _ => ⟨⟨hRd, rdThA_of_ne fun _ h => by rw [hc'] at h; cases h⟩, + m, J'.j7.pinUnreadA (by rw [hc']; exact unread)⟩, + fun _ _ _ h => by cases h⟩ + +end + +end + +end Fl4ProofsA64.P3.Vcpu diff --git a/proofs/Fl4ProofsA64/P3/VcpuDelFrameA.lean b/proofs/Fl4ProofsA64/P3/VcpuDelFrameA.lean new file mode 100644 index 000000000..d58769819 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/VcpuDelFrameA.lean @@ -0,0 +1,2133 @@ +/- +P3 (aarch64, A64-P3 S4 VCPU): **Shared `JStage3TA` framing core for VCPU deletion/detach**. + +Provides (rebased on record edit #6 `b2265caf8`): +- `J7FrA` + instances `j7FrA_j7A` / `j7FrA_j7LA` (Architect ruling C(2): projects `FrameOK`, + `FrUb`, `UbFr`, `VcW_off_Ub`, `link`, and `keepFr` from `J7A`/`J7LA`, leaving `J7PpA` unchanged). +- `VcpuRec6A`: bundles the #6 per-step facts carried alongside `JStage3TA` (`VcpuStateSnapA`, + `VcSym1A`, `VcSym2A`, `vcBack`, `runBack`, `vcOS`, `UserFrSpecA`, `bfSpec`). +- `VcHdrWrA` / `VcpuWrNoStA` / `VcpuSpecStepA`: exact write footprint (`v+0`, `v+8`, `v+24`; + TCB regs `256..3327` and links `3664, 3672`; non-device OS frame words) and spec-step + characterization covering `vcpu.cancel_run`, `vcpu.read_state_to_frame`, `vcpu.complete_run` + (links+regs), and `vcpu.unbind` (`dissoc_bind`). +- `JStage3TA.of_vcpuStepA`, `SchedValidA.of_vcpuStepA`, and `VcpuStateSnapA.of_vcpuStepA`: + one-shot preservation across any `VcpuWrNoStA` step. + +No new named premise. +-/ +import Fl4ProofsA64.P3.Stage3TA +import Fl4ProofsA64.P3.FrAgDefA +import Fl4ProofsA64.P3.Stage3J5A +import Fl4ProofsA64.P3.Stage3PreemptTA +import Fl4ProofsA64.P3.Stage3SchedValidA +import Fl4ProofsA64.P3.DelJ7LA +import Fl4ProofsA64.P3.VcpuPremisesA +import Fl4ProofsA64.P3.VcpuDelCommA +import Fl4ProofsA64.P3.Stage3MapUnmA +import Fl4ProofsA64.P1.QueueHeapDbg + +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3.Vcpu + +open Fl4Spec Fl4SpecA64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST +open Fl4ProofsA64.P3 Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## Section 1: `J7FrA` and `VcpuRec6A` -/ + +/-- Frame-word placement and preservation projected from `J7A` / `J7LA` for `vcpu.complete_run`'s +`read_state_to_frame` (`out_mask ≠ 0`), leaving `J7PpA` unchanged (Architect ruling C(2)). -/ +class J7FrA {S : Type} (st : machine.Store S) [L : LawfulStore st] (J7 : JSlotA S) : Prop where + fr : ∀ Z H OS s σ m, J7 Z H OS s σ m → ∃ Pt Ub : Set U64, + VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt ∧ + S1aArmCol st H Z.Cs OS Z.V Ub s ∧ + UbFr st OS Ub s ∧ FrameOK st H OS s ∧ + (∀ p, SchedPageA st Z OS s p → p ∈ Pt) ∧ + FrUb st Ub OS s ∧ (∀ b ∈ Ub, Aligned b) ∧ (∀ b, VcW Z.V b → b ∉ Ub) ∧ + (∀ (Pp Tb : Set U64) (Lv : U64 → Nat), VsTyR st (L.ksb s) (PoolSet st Pp s) ∅ Tb Lv s → + utVsPtA st Pp Tb Lv s ⊆ Pt) + keepFr : ∀ Z (hw' : HwRootA) H OS s s' σ σ' m, J7 Z H OS s σ m → L.ksb s' = L.ksb s → OSKeep st OS s s' → + (∀ Pt Ub : Set U64, VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt → + FrUb st Ub OS s → VsKeep st (L.ksb s) Pt s s') → + J7 { Z with hw := hw' } H OS s' σ' m + +instance j7FrA_j7A {S : Type} {st : machine.Store S} [L : LawfulStore st] + {A : Type} (ar : machine.Arch A) (nA : U64) : J7FrA st (J7A st (J7R2A st ar nA)) where + fr := fun _ _ _ s _ _ J => by + obtain ⟨Pt, Ub, vr, -, arm, capc, -, -, -, link, -, frub, ubal, vcub, + Pp', Tb', Hr, Te, Kt, Lv', Re, bits, R2, -⟩ := J + refine ⟨Pt, Ub, vr, arm, capc.uf, capc.fo, link, frub, ubal, vcub, fun Pp Tb Lv hty p hp => ?_⟩ + have hpools : PoolSet st Pp s ⊆ PoolSet st Pp' s := fun q ⟨_, o, h1, h2, h3, h4, h5⟩ => by + rw [← h4]; exact R2.core.stage1.tbl o h1 h2 h3 (by rw [h4]; exact h5) + rcases hp with hP | ⟨-, hr⟩ + · exact R2.place.pools (hpools hP) + · exact R2.place.tables (vsReach_bridgeA hty R2.core.stage1 hpools hr).2.2.1 + keepFr := fun Z _ H OS s s' _ _ _ J kb osk vsk => by + obtain ⟨Pt, Ub, vr, pn, arm, cc, vcc, dfa, hi, link, vcw, fu, ual, vcu, Pp', Tb', Hr, Te, Kt, Lv', Re, bits, R2, Kut, + KLv, ktK, hik⟩ := J + have vsr := vsk Pt Ub vr fu + have tab : TabKeep st s s' := ⟨kb, fun i => by rw [kb]; exact vsr _ (Or.inr (asW_at _ i))⟩ + refine ⟨Pt, Ub, by rw [kb]; exact vr, PoolNamed.ofTab kb (fun j => tab.2 j) osk pn, S1aArmCol.ofTab tab arm, + S1aCapCol.ofOSK osk cc, CapInv.keepO vcc osk, CapInv.keepO dfa osk, + HiCap511.keep hi (fun y hy => osk.capA hy) (fun y hy p m a e => ?_), fun p hp => link p (pp_pageA osk hp), + by rw [kb]; exact vcw, FrUb.keepO fu osk, ual, vcu, Pp', Tb', Hr, Te, Kt, Lv', Re, bits, + R2.ofImage kb vsr (S1aSlotImage.ofSame R2.capRefs.refs fun _ hy => Or.inl (osk.capA hy)), Kut.keep osk, KLv, ktK, + S1aHiK.keep hik ⟨EdgeKeepOn.ofVsKeep R2 vsr, fun y hy R m a c => ⟨y, hy, m, a, by rwa [osk.capA hy] at c⟩⟩⟩ + · have live : p ∈ Hr := by rw [R2.highRoots]; exact ⟨y, hy, m, a, e⟩ + exact vsr _ (Or.inl ⟨p, R2.place.tables (R2.place.high.sub live), 4088#u64, by decide, by decide, rfl⟩) + +instance j7FrA_j7LA {S : Type} {st : machine.Store S} [L : LawfulStore st] + {A : Type} (ar : machine.Arch A) (nA : U64) : J7FrA st (J7LA st (J7R2A st ar nA)) where + fr := fun _ _ _ s _ _ J => by + obtain ⟨Pt, Ub, vr, -, arm, ⟨uf, fo⟩, -, -, -, link, -, frub, ubal, vcub, + Pp', Tb', Hr, Te, Kt, Lv', Re, bits, R2, -⟩ := J + refine ⟨Pt, Ub, vr, arm, uf, fo, link, frub, ubal, vcub, fun Pp Tb Lv hty p hp => ?_⟩ + have hpools : PoolSet st Pp s ⊆ PoolSet st Pp' s := fun q ⟨_, o, h1, h2, h3, h4, h5⟩ => by + rw [← h4]; exact R2.core.stage1.tbl o h1 h2 h3 (by rw [h4]; exact h5) + rcases hp with hP | ⟨-, hr⟩ + · exact R2.place.pools (hpools hP) + · exact R2.place.tables (vsReach_bridgeA hty R2.core.stage1 hpools hr).2.2.1 + keepFr := fun Z _ H OS s s' _ _ _ J kb osk vsk => by + obtain ⟨Pt, Ub, vr, pn, arm, cc, vcc, dfa, hi, link, vcw, fu, ual, vcu, Pp', Tb', Hr, Te, Kt, Lv', Re, bits, R2, Kut, + KLv, ktK, hik⟩ := J + have vsr := vsk Pt Ub vr fu + have tab : TabKeep st s s' := ⟨kb, fun i => by rw [kb]; exact vsr _ (Or.inr (asW_at _ i))⟩ + have cc' : UbFr st OS Ub s' ∧ FrameOK st H OS s' := by + refine ⟨fun b hb => ?_, fun y hy ptr size rights x1 x2 x3 x4 hc => ?_⟩ + · obtain ⟨y, hy, ptr, size, rights, x1, x2, x3, x4, bits, hc, h⟩ := cc.1 b hb + exact ⟨y, hy, ptr, size, rights, x1, x2, x3, x4, bits, (osk.capA hy).trans hc, h⟩ + · exact cc.2 y hy ptr size rights x1 x2 x3 x4 ((osk.capA hy).symm.trans hc) + refine ⟨Pt, Ub, by rw [kb]; exact vr, PoolNamed.ofTab kb (fun j => tab.2 j) osk pn, S1aArmCol.ofTab tab arm, + cc', CapInv.keepO vcc osk, CapInv.keepO dfa osk, + HiCap511.keep hi (fun y hy => osk.capA hy) (fun y hy p m a e => ?_), fun p hp => link p (pp_pageA osk hp), + by rw [kb]; exact vcw, FrUb.keepO fu osk, ual, vcu, Pp', Tb', Hr, Te, Kt, Lv', Re, bits, + R2.ofImage kb vsr (S1aSlotImage.ofSame R2.capRefs.refs fun _ hy => Or.inl (osk.capA hy)), Kut.keep osk, KLv, ktK, + S1aHiK.keep hik ⟨EdgeKeepOn.ofVsKeep R2 vsr, fun y hy R m a c => ⟨y, hy, m, a, by rwa [osk.capA hy] at c⟩⟩⟩ + · have live : p ∈ Hr := by rw [R2.highRoots]; exact ⟨y, hy, m, a, e⟩ + exact vsr _ (Or.inl ⟨p, R2.place.tables (R2.place.high.sub live), 4088#u64, by decide, by decide, rfl⟩) + +/-- Record-edit #6 per-step facts carried alongside `JStage3TA` (projected directly from `JStage3A` +and `DelRecA`). -/ +structure VcpuRec6A {S : Type} (st : machine.Store S) [L : LawfulStore st] + (Z : XSetsA) (OS : Set U64) (Bf : Set (U64 × Fl4SpecA64.Word)) + (s : S) (σ : Fl4SpecA64.State Unit) : Prop where + snap : VcpuStateSnapA Z.V (L.mem s) σ + vcSym1 : VcSym1A st Z.T s + vcSym2 : VcSym2A st Z.V s + vcBack : ∀ v ∈ Z.V, L.mem s v ≠ 0#u64 → L.mem s (wa (L.mem s v) 3664#u64) = v + runBack : ∀ t ∈ Z.T, L.mem s (runVcpuA t) ≠ 0#u64 → L.mem s (wa (L.mem s (runVcpuA t)) 24#u64) = t + vcOS : ∀ b, VcW Z.V b → ¬ InOS OS b + frOff : FrOff st Z.Cs OS (L.ksb s) s + userFr : UserFrSpecA st OS s σ + frTh : FrThA st Z.th OS s + bfSpec : ∀ bp ∈ Bf, bp.2 = specPtr bp.1 + +/-! ## Section 2: Write footprint and disjointness -/ + +section frame +variable {S A : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- The three VCPU header words written by `cancel_run`, `complete_run`, and `unbind`: +`v + 0` (`TCB`), `v + 8` (`OWNER_CPU`), and `v + 24` (`RUN_CALLER`). -/ +def VcHdrWrA (V : Set U64) (b : U64) : Prop := + ∃ v ∈ V, b = v ∨ b = wa v 8#u64 ∨ b = wa v 24#u64 + +theorem VcHdrWrA.in_vcW {V : Set U64} {b : U64} (h : VcHdrWrA V b) : VcW V b := by + rcases h with ⟨v, hv, e | e | e⟩ + · subst b; exact ⟨v, hv, 0#u64, by simp, by simp, (wa_zeroVcA v).symm⟩ + · subst b; exact ⟨v, hv, 8#u64, by simp, by simp, rfl⟩ + · subst b; exact ⟨v, hv, 24#u64, by simp, by simp, rfl⟩ + +/-- Concrete word footprint written by VCPU link/reg/frame steps (excluding `stA`): +- VCPU header words (`v+0`, `v+8`, `v+24`), +- TCB user register words (`256 ≤ K < 3328`) and VCPU link words (`K = 3664` or `K = 3672`), +- Non-device user frame words from `OS` caps. -/ +def VcpuWrFootA (st : machine.Store S) [L : LawfulStore st] + (regWr : Bool) (Z : XSetsA) (OS : Set U64) (s : S) (b : U64) : Prop := + (if regWr then VcW Z.V b else VcHdrWrA Z.V b) ∨ + (∃ t ∈ Z.T, ∃ K : U64, K.val % 8 = 0 ∧ + ((256 ≤ K.val ∧ K.val < 3328) ∨ K = 3664#u64 ∨ K = 3672#u64) ∧ + b = wa t K) ∨ + (∃ y ∈ OS, ∃ ptr size rights mapped ept asid va bits, + capA st s y = .Frame ptr size rights false mapped ept asid va ∧ + size.bits = ok bits ∧ + Aligned b ∧ ptr.val ≤ b.val ∧ b.val < ptr.val + 2 ^ bits.val) + +abbrev VcpuWrNoStA (st : machine.Store S) [L : LawfulStore st] + (Z : XSetsA) (OS : Set U64) (s : S) (b : U64) : Prop := + VcpuWrFootA st false Z OS s b + +/-- Non-VCPU, non-context TCB fields kept by any `VcpuSpecStepA` step. -/ +structure TcbKeepFieldsA (x x' : Fl4SpecA64.Tcb) : Prop where + ctable : x'.tcbCtable = x.tcbCtable + vtable : x'.tcbVtable = x.tcbVtable + ipcfr : x'.tcbIpcframe = x.tcbIpcframe + fh : x'.tcbFaultHandler = x.tcbFaultHandler + th : x'.tcbTimeoutHandler = x.tcbTimeoutHandler + s2 : x'.tcbExt.tcbStage2 = x.tcbExt.tcbStage2 + hv : x'.tcbExt.tcbHighVSpace = x.tcbExt.tcbHighVSpace + ibuf : x'.tcbIpcBuffer = x.tcbIpcBuffer + fault : x'.tcbFault = x.tcbFault + bnd : x'.tcbBoundNotification = x.tcbBoundNotification + sc : x'.tcbSchedContext = x.tcbSchedContext + prio : x'.tcbPriority = x.tcbPriority + mcp : x'.tcbMcpriority = x.tcbMcpriority + dom : x'.tcbDomain = x.tcbDomain + flags : x'.tcbFlags = x.tcbFlags + state : x'.tcbState = x.tcbState + yield : x'.tcbYieldTo = x.tcbYieldTo + /-- (record edit #8b, weakened for SetTCB (60), which binds a VCPU) An idle-state TCB with no VCPU gains none. -/ + vcpuNone : x.tcbState = .idleThreadState → x.tcbArch.tcbVcpu = none → x'.tcbArch.tcbVcpu = none + +/-- Spec-side relation for a VCPU link/reg/frame step (does not alter queues, cur/idle/sc, +action, CDT, origCaps, IRQ, domains, ASID table, time, or non-TCB/non-VCPU objects). -/ +structure VcpuSpecStepA (H : Heap) (σ σ' : Fl4SpecA64.State Unit) (regWr : Bool := false) : Prop where + rq : σ'.readyQueues = σ.readyQueues + rls : σ'.releaseQueue = σ.releaseQueue + cur : σ'.curThread = σ.curThread + idle : σ'.idleThread = σ.idleThread + csc : σ'.curSc = σ.curSc + act : σ'.schedulerAction = σ.schedulerAction + cdt : σ'.cdt = σ.cdt + orig : σ'.isOriginalCap = σ.isOriginalCap + irqN : σ'.interruptIrqNode = σ.interruptIrqNode + irqS : σ'.interruptStates = σ.interruptStates + dom : σ'.curDomain = σ.curDomain + domT : σ'.domainTime = σ.domainTime + domI : σ'.domainIndex = σ.domainIndex + domL : σ'.domainList = σ.domainList + curT : σ'.curTime = σ.curTime + consT : σ'.consumedTime = σ.consumedTime + reprog : σ'.reprogramTimer = σ.reprogramTimer + asid : σ'.archState.armAsidTable = σ.archState.armAsidTable + vcur : σ'.archState.armCurrentVcpu = σ.archState.armCurrentVcpu + timeS : σ'.machineState.timeState = σ.machineState.timeState + lastT : σ'.machineState.lastMachineTime = σ.machineState.lastMachineTime + /-- (record edit #8b-B1) -/ + domS : σ'.domainStartIndex = σ.domainStartIndex + /-- (record edit F4) The global user VSpace root is kept. -/ + gvs : σ'.archState.armUsGlobalVspace = σ.archState.armUsGlobalVspace + kh : ∀ p, σ'.kheap p = σ.kheap p ∨ + (∃ x x', σ.kheap p = some (.tcb x) ∧ σ'.kheap p = some (.tcb x') ∧ TcbKeepFieldsA x x') ∨ + (∃ v v', σ.kheap p = some (.archObj (.vcpu v)) ∧ σ'.kheap p = some (.archObj (.vcpu v')) ∧ + (if regWr then True else + v'.vcpuRegs = v.vcpuRegs ∧ v'.vcpuVppiMasked = v.vcpuVppiMasked ∧ + v'.vcpuVgic = v.vcpuVgic ∧ v'.vcpuExt.regs = v.vcpuExt.regs ∧ + v'.vcpuExt.runOffset = v.vcpuExt.runOffset ∧ + v'.vcpuExt.runOutMask = v.vcpuExt.runOutMask)) + umOff : ∀ a : Fl4SpecA64.Word, + σ'.machineState.underlyingMemory a ≠ σ.machineState.underlyingMemory a → + ∃ p bits, H p = some (.other bits) ∧ p.val ≤ (fl4Ptr a).val ∧ (fl4Ptr a).val < p.val + 2 ^ bits + +theorem VcpuSpecStepA.domk {H : Heap} {σ σ' : Fl4SpecA64.State Unit} {regWr : Bool} (HS : VcpuSpecStepA H σ σ' regWr) : DomKeepA σ σ' := + ⟨HS.domI, HS.domS, HS.domL, HS.gvs⟩ + +/-- (record edit #8c) A VCPU / TCB-register spec step keeps priorities, the IPC queues, the SCs and the replies. -/ +theorem QKeepA.of_vcpuStepA {H : Heap} {σ σ' : Fl4SpecA64.State Unit} {regWr : Bool} (HS : VcpuSpecStepA H σ σ' regWr) : QKeepA σ σ' := by + refine ⟨fun t y hy => ?_, fun p x' hp => ?_, fun p x' hp => ?_, fun p y k hp => ?_, fun p y hp => ?_, HS.domk⟩ + · rcases HS.kh t with e | ⟨x, x', h1, h2, hkf⟩ | ⟨_, _, _, h2, -⟩ + · exact ⟨y, by rw [← e]; exact hy, rfl⟩ + · rw [hy] at h2; cases h2; exact ⟨x, h1, hkf.prio⟩ + · rw [hy] at h2; cases h2 + · rcases HS.kh p with e | ⟨_, _, _, h2, -⟩ | ⟨_, _, _, h2, -⟩ + · exact ⟨x', by rw [← e]; exact hp, List.Sublist.refl _⟩ + · rw [hp] at h2; cases h2 + · rw [hp] at h2; cases h2 + · rcases HS.kh p with e | ⟨_, _, _, h2, -⟩ | ⟨_, _, _, h2, -⟩ + · exact ⟨x', by rw [← e]; exact hp, List.Sublist.refl _⟩ + · rw [hp] at h2; cases h2 + · rw [hp] at h2; cases h2 + · rcases HS.kh p with e | ⟨_, _, _, h2, -⟩ | ⟨_, _, _, h2, -⟩ + · exact ⟨y, k, by rw [← e]; exact hp, fun r => ⟨r, id⟩⟩ + · rw [hp] at h2; cases h2 + · rw [hp] at h2; cases h2 + · rcases HS.kh p with e | ⟨_, _, _, h2, -⟩ | ⟨_, _, _, h2, -⟩ + · exact ⟨y, by rw [← e]; exact hp, id⟩ + · rw [hp] at h2; cases h2 + · rw [hp] at h2; cases h2 + +theorem VcpuSpecStepA.refl (H : Heap) (σ : Fl4SpecA64.State Unit) (regWr : Bool := false) : VcpuSpecStepA H σ σ regWr := + ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, + fun _ => Or.inl rfl, fun _ h => absurd rfl h⟩ + +theorem VcpuSpecStepA.keepObj {H : Heap} {σ σ' : Fl4SpecA64.State Unit} {regWr : Bool} (HS : VcpuSpecStepA H σ σ' regWr) + {p : ObjRef} {o : KernelObject} (h : σ.kheap p = some o) + (hnt : ∀ x, o ≠ .tcb x) (hnv : ∀ v, o ≠ .archObj (.vcpu v)) : + σ'.kheap p = some o := by + rcases HS.kh p with e | ⟨x, _, h1, -⟩ | ⟨v, _, h1, -⟩ + · rw [e]; exact h + · rw [h] at h1; exact absurd (Option.some.inj h1) (hnt x) + · rw [h] at h1; exact absurd (Option.some.inj h1) (hnv v) + +theorem VcpuSpecStepA.backObj {H : Heap} {σ σ' : Fl4SpecA64.State Unit} {regWr : Bool} (HS : VcpuSpecStepA H σ σ' regWr) + {p : ObjRef} {o : KernelObject} (h : σ'.kheap p = some o) + (hnt : ∀ x, o ≠ .tcb x) (hnv : ∀ v, o ≠ .archObj (.vcpu v)) : + σ.kheap p = some o := by + rcases HS.kh p with e | ⟨_, x', _, h2, -⟩ | ⟨_, v', _, h2, -⟩ + · rw [← e]; exact h + · rw [h] at h2; exact absurd (Option.some.inj h2) (hnt x') + · rw [h] at h2; exact absurd (Option.some.inj h2) (hnv v') + +/-- (item 11) The three current-SC facts across a VCPU / TCB-register spec step. -/ +theorem VcpuSpecStepA.curFacts {H : Heap} {σ σ' : Fl4SpecA64.State Unit} {regWr : Bool} (HS : VcpuSpecStepA H σ σ' regWr) + (h : ZeroConsA σ ∧ RlsRunA σ ∧ CurRdyA σ) : ZeroConsA σ' ∧ RlsRunA σ' ∧ CurRdyA σ' := + CurFactsA.back h (fun t ht => by rw [HS.rls] at ht; exact ht) HS.consT HS.csc HS.curT + (fun t x' hx => by + rcases HS.kh t with e | ⟨y, y', h1, h2, hkf⟩ | ⟨v, v', _, h2, -⟩ + · exact ⟨x', by rw [← e]; exact hx, rfl⟩ + · rw [hx] at h2; cases h2; exact ⟨y, h1, hkf.sc.symm⟩ + · rw [hx] at h2; cases h2) + (fun t x hx hr => by + rcases HS.kh t with e | ⟨y, y', h1, h2, hkf⟩ | ⟨v, v', h1, -⟩ + · exact ⟨x, by rw [e]; exact hx, id⟩ + · rw [hx] at h1; cases h1; exact ⟨y', h2, fun r => by rw [hkf.state]; exact r⟩ + · rw [hx] at h1; cases h1) + (fun y k hy => ⟨y, HS.backObj hy (fun _ h => by cases h) (fun _ h => by cases h), fun a => ⟨a, id⟩⟩) + +theorem VcpuSpecStepA.specCapAt {H : Heap} {σ σ' : Fl4SpecA64.State Unit} {regWr : Bool} (HS : VcpuSpecStepA H σ σ' regWr) + (sl : CslotPtr) : specCapAtA σ' sl = specCapAtA σ sl := by + unfold specCapAtA + rcases HS.kh sl.1 with e | ⟨x, x', h1, h2, hk⟩ | ⟨v, v', h1, h2, -⟩ + · rw [e] + · rw [h1, h2]; simp only [tcbCnodeMap, hk.ctable, hk.vtable, hk.ipcfr, hk.fh, hk.th, hk.s2, hk.hv] + · rw [h1, h2] + +theorem VcpuSpecStepA.none_keep {H : Heap} {σ σ' : Fl4SpecA64.State Unit} {regWr : Bool} (HS : VcpuSpecStepA H σ σ' regWr) + {p : ObjRef} (h : σ.kheap p = none) : σ'.kheap p = none := by + rcases HS.kh p with e | ⟨_, _, h1, -⟩ | ⟨_, _, h1, -⟩ + · rw [e]; exact h + · rw [h] at h1; cases h1 + · rw [h] at h1; cases h1 + +/-- Internal `JStage3TA` core without `sact : SActA σ`, so `of_vcpuStepA` and `vcpu_complete_run` +serve both `JStage3TA` (deletion / normal syscalls) and `JStage3A` (when `curThread` is in +`.ext .runningVM`, where `SActA.f2` does not hold). -/ +structure JStage3VcpuCoreA {S A : Type} (st : machine.Store S) [L : LawfulStore st] + (AR : A → HwRootA → Prop) (Z : XSetsA) (H : Heap) (OS U Ac Pp Tb : Set U64) (Lv : U64 → Nat) + (ia ie : Nat) (N E Rp : Set U64) (Bf : Set (U64 × Fl4SpecA64.Word)) (stack : U64 → List U64) (κ : U64 → U64) + (n : Nat) (J5 J7 : JSlotA S) (s : S) (a : A) (σ : Fl4SpecA64.State Unit) (m : Fl4SpecA64.McState) : Prop + extends JEntTA st AR Z H OS U Ac Pp Tb Lv ia ie s a σ m where + sr : SchedSRA st Z m κ s σ + scCov : ScCovA Z.C m σ + scSize : ∀ c ∈ Z.C, ∀ x n, σ.kheap (scNameA m c) = some (.schedContext x n) → + n + Fl4SpecA64.minSchedContextBits ≤ H.scBits c ∧ + (Sched.NotIdleScA m (scNameA m c) → n + Fl4SpecA64.minSchedContextBits = H.scBits c) + ep : EpStateRel Z.T E Rp Bf (L.mem s) σ + ntfn : NtfnStateRel N Z.T (L.mem s) σ + reply : ReplyStackRelA (L.mem s) Rp Z.C σ (scNameA m) stack + bufCover : BufCover st Z.T Bf s σ + bufNative : BufNativeA Z.T (L.mem s) + iplaced : IPlacedA N Z.T E Rp Bf + ipcPlace : IpcSchedPlacementA Z.th N E Rp Bf + idleNoFault : IdleNoFaultA m σ + mcp : McpRelA Z.T m (L.mem s) σ + yieldRel : YieldRelA Z.T Z.C m (L.mem s) σ + sinv : SInvXA σ m n + setsInH : SetsInHA Z H + utIpc : UtIpcHeapA H N E Rp + ipcTh : IpcThA Z.th N E Rp + deadExt : DeadExtA st Z OS Rp stack s σ + specFree : SpecFreeA st OS s σ + utBlk : UtBlk39A st OS s + unmPt : UnmPtSpecA st Z m OS s σ + tabS1 : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s q + tabExt : ExtTabOffA (L := L) Z m s + specZero : SpecZeroFreeA st OS s σ + origLive : ∀ sl, σ.isOriginalCap sl = true → AbsSlotA Z.th sl + nullOrig : ∀ sl, specCapAtA σ sl = some .nullCap → σ.isOriginalCap sl = false + thPos : ∀ p z, Z.th p = some (.other z) → 0 < z + frBase : FrBaseApartA st OS s + devFr : DevFrDisjA st H OS s σ + finCov : FinCovA Z.th σ + idleAll : IdleAllA st ia ie Z.Cs m s + ipcHeapCov : IpcHeapCovA H N E Rp + zombieNoVcpu : ZombieTcbNoVcpuA st OS s + zombieTcb : ZombieTcbTA st H OS s + bufOff : BufOffA st Z OS s Bf + vsOk : UtVsOkA st H OS Z.V Pp Tb Lv s + bufAl : UtBufAlA Z.T σ + zombieBnd : UtZombieA st OS s + devUtOff : DevUtOffA st Z H OS ia ie s + sidle : SIdleA σ m n + capTy : CapTyA st H OS s + scCpu : ∀ c ∈ Z.C, (κ c).val < Fl4SpecA64.fl4A64Platform.numNodes + scNZ : ∀ c ∈ Z.C, c ≠ 0#u64 + vcRun : VcRunA st Z.T Z.V s + runVcpu : RunVcpuInv st Z.T s + vr : ∀ sc ∈ Z.C, ∀ r ∈ stack sc, RBlk st s r + blkQ : BlkQueuedA Z.T E N (L.mem s) σ + ciSc : ∀ u ∈ OS, ∀ p b, capA st s u = .SchedContext p b → specPtr p ≠ m.idleScOf m.curCore + noSwitch : ∀ u, σ.schedulerAction ≠ .switchThread u + curVcpuNone : σ.archState.armCurrentVcpu = none + ipcSorted : IpcSortedA (specPtr '' E) (specPtr '' N) σ + activeReplyScs : ActRepA σ + bufVs : ∀ b, BufW Bf b → ¬ VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b + domOK : Sched.NdOKA (st := st) s σ + ipiZero : Sched.SilentScheduleIpiA (st := st) s + j5 : J5 Z H OS s σ m + j7 : J7 Z H OS s σ m + +section disj +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J5 J7 : JSlotA S} + [J7PpA st J7] [J7FrA st J7] {s s' : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +omit [J7PpA st J7] [J7FrA st J7] in +theorem _root_.Fl4ProofsA64.P3.JStage3TA.toVcpuCoreA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) : + JStage3VcpuCoreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m := + { toJEntTA := J.toJEntTA, sr := J.sr, scCov := J.scCov, scSize := J.scSize, ep := J.ep, + ntfn := J.ntfn, reply := J.reply, bufCover := J.bufCover, bufNative := J.bufNative, + iplaced := J.iplaced, ipcPlace := J.ipcPlace, idleNoFault := J.idleNoFault, mcp := J.mcp, + yieldRel := J.yieldRel, sinv := J.sinv, setsInH := J.setsInH, utIpc := J.utIpc, + ipcTh := J.ipcTh, deadExt := J.deadExt, specFree := J.specFree, utBlk := J.utBlk, + unmPt := J.unmPt, tabS1 := J.tabS1, tabExt := J.tabExt, specZero := J.specZero, + origLive := J.origLive, nullOrig := J.nullOrig, thPos := J.thPos, frBase := J.frBase, + devFr := J.devFr, finCov := J.finCov, idleAll := J.idleAll, ipcHeapCov := J.ipcHeapCov, + zombieNoVcpu := J.zombieNoVcpu, zombieTcb := J.zombieTcb, bufOff := J.bufOff, vsOk := J.vsOk, + bufAl := J.bufAl, zombieBnd := J.zombieBnd, devUtOff := J.devUtOff, sidle := J.sidle, + capTy := J.capTy, scCpu := J.scCpu, scNZ := J.scNZ, vcRun := J.vcRun, runVcpu := J.runVcpu, + vr := J.vr, blkQ := J.blkQ, ciSc := J.ciSc, noSwitch := J.noSwitch, + curVcpuNone := J.curVcpuNone, ipcSorted := J.ipcSorted, activeReplyScs := J.activeReplyScs, + bufVs := J.bufVs, domOK := J.domOK, ipiZero := J.ipiZero, j5 := J.j5, j7 := J.j7 } + +omit [J7PpA st J7] [J7FrA st J7] in +private theorem stackBlkVcA (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) : + ∀ sc ∈ Z.C, ∀ r ∈ stack sc, RBlk st s r := by + intro sc hc r hr + obtain ⟨x, k, hk, hrel⟩ := J.sr.scr.sc sc hc + obtain ⟨⟨y, k', hy, eq⟩, hm, -, hw⟩ := J.reply.stackRel sc hc + rw [hk] at hy; cases hy + have PE := J.iplaced.e + have head : ∀ r0 l, stack sc = r0 :: l → RBlk st s r0 := by + intro r0 l e + have he := hrel.reply + rw [← Sched.scReplyA_eqS, eq, e] at he + have hne : L.mem s (scReplyA sc) ≠ 0#u64 := fun h => by + rw [h, Sched.optRefS_zero] at he; cases he + rw [Sched.optRefS_ne hne] at he + have e0 : L.mem s (scReplyA sc) = r0 := (specPtr_inj (Option.some.inj he)).symm + have := J.sys.sysl.vr.screp sc (by rw [J.scs]; exact hc) hne + rwa [e0] at this + have all : ∀ i, ∀ hi : i < (stack sc).length, RBlk st s (stack sc)[i] := by + intro i + induction i with + | zero => + intro hi + obtain ⟨r0, l, e⟩ : ∃ r0 l, stack sc = r0 :: l := by + revert hi; generalize stack sc = rs; intro hi + cases rs with + | nil => exact absurd hi (Nat.not_lt_zero _) + | cons r0 l => exact ⟨r0, l, rfl⟩ + have h0 : (stack sc)[0] = r0 := by simp [e] + exact h0 ▸ head r0 l e + | succ i ih => + intro hi + have hi' : i < (stack sc).length := Nat.lt_of_succ_lt hi + have hp := (hw i hi').1 + have hq : (stack sc).getD (i + 1) 0#u64 = (stack sc)[i + 1] := by + rw [List.getD_eq_getElem?_getD, List.getElem?_eq_getElem hi]; rfl + rw [hq] at hp + have mR : (stack sc)[i + 1] ∈ Rp := hm _ (List.getElem_mem hi) + have rep := PE.rep _ mR + have mask : (stack sc)[i + 1] &&& ~~~1#u64 = (stack sc)[i + 1] := + mask1_of_even (by have := rep.1; omega) + have ne0 : (stack sc)[i + 1] ≠ 0#u64 := fun e => rep.2.1 (by rw [e]; rfl) + have h := J.sys.sysl.vr.rprev (stack sc)[i] (J.utIpc.reply _ (hm _ (List.getElem_mem hi'))) + (by rw [hp, mask]; exact ne0) + rwa [hp, mask] at h + obtain ⟨i, hi, e⟩ := List.getElem_of_mem hr + exact e ▸ all i hi + +omit [J7PpA st J7] [J7FrA st J7] in +private theorem ciScVcA (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) : + ∀ u ∈ OS, ∀ p b, capA st s u = .SchedContext p b → specPtr p ≠ m.idleScOf m.curCore := by + intro u hu p b hc hp + obtain ⟨x, y, k, hx, hsc, -, -⟩ := J.sinv.idleSc + have hid : σ.idleThread = specPtr (L.idleW s) := J.sr.sch.idle + have htZ : L.idleW s ∈ Z.T := J.j.1.idleT _ J.sr.sch.cpuCs + obtain ⟨x', hx', hs⟩ := J.sr.sch.tcb _ htZ + rw [← hid, hx] at hx' + have ex := Fl4SpecA64.KernelObject.tcb.inj (Option.some.inj hx') + rw [← ex, hsc] at hs + have hw0 : L.mem s (scA (L.idleW s)) ≠ 0#u64 := fun h => by + rw [h] at hs; unfold optScA at hs; simp at hs + unfold optScA at hs + rw [if_neg hw0] at hs + have hC := J.sr.rest.scMem _ htZ hw0 + have hI : specPtr (L.mem s (scA (L.idleW s))) = m.idleScOf m.curCore := by + by_contra hne + refine J.sr.sch.scOff _ hC hne ?_ + have h2 := Option.some.inj hs + unfold scRenA at h2 + rw [if_neg hne] at h2 + exact h2.symm + have e : p = L.mem s (scA (L.idleW s)) := specPtr_inj (hp.trans hI.symm) + exact (J.sys.sysl.ci u hu).2.1 b (e ▸ hc) + +omit [J7PpA st J7] [J7FrA st J7] in +theorem _root_.Fl4ProofsA64.P3.JStage3A.toVcpuCoreA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (noSwitch : ∀ u, σ.schedulerAction ≠ .switchThread u) : + JStage3VcpuCoreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m := + { toJEntTA := J.toJEntA.toTA, sr := J.sr, scCov := J.scCov, scSize := J.scSize, ep := J.ep, + ntfn := J.ntfn, reply := J.reply, bufCover := J.bufCover, bufNative := J.bufNative, + iplaced := J.iplaced, ipcPlace := J.ipcPlace, idleNoFault := J.idleNoFault, mcp := J.mcp, + yieldRel := J.yieldRel, sinv := J.sinv, setsInH := J.setsInH, utIpc := J.utIpc, + ipcTh := J.ipcTh, deadExt := J.deadExt, specFree := J.specFree, utBlk := J.utBlk, + unmPt := J.unmPt, tabS1 := J.tabS1, tabExt := J.tabExt, specZero := J.specZero, + origLive := J.origLive, nullOrig := J.nullOrig, thPos := J.thPos, frBase := J.frBase, + devFr := J.devFr, finCov := J.finCov, idleAll := J.idleAll, ipcHeapCov := J.ipcHeapCov, + zombieNoVcpu := J.zombieNoVcpu, zombieTcb := J.zombieTcb, bufOff := J.bufOff, vsOk := J.vsOk, + bufAl := J.bufAl, zombieBnd := J.zombieBnd, devUtOff := J.devUtOff, sidle := J.sidle, + capTy := J.capTy, scCpu := J.scCpu, scNZ := J.scNZ, vcRun := J.vcRun, runVcpu := J.runVcpu, + vr := stackBlkVcA J, blkQ := J.blkQ, ciSc := ciScVcA J, noSwitch := noSwitch, + curVcpuNone := J.curVcpuNone, ipcSorted := J.ipcSorted, activeReplyScs := J.activeReplyScs, + bufVs := J.bufVs, domOK := J.domOK, ipiZero := J.ipiZero, j5 := J.j5, j7 := J.j7 } + +omit [J7PpA st J7] [J7FrA st J7] in +theorem JStage3VcpuCoreA.toTA + (J : JStage3VcpuCoreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (sact : SActA σ) : + JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m := + { toJEntTA := J.toJEntTA, sr := J.sr, scCov := J.scCov, scSize := J.scSize, ep := J.ep, + ntfn := J.ntfn, reply := J.reply, bufCover := J.bufCover, bufNative := J.bufNative, + iplaced := J.iplaced, ipcPlace := J.ipcPlace, idleNoFault := J.idleNoFault, mcp := J.mcp, + yieldRel := J.yieldRel, sinv := J.sinv, setsInH := J.setsInH, utIpc := J.utIpc, + ipcTh := J.ipcTh, deadExt := J.deadExt, specFree := J.specFree, utBlk := J.utBlk, + unmPt := J.unmPt, tabS1 := J.tabS1, tabExt := J.tabExt, specZero := J.specZero, + origLive := J.origLive, nullOrig := J.nullOrig, thPos := J.thPos, frBase := J.frBase, + devFr := J.devFr, finCov := J.finCov, idleAll := J.idleAll, ipcHeapCov := J.ipcHeapCov, + zombieNoVcpu := J.zombieNoVcpu, zombieTcb := J.zombieTcb, bufOff := J.bufOff, vsOk := J.vsOk, + bufAl := J.bufAl, zombieBnd := J.zombieBnd, devUtOff := J.devUtOff, sidle := J.sidle, + capTy := J.capTy, scCpu := J.scCpu, scNZ := J.scNZ, vcRun := J.vcRun, runVcpu := J.runVcpu, + vr := J.vr, blkQ := J.blkQ, ciSc := J.ciSc, noSwitch := J.noSwitch, sact := sact, + curVcpuNone := J.curVcpuNone, ipcSorted := J.ipcSorted, activeReplyScs := J.activeReplyScs, + bufVs := J.bufVs, domOK := J.domOK, ipiZero := J.ipiZero, j5 := J.j5, j7 := J.j7 } + +omit [J7PpA st J7] [J7FrA st J7] in +theorem JStage3VcpuCoreA.toJA + (J : JStage3VcpuCoreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (sysl : SysL st H Z.Cs OS ia ie s) (vcSym1 : VcSym1A st Z.T s) (vcSym2 : VcSym2A st Z.V s) + (heapWin : ∀ p k, H p = some k → layout.PPTR_BASE.val ≤ p.val ∧ p.val + 2 ^ k.bits ≤ layout.PPTR_BASE.val + 2 ^ 44) + (coresAll : ∀ k < Fl4SpecA64.fl4A64Platform.numNodes, ∃ c ∈ Z.Cs, c.val = k) + (ptCapIn : PtCapInA Z (L.mem s)) (symL : SymL st H Z.Cs s) + (fhOK : ∀ t ∈ Z.T, NullOrEp H (capA st s (slotIdx t layout.tcb.SLOT_FAULT_HANDLER))) + (zwf : DelInvA st Z.th s) (schedValid : SchedValidA σ) + (utWin : UtWinCapA st OS s) (devWin : DevWinCapA st OS s) + (vcBack : ∀ v ∈ Z.V, L.mem s v ≠ 0#u64 → L.mem s (wa (L.mem s v) 3664#u64) = v) + (runBack : ∀ t ∈ Z.T, L.mem s (runVcpuA t) ≠ 0#u64 → L.mem s (wa (L.mem s (runVcpuA t)) 24#u64) = t) + (vcSnap : VcpuStateSnapA Z.V (L.mem s) σ) (vcOS : ∀ b, VcW Z.V b → ¬ InOS OS b) + (userFr : UserFrSpecA st OS s σ) (frTh : FrThA st Z.th OS s) (bfSpec : ∀ bp ∈ Bf, bp.2 = specPtr bp.1) : + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m := + { toJEntA := ⟨⟨J.ent, sysl⟩, J.j, J.tcbs, J.scs, J.sq, J.ap, J.pp, J.s1, J.ko, J.ptH, J.tabInj, + J.poolOff, J.objAlloc, J.idleAnchor, J.vsVc⟩ + sr := J.sr, scCov := J.scCov, scSize := J.scSize, ep := J.ep, ntfn := J.ntfn, reply := J.reply, + bufCover := J.bufCover, bufNative := J.bufNative, iplaced := J.iplaced, ipcPlace := J.ipcPlace, + idleNoFault := J.idleNoFault, mcp := J.mcp, yieldRel := J.yieldRel, sinv := J.sinv, + setsInH := J.setsInH, utIpc := J.utIpc, ipcTh := J.ipcTh, deadExt := J.deadExt, + specFree := J.specFree, utBlk := J.utBlk, unmPt := J.unmPt, tabS1 := J.tabS1, + tabExt := J.tabExt, specZero := J.specZero, origLive := J.origLive, nullOrig := J.nullOrig, + thPos := J.thPos, frBase := J.frBase, devFr := J.devFr, finCov := J.finCov, + idleAll := J.idleAll, ipcHeapCov := J.ipcHeapCov, zombieNoVcpu := J.zombieNoVcpu, + zombieTcb := J.zombieTcb, bufOff := J.bufOff, vsOk := J.vsOk, bufAl := J.bufAl, + zombieBnd := J.zombieBnd, devUtOff := J.devUtOff, sidle := J.sidle, capTy := J.capTy, + scCpu := J.scCpu, scNZ := J.scNZ, vcRun := J.vcRun, vcSym1 := vcSym1, runVcpu := J.runVcpu, + vcSym2 := vcSym2, blkQ := J.blkQ, heapWin := heapWin, coresAll := coresAll, + ptCapIn := ptCapIn, symL := symL, fhOK := fhOK, zwf := zwf, schedValid := schedValid, + utWin := utWin, devWin := devWin, vcBack := vcBack, runBack := runBack, vcSnap := vcSnap, + vcOS := vcOS, userFr := userFr, frTh := frTh, bfSpec := bfSpec, curVcpuNone := J.curVcpuNone, + ipcSorted := J.ipcSorted, activeReplyScs := J.activeReplyScs, bufVs := J.bufVs, + domOK := J.domOK, ipiZero := J.ipiZero, j5 := J.j5, j7 := J.j7 } + +omit [J7PpA st J7] [J7FrA st J7] in +theorem JStage3VcpuCoreA.toJA_vcpuA {hw' : HwRootA} {a' : A} {σ' : Fl4SpecA64.State Unit} + (J0 : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + (J' : JStage3VcpuCoreA st AR { Z with hw := hw' } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s' a' σ' m) + (hSV' : SchedValidA σ') (R6' : VcpuRec6A st { Z with hw := hw' } OS Bf s' σ') + (osk : OSKeep st OS s s') (sysl : SysL st H Z.Cs OS ia ie s') (symL : SymL st H Z.Cs s') : + JStage3A st AR { Z with hw := hw' } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s' a' σ' m := + have capE : ∀ y ∈ OS, capA st s' y = capA st s y := fun y hy => osk.capA hy + J'.toJA sysl R6'.vcSym1 R6'.vcSym2 J0.heapWin J0.coresAll + (fun y hy => by have := J0.ptCapIn y hy; rwa [capAtA_eq, capE y (J0.j5.2.1 y hy), ← capAtA_eq]) + symL + (fun t ht => by rw [capE _ (J0.j5.2.1 _ (isSlot_fhA ((J0.sr.thT t).2 ht)))]; exact J0.fhOK t ht) + (fun y hy p zt n hc => J0.zwf y hy p zt n ((capE y (J0.j5.2.1 y hy)).symm.trans hc)) + hSV' (J0.utWin.keepA capE) (J0.devWin.keepA capE) + R6'.vcBack R6'.runBack R6'.snap R6'.vcOS R6'.userFr R6'.frTh R6'.bfSpec + +omit [J7PpA st J7] [J7FrA st J7] in +theorem JStage3VcpuCoreA.scFoot_heapPA + (J : JStage3VcpuCoreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {b : U64} (h : ScFootA Z.C m σ b) : + ∃ c ∈ Z.C, ∃ z, H c = some (.sc z) ∧ InSC c (H.scBits c) b := by + obtain ⟨c, hc, x, k, hk, h1, h2⟩ := h + have hcH : c ∈ H.scs := by rw [J.scs]; exact hc + obtain ⟨z, hz⟩ := hcH + refine ⟨c, hc, z, hz, h1, lt_of_lt_of_le h2 ?_⟩ + have := (J.scSize c hc x k hk).1 + exact Nat.add_le_add_left (Nat.pow_le_pow_right (by decide) this) _ + +omit [J7PpA st J7] [J7FrA st J7] in +theorem JStage3VcpuCoreA.vr_keepA + (J : JStage3VcpuCoreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (hr : ∀ r ∈ Rp, L.mem s' (Fl4ProofsA64.P1.rTcbA r) = L.mem s (Fl4ProofsA64.P1.rTcbA r)) + (hs : ∀ t ∈ Z.T, L.mem s' (stA t) = L.mem s (stA t)) : ∀ sc ∈ Z.C, ∀ r ∈ stack sc, RBlk st s' r := by + intro sc hc r h + have mR : r ∈ Rp := (J.reply.stackRel sc hc).2.1 r h + have old := J.vr sc hc r h + have mt : L.mem s (Fl4ProofsA64.P1.rTcbA r) ∈ Z.T := by + rw [← J.tcbs] + exact (J.ent.bxi.2.2.1.2.1.back r (J.utIpc.reply r mR) old.tcb).2.1 + exact ⟨by rw [hr r mR]; exact old.tcb, by rw [hr r mR, hs _ mt]; exact old.blk⟩ + +omit [J7PpA st J7] in +private theorem vcpuWr_inHeapA + (J : JStage3VcpuCoreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {regWr : Bool} {b : U64} (hb : VcpuWrFootA st regWr Z OS s b) : + (∃ v ∈ Z.V, H v = some (.other 12) ∧ v.val ≤ b.val ∧ b.val < v.val + 4096) ∨ + (∃ t ∈ Z.T, H t = some .tcb ∧ ∃ K : U64, + ((256 ≤ K.val ∧ K.val < 3328) ∨ K.val = 3664 ∨ K.val = 3672) ∧ + b.val = t.val + K.val) ∨ + (∃ ptr bits, H ptr = some (.other bits) ∧ ptr.val ≤ b.val ∧ b.val < ptr.val + 2 ^ bits) := by + obtain ⟨_, _, -, arm, -, fo, -, -, -, -⟩ := J7FrA.fr (st := st) _ _ _ _ _ _ J.j7 + rcases hb with hvc | ⟨t, ht, K, -, hK, rfl⟩ | ⟨y, hy, ptr, sz, r, mp, ept, asid, va, bits, hc, hbits, -, h1, h2⟩ + · have hvcW : VcW Z.V b := by cases regWr <;> [exact hvc.in_vcW; exact hvc] + obtain ⟨v, hv, o, ho, -, rfl⟩ := hvcW + have hH := arm.vh v hv + have hfit := J.ent.heap.fits v _ hH + simp only [HKind.bits, Nat.reducePow] at hfit + have ev : (wa v o).val = v.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact Or.inl ⟨v, hv, hH, by omega, by omega⟩ + · have htH : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + have hfit := J.ent.heap.fits t .tcb htH + simp only [HKind.bits, Nat.reducePow] at hfit + have hKv : (256 ≤ K.val ∧ K.val < 3328) ∨ K.val = 3664 ∨ K.val = 3672 := by + rcases hK with h | rfl | rfl + · exact Or.inl h + · exact Or.inr (Or.inl rfl) + · exact Or.inr (Or.inr rfl) + have ev : (wa t K).val = t.val + K.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact Or.inr (Or.inl ⟨t, ht, htH, K, hKv, ev⟩) + · exact Or.inr (Or.inr ⟨ptr, bits.val, fo y hy ptr sz r mp ept asid va hc bits hbits, h1, h2⟩) + +omit [J7PpA st J7] in +/-- No `OS` slot word lies in `VcpuWrNoStA` (core). -/ +theorem os_off_vcpuWrCoreA + (J : JStage3VcpuCoreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (hVcOS : ∀ b, VcW Z.V b → ¬ InOS OS b) + (hFrOff : FrOff st Z.Cs OS (L.ksb s) s) + {regWr : Bool} {y b : U64} (hy : y ∈ OS) (h1 : y.val ≤ b.val) (h2 : b.val < y.val + 32) : + ¬ VcpuWrFootA st regWr Z OS s b := by + intro hw + obtain ⟨_, _, -, arm, -, -, -, -, -, -⟩ := J7FrA.fr (st := st) _ _ _ _ _ _ J.j7 + rcases hw with hvc | ⟨t, ht, K, -, hK, rfl⟩ | ⟨u, hu, ptr, sz, r, mp, ept, asid, va, bits, hc, hbits, -, hb1, hb2⟩ + · exact hVcOS b (by cases regWr <;> [exact hvc.in_vcW; exact hvc]) ⟨y, hy, h1, h2⟩ + · have htH : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + have hfit := J.ent.heap.fits t .tcb htH + simp only [HKind.bits, Nat.reducePow] at hfit + have hKv : (256 ≤ K.val ∧ K.val < 3328) ∨ K.val = 3664 ∨ K.val = 3672 := by + rcases hK with h | rfl | rfl + · exact Or.inl h + · exact Or.inr (Or.inl rfl) + · exact Or.inr (Or.inr rfl) + have ev : (wa t K).val = t.val + K.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + obtain ⟨-, q, k, hq, hqy, hqhi, hqt, -⟩ := arm.case.slot y hy + by_cases hqt' : q = t + · subst hqt'; rw [htH] at hq; cases hq; have := hqt rfl; omega + · have hd := J.ent.heap.disj q k t .tcb hq htH hqt' + change q.val + 2 ^ k.bits ≤ t.val ∨ t.val + 4096 ≤ q.val at hd + have hK4 : K.val < 4096 := by omega + rcases hd with hd | hd <;> omega + · have hfo := (hFrOff u hu ptr sz r mp ept asid va hc bits hbits).1 y hy + omega + +omit [J7PpA st J7] in +/-- No `OS` slot word lies in `VcpuWrNoStA`. -/ +theorem os_off_vcpuWrA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (hVcOS : ∀ b, VcW Z.V b → ¬ InOS OS b) + (hFrOff : FrOff st Z.Cs OS (L.ksb s) s) + {regWr : Bool} {y b : U64} (hy : y ∈ OS) (h1 : y.val ≤ b.val) (h2 : b.val < y.val + 32) : + ¬ VcpuWrFootA st regWr Z OS s b := + os_off_vcpuWrCoreA J.toVcpuCoreA hVcOS hFrOff hy h1 h2 + +omit [J7PpA st J7] in +/-- `WritesIn st s s' (VcpuWrNoStA st Z OS s)` preserves `OSKeep` (core). -/ +theorem osKeep_of_vcpuWrCoreA + (J : JStage3VcpuCoreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (hVcOS : ∀ b, VcW Z.V b → ¬ InOS OS b) + (hFrOff : FrOff st Z.Cs OS (L.ksb s) s) + {regWr : Bool} (w : WritesIn st s s' (VcpuWrFootA st regWr Z OS s)) : OSKeep st OS s s' := by + obtain ⟨_, _, -, arm, -, -, -, -, -, -⟩ := J7FrA.fr (st := st) _ _ _ _ _ _ J.j7 + intro y hy + have hal := (arm.case.slot y hy).1 + have hfit := J.ent.os.fit y hy + have step : ∀ o : U64, o.val < 32 → o.val % 8 = 0 → L.mem s' (wa y o) = L.mem s (wa y o) := fun o ho h8 => by + have ev : (wa y o).val = y.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + refine w.mem_eq (by show (wa y o).val % 8 = 0; rw [ev]; omega) ?_ + exact os_off_vcpuWrCoreA J hVcOS hFrOff hy (by omega) (by omega) + refine ⟨?_, step 8#u64 (by simp) (by simp), step 16#u64 (by simp) (by simp), step 24#u64 (by simp) (by simp)⟩ + have h0 := step 0#u64 (by simp) (by simp) + rwa [wa_zeroVcA] at h0 + +omit [J7PpA st J7] in +/-- `WritesIn st s s' (VcpuWrNoStA st Z OS s)` preserves `OSKeep`. -/ +theorem osKeep_of_vcpuWrA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (hVcOS : ∀ b, VcW Z.V b → ¬ InOS OS b) + (hFrOff : FrOff st Z.Cs OS (L.ksb s) s) + {regWr : Bool} (w : WritesIn st s s' (VcpuWrFootA st regWr Z OS s)) : OSKeep st OS s s' := + osKeep_of_vcpuWrCoreA J.toVcpuCoreA hVcOS hFrOff w + +omit [J7PpA st J7] in +/-- Any word in a non-TCB, non-`.other` heap object (`ep`, `ntfn`, `reply`, `sc`) is off `VcpuWrNoStA` (core). -/ +theorem heapObj_off_vcpuWrCoreA + (J : JStage3VcpuCoreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {regWr : Bool} {p b : U64} {k : HKind} (hp : H p = some k) + (hnt : k ≠ .tcb) (hno : ∀ bits, k ≠ .other bits) + (h1 : p.val ≤ b.val) (h2 : b.val < p.val + 2 ^ k.bits) : + ¬ VcpuWrFootA st regWr Z OS s b := by + intro hw + rcases vcpuWr_inHeapA J hw with ⟨v, -, hvH, hv1, hv2⟩ | ⟨t, -, htH, K, hK, ev⟩ | ⟨ptr, bits, hpH, hp1, hp2⟩ + · have hne : p ≠ v := fun e => by rw [e, hvH] at hp; exact hno 12 (Option.some.inj hp).symm + have hd := J.ent.heap.disj p k v (.other 12) hp hvH hne + change p.val + 2 ^ k.bits ≤ v.val ∨ v.val + 4096 ≤ p.val at hd + rcases hd with hd | hd <;> omega + · have hne : p ≠ t := fun e => by rw [e, htH] at hp; exact hnt (Option.some.inj hp).symm + have hd := J.ent.heap.disj p k t .tcb hp htH hne + change p.val + 2 ^ k.bits ≤ t.val ∨ t.val + 4096 ≤ p.val at hd + have hK4 : K.val < 4096 := by omega + rcases hd with hd | hd <;> omega + · have hne : p ≠ ptr := fun e => by rw [e, hpH] at hp; exact hno bits (Option.some.inj hp).symm + have hd := J.ent.heap.disj p k ptr (.other bits) hp hpH hne + change p.val + 2 ^ k.bits ≤ ptr.val ∨ ptr.val + 2 ^ bits ≤ p.val at hd + rcases hd with hd | hd <;> omega + +omit [J7PpA st J7] in +/-- Any word in a non-TCB, non-`.other` heap object (`ep`, `ntfn`, `reply`, `sc`) is off `VcpuWrNoStA`. -/ +theorem heapObj_off_vcpuWrA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {regWr : Bool} {p b : U64} {k : HKind} (hp : H p = some k) + (hnt : k ≠ .tcb) (hno : ∀ bits, k ≠ .other bits) + (h1 : p.val ≤ b.val) (h2 : b.val < p.val + 2 ^ k.bits) : + ¬ VcpuWrFootA st regWr Z OS s b := + heapObj_off_vcpuWrCoreA J.toVcpuCoreA hp hnt hno h1 h2 + +omit [J7PpA st J7] in +/-- Any TCB scheduler word (`3328 ≤ K < 3664`) is off `VcpuWrNoStA` (core). -/ +theorem tcbSched_off_vcpuWrCoreA + (J : JStage3VcpuCoreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {regWr : Bool} {t K : U64} (ht : t ∈ Z.T) (h1 : 3328 ≤ K.val) (h2 : K.val < 3664) : + ¬ VcpuWrFootA st regWr Z OS s (wa t K) := by + intro hw + have htH : H t = some .tcb := show t ∈ H.tcbs by rw [J.tcbs]; exact ht + have hfit := J.ent.heap.fits t .tcb htH + change t.val + 4096 ≤ 2 ^ 64 at hfit + have ev0 : (wa t K).val = t.val + K.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + rcases vcpuWr_inHeapA J hw with ⟨v, -, hvH, hv1, hv2⟩ | ⟨t', -, ht'H, K', hK', ev'⟩ | ⟨ptr, bits, hpH, hp1, hp2⟩ + · have hne : t ≠ v := fun e => by rw [e, hvH] at htH; cases htH + have hd := J.ent.heap.disj t .tcb v (.other 12) htH hvH hne + change t.val + 4096 ≤ v.val ∨ v.val + 4096 ≤ t.val at hd + rcases hd with hd | hd <;> omega + · by_cases he : t = t' + · subst he; omega + · have hd := J.ent.heap.disj t .tcb t' .tcb htH ht'H he + change t.val + 4096 ≤ t'.val ∨ t'.val + 4096 ≤ t.val at hd + have hK4 : K'.val < 4096 := by omega + rcases hd with hd | hd <;> omega + · have hne : t ≠ ptr := fun e => by rw [e, hpH] at htH; cases htH + have hd := J.ent.heap.disj t .tcb ptr (.other bits) htH hpH hne + change t.val + 4096 ≤ ptr.val ∨ ptr.val + 2 ^ bits ≤ t.val at hd + rcases hd with hd | hd <;> omega + +omit [J7PpA st J7] in +/-- Any TCB scheduler word (`3328 ≤ K < 3664`) is off `VcpuWrNoStA`. -/ +theorem tcbSched_off_vcpuWrA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {regWr : Bool} {t K : U64} (ht : t ∈ Z.T) (h1 : 3328 ≤ K.val) (h2 : K.val < 3664) : + ¬ VcpuWrFootA st regWr Z OS s (wa t K) := + tcbSched_off_vcpuWrCoreA J.toVcpuCoreA ht h1 h2 + +/-- Every `VcpuWrNoStA` word is completely off the kernel-state block `[ksb, ksb + 788736)` (core). -/ +theorem ks_off_vcpuWrCoreA + (J : JStage3VcpuCoreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {regWr : Bool} {b : U64} (h1 : (L.ksb s).val ≤ b.val) (h2 : b.val < (L.ksb s).val + 788736) : + ¬ VcpuWrFootA st regWr Z OS s b := by + intro hw + obtain ⟨_, _, -, -, -, apart⟩ := J7PpA.core (st := st) _ _ _ _ _ _ J.j7 + obtain ⟨_, Ub, -, arm, -, -, -, frub, -, -⟩ := J7FrA.fr (st := st) _ _ _ _ _ _ J.j7 + have hksw := J.ent.ksw + rcases hw with hvc | ⟨t, ht, K, -, hK, rfl⟩ | ⟨u, hu, ptr, sz, r, mp, ept, asid, va, bits, hc, hbits, hal, hb1, hb2⟩ + · have hvcW : VcW Z.V b := by cases regWr <;> [exact hvc.in_vcW; exact hvc] + obtain ⟨v, hv, o, ho, -, rfl⟩ := hvcW + have hH := arm.vh v hv + have hfit := J.ent.heap.fits v _ hH + change v.val + 4096 ≤ 2 ^ 64 at hfit + have ev : (wa v o).val = v.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + rcases arm.kb.ksIn v 12 hH with g | g | ⟨g1, g2⟩ | ⟨g1, g2⟩ + · omega + · omega + · let d : U64 := ⟨BitVec.ofNat 64 (v.val - (L.ksb s).val)⟩ + have hd : d.val = v.val - (L.ksb s).val := by + show (BitVec.ofNat 64 _).toNat = _; rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + have heq : wa (L.ksb s) d = v := by + apply UScalar.eq_of_val_eq; rw [wa_valm, hd]; rw [Nat.mod_eq_of_lt (by omega)]; omega + have hvw : VcW Z.V v := ⟨v, hv, 0#u64, by simp, by simp, (wa_zeroVcA v).symm⟩ + exact apart v hvw d (by omega) heq + · omega + · have htH : H t = some .tcb := show t ∈ H.tcbs by rw [J.tcbs]; exact ht + have hfit := J.ent.heap.fits t .tcb htH + change t.val + 4096 ≤ 2 ^ 64 at hfit + have hKv : K.val < 4096 := by + rcases hK with h | rfl | rfl + · omega + · decide + · decide + have ev : (wa t K).val = t.val + K.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + rcases arm.kb.ks t .tcb htH (fun _ e => by cases e) with g | g + · change t.val + 4096 ≤ (L.ksb s).val at g; omega + · omega + · have hbUb : b ∈ Ub := frub u hu ptr sz r mp ept asid va hc bits hbits b hal hb1 hb2 + rcases arm.kb.ksU b hbUb with g | g <;> omega + +/-- Every `VcpuWrNoStA` word is completely off the kernel-state block `[ksb, ksb + 788736)`. -/ +theorem ks_off_vcpuWrA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {regWr : Bool} {b : U64} (h1 : (L.ksb s).val ≤ b.val) (h2 : b.val < (L.ksb s).val + 788736) : + ¬ VcpuWrFootA st regWr Z OS s b := + ks_off_vcpuWrCoreA J.toVcpuCoreA h1 h2 + +omit [J7PpA st J7] [J7FrA st J7] in +/-- Every `VsR (L.ksb s) Pt` word (under any `VrOK` with `FrUb st Ub' OS s`) is off `VcpuWrNoStA` (core). -/ +theorem vsr_off_vcpuWrCoreA + (J : JStage3VcpuCoreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {Pt Ub' : Set U64} + (vr : VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub' (L.ksb s) Pt) + (hFrUb : FrUb st Ub' OS s) + {regWr : Bool} {b : U64} (hb : VsR (L.ksb s) Pt b) : + ¬ VcpuWrFootA st regWr Z OS s b := by + intro hw + rcases hw with hvc | ⟨t, ht, K, -, hK, rfl⟩ | ⟨u, hu, ptr, sz, r, mp, ept, asid, va, bits, hc, hbits, hal, hb1, hb2⟩ + · exact vr.vc b hb (by cases regWr <;> [exact hvc.in_vcW; exact hvc]) + · have htH : H t = some .tcb := show t ∈ H.tcbs by rw [J.tcbs]; exact ht + have hfit := J.ent.heap.fits t .tcb htH + change t.val + 4096 ≤ 2 ^ 64 at hfit + have hKv : K.val < 4096 := by + rcases hK with h | rfl | rfl + · omega + · decide + · decide + have ev : (wa t K).val = t.val + K.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + obtain ⟨-, p, k, hp, hk, hin⟩ := vr.om (wa t K) hb + have hne : p ≠ t := fun e => by + rw [e, htH] at hp + rcases hk with rfl | ⟨_, rfl⟩ | ⟨_, rfl⟩ | ⟨rfl, -⟩ <;> cases hp + have hd := J.ent.heap.disj p k t .tcb hp htH hne + unfold InObj at hin; change p.val + 2 ^ k.bits ≤ t.val ∨ t.val + 4096 ≤ p.val at hd + rcases hd with hd | hd <;> omega + · exact vr.ub b hb (hFrUb u hu ptr sz r mp ept asid va hc bits hbits b hal hb1 hb2) + +omit [J7PpA st J7] [J7FrA st J7] in +/-- Every `VsR (L.ksb s) Pt` word (under any `VrOK` with `FrUb st Ub' OS s`) is off `VcpuWrNoStA`. -/ +theorem vsr_off_vcpuWrA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {Pt Ub' : Set U64} + (vr : VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub' (L.ksb s) Pt) + (hFrUb : FrUb st Ub' OS s) + {regWr : Bool} {b : U64} (hb : VsR (L.ksb s) Pt b) : + ¬ VcpuWrFootA st regWr Z OS s b := + vsr_off_vcpuWrCoreA J.toVcpuCoreA vr hFrUb hb + +omit [J7PpA st J7] in +/-- Every `VsR (L.ksb s) (utVsPtA st Pp Tb Lv s)` word is off `VcpuWrNoStA` (core). -/ +theorem utVs_off_vcpuWrCoreA + (J : JStage3VcpuCoreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {regWr : Bool} {b : U64} (hb : VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b) : + ¬ VcpuWrFootA st regWr Z OS s b := by + obtain ⟨Pt, Ub, vr, -, -, -, -, frub, -, -, hut⟩ := J7FrA.fr (st := st) _ _ _ _ _ _ J.j7 + have hsub := hut Pp Tb Lv J.ent.vs + have hbPt : VsR (L.ksb s) Pt b := by + rcases hb with ⟨p, hp, o, ho1, ho2, rfl⟩ | has + · exact Or.inl ⟨p, hsub hp, o, ho1, ho2, rfl⟩ + · exact Or.inr has + exact vsr_off_vcpuWrCoreA J vr frub hbPt + +omit [J7PpA st J7] in +/-- Every `VsR (L.ksb s) (utVsPtA st Pp Tb Lv s)` word is off `VcpuWrNoStA` (via `J7FrA.fr`). -/ +theorem utVs_off_vcpuWrA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {regWr : Bool} {b : U64} (hb : VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b) : + ¬ VcpuWrFootA st regWr Z OS s b := + utVs_off_vcpuWrCoreA J.toVcpuCoreA hb + +omit [J7PpA st J7] in +/-- Every `VcpuStateSnapA` word (`v + 16`, `v + 32`, `v + 40`, `v + 64 + 8*i` for `i < 64`) +is off `VcpuWrNoStA` (core). -/ +theorem vcpuSnap_off_vcpuWrCoreA + (J : JStage3VcpuCoreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {v o : U64} (hv : v ∈ Z.V) (ho1 : 16 ≤ o.val) (ho2 : o.val < 4096) (ho24 : o.val ≠ 24) : + ¬ VcpuWrNoStA st Z OS s (wa v o) := by + intro hw + obtain ⟨_, Ub, -, arm, -, fo, -, frub, -, vcub⟩ := J7FrA.fr (st := st) _ _ _ _ _ _ J.j7 + have hH := arm.vh v hv + have hfit := J.ent.heap.fits v _ hH + simp only [HKind.bits] at hfit + have ev0 : (wa v o).val = v.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + rcases hw with hvc | ⟨t, ht, K, -, hK, e⟩ | ⟨u, hu, ptr, sz, r, mp, ept, asid, va, bits, hc, hbits, hal, hb1, hb2⟩ + · rcases hvc with ⟨v', hv', e | e | e⟩ + · have hv'H := arm.vh v' hv' + by_cases he : v = v' + · subst he; have := congrArg UScalar.val e; omega + · have hd := J.ent.heap.disj v (.other 12) v' (.other 12) hH hv'H he + simp only [HKind.bits] at hd; have := congrArg UScalar.val e; omega + · have hv'H := arm.vh v' hv' + have hfit' := J.ent.heap.fits v' _ hv'H; change v'.val + 4096 ≤ 2 ^ 64 at hfit' + have h8 : (8#u64).val = 8 := rfl + have ev' : (wa v' 8#u64).val = v'.val + 8 := by rw [wa_valm, h8]; exact Nat.mod_eq_of_lt (by omega) + by_cases he : v = v' + · subst he; have := congrArg UScalar.val e; omega + · have hd := J.ent.heap.disj v (.other 12) v' (.other 12) hH hv'H he + change v.val + 4096 ≤ v'.val ∨ v'.val + 4096 ≤ v.val at hd; have := congrArg UScalar.val e; omega + · have hv'H := arm.vh v' hv' + have hfit' := J.ent.heap.fits v' _ hv'H; change v'.val + 4096 ≤ 2 ^ 64 at hfit' + have h24 : (24#u64).val = 24 := rfl + have ev' : (wa v' 24#u64).val = v'.val + 24 := by rw [wa_valm, h24]; exact Nat.mod_eq_of_lt (by omega) + by_cases he : v = v' + · subst he; have := congrArg UScalar.val e; omega + · have hd := J.ent.heap.disj v (.other 12) v' (.other 12) hH hv'H he + change v.val + 4096 ≤ v'.val ∨ v'.val + 4096 ≤ v.val at hd; have := congrArg UScalar.val e; omega + · have htH : H t = some .tcb := show t ∈ H.tcbs by rw [J.tcbs]; exact ht + have hfit_t := J.ent.heap.fits t .tcb htH; change t.val + 4096 ≤ 2 ^ 64 at hfit_t + have hKv : K.val < 4096 := by + rcases hK with h | rfl | rfl + · omega + · decide + · decide + have ev' : (wa t K).val = t.val + K.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have hne : v ≠ t := fun h => by rw [h, htH] at hH; cases hH + have hd := J.ent.heap.disj v (.other 12) t .tcb hH htH hne + change v.val + 4096 ≤ t.val ∨ t.val + 4096 ≤ v.val at hd; have := congrArg UScalar.val e; omega + · have hptrH := fo u hu ptr sz r mp ept asid va hc bits hbits + by_cases he : v = ptr + · subst he + have hbits12 : bits.val = 12 := HKind.other.inj (Option.some.inj (hH.symm.trans hptrH)).symm + have hv_al : Aligned v := by + have := J.ent.heap.align v _ hH; change v.val % 4096 = 0 at this; show v.val % 8 = 0; omega + have hv_ub : v ∈ Ub := frub u hu v sz r mp ept asid va hc bits hbits v hv_al (le_refl _) (by rw [hbits12]; omega) + exact vcub.1 v ⟨v, hv, 0#u64, by simp, by simp, (wa_zeroVcA v).symm⟩ hv_ub + · have hd := J.ent.heap.disj v (.other 12) ptr (.other bits.val) hH hptrH he + change v.val + 4096 ≤ ptr.val ∨ ptr.val + 2 ^ bits.val ≤ v.val at hd; omega + +omit [J7PpA st J7] in +/-- Every `VcpuStateSnapA` word (`v + 16`, `v + 32`, `v + 40`, `v + 64 + 8*i` for `i < 64`) +is off `VcpuWrNoStA`. -/ +theorem vcpuSnap_off_vcpuWrA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + {v o : U64} (hv : v ∈ Z.V) (ho1 : 16 ≤ o.val) (ho2 : o.val < 4096) (ho24 : o.val ≠ 24) : + ¬ VcpuWrNoStA st Z OS s (wa v o) := + vcpuSnap_off_vcpuWrCoreA J.toVcpuCoreA hv ho1 ho2 ho24 + +end disj + +/-! ## Section 3: `BlkOK`, `SchedValidA`, `VcpuStateSnapA`, and `JStage3TA` transport across `VcpuWrNoStA` -/ + +section transport +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J7 : JSlotA S} + [J7PpA st J7] [J7FrA st J7] + {s s' : S} {a a' : A} {σ σ' : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +private theorem blkOK_of_vcpuWrNoStA + (J : JStage3VcpuCoreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + {regWr : Bool} (w : WritesIn st s s' (VcpuWrFootA st regWr Z OS s)) : + SchedQueuesInv st H.tcbs Z.Cs H.omega H s' Z.Q Z.R ∧ + BXI st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s' := by + have hsq := J.sq + have hT := hsq.tcbs + have hk := w.1 + have hc := w.2.1 + have fld : ∀ y ∈ H.tcbs, ∀ K : U64, 3328 ≤ K.val → K.val < 3664 → K.val % 8 = 0 → + L.mem s' (wa y K) = L.mem s (wa y K) := fun y hy K h1 h2 h8 => + w.mem_eq (field_aligned (hT y hy) (by omega) h8) (tcbSched_off_vcpuWrCoreA J (J.tcbs ▸ hy) h1 h2) + have nod : ∀ c ∈ Z.Cs, ∀ o : U64, o.val < 4288 → o.val % 8 = 0 → + L.mem s' (L.nodeAt s c o) = L.mem s (L.nodeAt s c o) := fun c hc' o ho h8 => by + have hN := hsq.nodes c hc' + refine w.mem_eq (nodeAt_aligned hN ho h8) ?_ + obtain ⟨nn, -, hkn, pcs⟩ := J.sr.toSchedSRN.csN + have hcv : c.val < 128 := by have := (pcs c).1 hc'; omega + have hv := nodeAt_valSc J.sr.ksFit hcv ho + exact ks_off_vcpuWrCoreA J (by omega) (by omega) + have curN : ∀ o : U64, o.val < 4288 → o.val % 8 = 0 → + L.mem s' (L.nodeAt s' (L.cpu s') o) = L.mem s (L.nodeAt s (L.cpu s) o) := fun o ho h8 => by + rw [hc, nodeAt_of_ksb hk]; exact nod _ hsq.cur o ho h8 + have sig : ∀ b ∈ H.frame, L.mem s' b = L.mem s b := fun b hb => by + obtain ⟨hal, p, k, hp, hkind, hin⟩ := hb + refine w.mem_eq hal (heapObj_off_vcpuWrCoreA J hp ?_ ?_ hin.1 hin.2) + · rcases hkind with rfl | ⟨_, rfl⟩ | ⟨rfl, -⟩ <;> intro e <;> cases e + · intro bits; rcases hkind with rfl | ⟨_, rfl⟩ | ⟨rfl, -⟩ <;> intro e <;> cases e + have epw : ∀ e, (H e = some .ep ∨ H e = some .ntfn) → + L.mem s' (epHeadA e) = L.mem s (epHeadA e) ∧ L.mem s' (epTSA e) = L.mem s (epTSA e) := fun e he => by + have step : ∀ (o : U64), o.val < 16 → o.val % 8 = 0 → L.mem s' (wa e o) = L.mem s (wa e o) := fun o ho h8 => by + rcases he with hep | hnt + · have hfit := J.ent.heap.fits e .ep hep; have hal := J.ent.heap.align e .ep hep + change e.val + 16 ≤ 2 ^ 64 at hfit; change e.val % 16 = 0 at hal + have ev : (wa e o).val = e.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact w.mem_eq (by show (wa e o).val % 8 = 0; rw [ev]; omega) + (heapObj_off_vcpuWrCoreA J hep (by intro h; cases h) (fun _ h => by cases h) (by omega) (by change (wa e o).val < e.val + 16; omega)) + · have hfit := J.ent.heap.fits e .ntfn hnt; have hal := J.ent.heap.align e .ntfn hnt + change e.val + 64 ≤ 2 ^ 64 at hfit; change e.val % 64 = 0 at hal + have ev : (wa e o).val = e.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact w.mem_eq (by show (wa e o).val % 8 = 0; rw [ev]; omega) + (heapObj_off_vcpuWrCoreA J hnt (by intro h; cases h) (fun _ h => by cases h) (by omega) (by change (wa e o).val < e.val + 64; omega)) + exact ⟨by unfold epHeadA layout.ep.HEAD; exact step 0#u64 (by decide) (by decide), + by unfold epTSA layout.ep.TAIL_STATE; exact step 8#u64 (by decide) (by decide)⟩ + have hsb : ∀ y ∈ H.tcbs, L.mem s' (stA y) = L.mem s (stA y) ∧ L.mem s' (blockA y) = L.mem s (blockA y) := + fun y hy => ⟨by rw [stA_eq]; exact fld y hy _ (by simp) (by simp) (by simp), + by rw [blockA_eq]; exact fld y hy _ (by simp) (by simp) (by simp)⟩ + have hact : L.actionW s' = L.actionW s := by + unfold LawfulStore.actionW; exact curN _ (by rw [act_val]; omega) (by rw [act_val]) + have hnb : ∀ c, L.nodeBase s' c = L.nodeBase s c := fun c => by simp [LawfulStore.nodeBase, hk] + obtain ⟨E0, EQ, w1, w2, w5, w3, w4, w6⟩ := hsq.eps + have k3 := w6.2.2.1 + have ew : ∀ e ∈ E0, L.mem s' (epHeadA e) = L.mem s (epHeadA e) ∧ L.mem s' (epTSA e) = L.mem s (epTSA e) := + fun e he => epw e ((k3 e).1 he) + have sq' : SchedQueuesInv st H.tcbs Z.Cs H.omega H s' Z.Q Z.R := by + refine inv_of_agree hsq hk hc (fun y hy => ⟨?_, ?_⟩) + (fun c hc' o h1 h2 _ => nod c hc' o h1 h2) + (fun y hy => by rw [inqA_eq]; exact fld y hy _ (by simp) (by simp) (by simp)) + (fun y hy => by unfold affA; exact fld y hy _ (by simp) (by simp) (by simp)) + (fun y hy => Or.inl (by unfold prioA; exact fld y hy _ (by simp) (by simp) (by simp))) + (by rw [hact]; exact hsq.action) hsb + (by rw [hact]; intro haT; rw [(hsb _ haT).1]; exact hsq.actOK haT) ?_ + (by rw [curN _ (by rw [curth0]; omega) (by rw [curth0])]; exact hsq.curT) + (by rw [curN _ (by rw [idle8]; omega) (by rw [idle8])]; exact hsq.idleT) + · rw [nextA_eq']; exact fld y hy _ (by simp [nextOff]) (by simp [nextOff]) (by simp [nextOff]) + · rw [prevA_eq']; exact fld y hy _ (by simp [prevOff]) (by simp [prevOff]) (by simp [prevOff]) + refine ⟨E0, EQ, fun e he => ?_, w2, w5, fun e he y hy => ?_, fun y hy hyX hb => ?_, + w6.agree (fun y hy => Or.inl hy) (fun y hy _ _ => hsb y hy) (fun _ _ _ hy => hy) + (fun e he y hy => (hsb y ((w1 e he).2.2.mem y hy)).1) + (fun e he _ => by simp only [LawfulStore.epState, (ew e he).2]) + (fun e he h0 => Or.inl ⟨h0, by simp only [LawfulStore.epState, (ew e he).2]⟩)⟩ + · obtain ⟨hE, hEN, hq⟩ := w1 e he + have eq : L.epQ s' e = L.epQ s e := by simp only [LawfulStore.epQ, (ew e he).1, (ew e he).2] + refine ⟨hE, ⟨fun c hc' => by rw [hnb]; exact hEN.1 c hc', by rw [hk]; exact hEN.2⟩, ?_⟩ + rw [eq] + exact ⟨hq.nodup, hq.mem, hq.head, hq.last, hq.seg.congr fun y hy => + ⟨by rw [prevA_eq']; exact fld y (hq.mem y hy) _ (by simp [prevOff]) (by simp [prevOff]) (by simp [prevOff]), + by rw [nextA_eq']; exact fld y (hq.mem y hy) _ (by simp [nextOff]) (by simp [nextOff]) (by simp [nextOff])⟩⟩ + · have hyT := (w1 e he).2.2.mem y hy; rw [(hsb y hyT).1, (hsb y hyT).2]; exact w3 e he y hy + · rw [(hsb y hy).1] at hb; rw [(hsb y hy).2]; exact w4 y hy hyX hb + obtain ⟨-, hB, hX, hSg, hS⟩ := J.ent.bxi + have hsc : ∀ y ∈ H.tcbs, L.mem s' (scA y) = L.mem s (scA y) := fun y hy => by + rw [scA_eq]; exact fld y hy _ (by simp) (by simp) (by simp) + have hst : ∀ y ∈ H.tcbs, L.mem s' (stA y) = L.mem s (stA y) := fun y hy => (hsb y hy).1 + have hinq : ∀ y ∈ H.tcbs, L.mem s' (inqA y) = L.mem s (inqA y) := fun y hy => by + rw [inqA_eq]; exact fld y hy _ (by simp) (by simp) (by simp) + have ec : L.curW s' = L.curW s := by + unfold LawfulStore.curW; exact curN _ (by rw [curth0]; omega) (by rw [curth0]) + have ei : L.idleW s' = L.idleW s := by + unfold LawfulStore.idleW; exact curN _ (by rw [idle8]; omega) (by rw [idle8]) + have ecs : L.cscW s' = L.cscW s := by + unfold LawfulStore.cscW LawfulStore.nodeA + exact curN _ (by simp [layout.node.CUR_SC]) (by simp [layout.node.CUR_SC]) + have hB' : BoundInv st H.tcbs Ac s' := + hB.mk' hsc (fun y hy hf => Or.inl (by rw [← hinq y hy]; exact hf)) + (fun y hy hf => Or.inr (Or.inl (by rw [← hinq y hy]; exact hf))) + (fun haT => by rw [hact] at haT ⊢; exact ⟨haT, hB.act haT⟩) + (by rw [ei]; exact ⟨hsq.idleT, hB.idle⟩) + (fun h0' => by rw [hact] at h0'; rw [ec]; exact ⟨hsq.curT, hB.resume h0'⟩) + (hB.nb ei (hst _ hsq.idleT)) + (fun y hy hS0 _ => Or.inl (Sch.of_eq (fun hf => by rwa [hinq y hy] at hf) + (fun hf => by rwa [hinq y hy] at hf) hact ei ec (hst y hy) hS0)) + have tm := hX.2.2.tm + have tmo : TimerOK st H.tcbs Z.Cs s' := by + refine ⟨fun c hc' => (tm.npl c hc').ksb hk, fun y hy => ?_, fun y hy => ?_, fun y hy => ?_, + fun c hc' => ?_, fun c hc' => ?_, fun y hy hd c hcv => ?_, fun y hy => ?_, fun y hy => ?_, fun c hc' => ?_⟩ + · rw [show tlA y = wa y 3544#u64 from rfl, fld y hy _ (by simp) (by simp) (by simp)]; exact tm.lt y hy + · rw [show trA y = wa y 3552#u64 from rfl, fld y hy _ (by simp) (by simp) (by simp)]; exact tm.rt y hy + · rw [show tpA y = wa y 3560#u64 from rfl, fld y hy _ (by simp) (by simp) (by simp)]; exact tm.pt y hy + · rw [nodeAt_of_ksb hk, nod c hc' _ (by rw [timer_root_val]; omega) (by rw [timer_root_val])]; exact tm.root c hc' + · rw [nodeAt_of_ksb hk, nod c hc' _ (by rw [timer_min_val]; omega) (by rw [timer_min_val])]; exact tm.min c hc' + · rw [show tdA y = wa y 3528#u64 from rfl, fld y hy _ (by simp) (by simp) (by simp)] at hd + rw [show thA y = wa y 3568#u64 from rfl, fld y hy _ (by simp) (by simp) (by simp)] at hcv + exact tm.core y hy hd c hcv + · rw [show dnA y = wa y 3576#u64 from rfl, fld y hy _ (by simp) (by simp) (by simp)]; exact tm.dn y hy + · rw [show dpA y = wa y 3584#u64 from rfl, fld y hy _ (by simp) (by simp) (by simp)]; exact tm.dp y hy + · rw [nodeAt_of_ksb hk, nod c hc' _ (by simp [layout.node.DEBUG_TCBS]) (by simp [layout.node.DEBUG_TCBS])] + exact tm.dh c hc' + have ntb : NtbOK st H.tcbs H.ntfns s' := fun n hn => by rw [sig _ (hX.1.ntB n hn).2]; exact hX.2.2.ntb n hn + have rb : TimerRB st H.tcbs Z.Cs s' := hX.2.2.rb.congr + ⟨fun y hy => fld y hy 3528#u64 (by simp) (by simp) (by simp), + fun y hy => fld y hy 3544#u64 (by simp) (by simp) (by simp), + fun y hy => fld y hy 3552#u64 (by simp) (by simp) (by simp), + fun y hy => fld y hy 3560#u64 (by simp) (by simp) (by simp), + fun y hy => fld y hy 3568#u64 (by simp) (by simp) (by simp), + fun c hc' => by rw [nodeAt_of_ksb hk, nod c hc' _ (by rw [timer_root_val]; omega) (by rw [timer_root_val])], + fun c hc' => by rw [nodeAt_of_ksb hk, nod c hc' _ (by rw [timer_min_val]; omega) (by rw [timer_min_val])]⟩ + have hX' : XInv st H.tcbs Z.Cs H.omega H.frame H.replies H.scs H.ntfns H.scBits s' := + hX.keep (fun r hr => ⟨sig _ (hX.1.tcbW hr), sig _ (hX.1.prevW hr), sig _ (hX.1.nextW hr)⟩) + (fun c hc' => ⟨sig _ (hX.1.fixW c hc').1, sig _ (hX.1.fixW c hc').2⟩) hsc + (fun y hy => by unfold replyA; exact fld y hy _ (by simp) (by simp) (by simp)) hst + (by rw [ecs]; exact hX.2.2.cur) (fun c hc' => sig _ (hX.1.repW c hc')) + (fun y hy => by unfold boundA; exact fld y hy _ (by simp) (by simp) (by simp)) + (fun n hn => sig _ (hX.1.ntW n hn)) ⟨tmo, ntb, rb, fun n hn => sig _ (hX.1.ntB n hn).2⟩ + (fun c hc' => sig _ (hX.1.snW c hc')) + (fun y hy => by unfold yieldA; exact fld y hy _ (by simp) (by simp) (by simp)) + (fun c hc' => sig _ (hX.1.syW c hc')) + (fun c hc' => sig _ (hX.1.mxW c hc')) ei + (fun y hy => by unfold affA; exact fld y hy _ (by simp) (by simp) (by simp)) + have hS' : SCsOK st H.tcbs H.omega H.frame H.scs H.scBits Ac s' := by + refine ⟨hS.sub, fun b hb => by rw [hk]; exact hS.ks b hb, fun o h1 h2 => by rw [hk]; exact hS.ksΩ o h1 h2, + fun t ht hb hne => ?_, fun c hc' => (hS.all c hc').agree sig, + fun c hc' => by rw [show L.rmax s' c = L.rmax s c from sig _ (hS.all c hc').2.1]; exact hS.act c hc', + fun c hc' h1 => hS.spor c hc' (by rw [← sig _ (hS.spSg c hc')]; exact h1), hS.spSg, + fun c hc' h1 => hS.rep c hc' (by rw [← sig _ (hS.repSg c hc')]; exact h1), hS.repSg, + fun c hc' => by rw [hk]; exact hS.scKs c hc'⟩ + rw [hsc t ht] at hb hne ⊢; rw [ecs] at hne ⊢; exact hS.disj t ht hb hne + have hSg' : SgOK st H.omega H.frame s' := + ⟨hSg.sub, fun b hb => by rw [hk]; exact hSg.ks b hb, fun o h1 h2 => by rw [hk]; exact hSg.ksΩ o h1 h2⟩ + exact ⟨sq', ⟨Z.Q, Z.R, sq'⟩, hB', hX', hSg', hS'⟩ + +/-- `SchedValidA` is preserved by any `VcpuSpecStepA` step. -/ +theorem SchedValidA.of_vcpuStepA {σ σ' : Fl4SpecA64.State Unit} {regWr : Bool} + (hSV : SchedValidA σ) (HS : VcpuSpecStepA H σ σ' regWr) : SchedValidA σ' := by + refine hSV.congr HS.rq HS.rls HS.act HS.cur HS.idle HS.csc + (fun p y k h => HS.keepObj h (fun _ e => by cases e) (fun _ e => by cases e)) + (fun t x h => ?_) (fun t x' h => ?_) + · rcases HS.kh t with e | ⟨y, y', h1, h2, hk⟩ | ⟨_, _, h1, -⟩ + · exact ⟨x, by rw [e]; exact h, rfl⟩ + · rw [h] at h1; cases h1; exact ⟨y', h2, hk.sc⟩ + · rw [h] at h1; cases h1 + · rcases HS.kh t with e | ⟨y, y', h1, h2, hk⟩ | ⟨_, _, _, h2, -⟩ + · exact ⟨x', by rw [← e]; exact h, rfl, rfl⟩ + · rw [h] at h2; cases h2; exact ⟨y, h1, hk.state, hk.sc⟩ + · rw [h] at h2; cases h2 + +/-- (record edit #8b-C, item 17) A bound VCPU's `OWNER_CPU` word is `≤ 128`, from the snapshot at the state. -/ +theorem JStage3TA.ownLeA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + (hSnap : VcpuStateSnapA Z.V (L.mem s) σ) : ∀ v ∈ Z.V, (L.mem s (wa v 8#u64)).val ≤ 128 := fun v hv => by + obtain ⟨x, hx, -⟩ := J.j.2.1.obj v hv + exact (hSnap.snap v hv x hx).2.2.2.2 + +omit [J7PpA st J7] in +/-- `VcpuStateSnapA` is preserved across any `VcpuWrNoStA` + `VcpuSpecStepA` step (core). -/ +theorem VcpuStateSnapA.of_vcpuStepCoreA + (J : JStage3VcpuCoreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + (hSnap : VcpuStateSnapA Z.V (L.mem s) σ) + (w : WritesIn st s s' (VcpuWrNoStA st Z OS s)) + (HS : VcpuSpecStepA H σ σ') + (hOwn : ∀ v ∈ Z.V, (L.mem s' (wa v 8#u64)).val ≤ 128) : + VcpuStateSnapA Z.V (L.mem s') σ' := by + obtain ⟨_, _, -, arm, -, -, -, -, -, -⟩ := J7FrA.fr (st := st) _ _ _ _ _ _ J.j7 + refine ⟨fun v hv x' hkx' => ?_, fun v hv x' hkx' => ?_⟩ + · have hH := arm.vh v hv + have hfit := J.ent.heap.fits v _ hH; have hal := J.ent.heap.align v _ hH + simp only [HKind.bits] at hfit hal + have keepO : ∀ o : U64, 16 ≤ o.val → o.val < 4096 → o.val % 8 = 0 → o.val ≠ 24 → + L.mem s' (wa v o) = L.mem s (wa v o) := fun o h1 h2 h8 h24 => by + have ev : (wa v o).val = v.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact w.mem_eq (by show (wa v o).val % 8 = 0; rw [ev]; omega) (vcpuSnap_off_vcpuWrCoreA J hv h1 h2 h24) + rcases HS.kh (specPtr v) with e | ⟨_, _, _, h2, -⟩ | ⟨x0, x1, h1, h2, hreg, hvppi, hvgic, her, hoff, hout⟩ + · have h0 := hSnap.snap v hv x' (by rw [← e]; exact hkx') + refine ⟨fun i hi => ?_, ?_, ?_, ?_, hOwn v hv⟩ + · have ho : (⟨BitVec.ofNat 64 (64 + 8 * i)⟩ : U64).val = 64 + 8 * i := by + show (BitVec.ofNat 64 _).toNat = _; rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + rw [keepO _ (by omega) (by omega) (by omega) (by omega)]; exact h0.1 i hi + · rw [keepO 16#u64 (by simp) (by simp) (by simp) (by simp)]; exact h0.2.1 + · rw [keepO 32#u64 (by simp) (by simp) (by simp) (by simp)]; exact h0.2.2.1 + · rw [keepO 40#u64 (by simp) (by simp) (by simp) (by simp)]; exact h0.2.2.2.1 + · rw [hkx'] at h2; cases h2 + · rw [hkx'] at h2; cases h2 + have h0 := hSnap.snap v hv x0 h1 + refine ⟨fun i hi => ?_, ?_, ?_, ?_, hOwn v hv⟩ + · have ho : (⟨BitVec.ofNat 64 (64 + 8 * i)⟩ : U64).val = 64 + 8 * i := by + show (BitVec.ofNat 64 _).toNat = _; rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + rw [keepO _ (by omega) (by omega) (by omega) (by omega), h0.1 i hi] + unfold fl4VcpuRegGet; rw [hreg, hvgic, her] + · rw [keepO 16#u64 (by simp) (by simp) (by simp) (by simp), hvppi]; exact h0.2.1 + · rw [keepO 32#u64 (by simp) (by simp) (by simp) (by simp), hoff]; exact h0.2.2.1 + · rw [keepO 40#u64 (by simp) (by simp) (by simp) (by simp), hout]; exact h0.2.2.2.1 + · rcases HS.kh (specPtr v) with e | ⟨_, _, _, h2, -⟩ | ⟨x0, x1, h1, h2, -, -, -, -, -, hout⟩ + · exact hSnap.mask v hv x' (by rw [← e]; exact hkx') + · rw [hkx'] at h2; cases h2 + · rw [hkx'] at h2; cases h2; rw [hout]; exact hSnap.mask v hv x0 h1 + +omit [J7PpA st J7] in +/-- `VcpuStateSnapA` is preserved across any `VcpuWrNoStA` + `VcpuSpecStepA` step. -/ +theorem VcpuStateSnapA.of_vcpuStepA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + (hSnap : VcpuStateSnapA Z.V (L.mem s) σ) + (w : WritesIn st s s' (VcpuWrNoStA st Z OS s)) + (HS : VcpuSpecStepA H σ σ') + (hOwn : ∀ v ∈ Z.V, (L.mem s' (wa v 8#u64)).val ≤ 128) : + VcpuStateSnapA Z.V (L.mem s') σ' := + VcpuStateSnapA.of_vcpuStepCoreA J.toVcpuCoreA hSnap w HS hOwn + +set_option maxHeartbeats 1600000 in +/-- **Master `JStage3VcpuCoreA` transport across any `VcpuWrFootA` + `VcpuSpecStepA` step** +(serves both `JStage3TA.of_vcpuStepA` and `JStage3A.of_vcpuStepA` without requiring `SActA σ`). -/ +theorem JStage3VcpuCoreA.of_vcpuStepA + (J : JStage3VcpuCoreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + (R6 : VcpuRec6A st Z OS Bf s σ) + {hw' : HwRootA} {regWr : Bool} + (w : WritesIn st s s' (VcpuWrFootA st regWr Z OS s)) + (HS : VcpuSpecStepA H σ σ' regWr) + (har : AR a' hw') + (hUtVs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → L.mem s' b = L.mem s b) + (hVcCore : VcpuCoreA (L := L) { Z with hw := hw' } s' σ') + (hVcRun : VcRunA st Z.T Z.V s') + (hRunVcpu : RunVcpuInv st Z.T s') + (hEpTcb : ∀ t ∈ Z.T, ∃ x, σ'.kheap (specPtr t) = some (.tcb x) ∧ TcbRelE Rp (L.mem s') t x) + (hEpBuf : ∀ bp ∈ Bf, ∀ k : U64, k.val < 128 → + (L.mem s' (bufW bp.1 k)).bv = loadWordAt σ'.machineState.underlyingMemory (bufAddr bp.2 k.val) ∧ + (bufAddr bp.2 k.val).toNat % 8 = 0) + (hDeadVcpu : ∀ v ∈ Z.V, DeadA st OS .vcpu s' v → ∀ x, σ'.kheap (specPtr v) = some (.archObj (.vcpu x)) → + x.vcpuTcb = none) + (hDeadTvcpu : ∀ t ∈ Z.T, DeadA st OS .tcb s' t → L.mem s' (wa t 3664#u64) = 0#u64) + (hDeadVcRun : ∀ v ∈ Z.V, DeadA st OS .vcpu s' v → L.mem s' (wa v 24#u64) = 0#u64) + (hZombNoVc : ZombieTcbNoVcpuA st OS s') : + JStage3VcpuCoreA st AR { Z with hw := hw' } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s' a' σ' m := by + have hk := w.1 + have hc := w.2.1 + have osk := osKeep_of_vcpuWrCoreA J R6.vcOS R6.frOff w + have capE : ∀ u ∈ OS, capA st s' u = capA st s u := fun u hu => by + unfold capA; rw [(osk u hu).1, (osk u hu).2.1] + obtain ⟨sq', bxi'⟩ := blkOK_of_vcpuWrNoStA J w + have hT := J.sq.tcbs + have tcbS : ∀ t ∈ Z.T, ∀ K : U64, 3328 ≤ K.val → K.val < 3664 → K.val % 8 = 0 → + L.mem s' (wa t K) = L.mem s (wa t K) := fun t ht K h1 h2 h8 => + w.mem_eq (field_aligned (hT t (J.tcbs ▸ ht)) (by omega) h8) (tcbSched_off_vcpuWrCoreA J ht h1 h2) + have ksK : ∀ o : U64, o.val < 788736 → o.val % 8 = 0 → + L.mem s' (wa (L.ksb s) o) = L.mem s (wa (L.ksb s) o) := fun o ho h8 => by + have hksw := J.ent.ksw; have hka := J.j.1.ksbAl + have ev : (wa (L.ksb s) o).val = (L.ksb s).val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact w.mem_eq (by show (wa (L.ksb s) o).val % 8 = 0; rw [ev]; omega) (ks_off_vcpuWrCoreA J (by omega) (by omega)) + have nodK : ∀ c ∈ Z.Cs, ∀ o : U64, o.val < 4288 → o.val % 8 = 0 → + L.mem s' (L.nodeAt s c o) = L.mem s (L.nodeAt s c o) := fun c hc' o ho h8 => by + have hN := J.sq.nodes c hc' + obtain ⟨nn, -, hkn, pcs⟩ := J.sr.toSchedSRN.csN + have hcv : c.val < 128 := by have := (pcs c).1 hc'; omega + have hv := nodeAt_valSc J.sr.ksFit hcv ho + exact w.mem_eq (nodeAt_aligned hN ho h8) (ks_off_vcpuWrCoreA J (by omega) (by omega)) + have slotK : ∀ y, SlotWA Z.th y → ∀ x, (x = y ∨ x = wa y 8#u64 ∨ x = wa y 16#u64 ∨ x = wa y 24#u64) → + L.mem s' x = L.mem s x := by + intro y hy x hx + have hIs : IsSlotA Z.th y := by + rcases hy with h | ⟨p, r, hp, idx, hl, rfl⟩ + · exact h + · obtain ⟨e, hb, -⟩ := cn_slotV J.sr.place hp hl + exact ⟨p, Or.inl ⟨r, hp, blvA idx, hb, e⟩⟩ + have hyOS : y ∈ OS := J.j5.2.1 y hIs + rcases hx with hx | hx | hx | hx + · rw [hx]; exact (osk y hyOS).1 + · rw [hx]; exact (osk y hyOS).2.1 + · rw [hx]; exact (osk y hyOS).2.2.1 + · rw [hx]; exact (osk y hyOS).2.2.2 + have scK : ∀ c ∈ Z.C, ∀ o : U64, o.val < 128 → o.val % 8 = 0 → L.mem s' (wa c o) = L.mem s (wa c o) := by + intro c hc' o ho h8 + have hcH : c ∈ H.scs := by rw [J.scs]; exact hc' + obtain ⟨z, hz⟩ := hcH + have hmin := J.ent.heap.scmin c z hz + have hfit := J.ent.heap.fits c (.sc z) hz; have hal := J.ent.heap.align c (.sc z) hz + change c.val + 2 ^ z ≤ 2 ^ 64 at hfit; change c.val % 2 ^ z = 0 at hal + have hpow : 128 ≤ 2 ^ z := by + have : 2 ^ 7 ≤ 2 ^ z := Nat.pow_le_pow_right (by omega) hmin; simpa using this + have h8div : 8 ∣ 2 ^ z := ⟨2 ^ (z - 3), by rw [show (8 : Nat) = 2 ^ 3 from rfl, ← Nat.pow_add]; congr 1; omega⟩ + have hc8 : c.val % 8 = 0 := Nat.dvd_iff_mod_eq_zero.mp (Nat.dvd_trans h8div (Nat.dvd_iff_mod_eq_zero.mpr hal)) + have ev : (wa c o).val = c.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact w.mem_eq (by show (wa c o).val % 8 = 0; rw [ev]; omega) + (heapObj_off_vcpuWrCoreA J hz (by intro e; cases e) (fun _ e => by cases e) (by omega) (by change (wa c o).val < c.val + 2 ^ z; omega)) + -- VSpace / page table preservation via `J7FrA` + obtain ⟨Pt0, Ub0, vr0, -, -, -, link0, frub0, -, -⟩ := J7FrA.fr (st := st) _ _ _ _ _ _ J.j7 + have vsr0 : ∀ b, VsR (L.ksb s) Pt0 b → L.mem s' b = L.mem s b := fun b hb => + w.mem_eq (vr0.al b hb) (vsr_off_vcpuWrCoreA J vr0 frub0 hb) + have vsfoot : ∀ b, VsFootA Z.Pp Z.S1 Z.Y (L.ksb s) b → L.mem s' b = L.mem s b := fun b hb => by + refine vsr0 b ?_ + rcases hb.vsR with ⟨p, hp, h⟩ | h + · refine Or.inl ⟨p, link0 p ?_, h⟩ + rcases hp with hp | hp | hp + · exact Or.inl hp + · exact Or.inr (Or.inl hp) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl hp)))))) + · exact Or.inr h + have word : ∀ p, SchedPageA st Z OS s p → ∀ i : U64, i.val < 512 → L.mem s' (peAF p i) = L.mem s (peAF p i) := + fun p hp i hi => by + refine vsr0 _ (Or.inl ⟨p, link0 p hp, core.num.U64.wrapping_mul i 8#u64, ?_, ?_, rfl⟩) + · rw [wm8_valA hi]; omega + · rw [wm8_valA hi]; omega + have extfoot : ∀ b, ExtFootE Z.X Z.Y Z.T2 b → L.mem s' b = L.mem s b := fun b hb => by + cases hb with + | s2lo r i hr hi => exact word r (Or.inr (Or.inr (Or.inr (Or.inl hr)))) i hi + | s2hi r i hr hi => exact word _ (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨r, hr, rfl⟩))))) i hi + | s2pt t i ht hi => exact word t (Or.inr (Or.inr (Or.inl ht))) i hi + | wl0 y hy => exact word y (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl hy)))))) _ (by simp) + | wlo y i hy hi => exact word y (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl hy)))))) i (by omega) + have j7' : J7 { Z with hw := hw' } H OS s' σ' m := + J7FrA.keepFr (st := st) Z hw' H OS s s' σ σ' m J.j7 hk osk + (fun Pt Ub vr hfu b hb => w.mem_eq (vr.al b hb) (vsr_off_vcpuWrCoreA J vr hfu hb)) + have utVsK : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → L.mem s' b = L.mem s b := fun b hb => + hUtVs b hb + have ⟨vsTy', utVsEq⟩ := utVsPt_keepA J.ent.vs hk utVsK + have ent' : EntryInvA3 st H Z.Cs OS U Ac Pp Tb Lv s' := + ⟨J.ent.heap, bxi', J.ent.os, J.ent.user.ksb hk, vsTy', by rw [hk]; exact J.ent.ksw⟩ + have vsOk' : UtVsOkA st H OS Z.V Pp Tb Lv s' := by + unfold UtVsOkA; rw [hk, utVsEq]; exact J.vsOk + have hTr : TreeA st OS Z.G.chains Z.G.par s := + J.j.1.cdt.tree.grow J.j5.2.1 (fun x hx hn => J.j5.2.2 x hx hn) + have j5' : J5A st { Z with hw := hw' } H OS s' σ' m := by + refine ⟨⟨MdbInvA.keepO hTr.lk hTr.tree J.j5.1.1 osk, IrqRunA.keepO hTr.lk J.j5.1.2.1 osk, + HandlersActiveA.keepO hTr.lk J.j5.1.2.2 osk hk ?_⟩, J.j5.2.1, + fun y hy hn => by rw [capE y hy]; exact J.j5.2.2 y hy hn⟩ + intro i hi + have eI : irqStAddrA (L.ksb s) i = irqWordA (L.ksb s) i := rfl + have hv := irqWordA_val J.sr.ksFit hi + have hka := J.j.1.ksbAl + rw [eI] + exact w.mem_eq (by show (irqWordA (L.ksb s) i).val % 8 = 0; omega) (ks_off_vcpuWrCoreA J (by omega) (by omega)) + -- `StateRelA`, `CdtRelA`, `SchedRelA`, `IrqRelA`, `SchedRestA`, `VsRelA`, `ExtVsRelA` + have stRel' : StateRelA Z.th (L.mem s') σ' := by + refine ⟨fun p r hp => ?_, fun t ht => ?_⟩ + · obtain ⟨cs, hcs, hwf, hsl⟩ := J.j.1.st.cnode p r hp + refine ⟨cs, HS.keepObj hcs (fun _ e => by cases e) (fun _ e => by cases e), hwf, fun idx hlen => ?_⟩ + have hsw : SlotWA Z.th (slotAddrA (specPtr p) (ofBl idx)) := Or.inr ⟨p, r, hp, idx, hlen, rfl⟩ + have hsr := hsl idx hlen + unfold SlotRelA at hsr ⊢; unfold capAtA at hsr ⊢ + rw [slotK _ hsw _ (Or.inl rfl), slotK _ hsw _ (Or.inr (Or.inl rfl))] + exact hsr + · obtain ⟨x, hx, hsl⟩ := J.j.1.st.tcb t ht + have ⟨x', hx', hk⟩ : ∃ x', σ'.kheap (specPtr t) = some (.tcb x') ∧ TcbKeepFieldsA x x' := by + rcases HS.kh (specPtr t) with e | ⟨y, y', h1, h2, hk⟩ | ⟨_, _, h1, -⟩ + · exact ⟨x, by rw [e]; exact hx, ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, fun _ => id⟩⟩ + · rw [hx] at h1; cases h1; exact ⟨y', h2, hk⟩ + · rw [hx] at h1; cases h1 + refine ⟨x', hx', fun i hi => ?_⟩ + have hsw : SlotWA Z.th (slotAddrA (specPtr t) (BitVec.ofNat 64 i)) := + Or.inl ⟨t, Or.inr ⟨ht, i, hi, rfl⟩⟩ + have hsr := hsl i hi + unfold SlotRelA at hsr ⊢; unfold capAtA at hsr ⊢ + rw [slotK _ hsw _ (Or.inl rfl), slotK _ hsw _ (Or.inr (Or.inl rfl))] + simp only [tcbCnodeMap, hk.ctable, hk.vtable, hk.ipcfr, hk.fh, hk.th, hk.s2, hk.hv]; exact hsr + have cdt' : CdtRelA st Z.th s' σ' Z.G := by + have h0 := J.j.1.cdt + have oskIs : OSKeep st {a | IsSlotA Z.th a} s s' := fun y hy => osk y (J.j5.2.1 y hy) + have absIs : ∀ sl, AbsSlotA Z.th sl → IsSlotA Z.th (cslotAddrA sl) := fun sl hsl => by + obtain ⟨p, hp1, hp2⟩ := hsl + unfold cslotAddrA; rw [hp1] + rcases hp2 with hcn | ⟨ht, i, hi, hidx⟩ + · obtain ⟨e, hb, -⟩ := cn_slotV J.sr.place hcn rfl + exact ⟨p, Or.inl ⟨sl.2.length, hcn, blvA sl.2, hb, e⟩⟩ + · rw [hidx, ofBl_tcbIdx i (by unfold tcbSlotsA at hi; omega)] + exact ⟨p, Or.inr ⟨ht, i, hi, rfl⟩⟩ + refine ⟨h0.tree.keepO oskIs, fun sl hsl => by rw [HS.cdt]; exact h0.cdt sl hsl, + fun sl q hq => h0.cdt_slot sl q (by rw [← HS.cdt]; exact hq), + fun sl hsl => by + have hsw : IsSlotA Z.th (cslotAddrA sl) := absIs sl hsl + rw [HS.orig]; unfold revA; rw [(oskIs _ hsw).2.2.2]; exact h0.orig sl hsl, + h0.par_slot, + fun a p hp => by + obtain ⟨sl, hsl, ha⟩ := h0.par_slot a p hp + have hcd := h0.cdt sl hsl + rw [ha, hp] at hcd + cases hcsl : σ.cdt sl with + | none => rw [hcsl] at hcd; cases hcd + | some q => + rw [hcsl] at hcd; simp at hcd + have hqIs : IsSlotA Z.th p := hcd ▸ absIs q (h0.cdt_slot sl q hcsl).2 + unfold revA; rw [(oskIs p hqIs).2.2.2]; exact h0.prev a p hp⟩ + have irq' : IrqRelA (L.mem s') (L.ksb s') σ' := by + have h0 := J.j.1.irq + have hI : ∀ i : U64, i.val < 5376 → L.mem s' (irqWordA (L.ksb s') i) = L.mem s (irqWordA (L.ksb s) i) := fun i hi => by + rw [hk]; have hv := irqWordA_val J.sr.ksFit hi; have hka := J.j.1.ksbAl + exact w.mem_eq (by show (irqWordA (L.ksb s) i).val % 8 = 0; omega) (ks_off_vcpuWrCoreA J (by omega) (by omega)) + refine ⟨fun i hi => by rw [HS.irqS, hI i hi]; exact h0.st i hi, + fun i hi => by rw [HS.irqN, hk]; exact h0.node i hi, + fun i hi => by rw [HS.irqN, hk]; exact h0.nodeHi i hi⟩ + have hcore' : SchedCoreA st Z.T Z.Cs s' Z.Q Z.R := by + have h0 := J.sr.core + have haff : ∀ t ∈ Z.T, L.mem s' (affA t) = L.mem s (affA t) := fun t ht => by + unfold affA; exact tcbS t ht _ (by decide) (by decide) (by decide) + have hprio : ∀ t ∈ Z.T, L.mem s' (prioA t) = L.mem s (prioA t) := fun t ht => by + unfold prioA; exact tcbS t ht _ (by decide) (by decide) (by decide) + have hinq : ∀ t ∈ Z.T, L.mem s' (inqA t) = L.mem s (inqA t) := fun t ht => by + unfold inqA; exact tcbS t ht _ (by decide) (by decide) (by decide) + refine ⟨J.tcbs ▸ sq'.tcbs, fun c hc' => J.tcbs ▸ sq'.nodes c hc', + fun t ht => by rw [haff t ht]; exact h0.aff t ht, + fun t ht => by rw [hprio t ht]; exact h0.prio t ht, + fun c hc' => J.tcbs ▸ sq'.ready c hc', + fun c hc' => J.tcbs ▸ sq'.release c hc', + fun c hc' p hp t ht => by + have htT : t ∈ Z.T := ((h0.ready c hc').1.rel p hp).mem t ht + rw [haff t htT, hprio t htT]; exact h0.inQ c hc' p hp t ht, + fun c hc' t ht => by + have htT : t ∈ Z.T := (h0.release c hc').mem t ht + rw [haff t htT]; exact h0.inR c hc' t ht, + fun t ht => by rw [hinq t ht, haff t ht, hprio t ht]; exact h0.queued t ht, + fun t ht => by rw [hinq t ht, haff t ht]; exact h0.released t ht, + fun t ht => by rw [haff t ht, hprio t ht]; exact h0.excl t ht⟩ + have hScInv' : ScInvA m σ' := by + have h0 := J.sr.inv + have hkhSc : ∀ p sc n', σ'.kheap p = some (.schedContext sc n') ↔ σ.kheap p = some (.schedContext sc n') := fun p sc n' => + ⟨fun h => HS.backObj h (fun _ e => by cases e) (fun _ e => by cases e), + fun h => HS.keepObj h (fun _ e => by cases e) (fun _ e => by cases e)⟩ + have hkhTcb : ∀ p x, σ'.kheap p = some (.tcb x) → ∃ x0, σ.kheap p = some (.tcb x0) ∧ + x.tcbState = x0.tcbState ∧ x.tcbSchedContext = x0.tcbSchedContext ∧ + x.tcbDomain = x0.tcbDomain ∧ x.tcbPriority = x0.tcbPriority := fun p x hx => by + rcases HS.kh p with e | ⟨x0, x1, h1, h2, hkf⟩ | ⟨_, _, _, h2, -⟩ + · exact ⟨x, by rw [← e]; exact hx, rfl, rfl, rfl, rfl⟩ + · rw [hx] at h2; cases h2; exact ⟨x0, h1, hkf.state, hkf.sc, hkf.dom, hkf.prio⟩ + · rw [hx] at h2; cases h2 + have hkhTcbF : ∀ p x0, σ.kheap p = some (.tcb x0) → ∃ x, σ'.kheap p = some (.tcb x) ∧ + x.tcbState = x0.tcbState ∧ x.tcbSchedContext = x0.tcbSchedContext ∧ + x.tcbDomain = x0.tcbDomain ∧ x.tcbPriority = x0.tcbPriority := fun p x0 hx0 => by + rcases HS.kh p with e | ⟨y0, y1, h1, h2, hkf⟩ | ⟨_, _, h1, -⟩ + · exact ⟨x0, by rw [e]; exact hx0, rfl, rfl, rfl, rfl⟩ + · rw [hx0] at h1; cases h1; exact ⟨y1, h2, hkf.state, hkf.sc, hkf.dom, hkf.prio⟩ + · rw [hx0] at h1; cases h1 + have hrt_eq : ∀ u ∈ σ.releaseQueue, specRtA σ' u = specRtA σ u := fun u hu => by + obtain ⟨x0, p, y, n', hx0, hsc0, hkp, hact⟩ := h0.relSc u hu + have hne := J.sinv.rpos p y n' hkp hact + obtain ⟨x, hx, -, hsc, -⟩ := hkhTcbF u x0 hx0 + rw [specRtA_of hx (hsc.trans hsc0) ((hkhSc p y n').mpr hkp) hne, + specRtA_of hx0 hsc0 hkp hne] + refine ⟨fun p y n' h => h0.refills p y n' ((hkhSc p y n').mp h), + fun p y n' h => h0.rr p y n' ((hkhSc p y n').mp h), + fun q x h => h0.ntfnIdle q x (HS.backObj h (fun _ e => by cases e) (fun _ e => by cases e)), + fun q x h => h0.ntfnIdleOf q x (HS.backObj h (fun _ e => by cases e) (fun _ e => by cases e)), + fun u hu => by + rw [HS.rls] at hu + obtain ⟨x0, p, y, n', hx0, hsc0, hkp, hact⟩ := h0.relSc u hu + obtain ⟨x, hx, -, hsc, -⟩ := hkhTcbF u x0 hx0 + exact ⟨x, p, y, n', hx, hsc.trans hsc0, (hkhSc p y n').mpr hkp, hact⟩, + by rw [HS.rls]; exact h0.sorted.imp_of_mem fun {a b} ha hb h => by rw [hrt_eq a ha, hrt_eq b hb]; exact h, + fun u x p hx hsc => by + obtain ⟨x0, hx0, -, hsc0, -⟩ := hkhTcb u x hx + obtain ⟨y, n', h1, h2⟩ := h0.back u x0 p hx0 (hsc0.symm.trans hsc) + exact ⟨y, n', (hkhSc p y n').mpr h1, h2⟩, + fun u x p hx hsc hidle => by + obtain ⟨x0, hx0, -, hsc0, -⟩ := hkhTcb u x hx + exact h0.tcbScNotIdle u x0 p hx0 (hsc0.symm.trans hsc) hidle⟩ + have hRunQ' : RunQA σ' := by + have h0 := J.sr.run + refine ⟨fun d p t ht => ?_, fun t ht => ?_⟩ + · rw [HS.rq] at ht + obtain ⟨x0, hx0, hrun0⟩ := h0.1 d p t ht + rcases HS.kh t with e | ⟨y0, y1, h1, h2, hkf⟩ | ⟨_, _, h1, -⟩ + · exact ⟨x0, by rw [e]; exact hx0, hrun0⟩ + · rw [hx0] at h1; cases h1; exact ⟨y1, h2, by rw [hkf.state]; exact hrun0⟩ + · rw [hx0] at h1; cases h1 + · rw [HS.rls] at ht + obtain ⟨x0, hx0, hrun0⟩ := h0.2 t ht + rcases HS.kh t with e | ⟨y0, y1, h1, h2, hkf⟩ | ⟨_, _, h1, -⟩ + · exact ⟨x0, by rw [e]; exact hx0, hrun0⟩ + · rw [hx0] at h1; cases h1; exact ⟨y1, h2, by rw [hkf.state]; exact hrun0⟩ + · rw [hx0] at h1; cases h1 + have hRd' : RdA σ' := fun u x hx hin => by + have ⟨x0, hx0, hst, hsc, hflt⟩ : ∃ x0, σ.kheap u = some (.tcb x0) ∧ + x.tcbState = x0.tcbState ∧ x.tcbSchedContext = x0.tcbSchedContext ∧ x.tcbFault = x0.tcbFault := by + rcases HS.kh u with e | ⟨y0, y1, h1, h2, hkf⟩ | ⟨_, _, _, h2, -⟩ + · exact ⟨x, by rw [← e]; exact hx, rfl, rfl, rfl⟩ + · rw [hx] at h2; cases h2; exact ⟨y0, h1, hkf.state, hkf.sc, hkf.fault⟩ + · rw [hx] at h2; cases h2 + have hr0 := J.sr.rd u x0 hx0 (by rwa [← hst, ← hflt]) + intro x' scp y n' hx' hsc' hkp hact hnrel + rw [hx] at hx'; cases hx' + have hkp0 := HS.backObj hkp (fun _ e => by cases e) (fun _ e => by cases e) + have hrel0 := hr0 x0 scp y n' hx0 (hsc.symm.trans hsc') hkp0 hact (by rwa [← HS.rls]) + unfold releasedSc at hrel0 ⊢; rw [HS.curT]; exact hrel0 + have sr0 : SchedSRA st Z m κ s' σ' := + J.sr.rebuild (Q' := Z.Q) hk hc + (fun c hc' o ho h8 _ => nodK c hc' o (by omega) h8) + (fun K hK => ksK K (by rcases hK with rfl | rfl <;> decide) (by rcases hK with rfl | rfl <;> decide)) + (fun t ht K hK => tcbS t ht K (by rcases hK with rfl | rfl | rfl | rfl <;> decide) + (by rcases hK with rfl | rfl | rfl | rfl <;> decide) + (by rcases hK with rfl | rfl | rfl | rfl <;> decide)) + (fun a hal ha => by + obtain ⟨c, hc', z, hz, h1, h2⟩ := J.scFoot_heapPA ha + have hbits : H.scBits c = z := by unfold Heap.scBits; rw [hz] + rw [hbits] at h2 + exact w.mem_eq hal (heapObj_off_vcpuWrCoreA J hz (by intro e; cases e) (fun _ e => by cases e) h1 (by change a.val < c.val + 2 ^ z; exact h2))) + (fun c hc' => by + obtain ⟨x, k, hx, -⟩ := J.sr.scr.sc c hc' + rw [HS.keepObj hx (fun _ e => by cases e) (fun _ e => by cases e), hx]) + (fun t ht x hx => by + rcases HS.kh (specPtr t) with e | ⟨y0, y1, h1, h2, hkf⟩ | ⟨_, _, h1, -⟩ + · exact ⟨x, by rw [e]; exact hx, rfl, rfl, rfl⟩ + · rw [hx] at h1; cases h1; exact ⟨y1, h2, hkf.sc, hkf.dom, hkf.prio⟩ + · rw [hx] at h1; cases h1) + ⟨HS.cur, HS.csc, HS.idle, HS.curT, HS.consT, HS.reprog, HS.dom, HS.domT⟩ + (by rw [HS.act, nodK _ J.j.1.sch.cpuCs _ (by rw [act_val]; omega) (by rw [act_val])]; exact J.sr.sch.act) + (fun u hu => J.sr.actOn u (by rw [← HS.act]; exact hu)) + hRd' hScInv' hcore' + (fun p hp => by rw [HS.rq]; exact J.sr.rest.rq p hp) + (fun d p hd => by rw [HS.rq]; exact J.sr.rest.rqDom d p hd) + HS.rls hRunQ' + have sr' : SchedSRA st { Z with hw := hw' } m κ s' σ' := + { toSchedSRN := { sr0.toSchedSRN with rest := { sr0.rest with } }, rd := sr0.rd } + have vsRel' : VsRelA Z.Pp Z.S1 Z.Lv Z.X Z.Y (L.mem s') (L.ksb s') σ' := by + have h0 := J.j.1.vs + rw [hk] + have hvs_mem : VsRelA Z.Pp Z.S1 Z.Lv Z.X Z.Y (L.mem s') (L.ksb s) σ := + h0.of_mem (fun b hb => vsfoot b hb) + exact hvs_mem.of_spec HS.asid fun p hp => by + rcases hp with ⟨q, hq, rfl⟩ | ⟨q, hq, rfl⟩ | ⟨q, hq, rfl⟩ + · obtain ⟨f, hf, -⟩ := h0.pool q hq + rw [HS.keepObj hf (fun _ e => by cases e) (fun _ e => by cases e), hf] + · obtain ⟨t, ht, -⟩ := h0.pt q hq + rw [HS.keepObj ht (fun _ e => by cases e) (fun _ e => by cases e), ht] + · obtain ⟨l1, hl1, -⟩ := h0.high q hq + rw [HS.keepObj hl1 (fun _ e => by cases e) (fun _ e => by cases e), hl1] + have extRel' : ExtVsRelA Z.Pp Z.S1 Z.X Z.Y Z.T2 Z.Sh2 (L.mem s') σ' := by + have h0 := J.j.1.ext + have hext_mem : ExtVsRelA Z.Pp Z.S1 Z.X Z.Y Z.T2 Z.Sh2 (L.mem s') σ := + h0.of_mem (fun b hb => extfoot b hb) + exact hext_mem.of_spec HS.asid fun q hq => by + rcases hq with hq | hq | hq + · obtain ⟨f, hf, -⟩ := J.j.1.vs.pool q hq + rw [HS.keepObj hf (fun _ e => by cases e) (fun _ e => by cases e), hf] + · obtain ⟨es, hes, -⟩ := h0.s2root q hq + rw [HS.keepObj hes (fun _ e => by cases e) (fun _ e => by cases e), hes] + · obtain ⟨pt, hpt, -⟩ := h0.s2pt q hq + rw [HS.keepObj hpt (fun _ e => by cases e) (fun _ e => by cases e), hpt] + have hStEq' : StEqA Z.T (L.mem s') σ' := fun t ht x hx => by + have hst0 := Sched.SchedRelA.stEq J.j.1.sch t ht + rw [stA_eq, tcbS t ht _ (by decide) (by decide) (by decide), ← stA_eq] + rcases HS.kh (specPtr t) with e | ⟨y0, y1, h1, h2, hkf⟩ | ⟨_, _, _, h2, -⟩ + · exact hst0 x (by rw [← e]; exact hx) + · rw [hx] at h2; cases h2; rw [hkf.state]; exact hst0 y0 h1 + · rw [hx] at h2; cases h2 + have jAt' : JAtZA (L := L) AR { Z with hw := hw' } s' a' σ' m := + { st := stRel', cdt := cdt', sch := sr'.sch.withSt hStEq', irq := irq', rest := sr'.rest, ar := har, + place := by rw [hk]; exact J.j.1.place, thT := J.j.1.thT, csN := J.j.1.csN, + idleT := fun c hc' => by rw [nodeAt_of_ksb hk, nodK c hc' _ (by rw [idle8]; omega) (by rw [idle8])]; exact J.j.1.idleT c hc', + idleC := fun c hc' => by rw [nodeAt_of_ksb hk, nodK c hc' _ (by simp [layout.node.IDLE_SC]) (by simp [layout.node.IDLE_SC])]; exact J.j.1.idleC c hc', + curT := fun c hc' => by rw [nodeAt_of_ksb hk, nodK c hc' _ (by rw [curth0]; omega) (by rw [curth0])]; exact J.j.1.curT c hc', + cscC := fun c hc' => by rw [nodeAt_of_ksb hk, nodK c hc' _ (by simp [layout.node.CUR_SC]) (by simp [layout.node.CUR_SC])]; exact J.j.1.cscC c hc', + ksFit := by rw [hk]; exact J.j.1.ksFit, scOffD := J.j.1.scOffD, + cv := fun y hy => by + have hyOS : y ∈ OS := J.j5.2.1 y hy + unfold capAtA; rw [(osk y hyOS).1, (osk y hyOS).2.1]; exact J.j.1.cv y hy, + pml := fun y hy p mp asid h => by + have hyOS : y ∈ OS := J.j5.2.1 y hy + unfold capAtA at h; rw [(osk y hyOS).1, (osk y hyOS).2.1] at h; exact J.j.1.pml y hy p mp asid h, + vs := vsRel', ext := extRel' } + have s2Core' : Stage2CoreA (L := L) { Z with hw := hw' } s' σ' := + ⟨fun r hr => let ⟨es, hes⟩ := J.j.2.2.1.obj r hr; ⟨es, HS.keepObj hes (fun _ e => by cases e) (fun _ e => by cases e)⟩, + fun p es hp => J.j.2.2.1.back p es (HS.backObj hp (fun _ e => by cases e) (fun _ e => by cases e)), + fun y hy p l mp asid va hc => by + have hyOS : y ∈ OS := J.j5.2.1 y hy + unfold capAtA at hc; rw [(osk y hyOS).1, (osk y hyOS).2.1] at hc + exact J.j.2.2.1.cap y hy p l mp asid va hc⟩ + have hvCore' : HighVSpaceCoreA (L := L) { Z with hw := hw' } s' σ' := + ⟨fun r hr => let ⟨l1, hl1⟩ := J.j.2.2.2.obj r hr; ⟨l1, HS.keepObj hl1 (fun _ e => by cases e) (fun _ e => by cases e)⟩, + fun p l1 hp => J.j.2.2.2.back p l1 (HS.backObj hp (fun _ e => by cases e) (fun _ e => by cases e)), + fun y hy p mp asid hc => by + have hyOS : y ∈ OS := J.j5.2.1 y hy + unfold capAtA at hc; rw [(osk y hyOS).1, (osk y hyOS).2.1] at hc + exact J.j.2.2.2.cap y hy p mp asid hc⟩ + -- `EpStateRel`, `NtfnStateRel`, `ReplyStackRelA`, `BufCover`, `McpRelA`, `YieldRelA` + have ep' : EpStateRel Z.T E Rp Bf (L.mem s') σ' := by + refine ⟨fun e he => ?_, fun r hr => ?_, hEpTcb, hEpBuf⟩ + · obtain ⟨x, hx, hrel⟩ := J.ep.ep e he + have hepH : H e = some .ep := J.utIpc.ep e he + have hfit := J.ent.heap.fits e .ep hepH; have hal := J.ent.heap.align e .ep hepH + change e.val + 16 ≤ 2 ^ 64 at hfit; change e.val % 16 = 0 at hal + have h0 : L.mem s' e = L.mem s e := w.mem_eq (by show e.val % 8 = 0; omega) + (heapObj_off_vcpuWrCoreA J hepH (by intro h; cases h) (fun _ h => by cases h) (le_refl _) (by change e.val < e.val + 16; omega)) + have h8 : L.mem s' (wa e 8#u64) = L.mem s (wa e 8#u64) := by + have h8v : (8#u64).val = 8 := rfl + have ev : (wa e 8#u64).val = e.val + 8 := by rw [wa_valm, h8v]; exact Nat.mod_eq_of_lt (by omega) + exact w.mem_eq (by show (wa e 8#u64).val % 8 = 0; rw [ev]; omega) + (heapObj_off_vcpuWrCoreA J hepH (by intro h; cases h) (fun _ h => by cases h) (by omega) (by change (wa e 8#u64).val < e.val + 16; omega)) + refine ⟨x, HS.keepObj hx (fun _ h => by cases h) (fun _ h => by cases h), ?_⟩ + exact hrel.frame (by unfold epHeadA layout.ep.HEAD; rwa [wa_zeroVcA]) (by unfold epTSA layout.ep.TAIL_STATE; exact h8) fun y hy _ => + ⟨by rw [stA_eq]; exact tcbS y hy _ (by decide) (by decide) (by decide), + by unfold tBlockA; exact tcbS y hy _ (by decide) (by decide) (by decide), + ⟨by rw [prevA_eq']; exact tcbS y hy _ (by decide) (by decide) (by decide), + by rw [nextA_eq']; exact tcbS y hy _ (by decide) (by decide) (by decide)⟩⟩ + · obtain ⟨x, hx, hrt⟩ := J.ep.reply r hr + have hrH : H r = some .reply := J.utIpc.reply r hr + have hal := J.ent.heap.align r .reply hrH; change r.val % 32 = 0 at hal + have h0 : L.mem s' (Endp.rTcbA r) = L.mem s (Endp.rTcbA r) := by + unfold Endp.rTcbA layout.reply.TCB + rw [wa_zeroVcA] + exact w.mem_eq (by show r.val % 8 = 0; omega) + (heapObj_off_vcpuWrCoreA J hrH (by intro h; cases h) (fun _ h => by cases h) (le_refl _) (by change r.val < r.val + 32; omega)) + exact ⟨x, HS.keepObj hx (fun _ h => by cases h) (fun _ h => by cases h), by rw [h0]; exact hrt⟩ + have ntfn' : NtfnStateRel N Z.T (L.mem s') σ' := by + refine ⟨fun nu hnu => ?_, fun t ht => ?_⟩ + · obtain ⟨x, hx, hrel⟩ := J.ntfn.ntfn nu hnu + have hnH : H nu = some .ntfn := J.utIpc.ntfn nu hnu + have hfit := J.ent.heap.fits nu .ntfn hnH; have hal := J.ent.heap.align nu .ntfn hnH + change nu.val + 64 ≤ 2 ^ 64 at hfit; change nu.val % 64 = 0 at hal + have hW : ∀ o : U64, o.val < 64 → o.val % 8 = 0 → L.mem s' (wa nu o) = L.mem s (wa nu o) := fun o ho h8 => by + have ev : (wa nu o).val = nu.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact w.mem_eq (by show (wa nu o).val % 8 = 0; rw [ev]; omega) + (heapObj_off_vcpuWrCoreA J hnH (by intro h; cases h) (fun _ h => by cases h) (by omega) (by change (wa nu o).val < nu.val + 64; omega)) + refine ⟨x, HS.keepObj hx (fun _ h => by cases h) (fun _ h => by cases h), ?_⟩ + exact hrel.frame + ⟨by unfold epHeadA layout.ep.HEAD; exact hW 0#u64 (by decide) (by decide), + by unfold epTSA layout.ep.TAIL_STATE; exact hW 8#u64 (by decide) (by decide), + by unfold Notif.nBadgeA layout.ntfn.BADGE; exact hW 16#u64 (by decide) (by decide), + by unfold Notif.nBoundA layout.ntfn.BOUND_TCB; exact hW 24#u64 (by decide) (by decide), + by unfold Notif.nScA layout.ntfn.SC; exact hW 32#u64 (by decide) (by decide)⟩ + fun y hy _ => + ⟨by rw [stA_eq]; exact tcbS y hy _ (by decide) (by decide) (by decide), + by unfold tBlockA; exact tcbS y hy _ (by decide) (by decide) (by decide), + ⟨by rw [prevA_eq']; exact tcbS y hy _ (by decide) (by decide) (by decide), + by rw [nextA_eq']; exact tcbS y hy _ (by decide) (by decide) (by decide)⟩⟩ + · obtain ⟨x, hx, hrel⟩ := J.ntfn.tcb t ht + obtain ⟨xE, hxE, hrelE⟩ := hEpTcb t ht + have hst : L.mem s' (stA t) = L.mem s (stA t) := by rw [stA_eq]; exact tcbS t ht _ (by decide) (by decide) (by decide) + have hblk : L.mem s' (tBlockA t) = L.mem s (tBlockA t) := by unfold tBlockA; exact tcbS t ht _ (by decide) (by decide) (by decide) + have hprio : L.mem s' (prioA t) = L.mem s (prioA t) := by unfold prioA; exact tcbS t ht _ (by decide) (by decide) (by decide) + have hbdg : (L.mem s' (Notif.badgeRegA t)).bv = Notif.badgeOf xE := by + have hreg := hrelE.regs badge_register + unfold Notif.badgeRegA Notif.badgeOf + have h1 : wa (wa t layout.tcb.REGS) (core.num.U64.wrapping_mul layout.reg.BADGE 8#u64) = + Endp.regA t (Endp.regIdxA badge_register) := by + unfold Endp.regA Endp.regIdxA badge_register badgeRegister layout.tcb.REGS layout.reg.BADGE layout.reg.X0 + rfl + rw [h1]; exact hreg + rcases HS.kh (specPtr t) with e | ⟨y, y1, h1, h2, hkf⟩ | ⟨_, _, h1, -⟩ + · rw [e, hx] at hxE; cases hxE + exact ⟨x, by rw [e]; exact hx, ⟨hst ▸ hrel.st, fun n hn => hblk ▸ hrel.blk n hn, hbdg, hprio ▸ hrel.prio⟩⟩ + · rw [hx] at h1; cases h1; rw [h2] at hxE; cases hxE + exact ⟨xE, h2, ⟨hkf.state ▸ hst ▸ hrel.st, fun n hn => hblk ▸ hrel.blk n (hkf.state ▸ hn), + hbdg, hkf.prio ▸ hprio ▸ hrel.prio⟩⟩ + · rw [hx] at h1; cases h1 + have hRepW : ∀ r ∈ Rp, L.mem s' (rPrevA r) = L.mem s (rPrevA r) ∧ L.mem s' (rNextA r) = L.mem s (rNextA r) := fun r hr => by + have hrH : H r = some .reply := J.utIpc.reply r hr + have hfit := J.ent.heap.fits r .reply hrH; have hal := J.ent.heap.align r .reply hrH + change r.val + 32 ≤ 2 ^ 64 at hfit; change r.val % 32 = 0 at hal + have hR : ∀ o : U64, o.val < 32 → o.val % 8 = 0 → L.mem s' (wa r o) = L.mem s (wa r o) := fun o ho h8 => by + have ev : (wa r o).val = r.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact w.mem_eq (by show (wa r o).val % 8 = 0; rw [ev]; omega) + (heapObj_off_vcpuWrCoreA J hrH (by intro h; cases h) (fun _ h => by cases h) (by omega) (by change (wa r o).val < r.val + 32; omega)) + exact ⟨by unfold rPrevA layout.reply.PREV; exact hR 8#u64 (by decide) (by decide), + by unfold rNextA layout.reply.NEXT; exact hR 16#u64 (by decide) (by decide)⟩ + have reply' : ReplyStackRelA (L.mem s') Rp Z.C σ' (scNameA m) stack := by + refine ⟨fun c hc' => ?_, J.reply.unique, + fun r hr x hx => J.reply.owner r hr x (HS.backObj hx (fun _ h => by cases h) (fun _ h => by cases h)), + fun r hr hdet => ?_⟩ + · obtain ⟨⟨y, k, hy, eq⟩, hmem, hnd, hw⟩ := J.reply.stackRel c hc' + refine ⟨⟨y, k, HS.keepObj hy (fun _ h => by cases h) (fun _ h => by cases h), eq⟩, hmem, hnd, + fun i hi => ?_⟩ + have hri : (stack c)[i] ∈ Rp := hmem _ (List.getElem_mem hi) + rw [(hRepW _ hri).1, (hRepW _ hri).2]; exact hw i hi + · obtain ⟨hb, hn, hp⟩ := J.reply.detached r hr hdet + rw [(hRepW r hr).1, (hRepW r hr).2] + refine ⟨hb, fun hne => ?_, fun hne => ?_⟩ + · obtain ⟨h1, h2, h3⟩ := hn hne + exact ⟨h1, h2, by rw [(hRepW _ h1).1]; exact h3⟩ + · obtain ⟨h1, h2, h3⟩ := hp hne + exact ⟨h1, h2, by rw [(hRepW _ h1).2]; exact h3⟩ + have bufCover' : BufCover st Z.T Bf s' σ' := by + intro t ht x' b sb w0 hx' hib hlu + have ⟨x, hx, hbuf, hbf⟩ : ∃ x, σ.kheap (specPtr t) = some (.tcb x) ∧ + x'.tcbIpcBuffer = x.tcbIpcBuffer ∧ x'.tcbIpcframe = x.tcbIpcframe := by + rcases HS.kh (specPtr t) with e | ⟨y, y', h1, h2, hkf⟩ | ⟨_, _, _, h2, -⟩ + · exact ⟨x', by rw [← e]; exact hx', rfl, rfl⟩ + · rw [hx'] at h2; cases h2; exact ⟨y, h1, hkf.ibuf, hkf.ipcfr⟩ + · rw [hx'] at h2; cases h2 + have hib0 : ipcBufOfA b x = some sb := by + unfold ipcBufOfA at hib ⊢; rw [← hbuf, ← hbf]; exact hib + have hsl : sbWordA t ∈ OS := by + have h1 : sbWordA t = slotAddrA (specPtr t) (BitVec.ofNat 64 2) := by + rw [slotAddrA_sp] + unfold sbWordA layout.tcb.SLOT_BUFFER layout.cte.SIZE + apply UScalar.eq_of_val_eq + rw [wa_valm] + have h64 : (core.num.U64.wrapping_mul 2#u64 32#u64).val = 64 := by + unfold core.num.U64.wrapping_mul UScalar.wrapping_mul; simp [UScalar.val] + have hb : ((BitVec.ofNat 64 2) * 32#64).toNat = 64 := by + rw [BitVec.toNat_mul]; simp + change (t.val + (core.num.U64.wrapping_mul 2#u64 32#u64).val) % 2 ^ 64 = + (t.bv + (BitVec.ofNat 64 2) * 32#64).toNat + rw [h64, BitVec.toNat_add, hb]; rfl + rw [h1] + exact J.j5.2.1 _ ⟨t, Or.inr ⟨(J.sr.thT t).2 ht, 2, by unfold tcbSlotsA; omega, rfl⟩⟩ + have hlu0 : msg.lookup_ipc_buffer st s b t = ok w0 := by + unfold msg.lookup_ipc_buffer at hlu ⊢ + have hcap : obj.Slot.cap st (wa t (core.num.U64.wrapping_mul 2#u64 layout.cte.SIZE)) s' = + obj.Slot.cap st (wa t (core.num.U64.wrapping_mul 2#u64 layout.cte.SIZE)) s := by + have hsb : wa t (core.num.U64.wrapping_mul 2#u64 layout.cte.SIZE) = sbWordA t := by + unfold sbWordA layout.tcb.SLOT_BUFFER layout.cte.SIZE; rfl + rw [hsb] + unfold obj.Slot.cap + rw [L.read_pair_eq, L.read_pair_eq, (osk _ hsl).1, (osk _ hsl).2.1] + have hipc : L.mem s' (wa t 3512#u64) = L.mem s (wa t 3512#u64) := + tcbS t ht 3512#u64 (by decide) (by decide) (by decide) + have h184 : layout.tcb.F + 184#u64 = ok 3512#u64 := by + unfold layout.tcb.F; rfl + simpa only [obj.Tcb.slot, layout.tcb.SLOT_BUFFER, obj.Tcb.ipc_buffer, layout.tcb.IPC_BUFFER, + lift, bind_ok, L.read_eq, h184, hipc, hcap] using hlu + exact J.bufCover t ht x b sb w0 hx hib0 hlu0 + -- `SInvXA`, `SIdleA`, `SActA`, `IdleNoFaultA`, `McpRelA`, `YieldRelA`, `BlkQueuedA`, `RBlk` + have sinv' : SInvXA σ' m n := by + have h0 := J.sinv + have hIrqN' : SpecIrqA σ' := + ⟨⟨⟨fun i j hi hj h => h0.irqN.node.1.1 i j hi hj (by rw [← HS.irqN]; exact h), + fun irq hi => by + rw [HS.irqN] + obtain ⟨cs, hcs, hwf⟩ := h0.irqN.node.1.2 irq hi + exact ⟨cs, HS.keepObj hcs (fun _ e => by cases e) (fun _ e => by cases e), hwf⟩⟩, + fun irq hi nc ⟨cs, hcs, hwf⟩ => by + rw [HS.irqN] at hcs + exact h0.irqN.node.2 irq hi nc ⟨cs, HS.backObj hcs (fun _ e => by cases e) (fun _ e => by cases e), hwf⟩⟩, + fun irq hi o b a ho hcap => by + rw [HS.irqN] at ho + rcases HS.kh (σ.interruptIrqNode irq) with e | ⟨y, y1, h1, h2, hkf⟩ | ⟨_, _, _, h2, -⟩ + · exact h0.irqN.slot irq hi o b a (by rw [← e]; exact ho) hcap + · rw [ho] at h2; cases h2 + have hc0 : capOf (.tcb y) b = some a := by + unfold capOf tcbCnodeMap at hcap ⊢; dsimp only at hcap ⊢ + rw [← hkf.ctable, ← hkf.vtable, ← hkf.ipcfr, ← hkf.fh, ← hkf.th, ← hkf.s2, ← hkf.hv] + exact hcap + exact h0.irqN.slot irq hi (.tcb y) b a h1 hc0 + · rw [ho] at h2; cases h2; simp [capOf] at hcap, + fun p o b a ho hcap => by + rw [HS.irqN] + rcases HS.kh p with e | ⟨y, y1, h1, h2, hkf⟩ | ⟨_, _, _, h2, -⟩ + · exact h0.irqN.ref p o b a (by rw [← e]; exact ho) hcap + · rw [ho] at h2; cases h2 + have hc0 : capOf (.tcb y) b = some a := by + unfold capOf tcbCnodeMap at hcap ⊢; dsimp only at hcap ⊢ + rw [← hkf.ctable, ← hkf.vtable, ← hkf.ipcfr, ← hkf.fh, ← hkf.th, ← hkf.s2, ← hkf.hv] + exact hcap + exact h0.irqN.ref p (.tcb y) b a h1 hc0 + · rw [ho] at h2; cases h2; simp [capOf] at hcap⟩ + refine ⟨h0.mc, by unfold Fl4SpecA64.McView; rw [HS.idle]; exact h0.view, + ?_, ?_, ?_, ?_, ?_, + by rw [HS.cur, HS.rq, HS.act]; exact h0.ctQ, by rw [HS.rq]; exact h0.idleQ, + ?_, ?_, ?_, hIrqN', (HS.curFacts ⟨h0.zc, h0.rlsRun, h0.curScReady⟩).1, + (HS.curFacts ⟨h0.zc, h0.rlsRun, h0.curScReady⟩).2.1, (HS.curFacts ⟨h0.zc, h0.rlsRun, h0.curScReady⟩).2.2, + fun p y hy hf => by + rcases HS.kh p with e | ⟨x, x', h1, h2, hkf⟩ | ⟨v, v', _, h2, -⟩ + · rw [e] at hy; exact h0.faultSt p y hy hf + · rw [hy] at h2; cases h2 + have := h0.faultSt p x h1 (fun e => hf (by rw [hkf.fault]; exact e)) + rw [hkf.state]; exact this + · rw [hy] at h2; cases h2⟩ + · obtain ⟨x, hx⟩ := h0.curTcb + rw [HS.cur] + rcases HS.kh σ.curThread with e | ⟨y, y', h1, h2, -⟩ | ⟨_, _, h1, -⟩ + · exact ⟨x, by rw [e]; exact hx⟩ + · rw [hx] at h1; cases h1; exact ⟨y', h2⟩ + · rw [hx] at h1; cases h1 + · obtain ⟨x, hx, hs⟩ := h0.idleTcb + rw [HS.idle] + rcases HS.kh σ.idleThread with e | ⟨y, y', h1, h2, hkf⟩ | ⟨_, _, h1, -⟩ + · exact ⟨x, by rw [e]; exact hx, hs⟩ + · rw [hx] at h1; cases h1; exact ⟨y', h2, hkf.state.trans hs⟩ + · rw [hx] at h1; cases h1 + · intro t x' hx' hst + rcases HS.kh t with e | ⟨y, y', h1, h2, hkf⟩ | ⟨_, _, _, h2, -⟩ + · exact h0.onlyIdle t x' (by rw [← e]; exact hx') hst + · rw [hx'] at h2; cases h2; exact h0.onlyIdle t y h1 (hkf.state.symm.trans hst) + · rw [hx'] at h2; cases h2 + · obtain ⟨x, y, k, hx, hsc, hksc, hst⟩ := h0.idleSc + rw [HS.idle] + rcases HS.kh σ.idleThread with e | ⟨z, z', h1, h2, hkf⟩ | ⟨_, _, h1, -⟩ + · exact ⟨x, y, k, by rw [e]; exact hx, hsc, HS.keepObj hksc (fun _ h => by cases h) (fun _ h => by cases h), hst⟩ + · rw [hx] at h1; cases h1 + exact ⟨z', y, k, h2, hkf.sc.trans hsc, HS.keepObj hksc (fun _ h => by cases h) (fun _ h => by cases h), hst⟩ + · rw [hx] at h1; cases h1 + · obtain ⟨y, k, hy⟩ := h0.curSc + rw [HS.csc]; exact ⟨y, k, HS.keepObj hy (fun _ h => by cases h) (fun _ h => by cases h)⟩ + · intro d p t hm; rw [HS.rq] at hm + obtain ⟨x, hx, hr⟩ := h0.rq d p t hm + rcases HS.kh t with e | ⟨y, y', h1, h2, hkf⟩ | ⟨_, _, h1, -⟩ + · exact ⟨x, by rw [e]; exact hx, hr⟩ + · rw [hx] at h1; cases h1; exact ⟨y', h2, by rw [hkf.state]; exact hr⟩ + · rw [hx] at h1; cases h1 + · intro t hm; rw [HS.rls] at hm + obtain ⟨x, hx⟩ := h0.rls t hm + rcases HS.kh t with e | ⟨y, y', h1, h2, -⟩ | ⟨_, _, h1, -⟩ + · exact ⟨x, by rw [e]; exact hx⟩ + · rw [hx] at h1; cases h1; exact ⟨y', h2⟩ + · rw [hx] at h1; cases h1 + · intro p y k hy ha + exact h0.rpos p y k (HS.backObj hy (fun _ h => by cases h) (fun _ h => by cases h)) ha + have sidle' : SIdleA σ' m n := by + have idleVt : ∀ c < n, vtCapA σ' (m.idleThreadOf c) = some .nullCap ∧ vcpuOfA σ' (m.idleThreadOf c) = none := by + intro c hc' + obtain ⟨x, hx, -⟩ := J.sidle.f3 c hc' + have a := J.sidle.idleSlot c hc' + have b := J.sidle.idleBound c hc' + rcases HS.kh (m.idleThreadOf c) with e | ⟨y, y', h1, h2, hkf⟩ | ⟨_, _, h1, -⟩ + · exact idleVtSameA e a b + · obtain ⟨xi, hxi, hst_i⟩ := J.sidle.f3 c hc' + rw [h1] at hxi; cases hxi + rw [vtCapA_tcbI h1] at a; rw [vcpuOfA_tcbI h1] at b + rw [vtCapA_tcbI h2, vcpuOfA_tcbI h2, hkf.vtable] + exact ⟨a, hkf.vcpuNone hst_i b⟩ + · rw [hx] at h1; cases h1 + refine ⟨fun c hc' => ?_, fun y k hy => J.sidle.f4 y k (HS.backObj hy (fun _ h => by cases h) (fun _ h => by cases h)), + fun p y k hy hsc => J.sidle.f5 p y k (HS.backObj hy (fun _ h => by cases h) (fun _ h => by cases h)) (by rw [← HS.idle]; exact hsc), + J.sidle.f6.of_eq HS.specCapAt, + fun y k hy => by + rw [HS.curT]; exact J.sidle.f7 y k (HS.backObj hy (fun _ h => by cases h) (fun _ h => by cases h)), + fun c hc' => (idleVt c hc').1, fun c hc' => (idleVt c hc').2⟩ + obtain ⟨x, hx, hs⟩ := J.sidle.f3 c hc' + rcases HS.kh (m.idleThreadOf c) with e | ⟨y, y', h1, h2, hkf⟩ | ⟨_, _, h1, -⟩ + · exact ⟨x, by rw [e]; exact hx, hs⟩ + · rw [hx] at h1; cases h1; exact ⟨y', h2, hkf.state.trans hs⟩ + · rw [hx] at h1; cases h1 + have free : ∀ dev x, UtFreeA st OS s' dev x → UtFreeA st OS s dev x := by + rintro dev x ⟨u, hu, q, b, fi, d, hcap, h⟩ + exact ⟨u, hu, q, b, fi, d, (capE u hu).symm.trans hcap, h⟩ + have dead : ∀ k p, DeadA st OS k s' p → DeadA st OS k s p := fun k p h u hu hp => by + have := h u hu; rw [capE u hu] at this; exact this hp + have specZero' : SpecZeroFreeA st OS s' σ' := by + intro a ha + have hf0 := free _ _ ha + have hzero := J.specZero a hf0 + by_cases heq : σ'.machineState.underlyingMemory a = σ.machineState.underlyingMemory a + · rw [heq]; exact hzero + · obtain ⟨p, bits, hp, h1, h2⟩ := HS.umOff a heq + obtain ⟨u, hu, q, b, fi, d, hcap, -, f1, f2⟩ := hf0 + have hO := J.objAlloc p (2 ^ bits) (Or.inr (Or.inr (Or.inr ⟨.other bits, hp, rfl⟩))) u hu q b fi d hcap + omega + have keepE : ∀ {q : ObjRef} {e : ExtArchObj PTE}, + σ.kheap q = some (.archObj (.ext e)) → σ'.kheap q = some (.archObj (.ext e)) := fun h1 => + HS.keepObj h1 (fun _ h => by cases h) (fun _ h => by cases h) + have unmS2 : ∀ u ∈ OS, ∀ p a v, capA st s u = .Ept p 0#u64 false a v → UnmS2RootA st s σ p → + UnmS2RootA st s' σ' p := by + intro u hu p a v hc U + obtain ⟨es, h1, h2⟩ := U.spec + refine { inv := fun i hi => ?_, spec := ⟨es, keepE h1, h2⟩ } + by_cases hlo : i.val < 512 + · rw [word p (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ⟨u, hu, Or.inr (Or.inr (Or.inl ⟨a, v, hc⟩))⟩))))))) i hlo] + exact U.inv i hi + · obtain ⟨j, hj, e⟩ := peAF_hiA p i (by omega) hi + rw [e, word _ (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ⟨u, hu, Or.inr (Or.inr (Or.inr (Or.inl ⟨p, a, v, hc, rfl⟩)))⟩))))))) j hj, ← e] + exact U.inv i hi + have unmHv : ∀ u ∈ OS, ∀ y a, capA st s u = .HighVSpace y false a → UnmHiRootA st s σ y → + UnmHiRootA st s' σ' y := by + intro u hu y a hc U + obtain ⟨l1, h1, h2⟩ := U.spec + exact { inv := fun i hi => by + rw [word _ (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ⟨u, hu, Or.inr (Or.inr (Or.inr (Or.inr ⟨y, a, hc, rfl⟩)))⟩))))))) i hi] + exact U.inv i hi + spec := ⟨l1, keepE h1, h2⟩ } + have hFootHw : ∀ (Z0 : XSetsA) (q : U64), FootA (L := L) { Z0 with hw := hw' } s' q → FootA (L := L) Z0 s q := by + intro Z0 q h + have hn := fun c o => nodeAt_ksb (L := L) hk c o + cases h with + | slot y _ hy hw => exact .slot y _ hy hw + | nslot _ h => + refine .nslot _ ?_ + cases h with + | node c _ hcm ho => simp only [hn] at ho; exact .node c _ hcm ho + | cur _ ho => simp only [hc, hn] at ho; exact .cur _ ho + | tcb t _ ht ho => exact .tcb t _ ht ho + | sc c hcm => exact .sc c hcm + | vcpu v _ hv hw => exact .vcpu v _ hv hw + | irq i hi => rw [hk]; exact .irq i hi + | cur2 _ ho => simp only [hc, hn] at ho; exact .cur2 _ ho + | glob _ ho => rw [hk] at ho; exact .glob _ ho + | tcb2 t _ ht ho => exact .tcb2 t _ ht ho + | sc2 c hcm => exact .sc2 c hcm + | vs _ hb => rw [hk] at hb; exact .vs _ hb + | ext _ hb => exact .ext _ hb + have unm : ∀ ty p, SchedPageA st Z OS s p → UnmTabA st Z m s σ ty p → + UnmTabA st { Z with hw := hw' } m s' σ' ty p := by + intro ty p hp U1 + obtain ⟨tb, h1, h2, h3⟩ := U1.spec + exact { inv := fun i hi => by rw [word p hp i hi]; exact U1.inv i hi + spec := ⟨tb, HS.keepObj h1 (fun _ h => by cases h) (fun _ h => by cases h), h2, h3⟩ + off := ⟨fun i hi h => U1.off.mem i hi (hFootHw { Z with S1 := ∅ } (peAF p i) h), U1.off.obj⟩ } + exact + { ent := ent' + j := ⟨jAt', hVcCore, s2Core', hvCore'⟩ + tcbs := J.tcbs, scs := J.scs, sq := sq' + ap := by rw [hk]; exact { J.ap with } + pp := J.pp, s1 := J.s1, ko := by rw [hk]; exact J.ko, ptH := J.ptH + tabInj := by rw [HS.asid]; exact J.tabInj + poolOff := poolOff_keepTableA (Z := Z) J.poolOff hk fun i => + let hv := AtOff.v i; ksK (AtOff i) (by have := hv.2.1; omega) hv.2.2 + objAlloc := ObjAllocA.keep (Z := Z) J.objAlloc osk + idleAnchor := IdleAnchorOffA.keep J.idleAnchor osk + vsVc := by rw [hk]; exact J.vsVc + sr := sr' + scCov := fun p x k hp => J.scCov p x k (HS.backObj hp (fun _ h => by cases h) (fun _ h => by cases h)) + scSize := fun c hc' x k hx => J.scSize c hc' x k (HS.backObj hx (fun _ h => by cases h) (fun _ h => by cases h)) + ep := ep', ntfn := ntfn', reply := reply', bufCover := bufCover' + bufNative := fun t ht => by + have hsl : slotAddrA (specPtr t) (BitVec.ofNat 64 2) ∈ OS := + J.j5.2.1 _ ⟨t, Or.inr ⟨(J.sr.thT t).2 ht, 2, by unfold tcbSlotsA; omega, rfl⟩⟩ + have e : capAtA (L.mem s') (slotAddrA (specPtr t) (BitVec.ofNat 64 2)) = + capAtA (L.mem s) (slotAddrA (specPtr t) (BitVec.ofNat 64 2)) := by + unfold capAtA; rw [(osk _ hsl).1, (osk _ hsl).2.1] + rw [e]; exact J.bufNative t ht + iplaced := J.iplaced, ipcPlace := J.ipcPlace + idleNoFault := fun c hc' => by + obtain ⟨x, hx, hf⟩ := J.idleNoFault c hc' + rcases HS.kh (m.idleThreadOf c) with e | ⟨y, y1, h1, h2, hkf⟩ | ⟨_, _, h1, -⟩ + · exact ⟨x, by rw [e]; exact hx, hf⟩ + · rw [hx] at h1; cases h1; exact ⟨y1, h2, hkf.fault.trans hf⟩ + · rw [hx] at h1; cases h1 + mcp := J.mcp.congrA (fun t ht => ⟨by unfold mcpA; exact tcbS t ht _ (by decide) (by decide) (by decide), + by unfold flagsA; exact tcbS t ht _ (by decide) (by decide) (by decide)⟩) (fun t ht x hx => by + rcases HS.kh (specPtr t) with e | ⟨y, y1, h1, h2, hkf⟩ | ⟨_, _, h1, -⟩ + · exact ⟨x, by rw [e]; exact hx, rfl, rfl⟩ + · rw [hx] at h1; cases h1; exact ⟨y1, h2, hkf.mcp, hkf.flags⟩ + · rw [hx] at h1; cases h1) + yieldRel := by + have hy : ∀ t ∈ Z.T, L.mem s' (yieldA t) = L.mem s (yieldA t) := fun t ht => by + unfold yieldA; exact tcbS t ht _ (by decide) (by decide) (by decide) + refine ⟨fun t ht => ?_, fun t ht hne => ?_, fun c hc' y k hyk p hp => ?_⟩ + · obtain ⟨x, hx, heq⟩ := J.yieldRel.tcb t ht + rw [hy t ht] + rcases HS.kh (specPtr t) with e | ⟨y0, y1, h1, h2, hkf⟩ | ⟨_, _, h1, -⟩ + · exact ⟨x, by rw [e]; exact hx, heq⟩ + · rw [hx] at h1; cases h1; exact ⟨y1, h2, hkf.yield.trans heq⟩ + · rw [hx] at h1; cases h1 + · rw [hy t ht] at hne ⊢ + obtain ⟨hc', y, k, hyk, hfr⟩ := J.yieldRel.toSc t ht hne + exact ⟨hc', y, k, HS.keepObj hyk (fun _ h => by cases h) (fun _ h => by cases h), hfr⟩ + · obtain ⟨t, ht, hsp, hyt⟩ := J.yieldRel.frm c hc' y k (HS.backObj hyk (fun _ h => by cases h) (fun _ h => by cases h)) p hp + exact ⟨t, ht, hsp, (hy t ht).trans hyt⟩ + sinv := sinv', setsInH := { J.setsInH with }, utIpc := J.utIpc, ipcTh := J.ipcTh + deadExt := + { vcpu := hDeadVcpu + reply := fun r hr hd => J.deadExt.reply r hr (dead _ r hd) + asid := fun i p h hd => J.deadExt.asid i p (by rw [HS.asid] at h; exact h) (dead _ p hd) + tvcpu := hDeadTvcpu + vcDeadRun := hDeadVcRun + replyTcb := fun r hr hd x hx => by + obtain ⟨y, hy, k⟩ := (QKeepA.of_vcpuStepA HS).rt _ x hx + exact k (J.deadExt.replyTcb r hr (dead _ r hd) y hy) } + specFree := + { heap := fun p h => HS.none_keep (J.specFree.heap p (free _ _ h)) + cdt := fun x y h p hf => J.specFree.cdt x y (by rw [HS.cdt] at h; exact h) p (free _ _ hf) + orig := fun x h p hf => J.specFree.orig x (by rw [HS.orig] at h; exact h) p (free _ _ hf) } + utBlk := fun u hu q b fi d h => J.utBlk u hu q b fi d ((capE u hu).symm.trans h) + unmPt := + { pt := fun u hu p asid va h => by + have h0 := (capE u hu).symm.trans h + exact unm _ p (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ⟨u, hu, Or.inl ⟨_, _, _, h0⟩⟩))))))) (J.unmPt.pt u hu p asid va h0) + vs := fun u hu p asid h => by + have h0 := (capE u hu).symm.trans h + exact unm _ p (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ⟨u, hu, Or.inr (Or.inl ⟨_, _, h0⟩)⟩))))))) (J.unmPt.vs u hu p asid h0) + s2 := fun u hu p asid va h => by + have h0 := (capE u hu).symm.trans h; exact unmS2 u hu p asid va h0 (J.unmPt.s2 u hu p asid va h0) + hv := fun u hu p asid h => by + have h0 := (capE u hu).symm.trans h; exact unmHv u hu p asid h0 (J.unmPt.hv u hu p asid h0) } + tabS1 := fun q hq => ⟨fun i hi h => (J.tabS1 q hq).mem i hi (hFootHw { Z with S1 := ∅ } (peAF q i) h), (J.tabS1 q hq).obj⟩ + tabExt := ⟨fun q hq i hi h => J.tabExt.mem q hq i hi (hFootHw (extZA Z) (peAF q i) h), J.tabExt.obj⟩ + specZero := specZero' + origLive := fun x h => J.origLive x (by rw [HS.orig] at h; exact h) + nullOrig := fun sl h => by rw [HS.orig]; exact J.nullOrig sl (by rw [← HS.specCapAt sl]; exact h) + thPos := J.thPos + frBase := J.frBase.keepA capE + devFr := J.devFr.keepA capE fun p dev pg h => HS.keepObj h (fun _ e => by cases e) (fun _ e => by cases e) + finCov := J.finCov.keepA + (fun q r cs h => HS.backObj h (fun _ e => by cases e) (fun _ e => by cases e)) + (fun q x' h => by + rcases HS.kh q with e | ⟨y, _, h1, h2, -⟩ | ⟨_, _, _, h2, -⟩ + · exact ⟨x', by rw [← e]; exact h⟩ + · rw [h] at h2; cases h2; exact ⟨y, h1⟩ + · rw [h] at h2; cases h2) + idleAll := J.idleAll.keepA + (fun c hc' => by rw [nodeAt_of_ksb hk, nodK c hc' _ (by rw [idle8]; omega) (by rw [idle8])]) + (fun c hc' => by rw [nodeAt_of_ksb hk, nodK c hc' _ (by simp [layout.node.IDLE_SC]) (by simp [layout.node.IDLE_SC])]) + ipcHeapCov := J.ipcHeapCov + zombieNoVcpu := hZombNoVc + zombieTcb := J.zombieTcb.keepA capE + bufOff := J.bufOff.keepA (Z := Z) (Z' := { Z with hw := hw' }) hk rfl rfl rfl rfl rfl rfl rfl capE + vsOk := vsOk' + ipcSorted := J.ipcSorted.of_q (QKeepA.of_vcpuStepA HS) + activeReplyScs := J.activeReplyScs.of_q (QKeepA.of_vcpuStepA HS) + domOK := J.domOK.keepS J.sr.toSchedSRN HS.domk hk hc (fun b hal hd => by + obtain ⟨o, ho, eo⟩ := hd.wa_exists J.sr.ksFit + have h8 : o.val % 8 = 0 := by + have hka := J.j.1.ksbAl + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.ent.ksw + have ev : (wa (L.ksb s) o).val = (L.ksb s).val + o.val := by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have hb : b.val % 8 = 0 := hal + rw [← eo, ev] at hb; omega + rw [← eo]; exact ksK o (by omega) h8) + ipiZero := J.ipiZero.keepW hk hc fun o ho => by + obtain ⟨hv, hcl⟩ := J.sr.toSchedSRN.ipi_val ho + have hks : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.ent.ksw + exact w.mem_eq (nodeAt_aligned (J.sr.core.nodes _ J.sr.sch.cpuCs) (by omega) (by omega)) + (ks_off_vcpuWrCoreA J (by omega) (by omega)) + bufVs := by rw [hk, utVsEq]; exact J.bufVs + bufAl := fun t ht y hy => by + rcases HS.kh (specPtr t) with e | ⟨x0, x1, h1, h2, hkf⟩ | ⟨_, _, _, h2, -⟩ + · exact J.bufAl t ht y (by rw [← e]; exact hy) + · rw [hy] at h2; cases h2; rw [hkf.ibuf]; exact J.bufAl t ht x0 h1 + · rw [hy] at h2; cases h2 + zombieBnd := J.zombieBnd.keepA capE + devUtOff := J.devUtOff.keepA (Z := Z) capE hk (fun _ _ h => h) rfl + sidle := sidle' + capTy := J.capTy.keepA capE + scCpu := J.scCpu, scNZ := J.scNZ + vcRun := hVcRun, runVcpu := hRunVcpu + vr := J.vr_keepA + (fun r hr => by + have hrH : H r = some .reply := J.utIpc.reply r hr + have hal := J.ent.heap.align r .reply hrH; change r.val % 32 = 0 at hal + unfold P1.rTcbA layout.reply.TCB; rw [wa_zeroVcA] + exact w.mem_eq (by show r.val % 8 = 0; omega) + (heapObj_off_vcpuWrCoreA J hrH (by intro h; cases h) (fun _ h => by cases h) (le_refl _) (by change r.val < r.val + 32; omega))) + (fun t ht => by rw [stA_eq]; exact tcbS t ht _ (by decide) (by decide) (by decide)) + blkQ := J.blkQ.keepA (fun t ht => tcbS t ht 3336#u64 (by decide) (by decide) (by decide)) + (fun t ht x' hx' => by + rcases HS.kh (specPtr t) with e | ⟨x0, x1, h1, h2, hkf⟩ | ⟨_, _, _, h2, -⟩ + · exact Or.inr ⟨x', by rw [← e]; exact hx', rfl⟩ + · rw [hx'] at h2; cases h2; exact Or.inr ⟨x0, h1, hkf.state.symm⟩ + · rw [hx'] at h2; cases h2) + (fun p y h => HS.keepObj h (fun _ e => by cases e) (fun _ e => by cases e)) + (fun p y h => HS.keepObj h (fun _ e => by cases e) (fun _ e => by cases e)) + ciSc := fun u hu p b h => J.ciSc u hu p b ((capE u hu).symm.trans h) + noSwitch := fun u => by rw [HS.act]; exact J.noSwitch u + curVcpuNone := HS.vcur.trans J.curVcpuNone + j5 := j5', j7 := j7' } + +/-- `SActA` is preserved across any `VcpuSpecStepA` step. -/ +theorem _root_.Fl4ProofsA64.P3.SActA.of_vcpuStepA {H : Heap} {σ σ' : Fl4SpecA64.State Unit} {regWr : Bool} + (hsact : SActA σ) (HS : VcpuSpecStepA H σ σ' regWr) : SActA σ' := by + refine ⟨fun u hu => by rw [HS.act] at hu; rw [HS.cur, HS.idle]; exact hsact.f1 u hu, ?_⟩ + obtain ⟨x, hx, hs⟩ := hsact.f2 + rw [HS.cur] + rcases HS.kh σ.curThread with e | ⟨y, y', h1, h2, hkf⟩ | ⟨_, _, h1, -⟩ + · exact ⟨x, by rw [e]; exact hx, hs⟩ + · rw [hx] at h1; cases h1; exact ⟨y', h2, by rw [hkf.state]; exact hs⟩ + · rw [hx] at h1; cases h1 + +/-- **Master `JStage3TA` transport across any `VcpuWrFootA` + `VcpuSpecStepA` step** +(wrapper over `JStage3VcpuCoreA.of_vcpuStepA`). -/ +theorem JStage3TA.of_vcpuStepA + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + (R6 : VcpuRec6A st Z OS Bf s σ) + {hw' : HwRootA} {regWr : Bool} + (w : WritesIn st s s' (VcpuWrFootA st regWr Z OS s)) + (HS : VcpuSpecStepA H σ σ' regWr) + (har : AR a' hw') + (hUtVs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → L.mem s' b = L.mem s b) + (hVcCore : VcpuCoreA (L := L) { Z with hw := hw' } s' σ') + (hVcRun : VcRunA st Z.T Z.V s') + (hRunVcpu : RunVcpuInv st Z.T s') + (hEpTcb : ∀ t ∈ Z.T, ∃ x, σ'.kheap (specPtr t) = some (.tcb x) ∧ TcbRelE Rp (L.mem s') t x) + (hEpBuf : ∀ bp ∈ Bf, ∀ k : U64, k.val < 128 → + (L.mem s' (bufW bp.1 k)).bv = loadWordAt σ'.machineState.underlyingMemory (bufAddr bp.2 k.val) ∧ + (bufAddr bp.2 k.val).toNat % 8 = 0) + (hDeadVcpu : ∀ v ∈ Z.V, DeadA st OS .vcpu s' v → ∀ x, σ'.kheap (specPtr v) = some (.archObj (.vcpu x)) → + x.vcpuTcb = none) + (hDeadTvcpu : ∀ t ∈ Z.T, DeadA st OS .tcb s' t → L.mem s' (wa t 3664#u64) = 0#u64) + (hDeadVcRun : ∀ v ∈ Z.V, DeadA st OS .vcpu s' v → L.mem s' (wa v 24#u64) = 0#u64) + (hZombNoVc : ZombieTcbNoVcpuA st OS s') : + JStage3TA st AR { Z with hw := hw' } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s' a' σ' m := + (JStage3VcpuCoreA.of_vcpuStepA J.toVcpuCoreA R6 w HS har hUtVs hVcCore hVcRun hRunVcpu + hEpTcb hEpBuf hDeadVcpu hDeadTvcpu hDeadVcRun hZombNoVc).toTA (J.sact.of_vcpuStepA HS) + +/-- **Master `JStage3A` transport across any `VcpuWrFootA` + `VcpuSpecStepA` step** +(requires NO `SActA σ`, so valid when `curThread` is in `.ext .runningVM`). -/ +theorem _root_.Fl4ProofsA64.P3.JStage3A.of_vcpuStepA + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + (hNoSw : ∀ u, σ.schedulerAction ≠ .switchThread u) + (R6 : VcpuRec6A st Z OS Bf s σ) + {hw' : HwRootA} {regWr : Bool} + (w : WritesIn st s s' (VcpuWrFootA st regWr Z OS s)) + (HS : VcpuSpecStepA H σ σ' regWr) + (har : AR a' hw') + (hUtVs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → L.mem s' b = L.mem s b) + (hVcCore : VcpuCoreA (L := L) { Z with hw := hw' } s' σ') + (hVcRun : VcRunA st Z.T Z.V s') + (hRunVcpu : RunVcpuInv st Z.T s') + (hEpTcb : ∀ t ∈ Z.T, ∃ x, σ'.kheap (specPtr t) = some (.tcb x) ∧ TcbRelE Rp (L.mem s') t x) + (hEpBuf : ∀ bp ∈ Bf, ∀ k : U64, k.val < 128 → + (L.mem s' (bufW bp.1 k)).bv = loadWordAt σ'.machineState.underlyingMemory (bufAddr bp.2 k.val) ∧ + (bufAddr bp.2 k.val).toNat % 8 = 0) + (hDeadVcpu : ∀ v ∈ Z.V, DeadA st OS .vcpu s' v → ∀ x, σ'.kheap (specPtr v) = some (.archObj (.vcpu x)) → + x.vcpuTcb = none) + (hDeadTvcpu : ∀ t ∈ Z.T, DeadA st OS .tcb s' t → L.mem s' (wa t 3664#u64) = 0#u64) + (hDeadVcRun : ∀ v ∈ Z.V, DeadA st OS .vcpu s' v → L.mem s' (wa v 24#u64) = 0#u64) + (hZombNoVc : ZombieTcbNoVcpuA st OS s') + (hSV' : SchedValidA σ') (R6' : VcpuRec6A st { Z with hw := hw' } OS Bf s' σ') + (sysl : SysL st H Z.Cs OS ia ie s') (symL : SymL st H Z.Cs s') : + JStage3A st AR { Z with hw := hw' } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s' a' σ' m := + (JStage3VcpuCoreA.of_vcpuStepA (J.toVcpuCoreA hNoSw) R6 w HS har hUtVs hVcCore hVcRun hRunVcpu + hEpTcb hEpBuf hDeadVcpu hDeadTvcpu hDeadVcRun hZombNoVc).toJA_vcpuA + J hSV' R6' (osKeep_of_vcpuWrCoreA (J.toVcpuCoreA hNoSw) R6.vcOS R6.frOff w) sysl symL + +end transport +end frame +end +/-! ## Live frame words are off the VCPU steps' kernel words -/ + +section frlive +variable {S A : Type} {st : machine.Store S} [L : LawfulStore st] {ar : machine.Arch A} + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J7 : JSlotA S} [J7FrA st J7] + {s s' : S} {a : A} {σ σ' : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- A live frame word is no VCPU word and no TCB word, and lies in a non-TCB heap object. -/ +theorem _root_.Fl4ProofsA64.P3.JStage3TA.liveOffKerA (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + {b : U64} (al : Aligned b) (lv : FrameWordLive st OS s b) : + ¬ VcW Z.V b ∧ (∀ t ∈ Z.T, ∀ o : U64, o.val < 4096 → b ≠ core.num.U64.wrapping_add t o) ∧ + ∃ p k, H p = some k ∧ p.val ≤ b.val ∧ b.val < p.val + 2 ^ k.bits := by + obtain ⟨u, hu, ptr, size, rights, x1, x2, x3, x4, bits, hc, hb, lo, hi⟩ := lv + obtain ⟨_, Ub, -, -, -, fo, -, frub, -, vcub, -⟩ := J7FrA.fr (st := st) _ _ _ _ _ _ J.j7 + have hpH : H ptr = some (.other bits.val) := fo u hu ptr size rights x1 x2 x3 x4 hc bits hb + refine ⟨fun h => vcub b h (frub u hu ptr size rights x1 x2 x3 x4 hc bits hb b al lo hi), + fun t ht o ho e => ?_, ptr, .other bits.val, hpH, lo, hi⟩ + have htH : H t = some .tcb := show t ∈ H.tcbs by rw [J.tcbs]; exact ht + have hft := J.ent.heap.fits t .tcb htH; change t.val + 4096 ≤ 2 ^ 64 at hft + have hne : t ≠ ptr := fun e => by rw [e, hpH] at htH; cases htH + have hd := J.ent.heap.disj t .tcb ptr (.other bits.val) htH hpH hne + change t.val + 4096 ≤ ptr.val ∨ ptr.val + 2 ^ bits.val ≤ t.val at hd + have ev : (core.num.U64.wrapping_add t o).val = t.val + o.val := by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have := congrArg UScalar.val e; omega + +/-- A step writing only VCPU / TCB words, keeping the `OS` slots and the spec's user memory. -/ +theorem _root_.Fl4ProofsA64.P3.FrKeepA.kerA (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + (osk : OSKeep st OS s s') (um : σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory) + (fm : ∀ b, Aligned b → ¬ VcW Z.V b → (∀ t ∈ Z.T, ∀ o : U64, o.val < 4096 → b ≠ core.num.U64.wrapping_add t o) → + L.mem s' b = L.mem s b) : FrKeepA st OS s σ s' σ' := + FrKeepA.of_off (fun u hu => by unfold capA; rw [(osk u hu).1, (osk u hu).2.1]) + (fun b al lv => fm b al (J.liveOffKerA al lv).1 (J.liveOffKerA al lv).2.1) um + +/-- A live frame word is no VCPU word and no TCB word, and lies in a non-TCB heap object. -/ +theorem JStage3VcpuCoreA.liveOffKerCoreA (J : JStage3VcpuCoreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + {b : U64} (al : Aligned b) (lv : FrameWordLive st OS s b) : + ¬ VcW Z.V b ∧ (∀ t ∈ Z.T, ∀ o : U64, o.val < 4096 → b ≠ core.num.U64.wrapping_add t o) ∧ + ∃ p k, H p = some k ∧ p.val ≤ b.val ∧ b.val < p.val + 2 ^ k.bits := by + obtain ⟨u, hu, ptr, size, rights, x1, x2, x3, x4, bits, hc, hb, lo, hi⟩ := lv + obtain ⟨_, Ub, -, -, -, fo, -, frub, -, vcub, -⟩ := J7FrA.fr (st := st) _ _ _ _ _ _ J.j7 + have hpH : H ptr = some (.other bits.val) := fo u hu ptr size rights x1 x2 x3 x4 hc bits hb + refine ⟨fun h => vcub b h (frub u hu ptr size rights x1 x2 x3 x4 hc bits hb b al lo hi), + fun t ht o ho e => ?_, ptr, .other bits.val, hpH, lo, hi⟩ + have htH : H t = some .tcb := show t ∈ H.tcbs by rw [J.tcbs]; exact ht + have hft := J.ent.heap.fits t .tcb htH; change t.val + 4096 ≤ 2 ^ 64 at hft + have hne : t ≠ ptr := fun e => by rw [e, hpH] at htH; cases htH + have hd := J.ent.heap.disj t .tcb ptr (.other bits.val) htH hpH hne + change t.val + 4096 ≤ ptr.val ∨ ptr.val + 2 ^ bits.val ≤ t.val at hd + have ev : (core.num.U64.wrapping_add t o).val = t.val + o.val := by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have := congrArg UScalar.val e; omega + +/-- A step writing only VCPU / TCB words, keeping the `OS` slots and the spec's user memory. -/ +theorem _root_.Fl4ProofsA64.P3.FrKeepA.kerCoreA (J : JStage3VcpuCoreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + (osk : OSKeep st OS s s') (um : σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory) + (fm : ∀ b, Aligned b → ¬ VcW Z.V b → (∀ t ∈ Z.T, ∀ o : U64, o.val < 4096 → b ≠ core.num.U64.wrapping_add t o) → + L.mem s' b = L.mem s b) : FrKeepA st OS s σ s' σ' := + FrKeepA.of_off (fun u hu => by unfold capA; rw [(osk u hu).1, (osk u hu).2.1]) + (fun b al lv => fm b al (J.liveOffKerCoreA al lv).1 (J.liveOffKerCoreA al lv).2.1) um + +end frlive + +end Fl4ProofsA64.P3.Vcpu diff --git a/proofs/Fl4ProofsA64/P3/VcpuDelHookA.lean b/proofs/Fl4ProofsA64/P3/VcpuDelHookA.lean new file mode 100644 index 000000000..a7353e9fc --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/VcpuDelHookA.lean @@ -0,0 +1,4101 @@ +/- +P3 (aarch64, A64-P3 S4): **VCPU deletion / finalisation hooks on `JStage3TA`**. + +Provides the `JStage3TA` refinement theorems for the VCPU deletion/finalisation chain +(synced to record edit #7 and `WS/handoff/s4-detach-stmt.lean` / `DelS4A`): +- `set_thread_state_running_stage3TA`: `sched.set_thread_state` setting a runnable TCB with + cleared `RUN_VCPU` to `TS_RUNNING` on `JStage3TA` via `sc_set_runnable_stateA` + + `set_thread_state_exAnyA` + `sts_all` + `JStage3TA.of_schedWr`. +- `vcpu_complete_run_stage3TA`: `vcpu.complete_run` refines `fl4CompleteRun` on `JStage3TA`. +- `vcpu_unbind_stage3TA`: `vcpu.unbind` refines `fl4VcpuUnbind` on `JStage3TA`, unconditionally + clearing both `vc + 0` (`TCB`) and `vc + 24` (`RUN_CALLER`). +- `vcpu_finalise_stage3TA`: `vcpu.finalise` refines `fl4VcpuUnbind` on `JStage3TA`, + unconditionally clearing both `vc + 0` (`TCB`) and `vc + 24` (`RUN_CALLER`) for `RvVcA`. +- `vcpu_detach_stage3TA`: `vcpu.detach` refines `fl4VcpuDetach` on `JStage3TA` (exact signature + matching `WS/handoff/s4-detach-stmt.lean` and `DelS4A.detach`). + +Re-exports `fl4Dissociate_comm_tcbFinA`, `fl4VcpuDetach_timeA`, `fl4VcpuEndRun_keeps_bindA` +via imports of `VcpuDelSpecA` (which imports `VcpuDelCommA`). + +No new named premise (uses `[P] VcpuPlatAgreeA`, `[P] VcpuArchKeepsA`, `[P] VcpuVmRootA`, +and `[S] numNodes = 1`). +-/ +import Fl4ProofsA64.P3.VcpuDelFrameA +import Fl4ProofsA64.P3.VcpuDelSpecA +import Fl4ProofsA64.P3.VcpuDelStepsA +import Fl4ProofsA64.P3.VcpuDelReadA +import Fl4ProofsA64.P3.Stage3SchedWrTA +import Fl4ProofsA64.P3.Stage3PreemptTA +import Fl4ProofsA64.P3.ScSetStateA +import Fl4ProofsA64.P3.ReplyRunExA +import Fl4ProofsA64.P3.Stage3DelInA +import Fl4ProofsA64.P1.MdbSetTcbQ + +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3.Vcpu + +open Fl4Spec Fl4SpecA64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST +open Fl4ProofsA64.P3 Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section hooks +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +private theorem lift_okH {α : Type} (x : α) : lift x = ok x := rfl + +private theorem wa_zeroH (x : U64) : wa x 0#u64 = x := by + apply UScalar.eq_of_val_eq + have h0v : (0#u64).val = 0 := rfl + rw [wa_valm, h0v, Nat.add_zero]; exact Nat.mod_eq_of_lt x.hBounds + +private theorem u64_eq_of_bv_eqH {x y : U64} (h : x.bv = y.bv) : x = y := by + apply UScalar.eq_of_val_eq; exact congrArg BitVec.toNat h + +private theorem u64_sub_okH {x y : U64} (h : y.val ≤ x.val) : + x - y = ok ⟨BitVec.ofNat 64 (x.val - y.val)⟩ := by + change UScalar.sub x y = _ + unfold UScalar.sub + rw [if_neg (Nat.not_lt.mpr h)]; rfl + +private theorem u64_shl_u64_okH {x y : U64} (hy : y.val < 64) : + x <<< y = ok ⟨x.bv <<< y.val⟩ := by + change UScalar.shiftLeft x y = _ + unfold UScalar.shiftLeft + have hy' : y.val < UScalarTy.numBits UScalarTy.U64 := hy + rw [if_pos hy']; rfl + +private theorem u64_shl_i32_okH {x : U64} {y : I32} (h0 : 0 ≤ y.val) (h64 : y.val < 64) : + x <<< y = ok ⟨x.bv <<< y.val.toNat⟩ := by + change UScalar.shiftLeft_IScalar x y = _ + unfold UScalar.shiftLeft_IScalar + rw [if_pos h0] + unfold UScalar.shiftLeft + have h1 : y.toNat < UScalarTy.numBits UScalarTy.U64 := by + change y.val.toNat < 64; omega + rw [if_pos h1]; rfl + +private theorem regs_sanitise_spsr_eqH (v : U64) : + regs.sanitise layout.reg.SPSR_EL1 v = ok ⟨sanitiseRegister false .spsrEl1 v.bv⟩ := by + unfold regs.sanitise + rw [if_pos rfl, lift_okH, bind_ok] + have hpu : regs.PSTATE_USER = ok 320#u64 := by + unfold regs.PSTATE_USER + rw [u64_shl_i32_okH (by decide) (by decide), bind_ok, + u64_shl_i32_okH (by decide) (by decide), bind_ok] + congr 1 + rw [hpu, bind_ok] + congr 1 + apply u64_eq_of_bv_eqH + unfold sanitiseRegister regs.PSTATE_FLAGS + simp only [Bool.false_eq_true, false_and, ↓reduceIte] + rfl + +private theorem msginfo_4_eqH : + msginfo.MessageInfo.new 0#u64 0#u64 0#u64 4#u64 = ok ⟨0#u64, 0#u64, 0#u64, 4#u64⟩ ∧ + msginfo.MessageInfo.to_word ⟨0#u64, 0#u64, 0#u64, 4#u64⟩ = ok 4#u64 ∧ + (4#u64 : U64).bv = Fl4SpecA64.messageInfoToData ⟨4, 0, 0, 0⟩ := by + refine ⟨by unfold msginfo.MessageInfo.new; rfl, ?_, by unfold Fl4SpecA64.messageInfoToData; decide⟩ + unfold msginfo.MessageInfo.to_word + rw [lift_okH, bind_ok, u64_shl_i32_okH (by decide) (by decide), bind_ok, + lift_okH, bind_ok, u64_shl_i32_okH (by decide) (by decide), bind_ok, + lift_okH, bind_ok, lift_okH, bind_ok, u64_shl_i32_okH (by decide) (by decide), bind_ok, + lift_okH, bind_ok, lift_okH, bind_ok] + congr 1 + +private theorem msg_reg_0_3_eqH : + msg.msg_register 0#u64 = ok layout.reg.X2 ∧ + msg.msg_register 1#u64 = ok layout.reg.X3 ∧ + msg.msg_register 2#u64 = ok layout.reg.X4 ∧ + msg.msg_register 3#u64 = ok layout.reg.X5 := + ⟨by unfold msg.msg_register; rfl, by unfold msg.msg_register; rfl, + by unfold msg.msg_register; rfl, by unfold msg.msg_register; rfl⟩ + +private theorem tcb_set_reg_eqH (s : S) (t r v : U64) : + obj.Tcb.set_reg st t s r v = ok (L.wr s (Endp.regA t r) v) := by + unfold obj.Tcb.set_reg Endp.regA + rw [lift_okH, bind_ok, lift_okH, bind_ok, lift_okH, bind_ok, L.write_eq] + +private theorem tcb_reg_eqH (s : S) (t r : U64) : + obj.Tcb.reg st t s r = ok (L.mem s (Endp.regA t r)) := by + unfold obj.Tcb.reg Endp.regA + rw [lift_okH, bind_ok, lift_okH, bind_ok, lift_okH, bind_ok, L.read_eq] + +private theorem tcb_reg_addr_eqH {t r : U64} (hr : r.val < 384) (hfit : t.val + 4096 ≤ 2 ^ 64) : + Endp.regA t r = wa t ⟨BitVec.ofNat 64 (256 + 8 * r.val)⟩ := by + unfold Endp.regA + have hregs : layout.tcb.REGS = 256#u64 := by simp [layout.tcb.REGS] + rw [hregs] + apply UScalar.eq_of_val_eq + have h256 : (256#u64 : U64).val = 256 := rfl + have h8 : (8#u64 : U64).val = 8 := rfl + have hmul : (core.num.U64.wrapping_mul r 8#u64).val = 8 * r.val := by + rw [core.num.U64.wrapping_mul_val_eq, h8] + have hs : UScalar.size UScalarTy.U64 = 2 ^ 64 := by simp [U64.size_def, U64.numBits] + rw [hs, Nat.mod_eq_of_lt (by omega)]; omega + have hadd1 : (wa t 256#u64).val = t.val + 256 := by + rw [wa_valm, h256]; exact Nat.mod_eq_of_lt (by omega) + have hdv : (⟨BitVec.ofNat 64 (256 + 8 * r.val)⟩ : U64).val = 256 + 8 * r.val := by + show (BitVec.ofNat 64 _).toNat = _; rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + rw [wa_valm, hadd1, hmul, wa_valm, hdv, Nat.mod_eq_of_lt (by omega), Nat.mod_eq_of_lt (by omega)]; omega + +private theorem tcb_reg_in_vcpuWrH {Z : XSetsA} {OS : Set U64} {s : S} + {t r : U64} (ht : t ∈ Z.T) (hfit : t.val + 4096 ≤ 2 ^ 64) (hr : r.val < 384) : + VcpuWrNoStA st Z OS s (Endp.regA t r) := by + rw [tcb_reg_addr_eqH hr hfit] + refine Or.inr (Or.inl ⟨t, ht, ⟨BitVec.ofNat 64 (256 + 8 * r.val)⟩, ?_, Or.inl ⟨?_, ?_⟩, rfl⟩) + · have hdv : (⟨BitVec.ofNat 64 (256 + 8 * r.val)⟩ : U64).val = 256 + 8 * r.val := by + show (BitVec.ofNat 64 _).toNat = _; rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + rw [hdv]; omega + · have hdv : (⟨BitVec.ofNat 64 (256 + 8 * r.val)⟩ : U64).val = 256 + 8 * r.val := by + show (BitVec.ofNat 64 _).toNat = _; rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + rw [hdv]; omega + · have hdv : (⟨BitVec.ofNat 64 (256 + 8 * r.val)⟩ : U64).val = 256 + 8 * r.val := by + show (BitVec.ofNat 64 _).toNat = _; rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + rw [hdv]; omega + +private theorem setObject_tcb_eqH {σ : State Unit} {t : ObjRef} {x x' : Tcb} + (hkt : σ.kheap t = some (.tcb x)) : + setObject (Z := Unit) t (.tcb x') σ = ({((), updKSc σ t (.tcb x'))}, False) := by + unfold setObject getObject + have hg0 : NonDet.getsThe (readObject (Z := Unit) t) σ = ({(.tcb x, σ)}, False) := by + simp [NonDet.getsThe, readObject, hkt, NonDet.assertOpt]; rfl + rw [nd_bind_oneSc hg0] + have ha : NonDet.assert (σ := State Unit) (aType (.tcb x) = aType (.tcb x')) σ = ({((), σ)}, False) := by + simp [NonDet.assert, aType] + rw [nd_bind_oneSc ha]; simp [updKSc, NonDet.get, NonDet.put] + +private theorem asUser_eq_updH {α : Type} {σ : State Unit} {t : ObjRef} {x : Tcb} + {f : NonDet UserContext α} {a : α} {uc' : UserContext} + (hkt : σ.kheap t = some (.tcb x)) + (hf : f (archTcbContextGet x.tcbArch) = ({(a, uc')}, False)) : + asUser (Z := Unit) t f σ = ({(a, updKSc σ t (.tcb { x with tcbArch := archTcbContextSet uc' x.tcbArch }))}, False) := by + unfold asUser + have hg : NonDet.getsThe (getTcb (Z := Unit) t) σ = ({(x, σ)}, False) := by + simp [NonDet.getsThe, getTcb, readObject, hkt, NonDet.assertOpt]; rfl + rw [nd_bind_oneSc hg, nd_bind_oneSc (show (pure (archTcbContextGet x.tcbArch) : SMonad Unit UserContext) σ = _ from rfl), hf] + have hsf : NonDet.selectF (σ := State Unit) ({(a, uc')}, False) σ = ({((a, uc'), σ)}, False) := by + unfold NonDet.selectF; simp; ext ⟨x1, x2⟩; simp + rw [nd_bind_oneSc hsf] + dsimp only + rw [nd_bind_oneSc (show (pure ({ x with tcbArch := archTcbContextSet uc' x.tcbArch } : Tcb) : SMonad Unit Tcb) σ = _ from rfl), + nd_bind_oneSc (setObject_tcb_eqH hkt)] + rfl + +private theorem asUser_setReg_eqH {σ : State Unit} {t : ObjRef} {x : Tcb} + (hkt : σ.kheap t = some (.tcb x)) (r : Register) (v : Word) : + let uc := x.tcbArch.tcbContext + let x' := { x with tcbArch := { x.tcbArch with tcbContext := ⟨uc.userFpuState, Function.update uc.userRegs r v⟩ } } + asUser (Z := Unit) t (setRegister r v) σ = ({((), updKSc σ t (.tcb x'))}, False) := + asUser_eq_updH hkt rfl + +private theorem asUser_seqH {σ σ1 σ2 : State Unit} {t : ObjRef} {x : Tcb} + {f : NonDet UserContext Unit} {g : NonDet UserContext Unit} + {uc1 uc2 : UserContext} + (hkt : σ.kheap t = some (.tcb x)) + (hf : f (archTcbContextGet x.tcbArch) = ({((), uc1)}, False)) + (hg : g uc1 = ({((), uc2)}, False)) + (h1 : asUser (Z := Unit) t f σ = ({((), σ1)}, False)) + (h2 : asUser (Z := Unit) t g σ1 = ({((), σ2)}, False)) : + asUser (Z := Unit) t (do f; g) σ = ({((), σ2)}, False) := by + have hfg : (do f; g) (archTcbContextGet x.tcbArch) = ({((), uc2)}, False) := by + rw [nd_bind_oneSc hf]; exact hg + have h1' : σ1 = updKSc σ t (.tcb { x with tcbArch := archTcbContextSet uc1 x.tcbArch }) := + (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst (h1.symm.trans (asUser_eq_updH hkt hf))))).2 + subst h1' + have hkt1 : (updKSc σ t (.tcb { x with tcbArch := archTcbContextSet uc1 x.tcbArch })).kheap t = + some (.tcb { x with tcbArch := archTcbContextSet uc1 x.tcbArch }) := by + simp [updKSc, Function.update] + have h2' : σ2 = updKSc (updKSc σ t (.tcb { x with tcbArch := archTcbContextSet uc1 x.tcbArch })) t + (.tcb { x with tcbArch := archTcbContextSet uc2 x.tcbArch }) := + (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst (h2.symm.trans (asUser_eq_updH hkt1 hg))))).2 + have h2'' : σ2 = updKSc σ t (.tcb { x with tcbArch := archTcbContextSet uc2 x.tcbArch }) := by + rw [h2']; simp [updKSc, Function.update_idem] + rw [h2'']; exact asUser_eq_updH hkt hfg + +private theorem VcpuRec6A.with_hw {Z : XSetsA} {OS : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {s : S} {σ : Fl4SpecA64.State Unit} (R6 : VcpuRec6A st Z OS Bf s σ) (hw' : HwRootA) : + VcpuRec6A st { Z with hw := hw' } OS Bf s σ := + ⟨R6.snap, R6.vcSym1, R6.vcSym2, R6.vcBack, R6.runBack, R6.vcOS, R6.frOff, R6.userFr, R6.frTh, R6.bfSpec⟩ + +private def j3aVcpuRec6A {AR : A → HwRootA → Prop} + {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J5 J7 : JSlotA S} + {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) : + VcpuRec6A st Z OS Bf s σ := + ⟨J.vcSnap, J.vcSym1, J.vcSym2, J.vcBack, J.runBack, J.vcOS, J.sys.sysl.fo, J.userFr, J.frTh, J.bfSpec⟩ + +/-! ## Section 0b: `SysL ∧ SymL` transport for VCPU / `set_thread_state` writes on `JStage3A` -/ + +theorem sysL_symL_of_vwA + {AR : A → HwRootA → Prop} {nA : U64} + {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J7 : JSlotA S} + {Pt Ub Pp' Tb' Hr Te Kt : Set U64} {Lv' Re bits KLv : U64 → Nat} + {s s' : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + (W : J7UnpA st ar nA Z H OS s Pt Ub Pp' Tb' Hr Te Kt Lv' Re bits KLv) + {Cw : U64 → Prop} + (w : WritesIn st s s' (VW st H.tcbs Z.Cs Z.V {b | b ∈ Ub ∧ FrBw H OS b ∧ FrCw st OS s b} (L.ksb s) Cw)) + (hi : L.idleW s' = L.idleW s) + (hc : ∀ y, Cw y → L.mem s (stA y) ≠ sched.TS_BLOCKED_ON_REPLY ∧ L.mem s (stA y) ≠ sched.TS_INACTIVE) : + SysL st H Z.Cs OS ia ie s' ∧ SymL st H Z.Cs s' := by + have hB : BlkOK st H.tcbs Z.Cs H.omega H H.frame H.replies H.scs H.ntfns H.scBits Ac s := J.sys.ent.bxi.blk + have h0 : H 0#u64 = none := J.sys.sysl.ly.h0 + have V := J.vcOK W J.vcOS + have vty := J.vcTy W V.bcov + have hW : ∀ b, VcW Z.V b → b ∈ H.omega ∧ b ∉ H.frame := fun b ⟨v, hv, o, ho, h8, e⟩ => by + subst e; exact ⟨(vty.obj v hv).1 o ho h8 |>.2.1, (vty.obj v hv).1 o ho h8 |>.2.2⟩ + have d := DMB.ofVW (U := {b | b ∈ Ub ∧ FrBw H OS b ∧ FrCw st OS s b}) (sd := s) + hB h0 J.symL J.sys.sysl.vr (fun b hb => vty.usr b hb.1) (fun _ hb => hb.2) hW w hi hc + have hp0 : ∀ k, H 0#u64 = some k → ∃ y ∈ OS, NamesA (capA st s y) 0#u64 k := fun k hk => by + rw [h0] at hk; cases hk + exact SysL.dfMDB J.sys.ent.heap J.sys.ent.os V (fun b hb => (hW b hb).1) hB J.ap.sca hp0 hp0 J.sys.sysl d + +private theorem vcpuWrNoSt_to_vwA + {AR : A → HwRootA → Prop} {nA : U64} + {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J7 : JSlotA S} + {Pt Ub Pp' Tb' Hr Te Kt : Set U64} {Lv' Re bits KLv : U64 → Nat} + {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + (W : J7UnpA st ar nA Z H OS s Pt Ub Pp' Tb' Hr Te Kt Lv' Re bits KLv) + {b : U64} (hb : VcpuWrNoStA st Z OS s b) : + VW st H.tcbs Z.Cs Z.V {b | b ∈ Ub ∧ FrBw H OS b ∧ FrCw st OS s b} (L.ksb s) (fun _ => False) b := by + rcases hb with ⟨v, hv, e | e | e⟩ | ⟨t, ht, K, hK, hcase, rfl⟩ | + ⟨y, hy, ptr, sz, r, mp, ept, asid, va, bits, hc, hbits, hal, h1, h2⟩ + · exact Or.inr (Or.inl ⟨v, hv, 0#u64, by decide, by decide, e.trans (wa_zeroH v).symm⟩) + · exact Or.inr (Or.inl ⟨v, hv, 8#u64, by decide, by decide, e⟩) + · exact Or.inr (Or.inl ⟨v, hv, 24#u64, by decide, by decide, e⟩) + · have htH : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + rcases hcase with ⟨hlo, hhi⟩ | rfl | rfl + · exact Or.inr (Or.inr (Or.inl ⟨t, htH, Or.inl ⟨K, hlo, hhi, hK, rfl⟩⟩)) + · exact Or.inr (Or.inr (Or.inl ⟨t, htH, Or.inr (Or.inl rfl)⟩)) + · exact Or.inr (Or.inr (Or.inl ⟨t, htH, Or.inr (Or.inr (Or.inl rfl))⟩)) + · have hp : H ptr = some (.other bits.val) := W.cc.fo y hy ptr sz r mp ept asid va hc bits hbits + have hFo := (J.sys.sysl.fo y hy ptr sz r mp ept asid va hc bits hbits).1 + have hb12 : 12 ≤ bits.val := by + cases sz <;> simp only [capability.FrameSize.bits] at hbits <;> rw [← okinj hbits] <;> simp + exact Or.inl ⟨W.fu y hy ptr sz r mp ept asid va hc bits hbits b hal h1 h2, + ⟨frame_word J.sys.ent.heap hp hal ⟨h1, h2⟩, ⟨ptr, bits.val, hp, hb12, h1, h2⟩, fun z hz g => by + rcases hFo z hz with g' | g' <;> omega⟩, + ⟨y, hy, ptr, sz, r, mp, ept, asid, va, bits, hc, hbits, h1, h2⟩⟩ + +private theorem sysL_symL_of_vcpuWrA + {AR : A → HwRootA → Prop} {nA : U64} + {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J7 : JSlotA S} + [J7PpA st J7] [J7FrA st J7] + {Pt Ub Pp' Tb' Hr Te Kt : Set U64} {Lv' Re bits KLv : U64 → Nat} + {s s' : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + (W : J7UnpA st ar nA Z H OS s Pt Ub Pp' Tb' Hr Te Kt Lv' Re bits KLv) + (hNoSw : ∀ u, σ.schedulerAction ≠ .switchThread u) + (w : WritesIn st s s' (VcpuWrNoStA st Z OS s)) : + SysL st H Z.Cs OS ia ie s' ∧ SymL st H Z.Cs s' := by + have wv := w.mono (fun _ hb => vcpuWrNoSt_to_vwA J W hb) + have hi : L.idleW s' = L.idleW s := by + unfold LawfulStore.idleW + have h8 : layout.node.IDLE_THREAD.val = 8 := by unfold layout.node.IDLE_THREAD; rfl + have hal : Aligned (L.nodeAt s (L.cpu s) layout.node.IDLE_THREAD) := + nodeAt_aligned (J.sq.nodes _ J.sr.sch.cpuCs) (by rw [h8]; decide) (by rw [h8]) + rw [w.2.1, nodeAt_of_ksb w.1] + exact w.mem_eq hal (fun hb => by + have hna := nodeAt_val (J.sq.nodes _ J.sr.sch.cpuCs) (by rw [h8]; decide) + unfold LawfulStore.nodeBase at hna + have hcpu128 : (L.cpu s).val < 128 := by + obtain ⟨nn, -, hkn, pcs⟩ := J.sr.toSchedSRN.csN; have := (pcs _).1 J.sr.sch.cpuCs; omega + have hfit := J.sys.ent.ksw + have hks : (L.ksb s).val ≤ (L.nodeAt s (L.cpu s) layout.node.IDLE_THREAD).val ∧ + (L.nodeAt s (L.cpu s) layout.node.IDLE_THREAD).val < (L.ksb s).val + ksSizeA := by + unfold ksSizeA; rw [hna, h8]; omega + exact ks_off_vcpuWrCoreA (J.toVcpuCoreA hNoSw) hks.1 hks.2 hb) + exact sysL_symL_of_vwA J W wv hi (fun _ h => h.elim) + +/-! ## Section 1: `sched.set_thread_state ... TS_RUNNING` on `JStage3TA` and `JStage3A` -/ + +private structure StsRunOutA + (AR : A → HwRootA → Prop) (Clock : A → Fl4SpecA64.MachineState → Prop) + (Z : XSetsA) (H : Heap) (OS U Ac Pp Tb : Set U64) (Lv : U64 → Nat) + (ia ie : Nat) (N E Rp : Set U64) (Bf : Set (U64 × Fl4SpecA64.Word)) + (stack : U64 → List U64) (κ : U64 → U64) (n : Nat) (J7 : JSlotA S) + (s s' : S) (a : A) (σ σ' : Fl4SpecA64.State Unit) (m : Fl4SpecA64.McState) + (t : U64) (x : Fl4SpecA64.Tcb) : Prop where + hsts : setThreadState (Z := Unit) (specPtr t) .running σ = ({((), σ')}, False) + hkh' : σ'.kheap = (updKSc σ (specPtr t) (.tcb { x with tcbState := .running })).kheap + hKhOther : ∀ p, p ≠ specPtr t → σ'.kheap p = σ.kheap p + hcur : σ'.curThread = σ.curThread + hms : σ'.machineState = σ.machineState + hstepW : WritesIn st s s' (SchedStepWA (st := st) Z m σ s) + hSpecS : SpecSchedA σ σ' + hSSz : SSzA σ σ' + hQk : QKeepA σ σ' + bufAl' : UtBufAlA Z.T σ' + ent' : EntryInvA3 st H Z.Cs OS U Ac Pp Tb Lv s' + sq' : SchedQueuesInv st H.tcbs Z.Cs H.omega H s' Z.Q Z.R + poolOff' : PoolOff st H Z.Cs OS s' + sr' : SchedSRA st Z m κ s' σ' + scCov' : ScCovA Z.C m σ' + ep' : EpStateRel Z.T E Rp Bf (L.mem s') σ' + ntfn' : NtfnStateRel N Z.T (L.mem s') σ' + reply' : ReplyStackRelA (L.mem s') Rp Z.C σ' (scNameA m) stack + bufCover' : BufCover st Z.T Bf s' σ' + idleNoFault' : IdleNoFaultA m σ' + mcp' : McpRelA Z.T m (L.mem s') σ' + yieldRel' : YieldRelA Z.T Z.C m (L.mem s') σ' + sinv' : SInvXA σ' m n + sidle' : SIdleA σ' m n + runVcpu' : RunVcpuInv st Z.T s' + vr'' : ∀ sc ∈ Z.C, ∀ r ∈ stack sc, RBlk st s' r + blkQ' : BlkQueuedA Z.T E N (L.mem s') σ' + noSwitch' : ∀ u, σ'.schedulerAction ≠ .switchThread u + j5' : J5A st Z H OS s' σ' m + j7' : J7 Z H OS s' σ' m + hSV' : SchedValidA σ' + hR6' : VcpuRec6A st Z OS Bf s' σ' + hCl' : Clock a σ'.machineState + kb : L.ksb s' = L.ksb s + hc : L.cpu s' = L.cpu s + osk : OSKeep st OS s s' + w2 : WritesIn st s s' (fun b => b = stA t ∨ b = L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) + +set_option maxHeartbeats 1200000 in +private theorem set_thread_state_running_coreA + {AR : A → HwRootA → Prop} {Clock : A → Fl4SpecA64.MachineState → Prop} + {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J7 : JSlotA S} + [J7PpA st J7] [J7PpKA st J7] [J7FrA st J7] + {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3VcpuCoreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + (hSV : SchedValidA σ) + (R6 : VcpuRec6A st Z OS Bf s σ) + (hCl : Clock a σ.machineState) + {t : U64} (ht : t ∈ Z.T) {x : Fl4SpecA64.Tcb} + (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hrun : runnable x.tcbState = true) + (hrv0 : L.mem s (runVcpuA t) = 0#u64) + (hOskStep : ∀ {s'}, WritesIn st s s' (SchedStepWA (st := st) Z m σ s) → OSKeep st OS s s') + (hHeapOffNodes : ∀ {p : U64} {k : HKind}, H p = some k → ∀ {b : U64}, p.val ≤ b.val → b.val < p.val + 2 ^ k.bits → + ¬ ((L.ksb s).val + 223488 ≤ b.val ∧ b.val < (L.ksb s).val + 772352)) + (hAsidOff : ∀ {b : U64}, (L.ksb s).val + 772352 ≤ b.val → b.val < (L.ksb s).val + 773376 → + ¬ SchedStepWA (st := st) Z m σ s b) + (hVsrOff : ∀ {b : U64}, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → ¬ AsW (L.ksb s) b → + ¬ SchedStepWA (st := st) Z m σ s b) + (hPpVsr : ∀ {s'}, WritesIn st s s' (SchedStepWA (st := st) Z m σ s) → + ∀ Pt Ub : Set U64, VrOK H.omega H.frame H.scs H.scBits OS Z.V Ub (L.ksb s) Pt → VsKeep st (L.ksb s) Pt s s') : + sched.set_thread_state st s t sched.TS_RUNNING ⦃ s' => + ∃ σ' : Fl4SpecA64.State Unit, + StsRunOutA (st := st) AR Clock Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J7 s s' a σ σ' m t x ⦄ := by + have hst : StEqA Z.T (L.mem s) σ := fun u hu y hy => (J.ep.at_tcb hu hy).st + have hv : sched.TS_RUNNING.val = tsCodeA .running := by unfold sched.TS_RUNNING; rfl + have htH : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + have hRunC : Runnable (L.mem s (stA t)) := (runnable_iffA (hst t ht x hk)).2 hrun + have hnb : ¬ Blocked (L.mem s (stA t)) := by + intro hb + rcases hRunC with h | h | h <;> rcases hb with h' | h' | h' <;> + (rw [h] at h'; have hv' := congrArg UScalar.val h'; simp [sched.TS_RUNNING, sched.TS_RESTART, + sched.TS_RUNNING_VM, sched.TS_BLOCKED_ON_SEND, sched.TS_BLOCKED_ON_RECV, sched.TS_BLOCKED_ON_NTFN] at hv') + have hnr : L.mem s (stA t) ≠ sched.TS_BLOCKED_ON_REPLY := by + intro h' + rcases hRunC with h | h | h <;> + (rw [h] at h'; have hv' := congrArg UScalar.val h'; simp [sched.TS_RUNNING, sched.TS_RESTART, + sched.TS_RUNNING_VM, sched.TS_BLOCKED_ON_REPLY] at hv') + obtain ⟨-, hB, hX, hSg, hS⟩ := J.ent.bxi + have hnoref : ∀ r ∈ H.replies, L.mem s (P1.rTcbA r) ≠ t := + hX.noref (J.sq.tcbs t htH) hnr (fun e => hnb (Or.inr (Or.inl e))) + have hnb_run : ¬ Blocked sched.TS_RUNNING := by + rintro (h | h | h) <;> + (have hv' := congrArg UScalar.val h; simp [sched.TS_RUNNING, sched.TS_BLOCKED_ON_SEND, + sched.TS_BLOCKED_ON_RECV, sched.TS_BLOCKED_ON_NTFN] at hv') + have hne_rep : sched.TS_RUNNING ≠ sched.TS_BLOCKED_ON_REPLY ∧ sched.TS_RUNNING ≠ sched.TS_BLOCKED_ON_RECV := + ⟨fun h => by have hv' := congrArg UScalar.val h; simp [sched.TS_RUNNING, sched.TS_BLOCKED_ON_REPLY] at hv', + fun h => by have hv' := congrArg UScalar.val h; simp [sched.TS_RUNNING, sched.TS_BLOCKED_ON_RECV] at hv'⟩ + have hrun_run : Runnable sched.TS_RUNNING := Or.inl rfl + refine WP.spec_mono (WP.spec_and (WP.spec_and + (sc_set_runnable_stateA J.sr hst ht hk hrun (ts := .running) rfl hv) + (set_thread_state_exAnyA J.sr hst ht hk (ts := .running) rfl hv)) + (sts_all s t sched.TS_RUNNING Z.Q Z.R J.sq hB hX hS hSg htH hnb_run hne_rep (Or.inr hnb) hnoref + ⟨fun _ _ _ _ _ => hrun_run, fun _ _ _ => hrun_run, fun _ => hrun_run⟩)) ?_ + rintro s' ⟨⟨⟨σ', hsts, sr', hst', hkh', wS⟩, ⟨-, -, -, -, -, w2⟩⟩, ⟨sq1, hB1, hX1, hS1, hSg1, -, -⟩⟩ + have sq' : SchedQueuesInv st H.tcbs Z.Cs H.omega H s' Z.Q Z.R := sq1.congrX empty_congr + have hσ'_eq : σ' = stsActA (updKSc σ (specPtr t) (.tcb { x with tcbState := .running })) (specPtr t) := + congrArg Prod.snd (Set.singleton_eq_singleton_iff.1 + (congrArg Prod.fst (hsts.symm.trans (setThreadState_stsActExA hk .running)))) + have hms : σ'.machineState = σ.machineState := by + rw [hσ'_eq]; unfold stsActA; split <;> rfl + have hrq : σ'.readyQueues = σ.readyQueues := by + rw [hσ'_eq]; unfold stsActA; split <;> rfl + have hrls : σ'.releaseQueue = σ.releaseQueue := by + rw [hσ'_eq]; unfold stsActA; split <;> rfl + have hcur : σ'.curThread = σ.curThread := by + rw [hσ'_eq]; unfold stsActA; split <;> rfl + have hidle : σ'.idleThread = σ.idleThread := by + rw [hσ'_eq]; unfold stsActA; split <;> rfl + have hct : σ'.curTime = σ.curTime := by + rw [hσ'_eq]; unfold stsActA; split <;> rfl + have hcsc : σ'.curSc = σ.curSc := by + rw [hσ'_eq]; unfold stsActA; split <;> rfl + have hact_cases : σ'.schedulerAction = σ.schedulerAction ∨ σ'.schedulerAction = .chooseNewThread := by + rw [hσ'_eq]; unfold stsActA; split + · exact Or.inr rfl + · exact Or.inl rfl + have hstepW : WritesIn st s s' (SchedStepWA (st := st) Z m σ s) := + wS.mono fun b hb => hb.elim (fun h => Or.inl (Or.inr h)) + (fun e => by subst e; exact Or.inr (stA_tcbStepWA (fun u hu => J.sr.core.tcbs u hu) ht)) + have hSpecS : SpecSchedA σ σ' := by + refine ⟨fun p => ?_, + by rw [hσ'_eq]; unfold stsActA; split <;> rfl, + by rw [hσ'_eq]; unfold stsActA; split <;> rfl, + by rw [hσ'_eq]; unfold stsActA; split <;> rfl, + by rw [hσ'_eq]; unfold stsActA; split <;> rfl, + by rw [hσ'_eq]; unfold stsActA; split <;> rfl, + by rw [hσ'_eq]; unfold stsActA; split <;> rfl⟩ + by_cases ep : p = specPtr t + · subst ep + refine Or.inr (Or.inl ⟨x, { x with tcbState := .running }, hk, by rw [hkh', updKSc_same], + ⟨rfl, rfl, rfl⟩⟩) + · exact Or.inl (by rw [hkh', updKSc_kheap, Function.update_of_ne ep]) + have hSSz : SSzA σ σ' := fun p y k h => by + by_cases ep : p = specPtr t + · subst ep; rw [hkh', updKSc_same] at h; cases h + · exact ⟨y, by rw [hkh', updKSc_kheap, Function.update_of_ne ep] at h; exact h⟩ + have kb := w2.1 + have hc := w2.2.1 + have osk := hOskStep hstepW + have capE : ∀ y ∈ OS, capA st s' y = capA st s y := fun y hy => by + unfold capA; rw [(osk y hy).1, (osk y hy).2.1] + have hksFit : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.ent.ksw + have hcpu_lt : (L.cpu s).val < 128 := by + obtain ⟨nn, -, hkn, pcs⟩ := J.sr.toSchedSRN.csN + have := (pcs (L.cpu s)).1 J.sr.sch.cpuCs; omega + have hnode_val : (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION).val = + (L.ksb s).val + 223488 + (L.cpu s).val * 4288 + 16 := by + have h16 : (layout.node.SCHED_ACTION : U64).val = 16 := by unfold layout.node.SCHED_ACTION; rfl + have hna := nodeAt_val (J.sq.nodes _ J.sr.sch.cpuCs) (by rw [h16]; omega) + rw [hna, h16]; rfl + have hTcbOff : ∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → + (u ≠ t ∨ K ≠ 3328#u64) → L.mem s' (wa u K) = L.mem s (wa u K) := fun u hu K hK1 hK8 hne => by + have hal := field_aligned (J.sr.core.tcbs u hu) hK1 hK8 + refine w2.mem_eq hal fun h => ?_ + rcases h with h | h + · rw [stA_eq] at h + have huH : H u = some .tcb := show u ∈ H.tcbs by rw [J.tcbs]; exact hu + have htH' : H t = some .tcb := htH + have hfu := J.ent.heap.fits u .tcb huH; change u.val + 4096 ≤ 2 ^ 64 at hfu + have hft := J.ent.heap.fits t .tcb htH'; change t.val + 4096 ≤ 2 ^ 64 at hft + have hv1 : (wa u K).val = u.val + K.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have hv2 : (wa t 3328#u64).val = t.val + 3328 := by + rw [wa_valm]; change (t.val + 3328) % _ = _; exact Nat.mod_eq_of_lt (by omega) + have hev := congrArg UScalar.val h; rw [hv1, hv2] at hev + rcases hne with eut | eK + · have hd := J.ent.heap.disj u .tcb t .tcb huH htH' eut + change u.val + 4096 ≤ t.val ∨ t.val + 4096 ≤ u.val at hd; omega + · exact eK (UScalar.eq_of_val_eq (by + by_cases eut : u = t + · subst eut; change K.val = 3328; omega + · have hd := J.ent.heap.disj u .tcb t .tcb huH htH' eut + change u.val + 4096 ≤ t.val ∨ t.val + 4096 ≤ u.val at hd; omega)) + · have huH : H u = some .tcb := show u ∈ H.tcbs by rw [J.tcbs]; exact hu + have hfu := J.ent.heap.fits u .tcb huH; change u.val + 4096 ≤ 2 ^ 64 at hfu + have hv1 : (wa u K).val = u.val + K.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have hko := hHeapOffNodes huH (b := wa u K) (by omega) (by change (wa u K).val < u.val + 4096; omega) + rw [h] at hko; exact hko ⟨by omega, by omega⟩ + have hNonTcb : ∀ p k, H p = some k → k ≠ .tcb → (∀ z, k ≠ .other z) → + ∀ b, Aligned b → p.val ≤ b.val → b.val < p.val + 2 ^ k.bits → L.mem s' b = L.mem s b := by + intro p k hp hkT hkO b hb h1 h2 + refine w2.mem_eq hb fun h => ?_ + rcases h with rfl | rfl + · have hft := J.ent.heap.fits t .tcb htH; change t.val + 4096 ≤ 2 ^ 64 at hft + have hne : p ≠ t := fun e => by rw [e, htH] at hp; cases hp; exact hkT rfl + have hd := J.ent.heap.disj p k t .tcb hp htH hne + have hv : (stA t).val = t.val + 3328 := by + rw [stA_eq, wa_valm]; change (t.val + 3328) % _ = _; exact Nat.mod_eq_of_lt (by omega) + change p.val + 2 ^ k.bits ≤ t.val ∨ t.val + 4096 ≤ p.val at hd; omega + · exact hHeapOffNodes hp h1 h2 ⟨by omega, by omega⟩ + have hvs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → L.mem s' b = L.mem s b := fun b hb => by + refine hstepW.mem_eq (J.vsOk.al b hb) ?_ + by_cases ha : AsW (L.ksb s) b + · obtain ⟨o, o1, o2, -, e⟩ := ha + have ev : b.val = (L.ksb s).val + o.val := by rw [e, wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact hAsidOff (by omega) (by omega) + · exact hVsrOff hb ha + have hvsT : VsTyR st (L.ksb s') (PoolSet st Pp s') ∅ Tb Lv s' := (utVsPt_keepA J.ent.vs kb hvs).1 + have ent' : EntryInvA3 st H Z.Cs OS U Ac Pp Tb Lv s' := + ppEntA3 J.ent ⟨⟨Z.Q, Z.R, sq'⟩, hB1, hX1, hS1, hSg1⟩ kb hvsT + have poolOff' : PoolOff st H Z.Cs OS s' := by + refine poolOff_keepTableA (Z := Z) J.poolOff kb fun i => ?_ + have hv := AtOff.v i + have ev : (wa (L.ksb s) (AtOff i)).val = (L.ksb s).val + (AtOff i).val := by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have hba : Aligned (wa (L.ksb s) (AtOff i)) := by + have := J.j.1.ksbAl; show _ % 8 = 0; omega + exact hstepW.mem_eq hba (hAsidOff (by omega) (by omega)) + have hKhOther : ∀ p, p ≠ specPtr t → σ'.kheap p = σ.kheap p := fun p hp => by + rw [hkh', updKSc_kheap, Function.update_of_ne hp] + have scCov' : ScCovA Z.C m σ' := fun p y k hp => + J.scCov p y k (by + by_cases ep : p = specPtr t + · subst ep; rw [hkh', updKSc_same] at hp; cases hp + · exact (hKhOther p ep).symm.trans hp) + have hBufEq : ∀ bp ∈ Bf, ∀ k : U64, k.val < 128 → L.mem s' (bufW bp.1 k) = L.mem s (bufW bp.1 k) := by + intro bp hbp k hk + have hbw : BufW Bf (bufW bp.1 k) := ⟨bp, hbp, k, hk, rfl⟩ + refine w2.mem_eq (J.iplaced.e.balign _ hbw) fun h => ?_ + rcases h with h | h + · have hbt := J.iplaced.e.bt _ hbw t ht + have hft := J.ent.heap.fits t .tcb htH; change t.val + 4096 ≤ 2 ^ 64 at hft + have hv : (stA t).val = t.val + 3328 := by + rw [stA_eq, wa_valm]; change (t.val + 3328) % _ = _; exact Nat.mod_eq_of_lt (by omega) + rw [h, hv] at hbt; omega + · obtain ⟨p, len, hp, hlo, hhi⟩ := J.ipcPlace.buffer _ hbw + have hks := J.sr.place.ks p (.other len) hp (fun irq => by cases J.sr.place.irqK hp irq) + simp only [ObjKindA.size] at hks + rw [h, hnode_val] at hlo hhi; unfold ksSizeA at hks; omega + have ep' : EpStateRel Z.T E Rp Bf (L.mem s') σ' := by + refine ⟨fun e he => ?_, fun r hr => ?_, fun u hu => ?_, fun bp hbp k hk => ?_⟩ + · obtain ⟨ep0, hke, hrel⟩ := J.ep.ep e he + have heH : H e = some .ep := J.utIpc.ep e he + have hne : specPtr e ≠ specPtr t := fun eq => by + have : e = t := specPtr_inj eq; rw [this, htH] at heH; cases heH + have hfe := J.ent.heap.fits e .ep heH; simp only [HKind.bits] at hfe + have hae := J.ent.heap.align e .ep heH; simp only [HKind.bits] at hae + have heq : ∀ o : U64, o.val < 16 → o.val % 8 = 0 → L.mem s' (wa e o) = L.mem s (wa e o) := fun o ho1 ho8 => by + have ev : (wa e o).val = e.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact hNonTcb e .ep heH (by intro h; cases h) (fun _ h => by cases h) _ (by show _ % 8 = 0; omega) (by omega) (by simp only [HKind.bits]; omega) + refine ⟨ep0, by rw [hKhOther _ hne]; exact hke, ?_⟩ + refine hrel.frame (by unfold epHeadA layout.ep.HEAD; exact heq 0#u64 (by decide) (by decide)) + (by unfold epTSA layout.ep.TAIL_STATE; exact heq 8#u64 (by decide) (by decide)) fun y hy hq => ?_ + have hne_yt : y ≠ t := fun ey => by + subst ey + have hcode := (J.ep.at_tcb ht hk).st + cases ep0 with + | idleEP => simp [Endpoint.epQueue] at hq + | sendEP q => + obtain ⟨-, xs, -, rfl, -, hxs⟩ := hrel + obtain ⟨y', hy', e⟩ := List.mem_map.1 hq + have := specPtr_inj e; subst this + have h4 := (hxs y' hy').1; rw [hcode] at h4 + cases hs : x.tcbState with + | ext e => cases e; rw [hs] at h4; simp [tsCode] at h4 + | _ => rw [hs] at h4 hrun; simp [tsCode, runnable] at h4 hrun + | recvEP q => + obtain ⟨-, xs, -, rfl, -, hxs⟩ := hrel + obtain ⟨y', hy', e⟩ := List.mem_map.1 hq + have := specPtr_inj e; subst this + have h3 := (hxs y' hy').1; rw [hcode] at h3 + cases hs : x.tcbState with + | ext e => cases e; rw [hs] at h3; simp [tsCode] at h3 + | _ => rw [hs] at h3 hrun; simp [tsCode, runnable] at h3 hrun + exact ⟨by rw [stA_eq, hTcbOff y hy 3328#u64 (by decide) (by decide) (Or.inl hne_yt), ← stA_eq], + by unfold tBlockA; exact hTcbOff y hy _ (by simp) (by simp) (Or.inl hne_yt), + by rw [prevA_eq']; exact hTcbOff y hy _ (by simp [prevOff]) (by simp [prevOff]) (Or.inl hne_yt), + by rw [nextA_eq']; exact hTcbOff y hy _ (by simp [nextOff]) (by simp [nextOff]) (Or.inl hne_yt)⟩ + · obtain ⟨rp0, hkr, hrel⟩ := J.ep.reply r hr + have hrH : H r = some .reply := J.utIpc.reply r hr + have hne : specPtr r ≠ specPtr t := fun eq => by + have : r = t := specPtr_inj eq; rw [this, htH] at hrH; cases hrH + have hfr := J.ent.heap.fits r .reply hrH; simp only [HKind.bits] at hfr + have har := J.ent.heap.align r .reply hrH; simp only [HKind.bits] at har + have hrt : L.mem s' (Endp.rTcbA r) = L.mem s (Endp.rTcbA r) := by + unfold Endp.rTcbA layout.reply.TCB; rw [wa_zeroH] + exact hNonTcb r .reply hrH (by intro h; cases h) (fun _ h => by cases h) r (by show r.val % 8 = 0; omega) (le_refl _) (by simp only [HKind.bits]; omega) + exact ⟨rp0, by rw [hKhOther _ hne]; exact hkr, by rw [hrt]; exact hrel⟩ + · obtain ⟨y, hy, hrel⟩ := J.ep.tcb u hu + by_cases eu : u = t + · subst eu; rw [hk] at hy; cases hy + refine ⟨{ x with tcbState := .running }, by rw [hkh', updKSc_same], ?_⟩ + refine ⟨by rw [hst' u hu _ (by rw [hkh', updKSc_same])]; rfl, + (fun _ h => by simp [epBlocked] at h), + (fun _ _ h => by cases h), + (fun _ _ _ h => by cases h), + (fun _ h => by cases h), + (fun r => by + have hr := regIdxA_lt r + have hft := J.ent.heap.fits u .tcb htH; change u.val + 4096 ≤ 2 ^ 64 at hft + rw [tcb_reg_addr_eqH (by omega) hft] + have hdv : (⟨BitVec.ofNat 64 (256 + 8 * (regIdxA r).val)⟩ : U64).val = 256 + 8 * (regIdxA r).val := by + show (BitVec.ofNat 64 _).toNat = _; rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + rw [hTcbOff u hu _ (by omega) (by omega) (Or.inr (by intro e; have := congrArg UScalar.val e; rw [hdv] at this; change 256 + 8 * (regIdxA r).val = 3328 at this; omega))] + rw [← tcb_reg_addr_eqH (by omega) hft]; exact hrel.regs r), + (by unfold prioA; rw [hTcbOff u hu _ (by simp) (by simp) (Or.inr (by decide))]; exact hrel.prio), + (by unfold tFaultA; rw [hTcbOff u hu _ (by simp) (by simp) (Or.inr (by decide))]; exact hrel.fault0), + (by unfold tFaultA; rw [hTcbOff u hu _ (by simp) (by simp) (Or.inr (by decide))]; exact hrel.faultT), + (by unfold tBoundA; rw [hTcbOff u hu _ (by simp) (by simp) (Or.inr (by decide))]; exact hrel.bound), + (by unfold FaultRel tFaultA tFw0A tFw1A tFw2A tLfA tLf0A tLf1A + rw [hTcbOff u hu _ (by simp) (by simp) (Or.inr (by decide)), + hTcbOff u hu _ (by simp) (by simp) (Or.inr (by decide)), + hTcbOff u hu _ (by simp) (by simp) (Or.inr (by decide)), + hTcbOff u hu _ (by simp) (by simp) (Or.inr (by decide)), + hTcbOff u hu _ (by simp) (by simp) (Or.inr (by decide)), + hTcbOff u hu _ (by simp) (by simp) (Or.inr (by decide)), + hTcbOff u hu _ (by simp) (by simp) (Or.inr (by decide))] + exact hrel.faultR)⟩ + · have hne : specPtr u ≠ specPtr t := fun e => eu (specPtr_inj e) + refine ⟨y, by rw [hKhOther _ hne]; exact hy, hrel.frame (J.sr.core.tcbs u hu) fun b hb hbu => ?_⟩ + have huH : H u = some .tcb := show u ∈ H.tcbs by rw [J.tcbs]; exact hu + have hfu := J.ent.heap.fits u .tcb huH; change u.val + 4096 ≤ 2 ^ 64 at hfu + have hlt : b.val - u.val < 3664 := by have := hbu.2; unfold TOffN at this; omega + have hb_eq : b = wa u ⟨BitVec.ofNat 64 (b.val - u.val)⟩ := by + apply UScalar.eq_of_val_eq; rw [wa_valm] + have hdv : (⟨BitVec.ofNat 64 (b.val - u.val)⟩ : U64).val = b.val - u.val := by + show (BitVec.ofNat 64 _).toNat = _; rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + rw [hdv, Nat.add_sub_of_le hbu.1]; exact (Nat.mod_eq_of_lt (by omega)).symm + have hdv : (⟨BitVec.ofNat 64 (b.val - u.val)⟩ : U64).val = b.val - u.val := by + show (BitVec.ofNat 64 _).toNat = _; rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + have hua := J.ent.heap.align u .tcb huH; change u.val % 4096 = 0 at hua + have h8 : (b.val - u.val) % 8 = 0 := by have : b.val % 8 = 0 := hb; omega + rw [hb_eq]; exact hTcbOff u hu _ (by omega) (by omega) (Or.inl eu) + · rw [hBufEq bp hbp k hk, hms]; exact J.ep.buf bp hbp k hk + have ntfn' : NtfnStateRel N Z.T (L.mem s') σ' := by + refine ⟨fun n' hn' => ?_, fun u hu => ?_⟩ + · obtain ⟨nf, hkn, hrel⟩ := J.ntfn.ntfn n' hn' + have hnH : H n' = some .ntfn := J.utIpc.ntfn n' hn' + have hne : specPtr n' ≠ specPtr t := fun eq => by + have : n' = t := specPtr_inj eq; rw [this, htH] at hnH; cases hnH + have hfn := J.ent.heap.fits n' .ntfn hnH; change n'.val + 64 ≤ 2 ^ 64 at hfn + have han := J.ent.heap.align n' .ntfn hnH; change n'.val % 64 = 0 at han + have hnq : ∀ o : U64, o.val < 64 → o.val % 8 = 0 → L.mem s' (wa n' o) = L.mem s (wa n' o) := fun o ho1 ho8 => by + have ev : (wa n' o).val = n'.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact hNonTcb n' .ntfn hnH (by intro h; cases h) (fun _ h => by cases h) _ (by show _ % 8 = 0; omega) (by omega) (by change (wa n' o).val < n'.val + 64; omega) + refine ⟨nf, by rw [hKhOther _ hne]; exact hkn, ?_⟩ + refine hrel.frame ⟨by unfold epHeadA layout.ep.HEAD; exact hnq 0#u64 (by decide) (by decide), + by unfold epTSA layout.ep.TAIL_STATE; exact hnq 8#u64 (by decide) (by decide), + by unfold nBadgeA; exact hnq _ (by simp [layout.ntfn.BADGE]) (by simp [layout.ntfn.BADGE]), + by unfold nBoundA; exact hnq _ (by simp [layout.ntfn.BOUND_TCB]) (by simp [layout.ntfn.BOUND_TCB]), + by unfold nScA; exact hnq _ (by simp [layout.ntfn.SC]) (by simp [layout.ntfn.SC])⟩ fun y hy hq => ?_ + have hne_yt : y ≠ t := fun ey => by + subst ey + have hblk := (hrel.obj.blocked hq).2.1 + have hcode := (J.ep.at_tcb ht hk).st; rw [hcode] at hblk + cases hs : x.tcbState with + | ext e => cases e; rw [hs] at hblk; simp [tsCode] at hblk + | _ => rw [hs] at hblk hrun; simp [tsCode, runnable] at hblk hrun + exact ⟨by rw [stA_eq, hTcbOff y hy 3328#u64 (by decide) (by decide) (Or.inl hne_yt), ← stA_eq], + by unfold tBlockA; exact hTcbOff y hy _ (by simp) (by simp) (Or.inl hne_yt), + by rw [prevA_eq']; exact hTcbOff y hy _ (by simp [prevOff]) (by simp [prevOff]) (Or.inl hne_yt), + by rw [nextA_eq']; exact hTcbOff y hy _ (by simp [nextOff]) (by simp [nextOff]) (Or.inl hne_yt)⟩ + · obtain ⟨y, hy, hrel⟩ := J.ntfn.tcb u hu + by_cases eu : u = t + · subst eu; rw [hk] at hy; cases hy + refine ⟨{ x with tcbState := .running }, by rw [hkh', updKSc_same], + ⟨(by rw [hst' u hu _ (by rw [hkh', updKSc_same])]; rfl), + (fun _ hn => by cases hn), + (by rw [badgeRegA_eq, hTcbOff u hu _ (by simp) (by simp) (Or.inr (by decide)), ← badgeRegA_eq]; exact hrel.badge), + (by rw [prioA_eq', hTcbOff u hu _ (by simp) (by simp) (Or.inr (by decide)), ← prioA_eq']; exact hrel.prio)⟩⟩ + · have hne : specPtr u ≠ specPtr t := fun e => eu (specPtr_inj e) + exact ⟨y, by rw [hKhOther _ hne]; exact hy, + ⟨(by rw [stA_eq, hTcbOff u hu _ (by simp) (by simp) (Or.inl eu), ← stA_eq]; exact hrel.st), + (fun n' hn => by unfold tBlockA; rw [hTcbOff u hu _ (by simp) (by simp) (Or.inl eu)]; exact hrel.blk n' hn), + (by rw [badgeRegA_eq, hTcbOff u hu _ (by simp) (by simp) (Or.inl eu), ← badgeRegA_eq]; exact hrel.badge), + (by rw [prioA_eq', hTcbOff u hu _ (by simp) (by simp) (Or.inl eu), ← prioA_eq']; exact hrel.prio)⟩⟩ + have reply' : ReplyStackRelA (L.mem s') Rp Z.C σ' (scNameA m) stack := by + refine J.reply.frame (fun r hr => ?_) + (fun c _ y k h => ⟨y, k, by rw [hKhOther _ (fun e => by rw [e, hk] at h; cases h)]; exact h, rfl⟩) + (fun r _ y h => ⟨y, by + by_cases e : specPtr r = specPtr t + · rw [e, hkh', updKSc_same] at h; cases h + · exact (hKhOther _ e).symm.trans h, rfl⟩) + have hrH : H r = some .reply := J.utIpc.reply r hr + have hfr := J.ent.heap.fits r .reply hrH; change r.val + 32 ≤ 2 ^ 64 at hfr + have har := J.ent.heap.align r .reply hrH; change r.val % 32 = 0 at har + have hrq : ∀ o : U64, o.val < 32 → o.val % 8 = 0 → L.mem s' (wa r o) = L.mem s (wa r o) := fun o ho1 ho8 => by + have ev : (wa r o).val = r.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact hNonTcb r .reply hrH (by intro h; cases h) (fun _ h => by cases h) _ (by show _ % 8 = 0; omega) (by omega) (by change (wa r o).val < r.val + 32; omega) + exact ⟨hrq _ (by simp [layout.reply.NEXT]) (by simp [layout.reply.NEXT]), + hrq _ (by simp [layout.reply.PREV]) (by simp [layout.reply.PREV])⟩ + have bufCover' : BufCover st Z.T Bf s' σ' := by + intro u hu y b sb v hy hb hl + have hib : IbSame (L := L) s s' u := by + refine ⟨hTcbOff u hu _ (by simp) (by simp) (Or.inr (by decide)), ?_, ?_⟩ + · rw [sbWordA_eq]; exact hTcbOff u hu _ (by simp) (by simp) (Or.inr (by decide)) + · rw [sbWordA8_eq]; exact hTcbOff u hu _ (by simp) (by simp) (Or.inr (by decide)) + rw [lookup_ipc_buffer_sameA hib] at hl + by_cases eu : u = t + · rcases eu with rfl; rw [hkh', updKSc_same] at hy; cases hy + exact J.bufCover u hu x b sb v hk hb hl + · rw [hKhOther _ (fun e => eu (specPtr_inj e))] at hy + exact J.bufCover u hu y b sb v hy hb hl + have idleNoFault' : IdleNoFaultA m σ' := fun c hc' => by + obtain ⟨y, hy, hf⟩ := J.idleNoFault c hc' + by_cases e : m.idleThreadOf c = specPtr t + · rw [e, hk] at hy; cases hy + exact ⟨{ x with tcbState := .running }, by rw [e, hkh', updKSc_same], hf⟩ + · exact ⟨y, by rw [hKhOther _ e]; exact hy, hf⟩ + have mcp' : McpRelA Z.T m (L.mem s') σ' := + J.mcp.congrA (fun u hu => ⟨by unfold mcpA; exact hTcbOff u hu _ (by simp) (by simp) (Or.inr (by decide)), + by unfold flagsA; exact hTcbOff u hu _ (by simp) (by simp) (Or.inr (by decide))⟩) (fun u hu y hy => by + by_cases eu : u = t + · rcases eu with rfl; rw [hk] at hy; cases hy + exact ⟨{ x with tcbState := .running }, by rw [hkh', updKSc_same], rfl, rfl⟩ + · exact ⟨y, by rw [hKhOther _ (fun e => eu (specPtr_inj e))]; exact hy, rfl, rfl⟩) + have yieldRel' : YieldRelA Z.T Z.C m (L.mem s') σ' := by + have hye : ∀ u ∈ Z.T, L.mem s' (yieldA u) = L.mem s (yieldA u) := fun u hu => by + unfold yieldA; exact hTcbOff u hu _ (by simp) (by simp) (Or.inr (by decide)) + refine ⟨fun u hu => ?_, fun u hu h => ?_, fun c hcc y k h p hp => ?_⟩ + · obtain ⟨y, hy, e⟩ := J.yieldRel.tcb u hu + by_cases eu : u = t + · rcases eu with rfl; rw [hk] at hy; cases hy + exact ⟨{ x with tcbState := .running }, by rw [hkh', updKSc_same], by rw [hye u hu]; exact e⟩ + · exact ⟨y, by rw [hKhOther _ (fun e => eu (specPtr_inj e))]; exact hy, by rw [hye u hu]; exact e⟩ + · rw [hye u hu] at h ⊢ + obtain ⟨hC, y, k, hy, e⟩ := J.yieldRel.toSc u hu h + exact ⟨hC, y, k, by rw [hKhOther _ (fun eq => by rw [eq, hk] at hy; cases hy)]; exact hy, e⟩ + · have h0 : σ.kheap (scNameA m c) = some (.schedContext y k) := by + by_cases e : scNameA m c = specPtr t + · rw [e, hkh', updKSc_same] at h; cases h + · rwa [hKhOther _ e] at h + obtain ⟨u, hu, e1, e2⟩ := J.yieldRel.frm c hcc y k h0 p hp + exact ⟨u, hu, e1, by rw [hye u hu]; exact e2⟩ + have htNotIdle : x.tcbState ≠ .idleThreadState := fun hi => by rw [hi] at hrun; exact absurd hrun (by decide) + have htNeIdle : ∀ c < n, m.idleThreadOf c ≠ specPtr t := fun c hc e => by + obtain ⟨y, hy, hs⟩ := J.sidle.f3 c hc; rw [e, hk] at hy; cases hy; exact htNotIdle hs + have htNeCurIdle : σ.idleThread ≠ specPtr t := fun e => by + obtain ⟨y, hy, hs⟩ := J.sinv.idleTcb; rw [e, hk] at hy; cases hy; exact htNotIdle hs + have hSpecCapEq : ∀ sl, specCapAtA σ' sl = specCapAtA σ sl := fun sl => by + unfold specCapAtA + by_cases e : sl.1 = specPtr t + · rw [e, hkh', updKSc_same, hk]; rfl + · rw [hKhOther _ e] + have hcn : σ'.consumedTime = σ.consumedTime := by + rw [hσ'_eq]; unfold stsActA; split <;> rfl + have CF := CurFactsA.back (σ := σ) (σ' := σ') ⟨J.sinv.zc, J.sinv.rlsRun, J.sinv.curScReady⟩ + (fun t' h => by rw [hrls] at h; exact h) hcn hcsc hct + (fun t' x' hx' => by + by_cases e : t' = specPtr t + · subst e; rw [hkh', updKSc_same] at hx'; cases hx'; exact ⟨x, hk, rfl⟩ + · exact ⟨x', by rw [← hKhOther _ e]; exact hx', rfl⟩) + (fun t' x0 hx0 _ => by + by_cases e : t' = specPtr t + · subst e; rw [hk] at hx0; cases hx0 + exact ⟨{ x with tcbState := .running }, by rw [hkh', updKSc_same], fun _ => rfl⟩ + · exact ⟨x0, by rw [hKhOther _ e]; exact hx0, id⟩) + (fun y k hy => by + have hneSc : σ.curSc ≠ specPtr t := fun e => by + obtain ⟨y1, k1, h1⟩ := J.sinv.curSc + rw [e, hk] at h1; cases h1 + rw [hKhOther _ hneSc] at hy + exact ⟨y, hy, fun a => ⟨a, id⟩⟩) + have sinv' : SInvXA σ' m n := + { mc := J.sinv.mc + view := by unfold McView; rw [hidle]; exact J.sinv.view + curTcb := by + obtain ⟨y, hy⟩ := J.sinv.curTcb + rw [hcur] + by_cases e : σ.curThread = specPtr t + · rw [e, hk] at hy; cases hy; exact ⟨{ x with tcbState := .running }, by rw [e, hkh', updKSc_same]⟩ + · exact ⟨y, by rw [hKhOther _ e]; exact hy⟩ + idleTcb := by + obtain ⟨y, hy, hs⟩ := J.sinv.idleTcb + exact ⟨y, by rw [hidle, hKhOther _ htNeCurIdle]; exact hy, hs⟩ + onlyIdle := fun p y hy hs => by + by_cases e : p = specPtr t + · rw [e, hkh', updKSc_same] at hy; cases hy; cases hs + · rw [hKhOther _ e] at hy; exact J.sinv.onlyIdle p y hy hs + idleSc := by + obtain ⟨y, sc, k, hy, hsc1, hsc2, hsc3⟩ := J.sinv.idleSc + have hneSc : Fl4SpecA64.idleScPtr ≠ specPtr t := fun e => by rw [e, hk] at hsc2; cases hsc2 + exact ⟨y, sc, k, by rw [hidle, hKhOther _ htNeCurIdle]; exact hy, hsc1, + by rw [hKhOther _ hneSc]; exact hsc2, by rw [hidle]; exact hsc3⟩ + curSc := by + obtain ⟨sc, k, hsc⟩ := J.sinv.curSc + have hneSc : σ.curSc ≠ specPtr t := fun e => by rw [e, hk] at hsc; cases hsc + exact ⟨sc, k, by rw [hcsc, hKhOther _ hneSc]; exact hsc⟩ + ctQ := fun ha d p => by + rw [hcur, hrq] + rcases hact_cases with h | h + · exact J.sinv.ctQ (h.symm.trans ha) d p + · rw [h] at ha; cases ha + idleQ := fun d p c hc => by rw [hrq]; exact J.sinv.idleQ d p c hc + rq := fun d p q hq => by + rw [hrq] at hq + obtain ⟨y, hy, hry⟩ := J.sinv.rq d p q hq + by_cases e : q = specPtr t + · rw [e, hk] at hy; cases hy; exact ⟨{ x with tcbState := .running }, by rw [e, hkh', updKSc_same], rfl⟩ + · exact ⟨y, by rw [hKhOther _ e]; exact hy, hry⟩ + rls := fun q hq => by + rw [hrls] at hq + obtain ⟨y, hy⟩ := J.sinv.rls q hq + by_cases e : q = specPtr t + · rw [e, hk] at hy; cases hy; exact ⟨{ x with tcbState := .running }, by rw [e, hkh', updKSc_same]⟩ + · exact ⟨y, by rw [hKhOther _ e]; exact hy⟩ + rpos := fun p sc k hsc => by + have hneSc : p ≠ specPtr t := fun e => by rw [e, hkh', updKSc_same] at hsc; cases hsc + rw [hKhOther _ hneSc] at hsc; exact J.sinv.rpos p sc k hsc + irqN := by + have hirqn : σ'.interruptIrqNode = σ.interruptIrqNode := hSpecS.irqN + have hcnIff : ∀ p sz cn, σ'.kheap p = some (.cnode sz cn) ↔ σ.kheap p = some (.cnode sz cn) := fun p sz cn => by + by_cases e : p = specPtr t + · rw [e, hkh', updKSc_same, hk]; simp + · rw [hKhOther _ e] + have hi := J.sinv.irqN + refine ⟨⟨⟨fun irq1 irq2 h1 h2 heq => by rw [hirqn] at heq; exact hi.node.1.1 irq1 irq2 h1 h2 heq, + fun irq h1 => by + obtain ⟨cn, hcn, hwf⟩ := hi.node.1.2 irq h1 + exact ⟨cn, by rw [hirqn, hcnIff]; exact hcn, hwf⟩⟩, + fun irq h1 sz hcn => by + obtain ⟨cn, hcn', hwf⟩ := hcn + rw [hirqn, hcnIff] at hcn' + exact hi.node.2 irq h1 sz ⟨cn, hcn', hwf⟩⟩, + fun irq h1 o b a ho hcap => by + rw [hirqn] at ho + by_cases e : σ.interruptIrqNode irq = specPtr t + · rw [e, hkh', updKSc_same] at ho; cases ho + exact hi.slot irq h1 (.tcb x) b a (by rw [e]; exact hk) hcap + · rw [hKhOther _ e] at ho; exact hi.slot irq h1 o b a ho hcap, + fun p o b a ho hcap => by + rw [hirqn] + by_cases e : p = specPtr t + · rw [e, hkh', updKSc_same] at ho; cases ho + exact hi.ref p (.tcb x) b a (by rw [e]; exact hk) hcap + · rw [hKhOther _ e] at ho; exact hi.ref p o b a ho hcap⟩ + zc := CF.1 + rlsRun := CF.2.1 + curScReady := CF.2.2 + faultSt := fun p y hy hf => by + by_cases e : p = specPtr t + · subst e; rw [hkh', updKSc_same] at hy; cases hy + have h0 := J.sinv.faultSt (specPtr t) x hk hf + exact absurd hrun (by rw [h0.notRun]; decide) + · rw [hKhOther _ e] at hy; exact J.sinv.faultSt p y hy hf } + have sidle' : SIdleA σ' m n := + { f3 := fun c hc => by + obtain ⟨y, hy, hs⟩ := J.sidle.f3 c hc + exact ⟨y, by rw [hKhOther _ (htNeIdle c hc)]; exact hy, hs⟩ + f4 := fun y k hy => by + have hne : Fl4SpecA64.idleScPtr ≠ specPtr t := fun e => by rw [e, hkh', updKSc_same] at hy; cases hy + rw [hKhOther _ hne] at hy; exact J.sidle.f4 y k hy + f5 := fun p y k hy he => by + have hne : p ≠ specPtr t := fun e => by rw [e, hkh', updKSc_same] at hy; cases hy + rw [hKhOther _ hne] at hy; rw [hidle] at he; exact J.sidle.f5 p y k hy he + f6 := J.sidle.f6.of_eq hSpecCapEq + f7 := fun y k hy => by + have hne : Fl4SpecA64.idleScPtr ≠ specPtr t := fun e => by rw [e, hkh', updKSc_same] at hy; cases hy + rw [hKhOther _ hne] at hy; rw [hct]; exact J.sidle.f7 y k hy + idleSlot := fun c hc => + (idleVtSameA (hKhOther _ (htNeIdle c hc)) (J.sidle.idleSlot c hc) (J.sidle.idleBound c hc)).1 + idleBound := fun c hc => + (idleVtSameA (hKhOther _ (htNeIdle c hc)) (J.sidle.idleSlot c hc) (J.sidle.idleBound c hc)).2 } + have runVcpu' : RunVcpuInv st Z.T s' := fun u hu h0 => by + have hrv : L.mem s' (runVcpuA u) = L.mem s (runVcpuA u) := by + unfold runVcpuA; exact hTcbOff u hu 3672#u64 (by simp) (by simp) (Or.inr (by decide)) + rw [hrv] at h0 + have hne : u ≠ t := fun e => h0 (e ▸ hrv0) + rw [stA_eq, hTcbOff u hu 3328#u64 (by simp) (by simp) (Or.inl hne), ← stA_eq] + exact J.runVcpu u hu h0 + have vr'' : ∀ sc ∈ Z.C, ∀ r ∈ stack sc, RBlk st s' r := by + intro sc hsc r hr + have h0 := J.vr sc hsc r hr + have hrRp : r ∈ Rp := (J.reply.stackRel sc hsc).2.1 r hr + have hrH : H r = some .reply := J.utIpc.reply r hrRp + have hfr := J.ent.heap.fits r .reply hrH; change r.val + 32 ≤ 2 ^ 64 at hfr + have har := J.ent.heap.align r .reply hrH; change r.val % 32 = 0 at har + have hrt : L.mem s' (P1.rTcbA r) = L.mem s (P1.rTcbA r) := by + unfold P1.rTcbA layout.reply.TCB; rw [wa_zeroH] + exact hNonTcb r .reply hrH (by intro h; cases h) (fun _ h => by cases h) r (by show r.val % 8 = 0; omega) (le_refl _) (by change r.val < r.val + 32; omega) + have hne_t : L.mem s (P1.rTcbA r) ≠ t := fun et => by + have hb := congrArg UScalar.val h0.blk; rw [et, (J.ep.at_tcb ht hk).st] at hb + cases hs : x.tcbState with + | ext e => cases e; rw [hs] at hb; simp [tsCode, sched.TS_BLOCKED_ON_REPLY] at hb + | _ => rw [hs] at hb hrun; simp [tsCode, runnable, sched.TS_BLOCKED_ON_REPLY] at hb hrun + have huT : L.mem s (P1.rTcbA r) ∈ Z.T := by + rw [← J.tcbs] + exact (J.ent.bxi.2.2.1.2.1.back r hrH h0.tcb).2.1 + have hst_eq : L.mem s' (stA (L.mem s (P1.rTcbA r))) = L.mem s (stA (L.mem s (P1.rTcbA r))) := by + rw [stA_eq, hTcbOff _ huT 3328#u64 (by simp) (by simp) (Or.inl hne_t), ← stA_eq] + exact ⟨by rw [hrt]; exact h0.tcb, by rw [hrt, hst_eq]; exact h0.blk⟩ + have blkQ' : BlkQueuedA Z.T E N (L.mem s') σ' := + { ep := fun u hu y hy e hb => by + by_cases eu : specPtr u = specPtr t + · rw [eu, hkh', updKSc_same] at hy; cases hy; simp [epBlocked] at hb + · rw [hKhOther _ eu] at hy + obtain ⟨a, ep0, b, c⟩ := J.blkQ.ep u hu y hy e hb + have hne : e ≠ specPtr t := fun eq => by rw [eq, hk] at b; cases b + exact ⟨by unfold tBlockA; rw [hTcbOff u hu 3336#u64 (by simp) (by simp) (Or.inr (by decide))]; exact a, + ep0, by rw [hKhOther _ hne]; exact b, c⟩ + ntfn := fun u hu y hy p hb => by + by_cases eu : specPtr u = specPtr t + · rw [eu, hkh', updKSc_same] at hy; cases hy; cases hb + · rw [hKhOther _ eu] at hy + obtain ⟨a, nf0, q, b, c, d⟩ := J.blkQ.ntfn u hu y hy p hb + have hne : p ≠ specPtr t := fun eq => by rw [eq, hk] at b; cases b + exact ⟨by unfold tBlockA; rw [hTcbOff u hu 3336#u64 (by simp) (by simp) (Or.inr (by decide))]; exact a, + nf0, q, by rw [hKhOther _ hne]; exact b, c, d⟩ } + have noSwitch' : ∀ u, σ'.schedulerAction ≠ .switchThread u := fun u => by + rcases hact_cases with h | h + · rw [h]; exact J.noSwitch u + · rw [h]; intro e; cases e + have hTr : TreeA st OS Z.G.chains Z.G.par s := + J.j.1.cdt.tree.grow J.j5.2.1 (fun y hy hn => J.j5.2.2 y hy hn) + have j5' : J5A st Z H OS s' σ' m := by + refine ⟨⟨MdbInvA.keepO hTr.lk hTr.tree J.j5.1.1 osk, IrqRunA.keepO hTr.lk J.j5.1.2.1 osk, + HandlersActiveA.keepO hTr.lk J.j5.1.2.2 osk kb ?_⟩, J.j5.2.1, + fun y hy hn => by rw [capE y hy]; exact J.j5.2.2 y hy hn⟩ + intro i hi + have eI : irqStAddrA (L.ksb s) i = irqWordA (L.ksb s) i := rfl + have hv := irqWordA_val J.sr.ksFit hi + have hal : Aligned (irqWordA (L.ksb s) i) := by + have := J.j.1.ksbAl; show _ % 8 = 0; omega + rw [eI] + refine w2.mem_eq hal fun h => ?_ + rcases h with h | h + · have hft := J.ent.heap.fits t .tcb htH; change t.val + 4096 ≤ 2 ^ 64 at hft + have hstV : (stA t).val = t.val + 3328 := by + rw [stA_eq, wa_valm]; change (t.val + 3328) % _ = _; exact Nat.mod_eq_of_lt (by omega) + have hkt := J.sr.tcb_ks ht; unfold ksSizeA at hkt + rw [h, hstV] at hv; omega + · rw [h, hnode_val] at hv; omega + have j7' : J7 Z H OS s' σ' m := + J7PpKA.keep (st := st) _ _ _ _ _ σ σ' _ J.j7 kb osk (hPpVsr hstepW) + have bufAl' : UtBufAlA Z.T σ' := fun u hu y hy => by + by_cases eu : u = t + · rcases eu with rfl; rw [hkh', updKSc_same] at hy; cases hy; exact J.bufAl u hu x hk + · rw [hKhOther _ (fun e => eu (specPtr_inj e))] at hy; exact J.bufAl u hu y hy + have hQk : QKeepA σ σ' := by + have tc : ∀ p o, σ'.kheap p = some o → (∀ y, o ≠ .tcb y) → σ.kheap p = some o := fun p o hp hn => by + by_cases e : p = specPtr t + · subst e; rw [hkh', updKSc_same] at hp; cases hp; exact absurd rfl (hn _) + · rw [hKhOther _ e] at hp; exact hp + refine ⟨fun q y hy => ?_, fun p x' hp => ⟨x', tc _ _ hp (fun _ h => by cases h), List.Sublist.refl _⟩, + fun p x' hp => ⟨x', tc _ _ hp (fun _ h => by cases h), List.Sublist.refl _⟩, + fun p y k hp => ⟨y, k, tc _ _ hp (fun _ h => by cases h), fun r => ⟨r, id⟩⟩, + fun p y hp => ⟨y, tc _ _ hp (fun _ h => by cases h), id⟩, + ⟨by rw [hσ'_eq]; unfold stsActA; split <;> rfl, by rw [hσ'_eq]; unfold stsActA; split <;> rfl, + by rw [hσ'_eq]; unfold stsActA; split <;> rfl, by rw [hσ'_eq]; unfold stsActA; split <;> rfl⟩⟩ + by_cases e : q = specPtr t + · subst e; rw [hkh', updKSc_same] at hy; cases hy; exact ⟨x, hk, rfl⟩ + · rw [hKhOther _ e] at hy; exact ⟨y, hy, rfl⟩ + have hSV' : SchedValidA σ' := by + have sc : ∀ p, ScActiveAtA σ p → ScActiveAtA σ' p := fun p ⟨y, k, e, act⟩ => + ⟨y, k, by rw [hKhOther p (fun h => by rw [h, hk] at e; cases e)]; exact e, act⟩ + have tcbAct : ∀ p, (∃ y c, σ.kheap p = some (.tcb y) ∧ y.tcbSchedContext = some c ∧ ScActiveAtA σ c) → + ∃ y c, σ'.kheap p = some (.tcb y) ∧ y.tcbSchedContext = some c ∧ ScActiveAtA σ' c := fun p ⟨y, c, e, es, act⟩ => by + by_cases h : p = specPtr t + · rw [h] at e ⊢; rw [hk] at e; cases e; exact ⟨{ x with tcbState := .running }, c, by rw [hkh', updKSc_same], es, sc _ act⟩ + · exact ⟨y, c, by rw [hKhOther p h]; exact e, es, sc _ act⟩ + refine ⟨fun d p u hm => ?_, fun e1 e2 => ?_, fun u x' c e1 e2 e3 => ?_, fun u hu => ?_⟩ + · rw [hrq] at hm; obtain ⟨a0, nr⟩ := hSV.rq d p u hm + exact ⟨tcbAct u a0, by rw [hrls]; exact nr⟩ + · rcases hact_cases with h | h + · rw [h] at e1; rw [hcur, hidle] at e2 + obtain ⟨act0, nr⟩ := hSV.act e1 e2 + exact ⟨by rw [hcur]; exact tcbAct _ act0, by rw [hcur, hrls]; exact nr⟩ + · rw [h] at e1; cases e1 + · by_cases h : u = specPtr t + · subst h; rw [hkh', updKSc_same] at e1; cases e1; simp [BlockedTsA] at e2 + · rw [hKhOther u h] at e1; exact sc _ (hSV.blk u x' c e1 e2 e3) + · rw [hrls] at hu; exact tcbAct u (hSV.rls u hu) + have hVcEq : ∀ v ∈ Z.V, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → L.mem s' (wa v o) = L.mem s (wa v o) := by + intro v hv o ho1 ho8 + obtain ⟨_, _, -, arm, -, -, -, -, -, -, -⟩ := J7FrA.fr (st := st) _ _ _ _ _ _ J.j7 + have hvH := arm.vh v hv + have hfv := J.ent.heap.fits v _ hvH; have hav := J.ent.heap.align v _ hvH + simp only [HKind.bits] at hfv hav + have ev : (wa v o).val = v.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + refine w2.mem_eq (by show _ % 8 = 0; omega) fun h => ?_ + rcases h with h | h + · have hne : v ≠ t := fun e => by rw [e, htH] at hvH; cases hvH + have hd := J.ent.heap.disj v (.other 12) t .tcb hvH htH hne + have hft := J.ent.heap.fits t .tcb htH; change t.val + 4096 ≤ 2 ^ 64 at hft + have hstV : (stA t).val = t.val + 3328 := by + rw [stA_eq, wa_valm]; change (t.val + 3328) % _ = _; exact Nat.mod_eq_of_lt (by omega) + change v.val + 4096 ≤ t.val ∨ t.val + 4096 ≤ v.val at hd + have := congrArg UScalar.val h; rw [ev, hstV] at this; omega + · have hko := hHeapOffNodes hvH (b := wa v o) (by omega) (by change (wa v o).val < v.val + 4096; omega) + rw [h] at hko; exact hko ⟨by omega, by omega⟩ + have hSnap' : VcpuStateSnapA Z.V (L.mem s') σ' := by + have hkv0f : ∀ v xv, σ'.kheap (specPtr v) = some (.archObj (.vcpu xv)) → + σ.kheap (specPtr v) = some (.archObj (.vcpu xv)) := fun v xv hkv => by + by_cases e : specPtr v = specPtr t + · rw [e, hkh', updKSc_same] at hkv; cases hkv + · rwa [hKhOther _ e] at hkv + refine ⟨fun v hv xv hkv => ?_, fun v hv xv hkv => R6.snap.mask v hv xv (hkv0f v xv hkv)⟩ + have hkv0 := hkv0f v xv hkv + have h0 := R6.snap.snap v hv xv hkv0 + exact ⟨fun r hr => by + have hdv : (⟨BitVec.ofNat 64 (64 + 8 * r)⟩ : U64).val = 64 + 8 * r := by + show (BitVec.ofNat 64 _).toNat = _; rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + rw [hVcEq v hv _ (by omega) (by omega)]; exact h0.1 r hr, + by rw [hVcEq v hv 16#u64 (by decide) (by decide)]; exact h0.2.1, + by rw [hVcEq v hv 32#u64 (by decide) (by decide)]; exact h0.2.2.1, + by rw [hVcEq v hv 40#u64 (by decide) (by decide)]; exact h0.2.2.2.1, + by rw [hVcEq v hv 8#u64 (by decide) (by decide)]; exact h0.2.2.2.2⟩ + have hFrOff' : FrOff st Z.Cs OS (L.ksb s') s' := by + rw [kb] + intro u hu p0 sz0 r0 mp0 ept0 a0 va0 hc0 b0 hb0 + exact R6.frOff u hu p0 sz0 r0 mp0 ept0 a0 va0 ((capE u hu).symm.trans hc0) b0 hb0 + have hFrTh' : FrThA st Z.th OS s' := R6.frTh.keepA capE fun _ _ e => e + have hUserFr' : UserFrSpecA st OS s' σ' := + R6.userFr.keepA capE fun p _ _ h => by + rw [hKhOther p (fun e => by rw [e, hk] at h; cases h)]; exact h + have hR6' : VcpuRec6A st Z OS Bf s' σ' := by + refine ⟨hSnap', fun u hu => ?_, fun v hv => ?_, fun v hv hv0 => ?_, fun u hu hu0 => ?_, + R6.vcOS, hFrOff', hUserFr', hFrTh', R6.bfSpec⟩ + · rw [hTcbOff u hu 3664#u64 (by simp) (by simp) (Or.inr (by decide))] + intro hne + have hvV := (J.vcRun.tcb u hu).resolve_left hne + have h0 := hVcEq _ hvV 0#u64 (by decide) (by decide); rw [wa_zeroH] at h0 + rw [h0]; exact R6.vcSym1 u hu hne + · rw [hVcEq v hv 24#u64 (by decide) (by decide)] + intro hne + have huT := (J.vcRun.vcpu v hv).resolve_left hne + unfold runVcpuA; rw [hTcbOff _ huT 3672#u64 (by simp) (by simp) (Or.inr (by decide))] + exact R6.vcSym2 v hv hne + · have h0 := hVcEq v hv 0#u64 (by decide) (by decide); rw [wa_zeroH] at h0 + rw [h0] at hv0 ⊢ + have huT := (J.vcRun.vtcb v hv).resolve_left hv0 + rw [hTcbOff _ huT 3664#u64 (by simp) (by simp) (Or.inr (by decide))] + exact R6.vcBack v hv hv0 + · unfold runVcpuA at hu0 ⊢ + rw [hTcbOff u hu 3672#u64 (by simp) (by simp) (Or.inr (by decide))] at hu0 ⊢ + have hvV : L.mem s (wa u 3672#u64) ∈ Z.V := (J.vcRun.run u hu).resolve_left hu0 + rw [hVcEq _ hvV 24#u64 (by decide) (by decide)] + exact R6.runBack u hu hu0 + exact ⟨σ', ⟨hsts, hkh', hKhOther, hcur, hms, hstepW, hSpecS, hSSz, hQk, bufAl', ent', sq', poolOff', sr', + scCov', ep', ntfn', reply', bufCover', idleNoFault', mcp', yieldRel', sinv', sidle', runVcpu', vr'', blkQ', + noSwitch', j5', j7', hSV', hR6', by rw [hms]; exact hCl, kb, hc, osk, w2⟩⟩ + +/-- Setting a runnable TCB (with cleared `RUN_VCPU`) to `TS_RUNNING` refines +`setThreadState (specPtr t) .running` on `JStage3TA` without changing `Z`. -/ +theorem set_thread_state_running_stage3TA + {AR : A → HwRootA → Prop} {Clock : A → Fl4SpecA64.MachineState → Prop} + {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J7 : JSlotA S} + [J7PpA st J7] [J7PpKA st J7] [J7FrA st J7] + {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + (hSV : SchedValidA σ) + (R6 : VcpuRec6A st Z OS Bf s σ) + (hCl : Clock a σ.machineState) + {t : U64} (ht : t ∈ Z.T) {x : Fl4SpecA64.Tcb} + (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hrun : runnable x.tcbState = true) + (hrv0 : L.mem s (runVcpuA t) = 0#u64) : + sched.set_thread_state st s t sched.TS_RUNNING ⦃ s' => + ∃ σ' : Fl4SpecA64.State Unit, + setThreadState (Z := Unit) (specPtr t) .running σ = ({((), σ')}, False) ∧ + σ'.kheap = (updKSc σ (specPtr t) (.tcb { x with tcbState := .running })).kheap ∧ + JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s' a σ' m ∧ + SchedValidA σ' ∧ + VcpuRec6A st Z OS Bf s' σ' ∧ + Clock a σ'.machineState ∧ + σ'.machineState = σ.machineState ∧ + L.ksb s' = L.ksb s ∧ L.cpu s' = L.cpu s ∧ + OSKeep st OS s s' ∧ + WritesIn st s s' (fun b => b = stA t ∨ b = L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) ∧ + FrKeepA st OS s σ s' σ' ⦄ := by + refine WP.spec_mono (set_thread_state_running_coreA J.toVcpuCoreA hSV R6 hCl ht hk hrun hrv0 + (fun w => J.osKeep_stepA w) (fun {_ _} hp {_} h1 h2 => J.heap_offNodesPA hp h1 h2) + (fun {_} h1 h2 => J.asid_offStepA h1 h2) (fun {_} hb ha => J.vsr_offStepA J.vsOk hb ha) + (fun w => J.pp_vsrA w)) ?_ + rintro s' ⟨σ', o⟩ + have sact' : SActA σ' := + { f1 := fun u hu => absurd hu (o.noSwitch' u) + f2 := by + obtain ⟨y, hy, hs⟩ := J.sact.f2 + rw [o.hcur] + by_cases e : σ.curThread = specPtr t + · rw [e, hk] at hy; cases hy + exact ⟨{ x with tcbState := .running }, by rw [e, o.hkh', updKSc_same], Or.inl rfl⟩ + · exact ⟨y, by rw [o.hKhOther _ e]; exact hy, hs⟩ } + have J' := JStage3TA.of_schedWr (Q := Z.Q) (Rl := Z.R) (curOf := Z.curOf) (curScOf := Z.curScOf) + J o.hstepW o.hSpecS o.hSSz o.hQk (by rw [o.hms]) J.j.1.ar (fun _ _ => rfl) o.bufAl' o.ent' o.sq' o.poolOff' o.sr' o.scCov' + o.ep' o.ntfn' o.reply' o.bufCover' o.idleNoFault' o.mcp' o.yieldRel' o.sinv' o.sidle' o.runVcpu' o.vr'' o.blkQ' + o.noSwitch' sact' o.j5' o.j7' + exact ⟨σ', o.hsts, o.hkh', J', o.hSV', o.hR6', o.hCl', o.hms, o.kb, o.hc, o.osk, o.w2, + FrKeepA.of_off (fun u hu => by unfold capA; rw [(o.osk u hu).1, (o.osk u hu).2.1]) + (fun b al lv => by + obtain ⟨-, nt, p, k, hpH, lo, hi⟩ := J.liveOffKerA al lv + refine o.w2.mem_eq al fun h => ?_ + rcases h with h | h + · exact nt t ht 3328#u64 (by decide) (h.trans (stA_eq t)) + · have hcpu_lt : (L.cpu s).val < 128 := by + obtain ⟨nn, -, hkn, pcs⟩ := J.sr.toSchedSRN.csN + have := (pcs (L.cpu s)).1 J.sr.sch.cpuCs; omega + have hksFit : (L.ksb s).val + 788736 ≤ 2 ^ 64 := J.ent.ksw + have hnode : (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION).val = + (L.ksb s).val + 223488 + (L.cpu s).val * 4288 + 16 := by + have h16 : (layout.node.SCHED_ACTION : U64).val = 16 := by unfold layout.node.SCHED_ACTION; rfl + have hna := nodeAt_val (J.sq.nodes _ J.sr.sch.cpuCs) (by rw [h16]; omega) + rw [hna, h16]; rfl + have hko := J.heap_offNodesPA hpH (b := b) lo hi + rw [h] at hko; exact hko ⟨by omega, by omega⟩) + (by rw [o.hms])⟩ + +/-- Setting a runnable TCB (with cleared `RUN_VCPU`) to `TS_RUNNING` refines +`setThreadState (specPtr t) .running` on `JStage3A` without requiring `SActA σ` +(valid even when `curThread` is in `.ext .runningVM`). -/ +theorem set_thread_state_running_stage3A + {AR : A → HwRootA → Prop} {Clock : A → Fl4SpecA64.MachineState → Prop} {nA : U64} + {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} + {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (hNoSw : ∀ u, σ.schedulerAction ≠ .switchThread u) + (hCl : Clock a σ.machineState) + {t : U64} (ht : t ∈ Z.T) {x : Fl4SpecA64.Tcb} + (hk : σ.kheap (specPtr t) = some (.tcb x)) + (hrun : runnable x.tcbState = true) + (hrv0 : L.mem s (runVcpuA t) = 0#u64) : + sched.set_thread_state st s t sched.TS_RUNNING ⦃ s' => + ∃ σ' : Fl4SpecA64.State Unit, + setThreadState (Z := Unit) (specPtr t) .running σ = ({((), σ')}, False) ∧ + σ'.kheap = (updKSc σ (specPtr t) (.tcb { x with tcbState := .running })).kheap ∧ + JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s' a σ' m ∧ + (∀ u, σ'.schedulerAction ≠ .switchThread u) ∧ + Clock a σ'.machineState ∧ + σ'.machineState = σ.machineState ∧ + L.ksb s' = L.ksb s ∧ L.cpu s' = L.cpu s ∧ + OSKeep st OS s s' ∧ + WritesIn st s s' (fun b => b = stA t ∨ b = L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) ⦄ := by + obtain ⟨Pt, Ub, Pp', Tb', Hr, Te, Kt, Lv', Re, bits, KLv, W⟩ := J7A.unp J.j7 + refine WP.spec_mono (set_thread_state_running_coreA (J.toVcpuCoreA hNoSw) J.schedValid (j3aVcpuRec6A J) hCl ht hk hrun hrv0 + (fun w => J.osKeep_stepA w) + (fun {p k} hp {_} h1 h2 ⟨n1, n2⟩ => by + by_cases ho : ∃ z, k = HKind.other z + · obtain ⟨z, rfl⟩ := ho; simp only [HKind.bits] at h2 + rcases J.ko.2 p z hp with h | h | h | h <;> omega + · have := J.ko.1 p k hp (fun z e => ho ⟨z, e⟩); omega) + (fun {_} h1 h2 => J.asid_offStepA h1 h2) + (fun {_} hb ha => J.vsr_offStepA J.vsOk hb ha) + (fun w Pt Ub vr b hb => w.mem_eq (vr.al b hb) (by + by_cases ha : AsW (L.ksb s) b + · obtain ⟨o, o1, o2, -, e⟩ := ha + have hfit := J.sys.ent.ksw + have ev : b.val = (L.ksb s).val + o.val := by rw [e, wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact J.asid_offStepA (by omega) (by omega) + · exact J.vsr_offStepA vr hb ha))) ?_ + rintro s' ⟨σ', o⟩ + have htH : t ∈ H.tcbs := by rw [J.tcbs]; exact ht + have wv : WritesIn st s s' (VW st H.tcbs Z.Cs Z.V {b | b ∈ Ub ∧ FrBw H OS b ∧ FrCw st OS s b} (L.ksb s) (fun y => y = t)) := + o.w2.mono fun b hb => by + rcases hb with rfl | rfl + · exact Or.inr (Or.inr (Or.inl ⟨t, htH, Or.inr (Or.inr (Or.inr ⟨rfl, rfl⟩))⟩)) + · exact Or.inr (Or.inr (Or.inr ⟨L.cpu s, J.sr.sch.cpuCs, layout.node.SCHED_ACTION, + by unfold layout.node.SCHED_ACTION; decide, by unfold layout.node.SCHED_ACTION; decide, s, rfl, rfl⟩)) + have hi : L.idleW s' = L.idleW s := by + unfold LawfulStore.idleW + have h8 : layout.node.IDLE_THREAD.val = 8 := by unfold layout.node.IDLE_THREAD; rfl + have h16 : layout.node.SCHED_ACTION.val = 16 := by unfold layout.node.SCHED_ACTION; rfl + have hna8 := nodeAt_val (J.sq.nodes _ J.sr.sch.cpuCs) (by rw [h8]; decide) + have hna16 := nodeAt_val (J.sq.nodes _ J.sr.sch.cpuCs) (by rw [h16]; decide) + unfold LawfulStore.nodeBase at hna8 hna16 + have hal : Aligned (L.nodeAt s (L.cpu s) layout.node.IDLE_THREAD) := + nodeAt_aligned (J.sq.nodes _ J.sr.sch.cpuCs) (by rw [h8]; decide) (by rw [h8]) + rw [o.hc, nodeAt_of_ksb o.kb] + refine o.w2.mem_eq hal fun h => ?_ + rcases h with h | h + · have hft := J.sys.ent.heap.fits t .tcb htH; change t.val + 4096 ≤ 2 ^ 64 at hft + have hstV : (stA t).val = t.val + 3328 := by + rw [stA_eq, wa_valm]; change (t.val + 3328) % _ = _; exact Nat.mod_eq_of_lt (by omega) + have hkt := J.sr.tcb_ks ht; unfold ksSizeA at hkt + have hcpu128 : (L.cpu s).val < 128 := by + obtain ⟨nn, -, hkn, pcs⟩ := J.sr.toSchedSRN.csN; have := (pcs _).1 J.sr.sch.cpuCs; omega + have hfit := J.sys.ent.ksw + have ev := congrArg UScalar.val h; rw [hna8, h8, hstV] at ev; omega + · have ev := congrArg UScalar.val h; rw [hna8, hna16, h8, h16] at ev; omega + have hc_st : ∀ y, y = t → L.mem s (stA y) ≠ sched.TS_BLOCKED_ON_REPLY ∧ L.mem s (stA y) ≠ sched.TS_INACTIVE := by + intro y hy; rw [hy] + have hRunC : Runnable (L.mem s (stA t)) := (runnable_iffA (J.ep.at_tcb ht hk).st).2 hrun + refine ⟨fun h => ?_, fun h => ?_⟩ <;> + rcases hRunC with h' | h' | h' <;> rw [h'] at h <;> + (have hv := congrArg UScalar.val h; simp [sched.TS_RUNNING, sched.TS_RESTART, sched.TS_RUNNING_VM, + sched.TS_BLOCKED_ON_REPLY, sched.TS_INACTIVE] at hv) + obtain ⟨sysl', symL'⟩ := sysL_symL_of_vwA J W wv hi hc_st + have J' := JStage3A.of_schedWr (Q := Z.Q) (Rl := Z.R) (curOf := Z.curOf) (curScOf := Z.curScOf) + J o.hstepW o.hSpecS o.hSSz o.hQk (by rw [o.hms]) J.j.1.ar (fun _ _ => rfl) o.bufAl' ⟨o.ent', sysl'⟩ o.sq' + o.poolOff' o.sr' o.scCov' o.ep' o.ntfn' o.reply' o.bufCover' o.idleNoFault' o.mcp' o.yieldRel' o.sinv' o.sidle' + o.runVcpu' o.blkQ' symL' o.hSV' o.j5' o.j7' + exact ⟨σ', o.hsts, o.hkh', J', o.noSwitch', o.hCl', o.hms, o.kb, o.hc, o.osk, o.w2⟩ + +/-! ## Section 1b: Reusable `JStage3TA` micro-steps (clear run-link, single TCB reg write, dissoc, vm_root) -/ + +set_option maxHeartbeats 800000 in +/-- Clear `vc + 24 := 0` (`RUN_CALLER`) and `ct + 3672 := 0` (`RUN_VCPU`) in lockstep with +`fl4VcpuExtUpdate (specPtr vc) (runCaller := none) >> setRunVcpu (specPtr ct) none`. -/ +private theorem vcpu_clear_run_pair_coreA + {AR : A → HwRootA → Prop} {Clock : A → Fl4SpecA64.MachineState → Prop} + {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J7 : JSlotA S} + [J7PpA st J7] [J7PpKA st J7] [J7FrA st J7] + {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3VcpuCoreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + (hSV : SchedValidA σ) + (R6 : VcpuRec6A st Z OS Bf s σ) + (hCl : Clock a σ.machineState) + {vc ct : U64} (hvc : vc ∈ Z.V) (hct : ct ∈ Z.T) + (hlink : L.mem s (wa vc 24#u64) = ct) (hback : L.mem s (runVcpuA ct) = vc) + {xv : Fl4SpecA64.Vcpu} {xt : Fl4SpecA64.Tcb} + (hkv : σ.kheap (specPtr vc) = some (.archObj (.vcpu xv))) + (hkt : σ.kheap (specPtr ct) = some (.tcb xt)) : + let s1 := L.wr s (wa vc 24#u64) 0#u64 + let s2 := L.wr s1 (runVcpuA ct) 0#u64 + let σ' := updKSc (updKSc σ (specPtr vc) (.archObj (.vcpu { xv with vcpuExt := { xv.vcpuExt with runCaller := none } }))) + (specPtr ct) (.tcb { xt with tcbExt := { xt.tcbExt with tcbRunVcpu := none } }) + JStage3VcpuCoreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s2 a σ' m ∧ + VcpuSpecStepA H σ σ' ∧ + WritesIn st s s2 (VcpuWrNoStA st Z OS s) ∧ + SchedValidA σ' ∧ + VcpuRec6A st Z OS Bf s2 σ' ∧ + Clock a σ'.machineState ∧ + L.mem s2 (wa vc 24#u64) = 0#u64 ∧ + L.mem s2 (runVcpuA ct) = 0#u64 ∧ + (∀ v ∈ Z.V, L.mem s2 v = L.mem s v) ∧ + (∀ u ∈ Z.T, L.mem s2 (wa u 3664#u64) = L.mem s (wa u 3664#u64)) ∧ + (∀ u ∈ Z.T, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → (u ≠ ct ∨ o ≠ 3672#u64) → L.mem s2 (wa u o) = L.mem s (wa u o)) ∧ + L.ksb s2 = L.ksb s ∧ L.cpu s2 = L.cpu s ∧ + OSKeep st OS s s2 ∧ + FrKeepA st OS s σ s2 σ' := by + dsimp only + set s1 := L.wr s (wa vc 24#u64) 0#u64 + set s2 := L.wr s1 (runVcpuA ct) 0#u64 + set σ' := updKSc (updKSc σ (specPtr vc) (.archObj (.vcpu { xv with vcpuExt := { xv.vcpuExt with runCaller := none } }))) + (specPtr ct) (.tcb { xt with tcbExt := { xt.tcbExt with tcbRunVcpu := none } }) + obtain ⟨_, _, -, arm, -, -, -, -, -, -, -⟩ := J7FrA.fr (st := st) _ _ _ _ _ _ J.j7 + have hvH : H vc = some (.other 12) := arm.vh vc hvc + have htH : H ct = some .tcb := show ct ∈ H.tcbs by rw [J.tcbs]; exact hct + have hne_spec : specPtr ct ≠ specPtr vc := fun e => by rw [e, hkv] at hkt; cases hkt + have al_tcb : ∀ u ∈ Z.T, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → Aligned (wa u o) := fun u hu o h1 h8 => + field_aligned (J.sr.core.tcbs u hu) h1 h8 + have al_vc : ∀ v ∈ Z.V, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → Aligned (wa v o) := fun v hv o h1 h8 => by + have hv' := arm.vh v hv + have hf := J.ent.heap.fits v _ hv'; have ha := J.ent.heap.align v _ hv' + simp only [HKind.bits] at hf ha + have ev : (wa v o).val = v.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + show _ % 8 = 0; omega + have hal_vc24 : Aligned (wa vc 24#u64) := al_vc vc hvc 24#u64 (by decide) (by decide) + have hal_rt : Aligned (runVcpuA ct) := al_tcb ct hct 3672#u64 (by decide) (by decide) + have hdisj_vc_t : ∀ v ∈ Z.V, ∀ o : U64, o.val < 4096 → wa v o ≠ runVcpuA ct := fun v hv o ho e => by + have hv' := arm.vh v hv + have hfv := J.ent.heap.fits v _ hv'; have hft := J.ent.heap.fits ct .tcb htH + simp only [HKind.bits] at hfv hft + have hne : v ≠ ct := fun eq => by rw [eq, htH] at hv'; cases hv' + have hd := J.ent.heap.disj v (.other 12) ct .tcb hv' htH hne + change v.val + 4096 ≤ ct.val ∨ ct.val + 4096 ≤ v.val at hd + have ev1 : (wa v o).val = v.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have ev2 : (runVcpuA ct).val = ct.val + 3672 := by + unfold runVcpuA; rw [wa_valm]; change (ct.val + 3672) % _ = _; exact Nat.mod_eq_of_lt (by omega) + have ev := congrArg UScalar.val e; rw [ev1, ev2] at ev; omega + have hdisj_tcb_vc : ∀ u ∈ Z.T, ∀ o : U64, o.val < 4096 → wa u o ≠ wa vc 24#u64 := fun u hu o ho e => by + have huH : H u = some .tcb := show u ∈ H.tcbs by rw [J.tcbs]; exact hu + have hfu := J.ent.heap.fits u .tcb huH; have hfv := J.ent.heap.fits vc _ hvH + simp only [HKind.bits] at hfu hfv + have hne : u ≠ vc := fun eq => by rw [eq, hvH] at huH; cases huH + have hd := J.ent.heap.disj u .tcb vc (.other 12) huH hvH hne + change u.val + 4096 ≤ vc.val ∨ vc.val + 4096 ≤ u.val at hd + have ev1 : (wa u o).val = u.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have ev2 : (wa vc 24#u64).val = vc.val + 24 := by + rw [wa_valm]; change (vc.val + 24) % _ = _; exact Nat.mod_eq_of_lt (by omega) + have ev := congrArg UScalar.val e; rw [ev1, ev2] at ev; omega + have hdisj_tcb_t : ∀ u ∈ Z.T, ∀ o : U64, o.val < 4096 → (u ≠ ct ∨ o ≠ 3672#u64) → wa u o ≠ runVcpuA ct := fun u hu o ho hne e => by + have huH : H u = some .tcb := show u ∈ H.tcbs by rw [J.tcbs]; exact hu + have hfu := J.ent.heap.fits u .tcb huH; have hft := J.ent.heap.fits ct .tcb htH + simp only [HKind.bits] at hfu hft + have ev1 : (wa u o).val = u.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have ev2 : (runVcpuA ct).val = ct.val + 3672 := by + unfold runVcpuA; rw [wa_valm]; change (ct.val + 3672) % _ = _; exact Nat.mod_eq_of_lt (by omega) + have ev := congrArg UScalar.val e; rw [ev1, ev2] at ev + rcases hne with eut | eo + · have hd := J.ent.heap.disj u .tcb ct .tcb huH htH eut + change u.val + 4096 ≤ ct.val ∨ ct.val + 4096 ≤ u.val at hd; omega + · by_cases eut : u = ct + · subst eut; exact eo (UScalar.eq_of_val_eq (by change o.val = 3672; omega)) + · have hd := J.ent.heap.disj u .tcb ct .tcb huH htH eut + change u.val + 4096 ≤ ct.val ∨ ct.val + 4096 ≤ u.val at hd; omega + have hdisj_vc_vc : ∀ v ∈ Z.V, ∀ o : U64, o.val < 4096 → (v ≠ vc ∨ o ≠ 24#u64) → wa v o ≠ wa vc 24#u64 := fun v hv o ho hne e => by + have hv' := arm.vh v hv + have hfv := J.ent.heap.fits v _ hv'; have hfvc := J.ent.heap.fits vc _ hvH + simp only [HKind.bits] at hfv hfvc + have ev1 : (wa v o).val = v.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have ev2 : (wa vc 24#u64).val = vc.val + 24 := by + rw [wa_valm]; change (vc.val + 24) % _ = _; exact Nat.mod_eq_of_lt (by omega) + have ev := congrArg UScalar.val e; rw [ev1, ev2] at ev + rcases hne with evc | eo + · have hd := J.ent.heap.disj v (.other 12) vc (.other 12) hv' hvH evc + change v.val + 4096 ≤ vc.val ∨ vc.val + 4096 ≤ v.val at hd; omega + · by_cases evc : v = vc + · subst evc; exact eo (UScalar.eq_of_val_eq (by change o.val = 24; omega)) + · have hd := J.ent.heap.disj v (.other 12) vc (.other 12) hv' hvH evc + change v.val + 4096 ≤ vc.val ∨ vc.val + 4096 ≤ v.val at hd; omega + have hmem_other : ∀ b, Aligned b → b ≠ wa vc 24#u64 → b ≠ runVcpuA ct → L.mem s2 b = L.mem s b := fun b hb h1 h2 => by + rw [L.mem_wr_ne hal_rt hb (fun e => h2 (UScalar.eq_of_val_eq e).symm), + L.mem_wr_ne hal_vc24 hb (fun e => h1 (UScalar.eq_of_val_eq e).symm)] + have hmem_rt : L.mem s2 (runVcpuA ct) = 0#u64 := L.mem_wr_same hal_rt + have hmem_vc24 : L.mem s2 (wa vc 24#u64) = 0#u64 := by + rw [L.mem_wr_ne hal_rt hal_vc24 (fun e => (hdisj_vc_t vc hvc 24#u64 (by decide)) (UScalar.eq_of_val_eq e).symm)] + exact L.mem_wr_same hal_vc24 + have hw1 : WritesIn st s s1 (VcpuWrNoStA st Z OS s) := + (writesIn_wr hal_vc24).mono (fun _ e => by subst e; exact Or.inl ⟨vc, hvc, Or.inr (Or.inr rfl)⟩) + have hw2 : WritesIn st s1 s2 (VcpuWrNoStA st Z OS s) := + (writesIn_wr hal_rt).mono (fun _ e => by + subst e; exact Or.inr (Or.inl ⟨ct, hct, 3672#u64, by decide, Or.inr (Or.inr rfl), rfl⟩)) + have hw : WritesIn st s s2 (VcpuWrNoStA st Z OS s) := (hw1.trans hw2).mono (fun _ h => h.elim id id) + have hHS : VcpuSpecStepA H σ σ' := by + refine ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, ?_, fun _ h => absurd rfl h⟩ + intro p + by_cases ept : p = specPtr ct + · subst ept + exact Or.inr (Or.inl ⟨xt, { xt with tcbExt := { xt.tcbExt with tcbRunVcpu := none } }, hkt, + by simp [σ', updKSc, Function.update], + ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, fun _ => by first | exact id | exact fun _ => rfl⟩⟩) + · by_cases epv : p = specPtr vc + · subst epv + exact Or.inr (Or.inr ⟨xv, { xv with vcpuExt := { xv.vcpuExt with runCaller := none } }, hkv, + by simp [σ', updKSc, Function.update, ept], rfl, rfl, rfl, rfl, rfl, rfl⟩) + · exact Or.inl (by simp [σ', updKSc, Function.update, ept, epv]) + have osk := osKeep_of_vcpuWrCoreA J R6.vcOS R6.frOff hw + have capE : ∀ y ∈ OS, capA st s2 y = capA st s y := fun y hy => by + unfold capA; rw [(osk y hy).1, (osk y hy).2.1] + have hVcCore : VcpuCoreA (L := L) Z s2 σ' := by + refine ⟨fun v hv => ?_, fun p xv' hxp => ?_, fun u hu => ?_, fun y hy p hc => ?_⟩ + · obtain ⟨xv0, hkv0, hvt0, hrc0⟩ := J.j.2.1.obj v hv + have hal0 : Aligned v := by have := al_vc v hv 0#u64 (by decide) (by decide); rwa [wa_zeroH] at this + have hm0 : L.mem s2 v = L.mem s v := by + have h1 := hdisj_vc_vc v hv 0#u64 (by decide) (Or.inr (by decide)) + have h2 := hdisj_vc_t v hv 0#u64 (by decide) + rw [wa_zeroH] at h1 h2; exact hmem_other v hal0 h1 h2 + by_cases ev : v = vc + · subst ev; rw [hkv] at hkv0; cases hkv0 + exact ⟨{ xv with vcpuExt := { xv.vcpuExt with runCaller := none } }, + by simp [σ', updKSc, Function.update, hne_spec.symm], by rw [hm0]; exact hvt0, by rw [hmem_vc24]; rfl⟩ + · have hne_s1 : specPtr v ≠ specPtr ct := fun e => by + have : v = ct := specPtr_inj e; have hv' := arm.vh v hv; rw [this, htH] at hv'; cases hv' + have hne_s2 : specPtr v ≠ specPtr vc := fun e => ev (specPtr_inj e) + have hm24 : L.mem s2 (wa v 24#u64) = L.mem s (wa v 24#u64) := + hmem_other _ (al_vc v hv 24#u64 (by decide) (by decide)) + (hdisj_vc_vc v hv 24#u64 (by decide) (Or.inl ev)) (hdisj_vc_t v hv 24#u64 (by decide)) + exact ⟨xv0, by simp [σ', updKSc, Function.update, hne_s1, hne_s2, hkv0], by rw [hm0]; exact hvt0, by rw [hm24]; exact hrc0⟩ + · by_cases ept : p = specPtr ct + · subst ept; simp [σ', updKSc, Function.update] at hxp + · by_cases epv : p = specPtr vc + · subst epv; exact ⟨vc, hvc, rfl⟩ + · exact J.j.2.1.back p xv' (by simpa [σ', updKSc, Function.update, ept, epv] using hxp) + · obtain ⟨xt0, hkt0, h1, h2⟩ := J.j.2.1.tcb u hu + have hm3664 : L.mem s2 (wa u 3664#u64) = L.mem s (wa u 3664#u64) := + hmem_other _ (al_tcb u hu 3664#u64 (by decide) (by decide)) + (hdisj_tcb_vc u hu 3664#u64 (by decide)) (hdisj_tcb_t u hu 3664#u64 (by decide) (Or.inr (by decide))) + by_cases eu : u = ct + · subst eu; rw [hkt] at hkt0; cases hkt0 + exact ⟨{ xt with tcbExt := { xt.tcbExt with tcbRunVcpu := none } }, + by simp [σ', updKSc, Function.update], by rw [hm3664]; exact h1, by rw [hmem_rt]; rfl⟩ + · have hne_s1 : specPtr u ≠ specPtr ct := fun e => eu (specPtr_inj e) + have huH : H u = some .tcb := show u ∈ H.tcbs by rw [J.tcbs]; exact hu + have hne_s2 : specPtr u ≠ specPtr vc := fun e => by + have : u = vc := specPtr_inj e; rw [this, hvH] at huH; cases huH + have hm3672 : L.mem s2 (runVcpuA u) = L.mem s (runVcpuA u) := + hmem_other _ (al_tcb u hu 3672#u64 (by decide) (by decide)) + (hdisj_tcb_vc u hu 3672#u64 (by decide)) (hdisj_tcb_t u hu 3672#u64 (by decide) (Or.inl eu)) + exact ⟨xt0, by simp [σ', updKSc, Function.update, hne_s1, hne_s2, hkt0], by rw [hm3664]; exact h1, by rw [hm3672]; exact h2⟩ + · have hyOS : y ∈ OS := J.j5.2.1 y hy + unfold capAtA at hc; rw [(osk y hyOS).1, (osk y hyOS).2.1] at hc + exact J.j.2.1.cap y hy p hc + have hVcRun : VcRunA st Z.T Z.V s2 := by + refine ⟨fun u hu => ?_, fun v hv => ?_, fun v hv => ?_, fun u hu => ?_, fun u hu h => ?_⟩ + · rw [hmem_other _ (al_tcb u hu 3664#u64 (by decide) (by decide)) + (hdisj_tcb_vc u hu 3664#u64 (by decide)) (hdisj_tcb_t u hu 3664#u64 (by decide) (Or.inr (by decide)))] + exact J.vcRun.tcb u hu + · by_cases ev : v = vc + · subst ev; exact Or.inl hmem_vc24 + · rw [hmem_other _ (al_vc v hv 24#u64 (by decide) (by decide)) + (hdisj_vc_vc v hv 24#u64 (by decide) (Or.inl ev)) (hdisj_vc_t v hv 24#u64 (by decide))] + exact J.vcRun.vcpu v hv + · have hal0 : Aligned v := by have := al_vc v hv 0#u64 (by decide) (by decide); rwa [wa_zeroH] at this + have h1 := hdisj_vc_vc v hv 0#u64 (by decide) (Or.inr (by decide)) + have h2 := hdisj_vc_t v hv 0#u64 (by decide) + rw [wa_zeroH] at h1 h2; rw [hmem_other v hal0 h1 h2]; exact J.vcRun.vtcb v hv + · by_cases eu : u = ct + · subst eu; exact Or.inl hmem_rt + · unfold runVcpuA + rw [hmem_other _ (al_tcb u hu 3672#u64 (by decide) (by decide)) + (hdisj_tcb_vc u hu 3672#u64 (by decide)) (hdisj_tcb_t u hu 3672#u64 (by decide) (Or.inl eu))] + exact J.vcRun.run u hu + · by_cases eu : u = ct + · subst eu; exact hmem_rt + · rw [hmem_other _ (al_tcb u hu 3664#u64 (by decide) (by decide)) + (hdisj_tcb_vc u hu 3664#u64 (by decide)) (hdisj_tcb_t u hu 3664#u64 (by decide) (Or.inr (by decide)))] at h + unfold runVcpuA + rw [hmem_other _ (al_tcb u hu 3672#u64 (by decide) (by decide)) + (hdisj_tcb_vc u hu 3672#u64 (by decide)) (hdisj_tcb_t u hu 3672#u64 (by decide) (Or.inl eu))] + exact J.vcRun.disj u hu h + have hRunVcpu : RunVcpuInv st Z.T s2 := fun u hu => by + have hst_eq : L.mem s2 (stA u) = L.mem s (stA u) := by + rw [stA_eq] + exact hmem_other _ (al_tcb u hu 3328#u64 (by decide) (by decide)) + (hdisj_tcb_vc u hu 3328#u64 (by decide)) (hdisj_tcb_t u hu 3328#u64 (by decide) (Or.inr (by decide))) + by_cases eu : u = ct + · subst eu; intro h; exact absurd hmem_rt h + · unfold runVcpuA + rw [hst_eq, hmem_other _ (al_tcb u hu 3672#u64 (by decide) (by decide)) + (hdisj_tcb_vc u hu 3672#u64 (by decide)) (hdisj_tcb_t u hu 3672#u64 (by decide) (Or.inl eu))] + exact J.runVcpu u hu + have hEpTcb : ∀ u ∈ Z.T, ∃ x, σ'.kheap (specPtr u) = some (.tcb x) ∧ TcbRelE Rp (L.mem s2) u x := fun u hu => by + obtain ⟨x0, hkx0, hrel0⟩ := J.ep.tcb u hu + have huH : H u = some .tcb := show u ∈ H.tcbs by rw [J.tcbs]; exact hu + have hfu := J.ent.heap.fits u .tcb huH; change u.val + 4096 ≤ 2 ^ 64 at hfu + have hrel_s2 : TcbRelE Rp (L.mem s2) u x0 := by + refine hrel0.frame (J.sq.tcbs u huH) fun b hb hbu => ?_ + have hlt : b.val - u.val < 3664 := by have := hbu.2; unfold TOffN at this; omega + have hb_eq : b = wa u ⟨BitVec.ofNat 64 (b.val - u.val)⟩ := by + apply UScalar.eq_of_val_eq; rw [wa_valm] + have hdv : (⟨BitVec.ofNat 64 (b.val - u.val)⟩ : U64).val = b.val - u.val := by + show (BitVec.ofNat 64 _).toNat = _; rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + rw [hdv, Nat.add_sub_of_le hbu.1]; exact (Nat.mod_eq_of_lt (by omega)).symm + have hdv : (⟨BitVec.ofNat 64 (b.val - u.val)⟩ : U64).val = b.val - u.val := by + show (BitVec.ofNat 64 _).toNat = _; rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + rw [hb_eq] + exact hmem_other _ (by rw [← hb_eq]; exact hb) (hdisj_tcb_vc u hu _ (by omega)) + (hdisj_tcb_t u hu _ (by omega) (Or.inr (by intro e; have := congrArg UScalar.val e; rw [hdv] at this; change b.val - u.val = 3672 at this; omega))) + by_cases eu : u = ct + · rcases eu with rfl; rw [hkt] at hkx0; cases hkx0 + exact ⟨{ xt with tcbExt := { xt.tcbExt with tcbRunVcpu := none } }, by simp [σ', updKSc, Function.update], + ⟨hrel_s2.st, hrel_s2.blk, hrel_s2.send, hrel_s2.recv, hrel_s2.reply, hrel_s2.regs, hrel_s2.prio, + hrel_s2.fault0, hrel_s2.faultT, hrel_s2.bound, hrel_s2.faultR⟩⟩ + · have hne_s1 : specPtr u ≠ specPtr ct := fun e => eu (specPtr_inj e) + have hne_s2 : specPtr u ≠ specPtr vc := fun e => by + have : u = vc := specPtr_inj e; rw [this, hvH] at huH; cases huH + exact ⟨x0, by simp [σ', updKSc, Function.update, hne_s1, hne_s2, hkx0], hrel_s2⟩ + have hEpBuf : ∀ bp ∈ Bf, ∀ k : U64, k.val < 128 → + (L.mem s2 (bufW bp.1 k)).bv = loadWordAt σ'.machineState.underlyingMemory (bufAddr bp.2 k.val) ∧ + (bufAddr bp.2 k.val).toNat % 8 = 0 := fun bp hbp k hk => by + have ⟨h1, h2⟩ := J.ep.buf bp hbp k hk + have hbw : BufW Bf (bufW bp.1 k) := ⟨bp, hbp, k, hk, rfl⟩ + have hmem_bw : L.mem s2 (bufW bp.1 k) = L.mem s (bufW bp.1 k) := by + refine hmem_other _ (J.iplaced.e.balign _ hbw) (J.bufOff.vcw _ hbw vc hvc 24#u64 (by decide) (by decide)) ?_ + intro e + have hft := J.ent.heap.fits ct .tcb htH; change ct.val + 4096 ≤ 2 ^ 64 at hft + have ev : (runVcpuA ct).val = ct.val + 3672 := by + unfold runVcpuA; rw [wa_valm]; change (ct.val + 3672) % _ = _; exact Nat.mod_eq_of_lt (by omega) + have hbt := J.iplaced.e.bt _ hbw ct hct; rw [e, ev] at hbt; omega + rw [hmem_bw]; exact ⟨h1, h2⟩ + have hDeadVcpu : ∀ v ∈ Z.V, DeadA st OS .vcpu s2 v → ∀ x, σ'.kheap (specPtr v) = some (.archObj (.vcpu x)) → + x.vcpuTcb = none := fun v hv hd x hx => by + have hd0 : DeadA st OS .vcpu s v := fun y hy hc => hd y hy (by rw [capE y hy]; exact hc) + by_cases ev : v = vc + · rcases ev with rfl; simp [σ', updKSc, Function.update, hne_spec.symm] at hx; subst hx + exact J.deadExt.vcpu v hvc hd0 xv hkv + · have hne_s1 : specPtr v ≠ specPtr ct := fun e => by + have : v = ct := specPtr_inj e; have hv' := arm.vh v hv; rw [this, htH] at hv'; cases hv' + have hne_s2 : specPtr v ≠ specPtr vc := fun e => ev (specPtr_inj e) + simp [σ', updKSc, Function.update, hne_s1, hne_s2] at hx + exact J.deadExt.vcpu v hv hd0 x hx + have hDeadTvcpu : ∀ u ∈ Z.T, DeadA st OS .tcb s2 u → L.mem s2 (wa u 3664#u64) = 0#u64 := fun u hu hd => by + have hd0 : DeadA st OS .tcb s u := fun y hy hc => hd y hy (by rw [capE y hy]; exact hc) + rw [hmem_other _ (al_tcb u hu 3664#u64 (by decide) (by decide)) + (hdisj_tcb_vc u hu 3664#u64 (by decide)) (hdisj_tcb_t u hu 3664#u64 (by decide) (Or.inr (by decide)))] + exact J.deadExt.tvcpu u hu hd0 + have hDeadVcRun : ∀ v ∈ Z.V, DeadA st OS .vcpu s2 v → L.mem s2 (wa v 24#u64) = 0#u64 := fun v hv hd => by + by_cases ev : v = vc + · subst ev; exact hmem_vc24 + · have hd0 : DeadA st OS .vcpu s v := fun y hy hc => hd y hy (by rw [capE y hy]; exact hc) + rw [hmem_other _ (al_vc v hv 24#u64 (by decide) (by decide)) + (hdisj_vc_vc v hv 24#u64 (by decide) (Or.inl ev)) (hdisj_vc_t v hv 24#u64 (by decide))] + exact J.deadExt.vcDeadRun v hv hd0 + have hZombNoVc : ZombieTcbNoVcpuA st OS s2 := fun y hy u num hz => by + have hz0 : capA st s y = .Zombie u capability.ZOMBIE_TCB num := by rwa [capE y hy] at hz + have huT : u ∈ Z.T := J.tcbs ▸ J.zombieTcb y hy u num hz0 + rw [hmem_other _ (al_tcb u huT 3664#u64 (by decide) (by decide)) + (hdisj_tcb_vc u huT 3664#u64 (by decide)) (hdisj_tcb_t u huT 3664#u64 (by decide) (Or.inr (by decide)))] + exact J.zombieNoVcpu y hy u num hz0 + have hUtVs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → L.mem s2 b = L.mem s b := fun b hb => by + have hno := utVs_off_vcpuWrCoreA (regWr := false) J hb + exact hmem_other b (J.vsOk.al b hb) (fun e => hno (Or.inl ⟨vc, hvc, Or.inr (Or.inr e)⟩)) + (fun e => hno (Or.inr (Or.inl ⟨ct, hct, 3672#u64, by decide, Or.inr (Or.inr rfl), e⟩))) + have J' := JStage3VcpuCoreA.of_vcpuStepA J R6 hw hHS J.j.1.ar hUtVs hVcCore hVcRun hRunVcpu + hEpTcb hEpBuf hDeadVcpu hDeadTvcpu hDeadVcRun hZombNoVc + have hSV' := SchedValidA.of_vcpuStepA hSV hHS + have hSnap' := VcpuStateSnapA.of_vcpuStepCoreA J R6.snap hw hHS fun v hv => by + rw [hmem_other _ (al_vc v hv 8#u64 (by decide) (by decide)) + (hdisj_vc_vc v hv 8#u64 (by decide) (Or.inr (by decide))) (hdisj_vc_t v hv 8#u64 (by decide))] + obtain ⟨x, hkx, -⟩ := J.j.2.1.obj v hv + exact (R6.snap.snap v hv x hkx).2.2.2.2 + have hFrOff' : FrOff st Z.Cs OS (L.ksb s2) s2 := by + rw [hw.1]; intro u hu p0 sz0 r0 mp0 ept0 a0 va0 hc0 b0 hb0 + exact R6.frOff u hu p0 sz0 r0 mp0 ept0 a0 va0 ((capE u hu).symm.trans hc0) b0 hb0 + have hFrTh' : FrThA st Z.th OS s2 := R6.frTh.keepA capE fun _ _ e => e + have hUserFr' : UserFrSpecA st OS s2 σ' := + R6.userFr.keepA capE fun _ _ _ h => hHS.keepObj h (fun _ e => by cases e) (fun _ e => by cases e) + have hR6' : VcpuRec6A st Z OS Bf s2 σ' := by + refine ⟨hSnap', fun u hu => ?_, fun v hv => ?_, fun v hv hv0 => ?_, fun u hu hu0 => ?_, + R6.vcOS, hFrOff', hUserFr', hFrTh', R6.bfSpec⟩ + · rw [hmem_other _ (al_tcb u hu 3664#u64 (by decide) (by decide)) + (hdisj_tcb_vc u hu 3664#u64 (by decide)) (hdisj_tcb_t u hu 3664#u64 (by decide) (Or.inr (by decide)))] + intro hne + have hvV' := (J.vcRun.tcb u hu).resolve_left hne + have hal0 : Aligned (L.mem s (wa u 3664#u64)) := by + have := al_vc _ hvV' 0#u64 (by decide) (by decide); rwa [wa_zeroH] at this + have h1 := hdisj_vc_vc _ hvV' 0#u64 (by decide) (Or.inr (by decide)) + have h2 := hdisj_vc_t _ hvV' 0#u64 (by decide) + rw [wa_zeroH] at h1 h2; rw [hmem_other _ hal0 h1 h2]; exact R6.vcSym1 u hu hne + · by_cases ev : v = vc + · subst ev; intro h; exact absurd hmem_vc24 h + · rw [hmem_other _ (al_vc v hv 24#u64 (by decide) (by decide)) + (hdisj_vc_vc v hv 24#u64 (by decide) (Or.inl ev)) (hdisj_vc_t v hv 24#u64 (by decide))] + intro hne + have huT := (J.vcRun.vcpu v hv).resolve_left hne + have hne_ut : L.mem s (wa v 24#u64) ≠ ct := fun e => by + have := R6.vcSym2 v hv hne; rw [e, hback] at this; exact ev this.symm + unfold runVcpuA + rw [hmem_other _ (al_tcb _ huT 3672#u64 (by decide) (by decide)) + (hdisj_tcb_vc _ huT 3672#u64 (by decide)) (hdisj_tcb_t _ huT 3672#u64 (by decide) (Or.inl hne_ut))] + exact R6.vcSym2 v hv hne + · have hal0 : Aligned v := by have := al_vc v hv 0#u64 (by decide) (by decide); rwa [wa_zeroH] at this + have h1 := hdisj_vc_vc v hv 0#u64 (by decide) (Or.inr (by decide)) + have h2 := hdisj_vc_t v hv 0#u64 (by decide) + rw [wa_zeroH] at h1 h2; rw [hmem_other v hal0 h1 h2] at hv0 ⊢ + have huT := (J.vcRun.vtcb v hv).resolve_left hv0 + rw [hmem_other _ (al_tcb _ huT 3664#u64 (by decide) (by decide)) + (hdisj_tcb_vc _ huT 3664#u64 (by decide)) (hdisj_tcb_t _ huT 3664#u64 (by decide) (Or.inr (by decide)))] + exact R6.vcBack v hv hv0 + · by_cases eu : u = ct + · subst eu; exact absurd hmem_rt hu0 + · unfold runVcpuA at hu0 ⊢ + rw [hmem_other _ (al_tcb u hu 3672#u64 (by decide) (by decide)) + (hdisj_tcb_vc u hu 3672#u64 (by decide)) (hdisj_tcb_t u hu 3672#u64 (by decide) (Or.inl eu))] at hu0 ⊢ + have hvV' : L.mem s (wa u 3672#u64) ∈ Z.V := (J.vcRun.run u hu).resolve_left hu0 + have hne_v : L.mem s (wa u 3672#u64) ≠ vc := fun e => by + have := R6.runBack u hu hu0; rw [show runVcpuA u = wa u 3672#u64 from rfl, e, hlink] at this; exact eu this.symm + rw [hmem_other _ (al_vc _ hvV' 24#u64 (by decide) (by decide)) + (hdisj_vc_vc _ hvV' 24#u64 (by decide) (Or.inl hne_v)) (hdisj_vc_t _ hvV' 24#u64 (by decide))] + exact R6.runBack u hu hu0 + refine ⟨J', hHS, hw, hSV', hR6', hCl, hmem_vc24, hmem_rt, ?_, ?_, ?_, hw.1, hw.2.1, osk, + FrKeepA.kerCoreA J osk rfl fun b al nv nt => + hmem_other b al (fun e => nv ⟨vc, hvc, 24#u64, by decide, by decide, e⟩) (nt ct hct 3672#u64 (by decide))⟩ + · intro v hv + have hal0 : Aligned v := by have := al_vc v hv 0#u64 (by decide) (by decide); rwa [wa_zeroH] at this + have h1 := hdisj_vc_vc v hv 0#u64 (by decide) (Or.inr (by decide)) + have h2 := hdisj_vc_t v hv 0#u64 (by decide) + rw [wa_zeroH] at h1 h2; exact hmem_other v hal0 h1 h2 + · intro u hu + exact hmem_other _ (al_tcb u hu 3664#u64 (by decide) (by decide)) + (hdisj_tcb_vc u hu 3664#u64 (by decide)) (hdisj_tcb_t u hu 3664#u64 (by decide) (Or.inr (by decide))) + · intro u hu o ho1 ho8 hne + exact hmem_other _ (al_tcb u hu o ho1 ho8) (hdisj_tcb_vc u hu o ho1) (hdisj_tcb_t u hu o ho1 hne) + +set_option maxHeartbeats 800000 in +set_option maxRecDepth 2000 in +/-- Single TCB user-register write (`obj.Tcb.set_reg ct (regIdxA r) v`) in lockstep with +`asUser (specPtr ct) (setRegister r v.bv)` on `JStage3TA`. -/ +private theorem vcpu_tcb_reg_wr_coreA + {AR : A → HwRootA → Prop} {Clock : A → Fl4SpecA64.MachineState → Prop} + {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J7 : JSlotA S} + [J7PpA st J7] [J7PpKA st J7] [J7FrA st J7] + {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3VcpuCoreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + (hSV : SchedValidA σ) + (R6 : VcpuRec6A st Z OS Bf s σ) + (hCl : Clock a σ.machineState) + {ct : U64} (hct : ct ∈ Z.T) {x : Fl4SpecA64.Tcb} + (hkt : σ.kheap (specPtr ct) = some (.tcb x)) + (r : Register) (v : U64) : + let s' := L.wr s (Endp.regA ct (regIdxA r)) v + let uc := x.tcbArch.tcbContext + let x' := { x with tcbArch := { x.tcbArch with tcbContext := ⟨uc.userFpuState, Function.update uc.userRegs r v.bv⟩ } } + let σ' := updKSc σ (specPtr ct) (.tcb x') + asUser (Z := Unit) (specPtr ct) (setRegister r v.bv) σ = ({((), σ')}, False) ∧ + JStage3VcpuCoreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s' a σ' m ∧ + VcpuSpecStepA H σ σ' ∧ + WritesIn st s s' (VcpuWrNoStA st Z OS s) ∧ + SchedValidA σ' ∧ + VcpuRec6A st Z OS Bf s' σ' ∧ + Clock a σ'.machineState ∧ + (∀ vc ∈ Z.V, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → L.mem s' (wa vc o) = L.mem s (wa vc o)) ∧ + (∀ u ∈ Z.T, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → + (u ≠ ct ∨ o.val < 256 ∨ 3328 ≤ o.val) → L.mem s' (wa u o) = L.mem s (wa u o)) ∧ + L.ksb s' = L.ksb s ∧ L.cpu s' = L.cpu s ∧ + OSKeep st OS s s' ∧ + FrKeepA st OS s σ s' σ' := by + dsimp only + set s' := L.wr s (Endp.regA ct (regIdxA r)) v + set uc := x.tcbArch.tcbContext + set x' : Tcb := { x with tcbArch := { x.tcbArch with tcbContext := ⟨uc.userFpuState, Function.update uc.userRegs r v.bv⟩ } } + set σ' := updKSc σ (specPtr ct) (.tcb x') + have hsp : asUser (Z := Unit) (specPtr ct) (setRegister r v.bv) σ = ({((), σ')}, False) := + asUser_setReg_eqH hkt r v.bv + obtain ⟨_, _, -, arm, -, -, -, -, -, -, -⟩ := J7FrA.fr (st := st) _ _ _ _ _ _ J.j7 + have htH : H ct = some .tcb := show ct ∈ H.tcbs by rw [J.tcbs]; exact hct + have hft := J.ent.heap.fits ct .tcb htH; change ct.val + 4096 ≤ 2 ^ 64 at hft + have hr : (regIdxA r).val < 384 := by have := regIdxA_lt r; omega + have hreg_eq : Endp.regA ct (regIdxA r) = wa ct ⟨BitVec.ofNat 64 (256 + 8 * (regIdxA r).val)⟩ := + tcb_reg_addr_eqH hr hft + have hdv : (⟨BitVec.ofNat 64 (256 + 8 * (regIdxA r).val)⟩ : U64).val = 256 + 8 * (regIdxA r).val := by + show (BitVec.ofNat 64 _).toNat = _; rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + have hal_reg : Aligned (Endp.regA ct (regIdxA r)) := by + rw [hreg_eq]; exact field_aligned (J.sr.core.tcbs ct hct) (by omega) (by omega) + have hreg_val : (Endp.regA ct (regIdxA r)).val = ct.val + (256 + 8 * (regIdxA r).val) := by + rw [hreg_eq, wa_valm, hdv]; exact Nat.mod_eq_of_lt (by omega) + have hw : WritesIn st s s' (VcpuWrNoStA st Z OS s) := + (writesIn_wr hal_reg).mono fun _ e => by subst e; exact tcb_reg_in_vcpuWrH hct hft hr + have hHS : VcpuSpecStepA H σ σ' := by + refine ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, ?_, fun _ h => absurd rfl h⟩ + intro p + by_cases ep : p = specPtr ct + · subst ep + exact Or.inr (Or.inl ⟨x, x', hkt, by simp [σ', updKSc, Function.update], + ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, fun _ => by first | exact id | exact fun _ => rfl⟩⟩) + · exact Or.inl (by simp [σ', updKSc, Function.update, ep]) + have osk := osKeep_of_vcpuWrCoreA J R6.vcOS R6.frOff hw + have capE : ∀ y ∈ OS, capA st s' y = capA st s y := fun y hy => by + unfold capA; rw [(osk y hy).1, (osk y hy).2.1] + have hVcEq : ∀ vc ∈ Z.V, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → L.mem s' (wa vc o) = L.mem s (wa vc o) := by + intro vc hvc o ho1 ho8 + have hvH := arm.vh vc hvc + have hfv := J.ent.heap.fits vc _ hvH; have hav := J.ent.heap.align vc _ hvH + simp only [HKind.bits] at hfv hav + have hne : vc ≠ ct := fun e => by rw [e, htH] at hvH; cases hvH + have hd := J.ent.heap.disj vc (.other 12) ct .tcb hvH htH hne + change vc.val + 4096 ≤ ct.val ∨ ct.val + 4096 ≤ vc.val at hd + have ev : (wa vc o).val = vc.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact L.mem_wr_ne hal_reg (by show _ % 8 = 0; omega) (fun e => by rw [hreg_val, ev] at e; omega) + have hTcbEq : ∀ u ∈ Z.T, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → + (u ≠ ct ∨ o.val < 256 ∨ 3328 ≤ o.val) → L.mem s' (wa u o) = L.mem s (wa u o) := by + intro u hu o ho1 ho8 hne + have huH : H u = some .tcb := show u ∈ H.tcbs by rw [J.tcbs]; exact hu + have hfu := J.ent.heap.fits u .tcb huH; change u.val + 4096 ≤ 2 ^ 64 at hfu + have ev : (wa u o).val = u.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + refine L.mem_wr_ne hal_reg (field_aligned (J.sr.core.tcbs u hu) ho1 ho8) fun e => ?_ + rw [hreg_val, ev] at e + rcases hne with eut | hlo | hhi + · have hd := J.ent.heap.disj u .tcb ct .tcb huH htH eut + change u.val + 4096 ≤ ct.val ∨ ct.val + 4096 ≤ u.val at hd; omega + · by_cases eut : u = ct + · subst eut; omega + · have hd := J.ent.heap.disj u .tcb ct .tcb huH htH eut + change u.val + 4096 ≤ ct.val ∨ ct.val + 4096 ≤ u.val at hd; omega + · by_cases eut : u = ct + · subst eut; omega + · have hd := J.ent.heap.disj u .tcb ct .tcb huH htH eut + change u.val + 4096 ≤ ct.val ∨ ct.val + 4096 ≤ u.val at hd; omega + have hVcCore : VcpuCoreA (L := L) Z s' σ' := by + refine ⟨fun vc hvc => ?_, fun p xv' hxp => ?_, fun u hu => ?_, fun y hy p hc => ?_⟩ + · obtain ⟨xv0, hkv0, hvt0, hrc0⟩ := J.j.2.1.obj vc hvc + have hne : specPtr vc ≠ specPtr ct := fun e => by + have : vc = ct := specPtr_inj e; have hvH := arm.vh vc hvc; rw [this, htH] at hvH; cases hvH + have h0 := hVcEq vc hvc 0#u64 (by decide) (by decide); rw [wa_zeroH] at h0 + exact ⟨xv0, by simp [σ', updKSc, Function.update, hne, hkv0], by rw [h0]; exact hvt0, + by rw [hVcEq vc hvc 24#u64 (by decide) (by decide)]; exact hrc0⟩ + · by_cases ept : p = specPtr ct + · subst ept; simp [σ', updKSc, Function.update] at hxp + · exact J.j.2.1.back p xv' (by simpa [σ', updKSc, Function.update, ept] using hxp) + · obtain ⟨xt0, hkt0, h1, h2⟩ := J.j.2.1.tcb u hu + have hm3664 := hTcbEq u hu 3664#u64 (by decide) (by decide) (Or.inr (Or.inr (by decide))) + have hm3672 := hTcbEq u hu 3672#u64 (by decide) (by decide) (Or.inr (Or.inr (by decide))) + by_cases eu : u = ct + · subst eu; rw [hkt] at hkt0; cases hkt0 + exact ⟨x', by simp [σ', updKSc, Function.update], by rw [hm3664]; exact h1, by unfold runVcpuA; rw [hm3672]; exact h2⟩ + · have hne : specPtr u ≠ specPtr ct := fun e => eu (specPtr_inj e) + exact ⟨xt0, by simp [σ', updKSc, Function.update, hne, hkt0], + by rw [hm3664]; exact h1, by unfold runVcpuA; rw [hm3672]; exact h2⟩ + · have hyOS : y ∈ OS := J.j5.2.1 y hy + unfold capAtA at hc; rw [(osk y hyOS).1, (osk y hyOS).2.1] at hc + exact J.j.2.1.cap y hy p hc + have hVcRun : VcRunA st Z.T Z.V s' := + ⟨fun u hu => by rw [hTcbEq u hu 3664#u64 (by decide) (by decide) (Or.inr (Or.inr (by decide)))]; exact J.vcRun.tcb u hu, + fun vc hvc => by rw [hVcEq vc hvc 24#u64 (by decide) (by decide)]; exact J.vcRun.vcpu vc hvc, + fun vc hvc => by have h0 := hVcEq vc hvc 0#u64 (by decide) (by decide); rw [wa_zeroH] at h0; rw [h0]; exact J.vcRun.vtcb vc hvc, + fun u hu => by unfold runVcpuA; rw [hTcbEq u hu 3672#u64 (by decide) (by decide) (Or.inr (Or.inr (by decide)))]; exact J.vcRun.run u hu, + fun u hu h => by + rw [hTcbEq u hu 3664#u64 (by decide) (by decide) (Or.inr (Or.inr (by decide)))] at h + unfold runVcpuA; rw [hTcbEq u hu 3672#u64 (by decide) (by decide) (Or.inr (Or.inr (by decide)))] + exact J.vcRun.disj u hu h⟩ + have hRunVcpu : RunVcpuInv st Z.T s' := fun u hu => by + rw [stA_eq, hTcbEq u hu 3328#u64 (by decide) (by decide) (Or.inr (Or.inr (by decide))), ← stA_eq] + unfold runVcpuA; rw [hTcbEq u hu 3672#u64 (by decide) (by decide) (Or.inr (Or.inr (by decide)))] + exact J.runVcpu u hu + have hEpTcb : ∀ u ∈ Z.T, ∃ y, σ'.kheap (specPtr u) = some (.tcb y) ∧ TcbRelE Rp (L.mem s') u y := fun u hu => by + obtain ⟨y0, hky0, hrel0⟩ := J.ep.tcb u hu + by_cases eu : u = ct + · subst eu; rw [hkt] at hky0; cases hky0 + refine ⟨x', by simp [σ', updKSc, Function.update], ?_⟩ + refine ⟨(by rw [stA_eq, hTcbEq u hu 3328#u64 (by decide) (by decide) (Or.inr (Or.inr (by decide))), ← stA_eq]; exact hrel0.st), + (fun e he => by unfold tBlockA; rw [hTcbEq u hu _ (by simp) (by simp) (Or.inr (Or.inr (by decide)))]; exact hrel0.blk e he), + (fun e d hs => by unfold tBadgeA tFlagsA; rw [hTcbEq u hu _ (by simp) (by simp) (Or.inr (Or.inr (by decide))), hTcbEq u hu _ (by simp) (by simp) (Or.inr (Or.inr (by decide)))]; exact hrel0.send e d hs), + (fun e r0 d hr0 => by unfold tReplyA; rw [hTcbEq u hu _ (by simp) (by simp) (Or.inr (Or.inr (by decide)))]; exact hrel0.recv e r0 d hr0), + (fun r0 hr0 => by unfold tReplyA; rw [hTcbEq u hu _ (by simp) (by simp) (Or.inr (Or.inr (by decide)))]; exact hrel0.reply r0 hr0), + (fun r' => ?_), + (by unfold prioA; rw [hTcbEq u hu _ (by simp) (by simp) (Or.inr (Or.inr (by decide)))]; exact hrel0.prio), + (by unfold tFaultA; rw [hTcbEq u hu _ (by simp) (by simp) (Or.inr (Or.inr (by decide)))]; exact hrel0.fault0), + (by unfold tFaultA; rw [hTcbEq u hu _ (by simp) (by simp) (Or.inr (Or.inr (by decide)))]; exact hrel0.faultT), + (by unfold tBoundA; rw [hTcbEq u hu _ (by simp) (by simp) (Or.inr (Or.inr (by decide)))]; exact hrel0.bound), + (by unfold FaultRel tFaultA tFw0A tFw1A tFw2A tLfA tLf0A tLf1A + rw [hTcbEq u hu _ (by simp) (by simp) (Or.inr (Or.inr (by decide))), + hTcbEq u hu _ (by simp) (by simp) (Or.inr (Or.inr (by decide))), + hTcbEq u hu _ (by simp) (by simp) (Or.inr (Or.inr (by decide))), + hTcbEq u hu _ (by simp) (by simp) (Or.inr (Or.inr (by decide))), + hTcbEq u hu _ (by simp) (by simp) (Or.inr (Or.inr (by decide))), + hTcbEq u hu _ (by simp) (by simp) (Or.inr (Or.inr (by decide))), + hTcbEq u hu _ (by simp) (by simp) (Or.inr (Or.inr (by decide)))] + exact hrel0.faultR)⟩ + by_cases er : r' = r + · subst er; rw [L.mem_wr_same hal_reg]; simp [x', uc, Function.update] + · have hr' : (regIdxA r').val < 384 := by have := regIdxA_lt r'; omega + have hreg'_eq := tcb_reg_addr_eqH (t := u) hr' hft + have hdv' : (⟨BitVec.ofNat 64 (256 + 8 * (regIdxA r').val)⟩ : U64).val = 256 + 8 * (regIdxA r').val := by + show (BitVec.ofNat 64 _).toNat = _; rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + have hal' : Aligned (Endp.regA u (regIdxA r')) := by + rw [hreg'_eq]; exact field_aligned (J.sr.core.tcbs u hu) (by omega) (by omega) + have hne_idx : (regIdxA r').val ≠ (regIdxA r).val := fun e => er (regIdxA_inj e) + have hne_val : (Endp.regA u (regIdxA r)).val ≠ (Endp.regA u (regIdxA r')).val := by + rw [hreg_val, hreg'_eq, wa_valm, hdv', Nat.mod_eq_of_lt (by omega)]; omega + rw [L.mem_wr_ne hal_reg hal' hne_val]; simp [x', uc, Function.update, er]; exact hrel0.regs r' + · have huH : H u = some .tcb := show u ∈ H.tcbs by rw [J.tcbs]; exact hu + have hfu := J.ent.heap.fits u .tcb huH; change u.val + 4096 ≤ 2 ^ 64 at hfu + have hne : specPtr u ≠ specPtr ct := fun e => eu (specPtr_inj e) + refine ⟨y0, by simp [σ', updKSc, Function.update, hne, hky0], hrel0.frame (J.sq.tcbs u huH) fun b hb hbu => ?_⟩ + have hlt : b.val - u.val < 3664 := by have := hbu.2; unfold TOffN at this; omega + have hb_eq : b = wa u ⟨BitVec.ofNat 64 (b.val - u.val)⟩ := by + apply UScalar.eq_of_val_eq; rw [wa_valm] + have hdv' : (⟨BitVec.ofNat 64 (b.val - u.val)⟩ : U64).val = b.val - u.val := by + show (BitVec.ofNat 64 _).toNat = _; rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + rw [hdv', Nat.add_sub_of_le hbu.1]; exact (Nat.mod_eq_of_lt (by omega)).symm + have hdv' : (⟨BitVec.ofNat 64 (b.val - u.val)⟩ : U64).val = b.val - u.val := by + show (BitVec.ofNat 64 _).toNat = _; rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + have hua := J.ent.heap.align u .tcb huH; change u.val % 4096 = 0 at hua + have h8 : (b.val - u.val) % 8 = 0 := by have : b.val % 8 = 0 := hb; omega + rw [hb_eq]; exact hTcbEq u hu _ (by omega) (by omega) (Or.inl eu) + have hEpBuf : ∀ bp ∈ Bf, ∀ k : U64, k.val < 128 → + (L.mem s' (bufW bp.1 k)).bv = loadWordAt σ'.machineState.underlyingMemory (bufAddr bp.2 k.val) ∧ + (bufAddr bp.2 k.val).toNat % 8 = 0 := fun bp hbp k hk => by + have ⟨h1, h2⟩ := J.ep.buf bp hbp k hk + have hbw : BufW Bf (bufW bp.1 k) := ⟨bp, hbp, k, hk, rfl⟩ + have hmem_bw : L.mem s' (bufW bp.1 k) = L.mem s (bufW bp.1 k) := by + refine L.mem_wr_ne hal_reg (J.iplaced.e.balign _ hbw) fun e => ?_ + have hbt := J.iplaced.e.bt _ hbw ct hct; rw [hreg_val] at e; omega + rw [hmem_bw]; exact ⟨h1, h2⟩ + have hDeadVcpu : ∀ vc ∈ Z.V, DeadA st OS .vcpu s' vc → ∀ xv0, σ'.kheap (specPtr vc) = some (.archObj (.vcpu xv0)) → + xv0.vcpuTcb = none := fun vc hvc hd xv0 hx => by + have hne : specPtr vc ≠ specPtr ct := fun e => by + have : vc = ct := specPtr_inj e; have hvH := arm.vh vc hvc; rw [this, htH] at hvH; cases hvH + simp [σ', updKSc, Function.update, hne] at hx + exact J.deadExt.vcpu vc hvc (fun y hy hc => hd y hy (by rw [capE y hy]; exact hc)) xv0 hx + have hDeadTvcpu : ∀ u ∈ Z.T, DeadA st OS .tcb s' u → L.mem s' (wa u 3664#u64) = 0#u64 := fun u hu hd => by + rw [hTcbEq u hu 3664#u64 (by decide) (by decide) (Or.inr (Or.inr (by decide)))] + exact J.deadExt.tvcpu u hu (fun y hy hc => hd y hy (by rw [capE y hy]; exact hc)) + have hDeadVcRun : ∀ vc ∈ Z.V, DeadA st OS .vcpu s' vc → L.mem s' (wa vc 24#u64) = 0#u64 := fun vc hvc hd => by + rw [hVcEq vc hvc 24#u64 (by decide) (by decide)] + exact J.deadExt.vcDeadRun vc hvc (fun y hy hc => hd y hy (by rw [capE y hy]; exact hc)) + have hZombNoVc : ZombieTcbNoVcpuA st OS s' := fun y hy u num hz => by + have hz0 : capA st s y = .Zombie u capability.ZOMBIE_TCB num := by rwa [capE y hy] at hz + have huT : u ∈ Z.T := J.tcbs ▸ J.zombieTcb y hy u num hz0 + rw [hTcbEq u huT 3664#u64 (by decide) (by decide) (Or.inr (Or.inr (by decide)))] + exact J.zombieNoVcpu y hy u num hz0 + have hUtVs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → L.mem s' b = L.mem s b := fun b hb => by + have hno := utVs_off_vcpuWrCoreA (regWr := false) J hb + exact L.mem_wr_ne hal_reg (J.vsOk.al b hb) (fun e => by + have hbe : b = Endp.regA ct (regIdxA r) := (UScalar.eq_of_val_eq e).symm + rw [hbe, hreg_eq] at hno + exact hno (Or.inr (Or.inl ⟨ct, hct, ⟨BitVec.ofNat 64 (256 + 8 * (regIdxA r).val)⟩, by omega, Or.inl ⟨by omega, by omega⟩, rfl⟩))) + have J' := JStage3VcpuCoreA.of_vcpuStepA J R6 hw hHS J.j.1.ar hUtVs hVcCore hVcRun hRunVcpu + hEpTcb hEpBuf hDeadVcpu hDeadTvcpu hDeadVcRun hZombNoVc + have hSV' := SchedValidA.of_vcpuStepA hSV hHS + have hSnap' := VcpuStateSnapA.of_vcpuStepCoreA J R6.snap hw hHS fun vc hvc => by + rw [hVcEq vc hvc 8#u64 (by decide) (by decide)] + obtain ⟨xv, hkv, -⟩ := J.j.2.1.obj vc hvc + exact (R6.snap.snap vc hvc xv hkv).2.2.2.2 + have hFrOff' : FrOff st Z.Cs OS (L.ksb s') s' := by + rw [hw.1]; intro u hu p0 sz0 r0 mp0 ept0 a0 va0 hc0 b0 hb0 + exact R6.frOff u hu p0 sz0 r0 mp0 ept0 a0 va0 ((capE u hu).symm.trans hc0) b0 hb0 + have hFrTh' : FrThA st Z.th OS s' := R6.frTh.keepA capE fun _ _ e => e + have hUserFr' : UserFrSpecA st OS s' σ' := + R6.userFr.keepA capE fun _ _ _ h => hHS.keepObj h (fun _ e => by cases e) (fun _ e => by cases e) + have hR6' : VcpuRec6A st Z OS Bf s' σ' := by + refine ⟨hSnap', fun u hu => ?_, fun vc hvc => ?_, fun vc hvc hv0 => ?_, fun u hu hu0 => ?_, + R6.vcOS, hFrOff', hUserFr', hFrTh', R6.bfSpec⟩ + · rw [hTcbEq u hu 3664#u64 (by decide) (by decide) (Or.inr (Or.inr (by decide)))] + intro hne + have hvV' := (J.vcRun.tcb u hu).resolve_left hne + have h0 := hVcEq _ hvV' 0#u64 (by decide) (by decide); rw [wa_zeroH] at h0 + rw [h0]; exact R6.vcSym1 u hu hne + · rw [hVcEq vc hvc 24#u64 (by decide) (by decide)] + intro hne + have huT := (J.vcRun.vcpu vc hvc).resolve_left hne + unfold runVcpuA; rw [hTcbEq _ huT 3672#u64 (by decide) (by decide) (Or.inr (Or.inr (by decide)))] + exact R6.vcSym2 vc hvc hne + · have h0 := hVcEq vc hvc 0#u64 (by decide) (by decide); rw [wa_zeroH] at h0 + rw [h0] at hv0 ⊢ + have huT := (J.vcRun.vtcb vc hvc).resolve_left hv0 + rw [hTcbEq _ huT 3664#u64 (by decide) (by decide) (Or.inr (Or.inr (by decide)))] + exact R6.vcBack vc hvc hv0 + · unfold runVcpuA at hu0 ⊢ + rw [hTcbEq u hu 3672#u64 (by decide) (by decide) (Or.inr (Or.inr (by decide)))] at hu0 ⊢ + have hvV' : L.mem s (wa u 3672#u64) ∈ Z.V := (J.vcRun.run u hu).resolve_left hu0 + rw [hVcEq _ hvV' 24#u64 (by decide) (by decide)] + exact R6.runBack u hu hu0 + exact ⟨hsp, J', hHS, hw, hSV', hR6', hCl, hVcEq, hTcbEq, hw.1, hw.2.1, osk, + FrKeepA.kerCoreA J osk rfl fun b al _ nt => (writesIn_wr hal_reg).mem_eq al fun e => + nt ct hct ⟨BitVec.ofNat 64 (256 + 8 * (regIdxA r).val)⟩ (by rw [hdv]; omega) (e.trans hreg_eq)⟩ + +set_option maxHeartbeats 800000 in +/-- Lockstep dissociation of `(vc, t)`: clearing `vc.TCB := 0`, `vc.OWNER_CPU := 0`, +`t.VCPU := 0`, and sanitising `t.SPSR_EL1` matches `dissociateVcpuTcb (specPtr vc) (specPtr t)` +on `JStage3TA`. -/ +private theorem vcpu_dissoc_bind_stage3TA + {AR : A → HwRootA → Prop} {Clock : A → Fl4SpecA64.MachineState → Prop} + {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J7 : JSlotA S} + [J7PpA st J7] [J7PpKA st J7] [J7FrA st J7] + {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + (hSV : SchedValidA σ) + (R6 : VcpuRec6A st Z OS Bf s σ) + (hCl : Clock a σ.machineState) + {vc t : U64} (hvc : vc ∈ Z.V) (hvc_nz : vc ≠ 0#u64) (ht : t ∈ Z.T) + (hlink : L.mem s vc = t) (hback : L.mem s (wa t 3664#u64) = vc) + {xv : Fl4SpecA64.Vcpu} {xt : Fl4SpecA64.Tcb} + (hkv : σ.kheap (specPtr vc) = some (.archObj (.vcpu xv))) + (hkt : σ.kheap (specPtr t) = some (.tcb xt)) : + let s1 := L.wr s vc 0#u64 + let s2 := L.wr s1 (wa vc 8#u64) 0#u64 + let s3 := L.wr s2 (wa t 3664#u64) 0#u64 + let spsr' : U64 := ⟨sanitiseRegister false .spsrEl1 (L.mem s3 (Endp.regA t (regIdxA .spsrEl1))).bv⟩ + let s4 := L.wr s3 (Endp.regA t (regIdxA .spsrEl1)) spsr' + let σ' := updKSc (updKSc σ (specPtr t) (.tcb (dissociateTcbA xt))) (specPtr vc) + (.archObj (.vcpu { xv with vcpuTcb := none })) + dissociateVcpuTcb (Z := Unit) (specPtr vc) (specPtr t) σ = ({((), σ')}, False) ∧ + JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s4 a σ' m ∧ + SchedValidA σ' ∧ + VcpuRec6A st Z OS Bf s4 σ' ∧ + Clock a σ'.machineState ∧ + L.mem s4 vc = 0#u64 ∧ + L.mem s4 (wa t 3664#u64) = 0#u64 ∧ + L.mem s4 (wa vc 24#u64) = L.mem s (wa vc 24#u64) ∧ + (∀ u ∈ Z.T, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → + (u ≠ t ∨ (o ≠ 3664#u64 ∧ (o.val < 256 ∨ 3328 ≤ o.val))) → L.mem s4 (wa u o) = L.mem s (wa u o)) ∧ + L.ksb s4 = L.ksb s ∧ L.cpu s4 = L.cpu s ∧ + OSKeep st OS s s4 ∧ + FrKeepA st OS s σ s4 σ' := by + dsimp only + set s1 := L.wr s vc 0#u64 + set s2 := L.wr s1 (wa vc 8#u64) 0#u64 + set s3 := L.wr s2 (wa t 3664#u64) 0#u64 + set spsr' : U64 := ⟨sanitiseRegister false .spsrEl1 (L.mem s3 (Endp.regA t (regIdxA .spsrEl1))).bv⟩ + set s4 := L.wr s3 (Endp.regA t (regIdxA .spsrEl1)) spsr' + set σ' := updKSc (updKSc σ (specPtr t) (.tcb (dissociateTcbA xt))) (specPtr vc) + (.archObj (.vcpu { xv with vcpuTcb := none })) + obtain ⟨xv0, hkv0, hvt0, -⟩ := J.j.2.1.obj vc hvc + rw [hkv] at hkv0; cases hkv0 + obtain ⟨xt0, hkt0, htv0, -⟩ := J.j.2.1.tcb t ht + rw [hkt] at hkt0; cases hkt0 + have hvt : xv.vcpuTcb = some (specPtr t) := by + have hne : t ≠ 0#u64 := fun e => (J.sr.core.tcbs t ht).ne0 (by rw [e]; rfl) + rw [hlink] at hvt0; rw [hvt0]; unfold optRefA; simp [hne] + have htv : xt.tcbArch.tcbVcpu = some (specPtr vc) := by + rw [hback] at htv0; rw [htv0]; unfold optRefA; simp [hvc_nz] + have hsp : dissociateVcpuTcb (Z := Unit) (specPtr vc) (specPtr t) σ = ({((), σ')}, False) := + dissociateVcpuTcb_eqA J.curVcpuNone hkv hkt hvt htv + obtain ⟨_, _, -, arm, -, -, -, -, -, -, -⟩ := J7FrA.fr (st := st) _ _ _ _ _ _ J.j7 + have hvH : H vc = some (.other 12) := arm.vh vc hvc + have htH : H t = some .tcb := show t ∈ H.tcbs by rw [J.tcbs]; exact ht + have hft := J.ent.heap.fits t .tcb htH; change t.val + 4096 ≤ 2 ^ 64 at hft + have hne_spec : specPtr t ≠ specPtr vc := fun e => by rw [e, hkv] at hkt; cases hkt + have al_tcb : ∀ u ∈ Z.T, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → Aligned (wa u o) := fun u hu o h1 h8 => + field_aligned (J.sr.core.tcbs u hu) h1 h8 + have al_vc : ∀ v ∈ Z.V, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → Aligned (wa v o) := fun v hv o h1 h8 => by + have hv' := arm.vh v hv + have hf := J.ent.heap.fits v _ hv'; have ha := J.ent.heap.align v _ hv' + simp only [HKind.bits] at hf ha + have ev : (wa v o).val = v.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + show _ % 8 = 0; omega + have hal_v0 : Aligned vc := by have := al_vc vc hvc 0#u64 (by decide) (by decide); rwa [wa_zeroH] at this + have hal_v8 : Aligned (wa vc 8#u64) := al_vc vc hvc 8#u64 (by decide) (by decide) + have hal_t3664 : Aligned (wa t 3664#u64) := al_tcb t ht 3664#u64 (by decide) (by decide) + have hr_spsr : (regIdxA .spsrEl1).val < 384 := by have := regIdxA_lt .spsrEl1; omega + have hspsr_eq : Endp.regA t (regIdxA .spsrEl1) = wa t ⟨BitVec.ofNat 64 (256 + 8 * (regIdxA .spsrEl1).val)⟩ := + tcb_reg_addr_eqH hr_spsr hft + have hdv_spsr : (⟨BitVec.ofNat 64 (256 + 8 * (regIdxA .spsrEl1).val)⟩ : U64).val = 256 + 8 * (regIdxA .spsrEl1).val := by + show (BitVec.ofNat 64 _).toNat = _; rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + have hal_spsr : Aligned (Endp.regA t (regIdxA .spsrEl1)) := by + rw [hspsr_eq]; exact al_tcb t ht _ (by omega) (by omega) + have hspsr_val : (Endp.regA t (regIdxA .spsrEl1)).val = t.val + (256 + 8 * (regIdxA .spsrEl1).val) := by + rw [hspsr_eq, wa_valm, hdv_spsr]; exact Nat.mod_eq_of_lt (by omega) + have hmem_other : ∀ b, Aligned b → b ≠ vc → b ≠ wa vc 8#u64 → b ≠ wa t 3664#u64 → + b ≠ Endp.regA t (regIdxA .spsrEl1) → L.mem s4 b = L.mem s b := fun b hb h1 h2 h3 h4 => by + rw [L.mem_wr_ne hal_spsr hb (fun e => h4 (UScalar.eq_of_val_eq e).symm), + L.mem_wr_ne hal_t3664 hb (fun e => h3 (UScalar.eq_of_val_eq e).symm), + L.mem_wr_ne hal_v8 hb (fun e => h2 (UScalar.eq_of_val_eq e).symm), + L.mem_wr_ne hal_v0 hb (fun e => h1 (UScalar.eq_of_val_eq e).symm)] + have hVcEq : ∀ v ∈ Z.V, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → + (v ≠ vc ∨ (o ≠ 0#u64 ∧ o ≠ 8#u64)) → L.mem s4 (wa v o) = L.mem s (wa v o) := by + intro v hv o ho1 ho8 hne + have hv' := arm.vh v hv + have hfv := J.ent.heap.fits v _ hv'; have hfvc := J.ent.heap.fits vc _ hvH + simp only [HKind.bits] at hfv hfvc + have ev : (wa v o).val = v.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have hne_vt : v ≠ t := fun e => by rw [e, htH] at hv'; cases hv' + have hdt := J.ent.heap.disj v (.other 12) t .tcb hv' htH hne_vt + change v.val + 4096 ≤ t.val ∨ t.val + 4096 ≤ v.val at hdt + refine hmem_other _ (al_vc v hv o ho1 ho8) ?_ ?_ ?_ ?_ + · intro e; have hv0 := congrArg UScalar.val e; rw [ev] at hv0 + rcases hne with evc | ⟨ho0, -⟩ + · have hd := J.ent.heap.disj v (.other 12) vc (.other 12) hv' hvH evc + change v.val + 4096 ≤ vc.val ∨ vc.val + 4096 ≤ v.val at hd; omega + · by_cases evc : v = vc + · subst evc; exact ho0 (UScalar.eq_of_val_eq (by change o.val = 0; omega)) + · have hd := J.ent.heap.disj v (.other 12) vc (.other 12) hv' hvH evc + change v.val + 4096 ≤ vc.val ∨ vc.val + 4096 ≤ v.val at hd; omega + · intro e; have hv8 := congrArg UScalar.val e + rw [ev, wa_valm] at hv8; change v.val + o.val = (vc.val + 8) % _ at hv8 + rw [Nat.mod_eq_of_lt (by omega)] at hv8 + rcases hne with evc | ⟨-, ho8'⟩ + · have hd := J.ent.heap.disj v (.other 12) vc (.other 12) hv' hvH evc + change v.val + 4096 ≤ vc.val ∨ vc.val + 4096 ≤ v.val at hd; omega + · by_cases evc : v = vc + · subst evc; exact ho8' (UScalar.eq_of_val_eq (by change o.val = 8; omega)) + · have hd := J.ent.heap.disj v (.other 12) vc (.other 12) hv' hvH evc + change v.val + 4096 ≤ vc.val ∨ vc.val + 4096 ≤ v.val at hd; omega + · intro e; have hv3 := congrArg UScalar.val e + rw [ev, wa_valm] at hv3; change v.val + o.val = (t.val + 3664) % _ at hv3 + rw [Nat.mod_eq_of_lt (by omega)] at hv3; omega + · intro e; have hvs := congrArg UScalar.val e; rw [ev, hspsr_val] at hvs; omega + have hTcbEq : ∀ u ∈ Z.T, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → + (u ≠ t ∨ (o ≠ 3664#u64 ∧ (o.val < 256 ∨ 3328 ≤ o.val))) → L.mem s4 (wa u o) = L.mem s (wa u o) := by + intro u hu o ho1 ho8 hne + have huH : H u = some .tcb := show u ∈ H.tcbs by rw [J.tcbs]; exact hu + have hfu := J.ent.heap.fits u .tcb huH; have hfvc := J.ent.heap.fits vc _ hvH + simp only [HKind.bits] at hfu hfvc + have ev : (wa u o).val = u.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have hne_uv : u ≠ vc := fun e => by rw [e, hvH] at huH; cases huH + have hdv := J.ent.heap.disj u .tcb vc (.other 12) huH hvH hne_uv + change u.val + 4096 ≤ vc.val ∨ vc.val + 4096 ≤ u.val at hdv + refine hmem_other _ (al_tcb u hu o ho1 ho8) ?_ ?_ ?_ ?_ + · intro e; have hv0 := congrArg UScalar.val e; rw [ev] at hv0; omega + · intro e; have hv8 := congrArg UScalar.val e + rw [ev, wa_valm] at hv8; change u.val + o.val = (vc.val + 8) % _ at hv8 + rw [Nat.mod_eq_of_lt (by omega)] at hv8; omega + · intro e; have hv3 := congrArg UScalar.val e + rw [ev, wa_valm] at hv3; change u.val + o.val = (t.val + 3664) % _ at hv3 + rw [Nat.mod_eq_of_lt (by omega)] at hv3 + rcases hne with eut | ⟨ho3, -⟩ + · have hd := J.ent.heap.disj u .tcb t .tcb huH htH eut + change u.val + 4096 ≤ t.val ∨ t.val + 4096 ≤ u.val at hd; omega + · by_cases eut : u = t + · subst eut; exact ho3 (UScalar.eq_of_val_eq (by change o.val = 3664; omega)) + · have hd := J.ent.heap.disj u .tcb t .tcb huH htH eut + change u.val + 4096 ≤ t.val ∨ t.val + 4096 ≤ u.val at hd; omega + · intro e; have hvs := congrArg UScalar.val e; rw [ev, hspsr_val] at hvs + rcases hne with eut | ⟨-, hlo | hhi⟩ + · have hd := J.ent.heap.disj u .tcb t .tcb huH htH eut + change u.val + 4096 ≤ t.val ∨ t.val + 4096 ≤ u.val at hd; omega + · by_cases eut : u = t + · subst eut; omega + · have hd := J.ent.heap.disj u .tcb t .tcb huH htH eut + change u.val + 4096 ≤ t.val ∨ t.val + 4096 ≤ u.val at hd; omega + · by_cases eut : u = t + · subst eut; omega + · have hd := J.ent.heap.disj u .tcb t .tcb huH htH eut + change u.val + 4096 ≤ t.val ∨ t.val + 4096 ≤ u.val at hd; omega + have hmem_v0 : L.mem s4 vc = 0#u64 := by + have hfvc := J.ent.heap.fits vc _ hvH; simp only [HKind.bits] at hfvc + have hne_vt : vc ≠ t := fun e => by rw [e, htH] at hvH; cases hvH + have hdt := J.ent.heap.disj vc (.other 12) t .tcb hvH htH hne_vt + change vc.val + 4096 ≤ t.val ∨ t.val + 4096 ≤ vc.val at hdt + have hv8 : (wa vc 8#u64).val = vc.val + 8 := by rw [wa_valm]; change (vc.val + 8) % _ = _; exact Nat.mod_eq_of_lt (by omega) + have hv3 : (wa t 3664#u64).val = t.val + 3664 := by rw [wa_valm]; change (t.val + 3664) % _ = _; exact Nat.mod_eq_of_lt (by omega) + rw [L.mem_wr_ne hal_spsr hal_v0 (by omega), L.mem_wr_ne hal_t3664 hal_v0 (by omega), + L.mem_wr_ne hal_v8 hal_v0 (by omega), L.mem_wr_same hal_v0] + have hmem_v8 : L.mem s4 (wa vc 8#u64) = 0#u64 := by + have hfvc := J.ent.heap.fits vc _ hvH; simp only [HKind.bits] at hfvc + have hne_vt : vc ≠ t := fun e => by rw [e, htH] at hvH; cases hvH + have hdt := J.ent.heap.disj vc (.other 12) t .tcb hvH htH hne_vt + change vc.val + 4096 ≤ t.val ∨ t.val + 4096 ≤ vc.val at hdt + have hv8 : (wa vc 8#u64).val = vc.val + 8 := by rw [wa_valm]; change (vc.val + 8) % _ = _; exact Nat.mod_eq_of_lt (by omega) + have hv3 : (wa t 3664#u64).val = t.val + 3664 := by rw [wa_valm]; change (t.val + 3664) % _ = _; exact Nat.mod_eq_of_lt (by omega) + rw [L.mem_wr_ne hal_spsr hal_v8 (by omega), L.mem_wr_ne hal_t3664 hal_v8 (by omega), L.mem_wr_same hal_v8] + have hmem_t3664 : L.mem s4 (wa t 3664#u64) = 0#u64 := by + have hv3 : (wa t 3664#u64).val = t.val + 3664 := by rw [wa_valm]; change (t.val + 3664) % _ = _; exact Nat.mod_eq_of_lt (by omega) + rw [L.mem_wr_ne hal_spsr hal_t3664 (by omega), L.mem_wr_same hal_t3664] + have hmem_spsr3 : L.mem s3 (Endp.regA t (regIdxA .spsrEl1)) = L.mem s (Endp.regA t (regIdxA .spsrEl1)) := by + have hfvc := J.ent.heap.fits vc _ hvH; simp only [HKind.bits] at hfvc + have hne_vt : vc ≠ t := fun e => by rw [e, htH] at hvH; cases hvH + have hdt := J.ent.heap.disj vc (.other 12) t .tcb hvH htH hne_vt + change vc.val + 4096 ≤ t.val ∨ t.val + 4096 ≤ vc.val at hdt + have hv8 : (wa vc 8#u64).val = vc.val + 8 := by rw [wa_valm]; change (vc.val + 8) % _ = _; exact Nat.mod_eq_of_lt (by omega) + have hv3 : (wa t 3664#u64).val = t.val + 3664 := by rw [wa_valm]; change (t.val + 3664) % _ = _; exact Nat.mod_eq_of_lt (by omega) + rw [L.mem_wr_ne hal_t3664 hal_spsr (by omega), L.mem_wr_ne hal_v8 hal_spsr (by omega), L.mem_wr_ne hal_v0 hal_spsr (by omega)] + have hw1 : WritesIn st s s1 (VcpuWrNoStA st Z OS s) := + (writesIn_wr hal_v0).mono (fun _ e => by rw [e]; exact Or.inl ⟨vc, hvc, Or.inl rfl⟩) + have hw2 : WritesIn st s1 s2 (VcpuWrNoStA st Z OS s) := + (writesIn_wr hal_v8).mono (fun _ e => by rw [e]; exact Or.inl ⟨vc, hvc, Or.inr (Or.inl rfl)⟩) + have hw3 : WritesIn st s2 s3 (VcpuWrNoStA st Z OS s) := + (writesIn_wr hal_t3664).mono (fun _ e => by subst e; exact Or.inr (Or.inl ⟨t, ht, 3664#u64, by decide, Or.inr (Or.inl rfl), rfl⟩)) + have hw4 : WritesIn st s3 s4 (VcpuWrNoStA st Z OS s) := + (writesIn_wr hal_spsr).mono (fun _ e => by subst e; exact tcb_reg_in_vcpuWrH ht hft hr_spsr) + have hw : WritesIn st s s4 (VcpuWrNoStA st Z OS s) := + (((hw1.trans hw2).mono (fun _ h => h.elim id id)).trans hw3 |>.mono (fun _ h => h.elim id id)).trans hw4 |>.mono (fun _ h => h.elim id id) + have hHS : VcpuSpecStepA H σ σ' := by + refine ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, ?_, fun _ h => absurd rfl h⟩ + intro p + by_cases epv : p = specPtr vc + · subst epv + exact Or.inr (Or.inr ⟨xv, { xv with vcpuTcb := none }, hkv, by simp [σ', updKSc, Function.update], rfl, rfl, rfl, rfl, rfl, rfl⟩) + · by_cases ept : p = specPtr t + · subst ept + exact Or.inr (Or.inl ⟨xt, dissociateTcbA xt, hkt, by simp [σ', updKSc, Function.update, epv], + ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, fun _ => by first | exact id | exact fun _ => rfl⟩⟩) + · exact Or.inl (by simp [σ', updKSc, Function.update, epv, ept]) + have osk := osKeep_of_vcpuWrA J R6.vcOS R6.frOff hw + have capE : ∀ y ∈ OS, capA st s4 y = capA st s y := fun y hy => by + unfold capA; rw [(osk y hy).1, (osk y hy).2.1] + have hVcCore : VcpuCoreA (L := L) Z s4 σ' := by + refine ⟨fun v hv => ?_, fun p xv' hxp => ?_, fun u hu => ?_, fun y hy p hc => ?_⟩ + · obtain ⟨xv0, hkv0, hvt0, hrc0⟩ := J.j.2.1.obj v hv + have hm24 := hVcEq v hv 24#u64 (by decide) (by decide) (Or.inr ⟨by decide, by decide⟩) + by_cases ev : v = vc + · rcases ev with rfl; rw [hkv] at hkv0; cases hkv0 + exact ⟨{ xv with vcpuTcb := none }, by simp [σ', updKSc, Function.update], by rw [hmem_v0]; rfl, by rw [hm24]; exact hrc0⟩ + · have hne_s1 : specPtr v ≠ specPtr t := fun e => by + have : v = t := specPtr_inj e; have hv' := arm.vh v hv; rw [this, htH] at hv'; cases hv' + have hne_s2 : specPtr v ≠ specPtr vc := fun e => ev (specPtr_inj e) + have h0 := hVcEq v hv 0#u64 (by decide) (by decide) (Or.inl ev); rw [wa_zeroH] at h0 + exact ⟨xv0, by simp [σ', updKSc, Function.update, hne_s1, hne_s2, hkv0], by rw [h0]; exact hvt0, by rw [hm24]; exact hrc0⟩ + · by_cases epv : p = specPtr vc + · subst epv; exact ⟨vc, hvc, rfl⟩ + · by_cases ept : p = specPtr t + · subst ept; simp [σ', updKSc, Function.update, epv] at hxp + · exact J.j.2.1.back p xv' (by simpa [σ', updKSc, Function.update, epv, ept] using hxp) + · obtain ⟨xt0, hkt0, h1, h2⟩ := J.j.2.1.tcb u hu + have hm3672 := hTcbEq u hu 3672#u64 (by decide) (by decide) (Or.inr ⟨by decide, Or.inr (by decide)⟩) + by_cases eu : u = t + · subst eu; rw [hkt] at hkt0; cases hkt0 + exact ⟨dissociateTcbA xt, by simp [σ', updKSc, Function.update, hne_spec], + by rw [hmem_t3664]; rfl, by unfold runVcpuA; rw [hm3672]; exact h2⟩ + · have hne_s1 : specPtr u ≠ specPtr t := fun e => eu (specPtr_inj e) + have huH : H u = some .tcb := show u ∈ H.tcbs by rw [J.tcbs]; exact hu + have hne_s2 : specPtr u ≠ specPtr vc := fun e => by + have : u = vc := specPtr_inj e; rw [this, hvH] at huH; cases huH + have hm3664 := hTcbEq u hu 3664#u64 (by decide) (by decide) (Or.inl eu) + exact ⟨xt0, by simp [σ', updKSc, Function.update, hne_s1, hne_s2, hkt0], + by rw [hm3664]; exact h1, by unfold runVcpuA; rw [hm3672]; exact h2⟩ + · have hyOS : y ∈ OS := J.j5.2.1 y hy + unfold capAtA at hc; rw [(osk y hyOS).1, (osk y hyOS).2.1] at hc + exact J.j.2.1.cap y hy p hc + have hVcRun : VcRunA st Z.T Z.V s4 := by + refine ⟨fun u hu => ?_, fun v hv => ?_, fun v hv => ?_, fun u hu => ?_, fun u hu h => ?_⟩ + · by_cases eu : u = t + · subst eu; exact Or.inl hmem_t3664 + · rw [hTcbEq u hu 3664#u64 (by decide) (by decide) (Or.inl eu)]; exact J.vcRun.tcb u hu + · rw [hVcEq v hv 24#u64 (by decide) (by decide) (Or.inr ⟨by decide, by decide⟩)]; exact J.vcRun.vcpu v hv + · by_cases ev : v = vc + · subst ev; exact Or.inl hmem_v0 + · have h0 := hVcEq v hv 0#u64 (by decide) (by decide) (Or.inl ev); rw [wa_zeroH] at h0; rw [h0]; exact J.vcRun.vtcb v hv + · unfold runVcpuA; rw [hTcbEq u hu 3672#u64 (by decide) (by decide) (Or.inr ⟨by decide, Or.inr (by decide)⟩)] + exact J.vcRun.run u hu + · by_cases eu : u = t + · subst eu; exact absurd hmem_t3664 h + · rw [hTcbEq u hu 3664#u64 (by decide) (by decide) (Or.inl eu)] at h + unfold runVcpuA; rw [hTcbEq u hu 3672#u64 (by decide) (by decide) (Or.inr ⟨by decide, Or.inr (by decide)⟩)] + exact J.vcRun.disj u hu h + have hRunVcpu : RunVcpuInv st Z.T s4 := fun u hu => by + rw [stA_eq, hTcbEq u hu 3328#u64 (by decide) (by decide) (Or.inr ⟨by decide, Or.inr (by decide)⟩), ← stA_eq] + unfold runVcpuA; rw [hTcbEq u hu 3672#u64 (by decide) (by decide) (Or.inr ⟨by decide, Or.inr (by decide)⟩)] + exact J.runVcpu u hu + have hEpTcb : ∀ u ∈ Z.T, ∃ y, σ'.kheap (specPtr u) = some (.tcb y) ∧ TcbRelE Rp (L.mem s4) u y := fun u hu => by + obtain ⟨y0, hky0, hrel0⟩ := J.ep.tcb u hu + by_cases eu : u = t + · subst eu; rw [hkt] at hky0; cases hky0 + refine ⟨dissociateTcbA xt, by simp [σ', updKSc, Function.update, hne_spec], ?_⟩ + refine ⟨(by rw [stA_eq, hTcbEq u hu 3328#u64 (by decide) (by decide) (Or.inr ⟨by decide, Or.inr (by decide)⟩), ← stA_eq]; exact hrel0.st), + (fun e he => by unfold tBlockA; rw [hTcbEq u hu _ (by simp) (by simp) (Or.inr ⟨by decide, Or.inr (by decide)⟩)]; exact hrel0.blk e he), + (fun e d hs => by unfold tBadgeA tFlagsA; rw [hTcbEq u hu _ (by simp) (by simp) (Or.inr ⟨by decide, Or.inr (by decide)⟩), hTcbEq u hu _ (by simp) (by simp) (Or.inr ⟨by decide, Or.inr (by decide)⟩)]; exact hrel0.send e d hs), + (fun e r0 d hr0 => by unfold tReplyA; rw [hTcbEq u hu _ (by simp) (by simp) (Or.inr ⟨by decide, Or.inr (by decide)⟩)]; exact hrel0.recv e r0 d hr0), + (fun r0 hr0 => by unfold tReplyA; rw [hTcbEq u hu _ (by simp) (by simp) (Or.inr ⟨by decide, Or.inr (by decide)⟩)]; exact hrel0.reply r0 hr0), + (fun r' => ?_), + (by unfold prioA; rw [hTcbEq u hu _ (by simp) (by simp) (Or.inr ⟨by decide, Or.inr (by decide)⟩)]; exact hrel0.prio), + (by unfold tFaultA; rw [hTcbEq u hu _ (by simp) (by simp) (Or.inr ⟨by decide, Or.inr (by decide)⟩)]; exact hrel0.fault0), + (by unfold tFaultA; rw [hTcbEq u hu _ (by simp) (by simp) (Or.inr ⟨by decide, Or.inr (by decide)⟩)]; exact hrel0.faultT), + (by unfold tBoundA; rw [hTcbEq u hu _ (by simp) (by simp) (Or.inr ⟨by decide, Or.inr (by decide)⟩)]; exact hrel0.bound), + (by unfold FaultRel tFaultA tFw0A tFw1A tFw2A tLfA tLf0A tLf1A + rw [hTcbEq u hu _ (by simp) (by simp) (Or.inr ⟨by decide, Or.inr (by decide)⟩), + hTcbEq u hu _ (by simp) (by simp) (Or.inr ⟨by decide, Or.inr (by decide)⟩), + hTcbEq u hu _ (by simp) (by simp) (Or.inr ⟨by decide, Or.inr (by decide)⟩), + hTcbEq u hu _ (by simp) (by simp) (Or.inr ⟨by decide, Or.inr (by decide)⟩), + hTcbEq u hu _ (by simp) (by simp) (Or.inr ⟨by decide, Or.inr (by decide)⟩), + hTcbEq u hu _ (by simp) (by simp) (Or.inr ⟨by decide, Or.inr (by decide)⟩), + hTcbEq u hu _ (by simp) (by simp) (Or.inr ⟨by decide, Or.inr (by decide)⟩)] + exact hrel0.faultR)⟩ + by_cases er : r' = .spsrEl1 + · subst er; rw [L.mem_wr_same hal_spsr]; simp [dissociateTcbA, Function.update, spsr', hmem_spsr3] + rw [hrel0.regs .spsrEl1] + · have hr' : (regIdxA r').val < 384 := by have := regIdxA_lt r'; omega + have hreg'_eq := tcb_reg_addr_eqH (t := u) hr' hft + have hdv' : (⟨BitVec.ofNat 64 (256 + 8 * (regIdxA r').val)⟩ : U64).val = 256 + 8 * (regIdxA r').val := by + show (BitVec.ofNat 64 _).toNat = _; rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + have hal' : Aligned (Endp.regA u (regIdxA r')) := by + rw [hreg'_eq]; exact al_tcb u hu _ (by omega) (by omega) + have hne_idx : (regIdxA r').val ≠ (regIdxA .spsrEl1).val := fun e => er (regIdxA_inj e) + have hne_val : (Endp.regA u (regIdxA .spsrEl1)).val ≠ (Endp.regA u (regIdxA r')).val := by + rw [hspsr_val, hreg'_eq, wa_valm, hdv', Nat.mod_eq_of_lt (by omega)]; omega + have hfvc := J.ent.heap.fits vc _ hvH; simp only [HKind.bits] at hfvc + have hne_vt : vc ≠ u := fun e => by rw [e, htH] at hvH; cases hvH + have hdt := J.ent.heap.disj vc (.other 12) u .tcb hvH htH hne_vt + change vc.val + 4096 ≤ u.val ∨ u.val + 4096 ≤ vc.val at hdt + have hrv' : (Endp.regA u (regIdxA r')).val = u.val + (256 + 8 * (regIdxA r').val) := by + rw [hreg'_eq, wa_valm, hdv']; exact Nat.mod_eq_of_lt (by omega) + have hv8 : (wa vc 8#u64).val = vc.val + 8 := by rw [wa_valm]; change (vc.val + 8) % _ = _; exact Nat.mod_eq_of_lt (by omega) + have hv3 : (wa u 3664#u64).val = u.val + 3664 := by rw [wa_valm]; change (u.val + 3664) % _ = _; exact Nat.mod_eq_of_lt (by omega) + rw [L.mem_wr_ne hal_spsr hal' hne_val, L.mem_wr_ne hal_t3664 hal' (by omega), + L.mem_wr_ne hal_v8 hal' (by omega), L.mem_wr_ne hal_v0 hal' (by omega)] + simp [dissociateTcbA, Function.update, er]; exact hrel0.regs r' + · have huH : H u = some .tcb := show u ∈ H.tcbs by rw [J.tcbs]; exact hu + have hfu := J.ent.heap.fits u .tcb huH; change u.val + 4096 ≤ 2 ^ 64 at hfu + have hne1 : specPtr u ≠ specPtr t := fun e => eu (specPtr_inj e) + have hne2 : specPtr u ≠ specPtr vc := fun e => by + have : u = vc := specPtr_inj e; rw [this, hvH] at huH; cases huH + refine ⟨y0, by simp [σ', updKSc, Function.update, hne1, hne2, hky0], hrel0.frame (J.sq.tcbs u huH) fun b hb hbu => ?_⟩ + have hlt : b.val - u.val < 3664 := by have := hbu.2; unfold TOffN at this; omega + have hb_eq : b = wa u ⟨BitVec.ofNat 64 (b.val - u.val)⟩ := by + apply UScalar.eq_of_val_eq; rw [wa_valm] + have hdv' : (⟨BitVec.ofNat 64 (b.val - u.val)⟩ : U64).val = b.val - u.val := by + show (BitVec.ofNat 64 _).toNat = _; rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + rw [hdv', Nat.add_sub_of_le hbu.1]; exact (Nat.mod_eq_of_lt (by omega)).symm + have hdv' : (⟨BitVec.ofNat 64 (b.val - u.val)⟩ : U64).val = b.val - u.val := by + show (BitVec.ofNat 64 _).toNat = _; rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + have hua := J.ent.heap.align u .tcb huH; change u.val % 4096 = 0 at hua + have h8 : (b.val - u.val) % 8 = 0 := by have : b.val % 8 = 0 := hb; omega + rw [hb_eq]; exact hTcbEq u hu _ (by omega) (by omega) (Or.inl eu) + have hEpBuf : ∀ bp ∈ Bf, ∀ k : U64, k.val < 128 → + (L.mem s4 (bufW bp.1 k)).bv = loadWordAt σ'.machineState.underlyingMemory (bufAddr bp.2 k.val) ∧ + (bufAddr bp.2 k.val).toNat % 8 = 0 := fun bp hbp k hk => by + have ⟨h1, h2⟩ := J.ep.buf bp hbp k hk + have hbw : BufW Bf (bufW bp.1 k) := ⟨bp, hbp, k, hk, rfl⟩ + have hmem_bw : L.mem s4 (bufW bp.1 k) = L.mem s (bufW bp.1 k) := by + refine hmem_other _ (J.iplaced.e.balign _ hbw) + (by have := J.bufOff.vcw _ hbw vc hvc 0#u64 (by decide) (by decide); rwa [wa_zeroH] at this) + (by + intro e + have h8v : (8#u64 : U64).val = 8 := rfl + have hsz : UScalar.size UScalarTy.U64 = 2 ^ 64 := by simp [U64.size_def, U64.numBits] + have hfvc := J.ent.heap.fits vc _ hvH; simp only [HKind.bits] at hfvc + have hkm : (core.num.U64.wrapping_mul k 8#u64).val = 8 * k.val := by + rw [core.num.U64.wrapping_mul_val_eq, h8v, hsz, Nat.mod_eq_of_lt (by omega)]; omega + have hvc8 : (wa vc 8#u64).val = vc.val + 8 := by + rw [wa_valm, h8v]; exact Nat.mod_eq_of_lt (by omega) + have hev : (bp.1.val + 8 * k.val) % 2 ^ 64 = vc.val + 8 := by + have h0 := congrArg UScalar.val e + unfold bufW at h0 + rw [wa_valm, hkm, hvc8] at h0 + exact h0 + by_cases hk0 : k.val = 0 + · have hk2 : (2#u64 : U64).val < 128 := by decide + have h24 : bufW bp.1 2#u64 = wa vc 24#u64 := by + apply UScalar.eq_of_val_eq + unfold bufW + have h2m : (core.num.U64.wrapping_mul 2#u64 8#u64).val = 16 := by decide + have h24v : (24#u64 : U64).val = 24 := rfl + have hvc24 : (wa vc 24#u64).val = vc.val + 24 := by + rw [wa_valm, h24v]; exact Nat.mod_eq_of_lt (by omega) + rw [hk0] at hev + have hbp : bp.1.val = vc.val + 8 := by + have hlt : bp.1.val < 2 ^ 64 := bp.1.hBounds + rw [mul_zero, add_zero, Nat.mod_eq_of_lt hlt] at hev; exact hev + rw [wa_valm, h2m, hbp, hvc24, Nat.mod_eq_of_lt (by omega)] + exact J.bufOff.vcw _ ⟨bp, hbp, 2#u64, hk2, rfl⟩ vc hvc 24#u64 (by decide) (by decide) h24 + · set k' : U64 := ⟨BitVec.ofNat 64 (k.val - 1)⟩ + have hk'v : k'.val = k.val - 1 := by + show (BitVec.ofNat 64 (k.val - 1)).toNat = _; rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + have hk' : k'.val < 128 := by omega + have h00 : bufW bp.1 k' = wa vc 0#u64 := by + apply UScalar.eq_of_val_eq + unfold bufW + have hk'm : (core.num.U64.wrapping_mul k' 8#u64).val = 8 * (k.val - 1) := by + rw [core.num.U64.wrapping_mul_val_eq, h8v, hsz, hk'v] + have hlt : (k.val - 1) * 8 < 2 ^ 64 := by omega + rw [Nat.mod_eq_of_lt hlt]; omega + rw [wa_valm, hk'm, wa_zeroH] + have h1 : bp.1.val + 8 * k.val = bp.1.val + 8 * (k.val - 1) + 8 := by omega + rw [h1] at hev + have hlt : vc.val + 8 < 2 ^ 64 := by omega + omega + exact J.bufOff.vcw _ ⟨bp, hbp, k', hk', rfl⟩ vc hvc 0#u64 (by decide) (by decide) h00) + (by + have hbt := J.iplaced.e.bt _ hbw t ht + have hv3 : (wa t 3664#u64).val = t.val + 3664 := by rw [wa_valm]; change (t.val + 3664) % _ = _; exact Nat.mod_eq_of_lt (by omega) + intro e; rw [e, hv3] at hbt; omega) + (by + have hbt := J.iplaced.e.bt _ hbw t ht + intro e; rw [e, hspsr_val] at hbt; omega) + rw [hmem_bw]; exact ⟨h1, h2⟩ + have hDeadVcpu : ∀ v ∈ Z.V, DeadA st OS .vcpu s4 v → ∀ x, σ'.kheap (specPtr v) = some (.archObj (.vcpu x)) → + x.vcpuTcb = none := fun v hv hd x hx => by + have hd0 : DeadA st OS .vcpu s v := fun y hy hc => hd y hy (by rw [capE y hy]; exact hc) + by_cases ev : v = vc + · subst ev; simp [σ', updKSc, Function.update] at hx; subst hx; rfl + · have hne1 : specPtr v ≠ specPtr t := fun e => by + have : v = t := specPtr_inj e; have hv' := arm.vh v hv; rw [this, htH] at hv'; cases hv' + have hne2 : specPtr v ≠ specPtr vc := fun e => ev (specPtr_inj e) + simp [σ', updKSc, Function.update, hne1, hne2] at hx + exact J.deadExt.vcpu v hv hd0 x hx + have hDeadTvcpu : ∀ u ∈ Z.T, DeadA st OS .tcb s4 u → L.mem s4 (wa u 3664#u64) = 0#u64 := fun u hu hd => by + by_cases eu : u = t + · subst eu; exact hmem_t3664 + · rw [hTcbEq u hu 3664#u64 (by decide) (by decide) (Or.inl eu)] + exact J.deadExt.tvcpu u hu (fun y hy hc => hd y hy (by rw [capE y hy]; exact hc)) + have hDeadVcRun : ∀ v ∈ Z.V, DeadA st OS .vcpu s4 v → L.mem s4 (wa v 24#u64) = 0#u64 := fun v hv hd => by + rw [hVcEq v hv 24#u64 (by decide) (by decide) (Or.inr ⟨by decide, by decide⟩)] + exact J.deadExt.vcDeadRun v hv (fun y hy hc => hd y hy (by rw [capE y hy]; exact hc)) + have hZombNoVc : ZombieTcbNoVcpuA st OS s4 := fun y hy u num hz => by + have hz0 : capA st s y = .Zombie u capability.ZOMBIE_TCB num := by rwa [capE y hy] at hz + have huT : u ∈ Z.T := J.tcbs ▸ J.zombieTcb y hy u num hz0 + by_cases eu : u = t + · subst eu; exact hmem_t3664 + · rw [hTcbEq u huT 3664#u64 (by decide) (by decide) (Or.inl eu)] + exact J.zombieNoVcpu y hy u num hz0 + have hUtVs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → L.mem s4 b = L.mem s b := fun b hb => by + have hno := utVs_off_vcpuNoFrA J b hb + exact hmem_other b (J.vsOk.al b hb) (fun e => hno (Or.inl ⟨vc, hvc, Or.inl e⟩)) + (fun e => hno (Or.inl ⟨vc, hvc, Or.inr (Or.inl e)⟩)) + (fun e => hno (Or.inr ⟨t, ht, 3664#u64, by decide, Or.inr (Or.inl rfl), e⟩)) + (fun e => hno (Or.inr ⟨t, ht, ⟨BitVec.ofNat 64 (256 + 8 * (regIdxA .spsrEl1).val)⟩, by omega, Or.inl ⟨by omega, by omega⟩, by rwa [← hspsr_eq]⟩)) + have J' := JStage3TA.of_vcpuStepA J R6 hw hHS J.j.1.ar hUtVs hVcCore hVcRun hRunVcpu + hEpTcb hEpBuf hDeadVcpu hDeadTvcpu hDeadVcRun hZombNoVc + have hSV' := SchedValidA.of_vcpuStepA hSV hHS + have hSnap' := VcpuStateSnapA.of_vcpuStepA J R6.snap hw hHS (fun v hv => by + by_cases ev : v = vc + · subst ev; rw [hmem_v8]; simp + · rw [hVcEq v hv 8#u64 (by decide) (by decide) (Or.inl ev)]; exact JStage3TA.ownLeA J R6.snap v hv) + have hFrOff' : FrOff st Z.Cs OS (L.ksb s4) s4 := by + rw [hw.1]; intro u hu p0 sz0 r0 mp0 ept0 a0 va0 hc0 b0 hb0 + exact R6.frOff u hu p0 sz0 r0 mp0 ept0 a0 va0 ((capE u hu).symm.trans hc0) b0 hb0 + have hFrTh' : FrThA st Z.th OS s4 := R6.frTh.keepA capE fun _ _ e => e + have hUserFr' : UserFrSpecA st OS s4 σ' := + R6.userFr.keepA capE fun _ _ _ h => hHS.keepObj h (fun _ e => by cases e) (fun _ e => by cases e) + have hR6' : VcpuRec6A st Z OS Bf s4 σ' := by + refine ⟨hSnap', fun u hu => ?_, fun v hv => ?_, fun v hv hv0 => ?_, fun u hu hu0 => ?_, + R6.vcOS, hFrOff', hUserFr', hFrTh', R6.bfSpec⟩ + · by_cases eu : u = t + · subst eu; intro h; exact absurd hmem_t3664 h + · rw [hTcbEq u hu 3664#u64 (by decide) (by decide) (Or.inl eu)] + intro hne + have hvV' := (J.vcRun.tcb u hu).resolve_left hne + have hne_v : L.mem s (wa u 3664#u64) ≠ vc := fun e => by + have := R6.vcSym1 u hu hne; rw [e, hlink] at this; exact eu this.symm + have h0 := hVcEq _ hvV' 0#u64 (by decide) (by decide) (Or.inl hne_v); rw [wa_zeroH] at h0 + rw [h0]; exact R6.vcSym1 u hu hne + · rw [hVcEq v hv 24#u64 (by decide) (by decide) (Or.inr ⟨by decide, by decide⟩)] + intro hne + have huT := (J.vcRun.vcpu v hv).resolve_left hne + unfold runVcpuA; rw [hTcbEq _ huT 3672#u64 (by decide) (by decide) (Or.inr ⟨by decide, Or.inr (by decide)⟩)] + exact R6.vcSym2 v hv hne + · by_cases ev : v = vc + · subst ev; exact absurd hmem_v0 hv0 + · have h0 := hVcEq v hv 0#u64 (by decide) (by decide) (Or.inl ev); rw [wa_zeroH] at h0 + rw [h0] at hv0 ⊢ + have huT := (J.vcRun.vtcb v hv).resolve_left hv0 + have hne_ut : L.mem s v ≠ t := fun e => by + have := R6.vcBack v hv hv0; rw [e, hback] at this; exact ev this.symm + rw [hTcbEq _ huT 3664#u64 (by decide) (by decide) (Or.inl hne_ut)] + exact R6.vcBack v hv hv0 + · unfold runVcpuA at hu0 ⊢ + rw [hTcbEq u hu 3672#u64 (by decide) (by decide) (Or.inr ⟨by decide, Or.inr (by decide)⟩)] at hu0 ⊢ + have hvV' : L.mem s (wa u 3672#u64) ∈ Z.V := (J.vcRun.run u hu).resolve_left hu0 + rw [hVcEq _ hvV' 24#u64 (by decide) (by decide) (Or.inr ⟨by decide, by decide⟩)] + exact R6.runBack u hu hu0 + exact ⟨hsp, J', hSV', hR6', hCl, hmem_v0, hmem_t3664, + hVcEq vc hvc 24#u64 (by decide) (by decide) (Or.inr ⟨by decide, by decide⟩), hTcbEq, hw.1, hw.2.1, osk, + FrKeepA.kerA J osk rfl fun b al nv nt => by + refine ((((writesIn_wr hal_v0).trans (writesIn_wr hal_v8)).trans (writesIn_wr hal_t3664)).trans + (writesIn_wr hal_spsr)).mem_eq al fun h => ?_ + rcases h with ((h | h) | h) | h + · exact nv ⟨vc, hvc, 0#u64, by decide, by decide, h.trans (wa_zeroH vc).symm⟩ + · exact nv ⟨vc, hvc, 8#u64, by decide, by decide, h⟩ + · exact nt t ht 3664#u64 (by decide) h + · exact nt t ht ⟨BitVec.ofNat 64 (256 + 8 * (regIdxA .spsrEl1).val)⟩ + (by change (BitVec.ofNat 64 _).toNat < 4096; rw [BitVec.toNat_ofNat]; omega) + (h.trans (tcb_reg_addr_eqH hr_spsr hft))⟩ + +/-- Optional `vspace.set_vm_root` when `t = cur_thread`: leaves store and spec state unchanged, +updating only `Z.hw` and preserving `Clock`. -/ +private theorem vcpu_set_vm_root_if_cur_coreA + {AR : A → HwRootA → Prop} {Clock : A → Fl4SpecA64.MachineState → Prop} + {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J7 : JSlotA S} + [J7PpA st J7] [J7PpKA st J7] [J7FrA st J7] + {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3VcpuCoreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + (R6 : VcpuRec6A st Z OS Bf s σ) + (hCl : Clock a σ.machineState) + (hClVm : ∀ s' a' ms (u : U64), Clock a' ms → vspace.set_vm_root st ar s' a' u ⦃ r => Clock r.2 ms ⦄) + (hVR : VcpuVmRootA st ar AR) (u : U64) : + (do + let cur ← sched.cur_thread st s + let b ← obj.Tcb.Insts.CoreCmpPartialEqTcb.eq u cur + if b then vspace.set_vm_root st ar s a u else ok (s, a)) ⦃ res => + res.1 = s ∧ ∃ hw' : HwRootA, + JStage3VcpuCoreA st AR { Z with hw := hw' } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s res.2 σ m ∧ + Clock res.2 σ.machineState ⦄ := by + refine WP.spec_bind (sched.cur_thread.spec s) ?_ + intro cur hcur + unfold obj.Tcb.Insts.CoreCmpPartialEqTcb.eq + simp only [bind_ok] + by_cases heq : u = cur + · rw [if_pos (decide_eq_true heq)] + refine WP.spec_mono (WP.spec_and (hVR.set_vm_root s a Z.hw u J.j.1.ar) (hClVm s a σ.machineState u hCl)) ?_ + rintro ⟨s', a'⟩ ⟨⟨hs', hw', har'⟩, hCl'⟩ + dsimp only at hs' har' hCl' + rcases hs' with rfl + refine ⟨rfl, hw', ?_, hCl'⟩ + exact JStage3VcpuCoreA.of_vcpuStepA J R6 (WritesIn.refl s' _) (VcpuSpecStepA.refl H σ) har' + (fun _ _ => rfl) ⟨J.j.2.1.obj, J.j.2.1.back, J.j.2.1.tcb, J.j.2.1.cap⟩ J.vcRun J.runVcpu J.ep.tcb J.ep.buf + J.deadExt.vcpu J.deadExt.tvcpu J.deadExt.vcDeadRun J.zombieNoVcpu + · rw [decide_eq_false heq]; simp only [Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + exact ⟨trivial, Z.hw, J, hCl⟩ + +/-- Single-register opaque step wrapper around `vcpu_tcb_reg_wr_coreA`, keeping `σ'` and `x'` +atomic so multi-register chains never build nested `updKSc` / `Tcb` record updates. -/ +private theorem vcpu_tcb_reg_wr_step_coreA + {AR : A → HwRootA → Prop} {Clock : A → Fl4SpecA64.MachineState → Prop} + {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J7 : JSlotA S} + [J7PpA st J7] [J7PpKA st J7] [J7FrA st J7] + {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3VcpuCoreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + (hSV : SchedValidA σ) (R6 : VcpuRec6A st Z OS Bf s σ) (hCl : Clock a σ.machineState) + {t : U64} (ht : t ∈ Z.T) {xt : Fl4SpecA64.Tcb} + (hkt : σ.kheap (specPtr t) = some (.tcb xt)) (r : Fl4SpecA64.Register) (v : U64) : + ∃ (σ' : Fl4SpecA64.State Unit) (x' : Fl4SpecA64.Tcb), + asUser (Z := Unit) (specPtr t) (setRegister r v.bv) σ = ({((), σ')}, False) ∧ + σ'.kheap (specPtr t) = some (.tcb x') ∧ + (∀ p, p ≠ specPtr t → σ'.kheap p = σ.kheap p) ∧ + x'.tcbState = xt.tcbState ∧ + x'.tcbArch.tcbVcpu = xt.tcbArch.tcbVcpu ∧ + x'.tcbBoundNotification = xt.tcbBoundNotification ∧ + x'.tcbSchedContext = xt.tcbSchedContext ∧ + JStage3VcpuCoreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 (L.wr s (Endp.regA t (regIdxA r)) v) a σ' m ∧ + VcpuSpecStepA H σ σ' ∧ + WritesIn st s (L.wr s (Endp.regA t (regIdxA r)) v) (VcpuWrNoStA st Z OS s) ∧ + SchedValidA σ' ∧ + VcpuRec6A st Z OS Bf (L.wr s (Endp.regA t (regIdxA r)) v) σ' ∧ + Clock a σ'.machineState ∧ + σ'.machineState = σ.machineState ∧ + (∀ vc ∈ Z.V, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → + L.mem (L.wr s (Endp.regA t (regIdxA r)) v) (wa vc o) = L.mem s (wa vc o)) ∧ + (∀ u ∈ Z.T, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → + (u ≠ t ∨ o.val < 256 ∨ 3328 ≤ o.val) → + L.mem (L.wr s (Endp.regA t (regIdxA r)) v) (wa u o) = L.mem s (wa u o)) ∧ + L.ksb (L.wr s (Endp.regA t (regIdxA r)) v) = L.ksb s ∧ + L.cpu (L.wr s (Endp.regA t (regIdxA r)) v) = L.cpu s ∧ + OSKeep st OS s (L.wr s (Endp.regA t (regIdxA r)) v) ∧ + FrKeepA st OS s σ (L.wr s (Endp.regA t (regIdxA r)) v) σ' := by + obtain ⟨hsp, J', hHS, hw, hSV', R6', hCl', hvc, htcb, hksb, hcpu, osk, fk⟩ := + vcpu_tcb_reg_wr_coreA J hSV R6 hCl ht hkt r v + let x' : Fl4SpecA64.Tcb := { xt with tcbArch := { xt.tcbArch with tcbContext := + ⟨xt.tcbArch.tcbContext.userFpuState, Function.update xt.tcbArch.tcbContext.userRegs r v.bv⟩ } } + exact ⟨updKSc σ (specPtr t) (.tcb x'), x', hsp, updKSc_same _ _ _, fun p hp => updKSc_other _ hp, + rfl, rfl, rfl, rfl, J', hHS, hw, hSV', R6', hCl', rfl, hvc, htcb, hksb, hcpu, osk, fk⟩ + +/-- The 4-register payload write (`X2 := stop`, `X3 := w1`, `X4 := w2`, `X5 := w3`). -/ +private theorem vcpu_complete_run_4regs_coreA + {AR : A → HwRootA → Prop} {Clock : A → Fl4SpecA64.MachineState → Prop} + {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J7 : JSlotA S} + [J7PpA st J7] [J7PpKA st J7] [J7FrA st J7] + {s4 : S} {a : A} {σ4 : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J4 : JStage3VcpuCoreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s4 a σ4 m) + (hSV4 : SchedValidA σ4) (R6_4 : VcpuRec6A st Z OS Bf s4 σ4) (hCl4 : Clock a σ4.machineState) + {ct : U64} (hct : ct ∈ Z.T) {x4 : Fl4SpecA64.Tcb} + (hkc4 : σ4.kheap (specPtr ct) = some (.tcb x4)) + {stop : U64} {stopSpec : RunStop} (hstop : stop.bv = stopSpec.word) (w1 w2 w3 : U64) : + let s8 := L.wr (L.wr (L.wr (L.wr s4 (Endp.regA ct (regIdxA .x2)) stop) + (Endp.regA ct (regIdxA .x3)) w1) (Endp.regA ct (regIdxA .x4)) w2) (Endp.regA ct (regIdxA .x5)) w3 + ∃ (σ8 : Fl4SpecA64.State Unit) (x8 : Fl4SpecA64.Tcb), + asUser (Z := Unit) (specPtr ct) (do + setRegister .x2 stopSpec.word + setRegister .x3 w1.bv + setRegister .x4 w2.bv + setRegister .x5 w3.bv) σ4 = ({((), σ8)}, False) ∧ + σ8.kheap (specPtr ct) = some (.tcb x8) ∧ + (∀ p, p ≠ specPtr ct → σ8.kheap p = σ4.kheap p) ∧ + x8.tcbState = x4.tcbState ∧ + x8.tcbArch.tcbVcpu = x4.tcbArch.tcbVcpu ∧ + x8.tcbBoundNotification = x4.tcbBoundNotification ∧ + x8.tcbSchedContext = x4.tcbSchedContext ∧ + JStage3VcpuCoreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s8 a σ8 m ∧ + (SActA σ4 → SActA σ8) ∧ + WritesIn st s4 s8 (VcpuWrNoStA st Z OS s4) ∧ + SchedValidA σ8 ∧ VcpuRec6A st Z OS Bf s8 σ8 ∧ Clock a σ8.machineState ∧ + σ8.machineState = σ4.machineState ∧ + (∀ v ∈ Z.V, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → L.mem s8 (wa v o) = L.mem s4 (wa v o)) ∧ + (∀ u ∈ Z.T, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → + (u ≠ ct ∨ o.val < 256 ∨ 3328 ≤ o.val) → L.mem s8 (wa u o) = L.mem s4 (wa u o)) ∧ + L.ksb s8 = L.ksb s4 ∧ L.cpu s8 = L.cpu s4 ∧ OSKeep st OS s4 s8 ∧ + FrKeepA st OS s4 σ4 s8 σ8 := by + dsimp only + have hOskT : ∀ {a b c : S}, OSKeep st OS a b → OSKeep st OS b c → OSKeep st OS a c := fun h1 h2 y hy => + ⟨(h2 y hy).1.trans (h1 y hy).1, (h2 y hy).2.1.trans (h1 y hy).2.1, + (h2 y hy).2.2.1.trans (h1 y hy).2.2.1, (h2 y hy).2.2.2.trans (h1 y hy).2.2.2⟩ + have hWrKeep : ∀ {sA sB : S} (osk : OSKeep st OS sA sB) {b}, VcpuWrNoStA st Z OS sB b → VcpuWrNoStA st Z OS sA b := by + intro sA sB osk b hb + rcases hb with h | h | ⟨y, hy, ptr, sz, r, mp, ept, asid, va, bits, hc, hbits, hal, h1, h2⟩ + · exact Or.inl h + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr ⟨y, hy, ptr, sz, r, mp, ept, asid, va, bits, by rw [← osk.capA hy]; exact hc, hbits, hal, h1, h2⟩) + obtain ⟨σ5, x5, hsp5, hkc5, hne5, hst5, hv5, hb5, hsc5, J5, hHS5, hw5, hSV5, R6_5, hCl5, hms5, hvc5, htcb5, hksb5, hcpu5, osk5, fk5⟩ := + vcpu_tcb_reg_wr_step_coreA J4 hSV4 R6_4 hCl4 hct hkc4 .x2 stop + obtain ⟨σ6, x6, hsp6, hkc6, hne6, hst6, hv6, hb6, hsc6, J6, hHS6, hw6, hSV6, R6_6, hCl6, hms6, hvc6, htcb6, hksb6, hcpu6, osk6, fk6⟩ := + vcpu_tcb_reg_wr_step_coreA J5 hSV5 R6_5 hCl5 hct hkc5 .x3 w1 + obtain ⟨σ7, x7, hsp7, hkc7, hne7, hst7, hv7, hb7, hsc7, J7', hHS7, hw7, hSV7, R6_7, hCl7, hms7, hvc7, htcb7, hksb7, hcpu7, osk7, fk7⟩ := + vcpu_tcb_reg_wr_step_coreA J6 hSV6 R6_6 hCl6 hct hkc6 .x4 w2 + obtain ⟨σ8, x8, hsp8, hkc8, hne8, hst8, hv8, hb8, hsc8, J8, hHS8, hw8, hSV8, R6_8, hCl8, hms8, hvc8, htcb8, hksb8, hcpu8, osk8, fk8⟩ := + vcpu_tcb_reg_wr_step_coreA J7' hSV7 R6_7 hCl7 hct hkc7 .x5 w3 + have hw48 : WritesIn st s4 _ (VcpuWrNoStA st Z OS s4) := + (((hw5.trans (hw6.mono (fun _ h => hWrKeep osk5 h))).mono fun _ h => h.elim id id).trans + (hw7.mono (fun _ h => hWrKeep (hOskT osk5 osk6) h)) |>.mono fun _ h => h.elim id id).trans + (hw8.mono (fun _ h => hWrKeep (hOskT (hOskT osk5 osk6) osk7) h)) |>.mono fun _ h => h.elim id id + have hsp78 : asUser (Z := Unit) (specPtr ct) (do setRegister .x4 w2.bv; setRegister .x5 w3.bv) σ6 = ({((), σ8)}, False) := + asUser_seqH hkc6 rfl rfl hsp7 hsp8 + have hsp68 : asUser (Z := Unit) (specPtr ct) (do setRegister .x3 w1.bv; setRegister .x4 w2.bv; setRegister .x5 w3.bv) σ5 = ({((), σ8)}, False) := + asUser_seqH hkc5 rfl (by rw [nd_bind_oneSc (m := setRegister .x4 w2.bv) rfl]; rfl) hsp6 hsp78 + have hsp58 : asUser (Z := Unit) (specPtr ct) (do setRegister .x2 stopSpec.word; setRegister .x3 w1.bv; setRegister .x4 w2.bv; setRegister .x5 w3.bv) σ4 = ({((), σ8)}, False) := by + rw [← hstop] + exact asUser_seqH hkc4 rfl (by rw [nd_bind_oneSc (m := setRegister .x3 w1.bv) rfl, nd_bind_oneSc (m := setRegister .x4 w2.bv) rfl]; rfl) hsp5 hsp68 + exact ⟨σ8, x8, hsp58, hkc8, fun p hp => (hne8 p hp).trans ((hne7 p hp).trans ((hne6 p hp).trans (hne5 p hp))), + hst8.trans (hst7.trans (hst6.trans hst5)), hv8.trans (hv7.trans (hv6.trans hv5)), + hb8.trans (hb7.trans (hb6.trans hb5)), hsc8.trans (hsc7.trans (hsc6.trans hsc5)), + J8, fun hs => (((hs.of_vcpuStepA hHS5).of_vcpuStepA hHS6).of_vcpuStepA hHS7).of_vcpuStepA hHS8, hw48, + hSV8, R6_8, hCl8, hms8.trans (hms7.trans (hms6.trans hms5)), + fun v hv o h1 h8 => (hvc8 v hv o h1 h8).trans ((hvc7 v hv o h1 h8).trans ((hvc6 v hv o h1 h8).trans (hvc5 v hv o h1 h8))), + fun u hu o h1 h8 hn => (htcb8 u hu o h1 h8 hn).trans ((htcb7 u hu o h1 h8 hn).trans ((htcb6 u hu o h1 h8 hn).trans (htcb5 u hu o h1 h8 hn))), + hksb8.trans (hksb7.trans (hksb6.trans hksb5)), hcpu8.trans (hcpu7.trans (hcpu6.trans hcpu5)), + hOskT (hOskT (hOskT osk5 osk6) osk7) osk8, + ((fk5.trans fk6).trans fk7).trans fk8⟩ + +/-- The 7-register reply sequence of `vcpu.complete_run` (`BADGE := 0`, `X2 := stop`, +`X3 := w1`, `X4 := w2`, `X5 := w3`, `MSG_INFO := 4`, `FAULT_IP := NEXT_IP`) packaged +with an opaque post-state `σ10` / `x10`. -/ +private theorem vcpu_complete_run_7regs_coreA + {AR : A → HwRootA → Prop} {Clock : A → Fl4SpecA64.MachineState → Prop} + {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J7 : JSlotA S} + [J7PpA st J7] [J7PpKA st J7] [J7FrA st J7] + {s3 : S} {a : A} {σ3 : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J3 : JStage3VcpuCoreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s3 a σ3 m) + (hSV3 : SchedValidA σ3) + (R6_3 : VcpuRec6A st Z OS Bf s3 σ3) + (hCl3 : Clock a σ3.machineState) + {ct : U64} (hct : ct ∈ Z.T) {x3 : Fl4SpecA64.Tcb} + (hkc3 : σ3.kheap (specPtr ct) = some (.tcb x3)) + {stop : U64} {stopSpec : RunStop} (hstop : stop.bv = stopSpec.word) (w1 w2 w3 : U64) : + let s4 := L.wr s3 (Endp.regA ct (regIdxA .x0)) 0#u64 + let s5 := L.wr s4 (Endp.regA ct (regIdxA .x2)) stop + let s6 := L.wr s5 (Endp.regA ct (regIdxA .x3)) w1 + let s7 := L.wr s6 (Endp.regA ct (regIdxA .x4)) w2 + let s8 := L.wr s7 (Endp.regA ct (regIdxA .x5)) w3 + let s9 := L.wr s8 (Endp.regA ct (regIdxA .x1)) 4#u64 + let s10 := L.wr s9 (Endp.regA ct (regIdxA .faultIP)) (L.mem s9 (Endp.regA ct (regIdxA .nextIP))) + ∃ (σ10 : Fl4SpecA64.State Unit) (x10 : Fl4SpecA64.Tcb), + (∀ {α : Type} (k : Unit → NonDet (Fl4SpecA64.State Unit) α), (do + asUser (Z := Unit) (specPtr ct) (setRegister badge_register 0) + asUser (Z := Unit) (specPtr ct) do + setRegister .x2 stopSpec.word + setRegister .x3 w1.bv + setRegister .x4 w2.bv + setRegister .x5 w3.bv + setMessageInfo (Z := Unit) (specPtr ct) ⟨4, 0, 0, 0⟩ + asUser (Z := Unit) (specPtr ct) do + let ip ← getRegister nextInstructionRegister + setRegister faultRegister ip + k ()) σ3 = k () σ10) ∧ + σ10.kheap (specPtr ct) = some (.tcb x10) ∧ + (∀ p, p ≠ specPtr ct → σ10.kheap p = σ3.kheap p) ∧ + x10.tcbState = x3.tcbState ∧ + x10.tcbArch.tcbVcpu = x3.tcbArch.tcbVcpu ∧ + x10.tcbBoundNotification = x3.tcbBoundNotification ∧ + x10.tcbSchedContext = x3.tcbSchedContext ∧ + JStage3VcpuCoreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s10 a σ10 m ∧ + (SActA σ3 → SActA σ10) ∧ + WritesIn st s3 s10 (VcpuWrNoStA st Z OS s3) ∧ + SchedValidA σ10 ∧ + VcpuRec6A st Z OS Bf s10 σ10 ∧ + Clock a σ10.machineState ∧ + σ10.machineState = σ3.machineState ∧ + (∀ v ∈ Z.V, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → L.mem s10 (wa v o) = L.mem s3 (wa v o)) ∧ + (∀ u ∈ Z.T, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → + (u ≠ ct ∨ o.val < 256 ∨ 3328 ≤ o.val) → L.mem s10 (wa u o) = L.mem s3 (wa u o)) ∧ + L.ksb s10 = L.ksb s3 ∧ L.cpu s10 = L.cpu s3 ∧ + OSKeep st OS s3 s10 ∧ + FrKeepA st OS s3 σ3 s10 σ10 := by + dsimp only + have hmi := msginfo_4_eqH + have hOskT : ∀ {a b c : S}, OSKeep st OS a b → OSKeep st OS b c → OSKeep st OS a c := fun h1 h2 y hy => + ⟨(h2 y hy).1.trans (h1 y hy).1, (h2 y hy).2.1.trans (h1 y hy).2.1, + (h2 y hy).2.2.1.trans (h1 y hy).2.2.1, (h2 y hy).2.2.2.trans (h1 y hy).2.2.2⟩ + have hWrKeep : ∀ {sA sB : S} (osk : OSKeep st OS sA sB) {b}, VcpuWrNoStA st Z OS sB b → VcpuWrNoStA st Z OS sA b := by + intro sA sB osk b hb + rcases hb with h | h | ⟨y, hy, ptr, sz, r, mp, ept, asid, va, bits, hc, hbits, hal, h1, h2⟩ + · exact Or.inl h + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr ⟨y, hy, ptr, sz, r, mp, ept, asid, va, bits, by rw [← osk.capA hy]; exact hc, hbits, hal, h1, h2⟩) + obtain ⟨σ4, x4, hsp4, hkc4, hne4, hst4, hv4, hb4, hsc4, J4, hHS4, hw4, hSV4, R6_4, hCl4, hms4, hvc4, htcb4, hksb4, hcpu4, osk4, fk4⟩ := + vcpu_tcb_reg_wr_step_coreA J3 hSV3 R6_3 hCl3 hct hkc3 .x0 0#u64 + obtain ⟨σ8, x8, hsp58, hkc8, hne8, hst8, hv8, hb8, hsc8, J8, hSAct8, hw8, hSV8, R6_8, hCl8, hms8, hvc8, htcb8, hksb8, hcpu8, osk8, fk8⟩ := + vcpu_complete_run_4regs_coreA J4 hSV4 R6_4 hCl4 hct hkc4 hstop w1 w2 w3 + obtain ⟨σ9, x9, hsp9, hkc9, hne9, hst9, hv9, hb9, hsc9, J9, hHS9, hw9, hSV9, R6_9, hCl9, hms9, hvc9, htcb9, hksb9, hcpu9, osk9, fk9⟩ := + vcpu_tcb_reg_wr_step_coreA J8 hSV8 R6_8 hCl8 hct hkc8 .x1 4#u64 + set s9 := L.wr (L.wr (L.wr (L.wr (L.wr (L.wr s3 (Endp.regA ct (regIdxA .x0)) 0#u64) + (Endp.regA ct (regIdxA .x2)) stop) (Endp.regA ct (regIdxA .x3)) w1) + (Endp.regA ct (regIdxA .x4)) w2) (Endp.regA ct (regIdxA .x5)) w3) (Endp.regA ct (regIdxA .x1)) 4#u64 + obtain ⟨σ10, x10, hsp10, hkc10, hne10, hst10, hv10, hb10, hsc10, J10, hHS10, hw10, hSV10, R6_10, hCl10, hms10, hvc10, htcb10, hksb10, hcpu10, osk10, fk10⟩ := + vcpu_tcb_reg_wr_step_coreA J9 hSV9 R6_9 hCl9 hct hkc9 .faultIP (L.mem s9 (Endp.regA ct (regIdxA .nextIP))) + have hw310 : WritesIn st s3 _ (VcpuWrNoStA st Z OS s3) := + (((hw4.trans (hw8.mono (fun _ h => hWrKeep osk4 h))).mono fun _ h => h.elim id id).trans + (hw9.mono (fun _ h => hWrKeep (hOskT osk4 osk8) h)) |>.mono fun _ h => h.elim id id).trans + (hw10.mono (fun _ h => hWrKeep (hOskT (hOskT osk4 osk8) osk9) h)) |>.mono fun _ h => h.elim id id + have hsp : ∀ {α : Type} (k : Unit → NonDet (Fl4SpecA64.State Unit) α), (do + asUser (Z := Unit) (specPtr ct) (setRegister badge_register 0) + asUser (Z := Unit) (specPtr ct) do + setRegister .x2 stopSpec.word + setRegister .x3 w1.bv + setRegister .x4 w2.bv + setRegister .x5 w3.bv + setMessageInfo (Z := Unit) (specPtr ct) ⟨4, 0, 0, 0⟩ + asUser (Z := Unit) (specPtr ct) do + let ip0 ← getRegister nextInstructionRegister + setRegister faultRegister ip0 + k ()) σ3 = k () σ10 := by + intro α k + have hbr : badge_register = .x0 := rfl + have h0bv : U64.bv 0#u64 = 0 := rfl + rw [h0bv] at hsp4 + rw [hbr, nd_bind_oneSc hsp4, nd_bind_oneSc hsp58] + have hsmi : setMessageInfo (Z := Unit) (specPtr ct) ⟨4, 0, 0, 0⟩ σ8 = ({((), σ9)}, False) := by + unfold setMessageInfo + have hmi_reg : msg_info_register = .x1 := rfl + rw [hmi_reg, ← hmi.2.2]; exact hsp9 + rw [nd_bind_oneSc hsmi] + have hget_ip : (L.mem s9 (Endp.regA ct (regIdxA .nextIP))).bv = x9.tcbArch.tcbContext.userRegs .nextIP := + (J9.ep.at_tcb hct hkc9).regs .nextIP + have hnr : nextInstructionRegister = .nextIP := rfl + have hfr : faultRegister = .faultIP := rfl + rw [hnr, hfr] + have hf_eq : (do let ip0 ← getRegister .nextIP; setRegister .faultIP ip0) (archTcbContextGet x9.tcbArch) = + setRegister .faultIP (L.mem s9 (Endp.regA ct (regIdxA .nextIP))).bv (archTcbContextGet x9.tcbArch) := by + rw [nd_bind_oneSc (m := getRegister .nextIP) rfl]; dsimp only [archTcbContextGet]; rw [← hget_ip] + have hfault_eq : asUser (Z := Unit) (specPtr ct) (do let ip0 ← getRegister .nextIP; setRegister .faultIP ip0) σ9 = + asUser (Z := Unit) (specPtr ct) (setRegister .faultIP (L.mem s9 (Endp.regA ct (regIdxA .nextIP))).bv) σ9 := by + unfold asUser + have hg : NonDet.getsThe (getTcb (Z := Unit) (specPtr ct)) σ9 = ({(x9, σ9)}, False) := by + simp [NonDet.getsThe, getTcb, readObject, hkc9, NonDet.assertOpt]; rfl + rw [nd_bind_oneSc hg, nd_bind_oneSc (show (pure (archTcbContextGet x9.tcbArch) : SMonad Unit UserContext) σ9 = _ from rfl), hf_eq] + rw [nd_bind_oneSc hg, nd_bind_oneSc (show (pure (archTcbContextGet x9.tcbArch) : SMonad Unit UserContext) σ9 = _ from rfl)] + exact nd_bind_oneSc (f := fun _ => k ()) (hfault_eq.trans hsp10) + exact ⟨σ10, x10, hsp, hkc10, + fun p hp => (hne10 p hp).trans ((hne9 p hp).trans ((hne8 p hp).trans (hne4 p hp))), + hst10.trans (hst9.trans (hst8.trans hst4)), hv10.trans (hv9.trans (hv8.trans hv4)), + hb10.trans (hb9.trans (hb8.trans hb4)), hsc10.trans (hsc9.trans (hsc8.trans hsc4)), + J10, fun hs => ((hSAct8 (hs.of_vcpuStepA hHS4)).of_vcpuStepA hHS9).of_vcpuStepA hHS10, hw310, + hSV10, R6_10, hCl10, hms10.trans (hms9.trans (hms8.trans hms4)), + fun v hv o h1 h8 => (hvc10 v hv o h1 h8).trans ((hvc9 v hv o h1 h8).trans ((hvc8 v hv o h1 h8).trans (hvc4 v hv o h1 h8))), + fun u hu o h1 h8 hn => (htcb10 u hu o h1 h8 hn).trans ((htcb9 u hu o h1 h8 hn).trans ((htcb8 u hu o h1 h8 hn).trans (htcb4 u hu o h1 h8 hn))), + hksb10.trans (hksb9.trans (hksb8.trans hksb4)), hcpu10.trans (hcpu9.trans (hcpu8.trans hcpu4)), + hOskT (hOskT (hOskT osk4 osk8) osk9) osk10, + ((fk4.trans fk8).trans fk9).trans fk10⟩ + +/-! ## Section 2: `vcpu.complete_run` on `JStage3TA` and `JStage3A` -/ + +set_option maxHeartbeats 1200000 in +/-- Steps 1–4 of `vcpu.complete_run` (when `ct.RUN_VCPU ≠ 0`): `vcpu_flush_state`, optional +`read_state_to_frame`, clear `(vc.RUN_CALLER, ct.RUN_VCPU)`, and the 7 reply/IP register writes, +on `JStage3VcpuCoreA` (requiring no `SActA σ`). -/ +private theorem vcpu_complete_run_pre_sts_coreA + {AR : A → HwRootA → Prop} {Clock : A → Fl4SpecA64.MachineState → Prop} + {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J7 : JSlotA S} + [J7PpA st J7] [J7PpKA st J7] [J7FrA st J7] + {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3VcpuCoreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + (hSV : SchedValidA σ) + (R6 : VcpuRec6A st Z OS Bf s σ) + (hCl : Clock a σ.machineState) + (hClFlushState : ∀ a' ms (vc om : U64), Clock a' ms → ar.vcpu_flush_state a' vc om ⦃ a'' => Clock a'' ms ⦄) + (hClFr : ∀ a' ms ms', Clock a' ms → ms'.timeState = ms.timeState → + ms'.lastMachineTime = ms.lastMachineTime → Clock a' ms') + (hP : VcpuPlatAgreeA ar) (hAK : VcpuArchKeepsA ar AR) + {ct : U64} (hct : ct ∈ Z.T) {xct : Fl4SpecA64.Tcb} + (hkc : σ.kheap (specPtr ct) = some (.tcb xct)) + (hvc0 : L.mem s (runVcpuA ct) ≠ 0#u64) + {stop : U64} {stopSpec : RunStop} (hstop : stop.bv = stopSpec.word) (w1 w2 w3 : U64) + {k : S → A → Result (S × A)} {P : S × A → Prop} + (hk_cont : ∀ s10 a1, + (∃ (σ10 : Fl4SpecA64.State Unit) (x10 : Fl4SpecA64.Tcb), + fl4CompleteRun (Z := Unit) (specPtr ct) stopSpec w1.bv w2.bv w3.bv σ = + setThreadState (Z := Unit) (specPtr ct) .running σ10 ∧ + σ10.kheap (specPtr ct) = some (.tcb x10) ∧ + runnable x10.tcbState = true ∧ + x10.tcbArch.tcbVcpu = xct.tcbArch.tcbVcpu ∧ + x10.tcbBoundNotification = xct.tcbBoundNotification ∧ + x10.tcbSchedContext = xct.tcbSchedContext ∧ + (∀ u ∈ Z.T, u ≠ ct → σ10.kheap (specPtr u) = σ.kheap (specPtr u)) ∧ + JStage3VcpuCoreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s10 a1 σ10 m ∧ + (SActA σ → SActA σ10) ∧ + WritesIn st s s10 (VcpuWrNoStA st Z OS s) ∧ + SchedValidA σ10 ∧ + VcpuRec6A st Z OS Bf s10 σ10 ∧ + Clock a1 σ10.machineState ∧ + σ10.machineState.timeState = σ.machineState.timeState ∧ + σ10.machineState.lastMachineTime = σ.machineState.lastMachineTime ∧ + L.mem s10 (runVcpuA ct) = 0#u64 ∧ + L.mem s10 (wa (L.mem s (runVcpuA ct)) 24#u64) = 0#u64 ∧ + (∀ v ∈ Z.V, L.mem s10 v = L.mem s v) ∧ + (∀ u ∈ Z.T, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → + (u ≠ ct ∨ o ≠ 3672#u64) → (u ≠ ct ∨ o.val < 256 ∨ 3328 ≤ o.val) → + L.mem s10 (wa u o) = L.mem s (wa u o)) ∧ + L.ksb s10 = L.ksb s ∧ L.cpu s10 = L.cpu s ∧ + OSKeep st OS s s10 ∧ + FrKeepA st OS s σ s10 σ10) → k s10 a1 ⦃ P ⦄) : + let vc := L.mem s (runVcpuA ct) + (do + let i2 ← lift (wa vc vcpu.RUN_OFFSET) + let offset ← st.read s i2 + let i3 ← lift (wa vc vcpu.RUN_OUT_MASK) + let out_mask ← st.read s i3 + let a1 ← ar.vcpu_flush_state a vc out_mask + let s1 ← + if out_mask != 0#u64 then do + let s2 ← obj.Tcb.slot ct layout.tcb.SLOT_BUFFER + let c ← obj.Slot.cap st s2 s + match c with + | capability.Cap.Null => ok s + | capability.Cap.Untyped _ _ _ _ => ok s + | capability.Cap.Endpoint _ _ _ _ _ _ => ok s + | capability.Cap.Notification _ _ _ _ => ok s + | capability.Cap.Reply _ _ => ok s + | capability.Cap.CNode _ _ _ _ => ok s + | capability.Cap.Thread _ => ok s + | capability.Cap.IrqControl => ok s + | capability.Cap.IrqHandler _ => ok s + | capability.Cap.Zombie _ _ _ => ok s + | capability.Cap.Domain => ok s + | capability.Cap.SchedContext _ _ => ok s + | capability.Cap.SchedControl _ => ok s + | capability.Cap.Frame ptr size rights is_device _ _ _ _ => + let i4 ← capability.FrameSize.bits size + let i5 ← lift (i4 &&& 63#u64) + let sz ← 1#u64 <<< i5 + if is_device then ok s + else if rights = capability.VM_READ_WRITE then + let i6 ← lift (offset &&& 63#u64) + if i6 = 0#u64 then + if offset < sz then + let i7 ← sz - offset + if i7 >= fl4_abi.consts.vcpu_state_mask.SIZE then + let i8 ← lift (wa ptr offset) + vcpu.read_state_to_frame st ar s a1 vc i8 out_mask + else ok s + else ok s + else ok s + else ok s + | capability.Cap.PageTable _ _ _ _ => ok s + | capability.Cap.PageDirectory _ _ _ _ => ok s + | capability.Cap.Pdpt _ _ _ _ => ok s + | capability.Cap.Pml4 _ _ _ => ok s + | capability.Cap.Vcpu _ => ok s + | capability.Cap.Ept _ _ _ _ _ => ok s + | capability.Cap.AsidControl => ok s + | capability.Cap.AsidPool _ _ => ok s + | capability.Cap.IoPortControl => ok s + | capability.Cap.IoPort _ _ _ => ok s + | capability.Cap.TdxControl => ok s + | capability.Cap.HighVSpace _ _ _ => ok s + else ok s + let i4 ← lift (wa vc vcpu.RUN_CALLER) + let s2 ← st.write s1 i4 0#u64 + let i5 ← lift (runVcpuA ct) + let s3 ← st.write s2 i5 0#u64 + let s4 ← obj.Tcb.set_reg st ct s3 layout.reg.BADGE 0#u64 + let i6 ← msg.msg_register 0#u64 + let s5 ← obj.Tcb.set_reg st ct s4 i6 stop + let i7 ← msg.msg_register 1#u64 + let s6 ← obj.Tcb.set_reg st ct s5 i7 w1 + let i8 ← msg.msg_register 2#u64 + let s7 ← obj.Tcb.set_reg st ct s6 i8 w2 + let i9 ← msg.msg_register 3#u64 + let s8 ← obj.Tcb.set_reg st ct s7 i9 w3 + let info ← msginfo.MessageInfo.new 0#u64 0#u64 0#u64 4#u64 + let s9 ← msg.set_msg_info st s8 ct info + let ip ← obj.Tcb.reg st ct s9 layout.reg.NEXT_IP + let s10 ← obj.Tcb.set_reg st ct s9 layout.reg.FAULT_IP ip + k s10 a1) ⦃ P ⦄ := by + dsimp only + set vc := L.mem s (runVcpuA ct) + have hOskT : ∀ {a b c : S}, OSKeep st OS a b → OSKeep st OS b c → OSKeep st OS a c := fun h1 h2 y hy => + ⟨(h2 y hy).1.trans (h1 y hy).1, (h2 y hy).2.1.trans (h1 y hy).2.1, + (h2 y hy).2.2.1.trans (h1 y hy).2.2.1, (h2 y hy).2.2.2.trans (h1 y hy).2.2.2⟩ + have hWrKeep : ∀ {sA sB : S} (osk : OSKeep st OS sA sB) {b}, VcpuWrNoStA st Z OS sB b → VcpuWrNoStA st Z OS sA b := by + intro sA sB osk b hb + rcases hb with h | h | ⟨y, hy, ptr, sz, r, mp, ept, asid, va, bits, hc, hbits, hal, h1, h2⟩ + · exact Or.inl h + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr ⟨y, hy, ptr, sz, r, mp, ept, asid, va, bits, by rw [← osk.capA hy]; exact hc, hbits, hal, h1, h2⟩) + obtain ⟨xct0, hkc0, -, hrv_spec⟩ := J.j.2.1.tcb ct hct + rw [hkc] at hkc0; cases hkc0 + have hvcV : vc ∈ Z.V := (J.vcRun.run ct hct).resolve_left hvc0 + have hrc_eq : L.mem s (wa vc 24#u64) = ct := R6.runBack ct hct hvc0 + obtain ⟨xv, hkv, -, hrc_spec⟩ := J.j.2.1.obj vc hvcV + have hsnap_vc := R6.snap.snap vc hvcV xv hkv + have hro_eq : (L.mem s (wa vc 32#u64)).bv = xv.vcpuExt.runOffset := hsnap_vc.2.2.1 + have hrm_eq : (L.mem s (wa vc 40#u64)).bv = xv.vcpuExt.runOutMask := hsnap_vc.2.2.2.1 + have hst_ct_vm : L.mem s (stA ct) = sched.TS_RUNNING_VM := J.runVcpu ct hct hvc0 + have hct_st_spec : xct.tcbState = .ext .runningVM := by + have hcode := (J.ep.at_tcb hct hkc).st + rw [hst_ct_vm] at hcode + have h8 : sched.TS_RUNNING_VM.val = 8 := by unfold sched.TS_RUNNING_VM; rfl + rw [h8] at hcode + cases hs : xct.tcbState with + | ext e => cases e; rfl + | _ => rw [hs] at hcode; simp [tsCode] at hcode + have hro_off : vcpu.RUN_OFFSET = 32#u64 := by simp [vcpu.RUN_OFFSET] + have hrm_off : vcpu.RUN_OUT_MASK = 40#u64 := by simp [vcpu.RUN_OUT_MASK] + rw [hro_off, hrm_off, lift_okH, bind_ok, L.read_eq, bind_ok, lift_okH, bind_ok, L.read_eq, bind_ok] + set offset := L.mem s (wa vc 32#u64) + set out_mask := L.mem s (wa vc 40#u64) + refine WP.spec_bind (WP.spec_and (hAK.vcpu_flush_state a Z.hw vc out_mask J.j.1.ar) + (hClFlushState a σ.machineState vc out_mask hCl)) ?_ + rintro a1 ⟨har1, hCl1⟩ + have J1 : JStage3VcpuCoreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a1 σ m := + JStage3VcpuCoreA.of_vcpuStepA J R6 (WritesIn.refl s _) (VcpuSpecStepA.refl H σ) har1 + (fun _ _ => rfl) J.j.2.1 J.vcRun J.runVcpu J.ep.tcb J.ep.buf + J.deadExt.vcpu J.deadExt.tvcpu J.deadExt.vcDeadRun J.zombieNoVcpu + -- Optional read_state_to_frame step + have hReadStep : (if out_mask != 0#u64 then do + let s2 ← obj.Tcb.slot ct layout.tcb.SLOT_BUFFER + let c ← obj.Slot.cap st s2 s + match c with + | capability.Cap.Null => ok s + | capability.Cap.Untyped _ _ _ _ => ok s + | capability.Cap.Endpoint _ _ _ _ _ _ => ok s + | capability.Cap.Notification _ _ _ _ => ok s + | capability.Cap.Reply _ _ => ok s + | capability.Cap.CNode _ _ _ _ => ok s + | capability.Cap.Thread _ => ok s + | capability.Cap.IrqControl => ok s + | capability.Cap.IrqHandler _ => ok s + | capability.Cap.Zombie _ _ _ => ok s + | capability.Cap.Domain => ok s + | capability.Cap.SchedContext _ _ => ok s + | capability.Cap.SchedControl _ => ok s + | capability.Cap.Frame ptr size rights is_device _ _ _ _ => + let i4 ← capability.FrameSize.bits size + let i5 ← lift (i4 &&& 63#u64) + let sz ← 1#u64 <<< i5 + if is_device then ok s + else if rights = capability.VM_READ_WRITE then + let i6 ← lift (offset &&& 63#u64) + if i6 = 0#u64 then + if offset < sz then + let i7 ← sz - offset + if i7 >= fl4_abi.consts.vcpu_state_mask.SIZE then + let i8 ← lift (wa ptr offset) + vcpu.read_state_to_frame st ar s a1 vc i8 out_mask + else ok s + else ok s + else ok s + else ok s + | capability.Cap.PageTable _ _ _ _ => ok s + | capability.Cap.PageDirectory _ _ _ _ => ok s + | capability.Cap.Pdpt _ _ _ _ => ok s + | capability.Cap.Pml4 _ _ _ => ok s + | capability.Cap.Vcpu _ => ok s + | capability.Cap.Ept _ _ _ _ _ => ok s + | capability.Cap.AsidControl => ok s + | capability.Cap.AsidPool _ _ => ok s + | capability.Cap.IoPortControl => ok s + | capability.Cap.IoPort _ _ _ => ok s + | capability.Cap.TdxControl => ok s + | capability.Cap.HighVSpace _ _ _ => ok s + else ok s) ⦃ s1 => + ∃ σ1 : Fl4SpecA64.State Unit, + (NonDet.when (xv.vcpuExt.runOutMask ≠ 0) do + let buf ← getCap (specPtr ct, tcbCnodeIndex 2) + match frameRightsDev buf, stateWindow buf xv.vcpuExt.runOffset with + | some (R, false), some (some frame) => + NonDet.when (R = vmReadWrite) <| fl4ReadStateToFrame (specPtr vc) frame xv.vcpuExt.runOutMask + | _, _ => pure ()) σ = ({((), σ1)}, False) ∧ + JStage3VcpuCoreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s1 a1 σ1 m ∧ + (SActA σ → SActA σ1) ∧ + WritesIn st s s1 (VcpuWrNoStA st Z OS s) ∧ + SchedValidA σ1 ∧ + VcpuRec6A st Z OS Bf s1 σ1 ∧ + Clock a1 σ1.machineState ∧ + σ1.machineState.timeState = σ.machineState.timeState ∧ + σ1.machineState.lastMachineTime = σ.machineState.lastMachineTime ∧ + σ1.kheap = σ.kheap ∧ + L.ksb s1 = L.ksb s ∧ L.cpu s1 = L.cpu s ∧ + OSKeep st OS s s1 ∧ + (∀ v ∈ Z.V, L.mem s1 v = L.mem s v) ∧ + (∀ u ∈ Z.T, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → L.mem s1 (wa u o) = L.mem s (wa u o)) ∧ + FrKeepA st OS s σ s1 σ1 ⦄ := by + by_cases hm0 : (out_mask != 0#u64) = true + · have hmb : xv.vcpuExt.runOutMask ≠ 0 := by + rw [← hrm_eq]; intro e + have h0 : out_mask = 0#u64 := u64_eq_of_bv_eqH e + exact (bne_iff_ne.mp hm0) h0 + rw [if_pos hm0] + have hsbuf : obj.Tcb.slot ct layout.tcb.SLOT_BUFFER = ok (sbWordA ct) := by + rw [tcb_slot_bufferA ct, bufSlot_eqA ct] + have hcap_ok : obj.Slot.cap st (sbWordA ct) s = ok (capA st s (sbWordA ct)) := + Fl4ProofsA64.P1.W7.R.ST.capA_eq s (sbWordA ct) + have his_buf : IsSlotA Z.th (sbWordA ct) := + ⟨ct, Or.inr ⟨(J.sr.thT ct).2 hct, 2, by unfold tcbSlotsA; omega, (bufSlot_eqA ct).symm⟩⟩ + have hos_buf : sbWordA ct ∈ OS := J.j5.2.1 _ his_buf + have hcapRel : CapRelA (capA st s (sbWordA ct)) xct.tcbIpcframe := by + obtain ⟨x', hx', hsr⟩ := J.j.1.st.tcb ct ((J.sr.thT ct).2 hct) + rw [hkc] at hx'; cases hx' + obtain ⟨ac, hac, hcr⟩ := hsr 2 (by unfold tcbSlotsA; omega) + rw [tcbCnodeMap_twoA xct] at hac; cases hac + rw [bufSlot_eqA ct, capAtA_eq] at hcr; exact hcr + have hgetCap : getCap (Z := Unit) (specPtr ct, tcbCnodeIndex 2) σ = ({(xct.tcbIpcframe, σ)}, False) := + getCap_tcb_eqA hkc (tcbCnodeMap_twoA xct) + have hnoop : ∀ (hsp : (match frameRightsDev xct.tcbIpcframe, stateWindow xct.tcbIpcframe xv.vcpuExt.runOffset with + | some (R, false), some (some frame) => + NonDet.when (R = vmReadWrite) <| fl4ReadStateToFrame (specPtr vc) frame xv.vcpuExt.runOutMask + | _, _ => pure ()) σ = ({((), σ)}, False)), + ok s ⦃ s1 => ∃ σ1, (NonDet.when (xv.vcpuExt.runOutMask ≠ 0) do + let buf ← getCap (specPtr ct, tcbCnodeIndex 2) + match frameRightsDev buf, stateWindow buf xv.vcpuExt.runOffset with + | some (R, false), some (some frame) => + NonDet.when (R = vmReadWrite) <| fl4ReadStateToFrame (specPtr vc) frame xv.vcpuExt.runOutMask + | _, _ => pure ()) σ = ({((), σ1)}, False) ∧ + JStage3VcpuCoreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s1 a1 σ1 m ∧ + (SActA σ → SActA σ1) ∧ WritesIn st s s1 (VcpuWrNoStA st Z OS s) ∧ + SchedValidA σ1 ∧ VcpuRec6A st Z OS Bf s1 σ1 ∧ Clock a1 σ1.machineState ∧ + σ1.machineState.timeState = σ.machineState.timeState ∧ + σ1.machineState.lastMachineTime = σ.machineState.lastMachineTime ∧ + σ1.kheap = σ.kheap ∧ L.ksb s1 = L.ksb s ∧ L.cpu s1 = L.cpu s ∧ + OSKeep st OS s s1 ∧ (∀ v ∈ Z.V, L.mem s1 v = L.mem s v) ∧ + (∀ u ∈ Z.T, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → L.mem s1 (wa u o) = L.mem s (wa u o)) ∧ + FrKeepA st OS s σ s1 σ1 ⦄ := fun hsp => by + rw [WP.spec_ok] + refine ⟨σ, ?_, J1, id, WritesIn.refl s _, hSV, R6, hCl1, rfl, rfl, rfl, rfl, rfl, fun _ _ => ⟨rfl, rfl, rfl, rfl⟩, fun _ _ => rfl, fun _ _ _ _ _ => rfl, FrKeepA.refl⟩ + unfold NonDet.when; rw [if_pos hmb, nd_bind_oneSc hgetCap]; exact hsp + rw [hsbuf, bind_ok, hcap_ok, bind_ok] + rcases hcapRel with ⟨hwf, habs⟩ + have hnat := J.bufNative ct hct; rw [bufSlot_eqA ct] at hnat + cases hc_eq : capA st s (sbWordA ct) with + | Frame ptr size rights is_device mp is_s2 asid va => + have hs2f : is_s2 = false := by + have h1 : capAtA (L.mem s) (sbWordA ct) = .Frame ptr size rights is_device mp is_s2 asid va := hc_eq + rw [h1] at hnat; exact hnat + subst hs2f + rw [hc_eq] at habs hwf + have hbuf_eq : xct.tcbIpcframe = .archObjectCap (.frameCap (specPtr ptr) (vmRightsOfA rights) + (frameSizeOfA size) is_device (mapDataA mp asid va)) := by + rw [← habs]; rfl + have hfrd : frameRightsDev xct.tcbIpcframe = some (vmRightsOfA rights, is_device) := by + rw [hbuf_eq]; rfl + have hbits_ok : capability.FrameSize.bits size = ok ⟨BitVec.ofNat 64 (frameBitsA size)⟩ := by + cases size <;> rfl + have hbits_eq : frameBitsA size = pageBitsForSize (frameSizeOfA size) := by + cases size <;> rfl + have hbits_lt64 : frameBitsA size < 64 := by cases size <;> decide + have hbits_and : (⟨BitVec.ofNat 64 (frameBitsA size)⟩ &&& 63#u64 : U64) = + ⟨BitVec.ofNat 64 (frameBitsA size)⟩ := by cases size <;> apply UScalar.eq_of_val_eq <;> rfl + have hbits_val : (⟨BitVec.ofNat 64 (frameBitsA size)⟩ : U64).val = frameBitsA size := by + cases size <;> rfl + dsimp only + rw [hbits_ok, bind_ok, lift_okH, bind_ok, hbits_and, + u64_shl_u64_okH (by rw [hbits_val]; exact hbits_lt64), hbits_val, bind_ok] + set sz1 : U64 := ⟨(1#u64 : U64).bv <<< frameBitsA size⟩ + have hsz1_bv : sz1.bv = (1 : Word) <<< pageBitsForSize (frameSizeOfA size) := by + rw [← hbits_eq]; rfl + have hsz1_val : sz1.val = 2 ^ frameBitsA size := by + show ((1#u64 : U64).bv <<< frameBitsA size).toNat = 2 ^ frameBitsA size + rw [BitVec.toNat_shiftLeft, show (1#u64 : U64).bv.toNat = 1 from rfl, Nat.shiftLeft_eq, Nat.one_mul] + exact Nat.mod_eq_of_lt (Nat.pow_lt_pow_right (by omega) hbits_lt64) + have hwf_r : 1 ≤ rights.val ∧ rights.val ≤ 3 := ⟨hwf.1, hwf.2.1⟩ + have hrw_iff : rights = capability.VM_READ_WRITE ↔ vmRightsOfA rights = vmReadWrite := by + have h3 : capability.VM_READ_WRITE.val = 3 := by unfold capability.VM_READ_WRITE; rfl + unfold vmRightsOfA + constructor + · intro e; rw [e, h3]; rfl + · intro e + by_cases hr3 : rights.val = 3 + · exact UScalar.eq_of_val_eq (by omega) + · by_cases hr2 : rights.val = 2 + · rw [if_neg hr3, if_pos hr2] at e; exfalso + have hm := (Set.ext_iff.1 e .allowWrite).2 (by simp [vmReadWrite]) + simp [vmReadOnly] at hm + · rw [if_neg hr3, if_neg hr2] at e; exfalso + have hm := (Set.ext_iff.1 e .allowRead).2 (by simp [vmReadWrite]) + simp [vmKernelOnly] at hm + by_cases hdev : is_device = true + · rw [if_pos hdev]; exact hnoop (by rw [hfrd, hdev]; rfl) + · have hdevf : is_device = false := by cases is_device with | true => exact absurd rfl hdev | false => rfl + rw [if_neg hdev] + by_cases hrw : rights = capability.VM_READ_WRITE + · rw [if_pos hrw, lift_okH, bind_ok] + by_cases hal64 : (offset &&& 63#u64) = 0#u64 + · rw [if_pos hal64] + have hal64' : offset.bv &&& 63 = 0 := congrArg UScalar.bv hal64 + by_cases hlt_sz : offset < sz1 + · rw [if_pos hlt_sz] + have hlt_sz_val : offset.val < sz1.val := hlt_sz + rw [u64_sub_okH (by omega), bind_ok] + have hsub_val : (⟨BitVec.ofNat 64 (sz1.val - offset.val)⟩ : U64).val = sz1.val - offset.val := by + have hsz_lt : sz1.val < 2 ^ 64 := sz1.bv.isLt + show (BitVec.ofNat 64 _).toNat = _ + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + have hsm_sz : fl4_abi.consts.vcpu_state_mask.SIZE.val = 1024 := by + unfold fl4_abi.consts.vcpu_state_mask.SIZE; rfl + by_cases hge1024 : (⟨BitVec.ofNat 64 (sz1.val - offset.val)⟩ : U64) ≥ fl4_abi.consts.vcpu_state_mask.SIZE + · rw [if_pos hge1024, lift_okH, bind_ok] + have hge1024_val : 1024 ≤ sz1.val - offset.val := by + have : fl4_abi.consts.vcpu_state_mask.SIZE.val ≤ (⟨BitVec.ofNat 64 (sz1.val - offset.val)⟩ : U64).val := hge1024 + omega + have hcap_rw : capA st s (sbWordA ct) = .Frame ptr size capability.VM_READ_WRITE false mp false asid va := by + rwa [hrw, hdevf] at hc_eq + refine WP.spec_mono (vcpu_read_state_to_frame_coreA J1 hSV R6 hCl1 hClFr hP hvcV hos_buf + hcap_rw hbits_ok hal64 (by rw [hbits_val]; omega) (by rw [hbits_val]; omega) out_mask) ?_ + rintro s1 ⟨σ1, hsp_rd, J1', hSV1, R6_1, hCl1', ht1, hl1, hkh1, hksb1, hcpu1, osk1, hw1, hHS1, hVcEq1, hTcbEq1, fkr⟩ + have hsw : stateWindow xct.tcbIpcframe xv.vcpuExt.runOffset = some (some (specPtr (wa ptr offset))) := by + rw [hbuf_eq, ← hro_eq]; unfold stateWindow; dsimp only + have hcond : ¬ (offset.bv &&& 63 ≠ 0 ∨ offset.bv ≥ (1 : Word) <<< pageBitsForSize (frameSizeOfA size) ∨ + ((1 : Word) <<< pageBitsForSize (frameSizeOfA size)) - offset.bv < smSize) := by + rw [← hsz1_bv] + rintro (h1 | h2 | h3) + · exact h1 hal64' + · have : sz1.val ≤ offset.val := BitVec.le_def.mp h2; omega + · have hsm : smSize.toNat = 1024 := rfl + have hsub_nat : (sz1.bv - offset.bv).toNat = sz1.val - offset.val := + BitVec.toNat_sub_of_le (le_of_lt hlt_sz_val) + have : (sz1.bv - offset.bv).toNat < smSize.toNat := BitVec.lt_def.mp h3; omega + rw [if_neg hcond] + congr 2 + unfold specPtr + obtain ⟨_, _, -, -, -, fo, -, -, -, -, -⟩ := J7FrA.fr (st := st) _ _ _ _ _ _ J.j7 + have hpH := fo _ hos_buf ptr size _ mp false asid va hcap_rw _ hbits_ok + have hpFit := J.ent.heap.fits ptr _ hpH; simp only [HKind.bits, hbits_val] at hpFit + have hwa_bv : (wa ptr offset).bv = ptr.bv + offset.bv := by + apply BitVec.eq_of_toNat_eq + change (wa ptr offset).val = (ptr.bv + offset.bv).toNat + rw [wa_valm, BitVec.toNat_add]; rfl + rw [hwa_bv, BitVec.add_assoc, BitVec.add_comm pptrShiftA offset.bv, ← BitVec.add_assoc] + refine ⟨σ1, ?_, J1', fun hs => hs.of_vcpuStepA hHS1, hw1, hSV1, R6_1, hCl1', ht1, hl1, hkh1, hksb1, hcpu1, osk1, ?_, hTcbEq1, fkr⟩ + · unfold NonDet.when; rw [if_pos hmb, nd_bind_oneSc hgetCap, hfrd, hdevf, hsw] + dsimp only; rw [if_pos (hrw_iff.1 hrw), ← hrm_eq]; exact hsp_rd + · intro v hv; simpa [wa_zeroH] using hVcEq1 v hv 0#u64 (by decide) (by decide) + · rw [if_neg hge1024] + refine hnoop ?_ + rw [hfrd, hdevf, hbuf_eq, ← hro_eq]; unfold stateWindow; dsimp only + have hcond : offset.bv &&& 63 ≠ 0 ∨ offset.bv ≥ (1 : Word) <<< pageBitsForSize (frameSizeOfA size) ∨ + ((1 : Word) <<< pageBitsForSize (frameSizeOfA size)) - offset.bv < smSize := by + right; right; rw [← hsz1_bv, BitVec.lt_def, BitVec.toNat_sub_of_le (le_of_lt hlt_sz_val)] + have hsm : smSize.toNat = 1024 := rfl + have h1024 : fl4_abi.consts.vcpu_state_mask.SIZE.val = 1024 := by + unfold fl4_abi.consts.vcpu_state_mask.SIZE; rfl + have hsub : (⟨BitVec.ofNat 64 (sz1.val - offset.val)⟩ : U64).val = sz1.val - offset.val := by + have hlt64 : sz1.val < 2 ^ 64 := sz1.bv.isLt + show (BitVec.ofNat 64 _).toNat = _; rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + have hng : ¬ (fl4_abi.consts.vcpu_state_mask.SIZE.val ≤ (⟨BitVec.ofNat 64 (sz1.val - offset.val)⟩ : U64).val) := hge1024 + rw [h1024, hsub] at hng + change sz1.val - offset.val < smSize.toNat + rw [hsm]; exact Nat.lt_of_not_ge hng + rw [if_pos hcond]; rfl + · rw [if_neg hlt_sz] + refine hnoop ?_ + rw [hfrd, hdevf, hbuf_eq, ← hro_eq]; unfold stateWindow; dsimp only + have hcond : offset.bv &&& 63 ≠ 0 ∨ offset.bv ≥ (1 : Word) <<< pageBitsForSize (frameSizeOfA size) ∨ + ((1 : Word) <<< pageBitsForSize (frameSizeOfA size)) - offset.bv < smSize := by + right; left; rw [← hsz1_bv]; show sz1.bv ≤ offset.bv; rw [BitVec.le_def] + have hnl : ¬ offset.val < sz1.val := hlt_sz + change sz1.val ≤ offset.val; omega + rw [if_pos hcond]; rfl + · rw [if_neg hal64] + refine hnoop ?_ + rw [hfrd, hdevf, hbuf_eq, ← hro_eq]; unfold stateWindow; dsimp only + have hcond : offset.bv &&& 63 ≠ 0 ∨ offset.bv ≥ (1 : Word) <<< pageBitsForSize (frameSizeOfA size) ∨ + ((1 : Word) <<< pageBitsForSize (frameSizeOfA size)) - offset.bv < smSize := + Or.inl fun e => hal64 (u64_eq_of_bv_eqH e) + rw [if_pos hcond]; rfl + · rw [if_neg hrw] + refine hnoop ?_ + rw [hfrd, hdevf] + cases hsw : stateWindow xct.tcbIpcframe xv.vcpuExt.runOffset with + | none => rfl + | some mf => cases mf with + | none => rfl + | some fr => dsimp only; unfold NonDet.when; rw [if_neg (fun e => hrw (hrw_iff.2 e))]; rfl + | PageTable p m asid va => + cases m + · rw [hc_eq] at habs; exact hnoop (by rw [← habs]; rfl) + · cases hcf : xct.tcbIpcframe with + | archObjectCap ac => + cases ac with + | frameCap _ _ _ _ _ => rw [hcf, hc_eq] at habs; simp [CapEqA, absCapA] at habs + | _ => exact hnoop (by simp [hcf, frameRightsDev]) + | _ => exact hnoop (by simp [hcf, frameRightsDev]) + | Null | Untyped _ _ _ _ | Endpoint _ _ _ _ _ _ | Notification _ _ _ _ | Reply _ _ + | CNode _ _ _ _ | Thread _ | IrqControl | IrqHandler _ | Zombie _ _ _ | Domain + | SchedContext _ _ | SchedControl _ | PageDirectory _ _ _ _ | Pdpt _ _ _ _ + | Pml4 _ _ _ | Vcpu _ | Ept _ _ _ _ _ | AsidControl | AsidPool _ _ + | IoPortControl | IoPort _ _ _ | TdxControl | HighVSpace _ _ _ => + rw [hc_eq] at habs; exact hnoop (by rw [← habs]; rfl) + · have hmb : ¬ (xv.vcpuExt.runOutMask ≠ 0) := by + rw [← hrm_eq]; intro hne; exact hm0 (bne_iff_ne.2 fun e => hne (congrArg UScalar.bv e)) + rw [if_neg hm0, WP.spec_ok] + exact ⟨σ, by unfold NonDet.when; rw [if_neg hmb]; rfl, J1, id, WritesIn.refl s _, hSV, R6, hCl1, rfl, rfl, rfl, rfl, rfl, + fun _ _ => ⟨rfl, rfl, rfl, rfl⟩, fun _ _ => rfl, fun _ _ _ _ _ => rfl, FrKeepA.refl⟩ + refine WP.spec_bind hReadStep ?_ + rintro s1 ⟨σ1, hsp_rd, J1', hSAct1, hw1, hSV1, R6_1, hCl1', ht1, hl1, hkh1, hksb1, hcpu1, osk1, hvc1, htcb1, fkr⟩ + have hkv1 : σ1.kheap (specPtr vc) = some (.archObj (.vcpu xv)) := by rw [hkh1]; exact hkv + have hkc1 : σ1.kheap (specPtr ct) = some (.tcb xct) := by rw [hkh1]; exact hkc + have hback1 : L.mem s1 (runVcpuA ct) = vc := by + unfold runVcpuA; rw [htcb1 ct hct 3672#u64 (by decide) (by decide)]; exact rfl + have hlink1 : L.mem s1 (wa vc 24#u64) = ct := by + have h := R6_1.runBack ct hct (by rw [hback1]; exact hvc0) + rwa [hback1] at h + -- Step A: clear `vc.RUN_CALLER` and `ct.RUN_VCPU` + have hrc_off : vcpu.RUN_CALLER = 24#u64 := by simp [vcpu.RUN_CALLER] + rw [hrc_off, lift_okH, bind_ok, L.write_eq, bind_ok, lift_okH, bind_ok, L.write_eq, bind_ok] + obtain ⟨J3, hHS3, hw3, hSV3, R6_3, hCl3, hvc24_3, hrt_3, hvc0_3, h3664_3, htcb3, hksb3, hcpu3, osk3, fk3⟩ := + vcpu_clear_run_pair_coreA J1' hSV1 R6_1 hCl1' hvcV hct hlink1 hback1 hkv1 hkc1 + set s3 := L.wr (L.wr s1 (wa vc 24#u64) 0#u64) (runVcpuA ct) 0#u64 + set σ3 := updKSc (updKSc σ1 (specPtr vc) (.archObj (.vcpu { xv with vcpuExt := { xv.vcpuExt with runCaller := none } }))) + (specPtr ct) (.tcb { xct with tcbExt := { xct.tcbExt with tcbRunVcpu := none } }) + have hne_ct_vc : specPtr ct ≠ specPtr vc := fun e => by rw [e, hkv] at hkc; cases hkc + set x3 : Tcb := { xct with tcbExt := { xct.tcbExt with tcbRunVcpu := none } } + have hkc3 : σ3.kheap (specPtr ct) = some (.tcb x3) := by simp [σ3, x3, updKSc, Function.update] + -- Step B: 7 TCB user-register writes (BADGE, X2..X5, MSG_INFO, FAULT_IP := NEXT_IP) + have hmr := msg_reg_0_3_eqH + have hmi := msginfo_4_eqH + have hbad_idx : layout.reg.BADGE = regIdxA .x0 := by simp [layout.reg.BADGE, layout.reg.X0, regIdxA] + have hx2_idx : layout.reg.X2 = regIdxA .x2 := by simp [layout.reg.X2, regIdxA] + have hx3_idx : layout.reg.X3 = regIdxA .x3 := by simp [layout.reg.X3, regIdxA] + have hx4_idx : layout.reg.X4 = regIdxA .x4 := by simp [layout.reg.X4, regIdxA] + have hx5_idx : layout.reg.X5 = regIdxA .x5 := by simp [layout.reg.X5, regIdxA] + have hmi_idx : layout.reg.MSG_INFO = regIdxA .x1 := by simp [layout.reg.MSG_INFO, layout.reg.X1, regIdxA] + have hnip_idx : layout.reg.NEXT_IP = regIdxA .nextIP := Endp.regIdxA_nextIP.symm + have hfip_idx : layout.reg.FAULT_IP = regIdxA .faultIP := by simp [layout.reg.FAULT_IP, regIdxA] + rw [tcb_set_reg_eqH, bind_ok, hmr.1, bind_ok, tcb_set_reg_eqH, bind_ok, + hmr.2.1, bind_ok, tcb_set_reg_eqH, bind_ok, hmr.2.2.1, bind_ok, tcb_set_reg_eqH, bind_ok, + hmr.2.2.2, bind_ok, tcb_set_reg_eqH, bind_ok, hmi.1, bind_ok] + have hsmi_eq : ∀ s8, msg.set_msg_info st s8 ct ⟨0#u64, 0#u64, 0#u64, 4#u64⟩ = + ok (L.wr s8 (Endp.regA ct layout.reg.MSG_INFO) 4#u64) := fun s8 => by + unfold msg.set_msg_info; rw [hmi.2.1, bind_ok, tcb_set_reg_eqH] + rw [hsmi_eq, bind_ok, tcb_reg_eqH, bind_ok, tcb_set_reg_eqH, bind_ok] + rw [hbad_idx, hx2_idx, hx3_idx, hx4_idx, hx5_idx, hmi_idx, hnip_idx, hfip_idx] + obtain ⟨σ10, x10, hsp7r, hkc10, hkh10_ne, hst10_eq, hv10_eq, hb10_eq, hsc10_eq, + J10, hSAct10, hw10, hSV10, R6_10, hCl10, hms10, hvc10, htcb10, hksb10, hcpu10, osk10, fk10⟩ := + vcpu_complete_run_7regs_coreA J3 hSV3 R6_3 hCl3 hct hkc3 hstop w1 w2 w3 + have hw_s_s10 : WritesIn st s _ (VcpuWrNoStA st Z OS s) := + ((hw1.trans (hw3.mono (fun _ h => hWrKeep osk1 h))).mono fun _ h => h.elim id id).trans + (hw10.mono (fun _ h => hWrKeep (hOskT osk1 osk3) h)) |>.mono fun _ h => h.elim id id + have hsp_pre : fl4CompleteRun (Z := Unit) (specPtr ct) stopSpec w1.bv w2.bv w3.bv σ = + setThreadState (Z := Unit) (specPtr ct) .running σ10 := by + unfold fl4CompleteRun + have hopt : optRefA (L.mem s (runVcpuA ct)) = some (specPtr vc) := by + change optRefA vc = some (specPtr vc); unfold optRefA; simp [hvc0] + rw [nd_bind_oneSc (getRunVcpu_eqA hkc), hrv_spec, hopt] + dsimp only + rw [nd_bind_oneSc (fl4VcpuFlush_noneA J.curVcpuNone (specPtr vc)), + nd_bind_oneSc (getVcpu_eqA hkv)] + trans (((NonDet.when (xv.vcpuExt.runOutMask ≠ 0) do + let buf ← getCap (Z := Unit) (specPtr ct, tcbCnodeIndex 2) + match frameRightsDev buf, stateWindow buf xv.vcpuExt.runOffset with + | some (R, false), some (some frame) => + NonDet.when (R = vmReadWrite) (fl4ReadStateToFrame (specPtr vc) frame xv.vcpuExt.runOutMask) + | _, _ => pure ()) >>= fun _ => do + fl4VcpuExtUpdate (Z := Unit) (specPtr vc) fun e => { e with runCaller := none } + setRunVcpu (Z := Unit) (specPtr ct) none + asUser (Z := Unit) (specPtr ct) (setRegister badge_register 0) + asUser (Z := Unit) (specPtr ct) do + setRegister .x2 stopSpec.word + setRegister .x3 w1.bv + setRegister .x4 w2.bv + setRegister .x5 w3.bv + setMessageInfo (Z := Unit) (specPtr ct) ⟨4, 0, 0, 0⟩ + asUser (Z := Unit) (specPtr ct) do + let ip ← getRegister nextInstructionRegister + setRegister faultRegister ip + setThreadState (Z := Unit) (specPtr ct) .running) σ) + · rfl + rw [nd_bind_oneSc hsp_rd] + have hsp_c1 : fl4VcpuExtUpdate (Z := Unit) (specPtr vc) (fun e => { e with runCaller := none }) σ1 = + ({((), updKSc σ1 (specPtr vc) (.archObj (.vcpu { xv with vcpuExt := { xv.vcpuExt with runCaller := none } })))}, False) := + fl4VcpuExtUpdate_eqA _ hkv1 + rw [nd_bind_oneSc hsp_c1, nd_bind_oneSc (setRunVcpu_eqA none (by rw [updKSc_other _ hne_ct_vc]; exact hkc1))] + exact hsp7r (fun _ => setThreadState (Z := Unit) (specPtr ct) .running) + refine hk_cont _ a1 ⟨σ10, x10, hsp_pre, hkc10, by rw [hst10_eq]; show runnable xct.tcbState = true; rw [hct_st_spec]; rfl, + hv10_eq, hb10_eq, hsc10_eq, ?_, J10, fun hs => hSAct10 ((hSAct1 hs).of_vcpuStepA hHS3), hw_s_s10, + hSV10, R6_10, hCl10, by rw [hms10]; exact ht1, by rw [hms10]; exact hl1, ?_, ?_, ?_, ?_, + hksb10.trans (hksb3.trans hksb1), hcpu10.trans (hcpu3.trans hcpu1), hOskT (hOskT osk1 osk3) osk10, + (fkr.trans fk3).trans fk10⟩ + · intro u hu hne + have hne1 : specPtr u ≠ specPtr ct := fun e => hne (specPtr_inj e) + have huH : H u = some .tcb := show u ∈ H.tcbs by rw [J.tcbs]; exact hu + obtain ⟨_, _, -, arm, -, -, -, -, -, -, -⟩ := J7FrA.fr (st := st) _ _ _ _ _ _ J.j7 + have hvH := arm.vh vc hvcV + have hne2 : specPtr u ≠ specPtr vc := fun e => by + have : u = vc := specPtr_inj e; rw [this, hvH] at huH; cases huH + rw [hkh10_ne _ hne1]; simp [σ3, updKSc, Function.update, hne1, hne2, hkh1] + · unfold runVcpuA + rw [htcb10 ct hct 3672#u64 (by simp) (by simp) (Or.inr (Or.inr (by simp)))] + exact hrt_3 + · rw [hvc10 vc hvcV 24#u64 (by simp) (by simp)]; exact hvc24_3 + · intro v hv + have h10 := hvc10 v hv 0#u64 (by simp) (by simp); rw [wa_zeroH] at h10 + rw [h10, hvc0_3 v hv, hvc1 v hv] + · intro u hu o ho1 ho8 hne1 hne2 + rw [htcb10 u hu o ho1 ho8 hne2, htcb3 u hu o ho1 ho8 hne1, htcb1 u hu o ho1 ho8] + +set_option maxHeartbeats 1200000 in +/-- **`vcpu.complete_run` refinement on `JStage3TA`**: flushes VCPU state, optionally reads +registers into the caller's `SLOT_BUFFER` frame, clears `vc.RUN_CALLER` and `ct.RUN_VCPU`, +writes the 7 reply/IP registers, sets `ct` to `TS_RUNNING`, and restores `vspace.set_vm_root` +if `ct = cur_thread`. -/ +theorem vcpu_complete_run_stage3TA + {AR : A → HwRootA → Prop} {Clock : A → Fl4SpecA64.MachineState → Prop} + {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J7 : JSlotA S} + [J7PpA st J7] [J7PpKA st J7] [J7FrA st J7] + {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + (hSV : SchedValidA σ) + (R6 : VcpuRec6A st Z OS Bf s σ) + (hCl : Clock a σ.machineState) + (hClVm : ∀ s' a' ms (u : U64), Clock a' ms → vspace.set_vm_root st ar s' a' u ⦃ r => Clock r.2 ms ⦄) + (hClFlushState : ∀ a' ms (vc om : U64), Clock a' ms → ar.vcpu_flush_state a' vc om ⦃ a'' => Clock a'' ms ⦄) + (hClFr : ∀ a' ms ms', Clock a' ms → ms'.timeState = ms.timeState → + ms'.lastMachineTime = ms.lastMachineTime → Clock a' ms') + (hP : VcpuPlatAgreeA ar) (hAK : VcpuArchKeepsA ar AR) (hVR : VcpuVmRootA st ar AR) + {ct : U64} (hct : ct ∈ Z.T) + {stop : U64} {stopSpec : RunStop} (hstop : stop.bv = stopSpec.word) (w1 w2 w3 : U64) : + vcpu.complete_run st ar s a ct stop w1 w2 w3 ⦃ res => + ∃ (hw' : HwRootA) (σ' : Fl4SpecA64.State Unit), + fl4CompleteRun (Z := Unit) (specPtr ct) stopSpec w1.bv w2.bv w3.bv σ = ({((), σ')}, False) ∧ + JStage3TA st AR { Z with hw := hw' } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 res.1 res.2 σ' m ∧ + SchedValidA σ' ∧ + VcpuRec6A st { Z with hw := hw' } OS Bf res.1 σ' ∧ + Clock res.2 σ'.machineState ∧ + σ'.machineState.timeState = σ.machineState.timeState ∧ + σ'.machineState.lastMachineTime = σ.machineState.lastMachineTime ∧ + L.mem res.1 (runVcpuA ct) = 0#u64 ∧ + (L.mem s (runVcpuA ct) ≠ 0#u64 → L.mem res.1 (wa (L.mem s (runVcpuA ct)) 24#u64) = 0#u64) ∧ + (∀ v ∈ Z.V, L.mem res.1 v = L.mem s v) ∧ + (∀ u ∈ Z.T, L.mem res.1 (wa u 3664#u64) = L.mem s (wa u 3664#u64)) ∧ + L.ksb res.1 = L.ksb s ∧ L.cpu res.1 = L.cpu s ∧ + OSKeep st OS s res.1 ∧ + (∀ u ∈ Z.T, L.mem res.1 (boundA u) = L.mem s (boundA u) ∧ L.mem res.1 (scA u) = L.mem s (scA u)) ∧ + (∀ u ∈ Z.T, u ≠ ct → L.mem res.1 (stA u) = L.mem s (stA u)) ∧ + (∀ u ∈ Z.T, ∀ x, σ.kheap (specPtr u) = some (.tcb x) → + ∃ x', σ'.kheap (specPtr u) = some (.tcb x') ∧ + x'.tcbArch.tcbVcpu = x.tcbArch.tcbVcpu ∧ + x'.tcbBoundNotification = x.tcbBoundNotification ∧ + x'.tcbSchedContext = x.tcbSchedContext ∧ + (u ≠ ct → x'.tcbState = x.tcbState ∧ x'.tcbExt.tcbRunVcpu = x.tcbExt.tcbRunVcpu)) ∧ + FrKeepA st OS s σ res.1 σ' ⦄ := by + unfold vcpu.complete_run + have hrv_const : layout.tcb.RUN_VCPU = ok 3672#u64 := + eq_of_spec_fd Fl4ProofsA64.P0.Consts.layout.tcb.RUN_VCPU.spec + have hrun_eq : wa ct 3672#u64 = runVcpuA ct := rfl + rw [hrv_const, bind_ok, lift_okH, bind_ok, hrun_eq, L.read_eq, bind_ok] + obtain ⟨xct, hkc, -, hrv_spec⟩ := J.j.2.1.tcb ct hct + by_cases hvc0 : L.mem s (runVcpuA ct) = 0#u64 + · rw [if_pos hvc0, WP.spec_ok] + have hsp : fl4CompleteRun (Z := Unit) (specPtr ct) stopSpec w1.bv w2.bv w3.bv σ = ({((), σ)}, False) := by + unfold fl4CompleteRun + rw [nd_bind_oneSc (getRunVcpu_eqA hkc), hrv_spec, hvc0]; rfl + exact ⟨Z.hw, σ, hsp, J, hSV, R6, hCl, rfl, rfl, hvc0, fun h => absurd hvc0 h, + fun _ _ => rfl, fun _ _ => rfl, rfl, rfl, fun _ _ => ⟨rfl, rfl, rfl, rfl⟩, fun _ _ => ⟨rfl, rfl⟩, + fun _ _ _ => rfl, fun _ _ x hx => ⟨x, hx, rfl, rfl, rfl, fun _ => ⟨rfl, rfl⟩⟩, FrKeepA.refl⟩ + · rw [if_neg hvc0] + set vc := L.mem s (runVcpuA ct) + have hvcV : vc ∈ Z.V := (J.vcRun.run ct hct).resolve_left hvc0 + refine vcpu_complete_run_pre_sts_coreA J.toVcpuCoreA hSV R6 hCl hClFlushState hClFr hP hAK hct hkc hvc0 hstop w1 w2 w3 + (k := fun s10 a1 => do + let s11 ← sched.set_thread_state st s10 ct sched.TS_RUNNING + let cur ← sched.cur_thread st s11 + let b ← obj.Tcb.Insts.CoreCmpPartialEqTcb.eq ct cur + if b then vspace.set_vm_root st ar s11 a1 ct else ok (s11, a1)) ?_ + rintro s10 a1 ⟨σ10, x10, hsp_pre, hkc10, hrun10, hv10_eq, hb10_eq, hsc10_eq, hkh10_ne, + J10_c, hSAct10, -, hSV10, R6_10, hCl10, ht10, hl10, hrt10, hvc24_10, hvc0_10, hTcb10, hksb10, hcpu10, osk10, fk10⟩ + have J10 := J10_c.toTA (hSAct10 J.sact) + refine WP.spec_bind (set_thread_state_running_stage3TA J10 hSV10 R6_10 hCl10 hct hkc10 hrun10 hrt10) ?_ + rintro s11 ⟨σ11, hsts11, hkh11, J11, hSV11, R6_11, hCl11, hms11, hksb11, hcpu11, osk11, hw11, fk11⟩ + refine WP.spec_mono (vcpu_set_vm_root_if_cur_coreA J11.toVcpuCoreA R6_11 hCl11 hClVm hVR ct) ?_ + rintro ⟨sf, af⟩ ⟨hsf, hw', Jf_c, hClf⟩ + dsimp only at hsf Jf_c hClf; rcases hsf.symm with rfl + have hsp_full : fl4CompleteRun (Z := Unit) (specPtr ct) stopSpec w1.bv w2.bv w3.bv σ = ({((), σ11)}, False) := + hsp_pre.trans hsts11 + have hksFit10 : (L.ksb s10).val + 788736 ≤ 2 ^ 64 := J10.ent.ksw + have hcpu_lt10 : (L.cpu s10).val < 128 := by + obtain ⟨nn, -, hkn, pcs⟩ := J10.sr.toSchedSRN.csN + have := (pcs (L.cpu s10)).1 J10.sr.sch.cpuCs; omega + have hnode10 : (L.nodeAt s10 (L.cpu s10) layout.node.SCHED_ACTION).val = + (L.ksb s10).val + 223488 + (L.cpu s10).val * 4288 + 16 := by + have h16 : (layout.node.SCHED_ACTION : U64).val = 16 := by unfold layout.node.SCHED_ACTION; rfl + have hna := nodeAt_val (J10.sq.nodes _ J10.sr.sch.cpuCs) (by rw [h16]; omega) + rw [hna, h16]; rfl + have hVc11 : ∀ v ∈ Z.V, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → L.mem s11 (wa v o) = L.mem s10 (wa v o) := by + intro v hv o ho1 ho8 + obtain ⟨_, _, -, arm, -, -, -, -, -, -, -⟩ := J7FrA.fr (st := st) _ _ _ _ _ _ J10.j7 + have hvH := arm.vh v hv + have hfv := J10.ent.heap.fits v _ hvH; have hav := J10.ent.heap.align v _ hvH + simp only [HKind.bits] at hfv hav + have ev : (wa v o).val = v.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + refine hw11.mem_eq (by show _ % 8 = 0; omega) fun h => ?_ + rcases h with h | h + · have htH : H ct = some .tcb := show ct ∈ H.tcbs by rw [J.tcbs]; exact hct + have hne : v ≠ ct := fun e => by rw [e, htH] at hvH; cases hvH + have hd := J10.ent.heap.disj v (.other 12) ct .tcb hvH htH hne + have hft := J10.ent.heap.fits ct .tcb htH; change ct.val + 4096 ≤ 2 ^ 64 at hft + have hstV : (stA ct).val = ct.val + 3328 := by + rw [stA_eq, wa_valm]; change (ct.val + 3328) % _ = _; exact Nat.mod_eq_of_lt (by omega) + change v.val + 4096 ≤ ct.val ∨ ct.val + 4096 ≤ v.val at hd + have := congrArg UScalar.val h; rw [ev, hstV] at this; omega + · have hko := J10.heap_offNodesPA hvH (b := wa v o) (by omega) (by change (wa v o).val < v.val + 4096; omega) + rw [h] at hko; exact hko ⟨by omega, by omega⟩ + have hTcb11 : ∀ u ∈ Z.T, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → (u ≠ ct ∨ o ≠ 3328#u64) → + L.mem s11 (wa u o) = L.mem s10 (wa u o) := by + intro u hu o ho1 ho8 hne + have huH : H u = some .tcb := show u ∈ H.tcbs by rw [J.tcbs]; exact hu + have htH : H ct = some .tcb := show ct ∈ H.tcbs by rw [J.tcbs]; exact hct + have hfu := J10.ent.heap.fits u .tcb huH; change u.val + 4096 ≤ 2 ^ 64 at hfu + have hft := J10.ent.heap.fits ct .tcb htH; change ct.val + 4096 ≤ 2 ^ 64 at hft + have ev : (wa u o).val = u.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + refine hw11.mem_eq (field_aligned (J10.sr.core.tcbs u hu) ho1 ho8) fun h => ?_ + rcases h with h | h + · have hstV : (stA ct).val = ct.val + 3328 := by + rw [stA_eq, wa_valm]; change (ct.val + 3328) % _ = _; exact Nat.mod_eq_of_lt (by omega) + have hev := congrArg UScalar.val h; rw [ev, hstV] at hev + rcases hne with eut | eo + · have hd := J10.ent.heap.disj u .tcb ct .tcb huH htH eut + change u.val + 4096 ≤ ct.val ∨ ct.val + 4096 ≤ u.val at hd; omega + · by_cases eut : u = ct + · subst eut; exact eo (UScalar.eq_of_val_eq (by change o.val = 3328; omega)) + · have hd := J10.ent.heap.disj u .tcb ct .tcb huH htH eut + change u.val + 4096 ≤ ct.val ∨ ct.val + 4096 ≤ u.val at hd; omega + · have hko := J10.heap_offNodesPA huH (b := wa u o) (by omega) (by change (wa u o).val < u.val + 4096; omega) + rw [h] at hko; exact hko ⟨by omega, by omega⟩ + have hOskF : OSKeep st OS s s11 := fun y hy => + ⟨(osk11 y hy).1.trans (osk10 y hy).1, (osk11 y hy).2.1.trans (osk10 y hy).2.1, + (osk11 y hy).2.2.1.trans (osk10 y hy).2.2.1, (osk11 y hy).2.2.2.trans (osk10 y hy).2.2.2⟩ + refine ⟨hw', σ11, hsp_full, Jf_c.toTA J11.sact, hSV11, R6_11.with_hw hw', hClf, by rw [hms11]; exact ht10, by rw [hms11]; exact hl10, + ?_, ?_, ?_, ?_, hksb11.trans hksb10, hcpu11.trans hcpu10, hOskF, ?_, ?_, ?_, fk10.trans fk11⟩ + · unfold runVcpuA; rw [hTcb11 ct hct 3672#u64 (by simp) (by simp) (Or.inr (fun e => by have := congrArg UScalar.val e; simp at this))]; exact hrt10 + · intro _; rw [hVc11 vc hvcV 24#u64 (by simp) (by simp)]; exact hvc24_10 + · intro v hv + have h0 := hVc11 v hv 0#u64 (by simp) (by simp); rw [wa_zeroH] at h0 + rw [h0, hvc0_10 v hv] + · intro u hu + rw [hTcb11 u hu 3664#u64 (by simp) (by simp) (Or.inr (fun e => by have := congrArg UScalar.val e; simp at this)), + hTcb10 u hu 3664#u64 (by simp) (by simp) (Or.inr (fun e => by have := congrArg UScalar.val e; simp at this)) (Or.inr (Or.inr (by simp)))] + · intro u hu + refine ⟨?_, ?_⟩ + · unfold boundA + rw [hTcb11 u hu _ (by simp) (by simp) (Or.inr (fun e => by have := congrArg UScalar.val e; simp at this)), + hTcb10 u hu _ (by simp) (by simp) (Or.inr (fun e => by have := congrArg UScalar.val e; simp at this)) (Or.inr (Or.inr (by simp)))] + · unfold scA + rw [hTcb11 u hu _ (by simp) (by simp) (Or.inr (fun e => by have := congrArg UScalar.val e; simp at this)), + hTcb10 u hu _ (by simp) (by simp) (Or.inr (fun e => by have := congrArg UScalar.val e; simp at this)) (Or.inr (Or.inr (by simp)))] + · intro u hu hne + rw [stA_eq, hTcb11 u hu 3328#u64 (by simp) (by simp) (Or.inl hne), + hTcb10 u hu 3328#u64 (by simp) (by simp) (Or.inl hne) (Or.inl hne), ← stA_eq] + · intro u hu y hy + by_cases eu : u = ct + · subst eu; rw [hkc] at hy; cases hy + exact ⟨{ x10 with tcbState := .running }, by rw [hkh11, updKSc_same], hv10_eq, hb10_eq, hsc10_eq, fun h => absurd rfl h⟩ + · have hne1 : specPtr u ≠ specPtr ct := fun e => eu (specPtr_inj e) + exact ⟨y, by rw [hkh11, updKSc_other _ hne1, hkh10_ne u hu eu]; exact hy, rfl, rfl, rfl, fun _ => ⟨rfl, rfl⟩⟩ + +set_option maxHeartbeats 1200000 in +/-- **`vcpu.complete_run` refinement on `JStage3A`** (NO `SActA σ` required — non-vacuous when +`curThread` is in `.ext .runningVM`, e.g. VM-exit and `vcpu.run` pre-entry). -/ +theorem vcpu_complete_run_stage3A + {AR : A → HwRootA → Prop} {Clock : A → Fl4SpecA64.MachineState → Prop} {nA : U64} + {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} + {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) (J7A st (J7R2A st ar nA)) s a σ m) + (hNoSw : ∀ u, σ.schedulerAction ≠ .switchThread u) + (hCl : Clock a σ.machineState) + (hClVm : ∀ s' a' ms (u : U64), Clock a' ms → vspace.set_vm_root st ar s' a' u ⦃ r => Clock r.2 ms ⦄) + (hClFlushState : ∀ a' ms (vc om : U64), Clock a' ms → ar.vcpu_flush_state a' vc om ⦃ a'' => Clock a'' ms ⦄) + (hClFr : ∀ a' ms ms', Clock a' ms → ms'.timeState = ms.timeState → + ms'.lastMachineTime = ms.lastMachineTime → Clock a' ms') + (hP : VcpuPlatAgreeA ar) (hAK : VcpuArchKeepsA ar AR) (hVR : VcpuVmRootA st ar AR) + {ct : U64} (hct : ct ∈ Z.T) + {stop : U64} {stopSpec : RunStop} (hstop : stop.bv = stopSpec.word) (w1 w2 w3 : U64) : + vcpu.complete_run st ar s a ct stop w1 w2 w3 ⦃ res => + ∃ (hw' : HwRootA) (σ' : Fl4SpecA64.State Unit), + fl4CompleteRun (Z := Unit) (specPtr ct) stopSpec w1.bv w2.bv w3.bv σ = ({((), σ')}, False) ∧ + JStage3A st AR { Z with hw := hw' } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) res.1 res.2 σ' m ∧ + (∀ u, σ'.schedulerAction ≠ .switchThread u) ∧ + Clock res.2 σ'.machineState ∧ + σ'.machineState.timeState = σ.machineState.timeState ∧ + σ'.machineState.lastMachineTime = σ.machineState.lastMachineTime ∧ + L.mem res.1 (runVcpuA ct) = 0#u64 ∧ + L.ksb res.1 = L.ksb s ∧ L.cpu res.1 = L.cpu s ∧ + OSKeep st OS s res.1 ⦄ := by + obtain ⟨Pt, Ub, Pp', Tb', Hr, Te, Kt, Lv', Re, bits, KLv, W⟩ := J7A.unp J.j7 + unfold vcpu.complete_run + have hrv_const : layout.tcb.RUN_VCPU = ok 3672#u64 := + eq_of_spec_fd Fl4ProofsA64.P0.Consts.layout.tcb.RUN_VCPU.spec + have hrun_eq : wa ct 3672#u64 = runVcpuA ct := rfl + rw [hrv_const, bind_ok, lift_okH, bind_ok, hrun_eq, L.read_eq, bind_ok] + obtain ⟨xct, hkc, -, hrv_spec⟩ := J.j.2.1.tcb ct hct + by_cases hvc0 : L.mem s (runVcpuA ct) = 0#u64 + · rw [if_pos hvc0, WP.spec_ok] + have hsp : fl4CompleteRun (Z := Unit) (specPtr ct) stopSpec w1.bv w2.bv w3.bv σ = ({((), σ)}, False) := by + unfold fl4CompleteRun + rw [nd_bind_oneSc (getRunVcpu_eqA hkc), hrv_spec, hvc0]; rfl + exact ⟨Z.hw, σ, hsp, J, hNoSw, hCl, rfl, rfl, hvc0, rfl, rfl, fun _ _ => ⟨rfl, rfl, rfl, rfl⟩⟩ + · rw [if_neg hvc0] + refine vcpu_complete_run_pre_sts_coreA (J.toVcpuCoreA hNoSw) J.schedValid (j3aVcpuRec6A J) hCl + hClFlushState hClFr hP hAK hct hkc hvc0 hstop w1 w2 w3 + (k := fun s10 a1 => do + let s11 ← sched.set_thread_state st s10 ct sched.TS_RUNNING + let cur ← sched.cur_thread st s11 + let b ← obj.Tcb.Insts.CoreCmpPartialEqTcb.eq ct cur + if b then vspace.set_vm_root st ar s11 a1 ct else ok (s11, a1)) ?_ + rintro s10 a1 ⟨σ10, x10, hsp_pre, hkc10, hrun10, -, -, -, -, + J10_c, -, hw10, hSV10, R6_10, hCl10, ht10, hl10, hrt10, -, -, -, hksb10, hcpu10, osk10, -⟩ + obtain ⟨sysl10, symL10⟩ := sysL_symL_of_vcpuWrA J W hNoSw hw10 + have J10 : JStage3A st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) + (J7A st (J7R2A st ar nA)) s10 a1 σ10 m := by + have hZ : ({ Z with hw := Z.hw } : XSetsA) = Z := by cases Z; rfl + have h := J10_c.toJA_vcpuA J hSV10 R6_10 osk10 sysl10 symL10 + rwa [hZ] at h + refine WP.spec_bind (set_thread_state_running_stage3A J10 J10_c.noSwitch hCl10 hct hkc10 hrun10 hrt10) ?_ + rintro s11 ⟨σ11, hsts11, -, J11, hNoSw11, hCl11, hms11, hksb11, hcpu11, osk11, hw11⟩ + refine WP.spec_mono (vcpu_set_vm_root_if_cur_coreA (J11.toVcpuCoreA hNoSw11) (j3aVcpuRec6A J11) hCl11 hClVm hVR ct) ?_ + rintro ⟨sf, af⟩ ⟨hsf, hw', Jf_c, hClf⟩ + dsimp only at hsf Jf_c hClf; rcases hsf.symm with rfl + have hsp_full : fl4CompleteRun (Z := Unit) (specPtr ct) stopSpec w1.bv w2.bv w3.bv σ = ({((), σ11)}, False) := + hsp_pre.trans hsts11 + have hOskF : OSKeep st OS s s11 := fun y hy => + ⟨(osk11 y hy).1.trans (osk10 y hy).1, (osk11 y hy).2.1.trans (osk10 y hy).2.1, + (osk11 y hy).2.2.1.trans (osk10 y hy).2.2.1, (osk11 y hy).2.2.2.trans (osk10 y hy).2.2.2⟩ + have hrt11 : L.mem s11 (runVcpuA ct) = 0#u64 := by + have htH : H ct = some .tcb := show ct ∈ H.tcbs by rw [J.tcbs]; exact hct + have hft := J10.sys.ent.heap.fits ct .tcb htH; change ct.val + 4096 ≤ 2 ^ 64 at hft + have hrv_val : (runVcpuA ct).val = ct.val + 3672 := by + unfold runVcpuA; rw [wa_valm]; change (ct.val + 3672) % _ = _; exact Nat.mod_eq_of_lt (by omega) + have hst_val : (stA ct).val = ct.val + 3328 := by + rw [stA_eq, wa_valm]; change (ct.val + 3328) % _ = _; exact Nat.mod_eq_of_lt (by omega) + have hal : Aligned (runVcpuA ct) := field_aligned (J10.sr.core.tcbs ct hct) (by decide) (by decide) + rw [hw11.mem_eq hal (fun h => by + rcases h with h | h + · have := congrArg UScalar.val h; rw [hrv_val, hst_val] at this; omega + · have h16 : layout.node.SCHED_ACTION.val = 16 := by unfold layout.node.SCHED_ACTION; rfl + have hna := nodeAt_val (J10.sq.nodes _ J10.sr.sch.cpuCs) (by rw [h16]; decide) + unfold LawfulStore.nodeBase at hna + have hcpu128 : (L.cpu s10).val < 128 := by + obtain ⟨nn, -, hkn, pcs⟩ := J10.sr.toSchedSRN.csN; have := (pcs _).1 J10.sr.sch.cpuCs; omega + have hfit := J10.sys.ent.ksw + have hko := J10.sr.tcb_ks hct; unfold ksSizeA at hko + have ev := congrArg UScalar.val h; rw [hrv_val, hna, h16] at ev; omega)] + exact hrt10 + exact ⟨hw', σ11, hsp_full, + Jf_c.toJA_vcpuA J11 J11.schedValid ((j3aVcpuRec6A J11).with_hw hw') (fun _ _ => ⟨rfl, rfl, rfl, rfl⟩) J11.sys.sysl J11.symL, + hNoSw11, hClf, by rw [hms11]; exact ht10, by rw [hms11]; exact hl10, hrt11, + hksb11.trans hksb10, hcpu11.trans hcpu10, hOskF⟩ + +/-! ## Section 3: `vcpu.unbind` on `JStage3TA` -/ + +private theorem spec_bind_prodH {α β γ : Type} {m : Result (α × β)} {a : α} {b : β} + {f : α → β → Result γ} {P : γ → Prop} + (hm : m = ok (a, b)) (hf : f a b ⦃ P ⦄) : + (do let (x, y) ← m; f x y) ⦃ P ⦄ := by + rw [hm, bind_ok]; exact hf + +private theorem vcpu_unbind_phaseA_vm_stage3TA + {AR : A → HwRootA → Prop} {Clock : A → Fl4SpecA64.MachineState → Prop} + {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J7 : JSlotA S} + [J7PpA st J7] [J7PpKA st J7] [J7FrA st J7] + {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + (hSV : SchedValidA σ) (R6 : VcpuRec6A st Z OS Bf s σ) (hCl : Clock a σ.machineState) + (hClVm : ∀ s' a' ms (u : U64), Clock a' ms → vspace.set_vm_root st ar s' a' u ⦃ r => Clock r.2 ms ⦄) + (hClFlushState : ∀ a' ms (vc om : U64), Clock a' ms → ar.vcpu_flush_state a' vc om ⦃ a'' => Clock a'' ms ⦄) + (hClFr : ∀ a' ms ms', Clock a' ms → ms'.timeState = ms.timeState → + ms'.lastMachineTime = ms.lastMachineTime → Clock a' ms') + (hP : VcpuPlatAgreeA ar) (hAK : VcpuArchKeepsA ar AR) (hVR : VcpuVmRootA st ar AR) + {vc : U64} (hvc : vc ∈ Z.V) (hvc_nz : vc ≠ 0#u64) + {xv : Vcpu} (hkv : σ.kheap (specPtr vc) = some (.archObj (.vcpu xv))) + {xc : Tcb} (hcT : L.mem s (wa vc 24#u64) ∈ Z.T) + (hkc : σ.kheap (specPtr (L.mem s (wa vc 24#u64))) = some (.tcb xc)) + (hrc_some : xv.vcpuExt.runCaller = some (specPtr (L.mem s (wa vc 24#u64)))) + (hback : L.mem s (runVcpuA (L.mem s (wa vc 24#u64))) = vc) + (hvm : L.mem s (stA (L.mem s (wa vc 24#u64))) = sched.TS_RUNNING_VM) : + vcpu.complete_run st ar s a (L.mem s (wa vc 24#u64)) fl4_abi.consts.vcpu_run_stop.INTERRUPT 0#u64 0#u64 0#u64 ⦃ r1 => + ∃ (hw1 : HwRootA) (σ1 : Fl4SpecA64.State Unit), + fl4VcpuEndRun (Z := Unit) (specPtr vc) σ = ({((), σ1)}, False) ∧ + JStage3TA st AR { Z with hw := hw1 } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 r1.1 r1.2 σ1 m ∧ + SchedValidA σ1 ∧ VcpuRec6A st { Z with hw := hw1 } OS Bf r1.1 σ1 ∧ Clock r1.2 σ1.machineState ∧ + σ1.machineState.timeState = σ.machineState.timeState ∧ + σ1.machineState.lastMachineTime = σ.machineState.lastMachineTime ∧ + L.mem r1.1 (wa vc 24#u64) = 0#u64 ∧ L.mem r1.1 vc = L.mem s vc ∧ + L.ksb r1.1 = L.ksb s ∧ L.cpu r1.1 = L.cpu s ∧ OSKeep st OS s r1.1 ∧ + (∀ u ∈ Z.T, L.mem r1.1 (boundA u) = L.mem s (boundA u) ∧ L.mem r1.1 (scA u) = L.mem s (scA u)) ∧ + (∀ u ∈ Z.T, u ≠ L.mem s (wa vc 24#u64) → L.mem r1.1 (stA u) = L.mem s (stA u) ∧ L.mem r1.1 (runVcpuA u) = L.mem s (runVcpuA u)) ∧ + (∀ u ∈ Z.T, ∀ x, σ.kheap (specPtr u) = some (.tcb x) → + ∃ x', σ1.kheap (specPtr u) = some (.tcb x') ∧ + x'.tcbArch.tcbVcpu = x.tcbArch.tcbVcpu ∧ + x'.tcbBoundNotification = x.tcbBoundNotification ∧ + x'.tcbSchedContext = x.tcbSchedContext ∧ + (u ≠ L.mem s (wa vc 24#u64) → x'.tcbState = x.tcbState ∧ x'.tcbExt.tcbRunVcpu = x.tcbExt.tcbRunVcpu)) ∧ + FrKeepA st OS s σ r1.1 σ1 ⦄ := by + have hvm_spec : xc.tcbState = .ext .runningVM := by + have hcode := (J.ep.at_tcb hcT hkc).st; rw [hvm] at hcode + have h8 : sched.TS_RUNNING_VM.val = 8 := by unfold sched.TS_RUNNING_VM; rfl + rw [h8] at hcode + cases hs : xc.tcbState with + | ext e => cases e; rfl + | _ => rw [hs] at hcode; simp [tsCode] at hcode + have hint : (fl4_abi.consts.vcpu_run_stop.INTERRUPT : U64).bv = RunStop.interrupt.word := by + unfold fl4_abi.consts.vcpu_run_stop.INTERRUPT RunStop.word; rfl + refine WP.spec_mono (vcpu_complete_run_stage3TA J hSV R6 hCl hClVm hClFlushState hClFr hP hAK hVR hcT hint 0#u64 0#u64 0#u64) ?_ + rintro r1 ⟨hw1, σ1, hsp_cr, J1, hSV1, R6_1, hCl1, ht1, hl1, -, hvc24_1, hvc0_1, -, hksb1, hcpu1, osk1, hbs1, hst1, hkh1, fk⟩ + have hvc_ne0 : L.mem s (runVcpuA (L.mem s (wa vc 24#u64))) ≠ 0#u64 := by rw [hback]; exact hvc_nz + refine ⟨hw1, σ1, (fl4VcpuEndRun_runningVmA hkv hkc hrc_some hvm_spec).trans hsp_cr, + J1, hSV1, R6_1, hCl1, ht1, hl1, by rw [← hback]; exact hvc24_1 hvc_ne0, hvc0_1 vc hvc, + hksb1, hcpu1, osk1, hbs1, fun u hu hne => ⟨hst1 u hu hne, ?_⟩, hkh1, fk⟩ + obtain ⟨x0, hku0, -, hrv0⟩ := J.j.2.1.tcb u hu + obtain ⟨x1, hku1, -, hrv1⟩ := J1.j.2.1.tcb u hu + obtain ⟨x1', hku1', -, -, -, hne_sp⟩ := hkh1 u hu x0 hku0 + rw [hku1'] at hku1; cases hku1 + have hrv_eq : x1.tcbExt.tcbRunVcpu = x0.tcbExt.tcbRunVcpu := (hne_sp hne).2 + rw [hrv0, hrv1] at hrv_eq + unfold optRefA at hrv_eq + by_cases h10 : L.mem r1.1 (runVcpuA u) = 0#u64 + · by_cases h00 : L.mem s (runVcpuA u) = 0#u64 + · rw [h10, h00] + · simp [h10, h00] at hrv_eq + · by_cases h00 : L.mem s (runVcpuA u) = 0#u64 + · simp [h10, h00] at hrv_eq + · simp [h10, h00] at hrv_eq; exact specPtr_inj hrv_eq + +private theorem vcpu_unbind_phaseA_stale_stage3TA + {AR : A → HwRootA → Prop} {Clock : A → Fl4SpecA64.MachineState → Prop} + {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J7 : JSlotA S} + [J7PpA st J7] [J7PpKA st J7] [J7FrA st J7] + {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + (hSV : SchedValidA σ) (R6 : VcpuRec6A st Z OS Bf s σ) (hCl : Clock a σ.machineState) + {vc : U64} (hvc : vc ∈ Z.V) + {xv : Vcpu} (hkv : σ.kheap (specPtr vc) = some (.archObj (.vcpu xv))) + {xc : Tcb} (hcT : L.mem s (wa vc 24#u64) ∈ Z.T) + (hkc : σ.kheap (specPtr (L.mem s (wa vc 24#u64))) = some (.tcb xc)) + (hrc_some : xv.vcpuExt.runCaller = some (specPtr (L.mem s (wa vc 24#u64)))) + (hback : L.mem s (runVcpuA (L.mem s (wa vc 24#u64))) = vc) + (hvm : L.mem s (stA (L.mem s (wa vc 24#u64))) ≠ sched.TS_RUNNING_VM) : + (do + let i2 ← lift (wa vc 24#u64) + let s3 ← st.write s i2 0#u64 + let i3 ← layout.tcb.RUN_VCPU + let i4 ← lift (wa (L.mem s (wa vc 24#u64)) i3) + let s4 ← st.write s3 i4 0#u64 + ok (s4, a)) ⦃ r1 => + ∃ (hw1 : HwRootA) (σ1 : Fl4SpecA64.State Unit), + fl4VcpuEndRun (Z := Unit) (specPtr vc) σ = ({((), σ1)}, False) ∧ + JStage3TA st AR { Z with hw := hw1 } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 r1.1 r1.2 σ1 m ∧ + SchedValidA σ1 ∧ VcpuRec6A st { Z with hw := hw1 } OS Bf r1.1 σ1 ∧ Clock r1.2 σ1.machineState ∧ + σ1.machineState.timeState = σ.machineState.timeState ∧ + σ1.machineState.lastMachineTime = σ.machineState.lastMachineTime ∧ + L.mem r1.1 (wa vc 24#u64) = 0#u64 ∧ L.mem r1.1 vc = L.mem s vc ∧ + L.ksb r1.1 = L.ksb s ∧ L.cpu r1.1 = L.cpu s ∧ OSKeep st OS s r1.1 ∧ + (∀ u ∈ Z.T, L.mem r1.1 (boundA u) = L.mem s (boundA u) ∧ L.mem r1.1 (scA u) = L.mem s (scA u)) ∧ + (∀ u ∈ Z.T, u ≠ L.mem s (wa vc 24#u64) → L.mem r1.1 (stA u) = L.mem s (stA u) ∧ L.mem r1.1 (runVcpuA u) = L.mem s (runVcpuA u)) ∧ + (∀ u ∈ Z.T, ∀ x, σ.kheap (specPtr u) = some (.tcb x) → + ∃ x', σ1.kheap (specPtr u) = some (.tcb x') ∧ + x'.tcbArch.tcbVcpu = x.tcbArch.tcbVcpu ∧ + x'.tcbBoundNotification = x.tcbBoundNotification ∧ + x'.tcbSchedContext = x.tcbSchedContext ∧ + (u ≠ L.mem s (wa vc 24#u64) → x'.tcbState = x.tcbState ∧ x'.tcbExt.tcbRunVcpu = x.tcbExt.tcbRunVcpu)) ∧ + FrKeepA st OS s σ r1.1 σ1 ⦄ := by + have hnvm_spec : xc.tcbState ≠ .ext .runningVM := fun e => by + have hcode := (J.ep.at_tcb hcT hkc).st; rw [e] at hcode + have h8 : sched.TS_RUNNING_VM.val = 8 := by unfold sched.TS_RUNNING_VM; rfl + exact hvm (UScalar.eq_of_val_eq (by rw [h8, hcode]; rfl)) + have hrv_const : layout.tcb.RUN_VCPU = ok 3672#u64 := + eq_of_spec_fd Fl4ProofsA64.P0.Consts.layout.tcb.RUN_VCPU.spec + have hrun_eq : wa (L.mem s (wa vc 24#u64)) 3672#u64 = runVcpuA (L.mem s (wa vc 24#u64)) := rfl + rw [lift_okH, bind_ok, L.write_eq, bind_ok, hrv_const, bind_ok, lift_okH, bind_ok, hrun_eq, L.write_eq, bind_ok, WP.spec_ok] + obtain ⟨J1, hHS1, -, hSV1, R6_1, hCl1, hvc24_1, -, hvc0_1, -, htcb1, hksb1, hcpu1, osk1, fk⟩ := + vcpu_clear_run_pair_coreA J.toVcpuCoreA hSV R6 hCl hvc hcT rfl hback hkv hkc + refine ⟨Z.hw, _, fl4VcpuEndRun_staleA hkv hkc hrc_some hnvm_spec, J1.toTA (J.sact.of_vcpuStepA hHS1), hSV1, R6_1, hCl1, + rfl, rfl, hvc24_1, hvc0_1 vc hvc, hksb1, hcpu1, osk1, ?_, ?_, ?_, fk⟩ + · intro u hu + refine ⟨?_, ?_⟩ + · unfold boundA + exact htcb1 u hu 3368#u64 (by change (3368 : Nat) < 4096; omega) (by change (3368 : Nat) % 8 = 0; omega) + (Or.inr (fun e => by have h0 : (3368 : Nat) = 3672 := congrArg UScalar.val e; omega)) + · unfold scA + exact htcb1 u hu 3464#u64 (by change (3464 : Nat) < 4096; omega) (by change (3464 : Nat) % 8 = 0; omega) + (Or.inr (fun e => by have h0 : (3464 : Nat) = 3672 := congrArg UScalar.val e; omega)) + · intro u hu hne + exact ⟨by rw [stA_eq, htcb1 u hu 3328#u64 (by change (3328 : Nat) < 4096; omega) (by change (3328 : Nat) % 8 = 0; omega) (Or.inl hne), ← stA_eq], + by unfold runVcpuA; exact htcb1 u hu 3672#u64 (by change (3672 : Nat) < 4096; omega) (by change (3672 : Nat) % 8 = 0; omega) (Or.inl hne)⟩ + · intro u hu y hy + by_cases eu : u = L.mem s (wa vc 24#u64) + · rw [eu] at hy ⊢; rw [hkc] at hy; cases hy + exact ⟨{ xc with tcbExt := { xc.tcbExt with tcbRunVcpu := none } }, updKSc_same _ _ _, rfl, rfl, rfl, fun h => absurd rfl h⟩ + · have hne1 : specPtr u ≠ specPtr (L.mem s (wa vc 24#u64)) := fun e => eu (specPtr_inj e) + have huH : H u = some .tcb := show u ∈ H.tcbs by rw [J.tcbs]; exact hu + obtain ⟨_, _, -, arm, -, -, -, -, -, -, -⟩ := J7FrA.fr (st := st) _ _ _ _ _ _ J.j7 + have hvH := arm.vh vc hvc + have hne2 : specPtr u ≠ specPtr vc := fun e => by + have : u = vc := specPtr_inj e; rw [this, hvH] at huH; cases huH + exact ⟨y, (updKSc_other _ hne1).trans ((updKSc_other _ hne2).trans hy), rfl, rfl, rfl, fun _ => ⟨rfl, rfl⟩⟩ + +private theorem vcpu_unbind_phaseA_stage3TA + {AR : A → HwRootA → Prop} {Clock : A → Fl4SpecA64.MachineState → Prop} + {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J7 : JSlotA S} + [J7PpA st J7] [J7PpKA st J7] [J7FrA st J7] + {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + (hSV : SchedValidA σ) + (R6 : VcpuRec6A st Z OS Bf s σ) + (hCl : Clock a σ.machineState) + (hClVm : ∀ s' a' ms (u : U64), Clock a' ms → vspace.set_vm_root st ar s' a' u ⦃ r => Clock r.2 ms ⦄) + (hClFlushState : ∀ a' ms (vc om : U64), Clock a' ms → ar.vcpu_flush_state a' vc om ⦃ a'' => Clock a'' ms ⦄) + (hClFr : ∀ a' ms ms', Clock a' ms → ms'.timeState = ms.timeState → + ms'.lastMachineTime = ms.lastMachineTime → Clock a' ms') + (hP : VcpuPlatAgreeA ar) (hAK : VcpuArchKeepsA ar AR) (hVR : VcpuVmRootA st ar AR) + (hCs : ∀ c ∈ Z.Cs, c.val < Fl4SpecA64.fl4A64Platform.numNodes) + (h1 : Fl4SpecA64.fl4A64Platform.numNodes = 1) + {vc : U64} (hvc : vc ∈ Z.V) (hvc_nz : vc ≠ 0#u64) : + (if L.mem s (wa vc 24#u64) != 0#u64 then do + let (s2, a2) ← smp.remote_tcb_stall st ar s a (L.mem s (wa vc 24#u64)) + let i1 ← obj.Tcb.state st (L.mem s (wa vc 24#u64)) s2 + if i1 = sched.TS_RUNNING_VM then + vcpu.complete_run st ar s2 a2 (L.mem s (wa vc 24#u64)) fl4_abi.consts.vcpu_run_stop.INTERRUPT 0#u64 0#u64 0#u64 + else + let i2 ← lift (wa vc 24#u64) + let s3 ← st.write s2 i2 0#u64 + let i3 ← layout.tcb.RUN_VCPU + let i4 ← lift (wa (L.mem s (wa vc 24#u64)) i3) + let s4 ← st.write s3 i4 0#u64 + ok (s4, a2) + else ok (s, a)) ⦃ r1 => + ∃ (hw1 : HwRootA) (σ1 : Fl4SpecA64.State Unit), + fl4VcpuEndRun (Z := Unit) (specPtr vc) σ = ({((), σ1)}, False) ∧ + JStage3TA st AR { Z with hw := hw1 } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 r1.1 r1.2 σ1 m ∧ + SchedValidA σ1 ∧ + VcpuRec6A st { Z with hw := hw1 } OS Bf r1.1 σ1 ∧ + Clock r1.2 σ1.machineState ∧ + σ1.machineState.timeState = σ.machineState.timeState ∧ + σ1.machineState.lastMachineTime = σ.machineState.lastMachineTime ∧ + L.mem r1.1 (wa vc 24#u64) = 0#u64 ∧ + L.mem r1.1 vc = L.mem s vc ∧ + L.ksb r1.1 = L.ksb s ∧ L.cpu r1.1 = L.cpu s ∧ + OSKeep st OS s r1.1 ∧ + (∀ u ∈ Z.T, L.mem r1.1 (boundA u) = L.mem s (boundA u) ∧ L.mem r1.1 (scA u) = L.mem s (scA u)) ∧ + (∀ u ∈ Z.T, u ≠ L.mem s (wa vc 24#u64) → L.mem r1.1 (stA u) = L.mem s (stA u) ∧ L.mem r1.1 (runVcpuA u) = L.mem s (runVcpuA u)) ∧ + (∀ u ∈ Z.T, ∀ x, σ.kheap (specPtr u) = some (.tcb x) → + ∃ x', σ1.kheap (specPtr u) = some (.tcb x') ∧ + x'.tcbArch.tcbVcpu = x.tcbArch.tcbVcpu ∧ + x'.tcbBoundNotification = x.tcbBoundNotification ∧ + x'.tcbSchedContext = x.tcbSchedContext ∧ + (u ≠ L.mem s (wa vc 24#u64) → x'.tcbState = x.tcbState ∧ x'.tcbExt.tcbRunVcpu = x.tcbExt.tcbRunVcpu)) ∧ + FrKeepA st OS s σ r1.1 σ1 ⦄ := by + set caller_ptr := L.mem s (wa vc 24#u64) + obtain ⟨xv, hkv, hvt_spec, hrc_spec⟩ := J.j.2.1.obj vc hvc + by_cases hc0 : caller_ptr = 0#u64 + · have hbn : (caller_ptr != 0#u64) = false := bne_eq_false_iff_eq.mpr hc0 + rw [hbn, if_neg Bool.false_ne_true, WP.spec_ok] + have hrc_none : xv.vcpuExt.runCaller = none := by rw [hrc_spec]; change optRefA caller_ptr = none; unfold optRefA; simp [hc0] + exact ⟨Z.hw, σ, fl4VcpuEndRun_noneA hkv hrc_none, J, hSV, R6, hCl, rfl, rfl, hc0, rfl, + rfl, rfl, fun _ _ => ⟨rfl, rfl, rfl, rfl⟩, fun _ _ => ⟨rfl, rfl⟩, fun _ _ _ => ⟨rfl, rfl⟩, + fun _ _ x hx => ⟨x, hx, rfl, rfl, rfl, fun _ => ⟨rfl, rfl⟩⟩, FrKeepA.refl⟩ + · have hbn : (caller_ptr != 0#u64) = true := bne_iff_ne.mpr hc0 + rw [if_pos hbn] + have hcT : caller_ptr ∈ Z.T := (J.vcRun.vcpu vc hvc).resolve_left hc0 + have hback : L.mem s (runVcpuA caller_ptr) = vc := R6.vcSym2 vc hvc hc0 + obtain ⟨xc, hkc, -, -⟩ := J.j.2.1.tcb caller_ptr hcT + have hrc_some : xv.vcpuExt.runCaller = some (specPtr caller_ptr) := by + rw [hrc_spec]; change optRefA caller_ptr = some (specPtr caller_ptr); unfold optRefA; simp [hc0] + refine spec_bind_prodH (remote_tcb_stall_singleA J hCs h1 hcT) ?_ + have hst_read : obj.Tcb.state st caller_ptr s = ok (L.mem s (stA caller_ptr)) := by + unfold obj.Tcb.state stA + rw [lift_okH, bind_ok, L.read_eq] + rw [hst_read, bind_ok] + by_cases hvm : L.mem s (stA caller_ptr) = sched.TS_RUNNING_VM + · rw [if_pos hvm] + exact vcpu_unbind_phaseA_vm_stage3TA J hSV R6 hCl hClVm hClFlushState hClFr hP hAK hVR hvc hvc_nz hkv hcT hkc hrc_some hback hvm + · rw [if_neg hvm] + exact vcpu_unbind_phaseA_stale_stage3TA J hSV R6 hCl hvc hkv hcT hkc hrc_some hback hvm + +private theorem vcpu_unbind_phaseB_stage3TA + {AR : A → HwRootA → Prop} {Clock : A → Fl4SpecA64.MachineState → Prop} + {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J7 : JSlotA S} + [J7PpA st J7] [J7PpKA st J7] [J7FrA st J7] + {s1 : S} {a1 : A} {σ1 : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J1 : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s1 a1 σ1 m) + (hSV1 : SchedValidA σ1) + (R6_1 : VcpuRec6A st Z OS Bf s1 σ1) + (hCl1 : Clock a1 σ1.machineState) + (hClVm : ∀ s' a' ms (u : U64), Clock a' ms → vspace.set_vm_root st ar s' a' u ⦃ r => Clock r.2 ms ⦄) + (hClFlush : ∀ a' ms (vc : U64), Clock a' ms → ar.vcpu_flush a' vc ⦃ a'' => Clock a'' ms ⦄) + (hAK : VcpuArchKeepsA ar AR) (hVR : VcpuVmRootA st ar AR) + (hCs : ∀ c ∈ Z.Cs, c.val < Fl4SpecA64.fl4A64Platform.numNodes) + (h1 : Fl4SpecA64.fl4A64Platform.numNodes = 1) + {vc : U64} (hvc : vc ∈ Z.V) (hvc_nz : vc ≠ 0#u64) : + (do + let i1 ← lift (wa vc vcpu.TCB) + let t_ptr ← st.read s1 i1 + if t_ptr = 0#u64 then ok (s1, a1) + else do + let (s2, a2) ← smp.remote_tcb_stall st ar s1 a1 t_ptr + let a3 ← ar.vcpu_flush a2 vc + let i2 ← lift (wa vc vcpu.TCB) + let s3 ← st.write s2 i2 0#u64 + let i3 ← lift (wa vc vcpu.OWNER_CPU) + let s4 ← st.write s3 i3 0#u64 + let s5 ← vcpu.set_bound st s4 t_ptr 0#u64 + let spsr ← obj.Tcb.reg st t_ptr s5 layout.reg.SPSR_EL1 + let spsr1 ← regs.sanitise layout.reg.SPSR_EL1 spsr + let s6 ← obj.Tcb.set_reg st t_ptr s5 layout.reg.SPSR_EL1 spsr1 + let cur ← sched.cur_thread st s6 + let b ← obj.Tcb.Insts.CoreCmpPartialEqTcb.eq t_ptr cur + if b then vspace.set_vm_root st ar s6 a3 t_ptr else ok (s6, a3)) ⦃ res => + ∃ (hw' : HwRootA) (σ' : Fl4SpecA64.State Unit), + (do + let v ← getVcpu (Z := Unit) (specPtr vc) + match v.vcpuTcb with + | some t => dissociateVcpuTcb (specPtr vc) t + | none => pure ()) σ1 = ({((), σ')}, False) ∧ + JStage3TA st AR { Z with hw := hw' } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 res.1 res.2 σ' m ∧ + SchedValidA σ' ∧ + VcpuRec6A st { Z with hw := hw' } OS Bf res.1 σ' ∧ + Clock res.2 σ'.machineState ∧ + σ'.machineState.timeState = σ1.machineState.timeState ∧ + σ'.machineState.lastMachineTime = σ1.machineState.lastMachineTime ∧ + L.mem res.1 vc = 0#u64 ∧ + L.mem res.1 (wa vc 24#u64) = L.mem s1 (wa vc 24#u64) ∧ + (L.mem s1 vc ≠ 0#u64 → L.mem res.1 (wa (L.mem s1 vc) 3664#u64) = 0#u64) ∧ + L.ksb res.1 = L.ksb s1 ∧ L.cpu res.1 = L.cpu s1 ∧ + OSKeep st OS s1 res.1 ∧ + (∀ u ∈ Z.T, L.mem res.1 (boundA u) = L.mem s1 (boundA u) ∧ L.mem res.1 (scA u) = L.mem s1 (scA u)) ∧ + (∀ u ∈ Z.T, L.mem res.1 (stA u) = L.mem s1 (stA u) ∧ L.mem res.1 (runVcpuA u) = L.mem s1 (runVcpuA u)) ∧ + (∀ u ∈ Z.T, ∀ x1, σ1.kheap (specPtr u) = some (.tcb x1) → + ∃ x', σ'.kheap (specPtr u) = some (.tcb x') ∧ + x'.tcbBoundNotification = x1.tcbBoundNotification ∧ + x'.tcbSchedContext = x1.tcbSchedContext ∧ + x'.tcbState = x1.tcbState ∧ + x'.tcbExt.tcbRunVcpu = x1.tcbExt.tcbRunVcpu ∧ + (u = L.mem s1 vc → x'.tcbArch.tcbVcpu = none)) ∧ + FrKeepA st OS s1 σ1 res.1 σ' ⦄ := by + have htcb_off : vcpu.TCB = 0#u64 := by simp [vcpu.TCB] + rw [htcb_off, lift_okH, bind_ok, wa_zeroH, L.read_eq, bind_ok] + set t_ptr := L.mem s1 vc + obtain ⟨xv1, hkv1, hvt1_spec, -⟩ := J1.j.2.1.obj vc hvc + by_cases ht0 : t_ptr = 0#u64 + · rw [if_pos ht0, WP.spec_ok] + have hvt_none : xv1.vcpuTcb = none := by rw [hvt1_spec]; change optRefA t_ptr = none; unfold optRefA; simp [ht0] + refine ⟨Z.hw, σ1, by rw [nd_bind_oneSc (getVcpu_eqA hkv1), hvt_none]; rfl, + J1, hSV1, R6_1, hCl1, rfl, rfl, ht0, rfl, + fun h => absurd ht0 h, rfl, rfl, fun _ _ => ⟨rfl, rfl, rfl, rfl⟩, + fun _ _ => ⟨rfl, rfl⟩, fun _ _ => ⟨rfl, rfl⟩, fun u hu x1 hx1 => ?_, FrKeepA.refl⟩ + refine ⟨x1, hx1, rfl, rfl, rfl, rfl, fun eu => ?_⟩ + exfalso; have hu0 : u ≠ 0#u64 := fun e => (J1.sr.core.tcbs u hu).ne0 (by rw [e]; rfl) + exact hu0 (eu.trans ht0) + · rw [if_neg ht0] + have htT : t_ptr ∈ Z.T := (J1.vcRun.vtcb vc hvc).resolve_left ht0 + have hback1 : L.mem s1 (wa t_ptr 3664#u64) = vc := R6_1.vcBack vc hvc ht0 + obtain ⟨xt1, hkt1, -, -⟩ := J1.j.2.1.tcb t_ptr htT + have hvt_some : xv1.vcpuTcb = some (specPtr t_ptr) := by + rw [hvt1_spec]; change optRefA t_ptr = some (specPtr t_ptr); unfold optRefA; simp [ht0] + rw [remote_tcb_stall_singleA J1 hCs h1 htT, bind_ok] + refine WP.spec_bind (WP.spec_and (hAK.vcpu_flush a1 Z.hw vc J1.j.1.ar) (hClFlush a1 σ1.machineState vc hCl1)) ?_ + rintro a3 ⟨har3, hCl3⟩ + have J1' : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s1 a3 σ1 m := + JStage3TA.of_vcpuStepA J1 R6_1 (WritesIn.refl s1 _) (VcpuSpecStepA.refl H σ1) har3 + (fun _ _ => rfl) J1.j.2.1 J1.vcRun J1.runVcpu J1.ep.tcb J1.ep.buf + J1.deadExt.vcpu J1.deadExt.tvcpu J1.deadExt.vcDeadRun J1.zombieNoVcpu + have hown_off : vcpu.OWNER_CPU = 8#u64 := by simp [vcpu.OWNER_CPU] + have hset_bound : ∀ s4, vcpu.set_bound st s4 t_ptr 0#u64 = ok (L.wr s4 (wa t_ptr 3664#u64) 0#u64) := fun s4 => by + unfold vcpu.set_bound + have hv_off : layout.tcb.VCPU = ok 3664#u64 := eq_of_spec_fd Fl4ProofsA64.P0.Consts.layout.tcb.VCPU.spec + rw [hv_off, bind_ok, lift_okH, bind_ok, L.write_eq] + have hspsr_idx : layout.reg.SPSR_EL1 = regIdxA .spsrEl1 := by simp [layout.reg.SPSR_EL1, regIdxA] + rw [bind_ok, L.write_eq, bind_ok, + hown_off, lift_okH, bind_ok, L.write_eq, bind_ok, + hset_bound, bind_ok, tcb_reg_eqH, bind_ok, regs_sanitise_spsr_eqH, bind_ok, + tcb_set_reg_eqH, bind_ok] + rw [hspsr_idx] + obtain ⟨hspB, J6, hSV6, R6_6, hCl6, hvc0_6, ht3664_6, hvc24_6, htcb6, hksb6, hcpu6, osk6, fk⟩ := + vcpu_dissoc_bind_stage3TA J1' hSV1 R6_1 hCl3 hvc hvc_nz htT rfl hback1 hkv1 hkt1 + refine WP.spec_mono (vcpu_set_vm_root_if_cur_coreA J6.toVcpuCoreA R6_6 hCl6 hClVm hVR t_ptr) ?_ + rintro ⟨sf, af⟩ ⟨hsf, hw', Jf, hClf⟩ + dsimp only at hsf Jf hClf; rcases hsf with rfl + set σ' := updKSc (updKSc σ1 (specPtr t_ptr) (.tcb (dissociateTcbA xt1))) (specPtr vc) + (.archObj (.vcpu { xv1 with vcpuTcb := none })) + refine ⟨hw', σ', by rw [nd_bind_oneSc (getVcpu_eqA hkv1), hvt_some]; exact hspB, + Jf.toTA J6.sact, hSV6, R6_6.with_hw hw', hClf, rfl, rfl, hvc0_6, hvc24_6, + fun _ => ht3664_6, hksb6, hcpu6, osk6, ?_, ?_, ?_, fk⟩ + · intro u hu + exact ⟨by unfold boundA; exact htcb6 u hu _ (by simp) (by simp) (Or.inr ⟨fun e => by have := congrArg UScalar.val e; simp at this, Or.inr (by simp)⟩), + by unfold scA; exact htcb6 u hu _ (by simp) (by simp) (Or.inr ⟨fun e => by have := congrArg UScalar.val e; simp at this, Or.inr (by simp)⟩)⟩ + · intro u hu + exact ⟨by rw [stA_eq, htcb6 u hu 3328#u64 (by simp) (by simp) (Or.inr ⟨fun e => by have := congrArg UScalar.val e; simp at this, Or.inr (by simp)⟩), ← stA_eq], + by unfold runVcpuA; exact htcb6 u hu 3672#u64 (by simp) (by simp) (Or.inr ⟨fun e => by have := congrArg UScalar.val e; simp at this, Or.inr (by simp)⟩)⟩ + · intro u hu x1 hx1 + have hne_vc : specPtr u ≠ specPtr vc := fun e => by + have huH : H u = some .tcb := show u ∈ H.tcbs by rw [J1.tcbs]; exact hu + obtain ⟨_, _, -, arm, -, -, -, -, -, -, -⟩ := J7FrA.fr (st := st) _ _ _ _ _ _ J1.j7 + have hvH := arm.vh vc hvc + have : u = vc := specPtr_inj e; rw [this, hvH] at huH; cases huH + by_cases eu : u = t_ptr + · subst eu; rw [hkt1] at hx1; cases hx1 + exact ⟨dissociateTcbA xt1, by simp [σ', updKSc, Function.update, hne_vc], rfl, rfl, rfl, rfl, fun _ => rfl⟩ + · have hne_t : specPtr u ≠ specPtr t_ptr := fun e => eu (specPtr_inj e) + exact ⟨x1, by simp [σ', updKSc, Function.update, hne_vc, hne_t, hx1], rfl, rfl, rfl, rfl, fun eq => absurd eq eu⟩ + +/-- **`vcpu.unbind` refinement on `JStage3TA`**: ends any active run on `vc` (`fl4VcpuEndRun`), +then dissociates any bound TCB (`dissociateVcpuTcb`), unconditionally clearing both `vc + 0` +(`TCB`) and `vc + 24` (`RUN_CALLER`). -/ +theorem vcpu_unbind_stage3TA + {AR : A → HwRootA → Prop} {Clock : A → Fl4SpecA64.MachineState → Prop} + {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J7 : JSlotA S} + [J7PpA st J7] [J7PpKA st J7] [J7FrA st J7] + {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + (hSV : SchedValidA σ) + (R6 : VcpuRec6A st Z OS Bf s σ) + (hCl : Clock a σ.machineState) + (hClVm : ∀ s' a' ms (u : U64), Clock a' ms → vspace.set_vm_root st ar s' a' u ⦃ r => Clock r.2 ms ⦄) + (hClFlush : ∀ a' ms (vc : U64), Clock a' ms → ar.vcpu_flush a' vc ⦃ a'' => Clock a'' ms ⦄) + (hClFlushState : ∀ a' ms (vc om : U64), Clock a' ms → ar.vcpu_flush_state a' vc om ⦃ a'' => Clock a'' ms ⦄) + (hClFr : ∀ a' ms ms', Clock a' ms → ms'.timeState = ms.timeState → + ms'.lastMachineTime = ms.lastMachineTime → Clock a' ms') + (hP : VcpuPlatAgreeA ar) (hAK : VcpuArchKeepsA ar AR) (hVR : VcpuVmRootA st ar AR) + (hCs : ∀ c ∈ Z.Cs, c.val < Fl4SpecA64.fl4A64Platform.numNodes) + (h1 : Fl4SpecA64.fl4A64Platform.numNodes = 1) + {vc : U64} (hvc : vc ∈ Z.V) (hvc_nz : vc ≠ 0#u64) : + vcpu.unbind st ar s a vc ⦃ res => + ∃ (hw' : HwRootA) (σ' : Fl4SpecA64.State Unit), + fl4VcpuUnbind (specPtr vc) σ = ({((), σ')}, False) ∧ + JStage3TA st AR { Z with hw := hw' } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 res.1 res.2 σ' m ∧ + SchedValidA σ' ∧ + VcpuRec6A st { Z with hw := hw' } OS Bf res.1 σ' ∧ + Clock res.2 σ'.machineState ∧ + σ'.machineState.timeState = σ.machineState.timeState ∧ + σ'.machineState.lastMachineTime = σ.machineState.lastMachineTime ∧ + L.mem res.1 vc = 0#u64 ∧ + L.mem res.1 (wa vc 24#u64) = 0#u64 ∧ + (L.mem s vc ≠ 0#u64 → L.mem res.1 (wa (L.mem s vc) 3664#u64) = 0#u64) ∧ + L.ksb res.1 = L.ksb s ∧ L.cpu res.1 = L.cpu s ∧ + OSKeep st OS s res.1 ∧ + (∀ u ∈ Z.T, L.mem res.1 (boundA u) = L.mem s (boundA u) ∧ L.mem res.1 (scA u) = L.mem s (scA u)) ∧ + (∀ u ∈ Z.T, u ≠ L.mem s (wa vc 24#u64) → L.mem res.1 (stA u) = L.mem s (stA u) ∧ L.mem res.1 (runVcpuA u) = L.mem s (runVcpuA u)) ∧ + (∀ u ∈ Z.T, ∀ x, σ.kheap (specPtr u) = some (.tcb x) → + ∃ x', σ'.kheap (specPtr u) = some (.tcb x') ∧ + x'.tcbBoundNotification = x.tcbBoundNotification ∧ + x'.tcbSchedContext = x.tcbSchedContext ∧ + (u ≠ L.mem s (wa vc 24#u64) → x'.tcbState = x.tcbState ∧ x'.tcbExt.tcbRunVcpu = x.tcbExt.tcbRunVcpu) ∧ + (u = L.mem s vc → x'.tcbArch.tcbVcpu = none)) ∧ + FrKeepA st OS s σ res.1 σ' ⦄ := by + unfold vcpu.unbind + have hrc_off : vcpu.RUN_CALLER = 24#u64 := by simp [vcpu.RUN_CALLER] + rw [hrc_off, lift_okH, bind_ok, L.read_eq, bind_ok] + refine WP.spec_bind (vcpu_unbind_phaseA_stage3TA J hSV R6 hCl hClVm hClFlushState hClFr hP hAK hVR hCs h1 hvc hvc_nz) ?_ + rintro ⟨s1, a1⟩ ⟨hw1, σ1, hspA, J1, hSV1, R6_1, hCl1, ht1, hl1, hvc24_1, hvc0_1, hksb1, hcpu1, osk1, hbs1, hst1, hkh1, fk1⟩ + dsimp only at J1 hSV1 R6_1 hCl1 ht1 hl1 hvc24_1 hvc0_1 hksb1 hcpu1 osk1 hbs1 hst1 hkh1 ⊢ + refine WP.spec_mono (vcpu_unbind_phaseB_stage3TA J1 hSV1 R6_1 hCl1 hClVm hClFlush hAK hVR hCs h1 hvc hvc_nz) ?_ + rintro ⟨sf, af⟩ ⟨hw', σ', hspB, Jf, hSVf, R6_f, hClf, htf, hlf, hvc0_f, hvc24_f, ht3664_f, hksbf, hcpuf, oskf, hbsf, hstf, hkhf, fk2⟩ + dsimp only at Jf hSVf R6_f hClf htf hlf hvc0_f hvc24_f ht3664_f hksbf hcpuf oskf hbsf hstf hkhf ⊢ + refine ⟨hw', σ', by unfold fl4VcpuUnbind; rw [nd_bind_oneSc hspA]; exact hspB, + Jf, hSVf, R6_f, hClf, htf.trans ht1, hlf.trans hl1, hvc0_f, hvc24_f.trans hvc24_1, + fun h => by rw [← hvc0_1]; exact ht3664_f (by rw [hvc0_1]; exact h), + hksbf.trans hksb1, hcpuf.trans hcpu1, + fun y hy => ⟨(oskf y hy).1.trans (osk1 y hy).1, (oskf y hy).2.1.trans (osk1 y hy).2.1, + (oskf y hy).2.2.1.trans (osk1 y hy).2.2.1, (oskf y hy).2.2.2.trans (osk1 y hy).2.2.2⟩, + fun u hu => ⟨(hbsf u hu).1.trans (hbs1 u hu).1, (hbsf u hu).2.trans (hbs1 u hu).2⟩, + fun u hu hne => ⟨(hstf u hu).1.trans (hst1 u hu hne).1, (hstf u hu).2.trans (hst1 u hu hne).2⟩, + fun u hu x hx => ?_, fk1.trans fk2⟩ + obtain ⟨x1, hx1, -, hb1, hsc1, hne1⟩ := hkh1 u hu x hx + obtain ⟨x', hx', hbf, hscf, hstf_sp, hrvf_sp, htvf_sp⟩ := hkhf u hu x1 hx1 + exact ⟨x', hx', hbf.trans hb1, hscf.trans hsc1, + fun hne => ⟨hstf_sp.trans (hne1 hne).1, hrvf_sp.trans (hne1 hne).2⟩, + fun eu => htvf_sp (eu.trans hvc0_1.symm)⟩ + +/-! ## Section 4: `vcpu.finalise` on `JStage3TA` -/ + +/-- **`vcpu.finalise` refinement on `JStage3TA`**: calls `vcpu.unbind` followed by `ar.vcpu_flush`, +matching `fl4VcpuUnbind` and unconditionally clearing both `vc + 0` (`TCB`) and `vc + 24` (`RUN_CALLER`) +as required by `RvVcA`. -/ +theorem vcpu_finalise_stage3TA + {AR : A → HwRootA → Prop} {Clock : A → Fl4SpecA64.MachineState → Prop} + {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J7 : JSlotA S} + [J7PpA st J7] [J7PpKA st J7] [J7FrA st J7] + {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + (hSV : SchedValidA σ) + (R6 : VcpuRec6A st Z OS Bf s σ) + (hCl : Clock a σ.machineState) + (hClVm : ∀ s' a' ms (u : U64), Clock a' ms → vspace.set_vm_root st ar s' a' u ⦃ r => Clock r.2 ms ⦄) + (hClFlush : ∀ a' ms (vc : U64), Clock a' ms → ar.vcpu_flush a' vc ⦃ a'' => Clock a'' ms ⦄) + (hClFlushState : ∀ a' ms (vc om : U64), Clock a' ms → ar.vcpu_flush_state a' vc om ⦃ a'' => Clock a'' ms ⦄) + (hClFr : ∀ a' ms ms', Clock a' ms → ms'.timeState = ms.timeState → + ms'.lastMachineTime = ms.lastMachineTime → Clock a' ms') + (hP : VcpuPlatAgreeA ar) (hAK : VcpuArchKeepsA ar AR) (hVR : VcpuVmRootA st ar AR) + (hCs : ∀ c ∈ Z.Cs, c.val < Fl4SpecA64.fl4A64Platform.numNodes) + (h1 : Fl4SpecA64.fl4A64Platform.numNodes = 1) + {vc : U64} (hvc : vc ∈ Z.V) (hvc_nz : vc ≠ 0#u64) : + vcpu.finalise st ar s a vc ⦃ res => + ∃ (hw' : HwRootA) (σ' : Fl4SpecA64.State Unit), + fl4VcpuUnbind (specPtr vc) σ = ({((), σ')}, False) ∧ + JStage3TA st AR { Z with hw := hw' } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 res.1 res.2 σ' m ∧ + SchedValidA σ' ∧ + VcpuRec6A st { Z with hw := hw' } OS Bf res.1 σ' ∧ + Clock res.2 σ'.machineState ∧ + σ'.machineState.timeState = σ.machineState.timeState ∧ + σ'.machineState.lastMachineTime = σ.machineState.lastMachineTime ∧ + L.mem res.1 vc = 0#u64 ∧ + L.mem res.1 (wa vc 24#u64) = 0#u64 ∧ + L.ksb res.1 = L.ksb s ∧ L.cpu res.1 = L.cpu s ∧ + OSKeep st OS s res.1 ∧ + FrKeepA st OS s σ res.1 σ' ⦄ := by + unfold vcpu.finalise + refine WP.spec_bind (vcpu_unbind_stage3TA J hSV R6 hCl hClVm hClFlush hClFlushState hClFr hP hAK hVR hCs h1 hvc hvc_nz) ?_ + rintro ⟨s1, a1⟩ ⟨hw1, σ1, hsp, J1, hSV1, R6_1, hCl1, ht1, hl1, hvc0, hvc24, -, hksb1, hcpu1, osk1, -, -, -, fk⟩ + dsimp only at J1 hSV1 R6_1 hCl1 ht1 hl1 hvc0 hvc24 hksb1 hcpu1 osk1 ⊢ + refine WP.spec_bind (WP.spec_and (hAK.vcpu_flush a1 hw1 vc J1.j.1.ar) (hClFlush a1 σ1.machineState vc hCl1)) ?_ + rintro a2 ⟨har2, hCl2⟩ + rw [WP.spec_ok] + have J2 : JStage3TA st AR { Z with hw := hw1 } H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s1 a2 σ1 m := + JStage3TA.of_vcpuStepA J1 R6_1 (WritesIn.refl s1 _) (VcpuSpecStepA.refl H σ1) har2 + (fun _ _ => rfl) J1.j.2.1 J1.vcRun J1.runVcpu J1.ep.tcb J1.ep.buf + J1.deadExt.vcpu J1.deadExt.tvcpu J1.deadExt.vcDeadRun J1.zombieNoVcpu + exact ⟨hw1, σ1, hsp, J2, hSV1, R6_1, hCl2, ht1, hl1, hvc0, hvc24, hksb1, hcpu1, osk1, fk⟩ + +/-! ## Section 5: `vcpu.detach` on `JStage3TA` (`DelS4A.detach` plus `FrKeepA`) -/ + +/-- `JStage3TA`-level effect of `vcpu.detach` for the TCB finaliser (`par/a64-delhooks`). +Clears `t`'s VCPU binding (`t + 3664`), `t`'s `RUN_VCPU` (`t + 3672`), and (when `t` was bound +to `v₀ ≠ 0`) `v₀`'s `TCB` (`v₀ + 0`) and `RUN_CALLER` (`v₀ + 24`); preserves `JStage3TA`, +`SchedValidA` (F8), `VcSym1A`/`VcSym2A`, `vcBack`/`runBack`, `VcpuStateSnapA`, `UserFrSpecA`, +`OSKeep st OS s res.1`, `Clock` (`DelPpTimeA`), spec time fields (`timeState`/`lastMachineTime`), +and all non-VCPU/non-runner-wake TCB fields of `t`. + +Both internal `smp.remote_tcb_stall` calls are discharged from `[S] numNodes = 1` + `[P] hCs`. -/ +theorem vcpu_detach_stage3GA + {AR : A → HwRootA → Prop} {Clock : A → Fl4SpecA64.MachineState → Prop} + {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J7 : JSlotA S} + [J7PpA st J7] [J7PpKA st J7] [J7FrA st J7] + {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + (hSV : SchedValidA σ) + (hSnap : VcpuStateSnapA Z.V (L.mem s) σ) + (hVcSym1 : VcSym1A st Z.T s) (hVcSym2 : VcSym2A st Z.V s) + (hVcBack : ∀ v ∈ Z.V, L.mem s v ≠ 0#u64 → L.mem s (wa (L.mem s v) 3664#u64) = v) + (hRunBack : ∀ t ∈ Z.T, L.mem s (runVcpuA t) ≠ 0#u64 → L.mem s (wa (L.mem s (runVcpuA t)) 24#u64) = t) + (hVcOS : ∀ b, VcW Z.V b → ¬ InOS OS b) + (hFrOff : FrOff st Z.Cs OS (L.ksb s) s) + (hUserFr : UserFrSpecA st OS s σ) (hFrTh : FrThA st Z.th OS s) + (hBfSpec : ∀ bp ∈ Bf, bp.2 = specPtr bp.1) + (hCl : Clock a σ.machineState) + (hClVm : ∀ s' a' ms (u : U64), Clock a' ms → vspace.set_vm_root st ar s' a' u ⦃ r => Clock r.2 ms ⦄) + (hClFlush : ∀ a' ms (vc : U64), Clock a' ms → ar.vcpu_flush a' vc ⦃ a'' => Clock a'' ms ⦄) + (hClFlushState : ∀ a' ms (vc om : U64), Clock a' ms → ar.vcpu_flush_state a' vc om ⦃ a'' => Clock a'' ms ⦄) + (hClFr : ∀ a' ms ms', Clock a' ms → ms'.timeState = ms.timeState → + ms'.lastMachineTime = ms.lastMachineTime → Clock a' ms') + (hP : VcpuPlatAgreeA ar) (hAK : VcpuArchKeepsA ar AR) (hVR : VcpuVmRootA st ar AR) + (hCs : ∀ c ∈ Z.Cs, c.val < Fl4SpecA64.fl4A64Platform.numNodes) + (h1 : Fl4SpecA64.fl4A64Platform.numNodes = 1) + {t : U64} (ht : t ∈ Z.T) : + vcpu.detach st ar s a t ⦃ res => + ∃ (Q' : U64 → U64 → List U64) (R' : U64 → List U64) (hw' : HwRootA) (σ' : Fl4SpecA64.State Unit), + ((), σ') ∈ (fl4VcpuDetach (specPtr t) σ).1 ∧ + JStage3TA st AR { Z with Q := Q', R := R', hw := hw' } H OS U Ac Pp Tb Lv ia ie + N E Rp Bf stack κ n (J5A st) J7 res.1 res.2 σ' m ∧ + SchedValidA σ' ∧ + VcpuStateSnapA Z.V (L.mem res.1) σ' ∧ + VcSym1A st Z.T res.1 ∧ VcSym2A st Z.V res.1 ∧ + (∀ v ∈ Z.V, L.mem res.1 v ≠ 0#u64 → L.mem res.1 (wa (L.mem res.1 v) 3664#u64) = v) ∧ + (∀ u ∈ Z.T, L.mem res.1 (runVcpuA u) ≠ 0#u64 → L.mem res.1 (wa (L.mem res.1 (runVcpuA u)) 24#u64) = u) ∧ + UserFrSpecA st OS res.1 σ' ∧ + Clock res.2 σ'.machineState ∧ + σ'.machineState.timeState = σ.machineState.timeState ∧ + σ'.machineState.lastMachineTime = σ.machineState.lastMachineTime ∧ + L.mem res.1 (wa t 3664#u64) = 0#u64 ∧ + L.mem res.1 (runVcpuA t) = 0#u64 ∧ + (L.mem s (wa t 3664#u64) ≠ 0#u64 → + L.mem res.1 (L.mem s (wa t 3664#u64)) = 0#u64 ∧ + L.mem res.1 (wa (L.mem s (wa t 3664#u64)) 24#u64) = 0#u64) ∧ + L.ksb res.1 = L.ksb s ∧ L.cpu res.1 = L.cpu s ∧ + OSKeep st OS s res.1 ∧ + (∀ y ∈ OS, capA st res.1 y = capA st s y) ∧ + L.mem res.1 (boundA t) = L.mem s (boundA t) ∧ + L.mem res.1 (scA t) = L.mem s (scA t) ∧ + L.mem res.1 (stA t) = L.mem s (stA t) ∧ + (∀ x, σ.kheap (specPtr t) = some (.tcb x) → + ∃ x', σ'.kheap (specPtr t) = some (.tcb x') ∧ + x'.tcbArch.tcbVcpu = none ∧ x'.tcbExt.tcbRunVcpu = none ∧ + x'.tcbBoundNotification = x.tcbBoundNotification ∧ + x'.tcbSchedContext = x.tcbSchedContext ∧ + x'.tcbState = x.tcbState) ∧ + FrKeepA st OS s σ res.1 σ' ⦄ := by + let R6 : VcpuRec6A st Z OS Bf s σ := + ⟨hSnap, hVcSym1, hVcSym2, hVcBack, hRunBack, hVcOS, hFrOff, hUserFr, hFrTh, hBfSpec⟩ + unfold vcpu.detach + refine WP.spec_bind (vcpu_cancel_run_stage3TA J hSV R6 hCl ht) ?_ + rintro ⟨s1, a1⟩ ⟨σ1, ha1, hsp_can, J1, hSV1, R6_1, hCl1, ht1, hl1, hrt1, -, hksb1, hcpu1, osk1, hw1, hHS1, fk1⟩ + dsimp only at ha1 J1 hSV1 R6_1 hCl1 ht1 hl1 hrt1 hksb1 hcpu1 osk1 hw1 hHS1 ⊢ + rcases ha1 with rfl + have hv_off : layout.tcb.VCPU = ok 3664#u64 := eq_of_spec_fd Fl4ProofsA64.P0.Consts.layout.tcb.VCPU.spec + have hbound_rd : vcpu.bound st s1 t = ok (L.mem s1 (wa t 3664#u64)) := by + unfold vcpu.bound; rw [hv_off, bind_ok, lift_okH, bind_ok, L.read_eq] + change (do let v ← vcpu.bound st s1 t; if v != 0#u64 then vcpu.unbind st ar s1 a1 v else ok (s1, a1)) ⦃ _ ⦄ + rw [hbound_rd, bind_ok] + -- `vcpu_cancel_run_stage3TA` did not touch `t + 3664`, `boundA t`, `scA t`, `stA t`! + -- Actually `hw1` is `WritesIn st s s1 (VcpuWrNoStA st Z OS s)`, so let's read `t + 3664` from `J.j.2.1.tcb` + `J1.j.2.1.tcb` + `hHS1`, + -- and `boundA`/`scA`/`stA` from `J.ep.tcb` + `J1.ep.tcb` + `hHS1`! + have nd_bind_mem_iffH : ∀ {α β S : Type} {f : NonDet S α} {g : α → NonDet S β} {s0 s' : S} {b : β}, + (b, s') ∈ ((f >>= g) s0).1 ↔ ∃ a sm, (a, sm) ∈ (f s0).1 ∧ (b, s') ∈ (g a sm).1 := by + intro α β S f g s0 s' b; simp [Bind.bind, NonDet.bind] + have hkh1_t : ∀ x, σ.kheap (specPtr t) = some (.tcb x) → + ∃ x1, σ1.kheap (specPtr t) = some (.tcb x1) ∧ + x1.tcbArch.tcbVcpu = x.tcbArch.tcbVcpu ∧ + x1.tcbExt.tcbRunVcpu = none ∧ + x1.tcbBoundNotification = x.tcbBoundNotification ∧ + x1.tcbSchedContext = x.tcbSchedContext ∧ + x1.tcbState = x.tcbState := fun x hx => by + obtain ⟨x1, hx1, -, hrv1⟩ := J1.j.2.1.tcb t ht + have hrv1_none : x1.tcbExt.tcbRunVcpu = none := by rw [hrv1]; unfold optRefA; simp [hrt1] + rcases hHS1.kh (specPtr t) with e | ⟨y0, y1, h0, h1', hkf⟩ | ⟨_, _, h0, -⟩ + · rw [e, hx] at hx1; cases hx1 + exact ⟨x, by rw [e]; exact hx, rfl, hrv1_none, rfl, rfl, rfl⟩ + · rw [hx] at h0; cases h0; rw [hx1] at h1'; cases h1' + have htv1 : x1.tcbArch.tcbVcpu = x.tcbArch.tcbVcpu := by + obtain ⟨x0, hx0, -, hrv0⟩ := J.j.2.1.tcb t ht + rw [hx] at hx0; cases hx0 + unfold fl4CancelRun at hsp_can + rw [nd_bind_oneSc (getRunVcpu_eqA hx)] at hsp_can + by_cases hr0 : L.mem s (runVcpuA t) = 0#u64 + · have hrv_none : x.tcbExt.tcbRunVcpu = none := by rw [hrv0]; unfold optRefA; simp [hr0] + rw [hrv_none] at hsp_can; simp [Pure.pure, NonDet.pure] at hsp_can + subst hsp_can; rw [hx] at hx1; cases hx1; rfl + · have hvV : L.mem s (runVcpuA t) ∈ Z.V := (J.vcRun.run t ht).resolve_left hr0 + have hrv_some : x.tcbExt.tcbRunVcpu = some (specPtr (L.mem s (runVcpuA t))) := by + rw [hrv0]; unfold optRefA; simp [hr0] + obtain ⟨v, hkv, -, -⟩ := J.j.2.1.obj _ hvV + have hne_tv : specPtr t ≠ specPtr (L.mem s (runVcpuA t)) := fun eq => by rw [eq, hkv] at hx; cases hx + rw [hrv_some] at hsp_can; dsimp only at hsp_can + rw [nd_bind_oneSc (fl4VcpuExtUpdate_eqA _ hkv)] at hsp_can + rw [setRunVcpu_eqA none (by rw [updKSc_other _ hne_tv]; exact hx)] at hsp_can + simp at hsp_can; subst hsp_can + simp [updKSc, Function.update] at hx1; rw [← hx1] + exact ⟨x1, hx1, htv1, hrv1_none, hkf.bnd, hkf.sc, hkf.state⟩ + · rw [hx] at h0; cases h0 + have h3664_1 : L.mem s1 (wa t 3664#u64) = L.mem s (wa t 3664#u64) := by + obtain ⟨x0, hx0, htv0, -⟩ := J.j.2.1.tcb t ht + obtain ⟨x1, hx1, htv1, -⟩ := J1.j.2.1.tcb t ht + obtain ⟨x1', hx1', htv_eq, -, -, -, -⟩ := hkh1_t x0 hx0 + rw [hx1'] at hx1; cases hx1 + rw [htv0, htv1] at htv_eq + unfold optRefA at htv_eq + by_cases h10 : L.mem s1 (wa t 3664#u64) = 0#u64 + · by_cases h00 : L.mem s (wa t 3664#u64) = 0#u64 + · rw [h10, h00] + · simp [h10, h00] at htv_eq + · by_cases h00 : L.mem s (wa t 3664#u64) = 0#u64 + · simp [h10, h00] at htv_eq + · simp [h10, h00] at htv_eq; exact specPtr_inj htv_eq + have hBnd1 : L.mem s1 (boundA t) = L.mem s (boundA t) := by + unfold boundA + exact hw1.mem_eq (field_aligned (J.sr.core.tcbs t ht) (by simp) (by simp)) + (tcbSched_off_vcpuWrA J ht (by simp) (by simp)) + have hSc1 : L.mem s1 (scA t) = L.mem s (scA t) := by + unfold scA + exact hw1.mem_eq (field_aligned (J.sr.core.tcbs t ht) (by simp) (by simp)) + (tcbSched_off_vcpuWrA J ht (by simp) (by simp)) + have hSt1 : L.mem s1 (stA t) = L.mem s (stA t) := by + rw [stA_eq] + exact hw1.mem_eq (field_aligned (J.sr.core.tcbs t ht) (by decide) (by decide)) + (tcbSched_off_vcpuWrA J ht (by decide) (by decide)) + set v := L.mem s1 (wa t 3664#u64) + by_cases hv0 : v = 0#u64 + · have hbn : (v != 0#u64) = false := bne_eq_false_iff_eq.mpr hv0 + simp only [hbn, Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + obtain ⟨xt1, hkt1, htv1_spec, -⟩ := J1.j.2.1.tcb t ht + have htv_none : xt1.tcbArch.tcbVcpu = none := by rw [htv1_spec]; change optRefA v = none; unfold optRefA; simp [hv0] + have hsp : ((), σ1) ∈ (fl4VcpuDetach (specPtr t) σ).1 := by + unfold fl4VcpuDetach + rw [nd_bind_mem_iffH] + refine ⟨(), σ1, hsp_can, ?_⟩ + rw [nd_bind_oneSc (archThreadGet_eqA _ hkt1), htv_none]; rfl + refine ⟨Z.Q, Z.R, Z.hw, σ1, hsp, J1, hSV1, R6_1.snap, R6_1.vcSym1, R6_1.vcSym2, R6_1.vcBack, + R6_1.runBack, R6_1.userFr, hCl1, ht1, hl1, hv0, hrt1, fun h => absurd (h3664_1.symm.trans hv0) h, + hksb1, hcpu1, osk1, fun y hy => by unfold capA; rw [(osk1 y hy).1, (osk1 y hy).2.1], + hBnd1, hSc1, hSt1, fun x hx => ?_, fk1⟩ + obtain ⟨x1, hx1, -, hrv1, hb1, hsc1, hst1⟩ := hkh1_t x hx + rw [hkt1] at hx1; cases hx1 + exact ⟨xt1, hkt1, htv_none, hrv1, hb1, hsc1, hst1⟩ + · have hbn : (v != 0#u64) = true := bne_iff_ne.mpr hv0 + simp only [hbn, ↓reduceIte] + have hvV : v ∈ Z.V := (J1.vcRun.tcb t ht).resolve_left hv0 + have hv_t : L.mem s1 v = t := R6_1.vcSym1 t ht hv0 + have hrc_ne_t : t ≠ L.mem s1 (wa v 24#u64) := fun eq => by + have hne_rc : L.mem s1 (wa v 24#u64) ≠ 0#u64 := by + rw [← eq]; exact fun e => (J.sr.core.tcbs t ht).ne0 (by rw [e]; rfl) + have hsym := R6_1.vcSym2 v hvV hne_rc + rw [← eq, hrt1] at hsym + exact hv0 hsym.symm + obtain ⟨xt1, hkt1, htv1_spec, -⟩ := J1.j.2.1.tcb t ht + have htv_some : xt1.tcbArch.tcbVcpu = some (specPtr v) := by + rw [htv1_spec]; change optRefA v = some (specPtr v); unfold optRefA; simp [hv0] + refine WP.spec_mono (vcpu_unbind_stage3TA J1 hSV1 R6_1 hCl1 hClVm hClFlush hClFlushState hClFr hP hAK hVR hCs h1 hvV hv0) ?_ + rintro res ⟨hw', σ', hsp_ub, J2, hSV2, R6_2, hCl2, ht2, hl2, hv0_2, hv24_2, ht3664_2, hksb2, hcpu2, osk2, hbs2, hst2, hkh2, fk2⟩ + have hsp : ((), σ') ∈ (fl4VcpuDetach (specPtr t) σ).1 := by + unfold fl4VcpuDetach + rw [nd_bind_mem_iffH] + refine ⟨(), σ1, hsp_can, ?_⟩ + rw [nd_bind_oneSc (archThreadGet_eqA _ hkt1), htv_some]; dsimp only; rw [hsp_ub]; simp + have hOskF : OSKeep st OS s res.1 := fun y hy => + ⟨(osk2 y hy).1.trans (osk1 y hy).1, (osk2 y hy).2.1.trans (osk1 y hy).2.1, + (osk2 y hy).2.2.1.trans (osk1 y hy).2.2.1, (osk2 y hy).2.2.2.trans (osk1 y hy).2.2.2⟩ + have htv0_2 : L.mem res.1 (wa t 3664#u64) = 0#u64 := by + have hne_vt : L.mem s1 v ≠ 0#u64 := by + rw [hv_t]; exact fun e => (J.sr.core.tcbs t ht).ne0 (by rw [e]; rfl) + have h1' := ht3664_2 hne_vt; rwa [hv_t] at h1' + refine ⟨Z.Q, Z.R, hw', σ', hsp, J2, hSV2, R6_2.snap, R6_2.vcSym1, R6_2.vcSym2, R6_2.vcBack, + R6_2.runBack, R6_2.userFr, hCl2, ht2.trans ht1, hl2.trans hl1, htv0_2, + (hst2 t ht hrc_ne_t).2.trans hrt1, fun _ => ⟨by rw [← h3664_1]; exact hv0_2, by rw [← h3664_1]; exact hv24_2⟩, + hksb2.trans hksb1, hcpu2.trans hcpu1, hOskF, + fun y hy => by unfold capA; rw [(hOskF y hy).1, (hOskF y hy).2.1], + (hbs2 t ht).1.trans hBnd1, (hbs2 t ht).2.trans hSc1, (hst2 t ht hrc_ne_t).1.trans hSt1, fun x hx => ?_, fk1.trans fk2⟩ + obtain ⟨x1, hx1, -, hrv1, hb1, hsc1, hst1⟩ := hkh1_t x hx + obtain ⟨x', hx', hb2, hsc2, hne2, hv2⟩ := hkh2 t ht x1 hx1 + exact ⟨x', hx', hv2 hv_t.symm, (hne2 hrc_ne_t).2.trans hrv1, hb2.trans hb1, hsc2.trans hsc1, (hne2 hrc_ne_t).1.trans hst1⟩ + +end hooks +end +end Fl4ProofsA64.P3.Vcpu + diff --git a/proofs/Fl4ProofsA64/P3/VcpuDelReadA.lean b/proofs/Fl4ProofsA64/P3/VcpuDelReadA.lean new file mode 100644 index 000000000..e7d5ef6bc --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/VcpuDelReadA.lean @@ -0,0 +1,1481 @@ +/- +P3 (aarch64, A64-P3 S4): **`vcpu.read_state_to_frame` refinement on `JStage3TA`**. + +Lifts `vcpu.read_state_to_frame` ↔ `fl4ReadStateToFrame` across all 6 mask bits +(`EL1_SYSREGS`, `HYP`, `TIMER`, `VGIC`, `VGIC_LR`, `GPRS`) onto `JStage3TA` + +`SchedValidA` + `VcpuRec6A` + `Clock`, using `JStage3TA.of_vcpuStepA` once at the end. + +No new named premise. +-/ +import Fl4ProofsA64.P3.VcpuDelFrameA +import Fl4ProofsA64.P3.VcpuFrameMemA +import Fl4ProofsA64.P3.FrAgDefA + +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3.Vcpu + +open Fl4Spec Fl4SpecA64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST +open Fl4ProofsA64.P3 Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section read_stage3 +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +private theorem lift_ok {α : Type} (x : α) : lift x = ok x := rfl + +private theorem u64_mul_okR {x y : U64} (h : x.val * y.val < 2 ^ 64) : + x * y = ok ⟨BitVec.ofNat 64 (x.val * y.val)⟩ := by + have h1 : UScalar.check_bounds UScalarTy.U64 (x.val * y.val) = true := by + simp only [UScalar.check_bounds, UScalar.cMax, UScalarTy.numBits, decide_eq_true_eq] + omega + change UScalar.mul x y = _ + unfold UScalar.mul UScalar.tryMk UScalar.tryMkOpt ofOption + rw [dif_pos h1] + dsimp only; congr 1; apply UScalar.eq_of_val_eq + show (UScalar.ofNatCore (x.val * y.val) _).val = (BitVec.ofNat 64 (x.val * y.val)).toNat + rw [UScalar.ofNatCore_val_eq, BitVec.toNat_ofNat, Nat.mod_eq_of_lt h] + +private theorem inUserFrame_of_kheapR {p : ObjRef} {σ σ' : State Unit} + (hkh : σ'.kheap = σ.kheap) (h : inUserFrame p σ) : inUserFrame p σ' := by + simpa [inUserFrame, hkh] using h + +private theorem u64_ofNat_valR (r : U64) : (⟨BitVec.ofNat 64 r.val⟩ : U64) = r := + UScalar.eq_of_val_eq (by + change (BitVec.ofNat 64 r.val).toNat = r.val + rw [BitVec.toNat_ofNat] + have := r.hBounds; simp only [UScalar.size, UScalarTy.numBits] at this + exact Nat.mod_eq_of_lt this) + +private theorem vcpu_reg_get_u64_eqR (s : S) (vc r : U64) (hr : r.val < 64) : + vcpu.reg_get st s vc r = ok (L.mem s (vcpuRegWA vc r.val)) := by + have h := vcpu_reg_get_eqA (L := L) s vc hr + rwa [u64_ofNat_valR] at h + +private theorem storeWordOffs_eqR (frame : ObjRef) (k : Nat) (w : Word) (σ : State Unit) + (hal : bufAddr frame k &&& mask 3 = 0) (huf : inUserFrame (bufAddr frame k) σ) : + storeWordOffs (Z := Unit) frame k w σ = + ({((), setUm σ (storeMem σ.machineState.underlyingMemory (bufAddr frame k) w))}, False) := by + unfold storeWordOffs + rw [nd_bind_oneSc (m := NonDet.get) rfl] + have ha1 : NonDet.assert (inUserFrame (bufAddr frame k) σ) σ = ({((), σ)}, False) := by + unfold NonDet.assert; exact Prod.ext (by ext x; simp [huf]) (by simp [huf]) + rw [show frame + BitVec.ofNat 64 (k * wordSize) = bufAddr frame k from rfl, nd_bind_oneSc ha1] + have hsw : storeWord (bufAddr frame k) w σ.machineState = + ({((), { σ.machineState with underlyingMemory := storeMem σ.machineState.underlyingMemory (bufAddr frame k) w })}, False) := by + unfold storeWord + have ha2 : NonDet.assert (bufAddr frame k &&& mask 3 = 0) σ.machineState = ({((), σ.machineState)}, False) := by + unfold NonDet.assert; exact Prod.ext (by ext x; simp [hal]) (by simp [hal]) + rw [nd_bind_oneSc ha2]; rfl + unfold doMachineOp + rw [nd_bind_oneSc (m := NonDet.gets _) rfl] + have hsf : NonDet.selectF (storeWord (bufAddr frame k) w σ.machineState) σ = + ({(((), { σ.machineState with underlyingMemory := storeMem σ.machineState.underlyingMemory (bufAddr frame k) w }), σ)}, False) := by + rw [hsw]; simp only [NonDet.selectF, Set.mem_singleton_iff, Prod.mk.injEq] + exact ⟨Set.ext fun x => by simp [Prod.ext_iff], trivial⟩ + rw [nd_bind_oneSc hsf]; dsimp only + rw [nd_bind_oneSc (m := NonDet.modify _) rfl]; rfl + +private theorem mapMx_appendR {α : Type} (f : α → SMonad Unit Unit) (xs ys : List α) (σ : State Unit) : + NonDet.mapMx f (xs ++ ys) σ = (NonDet.mapMx f xs >>= fun _ => NonDet.mapMx f ys) σ := by + induction xs generalizing σ with + | nil => + change NonDet.mapMx f ys σ = (NonDet.pure () >>= fun _ => NonDet.mapMx f ys) σ + rw [nd_bind_oneSc (m := NonDet.pure ()) rfl] + | cons a tl ih => + change (f a >>= fun _ => NonDet.mapMx f (tl ++ ys)) σ = + ((f a >>= fun _ => NonDet.mapMx f tl) >>= fun _ => NonDet.mapMx f ys) σ + rw [nd_bind_assocSc] + have hfn : (fun _ : Unit => NonDet.mapMx f (tl ++ ys)) = + (fun _ : Unit => NonDet.mapMx f tl >>= fun _ => NonDet.mapMx f ys) := by + funext _; funext σ1; exact ih σ1 + rw [hfn] + +private theorem sm_el1_okR : fl4_abi.consts.vcpu_state_mask.EL1_SYSREGS = ok 1#u64 := + eq_of_spec_fd Fl4ProofsA64.P0.Consts.fl4_abi.consts.vcpu_state_mask.EL1_SYSREGS.spec +private theorem sm_hyp_okR : fl4_abi.consts.vcpu_state_mask.HYP = ok 2#u64 := + eq_of_spec_fd Fl4ProofsA64.P0.Consts.fl4_abi.consts.vcpu_state_mask.HYP.spec +private theorem sm_timer_okR : fl4_abi.consts.vcpu_state_mask.TIMER = ok 4#u64 := + eq_of_spec_fd Fl4ProofsA64.P0.Consts.fl4_abi.consts.vcpu_state_mask.TIMER.spec +private theorem sm_vgic_okR : fl4_abi.consts.vcpu_state_mask.VGIC = ok 8#u64 := + eq_of_spec_fd Fl4ProofsA64.P0.Consts.fl4_abi.consts.vcpu_state_mask.VGIC.spec +private theorem sm_gprs_okR : fl4_abi.consts.vcpu_state_mask.GPRS = ok 16#u64 := + eq_of_spec_fd Fl4ProofsA64.P0.Consts.fl4_abi.consts.vcpu_state_mask.GPRS.spec +private theorem sm_vgic_lr_okR : fl4_abi.consts.vcpu_state_mask.VGIC_LR = ok 32#u64 := + eq_of_spec_fd Fl4ProofsA64.P0.Consts.fl4_abi.consts.vcpu_state_mask.VGIC_LR.spec + +private theorem fl4UserRegs_getElemR (i : Nat) (hi : i < 37) : + fl4UserRegs[i]'(by simp [fl4UserRegs]; omega) = idxRegA i := by + have : i < 37 := hi; interval_cases i <;> rfl + +private theorem regIdxA_idxRegR (i : Nat) (hi : i < 37) : + regIdxA (idxRegA i) = ⟨BitVec.ofNat 64 i⟩ := by + interval_cases i <;> rfl + +/-- The 128-word write footprint of `vcpu.read_state_to_frame` at `frame`. -/ +def FrameWinW (frame : U64) (b : U64) : Prop := + ∃ k < 128, b = wa frame ⟨BitVec.ofNat 64 (8 * k)⟩ + +/-- Compact loop invariant maintained across every single word store inside +`vcpu.read_state_to_frame`, discharged into `JStage3TA.of_vcpuStepA` once at the end. -/ +private structure ReadInvA (s0 : S) (σ0 : Fl4SpecA64.State Unit) (frame : U64) + (Bf : Set (U64 × Fl4SpecA64.Word)) (s1 : S) (σ1 : Fl4SpecA64.State Unit) : Prop where + wr : WritesIn st s0 s1 (FrameWinW frame) + eqUm : σ1 = setUm σ0 σ1.machineState.underlyingMemory + umOff : ∀ p : ObjRef, + (∀ k < 128, ¬ (frame.val + 8 * k ≤ (fl4Ptr p).val ∧ (fl4Ptr p).val < frame.val + 8 * k + 8)) → + σ1.machineState.underlyingMemory p = σ0.machineState.underlyingMemory p + buf : ∀ bp ∈ Bf, ∀ j : U64, j.val < 128 → + (L.mem s1 (bufW bp.1 j)).bv = loadWordAt σ1.machineState.underlyingMemory (bufAddr bp.2 j.val) ∧ + (bufAddr bp.2 j.val).toNat % 8 = 0 + ag : ∀ c : U64, Aligned c → (L.mem s0 c).bv = loadWordAt σ0.machineState.underlyingMemory (specPtr c) → + (L.mem s1 c).bv = loadWordAt σ1.machineState.underlyingMemory (specPtr c) + +private theorem ReadInvA.refl {s0 : S} {σ0 : Fl4SpecA64.State Unit} {frame : U64} + {Bf : Set (U64 × Fl4SpecA64.Word)} + (hBuf : ∀ bp ∈ Bf, ∀ j : U64, j.val < 128 → + (L.mem s0 (bufW bp.1 j)).bv = loadWordAt σ0.machineState.underlyingMemory (bufAddr bp.2 j.val) ∧ + (bufAddr bp.2 j.val).toNat % 8 = 0) : + ReadInvA (L := L) s0 σ0 frame Bf s0 σ0 := + ⟨⟨rfl, rfl, fun _ _ _ => rfl⟩, rfl, fun _ _ => rfl, hBuf, fun _ _ h => h⟩ + +private theorem ReadInvA.kheap {s0 s1 : S} {σ0 σ1 : Fl4SpecA64.State Unit} {frame : U64} + {Bf : Set (U64 × Fl4SpecA64.Word)} (I : ReadInvA (L := L) s0 σ0 frame Bf s1 σ1) : + σ1.kheap = σ0.kheap := by + rw [I.eqUm]; rfl + +private theorem frame_word_valA {frame : U64} (hfit : frame.val + 1024 ≤ 2 ^ 64) {k : Nat} (hk : k < 128) : + (wa frame ⟨BitVec.ofNat 64 (8 * k)⟩).val = frame.val + 8 * k := by + have hk8 : (⟨BitVec.ofNat 64 (8 * k)⟩ : U64).val = 8 * k := by + show (BitVec.ofNat 64 (8 * k)).toNat = 8 * k + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + rw [wa_valm, hk8]; exact Nat.mod_eq_of_lt (by omega) + +private theorem frame_word_alA {frame : U64} (hfit : frame.val + 1024 ≤ 2 ^ 64) + (hal : frame.val % 8 = 0) {k : Nat} (hk : k < 128) : + Aligned (wa frame ⟨BitVec.ofNat 64 (8 * k)⟩) := by + show (wa frame ⟨BitVec.ofNat 64 (8 * k)⟩).val % 8 = 0 + rw [frame_word_valA hfit hk]; omega + +private theorem frame_bufAddr_al3A {frame : U64} (hfit : frame.val + 1024 ≤ 2 ^ 64) + (hal : frame.val % 8 = 0) {k : Nat} (hk : k < 128) : + bufAddr (specPtr frame) k &&& Fl4SpecA64.mask 3 = 0 := by + have hsp : specPtr (wa frame ⟨BitVec.ofNat 64 (8 * k)⟩) = bufAddr (specPtr frame) k := + specPtr_wa_wordA (by omega) + rw [← hsp] + have hba : (wa frame ⟨BitVec.ofNat 64 (8 * k)⟩).val % 8 = 0 := frame_word_alA hfit hal hk + have hsp8 : (specPtr (wa frame ⟨BitVec.ofNat 64 (8 * k)⟩)).toNat % 8 = 0 := by + unfold specPtr; rw [BitVec.toNat_add] + have hsh8 : pptrShiftA.toNat % 8 = 0 := by decide + have hb8 : (wa frame ⟨BitVec.ofNat 64 (8 * k)⟩).bv.toNat % 8 = 0 := hba + omega + apply BitVec.eq_of_toNat_eq + rw [BitVec.toNat_and] + have h7 : (Fl4SpecA64.mask 3 : Word).toNat = 7 := by decide + rw [h7] + have hm := Nat.and_two_pow_sub_one_eq_mod (specPtr (wa frame ⟨BitVec.ofNat 64 (8 * k)⟩)).toNat 3 + simpa using hm.trans hsp8 + +private theorem storeMem_byte_neA (um : Word → Word8) (b : U64) (p : ObjRef) (w : Word) + (hfit : b.val + 8 ≤ 2 ^ 64) + (hout : ¬ (b.val ≤ (fl4Ptr p).val ∧ (fl4Ptr p).val < b.val + 8)) : + storeMem um (specPtr b) w p = um p := by + have hne : ∀ i : Nat, i < 8 → p ≠ specPtr b + BitVec.ofInt 64 (Int.ofNat i) := fun i hi e => by + have hof : BitVec.ofInt 64 (Int.ofNat i) = BitVec.ofNat 64 i := by simp [BitVec.ofInt_natCast] + rw [hof] at e + have hfp : fl4Ptr p = ⟨b.bv + BitVec.ofNat 64 i⟩ := by + rw [e]; unfold fl4Ptr specPtr; congr 1 + rw [BitVec.add_assoc, BitVec.add_comm pptrShiftA, ← BitVec.add_assoc, BitVec.add_sub_cancel] + have hv : (fl4Ptr p).val = b.val + i := by + rw [hfp]; change (b.bv + BitVec.ofNat 64 i).toNat = b.val + i + rw [BitVec.toNat_add, BitVec.toNat_ofNat] + have hbv : b.bv.toNat = b.val := rfl + have hlt : i < 2 ^ 64 := by omega + rw [hbv, Nat.mod_eq_of_lt hlt]; exact Nat.mod_eq_of_lt (by omega) + exact hout ⟨by omega, by omega⟩ + have hne' : ∀ k : Int, 0 ≤ k → k < 8 → p ≠ specPtr b + BitVec.ofInt 64 k := fun k hk0 hk8 => by + have hk : k = Int.ofNat k.toNat := (Int.toNat_of_nonneg hk0).symm + rw [hk]; exact hne k.toNat (by omega) + unfold storeMem + rw [intUpto_07] + simp only [List.foldl_cons, List.foldl_nil] + rw [Function.update_of_ne (hne' 7 (by omega) (by omega)), + Function.update_of_ne (hne' 6 (by omega) (by omega)), + Function.update_of_ne (hne' 5 (by omega) (by omega)), + Function.update_of_ne (hne' 4 (by omega) (by omega)), + Function.update_of_ne (hne' 3 (by omega) (by omega)), + Function.update_of_ne (hne' 2 (by omega) (by omega)), + Function.update_of_ne (hne' 1 (by omega) (by omega)), + Function.update_of_ne (hne' 0 (by omega) (by omega))] + +private theorem ReadInvA.step {s0 s1 : S} {σ0 σ1 : Fl4SpecA64.State Unit} {frame : U64} + {Bf : Set (U64 × Fl4SpecA64.Word)} + (I : ReadInvA (L := L) s0 σ0 frame Bf s1 σ1) + (hfit : frame.val + 1024 ≤ 2 ^ 64) (hal : frame.val % 8 = 0) + (hBfAl : ∀ bp ∈ Bf, ∀ j : U64, j.val < 128 → Aligned (bufW bp.1 j)) + (hBfSpec : ∀ bp ∈ Bf, bp.2 = specPtr bp.1) + (huf : ∀ k < 128, inUserFrame (bufAddr (specPtr frame) k) σ0) + {k : Nat} (hk : k < 128) (w : U64) : + let b := wa frame ⟨BitVec.ofNat 64 (8 * k)⟩ + let σ2 := setUm σ1 (storeMem σ1.machineState.underlyingMemory (bufAddr (specPtr frame) k) w.bv) + storeWordOffs (Z := Unit) (specPtr frame) k w.bv σ1 = ({((), σ2)}, False) ∧ + ReadInvA (L := L) s0 σ0 frame Bf (L.wr s1 b w) σ2 := by + dsimp only + set b : U64 := wa frame ⟨BitVec.ofNat 64 (8 * k)⟩ + set σ2 := setUm σ1 (storeMem σ1.machineState.underlyingMemory (bufAddr (specPtr frame) k) w.bv) + have hba : Aligned b := frame_word_alA hfit hal hk + have hsp : specPtr b = bufAddr (specPtr frame) k := specPtr_wa_wordA (by omega) + have hal3 : bufAddr (specPtr frame) k &&& mask 3 = 0 := frame_bufAddr_al3A hfit hal hk + have huf1 : inUserFrame (bufAddr (specPtr frame) k) σ1 := inUserFrame_of_kheapR I.kheap (huf k hk) + refine ⟨storeWordOffs_eqR (specPtr frame) k w.bv σ1 hal3 huf1, ?_⟩ + have ag : ∀ c : U64, Aligned c → (L.mem s0 c).bv = loadWordAt σ0.machineState.underlyingMemory (specPtr c) → + (L.mem (L.wr s1 b w) c).bv = + loadWordAt (storeMem σ1.machineState.underlyingMemory (bufAddr (specPtr frame) k) w.bv) (specPtr c) := by + intro c cal h0 + by_cases heq : c = b + · rw [heq, L.mem_wr_same hba, hsp, load_store_same] + · rw [L.mem_wr_ne hba cal (fun e => heq (UScalar.eq_of_val_eq e).symm)] + have al8 : ∀ x : U64, Aligned x → (specPtr x).toNat % 8 = 0 := fun x hx => by + unfold specPtr; rw [BitVec.toNat_add] + have hsh8 : pptrShiftA.toNat % 8 = 0 := by decide + have hb8 : x.bv.toNat % 8 = 0 := hx + omega + rw [load_store_other _ _ (hsp ▸ al8 b hba) (al8 c cal) (fun e => heq (specPtr_inj (e.trans hsp.symm)))] + exact I.ag c cal h0 + refine ⟨(I.wr.trans (writesIn_wr hba)).mono fun _ h => h.elim id fun e => e ▸ ⟨k, hk, rfl⟩, ?_, ?_, ?_, ag⟩ + · dsimp only [σ2]; rw [I.eqUm]; rfl + · intro p hp + change storeMem σ1.machineState.underlyingMemory (bufAddr (specPtr frame) k) w.bv p = _ + rw [← hsp, storeMem_byte_neA _ b p _ (by rw [frame_word_valA hfit hk]; omega) + (by rw [frame_word_valA hfit hk]; exact hp k hk)] + exact I.umOff p hp + · intro bp hbp j hj + have hbwAl : Aligned (bufW bp.1 j) := hBfAl bp hbp j hj + have hbwSp : specPtr (bufW bp.1 j) = bufAddr bp.2 j.val := by + rw [hBfSpec bp hbp]; unfold bufW specPtr bufAddr wordSize + have hmul : (core.num.U64.wrapping_mul j 8#u64).bv = BitVec.ofNat 64 (j.val * 8) := by + apply BitVec.eq_of_toNat_eq + have h8 : (8#u64 : U64).val = 8 := rfl + have hs : UScalar.size UScalarTy.U64 = 2 ^ 64 := by simp [U64.size_def, U64.numBits] + change (core.num.U64.wrapping_mul j 8#u64).val = _ + rw [core.num.U64.wrapping_mul_val_eq, h8, hs, BitVec.toNat_ofNat] + have hwa : (wa bp.1 (core.num.U64.wrapping_mul j 8#u64)).bv = bp.1.bv + (core.num.U64.wrapping_mul j 8#u64).bv := rfl + rw [hwa, hmul, BitVec.add_assoc, BitVec.add_comm (BitVec.ofNat 64 (j.val * 8)) pptrShiftA, ← BitVec.add_assoc] + refine ⟨?_, (I.buf bp hbp j hj).2⟩ + change (L.mem (L.wr s1 b w) (bufW bp.1 j)).bv = + loadWordAt (storeMem σ1.machineState.underlyingMemory (bufAddr (specPtr frame) k) w.bv) (bufAddr bp.2 j.val) + by_cases heq : bufW bp.1 j = b + · rw [heq, L.mem_wr_same hba, ← hbwSp, heq, hsp, load_store_same] + · rw [L.mem_wr_ne hba hbwAl (fun e => heq (UScalar.eq_of_val_eq e).symm)] + have hne_sp : bufAddr bp.2 j.val ≠ bufAddr (specPtr frame) k := fun e => + heq (specPtr_inj (hbwSp.trans (e.trans hsp.symm))) + have ha1 : (bufAddr (specPtr frame) k).toNat % 8 = 0 := by + rw [← hsp]; unfold specPtr; rw [BitVec.toNat_add] + have hsh8 : pptrShiftA.toNat % 8 = 0 := by decide + have hb8 : b.bv.toNat % 8 = 0 := hba + omega + have ha2 : (bufAddr bp.2 j.val).toNat % 8 = 0 := (I.buf bp hbp j hj).2 + rw [load_store_other _ _ ha1 ha2 hne_sp] + exact (I.buf bp hbp j hj).1 + +/-- Helper: `copyRegsToFrame` cons reduction (without `VcpuXferStateA`). -/ +private theorem copyRegsToFrame_consR (vc frame : ObjRef) (r : Nat) (rs : List Nat) + {σ : State Unit} {v : Vcpu} (hk : σ.kheap vc = some (.archObj (.vcpu v))) + (hal : bufAddr frame r &&& mask 3 = 0) (huf : inUserFrame (bufAddr frame r) σ) : + copyRegsToFrame vc frame (r :: rs) σ = + copyRegsToFrame vc frame rs + (setUm σ (storeMem σ.machineState.underlyingMemory (bufAddr frame r) (fl4VcpuRegGet v r))) := by + unfold copyRegsToFrame + change ((do let w ← fl4VcpuGet vc r; storeWordOffs frame r w) >>= fun _ => NonDet.mapMx _ rs) σ = _ + rw [nd_bind_oneSc (by + rw [nd_bind_oneSc (fl4VcpuGet_eqA r hk), storeWordOffs_eqR frame r (fl4VcpuRegGet v r) σ hal huf])] + +/-- Helper: `asUser t (getRegister r)` on a TCB `x`. -/ +private theorem asUser_getReg_eqR {σ : State Unit} {t : ObjRef} {x : Tcb} + (hk : σ.kheap t = some (.tcb x)) (r : Register) : + asUser t (getRegister r) σ = ({(x.tcbArch.tcbContext.userRegs r, σ)}, False) := by + have hg : NonDet.getsThe (getTcb (Z := Unit) t) σ = ({(x, σ)}, False) := by + simp [NonDet.getsThe, getTcb, readObject, hk, NonDet.assertOpt]; rfl + have ho : getObject (Z := Unit) t σ = ({(.tcb x, σ)}, False) := by + simp [getObject, NonDet.getsThe, readObject, hk, NonDet.assertOpt]; rfl + have hsel : NonDet.selectF (getRegister r (archTcbContextGet x.tcbArch)) σ = + ({((x.tcbArch.tcbContext.userRegs r, x.tcbArch.tcbContext), σ)}, False) := by + have h1 : getRegister r (archTcbContextGet x.tcbArch) = + ({(x.tcbArch.tcbContext.userRegs r, x.tcbArch.tcbContext)}, False) := rfl + rw [h1] + simp only [NonDet.selectF, Set.mem_singleton_iff, Prod.mk.injEq] + refine ⟨Set.ext fun y => ?_, trivial⟩ + simp [Prod.ext_iff] + have hso : setObject (Z := Unit) t (.tcb { x with tcbArch := archTcbContextSet x.tcbArch.tcbContext x.tcbArch }) σ = + ({((), σ)}, False) := by + unfold setObject + rw [nd_bind_singleA ho, nd_bind_singleA (show (NonDet.assert (aType (.tcb x) = aType (.tcb _)) : + NonDet (State Unit) Unit) σ = ({((), σ)}, False) by simp [NonDet.assert, aType]), + nd_bind_singleA (show NonDet.get σ = ({(σ, σ)}, False) from rfl)] + have hupd : { σ with kheap := Function.update σ.kheap t (some (.tcb x)) } = σ := by + have hfe : Function.update σ.kheap t (some (.tcb x)) = σ.kheap := Function.update_eq_self_iff.2 hk.symm + rw [hfe] + exact congrArg (fun s => ({((), s)}, False)) hupd + unfold asUser + rw [nd_bind_oneA hg, nd_bind_oneA (show (pure (archTcbContextGet x.tcbArch) : SMonad Unit UserContext) σ = _ from rfl), + nd_bind_oneA hsel] + dsimp only + rw [nd_bind_oneA (show (pure _ : SMonad Unit Tcb) σ = _ from rfl), nd_bind_oneA hso] + rfl + +/-- Helper: `vcpu.copy_reg_range_to_frame` refines `copyRegsToFrame` under `ReadInvA`. -/ +private theorem copy_reg_range_invA {s0 s1 : S} {σ0 σ1 : Fl4SpecA64.State Unit} + {vc frame start end_incl : U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {xv : Fl4SpecA64.Vcpu} + (I1 : ReadInvA (L := L) s0 σ0 frame Bf s1 σ1) + (hfit : frame.val + 1024 ≤ 2 ^ 64) (hal : frame.val % 8 = 0) + (hBfAl : ∀ bp ∈ Bf, ∀ j : U64, j.val < 128 → Aligned (bufW bp.1 j)) + (hBfSpec : ∀ bp ∈ Bf, bp.2 = specPtr bp.1) + (huf : ∀ k < 128, inUserFrame (bufAddr (specPtr frame) k) σ0) + (hkv : σ0.kheap (specPtr vc) = some (.archObj (.vcpu xv))) + (hSnap : ∀ i < 64, (L.mem s0 (vcpuRegWA vc i)).bv = fl4VcpuRegGet xv i) + (hVcOff : ∀ i < 64, Aligned (vcpuRegWA vc i) ∧ ¬ FrameWinW frame (vcpuRegWA vc i)) + (hle : start.val ≤ end_incl.val) (hend : end_incl.val < 64) : + vcpu.copy_reg_range_to_frame st s1 vc frame start end_incl ⦃ s2 => + ∃ σ2, copyRegsToFrame (specPtr vc) (specPtr frame) (idxRange start.val end_incl.val) σ1 = ({((), σ2)}, False) ∧ + ReadInvA (L := L) s0 σ0 frame Bf s2 σ2 ⦄ := by + unfold vcpu.copy_reg_range_to_frame vcpu.copy_reg_range_to_frame_loop + let Post : S → Prop := fun s2 => + ∃ σ2, copyRegsToFrame (specPtr vc) (specPtr frame) (idxRange start.val end_incl.val) σ1 = ({((), σ2)}, False) ∧ + ReadInvA (L := L) s0 σ0 frame Bf s2 σ2 + let Inv : S × U64 → Prop := fun p => + start.val ≤ p.2.val ∧ p.2.val ≤ end_incl.val + 1 ∧ + ∃ σk, copyRegsToFrame (specPtr vc) (specPtr frame) (List.range' start.val (p.2.val - start.val)) σ1 = ({((), σk)}, False) ∧ + ReadInvA (L := L) s0 σ0 frame Bf p.1 σk + have hInv0 : Inv (s1, start) := + ⟨le_rfl, by dsimp only; omega, σ1, by dsimp only; simp [copyRegsToFrame, NonDet.mapMx]; rfl, I1⟩ + apply loop.spec_decr_nat (measure := fun p => end_incl.val + 1 - p.2.val) (inv := Inv) (post := Post) + swap + · exact hInv0 + rintro ⟨sk, r⟩ ⟨hlo, hhi, σk, hspec_k, Ik⟩ + dsimp only at hlo hhi ⊢ + unfold vcpu.copy_reg_range_to_frame_loop.body + by_cases hle_r : r ≤ end_incl + · have hr_le : r.val ≤ end_incl.val := hle_r + have hr64 : r.val < 64 := by omega + rw [if_pos hle_r, u64_mul_okR (by change r.val * 8 < 2 ^ 64; omega), bind_ok, lift_ok, bind_ok, + vcpu_reg_get_u64_eqR sk vc r hr64, bind_ok, L.write_eq, bind_ok, + u64_add_ok (by change r.val + 1 < 2 ^ 64; omega), bind_ok, WP.spec_ok] + have hmul : (⟨BitVec.ofNat 64 (r.val * 8#u64.val)⟩ : U64) = ⟨BitVec.ofNat 64 (8 * r.val)⟩ := by + congr 1; congr 1; change r.val * 8 = 8 * r.val; omega + rw [hmul] + have hreg_eq : L.mem sk (vcpuRegWA vc r.val) = L.mem s0 (vcpuRegWA vc r.val) := + Ik.wr.mem_eq (hVcOff r.val hr64).1 (hVcOff r.val hr64).2 + obtain ⟨hst, Ik'⟩ := Ik.step hfit hal hBfAl hBfSpec huf (k := r.val) (by omega) (L.mem sk (vcpuRegWA vc r.val)) + have hkv_k : σk.kheap (specPtr vc) = some (.archObj (.vcpu xv)) := by rw [Ik.kheap]; exact hkv + have hstep_spec : copyRegsToFrame (specPtr vc) (specPtr frame) (List.range' start.val (r.val + 1 - start.val)) σ1 = + ({((), setUm σk (storeMem σk.machineState.underlyingMemory (bufAddr (specPtr frame) r.val) + (L.mem sk (vcpuRegWA vc r.val)).bv))}, False) := by + have hlen : r.val + 1 - start.val = (r.val - start.val) + 1 := by omega + rw [hlen, List.range'_concat] + have hadd : start.val + (r.val - start.val) = r.val := by omega + rw [Nat.one_mul, hadd] + unfold copyRegsToFrame at hspec_k ⊢ + rw [mapMx_appendR, nd_bind_oneSc hspec_k] + change copyRegsToFrame (specPtr vc) (specPtr frame) [r.val] σk = _ + rw [copyRegsToFrame_consR _ _ r.val [] hkv_k (frame_bufAddr_al3A hfit hal (by omega)) + (inUserFrame_of_kheapR Ik.kheap (huf r.val (by omega))), + ← hSnap r.val hr64, ← hreg_eq] + simp [copyRegsToFrame, NonDet.mapMx]; rfl + have hr1 : (⟨BitVec.ofNat 64 (r.val + 1#u64.val)⟩ : U64).val = r.val + 1 := by + change (BitVec.ofNat 64 (r.val + 1)).toNat = r.val + 1 + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + refine ⟨⟨by rw [hr1]; omega, by rw [hr1]; omega, _, by rw [hr1]; exact hstep_spec, Ik'⟩, by dsimp only; rw [hr1]; omega⟩ + · rw [if_neg hle_r, WP.spec_ok] + have hreq : r.val = end_incl.val + 1 := by have : ¬ r.val ≤ end_incl.val := hle_r; omega + refine ⟨σk, ?_, Ik⟩ + unfold idxRange; rw [← hreq]; exact hspec_k + +/-- Helper: `vcpu.read_state_to_frame_loop0` (VGIC AP1R0..3). -/ +private theorem read_ap1r_loop_invA {s0 s1 : S} {σ0 σ1 : Fl4SpecA64.State Unit} + {vc frame : U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {xv : Fl4SpecA64.Vcpu} + (I1 : ReadInvA (L := L) s0 σ0 frame Bf s1 σ1) + (hfit : frame.val + 1024 ≤ 2 ^ 64) (hal : frame.val % 8 = 0) + (hBfAl : ∀ bp ∈ Bf, ∀ j : U64, j.val < 128 → Aligned (bufW bp.1 j)) + (hBfSpec : ∀ bp ∈ Bf, bp.2 = specPtr bp.1) + (huf : ∀ k < 128, inUserFrame (bufAddr (specPtr frame) k) σ0) + (hkv : σ0.kheap (specPtr vc) = some (.archObj (.vcpu xv))) + (hSnap : ∀ i < 64, (L.mem s0 (vcpuRegWA vc i)).bv = fl4VcpuRegGet xv i) + (hVcOff : ∀ i < 64, Aligned (vcpuRegWA vc i) ∧ ¬ FrameWinW frame (vcpuRegWA vc i)) : + vcpu.read_state_to_frame_loop0 st s1 vc frame fl4GicV3 fl4_abi.consts.vcpu_reg.VGIC_AP1R0 ⦃ s2 => + ∃ σ2, (NonDet.mapMx (fun r => do + let w ← if fl4GicV3 then fl4VcpuGet (specPtr vc) r else pure 0 + storeWordOffs (specPtr frame) r w) (idxRange vrVgicAp1r0 vrVgicAp1r3)) σ1 = ({((), σ2)}, False) ∧ + ReadInvA (L := L) s0 σ0 frame Bf s2 σ2 ⦄ := by + unfold vcpu.read_state_to_frame_loop0 + have h44 : fl4_abi.consts.vcpu_reg.VGIC_AP1R0.val = vrVgicAp1r0 := by + unfold fl4_abi.consts.vcpu_reg.VGIC_AP1R0 vrVgicAp1r0; rfl + have h47 : fl4_abi.consts.vcpu_reg.VGIC_AP1R3.val = vrVgicAp1r3 := by + unfold fl4_abi.consts.vcpu_reg.VGIC_AP1R3 vrVgicAp1r3; rfl + let stepSpec : Nat → SMonad Unit Unit := fun r => do + let w ← if fl4GicV3 then fl4VcpuGet (specPtr vc) r else pure 0 + storeWordOffs (specPtr frame) r w + let Inv : S × U64 → Prop := fun p => + vrVgicAp1r0 ≤ p.2.val ∧ p.2.val ≤ vrVgicAp1r3 + 1 ∧ + ∃ σk, NonDet.mapMx stepSpec (List.range' vrVgicAp1r0 (p.2.val - vrVgicAp1r0)) σ1 = ({((), σk)}, False) ∧ + ReadInvA (L := L) s0 σ0 frame Bf p.1 σk + let Post : S → Prop := fun s2 => + ∃ σ2, (NonDet.mapMx stepSpec (idxRange vrVgicAp1r0 vrVgicAp1r3)) σ1 = ({((), σ2)}, False) ∧ + ReadInvA (L := L) s0 σ0 frame Bf s2 σ2 + apply loop.spec_decr_nat (measure := fun p => vrVgicAp1r3 + 1 - p.2.val) (inv := Inv) (post := Post) + swap + · exact ⟨by rw [h44], by rw [h44]; unfold vrVgicAp1r0 vrVgicAp1r3; decide, + σ1, by rw [h44]; simp [NonDet.mapMx]; rfl, I1⟩ + rintro ⟨sk, r⟩ ⟨hsr, hre, σk, hspec_k, Ik⟩ + dsimp only at hsr hre ⊢; unfold vcpu.read_state_to_frame_loop0.body + by_cases hle_r : r ≤ fl4_abi.consts.vcpu_reg.VGIC_AP1R3 + · have hle_rv : r.val ≤ vrVgicAp1r3 := by + have : r.val ≤ fl4_abi.consts.vcpu_reg.VGIC_AP1R3.val := hle_r; omega + have hr64 : r.val < 64 := by unfold vrVgicAp1r3 at *; omega + have hval_ok : (if fl4GicV3 = true then vcpu.reg_get st sk vc r else ok 0#u64) = + ok (if fl4GicV3 then L.mem sk (vcpuRegWA vc r.val) else 0#u64) := by + cases fl4GicV3 + · simp + · simp [vcpu_reg_get_u64_eqR sk vc r hr64] + rw [if_pos hle_r, hval_ok, bind_ok, + u64_mul_okR (by change r.val * 8 < 2 ^ 64; omega), bind_ok, + lift_ok, bind_ok, L.write_eq, bind_ok, u64_add_ok (by change r.val + 1 < 2 ^ 64; omega), bind_ok, WP.spec_ok] + set w : U64 := if fl4GicV3 then L.mem sk (vcpuRegWA vc r.val) else 0#u64 + have hmul : (⟨BitVec.ofNat 64 (r.val * 8#u64.val)⟩ : U64) = ⟨BitVec.ofNat 64 (8 * r.val)⟩ := by + congr 1; congr 1; change r.val * 8 = 8 * r.val; omega + rw [hmul] + obtain ⟨hst, Ik'⟩ := Ik.step hfit hal hBfAl hBfSpec huf (k := r.val) (by omega) w + have hkv_k : σk.kheap (specPtr vc) = some (.archObj (.vcpu xv)) := by rw [Ik.kheap]; exact hkv + have hw_bv : w.bv = if fl4GicV3 then fl4VcpuRegGet xv r.val else 0 := by + dsimp only [w] + cases fl4GicV3 + · rfl + · simp only [↓reduceIte] + rw [Ik.wr.mem_eq (hVcOff r.val hr64).1 (hVcOff r.val hr64).2] + exact hSnap r.val hr64 + have hstep_spec : NonDet.mapMx stepSpec (List.range' vrVgicAp1r0 (r.val + 1 - vrVgicAp1r0)) σ1 = + ({((), setUm σk (storeMem σk.machineState.underlyingMemory (bufAddr (specPtr frame) r.val) w.bv))}, False) := by + have hlen : r.val + 1 - vrVgicAp1r0 = (r.val - vrVgicAp1r0) + 1 := by omega + rw [hlen, List.range'_concat] + have hadd : vrVgicAp1r0 + (r.val - vrVgicAp1r0) = r.val := by omega + rw [Nat.one_mul, hadd, mapMx_appendR, nd_bind_oneSc hspec_k] + unfold NonDet.mapMx + change (stepSpec r.val >>= fun _ => NonDet.pure ()) σk = _ + have h1 : stepSpec r.val σk = + ({((), setUm σk (storeMem σk.machineState.underlyingMemory (bufAddr (specPtr frame) r.val) w.bv))}, False) := by + dsimp only [stepSpec] + cases hgv : fl4GicV3 + · rw [hgv] at hw_bv; simp only [Bool.false_eq_true, ↓reduceIte] at hw_bv ⊢ + change (NonDet.pure 0 >>= _) σk = _ + rw [nd_bind_oneSc (m := NonDet.pure 0) rfl, ← hw_bv]; exact hst + · rw [hgv] at hw_bv; simp only [↓reduceIte] at hw_bv ⊢ + rw [nd_bind_oneSc (fl4VcpuGet_eqA r.val hkv_k), ← hw_bv]; exact hst + rw [nd_bind_oneSc h1]; rfl + have hr1 : (⟨BitVec.ofNat 64 (r.val + 1#u64.val)⟩ : U64).val = r.val + 1 := by + change (BitVec.ofNat 64 (r.val + 1)).toNat = r.val + 1 + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + refine ⟨⟨by rw [hr1]; omega, by rw [hr1]; omega, _, by rw [hr1]; exact hstep_spec, Ik'⟩, by dsimp only; rw [hr1]; omega⟩ + · rw [if_neg hle_r, WP.spec_ok] + have hreq : r.val = vrVgicAp1r3 + 1 := by + have : ¬ r.val ≤ fl4_abi.consts.vcpu_reg.VGIC_AP1R3.val := hle_r; omega + refine ⟨σk, ?_, Ik⟩ + unfold idxRange; rw [← hreq]; exact hspec_k + +/-- Helper: `vcpu.read_state_to_frame_loop1` (VGIC LR0..15). -/ +private theorem read_lrs_loop_invA {s0 s1 : S} {σ0 σ1 : Fl4SpecA64.State Unit} + {vc frame : U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {xv : Fl4SpecA64.Vcpu} + (I1 : ReadInvA (L := L) s0 σ0 frame Bf s1 σ1) + (hfit : frame.val + 1024 ≤ 2 ^ 64) (hal : frame.val % 8 = 0) + (hBfAl : ∀ bp ∈ Bf, ∀ j : U64, j.val < 128 → Aligned (bufW bp.1 j)) + (hBfSpec : ∀ bp ∈ Bf, bp.2 = specPtr bp.1) + (huf : ∀ k < 128, inUserFrame (bufAddr (specPtr frame) k) σ0) + (hkv : σ0.kheap (specPtr vc) = some (.archObj (.vcpu xv))) + (hSnap : ∀ i < 64, (L.mem s0 (vcpuRegWA vc i)).bv = fl4VcpuRegGet xv i) + (hVcOff : ∀ i < 64, Aligned (vcpuRegWA vc i) ∧ ¬ FrameWinW frame (vcpuRegWA vc i)) : + vcpu.read_state_to_frame_loop1 st s1 vc frame ⟨BitVec.ofNat 64 fl4NumLrs⟩ 0#u64 ⦃ s2 => + ∃ σ2, (NonDet.mapMx (fun i => do + let w ← if i < fl4NumLrs then fl4VcpuGet (specPtr vc) (vrVgicLr0 + i) else pure 0 + storeWordOffs (specPtr frame) (vrVgicLr0 + i) w) (List.range 16)) σ1 = ({((), σ2)}, False) ∧ + ReadInvA (L := L) s0 σ0 frame Bf s2 σ2 ⦄ := by + unfold vcpu.read_state_to_frame_loop1 + have hnl : (⟨BitVec.ofNat 64 fl4NumLrs⟩ : U64).val = fl4NumLrs := by + change (BitVec.ofNat 64 fl4NumLrs).toNat = fl4NumLrs + rw [BitVec.toNat_ofNat] + have hle : fl4NumLrs ≤ 16 := by unfold fl4NumLrs; dsimp only; split_ifs <;> omega + exact Nat.mod_eq_of_lt (by omega) + let stepSpec : Nat → SMonad Unit Unit := fun i => do + let w ← if i < fl4NumLrs then fl4VcpuGet (specPtr vc) (vrVgicLr0 + i) else pure 0 + storeWordOffs (specPtr frame) (vrVgicLr0 + i) w + let Inv : S × U64 → Prop := fun p => + p.2.val ≤ 16 ∧ + ∃ σk, NonDet.mapMx stepSpec (List.range p.2.val) σ1 = ({((), σk)}, False) ∧ + ReadInvA (L := L) s0 σ0 frame Bf p.1 σk + let Post : S → Prop := fun s2 => + ∃ σ2, NonDet.mapMx stepSpec (List.range 16) σ1 = ({((), σ2)}, False) ∧ + ReadInvA (L := L) s0 σ0 frame Bf s2 σ2 + apply loop.spec_decr_nat (measure := fun p => 16 - p.2.val) (inv := Inv) (post := Post) + swap + · exact ⟨by dsimp only; decide, σ1, by simp [NonDet.mapMx]; rfl, I1⟩ + rintro ⟨sk, i⟩ ⟨hle16, σk, hspec_k, Ik⟩ + dsimp only at hle16 ⊢; unfold vcpu.read_state_to_frame_loop1.body + by_cases hi16 : i < 16#u64 + · have hilt : i.val < 16 := hi16 + have hr_val : (wa fl4_abi.consts.vcpu_reg.VGIC_LR0 i).val = vrVgicLr0 + i.val := by + rw [wa_valP] + have h48 : fl4_abi.consts.vcpu_reg.VGIC_LR0.val = 48 := by unfold fl4_abi.consts.vcpu_reg.VGIC_LR0; rfl + rw [h48]; unfold vrVgicLr0; exact Nat.mod_eq_of_lt (by omega) + have hr64 : (wa fl4_abi.consts.vcpu_reg.VGIC_LR0 i).val < 64 := by rw [hr_val]; unfold vrVgicLr0; omega + have hval_ok : (if i < ⟨BitVec.ofNat 64 fl4NumLrs⟩ then + vcpu.reg_get st sk vc (wa fl4_abi.consts.vcpu_reg.VGIC_LR0 i) else ok 0#u64) = + ok (if i.val < fl4NumLrs then L.mem sk (vcpuRegWA vc (vrVgicLr0 + i.val)) else 0#u64) := by + by_cases hlt : i.val < fl4NumLrs + · rw [if_pos (by change i.val < (⟨BitVec.ofNat 64 fl4NumLrs⟩ : U64).val; omega), if_pos hlt, + vcpu_reg_get_u64_eqR sk vc _ hr64, hr_val] + · rw [if_neg (by change ¬ i.val < (⟨BitVec.ofNat 64 fl4NumLrs⟩ : U64).val; omega), if_neg hlt] + rw [if_pos hi16, lift_ok, bind_ok, hval_ok, bind_ok, + u64_mul_okR (by rw [hr_val]; change (vrVgicLr0 + i.val) * 8 < 2 ^ 64; unfold vrVgicLr0; omega), bind_ok, + lift_ok, bind_ok, L.write_eq, bind_ok, u64_add_ok (by change i.val + 1 < 2 ^ 64; omega), bind_ok, WP.spec_ok] + set w : U64 := if i.val < fl4NumLrs then L.mem sk (vcpuRegWA vc (vrVgicLr0 + i.val)) else 0#u64 + have hmul : (⟨BitVec.ofNat 64 ((wa fl4_abi.consts.vcpu_reg.VGIC_LR0 i).val * 8#u64.val)⟩ : U64) = + ⟨BitVec.ofNat 64 (8 * (vrVgicLr0 + i.val))⟩ := by + congr 1; rw [hr_val]; congr 1; change (vrVgicLr0 + i.val) * 8 = 8 * (vrVgicLr0 + i.val); omega + rw [hmul] + obtain ⟨hst, Ik'⟩ := Ik.step hfit hal hBfAl hBfSpec huf (k := vrVgicLr0 + i.val) (by unfold vrVgicLr0; omega) w + have hkv_k : σk.kheap (specPtr vc) = some (.archObj (.vcpu xv)) := by rw [Ik.kheap]; exact hkv + have hw_bv : w.bv = if i.val < fl4NumLrs then fl4VcpuRegGet xv (vrVgicLr0 + i.val) else 0 := by + dsimp only [w] + by_cases hlt : i.val < fl4NumLrs + · rw [if_pos hlt, if_pos hlt] + rw [Ik.wr.mem_eq (hVcOff _ (by unfold vrVgicLr0; omega)).1 (hVcOff _ (by unfold vrVgicLr0; omega)).2] + exact hSnap _ (by unfold vrVgicLr0; omega) + · rw [if_neg hlt, if_neg hlt]; rfl + have hstep_spec : NonDet.mapMx stepSpec (List.range (i.val + 1)) σ1 = + ({((), setUm σk (storeMem σk.machineState.underlyingMemory (bufAddr (specPtr frame) (vrVgicLr0 + i.val)) w.bv))}, False) := by + rw [List.range_succ, mapMx_appendR, nd_bind_oneSc hspec_k] + unfold NonDet.mapMx + change (stepSpec i.val >>= fun _ => NonDet.pure ()) σk = _ + have h1 : stepSpec i.val σk = + ({((), setUm σk (storeMem σk.machineState.underlyingMemory (bufAddr (specPtr frame) (vrVgicLr0 + i.val)) w.bv))}, False) := by + dsimp only [stepSpec] + by_cases hlt : i.val < fl4NumLrs + · rw [if_pos hlt] at hw_bv ⊢ + rw [nd_bind_oneSc (fl4VcpuGet_eqA (vrVgicLr0 + i.val) hkv_k), ← hw_bv]; exact hst + · rw [if_neg hlt] at hw_bv ⊢ + change (NonDet.pure 0 >>= _) σk = _ + rw [nd_bind_oneSc (m := NonDet.pure 0) rfl, ← hw_bv]; exact hst + rw [nd_bind_oneSc h1]; rfl + have hi1 : (⟨BitVec.ofNat 64 (i.val + 1#u64.val)⟩ : U64).val = i.val + 1 := by + change (BitVec.ofNat 64 (i.val + 1)).toNat = i.val + 1 + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + refine ⟨⟨by rw [hi1]; omega, _, by rw [hi1]; exact hstep_spec, Ik'⟩, by dsimp only; rw [hi1]; omega⟩ + · rw [if_neg hi16, WP.spec_ok] + have hieq : i.val = 16 := by have : ¬ i.val < 16 := hi16; omega + exact ⟨σk, by rwa [← hieq], Ik⟩ + +/-- Helper: `vcpu.read_state_to_frame_loop2` (37 GPRs). -/ +private theorem read_gprs_loop_invA {s0 s1 : S} {σ0 σ1 : Fl4SpecA64.State Unit} + {vc frame : U64} {Bf : Set (U64 × Fl4SpecA64.Word)} {xv : Fl4SpecA64.Vcpu} + (I1 : ReadInvA (L := L) s0 σ0 frame Bf s1 σ1) + (hfit : frame.val + 1024 ≤ 2 ^ 64) (hal : frame.val % 8 = 0) + (hBfAl : ∀ bp ∈ Bf, ∀ j : U64, j.val < 128 → Aligned (bufW bp.1 j)) + (hBfSpec : ∀ bp ∈ Bf, bp.2 = specPtr bp.1) + (huf : ∀ k < 128, inUserFrame (bufAddr (specPtr frame) k) σ0) + (hvt : xv.vcpuTcb = optRefA (L.mem s0 vc)) + (hTcb : L.mem s0 vc ≠ 0#u64 → ∃ xt, σ0.kheap (specPtr (L.mem s0 vc)) = some (.tcb xt) ∧ + (∀ i < 37, (L.mem s0 (wa (wa (L.mem s0 vc) layout.tcb.REGS) ⟨BitVec.ofNat 64 (8 * i)⟩)).bv = + xt.tcbArch.tcbContext.userRegs (idxRegA i) ∧ + Aligned (wa (wa (L.mem s0 vc) layout.tcb.REGS) ⟨BitVec.ofNat 64 (8 * i)⟩) ∧ + ¬ FrameWinW frame (wa (wa (L.mem s0 vc) layout.tcb.REGS) ⟨BitVec.ofNat 64 (8 * i)⟩))) : + vcpu.read_state_to_frame_loop2 st s1 (wa frame 512#u64) (L.mem s0 vc) 0#u64 ⦃ s2 => + ∃ σ2, (NonDet.mapMx (fun (i, r) => do + let w ← match xv.vcpuTcb with | some t => asUser t (getRegister r) | none => pure 0 + storeWordOffs (specPtr frame) (64 + i) w) + (List.range fl4UserRegs.length |>.zip fl4UserRegs)) σ1 = ({((), σ2)}, False) ∧ + ReadInvA (L := L) s0 σ0 frame Bf s2 σ2 ⦄ := by + unfold vcpu.read_state_to_frame_loop2 + let stepSpec : Nat × Register → SMonad Unit Unit := fun (i, r) => do + let w ← match xv.vcpuTcb with | some t => asUser t (getRegister r) | none => pure 0 + storeWordOffs (specPtr frame) (64 + i) w + have hlen37 : fl4UserRegs.length = 37 := rfl + let Inv : S × U64 → Prop := fun p => + p.2.val ≤ 37 ∧ + ∃ σk, NonDet.mapMx stepSpec ((List.range p.2.val).zip (fl4UserRegs.take p.2.val)) σ1 = ({((), σk)}, False) ∧ + ReadInvA (L := L) s0 σ0 frame Bf p.1 σk + let Post : S → Prop := fun s2 => + ∃ σ2, NonDet.mapMx stepSpec (List.range fl4UserRegs.length |>.zip fl4UserRegs) σ1 = ({((), σ2)}, False) ∧ + ReadInvA (L := L) s0 σ0 frame Bf s2 σ2 + apply loop.spec_decr_nat (measure := fun p => 37 - p.2.val) (inv := Inv) (post := Post) + swap + · exact ⟨by dsimp only; decide, σ1, by simp [NonDet.mapMx]; rfl, I1⟩ + rintro ⟨sk, i⟩ ⟨hle37, σk, hspec_k, Ik⟩ + dsimp only at hle37 ⊢; unfold vcpu.read_state_to_frame_loop2.body + have hcnt : layout.reg.COUNT = 37#u64 := by unfold layout.reg.COUNT; rfl + rw [hcnt] + by_cases hi37 : i < 37#u64 + · have hilt : i.val < 37 := hi37 + have hval_ok : (if L.mem s0 vc != 0#u64 then obj.Tcb.reg st (L.mem s0 vc) sk i else ok 0#u64) = + ok (if L.mem s0 vc ≠ 0#u64 then L.mem sk (wa (wa (L.mem s0 vc) layout.tcb.REGS) ⟨BitVec.ofNat 64 (8 * i.val)⟩) else 0#u64) := by + by_cases ht0 : L.mem s0 vc = 0#u64 + · have hne : (L.mem s0 vc != 0#u64) = false := by rw [ht0]; rfl + rw [hne, if_neg (by decide), if_neg (not_not.mpr ht0)] + · have hne : (L.mem s0 vc != 0#u64) = true := bne_iff_ne.mpr ht0 + rw [hne, if_pos rfl, if_pos ht0] + unfold obj.Tcb.reg + rw [lift_ok, bind_ok, lift_ok, bind_ok, lift_ok, bind_ok, L.read_eq] + congr 2; congr 1; apply UScalar.eq_of_val_eq + have h8 : (8#u64 : U64).val = 8 := rfl + have hs : UScalar.size UScalarTy.U64 = 2 ^ 64 := by simp [U64.size_def, U64.numBits] + change (core.num.U64.wrapping_mul i 8#u64).val = (⟨BitVec.ofNat 64 (8 * i.val)⟩ : U64).val + rw [core.num.U64.wrapping_mul_val_eq, h8, hs] + show (i.val * 8) % 2 ^ 64 = (BitVec.ofNat 64 (8 * i.val)).toNat + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega), Nat.mod_eq_of_lt (by omega)]; omega + rw [if_pos hi37, hval_ok, bind_ok, + u64_mul_okR (by change i.val * 8 < 2 ^ 64; omega), bind_ok, lift_ok, bind_ok, L.write_eq, bind_ok, + u64_add_ok (by change i.val + 1 < 2 ^ 64; omega), bind_ok, WP.spec_ok] + set w : U64 := if L.mem s0 vc ≠ 0#u64 then + L.mem sk (wa (wa (L.mem s0 vc) layout.tcb.REGS) ⟨BitVec.ofNat 64 (8 * i.val)⟩) else 0#u64 + have haddr : wa (wa frame 512#u64) ⟨BitVec.ofNat 64 (i.val * 8#u64.val)⟩ = + wa frame ⟨BitVec.ofNat 64 (8 * (64 + i.val))⟩ := by + apply UScalar.eq_of_val_eq + have h512 : (512#u64 : U64).val = 512 := rfl + have hm1 : (⟨BitVec.ofNat 64 (i.val * 8#u64.val)⟩ : U64).val = i.val * 8 := by + show (BitVec.ofNat 64 (i.val * 8)).toNat = i.val * 8 + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + have e1 : (wa frame 512#u64).val = frame.val + 512 := by rw [wa_valm, h512]; exact Nat.mod_eq_of_lt (by omega) + rw [wa_valm, e1, hm1, frame_word_valA hfit (by omega), Nat.mod_eq_of_lt (by omega)]; omega + rw [haddr] + obtain ⟨hst, Ik'⟩ := Ik.step hfit hal hBfAl hBfSpec huf (k := 64 + i.val) (by omega) w + have hstep_spec : NonDet.mapMx stepSpec ((List.range (i.val + 1)).zip (fl4UserRegs.take (i.val + 1))) σ1 = + ({((), setUm σk (storeMem σk.machineState.underlyingMemory (bufAddr (specPtr frame) (64 + i.val)) w.bv))}, False) := by + have hzip : (List.range (i.val + 1)).zip (fl4UserRegs.take (i.val + 1)) = + ((List.range i.val).zip (fl4UserRegs.take i.val)) ++ [(i.val, idxRegA i.val)] := by + rw [List.range_succ, List.take_succ_eq_append_getElem (by rw [hlen37]; omega), + List.zip_append (by rw [List.length_range, List.length_take, hlen37]; omega), + fl4UserRegs_getElemR i.val hilt]; rfl + rw [hzip, mapMx_appendR, nd_bind_oneSc hspec_k] + unfold NonDet.mapMx + change (stepSpec (i.val, idxRegA i.val) >>= fun _ => NonDet.pure ()) σk = _ + have h1 : stepSpec (i.val, idxRegA i.val) σk = + ({((), setUm σk (storeMem σk.machineState.underlyingMemory (bufAddr (specPtr frame) (64 + i.val)) w.bv))}, False) := by + dsimp only [stepSpec] + by_cases ht0 : L.mem s0 vc = 0#u64 + · have hvn : xv.vcpuTcb = none := by rw [hvt, ht0]; unfold optRefA; simp + have hw0 : w.bv = 0 := by dsimp only [w]; rw [if_neg (not_not.mpr ht0)]; rfl + rw [hvn, hw0] + change (NonDet.pure 0 >>= _) σk = _ + rw [nd_bind_oneSc (m := NonDet.pure 0) rfl, ← hw0]; exact hst + · have hvs : xv.vcpuTcb = some (specPtr (L.mem s0 vc)) := by rw [hvt]; unfold optRefA; simp [ht0] + obtain ⟨xt, hkt, hregs⟩ := hTcb ht0 + obtain ⟨hrv, hral, hroff⟩ := hregs i.val hilt + have hkt_k : σk.kheap (specPtr (L.mem s0 vc)) = some (.tcb xt) := by rw [Ik.kheap]; exact hkt + have hwbv : w.bv = xt.tcbArch.tcbContext.userRegs (idxRegA i.val) := by + dsimp only [w]; rw [if_pos ht0, Ik.wr.mem_eq hral hroff]; exact hrv + rw [hvs]; dsimp only + rw [nd_bind_oneSc (asUser_getReg_eqR hkt_k (idxRegA i.val)), ← hwbv]; exact hst + rw [nd_bind_oneSc h1]; rfl + have hi1 : (⟨BitVec.ofNat 64 (i.val + 1#u64.val)⟩ : U64).val = i.val + 1 := by + change (BitVec.ofNat 64 (i.val + 1)).toNat = i.val + 1 + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + refine ⟨⟨by rw [hi1]; omega, _, by rw [hi1]; exact hstep_spec, Ik'⟩, by dsimp only; rw [hi1]; omega⟩ + · rw [if_neg hi37, WP.spec_ok] + have hieq : i.val = 37 := by have : ¬ i.val < 37 := hi37; omega + refine ⟨σk, ?_, Ik⟩ + rw [hieq, ← hlen37, List.take_length] at hspec_k; exact hspec_k + +set_option maxHeartbeats 800000 in +/-- **`vcpu.read_state_to_frame` refinement on `JStage3VcpuCoreA`** (serves both `JStage3TA` +and `JStage3A` without requiring `SActA σ`). -/ +theorem vcpu_read_state_to_frame_coreA + {AR : A → HwRootA → Prop} {Clock : A → Fl4SpecA64.MachineState → Prop} + {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J7 : JSlotA S} + [J7PpA st J7] [J7PpKA st J7] [J7FrA st J7] + {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3VcpuCoreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + (hSV : SchedValidA σ) + (R6 : VcpuRec6A st Z OS Bf s σ) + (hCl : Clock a σ.machineState) + (hClFr : ∀ a' ms ms', Clock a' ms → ms'.timeState = ms.timeState → + ms'.lastMachineTime = ms.lastMachineTime → Clock a' ms') + (hP : VcpuPlatAgreeA ar) + {vc : U64} (hvc : vc ∈ Z.V) + {y ptr : U64} {sz : capability.FrameSize} {mp ept : Bool} {asid va bits offset : U64} + (hy : y ∈ OS) + (hcap : capA st s y = .Frame ptr sz capability.VM_READ_WRITE false mp ept asid va) + (hbits : capability.FrameSize.bits sz = ok bits) + (hoff64 : offset &&& 63#u64 = 0#u64) + (hoff_lt : offset.val < 2 ^ bits.val) + (hoff_sz : 2 ^ bits.val - offset.val ≥ 1024) + (mask : U64) : + let frame := wa ptr offset + vcpu.read_state_to_frame st ar s a vc frame mask ⦃ s' => + ∃ σ' : Fl4SpecA64.State Unit, + fl4ReadStateToFrame (specPtr vc) (specPtr frame) mask.bv σ = ({((), σ')}, False) ∧ + JStage3VcpuCoreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s' a σ' m ∧ + SchedValidA σ' ∧ + VcpuRec6A st Z OS Bf s' σ' ∧ + Clock a σ'.machineState ∧ + σ'.machineState.timeState = σ.machineState.timeState ∧ + σ'.machineState.lastMachineTime = σ.machineState.lastMachineTime ∧ + σ'.kheap = σ.kheap ∧ + L.ksb s' = L.ksb s ∧ L.cpu s' = L.cpu s ∧ + OSKeep st OS s s' ∧ + WritesIn st s s' (VcpuWrNoStA st Z OS s) ∧ + VcpuSpecStepA H σ σ' ∧ + (∀ v ∈ Z.V, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → L.mem s' (wa v o) = L.mem s (wa v o)) ∧ + (∀ u ∈ Z.T, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → L.mem s' (wa u o) = L.mem s (wa u o)) ∧ + FrKeepA st OS s σ s' σ' ⦄ := by + dsimp only + set frame := wa ptr offset + obtain ⟨Pt, Ub, vr, arm, -, fo, -, frub, ubal, vcub, -⟩ := J7FrA.fr (st := st) _ _ _ _ _ _ J.j7 + have hpH : H ptr = some (.other bits.val) := fo y hy ptr sz capability.VM_READ_WRITE mp ept asid va hcap bits hbits + have hptrFit := J.ent.heap.fits ptr (.other bits.val) hpH + have hptrAl := J.ent.heap.align ptr (.other bits.val) hpH + simp only [HKind.bits] at hptrFit hptrAl + have hbits12 : 12 ≤ bits.val := by + cases sz <;> simp [capability.FrameSize.bits] at hbits <;> subst hbits <;> decide + have hpow8 : 8 ∣ 2 ^ bits.val := by + have : 2 ^ 3 ∣ 2 ^ bits.val := Nat.pow_dvd_pow 2 (by omega) + exact this + have hptr8 : ptr.val % 8 = 0 := by + have : 8 ∣ ptr.val := Nat.dvd_trans hpow8 (Nat.dvd_of_mod_eq_zero hptrAl) + exact Nat.mod_eq_zero_of_dvd this + have hoff8 : offset.val % 8 = 0 := by + have h64 : offset.val % 64 = 0 := by + have hm : offset.val &&& 63 = offset.val % 64 := by + simpa using Nat.and_two_pow_sub_one_eq_mod offset.val 6 + have h0 : (offset &&& 63#u64).val = 0 := by rw [hoff64]; rfl + have hand : (offset &&& 63#u64).val = offset.val &&& 63 := by + change (offset.bv &&& (63#64 : BitVec 64)).toNat = offset.val &&& 63 + rw [BitVec.toNat_and]; rfl + omega + omega + have hfrVal : frame.val = ptr.val + offset.val := by + rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have hfit : frame.val + 1024 ≤ 2 ^ 64 := by omega + have hal : frame.val % 8 = 0 := by rw [hfrVal]; omega + have hFwWr : ∀ b, FrameWinW frame b → VcpuWrNoStA st Z OS s b := by + rintro b ⟨k, hk, rfl⟩ + have hval := frame_word_valA hfit hk + exact Or.inr (Or.inr ⟨y, hy, ptr, sz, capability.VM_READ_WRITE, mp, ept, asid, va, bits, + hcap, hbits, frame_word_alA hfit hal hk, by omega, by omega⟩) + have hFwUb : ∀ b, FrameWinW frame b → b ∈ Ub := by + rintro b ⟨k, hk, rfl⟩ + have hval := frame_word_valA hfit hk + exact frub y hy ptr sz _ mp ept asid va hcap bits hbits _ (frame_word_alA hfit hal hk) (by omega) (by omega) + have huf : ∀ k < 128, inUserFrame (bufAddr (specPtr frame) k) σ := by + intro k hk + have hsp : specPtr (wa frame ⟨BitVec.ofNat 64 (8 * k)⟩) = bufAddr (specPtr frame) k := + specPtr_wa_wordA (by omega) + rw [← hsp] + have hval := frame_word_valA hfit hk + have hsz_eq : pageBitsForSize (frameSizeOfA sz) = bits.val := by + cases sz <;> simp [capability.FrameSize.bits] at hbits <;> subst hbits <;> decide + have hbits_le30 : bits.val ≤ 30 := by + cases sz <;> simp [capability.FrameSize.bits] at hbits <;> subst hbits <;> decide + have hdev_k := R6.userFr y hy ptr sz capability.VM_READ_WRITE mp ept asid va hcap + refine ⟨frameSizeOfA sz, ?_⟩ + rw [hsz_eq] + have hmask_eq : specPtr (wa frame ⟨BitVec.ofNat 64 (8 * k)⟩) &&& ~~~Fl4SpecA64.mask bits.val = specPtr ptr := by + have hp_al : (specPtr ptr).toNat % 2 ^ bits.val = 0 := by + unfold specPtr + have hsh : pptrShiftA.toNat % 2 ^ bits.val = 0 := by + have hd : 2 ^ bits.val ∣ 2 ^ 30 := Nat.pow_dvd_pow 2 hbits_le30 + have hsh30 : 2 ^ 30 ∣ pptrShiftA.toNat := by decide + exact Nat.mod_eq_zero_of_dvd (Nat.dvd_trans hd hsh30) + rw [BitVec.toNat_add] + have h264 : 2 ^ bits.val ∣ 2 ^ 64 := Nat.pow_dvd_pow 2 (by omega) + rw [Nat.mod_mod_of_dvd _ h264, Nat.add_mod] + have hptr_mod : ptr.bv.toNat % 2 ^ bits.val = 0 := hptrAl + rw [hptr_mod, hsh]; simp + have hsp_add : specPtr (wa frame ⟨BitVec.ofNat 64 (8 * k)⟩) = + specPtr ptr + BitVec.ofNat 64 (offset.val + 8 * k) := by + have hwf : wa frame ⟨BitVec.ofNat 64 (8 * k)⟩ = wa ptr ⟨BitVec.ofNat 64 (offset.val + 8 * k)⟩ := by + apply UScalar.eq_of_val_eq + have hd : (⟨BitVec.ofNat 64 (offset.val + 8 * k)⟩ : U64).val = offset.val + 8 * k := by + show (BitVec.ofNat 64 _).toNat = _; rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + rw [hval, hfrVal, wa_valm, hd, Nat.mod_eq_of_lt (by omega)]; omega + rw [hwf]; unfold specPtr + have hadd : (wa ptr ⟨BitVec.ofNat 64 (offset.val + 8 * k)⟩).bv = + ptr.bv + BitVec.ofNat 64 (offset.val + 8 * k) := by + apply BitVec.eq_of_toNat_eq + have hd : (⟨BitVec.ofNat 64 (offset.val + 8 * k)⟩ : U64).val = offset.val + 8 * k := by + show (BitVec.ofNat 64 _).toNat = _; rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + change (wa ptr ⟨BitVec.ofNat 64 (offset.val + 8 * k)⟩).val = _ + rw [wa_valm, hd, BitVec.toNat_add, BitVec.toNat_ofNat, + Nat.mod_eq_of_lt (by omega : offset.val + 8 * k < 2 ^ 64), + Nat.mod_eq_of_lt (by change ptr.val + (offset.val + 8 * k) < 2 ^ 64; omega)] + exact (Nat.mod_eq_of_lt (by change ptr.val + (offset.val + 8 * k) < 2 ^ 64; omega)).symm + rw [hadd, BitVec.add_assoc, BitVec.add_comm (BitVec.ofNat 64 (offset.val + 8 * k)) pptrShiftA, ← BitVec.add_assoc] + rw [hsp_add] + set p := specPtr ptr + set d := offset.val + 8 * k + have hd_lt : d < 2 ^ bits.val := by omega + have hp_div : p.toNat = (p.toNat / 2 ^ bits.val) * 2 ^ bits.val := by + have hdm := Nat.div_add_mod p.toNat (2 ^ bits.val) + rw [hp_al, Nat.add_zero, Nat.mul_comm] at hdm; exact hdm.symm + have hpd_lt : p.toNat + d < 2 ^ 64 := by + have hp_lt : p.toNat < 2 ^ 64 := p.isLt + have h264 : 2 ^ bits.val ∣ 2 ^ 64 := Nat.pow_dvd_pow 2 (by omega) + have hle : p.toNat + 2 ^ bits.val ≤ 2 ^ 64 := by + obtain ⟨q, hq⟩ := h264 + have hq' : q * 2 ^ bits.val = 2 ^ 64 := by rw [Nat.mul_comm]; exact hq.symm + have hltq : p.toNat / 2 ^ bits.val < q := by + by_contra hge; push_neg at hge + have hmul : q * 2 ^ bits.val ≤ (p.toNat / 2 ^ bits.val) * 2 ^ bits.val := Nat.mul_le_mul_right _ hge + omega + have hstep : (p.toNat / 2 ^ bits.val + 1) * 2 ^ bits.val ≤ q * 2 ^ bits.val := Nat.mul_le_mul_right _ hltq + rw [Nat.add_mul, Nat.one_mul, ← hp_div, hq'] at hstep; exact hstep + omega + have hmv : (Fl4SpecA64.mask bits.val : Word).toNat = 2 ^ bits.val - 1 := by + cases sz <;> simp [capability.FrameSize.bits] at hbits <;> subst hbits <;> decide + apply BitVec.eq_of_getLsbD_eq + intro j hj64 + simp only [BitVec.getLsbD_and, BitVec.getLsbD_not, hj64, decide_true, Bool.true_and] + have hmt : (Fl4SpecA64.mask bits.val : Word).getLsbD j = decide (j < bits.val) := by + change (Fl4SpecA64.mask bits.val : Word).toNat.testBit j = _ + rw [hmv, Nat.testBit_two_pow_sub_one] + rw [hmt] + by_cases hj : j < bits.val + · simp only [hj, decide_true, Bool.not_true, Bool.and_false] + symm; change p.toNat.testBit j = false + rw [hp_div, Nat.mul_comm, Nat.testBit_two_pow_mul]; simp [hj] + · simp only [hj, decide_false, Bool.not_false, Bool.and_true] + change (p + BitVec.ofNat 64 d).toNat.testBit j = p.toNat.testBit j + rw [BitVec.toNat_add, BitVec.toNat_ofNat, + Nat.mod_eq_of_lt (by omega : d < 2 ^ 64), Nat.mod_eq_of_lt hpd_lt, + hp_div, Nat.mul_comm, Nat.testBit_two_pow_mul_add _ hd_lt, Nat.testBit_two_pow_mul] + simp [hj, show bits.val ≤ j by omega] + rw [hmask_eq]; exact hdev_k + have hBfAl : ∀ bp ∈ Bf, ∀ j : U64, j.val < 128 → Aligned (bufW bp.1 j) := fun bp hbp j hj => + J.iplaced.e.balign (bufW bp.1 j) ⟨bp, hbp, j, hj, rfl⟩ + obtain ⟨xv, hkv, hvt, -⟩ := J.j.2.1.obj vc hvc + have hvcH : H vc = some (.other 12) := arm.vh vc hvc + have hvcFit := J.ent.heap.fits vc (.other 12) hvcH + have hvcAl := J.ent.heap.align vc (.other 12) hvcH + simp only [HKind.bits] at hvcFit hvcAl + have hSnap0 : ∀ i < 64, (L.mem s (vcpuRegWA vc i)).bv = fl4VcpuRegGet xv i := + (R6.snap.snap vc hvc xv hkv).1 + have hVcOff : ∀ i < 64, Aligned (vcpuRegWA vc i) ∧ ¬ FrameWinW frame (vcpuRegWA vc i) := fun i hi => by + have ho : (⟨BitVec.ofNat 64 (64 + 8 * i)⟩ : U64).val = 64 + 8 * i := by + show (BitVec.ofNat 64 _).toNat = _; rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + have ev : (vcpuRegWA vc i).val = vc.val + (64 + 8 * i) := by + unfold vcpuRegWA vcpuRegOffA; rw [wa_valm, ho]; exact Nat.mod_eq_of_lt (by omega) + refine ⟨by show (vcpuRegWA vc i).val % 8 = 0; rw [ev]; omega, fun hfw => + vcub (vcpuRegWA vc i) ⟨vc, hvc, ⟨BitVec.ofNat 64 (64 + 8 * i)⟩, by omega, by omega, rfl⟩ (hFwUb _ hfw)⟩ + have hVcHdrOff : ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → Aligned (wa vc o) ∧ ¬ FrameWinW frame (wa vc o) := + fun o ho1 ho8 => by + have ev : (wa vc o).val = vc.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + refine ⟨by show (wa vc o).val % 8 = 0; rw [ev]; omega, fun hfw => + vcub (wa vc o) ⟨vc, hvc, o, ho1, ho8, rfl⟩ (hFwUb _ hfw)⟩ + have hTcb : L.mem s vc ≠ 0#u64 → ∃ xt, σ.kheap (specPtr (L.mem s vc)) = some (.tcb xt) ∧ + (∀ i < 37, (L.mem s (wa (wa (L.mem s vc) layout.tcb.REGS) ⟨BitVec.ofNat 64 (8 * i)⟩)).bv = + xt.tcbArch.tcbContext.userRegs (idxRegA i) ∧ + Aligned (wa (wa (L.mem s vc) layout.tcb.REGS) ⟨BitVec.ofNat 64 (8 * i)⟩) ∧ + ¬ FrameWinW frame (wa (wa (L.mem s vc) layout.tcb.REGS) ⟨BitVec.ofNat 64 (8 * i)⟩)) := by + intro ht0 + have htT : L.mem s vc ∈ Z.T := (J.vcRun.vtcb vc hvc).resolve_left ht0 + obtain ⟨xt, hkt, hrel⟩ := J.ep.tcb (L.mem s vc) htT + have htH : H (L.mem s vc) = some .tcb := show L.mem s vc ∈ H.tcbs by rw [J.tcbs]; exact htT + have htFit := J.ent.heap.fits _ .tcb htH; change (L.mem s vc).val + 4096 ≤ 2 ^ 64 at htFit + have htAl := J.ent.heap.align _ .tcb htH; change (L.mem s vc).val % 4096 = 0 at htAl + have hne_tp : L.mem s vc ≠ ptr := fun e => by rw [e, hpH] at htH; cases htH + have hdisj := J.ent.heap.disj _ .tcb ptr (.other bits.val) htH hpH hne_tp + change (L.mem s vc).val + 4096 ≤ ptr.val ∨ ptr.val + 2 ^ bits.val ≤ (L.mem s vc).val at hdisj + refine ⟨xt, hkt, fun i hi => ?_⟩ + have h256 : layout.tcb.REGS.val = 256 := by unfold layout.tcb.REGS; rfl + have h8i : (⟨BitVec.ofNat 64 (8 * i)⟩ : U64).val = 8 * i := by + show (BitVec.ofNat 64 _).toNat = _; rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + have e1 : (wa (L.mem s vc) layout.tcb.REGS).val = (L.mem s vc).val + 256 := by + rw [wa_valm, h256]; exact Nat.mod_eq_of_lt (by omega) + have ev : (wa (wa (L.mem s vc) layout.tcb.REGS) ⟨BitVec.ofNat 64 (8 * i)⟩).val = (L.mem s vc).val + (256 + 8 * i) := by + rw [wa_valm, e1, h8i, Nat.mod_eq_of_lt (by omega)]; omega + have hreg_eq : (L.mem s (wa (wa (L.mem s vc) layout.tcb.REGS) ⟨BitVec.ofNat 64 (8 * i)⟩)).bv = + xt.tcbArch.tcbContext.userRegs (idxRegA i) := by + have h1 := hrel.regs (idxRegA i) + have hri := regIdxA_idxRegR i hi + unfold Endp.regA at h1 + have hmul : core.num.U64.wrapping_mul (regIdxA (idxRegA i)) 8#u64 = ⟨BitVec.ofNat 64 (8 * i)⟩ := by + rw [hri]; apply UScalar.eq_of_val_eq + have h8 : (8#u64 : U64).val = 8 := rfl + have hi_v : (⟨BitVec.ofNat 64 i⟩ : U64).val = i := by + show (BitVec.ofNat 64 i).toNat = i; rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + rw [core.num.U64.wrapping_mul_val_eq, hi_v, h8] + show (i * 8) % UScalar.size UScalarTy.U64 = (BitVec.ofNat 64 (8 * i)).toNat + rw [BitVec.toNat_ofNat] + have hs : UScalar.size UScalarTy.U64 = 2 ^ 64 := by simp [U64.size_def, U64.numBits] + rw [hs, Nat.mod_eq_of_lt (by omega), Nat.mod_eq_of_lt (by omega)]; omega + rw [hmul] at h1; exact h1 + refine ⟨hreg_eq, by show _ % 8 = 0; rw [ev]; omega, fun ⟨k, hk, heq⟩ => ?_⟩ + have hv2 := congrArg UScalar.val heq + rw [ev, frame_word_valA hfit hk] at hv2; omega + have hBfSpec := R6.bfSpec + -- Lift final `ReadInvA` to `JStage3VcpuCoreA` in one shot via `JStage3VcpuCoreA.of_vcpuStepA`. + have hFinish : ∀ s' σ', + fl4ReadStateToFrame (specPtr vc) (specPtr frame) mask.bv σ = ({((), σ')}, False) → + ReadInvA (L := L) s σ frame Bf s' σ' → + ∃ σ' : Fl4SpecA64.State Unit, + fl4ReadStateToFrame (specPtr vc) (specPtr frame) mask.bv σ = ({((), σ')}, False) ∧ + JStage3VcpuCoreA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s' a σ' m ∧ + SchedValidA σ' ∧ + VcpuRec6A st Z OS Bf s' σ' ∧ + Clock a σ'.machineState ∧ + σ'.machineState.timeState = σ.machineState.timeState ∧ + σ'.machineState.lastMachineTime = σ.machineState.lastMachineTime ∧ + σ'.kheap = σ.kheap ∧ + L.ksb s' = L.ksb s ∧ L.cpu s' = L.cpu s ∧ + OSKeep st OS s s' ∧ + WritesIn st s s' (VcpuWrNoStA st Z OS s) ∧ + VcpuSpecStepA H σ σ' ∧ + (∀ v ∈ Z.V, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → L.mem s' (wa v o) = L.mem s (wa v o)) ∧ + (∀ u ∈ Z.T, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → L.mem s' (wa u o) = L.mem s (wa u o)) ∧ + FrKeepA st OS s σ s' σ' := by + intro s' σ' hsp I' + have hw : WritesIn st s s' (VcpuWrNoStA st Z OS s) := I'.wr.mono hFwWr + have hHS : VcpuSpecStepA H σ σ' := by + have heq := I'.eqUm + refine ⟨by rw [heq]; rfl, by rw [heq]; rfl, by rw [heq]; rfl, by rw [heq]; rfl, + by rw [heq]; rfl, by rw [heq]; rfl, by rw [heq]; rfl, by rw [heq]; rfl, + by rw [heq]; rfl, by rw [heq]; rfl, by rw [heq]; rfl, by rw [heq]; rfl, + by rw [heq]; rfl, by rw [heq]; rfl, by rw [heq]; rfl, by rw [heq]; rfl, + by rw [heq]; rfl, by rw [heq]; rfl, by rw [heq]; rfl, by rw [heq]; rfl, + by rw [heq]; rfl, by rw [heq]; rfl, by rw [heq]; rfl, fun p => Or.inl (by rw [heq]; rfl), fun p hp => ?_⟩ + by_cases hin : ∃ k < 128, frame.val + 8 * k ≤ (fl4Ptr p).val ∧ + (fl4Ptr p).val < frame.val + 8 * k + 8 + · obtain ⟨k, hk, h1, h2⟩ := hin + exact ⟨ptr, bits.val, hpH, by omega, by omega⟩ + · exact absurd (I'.umOff p (fun k hk h => hin ⟨k, hk, h⟩)) hp + have osk := osKeep_of_vcpuWrCoreA J R6.vcOS R6.frOff hw + have capE : ∀ u ∈ OS, capA st s' u = capA st s u := fun u hu => by + unfold capA; rw [(osk u hu).1, (osk u hu).2.1] + have hVcEq : ∀ v ∈ Z.V, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → L.mem s' (wa v o) = L.mem s (wa v o) := by + intro v hv o ho1 ho8 + have hvH : H v = some (.other 12) := arm.vh v hv + have hvf := J.ent.heap.fits v _ hvH; have hva := J.ent.heap.align v _ hvH + simp only [HKind.bits] at hvf hva + have ev : (wa v o).val = v.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact I'.wr.mem_eq (by show _ % 8 = 0; rw [ev]; omega) fun hfw => + vcub (wa v o) ⟨v, hv, o, ho1, ho8, rfl⟩ (hFwUb _ hfw) + have hTcbEq : ∀ u ∈ Z.T, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → L.mem s' (wa u o) = L.mem s (wa u o) := by + intro u hu o ho1 ho8 + have huH : H u = some .tcb := show u ∈ H.tcbs by rw [J.tcbs]; exact hu + have huf' := J.ent.heap.fits u .tcb huH; change u.val + 4096 ≤ 2 ^ 64 at huf' + have hua' := J.ent.heap.align u .tcb huH; change u.val % 4096 = 0 at hua' + have hne_up : u ≠ ptr := fun e => by rw [e, hpH] at huH; cases huH + have hd := J.ent.heap.disj u .tcb ptr (.other bits.val) huH hpH hne_up + change u.val + 4096 ≤ ptr.val ∨ ptr.val + 2 ^ bits.val ≤ u.val at hd + have ev : (wa u o).val = u.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + exact I'.wr.mem_eq (by show _ % 8 = 0; rw [ev]; omega) fun ⟨k, hk, heq⟩ => by + have hv2 := congrArg UScalar.val heq; rw [ev, frame_word_valA hfit hk] at hv2; omega + have hwa0 : ∀ x : U64, wa x 0#u64 = x := fun x => by + apply UScalar.eq_of_val_eq + have h0v : (0#u64 : U64).val = 0 := rfl + rw [wa_valm, h0v, Nat.add_zero]; exact Nat.mod_eq_of_lt x.hBounds + have hVcCore : VcpuCoreA (L := L) Z s' σ' := by + refine ⟨fun v hv => ?_, ?_, fun t ht => ?_, fun y hy v hc => ?_⟩ + · obtain ⟨x0, hk0, hvt0, hrc0⟩ := J.j.2.1.obj v hv + have h0 := hVcEq v hv 0#u64 (by change 0 < 4096; omega) (by change 0 % 8 = 0; omega) + rw [hwa0] at h0 + have h24 := hVcEq v hv 24#u64 (by change 24 < 4096; omega) (by change 24 % 8 = 0; omega) + exact ⟨x0, by rw [I'.kheap]; exact hk0, by rw [h0]; exact hvt0, by rw [h24]; exact hrc0⟩ + · intro p x hxp; exact J.j.2.1.back p x (by rwa [I'.kheap] at hxp) + · obtain ⟨xt, hkt, hv0, hr0⟩ := J.j.2.1.tcb t ht + have h3664 := hTcbEq t ht 3664#u64 (by change 3664 < 4096; omega) (by change 3664 % 8 = 0; omega) + have h3672 := hTcbEq t ht 3672#u64 (by change 3672 < 4096; omega) (by change 3672 % 8 = 0; omega) + exact ⟨xt, by rw [I'.kheap]; exact hkt, by rw [h3664]; exact hv0, by unfold runVcpuA; rw [h3672]; exact hr0⟩ + · have hyOS : y ∈ OS := J.j5.2.1 y hy + have hc0 : capAtA (L.mem s) y = .Vcpu v := by + rw [capAtA_eq, ← capE y hyOS, ← capAtA_eq]; exact hc + exact J.j.2.1.cap y hy v hc0 + have hVcRun : VcRunA st Z.T Z.V s' := by + refine ⟨fun t ht => by + rw [hTcbEq t ht 3664#u64 (by change 3664 < 4096; omega) (by change 3664 % 8 = 0; omega)] + exact J.vcRun.tcb t ht, + fun v hv => by + rw [hVcEq v hv 24#u64 (by change 24 < 4096; omega) (by change 24 % 8 = 0; omega)] + exact J.vcRun.vcpu v hv, + fun v hv => by + have h0 := hVcEq v hv 0#u64 (by change 0 < 4096; omega) (by change 0 % 8 = 0; omega) + rw [hwa0] at h0; rw [h0]; exact J.vcRun.vtcb v hv, + fun t ht => by + unfold runVcpuA + rw [hTcbEq t ht 3672#u64 (by change 3672 < 4096; omega) (by change 3672 % 8 = 0; omega)] + exact J.vcRun.run t ht, + fun t ht h => by + rw [hTcbEq t ht 3664#u64 (by change 3664 < 4096; omega) (by change 3664 % 8 = 0; omega)] at h + unfold runVcpuA + rw [hTcbEq t ht 3672#u64 (by change 3672 < 4096; omega) (by change 3672 % 8 = 0; omega)] + exact J.vcRun.disj t ht h⟩ + have hRunVcpu : RunVcpuInv st Z.T s' := fun t ht => by + have hst := hTcbEq t ht 3328#u64 (by change 3328 < 4096; omega) (by change 3328 % 8 = 0; omega) + have hrv := hTcbEq t ht 3672#u64 (by change 3672 < 4096; omega) (by change 3672 % 8 = 0; omega) + intro h + unfold runVcpuA at h; rw [hrv] at h + have h0 := J.runVcpu t ht h + rw [stA_eq] at h0 ⊢; rwa [hst] + have hEpTcb : ∀ u ∈ Z.T, ∃ x, σ'.kheap (specPtr u) = some (.tcb x) ∧ TcbRelE Rp (L.mem s') u x := by + intro u hu + obtain ⟨x, hku, hrel⟩ := J.ep.tcb u hu + have huH : H u = some .tcb := show u ∈ H.tcbs by rw [J.tcbs]; exact hu + have huf' := J.ent.heap.fits u .tcb huH; change u.val + 4096 ≤ 2 ^ 64 at huf' + have hne_up : u ≠ ptr := fun e => by rw [e, hpH] at huH; cases huH + have hd := J.ent.heap.disj u .tcb ptr (.other bits.val) huH hpH hne_up + change u.val + 4096 ≤ ptr.val ∨ ptr.val + 2 ^ bits.val ≤ u.val at hd + refine ⟨x, by rw [I'.kheap]; exact hku, hrel.frame (J.sq.tcbs u huH) fun a h8 ha => ?_⟩ + have hlo := ha.1; have hhi := ha.2; unfold TOffN at hhi + exact I'.wr.mem_eq h8 fun ⟨k, hk, heq⟩ => by + have hv2 := congrArg UScalar.val heq; rw [frame_word_valA hfit hk] at hv2; omega + have hDeadVcpu : ∀ v ∈ Z.V, DeadA st OS .vcpu s' v → ∀ x, σ'.kheap (specPtr v) = some (.archObj (.vcpu x)) → + x.vcpuTcb = none := fun v hv hd x hx => + J.deadExt.vcpu v hv (fun u hu => by rw [← capE u hu]; exact hd u hu) x (by rwa [I'.kheap] at hx) + have hDeadTvcpu : ∀ t ∈ Z.T, DeadA st OS .tcb s' t → L.mem s' (wa t 3664#u64) = 0#u64 := fun t ht hd => by + rw [hTcbEq t ht 3664#u64 (by change 3664 < 4096; omega) (by change 3664 % 8 = 0; omega)] + exact J.deadExt.tvcpu t ht (fun u hu => by rw [← capE u hu]; exact hd u hu) + have hDeadVcRun : ∀ v ∈ Z.V, DeadA st OS .vcpu s' v → L.mem s' (wa v 24#u64) = 0#u64 := fun v hv hd => by + rw [hVcEq v hv 24#u64 (by change 24 < 4096; omega) (by change 24 % 8 = 0; omega)] + exact J.deadExt.vcDeadRun v hv (fun u hu => by rw [← capE u hu]; exact hd u hu) + have hZombNoVc : ZombieTcbNoVcpuA st OS s' := fun z hz u num hzcap => by + have hz0 : capA st s z = .Zombie u capability.ZOMBIE_TCB num := by rw [← capE z hz]; exact hzcap + have huT : u ∈ Z.T := by rw [← J.tcbs]; exact J.zombieTcb z hz u num hz0 + rw [hTcbEq u huT 3664#u64 (by change 3664 < 4096; omega) (by change 3664 % 8 = 0; omega)] + exact J.zombieNoVcpu z hz u num hz0 + have J' := JStage3VcpuCoreA.of_vcpuStepA J R6 hw hHS J.j.1.ar + (fun b hb => hw.mem_eq (J.vsOk.al b hb) (utVs_off_vcpuWrCoreA J hb)) + hVcCore hVcRun hRunVcpu hEpTcb I'.buf hDeadVcpu hDeadTvcpu hDeadVcRun hZombNoVc + have hSV' := SchedValidA.of_vcpuStepA hSV hHS + have hSnap' := VcpuStateSnapA.of_vcpuStepCoreA J R6.snap hw hHS fun v hv => by + rw [hVcEq v hv 8#u64 (by change 8 < 4096; omega) (by change 8 % 8 = 0; omega)] + obtain ⟨x, hkx, -⟩ := J.j.2.1.obj v hv + exact (R6.snap.snap v hv x hkx).2.2.2.2 + have hFrOff' : FrOff st Z.Cs OS (L.ksb s') s' := by + rw [hw.1] + intro u hu p0 sz0 r0 mp0 ept0 a0 va0 hc0 b0 hb0 + exact R6.frOff u hu p0 sz0 r0 mp0 ept0 a0 va0 ((capE u hu).symm.trans hc0) b0 hb0 + have hFrTh' : FrThA st Z.th OS s' := R6.frTh.keepA capE fun _ _ e => e + have hUserFr' : UserFrSpecA st OS s' σ' := + R6.userFr.keepA capE fun _ _ _ h => hHS.keepObj h (fun _ e => by cases e) (fun _ e => by cases e) + have hR6' : VcpuRec6A st Z OS Bf s' σ' := by + refine ⟨hSnap', fun u hu => ?_, fun v hv => ?_, fun v hv hv0 => ?_, fun u hu hu0 => ?_, + R6.vcOS, hFrOff', hUserFr', hFrTh', R6.bfSpec⟩ + · rw [hTcbEq u hu 3664#u64 (by change 3664 < 4096; omega) (by change 3664 % 8 = 0; omega)] + intro hne + have hvV := (J.vcRun.tcb u hu).resolve_left hne + have h0 := hVcEq _ hvV 0#u64 (by change 0 < 4096; omega) (by change 0 % 8 = 0; omega) + rw [hwa0] at h0; rw [h0]; exact R6.vcSym1 u hu hne + · rw [hVcEq v hv 24#u64 (by change 24 < 4096; omega) (by change 24 % 8 = 0; omega)] + intro hne + have huT := (J.vcRun.vcpu v hv).resolve_left hne + unfold runVcpuA + rw [hTcbEq _ huT 3672#u64 (by change 3672 < 4096; omega) (by change 3672 % 8 = 0; omega)] + exact R6.vcSym2 v hv hne + · have h0 := hVcEq v hv 0#u64 (by change 0 < 4096; omega) (by change 0 % 8 = 0; omega) + rw [hwa0] at h0; rw [h0] at hv0 ⊢ + have huT := (J.vcRun.vtcb v hv).resolve_left hv0 + rw [hTcbEq _ huT 3664#u64 (by change 3664 < 4096; omega) (by change 3664 % 8 = 0; omega)] + exact R6.vcBack v hv hv0 + · unfold runVcpuA at hu0 ⊢ + rw [hTcbEq u hu 3672#u64 (by change 3672 < 4096; omega) (by change 3672 % 8 = 0; omega)] at hu0 ⊢ + have hvV := (J.vcRun.run u hu).resolve_left (by unfold runVcpuA; exact hu0) + unfold runVcpuA at hvV + rw [hVcEq _ hvV 24#u64 (by change 24 < 4096; omega) (by change 24 % 8 = 0; omega)] + exact R6.runBack u hu (by unfold runVcpuA; exact hu0) + have hts : σ'.machineState.timeState = σ.machineState.timeState := by rw [I'.eqUm]; rfl + have hlm : σ'.machineState.lastMachineTime = σ.machineState.lastMachineTime := by rw [I'.eqUm]; rfl + exact ⟨σ', hsp, J', hSV', hR6', hClFr a σ.machineState σ'.machineState hCl hts hlm, + hts, hlm, I'.kheap, hw.1, hw.2.1, osk, hw, hHS, hVcEq, hTcbEq, + capE, fun c cal _ h => I'.ag c cal h⟩ + -- Now run `vcpu.read_state_to_frame` under `ReadInvA`! + have I0 : ReadInvA (L := L) s σ frame Bf s σ := ReadInvA.refl J.ep.buf + refine WP.spec_mono (P₀ := fun s' => + ∃ σ', fl4ReadStateToFrame (specPtr vc) (specPtr frame) mask.bv σ = ({((), σ')}, False) ∧ + ReadInvA (L := L) s σ frame Bf s' σ') ?_ (fun s' ⟨σ', hsp, I'⟩ => hFinish s' σ' hsp I') + unfold vcpu.read_state_to_frame + by_cases hm0 : mask = 0#u64 + · rw [if_pos hm0, WP.spec_ok] + refine ⟨σ, ?_, I0⟩ + subst hm0 + unfold fl4ReadStateToFrame NonDet.when smEl1 smHyp smTimer smVgic smVgicLr smGprs + have hz : ∀ c : Word, (0 : Word) &&& c = 0 := fun _ => BitVec.zero_and + rw [show U64.bv 0#u64 = (0 : Word) from rfl] + simp only [hz, ne_eq, not_true_eq_false, ↓reduceIte] + rw [nd_bind_oneSc (m := NonDet.pure ()) rfl, nd_bind_oneSc (m := NonDet.pure ()) rfl, + nd_bind_oneSc (m := NonDet.pure ()) rfl, nd_bind_oneSc (m := NonDet.pure ()) rfl]; rfl + · rw [if_neg hm0] + -- Trailing GPRS block (shared across all 3 VGIC branches) + have hGprs_step : ∀ (sG : S) (σG : State Unit) (IG : ReadInvA (L := L) s σ frame Bf sG σG), + (do + let i13 ← fl4_abi.consts.vcpu_state_mask.GPRS + let i14 ← lift (mask &&& i13) + if i14 != 0#u64 then do + let i15 ← 64#u64 * 8#u64 + let gprs_base ← lift (wa frame i15) + let i16 ← lift (wa vc vcpu.TCB) + let t_ptr ← st.read sG i16 + vcpu.read_state_to_frame_loop2 st sG gprs_base t_ptr 0#u64 + else ok sG) ⦃ s' => + ∃ σ', (NonDet.when (mask.bv &&& smGprs ≠ 0) do + let v ← getVcpu (specPtr vc) + NonDet.mapMx (fun (i, r) => do + let w ← match v.vcpuTcb with | some t => asUser t (getRegister r) | none => pure 0 + storeWordOffs (specPtr frame) (64 + i) w) + (List.range fl4UserRegs.length |>.zip fl4UserRegs)) σG = ({((), σ')}, False) ∧ + ReadInvA (L := L) s σ frame Bf s' σ' ⦄ := by + intro sG σG IG + rw [sm_gprs_okR, bind_ok, lift_ok, bind_ok] + by_cases hg : (mask &&& 16#u64 != 0#u64) = true + · have hgb : mask.bv &&& smGprs ≠ 0 := fun e => by + have : mask &&& 16#u64 = 0#u64 := u64_eq_of_bv_eq e; simp [this] at hg + rw [if_pos hg, u64_mul_okR (by decide), bind_ok, lift_ok, bind_ok, lift_ok, bind_ok, L.read_eq, bind_ok] + have hwa0 : wa vc vcpu.TCB = vc := by + apply UScalar.eq_of_val_eq + have h0v : vcpu.TCB.val = 0 := by unfold vcpu.TCB; rfl + rw [wa_valm, h0v, Nat.add_zero]; exact Nat.mod_eq_of_lt vc.hBounds + have hmul512 : (⟨BitVec.ofNat 64 (64#u64.val * 8#u64.val)⟩ : U64) = 512#u64 := rfl + rw [hwa0, hmul512] + have hvc0_eq : L.mem sG vc = L.mem s vc := by + have h0 := hVcHdrOff 0#u64 (by change 0 < 4096; omega) (by change 0 % 8 = 0; omega) + have hwa0' : wa vc 0#u64 = vc := by + apply UScalar.eq_of_val_eq + have h0v : (0#u64 : U64).val = 0 := rfl + rw [wa_valm, h0v, Nat.add_zero]; exact Nat.mod_eq_of_lt vc.hBounds + rw [hwa0'] at h0; exact IG.wr.mem_eq h0.1 h0.2 + rw [hvc0_eq] + have hkvG : σG.kheap (specPtr vc) = some (.archObj (.vcpu xv)) := by rw [IG.kheap]; exact hkv + refine WP.spec_mono (read_gprs_loop_invA IG hfit hal hBfAl hBfSpec huf hvt hTcb) ?_ + rintro s' ⟨σ', hsp', I'⟩ + refine ⟨σ', ?_, I'⟩ + unfold NonDet.when; rw [if_pos hgb, nd_bind_oneSc (getVcpu_eqA hkvG)]; exact hsp' + · have hgb : ¬ (mask.bv &&& smGprs ≠ 0) := fun hne => hg (bne_iff_ne.2 (fun e => hne (congrArg UScalar.bv e))) + rw [if_neg hg, WP.spec_ok] + exact ⟨σG, by unfold NonDet.when; rw [if_neg hgb]; rfl, IG⟩ + -- Step 1: EL1_SYSREGS + have hStep1 : (do + let i ← fl4_abi.consts.vcpu_state_mask.EL1_SYSREGS + let i1 ← lift (mask &&& i) + if i1 != 0#u64 then do + let s2 ← vcpu.copy_reg_range_to_frame st s vc frame 0#u64 fl4_abi.consts.vcpu_reg.CSSELR_EL1 + vcpu.copy_reg_range_to_frame st s2 vc frame fl4_abi.consts.vcpu_reg.ACTLR_EL1 fl4_abi.consts.vcpu_reg.MDCCINT_EL1 + else ok s) ⦃ s1 => + ∃ σ1, (NonDet.when (mask.bv &&& smEl1 ≠ 0) do + copyRegsToFrame (specPtr vc) (specPtr frame) (idxRange 0 vrCsselr) + copyRegsToFrame (specPtr vc) (specPtr frame) (idxRange vrActlr vrMdccint)) σ = ({((), σ1)}, False) ∧ + ReadInvA (L := L) s σ frame Bf s1 σ1 ⦄ := by + rw [sm_el1_okR, bind_ok, lift_ok, bind_ok] + by_cases h1 : (mask &&& 1#u64 != 0#u64) = true + · have h1b : mask.bv &&& smEl1 ≠ 0 := fun e => by + have : mask &&& 1#u64 = 0#u64 := u64_eq_of_bv_eq e; simp [this] at h1 + rw [if_pos h1] + have hcs : fl4_abi.consts.vcpu_reg.CSSELR_EL1.val = vrCsselr := by unfold fl4_abi.consts.vcpu_reg.CSSELR_EL1 vrCsselr; rfl + have hact : fl4_abi.consts.vcpu_reg.ACTLR_EL1.val = vrActlr := by unfold fl4_abi.consts.vcpu_reg.ACTLR_EL1 vrActlr; rfl + have hmdc : fl4_abi.consts.vcpu_reg.MDCCINT_EL1.val = vrMdccint := by unfold fl4_abi.consts.vcpu_reg.MDCCINT_EL1 vrMdccint; rfl + refine WP.spec_bind (copy_reg_range_invA I0 hfit hal hBfAl hBfSpec huf hkv hSnap0 hVcOff + (by rw [hcs]; unfold vrCsselr; decide) (by rw [hcs]; unfold vrCsselr; decide)) ?_ + rintro s2 ⟨σ2, hsp2, I2⟩ + refine WP.spec_mono (copy_reg_range_invA I2 hfit hal hBfAl hBfSpec huf hkv hSnap0 hVcOff + (by rw [hact, hmdc]; unfold vrActlr vrMdccint; decide) (by rw [hmdc]; unfold vrMdccint; decide)) ?_ + rintro s1 ⟨σ1, hsp1, I1⟩ + refine ⟨σ1, ?_, I1⟩ + unfold NonDet.when; rw [if_pos h1b] + rw [show (0#u64 : U64).val = 0 from rfl, hcs] at hsp2; rw [hact, hmdc] at hsp1 + rw [nd_bind_oneSc hsp2]; exact hsp1 + · have h1b : ¬ (mask.bv &&& smEl1 ≠ 0) := fun hne => h1 (bne_iff_ne.2 (fun e => hne (congrArg UScalar.bv e))) + rw [if_neg h1, WP.spec_ok] + exact ⟨σ, by unfold NonDet.when; rw [if_neg h1b]; rfl, I0⟩ + rw [sm_el1_okR, bind_ok, lift_ok, bind_ok] at hStep1 ⊢ + refine WP.spec_bind hStep1 ?_ + rintro s1 ⟨σ1, hsp1, I1⟩ + -- Step 2: HYP + have hStep2 : (do + let i2 ← fl4_abi.consts.vcpu_state_mask.HYP + let i3 ← lift (mask &&& i2) + if i3 != 0#u64 then do + let i4 ← fl4_abi.consts.vcpu_reg.VMPIDR_EL2 * 8#u64 + let i5 ← lift (wa frame i4) + let i6 ← vcpu.reg_get st s1 vc fl4_abi.consts.vcpu_reg.VMPIDR_EL2 + let s3 ← st.write s1 i5 i6 + let i7 ← fl4_abi.consts.vcpu_reg.VDISR_EL2 * 8#u64 + let i8 ← lift (wa frame i7) + let i9 ← vcpu.reg_get st s3 vc fl4_abi.consts.vcpu_reg.VDISR_EL2 + let s4 ← st.write s3 i8 i9 + let s5 ← vcpu.copy_reg_range_to_frame st s4 vc frame fl4_abi.consts.vcpu_reg.VPIDR_EL2 fl4_abi.consts.vcpu_reg.VSESR_EL2 + vcpu.copy_reg_range_to_frame st s5 vc frame fl4_abi.consts.vcpu_reg.FAR_EL2 fl4_abi.consts.vcpu_reg.ESR_EL2 + else ok s1) ⦃ s2 => + ∃ σ2, (NonDet.when (mask.bv &&& smHyp ≠ 0) do + copyRegsToFrame (specPtr vc) (specPtr frame) [vrVmpidr, vrVdisr] + copyRegsToFrame (specPtr vc) (specPtr frame) (idxRange vrVpidr vrVsesr) + copyRegsToFrame (specPtr vc) (specPtr frame) (idxRange vrFarEl2 vrEsrEl2)) σ1 = ({((), σ2)}, False) ∧ + ReadInvA (L := L) s σ frame Bf s2 σ2 ⦄ := by + rw [sm_hyp_okR, bind_ok, lift_ok, bind_ok] + by_cases h2 : (mask &&& 2#u64 != 0#u64) = true + · have h2b : mask.bv &&& smHyp ≠ 0 := fun e => by + have : mask &&& 2#u64 = 0#u64 := u64_eq_of_bv_eq e; simp [this] at h2 + rw [if_pos h2] + have hvmp : fl4_abi.consts.vcpu_reg.VMPIDR_EL2.val = vrVmpidr := by unfold fl4_abi.consts.vcpu_reg.VMPIDR_EL2 vrVmpidr; rfl + have hvdi : fl4_abi.consts.vcpu_reg.VDISR_EL2.val = vrVdisr := by unfold fl4_abi.consts.vcpu_reg.VDISR_EL2 vrVdisr; rfl + have hvpi : fl4_abi.consts.vcpu_reg.VPIDR_EL2.val = vrVpidr := by unfold fl4_abi.consts.vcpu_reg.VPIDR_EL2 vrVpidr; rfl + have hvse : fl4_abi.consts.vcpu_reg.VSESR_EL2.val = vrVsesr := by unfold fl4_abi.consts.vcpu_reg.VSESR_EL2 vrVsesr; rfl + have hfar : fl4_abi.consts.vcpu_reg.FAR_EL2.val = vrFarEl2 := by unfold fl4_abi.consts.vcpu_reg.FAR_EL2 vrFarEl2; rfl + have hesr : fl4_abi.consts.vcpu_reg.ESR_EL2.val = vrEsrEl2 := by unfold fl4_abi.consts.vcpu_reg.ESR_EL2 vrEsrEl2; rfl + rw [u64_mul_okR (by rw [hvmp]; unfold vrVmpidr; decide), bind_ok, lift_ok, bind_ok, + vcpu_reg_get_u64_eqR s1 vc _ (by rw [hvmp]; unfold vrVmpidr; decide), bind_ok, L.write_eq, bind_ok] + have hm1 : (⟨BitVec.ofNat 64 (fl4_abi.consts.vcpu_reg.VMPIDR_EL2.val * 8#u64.val)⟩ : U64) = + ⟨BitVec.ofNat 64 (8 * vrVmpidr)⟩ := by congr 1; rw [hvmp]; unfold vrVmpidr; rfl + rw [hm1, hvmp] + obtain ⟨hst_vmp, Is3⟩ := I1.step hfit hal hBfAl hBfSpec huf (k := vrVmpidr) (by unfold vrVmpidr; decide) + (L.mem s1 (vcpuRegWA vc vrVmpidr)) + set s3 := L.wr s1 (wa frame ⟨BitVec.ofNat 64 (8 * vrVmpidr)⟩) (L.mem s1 (vcpuRegWA vc vrVmpidr)) + set σs3 := setUm σ1 (storeMem σ1.machineState.underlyingMemory (bufAddr (specPtr frame) vrVmpidr) + (L.mem s1 (vcpuRegWA vc vrVmpidr)).bv) + rw [u64_mul_okR (by rw [hvdi]; unfold vrVdisr; decide), bind_ok, lift_ok, bind_ok, + vcpu_reg_get_u64_eqR s3 vc _ (by rw [hvdi]; unfold vrVdisr; decide), bind_ok, L.write_eq, bind_ok] + have hm2 : (⟨BitVec.ofNat 64 (fl4_abi.consts.vcpu_reg.VDISR_EL2.val * 8#u64.val)⟩ : U64) = + ⟨BitVec.ofNat 64 (8 * vrVdisr)⟩ := by congr 1; rw [hvdi]; unfold vrVdisr; rfl + rw [hm2, hvdi] + obtain ⟨hst_vdi, Is4⟩ := Is3.step hfit hal hBfAl hBfSpec huf (k := vrVdisr) (by unfold vrVdisr; decide) + (L.mem s3 (vcpuRegWA vc vrVdisr)) + have hkv1 : σ1.kheap (specPtr vc) = some (.archObj (.vcpu xv)) := by rw [I1.kheap]; exact hkv + have hkv3 : σs3.kheap (specPtr vc) = some (.archObj (.vcpu xv)) := by rw [Is3.kheap]; exact hkv + have hreg_vmp : (L.mem s1 (vcpuRegWA vc vrVmpidr)).bv = fl4VcpuRegGet xv vrVmpidr := by + rw [I1.wr.mem_eq (hVcOff _ (by unfold vrVmpidr; decide)).1 (hVcOff _ (by unfold vrVmpidr; decide)).2] + exact hSnap0 _ (by unfold vrVmpidr; decide) + have hreg_vdi : (L.mem s3 (vcpuRegWA vc vrVdisr)).bv = fl4VcpuRegGet xv vrVdisr := by + rw [Is3.wr.mem_eq (hVcOff _ (by unfold vrVdisr; decide)).1 (hVcOff _ (by unfold vrVdisr; decide)).2] + exact hSnap0 _ (by unfold vrVdisr; decide) + have hpair_spec : copyRegsToFrame (specPtr vc) (specPtr frame) [vrVmpidr, vrVdisr] σ1 = + ({((), setUm σs3 (storeMem σs3.machineState.underlyingMemory (bufAddr (specPtr frame) vrVdisr) + (L.mem s3 (vcpuRegWA vc vrVdisr)).bv))}, False) := by + rw [copyRegsToFrame_consR _ _ vrVmpidr [vrVdisr] hkv1 + (frame_bufAddr_al3A hfit hal (by unfold vrVmpidr; decide)) + (inUserFrame_of_kheapR I1.kheap (huf vrVmpidr (by unfold vrVmpidr; decide))), ← hreg_vmp, + copyRegsToFrame_consR _ _ vrVdisr [] hkv3 + (frame_bufAddr_al3A hfit hal (by unfold vrVdisr; decide)) + (inUserFrame_of_kheapR Is3.kheap (huf vrVdisr (by unfold vrVdisr; decide))), ← hreg_vdi] + simp [copyRegsToFrame, NonDet.mapMx]; rfl + refine WP.spec_bind (copy_reg_range_invA Is4 hfit hal hBfAl hBfSpec huf hkv hSnap0 hVcOff + (by rw [hvpi, hvse]; unfold vrVpidr vrVsesr; decide) (by rw [hvse]; unfold vrVsesr; decide)) ?_ + rintro s5 ⟨σ5, hsp5, I5⟩ + refine WP.spec_mono (copy_reg_range_invA I5 hfit hal hBfAl hBfSpec huf hkv hSnap0 hVcOff + (by rw [hfar, hesr]; unfold vrFarEl2 vrEsrEl2; decide) (by rw [hesr]; unfold vrEsrEl2; decide)) ?_ + rintro s2 ⟨σ2, hsp2, I2⟩ + refine ⟨σ2, ?_, I2⟩ + unfold NonDet.when; rw [if_pos h2b, nd_bind_oneSc hpair_spec] + rw [hvpi, hvse] at hsp5; rw [hfar, hesr] at hsp2 + rw [nd_bind_oneSc hsp5]; exact hsp2 + · have h2b : ¬ (mask.bv &&& smHyp ≠ 0) := fun hne => h2 (bne_iff_ne.2 (fun e => hne (congrArg UScalar.bv e))) + rw [if_neg h2, WP.spec_ok] + exact ⟨σ1, by unfold NonDet.when; rw [if_neg h2b]; rfl, I1⟩ + rw [sm_hyp_okR, bind_ok, lift_ok, bind_ok] at hStep2 ⊢ + refine WP.spec_bind hStep2 ?_ + rintro s2 ⟨σ2, hsp2, I2⟩ + -- Step 3: TIMER + have hStep3 : (do + let i4 ← fl4_abi.consts.vcpu_state_mask.TIMER + let i5 ← lift (mask &&& i4) + if i5 != 0#u64 then do + let s4 ← vcpu.copy_reg_range_to_frame st s2 vc frame fl4_abi.consts.vcpu_reg.CNTV_CTL fl4_abi.consts.vcpu_reg.CNTKCTL_EL1 + let i6 ← 64#u64 + 37#u64 + let i7 ← i6 * 8#u64 + let vppi_addr ← lift (wa frame i7) + let i8 ← lift (wa vc vcpu.VPPI_MASKED) + let i9 ← st.read s4 i8 + st.write s4 vppi_addr i9 + else ok s2) ⦃ s3 => + ∃ σ3, (NonDet.when (mask.bv &&& smTimer ≠ 0) do + copyRegsToFrame (specPtr vc) (specPtr frame) (idxRange vrCntvCtl vrCntkctl) + let v ← getVcpu (specPtr vc) + storeWordOffs (specPtr frame) vppiWord (if v.vcpuVppiMasked .vppiEventIRQ_VTimer then 1 else 0)) σ2 = ({((), σ3)}, False) ∧ + ReadInvA (L := L) s σ frame Bf s3 σ3 ⦄ := by + rw [sm_timer_okR, bind_ok, lift_ok, bind_ok] + by_cases h3 : (mask &&& 4#u64 != 0#u64) = true + · have h3b : mask.bv &&& smTimer ≠ 0 := fun e => by + have : mask &&& 4#u64 = 0#u64 := u64_eq_of_bv_eq e; simp [this] at h3 + rw [if_pos h3] + have hctl : fl4_abi.consts.vcpu_reg.CNTV_CTL.val = vrCntvCtl := by unfold fl4_abi.consts.vcpu_reg.CNTV_CTL vrCntvCtl; rfl + have hkctl : fl4_abi.consts.vcpu_reg.CNTKCTL_EL1.val = vrCntkctl := by unfold fl4_abi.consts.vcpu_reg.CNTKCTL_EL1 vrCntkctl; rfl + refine WP.spec_bind (copy_reg_range_invA I2 hfit hal hBfAl hBfSpec huf hkv hSnap0 hVcOff + (by rw [hctl, hkctl]; unfold vrCntvCtl vrCntkctl; decide) (by rw [hkctl]; unfold vrCntkctl; decide)) ?_ + rintro s4 ⟨σ4, hsp4, I4⟩ + rw [u64_add_ok (by decide), bind_ok, u64_mul_okR (by decide), bind_ok, lift_ok, bind_ok, + lift_ok, bind_ok, L.read_eq, bind_ok, L.write_eq, WP.spec_ok] + have hvppi_off : wa vc vcpu.VPPI_MASKED = wa vc 16#u64 := by unfold vcpu.VPPI_MASKED; rfl + rw [hvppi_off] + have hvppi_eq : L.mem s4 (wa vc 16#u64) = L.mem s (wa vc 16#u64) := + I4.wr.mem_eq (hVcHdrOff 16#u64 (by change 16 < 4096; omega) (by change 16 % 8 = 0; omega)).1 + (hVcHdrOff 16#u64 (by change 16 < 4096; omega) (by change 16 % 8 = 0; omega)).2 + have hvppi_bv : (L.mem s4 (wa vc 16#u64)).bv = if xv.vcpuVppiMasked .vppiEventIRQ_VTimer then 1 else 0 := by + rw [hvppi_eq] + have h := (R6.snap.snap vc hvc xv hkv).2.1 + cases hv : xv.vcpuVppiMasked .vppiEventIRQ_VTimer <;> simp [hv] at h ⊢ <;> rw [h] <;> rfl + have hkv4 : σ4.kheap (specPtr vc) = some (.archObj (.vcpu xv)) := by rw [I4.kheap]; exact hkv + have hvp_mul : (⟨BitVec.ofNat 64 ((⟨BitVec.ofNat 64 (64#u64.val + 37#u64.val)⟩ : U64).val * 8#u64.val)⟩ : U64) = + ⟨BitVec.ofNat 64 (8 * vppiWord)⟩ := rfl + rw [hvp_mul] + obtain ⟨hst_vp, I3⟩ := I4.step hfit hal hBfAl hBfSpec huf (k := vppiWord) (by unfold vppiWord; decide) + (L.mem s4 (wa vc 16#u64)) + refine ⟨_, ?_, I3⟩ + unfold NonDet.when; rw [if_pos h3b] + rw [hctl, hkctl] at hsp4 + rw [nd_bind_oneSc hsp4, nd_bind_oneSc (getVcpu_eqA hkv4), ← hvppi_bv]; exact hst_vp + · have h3b : ¬ (mask.bv &&& smTimer ≠ 0) := fun hne => h3 (bne_iff_ne.2 (fun e => hne (congrArg UScalar.bv e))) + rw [if_neg h3, WP.spec_ok] + exact ⟨σ2, by unfold NonDet.when; rw [if_neg h3b]; rfl, I2⟩ + rw [sm_timer_okR, bind_ok, lift_ok, bind_ok] at hStep3 ⊢ + refine WP.spec_bind hStep3 ?_ + rintro s3 ⟨σ3, hsp3, I3⟩ + -- Step 4: VGIC / VGIC_LR / GPRS + rw [sm_vgic_okR, bind_ok, lift_ok, bind_ok] + by_cases h4 : (mask &&& 8#u64 != 0#u64) = true + · have h4b : mask.bv &&& smVgic ≠ 0 := fun e => by + have : mask &&& 8#u64 = 0#u64 := u64_eq_of_bv_eq e; simp [this] at h4 + rw [if_pos h4, vcpu_num_lrs_eqA hP a, bind_ok] + obtain ⟨g, hga, hgb⟩ := hP.gic a + rw [hga, bind_ok] + have hgvtr : fl4_abi.consts.vcpu_reg.VGIC_VTR.val = vrVgicVtr := by unfold fl4_abi.consts.vcpu_reg.VGIC_VTR vrVgicVtr; rfl + have hvhcr : fl4_abi.consts.vcpu_reg.VGIC_HCR.val = vrVgicHcr := by unfold fl4_abi.consts.vcpu_reg.VGIC_HCR vrVgicHcr; rfl + have hap03 : fl4_abi.consts.vcpu_reg.VGIC_AP0R3.val = vrVgicAp0r3 := by unfold fl4_abi.consts.vcpu_reg.VGIC_AP0R3 vrVgicAp0r3; rfl + have hdec : (decide (g = 3#u64)) = fl4GicV3 := hgb + rw [u64_mul_okR (by rw [hgvtr]; unfold vrVgicVtr; decide), bind_ok, lift_ok, bind_ok, + hP.vtr a, bind_ok, hdec, vcpu_abi_vgic_vtr_eqA hP a, bind_ok, L.write_eq, bind_ok] + have hm_vtr : (⟨BitVec.ofNat 64 (fl4_abi.consts.vcpu_reg.VGIC_VTR.val * 8#u64.val)⟩ : U64) = + ⟨BitVec.ofNat 64 (8 * vrVgicVtr)⟩ := by congr 1; rw [hgvtr]; unfold vrVgicVtr; rfl + rw [hm_vtr] + obtain ⟨hst_vtr, Is4⟩ := I3.step hfit hal hBfAl hBfSpec huf (k := vrVgicVtr) (by unfold vrVgicVtr; decide) ⟨abiVgicVtr⟩ + refine WP.spec_bind (copy_reg_range_invA Is4 hfit hal hBfAl hBfSpec huf hkv hSnap0 hVcOff + (by rw [hvhcr, hap03]; unfold vrVgicHcr vrVgicAp0r3; decide) (by rw [hap03]; unfold vrVgicAp0r3; decide)) ?_ + rintro s5 ⟨σ5, hsp5, I5⟩ + refine WP.spec_bind (read_ap1r_loop_invA I5 hfit hal hBfAl hBfSpec huf hkv hSnap0 hVcOff) ?_ + rintro s6 ⟨σ6, hsp6, I6⟩ + refine WP.spec_bind (read_lrs_loop_invA I6 hfit hal hBfAl hBfSpec huf hkv hSnap0 hVcOff) ?_ + rintro s7 ⟨σ7, hsp7, I7⟩ + refine WP.spec_mono (hGprs_step s7 σ7 I7) ?_ + rintro s' ⟨σ', hsp', I'⟩ + refine ⟨σ', ?_, I'⟩ + unfold fl4ReadStateToFrame + rw [nd_bind_oneSc hsp1, nd_bind_oneSc hsp2, nd_bind_oneSc hsp3, if_pos h4b, nd_bind_oneSc hst_vtr] + rw [hvhcr, hap03] at hsp5 + rw [nd_bind_oneSc hsp5, nd_bind_oneSc hsp6, nd_bind_oneSc hsp7]; exact hsp' + · have h4b : ¬ (mask.bv &&& smVgic ≠ 0) := fun hne => h4 (bne_iff_ne.2 (fun e => hne (congrArg UScalar.bv e))) + rw [if_neg h4, sm_vgic_lr_okR, bind_ok, lift_ok, bind_ok] + by_cases h5 : (mask &&& 32#u64 != 0#u64) = true + · have h5b : mask.bv &&& smVgicLr ≠ 0 := fun e => by + have : mask &&& 32#u64 = 0#u64 := u64_eq_of_bv_eq e; simp [this] at h5 + rw [if_pos h5, vcpu_num_lrs_eqA hP a, bind_ok] + have hvhcr : fl4_abi.consts.vcpu_reg.VGIC_HCR.val = vrVgicHcr := by unfold fl4_abi.consts.vcpu_reg.VGIC_HCR vrVgicHcr; rfl + have helrsr : fl4_abi.consts.vcpu_reg.VGIC_ELRSR.val = vrVgicElrsr := by unfold fl4_abi.consts.vcpu_reg.VGIC_ELRSR vrVgicElrsr; rfl + rw [u64_mul_okR (by rw [hvhcr]; unfold vrVgicHcr; decide), bind_ok, lift_ok, bind_ok, + vcpu_reg_get_u64_eqR s3 vc _ (by rw [hvhcr]; unfold vrVgicHcr; decide), bind_ok, L.write_eq, bind_ok] + have hm_hcr : (⟨BitVec.ofNat 64 (fl4_abi.consts.vcpu_reg.VGIC_HCR.val * 8#u64.val)⟩ : U64) = + ⟨BitVec.ofNat 64 (8 * vrVgicHcr)⟩ := by congr 1; rw [hvhcr]; unfold vrVgicHcr; rfl + rw [hm_hcr, hvhcr] + obtain ⟨hst_hcr, Is4⟩ := I3.step hfit hal hBfAl hBfSpec huf (k := vrVgicHcr) (by unfold vrVgicHcr; decide) + (L.mem s3 (vcpuRegWA vc vrVgicHcr)) + set s4 := L.wr s3 (wa frame ⟨BitVec.ofNat 64 (8 * vrVgicHcr)⟩) (L.mem s3 (vcpuRegWA vc vrVgicHcr)) + set σs4 := setUm σ3 (storeMem σ3.machineState.underlyingMemory (bufAddr (specPtr frame) vrVgicHcr) + (L.mem s3 (vcpuRegWA vc vrVgicHcr)).bv) + rw [u64_mul_okR (by rw [helrsr]; unfold vrVgicElrsr; decide), bind_ok, lift_ok, bind_ok, + vcpu_reg_get_u64_eqR s4 vc _ (by rw [helrsr]; unfold vrVgicElrsr; decide), bind_ok, L.write_eq, bind_ok] + have hm_elr : (⟨BitVec.ofNat 64 (fl4_abi.consts.vcpu_reg.VGIC_ELRSR.val * 8#u64.val)⟩ : U64) = + ⟨BitVec.ofNat 64 (8 * vrVgicElrsr)⟩ := by congr 1; rw [helrsr]; unfold vrVgicElrsr; rfl + rw [hm_elr, helrsr] + obtain ⟨hst_elr, Is5⟩ := Is4.step hfit hal hBfAl hBfSpec huf (k := vrVgicElrsr) (by unfold vrVgicElrsr; decide) + (L.mem s4 (vcpuRegWA vc vrVgicElrsr)) + have hkv3 : σ3.kheap (specPtr vc) = some (.archObj (.vcpu xv)) := by rw [I3.kheap]; exact hkv + have hkv4 : σs4.kheap (specPtr vc) = some (.archObj (.vcpu xv)) := by rw [Is4.kheap]; exact hkv + have hreg_hcr : (L.mem s3 (vcpuRegWA vc vrVgicHcr)).bv = fl4VcpuRegGet xv vrVgicHcr := by + rw [I3.wr.mem_eq (hVcOff _ (by unfold vrVgicHcr; decide)).1 (hVcOff _ (by unfold vrVgicHcr; decide)).2] + exact hSnap0 _ (by unfold vrVgicHcr; decide) + have hreg_elr : (L.mem s4 (vcpuRegWA vc vrVgicElrsr)).bv = fl4VcpuRegGet xv vrVgicElrsr := by + rw [Is4.wr.mem_eq (hVcOff _ (by unfold vrVgicElrsr; decide)).1 (hVcOff _ (by unfold vrVgicElrsr; decide)).2] + exact hSnap0 _ (by unfold vrVgicElrsr; decide) + have hpair_spec : copyRegsToFrame (specPtr vc) (specPtr frame) [vrVgicHcr, vrVgicElrsr] σ3 = + ({((), setUm σs4 (storeMem σs4.machineState.underlyingMemory (bufAddr (specPtr frame) vrVgicElrsr) + (L.mem s4 (vcpuRegWA vc vrVgicElrsr)).bv))}, False) := by + rw [copyRegsToFrame_consR _ _ vrVgicHcr [vrVgicElrsr] hkv3 + (frame_bufAddr_al3A hfit hal (by unfold vrVgicHcr; decide)) + (inUserFrame_of_kheapR I3.kheap (huf vrVgicHcr (by unfold vrVgicHcr; decide))), ← hreg_hcr, + copyRegsToFrame_consR _ _ vrVgicElrsr [] hkv4 + (frame_bufAddr_al3A hfit hal (by unfold vrVgicElrsr; decide)) + (inUserFrame_of_kheapR Is4.kheap (huf vrVgicElrsr (by unfold vrVgicElrsr; decide))), ← hreg_elr] + simp [copyRegsToFrame, NonDet.mapMx]; rfl + have hloop3 : vcpu.read_state_to_frame_loop3 st (L.wr s4 (wa frame ⟨BitVec.ofNat 64 (8 * vrVgicElrsr)⟩) + (L.mem s4 (vcpuRegWA vc vrVgicElrsr))) vc frame ⟨BitVec.ofNat 64 fl4NumLrs⟩ 0#u64 = + vcpu.read_state_to_frame_loop1 st (L.wr s4 (wa frame ⟨BitVec.ofNat 64 (8 * vrVgicElrsr)⟩) + (L.mem s4 (vcpuRegWA vc vrVgicElrsr))) vc frame ⟨BitVec.ofNat 64 fl4NumLrs⟩ 0#u64 := by + unfold vcpu.read_state_to_frame_loop3 vcpu.read_state_to_frame_loop1; congr 1 + rw [hloop3] + refine WP.spec_bind (read_lrs_loop_invA Is5 hfit hal hBfAl hBfSpec huf hkv hSnap0 hVcOff) ?_ + rintro s6 ⟨σ6, hsp6, I6⟩ + refine WP.spec_mono (hGprs_step s6 σ6 I6) ?_ + rintro s' ⟨σ', hsp', I'⟩ + refine ⟨σ', ?_, I'⟩ + unfold fl4ReadStateToFrame + rw [nd_bind_oneSc hsp1, nd_bind_oneSc hsp2, nd_bind_oneSc hsp3, if_neg h4b] + rw [nd_bind_oneSc (by unfold NonDet.when; rw [if_pos h5b, nd_bind_oneSc hpair_spec]; exact hsp6)] + exact hsp' + · have h5b : ¬ (mask.bv &&& smVgicLr ≠ 0) := fun hne => h5 (bne_iff_ne.2 (fun e => hne (congrArg UScalar.bv e))) + rw [if_neg h5] + refine WP.spec_mono (hGprs_step s3 σ3 I3) ?_ + rintro s' ⟨σ', hsp', I'⟩ + refine ⟨σ', ?_, I'⟩ + unfold fl4ReadStateToFrame + rw [nd_bind_oneSc hsp1, nd_bind_oneSc hsp2, nd_bind_oneSc hsp3, if_neg h4b] + rw [nd_bind_oneSc (by unfold NonDet.when; rw [if_neg h5b]; rfl)] + exact hsp' + +/-- **`vcpu.read_state_to_frame` refinement on `JStage3TA`**: when `frame = wa ptr offset` +lies inside a non-device `VM_READ_WRITE` frame cap in `OS` with `offset % 64 = 0` and +`offset + 1024 ≤ 2 ^ bits`, `vcpu.read_state_to_frame` refines `fl4ReadStateToFrame` and +preserves `JStage3TA`, `SchedValidA`, `VcpuRec6A`, `Clock`, spec time, `OSKeep`, +`WritesIn ... (VcpuWrNoStA ...)`, and `VcpuSpecStepA`. -/ +theorem vcpu_read_state_to_frame_stage3TA + {AR : A → HwRootA → Prop} {Clock : A → Fl4SpecA64.MachineState → Prop} + {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J7 : JSlotA S} + [J7PpA st J7] [J7PpKA st J7] [J7FrA st J7] + {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + (hSV : SchedValidA σ) + (R6 : VcpuRec6A st Z OS Bf s σ) + (hCl : Clock a σ.machineState) + (hClFr : ∀ a' ms ms', Clock a' ms → ms'.timeState = ms.timeState → + ms'.lastMachineTime = ms.lastMachineTime → Clock a' ms') + (hP : VcpuPlatAgreeA ar) + {vc : U64} (hvc : vc ∈ Z.V) + {y ptr : U64} {sz : capability.FrameSize} {mp ept : Bool} {asid va bits offset : U64} + (hy : y ∈ OS) + (hcap : capA st s y = .Frame ptr sz capability.VM_READ_WRITE false mp ept asid va) + (hbits : capability.FrameSize.bits sz = ok bits) + (hoff64 : offset &&& 63#u64 = 0#u64) + (hoff_lt : offset.val < 2 ^ bits.val) + (hoff_sz : 2 ^ bits.val - offset.val ≥ 1024) + (mask : U64) : + let frame := wa ptr offset + vcpu.read_state_to_frame st ar s a vc frame mask ⦃ s' => + ∃ σ' : Fl4SpecA64.State Unit, + fl4ReadStateToFrame (specPtr vc) (specPtr frame) mask.bv σ = ({((), σ')}, False) ∧ + JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s' a σ' m ∧ + SchedValidA σ' ∧ + VcpuRec6A st Z OS Bf s' σ' ∧ + Clock a σ'.machineState ∧ + σ'.machineState.timeState = σ.machineState.timeState ∧ + σ'.machineState.lastMachineTime = σ.machineState.lastMachineTime ∧ + σ'.kheap = σ.kheap ∧ + L.ksb s' = L.ksb s ∧ L.cpu s' = L.cpu s ∧ + OSKeep st OS s s' ∧ + WritesIn st s s' (VcpuWrNoStA st Z OS s) ∧ + VcpuSpecStepA H σ σ' ∧ + (∀ v ∈ Z.V, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → L.mem s' (wa v o) = L.mem s (wa v o)) ∧ + (∀ u ∈ Z.T, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → L.mem s' (wa u o) = L.mem s (wa u o)) ∧ + FrKeepA st OS s σ s' σ' ⦄ := by + refine WP.spec_mono (vcpu_read_state_to_frame_coreA J.toVcpuCoreA hSV R6 hCl hClFr hP hvc hy hcap hbits hoff64 hoff_lt hoff_sz mask) ?_ + rintro s' ⟨σ', hsp, J', hSV', R6', hCl', hts, hlm, hkh, hksb, hcpu, osk, hw, hHS, hvcE, htcbE, fk⟩ + exact ⟨σ', hsp, J'.toTA (J.sact.of_vcpuStepA hHS), hSV', R6', hCl', hts, hlm, hkh, hksb, hcpu, osk, hw, hHS, hvcE, htcbE, fk⟩ + +end read_stage3 + +end +end Fl4ProofsA64.P3.Vcpu diff --git a/proofs/Fl4ProofsA64/P3/VcpuDelSpecA.lean b/proofs/Fl4ProofsA64/P3/VcpuDelSpecA.lean new file mode 100644 index 000000000..75f790394 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/VcpuDelSpecA.lean @@ -0,0 +1,769 @@ +/- +P3 (aarch64, A64-P3 S4 VCPU): **Standalone pure-spec lemmas for `fl4VcpuDetach` and `fl4VcpuEndRun`**. + +Provides (matching `WS/handoff/s4-detach-stmt.lean` and `DelThrHookA`'s `hTd`/`hLink`): +- `fl4VcpuDetach_timeA`: `fl4VcpuDetach` keeps `machineState.timeState` and `machineState.lastMachineTime` + (covering all `armCurrentVcpu` branches via `VcpuDelCommA.dissociateVcpuTcb_mem_iffA`). +- `fl4VcpuEndRun_keeps_bindA`: `fl4CancelRun` followed by `fl4VcpuEndRun` keeps `t.tcbArch.tcbVcpu` + and `v.vcpuTcb`. + +No new named premise. +-/ +import Fl4ProofsA64.P3.VcpuDelCommA +import Fl4ProofsA64.P3.VcpuPremisesA + +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3.Vcpu + +open Fl4Spec Fl4SpecA64 Fl4ProofsA64.P3 Fl4ProofsA64.P3.Sched + +noncomputable section + +/-! ## Section 1: Reflexive-transitive preservation of time + VCPU↔TCB bindings -/ + +private theorem nd_bind_mem_iffA {σ α β : Type} {m : NonDet σ α} {f : α → NonDet σ β} + {s : σ} {b : β} {s' : σ} : + (b, s') ∈ ((m >>= f) s).1 ↔ ∃ a sm, (a, sm) ∈ (m s).1 ∧ (b, s') ∈ (f a sm).1 := by + show (b, s') ∈ (NonDet.bind m f s).1 ↔ _; unfold NonDet.bind; simp + +private theorem nd_bind2_mem_iffA {σ α β : Type} {m : NonDet σ α} {f : α → NonDet σ β} + {s : σ} {b : β} {s' : σ} : + (b, s') ∈ (NonDet.bind m f s).1 ↔ ∃ a sm, (a, sm) ∈ (m s).1 ∧ (b, s') ∈ (f a sm).1 := by + unfold NonDet.bind; simp + +private theorem nd_assertOpt_mem_iffA {σ α : Type} {o : Option α} {a : α} {s s' : σ} : + (a, s') ∈ (NonDet.assertOpt o s).1 ↔ o = some a ∧ s' = s := by + cases o <;> simp [NonDet.assertOpt, NonDet.fail, NonDet.pure, eq_comm] + +private theorem nd_getsThe_mem_iffA {σ α : Type} {f : σ → Option α} {s s' : σ} {a : α} : + (a, s') ∈ (NonDet.getsThe f s).1 ↔ f s = some a ∧ s' = s := by + unfold NonDet.getsThe; exact nd_assertOpt_mem_iffA + +private theorem nd_pure_eqStateA {σ α : Type} {a b : α} {s s' : σ} + (h : (b, s') ∈ ((pure a : NonDet σ α) s).1) : s' = s := by + change (b, s') ∈ (NonDet.pure a s).1 at h + unfold NonDet.pure at h; simp at h; exact h.2 + +private theorem nd_get_mem_iffA {σ : Type} {s a s' : σ} : + (a, s') ∈ (NonDet.get s).1 ↔ a = s ∧ s' = s := by + unfold NonDet.get; simp + +private theorem nd_put_mem_iffA {σ : Type} {s0 s s' : σ} {u : Unit} : + (u, s') ∈ (NonDet.put s0 s).1 ↔ u = () ∧ s' = s0 := by + unfold NonDet.put; simp + +private theorem nd_modify_mem_iffA {σ : Type} {f : σ → σ} {s s' : σ} {u : Unit} : + (u, s') ∈ (NonDet.modify f s).1 ↔ u = () ∧ s' = f s := by + unfold NonDet.modify; simp + +private theorem nd_assert_mem_iffA {σ : Type} {P : Prop} {s s' : σ} {u : Unit} : + (u, s') ∈ (NonDet.assert P s).1 ↔ u = () ∧ P ∧ s' = s := by + unfold NonDet.assert; by_cases h : P <;> simp [h] + +private theorem doMachineOp_mem_iff_aA {α : Type} {f : MachineMonad α} {σ σ' : State Unit} {a : α} : + (a, σ') ∈ (doMachineOp (Z := Unit) f σ).1 ↔ + ∃ ms', (a, ms') ∈ (f σ.machineState).1 ∧ σ' = { σ with machineState := ms' } := by + unfold doMachineOp + rw [nd_bind_mem_iffA]; constructor + · rintro ⟨ms, sm1, h1, h2⟩ + have h1' : ms = σ.machineState ∧ sm1 = σ := by unfold NonDet.gets at h1; simp at h1; exact h1 + obtain ⟨rfl, hsm1⟩ := h1'; subst sm1 + rw [nd_bind_mem_iffA] at h2; obtain ⟨⟨r, ms'⟩, sm2, h2a, h2b⟩ := h2 + have h2a' : (r, ms') ∈ (f σ.machineState).1 ∧ sm2 = σ := by unfold NonDet.selectF at h2a; simp at h2a; exact h2a + obtain ⟨hfm, hsm2⟩ := h2a'; subst sm2 + rw [nd_bind_mem_iffA] at h2b; obtain ⟨_, sm3, h3, h4⟩ := h2b + rw [nd_modify_mem_iffA] at h3; obtain ⟨-, rfl⟩ := h3 + have h4' : a = r ∧ σ' = { σ with machineState := ms' } := by + change (a, σ') ∈ (NonDet.pure r { σ with machineState := ms' }).1 at h4 + unfold NonDet.pure at h4; simp at h4; exact h4 + obtain ⟨rfl, rfl⟩ := h4'; exact ⟨ms', hfm, rfl⟩ + · rintro ⟨ms', hfm, rfl⟩ + refine ⟨σ.machineState, σ, by unfold NonDet.gets; simp, ?_⟩ + rw [nd_bind_mem_iffA] + refine ⟨(a, ms'), σ, by unfold NonDet.selectF; simp [hfm], ?_⟩ + rw [nd_bind_mem_iffA] + exact ⟨(), { σ with machineState := ms' }, by rw [nd_modify_mem_iffA]; exact ⟨rfl, rfl⟩, + by change (a, _) ∈ (NonDet.pure a _).1; unfold NonDet.pure; simp⟩ + +private theorem machineOpLift_timeA {f : MachineRestMonad Unit} {ms ms' : MachineState} + (h : ((), ms') ∈ (machineOpLift f ms).1) : + ms'.timeState = ms.timeState ∧ ms'.lastMachineTime = ms.lastMachineTime := by + unfold machineOpLift machineRestLift at h + rw [nd_bind_mem_iffA] at h; obtain ⟨s0, sm0, h0, h⟩ := h + have h0' : s0 = ms.machineStateRest ∧ sm0 = ms := by unfold NonDet.gets at h0; simp at h0; exact h0 + obtain ⟨rfl, hsm0⟩ := h0'; subst sm0 + rw [nd_bind_mem_iffA] at h; obtain ⟨⟨r, rest'⟩, sm1, h1, h⟩ := h + have h1' : sm1 = ms := by unfold NonDet.selectF at h1; simp at h1; exact h1.2 + subst sm1 + rw [nd_bind_mem_iffA] at h; obtain ⟨_, sm2, h2, h3⟩ := h + rw [nd_modify_mem_iffA] at h2; obtain ⟨-, hsm2⟩ := h2; subst sm2 + have h3' : ms' = { ms with machineStateRest := rest' } := nd_pure_eqStateA h3 + subst ms'; exact ⟨rfl, rfl⟩ + +/-- Preserves `machineState.timeState`, `machineState.lastMachineTime`, and every TCB's `tcbVcpu` +and every VCPU's `vcpuTcb`. -/ +def VcpuSpecKeepA (σ σ' : State Unit) : Prop := + σ'.machineState.timeState = σ.machineState.timeState ∧ + σ'.machineState.lastMachineTime = σ.machineState.lastMachineTime ∧ + (∀ p x, σ.kheap p = some (.tcb x) → ∃ x', σ'.kheap p = some (.tcb x') ∧ x'.tcbArch.tcbVcpu = x.tcbArch.tcbVcpu) ∧ + (∀ p v, σ.kheap p = some (.archObj (.vcpu v)) → ∃ v', σ'.kheap p = some (.archObj (.vcpu v')) ∧ v'.vcpuTcb = v.vcpuTcb) ∧ + σ'.curTime = σ.curTime + +private theorem VcpuSpecKeepA.refl (σ : State Unit) : VcpuSpecKeepA σ σ := + ⟨rfl, rfl, fun _ x h => ⟨x, h, rfl⟩, fun _ v h => ⟨v, h, rfl⟩, rfl⟩ + +private theorem VcpuSpecKeepA.trans {σ1 σ2 σ3 : State Unit} + (h1 : VcpuSpecKeepA σ1 σ2) (h2 : VcpuSpecKeepA σ2 σ3) : VcpuSpecKeepA σ1 σ3 := + ⟨h2.1.trans h1.1, h2.2.1.trans h1.2.1, + fun p x hx => let ⟨x', hx', ex⟩ := h1.2.2.1 p x hx; let ⟨x'', hx'', ex'⟩ := h2.2.2.1 p x' hx' + ⟨x'', hx'', ex'.trans ex⟩, + fun p v hv => let ⟨v', hv', ev⟩ := h1.2.2.2.1 p v hv; let ⟨v'', hv'', ev'⟩ := h2.2.2.2.1 p v' hv' + ⟨v'', hv'', ev'.trans ev⟩, + h2.2.2.2.2.trans h1.2.2.2.2⟩ + +private theorem VcpuSpecKeepA.of_eq {σ σ' : State Unit} (h : σ' = σ) : VcpuSpecKeepA σ σ' := by + subst σ'; exact VcpuSpecKeepA.refl σ + +private theorem setObject_eq_kheapA {p : ObjRef} {obj : KernelObject} {σ σ' : State Unit} {u : Unit} + (h : (u, σ') ∈ (setObject (Z := Unit) p obj σ).1) : + σ' = { σ with kheap := Function.update σ.kheap p (some obj) } := by + unfold setObject getObject at h + rw [nd_bind_mem_iffA] at h; obtain ⟨ko, sm0, h0, h⟩ := h + rw [nd_getsThe_mem_iffA] at h0; obtain ⟨-, hsm0⟩ := h0; subst hsm0 + rw [nd_bind_mem_iffA] at h; obtain ⟨_, sm1, h1, h⟩ := h + rw [nd_assert_mem_iffA] at h1; obtain ⟨-, -, hsm1⟩ := h1; subst hsm1 + rw [nd_bind_mem_iffA] at h; obtain ⟨s0, sm2, h2, h⟩ := h + rw [nd_get_mem_iffA] at h2; obtain ⟨rfl, hsm2⟩ := h2; subst hsm2 + rw [nd_put_mem_iffA] at h; exact h.2 + +private theorem getVcpu_kheap_eqA {vc : ObjRef} {v : Vcpu} {σ σ' : State Unit} + (h : (v, σ') ∈ (getVcpu (Z := Unit) vc σ).1) : + σ.kheap vc = some (.archObj (.vcpu v)) ∧ σ' = σ := by + unfold getVcpu NonDet.getsMap at h + rw [nd_bind2_mem_iffA] at h; obtain ⟨m, sm', hm1, hm2⟩ := h + have hm1' : m = vcpusOf σ ∧ sm' = σ := by unfold NonDet.gets at hm1; simp at hm1; exact hm1 + obtain ⟨rfl, hsm'⟩ := hm1'; subst sm'; rw [nd_assertOpt_mem_iffA] at hm2 + refine ⟨?_, hm2.2⟩ + have hv := hm2.1 + unfold vcpusOf optMap aobjsOf aobjOf vcpuOf at hv + unfold optMap at hv + cases hk : σ.kheap vc with + | none => rw [hk] at hv; simp at hv + | some ko' => + rw [hk] at hv + cases ko' with + | archObj ao => + cases ao with + | vcpu vv => simp at hv; subst hv; rfl + | _ => simp at hv + | _ => simp at hv + +private theorem doMachineOp_lift_keepA (f : MachineRestMonad Unit) : + ∀ σ σ', ((), σ') ∈ (doMachineOp (Z := Unit) (machineOpLift f) σ).1 → VcpuSpecKeepA σ σ' := fun σ σ' h => by + rw [doMachineOp_mem_iff_aA] at h; obtain ⟨ms', hm, rfl⟩ := h + obtain ⟨ht, hl⟩ := machineOpLift_timeA hm + exact ⟨ht, hl, fun _ x hx => ⟨x, hx, rfl⟩, fun _ v hv => ⟨v, hv, rfl⟩, rfl⟩ + +private theorem doMachineOp_gets_keepA {α : Type} (f : MachineState → α) : + ∀ σ a σ', (a, σ') ∈ (doMachineOp (Z := Unit) (NonDet.gets f) σ).1 → VcpuSpecKeepA σ σ' := fun σ a σ' h => by + rw [doMachineOp_mem_iff_aA] at h; obtain ⟨ms', hm, rfl⟩ := h + have hm' : ms' = σ.machineState := by unfold NonDet.gets at hm; simp at hm; exact hm.2 + subst hm'; exact VcpuSpecKeepA.refl σ + +private theorem vcpuUpdate_keepA (vc : ObjRef) (f : Vcpu → Vcpu) (hf : ∀ v, (f v).vcpuTcb = v.vcpuTcb) : + ∀ σ σ', ((), σ') ∈ (vcpuUpdate (Z := Unit) vc f σ).1 → VcpuSpecKeepA σ σ' := fun σ σ' h => by + unfold vcpuUpdate at h + rw [nd_bind_mem_iffA] at h; obtain ⟨v, sm, h1, h2⟩ := h + obtain ⟨hkv, rfl⟩ := getVcpu_kheap_eqA h1 + unfold setVcpu at h2 + have hσ' := setObject_eq_kheapA h2; subst hσ' + refine ⟨rfl, rfl, fun p x hx => ?_, fun p v0 hv0 => ?_, rfl⟩ + · by_cases ep : p = vc + · subst ep; rw [hkv] at hx; cases hx + · exact ⟨x, by simp [Function.update, ep, hx], rfl⟩ + · by_cases ep : p = vc + · subst ep; rw [hkv] at hv0; cases hv0; exact ⟨f v, by simp [Function.update], hf v⟩ + · exact ⟨v0, by simp [Function.update, ep, hv0], rfl⟩ + +private theorem VcpuSpecKeepA.bind {α β : Type} {m : SMonad Unit α} {f : α → SMonad Unit β} + (hm : ∀ σ a σ', (a, σ') ∈ (m σ).1 → VcpuSpecKeepA σ σ') + (hf : ∀ a σ b σ', (b, σ') ∈ (f a σ).1 → VcpuSpecKeepA σ σ') : + ∀ σ b σ', (b, σ') ∈ ((m >>= f) σ).1 → VcpuSpecKeepA σ σ' := fun σ b σ' h => by + rw [nd_bind_mem_iffA] at h + obtain ⟨a, sm, h1, h2⟩ := h + exact (hm σ a sm h1).trans (hf a sm b σ' h2) + +private theorem VcpuSpecKeepA.when {c : Prop} [Decidable c] {m : SMonad Unit Unit} + (hm : ∀ σ u σ', (u, σ') ∈ (m σ).1 → VcpuSpecKeepA σ σ') : + ∀ σ u σ', (u, σ') ∈ (NonDet.when c m σ).1 → VcpuSpecKeepA σ σ' := fun σ u σ' h => by + unfold NonDet.when at h + split at h + · exact hm σ u σ' h + · unfold NonDet.pure at h; simp at h; exact VcpuSpecKeepA.of_eq h + +private theorem VcpuSpecKeepA.mapMx {α : Type} {f : α → SMonad Unit Unit} + (hf : ∀ a σ σ', ((), σ') ∈ (f a σ).1 → VcpuSpecKeepA σ σ') : + ∀ (l : List α) σ σ', ((), σ') ∈ (NonDet.mapMx f l σ).1 → VcpuSpecKeepA σ σ' := by + intro l; induction l with + | nil => intro σ σ' h; unfold NonDet.mapMx NonDet.pure at h; simp at h; exact VcpuSpecKeepA.of_eq h + | cons hd tl ih => + intro σ σ' h + unfold NonDet.mapMx at h + rw [nd_bind2_mem_iffA] at h + obtain ⟨_, sm, h1, h2⟩ := h + exact (hf hd σ sm h1).trans (ih sm σ' h2) + +private theorem msTime_storeWord {p : ObjRef} {w : Word} {ms ms' : MachineState} + (h : ((), ms') ∈ (storeWord p w ms).1) : + ms'.timeState = ms.timeState ∧ ms'.lastMachineTime = ms.lastMachineTime := by + unfold storeWord at h + rw [nd_bind_mem_iffA] at h; obtain ⟨_, sm, h1, h2⟩ := h + rw [nd_assert_mem_iffA] at h1; obtain ⟨-, -, rfl⟩ := h1 + rw [nd_modify_mem_iffA] at h2; obtain ⟨-, rfl⟩ := h2 + exact ⟨rfl, rfl⟩ + +private theorem storeWordOffs_keepA (p : ObjRef) (k : Nat) (w : Word) : + ∀ σ σ', ((), σ') ∈ (storeWordOffs (Z := Unit) p k w σ).1 → VcpuSpecKeepA σ σ' := fun σ σ' h => by + unfold storeWordOffs at h + rw [nd_bind_mem_iffA] at h; obtain ⟨s0, sm0, h0, h⟩ := h + rw [nd_get_mem_iffA] at h0; obtain ⟨rfl, rfl⟩ := h0 + rw [nd_bind_mem_iffA] at h; obtain ⟨_, sm1, h1, h⟩ := h + rw [nd_assert_mem_iffA] at h1; obtain ⟨-, -, rfl⟩ := h1 + rw [doMachineOp_mem_iffA] at h; obtain ⟨ms', hm, rfl⟩ := h + obtain ⟨ht, hl⟩ := msTime_storeWord hm + exact ⟨ht, hl, fun _ x hx => ⟨x, hx, rfl⟩, fun _ v hv => ⟨v, hv, rfl⟩, rfl⟩ + +private theorem getVcpu_eqStateA {vc : ObjRef} {v : Vcpu} {σ σ' : State Unit} + (h : (v, σ') ∈ (getVcpu (Z := Unit) vc σ).1) : σ' = σ := + (getVcpu_kheap_eqA h).2 + +private theorem fl4VcpuGet_eqStateA {vc : ObjRef} {r : Nat} {w : Word} {σ σ' : State Unit} + (h : (w, σ') ∈ (fl4VcpuGet (Z := Unit) vc r σ).1) : σ' = σ := by + unfold fl4VcpuGet at h + rw [nd_bind_mem_iffA] at h; obtain ⟨v, sm, h1, h2⟩ := h + have := getVcpu_eqStateA h1; subst this + exact nd_pure_eqStateA h2 + +private theorem fl4VcpuExtUpdate_keepA (vc : ObjRef) (f : ExtVcpu → ExtVcpu) : + ∀ σ σ', ((), σ') ∈ (fl4VcpuExtUpdate (Z := Unit) vc f σ).1 → VcpuSpecKeepA σ σ' := fun σ σ' h => by + unfold fl4VcpuExtUpdate at h + rw [nd_bind_mem_iffA] at h; obtain ⟨v, sm, h1, h2⟩ := h + obtain ⟨hkv, rfl⟩ := getVcpu_kheap_eqA h1 + unfold setVcpu at h2 + have hσ' := setObject_eq_kheapA h2; subst hσ' + refine ⟨rfl, rfl, fun p x hx => ?_, fun p v0 hv0 => ?_, rfl⟩ + · by_cases ep : p = vc + · subst ep; rw [hkv] at hx; cases hx + · exact ⟨x, by simp [Function.update, ep, hx], rfl⟩ + · by_cases ep : p = vc + · subst ep; rw [hkv] at hv0; cases hv0 + exact ⟨{ v with vcpuExt := f v.vcpuExt }, by simp [Function.update], rfl⟩ + · exact ⟨v0, by simp [Function.update, ep, hv0], rfl⟩ + +private theorem threadGet_eqStateA {α : Type} {f : Tcb → α} {t : ObjRef} {a : α} {σ σ' : State Unit} + (h : (a, σ') ∈ (threadGet (Z := Unit) f t σ).1) : σ' = σ := by + unfold threadGet at h + rw [nd_bind_mem_iffA] at h; obtain ⟨x, sm, h1, h2⟩ := h + rw [nd_getsThe_mem_iffA] at h1; obtain ⟨-, hsm⟩ := h1; subst hsm + exact nd_pure_eqStateA h2 + +private theorem threadSet_keepA {f : Tcb → Tcb} (hf : ∀ x, (f x).tcbArch.tcbVcpu = x.tcbArch.tcbVcpu) (t : ObjRef) : + ∀ σ (u : Unit) σ', (u, σ') ∈ (threadSet (Z := Unit) f t σ).1 → VcpuSpecKeepA σ σ' := fun σ u σ' h => by + unfold threadSet at h + rw [nd_bind_mem_iffA] at h; obtain ⟨x, sm, h1, h2⟩ := h + rw [nd_getsThe_mem_iffA] at h1; obtain ⟨hx, hsm⟩ := h1; subst hsm + have hkt : sm.kheap t = some (KernelObject.tcb x) := by + unfold getTcb readObject at hx + cases hk : sm.kheap t with + | none => simp [hk] at hx + | some ko => cases ko with | tcb y => simp [hk] at hx; subst hx; rfl | _ => simp [hk] at hx + have hσ' := setObject_eq_kheapA h2; subst hσ' + refine ⟨rfl, rfl, fun p y hy => ?_, fun p v hv => ?_, rfl⟩ + · by_cases ep : p = t + · subst ep; rw [hkt] at hy; cases hy; exact ⟨f x, by simp [Function.update], hf x⟩ + · exact ⟨y, by simp [Function.update, ep, hy], rfl⟩ + · by_cases ep : p = t + · subst ep; rw [hkt] at hv; cases hv + · exact ⟨v, by simp [Function.update, ep, hv], rfl⟩ + +private theorem asUser_keepA {α : Type} (t : ObjRef) (f : NonDet UserContext α) : + ∀ σ a σ', (a, σ') ∈ (asUser (Z := Unit) t f σ).1 → VcpuSpecKeepA σ σ' := fun σ a σ' h => by + unfold asUser at h + rw [nd_bind_mem_iffA] at h; obtain ⟨x, sm1, h1, h⟩ := h + rw [nd_getsThe_mem_iffA] at h1; obtain ⟨hx, hsm1⟩ := h1; subst hsm1 + have hkt : sm1.kheap t = some (KernelObject.tcb x) := by + unfold getTcb readObject at hx + cases hk : sm1.kheap t with + | none => simp [hk] at hx + | some ko => cases ko with | tcb y => simp [hk] at hx; subst hx; rfl | _ => simp [hk] at hx + rw [nd_bind_mem_iffA] at h; obtain ⟨uc, sm1b, h1b, h⟩ := h + have hsm1b := nd_pure_eqStateA h1b; subst sm1b + rw [nd_bind_mem_iffA] at h; obtain ⟨⟨a', uc'⟩, sm2, h2, h⟩ := h + have hsm2 : sm2 = sm1 := by unfold NonDet.selectF at h2; simp at h2; exact h2.2 + subst sm2 + rw [nd_bind_mem_iffA] at h; obtain ⟨newTcb, sm3, h3, h⟩ := h + have h3p : newTcb = { x with tcbArch := archTcbContextSet uc' x.tcbArch } ∧ sm3 = sm1 := by + change (newTcb, sm3) ∈ ({({ x with tcbArch := archTcbContextSet uc' x.tcbArch }, sm1)}, False).1 at h3 + simp at h3; exact h3 + obtain ⟨rfl, rfl⟩ := h3p + rw [nd_bind_mem_iffA] at h; obtain ⟨_, sm4, h4, h5⟩ := h + have hsm4 := setObject_eq_kheapA h4; subst hsm4 + have hσ' := nd_pure_eqStateA h5; subst hσ' + refine ⟨rfl, rfl, fun p y hy => ?_, fun p v hv => ?_, rfl⟩ + · by_cases ep : p = t + · subst ep; rw [hkt] at hy; cases hy + exact ⟨{ x with tcbArch := archTcbContextSet uc' x.tcbArch }, by simp [Function.update], rfl⟩ + · exact ⟨y, by simp [Function.update, ep, hy], rfl⟩ + · by_cases ep : p = t + · subst ep; rw [hkt] at hv; cases hv + · exact ⟨v, by simp [Function.update, ep, hv], rfl⟩ + +private theorem copyRegsToFrame_keepA (vc frame : ObjRef) (rs : List Nat) : + ∀ σ σ', ((), σ') ∈ (copyRegsToFrame (Z := Unit) vc frame rs σ).1 → VcpuSpecKeepA σ σ' := by + unfold copyRegsToFrame + exact VcpuSpecKeepA.mapMx (fun r => + fun σ σ' h => VcpuSpecKeepA.bind (fun _ _ _ h => VcpuSpecKeepA.of_eq (fl4VcpuGet_eqStateA h)) + (fun w σ _ σ' hs => storeWordOffs_keepA frame r w σ σ' hs) σ () σ' h) rs + +set_option maxHeartbeats 800000 in +private theorem fl4ReadStateToFrame_keepA (vc frame m : ObjRef) : + ∀ σ σ', ((), σ') ∈ (fl4ReadStateToFrame (Z := Unit) vc frame m σ).1 → VcpuSpecKeepA σ σ' := by + intro σ σ' h + unfold fl4ReadStateToFrame at h + rw [nd_bind_mem_iffA] at h; obtain ⟨_, s1, h1, h⟩ := h + have k1 : VcpuSpecKeepA σ s1 := VcpuSpecKeepA.when (VcpuSpecKeepA.bind + (fun σ _ σ' h => copyRegsToFrame_keepA vc frame _ σ σ' h) + (fun _ σ _ σ' h => copyRegsToFrame_keepA vc frame _ σ σ' h)) σ () s1 h1 + rw [nd_bind_mem_iffA] at h; obtain ⟨_, s2, h2, h⟩ := h + have k2 : VcpuSpecKeepA s1 s2 := VcpuSpecKeepA.when (VcpuSpecKeepA.bind + (fun σ _ σ' h => copyRegsToFrame_keepA vc frame _ σ σ' h) fun _ => + VcpuSpecKeepA.bind (fun σ _ σ' h => copyRegsToFrame_keepA vc frame _ σ σ' h) + (fun _ σ _ σ' h => copyRegsToFrame_keepA vc frame _ σ σ' h)) s1 () s2 h2 + rw [nd_bind_mem_iffA] at h; obtain ⟨_, s3, h3, h⟩ := h + have k3 : VcpuSpecKeepA s2 s3 := VcpuSpecKeepA.when (VcpuSpecKeepA.bind + (fun σ _ σ' h => copyRegsToFrame_keepA vc frame _ σ σ' h) fun _ => + VcpuSpecKeepA.bind (fun _ _ _ h => VcpuSpecKeepA.of_eq (getVcpu_eqStateA h)) + (fun v σ _ σ' h => storeWordOffs_keepA frame _ _ σ σ' h)) s2 () s3 h3 + have k45 : VcpuSpecKeepA s3 σ' := by + have hGprs : ∀ s4 : State Unit, ∀ σ'' : State Unit, + ((), σ'') ∈ (NonDet.when (m &&& smGprs ≠ 0) (do + let v ← getVcpu (Z := Unit) vc + NonDet.mapMx (fun (ir : Nat × Register) => do + let w ← match v.vcpuTcb with + | some t => asUser t (getRegister ir.2) + | none => pure 0 + storeWordOffs frame (64 + ir.1) w) (List.range fl4UserRegs.length |>.zip fl4UserRegs)) s4).1 → + VcpuSpecKeepA s4 σ'' := fun s4 σ'' hg => + VcpuSpecKeepA.when (VcpuSpecKeepA.bind + (fun _ _ _ h => VcpuSpecKeepA.of_eq (getVcpu_eqStateA h)) fun v => + fun σ _ σ' h => VcpuSpecKeepA.mapMx (fun ir σ σ' h => by + dsimp only at h + cases hvt : v.vcpuTcb with + | some t => + simp only [hvt] at h + exact VcpuSpecKeepA.bind (asUser_keepA t _) (fun w σ _ σ' hs => storeWordOffs_keepA frame _ w σ σ' hs) σ () σ' h + | none => + simp only [hvt] at h + exact VcpuSpecKeepA.bind (fun _ _ _ hw => VcpuSpecKeepA.of_eq (nd_pure_eqStateA hw)) + (fun w σ _ σ' hs => storeWordOffs_keepA frame _ w σ σ' hs) σ () σ' h) _ σ σ' h) s4 () σ'' hg + dsimp only at h + split at h + · rw [nd_bind_mem_iffA] at h; obtain ⟨_, s3a, ha, h⟩ := h + rw [nd_bind_mem_iffA] at h; obtain ⟨_, s3b, hb, h⟩ := h + rw [nd_bind_mem_iffA] at h; obtain ⟨_, s3c, hc, h⟩ := h + rw [nd_bind_mem_iffA] at h; obtain ⟨_, s3d, hd, hg⟩ := h + have ka := storeWordOffs_keepA frame _ _ s3 s3a ha + have kb := copyRegsToFrame_keepA vc frame _ s3a s3b hb + have kc : VcpuSpecKeepA s3b s3c := VcpuSpecKeepA.mapMx (fun r σ σ' h => by + split at h + · exact VcpuSpecKeepA.bind (fun _ _ _ hw => VcpuSpecKeepA.of_eq (fl4VcpuGet_eqStateA hw)) + (fun w σ _ σ' hs => storeWordOffs_keepA frame r w σ σ' hs) σ () σ' h + · exact VcpuSpecKeepA.bind (fun _ _ _ hw => VcpuSpecKeepA.of_eq (nd_pure_eqStateA hw)) + (fun w σ _ σ' hs => storeWordOffs_keepA frame r w σ σ' hs) σ () σ' h) _ s3b s3c hc + have kd : VcpuSpecKeepA s3c s3d := VcpuSpecKeepA.mapMx (fun i σ σ' h => by + split at h + · exact VcpuSpecKeepA.bind (fun _ _ _ hw => VcpuSpecKeepA.of_eq (fl4VcpuGet_eqStateA hw)) + (fun w σ _ σ' hs => storeWordOffs_keepA frame _ w σ σ' hs) σ () σ' h + · exact VcpuSpecKeepA.bind (fun _ _ _ hw => VcpuSpecKeepA.of_eq (nd_pure_eqStateA hw)) + (fun w σ _ σ' hs => storeWordOffs_keepA frame _ w σ σ' hs) σ () σ' h) _ s3c s3d hd + exact (((ka.trans kb).trans kc).trans kd).trans (hGprs s3d σ' hg) + · rw [nd_bind_mem_iffA] at h; obtain ⟨_, s3d, hd, hg⟩ := h + have kd : VcpuSpecKeepA s3 s3d := VcpuSpecKeepA.when (VcpuSpecKeepA.bind + (fun σ _ σ' h => copyRegsToFrame_keepA vc frame _ σ σ' h) + (fun _ σ _ σ' h => VcpuSpecKeepA.mapMx (fun i σ σ' h => by + split at h + · exact VcpuSpecKeepA.bind (fun _ _ _ hw => VcpuSpecKeepA.of_eq (fl4VcpuGet_eqStateA hw)) + (fun w σ _ σ' hs => storeWordOffs_keepA frame _ w σ σ' hs) σ () σ' h + · exact VcpuSpecKeepA.bind (fun _ _ _ hw => VcpuSpecKeepA.of_eq (nd_pure_eqStateA hw)) + (fun w σ _ σ' hs => storeWordOffs_keepA frame _ w σ σ' hs) σ () σ' h) _ σ σ' h)) s3 () s3d hd + exact kd.trans (hGprs s3d σ' hg) + exact ((k1.trans k2).trans k3).trans k45 + +private theorem getCap_eqStateA {sl : CslotPtr} {c : Cap} {σ σ' : State Unit} + (h : (c, σ') ∈ (getCap (Z := Unit) sl σ).1) : σ' = σ := by + unfold getCap getObject at h + rw [nd_bind_mem_iffA] at h; obtain ⟨ko, sm, h1, h2⟩ := h + rw [nd_getsThe_mem_iffA] at h1; obtain ⟨-, rfl⟩ := h1 + cases ko with + | cnode sz cs => + simp only at h2 + rw [nd_bind_mem_iffA] at h2; obtain ⟨_, s1, ha, h2⟩ := h2 + rw [nd_assert_mem_iffA] at ha; obtain ⟨-, -, rfl⟩ := ha + rw [nd_bind_mem_iffA] at h2; obtain ⟨_, s2, hb, h2⟩ := h2 + have := nd_pure_eqStateA hb; subst s2 + rw [nd_assertOpt_mem_iffA] at h2; exact h2.2 + | tcb x => + simp only at h2 + rw [nd_bind_mem_iffA] at h2; obtain ⟨_, s2, hb, h2⟩ := h2 + have := nd_pure_eqStateA hb; subst s2 + rw [nd_assertOpt_mem_iffA] at h2; exact h2.2 + | _ => simp [NonDet.fail] at h2 + +private theorem setThreadState_keepA (t : ObjRef) (ts : ThreadState) : + ∀ σ σ', ((), σ') ∈ (setThreadState (Z := Unit) t ts σ).1 → VcpuSpecKeepA σ σ' := fun σ σ' h => by + unfold setThreadState at h + rw [nd_bind_mem_iffA] at h; obtain ⟨_, sm1, h1, h2⟩ := h + have hk1 := threadSet_keepA (f := fun x => { x with tcbState := ts }) (fun _ => rfl) t σ () sm1 h1 + refine hk1.trans ?_ + unfold setThreadStateAct at h2 + rw [nd_bind_mem_iffA] at h2; obtain ⟨ct, sm2, h2a, h2⟩ := h2 + have h2a' : sm2 = sm1 := by unfold NonDet.gets at h2a; simp at h2a; exact h2a.2 + subst sm2 + rw [nd_bind_mem_iffA] at h2; obtain ⟨sa, sm3, h2b, h2⟩ := h2 + have h2b' : sm3 = sm1 := by unfold NonDet.gets at h2b; simp at h2b; exact h2b.2 + subst sm3 + rw [nd_bind_mem_iffA] at h2; obtain ⟨ts', sm4, h2c, h2⟩ := h2 + have h2c' : sm4 = sm1 := by unfold NonDet.gets at h2c; simp at h2c; exact h2c.2 + subst sm4 + unfold NonDet.when at h2; split at h2 + · unfold setSchedulerAction at h2; rw [nd_modify_mem_iffA] at h2; obtain ⟨-, rfl⟩ := h2 + exact ⟨rfl, rfl, fun _ x hx => ⟨x, hx, rfl⟩, fun _ v hv => ⟨v, hv, rfl⟩, rfl⟩ + · unfold NonDet.pure at h2; simp at h2; exact VcpuSpecKeepA.of_eq h2 + +private theorem VcpuSpecKeepA.mapM {α β : Type} {f : α → SMonad Unit β} + (hf : ∀ a σ b σ', (b, σ') ∈ (f a σ).1 → VcpuSpecKeepA σ σ') : + ∀ (l : List α) σ bs σ', (bs, σ') ∈ (NonDet.mapM f l σ).1 → VcpuSpecKeepA σ σ' := by + intro l; induction l with + | nil => intro σ bs σ' h; unfold NonDet.mapM NonDet.pure at h; simp at h; exact VcpuSpecKeepA.of_eq h.2 + | cons hd tl ih => + intro σ bs σ' h + unfold NonDet.mapM at h + rw [nd_bind2_mem_iffA] at h; obtain ⟨b, sm1, h1, h2⟩ := h + rw [nd_bind2_mem_iffA] at h2; obtain ⟨bs', sm2, h2a, h2b⟩ := h2 + have h2b' : σ' = sm2 := by unfold NonDet.pure at h2b; simp at h2b; exact h2b.2 + subst σ' + exact (hf hd σ b sm1 h1).trans (ih sm1 bs' sm2 h2a) + +private theorem vcpuSaveReg_keepA (vr : ObjRef) (reg : VCPUReg) : + ∀ σ σ', ((), σ') ∈ (vcpuSaveReg (Z := Unit) vr reg σ).1 → VcpuSpecKeepA σ σ' := fun σ σ' h => by + unfold vcpuSaveReg at h + rw [nd_bind_mem_iffA] at h; obtain ⟨rval, sm, h1, h2⟩ := h + have h1k : VcpuSpecKeepA σ sm := by + unfold readVCPUHardwareReg at h1 + exact doMachineOp_gets_keepA _ σ rval sm h1 + exact h1k.trans (vcpuUpdate_keepA vr (fun vcpu => { vcpu with vcpuRegs := Function.update vcpu.vcpuRegs reg rval }) (fun _ => rfl) sm σ' h2) + +private theorem vcpuInvalidateActive_keepA : + ∀ σ σ', ((), σ') ∈ (vcpuInvalidateActive (Z := Unit) σ).1 → VcpuSpecKeepA σ σ' := fun σ σ' h => by + unfold vcpuInvalidateActive at h + rw [nd_bind_mem_iffA] at h; obtain ⟨cur, sm1, h1, h⟩ := h + have h1' : sm1 = σ := by unfold NonDet.gets at h1; simp at h1; exact h1.2 + subst sm1 + cases cur with + | none => + simp only at h + rw [nd_modify_mem_iffA] at h; obtain ⟨-, rfl⟩ := h + exact ⟨rfl, rfl, fun _ x hx => ⟨x, hx, rfl⟩, fun _ vv hvv => ⟨vv, hvv, rfl⟩, rfl⟩ + | some p => + cases p with + | mk v act => + cases act with + | false => + simp only at h + rw [nd_modify_mem_iffA] at h; obtain ⟨-, rfl⟩ := h + exact ⟨rfl, rfl, fun _ x hx => ⟨x, hx, rfl⟩, fun _ vv hvv => ⟨vv, hvv, rfl⟩, rfl⟩ + | true => + simp only at h + rw [nd_bind_mem_iffA] at h; obtain ⟨_, sm2, h2, h3⟩ := h + have h2k : VcpuSpecKeepA σ sm2 := by + rw [vcpuDisable_none_eqA, doMachineOp_mem_iffA] at h2 + obtain ⟨ms', hm', rfl⟩ := h2 + have := (vcpuDisableNoneMopA_comm.ok σ.machineState).2.2 ((), ms') hm' + exact ⟨this.2.1, this.2.2, fun _ x hx => ⟨x, hx, rfl⟩, fun _ vv hvv => ⟨vv, hvv, rfl⟩, rfl⟩ + rw [nd_modify_mem_iffA] at h3; obtain ⟨-, rfl⟩ := h3 + exact h2k.trans ⟨rfl, rfl, fun _ x hx => ⟨x, hx, rfl⟩, fun _ vv hvv => ⟨vv, hvv, rfl⟩, rfl⟩ + +private theorem vgicUpdate_keepA (vr : ObjRef) (f : GicVcpuInterface → GicVcpuInterface) : + ∀ σ σ', ((), σ') ∈ (vgicUpdate (Z := Unit) vr f σ).1 → VcpuSpecKeepA σ σ' := + fun σ σ' h => vcpuUpdate_keepA vr (fun v => { v with vcpuVgic := f v.vcpuVgic }) (fun _ => rfl) σ σ' h + +private theorem vgicUpdateLr_keepA (vr : ObjRef) (i : Nat) (w : Word) : + ∀ σ σ', ((), σ') ∈ (vgicUpdateLr (Z := Unit) vr i w σ).1 → VcpuSpecKeepA σ σ' := + fun σ σ' h => vgicUpdate_keepA vr (fun vgic => { vgic with vgicLr := Function.update vgic.vgicLr i w }) σ σ' h + +private theorem getGicVcpuCtrlLr_keepA (n : Word) : + ∀ σ w σ', (w, σ') ∈ (doMachineOp (Z := Unit) (getGicVcpuCtrlLr n) σ).1 → VcpuSpecKeepA σ σ' := fun σ w σ' h => by + rw [doMachineOp_mem_iff_aA] at h; obtain ⟨ms', hm, rfl⟩ := h + unfold getGicVcpuCtrlLr at hm + rw [nd_bind_mem_iffA] at hm; obtain ⟨u, ms1, hm1, hm2⟩ := hm; cases u + obtain ⟨ht, hl⟩ := machineOpLift_timeA hm1 + have hm2' : ms' = ms1 := by unfold NonDet.gets at hm2; simp at hm2; exact hm2.2 + subst hm2' + exact ⟨ht, hl, fun _ x hx => ⟨x, hx, rfl⟩, fun _ v hv => ⟨v, hv, rfl⟩, rfl⟩ + +private theorem writeVCPUHardwareReg_keepA (reg : VCPUReg) (val : Word) : + ∀ σ σ', ((), σ') ∈ (doMachineOp (Z := Unit) (writeVCPUHardwareReg reg val) σ).1 → VcpuSpecKeepA σ σ' := by + intro σ σ' h + rw [doMachineOp_mem_iff_aA] at h; obtain ⟨ms', hm, rfl⟩ := h + unfold writeVCPUHardwareReg at hm + rw [nd_bind_mem_iffA] at hm; obtain ⟨u, ms1, hm1, hm2⟩ := hm; cases u + rw [nd_modify_mem_iffA] at hm1; obtain ⟨-, rfl⟩ := hm1 + obtain ⟨ht, hl⟩ := machineOpLift_timeA hm2 + exact ⟨ht, hl, fun _ x hx => ⟨x, hx, rfl⟩, fun _ v hv => ⟨v, hv, rfl⟩, rfl⟩ + +private theorem checkExportArchTimer_keepA : + ∀ σ σ', ((), σ') ∈ (doMachineOp (Z := Unit) checkExportArchTimer σ).1 → VcpuSpecKeepA σ σ' := by + intro σ σ' h + rw [doMachineOp_mem_iff_aA] at h; obtain ⟨ms', hm, rfl⟩ := h + unfold checkExportArchTimer at hm + rw [nd_bind_mem_iffA] at hm; obtain ⟨u, ms1, hm1, hm2⟩ := hm; cases u + rw [nd_modify_mem_iffA] at hm1; obtain ⟨-, rfl⟩ := hm1 + obtain ⟨ht, hl⟩ := machineOpLift_timeA hm2 + exact ⟨ht, hl, fun _ x hx => ⟨x, hx, rfl⟩, fun _ v hv => ⟨v, hv, rfl⟩, rfl⟩ + +private theorem disableVirtTimerTail_keepA : + ∀ σ σ', ((), σ') ∈ (((do + doMachineOp (Z := Unit) (writeVCPUHardwareReg .vcpuRegCNTV_CTL 0) + doMachineOp (Z := Unit) checkExportArchTimer) : SMonad Unit Unit) σ).1 → VcpuSpecKeepA σ σ' := by + intro s1 σ'' ht + rw [nd_bind_mem_iffA] at ht; obtain ⟨u2, s2, h2, h3⟩ := ht; cases u2 + exact (writeVCPUHardwareReg_keepA .vcpuRegCNTV_CTL 0 s1 s2 h2).trans (checkExportArchTimer_keepA s2 σ'' h3) + +private theorem disableVirtTimer_keepA (vr : Option ObjRef) : + ∀ σ σ', ((), σ') ∈ (disableVirtTimer (Z := Unit) vr σ).1 → VcpuSpecKeepA σ σ' := fun σ σ' h => by + unfold disableVirtTimer at h + cases vr with + | none => + simp only at h + exact disableVirtTimerTail_keepA σ σ' h + | some p => + simp only at h + rw [nd_bind_mem_iffA] at h; obtain ⟨_, s1, h1, h⟩ := h + rw [nd_bind_mem_iffA] at h; obtain ⟨_, s2, h2, h⟩ := h + rw [nd_bind_mem_iffA] at h; obtain ⟨_, s3, h3, h⟩ := h + rw [nd_bind_mem_iffA] at h; obtain ⟨_, s4, h4, ht⟩ := h + exact (((vcpuSaveReg_keepA p .vcpuRegCNTV_CTL σ s1 h1).trans + (vcpuSaveReg_keepA p .vcpuRegCNTV_CVAL s1 s2 h2)).trans + (vcpuSaveReg_keepA p .vcpuRegCNTVOFF s2 s3 h3)).trans + ((vcpuSaveReg_keepA p .vcpuRegCNTKCTL_EL1 s3 s4 h4).trans (disableVirtTimerTail_keepA s4 σ' ht)) + +private theorem fl4VcpuFlush_keepA (vc : ObjRef) : + ∀ σ σ', ((), σ') ∈ (fl4VcpuFlush (Z := Unit) vc σ).1 → VcpuSpecKeepA σ σ' := by + unfold fl4VcpuFlush + refine fun σ σ' h => VcpuSpecKeepA.bind (fun σ _ σ' h => by unfold NonDet.gets at h; simp at h; exact VcpuSpecKeepA.of_eq h.2) + (fun cur => VcpuSpecKeepA.when fun σ u σ' h => by + cases u + unfold vcpuFlush at h + rw [nd_bind_mem_iffA] at h; obtain ⟨curV, sm0, h0, h⟩ := h + have h0' : sm0 = σ := by unfold NonDet.gets at h0; simp at h0; exact h0.2 + subst sm0 + refine VcpuSpecKeepA.when (VcpuSpecKeepA.bind (fun σ u σ' h => ?_) (fun _ σ u σ' h => by cases u; exact vcpuInvalidateActive_keepA σ σ' h)) σ () σ' h + cases u + unfold vcpuSave at h + cases curV with + | none => unfold NonDet.fail at h; simp at h + | some p => + cases p with + | mk vr act => + simp only at h + refine VcpuSpecKeepA.bind (fun σ u σ' h => by cases u; unfold dsb at h; exact doMachineOp_lift_keepA dsbImpl σ σ' h) (fun _ => + VcpuSpecKeepA.bind (VcpuSpecKeepA.when ( + VcpuSpecKeepA.bind (fun σ u σ' h => by cases u; exact vcpuSaveReg_keepA vr .vcpuRegSCTLR σ σ' h) fun _ => + VcpuSpecKeepA.bind (fun σ v σ' h => by unfold getGicVcpuCtrlHcr at h; exact doMachineOp_gets_keepA _ σ v σ' h) fun hcr => + VcpuSpecKeepA.bind (fun σ u σ' h => by cases u; exact vgicUpdate_keepA vr _ σ σ' h) fun _ => + fun σ u σ' h => by cases u; exact disableVirtTimer_keepA (some vr) σ σ' h)) fun _ => + VcpuSpecKeepA.bind (fun σ v σ' h => by unfold getGicVcpuCtrlVmcr at h; exact doMachineOp_gets_keepA _ σ v σ' h) fun vmcr => + VcpuSpecKeepA.bind (fun σ u σ' h => by cases u; exact vgicUpdate_keepA vr _ σ σ' h) fun _ => + VcpuSpecKeepA.bind (fun σ v σ' h => by unfold getGicVcpuCtrlApr at h; exact doMachineOp_gets_keepA _ σ v σ' h) fun apr => + VcpuSpecKeepA.bind (fun σ u σ' h => by cases u; exact vgicUpdate_keepA vr _ σ σ' h) fun _ => + VcpuSpecKeepA.bind (fun σ _ σ' h => by unfold NonDet.gets at h; simp at h; exact VcpuSpecKeepA.of_eq h.2) fun nlr => + VcpuSpecKeepA.bind (fun σ _ σ' h => VcpuSpecKeepA.of_eq (nd_pure_eqStateA h)) fun idxs => + VcpuSpecKeepA.bind (fun σ bs σ' h => VcpuSpecKeepA.mapM (fun vreg => + VcpuSpecKeepA.bind (getGicVcpuCtrlLr_keepA _) fun val σ u σ' h => by + cases u; exact vgicUpdateLr_keepA vr vreg val σ σ' h) idxs σ bs σ' h) fun _ => + VcpuSpecKeepA.bind (VcpuSpecKeepA.when ( + VcpuSpecKeepA.bind (fun σ u σ' h => by cases u; exact vcpuSaveReg_keepA vr .vcpuRegCPACR σ σ' h) fun _ => + fun σ u σ' h => by cases u; exact vcpuSaveReg_keepA vr .vcpuRegTCR σ σ' h)) fun _ => + fun σ u σ' h => by cases u; unfold vcpuSaveRegRange at h; exact VcpuSpecKeepA.mapMx (fun r => vcpuSaveReg_keepA vr r) _ σ σ' h) σ () σ' h) σ () σ' h + +private theorem fl4VcpuExtUpdate_clearCaller_keepA (vc : ObjRef) : + ∀ σ (u : Unit) σ', (u, σ') ∈ (fl4VcpuExtUpdate (Z := Unit) vc (fun e => { e with runCaller := none }) σ).1 → + VcpuSpecKeepA σ σ' := fun σ u σ' h => by cases u; exact fl4VcpuExtUpdate_keepA vc _ σ σ' h + +private theorem setRunVcpu_none_keepA (t : ObjRef) : + ∀ σ (u : Unit) σ', (u, σ') ∈ (setRunVcpu (Z := Unit) t none σ).1 → VcpuSpecKeepA σ σ' := + threadSet_keepA (f := fun tcb => { tcb with tcbExt := { tcb.tcbExt with tcbRunVcpu := none } }) (fun _ => rfl) t + +private theorem fl4CompleteRun_keepA (ct : ObjRef) (stop : RunStop) (w1 w2 w3 : Word) : + ∀ σ σ', ((), σ') ∈ (fl4CompleteRun (Z := Unit) ct stop w1 w2 w3 σ).1 → VcpuSpecKeepA σ σ' := by + intro σ σ' h + unfold fl4CompleteRun getRunVcpu at h + rw [nd_bind_mem_iffA] at h; obtain ⟨vcOpt, s0, h0, h⟩ := h + have hs0 := threadGet_eqStateA h0; subst s0 + cases vcOpt with + | none => exact VcpuSpecKeepA.of_eq (nd_pure_eqStateA h) + | some vc => + simp only at h + rw [nd_bind_mem_iffA] at h; obtain ⟨_, s1, h1, h⟩ := h + have k1 := fl4VcpuFlush_keepA vc σ s1 h1 + rw [nd_bind_mem_iffA] at h; obtain ⟨v, s2, h2, h⟩ := h + have hs2 := getVcpu_eqStateA h2; subst s2 + rw [nd_bind_mem_iffA] at h; obtain ⟨_, s3, h3, h⟩ := h + have k3 : VcpuSpecKeepA s1 s3 := VcpuSpecKeepA.when (VcpuSpecKeepA.bind + (fun _ _ _ h => VcpuSpecKeepA.of_eq (getCap_eqStateA h)) fun buf => + fun σ _ σ' h => by + cases hc1 : frameRightsDev buf with + | none => simp [hc1] at h; exact VcpuSpecKeepA.of_eq h + | some rd => + cases rd with + | mk R dev => + cases dev with + | true => simp [hc1] at h; exact VcpuSpecKeepA.of_eq h + | false => + cases hc2 : stateWindow buf v.vcpuExt.runOffset with + | none => simp [hc1, hc2] at h; exact VcpuSpecKeepA.of_eq h + | some mf => + cases mf with + | none => simp [hc1, hc2] at h; exact VcpuSpecKeepA.of_eq h + | some frame => + simp [hc1, hc2] at h + exact VcpuSpecKeepA.when (fun σ _ σ' h => fl4ReadStateToFrame_keepA vc frame v.vcpuExt.runOutMask σ σ' h) σ () σ' h) s1 () s3 h3 + rw [nd_bind_mem_iffA] at h; obtain ⟨_, s4, h4, h⟩ := h + have k4 := fl4VcpuExtUpdate_clearCaller_keepA vc s3 () s4 h4 + rw [nd_bind_mem_iffA] at h; obtain ⟨_, s5, h5, h⟩ := h + have k5 := setRunVcpu_none_keepA ct s4 () s5 h5 + rw [nd_bind_mem_iffA] at h; obtain ⟨_, s6, h6, h⟩ := h + have k6 := asUser_keepA ct _ s5 () s6 h6 + rw [nd_bind_mem_iffA] at h; obtain ⟨_, s7, h7, h⟩ := h + have k7 := asUser_keepA ct _ s6 () s7 h7 + rw [nd_bind_mem_iffA] at h; obtain ⟨_, s8, h8, h⟩ := h + have k8 := asUser_keepA ct _ s7 () s8 h8 + rw [nd_bind_mem_iffA] at h; obtain ⟨_, s9, h9, h10⟩ := h + have k9 := asUser_keepA ct _ s8 () s9 h9 + have k10 := setThreadState_keepA ct .running s9 σ' h10 + exact (((((((k1.trans k3).trans k4).trans k5).trans k6).trans k7).trans k8).trans k9).trans k10 + +private theorem fl4CancelRun_keepA (t : ObjRef) : + ∀ σ σ', ((), σ') ∈ (fl4CancelRun (Z := Unit) t σ).1 → VcpuSpecKeepA σ σ' := by + intro σ σ' h + unfold fl4CancelRun getRunVcpu at h + rw [nd_bind_mem_iffA] at h; obtain ⟨vcOpt, s0, h0, h⟩ := h + have hs0 := threadGet_eqStateA h0; subst s0 + cases vcOpt with + | none => exact VcpuSpecKeepA.of_eq (nd_pure_eqStateA h) + | some vc => + simp only at h + rw [nd_bind_mem_iffA] at h; obtain ⟨_, s1, h1, h2⟩ := h + exact (fl4VcpuExtUpdate_clearCaller_keepA vc σ () s1 h1).trans (setRunVcpu_none_keepA t s1 () σ' h2) + +private theorem fl4VcpuEndRun_keepA (vc : ObjRef) : + ∀ σ σ', ((), σ') ∈ (fl4VcpuEndRun (Z := Unit) vc σ).1 → VcpuSpecKeepA σ σ' := by + intro σ σ' h + unfold fl4VcpuEndRun getThreadState at h + rw [nd_bind_mem_iffA] at h; obtain ⟨v, s0, h0, h⟩ := h + have hs0 := getVcpu_eqStateA h0; subst s0 + cases hrc : v.vcpuExt.runCaller with + | none => simp only [hrc] at h; exact VcpuSpecKeepA.of_eq (nd_pure_eqStateA h) + | some c => + simp only [hrc] at h + rw [nd_bind_mem_iffA] at h; obtain ⟨st, s1, h1, h⟩ := h + have hs1 := threadGet_eqStateA h1; subst s1 + split at h + · exact fl4CompleteRun_keepA c .interrupt 0 0 0 σ σ' h + · rw [nd_bind_mem_iffA] at h; obtain ⟨_, s2, h2, h3⟩ := h + exact (fl4VcpuExtUpdate_clearCaller_keepA vc σ () s2 h2).trans (setRunVcpu_none_keepA c s2 () σ' h3) + +/-! ## Section 2: Exported standalone theorems (`hTd` and `hLink`) -/ + +/-- Standalone spec time-keep lemma (`DelThrHookA`'s `hTd`): `fl4VcpuDetach` keeps +`machineState.timeState` and `machineState.lastMachineTime` across all `armCurrentVcpu` branches. -/ +theorem fl4VcpuDetach_timeA {t : ObjRef} {σ σ1 : Fl4SpecA64.State Unit} + (h : ((), σ1) ∈ (fl4VcpuDetach t σ).1) : + σ1.machineState.timeState = σ.machineState.timeState ∧ + σ1.machineState.lastMachineTime = σ.machineState.lastMachineTime := by + unfold fl4VcpuDetach at h + rw [nd_bind_mem_iffA] at h; obtain ⟨_, σc, hc, h⟩ := h + have hkc := fl4CancelRun_keepA t σ σc hc + rw [nd_bind_mem_iffA] at h; obtain ⟨vOpt, σg, hg, h⟩ := h + have hsg := threadGet_eqStateA hg; subst σg + cases vOpt with + | none => + simp only at h + have hσ1 := nd_pure_eqStateA h; subst hσ1 + exact ⟨hkc.1, hkc.2.1⟩ + | some vc => + simp only at h + unfold fl4VcpuUnbind at h + rw [nd_bind_mem_iffA] at h; obtain ⟨_, σe, he, h⟩ := h + have hke := hkc.trans (fl4VcpuEndRun_keepA vc σc σe he) + rw [nd_bind_mem_iffA] at h; obtain ⟨v, σv, hv, h⟩ := h + have hsv := getVcpu_eqStateA hv; subst σv + cases hvt : v.vcpuTcb with + | none => + simp only [hvt] at h + have hσ1 := nd_pure_eqStateA h; subst hσ1 + exact ⟨hke.1, hke.2.1⟩ + | some t' => + simp only [hvt] at h + -- Extract time-keep from `dissociateVcpuTcb` + unfold dissociateVcpuTcb at h + rw [nd_bind_mem_iffA] at h; obtain ⟨_, s1, h1, h⟩ := h + have hs1 := threadGet_eqStateA h1; subst s1 + rw [nd_bind_mem_iffA] at h; obtain ⟨_, s2, h2, h⟩ := h + have hs2 := getVcpu_eqStateA h2; subst s2 + rw [nd_bind_mem_iffA] at h; obtain ⟨_, s3, h3, h⟩ := h + rw [nd_assert_mem_iffA] at h3; obtain ⟨-, -, hs3⟩ := h3; subst s3 + rw [nd_bind_mem_iffA] at h; obtain ⟨curV, s4, h4, h⟩ := h + have hs4 : s4 = σe := by unfold NonDet.gets at h4; simp at h4; exact h4.2 + subst s4 + rw [nd_bind_mem_iffA] at h; obtain ⟨_, s5, h5, h⟩ := h + have hk5 : VcpuSpecKeepA σe s5 := + VcpuSpecKeepA.when (fun σ _ σ' h => vcpuInvalidateActive_keepA σ σ' h) σe () s5 h5 + rw [nd_bind_mem_iffA] at h; obtain ⟨_, s6, h6, h⟩ := h + have hms6 : s6.machineState = s5.machineState := by + unfold archThreadSet at h6 + rw [nd_bind_mem_iffA] at h6; obtain ⟨x, sm, ha, hb⟩ := h6 + rw [nd_getsThe_mem_iffA] at ha; obtain ⟨-, hsm⟩ := ha; subst sm + rw [setObject_eq_kheapA hb] + rw [nd_bind_mem_iffA] at h; obtain ⟨_, s7, h7, h8⟩ := h + have hms7 : s7.machineState = s6.machineState := by + unfold setVcpu at h7 + rw [setObject_eq_kheapA h7] + have h8k := asUser_keepA t' _ s7 () σ1 h8 + exact ⟨by rw [h8k.1, hms7, hms6, hk5.1, hke.1], by rw [h8k.2.1, hms7, hms6, hk5.2.1, hke.2.1]⟩ + +/-- Standalone spec link-keep lemma (`DelThrHookA`'s `hLink`): `fl4VcpuEndRun` keeps +the TCB↔VCPU binding links (`tcbVcpu` / `vcpuTcb`). -/ +theorem fl4VcpuEndRun_keeps_bindA {σ σc σe : Fl4SpecA64.State Unit} {t v : ObjRef} {xc : Tcb} + (hv : ∃ xv, σ.kheap v = some (.archObj (.vcpu xv)) ∧ xv.vcpuTcb = some t) + (hc : ((), σc) ∈ (fl4CancelRun (Z := Unit) t σ).1) + (hkc : σc.kheap t = some (.tcb xc)) (htv : xc.tcbArch.tcbVcpu = some v) + (he : ((), σe) ∈ (fl4VcpuEndRun (Z := Unit) v σc).1) : + (∃ xt, σe.kheap t = some (.tcb xt) ∧ xt.tcbArch.tcbVcpu = some v) ∧ + (∃ xv, σe.kheap v = some (.archObj (.vcpu xv)) ∧ xv.vcpuTcb = some t) := by + have hkc' := fl4CancelRun_keepA t σ σc hc + have hke' := fl4VcpuEndRun_keepA v σc σe he + obtain ⟨xv0, hkv0, hvt0⟩ := hv + obtain ⟨xv1, hkv1, hvt1⟩ := hkc'.2.2.2.1 v xv0 hkv0 + obtain ⟨xt, hkt, htv'⟩ := hke'.2.2.1 t xc hkc + obtain ⟨xv, hkv, hvt'⟩ := hke'.2.2.2.1 v xv1 hkv1 + exact ⟨⟨xt, hkt, htv'.trans htv⟩, ⟨xv, hkv, hvt'.trans (hvt1.trans hvt0)⟩⟩ + +/-- `fl4VcpuEndRun` preserves `curTime`. -/ +theorem fl4VcpuEndRun_curTimeA {v : ObjRef} {σ σ' : Fl4SpecA64.State Unit} + (h : ((), σ') ∈ (fl4VcpuEndRun (Z := Unit) v σ).1) : σ'.curTime = σ.curTime := + (fl4VcpuEndRun_keepA v σ σ' h).2.2.2.2 + +end +end Fl4ProofsA64.P3.Vcpu diff --git a/proofs/Fl4ProofsA64/P3/VcpuDelStepsA.lean b/proofs/Fl4ProofsA64/P3/VcpuDelStepsA.lean new file mode 100644 index 000000000..942d4d404 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/VcpuDelStepsA.lean @@ -0,0 +1,561 @@ +/- +P3 (aarch64, A64-P3 S4): **VCPU deletion / suspend helper steps on `JStage3TA`**. + +Provides the core `JStage3TA` step lemmas used by `TCB_Suspend` (`suspend_recA`), +`vcpu.complete_run`, `vcpu.unbind`, and `vcpu.detach` (`VcpuDelHookA`): +- `utVs_off_vcpuWrA`: discharges `hUtVs` for `JStage3TA.of_vcpuStepA` from `J.vsOk` + `J7FrA.fr`. +- `remote_tcb_stall_singleA`: discharges `smp.remote_tcb_stall st ar s a t = ok (s, a)` from + `[S] numNodes = 1` + `J.sr.sch.aff` + `J.sr.sch.cpuCs` + `J.vcRun` (no `[P]` premise). +- `vcpu_cancel_run_stage3TA`: live `vcpu.cancel_run` refinement on `JStage3TA` (matching + `handoff/s4-vcpu-arms-stmt.lean` Section 2 verbatim). +-/ +import Fl4ProofsA64.P3.VcpuDelFrameA +import Fl4ProofsA64.P3.VcpuDelSpecA + +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3.Vcpu + +open Fl4Spec Fl4SpecA64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST +open Fl4ProofsA64.P3 Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section steps +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +private theorem wa_zeroVcS (x : U64) : wa x 0#u64 = x := by + apply UScalar.eq_of_val_eq + have h0v : (0#u64).val = 0 := rfl + rw [wa_valm, h0v, Nat.add_zero]; exact Nat.mod_eq_of_lt x.hBounds + +/-- Any word in `VcHdrWrA` or a TCB register/link word (`256..3327, 3664, 3672`) is off +`VsR (L.ksb s) (utVsPtA st Pp Tb Lv s)`. -/ +theorem utVs_off_vcpuNoFrA + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J5 J7 : JSlotA S} + {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) : + ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → + ¬ (VcHdrWrA Z.V b ∨ ∃ t ∈ Z.T, ∃ K : U64, K.val % 8 = 0 ∧ + ((256 ≤ K.val ∧ K.val < 3328) ∨ K = 3664#u64 ∨ K = 3672#u64) ∧ b = wa t K) := by + intro b hb hw + have vr := J.vsOk + rcases hw with hvc | ⟨t, ht, K, -, hK, rfl⟩ + · exact vr.vc b hb hvc.in_vcW + · have htH : H t = some .tcb := show t ∈ H.tcbs by rw [J.tcbs]; exact ht + have hfit := J.ent.heap.fits t .tcb htH + change t.val + 4096 ≤ 2 ^ 64 at hfit + have hKv : K.val < 4096 := by + rcases hK with h | rfl | rfl + · omega + · change (3664 : Nat) < 4096; omega + · change (3672 : Nat) < 4096; omega + have ev : (wa t K).val = t.val + K.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + obtain ⟨-, p, k, hp, hk, hin⟩ := vr.om (wa t K) hb + have hne : p ≠ t := fun e => by + rw [e, htH] at hp + rcases hk with rfl | ⟨_, rfl⟩ | ⟨_, rfl⟩ | ⟨rfl, -⟩ <;> cases hp + have hd := J.ent.heap.disj p k t .tcb hp htH hne + unfold InObj at hin; change p.val + 2 ^ k.bits ≤ t.val ∨ t.val + 4096 ≤ p.val at hd + rcases hd with hd | hd <;> omega + +/-- **Single-core `smp.remote_tcb_stall` no-op**: when `numNodes = 1` and all cores in `Z.Cs` +satisfy `c.val < numNodes`, both `t` and any `runCaller` of `t`'s bound VCPU have affinity equal +to `cur_cpu`, so `smp.remote_tcb_stall` returns `ok (s, a)` with zero arch calls. -/ +theorem remote_tcb_stall_singleA + {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J5 J7 : JSlotA S} + {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n J5 J7 s a σ m) + (hCs : ∀ c ∈ Z.Cs, c.val < Fl4SpecA64.fl4A64Platform.numNodes) + (h1 : Fl4SpecA64.fl4A64Platform.numNodes = 1) + {t : U64} (ht : t ∈ Z.T) : + smp.remote_tcb_stall st ar s a t = ok (s, a) := by + have hcpu_eq : ∀ u ∈ Z.T, L.mem s (affA u) = L.cpu s := fun u hu => by + have ha := hCs _ (J.sr.core.aff u hu) + have hc := hCs _ J.sr.sch.cpuCs + apply UScalar.eq_of_val_eq; omega + unfold smp.remote_tcb_stall + have haff_off : layout.tcb.AFFINITY = ok 3520#u64 := + eq_of_spec_fd Fl4ProofsA64.P0.Consts.layout.tcb.AFFINITY.spec + have haff_t : obj.Tcb.affinity st t s = ok (L.mem s (affA t)) := by + unfold obj.Tcb.affinity affA; rw [haff_off]; simp only [lift, bind_ok, L.read_eq] + have hcur_eq : st.cur_cpu s = ok (L.cpu s) := L.cur_cpu_eq s + rw [haff_t, bind_ok, hcur_eq, bind_ok, hcpu_eq t ht] + simp only [bne_self_eq_false, Bool.false_eq_true, ↓reduceIte, bind_ok] + have hvcpu_off : layout.tcb.VCPU = ok 3664#u64 := + eq_of_spec_fd Fl4ProofsA64.P0.Consts.layout.tcb.VCPU.spec + have hbound : vcpu.bound st s t = ok (L.mem s (wa t 3664#u64)) := by + unfold vcpu.bound; rw [hvcpu_off]; simp only [lift, bind_ok, L.read_eq] + change (do + let v ← vcpu.bound st s t + if (v != 0#u64) = true then _ else ok (s, a)) = ok (s, a) + rw [hbound, bind_ok] + by_cases hv0 : L.mem s (wa t 3664#u64) = 0#u64 + · rw [hv0]; simp only [bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + · have hne_v : (L.mem s (wa t 3664#u64) != 0#u64) = true := bne_iff_ne.mpr hv0 + rw [hne_v]; simp only [↓reduceIte] + have hvV : L.mem s (wa t 3664#u64) ∈ Z.V := (J.vcRun.tcb t ht).resolve_left hv0 + simp only [vcpu.RUN_CALLER, L.read_eq, bind_ok, lift] + by_cases hc0 : L.mem s (wa (L.mem s (wa t 3664#u64)) 24#u64) = 0#u64 + · rw [hc0]; simp only [bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + · have hne_c : (L.mem s (wa (L.mem s (wa t 3664#u64)) 24#u64) != 0#u64) = true := bne_iff_ne.mpr hc0 + rw [hne_c]; simp only [↓reduceIte] + have hcT : L.mem s (wa (L.mem s (wa t 3664#u64)) 24#u64) ∈ Z.T := + (J.vcRun.vcpu _ hvV).resolve_left hc0 + have haff_c : obj.Tcb.affinity st (L.mem s (wa (L.mem s (wa t 3664#u64)) 24#u64)) s = + ok (L.mem s (affA (L.mem s (wa (L.mem s (wa t 3664#u64)) 24#u64)))) := by + unfold obj.Tcb.affinity affA; rw [haff_off]; simp only [lift, bind_ok, L.read_eq] + rw [haff_c, bind_ok, hcur_eq, bind_ok, hcpu_eq _ hcT] + simp only [bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + +private theorem fl4CancelRun_vcpuStepA {H : Heap} {σ : Fl4SpecA64.State Unit} {t vc : ObjRef} + {xt : Fl4SpecA64.Tcb} {xv : Fl4SpecA64.Vcpu} + (hkt : σ.kheap t = some (.tcb xt)) (hkv : σ.kheap vc = some (.archObj (.vcpu xv))) : + VcpuSpecStepA H σ + { σ with kheap := (Function.update + (Function.update σ.kheap vc (some (.archObj (.vcpu { xv with vcpuExt := { xv.vcpuExt with runCaller := none } })))) + t (some (.tcb { xt with tcbExt := { xt.tcbExt with tcbRunVcpu := none } }))) } := by + have hne : t ≠ vc := fun e => by rw [e, hkv] at hkt; cases hkt + refine ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, ?_, fun _ h => absurd rfl h⟩ + intro p + by_cases ept : p = t + · subst ept + refine Or.inr (Or.inl ⟨xt, { xt with tcbExt := { xt.tcbExt with tcbRunVcpu := none } }, hkt, + by simp [Function.update], ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, fun _ => by first | exact id | exact fun _ => rfl⟩⟩) + · by_cases epv : p = vc + · subst epv + refine Or.inr (Or.inr ⟨xv, { xv with vcpuExt := { xv.vcpuExt with runCaller := none } }, hkv, + by simp [Function.update, ept], rfl, rfl, rfl, rfl, rfl, rfl⟩) + · exact Or.inl (by simp [Function.update, ept, epv]) + +/-- **Live `vcpu.cancel_run` on `JStage3TA`** (matching `handoff/s4-vcpu-arms-stmt.lean` Section 2 +verbatim): clears `t`'s `RUN_VCPU` (`t + 3672`) and (when `t` was running `vc ≠ 0`) `vc`'s +`RUN_CALLER` (`vc + 24`); preserves `JStage3TA`, `SchedValidA`, `VcpuRec6A`, `Clock`, spec time, +and `OSKeep`. -/ +theorem vcpu_cancel_run_stage3TA + {AR : A → HwRootA → Prop} {Clock : A → Fl4SpecA64.MachineState → Prop} + {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {N E Rp : Set U64} {Bf : Set (U64 × Fl4SpecA64.Word)} + {stack : U64 → List U64} {κ : U64 → U64} {n : Nat} {J7 : JSlotA S} + [J7PpA st J7] [J7PpKA st J7] [J7FrA st J7] + {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 s a σ m) + (hSV : SchedValidA σ) + (R6 : VcpuRec6A st Z OS Bf s σ) + (hCl : Clock a σ.machineState) + {t : U64} (ht : t ∈ Z.T) : + vcpu.cancel_run st ar s a t ⦃ res => + ∃ σ' : Fl4SpecA64.State Unit, + res.2 = a ∧ + ((), σ') ∈ (fl4CancelRun (Z := Unit) (specPtr t) σ).1 ∧ + JStage3TA st AR Z H OS U Ac Pp Tb Lv ia ie N E Rp Bf stack κ n (J5A st) J7 res.1 a σ' m ∧ + SchedValidA σ' ∧ + VcpuRec6A st Z OS Bf res.1 σ' ∧ + Clock a σ'.machineState ∧ + σ'.machineState.timeState = σ.machineState.timeState ∧ + σ'.machineState.lastMachineTime = σ.machineState.lastMachineTime ∧ + L.mem res.1 (runVcpuA t) = 0#u64 ∧ + (L.mem s (runVcpuA t) ≠ 0#u64 → L.mem res.1 (wa (L.mem s (runVcpuA t)) 24#u64) = 0#u64) ∧ + L.ksb res.1 = L.ksb s ∧ L.cpu res.1 = L.cpu s ∧ + OSKeep st OS s res.1 ∧ + WritesIn st s res.1 (VcpuWrNoStA st Z OS s) ∧ + VcpuSpecStepA H σ σ' ∧ + FrKeepA st OS s σ res.1 σ' ⦄ := by + unfold vcpu.cancel_run + have hrun_off : layout.tcb.RUN_VCPU = ok 3672#u64 := + eq_of_spec_fd Fl4ProofsA64.P0.Consts.layout.tcb.RUN_VCPU.spec + rw [hrun_off, bind_ok] + simp only [lift, bind_ok, L.read_eq] + have hrunA : wa t 3672#u64 = runVcpuA t := by unfold runVcpuA; rfl + rw [hrunA] + obtain ⟨xt, hkt, -, hrv_t⟩ := J.j.2.1.tcb t ht + by_cases hr0 : L.mem s (runVcpuA t) = 0#u64 + · have hne0 : (L.mem s (runVcpuA t) != 0#u64) = false := by rw [hr0]; rfl + rw [hne0]; simp only [Bool.false_eq_true, ↓reduceIte]; rw [WP.spec_ok] + have hnone : xt.tcbExt.tcbRunVcpu = none := by rw [hrv_t, hr0]; unfold optRefA; simp + have hspec : ((), σ) ∈ (fl4CancelRun (Z := Unit) (specPtr t) σ).1 := by + rw [fl4CancelRun_noneA hkt hnone]; simp + exact ⟨σ, rfl, hspec, J, hSV, R6, hCl, rfl, rfl, hr0, fun h => absurd hr0 h, + rfl, rfl, fun _ _ => ⟨rfl, rfl, rfl, rfl⟩, + ⟨rfl, rfl, fun _ _ _ => rfl⟩, VcpuSpecStepA.refl H σ, FrKeepA.refl⟩ + · have hne0 : (L.mem s (runVcpuA t) != 0#u64) = true := bne_iff_ne.mpr hr0 + rw [hne0]; simp only [↓reduceIte] + set vc := L.mem s (runVcpuA t) with hvc_def + have hvcV : vc ∈ Z.V := (J.vcRun.run t ht).resolve_left hr0 + have hrc_eq : L.mem s (wa vc 24#u64) = t := R6.runBack t ht hr0 + have hrv_some : xt.tcbExt.tcbRunVcpu = some (specPtr vc) := by + rw [hrv_t]; unfold optRefA; simp [hr0] + obtain ⟨xv, hkv, -, -⟩ := J.j.2.1.obj vc hvcV + simp only [vcpu.RUN_CALLER, bind_ok, L.write_eq, WP.spec_ok] + set s1 := L.wr s (wa vc 24#u64) 0#u64 with hs1_def + set s2 := L.wr s1 (runVcpuA t) 0#u64 with hs2_def + set σ' : Fl4SpecA64.State Unit := + { σ with kheap := (Function.update + (Function.update σ.kheap (specPtr vc) (some (.archObj (.vcpu { xv with vcpuExt := { xv.vcpuExt with runCaller := none } })))) + (specPtr t) (some (.tcb { xt with tcbExt := { xt.tcbExt with tcbRunVcpu := none } }))) } with hσ'_def + have hspec : ((), σ') ∈ (fl4CancelRun (Z := Unit) (specPtr t) σ).1 := by + rw [fl4CancelRun_someA hkt hkv hrv_some]; simp [hσ'_def, updKSc] + have hHS : VcpuSpecStepA H σ σ' := fl4CancelRun_vcpuStepA hkt hkv + have htH : H t = some .tcb := show t ∈ H.tcbs by rw [J.tcbs]; exact ht + have hvH : H vc = some (.other 12) := by + obtain ⟨_, _, -, arm, -, -, -, -, -, -⟩ := J7FrA.fr (st := st) _ _ _ _ _ _ J.j7 + exact arm.vh vc hvcV + have al_wa4096 : ∀ {p o : U64}, p.val + 4096 ≤ 2 ^ 64 → p.val % 4096 = 0 → + o.val < 4096 → o.val % 8 = 0 → Aligned (wa p o) := fun {p o} hf ha ho1 ho8 => by + have ev : (wa p o).val = p.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + show (wa p o).val % 8 = 0; rw [ev]; omega + have al_tcb : ∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → Aligned (wa u K) := fun u hu K hK h8 => by + have huH : H u = some .tcb := by rw [← J.tcbs] at hu; exact hu + exact al_wa4096 (J.ent.heap.fits u .tcb huH) (J.ent.heap.align u .tcb huH) hK h8 + have al_vc : ∀ v ∈ Z.V, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → Aligned (wa v o) := fun v hv o ho h8 => by + obtain ⟨_, _, -, arm, -, -, -, -, -, -⟩ := J7FrA.fr (st := st) _ _ _ _ _ _ J.j7 + have hv'H := arm.vh v hv + exact al_wa4096 (J.ent.heap.fits v _ hv'H) (J.ent.heap.align v _ hv'H) ho h8 + have hal_vc24 : Aligned (wa vc 24#u64) := al_vc vc hvcV 24#u64 (by change 24 < 4096; omega) (by change 24 % 8 = 0; omega) + have hal_rt : Aligned (runVcpuA t) := al_tcb t ht 3672#u64 (by change 3672 < 4096; omega) (by change 3672 % 8 = 0; omega) + have hne_addr : wa vc 24#u64 ≠ runVcpuA t := by + intro e + have hfit_v := J.ent.heap.fits vc _ hvH; change vc.val + 4096 ≤ 2 ^ 64 at hfit_v + have hfit_t := J.ent.heap.fits t .tcb htH; change t.val + 4096 ≤ 2 ^ 64 at hfit_t + have hne_vt : vc ≠ t := fun h => by rw [h, htH] at hvH; cases hvH + have hd := J.ent.heap.disj vc (.other 12) t .tcb hvH htH hne_vt + change vc.val + 4096 ≤ t.val ∨ t.val + 4096 ≤ vc.val at hd + have h24v : (24#u64).val = 24 := rfl + have h3672v : (3672#u64).val = 3672 := rfl + have e1 : (wa vc 24#u64).val = vc.val + 24 := by rw [wa_valm, h24v]; exact Nat.mod_eq_of_lt (by omega) + have e2 : (runVcpuA t).val = t.val + 3672 := by unfold runVcpuA; rw [wa_valm, h3672v]; exact Nat.mod_eq_of_lt (by omega) + have := congrArg UScalar.val e; omega + have hw : WritesIn st s s2 (VcpuWrNoStA st Z OS s) := by + refine ⟨by rw [hs2_def, hs1_def, L.ksb_wr, L.ksb_wr], + by rw [hs2_def, hs1_def, L.cpu_wr, L.cpu_wr], fun b hb hnot => ?_⟩ + have hne1 : (runVcpuA t).val ≠ b.val := fun e => + hnot (Or.inr (Or.inl ⟨t, ht, 3672#u64, by change (3672 : Nat) % 8 = 0; omega, Or.inr (Or.inr rfl), (UScalar.eq_of_val_eq e).symm⟩)) + have hne2 : (wa vc 24#u64).val ≠ b.val := fun e => + hnot (Or.inl ⟨vc, hvcV, Or.inr (Or.inr (UScalar.eq_of_val_eq e).symm)⟩) + rw [hs2_def, L.mem_wr_ne hal_rt hb hne1, hs1_def, L.mem_wr_ne hal_vc24 hb hne2] + have hmem_rt : L.mem s2 (runVcpuA t) = 0#u64 := by + rw [hs2_def, L.mem_wr_same hal_rt] + have hmem_vc24 : L.mem s2 (wa vc 24#u64) = 0#u64 := by + rw [hs2_def, L.mem_wr_ne hal_rt hal_vc24 (fun e => hne_addr (UScalar.eq_of_val_eq e).symm), hs1_def, L.mem_wr_same hal_vc24] + have hmem_other : ∀ b, Aligned b → b ≠ wa vc 24#u64 → b ≠ runVcpuA t → L.mem s2 b = L.mem s b := + fun b hb h1 h2 => by + rw [hs2_def, L.mem_wr_ne hal_rt hb (fun e => h2 (UScalar.eq_of_val_eq e).symm), + hs1_def, L.mem_wr_ne hal_vc24 hb (fun e => h1 (UScalar.eq_of_val_eq e).symm)] + have hne_spec : specPtr t ≠ specPtr vc := fun e => by + have : t = vc := specPtr_inj e + rw [this, hvH] at htH; cases htH + -- Helper: disjointness of `wa u K` from `wa vc 24#u64` and `runVcpuA t` + have hdisj_tcb_vc : ∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → wa u K ≠ wa vc 24#u64 := fun u hu K hK e => by + have huH : H u = some .tcb := show u ∈ H.tcbs by rw [J.tcbs]; exact hu + have hfit_u := J.ent.heap.fits u .tcb huH; change u.val + 4096 ≤ 2 ^ 64 at hfit_u + have hfit_v := J.ent.heap.fits vc _ hvH; change vc.val + 4096 ≤ 2 ^ 64 at hfit_v + have hne_uv : u ≠ vc := fun h => by rw [h, hvH] at huH; cases huH + have hd := J.ent.heap.disj u .tcb vc (.other 12) huH hvH hne_uv + change u.val + 4096 ≤ vc.val ∨ vc.val + 4096 ≤ u.val at hd + have h24v : (24#u64).val = 24 := rfl + have e1 : (wa u K).val = u.val + K.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have e2 : (wa vc 24#u64).val = vc.val + 24 := by rw [wa_valm, h24v]; exact Nat.mod_eq_of_lt (by omega) + have := congrArg UScalar.val e; omega + have hdisj_vc_t : ∀ v ∈ Z.V, ∀ o : U64, o.val < 4096 → wa v o ≠ runVcpuA t := fun v hv o ho e => by + obtain ⟨_, _, -, arm, -, -, -, -, -, -⟩ := J7FrA.fr (st := st) _ _ _ _ _ _ J.j7 + have hv'H := arm.vh v hv + have hfit_v := J.ent.heap.fits v _ hv'H; change v.val + 4096 ≤ 2 ^ 64 at hfit_v + have hfit_t := J.ent.heap.fits t .tcb htH; change t.val + 4096 ≤ 2 ^ 64 at hfit_t + have hne_vt : v ≠ t := fun h => by rw [h, htH] at hv'H; cases hv'H + have hd := J.ent.heap.disj v (.other 12) t .tcb hv'H htH hne_vt + change v.val + 4096 ≤ t.val ∨ t.val + 4096 ≤ v.val at hd + have h3672v : (3672#u64).val = 3672 := rfl + have e1 : (wa v o).val = v.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have e2 : (runVcpuA t).val = t.val + 3672 := by unfold runVcpuA; rw [wa_valm, h3672v]; exact Nat.mod_eq_of_lt (by omega) + have := congrArg UScalar.val e; omega + have hdisj_tcb_t : ∀ u ∈ Z.T, ∀ K : U64, K.val < 4096 → (u ≠ t ∨ K.val ≠ 3672) → wa u K ≠ runVcpuA t := + fun u hu K hK hor e => by + have huH : u ∈ H.tcbs := by rw [J.tcbs]; exact hu + have hfit_u := J.ent.heap.fits u .tcb huH; change u.val + 4096 ≤ 2 ^ 64 at hfit_u + have hfit_t := J.ent.heap.fits t .tcb htH; change t.val + 4096 ≤ 2 ^ 64 at hfit_t + have h3672v : (3672#u64).val = 3672 := rfl + have e1 : (wa u K).val = u.val + K.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have e2 : (runVcpuA t).val = t.val + 3672 := by unfold runVcpuA; rw [wa_valm, h3672v]; exact Nat.mod_eq_of_lt (by omega) + have := congrArg UScalar.val e + by_cases eu : u = t + · subst eu; rcases hor with h | h + · exact h rfl + · omega + · have hd := J.ent.heap.disj u .tcb t .tcb huH htH eu + change u.val + 4096 ≤ t.val ∨ t.val + 4096 ≤ u.val at hd; omega + have hdisj_vc_vc : ∀ v ∈ Z.V, ∀ o : U64, o.val < 4096 → (v ≠ vc ∨ o.val ≠ 24) → wa v o ≠ wa vc 24#u64 := + fun v hv o ho hor e => by + obtain ⟨_, _, -, arm, -, -, -, -, -, -⟩ := J7FrA.fr (st := st) _ _ _ _ _ _ J.j7 + have hv'H := arm.vh v hv + have hfit_v := J.ent.heap.fits v _ hv'H; change v.val + 4096 ≤ 2 ^ 64 at hfit_v + have hfit_vc := J.ent.heap.fits vc _ hvH; change vc.val + 4096 ≤ 2 ^ 64 at hfit_vc + have h24v : (24#u64).val = 24 := rfl + have e1 : (wa v o).val = v.val + o.val := by rw [wa_valm]; exact Nat.mod_eq_of_lt (by omega) + have e2 : (wa vc 24#u64).val = vc.val + 24 := by rw [wa_valm, h24v]; exact Nat.mod_eq_of_lt (by omega) + have hval := congrArg UScalar.val e + by_cases ev : v = vc + · subst ev; rcases hor with h | h + · exact h rfl + · omega + · have hd := J.ent.heap.disj v (.other 12) vc (.other 12) hv'H hvH ev + change v.val + 4096 ≤ vc.val ∨ vc.val + 4096 ≤ v.val at hd; omega + have hVcCore : VcpuCoreA (L := L) Z s2 σ' := by + have osk := osKeep_of_vcpuWrA J R6.vcOS R6.frOff hw + refine ⟨fun v hv => ?_, fun p x hx => ?_, fun u hu => ?_, fun y hy p hc => ?_⟩ + · obtain ⟨_, _, -, arm, -, -, -, -, -, -⟩ := J7FrA.fr (st := st) _ _ _ _ _ _ J.j7 + have hv'H := arm.vh v hv + have hal0 : Aligned v := by have := al_vc v hv 0#u64 (by change 0 < 4096; omega) (by change 0 % 8 = 0; omega); rwa [wa_zeroVcS] at this + have hal24 : Aligned (wa v 24#u64) := al_vc v hv 24#u64 (by change 24 < 4096; omega) (by change 24 % 8 = 0; omega) + have hm0 : L.mem s2 v = L.mem s v := by + have h1 := hdisj_vc_vc v hv 0#u64 (by change 0 < 4096; omega) (Or.inr (by change 0 ≠ 24; omega)) + have h2 := hdisj_vc_t v hv 0#u64 (by change 0 < 4096; omega) + rw [wa_zeroVcS] at h1 h2; exact hmem_other v hal0 h1 h2 + by_cases ev : v = vc + · subst ev + obtain ⟨xv0, hkv0, h1, -⟩ := J.j.2.1.obj vc hvcV + rw [hkv] at hkv0; cases hkv0 + refine ⟨{ xv with vcpuExt := { xv.vcpuExt with runCaller := none } }, + by simp [hσ'_def, Function.update, hne_spec.symm], by rw [hm0]; exact h1, by rw [hmem_vc24]; rfl⟩ + · obtain ⟨xv0, hkv0, h1, h2⟩ := J.j.2.1.obj v hv + have hne_s1 : specPtr v ≠ specPtr t := fun e => by + have : v = t := specPtr_inj e; rw [this, htH] at hv'H; cases hv'H + have hne_s2 : specPtr v ≠ specPtr vc := fun e => ev (specPtr_inj e) + have hm24 : L.mem s2 (wa v 24#u64) = L.mem s (wa v 24#u64) := + hmem_other _ hal24 (hdisj_vc_vc v hv 24#u64 (by change 24 < 4096; omega) (Or.inl ev)) (hdisj_vc_t v hv 24#u64 (by change 24 < 4096; omega)) + exact ⟨xv0, by simp [hσ'_def, Function.update, hne_s1, hne_s2, hkv0], + by rw [hm0]; exact h1, by rw [hm24]; exact h2⟩ + · rcases hHS.kh p with e | ⟨_, _, _, h2, -⟩ | ⟨v0, _, h1, h2, -⟩ + · exact J.j.2.1.back p x (by rw [← e]; exact hx) + · rw [hx] at h2; cases h2 + · rw [hx] at h2; cases h2; exact J.j.2.1.back p v0 h1 + · have huH : H u = some .tcb := show u ∈ H.tcbs by rw [J.tcbs]; exact hu + have hal3664 : Aligned (wa u 3664#u64) := al_tcb u hu 3664#u64 (by change 3664 < 4096; omega) (by change 3664 % 8 = 0; omega) + have hal3672 : Aligned (runVcpuA u) := al_tcb u hu 3672#u64 (by change 3672 < 4096; omega) (by change 3672 % 8 = 0; omega) + have hm3664 : L.mem s2 (wa u 3664#u64) = L.mem s (wa u 3664#u64) := + hmem_other _ hal3664 (hdisj_tcb_vc u hu 3664#u64 (by change 3664 < 4096; omega)) + (hdisj_tcb_t u hu 3664#u64 (by change 3664 < 4096; omega) (Or.inr (by change 3664 ≠ 3672; omega))) + by_cases eu : u = t + · subst u + obtain ⟨xt0, hkt0, h1, -⟩ := J.j.2.1.tcb t ht + rw [hkt] at hkt0; cases hkt0 + exact ⟨{ xt with tcbExt := { xt.tcbExt with tcbRunVcpu := none } }, + by simp [hσ'_def, Function.update], by rw [hm3664]; exact h1, by rw [hmem_rt]; rfl⟩ + · obtain ⟨xt0, hkt0, h1, h2⟩ := J.j.2.1.tcb u hu + have hne_s1 : specPtr u ≠ specPtr t := fun e => eu (specPtr_inj e) + have hne_s2 : specPtr u ≠ specPtr vc := fun e => by + have : u = vc := specPtr_inj e; rw [this, hvH] at huH; cases huH + have hm3672 : L.mem s2 (runVcpuA u) = L.mem s (runVcpuA u) := + hmem_other _ hal3672 (hdisj_tcb_vc u hu 3672#u64 (by change 3672 < 4096; omega)) + (hdisj_tcb_t u hu 3672#u64 (by change 3672 < 4096; omega) (Or.inl eu)) + exact ⟨xt0, by simp [hσ'_def, Function.update, hne_s1, hne_s2, hkt0], + by rw [hm3664]; exact h1, by rw [hm3672]; exact h2⟩ + · have hyOS : y ∈ OS := J.j5.2.1 y hy + unfold capAtA at hc; rw [(osk y hyOS).1, (osk y hyOS).2.1] at hc + exact J.j.2.1.cap y hy p hc + have hVcRun : VcRunA st Z.T Z.V s2 := by + refine ⟨fun u hu => ?_, fun v hv => ?_, fun v hv => ?_, fun u hu => ?_, fun u hu h => ?_⟩ + · have hal : Aligned (wa u 3664#u64) := al_tcb u hu 3664#u64 (by change 3664 < 4096; omega) (by change 3664 % 8 = 0; omega) + rw [hmem_other _ hal (hdisj_tcb_vc u hu 3664#u64 (by change 3664 < 4096; omega)) + (hdisj_tcb_t u hu 3664#u64 (by change 3664 < 4096; omega) (Or.inr (by change 3664 ≠ 3672; omega)))] + exact J.vcRun.tcb u hu + · by_cases ev : v = vc + · subst ev; exact Or.inl hmem_vc24 + · have hal : Aligned (wa v 24#u64) := al_vc v hv 24#u64 (by change 24 < 4096; omega) (by change 24 % 8 = 0; omega) + rw [hmem_other _ hal (hdisj_vc_vc v hv 24#u64 (by change 24 < 4096; omega) (Or.inl ev)) + (hdisj_vc_t v hv 24#u64 (by change 24 < 4096; omega))] + exact J.vcRun.vcpu v hv + · have hal : Aligned v := by have := al_vc v hv 0#u64 (by change 0 < 4096; omega) (by change 0 % 8 = 0; omega); rwa [wa_zeroVcS] at this + have h1 := hdisj_vc_vc v hv 0#u64 (by change 0 < 4096; omega) (Or.inr (by change 0 ≠ 24; omega)) + have h2 := hdisj_vc_t v hv 0#u64 (by change 0 < 4096; omega) + rw [wa_zeroVcS] at h1 h2; rw [hmem_other v hal h1 h2] + exact J.vcRun.vtcb v hv + · by_cases eu : u = t + · subst eu; exact Or.inl hmem_rt + · have hal : Aligned (runVcpuA u) := al_tcb u hu 3672#u64 (by change 3672 < 4096; omega) (by change 3672 % 8 = 0; omega) + rw [hmem_other _ hal (hdisj_tcb_vc u hu 3672#u64 (by change 3672 < 4096; omega)) + (hdisj_tcb_t u hu 3672#u64 (by change 3672 < 4096; omega) (Or.inl eu))] + exact J.vcRun.run u hu + · by_cases eu : u = t + · subst eu; exact hmem_rt + · have hal : Aligned (wa u 3664#u64) := al_tcb u hu 3664#u64 (by change 3664 < 4096; omega) (by change 3664 % 8 = 0; omega) + rw [hmem_other _ hal (hdisj_tcb_vc u hu 3664#u64 (by change 3664 < 4096; omega)) + (hdisj_tcb_t u hu 3664#u64 (by change 3664 < 4096; omega) (Or.inr (by change 3664 ≠ 3672; omega)))] at h + have hal2 : Aligned (runVcpuA u) := al_tcb u hu 3672#u64 (by change 3672 < 4096; omega) (by change 3672 % 8 = 0; omega) + rw [hmem_other _ hal2 (hdisj_tcb_vc u hu 3672#u64 (by change 3672 < 4096; omega)) + (hdisj_tcb_t u hu 3672#u64 (by change 3672 < 4096; omega) (Or.inl eu))] + exact J.vcRun.disj u hu h + have hRunVcpu : RunVcpuInv st Z.T s2 := fun u hu => by + have hst_eq : L.mem s2 (stA u) = L.mem s (stA u) := by + rw [stA_eq] + exact hmem_other _ (al_tcb u hu 3328#u64 (by change 3328 < 4096; omega) (by change 3328 % 8 = 0; omega)) + (hdisj_tcb_vc u hu 3328#u64 (by change 3328 < 4096; omega)) + (hdisj_tcb_t u hu 3328#u64 (by change 3328 < 4096; omega) (Or.inr (by change 3328 ≠ 3672; omega))) + by_cases eu : u = t + · subst u; intro h; exact absurd hmem_rt h + · have hal_rv : Aligned (runVcpuA u) := al_tcb u hu 3672#u64 (by change 3672 < 4096; omega) (by change 3672 % 8 = 0; omega) + rw [hst_eq, hmem_other _ hal_rv (hdisj_tcb_vc u hu 3672#u64 (by change 3672 < 4096; omega)) + (hdisj_tcb_t u hu 3672#u64 (by change 3672 < 4096; omega) (Or.inl eu))] + exact J.runVcpu u hu + have hEpTcb : ∀ u ∈ Z.T, ∃ x, σ'.kheap (specPtr u) = some (.tcb x) ∧ TcbRelE Rp (L.mem s2) u x := fun u hu => by + obtain ⟨x0, hkx0, hrel0⟩ := J.ep.tcb u hu + have huH : H u = some .tcb := show u ∈ H.tcbs by rw [J.tcbs]; exact hu + have hfit_u := J.ent.heap.fits u .tcb huH; change u.val + 4096 ≤ 2 ^ 64 at hfit_u + have hrel_s2 : TcbRelE Rp (L.mem s2) u x0 := by + refine hrel0.frame (J.sq.tcbs u huH) fun a h8 ha => ?_ + have hlt : a.val - u.val < 3664 := by + have := ha.2; unfold TOffN at this; omega + have ha_eq : a = wa u ⟨BitVec.ofNat 64 (a.val - u.val)⟩ := by + apply UScalar.eq_of_val_eq; rw [wa_valm] + have hdv : (⟨BitVec.ofNat 64 (a.val - u.val)⟩ : U64).val = a.val - u.val := by + show (BitVec.ofNat 64 _).toNat = _; rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + rw [hdv, Nat.add_sub_of_le ha.1]; exact (Nat.mod_eq_of_lt (by omega)).symm + rw [ha_eq] + have hdv : (⟨BitVec.ofNat 64 (a.val - u.val)⟩ : U64).val = a.val - u.val := by + show (BitVec.ofNat 64 _).toNat = _; rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + exact hmem_other _ (by rw [← ha_eq]; exact h8) (hdisj_tcb_vc u hu _ (by omega)) + (hdisj_tcb_t u hu _ (by omega) (Or.inr (by omega))) + by_cases eu : u = t + · subst eu; rw [hkt] at hkx0; cases hkx0 + exact ⟨{ xt with tcbExt := { xt.tcbExt with tcbRunVcpu := none } }, + by simp [hσ'_def, Function.update], { hrel_s2 with }⟩ + · have hne_s1 : specPtr u ≠ specPtr t := fun e => eu (specPtr_inj e) + have hne_s2 : specPtr u ≠ specPtr vc := fun e => by + have : u = vc := specPtr_inj e; rw [this, hvH] at huH; cases huH + exact ⟨x0, by simp [hσ'_def, Function.update, hne_s1, hne_s2, hkx0], hrel_s2⟩ + have hEpBuf : ∀ bp ∈ Bf, ∀ k : U64, k.val < 128 → + (L.mem s2 (bufW bp.1 k)).bv = loadWordAt σ'.machineState.underlyingMemory (bufAddr bp.2 k.val) ∧ + (bufAddr bp.2 k.val).toNat % 8 = 0 := fun bp hbp k hk => by + have ⟨h1, h2⟩ := J.ep.buf bp hbp k hk + have hbufW : BufW Bf (bufW bp.1 k) := ⟨bp, hbp, k, hk, rfl⟩ + have hbw : L.mem s2 (bufW bp.1 k) = L.mem s (bufW bp.1 k) := by + refine hmem_other _ (J.iplaced.e.balign _ hbufW) + (J.bufOff.vcw _ hbufW vc hvcV 24#u64 (by decide) (by decide)) ?_ + intro e + have hfit_t := J.ent.heap.fits t .tcb htH; change t.val + 4096 ≤ 2 ^ 64 at hfit_t + have h3672 : (3672#u64).val = 3672 := rfl + have ev : (runVcpuA t).val = t.val + 3672 := by unfold runVcpuA; rw [wa_valm, h3672]; exact Nat.mod_eq_of_lt (by omega) + have hbt := J.iplaced.e.bt _ hbufW t ht + rw [e, ev] at hbt; omega + rw [hbw]; exact ⟨h1, h2⟩ + have osk := osKeep_of_vcpuWrA J R6.vcOS R6.frOff hw + have capE : ∀ y ∈ OS, capA st s2 y = capA st s y := fun y hy => by + unfold capA; rw [(osk y hy).1, (osk y hy).2.1] + have hDeadVcpu : ∀ v ∈ Z.V, DeadA st OS .vcpu s2 v → ∀ x, σ'.kheap (specPtr v) = some (.archObj (.vcpu x)) → + x.vcpuTcb = none := fun v hv hd x hx => by + have hd0 : DeadA st OS .vcpu s v := fun y hy hc => hd y hy (by rw [capE y hy]; exact hc) + by_cases ev : v = vc + · subst ev; simp [hσ'_def, Function.update, hne_spec.symm] at hx; subst hx + exact J.deadExt.vcpu vc hvcV hd0 xv hkv + · have hne_s1 : specPtr v ≠ specPtr t := fun e => by + obtain ⟨_, _, -, arm, -, -, -, -, -, -⟩ := J7FrA.fr (st := st) _ _ _ _ _ _ J.j7 + have : v = t := specPtr_inj e + have hv'H := arm.vh v hv; rw [this, htH] at hv'H; cases hv'H + have hne_s2 : specPtr v ≠ specPtr vc := fun e => ev (specPtr_inj e) + simp [hσ'_def, Function.update, hne_s1, hne_s2] at hx + exact J.deadExt.vcpu v hv hd0 x hx + have hDeadTvcpu : ∀ u ∈ Z.T, DeadA st OS .tcb s2 u → L.mem s2 (wa u 3664#u64) = 0#u64 := fun u hu hd => by + have hd0 : DeadA st OS .tcb s u := fun y hy hc => hd y hy (by rw [capE y hy]; exact hc) + have hal : Aligned (wa u 3664#u64) := al_tcb u hu 3664#u64 (by change 3664 < 4096; omega) (by change 3664 % 8 = 0; omega) + rw [hmem_other _ hal (hdisj_tcb_vc u hu 3664#u64 (by change 3664 < 4096; omega)) + (hdisj_tcb_t u hu 3664#u64 (by change 3664 < 4096; omega) (Or.inr (by change 3664 ≠ 3672; omega)))] + exact J.deadExt.tvcpu u hu hd0 + have hDeadVcRun : ∀ v ∈ Z.V, DeadA st OS .vcpu s2 v → L.mem s2 (wa v 24#u64) = 0#u64 := fun v hv hd => by + by_cases ev : v = vc + · subst ev; exact hmem_vc24 + · have hd0 : DeadA st OS .vcpu s v := fun y hy hc => hd y hy (by rw [capE y hy]; exact hc) + have hal : Aligned (wa v 24#u64) := al_vc v hv 24#u64 (by change 24 < 4096; omega) (by change 24 % 8 = 0; omega) + rw [hmem_other _ hal (hdisj_vc_vc v hv 24#u64 (by change 24 < 4096; omega) (Or.inl ev)) + (hdisj_vc_t v hv 24#u64 (by change 24 < 4096; omega))] + exact J.deadExt.vcDeadRun v hv hd0 + have hZombNoVc : ZombieTcbNoVcpuA st OS s2 := fun y hy u num hz => by + have hz0 : capA st s y = .Zombie u capability.ZOMBIE_TCB num := by rwa [capE y hy] at hz + have huT : u ∈ Z.T := J.tcbs ▸ J.zombieTcb y hy u num hz0 + have hal : Aligned (wa u 3664#u64) := al_tcb u huT 3664#u64 (by change 3664 < 4096; omega) (by change 3664 % 8 = 0; omega) + rw [hmem_other _ hal (hdisj_tcb_vc u huT 3664#u64 (by change 3664 < 4096; omega)) + (hdisj_tcb_t u huT 3664#u64 (by change 3664 < 4096; omega) (Or.inr (by change 3664 ≠ 3672; omega)))] + exact J.zombieNoVcpu y hy u num hz0 + have hUtVs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → L.mem s2 b = L.mem s b := by + intro b hb + have hno := utVs_off_vcpuNoFrA J b hb + exact hmem_other b (J.vsOk.al b hb) + (fun e => hno (Or.inl ⟨vc, hvcV, Or.inr (Or.inr e)⟩)) + (fun e => hno (Or.inr ⟨t, ht, 3672#u64, by change (3672 : Nat) % 8 = 0; omega, Or.inr (Or.inr rfl), e⟩)) + have J' := JStage3TA.of_vcpuStepA J R6 hw hHS J.j.1.ar hUtVs hVcCore hVcRun hRunVcpu + hEpTcb hEpBuf hDeadVcpu hDeadTvcpu hDeadVcRun hZombNoVc + have hSV' := SchedValidA.of_vcpuStepA hSV hHS + have hSnap' := VcpuStateSnapA.of_vcpuStepA J R6.snap hw hHS (fun v hv => by + rw [hmem_other _ (al_vc v hv 8#u64 (by change 8 < 4096; omega) (by change 8 % 8 = 0; omega)) + (hdisj_vc_vc v hv 8#u64 (by change 8 < 4096; omega) (Or.inr (by change (8 : Nat) ≠ 24; omega))) + (hdisj_vc_t v hv 8#u64 (by change 8 < 4096; omega))] + exact JStage3TA.ownLeA J R6.snap v hv) + have hFrOff' : FrOff st Z.Cs OS (L.ksb s2) s2 := by + rw [hw.1] + intro u hu ptr sz r mp ept asid va hc bits hbits + exact R6.frOff u hu ptr sz r mp ept asid va ((capE u hu).symm.trans hc) bits hbits + have hFrTh' : FrThA st Z.th OS s2 := R6.frTh.keepA capE fun _ _ e => e + have hUserFr' : UserFrSpecA st OS s2 σ' := + R6.userFr.keepA capE fun _ _ _ h => hHS.keepObj h (fun _ e => by cases e) (fun _ e => by cases e) + have hR6' : VcpuRec6A st Z OS Bf s2 σ' := by + refine ⟨hSnap', fun u hu => ?_, fun v hv => ?_, fun v hv hv0 => ?_, fun u hu hu0 => ?_, + R6.vcOS, hFrOff', hUserFr', hFrTh', R6.bfSpec⟩ + · have hal3664 : Aligned (wa u 3664#u64) := al_tcb u hu 3664#u64 (by change 3664 < 4096; omega) (by change 3664 % 8 = 0; omega) + have h3664 : L.mem s2 (wa u 3664#u64) = L.mem s (wa u 3664#u64) := + hmem_other _ hal3664 (hdisj_tcb_vc u hu 3664#u64 (by change 3664 < 4096; omega)) + (hdisj_tcb_t u hu 3664#u64 (by change 3664 < 4096; omega) (Or.inr (by change 3664 ≠ 3672; omega))) + rw [h3664]; intro hne + have hvV' : L.mem s (wa u 3664#u64) ∈ Z.V := (J.vcRun.tcb u hu).resolve_left hne + have hal0 : Aligned (L.mem s (wa u 3664#u64)) := by + have := al_vc _ hvV' 0#u64 (by change 0 < 4096; omega) (by change 0 % 8 = 0; omega); rwa [wa_zeroVcS] at this + have h1 := hdisj_vc_vc _ hvV' 0#u64 (by change 0 < 4096; omega) (Or.inr (by change 0 ≠ 24; omega)) + have h2 := hdisj_vc_t _ hvV' 0#u64 (by change 0 < 4096; omega) + rw [wa_zeroVcS] at h1 h2; rw [hmem_other _ hal0 h1 h2] + exact R6.vcSym1 u hu hne + · have hal24 : Aligned (wa v 24#u64) := al_vc v hv 24#u64 (by change 24 < 4096; omega) (by change 24 % 8 = 0; omega) + by_cases ev : v = vc + · subst ev; intro h; exact absurd hmem_vc24 h + · have h24 : L.mem s2 (wa v 24#u64) = L.mem s (wa v 24#u64) := + hmem_other _ hal24 (hdisj_vc_vc v hv 24#u64 (by change 24 < 4096; omega) (Or.inl ev)) + (hdisj_vc_t v hv 24#u64 (by change 24 < 4096; omega)) + rw [h24]; intro hne + have huT : L.mem s (wa v 24#u64) ∈ Z.T := (J.vcRun.vcpu v hv).resolve_left hne + have hne_ut : L.mem s (wa v 24#u64) ≠ t := fun e => by + have := R6.vcSym2 v hv hne; rw [e] at this; exact ev this.symm + have hal_rv : Aligned (runVcpuA (L.mem s (wa v 24#u64))) := al_tcb _ huT 3672#u64 (by change 3672 < 4096; omega) (by change 3672 % 8 = 0; omega) + rw [hmem_other _ hal_rv (hdisj_tcb_vc _ huT 3672#u64 (by change 3672 < 4096; omega)) + (hdisj_tcb_t _ huT 3672#u64 (by change 3672 < 4096; omega) (Or.inl hne_ut))] + exact R6.vcSym2 v hv hne + · have hal0 : Aligned v := by have := al_vc v hv 0#u64 (by change 0 < 4096; omega) (by change 0 % 8 = 0; omega); rwa [wa_zeroVcS] at this + have h1 := hdisj_vc_vc v hv 0#u64 (by change 0 < 4096; omega) (Or.inr (by change 0 ≠ 24; omega)) + have h2 := hdisj_vc_t v hv 0#u64 (by change 0 < 4096; omega) + rw [wa_zeroVcS] at h1 h2 + rw [hmem_other v hal0 h1 h2] at hv0 ⊢ + have huT : L.mem s v ∈ Z.T := (J.vcRun.vtcb v hv).resolve_left hv0 + have hal3664 : Aligned (wa (L.mem s v) 3664#u64) := al_tcb _ huT 3664#u64 (by change 3664 < 4096; omega) (by change 3664 % 8 = 0; omega) + rw [hmem_other _ hal3664 (hdisj_tcb_vc _ huT 3664#u64 (by change 3664 < 4096; omega)) + (hdisj_tcb_t _ huT 3664#u64 (by change 3664 < 4096; omega) (Or.inr (by change 3664 ≠ 3672; omega)))] + exact R6.vcBack v hv hv0 + · by_cases eu : u = t + · subst eu; exact absurd hmem_rt hu0 + · have hal_rv : Aligned (runVcpuA u) := al_tcb u hu 3672#u64 (by change 3672 < 4096; omega) (by change 3672 % 8 = 0; omega) + rw [hmem_other _ hal_rv (hdisj_tcb_vc u hu 3672#u64 (by change 3672 < 4096; omega)) + (hdisj_tcb_t u hu 3672#u64 (by change 3672 < 4096; omega) (Or.inl eu))] at hu0 ⊢ + have hvV' : L.mem s (runVcpuA u) ∈ Z.V := (J.vcRun.run u hu).resolve_left hu0 + have hne_v : L.mem s (runVcpuA u) ≠ vc := fun e => by + have := R6.runBack u hu hu0; rw [e, hrc_eq] at this; exact eu this.symm + have hal24 : Aligned (wa (L.mem s (runVcpuA u)) 24#u64) := al_vc _ hvV' 24#u64 (by change 24 < 4096; omega) (by change 24 % 8 = 0; omega) + rw [hmem_other _ hal24 (hdisj_vc_vc _ hvV' 24#u64 (by change 24 < 4096; omega) (Or.inl hne_v)) + (hdisj_vc_t _ hvV' 24#u64 (by change 24 < 4096; omega))] + exact R6.runBack u hu hu0 + exact ⟨σ', True.intro, hspec, J', hSV', hR6', hCl, rfl, rfl, hmem_rt, fun _ => hmem_vc24, + hw.1, hw.2.1, osk, hw, hHS, FrKeepA.kerA J osk rfl fun b al nv nt => + hmem_other b al (fun e => nv ⟨vc, hvcV, 24#u64, by decide, by decide, e⟩) (nt t ht 3672#u64 (by decide))⟩ + +end steps +end +end Fl4ProofsA64.P3.Vcpu diff --git a/proofs/Fl4ProofsA64/P3/VcpuExitJA.lean b/proofs/Fl4ProofsA64/P3/VcpuExitJA.lean new file mode 100644 index 000000000..66048abaa --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/VcpuExitJA.lean @@ -0,0 +1,401 @@ +import Fl4ProofsA64.P3.VcpuTimestampA + +/-! # AArch64 P3 VCPU Guest Exits (`vcpu_run_exit_jA`, `vcpu_run_vppi_jA`) -/ + +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3.Vcpu + +open Fl4Spec Fl4SpecA64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3 Fl4ProofsA64.P3.Sched +open Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +private theorem runVcpuA_eq (t : U64) : runVcpuA t = wa t 3672#u64 := rfl + +private theorem fl4HandleVcpuRunExit_nonvppiA {σ σ1 σ2 : State Unit} {ct : ObjRef} + {stopSpec : RunStop} {w1 w2 w3 : Word} + (hne_vppi : stopSpec ≠ .vppi) + (hsp_ts : updateTimeStamp (Z := Unit) σ = ({((), σ1)}, False)) + (hct1 : σ1.curThread = ct) + (hsp_cr : fl4CompleteRun (Z := Unit) ct stopSpec w1 w2 w3 σ1 = ({((), σ2)}, False)) + (hcb_sp : checkBudget (Z := Unit) σ2 = ({(true, σ2)}, False)) : + fl4HandleVcpuRunExit (Z := Unit) ⟨stopSpec, w1, w2, w3⟩ σ = ({((), σ2)}, False) := by + unfold fl4HandleVcpuRunExit + rw [nd_bind_oneSc hsp_ts, nd_bind_oneSc (m := NonDet.gets _) rfl, hct1] + rw [nd_bind_oneSc (by unfold NonDet.when; rw [if_neg hne_vppi]; rfl)] + rw [nd_bind_oneSc hsp_cr, nd_bind_oneSc hcb_sp]; rfl + +private theorem spec_let_triple_okA {α β γ δ : Type} {a : α} {b : β} {c : γ} + {f : β → γ → δ} {P : δ → Prop} (h : P (f b c)) : + (let (_, x, y) := (a, b, c); ok (f x y)) ⦃ P ⦄ := (WP.spec_ok (f b c)).2 h + +private theorem vppi_when_eqA (ct : ObjRef) (irq : Word) (σ1 : State Unit) : + (do let vcOpt ← getRunVcpu (Z := Unit) ct + match vcOpt with + | some vc => NonDet.when (irq = 27) do + let v ← getVcpu vc + let mk := Function.update v.vcpuVppiMasked .vppiEventIRQ_VTimer true + setVcpu vc { v with vcpuVppiMasked := mk } + | none => pure () + doMachineOp (maskInterrupt true (ucast irqLen irq))) σ1 = + ((do let vcOpt ← getRunVcpu (Z := Unit) ct + match vcOpt with + | some vc => NonDet.when (irq = 27) do + let v ← getVcpu vc + let mk := Function.update v.vcpuVppiMasked .vppiEventIRQ_VTimer true + setVcpu vc { v with vcpuVppiMasked := mk } + | none => pure ()) >>= fun _ => doMachineOp (maskInterrupt true (ucast irqLen irq))) σ1 := by + rw [nd_bind_assocSc] + apply congrFun; apply congrArg (Bind.bind (getRunVcpu ct)); funext vcOpt + cases vcOpt with + | some vc => rfl + | none => funext s; rw [nd_bind_oneSc (m := pure ()) rfl] + +private theorem fl4HandleVcpuRunExit_vppiA {σ σ1 σ2 σ3 : State Unit} {ct : ObjRef} + {irq : Word} + (hsp_ts : updateTimeStamp (Z := Unit) σ = ({((), σ1)}, False)) + (hct1 : σ1.curThread = ct) + (hsp_vp : (do let vcOpt ← getRunVcpu (Z := Unit) ct + match vcOpt with + | some vc => NonDet.when (irq = 27) do + let v ← getVcpu vc + let mk := Function.update v.vcpuVppiMasked .vppiEventIRQ_VTimer true + setVcpu vc { v with vcpuVppiMasked := mk } + | none => pure ()) σ1 = ({((), σ2)}, False)) + (hsp_cr : fl4CompleteRun (Z := Unit) ct .vppi irq 0 0 + { σ2 with machineState := { σ2.machineState with + irqMasks := Function.update σ2.machineState.irqMasks (ucast irqLen irq) true } } = ({((), σ3)}, False)) + (hcb_sp : checkBudget (Z := Unit) σ3 = ({(true, σ3)}, False)) : + fl4HandleVcpuRunExit (Z := Unit) ⟨.vppi, irq, 0, 0⟩ σ = ({((), σ3)}, False) := by + unfold fl4HandleVcpuRunExit + rw [nd_bind_oneSc hsp_ts, nd_bind_oneSc (m := NonDet.gets _) rfl, hct1] + rw [nd_bind_oneSc (by + rw [NonDet.when, if_pos rfl] + exact (vppi_when_eqA ct irq σ1).trans (by rw [nd_bind_oneSc hsp_vp, maskInterrupt_stateA]))] + rw [nd_bind_oneSc hsp_cr, nd_bind_oneSc hcb_sp]; rfl + +section run_exits +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +set_option maxHeartbeats 200000 in +/-- **Guest-exit refinement (non-VPPI, pre-`syscall.finish`)**: the pre-`syscall.finish` body of +`syscall.handle_vcpu_run_exit` refines `fl4HandleVcpuRunExit ⟨stopSpec, w1.bv, w2.bv, w3.bv⟩` +for any `stopSpec ≠ .vppi`. -/ +theorem vcpu_run_exit_jA {AR : A → HwRootA → Prop} {Z : XSetsA} + {OS Ub : Set U64} {m : Fl4SpecA64.McState} {a : A} {s : S} {σ : State Unit} + (X : VcpuXferStateA (L := L) AR Z OS Ub m a s σ) + {Clock : A → MachineState → Prop} + (hNow : NowAgreeA ar Clock) (hNowAR : ∀ a0 h, AR a0 h → ar.now a0 ⦃ r => AR r.2 h ⦄) + (hClock : Clock a σ.machineState) + (hnd : numDomains = 1) + (hNowEq : fl4ReadRawCounter σ.machineState = getCurrentTime σ.machineState) + (hP : VcpuPlatAgreeA ar) (hAK : VcpuArchKeepsA ar AR) (hVR : VcpuVmRootA st ar AR) + (hU : UbFootDisjA Z OS Ub) (hLD : TcbLinkDisjA Z (L.ksb s)) + (hUbUser : ∀ q ∈ Ub, inUserFrame (specPtr q) σ) + (hCs : ∀ c ∈ Z.Cs, c.val < 128) (hcpu : (L.cpu s).val < 128) + (hExt : ∀ b, VcPage Z.V b → ¬ ExtFootE Z.X Z.Y Z.T2 b) + (hSw : ∀ b, VcPage Z.V b → ∀ y, SlotWA Z.th y → + b ≠ y ∧ b ≠ wa y 8#u64 ∧ b ≠ wa y 16#u64 ∧ b ≠ wa y 24#u64) + (hfa : ∀ q, FootA (L := L) Z s q → Aligned q) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hksTcb : ∀ t ∈ Z.T, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → + ∀ o : U64, o.val < ksSizeA → wa t K ≠ wa (L.ksb s) o) + (hSlotTcb : ∀ t ∈ Z.T, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → + ∀ y, SlotWA Z.th y → wa t K ≠ y ∧ wa t K ≠ wa y 8#u64 ∧ wa t K ≠ wa y 16#u64 ∧ wa t K ≠ wa y 24#u64) + (hOS_tcb : ∀ t ∈ Z.T, ∀ K : U64, (K = 3664#u64 ∨ K = 3672#u64) → ∀ y ∈ OS, + wa t K ≠ y ∧ wa t K ≠ wa y 8#u64 ∧ wa t K ≠ wa y 16#u64 ∧ wa t K ≠ wa y 24#u64) + (hClkDisj : ∀ q, (q = L.nodeA s layout.node.CUR_TIME ∨ q = L.nodeA s layout.node.CONSUMED) → + (∀ y, SlotWA Z.th y → q ≠ y ∧ q ≠ wa y 8#u64 ∧ q ≠ wa y 16#u64 ∧ q ≠ wa y 24#u64) ∧ + (∀ y ∈ OS, q ≠ y ∧ q ≠ wa y 8#u64 ∧ q ≠ wa y 16#u64 ∧ q ≠ wa y 24#u64) ∧ + (∀ t ∈ Z.T, ∀ o : U64, o.val < 4096 → q ≠ wa t o) ∧ + (∀ c ∈ Z.C, ∀ o : U64, o.val < 4096 → q ≠ wa c o) ∧ + (¬ VsFootA Z.Pp Z.S1 Z.Y (L.ksb s) q) ∧ + (¬ ExtFootE Z.X Z.Y Z.T2 q) ∧ + (q ≠ wa (L.ksb s) 8#u64 ∧ q ≠ wa (L.ksb s) 16#u64) ∧ + (∀ i : U64, i.val < 5376 → q ≠ irqWordA (L.ksb s) i)) + (hBufNotS2 : ∀ p sz r d mp asid va, + capAtA (L.mem s) (sbWordA (L.curW s)) ≠ .Frame p sz r d mp true asid va) + (hRunSched : L.mem s (runVcpuA (L.curW s)) ≠ 0#u64 → + L.mem s (scA (L.curW s)) ≠ 0#u64 ∧ L.rmax s (L.mem s (scA (L.curW s))) ≠ 0#u64 ∧ + (L.mem s (inqA (L.curW s)) &&& sched.Q_RELEASE) = 0#u64 ∧ + specPtr (L.curW s) ∉ σ.releaseQueue ∧ + Aligned (maxA (L.mem s (scA (L.curW s)))) ∧ stA (L.curW s) ≠ maxA (L.mem s (scA (L.curW s)))) + (hNotRel : specPtr (L.curW s) ∉ σ.releaseQueue) + (hSlotSt : ∀ y, SlotWA Z.th y → stA (L.curW s) ≠ y ∧ stA (L.curW s) ≠ wa y 8#u64 ∧ + stA (L.curW s) ≠ wa y 16#u64 ∧ stA (L.curW s) ≠ wa y 24#u64) + (hOS_st : ∀ y ∈ OS, stA (L.curW s) ≠ y ∧ stA (L.curW s) ≠ wa y 8#u64 ∧ + stA (L.curW s) ≠ wa y 16#u64 ∧ stA (L.curW s) ≠ wa y 24#u64) + (hKs_st : ∀ o : U64, o.val < ksSizeA → stA (L.curW s) ≠ wa (L.ksb s) o) + (hSc_st : ∀ c ∈ Z.C, stA (L.curW s) ≠ wa c 24#u64 ∧ stA (L.curW s) ≠ wa c 72#u64) + (hVs_st : ¬ VsFootA Z.Pp Z.S1 Z.Y (L.ksb s) (stA (L.curW s))) + (hExt_st : ¬ ExtFootE Z.X Z.Y Z.T2 (stA (L.curW s))) + -- Budget check assumptions (for `check_budget_sufficientJ` after `complete_run`) + (hScPost : ∀ (s2 : S) (a2 : A) (hw2 : HwRootA) (σ2 : State Unit), + VcpuXferStateA (L := L) AR { Z with hw := hw2 } OS Ub m a2 s2 σ2 → + ScStateRelA st Z.C m s2 σ2 ∧ + (∃ x n, σ2.kheap (scNameA m (L.cscW s2)) = some (.schedContext x n) ∧ scActive x = true) ∧ + sporadic.min_budget ar a2 ⦃ mb => mb.bv = MIN_BUDGET ⦄ ∧ + getScRefillSufficient (Z := Unit) σ2.curSc σ2.consumedTime σ2 = ({(true, σ2)}, False)) + {stop : U64} {stopSpec : RunStop} (hstop : stop.bv = stopSpec.word) (hne_vppi : stopSpec ≠ .vppi) + (w1 w2 w3 : U64) : + (do + let (s1, a1) ← sched.update_timestamp st ar s a + let ct ← sched.cur_thread st s1 + let (s2, a2) ← vcpu.complete_run st ar s1 a1 ct stop w1 w2 w3 + let (_, s3, a3) ← sched.check_budget st ar s2 a2 + ok (s3, a3)) ⦃ res => + ∃ hw' σ', + fl4HandleVcpuRunExit (Z := Unit) ⟨stopSpec, w1.bv, w2.bv, w3.bv⟩ σ = ({((), σ')}, False) ∧ + VcpuXferStateA (L := L) AR { Z with hw := hw' } OS Ub m res.2 res.1 σ' ⦄ := by + refine WP.spec_bind (X.update_timestamp hNow hNowAR hClock hnd hNowEq hCs hcpu hfa hsub hClkDisj) ?_ + rintro ⟨s1, a1⟩ ⟨σ1, hsp_ts, hσ1_eq, X1, -, hksb1, hcpu1, hpres1, hmem1⟩ + have hcur_eq : L.mem s1 (L.nodeAt s1 (L.cpu s1) layout.node.CUR_THREAD) = L.curW s := by + rw [hcpu1, nodeAt_ksb hksb1] + have hal_ct : Aligned (L.nodeA s layout.node.CUR_THREAD) := X.vcpu.place.node_al (L.cpu s) layout.node.CUR_THREAD (by unfold layout.node.CUR_THREAD; decide) + have hfit := X.j.1.ksFit + have hct0 : layout.node.CUR_THREAD.val = 0 := by unfold layout.node.CUR_THREAD; rfl + have hne_c : ∀ k : U64, k.val < 128 → k ≠ layout.node.CUR_THREAD → L.nodeA s layout.node.CUR_THREAD ≠ L.nodeA s k := fun k hk hne e => by + have hv := congrArg UScalar.val e; unfold LawfulStore.nodeA at hv + rw [nodeAt_valA, nodeAt_valA, hct0, Nat.mod_eq_of_lt (by unfold ksSizeA at hfit; omega), + Nat.mod_eq_of_lt (by unfold ksSizeA at hfit; omega)] at hv + exact hne (UScalar.eq_of_val_eq (by rw [hct0]; omega)) + exact hmem1 _ hal_ct (hne_c layout.node.CUR_TIME (by unfold layout.node.CUR_TIME; decide) (by unfold layout.node.CUR_TIME layout.node.CUR_THREAD; decide)) + (hne_c layout.node.CONSUMED (by unfold layout.node.CONSUMED; decide) (by unfold layout.node.CONSUMED layout.node.CUR_THREAD; decide)) + refine WP.spec_bind (sched.cur_thread.spec s1) ?_ + rintro _ rfl; rw [hcur_eq] + set ct := L.curW s + have hct : ct ∈ Z.T := X.j.1.curT (L.cpu s) X.j.1.sch.cpuCs + have hfa1 : ∀ q, FootA (L := L) Z s1 q → Aligned q := fun q hq => hfa q (footA_of_ksb_cpuA hksb1 hcpu1 hq) + have hsb_os : sbWordA ct ∈ OS := + hsub _ ⟨ct, Or.inr ⟨(X.j.1.thT ct).2 hct, 2, by unfold tcbSlotsA; decide, (bufSlot_eqA ct).symm⟩⟩ + have hBuf1 : ∀ p sz r d mp asid va, capAtA (L.mem s1) (sbWordA ct) ≠ .Frame p sz r d mp true asid va := by + have he : capAtA (L.mem s1) (sbWordA ct) = capAtA (L.mem s) (sbWordA ct) := by + unfold capAtA; rw [(hpres1.os _ hsb_os).1, (hpres1.os _ hsb_os).2.1] + rw [he]; exact hBufNotS2 + have hRun1 : L.mem s1 (runVcpuA ct) ≠ 0#u64 → + L.mem s1 (scA ct) ≠ 0#u64 ∧ L.rmax s1 (L.mem s1 (scA ct)) ≠ 0#u64 ∧ + (L.mem s1 (inqA ct) &&& sched.Q_RELEASE) = 0#u64 ∧ + specPtr ct ∉ σ1.releaseQueue ∧ + Aligned (maxA (L.mem s1 (scA ct))) ∧ stA ct ≠ maxA (L.mem s1 (scA ct)) := fun hnz => by + obtain ⟨h1, h2, h3, h4, h5, h6⟩ := hRunSched (hpres1.run_mon ct hct hnz) + rw [(hpres1.tcb_meta ct hct).2, (hpres1.tcb_meta ct hct).1] + have hscC := X.j.1.rest.scMem ct hct h1 + refine ⟨h1, ?_, h3, by rw [hσ1_eq]; exact h4, h5, h6⟩ + show L.mem s1 (maxA (L.mem s (scA ct))) ≠ 0#u64; rw [hpres1.sc_max _ hscC]; exact h2 + refine WP.spec_bind (complete_run_refinesA X1 hP hAK hVR hU (by rw [hksb1]; exact hLD) + (fun q hq => hpres1.user_frame _ (hUbUser q hq)) hCs (hcpu1 ▸ hcpu) hExt hSw hfa1 hsub + (fun c hc => X1.j.1.idleT c hc) (by rw [hksb1]; exact hksTcb) hSlotTcb hOS_tcb hct hBuf1 hRun1 + (by rw [hσ1_eq]; exact hNotRel) hSlotSt hOS_st (by rw [hksb1]; exact hKs_st) hSc_st + (by rw [hksb1]; exact hVs_st) hExt_st hstop w1 w2 w3) ?_ + rintro ⟨s2, a2⟩ ⟨hw2, σ2, hsp_cr, X2, -, -, -, -, -, -, -⟩ + obtain ⟨hSc2, ⟨xsc, nsc, hks2, hact2⟩, hmb2, hsuf2⟩ := hScPost s2 a2 hw2 σ2 X2 + refine WP.spec_bind (check_budget_sufficientJ (AR := AR) a2 X2.j.1 hSc2 hks2 hact2 hmb2 hnd hsuf2) ?_ + rintro ⟨b, s3, a3⟩ ⟨heq3, hcb_sp⟩ + have hs3 : s3 = s2 := congrArg (fun t => t.2.1) heq3 + have ha3 : a3 = a2 := congrArg (fun t => t.2.2) heq3 + rw [hs3, ha3] + exact spec_let_triple_okA (a := b) (f := Prod.mk) ⟨hw2, σ2, fl4HandleVcpuRunExit_nonvppiA hne_vppi hsp_ts + (by rw [hσ1_eq]; exact X.j.1.sch.cur.symm.trans (by rw [X.j.1.sch.core]; exact X.j.1.sch.curOfW _ X.j.1.sch.cpuCs)) hsp_cr hcb_sp, X2⟩ + +set_option maxHeartbeats 200000 in +/-- **Guest-exit refinement (VPPI, pre-`syscall.finish`)**: the pre-`syscall.finish` body of +`syscall.handle_vcpu_run_vppi` refines `fl4HandleVcpuRunExit ⟨.vppi, irq_id.bv, 0, 0⟩`. -/ +theorem vcpu_run_vppi_jA {AR : A → HwRootA → Prop} {Z : XSetsA} + {OS Ub : Set U64} {m : Fl4SpecA64.McState} {a : A} {s : S} {σ : State Unit} + (X : VcpuXferStateA (L := L) AR Z OS Ub m a s σ) + {Clock : A → MachineState → Prop} + (hNow : NowAgreeA ar Clock) (hNowAR : ∀ a0 h, AR a0 h → ar.now a0 ⦃ r => AR r.2 h ⦄) + (hClock : Clock a σ.machineState) + (hnd : numDomains = 1) + (hNowEq : fl4ReadRawCounter σ.machineState = getCurrentTime σ.machineState) + (hP : VcpuPlatAgreeA ar) (hAK : VcpuArchKeepsA ar AR) (hVR : VcpuVmRootA st ar AR) + (hU : UbFootDisjA Z OS Ub) (hLD : TcbLinkDisjA Z (L.ksb s)) + (hUbUser : ∀ q ∈ Ub, inUserFrame (specPtr q) σ) + (hCs : ∀ c ∈ Z.Cs, c.val < 128) (hcpu : (L.cpu s).val < 128) + (hExt : ∀ b, VcPage Z.V b → ¬ ExtFootE Z.X Z.Y Z.T2 b) + (hSw : ∀ b, VcPage Z.V b → ∀ y, SlotWA Z.th y → + b ≠ y ∧ b ≠ wa y 8#u64 ∧ b ≠ wa y 16#u64 ∧ b ≠ wa y 24#u64) + (hfa : ∀ q, FootA (L := L) Z s q → Aligned q) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hksTcb : ∀ t ∈ Z.T, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → + ∀ o : U64, o.val < ksSizeA → wa t K ≠ wa (L.ksb s) o) + (hSlotTcb : ∀ t ∈ Z.T, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → + ∀ y, SlotWA Z.th y → wa t K ≠ y ∧ wa t K ≠ wa y 8#u64 ∧ wa t K ≠ wa y 16#u64 ∧ wa t K ≠ wa y 24#u64) + (hOS_tcb : ∀ t ∈ Z.T, ∀ K : U64, (K = 3664#u64 ∨ K = 3672#u64) → ∀ y ∈ OS, + wa t K ≠ y ∧ wa t K ≠ wa y 8#u64 ∧ wa t K ≠ wa y 16#u64 ∧ wa t K ≠ wa y 24#u64) + (hClkDisj : ∀ q, (q = L.nodeA s layout.node.CUR_TIME ∨ q = L.nodeA s layout.node.CONSUMED) → + (∀ y, SlotWA Z.th y → q ≠ y ∧ q ≠ wa y 8#u64 ∧ q ≠ wa y 16#u64 ∧ q ≠ wa y 24#u64) ∧ + (∀ y ∈ OS, q ≠ y ∧ q ≠ wa y 8#u64 ∧ q ≠ wa y 16#u64 ∧ q ≠ wa y 24#u64) ∧ + (∀ t ∈ Z.T, ∀ o : U64, o.val < 4096 → q ≠ wa t o) ∧ + (∀ c ∈ Z.C, ∀ o : U64, o.val < 4096 → q ≠ wa c o) ∧ + (¬ VsFootA Z.Pp Z.S1 Z.Y (L.ksb s) q) ∧ + (¬ ExtFootE Z.X Z.Y Z.T2 q) ∧ + (q ≠ wa (L.ksb s) 8#u64 ∧ q ≠ wa (L.ksb s) 16#u64) ∧ + (∀ i : U64, i.val < 5376 → q ≠ irqWordA (L.ksb s) i)) + (hBufNotS2 : ∀ p sz r d mp asid va, + capAtA (L.mem s) (sbWordA (L.curW s)) ≠ .Frame p sz r d mp true asid va) + (hRunSched : L.mem s (runVcpuA (L.curW s)) ≠ 0#u64 → + L.mem s (scA (L.curW s)) ≠ 0#u64 ∧ L.rmax s (L.mem s (scA (L.curW s))) ≠ 0#u64 ∧ + (L.mem s (inqA (L.curW s)) &&& sched.Q_RELEASE) = 0#u64 ∧ + specPtr (L.curW s) ∉ σ.releaseQueue ∧ + Aligned (maxA (L.mem s (scA (L.curW s)))) ∧ stA (L.curW s) ≠ maxA (L.mem s (scA (L.curW s)))) + (hNotRel : specPtr (L.curW s) ∉ σ.releaseQueue) + (hSlotSt : ∀ y, SlotWA Z.th y → stA (L.curW s) ≠ y ∧ stA (L.curW s) ≠ wa y 8#u64 ∧ + stA (L.curW s) ≠ wa y 16#u64 ∧ stA (L.curW s) ≠ wa y 24#u64) + (hOS_st : ∀ y ∈ OS, stA (L.curW s) ≠ y ∧ stA (L.curW s) ≠ wa y 8#u64 ∧ + stA (L.curW s) ≠ wa y 16#u64 ∧ stA (L.curW s) ≠ wa y 24#u64) + (hKs_st : ∀ o : U64, o.val < ksSizeA → stA (L.curW s) ≠ wa (L.ksb s) o) + (hSc_st : ∀ c ∈ Z.C, stA (L.curW s) ≠ wa c 24#u64 ∧ stA (L.curW s) ≠ wa c 72#u64) + (hVs_st : ¬ VsFootA Z.Pp Z.S1 Z.Y (L.ksb s) (stA (L.curW s))) + (hExt_st : ¬ ExtFootE Z.X Z.Y Z.T2 (stA (L.curW s))) + (hScPost : ∀ (s3 : S) (a3 : A) (hw3 : HwRootA) (σ3 : State Unit), + VcpuXferStateA (L := L) AR { Z with hw := hw3 } OS Ub m a3 s3 σ3 → + ScStateRelA st Z.C m s3 σ3 ∧ + (∃ x n, σ3.kheap (scNameA m (L.cscW s3)) = some (.schedContext x n) ∧ scActive x = true) ∧ + sporadic.min_budget ar a3 ⦃ mb => mb.bv = MIN_BUDGET ⦄ ∧ + getScRefillSufficient (Z := Unit) σ3.curSc σ3.consumedTime σ3 = ({(true, σ3)}, False)) + (irq_id : U64) : + (do + let (s1, a1) ← sched.update_timestamp st ar s a + let ct ← sched.cur_thread st s1 + let i ← layout.tcb.RUN_VCPU + let i1 ← lift (wa ct i) + let v ← st.read s1 i1 + let s2 ← if v != 0#u64 then + if irq_id = layout.irq.TIMER_ID then do + let i2 ← lift (wa v vcpu.VPPI_MASKED) + let masked ← st.read s1 i2 + let i3 ← lift (wa v vcpu.VPPI_MASKED) + let i4 ← lift (masked ||| 1#u64) + st.write s1 i3 i4 + else ok s1 + else ok s1 + let i2 ← st.cur_cpu s2 + let i3 ← layout.irq.of irq_id i2 + let a2 ← ar.mask_irq a1 i3 true + let (s3, a3) ← vcpu.complete_run st ar s2 a2 ct fl4_abi.consts.vcpu_run_stop.VPPI irq_id 0#u64 0#u64 + let (_, s4, a4) ← sched.check_budget st ar s3 a3 + ok (s4, a4)) ⦃ res => + ∃ hw' σ', + fl4HandleVcpuRunExit (Z := Unit) ⟨.vppi, irq_id.bv, 0, 0⟩ σ = ({((), σ')}, False) ∧ + VcpuXferStateA (L := L) AR { Z with hw := hw' } OS Ub m res.2 res.1 σ' ⦄ := by + refine WP.spec_bind (X.update_timestamp hNow hNowAR hClock hnd hNowEq hCs hcpu hfa hsub hClkDisj) ?_ + rintro ⟨s1, a1⟩ ⟨σ1, hsp_ts, hσ1_eq, X1, -, hksb1, hcpu1, hpres1, hmem1⟩ + have hcur_eq : L.mem s1 (L.nodeAt s1 (L.cpu s1) layout.node.CUR_THREAD) = L.curW s := by + rw [hcpu1, nodeAt_ksb hksb1] + have hal_ct : Aligned (L.nodeA s layout.node.CUR_THREAD) := X.vcpu.place.node_al (L.cpu s) layout.node.CUR_THREAD (by unfold layout.node.CUR_THREAD; decide) + have hfit := X.j.1.ksFit + have hct0 : layout.node.CUR_THREAD.val = 0 := by unfold layout.node.CUR_THREAD; rfl + have hne_c : ∀ k : U64, k.val < 128 → k ≠ layout.node.CUR_THREAD → L.nodeA s layout.node.CUR_THREAD ≠ L.nodeA s k := fun k hk hne e => by + have hv := congrArg UScalar.val e; unfold LawfulStore.nodeA at hv + rw [nodeAt_valA, nodeAt_valA, hct0, Nat.mod_eq_of_lt (by unfold ksSizeA at hfit; omega), + Nat.mod_eq_of_lt (by unfold ksSizeA at hfit; omega)] at hv + exact hne (UScalar.eq_of_val_eq (by rw [hct0]; omega)) + exact hmem1 _ hal_ct (hne_c layout.node.CUR_TIME (by unfold layout.node.CUR_TIME; decide) (by unfold layout.node.CUR_TIME layout.node.CUR_THREAD; decide)) + (hne_c layout.node.CONSUMED (by unfold layout.node.CONSUMED; decide) (by unfold layout.node.CONSUMED layout.node.CUR_THREAD; decide)) + refine WP.spec_bind (sched.cur_thread.spec s1) ?_ + rintro _ rfl + rw [hcur_eq, layout_tcb_run_vcpu_eqA, bind_ok, lift_ok, bind_ok, ← runVcpuA_eq, L.read_eq, bind_ok] + set ct := L.curW s + have hct : ct ∈ Z.T := X.j.1.curT (L.cpu s) X.j.1.sch.cpuCs + have hfa1 : ∀ q, FootA (L := L) Z s1 q → Aligned q := fun q hq => hfa q (footA_of_ksb_cpuA hksb1 hcpu1 hq) + obtain ⟨xct1, hkc1, -, hrv1_c⟩ := X1.vcpu.core.tcb ct hct + -- Optional VPPI_MASKED |= 1 step + have hVppiStep : (if L.mem s1 (runVcpuA ct) != 0#u64 then + if irq_id = layout.irq.TIMER_ID then do + let i2 ← lift (wa (L.mem s1 (runVcpuA ct)) vcpu.VPPI_MASKED) + let masked ← st.read s1 i2 + let i3 ← lift (wa (L.mem s1 (runVcpuA ct)) vcpu.VPPI_MASKED) + let i4 ← lift (masked ||| 1#u64) + st.write s1 i3 i4 + else ok s1 + else ok s1) ⦃ s2 => + ∃ σ2, + (do let vcOpt ← getRunVcpu (Z := Unit) (specPtr ct) + match vcOpt with + | some vc => NonDet.when (irq_id.bv = 27) do + let v ← getVcpu vc + let mk := Function.update v.vcpuVppiMasked .vppiEventIRQ_VTimer true + setVcpu vc { v with vcpuVppiMasked := mk } + | none => pure ()) σ1 = ({((), σ2)}, False) ∧ + VcpuXferStateA (L := L) AR Z OS Ub m a1 s2 σ2 ∧ + L.ksb s2 = L.ksb s ∧ L.cpu s2 = L.cpu s ∧ + σ2.releaseQueue = σ.releaseQueue ∧ + VcpuRunPresA L Z OS s s2 σ σ2 ⦄ := by + by_cases hrv_nz : (L.mem s1 (runVcpuA ct) != 0#u64) = true + · have hrv_ne0 : L.mem s1 (runVcpuA ct) ≠ 0#u64 := bne_iff_ne.1 hrv_nz + rw [if_pos hrv_nz] + set vc := L.mem s1 (runVcpuA ct) + obtain ⟨hvcV, -⟩ := X1.vcpu.run_c2v ct hct vc rfl hrv_ne0 + have hrv_some : xct1.tcbExt.tcbRunVcpu = some (specPtr vc) := by rw [hrv1_c]; unfold optRefA; rw [if_neg hrv_ne0] + by_cases htm : irq_id = layout.irq.TIMER_ID + · have htm_bv : irq_id.bv = 27 := by rw [htm]; unfold layout.irq.TIMER_ID; rfl + rw [if_pos htm] + have hvp_off : vcpu.VPPI_MASKED = 16#u64 := by unfold vcpu.VPPI_MASKED; rfl + rw [hvp_off, lift_ok, bind_ok, L.read_eq, bind_ok, bind_ok, lift_ok, bind_ok, L.write_eq, WP.spec_ok] + obtain ⟨v1, hkv1, -, -⟩ := X1.vcpu.core.obj vc hvcV + obtain ⟨X2, hpres2⟩ := X1.write_vppi_masked_bit0 hCs (hcpu1 ▸ hcpu) hExt hSw hfa1 hsub (fun c hc => X1.j.1.idleT c hc) hvcV hkv1 + refine ⟨_, ?_, X2, (L.ksb_wr _ _ _).trans hksb1, (L.cpu_wr _ _ _).trans hcpu1, by rw [hσ1_eq]; rfl, hpres1.trans hpres2⟩ + rw [nd_bind_oneSc (getRunVcpu_eqA hkc1), hrv_some]; dsimp only; unfold NonDet.when; rw [if_pos htm_bv] + rw [nd_bind_oneSc (getVcpu_eqA hkv1), setVcpu_eqA hkv1] + · have htm_nbv : ¬ (irq_id.bv = 27) := fun e => htm (u64_eq_of_bv_eq (by rw [e]; unfold layout.irq.TIMER_ID; rfl)) + rw [if_neg htm, WP.spec_ok] + refine ⟨σ1, ?_, X1, hksb1, hcpu1, by rw [hσ1_eq]; rfl, hpres1⟩ + rw [nd_bind_oneSc (getRunVcpu_eqA hkc1), hrv_some]; dsimp only; unfold NonDet.when; rw [if_neg htm_nbv]; rfl + · have hrv_eq0 : L.mem s1 (runVcpuA ct) = 0#u64 := by by_contra hne; exact hrv_nz (bne_iff_ne.2 hne) + rw [if_neg hrv_nz, WP.spec_ok] + have hrv_none : xct1.tcbExt.tcbRunVcpu = none := by rw [hrv1_c, hrv_eq0]; rfl + refine ⟨σ1, ?_, X1, hksb1, hcpu1, by rw [hσ1_eq]; rfl, hpres1⟩ + rw [nd_bind_oneSc (getRunVcpu_eqA hkc1), hrv_none]; rfl + refine WP.spec_bind hVppiStep ?_ + rintro s2 ⟨σ2, hsp_vp, X2, hksb2, hcpu2, hrel2, hpres12⟩ + rw [L.cur_cpu_eq, bind_ok] + refine WP.spec_bind (layout_irq_of_totalA irq_id (L.cpu s2)) ?_ + intro irq_hw _ + refine WP.spec_bind (hAK.mask_irq a1 Z.hw irq_hw true X2.j.1.ar) ?_ + intro a2 hAR2 + set σ2m := { σ2 with machineState := { σ2.machineState with + irqMasks := Function.update σ2.machineState.irqMasks (ucast irqLen irq_id.bv) true } } + have X2m : VcpuXferStateA (L := L) AR Z OS Ub m a2 s2 σ2m := + vcpuXferStateA_of_maskInterruptA X2 hAR2 true (ucast irqLen irq_id.bv) + have hfa2 : ∀ q, FootA (L := L) Z s2 q → Aligned q := fun q hq => hfa q (footA_of_ksb_cpuA hksb2 hcpu2 hq) + have hsb_os : sbWordA ct ∈ OS := + hsub _ ⟨ct, Or.inr ⟨(X.j.1.thT ct).2 hct, 2, by unfold tcbSlotsA; decide, (bufSlot_eqA ct).symm⟩⟩ + have hBuf2 : ∀ p sz r d mp asid va, capAtA (L.mem s2) (sbWordA ct) ≠ .Frame p sz r d mp true asid va := by + have he : capAtA (L.mem s2) (sbWordA ct) = capAtA (L.mem s) (sbWordA ct) := by + unfold capAtA; rw [(hpres12.os _ hsb_os).1, (hpres12.os _ hsb_os).2.1] + rw [he]; exact hBufNotS2 + have hRun2 : L.mem s2 (runVcpuA ct) ≠ 0#u64 → + L.mem s2 (scA ct) ≠ 0#u64 ∧ L.rmax s2 (L.mem s2 (scA ct)) ≠ 0#u64 ∧ + (L.mem s2 (inqA ct) &&& sched.Q_RELEASE) = 0#u64 ∧ + specPtr ct ∉ σ2m.releaseQueue ∧ + Aligned (maxA (L.mem s2 (scA ct))) ∧ stA ct ≠ maxA (L.mem s2 (scA ct)) := fun hnz => by + obtain ⟨h1, h2, h3, h4, h5, h6⟩ := hRunSched (hpres12.run_mon ct hct hnz) + rw [(hpres12.tcb_meta ct hct).2, (hpres12.tcb_meta ct hct).1] + have hscC := X.j.1.rest.scMem ct hct h1 + refine ⟨h1, ?_, h3, by change specPtr ct ∉ σ2.releaseQueue; rw [hrel2]; exact h4, h5, h6⟩ + show L.mem s2 (maxA (L.mem s (scA ct))) ≠ 0#u64; rw [hpres12.sc_max _ hscC]; exact h2 + have hvppi_bv : fl4_abi.consts.vcpu_run_stop.VPPI.bv = RunStop.vppi.word := by + unfold fl4_abi.consts.vcpu_run_stop.VPPI RunStop.word; rfl + refine WP.spec_bind (complete_run_refinesA X2m hP hAK hVR hU (by rw [hksb2]; exact hLD) + (fun q hq => hpres12.user_frame _ (hUbUser q hq)) hCs (hcpu2 ▸ hcpu) hExt hSw hfa2 hsub + (fun c hc => X2m.j.1.idleT c hc) (by rw [hksb2]; exact hksTcb) hSlotTcb hOS_tcb hct hBuf2 hRun2 + (by change specPtr ct ∉ σ2.releaseQueue; rw [hrel2]; exact hNotRel) hSlotSt hOS_st + (by rw [hksb2]; exact hKs_st) hSc_st (by rw [hksb2]; exact hVs_st) hExt_st hvppi_bv irq_id 0#u64 0#u64) ?_ + rintro ⟨s3, a3⟩ ⟨hw3, σ3, hsp_cr, X3, -, -, -, -, -, -, -⟩ + obtain ⟨hSc3, ⟨xsc, nsc, hks3, hact3⟩, hmb3, hsuf3⟩ := hScPost s3 a3 hw3 σ3 X3 + refine WP.spec_bind (check_budget_sufficientJ (AR := AR) a3 X3.j.1 hSc3 hks3 hact3 hmb3 hnd hsuf3) ?_ + rintro ⟨b, s4, a4⟩ ⟨heq4, hcb_sp⟩ + have hs4 : s4 = s3 := congrArg (fun t => t.2.1) heq4 + have ha4 : a4 = a3 := congrArg (fun t => t.2.2) heq4 + rw [hs4, ha4] + exact spec_let_triple_okA (a := b) (f := Prod.mk) ⟨hw3, σ3, fl4HandleVcpuRunExit_vppiA hsp_ts + (by rw [hσ1_eq]; exact X.j.1.sch.cur.symm.trans (by rw [X.j.1.sch.core]; exact X.j.1.sch.curOfW _ X.j.1.sch.cpuCs)) hsp_vp hsp_cr hcb_sp, X3⟩ + +end run_exits + +end +end Fl4ProofsA64.P3.Vcpu diff --git a/proofs/Fl4ProofsA64/P3/VcpuFrameMemA.lean b/proofs/Fl4ProofsA64/P3/VcpuFrameMemA.lean new file mode 100644 index 000000000..c1929af9e --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/VcpuFrameMemA.lean @@ -0,0 +1,942 @@ +/- +P3 (aarch64, A64-P3 S4 W1): **User-frame memory relation (`FrameMemRelA`, D6), lockstep +`storeWordOffs` preservation (`storeWordOffs_frameMemA`), and `vcpu.sync_vgic_status` loop +refinement (`sync_vgic_status_refinesA`)**. + +No new named premise. +-/ +import Fl4ProofsA64.P3.VcpuRelA +import Fl4ProofsA64.P3.EpRelA + +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3.Vcpu + +open Fl4Spec Fl4SpecA64 Fl4ProofsA64.P1 Fl4ProofsA64.P3 Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Endp + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## `FrameMemRelA` (contract §4.2, D6) -/ + +/-- Page size in bits for FL4's `Frame` cap size encoding (`Small` = 4 KiB, `Large` = 2 MiB, `Huge` = 1 GiB). -/ +def frameBitsA : capability.FrameSize → Nat + | .Small => 12 + | .Large => 21 + | .Huge => 30 + +/-- **D6 `FrameMemRelA`**: every word in the non-device user-frame set `Ub` is 8-byte aligned and +agrees with `σ.machineState.underlyingMemory` (`loadWordAtA`), every non-device `Cap::Frame` in +`OS` has all its aligned words in `Ub`, and `Ub` is disjoint from kernel slots/objects. -/ +structure FrameMemRelA (Z : XSetsA) (OS Ub : Set U64) (ksb : U64) + (mem : U64 → U64) (um : Word → Word8) : Prop where + aligned : ∀ b ∈ Ub, Aligned b + agree : ∀ b ∈ Ub, (mem b).bv = loadWordAtA um (specPtr b) + frames : ∀ y ∈ OS, ∀ ptr sz r mp s2 asid va, + capAtA mem y = .Frame ptr sz r false mp s2 asid va → + ptr.val % 2 ^ frameBitsA sz = 0 ∧ ptr.val + 2 ^ frameBitsA sz ≤ 2 ^ 64 ∧ + ∀ o : U64, o.val < 2 ^ frameBitsA sz → o.val % 8 = 0 → wa ptr o ∈ Ub + disj_os : ∀ b ∈ Ub, ∀ y ∈ OS, b ≠ y ∧ b ≠ wa y 8#u64 ∧ b ≠ wa y 16#u64 ∧ b ≠ wa y 24#u64 + disj_slots : ∀ b ∈ Ub, ∀ y, IsSlotA Z.th y → b ≠ y ∧ b ≠ wa y 8#u64 + disj_tcb : ∀ b ∈ Ub, ∀ t ∈ Z.T, ∀ o : U64, o.val < 4096 → b ≠ wa t o + disj_sc : ∀ b ∈ Ub, ∀ c ∈ Z.C, ∀ o : U64, o.val < 4096 → b ≠ wa c o + disj_ks : ∀ b ∈ Ub, ¬ (ksb.val ≤ b.val ∧ b.val < ksb.val + ksSizeA) + +theorem loadWordAtA_eq_loadWordAt (um : Word → Word8) (p : Word) : + loadWordAtA um p = loadWordAt um p := rfl + +theorem specPtr_wa_wordA {frame : U64} {k : Nat} (_hfit : frame.val + 8 * k < 2 ^ 64) : + specPtr (wa frame ⟨BitVec.ofNat 64 (8 * k)⟩) = bufAddr (specPtr frame) k := by + have hwa : (wa frame ⟨BitVec.ofNat 64 (8 * k)⟩).bv = frame.bv + BitVec.ofNat 64 (8 * k) := by + apply BitVec.eq_of_toNat_eq + change (wa frame ⟨BitVec.ofNat 64 (8 * k)⟩).val = _ + rw [wa_valP, BitVec.toNat_add, BitVec.toNat_ofNat] + rfl + unfold specPtr bufAddr wordSize + rw [hwa, Nat.mul_comm k 8, BitVec.add_assoc, BitVec.add_comm (BitVec.ofNat 64 (8 * k)) pptrShiftA, ← BitVec.add_assoc] + +private theorem specPtr_al8 {b : U64} (hb : Aligned b) : (specPtr b).toNat % 8 = 0 := by + rw [show (8 : Nat) = 2 ^ 3 from rfl, specPtr_mod b (by omega)] + exact hb + +/-- **Lockstep user-frame word store (`storeWordOffs_frameMemA`)**: writing `w` at `b ∈ Ub` in `s` +and `storeMem um (specPtr b) w.bv` in `um` preserves `FrameMemRelA` AND updates every word in `Ub` +(including any overlapping IPC buffer `Bf` inside `Ub`) in lockstep. -/ +theorem storeWordOffs_frameMemA {S : Type} {st : machine.Store S} [L : LawfulStore st] + {Z : XSetsA} {OS Ub : Set U64} {s : S} {um : Word → Word8} + (hF : FrameMemRelA Z OS Ub (L.ksb s) (L.mem s) um) + (hOS_al : ∀ y ∈ OS, Aligned y) + {b : U64} (hb : b ∈ Ub) (w : U64) : + FrameMemRelA Z OS Ub (L.ksb (L.wr s b w)) (L.mem (L.wr s b w)) (storeMem um (specPtr b) w.bv) := by + have hba : Aligned b := hF.aligned b hb + have hcap : ∀ y ∈ OS, capAtA (L.mem (L.wr s b w)) y = capAtA (L.mem s) y := by + intro y hy + obtain ⟨h1, h2, -, -⟩ := hF.disj_os b hb y hy + unfold capAtA + rw [mem_wr_neA hba (hOS_al y hy) h1, mem_wr_neA hba (aligned_wa (hOS_al y hy) 8#u64 (by decide)) h2] + have hal_b : (specPtr b).toNat % 8 = 0 := specPtr_al8 hba + refine ⟨hF.aligned, ?_, ?_, hF.disj_os, hF.disj_slots, hF.disj_tcb, hF.disj_sc, ?_⟩ + · intro q hq + by_cases hqb : q = b + · subst hqb + rw [L.mem_wr_same hba, loadWordAtA_eq_loadWordAt, load_store_same] + · rw [mem_wr_neA hba (hF.aligned q hq) (Ne.symm hqb)] + have hsp : specPtr q ≠ specPtr b := fun h => hqb (specPtr_inj h) + have hal_q : (specPtr q).toNat % 8 = 0 := specPtr_al8 (hF.aligned q hq) + rw [loadWordAtA_eq_loadWordAt, load_store_other um w.bv hal_b hal_q hsp] + exact hF.agree q hq + · intro y hy ptr sz r mp s2 asid va hc + rw [hcap y hy] at hc + exact hF.frames y hy ptr sz r mp s2 asid va hc + · rw [L.ksb_wr]; exact hF.disj_ks + +/-- Writing a user-frame word `b ∈ Ub` paired with `setUm σ (storeMem um (specPtr b) w.bv)` +preserves `VcpuRelA` (since `VcPage Z.V` is disjoint from `Ub`). -/ +theorem VcpuRelA.frame_ub_wrA {S : Type} {st : machine.Store S} [L : LawfulStore st] + {Z : XSetsA} {OS Ub : Set U64} {s : S} {σ : State Unit} + (hV : VcpuRelA (L := L) Z OS Ub s σ) + (hF : FrameMemRelA Z OS Ub (L.ksb s) (L.mem s) σ.machineState.underlyingMemory) + (hksFit : (L.ksb s).val + ksSizeA ≤ 2 ^ 64) + (hCs : ∀ c ∈ Z.Cs, c.val < 128) + (hIdleT : ∀ c ∈ Z.Cs, L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) ∈ Z.T) + {b : U64} (hb : b ∈ Ub) (w : U64) : + VcpuRelA (L := L) Z OS Ub (L.wr s b w) + (setUm σ (storeMem σ.machineState.underlyingMemory (specPtr b) w.bv)) := by + have hba : Aligned b := hF.aligned b hb + have hne_vc : ∀ v ∈ Z.V, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → b ≠ wa v o := by + intro v hv o ho hal heq + exact hV.place.disj_ub b (heq ▸ vcpuHdr_in_vcPage hv ho hal) hb + have hne_vc0 : ∀ v ∈ Z.V, b ≠ v := by + intro v hv + have h0 := hne_vc v hv 0#u64 (by decide) (by decide) + have hwa0 : wa v 0#u64 = v := by + apply UScalar.eq_of_val_eq; rw [wa_valP]; simp [UScalar.hBounds] + rwa [hwa0] at h0 + have hne_tcb : ∀ t ∈ Z.T, ∀ o : U64, o.val < 4096 → b ≠ wa t o := + fun t ht o ho => hF.disj_tcb b hb t ht o ho + have hcap_os : ∀ y ∈ OS, capAtA (L.mem (L.wr s b w)) y = capAtA (L.mem s) y := by + intro y hy + obtain ⟨h1, h2, -, -⟩ := hF.disj_os b hb y hy + unfold capAtA + rw [mem_wr_neA hba (hV.place.al_os y hy) h1, + mem_wr_neA hba (aligned_wa (hV.place.al_os y hy) 8#u64 (by decide)) h2] + have hcap_sl : ∀ y, IsSlotA Z.th y → capAtA (L.mem (L.wr s b w)) y = capAtA (L.mem s) y := by + intro y hy + obtain ⟨h1, h2⟩ := hF.disj_slots b hb y hy + unfold capAtA + rw [mem_wr_neA hba (hV.place.al_slot y hy) h1, + mem_wr_neA hba (aligned_wa (hV.place.al_slot y hy) 8#u64 (by decide)) h2] + refine ⟨?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, hV.arch, ?_⟩ + · refine ⟨?_, hV.core.back, ?_, ?_⟩ + · intro v hv + obtain ⟨xv, hkv, htv, hrv⟩ := hV.core.obj v hv + exact ⟨xv, hkv, + by rw [mem_wr_neA hba (hV.place.vc_al hv) (hne_vc0 v hv)]; exact htv, + by rw [mem_wr_neA hba (aligned_wa (hV.place.vc_al hv) 24#u64 (by decide)) (hne_vc v hv 24#u64 (by decide) (by decide))]; exact hrv⟩ + · intro t ht + obtain ⟨xt, hkt, htv, htr⟩ := hV.core.tcb t ht + exact ⟨xt, hkt, + by rw [mem_wr_neA hba (aligned_wa (hV.place.al_t t ht) 3664#u64 (by decide)) (hne_tcb t ht 3664#u64 (by decide))]; exact htv, + by rw [runVcpuA, mem_wr_neA hba (aligned_wa (hV.place.al_t t ht) 3672#u64 (by decide)) (hne_tcb t ht 3672#u64 (by decide))]; exact htr⟩ + · intro y hy p hc + rw [hcap_sl y hy] at hc; exact hV.core.cap y hy p hc + · intro v hv + rw [mem_wr_neA hba (aligned_wa (hV.place.vc_al hv) 8#u64 (by decide)) (hne_vc v hv 8#u64 (by decide) (by decide))] + exact hV.owner v hv + · intro v hv xv hkv + rw [mem_wr_neA hba (aligned_wa (hV.place.vc_al hv) 16#u64 (by decide)) (hne_vc v hv 16#u64 (by decide) (by decide))] + exact hV.vppi v hv xv hkv + · intro v hv xv hkv + rw [mem_wr_neA hba (aligned_wa (hV.place.vc_al hv) 32#u64 (by decide)) (hne_vc v hv 32#u64 (by decide) (by decide))] + exact hV.runOff v hv xv hkv + · intro v hv xv hkv + rw [mem_wr_neA hba (aligned_wa (hV.place.vc_al hv) 40#u64 (by decide)) (hne_vc v hv 40#u64 (by decide) (by decide))] + exact hV.runOut v hv xv hkv + · intro v hv xv hkv j hj + rw [mem_wr_neA hba (hV.place.vcPage_al (vcpuRegWA_in_vcPage hv hj)) + (hne_vc v hv (vcpuRegOffA j) (by rw [vcpuRegOffA_val hj]; omega) + (by rw [vcpuRegOffA_val hj]; omega))] + exact hV.regs v hv xv hkv j hj + · intro v hv t ht hne + rw [mem_wr_neA hba (hV.place.vc_al hv) (hne_vc0 v hv)] at ht + obtain ⟨htT, htv⟩ := hV.bind_v2t v hv t ht hne + exact ⟨htT, by rw [mem_wr_neA hba (aligned_wa (hV.place.al_t t htT) 3664#u64 (by decide)) (hne_tcb t htT 3664#u64 (by decide))]; exact htv⟩ + · intro t ht v hv hne + rw [mem_wr_neA hba (aligned_wa (hV.place.al_t t ht) 3664#u64 (by decide)) (hne_tcb t ht 3664#u64 (by decide))] at hv + obtain ⟨hvV, hvt⟩ := hV.bind_t2v t ht v hv hne + exact ⟨hvV, by rw [mem_wr_neA hba (hV.place.vc_al hvV) (hne_vc0 v hvV)]; exact hvt⟩ + · intro v hv c hc hne + rw [mem_wr_neA hba (aligned_wa (hV.place.vc_al hv) 24#u64 (by decide)) (hne_vc v hv 24#u64 (by decide) (by decide))] at hc + obtain ⟨hcT, hcv⟩ := hV.run_v2c v hv c hc hne + exact ⟨hcT, by rw [runVcpuA, mem_wr_neA hba (aligned_wa (hV.place.al_t c hcT) 3672#u64 (by decide)) (hne_tcb c hcT 3672#u64 (by decide))]; exact hcv⟩ + · intro c hc v hv hne + rw [runVcpuA, mem_wr_neA hba (aligned_wa (hV.place.al_t c hc) 3672#u64 (by decide)) (hne_tcb c hc 3672#u64 (by decide))] at hv + obtain ⟨hvV, hvc⟩ := hV.run_c2v c hc v hv hne + exact ⟨hvV, by rw [mem_wr_neA hba (aligned_wa (hV.place.vc_al hvV) 24#u64 (by decide)) (hne_vc v hvV 24#u64 (by decide) (by decide))]; exact hvc⟩ + · intro c hc hne + rw [runVcpuA, mem_wr_neA hba (aligned_wa (hV.place.al_t c hc) 3672#u64 (by decide)) (hne_tcb c hc 3672#u64 (by decide))] at hne + rw [stA_eq_wa, mem_wr_neA hba (aligned_wa (hV.place.al_t c hc) 3328#u64 (by decide)) (hne_tcb c hc 3328#u64 (by decide))] + rw [← stA_eq_wa]; exact hV.run_state c hc hne + · intro t ht hne + rw [mem_wr_neA hba (aligned_wa (hV.place.al_t t ht) 3664#u64 (by decide)) (hne_tcb t ht 3664#u64 (by decide))] at hne + rw [runVcpuA, mem_wr_neA hba (aligned_wa (hV.place.al_t t ht) 3672#u64 (by decide)) (hne_tcb t ht 3672#u64 (by decide))] + exact hV.bind_run_disj t ht hne + · intro c hc + have hnd : L.nodeAt (L.wr s b w) c layout.node.IDLE_THREAD = L.nodeAt s c layout.node.IDLE_THREAD := by + simp [LawfulStore.nodeAt, L.ksb_wr] + rw [hnd] + have hne_nd : b ≠ L.nodeAt s c layout.node.IDLE_THREAD := by + intro heq + apply hF.disj_ks b hb + rw [heq] + have hval : (L.nodeAt s c layout.node.IDLE_THREAD).val = + (L.ksb s).val + 223488 + c.val * 4288 + (layout.node.IDLE_THREAD).val := by + have hk := (L.ksb s).hBounds + have hc' := c.hBounds + have hc128 := hCs c hc + simp only [ksSizeA] at hksFit + simp [LawfulStore.nodeAt, core.num.U64.wrapping_add_val_eq, core.num.U64.wrapping_mul_val_eq, + U64.size_def, U64.numBits, UScalar.size] at hk hc' ⊢ + omega + rw [hval] + have hc128 := hCs c hc + simp only [ksSizeA] + have hid : (layout.node.IDLE_THREAD).val < 4288 := by unfold layout.node.IDLE_THREAD; decide + omega + rw [mem_wr_neA hba (hV.place.node_al c layout.node.IDLE_THREAD (by unfold layout.node.IDLE_THREAD; decide)) hne_nd] + have hit := hIdleT c hc + obtain ⟨h1, h2⟩ := hV.idle_no_vcpu c hc + exact ⟨by rw [mem_wr_neA hba (aligned_wa (hV.place.al_t _ hit) 3664#u64 (by decide)) (hne_tcb _ hit 3664#u64 (by decide))]; exact h1, + by rw [runVcpuA, mem_wr_neA hba (aligned_wa (hV.place.al_t _ hit) 3672#u64 (by decide)) (hne_tcb _ hit 3672#u64 (by decide))]; exact h2⟩ + · intro v hv hor + rw [mem_wr_neA hba (hV.place.vc_al hv) (hne_vc0 v hv), + mem_wr_neA hba (aligned_wa (hV.place.vc_al hv) 24#u64 (by decide)) (hne_vc v hv 24#u64 (by decide) (by decide))] at hor + obtain ⟨y, hy, hc⟩ := hV.live v hv hor + exact ⟨y, hy, by rw [hcap_os y hy]; exact hc⟩ + · rw [L.ksb_wr]; exact hV.place + +/-! ## `vcpu.sync_vgic_status` loop refinement (`sync_vgic_status_refinesA`) -/ + +section sync_vgic +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +private def syncStepSpec (x : Fl4SpecA64.Vcpu) (gicV3 : Bool) + (acc : Word × Word × Nat × Bool) (i : Nat) : Word × Word × Nat × Bool := + let (elrsr, eisr, valid, pend) := acc + let (state, hw, eoi) := lrFields (fl4VcpuRegGet x (vrVgicLr0 + i)) gicV3 + if state = 0 ∧ (hw ≠ 0 ∨ eoi = 0) then (elrsr ||| (1 <<< i), eisr, valid, pend) + else if state = 0 ∧ hw = 0 ∧ eoi ≠ 0 then (elrsr, eisr ||| (1 <<< i), valid, pend) + else (elrsr, eisr, valid + 1, pend || decide (state = 1)) + +private theorem u64_shl_one_eq (i : Nat) (hi : i < 64) : + (1#u64 <<< (⟨BitVec.ofNat 64 i⟩ : U64)) = ok ⟨(1 : Word) <<< i⟩ := by + have hival : (⟨BitVec.ofNat 64 i⟩ : U64).val = i := by + change (BitVec.ofNat 64 i).toNat = i + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + rw [u64_shl_ok (by rw [hival]; exact hi), hival]; rfl + +private theorem range'_zero_eq_range (n : Nat) : List.range' 0 n = List.range n := by + induction n with + | zero => rfl + | succ m ih => rw [List.range'_concat, ih, Nat.zero_add, Nat.one_mul, List.range_succ] + +private theorem sync_vgic_loop_refinesA (s : S) (vc : U64) (x : Fl4SpecA64.Vcpu) + (lrs : Nat) (hlrs : lrs ≤ 16) (gicV3 : Bool) + (hregs : ∀ i < lrs, (L.mem s (vcpuRegWA vc (vrVgicLr0 + i))).bv = fl4VcpuRegGet x (vrVgicLr0 + i)) : + vcpu.sync_vgic_status_loop st s vc ⟨BitVec.ofNat 64 lrs⟩ gicV3 0#u64 0#u64 0#u64 false 0#u64 = + ok (let res := (List.range lrs).foldl (syncStepSpec x gicV3) (0, 0, 0, false) + (⟨res.1⟩, ⟨res.2.1⟩, ⟨BitVec.ofNat 64 res.2.2.1⟩, res.2.2.2)) := by + apply eq_of_spec_fd + unfold vcpu.sync_vgic_status_loop + have hlrsv : (⟨BitVec.ofNat 64 lrs⟩ : U64).val = lrs := by + change (BitVec.ofNat 64 lrs).toNat = lrs + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + apply loop.spec_decr_nat + (measure := fun st5 => lrs - st5.2.2.2.2.val) + (inv := fun st5 => + let (elrsr, eisr, valid_lrs, pend, i) := st5 + i.val ≤ lrs ∧ valid_lrs.val ≤ i.val ∧ + (List.range' i.val (lrs - i.val)).foldl (syncStepSpec x gicV3) + (elrsr.bv, eisr.bv, valid_lrs.val, pend) = + (List.range lrs).foldl (syncStepSpec x gicV3) (0, 0, 0, false)) + · rintro ⟨elrsrU, eisrU, validU, pend, iU⟩ ⟨hk, hvalid, hinv⟩ + dsimp only at hk hvalid hinv ⊢ + unfold vcpu.sync_vgic_status_loop.body + by_cases hlt_u : iU < ⟨BitVec.ofNat 64 lrs⟩ + · rw [if_pos hlt_u] + set k := iU.val + set elrsr := elrsrU.bv + set eisr := eisrU.bv + set valid := validU.val + have hklt : k < lrs := by + have : iU.val < (⟨BitVec.ofNat 64 lrs⟩ : U64).val := hlt_u + rw [hlrsv] at this; exact this + have hk64 : k < 64 := by omega + have hiU_eq : iU = ⟨BitVec.ofNat 64 k⟩ := by + apply UScalar.eq_of_val_eq + change k = (BitVec.ofNat 64 k).toNat + rw [BitVec.toNat_ofNat]; exact (Nat.mod_eq_of_lt (by omega)).symm + have hvalidU_eq : validU = ⟨BitVec.ofNat 64 valid⟩ := by + apply UScalar.eq_of_val_eq + change valid = (BitVec.ofNat 64 valid).toNat + rw [BitVec.toNat_ofNat]; exact (Nat.mod_eq_of_lt (by omega)).symm + have helrsrU_eq : elrsrU = ⟨elrsr⟩ := rfl + have heisrU_eq : eisrU = ⟨eisr⟩ := rfl + have hwa_lr : wa fl4_abi.consts.vcpu_reg.VGIC_LR0 iU = + ⟨BitVec.ofNat 64 (vrVgicLr0 + k)⟩ := by + apply UScalar.eq_of_val_eq + rw [wa_valP] + have h48 : (fl4_abi.consts.vcpu_reg.VGIC_LR0).val = 48 := by + unfold fl4_abi.consts.vcpu_reg.VGIC_LR0; rfl + rw [h48] + change (48 + k) % 2 ^ 64 = (BitVec.ofNat 64 (48 + k)).toNat + rw [BitVec.toNat_ofNat] + rw [show lift (wa fl4_abi.consts.vcpu_reg.VGIC_LR0 iU) = + ok (wa fl4_abi.consts.vcpu_reg.VGIC_LR0 iU) from rfl, + bind_ok, hwa_lr, vcpu_reg_get_eqA s vc (by unfold vrVgicLr0; omega), bind_ok] + have hlr_bv : (L.mem s (vcpuRegWA vc (vrVgicLr0 + k))).bv = fl4VcpuRegGet x (vrVgicLr0 + k) := + hregs k hklt + set f := lrFields (fl4VcpuRegGet x (vrVgicLr0 + k)) gicV3 + have hfields : + (if gicV3 then do + let i2 ← L.mem s (vcpuRegWA vc (vrVgicLr0 + k)) >>> 62#i32 + let i3 ← lift (i2 &&& 3#u64) + let i4 ← L.mem s (vcpuRegWA vc (vrVgicLr0 + k)) >>> 61#i32 + let i5 ← lift (i4 &&& 1#u64) + let i6 ← L.mem s (vcpuRegWA vc (vrVgicLr0 + k)) >>> 41#i32 + let i7 ← lift (i6 &&& 1#u64) + ok (i3, i5, i7) + else do + let i2 ← L.mem s (vcpuRegWA vc (vrVgicLr0 + k)) >>> 28#i32 + let i3 ← lift (i2 &&& 3#u64) + let i4 ← L.mem s (vcpuRegWA vc (vrVgicLr0 + k)) >>> 31#i32 + let i5 ← lift (i4 &&& 1#u64) + let i6 ← L.mem s (vcpuRegWA vc (vrVgicLr0 + k)) >>> 19#i32 + let i7 ← lift (i6 &&& 1#u64) + ok (i3, i5, i7)) = + ok ((⟨f.1⟩ : U64), (⟨f.2.1⟩ : U64), (⟨f.2.2⟩ : U64)) := by + have hshr : ∀ (u : U64) (sh : I32) (n : Nat) (hsh : 0 ≤ sh.val ∧ sh.val < 64) + (hn : sh.val.toNat = n), u >>> sh = ok ⟨u.bv >>> n⟩ := + fun _ _ _ hsh hn => by rw [u64_shr_i32_ok hsh.1 hsh.2, hn] + cases gicV3 + · simp only [Bool.false_eq_true, ↓reduceIte, f, lrFields] + rw [hshr _ 28#i32 28 (by decide) rfl, + hshr _ 31#i32 31 (by decide) rfl, + hshr _ 19#i32 19 (by decide) rfl] + simp only [lift, bind_ok, hlr_bv]; rfl + · simp only [↓reduceIte, f, lrFields] + rw [hshr _ 62#i32 62 (by decide) rfl, + hshr _ 61#i32 61 (by decide) rfl, + hshr _ 41#i32 41 (by decide) rfl] + simp only [lift, bind_ok, hlr_bv]; rfl + rw [hfields, bind_ok] + set stW := f.1 + set hwW := f.2.1 + set eoiW := f.2.2 + rw [hiU_eq, hvalidU_eq, helrsrU_eq, heisrU_eq] + change (do + let (elrsr1, eisr1, valid_lrs1, has_pend1) ← + if (⟨stW⟩ : U64) = 0#u64 then + if (⟨hwW⟩ : U64) != 0#u64 then do + let i2 ← 1#u64 <<< (⟨BitVec.ofNat 64 k⟩ : U64) + let elrsr2 ← lift ((⟨elrsr⟩ : U64) ||| i2) + ok (elrsr2, (⟨eisr⟩ : U64), (⟨BitVec.ofNat 64 valid⟩ : U64), pend) + else if (⟨eoiW⟩ : U64) = 0#u64 then do + let i2 ← 1#u64 <<< (⟨BitVec.ofNat 64 k⟩ : U64) + let elrsr2 ← lift ((⟨elrsr⟩ : U64) ||| i2) + ok (elrsr2, (⟨eisr⟩ : U64), (⟨BitVec.ofNat 64 valid⟩ : U64), pend) + else do + let (i2, i3, b) ← + if (⟨stW⟩ : U64) = 0#u64 then + if (⟨hwW⟩ : U64) = 0#u64 then + if (⟨eoiW⟩ : U64) != 0#u64 then do + let i4 ← 1#u64 <<< (⟨BitVec.ofNat 64 k⟩ : U64) + let eisr2 ← lift ((⟨eisr⟩ : U64) ||| i4) + ok (eisr2, (⟨BitVec.ofNat 64 valid⟩ : U64), pend) + else do + let valid_lrs2 ← (⟨BitVec.ofNat 64 valid⟩ : U64) + 1#u64 + let b1 ← if (⟨stW⟩ : U64) = 1#u64 then ok true else ok pend + ok ((⟨eisr⟩ : U64), valid_lrs2, b1) + else do + let valid_lrs2 ← (⟨BitVec.ofNat 64 valid⟩ : U64) + 1#u64 + let b1 ← if (⟨stW⟩ : U64) = 1#u64 then ok true else ok pend + ok ((⟨eisr⟩ : U64), valid_lrs2, b1) + else do + let valid_lrs2 ← (⟨BitVec.ofNat 64 valid⟩ : U64) + 1#u64 + let b1 ← if (⟨stW⟩ : U64) = 1#u64 then ok true else ok pend + ok ((⟨eisr⟩ : U64), valid_lrs2, b1) + ok ((⟨elrsr⟩ : U64), i2, i3, b) + else do + let (i2, i3, b) ← + if (⟨stW⟩ : U64) = 0#u64 then + if (⟨hwW⟩ : U64) = 0#u64 then + if (⟨eoiW⟩ : U64) != 0#u64 then do + let i4 ← 1#u64 <<< (⟨BitVec.ofNat 64 k⟩ : U64) + let eisr2 ← lift ((⟨eisr⟩ : U64) ||| i4) + ok (eisr2, (⟨BitVec.ofNat 64 valid⟩ : U64), pend) + else do + let valid_lrs2 ← (⟨BitVec.ofNat 64 valid⟩ : U64) + 1#u64 + let b1 ← if (⟨stW⟩ : U64) = 1#u64 then ok true else ok pend + ok ((⟨eisr⟩ : U64), valid_lrs2, b1) + else do + let valid_lrs2 ← (⟨BitVec.ofNat 64 valid⟩ : U64) + 1#u64 + let b1 ← if (⟨stW⟩ : U64) = 1#u64 then ok true else ok pend + ok ((⟨eisr⟩ : U64), valid_lrs2, b1) + else do + let valid_lrs2 ← (⟨BitVec.ofNat 64 valid⟩ : U64) + 1#u64 + let b1 ← if (⟨stW⟩ : U64) = 1#u64 then ok true else ok pend + ok ((⟨eisr⟩ : U64), valid_lrs2, b1) + ok ((⟨elrsr⟩ : U64), i2, i3, b) + let i2 ← (⟨BitVec.ofNat 64 k⟩ : U64) + 1#u64 + ok (ControlFlow.cont (elrsr1, eisr1, valid_lrs1, has_pend1, i2))) ⦃ _ ⦄ + have hstep_body : + let next := syncStepSpec x gicV3 (elrsr, eisr, valid, pend) k + (if (⟨stW⟩ : U64) = 0#u64 then + if (⟨hwW⟩ : U64) != 0#u64 then do + let i2 ← 1#u64 <<< (⟨BitVec.ofNat 64 k⟩ : U64) + let elrsr2 ← lift ((⟨elrsr⟩ : U64) ||| i2) + ok (elrsr2, (⟨eisr⟩ : U64), (⟨BitVec.ofNat 64 valid⟩ : U64), pend) + else if (⟨eoiW⟩ : U64) = 0#u64 then do + let i2 ← 1#u64 <<< (⟨BitVec.ofNat 64 k⟩ : U64) + let elrsr2 ← lift ((⟨elrsr⟩ : U64) ||| i2) + ok (elrsr2, (⟨eisr⟩ : U64), (⟨BitVec.ofNat 64 valid⟩ : U64), pend) + else do + let (i2, i3, b) ← + if (⟨stW⟩ : U64) = 0#u64 then + if (⟨hwW⟩ : U64) = 0#u64 then + if (⟨eoiW⟩ : U64) != 0#u64 then do + let i4 ← 1#u64 <<< (⟨BitVec.ofNat 64 k⟩ : U64) + let eisr2 ← lift ((⟨eisr⟩ : U64) ||| i4) + ok (eisr2, (⟨BitVec.ofNat 64 valid⟩ : U64), pend) + else do + let valid_lrs2 ← (⟨BitVec.ofNat 64 valid⟩ : U64) + 1#u64 + let b1 ← if (⟨stW⟩ : U64) = 1#u64 then ok true else ok pend + ok ((⟨eisr⟩ : U64), valid_lrs2, b1) + else do + let valid_lrs2 ← (⟨BitVec.ofNat 64 valid⟩ : U64) + 1#u64 + let b1 ← if (⟨stW⟩ : U64) = 1#u64 then ok true else ok pend + ok ((⟨eisr⟩ : U64), valid_lrs2, b1) + else do + let valid_lrs2 ← (⟨BitVec.ofNat 64 valid⟩ : U64) + 1#u64 + let b1 ← if (⟨stW⟩ : U64) = 1#u64 then ok true else ok pend + ok ((⟨eisr⟩ : U64), valid_lrs2, b1) + ok ((⟨elrsr⟩ : U64), i2, i3, b) + else do + let (i2, i3, b) ← + if (⟨stW⟩ : U64) = 0#u64 then + if (⟨hwW⟩ : U64) = 0#u64 then + if (⟨eoiW⟩ : U64) != 0#u64 then do + let i4 ← 1#u64 <<< (⟨BitVec.ofNat 64 k⟩ : U64) + let eisr2 ← lift ((⟨eisr⟩ : U64) ||| i4) + ok (eisr2, (⟨BitVec.ofNat 64 valid⟩ : U64), pend) + else do + let valid_lrs2 ← (⟨BitVec.ofNat 64 valid⟩ : U64) + 1#u64 + let b1 ← if (⟨stW⟩ : U64) = 1#u64 then ok true else ok pend + ok ((⟨eisr⟩ : U64), valid_lrs2, b1) + else do + let valid_lrs2 ← (⟨BitVec.ofNat 64 valid⟩ : U64) + 1#u64 + let b1 ← if (⟨stW⟩ : U64) = 1#u64 then ok true else ok pend + ok ((⟨eisr⟩ : U64), valid_lrs2, b1) + else do + let valid_lrs2 ← (⟨BitVec.ofNat 64 valid⟩ : U64) + 1#u64 + let b1 ← if (⟨stW⟩ : U64) = 1#u64 then ok true else ok pend + ok ((⟨eisr⟩ : U64), valid_lrs2, b1) + ok ((⟨elrsr⟩ : U64), i2, i3, b)) = + ok ((⟨next.1⟩ : U64), (⟨next.2.1⟩ : U64), (⟨BitVec.ofNat 64 next.2.2.1⟩ : U64), next.2.2.2) ∧ + next.2.2.1 ≤ k + 1 := by + have hadd_v : (⟨BitVec.ofNat 64 valid⟩ : U64) + 1#u64 = ok ⟨BitVec.ofNat 64 (valid + 1)⟩ := by + have hv_val : (⟨BitVec.ofNat 64 valid⟩ : U64).val = valid := by + change (BitVec.ofNat 64 valid).toNat = valid + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + have hlt : (⟨BitVec.ofNat 64 valid⟩ : U64).val + (1#u64 : U64).val < 2 ^ 64 := by + rw [hv_val]; change _ + 1 < _; omega + rw [u64_add_ok hlt, hv_val]; rfl + unfold syncStepSpec + dsimp only [stW, hwW, eoiW] + set rhsStep := + if f.1 = 0 ∧ (f.2.1 ≠ 0 ∨ f.2.2 = 0) then (elrsr ||| (1 <<< k), eisr, valid, pend) + else if f.1 = 0 ∧ f.2.1 = 0 ∧ f.2.2 ≠ 0 then (elrsr, eisr ||| (1 <<< k), valid, pend) + else (elrsr, eisr, valid + 1, pend || decide (f.1 = 1)) + change (if (⟨f.1⟩ : U64) = 0#u64 then _ else _) = + ok ((⟨rhsStep.1⟩ : U64), (⟨rhsStep.2.1⟩ : U64), (⟨BitVec.ofNat 64 rhsStep.2.2.1⟩ : U64), rhsStep.2.2.2) ∧ + rhsStep.2.2.1 ≤ k + 1 + by_cases hst0 : f.1 = 0 + · have hst0' : (⟨f.1⟩ : U64) = 0#u64 := u64_eq_of_bv_eq hst0 + rw [if_pos hst0'] + by_cases hhw0 : f.2.1 = 0 + · have hhw0' : (⟨f.2.1⟩ : U64) = 0#u64 := u64_eq_of_bv_eq hhw0 + have hhw0b : ((⟨f.2.1⟩ : U64) != 0#u64) = false := by rw [hhw0']; decide + rw [if_neg (by rw [hhw0b]; decide)] + by_cases heoi0 : f.2.2 = 0 + · have heoi0' : (⟨f.2.2⟩ : U64) = 0#u64 := u64_eq_of_bv_eq heoi0 + have hrhs : rhsStep = (elrsr ||| (1 <<< k), eisr, valid, pend) := if_pos ⟨hst0, Or.inr heoi0⟩ + rw [if_pos heoi0', hrhs, u64_shl_one_eq k hk64, bind_ok] + simp only [lift, bind_ok] + exact ⟨rfl, by omega⟩ + · have heoi0' : (⟨f.2.2⟩ : U64) ≠ 0#u64 := fun h => heoi0 (congrArg UScalar.bv h) + have hrhs : rhsStep = (elrsr, eisr ||| (1 <<< k), valid, pend) := by + change (if f.1 = 0 ∧ (f.2.1 ≠ 0 ∨ f.2.2 = 0) then _ else _) = _ + rw [if_neg (fun h => h.2.elim (fun h1 => h1 hhw0) heoi0), if_pos ⟨hst0, hhw0, heoi0⟩] + rw [if_neg heoi0', if_pos hst0', if_pos hhw0', if_pos (bne_iff_ne.2 heoi0'), + hrhs, u64_shl_one_eq k hk64, bind_ok] + simp only [lift, bind_ok] + exact ⟨rfl, by omega⟩ + · have hhw0' : (⟨f.2.1⟩ : U64) ≠ 0#u64 := fun h => hhw0 (congrArg UScalar.bv h) + have hrhs : rhsStep = (elrsr ||| (1 <<< k), eisr, valid, pend) := if_pos ⟨hst0, Or.inl hhw0⟩ + rw [if_pos (bne_iff_ne.2 hhw0'), hrhs, u64_shl_one_eq k hk64, bind_ok] + simp only [lift, bind_ok] + exact ⟨rfl, by omega⟩ + · have hst0' : (⟨f.1⟩ : U64) ≠ 0#u64 := fun h => hst0 (congrArg UScalar.bv h) + have hrhs : rhsStep = (elrsr, eisr, valid + 1, pend || decide (f.1 = 1)) := by + change (if f.1 = 0 ∧ (f.2.1 ≠ 0 ∨ f.2.2 = 0) then _ else _) = _ + rw [if_neg (fun h => hst0 h.1), if_neg (fun h => hst0 h.1)] + rw [if_neg hst0', if_neg hst0', hrhs, hadd_v, bind_ok] + by_cases hst1 : f.1 = 1 + · have hst1' : (⟨f.1⟩ : U64) = 1#u64 := u64_eq_of_bv_eq hst1 + rw [if_pos hst1', bind_ok, bind_ok, decide_eq_true hst1, Bool.or_true] + exact ⟨rfl, by dsimp only; omega⟩ + · have hst1' : (⟨f.1⟩ : U64) ≠ 1#u64 := fun h => hst1 (congrArg UScalar.bv h) + rw [if_neg hst1', bind_ok, bind_ok, decide_eq_false hst1, Bool.or_false] + exact ⟨rfl, by dsimp only; omega⟩ + obtain ⟨hbody, hval_next⟩ := hstep_body + rw [hbody, bind_ok] + have hkv : (⟨BitVec.ofNat 64 k⟩ : U64).val = k := by + change (BitVec.ofNat 64 k).toNat = k + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + have hadd_k : (⟨BitVec.ofNat 64 k⟩ : U64) + 1#u64 = ok ⟨BitVec.ofNat 64 (k + 1)⟩ := by + have hlt : (⟨BitVec.ofNat 64 k⟩ : U64).val + (1#u64 : U64).val < 2 ^ 64 := by + rw [hkv]; change _ + 1 < _; omega + rw [u64_add_ok hlt, hkv]; rfl + change (do + let i2 ← (⟨BitVec.ofNat 64 k⟩ : U64) + 1#u64 + ok (ControlFlow.cont ( + (⟨(syncStepSpec x gicV3 (elrsr, eisr, valid, pend) k).1⟩ : U64), + (⟨(syncStepSpec x gicV3 (elrsr, eisr, valid, pend) k).2.1⟩ : U64), + (⟨BitVec.ofNat 64 (syncStepSpec x gicV3 (elrsr, eisr, valid, pend) k).2.2.1⟩ : U64), + (syncStepSpec x gicV3 (elrsr, eisr, valid, pend) k).2.2.2, i2))) ⦃ _ ⦄ + rw [hadd_k, bind_ok, WP.spec_ok] + have hk1v : (⟨BitVec.ofNat 64 (k + 1)⟩ : U64).val = k + 1 := by + change (BitVec.ofNat 64 (k + 1)).toNat = k + 1 + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + have hnv : (⟨BitVec.ofNat 64 (syncStepSpec x gicV3 (elrsr, eisr, valid, pend) k).2.2.1⟩ : U64).val = + (syncStepSpec x gicV3 (elrsr, eisr, valid, pend) k).2.2.1 := by + change (BitVec.ofNat 64 _).toNat = _ + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + refine ⟨⟨by rw [hk1v]; omega, by rw [hnv, hk1v]; exact hval_next, ?_⟩, by rw [hk1v]; omega⟩ + rw [hk1v, hnv] + have hrange : List.range' k (lrs - k) = k :: List.range' (k + 1) (lrs - (k + 1)) := by + have hm : lrs - k = (lrs - (k + 1)) + 1 := by omega + rw [hm, List.range'_succ] + rw [← hinv, hrange, List.foldl_cons] + · rw [if_neg hlt_u, WP.spec_ok] + have hkeq : iU.val = lrs := by + have : ¬ (iU.val < (⟨BitVec.ofNat 64 lrs⟩ : U64).val) := hlt_u + rw [hlrsv] at this; omega + rw [hkeq, Nat.sub_self, List.range'_zero, List.foldl_nil] at hinv + have h1 : elrsrU = ⟨((List.range lrs).foldl (syncStepSpec x gicV3) (0, 0, 0, false)).1⟩ := by + apply u64_eq_of_bv_eq; exact congrArg Prod.fst hinv + have h2 : eisrU = ⟨((List.range lrs).foldl (syncStepSpec x gicV3) (0, 0, 0, false)).2.1⟩ := by + apply u64_eq_of_bv_eq; exact congrArg (fun p => p.2.1) hinv + have h3 : validU = ⟨BitVec.ofNat 64 ((List.range lrs).foldl (syncStepSpec x gicV3) (0, 0, 0, false)).2.2.1⟩ := by + apply UScalar.eq_of_val_eq + have hv := congrArg (fun p => p.2.2.1) hinv + rw [← hv] + change validU.val = (BitVec.ofNat 64 validU.val).toNat + rw [BitVec.toNat_ofNat]; exact (Nat.mod_eq_of_lt (by omega)).symm + have h4 : pend = ((List.range lrs).foldl (syncStepSpec x gicV3) (0, 0, 0, false)).2.2.2 := + congrArg (fun p => p.2.2.2) hinv + simp only [h1, h2, h3, h4] + · refine ⟨by change 0 ≤ lrs; omega, by change 0 ≤ 0; omega, ?_⟩ + change (List.range' 0 (lrs - 0)).foldl (syncStepSpec x gicV3) (0, 0, 0, false) = _ + rw [Nat.sub_zero, range'_zero_eq_range] + +private def misrBit (b : Nat) (c : Bool) : Word := if c then (1 : Word) <<< b else 0 + +private def calcMisrSpec (eisrW hcrW vmcrW : Word) (valid : Nat) (pend : Bool) : Word := + misrBit 0 (eisrW ≠ 0) ||| misrBit 1 (hcrW &&& 2 ≠ 0 ∧ valid ≤ 1) ||| + misrBit 2 (hcrW &&& 4 ≠ 0 ∧ (hcrW >>> 27) &&& 0x1f ≠ 0) ||| misrBit 3 (hcrW &&& 8 ≠ 0 ∧ ¬pend) ||| + misrBit 4 (hcrW &&& 16 ≠ 0 ∧ vmcrW &&& 1 ≠ 0) ||| misrBit 5 (hcrW &&& 32 ≠ 0 ∧ vmcrW &&& 1 = 0) ||| + misrBit 6 (hcrW &&& 64 ≠ 0 ∧ vmcrW &&& 2 ≠ 0) ||| misrBit 7 (hcrW &&& 128 ≠ 0 ∧ vmcrW &&& 2 = 0) + +private theorem u64_or_bit_true (m : Word) (sh : Nat) {p : Prop} [Decidable p] (hp : p) : + ((⟨m⟩ : U64) ||| ⟨(1 : Word) <<< sh⟩) = ⟨m ||| misrBit sh (decide p)⟩ := by + apply u64_eq_of_bv_eq; unfold misrBit; rw [decide_eq_true hp]; rfl + +private theorem u64_or_bit_false (m : Word) (sh : Nat) {p : Prop} [Decidable p] (hp : ¬p) : + (⟨m⟩ : U64) = ⟨m ||| misrBit sh (decide p)⟩ := by + apply u64_eq_of_bv_eq; unfold misrBit; rw [decide_eq_false hp]; exact BitVec.or_zero.symm + +private theorem calcMisr_bind_eqA {α : Type} (eisrW hcrW vmcrW : Word) (valid : Nat) (hvalid : valid ≤ 16) + (pend : Bool) (k : U64 → Result α) : + (do + let misr ← if (⟨eisrW⟩ : U64) != 0#u64 then do + let i ← 1#u64 <<< 0#i32; ok (0#u64 ||| i) else ok 0#u64 + let i ← 1#u64 <<< 1#i32 + let i1 ← lift ((⟨hcrW⟩ : U64) &&& i) + let misr1 ← if i1 != 0#u64 then if (⟨BitVec.ofNat 64 valid⟩ : U64) <= 1#u64 then ok (misr ||| i) else ok misr else ok misr + let i2 ← 1#u64 <<< 2#i32 + let i3 ← lift ((⟨hcrW⟩ : U64) &&& i2) + let misr2 ← if i3 != 0#u64 then do + let i4 ← (⟨hcrW⟩ : U64) >>> 27#i32 + let i5 ← lift (i4 &&& 31#u64) + if i5 != 0#u64 then ok (misr1 ||| i2) else ok misr1 else ok misr1 + let i4 ← 1#u64 <<< 3#i32 + let i5 ← lift ((⟨hcrW⟩ : U64) &&& i4) + let misr3 ← if i5 != 0#u64 then if pend then ok misr2 else ok (misr2 ||| i4) else ok misr2 + let i6 ← 1#u64 <<< 4#i32 + let i7 ← lift ((⟨hcrW⟩ : U64) &&& i6) + let misr4 ← if i7 != 0#u64 then do + let i8 ← 1#u64 <<< 0#i32 + let i9 ← lift ((⟨vmcrW⟩ : U64) &&& i8) + if i9 != 0#u64 then ok (misr3 ||| i6) else ok misr3 else ok misr3 + let i8 ← 1#u64 <<< 5#i32 + let i9 ← lift ((⟨hcrW⟩ : U64) &&& i8) + let misr5 ← if i9 != 0#u64 then do + let i10 ← 1#u64 <<< 0#i32 + let i11 ← lift ((⟨vmcrW⟩ : U64) &&& i10) + if i11 = 0#u64 then ok (misr4 ||| i8) else ok misr4 else ok misr4 + let i10 ← 1#u64 <<< 6#i32 + let i11 ← lift ((⟨hcrW⟩ : U64) &&& i10) + let misr6 ← if i11 != 0#u64 then do + let i12 ← lift ((⟨vmcrW⟩ : U64) &&& i) + if i12 != 0#u64 then ok (misr5 ||| i10) else ok misr5 else ok misr5 + let i12 ← 1#u64 <<< 7#i32 + let i13 ← lift ((⟨hcrW⟩ : U64) &&& i12) + let misr7 ← if i13 != 0#u64 then do + let i14 ← lift ((⟨vmcrW⟩ : U64) &&& i) + if i14 = 0#u64 then ok (misr6 ||| i12) else ok misr6 else ok misr6 + k misr7) = k ⟨calcMisrSpec eisrW hcrW vmcrW valid pend⟩ := by + have hshl : ∀ (k : I32) (n : Nat) (hk : 0 ≤ k.val ∧ k.val < 64) (hn : k.val.toNat = n), + (1#u64 <<< k) = ok ⟨(1 : Word) <<< n⟩ := + fun _ _ hk hn => by rw [u64_shl_i32_ok hk.1 hk.2, hn]; rfl + have hshr27 : (⟨hcrW⟩ : U64) >>> 27#i32 = ok ⟨hcrW >>> 27⟩ := + u64_shr_i32_ok (by decide) (by decide) + have hval_le1 : ((⟨BitVec.ofNat 64 valid⟩ : U64) ≤ 1#u64) ↔ (valid ≤ 1) := by + change (BitVec.ofNat 64 valid).toNat ≤ 1 ↔ valid ≤ 1 + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)] + have hb0 : (if (⟨eisrW⟩ : U64) != 0#u64 then do let i ← 1#u64 <<< 0#i32; ok (0#u64 ||| i) else ok 0#u64) = + ok ⟨misrBit 0 (eisrW ≠ 0)⟩ := by + by_cases h : eisrW ≠ 0 + · rw [if_pos (bne_iff_ne.2 (fun e => h (congrArg UScalar.bv e))), hshl 0#i32 0 (by decide) rfl, bind_ok] + congr 1; apply u64_eq_of_bv_eq; unfold misrBit; rw [decide_eq_true h]; exact BitVec.zero_or + · have hf : ((⟨eisrW⟩ : U64) != 0#u64) = false := by + rw [bne_eq_false_iff_eq]; exact u64_eq_of_bv_eq (not_not.1 h) + rw [if_neg (by rw [hf]; decide)] + congr 1; apply u64_eq_of_bv_eq; unfold misrBit; rw [decide_eq_false h]; rfl + rw [hb0, bind_ok] + unfold calcMisrSpec + generalize misrBit 0 (eisrW ≠ 0) = m0 + rw [hshl 1#i32 1 (by decide) rfl, bind_ok] + rw [show lift ((⟨hcrW⟩ : U64) &&& ⟨(1 : Word) <<< 1⟩) = ok ((⟨hcrW⟩ : U64) &&& ⟨(1 : Word) <<< 1⟩) from rfl, bind_ok] + have h1 : (if (((⟨hcrW⟩ : U64) &&& ⟨(1 : Word) <<< 1⟩) != 0#u64) then + if (⟨BitVec.ofNat 64 valid⟩ : U64) <= 1#u64 then ok ((⟨m0⟩ : U64) ||| ⟨(1 : Word) <<< 1⟩) else ok (⟨m0⟩ : U64) + else ok (⟨m0⟩ : U64)) = ok ⟨m0 ||| misrBit 1 (hcrW &&& 2 ≠ 0 ∧ valid ≤ 1)⟩ := by + by_cases c1 : hcrW &&& 2 ≠ 0 + · have c1' : (((⟨hcrW⟩ : U64) &&& ⟨(1 : Word) <<< 1⟩) != 0#u64) = true := + bne_iff_ne.2 (fun e => c1 (congrArg UScalar.bv e)) + rw [if_pos c1'] + by_cases c2 : valid ≤ 1 + · rw [if_pos (hval_le1.2 c2)]; congr 1; exact u64_or_bit_true m0 1 ⟨c1, c2⟩ + · rw [if_neg (mt hval_le1.1 c2)]; congr 1; exact u64_or_bit_false m0 1 (fun h => c2 h.2) + · have c1' : (((⟨hcrW⟩ : U64) &&& ⟨(1 : Word) <<< 1⟩) != 0#u64) = false := by + rw [bne_eq_false_iff_eq]; exact u64_eq_of_bv_eq (not_not.1 c1) + rw [if_neg (by rw [c1']; decide)]; congr 1; exact u64_or_bit_false m0 1 (fun h => c1 h.1) + rw [h1, bind_ok] + generalize m0 ||| misrBit 1 (hcrW &&& 2 ≠ 0 ∧ valid ≤ 1) = m1 + rw [hshl 2#i32 2 (by decide) rfl, bind_ok] + rw [show lift ((⟨hcrW⟩ : U64) &&& ⟨(1 : Word) <<< 2⟩) = ok ((⟨hcrW⟩ : U64) &&& ⟨(1 : Word) <<< 2⟩) from rfl, bind_ok] + have h2 : (if (((⟨hcrW⟩ : U64) &&& ⟨(1 : Word) <<< 2⟩) != 0#u64) then do + let i4 ← (⟨hcrW⟩ : U64) >>> 27#i32 + let i5 ← lift (i4 &&& 31#u64) + if i5 != 0#u64 then ok ((⟨m1⟩ : U64) ||| ⟨(1 : Word) <<< 2⟩) else ok (⟨m1⟩ : U64) + else ok (⟨m1⟩ : U64)) = ok ⟨m1 ||| misrBit 2 (hcrW &&& 4 ≠ 0 ∧ (hcrW >>> 27) &&& 0x1f ≠ 0)⟩ := by + by_cases c1 : hcrW &&& 4 ≠ 0 + · have c1' : (((⟨hcrW⟩ : U64) &&& ⟨(1 : Word) <<< 2⟩) != 0#u64) = true := + bne_iff_ne.2 (fun e => c1 (congrArg UScalar.bv e)) + rw [if_pos c1', hshr27, bind_ok]; rw [show lift ((⟨hcrW >>> 27⟩ : U64) &&& 31#u64) = ok ((⟨hcrW >>> 27⟩ : U64) &&& 31#u64) from rfl, bind_ok] + by_cases c2 : (hcrW >>> 27) &&& 0x1f ≠ 0 + · rw [if_pos (bne_iff_ne.2 (fun e => c2 (congrArg UScalar.bv e)))] + congr 1; exact u64_or_bit_true m1 2 ⟨c1, c2⟩ + · have c2' : (((⟨hcrW >>> 27⟩ : U64) &&& 31#u64) != 0#u64) = false := by + rw [bne_eq_false_iff_eq]; exact u64_eq_of_bv_eq (not_not.1 c2) + rw [if_neg (by rw [c2']; decide)]; congr 1; exact u64_or_bit_false m1 2 (fun h => c2 h.2) + · have c1' : (((⟨hcrW⟩ : U64) &&& ⟨(1 : Word) <<< 2⟩) != 0#u64) = false := by + rw [bne_eq_false_iff_eq]; exact u64_eq_of_bv_eq (not_not.1 c1) + rw [if_neg (by rw [c1']; decide)]; congr 1; exact u64_or_bit_false m1 2 (fun h => c1 h.1) + rw [h2, bind_ok] + generalize m1 ||| misrBit 2 (hcrW &&& 4 ≠ 0 ∧ (hcrW >>> 27) &&& 0x1f ≠ 0) = m2 + rw [hshl 3#i32 3 (by decide) rfl, bind_ok] + rw [show lift ((⟨hcrW⟩ : U64) &&& ⟨(1 : Word) <<< 3⟩) = ok ((⟨hcrW⟩ : U64) &&& ⟨(1 : Word) <<< 3⟩) from rfl, bind_ok] + have h3 : (if (((⟨hcrW⟩ : U64) &&& ⟨(1 : Word) <<< 3⟩) != 0#u64) then + if pend then ok (⟨m2⟩ : U64) else ok ((⟨m2⟩ : U64) ||| ⟨(1 : Word) <<< 3⟩) else ok (⟨m2⟩ : U64)) = + ok ⟨m2 ||| misrBit 3 (hcrW &&& 8 ≠ 0 ∧ ¬pend)⟩ := by + by_cases c1 : hcrW &&& 8 ≠ 0 + · have c1' : (((⟨hcrW⟩ : U64) &&& ⟨(1 : Word) <<< 3⟩) != 0#u64) = true := + bne_iff_ne.2 (fun e => c1 (congrArg UScalar.bv e)) + rw [if_pos c1'] + cases pend + · rw [if_neg (by decide)]; congr 1; exact u64_or_bit_true m2 3 ⟨c1, by decide⟩ + · rw [if_pos rfl]; congr 1; exact u64_or_bit_false m2 3 (fun h => h.2 rfl) + · have c1' : (((⟨hcrW⟩ : U64) &&& ⟨(1 : Word) <<< 3⟩) != 0#u64) = false := by + rw [bne_eq_false_iff_eq]; exact u64_eq_of_bv_eq (not_not.1 c1) + rw [if_neg (by rw [c1']; decide)]; congr 1; exact u64_or_bit_false m2 3 (fun h => c1 h.1) + rw [h3, bind_ok] + generalize m2 ||| misrBit 3 (hcrW &&& 8 ≠ 0 ∧ ¬pend) = m3 + rw [hshl 4#i32 4 (by decide) rfl, bind_ok] + rw [show lift ((⟨hcrW⟩ : U64) &&& ⟨(1 : Word) <<< 4⟩) = ok ((⟨hcrW⟩ : U64) &&& ⟨(1 : Word) <<< 4⟩) from rfl, bind_ok] + have h4 : (if (((⟨hcrW⟩ : U64) &&& ⟨(1 : Word) <<< 4⟩) != 0#u64) then do + let i8 ← 1#u64 <<< 0#i32 + let i9 ← lift ((⟨vmcrW⟩ : U64) &&& i8) + if i9 != 0#u64 then ok ((⟨m3⟩ : U64) ||| ⟨(1 : Word) <<< 4⟩) else ok (⟨m3⟩ : U64) else ok (⟨m3⟩ : U64)) = + ok ⟨m3 ||| misrBit 4 (hcrW &&& 16 ≠ 0 ∧ vmcrW &&& 1 ≠ 0)⟩ := by + by_cases c1 : hcrW &&& 16 ≠ 0 + · have c1' : (((⟨hcrW⟩ : U64) &&& ⟨(1 : Word) <<< 4⟩) != 0#u64) = true := + bne_iff_ne.2 (fun e => c1 (congrArg UScalar.bv e)) + rw [if_pos c1', hshl 0#i32 0 (by decide) rfl, bind_ok]; rw [show lift ((⟨vmcrW⟩ : U64) &&& ⟨(1 : Word) <<< 0⟩) = ok ((⟨vmcrW⟩ : U64) &&& ⟨(1 : Word) <<< 0⟩) from rfl, bind_ok] + by_cases c2 : vmcrW &&& 1 ≠ 0 + · rw [if_pos (bne_iff_ne.2 (fun e => c2 (congrArg UScalar.bv e)))] + congr 1; exact u64_or_bit_true m3 4 ⟨c1, c2⟩ + · have c2' : (((⟨vmcrW⟩ : U64) &&& ⟨(1 : Word) <<< 0⟩) != 0#u64) = false := by + rw [bne_eq_false_iff_eq]; exact u64_eq_of_bv_eq (not_not.1 c2) + rw [if_neg (by rw [c2']; decide)]; congr 1; exact u64_or_bit_false m3 4 (fun h => c2 h.2) + · have c1' : (((⟨hcrW⟩ : U64) &&& ⟨(1 : Word) <<< 4⟩) != 0#u64) = false := by + rw [bne_eq_false_iff_eq]; exact u64_eq_of_bv_eq (not_not.1 c1) + rw [if_neg (by rw [c1']; decide)]; congr 1; exact u64_or_bit_false m3 4 (fun h => c1 h.1) + rw [h4, bind_ok] + generalize m3 ||| misrBit 4 (hcrW &&& 16 ≠ 0 ∧ vmcrW &&& 1 ≠ 0) = m4 + rw [hshl 5#i32 5 (by decide) rfl, bind_ok] + rw [show lift ((⟨hcrW⟩ : U64) &&& ⟨(1 : Word) <<< 5⟩) = ok ((⟨hcrW⟩ : U64) &&& ⟨(1 : Word) <<< 5⟩) from rfl, bind_ok] + have h5 : (if (((⟨hcrW⟩ : U64) &&& ⟨(1 : Word) <<< 5⟩) != 0#u64) then do + let i10 ← 1#u64 <<< 0#i32 + let i11 ← lift ((⟨vmcrW⟩ : U64) &&& i10) + if i11 = 0#u64 then ok ((⟨m4⟩ : U64) ||| ⟨(1 : Word) <<< 5⟩) else ok (⟨m4⟩ : U64) else ok (⟨m4⟩ : U64)) = + ok ⟨m4 ||| misrBit 5 (hcrW &&& 32 ≠ 0 ∧ vmcrW &&& 1 = 0)⟩ := by + by_cases c1 : hcrW &&& 32 ≠ 0 + · have c1' : (((⟨hcrW⟩ : U64) &&& ⟨(1 : Word) <<< 5⟩) != 0#u64) = true := + bne_iff_ne.2 (fun e => c1 (congrArg UScalar.bv e)) + rw [if_pos c1', hshl 0#i32 0 (by decide) rfl, bind_ok]; rw [show lift ((⟨vmcrW⟩ : U64) &&& ⟨(1 : Word) <<< 0⟩) = ok ((⟨vmcrW⟩ : U64) &&& ⟨(1 : Word) <<< 0⟩) from rfl, bind_ok] + by_cases c2 : vmcrW &&& 1 = 0 + · rw [if_pos (u64_eq_of_bv_eq (show (((⟨vmcrW⟩ : U64) &&& ⟨(1 : Word) <<< 0⟩) : U64).bv = (0#u64 : U64).bv from c2))] + congr 1; exact u64_or_bit_true m4 5 ⟨c1, c2⟩ + · rw [if_neg (fun e => c2 (congrArg UScalar.bv e))]; congr 1; exact u64_or_bit_false m4 5 (fun h => c2 h.2) + · have c1' : (((⟨hcrW⟩ : U64) &&& ⟨(1 : Word) <<< 5⟩) != 0#u64) = false := by + rw [bne_eq_false_iff_eq]; exact u64_eq_of_bv_eq (not_not.1 c1) + rw [if_neg (by rw [c1']; decide)]; congr 1; exact u64_or_bit_false m4 5 (fun h => c1 h.1) + rw [h5, bind_ok] + generalize m4 ||| misrBit 5 (hcrW &&& 32 ≠ 0 ∧ vmcrW &&& 1 = 0) = m5 + rw [hshl 6#i32 6 (by decide) rfl, bind_ok] + rw [show lift ((⟨hcrW⟩ : U64) &&& ⟨(1 : Word) <<< 6⟩) = ok ((⟨hcrW⟩ : U64) &&& ⟨(1 : Word) <<< 6⟩) from rfl, bind_ok] + have h6 : (if (((⟨hcrW⟩ : U64) &&& ⟨(1 : Word) <<< 6⟩) != 0#u64) then do + let i12 ← lift ((⟨vmcrW⟩ : U64) &&& ⟨(1 : Word) <<< 1⟩) + if i12 != 0#u64 then ok ((⟨m5⟩ : U64) ||| ⟨(1 : Word) <<< 6⟩) else ok (⟨m5⟩ : U64) else ok (⟨m5⟩ : U64)) = + ok ⟨m5 ||| misrBit 6 (hcrW &&& 64 ≠ 0 ∧ vmcrW &&& 2 ≠ 0)⟩ := by + by_cases c1 : hcrW &&& 64 ≠ 0 + · have c1' : (((⟨hcrW⟩ : U64) &&& ⟨(1 : Word) <<< 6⟩) != 0#u64) = true := + bne_iff_ne.2 (fun e => c1 (congrArg UScalar.bv e)) + rw [if_pos c1']; rw [show lift ((⟨vmcrW⟩ : U64) &&& ⟨(1 : Word) <<< 1⟩) = ok ((⟨vmcrW⟩ : U64) &&& ⟨(1 : Word) <<< 1⟩) from rfl, bind_ok] + by_cases c2 : vmcrW &&& 2 ≠ 0 + · rw [if_pos (bne_iff_ne.2 (fun e => c2 (congrArg UScalar.bv e)))] + congr 1; exact u64_or_bit_true m5 6 ⟨c1, c2⟩ + · have c2' : (((⟨vmcrW⟩ : U64) &&& ⟨(1 : Word) <<< 1⟩) != 0#u64) = false := by + rw [bne_eq_false_iff_eq]; exact u64_eq_of_bv_eq (not_not.1 c2) + rw [if_neg (by rw [c2']; decide)]; congr 1; exact u64_or_bit_false m5 6 (fun h => c2 h.2) + · have c1' : (((⟨hcrW⟩ : U64) &&& ⟨(1 : Word) <<< 6⟩) != 0#u64) = false := by + rw [bne_eq_false_iff_eq]; exact u64_eq_of_bv_eq (not_not.1 c1) + rw [if_neg (by rw [c1']; decide)]; congr 1; exact u64_or_bit_false m5 6 (fun h => c1 h.1) + rw [h6, bind_ok] + generalize m5 ||| misrBit 6 (hcrW &&& 64 ≠ 0 ∧ vmcrW &&& 2 ≠ 0) = m6 + rw [hshl 7#i32 7 (by decide) rfl, bind_ok] + rw [show lift ((⟨hcrW⟩ : U64) &&& ⟨(1 : Word) <<< 7⟩) = ok ((⟨hcrW⟩ : U64) &&& ⟨(1 : Word) <<< 7⟩) from rfl, bind_ok] + have h7 : (if (((⟨hcrW⟩ : U64) &&& ⟨(1 : Word) <<< 7⟩) != 0#u64) then do + let i14 ← lift ((⟨vmcrW⟩ : U64) &&& ⟨(1 : Word) <<< 1⟩) + if i14 = 0#u64 then ok ((⟨m6⟩ : U64) ||| ⟨(1 : Word) <<< 7⟩) else ok (⟨m6⟩ : U64) else ok (⟨m6⟩ : U64)) = + ok ⟨m6 ||| misrBit 7 (hcrW &&& 128 ≠ 0 ∧ vmcrW &&& 2 = 0)⟩ := by + by_cases c1 : hcrW &&& 128 ≠ 0 + · have c1' : (((⟨hcrW⟩ : U64) &&& ⟨(1 : Word) <<< 7⟩) != 0#u64) = true := + bne_iff_ne.2 (fun e => c1 (congrArg UScalar.bv e)) + rw [if_pos c1']; rw [show lift ((⟨vmcrW⟩ : U64) &&& ⟨(1 : Word) <<< 1⟩) = ok ((⟨vmcrW⟩ : U64) &&& ⟨(1 : Word) <<< 1⟩) from rfl, bind_ok] + by_cases c2 : vmcrW &&& 2 = 0 + · rw [if_pos (u64_eq_of_bv_eq (show (((⟨vmcrW⟩ : U64) &&& ⟨(1 : Word) <<< 1⟩) : U64).bv = (0#u64 : U64).bv from c2))] + congr 1; exact u64_or_bit_true m6 7 ⟨c1, c2⟩ + · rw [if_neg (fun e => c2 (congrArg UScalar.bv e))]; congr 1; exact u64_or_bit_false m6 7 (fun h => c2 h.2) + · have c1' : (((⟨hcrW⟩ : U64) &&& ⟨(1 : Word) <<< 7⟩) != 0#u64) = false := by + rw [bne_eq_false_iff_eq]; exact u64_eq_of_bv_eq (not_not.1 c1) + rw [if_neg (by rw [c1']; decide)]; congr 1; exact u64_or_bit_false m6 7 (fun h => c1 h.1) + rw [h7, bind_ok] + +/-- **`vcpu.sync_vgic_status` refinement**: computes the exact same `ELRSR`, `EISR`, `MISR` words +as `Fl4SpecA64.syncVgicStatus`, writing strictly to registers 39, 38, 37 and preserving `VcpuRelA`. -/ +theorem sync_vgic_status_refinesA {Z : XSetsA} {OS Ub : Set U64} {s : S} {σ : State Unit} + (hV : VcpuRelA (L := L) Z OS Ub s σ) + (hksFit : (L.ksb s).val + ksSizeA ≤ 2 ^ 64) + (hCs : ∀ c ∈ Z.Cs, c.val < 128) + (hIdleT : ∀ c ∈ Z.Cs, L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) ∈ Z.T) + {vc : U64} (hvc : vc ∈ Z.V) {x : Fl4SpecA64.Vcpu} + (hk : σ.kheap (specPtr vc) = some (.archObj (.vcpu x))) : + vcpu.sync_vgic_status st s vc ⟨BitVec.ofNat 64 fl4NumLrs⟩ fl4GicV3 ⦃ s' => + VcpuRelA (L := L) Z OS Ub s' + (updKSc σ (specPtr vc) (.archObj (.vcpu (syncVgicStatus x fl4NumLrs fl4GicV3)))) ∧ + WritesIn st s s' (fun b => + b = vcpuRegWA vc vrVgicElrsr ∨ b = vcpuRegWA vc vrVgicEisr ∨ b = vcpuRegWA vc vrVgicMisr) ⦄ := by + have hlrs16 : fl4NumLrs ≤ 16 := fl4NumLrs_le_16 + have hregs : ∀ i < fl4NumLrs, (L.mem s (vcpuRegWA vc (vrVgicLr0 + i))).bv = + fl4VcpuRegGet x (vrVgicLr0 + i) := + fun i hi => hV.regs vc hvc x hk (vrVgicLr0 + i) (by unfold vrVgicLr0; omega) + have hloop := sync_vgic_loop_refinesA s vc x fl4NumLrs hlrs16 fl4GicV3 hregs + unfold vcpu.sync_vgic_status + rw [hloop, bind_ok] + set acc := (List.range fl4NumLrs).foldl (syncStepSpec x fl4GicV3) (0, 0, 0, false) + have hhcr_eq : vcpu.reg_get st s vc fl4_abi.consts.vcpu_reg.VGIC_HCR = ok (L.mem s (vcpuRegWA vc vrVgicHcr)) := by + rw [show fl4_abi.consts.vcpu_reg.VGIC_HCR = ⟨BitVec.ofNat 64 vrVgicHcr⟩ by + unfold fl4_abi.consts.vcpu_reg.VGIC_HCR vrVgicHcr; rfl] + exact vcpu_reg_get_eqA s vc (i := vrVgicHcr) (by unfold vrVgicHcr; omega) + have hvmcr_eq : vcpu.reg_get st s vc fl4_abi.consts.vcpu_reg.VGIC_VMCR = ok (L.mem s (vcpuRegWA vc vrVgicVmcr)) := by + rw [show fl4_abi.consts.vcpu_reg.VGIC_VMCR = ⟨BitVec.ofNat 64 vrVgicVmcr⟩ by + unfold fl4_abi.consts.vcpu_reg.VGIC_VMCR vrVgicVmcr; rfl] + exact vcpu_reg_get_eqA s vc (i := vrVgicVmcr) (by unfold vrVgicVmcr; omega) + have hhcr_u : L.mem s (vcpuRegWA vc vrVgicHcr) = ⟨fl4VcpuRegGet x vrVgicHcr⟩ := + u64_eq_of_bv_eq (hV.regs vc hvc x hk vrVgicHcr (by unfold vrVgicHcr; omega)) + have hvmcr_u : L.mem s (vcpuRegWA vc vrVgicVmcr) = ⟨fl4VcpuRegGet x vrVgicVmcr⟩ := + u64_eq_of_bv_eq (hV.regs vc hvc x hk vrVgicVmcr (by unfold vrVgicVmcr; omega)) + set hcrW := fl4VcpuRegGet x vrVgicHcr + set vmcrW := fl4VcpuRegGet x vrVgicVmcr + change (do + let hcr ← vcpu.reg_get st s vc fl4_abi.consts.vcpu_reg.VGIC_HCR + let vmcr ← vcpu.reg_get st s vc fl4_abi.consts.vcpu_reg.VGIC_VMCR + let misr ← if (⟨acc.2.1⟩ : U64) != 0#u64 then do + let i ← 1#u64 <<< 0#i32; ok (0#u64 ||| i) else ok 0#u64 + let i ← 1#u64 <<< 1#i32 + let i1 ← lift (hcr &&& i) + let misr1 ← if i1 != 0#u64 then if (⟨BitVec.ofNat 64 acc.2.2.1⟩ : U64) <= 1#u64 then ok (misr ||| i) else ok misr else ok misr + let i2 ← 1#u64 <<< 2#i32 + let i3 ← lift (hcr &&& i2) + let misr2 ← if i3 != 0#u64 then do + let i4 ← hcr >>> 27#i32 + let i5 ← lift (i4 &&& 31#u64) + if i5 != 0#u64 then ok (misr1 ||| i2) else ok misr1 else ok misr1 + let i4 ← 1#u64 <<< 3#i32 + let i5 ← lift (hcr &&& i4) + let misr3 ← if i5 != 0#u64 then if acc.2.2.2 then ok misr2 else ok (misr2 ||| i4) else ok misr2 + let i6 ← 1#u64 <<< 4#i32 + let i7 ← lift (hcr &&& i6) + let misr4 ← if i7 != 0#u64 then do + let i8 ← 1#u64 <<< 0#i32 + let i9 ← lift (vmcr &&& i8) + if i9 != 0#u64 then ok (misr3 ||| i6) else ok misr3 else ok misr3 + let i8 ← 1#u64 <<< 5#i32 + let i9 ← lift (hcr &&& i8) + let misr5 ← if i9 != 0#u64 then do + let i10 ← 1#u64 <<< 0#i32 + let i11 ← lift (vmcr &&& i10) + if i11 = 0#u64 then ok (misr4 ||| i8) else ok misr4 else ok misr4 + let i10 ← 1#u64 <<< 6#i32 + let i11 ← lift (hcr &&& i10) + let misr6 ← if i11 != 0#u64 then do + let i12 ← lift (vmcr &&& i) + if i12 != 0#u64 then ok (misr5 ||| i10) else ok misr5 else ok misr5 + let i12 ← 1#u64 <<< 7#i32 + let i13 ← lift (hcr &&& i12) + let misr7 ← if i13 != 0#u64 then do + let i14 ← lift (vmcr &&& i) + if i14 = 0#u64 then ok (misr6 ||| i12) else ok misr6 else ok misr6 + let s1 ← vcpu.reg_set st s vc fl4_abi.consts.vcpu_reg.VGIC_ELRSR ⟨acc.1⟩ + let s2 ← vcpu.reg_set st s1 vc fl4_abi.consts.vcpu_reg.VGIC_EISR ⟨acc.2.1⟩ + vcpu.reg_set st s2 vc fl4_abi.consts.vcpu_reg.VGIC_MISR misr7) ⦃ _ ⦄ + rw [hhcr_eq, bind_ok, hvmcr_eq, bind_ok, hhcr_u, hvmcr_u] + have hvalid_le : acc.2.2.1 ≤ 16 := by + have hfold : ∀ (xs : List Nat) (a : Word × Word × Nat × Bool), + (xs.foldl (syncStepSpec x fl4GicV3) a).2.2.1 ≤ a.2.2.1 + xs.length := by + intro xs + induction xs with + | nil => intro a; simp + | cons hd tl ih => + intro a + simp only [List.foldl_cons, List.length_cons] + have h1 : (syncStepSpec x fl4GicV3 a hd).2.2.1 ≤ a.2.2.1 + 1 := by + rcases a with ⟨elrsr, eisr, valid, pend⟩ + unfold syncStepSpec; dsimp only; split_ifs <;> dsimp only <;> omega + have h2 := ih (syncStepSpec x fl4GicV3 a hd) + omega + have h0 := hfold (List.range fl4NumLrs) (0, 0, 0, false) + rw [List.length_range] at h0 + dsimp only at h0 + change ((List.range fl4NumLrs).foldl (syncStepSpec x fl4GicV3) (0, 0, 0, false)).2.2.1 ≤ 16 + omega + have hmisr_step := calcMisr_bind_eqA acc.2.1 hcrW vmcrW acc.2.2.1 hvalid_le acc.2.2.2 + (fun misr7 => do + let s1 ← vcpu.reg_set st s vc fl4_abi.consts.vcpu_reg.VGIC_ELRSR ⟨acc.1⟩ + let s2 ← vcpu.reg_set st s1 vc fl4_abi.consts.vcpu_reg.VGIC_EISR ⟨acc.2.1⟩ + vcpu.reg_set st s2 vc fl4_abi.consts.vcpu_reg.VGIC_MISR misr7) + have hset1 : vcpu.reg_set st s vc fl4_abi.consts.vcpu_reg.VGIC_ELRSR ⟨acc.1⟩ = + ok (L.wr s (vcpuRegWA vc vrVgicElrsr) ⟨acc.1⟩) := by + rw [show fl4_abi.consts.vcpu_reg.VGIC_ELRSR = ⟨BitVec.ofNat 64 vrVgicElrsr⟩ by + unfold fl4_abi.consts.vcpu_reg.VGIC_ELRSR vrVgicElrsr; rfl] + exact vcpu_reg_set_eqA s vc (i := vrVgicElrsr) (by unfold vrVgicElrsr; omega) _ + have hset2 : ∀ s1, vcpu.reg_set st s1 vc fl4_abi.consts.vcpu_reg.VGIC_EISR ⟨acc.2.1⟩ = + ok (L.wr s1 (vcpuRegWA vc vrVgicEisr) ⟨acc.2.1⟩) := fun s1 => by + rw [show fl4_abi.consts.vcpu_reg.VGIC_EISR = ⟨BitVec.ofNat 64 vrVgicEisr⟩ by + unfold fl4_abi.consts.vcpu_reg.VGIC_EISR vrVgicEisr; rfl] + exact vcpu_reg_set_eqA s1 vc (i := vrVgicEisr) (by unfold vrVgicEisr; omega) _ + have hset3 : ∀ s2 m, vcpu.reg_set st s2 vc fl4_abi.consts.vcpu_reg.VGIC_MISR m = + ok (L.wr s2 (vcpuRegWA vc vrVgicMisr) m) := fun s2 m => by + rw [show fl4_abi.consts.vcpu_reg.VGIC_MISR = ⟨BitVec.ofNat 64 vrVgicMisr⟩ by + unfold fl4_abi.consts.vcpu_reg.VGIC_MISR vrVgicMisr; rfl] + exact vcpu_reg_set_eqA s2 vc (i := vrVgicMisr) (by unfold vrVgicMisr; omega) m + rw [hmisr_step, hset1, bind_ok, hset2, bind_ok, hset3, WP.spec_ok] + -- Apply `VcpuRelA.upd_regA` 3 times (registers 39, 38, 37) + have h1 := hV.upd_regA hksFit hCs hIdleT hvc hk (i := vrVgicElrsr) (by unfold vrVgicElrsr; omega) ⟨acc.1⟩ + (fun h => by simp [vrVgicElrsr, vrVgicHcr] at h) + have hks1 : (L.ksb (L.wr s (vcpuRegWA vc vrVgicElrsr) ⟨acc.1⟩)).val + ksSizeA ≤ 2 ^ 64 := by + rw [L.ksb_wr]; exact hksFit + have hidle1 : ∀ c ∈ Z.Cs, L.mem (L.wr s (vcpuRegWA vc vrVgicElrsr) ⟨acc.1⟩) + (L.nodeAt (L.wr s (vcpuRegWA vc vrVgicElrsr) ⟨acc.1⟩) c layout.node.IDLE_THREAD) ∈ Z.T := by + intro c hc + have hnd : L.nodeAt (L.wr s (vcpuRegWA vc vrVgicElrsr) ⟨acc.1⟩) c layout.node.IDLE_THREAD = + L.nodeAt s c layout.node.IDLE_THREAD := by simp [LawfulStore.nodeAt, L.ksb_wr] + rw [hnd, mem_wr_neA (hV.place.vcPage_al (vcpuRegWA_in_vcPage hvc (by unfold vrVgicElrsr; omega))) + (hV.place.node_al c layout.node.IDLE_THREAD (by unfold layout.node.IDLE_THREAD; decide)) + (hV.place.disj_node hksFit (vcpuRegWA_in_vcPage hvc (by unfold vrVgicElrsr; omega)) + (hCs c hc) (by unfold layout.node.IDLE_THREAD; decide))] + exact hIdleT c hc + have h2 := h1.upd_regA hks1 hCs hidle1 hvc (updKSc_same _ _ _) (i := vrVgicEisr) + (by unfold vrVgicEisr; omega) ⟨acc.2.1⟩ (fun h => by simp [vrVgicEisr, vrVgicHcr] at h) + have hks2 : (L.ksb (L.wr (L.wr s (vcpuRegWA vc vrVgicElrsr) ⟨acc.1⟩) (vcpuRegWA vc vrVgicEisr) ⟨acc.2.1⟩)).val + ksSizeA ≤ 2 ^ 64 := by + rw [L.ksb_wr]; exact hks1 + have hidle2 : ∀ c ∈ Z.Cs, L.mem (L.wr (L.wr s (vcpuRegWA vc vrVgicElrsr) ⟨acc.1⟩) (vcpuRegWA vc vrVgicEisr) ⟨acc.2.1⟩) + (L.nodeAt (L.wr (L.wr s (vcpuRegWA vc vrVgicElrsr) ⟨acc.1⟩) (vcpuRegWA vc vrVgicEisr) ⟨acc.2.1⟩) c layout.node.IDLE_THREAD) ∈ Z.T := by + intro c hc + have hnd : L.nodeAt (L.wr (L.wr s (vcpuRegWA vc vrVgicElrsr) ⟨acc.1⟩) (vcpuRegWA vc vrVgicEisr) ⟨acc.2.1⟩) c layout.node.IDLE_THREAD = + L.nodeAt (L.wr s (vcpuRegWA vc vrVgicElrsr) ⟨acc.1⟩) c layout.node.IDLE_THREAD := by simp [LawfulStore.nodeAt, L.ksb_wr] + rw [hnd, mem_wr_neA (h1.place.vcPage_al (vcpuRegWA_in_vcPage hvc (by unfold vrVgicEisr; omega))) + (h1.place.node_al c layout.node.IDLE_THREAD (by unfold layout.node.IDLE_THREAD; decide)) + (h1.place.disj_node hks1 (vcpuRegWA_in_vcPage hvc (by unfold vrVgicEisr; omega)) + (hCs c hc) (by unfold layout.node.IDLE_THREAD; decide))] + exact hidle1 c hc + have h3 := h2.upd_regA hks2 hCs hidle2 hvc (updKSc_same _ _ _) (i := vrVgicMisr) + (by unfold vrVgicMisr; omega) ⟨calcMisrSpec acc.2.1 hcrW vmcrW acc.2.2.1 acc.2.2.2⟩ + (fun h => by simp [vrVgicMisr, vrVgicHcr] at h) + have hsync_eq : ∀ (n : Nat) (g : Bool), + let a := (List.range n).foldl (syncStepSpec x g) (0, 0, 0, false) + fl4VcpuRegSet vrVgicMisr (calcMisrSpec a.2.1 hcrW vmcrW a.2.2.1 a.2.2.2) + (fl4VcpuRegSet vrVgicEisr a.2.1 (fl4VcpuRegSet vrVgicElrsr a.1 x)) = + syncVgicStatus x n g := fun _ _ => rfl + have hupd_eq : updKSc (updKSc (updKSc σ (specPtr vc) (.archObj (.vcpu (fl4VcpuRegSet vrVgicElrsr acc.1 x)))) + (specPtr vc) (.archObj (.vcpu (fl4VcpuRegSet vrVgicEisr acc.2.1 (fl4VcpuRegSet vrVgicElrsr acc.1 x))))) + (specPtr vc) (.archObj (.vcpu (fl4VcpuRegSet vrVgicMisr (calcMisrSpec acc.2.1 hcrW vmcrW acc.2.2.1 acc.2.2.2) (fl4VcpuRegSet vrVgicEisr acc.2.1 (fl4VcpuRegSet vrVgicElrsr acc.1 x))))) = + updKSc σ (specPtr vc) (.archObj (.vcpu (syncVgicStatus x fl4NumLrs fl4GicV3))) := by + rw [updKSc_updKSc, updKSc_updKSc, hsync_eq fl4NumLrs fl4GicV3] + rw [hupd_eq] at h3 + refine ⟨h3, by simp [L.ksb_wr], by simp [L.cpu_wr], fun b hba hnot => ?_⟩ + rw [mem_wr_neA (hV.place.vcPage_al (vcpuRegWA_in_vcPage hvc (by unfold vrVgicMisr; omega))) hba + (fun h => hnot (Or.inr (Or.inr h.symm))), + mem_wr_neA (hV.place.vcPage_al (vcpuRegWA_in_vcPage hvc (by unfold vrVgicEisr; omega))) hba + (fun h => hnot (Or.inr (Or.inl h.symm))), + mem_wr_neA (hV.place.vcPage_al (vcpuRegWA_in_vcPage hvc (by unfold vrVgicElrsr; omega))) hba + (fun h => hnot (Or.inl h.symm))] + +end sync_vgic + +end + +end Fl4ProofsA64.P3.Vcpu diff --git a/proofs/Fl4ProofsA64/P3/VcpuPremisesA.lean b/proofs/Fl4ProofsA64/P3/VcpuPremisesA.lean new file mode 100644 index 000000000..a447c8918 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/VcpuPremisesA.lean @@ -0,0 +1,62 @@ +/- +P3 (aarch64, A64-P3 S4 VCPU): **Shared VCPU platform premises and detach spec helper**. + +Definitions only (no proofs): leaf module shared by `par/a64-delhooks` and `par/a64-s4-vcpu`: +- `[P] VcpuPlatAgreeA ar`: hardware `vcpu_midr`, `vcpu_vgic_vtr`, and `vcpu_gic_version` agree + with `Fl4SpecA64.fl4A64Platform` and `Fl4SpecA64.config_ARM_GIC_V3`. +- `[P] VcpuArchKeepsA ar AR`: pure-arch VCPU/FPU/IRQ operations preserve `AR · h`. +- `[P] VcpuVmRootA st ar AR`: `vspace.set_vm_root` leaves the store unchanged and preserves `AR`. +- `fl4VcpuUnbind` / `fl4VcpuDetach`: spec helpers matching `vcpu::unbind` and `vcpu::detach` + (`crates/fl4-core/src/arch/aarch64/vcpu.rs:324-330`) on top of `Fl4SpecA64/Ext/ARMVCPURun`. +-/ +import Fl4SpecA64.Ext.ARMVCPURun +import Fl4ProofsA64.P3.JointA + +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3.Vcpu + +open Fl4Spec Fl4SpecA64 Fl4ProofsA64.P3 + +noncomputable section + +/-- `[P]` Hardware VCPU platform constants agree with `Fl4SpecA64.fl4A64Platform` and `config_ARM_GIC_V3`. -/ +structure VcpuPlatAgreeA {A : Type} (ar : machine.Arch A) : Prop where + midr : ∀ a, ar.vcpu_midr a = ok ⟨Fl4SpecA64.fl4A64Platform.vcpuMidr⟩ + vtr : ∀ a, ar.vcpu_vgic_vtr a = ok ⟨Fl4SpecA64.fl4A64Platform.vgicVtr⟩ + gic : ∀ a, ∃ g : U64, ar.vcpu_gic_version a = ok g ∧ + decide (g = 3#u64) = Fl4SpecA64.config_ARM_GIC_V3 + +/-- `[P]` Pure-arch VCPU/FPU/IRQ operations preserve the hardware root relation `AR · h`. -/ +structure VcpuArchKeepsA {A : Type} (ar : machine.Arch A) (AR : A → HwRootA → Prop) : Prop where + vcpu_flush : ∀ a h vc, AR a h → ar.vcpu_flush a vc ⦃ a' => AR a' h ⦄ + vcpu_flush_state : ∀ a h vc m, AR a h → ar.vcpu_flush_state a vc m ⦃ a' => AR a' h ⦄ + fpu_flush : ∀ a h t, AR a h → ar.fpu_flush a t ⦃ a' => AR a' h ⦄ + fpu_release : ∀ a h t, AR a h → ar.fpu_release a t ⦃ a' => AR a' h ⦄ + mask_irq : ∀ a h irq m, AR a h → ar.mask_irq a irq m ⦃ a' => AR a' h ⦄ + +/-- `[P]` `vspace.set_vm_root` leaves the store unchanged and preserves `AR`. -/ +structure VcpuVmRootA {S : Type} (st : machine.Store S) {A : Type} (ar : machine.Arch A) + (AR : A → HwRootA → Prop) : Prop where + set_vm_root : ∀ s a h (t : U64), AR a h → + vspace.set_vm_root st ar s a t ⦃ r => r.1 = s ∧ ∃ h', AR r.2 h' ⦄ + +/-- Spec helper for `vcpu.unbind`: `fl4VcpuEndRun` followed by `dissociateVcpuTcb` on any bound TCB. -/ +def fl4VcpuUnbind (vc : ObjRef) : SMonad Unit Unit := do + fl4VcpuEndRun vc + let v ← getVcpu vc + match v.vcpuTcb with + | some t => dissociateVcpuTcb vc t + | none => pure () + +/-- Spec helper for `vcpu.detach`: `fl4CancelRun` followed by `fl4VcpuUnbind` on any bound VCPU. -/ +def fl4VcpuDetach (t : ObjRef) : SMonad Unit Unit := do + fl4CancelRun t + let vOpt ← archThreadGet ArchTcb.tcbVcpu t + match vOpt with + | some vc => fl4VcpuUnbind vc + | none => pure () + +end + +end Fl4ProofsA64.P3.Vcpu diff --git a/proofs/Fl4ProofsA64/P3/VcpuRegJA.lean b/proofs/Fl4ProofsA64/P3/VcpuRegJA.lean new file mode 100644 index 000000000..f52118be9 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/VcpuRegJA.lean @@ -0,0 +1,2055 @@ +/- +P3 (aarch64, A64-P3 S4 W2a): **Simple VCPU register & VPPI invocations on `VcpuTopPostA`**: +- Sanitizer / helper equivalences: `vcpu_is_readonly_reg_eqA`, `vcpu_is_vgic_ap1r_eqA`, + `vcpu_sanitize_hcr_eqA`, `vcpu_sanitize_vgic_hcr_eqA`, `vcpu_sanitize_vgic_lr_eqA`, + `vcpu_lr_is_empty_eqA`. +- Non-link VCPU write framing: `JAtZAX.of_vcpuNonLinkWrA`, `TreeA.of_vcpuNonLinkWrA`, + `CarryA.of_vcpuNonLinkWrA`. +- Label 64 `ARMVCPUAckVPPI`: `vcpu_ack_vppi_jA`. +- Label 62 `ARMVCPUReadReg`: `vcpu_read_reg_jA` (reusing `FrReplyA.reply1_jA`). +- Label 63 `ARMVCPUWriteReg`: `vcpu_write_reg_jA` (using `sync_vgic_status_refinesA`). +- Label 61 `ARMVCPUInjectIRQ`: `vcpu_inject_irq_jA` (including post-`stall_vcpu` `DeleteFirst` on + `VcpuTopPostA` with `AR a1 Z.hw`). + +Named premises: `[P] VcpuPlatAgreeA ar`, `[P] VcpuArchKeepsA ar AR`, `[S]` local-core +`stall_vcpu` affinity. +-/ +import Fl4ProofsA64.P3.VcpuFrameMemA +import Fl4ProofsA64.P3.RestartA + +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3.Vcpu + +open Fl4Spec Fl4SpecA64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3 Fl4ProofsA64.P3.Sched + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## Pure helper & sanitizer equivalences -/ + +private theorem lift_ok {α : Type} (x : α) : (lift x : Result α) = ok x := rfl + +private theorem spec_bind_assoc {α β γ : Type} {x : Result α} {f : α → Result β} {g : β → Result γ} + {P : β → Prop} {Q : γ → Prop} + (h1 : (do let a ← x; f a) ⦃ P ⦄) + (h2 : ∀ b, P b → g b ⦃ Q ⦄) : + (do let a ← x; let b ← f a; g b) ⦃ Q ⦄ := by + rw [← Std.bind_assoc]; exact WP.spec_bind h1 h2 + +private theorem spec_bind_of_ok {α β : Type} {x : Result α} {g : α → Result β} + {P : α → Prop} {Q : β → Prop} + (h1 : (do let a ← x; ok a) ⦃ P ⦄) + (h2 : ∀ a, P a → g a ⦃ Q ⦄) : + (do let a ← x; g a) ⦃ Q ⦄ := by + have h := spec_bind_assoc h1 h2 + simpa only [bind_ok] using h + +theorem vcpu_is_readonly_reg_eqA (r : U64) : + vcpu.is_readonly_reg r = ok (fl4VcpuRegReadOnly r.val) := by + unfold vcpu.is_readonly_reg fl4VcpuRegReadOnly + vrFarEl2 vrHpfarEl2 vrEsrEl2 vrVgicVtr vrVgicMisr vrVgicEisr vrVgicElrsr + split + · rfl + · rfl + · rfl + · rfl + · rfl + · rfl + · rfl + · rename_i _ h31 h32 h33 h34 h37 h38 h39 + have hne : ∀ (k : U64) (n : Nat), k.val = n → r ≠ k → r.val ≠ n := + fun k _ hn h e => h (UScalar.eq_of_val_eq (e.trans hn.symm)) + simp [hne 31#u64 31 rfl h31, hne 32#u64 32 rfl h32, hne 33#u64 33 rfl h33, + hne 34#u64 34 rfl h34, hne 37#u64 37 rfl h37, hne 38#u64 38 rfl h38, + hne 39#u64 39 rfl h39] + +theorem vcpu_is_vgic_ap1r_eqA (r : U64) : + vcpu.is_vgic_ap1r r = ok (fl4IsVgicAp1r r.val) := by + unfold vcpu.is_vgic_ap1r fl4IsVgicAp1r vrVgicAp1r0 vrVgicAp1r3 + have h44 : (fl4_abi.consts.vcpu_reg.VGIC_AP1R0).val = 44 := by + unfold fl4_abi.consts.vcpu_reg.VGIC_AP1R0; rfl + have h47 : (fl4_abi.consts.vcpu_reg.VGIC_AP1R3).val = 47 := by + unfold fl4_abi.consts.vcpu_reg.VGIC_AP1R3; rfl + by_cases hge : r ≥ fl4_abi.consts.vcpu_reg.VGIC_AP1R0 + · rw [if_pos hge] + have hge' : 44 ≤ r.val := by + have : fl4_abi.consts.vcpu_reg.VGIC_AP1R0.val ≤ r.val := hge + omega + by_cases hle : r ≤ fl4_abi.consts.vcpu_reg.VGIC_AP1R3 + · have hle' : r.val ≤ 47 := by + have : r.val ≤ fl4_abi.consts.vcpu_reg.VGIC_AP1R3.val := hle + omega + simp [hle, hge', hle'] + · have hle' : ¬ r.val ≤ 47 := by + intro h; apply hle; change r.val ≤ fl4_abi.consts.vcpu_reg.VGIC_AP1R3.val; omega + simp [hle, hge', hle'] + · rw [if_neg hge] + have hge' : ¬ 44 ≤ r.val := by + intro h; apply hge; change fl4_abi.consts.vcpu_reg.VGIC_AP1R0.val ≤ r.val; omega + simp [hge'] + +theorem vcpu_sanitize_hcr_eqA (v : U64) : + vcpu.sanitize_hcr v = ok ⟨sanitizeHcr v.bv⟩ := by + unfold vcpu.sanitize_hcr sanitizeHcr + rw [eq_of_spec_fd Fl4ProofsA64.P0.Consts.vcpu.HCR_VMM_ALLOWED.spec, bind_ok] + simp only [lift, bind_ok] + rw [eq_of_spec_fd Fl4ProofsA64.P0.Consts.vcpu.HCR_FORCED_1.spec, bind_ok] + rfl + +theorem vcpu_sanitize_vgic_hcr_eqA (v : U64) (gicV3 : Bool) : + vcpu.sanitize_vgic_hcr v gicV3 = ok ⟨sanitizeVgicHcr v.bv gicV3⟩ := by + unfold vcpu.sanitize_vgic_hcr sanitizeVgicHcr + have hshl : ∀ (x : U64) (k : I32) (n : Nat) (hk : 0 ≤ k.val ∧ k.val < 64) (hn : k.val.toNat = n), + x <<< k = ok ⟨x.bv <<< n⟩ := + fun _ _ _ hk hn => by rw [u64_shl_i32_ok hk.1 hk.2, hn] + cases gicV3 + · simp only [Bool.false_eq_true, ↓reduceIte] + rw [hshl 31#u64 27#i32 27 (by decide) rfl, bind_ok] + simp only [lift, bind_ok]; rfl + · simp only [↓reduceIte] + rw [hshl 31#u64 27#i32 27 (by decide) rfl, bind_ok, + hshl 1#u64 14#i32 14 (by decide) rfl, bind_ok, lift_ok, bind_ok, + hshl 1#u64 12#i32 12 (by decide) rfl, bind_ok, lift_ok, bind_ok, + hshl 1#u64 11#i32 11 (by decide) rfl, bind_ok, lift_ok, bind_ok, + hshl 1#u64 10#i32 10 (by decide) rfl, bind_ok, lift_ok, bind_ok, lift_ok, bind_ok] + rfl + +theorem vcpu_sanitize_vgic_lr_eqA (v : U64) (gicV3 : Bool) : + vcpu.sanitize_vgic_lr v gicV3 = ok ((sanitizeVgicLr v.bv gicV3).map fun w => (⟨w⟩ : U64)) := by + unfold vcpu.sanitize_vgic_lr sanitizeVgicLr + have hshr : ∀ (x : U64) (k : I32) (n : Nat) (hk : 0 ≤ k.val ∧ k.val < 64) (hn : k.val.toNat = n), + x >>> k = ok ⟨x.bv >>> n⟩ := + fun _ _ _ hk hn => by rw [u64_shr_i32_ok hk.1 hk.2, hn] + cases gicV3 + · simp only [Bool.false_eq_true, ↓reduceIte] + rw [hshr v 28#i32 28 (by decide) rfl, bind_ok] + simp only [lift, bind_ok] + have hmask : vcpu.sanitize_vgic_lr.MASK_V2 = + ok ⟨((1 : Word) <<< 30) ||| ((3 : Word) <<< 28) ||| ((0x1f : Word) <<< 23) ||| + ((1 : Word) <<< 19) ||| ((7 : Word) <<< 10) ||| (0x3ff : Word)⟩ := by + rw [eq_of_spec_fd Fl4ProofsA64.P0.Consts.vcpu.sanitize_vgic_lr.MASK_V2.spec]; rfl + by_cases hst : (v.bv >>> 28) &&& 3 ≠ 0 + · have hst' : ((⟨v.bv >>> 28⟩ &&& 3#u64 : U64) != 0#u64) = true := + bne_iff_ne.2 (fun e => hst (by have := congrArg UScalar.bv e; simpa using this)) + rw [if_pos hst'] + by_cases hvid : (v.bv &&& 0x3ff) > 1019 + · have hvid' : (v &&& 1023#u64 : U64) > 1019#u64 := hvid + rw [if_pos hvid', if_pos ⟨hst, hvid⟩]; rfl + · have hvid' : ¬ ((v &&& 1023#u64 : U64) > 1019#u64) := hvid + rw [if_neg hvid', if_neg (fun h => hvid h.2), hmask, bind_ok]; rfl + · have hst' : ((⟨v.bv >>> 28⟩ &&& 3#u64 : U64) != 0#u64) = false := by + rw [bne_eq_false_iff_eq]; apply u64_eq_of_bv_eq; simpa using not_not.1 hst + rw [if_neg (by simp [hst']), if_neg (fun h => hst h.1), hmask, bind_ok]; rfl + · simp only [↓reduceIte] + rw [hshr v 62#i32 62 (by decide) rfl, bind_ok] + simp only [lift, bind_ok] + have hmask : vcpu.sanitize_vgic_lr.MASK_V3 = + ok ⟨((3 : Word) <<< 62) ||| ((1 : Word) <<< 60) ||| ((0xff : Word) <<< 48) ||| + ((1 : Word) <<< 41) ||| (0xffffffff : Word)⟩ := by + rw [eq_of_spec_fd Fl4ProofsA64.P0.Consts.vcpu.sanitize_vgic_lr.MASK_V3.spec]; rfl + by_cases hst : (v.bv >>> 62) &&& 3 ≠ 0 + · have hst' : ((⟨v.bv >>> 62⟩ &&& 3#u64 : U64) != 0#u64) = true := + bne_iff_ne.2 (fun e => hst (by have := congrArg UScalar.bv e; simpa using this)) + rw [if_pos hst'] + by_cases hvid1 : (v.bv &&& 0xffffffff) > 1019 + · have hvid1' : (v &&& 4294967295#u64 : U64) > 1019#u64 := hvid1 + rw [if_pos hvid1'] + by_cases hvid2 : (v.bv &&& 0xffffffff) < 8192 + · have hvid2' : (v &&& 4294967295#u64 : U64) < 8192#u64 := hvid2 + rw [if_pos hvid2', if_pos ⟨hst, hvid1, hvid2⟩]; rfl + · have hvid2' : ¬ ((v &&& 4294967295#u64 : U64) < 8192#u64) := hvid2 + rw [if_neg hvid2', if_neg (fun h => hvid2 h.2.2), hmask, bind_ok]; rfl + · have hvid1' : ¬ ((v &&& 4294967295#u64 : U64) > 1019#u64) := hvid1 + rw [if_neg hvid1', if_neg (fun h => hvid1 h.2.1), hmask, bind_ok]; rfl + · have hst' : ((⟨v.bv >>> 62⟩ &&& 3#u64 : U64) != 0#u64) = false := by + rw [bne_eq_false_iff_eq]; apply u64_eq_of_bv_eq; simpa using not_not.1 hst + rw [if_neg (by simp [hst']), if_neg (fun h => hst h.1), hmask, bind_ok]; rfl + +theorem vcpu_lr_is_empty_eqA (lr : U64) (gicV3 : Bool) : + vcpu.lr_is_empty lr gicV3 = ok (lrIsEmpty lr.bv gicV3) := by + unfold vcpu.lr_is_empty lrIsEmpty lrFields + have hshr : ∀ (x : U64) (k : I32) (n : Nat) (hk : 0 ≤ k.val ∧ k.val < 64) (hn : k.val.toNat = n), + x >>> k = ok ⟨x.bv >>> n⟩ := + fun _ _ _ hk hn => by rw [u64_shr_i32_ok hk.1 hk.2, hn] + cases gicV3 + · simp only [Bool.false_eq_true, ↓reduceIte] + rw [hshr lr 28#i32 28 (by decide) rfl, bind_ok, lift_ok, bind_ok, + hshr lr 31#i32 31 (by decide) rfl, bind_ok, lift_ok, bind_ok, + hshr lr 19#i32 19 (by decide) rfl, bind_ok, lift_ok, bind_ok, bind_ok] + change (if ((⟨lr.bv >>> 28⟩ : U64) &&& 3#u64) = 0#u64 then + if (((⟨lr.bv >>> 31⟩ : U64) &&& 1#u64) != 0#u64) = true then ok true + else ok (decide (((⟨lr.bv >>> 19⟩ : U64) &&& 1#u64) = 0#u64)) + else ok false) = _ + by_cases hst : (lr.bv >>> 28) &&& 3 = 0 + · have hst' : (⟨lr.bv >>> 28⟩ &&& 3#u64 : U64) = 0#u64 := by apply u64_eq_of_bv_eq; exact hst + rw [if_pos hst'] + by_cases hhw : (lr.bv >>> 31) &&& 1 ≠ 0 + · have hhw' : ((⟨lr.bv >>> 31⟩ &&& 1#u64 : U64) != 0#u64) = true := + bne_iff_ne.2 (fun e => hhw (by have := congrArg UScalar.bv e; simpa using this)) + rw [if_pos hhw']; exact congrArg ok (decide_eq_true ⟨hst, Or.inl hhw⟩).symm + · have hhw' : ((⟨lr.bv >>> 31⟩ &&& 1#u64 : U64) != 0#u64) = false := by + rw [bne_eq_false_iff_eq]; apply u64_eq_of_bv_eq; simpa using not_not.1 hhw + rw [if_neg (by simp [hhw'])] + by_cases heoi : (lr.bv >>> 19) &&& 1 = 0 + · have heoi' : (⟨lr.bv >>> 19⟩ &&& 1#u64 : U64) = 0#u64 := by apply u64_eq_of_bv_eq; exact heoi + exact congrArg ok (by rw [decide_eq_true heoi', decide_eq_true ⟨hst, Or.inr heoi⟩]) + · have heoi' : (⟨lr.bv >>> 19⟩ &&& 1#u64 : U64) ≠ 0#u64 := fun e => heoi (congrArg UScalar.bv e) + exact congrArg ok (by rw [decide_eq_false heoi', decide_eq_false (fun h => h.2.elim hhw heoi)]) + · have hst' : (⟨lr.bv >>> 28⟩ &&& 3#u64 : U64) ≠ 0#u64 := fun e => hst (congrArg UScalar.bv e) + rw [if_neg hst']; exact congrArg ok (decide_eq_false (fun h => hst h.1)).symm + · simp only [↓reduceIte] + rw [hshr lr 62#i32 62 (by decide) rfl, bind_ok, lift_ok, bind_ok, + hshr lr 61#i32 61 (by decide) rfl, bind_ok, lift_ok, bind_ok, + hshr lr 41#i32 41 (by decide) rfl, bind_ok, lift_ok, bind_ok, bind_ok] + change (if ((⟨lr.bv >>> 62⟩ : U64) &&& 3#u64) = 0#u64 then + if (((⟨lr.bv >>> 61⟩ : U64) &&& 1#u64) != 0#u64) = true then ok true + else ok (decide (((⟨lr.bv >>> 41⟩ : U64) &&& 1#u64) = 0#u64)) + else ok false) = _ + by_cases hst : (lr.bv >>> 62) &&& 3 = 0 + · have hst' : (⟨lr.bv >>> 62⟩ &&& 3#u64 : U64) = 0#u64 := by apply u64_eq_of_bv_eq; exact hst + rw [if_pos hst'] + by_cases hhw : (lr.bv >>> 61) &&& 1 ≠ 0 + · have hhw' : ((⟨lr.bv >>> 61⟩ &&& 1#u64 : U64) != 0#u64) = true := + bne_iff_ne.2 (fun e => hhw (by have := congrArg UScalar.bv e; simpa using this)) + rw [if_pos hhw']; exact congrArg ok (decide_eq_true ⟨hst, Or.inl hhw⟩).symm + · have hhw' : ((⟨lr.bv >>> 61⟩ &&& 1#u64 : U64) != 0#u64) = false := by + rw [bne_eq_false_iff_eq]; apply u64_eq_of_bv_eq; simpa using not_not.1 hhw + rw [if_neg (by simp [hhw'])] + by_cases heoi : (lr.bv >>> 41) &&& 1 = 0 + · have heoi' : (⟨lr.bv >>> 41⟩ &&& 1#u64 : U64) = 0#u64 := by apply u64_eq_of_bv_eq; exact heoi + exact congrArg ok (by rw [decide_eq_true heoi', decide_eq_true ⟨hst, Or.inr heoi⟩]) + · have heoi' : (⟨lr.bv >>> 41⟩ &&& 1#u64 : U64) ≠ 0#u64 := fun e => heoi (congrArg UScalar.bv e) + exact congrArg ok (by rw [decide_eq_false heoi', decide_eq_false (fun h => h.2.elim hhw heoi)]) + · have hst' : (⟨lr.bv >>> 62⟩ &&& 3#u64 : U64) ≠ 0#u64 := fun e => hst (congrArg UScalar.bv e) + rw [if_neg hst']; exact congrArg ok (decide_eq_false (fun h => hst h.1)).symm + +/-! ## Non-link VCPU write framing (`JAtZAX`, `TreeA`, `CarryA`) -/ + +section vcpu_frame +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-- Any write strictly inside `VcPage Z.V` and off `{v, wa v 24#u64}` misses `FootA Z s`. -/ +theorem not_footA_of_vcNonLinkA {Z : XSetsA} {OS Ub : Set U64} {s : S} + (hP : VcPlacedA Z OS Ub (L.ksb s)) (hksFit : (L.ksb s).val + ksSizeA ≤ 2 ^ 64) + (hCs : ∀ c ∈ Z.Cs, c.val < 128) (hcpu : (L.cpu s).val < 128) + (hExt : ∀ b, VcPage Z.V b → ¬ ExtFootE Z.X Z.Y Z.T2 b) + (hSw : ∀ b, VcPage Z.V b → ∀ y, SlotWA Z.th y → + b ≠ y ∧ b ≠ wa y 8#u64 ∧ b ≠ wa y 16#u64 ∧ b ≠ wa y 24#u64) + {b : U64} (hb : VcPage Z.V b) (hnl : ∀ v ∈ Z.V, b ≠ v ∧ b ≠ wa v 24#u64) : + ¬ FootA (L := L) Z s b := by + intro hf + cases hf with + | slot y _ hy h4 => + obtain ⟨h0, h8, h16, h24⟩ := hSw b hb y hy + rcases h4 with rfl | rfl | rfl | rfl <;> contradiction + | nslot _ hfn => + cases hfn with + | node c _ hc h4 => + rcases h4 with rfl | rfl | rfl | rfl + · exact hP.disj_node hksFit hb (hCs c hc) (by unfold layout.node.CUR_THREAD; decide) rfl + · exact hP.disj_node hksFit hb (hCs c hc) (by unfold layout.node.IDLE_THREAD; decide) rfl + · exact hP.disj_node hksFit hb (hCs c hc) (by unfold layout.node.CUR_SC; decide) rfl + · exact hP.disj_node hksFit hb (hCs c hc) (by unfold layout.node.IDLE_SC; decide) rfl + | cur _ h2 => + rcases h2 with rfl | rfl + · exact hP.disj_node hksFit hb hcpu (by unfold layout.node.SCHED_ACTION; decide) rfl + · exact hP.disj_node hksFit hb hcpu (by unfold layout.node.CUR_TIME; decide) rfl + | tcb t _ ht h5 => + obtain ⟨v, hv, o, ho, _, hbe⟩ := hb + rcases h5 with h5 | h5 | h5 | h5 | h5 + · rw [stA_eq_wa] at h5; exact hP.disj_tcb v hv t ht o 3328#u64 ho (by decide) (hbe.symm.trans h5) + · exact hP.disj_tcb v hv t ht o _ ho (by decide) (hbe.symm.trans h5) + · exact hP.disj_tcb v hv t ht o _ ho (by decide) (hbe.symm.trans h5) + · exact hP.disj_tcb v hv t ht o _ ho (by decide) (hbe.symm.trans h5) + · rw [runVcpuA] at h5; exact hP.disj_tcb v hv t ht o _ ho (by decide) (hbe.symm.trans h5) + | sc c hc => + obtain ⟨v, hv, o, ho, _, hbe⟩ := hb + exact hP.disj_sc v hv c hc o 24#u64 ho (by decide) hbe.symm + | vcpu v _ hv h2 => + obtain ⟨h0, h24⟩ := hnl v hv + rcases h2 with rfl | rfl <;> contradiction + | irq i hi => + apply hP.disj_ks _ hb + have hval : (irqWordA (L.ksb s) i).val = (L.ksb s).val + 224 + 8 * i.val := by + have hk := (L.ksb s).hBounds + simp only [ksSizeA] at hksFit + unfold irqWordA irqOffA + simp only [core.num.U64.wrapping_add_val_eq, core.num.U64.wrapping_mul_val_eq, + U64.size_def, U64.numBits, UScalar.size, UScalarTy.numBits] at hk ⊢ + have h224 : (224#u64 : U64).val = 224 := rfl + have h8 : (8#u64 : U64).val = 8 := rfl + rw [h224, h8] + omega + rw [hval]; simp only [ksSizeA]; omega + | cur2 _ h2 => + rcases h2 with rfl | rfl + · exact hP.disj_node hksFit hb hcpu (by unfold layout.node.CONSUMED; decide) rfl + · exact hP.disj_node hksFit hb hcpu (by unfold layout.node.REPROGRAM; decide) rfl + | glob _ h2 => + apply hP.disj_ks _ hb + rcases h2 with rfl | rfl + · have hval : (wa (L.ksb s) 8#u64).val = (L.ksb s).val + 8 := by + rw [wa_valP]; simp only [ksSizeA] at hksFit + have h8 : (8#u64 : U64).val = 8 := rfl; rw [h8] + exact Nat.mod_eq_of_lt (by omega) + rw [hval]; simp only [ksSizeA]; omega + · have hval : (wa (L.ksb s) 16#u64).val = (L.ksb s).val + 16 := by + rw [wa_valP]; simp only [ksSizeA] at hksFit + have h16 : (16#u64 : U64).val = 16 := rfl; rw [h16] + exact Nat.mod_eq_of_lt (by omega) + rw [hval]; simp only [ksSizeA]; omega + | tcb2 t _ ht h2 => + obtain ⟨v, hv, o, ho, _, hbe⟩ := hb + rcases h2 with h2 | h2 + · exact hP.disj_tcb v hv t ht o _ ho (by decide) (hbe.symm.trans h2) + · exact hP.disj_tcb v hv t ht o _ ho (by decide) (hbe.symm.trans h2) + | sc2 c hc => + obtain ⟨v, hv, o, ho, _, hbe⟩ := hb + exact hP.disj_sc v hv c hc o 72#u64 ho (by decide) hbe.symm + | vs _ hvs => exact hP.disj_vs _ hb hvs + | ext _ hext => exact hExt _ hb hext + +/-- Updating a VCPU's non-link fields (`vcpuTcb` and `vcpuExt.runCaller` unchanged) in `σ` +preserves `JAtZAX` and `TyKeepA`. -/ +theorem JAtZAX.of_specVcpuNonLinkA {AR : A → HwRootA → Prop} {Z : XSetsA} {s : S} {a : A} + {σ : State Unit} {m : Fl4SpecA64.McState} + (J : JAtZAX (L := L) AR Z s a σ m) {vc : U64} (hvc : vc ∈ Z.V) + {v v' : Fl4SpecA64.Vcpu} (hk : σ.kheap (specPtr vc) = some (.archObj (.vcpu v))) + (htcb : v'.vcpuTcb = v.vcpuTcb) (hrc : v'.vcpuExt.runCaller = v.vcpuExt.runCaller) : + JAtZAX (L := L) AR Z s a (updKSc σ (specPtr vc) (.archObj (.vcpu v'))) m ∧ + TyKeepA σ (updKSc σ (specPtr vc) (.archObj (.vcpu v'))) := by + have hne_nonV : ∀ p, σ.kheap p ≠ some (.archObj (.vcpu v)) → p ≠ specPtr vc := by + intro p hne heq; subst heq; exact hne hk + have hkh_other : ∀ p, p ≠ specPtr vc → (updKSc σ (specPtr vc) (.archObj (.vcpu v'))).kheap p = σ.kheap p := + fun p hne => updKSc_other σ hne + have hT : TcbKeepA Z.T σ (updKSc σ (specPtr vc) (.archObj (.vcpu v'))) := by + intro t ht x hkt + exact ⟨x, by rw [hkh_other (specPtr t) (hne_nonV _ (by rw [hkt]; simp))]; exact hkt, rfl⟩ + have hC : ∀ c ∈ Z.C, (updKSc σ (specPtr vc) (.archObj (.vcpu v'))).kheap (scRenA m c) = + σ.kheap (scRenA m c) := by + intro c hc + obtain ⟨y, n, hkc, -⟩ := J.1.sch.sc c hc + exact hkh_other _ (hne_nonV _ (by rw [hkc]; simp)) + have hG : SpecGlobA σ (updKSc σ (specPtr vc) (.archObj (.vcpu v'))) := + ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, ⟨rfl, rfl, rfl, rfl⟩⟩ + have hst' : StateRelA Z.th (L.mem s) (updKSc σ (specPtr vc) (.archObj (.vcpu v'))) := + { cnode := fun p r hp => by + obtain ⟨cs, hkcs, hrest⟩ := J.1.st.cnode p r hp + rw [hkh_other (specPtr p) (hne_nonV _ (by rw [hkcs]; simp))] + exact ⟨cs, hkcs, hrest⟩ + tcb := fun t ht => by + obtain ⟨x, hkt, hrest⟩ := J.1.st.tcb t ht + rw [hkh_other (specPtr t) (hne_nonV _ (by rw [hkt]; simp))] + exact ⟨x, hkt, hrest⟩ } + have hcdt' : CdtRelA st Z.th s (updKSc σ (specPtr vc) (.archObj (.vcpu v'))) Z.G := + ⟨J.1.cdt.tree, J.1.cdt.cdt, J.1.cdt.cdt_slot, J.1.cdt.orig, J.1.cdt.par_slot, J.1.cdt.prev⟩ + have hvs' : VsRelA Z.Pp Z.S1 Z.Lv Z.X Z.Y (L.mem s) (L.ksb s) + (updKSc σ (specPtr vc) (.archObj (.vcpu v'))) := + J.1.vs.of_spec rfl fun p hp => by + apply hkh_other p + intro heq; subst heq + rcases hp with ⟨q, hq, hqp⟩ | ⟨q, hq, hqp⟩ | ⟨q, hq, hqp⟩ + · obtain ⟨ap, hkap, -⟩ := J.1.vs.pool q hq + rw [hqp, hk] at hkap; cases hkap + · obtain ⟨pt, hkpt, -⟩ := J.1.vs.pt q hq + rw [hqp, hk] at hkpt; cases hkpt + · obtain ⟨l1, hkl1⟩ := J.2.2.2.obj q hq + rw [hqp, hk] at hkl1; cases hkl1 + have hext' : ExtVsRelA Z.Pp Z.S1 Z.X Z.Y Z.T2 Z.Sh2 (L.mem s) + (updKSc σ (specPtr vc) (.archObj (.vcpu v'))) := + J.1.ext.of_spec rfl fun q hq => by + apply hkh_other (specPtr q) + intro heq + rcases hq with hq | hq | hq + · obtain ⟨ap, hkap, -⟩ := J.1.vs.pool q hq + rw [heq, hk] at hkap; cases hkap + · obtain ⟨es, hkes⟩ := J.2.2.1.obj q hq + rw [heq, hk] at hkes; cases hkes + · obtain ⟨pt, hkpt, -⟩ := J.1.ext.s2pt q hq + rw [heq, hk] at hkpt; cases hkpt + have J1' : JAtZA (L := L) AR Z s a (updKSc σ (specPtr vc) (.archObj (.vcpu v'))) m := + { J.1 with + st := hst' + cdt := hcdt' + sch := J.1.sch.of_spec hT hC hG + irq := J.1.irq.of_spec hG + rest := J.1.rest.of_spec hT hC hG + vs := hvs' + ext := hext' } + refine ⟨⟨J1', ?_, ?_, ?_⟩, ?_⟩ + · refine ⟨?_, ?_, ?_, J.2.1.cap⟩ + · intro w hw + by_cases hweq : w = vc + · subst hweq + obtain ⟨x0, hk0, h0, h24⟩ := J.2.1.obj w hw + rw [hk] at hk0; cases hk0 + exact ⟨v', updKSc_same _ _ _, by rw [htcb]; exact h0, by rw [hrc]; exact h24⟩ + · obtain ⟨xw, hkw, h0, h24⟩ := J.2.1.obj w hw + exact ⟨xw, by rw [hkh_other _ (fun h => hweq (specPtr_inj h))]; exact hkw, h0, h24⟩ + · intro p x hkp + by_cases hp : p = specPtr vc + · subst hp; exact ⟨vc, hvc, rfl⟩ + · rw [hkh_other p hp] at hkp; exact J.2.1.back p x hkp + · intro t ht + obtain ⟨xt, hkt, h1, h2⟩ := J.2.1.tcb t ht + exact ⟨xt, by rw [hkh_other _ (hne_nonV _ (by rw [hkt]; simp))]; exact hkt, h1, h2⟩ + · refine ⟨?_, ?_, J.2.2.1.cap⟩ + · intro r hr + obtain ⟨es, hkr⟩ := J.2.2.1.obj r hr + exact ⟨es, by rw [hkh_other _ (hne_nonV _ (by rw [hkr]; simp))]; exact hkr⟩ + · intro p es hkp + by_cases hp : p = specPtr vc + · subst hp; rw [updKSc_same] at hkp; cases hkp + · rw [hkh_other p hp] at hkp; exact J.2.2.1.back p es hkp + · refine ⟨?_, ?_, J.2.2.2.cap⟩ + · intro r hr + obtain ⟨l1, hkr⟩ := J.2.2.2.obj r hr + exact ⟨l1, by rw [hkh_other _ (hne_nonV _ (by rw [hkr]; simp))]; exact hkr⟩ + · intro p l1 hkp + by_cases hp : p = specPtr vc + · subst hp; rw [updKSc_same] at hkp; cases hkp + · rw [hkh_other p hp] at hkp; exact J.2.2.2.back p l1 hkp + · refine ⟨fun o r cs ho => ?_, fun o t ho => ?_⟩ + · by_cases hp : o = specPtr vc + · subst hp; rw [updKSc_same] at ho; cases ho + · rw [hkh_other o hp] at ho; exact ⟨cs, ho⟩ + · by_cases hp : o = specPtr vc + · subst hp; rw [updKSc_same] at ho; cases ho + · rw [hkh_other o hp] at ho; exact ⟨t, ho⟩ + +/-- Combined framing of `JAtZAX`, `TreeA`, `CarryA` across a non-link VCPU write + spec update. -/ +theorem vcpuNonLinkWr_frameA {AR : A → HwRootA → Prop} {Z : XSetsA} {OS Ub : Set U64} + {s s' : S} {a : A} {σ : State Unit} {m : Fl4SpecA64.McState} + (J : JAtZAX (L := L) AR Z s a σ m) + (hTr : TreeA st OS Z.G.chains Z.G.par s) + (hP : VcPlacedA Z OS Ub (L.ksb s)) + (hCs : ∀ c ∈ Z.Cs, c.val < 128) (hcpu : (L.cpu s).val < 128) + (hExt : ∀ b, VcPage Z.V b → ¬ ExtFootE Z.X Z.Y Z.T2 b) + (hSw : ∀ b, VcPage Z.V b → ∀ y, SlotWA Z.th y → + b ≠ y ∧ b ≠ wa y 8#u64 ∧ b ≠ wa y 16#u64 ∧ b ≠ wa y 24#u64) + (hfa : ∀ b, FootA (L := L) Z s b → Aligned b) + {W : U64 → Prop} (hw : WritesIn st s s' W) + (hWvc : ∀ b, W b → VcPage Z.V b ∧ ∀ v ∈ Z.V, b ≠ v ∧ b ≠ wa v 24#u64) + {vc : U64} (hvc : vc ∈ Z.V) {v v' : Fl4SpecA64.Vcpu} + (hk : σ.kheap (specPtr vc) = some (.archObj (.vcpu v))) + (htcb : v'.vcpuTcb = v.vcpuTcb) (hrc : v'.vcpuExt.runCaller = v.vcpuExt.runCaller) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) : + let σ' := updKSc σ (specPtr vc) (.archObj (.vcpu v')) + JAtZAX (L := L) AR Z s' a σ' m ∧ + TreeA st OS Z.G.chains Z.G.par s' ∧ + (CarryA Z (L.mem s) σ → CarryA Z (L.mem s') σ') := by + have hJ1 : JAtZAX (L := L) AR Z s' a σ m := + J.of_writesIn hw (fun b hb => + let ⟨hbp, hnl⟩ := hWvc b hb + not_footA_of_vcNonLinkA hP J.1.ksFit hCs hcpu hExt hSw hbp hnl) hfa + obtain ⟨hJ2, hTy⟩ := JAtZAX.of_specVcpuNonLinkA hJ1 hvc hk htcb hrc + have hosKeep : OSKeep st OS s s' := by + intro y hy + have hal : Aligned y := hP.al_os y hy + have hdisj : ∀ b, W b → b ≠ y ∧ b ≠ wa y 8#u64 ∧ b ≠ wa y 16#u64 ∧ b ≠ wa y 24#u64 := + fun b hb => hP.disj_os b (hWvc b hb).1 y hy + have halK : ∀ K : U64, K.val % 8 = 0 → Aligned (wa y K) := fun K hK => by + unfold Aligned at hal ⊢ + rw [wa_valP] + have : (y.val + K.val) % 2 ^ 64 % 8 = (y.val + K.val) % 8 := Nat.mod_mod_of_dvd _ (by decide) + omega + refine ⟨hw.mem_eq hal (fun h => (hdisj y h).1 rfl), + hw.mem_eq (halK 8#u64 (by decide)) (fun h => (hdisj _ h).2.1 rfl), + hw.mem_eq (halK 16#u64 (by decide)) (fun h => (hdisj _ h).2.2.1 rfl), + hw.mem_eq (halK 24#u64 (by decide)) (fun h => (hdisj _ h).2.2.2 rfl)⟩ + have hTr' : TreeA st OS Z.G.chains Z.G.par s' := hTr.keepO hosKeep + refine ⟨hJ2, hTr', fun C => ?_⟩ + have hcap_os : ∀ y ∈ OS, capAtA (L.mem s') y = capAtA (L.mem s) y := fun y hy => by + obtain ⟨h0, h8, -, -⟩ := hosKeep y hy + unfold capAtA; rw [h0, h8] + refine ⟨C.1.of_capsA rfl (fun _ h => h) (fun _ h => h) (fun y hy => Or.inl (hcap_os y (hsub y hy))), + C.2.of_keepA hTy.1 hTy.2 fun y hy => ?_⟩ + rw [hcap_os y ((hTr.lk.members y).1 hy).1] + exact CapFinEqA.refl _ + +end vcpu_frame + +/-! ## Label 64: `ARMVCPUAckVPPI` (`vcpu_ack_vppi_jA`) -/ + +section labels +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + +/-- Shared decode-site context for VCPU invocations (no new premise: all fields are standard S3 +decode-site facts plus `VcpuRelA` and `[S]` local-core `stall_vcpu` affinity). -/ +structure VcpuDecCtxA (AR : A → HwRootA → Prop) (Z : XSetsA) (H : Heap) + (OS U Ac Pp Tb Ub : Set U64) (Lv : U64 → Nat) (ia ie : Nat) + (s : S) (a : A) (σ : State Unit) (m : Fl4SpecA64.McState) (vc : U64) : Prop where + E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m + hV : VcpuRelA (L := L) Z OS Ub s σ + hTr : TreeA st OS Z.G.chains Z.G.par s + hvc : vc ∈ Z.V + hExt : ∀ b, VcPage Z.V b → ¬ ExtFootE Z.X Z.Y Z.T2 b + hSw : ∀ b, VcPage Z.V b → ∀ y, SlotWA Z.th y → + b ≠ y ∧ b ≠ wa y 8#u64 ∧ b ≠ wa y 16#u64 ∧ b ≠ wa y 24#u64 + hfa : ∀ b, FootA (L := L) Z s b → Aligned b + hsub : ∀ y, IsSlotA Z.th y → y ∈ OS + hIdleT : ∀ c ∈ Z.Cs, L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) ∈ Z.T + hCurNoRun : L.mem s (runVcpuA (L.curW s)) = 0#u64 + hStallT : L.mem s vc ≠ 0#u64 → smp.remote_tcb_stall st ar s a (L.mem s vc) = ok (s, a) + hStallC : L.mem s (wa vc 24#u64) ≠ 0#u64 → + smp.remote_tcb_stall st ar s a (L.mem s (wa vc 24#u64)) = ok (s, a) + +/-- Helper: `NonDetE.liftME Fl4ArchInvocation.vcpu` on a single error outcome. -/ +theorem liftME_vcpu_errA {d : SEMonad Unit Fl4VcpuInvocation} {σ : State Unit} {e : SyscallError} + (h : d.run σ = ({(.error e, σ)}, False)) : + (NonDetE.liftME Fl4ArchInvocation.vcpu d).run σ = ({(.error e, σ)}, False) := by + unfold NonDetE.liftME + change (d.run >>= _) σ = _ + rw [nd_bind_oneSc h]; rfl + +/-- Helper: `NonDetE.liftME Fl4ArchInvocation.vcpu` on a single ok outcome. -/ +theorem liftME_vcpu_okA {d : SEMonad Unit Fl4VcpuInvocation} {σ : State Unit} {inv : Fl4VcpuInvocation} + (h : d.run σ = ({(.ok inv, σ)}, False)) : + (.ok (Fl4ArchInvocation.vcpu inv), σ) ∈ ((NonDetE.liftME Fl4ArchInvocation.vcpu d).run σ).1 ∧ + ¬ ((NonDetE.liftME Fl4ArchInvocation.vcpu d).run σ).2 := by + unfold NonDetE.liftME + change (_, _) ∈ ((d.run >>= _) σ).1 ∧ ¬ ((d.run >>= _) σ).2 + rw [nd_bind_oneSc h] + exact ⟨Set.mem_singleton _, id⟩ + +private theorem ndE_throw_bind {α β : Type} (e : SyscallError) (f : α → SEMonad Unit β) (σ : State Unit) : + (NonDetE.throwError e >>= f).run σ = ({(.error e, σ)}, False) := by + unfold NonDetE.throwError + change ((NonDet.pure (Except.error e)) >>= (ExceptT.bindCont f)) σ = _ + rw [nd_bind_oneSc (rfl : NonDet.pure (Except.error e) σ = ({(Except.error e, σ)}, False))] + rfl + +private theorem ndE_pure_bind {α β : Type} (x : α) (f : α → SEMonad Unit β) (σ : State Unit) : + ((pure x : SEMonad Unit α) >>= f).run σ = (f x).run σ := by + change ((NonDet.pure (Except.ok x)) >>= (ExceptT.bindCont f)) σ = _ + rw [nd_bind_oneSc (rfl : NonDet.pure (Except.ok x) σ = ({(Except.ok x, σ)}, False))] + rfl + +private theorem ndE_whenE_true {β : Type} {P : Prop} [Decidable P] (h : P) + (e : SyscallError) (f : Unit → SEMonad Unit β) (σ : State Unit) : + (NonDetE.whenE P (NonDetE.throwError e) >>= f).run σ = ({(.error e, σ)}, False) := by + unfold NonDetE.whenE; rw [if_pos h]; exact ndE_throw_bind e f σ + +private theorem ndE_whenE_false {β : Type} {P : Prop} [Decidable P] (h : ¬ P) + (e : SyscallError) (f : Unit → SEMonad Unit β) (σ : State Unit) : + (NonDetE.whenE P (NonDetE.throwError e) >>= f).run σ = (f ()).run σ := by + unfold NonDetE.whenE; rw [if_neg h]; exact ndE_pure_bind () f σ + +private theorem ndE_unlessE_true {β : Type} {P : Prop} [Decidable P] (h : P) + (e : SyscallError) (f : Unit → SEMonad Unit β) (σ : State Unit) : + (NonDetE.unlessE P (NonDetE.throwError e) >>= f).run σ = (f ()).run σ := by + unfold NonDetE.unlessE; rw [if_pos h]; exact ndE_pure_bind () f σ + +private theorem ndE_unlessE_false {β : Type} {P : Prop} [Decidable P] (h : ¬ P) + (e : SyscallError) (f : Unit → SEMonad Unit β) (σ : State Unit) : + (NonDetE.unlessE P (NonDetE.throwError e) >>= f).run σ = ({(.error e, σ)}, False) := by + unfold NonDetE.unlessE; rw [if_neg h]; exact ndE_throw_bind e f σ + +private theorem ndE_liftE_bind {α β : Type} (m : SMonad Unit α) (f : α → SEMonad Unit β) + (σ σ' : State Unit) (a : α) (hm : m σ = ({(a, σ')}, False)) : + (NonDetE.liftE m >>= f).run σ = (f a).run σ' := by + have h1 : (NonDetE.liftE (ε := SyscallError) m).run σ = ({(Except.ok a, σ')}, False) := by + change (m >>= fun x => NonDet.pure (Except.ok x)) σ = _ + rw [nd_bind_oneSc hm]; rfl + change (ExceptT.run (NonDetE.liftE (ε := SyscallError) m) >>= ExceptT.bindCont f) σ = _ + rw [nd_bind_oneSc h1]; rfl + +private theorem u64_sub_ok {x y : U64} (hle : y.val ≤ x.val) : + x - y = ok ⟨BitVec.ofNat 64 (x.val - y.val)⟩ := by + apply eq_of_spec_fd + refine WP.spec_mono (U64.sub_spec (x := x) (y := y) hle) ?_ + intro r hr + apply UScalar.eq_of_val_eq + rw [hr.1] + change x.val - y.val = (BitVec.ofNat 64 (x.val - y.val)).toNat + rw [BitVec.toNat_ofNat] + have hx := x.hBounds + simp only [U64.size_def, U64.numBits, UScalar.size, UScalarTy.numBits] at hx + exact (Nat.mod_eq_of_lt (by omega)).symm + +private theorem writesIn_wrA {S : Type} {st : machine.Store S} [L : LawfulStore st] + (s : S) {a v : U64} (ha : Aligned a) : + WritesIn st s (L.wr s a v) (fun b => b = a) := + ⟨L.ksb_wr _ _ _, L.cpu_wr _ _ _, fun b hb hne => mem_wr_neA ha hb (Ne.symm hne)⟩ + +private theorem footA_of_wrA {S : Type} {st : machine.Store S} [L : LawfulStore st] + {Z : XSetsA} {s : S} (hfa : ∀ b, FootA (L := L) Z s b → Aligned b) (a v : U64) : + ∀ b, FootA (L := L) Z (L.wr s a v) b → Aligned b := by + intro b hb; apply hfa b + cases hb with + | slot y _ hy h4 => exact FootA.slot y _ hy h4 + | nslot _ hfn => + refine FootA.nslot _ ?_ + cases hfn with + | node c _ hc h4 => exact FootNA.node c _ hc (by simpa [LawfulStore.nodeAt, L.ksb_wr] using h4) + | cur _ h2 => exact FootNA.cur _ (by simpa [LawfulStore.nodeAt, L.ksb_wr, L.cpu_wr] using h2) + | tcb t _ ht h5 => exact FootNA.tcb t _ ht h5 + | sc c hc => exact FootNA.sc c hc + | vcpu v _ hv h2 => exact FootNA.vcpu v _ hv h2 + | irq i hi => exact FootNA.irq i hi |> fun h => by simpa [L.ksb_wr] using h + | cur2 _ h2 => exact FootNA.cur2 _ (by simpa [LawfulStore.nodeAt, L.ksb_wr, L.cpu_wr] using h2) + | glob _ h2 => exact FootNA.glob _ (by simpa [L.ksb_wr] using h2) + | tcb2 t _ ht h2 => exact FootNA.tcb2 t _ ht h2 + | sc2 c hc => exact FootNA.sc2 c hc + | vs _ hvs => exact FootNA.vs _ (by simpa [L.ksb_wr] using hvs) + | ext _ hext => exact FootNA.ext _ hext + +private theorem vc_ne_stA {Z : XSetsA} {OS Ub : Set U64} {ksb : U64} + (hPl : VcPlacedA Z OS Ub ksb) {t : U64} (ht : t ∈ Z.T) : + ∀ v ∈ Z.V, stA t ≠ v ∧ stA t ≠ wa v 24#u64 := by + intro v hv + have hwa0 : wa v 0#u64 = v := by + apply UScalar.eq_of_val_eq; rw [wa_valP]; simp [UScalar.hBounds] + have h0 := (hPl.disj_tcb v hv t ht 0#u64 3328#u64 (by decide) (by decide)).symm + rw [hwa0, ← stA_eq_wa] at h0 + have h24 := (hPl.disj_tcb v hv t ht 24#u64 3328#u64 (by decide) (by decide)).symm + rw [← stA_eq_wa] at h24 + exact ⟨h0, h24⟩ + +private theorem vcpuRegWA_nonLinkA {Z : XSetsA} {OS Ub : Set U64} {ksb : U64} + (hPl : VcPlacedA Z OS Ub ksb) {vc : U64} (hvc : vc ∈ Z.V) {i : Nat} (hi : i < 64) : + ∀ b, b = vcpuRegWA vc i → VcPage Z.V b ∧ ∀ v' ∈ Z.V, b ≠ v' ∧ b ≠ wa v' 24#u64 := by + rintro _ rfl + refine ⟨vcpuRegWA_in_vcPage hvc hi, fun v' hv' => ?_⟩ + have hwa0 : wa v' 0#u64 = v' := by + apply UScalar.eq_of_val_eq; rw [wa_valP]; simp [UScalar.hBounds] + have hlt : (vcpuRegOffA i).val < 4096 := by rw [vcpuRegOffA_val hi]; omega + by_cases heq : v' = vc + · subst heq + have hne0 : vcpuRegOffA i ≠ 0#u64 := fun h => by + have := congrArg UScalar.val h; rw [vcpuRegOffA_val hi] at this; change _ = 0 at this; omega + have hne24 : vcpuRegOffA i ≠ 24#u64 := fun h => by + have := congrArg UScalar.val h; rw [vcpuRegOffA_val hi] at this; change _ = 24 at this; omega + have h1 := hPl.same_vcpu_ne hv' hlt (by decide) hne0 + rw [hwa0] at h1 + exact ⟨h1, hPl.same_vcpu_ne hv' hlt (by decide) hne24⟩ + · have h1 := hPl.disj_vcpu (ov₁ := vcpuRegOffA i) (ov₂ := 0#u64) hvc hv' (Ne.symm heq) hlt (by decide) + rw [hwa0] at h1 + exact ⟨h1, hPl.disj_vcpu (ov₁ := vcpuRegOffA i) (ov₂ := 24#u64) hvc hv' (Ne.symm heq) hlt (by decide)⟩ + +private theorem carryA_of_restartA {S : Type} {st : machine.Store S} [L : LawfulStore st] + {Z : XSetsA} {OS : Set U64} {s : S} {σ : State Unit} {xct : Tcb} + (hTr : TreeA st OS Z.G.chains Z.G.par s) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hkct : σ.kheap σ.curThread = some (.tcb xct)) + (hos1 : ∀ y ∈ OS, L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART) y = L.mem s y ∧ + L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART) (wa y 8#u64) = L.mem s (wa y 8#u64) ∧ + L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART) (wa y 16#u64) = L.mem s (wa y 16#u64) ∧ + L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART) (wa y 24#u64) = L.mem s (wa y 24#u64)) : + CarryA Z (L.mem s) σ → + CarryA Z (L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART)) + (stUpdA σ σ.curThread xct .restart) := by + intro hC + have hcap_os : ∀ y ∈ OS, capAtA (L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART)) y = + capAtA (L.mem s) y := fun y hy => by + obtain ⟨h0, h8, -, -⟩ := hos1 y hy; unfold capAtA; rw [h0, h8] + have hTy1 : TyKeepA σ (stUpdA σ σ.curThread xct .restart) := by + refine ⟨fun o r cs ho => ?_, fun o t ho => ?_⟩ + · by_cases hp : o = σ.curThread + · subst hp; unfold stUpdA setObjA at ho; simp [Function.update] at ho + · unfold stUpdA setObjA at ho; simp [Function.update, hp] at ho; exact ⟨cs, ho⟩ + · by_cases hp : o = σ.curThread + · subst hp; exact ⟨xct, hkct⟩ + · unfold stUpdA setObjA at ho; simp [Function.update, hp] at ho; exact ⟨t, ho⟩ + exact ⟨hC.1.of_capsA rfl (fun _ h => h) (fun _ h => h) (fun y hy => Or.inl (hcap_os y (hsub y hy))), + hC.2.of_keepA hTy1.1 hTy1.2 fun y hy => by + rw [hcap_os y ((hTr.lk.members y).1 hy).1]; exact CapFinEqA.refl _⟩ + +/-- **Label 64 (`ARMVCPUAckVPPI`)** on `VcpuTopPostA`. -/ +theorem vcpu_ack_vppi_jA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} + {OS U Ac Pp Tb Ub : Set U64} {Lv : U64 → Nat} {ia ie : Nat} + {s : S} {a : A} {σ : State Unit} {m : Fl4SpecA64.McState} {vc : U64} + (hAK : VcpuArchKeepsA ar AR) + (C : VcpuDecCtxA (st := st) (ar := ar) AR Z H OS U Ac Pp Tb Ub Lv ia ie s a σ m vc) + {Rp Nt : Set U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} {slot : U64} + (P : SysPlace st H Z.Cs H.frame Rp Nt OS s extra slot) (Y : SymL st H Z.Cs s) + (hrun : Runnable (L.mem s (stA (L.curW s)))) + (hcur : (L.mem s (inqA (L.curW s)) &&& sched.Q_RELEASE) = 0#u64 ∧ L.mem s (scA (L.curW s)) ≠ 0#u64 ∧ + L.mem s (scA (L.curW s)) ∈ Ac) + (hvs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → Aligned b ∧ b ≠ stA (L.curW s)) + (length buf : U64) (calling : Bool) (args : List U64) (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) : + vcpu.decode_ack_vppi st ar s a vc length buf ⦃ res => + VcpuTopPostA (L := L) AR Z OS m calling σ + ((NonDetE.liftME Fl4ArchInvocation.vcpu <| + decodeFl4VcpuAckVppi (Z := Unit) (specPtr vc) (args.map UScalar.bv)).run σ) s res ⦄ := by + have hAR : AR a Z.hw := C.E.j.1.ar + unfold vcpu.decode_ack_vppi + by_cases hlen1 : length < 1#u64 + · rw [if_pos hlen1] + unfold error.truncated + rw [bind_ok, WP.spec_ok] + have hlt : (args.map UScalar.bv).length < 1 := by + rw [List.length_map, ← hlen]; exact hlen1 + have hd : (decodeFl4VcpuAckVppi (Z := Unit) (specPtr vc) (args.map UScalar.bv)).run σ = + ({(.error .truncatedMessage, σ)}, False) := by + unfold decodeFl4VcpuAckVppi NonDetE.whenE + rw [if_pos hlt]; exact ndE_throw_bind _ _ _ + exact ⟨rfl, hAR, .truncatedMessage, liftME_vcpu_errA hd, Or.inl trivial⟩ + · rw [if_neg hlen1] + have hge1 : 1 ≤ args.length := by + have : ¬ length.val < 1 := hlen1 + omega + have hct : sched.cur_thread st s = ok (L.curW s) := eq_of_spec_fd (sched.cur_thread.spec s) + rw [hct, bind_ok] + cases hargs_eq : args with + | nil => simp [hargs_eq] at hge1 + | cons a0 rest => + have ha0 : msg.syscall_arg st s (L.curW s) buf 0#u64 = ok a0 := + hargs 0#u64 a0 (by simp [hargs_eq]) + rw [ha0, bind_ok] + have htid : layout.irq.TIMER_ID = 27#u64 := by + unfold layout.irq.TIMER_ID; rfl + rw [htid] + by_cases h27 : a0 = 27#u64 + · subst h27 + simp only [bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + have hcurT : L.curW s ∈ Z.T := C.E.j.1.curT (L.cpu s) C.E.j.1.sch.cpuCs + have hvc_st := vc_ne_stA C.hV.place hcurT + refine WP.spec_bind (stall_vcpu_localA hAK hAR vc C.hStallT C.hStallC) ?_ + rintro ⟨s1, a1⟩ ⟨hs1, hAR1⟩ + dsimp only at hs1; subst s1 + change (do + let s2 ← sched.set_thread_state st s (L.curW s) sched.TS_RESTART + let i ← lift (wa vc vcpu.VPPI_MASKED) + let masked ← st.read s2 i + let i1 ← lift (wa vc vcpu.VPPI_MASKED) + let i2 ← lift (~~~1#u64) + let i3 ← lift (masked &&& i2) + let s3 ← st.write s2 i1 i3 + ok (core.result.Result.Ok (), s3, a1)) ⦃ _ ⦄ + obtain ⟨xct, hkct, hst_ok, hst_spec, E1, -, hos1, -, -⟩ := + C.E.restartA P Y hrun hcur hvs hvc_st + rw [hst_ok, bind_ok] + unfold vcpu.VPPI_MASKED + simp only [lift, bind_ok, L.read_eq, L.write_eq, WP.spec_ok] + -- Build spec decode + perform + have hd : (decodeFl4VcpuAckVppi (Z := Unit) (specPtr vc) ((27#u64 :: rest).map UScalar.bv)).run σ = + ({(.ok (.ackVppi (specPtr vc)), σ)}, False) := by + unfold decodeFl4VcpuAckVppi NonDetE.whenE + simp [the] + obtain ⟨v, hkv, htv, hrv⟩ := C.hV.core.obj vc C.hvc + have hne_vc_ct : specPtr vc ≠ σ.curThread := by + intro h; rw [h, hkct] at hkv; cases hkv + have hkv1 : (stUpdA σ σ.curThread xct .restart).kheap (specPtr vc) = some (.archObj (.vcpu v)) := by + unfold stUpdA setObjA; simp [Function.update, hne_vc_ct, hkv] + have hperf : fl4PerformVcpuInv (Z := Unit) (.ackVppi (specPtr vc)) + (stUpdA σ σ.curThread xct .restart) = + ({([], updKSc (stUpdA σ σ.curThread xct .restart) (specPtr vc) (.archObj (.vcpu + { v with vcpuVppiMasked := Function.update v.vcpuVppiMasked .vppiEventIRQ_VTimer false })))}, + False) := + fl4PerformVcpuInv_ackVppi_eqA C.hV.arch hkv1 + -- Frame JAtZAX, TreeA, CarryA across `stA` write and `wa vc 16` write + have hE1_a1 : JAtZAX (L := L) AR Z (L.wr s (stA (L.curW s)) sched.TS_RESTART) a1 + (stUpdA σ σ.curThread xct .restart) m := + ⟨{ E1.j.1 with ar := hAR1 }, E1.j.2⟩ + have hTr1 : TreeA st OS Z.G.chains Z.G.par (L.wr s (stA (L.curW s)) sched.TS_RESTART) := + C.hTr.keepO hos1 + have hCs128 : ∀ c ∈ Z.Cs, c.val < 128 := by + obtain ⟨n, -, hn128, hcn⟩ := C.E.j.1.csN + intro c hc; have := (hcn c).1 hc; omega + have hcpu128 : (L.cpu (L.wr s (stA (L.curW s)) sched.TS_RESTART)).val < 128 := by + rw [L.cpu_wr]; exact hCs128 _ C.E.j.1.sch.cpuCs + have hfa1 := footA_of_wrA C.hfa (stA (L.curW s)) sched.TS_RESTART + have hWvc : ∀ b, b = wa vc 16#u64 → VcPage Z.V b ∧ ∀ v' ∈ Z.V, b ≠ v' ∧ b ≠ wa v' 24#u64 := by + rintro _ rfl + refine ⟨vcpuHdr_in_vcPage C.hvc (by decide) (by decide), fun v' hv' => ?_⟩ + have hwa0 : wa v' 0#u64 = v' := by + apply UScalar.eq_of_val_eq; rw [wa_valP]; simp [UScalar.hBounds] + by_cases heq : v' = vc + · subst heq + have h1 := C.hV.place.same_vcpu_ne (o₁ := 16#u64) (o₂ := 0#u64) hv' (by decide) (by decide) (by decide) + rw [hwa0] at h1 + exact ⟨h1, C.hV.place.same_vcpu_ne (o₁ := 16#u64) (o₂ := 24#u64) hv' (by decide) (by decide) (by decide)⟩ + · have h1 := C.hV.place.disj_vcpu (ov₁ := 16#u64) (ov₂ := 0#u64) C.hvc hv' (Ne.symm heq) (by decide) (by decide) + rw [hwa0] at h1 + exact ⟨h1, C.hV.place.disj_vcpu (ov₁ := 16#u64) (ov₂ := 24#u64) C.hvc hv' (Ne.symm heq) (by decide) (by decide)⟩ + have h16_al : Aligned (wa vc 16#u64) := aligned_wa (C.hV.place.vc_al C.hvc) 16#u64 (by decide) + obtain ⟨hJ2, hTr2, hCar2⟩ := vcpuNonLinkWr_frameA + (v' := { v with vcpuVppiMasked := Function.update v.vcpuVppiMasked .vppiEventIRQ_VTimer false }) + hE1_a1 hTr1 (by rw [L.ksb_wr]; exact C.hV.place) hCs128 hcpu128 C.hExt C.hSw hfa1 + (writesIn_wrA _ h16_al) hWvc C.hvc hkv1 rfl rfl C.hsub + have hCar1 := carryA_of_restartA C.hTr C.hsub hkct hos1 + refine ⟨.vcpu (.ackVppi (specPtr vc)), liftME_vcpu_okA hd, + stUpdA σ σ.curThread xct .restart, hst_spec, _, [], ?_, Or.inl ⟨Z, hJ2, hTr2, fun hC => hCar2 (hCar1 hC)⟩⟩ + unfold fl4ArchPerformInvocation NonDetE.liftE + change (Except.ok [], _) ∈ ((fl4PerformVcpuInv (.ackVppi (specPtr vc)) >>= _) _).1 + rw [nd_bind_oneSc hperf] + exact Set.mem_singleton _ + · rw [if_pos (bne_iff_ne.2 h27)] + unfold error.invalid_arg + rw [bind_ok, WP.spec_ok] + have h27bv : a0.bv ≠ 27 := fun e => h27 (u64_eq_of_bv_eq e) + have hd : (decodeFl4VcpuAckVppi (Z := Unit) (specPtr vc) ((a0 :: rest).map UScalar.bv)).run σ = + ({(.error (.invalidArgument 0), σ)}, False) := by + unfold decodeFl4VcpuAckVppi + rw [ndE_whenE_false (by simp)] + exact ndE_whenE_true (by simpa [the] using h27bv) _ _ _ + exact ⟨rfl, hAR, .invalidArgument 0, liftME_vcpu_errA hd, Or.inl rfl⟩ + +/-- `vcpu.reply1` is definitionally identical to `vspace.reply1` (`FrReplyA.reply1_jA`). -/ +theorem vcpu_reply1_eq_vspace (s : S) (t : obj.Tcb) (call : Bool) (v : U64) : + vcpu.reply1 st s t call v = vspace.reply1 st s t call v := rfl + +/-- **Label 62 (`ARMVCPUReadReg`)** on `VcpuTopPostA` (reply-aware branch via `FrReplyA.reply1_jA`). -/ +theorem vcpu_read_reg_jA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} + {OS U Ac Pp Tb Ub : Set U64} {Lv : U64 → Nat} {ia ie : Nat} + {s : S} {a : A} {σ : State Unit} {m : Fl4SpecA64.McState} {vc : U64} + (hP : VcpuPlatAgreeA ar) (hAK : VcpuArchKeepsA ar AR) + (C : VcpuDecCtxA (st := st) (ar := ar) AR Z H OS U Ac Pp Tb Ub Lv ia ie s a σ m vc) + (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) + (hvcR : ∀ v ∈ Z.V, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → + wa (L.curW s) K ≠ v ∧ wa (L.curW s) K ≠ wa v 24#u64) + {Rp Nt : Set U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} {slot : U64} + (Pl : SysPlace st H Z.Cs H.frame Rp Nt OS s extra slot) (Y : SymL st H Z.Cs s) + (hrun : Runnable (L.mem s (stA (L.curW s)))) + (hcur : (L.mem s (inqA (L.curW s)) &&& sched.Q_RELEASE) = 0#u64 ∧ L.mem s (scA (L.curW s)) ≠ 0#u64 ∧ + L.mem s (scA (L.curW s)) ∈ Ac) + (hvs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → Aligned b ∧ b ≠ stA (L.curW s)) + (length buf : U64) (call : Bool) (args : List U64) (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) : + vcpu.decode_read_reg st ar s a vc length call buf ⦃ res => + VcpuTopPostA (L := L) AR Z OS m call σ + ((NonDetE.liftME Fl4ArchInvocation.vcpu <| + decodeFl4VcpuReadReg (Z := Unit) (specPtr vc) (args.map UScalar.bv)).run σ) s res ⦄ := by + have hAR : AR a Z.hw := C.E.j.1.ar + unfold vcpu.decode_read_reg + by_cases hlen1 : length < 1#u64 + · rw [if_pos hlen1] + unfold error.truncated; rw [bind_ok, WP.spec_ok] + have hlt : (args.map UScalar.bv).length < 1 := by rw [List.length_map, ← hlen]; exact hlen1 + have hd : (decodeFl4VcpuReadReg (Z := Unit) (specPtr vc) (args.map UScalar.bv)).run σ = + ({(.error .truncatedMessage, σ)}, False) := by + unfold decodeFl4VcpuReadReg NonDetE.whenE; rw [if_pos hlt]; exact ndE_throw_bind _ _ _ + exact ⟨rfl, hAR, .truncatedMessage, liftME_vcpu_errA hd, Or.inl trivial⟩ + · rw [if_neg hlen1] + have hge1 : 1 ≤ args.length := by have : ¬ length.val < 1 := hlen1; omega + have hct : sched.cur_thread st s = ok (L.curW s) := eq_of_spec_fd (sched.cur_thread.spec s) + rw [hct, bind_ok] + cases hargs_eq : args with + | nil => simp [hargs_eq] at hge1 + | cons field rest => + rw [hargs 0#u64 field (by simp [hargs_eq]), bind_ok] + have hnum : (fl4_abi.consts.vcpu_reg.NUM).val = 64 := by + unfold fl4_abi.consts.vcpu_reg.NUM; rfl + by_cases hge64 : field ≥ fl4_abi.consts.vcpu_reg.NUM + · rw [if_pos hge64] + unfold error.invalid_arg; rw [bind_ok, WP.spec_ok] + have hf64 : field.val ≥ vrNum := by + have : fl4_abi.consts.vcpu_reg.NUM.val ≤ field.val := hge64 + unfold vrNum; omega + have hd : (decodeFl4VcpuReadReg (Z := Unit) (specPtr vc) ((field :: rest).map UScalar.bv)).run σ = + ({(.error (.invalidArgument 0), σ)}, False) := by + unfold decodeFl4VcpuReadReg + rw [ndE_whenE_false (by simp)] + exact ndE_whenE_true (by simpa [the] using hf64) _ _ _ + exact ⟨rfl, hAR, .invalidArgument 0, liftME_vcpu_errA hd, Or.inl rfl⟩ + · rw [if_neg hge64, vcpu_num_lrs_eqA hP a, bind_ok] + have hf_lt64 : field.val < 64 := by + have : ¬ fl4_abi.consts.vcpu_reg.NUM.val ≤ field.val := hge64; omega + have hlr0 : (fl4_abi.consts.vcpu_reg.VGIC_LR0).val = 48 := by + unfold fl4_abi.consts.vcpu_reg.VGIC_LR0; rfl + have hlrs_val : (⟨BitVec.ofNat 64 fl4NumLrs⟩ : U64).val = fl4NumLrs := by + change (BitVec.ofNat 64 fl4NumLrs).toNat = fl4NumLrs + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by have := fl4NumLrs_le_16; omega) + -- Shared body for the valid-field path (both `field ≥ 48 ∧ field - 48 < lrs` and `field < 48`) + have hvalid_body : ¬ (field.val ≥ vrVgicLr0 ∧ field.val - vrVgicLr0 ≥ fl4NumLrs) → + (do + let (s1, a1) ← vcpu.stall_vcpu st ar s a vc + let i1 ← ar.vcpu_gic_version a1 + let val ← + if field = fl4_abi.consts.vcpu_reg.VGIC_VTR then do + let i2 ← ar.vcpu_vgic_vtr a1 + vcpu.abi_vgic_vtr i2 (i1 = 3#u64) + else if i1 = 3#u64 then vcpu.reg_get st s1 vc field + else do + let b ← vcpu.is_vgic_ap1r field + if b then ok 0#u64 else vcpu.reg_get st s1 vc field + let s2 ← sched.set_thread_state st s1 (L.curW s) sched.TS_RESTART + let s3 ← vcpu.reply1 st s2 (L.curW s) call val + ok (core.result.Result.Ok (), s3, a1)) ⦃ res => + VcpuTopPostA (L := L) AR Z OS m call σ + ((NonDetE.liftME Fl4ArchInvocation.vcpu <| + decodeFl4VcpuReadReg (Z := Unit) (specPtr vc) ((field :: rest).map UScalar.bv)).run σ) s res ⦄ := by + intro hlr_ok + have hcurT : L.curW s ∈ Z.T := C.E.j.1.curT (L.cpu s) C.E.j.1.sch.cpuCs + have hvc_st := vc_ne_stA C.hV.place hcurT + refine WP.spec_bind (stall_vcpu_localA hAK hAR vc C.hStallT C.hStallC) ?_ + rintro ⟨s1, a1⟩ ⟨hs1, hAR1⟩ + dsimp only at hs1; subst s1 + change (do + let i1 ← ar.vcpu_gic_version a1 + let val ← + if field = fl4_abi.consts.vcpu_reg.VGIC_VTR then do + let i2 ← ar.vcpu_vgic_vtr a1 + vcpu.abi_vgic_vtr i2 (i1 = 3#u64) + else if i1 = 3#u64 then vcpu.reg_get st s vc field + else do + let b ← vcpu.is_vgic_ap1r field + if b then ok 0#u64 else vcpu.reg_get st s vc field + let s2 ← sched.set_thread_state st s (L.curW s) sched.TS_RESTART + let s3 ← vcpu.reply1 st s2 (L.curW s) call val + ok (core.result.Result.Ok (), s3, a1)) ⦃ _ ⦄ + obtain ⟨g, hga1, hgb1⟩ := hP.gic a1 + rw [hga1, bind_ok] + have hgb1' : decide (g = 3#u64) = fl4GicV3 := hgb1 + obtain ⟨v, hkv, -, -⟩ := C.hV.core.obj vc C.hvc + have hfield_eq : field = ⟨BitVec.ofNat 64 field.val⟩ := by + apply UScalar.eq_of_val_eq + change field.val = (BitVec.ofNat 64 field.val).toNat + rw [BitVec.toNat_ofNat]; exact (Nat.mod_eq_of_lt (by omega)).symm + have hget_f : vcpu.reg_get st s vc field = ok (L.mem s (vcpuRegWA vc field.val)) := by + conv_lhs => rw [hfield_eq] + exact vcpu_reg_get_eqA s vc hf_lt64 + have hval_eval : + (if field = fl4_abi.consts.vcpu_reg.VGIC_VTR then do + let i2 ← ar.vcpu_vgic_vtr a1 + vcpu.abi_vgic_vtr i2 (g = 3#u64) + else if g = 3#u64 then vcpu.reg_get st s vc field + else do + let b ← vcpu.is_vgic_ap1r field + if b then ok 0#u64 else vcpu.reg_get st s vc field) = + ok ⟨if field.val = vrVgicVtr then abiVgicVtr + else if ¬fl4GicV3 ∧ fl4IsVgicAp1r field.val then 0 + else fl4VcpuRegGet v field.val⟩ := by + by_cases hvtr : field.val = vrVgicVtr + · have hvtr' : field = fl4_abi.consts.vcpu_reg.VGIC_VTR := + UScalar.eq_of_val_eq (by unfold fl4_abi.consts.vcpu_reg.VGIC_VTR vrVgicVtr at *; simpa using hvtr) + rw [if_pos hvtr', if_pos hvtr, hP.vtr a1, bind_ok] + have hdec : (decide (g = 3#u64)) = fl4GicV3 := hgb1' + simpa [hdec] using vcpu_abi_vgic_vtr_eqA hP a1 + · have hvtr' : field ≠ fl4_abi.consts.vcpu_reg.VGIC_VTR := + fun e => hvtr (by have := congrArg UScalar.val e; unfold fl4_abi.consts.vcpu_reg.VGIC_VTR vrVgicVtr at *; simpa using this) + rw [if_neg hvtr', if_neg hvtr] + by_cases hg3 : g = 3#u64 + · have hfl : fl4GicV3 = true := by rw [← hgb1', decide_eq_true hg3] + rw [if_pos hg3] + simp only [hfl, not_true_eq_false, false_and, ↓reduceIte] + rw [hget_f] + congr 1; apply u64_eq_of_bv_eq + exact C.hV.regs vc C.hvc v hkv field.val hf_lt64 + · have hfl : fl4GicV3 = false := by rw [← hgb1', decide_eq_false hg3] + rw [if_neg hg3, vcpu_is_vgic_ap1r_eqA field, bind_ok] + simp only [hfl, Bool.false_eq_true, not_false_eq_true, true_and] + by_cases hap : fl4IsVgicAp1r field.val = true + · rw [if_pos hap, if_pos hap]; rfl + · rw [if_neg hap, if_neg hap, hget_f] + congr 1; apply u64_eq_of_bv_eq + exact C.hV.regs vc C.hvc v hkv field.val hf_lt64 + rw [hval_eval, bind_ok] + obtain ⟨xct, hkct, hst_ok, hst_spec, E1, -, hos1, -, -⟩ := + C.E.restartA Pl Y hrun hcur hvs hvc_st + rw [hst_ok, bind_ok, vcpu_reply1_eq_vspace] + have hE1_a1 : JAtZAX (L := L) AR Z (L.wr s (stA (L.curW s)) sched.TS_RESTART) a1 + (stUpdA σ σ.curThread xct .restart) m := + ⟨{ E1.j.1 with ar := hAR1 }, E1.j.2⟩ + have hTr1 : TreeA st OS Z.G.chains Z.G.par (L.wr s (stA (L.curW s)) sched.TS_RESTART) := + C.hTr.keepO hos1 + have hE_a1 : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a1 σ m := + { C.E with j := ⟨{ C.E.j.1 with ar := hAR1 }, C.E.j.2⟩ } + have hO0 := C.E.stOffA RS.vc + have hst_al : Aligned (stA (L.curW s)) := by + rw [stA_eq_wa]; exact aligned_wa (C.hV.place.al_t _ hcurT) 3328#u64 (by decide) + have hks1 : L.ksb (L.wr s (stA (L.curW s)) sched.TS_RESTART) = L.ksb s := L.ksb_wr _ _ _ + have hcpu1 : L.cpu (L.wr s (stA (L.curW s)) sched.TS_RESTART) = L.cpu s := L.cpu_wr _ _ _ + have hcur1 : L.curW (L.wr s (stA (L.curW s)) sched.TS_RESTART) = L.curW s := by + set s2 := L.wr s (stA (L.curW s)) sched.TS_RESTART + change L.mem s2 (L.nodeAt s2 (L.cpu s2) layout.node.CUR_THREAD) = L.curW s + rw [hcpu1, show L.nodeAt s2 (L.cpu s) layout.node.CUR_THREAD = + L.nodeAt s (L.cpu s) layout.node.CUR_THREAD by unfold LawfulStore.nodeAt; rw [hks1]] + exact mem_wr_neA hst_al (C.hV.place.node_al (L.cpu s) layout.node.CUR_THREAD (by unfold layout.node.CUR_THREAD; decide)) + (hO0.node (L.cpu s) C.E.j.1.sch.cpuCs layout.node.CUR_THREAD (by unfold layout.node.CUR_THREAD; decide)).symm + have hd : (decodeFl4VcpuReadReg (Z := Unit) (specPtr vc) ((field :: rest).map UScalar.bv)).run σ = + ({(.ok (.readReg (specPtr vc) field.val), σ)}, False) := by + unfold decodeFl4VcpuReadReg + rw [ndE_whenE_false (by simp), + ndE_whenE_false (by simp [the, vrNum]; omega), + ndE_whenE_false (by simpa [the] using hlr_ok)] + rfl + have hne_vc_ct : specPtr vc ≠ σ.curThread := by + intro h; rw [h, hkct] at hkv; cases hkv + have hkv1 : (stUpdA σ σ.curThread xct .restart).kheap (specPtr vc) = some (.archObj (.vcpu v)) := by + unfold stUpdA setObjA; simp [Function.update, hne_vc_ct, hkv] + have hperf := fl4PerformVcpuInv_readReg_eqA field.val + (show (stUpdA σ σ.curThread xct .restart).archState.armCurrentVcpu = none from C.hV.arch) hkv1 + have hsc_cur := C.E.j.1.sch.tcb (L.curW s) hcurT + obtain ⟨x0, hk0, -, hsc0⟩ := hsc_cur + rw [← C.E.j.1.sch.curThread, hkct] at hk0; cases hk0 + have hsc_some : xct.tcbSchedContext = some (scRenA m (L.mem s (scA (L.curW s)))) := by + rw [hsc0, optScA, if_neg hcur.2.1] + have hscC : L.mem s (scA (L.curW s)) ∈ Z.C := C.E.j.1.rest.scMem (L.curW s) hcurT hcur.2.1 + obtain ⟨ysc, nsc, hysc, -⟩ := C.E.j.1.sch.sc _ hscC + have hrm0 : 0 < ysc.scRefillMax := by + obtain ⟨y', n', hy', hrm'⟩ := C.E.j.1.rest.refillMax _ hscC + rw [hysc] at hy'; cases hy' + have hwH : L.mem s (scA (L.curW s)) ∈ H.scs := by rw [C.E.scs]; exact hscC + have hrm_nz : L.rmax s (L.mem s (scA (L.curW s))) ≠ 0#u64 := + (C.E.sys.ent.bxi.2.2.2.2.act _ hwH).1 hcur.2.2 + rw [hrm'] + have e2 : L.rmax s (L.mem s (scA (L.curW s))) = L.mem s (wa (L.mem s (scA (L.curW s))) 72#u64) := by + unfold LawfulStore.rmax; rw [maxA_eqA] + rw [e2] at hrm_nz + exact Nat.pos_of_ne_zero (fun e => hrm_nz (UScalar.eq_of_val_eq (by simpa using e))) + have hne_st_sc : stA (L.curW s) ≠ scA (L.curW s) := by + rw [scA]; exact (hO0.fld (L.curW s) hcurT 3464#u64 (by decide) (fun _ => by decide)).symm + have hne_st_inq : stA (L.curW s) ≠ inqA (L.curW s) := by + rw [inqA]; exact (hO0.fld (L.curW s) hcurT 3592#u64 (by decide) (fun _ => by decide)).symm + have hrm1 : L.rmax (L.wr s (stA (L.curW s)) sched.TS_RESTART) (L.mem s (scA (L.curW s))) = + L.rmax s (L.mem s (scA (L.curW s))) := by + have hwH : L.mem s (scA (L.curW s)) ∈ H.scs := by rw [C.E.scs]; exact hscC + have hmΩ : maxA (L.mem s (scA (L.curW s))) ∈ H.omega := + C.E.sys.ent.bxi.2.2.2.2.sub _ (C.E.sys.ent.bxi.2.2.2.2.all _ hwH).2.1 + have hma : Aligned (maxA (L.mem s (scA (L.curW s)))) := (C.E.sq.omega _ hmΩ).1 + have hmx : stA (L.curW s) ≠ maxA (L.mem s (scA (L.curW s))) := + fun e => hO0.om _ (Or.inr (Or.inr (Or.inr ⟨L.mem s (scA (L.curW s)), hscC, Or.inr (maxA_eqA _)⟩))) e.symm + exact mem_wr_neA hst_al hma hmx + have hrq1 : σ.curThread ∉ (stUpdA σ σ.curThread xct .restart).releaseQueue := by + have hq : specPtr (L.curW s) ∉ σ.releaseQueue := by + rw [C.E.j.1.rest.rel]; intro hm + obtain ⟨r, hr, er⟩ := List.mem_map.1 hm + have e' : r = L.curW s := specPtr_inj er; subst e' + have hct : L.curW s ∈ H.tcbs := C.E.sq.curT + have haf := C.E.sq.inR _ C.E.sq.cur _ hr + exact ((C.E.sq.released _ hct).2 (by rw [haf]; exact hr)) hcur.1 + simpa [stUpdA, setObjA, C.E.j.1.sch.curThread] using hq + have hk1_ct : (stUpdA σ σ.curThread xct .restart).kheap σ.curThread = + some (.tcb { xct with tcbState := .restart }) := by + unfold stUpdA setObjA; simp [Function.update] + have hhy1 : ∃ y n, (stUpdA σ σ.curThread xct .restart).kheap + (scRenA m (L.mem s (scA (L.curW s)))) = some (.schedContext y n) ∧ 0 < y.scRefillMax := by + have hne_sc_ct : scRenA m (L.mem s (scA (L.curW s))) ≠ σ.curThread := by + intro h; rw [h, hkct] at hysc; cases hysc + refine ⟨ysc, nsc, ?_, hrm0⟩ + unfold stUpdA setObjA; simp [Function.update, hne_sc_ct, hysc] + have hsc1 : L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART) (scA (L.curW s)) = + L.mem s (scA (L.curW s)) := + mem_wr_neA hst_al (aligned_wa (C.hV.place.al_t _ hcurT) 3464#u64 (by decide)) hne_st_sc + have hiq1 : L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART) (inqA (L.curW s)) = + L.mem s (inqA (L.curW s)) := + mem_wr_neA hst_al (aligned_wa (C.hV.place.al_t _ hcurT) 3592#u64 (by decide)) hne_st_inq + refine WP.spec_bind (reply1_jA hE_a1 RS hvcR hE1_a1 hTr1 hks1 hcpu1 hcur1 + hsc1 hiq1 hrm1 hk1_ct rfl hrq1 hsc_some hhy1 call _) ?_ + rintro s3 ⟨σ3, hrep3, hJ3, hTr3, hCar3⟩ + rw [WP.spec_ok] + have hCar1 := carryA_of_restartA C.hTr C.hsub hkct hos1 + refine ⟨.vcpu (.readReg (specPtr vc) field.val), liftME_vcpu_okA hd, + stUpdA σ σ.curThread xct .restart, hst_spec, stUpdA σ σ.curThread xct .restart, _, ?_, + Or.inr ⟨σ3, hrep3, hJ3, hTr3, fun hC => hCar3 (hCar1 hC)⟩⟩ + unfold fl4ArchPerformInvocation NonDetE.liftE + change (Except.ok _, _) ∈ ((fl4PerformVcpuInv (.readReg (specPtr vc) field.val) >>= _) _).1 + rw [nd_bind_oneSc hperf] + exact Set.mem_singleton _ + by_cases hge48 : field ≥ fl4_abi.consts.vcpu_reg.VGIC_LR0 + · rw [if_pos hge48] + have hge48' : 48 ≤ field.val := by + have : fl4_abi.consts.vcpu_reg.VGIC_LR0.val ≤ field.val := hge48; omega + have hsub_eq := u64_sub_ok (x := field) (y := fl4_abi.consts.vcpu_reg.VGIC_LR0) + (by rw [hlr0]; omega) + have hsub_val : (⟨BitVec.ofNat 64 (field.val - fl4_abi.consts.vcpu_reg.VGIC_LR0.val)⟩ : U64).val = + field.val - fl4_abi.consts.vcpu_reg.VGIC_LR0.val := by + change (BitVec.ofNat 64 _).toNat = _ + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + rw [hsub_eq, bind_ok] + set sub : U64 := ⟨BitVec.ofNat 64 (field.val - fl4_abi.consts.vcpu_reg.VGIC_LR0.val)⟩ + by_cases hge_lrs : sub ≥ ⟨BitVec.ofNat 64 fl4NumLrs⟩ + · rw [if_pos hge_lrs] + unfold error.invalid_arg; rw [bind_ok, WP.spec_ok] + have hcond : field.val ≥ vrVgicLr0 ∧ field.val - vrVgicLr0 ≥ fl4NumLrs := by + have h1 : fl4NumLrs ≤ sub.val := by + have : (⟨BitVec.ofNat 64 fl4NumLrs⟩ : U64).val ≤ sub.val := hge_lrs + omega + unfold vrVgicLr0; omega + have hd : (decodeFl4VcpuReadReg (Z := Unit) (specPtr vc) ((field :: rest).map UScalar.bv)).run σ = + ({(.error (.invalidArgument 0), σ)}, False) := by + unfold decodeFl4VcpuReadReg + rw [ndE_whenE_false (by simp), + ndE_whenE_false (by simp [the, vrNum]; omega)] + exact ndE_whenE_true (by simpa [the] using hcond) _ _ _ + exact ⟨rfl, hAR, .invalidArgument 0, liftME_vcpu_errA hd, Or.inl rfl⟩ + · rw [if_neg hge_lrs] + apply hvalid_body + intro hbad + apply hge_lrs + change (⟨BitVec.ofNat 64 fl4NumLrs⟩ : U64).val ≤ sub.val + rw [hlrs_val, hsub_val, hlr0] + unfold vrVgicLr0 at hbad; omega + · rw [if_neg hge48] + apply hvalid_body + intro hbad + apply hge48 + change fl4_abi.consts.vcpu_reg.VGIC_LR0.val ≤ field.val + rw [hlr0]; unfold vrVgicLr0 at hbad; omega + +/-! ### Helper for `VcpuRelA` across `restartA` and `reg_set` + optional `sync_vgic_status` -/ + +private theorem vcpuRelA_of_restartA {S : Type} {st : machine.Store S} [L : LawfulStore st] + {Z : XSetsA} {OS Ub : Set U64} {s : S} {σ : State Unit} {xct : Tcb} + (hV : VcpuRelA (L := L) Z OS Ub s σ) + (hcurT : L.curW s ∈ Z.T) + (hCurNoRun : L.mem s (runVcpuA (L.curW s)) = 0#u64) + (hkct : σ.kheap σ.curThread = some (.tcb xct)) + (hcore1 : VcpuCoreA (L := L) Z (L.wr s (stA (L.curW s)) sched.TS_RESTART) + (stUpdA σ σ.curThread xct .restart)) + (hos1 : ∀ y ∈ OS, L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART) y = L.mem s y ∧ + L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART) (wa y 8#u64) = L.mem s (wa y 8#u64) ∧ + L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART) (wa y 16#u64) = L.mem s (wa y 16#u64) ∧ + L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART) (wa y 24#u64) = L.mem s (wa y 24#u64)) + (hCs128 : ∀ c ∈ Z.Cs, c.val < 128) + (hIdleT : ∀ c ∈ Z.Cs, L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) ∈ Z.T) + (hO0_node : ∀ c ∈ Z.Cs, L.nodeAt s c layout.node.IDLE_THREAD ≠ stA (L.curW s)) + (hTcbDisj : ∀ t₁ ∈ Z.T, ∀ t₂ ∈ Z.T, ∀ o₁ o₂ : U64, o₁.val < 4096 → o₂.val < 4096 → + (t₁ ≠ t₂ ∨ o₁ ≠ o₂) → wa t₁ o₁ ≠ wa t₂ o₂) : + VcpuRelA (L := L) Z OS Ub (L.wr s (stA (L.curW s)) sched.TS_RESTART) + (stUpdA σ σ.curThread xct .restart) := by + set s2 := L.wr s (stA (L.curW s)) sched.TS_RESTART + set σ1 := stUpdA σ σ.curThread xct .restart + have hst_al : Aligned (stA (L.curW s)) := by + rw [stA_eq_wa]; exact aligned_wa (hV.place.al_t _ hcurT) 3328#u64 (by decide) + have hvc_mem : ∀ v ∈ Z.V, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → + L.mem s2 (wa v o) = L.mem s (wa v o) := fun v hv o ho ho8 => by + have hne : stA (L.curW s) ≠ wa v o := by + rw [stA_eq_wa]; exact (hV.place.disj_tcb v hv (L.curW s) hcurT o 3328#u64 ho (by decide)).symm + exact mem_wr_neA hst_al (aligned_wa (hV.place.vc_al hv) o ho8) hne + have hwa0 : ∀ v : U64, wa v 0#u64 = v := fun v => by + apply UScalar.eq_of_val_eq; rw [wa_valP]; simp [UScalar.hBounds] + have hvc0 : ∀ v ∈ Z.V, L.mem s2 v = L.mem s v := fun v hv => by + rw [← hwa0 v]; exact hvc_mem v hv 0#u64 (by decide) (by decide) + have htcb_other : ∀ t ∈ Z.T, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → o ≠ 3328#u64 → + L.mem s2 (wa t o) = L.mem s (wa t o) := fun t ht o ho ho8 hne => by + have hneq : stA (L.curW s) ≠ wa t o := by + rw [stA_eq_wa]; exact hTcbDisj _ hcurT t ht 3328#u64 o (by decide) ho (Or.inr hne.symm) + exact mem_wr_neA hst_al (aligned_wa (hV.place.al_t t ht) o ho8) hneq + have hrun_mem : ∀ t ∈ Z.T, L.mem s2 (runVcpuA t) = L.mem s (runVcpuA t) := fun t ht => by + have hreq : runVcpuA t = wa t 3672#u64 := by unfold runVcpuA; rfl + rw [hreq]; exact htcb_other t ht 3672#u64 (by decide) (by decide) (by decide) + have hkv_eq : ∀ v ∈ Z.V, ∀ x, σ1.kheap (specPtr v) = some (.archObj (.vcpu x)) → + σ.kheap (specPtr v) = some (.archObj (.vcpu x)) := fun v hv x hkx => by + obtain ⟨v0, hkv0, -, -⟩ := hV.core.obj v hv + have hne : specPtr v ≠ σ.curThread := fun h => by rw [h, hkct] at hkv0; cases hkv0 + unfold σ1 stUpdA setObjA at hkx + simpa [Function.update, hne] using hkx + refine ⟨hcore1, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, hV.arch, by rw [L.ksb_wr]; exact hV.place⟩ + · intro v hv; rw [hvc_mem v hv 8#u64 (by decide) (by decide)]; exact hV.owner v hv + · intro v hv x hkx; rw [hvc_mem v hv 16#u64 (by decide) (by decide)]; exact hV.vppi v hv x (hkv_eq v hv x hkx) + · intro v hv x hkx; rw [hvc_mem v hv 32#u64 (by decide) (by decide)]; exact hV.runOff v hv x (hkv_eq v hv x hkx) + · intro v hv x hkx; rw [hvc_mem v hv 40#u64 (by decide) (by decide)]; exact hV.runOut v hv x (hkv_eq v hv x hkx) + · intro v hv x hkx i hi + have hlt : (vcpuRegOffA i).val < 4096 := by rw [vcpuRegOffA_val hi]; omega + have h8 : (vcpuRegOffA i).val % 8 = 0 := by rw [vcpuRegOffA_val hi]; omega + rw [vcpuRegWA, hvc_mem v hv (vcpuRegOffA i) hlt h8] + exact hV.regs v hv x (hkv_eq v hv x hkx) i hi + · intro v hv t ht hne + rw [hvc0 v hv] at ht + obtain ⟨htT, htv⟩ := hV.bind_v2t v hv t ht hne + exact ⟨htT, by rw [htcb_other t htT 3664#u64 (by decide) (by decide) (by decide)]; exact htv⟩ + · intro t ht v hv hne + rw [htcb_other t ht 3664#u64 (by decide) (by decide) (by decide)] at hv + obtain ⟨hvV, hvt⟩ := hV.bind_t2v t ht v hv hne + exact ⟨hvV, by rw [hvc0 v hvV]; exact hvt⟩ + · intro v hv c hc hne + rw [hvc_mem v hv 24#u64 (by decide) (by decide)] at hc + obtain ⟨hcT, hcv⟩ := hV.run_v2c v hv c hc hne + exact ⟨hcT, by rw [hrun_mem c hcT]; exact hcv⟩ + · intro c hc v hv hne + rw [hrun_mem c hc] at hv + obtain ⟨hvV, hvc'⟩ := hV.run_c2v c hc v hv hne + exact ⟨hvV, by rw [hvc_mem v hvV 24#u64 (by decide) (by decide)]; exact hvc'⟩ + · intro c hc hne + rw [hrun_mem c hc] at hne + have hne_c : c ≠ L.curW s := fun h => by subst h; exact hne hCurNoRun + have hne_st : stA (L.curW s) ≠ stA c := by + rw [stA_eq_wa, stA_eq_wa] + exact hTcbDisj _ hcurT c hc 3328#u64 3328#u64 (by decide) (by decide) (Or.inl hne_c.symm) + rw [mem_wr_neA hst_al (by rw [stA_eq_wa]; exact aligned_wa (hV.place.al_t c hc) 3328#u64 (by decide)) hne_st] + exact hV.run_state c hc hne + · intro t ht hne + rw [htcb_other t ht 3664#u64 (by decide) (by decide) (by decide)] at hne + rw [hrun_mem t ht]; exact hV.bind_run_disj t ht hne + · intro c hc + have hni : L.nodeAt s2 c layout.node.IDLE_THREAD = L.nodeAt s c layout.node.IDLE_THREAD := by + unfold LawfulStore.nodeAt; rw [L.ksb_wr] + have hal_ni : Aligned (L.nodeAt s c layout.node.IDLE_THREAD) := + hV.place.node_al c layout.node.IDLE_THREAD (by unfold layout.node.IDLE_THREAD; decide) + have hmem_ni : L.mem s2 (L.nodeAt s2 c layout.node.IDLE_THREAD) = + L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) := by + rw [hni]; exact mem_wr_neA hst_al hal_ni (hO0_node c hc).symm + rw [hmem_ni] + have hidT := hIdleT c hc + rw [htcb_other _ hidT 3664#u64 (by decide) (by decide) (by decide), hrun_mem _ hidT] + exact hV.idle_no_vcpu c hc + · intro v hv hor + rw [hvc0 v hv, hvc_mem v hv 24#u64 (by decide) (by decide)] at hor + obtain ⟨y, hy, hcap⟩ := hV.live v hv hor + refine ⟨y, hy, ?_⟩ + obtain ⟨h0, h8, -, -⟩ := hos1 y hy + unfold capAtA; rw [h0, h8]; exact hcap + +private theorem sanitizeVgicHcr_u32 (v : Word) (gicV3 : Bool) : + ucast 64 (ucast 32 (sanitizeVgicHcr v gicV3)) = sanitizeVgicHcr v gicV3 := by + have hlt : (sanitizeVgicHcr v gicV3).toNat < 2 ^ 32 := by + unfold sanitizeVgicHcr + cases gicV3 + · simp only [Bool.false_eq_true, ↓reduceIte, BitVec.toNat_and] + exact Nat.lt_of_le_of_lt Nat.and_le_right (by decide) + · simp only [↓reduceIte, BitVec.toNat_and] + exact Nat.lt_of_le_of_lt Nat.and_le_right (by decide) + apply BitVec.eq_of_toNat_eq + simp [ucast]; omega + +private theorem vcpu_reg_sync_frameA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} + {OS U Ac Pp Tb Ub : Set U64} {Lv : U64 → Nat} {ia ie : Nat} + {s : S} {a a1 : A} {σ : State Unit} {m : Fl4SpecA64.McState} {vc : U64} {xct : Tcb} {v : Vcpu} + (C : VcpuDecCtxA (st := st) (ar := ar) AR Z H OS U Ac Pp Tb Ub Lv ia ie s a σ m vc) + (hAR1 : AR a1 Z.hw) + (hkct : σ.kheap σ.curThread = some (.tcb xct)) + (hkv : σ.kheap (specPtr vc) = some (.archObj (.vcpu v))) + (E1 : JAtZAX (L := L) AR Z (L.wr s (stA (L.curW s)) sched.TS_RESTART) a + (stUpdA σ σ.curThread xct .restart) m) + (hos1 : ∀ y ∈ OS, L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART) y = L.mem s y ∧ + L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART) (wa y 8#u64) = L.mem s (wa y 8#u64) ∧ + L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART) (wa y 16#u64) = L.mem s (wa y 16#u64) ∧ + L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART) (wa y 24#u64) = L.mem s (wa y 24#u64)) + (reg : U64) (hreg : reg.val < 64) (val : U64) + (hhcr32 : reg.val = vrVgicHcr → ucast 64 (ucast 32 val.bv) = val.bv) (doSync : Bool) : + (do + let s3 ← vcpu.reg_set st (L.wr s (stA (L.curW s)) sched.TS_RESTART) vc reg val + if doSync then vcpu.sync_vgic_status st s3 vc ⟨BitVec.ofNat 64 fl4NumLrs⟩ fl4GicV3 + else ok s3) ⦃ s' => + let v1 := fl4VcpuRegSet reg.val val.bv v + let v2 := if doSync then syncVgicStatus v1 fl4NumLrs fl4GicV3 else v1 + let σ2 := updKSc (stUpdA σ σ.curThread xct .restart) (specPtr vc) (.archObj (.vcpu v2)) + JAtZAX (L := L) AR Z s' a1 σ2 m ∧ + TreeA st OS Z.G.chains Z.G.par s' ∧ + (CarryA Z (L.mem s) σ → CarryA Z (L.mem s') σ2) ⦄ := by + set s2 := L.wr s (stA (L.curW s)) sched.TS_RESTART + set σ1 := stUpdA σ σ.curThread xct .restart + have hreg_eq : reg = ⟨BitVec.ofNat 64 reg.val⟩ := by + apply UScalar.eq_of_val_eq + change reg.val = (BitVec.ofNat 64 reg.val).toNat + rw [BitVec.toNat_ofNat]; exact (Nat.mod_eq_of_lt (by omega)).symm + have hset : vcpu.reg_set st s2 vc reg val = ok (L.wr s2 (vcpuRegWA vc reg.val) val) := by + conv_lhs => rw [hreg_eq] + exact vcpu_reg_set_eqA s2 vc hreg val + rw [hset, bind_ok] + set s3 := L.wr s2 (vcpuRegWA vc reg.val) val + have hcurT : L.curW s ∈ Z.T := C.E.j.1.curT (L.cpu s) C.E.j.1.sch.cpuCs + have hE1_a1 : JAtZAX (L := L) AR Z s2 a1 σ1 m := ⟨{ E1.1 with ar := hAR1 }, E1.2⟩ + have hTr1 : TreeA st OS Z.G.chains Z.G.par s2 := C.hTr.keepO hos1 + have hCar1 := carryA_of_restartA C.hTr C.hsub hkct hos1 + have hCs128 : ∀ c ∈ Z.Cs, c.val < 128 := by + obtain ⟨n, -, hn128, hcn⟩ := C.E.j.1.csN + intro c hc; have := (hcn c).1 hc; omega + have hcpu128 : (L.cpu s2).val < 128 := by rw [L.cpu_wr]; exact hCs128 _ C.E.j.1.sch.cpuCs + have hfa1 := footA_of_wrA C.hfa (stA (L.curW s)) sched.TS_RESTART + have hne_vc_ct : specPtr vc ≠ σ.curThread := fun h => by rw [h, hkct] at hkv; cases hkv + have hkv1 : σ1.kheap (specPtr vc) = some (.archObj (.vcpu v)) := by + unfold σ1 stUpdA setObjA; simp [Function.update, hne_vc_ct, hkv] + have hreg_al : Aligned (vcpuRegWA vc reg.val) := + C.hV.place.vcPage_al (vcpuRegWA_in_vcPage C.hvc hreg) + have hPl2 : VcPlacedA Z OS Ub (L.ksb s2) := by rw [L.ksb_wr]; exact C.hV.place + have hmeta1 := fl4VcpuRegSet_pres_metaA reg.val val.bv v + cases doSync with + | false => + simp only [Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + obtain ⟨hJ2, hTr2, hCar2⟩ := vcpuNonLinkWr_frameA hE1_a1 hTr1 hPl2 hCs128 hcpu128 + C.hExt C.hSw hfa1 (writesIn_wrA s2 hreg_al) (vcpuRegWA_nonLinkA C.hV.place C.hvc hreg) + C.hvc hkv1 hmeta1.1 hmeta1.2.2.1 C.hsub + exact ⟨hJ2, hTr2, fun hC => hCar2 (hCar1 hC)⟩ + | true => + simp only [↓reduceIte] + have hO0 := C.E.stOffA (vc_ne_stA C.hV.place hcurT) + have hO0_node : ∀ c ∈ Z.Cs, L.nodeAt s c layout.node.IDLE_THREAD ≠ stA (L.curW s) := fun c hc => + hO0.node c hc layout.node.IDLE_THREAD (by unfold layout.node.IDLE_THREAD; decide) + have hTcbDisj : ∀ t₁ ∈ Z.T, ∀ t₂ ∈ Z.T, ∀ o₁ o₂ : U64, o₁.val < 4096 → o₂.val < 4096 → + (t₁ ≠ t₂ ∨ o₁ ≠ o₂) → wa t₁ o₁ ≠ wa t₂ o₂ := by + intro t₁ ht₁ t₂ ht₂ o₁ o₂ ho₁ ho₂ hne heq + have h1 : t₁ ∈ H.tcbs := by rw [C.E.tcbs]; exact ht₁ + have h2 : t₂ ∈ H.tcbs := by rw [C.E.tcbs]; exact ht₂ + have hta1 : TcbAddr t₁ := C.E.sq.tcbs t₁ h1 + have hta2 : TcbAddr t₂ := C.E.sq.tcbs t₂ h2 + have hb1 := t₁.hBounds; have hb2 := t₂.hBounds + simp only [U64.size_def, U64.numBits, UScalar.size, UScalarTy.numBits] at hb1 hb2 + have h4k1 := hta1.align; have h4k2 := hta2.align + have hw1 : (wa t₁ o₁).val = t₁.val + o₁.val := by + rw [wa_valP]; exact Nat.mod_eq_of_lt (by omega) + have hw2 : (wa t₂ o₂).val = t₂.val + o₂.val := by + rw [wa_valP]; exact Nat.mod_eq_of_lt (by omega) + have hval := congrArg UScalar.val heq + rw [hw1, hw2] at hval + cases hne with + | inl hne_t => + exact hne_t (UScalar.eq_of_val_eq (by omega)) + | inr hne_o => + by_cases htEq : t₁ = t₂ + · subst htEq; exact hne_o (UScalar.eq_of_val_eq (by omega)) + · exact htEq (UScalar.eq_of_val_eq (by omega)) + have hV2 : VcpuRelA (L := L) Z OS Ub s2 σ1 := + vcpuRelA_of_restartA C.hV hcurT C.hCurNoRun hkct E1.2.1 hos1 hCs128 C.hIdleT hO0_node hTcbDisj + have hksFit2 : (L.ksb s2).val + ksSizeA ≤ 2 ^ 64 := by rw [L.ksb_wr]; exact C.E.j.1.ksFit + have hIdleT2 : ∀ c ∈ Z.Cs, L.mem s2 (L.nodeAt s2 c layout.node.IDLE_THREAD) ∈ Z.T := fun c hc => by + have hni : L.nodeAt s2 c layout.node.IDLE_THREAD = L.nodeAt s c layout.node.IDLE_THREAD := by + unfold LawfulStore.nodeAt; rw [L.ksb_wr] + have hst_al : Aligned (stA (L.curW s)) := by + rw [stA_eq_wa]; exact aligned_wa (C.hV.place.al_t _ hcurT) 3328#u64 (by decide) + have hal_ni : Aligned (L.nodeAt s c layout.node.IDLE_THREAD) := + C.hV.place.node_al c layout.node.IDLE_THREAD (by unfold layout.node.IDLE_THREAD; decide) + rw [hni, mem_wr_neA hst_al hal_ni (hO0_node c hc).symm]; exact C.hIdleT c hc + have hV3 : VcpuRelA (L := L) Z OS Ub s3 + (updKSc σ1 (specPtr vc) (.archObj (.vcpu (fl4VcpuRegSet reg.val val.bv v)))) := + hV2.upd_regA hksFit2 hCs128 hIdleT2 C.hvc hkv1 hreg val hhcr32 + have hksFit3 : (L.ksb s3).val + ksSizeA ≤ 2 ^ 64 := by rw [L.ksb_wr]; exact hksFit2 + have hIdleT3 : ∀ c ∈ Z.Cs, L.mem s3 (L.nodeAt s3 c layout.node.IDLE_THREAD) ∈ Z.T := fun c hc => by + have hni : L.nodeAt s3 c layout.node.IDLE_THREAD = L.nodeAt s2 c layout.node.IDLE_THREAD := by + unfold LawfulStore.nodeAt; rw [L.ksb_wr] + have hal_ni : Aligned (L.nodeAt s2 c layout.node.IDLE_THREAD) := + hPl2.node_al c layout.node.IDLE_THREAD (by unfold layout.node.IDLE_THREAD; decide) + have hne_ni : vcpuRegWA vc reg.val ≠ L.nodeAt s2 c layout.node.IDLE_THREAD := + fun heq => hPl2.disj_node hksFit2 (vcpuRegWA_in_vcPage C.hvc hreg) (hCs128 c hc) + (by unfold layout.node.IDLE_THREAD; decide) heq + rw [hni, mem_wr_neA hreg_al hal_ni hne_ni]; exact hIdleT2 c hc + refine WP.spec_mono (sync_vgic_status_refinesA hV3 hksFit3 hCs128 hIdleT3 C.hvc (updKSc_same σ1 _ _)) ?_ + rintro s4 ⟨-, hwr4⟩ + have hwr_all : WritesIn st s2 s4 (fun b => + b = vcpuRegWA vc reg.val ∨ b = vcpuRegWA vc vrVgicElrsr ∨ + b = vcpuRegWA vc vrVgicEisr ∨ b = vcpuRegWA vc vrVgicMisr) := by + refine ⟨hwr4.1.trans (L.ksb_wr _ _ _), hwr4.2.1.trans (L.cpu_wr _ _ _), fun b hb hnot => ?_⟩ + have hnot4 : ¬ (b = vcpuRegWA vc vrVgicElrsr ∨ b = vcpuRegWA vc vrVgicEisr ∨ b = vcpuRegWA vc vrVgicMisr) := + fun h => hnot (Or.inr h) + have hnot3 : b ≠ vcpuRegWA vc reg.val := fun h => hnot (Or.inl h) + rw [hwr4.2.2 b hb hnot4] + exact mem_wr_neA hreg_al hb hnot3.symm + have hW_all : ∀ b, (b = vcpuRegWA vc reg.val ∨ b = vcpuRegWA vc vrVgicElrsr ∨ + b = vcpuRegWA vc vrVgicEisr ∨ b = vcpuRegWA vc vrVgicMisr) → + VcPage Z.V b ∧ ∀ v' ∈ Z.V, b ≠ v' ∧ b ≠ wa v' 24#u64 := by + intro b hb + rcases hb with rfl | rfl | rfl | rfl + · exact vcpuRegWA_nonLinkA C.hV.place C.hvc hreg _ rfl + · exact vcpuRegWA_nonLinkA C.hV.place C.hvc (by unfold vrVgicElrsr; omega) _ rfl + · exact vcpuRegWA_nonLinkA C.hV.place C.hvc (by unfold vrVgicEisr; omega) _ rfl + · exact vcpuRegWA_nonLinkA C.hV.place C.hvc (by unfold vrVgicMisr; omega) _ rfl + have hmeta2 := syncVgicStatus_pres_metaA (fl4VcpuRegSet reg.val val.bv v) fl4NumLrs fl4GicV3 + obtain ⟨hJ2, hTr2, hCar2⟩ := vcpuNonLinkWr_frameA hE1_a1 hTr1 hPl2 hCs128 hcpu128 + C.hExt C.hSw hfa1 hwr_all hW_all C.hvc hkv1 + (hmeta2.1.trans hmeta1.1) (hmeta2.2.2.1.trans hmeta1.2.2.1) C.hsub + exact ⟨hJ2, hTr2, fun hC => hCar2 (hCar1 hC)⟩ + +/-- **Label 63 (`ARMVCPUWriteReg`)** on `VcpuTopPostA`. -/ +theorem vcpu_write_reg_jA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} + {OS U Ac Pp Tb Ub : Set U64} {Lv : U64 → Nat} {ia ie : Nat} + {s : S} {a : A} {σ : State Unit} {m : Fl4SpecA64.McState} {vc : U64} + (hP : VcpuPlatAgreeA ar) (hAK : VcpuArchKeepsA ar AR) + (C : VcpuDecCtxA (st := st) (ar := ar) AR Z H OS U Ac Pp Tb Ub Lv ia ie s a σ m vc) + {Rp Nt : Set U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} {slot : U64} + (Pl : SysPlace st H Z.Cs H.frame Rp Nt OS s extra slot) (Y : SymL st H Z.Cs s) + (hrun : Runnable (L.mem s (stA (L.curW s)))) + (hcur : (L.mem s (inqA (L.curW s)) &&& sched.Q_RELEASE) = 0#u64 ∧ L.mem s (scA (L.curW s)) ≠ 0#u64 ∧ + L.mem s (scA (L.curW s)) ∈ Ac) + (hvs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → Aligned b ∧ b ≠ stA (L.curW s)) + (length buf : U64) (call : Bool) (args : List U64) (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) : + vcpu.decode_write_reg st ar s a vc length buf ⦃ res => + VcpuTopPostA (L := L) AR Z OS m call σ + ((NonDetE.liftME Fl4ArchInvocation.vcpu <| + decodeFl4VcpuWriteReg (Z := Unit) (specPtr vc) (args.map UScalar.bv)).run σ) s res ⦄ := by + have hAR : AR a Z.hw := C.E.j.1.ar + unfold vcpu.decode_write_reg + by_cases hlen2 : length < 2#u64 + · rw [if_pos hlen2] + unfold error.truncated; rw [bind_ok, WP.spec_ok] + have hlt : (args.map UScalar.bv).length < 2 := by rw [List.length_map, ← hlen]; exact hlen2 + have hd : (decodeFl4VcpuWriteReg (Z := Unit) (specPtr vc) (args.map UScalar.bv)).run σ = + ({(.error .truncatedMessage, σ)}, False) := by + unfold decodeFl4VcpuWriteReg NonDetE.whenE; rw [if_pos hlt]; exact ndE_throw_bind _ _ _ + exact ⟨rfl, hAR, .truncatedMessage, liftME_vcpu_errA hd, Or.inl trivial⟩ + · rw [if_neg hlen2] + have hge2 : 2 ≤ args.length := by have : ¬ length.val < 2 := hlen2; omega + have hct : sched.cur_thread st s = ok (L.curW s) := eq_of_spec_fd (sched.cur_thread.spec s) + rw [hct, bind_ok] + cases hargs_eq : args with + | nil => simp [hargs_eq] at hge2 + | cons field rest => + cases hrest_eq : rest with + | nil => simp [hargs_eq, hrest_eq] at hge2 + | cons val rest2 => + rw [hargs 0#u64 field (by simp [hargs_eq]), bind_ok, + hargs 1#u64 val (by simp [hargs_eq, hrest_eq]), bind_ok] + have hnum : (fl4_abi.consts.vcpu_reg.NUM).val = 64 := by + unfold fl4_abi.consts.vcpu_reg.NUM; rfl + have hlr0 : (fl4_abi.consts.vcpu_reg.VGIC_LR0).val = 48 := by + unfold fl4_abi.consts.vcpu_reg.VGIC_LR0; rfl + by_cases hge64 : field ≥ fl4_abi.consts.vcpu_reg.NUM + · rw [if_pos hge64] + unfold error.invalid_arg; rw [bind_ok, WP.spec_ok] + have hf64 : field.val ≥ vrNum := by + have : fl4_abi.consts.vcpu_reg.NUM.val ≤ field.val := hge64 + unfold vrNum; omega + have hd : (decodeFl4VcpuWriteReg (Z := Unit) (specPtr vc) + ((field :: val :: rest2).map UScalar.bv)).run σ = + ({(.error (.invalidArgument 0), σ)}, False) := by + unfold decodeFl4VcpuWriteReg + rw [ndE_whenE_false (by simp)] + exact ndE_whenE_true (by simpa [the] using Or.inl hf64) _ _ _ + exact ⟨rfl, hAR, .invalidArgument 0, liftME_vcpu_errA hd, Or.inl rfl⟩ + · rw [if_neg hge64, vcpu_is_readonly_reg_eqA field, bind_ok] + have hf_lt64 : field.val < 64 := by + have : ¬ fl4_abi.consts.vcpu_reg.NUM.val ≤ field.val := hge64; omega + by_cases hro : fl4VcpuRegReadOnly field.val = true + · rw [if_pos hro] + unfold error.invalid_arg; rw [bind_ok, WP.spec_ok] + have hd : (decodeFl4VcpuWriteReg (Z := Unit) (specPtr vc) + ((field :: val :: rest2).map UScalar.bv)).run σ = + ({(.error (.invalidArgument 0), σ)}, False) := by + unfold decodeFl4VcpuWriteReg + rw [ndE_whenE_false (by simp)] + exact ndE_whenE_true (by simpa [the] using Or.inr hro) _ _ _ + exact ⟨rfl, hAR, .invalidArgument 0, liftME_vcpu_errA hd, Or.inl rfl⟩ + · rw [if_neg hro, vcpu_num_lrs_eqA hP a, bind_ok] + obtain ⟨g, hga, hgb⟩ := hP.gic a + rw [hga, bind_ok] + have hgb' : decide (g = 3#u64) = fl4GicV3 := hgb + have hlrs_val : (⟨BitVec.ofNat 64 fl4NumLrs⟩ : U64).val = fl4NumLrs := by + change (BitVec.ofNat 64 fl4NumLrs).toNat = fl4NumLrs + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by have := fl4NumLrs_le_16; omega) + have hcurT : L.curW s ∈ Z.T := C.E.j.1.curT (L.cpu s) C.E.j.1.sch.cpuCs + have hvc_st := vc_ne_stA C.hV.place hcurT + obtain ⟨v, hkv, -, -⟩ := C.hV.core.obj vc C.hvc + have hnot_bad : ¬ (field.val ≥ vrNum ∨ fl4VcpuRegReadOnly field.val = true) := by + unfold vrNum; exact fun h => h.elim (by omega) hro + by_cases hge48 : field ≥ fl4_abi.consts.vcpu_reg.VGIC_LR0 + · rw [if_pos hge48] + have hge48' : 48 ≤ field.val := by + have : fl4_abi.consts.vcpu_reg.VGIC_LR0.val ≤ field.val := hge48; omega + have hsub_eq := u64_sub_ok (x := field) (y := fl4_abi.consts.vcpu_reg.VGIC_LR0) + (by rw [hlr0]; omega) + have hsub_val : (⟨BitVec.ofNat 64 (field.val - fl4_abi.consts.vcpu_reg.VGIC_LR0.val)⟩ : U64).val = + field.val - fl4_abi.consts.vcpu_reg.VGIC_LR0.val := by + change (BitVec.ofNat 64 _).toNat = _ + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + rw [hsub_eq, bind_ok] + set sub : U64 := ⟨BitVec.ofNat 64 (field.val - fl4_abi.consts.vcpu_reg.VGIC_LR0.val)⟩ + by_cases hge_lrs : sub ≥ ⟨BitVec.ofNat 64 fl4NumLrs⟩ + · rw [if_pos hge_lrs] + unfold error.invalid_arg; rw [bind_ok, WP.spec_ok] + have hcond : field.val - vrVgicLr0 ≥ fl4NumLrs := by + have h1 : fl4NumLrs ≤ sub.val := by + have : (⟨BitVec.ofNat 64 fl4NumLrs⟩ : U64).val ≤ sub.val := hge_lrs; omega + unfold vrVgicLr0; omega + have hd : (decodeFl4VcpuWriteReg (Z := Unit) (specPtr vc) + ((field :: val :: rest2).map UScalar.bv)).run σ = + ({(.error (.invalidArgument 0), σ)}, False) := by + unfold decodeFl4VcpuWriteReg + rw [ndE_whenE_false (by simp), ndE_whenE_false (by simpa [the] using hnot_bad), + if_pos (by simp [the, vrVgicLr0]; omega)] + exact ndE_whenE_true (by simpa [the] using hcond) _ _ _ + exact ⟨rfl, hAR, .invalidArgument 0, liftME_vcpu_errA hd, Or.inl rfl⟩ + · rw [if_neg hge_lrs] + have hlt_lrs : ¬ (field.val - vrVgicLr0 ≥ fl4NumLrs) := by + intro hbad; apply hge_lrs + change (⟨BitVec.ofNat 64 fl4NumLrs⟩ : U64).val ≤ sub.val + rw [hlrs_val, hsub_val, hlr0]; unfold vrVgicLr0 at hbad; omega + have hsan := vcpu_sanitize_vgic_lr_eqA val fl4GicV3 + rw [show (decide (g = 3#u64)) = fl4GicV3 from hgb'] + rw [hsan, bind_ok] + cases hopt : sanitizeVgicLr val.bv fl4GicV3 with + | none => + simp only [Option.map_none] + unfold error.invalid_arg; rw [bind_ok, WP.spec_ok] + have hd : (decodeFl4VcpuWriteReg (Z := Unit) (specPtr vc) + ((field :: val :: rest2).map UScalar.bv)).run σ = + ({(.error (.invalidArgument 1), σ)}, False) := by + unfold decodeFl4VcpuWriteReg + rw [ndE_whenE_false (by simp), ndE_whenE_false (by simpa [the] using hnot_bad), + if_pos (by simp [the, vrVgicLr0]; omega), + ndE_whenE_false (by simpa [the] using hlt_lrs)] + simp only [List.map_cons, List.getElem?_cons_succ, List.getElem?_cons_zero, the, hopt]; rfl + exact ⟨rfl, hAR, .invalidArgument 1, liftME_vcpu_errA hd, Or.inl rfl⟩ + | some san => + simp only [Option.map_some] + refine WP.spec_bind (stall_vcpu_localA hAK hAR vc C.hStallT C.hStallC) ?_ + rintro ⟨s1, a1⟩ ⟨hs1, hAR1⟩ + dsimp only at hs1; subst s1 + change (do + let s2 ← sched.set_thread_state st s (L.curW s) sched.TS_RESTART + let s3 ← vcpu.reg_set st s2 vc field ⟨san⟩ + let s4 ← vcpu.sync_vgic_status st s3 vc ⟨BitVec.ofNat 64 fl4NumLrs⟩ fl4GicV3 + ok (core.result.Result.Ok (), s4, a1)) ⦃ _ ⦄ + obtain ⟨xct, hkct, hst_ok, hst_spec, E1, -, hos1, -, -⟩ := + C.E.restartA Pl Y hrun hcur hvs hvc_st + rw [hst_ok, bind_ok] + have hne_vc_ct : specPtr vc ≠ σ.curThread := fun h => by rw [h, hkct] at hkv; cases hkv + have hkv1 : (stUpdA σ σ.curThread xct .restart).kheap (specPtr vc) = some (.archObj (.vcpu v)) := by + unfold stUpdA setObjA; simp [Function.update, hne_vc_ct, hkv] + have hnot_ap1r : fl4IsVgicAp1r field.val = false := by + unfold fl4IsVgicAp1r vrVgicAp1r0 vrVgicAp1r3; simp; omega + have hsync_cond : field.val ≥ vrVgicLr0 ∨ field.val = vrVgicHcr ∨ field.val = vrVgicVmcr := + Or.inl (by unfold vrVgicLr0; omega) + have hperf := fl4PerformVcpuInv_writeReg_eqA field.val san + (show (stUpdA σ σ.curThread xct .restart).archState.armCurrentVcpu = none from C.hV.arch) hkv1 + simp only [hnot_ap1r, Bool.false_eq_true, and_false, ↓reduceIte, hsync_cond] at hperf + have hd : (decodeFl4VcpuWriteReg (Z := Unit) (specPtr vc) + ((field :: val :: rest2).map UScalar.bv)).run σ = + ({(.ok (.writeReg (specPtr vc) field.val san), σ)}, False) := by + unfold decodeFl4VcpuWriteReg + rw [ndE_whenE_false (by simp), ndE_whenE_false (by simpa [the] using hnot_bad), + if_pos (by simp [the, vrVgicLr0]; omega), + ndE_whenE_false (by simpa [the] using hlt_lrs)] + simp only [List.map_cons, List.getElem?_cons_succ, List.getElem?_cons_zero, the, hopt]; rfl + have hstep := vcpu_reg_sync_frameA C hAR1 hkct hkv E1.j hos1 field hf_lt64 ⟨san⟩ + (by intro h; unfold vrVgicHcr at h; omega) true + simp only [↓reduceIte] at hstep + refine spec_bind_assoc hstep ?_ + rintro s4 ⟨hJ4, hTr4, hCar4⟩ + rw [WP.spec_ok] + refine ⟨.vcpu (.writeReg (specPtr vc) field.val san), liftME_vcpu_okA hd, + stUpdA σ σ.curThread xct .restart, hst_spec, _, [], ?_, Or.inl ⟨Z, hJ4, hTr4, hCar4⟩⟩ + unfold fl4ArchPerformInvocation NonDetE.liftE + change (Except.ok [], _) ∈ ((fl4PerformVcpuInv (.writeReg (specPtr vc) field.val san) >>= _) _).1 + rw [nd_bind_oneSc hperf]; exact Set.mem_singleton _ + · rw [if_neg hge48] + have hlt48 : ¬ (field.val ≥ vrVgicLr0) := by + intro hbad; apply hge48; change fl4_abi.consts.vcpu_reg.VGIC_LR0.val ≤ field.val + rw [hlr0]; unfold vrVgicLr0 at hbad; omega + refine WP.spec_bind (stall_vcpu_localA hAK hAR vc C.hStallT C.hStallC) ?_ + rintro ⟨s1, a1⟩ ⟨hs1, hAR1⟩ + dsimp only at hs1; subst s1 + change (do + let s2 ← sched.set_thread_state st s (L.curW s) sched.TS_RESTART + if g = 3#u64 then do + let v0 ← if field = fl4_abi.consts.vcpu_reg.HCR_EL2 then vcpu.sanitize_hcr val + else if field = fl4_abi.consts.vcpu_reg.VGIC_HCR then vcpu.sanitize_vgic_hcr val (g = 3#u64) + else ok val + let s3 ← vcpu.reg_set st s2 vc field v0 + if field = fl4_abi.consts.vcpu_reg.VGIC_HCR then do + let s4 ← vcpu.sync_vgic_status st s3 vc ⟨BitVec.ofNat 64 fl4NumLrs⟩ (g = 3#u64) + ok (core.result.Result.Ok (), s4, a1) + else if field = fl4_abi.consts.vcpu_reg.VGIC_VMCR then do + let s4 ← vcpu.sync_vgic_status st s3 vc ⟨BitVec.ofNat 64 fl4NumLrs⟩ (g = 3#u64) + ok (core.result.Result.Ok (), s4, a1) + else ok (core.result.Result.Ok (), s3, a1) + else do + let b1 ← vcpu.is_vgic_ap1r field + if b1 then ok (core.result.Result.Ok (), s2, a1) + else do + let v0 ← if field = fl4_abi.consts.vcpu_reg.HCR_EL2 then vcpu.sanitize_hcr val + else if field = fl4_abi.consts.vcpu_reg.VGIC_HCR then vcpu.sanitize_vgic_hcr val (g = 3#u64) + else ok val + let s3 ← vcpu.reg_set st s2 vc field v0 + if field = fl4_abi.consts.vcpu_reg.VGIC_HCR then do + let s4 ← vcpu.sync_vgic_status st s3 vc ⟨BitVec.ofNat 64 fl4NumLrs⟩ (g = 3#u64) + ok (core.result.Result.Ok (), s4, a1) + else if field = fl4_abi.consts.vcpu_reg.VGIC_VMCR then do + let s4 ← vcpu.sync_vgic_status st s3 vc ⟨BitVec.ofNat 64 fl4NumLrs⟩ (g = 3#u64) + ok (core.result.Result.Ok (), s4, a1) + else ok (core.result.Result.Ok (), s3, a1)) ⦃ _ ⦄ + obtain ⟨xct, hkct, hst_ok, hst_spec, E1, -, hos1, -, -⟩ := + C.E.restartA Pl Y hrun hcur hvs hvc_st + rw [hst_ok, bind_ok] + have hne_vc_ct : specPtr vc ≠ σ.curThread := fun h => by rw [h, hkct] at hkv; cases hkv + have hkv1 : (stUpdA σ σ.curThread xct .restart).kheap (specPtr vc) = some (.archObj (.vcpu v)) := by + unfold stUpdA setObjA; simp [Function.update, hne_vc_ct, hkv] + set wSpec : Word := + if field.val = vrHcr then sanitizeHcr val.bv + else if field.val = vrVgicHcr then sanitizeVgicHcr val.bv fl4GicV3 + else val.bv + have hd : (decodeFl4VcpuWriteReg (Z := Unit) (specPtr vc) + ((field :: val :: rest2).map UScalar.bv)).run σ = + ({(.ok (.writeReg (specPtr vc) field.val wSpec), σ)}, False) := by + unfold decodeFl4VcpuWriteReg + rw [ndE_whenE_false (by simp), ndE_whenE_false (by simpa [the] using hnot_bad), + if_neg (by simpa [the] using hlt48)] + rfl + have hval_san : + (if field = fl4_abi.consts.vcpu_reg.HCR_EL2 then vcpu.sanitize_hcr val + else if field = fl4_abi.consts.vcpu_reg.VGIC_HCR then vcpu.sanitize_vgic_hcr val fl4GicV3 + else ok val) = ok ⟨wSpec⟩ := by + have hhcr_val : (fl4_abi.consts.vcpu_reg.HCR_EL2).val = vrHcr := by + unfold fl4_abi.consts.vcpu_reg.HCR_EL2 vrHcr; rfl + have hvhcr_val : (fl4_abi.consts.vcpu_reg.VGIC_HCR).val = vrVgicHcr := by + unfold fl4_abi.consts.vcpu_reg.VGIC_HCR vrVgicHcr; rfl + by_cases hh : field.val = vrHcr + · have hh' : field = fl4_abi.consts.vcpu_reg.HCR_EL2 := UScalar.eq_of_val_eq (by omega) + rw [if_pos hh', vcpu_sanitize_hcr_eqA val]; simp only [wSpec, hh, ↓reduceIte] + · have hh' : field ≠ fl4_abi.consts.vcpu_reg.HCR_EL2 := fun e => hh (by rw [e, hhcr_val]) + rw [if_neg hh'] + by_cases hvh : field.val = vrVgicHcr + · have hvh' : field = fl4_abi.consts.vcpu_reg.VGIC_HCR := UScalar.eq_of_val_eq (by omega) + rw [if_pos hvh', vcpu_sanitize_vgic_hcr_eqA val fl4GicV3] + change ok (⟨sanitizeVgicHcr val.bv fl4GicV3⟩ : U64) = + ok (⟨if field.val = vrHcr then _ else if field.val = vrVgicHcr then _ else _⟩ : U64) + rw [if_neg hh, if_pos hvh] + · have hvh' : field ≠ fl4_abi.consts.vcpu_reg.VGIC_HCR := fun e => hvh (by rw [e, hvhcr_val]) + rw [if_neg hvh'] + change ok val = + ok (⟨if field.val = vrHcr then _ else if field.val = vrVgicHcr then _ else _⟩ : U64) + rw [if_neg hh, if_neg hvh] + -- Shared non-AP1R write+sync tail + have htail : ¬ (¬fl4GicV3 = true ∧ fl4IsVgicAp1r field.val = true) → + (do + let v0 ← if field = fl4_abi.consts.vcpu_reg.HCR_EL2 then vcpu.sanitize_hcr val + else if field = fl4_abi.consts.vcpu_reg.VGIC_HCR then vcpu.sanitize_vgic_hcr val fl4GicV3 + else ok val + let s3 ← vcpu.reg_set st (L.wr s (stA (L.curW s)) sched.TS_RESTART) vc field v0 + if field = fl4_abi.consts.vcpu_reg.VGIC_HCR then do + let s4 ← vcpu.sync_vgic_status st s3 vc ⟨BitVec.ofNat 64 fl4NumLrs⟩ fl4GicV3 + ok (core.result.Result.Ok (), s4, a1) + else if field = fl4_abi.consts.vcpu_reg.VGIC_VMCR then do + let s4 ← vcpu.sync_vgic_status st s3 vc ⟨BitVec.ofNat 64 fl4NumLrs⟩ fl4GicV3 + ok (core.result.Result.Ok (), s4, a1) + else ok (core.result.Result.Ok (), s3, a1)) ⦃ res => + VcpuTopPostA (L := L) AR Z OS m call σ + ((NonDetE.liftME Fl4ArchInvocation.vcpu <| + decodeFl4VcpuWriteReg (Z := Unit) (specPtr vc) ((field :: val :: rest2).map UScalar.bv)).run σ) s res ⦄ := by + intro hnot_ap + rw [hval_san, bind_ok] + have hvhcr_val : (fl4_abi.consts.vcpu_reg.VGIC_HCR).val = vrVgicHcr := by + unfold fl4_abi.consts.vcpu_reg.VGIC_HCR vrVgicHcr; rfl + have hvmcr_val : (fl4_abi.consts.vcpu_reg.VGIC_VMCR).val = vrVgicVmcr := by + unfold fl4_abi.consts.vcpu_reg.VGIC_VMCR vrVgicVmcr; rfl + have hperf := fl4PerformVcpuInv_writeReg_eqA field.val wSpec + (show (stUpdA σ σ.curThread xct .restart).archState.armCurrentVcpu = none from C.hV.arch) hkv1 + rw [if_neg hnot_ap] at hperf + by_cases hvh : field = fl4_abi.consts.vcpu_reg.VGIC_HCR + · simp only [if_pos hvh] + have hsync : field.val ≥ vrVgicLr0 ∨ field.val = vrVgicHcr ∨ field.val = vrVgicVmcr := + Or.inr (Or.inl (by rw [hvh, hvhcr_val])) + dsimp only at hperf; rw [if_pos hsync] at hperf + have hstep := vcpu_reg_sync_frameA C hAR1 hkct hkv E1.j hos1 field hf_lt64 ⟨wSpec⟩ + (by + intro _; simp only [wSpec] + have hne_hcr : field.val ≠ vrHcr := by + rw [hvh, hvhcr_val]; unfold vrVgicHcr vrHcr; decide + have heq_vhcr : field.val = vrVgicHcr := by rw [hvh, hvhcr_val] + rw [if_neg hne_hcr, if_pos heq_vhcr] + exact sanitizeVgicHcr_u32 val.bv fl4GicV3) true + simp only [↓reduceIte] at hstep + refine spec_bind_assoc hstep ?_ + rintro s4 ⟨hJ4, hTr4, hCar4⟩ + rw [WP.spec_ok] + refine ⟨.vcpu (.writeReg (specPtr vc) field.val wSpec), liftME_vcpu_okA hd, + stUpdA σ σ.curThread xct .restart, hst_spec, _, [], ?_, Or.inl ⟨Z, hJ4, hTr4, hCar4⟩⟩ + unfold fl4ArchPerformInvocation NonDetE.liftE + change (Except.ok [], _) ∈ ((fl4PerformVcpuInv (.writeReg (specPtr vc) field.val wSpec) >>= _) _).1 + rw [nd_bind_oneSc hperf]; exact Set.mem_singleton _ + · simp only [if_neg hvh] + by_cases hvm : field = fl4_abi.consts.vcpu_reg.VGIC_VMCR + · simp only [if_pos hvm] + have hsync : field.val ≥ vrVgicLr0 ∨ field.val = vrVgicHcr ∨ field.val = vrVgicVmcr := + Or.inr (Or.inr (by rw [hvm, hvmcr_val])) + dsimp only at hperf; rw [if_pos hsync] at hperf + have hstep := vcpu_reg_sync_frameA C hAR1 hkct hkv E1.j hos1 field hf_lt64 ⟨wSpec⟩ + (by intro h; exact absurd (UScalar.eq_of_val_eq (by rw [hvhcr_val]; exact h)) hvh) true + simp only [↓reduceIte] at hstep + refine spec_bind_assoc hstep ?_ + rintro s4 ⟨hJ4, hTr4, hCar4⟩ + rw [WP.spec_ok] + refine ⟨.vcpu (.writeReg (specPtr vc) field.val wSpec), liftME_vcpu_okA hd, + stUpdA σ σ.curThread xct .restart, hst_spec, _, [], ?_, Or.inl ⟨Z, hJ4, hTr4, hCar4⟩⟩ + unfold fl4ArchPerformInvocation NonDetE.liftE + change (Except.ok [], _) ∈ ((fl4PerformVcpuInv (.writeReg (specPtr vc) field.val wSpec) >>= _) _).1 + rw [nd_bind_oneSc hperf]; exact Set.mem_singleton _ + · simp only [if_neg hvm] + have hnsync : ¬ (field.val ≥ vrVgicLr0 ∨ field.val = vrVgicHcr ∨ field.val = vrVgicVmcr) := by + intro h; rcases h with h | h | h + · exact hlt48 h + · exact hvh (UScalar.eq_of_val_eq (by rw [hvhcr_val]; exact h)) + · exact hvm (UScalar.eq_of_val_eq (by rw [hvmcr_val]; exact h)) + dsimp only at hperf; rw [if_neg hnsync] at hperf + have hstep := vcpu_reg_sync_frameA C hAR1 hkct hkv E1.j hos1 field hf_lt64 ⟨wSpec⟩ + (by intro h; exact absurd (UScalar.eq_of_val_eq (by rw [hvhcr_val]; exact h)) hvh) false + simp only [Bool.false_eq_true, ↓reduceIte] at hstep + refine spec_bind_of_ok hstep ?_ + rintro s3 ⟨hJ3, hTr3, hCar3⟩ + rw [WP.spec_ok] + refine ⟨.vcpu (.writeReg (specPtr vc) field.val wSpec), liftME_vcpu_okA hd, + stUpdA σ σ.curThread xct .restart, hst_spec, _, [], ?_, Or.inl ⟨Z, hJ3, hTr3, hCar3⟩⟩ + unfold fl4ArchPerformInvocation NonDetE.liftE + change (Except.ok [], _) ∈ ((fl4PerformVcpuInv (.writeReg (specPtr vc) field.val wSpec) >>= _) _).1 + rw [nd_bind_oneSc hperf]; exact Set.mem_singleton _ + by_cases hg3 : g = 3#u64 + · have hfl : fl4GicV3 = true := by rw [← hgb', decide_eq_true hg3] + rw [if_pos hg3] + have hdec : (decide (g = 3#u64)) = fl4GicV3 := hgb' + simpa [hdec] using htail (by simp [hfl]) + · have hfl : fl4GicV3 = false := by rw [← hgb', decide_eq_false hg3] + rw [if_neg hg3, vcpu_is_vgic_ap1r_eqA field, bind_ok] + by_cases hap : fl4IsVgicAp1r field.val = true + · rw [if_pos hap, WP.spec_ok] + have hperf := fl4PerformVcpuInv_writeReg_eqA field.val wSpec + (show (stUpdA σ σ.curThread xct .restart).archState.armCurrentVcpu = none from C.hV.arch) hkv1 + have hap_cond : ¬fl4GicV3 = true ∧ fl4IsVgicAp1r field.val = true := by simp [hfl, hap] + rw [if_pos hap_cond] at hperf + have hE1_a1 : JAtZAX (L := L) AR Z (L.wr s (stA (L.curW s)) sched.TS_RESTART) a1 + (stUpdA σ σ.curThread xct .restart) m := + ⟨{ E1.j.1 with ar := hAR1 }, E1.j.2⟩ + have hTr1 : TreeA st OS Z.G.chains Z.G.par (L.wr s (stA (L.curW s)) sched.TS_RESTART) := + C.hTr.keepO hos1 + have hCar1 := carryA_of_restartA C.hTr C.hsub hkct hos1 + refine ⟨.vcpu (.writeReg (specPtr vc) field.val wSpec), liftME_vcpu_okA hd, + stUpdA σ σ.curThread xct .restart, hst_spec, _, [], ?_, Or.inl ⟨Z, hE1_a1, hTr1, hCar1⟩⟩ + unfold fl4ArchPerformInvocation NonDetE.liftE + change (Except.ok [], _) ∈ ((fl4PerformVcpuInv (.writeReg (specPtr vc) field.val wSpec) >>= _) _).1 + rw [nd_bind_oneSc hperf]; exact Set.mem_singleton _ + · rw [if_neg hap] + have hdec : (decide (g = 3#u64)) = fl4GicV3 := hgb' + simpa [hdec] using htail (by simp [hap]) + +private theorem vcpu_inject_lr_eqA (virq priority group : U64) (g : U64) + (hgb : decide (g = 3#u64) = fl4GicV3) : + (if g = 3#u64 then do + let i4 ← 1#u64 <<< 62#i32 + let i5 ← lift (group &&& 1#u64) + let i6 ← i5 <<< 60#i32 + let i7 ← lift (i4 ||| i6) + let i8 ← lift (priority &&& 255#u64) + let i9 ← i8 <<< 48#i32 + let i10 ← lift (i7 ||| i9) + let i11 ← 1#u64 <<< 41#i32 + let i12 ← lift (i10 ||| i11) + ok (i12 ||| virq) + else do + let i4 ← lift (group &&& 1#u64) + let i5 ← i4 <<< 30#i32 + let i6 ← 1#u64 <<< 28#i32 + let i7 ← lift (i5 ||| i6) + let i8 ← lift (priority &&& 31#u64) + let i9 ← i8 <<< 23#i32 + let i10 ← lift (i7 ||| i9) + let i11 ← 1#u64 <<< 19#i32 + let i12 ← lift (i10 ||| i11) + let i13 ← lift (virq &&& 1023#u64) + ok (i12 ||| i13)) = ok ⟨fl4InjectLr virq.bv priority.bv group.bv fl4GicV3⟩ := by + unfold fl4InjectLr + have hshl : ∀ (x : U64) (k : I32) (n : Nat) (hk : 0 ≤ k.val ∧ k.val < 64) (hn : k.val.toNat = n), + x <<< k = ok ⟨x.bv <<< n⟩ := + fun _ _ _ hk hn => by rw [u64_shl_i32_ok hk.1 hk.2, hn] + by_cases hg3 : g = 3#u64 + · have hfl : fl4GicV3 = true := by rw [← hgb, decide_eq_true hg3] + rw [if_pos hg3, if_pos hfl, + hshl 1#u64 62#i32 62 (by decide) rfl, bind_ok, + lift_ok, bind_ok, + hshl _ 60#i32 60 (by decide) rfl, bind_ok, + lift_ok, bind_ok, + lift_ok, bind_ok, + hshl _ 48#i32 48 (by decide) rfl, bind_ok, + lift_ok, bind_ok, + hshl 1#u64 41#i32 41 (by decide) rfl, bind_ok, + lift_ok, bind_ok] + exact congrArg ok (u64_eq_of_bv_eq rfl) + · have hfl : fl4GicV3 = false := by rw [← hgb, decide_eq_false hg3] + rw [if_neg hg3, if_neg (by simp [hfl]), + lift_ok, bind_ok, + hshl _ 30#i32 30 (by decide) rfl, bind_ok, + hshl 1#u64 28#i32 28 (by decide) rfl, bind_ok, + lift_ok, bind_ok, + lift_ok, bind_ok, + hshl _ 23#i32 23 (by decide) rfl, bind_ok, + lift_ok, bind_ok, + hshl 1#u64 19#i32 19 (by decide) rfl, bind_ok, + lift_ok, bind_ok, + lift_ok, bind_ok] + exact congrArg ok (u64_eq_of_bv_eq rfl) + +/-- **Label 61 (`ARMVCPUInjectIRQ`)** on `VcpuTopPostA`, including the post-`stall_vcpu` +`DeleteFirst` error branch (`AR a1 Z.hw`). -/ +theorem vcpu_inject_irq_jA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} + {OS U Ac Pp Tb Ub : Set U64} {Lv : U64 → Nat} {ia ie : Nat} + {s : S} {a : A} {σ : State Unit} {m : Fl4SpecA64.McState} {vc : U64} + (hP : VcpuPlatAgreeA ar) (hAK : VcpuArchKeepsA ar AR) + (C : VcpuDecCtxA (st := st) (ar := ar) AR Z H OS U Ac Pp Tb Ub Lv ia ie s a σ m vc) + {Rp Nt : Set U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} {slot : U64} + (Pl : SysPlace st H Z.Cs H.frame Rp Nt OS s extra slot) (Y : SymL st H Z.Cs s) + (hrun : Runnable (L.mem s (stA (L.curW s)))) + (hcur : (L.mem s (inqA (L.curW s)) &&& sched.Q_RELEASE) = 0#u64 ∧ L.mem s (scA (L.curW s)) ≠ 0#u64 ∧ + L.mem s (scA (L.curW s)) ∈ Ac) + (hvs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → Aligned b ∧ b ≠ stA (L.curW s)) + (length buf : U64) (call : Bool) (args : List U64) (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) : + vcpu.decode_inject_irq st ar s a vc length buf ⦃ res => + VcpuTopPostA (L := L) AR Z OS m call σ + ((NonDetE.liftME Fl4ArchInvocation.vcpu <| + decodeFl4VcpuInjectIrq (Z := Unit) (specPtr vc) (args.map UScalar.bv)).run σ) s res ⦄ := by + have hAR : AR a Z.hw := C.E.j.1.ar + unfold vcpu.decode_inject_irq + by_cases hlen1 : length < 1#u64 + · rw [if_pos hlen1] + unfold error.truncated; rw [bind_ok, WP.spec_ok] + have hlt : (args.map UScalar.bv).length < 1 := by rw [List.length_map, ← hlen]; exact hlen1 + have hd : (decodeFl4VcpuInjectIrq (Z := Unit) (specPtr vc) (args.map UScalar.bv)).run σ = + ({(.error .truncatedMessage, σ)}, False) := by + unfold decodeFl4VcpuInjectIrq NonDetE.whenE; rw [if_pos hlt]; exact ndE_throw_bind _ _ _ + exact ⟨rfl, hAR, .truncatedMessage, liftME_vcpu_errA hd, Or.inl trivial⟩ + · rw [if_neg hlen1] + have hge1 : 1 ≤ args.length := by have : ¬ length.val < 1 := hlen1; omega + have hct : sched.cur_thread st s = ok (L.curW s) := eq_of_spec_fd (sched.cur_thread.spec s) + rw [hct, bind_ok] + cases hargs_eq : args with + | nil => simp [hargs_eq] at hge1 + | cons mr0 rest => + have hshr : ∀ (x : U64) (k : I32) (n : Nat) (hk : 0 ≤ k.val ∧ k.val < 64) (hn : k.val.toNat = n), + x >>> k = ok ⟨x.bv >>> n⟩ := + fun _ _ _ hk hn => by rw [u64_shr_i32_ok hk.1 hk.2, hn] + rw [hargs 0#u64 mr0 (by simp [hargs_eq]), bind_ok, + lift_ok, bind_ok, + hshr mr0 16#i32 16 (by decide) rfl, bind_ok, + lift_ok, bind_ok, + hshr mr0 24#i32 24 (by decide) rfl, bind_ok, + lift_ok, bind_ok, + hshr mr0 32#i32 32 (by decide) rfl, bind_ok, + lift_ok, bind_ok] + rw [vcpu_num_lrs_eqA hP a, bind_ok] + obtain ⟨g, hga, hgb⟩ := hP.gic a + rw [hga, bind_ok] + set virq : U64 := mr0 &&& 65535#u64 + set priority : U64 := ⟨mr0.bv >>> 16⟩ &&& 255#u64 + set group : U64 := ⟨mr0.bv >>> 24⟩ &&& 255#u64 + set index : U64 := ⟨mr0.bv >>> 32⟩ &&& 255#u64 + have hlrs_val : (⟨BitVec.ofNat 64 fl4NumLrs⟩ : U64).val = fl4NumLrs := by + change (BitVec.ofNat 64 fl4NumLrs).toNat = fl4NumLrs + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by have := fl4NumLrs_le_16; omega) + by_cases hidx : index ≥ ⟨BitVec.ofNat 64 fl4NumLrs⟩ + · rw [if_pos hidx, lift_ok, bind_ok] + unfold error.range; rw [bind_ok, WP.spec_ok] + have hidx' : index.val ≥ fl4NumLrs := by + have : (⟨BitVec.ofNat 64 fl4NumLrs⟩ : U64).val ≤ index.val := hidx; omega + have hd : (decodeFl4VcpuInjectIrq (Z := Unit) (specPtr vc) + ((mr0 :: rest).map UScalar.bv)).run σ = + ({(.error (.rangeError 0 (BitVec.ofNat 64 (fl4NumLrs - 1))), σ)}, False) := by + unfold decodeFl4VcpuInjectIrq + rw [ndE_whenE_false (by simp)] + have hcond : ((mr0.bv >>> 32) &&& 0xff).toNat ≥ fl4NumLrs := hidx' + exact ndE_whenE_true (by simpa [the] using hcond) _ _ _ + have hsat : (core.num.U64.saturating_sub ⟨BitVec.ofNat 64 fl4NumLrs⟩ 1#u64).bv = + BitVec.ofNat 64 (fl4NumLrs - 1) := by + apply BitVec.eq_of_toNat_eq + change (core.num.U64.saturating_sub ⟨BitVec.ofNat 64 fl4NumLrs⟩ 1#u64).val = _ + rw [P0.saturating_sub_u64_val, hlrs_val] + change fl4NumLrs - 1 = (fl4NumLrs - 1) % 2 ^ 64 + exact (Nat.mod_eq_of_lt (by have := fl4NumLrs_le_16; omega)).symm + exact ⟨rfl, hAR, .rangeError 0 (BitVec.ofNat 64 (fl4NumLrs - 1)), liftME_vcpu_errA hd, + Or.inl ⟨rfl, hsat.symm⟩⟩ + · rw [if_neg hidx] + have hidx_lt : index.val < fl4NumLrs := by + have : ¬ (⟨BitVec.ofNat 64 fl4NumLrs⟩ : U64).val ≤ index.val := hidx; omega + by_cases hgrp : group > 1#u64 + · rw [if_pos hgrp] + unfold error.range; rw [bind_ok, WP.spec_ok] + have hgrp' : group.bv > 1 := hgrp + have hd : (decodeFl4VcpuInjectIrq (Z := Unit) (specPtr vc) + ((mr0 :: rest).map UScalar.bv)).run σ = + ({(.error (.rangeError 0 1), σ)}, False) := by + unfold decodeFl4VcpuInjectIrq + have hni : ¬ (((mr0.bv >>> 32) &&& 0xff).toNat ≥ fl4NumLrs) := by + change ¬ (index.val ≥ fl4NumLrs); omega + rw [ndE_whenE_false (by simp), ndE_whenE_false (by simpa [the] using hni)] + exact ndE_whenE_true (by simpa [the, group] using hgrp') _ _ _ + exact ⟨rfl, hAR, .rangeError 0 1, liftME_vcpu_errA hd, Or.inl ⟨rfl, rfl⟩⟩ + · rw [if_neg hgrp] + have hgrp_ok : ¬ (group.bv > 1) := hgrp + -- Shared valid-virq inject body + have hinject_body : ¬ (virq.bv > 1019 ∧ ¬ (fl4GicV3 = true ∧ virq.bv ≥ 8192)) → + (do + let (s1, a1) ← vcpu.stall_vcpu st ar s a vc + let lr_reg ← lift (core.num.U64.wrapping_add fl4_abi.consts.vcpu_reg.VGIC_LR0 index) + let cur ← vcpu.reg_get st s1 vc lr_reg + let b ← vcpu.lr_is_empty cur (g = 3#u64) + if b then do + let lr ← + if g = 3#u64 then do + let i4 ← 1#u64 <<< 62#i32 + let i5 ← lift (group &&& 1#u64) + let i6 ← i5 <<< 60#i32 + let i7 ← lift (i4 ||| i6) + let i8 ← lift (priority &&& 255#u64) + let i9 ← i8 <<< 48#i32 + let i10 ← lift (i7 ||| i9) + let i11 ← 1#u64 <<< 41#i32 + let i12 ← lift (i10 ||| i11) + ok (i12 ||| virq) + else do + let i4 ← lift (group &&& 1#u64) + let i5 ← i4 <<< 30#i32 + let i6 ← 1#u64 <<< 28#i32 + let i7 ← lift (i5 ||| i6) + let i8 ← lift (priority &&& 31#u64) + let i9 ← i8 <<< 23#i32 + let i10 ← lift (i7 ||| i9) + let i11 ← 1#u64 <<< 19#i32 + let i12 ← lift (i10 ||| i11) + let i13 ← lift (virq &&& 1023#u64) + ok (i12 ||| i13) + let s2 ← sched.set_thread_state st s1 (L.curW s) sched.TS_RESTART + let s3 ← vcpu.reg_set st s2 vc lr_reg lr + let s4 ← vcpu.sync_vgic_status st s3 vc ⟨BitVec.ofNat 64 fl4NumLrs⟩ (g = 3#u64) + ok (core.result.Result.Ok (), s4, a1) + else + ok (core.result.Result.Err (error.Exc.Sys error.SysError.DeleteFirst), s1, a1)) ⦃ res => + VcpuTopPostA (L := L) AR Z OS m call σ + ((NonDetE.liftME Fl4ArchInvocation.vcpu <| + decodeFl4VcpuInjectIrq (Z := Unit) (specPtr vc) ((mr0 :: rest).map UScalar.bv)).run σ) s res ⦄ := by + intro hvirq_ok + refine WP.spec_bind (stall_vcpu_localA hAK hAR vc C.hStallT C.hStallC) ?_ + rintro ⟨s1, a1⟩ ⟨hs1, hAR1⟩ + dsimp only at hs1; subst s1 + change (do + let lr_reg ← lift (core.num.U64.wrapping_add fl4_abi.consts.vcpu_reg.VGIC_LR0 index) + let cur ← vcpu.reg_get st s vc lr_reg + let b ← vcpu.lr_is_empty cur (g = 3#u64) + if b then do + let lr ← + if g = 3#u64 then do + let i4 ← 1#u64 <<< 62#i32 + let i5 ← lift (group &&& 1#u64) + let i6 ← i5 <<< 60#i32 + let i7 ← lift (i4 ||| i6) + let i8 ← lift (priority &&& 255#u64) + let i9 ← i8 <<< 48#i32 + let i10 ← lift (i7 ||| i9) + let i11 ← 1#u64 <<< 41#i32 + let i12 ← lift (i10 ||| i11) + ok (i12 ||| virq) + else do + let i4 ← lift (group &&& 1#u64) + let i5 ← i4 <<< 30#i32 + let i6 ← 1#u64 <<< 28#i32 + let i7 ← lift (i5 ||| i6) + let i8 ← lift (priority &&& 31#u64) + let i9 ← i8 <<< 23#i32 + let i10 ← lift (i7 ||| i9) + let i11 ← 1#u64 <<< 19#i32 + let i12 ← lift (i10 ||| i11) + let i13 ← lift (virq &&& 1023#u64) + ok (i12 ||| i13) + let s2 ← sched.set_thread_state st s (L.curW s) sched.TS_RESTART + let s3 ← vcpu.reg_set st s2 vc lr_reg lr + let s4 ← vcpu.sync_vgic_status st s3 vc ⟨BitVec.ofNat 64 fl4NumLrs⟩ (g = 3#u64) + ok (core.result.Result.Ok (), s4, a1) + else + ok (core.result.Result.Err (error.Exc.Sys error.SysError.DeleteFirst), s, a1)) ⦃ _ ⦄ + rw [lift_ok, bind_ok] + have hlrs16 := fl4NumLrs_le_16 + have hlr_reg_val : (wa fl4_abi.consts.vcpu_reg.VGIC_LR0 index).val = vrVgicLr0 + index.val := by + rw [wa_valP] + have h48 : (fl4_abi.consts.vcpu_reg.VGIC_LR0).val = 48 := by + unfold fl4_abi.consts.vcpu_reg.VGIC_LR0; rfl + rw [h48]; unfold vrVgicLr0; exact Nat.mod_eq_of_lt (by omega) + have hlr_lt64 : (wa fl4_abi.consts.vcpu_reg.VGIC_LR0 index).val < 64 := by + rw [hlr_reg_val]; unfold vrVgicLr0; omega + have hlr_eq : wa fl4_abi.consts.vcpu_reg.VGIC_LR0 index = ⟨BitVec.ofNat 64 (vrVgicLr0 + index.val)⟩ := by + apply UScalar.eq_of_val_eq + rw [hlr_reg_val] + change _ = (BitVec.ofNat 64 _).toNat + rw [BitVec.toNat_ofNat]; unfold vrVgicLr0; exact (Nat.mod_eq_of_lt (by omega)).symm + rw [hlr_eq, vcpu_reg_get_eqA s vc (by unfold vrVgicLr0; omega), bind_ok] + have hdec : (decide (g = 3#u64)) = fl4GicV3 := hgb + rw [hdec, vcpu_lr_is_empty_eqA _ fl4GicV3, bind_ok] + obtain ⟨v, hkv, -, -⟩ := C.hV.core.obj vc C.hvc + have hcur_bv : (L.mem s (vcpuRegWA vc (vrVgicLr0 + index.val))).bv = + fl4VcpuRegGet v (vrVgicLr0 + index.val) := + C.hV.regs vc C.hvc v hkv (vrVgicLr0 + index.val) (by unfold vrVgicLr0; omega) + rw [hcur_bv] + have hni : ¬ (((mr0.bv >>> 32) &&& 0xff).toNat ≥ fl4NumLrs) := by + change ¬ (index.val ≥ fl4NumLrs); omega + by_cases hemp : lrIsEmpty (fl4VcpuRegGet v (vrVgicLr0 + index.val)) fl4GicV3 = true + · rw [if_pos hemp, vcpu_inject_lr_eqA virq priority group g hgb, bind_ok] + have hcurT : L.curW s ∈ Z.T := C.E.j.1.curT (L.cpu s) C.E.j.1.sch.cpuCs + have hvc_st := vc_ne_stA C.hV.place hcurT + obtain ⟨xct, hkct, hst_ok, hst_spec, E1, -, hos1, -, -⟩ := + C.E.restartA Pl Y hrun hcur hvs hvc_st + rw [hst_ok, bind_ok] + have hne_vc_ct : specPtr vc ≠ σ.curThread := fun h => by rw [h, hkct] at hkv; cases hkv + have hkv1 : (stUpdA σ σ.curThread xct .restart).kheap (specPtr vc) = some (.archObj (.vcpu v)) := by + unfold stUpdA setObjA; simp [Function.update, hne_vc_ct, hkv] + have hd : (decodeFl4VcpuInjectIrq (Z := Unit) (specPtr vc) + ((mr0 :: rest).map UScalar.bv)).run σ = + ({(.ok (.injectIrq (specPtr vc) index.val + (fl4InjectLr virq.bv priority.bv group.bv fl4GicV3)), σ)}, False) := by + unfold decodeFl4VcpuInjectIrq + rw [ndE_whenE_false (by simp), ndE_whenE_false (by simpa [the] using hni), + ndE_whenE_false (by simpa [the, group] using hgrp_ok), + ndE_whenE_false (by simpa [the, virq] using hvirq_ok)] + have hidx_eq : ((the ((mr0 :: rest).map UScalar.bv)[0]? >>> 32) &&& 0xff).toNat = index.val := rfl + rw [ndE_liftE_bind _ _ _ _ () (fl4VcpuFlush_noneA C.hV.arch (specPtr vc)), + hidx_eq, + ndE_liftE_bind _ _ _ _ (fl4VcpuRegGet v (vrVgicLr0 + index.val)) + (fl4VcpuGet_eqA (vrVgicLr0 + index.val) hkv)] + exact ndE_unlessE_true hemp _ _ _ + have hperf := fl4PerformVcpuInv_injectIrq_eqA index.val + (fl4InjectLr virq.bv priority.bv group.bv fl4GicV3) hkv1 + have hstep := vcpu_reg_sync_frameA C hAR1 hkct hkv E1.j hos1 + (wa fl4_abi.consts.vcpu_reg.VGIC_LR0 index) hlr_lt64 + ⟨fl4InjectLr virq.bv priority.bv group.bv fl4GicV3⟩ + (by intro h; rw [hlr_reg_val] at h; unfold vrVgicLr0 vrVgicHcr at h; omega) true + simp only [↓reduceIte] at hstep + rw [hlr_reg_val, hlr_eq] at hstep + refine spec_bind_assoc hstep ?_ + rintro s4 ⟨hJ4, hTr4, hCar4⟩ + rw [WP.spec_ok] + refine ⟨.vcpu (.injectIrq (specPtr vc) index.val + (fl4InjectLr virq.bv priority.bv group.bv fl4GicV3)), liftME_vcpu_okA hd, + stUpdA σ σ.curThread xct .restart, hst_spec, _, [], ?_, Or.inl ⟨Z, hJ4, hTr4, hCar4⟩⟩ + unfold fl4ArchPerformInvocation NonDetE.liftE + change (Except.ok [], _) ∈ ((fl4PerformVcpuInv _ >>= _) _).1 + rw [nd_bind_oneSc hperf]; exact Set.mem_singleton _ + · rw [if_neg hemp, WP.spec_ok] + have hd : (decodeFl4VcpuInjectIrq (Z := Unit) (specPtr vc) + ((mr0 :: rest).map UScalar.bv)).run σ = + ({(.error .deleteFirst, σ)}, False) := by + unfold decodeFl4VcpuInjectIrq + rw [ndE_whenE_false (by simp), ndE_whenE_false (by simpa [the] using hni), + ndE_whenE_false (by simpa [the, group] using hgrp_ok), + ndE_whenE_false (by simpa [the, virq] using hvirq_ok)] + have hidx_eq : ((the ((mr0 :: rest).map UScalar.bv)[0]? >>> 32) &&& 0xff).toNat = index.val := rfl + rw [ndE_liftE_bind _ _ _ _ () (fl4VcpuFlush_noneA C.hV.arch (specPtr vc)), + hidx_eq, + ndE_liftE_bind _ _ _ _ (fl4VcpuRegGet v (vrVgicLr0 + index.val)) + (fl4VcpuGet_eqA (vrVgicLr0 + index.val) hkv)] + exact ndE_unlessE_false hemp _ _ _ + exact ⟨rfl, hAR1, .deleteFirst, liftME_vcpu_errA hd, Or.inl trivial⟩ + by_cases hgt : virq > 1019#u64 + · rw [if_pos hgt] + have hgt' : virq.bv > 1019 := hgt + by_cases hg3 : g = 3#u64 + · have hfl : fl4GicV3 = true := by unfold fl4GicV3; rw [← hgb, decide_eq_true hg3] + rw [if_pos hg3] + by_cases hlpi : virq ≥ 8192#u64 + · rw [if_pos hlpi] + exact hinject_body (fun h => h.2 ⟨hfl, hlpi⟩) + · rw [if_neg hlpi] + unfold error.range; rw [bind_ok, WP.spec_ok] + have hbad : virq.bv > 1019 ∧ ¬ (fl4GicV3 = true ∧ virq.bv ≥ 8192) := + ⟨hgt', fun h => hlpi h.2⟩ + have hd : (decodeFl4VcpuInjectIrq (Z := Unit) (specPtr vc) + ((mr0 :: rest).map UScalar.bv)).run σ = + ({(.error (.rangeError 0 1019), σ)}, False) := by + unfold decodeFl4VcpuInjectIrq + have hni : ¬ (((mr0.bv >>> 32) &&& 0xff).toNat ≥ fl4NumLrs) := by + change ¬ (index.val ≥ fl4NumLrs); omega + rw [ndE_whenE_false (by simp), ndE_whenE_false (by simpa [the] using hni), + ndE_whenE_false (by simpa [the, group] using hgrp_ok)] + exact ndE_whenE_true (by simpa [the, virq] using hbad) _ _ _ + exact ⟨rfl, hAR, .rangeError 0 1019, liftME_vcpu_errA hd, Or.inl ⟨rfl, rfl⟩⟩ + · have hfl : fl4GicV3 = false := by unfold fl4GicV3; rw [← hgb, decide_eq_false hg3] + rw [if_neg hg3] + unfold error.range; rw [bind_ok, WP.spec_ok] + have hbad : virq.bv > 1019 ∧ ¬ (fl4GicV3 = true ∧ virq.bv ≥ 8192) := + ⟨hgt', fun h => by rw [hfl] at h; cases h.1⟩ + have hd : (decodeFl4VcpuInjectIrq (Z := Unit) (specPtr vc) + ((mr0 :: rest).map UScalar.bv)).run σ = + ({(.error (.rangeError 0 1019), σ)}, False) := by + unfold decodeFl4VcpuInjectIrq + have hni : ¬ (((mr0.bv >>> 32) &&& 0xff).toNat ≥ fl4NumLrs) := by + change ¬ (index.val ≥ fl4NumLrs); omega + rw [ndE_whenE_false (by simp), ndE_whenE_false (by simpa [the] using hni), + ndE_whenE_false (by simpa [the, group] using hgrp_ok)] + exact ndE_whenE_true (by simpa [the, virq] using hbad) _ _ _ + exact ⟨rfl, hAR, .rangeError 0 1019, liftME_vcpu_errA hd, Or.inl ⟨rfl, rfl⟩⟩ + · rw [if_neg hgt] + exact hinject_body (fun h => hgt h.1) + +end labels + +end + +end Fl4ProofsA64.P3.Vcpu diff --git a/proofs/Fl4ProofsA64/P3/VcpuRelA.lean b/proofs/Fl4ProofsA64/P3/VcpuRelA.lean new file mode 100644 index 000000000..673b05d1b --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/VcpuRelA.lean @@ -0,0 +1,896 @@ +/- +P3 (aarch64, A64-P3 S4 W1): **VCPU full state relation (`VcpuRelA`), placement (`VcPage`, `VcPlacedA`), +platform premises (`VcpuPlatAgreeA`, `VcpuArchKeepsA`), top postcondition (`VcpuTopPostA`), +concrete VCPU helper specs, framing lemmas, and local-core `TS_RUNNING_VM` notification signal +companion (`send_signal_idle_bound_vm_localA`)**. + +Named premises defined here (contract §4.3–§4.4): +- `[P] VcpuPlatAgreeA ar`: hardware `vcpu_midr`, `vcpu_vgic_vtr`, and `vcpu_gic_version` agree + with `Fl4SpecA64.fl4A64Platform` and `Fl4SpecA64.config_ARM_GIC_V3`. +- `[P] VcpuArchKeepsA ar AR`: `vcpu_flush`, `vcpu_flush_state`, `fpu_flush`, `fpu_release`, and + `mask_irq` preserve `AR · h`. +- `[S]` local-core affinity hypotheses on `remote_tcb_stall` / `stall_vcpu` / `kick_guest`. +-/ +import Fl4ProofsA64.P3.VcpuSpecA +import Fl4ProofsA64.P3.VcpuPremisesA +import Fl4ProofsA64.P3.FrReplyA +import Fl4ProofsA64.P3.ScArchSwA +import Fl4ProofsA64.P3.NtfnSendA + +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3.Vcpu + +open Fl4Spec Fl4SpecA64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3 Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Notif + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## VCPU word addresses, 4 KiB page placement, and `VcpuRelA` -/ + +/-- Byte offset of VCPU register `i` (`0x40 + 8 * i`). -/ +def vcpuRegOffA (i : Nat) : U64 := ⟨BitVec.ofNat 64 (64 + 8 * i)⟩ + +/-- Word address of VCPU register `i` inside VCPU object `v`. -/ +def vcpuRegWA (v : U64) (i : Nat) : U64 := wa v (vcpuRegOffA i) + +/-- Every 8-byte-aligned word inside any 4 KiB VCPU page in `V`. -/ +def VcPage (V : Set U64) (b : U64) : Prop := + ∃ v ∈ V, ∃ o : U64, o.val < 4096 ∧ o.val % 8 = 0 ∧ b = wa v o + +/-- Full 4 KiB VCPU page alignment and disjointness from all other kernel/user regions +(contract §4.1 field 7). -/ +structure VcPlacedA (Z : XSetsA) (OS Ub : Set U64) (ksb : U64) : Prop where + al : ∀ v ∈ Z.V, v ≠ 0#u64 ∧ v.val % 4096 = 0 ∧ v.val + 4096 ≤ 2 ^ 64 + al_t : ∀ t ∈ Z.T, Aligned t + al_os : ∀ y ∈ OS, Aligned y + al_slot : ∀ y, IsSlotA Z.th y → Aligned y + al_ksb : Aligned ksb + disj_os : ∀ b, VcPage Z.V b → ∀ y ∈ OS, + b ≠ y ∧ b ≠ wa y 8#u64 ∧ b ≠ wa y 16#u64 ∧ b ≠ wa y 24#u64 + disj_slots : ∀ b, VcPage Z.V b → ∀ y, IsSlotA Z.th y → b ≠ y ∧ b ≠ wa y 8#u64 + disj_tcb : ∀ v ∈ Z.V, ∀ t ∈ Z.T, ∀ ov ot : U64, ov.val < 4096 → ot.val < 4096 → wa v ov ≠ wa t ot + disj_sc : ∀ v ∈ Z.V, ∀ c ∈ Z.C, ∀ ov oc : U64, ov.val < 4096 → oc.val < 128 → wa v ov ≠ wa c oc + disj_ks : ∀ b, VcPage Z.V b → ¬ (ksb.val ≤ b.val ∧ b.val < ksb.val + ksSizeA) + disj_vs : ∀ b, VcPage Z.V b → ¬ VsFootA Z.Pp Z.S1 Z.Y ksb b + disj_ub : ∀ b, VcPage Z.V b → b ∉ Ub + +theorem aligned_wa {a : U64} (ha : Aligned a) (o : U64) (ho : o.val % 8 = 0) : Aligned (wa a o) := by + change (wa a o).val % 8 = 0 + rw [wa_valP] + have h8 : (a.val + o.val) % 8 = 0 := by change a.val % 8 = 0 at ha; omega + have hm : (a.val + o.val) % 2 ^ 64 % 8 = (a.val + o.val) % 8 := + Nat.mod_mod_of_dvd _ (by decide : 8 ∣ 2 ^ 64) + omega + +theorem stA_eq_wa (t : U64) : stA t = wa t 3328#u64 := by + unfold stA layout.tcb.STATE layout.tcb.F; rfl + +theorem u64_eq_of_bv_eq {a b : U64} (h : a.bv = b.bv) : a = b := by + cases a; cases b; cases h; rfl + +theorem u64_add_ok {x y : U64} (hlt : x.val + y.val < 2 ^ 64) : + x + y = ok ⟨BitVec.ofNat 64 (x.val + y.val)⟩ := by + apply eq_of_spec_fd + refine WP.spec_mono (U64.add_spec (x := x) (y := y) + (by simp [U64.max, U64.numBits, UScalar.size, UScalarTy.numBits]; omega)) ?_ + intro r hr + apply UScalar.eq_of_val_eq + rw [hr] + change x.val + y.val = (BitVec.ofNat 64 (x.val + y.val)).toNat + rw [BitVec.toNat_ofNat] + exact (Nat.mod_eq_of_lt hlt).symm + +theorem u64_shl_ok {x y : U64} (hy : y.val < 64) : + (x <<< y) = ok ⟨x.bv <<< y.val⟩ := by + apply eq_of_spec_fd + refine WP.spec_mono (U64.ShiftLeft_spec (x := x) (y := y) hy) ?_ + intro r hr + exact u64_eq_of_bv_eq hr.2 + +theorem u64_shl_i32_ok {x : U64} {y : I32} (hy0 : 0 ≤ y.val) (hy64 : y.val < 64) : + (x <<< y) = ok ⟨x.bv <<< y.val.toNat⟩ := by + apply eq_of_spec_fd + refine WP.spec_mono (U64.ShiftLeft_IScalar_spec (x := x) (y := y) hy0 hy64) ?_ + intro r hr + exact u64_eq_of_bv_eq hr.2 + +theorem u64_shr_i32_ok {x : U64} {y : I32} (hy0 : 0 ≤ y.val) (hy64 : y.val < 64) : + (x >>> y) = ok ⟨x.bv >>> y.val.toNat⟩ := by + apply eq_of_spec_fd + refine WP.spec_mono (U64.ShiftRight_IScalar_spec (x := x) (y := y) hy0 hy64) ?_ + intro r hr + exact u64_eq_of_bv_eq hr.2 + +theorem mem_wr_neA {S : Type} {st : machine.Store S} [L : LawfulStore st] + {s : S} {a v b : U64} (ha : Aligned a) (hb : Aligned b) (hne : a ≠ b) : + L.mem (L.wr s a v) b = L.mem s b := + L.mem_wr_ne ha hb (fun h => hne (UScalar.eq_of_val_eq h)) + +theorem VcPlacedA.vc_al {Z : XSetsA} {OS Ub : Set U64} {ksb : U64} + (hP : VcPlacedA Z OS Ub ksb) {v : U64} (hv : v ∈ Z.V) : Aligned v := by + obtain ⟨_, h4k, _⟩ := hP.al v hv + change v.val % 8 = 0; omega + +theorem VcPlacedA.vcPage_al {Z : XSetsA} {OS Ub : Set U64} {ksb : U64} + (hP : VcPlacedA Z OS Ub ksb) {b : U64} (hb : VcPage Z.V b) : Aligned b := by + rcases hb with ⟨v, hv, o, _, ho8, rfl⟩ + exact aligned_wa (hP.vc_al hv) o ho8 + +theorem VcPlacedA.node_al {S : Type} {st : machine.Store S} [L : LawfulStore st] + {Z : XSetsA} {OS Ub : Set U64} {s : S} + (hP : VcPlacedA Z OS Ub (L.ksb s)) (c o : U64) (ho : o.val % 8 = 0) : + Aligned (L.nodeAt s c o) := by + have hks8 : (L.ksb s).val % 8 = 0 := hP.al_ksb + change (L.nodeAt s c o).val % 8 = 0 + simp only [LawfulStore.nodeAt, core.num.U64.wrapping_add_val_eq, core.num.U64.wrapping_mul_val_eq, + U64.size_def, U64.numBits, UScalar.size, UScalarTy.numBits] + have h223 : (223488#u64 : U64).val = 223488 := rfl + have h4288 : (4288#u64 : U64).val = 4288 := rfl + rw [h223, h4288] + have hm : ∀ x : Nat, x % 2 ^ 64 % 8 = x % 8 := + fun x => Nat.mod_mod_of_dvd x (by decide : 8 ∣ 2 ^ 64) + rw [hm] + have h1 : (((L.ksb s).val + 223488) % 2 ^ 64) % 8 = 0 := by rw [hm]; omega + have h2 : ((c.val * 4288) % 2 ^ 64) % 8 = 0 := by rw [hm]; omega + have h3 : ((((L.ksb s).val + 223488) % 2 ^ 64 + (c.val * 4288) % 2 ^ 64) % 2 ^ 64) % 8 = 0 := by + rw [hm]; omega + omega + +/-- Distinct VCPUs in `Z.V` have disjoint 4 KiB pages. -/ +theorem VcPlacedA.disj_vcpu {Z : XSetsA} {OS Ub : Set U64} {ksb : U64} + (hP : VcPlacedA Z OS Ub ksb) {v₁ v₂ ov₁ ov₂ : U64} + (hv₁ : v₁ ∈ Z.V) (hv₂ : v₂ ∈ Z.V) (hne : v₁ ≠ v₂) + (ho₁ : ov₁.val < 4096) (ho₂ : ov₂.val < 4096) : + wa v₁ ov₁ ≠ wa v₂ ov₂ := by + intro heq + obtain ⟨_, hal₁, hfit₁⟩ := hP.al v₁ hv₁ + obtain ⟨_, hal₂, hfit₂⟩ := hP.al v₂ hv₂ + have h1 : (wa v₁ ov₁).val = v₁.val + ov₁.val := by + rw [wa_valP]; exact Nat.mod_eq_of_lt (by omega) + have h2 : (wa v₂ ov₂).val = v₂.val + ov₂.val := by + rw [wa_valP]; exact Nat.mod_eq_of_lt (by omega) + have hval := congrArg UScalar.val heq + rw [h1, h2] at hval + have hvEq : v₁.val = v₂.val := by omega + exact hne (UScalar.eq_of_val_eq hvEq) + +/-- Distinct offsets within a single VCPU page give distinct word addresses. -/ +theorem VcPlacedA.same_vcpu_ne {Z : XSetsA} {OS Ub : Set U64} {ksb : U64} + (hP : VcPlacedA Z OS Ub ksb) {v o₁ o₂ : U64} (hv : v ∈ Z.V) + (ho₁ : o₁.val < 4096) (ho₂ : o₂.val < 4096) (hne : o₁ ≠ o₂) : + wa v o₁ ≠ wa v o₂ := by + intro heq + obtain ⟨_, _, hfit⟩ := hP.al v hv + have h1 : (wa v o₁).val = v.val + o₁.val := by + rw [wa_valP]; exact Nat.mod_eq_of_lt (by omega) + have h2 : (wa v o₂).val = v.val + o₂.val := by + rw [wa_valP]; exact Nat.mod_eq_of_lt (by omega) + have hval := congrArg UScalar.val heq + rw [h1, h2] at hval + exact hne (UScalar.eq_of_val_eq (by omega)) + +theorem vcpuRegOffA_val {i : Nat} (hi : i < 64) : (vcpuRegOffA i).val = 64 + 8 * i := by + change (BitVec.ofNat 64 (64 + 8 * i)).toNat = 64 + 8 * i + rw [BitVec.toNat_ofNat] + exact Nat.mod_eq_of_lt (by omega) + +theorem vcpuRegWA_in_vcPage {V : Set U64} {v : U64} (hv : v ∈ V) {i : Nat} (hi : i < 64) : + VcPage V (vcpuRegWA v i) := by + refine ⟨v, hv, vcpuRegOffA i, ?_, ?_, rfl⟩ + · rw [vcpuRegOffA_val hi]; omega + · rw [vcpuRegOffA_val hi]; omega + +theorem vcpuHdr_in_vcPage {V : Set U64} {v o : U64} (hv : v ∈ V) (ho : o.val < 4096) + (hal : o.val % 8 = 0) : VcPage V (wa v o) := + ⟨v, hv, o, ho, hal, rfl⟩ + +section rel +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **S4 full 70-word VCPU relation `VcpuRelA`** (contract §4.1). Sits beside `VcpuCoreA` +without editing `JointA.lean`. -/ +structure VcpuRelA (Z : XSetsA) (OS Ub : Set U64) (s : S) (σ : State Unit) : Prop where + core : VcpuCoreA (L := L) Z s σ + owner : ∀ v ∈ Z.V, (L.mem s (wa v 8#u64)).val ≤ 128 + vppi : ∀ v ∈ Z.V, ∀ x, σ.kheap (specPtr v) = some (.archObj (.vcpu x)) → + L.mem s (wa v 16#u64) = (if x.vcpuVppiMasked .vppiEventIRQ_VTimer then 1#u64 else 0#u64) + runOff : ∀ v ∈ Z.V, ∀ x, σ.kheap (specPtr v) = some (.archObj (.vcpu x)) → + (L.mem s (wa v 32#u64)).bv = x.vcpuExt.runOffset + runOut : ∀ v ∈ Z.V, ∀ x, σ.kheap (specPtr v) = some (.archObj (.vcpu x)) → + (L.mem s (wa v 40#u64)).bv = x.vcpuExt.runOutMask ∧ + (x.vcpuExt.runOutMask &&& ~~~Fl4SpecA64.smAll = 0) + regs : ∀ v ∈ Z.V, ∀ x, σ.kheap (specPtr v) = some (.archObj (.vcpu x)) → + ∀ i < 64, (L.mem s (vcpuRegWA v i)).bv = Fl4SpecA64.fl4VcpuRegGet x i + bind_v2t : ∀ v ∈ Z.V, ∀ t : U64, L.mem s v = t → t ≠ 0#u64 → + t ∈ Z.T ∧ L.mem s (wa t 3664#u64) = v + bind_t2v : ∀ t ∈ Z.T, ∀ v : U64, L.mem s (wa t 3664#u64) = v → v ≠ 0#u64 → + v ∈ Z.V ∧ L.mem s v = t + run_v2c : ∀ v ∈ Z.V, ∀ c : U64, L.mem s (wa v 24#u64) = c → c ≠ 0#u64 → + c ∈ Z.T ∧ L.mem s (runVcpuA c) = v + run_c2v : ∀ c ∈ Z.T, ∀ v : U64, L.mem s (runVcpuA c) = v → v ≠ 0#u64 → + v ∈ Z.V ∧ L.mem s (wa v 24#u64) = c + run_state : ∀ c ∈ Z.T, L.mem s (runVcpuA c) ≠ 0#u64 → + L.mem s (stA c) = sched.TS_RUNNING_VM + bind_run_disj : ∀ t ∈ Z.T, L.mem s (wa t 3664#u64) ≠ 0#u64 → + L.mem s (runVcpuA t) = 0#u64 + idle_no_vcpu : ∀ c ∈ Z.Cs, + L.mem s (wa (L.mem s (L.nodeAt s c layout.node.IDLE_THREAD)) 3664#u64) = 0#u64 ∧ + L.mem s (runVcpuA (L.mem s (L.nodeAt s c layout.node.IDLE_THREAD))) = 0#u64 + /-- **D7 `VcpuLiveCapA`**: any bound or running VCPU in `Z.V` has a live `.Vcpu` cap in `OS`. -/ + live : ∀ v ∈ Z.V, (L.mem s v ≠ 0#u64 ∨ L.mem s (wa v 24#u64) ≠ 0#u64) → + ∃ y ∈ OS, capAtA (L.mem s) y = .Vcpu v + arch : σ.archState.armCurrentVcpu = none + place : VcPlacedA Z OS Ub (L.ksb s) + +end rel + +/-! ## Platform premises (`[P]`) and S4 top postcondition (`VcpuTopPostA`, D4) -/ + +-- `VcpuPlatAgreeA` and `VcpuArchKeepsA` are imported from `Fl4ProofsA64.P3.VcpuPremisesA`. + +section post +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-- **D4 `VcpuTopPostA`**: identical to `FrReplyA.ArchTopPostA` on `.Ok`, and relaxes `.Err`'s +`a' = a` to `AR a' Z.hw` so `ARMVCPUInjectIRQ`'s post-`stall_vcpu` `DeleteFirst` composes directly +with S3's `ArchTopPostA`. -/ +def VcpuTopPostA (AR : A → HwRootA → Prop) (Z : XSetsA) (OS : Set U64) (m : Fl4SpecA64.McState) + (calling : Bool) (σ : State Unit) + (D : Set (Except Fl4SpecA64.SyscallError Fl4SpecA64.Fl4ArchInvocation × State Unit) × Prop) + (s : S) : core.result.Result Unit error.Exc × S × A → Prop + | (.Err e, s', a') => s' = s ∧ AR a' Z.hw ∧ ∃ e', D = ({(.error e', σ)}, False) ∧ VsErrRelA e e' + | (.Ok (), s', a') => ∃ inv, ((.ok inv, σ) ∈ D.1 ∧ ¬ D.2) ∧ ∃ σ1, + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ∃ (σ2 : State Unit) (rv : List Fl4SpecA64.Data), + (.ok rv, σ2) ∈ ((Fl4SpecA64.fl4ArchPerformInvocation (Z := Unit) inv).run σ1).1 ∧ + ((∃ Z' : XSetsA, + JAtZAX (L := L) AR Z' s' a' σ2 m ∧ + TreeA st OS Z'.G.chains Z'.G.par s' ∧ + (CarryA Z (L.mem s) σ → CarryA Z' (L.mem s') σ2)) ∨ + (∃ σ3 : State Unit, + ((), σ3) ∈ ((do + Fl4Spec.NonDet.when calling (Fl4SpecA64.replyFromKernel (Z := Unit) σ.curThread (0, rv)) + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .running) σ2).1 ∧ + JAtZAX (L := L) AR Z s' a' σ3 m ∧ + TreeA st OS Z.G.chains Z.G.par s' ∧ + (CarryA Z (L.mem s) σ → CarryA Z (L.mem s') σ3))) + +/-- **D4 embedding**: `ArchTopPostA` embeds into `VcpuTopPostA` with zero edits to `FrReplyA`. -/ +theorem ArchTopPostA.to_vcpuTopA {AR : A → HwRootA → Prop} {Z : XSetsA} {OS : Set U64} + {m : Fl4SpecA64.McState} {calling : Bool} {σ : State Unit} + {D : Set (Except Fl4SpecA64.SyscallError Fl4SpecA64.Fl4ArchInvocation × State Unit) × Prop} + {s : S} {a : A} (hAR : AR a Z.hw) : + ∀ {res}, ArchTopPostA (L := L) AR Z OS m calling σ D s a res → + VcpuTopPostA (L := L) AR Z OS m calling σ D s res + | (.Err _, _, _), ⟨hs, ha, e', hD, hEr⟩ => ⟨hs, ha ▸ hAR, e', hD, hEr⟩ + | (.Ok (), _, _), h => h + +end post + +/-! ## Concrete VCPU helper specs -/ + +section helpers +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + +theorem vcpu_reg_addr_eqA (vc : U64) {i : Nat} (hi : i < 64) : + vcpu.reg_addr vc ⟨BitVec.ofNat 64 i⟩ = ok (vcpuRegWA vc i) := by + unfold vcpu.reg_addr vcpu.REGS vcpuRegWA vcpuRegOffA + simp only [lift, bind_ok] + congr 1 + apply UScalar.eq_of_val_eq + rw [wa_valP, wa_valP, wa_valP, core.num.U64.wrapping_mul_val_eq] + simp only [U64.size_def, U64.numBits, UScalar.size, UScalarTy.numBits] + have h64 : (64#u64 : U64).val = 64 := rfl + have h8 : (8#u64 : U64).val = 8 := rfl + have hi_val : (⟨BitVec.ofNat 64 i⟩ : U64).val = i := by + change (BitVec.ofNat 64 i).toNat = i + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + have hoff : (⟨BitVec.ofNat 64 (64 + 8 * i)⟩ : U64).val = 64 + 8 * i := by + change (BitVec.ofNat 64 (64 + 8 * i)).toNat = 64 + 8 * i + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + rw [h64, h8, hi_val, hoff] + have hmul : (i * 8) % 2 ^ 64 = 8 * i := by + rw [Nat.mul_comm]; exact Nat.mod_eq_of_lt (by omega) + rw [hmul, Nat.mod_add_mod, Nat.add_assoc] + +theorem vcpu_reg_get_eqA (s : S) (vc : U64) {i : Nat} (hi : i < 64) : + vcpu.reg_get st s vc ⟨BitVec.ofNat 64 i⟩ = ok (L.mem s (vcpuRegWA vc i)) := by + unfold vcpu.reg_get + rw [vcpu_reg_addr_eqA vc hi, bind_ok, L.read_eq] + +theorem vcpu_reg_set_eqA (s : S) (vc : U64) {i : Nat} (hi : i < 64) (val : U64) : + vcpu.reg_set st s vc ⟨BitVec.ofNat 64 i⟩ val = ok (L.wr s (vcpuRegWA vc i) val) := by + unfold vcpu.reg_set + rw [vcpu_reg_addr_eqA vc hi, bind_ok, L.write_eq] + +theorem fl4NumLrs_le_16 : Fl4SpecA64.fl4NumLrs ≤ 16 := by + unfold Fl4SpecA64.fl4NumLrs + dsimp only + split_ifs <;> first | omega | exact Nat.min_le_right _ _ + +theorem vcpu_abi_vgic_vtr_eqA (hP : VcpuPlatAgreeA ar) (a : A) : + vcpu.abi_vgic_vtr ⟨Fl4SpecA64.fl4A64Platform.vgicVtr⟩ Fl4SpecA64.fl4GicV3 = + ok ⟨Fl4SpecA64.abiVgicVtr⟩ := by + unfold vcpu.abi_vgic_vtr Fl4SpecA64.abiVgicVtr + dsimp only + by_cases hz : Fl4SpecA64.fl4A64Platform.vgicVtr = 0 + · have hz' : (⟨Fl4SpecA64.fl4A64Platform.vgicVtr⟩ : U64) = 0#u64 := by + apply UScalar.eq_of_val_eq + change Fl4SpecA64.fl4A64Platform.vgicVtr.toNat = 0 + rw [hz]; rfl + rw [if_pos hz', if_pos hz]; rfl + · have hz' : (⟨Fl4SpecA64.fl4A64Platform.vgicVtr⟩ : U64) ≠ 0#u64 := by + intro h; exact hz (congrArg UScalar.bv h) + rw [if_neg hz', if_neg hz] + cases hg : Fl4SpecA64.fl4GicV3 + · simp only [Bool.false_eq_true, ↓reduceIte, lift, bind_ok] + by_cases hge : (Fl4SpecA64.fl4A64Platform.vgicVtr &&& (0x3f : Word)) ≥ (16 : Word) + · have hge' : (⟨Fl4SpecA64.fl4A64Platform.vgicVtr⟩ &&& 63#u64 : U64) ≥ 16#u64 := hge + rw [if_pos hge', if_pos hge]; rfl + · have hge' : ¬ ((⟨Fl4SpecA64.fl4A64Platform.vgicVtr⟩ &&& 63#u64 : U64) ≥ 16#u64) := hge + rw [if_neg hge', if_neg hge] + · simp only [↓reduceIte, lift, bind_ok] + by_cases hge : (Fl4SpecA64.fl4A64Platform.vgicVtr &&& (0x1f : Word)) ≥ (16 : Word) + · have hge' : (⟨Fl4SpecA64.fl4A64Platform.vgicVtr⟩ &&& 31#u64 : U64) ≥ 16#u64 := hge + rw [if_pos hge', if_pos hge]; rfl + · have hge' : ¬ ((⟨Fl4SpecA64.fl4A64Platform.vgicVtr⟩ &&& 31#u64 : U64) ≥ 16#u64) := hge + rw [if_neg hge', if_neg hge] + +theorem vcpu_num_lrs_eqA (hP : VcpuPlatAgreeA ar) (a : A) : + vcpu.num_lrs ar a = ok ⟨BitVec.ofNat 64 Fl4SpecA64.fl4NumLrs⟩ := by + unfold vcpu.num_lrs Fl4SpecA64.fl4NumLrs + rw [hP.vtr a, bind_ok] + dsimp only + by_cases hz : Fl4SpecA64.fl4A64Platform.vgicVtr = 0 + · have hz' : (⟨Fl4SpecA64.fl4A64Platform.vgicVtr⟩ : U64) = 0#u64 := by + apply UScalar.eq_of_val_eq + change Fl4SpecA64.fl4A64Platform.vgicVtr.toNat = 0 + rw [hz]; rfl + rw [if_pos hz', if_pos hz]; rfl + · have hz' : (⟨Fl4SpecA64.fl4A64Platform.vgicVtr⟩ : U64) ≠ 0#u64 := by + intro h; exact hz (congrArg UScalar.bv h) + rw [if_neg hz', if_neg hz] + obtain ⟨g, hga, hgb⟩ := hP.gic a + rw [hga, bind_ok] + have hgb' : decide (g = 3#u64) = Fl4SpecA64.fl4GicV3 := hgb + by_cases hg3 : g = 3#u64 + · have hfl : Fl4SpecA64.fl4GicV3 = true := by rw [← hgb', decide_eq_true hg3] + simp only [hg3, hfl, ↓reduceIte, lift, bind_ok] + have hle : (Fl4SpecA64.fl4A64Platform.vgicVtr &&& (0x1f : Word)).toNat ≤ 31 := by + rw [BitVec.toNat_and] + exact Nat.le_trans Nat.and_le_right (by decide) + have hvalAnd : (⟨Fl4SpecA64.fl4A64Platform.vgicVtr⟩ &&& 31#u64 : U64).val = + (Fl4SpecA64.fl4A64Platform.vgicVtr &&& (0x1f : Word)).toNat := rfl + have hadd : (⟨Fl4SpecA64.fl4A64Platform.vgicVtr⟩ &&& 31#u64 : U64) + 1#u64 = + ok ⟨BitVec.ofNat 64 ((Fl4SpecA64.fl4A64Platform.vgicVtr &&& 0x1f).toNat + 1)⟩ := by + have hlt : (⟨Fl4SpecA64.fl4A64Platform.vgicVtr⟩ &&& 31#u64 : U64).val + (1#u64 : U64).val < 2 ^ 64 := by + rw [hvalAnd]; change _ + 1 < _; omega + rw [u64_add_ok hlt, hvalAnd]; rfl + rw [hadd, bind_ok] + congr 1; apply UScalar.eq_of_val_eq + unfold core.cmp.impls.OrdU64.min + split_ifs with hlt16 + · have hlt16' : 16 < ((Fl4SpecA64.fl4A64Platform.vgicVtr &&& 0x1f).toNat + 1) % 2 ^ 64 := by + simpa [UScalar.lt_equiv, UScalar.val, BitVec.toNat_ofNat] using hlt16 + rw [Nat.mod_eq_of_lt (by omega)] at hlt16' + change 16 = (BitVec.ofNat 64 (min ((Fl4SpecA64.fl4A64Platform.vgicVtr &&& 0x1f).toNat + 1) 16)).toNat + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)]; omega + · have hle16' : ¬ (16 < ((Fl4SpecA64.fl4A64Platform.vgicVtr &&& 0x1f).toNat + 1) % 2 ^ 64) := by + simpa [UScalar.lt_equiv, UScalar.val, BitVec.toNat_ofNat] using hlt16 + rw [Nat.mod_eq_of_lt (by omega)] at hle16' + change (BitVec.ofNat 64 ((Fl4SpecA64.fl4A64Platform.vgicVtr &&& 0x1f).toNat + 1)).toNat = + (BitVec.ofNat 64 (min ((Fl4SpecA64.fl4A64Platform.vgicVtr &&& 0x1f).toNat + 1) 16)).toNat + rw [BitVec.toNat_ofNat, BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega), Nat.mod_eq_of_lt (by omega)]; omega + · have hfl : Fl4SpecA64.fl4GicV3 = false := by rw [← hgb', decide_eq_false hg3] + simp only [hg3, hfl, Bool.false_eq_true, ↓reduceIte, lift, bind_ok] + have hle : (Fl4SpecA64.fl4A64Platform.vgicVtr &&& (0x3f : Word)).toNat ≤ 63 := by + rw [BitVec.toNat_and] + exact Nat.le_trans Nat.and_le_right (by decide) + have hvalAnd : (⟨Fl4SpecA64.fl4A64Platform.vgicVtr⟩ &&& 63#u64 : U64).val = + (Fl4SpecA64.fl4A64Platform.vgicVtr &&& (0x3f : Word)).toNat := rfl + have hadd : (⟨Fl4SpecA64.fl4A64Platform.vgicVtr⟩ &&& 63#u64 : U64) + 1#u64 = + ok ⟨BitVec.ofNat 64 ((Fl4SpecA64.fl4A64Platform.vgicVtr &&& 0x3f).toNat + 1)⟩ := by + have hlt : (⟨Fl4SpecA64.fl4A64Platform.vgicVtr⟩ &&& 63#u64 : U64).val + (1#u64 : U64).val < 2 ^ 64 := by + rw [hvalAnd]; change _ + 1 < _; omega + rw [u64_add_ok hlt, hvalAnd]; rfl + rw [hadd, bind_ok] + congr 1; apply UScalar.eq_of_val_eq + unfold core.cmp.impls.OrdU64.min + split_ifs with hlt16 + · have hlt16' : 16 < ((Fl4SpecA64.fl4A64Platform.vgicVtr &&& 0x3f).toNat + 1) % 2 ^ 64 := by + simpa [UScalar.lt_equiv, UScalar.val, BitVec.toNat_ofNat] using hlt16 + rw [Nat.mod_eq_of_lt (by omega)] at hlt16' + change 16 = (BitVec.ofNat 64 (min ((Fl4SpecA64.fl4A64Platform.vgicVtr &&& 0x3f).toNat + 1) 16)).toNat + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)]; omega + · have hle16' : ¬ (16 < ((Fl4SpecA64.fl4A64Platform.vgicVtr &&& 0x3f).toNat + 1) % 2 ^ 64) := by + simpa [UScalar.lt_equiv, UScalar.val, BitVec.toNat_ofNat] using hlt16 + rw [Nat.mod_eq_of_lt (by omega)] at hle16' + change (BitVec.ofNat 64 ((Fl4SpecA64.fl4A64Platform.vgicVtr &&& 0x3f).toNat + 1)).toNat = + (BitVec.ofNat 64 (min ((Fl4SpecA64.fl4A64Platform.vgicVtr &&& 0x3f).toNat + 1) 16)).toNat + rw [BitVec.toNat_ofNat, BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega), Nat.mod_eq_of_lt (by omega)]; omega + +/-- `[S]` Local-core reduction of `smp.remote_tcb_stall`: when `t` and (if `t` is bound to a VCPU +with an active `RUN_CALLER`) that caller both have affinity `L.cpu s`, `remote_tcb_stall` is a no-op. -/ +theorem remote_tcb_stall_localA (s : S) (a : A) (t : U64) + (haff : L.mem s (affA t) = L.cpu s) + (haffC : ∀ v, L.mem s (wa t 3664#u64) = v → v ≠ 0#u64 → + ∀ c, L.mem s (wa v 24#u64) = c → c ≠ 0#u64 → L.mem s (affA c) = L.cpu s) : + smp.remote_tcb_stall st ar s a t = ok (s, a) := by + unfold smp.remote_tcb_stall + rw [eq_of_spec_fd (obj.Tcb.affinity.spec s t), bind_ok] + rw [eq_of_spec_fd (L.cur_cpu_spec s), bind_ok] + have h1 : (L.mem s (affA t) != L.cpu s) = false := by + simp [haff] + rw [h1]; simp only [Bool.false_eq_true, ↓reduceIte, bind_ok] + unfold vcpu.bound + rw [eq_of_spec_fd Fl4ProofsA64.P0.Consts.layout.tcb.VCPU.spec] + simp only [bind_ok, lift, L.read_eq] + change (if (L.mem s (wa t 3664#u64) != 0#u64) then _ else ok (s, a)) = ok (s, a) + by_cases hv : L.mem s (wa t 3664#u64) = 0#u64 + · simp [hv] + · have hvb : (L.mem s (wa t 3664#u64) != 0#u64) = true := bne_iff_ne.2 hv + rw [if_pos hvb] + simp only [vcpu.RUN_CALLER, lift, L.read_eq, bind_ok] + change (if (L.mem s (wa (L.mem s (wa t 3664#u64)) 24#u64) != 0#u64) then _ else ok (s, a)) = ok (s, a) + by_cases hc : L.mem s (wa (L.mem s (wa t 3664#u64)) 24#u64) = 0#u64 + · simp [hc] + · have hcb : (L.mem s (wa (L.mem s (wa t 3664#u64)) 24#u64) != 0#u64) = true := bne_iff_ne.2 hc + rw [if_pos hcb] + rw [eq_of_spec_fd (obj.Tcb.affinity.spec s _), bind_ok] + rw [eq_of_spec_fd (L.cur_cpu_spec s), bind_ok] + have hca := haffC _ rfl hv _ rfl hc + simp [hca] + +/-- `[S]` + `[P]` Local-core reduction of `vcpu.stall_vcpu`. -/ +theorem stall_vcpu_localA {AR : A → HwRootA → Prop} (hAK : VcpuArchKeepsA ar AR) + {h : HwRootA} {s : S} {a : A} (hAR : AR a h) (vc : U64) + (hStallT : L.mem s vc ≠ 0#u64 → smp.remote_tcb_stall st ar s a (L.mem s vc) = ok (s, a)) + (hStallC : L.mem s (wa vc 24#u64) ≠ 0#u64 → + smp.remote_tcb_stall st ar s a (L.mem s (wa vc 24#u64)) = ok (s, a)) : + vcpu.stall_vcpu st ar s a vc ⦃ r => r.1 = s ∧ AR r.2 h ⦄ := by + unfold vcpu.stall_vcpu vcpu.TCB vcpu.RUN_CALLER + simp only [lift, bind_ok, L.read_eq] + have hwa0 : wa vc 0#u64 = vc := by + apply UScalar.eq_of_val_eq; rw [wa_valP]; simp [UScalar.hBounds] + rw [hwa0] + have hstep1 : (if (L.mem s vc != 0#u64) then smp.remote_tcb_stall st ar s a (L.mem s vc) + else ok (s, a)) = ok (s, a) := by + by_cases ht : L.mem s vc = 0#u64 + · simp [ht] + · rw [if_pos (bne_iff_ne.2 ht)]; exact hStallT ht + rw [hstep1, bind_ok] + change (do + let (s2, a2) ← + if (L.mem s (wa vc 24#u64) != 0#u64) + then smp.remote_tcb_stall st ar s a (L.mem s (wa vc 24#u64)) + else ok (s, a) + let a3 ← ar.vcpu_flush a2 vc + ok (s2, a3)) ⦃ r => r.1 = s ∧ AR r.2 h ⦄ + have hstep2 : (if (L.mem s (wa vc 24#u64) != 0#u64) then + smp.remote_tcb_stall st ar s a (L.mem s (wa vc 24#u64)) else ok (s, a)) = ok (s, a) := by + by_cases hc : L.mem s (wa vc 24#u64) = 0#u64 + · simp [hc] + · rw [if_pos (bne_iff_ne.2 hc)]; exact hStallC hc + rw [hstep2, bind_ok] + change (do let a3 ← ar.vcpu_flush a vc; ok (s, a3)) ⦃ r => r.1 = s ∧ AR r.2 h ⦄ + refine WP.spec_bind (hAK.vcpu_flush a h vc hAR) ?_ + intro a' ha' + exact (WP.spec_ok _).2 ⟨rfl, ha'⟩ + +end helpers + +/-! ## `VcpuRelA` framing across VCPU register/header updates and disjoint writes -/ + +section frame +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +theorem VcPlacedA.disj_node {S : Type} {st : machine.Store S} [L : LawfulStore st] + {Z : XSetsA} {OS Ub : Set U64} {s : S} + (hP : VcPlacedA Z OS Ub (L.ksb s)) (hksFit : (L.ksb s).val + ksSizeA ≤ 2 ^ 64) + {b c off : U64} (hb : VcPage Z.V b) (hc : c.val < 128) (hoff : off.val < 4288) : + b ≠ L.nodeAt s c off := by + intro heq + apply hP.disj_ks b hb + rw [heq] + have hval : (L.nodeAt s c off).val = (L.ksb s).val + 223488 + c.val * 4288 + off.val := by + have hk := (L.ksb s).hBounds + have hc' := c.hBounds + simp only [ksSizeA] at hksFit + simp [LawfulStore.nodeAt, core.num.U64.wrapping_add_val_eq, core.num.U64.wrapping_mul_val_eq, + U64.size_def, U64.numBits, UScalar.size] at hk hc' ⊢ + omega + rw [hval] + simp only [ksSizeA] + omega + +/-- Helper: writing any word inside `VcPage Z.V` preserves `capAtA` at any slot in `OS` or `IsSlotA`. -/ +theorem capAtA_wr_vcPage {S : Type} {st : machine.Store S} [L : LawfulStore st] + {Z : XSetsA} {OS Ub : Set U64} {s : S} + (hP : VcPlacedA Z OS Ub (L.ksb s)) {b w y : U64} (hb : VcPage Z.V b) + (hy : y ∈ OS ∨ IsSlotA Z.th y) : + capAtA (L.mem (L.wr s b w)) y = capAtA (L.mem s) y := by + have hba : Aligned b := hP.vcPage_al hb + have hya : Aligned y := hy.elim (hP.al_os y) (hP.al_slot y) + have hne : b ≠ y ∧ b ≠ wa y 8#u64 := by + rcases hy with hy | hy + · have h := hP.disj_os b hb y hy; exact ⟨h.1, h.2.1⟩ + · exact hP.disj_slots b hb y hy + unfold capAtA + rw [mem_wr_neA hba hya hne.1, mem_wr_neA hba (aligned_wa hya 8#u64 (by decide)) hne.2] + +/-- Updating VCPU register `i < 64` (with upper 32 bits 0 if `i = vrVgicHcr`) preserves `VcpuRelA`. -/ +theorem VcpuRelA.upd_regA {Z : XSetsA} {OS Ub : Set U64} {s : S} {σ : State Unit} + (hV : VcpuRelA (L := L) Z OS Ub s σ) + (hksFit : (L.ksb s).val + ksSizeA ≤ 2 ^ 64) + (hCs : ∀ c ∈ Z.Cs, c.val < 128) + (hIdleT : ∀ c ∈ Z.Cs, L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) ∈ Z.T) + {vc : U64} (hvc : vc ∈ Z.V) + {x : Fl4SpecA64.Vcpu} (hk : σ.kheap (specPtr vc) = some (.archObj (.vcpu x))) + {i : Nat} (hi : i < 64) (w : U64) + (hhcr : i = vrVgicHcr → ucast 64 (ucast 32 w.bv) = w.bv) : + VcpuRelA (L := L) Z OS Ub (L.wr s (vcpuRegWA vc i) w) + (updKSc σ (specPtr vc) (.archObj (.vcpu (fl4VcpuRegSet i w.bv x)))) := by + have hbp : VcPage Z.V (vcpuRegWA vc i) := vcpuRegWA_in_vcPage hvc hi + have hba : Aligned (vcpuRegWA vc i) := hV.place.vcPage_al hbp + have hreg_ne_hdr : ∀ v ∈ Z.V, ∀ o : U64, o.val < 64 → vcpuRegWA vc i ≠ wa v o := by + intro v hv o ho + by_cases heq : v = vc + · subst heq + exact hV.place.same_vcpu_ne hv (by rw [vcpuRegOffA_val hi]; omega) (by omega) + (fun e => by have := congrArg UScalar.val e; rw [vcpuRegOffA_val hi] at this; omega) + · exact hV.place.disj_vcpu hvc hv (Ne.symm heq) (by rw [vcpuRegOffA_val hi]; omega) (by omega) + have hreg_ne_0 : ∀ v ∈ Z.V, vcpuRegWA vc i ≠ v := by + intro v hv + have h0 := hreg_ne_hdr v hv 0#u64 (by decide) + have hwa0 : wa v 0#u64 = v := by + apply UScalar.eq_of_val_eq; rw [wa_valP]; simp [UScalar.hBounds] + rwa [hwa0] at h0 + have hreg_ne_tcb : ∀ t ∈ Z.T, ∀ o : U64, o.val < 4096 → vcpuRegWA vc i ≠ wa t o := + fun t ht o ho => hV.place.disj_tcb vc hvc t ht (vcpuRegOffA i) o + (by rw [vcpuRegOffA_val hi]; omega) ho + have hmeta := fl4VcpuRegSet_pres_metaA i w.bv x + refine ⟨?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, hV.arch, ?_⟩ + · -- core : VcpuCoreA + refine ⟨?_, ?_, ?_, ?_⟩ + · intro v hv + by_cases heq : v = vc + · subst heq + refine ⟨fl4VcpuRegSet i w.bv x, updKSc_same _ _ _, ?_, ?_⟩ + · rw [mem_wr_neA hba (hV.place.vc_al hv) (hreg_ne_0 v hv), hmeta.1] + obtain ⟨x0, hk0, ht0, _⟩ := hV.core.obj v hv + rw [hk] at hk0; cases hk0; exact ht0 + · rw [mem_wr_neA hba (aligned_wa (hV.place.vc_al hv) 24#u64 (by decide)) (hreg_ne_hdr v hv 24#u64 (by decide)), + hmeta.2.2.1] + obtain ⟨x0, hk0, _, hr0⟩ := hV.core.obj v hv + rw [hk] at hk0; cases hk0; exact hr0 + · obtain ⟨xv, hkv, htv, hrv⟩ := hV.core.obj v hv + have hsp : specPtr v ≠ specPtr vc := fun h => heq (specPtr_inj h) + refine ⟨xv, by rw [updKSc_other _ hsp]; exact hkv, ?_, ?_⟩ + · rw [mem_wr_neA hba (hV.place.vc_al hv) (hreg_ne_0 v hv)]; exact htv + · rw [mem_wr_neA hba (aligned_wa (hV.place.vc_al hv) 24#u64 (by decide)) (hreg_ne_hdr v hv 24#u64 (by decide))]; exact hrv + · intro p xv hkp + by_cases heq : p = specPtr vc + · subst heq; exact ⟨vc, hvc, rfl⟩ + · rw [updKSc_other _ heq] at hkp; exact hV.core.back p xv hkp + · intro t ht + obtain ⟨xt, hkt, htv, htr⟩ := hV.core.tcb t ht + have hne_pt : specPtr t ≠ specPtr vc := by + intro h; rw [h, hk] at hkt; cases hkt + refine ⟨xt, by rw [updKSc_other _ hne_pt]; exact hkt, ?_, ?_⟩ + · rw [mem_wr_neA hba (aligned_wa (hV.place.al_t t ht) 3664#u64 (by decide)) (hreg_ne_tcb t ht 3664#u64 (by decide))]; exact htv + · rw [runVcpuA, mem_wr_neA hba (aligned_wa (hV.place.al_t t ht) 3672#u64 (by decide)) (hreg_ne_tcb t ht 3672#u64 (by decide))]; exact htr + · intro y hy p hcap + rw [capAtA_wr_vcPage hV.place hbp (Or.inr hy)] at hcap + exact hV.core.cap y hy p hcap + · -- owner + intro v hv + rw [mem_wr_neA hba (aligned_wa (hV.place.vc_al hv) 8#u64 (by decide)) (hreg_ne_hdr v hv 8#u64 (by decide))] + exact hV.owner v hv + · -- vppi + intro v hv xv hkv + rw [mem_wr_neA hba (aligned_wa (hV.place.vc_al hv) 16#u64 (by decide)) (hreg_ne_hdr v hv 16#u64 (by decide))] + by_cases heq : v = vc + · subst heq + rw [updKSc_same] at hkv; cases hkv + rw [hmeta.2.1]; exact hV.vppi v hv x hk + · rw [updKSc_other _ (fun h => heq (specPtr_inj h))] at hkv + exact hV.vppi v hv xv hkv + · -- runOff + intro v hv xv hkv + rw [mem_wr_neA hba (aligned_wa (hV.place.vc_al hv) 32#u64 (by decide)) (hreg_ne_hdr v hv 32#u64 (by decide))] + by_cases heq : v = vc + · subst heq + rw [updKSc_same] at hkv; cases hkv + rw [hmeta.2.2.2.1]; exact hV.runOff v hv x hk + · rw [updKSc_other _ (fun h => heq (specPtr_inj h))] at hkv + exact hV.runOff v hv xv hkv + · -- runOut + intro v hv xv hkv + rw [mem_wr_neA hba (aligned_wa (hV.place.vc_al hv) 40#u64 (by decide)) (hreg_ne_hdr v hv 40#u64 (by decide))] + by_cases heq : v = vc + · subst heq + rw [updKSc_same] at hkv; cases hkv + rw [hmeta.2.2.2.2.1]; exact hV.runOut v hv x hk + · rw [updKSc_other _ (fun h => heq (specPtr_inj h))] at hkv + exact hV.runOut v hv xv hkv + · -- regs + intro v hv xv hkv j hj + by_cases heq : v = vc + · subst heq + rw [updKSc_same] at hkv; cases hkv + by_cases hji : j = i + · subst hji + rw [L.mem_wr_same hba, fl4VcpuRegGet_set_sameA x hi w.bv] + split_ifs with h35 + · exact (hhcr h35).symm + · rfl + · have hne : vcpuRegWA v i ≠ vcpuRegWA v j := + hV.place.same_vcpu_ne hv (by rw [vcpuRegOffA_val hi]; omega) (by rw [vcpuRegOffA_val hj]; omega) + (fun e => by have := congrArg UScalar.val e; rw [vcpuRegOffA_val hj, vcpuRegOffA_val hi] at this; omega) + rw [mem_wr_neA hba (hV.place.vcPage_al (vcpuRegWA_in_vcPage hv hj)) hne, + fl4VcpuRegGet_set_otherA x hi hj hji w.bv] + exact hV.regs v hv x hk j hj + · have hne : vcpuRegWA vc i ≠ vcpuRegWA v j := + hV.place.disj_vcpu hvc hv (Ne.symm heq) (by rw [vcpuRegOffA_val hi]; omega) (by rw [vcpuRegOffA_val hj]; omega) + rw [mem_wr_neA hba (hV.place.vcPage_al (vcpuRegWA_in_vcPage hv hj)) hne] + rw [updKSc_other _ (fun h => heq (specPtr_inj h))] at hkv + exact hV.regs v hv xv hkv j hj + · -- bind_v2t + intro v hv t ht hne + rw [mem_wr_neA hba (hV.place.vc_al hv) (hreg_ne_0 v hv)] at ht + obtain ⟨htT, htv⟩ := hV.bind_v2t v hv t ht hne + exact ⟨htT, by rw [mem_wr_neA hba (aligned_wa (hV.place.al_t t htT) 3664#u64 (by decide)) (hreg_ne_tcb t htT 3664#u64 (by decide))]; exact htv⟩ + · -- bind_t2v + intro t ht v hv hne + rw [mem_wr_neA hba (aligned_wa (hV.place.al_t t ht) 3664#u64 (by decide)) (hreg_ne_tcb t ht 3664#u64 (by decide))] at hv + obtain ⟨hvV, hvt⟩ := hV.bind_t2v t ht v hv hne + exact ⟨hvV, by rw [mem_wr_neA hba (hV.place.vc_al hvV) (hreg_ne_0 v hvV)]; exact hvt⟩ + · -- run_v2c + intro v hv c hc hne + rw [mem_wr_neA hba (aligned_wa (hV.place.vc_al hv) 24#u64 (by decide)) (hreg_ne_hdr v hv 24#u64 (by decide))] at hc + obtain ⟨hcT, hcv⟩ := hV.run_v2c v hv c hc hne + exact ⟨hcT, by rw [runVcpuA, mem_wr_neA hba (aligned_wa (hV.place.al_t c hcT) 3672#u64 (by decide)) (hreg_ne_tcb c hcT 3672#u64 (by decide))]; exact hcv⟩ + · -- run_c2v + intro c hc v hv hne + rw [runVcpuA, mem_wr_neA hba (aligned_wa (hV.place.al_t c hc) 3672#u64 (by decide)) (hreg_ne_tcb c hc 3672#u64 (by decide))] at hv + obtain ⟨hvV, hvc'⟩ := hV.run_c2v c hc v hv hne + exact ⟨hvV, by rw [mem_wr_neA hba (aligned_wa (hV.place.vc_al hvV) 24#u64 (by decide)) (hreg_ne_hdr v hvV 24#u64 (by decide))]; exact hvc'⟩ + · -- run_state + intro c hc hne + rw [runVcpuA, mem_wr_neA hba (aligned_wa (hV.place.al_t c hc) 3672#u64 (by decide)) (hreg_ne_tcb c hc 3672#u64 (by decide))] at hne + rw [stA_eq_wa, mem_wr_neA hba (aligned_wa (hV.place.al_t c hc) 3328#u64 (by decide)) (hreg_ne_tcb c hc 3328#u64 (by decide))] + rw [← stA_eq_wa]; exact hV.run_state c hc hne + · -- bind_run_disj + intro t ht hne + rw [mem_wr_neA hba (aligned_wa (hV.place.al_t t ht) 3664#u64 (by decide)) (hreg_ne_tcb t ht 3664#u64 (by decide))] at hne + rw [runVcpuA, mem_wr_neA hba (aligned_wa (hV.place.al_t t ht) 3672#u64 (by decide)) (hreg_ne_tcb t ht 3672#u64 (by decide))] + exact hV.bind_run_disj t ht hne + · -- idle_no_vcpu + intro c hc + have hnd : L.nodeAt (L.wr s (vcpuRegWA vc i) w) c layout.node.IDLE_THREAD = + L.nodeAt s c layout.node.IDLE_THREAD := by + simp [LawfulStore.nodeAt, L.ksb_wr] + rw [hnd] + have hne_nd : vcpuRegWA vc i ≠ L.nodeAt s c layout.node.IDLE_THREAD := + hV.place.disj_node hksFit hbp (hCs c hc) (by unfold layout.node.IDLE_THREAD; decide) + rw [mem_wr_neA hba (hV.place.node_al c layout.node.IDLE_THREAD (by unfold layout.node.IDLE_THREAD; decide)) hne_nd] + have hit := hIdleT c hc + obtain ⟨h1, h2⟩ := hV.idle_no_vcpu c hc + exact ⟨by rw [mem_wr_neA hba (aligned_wa (hV.place.al_t _ hit) 3664#u64 (by decide)) (hreg_ne_tcb _ hit 3664#u64 (by decide))]; exact h1, + by rw [runVcpuA, mem_wr_neA hba (aligned_wa (hV.place.al_t _ hit) 3672#u64 (by decide)) (hreg_ne_tcb _ hit 3672#u64 (by decide))]; exact h2⟩ + · -- live + intro v hv hor + rw [mem_wr_neA hba (hV.place.vc_al hv) (hreg_ne_0 v hv), + mem_wr_neA hba (aligned_wa (hV.place.vc_al hv) 24#u64 (by decide)) (hreg_ne_hdr v hv 24#u64 (by decide))] at hor + obtain ⟨y, hy, hcap⟩ := hV.live v hv hor + exact ⟨y, hy, by rw [capAtA_wr_vcPage hV.place hbp (Or.inl hy)]; exact hcap⟩ + · -- place + rw [L.ksb_wr]; exact hV.place + +/-- Updating `VPPI_MASKED` (`wa vc 16#u64`) paired with updating `vcpuVppiMasked .vppiEventIRQ_VTimer` +preserves `VcpuRelA`. -/ +theorem VcpuRelA.upd_vppiA {Z : XSetsA} {OS Ub : Set U64} {s : S} {σ : State Unit} + (hV : VcpuRelA (L := L) Z OS Ub s σ) + (hksFit : (L.ksb s).val + ksSizeA ≤ 2 ^ 64) + (hCs : ∀ c ∈ Z.Cs, c.val < 128) + (hIdleT : ∀ c ∈ Z.Cs, L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) ∈ Z.T) + {vc : U64} (hvc : vc ∈ Z.V) + {x : Fl4SpecA64.Vcpu} (hk : σ.kheap (specPtr vc) = some (.archObj (.vcpu x))) + (b : Bool) : + VcpuRelA (L := L) Z OS Ub (L.wr s (wa vc 16#u64) (if b then 1#u64 else 0#u64)) + (updKSc σ (specPtr vc) (.archObj (.vcpu + { x with vcpuVppiMasked := Function.update x.vcpuVppiMasked .vppiEventIRQ_VTimer b }))) := by + have hbp : VcPage Z.V (wa vc 16#u64) := vcpuHdr_in_vcPage hvc (by decide) (by decide) + have hba : Aligned (wa vc 16#u64) := hV.place.vcPage_al hbp + have hne_hdr : ∀ v ∈ Z.V, ∀ o : U64, o.val < 4096 → o ≠ 16#u64 → wa vc 16#u64 ≠ wa v o := by + intro v hv o ho hno + by_cases heq : v = vc + · subst heq; exact hV.place.same_vcpu_ne hv (by decide) ho hno.symm + · exact hV.place.disj_vcpu hvc hv (Ne.symm heq) (by decide) ho + have hne_0 : ∀ v ∈ Z.V, wa vc 16#u64 ≠ v := by + intro v hv + have h0 := hne_hdr v hv 0#u64 (by decide) (by decide) + have hwa0 : wa v 0#u64 = v := by + apply UScalar.eq_of_val_eq; rw [wa_valP]; simp [UScalar.hBounds] + rwa [hwa0] at h0 + have hne_tcb : ∀ t ∈ Z.T, ∀ o : U64, o.val < 4096 → wa vc 16#u64 ≠ wa t o := + fun t ht o ho => hV.place.disj_tcb vc hvc t ht 16#u64 o (by decide) ho + refine ⟨?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, hV.arch, ?_⟩ + · refine ⟨?_, ?_, ?_, ?_⟩ + · intro v hv + by_cases heq : v = vc + · subst heq + refine ⟨_, updKSc_same _ _ _, ?_, ?_⟩ + · rw [mem_wr_neA hba (hV.place.vc_al hv) (hne_0 v hv)] + obtain ⟨x0, hk0, ht0, _⟩ := hV.core.obj v hv + rw [hk] at hk0; cases hk0; exact ht0 + · rw [mem_wr_neA hba (aligned_wa (hV.place.vc_al hv) 24#u64 (by decide)) (hne_hdr v hv 24#u64 (by decide) (by decide))] + obtain ⟨x0, hk0, _, hr0⟩ := hV.core.obj v hv + rw [hk] at hk0; cases hk0; exact hr0 + · obtain ⟨xv, hkv, htv, hrv⟩ := hV.core.obj v hv + refine ⟨xv, by rw [updKSc_other _ (fun h => heq (specPtr_inj h))]; exact hkv, + by rw [mem_wr_neA hba (hV.place.vc_al hv) (hne_0 v hv)]; exact htv, + by rw [mem_wr_neA hba (aligned_wa (hV.place.vc_al hv) 24#u64 (by decide)) (hne_hdr v hv 24#u64 (by decide) (by decide))]; exact hrv⟩ + · intro p xv hkp + by_cases heq : p = specPtr vc + · subst heq; exact ⟨vc, hvc, rfl⟩ + · rw [updKSc_other _ heq] at hkp; exact hV.core.back p xv hkp + · intro t ht + obtain ⟨xt, hkt, htv, htr⟩ := hV.core.tcb t ht + have hne_pt : specPtr t ≠ specPtr vc := by intro h; rw [h, hk] at hkt; cases hkt + exact ⟨xt, by rw [updKSc_other _ hne_pt]; exact hkt, + by rw [mem_wr_neA hba (aligned_wa (hV.place.al_t t ht) 3664#u64 (by decide)) (hne_tcb t ht 3664#u64 (by decide))]; exact htv, + by rw [runVcpuA, mem_wr_neA hba (aligned_wa (hV.place.al_t t ht) 3672#u64 (by decide)) (hne_tcb t ht 3672#u64 (by decide))]; exact htr⟩ + · intro y hy p hcap + rw [capAtA_wr_vcPage hV.place hbp (Or.inr hy)] at hcap + exact hV.core.cap y hy p hcap + · intro v hv + rw [mem_wr_neA hba (aligned_wa (hV.place.vc_al hv) 8#u64 (by decide)) (hne_hdr v hv 8#u64 (by decide) (by decide))] + exact hV.owner v hv + · intro v hv xv hkv + by_cases heq : v = vc + · subst heq; rw [updKSc_same] at hkv; cases hkv + rw [L.mem_wr_same hba]; simp + · rw [updKSc_other _ (fun h => heq (specPtr_inj h))] at hkv + have hne : wa vc 16#u64 ≠ wa v 16#u64 := + hV.place.disj_vcpu hvc hv (Ne.symm heq) (by decide) (by decide) + rw [mem_wr_neA hba (aligned_wa (hV.place.vc_al hv) 16#u64 (by decide)) hne] + exact hV.vppi v hv xv hkv + · intro v hv xv hkv + rw [mem_wr_neA hba (aligned_wa (hV.place.vc_al hv) 32#u64 (by decide)) (hne_hdr v hv 32#u64 (by decide) (by decide))] + by_cases heq : v = vc + · subst heq; rw [updKSc_same] at hkv; cases hkv; exact hV.runOff v hv x hk + · rw [updKSc_other _ (fun h => heq (specPtr_inj h))] at hkv; exact hV.runOff v hv xv hkv + · intro v hv xv hkv + rw [mem_wr_neA hba (aligned_wa (hV.place.vc_al hv) 40#u64 (by decide)) (hne_hdr v hv 40#u64 (by decide) (by decide))] + by_cases heq : v = vc + · subst heq; rw [updKSc_same] at hkv; cases hkv; exact hV.runOut v hv x hk + · rw [updKSc_other _ (fun h => heq (specPtr_inj h))] at hkv; exact hV.runOut v hv xv hkv + · intro v hv xv hkv j hj + have hne : wa vc 16#u64 ≠ vcpuRegWA v j := + hne_hdr v hv (vcpuRegOffA j) (by rw [vcpuRegOffA_val hj]; omega) + (fun e => by have := congrArg UScalar.val e; rw [vcpuRegOffA_val hj] at this; change 64 + 8 * j = 16 at this; omega) + rw [mem_wr_neA hba (hV.place.vcPage_al (vcpuRegWA_in_vcPage hv hj)) hne] + by_cases heq : v = vc + · subst heq; rw [updKSc_same] at hkv; cases hkv + exact hV.regs v hv x hk j hj + · rw [updKSc_other _ (fun h => heq (specPtr_inj h))] at hkv + exact hV.regs v hv xv hkv j hj + · intro v hv t ht hne + rw [mem_wr_neA hba (hV.place.vc_al hv) (hne_0 v hv)] at ht + obtain ⟨htT, htv⟩ := hV.bind_v2t v hv t ht hne + exact ⟨htT, by rw [mem_wr_neA hba (aligned_wa (hV.place.al_t t htT) 3664#u64 (by decide)) (hne_tcb t htT 3664#u64 (by decide))]; exact htv⟩ + · intro t ht v hv hne + rw [mem_wr_neA hba (aligned_wa (hV.place.al_t t ht) 3664#u64 (by decide)) (hne_tcb t ht 3664#u64 (by decide))] at hv + obtain ⟨hvV, hvt⟩ := hV.bind_t2v t ht v hv hne + exact ⟨hvV, by rw [mem_wr_neA hba (hV.place.vc_al hvV) (hne_0 v hvV)]; exact hvt⟩ + · intro v hv c hc hne + rw [mem_wr_neA hba (aligned_wa (hV.place.vc_al hv) 24#u64 (by decide)) (hne_hdr v hv 24#u64 (by decide) (by decide))] at hc + obtain ⟨hcT, hcv⟩ := hV.run_v2c v hv c hc hne + exact ⟨hcT, by rw [runVcpuA, mem_wr_neA hba (aligned_wa (hV.place.al_t c hcT) 3672#u64 (by decide)) (hne_tcb c hcT 3672#u64 (by decide))]; exact hcv⟩ + · intro c hc v hv hne + rw [runVcpuA, mem_wr_neA hba (aligned_wa (hV.place.al_t c hc) 3672#u64 (by decide)) (hne_tcb c hc 3672#u64 (by decide))] at hv + obtain ⟨hvV, hvc'⟩ := hV.run_c2v c hc v hv hne + exact ⟨hvV, by rw [mem_wr_neA hba (aligned_wa (hV.place.vc_al hvV) 24#u64 (by decide)) (hne_hdr v hvV 24#u64 (by decide) (by decide))]; exact hvc'⟩ + · intro c hc hne + rw [runVcpuA, mem_wr_neA hba (aligned_wa (hV.place.al_t c hc) 3672#u64 (by decide)) (hne_tcb c hc 3672#u64 (by decide))] at hne + rw [stA_eq_wa, mem_wr_neA hba (aligned_wa (hV.place.al_t c hc) 3328#u64 (by decide)) (hne_tcb c hc 3328#u64 (by decide))] + rw [← stA_eq_wa]; exact hV.run_state c hc hne + · intro t ht hne + rw [mem_wr_neA hba (aligned_wa (hV.place.al_t t ht) 3664#u64 (by decide)) (hne_tcb t ht 3664#u64 (by decide))] at hne + rw [runVcpuA, mem_wr_neA hba (aligned_wa (hV.place.al_t t ht) 3672#u64 (by decide)) (hne_tcb t ht 3672#u64 (by decide))] + exact hV.bind_run_disj t ht hne + · intro c hc + have hnd : L.nodeAt (L.wr s (wa vc 16#u64) (if b then 1#u64 else 0#u64)) c layout.node.IDLE_THREAD = + L.nodeAt s c layout.node.IDLE_THREAD := by + simp [LawfulStore.nodeAt, L.ksb_wr] + rw [hnd] + have hne_nd : wa vc 16#u64 ≠ L.nodeAt s c layout.node.IDLE_THREAD := + hV.place.disj_node hksFit hbp (hCs c hc) (by unfold layout.node.IDLE_THREAD; decide) + rw [mem_wr_neA hba (hV.place.node_al c layout.node.IDLE_THREAD (by unfold layout.node.IDLE_THREAD; decide)) hne_nd] + have hit := hIdleT c hc + obtain ⟨h1, h2⟩ := hV.idle_no_vcpu c hc + exact ⟨by rw [mem_wr_neA hba (aligned_wa (hV.place.al_t _ hit) 3664#u64 (by decide)) (hne_tcb _ hit 3664#u64 (by decide))]; exact h1, + by rw [runVcpuA, mem_wr_neA hba (aligned_wa (hV.place.al_t _ hit) 3672#u64 (by decide)) (hne_tcb _ hit 3672#u64 (by decide))]; exact h2⟩ + · intro v hv hor + rw [mem_wr_neA hba (hV.place.vc_al hv) (hne_0 v hv), + mem_wr_neA hba (aligned_wa (hV.place.vc_al hv) 24#u64 (by decide)) (hne_hdr v hv 24#u64 (by decide) (by decide))] at hor + obtain ⟨y, hy, hcap⟩ := hV.live v hv hor + exact ⟨y, hy, by rw [capAtA_wr_vcPage hV.place hbp (Or.inl hy)]; exact hcap⟩ + · rw [L.ksb_wr]; exact hV.place + +end frame + +/-! ## Local-core `TS_RUNNING_VM` companion to `NtfnSendA.send_signal_idle_bound_idle` -/ + +section ntfn_vm +variable {S : Type} {inst : machine.Store S} [L : LawfulStore inst] {A : Type} {ar : machine.Arch A} + +/-- **Local-core `TS_RUNNING_VM` companion to `NtfnSendA.send_signal_idle_bound_idle`** +(contract §2.1 & §4.3): when the bound TCB is in `TS_RUNNING_VM` and `remote_tcb_stall` on it +is a local no-op (e.g. via `remote_tcb_stall_localA`), `notification.send_signal` activates the +notification with `badge`, refining `sendSignal` with zero edits to `NtfnSendA.lean`. -/ +theorem send_signal_idle_bound_vm_localA {T N : Set U64} (hP : NPlaced T N) (s : S) (a : A) + (σ : State Unit) (hR : NtfnStateRel N T (L.mem s) σ) {n : U64} (hn : n ∈ N) + {x : Fl4SpecA64.Notification} (hk : σ.kheap (specPtr n) = some (.notification x)) + (hidle : x.ntfnObj = .idleNtfn) + {t : ObjRef} {tcb : Fl4SpecA64.Tcb} (hb : x.ntfnBoundTcb = some t) + (hkt : σ.kheap t = some (.tcb tcb)) (hvm : tcb.tcbState = .ext .runningVM) + (badge : U64) + (hstall : ∀ s1, + s1 = L.wr (L.wr s (epTSA n) + ((L.mem s (epTSA n) &&& ~~~3#u64) ||| (obj.NTFN_ACTIVE &&& 3#u64))) (nBadgeA n) badge → + smp.remote_tcb_stall inst ar s1 a (L.mem s (nBoundA n)) = ok (s1, a)) : + notification.send_signal inst ar s a n badge ⦃ r => + r.2 = a ∧ SingleRel (st := inst) N T s σ (Fl4SpecA64.sendSignal (Z := Unit) (specPtr n) badge.bv σ) r.1 ⦄ := by + have hx := hR.at_ntfn hn hk + obtain ⟨hb0, hbt⟩ := optRef_some (hx.bound ▸ hb) + have hbT := hx.bound_mem hb0 + have hy := hR.at_tcb hbT (hbt ▸ hkt) + have hrb : Fl4SpecA64.receiveBlocked tcb.tcbState = false := by rw [hvm]; rfl + have hst_vm : L.mem s (stA (L.mem s (nBoundA n))) = sched.TS_RUNNING_VM := by + apply UScalar.eq_of_val_eq + have h3 := hy.st + rw [hvm] at h3 + simpa [tsCode, sched.TS_RUNNING_VM] using h3 + have hns : L.mem s (stA (L.mem s (nBoundA n))) ≠ sched.TS_BLOCKED_ON_RECV := by + rw [hst_vm]; unfold sched.TS_RUNNING_VM sched.TS_BLOCKED_ON_RECV; decide + unfold notification.send_signal + rw [eq_of_spec_fd (obj.Ntfn.state.spec s n), bind_ok] + have hst_idle : L.epState s n = obj.NTFN_IDLE := stOf_idle hx.obj hidle + rw [if_pos hst_idle] + rw [eq_of_spec_fd (obj.Ntfn.bound_tcb.spec s n), bind_ok] + rw [eq_of_spec_fd (obj.Tcb.is_null.spec (L.mem s (nBoundA n))), bind_ok] + have hnb : (decide (L.mem s (nBoundA n) = 0#u64)) = false := decide_eq_false hb0 + rw [if_neg (by simp [hnb])] + rw [eq_of_spec_fd (obj.Tcb.state.spec s (L.mem s (nBoundA n))), bind_ok] + rw [if_neg hns, if_pos hst_vm] + unfold vcpu.signal_completes + rw [bind_ok, if_neg (show ¬ (false = true) by decide)] + rw [eq_of_spec_fd (notification.set_active.spec s n badge), bind_ok] + rw [if_neg (by simp [hnb])] + obtain ⟨hR', hW⟩ := set_active_rel hP hR hn hk hidle badge + have hst' : L.mem (L.wr (L.wr s (epTSA n) + ((L.mem s (epTSA n) &&& ~~~3#u64) ||| (obj.NTFN_ACTIVE &&& 3#u64))) (nBadgeA n) badge) + (stA (L.mem s (nBoundA n))) = sched.TS_RUNNING_VM := by + have h1 := nwr_wr (L := L) hP (K := 8#u64) hn (by simp) (by simp) s + ((L.mem s (epTSA n) &&& ~~~3#u64) ||| (obj.NTFN_ACTIVE &&& 3#u64)) + rw [← epTSA_eq] at h1 + have h2 := nwr_wr (L := L) hP (K := 16#u64) hn (by simp) (by simp) + (L.wr s (epTSA n) ((L.mem s (epTSA n) &&& ~~~3#u64) ||| (obj.NTFN_ACTIVE &&& 3#u64))) badge + rw [← nBadgeA_eq] at h2 + rw [((h1.trans h2).tcb _ hbT).1.st, hst_vm] + rw [eq_of_spec_fd (obj.Tcb.state.spec _ (L.mem s (nBoundA n))), bind_ok] + rw [if_pos hst'] + unfold notification.kick_guest + rw [hstall _ rfl, if_neg (by simp [hnb]), WP.spec_ok] + exact ⟨rfl, _, sendSignal_activate hk hidle (Or.inr ⟨t, tcb, hb, hkt, hrb⟩) badge.bv, hR', + NtfnStep.ntfn hk rfl rfl, hW⟩ + +end ntfn_vm + +end + +end Fl4ProofsA64.P3.Vcpu diff --git a/proofs/Fl4ProofsA64/P3/VcpuRunBaseA.lean b/proofs/Fl4ProofsA64/P3/VcpuRunBaseA.lean new file mode 100644 index 000000000..fa2c84fc5 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/VcpuRunBaseA.lean @@ -0,0 +1,2511 @@ +/- +P3 (aarch64, A64-P3 S4 W4): **Label 69 (`ARMVCPURun`) and guest-exit handlers +(`handle_vcpu_run_exit` / `handle_vcpu_run_vppi`) refinement**: +- `VcpuStage2RootA`: `[P]` Stage-2 root-switch premise for `vspace.set_stage2_root` + (leaves `Store S` and Stage-1 `HwRootA` unchanged). +- `VcpuXferStateA.write_owner_cpu`: `vc.OWNER_CPU := cur_cpu + 1` framing (concrete-only, + spec unchanged). +- `VcpuXferStateA.write_vppi_masked_bit0`: `vc.VPPI_MASKED |= 1` lockstep with setting + `vcpuVppiMasked .vppiEventIRQ_VTimer := true`. +- `VcpuXferStateA.enter_run_vm`: lockstep `RUN_CALLER`/`RUN_OFFSET`/`RUN_OUT_MASK`/`RUN_VCPU`, + `vspace.set_stage2_root`, and `sched.set_thread_state ... TS_RUNNING_VM`. +- `vcpu_run_jA`: `vcpu.decode_run` (label 69 `ARMVCPURun`) on `VcpuTopPostA`. +- `vcpu_run_exit_jA` & `vcpu_run_vppi_jA`: pre-`syscall.finish` bodies of + `syscall.handle_vcpu_run_exit` and `syscall.handle_vcpu_run_vppi` against `fl4HandleVcpuRunExit`. + +Named premises: `[P] VcpuPlatAgreeA ar`, `[P] VcpuArchKeepsA ar AR`, `[P] VcpuVmRootA st ar AR`, +`[P] VcpuStage2RootA st ar AR`, `[P] NowAgreeA ar Clock` (for guest exits), +`[S]` local-core `remote_tcb_stall` affinity. +-/ +import Fl4ProofsA64.P3.VcpuCheckJA +import Fl4ProofsA64.P3.ScTimeA +import Fl4ProofsA64.P3.ScCheckA + +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3.Vcpu + +open Fl4Spec Fl4SpecA64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3 Fl4ProofsA64.P3.Sched +open Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +private theorem runVcpuA_eq (t : U64) : runVcpuA t = wa t 3672#u64 := rfl + +/-- `[P]` Architecture Stage-2 root-switch premise for `vspace.set_stage2_root`: leaves `Store S` +unchanged and preserves Stage-1 `AR a h`. -/ +structure VcpuStage2RootA {S : Type} (st : machine.Store S) {A : Type} (ar : machine.Arch A) + (AR : A → HwRootA → Prop) : Prop where + set_stage2_root : ∀ s a h (t : U64), AR a h → + vspace.set_stage2_root st ar s a t ⦃ r => r.1 = s ∧ AR r.2 h ⦄ + +section run_helpers +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +private theorem aligned_of_wa24A {c : U64} (h : Aligned (wa c 24#u64)) : Aligned c := by + unfold Aligned at h ⊢ + rw [wa_valP] at h + have hm : (c.val + 24) % 2 ^ 64 % 8 = (c.val + 24) % 8 := Nat.mod_mod_of_dvd _ (by decide : 8 ∣ 2 ^ 64) + have h24 : (24#u64).val = 24 := rfl + rw [h24] at h; omega + +private theorem writesIn_reflA {s : S} {W : U64 → Prop} : WritesIn st s s W := + ⟨rfl, rfl, fun _ _ _ => rfl⟩ + +private theorem writesIn_trans1A {s s1 s2 : S} {W : U64 → Prop} + (h1 : WritesIn st s s1 W) (h2 : WritesIn st s1 s2 W) : WritesIn st s s2 W := + ⟨h2.1.trans h1.1, h2.2.1.trans h1.2.1, fun b hb hnw => (h2.2.2 b hb hnw).trans (h1.2.2 b hb hnw)⟩ + +private theorem writesIn_wr1A {s : S} {q v : U64} {W : U64 → Prop} (hal : Aligned q) (hq : W q) : + WritesIn st s (L.wr s q v) W := + ⟨L.ksb_wr _ _ _, L.cpu_wr _ _ _, fun b hb hnw => mem_wr_neA hal hb (fun e => hnw (e ▸ hq))⟩ + +theorem vc_off_ne {Z : XSetsA} {OS Ub : Set U64} {ksb : U64} + (P : VcPlacedA Z OS Ub ksb) {v1 v2 : U64} (hv1 : v1 ∈ Z.V) (hv2 : v2 ∈ Z.V) + {o1 o2 : U64} (ho1 : o1.val < 4096) (ho2 : o2.val < 4096) + (hne : v1 = v2 → o1 ≠ o2) : wa v1 o1 ≠ wa v2 o2 := by + by_cases h : v1 = v2 + · subst h; exact P.same_vcpu_ne hv1 ho1 ho2 (hne rfl) + · exact P.disj_vcpu hv1 hv2 h ho1 ho2 + +theorem nodeAt_valA (s : S) (c o : U64) : + (L.nodeAt s c o).val = ((L.ksb s).val + 223488 + c.val * 4288 + o.val) % 2 ^ 64 := by + unfold LawfulStore.nodeAt + rw [wa_valP, wa_valP, wa_valP, wm_valP] + have h1 : (223488#u64).val = 223488 := rfl + have h2 : (4288#u64).val = 4288 := rfl + rw [h1, h2] + simp only [Nat.mod_add_mod, Nat.add_mod_mod] + +theorem updKSc_idA {σ : State Unit} {p : ObjRef} {o : KernelObject} + (hk : σ.kheap p = some o) : updKSc σ p o = σ := by + cases σ with | mk a ex => + cases a + unfold updKSc; dsimp only + congr 1; congr 1 + funext q; by_cases hq : q = p + · subst hq; rw [Function.update_self]; exact hk.symm + · exact Function.update_of_ne hq _ _ + +theorem updKSc_commA (σ : State Unit) {p q : ObjRef} {o1 o2 : KernelObject} (hne : p ≠ q) : + updKSc (updKSc σ p o1) q o2 = updKSc (updKSc σ q o2) p o1 := by + cases σ with | mk a ex => + cases a; unfold updKSc; dsimp only; rw [Function.update_comm hne] + +private theorem updKSc_idemA (σ : State Unit) {p : ObjRef} {o1 o2 : KernelObject} : + updKSc (updKSc σ p o1) p o2 = updKSc σ p o2 := by + cases σ with | mk a ex => + cases a; unfold updKSc; dsimp only; rw [Function.update_idem] + +private theorem wa_zeroA (x : U64) : wa x 0#u64 = x := by + apply UScalar.eq_of_val_eq; rw [wa_valP]; simp [UScalar.hBounds] + +private theorem frameSize_bits_eqA (sz : capability.FrameSize) : + capability.FrameSize.bits sz = ok ⟨BitVec.ofNat 64 (frameBitsA sz)⟩ ∧ + frameBitsA sz = pageBitsForSize (frameSizeOfA sz) ∧ + frameBitsA sz < 64 := by + cases sz <;> + exact ⟨by unfold capability.FrameSize.bits frameBitsA; rfl, + by unfold frameBitsA frameSizeOfA pageBitsForSize; rfl, + by unfold frameBitsA; decide⟩ + +/-- `sched.set_thread_state ... TS_RUNNING_VM` on a bound thread with an active SC out of the +release queue performs a single state-word write. -/ +theorem set_thread_state_vm_wrA {s : S} {t : U64} (hta : TcbAddr t) + (hsc : L.mem s (scA t) ≠ 0#u64) (hrm : L.rmax s (L.mem s (scA t)) ≠ 0#u64) + (hiq : (L.mem s (inqA t) &&& sched.Q_RELEASE) = 0#u64) + (hma : Aligned (maxA (L.mem s (scA t)))) (hmx : stA t ≠ maxA (L.mem s (scA t))) : + sched.set_thread_state st s t sched.TS_RUNNING_VM = ok (L.wr s (stA t) sched.TS_RUNNING_VM) := by + have sA : Aligned (stA t) := by rw [stA_eq]; exact field_aligned hta (by simp) (by simp) + have aS : Aligned (scA t) := field_aligned (K := 3464#u64) hta (by simp) (by simp) + have aQ : Aligned (inqA t) := field_aligned (K := 3592#u64) hta (by simp) (by simp) + have nsc : stA t ≠ scA t := by + rw [stA_eq]; exact field_ne (K2 := 3464#u64) hta hta (by simp) (by simp) (fun _ => by simp) + have niq : stA t ≠ inqA t := by + rw [stA_eq]; exact field_ne (K2 := 3592#u64) hta hta (by simp) (by simp) (fun _ => by simp) + have hs : sched.is_schedulable st (L.wr s (stA t) sched.TS_RUNNING_VM) t = ok true := by + apply is_schedA + · rw [L.mem_wr_same sA]; exact Or.inr (Or.inr rfl) + · rw [mem_wr_ne' sA aS nsc]; exact hsc + · rw [mem_wr_ne' sA aQ niq]; exact hiq + · rw [mem_wr_ne' sA aS nsc] + show L.mem _ (maxA _) ≠ 0#u64 + rw [mem_wr_ne' sA hma hmx]; exact hrm + unfold sched.set_thread_state + rw [eq_of_spec_fd (obj.Tcb.set_state.spec (L := L) s t _)] + simp only [bind_ok] + exact schedule_tcb_sch _ _ hs + +/-- Writing `vc.OWNER_CPU := wa (L.cpu s) 1#u64` preserves `VcpuXferStateA` with `σ` unchanged. -/ +theorem VcpuXferStateA.write_owner_cpu {AR : A → HwRootA → Prop} {Z : XSetsA} + {OS Ub : Set U64} {m : Fl4SpecA64.McState} {a : A} {s : S} {σ : State Unit} + (X : VcpuXferStateA (L := L) AR Z OS Ub m a s σ) + (hCs : ∀ c ∈ Z.Cs, c.val < 128) (hcpu : (L.cpu s).val < 128) + (hExt : ∀ b, VcPage Z.V b → ¬ ExtFootE Z.X Z.Y Z.T2 b) + (hSw : ∀ b, VcPage Z.V b → ∀ y, SlotWA Z.th y → + b ≠ y ∧ b ≠ wa y 8#u64 ∧ b ≠ wa y 16#u64 ∧ b ≠ wa y 24#u64) + (hfa : ∀ q, FootA (L := L) Z s q → Aligned q) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hIdleT : ∀ c ∈ Z.Cs, L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) ∈ Z.T) + {vc : U64} (hvc : vc ∈ Z.V) : + let me := wa (L.cpu s) 1#u64 + let s' := L.wr s (wa vc 8#u64) me + VcpuXferStateA (L := L) AR Z OS Ub m a s' σ ∧ + VcpuRunPresA L Z OS s s' σ σ := by + dsimp only + set me := wa (L.cpu s) 1#u64 + set s' := L.wr s (wa vc 8#u64) me + have hal_ow : Aligned (wa vc 8#u64) := aligned_wa (X.vcpu.place.vc_al hvc) 8#u64 (by decide) + have hpage_ow : VcPage Z.V (wa vc 8#u64) := ⟨vc, hvc, 8#u64, by decide, by decide, rfl⟩ + have hWvc : ∀ b, b = wa vc 8#u64 → VcPage Z.V b ∧ ∀ v ∈ Z.V, b ≠ v ∧ b ≠ wa v 24#u64 := by + rintro b rfl + refine ⟨hpage_ow, fun v hv => ⟨?_, ?_⟩⟩ + · rw [← wa_zeroA v]; exact vc_off_ne X.vcpu.place hvc hv (by decide) (by decide) (fun _ => by decide) + · exact vc_off_ne X.vcpu.place hvc hv (by decide) (by decide) (fun _ => by decide) + obtain ⟨v0, hkv0, -, -⟩ := X.vcpu.core.obj vc hvc + have hw : WritesIn st s s' (fun b => b = wa vc 8#u64) := + writesIn_wr1A hal_ow rfl + obtain ⟨hJ', hTr', hCarF⟩ := vcpuNonLinkWr_frameA X.j X.tr X.vcpu.place hCs hcpu hExt hSw hfa + hw hWvc hvc hkv0 rfl rfl hsub + rw [updKSc_idA hkv0] at hJ' hCarF + have hCar' := hCarF X.car + have hme_le : me.val ≤ 128 := by + have : me.val = ((L.cpu s).val + 1) % 2 ^ 64 := wa_valP (L.cpu s) 1#u64 + rw [this, Nat.mod_eq_of_lt (by omega)]; omega + have hvc_ne : ∀ v ∈ Z.V, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → o ≠ 8#u64 → + L.mem s' (wa v o) = L.mem s (wa v o) := fun v hv o ho ho8 hne => + mem_wr_neA hal_ow (aligned_wa (X.vcpu.place.vc_al hv) o ho8) + (vc_off_ne X.vcpu.place hvc hv (by decide) ho (fun _ e => hne e.symm)) + have htcb_eq : ∀ t ∈ Z.T, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → + L.mem s' (wa t o) = L.mem s (wa t o) := fun t ht o ho ho8 => + mem_wr_neA hal_ow (aligned_wa (X.vcpu.place.al_t t ht) o ho8) + (X.vcpu.place.disj_tcb vc hvc t ht 8#u64 o (by decide) ho) + have hV' : VcpuRelA (L := L) Z OS Ub s' σ := by + refine ⟨hJ'.2.1, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, X.vcpu.arch, + by rw [L.ksb_wr]; exact X.vcpu.place⟩ + · intro v hv + by_cases hveq : v = vc + · subst hveq; rw [L.mem_wr_same hal_ow]; exact hme_le + · rw [mem_wr_neA hal_ow (aligned_wa (X.vcpu.place.vc_al hv) 8#u64 (by decide)) + (vc_off_ne X.vcpu.place hvc hv (by decide) (by decide) (fun e => absurd e.symm hveq))] + exact X.vcpu.owner v hv + · intro v hv x hkx; rw [hvc_ne v hv 16#u64 (by decide) (by decide) (by decide)]; exact X.vcpu.vppi v hv x hkx + · intro v hv x hkx; rw [hvc_ne v hv 32#u64 (by decide) (by decide) (by decide)]; exact X.vcpu.runOff v hv x hkx + · intro v hv x hkx; rw [hvc_ne v hv 40#u64 (by decide) (by decide) (by decide)]; exact X.vcpu.runOut v hv x hkx + · intro v hv x hkx i hi + have hlt : (vcpuRegOffA i).val < 4096 := by rw [vcpuRegOffA_val hi]; omega + have h8 : (vcpuRegOffA i).val % 8 = 0 := by rw [vcpuRegOffA_val hi]; omega + have hne : vcpuRegOffA i ≠ 8#u64 := fun e => by + have hv8 : (8#u64).val = 8 := rfl + have := congrArg UScalar.val e; rw [vcpuRegOffA_val hi, hv8] at this; omega + rw [vcpuRegWA, hvc_ne v hv (vcpuRegOffA i) hlt h8 hne]; exact X.vcpu.regs v hv x hkx i hi + · intro v hv t ht hne + rw [← wa_zeroA v, hvc_ne v hv 0#u64 (by decide) (by decide) (by decide), wa_zeroA] at ht + obtain ⟨htT, htv⟩ := X.vcpu.bind_v2t v hv t ht hne + exact ⟨htT, by rw [htcb_eq t htT 3664#u64 (by decide) (by decide)]; exact htv⟩ + · intro t ht v hv hne + rw [htcb_eq t ht 3664#u64 (by decide) (by decide)] at hv + obtain ⟨hvV, hvt⟩ := X.vcpu.bind_t2v t ht v hv hne + exact ⟨hvV, by rw [← wa_zeroA v, hvc_ne v hvV 0#u64 (by decide) (by decide) (by decide), wa_zeroA]; exact hvt⟩ + · intro v hv c hc hne + rw [hvc_ne v hv 24#u64 (by decide) (by decide) (by decide)] at hc + obtain ⟨hcT, hcv⟩ := X.vcpu.run_v2c v hv c hc hne + exact ⟨hcT, by rw [runVcpuA_eq, htcb_eq c hcT 3672#u64 (by decide) (by decide), ← runVcpuA_eq]; exact hcv⟩ + · intro c hc v hv hne + rw [runVcpuA_eq, htcb_eq c hc 3672#u64 (by decide) (by decide), ← runVcpuA_eq] at hv + obtain ⟨hvV, hvc'⟩ := X.vcpu.run_c2v c hc v hv hne + exact ⟨hvV, by rw [hvc_ne v hvV 24#u64 (by decide) (by decide) (by decide)]; exact hvc'⟩ + · intro c hc hne + rw [runVcpuA_eq, htcb_eq c hc 3672#u64 (by decide) (by decide), ← runVcpuA_eq] at hne + rw [stA_eq, htcb_eq c hc 3328#u64 (by decide) (by decide), ← stA_eq] + exact X.vcpu.run_state c hc hne + · intro t ht hne + rw [htcb_eq t ht 3664#u64 (by decide) (by decide)] at hne + rw [runVcpuA_eq, htcb_eq t ht 3672#u64 (by decide) (by decide), ← runVcpuA_eq] + exact X.vcpu.bind_run_disj t ht hne + · intro c hc + have hnd : L.nodeAt s' c layout.node.IDLE_THREAD = L.nodeAt s c layout.node.IDLE_THREAD := + nodeAt_of_ksb (L.ksb_wr _ _ _) c layout.node.IDLE_THREAD + have hal_i : Aligned (L.nodeAt s c layout.node.IDLE_THREAD) := + hfa _ (FootA.nslot _ (FootNA.node c _ hc (Or.inr (Or.inl rfl)))) + have hne_i : wa vc 8#u64 ≠ L.nodeAt s c layout.node.IDLE_THREAD := fun e => + not_footA_of_vcNonLinkA X.vcpu.place X.j.1.ksFit hCs hcpu hExt hSw hpage_ow (hWvc _ rfl).2 + (e ▸ FootA.nslot _ (FootNA.node c _ hc (Or.inr (Or.inl rfl)))) + have hid : L.mem s' (L.nodeAt s' c layout.node.IDLE_THREAD) = L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) := by + rw [hnd]; exact mem_wr_neA hal_ow hal_i hne_i + rw [hid, htcb_eq _ (hIdleT c hc) 3664#u64 (by decide) (by decide), + runVcpuA_eq, htcb_eq _ (hIdleT c hc) 3672#u64 (by decide) (by decide), ← runVcpuA_eq] + exact X.vcpu.idle_no_vcpu c hc + · intro v hv hact + rw [← wa_zeroA v, hvc_ne v hv 0#u64 (by decide) (by decide) (by decide), wa_zeroA, + hvc_ne v hv 24#u64 (by decide) (by decide) (by decide)] at hact + obtain ⟨y, hy, hcap⟩ := X.vcpu.live v hv hact + have hdisj := X.vcpu.place.disj_os _ hpage_ow y hy + have hal_y := X.vcpu.place.al_os y hy + refine ⟨y, hy, ?_⟩ + unfold capAtA + rw [mem_wr_neA hal_ow hal_y hdisj.1, + mem_wr_neA hal_ow (aligned_wa hal_y 8#u64 (by decide)) hdisj.2.1] + exact hcap + have hF' : FrameMemRelA Z OS Ub (L.ksb s') (L.mem s') σ.machineState.underlyingMemory := + ⟨X.fmem.aligned, + fun q hq => by + rw [mem_wr_neA hal_ow (X.fmem.aligned q hq) (fun e => X.vcpu.place.disj_ub _ hpage_ow (e ▸ hq))] + exact X.fmem.agree q hq, + fun y hy ptr sz r mp s2 asid va hcap => by + have hal_y := X.vcpu.place.al_os y hy + have hdisj := X.vcpu.place.disj_os _ hpage_ow y hy + have hcap0 : capAtA (L.mem s) y = .Frame ptr sz r false mp s2 asid va := by + unfold capAtA at hcap ⊢ + rwa [mem_wr_neA hal_ow hal_y hdisj.1, + mem_wr_neA hal_ow (aligned_wa hal_y 8#u64 (by decide)) hdisj.2.1] at hcap + exact X.fmem.frames y hy ptr sz r mp s2 asid va hcap0, + X.fmem.disj_os, X.fmem.disj_slots, X.fmem.disj_tcb, X.fmem.disj_sc, + by rw [L.ksb_wr]; exact X.fmem.disj_ks⟩ + have hR' : TcbRegsRelA (L.mem s') σ Z.T := fun t ht x hkx r => by + obtain ⟨hK, hreg⟩ := regA_eq_wa_KA t r + rw [hreg, htcb_eq t ht _ (by have := regIdxA_lt r; omega) (by omega), ← hreg] + exact X.regs t ht x hkx r + refine ⟨⟨hJ', hTr', hCar', hV', hF', hR'⟩, ?_⟩ + refine ⟨fun y hy => ?_, fun t ht => ?_, fun c hc => ?_, fun c hc => ?_, fun _ h => h, fun u hu h => ?_⟩ + · have hal_y := X.vcpu.place.al_os y hy + have hdisj := X.vcpu.place.disj_os _ hpage_ow y hy + exact ⟨mem_wr_neA hal_ow hal_y hdisj.1, + mem_wr_neA hal_ow (aligned_wa hal_y 8#u64 (by decide)) hdisj.2.1, + mem_wr_neA hal_ow (aligned_wa hal_y 16#u64 (by decide)) hdisj.2.2.1, + mem_wr_neA hal_ow (aligned_wa hal_y 24#u64 (by decide)) hdisj.2.2.2⟩ + · exact ⟨by rw [inqA_eq, htcb_eq t ht 3592#u64 (by decide) (by decide), ← inqA_eq], + by rw [scA_eq, htcb_eq t ht 3464#u64 (by decide) (by decide), ← scA_eq]⟩ + · have hal_c : Aligned (maxA c) := by + rw [maxA_eqA]; exact aligned_wa (aligned_of_wa24A (hfa _ (FootA.nslot _ (FootNA.sc c hc)))) 72#u64 (by decide) + rw [maxA_eqA] at hal_c ⊢ + exact mem_wr_neA hal_ow hal_c (X.vcpu.place.disj_sc vc hvc c hc 8#u64 72#u64 (by decide) (by decide)) + · have hnd : L.nodeAt s' c layout.node.IDLE_THREAD = L.nodeAt s c layout.node.IDLE_THREAD := + nodeAt_of_ksb (L.ksb_wr _ _ _) c layout.node.IDLE_THREAD + have hal_i : Aligned (L.nodeAt s c layout.node.IDLE_THREAD) := + hfa _ (FootA.nslot _ (FootNA.node c _ hc (Or.inr (Or.inl rfl)))) + have hne_i : wa vc 8#u64 ≠ L.nodeAt s c layout.node.IDLE_THREAD := fun e => + not_footA_of_vcNonLinkA X.vcpu.place X.j.1.ksFit hCs hcpu hExt hSw hpage_ow (hWvc _ rfl).2 + (e ▸ FootA.nslot _ (FootNA.node c _ hc (Or.inr (Or.inl rfl)))) + rw [hnd]; exact mem_wr_neA hal_ow hal_i hne_i + · rwa [runVcpuA_eq, htcb_eq u hu 3672#u64 (by decide) (by decide), ← runVcpuA_eq] at h + +/-- Setting bit 0 of `vc.VPPI_MASKED` locksteps with setting `vcpuVppiMasked .vppiEventIRQ_VTimer := true` +and preserves `VcpuXferStateA` and `VcpuRunPresA`. -/ +theorem VcpuXferStateA.write_vppi_masked_bit0 {AR : A → HwRootA → Prop} {Z : XSetsA} + {OS Ub : Set U64} {m : Fl4SpecA64.McState} {a : A} {s : S} {σ : State Unit} + (X : VcpuXferStateA (L := L) AR Z OS Ub m a s σ) + (hCs : ∀ c ∈ Z.Cs, c.val < 128) (hcpu : (L.cpu s).val < 128) + (hExt : ∀ b, VcPage Z.V b → ¬ ExtFootE Z.X Z.Y Z.T2 b) + (hSw : ∀ b, VcPage Z.V b → ∀ y, SlotWA Z.th y → + b ≠ y ∧ b ≠ wa y 8#u64 ∧ b ≠ wa y 16#u64 ∧ b ≠ wa y 24#u64) + (hfa : ∀ q, FootA (L := L) Z s q → Aligned q) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hIdleT : ∀ c ∈ Z.Cs, L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) ∈ Z.T) + {vc : U64} (hvc : vc ∈ Z.V) + {v0 : Vcpu} (hkv0 : σ.kheap (specPtr vc) = some (.archObj (.vcpu v0))) : + let s' := L.wr s (wa vc 16#u64) (L.mem s (wa vc 16#u64) ||| 1#u64) + let v1 := { v0 with vcpuVppiMasked := Function.update v0.vcpuVppiMasked .vppiEventIRQ_VTimer true } + let σ' := updKSc σ (specPtr vc) (.archObj (.vcpu v1)) + VcpuXferStateA (L := L) AR Z OS Ub m a s' σ' ∧ + VcpuRunPresA L Z OS s s' σ σ' := by + dsimp only + set s' := L.wr s (wa vc 16#u64) (L.mem s (wa vc 16#u64) ||| 1#u64) + set v1 := { v0 with vcpuVppiMasked := Function.update v0.vcpuVppiMasked .vppiEventIRQ_VTimer true } + set σ' := updKSc σ (specPtr vc) (.archObj (.vcpu v1)) + have hal_vp : Aligned (wa vc 16#u64) := aligned_wa (X.vcpu.place.vc_al hvc) 16#u64 (by decide) + have hpage_vp : VcPage Z.V (wa vc 16#u64) := ⟨vc, hvc, 16#u64, by decide, by decide, rfl⟩ + have hWvc : ∀ b, b = wa vc 16#u64 → VcPage Z.V b ∧ ∀ v ∈ Z.V, b ≠ v ∧ b ≠ wa v 24#u64 := by + rintro b rfl + refine ⟨hpage_vp, fun v hv => ⟨?_, ?_⟩⟩ + · rw [← wa_zeroA v]; exact vc_off_ne X.vcpu.place hvc hv (by decide) (by decide) (fun _ => by decide) + · exact vc_off_ne X.vcpu.place hvc hv (by decide) (by decide) (fun _ => by decide) + have hw : WritesIn st s s' (fun b => b = wa vc 16#u64) := + ⟨L.ksb_wr _ _ _, L.cpu_wr _ _ _, fun b hb hnw => mem_wr_neA hal_vp hb (fun e => hnw e.symm)⟩ + obtain ⟨hJ', hTr', hCarF⟩ := vcpuNonLinkWr_frameA (v' := v1) X.j X.tr X.vcpu.place hCs hcpu hExt hSw hfa + hw hWvc hvc hkv0 rfl rfl hsub + have hCar' := hCarF X.car + have hvc_ne : ∀ v ∈ Z.V, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → o ≠ 16#u64 → + L.mem s' (wa v o) = L.mem s (wa v o) := fun v hv o ho ho8 hne => + mem_wr_neA hal_vp (aligned_wa (X.vcpu.place.vc_al hv) o ho8) + (vc_off_ne X.vcpu.place hvc hv (by decide) ho (fun _ e => hne e.symm)) + have htcb_eq : ∀ t ∈ Z.T, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → + L.mem s' (wa t o) = L.mem s (wa t o) := fun t ht o ho ho8 => + mem_wr_neA hal_vp (aligned_wa (X.vcpu.place.al_t t ht) o ho8) + (X.vcpu.place.disj_tcb vc hvc t ht 16#u64 o (by decide) ho) + have hV' : VcpuRelA (L := L) Z OS Ub s' σ' := by + refine ⟨hJ'.2.1, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, X.vcpu.arch, + by rw [L.ksb_wr]; exact X.vcpu.place⟩ + · intro v hv; rw [hvc_ne v hv 8#u64 (by decide) (by decide) (by decide)]; exact X.vcpu.owner v hv + · intro v hv x hkx + by_cases hveq : v = vc + · subst v + rw [updKSc_same] at hkx + have hx : x = v1 := ArchKernelObj.vcpu.inj (KernelObject.archObj.inj (Option.some.inj hkx.symm)) + subst hx + rw [L.mem_wr_same hal_vp, X.vcpu.vppi vc hvc v0 hkv0] + simp only [v1, Function.update_self] + cases v0.vcpuVppiMasked .vppiEventIRQ_VTimer <;> decide + · have hne_p : specPtr v ≠ specPtr vc := fun e => hveq (specPtr_inj e) + rw [updKSc_other _ hne_p] at hkx + rw [mem_wr_neA hal_vp (aligned_wa (X.vcpu.place.vc_al hv) 16#u64 (by decide)) + (vc_off_ne X.vcpu.place hvc hv (by decide) (by decide) (fun e => absurd e.symm hveq))] + exact X.vcpu.vppi v hv x hkx + · intro v hv x hkx + rw [hvc_ne v hv 32#u64 (by decide) (by decide) (by decide)] + by_cases hveq : v = vc + · subst v; rw [updKSc_same] at hkx + have hx : x = v1 := ArchKernelObj.vcpu.inj (KernelObject.archObj.inj (Option.some.inj hkx.symm)) + subst hx; exact X.vcpu.runOff vc hvc v0 hkv0 + · rw [updKSc_other _ (fun e => hveq (specPtr_inj e))] at hkx; exact X.vcpu.runOff v hv x hkx + · intro v hv x hkx + rw [hvc_ne v hv 40#u64 (by decide) (by decide) (by decide)] + by_cases hveq : v = vc + · subst v; rw [updKSc_same] at hkx + have hx : x = v1 := ArchKernelObj.vcpu.inj (KernelObject.archObj.inj (Option.some.inj hkx.symm)) + subst hx; exact X.vcpu.runOut vc hvc v0 hkv0 + · rw [updKSc_other _ (fun e => hveq (specPtr_inj e))] at hkx; exact X.vcpu.runOut v hv x hkx + · intro v hv x hkx i hi + have hlt : (vcpuRegOffA i).val < 4096 := by rw [vcpuRegOffA_val hi]; omega + have h8 : (vcpuRegOffA i).val % 8 = 0 := by rw [vcpuRegOffA_val hi]; omega + have hne : vcpuRegOffA i ≠ 16#u64 := fun e => by + have hv16 : (16#u64).val = 16 := rfl + have := congrArg UScalar.val e; rw [vcpuRegOffA_val hi, hv16] at this; omega + rw [vcpuRegWA, hvc_ne v hv (vcpuRegOffA i) hlt h8 hne] + by_cases hveq : v = vc + · subst v; rw [updKSc_same] at hkx + have hx : x = v1 := ArchKernelObj.vcpu.inj (KernelObject.archObj.inj (Option.some.inj hkx.symm)) + subst hx; exact X.vcpu.regs vc hvc v0 hkv0 i hi + · rw [updKSc_other _ (fun e => hveq (specPtr_inj e))] at hkx; exact X.vcpu.regs v hv x hkx i hi + · intro v hv t ht hne + rw [← wa_zeroA v, hvc_ne v hv 0#u64 (by decide) (by decide) (by decide), wa_zeroA] at ht + obtain ⟨htT, htv⟩ := X.vcpu.bind_v2t v hv t ht hne + exact ⟨htT, by rw [htcb_eq t htT 3664#u64 (by decide) (by decide)]; exact htv⟩ + · intro t ht v hv hne + rw [htcb_eq t ht 3664#u64 (by decide) (by decide)] at hv + obtain ⟨hvV, hvt⟩ := X.vcpu.bind_t2v t ht v hv hne + exact ⟨hvV, by rw [← wa_zeroA v, hvc_ne v hvV 0#u64 (by decide) (by decide) (by decide), wa_zeroA]; exact hvt⟩ + · intro v hv c hc hne + rw [hvc_ne v hv 24#u64 (by decide) (by decide) (by decide)] at hc + obtain ⟨hcT, hcv⟩ := X.vcpu.run_v2c v hv c hc hne + exact ⟨hcT, by rw [runVcpuA_eq, htcb_eq c hcT 3672#u64 (by decide) (by decide), ← runVcpuA_eq]; exact hcv⟩ + · intro c hc v hv hne + rw [runVcpuA_eq, htcb_eq c hc 3672#u64 (by decide) (by decide), ← runVcpuA_eq] at hv + obtain ⟨hvV, hvc'⟩ := X.vcpu.run_c2v c hc v hv hne + exact ⟨hvV, by rw [hvc_ne v hvV 24#u64 (by decide) (by decide) (by decide)]; exact hvc'⟩ + · intro c hc hne + rw [runVcpuA_eq, htcb_eq c hc 3672#u64 (by decide) (by decide), ← runVcpuA_eq] at hne + rw [stA_eq, htcb_eq c hc 3328#u64 (by decide) (by decide), ← stA_eq] + exact X.vcpu.run_state c hc hne + · intro t ht hne + rw [htcb_eq t ht 3664#u64 (by decide) (by decide)] at hne + rw [runVcpuA_eq, htcb_eq t ht 3672#u64 (by decide) (by decide), ← runVcpuA_eq] + exact X.vcpu.bind_run_disj t ht hne + · intro c hc + have hnd : L.nodeAt s' c layout.node.IDLE_THREAD = L.nodeAt s c layout.node.IDLE_THREAD := + nodeAt_of_ksb (L.ksb_wr _ _ _) c layout.node.IDLE_THREAD + have hal_i : Aligned (L.nodeAt s c layout.node.IDLE_THREAD) := + hfa _ (FootA.nslot _ (FootNA.node c _ hc (Or.inr (Or.inl rfl)))) + have hne_i : wa vc 16#u64 ≠ L.nodeAt s c layout.node.IDLE_THREAD := fun e => + not_footA_of_vcNonLinkA X.vcpu.place X.j.1.ksFit hCs hcpu hExt hSw hpage_vp (hWvc _ rfl).2 + (e ▸ FootA.nslot _ (FootNA.node c _ hc (Or.inr (Or.inl rfl)))) + have hid : L.mem s' (L.nodeAt s' c layout.node.IDLE_THREAD) = L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) := by + rw [hnd]; exact mem_wr_neA hal_vp hal_i hne_i + rw [hid, htcb_eq _ (hIdleT c hc) 3664#u64 (by decide) (by decide), + runVcpuA_eq, htcb_eq _ (hIdleT c hc) 3672#u64 (by decide) (by decide), ← runVcpuA_eq] + exact X.vcpu.idle_no_vcpu c hc + · intro v hv hact + rw [← wa_zeroA v, hvc_ne v hv 0#u64 (by decide) (by decide) (by decide), wa_zeroA, + hvc_ne v hv 24#u64 (by decide) (by decide) (by decide)] at hact + obtain ⟨y, hy, hcap⟩ := X.vcpu.live v hv hact + have hdisj := X.vcpu.place.disj_os _ hpage_vp y hy + have hal_y := X.vcpu.place.al_os y hy + refine ⟨y, hy, ?_⟩ + unfold capAtA + rw [mem_wr_neA hal_vp hal_y hdisj.1, + mem_wr_neA hal_vp (aligned_wa hal_y 8#u64 (by decide)) hdisj.2.1] + exact hcap + have hF' : FrameMemRelA Z OS Ub (L.ksb s') (L.mem s') σ'.machineState.underlyingMemory := + ⟨X.fmem.aligned, + fun q hq => by + rw [mem_wr_neA hal_vp (X.fmem.aligned q hq) (fun e => X.vcpu.place.disj_ub _ hpage_vp (e ▸ hq))] + exact X.fmem.agree q hq, + fun y hy ptr sz r mp s2 asid va hcap => by + have hal_y := X.vcpu.place.al_os y hy + have hdisj := X.vcpu.place.disj_os _ hpage_vp y hy + have hcap0 : capAtA (L.mem s) y = .Frame ptr sz r false mp s2 asid va := by + unfold capAtA at hcap ⊢ + rwa [mem_wr_neA hal_vp hal_y hdisj.1, + mem_wr_neA hal_vp (aligned_wa hal_y 8#u64 (by decide)) hdisj.2.1] at hcap + exact X.fmem.frames y hy ptr sz r mp s2 asid va hcap0, + X.fmem.disj_os, X.fmem.disj_slots, X.fmem.disj_tcb, X.fmem.disj_sc, + by rw [L.ksb_wr]; exact X.fmem.disj_ks⟩ + have hR' : TcbRegsRelA (L.mem s') σ' Z.T := fun t ht x hkx r => by + have hne : specPtr t ≠ specPtr vc := fun e => by + obtain ⟨xt0, hkt0, -, -⟩ := X.vcpu.core.tcb t ht + rw [e, hkv0] at hkt0; cases hkt0 + rw [updKSc_other _ hne] at hkx + obtain ⟨hK, hreg⟩ := regA_eq_wa_KA t r + rw [hreg, htcb_eq t ht _ (by have := regIdxA_lt r; omega) (by omega), ← hreg] + exact X.regs t ht x hkx r + refine ⟨⟨hJ', hTr', hCar', hV', hF', hR'⟩, ?_⟩ + refine ⟨fun y hy => ?_, fun t ht => ?_, fun c hc => ?_, fun c hc => ?_, + fun q hq => inUserFrame_updKScA hkv0 (fun _ e => by cases e) hq, fun u hu h => ?_⟩ + · have hal_y := X.vcpu.place.al_os y hy + have hdisj := X.vcpu.place.disj_os _ hpage_vp y hy + exact ⟨mem_wr_neA hal_vp hal_y hdisj.1, + mem_wr_neA hal_vp (aligned_wa hal_y 8#u64 (by decide)) hdisj.2.1, + mem_wr_neA hal_vp (aligned_wa hal_y 16#u64 (by decide)) hdisj.2.2.1, + mem_wr_neA hal_vp (aligned_wa hal_y 24#u64 (by decide)) hdisj.2.2.2⟩ + · exact ⟨by rw [inqA_eq, htcb_eq t ht 3592#u64 (by decide) (by decide), ← inqA_eq], + by rw [scA_eq, htcb_eq t ht 3464#u64 (by decide) (by decide), ← scA_eq]⟩ + · have hal_c : Aligned (maxA c) := by + rw [maxA_eqA]; exact aligned_wa (aligned_of_wa24A (hfa _ (FootA.nslot _ (FootNA.sc c hc)))) 72#u64 (by decide) + rw [maxA_eqA] at hal_c ⊢ + exact mem_wr_neA hal_vp hal_c (X.vcpu.place.disj_sc vc hvc c hc 16#u64 72#u64 (by decide) (by decide)) + · have hnd : L.nodeAt s' c layout.node.IDLE_THREAD = L.nodeAt s c layout.node.IDLE_THREAD := + nodeAt_of_ksb (L.ksb_wr _ _ _) c layout.node.IDLE_THREAD + have hal_i : Aligned (L.nodeAt s c layout.node.IDLE_THREAD) := + hfa _ (FootA.nslot _ (FootNA.node c _ hc (Or.inr (Or.inl rfl)))) + have hne_i : wa vc 16#u64 ≠ L.nodeAt s c layout.node.IDLE_THREAD := fun e => + not_footA_of_vcNonLinkA X.vcpu.place X.j.1.ksFit hCs hcpu hExt hSw hpage_vp (hWvc _ rfl).2 + (e ▸ FootA.nslot _ (FootNA.node c _ hc (Or.inr (Or.inl rfl)))) + rw [hnd]; exact mem_wr_neA hal_vp hal_i hne_i + · rwa [runVcpuA_eq, htcb_eq u hu 3672#u64 (by decide) (by decide), ← runVcpuA_eq] at h + +/-! ## Section 2: Lockstep `enter_run_vm` (`RUN_CALLER`/`RUN_OFFSET`/`RUN_OUT_MASK`/`RUN_VCPU` + `TS_RUNNING_VM`) -/ + +set_option maxHeartbeats 800000 in +/-- Lockstep entry into `TS_RUNNING_VM`: setting `vc.RUN_CALLER := ct`, `vc.RUN_OFFSET := offset`, +`vc.RUN_OUT_MASK := out_mask`, `ct.RUN_VCPU := vc`, and `ct.STATE := TS_RUNNING_VM` matches +`fl4VcpuExtUpdate` + `setRunVcpu` + `setThreadState ct (.ext .runningVM)` and preserves +`VcpuXferStateA`. -/ +theorem VcpuXferStateA.enter_run_vm {AR : A → HwRootA → Prop} {Z : XSetsA} + {OS Ub : Set U64} {m : Fl4SpecA64.McState} {a : A} {s : S} {σ : State Unit} + (X : VcpuXferStateA (L := L) AR Z OS Ub m a s σ) + (hU : UbFootDisjA Z OS Ub) (hLD : TcbLinkDisjA Z (L.ksb s)) + (hCs : ∀ c ∈ Z.Cs, c.val < 128) (hcpu : (L.cpu s).val < 128) + (hExt : ∀ b, VcPage Z.V b → ¬ ExtFootE Z.X Z.Y Z.T2 b) + (hSw : ∀ b, VcPage Z.V b → ∀ y, SlotWA Z.th y → + b ≠ y ∧ b ≠ wa y 8#u64 ∧ b ≠ wa y 16#u64 ∧ b ≠ wa y 24#u64) + (hfa : ∀ q, FootA (L := L) Z s q → Aligned q) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hIdleT : ∀ c ∈ Z.Cs, L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) ∈ Z.T) + (hOS_tcb : ∀ t ∈ Z.T, ∀ K : U64, (K = 3664#u64 ∨ K = 3672#u64) → ∀ y ∈ OS, + wa t K ≠ y ∧ wa t K ≠ wa y 8#u64 ∧ wa t K ≠ wa y 16#u64 ∧ wa t K ≠ wa y 24#u64) + (hSlotSt : ∀ y, SlotWA Z.th y → stA (L.curW s) ≠ y ∧ stA (L.curW s) ≠ wa y 8#u64 ∧ + stA (L.curW s) ≠ wa y 16#u64 ∧ stA (L.curW s) ≠ wa y 24#u64) + (hOS_st : ∀ y ∈ OS, stA (L.curW s) ≠ y ∧ stA (L.curW s) ≠ wa y 8#u64 ∧ + stA (L.curW s) ≠ wa y 16#u64 ∧ stA (L.curW s) ≠ wa y 24#u64) + (hKs_st : ∀ o : U64, o.val < ksSizeA → stA (L.curW s) ≠ wa (L.ksb s) o) + (hSc_st : ∀ c ∈ Z.C, stA (L.curW s) ≠ wa c 24#u64 ∧ stA (L.curW s) ≠ wa c 72#u64 ∧ + stA (L.curW s) ≠ maxA c ∧ Aligned (maxA c)) + (hVs_st : ¬ VsFootA Z.Pp Z.S1 Z.Y (L.ksb s) (stA (L.curW s))) + (hExt_st : ¬ ExtFootE Z.X Z.Y Z.T2 (stA (L.curW s))) + {vc ct offset out_mask : U64} + (hct_eq : ct = L.curW s) (hvc : vc ∈ Z.V) (hct : ct ∈ Z.T) + (hrc0 : L.mem s (wa vc 24#u64) = 0#u64) + (htv0 : L.mem s (wa ct 3664#u64) = 0#u64) + (hrv0 : L.mem s (runVcpuA ct) = 0#u64) + (hct_not_idle : ∀ c ∈ Z.Cs, L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) ≠ ct) + (hsc : L.mem s (scA ct) ≠ 0#u64) + (hrm : L.rmax s (L.mem s (scA ct)) ≠ 0#u64) + (hiq : (L.mem s (inqA ct) &&& sched.Q_RELEASE) = 0#u64) + (hNotRel : specPtr ct ∉ σ.releaseQueue) + (hout_mask : out_mask.bv &&& ~~~Fl4SpecA64.smAll = 0) + (hvc_live : ∃ y ∈ OS, capAtA (L.mem s) y = .Vcpu vc) + {v0 : Vcpu} {x0 : Tcb} + (hkv0 : σ.kheap (specPtr vc) = some (.archObj (.vcpu v0))) + (hkt0 : σ.kheap (specPtr ct) = some (.tcb x0)) : + let s1 := L.wr s (wa vc 24#u64) ct + let s2 := L.wr s1 (wa vc 32#u64) offset + let s3 := L.wr s2 (wa vc 40#u64) out_mask + let s4 := L.wr s3 (runVcpuA ct) vc + let s5 := L.wr s4 (stA ct) sched.TS_RUNNING_VM + let v1 : Vcpu := { v0 with vcpuExt := { v0.vcpuExt with + runCaller := some (specPtr ct), runOffset := offset.bv, runOutMask := out_mask.bv } } + let x1 : Tcb := { x0 with tcbState := .ext .runningVM, tcbExt := { x0.tcbExt with tcbRunVcpu := some (specPtr vc) } } + let σ5 := updKSc (updKSc σ (specPtr vc) (.archObj (.vcpu v1))) (specPtr ct) (.tcb x1) + sched.set_thread_state st s4 ct sched.TS_RUNNING_VM = ok s5 ∧ + (do fl4VcpuExtUpdate (Z := Unit) (specPtr vc) fun e => + { e with runCaller := some (specPtr ct), runOffset := offset.bv, runOutMask := out_mask.bv } + setRunVcpu (specPtr ct) (some (specPtr vc)) + setThreadState (specPtr ct) (.ext .runningVM)) σ = ({((), σ5)}, False) ∧ + VcpuXferStateA (L := L) AR Z OS Ub m a s5 σ5 ∧ + (∀ y ∈ OS, L.mem s5 y = L.mem s y ∧ L.mem s5 (wa y 8#u64) = L.mem s (wa y 8#u64) ∧ + L.mem s5 (wa y 16#u64) = L.mem s (wa y 16#u64) ∧ L.mem s5 (wa y 24#u64) = L.mem s (wa y 24#u64)) ∧ + (∀ t ∈ Z.T, L.mem s5 (inqA t) = L.mem s (inqA t) ∧ L.mem s5 (scA t) = L.mem s (scA t)) ∧ + (∀ c ∈ Z.C, L.mem s5 (maxA c) = L.mem s (maxA c)) ∧ + (∀ c ∈ Z.Cs, L.mem s5 (L.nodeAt s5 c layout.node.IDLE_THREAD) = L.mem s (L.nodeAt s c layout.node.IDLE_THREAD)) := by + subst hct_eq + dsimp only + set ct := L.curW s + set s1 := L.wr s (wa vc 24#u64) ct + set s2 := L.wr s1 (wa vc 32#u64) offset + set s3 := L.wr s2 (wa vc 40#u64) out_mask + set s4 := L.wr s3 (runVcpuA ct) vc + set s5 := L.wr s4 (stA ct) sched.TS_RUNNING_VM + set v1 : Vcpu := { v0 with vcpuExt := { v0.vcpuExt with + runCaller := some (specPtr ct), runOffset := offset.bv, runOutMask := out_mask.bv } } + set x1 : Tcb := { x0 with tcbState := .ext .runningVM, tcbExt := { x0.tcbExt with tcbRunVcpu := some (specPtr vc) } } + set σ1 := updKSc σ (specPtr vc) (.archObj (.vcpu v1)) + set σ2 := updKSc σ1 (specPtr ct) (.tcb { x0 with tcbExt := { x0.tcbExt with tcbRunVcpu := some (specPtr vc) } }) + set σ5 := updKSc σ1 (specPtr ct) (.tcb x1) + have hta := hU.tcb_al ct hct + have hal_t : Aligned ct := by unfold Aligned; have := hta.align; omega + have hal_24 : Aligned (wa vc 24#u64) := aligned_wa (X.vcpu.place.vc_al hvc) 24#u64 (by decide) + have hal_32 : Aligned (wa vc 32#u64) := aligned_wa (X.vcpu.place.vc_al hvc) 32#u64 (by decide) + have hal_40 : Aligned (wa vc 40#u64) := aligned_wa (X.vcpu.place.vc_al hvc) 40#u64 (by decide) + have hal_rv : Aligned (runVcpuA ct) := by rw [runVcpuA_eq]; exact aligned_wa hal_t 3672#u64 (by decide) + have hal_st : Aligned (stA ct) := by rw [stA_eq]; exact aligned_wa hal_t 3328#u64 (by decide) + have hct_ne0 : ct ≠ 0#u64 := fun e => hta.ne0 (congrArg UScalar.val e) + have hvc_ne0 : vc ≠ 0#u64 := (X.vcpu.place.al vc hvc).1 + have hne_vc_ct : specPtr vc ≠ specPtr ct := fun e => by rw [e, hkt0] at hkv0; cases hkv0 + have hksb4 : L.ksb s4 = L.ksb s := by simp [s4, s3, s2, s1, L.ksb_wr] + have hksb5 : L.ksb s5 = L.ksb s := by rw [L.ksb_wr, hksb4] + have hcpu4 : L.cpu s4 = L.cpu s := by simp [s4, s3, s2, s1, L.cpu_wr] + have hcpu5 : L.cpu s5 = L.cpu s := by rw [L.cpu_wr, hcpu4] + have hmem_other4 : ∀ q, Aligned q → + q ≠ wa vc 24#u64 → q ≠ wa vc 32#u64 → q ≠ wa vc 40#u64 → q ≠ runVcpuA ct → + L.mem s4 q = L.mem s q := fun q hq h1 h2 h3 h4 => by + simp only [s4, s3, s2, s1] + rw [mem_wr_neA hal_rv hq h4.symm, mem_wr_neA hal_40 hq h3.symm, + mem_wr_neA hal_32 hq h2.symm, mem_wr_neA hal_24 hq h1.symm] + have hmem_other5 : ∀ q, Aligned q → + q ≠ wa vc 24#u64 → q ≠ wa vc 32#u64 → q ≠ wa vc 40#u64 → q ≠ runVcpuA ct → q ≠ stA ct → + L.mem s5 q = L.mem s q := fun q hq h1 h2 h3 h4 h5 => by + rw [mem_wr_neA hal_st hq h5.symm]; exact hmem_other4 q hq h1 h2 h3 h4 + have hvc_page : ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → VcPage Z.V (wa vc o) := + fun o ho ho8 => ⟨vc, hvc, o, ho, ho8, rfl⟩ + -- Disjointness of the 4 writes from any TCB field (except `runVcpuA ct`) + have htcb_other4 : ∀ t ∈ Z.T, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → (t = ct → o ≠ 3672#u64) → + L.mem s4 (wa t o) = L.mem s (wa t o) := fun t ht o ho ho8 hne => + hmem_other4 (wa t o) (aligned_wa (X.vcpu.place.al_t t ht) o ho8) + (X.vcpu.place.disj_tcb vc hvc t ht 24#u64 o (by decide) ho).symm + (X.vcpu.place.disj_tcb vc hvc t ht 32#u64 o (by decide) ho).symm + (X.vcpu.place.disj_tcb vc hvc t ht 40#u64 o (by decide) ho).symm + (by rw [runVcpuA_eq]; exact field_ne (hU.tcb_al t ht) hta ho (by decide) (fun e ev => by subst e; exact hne rfl (UScalar.eq_of_val_eq ev))) + have htcb_other5 : ∀ t ∈ Z.T, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → + (t = ct → o ≠ 3672#u64 ∧ o ≠ 3328#u64) → + L.mem s5 (wa t o) = L.mem s (wa t o) := fun t ht o ho ho8 hne => + hmem_other5 (wa t o) (aligned_wa (X.vcpu.place.al_t t ht) o ho8) + (X.vcpu.place.disj_tcb vc hvc t ht 24#u64 o (by decide) ho).symm + (X.vcpu.place.disj_tcb vc hvc t ht 32#u64 o (by decide) ho).symm + (X.vcpu.place.disj_tcb vc hvc t ht 40#u64 o (by decide) ho).symm + (by rw [runVcpuA_eq]; exact field_ne (hU.tcb_al t ht) hta ho (by decide) (fun e ev => by subst e; exact (hne rfl).1 (UScalar.eq_of_val_eq ev))) + (by rw [stA_eq]; exact field_ne (hU.tcb_al t ht) hta ho (by decide) (fun e ev => by subst e; exact (hne rfl).2 (UScalar.eq_of_val_eq ev))) + -- 1. `sched.set_thread_state st s4 ct TS_RUNNING_VM = ok s5` + have hscC : L.mem s (scA ct) ∈ Z.C := X.j.1.rest.scMem ct hct hsc + have hsc4 : L.mem s4 (scA ct) = L.mem s (scA ct) := by + rw [scA_eq, htcb_other4 ct hct 3464#u64 (by decide) (by decide) (fun _ => by decide), ← scA_eq] + have hiq4 : L.mem s4 (inqA ct) = L.mem s (inqA ct) := by + rw [inqA_eq, htcb_other4 ct hct 3592#u64 (by decide) (by decide) (fun _ => by decide), ← inqA_eq] + have hsc_max4 : ∀ c ∈ Z.C, L.mem s4 (maxA c) = L.mem s (maxA c) := fun c hc => by + have hal_m := (hSc_st c hc).2.2.2 + refine hmem_other4 (maxA c) hal_m ?_ ?_ ?_ ?_ + · rw [maxA_eqA]; exact (X.vcpu.place.disj_sc vc hvc c hc 24#u64 72#u64 (by decide) (by decide)).symm + · rw [maxA_eqA]; exact (X.vcpu.place.disj_sc vc hvc c hc 32#u64 72#u64 (by decide) (by decide)).symm + · rw [maxA_eqA]; exact (X.vcpu.place.disj_sc vc hvc c hc 40#u64 72#u64 (by decide) (by decide)).symm + · rw [runVcpuA_eq, maxA_eqA]; exact (hLD.offSc ct hct 3672#u64 (Or.inr rfl) c hc).2.symm + have hsts_ok : sched.set_thread_state st s4 ct sched.TS_RUNNING_VM = ok s5 := + set_thread_state_vm_wrA hta (hsc4 ▸ hsc) + (by show L.mem s4 (maxA (L.mem s4 (scA ct))) ≠ 0#u64; rw [hsc4, hsc_max4 _ hscC]; exact hrm) + (hiq4 ▸ hiq) (by rw [hsc4]; exact (hSc_st _ hscC).2.2.2) (by rw [hsc4]; exact (hSc_st _ hscC).2.2.1) + -- 2. Spec step + have hsp_step : (do + fl4VcpuExtUpdate (Z := Unit) (specPtr vc) fun e => + { e with runCaller := some (specPtr ct), runOffset := offset.bv, runOutMask := out_mask.bv } + setRunVcpu (specPtr ct) (some (specPtr vc)) + setThreadState (specPtr ct) (.ext .runningVM)) σ = ({((), σ5)}, False) := by + have hkt1 : σ1.kheap (specPtr ct) = some (.tcb x0) := by rw [updKSc_other _ hne_vc_ct.symm]; exact hkt0 + have hkt2 : σ2.kheap (specPtr ct) = some (.tcb { x0 with tcbExt := { x0.tcbExt with tcbRunVcpu := some (specPtr vc) } }) := + updKSc_same _ _ _ + have hsts_sp : setThreadState (Z := Unit) (specPtr ct) (.ext .runningVM) σ2 = ({((), σ5)}, False) := by + have hst_eq : stUpdA σ2 (specPtr ct) { x0 with tcbExt := { x0.tcbExt with tcbRunVcpu := some (specPtr vc) } } (.ext .runningVM) = σ5 := by + unfold stUpdA setObjA; exact updKSc_idemA σ1 + rw [← hst_eq] + apply setThreadState_eqA σ2 (specPtr ct) _ hkt2 (.ext .runningVM) + intro _ _ + obtain ⟨x0', hkt0', -, hsc_eq⟩ := X.j.1.sch.tcb ct hct + rw [hkt0] at hkt0'; cases hkt0' + have hsc_some : x1.tcbSchedContext = some (scRenA m (L.mem s (scA ct))) := by + show x0.tcbSchedContext = _; rw [hsc_eq]; unfold optScA; simp [hsc] + obtain ⟨y, n, hy, hrm_eq⟩ := X.j.1.rest.refillMax _ hscC + have hne_sc_vc : scRenA m (L.mem s (scA ct)) ≠ specPtr vc := fun e => by rw [e, hkv0] at hy; cases hy + have hne_sc_ct : scRenA m (L.mem s (scA ct)) ≠ specPtr ct := fun e => by rw [e, hkt0] at hy; cases hy + have hy5 : σ5.kheap (scRenA m (L.mem s (scA ct))) = some (.schedContext y n) := by + rw [updKSc_other _ hne_sc_ct, updKSc_other _ hne_sc_vc]; exact hy + have hpos : 0 < y.scRefillMax := by + rw [hrm_eq]; by_contra h0; apply hrm; apply UScalar.eq_of_val_eq + unfold LawfulStore.rmax; rw [maxA_eqA] + exact Nat.eq_zero_of_not_pos h0 + exact schedulable_ofA + (by change (stUpdA σ2 (specPtr ct) _ (.ext .runningVM)).kheap _ = _; rw [hst_eq]; exact updKSc_same σ1 (specPtr ct) (.tcb x1)) + rfl hsc_some + (by change (stUpdA σ2 (specPtr ct) _ (.ext .runningVM)).kheap _ = _; rw [hst_eq]; exact hy5) + hpos hNotRel + rw [nd_bind_oneSc (fl4VcpuExtUpdate_eqA _ hkv0), + nd_bind_oneSc (setRunVcpu_eqA _ hkt1)] + exact hsts_sp + -- 3. Disjointness helpers for `jAtZA_of_vcpuLinkStepA` (WAIT: `stA ct` changes! So handle `JAtZAX` via `set_state` style or direct `jAtZA`!) + -- Even simpler: first preserve `VcpuXferStateA` across `s → s4` and `σ → σ2` (where `stA ct` is UNCHANGED!), + -- then apply `VcpuXferStateA.set_state` for `s4 → s5` (`TS_RUNNING_VM`)! + have hslot4 : ∀ y, SlotWA Z.th y → + L.mem s4 y = L.mem s y ∧ L.mem s4 (wa y 8#u64) = L.mem s (wa y 8#u64) ∧ + L.mem s4 (wa y 16#u64) = L.mem s (wa y 16#u64) ∧ L.mem s4 (wa y 24#u64) = L.mem s (wa y 24#u64) := fun y hy => by + have hsw24 := hSw _ (hvc_page 24#u64 (by decide) (by decide)) y hy + have hsw32 := hSw _ (hvc_page 32#u64 (by decide) (by decide)) y hy + have hsw40 := hSw _ (hvc_page 40#u64 (by decide) (by decide)) y hy + have hld := hLD.offSlot ct hct 3672#u64 (Or.inr rfl) y hy + have hal0 : Aligned y := hfa y (FootA.slot y y hy (Or.inl rfl)) + have hal8 : Aligned (wa y 8#u64) := hfa _ (FootA.slot y _ hy (Or.inr (Or.inl rfl))) + have hal16 : Aligned (wa y 16#u64) := hfa _ (FootA.slot y _ hy (Or.inr (Or.inr (Or.inl rfl)))) + have hal24 : Aligned (wa y 24#u64) := hfa _ (FootA.slot y _ hy (Or.inr (Or.inr (Or.inr rfl)))) + exact ⟨hmem_other4 y hal0 hsw24.1.symm hsw32.1.symm hsw40.1.symm (by rw [runVcpuA_eq]; exact hld.1.symm), + hmem_other4 _ hal8 hsw24.2.1.symm hsw32.2.1.symm hsw40.2.1.symm (by rw [runVcpuA_eq]; exact hld.2.1.symm), + hmem_other4 _ hal16 hsw24.2.2.1.symm hsw32.2.2.1.symm hsw40.2.2.1.symm (by rw [runVcpuA_eq]; exact hld.2.2.1.symm), + hmem_other4 _ hal24 hsw24.2.2.2.symm hsw32.2.2.2.symm hsw40.2.2.2.symm (by rw [runVcpuA_eq]; exact hld.2.2.2.symm)⟩ + have hos4 : ∀ y ∈ OS, + L.mem s4 y = L.mem s y ∧ L.mem s4 (wa y 8#u64) = L.mem s (wa y 8#u64) ∧ + L.mem s4 (wa y 16#u64) = L.mem s (wa y 16#u64) ∧ L.mem s4 (wa y 24#u64) = L.mem s (wa y 24#u64) := fun y hy => by + have hal0 := X.vcpu.place.al_os y hy + have hd24 := X.vcpu.place.disj_os _ (hvc_page 24#u64 (by decide) (by decide)) y hy + have hd32 := X.vcpu.place.disj_os _ (hvc_page 32#u64 (by decide) (by decide)) y hy + have hd40 := X.vcpu.place.disj_os _ (hvc_page 40#u64 (by decide) (by decide)) y hy + have hto := hOS_tcb ct hct 3672#u64 (Or.inr rfl) y hy + exact ⟨hmem_other4 y hal0 hd24.1.symm hd32.1.symm hd40.1.symm (by rw [runVcpuA_eq]; exact hto.1.symm), + hmem_other4 _ (aligned_wa hal0 8#u64 (by decide)) hd24.2.1.symm hd32.2.1.symm hd40.2.1.symm (by rw [runVcpuA_eq]; exact hto.2.1.symm), + hmem_other4 _ (aligned_wa hal0 16#u64 (by decide)) hd24.2.2.1.symm hd32.2.2.1.symm hd40.2.2.1.symm (by rw [runVcpuA_eq]; exact hto.2.2.1.symm), + hmem_other4 _ (aligned_wa hal0 24#u64 (by decide)) hd24.2.2.2.symm hd32.2.2.2.symm hd40.2.2.2.symm (by rw [runVcpuA_eq]; exact hto.2.2.2.symm)⟩ + have hks_ne4 : ∀ o : U64, o.val < ksSizeA → Aligned (wa (L.ksb s) o) → + L.mem s4 (wa (L.ksb s) o) = L.mem s (wa (L.ksb s) o) := fun o ho hal_o => by + have hvc_ks : ∀ k : U64, k.val < 4096 → k.val % 8 = 0 → wa (L.ksb s) o ≠ wa vc k := fun k hk hk8 e => by + have hfit := X.j.1.ksFit + have hval : (wa (L.ksb s) o).val = (L.ksb s).val + o.val := by + rw [wa_valP]; exact Nat.mod_eq_of_lt (by omega) + exact X.vcpu.place.disj_ks _ (hvc_page k hk hk8) (by rw [← e, hval]; omega) + exact hmem_other4 _ hal_o (hvc_ks 24#u64 (by decide) (by decide)) + (hvc_ks 32#u64 (by decide) (by decide)) (hvc_ks 40#u64 (by decide) (by decide)) + (by rw [runVcpuA_eq]; exact (hLD.offKs ct hct 3672#u64 (Or.inr rfl) o ho).symm) + have hnode_eq4 : ∀ c o, c.val < 128 → o.val < 4288 → Aligned (L.nodeAt s c o) → + L.mem s4 (L.nodeAt s c o) = L.mem s (L.nodeAt s c o) := fun c o hc ho hal_n => by + have h1 : (L.nodeAt s c o).val = ((L.ksb s).val + (223488 + c.val * 4288 + o.val)) % 2 ^ 64 := by + have := nodeAt_valA (L := L) s c o + have hmod : (L.ksb s).val + 223488 + c.val * 4288 + o.val = (L.ksb s).val + (223488 + c.val * 4288 + o.val) := by omega + rw [this, hmod] + let k : U64 := ⟨BitVec.ofNat 64 (223488 + c.val * 4288 + o.val)⟩ + have hk : k.val < ksSizeA := by + have hv : k.val = (223488 + c.val * 4288 + o.val) % 2 ^ 64 := BitVec.toNat_ofNat _ _ + rw [hv, Nat.mod_eq_of_lt (by omega)]; unfold ksSizeA; omega + have heq : L.nodeAt s c o = wa (L.ksb s) k := by + apply UScalar.eq_of_val_eq; rw [h1, wa_valP] + have hv : k.val = 223488 + c.val * 4288 + o.val := by + change (BitVec.ofNat 64 _).toNat = _; rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + rw [hv] + rw [heq] at hal_n ⊢; exact hks_ne4 k hk hal_n + have hTy4 : TyKeepA σ σ2 := by + have h1 : TyKeepA σ σ1 := TyKeepA.upd hkv0 (fun _ _ e => by cases e) (fun _ e => by cases e) + have hkt1 : σ1.kheap (specPtr ct) = some (.tcb x0) := by rw [updKSc_other _ hne_vc_ct.symm]; exact hkt0 + have h2 : TyKeepA σ1 σ2 := TyKeepA.upd hkt1 (fun _ _ e => by cases e) (fun _ _ => ⟨x0, rfl⟩) + exact ⟨fun p n cs h => let ⟨cs1, h1'⟩ := h2.1 p n cs h; h1.1 p n cs1 h1', + fun p t h => let ⟨t1, h1'⟩ := h2.2 p t h; h1.2 p t1 h1'⟩ + obtain ⟨hJ4, hS2_4, hHV_4, hTr4, hCar4, hF4⟩ := jAtZA_of_vcpuLinkStepA (σ' := σ2) X hksb4 hcpu4 + hslot4 hos4 + (fun c hc => ⟨hnode_eq4 c _ (hCs c hc) (by unfold layout.node.CUR_THREAD; decide) (hfa _ (FootA.nslot _ (FootNA.node c _ hc (Or.inl rfl)))), + hnode_eq4 c _ (hCs c hc) (by unfold layout.node.IDLE_THREAD; decide) (hfa _ (FootA.nslot _ (FootNA.node c _ hc (Or.inr (Or.inl rfl))))), + hnode_eq4 c _ (hCs c hc) (by unfold layout.node.CUR_SC; decide) (hfa _ (FootA.nslot _ (FootNA.node c _ hc (Or.inr (Or.inr (Or.inl rfl)))))), + hnode_eq4 c _ (hCs c hc) (by unfold layout.node.IDLE_SC; decide) (hfa _ (FootA.nslot _ (FootNA.node c _ hc (Or.inr (Or.inr (Or.inr rfl))))))⟩) + (fun t ht => ⟨by rw [stA_eq, htcb_other4 t ht 3328#u64 (by decide) (by decide) (fun _ => by decide), ← stA_eq], + by rw [scA_eq, htcb_other4 t ht 3464#u64 (by decide) (by decide) (fun _ => by decide), ← scA_eq], + by unfold affA; exact htcb_other4 t ht 3520#u64 (by decide) (by decide) (fun _ => by decide), + by unfold prioA; exact htcb_other4 t ht 3448#u64 (by decide) (by decide) (fun _ => by decide), + by unfold domA; exact htcb_other4 t ht 3432#u64 (by decide) (by decide) (fun _ => by decide)⟩) + (fun c hc => ⟨hmem_other4 _ (hfa _ (FootA.nslot _ (FootNA.sc c hc))) + (X.vcpu.place.disj_sc vc hvc c hc 24#u64 24#u64 (by decide) (by decide)).symm + (X.vcpu.place.disj_sc vc hvc c hc 32#u64 24#u64 (by decide) (by decide)).symm + (X.vcpu.place.disj_sc vc hvc c hc 40#u64 24#u64 (by decide) (by decide)).symm + (by rw [runVcpuA_eq]; exact (hLD.offSc ct hct 3672#u64 (Or.inr rfl) c hc).1.symm), + hmem_other4 _ (aligned_wa (aligned_of_wa24A (hfa _ (FootA.nslot _ (FootNA.sc c hc)))) 72#u64 (by decide)) + (X.vcpu.place.disj_sc vc hvc c hc 24#u64 72#u64 (by decide) (by decide)).symm + (X.vcpu.place.disj_sc vc hvc c hc 32#u64 72#u64 (by decide) (by decide)).symm + (X.vcpu.place.disj_sc vc hvc c hc 40#u64 72#u64 (by decide) (by decide)).symm + (by rw [runVcpuA_eq]; exact (hLD.offSc ct hct 3672#u64 (Or.inr rfl) c hc).2.symm)⟩) + ⟨hnode_eq4 _ _ hcpu (by unfold layout.node.SCHED_ACTION; decide) (hfa _ (FootA.nslot _ (FootNA.cur _ (Or.inl rfl)))), + hnode_eq4 _ _ hcpu (by unfold layout.node.CUR_TIME; decide) (hfa _ (FootA.nslot _ (FootNA.cur _ (Or.inr rfl)))), + hnode_eq4 _ _ hcpu (by unfold layout.node.CONSUMED; decide) (hfa _ (FootA.nslot _ (FootNA.cur2 _ (Or.inl rfl)))), + hnode_eq4 _ _ hcpu (by unfold layout.node.REPROGRAM; decide) (hfa _ (FootA.nslot _ (FootNA.cur2 _ (Or.inr rfl))))⟩ + ⟨hks_ne4 8#u64 (by unfold ksSizeA; decide) (hfa _ (FootA.nslot _ (FootNA.glob _ (Or.inl rfl)))), + hks_ne4 16#u64 (by unfold ksSizeA; decide) (hfa _ (FootA.nslot _ (FootNA.glob _ (Or.inr rfl))))⟩ + (fun i hi => by + have hlt_o : (irqOffA i).val < ksSizeA := by + unfold irqOffA ksSizeA + simp only [core.num.U64.wrapping_add_val_eq, core.num.U64.wrapping_mul_val_eq, + UScalar.size, UScalarTy.numBits] + have h224 : (224#u64).val = 224 := rfl + have h8 : (8#u64).val = 8 := rfl + rw [h224, h8]; omega + have hal_i : Aligned (irqWordA (L.ksb s) i) := hfa _ (FootA.nslot _ (FootNA.irq i hi)) + unfold irqWordA at hal_i ⊢; exact hks_ne4 (irqOffA i) hlt_o hal_i) + (fun b hb => hmem_other4 b (hfa b (FootA.nslot b (FootNA.vs b hb))) + (fun e => X.vcpu.place.disj_vs _ (hvc_page 24#u64 (by decide) (by decide)) (e ▸ hb)) + (fun e => X.vcpu.place.disj_vs _ (hvc_page 32#u64 (by decide) (by decide)) (e ▸ hb)) + (fun e => X.vcpu.place.disj_vs _ (hvc_page 40#u64 (by decide) (by decide)) (e ▸ hb)) + (fun e => hLD.offVs ct hct 3672#u64 (Or.inr rfl) (by rw [← runVcpuA_eq, ← e]; exact hb))) + (fun b hb => hmem_other4 b (hfa b (FootA.nslot b (FootNA.ext b hb))) + (fun e => hExt _ (hvc_page 24#u64 (by decide) (by decide)) (e ▸ hb)) + (fun e => hExt _ (hvc_page 32#u64 (by decide) (by decide)) (e ▸ hb)) + (fun e => hExt _ (hvc_page 40#u64 (by decide) (by decide)) (e ▸ hb)) + (fun e => hLD.offExt ct hct 3672#u64 (Or.inr rfl) (by rw [← runVcpuA_eq, ← e]; exact hb))) + (fun b hb => hmem_other4 b (X.fmem.aligned b hb) + (fun e => X.vcpu.place.disj_ub _ (hvc_page 24#u64 (by decide) (by decide)) (e ▸ hb)) + (fun e => X.vcpu.place.disj_ub _ (hvc_page 32#u64 (by decide) (by decide)) (e ▸ hb)) + (fun e => X.vcpu.place.disj_ub _ (hvc_page 40#u64 (by decide) (by decide)) (e ▸ hb)) + (fun e => X.fmem.disj_tcb b hb ct hct 3672#u64 (by decide) (by rw [← runVcpuA_eq]; exact e))) + ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, ⟨rfl, rfl, rfl, rfl⟩⟩ + rfl rfl ⟨rfl, rfl⟩ + (fun p hpv hpt => by rw [updKSc_other _ (hpt ct hct).symm, updKSc_other _ (hpv vc hvc).symm]) + (fun t ht x hkx => by + by_cases hteq : t = ct + · subst hteq; rw [hkt0] at hkx; cases hkx + exact ⟨_, updKSc_same _ _ _, rfl, rfl, rfl, rfl, fun _ _ => rfl⟩ + · have hne1 : specPtr t ≠ specPtr ct := fun e => hteq (specPtr_inj e) + have hne2 : specPtr t ≠ specPtr vc := fun e => by rw [e, hkv0] at hkx; cases hkx + exact ⟨x, by rw [updKSc_other _ hne1, updKSc_other _ hne2]; exact hkx, rfl, rfl, rfl, rfl, fun _ _ => rfl⟩) + hTy4 + (fun p es hkp => by + have hne1 : p ≠ specPtr ct := fun e => by subst e; rw [updKSc_same] at hkp; cases hkp + have hne2 : p ≠ specPtr vc := fun e => by subst e; rw [updKSc_other _ hne1, updKSc_same] at hkp; cases hkp + rwa [updKSc_other _ hne1, updKSc_other _ hne2] at hkp) + (fun p hv hkp => by + have hne1 : p ≠ specPtr ct := fun e => by subst e; rw [updKSc_same] at hkp; cases hkp + have hne2 : p ≠ specPtr vc := fun e => by subst e; rw [updKSc_other _ hne1, updKSc_same] at hkp; cases hkp + rwa [updKSc_other _ hne1, updKSc_other _ hne2] at hkp) + hsub + -- Step `s4 → s5` (`stA ct := TS_RUNNING_VM`) on `JAtZA`, `VcpuRelA`, `TreeA`, `CarryA`, `FrameMemRelA`, `TcbRegsRelA` + have hnd5_eq : ∀ c : U64, c.val < 128 → ∀ k : U64, k.val < 128 → k.val % 8 = 0 → + L.mem s5 (L.nodeAt s4 c k) = L.mem s4 (L.nodeAt s4 c k) := fun c hc k hk hk8 => by + obtain ⟨o, ho, heq⟩ := nodeAt_eq_wa_ksbA (L := L) s4 c k hc (by omega) + rw [hksb4] at heq + have hal_nd : Aligned (L.nodeAt s4 c k) := by rw [nodeAt_ksb hksb4]; exact X.vcpu.place.node_al c k hk8 + exact mem_wr_neA hal_st hal_nd (by rw [heq]; exact hKs_st o ho) + have hcap_os5 : ∀ y ∈ OS, capAtA (L.mem s5) y = capAtA (L.mem s4) y := fun y hy => by + obtain ⟨h0, h8, -, -⟩ := hOS_st y hy + have hya := hU.al_os y hy + unfold capAtA; rw [mem_wr_neA hal_st hya h0, mem_wr_neA hal_st (aligned_wa hya 8#u64 (by decide)) h8] + have hcap_sl5 : ∀ y, SlotWA Z.th y → capAtA (L.mem s5) y = capAtA (L.mem s4) y := fun y hy => by + obtain ⟨h0, h8, -, -⟩ := hSlotSt y hy + unfold capAtA + rw [mem_wr_neA hal_st (hfa _ (.slot y _ hy (Or.inl rfl))) h0, + mem_wr_neA hal_st (hfa _ (.slot y _ hy (Or.inr (Or.inl rfl)))) h8] + have hrev_sl5 : ∀ y, SlotWA Z.th y → revA (L.mem s5) y = revA (L.mem s4) y := fun y hy => by + obtain ⟨-, -, -, h24⟩ := hSlotSt y hy + unfold revA; rw [mem_wr_neA hal_st (hfa _ (.slot y _ hy (Or.inr (Or.inr (Or.inr rfl))))) h24] + have hos5_4 : OSKeep st OS s4 s5 := fun y hy => by + obtain ⟨h0, h8, h16, h24⟩ := hOS_st y hy + have hya := hU.al_os y hy + exact ⟨mem_wr_neA hal_st hya h0, + mem_wr_neA hal_st (aligned_wa hya 8#u64 (by decide)) h8, + mem_wr_neA hal_st (aligned_wa hya 16#u64 (by decide)) h16, + mem_wr_neA hal_st (aligned_wa hya 24#u64 (by decide)) h24⟩ + have hkt2_same : σ2.kheap (specPtr ct) = some (.tcb { x0 with tcbExt := { x0.tcbExt with tcbRunVcpu := some (specPtr vc) } }) := + updKSc_same _ _ _ + have hσ5_eq_st : σ5 = stUpdA σ2 (specPtr ct) { x0 with tcbExt := { x0.tcbExt with tcbRunVcpu := some (specPtr vc) } } (.ext .runningVM) := by + unfold stUpdA setObjA; exact (updKSc_idemA σ1).symm + have hTy5 : TyKeepA σ2 σ5 := by + rw [hσ5_eq_st]; exact TyKeepA.upd hkt2_same (fun _ _ e => by cases e) (fun _ _ => ⟨_, rfl⟩) + have hC5 : ∀ c ∈ Z.C, σ5.kheap (scRenA m c) = σ2.kheap (scRenA m c) := fun c hc => by + have hne : scRenA m c ≠ specPtr ct := fun e => by + obtain ⟨xc, n, hkc0, -⟩ := hJ4.sch.sc c hc; rw [e, hkt2_same] at hkc0; cases hkc0 + rw [hσ5_eq_st]; unfold stUpdA setObjA; simp [Function.update, hne] + have hT5_other : ∀ t ∈ Z.T, t ≠ ct → σ5.kheap (specPtr t) = σ2.kheap (specPtr t) := fun t _ hne => by + rw [hσ5_eq_st]; unfold stUpdA setObjA; exact Function.update_of_ne (fun e => hne (specPtr_inj e)) _ _ + have hSt5 : StateRelA Z.th (L.mem s5) σ5 := by + refine ⟨fun p r hp => ?_, fun t ht => ?_⟩ + · obtain ⟨cs, hkc0, hwf, hslots⟩ := hJ4.st.cnode p r hp + have hne : specPtr p ≠ specPtr ct := fun e => by rw [e, hkt2_same] at hkc0; cases hkc0 + refine ⟨cs, by rw [hσ5_eq_st]; unfold stUpdA setObjA; simp [Function.update, hne, hkc0], hwf, fun idx hidx => ?_⟩ + have hsr := hslots idx hidx + unfold SlotRelA at hsr ⊢; rw [hcap_sl5 _ (Or.inr ⟨p, r, hp, idx, hidx, rfl⟩)]; exact hsr + · obtain ⟨xt, hkt, hslots⟩ := hJ4.st.tcb t ht + have htT : t ∈ Z.T := (hJ4.thT t).1 ht + by_cases et : t = ct + · subst t; rw [hkt2_same] at hkt; cases hkt + refine ⟨x1, updKSc_same _ _ _, fun i hi => ?_⟩ + have hsr := hslots i hi + unfold SlotRelA at hsr ⊢; rw [hcap_sl5 _ (Or.inl ⟨ct, Or.inr ⟨ht, i, hi, rfl⟩⟩)]; exact hsr + · refine ⟨xt, by rw [hT5_other t htT et]; exact hkt, fun i hi => ?_⟩ + have hsr := hslots i hi + unfold SlotRelA at hsr ⊢; rw [hcap_sl5 _ (Or.inl ⟨t, Or.inr ⟨ht, i, hi, rfl⟩⟩)]; exact hsr + have hCdt5 : CdtRelA st Z.th s5 σ5 Z.G := by + have h0 := hJ4.cdt + refine ⟨h0.tree.keepO (fun y hy => by + obtain ⟨h0w, h8w, h16w, h24w⟩ := hSlotSt y (Or.inl hy) + exact ⟨mem_wr_neA hal_st (hfa _ (.slot y _ (Or.inl hy) (Or.inl rfl))) h0w, + mem_wr_neA hal_st (hfa _ (.slot y _ (Or.inl hy) (Or.inr (Or.inl rfl)))) h8w, + mem_wr_neA hal_st (hfa _ (.slot y _ (Or.inl hy) (Or.inr (Or.inr (Or.inl rfl))))) h16w, + mem_wr_neA hal_st (hfa _ (.slot y _ (Or.inl hy) (Or.inr (Or.inr (Or.inr rfl))))) h24w⟩), + h0.cdt, h0.cdt_slot, fun sl hsl => by rw [hrev_sl5 _ (absSlot_w hsl)]; exact h0.orig sl hsl, + h0.par_slot, fun a' p' hp' => by + have hp_sl : SlotWA Z.th p' := by + obtain ⟨sl, hsl, hsl_a⟩ := h0.par_slot a' p' hp' + have hcdt := h0.cdt sl hsl; rw [hsl_a, hp'] at hcdt + cases hq : σ2.cdt sl with + | none => rw [hq] at hcdt; cases hcdt + | some q => + rw [hq] at hcdt; simp only [Option.map_some, Option.some.injEq] at hcdt + rw [← hcdt]; exact absSlot_w (h0.cdt_slot sl q hq).2 + rw [hrev_sl5 p' hp_sl]; exact h0.prev a' p' hp'⟩ + have hSch5 : SchedRelA (L := L) Z.T Z.C Z.Cs Z.curOf Z.curScOf s5 σ5 m := by + have h0 := hJ4.sch + have hnode5 : ∀ c ∈ Z.Cs, + L.mem s5 (L.nodeAt s5 c layout.node.CUR_THREAD) = L.mem s4 (L.nodeAt s4 c layout.node.CUR_THREAD) ∧ + L.mem s5 (L.nodeAt s5 c layout.node.IDLE_THREAD) = L.mem s4 (L.nodeAt s4 c layout.node.IDLE_THREAD) ∧ + L.mem s5 (L.nodeAt s5 c layout.node.CUR_SC) = L.mem s4 (L.nodeAt s4 c layout.node.CUR_SC) ∧ + L.mem s5 (L.nodeAt s5 c layout.node.IDLE_SC) = L.mem s4 (L.nodeAt s4 c layout.node.IDLE_SC) := fun c hc => by + have hksb5_4 : L.ksb s5 = L.ksb s4 := L.ksb_wr s4 (stA ct) sched.TS_RUNNING_VM + rw [nodeAt_ksb hksb5_4, nodeAt_ksb hksb5_4, nodeAt_ksb hksb5_4, nodeAt_ksb hksb5_4] + exact ⟨hnd5_eq c (hCs c hc) layout.node.CUR_THREAD (by unfold layout.node.CUR_THREAD; decide) (by unfold layout.node.CUR_THREAD; decide), + hnd5_eq c (hCs c hc) layout.node.IDLE_THREAD (by unfold layout.node.IDLE_THREAD; decide) (by unfold layout.node.IDLE_THREAD; decide), + hnd5_eq c (hCs c hc) layout.node.CUR_SC (by unfold layout.node.CUR_SC; decide) (by unfold layout.node.CUR_SC; decide), + hnd5_eq c (hCs c hc) layout.node.IDLE_SC (by unfold layout.node.IDLE_SC; decide) (by unfold layout.node.IDLE_SC; decide)⟩ + have hact5 : L.actionW s5 = L.actionW s4 := by + unfold LawfulStore.actionW; rw [L.cpu_wr, nodeAt_ksb (L.ksb_wr _ _ _)] + exact hnd5_eq (L.cpu s4) (hcpu4 ▸ hcpu) layout.node.SCHED_ACTION (by unfold layout.node.SCHED_ACTION; decide) (by unfold layout.node.SCHED_ACTION; decide) + have htime5 : L.curTime s5 = L.curTime s4 := by + unfold LawfulStore.curTime LawfulStore.nodeA; rw [L.cpu_wr, nodeAt_ksb (L.ksb_wr _ _ _)] + exact hnd5_eq (L.cpu s4) (hcpu4 ▸ hcpu) layout.node.CUR_TIME (by unfold layout.node.CUR_TIME; decide) (by unfold layout.node.CUR_TIME; decide) + exact { + core := by rw [L.cpu_wr]; exact h0.core + cpuCs := by rw [L.cpu_wr]; exact h0.cpuCs + curOfW := fun c hc => by rw [(hnode5 c hc).1]; exact h0.curOfW c hc + curScOfW := fun c hc => by rw [(hnode5 c hc).2.2.1]; exact h0.curScOfW c hc + cur := h0.cur + csc := h0.csc + idle := by unfold LawfulStore.idleW; rw [L.cpu_wr, nodeAt_ksb (L.ksb_wr _ _ _), hnd5_eq (L.cpu s4) (hcpu4 ▸ hcpu) layout.node.IDLE_THREAD (by unfold layout.node.IDLE_THREAD; decide) (by unfold layout.node.IDLE_THREAD; decide)]; exact h0.idle + idleOf := fun c hc => by rw [(hnode5 c hc).2.1]; exact h0.idleOf c hc + idleScOf := fun c hc => by rw [(hnode5 c hc).2.2.2]; exact h0.idleScOf c hc + act := by rw [hact5]; exact h0.act + time := by rw [htime5]; exact h0.time + aff := fun t ht => by + rw [mem_wr_neA hal_st (by unfold affA; exact aligned_wa (X.vcpu.place.al_t t ht) 3520#u64 (by decide)) + (by rw [stA_eq]; unfold affA; exact field_ne hta (hU.tcb_al t ht) (by decide) (by decide) (fun _ => by decide))] + exact h0.aff t ht + affT := h0.affT + tcb := fun t ht => by + obtain ⟨xt, hkt, hst0, hsc0⟩ := h0.tcb t ht + have hsc_eq : L.mem s5 (scA t) = L.mem s4 (scA t) := + mem_wr_neA hal_st (by unfold scA; exact aligned_wa (X.vcpu.place.al_t t ht) 3464#u64 (by decide)) + (by rw [stA_eq]; unfold scA; exact field_ne hta (hU.tcb_al t ht) (by decide) (by decide) (fun _ => by decide)) + by_cases et : t = ct + · subst t; rw [hkt2_same] at hkt; cases hkt + refine ⟨x1, updKSc_same _ _ _, ?_, by rw [hsc_eq]; exact hsc0⟩ + rw [L.mem_wr_same hal_st]; dsimp only [x1]; unfold sched.TS_RUNNING_VM tsCodeA; decide + · have hst_eq' : L.mem s5 (stA t) = L.mem s4 (stA t) := + mem_wr_neA hal_st (by rw [stA_eq]; exact aligned_wa (X.vcpu.place.al_t t ht) 3328#u64 (by decide)) + (by rw [stA_eq, stA_eq]; exact field_ne hta (hU.tcb_al t ht) (by decide) (by decide) (fun e => absurd e.symm et)) + exact ⟨xt, by rw [hT5_other t ht et]; exact hkt, by rw [hst_eq']; exact hst0, by rw [hsc_eq]; exact hsc0⟩ + sc := fun c hc => by + rw [hC5 c hc, mem_wr_neA hal_st (hfa _ (.nslot _ (.sc c hc))) (hSc_st c hc).1] + exact h0.sc c hc + scOff := h0.scOff } + have hIrq5 : IrqRelA (L.mem s5) (L.ksb s5) σ5 := by + have h0 : IrqRelA (L.mem s4) (L.ksb s4) σ5 := + hJ4.irq.of_spec ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, ⟨rfl, rfl, rfl, rfl⟩⟩ + exact { + st := fun i hi => by + rw [hksb5, ← hksb4] + have hlt_o : (irqOffA i).val < ksSizeA := by + unfold irqOffA ksSizeA + simp only [core.num.U64.wrapping_add_val_eq, core.num.U64.wrapping_mul_val_eq, + UScalar.size, UScalarTy.numBits] + have h224 : (224#u64).val = 224 := rfl + have h8 : (8#u64).val = 8 := rfl + rw [h224, h8]; omega + rw [mem_wr_neA hal_st (hfa _ (FootA.nslot _ (by rw [hksb4]; exact FootNA.irq i hi))) + (by unfold irqWordA; rw [hksb4]; exact hKs_st (irqOffA i) hlt_o)] + exact h0.st i hi + node := by rw [L.ksb_wr]; exact h0.node + nodeHi := by rw [L.ksb_wr]; exact h0.nodeHi } + have hRest5 : SchedRestA (L := L) Z s5 σ5 m := by + have h0 := hJ4.rest + exact { + rq := fun p hp => by rw [L.cpu_wr]; exact h0.rq p hp + rqDom := h0.rqDom + rel := by rw [L.cpu_wr]; exact h0.rel + consumed := by rw [L.cpu_wr, nodeAt_ksb (L.ksb_wr _ _ _), hnd5_eq (L.cpu s4) (hcpu4 ▸ hcpu) layout.node.CONSUMED (by unfold layout.node.CONSUMED; decide) (by unfold layout.node.CONSUMED; decide)]; exact h0.consumed + repr := by rw [L.cpu_wr, nodeAt_ksb (L.ksb_wr _ _ _), hnd5_eq (L.cpu s4) (hcpu4 ▸ hcpu) layout.node.REPROGRAM (by unfold layout.node.REPROGRAM; decide) (by unfold layout.node.REPROGRAM; decide)]; exact h0.repr + curDom := by rw [hksb5, mem_wr_neA hal_st (hfa _ (.nslot _ (.glob _ (Or.inl rfl)))) (hKs_st 8#u64 (by unfold ksSizeA; decide)), ← hksb4]; exact h0.curDom + domTime := by rw [hksb5, mem_wr_neA hal_st (hfa _ (.nslot _ (.glob _ (Or.inr rfl)))) (hKs_st 16#u64 (by unfold ksSizeA; decide)), ← hksb4]; exact h0.domTime + prio := fun t ht => by + obtain ⟨xt, hkt, h1, h2, h3⟩ := h0.prio t ht + have hp_eq : L.mem s5 (prioA t) = L.mem s4 (prioA t) := + mem_wr_neA hal_st (by unfold prioA; exact aligned_wa (X.vcpu.place.al_t t ht) 3448#u64 (by decide)) + (by rw [stA_eq]; unfold prioA; exact field_ne hta (hU.tcb_al t ht) (by decide) (by decide) (fun _ => by decide)) + have hd_eq : L.mem s5 (domA t) = L.mem s4 (domA t) := + mem_wr_neA hal_st (by unfold domA; exact aligned_wa (X.vcpu.place.al_t t ht) 3432#u64 (by decide)) + (by rw [stA_eq]; unfold domA; exact field_ne hta (hU.tcb_al t ht) (by decide) (by decide) (fun _ => by decide)) + by_cases et : t = ct + · subst t; rw [hkt2_same] at hkt; cases hkt + exact ⟨x1, updKSc_same _ _ _, h1, by rw [hd_eq]; exact h2, by rw [hp_eq]; exact h3⟩ + · exact ⟨xt, by rw [hT5_other t ht et]; exact hkt, h1, by rw [hd_eq]; exact h2, by rw [hp_eq]; exact h3⟩ + scMem := fun t ht hne => by + have hsc_eq : L.mem s5 (scA t) = L.mem s4 (scA t) := + mem_wr_neA hal_st (by unfold scA; exact aligned_wa (X.vcpu.place.al_t t ht) 3464#u64 (by decide)) + (by rw [stA_eq]; unfold scA; exact field_ne hta (hU.tcb_al t ht) (by decide) (by decide) (fun _ => by decide)) + rw [hsc_eq] at hne ⊢; exact h0.scMem t ht hne + refillMax := fun c hc => by + obtain ⟨y, n, hkc, hrm'⟩ := h0.refillMax c hc + refine ⟨y, n, by rw [hC5 c hc]; exact hkc, ?_⟩ + rw [← maxA_eqA, mem_wr_neA hal_st (hSc_st c hc).2.2.2 (hSc_st c hc).2.2.1, maxA_eqA]; exact hrm' } + have hVs_kh5 : ∀ q, q ∈ Z.Pp ∨ q ∈ Z.S1 ∨ q ∈ Z.Y → σ5.kheap (specPtr q) = σ2.kheap (specPtr q) := fun q hq => by + have hne : specPtr q ≠ specPtr ct := fun e => by + rcases hq with hq | hq | hq + · obtain ⟨ap, hkp, -⟩ := hJ4.vs.pool q hq; rw [e, hkt2_same] at hkp; cases hkp + · obtain ⟨pt, hkp, -⟩ := hJ4.vs.pt q hq; rw [e, hkt2_same] at hkp; cases hkp + · obtain ⟨l1, hkp⟩ := hHV_4.obj q hq; rw [e, hkt2_same] at hkp; cases hkp + rw [hσ5_eq_st]; unfold stUpdA setObjA; simp [Function.update, hne] + have hExt_kh5 : ∀ q, q ∈ Z.Pp ∨ q ∈ Z.X ∨ q ∈ Z.Y ∨ q ∈ Z.T2 → σ5.kheap (specPtr q) = σ2.kheap (specPtr q) := fun q hq => by + have hne : specPtr q ≠ specPtr ct := fun e => by + rcases hq with hq | hq | hq | hq + · obtain ⟨ap, hkp, -⟩ := hJ4.vs.pool q hq; rw [e, hkt2_same] at hkp; cases hkp + · obtain ⟨es, hkp⟩ := hS2_4.obj q hq; rw [e, hkt2_same] at hkp; cases hkp + · obtain ⟨l1, hkp⟩ := hHV_4.obj q hq; rw [e, hkt2_same] at hkp; cases hkp + · obtain ⟨pt, hkp, -⟩ := hJ4.ext.s2pt q hq; rw [e, hkt2_same] at hkp; cases hkp + rw [hσ5_eq_st]; unfold stUpdA setObjA; simp [Function.update, hne] + have hJ5 : JAtZA (L := L) AR Z s5 a σ5 m := { + st := hSt5, cdt := hCdt5, sch := hSch5, irq := hIrq5, rest := hRest5, ar := hJ4.ar, + place := by rw [L.ksb_wr]; exact hJ4.place, thT := hJ4.thT, csN := hJ4.csN, + idleT := fun c hc => by + rw [nodeAt_ksb (L.ksb_wr _ _ _), hnd5_eq c (hCs c hc) layout.node.IDLE_THREAD (by unfold layout.node.IDLE_THREAD; decide) (by unfold layout.node.IDLE_THREAD; decide)] + exact hJ4.idleT c hc + idleC := fun c hc => by + rw [nodeAt_ksb (L.ksb_wr _ _ _), hnd5_eq c (hCs c hc) layout.node.IDLE_SC (by unfold layout.node.IDLE_SC; decide) (by unfold layout.node.IDLE_SC; decide)] + exact hJ4.idleC c hc + curT := fun c hc => by + rw [nodeAt_ksb (L.ksb_wr _ _ _), hnd5_eq c (hCs c hc) layout.node.CUR_THREAD (by unfold layout.node.CUR_THREAD; decide) (by unfold layout.node.CUR_THREAD; decide)] + exact hJ4.curT c hc + cscC := fun c hc => by + rw [nodeAt_ksb (L.ksb_wr _ _ _), hnd5_eq c (hCs c hc) layout.node.CUR_SC (by unfold layout.node.CUR_SC; decide) (by unfold layout.node.CUR_SC; decide)] + exact hJ4.cscC c hc + ksFit := by rw [L.ksb_wr]; exact hJ4.ksFit + scOffD := hJ4.scOffD + cv := fun o ho => by rw [hcap_sl5 o (Or.inl ho)]; exact hJ4.cv o ho + pml := fun y hy p mp asid hc => by rw [hcap_sl5 y (Or.inl hy)] at hc; exact hJ4.pml y hy p mp asid hc + vs := by + rw [L.ksb_wr] + exact (hJ4.vs.of_spec (σ' := σ5) rfl (fun p hp => by + rcases hp with ⟨q, hq, rfl⟩ | ⟨q, hq, rfl⟩ | ⟨q, hq, rfl⟩ + · exact hVs_kh5 q (Or.inl hq) + · exact hVs_kh5 q (Or.inr (Or.inl hq)) + · exact hVs_kh5 q (Or.inr (Or.inr hq)))).of_mem + (fun b hb => by + have hb0 : VsFootA Z.Pp Z.S1 Z.Y (L.ksb s) b := hksb4 ▸ hb + exact mem_wr_neA hal_st (hfa b (.nslot b (.vs b hb0))) (fun e => hVs_st (e ▸ hb0))) + ext := + (hJ4.ext.of_spec (σ' := σ5) rfl (fun q hq => hExt_kh5 q (by rcases hq with h | h | h <;> simp [h]))).of_mem + (fun b hb => mem_wr_neA hal_st (hfa b (.nslot b (.ext b hb))) (fun e => hExt_st (e ▸ hb))) } + have hS2_5 : Stage2CoreA (L := L) Z s5 σ5 := + ⟨fun r hr => by rw [hExt_kh5 r (Or.inr (Or.inl hr))]; exact hS2_4.obj r hr, + fun p es hkp => by + have hne : p ≠ specPtr ct := fun e => by subst e; rw [updKSc_same] at hkp; cases hkp + rw [hσ5_eq_st] at hkp; unfold stUpdA setObjA at hkp; simp [Function.update, hne] at hkp + exact hS2_4.back p es hkp, + fun y hy p l mp asid va hc => by rw [hcap_sl5 y (Or.inl hy)] at hc; exact hS2_4.cap y hy p l mp asid va hc⟩ + have hHV_5 : HighVSpaceCoreA (L := L) Z s5 σ5 := + ⟨fun r hr => by rw [hExt_kh5 r (Or.inr (Or.inr (Or.inl hr)))]; exact hHV_4.obj r hr, + fun p l1 hkp => by + have hne : p ≠ specPtr ct := fun e => by subst e; rw [updKSc_same] at hkp; cases hkp + rw [hσ5_eq_st] at hkp; unfold stUpdA setObjA at hkp; simp [Function.update, hne] at hkp + exact hHV_4.back p l1 hkp, + fun y hy p mp asid hc => by rw [hcap_sl5 y (Or.inl hy)] at hc; exact hHV_4.cap y hy p mp asid hc⟩ + have hvc_mem5 : ∀ v ∈ Z.V, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → + (v = vc → o ≠ 24#u64 ∧ o ≠ 32#u64 ∧ o ≠ 40#u64) → + L.mem s5 (wa v o) = L.mem s (wa v o) := fun v hv o ho ho8 hne => + hmem_other5 (wa v o) (aligned_wa (X.vcpu.place.vc_al hv) o ho8) + (vc_off_ne X.vcpu.place hv hvc ho (by decide) (fun e => by subst e; exact (hne rfl).1)) + (vc_off_ne X.vcpu.place hv hvc ho (by decide) (fun e => by subst e; exact (hne rfl).2.1)) + (vc_off_ne X.vcpu.place hv hvc ho (by decide) (fun e => by subst e; exact (hne rfl).2.2)) + (by rw [runVcpuA_eq]; exact X.vcpu.place.disj_tcb v hv ct hct o 3672#u64 ho (by decide)) + (by rw [stA_eq]; exact X.vcpu.place.disj_tcb v hv ct hct o 3328#u64 ho (by decide)) + have hvc_24 : L.mem s5 (wa vc 24#u64) = ct := by + have h1 : L.mem s5 (wa vc 24#u64) = L.mem s4 (wa vc 24#u64) := + mem_wr_neA hal_st hal_24 (by rw [stA_eq]; exact (X.vcpu.place.disj_tcb vc hvc ct hct 24#u64 3328#u64 (by decide) (by decide)).symm) + rw [h1] + simp only [s4, s3, s2, s1] + rw [mem_wr_neA hal_rv hal_24 (by rw [runVcpuA_eq]; exact (X.vcpu.place.disj_tcb vc hvc ct hct 24#u64 3672#u64 (by decide) (by decide)).symm), + mem_wr_neA hal_40 hal_24 (vc_off_ne X.vcpu.place hvc hvc (by decide) (by decide) (fun _ => by decide)), + mem_wr_neA hal_32 hal_24 (vc_off_ne X.vcpu.place hvc hvc (by decide) (by decide) (fun _ => by decide)), + L.mem_wr_same hal_24] + have hvc_32 : L.mem s5 (wa vc 32#u64) = offset := by + have h1 : L.mem s5 (wa vc 32#u64) = L.mem s4 (wa vc 32#u64) := + mem_wr_neA hal_st hal_32 (by rw [stA_eq]; exact (X.vcpu.place.disj_tcb vc hvc ct hct 32#u64 3328#u64 (by decide) (by decide)).symm) + rw [h1] + simp only [s4, s3, s2] + rw [mem_wr_neA hal_rv hal_32 (by rw [runVcpuA_eq]; exact (X.vcpu.place.disj_tcb vc hvc ct hct 32#u64 3672#u64 (by decide) (by decide)).symm), + mem_wr_neA hal_40 hal_32 (vc_off_ne X.vcpu.place hvc hvc (by decide) (by decide) (fun _ => by decide)), + L.mem_wr_same hal_32] + have hvc_40 : L.mem s5 (wa vc 40#u64) = out_mask := by + have h1 : L.mem s5 (wa vc 40#u64) = L.mem s4 (wa vc 40#u64) := + mem_wr_neA hal_st hal_40 (by rw [stA_eq]; exact (X.vcpu.place.disj_tcb vc hvc ct hct 40#u64 3328#u64 (by decide) (by decide)).symm) + rw [h1] + simp only [s4, s3] + rw [mem_wr_neA hal_rv hal_40 (by rw [runVcpuA_eq]; exact (X.vcpu.place.disj_tcb vc hvc ct hct 40#u64 3672#u64 (by decide) (by decide)).symm), + L.mem_wr_same hal_40] + have hct_rv5 : L.mem s5 (runVcpuA ct) = vc := by + rw [mem_wr_neA hal_st hal_rv (by rw [stA_eq, runVcpuA_eq]; exact field_ne hta hta (by decide) (by decide) (fun _ => by decide)), + L.mem_wr_same hal_rv] + have hVcCore5 : VcpuCoreA (L := L) Z s5 σ5 := by + refine ⟨fun v hv => ?_, fun p xv hkp => ?_, fun t ht => ?_, fun y hy p hc => ?_⟩ + · by_cases hveq : v = vc + · subst v + refine ⟨v1, by rw [updKSc_other _ hne_vc_ct, updKSc_same], ?_, ?_⟩ + · rw [← wa_zeroA vc, hvc_mem5 vc hvc 0#u64 (by decide) (by decide) (fun _ => ⟨by decide, by decide, by decide⟩), wa_zeroA] + obtain ⟨v0', hkv0', hvt0, -⟩ := X.vcpu.core.obj vc hvc + rw [hkv0] at hkv0'; cases hkv0'; exact hvt0 + · rw [hvc_24]; unfold optRefA; rw [if_neg hct_ne0] + · have hne1 : specPtr v ≠ specPtr ct := fun e => by + obtain ⟨v0', hkv0', -, -⟩ := X.vcpu.core.obj v hv; rw [e, hkt0] at hkv0'; cases hkv0' + have hne2 : specPtr v ≠ specPtr vc := fun e => hveq (specPtr_inj e) + obtain ⟨xv, hkv, hvt, hrc⟩ := X.vcpu.core.obj v hv + refine ⟨xv, by rw [updKSc_other _ hne1, updKSc_other _ hne2]; exact hkv, ?_, ?_⟩ + · rw [← wa_zeroA v, hvc_mem5 v hv 0#u64 (by decide) (by decide) (fun e => absurd e hveq), wa_zeroA]; exact hvt + · rw [hvc_mem5 v hv 24#u64 (by decide) (by decide) (fun e => absurd e hveq)]; exact hrc + · by_cases hpc : p = specPtr vc + · subst hpc; exact ⟨vc, hvc, rfl⟩ + · have hne1 : p ≠ specPtr ct := fun e => by subst e; rw [updKSc_same] at hkp; cases hkp + rw [updKSc_other _ hne1, updKSc_other _ hpc] at hkp + exact X.vcpu.core.back p xv hkp + · by_cases hteq : t = ct + · subst t + refine ⟨x1, updKSc_same _ _ _, ?_, ?_⟩ + · rw [htcb_other5 ct hct 3664#u64 (by decide) (by decide) (fun _ => ⟨by decide, by decide⟩)] + obtain ⟨x0', hkt0', htv0', -⟩ := X.vcpu.core.tcb ct hct + rw [hkt0] at hkt0'; cases hkt0'; exact htv0' + · rw [hct_rv5]; unfold optRefA; rw [if_neg hvc_ne0] + · have hne1 : specPtr t ≠ specPtr ct := fun e => hteq (specPtr_inj e) + have hne2 : specPtr t ≠ specPtr vc := fun e => by + obtain ⟨xt', hkt', -, -⟩ := X.vcpu.core.tcb t ht; rw [e, hkv0] at hkt'; cases hkt' + obtain ⟨xt, hkt, htv, hrv⟩ := X.vcpu.core.tcb t ht + refine ⟨xt, by rw [updKSc_other _ hne1, updKSc_other _ hne2]; exact hkt, ?_, ?_⟩ + · rw [htcb_other5 t ht 3664#u64 (by decide) (by decide) (fun e => absurd e hteq)]; exact htv + · rw [runVcpuA_eq, htcb_other5 t ht 3672#u64 (by decide) (by decide) (fun e => absurd e hteq), ← runVcpuA_eq]; exact hrv + · rw [hcap_sl5 y (Or.inl hy)] at hc + unfold capAtA at hc; rw [(hslot4 y (Or.inl hy)).1, (hslot4 y (Or.inl hy)).2.1] at hc + exact X.vcpu.core.cap y hy p hc + have hidle5 : ∀ c ∈ Z.Cs, + L.mem s5 (L.nodeAt s5 c layout.node.IDLE_THREAD) = L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) := fun c hc => by + rw [nodeAt_ksb (L.ksb_wr _ _ _), + hnd5_eq c (hCs c hc) layout.node.IDLE_THREAD (by unfold layout.node.IDLE_THREAD; decide) (by unfold layout.node.IDLE_THREAD; decide), + nodeAt_ksb hksb4] + exact hnode_eq4 c _ (hCs c hc) (by unfold layout.node.IDLE_THREAD; decide) (hfa _ (FootA.nslot _ (FootNA.node c _ hc (Or.inr (Or.inl rfl))))) + have hV5 : VcpuRelA (L := L) Z OS Ub s5 σ5 := by + refine ⟨hVcCore5, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, X.vcpu.arch, + by rw [hksb5]; exact X.vcpu.place⟩ + · intro v hv; rw [hvc_mem5 v hv 8#u64 (by decide) (by decide) (fun _ => ⟨by decide, by decide, by decide⟩)]; exact X.vcpu.owner v hv + · intro v hv x hkx + rw [hvc_mem5 v hv 16#u64 (by decide) (by decide) (fun _ => ⟨by decide, by decide, by decide⟩)] + by_cases hveq : v = vc + · subst v; rw [updKSc_other _ hne_vc_ct, updKSc_same] at hkx + have hx : x = v1 := ArchKernelObj.vcpu.inj (KernelObject.archObj.inj (Option.some.inj hkx.symm)) + subst hx; exact X.vcpu.vppi vc hvc v0 hkv0 + · have hne1 : specPtr v ≠ specPtr ct := fun e => by + obtain ⟨v0', hkv0', -, -⟩ := X.vcpu.core.obj v hv; rw [e, hkt0] at hkv0'; cases hkv0' + rw [updKSc_other _ hne1, updKSc_other _ (fun e => hveq (specPtr_inj e))] at hkx; exact X.vcpu.vppi v hv x hkx + · intro v hv x hkx + by_cases hveq : v = vc + · subst v; rw [updKSc_other _ hne_vc_ct, updKSc_same] at hkx + have hx : x = v1 := ArchKernelObj.vcpu.inj (KernelObject.archObj.inj (Option.some.inj hkx.symm)) + subst hx; rw [hvc_32] + · have hne1 : specPtr v ≠ specPtr ct := fun e => by + obtain ⟨v0', hkv0', -, -⟩ := X.vcpu.core.obj v hv; rw [e, hkt0] at hkv0'; cases hkv0' + rw [updKSc_other _ hne1, updKSc_other _ (fun e => hveq (specPtr_inj e))] at hkx + rw [hvc_mem5 v hv 32#u64 (by decide) (by decide) (fun e => absurd e hveq)]; exact X.vcpu.runOff v hv x hkx + · intro v hv x hkx + by_cases hveq : v = vc + · subst v; rw [updKSc_other _ hne_vc_ct, updKSc_same] at hkx + have hx : x = v1 := ArchKernelObj.vcpu.inj (KernelObject.archObj.inj (Option.some.inj hkx.symm)) + subst hx; exact ⟨by rw [hvc_40], hout_mask⟩ + · have hne1 : specPtr v ≠ specPtr ct := fun e => by + obtain ⟨v0', hkv0', -, -⟩ := X.vcpu.core.obj v hv; rw [e, hkt0] at hkv0'; cases hkv0' + rw [updKSc_other _ hne1, updKSc_other _ (fun e => hveq (specPtr_inj e))] at hkx + rw [hvc_mem5 v hv 40#u64 (by decide) (by decide) (fun e => absurd e hveq)]; exact X.vcpu.runOut v hv x hkx + · intro v hv x hkx i hi + have hlt : (vcpuRegOffA i).val < 4096 := by rw [vcpuRegOffA_val hi]; omega + have h8 : (vcpuRegOffA i).val % 8 = 0 := by rw [vcpuRegOffA_val hi]; omega + rw [vcpuRegWA, hvc_mem5 v hv (vcpuRegOffA i) hlt h8 (fun _ => + ⟨fun e => by have h24 : (24#u64).val = 24 := rfl; have := congrArg UScalar.val e; rw [vcpuRegOffA_val hi, h24] at this; omega, + fun e => by have h32 : (32#u64).val = 32 := rfl; have := congrArg UScalar.val e; rw [vcpuRegOffA_val hi, h32] at this; omega, + fun e => by have h40 : (40#u64).val = 40 := rfl; have := congrArg UScalar.val e; rw [vcpuRegOffA_val hi, h40] at this; omega⟩)] + by_cases hveq : v = vc + · subst v; rw [updKSc_other _ hne_vc_ct, updKSc_same] at hkx + have hx : x = v1 := ArchKernelObj.vcpu.inj (KernelObject.archObj.inj (Option.some.inj hkx.symm)) + subst hx; exact X.vcpu.regs vc hvc v0 hkv0 i hi + · have hne1 : specPtr v ≠ specPtr ct := fun e => by + obtain ⟨v0', hkv0', -, -⟩ := X.vcpu.core.obj v hv; rw [e, hkt0] at hkv0'; cases hkv0' + rw [updKSc_other _ hne1, updKSc_other _ (fun e => hveq (specPtr_inj e))] at hkx; exact X.vcpu.regs v hv x hkx i hi + · intro v hv t ht hne + rw [← wa_zeroA v, hvc_mem5 v hv 0#u64 (by decide) (by decide) (fun _ => ⟨by decide, by decide, by decide⟩), wa_zeroA] at ht + obtain ⟨htT, htv⟩ := X.vcpu.bind_v2t v hv t ht hne + exact ⟨htT, by rw [htcb_other5 t htT 3664#u64 (by decide) (by decide) (fun _ => ⟨by decide, by decide⟩)]; exact htv⟩ + · intro t ht v hv hne + rw [htcb_other5 t ht 3664#u64 (by decide) (by decide) (fun _ => ⟨by decide, by decide⟩)] at hv + obtain ⟨hvV, hvt⟩ := X.vcpu.bind_t2v t ht v hv hne + exact ⟨hvV, by rw [← wa_zeroA v, hvc_mem5 v hvV 0#u64 (by decide) (by decide) (fun _ => ⟨by decide, by decide, by decide⟩), wa_zeroA]; exact hvt⟩ + · intro v hv c hc hne + by_cases hveq : v = vc + · subst hveq; rw [hvc_24] at hc; subst hc; exact ⟨hct, hct_rv5⟩ + · rw [hvc_mem5 v hv 24#u64 (by decide) (by decide) (fun e => absurd e hveq)] at hc + obtain ⟨hcT, hcv⟩ := X.vcpu.run_v2c v hv c hc hne + have hc_ne_ct : c ≠ ct := fun e => by subst e; rw [hrv0] at hcv; exact (X.vcpu.place.al v hv).1 hcv.symm + exact ⟨hcT, by rw [runVcpuA_eq, htcb_other5 c hcT 3672#u64 (by decide) (by decide) (fun e => absurd e hc_ne_ct), ← runVcpuA_eq]; exact hcv⟩ + · intro c hc v hv hne + by_cases hceq : c = ct + · subst hceq; rw [hct_rv5] at hv; subst hv; exact ⟨hvc, hvc_24⟩ + · rw [runVcpuA_eq, htcb_other5 c hc 3672#u64 (by decide) (by decide) (fun e => absurd e hceq), ← runVcpuA_eq] at hv + obtain ⟨hvV, hvc'⟩ := X.vcpu.run_c2v c hc v hv hne + have hv_ne_vc : v ≠ vc := fun e => by subst e; rw [hrc0] at hvc'; exact (hU.tcb_al c hc).ne0 (congrArg UScalar.val hvc'.symm) + exact ⟨hvV, by rw [hvc_mem5 v hvV 24#u64 (by decide) (by decide) (fun e => absurd e hv_ne_vc)]; exact hvc'⟩ + · intro c hc hne + by_cases hceq : c = ct + · subst hceq; exact L.mem_wr_same hal_st + · rw [runVcpuA_eq, htcb_other5 c hc 3672#u64 (by decide) (by decide) (fun e => absurd e hceq), ← runVcpuA_eq] at hne + rw [stA_eq, htcb_other5 c hc 3328#u64 (by decide) (by decide) (fun e => absurd e hceq), ← stA_eq] + exact X.vcpu.run_state c hc hne + · intro t ht hne + rw [htcb_other5 t ht 3664#u64 (by decide) (by decide) (fun _ => ⟨by decide, by decide⟩)] at hne + have ht_ne_ct : t ≠ ct := fun e => by subst e; exact hne htv0 + rw [runVcpuA_eq, htcb_other5 t ht 3672#u64 (by decide) (by decide) (fun e => absurd e ht_ne_ct), ← runVcpuA_eq] + exact X.vcpu.bind_run_disj t ht hne + · intro c hc + rw [hidle5 c hc] + have hidT := hIdleT c hc + have hid_ne : L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) ≠ ct := hct_not_idle c hc + rw [htcb_other5 _ hidT 3664#u64 (by decide) (by decide) (fun e => absurd e hid_ne), + runVcpuA_eq, htcb_other5 _ hidT 3672#u64 (by decide) (by decide) (fun e => absurd e hid_ne), ← runVcpuA_eq] + exact X.vcpu.idle_no_vcpu c hc + · intro v hv hact + by_cases hveq : v = vc + · subst v + obtain ⟨y, hy, hcap⟩ := hvc_live + refine ⟨y, hy, ?_⟩ + rw [hcap_os5 y hy] + obtain ⟨h0, h8, -, -⟩ := hos4 y hy + unfold capAtA; rw [h0, h8]; exact hcap + · rw [← wa_zeroA v, hvc_mem5 v hv 0#u64 (by decide) (by decide) (fun e => absurd e hveq), wa_zeroA, + hvc_mem5 v hv 24#u64 (by decide) (by decide) (fun e => absurd e hveq)] at hact + obtain ⟨y, hy, hcap⟩ := X.vcpu.live v hv hact + refine ⟨y, hy, ?_⟩ + rw [hcap_os5 y hy] + obtain ⟨h0, h8, -, -⟩ := hos4 y hy + unfold capAtA; rw [h0, h8]; exact hcap + have hTr5 : TreeA st OS Z.G.chains Z.G.par s5 := hTr4.keepO hos5_4 + have hCar5 : CarryA Z (L.mem s5) σ5 := by + refine ⟨hCar4.1.of_capsA rfl (fun _ h => h) (fun _ h => h) (fun y hy => Or.inl (hcap_os5 y (hsub y hy))), + hCar4.2.of_keepA hTy5.1 hTy5.2 fun y hy => ?_⟩ + rw [hcap_os5 y ((hTr4.lk.members y).1 hy).1]; exact CapFinEqA.refl _ + have hF5 : FrameMemRelA Z OS Ub (L.ksb s5) (L.mem s5) σ5.machineState.underlyingMemory := + ⟨hF4.aligned, + fun b hb => by + rw [mem_wr_neA hal_st (hF4.aligned b hb) + (fun e => hF4.disj_tcb b hb ct hct 3328#u64 (by decide) (by rw [← stA_eq]; exact e.symm))] + exact hF4.agree b hb, + fun y hy ptr sz r mp s2 asid va hcap => by + rw [hcap_os5 y hy] at hcap; exact hF4.frames y hy ptr sz r mp s2 asid va hcap, + hF4.disj_os, hF4.disj_slots, hF4.disj_tcb, hF4.disj_sc, by rw [L.ksb_wr]; exact hF4.disj_ks⟩ + have hR5 : TcbRegsRelA (L.mem s5) σ5 Z.T := fun t ht x hkx r => by + obtain ⟨hK, hreg⟩ := regA_eq_wa_KA t r + have h3328 : (3328#u64).val = 3328 := rfl + have h3672 : (3672#u64).val = 3672 := rfl + rw [hreg, htcb_other5 t ht _ (by have := regIdxA_lt r; omega) (by omega) + (fun _ => ⟨fun e => by have := congrArg UScalar.val e; rw [h3672] at this; have := regIdxA_lt r; omega, + fun e => by have := congrArg UScalar.val e; rw [h3328] at this; have := regIdxA_lt r; omega⟩), ← hreg] + by_cases hteq : t = ct + · subst hteq; rw [updKSc_same] at hkx; cases hkx; exact X.regs ct hct x0 hkt0 r + · have hne1 : specPtr t ≠ specPtr ct := fun e => hteq (specPtr_inj e) + have hne2 : specPtr t ≠ specPtr vc := fun e => by + obtain ⟨xt', hkt', -, -⟩ := X.vcpu.core.tcb t ht; rw [e, hkv0] at hkt'; cases hkt' + rw [updKSc_other _ hne1, updKSc_other _ hne2] at hkx; exact X.regs t ht x hkx r + refine ⟨hsts_ok, hsp_step, ⟨⟨hJ5, ⟨hVcCore5, hS2_5, hHV_5⟩⟩, hTr5, hCar5, hV5, hF5, hR5⟩, + fun y hy => ⟨(hos5_4 y hy).1.trans (hos4 y hy).1, (hos5_4 y hy).2.1.trans (hos4 y hy).2.1, + (hos5_4 y hy).2.2.1.trans (hos4 y hy).2.2.1, (hos5_4 y hy).2.2.2.trans (hos4 y hy).2.2.2⟩, + fun t ht => ⟨by rw [inqA_eq, htcb_other5 t ht 3592#u64 (by decide) (by decide) (fun _ => ⟨by decide, by decide⟩), ← inqA_eq], + by rw [scA_eq, htcb_other5 t ht 3464#u64 (by decide) (by decide) (fun _ => ⟨by decide, by decide⟩), ← scA_eq]⟩, + fun c hc => by rw [mem_wr_neA hal_st (hSc_st c hc).2.2.2 (hSc_st c hc).2.2.1]; exact hsc_max4 c hc, + hidle5⟩ + +end run_helpers + +/-! ## Section 3: `write_state_from_frame` write-set framing & `TS_RESTART` commutation -/ + +section write_frame_comm +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +/-- Address footprint of `vcpu.write_state_from_frame`: VCPU register words `0..63` of `vc` +and TCB register words (`256 ≤ K < 3328`) of `v_tcb`. -/ +def VcpuWriteFrameSetA (vc v_tcb : U64) (b : U64) : Prop := + b = wa vc 16#u64 ∨ + (∃ i, i < 64 ∧ b = vcpuRegWA vc i) ∨ + (∃ K : U64, 256 ≤ K.val ∧ K.val < 3328 ∧ b = wa v_tcb K) + +private theorem reg_set_writesInA {s : S} {vc v_tcb idx val : U64} + (hal : Aligned vc) (hidx : idx.val < 64) : + vcpu.reg_set st s vc idx val ⦃ s' => WritesIn st s s' (VcpuWriteFrameSetA vc v_tcb) ⦄ := by + unfold vcpu.reg_set + have hidx_eq : idx = ⟨BitVec.ofNat 64 idx.val⟩ := by + apply UScalar.eq_of_val_eq; change idx.val = (BitVec.ofNat 64 idx.val).toNat + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)] + rw [hidx_eq, vcpu_reg_addr_eqA vc hidx, bind_ok, L.write_eq, WP.spec_ok] + have hal_r : Aligned (vcpuRegWA vc idx.val) := by + unfold vcpuRegWA; exact aligned_wa hal _ (by rw [vcpuRegOffA_val hidx]; omega) + exact writesIn_wr1A hal_r (Or.inr (Or.inl ⟨idx.val, hidx, rfl⟩)) + +private theorem copy_reg_range_from_frame_writesInA {s : S} {vc v_tcb frame start end_ : U64} + (hal : Aligned vc) (hend : end_.val < 64) : + vcpu.copy_reg_range_from_frame st s vc frame start end_ ⦃ s' => + WritesIn st s s' (VcpuWriteFrameSetA vc v_tcb) ⦄ := by + unfold vcpu.copy_reg_range_from_frame vcpu.copy_reg_range_from_frame_loop + let Inv : (S × U64) → Prop := fun (s0, cur) => WritesIn st s s0 (VcpuWriteFrameSetA vc v_tcb) + apply loop.spec_decr_nat (measure := fun p => end_.val + 1 - p.2.val) (inv := Inv) + (post := fun s' => WritesIn st s s' (VcpuWriteFrameSetA vc v_tcb)) + swap + · exact writesIn_reflA + rintro ⟨s0, cur⟩ hw0 + dsimp only + unfold vcpu.copy_reg_range_from_frame_loop.body + by_cases hle : cur ≤ end_ + · rw [ite_eq_left hle] + have hcur64 : cur.val < 64 := by change cur.val ≤ end_.val at hle; omega + have h8v : (8#u64).val = 8 := rfl + have h1v : (1#u64).val = 1 := rfl + rw [u64_mul_ok (by rw [h8v]; omega), bind_ok, lift_ok, bind_ok, L.read_eq, bind_ok] + refine WP.spec_bind (reg_set_writesInA (v_tcb := v_tcb) hal hcur64) ?_ + intro s1 hw1 + rw [u64_add_ok (by rw [h1v]; omega), bind_ok, WP.spec_ok] + have hc1_val : (⟨BitVec.ofNat 64 (cur.val + (1#u64).val)⟩ : U64).val = cur.val + 1 := by + change (BitVec.ofNat 64 (cur.val + 1)).toNat = cur.val + 1 + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + have hle' : cur.val ≤ end_.val := hle + exact ⟨writesIn_trans1A hw0 hw1, by rw [hc1_val]; omega⟩ + · rw [ite_eq_right hle, WP.spec_ok]; exact hw0 + +private theorem sync_vgic_status_writesInA {s : S} {vc v_tcb lrs : U64} {gic_v3 : Bool} + (hal : Aligned vc) (hlrs : lrs.val ≤ 16) : + vcpu.sync_vgic_status st s vc lrs gic_v3 ⦃ s' => + WritesIn st s s' (VcpuWriteFrameSetA vc v_tcb) ⦄ := by + have hrg : ∀ r : U64, r.val < 64 → vcpu.reg_get st s vc r = ok (L.mem s (vcpuRegWA vc r.val)) := fun r hr => by + unfold vcpu.reg_get + have heq : r = ⟨BitVec.ofNat 64 r.val⟩ := by + apply UScalar.eq_of_val_eq; change _ = (BitVec.ofNat 64 _).toNat + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)] + conv_lhs => rw [heq, vcpu_reg_addr_eqA vc hr, bind_ok, L.read_eq] + have hloop : vcpu.sync_vgic_status_loop st s vc lrs gic_v3 0#u64 0#u64 0#u64 false 0#u64 ⦃ _ => True ⦄ := by + unfold vcpu.sync_vgic_status_loop + let Inv : U64 × U64 × U64 × Bool × U64 → Prop := + fun p => p.2.2.1.val ≤ p.2.2.2.2.val ∧ p.2.2.2.2.val ≤ lrs.val + apply loop.spec_decr_nat (measure := fun p => lrs.val - p.2.2.2.2.val) (inv := Inv) (post := fun _ => True) + swap + · exact ⟨by decide, Nat.zero_le _⟩ + rintro ⟨elrsr, eisr, valid_lrs, has_pend, cur⟩ ⟨hvl_le, hcur_le⟩ + dsimp only [Inv] at hvl_le hcur_le ⊢ + unfold vcpu.sync_vgic_status_loop.body + by_cases hlt : cur < lrs + · rw [ite_eq_left hlt] + have hcur_lt : cur.val < lrs.val := hlt + have hcur16 : cur.val < 16 := by omega + have hvl16 : valid_lrs.val < 16 := by omega + have hlr0 : fl4_abi.consts.vcpu_reg.VGIC_LR0.val = 48 := by unfold fl4_abi.consts.vcpu_reg.VGIC_LR0; rfl + have hidx64 : (wa fl4_abi.consts.vcpu_reg.VGIC_LR0 cur).val < 64 := by + rw [wa_valP, hlr0, Nat.mod_eq_of_lt (by omega)]; omega + rw [lift_ok, bind_ok, hrg _ hidx64, bind_ok] + have hshr : ∀ (u : U64) (sh : I32) (hsh : 0 ≤ sh.val ∧ sh.val < 64), + u >>> sh = ok ⟨u.bv >>> sh.val.toNat⟩ := fun _ _ hsh => u64_shr_i32_ok hsh.1 hsh.2 + have hlf : (if gic_v3 then do + let i2 ← L.mem s (vcpuRegWA vc (wa fl4_abi.consts.vcpu_reg.VGIC_LR0 cur).val) >>> 62#i32 + let i3 ← lift (i2 &&& 3#u64) + let i4 ← L.mem s (vcpuRegWA vc (wa fl4_abi.consts.vcpu_reg.VGIC_LR0 cur).val) >>> 61#i32 + let i5 ← lift (i4 &&& 1#u64) + let i6 ← L.mem s (vcpuRegWA vc (wa fl4_abi.consts.vcpu_reg.VGIC_LR0 cur).val) >>> 41#i32 + let i7 ← lift (i6 &&& 1#u64) + ok (i3, i5, i7) + else do + let i2 ← L.mem s (vcpuRegWA vc (wa fl4_abi.consts.vcpu_reg.VGIC_LR0 cur).val) >>> 28#i32 + let i3 ← lift (i2 &&& 3#u64) + let i4 ← L.mem s (vcpuRegWA vc (wa fl4_abi.consts.vcpu_reg.VGIC_LR0 cur).val) >>> 31#i32 + let i5 ← lift (i4 &&& 1#u64) + let i6 ← L.mem s (vcpuRegWA vc (wa fl4_abi.consts.vcpu_reg.VGIC_LR0 cur).val) >>> 19#i32 + let i7 ← lift (i6 &&& 1#u64) + ok (i3, i5, i7)) ⦃ _ => True ⦄ := by + cases gic_v3 + · rw [ite_eq_right (by decide), hshr _ 28#i32 (by decide), bind_ok, lift_ok, bind_ok, + hshr _ 31#i32 (by decide), bind_ok, lift_ok, bind_ok, + hshr _ 19#i32 (by decide), bind_ok, lift_ok, bind_ok, WP.spec_ok]; trivial + · rw [ite_eq_left rfl, hshr _ 62#i32 (by decide), bind_ok, lift_ok, bind_ok, + hshr _ 61#i32 (by decide), bind_ok, lift_ok, bind_ok, + hshr _ 41#i32 (by decide), bind_ok, lift_ok, bind_ok, WP.spec_ok]; trivial + refine WP.spec_bind hlf ?_ + rintro ⟨st_lr, hw_lr, eoi_lr⟩ _ + have h1v : (1#u64).val = 1 := rfl + have hc1_val : (⟨BitVec.ofNat 64 (cur.val + (1#u64).val)⟩ : U64).val = cur.val + 1 := by + change (BitVec.ofNat 64 (cur.val + 1)).toNat = cur.val + 1 + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + have hv1_val : (⟨BitVec.ofNat 64 (valid_lrs.val + 1)⟩ : U64).val = valid_lrs.val + 1 := by + change (BitVec.ofNat 64 (valid_lrs.val + 1)).toNat = valid_lrs.val + 1 + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + have hshl := u64_shl_u64_ok (x := 1#u64) (y := cur) (by omega : cur.val < 64) + have hadd_v := u64_add_ok (x := valid_lrs) (y := 1#u64) (by rw [h1v]; omega) + have hadd_c := u64_add_ok (x := cur) (y := 1#u64) (by rw [h1v]; omega) + refine WP.spec_bind (Pₘ := fun p => p.2.2.1.val ≤ cur.val + 1) ?_ ?_ + · split_ifs <;> simp [hshl, hadd_v, lift_ok, bind_ok, WP.spec_ok] <;> + first | omega | (rw [hv1_val]; omega) + · rintro ⟨elrsr1, eisr1, valid_lrs1, has_pend1⟩ hvl1 + simp only [hadd_c, bind_ok] + change ok (ControlFlow.cont (elrsr1, eisr1, valid_lrs1, has_pend1, (⟨BitVec.ofNat 64 (cur.val + (1#u64).val)⟩ : U64))) ⦃ _ => _ ⦄ + rw [WP.spec_ok] + exact ⟨⟨by rw [hc1_val]; exact hvl1, by rw [hc1_val]; omega⟩, by rw [hc1_val]; omega⟩ + · rw [ite_eq_right hlt, WP.spec_ok]; trivial + unfold vcpu.sync_vgic_status + refine WP.spec_bind hloop ?_ + rintro ⟨elrsr, eisr, valid_lrs, has_pend⟩ _ + simp only [hrg fl4_abi.consts.vcpu_reg.VGIC_HCR (by unfold fl4_abi.consts.vcpu_reg.VGIC_HCR; decide), + hrg fl4_abi.consts.vcpu_reg.VGIC_VMCR (by unfold fl4_abi.consts.vcpu_reg.VGIC_VMCR; decide), bind_ok] + have hsh : ∀ (k : I32) (hk : 0 ≤ k.val ∧ k.val < 64), 1#u64 <<< k = ok ⟨(1#u64).bv <<< k.val.toNat⟩ := + fun _ hk => u64_shl_i32_ok hk.1 hk.2 + have hshr27 := u64_shr_i32_ok (x := L.mem s (vcpuRegWA vc fl4_abi.consts.vcpu_reg.VGIC_HCR.val)) (y := 27#i32) (by decide) (by decide) + refine WP.spec_bind (Pₘ := fun _ => True) (by split_ifs <;> (try simp only [hsh 0#i32 (by decide), lift_ok, bind_ok]) <;> rw [WP.spec_ok] <;> trivial) fun _ _ => ?_ + rw [hsh 1#i32 (by decide), bind_ok, lift_ok, bind_ok] + refine WP.spec_bind (Pₘ := fun _ => True) (by split_ifs <;> (try simp only [lift_ok, bind_ok]) <;> (try split_ifs) <;> (try simp only [lift_ok, bind_ok]) <;> rw [WP.spec_ok] <;> trivial) fun _ _ => ?_ + rw [hsh 2#i32 (by decide), bind_ok, lift_ok, bind_ok] + refine WP.spec_bind (Pₘ := fun _ => True) (by split_ifs <;> (try simp only [hshr27, lift_ok, bind_ok]) <;> (try split_ifs) <;> (try simp only [lift_ok, bind_ok]) <;> rw [WP.spec_ok] <;> trivial) fun _ _ => ?_ + rw [hsh 3#i32 (by decide), bind_ok, lift_ok, bind_ok] + refine WP.spec_bind (Pₘ := fun _ => True) (by split_ifs <;> (try simp only [lift_ok, bind_ok]) <;> (try split_ifs) <;> (try simp only [lift_ok, bind_ok]) <;> rw [WP.spec_ok] <;> trivial) fun _ _ => ?_ + rw [hsh 4#i32 (by decide), bind_ok, lift_ok, bind_ok] + refine WP.spec_bind (Pₘ := fun _ => True) (by split_ifs <;> (try simp only [hsh 0#i32 (by decide), lift_ok, bind_ok]) <;> (try split_ifs) <;> (try simp only [lift_ok, bind_ok]) <;> rw [WP.spec_ok] <;> trivial) fun _ _ => ?_ + rw [hsh 5#i32 (by decide), bind_ok, lift_ok, bind_ok] + refine WP.spec_bind (Pₘ := fun _ => True) (by split_ifs <;> (try simp only [hsh 0#i32 (by decide), lift_ok, bind_ok]) <;> (try split_ifs) <;> (try simp only [lift_ok, bind_ok]) <;> rw [WP.spec_ok] <;> trivial) fun _ _ => ?_ + rw [hsh 6#i32 (by decide), bind_ok, lift_ok, bind_ok] + refine WP.spec_bind (Pₘ := fun _ => True) (by split_ifs <;> (try simp only [lift_ok, bind_ok]) <;> (try split_ifs) <;> (try simp only [lift_ok, bind_ok]) <;> rw [WP.spec_ok] <;> trivial) fun _ _ => ?_ + rw [hsh 7#i32 (by decide), bind_ok, lift_ok, bind_ok] + refine WP.spec_bind (Pₘ := fun _ => True) (by split_ifs <;> (try simp only [lift_ok, bind_ok]) <;> (try split_ifs) <;> (try simp only [lift_ok, bind_ok]) <;> rw [WP.spec_ok] <;> trivial) fun _ _ => ?_ + refine WP.spec_bind (reg_set_writesInA (v_tcb := v_tcb) hal (by unfold fl4_abi.consts.vcpu_reg.VGIC_ELRSR; decide)) ?_ + intro s1 hw1 + refine WP.spec_bind (reg_set_writesInA (v_tcb := v_tcb) hal (by unfold fl4_abi.consts.vcpu_reg.VGIC_EISR; decide)) ?_ + intro s2 hw2 + refine WP.spec_mono (reg_set_writesInA (v_tcb := v_tcb) hal (by unfold fl4_abi.consts.vcpu_reg.VGIC_MISR; decide)) ?_ + intro s3 hw3 + exact writesIn_trans1A (writesIn_trans1A hw1 hw2) hw3 + +private theorem write_loop_lr_writesInA {s : S} {vc v_tcb lrs : U64} + {vgic : Array U64 16#usize} (hal : Aligned vc) (hlrs : lrs.val ≤ 16) : + vcpu.write_state_from_frame_loop0 st s vc vgic lrs 0#u64 ⦃ s' => + WritesIn st s s' (VcpuWriteFrameSetA vc v_tcb) ⦄ := by + unfold vcpu.write_state_from_frame_loop0 + apply loop.spec_decr_nat (measure := fun p => lrs.val - p.2.val) + (inv := fun p => WritesIn st s p.1 (VcpuWriteFrameSetA vc v_tcb)) + (post := fun s' => WritesIn st s s' (VcpuWriteFrameSetA vc v_tcb)) + swap + · exact writesIn_reflA + rintro ⟨s0, cur⟩ hw0 + dsimp only + unfold vcpu.write_state_from_frame_loop0.body + by_cases hlt : cur < lrs + · rw [ite_eq_left hlt] + have hlt_v : cur.val < lrs.val := hlt + have hcur16 : cur.val < 16 := by omega + have hlt16 : cur < 16#u64 := hcur16 + rw [ite_eq_left hlt16, lift_ok, bind_ok, lift_ok, bind_ok] + have hlr0 : fl4_abi.consts.vcpu_reg.VGIC_LR0.val = 48 := by unfold fl4_abi.consts.vcpu_reg.VGIC_LR0; rfl + have hcast_val : (UScalar.cast .Usize cur).val = cur.val := by + rw [UScalar.cast_val_eq] + change cur.val % 2 ^ System.Platform.numBits = cur.val + exact Nat.mod_eq_of_lt (by rcases System.Platform.numBits_eq with h | h <;> rw [h] <;> omega) + have hcast_lt : (UScalar.cast .Usize cur).val < vgic.val.length := by + rw [vgic.property, hcast_val]; exact hcur16 + refine WP.spec_bind (Array.index_usize_spec vgic (UScalar.cast .Usize cur) hcast_lt) ?_ + intro w _ + have hidx64 : (wa fl4_abi.consts.vcpu_reg.VGIC_LR0 cur).val < 64 := by + rw [wa_valP, hlr0, Nat.mod_eq_of_lt (by omega)]; omega + refine WP.spec_bind (reg_set_writesInA (v_tcb := v_tcb) hal hidx64) ?_ + intro s1 hw1 + have h1v : (1#u64).val = 1 := rfl + rw [u64_add_ok (by rw [h1v]; omega), bind_ok, WP.spec_ok] + have hc1_val : (⟨BitVec.ofNat 64 (cur.val + (1#u64).val)⟩ : U64).val = cur.val + 1 := by + change (BitVec.ofNat 64 (cur.val + 1)).toNat = cur.val + 1 + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + exact ⟨writesIn_trans1A hw0 hw1, by rw [hc1_val]; omega⟩ + · rw [ite_eq_right hlt, WP.spec_ok]; exact hw0 + +private theorem write_loop_gprs_writesInA {s : S} {vc v_tcb gprs_base : U64} + (hta : TcbAddr v_tcb) : + vcpu.write_state_from_frame_loop1 st s gprs_base v_tcb 0#u64 ⦃ s' => + WritesIn st s s' (VcpuWriteFrameSetA vc v_tcb) ⦄ := by + have hal_t : Aligned v_tcb := by unfold Aligned; have := hta.align; omega + have hcnt : layout.reg.COUNT.val = 37 := by unfold layout.reg.COUNT; rfl + unfold vcpu.write_state_from_frame_loop1 + apply loop.spec_decr_nat (measure := fun p => 37 - p.2.val) + (inv := fun p => WritesIn st s p.1 (VcpuWriteFrameSetA vc v_tcb)) + (post := fun s' => WritesIn st s s' (VcpuWriteFrameSetA vc v_tcb)) + swap + · exact writesIn_reflA + rintro ⟨s0, cur⟩ hw0 + dsimp only + unfold vcpu.write_state_from_frame_loop1.body + by_cases hlt : cur < layout.reg.COUNT + · rw [ite_eq_left hlt] + have hcur37 : cur.val < 37 := by change cur.val < layout.reg.COUNT.val at hlt; omega + have h8v : (8#u64).val = 8 := rfl + have h1v : (1#u64).val = 1 := rfl + have h256v : (256#u64).val = 256 := rfl + rw [u64_mul_ok (by rw [h8v]; omega), bind_ok, lift_ok, bind_ok, L.read_eq, bind_ok] + have hsan : ∀ val, (if cur = layout.reg.SPSR_EL1 then regs.sanitise_vcpu_spsr val else ok val) ⦃ _ => True ⦄ := fun val => by + by_cases hs : cur = layout.reg.SPSR_EL1 + · rw [ite_eq_left hs, sanitise_vcpu_spsr_eqA, WP.spec_ok]; trivial + · rw [ite_eq_right hs, WP.spec_ok]; trivial + refine WP.spec_bind (hsan _) ?_ + intro val _ + rw [eq_of_spec_fd (obj.Tcb.set_reg.spec s0 v_tcb cur val), bind_ok] + let K : U64 := wa 256#u64 (core.num.U64.wrapping_mul cur 8#u64) + have hK_val : K.val = 256 + cur.val * 8 := by + simp only [K, wa_valP, wm_valP, h256v, h8v] + rw [Nat.mod_eq_of_lt (by omega), Nat.mod_eq_of_lt (by omega)] + have hreg_eq : Endp.regA v_tcb cur = wa v_tcb K := by + unfold Endp.regA layout.tcb.REGS K + apply UScalar.eq_of_val_eq + simp only [wa_valP, Nat.mod_add_mod, Nat.add_mod_mod, Nat.add_assoc] + have hw1 : WritesIn st s0 (L.wr s0 (Endp.regA v_tcb cur) val) (VcpuWriteFrameSetA vc v_tcb) := by + rw [hreg_eq] + exact writesIn_wr1A (aligned_wa hal_t K (by omega)) (Or.inr (Or.inr ⟨K, by omega, by omega, rfl⟩)) + rw [u64_add_ok (by rw [h1v]; omega), bind_ok, WP.spec_ok] + have hc1_val : (⟨BitVec.ofNat 64 (cur.val + (1#u64).val)⟩ : U64).val = cur.val + 1 := by + change (BitVec.ofNat 64 (cur.val + 1)).toNat = cur.val + 1 + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + exact ⟨writesIn_trans1A hw0 hw1, by rw [hc1_val]; omega⟩ + · rw [ite_eq_right hlt, WP.spec_ok]; exact hw0 + +set_option maxHeartbeats 800000 in +/-- `vcpu.write_state_from_frame` only writes VCPU register words of `vc` (`0..63`) and +TCB register words (`256 ≤ K < 3328`) of `v_tcb = L.mem s vc`. -/ +theorem write_state_from_frame_writesInA {s : S} {a : A} {vc v_tcb frame mask : U64} + {vgic : U64 × Array U64 16#usize} + (hP : VcpuPlatAgreeA ar) (hal : Aligned vc) (hta : TcbAddr v_tcb) + (hvc_tcb : L.mem s vc = v_tcb) + (hne_reg_vc : ∀ i, i < 64 → vcpuRegWA vc i ≠ vc) + (hne_tcb_vc : ∀ K : U64, 256 ≤ K.val → K.val < 3328 → wa v_tcb K ≠ vc) : + vcpu.write_state_from_frame st ar s a vc frame mask vgic ⦃ s' => + WritesIn st s s' (VcpuWriteFrameSetA vc v_tcb) ⦄ := by + have hal_t : Aligned v_tcb := by unfold Aligned; have := hta.align; omega + have hvc_keep : ∀ s0, WritesIn st s s0 (VcpuWriteFrameSetA vc v_tcb) → L.mem s0 vc = v_tcb := fun s0 hw => by + rw [hw.mem_eq hal (fun h => by + rcases h with heq | ⟨i, hi, heq⟩ | ⟨K, h1, h2, heq⟩ + · have hv := congrArg UScalar.val heq; rw [wa_valP] at hv + have h16v : (16#u64).val = 16 := rfl; rw [h16v] at hv; omega + · exact hne_reg_vc i hi heq.symm + · exact hne_tcb_vc K h1 h2 heq.symm)] + exact hvc_tcb + have hGprs : ∀ s0, WritesIn st s s0 (VcpuWriteFrameSetA vc v_tcb) → + (do + let i13 ← fl4_abi.consts.vcpu_state_mask.GPRS + let i14 ← lift (mask &&& i13) + if i14 != 0#u64 then do + let i15 ← 64#u64 * 8#u64 + let gprs_base ← lift (wa frame i15) + let i16 ← lift (wa vc vcpu.TCB) + let t_ptr ← st.read s0 i16 + if t_ptr != 0#u64 then do + let s10 ← vcpu.write_state_from_frame_loop1 st s0 gprs_base t_ptr 0#u64 + let pc ← obj.Tcb.reg st t_ptr s10 layout.reg.ELR_EL1 + obj.Tcb.set_reg st t_ptr s10 layout.reg.FAULT_IP pc + else ok s0 + else ok s0) ⦃ s' => WritesIn st s s' (VcpuWriteFrameSetA vc v_tcb) ⦄ := fun s0 hw0 => by + have hsm : fl4_abi.consts.vcpu_state_mask.GPRS = ok 16#u64 := by unfold fl4_abi.consts.vcpu_state_mask.GPRS; rfl + rw [hsm, bind_ok, lift_ok, bind_ok] + by_cases hg : (mask &&& 16#u64 != 0#u64) = true + · rw [if_pos hg, u64_mul_ok (by decide), bind_ok, lift_ok, bind_ok, lift_ok, bind_ok, L.read_eq, bind_ok] + have hwa0 : wa vc vcpu.TCB = vc := by unfold vcpu.TCB; exact wa_zeroA vc + rw [hwa0, hvc_keep s0 hw0] + by_cases hnz : (v_tcb != 0#u64) = true + · rw [if_pos hnz] + refine WP.spec_bind (write_loop_gprs_writesInA (vc := vc) hta) ?_ + intro s1 hw1 + rw [eq_of_spec_fd (obj.Tcb.reg.spec s1 v_tcb layout.reg.ELR_EL1), bind_ok, + eq_of_spec_fd (obj.Tcb.set_reg.spec s1 v_tcb layout.reg.FAULT_IP _), WP.spec_ok] + have hreg_fip : Endp.regA v_tcb layout.reg.FAULT_IP = Endp.regA v_tcb (regIdxA .faultIP) := by + unfold layout.reg.FAULT_IP regIdxA; rfl + obtain ⟨hK, hwa_fip⟩ := regA_eq_wa_KA v_tcb .faultIP + have hlt_fip := regIdxA_lt .faultIP + have hw2 : WritesIn st s1 (L.wr s1 (Endp.regA v_tcb layout.reg.FAULT_IP) (L.mem s1 (Endp.regA v_tcb layout.reg.ELR_EL1))) (VcpuWriteFrameSetA vc v_tcb) := by + rw [hreg_fip, hwa_fip] + exact writesIn_wr1A (aligned_wa hal_t _ (by omega)) (Or.inr (Or.inr ⟨_, by omega, by omega, rfl⟩)) + exact writesIn_trans1A (writesIn_trans1A hw0 hw1) hw2 + · rw [ite_eq_right hnz, WP.spec_ok]; exact hw0 + · rw [ite_eq_right hg, WP.spec_ok]; exact hw0 + unfold vcpu.write_state_from_frame + by_cases hm0 : mask = 0#u64 + · rw [ite_eq_left hm0, WP.spec_ok]; exact writesIn_reflA + · rw [ite_eq_right hm0] + -- Step 1: EL1_SYSREGS + have hsm1 : fl4_abi.consts.vcpu_state_mask.EL1_SYSREGS = ok 1#u64 := by unfold fl4_abi.consts.vcpu_state_mask.EL1_SYSREGS; rfl + rw [hsm1, bind_ok, lift_ok, bind_ok] + have hS1 : (if (mask &&& 1#u64 != 0#u64) = true then do + let s2 ← vcpu.copy_reg_range_from_frame st s vc frame 0#u64 fl4_abi.consts.vcpu_reg.CSSELR_EL1 + vcpu.copy_reg_range_from_frame st s2 vc frame fl4_abi.consts.vcpu_reg.ACTLR_EL1 fl4_abi.consts.vcpu_reg.MDCCINT_EL1 + else ok s) ⦃ s1 => WritesIn st s s1 (VcpuWriteFrameSetA vc v_tcb) ⦄ := by + by_cases h1 : (mask &&& 1#u64 != 0#u64) = true + · rw [ite_eq_left h1] + refine WP.spec_bind (copy_reg_range_from_frame_writesInA (s := s) (v_tcb := v_tcb) hal (by unfold fl4_abi.consts.vcpu_reg.CSSELR_EL1; decide)) ?_ + intro s2 hw2 + refine WP.spec_mono (copy_reg_range_from_frame_writesInA (s := s2) (v_tcb := v_tcb) hal (by unfold fl4_abi.consts.vcpu_reg.MDCCINT_EL1; decide)) ?_ + intro s3 hw3; exact writesIn_trans1A hw2 hw3 + · rw [ite_eq_right h1, WP.spec_ok]; exact writesIn_reflA + refine WP.spec_bind hS1 ?_ + intro s1 hw1 + -- Step 2: HYP + have hsm2 : fl4_abi.consts.vcpu_state_mask.HYP = ok 2#u64 := by unfold fl4_abi.consts.vcpu_state_mask.HYP; rfl + rw [hsm2, bind_ok, lift_ok, bind_ok] + have hS2 : (if (mask &&& 2#u64 != 0#u64) = true then do + let i4 ← lift (mask &&& 1#u64) + let s3 ← if i4 = 0#u64 then do + let i5 ← fl4_abi.consts.vcpu_reg.VMPIDR_EL2 * 8#u64 + let i6 ← lift (wa frame i5) + let i7 ← st.read s1 i6 + vcpu.reg_set st s1 vc fl4_abi.consts.vcpu_reg.VMPIDR_EL2 i7 + else ok s1 + let i5 ← fl4_abi.consts.vcpu_reg.VDISR_EL2 * 8#u64 + let i6 ← lift (wa frame i5) + let i7 ← st.read s3 i6 + let s4 ← vcpu.reg_set st s3 vc fl4_abi.consts.vcpu_reg.VDISR_EL2 i7 + let i8 ← fl4_abi.consts.vcpu_reg.VPIDR_EL2 * 8#u64 + let i9 ← lift (wa frame i8) + let i10 ← st.read s4 i9 + let s5 ← vcpu.reg_set st s4 vc fl4_abi.consts.vcpu_reg.VPIDR_EL2 i10 + let i11 ← fl4_abi.consts.vcpu_reg.HCR_EL2 * 8#u64 + let i12 ← lift (wa frame i11) + let i13 ← st.read s5 i12 + let i14 ← vcpu.sanitize_hcr i13 + let s6 ← vcpu.reg_set st s5 vc fl4_abi.consts.vcpu_reg.HCR_EL2 i14 + let i15 ← fl4_abi.consts.vcpu_reg.VSESR_EL2 * 8#u64 + let i16 ← lift (wa frame i15) + let i17 ← st.read s6 i16 + vcpu.reg_set st s6 vc fl4_abi.consts.vcpu_reg.VSESR_EL2 i17 + else ok s1) ⦃ s2 => WritesIn st s s2 (VcpuWriteFrameSetA vc v_tcb) ⦄ := by + by_cases h2 : (mask &&& 2#u64 != 0#u64) = true + · rw [ite_eq_left h2, lift_ok, bind_ok] + have hVmp : (if mask &&& 1#u64 = 0#u64 then do + let i5 ← fl4_abi.consts.vcpu_reg.VMPIDR_EL2 * 8#u64 + let i6 ← lift (wa frame i5) + let i7 ← st.read s1 i6 + vcpu.reg_set st s1 vc fl4_abi.consts.vcpu_reg.VMPIDR_EL2 i7 + else ok s1) ⦃ s3 => WritesIn st s s3 (VcpuWriteFrameSetA vc v_tcb) ⦄ := by + by_cases he : mask &&& 1#u64 = 0#u64 + · rw [ite_eq_left he, u64_mul_ok (by unfold fl4_abi.consts.vcpu_reg.VMPIDR_EL2; decide), bind_ok, lift_ok, bind_ok, L.read_eq, bind_ok] + refine WP.spec_mono (reg_set_writesInA (v_tcb := v_tcb) hal (by unfold fl4_abi.consts.vcpu_reg.VMPIDR_EL2; decide)) ?_ + intro s3 hw3; exact writesIn_trans1A hw1 hw3 + · rw [ite_eq_right he, WP.spec_ok]; exact hw1 + refine WP.spec_bind hVmp ?_; intro s3 hw3 + rw [u64_mul_ok (by unfold fl4_abi.consts.vcpu_reg.VDISR_EL2; decide), bind_ok, lift_ok, bind_ok, L.read_eq, bind_ok] + refine WP.spec_bind (reg_set_writesInA (v_tcb := v_tcb) hal (by unfold fl4_abi.consts.vcpu_reg.VDISR_EL2; decide)) ?_; intro s4 hw4 + rw [u64_mul_ok (by unfold fl4_abi.consts.vcpu_reg.VPIDR_EL2; decide), bind_ok, lift_ok, bind_ok, L.read_eq, bind_ok] + refine WP.spec_bind (reg_set_writesInA (v_tcb := v_tcb) hal (by unfold fl4_abi.consts.vcpu_reg.VPIDR_EL2; decide)) ?_; intro s5 hw5 + rw [u64_mul_ok (by unfold fl4_abi.consts.vcpu_reg.HCR_EL2; decide), bind_ok, lift_ok, bind_ok, L.read_eq, bind_ok, vcpu_sanitize_hcr_eqA, bind_ok] + refine WP.spec_bind (reg_set_writesInA (v_tcb := v_tcb) hal (by unfold fl4_abi.consts.vcpu_reg.HCR_EL2; decide)) ?_; intro s6 hw6 + rw [u64_mul_ok (by unfold fl4_abi.consts.vcpu_reg.VSESR_EL2; decide), bind_ok, lift_ok, bind_ok, L.read_eq, bind_ok] + refine WP.spec_mono (reg_set_writesInA (v_tcb := v_tcb) hal (by unfold fl4_abi.consts.vcpu_reg.VSESR_EL2; decide)) ?_; intro s7 hw7 + exact writesIn_trans1A (writesIn_trans1A (writesIn_trans1A (writesIn_trans1A hw3 hw4) hw5) hw6) hw7 + · rw [ite_eq_right h2, WP.spec_ok]; exact hw1 + refine WP.spec_bind hS2 ?_ + intro s2 hw2 + -- Step 3: TIMER + have hsm3 : fl4_abi.consts.vcpu_state_mask.TIMER = ok 4#u64 := by unfold fl4_abi.consts.vcpu_state_mask.TIMER; rfl + rw [hsm3, bind_ok, lift_ok, bind_ok] + have hS3 : (if (mask &&& 4#u64 != 0#u64) = true then do + let s4 ← vcpu.copy_reg_range_from_frame st s2 vc frame fl4_abi.consts.vcpu_reg.CNTV_CTL fl4_abi.consts.vcpu_reg.CNTKCTL_EL1 + let i6 ← 64#u64 + 37#u64 + let i7 ← i6 * 8#u64 + let vppi_addr ← lift (wa frame i7) + let i8 ← st.read s4 vppi_addr + let m ← lift (i8 &&& 1#u64) + let i9 ← lift (wa vc vcpu.VPPI_MASKED) + st.write s4 i9 m + else ok s2) ⦃ s3 => WritesIn st s s3 (VcpuWriteFrameSetA vc v_tcb) ⦄ := by + by_cases h3 : (mask &&& 4#u64 != 0#u64) = true + · rw [ite_eq_left h3] + refine WP.spec_bind (copy_reg_range_from_frame_writesInA (s := s2) (v_tcb := v_tcb) hal (by unfold fl4_abi.consts.vcpu_reg.CNTKCTL_EL1; decide)) ?_ + intro s4 hw4 + rw [u64_add_ok (by decide), bind_ok, u64_mul_ok (by decide), bind_ok, lift_ok, bind_ok, L.read_eq, bind_ok, + lift_ok, bind_ok, lift_ok, bind_ok, L.write_eq, WP.spec_ok] + have hvp16 : vcpu.VPPI_MASKED = 16#u64 := by unfold vcpu.VPPI_MASKED; rfl + rw [hvp16] + exact writesIn_trans1A (writesIn_trans1A hw2 hw4) + (writesIn_wr1A (aligned_wa hal 16#u64 (by decide)) (Or.inl rfl)) + · rw [ite_eq_right h3, WP.spec_ok]; exact hw2 + refine WP.spec_bind hS3 ?_ + intro s3 hw3 + -- Step 4: VGIC / VGIC_LR + GPRS + have hsmV : fl4_abi.consts.vcpu_state_mask.VGIC = ok 8#u64 := by unfold fl4_abi.consts.vcpu_state_mask.VGIC; rfl + have hsmL : fl4_abi.consts.vcpu_state_mask.VGIC_LR = ok 32#u64 := by unfold fl4_abi.consts.vcpu_state_mask.VGIC_LR; rfl + rw [hsmV, bind_ok, lift_ok, bind_ok] + by_cases h4 : (mask &&& 8#u64 != 0#u64) = true + · rw [ite_eq_left h4, vcpu_num_lrs_eqA hP a, bind_ok] + have hlrs16 : (⟨BitVec.ofNat 64 fl4NumLrs⟩ : U64).val ≤ 16 := by + change (BitVec.ofNat 64 fl4NumLrs).toNat ≤ 16 + rw [BitVec.toNat_ofNat]; exact Nat.le_trans (Nat.mod_le _ _) fl4NumLrs_le_16 + obtain ⟨gv, hgv, -⟩ := hP.gic a + rw [hgv, bind_ok] + refine WP.spec_bind (reg_set_writesInA (v_tcb := v_tcb) hal (by unfold fl4_abi.consts.vcpu_reg.VGIC_HCR; decide)) ?_; intro s4 hw4 + rw [u64_mul_ok (by unfold fl4_abi.consts.vcpu_reg.VGIC_VMCR; decide), bind_ok, lift_ok, bind_ok, L.read_eq, bind_ok] + refine WP.spec_bind (reg_set_writesInA (v_tcb := v_tcb) hal (by unfold fl4_abi.consts.vcpu_reg.VGIC_VMCR; decide)) ?_; intro s5 hw5 + refine WP.spec_bind (copy_reg_range_from_frame_writesInA (s := s5) (v_tcb := v_tcb) hal (by unfold fl4_abi.consts.vcpu_reg.VGIC_AP0R3; decide)) ?_; intro s6 hw6 + have hAp1r : (if gv = 3#u64 then + vcpu.copy_reg_range_from_frame st s6 vc frame fl4_abi.consts.vcpu_reg.VGIC_AP1R0 fl4_abi.consts.vcpu_reg.VGIC_AP1R3 + else ok s6) ⦃ s7 => WritesIn st s s7 (VcpuWriteFrameSetA vc v_tcb) ⦄ := by + by_cases h3 : gv = 3#u64 + · rw [ite_eq_left h3] + refine WP.spec_mono (copy_reg_range_from_frame_writesInA (s := s6) (v_tcb := v_tcb) hal (by unfold fl4_abi.consts.vcpu_reg.VGIC_AP1R3; decide)) ?_ + intro s7 hw7; exact writesIn_trans1A (writesIn_trans1A (writesIn_trans1A (writesIn_trans1A hw3 hw4) hw5) hw6) hw7 + · rw [ite_eq_right h3, WP.spec_ok]; exact writesIn_trans1A (writesIn_trans1A (writesIn_trans1A hw3 hw4) hw5) hw6 + refine WP.spec_bind hAp1r ?_; intro s7 hw7 + refine WP.spec_bind (write_loop_lr_writesInA (v_tcb := v_tcb) hal hlrs16) ?_; intro s8 hw8 + refine WP.spec_bind (sync_vgic_status_writesInA (v_tcb := v_tcb) hal hlrs16) ?_; intro s9 hw9 + exact hGprs s9 (writesIn_trans1A (writesIn_trans1A hw7 hw8) hw9) + · rw [ite_eq_right h4, hsmL, bind_ok, lift_ok, bind_ok] + by_cases h5 : (mask &&& 32#u64 != 0#u64) = true + · rw [ite_eq_left h5, vcpu_num_lrs_eqA hP a, bind_ok] + have hlrs16 : (⟨BitVec.ofNat 64 fl4NumLrs⟩ : U64).val ≤ 16 := by + change (BitVec.ofNat 64 fl4NumLrs).toNat ≤ 16 + rw [BitVec.toNat_ofNat]; exact Nat.le_trans (Nat.mod_le _ _) fl4NumLrs_le_16 + obtain ⟨gv, hgv, -⟩ := hP.gic a + rw [hgv, bind_ok] + refine WP.spec_bind (reg_set_writesInA (v_tcb := v_tcb) hal (by unfold fl4_abi.consts.vcpu_reg.VGIC_HCR; decide)) ?_; intro s4 hw4 + have hloop2 : ∀ (s0 : S) (arr : Array U64 16#usize) (l i : U64), + vcpu.write_state_from_frame_loop2 st s0 vc arr l i = + vcpu.write_state_from_frame_loop0 st s0 vc arr l i := fun _ _ _ _ => by + unfold vcpu.write_state_from_frame_loop2 vcpu.write_state_from_frame_loop0; congr 1 + rw [hloop2] + refine WP.spec_bind (write_loop_lr_writesInA (v_tcb := v_tcb) hal hlrs16) ?_; intro s5 hw5 + refine WP.spec_bind (sync_vgic_status_writesInA (v_tcb := v_tcb) hal hlrs16) ?_; intro s6 hw6 + have hloop3 : ∀ (s0 : S) (gb tp i : U64), + vcpu.write_state_from_frame_loop3 st s0 gb tp i = + vcpu.write_state_from_frame_loop1 st s0 gb tp i := fun _ _ _ _ => by + unfold vcpu.write_state_from_frame_loop3 vcpu.write_state_from_frame_loop1; congr 1 + simp only [hloop3] + exact hGprs s6 (writesIn_trans1A (writesIn_trans1A (writesIn_trans1A hw3 hw4) hw5) hw6) + · rw [if_neg h5] + have hloop4 : ∀ (s0 : S) (gb tp i : U64), + vcpu.write_state_from_frame_loop4 st s0 gb tp i = + vcpu.write_state_from_frame_loop1 st s0 gb tp i := fun _ _ _ _ => by + unfold vcpu.write_state_from_frame_loop4 vcpu.write_state_from_frame_loop1; congr 1 + simp only [hloop4] + exact hGprs s3 hw3 + +/-! ### Pure spec commutation of `fl4WriteStateFromFrame` with `updKSc · ct (.tcb x1)` -/ + +private theorem loadWordOffs_failA (frame : ObjRef) (k : Nat) (σ : State Unit) + (h : ¬ (bufAddr frame k &&& Fl4SpecA64.mask 3 = 0)) : + loadWordOffs (Z := Unit) frame k σ = (∅, True) := by + unfold loadWordOffs doMachineOp + have hlw : loadWord (bufAddr frame k) σ.machineState = (∅, True) := by + unfold loadWord + rw [nd_bind_oneSc (m := NonDet.gets MachineState.underlyingMemory) rfl] + have ha : NonDet.assert (bufAddr frame k &&& Fl4SpecA64.mask 3 = 0) σ.machineState = (∅, True) := by + unfold NonDet.assert + have h' : ¬ (bufAddr frame k &&& Fl4SpecA64.mask 3 = 0#64) := h + exact Prod.ext (by ext x; simp [h']) (by simp [h']) + change NonDet.bind (NonDet.assert _) _ _ = _; unfold NonDet.bind; rw [ha] + exact Prod.ext (by ext x; simp) (by simp) + rw [nd_bind_oneSc (m := NonDet.gets _) rfl] + change NonDet.bind (NonDet.selectF (loadWord (bufAddr frame k) σ.machineState)) _ σ = _ + rw [hlw]; unfold NonDet.selectF NonDet.bind + exact Prod.ext (by ext x; simp) (by simp) + +private theorem getVcpu_failA {vc : ObjRef} {σ : State Unit} + (h : ∀ v, σ.kheap vc ≠ some (.archObj (.vcpu v))) : + getVcpu (Z := Unit) vc σ = (∅, True) := by + unfold getVcpu NonDet.getsMap vcpusOf + change (NonDet.gets _ >>= _) σ = _ + rw [nd_bind_oneSc (m := NonDet.gets _) rfl] + cases hkv : σ.kheap vc with + | none => simp [optMap, aobjsOf, vcpuOf, hkv]; rfl + | some ko => + cases ko with + | archObj ao => + cases ao with + | vcpu v => exact absurd hkv (h v) + | _ => simp [optMap, aobjsOf, vcpuOf, hkv]; rfl + | _ => simp [optMap, aobjsOf, vcpuOf, hkv]; rfl + +private theorem getsThe_getTcb_eqA {t : ObjRef} {xt : Tcb} {σ : State Unit} + (hkt : σ.kheap t = some (.tcb xt)) : + NonDet.getsThe (getTcb (Z := Unit) t) σ = ({(xt, σ)}, False) := by + unfold NonDet.getsThe getTcb readObject + simp [hkt, NonDet.assertOpt]; rfl + +private theorem getsThe_getTcb_failA {t : ObjRef} {σ : State Unit} + (h : ∀ xt, σ.kheap t ≠ some (.tcb xt)) : + NonDet.getsThe (getTcb (Z := Unit) t) σ = (∅, True) := by + unfold NonDet.getsThe getTcb readObject + cases hkt : σ.kheap t with + | none => simp [NonDet.assertOpt]; rfl + | some ko => + cases ko with + | tcb xt => exact absurd hkt (h xt) + | _ => simp [NonDet.assertOpt]; rfl + +private theorem setObject_tcb_eqA {t : ObjRef} {xt nt : Tcb} {σ : State Unit} + (hkt : σ.kheap t = some (.tcb xt)) : + setObject (Z := Unit) t (.tcb nt) σ = ({((), updKSc σ t (.tcb nt))}, False) := by + unfold setObject + have hg : getObject (Z := Unit) t σ = ({(.tcb xt, σ)}, False) := by + unfold getObject NonDet.getsThe readObject + simp [hkt, NonDet.assertOpt]; rfl + rw [nd_bind_oneSc hg] + have ha : NonDet.assert (aType (.tcb xt) = aType (.tcb nt)) σ = ({((), σ)}, False) := by + unfold NonDet.assert; simp [aType] + rw [nd_bind_oneSc ha, nd_bind_oneSc (m := NonDet.get) rfl] + rfl + +/-- Spec predicate preserved by `fl4WriteStateFromFrame vc frame mask vgic` when `ct ≠ vc` and +`ct ≠ v_tcb`: `kheap` at `ct` and all non-`vc`/non-`v_tcb` pointers, `releaseQueue`, `inUserFrame`, +and commutation with updating `ct` to another TCB `x1`. -/ +private theorem fl4VcpuSet_comm_updA {vc ct : ObjRef} {idx : Nat} {val : Word} {x0 x1 : Tcb} + {σ σ' : State Unit} (hne : ct ≠ vc) + (h : fl4VcpuSet (Z := Unit) vc idx val σ = ({((), σ')}, False)) : + (∀ p, p ≠ vc → σ'.kheap p = σ.kheap p) ∧ + σ'.machineState = σ.machineState ∧ σ'.releaseQueue = σ.releaseQueue ∧ + (∀ q, inUserFrame q σ → inUserFrame q σ') ∧ + fl4VcpuSet (Z := Unit) vc idx val (updKSc σ ct (.tcb x1)) = ({((), updKSc σ' ct (.tcb x1))}, False) := by + unfold fl4VcpuSet at h ⊢ + cases hkv : σ.kheap vc with + | none => + have hfail := getVcpu_failA (vc := vc) (σ := σ) (fun _ e => by rw [hkv] at e; cases e) + change NonDet.bind (getVcpu vc) _ σ = _ at h; unfold NonDet.bind at h; rw [hfail] at h; simp at h + | some ko => + cases ko with + | archObj ao => + cases ao with + | vcpu v => + rw [nd_bind_oneSc (getVcpu_eqA hkv), setVcpu_eqA hkv] at h + have hσ' : σ' = updKSc σ vc (.archObj (.vcpu (fl4VcpuRegSet idx val v))) := + (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (Prod.mk.inj h).1)).2.symm + subst hσ' + have hkv1 : (updKSc σ ct (.tcb x1)).kheap vc = some (.archObj (.vcpu v)) := by + rw [updKSc_other _ hne.symm]; exact hkv + refine ⟨fun p hp => updKSc_other _ hp, rfl, rfl, + fun q hq => inUserFrame_updKScA hkv (fun _ e => by cases e) hq, ?_⟩ + rw [nd_bind_oneSc (getVcpu_eqA hkv1), setVcpu_eqA hkv1, updKSc_commA σ hne] + | _ => + have hfail := getVcpu_failA (vc := vc) (σ := σ) (fun _ e => by rw [hkv] at e; cases e) + change NonDet.bind (getVcpu vc) _ σ = _ at h; unfold NonDet.bind at h; rw [hfail] at h; simp at h + | _ => + have hfail := getVcpu_failA (vc := vc) (σ := σ) (fun _ e => by rw [hkv] at e; cases e) + change NonDet.bind (getVcpu vc) _ σ = _ at h; unfold NonDet.bind at h; rw [hfail] at h; simp at h + +private theorem loadWordOffs_comm_updA {frame ct : ObjRef} {off : Nat} {x1 : Tcb} + {σ : State Unit} {w : Word} + (h : loadWordOffs (Z := Unit) frame off σ = ({(w, σ)}, False)) : + loadWordOffs (Z := Unit) frame off (updKSc σ ct (.tcb x1)) = ({(w, updKSc σ ct (.tcb x1))}, False) := by + by_cases hal : bufAddr frame off &&& Fl4SpecA64.mask 3 = 0 + · rw [loadWordOffs_eqA frame off (updKSc σ ct (.tcb x1)) hal] + rw [loadWordOffs_eqA frame off σ hal] at h + have hw : w = loadWordAt σ.machineState.underlyingMemory (bufAddr frame off) := + (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (Prod.mk.inj h).1)).1.symm + rw [hw]; rfl + · rw [loadWordOffs_failA frame off σ hal] at h; simp at h + +private theorem copyRegsFromFrame_comm_updA {vc frame ct : ObjRef} {idxs : List Nat} {x1 : Tcb} + {σ σ' : State Unit} (hne : ct ≠ vc) + (h : copyRegsFromFrame (Z := Unit) vc frame idxs σ = ({((), σ')}, False)) : + (∀ p, p ≠ vc → σ'.kheap p = σ.kheap p) ∧ + σ'.machineState = σ.machineState ∧ σ'.releaseQueue = σ.releaseQueue ∧ + (∀ q, inUserFrame q σ → inUserFrame q σ') ∧ + copyRegsFromFrame (Z := Unit) vc frame idxs (updKSc σ ct (.tcb x1)) = ({((), updKSc σ' ct (.tcb x1))}, False) := by + induction idxs generalizing σ with + | nil => + unfold copyRegsFromFrame NonDet.mapMx at h ⊢ + have hσ' : σ' = σ := (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (Prod.mk.inj h).1)).2.symm + subst hσ'; exact ⟨fun _ _ => rfl, rfl, rfl, fun _ h => h, rfl⟩ + | cons i rest ih => + unfold copyRegsFromFrame NonDet.mapMx at h ⊢ + change ((do let w ← loadWordOffs frame i; fl4VcpuSet vc i w) >>= fun _ => copyRegsFromFrame vc frame rest) σ = _ at h + by_cases hal : bufAddr frame i &&& Fl4SpecA64.mask 3 = 0 + · have hl := loadWordOffs_eqA frame i σ hal + have hl1 := loadWordOffs_eqA frame i (updKSc σ ct (.tcb x1)) hal + rw [nd_bind_assocSc, nd_bind_oneSc hl] at h + -- Extract `fl4VcpuSet` step + cases hkv : σ.kheap vc with + | none => + have hfail : fl4VcpuSet (Z := Unit) vc i (loadWordAt σ.machineState.underlyingMemory (bufAddr frame i)) σ = (∅, True) := by + unfold fl4VcpuSet; change NonDet.bind (getVcpu vc) _ σ = _; unfold NonDet.bind + rw [getVcpu_failA (fun _ e => by rw [hkv] at e; cases e)]; exact Prod.ext (by ext x; simp) (by simp) + change NonDet.bind _ _ σ = _ at h; unfold NonDet.bind at h; rw [hfail] at h; simp at h + | some ko => + cases ko with + | archObj ao => + cases ao with + | vcpu v => + have hset : fl4VcpuSet (Z := Unit) vc i (loadWordAt σ.machineState.underlyingMemory (bufAddr frame i)) σ = + ({((), updKSc σ vc (.archObj (.vcpu (fl4VcpuRegSet i (loadWordAt σ.machineState.underlyingMemory (bufAddr frame i)) v))))}, False) := by + unfold fl4VcpuSet; rw [nd_bind_oneSc (getVcpu_eqA hkv), setVcpu_eqA hkv] + rw [nd_bind_oneSc hset] at h + obtain ⟨hk1, hms1, hrel1, huf1, hcomm1⟩ := fl4VcpuSet_comm_updA (x0 := x1) (x1 := x1) hne hset + obtain ⟨hk2, hms2, hrel2, huf2, hcomm2⟩ := ih h + refine ⟨fun p hp => (hk2 p hp).trans (hk1 p hp), hms2.trans hms1, hrel2.trans hrel1, + fun q hq => huf2 q (huf1 q hq), ?_⟩ + change ((do let w ← loadWordOffs frame i; fl4VcpuSet vc i w) >>= fun _ => copyRegsFromFrame vc frame rest) _ = _ + have hms_eq : (updKSc σ ct (.tcb x1)).machineState = σ.machineState := rfl + rw [hms_eq] at hl1 + rw [nd_bind_assocSc, nd_bind_oneSc hl1, nd_bind_oneSc hcomm1]; exact hcomm2 + | _ => + have hfail : fl4VcpuSet (Z := Unit) vc i (loadWordAt σ.machineState.underlyingMemory (bufAddr frame i)) σ = (∅, True) := by + unfold fl4VcpuSet; change NonDet.bind (getVcpu vc) _ σ = _; unfold NonDet.bind + rw [getVcpu_failA (fun _ e => by rw [hkv] at e; cases e)]; exact Prod.ext (by ext x; simp) (by simp) + change NonDet.bind _ _ σ = _ at h; unfold NonDet.bind at h; rw [hfail] at h; simp at h + | _ => + have hfail : fl4VcpuSet (Z := Unit) vc i (loadWordAt σ.machineState.underlyingMemory (bufAddr frame i)) σ = (∅, True) := by + unfold fl4VcpuSet; change NonDet.bind (getVcpu vc) _ σ = _; unfold NonDet.bind + rw [getVcpu_failA (fun _ e => by rw [hkv] at e; cases e)]; exact Prod.ext (by ext x; simp) (by simp) + change NonDet.bind _ _ σ = _ at h; unfold NonDet.bind at h; rw [hfail] at h; simp at h + · have hl_fail := loadWordOffs_failA frame i σ hal + rw [nd_bind_assocSc] at h + change NonDet.bind (loadWordOffs frame i) _ σ = _ at h; unfold NonDet.bind at h; rw [hl_fail] at h + simp at h + +private theorem asUser_comm_updA {t ct : ObjRef} {f : UserMonad Unit} {x1 : Tcb} + {σ σ' : State Unit} (hne : ct ≠ t) + (h : asUser (Z := Unit) t f σ = ({((), σ')}, False)) : + (∀ p, p ≠ t → σ'.kheap p = σ.kheap p) ∧ + σ'.machineState = σ.machineState ∧ σ'.releaseQueue = σ.releaseQueue ∧ + (∀ q, inUserFrame q σ → inUserFrame q σ') ∧ + asUser (Z := Unit) t f (updKSc σ ct (.tcb x1)) = ({((), updKSc σ' ct (.tcb x1))}, False) := by + cases hkt : σ.kheap t with + | none => + have hgf := getsThe_getTcb_failA (t := t) (σ := σ) (fun _ e => by rw [hkt] at e; cases e) + unfold asUser at h + change NonDet.bind (NonDet.getsThe (getTcb t)) _ σ = _ at h; unfold NonDet.bind at h; rw [hgf] at h; simp at h + | some ko => + cases ko with + | tcb xt => + have hget := getsThe_getTcb_eqA hkt + have hkt1 : (updKSc σ ct (.tcb x1)).kheap t = some (.tcb xt) := by rw [updKSc_other _ hne.symm]; exact hkt + have hget1 := getsThe_getTcb_eqA hkt1 + unfold asUser at h ⊢ + rw [nd_bind_oneSc hget, nd_bind_oneSc (m := pure _) rfl] at h + rw [nd_bind_oneSc hget1, nd_bind_oneSc (m := pure _) rfl] + cases hf_eq : f (archTcbContextGet xt.tcbArch) with + | mk S_f fl_f => + rw [hf_eq] at h + by_cases hfl : fl_f + · have hflT : fl_f = True := propext ⟨fun _ => trivial, fun _ => hfl⟩ + subst hflT + unfold NonDet.selectF at h + change NonDet.bind _ _ σ = _ at h; unfold NonDet.bind at h; simp at h + · have hfl0 : fl_f = False := propext ⟨fun x => hfl x, False.elim⟩ + subst hfl0 + have hmem_f : ∀ p ∈ S_f, ∀ q ∈ S_f, p = q := by + intro p hp q hq + have hp' : (p.1, updKSc σ t (.tcb { xt with tcbArch := archTcbContextSet p.2 xt.tcbArch })) ∈ + ((NonDet.selectF (S_f, False) >>= fun (rv, uc') => do + let newTcb ← pure { xt with tcbArch := archTcbContextSet uc' xt.tcbArch } + setObject t (.tcb newTcb) + return rv) σ).1 := by + change _ ∈ (NonDet.bind (NonDet.selectF (S_f, False)) _ σ).1 + unfold NonDet.selectF NonDet.bind; simp only [Set.mem_iUnion] + refine ⟨(p, σ), ⟨hp, rfl⟩, ?_⟩ + dsimp only; rw [nd_bind_oneSc (m := pure _) rfl, nd_bind_oneSc (setObject_tcb_eqA hkt)]; exact Set.mem_singleton _ + have hq' : (q.1, updKSc σ t (.tcb { xt with tcbArch := archTcbContextSet q.2 xt.tcbArch })) ∈ + ((NonDet.selectF (S_f, False) >>= fun (rv, uc') => do + let newTcb ← pure { xt with tcbArch := archTcbContextSet uc' xt.tcbArch } + setObject t (.tcb newTcb) + return rv) σ).1 := by + change _ ∈ (NonDet.bind (NonDet.selectF (S_f, False)) _ σ).1 + unfold NonDet.selectF NonDet.bind; simp only [Set.mem_iUnion] + refine ⟨(q, σ), ⟨hq, rfl⟩, ?_⟩ + dsimp only; rw [nd_bind_oneSc (m := pure _) rfl, nd_bind_oneSc (setObject_tcb_eqA hkt)]; exact Set.mem_singleton _ + rw [h] at hp' hq' + have h1 := Set.mem_singleton_iff.1 hp' + have h2 := Set.mem_singleton_iff.1 hq' + have hst_eq := (Prod.mk.inj h1).2.trans (Prod.mk.inj h2).2.symm + have hkh_t := congrArg (fun st : State Unit => st.kheap t) hst_eq + simp only [updKSc_same] at hkh_t + have harch := congrArg Tcb.tcbArch (KernelObject.tcb.inj (Option.some.inj hkh_t)) + have huc := congrArg ArchTcb.tcbContext harch + cases p; cases q; exact Prod.ext rfl huc + have hne_sf : S_f.Nonempty := by + by_contra hem + rw [Set.not_nonempty_iff_eq_empty] at hem; subst hem + unfold NonDet.selectF at h + have h1 := (Prod.mk.inj h).1; simp [NonDet.bind] at h1 + obtain ⟨⟨u0, uc'⟩, hmem0⟩ := hne_sf + have hSf_eq : S_f = {(u0, uc')} := by + ext p; constructor + · intro hp; exact Set.mem_singleton_iff.2 (hmem_f p hp (u0, uc') hmem0) + · intro hp; rw [Set.mem_singleton_iff.1 hp]; exact hmem0 + subst hSf_eq + have hsel : NonDet.selectF ({(u0, uc')}, False) σ = ({((u0, uc'), σ)}, False) := selectF_oneA _ _ + have hsel1 : NonDet.selectF ({(u0, uc')}, False) (updKSc σ ct (.tcb x1)) = + ({((u0, uc'), updKSc σ ct (.tcb x1))}, False) := selectF_oneA _ _ + rw [nd_bind_oneSc hsel] at h; dsimp only at h + rw [nd_bind_oneSc (m := pure _) rfl, nd_bind_oneSc (setObject_tcb_eqA hkt)] at h + have hσ' : σ' = updKSc σ t (.tcb { xt with tcbArch := archTcbContextSet uc' xt.tcbArch }) := + (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (Prod.mk.inj h).1)).2.symm + subst hσ' + refine ⟨fun p hp => updKSc_other _ hp, rfl, rfl, + fun q hq => inUserFrame_updKScA hkt (fun _ e => by cases e) hq, ?_⟩ + rw [nd_bind_oneSc hsel1]; dsimp only + rw [nd_bind_oneSc (m := pure _) rfl, nd_bind_oneSc (setObject_tcb_eqA hkt1), updKSc_commA σ hne]; rfl + | _ => + have hgf := getsThe_getTcb_failA (t := t) (σ := σ) (fun _ e => by rw [hkt] at e; cases e) + unfold asUser at h + change NonDet.bind (NonDet.getsThe (getTcb t)) _ σ = _ at h; unfold NonDet.bind at h; rw [hgf] at h; simp at h + +set_option maxHeartbeats 800000 in +/-- `fl4WriteStateFromFrame` preserves `kheap` at every pointer other than `vc` and `v_tcb`, +preserves `machineState`, `releaseQueue`, `inUserFrame`, and commutes with `updKSc · ct (.tcb x1)` +whenever `ct ≠ vc` and `ct ≠ v_tcb`. -/ +theorem fl4WriteStateFromFrame_comm_updA {vc v_tcb ct frame : ObjRef} {mask : Word} + {vgic : Word × List Word} {x1 : Tcb} {σ σ' : State Unit} + (hne_vc : ct ≠ vc) (hne_vt : ct ≠ v_tcb) + {v0 : Vcpu} (hkv0 : σ.kheap vc = some (.archObj (.vcpu v0))) (hvt0 : v0.vcpuTcb = some v_tcb) + (h : fl4WriteStateFromFrame (Z := Unit) vc frame mask vgic σ = ({((), σ')}, False)) : + (∀ p, p ≠ vc → p ≠ v_tcb → σ'.kheap p = σ.kheap p) ∧ + σ'.machineState = σ.machineState ∧ σ'.releaseQueue = σ.releaseQueue ∧ + (∀ q, inUserFrame q σ → inUserFrame q σ') ∧ + fl4WriteStateFromFrame (Z := Unit) vc frame mask vgic (updKSc σ ct (.tcb x1)) = + ({((), updKSc σ' ct (.tcb x1))}, False) := by + -- Helper: any single-step `m` with the invariant composes cleanly under `>>=` + let Inv (τ τ' : State Unit) : Prop := + (∀ p, p ≠ vc → p ≠ v_tcb → τ'.kheap p = τ.kheap p) ∧ + (∀ v, τ.kheap vc = some (.archObj (.vcpu v)) → ∃ v', τ'.kheap vc = some (.archObj (.vcpu v')) ∧ v'.vcpuTcb = v.vcpuTcb) ∧ + τ'.machineState = τ.machineState ∧ τ'.releaseQueue = τ.releaseQueue ∧ + (∀ q, inUserFrame q τ → inUserFrame q τ') + have hInv_refl : ∀ τ, Inv τ τ := fun τ => ⟨fun _ _ _ => rfl, fun v hv => ⟨v, hv, rfl⟩, rfl, rfl, fun _ h => h⟩ + have hInv_trans : ∀ {τ1 τ2 τ3}, Inv τ1 τ2 → Inv τ2 τ3 → Inv τ1 τ3 := fun h1 h2 => + ⟨fun p hp1 hp2 => (h2.1 p hp1 hp2).trans (h1.1 p hp1 hp2), + fun v hv => let ⟨v1, hv1, he1⟩ := h1.2.1 v hv; let ⟨v2, hv2, he2⟩ := h2.2.1 v1 hv1; ⟨v2, hv2, he2.trans he1⟩, + h2.2.2.1.trans h1.2.2.1, h2.2.2.2.1.trans h1.2.2.2.1, fun q hq => h2.2.2.2.2 q (h1.2.2.2.2 q hq)⟩ + -- Helper to extract a deterministic step from `(m >>= f) τ = ({((), τ')}, False)` when `m` is deterministic + have hDetBind : ∀ {m : SMonad Unit Unit} {f : Unit → SMonad Unit Unit} {τ τ' : State Unit} + (hm : ∃ τ1, m τ = ({((), τ1)}, False) ∧ Inv τ τ1 ∧ m (updKSc τ ct (.tcb x1)) = ({((), updKSc τ1 ct (.tcb x1))}, False)) + (hf : ∀ τ1, Inv τ τ1 → f () τ1 = ({((), τ')}, False) → Inv τ1 τ' ∧ f () (updKSc τ1 ct (.tcb x1)) = ({((), updKSc τ' ct (.tcb x1))}, False)), + (m >>= f) τ = ({((), τ')}, False) → + Inv τ τ' ∧ (m >>= f) (updKSc τ ct (.tcb x1)) = ({((), updKSc τ' ct (.tcb x1))}, False) := by + intro m f τ τ' ⟨τ1, hm1, hinv1, hcomm1⟩ hf hbf + rw [nd_bind_oneSc hm1] at hbf + obtain ⟨hinv2, hcomm2⟩ := hf τ1 hinv1 hbf + exact ⟨hInv_trans hinv1 hinv2, by rw [nd_bind_oneSc hcomm1]; exact hcomm2⟩ + -- For `fl4WriteStateFromFrame`, each of the 5 stages is deterministic when it succeeds on `σ`! + -- Rather than re-destructing `fl4WriteStateFromFrame` manually for every branch, observe that + -- `fl4WriteStateFromFrame` is a sequence of `when` / `if` / `copyRegsFromFrame` / `fl4VcpuSet` / + -- `vcpuUpdate (syncVgicStatus)` / `asUser v_tcb` — all of which commute with `updKSc · ct (.tcb x1)`. + have hSet_inv : ∀ (τ : State Unit) (i : Nat) (w : Word) (v : Vcpu), + τ.kheap vc = some (.archObj (.vcpu v)) → + ∃ τ1, fl4VcpuSet (Z := Unit) vc i w τ = ({((), τ1)}, False) ∧ Inv τ τ1 ∧ + fl4VcpuSet (Z := Unit) vc i w (updKSc τ ct (.tcb x1)) = ({((), updKSc τ1 ct (.tcb x1))}, False) := by + intro τ i w v hkv + refine ⟨updKSc τ vc (.archObj (.vcpu (fl4VcpuRegSet i w v))), ?_, ?_, ?_⟩ + · unfold fl4VcpuSet; rw [nd_bind_oneSc (getVcpu_eqA hkv), setVcpu_eqA hkv] + · refine ⟨fun p hp _ => updKSc_other _ hp, fun v' hv' => by + rw [hkv] at hv'; cases hv'; exact ⟨_, updKSc_same _ _ _, by unfold fl4VcpuRegSet; split_ifs <;> rfl⟩, + rfl, rfl, fun q hq => inUserFrame_updKScA hkv (fun _ e => by cases e) hq⟩ + · have hkv1 : (updKSc τ ct (.tcb x1)).kheap vc = some (.archObj (.vcpu v)) := by rw [updKSc_other _ hne_vc.symm]; exact hkv + unfold fl4VcpuSet; rw [nd_bind_oneSc (getVcpu_eqA hkv1), setVcpu_eqA hkv1, updKSc_commA τ hne_vc] + have hSync_inv : ∀ (τ : State Unit) (v : Vcpu), + τ.kheap vc = some (.archObj (.vcpu v)) → + ∃ τ1, (do let v ← getVcpu (Z := Unit) vc; setVcpu vc (syncVgicStatus v fl4NumLrs fl4GicV3)) τ = ({((), τ1)}, False) ∧ Inv τ τ1 ∧ + (do let v ← getVcpu (Z := Unit) vc; setVcpu vc (syncVgicStatus v fl4NumLrs fl4GicV3)) (updKSc τ ct (.tcb x1)) = ({((), updKSc τ1 ct (.tcb x1))}, False) := by + intro τ v hkv + refine ⟨updKSc τ vc (.archObj (.vcpu (syncVgicStatus v fl4NumLrs fl4GicV3))), ?_, ?_, ?_⟩ + · rw [nd_bind_oneSc (getVcpu_eqA hkv), setVcpu_eqA hkv] + · refine ⟨fun p hp _ => updKSc_other _ hp, fun v' hv' => by + rw [hkv] at hv'; cases hv'; exact ⟨_, updKSc_same _ _ _, rfl⟩, + rfl, rfl, fun q hq => inUserFrame_updKScA hkv (fun _ e => by cases e) hq⟩ + · have hkv1 : (updKSc τ ct (.tcb x1)).kheap vc = some (.archObj (.vcpu v)) := by rw [updKSc_other _ hne_vc.symm]; exact hkv + rw [nd_bind_oneSc (getVcpu_eqA hkv1), setVcpu_eqA hkv1, updKSc_commA τ hne_vc] + have hVppi_either : ∀ (τ : State Unit) (v : Vcpu), + τ.kheap vc = some (.archObj (.vcpu v)) → + (∃ τ1, (do + let w ← loadWordOffs (Z := Unit) frame vppiWord + let v ← getVcpu vc + let mk := Function.update v.vcpuVppiMasked .vppiEventIRQ_VTimer (decide (w &&& 1 ≠ 0)) + setVcpu vc { v with vcpuVppiMasked := mk }) τ = ({((), τ1)}, False)) ∨ + (do + let w ← loadWordOffs (Z := Unit) frame vppiWord + let v ← getVcpu vc + let mk := Function.update v.vcpuVppiMasked .vppiEventIRQ_VTimer (decide (w &&& 1 ≠ 0)) + setVcpu vc { v with vcpuVppiMasked := mk }) τ = (∅, True) := by + intro τ v hkv + by_cases hal : bufAddr frame vppiWord &&& Fl4SpecA64.mask 3 = 0 + · rw [nd_bind_oneSc (loadWordOffs_eqA frame vppiWord τ hal), nd_bind_oneSc (getVcpu_eqA hkv), setVcpu_eqA hkv] + exact Or.inl ⟨_, rfl⟩ + · right; have hl_fail := loadWordOffs_failA frame vppiWord τ hal + change NonDet.bind (loadWordOffs frame vppiWord) _ τ = _; unfold NonDet.bind; rw [hl_fail] + exact Prod.ext (by ext x; simp) (by simp) + have hVppi_comm : ∀ (τ τ1 : State Unit) (v : Vcpu), + τ.kheap vc = some (.archObj (.vcpu v)) → + (do + let w ← loadWordOffs (Z := Unit) frame vppiWord + let v ← getVcpu vc + let mk := Function.update v.vcpuVppiMasked .vppiEventIRQ_VTimer (decide (w &&& 1 ≠ 0)) + setVcpu vc { v with vcpuVppiMasked := mk }) τ = ({((), τ1)}, False) → + Inv τ τ1 ∧ + (do + let w ← loadWordOffs (Z := Unit) frame vppiWord + let v ← getVcpu vc + let mk := Function.update v.vcpuVppiMasked .vppiEventIRQ_VTimer (decide (w &&& 1 ≠ 0)) + setVcpu vc { v with vcpuVppiMasked := mk }) (updKSc τ ct (.tcb x1)) = ({((), updKSc τ1 ct (.tcb x1))}, False) := by + intro τ τ1 v hkv h0 + by_cases hal : bufAddr frame vppiWord &&& Fl4SpecA64.mask 3 = 0 + · rw [nd_bind_oneSc (loadWordOffs_eqA frame vppiWord τ hal), nd_bind_oneSc (getVcpu_eqA hkv), setVcpu_eqA hkv] at h0 + have e : τ1 = updKSc τ vc (.archObj (.vcpu { v with vcpuVppiMasked := Function.update v.vcpuVppiMasked .vppiEventIRQ_VTimer (decide (loadWordAt τ.machineState.underlyingMemory (bufAddr frame vppiWord) &&& 1 ≠ 0)) })) := + (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (Prod.mk.inj h0).1)).2.symm + subst τ1 + have hkv1 : (updKSc τ ct (.tcb x1)).kheap vc = some (.archObj (.vcpu v)) := by rw [updKSc_other _ hne_vc.symm]; exact hkv + refine ⟨⟨fun p hp _ => updKSc_other _ hp, fun v' hv' => by rw [hkv] at hv'; cases hv'; exact ⟨_, updKSc_same _ _ _, rfl⟩, + rfl, rfl, fun q hq => inUserFrame_updKScA hkv (fun _ e => by cases e) hq⟩, ?_⟩ + rw [nd_bind_oneSc (loadWordOffs_eqA frame vppiWord (updKSc τ ct (.tcb x1)) hal), + nd_bind_oneSc (getVcpu_eqA hkv1), setVcpu_eqA hkv1, updKSc_commA τ hne_vc]; rfl + · have hl_fail := loadWordOffs_failA frame vppiWord τ hal + change NonDet.bind (loadWordOffs frame vppiWord) _ τ = _ at h0; unfold NonDet.bind at h0; rw [hl_fail] at h0; simp at h0 + have hCopy_det : ∀ (idxs : List Nat) (τ τ' : State Unit) (v : Vcpu), + τ.kheap vc = some (.archObj (.vcpu v)) → + copyRegsFromFrame (Z := Unit) vc frame idxs τ = ({((), τ')}, False) → + Inv τ τ' ∧ copyRegsFromFrame (Z := Unit) vc frame idxs (updKSc τ ct (.tcb x1)) = ({((), updKSc τ' ct (.tcb x1))}, False) := by + intro idxs τ τ' v hkv hc + obtain ⟨hk, hms, hrel, huf, hcomm⟩ := copyRegsFromFrame_comm_updA (x1 := x1) hne_vc hc + -- Also show `vcpuTcb` preserved across `copyRegsFromFrame` + have hvt_pres : ∀ (l : List Nat) (τ0 τ1 : State Unit) (v0 : Vcpu), + τ0.kheap vc = some (.archObj (.vcpu v0)) → + copyRegsFromFrame (Z := Unit) vc frame l τ0 = ({((), τ1)}, False) → + ∃ v1, τ1.kheap vc = some (.archObj (.vcpu v1)) ∧ v1.vcpuTcb = v0.vcpuTcb := by + intro l; induction l with + | nil => intro τ0 τ1 v0 hkv0 hc0; unfold copyRegsFromFrame NonDet.mapMx at hc0 + have e := (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (Prod.mk.inj hc0).1)).2.symm; subst e; exact ⟨v0, hkv0, rfl⟩ + | cons i rest ih => + intro τ0 τ1 v0 hkv0 hc0 + unfold copyRegsFromFrame NonDet.mapMx at hc0 + change ((do let w ← loadWordOffs frame i; fl4VcpuSet vc i w) >>= fun _ => copyRegsFromFrame vc frame rest) τ0 = _ at hc0 + by_cases hal : bufAddr frame i &&& Fl4SpecA64.mask 3 = 0 + · rw [nd_bind_assocSc, nd_bind_oneSc (loadWordOffs_eqA frame i τ0 hal)] at hc0 + obtain ⟨τm, hset, hinv_m, -⟩ := hSet_inv τ0 i _ v0 hkv0 + rw [nd_bind_oneSc hset] at hc0 + obtain ⟨vm, hkvm, hvm_eq⟩ := hinv_m.2.1 v0 hkv0 + obtain ⟨v1, hkv1, hv1_eq⟩ := ih τm τ1 vm hkvm hc0 + exact ⟨v1, hkv1, hv1_eq.trans hvm_eq⟩ + · have hl_fail := loadWordOffs_failA frame i τ0 hal + rw [nd_bind_assocSc] at hc0 + change NonDet.bind (loadWordOffs frame i) _ τ0 = _ at hc0; unfold NonDet.bind at hc0; rw [hl_fail] at hc0 + simp at hc0 + exact ⟨⟨fun p hp _ => hk p hp, fun v' hv' => hvt_pres idxs τ τ' v' hv' hc, hms, hrel, huf⟩, hcomm⟩ + -- Helper: general `NonDet.mapMx` of `fl4VcpuSet` + have hMapLr_det : ∀ (l : List (Nat × Word)) (τ : State Unit) (v : Vcpu), + τ.kheap vc = some (.archObj (.vcpu v)) → + ∃ τ1, NonDet.mapMx (fun (i, w) => fl4VcpuSet (Z := Unit) vc (vrVgicLr0 + i) w) l τ = ({((), τ1)}, False) ∧ + Inv τ τ1 ∧ + NonDet.mapMx (fun (i, w) => fl4VcpuSet (Z := Unit) vc (vrVgicLr0 + i) w) l (updKSc τ ct (.tcb x1)) = ({((), updKSc τ1 ct (.tcb x1))}, False) := by + intro l; induction l with + | nil => intro τ v _; exact ⟨τ, rfl, hInv_refl τ, rfl⟩ + | cons p rest ih => + intro τ v hkv + obtain ⟨τ1, h1, hinv1, hcomm1⟩ := hSet_inv τ (vrVgicLr0 + p.1) p.2 v hkv + obtain ⟨v1, hkv1, -⟩ := hinv1.2.1 v hkv + obtain ⟨τ2, h2, hinv2, hcomm2⟩ := ih τ1 v1 hkv1 + refine ⟨τ2, ?_, hInv_trans hinv1 hinv2, ?_⟩ + · change (fl4VcpuSet (Z := Unit) vc (vrVgicLr0 + p.1) p.2 >>= fun _ => NonDet.mapMx _ rest) τ = _ + rw [nd_bind_oneSc h1]; exact h2 + · change (fl4VcpuSet (Z := Unit) vc (vrVgicLr0 + p.1) p.2 >>= fun _ => NonDet.mapMx _ rest) (updKSc τ ct (.tcb x1)) = _ + rw [nd_bind_oneSc hcomm1]; exact hcomm2 + -- Helper: `NonDet.mapMx` of `loadWordOffs` + `asUser v_tcb` + have hMapGpr_comm : ∀ (l : List (Nat × Register)) (τ τ' : State Unit), + NonDet.mapMx (fun (i, r) => do + let w ← loadWordOffs (Z := Unit) frame (64 + i) + asUser v_tcb (setRegister r (if r = Register.spsrEl1 then sanitiseVcpuSpsr w else w))) l τ = ({((), τ')}, False) → + Inv τ τ' ∧ + NonDet.mapMx (fun (i, r) => do + let w ← loadWordOffs (Z := Unit) frame (64 + i) + asUser v_tcb (setRegister r (if r = Register.spsrEl1 then sanitiseVcpuSpsr w else w))) l (updKSc τ ct (.tcb x1)) = + ({((), updKSc τ' ct (.tcb x1))}, False) := by + intro l; induction l with + | nil => + intro τ τ' h0; unfold NonDet.mapMx at h0 ⊢ + have e : τ' = τ := (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (Prod.mk.inj h0).1)).2.symm; subst τ' + exact ⟨hInv_refl τ, rfl⟩ + | cons p rest ih => + rcases p with ⟨i, r⟩ + intro τ τ' h0; unfold NonDet.mapMx at h0 ⊢; dsimp only at h0 ⊢ + by_cases hal : bufAddr frame (64 + i) &&& Fl4SpecA64.mask 3 = 0 + · have hl := loadWordOffs_eqA frame (64 + i) τ hal + have hl1 := loadWordOffs_eqA frame (64 + i) (updKSc τ ct (.tcb x1)) hal + have hms_eq : (updKSc τ ct (.tcb x1)).machineState = τ.machineState := rfl + rw [hms_eq] at hl1 + change ((do let w ← loadWordOffs frame (64 + i); asUser v_tcb (setRegister r (if r = Register.spsrEl1 then sanitiseVcpuSpsr w else w))) >>= fun _ => NonDet.mapMx _ rest) τ = _ at h0 + rw [nd_bind_assocSc, nd_bind_oneSc hl] at h0 + cases hkt : τ.kheap v_tcb with + | none => + have hgf := getsThe_getTcb_failA (t := v_tcb) (σ := τ) (fun _ e => by rw [hkt] at e; cases e) + have hf : asUser (Z := Unit) v_tcb (setRegister r (if r = Register.spsrEl1 then sanitiseVcpuSpsr (loadWordAt τ.machineState.underlyingMemory (bufAddr frame (64 + i))) else loadWordAt τ.machineState.underlyingMemory (bufAddr frame (64 + i)))) τ = (∅, True) := by + unfold asUser; change NonDet.bind (NonDet.getsThe (getTcb v_tcb)) _ τ = _ + unfold NonDet.bind; rw [hgf]; exact Prod.ext (by ext x; simp) (by simp) + change NonDet.bind _ _ τ = _ at h0; unfold NonDet.bind at h0; rw [hf] at h0; simp at h0 + | some ko => + cases ko with + | tcb xt => + set w_val := if r = Register.spsrEl1 then sanitiseVcpuSpsr (loadWordAt τ.machineState.underlyingMemory (bufAddr frame (64 + i))) else loadWordAt τ.machineState.underlyingMemory (bufAddr frame (64 + i)) + have hset : asUser (Z := Unit) v_tcb (setRegister r w_val) τ = + ({((), updKSc τ v_tcb (.tcb { xt with tcbArch := archTcbContextSet { xt.tcbArch.tcbContext with userRegs := Function.update xt.tcbArch.tcbContext.userRegs r w_val } xt.tcbArch }))}, False) := by + unfold asUser; rw [nd_bind_oneSc (getsThe_getTcb_eqA hkt), nd_bind_oneSc (m := pure _) rfl] + have hs1 : NonDet.selectF (setRegister r w_val (archTcbContextGet xt.tcbArch)) τ = ({(((), { xt.tcbArch.tcbContext with userRegs := Function.update xt.tcbArch.tcbContext.userRegs r w_val }), τ)}, False) := selectF_oneA _ _ + rw [nd_bind_oneSc hs1]; dsimp only; rw [nd_bind_oneSc (m := pure _) rfl, nd_bind_oneSc (setObject_tcb_eqA hkt)]; rfl + rw [nd_bind_oneSc hset] at h0 + obtain ⟨hk1, hms1, hrel1, huf1, hcomm1⟩ := asUser_comm_updA (x1 := x1) hne_vt hset + have hinv1 : Inv τ _ := ⟨fun q _ hq => hk1 q hq, fun v hv => by + have hne_vv : vc ≠ v_tcb := fun e => by rw [e, hkt] at hv; cases hv + exact ⟨v, by rw [hk1 vc hne_vv]; exact hv, rfl⟩, hms1, hrel1, huf1⟩ + obtain ⟨hinv2, hcomm2⟩ := ih _ τ' h0 + refine ⟨hInv_trans hinv1 hinv2, ?_⟩ + change ((do let w ← loadWordOffs frame (64 + i); asUser v_tcb (setRegister r (if r = Register.spsrEl1 then sanitiseVcpuSpsr w else w))) >>= fun _ => NonDet.mapMx _ rest) _ = _ + rw [nd_bind_assocSc, nd_bind_oneSc hl1, nd_bind_oneSc hcomm1]; exact hcomm2 + | _ => + have hgf := getsThe_getTcb_failA (t := v_tcb) (σ := τ) (fun _ e => by rw [hkt] at e; cases e) + have hf : asUser (Z := Unit) v_tcb (setRegister r (if r = Register.spsrEl1 then sanitiseVcpuSpsr (loadWordAt τ.machineState.underlyingMemory (bufAddr frame (64 + i))) else loadWordAt τ.machineState.underlyingMemory (bufAddr frame (64 + i)))) τ = (∅, True) := by + unfold asUser; change NonDet.bind (NonDet.getsThe (getTcb v_tcb)) _ τ = _ + unfold NonDet.bind; rw [hgf]; exact Prod.ext (by ext x; simp) (by simp) + change NonDet.bind _ _ τ = _ at h0; unfold NonDet.bind at h0; rw [hf] at h0; simp at h0 + · have hl_fail := loadWordOffs_failA frame (64 + i) τ hal + change ((do let w ← loadWordOffs frame (64 + i); asUser v_tcb (setRegister r (if r = Register.spsrEl1 then sanitiseVcpuSpsr w else w))) >>= fun _ => NonDet.mapMx _ rest) τ = _ at h0 + rw [nd_bind_assocSc] at h0 + change NonDet.bind (loadWordOffs frame (64 + i)) _ τ = _ at h0; unfold NonDet.bind at h0; rw [hl_fail] at h0 + simp at h0 + -- Helper: extract `Inv` + comm from `NonDet.when P m` when `m` is `Inv`-preserving and deterministic on success + have hDetStep : ∀ {m : SMonad Unit Unit} {f : Unit → SMonad Unit Unit} {τ τ' : State Unit} + (hm : ∀ τ1, m τ = ({((), τ1)}, False) → Inv τ τ1 ∧ m (updKSc τ ct (.tcb x1)) = ({((), updKSc τ1 ct (.tcb x1))}, False)) + (hdet : (∃ τ1, m τ = ({((), τ1)}, False)) ∨ m τ = (∅, True)) + (hf : ∀ τ1, Inv τ τ1 → f () τ1 = ({((), τ')}, False) → Inv τ1 τ' ∧ f () (updKSc τ1 ct (.tcb x1)) = ({((), updKSc τ' ct (.tcb x1))}, False)), + (m >>= f) τ = ({((), τ')}, False) → + Inv τ τ' ∧ (m >>= f) (updKSc τ ct (.tcb x1)) = ({((), updKSc τ' ct (.tcb x1))}, False) := by + intro m f τ τ' hm hdet hf hbf + rcases hdet with ⟨τ1, hm1⟩ | hfail + · obtain ⟨hinv1, hcomm1⟩ := hm τ1 hm1 + rw [nd_bind_oneSc hm1] at hbf + obtain ⟨hinv2, hcomm2⟩ := hf τ1 hinv1 hbf + exact ⟨hInv_trans hinv1 hinv2, by rw [nd_bind_oneSc hcomm1]; exact hcomm2⟩ + · change NonDet.bind m f τ = _ at hbf; unfold NonDet.bind at hbf; rw [hfail] at hbf; simp at hbf + -- Helper: `copyRegsFromFrame` is either `({((), τ1)}, False)` or `(∅, True)` when `vc` is a VCPU + have hCopy_either : ∀ (idxs : List Nat) (τ : State Unit) (v : Vcpu), + τ.kheap vc = some (.archObj (.vcpu v)) → + (∃ τ1, copyRegsFromFrame (Z := Unit) vc frame idxs τ = ({((), τ1)}, False)) ∨ + copyRegsFromFrame (Z := Unit) vc frame idxs τ = (∅, True) := by + intro idxs; induction idxs with + | nil => intro τ _ _; exact Or.inl ⟨τ, rfl⟩ + | cons i rest ih => + intro τ v hkv + unfold copyRegsFromFrame NonDet.mapMx + change (∃ τ1, ((do let w ← loadWordOffs frame i; fl4VcpuSet vc i w) >>= fun _ => copyRegsFromFrame vc frame rest) τ = _) ∨ + ((do let w ← loadWordOffs frame i; fl4VcpuSet vc i w) >>= fun _ => copyRegsFromFrame vc frame rest) τ = _ + by_cases hal : bufAddr frame i &&& Fl4SpecA64.mask 3 = 0 + · rw [nd_bind_assocSc, nd_bind_oneSc (loadWordOffs_eqA frame i τ hal)] + obtain ⟨τ1, hset, hinv1, -⟩ := hSet_inv τ i _ v hkv + rw [nd_bind_oneSc hset] + obtain ⟨v1, hkv1, -⟩ := hinv1.2.1 v hkv + exact ih τ1 v1 hkv1 + · right + have hl_fail := loadWordOffs_failA frame i τ hal + rw [nd_bind_assocSc] + change NonDet.bind (loadWordOffs frame i) _ τ = _; unfold NonDet.bind; rw [hl_fail] + exact Prod.ext (by ext x; simp) (by simp) + -- Helper: `loadWordOffs` + `fl4VcpuSet` either succeeds or fails + have hLdSet_either : ∀ (τ : State Unit) (i : Nat) (fn : Word → Word) (v : Vcpu), + τ.kheap vc = some (.archObj (.vcpu v)) → + (∃ τ1, (do let w ← loadWordOffs (Z := Unit) frame i; fl4VcpuSet vc i (fn w)) τ = ({((), τ1)}, False)) ∨ + (do let w ← loadWordOffs (Z := Unit) frame i; fl4VcpuSet vc i (fn w)) τ = (∅, True) := by + intro τ i fn v hkv + by_cases hal : bufAddr frame i &&& Fl4SpecA64.mask 3 = 0 + · rw [nd_bind_oneSc (loadWordOffs_eqA frame i τ hal)] + obtain ⟨τ1, hset, -, -⟩ := hSet_inv τ i _ v hkv; exact Or.inl ⟨τ1, hset⟩ + · right + have hl_fail := loadWordOffs_failA frame i τ hal + change NonDet.bind (loadWordOffs frame i) _ τ = _; unfold NonDet.bind; rw [hl_fail] + exact Prod.ext (by ext x; simp) (by simp) + have hLdSet_comm : ∀ (τ τ1 : State Unit) (i : Nat) (fn : Word → Word) (v : Vcpu), + τ.kheap vc = some (.archObj (.vcpu v)) → + (do let w ← loadWordOffs (Z := Unit) frame i; fl4VcpuSet vc i (fn w)) τ = ({((), τ1)}, False) → + Inv τ τ1 ∧ (do let w ← loadWordOffs (Z := Unit) frame i; fl4VcpuSet vc i (fn w)) (updKSc τ ct (.tcb x1)) = + ({((), updKSc τ1 ct (.tcb x1))}, False) := by + intro τ τ1 i fn v hkv h0 + by_cases hal : bufAddr frame i &&& Fl4SpecA64.mask 3 = 0 + · rw [nd_bind_oneSc (loadWordOffs_eqA frame i τ hal)] at h0 + obtain ⟨τ1', hset, hinv1, hcomm1⟩ := hSet_inv τ i _ v hkv + rw [hset] at h0 + have e : τ1 = τ1' := (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (Prod.mk.inj h0).1)).2.symm; subst τ1 + exact ⟨hinv1, by rw [nd_bind_oneSc (loadWordOffs_eqA frame i (updKSc τ ct (.tcb x1)) hal)]; exact hcomm1⟩ + · have hl_fail := loadWordOffs_failA frame i τ hal + change NonDet.bind (loadWordOffs frame i) _ τ = _ at h0; unfold NonDet.bind at h0; rw [hl_fail] at h0 + simp at h0 + -- Walk the 5 stages of `fl4WriteStateFromFrame`! + suffices hInv : Inv σ σ' ∧ fl4WriteStateFromFrame (Z := Unit) vc frame mask vgic (updKSc σ ct (.tcb x1)) = + ({((), updKSc σ' ct (.tcb x1))}, False) from + ⟨hInv.1.1, hInv.1.2.2.1, hInv.1.2.2.2.1, hInv.1.2.2.2.2, hInv.2⟩ + unfold fl4WriteStateFromFrame at h ⊢ + -- Stage 1: EL1 + refine hDetStep (m := NonDet.when (mask &&& smEl1 ≠ 0) _) ?_ ?_ ?_ h + · intro τ1 hm1; unfold NonDet.when at hm1 ⊢ + by_cases hp : mask &&& smEl1 ≠ 0 + · rw [if_pos hp] at hm1 ⊢ + refine hDetStep (fun τm hm => hCopy_det _ σ τm v0 hkv0 hm) (hCopy_either _ σ v0 hkv0) + (fun τm hinv_m hm2 => let ⟨vm, hkvm, _⟩ := hinv_m.2.1 v0 hkv0; hCopy_det _ τm τ1 vm hkvm hm2) hm1 + · rw [ite_eq_right hp] at hm1 ⊢ + have e : τ1 = σ := (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (Prod.mk.inj hm1).1)).2.symm; subst τ1 + exact ⟨hInv_refl σ, rfl⟩ + · unfold NonDet.when; by_cases hp : mask &&& smEl1 ≠ 0 + · rw [if_pos hp] + rcases hCopy_either _ σ v0 hkv0 with ⟨τm, hm⟩ | hf + · rw [nd_bind_oneSc hm] + obtain ⟨vm, hkvm, -⟩ := (hCopy_det _ σ τm v0 hkv0 hm).1.2.1 v0 hkv0 + exact hCopy_either _ τm vm hkvm + · right; change NonDet.bind _ _ σ = _; unfold NonDet.bind; rw [hf]; exact Prod.ext (by ext x; simp) (by simp) + · rw [if_neg hp]; exact Or.inl ⟨σ, rfl⟩ + intro σ1 hinv1 h1 + obtain ⟨v1, hkv1, hv1_tcb⟩ := hinv1.2.1 v0 hkv0 + -- Stage 2: HYP + refine hDetStep (m := NonDet.when (mask &&& smHyp ≠ 0) _) ?_ ?_ ?_ h1 + · intro τ2 hm2; unfold NonDet.when at hm2 ⊢ + by_cases hp : mask &&& smHyp ≠ 0 + · rw [if_pos hp] at hm2 ⊢ + refine hDetStep (m := NonDet.when (mask &&& smEl1 = 0) _) + (fun τa ha => by + unfold NonDet.when at ha ⊢; by_cases he : mask &&& smEl1 = 0 + · rw [if_pos he] at ha ⊢; exact hCopy_det _ σ1 τa v1 hkv1 ha + · rw [ite_eq_right he] at ha ⊢ + have e : τa = σ1 := (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (Prod.mk.inj ha).1)).2.symm; subst τa + exact ⟨hInv_refl σ1, rfl⟩) + (by unfold NonDet.when; by_cases he : mask &&& smEl1 = 0 + · rw [if_pos he]; exact hCopy_either _ σ1 v1 hkv1 + · rw [if_neg he]; exact Or.inl ⟨σ1, rfl⟩) + (fun τa hinva ha => by + obtain ⟨va, hkva, -⟩ := hinva.2.1 v1 hkv1 + refine hDetStep (fun τb hb => hCopy_det _ τa τb va hkva hb) (hCopy_either _ τa va hkva) + (fun τb hinvb hb => by + obtain ⟨vb, hkvb, -⟩ := hinvb.2.1 va hkva + rw [← nd_bind_assocSc] at hb ⊢ + refine hDetStep (fun τc hc => hLdSet_comm τb τc vrHcr sanitizeHcr vb hkvb hc) + (hLdSet_either τb vrHcr sanitizeHcr vb hkvb) + (fun τc hinvc hc => let ⟨vc', hkvc', _⟩ := hinvc.2.1 vb hkvb; hCopy_det _ τc τ2 vc' hkvc' hc) hb) ha) hm2 + · rw [ite_eq_right hp] at hm2 ⊢ + have e : τ2 = σ1 := (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (Prod.mk.inj hm2).1)).2.symm; subst τ2 + exact ⟨hInv_refl σ1, rfl⟩ + · unfold NonDet.when; by_cases hp : mask &&& smHyp ≠ 0 + · rw [ite_eq_left hp] + have hsub1 : (∃ τa, (if mask &&& smEl1 = 0 then copyRegsFromFrame (Z := Unit) vc frame [vrVmpidr] else NonDet.pure ()) σ1 = ({((), τa)}, False)) ∨ + (if mask &&& smEl1 = 0 then copyRegsFromFrame (Z := Unit) vc frame [vrVmpidr] else NonDet.pure ()) σ1 = (∅, True) := by + by_cases he : mask &&& smEl1 = 0 + · rw [ite_eq_left he]; exact hCopy_either _ σ1 v1 hkv1 + · rw [ite_eq_right he]; exact Or.inl ⟨σ1, rfl⟩ + rcases hsub1 with ⟨τa, ha⟩ | hf + · rw [nd_bind_oneSc ha] + have hkva : ∃ va, τa.kheap vc = some (.archObj (.vcpu va)) := by + by_cases he : mask &&& smEl1 = 0 + · rw [ite_eq_left he] at ha; obtain ⟨va, hkva, -⟩ := (hCopy_det _ σ1 τa v1 hkv1 ha).1.2.1 v1 hkv1; exact ⟨va, hkva⟩ + · rw [ite_eq_right he] at ha; have e : τa = σ1 := (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (Prod.mk.inj ha).1)).2.symm; subst τa; exact ⟨v1, hkv1⟩ + obtain ⟨va, hkva⟩ := hkva + rcases hCopy_either _ τa va hkva with ⟨τb, hb⟩ | hf + · rw [nd_bind_oneSc hb] + obtain ⟨vb, hkvb, -⟩ := (hCopy_det _ τa τb va hkva hb).1.2.1 va hkva + rw [← nd_bind_assocSc] + rcases hLdSet_either τb vrHcr sanitizeHcr vb hkvb with ⟨τc, hc⟩ | hf + · rw [nd_bind_oneSc hc] + obtain ⟨vc', hkvc', -⟩ := (hLdSet_comm τb τc vrHcr sanitizeHcr vb hkvb hc).1.2.1 vb hkvb + exact hCopy_either _ τc vc' hkvc' + · right; change NonDet.bind _ _ τb = _; unfold NonDet.bind; rw [hf]; exact Prod.ext (by ext x; simp) (by simp) + · right; change NonDet.bind _ _ τa = _; unfold NonDet.bind; rw [hf]; exact Prod.ext (by ext x; simp) (by simp) + · right; change NonDet.bind _ _ σ1 = _; unfold NonDet.bind; rw [hf]; exact Prod.ext (by ext x; simp) (by simp) + · rw [ite_eq_right hp]; exact Or.inl ⟨σ1, rfl⟩ + intro σ2 hinv2 h2 + obtain ⟨v2, hkv2, hv2_tcb⟩ := hinv2.2.1 v1 hkv1 + -- Stage 3: TIMER + refine hDetStep (m := NonDet.when (mask &&& smTimer ≠ 0) _) ?_ ?_ ?_ h2 + · intro τ3 hm3; unfold NonDet.when at hm3 ⊢ + by_cases hp : mask &&& smTimer ≠ 0 + · rw [ite_eq_left hp] at hm3 ⊢ + refine hDetStep (fun τa ha => hCopy_det _ σ2 τa v2 hkv2 ha) (hCopy_either _ σ2 v2 hkv2) + (fun τa hinva ha => let ⟨va, hkva, _⟩ := hinva.2.1 v2 hkv2; hVppi_comm τa τ3 va hkva ha) hm3 + · rw [ite_eq_right hp] at hm3 ⊢ + have e : τ3 = σ2 := (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (Prod.mk.inj hm3).1)).2.symm; subst τ3 + exact ⟨hInv_refl σ2, rfl⟩ + · unfold NonDet.when; by_cases hp : mask &&& smTimer ≠ 0 + · rw [ite_eq_left hp] + rcases hCopy_either _ σ2 v2 hkv2 with ⟨τa, ha⟩ | hf + · rw [nd_bind_oneSc ha] + obtain ⟨va, hkva, -⟩ := (hCopy_det _ σ2 τa v2 hkv2 ha).1.2.1 v2 hkv2 + exact hVppi_either τa va hkva + · right; change NonDet.bind _ _ σ2 = _; unfold NonDet.bind; rw [hf]; exact Prod.ext (by ext x; simp) (by simp) + · rw [ite_eq_right hp]; exact Or.inl ⟨σ2, rfl⟩ + intro σ3 hinv3 h3 + obtain ⟨v3, hkv3, hv3_tcb⟩ := hinv3.2.1 v2 hkv2 + -- Stage 4: VGIC / VGIC_LR + Stage 5: GPRS + have hStage5 : ∀ (σ4 : State Unit) (v4 : Vcpu), + σ4.kheap vc = some (.archObj (.vcpu v4)) → v4.vcpuTcb = some v_tcb → + (NonDet.when (mask &&& smGprs ≠ 0) do + let v ← getVcpu (Z := Unit) vc + match v.vcpuTcb with + | some t => do + NonDet.mapMx (fun (i, r) => do + let w ← loadWordOffs frame (64 + i) + asUser t (setRegister r (if r = Register.spsrEl1 then sanitiseVcpuSpsr w else w))) + (List.range fl4UserRegs.length |>.zip fl4UserRegs) + asUser t do + let pc ← getRegister nextInstructionRegister + setRegister faultRegister pc + | none => pure ()) σ4 = ({((), σ')}, False) → + Inv σ4 σ' ∧ + (NonDet.when (mask &&& smGprs ≠ 0) do + let v ← getVcpu (Z := Unit) vc + match v.vcpuTcb with + | some t => do + NonDet.mapMx (fun (i, r) => do + let w ← loadWordOffs frame (64 + i) + asUser t (setRegister r (if r = Register.spsrEl1 then sanitiseVcpuSpsr w else w))) + (List.range fl4UserRegs.length |>.zip fl4UserRegs) + asUser t do + let pc ← getRegister nextInstructionRegister + setRegister faultRegister pc + | none => pure ()) (updKSc σ4 ct (.tcb x1)) = ({((), updKSc σ' ct (.tcb x1))}, False) := by + intro σ4 v4 hkv4 hv4_eq h4 + unfold NonDet.when at h4 ⊢ + by_cases hp6 : mask &&& smGprs ≠ 0 + · rw [ite_eq_left hp6] at h4 ⊢ + have hkv4' : (updKSc σ4 ct (.tcb x1)).kheap vc = some (.archObj (.vcpu v4)) := by rw [updKSc_other _ hne_vc.symm]; exact hkv4 + rw [nd_bind_oneSc (getVcpu_eqA hkv4), hv4_eq] at h4 + rw [nd_bind_oneSc (getVcpu_eqA hkv4'), hv4_eq] + have hGpr_either : ∀ (l : List (Nat × Register)) (τ : State Unit), + (∃ τ1, NonDet.mapMx (fun (i, r) => do + let w ← loadWordOffs (Z := Unit) frame (64 + i) + asUser v_tcb (setRegister r (if r = Register.spsrEl1 then sanitiseVcpuSpsr w else w))) l τ = ({((), τ1)}, False)) ∨ + NonDet.mapMx (fun (i, r) => do + let w ← loadWordOffs (Z := Unit) frame (64 + i) + asUser v_tcb (setRegister r (if r = Register.spsrEl1 then sanitiseVcpuSpsr w else w))) l τ = (∅, True) := by + intro l; induction l with + | nil => intro τ; exact Or.inl ⟨τ, rfl⟩ + | cons p rest ih => + rcases p with ⟨i, r⟩ + intro τ; unfold NonDet.mapMx; dsimp only + change (∃ τ1, ((do let w ← loadWordOffs frame (64 + i); asUser v_tcb (setRegister r (if r = Register.spsrEl1 then sanitiseVcpuSpsr w else w))) >>= fun _ => NonDet.mapMx _ rest) τ = _) ∨ + ((do let w ← loadWordOffs frame (64 + i); asUser v_tcb (setRegister r (if r = Register.spsrEl1 then sanitiseVcpuSpsr w else w))) >>= fun _ => NonDet.mapMx _ rest) τ = _ + by_cases hal : bufAddr frame (64 + i) &&& Fl4SpecA64.mask 3 = 0 + · rw [nd_bind_assocSc, nd_bind_oneSc (loadWordOffs_eqA frame (64 + i) τ hal)] + cases hkt : τ.kheap v_tcb with + | none => + right; have hgf := getsThe_getTcb_failA (t := v_tcb) (σ := τ) (fun _ e => by rw [hkt] at e; cases e) + have hf : asUser (Z := Unit) v_tcb (setRegister r (if r = Register.spsrEl1 then sanitiseVcpuSpsr (loadWordAt τ.machineState.underlyingMemory (bufAddr frame (64 + i))) else loadWordAt τ.machineState.underlyingMemory (bufAddr frame (64 + i)))) τ = (∅, True) := by + unfold asUser; change NonDet.bind (NonDet.getsThe (getTcb v_tcb)) _ τ = _ + unfold NonDet.bind; rw [hgf]; exact Prod.ext (by ext x; simp) (by simp) + change NonDet.bind _ _ τ = _; unfold NonDet.bind; rw [hf]; exact Prod.ext (by ext x; simp) (by simp) + | some ko => + cases ko with + | tcb xt => + set w_val := if r = Register.spsrEl1 then sanitiseVcpuSpsr (loadWordAt τ.machineState.underlyingMemory (bufAddr frame (64 + i))) else loadWordAt τ.machineState.underlyingMemory (bufAddr frame (64 + i)) + have hset : asUser (Z := Unit) v_tcb (setRegister r w_val) τ = + ({((), updKSc τ v_tcb (.tcb { xt with tcbArch := archTcbContextSet { xt.tcbArch.tcbContext with userRegs := Function.update xt.tcbArch.tcbContext.userRegs r w_val } xt.tcbArch }))}, False) := by + unfold asUser; rw [nd_bind_oneSc (getsThe_getTcb_eqA hkt), nd_bind_oneSc (m := pure _) rfl] + have hs1 : NonDet.selectF (setRegister r w_val (archTcbContextGet xt.tcbArch)) τ = ({(((), { xt.tcbArch.tcbContext with userRegs := Function.update xt.tcbArch.tcbContext.userRegs r w_val }), τ)}, False) := selectF_oneA _ _ + rw [nd_bind_oneSc hs1]; dsimp only; rw [nd_bind_oneSc (m := pure _) rfl, nd_bind_oneSc (setObject_tcb_eqA hkt)]; rfl + rw [nd_bind_oneSc hset]; exact ih _ + | _ => + right; have hgf := getsThe_getTcb_failA (t := v_tcb) (σ := τ) (fun _ e => by rw [hkt] at e; cases e) + have hf : asUser (Z := Unit) v_tcb (setRegister r (if r = Register.spsrEl1 then sanitiseVcpuSpsr (loadWordAt τ.machineState.underlyingMemory (bufAddr frame (64 + i))) else loadWordAt τ.machineState.underlyingMemory (bufAddr frame (64 + i)))) τ = (∅, True) := by + unfold asUser; change NonDet.bind (NonDet.getsThe (getTcb v_tcb)) _ τ = _ + unfold NonDet.bind; rw [hgf]; exact Prod.ext (by ext x; simp) (by simp) + change NonDet.bind _ _ τ = _; unfold NonDet.bind; rw [hf]; exact Prod.ext (by ext x; simp) (by simp) + · right; rw [nd_bind_assocSc] + have hl_fail := loadWordOffs_failA frame (64 + i) τ hal + change NonDet.bind (loadWordOffs frame (64 + i)) _ τ = _; unfold NonDet.bind; rw [hl_fail] + exact Prod.ext (by ext x; simp) (by simp) + refine hDetStep (fun τa ha => hMapGpr_comm _ σ4 τa ha) (hGpr_either _ σ4) (fun τa _ ha => by + obtain ⟨hk1, hms1, hrel1, huf1, hcomm1⟩ := asUser_comm_updA (x1 := x1) hne_vt ha + have hinv1 : Inv τa σ' := ⟨fun q _ hq => hk1 q hq, fun v hv => by + have hne_vv : vc ≠ v_tcb := fun e => by + cases hkt : τa.kheap v_tcb with + | none => + have hgf := getsThe_getTcb_failA (t := v_tcb) (σ := τa) (fun _ e' => by rw [hkt] at e'; cases e') + unfold asUser at ha; change NonDet.bind (NonDet.getsThe (getTcb v_tcb)) _ τa = _ at ha + unfold NonDet.bind at ha; rw [hgf] at ha; simp at ha + | some ko => cases ko with + | tcb _ => rw [e, hkt] at hv; cases hv + | _ => + have hgf := getsThe_getTcb_failA (t := v_tcb) (σ := τa) (fun _ e' => by rw [hkt] at e'; cases e') + unfold asUser at ha; change NonDet.bind (NonDet.getsThe (getTcb v_tcb)) _ τa = _ at ha + unfold NonDet.bind at ha; rw [hgf] at ha; simp at ha + exact ⟨v, by rw [hk1 vc hne_vv]; exact hv, rfl⟩, hms1, hrel1, huf1⟩ + exact ⟨hinv1, hcomm1⟩) h4 + · rw [ite_eq_right hp6] at h4 ⊢ + have e : σ' = σ4 := (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (Prod.mk.inj h4).1)).2.symm; subst σ' + exact ⟨hInv_refl σ4, rfl⟩ + dsimp only at h3 ⊢ + by_cases hp4 : mask &&& smVgic ≠ 0 + · rw [ite_eq_left hp4] at h3 ⊢ + rw [nd_bind_assocSc] at h3 ⊢ + obtain ⟨τa, ha, hinva, hcomma⟩ := hSet_inv σ3 vrVgicHcr vgic.1 v3 hkv3 + refine hDetBind ⟨τa, ha, hinva, hcomma⟩ (fun τa' hinva' ha' => ?_) h3 + obtain ⟨va', hkva', hv_a⟩ := hinva'.2.1 v3 hkv3 + obtain ⟨τl, hl, hinvl, hcomml⟩ := hMapLr_det _ τa' va' hkva' + refine hDetBind ⟨τl, hl, hinvl, hcomml⟩ (fun τl' hinvl' hl' => ?_) ha' + obtain ⟨vl', hkvl', hv_l⟩ := hinvl'.2.1 va' hkva' + refine hDetStep (fun τb hb => hCopy_det _ τl' τb vl' hkvl' hb) (hCopy_either _ τl' vl' hkvl') + (fun τb hinvb hb => by + obtain ⟨vb, hkvb, hv_b⟩ := hinvb.2.1 vl' hkvl' + refine hDetStep (fun τb2 hb2 => hCopy_det _ τb τb2 vb hkvb hb2) (hCopy_either _ τb vb hkvb) + (fun τb2 hinvb2 hb2 => by + obtain ⟨vb2, hkvb2, hv_b2⟩ := hinvb2.2.1 vb hkvb + refine hDetStep (m := NonDet.when fl4GicV3 _) + (fun τc hc => by + unfold NonDet.when at hc ⊢; by_cases hg : fl4GicV3 + · rw [ite_eq_left hg] at hc ⊢; exact hCopy_det _ τb2 τc vb2 hkvb2 hc + · rw [ite_eq_right hg] at hc ⊢ + have e : τc = τb2 := (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (Prod.mk.inj hc).1)).2.symm; subst τc + exact ⟨hInv_refl τb2, rfl⟩) + (by unfold NonDet.when; by_cases hg : fl4GicV3 + · rw [ite_eq_left hg]; exact hCopy_either _ τb2 vb2 hkvb2 + · rw [ite_eq_right hg]; exact Or.inl ⟨τb2, rfl⟩) + (fun τc hinvc hc => by + obtain ⟨vc', hkvc', hv_c⟩ := hinvc.2.1 vb2 hkvb2 + rw [← nd_bind_assocSc] at hc ⊢ + obtain ⟨τe, he, hinve, hcomme⟩ := hSync_inv τc vc' hkvc' + refine hDetBind ⟨τe, he, hinve, hcomme⟩ (fun σ4 hinv4 h4 => by + obtain ⟨v4, hkv4, hv4_tcb⟩ := hinv4.2.1 vc' hkvc' + exact hStage5 σ4 v4 hkv4 + (hv4_tcb.trans (hv_c.trans (hv_b2.trans (hv_b.trans (hv_l.trans (hv_a.trans (hv3_tcb.trans (hv2_tcb.trans (hv1_tcb.trans hvt0))))))))) h4) hc) hb2) hb) hl' + · rw [ite_eq_right hp4] at h3 ⊢ + refine hDetStep (m := NonDet.when (mask &&& smVgicLr ≠ 0) _) + (fun τ4 hm4 => by + unfold NonDet.when at hm4 ⊢ + by_cases hp5 : mask &&& smVgicLr ≠ 0 + · rw [ite_eq_left hp5, nd_bind_assocSc] at hm4 ⊢ + obtain ⟨τa, ha, hinva, hcomma⟩ := hSet_inv σ3 vrVgicHcr vgic.1 v3 hkv3 + refine hDetBind ⟨τa, ha, hinva, hcomma⟩ (fun τa' hinva' ha' => by + obtain ⟨va', hkva', -⟩ := hinva'.2.1 v3 hkv3 + obtain ⟨τb, hb, hinvb, hcommb⟩ := hMapLr_det _ τa' va' hkva' + refine hDetBind ⟨τb, hb, hinvb, hcommb⟩ (fun τb' hinvb' hb' => by + obtain ⟨vb', hkvb', -⟩ := hinvb'.2.1 va' hkva' + obtain ⟨τc, hc, hinvc, hcommc⟩ := hSync_inv τb' vb' hkvb' + rw [hc] at hb' + have e : τ4 = τc := (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (Prod.mk.inj hb').1)).2.symm; subst τ4 + exact ⟨hinvc, hcommc⟩) ha') hm4 + · rw [ite_eq_right hp5] at hm4 ⊢ + have e : τ4 = σ3 := (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (Prod.mk.inj hm4).1)).2.symm; subst τ4 + exact ⟨hInv_refl σ3, rfl⟩) + (by + unfold NonDet.when; by_cases hp5 : mask &&& smVgicLr ≠ 0 + · rw [ite_eq_left hp5, nd_bind_assocSc] + obtain ⟨τa, ha, hinva, -⟩ := hSet_inv σ3 vrVgicHcr vgic.1 v3 hkv3 + rw [nd_bind_oneSc ha]; obtain ⟨va, hkva, -⟩ := hinva.2.1 v3 hkv3 + obtain ⟨τb, hb, hinvb, -⟩ := hMapLr_det _ τa va hkva + rw [nd_bind_oneSc hb]; obtain ⟨vb, hkvb, -⟩ := hinvb.2.1 va hkva + obtain ⟨τc, hc, -, -⟩ := hSync_inv τb vb hkvb + exact Or.inl ⟨τc, hc⟩ + · rw [ite_eq_right hp5]; exact Or.inl ⟨σ3, rfl⟩) + (fun σ4 hinv4 h4 => by + obtain ⟨v4, hkv4, hv4_tcb⟩ := hinv4.2.1 v3 hkv3 + exact hStage5 σ4 v4 hkv4 (hv4_tcb.trans (hv3_tcb.trans (hv2_tcb.trans (hv1_tcb.trans hvt0)))) h4) h3 + +end write_frame_comm + +end +end Fl4ProofsA64.P3.Vcpu diff --git a/proofs/Fl4ProofsA64/P3/VcpuRunBodyA.lean b/proofs/Fl4ProofsA64/P3/VcpuRunBodyA.lean new file mode 100644 index 000000000..13d2ae611 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/VcpuRunBodyA.lean @@ -0,0 +1,499 @@ +import Fl4ProofsA64.P3.VcpuRunBaseA + +/-! # AArch64 P3 VCPU Run Perform Body (`vcpu_run_bodyA`) -/ + +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3.Vcpu + +open Fl4Spec Fl4SpecA64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3 Fl4ProofsA64.P3.Sched +open Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +private theorem runVcpuA_eq (t : U64) : runVcpuA t = wa t 3672#u64 := rfl + +private theorem wa_zeroA (x : U64) : wa x 0#u64 = x := by + apply UScalar.eq_of_val_eq; rw [wa_valP]; have := x.bv.isLt; simp [Nat.mod_eq_of_lt this] + +private theorem updKSc_idemA (σ : State Unit) (p : ObjRef) (o1 o2 : KernelObject) : + updKSc (updKSc σ p o1) p o2 = updKSc σ p o2 := by + cases σ with + | mk a ext => + cases a + simp only [updKSc, State.mk.injEq, AbstractState.mk.injEq, true_and, and_true] + funext q; by_cases h : q = p <;> simp [Function.update, h] + +/-! ## Section 4: Label 69 (`ARMVCPURun`, `vcpu.decode_run`) on `VcpuTopPostA` -/ + +section decode_run +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +theorem getCap_tcbIpcframe_runA {σ : State Unit} {c : ObjRef} {xc : Tcb} + (hkc : σ.kheap c = some (.tcb xc)) : + getCap (Z := Unit) (c, tcbCnodeIndex 2) σ = ({(xc.tcbIpcframe, σ)}, False) := by + unfold getCap; dsimp only + have hobj : getObject (Z := Unit) c σ = ({(.tcb xc, σ)}, False) := by + unfold getObject NonDet.getsThe readObject + simp [NonDet.gets, hkc, NonDet.assertOpt]; rfl + have hmap2 : tcbCnodeMap xc (tcbCnodeIndex 2) = some xc.tcbIpcframe := by + unfold tcbCnodeMap tcbCnodeIndex; rfl + rw [nd_bind_oneSc hobj]; dsimp only + rw [nd_bind_oneSc (m := pure (tcbCnodeMap xc)) rfl, hmap2]; rfl + +theorem lift_vcpu_run_errA {σ : State Unit} {e : SyscallError} + {m : SEMonad Unit Fl4VcpuInvocation} + (hm : m.run σ = ({(.error e, σ)}, False)) : + (NonDetE.liftME Fl4ArchInvocation.vcpu m).run σ = ({(.error e, σ)}, False) := + liftME_vcpu_errA hm + +theorem lift_vcpu_run_okA {σ : State Unit} {inv : Fl4VcpuInvocation} + {m : SEMonad Unit Fl4VcpuInvocation} + (hm : m.run σ = ({(.ok inv, σ)}, False)) : + (.ok (.vcpu inv), σ) ∈ ((NonDetE.liftME Fl4ArchInvocation.vcpu m).run σ).1 := + (liftME_vcpu_okA hm).1 + +theorem ndE_pure_bindA {E α β : Type} (x : α) (f : α → NonDetE (State Unit) E β) (σ : State Unit) : + (ExceptT.run ((pure x : NonDetE (State Unit) E α) >>= f) σ) = (f x).run σ := by + change ((NonDet.pure (Except.ok x) >>= ExceptT.bindCont f) σ) = (f x).run σ + rw [nd_bind_oneSc (m := NonDet.pure (Except.ok x)) rfl]; rfl + +set_option maxHeartbeats 800000 in +theorem vcpu_run_bodyA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} + {OS U Ac Pp Tb Ub : Set U64} {Lv : U64 → Nat} {ia ie : Nat} + {s : S} {a : A} {σ : State Unit} {m : Fl4SpecA64.McState} {vc : U64} + (C : VcpuDecCtxA (L := L) (ar := ar) AR Z H OS U Ac Pp Tb Ub Lv ia ie s a σ m vc) + (hP : VcpuPlatAgreeA ar) (hAK : VcpuArchKeepsA ar AR) + (hVR : VcpuVmRootA st ar AR) (hS2R : VcpuStage2RootA st ar AR) + (hF : FrameMemRelA Z OS Ub (L.ksb s) (L.mem s) σ.machineState.underlyingMemory) + (hU : UbFootDisjA Z OS Ub) (hLD : TcbLinkDisjA Z (L.ksb s)) + (hUbUser : ∀ q ∈ Ub, inUserFrame (specPtr q) σ) + (hksTcb : ∀ t ∈ Z.T, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → + ∀ o : U64, o.val < ksSizeA → wa t K ≠ wa (L.ksb s) o) + (hSlotTcb : ∀ t ∈ Z.T, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → + ∀ y, SlotWA Z.th y → wa t K ≠ y ∧ wa t K ≠ wa y 8#u64 ∧ wa t K ≠ wa y 16#u64 ∧ wa t K ≠ wa y 24#u64) + (hOS_tcb : ∀ t ∈ Z.T, ∀ K : U64, (K = 3664#u64 ∨ K = 3672#u64) → ∀ y ∈ OS, + wa t K ≠ y ∧ wa t K ≠ wa y 8#u64 ∧ wa t K ≠ wa y 16#u64 ∧ wa t K ≠ wa y 24#u64) + (hBufNotS2 : ∀ p sz r d mp asid va, + capAtA (L.mem s) (sbWordA (L.curW s)) ≠ .Frame p sz r d mp true asid va) + (hCurNoRun : L.mem s (runVcpuA (L.curW s)) = 0#u64) + (hCurNotIdle : ∀ c ∈ Z.Cs, L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) ≠ L.curW s) + (hCurSched : L.mem s (scA (L.curW s)) ≠ 0#u64 ∧ L.rmax s (L.mem s (scA (L.curW s))) ≠ 0#u64 ∧ + (L.mem s (inqA (L.curW s)) &&& sched.Q_RELEASE) = 0#u64 ∧ + specPtr (L.curW s) ∉ σ.releaseQueue) + (hSlotSt : ∀ y, SlotWA Z.th y → stA (L.curW s) ≠ y ∧ stA (L.curW s) ≠ wa y 8#u64 ∧ + stA (L.curW s) ≠ wa y 16#u64 ∧ stA (L.curW s) ≠ wa y 24#u64) + (hOS_st : ∀ y ∈ OS, stA (L.curW s) ≠ y ∧ stA (L.curW s) ≠ wa y 8#u64 ∧ + stA (L.curW s) ≠ wa y 16#u64 ∧ stA (L.curW s) ≠ wa y 24#u64) + (hKs_st : ∀ o : U64, o.val < ksSizeA → stA (L.curW s) ≠ wa (L.ksb s) o) + (hSc_st : ∀ c ∈ Z.C, stA (L.curW s) ≠ wa c 24#u64 ∧ stA (L.curW s) ≠ wa c 72#u64 ∧ + stA (L.curW s) ≠ maxA c ∧ Aligned (maxA c)) + (hSc_reg : ∀ c ∈ Z.C, Endp.regA (L.curW s) layout.reg.FAULT_IP ≠ maxA c) + (hVs_st : ¬ VsFootA Z.Pp Z.S1 Z.Y (L.ksb s) (stA (L.curW s))) + (hExt_st : ¬ ExtFootE Z.X Z.Y Z.T2 (stA (L.curW s))) + (hNtfn : ∀ xct, σ.kheap (specPtr (L.curW s)) = some (.tcb xct) → + FinishNotificationA (st := st) s σ (L.curW s) xct ∧ + (∀ n, L.mem s (boundA (L.curW s)) = n → n ≠ 0#u64 → + NtfnStateWordDisjA Z OS Ub (L.ksb s) n ∧ Aligned (ntfnBdA n) ∧ + (∀ b, (∃ i, i < 64 ∧ b = vcpuRegWA vc i) → b ≠ epTSA n ∧ b ≠ ntfnBdA n) ∧ + (∀ v_tcb ∈ Z.T, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → wa v_tcb K ≠ epTSA n ∧ wa v_tcb K ≠ ntfnBdA n) ∧ + (∀ b, VcPage Z.V b → b ≠ epTSA n ∧ b ≠ ntfnBdA n) ∧ + (∀ t ∈ Z.T, ∀ K : U64, K.val < 4096 → wa t K ≠ epTSA n ∧ wa t K ≠ ntfnBdA n))) + {args : List U64} {a0 a1 a2 frame : U64} + (hm2_0 : ¬ (a2.bv &&& ~~~smAll ≠ 0)) + (hcurT : L.curW s ∈ Z.T) + {xct : Tcb} (hkct : σ.kheap (specPtr (L.curW s)) = some (.tcb xct)) + (hsb_os : sbWordA (L.curW s) ∈ OS) + {v0 : Vcpu} (hkv0 : σ.kheap (specPtr vc) = some (.archObj (.vcpu v0))) + (hvt0 : v0.vcpuTcb = optRefA (L.mem s vc)) + (hvt_eq0 : L.mem s vc ≠ 0#u64) (hvt_ct : L.mem s vc ≠ L.curW s) + (hct_v0 : L.mem s (wa (L.curW s) 3664#u64) = 0#u64) + (hrc_eq0 : L.mem s (wa vc 24#u64) = 0#u64) + (hrc_off : vcpu.RUN_CALLER = 24#u64) + (hWin : FrameWinA Ub frame) + (hvtT : L.mem s vc ∈ Z.T) + (hCs128 : ∀ c ∈ Z.Cs, c.val < 128) (hcpu128 : (L.cpu s).val < 128) + (hX0 : VcpuXferStateA (L := L) AR Z OS Ub m a s σ) + (hst_restart : setThreadState (Z := Unit) σ.curThread .restart σ = ({((), stUpdA σ (specPtr (L.curW s)) xct .restart)}, False)) + (vgic_c : U64 × Array U64 16#usize) (vgic_s : Word × List Word) + (hVgic : a1.bv &&& (smVgic ||| smVgicLr) ≠ 0 → + vgic_c.1.bv = vgic_s.1 ∧ vgic_s.1 >>> 32 = 0 ∧ + vgic_s.2.length = fl4NumLrs ∧ ∀ j, j < fl4NumLrs → (vgic_c.2.val[j]!).bv = vgic_s.2[j]!) + (hd_ok : (decodeVcpuRun (Z := Unit) (specPtr vc) (args.map UScalar.bv) true).run σ = + ({(.ok (.run (specPtr vc) (specPtr (L.curW s)) (specPtr frame) a0.bv a1.bv a2.bv vgic_s), σ)}, False)) + (a3 : A) (hAR3 : AR a3 Z.hw) : + (do + let i21 ← lift (wa vc vcpu.OWNER_CPU) + let s3 ← st.write s i21 (wa (L.cpu s) 1#u64) + let s4 ← vcpu.write_state_from_frame st ar s3 a3 vc frame a1 vgic_c + let i22 ← lift (wa vc vcpu.RUN_CALLER) + let s5 ← st.write s4 i22 (L.curW s) + let i23 ← lift (wa vc vcpu.RUN_OFFSET) + let s6 ← st.write s5 i23 a0 + let i24 ← lift (wa vc vcpu.RUN_OUT_MASK) + let s7 ← st.write s6 i24 a2 + let i25 ← layout.tcb.RUN_VCPU + let i26 ← lift (wa (L.curW s) i25) + let s8 ← st.write s7 i26 vc + let (s9, a4) ← vspace.set_stage2_root st ar s8 a3 (L.mem s vc) + let s10 ← sched.set_thread_state st s9 (L.curW s) sched.TS_RUNNING_VM + let (s11, a5) ← vcpu.check st ar s10 a4 (L.curW s) + ok (core.result.Result.Ok (), s11, a5)) ⦃ res => + VcpuTopPostA (L := L) AR Z OS m true σ + ((NonDetE.liftME Fl4ArchInvocation.vcpu <| + decodeVcpuRun (Z := Unit) (specPtr vc) (args.map UScalar.bv) true).run σ) s res ⦄ := by + set ct := L.curW s + set v_tcb := L.mem s vc + set σ_r := stUpdA σ (specPtr ct) xct .restart + have how_off : vcpu.OWNER_CPU = 8#u64 := by unfold vcpu.OWNER_CPU; rfl + rw [how_off, lift_ok, bind_ok, L.write_eq, bind_ok] + have X_a3 : VcpuXferStateA (L := L) AR Z OS Ub m a3 s σ := hX0.of_hw hAR3 + obtain ⟨X3, hpres3⟩ := X_a3.write_owner_cpu hCs128 hcpu128 C.hExt C.hSw C.hfa C.hsub (fun c hc => X_a3.j.1.idleT c hc) C.hvc + set s3 := L.wr s (wa vc 8#u64) (wa (L.cpu s) 1#u64) + have hksb3 : L.ksb s3 = L.ksb s := L.ksb_wr _ _ _ + have hcpu3 : L.cpu s3 = L.cpu s := L.cpu_wr _ _ _ + have hfa3 : ∀ q, FootA (L := L) Z s3 q → Aligned q := fun q hq => C.hfa q (footA_of_ksb_cpuA hksb3 hcpu3 hq) + have hvc_tcb3 : L.mem s3 vc = v_tcb := by + rw [← wa_zeroA vc, mem_wr_neA (aligned_wa (C.hV.place.vc_al C.hvc) 8#u64 (by decide)) + (aligned_wa (C.hV.place.vc_al C.hvc) 0#u64 (by decide)) + (vc_off_ne C.hV.place C.hvc C.hvc (by decide) (by decide) (fun _ => by decide)), wa_zeroA] + have hne_reg_vc : ∀ i, i < 64 → vcpuRegWA vc i ≠ vc := fun i hi => by + unfold vcpuRegWA; conv_rhs => rw [← wa_zeroA vc] + exact vc_off_ne C.hV.place C.hvc C.hvc (o1 := vcpuRegOffA i) (o2 := 0#u64) (by rw [vcpuRegOffA_val hi]; omega) (by decide) + (fun _ e => by have h0 : (0#u64).val = 0 := rfl; have := congrArg UScalar.val e; rw [vcpuRegOffA_val hi, h0] at this; omega) + have hne_tcb_vc : ∀ K : U64, 256 ≤ K.val → K.val < 3328 → wa v_tcb K ≠ vc := fun K _ hK => by + rw [← wa_zeroA vc]; exact (C.hV.place.disj_tcb vc C.hvc v_tcb hvtT 0#u64 K (by decide) (by omega)).symm + -- Combine `write_state_from_frame_refinesA` with `write_state_from_frame_writesInA` + have hWsf_both : vcpu.write_state_from_frame st ar s3 a3 vc frame a1 vgic_c ⦃ s4 => + (∃ σ4, fl4WriteStateFromFrame (specPtr vc) (specPtr frame) a1.bv vgic_s σ = ({((), σ4)}, False) ∧ + VcpuXferStateA (L := L) AR Z OS Ub m a3 s4 σ4) ∧ + WritesIn st s3 s4 (VcpuWriteFrameSetA vc v_tcb) ⦄ := + WP.spec_and + (write_state_from_frame_refinesA X3 hP hU hCs128 (hcpu3 ▸ hcpu128) C.hExt C.hSw hfa3 C.hsub + (fun c hc => X3.j.1.idleT c hc) (by rw [hksb3]; exact hksTcb) hSlotTcb C.hvc hWin hVgic) + (write_state_from_frame_writesInA hP (C.hV.place.vc_al C.hvc) (hU.tcb_al v_tcb hvtT) hvc_tcb3 hne_reg_vc hne_tcb_vc) + refine WP.spec_bind hWsf_both ?_ + rintro s4 ⟨⟨σ4, hsp4, X4⟩, hw4⟩ + have hksb4 : L.ksb s4 = L.ksb s := hw4.1.trans hksb3 + have hcpu4 : L.cpu s4 = L.cpu s := hw4.2.1.trans hcpu3 + have hfa4 : ∀ q, FootA (L := L) Z s4 q → Aligned q := fun q hq => C.hfa q (footA_of_ksb_cpuA hksb4 hcpu4 hq) + -- Show `s4` preserves `vc.RUN_CALLER = 0`, `ct.VCPU = 0`, `ct.RUN_VCPU = 0`, `scA ct`, `inqA ct`, `maxA`, `IDLE_THREAD`, `OS` + have hnot_wsf_vc24 : ¬ VcpuWriteFrameSetA vc v_tcb (wa vc 24#u64) := fun h => by + rcases h with heq | ⟨i, hi, heq⟩ | ⟨K, _, hK, heq⟩ + · exact vc_off_ne C.hV.place C.hvc C.hvc (by decide) (by decide) (fun _ => by decide) heq + · exact vc_off_ne C.hV.place C.hvc C.hvc (by decide) (by rw [vcpuRegOffA_val hi]; omega) + (fun _ e => by have h24 : (24#u64).val = 24 := rfl; have := congrArg UScalar.val e; rw [vcpuRegOffA_val hi, h24] at this; omega) heq + · exact C.hV.place.disj_tcb vc C.hvc v_tcb hvtT 24#u64 K (by decide) (by omega) heq + have hnot_wsf_tcb : ∀ t ∈ Z.T, ∀ o : U64, o.val < 4096 → (t = v_tcb → o.val < 256 ∨ 3328 ≤ o.val) → + ¬ VcpuWriteFrameSetA vc v_tcb (wa t o) := fun t ht o ho hne h => by + rcases h with heq | ⟨i, hi, heq⟩ | ⟨K, hK1, hK2, heq⟩ + · exact C.hV.place.disj_tcb vc C.hvc t ht 16#u64 o (by decide) ho heq.symm + · exact C.hV.place.disj_tcb vc C.hvc t ht (vcpuRegOffA i) o (by rw [vcpuRegOffA_val hi]; omega) ho heq.symm + · exact field_ne (hU.tcb_al t ht) (hU.tcb_al v_tcb hvtT) ho (by omega) + (fun e => by subst e; intro eq; rcases hne rfl with h1 | h2 <;> omega) heq + have hrc4 : L.mem s4 (wa vc 24#u64) = 0#u64 := by + have h3 : L.mem s3 (wa vc 24#u64) = 0#u64 := by + rw [mem_wr_neA (aligned_wa (C.hV.place.vc_al C.hvc) 8#u64 (by decide)) + (aligned_wa (C.hV.place.vc_al C.hvc) 24#u64 (by decide)) + (vc_off_ne C.hV.place C.hvc C.hvc (by decide) (by decide) (fun _ => by decide))]; exact hrc_eq0 + rw [hw4.2.2 _ (aligned_wa (C.hV.place.vc_al C.hvc) 24#u64 (by decide)) hnot_wsf_vc24]; exact h3 + have hct_eq4 : ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → + L.mem s4 (wa ct o) = L.mem s (wa ct o) := fun o ho ho8 => by + rw [hw4.2.2 _ (aligned_wa (C.hV.place.al_t ct hcurT) o ho8) + (hnot_wsf_tcb ct hcurT o ho (fun e => absurd e.symm hvt_ct))] + exact mem_wr_neA (aligned_wa (C.hV.place.vc_al C.hvc) 8#u64 (by decide)) + (aligned_wa (C.hV.place.al_t ct hcurT) o ho8) + (C.hV.place.disj_tcb vc C.hvc ct hcurT 8#u64 o (by decide) ho) + have htv4 : L.mem s4 (wa ct 3664#u64) = 0#u64 := by + rw [hct_eq4 3664#u64 (by decide) (by decide)]; exact hct_v0 + have hrv4 : L.mem s4 (runVcpuA ct) = 0#u64 := by + rw [runVcpuA_eq, hct_eq4 3672#u64 (by decide) (by decide), ← runVcpuA_eq]; exact hCurNoRun + have hsc4 : L.mem s4 (scA ct) = L.mem s (scA ct) := by + rw [scA_eq, hct_eq4 3464#u64 (by decide) (by decide), ← scA_eq] + have hiq4 : L.mem s4 (inqA ct) = L.mem s (inqA ct) := by + rw [inqA_eq, hct_eq4 3592#u64 (by decide) (by decide), ← inqA_eq] + have hmax4 : ∀ c ∈ Z.C, L.mem s4 (maxA c) = L.mem s (maxA c) := fun c hc => by + rw [hw4.2.2 _ (hSc_st c hc).2.2.2 (fun h => by + rcases h with heq | ⟨i, hi, heq⟩ | ⟨K, _, hK, heq⟩ + · rw [maxA_eqA] at heq + exact C.hV.place.disj_sc vc C.hvc c hc 16#u64 72#u64 (by decide) (by decide) heq.symm + · rw [maxA_eqA] at heq + exact C.hV.place.disj_sc vc C.hvc c hc (vcpuRegOffA i) 72#u64 (by rw [vcpuRegOffA_val hi]; omega) (by decide) heq.symm + · rw [maxA_eqA] at heq + exact hU.tcb_offSc v_tcb hvtT K (by omega) (by omega) c hc 72#u64 (by decide) heq.symm)] + exact hpres3.sc_max c hc + have hidle4 : ∀ c ∈ Z.Cs, L.mem s4 (L.nodeAt s4 c layout.node.IDLE_THREAD) = L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) := fun c hc => by + rw [nodeAt_ksb hw4.1, hw4.2.2 _ (X3.vcpu.place.node_al c layout.node.IDLE_THREAD (by unfold layout.node.IDLE_THREAD; decide)) (fun h => by + have hnd3 : L.nodeAt s3 c layout.node.IDLE_THREAD = L.nodeAt s c layout.node.IDLE_THREAD := nodeAt_ksb hksb3 _ _ + rw [hnd3] at h + obtain ⟨o, ho, heq_ks⟩ := nodeAt_eq_wa_ksbA (L := L) s c layout.node.IDLE_THREAD (hCs128 c hc) (by unfold layout.node.IDLE_THREAD; decide) + rcases h with heq | ⟨i, hi, heq⟩ | ⟨K, hK1, hK2, heq⟩ + · have hfit := C.E.j.1.ksFit + have hv : (wa (L.ksb s) o).val = (L.ksb s).val + o.val := by rw [wa_valP, Nat.mod_eq_of_lt (by omega)] + exact C.hV.place.disj_ks _ ⟨vc, C.hvc, 16#u64, by decide, by decide, rfl⟩ (by rw [← heq, heq_ks, hv]; omega) + · have hfit := C.E.j.1.ksFit + have hv : (wa (L.ksb s) o).val = (L.ksb s).val + o.val := by rw [wa_valP, Nat.mod_eq_of_lt (by omega)] + exact C.hV.place.disj_ks _ (vcpuRegWA_in_vcPage C.hvc hi) (by rw [← heq, heq_ks, hv]; omega) + · exact hksTcb v_tcb hvtT K hK1 hK2 o ho (heq.symm.trans heq_ks))] + exact hpres3.idle c hc + have hos4 : OSKeep st OS s s4 := fun y hy => by + have hal_y := C.hV.place.al_os y hy + have hdisj : ∀ q, q = y ∨ q = wa y 8#u64 ∨ q = wa y 16#u64 ∨ q = wa y 24#u64 → + ¬ VcpuWriteFrameSetA vc v_tcb q := fun q hq hw => by + rcases hw with heq | ⟨i, hi, heq⟩ | ⟨K, hK1, hK2, heq⟩ + · have hd := C.hV.place.disj_os _ ⟨vc, C.hvc, 16#u64, by decide, by decide, rfl⟩ y hy + rcases hq with rfl | rfl | rfl | rfl + · exact hd.1 heq.symm + · exact hd.2.1 heq.symm + · exact hd.2.2.1 heq.symm + · exact hd.2.2.2 heq.symm + · have hd := C.hV.place.disj_os _ (vcpuRegWA_in_vcPage C.hvc hi) y hy + rcases hq with rfl | rfl | rfl | rfl + · exact hd.1 heq.symm + · exact hd.2.1 heq.symm + · exact hd.2.2.1 heq.symm + · exact hd.2.2.2 heq.symm + · have hd := hU.tcb_offOs v_tcb hvtT K (by omega) (by omega) y hy + rcases hq with rfl | rfl | rfl | rfl + · exact hd.1 heq.symm + · exact hd.2.1 heq.symm + · exact hd.2.2.1 heq.symm + · exact hd.2.2.2 heq.symm + exact ⟨(hw4.2.2 _ hal_y (hdisj _ (Or.inl rfl))).trans (hpres3.os y hy).1, + (hw4.2.2 _ (aligned_wa hal_y 8#u64 (by decide)) (hdisj _ (Or.inr (Or.inl rfl)))).trans (hpres3.os y hy).2.1, + (hw4.2.2 _ (aligned_wa hal_y 16#u64 (by decide)) (hdisj _ (Or.inr (Or.inr (Or.inl rfl))))).trans (hpres3.os y hy).2.2.1, + (hw4.2.2 _ (aligned_wa hal_y 24#u64 (by decide)) (hdisj _ (Or.inr (Or.inr (Or.inr rfl))))).trans (hpres3.os y hy).2.2.2⟩ + -- Apply `fl4WriteStateFromFrame_comm_updA` to commute `.restart` past `fl4WriteStateFromFrame`! + have hvt0_some : v0.vcpuTcb = some (specPtr v_tcb) := by + rw [hvt0]; unfold optRefA; rw [if_neg hvt_eq0] + have hne_ct_vc : specPtr ct ≠ specPtr vc := fun e => by rw [e, hkv0] at hkct; cases hkct + have hne_ct_vt : specPtr ct ≠ specPtr v_tcb := fun e => hvt_ct (specPtr_inj e).symm + obtain ⟨hkh4_other, -, hrel4, huf4, hsp4_r⟩ := + fl4WriteStateFromFrame_comm_updA (x1 := { xct with tcbState := .restart }) hne_ct_vc hne_ct_vt hkv0 hvt0_some hsp4 + have hkt4 : σ4.kheap (specPtr ct) = some (.tcb xct) := by + rw [hkh4_other (specPtr ct) hne_ct_vc hne_ct_vt]; exact hkct + obtain ⟨v4, hkv4, -, -⟩ := X4.vcpu.core.obj vc C.hvc + have hcur4_eq : L.curW s4 = ct := by + unfold LawfulStore.curW; rw [hcpu4, nodeAt_ksb hw4.1, hw4.2.2 _ (X3.vcpu.place.node_al (L.cpu s) layout.node.CUR_THREAD (by unfold layout.node.CUR_THREAD; decide)) (fun h => by + rw [nodeAt_ksb hksb3] at h + obtain ⟨o, ho, heq_ks⟩ := nodeAt_eq_wa_ksbA (L := L) s (L.cpu s) layout.node.CUR_THREAD hcpu128 (by unfold layout.node.CUR_THREAD; decide) + rcases h with heq | ⟨i, hi, heq⟩ | ⟨K, hK1, hK2, heq⟩ + · have hfit := C.E.j.1.ksFit + have hv : (wa (L.ksb s) o).val = (L.ksb s).val + o.val := by rw [wa_valP, Nat.mod_eq_of_lt (by omega)] + exact C.hV.place.disj_ks _ ⟨vc, C.hvc, 16#u64, by decide, by decide, rfl⟩ (by rw [← heq, heq_ks, hv]; omega) + · have hfit := C.E.j.1.ksFit + have hv : (wa (L.ksb s) o).val = (L.ksb s).val + o.val := by rw [wa_valP, Nat.mod_eq_of_lt (by omega)] + exact C.hV.place.disj_ks _ (vcpuRegWA_in_vcPage C.hvc hi) (by rw [← heq, heq_ks, hv]; omega) + · exact hksTcb v_tcb hvtT K hK1 hK2 o ho (heq.symm.trans heq_ks))] + rw [nodeAt_ksb hksb3] + obtain ⟨o, ho, heq_ks⟩ := nodeAt_eq_wa_ksbA (L := L) s (L.cpu s) layout.node.CUR_THREAD hcpu128 (by unfold layout.node.CUR_THREAD; decide) + exact mem_wr_neA (aligned_wa (C.hV.place.vc_al C.hvc) 8#u64 (by decide)) + (C.hV.place.node_al (L.cpu s) layout.node.CUR_THREAD (by unfold layout.node.CUR_THREAD; decide)) + (fun e => by + have hfit := C.E.j.1.ksFit + have hv : (wa (L.ksb s) o).val = (L.ksb s).val + o.val := by rw [wa_valP, Nat.mod_eq_of_lt (by omega)] + exact C.hV.place.disj_ks _ ⟨vc, C.hvc, 8#u64, by decide, by decide, rfl⟩ (by rw [e, heq_ks, hv]; omega)) + -- Now write `RUN_CALLER`, `RUN_OFFSET`, `RUN_OUT_MASK`, `RUN_VCPU`, `set_stage2_root`, `TS_RUNNING_VM`! + have hro_off : vcpu.RUN_OFFSET = 32#u64 := by unfold vcpu.RUN_OFFSET; rfl + have hrm_off : vcpu.RUN_OUT_MASK = 40#u64 := by unfold vcpu.RUN_OUT_MASK; rfl + rw [hrc_off, lift_ok, bind_ok, L.write_eq, bind_ok, + hro_off, lift_ok, bind_ok, L.write_eq, bind_ok, + hrm_off, lift_ok, bind_ok, L.write_eq, bind_ok, + layout_tcb_run_vcpu_eqA, bind_ok, lift_ok, bind_ok, ← runVcpuA_eq, L.write_eq, bind_ok] + refine WP.spec_bind (hS2R.set_stage2_root _ a3 Z.hw v_tcb X4.j.1.ar) ?_ + rintro ⟨s9, a4⟩ ⟨hs9, hAR4⟩ + dsimp only at hs9; subst hs9 + have X4_a4 : VcpuXferStateA (L := L) AR Z OS Ub m a4 s4 σ4 := X4.of_hw hAR4 + have hscC : L.mem s (scA ct) ∈ Z.C := C.E.j.1.rest.scMem ct hcurT hCurSched.1 + obtain ⟨hsts_ok, hsp_enter, X10, hos10, hmeta10, hmax10, hidle10⟩ := + X4_a4.enter_run_vm (offset := a0) hU (by rw [hksb4]; exact hLD) hCs128 (hcpu4 ▸ hcpu128) + C.hExt C.hSw hfa4 C.hsub (fun c hc => X4_a4.j.1.idleT c hc) hOS_tcb + (by rw [hcur4_eq]; exact hSlotSt) (by rw [hcur4_eq]; exact hOS_st) + (by rw [hcur4_eq, hksb4]; exact hKs_st) (by rw [hcur4_eq]; exact hSc_st) + (by rw [hcur4_eq, hksb4]; exact hVs_st) (by rw [hcur4_eq]; exact hExt_st) + hcur4_eq.symm C.hvc hcurT hrc4 htv4 hrv4 + (fun c hc => by rw [hidle4 c hc]; exact hCurNotIdle c hc) + (hsc4 ▸ hCurSched.1) + (by show L.mem s4 (maxA (L.mem s4 (scA ct))) ≠ 0#u64; rw [hsc4, hmax4 _ hscC]; exact hCurSched.2.1) + (hiq4 ▸ hCurSched.2.2.1) (by rw [hrel4]; exact hCurSched.2.2.2) + (Classical.not_not.mp hm2_0) + (by obtain ⟨y, hy, hc⟩ := C.hV.live vc C.hvc (Or.inl hvt_eq0); exact ⟨y, hy, by unfold capAtA; rw [(hos4 y hy).1, (hos4 y hy).2.1]; exact hc⟩) + hkv4 hkt4 + refine WP.spec_bind (Pₘ := fun r => r = L.wr (L.wr (L.wr (L.wr (L.wr s4 (wa vc 24#u64) ct) (wa vc 32#u64) a0) (wa vc 40#u64) a2) (runVcpuA ct) vc) (stA ct) sched.TS_RUNNING_VM) + (by rw [hsts_ok]; exact (WP.spec_ok _).2 rfl) ?_ + rintro _ rfl + set s10 := L.wr (L.wr (L.wr (L.wr (L.wr s4 (wa vc 24#u64) ct) (wa vc 32#u64) a0) (wa vc 40#u64) a2) (runVcpuA ct) vc) (stA ct) sched.TS_RUNNING_VM + set v10 := { v4 with vcpuExt := { v4.vcpuExt with runCaller := some (specPtr ct), runOffset := a0.bv, runOutMask := a2.bv } } + set x10 : Tcb := { xct with tcbState := .ext .runningVM, tcbExt := { xct.tcbExt with tcbRunVcpu := some (specPtr vc) } } + set σ10 := updKSc (updKSc σ4 (specPtr vc) (.archObj (.vcpu v10))) (specPtr ct) (.tcb x10) + have hksb10 : L.ksb s10 = L.ksb s := by simp [s10, L.ksb_wr, hksb4] + have hcpu10 : L.cpu s10 = L.cpu s := by simp [s10, L.cpu_wr, hcpu4] + have hfa10 : ∀ q, FootA (L := L) Z s10 q → Aligned q := fun q hq => C.hfa q (footA_of_ksb_cpuA hksb10 hcpu10 hq) + have hUb10 : ∀ q ∈ Ub, inUserFrame (specPtr q) σ10 := fun q hq => + inUserFrame_updKScA (by rw [updKSc_other _ hne_ct_vc]; exact hkt4) (fun _ e => by cases e) + (inUserFrame_updKScA hkv4 (fun _ e => by cases e) (huf4 _ (hUbUser q hq))) + have hBuf10 : ∀ p sz r d mp asid va, + capAtA (L.mem s10) (sbWordA ct) ≠ .Frame p sz r d mp true asid va := by + have hcap10 : capAtA (L.mem s10) (sbWordA ct) = capAtA (L.mem s) (sbWordA ct) := by + unfold capAtA; rw [(hos10 _ hsb_os).1, (hos10 _ hsb_os).2.1, (hos4 _ hsb_os).1, (hos4 _ hsb_os).2.1] + rw [hcap10]; exact hBufNotS2 + have hVm10 : L.mem s10 (stA ct) = sched.TS_RUNNING_VM → + L.mem s10 (scA ct) ≠ 0#u64 ∧ L.rmax s10 (L.mem s10 (scA ct)) ≠ 0#u64 ∧ + (L.mem s10 (inqA ct) &&& sched.Q_RELEASE) = 0#u64 ∧ + specPtr ct ∉ σ10.releaseQueue ∧ + Aligned (maxA (L.mem s10 (scA ct))) ∧ stA ct ≠ maxA (L.mem s10 (scA ct)) := fun _ => by + rw [(hmeta10 ct hcurT).2, (hmeta10 ct hcurT).1, hsc4, hiq4] + refine ⟨hCurSched.1, ?_, hCurSched.2.2.1, by change specPtr ct ∉ σ4.releaseQueue; rw [hrel4]; exact hCurSched.2.2.2, + (hSc_st _ hscC).2.2.2, (hSc_st _ hscC).2.2.1⟩ + show L.mem s10 (maxA (L.mem s (scA ct))) ≠ 0#u64 + rw [hmax10 _ hscC, hmax4 _ hscC]; exact hCurSched.2.1 + -- Notification preservation from `(s, σ)` to `(s10, σ10)` for `vcpu_check_refinesA` + have hNtfn10 : ∀ xct', σ10.kheap (specPtr ct) = some (.tcb xct') → + L.mem s10 (stA ct) = sched.TS_RUNNING_VM → L.mem s10 (runVcpuA ct) ≠ 0#u64 → + FinishNotificationA (st := st) s10 σ10 ct xct' ∧ + (∀ n, L.mem s10 (boundA ct) = n → n ≠ 0#u64 → NtfnStateWordDisjA Z OS Ub (L.ksb s10) n) := by + intro xct' hkx' _ _ + rw [updKSc_same] at hkx'; cases hkx' + obtain ⟨hFN0, hND0⟩ := hNtfn xct hkct + have hta := hU.tcb_al ct hcurT + have hal_t : Aligned ct := by unfold Aligned; have := hta.align; omega + have hbd_eq : boundA ct = wa ct 3368#u64 := by unfold boundA; rfl + have hbd10 : L.mem s10 (boundA ct) = L.mem s (boundA ct) := by + rw [hbd_eq] + have hal_b : Aligned (wa ct 3368#u64) := aligned_wa hal_t 3368#u64 (by decide) + have h1 : L.mem s10 (wa ct 3368#u64) = L.mem s4 (wa ct 3368#u64) := by + simp only [s10] + rw [mem_wr_neA (by rw [stA_eq]; exact aligned_wa hal_t 3328#u64 (by decide)) hal_b + (by rw [stA_eq]; exact field_ne hta hta (by decide) (by decide) (fun _ => by decide)), + mem_wr_neA (by rw [runVcpuA_eq]; exact aligned_wa hal_t 3672#u64 (by decide)) hal_b + (by rw [runVcpuA_eq]; exact field_ne hta hta (by decide) (by decide) (fun _ => by decide)), + mem_wr_neA (aligned_wa (C.hV.place.vc_al C.hvc) 40#u64 (by decide)) hal_b + (C.hV.place.disj_tcb vc C.hvc ct hcurT 40#u64 3368#u64 (by decide) (by decide)), + mem_wr_neA (aligned_wa (C.hV.place.vc_al C.hvc) 32#u64 (by decide)) hal_b + (C.hV.place.disj_tcb vc C.hvc ct hcurT 32#u64 3368#u64 (by decide) (by decide)), + mem_wr_neA (aligned_wa (C.hV.place.vc_al C.hvc) 24#u64 (by decide)) hal_b + (C.hV.place.disj_tcb vc C.hvc ct hcurT 24#u64 3368#u64 (by decide) (by decide))] + rw [h1, hct_eq4 3368#u64 (by decide) (by decide)] + refine ⟨?_, fun n hn hnz => by rw [hksb10]; exact (hND0 n (hbd10 ▸ hn) hnz).1⟩ + refine ⟨by rw [hbd10]; exact hFN0.1, fun n hn hnz => ?_⟩ + rw [hbd10] at hn + obtain ⟨ntfn, hkn0, hobj0⟩ := hFN0.2 n hn hnz + obtain ⟨hnd_n, hal_msg, hne_vreg, hne_vtcb, hne_vpage, hne_tcb⟩ := hND0 n hn hnz + have hne_n_ct : specPtr n ≠ specPtr ct := fun e => by rw [e, hkct] at hkn0; cases hkn0 + have hne_n_vc : specPtr n ≠ specPtr vc := fun e => by rw [e, hkv0] at hkn0; cases hkn0 + have hne_n_vt : specPtr n ≠ specPtr v_tcb := fun e => by + obtain ⟨xvt, hkt_vt, -, -⟩ := C.hV.core.tcb v_tcb hvtT; rw [e, hkt_vt] at hkn0; cases hkn0 + have hkn10 : σ10.kheap (specPtr n) = some (.notification ntfn) := by + rw [updKSc_other _ hne_n_ct, updKSc_other _ hne_n_vc, hkh4_other _ hne_n_vc hne_n_vt]; exact hkn0 + have hep_eq : L.mem s10 (epTSA n) = L.mem s (epTSA n) := by + have h1 : L.mem s10 (epTSA n) = L.mem s4 (epTSA n) := by + simp only [s10] + rw [mem_wr_neA (by rw [stA_eq]; exact aligned_wa hal_t 3328#u64 (by decide)) hnd_n.al (by rw [stA_eq]; exact (hne_tcb ct hcurT 3328#u64 (by decide)).1), + mem_wr_neA (by rw [runVcpuA_eq]; exact aligned_wa hal_t 3672#u64 (by decide)) hnd_n.al (by rw [runVcpuA_eq]; exact (hne_tcb ct hcurT 3672#u64 (by decide)).1), + mem_wr_neA (aligned_wa (C.hV.place.vc_al C.hvc) 40#u64 (by decide)) hnd_n.al (hne_vpage _ ⟨vc, C.hvc, 40#u64, by decide, by decide, rfl⟩).1, + mem_wr_neA (aligned_wa (C.hV.place.vc_al C.hvc) 32#u64 (by decide)) hnd_n.al (hne_vpage _ ⟨vc, C.hvc, 32#u64, by decide, by decide, rfl⟩).1, + mem_wr_neA (aligned_wa (C.hV.place.vc_al C.hvc) 24#u64 (by decide)) hnd_n.al (hne_vpage _ ⟨vc, C.hvc, 24#u64, by decide, by decide, rfl⟩).1] + rw [h1, hw4.mem_eq hnd_n.al (fun h => by + rcases h with heq | ⟨i, hi, heq⟩ | ⟨K, hK1, hK2, heq⟩ + · exact (hne_vpage _ ⟨vc, C.hvc, 16#u64, by decide, by decide, rfl⟩).1 heq.symm + · exact (hne_vreg _ ⟨i, hi, rfl⟩).1 heq.symm + · exact (hne_vtcb v_tcb hvtT K hK1 hK2).1 heq.symm)] + exact mem_wr_neA (aligned_wa (C.hV.place.vc_al C.hvc) 8#u64 (by decide)) hnd_n.al + (hne_vpage _ ⟨vc, C.hvc, 8#u64, by decide, by decide, rfl⟩).1 + have hmsg_eq : L.mem s10 (ntfnBdA n) = L.mem s (ntfnBdA n) := by + have h1 : L.mem s10 (ntfnBdA n) = L.mem s4 (ntfnBdA n) := by + simp only [s10] + rw [mem_wr_neA (by rw [stA_eq]; exact aligned_wa hal_t 3328#u64 (by decide)) hal_msg (by rw [stA_eq]; exact (hne_tcb ct hcurT 3328#u64 (by decide)).2), + mem_wr_neA (by rw [runVcpuA_eq]; exact aligned_wa hal_t 3672#u64 (by decide)) hal_msg (by rw [runVcpuA_eq]; exact (hne_tcb ct hcurT 3672#u64 (by decide)).2), + mem_wr_neA (aligned_wa (C.hV.place.vc_al C.hvc) 40#u64 (by decide)) hal_msg (hne_vpage _ ⟨vc, C.hvc, 40#u64, by decide, by decide, rfl⟩).2, + mem_wr_neA (aligned_wa (C.hV.place.vc_al C.hvc) 32#u64 (by decide)) hal_msg (hne_vpage _ ⟨vc, C.hvc, 32#u64, by decide, by decide, rfl⟩).2, + mem_wr_neA (aligned_wa (C.hV.place.vc_al C.hvc) 24#u64 (by decide)) hal_msg (hne_vpage _ ⟨vc, C.hvc, 24#u64, by decide, by decide, rfl⟩).2] + rw [h1, hw4.mem_eq hal_msg (fun h => by + rcases h with heq | ⟨i, hi, heq⟩ | ⟨K, hK1, hK2, heq⟩ + · exact (hne_vpage _ ⟨vc, C.hvc, 16#u64, by decide, by decide, rfl⟩).2 heq.symm + · exact (hne_vreg _ ⟨i, hi, rfl⟩).2 heq.symm + · exact (hne_vtcb v_tcb hvtT K hK1 hK2).2 heq.symm)] + exact mem_wr_neA (aligned_wa (C.hV.place.vc_al C.hvc) 8#u64 (by decide)) hal_msg + (hne_vpage _ ⟨vc, C.hvc, 8#u64, by decide, by decide, rfl⟩).2 + refine ⟨ntfn, hkn10, ?_⟩ + cases hno : ntfn.ntfnObj <;> rw [hno] at hobj0 <;> simp only [LawfulStore.epState, hep_eq, hmsg_eq] at hobj0 ⊢ <;> exact hobj0 + -- Step: `vcpu.check st ar s10 a4 ct` + refine WP.spec_bind (vcpu_check_refinesA X10 hP hAK hVR hU (by rw [hksb10]; exact hLD) + hUb10 hCs128 (hcpu10 ▸ hcpu128) C.hExt C.hSw hfa10 C.hsub (fun c hc => X10.j.1.idleT c hc) + (by rw [hksb10]; exact hksTcb) hSlotTcb hOS_tcb hcurT hBuf10 hVm10 + (by change specPtr ct ∉ σ4.releaseQueue; rw [hrel4]; exact hCurSched.2.2.2) hSlotSt hOS_st (by rw [hksb10]; exact hKs_st) + hSc_st hSc_reg (by rw [hksb10]; exact hVs_st) hExt_st hNtfn10) ?_ + rintro ⟨s11, a5⟩ ⟨hw11, σ11, hsp11, X11, -, -, -⟩ + refine (WP.spec_ok (core.result.Result.Ok (), s11, a5)).2 ?_ + -- Show `fl4PerformVcpuInv (.run ...) σ_r = ({([], σ11)}, False)`! + have hperf_r : fl4PerformVcpuInv (Z := Unit) (.run (specPtr vc) (specPtr ct) (specPtr frame) a0.bv a1.bv a2.bv vgic_s) σ_r = + ({([], σ11)}, False) := by + unfold fl4PerformVcpuInv; dsimp only + have hfl : fl4VcpuFlush (Z := Unit) (specPtr vc) σ_r = ({((), σ_r)}, False) := by + unfold fl4VcpuFlush NonDet.when + rw [nd_bind_oneSc (m := NonDet.gets _) rfl] + have hcur : σ_r.archState.armCurrentVcpu = none := C.hV.arch + rw [hcur]; rfl + rw [nd_bind_oneSc hfl] + have hσ_r_eq : σ_r = updKSc σ (specPtr ct) (.tcb { xct with tcbState := .restart }) := rfl + rw [hσ_r_eq, nd_bind_oneSc hsp4_r] + -- Now `fl4VcpuExtUpdate` + `setRunVcpu` + `setThreadState ct (.ext .runningVM)` from `updKSc σ4 ct (.tcb { xct with tcbState := .restart })` + -- reaches the EXACT same `σ10` as from `σ4`! + have hkv4_r : (updKSc σ4 (specPtr ct) (.tcb { xct with tcbState := .restart })).kheap (specPtr vc) = + some (.archObj (.vcpu v4)) := by rw [updKSc_other _ hne_ct_vc.symm]; exact hkv4 + have hkt4_r : (updKSc (updKSc σ4 (specPtr ct) (.tcb { xct with tcbState := .restart })) (specPtr vc) (.archObj (.vcpu v10))).kheap (specPtr ct) = + some (.tcb { xct with tcbState := .restart }) := by + rw [updKSc_other _ hne_ct_vc, updKSc_same] + have hcomm_v : updKSc (updKSc (updKSc σ4 (specPtr ct) (.tcb { xct with tcbState := .restart })) + (specPtr vc) (.archObj (.vcpu v10))) (specPtr ct) + (.tcb { xct with tcbState := .restart, tcbExt := { xct.tcbExt with tcbRunVcpu := some (specPtr vc) } }) = + updKSc (updKSc σ4 (specPtr vc) (.archObj (.vcpu v10))) (specPtr ct) + (.tcb { xct with tcbState := .restart, tcbExt := { xct.tcbExt with tcbRunVcpu := some (specPtr vc) } }) := by + rw [updKSc_commA σ4 hne_ct_vc, updKSc_idemA] + rw [nd_bind_oneSc (fl4VcpuExtUpdate_eqA _ hkv4_r), + nd_bind_oneSc (setRunVcpu_eqA _ hkt4_r), hcomm_v] + -- `setThreadState ct (.ext .runningVM)` overwrites `.restart` to `.ext .runningVM`, giving `σ10`! + have hsts10_r : setThreadState (Z := Unit) (specPtr ct) (.ext .runningVM) + (updKSc (updKSc σ4 (specPtr vc) (.archObj (.vcpu v10))) (specPtr ct) + (.tcb { xct with tcbState := .restart, tcbExt := { xct.tcbExt with tcbRunVcpu := some (specPtr vc) } })) = + ({((), σ10)}, False) := by + have hst_eq' : setObjA (updKSc (updKSc σ4 (specPtr vc) (.archObj (.vcpu v10))) (specPtr ct) + (.tcb { xct with tcbState := .restart, tcbExt := { xct.tcbExt with tcbRunVcpu := some (specPtr vc) } })) + (specPtr ct) (.tcb { { xct with tcbState := .restart, tcbExt := { xct.tcbExt with tcbRunVcpu := some (specPtr vc) } } with tcbState := .ext .runningVM }) = σ10 := by + unfold setObjA; exact updKSc_idemA _ _ _ _ + rw [← hst_eq'] + apply setThreadState_eqA _ (specPtr ct) _ (updKSc_same _ _ _) (.ext .runningVM) + intro _ _ + -- Reuse `isSchedulable σ10` from `hsp_enter`! + have hsc_some : xct.tcbSchedContext = some (scRenA m (L.mem s (scA ct))) := by + obtain ⟨x0', hk0', -, hsc0⟩ := C.E.j.1.sch.tcb ct hcurT + rw [hkct] at hk0'; cases hk0'; rw [hsc0]; unfold optScA; rw [if_neg hCurSched.1] + obtain ⟨y, n, hy, hrm_eq⟩ := C.E.j.1.rest.refillMax _ hscC + have hne_sc_vc : scRenA m (L.mem s (scA ct)) ≠ specPtr vc := fun e => by rw [e, hkv0] at hy; cases hy + have hne_sc_ct : scRenA m (L.mem s (scA ct)) ≠ specPtr ct := fun e => by rw [e, hkct] at hy; cases hy + have hne_sc_vt : scRenA m (L.mem s (scA ct)) ≠ specPtr v_tcb := fun e => by + obtain ⟨xvt, hkt_vt, -, -⟩ := C.hV.core.tcb v_tcb hvtT; rw [e, hkt_vt] at hy; cases hy + have hy10 : σ10.kheap (scRenA m (L.mem s (scA ct))) = some (.schedContext y n) := by + rw [updKSc_other _ hne_sc_ct, updKSc_other _ hne_sc_vc, hkh4_other _ hne_sc_vc hne_sc_vt]; exact hy + have hpos : 0 < y.scRefillMax := by + rw [hrm_eq]; by_contra h0; apply hCurSched.2.1; apply UScalar.eq_of_val_eq + unfold LawfulStore.rmax; rw [maxA_eqA]; exact Nat.eq_zero_of_not_pos h0 + rw [hst_eq'] + exact schedulable_ofA (updKSc_same _ _ _) rfl hsc_some hy10 hpos + (by change specPtr ct ∉ σ4.releaseQueue; rw [hrel4]; exact hCurSched.2.2.2) + rw [nd_bind_oneSc hsts10_r, nd_bind_oneSc hsp11]; rfl + have hperf_mem : (.ok ([] : List Data), σ11) ∈ + ((fl4ArchPerformInvocation (Z := Unit) (.vcpu (.run (specPtr vc) (specPtr ct) (specPtr frame) a0.bv a1.bv a2.bv vgic_s))).run σ_r).1 := by + unfold fl4ArchPerformInvocation NonDetE.liftE + change (Except.ok [], σ11) ∈ ((fl4PerformVcpuInv _ >>= _) _).1 + rw [nd_bind_oneSc hperf_r]; exact Set.mem_singleton _ + exact ⟨.vcpu (.run (specPtr vc) (specPtr ct) (specPtr frame) a0.bv a1.bv a2.bv vgic_s), + liftME_vcpu_okA hd_ok, σ_r, hst_restart, σ11, [], hperf_mem, + Or.inl ⟨{ Z with hw := hw11 }, X11.j, X11.tr, fun _ => X11.car⟩⟩ + +end decode_run + +end +end Fl4ProofsA64.P3.Vcpu diff --git a/proofs/Fl4ProofsA64/P3/VcpuRunJA.lean b/proofs/Fl4ProofsA64/P3/VcpuRunJA.lean new file mode 100644 index 000000000..f8fe51a73 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/VcpuRunJA.lean @@ -0,0 +1,769 @@ +import Fl4ProofsA64.P3.VcpuRunBodyA + +/-! # AArch64 P3 VCPU Run (`vcpu_run_jA`) -/ + +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3.Vcpu + +open Fl4Spec Fl4SpecA64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3 Fl4ProofsA64.P3.Sched +open Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +private theorem wa_zeroA (x : U64) : wa x 0#u64 = x := by + apply UScalar.eq_of_val_eq; rw [wa_valP]; have := x.bv.isLt; simp [Nat.mod_eq_of_lt this] + +private theorem frameSize_bits_eqA (sz : capability.FrameSize) : + capability.FrameSize.bits sz = ok ⟨BitVec.ofNat 64 (frameBitsA sz)⟩ ∧ + frameBitsA sz = pageBitsForSize (frameSizeOfA sz) ∧ + frameBitsA sz < 64 := by + cases sz <;> + exact ⟨by unfold capability.FrameSize.bits frameBitsA; rfl, + by unfold frameBitsA frameSizeOfA pageBitsForSize; rfl, + by unfold frameBitsA; decide⟩ + +section decode_run +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +set_option maxHeartbeats 800000 in +/-- **Label 69 (`ARMVCPURun`) refinement on `VcpuTopPostA`**: +`vcpu.decode_run` refines `decodeVcpuRun` + `fl4PerformVcpuInv (.run ...)` (including +`write_state_from_frame`, `RUN_CALLER`/`RUN_OFFSET`/`RUN_OUT_MASK`/`RUN_VCPU`, +`vspace.set_stage2_root`, `TS_RUNNING_VM`, and `vcpu.check`). -/ +theorem vcpu_run_jA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} + {OS U Ac Pp Tb Ub : Set U64} {Lv : U64 → Nat} {ia ie : Nat} + {s : S} {a : A} {σ : State Unit} {m : Fl4SpecA64.McState} {vc : U64} + (C : VcpuDecCtxA (L := L) (ar := ar) AR Z H OS U Ac Pp Tb Ub Lv ia ie s a σ m vc) + (hCar : CarryA Z (L.mem s) σ) + (hP : VcpuPlatAgreeA ar) (hAK : VcpuArchKeepsA ar AR) + (hVR : VcpuVmRootA st ar AR) (hS2R : VcpuStage2RootA st ar AR) (hAR : AR a Z.hw) + (hF : FrameMemRelA Z OS Ub (L.ksb s) (L.mem s) σ.machineState.underlyingMemory) + (hR : TcbRegsRelA (L.mem s) σ Z.T) + (hU : UbFootDisjA Z OS Ub) (hLD : TcbLinkDisjA Z (L.ksb s)) + (hUbUser : ∀ q ∈ Ub, inUserFrame (specPtr q) σ) + (hksTcb : ∀ t ∈ Z.T, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → + ∀ o : U64, o.val < ksSizeA → wa t K ≠ wa (L.ksb s) o) + (hSlotTcb : ∀ t ∈ Z.T, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → + ∀ y, SlotWA Z.th y → wa t K ≠ y ∧ wa t K ≠ wa y 8#u64 ∧ wa t K ≠ wa y 16#u64 ∧ wa t K ≠ wa y 24#u64) + (hOS_tcb : ∀ t ∈ Z.T, ∀ K : U64, (K = 3664#u64 ∨ K = 3672#u64) → ∀ y ∈ OS, + wa t K ≠ y ∧ wa t K ≠ wa y 8#u64 ∧ wa t K ≠ wa y 16#u64 ∧ wa t K ≠ wa y 24#u64) + (hStall : ∀ t ∈ Z.T, ∀ s' a', smp.remote_tcb_stall st ar s' a' t = ok (s', a')) + (hBufNotS2 : ∀ p sz r d mp asid va, + capAtA (L.mem s) (sbWordA (L.curW s)) ≠ .Frame p sz r d mp true asid va) + (hCurNoRun : L.mem s (runVcpuA (L.curW s)) = 0#u64) + (hCurNotIdle : ∀ c ∈ Z.Cs, L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) ≠ L.curW s) + (hCurSched : L.mem s (scA (L.curW s)) ≠ 0#u64 ∧ L.rmax s (L.mem s (scA (L.curW s))) ≠ 0#u64 ∧ + (L.mem s (inqA (L.curW s)) &&& sched.Q_RELEASE) = 0#u64 ∧ + specPtr (L.curW s) ∉ σ.releaseQueue) + (hSlotSt : ∀ y, SlotWA Z.th y → stA (L.curW s) ≠ y ∧ stA (L.curW s) ≠ wa y 8#u64 ∧ + stA (L.curW s) ≠ wa y 16#u64 ∧ stA (L.curW s) ≠ wa y 24#u64) + (hOS_st : ∀ y ∈ OS, stA (L.curW s) ≠ y ∧ stA (L.curW s) ≠ wa y 8#u64 ∧ + stA (L.curW s) ≠ wa y 16#u64 ∧ stA (L.curW s) ≠ wa y 24#u64) + (hKs_st : ∀ o : U64, o.val < ksSizeA → stA (L.curW s) ≠ wa (L.ksb s) o) + (hSc_st : ∀ c ∈ Z.C, stA (L.curW s) ≠ wa c 24#u64 ∧ stA (L.curW s) ≠ wa c 72#u64 ∧ + stA (L.curW s) ≠ maxA c ∧ Aligned (maxA c)) + (hSc_reg : ∀ c ∈ Z.C, Endp.regA (L.curW s) layout.reg.FAULT_IP ≠ maxA c) + (hVs_st : ¬ VsFootA Z.Pp Z.S1 Z.Y (L.ksb s) (stA (L.curW s))) + (hExt_st : ¬ ExtFootE Z.X Z.Y Z.T2 (stA (L.curW s))) + (hNtfn : ∀ xct, σ.kheap (specPtr (L.curW s)) = some (.tcb xct) → + FinishNotificationA (st := st) s σ (L.curW s) xct ∧ + (∀ n, L.mem s (boundA (L.curW s)) = n → n ≠ 0#u64 → + NtfnStateWordDisjA Z OS Ub (L.ksb s) n ∧ Aligned (ntfnBdA n) ∧ + (∀ b, (∃ i, i < 64 ∧ b = vcpuRegWA vc i) → b ≠ epTSA n ∧ b ≠ ntfnBdA n) ∧ + (∀ v_tcb ∈ Z.T, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → wa v_tcb K ≠ epTSA n ∧ wa v_tcb K ≠ ntfnBdA n) ∧ + (∀ b, VcPage Z.V b → b ≠ epTSA n ∧ b ≠ ntfnBdA n) ∧ + (∀ t ∈ Z.T, ∀ K : U64, K.val < 4096 → wa t K ≠ epTSA n ∧ wa t K ≠ ntfnBdA n))) + {Rp Nt : Set U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} {slot : U64} + (Pl : SysPlace st H Z.Cs H.frame Rp Nt OS s extra slot) (Y : SymL st H Z.Cs s) + (hrun : Runnable (L.mem s (stA (L.curW s)))) + (hcur : (L.mem s (inqA (L.curW s)) &&& sched.Q_RELEASE) = 0#u64 ∧ L.mem s (scA (L.curW s)) ≠ 0#u64 ∧ + L.mem s (scA (L.curW s)) ∈ Ac) + (hvs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → Aligned b ∧ b ≠ stA (L.curW s)) + {length buf : U64} {args : List U64} + (hlen : length.val = args.length) + (hargs : ∀ (i : U64) (w : U64), args[i.val]? = some w → msg.syscall_arg st s (L.curW s) buf i = ok w) + (call : Bool) : + vcpu.decode_run st ar s a vc length call buf ⦃ res => + VcpuTopPostA (L := L) AR Z OS m call σ + ((NonDetE.liftME Fl4ArchInvocation.vcpu <| + decodeVcpuRun (Z := Unit) (specPtr vc) (args.map UScalar.bv) call).run σ) s res ⦄ := by + unfold vcpu.decode_run + cases call with + | false => + rw [ite_eq_right (by decide)]; unfold error.illegal; rw [bind_ok, WP.spec_ok] + have hd : (decodeVcpuRun (Z := Unit) (specPtr vc) (args.map UScalar.bv) false).run σ = + ({(.error .illegalOperation, σ)}, False) := by + unfold decodeVcpuRun NonDetE.unlessE; rw [ite_eq_right (by decide)]; exact ndE_throw_bind _ _ _ + exact ⟨rfl, hAR, .illegalOperation, lift_vcpu_run_errA hd, Or.inl trivial⟩ + | true => + rw [ite_eq_left rfl] + by_cases hlt3 : length < 3#u64 + · rw [ite_eq_left hlt3]; unfold error.truncated; rw [bind_ok, WP.spec_ok] + have hlen3 : (args.map UScalar.bv).length < 3 := by rw [List.length_map, ← hlen]; exact hlt3 + have hd : (decodeVcpuRun (Z := Unit) (specPtr vc) (args.map UScalar.bv) true).run σ = + ({(.error .truncatedMessage, σ)}, False) := by + unfold decodeVcpuRun NonDetE.unlessE; rw [ite_eq_left (by decide), ndE_pure_bindA] + exact ndE_whenE_true hlen3 _ _ _ + exact ⟨rfl, hAR, .truncatedMessage, lift_vcpu_run_errA hd, Or.inl trivial⟩ + · rw [ite_eq_right hlt3] + have hge3 : 3 ≤ args.length := by change ¬ length.val < 3 at hlt3; omega + obtain ⟨a0, a1, a2, rest, hargs_eq⟩ : ∃ a0 a1 a2 rest, args = a0 :: a1 :: a2 :: rest := by + match args, hge3 with + | a0 :: a1 :: a2 :: rest, _ => exact ⟨a0, a1, a2, rest, rfl⟩ + have hct_ok : sched.cur_thread st s = ok (L.curW s) := eq_of_spec_fd (sched.cur_thread.spec s) + have ha0 := hargs 0#u64 a0 (by simp [hargs_eq]) + have ha1 := hargs 1#u64 a1 (by simp [hargs_eq]) + have ha2 := hargs 2#u64 a2 (by simp [hargs_eq]) + have hsm_all : fl4_abi.consts.vcpu_state_mask.ALL = ok 63#u64 := by + simp only [fl4_abi.consts.vcpu_state_mask.ALL, fl4_abi.consts.vcpu_state_mask.EL1_SYSREGS, + fl4_abi.consts.vcpu_state_mask.HYP, fl4_abi.consts.vcpu_state_mask.TIMER, + fl4_abi.consts.vcpu_state_mask.VGIC, fl4_abi.consts.vcpu_state_mask.GPRS, + fl4_abi.consts.vcpu_state_mask.VGIC_LR, + u64_shl_i32_ok (x := 1#u64) (y := 0#i32) (by decide) (by decide), + u64_shl_i32_ok (x := 1#u64) (y := 1#i32) (by decide) (by decide), + u64_shl_i32_ok (x := 1#u64) (y := 2#i32) (by decide) (by decide), + u64_shl_i32_ok (x := 1#u64) (y := 3#i32) (by decide) (by decide), + u64_shl_i32_ok (x := 1#u64) (y := 4#i32) (by decide) (by decide), + u64_shl_i32_ok (x := 1#u64) (y := 5#i32) (by decide) (by decide), + bind_ok, lift_ok] + rfl + have hsmAll_bv : smAll = (63#u64).bv := rfl + rw [hct_ok, bind_ok, ha0, bind_ok, ha1, bind_ok, ha2, bind_ok, hsm_all, bind_ok, lift_ok, bind_ok, lift_ok, bind_ok] + set ct := L.curW s + have hcurT : ct ∈ Z.T := C.E.j.1.curT (L.cpu s) C.E.j.1.sch.cpuCs + have hct_spec : σ.curThread = specPtr ct := C.E.j.1.sch.curThread + have hd_prefix : + (decodeVcpuRun (Z := Unit) (specPtr vc) (args.map UScalar.bv) true).run σ = + ((do NonDetE.whenE (a1.bv &&& ~~~smAll ≠ 0) <| NonDetE.throwError (.invalidArgument 1) + NonDetE.whenE (a2.bv &&& ~~~smAll ≠ 0) <| NonDetE.throwError (.invalidArgument 2) + let buf ← NonDetE.liftE <| getCap (specPtr ct, tcbCnodeIndex 2) + match frameRightsDev buf, stateWindow buf a0.bv with + | some (R, dev), some w => do + NonDetE.whenE (dev ∨ R ≠ vmReadWrite) <| NonDetE.throwError .illegalOperation + match w with + | none => NonDetE.throwError (.invalidArgument 0) + | some frame => do + let v ← NonDetE.liftE <| getVcpu (specPtr vc) + let ctVcpu ← NonDetE.liftE <| archThreadGet ArchTcb.tcbVcpu (specPtr ct) + NonDetE.whenE (v.vcpuTcb = none ∨ v.vcpuTcb = some (specPtr ct) ∨ ctVcpu ≠ none) <| + NonDetE.throwError .illegalOperation + NonDetE.whenE (v.vcpuExt.runCaller ≠ none) <| NonDetE.throwError .deleteFirst + if a1.bv &&& (smVgic ||| smVgicLr) ≠ 0 then do + let vg ← NonDetE.liftE <| readVgicFrame frame + match vg with + | some vg => NonDetE.returnOk (.run (specPtr vc) (specPtr ct) frame a0.bv a1.bv a2.bv vg) + | none => NonDetE.throwError (.invalidArgument 1) + else NonDetE.returnOk (.run (specPtr vc) (specPtr ct) frame a0.bv a1.bv a2.bv (0, [])) + | _, _ => NonDetE.throwError .illegalOperation : SEMonad Unit Fl4VcpuInvocation)).run σ := by + unfold decodeVcpuRun + rw [NonDetE.unlessE, ite_eq_left (by decide), ndE_pure_bindA, + NonDetE.whenE, ite_eq_right (by rw [List.length_map, ← hlen]; exact hlt3), ndE_pure_bindA] + unfold NonDetE.liftE + change ((NonDet.gets (fun s : State Unit => s.curThread) >>= fun t => NonDet.pure (Except.ok t)) >>= _) σ = _ + rw [nd_bind_assocSc, nd_bind_oneSc (m := NonDet.gets _) rfl, nd_bind_oneSc (m := NonDet.pure _) rfl] + dsimp only [ExceptT.bindCont] + rw [hct_spec, hargs_eq] + simp only [List.map_cons, List.getElem?_cons_zero, List.getElem?_cons_succ, the]; rfl + by_cases hmask1 : (a1 &&& ~~~63#u64 != 0#u64) = true + · rw [ite_eq_left hmask1]; unfold error.invalid_arg; rw [bind_ok, WP.spec_ok] + have hm1 : a1.bv &&& ~~~smAll ≠ 0 := fun e => by + rw [hsmAll_bv] at e + have h0 : a1 &&& ~~~63#u64 = 0#u64 := u64_eq_of_bv_eq e + rw [h0] at hmask1; revert hmask1; decide + have hd : (decodeVcpuRun (Z := Unit) (specPtr vc) (args.map UScalar.bv) true).run σ = + ({(.error (.invalidArgument 1), σ)}, False) := by + rw [hd_prefix] + exact ndE_whenE_true hm1 _ _ _ + exact ⟨rfl, hAR, .invalidArgument 1, lift_vcpu_run_errA hd, Or.inl rfl⟩ + · rw [ite_eq_right hmask1, lift_ok, bind_ok, lift_ok, bind_ok] + have hm1_0 : ¬ (a1.bv &&& ~~~smAll ≠ 0) := fun h => hmask1 (bne_iff_ne.2 (fun e => h (by rw [hsmAll_bv]; exact congrArg UScalar.bv e))) + by_cases hmask2 : (a2 &&& ~~~63#u64 != 0#u64) = true + · rw [ite_eq_left hmask2]; unfold error.invalid_arg; rw [bind_ok, WP.spec_ok] + have hm2 : a2.bv &&& ~~~smAll ≠ 0 := fun e => by + rw [hsmAll_bv] at e + have h0 : a2 &&& ~~~63#u64 = 0#u64 := u64_eq_of_bv_eq e + rw [h0] at hmask2; revert hmask2; decide + have hd : (decodeVcpuRun (Z := Unit) (specPtr vc) (args.map UScalar.bv) true).run σ = + ({(.error (.invalidArgument 2), σ)}, False) := by + rw [hd_prefix, NonDetE.whenE, ite_eq_right hm1_0, ndE_pure_bindA] + exact ndE_whenE_true hm2 _ _ _ + exact ⟨rfl, hAR, .invalidArgument 2, lift_vcpu_run_errA hd, Or.inl rfl⟩ + · rw [ite_eq_right hmask2] + have hm2_0 : ¬ (a2.bv &&& ~~~smAll ≠ 0) := fun h => hmask2 (bne_iff_ne.2 (fun e => h (by rw [hsmAll_bv]; exact congrArg UScalar.bv e))) + have hslot_ok : obj.Tcb.slot ct layout.tcb.SLOT_BUFFER = ok (sbWordA ct) := by + simp [obj.Tcb.slot, lift, sbWordA, layout.tcb.SLOT_BUFFER, layout.cte.SIZE] + have hcap_ok : obj.Slot.cap st (sbWordA ct) s = ok (capAtA (L.mem s) (sbWordA ct)) := by + rw [Fl4ProofsA64.P1.W7.R.ST.capA_eq, ← capAtA_eq] + rw [hslot_ok, bind_ok, hcap_ok, bind_ok] + -- Relate `capAtA (L.mem s) (sbWordA ct)` to `xct.tcbIpcframe` + obtain ⟨xct, hkct, hslots_ct⟩ := C.E.j.1.st.tcb ct ((C.E.j.1.thT ct).2 hcurT) + have hsr2 := hslots_ct 2 (by unfold tcbSlotsA; decide) + have hsb_eq : slotAddrA (specPtr ct) (BitVec.ofNat 64 2) = sbWordA ct := + bufSlot_eqA ct + have htcb_buf_eq : tcbCnodeMap xct (tcbCnodeIndex 2) = some xct.tcbIpcframe := by + unfold tcbCnodeMap tcbCnodeIndex; rfl + rw [hsb_eq] at hsr2 + have hbuf_rel : CapRelA (capAtA (L.mem s) (sbWordA ct)) xct.tcbIpcframe := by + simpa [SlotRelA, htcb_buf_eq] using hsr2 + have hsb_os : sbWordA ct ∈ OS := + C.hsub (sbWordA ct) ⟨ct, Or.inr ⟨(C.E.j.1.thT ct).2 hcurT, 2, by unfold tcbSlotsA; decide, hsb_eq.symm⟩⟩ + have hd_buf : (decodeVcpuRun (Z := Unit) (specPtr vc) (args.map UScalar.bv) true).run σ = + ((match frameRightsDev xct.tcbIpcframe, stateWindow xct.tcbIpcframe a0.bv with + | some (R, dev), some w => do + NonDetE.whenE (dev ∨ R ≠ vmReadWrite) <| NonDetE.throwError .illegalOperation + match w with + | none => NonDetE.throwError (.invalidArgument 0) + | some frame => do + let v ← NonDetE.liftE <| getVcpu (specPtr vc) + let ctVcpu ← NonDetE.liftE <| archThreadGet ArchTcb.tcbVcpu (specPtr ct) + NonDetE.whenE (v.vcpuTcb = none ∨ v.vcpuTcb = some (specPtr ct) ∨ ctVcpu ≠ none) <| + NonDetE.throwError .illegalOperation + NonDetE.whenE (v.vcpuExt.runCaller ≠ none) <| NonDetE.throwError .deleteFirst + if a1.bv &&& (smVgic ||| smVgicLr) ≠ 0 then do + let vg ← NonDetE.liftE <| readVgicFrame frame + match vg with + | some vg => NonDetE.returnOk (.run (specPtr vc) (specPtr ct) frame a0.bv a1.bv a2.bv vg) + | none => NonDetE.throwError (.invalidArgument 1) + else NonDetE.returnOk (.run (specPtr vc) (specPtr ct) frame a0.bv a1.bv a2.bv (0, [])) + | _, _ => NonDetE.throwError .illegalOperation : SEMonad Unit Fl4VcpuInvocation)).run σ := by + rw [hd_prefix, NonDetE.whenE, ite_eq_right hm1_0, ndE_pure_bindA, + NonDetE.whenE, ite_eq_right hm2_0, ndE_pure_bindA] + unfold NonDetE.liftE + change ((getCap (specPtr ct, tcbCnodeIndex 2) >>= fun c => NonDet.pure (Except.ok c)) >>= _) σ = _ + rw [nd_bind_assocSc, nd_bind_oneSc (getCap_tcbIpcframe_runA hkct), nd_bind_oneSc (m := NonDet.pure _) rfl]; rfl + have hIllBuf : (∀ p r sz d mp, xct.tcbIpcframe ≠ .archObjectCap (.frameCap p r sz d mp)) → + (do let r ← error.illegal Unit; ok (r, s, a)) ⦃ res => + VcpuTopPostA (L := L) AR Z OS m true σ + ((NonDetE.liftME Fl4ArchInvocation.vcpu <| + decodeVcpuRun (Z := Unit) (specPtr vc) (args.map UScalar.bv) true).run σ) s res ⦄ := by + intro hnf; unfold error.illegal; rw [bind_ok, WP.spec_ok] + have hd : (decodeVcpuRun (Z := Unit) (specPtr vc) (args.map UScalar.bv) true).run σ = + ({(.error .illegalOperation, σ)}, False) := by + rw [hd_buf] + cases hfc : xct.tcbIpcframe with + | archObjectCap ac => + cases ac with + | frameCap p r sz d mp => exact absurd hfc (hnf p r sz d mp) + | _ => rfl + | _ => rfl + exact ⟨rfl, hAR, .illegalOperation, lift_vcpu_run_errA hd, Or.inl trivial⟩ + rcases hbuf_rel with ⟨hwf, habs⟩ + cases hcap_eq : capAtA (L.mem s) (sbWordA ct) with + | Frame ptr sz rights is_dev mp s2 asid va => + dsimp only + by_cases hs2 : s2 = true + · subst hs2; exact absurd hcap_eq (hBufNotS2 ptr sz rights is_dev mp asid va) + · have hs2f : s2 = false := Bool.eq_false_iff.2 hs2 + subst hs2f + rw [hcap_eq] at habs + have hbuf_spec : xct.tcbIpcframe = .archObjectCap (.frameCap (specPtr ptr) (vmRightsOfA rights) (frameSizeOfA sz) is_dev + (mapDataA mp asid va)) := by + rw [← habs]; rfl + by_cases hdev : is_dev = true + · subst hdev; rw [if_pos rfl]; unfold error.illegal; rw [bind_ok, WP.spec_ok] + have hd : (decodeVcpuRun (Z := Unit) (specPtr vc) (args.map UScalar.bv) true).run σ = + ({(.error .illegalOperation, σ)}, False) := by + rw [hd_buf, hbuf_spec]; unfold frameRightsDev stateWindow; dsimp only + split_ifs <;> exact ndE_whenE_true (Or.inl rfl) _ _ _ + exact ⟨rfl, hAR, .illegalOperation, lift_vcpu_run_errA hd, Or.inl trivial⟩ + · have hdevf : is_dev = false := Bool.eq_false_iff.2 hdev + subst hdevf; rw [if_neg (by decide)] + by_cases hrw : (rights != capability.VM_READ_WRITE) = true + · rw [if_pos hrw]; unfold error.illegal; rw [bind_ok, WP.spec_ok] + have hr_ne : vmRightsOfA rights ≠ vmReadWrite := by + have hne : rights ≠ capability.VM_READ_WRITE := bne_iff_ne.1 hrw + rw [hcap_eq] at hwf; unfold CapWfA at hwf + unfold capability.VM_READ_WRITE at hne + intro e + have h0 : rights.val = 3 := by + rcases hwf with ⟨h1, h2, -⟩ + have : rights.val = 1 ∨ rights.val = 2 ∨ rights.val = 3 := by omega + rcases this with h | h | h + · have haw := (Set.ext_iff.1 e .allowWrite).2 (by simp [vmReadWrite]); simp [vmRightsOfA, vmKernelOnly, h] at haw + · have haw := (Set.ext_iff.1 e .allowWrite).2 (by simp [vmReadWrite]); simp [vmRightsOfA, vmReadOnly, h] at haw + · exact h + exact hne (UScalar.eq_of_val_eq h0) + have hd : (decodeVcpuRun (Z := Unit) (specPtr vc) (args.map UScalar.bv) true).run σ = + ({(.error .illegalOperation, σ)}, False) := by + rw [hd_buf, hbuf_spec]; unfold frameRightsDev stateWindow; dsimp only + split_ifs <;> exact ndE_whenE_true (Or.inr hr_ne) _ _ _ + exact ⟨rfl, hAR, .illegalOperation, lift_vcpu_run_errA hd, Or.inl trivial⟩ + · rw [if_neg hrw] + have hr_eq : rights = capability.VM_READ_WRITE := by + by_contra hne; exact hrw (bne_iff_ne.2 hne) + have hr_sp : vmRightsOfA rights = vmReadWrite := by + rw [hr_eq]; unfold vmRightsOfA capability.VM_READ_WRITE vmReadWrite; rfl + obtain ⟨hbits_ok, hbits_sp, hbits_lt⟩ := frameSize_bits_eqA sz + rw [hbits_ok, bind_ok, lift_ok, bind_ok] + have hmask63 : (⟨BitVec.ofNat 64 (frameBitsA sz)⟩ : U64) &&& 63#u64 = + ⟨BitVec.ofNat 64 (frameBitsA sz)⟩ := by + apply u64_eq_of_bv_eq; cases sz <;> decide + rw [hmask63, u64_shl_u64_ok (by + change (BitVec.ofNat 64 (frameBitsA sz)).toNat < 64 + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)]; exact hbits_lt), bind_ok, lift_ok, bind_ok] + set size1 : U64 := ⟨(1#u64 : U64).bv <<< (⟨BitVec.ofNat 64 (frameBitsA sz)⟩ : U64).val⟩ + have hsize1_bv : size1.bv = (1 : Word) <<< pageBitsForSize (frameSizeOfA sz) := by + simp only [size1, ← hbits_sp] + have hv : (⟨BitVec.ofNat 64 (frameBitsA sz)⟩ : U64).val = frameBitsA sz := by + change (BitVec.ofNat 64 (frameBitsA sz)).toNat = frameBitsA sz + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + rw [hv]; rfl + have hsize1_val : size1.val = 2 ^ frameBitsA sz := by + cases sz <;> decide + -- Window checks: `offset &&& 63 ≠ 0`, `offset ≥ size1`, `size1 - offset < SIZE` + have hInvArg0 : (a0.bv &&& 63 ≠ 0 ∨ a0.bv ≥ size1.bv ∨ size1.bv - a0.bv < smSize) → + (do let r ← error.invalid_arg Unit 0#u64; ok (r, s, a)) ⦃ res => + VcpuTopPostA (L := L) AR Z OS m true σ + ((NonDetE.liftME Fl4ArchInvocation.vcpu <| + decodeVcpuRun (Z := Unit) (specPtr vc) (args.map UScalar.bv) true).run σ) s res ⦄ := by + intro hbad; unfold error.invalid_arg; rw [bind_ok, WP.spec_ok] + have hd : (decodeVcpuRun (Z := Unit) (specPtr vc) (args.map UScalar.bv) true).run σ = + ({(.error (.invalidArgument 0), σ)}, False) := by + rw [hd_buf, hbuf_spec]; unfold frameRightsDev stateWindow; dsimp only + rw [← hsize1_bv, if_pos hbad]; dsimp only + unfold NonDetE.whenE; rw [if_neg (by simp [hr_sp])] + change ((NonDet.pure (Except.ok ())) >>= _) σ = _ + rw [nd_bind_oneSc (m := NonDet.pure (Except.ok ())) rfl]; rfl + exact ⟨rfl, hAR, .invalidArgument 0, lift_vcpu_run_errA hd, Or.inl rfl⟩ + by_cases hoff63 : (a0 &&& 63#u64 != 0#u64) = true + · rw [if_pos hoff63] + exact hInvArg0 (Or.inl fun e => by have : a0 &&& 63#u64 = 0#u64 := u64_eq_of_bv_eq e; simp [this] at hoff63) + · rw [if_neg hoff63] + have hoff63_0 : a0.bv &&& 63 = 0 := by + have : a0 &&& 63#u64 = 0#u64 := by by_contra hne; exact hoff63 (bne_iff_ne.2 hne) + exact congrArg UScalar.bv this + by_cases hge_sz : a0 >= size1 + · rw [if_pos hge_sz] + exact hInvArg0 (Or.inr (Or.inl hge_sz)) + · rw [if_neg hge_sz] + have hlt_sz : a0.val < size1.val := by change ¬ a0.val ≥ size1.val at hge_sz; omega + rw [u64_sub_ok (by omega), bind_ok] + by_cases hlt_sm : (⟨BitVec.ofNat 64 (size1.val - a0.val)⟩ : U64) < fl4_abi.consts.vcpu_state_mask.SIZE + · rw [if_pos hlt_sm] + have hsub_bv : (⟨BitVec.ofNat 64 (size1.val - a0.val)⟩ : U64).bv = size1.bv - a0.bv := by + apply BitVec.eq_of_toNat_eq + change (BitVec.ofNat 64 (size1.val - a0.val)).toNat = (size1.bv - a0.bv).toNat + rw [BitVec.toNat_ofNat, BitVec.toNat_sub] + have hsb : size1.val < 2 ^ 64 := size1.bv.isLt + have hab : a0.val < 2 ^ 64 := a0.bv.isLt + change (size1.val - a0.val) % 2 ^ 64 = (2 ^ 64 - a0.val + size1.val) % 2 ^ 64 + omega + have hsm_bv : fl4_abi.consts.vcpu_state_mask.SIZE.bv = smSize := by + unfold fl4_abi.consts.vcpu_state_mask.SIZE smSize; rfl + exact hInvArg0 (Or.inr (Or.inr (by rwa [← hsub_bv, ← hsm_bv]))) + · rw [if_neg hlt_sm] + have hwin_ok : ¬ (a0.bv &&& 63 ≠ 0 ∨ a0.bv ≥ size1.bv ∨ size1.bv - a0.bv < smSize) := by + push_neg + refine ⟨hoff63_0, by intro h; change size1.val ≤ a0.val at h; omega, ?_⟩ + have hsub_bv : (⟨BitVec.ofNat 64 (size1.val - a0.val)⟩ : U64).bv = size1.bv - a0.bv := by + apply BitVec.eq_of_toNat_eq + change (BitVec.ofNat 64 (size1.val - a0.val)).toNat = (size1.bv - a0.bv).toNat + rw [BitVec.toNat_ofNat, BitVec.toNat_sub] + have hsb : size1.val < 2 ^ 64 := size1.bv.isLt + have hab : a0.val < 2 ^ 64 := a0.bv.isLt + change (size1.val - a0.val) % 2 ^ 64 = (2 ^ 64 - a0.val + size1.val) % 2 ^ 64 + omega + have hsm_bv : fl4_abi.consts.vcpu_state_mask.SIZE.bv = smSize := by + unfold fl4_abi.consts.vcpu_state_mask.SIZE smSize; rfl + rw [← hsub_bv, ← hsm_bv]; exact hlt_sm + -- Now check `v_tcb`, `vcpu.bound ct`, `vc.RUN_CALLER` + have htcb_off : vcpu.TCB = 0#u64 := by unfold vcpu.TCB; rfl + rw [htcb_off, lift_ok, bind_ok, wa_zeroA, L.read_eq, bind_ok] + set v_tcb := L.mem s vc + obtain ⟨v0, hkv0, hvt0, hrc0⟩ := C.hV.core.obj vc C.hvc + obtain ⟨xct', hkct', htv_ct, hrv_ct⟩ := C.hV.core.tcb ct hcurT + rw [hkct] at hkct'; cases hkct' + have hd_vcpu : (decodeVcpuRun (Z := Unit) (specPtr vc) (args.map UScalar.bv) true).run σ = + ((do NonDetE.whenE (v0.vcpuTcb = none ∨ v0.vcpuTcb = some (specPtr ct) ∨ xct.tcbArch.tcbVcpu ≠ none) <| + NonDetE.throwError .illegalOperation + NonDetE.whenE (v0.vcpuExt.runCaller ≠ none) <| NonDetE.throwError .deleteFirst + if a1.bv &&& (smVgic ||| smVgicLr) ≠ 0 then do + let vg ← NonDetE.liftE <| readVgicFrame (specPtr ptr + a0.bv) + match vg with + | some vg => NonDetE.returnOk (.run (specPtr vc) (specPtr ct) (specPtr ptr + a0.bv) a0.bv a1.bv a2.bv vg) + | none => NonDetE.throwError (.invalidArgument 1) + else NonDetE.returnOk (.run (specPtr vc) (specPtr ct) (specPtr ptr + a0.bv) a0.bv a1.bv a2.bv (0, [])) : SEMonad Unit Fl4VcpuInvocation)).run σ := by + rw [hd_buf, hbuf_spec]; unfold frameRightsDev stateWindow; dsimp only + rw [← hsize1_bv, ite_eq_right hwin_ok]; dsimp only + rw [NonDetE.whenE, ite_eq_right (by simp [hr_sp]), ndE_pure_bindA] + unfold NonDetE.liftE + change ((getVcpu (specPtr vc) >>= fun v => NonDet.pure (Except.ok v)) >>= _) σ = _ + rw [nd_bind_assocSc, nd_bind_oneSc (getVcpu_eqA hkv0), nd_bind_oneSc (m := NonDet.pure _) rfl] + change ((archThreadGet ArchTcb.tcbVcpu (specPtr ct) >>= fun tv => NonDet.pure (Except.ok tv)) >>= _) σ = _ + rw [nd_bind_assocSc, nd_bind_oneSc (archThreadGet_eqA _ hkct), nd_bind_oneSc (m := NonDet.pure _) rfl]; rfl + by_cases hvt_eq0 : v_tcb = 0#u64 + · rw [if_pos hvt_eq0]; unfold error.illegal; rw [bind_ok, WP.spec_ok] + have hnone : v0.vcpuTcb = none := by rw [hvt0]; change optRefA v_tcb = _; rw [hvt_eq0]; rfl + have hd : (decodeVcpuRun (Z := Unit) (specPtr vc) (args.map UScalar.bv) true).run σ = + ({(.error .illegalOperation, σ)}, False) := by + rw [hd_vcpu]; exact ndE_whenE_true (Or.inl hnone) _ _ _ + exact ⟨rfl, hAR, .illegalOperation, lift_vcpu_run_errA hd, Or.inl trivial⟩ + · rw [if_neg hvt_eq0] + by_cases hvt_ct : v_tcb = ct + · rw [if_pos hvt_ct]; unfold error.illegal; rw [bind_ok, WP.spec_ok] + have hsome : v0.vcpuTcb = some (specPtr ct) := by + rw [hvt0]; change optRefA v_tcb = _; rw [hvt_ct]; unfold optRefA; rw [ite_eq_right (by rw [← hvt_ct]; exact hvt_eq0)] + have hd : (decodeVcpuRun (Z := Unit) (specPtr vc) (args.map UScalar.bv) true).run σ = + ({(.error .illegalOperation, σ)}, False) := by + rw [hd_vcpu]; exact ndE_whenE_true (Or.inr (Or.inl hsome)) _ _ _ + exact ⟨rfl, hAR, .illegalOperation, lift_vcpu_run_errA hd, Or.inl trivial⟩ + · rw [if_neg hvt_ct] + have hbound_ok : vcpu.bound st s ct = ok (L.mem s (wa ct 3664#u64)) := by + unfold vcpu.bound; rw [eq_of_spec_fd Fl4ProofsA64.P0.Consts.layout.tcb.VCPU.spec, bind_ok, lift_ok, bind_ok, L.read_eq] + rw [hbound_ok, bind_ok] + by_cases hct_b : (L.mem s (wa ct 3664#u64) != 0#u64) = true + · rw [if_pos hct_b]; unfold error.illegal; rw [bind_ok, WP.spec_ok] + have hne0 : L.mem s (wa ct 3664#u64) ≠ 0#u64 := bne_iff_ne.1 hct_b + have hsome_ct : xct.tcbArch.tcbVcpu ≠ none := by + rw [htv_ct]; unfold optRefA; rw [if_neg hne0]; simp + have hd : (decodeVcpuRun (Z := Unit) (specPtr vc) (args.map UScalar.bv) true).run σ = + ({(.error .illegalOperation, σ)}, False) := by + rw [hd_vcpu]; exact ndE_whenE_true (Or.inr (Or.inr hsome_ct)) _ _ _ + exact ⟨rfl, hAR, .illegalOperation, lift_vcpu_run_errA hd, Or.inl trivial⟩ + · rw [if_neg hct_b] + have hct_v0 : L.mem s (wa ct 3664#u64) = 0#u64 := by by_contra hne; exact hct_b (bne_iff_ne.2 hne) + have hrc_off : vcpu.RUN_CALLER = 24#u64 := by unfold vcpu.RUN_CALLER; rfl + rw [hrc_off, lift_ok, bind_ok, L.read_eq, bind_ok] + have hno_ill : ¬ (v0.vcpuTcb = none ∨ v0.vcpuTcb = some (specPtr ct) ∨ xct.tcbArch.tcbVcpu ≠ none) := by + push_neg + refine ⟨by rw [hvt0]; unfold optRefA; rw [if_neg hvt_eq0]; simp, + by rw [hvt0]; unfold optRefA; rw [if_neg hvt_eq0]; intro e; exact hvt_ct (specPtr_inj (Option.some.inj e)), + by rw [htv_ct, hct_v0]; rfl⟩ + by_cases hrc_nz : (L.mem s (wa vc 24#u64) != 0#u64) = true + · rw [if_pos hrc_nz, WP.spec_ok] + have hne_rc : L.mem s (wa vc 24#u64) ≠ 0#u64 := bne_iff_ne.1 hrc_nz + have hrc_some : v0.vcpuExt.runCaller ≠ none := by + rw [hrc0]; unfold optRefA; rw [if_neg hne_rc]; simp + have hd : (decodeVcpuRun (Z := Unit) (specPtr vc) (args.map UScalar.bv) true).run σ = + ({(.error .deleteFirst, σ)}, False) := by + rw [hd_vcpu]; unfold NonDetE.whenE; rw [if_neg hno_ill] + change ((NonDet.pure (Except.ok ())) >>= _) σ = _ + rw [nd_bind_oneSc (m := NonDet.pure (Except.ok ())) rfl] + exact ndE_whenE_true hrc_some _ _ _ + exact ⟨rfl, hAR, .deleteFirst, lift_vcpu_run_errA hd, Or.inl trivial⟩ + · rw [if_neg hrc_nz] + have hrc_eq0 : L.mem s (wa vc 24#u64) = 0#u64 := by by_contra hne; exact hrc_nz (bne_iff_ne.2 hne) + have hrc_none : v0.vcpuExt.runCaller = none := by rw [hrc0, hrc_eq0]; rfl + rw [lift_ok, bind_ok] + set frame := wa ptr a0 + have hframe_spec : specPtr frame = specPtr ptr + a0.bv := by + unfold frame specPtr + rw [core.num.U64.wrapping_add_bv_eq, BitVec.add_assoc, BitVec.add_comm a0.bv pptrShiftA, ← BitVec.add_assoc] + obtain ⟨hptr_al, hptr_fit, hptr_ub⟩ := hF.frames (sbWordA ct) hsb_os ptr sz rights mp false asid va hcap_eq + have hWin : FrameWinA Ub frame := by + have hrem : 1024 ≤ size1.val - a0.val := by + have hsm : fl4_abi.consts.vcpu_state_mask.SIZE.val = 1024 := by + unfold fl4_abi.consts.vcpu_state_mask.SIZE; rfl + have hsub_val : (⟨BitVec.ofNat 64 (size1.val - a0.val)⟩ : U64).val = size1.val - a0.val := by + have hsb : size1.val < 2 ^ 64 := size1.bv.isLt + change (BitVec.ofNat 64 _).toNat = _; rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + change ¬ (⟨BitVec.ofNat 64 (size1.val - a0.val)⟩ : U64).val < fl4_abi.consts.vcpu_state_mask.SIZE.val at hlt_sm + omega + have hfit_sz : ptr.val + size1.val ≤ 2 ^ 64 := by rw [hsize1_val]; exact hptr_fit + have hfr_val : frame.val = ptr.val + a0.val := by + rw [wa_valP, Nat.mod_eq_of_lt (by omega)] + refine ⟨by omega, fun i hi => ?_⟩ + let o : U64 := ⟨BitVec.ofNat 64 (a0.val + 8 * i)⟩ + have ho_val : o.val = a0.val + 8 * i := by + change (BitVec.ofNat 64 _).toNat = _; rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + have ho_lt : o.val < 2 ^ frameBitsA sz := by rw [ho_val, ← hsize1_val]; omega + have ho8 : o.val % 8 = 0 := by + have ha0_8 : a0.val % 64 = 0 := by + have hbv : (a0.bv &&& 63).toNat = 0 := by rw [hoff63_0]; rfl + rw [BitVec.toNat_and] at hbv + have h63 : (63 : Word).toNat = 63 := rfl + rw [h63] at hbv + change a0.val &&& 63 = 0 at hbv + have hmod : a0.val % 64 = a0.val &&& 63 := by + have h6 := Nat.and_two_pow_sub_one_eq_mod a0.val 6 + simp only [Nat.reducePow, Nat.reduceSub] at h6; exact h6.symm + omega + rw [ho_val]; omega + have heq_f : wa frame ⟨BitVec.ofNat 64 (8 * i)⟩ = wa ptr o := by + apply UScalar.eq_of_val_eq + have h8i : (⟨BitVec.ofNat 64 (8 * i)⟩ : U64).val = 8 * i := by + change (BitVec.ofNat 64 _).toNat = _; rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + rw [wa_valP, wa_valP, wa_valP, ho_val, h8i, + Nat.mod_eq_of_lt (by omega), Nat.mod_eq_of_lt (by omega), Nat.mod_eq_of_lt (by omega)]; omega + rw [heq_f]; exact hptr_ub o ho_lt ho8 + -- Bind `num_lrs`, `vcpu_gic_version`, and state masks + rw [vcpu_num_lrs_eqA hP a, bind_ok] + obtain ⟨gv, hgv, hgv_eq⟩ := hP.gic a + rw [hgv, bind_ok] + have hsmV : fl4_abi.consts.vcpu_state_mask.VGIC = ok 8#u64 := by unfold fl4_abi.consts.vcpu_state_mask.VGIC; rfl + have hsmL : fl4_abi.consts.vcpu_state_mask.VGIC_LR = ok 32#u64 := by unfold fl4_abi.consts.vcpu_state_mask.VGIC_LR; rfl + rw [hsmV, bind_ok, hsmL, bind_ok, lift_ok, bind_ok, lift_ok, bind_ok] + -- Shared post-vgic body! + obtain ⟨hvtT, -⟩ := C.hV.bind_v2t vc C.hvc v_tcb rfl hvt_eq0 + have hCs128 : ∀ c ∈ Z.Cs, c.val < 128 := by + obtain ⟨n, -, hn128, hcn⟩ := C.E.j.1.csN; intro c hc; have := (hcn c).1 hc; omega + have hcpu128 : (L.cpu s).val < 128 := hCs128 _ C.E.j.1.sch.cpuCs + have hX0 : VcpuXferStateA (L := L) AR Z OS Ub m a s σ := + ⟨⟨{ C.E.j.1 with ar := hAR }, C.E.j.2⟩, C.hTr, hCar, C.hV, hF, hR⟩ + -- `setThreadState σ.curThread .restart σ` (for `VcpuTopPostA`) + have hvc_st : ∀ v ∈ Z.V, stA ct ≠ v ∧ stA ct ≠ wa v 24#u64 := fun v hv => by + rw [stA_eq] + exact ⟨by rw [← wa_zeroA v]; exact (C.hV.place.disj_tcb v hv ct hcurT 0#u64 3328#u64 (by decide) (by decide)).symm, + (C.hV.place.disj_tcb v hv ct hcurT 24#u64 3328#u64 (by decide) (by decide)).symm⟩ + obtain ⟨xct_r, hkct_r, -, hst_restart, -, -, -, -, -⟩ := C.E.restartA Pl Y hrun hcur hvs hvc_st + rw [hct_spec, hkct] at hkct_r; cases hkct_r + set σ_r := stUpdA σ (specPtr ct) xct .restart + -- Shared body lemma for both `in_mask &&& (VGIC ||| VGIC_LR) ≠ 0` and `= 0` + let hBody := vcpu_run_bodyA (args := args) (a0 := a0) (a1 := a1) C hP hAK hVR hS2R hF hU hLD hUbUser hksTcb hSlotTcb hOS_tcb + hBufNotS2 hCurNoRun hCurNotIdle hCurSched hSlotSt hOS_st hKs_st hSc_st hSc_reg hVs_st hExt_st hNtfn + hm2_0 hcurT hkct hsb_os hkv0 hvt0 hvt_eq0 hvt_ct hct_v0 hrc_eq0 hrc_off hWin hvtT hCs128 hcpu128 hX0 + (by rw [← hct_spec]; exact hst_restart) + -- Now split on `in_mask &&& (VGIC ||| VGIC_LR) != 0` + by_cases hvg_m : (a1 &&& (8#u64 ||| 32#u64) != 0#u64) = true + · have hvg_mb : a1.bv &&& (smVgic ||| smVgicLr) ≠ 0 := fun e => by + have : a1 &&& (8#u64 ||| 32#u64) = 0#u64 := u64_eq_of_bv_eq e; simp [this] at hvg_m + rw [if_pos hvg_m, hgv_eq] + refine WP.spec_bind (read_vgic_frame_refinesA hF hWin) ?_ + intro o ho + cases o with + | none => + dsimp only; unfold error.invalid_arg; rw [bind_ok, WP.spec_ok] + have hd : (decodeVcpuRun (Z := Unit) (specPtr vc) (args.map UScalar.bv) true).run σ = + ({(.error (.invalidArgument 1), σ)}, False) := by + rw [hd_vcpu]; unfold NonDetE.whenE; rw [if_neg hno_ill] + change ((NonDet.pure (Except.ok ())) >>= _) σ = _ + rw [nd_bind_oneSc (m := NonDet.pure (Except.ok ())) rfl, if_neg (by simp [hrc_none])] + change ((NonDet.pure (Except.ok ())) >>= _) σ = _ + rw [nd_bind_oneSc (m := NonDet.pure (Except.ok ())) rfl, if_pos hvg_mb, ← hframe_spec] + unfold NonDetE.liftE + change ((readVgicFrame (specPtr frame) >>= fun r => NonDet.pure (Except.ok r)) >>= _) σ = _ + rw [nd_bind_assocSc, nd_bind_oneSc ho, nd_bind_oneSc (m := NonDet.pure _) rfl]; rfl + exact ⟨rfl, hAR, .invalidArgument 1, lift_vcpu_run_errA hd, Or.inl rfl⟩ + | some v_vg => + obtain ⟨lrsList, hlen_l, hget_l, hread_ok⟩ := ho + have hd_ok : (decodeVcpuRun (Z := Unit) (specPtr vc) (args.map UScalar.bv) true).run σ = + ({(.ok (.run (specPtr vc) (specPtr ct) (specPtr frame) a0.bv a1.bv a2.bv (v_vg.1.bv, lrsList)), σ)}, False) := by + rw [hd_vcpu]; unfold NonDetE.whenE; rw [if_neg hno_ill] + change ((NonDet.pure (Except.ok ())) >>= _) σ = _ + rw [nd_bind_oneSc (m := NonDet.pure (Except.ok ())) rfl, if_neg (by simp [hrc_none])] + change ((NonDet.pure (Except.ok ())) >>= _) σ = _ + rw [nd_bind_oneSc (m := NonDet.pure (Except.ok ())) rfl, if_pos hvg_mb, ← hframe_spec] + unfold NonDetE.liftE + change ((readVgicFrame (specPtr frame) >>= fun r => NonDet.pure (Except.ok r)) >>= _) σ = _ + rw [nd_bind_assocSc, nd_bind_oneSc hread_ok, nd_bind_oneSc (m := NonDet.pure _) rfl]; rfl + have hVgic : a1.bv &&& (smVgic ||| smVgicLr) ≠ 0 → + v_vg.1.bv = v_vg.1.bv ∧ v_vg.1.bv >>> 32 = 0 ∧ + lrsList.length = fl4NumLrs ∧ ∀ j, j < fl4NumLrs → (v_vg.2.val[j]!).bv = lrsList[j]! := fun _ => + ⟨rfl, (by + rw [readVgicFrame_eqA _ _ (al3_of_frameWinA hF hWin)] at hread_ok + split_ifs at hread_ok with h_ok + · have heq := (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (Prod.mk.inj hread_ok).1)).1 + obtain ⟨hvg_eq, -⟩ := Prod.mk.inj (Option.some.inj heq) + rw [← hvg_eq] + have hlt : (sanitizeVgicHcr (loadWordAt σ.machineState.underlyingMemory (bufAddr (specPtr frame) vrVgicHcr)) fl4GicV3).toNat < 2 ^ 32 := by + unfold sanitizeVgicHcr + cases fl4GicV3 + · simp only [Bool.false_eq_true, ↓reduceIte, BitVec.toNat_and] + exact lt_of_le_of_lt Nat.and_le_right (by decide) + · simp only [↓reduceIte, BitVec.toNat_and] + exact lt_of_le_of_lt Nat.and_le_right (by decide) + change (sanitizeVgicHcr (loadWordAt σ.machineState.underlyingMemory (bufAddr (specPtr frame) vrVgicHcr)) fl4GicV3 >>> 32 : BitVec 64) = 0#64 + apply BitVec.eq_of_toNat_eq + rw [BitVec.toNat_ushiftRight, Nat.shiftRight_eq_div_pow] + exact Nat.div_eq_of_lt hlt + · have heq := (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (Prod.mk.inj hread_ok).1)).1 + cases heq), + hlen_l, hget_l⟩ + dsimp only + rw [eq_of_spec_fd (obj.Tcb.state.spec s v_tcb), bind_ok] + have hst_n : (if L.mem s (stA v_tcb) != sched.TS_INACTIVE then smp.remote_tcb_stall st ar s a v_tcb else ok (s, a)) = ok (s, a) := by + by_cases hi : (L.mem s (stA v_tcb) != sched.TS_INACTIVE) = true + · rw [if_pos hi]; exact hStall v_tcb hvtT s a + · rw [if_neg hi] + rw [hst_n, bind_ok] + refine WP.spec_bind (Pₘ := fun r => r = L.cpu s) (by rw [L.cur_cpu_eq]; exact (WP.spec_ok _).2 rfl) ?_ + rintro _ rfl + refine WP.spec_bind (Pₘ := fun r => r = wa (L.cpu s) 1#u64) (by rw [lift_ok]; exact (WP.spec_ok _).2 rfl) ?_ + rintro _ rfl + refine WP.spec_bind (Pₘ := fun r => r = wa vc vcpu.OWNER_CPU) ((WP.spec_ok _).2 rfl) ?_ + rintro _ rfl + rw [L.read_eq, bind_ok] + have hfl_st : ∀ (fi : U64), + (do let a4 ← ar.vcpu_flush_state a vc fi; ok (a4, v_vg)) ⦃ p => AR p.1 Z.hw ∧ p.2 = v_vg ⦄ := fun fi => + WP.spec_bind (hAK.vcpu_flush_state a Z.hw vc fi hAR) fun a4 hAR4 => by rw [WP.spec_ok]; exact ⟨hAR4, rfl⟩ + have hfi_ok : (do + let i21 ← lift (a1 &&& 8#u64) + let (vgic1, flush_in) ← if i21 = 0#u64 then do + let i22 ← lift (a1 &&& 32#u64) + if i22 != 0#u64 then + let (i23, _) := v_vg + let i24 ← lift (i23 &&& 240#u64) + let i25 ← if i24 != 0#u64 then ok (a1 ||| 8#u64) else ok a1 + ok (v_vg, i25) + else ok (v_vg, a1) + else ok (v_vg, a1) + let a4 ← ar.vcpu_flush_state a vc flush_in + ok (a4, vgic1)) ⦃ p => AR p.1 Z.hw ∧ p.2 = v_vg ⦄ := by + rw [lift_ok, bind_ok] + by_cases h8 : a1 &&& 8#u64 = 0#u64 + · rw [if_pos h8, lift_ok, bind_ok] + by_cases h32 : (a1 &&& 32#u64 != 0#u64) = true + · rw [if_pos h32] + have hpair : ∀ (v : U64 × Std.Array U64 16#usize), + (let (i23, _) := v + do + let i24 ← lift (i23 &&& 240#u64) + let i25 ← if i24 != 0#u64 then ok (a1 ||| 8#u64) else ok a1 + ok (v, i25)) ⦃ p => p.1 = v ⦄ := fun ⟨v1, v2⟩ => by + dsimp only; rw [lift_ok, bind_ok] + by_cases h240 : (v1 &&& 240#u64 != 0#u64) = true + · rw [if_pos h240, bind_ok, WP.spec_ok] + · rw [if_neg h240, bind_ok, WP.spec_ok] + refine WP.spec_bind (hpair v_vg) ?_ + rintro ⟨vg1, fi⟩ hvg1; dsimp only at hvg1; rw [hvg1]; exact hfl_st fi + · rw [if_neg h32, bind_ok]; exact hfl_st _ + · rw [if_neg h8, bind_ok]; exact hfl_st _ + have hbr : (if L.mem s (wa vc vcpu.OWNER_CPU) != 0#u64 then + if L.mem s (wa vc vcpu.OWNER_CPU) != wa (L.cpu s) 1#u64 then do + let a4 ← ar.vcpu_flush a vc + let a5 ← ar.fpu_flush a4 v_tcb + let a6 ← ar.fpu_release a5 v_tcb + ok (a6, v_vg) + else do + let i21 ← lift (a1 &&& 8#u64) + let (vgic1, flush_in) ← if i21 = 0#u64 then do + let i22 ← lift (a1 &&& 32#u64) + if i22 != 0#u64 then + let (i23, _) := v_vg + let i24 ← lift (i23 &&& 240#u64) + let i25 ← if i24 != 0#u64 then ok (a1 ||| 8#u64) else ok a1 + ok (v_vg, i25) + else ok (v_vg, a1) + else ok (v_vg, a1) + let a4 ← ar.vcpu_flush_state a vc flush_in + ok (a4, vgic1) + else do + let i21 ← lift (a1 &&& 8#u64) + let (vgic1, flush_in) ← if i21 = 0#u64 then do + let i22 ← lift (a1 &&& 32#u64) + if i22 != 0#u64 then + let (i23, _) := v_vg + let i24 ← lift (i23 &&& 240#u64) + let i25 ← if i24 != 0#u64 then ok (a1 ||| 8#u64) else ok a1 + ok (v_vg, i25) + else ok (v_vg, a1) + else ok (v_vg, a1) + let a4 ← ar.vcpu_flush_state a vc flush_in + ok (a4, vgic1)) ⦃ p => AR p.1 Z.hw ∧ p.2 = v_vg ⦄ := by + by_cases hp0 : (L.mem s (wa vc vcpu.OWNER_CPU) != 0#u64) = true + · rw [if_pos hp0] + by_cases hpme : (L.mem s (wa vc vcpu.OWNER_CPU) != wa (L.cpu s) 1#u64) = true + · rw [if_pos hpme] + refine WP.spec_bind (hAK.vcpu_flush a Z.hw vc hAR) fun a4 hAR4 => + WP.spec_bind (hAK.fpu_flush a4 Z.hw v_tcb hAR4) fun a5 hAR5 => + WP.spec_bind (hAK.fpu_release a5 Z.hw v_tcb hAR5) fun a6 hAR6 => by + rw [WP.spec_ok]; exact ⟨hAR6, rfl⟩ + · rw [if_neg hpme]; exact hfi_ok + · rw [if_neg hp0]; exact hfi_ok + refine WP.spec_bind hbr ?_ + rintro ⟨a3, vg'⟩ ⟨hAR3, hvgeq⟩ + have hvg : vg' = v_vg := hvgeq + rw [hvg] + have hb := hBody v_vg (v_vg.1.bv, lrsList) hVgic hd_ok a3 hAR3 + have hrc24 : vcpu.RUN_CALLER = 24#u64 := by unfold vcpu.RUN_CALLER; rfl + conv at hb => arg 1; rw [lift_ok, hrc24, lift_ok] + exact hb + · have hvg_mb : ¬ (a1.bv &&& (smVgic ||| smVgicLr) ≠ 0) := fun h => + hvg_m (bne_iff_ne.2 (fun e => h (congrArg UScalar.bv e))) + rw [if_neg hvg_m] + have hd_ok : (decodeVcpuRun (Z := Unit) (specPtr vc) (args.map UScalar.bv) true).run σ = + ({(.ok (.run (specPtr vc) (specPtr ct) (specPtr frame) a0.bv a1.bv a2.bv (0, [])), σ)}, False) := by + rw [hd_vcpu]; unfold NonDetE.whenE; rw [if_neg hno_ill] + change ((NonDet.pure (Except.ok ())) >>= _) σ = _ + rw [nd_bind_oneSc (m := NonDet.pure (Except.ok ())) rfl, if_neg (by simp [hrc_none])] + change ((NonDet.pure (Except.ok ())) >>= _) σ = _ + rw [nd_bind_oneSc (m := NonDet.pure (Except.ok ())) rfl, if_neg hvg_mb, ← hframe_spec]; rfl + rw [eq_of_spec_fd (obj.Tcb.state.spec s v_tcb), bind_ok] + have hst_n : (if L.mem s (stA v_tcb) != sched.TS_INACTIVE then smp.remote_tcb_stall st ar s a v_tcb else ok (s, a)) = ok (s, a) := by + by_cases hi : (L.mem s (stA v_tcb) != sched.TS_INACTIVE) = true + · rw [if_pos hi]; exact hStall v_tcb hvtT s a + · rw [if_neg hi] + rw [hst_n, bind_ok] + refine WP.spec_bind (Pₘ := fun r => r = L.cpu s) (by rw [L.cur_cpu_eq]; exact (WP.spec_ok _).2 rfl) ?_ + rintro _ rfl + refine WP.spec_bind (Pₘ := fun r => r = wa (L.cpu s) 1#u64) (by rw [lift_ok]; exact (WP.spec_ok _).2 rfl) ?_ + rintro _ rfl + refine WP.spec_bind (Pₘ := fun r => r = wa vc vcpu.OWNER_CPU) ((WP.spec_ok _).2 rfl) ?_ + rintro _ rfl + rw [L.read_eq, bind_ok] + have hfi0 : (do + let i21 ← lift (a1 &&& 8#u64) + let flush_in ← if i21 = 0#u64 then do + let i22 ← lift (a1 &&& 32#u64) + if i22 != 0#u64 then + let i23 ← lift (0#u64 &&& 240#u64) + if i23 != 0#u64 then ok (a1 ||| 8#u64) else ok a1 + else ok a1 + else ok a1 + ar.vcpu_flush_state a vc flush_in) ⦃ a3 => AR a3 Z.hw ⦄ := by + rw [lift_ok, bind_ok] + by_cases h8 : a1 &&& 8#u64 = 0#u64 + · rw [if_pos h8, lift_ok, bind_ok] + by_cases h32 : (a1 &&& 32#u64 != 0#u64) = true + · rw [if_pos h32, lift_ok, bind_ok] + simp only [show (0#u64 &&& 240#u64 != 0#u64) = false by decide, Bool.false_eq_true, ↓reduceIte, bind_ok] + exact hAK.vcpu_flush_state a Z.hw vc a1 hAR + · rw [if_neg h32, bind_ok]; exact hAK.vcpu_flush_state a Z.hw vc a1 hAR + · rw [if_neg h8, bind_ok]; exact hAK.vcpu_flush_state a Z.hw vc a1 hAR + have hbr0 : (if L.mem s (wa vc vcpu.OWNER_CPU) != 0#u64 then + if L.mem s (wa vc vcpu.OWNER_CPU) != wa (L.cpu s) 1#u64 then do + let a4 ← ar.vcpu_flush a vc + let a5 ← ar.fpu_flush a4 v_tcb + ar.fpu_release a5 v_tcb + else do + let i21 ← lift (a1 &&& 8#u64) + let flush_in ← if i21 = 0#u64 then do + let i22 ← lift (a1 &&& 32#u64) + if i22 != 0#u64 then + let i23 ← lift (0#u64 &&& 240#u64) + if i23 != 0#u64 then ok (a1 ||| 8#u64) else ok a1 + else ok a1 + else ok a1 + ar.vcpu_flush_state a vc flush_in + else do + let i21 ← lift (a1 &&& 8#u64) + let flush_in ← if i21 = 0#u64 then do + let i22 ← lift (a1 &&& 32#u64) + if i22 != 0#u64 then + let i23 ← lift (0#u64 &&& 240#u64) + if i23 != 0#u64 then ok (a1 ||| 8#u64) else ok a1 + else ok a1 + else ok a1 + ar.vcpu_flush_state a vc flush_in) ⦃ a3 => AR a3 Z.hw ⦄ := by + by_cases hp0 : (L.mem s (wa vc vcpu.OWNER_CPU) != 0#u64) = true + · rw [if_pos hp0] + by_cases hpme : (L.mem s (wa vc vcpu.OWNER_CPU) != wa (L.cpu s) 1#u64) = true + · rw [if_pos hpme] + refine WP.spec_bind (hAK.vcpu_flush a Z.hw vc hAR) fun a4 hAR4 => + WP.spec_bind (hAK.fpu_flush a4 Z.hw v_tcb hAR4) fun a5 hAR5 => + hAK.fpu_release a5 Z.hw v_tcb hAR5 + · rw [if_neg hpme]; exact hfi0 + · rw [if_neg hp0]; exact hfi0 + refine WP.spec_bind hbr0 ?_ + intro a3 hAR3 + have hb := hBody (0#u64, Array.repeat 16#usize 0#u64) (0, []) (fun h => absurd h hvg_mb) hd_ok a3 hAR3 + have hrc24 : vcpu.RUN_CALLER = 24#u64 := by unfold vcpu.RUN_CALLER; rfl + conv at hb => arg 1; rw [lift_ok, hrc24, lift_ok] + exact hb + | PageTable p m_pt asid va => + rw [hcap_eq] at habs; dsimp only + cases m_pt <;> exact hIllBuf (fun _ _ _ _ _ e => by rw [e] at habs; cases habs) + | _ => + rw [hcap_eq] at habs hwf; dsimp only + first + | exact False.elim hwf + | exact hIllBuf (fun _ _ _ _ _ e => by rw [e] at habs; cases habs) + +end decode_run + + +end +end Fl4ProofsA64.P3.Vcpu diff --git a/proofs/Fl4ProofsA64/P3/VcpuRunLinkJA.lean b/proofs/Fl4ProofsA64/P3/VcpuRunLinkJA.lean new file mode 100644 index 000000000..20c0b9c9a --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/VcpuRunLinkJA.lean @@ -0,0 +1,2601 @@ +/- +P3 (aarch64, A64-P3 S4 W2b-1): **VCPU link updates, `cancel_run`, and `complete_run` refinement**. + +Provides: +- `TcbLinkDisjA`: placement disjointness for TCB VCPU link words (`t + 3664`, `t + 3672`). +- `VcpuVmRootA`: architecture `vspace.set_vm_root` root-switch premise (discharged by `ScArchSwA` + + `vspace.set_vm_root.same`). +- `VcpuXferStateA.of_hw`: updating `(a, Z.hw)` across `vspace.set_vm_root` with `s, σ` unchanged. +- `VcpuXferStateA.clear_run_link`: lockstep clearing of `vc.RUN_CALLER := 0` and `ct.RUN_VCPU := 0`. +- `VcpuXferStateA.dissoc_bind`: lockstep clearing of `vc.TCB := 0`, `vc.OWNER_CPU := 0`, `t.VCPU := 0`, + and `t.SPSR_EL1` sanitisation (`fl4DissociateVcpuTcb`). +- `VcpuXferStateA.assoc_bind`: lockstep setting of `vc.TCB := t`, `t.VCPU := vc`. +- `VcpuXferStateA.set_state_running`: `sched.set_thread_state ... TS_RUNNING` from `TS_RUNNING_VM`. +- `cancel_run_refinesA`: `vcpu.cancel_run` refines `fl4CancelRun` (both quiet and live). +- `complete_run_refinesA`: `vcpu.complete_run` refines `fl4CompleteRun` (reusing W3's + `read_state_to_frame_refinesA`, 7× `VcpuXferStateA.tcb_reg_wr`, and `VcpuVmRootA`). + +Named premises: `[P] VcpuPlatAgreeA ar`, `[P] VcpuArchKeepsA ar AR`, `[P] VcpuVmRootA st ar AR`. +-/ +import Fl4ProofsA64.P3.VcpuXferJA + +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3.Vcpu + +open Fl4Spec Fl4SpecA64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3 Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Endp +open Fl4ProofsA64.P3.Notif + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## Section 1: Disjointness & VM-root premises -/ + +/-- Placement disjointness for TCB VCPU link words (`t + 3664` `VCPU`, `t + 3672` `RUN_VCPU`) +against non-VCPU footprint words (slots, kernel state block, SCs, VSpace). -/ +structure TcbLinkDisjA (Z : XSetsA) (ksb : U64) : Prop where + offSlot : ∀ t ∈ Z.T, ∀ K : U64, (K = 3664#u64 ∨ K = 3672#u64) → + ∀ y, SlotWA Z.th y → wa t K ≠ y ∧ wa t K ≠ wa y 8#u64 ∧ wa t K ≠ wa y 16#u64 ∧ wa t K ≠ wa y 24#u64 + offKs : ∀ t ∈ Z.T, ∀ K : U64, (K = 3664#u64 ∨ K = 3672#u64) → + ∀ o : U64, o.val < ksSizeA → wa t K ≠ wa ksb o + offSc : ∀ t ∈ Z.T, ∀ K : U64, (K = 3664#u64 ∨ K = 3672#u64) → + ∀ c ∈ Z.C, wa t K ≠ wa c 24#u64 ∧ wa t K ≠ wa c 72#u64 + offVs : ∀ t ∈ Z.T, ∀ K : U64, (K = 3664#u64 ∨ K = 3672#u64) → + ¬ VsFootA Z.Pp Z.S1 Z.Y ksb (wa t K) + offExt : ∀ t ∈ Z.T, ∀ K : U64, (K = 3664#u64 ∨ K = 3672#u64) → + ¬ ExtFootE Z.X Z.Y Z.T2 (wa t K) + +-- `VcpuVmRootA` is imported from `Fl4ProofsA64.P3.VcpuPremisesA` via `VcpuRelA`. + +section helpers +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + +/-- `FootA` depends on the store `s` only through `L.ksb s` and `L.cpu s`. -/ +theorem footA_of_ksb_cpuA {Z : XSetsA} {s s' : S} + (hksb : L.ksb s' = L.ksb s) (hcpu : L.cpu s' = L.cpu s) {q : U64} + (hq : FootA (L := L) Z s' q) : FootA (L := L) Z s q := by + match hq with + | .slot y _ hy hw => exact .slot y q hy hw + | .nslot _ hn => + refine .nslot q ?_ + match hn with + | .node c _ hc ho => + simp only [nodeAt_ksb hksb] at ho; exact .node c q hc ho + | .cur _ ho => + rw [hcpu] at ho; simp only [nodeAt_ksb hksb] at ho; exact .cur q ho + | .tcb t _ ht ho => exact .tcb t q ht ho + | .sc c hc => exact .sc c hc + | .vcpu v _ hv ho => exact .vcpu v q hv ho + | .irq i hi => rw [hksb]; exact .irq i hi + | .cur2 _ ho => + rw [hcpu] at ho; simp only [nodeAt_ksb hksb] at ho; exact .cur2 q ho + | .glob _ ho => rw [hksb] at ho; exact .glob q ho + | .tcb2 t _ ht ho => exact .tcb2 t q ht ho + | .sc2 c hc => exact .sc2 c hc + | .vs _ hv => rw [hksb] at hv; exact .vs q hv + | .ext _ he => exact .ext q he + +/-- Updating only `(a, Z.hw)` preserves `VcpuXferStateA` with `Z' := { Z with hw := hw' }`. -/ +theorem VcpuXferStateA.of_hw {AR : A → HwRootA → Prop} {Z : XSetsA} {OS Ub : Set U64} + {m : Fl4SpecA64.McState} {a a' : A} {hw' : HwRootA} {s : S} {σ : State Unit} + (X : VcpuXferStateA (L := L) AR Z OS Ub m a s σ) (hAR' : AR a' hw') : + VcpuXferStateA (L := L) AR { Z with hw := hw' } OS Ub m a' s σ := + ⟨⟨{ st := X.j.1.st, cdt := X.j.1.cdt, sch := X.j.1.sch, irq := X.j.1.irq, + rest := { rq := X.j.1.rest.rq, rqDom := X.j.1.rest.rqDom, rel := X.j.1.rest.rel, + consumed := X.j.1.rest.consumed, repr := X.j.1.rest.repr, curDom := X.j.1.rest.curDom, + domTime := X.j.1.rest.domTime, prio := X.j.1.rest.prio, scMem := X.j.1.rest.scMem, + refillMax := X.j.1.rest.refillMax }, + ar := hAR', place := X.j.1.place, thT := X.j.1.thT, csN := X.j.1.csN, + idleT := X.j.1.idleT, idleC := X.j.1.idleC, curT := X.j.1.curT, cscC := X.j.1.cscC, + ksFit := X.j.1.ksFit, scOffD := X.j.1.scOffD, cv := X.j.1.cv, pml := X.j.1.pml, + vs := X.j.1.vs, ext := X.j.1.ext }, + ⟨{ X.j.2.1 with }, { X.j.2.2.1 with }, { X.j.2.2.2 with }⟩⟩, + X.tr, X.car, { X.vcpu with core := { X.vcpu.core with }, place := { X.vcpu.place with } }, + { X.fmem with }, X.regs⟩ + +set_option maxHeartbeats 400000 in +/-- Framing `JAtZA`, `Stage2CoreA`, `HighVSpaceCoreA`, `TreeA`, `CarryA`, `FrameMemRelA`, and +`TcbRegsRelA` across a store/spec step that leaves all non-VCPU-link footprint words, all +user-frame words, all TCB user registers, and all non-VCPU/non-TCB-link spec fields unchanged. -/ +theorem jAtZA_of_vcpuLinkStepA {AR : A → HwRootA → Prop} {Z : XSetsA} + {OS Ub : Set U64} {m : Fl4SpecA64.McState} {a : A} {s s' : S} {σ σ' : State Unit} + (X : VcpuXferStateA (L := L) AR Z OS Ub m a s σ) + (hksb : L.ksb s' = L.ksb s) (hcpu : L.cpu s' = L.cpu s) + (hslot : ∀ y, SlotWA Z.th y → + L.mem s' y = L.mem s y ∧ L.mem s' (wa y 8#u64) = L.mem s (wa y 8#u64) ∧ + L.mem s' (wa y 16#u64) = L.mem s (wa y 16#u64) ∧ L.mem s' (wa y 24#u64) = L.mem s (wa y 24#u64)) + (hos : ∀ y ∈ OS, + L.mem s' y = L.mem s y ∧ L.mem s' (wa y 8#u64) = L.mem s (wa y 8#u64) ∧ + L.mem s' (wa y 16#u64) = L.mem s (wa y 16#u64) ∧ L.mem s' (wa y 24#u64) = L.mem s (wa y 24#u64)) + (hnode : ∀ c ∈ Z.Cs, + L.mem s' (L.nodeAt s c layout.node.CUR_THREAD) = L.mem s (L.nodeAt s c layout.node.CUR_THREAD) ∧ + L.mem s' (L.nodeAt s c layout.node.IDLE_THREAD) = L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) ∧ + L.mem s' (L.nodeAt s c layout.node.CUR_SC) = L.mem s (L.nodeAt s c layout.node.CUR_SC) ∧ + L.mem s' (L.nodeAt s c layout.node.IDLE_SC) = L.mem s (L.nodeAt s c layout.node.IDLE_SC)) + (htcb : ∀ t ∈ Z.T, + L.mem s' (stA t) = L.mem s (stA t) ∧ L.mem s' (scA t) = L.mem s (scA t) ∧ + L.mem s' (affA t) = L.mem s (affA t) ∧ L.mem s' (prioA t) = L.mem s (prioA t) ∧ + L.mem s' (domA t) = L.mem s (domA t)) + (hsc : ∀ c ∈ Z.C, L.mem s' (wa c 24#u64) = L.mem s (wa c 24#u64) ∧ + L.mem s' (wa c 72#u64) = L.mem s (wa c 72#u64)) + (hcur : L.mem s' (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) = L.mem s (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) ∧ + L.mem s' (L.nodeAt s (L.cpu s) layout.node.CUR_TIME) = L.mem s (L.nodeAt s (L.cpu s) layout.node.CUR_TIME) ∧ + L.mem s' (L.nodeAt s (L.cpu s) layout.node.CONSUMED) = L.mem s (L.nodeAt s (L.cpu s) layout.node.CONSUMED) ∧ + L.mem s' (L.nodeAt s (L.cpu s) layout.node.REPROGRAM) = L.mem s (L.nodeAt s (L.cpu s) layout.node.REPROGRAM)) + (hglob : L.mem s' (wa (L.ksb s) 8#u64) = L.mem s (wa (L.ksb s) 8#u64) ∧ + L.mem s' (wa (L.ksb s) 16#u64) = L.mem s (wa (L.ksb s) 16#u64)) + (hirq : ∀ i : U64, i.val < 5376 → L.mem s' (irqWordA (L.ksb s) i) = L.mem s (irqWordA (L.ksb s) i)) + (hvs : ∀ b, VsFootA Z.Pp Z.S1 Z.Y (L.ksb s) b → L.mem s' b = L.mem s b) + (hext : ∀ b, ExtFootE Z.X Z.Y Z.T2 b → L.mem s' b = L.mem s b) + (hub : ∀ b ∈ Ub, L.mem s' b = L.mem s b) + (hspec_glob : SpecGlobA σ σ') + (hspec_asid : σ'.archState.armAsidTable = σ.archState.armAsidTable) + (hspec_um : σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory) + (hspec_cdt : σ'.cdt = σ.cdt ∧ σ'.isOriginalCap = σ.isOriginalCap) + (hspec_kh : ∀ p, (∀ v ∈ Z.V, specPtr v ≠ p) → (∀ t ∈ Z.T, specPtr t ≠ p) → σ'.kheap p = σ.kheap p) + (hspec_tcb : ∀ t ∈ Z.T, ∀ x, σ.kheap (specPtr t) = some (.tcb x) → + ∃ x', σ'.kheap (specPtr t) = some (.tcb x') ∧ + x'.tcbState = x.tcbState ∧ x'.tcbSchedContext = x.tcbSchedContext ∧ + x'.tcbDomain = x.tcbDomain ∧ x'.tcbPriority = x.tcbPriority ∧ + (∀ i < tcbSlotsA, Fl4SpecA64.tcbCnodeMap x' (Fl4SpecA64.tcbCnodeIndex i) = + Fl4SpecA64.tcbCnodeMap x (Fl4SpecA64.tcbCnodeIndex i))) + (hspec_ty : TyKeepA σ σ') + (hspec_s2 : ∀ p es, σ'.kheap p = some (.archObj (.ext (.stage2Root es))) → + σ.kheap p = some (.archObj (.ext (.stage2Root es)))) + (hspec_hv : ∀ p l1, σ'.kheap p = some (.archObj (.ext (.highVSpace l1))) → + σ.kheap p = some (.archObj (.ext (.highVSpace l1)))) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) : + JAtZA (L := L) AR Z s' a σ' m ∧ + Stage2CoreA (L := L) Z s' σ' ∧ + HighVSpaceCoreA (L := L) Z s' σ' ∧ + TreeA st OS Z.G.chains Z.G.par s' ∧ + CarryA Z (L.mem s') σ' ∧ + FrameMemRelA Z OS Ub (L.ksb s') (L.mem s') σ'.machineState.underlyingMemory := by + have hcap_os : ∀ y ∈ OS, capAtA (L.mem s') y = capAtA (L.mem s) y := fun y hy => by + obtain ⟨h0, h8, -, -⟩ := hos y hy; unfold capAtA; rw [h0, h8] + have hcap_sl : ∀ y, SlotWA Z.th y → capAtA (L.mem s') y = capAtA (L.mem s) y := fun y hy => by + obtain ⟨h0, h8, -, -⟩ := hslot y hy; unfold capAtA; rw [h0, h8] + have hrev_sl : ∀ y, SlotWA Z.th y → revA (L.mem s') y = revA (L.mem s) y := fun y hy => by + obtain ⟨-, -, -, h24⟩ := hslot y hy; unfold revA; rw [h24] + have hC : ∀ c ∈ Z.C, σ'.kheap (scRenA m c) = σ.kheap (scRenA m c) := fun c hc => by + apply hspec_kh + · intro v hv heq + obtain ⟨xv, hkv, -, -⟩ := X.vcpu.core.obj v hv + obtain ⟨xc, n, hkc, -⟩ := X.j.1.sch.sc c hc + rw [heq] at hkv; rw [hkv] at hkc; cases hkc + · intro t ht heq + obtain ⟨xt, hkt, -, -⟩ := X.vcpu.core.tcb t ht + obtain ⟨xc, n, hkc, -⟩ := X.j.1.sch.sc c hc + rw [heq] at hkt; rw [hkt] at hkc; cases hkc + have hSt : StateRelA Z.th (L.mem s') σ' := by + refine ⟨fun p r hp => ?_, fun t ht => ?_⟩ + · obtain ⟨cs, hkc, hwf, hslots⟩ := X.j.1.st.cnode p r hp + have hne_v : ∀ v ∈ Z.V, specPtr v ≠ specPtr p := fun v hv e => by + obtain ⟨xv, hkv, -, -⟩ := X.vcpu.core.obj v hv; rw [e, hkc] at hkv; cases hkv + have hne_t : ∀ t ∈ Z.T, specPtr t ≠ specPtr p := fun t ht e => by + obtain ⟨xt, hkt, -, -⟩ := X.vcpu.core.tcb t ht; rw [e, hkc] at hkt; cases hkt + refine ⟨cs, by rw [hspec_kh _ hne_v hne_t]; exact hkc, hwf, fun idx hidx => ?_⟩ + have hsr := hslots idx hidx + have hsl : SlotWA Z.th (slotAddrA (specPtr p) (Fl4SpecA64.ofBl idx)) := + Or.inr ⟨p, r, hp, idx, hidx, rfl⟩ + obtain ⟨h0, h8, -, -⟩ := hslot _ hsl + unfold SlotRelA at hsr ⊢; unfold capAtA; rw [h0, h8]; exact hsr + · obtain ⟨x, hkt, hslots⟩ := X.j.1.st.tcb t ht + have htT : t ∈ Z.T := (X.j.1.thT t).1 ht + obtain ⟨x', hkt', -, -, -, -, hcm⟩ := hspec_tcb t htT x hkt + refine ⟨x', hkt', fun i hi => ?_⟩ + have hsr := hslots i hi + have hsl : IsSlotA Z.th (slotAddrA (specPtr t) (BitVec.ofNat 64 i)) := + ⟨t, Or.inr ⟨ht, i, hi, rfl⟩⟩ + obtain ⟨h0, h8, -, -⟩ := hslot _ (Or.inl hsl) + unfold SlotRelA at hsr ⊢; unfold capAtA; rw [h0, h8, hcm i hi]; exact hsr + have hCdt : CdtRelA st Z.th s' σ' Z.G := by + have h0 := X.j.1.cdt + refine ⟨h0.tree.keepO (fun y hy => hslot y (Or.inl hy)), ?_, ?_, ?_, h0.par_slot, ?_⟩ + · intro sl hsl; rw [hspec_cdt.1]; exact h0.cdt sl hsl + · intro sl q hq; rw [hspec_cdt.1] at hq; exact h0.cdt_slot sl q hq + · intro sl hsl + rw [hspec_cdt.2, hrev_sl _ (absSlot_w hsl)] + exact h0.orig sl hsl + · intro a' p' hp' + have hp_sl : SlotWA Z.th p' := by + obtain ⟨sl, hsl, hsl_a⟩ := h0.par_slot a' p' hp' + have hcdt := h0.cdt sl hsl + rw [hsl_a, hp'] at hcdt + cases hq : σ.cdt sl with + | none => rw [hq] at hcdt; cases hcdt + | some q => + rw [hq] at hcdt; simp only [Option.map_some, Option.some.injEq] at hcdt + rw [← hcdt]; exact absSlot_w (h0.cdt_slot sl q hq).2 + rw [hrev_sl p' hp_sl]; exact h0.prev a' p' hp' + have hSch : SchedRelA (L := L) Z.T Z.C Z.Cs Z.curOf Z.curScOf s' σ' m := by + have h0 := X.j.1.sch + have hnode' : ∀ c ∈ Z.Cs, + L.mem s' (L.nodeAt s' c layout.node.CUR_THREAD) = L.mem s (L.nodeAt s c layout.node.CUR_THREAD) ∧ + L.mem s' (L.nodeAt s' c layout.node.IDLE_THREAD) = L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) ∧ + L.mem s' (L.nodeAt s' c layout.node.CUR_SC) = L.mem s (L.nodeAt s c layout.node.CUR_SC) ∧ + L.mem s' (L.nodeAt s' c layout.node.IDLE_SC) = L.mem s (L.nodeAt s c layout.node.IDLE_SC) := by + intro c hc; simp only [nodeAt_ksb hksb]; exact hnode c hc + have hact : L.actionW s' = L.actionW s := by + unfold LawfulStore.actionW; rw [hcpu, nodeAt_ksb hksb]; exact hcur.1 + have htime : L.curTime s' = L.curTime s := by + unfold LawfulStore.curTime LawfulStore.nodeA; rw [hcpu, nodeAt_ksb hksb]; exact hcur.2.1 + exact { + core := by rw [hcpu]; exact h0.core + cpuCs := by rw [hcpu]; exact h0.cpuCs + curOfW := fun c hc => by rw [(hnode' c hc).1]; exact h0.curOfW c hc + curScOfW := fun c hc => by rw [(hnode' c hc).2.2.1]; exact h0.curScOfW c hc + cur := by rw [hspec_glob.cur]; exact h0.cur + csc := by rw [hspec_glob.csc]; exact h0.csc + idle := by unfold LawfulStore.idleW; rw [hspec_glob.idle, hcpu, nodeAt_ksb hksb, (hnode _ h0.cpuCs).2.1]; exact h0.idle + idleOf := fun c hc => by rw [(hnode' c hc).2.1]; exact h0.idleOf c hc + idleScOf := fun c hc => by rw [(hnode' c hc).2.2.2]; exact h0.idleScOf c hc + act := by rw [hspec_glob.act, hact]; exact h0.act + time := by rw [hspec_glob.time, htime]; exact h0.time + aff := fun t ht => by rw [(htcb t ht).2.2.1]; exact h0.aff t ht + affT := h0.affT + tcb := fun t ht => by + obtain ⟨x, hkt, hst, hsc0⟩ := h0.tcb t ht + obtain ⟨x', hkt', h1, h2, -⟩ := hspec_tcb t ht x hkt + exact ⟨x', hkt', by rw [(htcb t ht).1, h1]; exact hst, by rw [(htcb t ht).2.1, h2]; exact hsc0⟩ + sc := fun c hc => by rw [hC c hc, (hsc c hc).1]; exact h0.sc c hc + scOff := h0.scOff } + have hIrq : IrqRelA (L.mem s') (L.ksb s') σ' := by + have h0 := X.j.1.irq.of_spec hspec_glob + exact { + st := fun i hi => by rw [hksb, hirq i hi]; exact h0.st i hi + node := by rw [hksb]; exact h0.node + nodeHi := by rw [hksb]; exact h0.nodeHi } + have hRest : SchedRestA (L := L) Z s' σ' m := by + have h0 := X.j.1.rest + exact { + rq := fun p hp => by rw [hspec_glob.rq, hcpu]; exact h0.rq p hp + rqDom := fun d p hd => by rw [hspec_glob.rq]; exact h0.rqDom d p hd + rel := by rw [hspec_glob.rls, hcpu]; exact h0.rel + consumed := by rw [hspec_glob.cons, hcpu, nodeAt_ksb hksb, hcur.2.2.1]; exact h0.consumed + repr := by rw [hspec_glob.repr, hcpu, nodeAt_ksb hksb, hcur.2.2.2]; exact h0.repr + curDom := by rw [hspec_glob.dom, hksb, hglob.1]; exact h0.curDom + domTime := by rw [hspec_glob.domT, hksb, hglob.2]; exact h0.domTime + prio := fun t ht => by + obtain ⟨x, hkt, h1, h2, h3⟩ := h0.prio t ht + obtain ⟨x', hkt', -, -, hdom, hpr, -⟩ := hspec_tcb t ht x hkt + exact ⟨x', hkt', by rw [hdom]; exact h1, by rw [(htcb t ht).2.2.2.2]; exact h2, by rw [(htcb t ht).2.2.2.1, hpr]; exact h3⟩ + scMem := fun t ht hnz => by rw [(htcb t ht).2.1] at hnz ⊢; exact h0.scMem t ht hnz + refillMax := fun c hc => by rw [hC c hc, (hsc c hc).2]; exact h0.refillMax c hc } + have hVs_kh : ∀ p, (∃ q ∈ Z.Pp, specPtr q = p) ∨ (∃ q ∈ Z.S1, specPtr q = p) ∨ (∃ q ∈ Z.Y, specPtr q = p) → + σ'.kheap p = σ.kheap p := by + intro p hp; apply hspec_kh + · intro v hv heq + obtain ⟨xv, hkv, -, -⟩ := X.vcpu.core.obj v hv + rcases hp with ⟨q, hq, rfl⟩ | ⟨q, hq, rfl⟩ | ⟨q, hq, rfl⟩ + · obtain ⟨ap, hkp, -⟩ := X.j.1.vs.pool q hq; rw [heq] at hkv; rw [hkv] at hkp; cases hkp + · obtain ⟨pt, hkp, -⟩ := X.j.1.vs.pt q hq; rw [heq] at hkv; rw [hkv] at hkp; cases hkp + · obtain ⟨hv0, hkp⟩ := X.j.2.2.2.obj q hq; rw [heq] at hkv; rw [hkv] at hkp; cases hkp + · intro t ht heq + obtain ⟨xt, hkt, -, -⟩ := X.vcpu.core.tcb t ht + rcases hp with ⟨q, hq, rfl⟩ | ⟨q, hq, rfl⟩ | ⟨q, hq, rfl⟩ + · obtain ⟨ap, hkp, -⟩ := X.j.1.vs.pool q hq; rw [heq] at hkt; rw [hkt] at hkp; cases hkp + · obtain ⟨pt, hkp, -⟩ := X.j.1.vs.pt q hq; rw [heq] at hkt; rw [hkt] at hkp; cases hkp + · obtain ⟨hv0, hkp⟩ := X.j.2.2.2.obj q hq; rw [heq] at hkt; rw [hkt] at hkp; cases hkp + have hVs : VsRelA Z.Pp Z.S1 Z.Lv Z.X Z.Y (L.mem s') (L.ksb s') σ' := by + rw [hksb] + exact (X.j.1.vs.of_spec hspec_asid hVs_kh).of_mem hvs + have hExt_kh : ∀ q, q ∈ Z.Pp ∨ q ∈ Z.X ∨ q ∈ Z.Y ∨ q ∈ Z.T2 → σ'.kheap (specPtr q) = σ.kheap (specPtr q) := by + intro q hq; apply hspec_kh + · intro v hv heq + obtain ⟨xv, hkv, -, -⟩ := X.vcpu.core.obj v hv + rcases hq with hq | hq | hq | hq + · obtain ⟨ap, hkp, -⟩ := X.j.1.vs.pool q hq; rw [heq] at hkv; rw [hkv] at hkp; cases hkp + · obtain ⟨es, hkp⟩ := X.j.2.2.1.obj q hq; rw [heq] at hkv; rw [hkv] at hkp; cases hkp + · obtain ⟨l1, hkp⟩ := X.j.2.2.2.obj q hq; rw [heq] at hkv; rw [hkv] at hkp; cases hkp + · obtain ⟨pt, hkp, -⟩ := X.j.1.ext.s2pt q hq; rw [heq] at hkv; rw [hkv] at hkp; cases hkp + · intro t ht heq + obtain ⟨xt, hkt, -, -⟩ := X.vcpu.core.tcb t ht + rcases hq with hq | hq | hq | hq + · obtain ⟨ap, hkp, -⟩ := X.j.1.vs.pool q hq; rw [heq] at hkt; rw [hkt] at hkp; cases hkp + · obtain ⟨es, hkp⟩ := X.j.2.2.1.obj q hq; rw [heq] at hkt; rw [hkt] at hkp; cases hkp + · obtain ⟨l1, hkp⟩ := X.j.2.2.2.obj q hq; rw [heq] at hkt; rw [hkt] at hkp; cases hkp + · obtain ⟨pt, hkp, -⟩ := X.j.1.ext.s2pt q hq; rw [heq] at hkt; rw [hkt] at hkp; cases hkp + have hExt : ExtVsRelA Z.Pp Z.S1 Z.X Z.Y Z.T2 Z.Sh2 (L.mem s') σ' := + (X.j.1.ext.of_spec hspec_asid (fun q hq => hExt_kh q (by rcases hq with h | h | h <;> simp [h]))).of_mem hext + have hJ : JAtZA (L := L) AR Z s' a σ' m := + { st := hSt, cdt := hCdt, sch := hSch, irq := hIrq, rest := hRest, ar := X.j.1.ar, + place := by rw [hksb]; exact X.j.1.place, + thT := X.j.1.thT, csN := X.j.1.csN, + idleT := fun c hc => by simp only [nodeAt_ksb hksb]; rw [(hnode c hc).2.1]; exact X.j.1.idleT c hc, + idleC := fun c hc => by simp only [nodeAt_ksb hksb]; rw [(hnode c hc).2.2.2]; exact X.j.1.idleC c hc, + curT := fun c hc => by simp only [nodeAt_ksb hksb]; rw [(hnode c hc).1]; exact X.j.1.curT c hc, + cscC := fun c hc => by simp only [nodeAt_ksb hksb]; rw [(hnode c hc).2.2.1]; exact X.j.1.cscC c hc, + ksFit := by rw [hksb]; exact X.j.1.ksFit, + scOffD := X.j.1.scOffD, + cv := fun y hy => by rw [hcap_sl y (Or.inl hy)]; exact X.j.1.cv y hy, + pml := fun y hy p mp asid hc => by + rw [hcap_sl y (Or.inl hy)] at hc; exact X.j.1.pml y hy p mp asid hc, + vs := hVs, ext := hExt } + have hS2 : Stage2CoreA (L := L) Z s' σ' := + ⟨fun r hr => by rw [hExt_kh r (Or.inr (Or.inl hr))]; exact X.j.2.2.1.obj r hr, + fun p es h => X.j.2.2.1.back p es (hspec_s2 p es h), + fun y hy p l mp asid va hc => by + rw [hcap_sl y (Or.inl hy)] at hc; exact X.j.2.2.1.cap y hy p l mp asid va hc⟩ + have hHV : HighVSpaceCoreA (L := L) Z s' σ' := + ⟨fun r hr => by rw [hExt_kh r (Or.inr (Or.inr (Or.inl hr)))]; exact X.j.2.2.2.obj r hr, + fun p l1 h => X.j.2.2.2.back p l1 (hspec_hv p l1 h), + fun y hy p mp asid hc => by + rw [hcap_sl y (Or.inl hy)] at hc; exact X.j.2.2.2.cap y hy p mp asid hc⟩ + have hTr : TreeA st OS Z.G.chains Z.G.par s' := X.tr.keepO hos + have hCar : CarryA Z (L.mem s') σ' := + ⟨X.car.1.of_capsA rfl (fun _ h => h) (fun _ h => h) (fun y hy => Or.inl (hcap_os y (hsub y hy))), + X.car.2.of_keepA hspec_ty.1 hspec_ty.2 fun y hy => by + rw [hcap_os y ((X.tr.lk.members y).1 hy).1]; exact CapFinEqA.refl _⟩ + have hF : FrameMemRelA Z OS Ub (L.ksb s') (L.mem s') σ'.machineState.underlyingMemory := by + refine ⟨X.fmem.aligned, fun b hb => by rw [hub b hb, hspec_um]; exact X.fmem.agree b hb, + fun y hy ptr sz r mp s2 asid va hc => by + rw [hcap_os y hy] at hc; exact X.fmem.frames y hy ptr sz r mp s2 asid va hc, + X.fmem.disj_os, X.fmem.disj_slots, X.fmem.disj_tcb, X.fmem.disj_sc, + by rw [hksb]; exact X.fmem.disj_ks⟩ + exact ⟨hJ, hS2, hHV, hTr, hCar, hF⟩ + +/-- Helper: `VcPlacedA.disj_ks` in `≠ wa (L.ksb s) o` form. -/ +theorem vc_disj_ksA {Z : XSetsA} {OS Ub : Set U64} {ksb : U64} + (hpl : VcPlacedA Z OS Ub ksb) (hfit : ksb.val + ksSizeA ≤ 2 ^ 64) + {b : U64} (hvp : VcPage Z.V b) (o : U64) (ho : o.val < ksSizeA) : + b ≠ wa ksb o := fun e => + hpl.disj_ks b hvp (by rw [e, wa_valP, Nat.mod_eq_of_lt (by omega)]; omega) + +/-- Helper: `VcPlacedA.disj_ub` in `≠ b` form. -/ +theorem vc_disj_ubA {Z : XSetsA} {OS Ub : Set U64} {ksb : U64} + (hpl : VcPlacedA Z OS Ub ksb) {v : U64} (hvp : VcPage Z.V v) {b : U64} (hb : b ∈ Ub) : + v ≠ b := fun e => hpl.disj_ub v hvp (e ▸ hb) + +/-- `layout.tcb.RUN_VCPU = ok 3672#u64`. -/ +theorem layout_tcb_run_vcpu_eqA : layout.tcb.RUN_VCPU = ok 3672#u64 := + eq_of_spec_fd Fl4ProofsA64.P0.Consts.layout.tcb.RUN_VCPU.spec + +/-- Helper: a single aligned write to a VCPU header word (`wa vc o`, `o < 4096`, `o % 8 = 0`) +preserves every non-VCPU-link footprint word, every `OS` word, every `Ub` word, and every TCB +user-register word. -/ +private theorem vcHdr_wr_preservesA {AR : A → HwRootA → Prop} {Z : XSetsA} + {OS Ub : Set U64} {m : Fl4SpecA64.McState} {a : A} {s : S} {σ : State Unit} + (X : VcpuXferStateA (L := L) AR Z OS Ub m a s σ) + (hU : UbFootDisjA Z OS Ub) + (hCs : ∀ c ∈ Z.Cs, c.val < 128) (hcpu : (L.cpu s).val < 128) + (hExt : ∀ b, VcPage Z.V b → ¬ ExtFootE Z.X Z.Y Z.T2 b) + (hSw : ∀ b, VcPage Z.V b → ∀ y, SlotWA Z.th y → + b ≠ y ∧ b ≠ wa y 8#u64 ∧ b ≠ wa y 16#u64 ∧ b ≠ wa y 24#u64) + (hfa : ∀ q, FootA (L := L) Z s q → Aligned q) + {vc o : U64} (hvc : vc ∈ Z.V) (ho : o.val < 4096) (ho8 : o.val % 8 = 0) (val : U64) : + let s' := L.wr s (wa vc o) val + (∀ y, SlotWA Z.th y → + L.mem s' y = L.mem s y ∧ L.mem s' (wa y 8#u64) = L.mem s (wa y 8#u64) ∧ + L.mem s' (wa y 16#u64) = L.mem s (wa y 16#u64) ∧ L.mem s' (wa y 24#u64) = L.mem s (wa y 24#u64)) ∧ + (∀ y ∈ OS, + L.mem s' y = L.mem s y ∧ L.mem s' (wa y 8#u64) = L.mem s (wa y 8#u64) ∧ + L.mem s' (wa y 16#u64) = L.mem s (wa y 16#u64) ∧ L.mem s' (wa y 24#u64) = L.mem s (wa y 24#u64)) ∧ + (∀ c ∈ Z.Cs, + L.mem s' (L.nodeAt s c layout.node.CUR_THREAD) = L.mem s (L.nodeAt s c layout.node.CUR_THREAD) ∧ + L.mem s' (L.nodeAt s c layout.node.IDLE_THREAD) = L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) ∧ + L.mem s' (L.nodeAt s c layout.node.CUR_SC) = L.mem s (L.nodeAt s c layout.node.CUR_SC) ∧ + L.mem s' (L.nodeAt s c layout.node.IDLE_SC) = L.mem s (L.nodeAt s c layout.node.IDLE_SC)) ∧ + (∀ t ∈ Z.T, + L.mem s' (stA t) = L.mem s (stA t) ∧ L.mem s' (scA t) = L.mem s (scA t) ∧ + L.mem s' (affA t) = L.mem s (affA t) ∧ L.mem s' (prioA t) = L.mem s (prioA t) ∧ + L.mem s' (domA t) = L.mem s (domA t)) ∧ + (∀ c ∈ Z.C, L.mem s' (wa c 24#u64) = L.mem s (wa c 24#u64) ∧ + L.mem s' (wa c 72#u64) = L.mem s (wa c 72#u64)) ∧ + (L.mem s' (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) = L.mem s (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) ∧ + L.mem s' (L.nodeAt s (L.cpu s) layout.node.CUR_TIME) = L.mem s (L.nodeAt s (L.cpu s) layout.node.CUR_TIME) ∧ + L.mem s' (L.nodeAt s (L.cpu s) layout.node.CONSUMED) = L.mem s (L.nodeAt s (L.cpu s) layout.node.CONSUMED) ∧ + L.mem s' (L.nodeAt s (L.cpu s) layout.node.REPROGRAM) = L.mem s (L.nodeAt s (L.cpu s) layout.node.REPROGRAM)) ∧ + (L.mem s' (wa (L.ksb s) 8#u64) = L.mem s (wa (L.ksb s) 8#u64) ∧ + L.mem s' (wa (L.ksb s) 16#u64) = L.mem s (wa (L.ksb s) 16#u64)) ∧ + (∀ i : U64, i.val < 5376 → L.mem s' (irqWordA (L.ksb s) i) = L.mem s (irqWordA (L.ksb s) i)) ∧ + (∀ b, VsFootA Z.Pp Z.S1 Z.Y (L.ksb s) b → L.mem s' b = L.mem s b) ∧ + (∀ b, ExtFootE Z.X Z.Y Z.T2 b → L.mem s' b = L.mem s b) ∧ + (∀ b ∈ Ub, L.mem s' b = L.mem s b) ∧ + (∀ t ∈ Z.T, ∀ r : Register, L.mem s' (Endp.regA t (regIdxA r)) = L.mem s (Endp.regA t (regIdxA r))) := by + have hvp : VcPage Z.V (wa vc o) := vcpuHdr_in_vcPage hvc ho ho8 + have hal_w : Aligned (wa vc o) := X.vcpu.place.vcPage_al hvp + have hksFit := X.j.1.ksFit + have hnd : ∀ c : U64, c.val < 128 → ∀ k : U64, k.val < 128 → k.val % 8 = 0 → + L.mem (L.wr s (wa vc o) val) (L.nodeAt s c k) = L.mem s (L.nodeAt s c k) := fun c hc k hk hk8 => + mem_wr_neA hal_w (X.vcpu.place.node_al c k hk8) (X.vcpu.place.disj_node hksFit hvp hc (by omega)) + refine ⟨?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_⟩ + · intro y hy + obtain ⟨h0, h8, h16, h24⟩ := hSw _ hvp y hy + exact ⟨mem_wr_neA hal_w (hfa _ (.slot y _ hy (Or.inl rfl))) h0, + mem_wr_neA hal_w (hfa _ (.slot y _ hy (Or.inr (Or.inl rfl)))) h8, + mem_wr_neA hal_w (hfa _ (.slot y _ hy (Or.inr (Or.inr (Or.inl rfl))))) h16, + mem_wr_neA hal_w (hfa _ (.slot y _ hy (Or.inr (Or.inr (Or.inr rfl))))) h24⟩ + · intro y hy + have h0 := X.vcpu.place.disj_os _ hvp y hy + have hya : Aligned y := by + -- `OS` slot words are 8-aligned via `hU` or `X.tr` + have := (hU.al_os y hy) + exact this + exact ⟨mem_wr_neA hal_w hya h0.1, + mem_wr_neA hal_w (aligned_wa hya 8#u64 (by decide)) h0.2.1, + mem_wr_neA hal_w (aligned_wa hya 16#u64 (by decide)) h0.2.2.1, + mem_wr_neA hal_w (aligned_wa hya 24#u64 (by decide)) h0.2.2.2⟩ + · intro c hc + exact ⟨hnd c (hCs c hc) layout.node.CUR_THREAD (by unfold layout.node.CUR_THREAD; decide) (by unfold layout.node.CUR_THREAD; decide), + hnd c (hCs c hc) layout.node.IDLE_THREAD (by unfold layout.node.IDLE_THREAD; decide) (by unfold layout.node.IDLE_THREAD; decide), + hnd c (hCs c hc) layout.node.CUR_SC (by unfold layout.node.CUR_SC; decide) (by unfold layout.node.CUR_SC; decide), + hnd c (hCs c hc) layout.node.IDLE_SC (by unfold layout.node.IDLE_SC; decide) (by unfold layout.node.IDLE_SC; decide)⟩ + · intro t ht + have hta := X.vcpu.place.al_t t ht + refine ⟨mem_wr_neA hal_w (by rw [stA_eq]; exact aligned_wa hta 3328#u64 (by decide)) + (by rw [stA_eq]; exact X.vcpu.place.disj_tcb vc hvc t ht o 3328#u64 ho (by decide)), + mem_wr_neA hal_w (by unfold scA; exact aligned_wa hta 3464#u64 (by decide)) + (by unfold scA; exact X.vcpu.place.disj_tcb vc hvc t ht o 3464#u64 ho (by decide)), + mem_wr_neA hal_w (by unfold affA; exact aligned_wa hta 3520#u64 (by decide)) + (by unfold affA; exact X.vcpu.place.disj_tcb vc hvc t ht o 3520#u64 ho (by decide)), + mem_wr_neA hal_w (hfa _ (.nslot _ (.tcb2 t _ ht (Or.inl rfl)))) + (by unfold prioA; exact X.vcpu.place.disj_tcb vc hvc t ht o 3448#u64 ho (by decide)), + mem_wr_neA hal_w (hfa _ (.nslot _ (.tcb2 t _ ht (Or.inr rfl)))) + (by unfold domA; exact X.vcpu.place.disj_tcb vc hvc t ht o 3432#u64 ho (by decide))⟩ + · intro c hc + exact ⟨mem_wr_neA hal_w (hfa _ (.nslot _ (.sc c hc))) + (X.vcpu.place.disj_sc vc hvc c hc o 24#u64 ho (by decide)), + mem_wr_neA hal_w (hfa _ (.nslot _ (.sc2 c hc))) + (X.vcpu.place.disj_sc vc hvc c hc o 72#u64 ho (by decide))⟩ + · exact ⟨hnd (L.cpu s) hcpu layout.node.SCHED_ACTION (by unfold layout.node.SCHED_ACTION; decide) (by unfold layout.node.SCHED_ACTION; decide), + hnd (L.cpu s) hcpu layout.node.CUR_TIME (by unfold layout.node.CUR_TIME; decide) (by unfold layout.node.CUR_TIME; decide), + hnd (L.cpu s) hcpu layout.node.CONSUMED (by unfold layout.node.CONSUMED; decide) (by unfold layout.node.CONSUMED; decide), + hnd (L.cpu s) hcpu layout.node.REPROGRAM (by unfold layout.node.REPROGRAM; decide) (by unfold layout.node.REPROGRAM; decide)⟩ + · exact ⟨mem_wr_neA hal_w (hfa _ (.nslot _ (.glob _ (Or.inl rfl)))) + (vc_disj_ksA X.vcpu.place X.j.1.ksFit hvp 8#u64 (by unfold ksSizeA; decide)), + mem_wr_neA hal_w (hfa _ (.nslot _ (.glob _ (Or.inr rfl)))) + (vc_disj_ksA X.vcpu.place X.j.1.ksFit hvp 16#u64 (by unfold ksSizeA; decide))⟩ + · intro i hi + exact mem_wr_neA hal_w (hfa _ (.nslot _ (.irq i hi))) + (vc_disj_ksA X.vcpu.place X.j.1.ksFit hvp (irqOffA i) (by + unfold irqOffA ksSizeA + simp only [core.num.U64.wrapping_add_val_eq, core.num.U64.wrapping_mul_val_eq, + UScalar.size, UScalarTy.numBits] + have h16 : (224#u64).val = 224 := rfl + have h8 : (8#u64).val = 8 := rfl + rw [h16, h8]; omega)) + · intro b hb + exact mem_wr_neA hal_w (hfa _ (.nslot _ (.vs b hb))) + (fun e => X.vcpu.place.disj_vs _ hvp (e ▸ hb)) + · intro b hb + exact mem_wr_neA hal_w (hfa _ (.nslot _ (.ext b hb))) + (fun e => hExt _ hvp (e ▸ hb)) + · intro b hb + exact mem_wr_neA hal_w (X.fmem.aligned b hb) + (vc_disj_ubA X.vcpu.place hvp hb) + · intro t ht r + obtain ⟨hKval, hreg_eq⟩ := regA_eq_wa_KA t r + have hlt := regIdxA_lt r + have hta := hU.tcb_al t ht + have hal_r : Aligned (Endp.regA t (regIdxA r)) := by + rw [hreg_eq]; exact aligned_wa (by unfold Aligned; have := hta.align; omega) _ (by omega) + exact mem_wr_neA hal_w hal_r (by + rw [hreg_eq]; exact X.vcpu.place.disj_tcb vc hvc t ht o _ ho (by omega)) + +/-- Helper: a single aligned write to a TCB VCPU link word (`wa t K`, `K ∈ {3664, 3672}`) +preserves every non-VCPU-link footprint word, every `OS` word, every `Ub` word, every VCPU page +word, and every TCB user-register word. -/ +private theorem tcbLink_wr_preservesA {AR : A → HwRootA → Prop} {Z : XSetsA} + {OS Ub : Set U64} {m : Fl4SpecA64.McState} {a : A} {s : S} {σ : State Unit} + (X : VcpuXferStateA (L := L) AR Z OS Ub m a s σ) + (hU : UbFootDisjA Z OS Ub) (hLD : TcbLinkDisjA Z (L.ksb s)) + (hCs : ∀ c ∈ Z.Cs, c.val < 128) (hcpu : (L.cpu s).val < 128) + (hfa : ∀ q, FootA (L := L) Z s q → Aligned q) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hOS_tcb : ∀ t ∈ Z.T, ∀ K : U64, (K = 3664#u64 ∨ K = 3672#u64) → ∀ y ∈ OS, + wa t K ≠ y ∧ wa t K ≠ wa y 8#u64 ∧ wa t K ≠ wa y 16#u64 ∧ wa t K ≠ wa y 24#u64) + {t K : U64} (ht : t ∈ Z.T) (hK : K = 3664#u64 ∨ K = 3672#u64) (val : U64) : + let s' := L.wr s (wa t K) val + (∀ y, SlotWA Z.th y → + L.mem s' y = L.mem s y ∧ L.mem s' (wa y 8#u64) = L.mem s (wa y 8#u64) ∧ + L.mem s' (wa y 16#u64) = L.mem s (wa y 16#u64) ∧ L.mem s' (wa y 24#u64) = L.mem s (wa y 24#u64)) ∧ + (∀ y ∈ OS, + L.mem s' y = L.mem s y ∧ L.mem s' (wa y 8#u64) = L.mem s (wa y 8#u64) ∧ + L.mem s' (wa y 16#u64) = L.mem s (wa y 16#u64) ∧ L.mem s' (wa y 24#u64) = L.mem s (wa y 24#u64)) ∧ + (∀ c ∈ Z.Cs, + L.mem s' (L.nodeAt s c layout.node.CUR_THREAD) = L.mem s (L.nodeAt s c layout.node.CUR_THREAD) ∧ + L.mem s' (L.nodeAt s c layout.node.IDLE_THREAD) = L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) ∧ + L.mem s' (L.nodeAt s c layout.node.CUR_SC) = L.mem s (L.nodeAt s c layout.node.CUR_SC) ∧ + L.mem s' (L.nodeAt s c layout.node.IDLE_SC) = L.mem s (L.nodeAt s c layout.node.IDLE_SC)) ∧ + (∀ u ∈ Z.T, + L.mem s' (stA u) = L.mem s (stA u) ∧ L.mem s' (scA u) = L.mem s (scA u) ∧ + L.mem s' (affA u) = L.mem s (affA u) ∧ L.mem s' (prioA u) = L.mem s (prioA u) ∧ + L.mem s' (domA u) = L.mem s (domA u)) ∧ + (∀ c ∈ Z.C, L.mem s' (wa c 24#u64) = L.mem s (wa c 24#u64) ∧ + L.mem s' (wa c 72#u64) = L.mem s (wa c 72#u64)) ∧ + (L.mem s' (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) = L.mem s (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) ∧ + L.mem s' (L.nodeAt s (L.cpu s) layout.node.CUR_TIME) = L.mem s (L.nodeAt s (L.cpu s) layout.node.CUR_TIME) ∧ + L.mem s' (L.nodeAt s (L.cpu s) layout.node.CONSUMED) = L.mem s (L.nodeAt s (L.cpu s) layout.node.CONSUMED) ∧ + L.mem s' (L.nodeAt s (L.cpu s) layout.node.REPROGRAM) = L.mem s (L.nodeAt s (L.cpu s) layout.node.REPROGRAM)) ∧ + (L.mem s' (wa (L.ksb s) 8#u64) = L.mem s (wa (L.ksb s) 8#u64) ∧ + L.mem s' (wa (L.ksb s) 16#u64) = L.mem s (wa (L.ksb s) 16#u64)) ∧ + (∀ i : U64, i.val < 5376 → L.mem s' (irqWordA (L.ksb s) i) = L.mem s (irqWordA (L.ksb s) i)) ∧ + (∀ b, VsFootA Z.Pp Z.S1 Z.Y (L.ksb s) b → L.mem s' b = L.mem s b) ∧ + (∀ b, ExtFootE Z.X Z.Y Z.T2 b → L.mem s' b = L.mem s b) ∧ + (∀ b ∈ Ub, L.mem s' b = L.mem s b) ∧ + (∀ u ∈ Z.T, ∀ r : Register, L.mem s' (Endp.regA u (regIdxA r)) = L.mem s (Endp.regA u (regIdxA r))) ∧ + (∀ v ∈ Z.V, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → L.mem s' (wa v o) = L.mem s (wa v o)) := by + have hta := hU.tcb_al t ht + have hKlt : K.val < 4096 := by rcases hK with rfl | rfl <;> decide + have hK8 : K.val % 8 = 0 := by rcases hK with rfl | rfl <;> decide + have hal_w : Aligned (wa t K) := aligned_wa (by unfold Aligned; have := hta.align; omega) K hK8 + have hnd : ∀ c : U64, c.val < 128 → ∀ k : U64, k.val < 128 → k.val % 8 = 0 → + L.mem (L.wr s (wa t K) val) (L.nodeAt s c k) = L.mem s (L.nodeAt s c k) := fun c hc k hk hk8 => by + obtain ⟨o, ho, heq⟩ := nodeAt_eq_wa_ksbA (L := L) s c k hc (by omega) + exact mem_wr_neA hal_w (X.vcpu.place.node_al c k hk8) (by rw [heq]; exact hLD.offKs t ht K hK o ho) + refine ⟨?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_⟩ + · intro y hy + obtain ⟨h0, h8, h16, h24⟩ := hLD.offSlot t ht K hK y hy + exact ⟨mem_wr_neA hal_w (hfa _ (.slot y _ hy (Or.inl rfl))) h0, + mem_wr_neA hal_w (hfa _ (.slot y _ hy (Or.inr (Or.inl rfl)))) h8, + mem_wr_neA hal_w (hfa _ (.slot y _ hy (Or.inr (Or.inr (Or.inl rfl))))) h16, + mem_wr_neA hal_w (hfa _ (.slot y _ hy (Or.inr (Or.inr (Or.inr rfl))))) h24⟩ + · intro y hy + obtain ⟨h0, h8, h16, h24⟩ := hOS_tcb t ht K hK y hy + have hya : Aligned y := hU.al_os y hy + exact ⟨mem_wr_neA hal_w hya h0, + mem_wr_neA hal_w (aligned_wa hya 8#u64 (by decide)) h8, + mem_wr_neA hal_w (aligned_wa hya 16#u64 (by decide)) h16, + mem_wr_neA hal_w (aligned_wa hya 24#u64 (by decide)) h24⟩ + · intro c hc + exact ⟨hnd c (hCs c hc) layout.node.CUR_THREAD (by unfold layout.node.CUR_THREAD; decide) (by unfold layout.node.CUR_THREAD; decide), + hnd c (hCs c hc) layout.node.IDLE_THREAD (by unfold layout.node.IDLE_THREAD; decide) (by unfold layout.node.IDLE_THREAD; decide), + hnd c (hCs c hc) layout.node.CUR_SC (by unfold layout.node.CUR_SC; decide) (by unfold layout.node.CUR_SC; decide), + hnd c (hCs c hc) layout.node.IDLE_SC (by unfold layout.node.IDLE_SC; decide) (by unfold layout.node.IDLE_SC; decide)⟩ + · intro u hu + have hua := hU.tcb_al u hu + refine ⟨mem_wr_neA hal_w (by rw [stA_eq]; exact aligned_wa (X.vcpu.place.al_t u hu) 3328#u64 (by decide)) + (by rw [stA_eq]; exact field_ne hta hua hKlt (by decide) (fun _ => by rcases hK with rfl | rfl <;> decide)), + mem_wr_neA hal_w (by unfold scA; exact aligned_wa (X.vcpu.place.al_t u hu) 3464#u64 (by decide)) + (by unfold scA; exact field_ne hta hua hKlt (by decide) (fun _ => by rcases hK with rfl | rfl <;> decide)), + mem_wr_neA hal_w (by unfold affA; exact aligned_wa (X.vcpu.place.al_t u hu) 3520#u64 (by decide)) + (by unfold affA; exact field_ne hta hua hKlt (by decide) (fun _ => by rcases hK with rfl | rfl <;> decide)), + mem_wr_neA hal_w (hfa _ (.nslot _ (.tcb2 u _ hu (Or.inl rfl)))) + (by unfold prioA; exact field_ne hta hua hKlt (by decide) (fun _ => by rcases hK with rfl | rfl <;> decide)), + mem_wr_neA hal_w (hfa _ (.nslot _ (.tcb2 u _ hu (Or.inr rfl)))) + (by unfold domA; exact field_ne hta hua hKlt (by decide) (fun _ => by rcases hK with rfl | rfl <;> decide))⟩ + · intro c hc + obtain ⟨h24, h32⟩ := hLD.offSc t ht K hK c hc + exact ⟨mem_wr_neA hal_w (hfa _ (.nslot _ (.sc c hc))) h24, + mem_wr_neA hal_w (hfa _ (.nslot _ (.sc2 c hc))) h32⟩ + · exact ⟨hnd (L.cpu s) hcpu layout.node.SCHED_ACTION (by unfold layout.node.SCHED_ACTION; decide) (by unfold layout.node.SCHED_ACTION; decide), + hnd (L.cpu s) hcpu layout.node.CUR_TIME (by unfold layout.node.CUR_TIME; decide) (by unfold layout.node.CUR_TIME; decide), + hnd (L.cpu s) hcpu layout.node.CONSUMED (by unfold layout.node.CONSUMED; decide) (by unfold layout.node.CONSUMED; decide), + hnd (L.cpu s) hcpu layout.node.REPROGRAM (by unfold layout.node.REPROGRAM; decide) (by unfold layout.node.REPROGRAM; decide)⟩ + · exact ⟨mem_wr_neA hal_w (hfa _ (.nslot _ (.glob _ (Or.inl rfl)))) + (hLD.offKs t ht K hK 8#u64 (by unfold ksSizeA; decide)), + mem_wr_neA hal_w (hfa _ (.nslot _ (.glob _ (Or.inr rfl)))) + (hLD.offKs t ht K hK 16#u64 (by unfold ksSizeA; decide))⟩ + · intro i hi + exact mem_wr_neA hal_w (hfa _ (.nslot _ (.irq i hi))) + (hLD.offKs t ht K hK (irqOffA i) (by + unfold irqOffA ksSizeA + simp only [core.num.U64.wrapping_add_val_eq, core.num.U64.wrapping_mul_val_eq, + UScalar.size, UScalarTy.numBits] + have h16 : (224#u64).val = 224 := rfl + have h8 : (8#u64).val = 8 := rfl + rw [h16, h8]; omega)) + · intro b hb + exact mem_wr_neA hal_w (hfa _ (.nslot _ (.vs b hb))) + (fun e => hLD.offVs t ht K hK (e ▸ hb)) + · intro b hb + exact mem_wr_neA hal_w (hfa _ (.nslot _ (.ext b hb))) + (fun e => hLD.offExt t ht K hK (e ▸ hb)) + · intro b hb + exact mem_wr_neA hal_w (X.fmem.aligned b hb) + (fun e => X.fmem.disj_tcb b hb t ht K hKlt e.symm) + · intro u hu r + obtain ⟨hKval, hreg_eq⟩ := regA_eq_wa_KA u r + have hlt := regIdxA_lt r + have hua := hU.tcb_al u hu + have hal_r : Aligned (Endp.regA u (regIdxA r)) := by + rw [hreg_eq]; exact aligned_wa (by unfold Aligned; have := hua.align; omega) _ (by omega) + exact mem_wr_neA hal_w hal_r (by + rw [hreg_eq]; exact field_ne hta hua hKlt (by omega) (fun _ => by rcases hK with rfl | rfl <;> (have : (3664#u64).val = 3664 := rfl; have : (3672#u64).val = 3672 := rfl; omega))) + · intro v hv o ho ho8 + exact mem_wr_neA hal_w (aligned_wa (X.vcpu.place.vc_al hv) o ho8) + (X.vcpu.place.disj_tcb v hv t ht o K ho hKlt).symm + +/-! ## Section 2: Lockstep Run-Link Clear (`cancel_run` / `complete_run` / `unbind` stale) -/ + +/-- Clearing `vc.RUN_CALLER := 0` and `ct.RUN_VCPU := 0` in lockstep with +`fl4VcpuExtUpdate (specPtr vc) (runCaller := none) >> setRunVcpu (specPtr ct) none` +preserves `VcpuXferStateA`. -/ +theorem VcpuXferStateA.clear_run_link {AR : A → HwRootA → Prop} {Z : XSetsA} + {OS Ub : Set U64} {m : Fl4SpecA64.McState} {a : A} {s : S} {σ : State Unit} + (X : VcpuXferStateA (L := L) AR Z OS Ub m a s σ) + (hU : UbFootDisjA Z OS Ub) (hLD : TcbLinkDisjA Z (L.ksb s)) + (hCs : ∀ c ∈ Z.Cs, c.val < 128) (hcpu : (L.cpu s).val < 128) + (hExt : ∀ b, VcPage Z.V b → ¬ ExtFootE Z.X Z.Y Z.T2 b) + (hSw : ∀ b, VcPage Z.V b → ∀ y, SlotWA Z.th y → + b ≠ y ∧ b ≠ wa y 8#u64 ∧ b ≠ wa y 16#u64 ∧ b ≠ wa y 24#u64) + (hfa : ∀ q, FootA (L := L) Z s q → Aligned q) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hIdleT : ∀ c ∈ Z.Cs, L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) ∈ Z.T) + (hOS_tcb : ∀ t ∈ Z.T, ∀ K : U64, (K = 3664#u64 ∨ K = 3672#u64) → ∀ y ∈ OS, + wa t K ≠ y ∧ wa t K ≠ wa y 8#u64 ∧ wa t K ≠ wa y 16#u64 ∧ wa t K ≠ wa y 24#u64) + {vc ct : U64} (hvc : vc ∈ Z.V) (hct : ct ∈ Z.T) + (hlink : L.mem s (wa vc 24#u64) = ct) (hback : L.mem s (runVcpuA ct) = vc) + {v : Vcpu} {x : Tcb} + (hkv : σ.kheap (specPtr vc) = some (.archObj (.vcpu v))) + (hkc : σ.kheap (specPtr ct) = some (.tcb x)) : + let s1 := L.wr s (wa vc 24#u64) 0#u64 + let s2 := L.wr s1 (runVcpuA ct) 0#u64 + let σ2 := updKSc (updKSc σ (specPtr vc) (.archObj (.vcpu { v with vcpuExt := { v.vcpuExt with runCaller := none } }))) + (specPtr ct) (.tcb { x with tcbExt := { x.tcbExt with tcbRunVcpu := none } }) + VcpuXferStateA (L := L) AR Z OS Ub m a s2 σ2 := by + dsimp only + set s1 := L.wr s (wa vc 24#u64) 0#u64 + set s2 := L.wr s1 (runVcpuA ct) 0#u64 + set σ1 := updKSc σ (specPtr vc) (.archObj (.vcpu { v with vcpuExt := { v.vcpuExt with runCaller := none } })) + set σ2 := updKSc σ1 (specPtr ct) (.tcb { x with tcbExt := { x.tcbExt with tcbRunVcpu := none } }) + have hne_vc_ct : specPtr vc ≠ specPtr ct := fun e => by rw [e, hkc] at hkv; cases hkv + have hP1 := vcHdr_wr_preservesA X hU hCs hcpu hExt hSw hfa hvc (o := 24#u64) (by decide) (by decide) 0#u64 + -- Intermediate `VcpuXferStateA`? No, compose the two store-preservation facts directly! + have hksb1 : L.ksb s1 = L.ksb s := L.ksb_wr _ _ _ + have hcpu1 : L.cpu s1 = L.cpu s := L.cpu_wr _ _ _ + have hksb2 : L.ksb s2 = L.ksb s := by rw [L.ksb_wr, hksb1] + have hcpu2 : L.cpu s2 = L.cpu s := by rw [L.cpu_wr, hcpu1] + have hta_ct := hU.tcb_al ct hct + have hal_rv : Aligned (runVcpuA ct) := by + unfold runVcpuA; exact aligned_wa (by unfold Aligned; have := hta_ct.align; omega) 3672#u64 (by decide) + have hal_rc : Aligned (wa vc 24#u64) := X.vcpu.place.vcPage_al (vcpuHdr_in_vcPage hvc (by decide) (by decide)) + have hrun_eq : runVcpuA ct = wa ct 3672#u64 := by unfold runVcpuA; rfl + -- Check `tcbLink_wr_preservesA` on `s` and transfer to `s1` via `mem_wr_neA`! + have hmem_other : ∀ b, Aligned b → b ≠ wa vc 24#u64 → b ≠ runVcpuA ct → L.mem s2 b = L.mem s b := by + intro b hb h1 h2 + rw [mem_wr_neA hal_rv hb h2.symm, mem_wr_neA hal_rc hb h1.symm] + have hslot_ne : ∀ y, SlotWA Z.th y → + (wa vc 24#u64 ≠ y ∧ wa vc 24#u64 ≠ wa y 8#u64 ∧ wa vc 24#u64 ≠ wa y 16#u64 ∧ wa vc 24#u64 ≠ wa y 24#u64) ∧ + (runVcpuA ct ≠ y ∧ runVcpuA ct ≠ wa y 8#u64 ∧ runVcpuA ct ≠ wa y 16#u64 ∧ runVcpuA ct ≠ wa y 24#u64) := + fun y hy => ⟨hSw _ (vcpuHdr_in_vcPage hvc (by decide) (by decide)) y hy, + by rw [hrun_eq]; exact hLD.offSlot ct hct 3672#u64 (Or.inr rfl) y hy⟩ + have hnd_eq : ∀ c : U64, c.val < 128 → ∀ k : U64, k.val < 128 → k.val % 8 = 0 → + L.mem s2 (L.nodeAt s c k) = L.mem s (L.nodeAt s c k) := fun c hc k hk hk8 => by + obtain ⟨o, ho, heq⟩ := nodeAt_eq_wa_ksbA (L := L) s c k hc (by omega) + exact hmem_other _ (X.vcpu.place.node_al c k hk8) + (X.vcpu.place.disj_node X.j.1.ksFit (vcpuHdr_in_vcPage hvc (by decide) (by decide)) hc (by omega)).symm + (by rw [hrun_eq, heq]; exact (hLD.offKs ct hct 3672#u64 (Or.inr rfl) o ho).symm) + have hspec_tcb : ∀ t ∈ Z.T, ∀ xt, σ.kheap (specPtr t) = some (.tcb xt) → + ∃ x', σ2.kheap (specPtr t) = some (.tcb x') ∧ + x'.tcbState = xt.tcbState ∧ x'.tcbSchedContext = xt.tcbSchedContext ∧ + x'.tcbDomain = xt.tcbDomain ∧ x'.tcbPriority = xt.tcbPriority ∧ + (∀ i < tcbSlotsA, Fl4SpecA64.tcbCnodeMap x' (Fl4SpecA64.tcbCnodeIndex i) = + Fl4SpecA64.tcbCnodeMap xt (Fl4SpecA64.tcbCnodeIndex i)) := by + intro t ht xt hkt + by_cases et : t = ct + · subst t + rw [hkc] at hkt; cases hkt + exact ⟨{ x with tcbExt := { x.tcbExt with tcbRunVcpu := none } }, updKSc_same _ _ _, + rfl, rfl, rfl, rfl, fun _ _ => rfl⟩ + · have hne_t : specPtr t ≠ specPtr ct := fun e => et (specPtr_inj e) + have hne_v : specPtr t ≠ specPtr vc := fun e => by rw [e, hkv] at hkt; cases hkt + exact ⟨xt, by rw [updKSc_other _ hne_t, updKSc_other _ hne_v]; exact hkt, + rfl, rfl, rfl, rfl, fun _ _ => rfl⟩ + have hspec_ty : TyKeepA σ σ2 := by + refine ⟨fun o r cs ho => ?_, fun o t ho => ?_⟩ + · have h1 : o ≠ specPtr ct := fun e => by subst e; rw [updKSc_same] at ho; cases ho + have h2 : o ≠ specPtr vc := fun e => by subst e; rw [updKSc_other _ hne_vc_ct, updKSc_same] at ho; cases ho + rw [updKSc_other _ h1, updKSc_other _ h2] at ho; exact ⟨cs, ho⟩ + · by_cases e1 : o = specPtr ct + · subst e1; exact ⟨x, hkc⟩ + · have h2 : o ≠ specPtr vc := fun e => by subst e; rw [updKSc_other _ hne_vc_ct, updKSc_same] at ho; cases ho + rw [updKSc_other _ e1, updKSc_other _ h2] at ho; exact ⟨t, ho⟩ + obtain ⟨hJ, hS2, hHV, hTr, hCar, hF⟩ := jAtZA_of_vcpuLinkStepA (σ' := σ2) X hksb2 hcpu2 + (fun y hy => by + obtain ⟨⟨a0, a8, a16, a24⟩, ⟨b0, b8, b16, b24⟩⟩ := hslot_ne y hy + exact ⟨hmem_other _ (hfa _ (.slot y _ hy (Or.inl rfl))) a0.symm b0.symm, + hmem_other _ (hfa _ (.slot y _ hy (Or.inr (Or.inl rfl)))) a8.symm b8.symm, + hmem_other _ (hfa _ (.slot y _ hy (Or.inr (Or.inr (Or.inl rfl))))) a16.symm b16.symm, + hmem_other _ (hfa _ (.slot y _ hy (Or.inr (Or.inr (Or.inr rfl))))) a24.symm b24.symm⟩) + (fun y hy => by + have hya := hU.al_os y hy + have a := X.vcpu.place.disj_os (wa vc 24#u64) (vcpuHdr_in_vcPage hvc (by decide) (by decide)) y hy + obtain ⟨b0, b8, b16, b24⟩ := hOS_tcb ct hct 3672#u64 (Or.inr rfl) y hy + rw [← hrun_eq] at b0 b8 b16 b24 + exact ⟨hmem_other _ hya a.1.symm b0.symm, + hmem_other _ (aligned_wa hya 8#u64 (by decide)) a.2.1.symm b8.symm, + hmem_other _ (aligned_wa hya 16#u64 (by decide)) a.2.2.1.symm b16.symm, + hmem_other _ (aligned_wa hya 24#u64 (by decide)) a.2.2.2.symm b24.symm⟩) + (fun c hc => ⟨hnd_eq c (hCs c hc) layout.node.CUR_THREAD (by unfold layout.node.CUR_THREAD; decide) (by unfold layout.node.CUR_THREAD; decide), + hnd_eq c (hCs c hc) layout.node.IDLE_THREAD (by unfold layout.node.IDLE_THREAD; decide) (by unfold layout.node.IDLE_THREAD; decide), + hnd_eq c (hCs c hc) layout.node.CUR_SC (by unfold layout.node.CUR_SC; decide) (by unfold layout.node.CUR_SC; decide), + hnd_eq c (hCs c hc) layout.node.IDLE_SC (by unfold layout.node.IDLE_SC; decide) (by unfold layout.node.IDLE_SC; decide)⟩) + (fun u hu => by + have hua := hU.tcb_al u hu + exact ⟨hmem_other _ (by rw [stA_eq]; exact aligned_wa (X.vcpu.place.al_t u hu) 3328#u64 (by decide)) + (by rw [stA_eq]; exact (X.vcpu.place.disj_tcb vc hvc u hu 24#u64 3328#u64 (by decide) (by decide)).symm) + (by rw [stA_eq, hrun_eq]; exact (field_ne hta_ct hua (by decide) (by decide) (fun _ => by decide)).symm), + hmem_other _ (by unfold scA; exact aligned_wa (X.vcpu.place.al_t u hu) 3464#u64 (by decide)) + (by unfold scA; exact (X.vcpu.place.disj_tcb vc hvc u hu 24#u64 3464#u64 (by decide) (by decide)).symm) + (by unfold scA; rw [hrun_eq]; exact (field_ne hta_ct hua (by decide) (by decide) (fun _ => by decide)).symm), + hmem_other _ (by unfold affA; exact aligned_wa (X.vcpu.place.al_t u hu) 3520#u64 (by decide)) + (by unfold affA; exact (X.vcpu.place.disj_tcb vc hvc u hu 24#u64 3520#u64 (by decide) (by decide)).symm) + (by unfold affA; rw [hrun_eq]; exact (field_ne hta_ct hua (by decide) (by decide) (fun _ => by decide)).symm), + hmem_other _ (hfa _ (.nslot _ (.tcb2 u _ hu (Or.inl rfl)))) + (by unfold prioA; exact (X.vcpu.place.disj_tcb vc hvc u hu 24#u64 3448#u64 (by decide) (by decide)).symm) + (by unfold prioA; rw [hrun_eq]; exact (field_ne hta_ct hua (by decide) (by decide) (fun _ => by decide)).symm), + hmem_other _ (hfa _ (.nslot _ (.tcb2 u _ hu (Or.inr rfl)))) + (by unfold domA; exact (X.vcpu.place.disj_tcb vc hvc u hu 24#u64 3432#u64 (by decide) (by decide)).symm) + (by unfold domA; rw [hrun_eq]; exact (field_ne hta_ct hua (by decide) (by decide) (fun _ => by decide)).symm)⟩) + (fun c hc => by + obtain ⟨b24, b32⟩ := hLD.offSc ct hct 3672#u64 (Or.inr rfl) c hc + rw [← hrun_eq] at b24 b32 + exact ⟨hmem_other _ (hfa _ (.nslot _ (.sc c hc))) + (X.vcpu.place.disj_sc vc hvc c hc 24#u64 24#u64 (by decide) (by decide)).symm b24.symm, + hmem_other _ (hfa _ (.nslot _ (.sc2 c hc))) + (X.vcpu.place.disj_sc vc hvc c hc 24#u64 72#u64 (by decide) (by decide)).symm b32.symm⟩) + ⟨hnd_eq (L.cpu s) hcpu layout.node.SCHED_ACTION (by unfold layout.node.SCHED_ACTION; decide) (by unfold layout.node.SCHED_ACTION; decide), + hnd_eq (L.cpu s) hcpu layout.node.CUR_TIME (by unfold layout.node.CUR_TIME; decide) (by unfold layout.node.CUR_TIME; decide), + hnd_eq (L.cpu s) hcpu layout.node.CONSUMED (by unfold layout.node.CONSUMED; decide) (by unfold layout.node.CONSUMED; decide), + hnd_eq (L.cpu s) hcpu layout.node.REPROGRAM (by unfold layout.node.REPROGRAM; decide) (by unfold layout.node.REPROGRAM; decide)⟩ + ⟨hmem_other _ (hfa _ (.nslot _ (.glob _ (Or.inl rfl)))) + (vc_disj_ksA X.vcpu.place X.j.1.ksFit (vcpuHdr_in_vcPage hvc (by decide) (by decide)) 8#u64 (by unfold ksSizeA; decide)).symm + (by rw [hrun_eq]; exact (hLD.offKs ct hct 3672#u64 (Or.inr rfl) 8#u64 (by unfold ksSizeA; decide)).symm), + hmem_other _ (hfa _ (.nslot _ (.glob _ (Or.inr rfl)))) + (vc_disj_ksA X.vcpu.place X.j.1.ksFit (vcpuHdr_in_vcPage hvc (by decide) (by decide)) 16#u64 (by unfold ksSizeA; decide)).symm + (by rw [hrun_eq]; exact (hLD.offKs ct hct 3672#u64 (Or.inr rfl) 16#u64 (by unfold ksSizeA; decide)).symm)⟩ + (fun i hi => by + have ho : (irqOffA i).val < ksSizeA := by + unfold irqOffA ksSizeA + simp only [core.num.U64.wrapping_add_val_eq, core.num.U64.wrapping_mul_val_eq, + UScalar.size, UScalarTy.numBits] + have h16 : (224#u64).val = 224 := rfl + have h8 : (8#u64).val = 8 := rfl + rw [h16, h8]; omega + exact hmem_other _ (hfa _ (.nslot _ (.irq i hi))) + (vc_disj_ksA X.vcpu.place X.j.1.ksFit (vcpuHdr_in_vcPage hvc (by decide) (by decide)) (irqOffA i) ho).symm + (by rw [hrun_eq]; exact (hLD.offKs ct hct 3672#u64 (Or.inr rfl) (irqOffA i) ho).symm)) + (fun b hb => hmem_other _ (hfa _ (.nslot _ (.vs b hb))) + (fun e => X.vcpu.place.disj_vs _ (vcpuHdr_in_vcPage hvc (by decide) (by decide)) (e.symm ▸ hb)) + (fun e => hLD.offVs ct hct 3672#u64 (Or.inr rfl) (by rw [← hrun_eq, e.symm]; exact hb))) + (fun b hb => hmem_other _ (hfa _ (.nslot _ (.ext b hb))) + (fun e => hExt _ (vcpuHdr_in_vcPage hvc (by decide) (by decide)) (e.symm ▸ hb)) + (fun e => hLD.offExt ct hct 3672#u64 (Or.inr rfl) (by rw [← hrun_eq, e.symm]; exact hb))) + (fun b hb => hmem_other _ (X.fmem.aligned b hb) + (vc_disj_ubA X.vcpu.place (vcpuHdr_in_vcPage hvc (by decide) (by decide)) hb).symm + (fun e => X.fmem.disj_tcb b hb ct hct 3672#u64 (by decide) (by rw [← hrun_eq]; exact e))) + ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, ⟨rfl, rfl, rfl, rfl⟩⟩ rfl rfl ⟨rfl, rfl⟩ + (fun p hnv hnt => by + change (updKSc (updKSc σ (specPtr vc) _) (specPtr ct) _).kheap p = σ.kheap p + rw [updKSc_other _ (fun e => hnt ct hct e.symm), updKSc_other _ (fun e => hnv vc hvc e.symm)]) + hspec_tcb hspec_ty + (fun p es hp => by + change (updKSc (updKSc σ (specPtr vc) _) (specPtr ct) _).kheap p = _ at hp + have h1 : p ≠ specPtr ct := fun e => by subst e; rw [updKSc_same] at hp; cases hp + have h2 : p ≠ specPtr vc := fun e => by subst e; rw [updKSc_other _ hne_vc_ct, updKSc_same] at hp; cases hp + rw [updKSc_other _ h1, updKSc_other _ h2] at hp; exact hp) + (fun p l1 hp => by + change (updKSc (updKSc σ (specPtr vc) _) (specPtr ct) _).kheap p = _ at hp + have h1 : p ≠ specPtr ct := fun e => by subst e; rw [updKSc_same] at hp; cases hp + have h2 : p ≠ specPtr vc := fun e => by subst e; rw [updKSc_other _ hne_vc_ct, updKSc_same] at hp; cases hp + rw [updKSc_other _ h1, updKSc_other _ h2] at hp; exact hp) + hsub + -- Prove `VcpuCoreA`, `VcpuRelA`, `TcbRegsRelA` + have hvc_mem_ne : ∀ v' ∈ Z.V, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → + (v' ≠ vc ∨ o ≠ 24#u64) → L.mem s2 (wa v' o) = L.mem s (wa v' o) := fun v' hv' o ho ho8 hne => by + have h1 : wa v' o ≠ wa vc 24#u64 := by + by_cases ev : v' = vc + · subst ev + rcases hne with h | hone + · exact absurd rfl h + · exact X.vcpu.place.same_vcpu_ne hv' ho (by decide) hone + · exact X.vcpu.place.disj_vcpu hv' hvc ev ho (by decide) + have h2 : wa v' o ≠ runVcpuA ct := by + rw [hrun_eq]; exact X.vcpu.place.disj_tcb v' hv' ct hct o 3672#u64 ho (by decide) + exact hmem_other _ (aligned_wa (X.vcpu.place.vc_al hv') o ho8) h1 h2 + have hwa0 : ∀ v' : U64, wa v' 0#u64 = v' := fun v' => by + apply UScalar.eq_of_val_eq; rw [wa_valP]; simp [UScalar.hBounds] + have hvc0 : ∀ v' ∈ Z.V, L.mem s2 v' = L.mem s v' := fun v' hv' => by + rw [← hwa0 v']; exact hvc_mem_ne v' hv' 0#u64 (by decide) (by decide) (Or.inr (by decide)) + have hrc_same : L.mem s2 (wa vc 24#u64) = 0#u64 := by + have hne : runVcpuA ct ≠ wa vc 24#u64 := by + rw [hrun_eq]; exact (X.vcpu.place.disj_tcb vc hvc ct hct 24#u64 3672#u64 (by decide) (by decide)).symm + rw [mem_wr_neA hal_rv hal_rc hne, L.mem_wr_same hal_rc] + have hrv_same : L.mem s2 (runVcpuA ct) = 0#u64 := L.mem_wr_same hal_rv + have htcb_3664 : ∀ t ∈ Z.T, L.mem s2 (wa t 3664#u64) = L.mem s (wa t 3664#u64) := fun t ht => + hmem_other _ (aligned_wa (X.vcpu.place.al_t t ht) 3664#u64 (by decide)) + (X.vcpu.place.disj_tcb vc hvc t ht 24#u64 3664#u64 (by decide) (by decide)).symm + (by rw [hrun_eq]; exact field_ne (hU.tcb_al t ht) hta_ct (by decide) (by decide) (fun _ => by decide)) + have hrv_ne : ∀ t ∈ Z.T, t ≠ ct → L.mem s2 (runVcpuA t) = L.mem s (runVcpuA t) := fun t ht hne => + hmem_other _ (by unfold runVcpuA; exact aligned_wa (X.vcpu.place.al_t t ht) 3672#u64 (by decide)) + (by unfold runVcpuA; exact (X.vcpu.place.disj_tcb vc hvc t ht 24#u64 3672#u64 (by decide) (by decide)).symm) + (by rw [hrun_eq]; unfold runVcpuA; exact field_ne (hU.tcb_al t ht) hta_ct (by decide) (by decide) (fun e => absurd e hne)) + have hVcore : VcpuCoreA (L := L) Z s2 σ2 := by + refine ⟨?_, ?_, ?_, ?_⟩ + · intro v' hv' + by_cases ev : v' = vc + · subst v' + refine ⟨{ v with vcpuExt := { v.vcpuExt with runCaller := none } }, ?_, ?_, ?_⟩ + · rw [updKSc_other _ hne_vc_ct, updKSc_same] + · rw [hvc0 vc hvc] + obtain ⟨v0, hkv0, ht0, -⟩ := X.vcpu.core.obj vc hvc + rw [hkv] at hkv0; cases hkv0; exact ht0 + · rw [hrc_same]; rfl + · have hne1 : specPtr v' ≠ specPtr ct := fun e => by + obtain ⟨v0, hkv0, -, -⟩ := X.vcpu.core.obj v' hv'; rw [e, hkc] at hkv0; cases hkv0 + have hne2 : specPtr v' ≠ specPtr vc := fun e => ev (specPtr_inj e) + obtain ⟨v0, hkv0, ht0, hr0⟩ := X.vcpu.core.obj v' hv' + exact ⟨v0, by rw [updKSc_other _ hne1, updKSc_other _ hne2]; exact hkv0, + by rw [hvc0 v' hv']; exact ht0, + by rw [hvc_mem_ne v' hv' 24#u64 (by decide) (by decide) (Or.inl ev)]; exact hr0⟩ + · intro p xv hkp + by_cases ep : p = specPtr vc + · subst ep; exact ⟨vc, hvc, rfl⟩ + · have hne1 : p ≠ specPtr ct := fun e => by subst e; rw [updKSc_same] at hkp; cases hkp + rw [updKSc_other _ hne1, updKSc_other _ ep] at hkp + exact X.vcpu.core.back p xv hkp + · intro t ht + by_cases et : t = ct + · subst t + refine ⟨{ x with tcbExt := { x.tcbExt with tcbRunVcpu := none } }, updKSc_same _ _ _, ?_, ?_⟩ + · rw [htcb_3664 ct hct] + obtain ⟨x0, hkt0, hv0, -⟩ := X.vcpu.core.tcb ct hct + rw [hkc] at hkt0; cases hkt0; exact hv0 + · rw [hrv_same]; rfl + · have hne1 : specPtr t ≠ specPtr ct := fun e => et (specPtr_inj e) + have hne2 : specPtr t ≠ specPtr vc := fun e => by + obtain ⟨x0, hkt0, -, -⟩ := X.vcpu.core.tcb t ht; rw [e, hkv] at hkt0; cases hkt0 + obtain ⟨x0, hkt0, hv0, hr0⟩ := X.vcpu.core.tcb t ht + exact ⟨x0, by rw [updKSc_other _ hne1, updKSc_other _ hne2]; exact hkt0, + by rw [htcb_3664 t ht]; exact hv0, + by rw [hrv_ne t ht et]; exact hr0⟩ + · intro y hy p hcap + obtain ⟨⟨a0, a8, -, -⟩, ⟨b0, b8, -, -⟩⟩ := hslot_ne y (Or.inl hy) + have hc' : capAtA (L.mem s2) y = capAtA (L.mem s) y := by + unfold capAtA + rw [hmem_other _ (hfa _ (.slot y _ (Or.inl hy) (Or.inl rfl))) a0.symm b0.symm, + hmem_other _ (hfa _ (.slot y _ (Or.inl hy) (Or.inr (Or.inl rfl)))) a8.symm b8.symm] + rw [hc'] at hcap; exact X.vcpu.core.cap y hy p hcap + have hkv_cases : ∀ v' ∈ Z.V, ∀ x', σ2.kheap (specPtr v') = some (.archObj (.vcpu x')) → + (v' = vc ∧ x' = { v with vcpuExt := { v.vcpuExt with runCaller := none } }) ∨ + (v' ≠ vc ∧ σ.kheap (specPtr v') = some (.archObj (.vcpu x'))) := by + intro v' hv' x' hkx + by_cases ev : v' = vc + · subst v' + rw [updKSc_other _ hne_vc_ct, updKSc_same] at hkx; cases hkx + exact Or.inl ⟨rfl, rfl⟩ + · have hne1 : specPtr v' ≠ specPtr ct := fun e => by + obtain ⟨v0, hkv0, -, -⟩ := X.vcpu.core.obj v' hv'; rw [e, hkc] at hkv0; cases hkv0 + have hne2 : specPtr v' ≠ specPtr vc := fun e => ev (specPtr_inj e) + rw [updKSc_other _ hne1, updKSc_other _ hne2] at hkx + exact Or.inr ⟨ev, hkx⟩ + have hVrel : VcpuRelA (L := L) Z OS Ub s2 σ2 := by + refine ⟨hVcore, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, X.vcpu.arch, by rw [hksb2]; exact X.vcpu.place⟩ + · intro v' hv' + rw [hvc_mem_ne v' hv' 8#u64 (by decide) (by decide) (Or.inr (by decide))] + exact X.vcpu.owner v' hv' + · intro v' hv' x' hkx + rw [hvc_mem_ne v' hv' 16#u64 (by decide) (by decide) (Or.inr (by decide))] + rcases hkv_cases v' hv' x' hkx with ⟨rfl, rfl⟩ | ⟨-, h0⟩ + · exact X.vcpu.vppi v' hvc v hkv + · exact X.vcpu.vppi v' hv' x' h0 + · intro v' hv' x' hkx + rw [hvc_mem_ne v' hv' 32#u64 (by decide) (by decide) (Or.inr (by decide))] + rcases hkv_cases v' hv' x' hkx with ⟨rfl, rfl⟩ | ⟨-, h0⟩ + · exact X.vcpu.runOff v' hvc v hkv + · exact X.vcpu.runOff v' hv' x' h0 + · intro v' hv' x' hkx + rw [hvc_mem_ne v' hv' 40#u64 (by decide) (by decide) (Or.inr (by decide))] + rcases hkv_cases v' hv' x' hkx with ⟨rfl, rfl⟩ | ⟨-, h0⟩ + · exact X.vcpu.runOut v' hvc v hkv + · exact X.vcpu.runOut v' hv' x' h0 + · intro v' hv' x' hkx i hi + have hlt : (vcpuRegOffA i).val < 4096 := by rw [vcpuRegOffA_val hi]; omega + have h8 : (vcpuRegOffA i).val % 8 = 0 := by rw [vcpuRegOffA_val hi]; omega + have hne24 : vcpuRegOffA i ≠ 24#u64 := fun e => by + have := congrArg UScalar.val e; rw [vcpuRegOffA_val hi] at this; have : (24#u64).val = 24 := rfl; omega + rw [vcpuRegWA, hvc_mem_ne v' hv' (vcpuRegOffA i) hlt h8 (Or.inr hne24)] + rcases hkv_cases v' hv' x' hkx with ⟨rfl, rfl⟩ | ⟨-, h0⟩ + · exact X.vcpu.regs v' hvc v hkv i hi + · exact X.vcpu.regs v' hv' x' h0 i hi + · intro v' hv' t ht hne + rw [hvc0 v' hv'] at ht + obtain ⟨htT, htv⟩ := X.vcpu.bind_v2t v' hv' t ht hne + exact ⟨htT, by rw [htcb_3664 t htT]; exact htv⟩ + · intro t ht v' hv' hne + rw [htcb_3664 t ht] at hv' + obtain ⟨hvV, hvt⟩ := X.vcpu.bind_t2v t ht v' hv' hne + exact ⟨hvV, by rw [hvc0 v' hvV]; exact hvt⟩ + · intro v' hv' c hc hne + by_cases ev : v' = vc + · subst v'; rw [hrc_same] at hc; exact absurd hc.symm hne + · rw [hvc_mem_ne v' hv' 24#u64 (by decide) (by decide) (Or.inl ev)] at hc + obtain ⟨hcT, hcv⟩ := X.vcpu.run_v2c v' hv' c hc hne + have hne_c : c ≠ ct := fun ec => by subst ec; rw [hback] at hcv; exact ev hcv.symm + exact ⟨hcT, by rw [hrv_ne c hcT hne_c]; exact hcv⟩ + · intro c hc v' hv' hne + by_cases ec : c = ct + · subst c; rw [hrv_same] at hv'; exact absurd hv'.symm hne + · rw [hrv_ne c hc ec] at hv' + obtain ⟨hvV, hvc'⟩ := X.vcpu.run_c2v c hc v' hv' hne + have hne_v : v' ≠ vc := fun ev => by subst ev; rw [hlink] at hvc'; exact ec hvc'.symm + exact ⟨hvV, by rw [hvc_mem_ne v' hvV 24#u64 (by decide) (by decide) (Or.inl hne_v)]; exact hvc'⟩ + · intro c hc hne + by_cases ec : c = ct + · subst c; rw [hrv_same] at hne; exact absurd rfl hne + · rw [hrv_ne c hc ec] at hne + have hst_eq' : L.mem s2 (stA c) = L.mem s (stA c) := + hmem_other _ (by rw [stA_eq]; exact aligned_wa (X.vcpu.place.al_t c hc) 3328#u64 (by decide)) + (by rw [stA_eq]; exact (X.vcpu.place.disj_tcb vc hvc c hc 24#u64 3328#u64 (by decide) (by decide)).symm) + (by rw [stA_eq, hrun_eq]; exact (field_ne hta_ct (hU.tcb_al c hc) (by decide) (by decide) (fun _ => by decide)).symm) + rw [hst_eq']; exact X.vcpu.run_state c hc hne + · intro t ht hne + rw [htcb_3664 t ht] at hne + by_cases et : t = ct + · subst t; exact hrv_same + · rw [hrv_ne t ht et]; exact X.vcpu.bind_run_disj t ht hne + · intro c hc + have hni : L.nodeAt s2 c layout.node.IDLE_THREAD = L.nodeAt s c layout.node.IDLE_THREAD := by + unfold LawfulStore.nodeAt; rw [hksb2] + rw [hni, hnd_eq c (hCs c hc) layout.node.IDLE_THREAD (by unfold layout.node.IDLE_THREAD; decide) (by unfold layout.node.IDLE_THREAD; decide)] + have hidT := hIdleT c hc + refine ⟨by rw [htcb_3664 _ hidT]; exact (X.vcpu.idle_no_vcpu c hc).1, ?_⟩ + by_cases ec : L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) = ct + · rw [ec]; exact hrv_same + · rw [hrv_ne _ hidT ec]; exact (X.vcpu.idle_no_vcpu c hc).2 + · intro v' hv' hor + have hor0 : L.mem s v' ≠ 0#u64 ∨ L.mem s (wa v' 24#u64) ≠ 0#u64 := by + rcases hor with h | h + · rw [hvc0 v' hv'] at h; exact Or.inl h + · by_cases ev : v' = vc + · subst v'; rw [hrc_same] at h; exact absurd rfl h + · rw [hvc_mem_ne v' hv' 24#u64 (by decide) (by decide) (Or.inl ev)] at h; exact Or.inr h + obtain ⟨y, hy, hcap⟩ := X.vcpu.live v' hv' hor0 + refine ⟨y, hy, ?_⟩ + have hya := hU.al_os y hy + have a := X.vcpu.place.disj_os (wa vc 24#u64) (vcpuHdr_in_vcPage hvc (by decide) (by decide)) y hy + obtain ⟨b0, b8, -, -⟩ := hOS_tcb ct hct 3672#u64 (Or.inr rfl) y hy + rw [← hrun_eq] at b0 b8 + unfold capAtA + rw [hmem_other _ hya a.1.symm b0.symm, + hmem_other _ (aligned_wa hya 8#u64 (by decide)) a.2.1.symm b8.symm] + exact hcap + have hRegs : TcbRegsRelA (L.mem s2) σ2 Z.T := by + intro t ht xt hkt r + obtain ⟨hKval, hreg_eq⟩ := regA_eq_wa_KA t r + have hlt := regIdxA_lt r + have hta := hU.tcb_al t ht + have hal_r : Aligned (Endp.regA t (regIdxA r)) := by + rw [hreg_eq]; exact aligned_wa (by unfold Aligned; have := hta.align; omega) _ (by omega) + rw [hmem_other _ hal_r + (by rw [hreg_eq]; exact (X.vcpu.place.disj_tcb vc hvc t ht 24#u64 _ (by decide) (by omega)).symm) + (by rw [hreg_eq, hrun_eq]; exact field_ne hta hta_ct (by omega) (by decide) (fun _ => by have : (3672#u64).val = 3672 := rfl; omega))] + by_cases et : t = ct + · subst t; rw [updKSc_same] at hkt; cases hkt + exact X.regs ct hct x hkc r + · have hne1 : specPtr t ≠ specPtr ct := fun e => et (specPtr_inj e) + have hne2 : specPtr t ≠ specPtr vc := fun e => by + obtain ⟨x0, hkt0, -, -⟩ := X.vcpu.core.tcb t ht; rw [e, hkv] at hkt0; cases hkt0 + rw [updKSc_other _ hne1, updKSc_other _ hne2] at hkt + exact X.regs t ht xt hkt r + exact ⟨⟨hJ, hVcore, hS2, hHV⟩, hTr, hCar, hVrel, hF, hRegs⟩ + +/-! ## Section 3: `vcpu.cancel_run` refinement (quiet & live) -/ + +/-- **`vcpu.cancel_run` refines `fl4CancelRun`**, preserving `VcpuXferStateA` (both quiet +`RUN_VCPU = 0` and live `RUN_VCPU = vc ≠ 0`). -/ +theorem cancel_run_refinesA {AR : A → HwRootA → Prop} {Z : XSetsA} + {OS Ub : Set U64} {m : Fl4SpecA64.McState} {a : A} {s : S} {σ : State Unit} + (X : VcpuXferStateA (L := L) AR Z OS Ub m a s σ) + (hU : UbFootDisjA Z OS Ub) (hLD : TcbLinkDisjA Z (L.ksb s)) + (hCs : ∀ c ∈ Z.Cs, c.val < 128) (hcpu : (L.cpu s).val < 128) + (hExt : ∀ b, VcPage Z.V b → ¬ ExtFootE Z.X Z.Y Z.T2 b) + (hSw : ∀ b, VcPage Z.V b → ∀ y, SlotWA Z.th y → + b ≠ y ∧ b ≠ wa y 8#u64 ∧ b ≠ wa y 16#u64 ∧ b ≠ wa y 24#u64) + (hfa : ∀ q, FootA (L := L) Z s q → Aligned q) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hIdleT : ∀ c ∈ Z.Cs, L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) ∈ Z.T) + (hOS_tcb : ∀ t ∈ Z.T, ∀ K : U64, (K = 3664#u64 ∨ K = 3672#u64) → ∀ y ∈ OS, + wa t K ≠ y ∧ wa t K ≠ wa y 8#u64 ∧ wa t K ≠ wa y 16#u64 ∧ wa t K ≠ wa y 24#u64) + {t : U64} (ht : t ∈ Z.T) : + vcpu.cancel_run st ar s a t ⦃ res => + res.2 = a ∧ + L.ksb res.1 = L.ksb s ∧ L.cpu res.1 = L.cpu s ∧ + (L.mem s (runVcpuA t) = 0#u64 → res.1 = s) ∧ + L.mem res.1 (runVcpuA t) = 0#u64 ∧ + ∃ σ', + fl4CancelRun (Z := Unit) (specPtr t) σ = ({((), σ')}, False) ∧ + VcpuXferStateA (L := L) AR Z OS Ub m a res.1 σ' ⦄ := by + unfold vcpu.cancel_run + have hrv_const : layout.tcb.RUN_VCPU = ok 3672#u64 := layout_tcb_run_vcpu_eqA + rw [hrv_const, bind_ok, lift_ok, bind_ok, L.read_eq, bind_ok] + have hrun_eq : wa t 3672#u64 = runVcpuA t := by unfold runVcpuA; rfl + rw [hrun_eq] + obtain ⟨xt, hkt, -, hrv_spec⟩ := X.vcpu.core.tcb t ht + by_cases hnz : (L.mem s (runVcpuA t) != 0#u64) = true + · have hne0 : L.mem s (runVcpuA t) ≠ 0#u64 := by + intro e; rw [e] at hnz; exact absurd hnz (by decide) + rw [if_pos hnz] + set vc := L.mem s (runVcpuA t) + obtain ⟨hvcV, hlink⟩ := X.vcpu.run_c2v t ht vc rfl hne0 + obtain ⟨v, hkv, -, -⟩ := X.vcpu.core.obj vc hvcV + have hrc_const : vcpu.RUN_CALLER = 24#u64 := by unfold vcpu.RUN_CALLER; rfl + rw [hrc_const, lift_ok, bind_ok, L.write_eq, bind_ok, bind_ok, L.write_eq, bind_ok, WP.spec_ok] + have hsome : xt.tcbExt.tcbRunVcpu = some (specPtr vc) := by + rw [hrv_spec]; unfold optRefA; rw [if_neg hne0] + have hal_rv : Aligned (runVcpuA t) := by + unfold runVcpuA; exact aligned_wa (X.vcpu.place.al_t t ht) 3672#u64 (by decide) + exact ⟨rfl, by simp [L.ksb_wr], by simp [L.cpu_wr], fun h => absurd h hne0, L.mem_wr_same hal_rv, + _, fl4CancelRun_someA hkt hkv hsome, + X.clear_run_link hU hLD hCs hcpu hExt hSw hfa hsub hIdleT hOS_tcb hvcV ht hlink rfl hkv hkt⟩ + · have heq0 : L.mem s (runVcpuA t) = 0#u64 := by + by_contra hne; exact hnz (bne_iff_ne.2 hne) + rw [if_neg hnz, WP.spec_ok] + have hnone : xt.tcbExt.tcbRunVcpu = none := by + rw [hrv_spec, heq0]; rfl + exact ⟨rfl, rfl, rfl, fun _ => rfl, heq0, σ, fl4CancelRun_noneA hkt hnone, X⟩ + +/-! ## Section 4: `sched.set_thread_state ... TS_RUNNING` from `TS_RUNNING_VM` -/ + +set_option maxHeartbeats 400000 in +/-- Setting `ct` from `TS_RUNNING_VM` to `TS_RUNNING` (`sched.set_thread_state st s ct TS_RUNNING`) +when `ct` is schedulable (`scA ct ≠ 0`, `rmax ≠ 0`, not in `Q_RELEASE`), paired with +`setThreadState (specPtr ct) .running`, preserves `VcpuXferStateA`. -/ +theorem VcpuXferStateA.set_state_running_from_vm {AR : A → HwRootA → Prop} {Z : XSetsA} + {OS Ub : Set U64} {m : Fl4SpecA64.McState} {a : A} {s : S} {σ : State Unit} + (X : VcpuXferStateA (L := L) AR Z OS Ub m a s σ) + (hU : UbFootDisjA Z OS Ub) + (hCs : ∀ c ∈ Z.Cs, c.val < 128) (hcpu : (L.cpu s).val < 128) + (hfa : ∀ q, FootA (L := L) Z s q → Aligned q) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hIdleT : ∀ c ∈ Z.Cs, L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) ∈ Z.T) + {ct : U64} (hct : ct ∈ Z.T) + {x : Tcb} (hkc : σ.kheap (specPtr ct) = some (.tcb x)) + (hvm : x.tcbState = .ext .runningVM) + (hno_run : L.mem s (runVcpuA ct) = 0#u64) + (hsc : L.mem s (scA ct) ≠ 0#u64) + (hrm : L.rmax s (L.mem s (scA ct)) ≠ 0#u64) + (hiq : (L.mem s (inqA ct) &&& sched.Q_RELEASE) = 0#u64) + (hma : Aligned (maxA (L.mem s (scA ct)))) (hmx : stA ct ≠ maxA (L.mem s (scA ct))) + (hNotRel : specPtr ct ∉ σ.releaseQueue) + (hSlotSt : ∀ y, SlotWA Z.th y → stA ct ≠ y ∧ stA ct ≠ wa y 8#u64 ∧ stA ct ≠ wa y 16#u64 ∧ stA ct ≠ wa y 24#u64) + (hOS_st : ∀ y ∈ OS, stA ct ≠ y ∧ stA ct ≠ wa y 8#u64 ∧ stA ct ≠ wa y 16#u64 ∧ stA ct ≠ wa y 24#u64) + (hKs_st : ∀ o : U64, o.val < ksSizeA → stA ct ≠ wa (L.ksb s) o) + (hSc_st : ∀ c ∈ Z.C, stA ct ≠ wa c 24#u64 ∧ stA ct ≠ wa c 72#u64) + (hVs_st : ¬ VsFootA Z.Pp Z.S1 Z.Y (L.ksb s) (stA ct)) + (hExt_st : ¬ ExtFootE Z.X Z.Y Z.T2 (stA ct)) : + sched.set_thread_state st s ct sched.TS_RUNNING ⦃ s' => + s' = L.wr s (stA ct) sched.TS_RUNNING ∧ + ∃ σ', setThreadState (Z := Unit) (specPtr ct) .running σ = ({((), σ')}, False) ∧ + VcpuXferStateA (L := L) AR Z OS Ub m a s' σ' ∧ + σ' = stUpdA σ (specPtr ct) x .running ⦄ := by + have hta := hU.tcb_al ct hct + rw [set_thread_state_run_wrA hta hsc hrm hiq hma hmx, WP.spec_ok] + set s' := L.wr s (stA ct) sched.TS_RUNNING + set σ' := stUpdA σ (specPtr ct) x .running + have hal_st : Aligned (stA ct) := by + rw [stA_eq]; exact aligned_wa (by unfold Aligned; have := hta.align; omega) 3328#u64 (by decide) + -- `setThreadState (specPtr ct) .running σ = ({((), σ')}, False)` (`.running` + active SC + not in releaseQueue => schedulable) + have hst_spec : setThreadState (Z := Unit) (specPtr ct) .running σ = ({((), σ')}, False) := by + apply setThreadState_eqA σ (specPtr ct) x hkc .running + intro _ _ + have hsc_mem : L.mem s (scA ct) ∈ Z.C := X.j.1.rest.scMem ct hct hsc + obtain ⟨x0, hkc0, -, hsc_eq⟩ := X.j.1.sch.tcb ct hct + rw [hkc] at hkc0; cases hkc0 + have hsc_some : x.tcbSchedContext = some (scRenA m (L.mem s (scA ct))) := by + rw [hsc_eq]; unfold optScA; rw [if_neg hsc] + obtain ⟨y, n, hy, hrm_eq⟩ := X.j.1.rest.refillMax _ hsc_mem + have hne_sc : scRenA m (L.mem s (scA ct)) ≠ specPtr ct := fun e => by + rw [e, hkc] at hy; cases hy + have hhk' : σ'.kheap (specPtr ct) = some (.tcb { x with tcbState := .running }) := by + unfold σ' stUpdA setObjA; simp [Function.update] + have hy' : σ'.kheap (scRenA m (L.mem s (scA ct))) = some (.schedContext y n) := by + dsimp only [σ', stUpdA, setObjA]; rw [Function.update_of_ne hne_sc]; exact hy + have hpos : 0 < y.scRefillMax := by + rw [hrm_eq]; by_contra h0; apply hrm + apply UScalar.eq_of_val_eq + unfold LawfulStore.rmax; rw [maxA_eqA] + exact Nat.eq_zero_of_not_pos h0 + exact schedulable_ofA hhk' rfl hsc_some hy' hpos hNotRel + refine ⟨rfl, σ', hst_spec, ?_⟩ + have hksb' : L.ksb s' = L.ksb s := L.ksb_wr _ _ _ + have hcpu' : L.cpu s' = L.cpu s := L.cpu_wr _ _ _ + have hnd_eq : ∀ c : U64, c.val < 128 → ∀ k : U64, k.val < 128 → k.val % 8 = 0 → + L.mem s' (L.nodeAt s c k) = L.mem s (L.nodeAt s c k) := fun c hc k hk hk8 => by + obtain ⟨o, ho, heq⟩ := nodeAt_eq_wa_ksbA (L := L) s c k hc (by omega) + exact mem_wr_neA hal_st (X.vcpu.place.node_al c k hk8) (by rw [heq]; exact hKs_st o ho) + have hcap_os : ∀ y ∈ OS, capAtA (L.mem s') y = capAtA (L.mem s) y := fun y hy => by + obtain ⟨h0, h8, -, -⟩ := hOS_st y hy + have hya := hU.al_os y hy + unfold capAtA; rw [mem_wr_neA hal_st hya h0, mem_wr_neA hal_st (aligned_wa hya 8#u64 (by decide)) h8] + have hcap_sl : ∀ y, SlotWA Z.th y → capAtA (L.mem s') y = capAtA (L.mem s) y := fun y hy => by + obtain ⟨h0, h8, -, -⟩ := hSlotSt y hy + unfold capAtA + rw [mem_wr_neA hal_st (hfa _ (.slot y _ hy (Or.inl rfl))) h0, + mem_wr_neA hal_st (hfa _ (.slot y _ hy (Or.inr (Or.inl rfl)))) h8] + have hrev_sl : ∀ y, SlotWA Z.th y → revA (L.mem s') y = revA (L.mem s) y := fun y hy => by + obtain ⟨-, -, -, h24⟩ := hSlotSt y hy + unfold revA; rw [mem_wr_neA hal_st (hfa _ (.slot y _ hy (Or.inr (Or.inr (Or.inr rfl))))) h24] + have hos' : OSKeep st OS s s' := fun y hy => by + obtain ⟨h0, h8, h16, h24⟩ := hOS_st y hy + have hya := hU.al_os y hy + exact ⟨mem_wr_neA hal_st hya h0, + mem_wr_neA hal_st (aligned_wa hya 8#u64 (by decide)) h8, + mem_wr_neA hal_st (aligned_wa hya 16#u64 (by decide)) h16, + mem_wr_neA hal_st (aligned_wa hya 24#u64 (by decide)) h24⟩ + have hTy : TyKeepA σ σ' := TyKeepA.upd hkc (fun _ _ e => by cases e) (fun _ _ => ⟨x, rfl⟩) + have hC : ∀ c ∈ Z.C, σ'.kheap (scRenA m c) = σ.kheap (scRenA m c) := fun c hc => by + have hne : scRenA m c ≠ specPtr ct := fun e => by + obtain ⟨xc, n, hkc0, -⟩ := X.j.1.sch.sc c hc; rw [e, hkc] at hkc0; cases hkc0 + unfold σ' stUpdA setObjA; simp [Function.update, hne] + have hT_other : ∀ t ∈ Z.T, t ≠ ct → σ'.kheap (specPtr t) = σ.kheap (specPtr t) := fun t _ hne => by + unfold σ' stUpdA setObjA; exact Function.update_of_ne (fun e => hne (specPtr_inj e)) _ _ + have hSt : StateRelA Z.th (L.mem s') σ' := by + refine ⟨fun p r hp => ?_, fun t ht => ?_⟩ + · obtain ⟨cs, hkc0, hwf, hslots⟩ := X.j.1.st.cnode p r hp + have hne : specPtr p ≠ specPtr ct := fun e => by rw [e, hkc] at hkc0; cases hkc0 + refine ⟨cs, by unfold σ' stUpdA setObjA; simp [Function.update, hne, hkc0], hwf, fun idx hidx => ?_⟩ + have hsr := hslots idx hidx + have hsl : SlotWA Z.th (slotAddrA (specPtr p) (Fl4SpecA64.ofBl idx)) := + Or.inr ⟨p, r, hp, idx, hidx, rfl⟩ + unfold SlotRelA at hsr ⊢; rw [hcap_sl _ hsl]; exact hsr + · obtain ⟨xt, hkt, hslots⟩ := X.j.1.st.tcb t ht + have htT : t ∈ Z.T := (X.j.1.thT t).1 ht + by_cases et : t = ct + · subst t; rw [hkc] at hkt; cases hkt + refine ⟨{ x with tcbState := .running }, by unfold σ' stUpdA setObjA; simp [Function.update], fun i hi => ?_⟩ + have hsr := hslots i hi + have hsl : IsSlotA Z.th (slotAddrA (specPtr ct) (BitVec.ofNat 64 i)) := ⟨ct, Or.inr ⟨ht, i, hi, rfl⟩⟩ + unfold SlotRelA at hsr ⊢; rw [hcap_sl _ (Or.inl hsl)]; exact hsr + · refine ⟨xt, by rw [hT_other t htT et]; exact hkt, fun i hi => ?_⟩ + have hsr := hslots i hi + have hsl : IsSlotA Z.th (slotAddrA (specPtr t) (BitVec.ofNat 64 i)) := ⟨t, Or.inr ⟨ht, i, hi, rfl⟩⟩ + unfold SlotRelA at hsr ⊢; rw [hcap_sl _ (Or.inl hsl)]; exact hsr + have hCdt : CdtRelA st Z.th s' σ' Z.G := by + have h0 := X.j.1.cdt + refine ⟨h0.tree.keepO (fun y hy => by + obtain ⟨h0w, h8w, h16w, h24w⟩ := hSlotSt y (Or.inl hy) + exact ⟨mem_wr_neA hal_st (hfa _ (.slot y _ (Or.inl hy) (Or.inl rfl))) h0w, + mem_wr_neA hal_st (hfa _ (.slot y _ (Or.inl hy) (Or.inr (Or.inl rfl)))) h8w, + mem_wr_neA hal_st (hfa _ (.slot y _ (Or.inl hy) (Or.inr (Or.inr (Or.inl rfl))))) h16w, + mem_wr_neA hal_st (hfa _ (.slot y _ (Or.inl hy) (Or.inr (Or.inr (Or.inr rfl))))) h24w⟩), + h0.cdt, h0.cdt_slot, fun sl hsl => by rw [hrev_sl _ (absSlot_w hsl)]; exact h0.orig sl hsl, + h0.par_slot, fun a' p' hp' => by + have hp_sl : SlotWA Z.th p' := by + obtain ⟨sl, hsl, hsl_a⟩ := h0.par_slot a' p' hp' + have hcdt := h0.cdt sl hsl; rw [hsl_a, hp'] at hcdt + cases hq : σ.cdt sl with + | none => rw [hq] at hcdt; cases hcdt + | some q => + rw [hq] at hcdt; simp only [Option.map_some, Option.some.injEq] at hcdt + rw [← hcdt]; exact absSlot_w (h0.cdt_slot sl q hq).2 + rw [hrev_sl p' hp_sl]; exact h0.prev a' p' hp'⟩ + have hSch : SchedRelA (L := L) Z.T Z.C Z.Cs Z.curOf Z.curScOf s' σ' m := by + have h0 := X.j.1.sch + have hnode' : ∀ c ∈ Z.Cs, + L.mem s' (L.nodeAt s' c layout.node.CUR_THREAD) = L.mem s (L.nodeAt s c layout.node.CUR_THREAD) ∧ + L.mem s' (L.nodeAt s' c layout.node.IDLE_THREAD) = L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) ∧ + L.mem s' (L.nodeAt s' c layout.node.CUR_SC) = L.mem s (L.nodeAt s c layout.node.CUR_SC) ∧ + L.mem s' (L.nodeAt s' c layout.node.IDLE_SC) = L.mem s (L.nodeAt s c layout.node.IDLE_SC) := fun c hc => by + simp only [nodeAt_ksb hksb'] + exact ⟨hnd_eq c (hCs c hc) layout.node.CUR_THREAD (by unfold layout.node.CUR_THREAD; decide) (by unfold layout.node.CUR_THREAD; decide), + hnd_eq c (hCs c hc) layout.node.IDLE_THREAD (by unfold layout.node.IDLE_THREAD; decide) (by unfold layout.node.IDLE_THREAD; decide), + hnd_eq c (hCs c hc) layout.node.CUR_SC (by unfold layout.node.CUR_SC; decide) (by unfold layout.node.CUR_SC; decide), + hnd_eq c (hCs c hc) layout.node.IDLE_SC (by unfold layout.node.IDLE_SC; decide) (by unfold layout.node.IDLE_SC; decide)⟩ + have hact : L.actionW s' = L.actionW s := by + unfold LawfulStore.actionW; rw [hcpu', nodeAt_ksb hksb'] + exact hnd_eq (L.cpu s) hcpu layout.node.SCHED_ACTION (by unfold layout.node.SCHED_ACTION; decide) (by unfold layout.node.SCHED_ACTION; decide) + have htime : L.curTime s' = L.curTime s := by + unfold LawfulStore.curTime LawfulStore.nodeA; rw [hcpu', nodeAt_ksb hksb'] + exact hnd_eq (L.cpu s) hcpu layout.node.CUR_TIME (by unfold layout.node.CUR_TIME; decide) (by unfold layout.node.CUR_TIME; decide) + exact { + core := by rw [hcpu']; exact h0.core + cpuCs := by rw [hcpu']; exact h0.cpuCs + curOfW := fun c hc => by rw [(hnode' c hc).1]; exact h0.curOfW c hc + curScOfW := fun c hc => by rw [(hnode' c hc).2.2.1]; exact h0.curScOfW c hc + cur := h0.cur + csc := h0.csc + idle := by unfold LawfulStore.idleW; rw [hcpu', nodeAt_ksb hksb', hnd_eq (L.cpu s) hcpu layout.node.IDLE_THREAD (by unfold layout.node.IDLE_THREAD; decide) (by unfold layout.node.IDLE_THREAD; decide)]; exact h0.idle + idleOf := fun c hc => by rw [(hnode' c hc).2.1]; exact h0.idleOf c hc + idleScOf := fun c hc => by rw [(hnode' c hc).2.2.2]; exact h0.idleScOf c hc + act := by rw [hact]; exact h0.act + time := by rw [htime]; exact h0.time + aff := fun t ht => by + have haff_eq : L.mem s' (affA t) = L.mem s (affA t) := + mem_wr_neA hal_st (by unfold affA; exact aligned_wa (X.vcpu.place.al_t t ht) 3520#u64 (by decide)) + (by rw [stA_eq]; unfold affA; exact field_ne hta (hU.tcb_al t ht) (by decide) (by decide) (fun _ => by decide)) + rw [haff_eq]; exact h0.aff t ht + affT := h0.affT + tcb := fun t ht => by + obtain ⟨xt, hkt, hst0, hsc0⟩ := h0.tcb t ht + have hsc_eq : L.mem s' (scA t) = L.mem s (scA t) := + mem_wr_neA hal_st (by unfold scA; exact aligned_wa (X.vcpu.place.al_t t ht) 3464#u64 (by decide)) + (by rw [stA_eq]; unfold scA; exact field_ne hta (hU.tcb_al t ht) (by decide) (by decide) (fun _ => by decide)) + by_cases et : t = ct + · subst t; rw [hkc] at hkt; cases hkt + refine ⟨{ x with tcbState := .running }, ?_, ?_, by rw [hsc_eq]; exact hsc0⟩ + · unfold σ' stUpdA setObjA; simp [Function.update] + · rw [L.mem_wr_same hal_st]; dsimp only; unfold sched.TS_RUNNING tsCodeA; decide + · have hst_eq' : L.mem s' (stA t) = L.mem s (stA t) := + mem_wr_neA hal_st (by rw [stA_eq]; exact aligned_wa (X.vcpu.place.al_t t ht) 3328#u64 (by decide)) + (by rw [stA_eq, stA_eq]; exact field_ne hta (hU.tcb_al t ht) (by decide) (by decide) (fun e => absurd e.symm et)) + exact ⟨xt, by rw [hT_other t ht et]; exact hkt, by rw [hst_eq']; exact hst0, by rw [hsc_eq]; exact hsc0⟩ + sc := fun c hc => by + rw [hC c hc, mem_wr_neA hal_st (hfa _ (.nslot _ (.sc c hc))) (hSc_st c hc).1] + exact h0.sc c hc + scOff := h0.scOff } + have hIrq : IrqRelA (L.mem s') (L.ksb s') σ' := by + have h0 : IrqRelA (L.mem s) (L.ksb s) σ' := + X.j.1.irq.of_spec ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, ⟨rfl, rfl, rfl, rfl⟩⟩ + exact { + st := fun i hi => by + rw [hksb', mem_wr_neA hal_st (hfa _ (.nslot _ (.irq i hi))) (hKs_st (irqOffA i) (by + unfold irqOffA ksSizeA + simp only [core.num.U64.wrapping_add_val_eq, core.num.U64.wrapping_mul_val_eq, + UScalar.size, UScalarTy.numBits] + have h16 : (224#u64).val = 224 := rfl + have h8 : (8#u64).val = 8 := rfl + rw [h16, h8]; omega))] + exact h0.st i hi + node := by rw [hksb']; exact h0.node + nodeHi := by rw [hksb']; exact h0.nodeHi } + have hRest : SchedRestA (L := L) Z s' σ' m := by + have h0 := X.j.1.rest + exact { + rq := fun p hp => by rw [hcpu']; exact h0.rq p hp + rqDom := h0.rqDom + rel := by rw [hcpu']; exact h0.rel + consumed := by rw [hcpu', nodeAt_ksb hksb', hnd_eq (L.cpu s) hcpu layout.node.CONSUMED (by unfold layout.node.CONSUMED; decide) (by unfold layout.node.CONSUMED; decide)]; exact h0.consumed + repr := by rw [hcpu', nodeAt_ksb hksb', hnd_eq (L.cpu s) hcpu layout.node.REPROGRAM (by unfold layout.node.REPROGRAM; decide) (by unfold layout.node.REPROGRAM; decide)]; exact h0.repr + curDom := by rw [hksb', mem_wr_neA hal_st (hfa _ (.nslot _ (.glob _ (Or.inl rfl)))) (hKs_st 8#u64 (by unfold ksSizeA; decide))]; exact h0.curDom + domTime := by rw [hksb', mem_wr_neA hal_st (hfa _ (.nslot _ (.glob _ (Or.inr rfl)))) (hKs_st 16#u64 (by unfold ksSizeA; decide))]; exact h0.domTime + prio := fun t ht => by + obtain ⟨xt, hkt, h1, h2, h3⟩ := h0.prio t ht + have hp_eq : L.mem s' (prioA t) = L.mem s (prioA t) := + mem_wr_neA hal_st (hfa _ (.nslot _ (.tcb2 t _ ht (Or.inl rfl)))) + (by rw [stA_eq]; unfold prioA; exact field_ne hta (hU.tcb_al t ht) (by decide) (by decide) (fun _ => by decide)) + have hd_eq : L.mem s' (domA t) = L.mem s (domA t) := + mem_wr_neA hal_st (hfa _ (.nslot _ (.tcb2 t _ ht (Or.inr rfl)))) + (by rw [stA_eq]; unfold domA; exact field_ne hta (hU.tcb_al t ht) (by decide) (by decide) (fun _ => by decide)) + by_cases et : t = ct + · subst t; rw [hkc] at hkt; cases hkt + exact ⟨{ x with tcbState := .running }, by unfold σ' stUpdA setObjA; simp [Function.update], + h1, by rw [hd_eq]; exact h2, by rw [hp_eq]; exact h3⟩ + · exact ⟨xt, by rw [hT_other t ht et]; exact hkt, h1, by rw [hd_eq]; exact h2, by rw [hp_eq]; exact h3⟩ + scMem := fun t ht hnz => by + have hsc_eq : L.mem s' (scA t) = L.mem s (scA t) := + mem_wr_neA hal_st (by unfold scA; exact aligned_wa (X.vcpu.place.al_t t ht) 3464#u64 (by decide)) + (by rw [stA_eq]; unfold scA; exact field_ne hta (hU.tcb_al t ht) (by decide) (by decide) (fun _ => by decide)) + rw [hsc_eq] at hnz ⊢; exact h0.scMem t ht hnz + refillMax := fun c hc => by + rw [hC c hc, mem_wr_neA hal_st (hfa _ (.nslot _ (.sc2 c hc))) (hSc_st c hc).2] + exact h0.refillMax c hc } + have hVs_kh : ∀ p, (∃ q ∈ Z.Pp, specPtr q = p) ∨ (∃ q ∈ Z.S1, specPtr q = p) ∨ (∃ q ∈ Z.Y, specPtr q = p) → + σ'.kheap p = σ.kheap p := fun p hp => by + have hne : p ≠ specPtr ct := fun e => by + rcases hp with ⟨q, hq, rfl⟩ | ⟨q, hq, rfl⟩ | ⟨q, hq, rfl⟩ + · obtain ⟨ap, hkp, -⟩ := X.j.1.vs.pool q hq; rw [e, hkc] at hkp; cases hkp + · obtain ⟨pt, hkp, -⟩ := X.j.1.vs.pt q hq; rw [e, hkc] at hkp; cases hkp + · obtain ⟨l1, hkp⟩ := X.j.2.2.2.obj q hq; rw [e, hkc] at hkp; cases hkp + unfold σ' stUpdA setObjA; simp [Function.update, hne] + have hExt_kh : ∀ q, q ∈ Z.Pp ∨ q ∈ Z.X ∨ q ∈ Z.Y ∨ q ∈ Z.T2 → σ'.kheap (specPtr q) = σ.kheap (specPtr q) := fun q hq => by + have hne : specPtr q ≠ specPtr ct := fun e => by + rcases hq with hq | hq | hq | hq + · obtain ⟨ap, hkp, -⟩ := X.j.1.vs.pool q hq; rw [e, hkc] at hkp; cases hkp + · obtain ⟨es, hkp⟩ := X.j.2.2.1.obj q hq; rw [e, hkc] at hkp; cases hkp + · obtain ⟨l1, hkp⟩ := X.j.2.2.2.obj q hq; rw [e, hkc] at hkp; cases hkp + · obtain ⟨pt, hkp, -⟩ := X.j.1.ext.s2pt q hq; rw [e, hkc] at hkp; cases hkp + unfold σ' stUpdA setObjA; simp [Function.update, hne] + have hJ : JAtZA (L := L) AR Z s' a σ' m := + { st := hSt, cdt := hCdt, sch := hSch, irq := hIrq, rest := hRest, ar := X.j.1.ar, + place := by rw [hksb']; exact X.j.1.place, + thT := X.j.1.thT, csN := X.j.1.csN, + idleT := fun c hc => by simp only [nodeAt_ksb hksb']; rw [hnd_eq c (hCs c hc) layout.node.IDLE_THREAD (by unfold layout.node.IDLE_THREAD; decide) (by unfold layout.node.IDLE_THREAD; decide)]; exact X.j.1.idleT c hc, + idleC := fun c hc => by simp only [nodeAt_ksb hksb']; rw [hnd_eq c (hCs c hc) layout.node.IDLE_SC (by unfold layout.node.IDLE_SC; decide) (by unfold layout.node.IDLE_SC; decide)]; exact X.j.1.idleC c hc, + curT := fun c hc => by simp only [nodeAt_ksb hksb']; rw [hnd_eq c (hCs c hc) layout.node.CUR_THREAD (by unfold layout.node.CUR_THREAD; decide) (by unfold layout.node.CUR_THREAD; decide)]; exact X.j.1.curT c hc, + cscC := fun c hc => by simp only [nodeAt_ksb hksb']; rw [hnd_eq c (hCs c hc) layout.node.CUR_SC (by unfold layout.node.CUR_SC; decide) (by unfold layout.node.CUR_SC; decide)]; exact X.j.1.cscC c hc, + ksFit := by rw [hksb']; exact X.j.1.ksFit, + scOffD := X.j.1.scOffD, + cv := fun y hy => by rw [hcap_sl y (Or.inl hy)]; exact X.j.1.cv y hy, + pml := fun y hy p mp asid hc => by rw [hcap_sl y (Or.inl hy)] at hc; exact X.j.1.pml y hy p mp asid hc, + vs := by rw [hksb']; exact (X.j.1.vs.of_spec (rfl : σ'.archState.armAsidTable = σ.archState.armAsidTable) hVs_kh).of_mem fun b hb => mem_wr_neA hal_st (hfa _ (.nslot _ (.vs b hb))) (fun e => hVs_st (e ▸ hb)), + ext := (X.j.1.ext.of_spec (rfl : σ'.archState.armAsidTable = σ.archState.armAsidTable) fun q hq => hExt_kh q (by rcases hq with h | h | h <;> simp [h])).of_mem + fun b hb => mem_wr_neA hal_st (hfa _ (.nslot _ (.ext b hb))) (fun e => hExt_st (e ▸ hb)) } + have hS2 : Stage2CoreA (L := L) Z s' σ' := + ⟨fun r hr => by rw [hExt_kh r (Or.inr (Or.inl hr))]; exact X.j.2.2.1.obj r hr, + fun p es hp => by + have hne : p ≠ specPtr ct := fun e => by subst e; unfold σ' stUpdA setObjA at hp; simp [Function.update] at hp + unfold σ' stUpdA setObjA at hp; simp [Function.update, hne] at hp; exact X.j.2.2.1.back p es hp, + fun y hy p l mp asid va hc => by rw [hcap_sl y (Or.inl hy)] at hc; exact X.j.2.2.1.cap y hy p l mp asid va hc⟩ + have hHV : HighVSpaceCoreA (L := L) Z s' σ' := + ⟨fun r hr => by rw [hExt_kh r (Or.inr (Or.inr (Or.inl hr)))]; exact X.j.2.2.2.obj r hr, + fun p l1 hp => by + have hne : p ≠ specPtr ct := fun e => by subst e; unfold σ' stUpdA setObjA at hp; simp [Function.update] at hp + unfold σ' stUpdA setObjA at hp; simp [Function.update, hne] at hp; exact X.j.2.2.2.back p l1 hp, + fun y hy p mp asid hc => by rw [hcap_sl y (Or.inl hy)] at hc; exact X.j.2.2.2.cap y hy p mp asid hc⟩ + have hvc_mem : ∀ v ∈ Z.V, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → L.mem s' (wa v o) = L.mem s (wa v o) := + fun v hv o ho ho8 => mem_wr_neA hal_st (aligned_wa (X.vcpu.place.vc_al hv) o ho8) + (by rw [stA_eq]; exact (X.vcpu.place.disj_tcb v hv ct hct o 3328#u64 ho (by decide)).symm) + have hwa0 : ∀ v : U64, wa v 0#u64 = v := fun v => by + apply UScalar.eq_of_val_eq; rw [wa_valP]; simp [UScalar.hBounds] + have hvc0 : ∀ v ∈ Z.V, L.mem s' v = L.mem s v := fun v hv => by + rw [← hwa0 v]; exact hvc_mem v hv 0#u64 (by decide) (by decide) + have htcb_other : ∀ t ∈ Z.T, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → o ≠ 3328#u64 → + L.mem s' (wa t o) = L.mem s (wa t o) := fun t ht o ho ho8 hne => + mem_wr_neA hal_st (aligned_wa (X.vcpu.place.al_t t ht) o ho8) + (by rw [stA_eq]; exact field_ne hta (hU.tcb_al t ht) (by decide) ho (fun _ e => hne (UScalar.eq_of_val_eq e.symm))) + have hrun_mem : ∀ t ∈ Z.T, L.mem s' (runVcpuA t) = L.mem s (runVcpuA t) := fun t ht => by + have : runVcpuA t = wa t 3672#u64 := by unfold runVcpuA; rfl + rw [this]; exact htcb_other t ht 3672#u64 (by decide) (by decide) (by decide) + have hkv_eq : ∀ v ∈ Z.V, ∀ x', σ'.kheap (specPtr v) = some (.archObj (.vcpu x')) → + σ.kheap (specPtr v) = some (.archObj (.vcpu x')) := fun v hv x' hkx => by + obtain ⟨v0, hkv0, -, -⟩ := X.vcpu.core.obj v hv + have hne : specPtr v ≠ specPtr ct := fun e => by rw [e, hkc] at hkv0; cases hkv0 + unfold σ' stUpdA setObjA at hkx; simpa [Function.update, hne] using hkx + have hVcore : VcpuCoreA (L := L) Z s' σ' := by + refine ⟨?_, ?_, ?_, ?_⟩ + · intro v hv + obtain ⟨v0, hkv0, ht0, hr0⟩ := X.vcpu.core.obj v hv + have hne : specPtr v ≠ specPtr ct := fun e => by rw [e, hkc] at hkv0; cases hkv0 + exact ⟨v0, by unfold σ' stUpdA setObjA; simp [Function.update, hne, hkv0], + by rw [hvc0 v hv]; exact ht0, by rw [hvc_mem v hv 24#u64 (by decide) (by decide)]; exact hr0⟩ + · intro p xv hkp + have hne : p ≠ specPtr ct := fun e => by subst e; unfold σ' stUpdA setObjA at hkp; simp [Function.update] at hkp + unfold σ' stUpdA setObjA at hkp; simp [Function.update, hne] at hkp + exact X.vcpu.core.back p xv hkp + · intro t ht + obtain ⟨xt, hkt, hv0, hr0⟩ := X.vcpu.core.tcb t ht + by_cases et : t = ct + · subst t; rw [hkc] at hkt; cases hkt + exact ⟨{ x with tcbState := .running }, by unfold σ' stUpdA setObjA; simp [Function.update], + by rw [htcb_other ct hct 3664#u64 (by decide) (by decide) (by decide)]; exact hv0, + by rw [hrun_mem ct hct]; exact hr0⟩ + · exact ⟨xt, by rw [hT_other t ht et]; exact hkt, + by rw [htcb_other t ht 3664#u64 (by decide) (by decide) (by decide)]; exact hv0, + by rw [hrun_mem t ht]; exact hr0⟩ + · intro y hy p hcap; rw [hcap_sl y (Or.inl hy)] at hcap; exact X.vcpu.core.cap y hy p hcap + have hVrel : VcpuRelA (L := L) Z OS Ub s' σ' := by + refine ⟨hVcore, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, X.vcpu.arch, by rw [hksb']; exact X.vcpu.place⟩ + · intro v hv; rw [hvc_mem v hv 8#u64 (by decide) (by decide)]; exact X.vcpu.owner v hv + · intro v hv x' hkx; rw [hvc_mem v hv 16#u64 (by decide) (by decide)]; exact X.vcpu.vppi v hv x' (hkv_eq v hv x' hkx) + · intro v hv x' hkx; rw [hvc_mem v hv 32#u64 (by decide) (by decide)]; exact X.vcpu.runOff v hv x' (hkv_eq v hv x' hkx) + · intro v hv x' hkx; rw [hvc_mem v hv 40#u64 (by decide) (by decide)]; exact X.vcpu.runOut v hv x' (hkv_eq v hv x' hkx) + · intro v hv x' hkx i hi + have hlt : (vcpuRegOffA i).val < 4096 := by rw [vcpuRegOffA_val hi]; omega + have h8 : (vcpuRegOffA i).val % 8 = 0 := by rw [vcpuRegOffA_val hi]; omega + rw [vcpuRegWA, hvc_mem v hv (vcpuRegOffA i) hlt h8]; exact X.vcpu.regs v hv x' (hkv_eq v hv x' hkx) i hi + · intro v hv t ht hne; rw [hvc0 v hv] at ht + obtain ⟨htT, htv⟩ := X.vcpu.bind_v2t v hv t ht hne + exact ⟨htT, by rw [htcb_other t htT 3664#u64 (by decide) (by decide) (by decide)]; exact htv⟩ + · intro t ht v hv hne; rw [htcb_other t ht 3664#u64 (by decide) (by decide) (by decide)] at hv + obtain ⟨hvV, hvt⟩ := X.vcpu.bind_t2v t ht v hv hne + exact ⟨hvV, by rw [hvc0 v hvV]; exact hvt⟩ + · intro v hv c hc hne; rw [hvc_mem v hv 24#u64 (by decide) (by decide)] at hc + obtain ⟨hcT, hcv⟩ := X.vcpu.run_v2c v hv c hc hne + exact ⟨hcT, by rw [hrun_mem c hcT]; exact hcv⟩ + · intro c hc v hv hne; rw [hrun_mem c hc] at hv + obtain ⟨hvV, hvc'⟩ := X.vcpu.run_c2v c hc v hv hne + exact ⟨hvV, by rw [hvc_mem v hvV 24#u64 (by decide) (by decide)]; exact hvc'⟩ + · intro c hc hne; rw [hrun_mem c hc] at hne + have hne_c : c ≠ ct := fun ec => by subst ec; exact hne hno_run + rw [mem_wr_neA hal_st (by rw [stA_eq]; exact aligned_wa (X.vcpu.place.al_t c hc) 3328#u64 (by decide)) + (by rw [stA_eq, stA_eq]; exact field_ne hta (hU.tcb_al c hc) (by decide) (by decide) (fun e => absurd e.symm hne_c))] + exact X.vcpu.run_state c hc hne + · intro t ht hne; rw [htcb_other t ht 3664#u64 (by decide) (by decide) (by decide)] at hne + rw [hrun_mem t ht]; exact X.vcpu.bind_run_disj t ht hne + · intro c hc + have hni : L.nodeAt s' c layout.node.IDLE_THREAD = L.nodeAt s c layout.node.IDLE_THREAD := by + unfold LawfulStore.nodeAt; rw [hksb'] + rw [hni, hnd_eq c (hCs c hc) layout.node.IDLE_THREAD (by unfold layout.node.IDLE_THREAD; decide) (by unfold layout.node.IDLE_THREAD; decide)] + have hidT := hIdleT c hc + rw [htcb_other _ hidT 3664#u64 (by decide) (by decide) (by decide), hrun_mem _ hidT] + exact X.vcpu.idle_no_vcpu c hc + · intro v hv hor; rw [hvc0 v hv, hvc_mem v hv 24#u64 (by decide) (by decide)] at hor + obtain ⟨y, hy, hcap⟩ := X.vcpu.live v hv hor + exact ⟨y, hy, by rw [hcap_os y hy]; exact hcap⟩ + have hTr : TreeA st OS Z.G.chains Z.G.par s' := X.tr.keepO hos' + have hCar : CarryA Z (L.mem s') σ' := + ⟨X.car.1.of_capsA rfl (fun _ h => h) (fun _ h => h) (fun y hy => Or.inl (hcap_os y (hsub y hy))), + X.car.2.of_keepA hTy.1 hTy.2 fun y hy => by rw [hcap_os y ((X.tr.lk.members y).1 hy).1]; exact CapFinEqA.refl _⟩ + have hF : FrameMemRelA Z OS Ub (L.ksb s') (L.mem s') σ'.machineState.underlyingMemory := + ⟨X.fmem.aligned, fun b hb => by + rw [mem_wr_neA hal_st (X.fmem.aligned b hb) + (fun e => X.fmem.disj_tcb b hb ct hct 3328#u64 (by decide) (by rw [← stA_eq]; exact e.symm))] + exact X.fmem.agree b hb, + fun y hy ptr sz r mp s2 asid va hc => by rw [hcap_os y hy] at hc; exact X.fmem.frames y hy ptr sz r mp s2 asid va hc, + X.fmem.disj_os, X.fmem.disj_slots, X.fmem.disj_tcb, X.fmem.disj_sc, by rw [hksb']; exact X.fmem.disj_ks⟩ + have hRegs : TcbRegsRelA (L.mem s') σ' Z.T := by + intro t ht xt hkt r + obtain ⟨hKval, hreg_eq⟩ := regA_eq_wa_KA t r + have hlt := regIdxA_lt r + have hta_t := hU.tcb_al t ht + have hal_r : Aligned (Endp.regA t (regIdxA r)) := by + rw [hreg_eq]; exact aligned_wa (by unfold Aligned; have := hta_t.align; omega) _ (by omega) + rw [mem_wr_neA hal_st hal_r (by + rw [stA_eq, hreg_eq] + have h3328 : (3328#u64 : U64).val = 3328 := rfl + exact field_ne hta hta_t (by decide) (by omega) (fun _ => by omega))] + by_cases et : t = ct + · subst t; unfold σ' stUpdA setObjA at hkt; simp [Function.update] at hkt; subst hkt + exact X.regs ct hct x hkc r + · rw [hT_other t ht et] at hkt; exact X.regs t ht xt hkt r + exact ⟨⟨⟨hJ, hVcore, hS2, hHV⟩, hTr, hCar, hVrel, hF, hRegs⟩, rfl⟩ + +/-! ## Section 5: `vcpu.complete_run` refinement -/ + +/-- `inUserFrame` is preserved when `updKSc` overwrites a non-`dataPage` object (such as a TCB or VCPU). -/ +theorem inUserFrame_updKScA {σ : State Unit} {p : ObjRef} {o0 o1 : KernelObject} + (h0 : σ.kheap p = some o0) (hnd : ∀ sz, o0 ≠ .archObj (.dataPage false sz)) + {q : ObjRef} (hq : inUserFrame q σ) : + inUserFrame q (updKSc σ p o1) := by + obtain ⟨sz, hsz⟩ := hq + refine ⟨sz, ?_⟩ + have hne : (q &&& ~~~(mask (pageBitsForSize sz))) ≠ p := fun e => by + rw [e, h0] at hsz; exact hnd sz (Option.some.inj hsz) + rw [updKSc_other _ hne]; exact hsz + +/-- Preservation bundle for `complete_run_refinesA` and `unbind_refinesA`, allowing clean chaining +of `OSKeep`, `hBufNotS2`, `hRunSched`, `hUbUser`, `hvc_cap`, and `ht_not_idle` across multiple steps. -/ +structure VcpuRunPresA {S : Type} {st : machine.Store S} (L : LawfulStore st) + (Z : XSetsA) (OS : Set U64) (s s' : S) (σ σ' : State Unit) : Prop where + os : OSKeep st OS s s' + tcb_meta : ∀ t ∈ Z.T, + L.mem s' (inqA t) = L.mem s (inqA t) ∧ L.mem s' (scA t) = L.mem s (scA t) + sc_max : ∀ c ∈ Z.C, L.mem s' (maxA c) = L.mem s (maxA c) + idle : ∀ c ∈ Z.Cs, + L.mem s' (L.nodeAt s' c layout.node.IDLE_THREAD) = L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) + user_frame : ∀ q, inUserFrame q σ → inUserFrame q σ' + run_mon : ∀ u ∈ Z.T, L.mem s' (runVcpuA u) ≠ 0#u64 → L.mem s (runVcpuA u) ≠ 0#u64 + +theorem VcpuRunPresA.refl {S : Type} {st : machine.Store S} (L : LawfulStore st) + (Z : XSetsA) (OS : Set U64) (s : S) (σ : State Unit) : + VcpuRunPresA L Z OS s s σ σ := + ⟨fun _ _ => ⟨rfl, rfl, rfl, rfl⟩, fun _ _ => ⟨rfl, rfl⟩, fun _ _ => rfl, fun _ _ => rfl, fun _ h => h, fun _ _ h => h⟩ + +theorem VcpuRunPresA.trans {S : Type} {st : machine.Store S} {L : LawfulStore st} + {Z : XSetsA} {OS : Set U64} {s1 s2 s3 : S} {σ1 σ2 σ3 : State Unit} + (h1 : VcpuRunPresA L Z OS s1 s2 σ1 σ2) (h2 : VcpuRunPresA L Z OS s2 s3 σ2 σ3) : + VcpuRunPresA L Z OS s1 s3 σ1 σ3 := + ⟨fun y hy => ⟨(h2.os y hy).1.trans (h1.os y hy).1, (h2.os y hy).2.1.trans (h1.os y hy).2.1, + (h2.os y hy).2.2.1.trans (h1.os y hy).2.2.1, (h2.os y hy).2.2.2.trans (h1.os y hy).2.2.2⟩, + fun t ht => ⟨(h2.tcb_meta t ht).1.trans (h1.tcb_meta t ht).1, (h2.tcb_meta t ht).2.trans (h1.tcb_meta t ht).2⟩, + fun c hc => (h2.sc_max c hc).trans (h1.sc_max c hc), + fun c hc => (h2.idle c hc).trans (h1.idle c hc), + fun q hq => h2.user_frame q (h1.user_frame q hq), + fun u hu h => h1.run_mon u hu (h2.run_mon u hu h)⟩ + +/-- Helper: `msginfo.MessageInfo.to_word` of `MessageInfo.new 0 0 0 4` equals `4#u64`, +matching `Fl4SpecA64.messageInfoToData ⟨4, 0, 0, 0⟩`. -/ +private theorem msginfo_4_eqA : + (do let mi ← msginfo.MessageInfo.new 0#u64 0#u64 0#u64 4#u64; msginfo.MessageInfo.to_word mi) = ok 4#u64 ∧ + (4#u64 : U64).bv = Fl4SpecA64.messageInfoToData ⟨4, 0, 0, 0⟩ := by + constructor + · unfold msginfo.MessageInfo.new msginfo.MessageInfo.to_word + rw [bind_ok, lift_ok, bind_ok, u64_shl_i32_ok (by decide) (by decide), bind_ok, + lift_ok, bind_ok, u64_shl_i32_ok (by decide) (by decide), bind_ok, + lift_ok, bind_ok, lift_ok, bind_ok, u64_shl_i32_ok (by decide) (by decide), bind_ok, + lift_ok, bind_ok, lift_ok, bind_ok] + congr 1 + · unfold Fl4SpecA64.messageInfoToData; decide + +/-- Helper: `msg.msg_register` for `0..3` maps to `X2..X5`. -/ +private theorem msg_reg_0_3_eqA : + msg.msg_register 0#u64 = ok layout.reg.X2 ∧ + msg.msg_register 1#u64 = ok layout.reg.X3 ∧ + msg.msg_register 2#u64 = ok layout.reg.X4 ∧ + msg.msg_register 3#u64 = ok layout.reg.X5 := by + refine ⟨by unfold msg.msg_register; rfl, + by unfold msg.msg_register; rfl, + by unfold msg.msg_register; rfl, + by unfold msg.msg_register; rfl⟩ + +/-- Helper: `vcpu.read_state_to_frame` only writes aligned words inside `FrameWinA Ub frame`, +leaving every aligned word outside `Ub` untouched. -/ +private theorem read_state_to_frame_keeps_offUbA {Ub : Set U64} {s : S} {a : A} + (hP : VcpuPlatAgreeA ar) {vc frame mask : U64} (hWin : FrameWinA Ub frame) + (hUbAl : ∀ b ∈ Ub, Aligned b) : + vcpu.read_state_to_frame st ar s a vc frame mask ⦃ s' => + ∀ q, Aligned q → q ∉ Ub → L.mem s' q = L.mem s q ⦄ := by + have hwr_win : ∀ (s0 : S) (k : Nat) (hk : k < 128) (w : U64) (q : U64), + Aligned q → q ∉ Ub → L.mem (L.wr s0 (wa frame ⟨BitVec.ofNat 64 (8 * k)⟩) w) q = L.mem s0 q := by + intro s0 k hk w q hq hqu + exact mem_wr_neA (hUbAl _ (hWin.2 k hk)) hq (fun e => hqu (e ▸ hWin.2 k hk)) + have hcopy : ∀ (s0 : S) (from_r to_r : U64), from_r.val ≤ to_r.val + 1 → to_r.val < 64 → + vcpu.copy_reg_range_to_frame st s0 vc frame from_r to_r ⦃ s' => + ∀ q, Aligned q → q ∉ Ub → L.mem s' q = L.mem s0 q ⦄ := by + intro s0 from_r to_r hle0 hto + unfold vcpu.copy_reg_range_to_frame vcpu.copy_reg_range_to_frame_loop + apply loop.spec_decr_nat (measure := fun p : S × U64 => to_r.val + 1 - p.2.val) + (inv := fun p => p.2.val ≤ to_r.val + 1 ∧ ∀ q, Aligned q → q ∉ Ub → L.mem p.1 q = L.mem s0 q) + (post := fun s' => ∀ q, Aligned q → q ∉ Ub → L.mem s' q = L.mem s0 q) + swap + · exact ⟨hle0, fun _ _ _ => rfl⟩ + rintro ⟨s1, r⟩ ⟨hrle, hkeep⟩ + dsimp only + unfold vcpu.copy_reg_range_to_frame_loop.body + by_cases hle : r ≤ to_r + · have hrlt : r.val < 64 := by have : r.val ≤ to_r.val := hle; omega + rw [if_pos hle, u64_mul_ok (by change r.val * 8 < 2 ^ 64; omega), bind_ok, lift_ok, bind_ok, + vcpu_reg_get_u64_eqA s1 vc r hrlt, bind_ok, L.write_eq, bind_ok, + u64_add_ok (by change r.val + 1 < 2 ^ 64; omega), bind_ok, WP.spec_ok] + have h8r : (⟨BitVec.ofNat 64 (r.val * 8#u64.val)⟩ : U64) = ⟨BitVec.ofNat 64 (8 * r.val)⟩ := by + congr 1; congr 1; change r.val * 8 = 8 * r.val; omega + have hr1 : (⟨BitVec.ofNat 64 (r.val + 1#u64.val)⟩ : U64).val = r.val + 1 := by + change (BitVec.ofNat 64 (r.val + 1)).toNat = _; rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + refine ⟨⟨by rw [hr1]; have : r.val ≤ to_r.val := hle; omega, fun q hq hqu => ?_⟩, by dsimp only; rw [hr1]; have : r.val ≤ to_r.val := hle; omega⟩ + rw [h8r, hwr_win s1 r.val (by omega) _ q hq hqu, hkeep q hq hqu] + · rw [if_neg hle, WP.spec_ok]; exact hkeep + have hloop0 : ∀ (s0 : S), + vcpu.read_state_to_frame_loop0 st s0 vc frame fl4GicV3 fl4_abi.consts.vcpu_reg.VGIC_AP1R0 ⦃ s' => + ∀ q, Aligned q → q ∉ Ub → L.mem s' q = L.mem s0 q ⦄ := by + intro s0 + have h44 : fl4_abi.consts.vcpu_reg.VGIC_AP1R0.val = vrVgicAp1r0 := by unfold fl4_abi.consts.vcpu_reg.VGIC_AP1R0 vrVgicAp1r0; rfl + have h47 : fl4_abi.consts.vcpu_reg.VGIC_AP1R3.val = vrVgicAp1r3 := by unfold fl4_abi.consts.vcpu_reg.VGIC_AP1R3 vrVgicAp1r3; rfl + unfold vcpu.read_state_to_frame_loop0 + apply loop.spec_decr_nat (measure := fun p : S × U64 => vrVgicAp1r3 + 1 - p.2.val) + (inv := fun p => p.2.val ≤ vrVgicAp1r3 + 1 ∧ ∀ q, Aligned q → q ∉ Ub → L.mem p.1 q = L.mem s0 q) + (post := fun s' => ∀ q, Aligned q → q ∉ Ub → L.mem s' q = L.mem s0 q) + swap + · exact ⟨by rw [h44]; unfold vrVgicAp1r0 vrVgicAp1r3; decide, fun _ _ _ => rfl⟩ + rintro ⟨s1, r⟩ ⟨hrle, hkeep⟩ + dsimp only + unfold vcpu.read_state_to_frame_loop0.body + by_cases hle : r ≤ fl4_abi.consts.vcpu_reg.VGIC_AP1R3 + · have hrle' : r.val ≤ vrVgicAp1r3 := by have : r.val ≤ fl4_abi.consts.vcpu_reg.VGIC_AP1R3.val := hle; omega + have hrlt : r.val < 64 := by unfold vrVgicAp1r3 at *; omega + have hval_ok : (if fl4GicV3 then vcpu.reg_get st s1 vc r else ok 0#u64) = + ok (if fl4GicV3 then L.mem s1 (vcpuRegWA vc r.val) else 0#u64) := by + cases fl4GicV3 + · rfl + · exact vcpu_reg_get_u64_eqA s1 vc r hrlt + rw [if_pos hle, hval_ok, bind_ok, u64_mul_ok (by change r.val * 8 < 2 ^ 64; omega), bind_ok, + lift_ok, bind_ok, L.write_eq, bind_ok, u64_add_ok (by change r.val + 1 < 2 ^ 64; omega), bind_ok, WP.spec_ok] + have h8r : (⟨BitVec.ofNat 64 (r.val * 8#u64.val)⟩ : U64) = ⟨BitVec.ofNat 64 (8 * r.val)⟩ := by + congr 1; congr 1; change r.val * 8 = 8 * r.val; omega + have hr1 : (⟨BitVec.ofNat 64 (r.val + 1#u64.val)⟩ : U64).val = r.val + 1 := by + change (BitVec.ofNat 64 (r.val + 1)).toNat = _; rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + refine ⟨⟨by rw [hr1]; omega, fun q hq hqu => ?_⟩, by dsimp only; rw [hr1]; omega⟩ + rw [h8r, hwr_win s1 r.val (by omega) _ q hq hqu, hkeep q hq hqu] + · rw [if_neg hle, WP.spec_ok]; exact hkeep + have hloop1 : ∀ (s0 : S), + vcpu.read_state_to_frame_loop1 st s0 vc frame ⟨BitVec.ofNat 64 fl4NumLrs⟩ 0#u64 ⦃ s' => + ∀ q, Aligned q → q ∉ Ub → L.mem s' q = L.mem s0 q ⦄ := by + intro s0 + unfold vcpu.read_state_to_frame_loop1 + apply loop.spec_decr_nat (measure := fun p : S × U64 => 16 - p.2.val) + (inv := fun p => p.2.val ≤ 16 ∧ ∀ q, Aligned q → q ∉ Ub → L.mem p.1 q = L.mem s0 q) + (post := fun s' => ∀ q, Aligned q → q ∉ Ub → L.mem s' q = L.mem s0 q) + swap + · exact ⟨by dsimp; decide, fun _ _ _ => rfl⟩ + rintro ⟨s1, i⟩ ⟨hile, hkeep⟩ + dsimp only + unfold vcpu.read_state_to_frame_loop1.body + by_cases hi16 : i < 16#u64 + · have hilt : i.val < 16 := hi16 + have hr_val : (wa fl4_abi.consts.vcpu_reg.VGIC_LR0 i).val = vrVgicLr0 + i.val := by + rw [wa_valP] + have h48 : fl4_abi.consts.vcpu_reg.VGIC_LR0.val = 48 := by unfold fl4_abi.consts.vcpu_reg.VGIC_LR0; rfl + rw [h48]; unfold vrVgicLr0; exact Nat.mod_eq_of_lt (by omega) + have hr_lt64 : (wa fl4_abi.consts.vcpu_reg.VGIC_LR0 i).val < 64 := by rw [hr_val]; unfold vrVgicLr0; omega + have hval_ok : (if i < ⟨BitVec.ofNat 64 fl4NumLrs⟩ then + vcpu.reg_get st s1 vc (wa fl4_abi.consts.vcpu_reg.VGIC_LR0 i) else ok 0#u64) = + ok (if i < ⟨BitVec.ofNat 64 fl4NumLrs⟩ then L.mem s1 (vcpuRegWA vc (vrVgicLr0 + i.val)) else 0#u64) := by + by_cases hlt : i < ⟨BitVec.ofNat 64 fl4NumLrs⟩ + · rw [if_pos hlt, if_pos hlt, vcpu_reg_get_u64_eqA s1 vc _ hr_lt64, hr_val] + · rw [if_neg hlt, if_neg hlt] + rw [if_pos hi16, lift_ok, bind_ok, hval_ok, bind_ok, + u64_mul_ok (by rw [hr_val]; change (vrVgicLr0 + i.val) * 8 < 2 ^ 64; unfold vrVgicLr0; omega), bind_ok, + lift_ok, bind_ok, L.write_eq, bind_ok, u64_add_ok (by change i.val + 1 < 2 ^ 64; omega), bind_ok, WP.spec_ok] + have hmul : (⟨BitVec.ofNat 64 ((wa fl4_abi.consts.vcpu_reg.VGIC_LR0 i).val * 8#u64.val)⟩ : U64) = + ⟨BitVec.ofNat 64 (8 * (vrVgicLr0 + i.val))⟩ := by + congr 1; rw [hr_val]; congr 1; change (vrVgicLr0 + i.val) * 8 = 8 * (vrVgicLr0 + i.val); omega + have hi1 : (⟨BitVec.ofNat 64 (i.val + 1#u64.val)⟩ : U64).val = i.val + 1 := by + change (BitVec.ofNat 64 (i.val + 1)).toNat = _; rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + refine ⟨⟨by rw [hi1]; omega, fun q hq hqu => ?_⟩, by dsimp only; rw [hi1]; omega⟩ + rw [hmul, hwr_win s1 (vrVgicLr0 + i.val) (by unfold vrVgicLr0; omega) _ q hq hqu, hkeep q hq hqu] + · rw [if_neg hi16, WP.spec_ok]; exact hkeep + have hloop2 : ∀ (s0 : S) (tp : U64), + vcpu.read_state_to_frame_loop2 st s0 (wa frame 512#u64) tp 0#u64 ⦃ s' => + ∀ q, Aligned q → q ∉ Ub → L.mem s' q = L.mem s0 q ⦄ := by + intro s0 tp + have hcnt : layout.reg.COUNT.val = 37 := by unfold layout.reg.COUNT; rfl + unfold vcpu.read_state_to_frame_loop2 + apply loop.spec_decr_nat (measure := fun p : S × U64 => 37 - p.2.val) + (inv := fun p => p.2.val ≤ 37 ∧ ∀ q, Aligned q → q ∉ Ub → L.mem p.1 q = L.mem s0 q) + (post := fun s' => ∀ q, Aligned q → q ∉ Ub → L.mem s' q = L.mem s0 q) + swap + · exact ⟨by dsimp; decide, fun _ _ _ => rfl⟩ + rintro ⟨s1, i⟩ ⟨hile, hkeep⟩ + dsimp only + unfold vcpu.read_state_to_frame_loop2.body + by_cases hi37 : i < layout.reg.COUNT + · have hilt : i.val < 37 := by have : i.val < layout.reg.COUNT.val := hi37; omega + have hval_ok : (if tp != 0#u64 then obj.Tcb.reg st tp s1 i else ok 0#u64) = + ok (if tp != 0#u64 then L.mem s1 (Endp.regA tp i) else 0#u64) := by + by_cases hnz : (tp != 0#u64) = true + · rw [if_pos hnz, if_pos hnz]; exact eq_of_spec_fd (obj.Tcb.reg.spec s1 tp i) + · rw [if_neg hnz, if_neg hnz] + rw [if_pos hi37, hval_ok, bind_ok, u64_mul_ok (by change i.val * 8 < 2 ^ 64; omega), bind_ok, + lift_ok, bind_ok, L.write_eq, bind_ok, u64_add_ok (by change i.val + 1 < 2 ^ 64; omega), bind_ok, WP.spec_ok] + have hwa_eq : wa (wa frame 512#u64) ⟨BitVec.ofNat 64 (i.val * 8#u64.val)⟩ = + wa frame ⟨BitVec.ofNat 64 (8 * (64 + i.val))⟩ := by + apply UScalar.eq_of_val_eq + rw [wa_valP, wa_valP, wa_valP] + have h512 : (512#u64 : U64).val = 512 := rfl + have h8i : (⟨BitVec.ofNat 64 (i.val * 8#u64.val)⟩ : U64).val = 8 * i.val := by + change (BitVec.ofNat 64 (i.val * 8)).toNat = 8 * i.val + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)]; omega + have h864 : (⟨BitVec.ofNat 64 (8 * (64 + i.val))⟩ : U64).val = 512 + 8 * i.val := by + change (BitVec.ofNat 64 _).toNat = _ + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)]; omega + rw [h512, h8i, h864, Nat.mod_eq_of_lt (by have := hWin.1; omega), + Nat.mod_eq_of_lt (by have := hWin.1; omega), Nat.mod_eq_of_lt (by have := hWin.1; omega)]; omega + have hi1 : (⟨BitVec.ofNat 64 (i.val + 1#u64.val)⟩ : U64).val = i.val + 1 := by + change (BitVec.ofNat 64 (i.val + 1)).toNat = _; rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + refine ⟨⟨by rw [hi1]; omega, fun q hq hqu => ?_⟩, by dsimp only; rw [hi1]; omega⟩ + rw [hwa_eq, hwr_win s1 (64 + i.val) (by omega) _ q hq hqu, hkeep q hq hqu] + · rw [if_neg hi37, WP.spec_ok]; exact hkeep + have hGprs : ∀ (s0 : S), + (do + let i ← fl4_abi.consts.vcpu_state_mask.GPRS + let i1 ← lift (mask &&& i) + if i1 != 0#u64 then do + let i2 ← 64#u64 * 8#u64 + let gpr_base ← lift (wa frame i2) + let i3 ← lift (wa vc vcpu.TCB) + let t_ptr ← st.read s0 i3 + vcpu.read_state_to_frame_loop2 st s0 gpr_base t_ptr 0#u64 + else ok s0) ⦃ s' => ∀ q, Aligned q → q ∉ Ub → L.mem s' q = L.mem s0 q ⦄ := by + intro s0 + rw [sm_gprs_ok, bind_ok, lift_ok, bind_ok] + by_cases hg : (mask &&& 16#u64 != 0#u64) = true + · rw [if_pos hg, u64_mul_ok (by decide), bind_ok, lift_ok, bind_ok, lift_ok, bind_ok, L.read_eq, bind_ok] + exact hloop2 s0 (L.mem s0 (wa vc vcpu.TCB)) + · rw [if_neg hg, WP.spec_ok]; intro _ _ _; rfl + unfold vcpu.read_state_to_frame + by_cases hm0 : mask = 0#u64 + · rw [if_pos hm0, WP.spec_ok]; intro _ _ _; rfl + · rw [if_neg hm0] + -- Step 1: EL1_SYSREGS + rw [sm_el1_ok, bind_ok, lift_ok, bind_ok] + refine WP.spec_bind (Pₘ := fun s1 => ∀ q, Aligned q → q ∉ Ub → L.mem s1 q = L.mem s q) ?_ ?_ + · by_cases h1 : (mask &&& 1#u64 != 0#u64) = true + · rw [if_pos h1] + refine WP.spec_bind (hcopy s 0#u64 fl4_abi.consts.vcpu_reg.CSSELR_EL1 (by decide) (by unfold fl4_abi.consts.vcpu_reg.CSSELR_EL1; decide)) ?_ + intro s1a h1a + refine WP.spec_mono (hcopy s1a fl4_abi.consts.vcpu_reg.ACTLR_EL1 fl4_abi.consts.vcpu_reg.MDCCINT_EL1 + (by unfold fl4_abi.consts.vcpu_reg.ACTLR_EL1 fl4_abi.consts.vcpu_reg.MDCCINT_EL1; decide) + (by unfold fl4_abi.consts.vcpu_reg.MDCCINT_EL1; decide)) ?_ + intro s1 h1b q hq hqu; rw [h1b q hq hqu, h1a q hq hqu] + · rw [if_neg h1, WP.spec_ok]; intro _ _ _; rfl + · intro s1 h1' + -- Step 2: HYP + rw [sm_hyp_ok, bind_ok, lift_ok, bind_ok] + refine WP.spec_bind (Pₘ := fun s2 => ∀ q, Aligned q → q ∉ Ub → L.mem s2 q = L.mem s q) ?_ ?_ + · by_cases h2 : (mask &&& 2#u64 != 0#u64) = true + · rw [if_pos h2] + have hvmp : fl4_abi.consts.vcpu_reg.VMPIDR_EL2.val = vrVmpidr := by unfold fl4_abi.consts.vcpu_reg.VMPIDR_EL2 vrVmpidr; rfl + have hvdi : fl4_abi.consts.vcpu_reg.VDISR_EL2.val = vrVdisr := by unfold fl4_abi.consts.vcpu_reg.VDISR_EL2 vrVdisr; rfl + rw [u64_mul_ok (by rw [hvmp]; unfold vrVmpidr; decide), bind_ok, lift_ok, bind_ok, + vcpu_reg_get_u64_eqA s1 vc _ (by rw [hvmp]; unfold vrVmpidr; decide), bind_ok, L.write_eq, bind_ok, + u64_mul_ok (by rw [hvdi]; unfold vrVdisr; decide), bind_ok, lift_ok, bind_ok, + vcpu_reg_get_u64_eqA _ vc _ (by rw [hvdi]; unfold vrVdisr; decide), bind_ok, L.write_eq, bind_ok] + have hm1 : (⟨BitVec.ofNat 64 (fl4_abi.consts.vcpu_reg.VMPIDR_EL2.val * 8#u64.val)⟩ : U64) = ⟨BitVec.ofNat 64 (8 * vrVmpidr)⟩ := by + congr 1; rw [hvmp]; unfold vrVmpidr; rfl + have hm2 : (⟨BitVec.ofNat 64 (fl4_abi.consts.vcpu_reg.VDISR_EL2.val * 8#u64.val)⟩ : U64) = ⟨BitVec.ofNat 64 (8 * vrVdisr)⟩ := by + congr 1; rw [hvdi]; unfold vrVdisr; rfl + refine WP.spec_bind (hcopy _ fl4_abi.consts.vcpu_reg.VPIDR_EL2 fl4_abi.consts.vcpu_reg.VSESR_EL2 + (by unfold fl4_abi.consts.vcpu_reg.VPIDR_EL2 fl4_abi.consts.vcpu_reg.VSESR_EL2; decide) + (by unfold fl4_abi.consts.vcpu_reg.VSESR_EL2; decide)) ?_ + intro s2a h2a + refine WP.spec_mono (hcopy s2a fl4_abi.consts.vcpu_reg.FAR_EL2 fl4_abi.consts.vcpu_reg.ESR_EL2 + (by unfold fl4_abi.consts.vcpu_reg.FAR_EL2 fl4_abi.consts.vcpu_reg.ESR_EL2; decide) + (by unfold fl4_abi.consts.vcpu_reg.ESR_EL2; decide)) ?_ + intro s2 h2b q hq hqu + rw [h2b q hq hqu, h2a q hq hqu, hm2, hwr_win _ vrVdisr (by unfold vrVdisr; decide) _ q hq hqu, + hm1, hwr_win _ vrVmpidr (by unfold vrVmpidr; decide) _ q hq hqu, h1' q hq hqu] + · rw [if_neg h2, WP.spec_ok]; exact h1' + · intro s2 h2' + -- Step 3: TIMER + rw [sm_timer_ok, bind_ok, lift_ok, bind_ok] + refine WP.spec_bind (Pₘ := fun s3 => ∀ q, Aligned q → q ∉ Ub → L.mem s3 q = L.mem s q) ?_ ?_ + · by_cases h3 : (mask &&& 4#u64 != 0#u64) = true + · rw [if_pos h3] + refine WP.spec_bind (hcopy s2 fl4_abi.consts.vcpu_reg.CNTV_CTL fl4_abi.consts.vcpu_reg.CNTKCTL_EL1 + (by unfold fl4_abi.consts.vcpu_reg.CNTV_CTL fl4_abi.consts.vcpu_reg.CNTKCTL_EL1; decide) + (by unfold fl4_abi.consts.vcpu_reg.CNTKCTL_EL1; decide)) ?_ + intro s3a h3a + rw [u64_add_ok (by decide), bind_ok, u64_mul_ok (by decide), bind_ok, lift_ok, bind_ok, + lift_ok, bind_ok, L.read_eq, bind_ok, L.write_eq, WP.spec_ok] + intro q hq hqu + have h101 : (⟨BitVec.ofNat 64 ((⟨BitVec.ofNat 64 (64#u64.val + 37#u64.val)⟩ : U64).val * 8#u64.val)⟩ : U64) = + ⟨BitVec.ofNat 64 (8 * 101)⟩ := rfl + rw [h101, hwr_win s3a 101 (by decide) _ q hq hqu, h3a q hq hqu, h2' q hq hqu] + · rw [if_neg h3, WP.spec_ok]; exact h2' + · intro s3 h3' + -- Step 4: VGIC / VGIC_LR / GPRS + rw [sm_vgic_ok, bind_ok, lift_ok, bind_ok] + by_cases h4 : (mask &&& 8#u64 != 0#u64) = true + · rw [if_pos h4, vcpu_num_lrs_eqA hP a, bind_ok] + obtain ⟨g, hga, hgb⟩ := hP.gic a + rw [hga, bind_ok] + have hgvtr : fl4_abi.consts.vcpu_reg.VGIC_VTR.val = vrVgicVtr := by unfold fl4_abi.consts.vcpu_reg.VGIC_VTR vrVgicVtr; rfl + have hdec : (decide (g = 3#u64)) = fl4GicV3 := hgb + rw [u64_mul_ok (by rw [hgvtr]; unfold vrVgicVtr; decide), bind_ok, lift_ok, bind_ok, + hP.vtr a, bind_ok, hdec, vcpu_abi_vgic_vtr_eqA hP a, bind_ok, L.write_eq, bind_ok] + have hm_vtr : (⟨BitVec.ofNat 64 (fl4_abi.consts.vcpu_reg.VGIC_VTR.val * 8#u64.val)⟩ : U64) = + ⟨BitVec.ofNat 64 (8 * vrVgicVtr)⟩ := by congr 1; rw [hgvtr]; unfold vrVgicVtr; rfl + refine WP.spec_bind (hcopy _ fl4_abi.consts.vcpu_reg.VGIC_HCR fl4_abi.consts.vcpu_reg.VGIC_AP0R3 + (by unfold fl4_abi.consts.vcpu_reg.VGIC_HCR fl4_abi.consts.vcpu_reg.VGIC_AP0R3; decide) + (by unfold fl4_abi.consts.vcpu_reg.VGIC_AP0R3; decide)) ?_ + intro s4a h4a + refine WP.spec_bind (hloop0 s4a) ?_ + intro s4b h4b + refine WP.spec_bind (hloop1 s4b) ?_ + intro s4c h4c + refine WP.spec_mono (hGprs s4c) ?_ + intro s' hs' q hq hqu + rw [hs' q hq hqu, h4c q hq hqu, h4b q hq hqu, h4a q hq hqu, + hm_vtr, hwr_win s3 vrVgicVtr (by unfold vrVgicVtr; decide) _ q hq hqu, h3' q hq hqu] + · rw [if_neg h4, sm_vgic_lr_ok, bind_ok, lift_ok, bind_ok] + by_cases h5 : (mask &&& 32#u64 != 0#u64) = true + · rw [if_pos h5, vcpu_num_lrs_eqA hP a, bind_ok] + have hvhcr : fl4_abi.consts.vcpu_reg.VGIC_HCR.val = vrVgicHcr := by unfold fl4_abi.consts.vcpu_reg.VGIC_HCR vrVgicHcr; rfl + have helrsr : fl4_abi.consts.vcpu_reg.VGIC_ELRSR.val = vrVgicElrsr := by unfold fl4_abi.consts.vcpu_reg.VGIC_ELRSR vrVgicElrsr; rfl + rw [u64_mul_ok (by rw [hvhcr]; unfold vrVgicHcr; decide), bind_ok, lift_ok, bind_ok, + vcpu_reg_get_u64_eqA s3 vc _ (by rw [hvhcr]; unfold vrVgicHcr; decide), bind_ok, L.write_eq, bind_ok, + u64_mul_ok (by rw [helrsr]; unfold vrVgicElrsr; decide), bind_ok, lift_ok, bind_ok, + vcpu_reg_get_u64_eqA _ vc _ (by rw [helrsr]; unfold vrVgicElrsr; decide), bind_ok, L.write_eq, bind_ok] + have hm_hcr : (⟨BitVec.ofNat 64 (fl4_abi.consts.vcpu_reg.VGIC_HCR.val * 8#u64.val)⟩ : U64) = + ⟨BitVec.ofNat 64 (8 * vrVgicHcr)⟩ := by congr 1; rw [hvhcr]; unfold vrVgicHcr; rfl + have hm_elr : (⟨BitVec.ofNat 64 (fl4_abi.consts.vcpu_reg.VGIC_ELRSR.val * 8#u64.val)⟩ : U64) = + ⟨BitVec.ofNat 64 (8 * vrVgicElrsr)⟩ := by congr 1; rw [helrsr]; unfold vrVgicElrsr; rfl + have hloop3 : ∀ s0 : S, vcpu.read_state_to_frame_loop3 st s0 vc frame ⟨BitVec.ofNat 64 fl4NumLrs⟩ 0#u64 = + vcpu.read_state_to_frame_loop1 st s0 vc frame ⟨BitVec.ofNat 64 fl4NumLrs⟩ 0#u64 := fun s0 => by + unfold vcpu.read_state_to_frame_loop3 vcpu.read_state_to_frame_loop1; congr 1 + rw [hloop3] + refine WP.spec_bind (hloop1 _) ?_ + intro s5a h5a + refine WP.spec_mono (hGprs s5a) ?_ + intro s' hs' q hq hqu + rw [hs' q hq hqu, h5a q hq hqu, + hm_elr, hwr_win _ vrVgicElrsr (by unfold vrVgicElrsr; decide) _ q hq hqu, + hm_hcr, hwr_win s3 vrVgicHcr (by unfold vrVgicHcr; decide) _ q hq hqu, h3' q hq hqu] + · rw [if_neg h5] + refine WP.spec_mono (hGprs s3) ?_ + intro s' hs' q hq hqu + rw [hs' q hq hqu, h3' q hq hqu] + +private theorem ctxUpdA_idem (σ : State Unit) (t : ObjRef) (x : Tcb) (uc1 uc2 : UserContext) : + ctxUpdA (ctxUpdA σ t x uc1) t { x with tcbArch := archTcbContextSet uc1 x.tcbArch } uc2 = + ctxUpdA σ t x uc2 := by + cases σ with + | mk a b => + cases a; unfold ctxUpdA setObjA archTcbContextSet; dsimp only; congr 1; congr 1 + funext p; by_cases hp : p = t <;> simp [Function.update, hp] + +private theorem asUser_det_oneA {σ : State Unit} {t : ObjRef} {x : Tcb} + (hk : σ.kheap t = some (.tcb x)) {f : NonDet UserContext Unit} {uc' : UserContext} + (hf : f (archTcbContextGet x.tcbArch) = ({((), uc')}, False)) : + asUser (Z := Unit) t f σ = ({((), ctxUpdA σ t x uc')}, False) := by + have hg : NonDet.getsThe (getTcb (Z := Unit) t) σ = ({(x, σ)}, False) := by + simp [NonDet.getsThe, getTcb, readObject, hk, NonDet.assertOpt]; rfl + have ho : getObject (Z := Unit) t σ = ({(.tcb x, σ)}, False) := by + simp [getObject, NonDet.getsThe, readObject, hk, NonDet.assertOpt]; rfl + have hsel : NonDet.selectF (f (archTcbContextGet x.tcbArch)) σ = ({(((), uc'), σ)}, False) := by + rw [hf] + simp only [NonDet.selectF, Set.mem_singleton_iff, Prod.mk.injEq] + exact ⟨Set.ext fun y => by simp [Prod.ext_iff], trivial⟩ + have hso : setObject (Z := Unit) t (.tcb { x with tcbArch := archTcbContextSet uc' x.tcbArch }) σ = + ({((), ctxUpdA σ t x uc')}, False) := by + unfold setObject + rw [nd_bind_oneSc ho, nd_bind_oneSc (show (NonDet.assert (aType (.tcb x) = aType (.tcb _)) : + NonDet (State Unit) Unit) σ = ({((), σ)}, False) by simp [NonDet.assert, aType]), + nd_bind_oneSc (show NonDet.get σ = ({(σ, σ)}, False) from rfl)] + rfl + unfold asUser + rw [nd_bind_oneSc hg, nd_bind_oneSc (show (pure (archTcbContextGet x.tcbArch) : SMonad Unit UserContext) σ = _ from rfl), + nd_bind_oneSc hsel] + dsimp only + rw [nd_bind_oneSc (show (pure _ : SMonad Unit Tcb) σ = _ from rfl), nd_bind_oneSc hso] + rfl + +private theorem asUser_seqA {σ σ1 σ2 : State Unit} {t : ObjRef} {x : Tcb} + (hk : σ.kheap t = some (.tcb x)) {f g : NonDet UserContext Unit} + {uc1 uc2 : UserContext} + (hf : f (archTcbContextGet x.tcbArch) = ({((), uc1)}, False)) + (hg : g uc1 = ({((), uc2)}, False)) + (h1 : asUser (Z := Unit) t f σ = ({((), σ1)}, False)) + (h2 : asUser (Z := Unit) t g σ1 = ({((), σ2)}, False)) : + asUser (Z := Unit) t (f >>= fun _ => g) σ = ({((), σ2)}, False) := by + have heq1 := asUser_det_oneA hk hf + rw [h1] at heq1 + have hσ1 : σ1 = ctxUpdA σ t x uc1 := by + have h := congrArg Prod.fst heq1; dsimp only at h + have hmem : ((), σ1) ∈ ({((), ctxUpdA σ t x uc1)} : Set (Unit × State Unit)) := by rw [← h]; exact Set.mem_singleton _ + exact (Prod.mk.inj (Set.mem_singleton_iff.1 hmem)).2 + subst hσ1 + have hk1 : (ctxUpdA σ t x uc1).kheap t = some (.tcb { x with tcbArch := archTcbContextSet uc1 x.tcbArch }) := by + unfold ctxUpdA setObjA archTcbContextSet; simp [Function.update] + have heq2 := asUser_det_oneA hk1 (f := g) (uc' := uc2) (by simpa [archTcbContextGet, archTcbContextSet] using hg) + rw [h2] at heq2 + have hσ2 : σ2 = ctxUpdA σ t x uc2 := by + have h := congrArg Prod.fst heq2; dsimp only at h + have hmem : ((), σ2) ∈ ({((), ctxUpdA (ctxUpdA σ t x uc1) t { x with tcbArch := archTcbContextSet uc1 x.tcbArch } uc2)} : Set (Unit × State Unit)) := by + rw [← h]; exact Set.mem_singleton _ + have := (Prod.mk.inj (Set.mem_singleton_iff.1 hmem)).2 + rwa [ctxUpdA_idem] at this + subst hσ2 + exact asUser_det_oneA hk (by rw [nd_bind_oneSc hf]; exact hg) + +set_option maxHeartbeats 400000 in +/-- Helper: the 7 TCB user-register writes in `vcpu.complete_run` (BADGE, X2..X5, MSG_INFO, +FAULT_IP := NEXT_IP) refine the corresponding `asUser` / `setMessageInfo` spec steps and +preserve `VcpuXferStateA` and all non-register TCB/VCPU/SC words. -/ +private theorem complete_run_regs_stepA {AR : A → HwRootA → Prop} {Z : XSetsA} + {OS Ub : Set U64} {m : Fl4SpecA64.McState} {a1 : A} {s3 : S} {σ3 : State Unit} + (X3 : VcpuXferStateA (L := L) AR Z OS Ub m a1 s3 σ3) + (hU : UbFootDisjA Z OS Ub) + (hCs : ∀ c ∈ Z.Cs, c.val < 128) (hcpu3 : (L.cpu s3).val < 128) + (hfa3 : ∀ q, FootA (L := L) Z s3 q → Aligned q) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hIdle3 : ∀ c ∈ Z.Cs, L.mem s3 (L.nodeAt s3 c layout.node.IDLE_THREAD) ∈ Z.T) + (hksTcb3 : ∀ t ∈ Z.T, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → + ∀ o : U64, o.val < ksSizeA → wa t K ≠ wa (L.ksb s3) o) + (hSlotTcb : ∀ t ∈ Z.T, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → + ∀ y, SlotWA Z.th y → wa t K ≠ y ∧ wa t K ≠ wa y 8#u64 ∧ wa t K ≠ wa y 16#u64 ∧ wa t K ≠ wa y 24#u64) + {ct : U64} (hct : ct ∈ Z.T) {x3 : Tcb} (hkc3 : σ3.kheap (specPtr ct) = some (.tcb x3)) + {stop : U64} {stopSpec : RunStop} (hstop : stop.bv = stopSpec.word) (w1 w2 w3 : U64) + {α : Type} {k : S → Result α} {P : α → Prop} + (hk_cont : ∀ s10 σ10 x10, + VcpuXferStateA (L := L) AR Z OS Ub m a1 s10 σ10 → + (∀ tail : SMonad Unit Unit, (do + asUser (Z := Unit) (specPtr ct) (setRegister badge_register 0) + asUser (specPtr ct) do + setRegister .x2 stopSpec.word + setRegister .x3 w1.bv + setRegister .x4 w2.bv + setRegister .x5 w3.bv + setMessageInfo (specPtr ct) ⟨4, 0, 0, 0⟩ + asUser (specPtr ct) do + let ip ← getRegister nextInstructionRegister + setRegister faultRegister ip + tail) σ3 = tail σ10) → + σ10.kheap (specPtr ct) = some (.tcb x10) → + x10.tcbState = x3.tcbState → + L.ksb s10 = L.ksb s3 → L.cpu s10 = L.cpu s3 → + (∀ q, Aligned q → (∀ r : Register, Endp.regA ct (regIdxA r) ≠ q) → L.mem s10 q = L.mem s3 q) → + (∀ q, inUserFrame q σ3 → inUserFrame q σ10) → + k s10 ⦃ P ⦄) : + (do + let s4 ← obj.Tcb.set_reg st ct s3 layout.reg.BADGE 0#u64 + let i ← msg.msg_register 0#u64 + let s5 ← obj.Tcb.set_reg st ct s4 i stop + let i1 ← msg.msg_register 1#u64 + let s6 ← obj.Tcb.set_reg st ct s5 i1 w1 + let i2 ← msg.msg_register 2#u64 + let s7 ← obj.Tcb.set_reg st ct s6 i2 w2 + let i3 ← msg.msg_register 3#u64 + let s8 ← obj.Tcb.set_reg st ct s7 i3 w3 + let mi ← msginfo.MessageInfo.new 0#u64 0#u64 0#u64 4#u64 + let s9 ← msg.set_msg_info st s8 ct mi + let ip ← obj.Tcb.reg st ct s9 layout.reg.NEXT_IP + let s10 ← obj.Tcb.set_reg st ct s9 layout.reg.FAULT_IP ip + k s10) ⦃ P ⦄ := by + have hta_ct := hU.tcb_al ct hct + have hreg_al : ∀ r : Register, Aligned (Endp.regA ct (regIdxA r)) := fun r => by + obtain ⟨hKval, hreg_eq⟩ := regA_eq_wa_KA ct r + have hlt := regIdxA_lt r + rw [hreg_eq]; exact aligned_wa (by unfold Aligned; have := hta_ct.align; omega) _ (by omega) + have hreg_wr : ∀ {s0 : S} {σ0 : State Unit} {x0 : Tcb} + (X0 : VcpuXferStateA (L := L) AR Z OS Ub m a1 s0 σ0) + (hksb0 : L.ksb s0 = L.ksb s3) (hcpu0 : L.cpu s0 = L.cpu s3) + (hk0 : σ0.kheap (specPtr ct) = some (.tcb x0)) + (r : Register) (ri : U64) (hri : ri = regIdxA r) (w : U64), + ∃ s' σ' x', + obj.Tcb.set_reg st ct s0 ri w = ok s' ∧ + asUser (Z := Unit) (specPtr ct) (setRegister r w.bv) σ0 = ({((), σ')}, False) ∧ + σ'.kheap (specPtr ct) = some (.tcb x') ∧ + x'.tcbState = x0.tcbState ∧ + VcpuXferStateA (L := L) AR Z OS Ub m a1 s' σ' ∧ + L.ksb s' = L.ksb s3 ∧ L.cpu s' = L.cpu s3 ∧ + (∀ q, Aligned q → (∀ r' : Register, Endp.regA ct (regIdxA r') ≠ q) → L.mem s' q = L.mem s0 q) ∧ + (∀ q, inUserFrame q σ0 → inUserFrame q σ') := by + intro s0 σ0 x0 X0 hksb0 hcpu0 hk0 r ri hri w + let uc' : UserContext := { x0.tcbArch.tcbContext with userRegs := Function.update x0.tcbArch.tcbContext.userRegs r w.bv } + refine ⟨L.wr s0 (Endp.regA ct (regIdxA r)) w, ctxUpdA σ0 (specPtr ct) x0 uc', + { x0 with tcbArch := archTcbContextSet uc' x0.tcbArch }, + by rw [hri]; unfold obj.Tcb.set_reg Endp.regA; rw [lift_ok, bind_ok, lift_ok, bind_ok, lift_ok, bind_ok, L.write_eq], + asUser_setReg_oneA hk0 r w.bv, + by unfold ctxUpdA setObjA archTcbContextSet; simp [Function.update], + rfl, + X0.tcb_reg_wr hU hCs (by rw [hcpu0]; exact hcpu3) + (fun q hq => hfa3 q (footA_of_ksb_cpuA hksb0 hcpu0 hq)) + hsub (fun c hc => X0.j.1.idleT c hc) (by rw [hksb0]; exact hksTcb3) hSlotTcb hct hk0 r w, + by rw [L.ksb_wr, hksb0], by rw [L.cpu_wr, hcpu0], + fun q hq hne => mem_wr_neA (hreg_al r) hq (hne r), + fun q hq => inUserFrame_updKScA hk0 (fun _ h => by cases h) hq⟩ + obtain ⟨hmr0, hmr1, hmr2, hmr3⟩ := msg_reg_0_3_eqA + obtain ⟨hmi_ok, hmi_bv⟩ := msginfo_4_eqA + obtain ⟨s4, σ4, x4, hs4_ok, hsp4, hkc4, hx4_st, X4, hksb4, hcpu4, hmem4, huf4⟩ := + hreg_wr X3 rfl rfl hkc3 .x0 layout.reg.BADGE (by simp [layout.reg.BADGE, layout.reg.X0, regIdxA]) 0#u64 + obtain ⟨s5, σ5, x5, hs5_ok, hsp5, hkc5, hx5_st, X5, hksb5, hcpu5, hmem5, huf5⟩ := + hreg_wr X4 hksb4 hcpu4 hkc4 .x2 layout.reg.X2 (by simp [layout.reg.X2, regIdxA]) stop + obtain ⟨s6, σ6, x6, hs6_ok, hsp6, hkc6, hx6_st, X6, hksb6, hcpu6, hmem6, huf6⟩ := + hreg_wr X5 hksb5 hcpu5 hkc5 .x3 layout.reg.X3 (by simp [layout.reg.X3, regIdxA]) w1 + obtain ⟨s7, σ7, x7, hs7_ok, hsp7, hkc7, hx7_st, X7, hksb7, hcpu7, hmem7, huf7⟩ := + hreg_wr X6 hksb6 hcpu6 hkc6 .x4 layout.reg.X4 (by simp [layout.reg.X4, regIdxA]) w2 + obtain ⟨s8, σ8, x8, hs8_ok, hsp8, hkc8, hx8_st, X8, hksb8, hcpu8, hmem8, huf8⟩ := + hreg_wr X7 hksb7 hcpu7 hkc7 .x5 layout.reg.X5 (by simp [layout.reg.X5, regIdxA]) w3 + obtain ⟨s9, σ9, x9, hs9_ok, hsp9, hkc9, hx9_st, X9, hksb9, hcpu9, hmem9, huf9⟩ := + hreg_wr X8 hksb8 hcpu8 hkc8 .x1 layout.reg.MSG_INFO (by simp [layout.reg.MSG_INFO, layout.reg.X1, regIdxA]) 4#u64 + have hmi_new : msginfo.MessageInfo.new 0#u64 0#u64 0#u64 4#u64 = ok ⟨0#u64, 0#u64, 0#u64, 4#u64⟩ := by + unfold msginfo.MessageInfo.new; rfl + have hmi_word : msginfo.MessageInfo.to_word ⟨0#u64, 0#u64, 0#u64, 4#u64⟩ = ok 4#u64 := by + rw [hmi_new, bind_ok] at hmi_ok; exact hmi_ok + have hip_ok : obj.Tcb.reg st ct s9 layout.reg.NEXT_IP = ok (L.mem s9 (Endp.regA ct (regIdxA .nextIP))) := by + rw [← regIdxA_nextIP] + unfold obj.Tcb.reg Endp.regA; rw [lift_ok, bind_ok, lift_ok, bind_ok, lift_ok, bind_ok, L.read_eq] + let ip := L.mem s9 (Endp.regA ct (regIdxA .nextIP)) + obtain ⟨s10, σ10, x10, hs10_ok, hsp10, hkc10, hx10_st, X10, hksb10, hcpu10, hmem10, huf10⟩ := + hreg_wr X9 hksb9 hcpu9 hkc9 .faultIP layout.reg.FAULT_IP (by simp [layout.reg.FAULT_IP, regIdxA]) ip + rw [hs4_ok, bind_ok, hmr0, bind_ok, hs5_ok, bind_ok, hmr1, bind_ok, hs6_ok, bind_ok, + hmr2, bind_ok, hs7_ok, bind_ok, hmr3, bind_ok, hs8_ok, bind_ok, hmi_new, bind_ok] + unfold msg.set_msg_info + rw [hmi_word, bind_ok, hs9_ok, bind_ok, hip_ok, bind_ok, hs10_ok, bind_ok] + refine hk_cont s10 σ10 x10 X10 ?_ hkc10 + (hx10_st.trans (hx9_st.trans (hx8_st.trans (hx7_st.trans (hx6_st.trans (hx5_st.trans hx4_st)))))) + hksb10 hcpu10 (fun q hq hne => by + rw [hmem10 q hq hne, hmem9 q hq hne, hmem8 q hq hne, hmem7 q hq hne, + hmem6 q hq hne, hmem5 q hq hne, hmem4 q hq hne]) + (fun q hq => huf10 q (huf9 q (huf8 q (huf7 q (huf6 q (huf5 q (huf4 q hq))))))) + intro tail + have hbadge_eq : badge_register = .x0 := rfl + have hsp4' : asUser (Z := Unit) (specPtr ct) (setRegister .x0 0) σ3 = ({((), σ4)}, False) := hsp4 + rw [hbadge_eq, nd_bind_oneSc hsp4'] + -- Chain `hsp5..hsp8` via `asUser_seqA` + have hsp78 : asUser (Z := Unit) (specPtr ct) (do setRegister .x4 w2.bv; setRegister .x5 w3.bv) σ6 = ({((), σ8)}, False) := + asUser_seqA hkc6 rfl rfl hsp7 hsp8 + have hsp68 : asUser (Z := Unit) (specPtr ct) (do setRegister .x3 w1.bv; setRegister .x4 w2.bv; setRegister .x5 w3.bv) σ5 = ({((), σ8)}, False) := + asUser_seqA hkc5 rfl (by rw [nd_bind_oneSc (m := setRegister .x4 w2.bv) rfl]; rfl) hsp6 hsp78 + have hsp58 : asUser (Z := Unit) (specPtr ct) (do setRegister .x2 stopSpec.word; setRegister .x3 w1.bv; setRegister .x4 w2.bv; setRegister .x5 w3.bv) σ4 = ({((), σ8)}, False) := by + rw [← hstop] + exact asUser_seqA hkc4 rfl (by rw [nd_bind_oneSc (m := setRegister .x3 w1.bv) rfl, nd_bind_oneSc (m := setRegister .x4 w2.bv) rfl]; rfl) hsp5 hsp68 + rw [nd_bind_oneSc hsp58] + have hsmi : setMessageInfo (Z := Unit) (specPtr ct) ⟨4, 0, 0, 0⟩ σ8 = ({((), σ9)}, False) := by + unfold setMessageInfo + have hmi_reg : msg_info_register = .x1 := rfl + rw [hmi_reg, ← hmi_bv]; exact hsp9 + rw [nd_bind_oneSc hsmi] + have hget_ip : ip.bv = x9.tcbArch.tcbContext.userRegs .nextIP := X9.regs ct hct x9 hkc9 .nextIP + have hnr : nextInstructionRegister = .nextIP := rfl + have hfr : faultRegister = .faultIP := rfl + rw [hnr, hfr] + have hf_eq : (do let ip0 ← getRegister .nextIP; setRegister .faultIP ip0) (archTcbContextGet x9.tcbArch) = + setRegister .faultIP ip.bv (archTcbContextGet x9.tcbArch) := by + rw [nd_bind_oneSc (m := getRegister .nextIP) rfl]; dsimp only [archTcbContextGet]; rw [← hget_ip] + have hfault_eq : asUser (Z := Unit) (specPtr ct) (do let ip0 ← getRegister .nextIP; setRegister .faultIP ip0) σ9 = + asUser (Z := Unit) (specPtr ct) (setRegister .faultIP ip.bv) σ9 := by + unfold asUser + have hg : NonDet.getsThe (getTcb (Z := Unit) (specPtr ct)) σ9 = ({(x9, σ9)}, False) := by + simp [NonDet.getsThe, getTcb, readObject, hkc9, NonDet.assertOpt]; rfl + rw [nd_bind_oneSc hg, nd_bind_oneSc (show (pure (archTcbContextGet x9.tcbArch) : SMonad Unit UserContext) σ9 = _ from rfl), + hf_eq] + rw [nd_bind_oneSc hg, nd_bind_oneSc (show (pure (archTcbContextGet x9.tcbArch) : SMonad Unit UserContext) σ9 = _ from rfl)] + rw [nd_bind_oneSc (hfault_eq.trans hsp10)] + +set_option maxHeartbeats 600000 in +/-- **`vcpu.complete_run` refines `fl4CompleteRun`** (reusing W3's `read_state_to_frame_refinesA`, +`VcpuXferStateA.clear_run_link`, 7× `VcpuXferStateA.tcb_reg_wr`, `set_state_running_from_vm`, +and `VcpuVmRootA` updating `Z.hw` when `ct = cur_thread`). -/ +theorem complete_run_refinesA {AR : A → HwRootA → Prop} {Z : XSetsA} + {OS Ub : Set U64} {m : Fl4SpecA64.McState} {a : A} {s : S} {σ : State Unit} + (X : VcpuXferStateA (L := L) AR Z OS Ub m a s σ) + (hP : VcpuPlatAgreeA ar) (hAK : VcpuArchKeepsA ar AR) (hVR : VcpuVmRootA st ar AR) + (hU : UbFootDisjA Z OS Ub) (hLD : TcbLinkDisjA Z (L.ksb s)) + (hUbUser : ∀ q ∈ Ub, inUserFrame (specPtr q) σ) + (hCs : ∀ c ∈ Z.Cs, c.val < 128) (hcpu : (L.cpu s).val < 128) + (hExt : ∀ b, VcPage Z.V b → ¬ ExtFootE Z.X Z.Y Z.T2 b) + (hSw : ∀ b, VcPage Z.V b → ∀ y, SlotWA Z.th y → + b ≠ y ∧ b ≠ wa y 8#u64 ∧ b ≠ wa y 16#u64 ∧ b ≠ wa y 24#u64) + (hfa : ∀ q, FootA (L := L) Z s q → Aligned q) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hIdleT : ∀ c ∈ Z.Cs, L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) ∈ Z.T) + (hksTcb : ∀ t ∈ Z.T, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → + ∀ o : U64, o.val < ksSizeA → wa t K ≠ wa (L.ksb s) o) + (hSlotTcb : ∀ t ∈ Z.T, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → + ∀ y, SlotWA Z.th y → wa t K ≠ y ∧ wa t K ≠ wa y 8#u64 ∧ wa t K ≠ wa y 16#u64 ∧ wa t K ≠ wa y 24#u64) + (hOS_tcb : ∀ t ∈ Z.T, ∀ K : U64, (K = 3664#u64 ∨ K = 3672#u64) → ∀ y ∈ OS, + wa t K ≠ y ∧ wa t K ≠ wa y 8#u64 ∧ wa t K ≠ wa y 16#u64 ∧ wa t K ≠ wa y 24#u64) + {ct : U64} (hct : ct ∈ Z.T) + (hBufNotS2 : ∀ p sz r d mp asid va, + capAtA (L.mem s) (sbWordA ct) ≠ .Frame p sz r d mp true asid va) + (hRunSched : L.mem s (runVcpuA ct) ≠ 0#u64 → + L.mem s (scA ct) ≠ 0#u64 ∧ L.rmax s (L.mem s (scA ct)) ≠ 0#u64 ∧ + (L.mem s (inqA ct) &&& sched.Q_RELEASE) = 0#u64 ∧ + specPtr ct ∉ σ.releaseQueue ∧ + Aligned (maxA (L.mem s (scA ct))) ∧ stA ct ≠ maxA (L.mem s (scA ct))) + (hNotRel : specPtr ct ∉ σ.releaseQueue) + (hSlotSt : ∀ y, SlotWA Z.th y → stA ct ≠ y ∧ stA ct ≠ wa y 8#u64 ∧ stA ct ≠ wa y 16#u64 ∧ stA ct ≠ wa y 24#u64) + (hOS_st : ∀ y ∈ OS, stA ct ≠ y ∧ stA ct ≠ wa y 8#u64 ∧ stA ct ≠ wa y 16#u64 ∧ stA ct ≠ wa y 24#u64) + (hKs_st : ∀ o : U64, o.val < ksSizeA → stA ct ≠ wa (L.ksb s) o) + (hSc_st : ∀ c ∈ Z.C, stA ct ≠ wa c 24#u64 ∧ stA ct ≠ wa c 72#u64) + (hVs_st : ¬ VsFootA Z.Pp Z.S1 Z.Y (L.ksb s) (stA ct)) + (hExt_st : ¬ ExtFootE Z.X Z.Y Z.T2 (stA ct)) + {stop : U64} {stopSpec : RunStop} (hstop : stop.bv = stopSpec.word) (w1 w2 w3 : U64) : + vcpu.complete_run st ar s a ct stop w1 w2 w3 ⦃ res => + ∃ hw' σ', + fl4CompleteRun (Z := Unit) (specPtr ct) stopSpec w1.bv w2.bv w3.bv σ = ({((), σ')}, False) ∧ + VcpuXferStateA (L := L) AR { Z with hw := hw' } OS Ub m res.2 res.1 σ' ∧ + L.ksb res.1 = L.ksb s ∧ L.cpu res.1 = L.cpu s ∧ + L.mem res.1 (runVcpuA ct) = 0#u64 ∧ + (L.mem s (runVcpuA ct) ≠ 0#u64 → L.mem res.1 (wa (L.mem s (runVcpuA ct)) 24#u64) = 0#u64) ∧ + (∀ v ∈ Z.V, L.mem res.1 v = L.mem s v) ∧ + (∀ t ∈ Z.T, L.mem res.1 (wa t 3664#u64) = L.mem s (wa t 3664#u64)) ∧ + VcpuRunPresA L Z OS s res.1 σ σ' ⦄ := by + unfold vcpu.complete_run + have hrv_const : layout.tcb.RUN_VCPU = ok 3672#u64 := layout_tcb_run_vcpu_eqA + have hrun_eq : wa ct 3672#u64 = runVcpuA ct := by unfold runVcpuA; rfl + rw [hrv_const, bind_ok, lift_ok, bind_ok, hrun_eq, L.read_eq, bind_ok] + obtain ⟨xct, hkc, -, hrv_spec⟩ := X.vcpu.core.tcb ct hct + by_cases hvc0 : L.mem s (runVcpuA ct) = 0#u64 + · rw [if_pos hvc0, WP.spec_ok] + have hsp : fl4CompleteRun (Z := Unit) (specPtr ct) stopSpec w1.bv w2.bv w3.bv σ = ({((), σ)}, False) := by + unfold fl4CompleteRun + rw [nd_bind_oneSc (getRunVcpu_eqA hkc), hrv_spec, hvc0]; rfl + exact ⟨Z.hw, σ, hsp, X, rfl, rfl, hvc0, fun h => absurd hvc0 h, fun _ _ => rfl, fun _ _ => rfl, VcpuRunPresA.refl L Z OS s σ⟩ + · rw [if_neg hvc0] + set vc := L.mem s (runVcpuA ct) + obtain ⟨hvcV, hlink⟩ := X.vcpu.run_c2v ct hct vc rfl hvc0 + obtain ⟨v, hkv, -, -⟩ := X.vcpu.core.obj vc hvcV + have hoff_const : vcpu.RUN_OFFSET = 32#u64 := by unfold vcpu.RUN_OFFSET; rfl + have hmask_const : vcpu.RUN_OUT_MASK = 40#u64 := by unfold vcpu.RUN_OUT_MASK; rfl + rw [hoff_const, lift_ok, bind_ok, L.read_eq, bind_ok, + hmask_const, lift_ok, bind_ok, L.read_eq, bind_ok] + set offset := L.mem s (wa vc 32#u64) + set out_mask := L.mem s (wa vc 40#u64) + have hoff_bv : offset.bv = v.vcpuExt.runOffset := X.vcpu.runOff vc hvcV v hkv + have hmask_bv : out_mask.bv = v.vcpuExt.runOutMask := (X.vcpu.runOut vc hvcV v hkv).1 + refine WP.spec_bind (hAK.vcpu_flush_state a Z.hw vc out_mask X.j.1.ar) ?_ + intro a1 hAR1 + have X_a1 : VcpuXferStateA (L := L) AR Z OS Ub m a1 s σ := + ⟨⟨{ X.j.1 with ar := hAR1 }, X.j.2⟩, X.tr, X.car, X.vcpu, X.fmem, X.regs⟩ + -- Step 1: optional `read_state_to_frame` when `out_mask ≠ 0` + refine WP.spec_bind (Pₘ := fun s1 => + ∃ σ1, + (NonDet.when (v.vcpuExt.runOutMask ≠ 0) do + let buf ← getCap (specPtr ct, tcbCnodeIndex 2) + match frameRightsDev buf, stateWindow buf v.vcpuExt.runOffset with + | some (R, false), some (some frame) => + NonDet.when (R = vmReadWrite) <| fl4ReadStateToFrame (specPtr vc) frame v.vcpuExt.runOutMask + | _, _ => pure ()) σ = ({((), σ1)}, False) ∧ + VcpuXferStateA (L := L) AR Z OS Ub m a1 s1 σ1 ∧ + L.ksb s1 = L.ksb s ∧ L.cpu s1 = L.cpu s ∧ + L.mem s1 (wa vc 24#u64) = ct ∧ L.mem s1 (runVcpuA ct) = vc ∧ + L.mem s1 (scA ct) = L.mem s (scA ct) ∧ L.rmax s1 (L.mem s1 (scA ct)) = L.rmax s (L.mem s (scA ct)) ∧ + L.mem s1 (inqA ct) = L.mem s (inqA ct) ∧ + σ1.kheap = σ.kheap ∧ + (∀ q, Aligned q → q ∉ Ub → L.mem s1 q = L.mem s q)) ?_ ?_ + · by_cases hm0 : (out_mask != 0#u64) = true + · have hmb : v.vcpuExt.runOutMask ≠ 0 := by + rw [← hmask_bv]; intro e + have : out_mask = 0#u64 := u64_eq_of_bv_eq e; rw [this] at hm0; exact absurd hm0 (by decide) + rw [if_pos hm0] + have hsbuf : obj.Tcb.slot ct layout.tcb.SLOT_BUFFER = ok (sbWordA ct) := by + simp [obj.Tcb.slot, lift, sbWordA, layout.tcb.SLOT_BUFFER, layout.cte.SIZE] + have hcap_ok : obj.Slot.cap st (sbWordA ct) s = ok (capAtA (L.mem s) (sbWordA ct)) := by + rw [Fl4ProofsA64.P1.W7.R.ST.capA_eq, ← capAtA_eq] + rw [hsbuf, bind_ok, hcap_ok, bind_ok] + have hth_ct : Z.th ct = some .tcb := (X.j.1.thT ct).2 hct + have his_buf : IsSlotA Z.th (sbWordA ct) := + ⟨ct, Or.inr ⟨hth_ct, 2, by decide, (bufSlot_eqA ct).symm⟩⟩ + have hos_buf : sbWordA ct ∈ OS := hsub _ his_buf + obtain ⟨xct', hkc', hslots⟩ := X.j.1.st.tcb ct hth_ct + rw [hkc] at hkc'; cases hkc' + have hmap2 : tcbCnodeMap xct (tcbCnodeIndex 2) = some xct.tcbIpcframe := by + unfold tcbCnodeMap tcbCnodeIndex + simp only [Function.update] + rw [dif_neg (by decide), dif_neg (by decide), dif_neg (by decide), dif_neg (by decide), dif_pos trivial] + have hcapRel : CapRelA (capAtA (L.mem s) (sbWordA ct)) xct.tcbIpcframe := by + obtain ⟨ac, hac, hrel⟩ := hslots 2 (by decide) + rw [bufSlot_eqA ct] at hrel + change tcbCnodeMap xct (tcbCnodeIndex 2) = some ac at hac + rw [hmap2] at hac; cases hac; exact hrel + have hgetCap : getCap (Z := Unit) (specPtr ct, tcbCnodeIndex 2) σ = ({(xct.tcbIpcframe, σ)}, False) := by + unfold getCap; dsimp only + have hobj : getObject (Z := Unit) (specPtr ct) σ = ({(.tcb xct, σ)}, False) := by + unfold getObject NonDet.getsThe readObject + simp [NonDet.gets, hkc, NonDet.assertOpt]; rfl + rw [nd_bind_oneSc hobj]; dsimp only + rw [nd_bind_oneSc (m := pure (tcbCnodeMap xct)) rfl, hmap2]; rfl + have hnoop : ∀ (hsp : (match frameRightsDev xct.tcbIpcframe, stateWindow xct.tcbIpcframe v.vcpuExt.runOffset with + | some (R, false), some (some frame) => + NonDet.when (R = vmReadWrite) <| fl4ReadStateToFrame (specPtr vc) frame v.vcpuExt.runOutMask + | _, _ => pure ()) σ = ({((), σ)}, False)), + (ok s : Result S) ⦃ s1 => + ∃ σ1, + (NonDet.when (v.vcpuExt.runOutMask ≠ 0) do + let buf ← getCap (specPtr ct, tcbCnodeIndex 2) + match frameRightsDev buf, stateWindow buf v.vcpuExt.runOffset with + | some (R, false), some (some frame) => + NonDet.when (R = vmReadWrite) <| fl4ReadStateToFrame (specPtr vc) frame v.vcpuExt.runOutMask + | _, _ => pure ()) σ = ({((), σ1)}, False) ∧ + VcpuXferStateA (L := L) AR Z OS Ub m a1 s1 σ1 ∧ + L.ksb s1 = L.ksb s ∧ L.cpu s1 = L.cpu s ∧ + L.mem s1 (wa vc 24#u64) = ct ∧ L.mem s1 (runVcpuA ct) = vc ∧ + L.mem s1 (scA ct) = L.mem s (scA ct) ∧ L.rmax s1 (L.mem s1 (scA ct)) = L.rmax s (L.mem s (scA ct)) ∧ + L.mem s1 (inqA ct) = L.mem s (inqA ct) ∧ + σ1.kheap = σ.kheap ∧ + (∀ q, Aligned q → q ∉ Ub → L.mem s1 q = L.mem s q) ⦄ := by + intro hsp; rw [WP.spec_ok] + refine ⟨σ, ?_, X_a1, rfl, rfl, hlink, rfl, rfl, rfl, rfl, rfl, fun _ _ _ => rfl⟩ + unfold NonDet.when; rw [if_pos hmb, nd_bind_oneSc hgetCap]; exact hsp + rcases hcapRel with ⟨hwf, habs⟩ + cases hc_eq : capAtA (L.mem s) (sbWordA ct) with + | Frame ptr size rights is_device mp is_s2 asid va => + have hs2f : is_s2 = false := by + cases is_s2 with + | false => rfl + | true => exfalso; exact hBufNotS2 ptr size rights is_device mp asid va hc_eq + subst hs2f + rw [hc_eq] at habs hwf + have hbuf_eq : xct.tcbIpcframe = .archObjectCap (.frameCap (specPtr ptr) (vmRightsOfA rights) + (frameSizeOfA size) is_device (mapDataA mp asid va)) := by + rw [← habs]; rfl + have hfrd : frameRightsDev xct.tcbIpcframe = some (vmRightsOfA rights, is_device) := by + rw [hbuf_eq]; rfl + have hbits_ok : capability.FrameSize.bits size = ok ⟨BitVec.ofNat 64 (frameBitsA size)⟩ := by + cases size <;> rfl + have hbits_eq : frameBitsA size = pageBitsForSize (frameSizeOfA size) := by + cases size <;> rfl + have hbits_lt64 : frameBitsA size < 64 := by cases size <;> decide + have hbits_and : (⟨BitVec.ofNat 64 (frameBitsA size)⟩ &&& 63#u64 : U64) = + ⟨BitVec.ofNat 64 (frameBitsA size)⟩ := by cases size <;> exact u64_eq_of_bv_eq (by decide) + have hbits_val : (⟨BitVec.ofNat 64 (frameBitsA size)⟩ : U64).val = frameBitsA size := by + cases size <;> rfl + dsimp only + rw [hbits_ok, bind_ok, lift_ok, bind_ok, hbits_and, + u64_shl_u64_ok (by rw [hbits_val]; exact hbits_lt64), hbits_val, bind_ok] + set sz1 : U64 := ⟨(1#u64 : U64).bv <<< frameBitsA size⟩ + have hsz1_bv : sz1.bv = (1 : Word) <<< pageBitsForSize (frameSizeOfA size) := by + rw [← hbits_eq]; rfl + have hsz1_val : sz1.val = 2 ^ frameBitsA size := by cases size <;> rfl + have hwf_r : 1 ≤ rights.val ∧ rights.val ≤ 3 := ⟨hwf.1, hwf.2.1⟩ + have hrw_iff : rights = capability.VM_READ_WRITE ↔ vmRightsOfA rights = vmReadWrite := by + have h3 : capability.VM_READ_WRITE.val = 3 := by unfold capability.VM_READ_WRITE; rfl + unfold vmRightsOfA + constructor + · intro e; rw [e, h3]; rfl + · intro e + by_cases hr3 : rights.val = 3 + · exact UScalar.eq_of_val_eq (by omega) + · by_cases hr2 : rights.val = 2 + · rw [if_neg hr3, if_pos hr2] at e; exfalso + have hm := (Set.ext_iff.1 e .allowWrite).2 (by simp [vmReadWrite]) + simp [vmReadOnly] at hm + · rw [if_neg hr3, if_neg hr2] at e; exfalso + have hm := (Set.ext_iff.1 e .allowRead).2 (by simp [vmReadWrite]) + simp [vmKernelOnly] at hm + by_cases hdev : is_device = true + · rw [if_pos hdev] + exact hnoop (by rw [hfrd, hdev]; rfl) + · have hdevf : is_device = false := by cases is_device <;> simp_all + rw [if_neg hdev] + by_cases hrw : rights = capability.VM_READ_WRITE + · rw [if_pos hrw, lift_ok, bind_ok] + by_cases hal64 : (offset &&& 63#u64) = 0#u64 + · rw [if_pos hal64] + have hal64' : offset.bv &&& 63 = 0 := congrArg UScalar.bv hal64 + by_cases hlt_sz : offset < sz1 + · rw [if_pos hlt_sz] + have hlt_sz_val : offset.val < sz1.val := hlt_sz + rw [u64_sub_ok (by omega), bind_ok] + have hsub_val : (⟨BitVec.ofNat 64 (sz1.val - offset.val)⟩ : U64).val = sz1.val - offset.val := by + have hsz_lt : sz1.val < 2 ^ 64 := by rw [hsz1_val]; cases size <;> decide + change (BitVec.ofNat 64 _).toNat = _ + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + have hsub_bv : (⟨BitVec.ofNat 64 (sz1.val - offset.val)⟩ : U64).bv = sz1.bv - offset.bv := by + apply BitVec.eq_of_toNat_eq + change (⟨BitVec.ofNat 64 (sz1.val - offset.val)⟩ : U64).val = (sz1.bv - offset.bv).toNat + rw [hsub_val, BitVec.toNat_sub_of_le (by exact le_of_lt hlt_sz_val)]; rfl + have hsm_sz : fl4_abi.consts.vcpu_state_mask.SIZE.val = 1024 := by + unfold fl4_abi.consts.vcpu_state_mask.SIZE; rfl + have hsmSize_nat : smSize.toNat = 1024 := rfl + by_cases hge1024 : (⟨BitVec.ofNat 64 (sz1.val - offset.val)⟩ : U64) ≥ fl4_abi.consts.vcpu_state_mask.SIZE + · rw [if_pos hge1024, lift_ok, bind_ok] + have hge1024_val : 1024 ≤ sz1.val - offset.val := by + have : fl4_abi.consts.vcpu_state_mask.SIZE.val ≤ (⟨BitVec.ofNat 64 (sz1.val - offset.val)⟩ : U64).val := hge1024 + omega + obtain ⟨hptr_al, hptr_fit, hptr_ub⟩ := X.fmem.frames (sbWordA ct) hos_buf ptr size rights mp false asid va (by rwa [hdevf] at hc_eq) + have hoff_mod8 : offset.val % 8 = 0 := by + have h64 : offset.val % 64 = 0 := by + have := congrArg BitVec.toNat hal64' + simpa [BitVec.toNat_and, Nat.and_two_pow_sub_one_eq_mod _ 6] using this + omega + set frame : U64 := wa ptr offset + have hframe_val : frame.val = ptr.val + offset.val := by + rw [wa_valP, Nat.mod_eq_of_lt (by omega)] + have hWin : FrameWinA Ub frame := by + refine ⟨by omega, fun k hk => ?_⟩ + have hwa_eq : wa frame ⟨BitVec.ofNat 64 (8 * k)⟩ = wa ptr ⟨BitVec.ofNat 64 (offset.val + 8 * k)⟩ := by + apply UScalar.eq_of_val_eq + rw [wa_valP, hframe_val, wa_valP] + have h1 : (⟨BitVec.ofNat 64 (8 * k)⟩ : U64).val = 8 * k := by + change (BitVec.ofNat 64 _).toNat = _; rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + have h2 : (⟨BitVec.ofNat 64 (offset.val + 8 * k)⟩ : U64).val = offset.val + 8 * k := by + change (BitVec.ofNat 64 _).toNat = _; rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + rw [h1, h2]; congr 1; omega + rw [hwa_eq] + have hok_val : (⟨BitVec.ofNat 64 (offset.val + 8 * k)⟩ : U64).val = offset.val + 8 * k := by + change (BitVec.ofNat 64 _).toNat = _; rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + exact hptr_ub _ (by omega) (by omega) + have hsp_frame : specPtr frame = specPtr ptr + offset.bv := by + have hwa_bv : frame.bv = ptr.bv + offset.bv := by + apply BitVec.eq_of_toNat_eq + change frame.val = (ptr.bv + offset.bv).toNat + rw [hframe_val, BitVec.toNat_add]; exact (Nat.mod_eq_of_lt (by omega)).symm + unfold specPtr; rw [hwa_bv, BitVec.add_assoc, BitVec.add_comm offset.bv pptrShiftA, ← BitVec.add_assoc] + have huf : ∀ k < 128, inUserFrame (bufAddr (specPtr frame) k) σ := fun k hk => by + have hq := hUbUser _ (hWin.2 k hk) + rwa [specPtr_wa_wordA (by have := hWin.1; omega)] at hq + refine WP.spec_mono (WP.spec_and (read_state_to_frame_refinesA X_a1 hP hU hCs hcpu hfa hsub hIdleT hvcV hWin huf (mask := out_mask)) + (read_state_to_frame_keeps_offUbA hP hWin X_a1.fmem.aligned)) ?_ + rintro s1 ⟨⟨σ1, hsp1, X1, hkh1, hksb1, hcpu1, _, _⟩, hmem_off⟩ + refine ⟨σ1, ?_, X1, hksb1, hcpu1, ?_, ?_, ?_, ?_, ?_, hkh1, hmem_off⟩ + · unfold NonDet.when; rw [if_pos hmb, nd_bind_oneSc hgetCap, hfrd, hdevf, hbuf_eq] + unfold stateWindow; dsimp only + rw [← hoff_bv, ← hsz1_bv] + have hsw_ok : ¬ (offset.bv &&& 63 ≠ 0 ∨ offset.bv ≥ sz1.bv ∨ sz1.bv - offset.bv < smSize) := by + rintro (h | h | h) + · exact h hal64' + · have : sz1.val ≤ offset.val := h; omega + · rw [← hsub_bv] at h; have : (⟨BitVec.ofNat 64 (sz1.val - offset.val)⟩ : U64).val < smSize.toNat := h; omega + rw [if_neg hsw_ok]; dsimp only + rw [if_pos (hrw_iff.1 hrw), ← hsp_frame, ← hmask_bv]; exact hsp1 + · exact hmem_off (wa vc 24#u64) (X.vcpu.place.vcPage_al (vcpuHdr_in_vcPage hvcV (o := 24#u64) (by decide) (by decide))) + (fun hub => vc_disj_ubA X.vcpu.place (vcpuHdr_in_vcPage hvcV (o := 24#u64) (by decide) (by decide)) hub rfl) ▸ hlink + · exact hmem_off (runVcpuA ct) (by unfold runVcpuA; exact aligned_wa (X.vcpu.place.al_t ct hct) 3672#u64 (by decide)) + (fun hub => X.fmem.disj_tcb _ hub ct hct 3672#u64 (by decide) (by unfold runVcpuA; rfl)) + · exact hmem_off (scA ct) (by unfold scA; exact aligned_wa (X.vcpu.place.al_t ct hct) 3464#u64 (by decide)) + (fun hub => X.fmem.disj_tcb _ hub ct hct 3464#u64 (by decide) (by unfold scA; rfl)) + · have hsc_eq : L.mem s1 (scA ct) = L.mem s (scA ct) := + hmem_off (scA ct) (by unfold scA; exact aligned_wa (X.vcpu.place.al_t ct hct) 3464#u64 (by decide)) + (fun hub => X.fmem.disj_tcb _ hub ct hct 3464#u64 (by decide) (by unfold scA; rfl)) + rw [hsc_eq] + obtain ⟨hsc0, -, -, -, hma0, -⟩ := hRunSched hvc0 + have hscC : L.mem s (scA ct) ∈ Z.C := X.j.1.rest.scMem ct hct hsc0 + exact hmem_off (maxA (L.mem s (scA ct))) hma0 + (fun hub => X.fmem.disj_sc _ hub _ hscC 72#u64 (by decide) (maxA_eqA _)) + · exact hmem_off (inqA ct) (by unfold inqA; exact aligned_wa (X.vcpu.place.al_t ct hct) 3592#u64 (by decide)) + (fun hub => X.fmem.disj_tcb _ hub ct hct 3592#u64 (by decide) (by unfold inqA; rfl)) + · rw [if_neg hge1024] + exact hnoop (by + rw [hfrd, hdevf, hbuf_eq]; unfold stateWindow; dsimp only + rw [← hoff_bv, ← hsz1_bv, if_pos (Or.inr (Or.inr (by + rw [← hsub_bv]; change (⟨BitVec.ofNat 64 (sz1.val - offset.val)⟩ : U64).val < smSize.toNat + have : ¬ fl4_abi.consts.vcpu_state_mask.SIZE.val ≤ (⟨BitVec.ofNat 64 (sz1.val - offset.val)⟩ : U64).val := hge1024 + omega)))]; rfl) + · rw [if_neg hlt_sz] + exact hnoop (by + rw [hfrd, hdevf, hbuf_eq]; unfold stateWindow; dsimp only + rw [← hoff_bv, ← hsz1_bv, if_pos (Or.inr (Or.inl (by + change sz1.val ≤ offset.val; have : ¬ offset.val < sz1.val := hlt_sz; omega)))]; rfl) + · rw [if_neg hal64] + exact hnoop (by + rw [hfrd, hdevf, hbuf_eq]; unfold stateWindow; dsimp only + rw [← hoff_bv, ← hsz1_bv, if_pos (Or.inl (fun e => hal64 (u64_eq_of_bv_eq (a := offset &&& 63#u64) (b := 0#u64) e)))]; rfl) + · rw [if_neg hrw] + exact hnoop (by + rw [hfrd, hdevf, hbuf_eq]; unfold stateWindow; dsimp only + split_ifs + · rfl + · dsimp only; unfold NonDet.when; rw [if_neg (fun e => hrw (hrw_iff.2 e))]; rfl) + | PageTable p m_pt asid va => + rw [hc_eq] at habs + cases m_pt + · exact hnoop (by + have : frameRightsDev xct.tcbIpcframe = none := by rw [← habs]; rfl + rw [this]; rfl) + · exact hnoop (by + cases hbf : xct.tcbIpcframe with + | archObjectCap ac => + cases ac with + | pageTableCap _ _ _ => rfl + | _ => rw [hbf] at habs; cases habs + | _ => rw [hbf] at habs; cases habs) + | _ => + rw [hc_eq] at habs hwf + dsimp only + first + | exact False.elim hwf + | exact hnoop (by + have : frameRightsDev xct.tcbIpcframe = none := by rw [← habs]; rfl + rw [this]; rfl) + · have hmb : ¬ v.vcpuExt.runOutMask ≠ 0 := by + rw [← hmask_bv]; intro hne; exact hm0 (bne_iff_ne.2 (fun e => hne (congrArg UScalar.bv e))) + rw [if_neg hm0, WP.spec_ok] + refine ⟨σ, ?_, X_a1, rfl, rfl, hlink, rfl, rfl, rfl, rfl, rfl, fun _ _ _ => rfl⟩ + unfold NonDet.when; rw [if_neg hmb]; rfl + -- Bind Step 1 and run Steps 2..5 (clear run link, 7 TCB reg writes, set_thread_state TS_RUNNING, optional set_vm_root) + · rintro s1 ⟨σ1, hsp1, X1, hksb1, hcpu1, hlink1, hback1, hsc1, hrm1, hiq1, hkh1, hmem_off1⟩ + have hkv1 : σ1.kheap (specPtr vc) = some (.archObj (.vcpu v)) := by rw [hkh1]; exact hkv + have hkc1 : σ1.kheap (specPtr ct) = some (.tcb xct) := by rw [hkh1]; exact hkc + have hfa1 : ∀ q, FootA (L := L) Z s1 q → Aligned q := + fun q hq => hfa q (footA_of_ksb_cpuA hksb1 hcpu1 hq) + have hLD1 : TcbLinkDisjA Z (L.ksb s1) := by rw [hksb1]; exact hLD + have hcpu1' : (L.cpu s1).val < 128 := by rw [hcpu1]; exact hcpu + have hIdle1 : ∀ c ∈ Z.Cs, L.mem s1 (L.nodeAt s1 c layout.node.IDLE_THREAD) ∈ Z.T := fun c hc => X1.j.1.idleT c hc + have hrc_const : vcpu.RUN_CALLER = 24#u64 := by unfold vcpu.RUN_CALLER; rfl + rw [hrc_const, lift_ok, bind_ok, L.write_eq, bind_ok, bind_ok, L.write_eq, bind_ok] + -- Step 2: clear run link + set s2 := L.wr s1 (wa vc 24#u64) 0#u64 + set s3 := L.wr s2 (runVcpuA ct) 0#u64 + set σ3 := updKSc (updKSc σ1 (specPtr vc) (.archObj (.vcpu { v with vcpuExt := { v.vcpuExt with runCaller := none } }))) + (specPtr ct) (.tcb { xct with tcbExt := { xct.tcbExt with tcbRunVcpu := none } }) + have X3 : VcpuXferStateA (L := L) AR Z OS Ub m a1 s3 σ3 := + X1.clear_run_link hU hLD1 hCs hcpu1' hExt hSw hfa1 hsub hIdle1 hOS_tcb hvcV hct hlink1 hback1 hkv1 hkc1 + -- Step 3: 7 TCB user-register writes (BADGE, X2..X5, MSG_INFO, FAULT_IP := NEXT_IP) + have hksb3 : L.ksb s3 = L.ksb s := by rw [L.ksb_wr, L.ksb_wr, hksb1] + have hcpu3 : L.cpu s3 = L.cpu s := by rw [L.cpu_wr, L.cpu_wr, hcpu1] + have hksTcb3 : ∀ t ∈ Z.T, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → + ∀ o : U64, o.val < ksSizeA → wa t K ≠ wa (L.ksb s3) o := by rw [hksb3]; exact hksTcb + have hfa3 : ∀ q, FootA (L := L) Z s3 q → Aligned q := + fun q hq => hfa q (footA_of_ksb_cpuA hksb3 hcpu3 hq) + have hIdle3 : ∀ c ∈ Z.Cs, L.mem s3 (L.nodeAt s3 c layout.node.IDLE_THREAD) ∈ Z.T := fun c hc => X3.j.1.idleT c hc + have hkc3 : σ3.kheap (specPtr ct) = some (.tcb { xct with tcbExt := { xct.tcbExt with tcbRunVcpu := none } }) := + updKSc_same _ _ _ + refine complete_run_regs_stepA X3 hU hCs (by rw [hcpu3]; exact hcpu) hfa3 hsub hIdle3 hksTcb3 hSlotTcb hct hkc3 hstop w1 w2 w3 ?_ + intro s10 σ10 x10 X10 hsp_regs hkc10 hx10_st hksb10' hcpu10' hmem10 huf10 + -- Step 4: `sched.set_thread_state ... TS_RUNNING` + optional `vspace.set_vm_root` + have hksb10 : L.ksb s10 = L.ksb s := hksb10'.trans hksb3 + have hcpu10 : L.cpu s10 = L.cpu s := hcpu10'.trans hcpu3 + have hta_ct := hU.tcb_al ct hct + have hreg_ne_tcb : ∀ (r : Register) (o : U64), 3328 ≤ o.val → o.val < 4096 → + Endp.regA ct (regIdxA r) ≠ wa ct o := fun r o h1 h2 => by + obtain ⟨hKval, hreg_eq⟩ := regA_eq_wa_KA ct r + have hlt := regIdxA_lt r + rw [hreg_eq]; exact field_ne hta_ct hta_ct (by omega) h2 (fun _ => by omega) + have hreg_pres_tcb : ∀ (o : U64), 3328 ≤ o.val → o.val < 4096 → o.val % 8 = 0 → + L.mem s10 (wa ct o) = L.mem s3 (wa ct o) := fun o h1 h2 h8 => + hmem10 (wa ct o) (aligned_wa (by unfold Aligned; have := hta_ct.align; omega) o h8) (fun r => hreg_ne_tcb r o h1 h2) + have hno_run10 : L.mem s10 (runVcpuA ct) = 0#u64 := by + have hr_eq : runVcpuA ct = wa ct 3672#u64 := by unfold runVcpuA; rfl + rw [hr_eq, hreg_pres_tcb 3672#u64 (by decide) (by decide) (by decide), ← hr_eq] + exact L.mem_wr_same (by rw [hr_eq]; exact aligned_wa (by unfold Aligned; have := hta_ct.align; omega) 3672#u64 (by decide)) + obtain ⟨hsc0, hrm0, hiq0, hnr0, hma0, hmx0⟩ := hRunSched hvc0 + have hhal_rc : Aligned (wa vc 24#u64) := X.vcpu.place.vcPage_al (vcpuHdr_in_vcPage hvcV (o := 24#u64) (by decide) (by decide)) + have hhal_rv : Aligned (runVcpuA ct) := by + unfold runVcpuA; exact aligned_wa (by unfold Aligned; have := hta_ct.align; omega) 3672#u64 (by decide) + have hsc10 : L.mem s10 (scA ct) ≠ 0#u64 := by + have hsc_eq : scA ct = wa ct 3464#u64 := by unfold scA; rfl + rw [hsc_eq, hreg_pres_tcb 3464#u64 (by decide) (by decide) (by decide)] + have hal_sc : Aligned (wa ct 3464#u64) := aligned_wa (by unfold Aligned; have := hta_ct.align; omega) 3464#u64 (by decide) + rw [mem_wr_neA hhal_rv hal_sc (by unfold runVcpuA; exact field_ne hta_ct hta_ct (by decide) (by decide) (fun _ => by decide)), + mem_wr_neA hhal_rc hal_sc (X.vcpu.place.disj_tcb vc hvcV ct hct 24#u64 3464#u64 (by decide) (by decide)), + ← hsc_eq, hsc1] + exact hsc0 + have hsc10_eq : L.mem s10 (scA ct) = L.mem s (scA ct) := by + have hsc_eq : scA ct = wa ct 3464#u64 := by unfold scA; rfl + rw [hsc_eq, hreg_pres_tcb 3464#u64 (by decide) (by decide) (by decide)] + have hal_sc : Aligned (wa ct 3464#u64) := aligned_wa (by unfold Aligned; have := hta_ct.align; omega) 3464#u64 (by decide) + rw [mem_wr_neA hhal_rv hal_sc (by unfold runVcpuA; exact field_ne hta_ct hta_ct (by decide) (by decide) (fun _ => by decide)), + mem_wr_neA hhal_rc hal_sc (X.vcpu.place.disj_tcb vc hvcV ct hct 24#u64 3464#u64 (by decide) (by decide)), + ← hsc_eq, hsc1] + have hscC : L.mem s (scA ct) ∈ Z.C := X.j.1.rest.scMem ct hct hsc0 + have hrm10 : L.rmax s10 (L.mem s10 (scA ct)) ≠ 0#u64 := by + rw [hsc10_eq]; unfold LawfulStore.rmax + have hreg_ne_sc : ∀ r : Register, Endp.regA ct (regIdxA r) ≠ maxA (L.mem s (scA ct)) := fun r => by + obtain ⟨hKval, hreg_eq⟩ := regA_eq_wa_KA ct r; have hlt := regIdxA_lt r + rw [hreg_eq, maxA_eqA]; exact hU.tcb_offSc ct hct _ (by omega) (by omega) _ hscC 72#u64 (by decide) + rw [hmem10 (maxA (L.mem s (scA ct))) hma0 hreg_ne_sc, + mem_wr_neA hhal_rv hma0 (by unfold runVcpuA; rw [maxA_eqA]; exact (hLD.offSc ct hct 3672#u64 (Or.inr rfl) _ hscC).2), + mem_wr_neA hhal_rc hma0 (by rw [maxA_eqA]; exact X.vcpu.place.disj_sc vc hvcV _ hscC 24#u64 72#u64 (by decide) (by decide))] + unfold LawfulStore.rmax at hrm1 hrm0 + rw [← hsc1, hrm1]; exact hrm0 + have hiq10 : (L.mem s10 (inqA ct) &&& sched.Q_RELEASE) = 0#u64 := by + have hiq_eq : inqA ct = wa ct 3592#u64 := by unfold inqA; rfl + rw [hiq_eq, hreg_pres_tcb 3592#u64 (by decide) (by decide) (by decide)] + have hal_iq : Aligned (wa ct 3592#u64) := aligned_wa (by unfold Aligned; have := hta_ct.align; omega) 3592#u64 (by decide) + rw [mem_wr_neA hhal_rv hal_iq (by unfold runVcpuA; exact field_ne hta_ct hta_ct (by decide) (by decide) (fun _ => by decide)), + mem_wr_neA hhal_rc hal_iq (X.vcpu.place.disj_tcb vc hvcV ct hct 24#u64 3592#u64 (by decide) (by decide)), + ← hiq_eq, hiq1] + exact hiq0 + have hvm0 : x10.tcbState = .ext .runningVM := by + rw [hx10_st] + have hst_vm : L.mem s (stA ct) = sched.TS_RUNNING_VM := X.vcpu.run_state ct hct hvc0 + obtain ⟨xt0, hkt0, hst_rel, -⟩ := X.j.1.sch.tcb ct hct + have hx_eq : xt0 = xct := by rw [hkc] at hkt0; cases hkt0; rfl + rw [hx_eq, hst_vm] at hst_rel + cases hst_x : xct.tcbState with + | ext e => cases e <;> simp [hst_x, tsCodeA] at hst_rel ⊢ + | _ => simp [hst_x, tsCodeA] at hst_rel + refine WP.spec_bind (X10.set_state_running_from_vm hU hCs (by rw [hcpu10]; exact hcpu) + (fun q hq => hfa q (footA_of_ksb_cpuA hksb10 hcpu10 hq)) hsub + (fun c hc => X10.j.1.idleT c hc) hct hkc10 hvm0 hno_run10 hsc10 hrm10 hiq10 + (by rw [hsc10_eq]; exact hma0) (by rw [hsc10_eq]; exact hmx0) + (by rw [X10.j.1.rest.rel, hcpu10, ← X.j.1.rest.rel]; exact hnr0) + hSlotSt hOS_st (by rw [hksb10]; exact hKs_st) hSc_st + (by rw [hksb10]; exact hVs_st) hExt_st) ?_ + rintro s11 ⟨hs11_eq, σ11, hst11, X11, hσ11_eq⟩ + -- Build the full `fl4CompleteRun` spec equation + have hsp_full : fl4CompleteRun (Z := Unit) (specPtr ct) stopSpec w1.bv w2.bv w3.bv σ = ({((), σ11)}, False) := by + unfold fl4CompleteRun + have hsome_rv : xct.tcbExt.tcbRunVcpu = some (specPtr vc) := by + rw [hrv_spec]; unfold optRefA; rw [if_neg hvc0] + rw [nd_bind_oneSc (getRunVcpu_eqA hkc), hsome_rv]; dsimp only + rw [nd_bind_oneSc (fl4VcpuFlush_noneA X.vcpu.arch (specPtr vc)), + nd_bind_oneSc (getVcpu_eqA hkv)] + erw [nd_bind_oneSc hsp1] + rw [nd_bind_oneSc (fl4VcpuExtUpdate_eqA _ hkv1)] + have hne_ct_vc : specPtr ct ≠ specPtr vc := fun e => by rw [e, hkv] at hkc; cases hkc + have hkc1' : (updKSc σ1 (specPtr vc) (.archObj (.vcpu { v with vcpuExt := { v.vcpuExt with runCaller := none } }))).kheap (specPtr ct) = + some (.tcb xct) := by rw [updKSc_other _ hne_ct_vc]; exact hkc1 + rw [nd_bind_oneSc (setRunVcpu_eqA none hkc1'), hsp_regs] + exact hst11 + have hksb11 : L.ksb s11 = L.ksb s := by rw [hs11_eq, L.ksb_wr, hksb10] + have hcpu11 : L.cpu s11 = L.cpu s := by rw [hs11_eq, L.cpu_wr, hcpu10] + have hal_st10 : Aligned (stA ct) := by + rw [stA_eq]; exact aligned_wa (by unfold Aligned; have := hta_ct.align; omega) 3328#u64 (by decide) + have hrv11 : L.mem s11 (runVcpuA ct) = 0#u64 := by + rw [hs11_eq, mem_wr_neA hal_st10 hhal_rv (by rw [stA_eq]; unfold runVcpuA; exact field_ne hta_ct hta_ct (by decide) (by decide) (fun _ => by decide))] + exact hno_run10 + have hrc11 : L.mem s11 (wa vc 24#u64) = 0#u64 := by + rw [hs11_eq, mem_wr_neA hal_st10 hhal_rc (by rw [stA_eq]; exact (X.vcpu.place.disj_tcb vc hvcV ct hct 24#u64 3328#u64 (by decide) (by decide)).symm)] + have hreg_ne_rc : ∀ r : Register, Endp.regA ct (regIdxA r) ≠ wa vc 24#u64 := fun r => by + obtain ⟨hKval, hreg_eq⟩ := regA_eq_wa_KA ct r; have hlt := regIdxA_lt r + rw [hreg_eq]; exact (X.vcpu.place.disj_tcb vc hvcV ct hct 24#u64 _ (by decide) (by omega)).symm + rw [hmem10 (wa vc 24#u64) hhal_rc hreg_ne_rc, + mem_wr_neA hhal_rv hhal_rc (by unfold runVcpuA; exact (X.vcpu.place.disj_tcb vc hvcV ct hct 24#u64 3672#u64 (by decide) (by decide)).symm), + L.mem_wr_same hhal_rc] + have hmem_pres : ∀ q, Aligned q → q ∉ Ub → + wa vc 24#u64 ≠ q → runVcpuA ct ≠ q → (∀ r : Register, Endp.regA ct (regIdxA r) ≠ q) → + stA ct ≠ q → L.mem s11 q = L.mem s q := fun q hq hqu h1 h2 h3 h4 => by + rw [hs11_eq, mem_wr_neA hal_st10 hq h4, hmem10 q hq h3, + mem_wr_neA hhal_rv hq h2, mem_wr_neA hhal_rc hq h1, hmem_off1 q hq hqu] + have hpres11 : VcpuRunPresA L Z OS s s11 σ σ11 := by + refine ⟨?_, ?_, ?_, ?_, ?_, ?_⟩ + · intro y hy + have hya := hU.al_os y hy + have hd_vc := X.vcpu.place.disj_os _ (vcpuHdr_in_vcPage hvcV (o := 24#u64) (by decide) (by decide)) y hy + have hd_rv := hOS_tcb ct hct 3672#u64 (Or.inr rfl) y hy + have hd_st := hOS_st y hy + have hreg_os : ∀ r : Register, + Endp.regA ct (regIdxA r) ≠ y ∧ Endp.regA ct (regIdxA r) ≠ wa y 8#u64 ∧ + Endp.regA ct (regIdxA r) ≠ wa y 16#u64 ∧ Endp.regA ct (regIdxA r) ≠ wa y 24#u64 := fun r => by + obtain ⟨hKval, hreg_eq⟩ := regA_eq_wa_KA ct r; have hlt := regIdxA_lt r + rw [hreg_eq]; exact hU.tcb_offOs ct hct _ (by omega) (by omega) y hy + have hqu : ∀ o : U64, (o = 0#u64 ∨ o = 8#u64 ∨ o = 16#u64 ∨ o = 24#u64) → wa y o ∉ Ub := fun o ho hub => by + have := X.fmem.disj_os _ hub y hy + rcases ho with rfl | rfl | rfl | rfl + · have h0 : wa y 0#u64 = y := by apply UScalar.eq_of_val_eq; rw [wa_valP]; simp [UScalar.hBounds] + exact this.1 h0 + · exact this.2.1 rfl + · exact this.2.2.1 rfl + · exact this.2.2.2 rfl + have hqu0 : y ∉ Ub := fun hub => (X.fmem.disj_os _ hub y hy).1 rfl + exact ⟨hmem_pres y hya hqu0 hd_vc.1 (by unfold runVcpuA; exact hd_rv.1) (fun r => (hreg_os r).1) hd_st.1, + hmem_pres (wa y 8#u64) (aligned_wa hya 8#u64 (by decide)) (hqu 8#u64 (Or.inr (Or.inl rfl))) + hd_vc.2.1 (by unfold runVcpuA; exact hd_rv.2.1) (fun r => (hreg_os r).2.1) hd_st.2.1, + hmem_pres (wa y 16#u64) (aligned_wa hya 16#u64 (by decide)) (hqu 16#u64 (Or.inr (Or.inr (Or.inl rfl)))) + hd_vc.2.2.1 (by unfold runVcpuA; exact hd_rv.2.2.1) (fun r => (hreg_os r).2.2.1) hd_st.2.2.1, + hmem_pres (wa y 24#u64) (aligned_wa hya 24#u64 (by decide)) (hqu 24#u64 (Or.inr (Or.inr (Or.inr rfl)))) + hd_vc.2.2.2 (by unfold runVcpuA; exact hd_rv.2.2.2) (fun r => (hreg_os r).2.2.2) hd_st.2.2.2⟩ + · intro t ht + have hta_t := hU.tcb_al t ht + have ht_al : Aligned t := by unfold Aligned; have := hta_t.align; omega + have ht_field : ∀ (o : U64), (o = 3592#u64 ∨ o = 3464#u64) → L.mem s11 (wa t o) = L.mem s (wa t o) := fun o ho => by + have ho_lt : o.val < 4096 := by rcases ho with rfl | rfl <;> decide + have ho_8 : o.val % 8 = 0 := by rcases ho with rfl | rfl <;> decide + refine hmem_pres (wa t o) (aligned_wa ht_al o ho_8) + (fun hub => X.fmem.disj_tcb _ hub t ht o ho_lt rfl) + (X.vcpu.place.disj_tcb vc hvcV t ht 24#u64 o (by decide) ho_lt) + (by unfold runVcpuA; exact field_ne hta_ct hta_t (by decide) ho_lt (fun _ e => by rcases ho with rfl | rfl <;> exact absurd e (by decide))) + (fun r => by + obtain ⟨hKval, hreg_eq⟩ := regA_eq_wa_KA ct r; have hlt := regIdxA_lt r + have h3592 : (3592#u64 : U64).val = 3592 := rfl + have h3464 : (3464#u64 : U64).val = 3464 := rfl + rw [hreg_eq]; exact field_ne hta_ct hta_t (by omega) ho_lt (fun _ e => by rcases ho with rfl | rfl <;> omega)) + (by rw [stA_eq]; exact field_ne hta_ct hta_t (by decide) ho_lt (fun _ e => by rcases ho with rfl | rfl <;> exact absurd e (by decide))) + exact ⟨by unfold inqA; exact ht_field 3592#u64 (Or.inl rfl), + by unfold scA; exact ht_field 3464#u64 (Or.inr rfl)⟩ + · intro c hc + have hma_al : Aligned (maxA c) := by + rw [maxA_eqA]; exact hfa _ (.nslot _ (.sc2 c hc)) + refine hmem_pres (maxA c) hma_al + (fun hub => X.fmem.disj_sc _ hub c hc 72#u64 (by decide) (maxA_eqA c)) + (by rw [maxA_eqA]; exact X.vcpu.place.disj_sc vc hvcV c hc 24#u64 72#u64 (by decide) (by decide)) + (by unfold runVcpuA; rw [maxA_eqA]; exact (hLD.offSc ct hct 3672#u64 (Or.inr rfl) c hc).2) + (fun r => by + obtain ⟨hKval, hreg_eq⟩ := regA_eq_wa_KA ct r; have hlt := regIdxA_lt r + rw [hreg_eq, maxA_eqA]; exact hU.tcb_offSc ct hct _ (by omega) (by omega) c hc 72#u64 (by decide)) + (by rw [maxA_eqA]; exact (hSc_st c hc).2) + · intro c hc + have hnd_eq : L.nodeAt s11 c layout.node.IDLE_THREAD = L.nodeAt s c layout.node.IDLE_THREAD := by + unfold LawfulStore.nodeAt; rw [hksb11] + rw [hnd_eq] + have hnd_al : Aligned (L.nodeAt s c layout.node.IDLE_THREAD) := + X.vcpu.place.node_al c layout.node.IDLE_THREAD (by unfold layout.node.IDLE_THREAD; decide) + have hnd_lt : (layout.node.IDLE_THREAD : U64).val < 4288 := by unfold layout.node.IDLE_THREAD; decide + have hc128 := hCs c hc + have hksFit := X.j.1.ksFit + have hnd_val : (L.nodeAt s c layout.node.IDLE_THREAD).val = + (L.ksb s).val + 223488 + c.val * 4288 + (layout.node.IDLE_THREAD : U64).val := by + have hk := (L.ksb s).hBounds; have hc' := c.hBounds + simp only [ksSizeA] at hksFit + simp [LawfulStore.nodeAt, core.num.U64.wrapping_add_val_eq, core.num.U64.wrapping_mul_val_eq, + U64.size_def, U64.numBits, UScalar.size] at hk hc' ⊢; omega + let kOff : U64 := ⟨BitVec.ofNat 64 (223488 + c.val * 4288 + (layout.node.IDLE_THREAD : U64).val)⟩ + have hkOff_val : kOff.val = 223488 + c.val * 4288 + (layout.node.IDLE_THREAD : U64).val := by + change (BitVec.ofNat 64 _).toNat = _; rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + have hks_lt : kOff.val < ksSizeA := by rw [hkOff_val]; unfold ksSizeA; omega + have hnd_wa : L.nodeAt s c layout.node.IDLE_THREAD = wa (L.ksb s) kOff := by + apply UScalar.eq_of_val_eq; rw [hnd_val, wa_valP, hkOff_val] + simp only [ksSizeA] at hksFit; rw [Nat.mod_eq_of_lt (by omega)]; omega + refine hmem_pres (L.nodeAt s c layout.node.IDLE_THREAD) hnd_al + (fun hub => X.fmem.disj_ks _ hub ⟨by rw [hnd_val]; omega, by rw [hnd_val]; unfold ksSizeA; omega⟩) + (X.vcpu.place.disj_node hksFit (vcpuHdr_in_vcPage hvcV (o := 24#u64) (by decide) (by decide)) hc128 hnd_lt) + (by unfold runVcpuA; rw [hnd_wa]; exact hLD.offKs ct hct 3672#u64 (Or.inr rfl) kOff hks_lt) + (fun r => by + obtain ⟨hKval, hreg_eq⟩ := regA_eq_wa_KA ct r; have hlt := regIdxA_lt r + rw [hreg_eq, hnd_wa]; exact hksTcb ct hct _ (by omega) (by omega) kOff hks_lt) + (by rw [hnd_wa]; exact hKs_st kOff hks_lt) + · intro q hq + rw [hσ11_eq] + have hq1 : inUserFrame q σ1 := by unfold inUserFrame at hq ⊢; rwa [hkh1] + have hq2 : inUserFrame q (updKSc σ1 (specPtr vc) (.archObj (.vcpu { v with vcpuExt := { v.vcpuExt with runCaller := none } }))) := + inUserFrame_updKScA hkv1 (fun _ h => by cases h) hq1 + have hne_ct_vc' : specPtr ct ≠ specPtr vc := fun e => by rw [e, hkv1] at hkc1; cases hkc1 + have hq3 : inUserFrame q σ3 := + inUserFrame_updKScA (by rw [updKSc_other _ hne_ct_vc']; exact hkc1) (fun _ h => by cases h) hq2 + exact inUserFrame_updKScA hkc10 (fun _ h => by cases h) (huf10 q hq3) + · intro u hu hne + by_cases eu : u = ct + · subst eu; exact absurd hrv11 hne + · have hta_u := hU.tcb_al u hu + have hrv_eq : L.mem s11 (runVcpuA u) = L.mem s (runVcpuA u) := + hmem_pres (runVcpuA u) (by unfold runVcpuA; exact aligned_wa (by unfold Aligned; have := hta_u.align; omega) 3672#u64 (by decide)) + (fun hub => X.fmem.disj_tcb _ hub u hu 3672#u64 (by decide) (by unfold runVcpuA; rfl)) + (by unfold runVcpuA; exact X.vcpu.place.disj_tcb vc hvcV u hu 24#u64 3672#u64 (by decide) (by decide)) + (by unfold runVcpuA; exact field_ne hta_ct hta_u (by decide) (by decide) (fun e _ => eu e.symm)) + (fun r => by + obtain ⟨hKval, hreg_eq⟩ := regA_eq_wa_KA ct r; have hlt := regIdxA_lt r + have h3672 : (3672#u64 : U64).val = 3672 := rfl + rw [hreg_eq]; unfold runVcpuA; exact field_ne hta_ct hta_u (by omega) (by decide) (fun _ => by omega)) + (by rw [stA_eq]; unfold runVcpuA; exact field_ne hta_ct hta_u (by decide) (by decide) (fun _ => by decide)) + rwa [hrv_eq] at hne + have hv_pres11 : ∀ v' ∈ Z.V, L.mem s11 v' = L.mem s v' := fun v' hv' => by + have hwa0 : wa v' 0#u64 = v' := by apply UScalar.eq_of_val_eq; rw [wa_valP]; simp [UScalar.hBounds] + have hvp0 : VcPage Z.V v' := by rw [← hwa0]; exact vcpuHdr_in_vcPage hv' (o := 0#u64) (by decide) (by decide) + refine hmem_pres v' (X.vcpu.place.vc_al hv') (fun hub => vc_disj_ubA X.vcpu.place hvp0 hub rfl) + (by rw [← hwa0] + by_cases ev : vc = v' + · subst ev; exact X.vcpu.place.same_vcpu_ne hvcV (by decide) (by decide) (by decide) + · exact X.vcpu.place.disj_vcpu hvcV hv' ev (by decide) (by decide)) + (by rw [← hwa0]; unfold runVcpuA; exact (X.vcpu.place.disj_tcb v' hv' ct hct 0#u64 3672#u64 (by decide) (by decide)).symm) + (fun r => by + obtain ⟨hKval, hreg_eq⟩ := regA_eq_wa_KA ct r; have hlt := regIdxA_lt r + rw [hreg_eq, ← hwa0]; exact (X.vcpu.place.disj_tcb v' hv' ct hct 0#u64 _ (by decide) (by omega)).symm) + (by rw [stA_eq, ← hwa0]; exact (X.vcpu.place.disj_tcb v' hv' ct hct 0#u64 3328#u64 (by decide) (by decide)).symm) + have htv_pres11 : ∀ t ∈ Z.T, L.mem s11 (wa t 3664#u64) = L.mem s (wa t 3664#u64) := fun t ht => by + have hta_t := hU.tcb_al t ht + refine hmem_pres (wa t 3664#u64) (aligned_wa (by unfold Aligned; have := hta_t.align; omega) 3664#u64 (by decide)) + (fun hub => X.fmem.disj_tcb _ hub t ht 3664#u64 (by decide) rfl) + (X.vcpu.place.disj_tcb vc hvcV t ht 24#u64 3664#u64 (by decide) (by decide)) + (by unfold runVcpuA; exact field_ne hta_ct hta_t (by decide) (by decide) (fun _ => by decide)) + (fun r => by + obtain ⟨hKval, hreg_eq⟩ := regA_eq_wa_KA ct r; have hlt := regIdxA_lt r + have h3664 : (3664#u64 : U64).val = 3664 := rfl + rw [hreg_eq]; exact field_ne hta_ct hta_t (by omega) (by decide) (fun _ => by omega)) + (by rw [stA_eq]; exact field_ne hta_ct hta_t (by decide) (by decide) (fun _ => by decide)) + have hct_cur : sched.cur_thread st s11 = ok (L.curW s11) := eq_of_spec_fd (sched.cur_thread.spec s11) + rw [hct_cur, bind_ok] + have heq_ok : obj.Tcb.Insts.CoreCmpPartialEqTcb.eq ct (L.curW s11) = ok (decide (ct = L.curW s11)) := by + unfold obj.Tcb.Insts.CoreCmpPartialEqTcb.eq; rfl + rw [heq_ok, bind_ok] + by_cases hcur_eq : ct = L.curW s11 + · rw [if_pos (by simp [hcur_eq])] + refine WP.spec_mono (hVR.set_vm_root s11 a1 Z.hw ct X11.j.1.ar) ?_ + rintro ⟨s12, a12⟩ ⟨hs12, hw', hAR12⟩ + dsimp only at hs12; subst hs12 + exact ⟨hw', σ11, hsp_full, X11.of_hw hAR12, hksb11, hcpu11, hrv11, fun _ => hrc11, hv_pres11, htv_pres11, hpres11⟩ + · rw [if_neg (by simp [hcur_eq]), WP.spec_ok] + exact ⟨Z.hw, σ11, hsp_full, X11, hksb11, hcpu11, hrv11, fun _ => hrc11, hv_pres11, htv_pres11, hpres11⟩ + +end helpers + +end + +end Fl4ProofsA64.P3.Vcpu diff --git a/proofs/Fl4ProofsA64/P3/VcpuSetTcbJA.lean b/proofs/Fl4ProofsA64/P3/VcpuSetTcbJA.lean new file mode 100644 index 000000000..77dd3da84 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/VcpuSetTcbJA.lean @@ -0,0 +1,2020 @@ +/- +P3 (aarch64, A64-P3 S4 W2b-3): **Label 60 (`ARMVCPUSetTCB`) refinement** on `VcpuTopPostA`. + +Provides: +- `fl4ReadStateToFrame_updKScA`, `fl4CompleteRun_updKScA`: pure spec commutation of + `fl4ReadStateToFrame` and `fl4CompleteRun` with disjoint `kheap` updates (`updKSc`). +- `fl4SetTcb_rust_order_eqA`: equivalence between `vcpu.decode_set_tcb`'s Rust execution order + (`fl4VcpuUnbindExt vc` → optional `fl4VcpuUnbindExt oldV` → optional `complete_run`/`cancel_run` → + associate `(vc, t)`) and `fl4PerformVcpuInv (.setTcb vc t)` (`fl4VcpuSetTcbPre` → + `fl4AssociateVcpuTcb`). +- `vcpu_set_tcb_jA`: `vcpu.decode_set_tcb` refines `decodeVcpuSetTcb` + `fl4PerformVcpuInv (.setTcb)` + on `VcpuTopPostA`, for both `t = cur_thread` and `t ≠ cur_thread`, updating `Z.hw` via + `VcpuVmRootA`. + +Named premises: `[P] VcpuPlatAgreeA ar`, `[P] VcpuArchKeepsA ar AR`, `[P] VcpuVmRootA st ar AR`, +`[S]` local-core `remote_tcb_stall` affinity. +-/ +import Fl4ProofsA64.P3.VcpuUnbindJA + +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3.Vcpu + +open Fl4Spec Fl4SpecA64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3 Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Endp +open Fl4ProofsA64.P3.Notif + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## Section 1: Pure spec commutation with disjoint `updKSc` -/ + +private theorem updKSc_commA (σ : State Unit) {p q : ObjRef} {o1 o2 : KernelObject} (hne : p ≠ q) : + updKSc (updKSc σ p o1) q o2 = updKSc (updKSc σ q o2) p o1 := by + cases σ with + | mk a ex => + cases a + unfold updKSc + dsimp only + rw [Function.update_comm hne] + +private theorem updKSc_idemA (σ : State Unit) {p : ObjRef} {o1 o2 : KernelObject} : + updKSc (updKSc σ p o1) p o2 = updKSc σ p o2 := by + cases σ with + | mk a ex => + cases a + unfold updKSc + dsimp only + rw [Function.update_idem] + +private theorem getCap_tcbIpcframeA {σ : State Unit} {c : ObjRef} {xc : Tcb} + (hkc : σ.kheap c = some (.tcb xc)) : + getCap (Z := Unit) (c, tcbCnodeIndex 2) σ = ({(xc.tcbIpcframe, σ)}, False) := by + unfold getCap; dsimp only + have hobj : getObject (Z := Unit) c σ = ({(.tcb xc, σ)}, False) := by + unfold getObject NonDet.getsThe readObject + simp [NonDet.gets, hkc, NonDet.assertOpt]; rfl + have hmap2 : tcbCnodeMap xc (tcbCnodeIndex 2) = some xc.tcbIpcframe := by + unfold tcbCnodeMap tcbCnodeIndex; rfl + rw [nd_bind_oneSc hobj]; dsimp only + rw [nd_bind_oneSc (m := pure (tcbCnodeMap xc)) rfl, hmap2]; rfl + +private theorem inUserFrame_updKSc_iffA {σ : State Unit} {p : ObjRef} {o0 o1 : KernelObject} + (hk0 : σ.kheap p = some o0) + (h0 : ∀ sz, o0 ≠ .archObj (.dataPage false sz)) + (h1 : ∀ sz, o1 ≠ .archObj (.dataPage false sz)) (q : ObjRef) : + inUserFrame q (updKSc σ p o1) ↔ inUserFrame q σ := by + constructor + · intro hq + have hback : updKSc (updKSc σ p o1) p o0 = σ := by + have hkh : (updKSc (updKSc σ p o1) p o0).kheap = σ.kheap := by + funext r; by_cases hr : r = p + · subst hr; rw [updKSc_same, hk0] + · rw [updKSc_other _ hr, updKSc_other _ hr] + cases σ with | mk a ex => cases a; dsimp only [updKSc] at hkh ⊢; rw [hkh] + rw [← hback] + exact inUserFrame_updKScA (updKSc_same σ p o1) h1 hq + · exact inUserFrame_updKScA hk0 h0 + +private theorem nd_bind_failA {α β : Type} {m : SMonad Unit α} {f : α → SMonad Unit β} {σ : State Unit} + (hm : m σ = (∅, True)) : (m >>= f) σ = (∅, True) := by + change NonDet.bind m f σ = (∅, True) + unfold NonDet.bind; rw [hm] + exact Prod.ext (by ext x; simp) (by simp) + +private theorem storeWordOffs_failA (frame : ObjRef) (off : Nat) (w : Word) (τ : State Unit) + (h : ¬ (bufAddr frame off &&& mask 3 = 0 ∧ inUserFrame (bufAddr frame off) τ)) : + storeWordOffs (Z := Unit) frame off w τ = (∅, True) := by + unfold storeWordOffs + rw [nd_bind_oneSc (m := NonDet.get) rfl, show frame + BitVec.ofNat 64 (off * wordSize) = bufAddr frame off from rfl] + by_cases hu : inUserFrame (bufAddr frame off) τ + · have hal : ¬ (bufAddr frame off &&& mask 3 = 0) := fun ha => h ⟨ha, hu⟩ + have ha1 : NonDet.assert (inUserFrame (bufAddr frame off) τ) τ = ({((), τ)}, False) := by + unfold NonDet.assert; exact Prod.ext (by ext x; simp [hu]) (by simp [hu]) + rw [nd_bind_oneSc ha1] + have hsw : storeWord (bufAddr frame off) w τ.machineState = (∅, True) := by + unfold storeWord + have ha2 : NonDet.assert (bufAddr frame off &&& mask 3 = 0) τ.machineState = (∅, True) := by + unfold NonDet.assert; exact Prod.ext (by ext x; simp [show bufAddr frame off &&& mask 3 ≠ 0#64 from hal]) (by simp [show bufAddr frame off &&& mask 3 ≠ 0#64 from hal]) + change NonDet.bind (NonDet.assert _) _ τ.machineState = (∅, True) + unfold NonDet.bind; rw [ha2] + exact Prod.ext (by ext x; simp) (by simp) + unfold doMachineOp NonDet.selectF + rw [nd_bind_oneSc (m := NonDet.gets _) rfl] + change NonDet.bind (fun s => _) _ τ = (∅, True) + unfold NonDet.bind; simp only [hsw] + exact Prod.ext (by ext x; simp) (by simp) + · have ha1 : NonDet.assert (inUserFrame (bufAddr frame off) τ) τ = (∅, True) := by + unfold NonDet.assert; exact Prod.ext (by ext x; simp [hu]) (by simp [hu]) + exact nd_bind_failA ha1 + +private theorem storeWordOffs_msA (frame : ObjRef) (off : Nat) (w : Word) (τ : State Unit) + (hal : bufAddr frame off &&& mask 3 = 0) (huf : inUserFrame (bufAddr frame off) τ) : + storeWordOffs (Z := Unit) frame off w τ = + ({((), { τ with machineState := { τ.machineState with + underlyingMemory := storeMem τ.machineState.underlyingMemory (bufAddr frame off) w } })}, False) := by + rw [storeWordOffs_eqA frame off w τ hal huf] + cases τ with | mk a ex => cases a; rfl + +private theorem storeWordOffs_ms_updA (frame : ObjRef) (off : Nat) (w : Word) (τ : State Unit) (p : ObjRef) (o : KernelObject) + (hal : bufAddr frame off &&& mask 3 = 0) (huf : inUserFrame (bufAddr frame off) (updKSc τ p o)) : + storeWordOffs (Z := Unit) frame off w (updKSc τ p o) = + ({((), updKSc { τ with machineState := { τ.machineState with + underlyingMemory := storeMem τ.machineState.underlyingMemory (bufAddr frame off) w } } p o)}, False) := by + rw [storeWordOffs_eqA frame off w (updKSc τ p o) hal huf] + cases τ with | mk a ex => cases a; rfl + +private theorem storeWordOffs_ms_commA (frame : ObjRef) (off : Nat) (w : Word) (τ : State Unit) + (p : ObjRef) (o : KernelObject) + (huf_iff : ∀ q, inUserFrame q (updKSc τ p o) ↔ inUserFrame q τ) : + (∃ ms', storeWordOffs (Z := Unit) frame off w τ = ({((), { τ with machineState := ms' })}, False) ∧ + storeWordOffs (Z := Unit) frame off w (updKSc τ p o) = ({((), updKSc { τ with machineState := ms' } p o)}, False)) ∨ + (storeWordOffs (Z := Unit) frame off w τ = (∅, True) ∧ + storeWordOffs (Z := Unit) frame off w (updKSc τ p o) = (∅, True)) := by + by_cases h : bufAddr frame off &&& mask 3 = 0 ∧ inUserFrame (bufAddr frame off) τ + · exact Or.inl ⟨_, storeWordOffs_msA frame off w τ h.1 h.2, + storeWordOffs_ms_updA frame off w τ p o h.1 ((huf_iff _).2 h.2)⟩ + · exact Or.inr ⟨storeWordOffs_failA frame off w τ h, + storeWordOffs_failA frame off w (updKSc τ p o) (fun ⟨ha, hu⟩ => h ⟨ha, (huf_iff _).1 hu⟩)⟩ + +private theorem copyRegsToFrame_updKScA {vc frame p : ObjRef} {v : Vcpu} {o : KernelObject} + (hne : p ≠ vc) (rs : List Nat) : + ∀ (σ : State Unit), σ.kheap vc = some (.archObj (.vcpu v)) → + (∀ q, inUserFrame q (updKSc σ p o) ↔ inUserFrame q σ) → + (∃ ms', copyRegsToFrame (Z := Unit) vc frame rs σ = ({((), { σ with machineState := ms' })}, False) ∧ + copyRegsToFrame (Z := Unit) vc frame rs (updKSc σ p o) = + ({((), updKSc { σ with machineState := ms' } p o)}, False)) ∨ + (copyRegsToFrame (Z := Unit) vc frame rs σ = (∅, True) ∧ + copyRegsToFrame (Z := Unit) vc frame rs (updKSc σ p o) = (∅, True)) := by + induction rs with + | nil => + intro σ _ _ + exact Or.inl ⟨σ.machineState, rfl, rfl⟩ + | cons r rs ih => + intro σ hkv huf_iff + have hkv' : (updKSc σ p o).kheap vc = some (.archObj (.vcpu v)) := by + rw [updKSc_other _ hne.symm]; exact hkv + rcases storeWordOffs_ms_commA frame r (fl4VcpuRegGet v r) σ p o huf_iff with ⟨ms1, h1, h1'⟩ | ⟨hf, hf'⟩ + · have huf1 : ∀ q, inUserFrame q (updKSc { σ with machineState := ms1 } p o) ↔ inUserFrame q { σ with machineState := ms1 } := + fun q => huf_iff q + rcases ih { σ with machineState := ms1 } hkv huf1 with ⟨ms', h2, h2'⟩ | ⟨hf2, hf2'⟩ + · have hms_eq : ∀ (m2 : MachineState), { ({ σ with machineState := ms1 } : State Unit) with machineState := m2 } = { σ with machineState := m2 } := by + intro m2; cases σ with | mk a ex => cases a; rfl + rw [hms_eq] at h2 h2' + refine Or.inl ⟨ms', ?_, ?_⟩ + · unfold copyRegsToFrame NonDet.mapMx + change ((fl4VcpuGet vc r >>= fun w => storeWordOffs frame r w) >>= fun _ => copyRegsToFrame vc frame rs) σ = _ + rw [nd_bind_oneSc (by rw [nd_bind_oneSc (fl4VcpuGet_eqA r hkv)]; exact h1)] + exact h2 + · unfold copyRegsToFrame NonDet.mapMx + change ((fl4VcpuGet vc r >>= fun w => storeWordOffs frame r w) >>= fun _ => copyRegsToFrame vc frame rs) (updKSc σ p o) = _ + rw [nd_bind_oneSc (by rw [nd_bind_oneSc (fl4VcpuGet_eqA r hkv')]; exact h1')] + exact h2' + · refine Or.inr ⟨?_, ?_⟩ + · unfold copyRegsToFrame NonDet.mapMx + change ((fl4VcpuGet vc r >>= fun w => storeWordOffs frame r w) >>= fun _ => copyRegsToFrame vc frame rs) σ = _ + rw [nd_bind_oneSc (by rw [nd_bind_oneSc (fl4VcpuGet_eqA r hkv)]; exact h1)] + exact hf2 + · unfold copyRegsToFrame NonDet.mapMx + change ((fl4VcpuGet vc r >>= fun w => storeWordOffs frame r w) >>= fun _ => copyRegsToFrame vc frame rs) (updKSc σ p o) = _ + rw [nd_bind_oneSc (by rw [nd_bind_oneSc (fl4VcpuGet_eqA r hkv')]; exact h1')] + exact hf2' + · refine Or.inr ⟨?_, ?_⟩ + · unfold copyRegsToFrame NonDet.mapMx + change ((fl4VcpuGet vc r >>= fun w => storeWordOffs frame r w) >>= fun _ => copyRegsToFrame vc frame rs) σ = _ + exact nd_bind_failA (by rw [nd_bind_oneSc (fl4VcpuGet_eqA r hkv)]; exact hf) + · unfold copyRegsToFrame NonDet.mapMx + change ((fl4VcpuGet vc r >>= fun w => storeWordOffs frame r w) >>= fun _ => copyRegsToFrame vc frame rs) (updKSc σ p o) = _ + exact nd_bind_failA (by rw [nd_bind_oneSc (fl4VcpuGet_eqA r hkv')]; exact hf') + +private theorem mapMx_ms_updKScA {α : Type} {f : α → SMonad Unit Unit} {p : ObjRef} {o : KernelObject} + {P : State Unit → Prop} + (hstep : ∀ a σ, P σ → + (∃ ms', P { σ with machineState := ms' } ∧ + f a σ = ({((), { σ with machineState := ms' })}, False) ∧ + f a (updKSc σ p o) = ({((), updKSc { σ with machineState := ms' } p o)}, False)) ∨ + (f a σ = (∅, True) ∧ f a (updKSc σ p o) = (∅, True))) + (xs : List α) : + ∀ σ, P σ → + (∃ ms', P { σ with machineState := ms' } ∧ + NonDet.mapMx f xs σ = ({((), { σ with machineState := ms' })}, False) ∧ + NonDet.mapMx f xs (updKSc σ p o) = ({((), updKSc { σ with machineState := ms' } p o)}, False)) ∨ + (NonDet.mapMx f xs σ = (∅, True) ∧ NonDet.mapMx f xs (updKSc σ p o) = (∅, True)) := by + induction xs with + | nil => intro σ hP; exact Or.inl ⟨σ.machineState, hP, rfl, rfl⟩ + | cons a xs ih => + intro σ hP + rcases hstep a σ hP with ⟨ms1, hP1, h1, h1'⟩ | ⟨hf, hf'⟩ + · rcases ih { σ with machineState := ms1 } hP1 with ⟨ms2, hP2, h2, h2'⟩ | ⟨hf2, hf2'⟩ + · have hms_eq : { ({ σ with machineState := ms1 } : State Unit) with machineState := ms2 } = { σ with machineState := ms2 } := by + cases σ with | mk a ex => cases a; rfl + rw [hms_eq] at hP2 h2 h2' + exact Or.inl ⟨ms2, hP2, + by unfold NonDet.mapMx; change (f a >>= fun _ => NonDet.mapMx f xs) σ = _; rw [nd_bind_oneSc h1]; exact h2, + by unfold NonDet.mapMx; change (f a >>= fun _ => NonDet.mapMx f xs) (updKSc σ p o) = _; rw [nd_bind_oneSc h1']; exact h2'⟩ + · exact Or.inr + ⟨by unfold NonDet.mapMx; change (f a >>= fun _ => NonDet.mapMx f xs) σ = _; rw [nd_bind_oneSc h1]; exact hf2, + by unfold NonDet.mapMx; change (f a >>= fun _ => NonDet.mapMx f xs) (updKSc σ p o) = _; rw [nd_bind_oneSc h1']; exact hf2'⟩ + · exact Or.inr + ⟨by unfold NonDet.mapMx; change (f a >>= fun _ => NonDet.mapMx f xs) σ = _; exact nd_bind_failA hf, + by unfold NonDet.mapMx; change (f a >>= fun _ => NonDet.mapMx f xs) (updKSc σ p o) = _; exact nd_bind_failA hf'⟩ + +private theorem nd_when_trueA {c : Prop} [Decidable c] {m : SMonad Unit Unit} (h : c) : + NonDet.when c m = m := by unfold NonDet.when; exact if_pos h + +private theorem nd_when_falseA {c : Prop} [Decidable c] {m : SMonad Unit Unit} (h : ¬ c) : + NonDet.when c m = pure () := by unfold NonDet.when; exact if_neg h + +private theorem nd_bind_assocA {α β γ : Type} (m : SMonad Unit α) (f : α → SMonad Unit β) (g : β → SMonad Unit γ) + (σ : State Unit) : + ((m >>= f) >>= g) σ = (m >>= fun a => f a >>= g) σ := by + change NonDet.bind (NonDet.bind m f) g σ = NonDet.bind m (fun a => NonDet.bind (f a) g) σ + unfold NonDet.bind + ext1 + · ext ⟨c, s'⟩; simp only [Set.mem_setOf_eq]; constructor + · rintro ⟨⟨b, s1⟩, ⟨⟨a, s0⟩, ha, hb⟩, hc⟩; exact ⟨⟨a, s0⟩, ha, ⟨b, s1⟩, hb, hc⟩ + · rintro ⟨⟨a, s0⟩, ha, ⟨b, s1⟩, hb, hc⟩; exact ⟨⟨b, s1⟩, ⟨⟨a, s0⟩, ha, hb⟩, hc⟩ + · simp only [eq_iff_iff]; constructor + · rintro ((hm | ⟨⟨a, s0⟩, ha, hf⟩) | ⟨⟨b, s1⟩, ⟨⟨a, s0⟩, ha, hb⟩, hg⟩) + · exact Or.inl hm + · exact Or.inr ⟨⟨a, s0⟩, ha, Or.inl hf⟩ + · exact Or.inr ⟨⟨a, s0⟩, ha, Or.inr ⟨⟨b, s1⟩, hb, hg⟩⟩ + · rintro (hm | ⟨⟨a, s0⟩, ha, hf | ⟨⟨b, s1⟩, hb, hg⟩⟩) + · exact Or.inl (Or.inl hm) + · exact Or.inl (Or.inr ⟨⟨a, s0⟩, ha, hf⟩) + · exact Or.inr ⟨⟨b, s1⟩, ⟨⟨a, s0⟩, ha, hb⟩, hg⟩ + +private theorem asUser_getRegA {σ : State Unit} {t : ObjRef} {x : Tcb} + (hk : σ.kheap t = some (.tcb x)) (r : Register) : + asUser t (getRegister r) σ = ({(x.tcbArch.tcbContext.userRegs r, σ)}, False) := by + have hg : NonDet.getsThe (getTcb (Z := Unit) t) σ = ({(x, σ)}, False) := by + simp [NonDet.getsThe, getTcb, readObject, hk, NonDet.assertOpt]; rfl + have ho : getObject (Z := Unit) t σ = ({(.tcb x, σ)}, False) := by + simp [getObject, NonDet.getsThe, readObject, hk, NonDet.assertOpt]; rfl + have hsel : NonDet.selectF (getRegister r (archTcbContextGet x.tcbArch)) σ = + ({((x.tcbArch.tcbContext.userRegs r, x.tcbArch.tcbContext), σ)}, False) := by + have h1 : getRegister r (archTcbContextGet x.tcbArch) = + ({(x.tcbArch.tcbContext.userRegs r, x.tcbArch.tcbContext)}, False) := rfl + rw [h1] + simp only [NonDet.selectF, Set.mem_singleton_iff, Prod.mk.injEq] + refine ⟨Set.ext fun y => ?_, trivial⟩ + simp [Prod.ext_iff] + have hso : setObject (Z := Unit) t (.tcb { x with tcbArch := archTcbContextSet x.tcbArch.tcbContext x.tcbArch }) σ = + ({((), σ)}, False) := by + unfold setObject + rw [nd_bind_oneSc ho, nd_bind_oneSc (show (NonDet.assert (aType (.tcb x) = aType (.tcb _)) : + NonDet (State Unit) Unit) σ = ({((), σ)}, False) by simp [NonDet.assert, aType]), + nd_bind_oneSc (show NonDet.get σ = ({(σ, σ)}, False) from rfl)] + have hupd : { σ with kheap := Function.update σ.kheap t (some (.tcb { x with tcbArch := archTcbContextSet x.tcbArch.tcbContext x.tcbArch })) } = σ := by + have hkh : Function.update σ.kheap t (some (.tcb { x with tcbArch := archTcbContextSet x.tcbArch.tcbContext x.tcbArch })) = σ.kheap := by + funext q; by_cases eq : q = t + · subst eq; simp [Function.update, hk, archTcbContextSet] + · simp [Function.update, eq] + cases σ with | mk a ex => cases a; dsimp only at hkh ⊢; rw [hkh] + rw [hupd]; rfl + unfold asUser + rw [nd_bind_oneSc (show NonDet.getsThe (getTcb (Z := Unit) t) σ = ({(x, σ)}, False) from hg), + nd_bind_oneSc (show (pure (archTcbContextGet x.tcbArch) : SMonad Unit UserContext) σ = _ from rfl), + nd_bind_oneSc hsel] + dsimp only + rw [nd_bind_oneSc (show (pure _ : SMonad Unit Tcb) σ = _ from rfl), nd_bind_oneSc hso] + rfl + +private theorem fl4ReadStateToFrame_updKScA {vc frame p : ObjRef} {v : Vcpu} {o : KernelObject} + {σ : State Unit} (m : Word) + (hne : p ≠ vc) (hkv : σ.kheap vc = some (.archObj (.vcpu v))) + (huf_iff : ∀ q, inUserFrame q (updKSc σ p o) ↔ inUserFrame q σ) + (htcb : ∀ t, v.vcpuTcb = some t → p ≠ t ∧ ∃ xt, σ.kheap t = some (.tcb xt)) : + (∃ ms', fl4ReadStateToFrame (Z := Unit) vc frame m σ = ({((), { σ with machineState := ms' })}, False) ∧ + fl4ReadStateToFrame (Z := Unit) vc frame m (updKSc σ p o) = + ({((), updKSc { σ with machineState := ms' } p o)}, False)) ∨ + (fl4ReadStateToFrame (Z := Unit) vc frame m σ = (∅, True) ∧ + fl4ReadStateToFrame (Z := Unit) vc frame m (updKSc σ p o) = (∅, True)) := by + let Good (f : SMonad Unit Unit) (ms0 : MachineState) : Prop := + (∃ ms1, f { σ with machineState := ms0 } = ({((), { σ with machineState := ms1 })}, False) ∧ + f (updKSc { σ with machineState := ms0 } p o) = ({((), updKSc { σ with machineState := ms1 } p o)}, False)) ∨ + (f { σ with machineState := ms0 } = (∅, True) ∧ f (updKSc { σ with machineState := ms0 } p o) = (∅, True)) + have hms_idem : ∀ (m1 m2 : MachineState), + { ({ σ with machineState := m1 } : State Unit) with machineState := m2 } = { σ with machineState := m2 } := by + intro m1 m2; cases σ with | mk a ex => cases a; rfl + have hcomp : ∀ {f g : SMonad Unit Unit} {ms0 : MachineState}, + Good f ms0 → (∀ ms1, Good g ms1) → Good (f >>= fun _ => g) ms0 := by + intro f g ms0 hf hg + rcases hf with ⟨ms1, hf1, hf1'⟩ | ⟨hff, hff'⟩ + · rcases hg ms1 with ⟨ms2, hg1, hg1'⟩ | ⟨hgf, hgf'⟩ + · exact Or.inl ⟨ms2, by rw [nd_bind_oneSc hf1]; exact hg1, by rw [nd_bind_oneSc hf1']; exact hg1'⟩ + · exact Or.inr ⟨by rw [nd_bind_oneSc hf1]; exact hgf, by rw [nd_bind_oneSc hf1']; exact hgf'⟩ + · exact Or.inr ⟨nd_bind_failA hff, nd_bind_failA hff'⟩ + have hwhen : ∀ (c : Prop) [Decidable c] (f : SMonad Unit Unit) (ms0 : MachineState), + (c → Good f ms0) → Good (NonDet.when c f) ms0 := by + intro c _ f ms0 hf + dsimp only [Good]; unfold NonDet.when + by_cases hc : c + · rw [if_pos hc]; exact hf hc + · rw [if_neg hc]; exact Or.inl ⟨ms0, rfl, rfl⟩ + have hcopy : ∀ (rs : List Nat) (ms0 : MachineState), Good (copyRegsToFrame (Z := Unit) vc frame rs) ms0 := fun rs ms0 => by + rcases copyRegsToFrame_updKScA (o := o) hne rs { σ with machineState := ms0 } hkv (fun q => huf_iff q) with + ⟨ms1, h1, h1'⟩ | ⟨hf, hf'⟩ + · rw [hms_idem] at h1 h1'; exact Or.inl ⟨ms1, h1, h1'⟩ + · exact Or.inr ⟨hf, hf'⟩ + have hsw : ∀ (off : Nat) (w : Word) (ms0 : MachineState), Good (storeWordOffs (Z := Unit) frame off w) ms0 := fun off w ms0 => by + rcases storeWordOffs_ms_commA frame off w { σ with machineState := ms0 } p o (fun q => huf_iff q) with + ⟨ms1, h1, h1'⟩ | ⟨hf, hf'⟩ + · rw [hms_idem] at h1 h1'; exact Or.inl ⟨ms1, h1, h1'⟩ + · exact Or.inr ⟨hf, hf'⟩ + have hσ_eq : { σ with machineState := σ.machineState } = σ := by cases σ with | mk a ex => cases a; rfl + rw [← hσ_eq] + change Good (fl4ReadStateToFrame (Z := Unit) vc frame m) σ.machineState + unfold fl4ReadStateToFrame + apply hcomp + · apply hwhen; intro _ + exact hcomp (hcopy _ _) (fun ms1 => hcopy _ ms1) + intro ms1 + apply hcomp + · apply hwhen; intro _ + exact hcomp (hcopy _ ms1) fun ms2 => hcomp (hcopy _ ms2) fun ms3 => hcopy _ ms3 + intro ms2 + apply hcomp + · apply hwhen; intro _ + refine hcomp (hcopy _ ms2) fun ms3 => ?_ + have hkv' : (updKSc { σ with machineState := ms3 } p o).kheap vc = some (.archObj (.vcpu v)) := by + rw [updKSc_other _ hne.symm]; exact hkv + rcases hsw vppiWord (if v.vcpuVppiMasked .vppiEventIRQ_VTimer then 1 else 0) ms3 with ⟨ms4, hs1, hs2⟩ | ⟨hf, hf'⟩ + · exact Or.inl ⟨ms4, by rw [nd_bind_oneSc (getVcpu_eqA (σ := { σ with machineState := ms3 }) hkv)]; exact hs1, + by rw [nd_bind_oneSc (getVcpu_eqA hkv')]; exact hs2⟩ + · exact Or.inr ⟨by rw [nd_bind_oneSc (getVcpu_eqA (σ := { σ with machineState := ms3 }) hkv)]; exact hf, + by rw [nd_bind_oneSc (getVcpu_eqA hkv')]; exact hf'⟩ + intro ms3 + have hgprs : ∀ ms4, Good (NonDet.when (m &&& smGprs ≠ 0) (do + let v ← getVcpu vc + NonDet.mapMx (fun (i, r) => do + let w ← match v.vcpuTcb with + | some t => asUser t (getRegister r) + | none => pure 0 + storeWordOffs frame (64 + i) w) (List.range fl4UserRegs.length |>.zip fl4UserRegs))) ms4 := fun ms4 => by + apply hwhen; intro _ + have hkv' : (updKSc { σ with machineState := ms4 } p o).kheap vc = some (.archObj (.vcpu v)) := by + rw [updKSc_other _ hne.symm]; exact hkv + cases hvt : v.vcpuTcb with + | none => + rcases mapMx_ms_updKScA (P := fun τ => ∀ q, inUserFrame q (updKSc τ p o) ↔ inUserFrame q τ) + (f := fun ir : Nat × Register => (pure 0 : SMonad Unit Word) >>= fun w => storeWordOffs frame (64 + ir.1) w) + (fun ir τ hτ => by + rcases storeWordOffs_ms_commA frame (64 + ir.1) 0 τ p o hτ with ⟨ms', h1, h1'⟩ | ⟨hf, hf'⟩ + · exact Or.inl ⟨ms', hτ, + by rw [nd_bind_oneSc (m := pure 0) rfl]; exact h1, + by rw [nd_bind_oneSc (m := pure 0) rfl]; exact h1'⟩ + · exact Or.inr ⟨by rw [nd_bind_oneSc (m := pure 0) rfl]; exact hf, + by rw [nd_bind_oneSc (m := pure 0) rfl]; exact hf'⟩) + (List.range fl4UserRegs.length |>.zip fl4UserRegs) { σ with machineState := ms4 } (fun q => huf_iff q) with + ⟨ms', -, h1, h1'⟩ | ⟨hf, hf'⟩ + · rw [hms_idem] at h1 h1' + exact Or.inl ⟨ms', by rw [nd_bind_oneSc (getVcpu_eqA (σ := { σ with machineState := ms4 }) hkv), hvt]; exact h1, + by rw [nd_bind_oneSc (getVcpu_eqA hkv'), hvt]; exact h1'⟩ + · exact Or.inr ⟨by rw [nd_bind_oneSc (getVcpu_eqA (σ := { σ with machineState := ms4 }) hkv), hvt]; exact hf, + by rw [nd_bind_oneSc (getVcpu_eqA hkv'), hvt]; exact hf'⟩ + | some t => + obtain ⟨hpt, xt, hkt⟩ := htcb t hvt + rcases mapMx_ms_updKScA (P := fun τ => τ.kheap t = some (.tcb xt) ∧ ∀ q, inUserFrame q (updKSc τ p o) ↔ inUserFrame q τ) + (f := fun ir : Nat × Register => asUser t (getRegister ir.2) >>= fun w => storeWordOffs frame (64 + ir.1) w) + (fun ir τ ⟨hτ, hτu⟩ => by + have hτ' : (updKSc τ p o).kheap t = some (.tcb xt) := by rw [updKSc_other _ hpt.symm]; exact hτ + rcases storeWordOffs_ms_commA frame (64 + ir.1) (xt.tcbArch.tcbContext.userRegs ir.2) τ p o hτu with ⟨ms', h1, h1'⟩ | ⟨hf, hf'⟩ + · exact Or.inl ⟨ms', ⟨hτ, hτu⟩, + by rw [nd_bind_oneSc (asUser_getRegA hτ ir.2)]; exact h1, + by rw [nd_bind_oneSc (asUser_getRegA hτ' ir.2)]; exact h1'⟩ + · exact Or.inr ⟨by rw [nd_bind_oneSc (asUser_getRegA hτ ir.2)]; exact hf, + by rw [nd_bind_oneSc (asUser_getRegA hτ' ir.2)]; exact hf'⟩) + (List.range fl4UserRegs.length |>.zip fl4UserRegs) { σ with machineState := ms4 } ⟨hkt, fun q => huf_iff q⟩ with + ⟨ms', -, h1, h1'⟩ | ⟨hf, hf'⟩ + · rw [hms_idem] at h1 h1' + exact Or.inl ⟨ms', by rw [nd_bind_oneSc (getVcpu_eqA (σ := { σ with machineState := ms4 }) hkv), hvt]; exact h1, + by rw [nd_bind_oneSc (getVcpu_eqA hkv'), hvt]; exact h1'⟩ + · exact Or.inr ⟨by rw [nd_bind_oneSc (getVcpu_eqA (σ := { σ with machineState := ms4 }) hkv), hvt]; exact hf, + by rw [nd_bind_oneSc (getVcpu_eqA hkv'), hvt]; exact hf'⟩ + have hlr : ∀ ms0, Good (NonDet.mapMx (fun i => do + let w ← if i < fl4NumLrs then fl4VcpuGet (Z := Unit) vc (vrVgicLr0 + i) else pure 0 + storeWordOffs frame (vrVgicLr0 + i) w) (List.range 16)) ms0 := fun ms0 => by + rcases mapMx_ms_updKScA (P := fun τ => τ.kheap vc = some (.archObj (.vcpu v)) ∧ ∀ q, inUserFrame q (updKSc τ p o) ↔ inUserFrame q τ) + (f := fun i : Nat => do + let w ← if i < fl4NumLrs then fl4VcpuGet (Z := Unit) vc (vrVgicLr0 + i) else pure 0 + storeWordOffs frame (vrVgicLr0 + i) w) + (fun i τ ⟨hτ, hτu⟩ => by + have hτ' : (updKSc τ p o).kheap vc = some (.archObj (.vcpu v)) := by rw [updKSc_other _ hne.symm]; exact hτ + by_cases hi : i < fl4NumLrs + · rcases storeWordOffs_ms_commA frame (vrVgicLr0 + i) (fl4VcpuRegGet v (vrVgicLr0 + i)) τ p o hτu with ⟨ms', h1, h1'⟩ | ⟨hf, hf'⟩ + · exact Or.inl ⟨ms', ⟨hτ, hτu⟩, + by dsimp only; rw [if_pos hi, nd_bind_oneSc (fl4VcpuGet_eqA (vrVgicLr0 + i) hτ)]; exact h1, + by dsimp only; rw [if_pos hi, nd_bind_oneSc (fl4VcpuGet_eqA (vrVgicLr0 + i) hτ')]; exact h1'⟩ + · exact Or.inr ⟨by dsimp only; rw [if_pos hi, nd_bind_oneSc (fl4VcpuGet_eqA (vrVgicLr0 + i) hτ)]; exact hf, + by dsimp only; rw [if_pos hi, nd_bind_oneSc (fl4VcpuGet_eqA (vrVgicLr0 + i) hτ')]; exact hf'⟩ + · rcases storeWordOffs_ms_commA frame (vrVgicLr0 + i) 0 τ p o hτu with ⟨ms', h1, h1'⟩ | ⟨hf, hf'⟩ + · exact Or.inl ⟨ms', ⟨hτ, hτu⟩, + by dsimp only; rw [if_neg hi, nd_bind_oneSc (m := pure 0) rfl]; exact h1, + by dsimp only; rw [if_neg hi, nd_bind_oneSc (m := pure 0) rfl]; exact h1'⟩ + · exact Or.inr ⟨by dsimp only; rw [if_neg hi, nd_bind_oneSc (m := pure 0) rfl]; exact hf, + by dsimp only; rw [if_neg hi, nd_bind_oneSc (m := pure 0) rfl]; exact hf'⟩) + (List.range 16) { σ with machineState := ms0 } ⟨hkv, fun q => huf_iff q⟩ with + ⟨ms', -, h1, h1'⟩ | ⟨hf, hf'⟩ + · rw [hms_idem] at h1 h1'; exact Or.inl ⟨ms', h1, h1'⟩ + · exact Or.inr ⟨hf, hf'⟩ + by_cases hvg : m &&& smVgic ≠ 0 + · simp only [if_pos hvg] + refine hcomp (hsw vrVgicVtr abiVgicVtr ms3) fun ms4 => + hcomp (hcopy _ ms4) fun ms5 => hcomp ?_ fun ms6 => hcomp (hlr ms6) hgprs + rcases mapMx_ms_updKScA (P := fun τ => τ.kheap vc = some (.archObj (.vcpu v)) ∧ ∀ q, inUserFrame q (updKSc τ p o) ↔ inUserFrame q τ) + (f := fun r : Nat => do + let w ← if fl4GicV3 then fl4VcpuGet (Z := Unit) vc r else pure 0 + storeWordOffs frame r w) + (fun r τ ⟨hτ, hτu⟩ => by + have hτ' : (updKSc τ p o).kheap vc = some (.archObj (.vcpu v)) := by rw [updKSc_other _ hne.symm]; exact hτ + by_cases hg3 : fl4GicV3 + · rcases storeWordOffs_ms_commA frame r (fl4VcpuRegGet v r) τ p o hτu with ⟨ms', h1, h1'⟩ | ⟨hf, hf'⟩ + · exact Or.inl ⟨ms', ⟨hτ, hτu⟩, + by dsimp only; rw [if_pos hg3, nd_bind_oneSc (fl4VcpuGet_eqA r hτ)]; exact h1, + by dsimp only; rw [if_pos hg3, nd_bind_oneSc (fl4VcpuGet_eqA r hτ')]; exact h1'⟩ + · exact Or.inr ⟨by dsimp only; rw [if_pos hg3, nd_bind_oneSc (fl4VcpuGet_eqA r hτ)]; exact hf, + by dsimp only; rw [if_pos hg3, nd_bind_oneSc (fl4VcpuGet_eqA r hτ')]; exact hf'⟩ + · rcases storeWordOffs_ms_commA frame r 0 τ p o hτu with ⟨ms', h1, h1'⟩ | ⟨hf, hf'⟩ + · exact Or.inl ⟨ms', ⟨hτ, hτu⟩, + by dsimp only; rw [if_neg hg3, nd_bind_oneSc (m := pure 0) rfl]; exact h1, + by dsimp only; rw [if_neg hg3, nd_bind_oneSc (m := pure 0) rfl]; exact h1'⟩ + · exact Or.inr ⟨by dsimp only; rw [if_neg hg3, nd_bind_oneSc (m := pure 0) rfl]; exact hf, + by dsimp only; rw [if_neg hg3, nd_bind_oneSc (m := pure 0) rfl]; exact hf'⟩) + (idxRange vrVgicAp1r0 vrVgicAp1r3) { σ with machineState := ms5 } ⟨hkv, fun q => huf_iff q⟩ with + ⟨ms', -, h1, h1'⟩ | ⟨hf, hf'⟩ + · rw [hms_idem] at h1 h1'; exact Or.inl ⟨ms', h1, h1'⟩ + · exact Or.inr ⟨hf, hf'⟩ + · simp only [if_neg hvg] + refine hcomp ?_ hgprs + apply hwhen; intro _ + exact hcomp (hcopy _ ms3) hlr + +private theorem asUser_det_oneA {σ : State Unit} {t : ObjRef} {x : Tcb} + (hk : σ.kheap t = some (.tcb x)) {f : NonDet UserContext Unit} {uc' : UserContext} + (hf : f (archTcbContextGet x.tcbArch) = ({((), uc')}, False)) : + asUser (Z := Unit) t f σ = ({((), ctxUpdA σ t x uc')}, False) := by + have hg : NonDet.getsThe (getTcb (Z := Unit) t) σ = ({(x, σ)}, False) := by + simp [NonDet.getsThe, getTcb, readObject, hk, NonDet.assertOpt]; rfl + have ho : getObject (Z := Unit) t σ = ({(.tcb x, σ)}, False) := by + simp [getObject, NonDet.getsThe, readObject, hk, NonDet.assertOpt]; rfl + have hsel : NonDet.selectF (f (archTcbContextGet x.tcbArch)) σ = ({(((), uc'), σ)}, False) := by + rw [hf] + simp only [NonDet.selectF, Set.mem_singleton_iff, Prod.mk.injEq] + exact ⟨Set.ext fun y => by simp [Prod.ext_iff], trivial⟩ + have hso : setObject (Z := Unit) t (.tcb { x with tcbArch := archTcbContextSet uc' x.tcbArch }) σ = + ({((), ctxUpdA σ t x uc')}, False) := by + unfold setObject + rw [nd_bind_oneSc ho, nd_bind_oneSc (show (NonDet.assert (aType (.tcb x) = aType (.tcb _)) : + NonDet (State Unit) Unit) σ = ({((), σ)}, False) by simp [NonDet.assert, aType]), + nd_bind_oneSc (show NonDet.get σ = ({(σ, σ)}, False) from rfl)] + rfl + unfold asUser + rw [nd_bind_oneSc hg, nd_bind_oneSc (show (pure (archTcbContextGet x.tcbArch) : SMonad Unit UserContext) σ = _ from rfl), + nd_bind_oneSc hsel] + dsimp only + rw [nd_bind_oneSc (show (pure _ : SMonad Unit Tcb) σ = _ from rfl), nd_bind_oneSc hso] + rfl + +private theorem schedulable_updKScA {σ : State Unit} {c p : ObjRef} {xc : Tcb} {o : KernelObject} + (hkc : σ.kheap c = some (.tcb xc)) (hpc : p ≠ c) + (hsc : ∀ sc, xc.tcbSchedContext = some sc → p ≠ sc) : + schedulable c (updKSc σ p o) = schedulable c σ := by + have hkc' : (updKSc σ p o).kheap c = some (.tcb xc) := by rw [updKSc_other _ hpc.symm]; exact hkc + have ht1 : tcbsOf (updKSc σ p o) c = some xc := by simp [tcbsOf, tcbsOfKh, optMap, hkc', tcbOf] + have ht2 : tcbsOf σ c = some xc := by simp [tcbsOf, tcbsOfKh, optMap, hkc, tcbOf] + unfold schedulable + have h1 : optMapSome (tcbsOf (updKSc σ p o)) Tcb.tcbState c = optMapSome (tcbsOf σ) Tcb.tcbState c := by + simp [optMapSome, optMap, ht1, ht2] + have h2 : optMap (optMap (tcbsOf (updKSc σ p o)) Tcb.tcbSchedContext) (scsOf (updKSc σ p o)) c = + optMap (optMap (tcbsOf σ) Tcb.tcbSchedContext) (scsOf σ) c := by + simp only [optMap, ht1, ht2] + cases hsc0 : xc.tcbSchedContext with + | none => rfl + | some sc => + have hks : (updKSc σ p o).kheap sc = σ.kheap sc := updKSc_other _ (hsc sc hsc0).symm + simp [scsOf, scsOfKh, optMap, hks] + have h3 : (updKSc σ p o).releaseQueue = σ.releaseQueue := rfl + apply decide_eq_decide.2 + unfold optPred + rw [h1, h2, h3] + +private theorem setThreadStateAct_eqA (τ : State Unit) (c : ObjRef) : + setThreadStateAct (Z := Unit) c τ = + ({((), if c = τ.curThread ∧ τ.schedulerAction = .resumeCurThread ∧ ¬schedulable c τ + then { τ with schedulerAction := .chooseNewThread } else τ)}, False) := by + unfold setThreadStateAct + rw [nd_bind_oneSc (show NonDet.gets (fun s : State Unit => s.curThread) τ = ({(τ.curThread, τ)}, False) from rfl)] + rw [nd_bind_oneSc (show NonDet.gets (fun s : State Unit => s.schedulerAction) τ = ({(τ.schedulerAction, τ)}, False) from rfl)] + rw [nd_bind_oneSc (show NonDet.gets (schedulable c) τ = ({(schedulable c τ, τ)}, False) from rfl)] + unfold NonDet.when + by_cases h : c = τ.curThread ∧ τ.schedulerAction = .resumeCurThread ∧ ¬schedulable c τ + · rw [if_pos h, if_pos h]; unfold setSchedulerAction NonDet.modify; rfl + · rw [if_neg h, if_neg h]; rfl + +/-- `fl4CompleteRun` commutes with a single disjoint `updKSc`. -/ +private theorem fl4CompleteRun_updKScA {c ov p : ObjRef} {xc : Tcb} {vov : Vcpu} {o : KernelObject} + {σ : State Unit} {stop : RunStop} {w1 w2 w3 : Word} + (hcur : σ.archState.armCurrentVcpu = none) + (hkc : σ.kheap c = some (.tcb xc)) (hrun : xc.tcbExt.tcbRunVcpu = some ov) + (hkov : σ.kheap ov = some (.archObj (.vcpu vov))) + (hpc : p ≠ c) (hpov : p ≠ ov) + (huf_iff : ∀ q, inUserFrame q (updKSc σ p o) ↔ inUserFrame q σ) + (hsc_ne : ∀ sc, xc.tcbSchedContext = some sc → p ≠ sc) + (htcb : ∀ t, vov.vcpuTcb = some t → p ≠ t ∧ ∃ xt, σ.kheap t = some (.tcb xt)) : + (∃ σ', fl4CompleteRun (Z := Unit) c stop w1 w2 w3 σ = ({((), σ')}, False) ∧ + fl4CompleteRun (Z := Unit) c stop w1 w2 w3 (updKSc σ p o) = ({((), updKSc σ' p o)}, False) ∧ + σ'.archState = σ.archState ∧ + σ'.kheap ov = some (.archObj (.vcpu { vov with vcpuExt := { vov.vcpuExt with runCaller := none } })) ∧ + (∃ xc', σ'.kheap c = some (.tcb xc') ∧ xc'.tcbArch.tcbVcpu = xc.tcbArch.tcbVcpu ∧ xc'.tcbExt.tcbRunVcpu = none) ∧ + (∀ q, q ≠ c → q ≠ ov → σ'.kheap q = σ.kheap q)) ∨ + (fl4CompleteRun (Z := Unit) c stop w1 w2 w3 σ = (∅, True) ∧ + fl4CompleteRun (Z := Unit) c stop w1 w2 w3 (updKSc σ p o) = (∅, True)) := by + have hne_c_ov : c ≠ ov := fun e => by rw [e, hkov] at hkc; cases hkc + have hkc' : (updKSc σ p o).kheap c = some (.tcb xc) := by rw [updKSc_other _ hpc.symm]; exact hkc + have hkov' : (updKSc σ p o).kheap ov = some (.archObj (.vcpu vov)) := by rw [updKSc_other _ hpov.symm]; exact hkov + have hgetCap : getCap (Z := Unit) (c, tcbCnodeIndex 2) σ = ({(xc.tcbIpcframe, σ)}, False) := + getCap_tcbIpcframeA hkc + have hgetCap' : getCap (Z := Unit) (c, tcbCnodeIndex 2) (updKSc σ p o) = ({(xc.tcbIpcframe, updKSc σ p o)}, False) := + getCap_tcbIpcframeA hkc' + -- Step A: optional `fl4ReadStateToFrame` only changes `machineState` (or both fail) + have hRead : (∃ ms1, + (NonDet.when (vov.vcpuExt.runOutMask ≠ 0) do + let buf ← getCap (Z := Unit) (c, tcbCnodeIndex 2) + match frameRightsDev buf, stateWindow buf vov.vcpuExt.runOffset with + | some (R, false), some (some frame) => + NonDet.when (R = vmReadWrite) <| fl4ReadStateToFrame ov frame vov.vcpuExt.runOutMask + | _, _ => pure ()) σ = ({((), { σ with machineState := ms1 })}, False) ∧ + (NonDet.when (vov.vcpuExt.runOutMask ≠ 0) do + let buf ← getCap (Z := Unit) (c, tcbCnodeIndex 2) + match frameRightsDev buf, stateWindow buf vov.vcpuExt.runOffset with + | some (R, false), some (some frame) => + NonDet.when (R = vmReadWrite) <| fl4ReadStateToFrame ov frame vov.vcpuExt.runOutMask + | _, _ => pure ()) (updKSc σ p o) = ({((), updKSc { σ with machineState := ms1 } p o)}, False)) ∨ + ((NonDet.when (vov.vcpuExt.runOutMask ≠ 0) do + let buf ← getCap (Z := Unit) (c, tcbCnodeIndex 2) + match frameRightsDev buf, stateWindow buf vov.vcpuExt.runOffset with + | some (R, false), some (some frame) => + NonDet.when (R = vmReadWrite) <| fl4ReadStateToFrame ov frame vov.vcpuExt.runOutMask + | _, _ => pure ()) σ = (∅, True) ∧ + (NonDet.when (vov.vcpuExt.runOutMask ≠ 0) do + let buf ← getCap (Z := Unit) (c, tcbCnodeIndex 2) + match frameRightsDev buf, stateWindow buf vov.vcpuExt.runOffset with + | some (R, false), some (some frame) => + NonDet.when (R = vmReadWrite) <| fl4ReadStateToFrame ov frame vov.vcpuExt.runOutMask + | _, _ => pure ()) (updKSc σ p o) = (∅, True)) := by + by_cases hm : vov.vcpuExt.runOutMask ≠ 0 + · unfold NonDet.when; rw [if_pos hm, nd_bind_oneSc hgetCap, nd_bind_oneSc hgetCap'] + cases frameRightsDev xc.tcbIpcframe with + | none => exact Or.inl ⟨σ.machineState, rfl, rfl⟩ + | some rd => + cases rd with + | mk R dev => + cases dev with + | true => exact Or.inl ⟨σ.machineState, rfl, rfl⟩ + | false => + cases stateWindow xc.tcbIpcframe vov.vcpuExt.runOffset with + | none => exact Or.inl ⟨σ.machineState, rfl, rfl⟩ + | some fopt => + cases fopt with + | none => exact Or.inl ⟨σ.machineState, rfl, rfl⟩ + | some frame => + dsimp only + by_cases hR : R = vmReadWrite + · rw [if_pos hR] + exact fl4ReadStateToFrame_updKScA vov.vcpuExt.runOutMask hpov hkov huf_iff htcb + · rw [if_neg hR]; exact Or.inl ⟨σ.machineState, rfl, rfl⟩ + · unfold NonDet.when; rw [if_neg hm]; exact Or.inl ⟨σ.machineState, rfl, rfl⟩ + rcases hRead with ⟨ms1, hR1, hR1'⟩ | ⟨hRf, hRf'⟩ + swap + · refine Or.inr ⟨?_, ?_⟩ + · unfold fl4CompleteRun + rw [nd_bind_oneSc (getRunVcpu_eqA hkc), hrun]; dsimp only + rw [nd_bind_oneSc (fl4VcpuFlush_noneA hcur ov), nd_bind_oneSc (getVcpu_eqA hkov)] + exact nd_bind_failA hRf + · unfold fl4CompleteRun + rw [nd_bind_oneSc (getRunVcpu_eqA hkc'), hrun]; dsimp only + rw [nd_bind_oneSc (fl4VcpuFlush_noneA (σ := updKSc σ p o) hcur ov), nd_bind_oneSc (getVcpu_eqA hkov')] + exact nd_bind_failA hRf' + left + set σ1 := { σ with machineState := ms1 } + -- Step B: tail updates only `ov` and `c` in `kheap` + let vov' : Vcpu := { vov with vcpuExt := { vov.vcpuExt with runCaller := none } } + let xc0 : Tcb := { xc with tcbExt := { xc.tcbExt with tcbRunVcpu := none } } + let u0 := xc0.tcbArch.tcbContext + let u1 := { u0 with userRegs := Function.update u0.userRegs badge_register 0 } + let u2 := { u1 with userRegs := Function.update u1.userRegs .x2 stop.word } + let u3 := { u2 with userRegs := Function.update u2.userRegs .x3 w1 } + let u4 := { u3 with userRegs := Function.update u3.userRegs .x4 w2 } + let u5 := { u4 with userRegs := Function.update u4.userRegs .x5 w3 } + let u6 := { u5 with userRegs := Function.update u5.userRegs msg_info_register (messageInfoToData ⟨4, 0, 0, 0⟩) } + let u7 := { u6 with userRegs := Function.update u6.userRegs faultRegister (u6.userRegs nextInstructionRegister) } + let xc_fin : Tcb := { xc0 with tcbArch := { xc0.tcbArch with tcbContext := u7 }, tcbState := .running } + let σ_pre := updKSc (updKSc σ1 ov (.archObj (.vcpu vov'))) c (.tcb xc_fin) + let σ_fin := if c = σ_pre.curThread ∧ σ_pre.schedulerAction = .resumeCurThread ∧ ¬schedulable c σ_pre + then { σ_pre with schedulerAction := .chooseNewThread } else σ_pre + have htail : ∀ τ : State Unit, + τ.kheap ov = some (.archObj (.vcpu vov)) → τ.kheap c = some (.tcb xc) → + (do + fl4VcpuExtUpdate (Z := Unit) ov fun e => { e with runCaller := none } + setRunVcpu c none + asUser c (setRegister badge_register 0) + asUser c do + setRegister .x2 stop.word + setRegister .x3 w1 + setRegister .x4 w2 + setRegister .x5 w3 + setMessageInfo c ⟨4, 0, 0, 0⟩ + asUser c do + let ip ← getRegister nextInstructionRegister + setRegister faultRegister ip + setThreadState c .running) τ = + (let τf := updKSc (updKSc τ ov (.archObj (.vcpu vov'))) c (.tcb xc_fin) + ({((), if c = τf.curThread ∧ τf.schedulerAction = .resumeCurThread ∧ ¬schedulable c τf + then { τf with schedulerAction := .chooseNewThread } else τf)}, False)) := by + intro τ hτv hτc + have hτc1 : (updKSc τ ov (.archObj (.vcpu vov'))).kheap c = some (.tcb xc) := by + rw [updKSc_other _ hne_c_ov]; exact hτc + rw [nd_bind_oneSc (fl4VcpuExtUpdate_eqA _ hτv)] + rw [nd_bind_oneSc (setRunVcpu_eqA none hτc1)] + have hctx_upd : ∀ (τ' : State Unit) (x : Tcb) (uc : UserContext), + ctxUpdA τ' c x uc = updKSc τ' c (.tcb { x with tcbArch := { x.tcbArch with tcbContext := uc } }) := + fun _ _ _ => rfl + rw [nd_bind_oneSc (asUser_setReg_oneA (updKSc_same _ _ _) badge_register 0), hctx_upd, updKSc_idemA] + have h4_uc : (do setRegister .x2 stop.word; setRegister .x3 w1; setRegister .x4 w2; setRegister .x5 w3) u1 = + ({((), u5)}, False) := by + rw [nd_bind_oneSc (m := setRegister .x2 stop.word) rfl, + nd_bind_oneSc (m := setRegister .x3 w1) rfl, + nd_bind_oneSc (m := setRegister .x4 w2) rfl]; rfl + have h4 : asUser (Z := Unit) c (do setRegister .x2 stop.word; setRegister .x3 w1; setRegister .x4 w2; setRegister .x5 w3) + (updKSc (updKSc τ ov (.archObj (.vcpu vov'))) c (.tcb { xc0 with tcbArch := { xc0.tcbArch with tcbContext := u1 } })) = + ({((), updKSc (updKSc τ ov (.archObj (.vcpu vov'))) c (.tcb { xc0 with tcbArch := { xc0.tcbArch with tcbContext := u5 } }))}, False) := by + rw [asUser_det_oneA (updKSc_same _ _ _) h4_uc, hctx_upd, updKSc_idemA] + rw [nd_bind_oneSc h4] + have hmi : setMessageInfo (Z := Unit) c ⟨4, 0, 0, 0⟩ + (updKSc (updKSc τ ov (.archObj (.vcpu vov'))) c (.tcb { xc0 with tcbArch := { xc0.tcbArch with tcbContext := u5 } })) = + ({((), updKSc (updKSc τ ov (.archObj (.vcpu vov'))) c (.tcb { xc0 with tcbArch := { xc0.tcbArch with tcbContext := u6 } }))}, False) := by + unfold setMessageInfo + rw [asUser_setReg_oneA (updKSc_same _ _ _) msg_info_register (messageInfoToData ⟨4, 0, 0, 0⟩), hctx_upd, updKSc_idemA] + rw [nd_bind_oneSc hmi] + have hpc_uc : (do let ip ← getRegister nextInstructionRegister; setRegister faultRegister ip) u6 = + ({((), u7)}, False) := by + rw [nd_bind_oneSc (m := getRegister nextInstructionRegister) rfl]; rfl + have hpc_set : asUser (Z := Unit) c (do let ip ← getRegister nextInstructionRegister; setRegister faultRegister ip) + (updKSc (updKSc τ ov (.archObj (.vcpu vov'))) c (.tcb { xc0 with tcbArch := { xc0.tcbArch with tcbContext := u6 } })) = + ({((), updKSc (updKSc τ ov (.archObj (.vcpu vov'))) c (.tcb { xc0 with tcbArch := { xc0.tcbArch with tcbContext := u7 } }))}, False) := by + rw [asUser_det_oneA (updKSc_same _ _ _) hpc_uc, hctx_upd, updKSc_idemA] + rw [nd_bind_oneSc hpc_set] + unfold setThreadState + rw [nd_bind_oneSc (threadSet_eqA _ (updKSc_same _ _ _)), updKSc_idemA] + exact setThreadStateAct_eqA _ c + have hcomm_pre : updKSc (updKSc (updKSc σ1 p o) ov (.archObj (.vcpu vov'))) c (.tcb xc_fin) = updKSc σ_pre p o := by + rw [updKSc_commA σ1 hpov, updKSc_commA _ hpc] + have hsch_eq : schedulable c (updKSc σ_pre p o) = schedulable c σ_pre := + schedulable_updKScA (updKSc_same _ _ _) hpc hsc_ne + have hcomm_fin : + (if c = (updKSc σ_pre p o).curThread ∧ (updKSc σ_pre p o).schedulerAction = .resumeCurThread ∧ ¬schedulable c (updKSc σ_pre p o) + then { updKSc σ_pre p o with schedulerAction := .chooseNewThread } else updKSc σ_pre p o) = + updKSc σ_fin p o := by + dsimp only [σ_fin] + have hcur_eq : (updKSc σ_pre p o).curThread = σ_pre.curThread := rfl + have hsa_eq : (updKSc σ_pre p o).schedulerAction = σ_pre.schedulerAction := rfl + rw [hcur_eq, hsa_eq, hsch_eq] + split_ifs <;> rfl + have hfin_kheap : σ_fin.kheap = σ_pre.kheap := by dsimp only [σ_fin]; split_ifs <;> rfl + have hfin_arch : σ_fin.archState = σ.archState := by dsimp only [σ_fin]; split_ifs <;> rfl + refine ⟨σ_fin, ?_, ?_, hfin_arch, by rw [hfin_kheap, updKSc_other _ hne_c_ov.symm, updKSc_same], + ⟨xc_fin, by rw [hfin_kheap, updKSc_same], rfl, rfl⟩, + fun q hqc hqov => by rw [hfin_kheap, updKSc_other _ hqc, updKSc_other _ hqov]⟩ + · unfold fl4CompleteRun + rw [nd_bind_oneSc (getRunVcpu_eqA hkc), hrun]; dsimp only + rw [nd_bind_oneSc (fl4VcpuFlush_noneA hcur ov), nd_bind_oneSc (getVcpu_eqA hkov)] + exact (nd_bind_oneSc hR1 _).trans (htail σ1 hkov hkc) + · unfold fl4CompleteRun + rw [nd_bind_oneSc (getRunVcpu_eqA hkc'), hrun]; dsimp only + rw [nd_bind_oneSc (fl4VcpuFlush_noneA (σ := updKSc σ p o) hcur ov), nd_bind_oneSc (getVcpu_eqA hkov')] + refine (nd_bind_oneSc hR1' _).trans ?_ + rw [htail (updKSc σ1 p o) hkov' hkc'] + dsimp only; rw [hcomm_pre, hcomm_fin] + +/-- `fl4VcpuEndRun` commutes with a single disjoint `updKSc`. -/ +private theorem fl4VcpuEndRun_updKScA {ov p : ObjRef} {vov : Vcpu} {o : KernelObject} + {σ : State Unit} + (hcur : σ.archState.armCurrentVcpu = none) + (hkov : σ.kheap ov = some (.archObj (.vcpu vov))) + (hpov : p ≠ ov) + (huf_iff : ∀ q, inUserFrame q (updKSc σ p o) ↔ inUserFrame q σ) + (hrc : ∀ c, vov.vcpuExt.runCaller = some c → p ≠ c ∧ + ∃ xc, σ.kheap c = some (.tcb xc) ∧ xc.tcbExt.tcbRunVcpu = some ov ∧ + (∀ sc, xc.tcbSchedContext = some sc → p ≠ sc)) + (htcb : ∀ t, vov.vcpuTcb = some t → p ≠ t ∧ ∃ xt, σ.kheap t = some (.tcb xt)) : + (∃ σ', fl4VcpuEndRun (Z := Unit) ov σ = ({((), σ')}, False) ∧ + fl4VcpuEndRun (Z := Unit) ov (updKSc σ p o) = ({((), updKSc σ' p o)}, False) ∧ + σ'.archState = σ.archState ∧ + σ'.kheap ov = some (.archObj (.vcpu { vov with vcpuExt := { vov.vcpuExt with runCaller := none } })) ∧ + (∀ q, q ≠ ov → vov.vcpuExt.runCaller ≠ some q → σ'.kheap q = σ.kheap q) ∧ + (∀ c xc, vov.vcpuExt.runCaller = some c → σ.kheap c = some (.tcb xc) → + ∃ xc', σ'.kheap c = some (.tcb xc') ∧ xc'.tcbArch.tcbVcpu = xc.tcbArch.tcbVcpu ∧ xc'.tcbExt.tcbRunVcpu = none)) ∨ + (fl4VcpuEndRun (Z := Unit) ov σ = (∅, True) ∧ + fl4VcpuEndRun (Z := Unit) ov (updKSc σ p o) = (∅, True)) := by + have hkov' : (updKSc σ p o).kheap ov = some (.archObj (.vcpu vov)) := by rw [updKSc_other _ hpov.symm]; exact hkov + cases hrc0 : vov.vcpuExt.runCaller with + | none => + have hvov_eq : { vov with vcpuExt := { vov.vcpuExt with runCaller := none } } = vov := by + cases vov with | mk s t vm e => cases e; cases hrc0; rfl + exact Or.inl ⟨σ, fl4VcpuEndRun_noneA hkov hrc0, fl4VcpuEndRun_noneA hkov' hrc0, rfl, + by rw [hvov_eq]; exact hkov, + fun _ _ _ => rfl, fun _ _ h => by cases h⟩ + | some c => + obtain ⟨hpc, xc, hkc, hcrv, hsc_ne⟩ := hrc c hrc0 + have hkc' : (updKSc σ p o).kheap c = some (.tcb xc) := by rw [updKSc_other _ hpc.symm]; exact hkc + have hne_c_ov : c ≠ ov := fun e => by rw [e, hkov] at hkc; cases hkc + by_cases hvm : xc.tcbState = .ext .runningVM + · rcases fl4CompleteRun_updKScA (o := o) (stop := .interrupt) (w1 := 0) (w2 := 0) (w3 := 0) + hcur hkc hcrv hkov hpc hpov huf_iff hsc_ne htcb with + ⟨σ', hcr, hcr', harch', hkov_f, ⟨xc', hkc_f, htv_f, hrv_f⟩, hkh_f⟩ | ⟨hf, hf'⟩ + · refine Or.inl ⟨σ', by rw [fl4VcpuEndRun_runningVmA hkov hkc hrc0 hvm]; exact hcr, + by rw [fl4VcpuEndRun_runningVmA hkov' hkc' hrc0 hvm]; exact hcr', + harch', hkov_f, + fun q hqov hqc => hkh_f q (fun e => hqc (by rw [e])) hqov, + fun c' xc' hc' hkc0 => by + cases hc'; rw [hkc] at hkc0; cases hkc0 + exact ⟨xc', hkc_f, htv_f, hrv_f⟩⟩ + · exact Or.inr ⟨by rw [fl4VcpuEndRun_runningVmA hkov hkc hrc0 hvm]; exact hf, + by rw [fl4VcpuEndRun_runningVmA hkov' hkc' hrc0 hvm]; exact hf'⟩ + · let vov' : Vcpu := { vov with vcpuExt := { vov.vcpuExt with runCaller := none } } + let xc' : Tcb := { xc with tcbExt := { xc.tcbExt with tcbRunVcpu := none } } + let σ' := updKSc (updKSc σ ov (.archObj (.vcpu vov'))) c (.tcb xc') + have hcomm : updKSc (updKSc (updKSc σ p o) ov (.archObj (.vcpu vov'))) c (.tcb xc') = updKSc σ' p o := by + rw [updKSc_commA σ hpov, updKSc_commA _ hpc] + refine Or.inl ⟨σ', fl4VcpuEndRun_staleA hkov hkc hrc0 hvm, + by rw [fl4VcpuEndRun_staleA hkov' hkc' hrc0 hvm, hcomm], + rfl, by rw [updKSc_other _ hne_c_ov.symm, updKSc_same], + fun q hqov hqc => by + have hqc' : q ≠ c := fun e => hqc (by rw [e]) + rw [updKSc_other _ hqc', updKSc_other _ hqov], + fun c' xc0 hc' hkc0 => by + cases hc'; rw [hkc] at hkc0; cases hkc0 + exact ⟨xc', updKSc_same _ _ _, rfl, rfl⟩⟩ + +/-- Rust execution order of `vcpu.decode_set_tcb` (after `TS_RESTART`). -/ +def fl4VcpuSetTcbRustOrder (vc t : ObjRef) : SMonad Unit (List Data) := do + fl4VcpuUnbindExt vc + let oldV ← archThreadGet ArchTcb.tcbVcpu t + match oldV with + | some ov => fl4VcpuUnbindExt ov + | none => pure () + let rv ← getRunVcpu t + NonDet.when (rv ≠ none) do + let st ← getThreadState t + if st = .ext .runningVM then fl4CompleteRun t .interrupt 0 0 0 else fl4CancelRun t + archThreadSet (fun atcb => { atcb with tcbVcpu := some vc }) t + vcpuUpdate vc fun v => { v with vcpuTcb := some t } + return [] + +private theorem fl4Assoc_tail_none_noneA {σ : State Unit} {vc t : ObjRef} {v : Vcpu} {xt : Tcb} + (hkv : σ.kheap vc = some (.archObj (.vcpu v))) (hkt : σ.kheap t = some (.tcb xt)) + (hvt : v.vcpuTcb = none) (htv : xt.tcbArch.tcbVcpu = none) : + (fl4AssociateVcpuTcb (Z := Unit) vc t >>= fun _ => pure ([] : List Data)) σ = + (do + archThreadSet (Z := Unit) (fun atcb => { atcb with tcbVcpu := some vc }) t + vcpuUpdate (Z := Unit) vc fun v => { v with vcpuTcb := some t } + pure ([] : List Data)) σ := by + unfold fl4AssociateVcpuTcb + rw [nd_bind_assocA, nd_bind_oneSc (archThreadGet_eqA _ hkt), htv] + dsimp only + rw [nd_bind_assocA, nd_bind_oneSc (getVcpu_eqA hkv), hvt] + dsimp only + rw [nd_bind_assocA] + +set_option maxHeartbeats 400000 in +/-- **Pure spec equivalence**: on any well-formed VCPU state (bijective bind/run links and +`bind_run_disj`), `fl4VcpuSetTcbRustOrder vc t` produces the exact same result as +`fl4PerformVcpuInv (.setTcb vc t)`. -/ +theorem fl4SetTcb_rust_order_eqA {σ : State Unit} {vc t : ObjRef} {v0 : Vcpu} {xt0 : Tcb} + (inT : ObjRef → Prop) (ht_in : inT t) + (hcur : σ.archState.armCurrentVcpu = none) + (hkv0 : σ.kheap vc = some (.archObj (.vcpu v0))) + (hkt0 : σ.kheap t = some (.tcb xt0)) + (hbind_v2t : ∀ v' xv p, σ.kheap v' = some (.archObj (.vcpu xv)) → xv.vcpuTcb = some p → + inT p ∧ ∃ xp, σ.kheap p = some (.tcb xp) ∧ xp.tcbArch.tcbVcpu = some v') + (hbind_t2v : ∀ p xp v', inT p → σ.kheap p = some (.tcb xp) → xp.tcbArch.tcbVcpu = some v' → + ∃ xv, σ.kheap v' = some (.archObj (.vcpu xv)) ∧ xv.vcpuTcb = some p) + (hrun_v2c : ∀ v' xv c, σ.kheap v' = some (.archObj (.vcpu xv)) → xv.vcpuExt.runCaller = some c → + inT c ∧ ∃ xc, σ.kheap c = some (.tcb xc) ∧ xc.tcbExt.tcbRunVcpu = some v') + (hrun_c2v : ∀ c xc v', inT c → σ.kheap c = some (.tcb xc) → xc.tcbExt.tcbRunVcpu = some v' → + ∃ xv, σ.kheap v' = some (.archObj (.vcpu xv)) ∧ xv.vcpuExt.runCaller = some c) + (hdisj : ∀ p xp, inT p → σ.kheap p = some (.tcb xp) → xp.tcbArch.tcbVcpu ≠ none → xp.tcbExt.tcbRunVcpu = none) + (hsc_ty : ∀ p xp sc, inT p → σ.kheap p = some (.tcb xp) → xp.tcbSchedContext = some sc → ∃ y n, σ.kheap sc = some (.schedContext y n)) + {σ' : State Unit} + (hrust : fl4VcpuSetTcbRustOrder vc t σ = ({([], σ')}, False)) : + fl4PerformVcpuInv (Z := Unit) (.setTcb vc t) σ = ({([], σ')}, False) := by + have hne_vc_t : vc ≠ t := fun e => by rw [e, hkt0] at hkv0; cases hkv0 + -- Helper: evaluate `fl4VcpuEndRun vc` from `σ` to `σ1` + have hend1 : ∃ σ1 v1 xt1, + fl4VcpuEndRun (Z := Unit) vc σ = ({((), σ1)}, False) ∧ + σ1.archState = σ.archState ∧ + σ1.kheap vc = some (.archObj (.vcpu v1)) ∧ + v1.vcpuTcb = v0.vcpuTcb ∧ v1.vcpuExt.runCaller = none ∧ + σ1.kheap t = some (.tcb xt1) ∧ + xt1.tcbArch.tcbVcpu = xt0.tcbArch.tcbVcpu ∧ + (∀ q, q ≠ vc → (v0.vcpuExt.runCaller ≠ some q) → σ1.kheap q = σ.kheap q) ∧ + (v0.vcpuExt.runCaller = some t → xt1.tcbExt.tcbRunVcpu = none) ∧ + (v0.vcpuExt.runCaller ≠ some t → xt1 = xt0) := by + cases hrc0 : v0.vcpuExt.runCaller with + | none => + exact ⟨σ, v0, xt0, fl4VcpuEndRun_noneA hkv0 hrc0, rfl, hkv0, rfl, hrc0, hkt0, rfl, + fun _ _ _ => rfl, (fun h => by cases h), fun _ => rfl⟩ + | some c => + obtain ⟨hc_in, xc, hkc, hcrv⟩ := hrun_v2c vc v0 c hkv0 hrc0 + have hne_vc_c : vc ≠ c := fun e => by rw [e, hkc] at hkv0; cases hkv0 + by_cases hvm : xc.tcbState = .ext .runningVM + · -- Reuse `fl4CompleteRun_updKScA` with `p := c`? No, `p ≠ c`, so use `p := vc`? No, `p ≠ ov`! + -- Wait: just invoke `fl4CompleteRun_updKScA` with a dummy or evaluate directly! + -- Actually we only need `fl4VcpuEndRun vc σ` (no `updKSc`!), let's evaluate `fl4CompleteRun` cleanly! + have hgetCap : getCap (Z := Unit) (c, tcbCnodeIndex 2) σ = ({(xc.tcbIpcframe, σ)}, False) := + getCap_tcbIpcframeA hkc + have hRead : ∃ ms1, + (NonDet.when (v0.vcpuExt.runOutMask ≠ 0) do + let buf ← getCap (Z := Unit) (c, tcbCnodeIndex 2) + match frameRightsDev buf, stateWindow buf v0.vcpuExt.runOffset with + | some (R, false), some (some frame) => + NonDet.when (R = vmReadWrite) <| fl4ReadStateToFrame vc frame v0.vcpuExt.runOutMask + | _, _ => pure ()) σ = ({((), { σ with machineState := ms1 })}, False) := by + by_cases hm : v0.vcpuExt.runOutMask ≠ 0 + · unfold NonDet.when; rw [if_pos hm, nd_bind_oneSc hgetCap] + cases hfrd : frameRightsDev xc.tcbIpcframe with + | none => exact ⟨σ.machineState, rfl⟩ + | some rd => + cases rd with + | mk R dev => + cases dev with + | true => exact ⟨σ.machineState, rfl⟩ + | false => + cases hsw : stateWindow xc.tcbIpcframe v0.vcpuExt.runOffset with + | none => exact ⟨σ.machineState, rfl⟩ + | some fopt => + cases fopt with + | none => exact ⟨σ.machineState, rfl⟩ + | some frame => + dsimp only + by_cases hR : R = vmReadWrite + · rw [if_pos hR] + have hpt' : ∀ u, v0.vcpuTcb = some u → c ≠ u ∧ ∃ xu, σ.kheap u = some (.tcb xu) := fun u hu => by + obtain ⟨hu_in, xu, hku, htvu⟩ := hbind_v2t vc v0 u hkv0 hu + refine ⟨fun ecu => ?_, xu, hku⟩ + subst ecu; rw [hku] at hkc; cases hkc + have hnr := hdisj c xc hc_in hku (by rw [htvu]; simp) + rw [hcrv] at hnr; cases hnr + rcases fl4ReadStateToFrame_updKScA (p := c) (o := .tcb xc) v0.vcpuExt.runOutMask hne_vc_c.symm hkv0 + (inUserFrame_updKSc_iffA hkc (fun _ h => by cases h) (fun _ h => by cases h)) hpt' with + ⟨ms1, hrf, -⟩ | ⟨hfail, -⟩ + · exact ⟨ms1, hrf⟩ + · exfalso + have hcr_fail : fl4CompleteRun (Z := Unit) c .interrupt 0 0 0 σ = (∅, True) := by + unfold fl4CompleteRun + rw [nd_bind_oneSc (getRunVcpu_eqA hkc), hcrv]; dsimp only + rw [nd_bind_oneSc (fl4VcpuFlush_noneA hcur vc), nd_bind_oneSc (getVcpu_eqA hkv0)] + refine nd_bind_failA ?_ + unfold NonDet.when; rw [if_pos hm, nd_bind_oneSc hgetCap, hfrd, hsw]; dsimp only + rw [if_pos hR]; exact hfail + have hend_fail : fl4VcpuEndRun (Z := Unit) vc σ = (∅, True) := by + rw [fl4VcpuEndRun_runningVmA hkv0 hkc hrc0 hvm]; exact hcr_fail + unfold fl4VcpuSetTcbRustOrder fl4VcpuUnbindExt at hrust + rw [nd_bind_assocA, nd_bind_failA hend_fail] at hrust + have hne_f := (Prod.mk.inj hrust).2; simp at hne_f + · rw [if_neg hR]; exact ⟨σ.machineState, rfl⟩ + · unfold NonDet.when; rw [if_neg hm]; exact ⟨σ.machineState, rfl⟩ + obtain ⟨ms1, hR1⟩ := hRead + set σm := { σ with machineState := ms1 } + let v1 : Vcpu := { v0 with vcpuExt := { v0.vcpuExt with runCaller := none } } + let xc0 : Tcb := { xc with tcbExt := { xc.tcbExt with tcbRunVcpu := none } } + let u0 := xc0.tcbArch.tcbContext + let u1 := { u0 with userRegs := Function.update u0.userRegs badge_register 0 } + let u2 := { u1 with userRegs := Function.update u1.userRegs .x2 RunStop.interrupt.word } + let u3 := { u2 with userRegs := Function.update u2.userRegs .x3 0 } + let u4 := { u3 with userRegs := Function.update u3.userRegs .x4 0 } + let u5 := { u4 with userRegs := Function.update u4.userRegs .x5 0 } + let u6 := { u5 with userRegs := Function.update u5.userRegs msg_info_register (messageInfoToData ⟨4, 0, 0, 0⟩) } + let u7 := { u6 with userRegs := Function.update u6.userRegs faultRegister (u6.userRegs nextInstructionRegister) } + let xc_fin : Tcb := { xc0 with tcbArch := { xc0.tcbArch with tcbContext := u7 }, tcbState := .running } + let σ1_pre := updKSc (updKSc σm vc (.archObj (.vcpu v1))) c (.tcb xc_fin) + let σ1 := if c = σ1_pre.curThread ∧ σ1_pre.schedulerAction = .resumeCurThread ∧ ¬schedulable c σ1_pre + then { σ1_pre with schedulerAction := .chooseNewThread } else σ1_pre + have hσ1_kh : σ1.kheap = σ1_pre.kheap := by dsimp only [σ1]; split_ifs <;> rfl + have hσ1_arch : σ1.archState = σ.archState := by dsimp only [σ1]; split_ifs <;> rfl + have hcomp : fl4VcpuEndRun (Z := Unit) vc σ = ({((), σ1)}, False) := by + rw [fl4VcpuEndRun_runningVmA hkv0 hkc hrc0 hvm] + unfold fl4CompleteRun + rw [nd_bind_oneSc (getRunVcpu_eqA hkc), hcrv]; dsimp only + rw [nd_bind_oneSc (fl4VcpuFlush_noneA hcur vc), nd_bind_oneSc (getVcpu_eqA hkv0)] + refine (nd_bind_oneSc hR1 _).trans ?_ + have hctx_upd : ∀ (τ' : State Unit) (x : Tcb) (uc : UserContext), + ctxUpdA τ' c x uc = updKSc τ' c (.tcb { x with tcbArch := { x.tcbArch with tcbContext := uc } }) := + fun _ _ _ => rfl + have hkc1 : (updKSc σm vc (.archObj (.vcpu v1))).kheap c = some (.tcb xc) := by + rw [updKSc_other _ hne_vc_c.symm]; exact hkc + rw [nd_bind_oneSc (fl4VcpuExtUpdate_eqA (σ := σm) _ hkv0), nd_bind_oneSc (setRunVcpu_eqA none hkc1)] + rw [nd_bind_oneSc (asUser_setReg_oneA (updKSc_same _ _ _) badge_register 0), hctx_upd, updKSc_idemA] + have h4_uc : (do setRegister .x2 RunStop.interrupt.word; setRegister .x3 0; setRegister .x4 0; setRegister .x5 0) u1 = + ({((), u5)}, False) := by + rw [nd_bind_oneSc (m := setRegister .x2 RunStop.interrupt.word) rfl, + nd_bind_oneSc (m := setRegister .x3 0) rfl, + nd_bind_oneSc (m := setRegister .x4 0) rfl]; rfl + rw [nd_bind_oneSc (by rw [asUser_det_oneA (updKSc_same _ _ _) h4_uc, hctx_upd, updKSc_idemA])] + have hmi : setMessageInfo (Z := Unit) c ⟨4, 0, 0, 0⟩ + (updKSc (updKSc σm vc (.archObj (.vcpu v1))) c (.tcb { xc0 with tcbArch := { xc0.tcbArch with tcbContext := u5 } })) = + ({((), updKSc (updKSc σm vc (.archObj (.vcpu v1))) c (.tcb { xc0 with tcbArch := { xc0.tcbArch with tcbContext := u6 } }))}, False) := by + unfold setMessageInfo + rw [asUser_setReg_oneA (updKSc_same _ _ _) msg_info_register (messageInfoToData ⟨4, 0, 0, 0⟩), hctx_upd, updKSc_idemA] + rw [nd_bind_oneSc hmi] + have hpc_uc : (do let ip ← getRegister nextInstructionRegister; setRegister faultRegister ip) u6 = + ({((), u7)}, False) := by + rw [nd_bind_oneSc (m := getRegister nextInstructionRegister) rfl]; rfl + rw [nd_bind_oneSc (by rw [asUser_det_oneA (updKSc_same _ _ _) hpc_uc, hctx_upd, updKSc_idemA])] + unfold setThreadState + rw [nd_bind_oneSc (threadSet_eqA _ (updKSc_same _ _ _)), updKSc_idemA] + exact setThreadStateAct_eqA _ c + by_cases ect : c = t + · subst ect + rw [hkt0] at hkc; cases hkc + exact ⟨σ1, v1, xc_fin, hcomp, hσ1_arch, by rw [hσ1_kh, updKSc_other _ hne_vc_c, updKSc_same], rfl, rfl, + by rw [hσ1_kh, updKSc_same], rfl, + fun q hqv hqc => by + have hqc' : q ≠ c := fun e => hqc (by rw [e]) + rw [hσ1_kh, updKSc_other _ hqc', updKSc_other _ hqv], + fun _ => rfl, fun h => absurd rfl h⟩ + · exact ⟨σ1, v1, xt0, hcomp, hσ1_arch, by rw [hσ1_kh, updKSc_other _ hne_vc_c, updKSc_same], rfl, rfl, + by rw [hσ1_kh, updKSc_other _ (Ne.symm ect), updKSc_other _ hne_vc_t.symm]; exact hkt0, rfl, + fun q hqv hqc => by + have hqc' : q ≠ c := fun e => hqc (by rw [e]) + rw [hσ1_kh, updKSc_other _ hqc', updKSc_other _ hqv], + fun h => absurd (Option.some.inj h) ect, fun _ => rfl⟩ + · let v1 : Vcpu := { v0 with vcpuExt := { v0.vcpuExt with runCaller := none } } + let xc1 : Tcb := { xc with tcbExt := { xc.tcbExt with tcbRunVcpu := none } } + let σ1 := updKSc (updKSc σ vc (.archObj (.vcpu v1))) c (.tcb xc1) + have hst := fl4VcpuEndRun_staleA hkv0 hkc hrc0 hvm + by_cases ect : c = t + · subst ect; rw [hkt0] at hkc; cases hkc + exact ⟨σ1, v1, xc1, hst, rfl, by rw [updKSc_other _ hne_vc_c, updKSc_same], rfl, rfl, + updKSc_same _ _ _, rfl, + fun q hqv hqc => by + have hqc' : q ≠ c := fun e => hqc (by rw [e]) + rw [updKSc_other _ hqc', updKSc_other _ hqv], + fun _ => rfl, fun h => absurd rfl h⟩ + · exact ⟨σ1, v1, xt0, hst, rfl, by rw [updKSc_other _ hne_vc_c, updKSc_same], rfl, rfl, + by rw [updKSc_other _ (Ne.symm ect), updKSc_other _ hne_vc_t.symm]; exact hkt0, rfl, + fun q hqv hqc => by + have hqc' : q ≠ c := fun e => hqc (by rw [e]) + rw [updKSc_other _ hqc', updKSc_other _ hqv], + fun h => absurd (Option.some.inj h) ect, fun _ => rfl⟩ + obtain ⟨σ1, v1, xt1, hend1_eq, harch1, hkv1, hvt1, hrc1, hkt1, htv1, hkh1_other, hrv1_eq, hxt1_ne⟩ := hend1 + have hcur1 : σ1.archState.armCurrentVcpu = none := by rw [harch1]; exact hcur + -- Now split on `v0.vcpuTcb` + cases hv0_tcb : v0.vcpuTcb with + | none => + -- Rust Step 1 (`fl4VcpuUnbindExt vc`) does ONLY `fl4VcpuEndRun vc`! + have hvt1_none : v1.vcpuTcb = none := by rw [hvt1, hv0_tcb] + have hub1 : fl4VcpuUnbindExt vc σ = ({((), σ1)}, False) := by + unfold fl4VcpuUnbindExt + rw [nd_bind_oneSc hend1_eq, nd_bind_oneSc (getVcpu_eqA hkv1), hvt1_none]; rfl + unfold fl4VcpuSetTcbRustOrder at hrust + rw [nd_bind_oneSc hub1, nd_bind_oneSc (archThreadGet_eqA _ hkt1)] at hrust + unfold fl4PerformVcpuInv fl4VcpuSetTcbPre + dsimp only + rw [nd_bind_assocA, nd_bind_oneSc hend1_eq, nd_bind_assocA, nd_bind_oneSc (archThreadGet_eqA _ hkt1)] + cases hxt1_v : xt1.tcbArch.tcbVcpu with + | none => + rw [hxt1_v] at hrust; dsimp only at hrust ⊢ + rw [nd_bind_assocA] + rw [nd_bind_oneSc (getRunVcpu_eqA hkt1)] at hrust ⊢ + cases hrv1 : xt1.tcbExt.tcbRunVcpu with + | none => + rw [hrv1] at hrust + rw [nd_when_falseA (by simp)] at hrust ⊢ + rw [nd_bind_oneSc (m := pure ()) rfl] at hrust ⊢ + rw [fl4Assoc_tail_none_noneA hkv1 hkt1 hvt1_none hxt1_v] + exact hrust + | some rv => + rw [hrv1] at hrust + rw [nd_when_trueA (by simp)] at hrust ⊢ + rw [nd_bind_assocA] at hrust ⊢ + unfold getThreadState at hrust ⊢ + rw [nd_bind_oneSc (threadGet_eqSc _ hkt1)] at hrust ⊢ + -- `rv ≠ vc` because `v1.vcpuExt.runCaller = none` (if `rv = vc`, then `v0.runCaller = some t` so `xt1.runVcpu = none`, contradiction!) + have hne_c_t : v0.vcpuExt.runCaller ≠ some t := fun h => by have := hrv1_eq h; rw [hrv1] at this; cases this + have hxt_eq : xt1 = xt0 := hxt1_ne hne_c_t + have hrv0 : xt0.tcbExt.tcbRunVcpu = some rv := by rw [← hxt_eq]; exact hrv1 + obtain ⟨vrv0, hkvrv0, hrcrv0⟩ := hrun_c2v t xt0 rv ht_in hkt0 hrv0 + have hne_rv_vc : rv ≠ vc := fun e => by subst e; rw [hkv0] at hkvrv0; cases hkvrv0; exact hne_c_t hrcrv0 + have hne_rv_c : v0.vcpuExt.runCaller ≠ some rv := fun e => by + obtain ⟨-, xc, hkc, -⟩ := hrun_v2c vc v0 rv hkv0 e; rw [hkvrv0] at hkc; cases hkc + have hkvrv1 : σ1.kheap rv = some (.archObj (.vcpu vrv0)) := by + rw [hkh1_other rv hne_rv_vc hne_rv_c]; exact hkvrv0 + by_cases hvm : xt1.tcbState = .ext .runningVM + · rw [if_pos hvm] at hrust ⊢ + rcases fl4CompleteRun_updKScA (p := vc) (o := .archObj (.vcpu v1)) + hcur1 hkt1 hrv1 hkvrv1 hne_vc_t hne_rv_vc.symm + (inUserFrame_updKSc_iffA hkv1 (fun _ h => by cases h) (fun _ h => by cases h)) + (fun sc hsc => by + rw [hxt_eq] at hsc; obtain ⟨y, n, hks⟩ := hsc_ty t xt0 sc ht_in hkt0 hsc + intro e; subst e; rw [hkv0] at hks; cases hks) + (fun u hu => by + obtain ⟨hu_in, xu, hku, htvu⟩ := hbind_v2t rv vrv0 u hkvrv0 hu + have hne_u_vc : vc ≠ u := fun e => by subst e; rw [hku] at hkv0; cases hkv0 + have hne_u_c : v0.vcpuExt.runCaller ≠ some u := fun e => by + obtain ⟨-, xc, hkc, hcrv'⟩ := hrun_v2c vc v0 u hkv0 e + rw [hku] at hkc; cases hkc + have hnr := hdisj u xu hu_in hku (by rw [htvu]; simp) + rw [hcrv'] at hnr; cases hnr + exact ⟨hne_u_vc, xu, by rw [hkh1_other u hne_u_vc.symm hne_u_c]; exact hku⟩) with + ⟨σ2, hcr2, -, -, -, ⟨xt2, hkt2, htv2, -⟩, hkh2⟩ | ⟨hcr_fail, -⟩ + swap + · exfalso; rw [nd_bind_failA hcr_fail] at hrust; have hne_f := (Prod.mk.inj hrust).2; simp at hne_f + have hkv2 : σ2.kheap vc = some (.archObj (.vcpu v1)) := by + rw [hkh2 vc hne_vc_t hne_rv_vc.symm]; exact hkv1 + rw [hxt1_v] at htv2 + rw [nd_bind_oneSc hcr2] at hrust ⊢ + rw [fl4Assoc_tail_none_noneA hkv2 hkt2 hvt1_none htv2] + exact hrust + · rw [if_neg hvm] at hrust ⊢ + have hcan := fl4CancelRun_someA hkt1 hkvrv1 hrv1 + rw [nd_bind_oneSc hcan] at hrust ⊢ + have hkt2 : (updKSc (updKSc σ1 rv (.archObj (.vcpu { vrv0 with vcpuExt := { vrv0.vcpuExt with runCaller := none } }))) + t (.tcb { xt1 with tcbExt := { xt1.tcbExt with tcbRunVcpu := none } })).kheap t = + some (.tcb { xt1 with tcbExt := { xt1.tcbExt with tcbRunVcpu := none } }) := updKSc_same _ _ _ + have hkv2 : (updKSc (updKSc σ1 rv (.archObj (.vcpu { vrv0 with vcpuExt := { vrv0.vcpuExt with runCaller := none } }))) + t (.tcb { xt1 with tcbExt := { xt1.tcbExt with tcbRunVcpu := none } })).kheap vc = + some (.archObj (.vcpu v1)) := by + rw [updKSc_other _ hne_vc_t, updKSc_other _ hne_rv_vc.symm]; exact hkv1 + rw [fl4Assoc_tail_none_noneA hkv2 hkt2 hvt1_none hxt1_v] + exact hrust + | some ov => + rw [hxt1_v] at hrust; dsimp only at hrust ⊢ + have htv0 : xt0.tcbArch.tcbVcpu = some ov := by rw [← htv1, hxt1_v] + obtain ⟨vov0, hkov0, hov_tcb⟩ := hbind_t2v t xt0 ov ht_in hkt0 htv0 + have hne_ov_vc : ov ≠ vc := fun e => by subst e; rw [hkv0] at hkov0; cases hkov0; rw [hv0_tcb] at hov_tcb; cases hov_tcb + have hrv0_none : xt0.tcbExt.tcbRunVcpu = none := hdisj t xt0 ht_in hkt0 (by rw [htv0]; simp) + have hne_c_t : v0.vcpuExt.runCaller ≠ some t := fun e => by + obtain ⟨-, xc, hkc, hcrv⟩ := hrun_v2c vc v0 t hkv0 e; rw [hkt0] at hkc; cases hkc; rw [hrv0_none] at hcrv; cases hcrv + have hxt_eq : xt1 = xt0 := hxt1_ne hne_c_t + have hrv1_none : xt1.tcbExt.tcbRunVcpu = none := by rw [hxt_eq]; exact hrv0_none + have hne_ov_c : v0.vcpuExt.runCaller ≠ some ov := fun e => by + obtain ⟨-, xc, hkc, -⟩ := hrun_v2c vc v0 ov hkv0 e; rw [hkov0] at hkc; cases hkc + have hkov1 : σ1.kheap ov = some (.archObj (.vcpu vov0)) := by + rw [hkh1_other ov hne_ov_vc hne_ov_c]; exact hkov0 + rcases fl4VcpuEndRun_updKScA (p := vc) (o := .archObj (.vcpu v1)) hcur1 hkov1 hne_ov_vc.symm + (inUserFrame_updKSc_iffA hkv1 (fun _ h => by cases h) (fun _ h => by cases h)) + (fun c hc => by + obtain ⟨hc_in, xc, hkc, hcrv⟩ := hrun_v2c ov vov0 c hkov0 hc + have hne_vc_c : vc ≠ c := fun e => by rw [e, hkc] at hkv0; cases hkv0 + have hne_c0 : v0.vcpuExt.runCaller ≠ some c := fun e => by + obtain ⟨-, xc', hkc', hcrv'⟩ := hrun_v2c vc v0 c hkv0 e + rw [hkc] at hkc'; cases hkc'; rw [hcrv] at hcrv'; exact hne_ov_vc (Option.some.inj hcrv') + exact ⟨hne_vc_c, xc, by rw [hkh1_other c hne_vc_c.symm hne_c0]; exact hkc, hcrv, + fun sc hsc => by obtain ⟨y, n, hks⟩ := hsc_ty c xc sc hc_in hkc hsc; intro e; subst e; rw [hkv0] at hks; cases hks⟩) + (fun u hu => by + rw [hov_tcb] at hu; cases hu + exact ⟨hne_vc_t, xt1, hkt1⟩) with + ⟨σ2, hend2, -, harch2, hkov2, hkh2, -⟩ | ⟨hend_fail, -⟩ + swap + · exfalso; unfold fl4VcpuUnbindExt at hrust; rw [nd_bind_assocA, nd_bind_failA hend_fail] at hrust; have hne_f := (Prod.mk.inj hrust).2; simp at hne_f + have hne_ov_t : ov ≠ t := fun e => by rw [e, hkt0] at hkov0; cases hkov0 + have hne_rc_t : vov0.vcpuExt.runCaller ≠ some t := fun e => by + obtain ⟨-, xc, hkc, hcrv⟩ := hrun_v2c ov vov0 t hkov0 e; rw [hkt0] at hkc; cases hkc; rw [hrv0_none] at hcrv; cases hcrv + have hne_rc_vc : vov0.vcpuExt.runCaller ≠ some vc := fun e => by + obtain ⟨-, xc, hkc, -⟩ := hrun_v2c ov vov0 vc hkov0 e; rw [hkv0] at hkc; cases hkc + have hkt2 : σ2.kheap t = some (.tcb xt1) := by rw [hkh2 t hne_ov_t.symm hne_rc_t]; exact hkt1 + have hkv2 : σ2.kheap vc = some (.archObj (.vcpu v1)) := by rw [hkh2 vc hne_ov_vc.symm hne_rc_vc]; exact hkv1 + have hcur2 : σ2.archState.armCurrentVcpu = none := by rw [harch2]; exact hcur1 + let vov2 : Vcpu := { vov0 with vcpuExt := { vov0.vcpuExt with runCaller := none } } + have hdis_ov := fl4DissociateVcpuTcb_eqA hcur2 hkov2 hkt2 hov_tcb hxt1_v + let σ3 := updKSc (updKSc σ2 t (.tcb (dissociateTcbA xt1))) ov + (.archObj (.vcpu { vov2 with vcpuTcb := none, vcpuExt := { vov2.vcpuExt with ownerCpu := 0 } })) + have hub_ov : fl4VcpuUnbindExt ov σ1 = ({((), σ3)}, False) := by + unfold fl4VcpuUnbindExt + rw [nd_bind_oneSc hend2, nd_bind_oneSc (getVcpu_eqA hkov2)]; dsimp only + rw [hov_tcb]; exact hdis_ov + have hkt3 : σ3.kheap t = some (.tcb (dissociateTcbA xt1)) := by + rw [updKSc_other _ hne_ov_t.symm, updKSc_same] + have hkv3 : σ3.kheap vc = some (.archObj (.vcpu v1)) := by + rw [updKSc_other _ hne_ov_vc.symm, updKSc_other _ hne_vc_t]; exact hkv2 + rw [nd_bind_oneSc hub_ov, nd_bind_oneSc (getRunVcpu_eqA hkt3)] at hrust + change (NonDet.when (xt1.tcbExt.tcbRunVcpu ≠ none) _ >>= _) σ3 = _ at hrust + rw [hrv1_none, nd_when_falseA (by simp), nd_bind_oneSc (m := pure ()) rfl] at hrust + rw [nd_bind_assocA, nd_bind_oneSc hend2, nd_bind_assocA, nd_bind_oneSc (getRunVcpu_eqA hkt2), hrv1_none, + nd_when_falseA (by simp), nd_bind_oneSc (m := pure ()) rfl] + unfold fl4AssociateVcpuTcb + rw [nd_bind_assocA, nd_bind_oneSc (archThreadGet_eqA _ hkt2), hxt1_v]; dsimp only + rw [nd_bind_assocA, nd_bind_oneSc hdis_ov, nd_bind_assocA, nd_bind_oneSc (getVcpu_eqA hkv3), hvt1_none] + dsimp only; rw [nd_bind_assocA] + exact hrust + | some p0 => + obtain ⟨hp0_in, xp0, hkp0, hp0v⟩ := hbind_v2t vc v0 p0 hkv0 hv0_tcb + have hp0_rv0 : xp0.tcbExt.tcbRunVcpu = none := hdisj p0 xp0 hp0_in hkp0 (by rw [hp0v]; simp) + have hne_p0_vc : p0 ≠ vc := fun e => by rw [e, hkv0] at hkp0; cases hkp0 + have hne_rc0_p0 : v0.vcpuExt.runCaller ≠ some p0 := fun e => by + obtain ⟨-, xc, hkc, hcrv⟩ := hrun_v2c vc v0 p0 hkv0 e; rw [hkp0] at hkc; cases hkc; rw [hp0_rv0] at hcrv; cases hcrv + have hkp0_1 : σ1.kheap p0 = some (.tcb xp0) := by rw [hkh1_other p0 hne_p0_vc hne_rc0_p0]; exact hkp0 + have hvt1_some : v1.vcpuTcb = some p0 := by rw [hvt1, hv0_tcb] + have hdis1 := fl4DissociateVcpuTcb_eqA hcur1 hkv1 hkp0_1 hvt1_some hp0v + let o_p0 : KernelObject := .tcb (dissociateTcbA xp0) + let o_vc : KernelObject := .archObj (.vcpu { v1 with vcpuTcb := none, vcpuExt := { v1.vcpuExt with ownerCpu := 0 } }) + let σ1d := updKSc (updKSc σ1 p0 o_p0) vc o_vc + have hub1 : fl4VcpuUnbindExt vc σ = ({((), σ1d)}, False) := by + unfold fl4VcpuUnbindExt + rw [nd_bind_oneSc hend1_eq, nd_bind_oneSc (getVcpu_eqA hkv1), hvt1_some]; exact hdis1 + unfold fl4VcpuSetTcbRustOrder at hrust + rw [nd_bind_oneSc hub1] at hrust + unfold fl4PerformVcpuInv fl4VcpuSetTcbPre + dsimp only + rw [nd_bind_assocA, nd_bind_oneSc hend1_eq, nd_bind_assocA, nd_bind_oneSc (archThreadGet_eqA _ hkt1)] + by_cases ep0t : p0 = t + · -- Self-rebind (`p0 = t`): both sides dissociate `(vc, t)` then associate `(vc, t)` + subst ep0t + rw [hkt0] at hkp0; cases hkp0 + have hxt_eq : xt1 = xt0 := hxt1_ne hne_rc0_p0 + have hxt1_v : xt1.tcbArch.tcbVcpu = some vc := by rw [hxt_eq]; exact hp0v + have hxt1_rv : xt1.tcbExt.tcbRunVcpu = none := by rw [hxt_eq]; exact hp0_rv0 + have hkt1d : σ1d.kheap p0 = some o_p0 := by rw [updKSc_other _ hne_p0_vc, updKSc_same] + have hkv1d : σ1d.kheap vc = some o_vc := updKSc_same _ _ _ + rw [nd_bind_oneSc (archThreadGet_eqA _ hkt1d)] at hrust + have hd_v : (dissociateTcbA xt0).tcbArch.tcbVcpu = none := rfl + rw [hd_v] at hrust; dsimp only at hrust + rw [nd_bind_oneSc (getRunVcpu_eqA hkt1d)] at hrust + change (NonDet.when (xt0.tcbExt.tcbRunVcpu ≠ none) _ >>= _) σ1d = _ at hrust + rw [hp0_rv0, nd_when_falseA (by simp), nd_bind_oneSc (m := pure ()) rfl] at hrust + rw [hxt1_v]; dsimp only + rw [nd_bind_assocA, nd_bind_oneSc (fl4VcpuEndRun_noneA hkv1 hrc1), nd_bind_assocA, + nd_bind_oneSc (getRunVcpu_eqA hkt1), hxt1_rv, + nd_when_falseA (by simp), nd_bind_oneSc (m := pure ()) rfl] + unfold fl4AssociateVcpuTcb + rw [nd_bind_assocA, nd_bind_oneSc (archThreadGet_eqA _ hkt1), hxt1_v]; dsimp only + rw [nd_bind_assocA, nd_bind_oneSc hdis1, nd_bind_assocA, nd_bind_oneSc (getVcpu_eqA hkv1d)] + dsimp only; rw [nd_bind_assocA] + exact hrust + · -- Disjoint `p0 ≠ t`: commute `o_p0` and `o_vc` past `oldV` / `rv` steps! + have hkt1d : σ1d.kheap t = some (.tcb xt1) := by + rw [updKSc_other _ hne_vc_t.symm, updKSc_other _ (Ne.symm ep0t)]; exact hkt1 + rw [nd_bind_oneSc (archThreadGet_eqA _ hkt1d)] at hrust + cases hxt1_v : xt1.tcbArch.tcbVcpu with + | none => + rw [hxt1_v] at hrust; dsimp only at hrust ⊢ + rw [nd_bind_assocA] + rw [nd_bind_oneSc (getRunVcpu_eqA hkt1d)] at hrust + rw [nd_bind_oneSc (getRunVcpu_eqA hkt1)] + cases hrv1 : xt1.tcbExt.tcbRunVcpu with + | none => + rw [hrv1] at hrust + rw [nd_when_falseA (by simp), nd_bind_oneSc (m := pure ()) rfl] at hrust ⊢ + unfold fl4AssociateVcpuTcb + rw [nd_bind_assocA, nd_bind_oneSc (archThreadGet_eqA _ hkt1), hxt1_v]; dsimp only + rw [nd_bind_assocA, nd_bind_oneSc (getVcpu_eqA hkv1), hvt1_some]; dsimp only + rw [nd_bind_assocA, nd_bind_oneSc hdis1, nd_bind_assocA] + exact hrust + | some rv => + rw [hrv1] at hrust + rw [nd_when_trueA (by simp)] at hrust ⊢ + rw [nd_bind_assocA] at hrust ⊢ + unfold getThreadState at hrust ⊢ + rw [nd_bind_oneSc (threadGet_eqSc _ hkt1d)] at hrust + rw [nd_bind_oneSc (threadGet_eqSc _ hkt1)] + have hne_c_t : v0.vcpuExt.runCaller ≠ some t := fun h => by have := hrv1_eq h; rw [hrv1] at this; cases this + have hxt_eq : xt1 = xt0 := hxt1_ne hne_c_t + have hrv0 : xt0.tcbExt.tcbRunVcpu = some rv := by rw [← hxt_eq]; exact hrv1 + obtain ⟨vrv0, hkvrv0, hrcrv0⟩ := hrun_c2v t xt0 rv ht_in hkt0 hrv0 + have hne_rv_vc : rv ≠ vc := fun e => by subst e; rw [hkv0] at hkvrv0; cases hkvrv0; exact hne_c_t hrcrv0 + have hne_rv_p0 : rv ≠ p0 := fun e => by rw [e, hkp0] at hkvrv0; cases hkvrv0 + have hne_rv_c : v0.vcpuExt.runCaller ≠ some rv := fun e => by + obtain ⟨-, xc, hkc, -⟩ := hrun_v2c vc v0 rv hkv0 e; rw [hkvrv0] at hkc; cases hkc + have hkvrv1 : σ1.kheap rv = some (.archObj (.vcpu vrv0)) := by + rw [hkh1_other rv hne_rv_vc hne_rv_c]; exact hkvrv0 + by_cases hvm : xt1.tcbState = .ext .runningVM + · rw [if_pos hvm] at hrust ⊢ + -- Commute `o_p0` then `o_vc` across `fl4CompleteRun t` + have htcb_p0 : ∀ u, vrv0.vcpuTcb = some u → p0 ≠ u ∧ ∃ xu, σ1.kheap u = some (.tcb xu) := fun u hu => by + obtain ⟨hu_in, xu, hku, htvu⟩ := hbind_v2t rv vrv0 u hkvrv0 hu + have hne_p0_u : p0 ≠ u := fun e => by + subst e; rw [hkp0] at hku; cases hku; rw [hp0v] at htvu; exact hne_rv_vc (Option.some.inj htvu).symm + have hne_u_vc : u ≠ vc := fun e => by rw [e, hkv0] at hku; cases hku + have hne_u_c : v0.vcpuExt.runCaller ≠ some u := fun e => by + obtain ⟨-, xc, hkc, hcrv'⟩ := hrun_v2c vc v0 u hkv0 e + rw [hku] at hkc; cases hkc + have hnr := hdisj u xu hu_in hku (by rw [htvu]; simp) + rw [hcrv'] at hnr; cases hnr + exact ⟨hne_p0_u, xu, by rw [hkh1_other u hne_u_vc hne_u_c]; exact hku⟩ + have hkt1_p0 : (updKSc σ1 p0 o_p0).kheap t = some (.tcb xt1) := by rw [updKSc_other _ (Ne.symm ep0t)]; exact hkt1 + have hkvrv1_p0 : (updKSc σ1 p0 o_p0).kheap rv = some (.archObj (.vcpu vrv0)) := by rw [updKSc_other _ hne_rv_p0]; exact hkvrv1 + have hkv1_p0 : (updKSc σ1 p0 o_p0).kheap vc = some (.archObj (.vcpu v1)) := by rw [updKSc_other _ hne_p0_vc.symm]; exact hkv1 + have htcb_vc : ∀ u, vrv0.vcpuTcb = some u → vc ≠ u ∧ ∃ xu, (updKSc σ1 p0 o_p0).kheap u = some (.tcb xu) := fun u hu => by + obtain ⟨hne_p0_u, xu, hku1⟩ := htcb_p0 u hu + have hne_vc_u : vc ≠ u := fun e => by subst e; rw [hkv1] at hku1; cases hku1 + exact ⟨hne_vc_u, xu, by rw [updKSc_other _ hne_p0_u.symm]; exact hku1⟩ + rcases fl4CompleteRun_updKScA (p := p0) (o := o_p0) hcur1 hkt1 hrv1 hkvrv1 ep0t hne_rv_p0.symm + (inUserFrame_updKSc_iffA hkp0_1 (fun _ h => by cases h) (fun _ h => by dsimp only [o_p0] at h; cases h)) + (fun sc hsc => by + rw [hxt_eq] at hsc; obtain ⟨y, n, hks⟩ := hsc_ty t xt0 sc ht_in hkt0 hsc + intro e; subst e; rw [hkp0] at hks; cases hks) + htcb_p0 with + ⟨σ2, hcr2, hcr2_p0, harch2, -, ⟨xt2, hkt2, htv2, -⟩, hkh2⟩ | ⟨-, hcr2_p0_fail⟩ + swap + · exfalso + rcases fl4CompleteRun_updKScA (σ := updKSc σ1 p0 o_p0) (p := vc) (o := o_vc) hcur1 hkt1_p0 hrv1 hkvrv1_p0 hne_vc_t hne_rv_vc.symm + (inUserFrame_updKSc_iffA hkv1_p0 (fun _ h => by cases h) (fun _ h => by dsimp only [o_vc] at h; cases h)) + (fun sc hsc => by + rw [hxt_eq] at hsc; obtain ⟨y, n, hks⟩ := hsc_ty t xt0 sc ht_in hkt0 hsc + intro e; subst e; rw [hkv0] at hks; cases hks) + htcb_vc with + ⟨_, hcr2_p0', -, -, -, -, -⟩ | ⟨-, hcr2_d_fail⟩ + · rw [hcr2_p0_fail] at hcr2_p0'; have hne_f := (Prod.mk.inj hcr2_p0').2; simp at hne_f + · rw [nd_bind_failA hcr2_d_fail] at hrust; have hne_f := (Prod.mk.inj hrust).2; simp at hne_f + rcases fl4CompleteRun_updKScA (σ := updKSc σ1 p0 o_p0) (p := vc) (o := o_vc) hcur1 hkt1_p0 hrv1 hkvrv1_p0 hne_vc_t hne_rv_vc.symm + (inUserFrame_updKSc_iffA hkv1_p0 (fun _ h => by cases h) (fun _ h => by dsimp only [o_vc] at h; cases h)) + (fun sc hsc => by + rw [hxt_eq] at hsc; obtain ⟨y, n, hks⟩ := hsc_ty t xt0 sc ht_in hkt0 hsc + intro e; subst e; rw [hkv0] at hks; cases hks) + htcb_vc with + ⟨σ2_p0, hcr2_p0', hcr2_d, -, -, -, -⟩ | ⟨-, hcr2_d_fail⟩ + swap + · exfalso; rw [nd_bind_failA hcr2_d_fail] at hrust; have hne_f := (Prod.mk.inj hrust).2; simp at hne_f + have hσ2p0_eq : σ2_p0 = updKSc σ2 p0 o_p0 := by + rw [hcr2_p0] at hcr2_p0' + exact (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (Prod.mk.inj hcr2_p0').1)).2.symm + rw [hσ2p0_eq] at hcr2_d + have hkv2 : σ2.kheap vc = some (.archObj (.vcpu v1)) := by rw [hkh2 vc hne_vc_t hne_rv_vc.symm]; exact hkv1 + have hkp0_2 : σ2.kheap p0 = some (.tcb xp0) := by rw [hkh2 p0 ep0t hne_rv_p0.symm]; exact hkp0_1 + have hcur2 : σ2.archState.armCurrentVcpu = none := by rw [harch2]; exact hcur1 + rw [hxt1_v] at htv2 + rw [nd_bind_oneSc hcr2_d] at hrust + unfold fl4AssociateVcpuTcb + rw [nd_bind_oneSc hcr2, nd_bind_assocA, nd_bind_oneSc (archThreadGet_eqA _ hkt2), htv2]; dsimp only + rw [nd_bind_assocA, nd_bind_oneSc (getVcpu_eqA hkv2), hvt1_some]; dsimp only + rw [nd_bind_assocA, nd_bind_oneSc (fl4DissociateVcpuTcb_eqA hcur2 hkv2 hkp0_2 hvt1_some hp0v), nd_bind_assocA] + exact hrust + · rw [if_neg hvm] at hrust ⊢ + have hkvrv1d : σ1d.kheap rv = some (.archObj (.vcpu vrv0)) := by + rw [updKSc_other _ hne_rv_vc, updKSc_other _ hne_rv_p0]; exact hkvrv1 + have hcan := fl4CancelRun_someA hkt1 hkvrv1 hrv1 + have hcan_d := fl4CancelRun_someA hkt1d hkvrv1d hrv1 + let o_rv : KernelObject := .archObj (.vcpu { vrv0 with vcpuExt := { vrv0.vcpuExt with runCaller := none } }) + let o_t : KernelObject := .tcb { xt1 with tcbExt := { xt1.tcbExt with tcbRunVcpu := none } } + let σ2 := updKSc (updKSc σ1 rv o_rv) t o_t + have hcomm : updKSc (updKSc σ1d rv o_rv) t o_t = updKSc (updKSc σ2 p0 o_p0) vc o_vc := by + dsimp only [σ1d, σ2] + rw [updKSc_commA (updKSc σ1 p0 o_p0) hne_rv_vc.symm, updKSc_commA σ1 hne_rv_p0.symm] + rw [updKSc_commA _ hne_vc_t, updKSc_commA _ ep0t] + have hkt2 : σ2.kheap t = some o_t := updKSc_same _ _ _ + have hkv2 : σ2.kheap vc = some (.archObj (.vcpu v1)) := by + rw [updKSc_other _ hne_vc_t, updKSc_other _ hne_rv_vc.symm]; exact hkv1 + have hkp0_2 : σ2.kheap p0 = some (.tcb xp0) := by + rw [updKSc_other _ ep0t, updKSc_other _ hne_rv_p0.symm]; exact hkp0_1 + rw [nd_bind_oneSc hcan_d, hcomm] at hrust + unfold fl4AssociateVcpuTcb + rw [nd_bind_oneSc hcan, nd_bind_assocA, nd_bind_oneSc (archThreadGet_eqA _ hkt2)] + dsimp only; rw [hxt1_v]; dsimp only + rw [nd_bind_assocA, nd_bind_oneSc (getVcpu_eqA hkv2), hvt1_some]; dsimp only + rw [nd_bind_assocA, nd_bind_oneSc (fl4DissociateVcpuTcb_eqA (σ := σ2) hcur1 hkv2 hkp0_2 hvt1_some hp0v), nd_bind_assocA] + exact hrust + | some ov => + rw [hxt1_v] at hrust; dsimp only at hrust ⊢ + have htv0 : xt0.tcbArch.tcbVcpu = some ov := by rw [← htv1, hxt1_v] + obtain ⟨vov0, hkov0, hov_tcb⟩ := hbind_t2v t xt0 ov ht_in hkt0 htv0 + have hne_ov_vc : ov ≠ vc := fun e => by + subst e; rw [hkv0] at hkov0; cases hkov0; rw [hv0_tcb] at hov_tcb; exact ep0t (Option.some.inj hov_tcb) + have hne_ov_p0 : ov ≠ p0 := fun e => by rw [e, hkp0] at hkov0; cases hkov0 + have hne_ov_t : ov ≠ t := fun e => by rw [e, hkt0] at hkov0; cases hkov0 + have hrv0_none : xt0.tcbExt.tcbRunVcpu = none := hdisj t xt0 ht_in hkt0 (by rw [htv0]; simp) + have hne_c_t : v0.vcpuExt.runCaller ≠ some t := fun e => by + obtain ⟨-, xc, hkc, hcrv⟩ := hrun_v2c vc v0 t hkv0 e; rw [hkt0] at hkc; cases hkc; rw [hrv0_none] at hcrv; cases hcrv + have hxt_eq : xt1 = xt0 := hxt1_ne hne_c_t + have hrv1_none : xt1.tcbExt.tcbRunVcpu = none := by rw [hxt_eq]; exact hrv0_none + have hne_ov_c : v0.vcpuExt.runCaller ≠ some ov := fun e => by + obtain ⟨-, xc, hkc, -⟩ := hrun_v2c vc v0 ov hkv0 e; rw [hkov0] at hkc; cases hkc + have hkov1 : σ1.kheap ov = some (.archObj (.vcpu vov0)) := by + rw [hkh1_other ov hne_ov_vc hne_ov_c]; exact hkov0 + have hrc_p0 : ∀ c, vov0.vcpuExt.runCaller = some c → p0 ≠ c ∧ ∃ xc, σ1.kheap c = some (.tcb xc) ∧ xc.tcbExt.tcbRunVcpu = some ov ∧ + (∀ sc, xc.tcbSchedContext = some sc → p0 ≠ sc) := + fun c hc => by + obtain ⟨hc_in, xc, hkc, hcrv⟩ := hrun_v2c ov vov0 c hkov0 hc + have hne_p0_c : p0 ≠ c := fun e => by + subst e; rw [hkp0] at hkc; cases hkc; rw [hp0_rv0] at hcrv; cases hcrv + have hne_c_vc : c ≠ vc := fun e => by rw [e, hkv0] at hkc; cases hkc + have hne_c0 : v0.vcpuExt.runCaller ≠ some c := fun e => by + obtain ⟨-, xc', hkc', hcrv'⟩ := hrun_v2c vc v0 c hkv0 e + rw [hkc] at hkc'; cases hkc'; rw [hcrv] at hcrv'; exact hne_ov_vc (Option.some.inj hcrv') + exact ⟨hne_p0_c, xc, by rw [hkh1_other c hne_c_vc hne_c0]; exact hkc, hcrv, + fun sc hsc => by obtain ⟨y, n, hks⟩ := hsc_ty c xc sc hc_in hkc hsc; intro e; subst e; rw [hkp0] at hks; cases hks⟩ + have hkov1_p0 : (updKSc σ1 p0 o_p0).kheap ov = some (.archObj (.vcpu vov0)) := by + rw [updKSc_other _ hne_ov_p0]; exact hkov1 + have hkv1_p0 : (updKSc σ1 p0 o_p0).kheap vc = some (.archObj (.vcpu v1)) := by + rw [updKSc_other _ hne_p0_vc.symm]; exact hkv1 + have hrc_vc : ∀ c, vov0.vcpuExt.runCaller = some c → vc ≠ c ∧ ∃ xc, (updKSc σ1 p0 o_p0).kheap c = some (.tcb xc) ∧ xc.tcbExt.tcbRunVcpu = some ov ∧ + (∀ sc, xc.tcbSchedContext = some sc → vc ≠ sc) := + fun c hc => by + obtain ⟨hne_p0_c, xc, hkc1, hcrv, -⟩ := hrc_p0 c hc + have hne_vc_c : vc ≠ c := fun e => by subst e; rw [hkv1] at hkc1; cases hkc1 + obtain ⟨hc_in, xc0, hkc0, hcrv0⟩ := hrun_v2c ov vov0 c hkov0 hc + have hne_c0 : v0.vcpuExt.runCaller ≠ some c := fun e => by + obtain ⟨-, xc', hkc', hcrv'⟩ := hrun_v2c vc v0 c hkv0 e + rw [hkc0] at hkc'; cases hkc' + rw [hcrv0] at hcrv'; exact hne_ov_vc (Option.some.inj hcrv') + have hxc_eq : xc = xc0 := by + rw [hkh1_other c hne_vc_c.symm hne_c0, hkc0] at hkc1 + exact KernelObject.tcb.inj (Option.some.inj hkc1.symm) + exact ⟨hne_vc_c, xc, by rw [updKSc_other _ hne_p0_c.symm]; exact hkc1, hcrv, + fun sc hsc => by rw [hxc_eq] at hsc; obtain ⟨y, n, hks⟩ := hsc_ty c xc0 sc hc_in hkc0 hsc; intro e; subst e; rw [hkv0] at hks; cases hks⟩ + rcases fl4VcpuEndRun_updKScA (p := p0) (o := o_p0) hcur1 hkov1 hne_ov_p0.symm + (inUserFrame_updKSc_iffA hkp0_1 (fun _ h => by cases h) (fun _ h => by dsimp only [o_p0] at h; cases h)) hrc_p0 + (fun u hu => by rw [hov_tcb] at hu; cases hu; exact ⟨ep0t, xt1, hkt1⟩) with + ⟨σ2, hend2, hend2_p0, harch2, hkov2, hkh2, -⟩ | ⟨-, hend2_p0_fail⟩ + swap + · exfalso + rcases fl4VcpuEndRun_updKScA (σ := updKSc σ1 p0 o_p0) (p := vc) (o := o_vc) hcur1 hkov1_p0 hne_ov_vc.symm + (inUserFrame_updKSc_iffA hkv1_p0 (fun _ h => by cases h) (fun _ h => by dsimp only [o_vc] at h; cases h)) hrc_vc + (fun u hu => by rw [hov_tcb] at hu; cases hu; exact ⟨hne_vc_t, xt1, by rw [updKSc_other _ (Ne.symm ep0t)]; exact hkt1⟩) with + ⟨_, hend2_p0', -, -, -, -, -⟩ | ⟨-, hend2_d_fail⟩ + · rw [hend2_p0_fail] at hend2_p0'; have hne_f := (Prod.mk.inj hend2_p0').2; simp at hne_f + · unfold fl4VcpuUnbindExt at hrust; rw [nd_bind_assocA, nd_bind_failA hend2_d_fail] at hrust; have hne_f := (Prod.mk.inj hrust).2; simp at hne_f + rcases fl4VcpuEndRun_updKScA (σ := updKSc σ1 p0 o_p0) (p := vc) (o := o_vc) hcur1 hkov1_p0 hne_ov_vc.symm + (inUserFrame_updKSc_iffA hkv1_p0 (fun _ h => by cases h) (fun _ h => by dsimp only [o_vc] at h; cases h)) hrc_vc + (fun u hu => by rw [hov_tcb] at hu; cases hu; exact ⟨hne_vc_t, xt1, by rw [updKSc_other _ (Ne.symm ep0t)]; exact hkt1⟩) with + ⟨σ2_p0, hend2_p0', hend2_d, -, -, -, -⟩ | ⟨-, hend2_d_fail⟩ + swap + · exfalso; unfold fl4VcpuUnbindExt at hrust; rw [nd_bind_assocA, nd_bind_failA hend2_d_fail] at hrust; have hne_f := (Prod.mk.inj hrust).2; simp at hne_f + have hσ2p0_eq : σ2_p0 = updKSc σ2 p0 o_p0 := by + rw [hend2_p0] at hend2_p0' + exact (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (Prod.mk.inj hend2_p0').1)).2.symm + rw [hσ2p0_eq] at hend2_d + have hne_rc_t : vov0.vcpuExt.runCaller ≠ some t := fun e => by + obtain ⟨-, xc, hkc, hcrv⟩ := hrun_v2c ov vov0 t hkov0 e; rw [hkt0] at hkc; cases hkc; rw [hrv0_none] at hcrv; cases hcrv + have hne_rc_vc : vov0.vcpuExt.runCaller ≠ some vc := fun e => by + obtain ⟨-, xc, hkc, -⟩ := hrun_v2c ov vov0 vc hkov0 e; rw [hkv0] at hkc; cases hkc + have hne_rc_p0 : vov0.vcpuExt.runCaller ≠ some p0 := fun e => by + obtain ⟨hne_p0_c, -⟩ := hrc_p0 p0 e; exact hne_p0_c rfl + have hkt2 : σ2.kheap t = some (.tcb xt1) := by rw [hkh2 t hne_ov_t.symm hne_rc_t]; exact hkt1 + have hkv2 : σ2.kheap vc = some (.archObj (.vcpu v1)) := by rw [hkh2 vc hne_ov_vc.symm hne_rc_vc]; exact hkv1 + have hkp0_2 : σ2.kheap p0 = some (.tcb xp0) := by rw [hkh2 p0 hne_ov_p0.symm hne_rc_p0]; exact hkp0_1 + have hcur2 : σ2.archState.armCurrentVcpu = none := by rw [harch2]; exact hcur1 + let vov2 : Vcpu := { vov0 with vcpuExt := { vov0.vcpuExt with runCaller := none } } + let o_t : KernelObject := .tcb (dissociateTcbA xt1) + let o_ov : KernelObject := .archObj (.vcpu { vov2 with vcpuTcb := none, vcpuExt := { vov2.vcpuExt with ownerCpu := 0 } }) + let σ3 := updKSc (updKSc σ2 t o_t) ov o_ov + have hkov2d : (updKSc (updKSc σ2 p0 o_p0) vc o_vc).kheap ov = some (.archObj (.vcpu vov2)) := by + rw [updKSc_other _ hne_ov_vc, updKSc_other _ hne_ov_p0]; exact hkov2 + have hkt2d : (updKSc (updKSc σ2 p0 o_p0) vc o_vc).kheap t = some (.tcb xt1) := by + rw [updKSc_other _ hne_vc_t.symm, updKSc_other _ (Ne.symm ep0t)]; exact hkt2 + have hdis_ov := fl4DissociateVcpuTcb_eqA hcur2 hkov2 hkt2 hov_tcb hxt1_v + have hdis_ov_d := fl4DissociateVcpuTcb_eqA (σ := updKSc (updKSc σ2 p0 o_p0) vc o_vc) hcur2 hkov2d hkt2d hov_tcb hxt1_v + have hcomm3 : updKSc (updKSc (updKSc (updKSc σ2 p0 o_p0) vc o_vc) t o_t) ov o_ov = + updKSc (updKSc σ3 p0 o_p0) vc o_vc := by + dsimp only [σ3] + rw [updKSc_commA (updKSc σ2 p0 o_p0) hne_vc_t, updKSc_commA σ2 ep0t] + rw [updKSc_commA _ hne_ov_vc.symm, updKSc_commA _ hne_ov_p0.symm] + have hub_ov_d : fl4VcpuUnbindExt ov σ1d = ({((), updKSc (updKSc σ3 p0 o_p0) vc o_vc)}, False) := by + unfold fl4VcpuUnbindExt + rw [nd_bind_oneSc hend2_d, nd_bind_oneSc (getVcpu_eqA hkov2d)] + have hov2 : vov2.vcpuTcb = some t := hov_tcb + change (match vov2.vcpuTcb with | some t => fl4DissociateVcpuTcb ov t | none => pure ()) _ = _ + rw [hov2] + exact hdis_ov_d.trans (by rw [hcomm3]) + have hkt3d : (updKSc (updKSc σ3 p0 o_p0) vc o_vc).kheap t = some o_t := by + rw [updKSc_other _ hne_vc_t.symm, updKSc_other _ (Ne.symm ep0t), updKSc_other _ hne_ov_t.symm, updKSc_same] + have hkv3 : σ3.kheap vc = some (.archObj (.vcpu v1)) := by + rw [updKSc_other _ hne_ov_vc.symm, updKSc_other _ hne_vc_t]; exact hkv2 + have hkp0_3 : σ3.kheap p0 = some (.tcb xp0) := by + rw [updKSc_other _ hne_ov_p0.symm, updKSc_other _ ep0t]; exact hkp0_2 + rw [nd_bind_oneSc hub_ov_d, nd_bind_oneSc (getRunVcpu_eqA hkt3d)] at hrust + change (NonDet.when (xt1.tcbExt.tcbRunVcpu ≠ none) _ >>= _) _ = _ at hrust + rw [hrv1_none, nd_when_falseA (by simp), nd_bind_oneSc (m := pure ()) rfl] at hrust + rw [nd_bind_assocA, nd_bind_oneSc hend2, nd_bind_assocA, nd_bind_oneSc (getRunVcpu_eqA hkt2), hrv1_none, + nd_when_falseA (by simp), nd_bind_oneSc (m := pure ()) rfl] + unfold fl4AssociateVcpuTcb + rw [nd_bind_assocA, nd_bind_oneSc (archThreadGet_eqA _ hkt2), hxt1_v]; dsimp only + rw [nd_bind_assocA, nd_bind_oneSc hdis_ov, nd_bind_assocA, nd_bind_oneSc (getVcpu_eqA hkv3), hvt1_some]; dsimp only + rw [nd_bind_assocA, nd_bind_oneSc (fl4DissociateVcpuTcb_eqA (σ := σ3) hcur2 hkv3 hkp0_3 hvt1_some hp0v), nd_bind_assocA] + exact hrust + +/-! ## Section 2: `vcpu_set_tcb_jA` (label 60 `ARMVCPUSetTCB` on `VcpuTopPostA`) -/ + +/-- Spec decoder for label 60 (`ARMVCPUSetTCB`) matching `fl4ArchDecodeInvocation`. -/ +def decodeFl4VcpuSetTcb (v : ObjRef) (extras : List (Cap × CslotPtr)) : + SEMonad Unit Fl4ArchInvocation := do + let i ← decodeVcpuSetTcb (.vcpuCap v) extras + match i with + | .invokeVCPU (.vcpuSetTCB v' t) => NonDetE.returnOk (.vcpu (.setTcb v' t)) + | i => NonDetE.returnOk (.spec i) + +section set_tcb_j +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + +private theorem lift_vcpu_setTcb_errA {v : ObjRef} {extras : List (Cap × CslotPtr)} + {σ : State Unit} {e : SyscallError} + (h : (decodeVcpuSetTcb (Z := Unit) (.vcpuCap v) extras).run σ = ({(.error e, σ)}, False)) : + (decodeFl4VcpuSetTcb v extras).run σ = ({(.error e, σ)}, False) := by + unfold decodeFl4VcpuSetTcb ExceptT.run + change ((decodeVcpuSetTcb (Z := Unit) (.vcpuCap v) extras).run >>= _) σ = _ + rw [nd_bind_oneSc h]; rfl + +private theorem lift_vcpu_setTcb_okA {v t : ObjRef} {extras : List (Cap × CslotPtr)} + {σ : State Unit} + (h : (decodeVcpuSetTcb (Z := Unit) (.vcpuCap v) extras).run σ = ({(.ok (.invokeVCPU (.vcpuSetTCB v t)), σ)}, False)) : + (.ok (.vcpu (.setTcb v t)), σ) ∈ ((decodeFl4VcpuSetTcb v extras).run σ).1 ∧ + ¬ ((decodeFl4VcpuSetTcb v extras).run σ).2 := by + have heq : (decodeFl4VcpuSetTcb v extras).run σ = ({(.ok (.vcpu (.setTcb v t)), σ)}, False) := by + unfold decodeFl4VcpuSetTcb ExceptT.run + change ((decodeVcpuSetTcb (Z := Unit) (.vcpuCap v) extras).run >>= _) σ = _ + rw [nd_bind_oneSc h]; rfl + rw [heq]; exact ⟨Set.mem_singleton _, not_false⟩ + +/-- Helper: `VcpuXferStateA` and `VcpuRunPresA` preserved across `TS_RESTART` on `L.curW s`. -/ +private theorem vcpuXferStateA_of_restart_setTcbA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} + {OS U Ac Pp Tb Ub : Set U64} {Lv : U64 → Nat} {ia ie : Nat} + {s : S} {a : A} {σ : State Unit} {m : Fl4SpecA64.McState} {vc : U64} + (C : VcpuDecCtxA (L := L) (ar := ar) AR Z H OS U Ac Pp Tb Ub Lv ia ie s a σ m vc) + (hCar : CarryA Z (L.mem s) σ) + (hF : FrameMemRelA Z OS Ub (L.ksb s) (L.mem s) σ.machineState.underlyingMemory) + (hR : TcbRegsRelA (L.mem s) σ Z.T) + (hU : UbFootDisjA Z OS Ub) + (hSc_st : ∀ t ∈ Z.T, ∀ c ∈ Z.C, stA t ≠ wa c 24#u64 ∧ stA t ≠ wa c 72#u64) + {xct : Tcb} (hkct : σ.kheap σ.curThread = some (.tcb xct)) + (hJ1 : JAtZAX (L := L) AR Z (L.wr s (stA (L.curW s)) sched.TS_RESTART) a + (stUpdA σ σ.curThread xct .restart) m) + (hos1 : OSKeep st OS s (L.wr s (stA (L.curW s)) sched.TS_RESTART)) : + VcpuXferStateA (L := L) AR Z OS Ub m a (L.wr s (stA (L.curW s)) sched.TS_RESTART) + (stUpdA σ σ.curThread xct .restart) ∧ + VcpuRunPresA L Z OS s (L.wr s (stA (L.curW s)) sched.TS_RESTART) σ + (stUpdA σ σ.curThread xct .restart) := by + have hcurT : L.curW s ∈ Z.T := C.E.j.1.curT (L.cpu s) C.E.j.1.sch.cpuCs + have hta : TcbAddr (L.curW s) := hU.tcb_al (L.curW s) hcurT + have hst_al : Aligned (stA (L.curW s)) := by + rw [stA_eq]; apply aligned_wa (by unfold Aligned; have := hta.align; omega); decide + set s2 := L.wr s (stA (L.curW s)) sched.TS_RESTART + set σ1 := stUpdA σ σ.curThread xct .restart + have hTr1 : TreeA st OS Z.G.chains Z.G.par s2 := C.hTr.keepO hos1 + have hcap_os : ∀ y ∈ OS, capAtA (L.mem s2) y = capAtA (L.mem s) y := by + intro y hy; obtain ⟨h0, h8, -, -⟩ := hos1 y hy; unfold capAtA; rw [h0, h8] + have hTy : TyKeepA σ σ1 := + TyKeepA.upd hkct (fun _ _ e => by cases e) (fun _ _ => ⟨xct, rfl⟩) + have hCar1 : CarryA Z (L.mem s2) σ1 := by + refine ⟨hCar.1.of_capsA rfl (fun _ h => h) (fun _ h => h) (fun y hy => Or.inl (hcap_os y (C.hsub y hy))), + hCar.2.of_keepA hTy.1 hTy.2 fun y hy => ?_⟩ + rw [hcap_os y ((C.hTr.lk.members y).1 hy).1]; exact CapFinEqA.refl _ + have hvc_mem : ∀ v ∈ Z.V, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → + L.mem s2 (wa v o) = L.mem s (wa v o) := fun v hv o ho ho8 => + mem_wr_neA hst_al (aligned_wa (C.hV.place.vc_al hv) o ho8) + (by rw [stA_eq]; exact (C.hV.place.disj_tcb v hv (L.curW s) hcurT o 3328#u64 ho (by decide)).symm) + have hwa0 : ∀ v : U64, wa v 0#u64 = v := fun v => by + apply UScalar.eq_of_val_eq; rw [wa_valP]; simp [UScalar.hBounds] + have hvc0 : ∀ v ∈ Z.V, L.mem s2 v = L.mem s v := fun v hv => by + rw [← hwa0 v]; exact hvc_mem v hv 0#u64 (by decide) (by decide) + have htcb_other : ∀ t ∈ Z.T, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → o ≠ 3328#u64 → + L.mem s2 (wa t o) = L.mem s (wa t o) := fun t ht o ho ho8 hne => + mem_wr_neA hst_al (aligned_wa (C.hV.place.al_t t ht) o ho8) + (by rw [stA_eq]; exact field_ne hta (hU.tcb_al t ht) (by decide) ho (fun _ e => hne (UScalar.eq_of_val_eq e.symm))) + have hrun_mem : ∀ t ∈ Z.T, L.mem s2 (runVcpuA t) = L.mem s (runVcpuA t) := fun t ht => by + have hreq : runVcpuA t = wa t 3672#u64 := by unfold runVcpuA; rfl + rw [hreq]; exact htcb_other t ht 3672#u64 (by decide) (by decide) (by decide) + have hkv_eq : ∀ v ∈ Z.V, ∀ x, σ1.kheap (specPtr v) = some (.archObj (.vcpu x)) → + σ.kheap (specPtr v) = some (.archObj (.vcpu x)) := fun v hv x hkx => by + obtain ⟨v0, hkv0, -, -⟩ := C.hV.core.obj v hv + have hne : specPtr v ≠ σ.curThread := fun h => by rw [h, hkct] at hkv0; cases hkv0 + unfold σ1 stUpdA setObjA at hkx; simpa [Function.update, hne] using hkx + have hV1 : VcpuRelA (L := L) Z OS Ub s2 σ1 := by + refine ⟨hJ1.2.1, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, C.hV.arch, by rw [L.ksb_wr]; exact C.hV.place⟩ + · intro v hv; rw [hvc_mem v hv 8#u64 (by decide) (by decide)]; exact C.hV.owner v hv + · intro v hv x hkx; rw [hvc_mem v hv 16#u64 (by decide) (by decide)]; exact C.hV.vppi v hv x (hkv_eq v hv x hkx) + · intro v hv x hkx; rw [hvc_mem v hv 32#u64 (by decide) (by decide)]; exact C.hV.runOff v hv x (hkv_eq v hv x hkx) + · intro v hv x hkx; rw [hvc_mem v hv 40#u64 (by decide) (by decide)]; exact C.hV.runOut v hv x (hkv_eq v hv x hkx) + · intro v hv x hkx i hi + have hlt : (vcpuRegOffA i).val < 4096 := by rw [vcpuRegOffA_val hi]; omega + have h8 : (vcpuRegOffA i).val % 8 = 0 := by rw [vcpuRegOffA_val hi]; omega + rw [vcpuRegWA, hvc_mem v hv (vcpuRegOffA i) hlt h8]; exact C.hV.regs v hv x (hkv_eq v hv x hkx) i hi + · intro v hv t ht hne; rw [hvc0 v hv] at ht + obtain ⟨htT, htv⟩ := C.hV.bind_v2t v hv t ht hne + exact ⟨htT, by rw [htcb_other t htT 3664#u64 (by decide) (by decide) (by decide)]; exact htv⟩ + · intro t ht v hv hne; rw [htcb_other t ht 3664#u64 (by decide) (by decide) (by decide)] at hv + obtain ⟨hvV, hvt⟩ := C.hV.bind_t2v t ht v hv hne; exact ⟨hvV, by rw [hvc0 v hvV]; exact hvt⟩ + · intro v hv c hc hne; rw [hvc_mem v hv 24#u64 (by decide) (by decide)] at hc + obtain ⟨hcT, hcv⟩ := C.hV.run_v2c v hv c hc hne; exact ⟨hcT, by rw [hrun_mem c hcT]; exact hcv⟩ + · intro c hc v hv hne; rw [hrun_mem c hc] at hv + obtain ⟨hvV, hvc'⟩ := C.hV.run_c2v c hc v hv hne + exact ⟨hvV, by rw [hvc_mem v hvV 24#u64 (by decide) (by decide)]; exact hvc'⟩ + · intro c hc hne; rw [hrun_mem c hc] at hne + have hnc : c ≠ L.curW s := fun h => by subst h; exact hne C.hCurNoRun + rw [mem_wr_neA hst_al (by rw [stA_eq]; exact aligned_wa (C.hV.place.al_t c hc) 3328#u64 (by decide)) + (by rw [stA_eq, stA_eq]; exact field_ne hta (hU.tcb_al c hc) (by decide) (by decide) (fun e _ => hnc e.symm))] + exact C.hV.run_state c hc hne + · intro t ht hne; rw [htcb_other t ht 3664#u64 (by decide) (by decide) (by decide)] at hne + rw [hrun_mem t ht]; exact C.hV.bind_run_disj t ht hne + · intro c hc + have hal_idle : Aligned (L.nodeAt s c layout.node.IDLE_THREAD) := + C.hfa _ (FootA.nslot _ (FootNA.node c _ hc (Or.inr (Or.inl rfl)))) + have hne_idle : L.nodeAt s c layout.node.IDLE_THREAD ≠ stA (L.curW s) := by + rw [stA_eq]; exact node_ne_field (C.E.sq.nodes c hc) (by simp [layout.node.IDLE_THREAD]) C.E.sq.curT hta (by decide) + rw [nodeAt_wrA, mem_wr_neA hst_al hal_idle hne_idle.symm] + exact ⟨(htcb_other _ (C.hIdleT c hc) 3664#u64 (by decide) (by decide) (by decide)).trans (C.hV.idle_no_vcpu c hc).1, + (hrun_mem _ (C.hIdleT c hc)).trans (C.hV.idle_no_vcpu c hc).2⟩ + · intro v hv hor + have hor0 : L.mem s v ≠ 0#u64 ∨ L.mem s (wa v 24#u64) ≠ 0#u64 := by + rcases hor with h | h + · rw [hvc0 v hv] at h; exact Or.inl h + · rw [hvc_mem v hv 24#u64 (by decide) (by decide)] at h; exact Or.inr h + obtain ⟨y, hy, hcap⟩ := C.hV.live v hv hor0 + exact ⟨y, hy, by rw [hcap_os y hy]; exact hcap⟩ + have hF1 : FrameMemRelA Z OS Ub (L.ksb s2) (L.mem s2) σ1.machineState.underlyingMemory := by + refine ⟨hF.aligned, ?_, ?_, hF.disj_os, hF.disj_slots, hF.disj_tcb, hF.disj_sc, by rw [L.ksb_wr]; exact hF.disj_ks⟩ + · intro q hq + have hne : stA (L.curW s) ≠ q := fun e => hF.disj_tcb q hq (L.curW s) hcurT 3328#u64 (by decide) (by rw [← stA_eq]; exact e.symm) + rw [mem_wr_neA hst_al (hF.aligned q hq) hne]; exact hF.agree q hq + · intro y hy p sz r m_f d asid va hc; rw [hcap_os y hy] at hc; exact hF.frames y hy p sz r m_f d asid va hc + have hR1 : TcbRegsRelA (L.mem s2) σ1 Z.T := by + intro t ht xt hkt r + obtain ⟨hKval, hreg_eq⟩ := regA_eq_wa_KA t r; have hlt := regIdxA_lt r + have hta_t := hU.tcb_al t ht + have hal_r : Aligned (Endp.regA t (regIdxA r)) := by + rw [hreg_eq]; exact aligned_wa (by unfold Aligned; have := hta_t.align; omega) _ (by omega) + have h3328 : (3328#u64 : U64).val = 3328 := rfl + rw [mem_wr_neA hst_al hal_r (by rw [stA_eq, hreg_eq]; exact field_ne hta hta_t (by decide) (by omega) (fun _ => by omega))] + by_cases et : specPtr t = σ.curThread + · unfold σ1 stUpdA setObjA at hkt; rw [et] at hkt; simp [Function.update] at hkt; subst hkt + exact hR t ht xct (et ▸ hkct) r + · unfold σ1 stUpdA setObjA at hkt; simp [Function.update, et] at hkt; exact hR t ht xt hkt r + have hpres : VcpuRunPresA L Z OS s s2 σ σ1 := by + refine ⟨hos1, ?_, ?_, ?_, fun q hq => inUserFrame_updKScA hkct (fun _ h => by cases h) hq, + fun u hu hne => by rwa [hrun_mem u hu] at hne⟩ + · intro t ht + exact ⟨by unfold inqA; exact htcb_other t ht 3592#u64 (by decide) (by decide) (by decide), + by unfold scA; exact htcb_other t ht 3464#u64 (by decide) (by decide) (by decide)⟩ + · intro c hc + have hma_al : Aligned (maxA c) := by rw [maxA_eqA]; exact C.hfa _ (.nslot _ (.sc2 c hc)) + exact mem_wr_neA hst_al hma_al (by rw [maxA_eqA]; exact (hSc_st (L.curW s) hcurT c hc).2) + · intro c hc + have hal_idle : Aligned (L.nodeAt s c layout.node.IDLE_THREAD) := + C.hfa _ (FootA.nslot _ (FootNA.node c _ hc (Or.inr (Or.inl rfl)))) + have hne_idle : L.nodeAt s c layout.node.IDLE_THREAD ≠ stA (L.curW s) := by + rw [stA_eq]; exact node_ne_field (C.E.sq.nodes c hc) (by simp [layout.node.IDLE_THREAD]) C.E.sq.curT hta (by decide) + rw [nodeAt_wrA, mem_wr_neA hst_al hal_idle hne_idle.symm] + exact ⟨⟨hJ1, hTr1, hCar1, hV1, hF1, hR1⟩, hpres⟩ + +/-- Helper: lift `hUbUser`, `hBufNotS2`, `hRunSched` across `VcpuRunPresA`. -/ +private theorem pres_lift_setTcbA {AR : A → HwRootA → Prop} {Z : XSetsA} {OS Ub : Set U64} + {m : Fl4SpecA64.McState} {a a' : A} {s s' : S} {σ σ' : State Unit} {hw' : HwRootA} + (X : VcpuXferStateA (L := L) AR Z OS Ub m a s σ) + (X' : VcpuXferStateA (L := L) AR { Z with hw := hw' } OS Ub m a' s' σ') + (hcpu' : L.cpu s' = L.cpu s) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hpres : VcpuRunPresA L Z OS s s' σ σ') + (hUbUser : ∀ q ∈ Ub, inUserFrame (specPtr q) σ) + (hBufNotS2 : ∀ u ∈ Z.T, ∀ p sz r d mp asid va, + capAtA (L.mem s) (sbWordA u) ≠ .Frame p sz r d mp true asid va) + (hRunSched : ∀ u ∈ Z.T, L.mem s (runVcpuA u) ≠ 0#u64 → + L.mem s (scA u) ≠ 0#u64 ∧ L.rmax s (L.mem s (scA u)) ≠ 0#u64 ∧ + (L.mem s (inqA u) &&& sched.Q_RELEASE) = 0#u64 ∧ + specPtr u ∉ σ.releaseQueue ∧ + Aligned (maxA (L.mem s (scA u))) ∧ stA u ≠ maxA (L.mem s (scA u))) : + (∀ q ∈ Ub, inUserFrame (specPtr q) σ') ∧ + (∀ u ∈ Z.T, ∀ p sz r d mp asid va, + capAtA (L.mem s') (sbWordA u) ≠ .Frame p sz r d mp true asid va) ∧ + (∀ u ∈ Z.T, L.mem s' (runVcpuA u) ≠ 0#u64 → + L.mem s' (scA u) ≠ 0#u64 ∧ L.rmax s' (L.mem s' (scA u)) ≠ 0#u64 ∧ + (L.mem s' (inqA u) &&& sched.Q_RELEASE) = 0#u64 ∧ + specPtr u ∉ σ'.releaseQueue ∧ + Aligned (maxA (L.mem s' (scA u))) ∧ stA u ≠ maxA (L.mem s' (scA u))) := by + refine ⟨fun q hq => hpres.user_frame _ (hUbUser q hq), ?_, ?_⟩ + · intro u hu p sz r d mp asid va + have hth_u : Z.th u = some .tcb := (X.j.1.thT u).2 hu + have hos_u : sbWordA u ∈ OS := hsub _ ⟨u, Or.inr ⟨hth_u, 2, by decide, (bufSlot_eqA u).symm⟩⟩ + obtain ⟨h0, h8, -, -⟩ := hpres.os (sbWordA u) hos_u + have hcap : capAtA (L.mem s') (sbWordA u) = capAtA (L.mem s) (sbWordA u) := by unfold capAtA; rw [h0, h8] + rw [hcap]; exact hBufNotS2 u hu p sz r d mp asid va + · intro u hu hrv' + obtain ⟨hsc0, hrm0, hiq0, hnr0, hma0, hmx0⟩ := hRunSched u hu (hpres.run_mon u hu hrv') + obtain ⟨hiq_eq, hsc_eq⟩ := hpres.tcb_meta u hu + have hscC : L.mem s (scA u) ∈ Z.C := X.j.1.rest.scMem u hu hsc0 + refine ⟨by rw [hsc_eq]; exact hsc0, + by unfold LawfulStore.rmax at hrm0 ⊢; rw [hsc_eq, hpres.sc_max _ hscC]; exact hrm0, + by rw [hiq_eq]; exact hiq0, + by rw [X'.j.1.rest.rel, hcpu', ← X.j.1.rest.rel]; exact hnr0, + by rw [hsc_eq]; exact hma0, + by rw [hsc_eq]; exact hmx0⟩ + +set_option maxHeartbeats 800000 in +/-- **Label 60 (`ARMVCPUSetTCB`) refinement on `VcpuTopPostA`**: +`vcpu.decode_set_tcb` refines `decodeFl4VcpuSetTcb` + `fl4PerformVcpuInv (.setTcb)` for both +`t = cur_thread` and `t ≠ cur_thread`, updating `Z.hw` via `VcpuVmRootA`. -/ +theorem vcpu_set_tcb_jA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} + {OS U Ac Pp Tb Ub : Set U64} {Lv : U64 → Nat} {ia ie : Nat} + {s : S} {a : A} {σ : State Unit} {m : Fl4SpecA64.McState} {vc : U64} + (C : VcpuDecCtxA (L := L) (ar := ar) AR Z H OS U Ac Pp Tb Ub Lv ia ie s a σ m vc) + (hCar : CarryA Z (L.mem s) σ) + (hP : VcpuPlatAgreeA ar) (hAK : VcpuArchKeepsA ar AR) (hVR : VcpuVmRootA st ar AR) (hAR : AR a Z.hw) + (hF : FrameMemRelA Z OS Ub (L.ksb s) (L.mem s) σ.machineState.underlyingMemory) + (hR : TcbRegsRelA (L.mem s) σ Z.T) + (hU : UbFootDisjA Z OS Ub) (hLD : TcbLinkDisjA Z (L.ksb s)) + (hUbUser : ∀ q ∈ Ub, inUserFrame (specPtr q) σ) + (hksTcb : ∀ t ∈ Z.T, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → + ∀ o : U64, o.val < ksSizeA → wa t K ≠ wa (L.ksb s) o) + (hSlotTcb : ∀ t ∈ Z.T, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → + ∀ y, SlotWA Z.th y → wa t K ≠ y ∧ wa t K ≠ wa y 8#u64 ∧ wa t K ≠ wa y 16#u64 ∧ wa t K ≠ wa y 24#u64) + (hOS_tcb : ∀ t ∈ Z.T, ∀ K : U64, (K = 3664#u64 ∨ K = 3672#u64) → ∀ y ∈ OS, + wa t K ≠ y ∧ wa t K ≠ wa y 8#u64 ∧ wa t K ≠ wa y 16#u64 ∧ wa t K ≠ wa y 24#u64) + (hStall : ∀ t ∈ Z.T, ∀ s' a', smp.remote_tcb_stall st ar s' a' t = ok (s', a')) + (hBufNotS2 : ∀ t ∈ Z.T, ∀ p sz r d mp asid va, + capAtA (L.mem s) (sbWordA t) ≠ .Frame p sz r d mp true asid va) + (hRunSched : ∀ t ∈ Z.T, L.mem s (runVcpuA t) ≠ 0#u64 → + L.mem s (scA t) ≠ 0#u64 ∧ L.rmax s (L.mem s (scA t)) ≠ 0#u64 ∧ + (L.mem s (inqA t) &&& sched.Q_RELEASE) = 0#u64 ∧ + specPtr t ∉ σ.releaseQueue ∧ + Aligned (maxA (L.mem s (scA t))) ∧ stA t ≠ maxA (L.mem s (scA t))) + (hSlotSt : ∀ t ∈ Z.T, ∀ y, SlotWA Z.th y → stA t ≠ y ∧ stA t ≠ wa y 8#u64 ∧ stA t ≠ wa y 16#u64 ∧ stA t ≠ wa y 24#u64) + (hOS_st : ∀ t ∈ Z.T, ∀ y ∈ OS, stA t ≠ y ∧ stA t ≠ wa y 8#u64 ∧ stA t ≠ wa y 16#u64 ∧ stA t ≠ wa y 24#u64) + (hKs_st : ∀ t ∈ Z.T, ∀ o : U64, o.val < ksSizeA → stA t ≠ wa (L.ksb s) o) + (hSc_st : ∀ t ∈ Z.T, ∀ c ∈ Z.C, stA t ≠ wa c 24#u64 ∧ stA t ≠ wa c 72#u64) + (hVs_st : ∀ t ∈ Z.T, ¬ VsFootA Z.Pp Z.S1 Z.Y (L.ksb s) (stA t)) + (hExt_st : ∀ t ∈ Z.T, ¬ ExtFootE Z.X Z.Y Z.T2 (stA t)) + (hvc_cap : ∃ y ∈ OS, capAtA (L.mem s) y = .Vcpu vc) + {Rp Nt : Set U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} {slot : U64} + (Pl : SysPlace st H Z.Cs H.frame Rp Nt OS s extra slot) (Y : SymL st H Z.Cs s) + (hrun : Runnable (L.mem s (stA (L.curW s)))) + (hcur : (L.mem s (inqA (L.curW s)) &&& sched.Q_RELEASE) = 0#u64 ∧ L.mem s (scA (L.curW s)) ≠ 0#u64 ∧ + L.mem s (scA (L.curW s)) ∈ Ac) + (hvs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → Aligned b ∧ b ≠ stA (L.curW s)) + {ex : Array obj.Slot 3#usize} + {extras : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)} + (hEx : ExtraOKA Z.th (L.mem s) ex extras) + (hExSlot : extras ≠ [] → exAtA ex 0 ≠ 0#u64 ∧ exAtA ex 0 ∈ OS) + (hExTcb : ∀ ptr, capAtA (L.mem s) (exAtA ex 0) = .Thread ptr → + ptr ∈ Z.T ∧ ∀ c ∈ Z.Cs, L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) ≠ ptr) + (call : Bool) : + vcpu.decode_set_tcb st ar s a vc ex ⦃ res => + VcpuTopPostA (L := L) AR Z OS m call σ + ((decodeFl4VcpuSetTcb (specPtr vc) extras).run σ) s res ⦄ := by + unfold vcpu.decode_set_tcb + refine WP.spec_bind (Array.index_usize_spec ex 0#usize (by change 0 < ex.val.length; rw [ex.property]; decide)) ?_ + rintro s0 hs0_eq + have hs0_at : s0 = exAtA ex 0 := by simpa [exAtA] using hs0_eq + have hnull_ok : obj.Slot.is_null s0 = ok (decide (s0 = 0#u64)) := by unfold obj.Slot.is_null; rfl + rw [hnull_ok, bind_ok] + cases hex_eq : extras with + | nil => + have h0 : s0 = 0#u64 := by rw [hs0_at]; exact hEx.2.2 0 (by simp [hex_eq]) (by decide) + rw [if_pos (by simp [h0])] + unfold error.truncated; rw [bind_ok, WP.spec_ok] + exact ⟨rfl, hAR, .truncatedMessage, lift_vcpu_setTcb_errA rfl, Or.inl trivial⟩ + | cons ep rest_ex => + obtain ⟨hnz0, -⟩ := hExSlot (by simp [hex_eq]) + have hne0 : s0 ≠ 0#u64 := hs0_at ▸ hnz0 + rw [if_neg (by simp [hne0])] + have hcap_ok : obj.Slot.cap st s0 s = ok (capAtA (L.mem s) s0) := by + rw [Fl4ProofsA64.P1.W7.R.ST.capA_eq, ← capAtA_eq] + rw [hcap_ok, bind_ok] + obtain ⟨ps, hps, -, -, hcapRel⟩ := hEx.2.1 0 (by simp [hex_eq]) + rw [← hs0_at] at hcapRel + simp only [hex_eq, List.getElem_cons_zero] at hcapRel + have hIll : (∀ t, ep.1 ≠ .threadCap t) → + (do let r ← error.illegal Unit; ok (r, s, a)) ⦃ res => + VcpuTopPostA (L := L) AR Z OS m call σ + ((decodeFl4VcpuSetTcb (specPtr vc) (ep :: rest_ex)).run σ) s res ⦄ := by + intro hnt + unfold error.illegal; rw [bind_ok, WP.spec_ok] + have hd : (decodeVcpuSetTcb (Z := Unit) (.vcpuCap (specPtr vc)) (ep :: rest_ex)).run σ = + ({(.error .illegalOperation, σ)}, False) := by + rcases ep with ⟨ep1, ep2⟩ + unfold decodeVcpuSetTcb + cases hep : ep1 with + | threadCap t => exact absurd hep (hnt t) + | _ => rfl + exact ⟨rfl, hAR, .illegalOperation, lift_vcpu_setTcb_errA hd, Or.inl trivial⟩ + rcases hcapRel with ⟨hwf, habs⟩ + cases hc_eq : capAtA (L.mem s) s0 with + | Thread ptr => + dsimp only + rw [hc_eq] at habs + have hep1 : ep.1 = .threadCap (specPtr ptr) := by rw [← habs]; rfl + obtain ⟨hptrT, hptrNotIdle⟩ := hExTcb ptr (hs0_at ▸ hc_eq) + have hct_ok : sched.cur_thread st s = ok (L.curW s) := eq_of_spec_fd (sched.cur_thread.spec s) + rw [hct_ok, bind_ok] + have hcurT : L.curW s ∈ Z.T := C.E.j.1.curT (L.cpu s) C.E.j.1.sch.cpuCs + have hvc_st : ∀ v ∈ Z.V, stA (L.curW s) ≠ v ∧ stA (L.curW s) ≠ wa v 24#u64 := fun v hv => by + have hwa0 : wa v 0#u64 = v := by apply UScalar.eq_of_val_eq; rw [wa_valP]; simp [UScalar.hBounds] + rw [stA_eq] + exact ⟨by rw [← hwa0]; exact (C.hV.place.disj_tcb v hv _ hcurT 0#u64 3328#u64 (by decide) (by decide)).symm, + (C.hV.place.disj_tcb v hv _ hcurT 24#u64 3328#u64 (by decide) (by decide)).symm⟩ + have hCs128 : ∀ c ∈ Z.Cs, c.val < 128 := by + obtain ⟨n, -, hn128, hcn⟩ := C.E.j.1.csN; intro c hc; have := (hcn c).1 hc; omega + obtain ⟨xct, hkct, hst_ok, hst_spec, E1, -, hos1, -, -⟩ := C.E.restartA Pl Y hrun hcur hvs hvc_st + rw [hst_ok, bind_ok] + set s1 := L.wr s (stA (L.curW s)) sched.TS_RESTART + set σ1 := stUpdA σ σ.curThread xct .restart + have hJ1 : JAtZAX (L := L) AR Z s1 a σ1 m := ⟨{ E1.j.1 with ar := hAR }, E1.j.2⟩ + obtain ⟨X1, hpres1⟩ := vcpuXferStateA_of_restart_setTcbA C hCar hF hR hU hSc_st hkct hJ1 hos1 + have hksb1 : L.ksb s1 = L.ksb s := L.ksb_wr _ _ _ + have hcpu1 : L.cpu s1 = L.cpu s := L.cpu_wr _ _ _ + have hfa1 : ∀ q, FootA (L := L) Z s1 q → Aligned q := + fun q hq => C.hfa q (footA_of_ksb_cpuA hksb1 hcpu1 hq) + have hcpu1_128 : (L.cpu s1).val < 128 := by rw [hcpu1]; exact hCs128 _ C.E.j.1.sch.cpuCs + have hX0 : VcpuXferStateA (L := L) AR Z OS Ub m a s σ := + ⟨⟨{ C.E.j.1 with ar := hAR }, C.E.j.2⟩, C.hTr, hCar, C.hV, hF, hR⟩ + obtain ⟨hUbUser1, hBufNotS2_1, hRunSched1⟩ := + pres_lift_setTcbA hX0 (hw' := Z.hw) X1 hcpu1 C.hsub hpres1 hUbUser hBufNotS2 hRunSched + have hU_hw : ∀ hw', UbFootDisjA { Z with hw := hw' } OS Ub := fun _ => + ⟨hU.al_os, hU.disj_slotW, hU.disj_vs, hU.disj_ext, hU.tcb_al, hU.tcb_offVs, hU.tcb_offExt, + hU.tcb_offOs, hU.tcb_offKs, hU.tcb_offSc⟩ + have hLD_hw : ∀ hw' s', L.ksb s' = L.ksb s → TcbLinkDisjA { Z with hw := hw' } (L.ksb s') := fun _ _ hk => by + rw [hk]; exact ⟨hLD.offSlot, hLD.offKs, hLD.offSc, hLD.offVs, hLD.offExt⟩ + have hfa_hw : ∀ hw' s', L.ksb s' = L.ksb s → L.cpu s' = L.cpu s → + ∀ q, FootA (L := L) { Z with hw := hw' } s' q → Aligned q := fun _ s' hk hc q hq => by + have hq' : FootA (L := L) Z s' q := by + match hq with + | .slot y _ hy hw => exact .slot y q hy hw + | .nslot _ hn => + refine .nslot q ?_ + match hn with + | .node c _ hc ho => exact .node c q hc ho + | .cur _ ho => exact .cur q ho + | .tcb t _ ht ho => exact .tcb t q ht ho + | .sc c hc => exact .sc c hc + | .vcpu v _ hv ho => exact .vcpu v q hv ho + | .irq i hi => exact .irq i hi + | .cur2 _ ho => exact .cur2 q ho + | .glob _ ho => exact .glob q ho + | .tcb2 t _ ht ho => exact .tcb2 t q ht ho + | .sc2 c hc => exact .sc2 c hc + | .vs _ hv => exact .vs q hv + | .ext _ he => exact .ext q he + exact C.hfa q (footA_of_ksb_cpuA hk hc hq') + -- Step 1: `vcpu.unbind s1 a vc` + refine WP.spec_bind (unbind_refinesA X1 hP hAK hVR hU (by rw [hksb1]; exact hLD) + hUbUser1 hCs128 hcpu1_128 C.hExt C.hSw hfa1 C.hsub (fun c hc => X1.j.1.idleT c hc) + (by rw [hksb1]; exact hksTcb) hSlotTcb hOS_tcb C.hvc hStall hBufNotS2_1 hRunSched1 + hSlotSt hOS_st (by rw [hksb1]; exact hKs_st) hSc_st (by rw [hksb1]; exact hVs_st) hExt_st) ?_ + rintro ⟨s2, a1⟩ ⟨hw1, σ2, hsp_ub1, X2, hksb2', hcpu2', hvc0_2, -, -, -, hpres2⟩ + have hksb2 : L.ksb s2 = L.ksb s := hksb2'.trans hksb1 + have hcpu2 : L.cpu s2 = L.cpu s := hcpu2'.trans hcpu1 + have hpres12 : VcpuRunPresA L Z OS s s2 σ σ2 := hpres1.trans hpres2 + obtain ⟨hUbUser2, hBufNotS2_2, hRunSched2⟩ := + pres_lift_setTcbA hX0 X2 hcpu2 C.hsub hpres12 hUbUser hBufNotS2 hRunSched + -- Step 2: `old_v ← vcpu.bound s2 ptr; if old_v ≠ 0 then vcpu.unbind s2 a1 old_v else ok (s2, a1)` + show (Std.bind (vcpu.bound st s2 ptr) _) ⦃ _ ⦄ + have hbound_ok : vcpu.bound st s2 ptr = ok (L.mem s2 (wa ptr 3664#u64)) := by + unfold vcpu.bound; rw [eq_of_spec_fd Fl4ProofsA64.P0.Consts.layout.tcb.VCPU.spec, bind_ok, lift_ok, bind_ok, L.read_eq] + rw [hbound_ok, bind_ok] + obtain ⟨xt2, hkt2, htv2_spec, -⟩ := X2.vcpu.core.tcb ptr hptrT + refine WP.spec_bind (Pₘ := fun r2 => + ∃ hw2 σ3, + (do let oldV ← archThreadGet (Z := Unit) ArchTcb.tcbVcpu (specPtr ptr) + match oldV with | some ov => fl4VcpuUnbindExt ov | none => pure ()) σ2 = ({((), σ3)}, False) ∧ + VcpuXferStateA (L := L) AR { Z with hw := hw2 } OS Ub m r2.2 r2.1 σ3 ∧ + L.ksb r2.1 = L.ksb s ∧ L.cpu r2.1 = L.cpu s ∧ + L.mem r2.1 vc = 0#u64 ∧ L.mem r2.1 (wa ptr 3664#u64) = 0#u64 ∧ + VcpuRunPresA L Z OS s r2.1 σ σ3) ?_ ?_ + · by_cases hov_nz : (L.mem s2 (wa ptr 3664#u64) != 0#u64) = true + · have hov_ne0 : L.mem s2 (wa ptr 3664#u64) ≠ 0#u64 := fun e => by rw [e] at hov_nz; exact absurd hov_nz (by decide) + rw [if_pos hov_nz] + set ov := L.mem s2 (wa ptr 3664#u64) + obtain ⟨hovV, hov_back⟩ := X2.vcpu.bind_t2v ptr hptrT ov rfl hov_ne0 + have hov_ne_vc : ov ≠ vc := fun e => by + subst e; rw [hvc0_2] at hov_back; exact (hU.tcb_al ptr hptrT).ne0 (congrArg UScalar.val hov_back.symm) + have hsome_ov : xt2.tcbArch.tcbVcpu = some (specPtr ov) := by rw [htv2_spec]; unfold optRefA; rw [if_neg hov_ne0] + refine WP.spec_mono (unbind_refinesA X2 hP hAK hVR (hU_hw hw1) (hLD_hw hw1 s2 hksb2) + hUbUser2 hCs128 (by rw [hcpu2]; exact hCs128 _ C.E.j.1.sch.cpuCs) + C.hExt C.hSw (hfa_hw hw1 s2 hksb2 hcpu2) C.hsub (fun c hc => X2.j.1.idleT c hc) + (by rw [hksb2]; exact hksTcb) hSlotTcb hOS_tcb hovV hStall hBufNotS2_2 hRunSched2 + hSlotSt hOS_st (by rw [hksb2]; exact hKs_st) hSc_st (by rw [hksb2]; exact hVs_st) hExt_st) ?_ + rintro ⟨s3, a2⟩ ⟨hw2, σ3, hsp_ub2, X3, hksb3', hcpu3', -, -, hv_other3, htv_clr3, hpres3⟩ + have hvc0_3 : L.mem s3 vc = 0#u64 := (hv_other3 vc C.hvc hov_ne_vc.symm).trans hvc0_2 + have htv0_3 : L.mem s3 (wa ptr 3664#u64) = 0#u64 := by + have h := htv_clr3 (by rw [hov_back]; exact fun e => (hU.tcb_al ptr hptrT).ne0 (congrArg UScalar.val e)) + rwa [hov_back] at h + refine ⟨hw2, σ3, ?_, X3, hksb3'.trans hksb2, hcpu3'.trans hcpu2, hvc0_3, htv0_3, + hpres12.trans ⟨hpres3.os, hpres3.tcb_meta, hpres3.sc_max, hpres3.idle, hpres3.user_frame, hpres3.run_mon⟩⟩ + rw [nd_bind_oneSc (archThreadGet_eqA _ hkt2), hsome_ov]; exact hsp_ub2 + · have hov_eq0 : L.mem s2 (wa ptr 3664#u64) = 0#u64 := by by_contra hne; exact hov_nz (bne_iff_ne.2 hne) + rw [if_neg hov_nz, WP.spec_ok] + have hnone_ov : xt2.tcbArch.tcbVcpu = none := by rw [htv2_spec, hov_eq0]; rfl + refine ⟨hw1, σ2, ?_, X2, hksb2, hcpu2, hvc0_2, hov_eq0, hpres12⟩ + rw [nd_bind_oneSc (archThreadGet_eqA _ hkt2), hnone_ov]; rfl + -- Step 3: `remote_tcb_stall`, optional `complete_run` / `cancel_run`, `assoc_bind`, optional `set_vm_root` + rintro ⟨s3, a2⟩ ⟨hw2, σ3, hsp_ov, X3, hksb3, hcpu3, hvc0_3, htv0_3, hpres13⟩ + show (Std.bind (smp.remote_tcb_stall st ar s3 a2 ptr) _) ⦃ _ ⦄ + rw [hStall ptr hptrT s3 a2, bind_ok] + show (Std.bind layout.tcb.RUN_VCPU _) ⦃ _ ⦄ + have hrun_eq : wa ptr 3672#u64 = runVcpuA ptr := by unfold runVcpuA; rfl + rw [layout_tcb_run_vcpu_eqA, bind_ok, lift_ok, bind_ok, hrun_eq, L.read_eq, bind_ok] + obtain ⟨hUbUser3, hBufNotS2_3, hRunSched3⟩ := + pres_lift_setTcbA hX0 X3 hcpu3 C.hsub hpres13 hUbUser hBufNotS2 hRunSched + obtain ⟨xt3, hkt3, -, hrv3_spec⟩ := X3.vcpu.core.tcb ptr hptrT + refine WP.spec_bind (Pₘ := fun r3 => + ∃ hw3 σ4, + (do let rv ← getRunVcpu (Z := Unit) (specPtr ptr) + NonDet.when (rv ≠ none) do + let st ← getThreadState (specPtr ptr) + if st = ThreadState.ext .runningVM then fl4CompleteRun (specPtr ptr) .interrupt 0 0 0 else fl4CancelRun (specPtr ptr)) σ3 = + ({((), σ4)}, False) ∧ + VcpuXferStateA (L := L) AR { Z with hw := hw3 } OS Ub m r3.2 r3.1 σ4 ∧ + L.ksb r3.1 = L.ksb s ∧ L.cpu r3.1 = L.cpu s ∧ + L.mem r3.1 vc = 0#u64 ∧ L.mem r3.1 (wa ptr 3664#u64) = 0#u64 ∧ + L.mem r3.1 (runVcpuA ptr) = 0#u64 ∧ + VcpuRunPresA L Z OS s r3.1 σ σ4) ?_ ?_ + · by_cases hrv_nz : (L.mem s3 (runVcpuA ptr) != 0#u64) = true + · have hrv_ne0 : L.mem s3 (runVcpuA ptr) ≠ 0#u64 := fun e => by rw [e] at hrv_nz; exact absurd hrv_nz (by decide) + rw [if_pos hrv_nz] + have hst_ok3 : obj.Tcb.state st ptr s3 = ok (L.mem s3 (stA ptr)) := by + unfold obj.Tcb.state stA; rw [lift_ok, bind_ok, L.read_eq] + rw [hst_ok3, bind_ok] + have hst_vm : L.mem s3 (stA ptr) = sched.TS_RUNNING_VM := X3.vcpu.run_state ptr hptrT hrv_ne0 + rw [if_pos hst_vm] + have hxt3_vm : xt3.tcbState = .ext .runningVM := by + obtain ⟨xt0', hkt0', hst_rel, _⟩ := X3.j.1.sch.tcb ptr hptrT + have hx_eq : xt0' = xt3 := KernelObject.tcb.inj (Option.some.inj (hkt0'.symm.trans hkt3)) + rw [hx_eq, hst_vm] at hst_rel + cases hst_x : xt3.tcbState with + | ext e => cases e <;> simp [hst_x, tsCodeA] at hst_rel ⊢ + | _ => simp [hst_x, tsCodeA] at hst_rel + have hint_bv : fl4_abi.consts.vcpu_run_stop.INTERRUPT.bv = RunStop.interrupt.word := by + unfold fl4_abi.consts.vcpu_run_stop.INTERRUPT RunStop.word; rfl + refine WP.spec_mono (complete_run_refinesA X3 hP hAK hVR (hU_hw hw2) (hLD_hw hw2 s3 hksb3) + hUbUser3 hCs128 (by rw [hcpu3]; exact hCs128 _ C.E.j.1.sch.cpuCs) + C.hExt C.hSw (hfa_hw hw2 s3 hksb3 hcpu3) C.hsub (fun c hc => X3.j.1.idleT c hc) + (by rw [hksb3]; exact hksTcb) hSlotTcb hOS_tcb hptrT (hBufNotS2_3 ptr hptrT) + (hRunSched3 ptr hptrT) (hRunSched3 ptr hptrT hrv_ne0).2.2.2.1 + (hSlotSt ptr hptrT) (hOS_st ptr hptrT) (by rw [hksb3]; exact hKs_st ptr hptrT) + (hSc_st ptr hptrT) (by rw [hksb3]; exact hVs_st ptr hptrT) (hExt_st ptr hptrT) + hint_bv 0#u64 0#u64 0#u64) ?_ + rintro ⟨s4, a3⟩ ⟨hw3, σ4, hsp_cr, X4, hksb4', hcpu4', hrv0_4, -, hv_pres4, htv_pres4, hpres4⟩ + refine ⟨hw3, σ4, ?_, X4, hksb4'.trans hksb3, hcpu4'.trans hcpu3, + (hv_pres4 vc C.hvc).trans hvc0_3, (htv_pres4 ptr hptrT).trans htv0_3, hrv0_4, + hpres13.trans ⟨hpres4.os, hpres4.tcb_meta, hpres4.sc_max, hpres4.idle, hpres4.user_frame, hpres4.run_mon⟩⟩ + have hsome_rv : xt3.tcbExt.tcbRunVcpu = some (specPtr (L.mem s3 (runVcpuA ptr))) := by + rw [hrv3_spec]; unfold optRefA; rw [if_neg hrv_ne0] + rw [nd_bind_oneSc (getRunVcpu_eqA hkt3), hsome_rv, nd_when_trueA (by simp)] + unfold getThreadState + rw [nd_bind_oneSc (threadGet_eqSc _ hkt3), if_pos hxt3_vm]; exact hsp_cr + · have hrv_eq0 : L.mem s3 (runVcpuA ptr) = 0#u64 := by by_contra hne; exact hrv_nz (bne_iff_ne.2 hne) + rw [if_neg hrv_nz, WP.spec_ok] + have hnone_rv : xt3.tcbExt.tcbRunVcpu = none := by rw [hrv3_spec, hrv_eq0]; rfl + refine ⟨hw2, σ3, ?_, X3, hksb3, hcpu3, hvc0_3, htv0_3, hrv_eq0, hpres13⟩ + rw [nd_bind_oneSc (getRunVcpu_eqA hkt3), hnone_rv, nd_when_falseA (by simp)]; rfl + -- Step 4: write `vc.TCB := ptr` and `ptr.VCPU := vc`, optional `set_vm_root` + rintro ⟨s4, a3⟩ ⟨hw3, σ4, hsp_rv, X4, hksb4, hcpu4, hvc0_4, htv0_4, hrv0_4, hpres14⟩ + show (Std.bind (lift (wa vc vcpu.TCB)) _) ⦃ _ ⦄ + have htcb_const : vcpu.TCB = 0#u64 := by unfold vcpu.TCB; rfl + have hwa0 : wa vc 0#u64 = vc := by apply UScalar.eq_of_val_eq; rw [wa_valP]; simp [UScalar.hBounds] + rw [htcb_const, lift_ok, bind_ok, hwa0, L.write_eq, bind_ok] + have hset_b : vcpu.set_bound st (L.wr s4 vc ptr) ptr vc = + ok (L.wr (L.wr s4 vc ptr) (wa ptr 3664#u64) vc) := by + unfold vcpu.set_bound; rw [eq_of_spec_fd Fl4ProofsA64.P0.Consts.layout.tcb.VCPU.spec, bind_ok, lift_ok, bind_ok, L.write_eq] + rw [hset_b, bind_ok] + obtain ⟨v4, hkv4, -, -⟩ := X4.vcpu.core.obj vc C.hvc + obtain ⟨xt4, hkt4, -, -⟩ := X4.vcpu.core.tcb ptr hptrT + have hvc_cap4 : ∃ y ∈ OS, capAtA (L.mem s4) y = .Vcpu vc := by + obtain ⟨y, hy, hcap⟩ := hvc_cap + obtain ⟨h0, h8, -, -⟩ := hpres14.os y hy + exact ⟨y, hy, by unfold capAtA; rw [h0, h8]; exact hcap⟩ + have hptr_not_idle4 : ∀ c ∈ Z.Cs, L.mem s4 (L.nodeAt s4 c layout.node.IDLE_THREAD) ≠ ptr := fun c hc => by + rw [hpres14.idle c hc]; exact hptrNotIdle c hc + have X5 := X4.assoc_bind (hU_hw hw3) (hLD_hw hw3 s4 hksb4) hCs128 + (by rw [hcpu4]; exact hCs128 _ C.E.j.1.sch.cpuCs) + C.hExt C.hSw (hfa_hw hw3 s4 hksb4 hcpu4) C.hsub (fun c hc => X4.j.1.idleT c hc) + hOS_tcb C.hvc hptrT (X4.vcpu.place.al vc C.hvc).1 (fun e => (hU.tcb_al ptr hptrT).ne0 (congrArg UScalar.val e)) + hvc0_4 htv0_4 hrv0_4 hptr_not_idle4 hvc_cap4 hkv4 hkt4 + set s5 := L.wr (L.wr s4 vc ptr) (wa ptr 3664#u64) vc + set σ5 := updKSc (updKSc σ4 (specPtr ptr) (.tcb { xt4 with tcbArch := { xt4.tcbArch with tcbVcpu := some (specPtr vc) } })) + (specPtr vc) (.archObj (.vcpu { v4 with vcpuTcb := some (specPtr ptr) })) + -- Combine spec steps into `fl4VcpuSetTcbRustOrder` and convert to `fl4PerformVcpuInv (.setTcb)` + have hrust : fl4VcpuSetTcbRustOrder (specPtr vc) (specPtr ptr) σ1 = ({([], σ5)}, False) := by + unfold fl4VcpuSetTcbRustOrder + rw [nd_bind_oneSc hsp_ub1] + rw [nd_bind_oneSc (archThreadGet_eqA _ hkt2)] at hsp_ov ⊢ + dsimp only at hsp_ov ⊢ + have htail_rv : (do + let rv ← getRunVcpu (Z := Unit) (specPtr ptr) + NonDet.when (rv ≠ none) do + let st ← getThreadState (specPtr ptr) + if st = ThreadState.ext .runningVM then fl4CompleteRun (specPtr ptr) .interrupt 0 0 0 else fl4CancelRun (specPtr ptr) + archThreadSet (fun atcb => { atcb with tcbVcpu := some (specPtr vc) }) (specPtr ptr) + vcpuUpdate (specPtr vc) fun v => { v with vcpuTcb := some (specPtr ptr) } + return ([] : List Data)) σ3 = ({(([] : List Data), σ5)}, False) := by + rw [nd_bind_oneSc (getRunVcpu_eqA hkt3)] at hsp_rv ⊢ + rw [nd_bind_oneSc hsp_rv] + have hne_vc_ptr : specPtr vc ≠ specPtr ptr := fun e => by rw [e, hkt4] at hkv4; cases hkv4 + have hkv4' : (updKSc σ4 (specPtr ptr) (.tcb { xt4 with tcbArch := { xt4.tcbArch with tcbVcpu := some (specPtr vc) } })).kheap (specPtr vc) = + some (.archObj (.vcpu v4)) := by rw [updKSc_other _ hne_vc_ptr]; exact hkv4 + rw [nd_bind_oneSc (archThreadSet_eqA _ hkt4), nd_bind_oneSc (vcpuUpdate_eqA _ hkv4')]; rfl + cases hov : xt2.tcbArch.tcbVcpu with + | none => + rw [hov] at hsp_ov + have hσ3_eq : σ3 = σ2 := (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (Prod.mk.inj hsp_ov).1)).2.symm + rw [← hσ3_eq]; exact htail_rv + | some ov => + rw [hov] at hsp_ov + dsimp only at hsp_ov ⊢ + rw [nd_bind_oneSc hsp_ov]; exact htail_rv + obtain ⟨v1_0, hkv1_0, -, -⟩ := X1.vcpu.core.obj vc C.hvc + obtain ⟨xt1_0, hkt1_0, -, -⟩ := X1.vcpu.core.tcb ptr hptrT + have hperf : fl4PerformVcpuInv (Z := Unit) (.setTcb (specPtr vc) (specPtr ptr)) σ1 = ({([], σ5)}, False) := + fl4SetTcb_rust_order_eqA (fun p => ∃ u ∈ Z.T, specPtr u = p) ⟨ptr, hptrT, rfl⟩ + X1.vcpu.arch hkv1_0 hkt1_0 + (fun v' xv p hkv' hvt' => by + obtain ⟨v0, hv0V, rfl⟩ := X1.vcpu.core.back v' xv hkv' + obtain ⟨xv0, hkv0', hvt0', -⟩ := X1.vcpu.core.obj v0 hv0V + rw [hkv'] at hkv0'; cases hkv0' + have hnz : L.mem s1 v0 ≠ 0#u64 := by intro e; rw [e] at hvt0'; rw [hvt0'] at hvt'; cases hvt' + obtain ⟨hpT, hp_back⟩ := X1.vcpu.bind_v2t v0 hv0V _ rfl hnz + obtain ⟨xp, hkp, htvp, -⟩ := X1.vcpu.core.tcb _ hpT + have hp_eq : p = specPtr (L.mem s1 v0) := by + rw [hvt0'] at hvt'; unfold optRefA at hvt'; rw [if_neg hnz] at hvt'; exact (Option.some.inj hvt').symm + subst hp_eq + exact ⟨⟨_, hpT, rfl⟩, xp, hkp, by rw [htvp, hp_back]; unfold optRefA; rw [if_neg (X1.vcpu.place.al v0 hv0V).1]⟩) + (fun p xp v' hp hkp htvp => by + rcases hp with ⟨u, huT, rfl⟩ + obtain ⟨xp0, hkp0, htvp0, -⟩ := X1.vcpu.core.tcb u huT + rw [hkp] at hkp0; cases hkp0 + have hnz : L.mem s1 (wa u 3664#u64) ≠ 0#u64 := by intro e; rw [e] at htvp0; rw [htvp0] at htvp; cases htvp + obtain ⟨hvV, hv_back⟩ := X1.vcpu.bind_t2v u huT _ rfl hnz + obtain ⟨xv, hkv', hvt', -⟩ := X1.vcpu.core.obj _ hvV + have hv_eq : v' = specPtr (L.mem s1 (wa u 3664#u64)) := by + rw [htvp0] at htvp; unfold optRefA at htvp; rw [if_neg hnz] at htvp; exact (Option.some.inj htvp).symm + subst hv_eq + exact ⟨xv, hkv', by rw [hvt', hv_back]; unfold optRefA; rw [if_neg (fun e => (hU.tcb_al u huT).ne0 (congrArg UScalar.val e))]⟩) + (fun v' xv c hkv' hrc' => by + obtain ⟨v0, hv0V, rfl⟩ := X1.vcpu.core.back v' xv hkv' + obtain ⟨xv0, hkv0', -, hrc0'⟩ := X1.vcpu.core.obj v0 hv0V + rw [hkv'] at hkv0'; cases hkv0' + have hnz : L.mem s1 (wa v0 24#u64) ≠ 0#u64 := by intro e; rw [e] at hrc0'; rw [hrc0'] at hrc'; cases hrc' + obtain ⟨hcT, hc_back⟩ := X1.vcpu.run_v2c v0 hv0V _ rfl hnz + obtain ⟨xc, hkc, -, hcrv⟩ := X1.vcpu.core.tcb _ hcT + have hc_eq : c = specPtr (L.mem s1 (wa v0 24#u64)) := by + rw [hrc0'] at hrc'; unfold optRefA at hrc'; rw [if_neg hnz] at hrc'; exact (Option.some.inj hrc').symm + subst hc_eq + exact ⟨⟨_, hcT, rfl⟩, xc, hkc, by rw [hcrv, hc_back]; unfold optRefA; rw [if_neg (X1.vcpu.place.al v0 hv0V).1]⟩) + (fun c xc v' hc hkc hcrv => by + rcases hc with ⟨u, huT, rfl⟩ + obtain ⟨xc0, hkc0, -, hcrv0⟩ := X1.vcpu.core.tcb u huT + rw [hkc] at hkc0; cases hkc0 + have hnz : L.mem s1 (runVcpuA u) ≠ 0#u64 := by intro e; rw [e] at hcrv0; rw [hcrv0] at hcrv; cases hcrv + obtain ⟨hvV, hv_back⟩ := X1.vcpu.run_c2v u huT _ rfl hnz + obtain ⟨xv, hkv', -, hrc'⟩ := X1.vcpu.core.obj _ hvV + have hv_eq : v' = specPtr (L.mem s1 (runVcpuA u)) := by + rw [hcrv0] at hcrv; unfold optRefA at hcrv; rw [if_neg hnz] at hcrv; exact (Option.some.inj hcrv).symm + subst hv_eq + exact ⟨xv, hkv', by rw [hrc', hv_back]; unfold optRefA; rw [if_neg (fun e => (hU.tcb_al u huT).ne0 (congrArg UScalar.val e))]⟩) + (fun p xp hp hkp htvp => by + rcases hp with ⟨u, huT, rfl⟩ + obtain ⟨xp0, hkp0, htvp0, hrvp0⟩ := X1.vcpu.core.tcb u huT + rw [hkp] at hkp0; cases hkp0 + have hnz : L.mem s1 (wa u 3664#u64) ≠ 0#u64 := by intro e; rw [e] at htvp0; exact htvp (by rw [htvp0]; rfl) + rw [hrvp0, X1.vcpu.bind_run_disj u huT hnz]; rfl) + (fun p xp sc hp hkp hsc => by + rcases hp with ⟨u, huT, rfl⟩ + obtain ⟨xp0, hkp0, _, hsc0⟩ := X1.j.1.sch.tcb u huT + have hx_eq : xp0 = xp := KernelObject.tcb.inj (Option.some.inj (hkp0.symm.trans hkp)) + rw [← hx_eq, hsc0] at hsc + have hnz : L.mem s1 (scA u) ≠ 0#u64 := by intro e; unfold optScA at hsc; rw [e, if_pos rfl] at hsc; cases hsc + have hscC := X1.j.1.rest.scMem u huT hnz + obtain ⟨y, n, hks, -⟩ := X1.j.1.sch.sc _ hscC + unfold optScA at hsc; rw [if_neg hnz] at hsc + rw [← Option.some.inj hsc]; exact ⟨y, n, hks⟩) + hrust + have hd_ok : (decodeVcpuSetTcb (Z := Unit) (.vcpuCap (specPtr vc)) (ep :: rest_ex)).run σ = + ({(.ok (.invokeVCPU (.vcpuSetTCB (specPtr vc) (specPtr ptr))), σ)}, False) := by + unfold decodeVcpuSetTcb + rw [show ep = (.threadCap (specPtr ptr), ep.2) from Prod.ext hep1 rfl]; rfl + have hperf_mem : (.ok ([] : List Data), σ5) ∈ + ((fl4ArchPerformInvocation (Z := Unit) (.vcpu (.setTcb (specPtr vc) (specPtr ptr)))).run σ1).1 := by + unfold fl4ArchPerformInvocation NonDetE.liftE + change (Except.ok [], σ5) ∈ ((fl4PerformVcpuInv _ >>= _) _).1 + rw [nd_bind_oneSc hperf]; exact Set.mem_singleton _ + have hcur_ok5 : sched.cur_thread st s5 = ok (L.curW s5) := eq_of_spec_fd (sched.cur_thread.spec s5) + have heq_ok5 : obj.Tcb.Insts.CoreCmpPartialEqTcb.eq ptr (L.curW s5) = ok (decide (ptr = L.curW s5)) := by + unfold obj.Tcb.Insts.CoreCmpPartialEqTcb.eq; rfl + rw [hcur_ok5, bind_ok, heq_ok5, bind_ok] + by_cases hcur_eq : ptr = L.curW s5 + · rw [if_pos (by simp [hcur_eq])] + refine WP.spec_bind (hVR.set_vm_root s5 a3 hw3 ptr X5.j.1.ar) ?_ + rintro ⟨s6, a5⟩ ⟨hs6, hw5, hAR5⟩ + dsimp only at hs6; subst hs6 + change (ok (core.result.Result.Ok (), s5, a5) : Result _) ⦃ _ ⦄ + rw [WP.spec_ok] + have X5' := X5.of_hw hAR5 + exact ⟨.vcpu (.setTcb (specPtr vc) (specPtr ptr)), lift_vcpu_setTcb_okA hd_ok, + σ1, hst_spec, σ5, [], hperf_mem, Or.inl ⟨{ Z with hw := hw5 }, X5'.j, X5'.tr, fun _ => X5'.car⟩⟩ + · rw [if_neg (by simp [hcur_eq]), WP.spec_ok] + exact ⟨.vcpu (.setTcb (specPtr vc) (specPtr ptr)), lift_vcpu_setTcb_okA hd_ok, + σ1, hst_spec, σ5, [], hperf_mem, Or.inl ⟨{ Z with hw := hw3 }, X5.j, X5.tr, fun _ => X5.car⟩⟩ + | PageTable p m_pt asid va => + rw [hc_eq] at habs; dsimp only + cases m_pt <;> exact hIll (fun t e => by rw [e] at habs; cases habs) + | _ => + rw [hc_eq] at habs hwf + dsimp only + first + | exact False.elim hwf + | exact hIll (fun t e => by rw [e] at habs; cases habs) + +end set_tcb_j + +end + +end Fl4ProofsA64.P3.Vcpu diff --git a/proofs/Fl4ProofsA64/P3/VcpuSpecA.lean b/proofs/Fl4ProofsA64/P3/VcpuSpecA.lean new file mode 100644 index 000000000..810ab6806 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/VcpuSpecA.lean @@ -0,0 +1,1390 @@ +/- +P3 (aarch64, A64-P3 S4 W0): **VCPU spec deltas conservativity, `setVmRoot` side lemma, and +deterministic `SMonad` single-step equations** for `Fl4SpecA64/Ext/ARMVCPURun.lean`. + +Contents: +- D1 conservativity: `fl4VcpuRegGet_vmcr_u32`, `fl4VcpuRegGet_vmcr_init`, `fl4VcpuRegGet_set_sameA`, + `fl4VcpuRegGet_set_otherA`, `syncVgicStatus_pres_nonStatusA`, `fl4AssociateVcpuTcb_ne_cur`. +- D3 side lemma: `VmRootStateEq`, `setVmRoot_preserves_state`, `fl4CompleteRun_preserves_curThread`, + `fl4CompleteRun_ne_cur`. +- Deterministic single-step equations for `getVcpu`, `setVcpu`, `vcpuUpdate`, `fl4VcpuGet`, + `fl4VcpuSet`, `fl4VcpuExtUpdate`, `fl4VcpuFlush`, `threadSet`, `getRunVcpu`, `setRunVcpu`, + `archThreadGet`, `archThreadSet`, `dissociateVcpuTcb`, `fl4DissociateVcpuTcb`, `fl4CancelRun`, + `fl4VcpuEndRun`, `fl4VcpuCheck`, and `fl4PerformVcpuInv`. + +No named premise. +-/ +import Fl4ProofsA64.P3.ScSpecA +import Fl4ProofsA64.P3.ScCheckA +import Fl4ProofsA64.P3.FrontEndExtraA +import Fl4ProofsA64.P3.EpBufA +import Fl4ProofsA64.P3.NtfnSpecA +import Fl4SpecA64.Ext.Invocation + +namespace Fl4ProofsA64.P3.Vcpu + +open Fl4Spec Fl4SpecA64 Fl4ProofsA64.P3.Sched + +noncomputable section + +/-! ## D1 conservativity: 64-bit `VGIC_VMCR` and 64-register view -/ + +theorem ucast_64_32_of_hi_zero {w : Word} (hhi : w >>> 32 = 0) : + ucast 64 (ucast 32 w) = w := by + ext i hi + unfold ucast + rw [BitVec.getElem_setWidth, BitVec.getLsbD_setWidth] + by_cases h32 : i < 32 + · simp [h32, BitVec.getLsbD_eq_getElem hi] + · simp only [h32, decide_false, Bool.false_and] + have h0 : BitVec.getLsbD w (32 + (i - 32)) = false := by + have := congrArg (fun x : BitVec 64 => x[i - 32]'(by omega)) hhi + simpa [BitVec.getElem_ushiftRight] using this + rw [show 32 + (i - 32) = i by omega] at h0 + exact h0.symm + +/-- **D1 conservativity (`VGIC_VMCR`)**: when the upper 32 bits of register 36 are 0 and +`vcpuVgic.vgicVmcr` mirrors its low 32 bits, `fl4VcpuRegGet v vrVgicVmcr` equals +`ucast 64 v.vcpuVgic.vgicVmcr`. -/ +theorem fl4VcpuRegGet_vmcr_u32 (v : Vcpu) + (hhi : v.vcpuExt.regs ⟨vrVgicVmcr, by decide⟩ >>> 32 = 0) + (hlo : ucast 32 (v.vcpuExt.regs ⟨vrVgicVmcr, by decide⟩) = v.vcpuVgic.vgicVmcr) : + fl4VcpuRegGet v vrVgicVmcr = ucast 64 v.vcpuVgic.vgicVmcr := by + have hget : fl4VcpuRegGet v vrVgicVmcr = v.vcpuExt.regs ⟨vrVgicVmcr, by decide⟩ := by + unfold fl4VcpuRegGet vrVgicHcr vrVgicLr0 vrVgicVmcr + simp + rw [hget, ← hlo, ucast_64_32_of_hi_zero hhi] + +/-- At `fl4VcpuInit defaultVcpu`, `VGIC_VMCR` is 0 in both `vcpuExt.regs` and `vcpuVgic.vgicVmcr`. -/ +theorem fl4VcpuRegGet_vmcr_init : + fl4VcpuRegGet (fl4VcpuInit defaultVcpu) vrVgicVmcr = + ucast 64 (fl4VcpuInit defaultVcpu).vcpuVgic.vgicVmcr := by + have h1 : (fl4VcpuInit defaultVcpu).vcpuExt.regs ⟨vrVgicVmcr, by decide⟩ = 0 := by + unfold fl4VcpuInit syncVgicStatus fl4VcpuRegSet defaultVcpu ExtVcpu.blank + simp [vrSctlr, vrVmpidr, vrVpidr, vrHcr, vrVgicHcr, vrVgicVtr, vrVgicVmcr, + vrVgicMisr, vrVgicEisr, vrVgicElrsr, vrVgicLr0] + have h2 : (fl4VcpuInit defaultVcpu).vcpuVgic.vgicVmcr = 0 := by + unfold fl4VcpuInit syncVgicStatus fl4VcpuRegSet defaultVcpu defaultGicVcpuInterface + simp [vrSctlr, vrVmpidr, vrVpidr, vrHcr, vrVgicHcr, vrVgicVtr, vrVgicVmcr, + vrVgicMisr, vrVgicEisr, vrVgicElrsr, vrVgicLr0] + apply fl4VcpuRegGet_vmcr_u32 + · rw [h1]; rfl + · rw [h1, h2]; rfl + +theorem vcpuReg_toEnum_inj {i j : Nat} (hi : i < 25) (hj : j < 25) + (h : VCPUReg.toEnum i = VCPUReg.toEnum j) : i = j := by + have hlen : VCPUReg.enum.length = 25 := by decide + have hnodup : VCPUReg.enum.Nodup := by decide + have hi' : i < VCPUReg.enum.length := by omega + have hj' : j < VCPUReg.enum.length := by omega + have hlt : ∀ (n : Nat) (hn : n < VCPUReg.enum.length), VCPUReg.toEnum n = VCPUReg.enum[n]'hn := by + intro n hn + unfold VCPUReg.toEnum + rw [List.getElem?_eq_getElem hn]; rfl + have heq : VCPUReg.enum[i]'hi' = VCPUReg.enum[j]'hj' := + (hlt i hi').symm.trans (h.trans (hlt j hj')) + exact (List.Nodup.getElem_inj_iff hnodup).1 heq + +theorem fl4VcpuRegGet_set_sameA (v : Vcpu) {i : Nat} (hi : i < 64) (w : Word) : + fl4VcpuRegGet (fl4VcpuRegSet i w v) i = + if i = vrVgicHcr then ucast 64 (ucast 32 w) else w := by + unfold fl4VcpuRegGet fl4VcpuRegSet vrVgicHcr vrVgicVmcr vrVgicLr0 + by_cases h25 : i < 25 + · have hne : i ≠ 35 := by omega + simp [h25, hne] + · by_cases hhcr : i = 35 + · subst hhcr; simp + · by_cases hvmcr : i = 36 + · subst hvmcr; simp + · by_cases hlr : 48 ≤ i + · simp [h25, hhcr, hvmcr, hlr, hi] + · simp [h25, hhcr, hvmcr, hlr, hi] + +theorem fl4VcpuRegGet_set_otherA (v : Vcpu) {i j : Nat} (hi : i < 64) (hj : j < 64) (hne : j ≠ i) + (w : Word) : + fl4VcpuRegGet (fl4VcpuRegSet i w v) j = fl4VcpuRegGet v j := by + unfold fl4VcpuRegGet fl4VcpuRegSet vrVgicHcr vrVgicVmcr vrVgicLr0 + by_cases hi25 : i < 25 + · simp only [hi25, ↓reduceIte] + by_cases hj25 : j < 25 + · simp only [hj25, ↓reduceIte] + have hne' : VCPUReg.toEnum j ≠ VCPUReg.toEnum i := fun h => hne (vcpuReg_toEnum_inj hj25 hi25 h) + simp [hne'] + · simp [hj25] + · simp only [hi25, ↓reduceIte] + by_cases hihcr : i = 35 + · simp only [hihcr, ↓reduceIte] + have hjhcr : j ≠ 35 := by omega + by_cases hj25 : j < 25 + · simp [hj25] + · simp [hj25, hjhcr] + · simp only [hihcr, ↓reduceIte] + by_cases hivmcr : i = 36 + · simp only [hivmcr, ↓reduceIte] + have hjvmcr : j ≠ 36 := by omega + by_cases hj25 : j < 25 + · simp [hj25] + · by_cases hjhcr : j = 35 + · simp [hj25, hjhcr] + · by_cases hjlr : 48 ≤ j + · simp [hj25, hjhcr, hjlr, hj] + · simp only [hj25, hjhcr, hjlr, hj, false_and, ↓reduceIte, ↓reduceDIte] + have hfin : (⟨j, hj⟩ : Fin 64) ≠ ⟨36, by decide⟩ := by + intro h; exact hjvmcr (congrArg Fin.val h) + exact Function.update_of_ne hfin _ _ + · simp only [hivmcr, ↓reduceIte] + by_cases hilr : 48 ≤ i + · simp only [hilr, hi, and_self, ↓reduceIte] + by_cases hj25 : j < 25 + · simp [hj25] + · by_cases hjhcr : j = 35 + · simp [hj25, hjhcr] + · by_cases hjlr : 48 ≤ j + · simp only [hj25, hjhcr, hjlr, hj, and_self, ↓reduceIte] + have hne' : j - 48 ≠ i - 48 := by omega + simp [hne'] + · simp [hj25, hjhcr, hjlr, hj] + · simp only [hilr, hi, false_and, ↓reduceIte, ↓reduceDIte] + by_cases hj25 : j < 25 + · simp [hj25] + · by_cases hjhcr : j = 35 + · simp [hj25, hjhcr] + · by_cases hjlr : 48 ≤ j + · simp [hj25, hjhcr, hjlr, hj] + · simp only [hj25, hjhcr, hjlr, hj, false_and, ↓reduceIte, ↓reduceDIte] + have hfin : (⟨j, hj⟩ : Fin 64) ≠ ⟨i, hi⟩ := by + intro h; exact hne (congrArg Fin.val h) + simp [hfin] + +theorem fl4VcpuRegSet_pres_metaA (i : Nat) (w : Word) (v : Vcpu) : + (fl4VcpuRegSet i w v).vcpuTcb = v.vcpuTcb ∧ + (fl4VcpuRegSet i w v).vcpuVppiMasked = v.vcpuVppiMasked ∧ + (fl4VcpuRegSet i w v).vcpuExt.runCaller = v.vcpuExt.runCaller ∧ + (fl4VcpuRegSet i w v).vcpuExt.runOffset = v.vcpuExt.runOffset ∧ + (fl4VcpuRegSet i w v).vcpuExt.runOutMask = v.vcpuExt.runOutMask ∧ + (fl4VcpuRegSet i w v).vcpuExt.ownerCpu = v.vcpuExt.ownerCpu := by + unfold fl4VcpuRegSet + split_ifs <;> simp + +theorem syncVgicStatus_pres_metaA (v : Vcpu) (lrs : Nat) (gicV3 : Bool) : + (syncVgicStatus v lrs gicV3).vcpuTcb = v.vcpuTcb ∧ + (syncVgicStatus v lrs gicV3).vcpuVppiMasked = v.vcpuVppiMasked ∧ + (syncVgicStatus v lrs gicV3).vcpuExt.runCaller = v.vcpuExt.runCaller ∧ + (syncVgicStatus v lrs gicV3).vcpuExt.runOffset = v.vcpuExt.runOffset ∧ + (syncVgicStatus v lrs gicV3).vcpuExt.runOutMask = v.vcpuExt.runOutMask ∧ + (syncVgicStatus v lrs gicV3).vcpuExt.ownerCpu = v.vcpuExt.ownerCpu := by + unfold syncVgicStatus + dsimp only + set acc := (List.range lrs).foldl (fun (acc : Word × Word × Nat × Bool) (i : Nat) => + let (elrsr, eisr, valid, pend) := acc + let (state, hw, eoi) := lrFields (fl4VcpuRegGet v (vrVgicLr0 + i)) gicV3 + if state = 0 ∧ (hw ≠ 0 ∨ eoi = 0) then (elrsr ||| (1 <<< i), eisr, valid, pend) + else if state = 0 ∧ hw = 0 ∧ eoi ≠ 0 then (elrsr, eisr ||| (1 <<< i), valid, pend) + else (elrsr, eisr, valid + 1, pend || decide (state = 1))) (0, 0, 0, false) + set v1 := fl4VcpuRegSet vrVgicElrsr acc.1 v + set v2 := fl4VcpuRegSet vrVgicEisr acc.2.1 v1 + have h1 := fl4VcpuRegSet_pres_metaA vrVgicElrsr acc.1 v + have h2 := fl4VcpuRegSet_pres_metaA vrVgicEisr acc.2.1 v1 + have h3 := fl4VcpuRegSet_pres_metaA vrVgicMisr + (let hcr := fl4VcpuRegGet v vrVgicHcr + let vmcr := fl4VcpuRegGet v vrVgicVmcr + let bit (b : Nat) (c : Bool) : Word := if c then (1 : Word) <<< b else 0 + bit 0 (acc.2.1 ≠ 0) ||| bit 1 (hcr &&& 2 ≠ 0 ∧ acc.2.2.1 ≤ 1) ||| + bit 2 (hcr &&& 4 ≠ 0 ∧ (hcr >>> 27) &&& 0x1f ≠ 0) ||| bit 3 (hcr &&& 8 ≠ 0 ∧ ¬acc.2.2.2) ||| + bit 4 (hcr &&& 16 ≠ 0 ∧ vmcr &&& 1 ≠ 0) ||| bit 5 (hcr &&& 32 ≠ 0 ∧ vmcr &&& 1 = 0) ||| + bit 6 (hcr &&& 64 ≠ 0 ∧ vmcr &&& 2 ≠ 0) ||| bit 7 (hcr &&& 128 ≠ 0 ∧ vmcr &&& 2 = 0)) v2 + refine ⟨h3.1.trans (h2.1.trans h1.1), + h3.2.1.trans (h2.2.1.trans h1.2.1), + h3.2.2.1.trans (h2.2.2.1.trans h1.2.2.1), + h3.2.2.2.1.trans (h2.2.2.2.1.trans h1.2.2.2.1), + h3.2.2.2.2.1.trans (h2.2.2.2.2.1.trans h1.2.2.2.2.1), + h3.2.2.2.2.2.trans (h2.2.2.2.2.2.trans h1.2.2.2.2.2)⟩ + +theorem syncVgicStatus_pres_otherRegA (v : Vcpu) (lrs : Nat) (gicV3 : Bool) {j : Nat} (hj : j < 64) + (h37 : j ≠ vrVgicMisr) (h38 : j ≠ vrVgicEisr) (h39 : j ≠ vrVgicElrsr) : + fl4VcpuRegGet (syncVgicStatus v lrs gicV3) j = fl4VcpuRegGet v j := by + unfold syncVgicStatus + dsimp only + rw [fl4VcpuRegGet_set_otherA _ (by unfold vrVgicMisr; omega) hj h37] + rw [fl4VcpuRegGet_set_otherA _ (by unfold vrVgicEisr; omega) hj h38] + rw [fl4VcpuRegGet_set_otherA _ (by unfold vrVgicElrsr; omega) hj h39] + +/-! ## D3 side lemma: `setVmRoot` preserves every non-`machineState`/VMID-cache field -/ + +/-- Equality of every `State Unit` field except `machineState.machineStateRest` and the three +hardware VMID-cache fields of `archState` (`armAsidMap`, `armVmidTable`, `armNextVmid`). -/ +def VmRootStateEq (σ τ : State Unit) : Prop := + ∃ msr am vt nv, + τ = { σ with + machineState := { σ.machineState with machineStateRest := msr } + archState := { σ.archState with + armAsidMap := am, armVmidTable := vt, armNextVmid := nv } } + +theorem VmRootStateEq.refl (σ : State Unit) : VmRootStateEq σ σ := + ⟨σ.machineState.machineStateRest, σ.archState.armAsidMap, σ.archState.armVmidTable, + σ.archState.armNextVmid, rfl⟩ + +theorem VmRootStateEq.trans {σ₁ σ₂ σ₃ : State Unit} (h₁ : VmRootStateEq σ₁ σ₂) + (h₂ : VmRootStateEq σ₂ σ₃) : VmRootStateEq σ₁ σ₃ := by + rcases h₁ with ⟨msr₁, am₁, vt₁, nv₁, rfl⟩ + rcases h₂ with ⟨msr₂, am₂, vt₂, nv₂, rfl⟩ + exact ⟨msr₂, am₂, vt₂, nv₂, rfl⟩ + +theorem updKSc_other (σ : State Unit) {p q : ObjRef} {o : KernelObject} (hne : q ≠ p) : + (updKSc σ p o).kheap q = σ.kheap q := by + simp [updKSc, Function.update, hne] + +private theorem doMachineOp_lift_vmRootStateEq (f : MachineRestMonad Unit) + {σ τ : State Unit} {a : Unit} + (h : (a, τ) ∈ (doMachineOp (Z := Unit) (machineOpLift f) σ).1) : + VmRootStateEq σ τ := by + unfold doMachineOp at h + rcases h with ⟨⟨ms, s1⟩, h1, h2⟩ + simp only [NonDet.gets, Set.mem_singleton_iff, Prod.mk.injEq] at h1 + rcases h1 with ⟨rfl, h1s⟩; subst s1 + rcases h2 with ⟨⟨⟨u, ms'⟩, s2⟩, h3, h4⟩ + simp only [NonDet.selectF, Set.mem_setOf_eq] at h3 + rcases h3 with ⟨hlift, hs2⟩; subst s2 + unfold machineOpLift at hlift + rcases hlift with ⟨⟨r0, ms1⟩, hg0, hrest0⟩ + simp only [NonDet.gets, Set.mem_singleton_iff, Prod.mk.injEq] at hg0 + rcases hg0 with ⟨rfl, hms1⟩; subst ms1 + rcases hrest0 with ⟨⟨⟨u'', r'⟩, ms2⟩, hsf, hrest1⟩ + simp only [NonDet.selectF, Set.mem_setOf_eq] at hsf + rcases hsf with ⟨_, hms2⟩; subst ms2 + rcases hrest1 with ⟨⟨_, ms3⟩, hmodm, hretm⟩ + simp only [NonDet.modify, Set.mem_singleton_iff, Prod.mk.injEq] at hmodm + rcases hmodm with ⟨_, hms3⟩; subst ms3 + simp only [pure, NonDet.pure, Set.mem_singleton_iff, Prod.mk.injEq] at hretm + rcases hretm with ⟨_, hms'⟩; subst ms' + rcases h4 with ⟨⟨_, s3⟩, hmod, hret⟩ + simp only [NonDet.modify, Set.mem_singleton_iff, Prod.mk.injEq] at hmod + rcases hmod with ⟨_, hmod_s⟩; subst s3 + simp only [pure, NonDet.pure, Set.mem_singleton_iff, Prod.mk.injEq] at hret + rcases hret with ⟨_, hτ⟩; subst τ + exact ⟨r', σ.archState.armAsidMap, σ.archState.armVmidTable, σ.archState.armNextVmid, rfl⟩ + +private theorem bind_vmRootStateEq {α β : Type} {m : SMonad Unit α} {f : α → SMonad Unit β} + (hm : ∀ σ a τ, (a, τ) ∈ (m σ).1 → VmRootStateEq σ τ) + (hf : ∀ a σ b τ, (b, τ) ∈ (f a σ).1 → VmRootStateEq σ τ) + {σ τ : State Unit} {b : β} (h : (b, τ) ∈ ((m >>= f) σ).1) : VmRootStateEq σ τ := by + rcases h with ⟨⟨a, σ₁⟩, h1, h2⟩ + exact (hm σ a σ₁ h1).trans (hf a σ₁ b τ h2) + +private theorem pure_vmRootStateEq {α : Type} (a : α) {σ τ : State Unit} {b : α} + (h : (b, τ) ∈ ((pure a : SMonad Unit α) σ).1) : VmRootStateEq σ τ := by + simp only [pure, NonDet.pure, Set.mem_singleton_iff, Prod.mk.injEq] at h + rw [h.2]; exact VmRootStateEq.refl _ + +private theorem updateAsidMap_vmRootStateEq (asid : Asid) (v : Option Vmid) + {σ τ : State Unit} {u : Unit} + (h : (u, τ) ∈ (updateAsidMap (Z := Unit) asid v σ).1) : VmRootStateEq σ τ := by + unfold updateAsidMap at h + rw [nd_gets_bindSc] at h + rcases h with ⟨⟨_, s1⟩, hp, hmod⟩ + simp only [pure, NonDet.pure, Set.mem_singleton_iff, Prod.mk.injEq] at hp + rcases hp with ⟨rfl, h1⟩; subst s1 + simp only [NonDet.modify, Set.mem_singleton_iff, Prod.mk.injEq] at hmod + rw [hmod.2] + exact ⟨σ.machineState.machineStateRest, Function.update (asidMap σ) asid v, + σ.archState.armVmidTable, σ.archState.armNextVmid, rfl⟩ + +private theorem invalidateVmidEntry_vmRootStateEq (vmid : Vmid) + {σ τ : State Unit} {u : Unit} + (h : (u, τ) ∈ (invalidateVmidEntry (Z := Unit) vmid σ).1) : VmRootStateEq σ τ := by + unfold invalidateVmidEntry at h + rw [nd_gets_bindSc] at h + rcases h with ⟨⟨_, s1⟩, hp, hmod⟩ + simp only [pure, NonDet.pure, Set.mem_singleton_iff, Prod.mk.injEq] at hp + rcases hp with ⟨rfl, h1⟩; subst s1 + simp only [NonDet.modify, Set.mem_singleton_iff, Prod.mk.injEq] at hmod + rw [hmod.2] + exact ⟨σ.machineState.machineStateRest, σ.archState.armAsidMap, + Function.update σ.archState.armVmidTable vmid none, σ.archState.armNextVmid, rfl⟩ + +private theorem storeVmid_vmRootStateEq (asid : Asid) (hw : Vmid) + {σ τ : State Unit} {u : Unit} + (h : (u, τ) ∈ (storeVmid (Z := Unit) asid hw σ).1) : VmRootStateEq σ τ := by + unfold storeVmid at h + refine bind_vmRootStateEq (fun _ _ _ h1 => updateAsidMap_vmRootStateEq asid (some hw) h1) ?_ h + intro _ σ₁ _ τ₁ h2 + rw [nd_gets_bindSc] at h2 + rcases h2 with ⟨⟨_, s1⟩, hp, hmod⟩ + simp only [pure, NonDet.pure, Set.mem_singleton_iff, Prod.mk.injEq] at hp + rcases hp with ⟨rfl, h1⟩; subst s1 + simp only [NonDet.modify, Set.mem_singleton_iff, Prod.mk.injEq] at hmod + rw [hmod.2] + exact ⟨σ₁.machineState.machineStateRest, σ₁.archState.armAsidMap, + Function.update σ₁.archState.armVmidTable hw (some asid), σ₁.archState.armNextVmid, rfl⟩ + +private theorem findFreeVmid_vmRootStateEq {σ τ : State Unit} {v : Vmid} + (h : (v, τ) ∈ (findFreeVmid (Z := Unit) σ).1) : VmRootStateEq σ τ := by + unfold findFreeVmid at h + rw [nd_gets_bindSc, nd_gets_bindSc] at h + rcases h with ⟨⟨opt, σ₁⟩, hopt, hrest⟩ + simp only [pure, NonDet.pure, Set.mem_singleton_iff, Prod.mk.injEq] at hopt + rcases hopt with ⟨rfl, h1⟩; subst σ₁ + cases hfind : List.find? (fun a => decide (σ.archState.armVmidTable a = none)) + (uptoEnum σ.archState.armNextVmid (BitVec.allOnes vmidLen) ++ + (uptoEnum vmidMin σ.archState.armNextVmid).dropLast) with + | some vmid => + rw [hfind] at hrest + exact pure_vmRootStateEq vmid hrest + | none => + rw [hfind] at hrest + refine bind_vmRootStateEq (fun _ _ _ h1 => updateAsidMap_vmRootStateEq _ none h1) ?_ hrest + intro _ σ₂ _ τ₂ h2 + refine bind_vmRootStateEq (fun _ _ _ h3 => doMachineOp_lift_vmRootStateEq _ h3) ?_ h2 + intro _ σ₃ _ τ₃ h3 + refine bind_vmRootStateEq (fun _ _ _ h4 => invalidateVmidEntry_vmRootStateEq _ h4) ?_ h3 + intro _ σ₄ _ τ₄ h4 + rcases h4 with ⟨⟨nv, σ₅⟩, hnv, h5⟩ + simp only [pure, NonDet.pure, Set.mem_singleton_iff, Prod.mk.injEq] at hnv + rcases hnv with ⟨rfl, h5e⟩; subst σ₅ + rcases h5 with ⟨⟨_, σ₆⟩, hmod, hret⟩ + simp only [NonDet.modify, Set.mem_singleton_iff, Prod.mk.injEq] at hmod + rcases hmod with ⟨_, h6e⟩; subst σ₆ + have h6 := pure_vmRootStateEq _ hret + exact VmRootStateEq.trans + ⟨σ₄.machineState.machineStateRest, σ₄.archState.armAsidMap, σ₄.archState.armVmidTable, + _, rfl⟩ h6 + +private theorem getVmid_vmRootStateEq (asid : Asid) {σ τ : State Unit} {v : Vmid} + (h : (v, τ) ∈ (getVmid (Z := Unit) asid σ).1) : VmRootStateEq σ τ := by + unfold getVmid loadVmid at h + rw [nd_bind_applySc] at h + rcases h with ⟨⟨m, s1⟩, hm1, hm2⟩ + rw [nd_gets_bindSc] at hm1 + simp only [pure, NonDet.pure, Set.mem_singleton_iff, Prod.mk.injEq] at hm1 + rcases hm1 with ⟨rfl, h1⟩; subst s1 + cases hm : asidMap σ asid with + | some vmid => + rw [hm] at hm2 + exact pure_vmRootStateEq vmid hm2 + | none => + rw [hm] at hm2 + refine bind_vmRootStateEq (fun _ _ _ h1 => findFreeVmid_vmRootStateEq h1) ?_ hm2 + intro nv σ₁ _ τ₁ h2 + refine bind_vmRootStateEq (fun _ _ _ h3 => storeVmid_vmRootStateEq asid nv h3) ?_ h2 + intro _ σ₂ _ τ₂ h3 + exact pure_vmRootStateEq nv h3 + +private theorem armContextSwitch_vmRootStateEq (vs : ObjRef) (asid : Asid) + {σ τ : State Unit} {u : Unit} + (h : (u, τ) ∈ (armContextSwitch (Z := Unit) vs asid σ).1) : VmRootStateEq σ τ := by + unfold armContextSwitch at h + exact bind_vmRootStateEq (fun _ _ _ h1 => getVmid_vmRootStateEq asid h1) + (fun _ _ _ _ h2 => doMachineOp_lift_vmRootStateEq _ h2) h + +private theorem setGlobalUserVspace_vmRootStateEq {σ τ : State Unit} {u : Unit} + (h : (u, τ) ∈ (setGlobalUserVspace (Z := Unit) σ).1) : VmRootStateEq σ τ := by + unfold setGlobalUserVspace at h + simp only [nd_gets_bindSc] at h + exact doMachineOp_lift_vmRootStateEq _ h + +/-- **D3 side lemma (`PORTING.md`)**: every outcome of `setVmRoot` leaves every +non-`machineState` field of `State Unit` unchanged (except the VMID cache +`armAsidMap`/`armVmidTable`/`armNextVmid` inside `archState`) and preserves +`machineState.underlyingMemory`. -/ +private theorem assert_pres_state {p : Prop} {σ τ : State Unit} {u : Unit} + (h : (u, τ) ∈ (NonDet.assert p σ).1) : τ = σ := by + unfold NonDet.assert at h + by_cases hp : p + · simp [hp] at h; exact h + · simp [hp] at h + +private theorem getCap_pres_state {slot : CslotPtr} {σ τ : State Unit} {c : Cap} + (h : (c, τ) ∈ (getCap (Z := Unit) slot σ).1) : τ = σ := by + unfold getCap getObject at h + rcases h with ⟨⟨kobj, σa⟩, hga, hgb⟩ + have hσa : σa = σ := by + unfold NonDet.getsThe at hga + cases hro : readObject slot.1 σ with + | none => simp [hro, NonDet.assertOpt, NonDet.fail] at hga + | some ko => + simp only [hro, NonDet.assertOpt, NonDet.pure, Set.mem_singleton_iff, Prod.mk.injEq] at hga + exact hga.2 + subst σa + cases kobj with + | cnode sz cs => + dsimp only at hgb + rcases hgb with ⟨⟨_, s1⟩, hass, h1⟩ + have hs1 : s1 = σ := assert_pres_state hass + subst s1 + rcases h1 with ⟨⟨_, s2⟩, hp, h2⟩ + simp only [pure, NonDet.pure, Set.mem_singleton_iff, Prod.mk.injEq] at hp + rcases hp with ⟨hcs_eq, hs2⟩; subst s2; rw [hcs_eq] at h2 + cases hcs : cs slot.2 <;> simp [hcs, NonDet.assertOpt, NonDet.fail, NonDet.pure] at h2 + exact h2.2 + | tcb t => + dsimp only at hgb + rcases hgb with ⟨⟨_, s1⟩, hp, h1⟩ + simp only [pure, NonDet.pure, Set.mem_singleton_iff, Prod.mk.injEq] at hp + rcases hp with ⟨rfl, hs1⟩; subst s1 + cases hcs : tcbCnodeMap t slot.2 <;> simp [hcs, NonDet.assertOpt, NonDet.fail, NonDet.pure] at h1 + exact h1.2 + | _ => simp [NonDet.fail] at hgb + +theorem setVmRoot_preserves_state {ct : ObjRef} {σ τ : State Unit} {u : Unit} + (h : (u, τ) ∈ (setVmRoot (Z := Unit) ct σ).1) : VmRootStateEq σ τ := by + unfold setVmRoot at h + rcases h with ⟨⟨slot, σ₁⟩, hslot, h1⟩ + simp only [pure, NonDet.pure, Set.mem_singleton_iff, Prod.mk.injEq] at hslot + rcases hslot with ⟨rfl, h1e⟩; subst σ₁ + rcases h1 with ⟨⟨cap, σ₂⟩, hcap, h2⟩ + have hσ₂ : σ₂ = σ := getCap_pres_state hcap + subst σ₂ + unfold NonDetE.catch at h2 + rcases h2 with ⟨⟨res, σ₃⟩, htry, hcont⟩ + have htryEq : VmRootStateEq σ σ₃ := by + cases cap with + | archObjectCap ac => + cases ac with + | pageTableCap pt ptT md => + cases ptT with + | vsRootPtT => + cases md with + | some p => + rcases p with ⟨asid, _⟩ + dsimp only at htry + rcases htry with ⟨⟨r1, σa⟩, hfind, hrest⟩ + have hσa : σa = σ := by + unfold findVspaceForAsid NonDetE.liftE NonDetE.throwOpt at hfind + rcases hfind with ⟨⟨x1, s1⟩, hfa, hfb⟩ + rcases hfa with ⟨⟨_, s0⟩, hg0, hr0⟩ + simp only [NonDet.gets, Set.mem_singleton_iff, Prod.mk.injEq] at hg0 + rcases hg0 with ⟨rfl, hs0⟩; subst s0 + simp only [Set.mem_singleton_iff] at hr0 + cases hr0 + rcases hfb with ⟨⟨x2, s2⟩, hfc, hfd⟩ + rcases hfc with ⟨⟨_, s1'⟩, hg1, hr1⟩ + simp only [NonDet.gets, Set.mem_singleton_iff, Prod.mk.injEq] at hg1 + rcases hg1 with ⟨rfl, hs1'⟩; subst s1' + simp only [Set.mem_singleton_iff] at hr1 + have hs2 : s2 = σ := (Prod.mk.inj hr1).2 + subst s2 + cases x2 with + | error e => + simp only [NonDetE.throwError, NonDet.pure, Set.mem_singleton_iff] at hfd + exact (Prod.mk.inj hfd).2 + | ok v => + cases v with + | none => + simp only [NonDetE.throwError, NonDet.pure, Set.mem_singleton_iff] at hfd + exact (Prod.mk.inj hfd).2 + | some _ => + simp only [NonDetE.returnOk, ExceptT.pure, pure, NonDet.pure, Set.mem_singleton_iff] at hfd + exact (Prod.mk.inj hfd).2 + subst σa + cases r1 with + | error e => + simp only [NonDetE.throwError, NonDet.pure, Set.mem_singleton_iff] at hrest + cases hrest; exact VmRootStateEq.refl _ + | ok pt' => + dsimp only at hrest + rcases hrest with ⟨⟨r2, σb⟩, hwhen, hlift⟩ + have hσb : σb = σ := by + unfold NonDetE.whenE at hwhen + by_cases hpteq : pt ≠ pt' + · rw [if_pos hpteq] at hwhen + simp only [NonDetE.throwError, NonDet.pure, Set.mem_singleton_iff] at hwhen + cases hwhen; rfl + · rw [if_neg hpteq] at hwhen + simp only [NonDetE.returnOk, ExceptT.pure, pure, NonDet.pure, + Set.mem_singleton_iff] at hwhen + cases hwhen; rfl + subst σb + cases r2 with + | error e => + simp only [NonDetE.throwError, NonDet.pure, Set.mem_singleton_iff] at hlift + cases hlift; exact VmRootStateEq.refl _ + | ok _ => + unfold NonDetE.liftE at hlift + rcases hlift with ⟨⟨_, σc⟩, hsw, hret⟩ + simp only [NonDet.pure, Set.mem_singleton_iff] at hret + cases hret + exact armContextSwitch_vmRootStateEq pt asid hsw + | none => + simp only [NonDetE.throwError, NonDet.pure, Set.mem_singleton_iff] at htry + cases htry; exact VmRootStateEq.refl _ + | normalPtT => + simp only [NonDetE.throwError, NonDet.pure, Set.mem_singleton_iff] at htry + cases htry; exact VmRootStateEq.refl _ + | _ => + simp only [NonDetE.throwError, NonDet.pure, Set.mem_singleton_iff] at htry + cases htry; exact VmRootStateEq.refl _ + | _ => + simp only [NonDetE.throwError, NonDet.pure, Set.mem_singleton_iff] at htry + cases htry; exact VmRootStateEq.refl _ + cases res with + | error e => exact htryEq.trans (setGlobalUserVspace_vmRootStateEq hcont) + | ok v => exact htryEq.trans (pure_vmRootStateEq v hcont) + +/-! ## Basic deterministic `SMonad` accessors for VCPUs and TCBs -/ + +theorem getVcpu_eqA {σ : State Unit} {p : ObjRef} {v : Vcpu} + (hk : σ.kheap p = some (.archObj (.vcpu v))) : + getVcpu (Z := Unit) p σ = ({(v, σ)}, False) := by + have h : vcpusOf σ p = some v := by + simp [vcpusOf, optMap, aobjsOf, aobjOf, vcpuOf, hk] + simp [getVcpu, NonDet.getsMap, NonDet.bind, NonDet.gets, h, NonDet.assertOpt] + rfl + +theorem setVcpu_eqA {σ : State Unit} {p : ObjRef} {v v' : Vcpu} + (hk : σ.kheap p = some (.archObj (.vcpu v))) : + setVcpu (Z := Unit) p v' σ = ({((), updKSc σ p (.archObj (.vcpu v')))}, False) := + setObject_eqSc hk rfl + +theorem vcpuUpdate_eqA {σ : State Unit} {p : ObjRef} {v : Vcpu} (f : Vcpu → Vcpu) + (hk : σ.kheap p = some (.archObj (.vcpu v))) : + vcpuUpdate (Z := Unit) p f σ = ({((), updKSc σ p (.archObj (.vcpu (f v))))}, False) := by + unfold vcpuUpdate + rw [nd_bind_oneSc (getVcpu_eqA hk)] + exact setVcpu_eqA hk + +theorem fl4VcpuGet_eqA {σ : State Unit} {p : ObjRef} {v : Vcpu} (i : Nat) + (hk : σ.kheap p = some (.archObj (.vcpu v))) : + fl4VcpuGet (Z := Unit) p i σ = ({(fl4VcpuRegGet v i, σ)}, False) := by + unfold fl4VcpuGet + rw [nd_bind_oneSc (getVcpu_eqA hk)] + rfl + +theorem fl4VcpuSet_eqA {σ : State Unit} {p : ObjRef} {v : Vcpu} (i : Nat) (w : Word) + (hk : σ.kheap p = some (.archObj (.vcpu v))) : + fl4VcpuSet (Z := Unit) p i w σ = + ({((), updKSc σ p (.archObj (.vcpu (fl4VcpuRegSet i w v))))}, False) := by + unfold fl4VcpuSet + rw [nd_bind_oneSc (getVcpu_eqA hk)] + exact setVcpu_eqA hk + +theorem fl4VcpuExtUpdate_eqA {σ : State Unit} {p : ObjRef} {v : Vcpu} (f : ExtVcpu → ExtVcpu) + (hk : σ.kheap p = some (.archObj (.vcpu v))) : + fl4VcpuExtUpdate (Z := Unit) p f σ = + ({((), updKSc σ p (.archObj (.vcpu { v with vcpuExt := f v.vcpuExt })))}, False) := by + unfold fl4VcpuExtUpdate + rw [nd_bind_oneSc (getVcpu_eqA hk)] + exact setVcpu_eqA hk + +theorem fl4VcpuFlush_noneA {σ : State Unit} (hcur : σ.archState.armCurrentVcpu = none) (vc : ObjRef) : + fl4VcpuFlush (Z := Unit) vc σ = ({((), σ)}, False) := by + unfold fl4VcpuFlush + rw [nd_gets_bindSc, hcur] + simp [NonDet.when] + rfl + +theorem threadSet_eqA {σ : State Unit} {t : ObjRef} {x : Tcb} (f : Tcb → Tcb) + (hk : σ.kheap t = some (.tcb x)) : + threadSet (Z := Unit) f t σ = ({((), updKSc σ t (.tcb (f x)))}, False) := by + unfold threadSet + rw [nd_bind_oneSc (show NonDet.getsThe (getTcb (Z := Unit) t) σ = ({(x, σ)}, False) by + simp [NonDet.getsThe, getTcb, readObject, hk, NonDet.assertOpt]; rfl)] + exact setObject_eqSc hk rfl + +theorem getRunVcpu_eqA {σ : State Unit} {t : ObjRef} {x : Tcb} + (hk : σ.kheap t = some (.tcb x)) : + getRunVcpu (Z := Unit) t σ = ({(x.tcbExt.tcbRunVcpu, σ)}, False) := + threadGet_eqSc _ hk + +theorem setRunVcpu_eqA {σ : State Unit} {t : ObjRef} {x : Tcb} (v : Option ObjRef) + (hk : σ.kheap t = some (.tcb x)) : + setRunVcpu (Z := Unit) t v σ = + ({((), updKSc σ t (.tcb { x with tcbExt := { x.tcbExt with tcbRunVcpu := v } }))}, False) := + threadSet_eqA _ hk + +theorem archThreadGet_eqA {α : Type} {σ : State Unit} {t : ObjRef} {x : Tcb} (f : ArchTcb → α) + (hk : σ.kheap t = some (.tcb x)) : + archThreadGet (Z := Unit) f t σ = ({(f x.tcbArch, σ)}, False) := by + unfold archThreadGet + rw [nd_bind_oneSc (show NonDet.getsThe (getTcb (Z := Unit) t) σ = ({(x, σ)}, False) by + simp [NonDet.getsThe, getTcb, readObject, hk, NonDet.assertOpt]; rfl)] + rfl + +theorem archThreadSet_eqA {σ : State Unit} {t : ObjRef} {x : Tcb} (f : ArchTcb → ArchTcb) + (hk : σ.kheap t = some (.tcb x)) : + archThreadSet (Z := Unit) f t σ = + ({((), updKSc σ t (.tcb { x with tcbArch := f x.tcbArch }))}, False) := by + unfold archThreadSet + rw [nd_bind_oneSc (show NonDet.getsThe (getTcb (Z := Unit) t) σ = ({(x, σ)}, False) by + simp [NonDet.getsThe, getTcb, readObject, hk, NonDet.assertOpt]; rfl)] + exact setObject_eqSc hk rfl + +/-! ## `fl4DissociateVcpuTcb` and D1 conservativity of `fl4AssociateVcpuTcb` -/ + +/-- TCB after dissociating its VCPU and downgrading `SPSR_EL1` to native EL0t. -/ +def dissociateTcbA (x : Tcb) : Tcb := + let uc := x.tcbArch.tcbContext + let uc' : UserContext := ⟨uc.userFpuState, + Function.update uc.userRegs .spsrEl1 (sanitiseRegister false .spsrEl1 (uc.userRegs .spsrEl1))⟩ + { x with tcbArch := { x.tcbArch with tcbVcpu := none, tcbContext := uc' } } + +theorem dissociateVcpuTcb_eqA {σ : State Unit} {vr t : ObjRef} {v : Vcpu} {x : Tcb} + (hcur : σ.archState.armCurrentVcpu = none) + (hkv : σ.kheap vr = some (.archObj (.vcpu v))) + (hkt : σ.kheap t = some (.tcb x)) + (hvt : v.vcpuTcb = some t) (htv : x.tcbArch.tcbVcpu = some vr) : + dissociateVcpuTcb (Z := Unit) vr t σ = + ({((), updKSc (updKSc σ t (.tcb (dissociateTcbA x))) vr + (.archObj (.vcpu { v with vcpuTcb := none })))}, False) := by + have hne : vr ≠ t := by + intro heq; subst heq; rw [hkv] at hkt; cases hkt + unfold dissociateVcpuTcb + rw [nd_bind_oneSc (archThreadGet_eqA _ hkt)] + rw [nd_bind_oneSc (getVcpu_eqA hkv)] + have hass : NonDet.assert (σ := State Unit) (x.tcbArch.tcbVcpu = some vr ∧ v.vcpuTcb = some t) σ = + ({((), σ)}, False) := by + simp [NonDet.assert, htv, hvt] + rw [nd_bind_oneSc hass, nd_gets_bindSc, hcur] + have hwhen : NonDet.when (σ := State Unit) (∃ a, (none : Option (ObjRef × Bool)) = some (vr, a)) + vcpuInvalidateActive σ = ({((), σ)}, False) := by + simp [NonDet.when]; rfl + rw [nd_bind_oneSc hwhen] + rw [nd_bind_oneSc (archThreadSet_eqA _ hkt)] + let x1 : Tcb := { x with tcbArch := { x.tcbArch with tcbVcpu := none } } + have hkv1 : (updKSc σ t (.tcb x1)).kheap vr = some (.archObj (.vcpu v)) := by + rw [updKSc_other _ hne]; exact hkv + rw [nd_bind_oneSc (setVcpu_eqA hkv1)] + set σ2 := updKSc (updKSc σ t (.tcb x1)) vr (.archObj (.vcpu { v with vcpuTcb := none })) + have hkt2 : σ2.kheap t = some (.tcb x1) := by + rw [updKSc_other _ hne.symm, updKSc_same] + have hasU : asUser (Z := Unit) t (do + let sr ← getRegister .spsrEl1 + setRegister .spsrEl1 (sanitiseRegister false .spsrEl1 sr)) σ2 = + ({((), updKSc σ2 t (.tcb (dissociateTcbA x)))}, False) := by + unfold asUser + rw [nd_bind_oneSc (show NonDet.getsThe (getTcb (Z := Unit) t) σ2 = ({(x1, σ2)}, False) by + simp [NonDet.getsThe, getTcb, readObject, hkt2, NonDet.assertOpt]; rfl)] + let uc := x.tcbArch.tcbContext + let uc' : UserContext := ⟨uc.userFpuState, + Function.update uc.userRegs .spsrEl1 (sanitiseRegister false .spsrEl1 (uc.userRegs .spsrEl1))⟩ + rw [nd_bind_oneSc (rfl : (pure (archTcbContextGet x1.tcbArch) : SMonad Unit UserContext) σ2 = + ({(uc, σ2)}, False))] + have hsel : NonDet.selectF (σ := State Unit) + ((do let sr ← getRegister .spsrEl1 + setRegister .spsrEl1 (sanitiseRegister false .spsrEl1 sr)) + (archTcbContextGet x1.tcbArch)) σ2 = + ({(((), uc'), σ2)}, False) := by + have hbody : (getRegister .spsrEl1 >>= fun sr => + setRegister .spsrEl1 (sanitiseRegister false .spsrEl1 sr)) + (archTcbContextGet x1.tcbArch) = ({((), uc')}, False) := by + simp [getRegister, setRegister, archTcbContextGet, x1, uc, uc', bind, + NonDet.bind, NonDet.gets, NonDet.modify, NonDet.pure] + rw [hbody] + simp only [NonDet.selectF, Set.mem_singleton_iff] + congr 1; ext ⟨a, b⟩; simp [Prod.mk.injEq] + rw [nd_bind_oneSc hsel] + dsimp only + rw [nd_bind_oneSc (rfl : (pure { x1 with tcbArch := archTcbContextSet uc' x1.tcbArch } : SMonad Unit Tcb) σ2 = + ({({ x1 with tcbArch := archTcbContextSet uc' x1.tcbArch }, σ2)}, False))] + rw [nd_bind_oneSc (setObject_eqSc (o' := .tcb { x1 with tcbArch := archTcbContextSet uc' x1.tcbArch }) hkt2 rfl)] + rfl + rw [hasU] + congr 1; congr 1; ext + simp only [Set.mem_singleton_iff, Prod.mk.injEq, true_and] + have hk : + Function.update + (Function.update (Function.update σ.kheap t (some (.tcb x1))) vr + (some (.archObj (.vcpu { v with vcpuTcb := none })))) + t (some (.tcb (dissociateTcbA x))) = + Function.update (Function.update σ.kheap t (some (.tcb (dissociateTcbA x)))) vr + (some (.archObj (.vcpu { v with vcpuTcb := none }))) := by + funext q + by_cases hqt : q = t + · subst hqt; simp [Function.update, hne.symm] + · by_cases hqv : q = vr + · subst hqv; simp [Function.update, hne] + · simp [Function.update, hqt, hqv] + have hcomm : updKSc σ2 t (.tcb (dissociateTcbA x)) = + updKSc (updKSc σ t (.tcb (dissociateTcbA x))) vr (.archObj (.vcpu { v with vcpuTcb := none })) := by + simp only [σ2, updKSc, hk] + rw [hcomm] + +theorem fl4DissociateVcpuTcb_eqA {σ : State Unit} {vr t : ObjRef} {v : Vcpu} {x : Tcb} + (hcur : σ.archState.armCurrentVcpu = none) + (hkv : σ.kheap vr = some (.archObj (.vcpu v))) + (hkt : σ.kheap t = some (.tcb x)) + (hvt : v.vcpuTcb = some t) (htv : x.tcbArch.tcbVcpu = some vr) : + fl4DissociateVcpuTcb (Z := Unit) vr t σ = + ({((), updKSc (updKSc σ t (.tcb (dissociateTcbA x))) vr + (.archObj (.vcpu { v with vcpuTcb := none, vcpuExt := { v.vcpuExt with ownerCpu := 0 } })))}, + False) := by + unfold fl4DissociateVcpuTcb + rw [nd_bind_oneSc (dissociateVcpuTcb_eqA hcur hkv hkt hvt htv)] + have hkv' : (updKSc (updKSc σ t (.tcb (dissociateTcbA x))) vr + (.archObj (.vcpu { v with vcpuTcb := none }))).kheap vr = + some (.archObj (.vcpu { v with vcpuTcb := none })) := updKSc_same _ _ _ + rw [fl4VcpuExtUpdate_eqA _ hkv'] + rw [updKSc_updKSc] + +/-- When `v.vcpuExt.ownerCpu = 0`, `fl4DissociateVcpuTcb` equals ported `dissociateVcpuTcb`. -/ +theorem fl4DissociateVcpuTcb_eq_ported {σ : State Unit} {vr t : ObjRef} {v : Vcpu} {x : Tcb} + (hcur : σ.archState.armCurrentVcpu = none) + (hkv : σ.kheap vr = some (.archObj (.vcpu v))) + (hkt : σ.kheap t = some (.tcb x)) + (hvt : v.vcpuTcb = some t) (htv : x.tcbArch.tcbVcpu = some vr) + (hown : v.vcpuExt.ownerCpu = 0) : + fl4DissociateVcpuTcb (Z := Unit) vr t σ = dissociateVcpuTcb (Z := Unit) vr t σ := by + rw [fl4DissociateVcpuTcb_eqA hcur hkv hkt hvt htv, dissociateVcpuTcb_eqA hcur hkv hkt hvt htv] + have he : { v.vcpuExt with ownerCpu := 0 } = v.vcpuExt := by + cases ve : v.vcpuExt; simp only [ve] at hown; simp [hown] + rw [he] + +/-- **D1 conservativity (`fl4AssociateVcpuTcb`)**: off `curThread` (and when `ownerCpu = 0` on +involved VCPUs and `armCurrentVcpu = none`), `fl4AssociateVcpuTcb` agrees identically with ported +`associateVcpuTcb` across all four `(t.tcbVcpu, v.vcpuTcb)` cases. -/ +theorem fl4AssociateVcpuTcb_ne_cur {σ : State Unit} {vc t : ObjRef} {v : Vcpu} {x : Tcb} + (hne : t ≠ σ.curThread) (hcur : σ.archState.armCurrentVcpu = none) + (hkv : σ.kheap vc = some (.archObj (.vcpu v))) + (hkt : σ.kheap t = some (.tcb x)) + (hownV : v.vcpuExt.ownerCpu = 0) + (htOld : ∀ p, x.tcbArch.tcbVcpu = some p → + ∃ vp, σ.kheap p = some (.archObj (.vcpu vp)) ∧ vp.vcpuTcb = some t ∧ vp.vcpuExt.ownerCpu = 0) + (hvOld : ∀ p, v.vcpuTcb = some p → + ∃ xp, σ.kheap p = some (.tcb xp) ∧ xp.tcbArch.tcbVcpu = some vc) : + fl4AssociateVcpuTcb (Z := Unit) vc t σ = associateVcpuTcb (Z := Unit) vc t σ := by + unfold fl4AssociateVcpuTcb associateVcpuTcb + rw [nd_bind_oneSc (archThreadGet_eqA _ hkt), nd_bind_oneSc (archThreadGet_eqA _ hkt)] + have hstep1 : + ∃ σ₁, + (match x.tcbArch.tcbVcpu with + | some p => fl4DissociateVcpuTcb (Z := Unit) p t + | none => pure ()) σ = ({((), σ₁)}, False) ∧ + (match x.tcbArch.tcbVcpu with + | some p => dissociateVcpuTcb (Z := Unit) p t + | _ => pure ()) σ = ({((), σ₁)}, False) ∧ + σ₁.curThread = σ.curThread ∧ + σ₁.archState.armCurrentVcpu = none ∧ + (∃ v₁, σ₁.kheap vc = some (.archObj (.vcpu v₁)) ∧ v₁.vcpuExt.ownerCpu = 0 ∧ + (∀ p, v₁.vcpuTcb = some p → ∃ xp, σ₁.kheap p = some (.tcb xp) ∧ xp.tcbArch.tcbVcpu = some vc)) ∧ + (∃ x₁, σ₁.kheap t = some (.tcb x₁)) := by + cases htv : x.tcbArch.tcbVcpu with + | none => + exact ⟨σ, rfl, rfl, rfl, hcur, ⟨v, hkv, hownV, hvOld⟩, ⟨x, hkt⟩⟩ + | some p => + obtain ⟨vp, hkp, hvpt, hownp⟩ := htOld p htv + have hne_pt : p ≠ t := by intro h; subst h; rw [hkp] at hkt; cases hkt + have hd1 := fl4DissociateVcpuTcb_eq_ported hcur hkp hkt hvpt htv hownp + have hd2 := dissociateVcpuTcb_eqA hcur hkp hkt hvpt htv + refine ⟨updKSc (updKSc σ t (.tcb (dissociateTcbA x))) p (.archObj (.vcpu { vp with vcpuTcb := none })), + by dsimp only; rw [hd1]; exact hd2, by dsimp only; exact hd2, rfl, hcur, ?_, ?_⟩ + · by_cases hpvc : p = vc + · subst hpvc + rw [hkp] at hkv; cases hkv + refine ⟨{ v with vcpuTcb := none }, updKSc_same _ _ _, hownp, ?_⟩ + intro q hq; cases hq + · refine ⟨v, ?_, hownV, ?_⟩ + · have hne_vct : vc ≠ t := by intro h; subst h; rw [hkv] at hkt; cases hkt + rw [updKSc_other _ (Ne.symm hpvc), updKSc_other _ hne_vct]; exact hkv + · intro q hq + obtain ⟨xq, hkq, hxq⟩ := hvOld q hq + have hne_qt : q ≠ t := by + intro h; subst h + rw [hkq] at hkt; cases hkt + rw [hxq] at htv; cases htv + exact hpvc rfl + have hne_qp : q ≠ p := by intro h; subst h; rw [hkq] at hkp; cases hkp + exact ⟨xq, by rw [updKSc_other _ hne_qp, updKSc_other _ hne_qt]; exact hkq, hxq⟩ + · exact ⟨_, by rw [updKSc_other _ hne_pt.symm, updKSc_same]⟩ + obtain ⟨σ₁, hf1, hp1, hct1, hcur1, ⟨v₁, hkv1, hown1, hvOld1⟩, ⟨x₁, hkt1⟩⟩ := hstep1 + have htail1 : + (do + let v ← getVcpu vc + match v.vcpuTcb with + | some p => fl4DissociateVcpuTcb (Z := Unit) vc p + | none => pure () + archThreadSet (fun atcb => { atcb with tcbVcpu := some vc }) t + vcpuUpdate vc fun v => { v with vcpuTcb := some t }) σ₁ = + (do + let v ← getVcpu vc + match v.vcpuTcb with + | some p => dissociateVcpuTcb (Z := Unit) vc p + | _ => pure () + archThreadSet (fun x => { x with tcbVcpu := some vc }) t + setVcpu vc { v with vcpuTcb := some t } + let ct ← NonDet.gets fun s : State Unit => s.curThread + NonDet.when (t = ct) (vcpuSwitch (some vc))) σ₁ := by + rw [nd_bind_oneSc (getVcpu_eqA hkv1), nd_bind_oneSc (getVcpu_eqA hkv1)] + have hstep2 : + ∃ σ₂, + (match v₁.vcpuTcb with + | some p => fl4DissociateVcpuTcb (Z := Unit) vc p + | none => pure ()) σ₁ = ({((), σ₂)}, False) ∧ + (match v₁.vcpuTcb with + | some p => dissociateVcpuTcb (Z := Unit) vc p + | _ => pure ()) σ₁ = ({((), σ₂)}, False) ∧ + σ₂.curThread = σ.curThread ∧ + σ₂.kheap vc = some (.archObj (.vcpu { v₁ with vcpuTcb := none })) ∧ + (∃ x₂, σ₂.kheap t = some (.tcb x₂)) := by + cases hvt1 : v₁.vcpuTcb with + | none => + have hv1eq : { v₁ with vcpuTcb := none } = v₁ := by cases v₁; simp at hvt1; simp [hvt1] + exact ⟨σ₁, rfl, rfl, hct1, by rw [hv1eq]; exact hkv1, ⟨x₁, hkt1⟩⟩ + | some p => + obtain ⟨xp, hkp, hxp⟩ := hvOld1 p hvt1 + have hne_vcp : vc ≠ p := by intro h; subst h; rw [hkv1] at hkp; cases hkp + have hd1 := fl4DissociateVcpuTcb_eq_ported hcur1 hkv1 hkp hvt1 hxp hown1 + have hd2 := dissociateVcpuTcb_eqA hcur1 hkv1 hkp hvt1 hxp + refine ⟨updKSc (updKSc σ₁ p (.tcb (dissociateTcbA xp))) vc (.archObj (.vcpu { v₁ with vcpuTcb := none })), + by dsimp only; rw [hd1]; exact hd2, by dsimp only; exact hd2, hct1, updKSc_same _ _ _, ?_⟩ + by_cases hpt : p = t + · subst hpt + exact ⟨_, by rw [updKSc_other _ hne_vcp.symm, updKSc_same]⟩ + · have hne_tvc : t ≠ vc := by intro h; subst h; rw [hkt1] at hkv1; cases hkv1 + exact ⟨x₁, by rw [updKSc_other _ hne_tvc, updKSc_other _ (Ne.symm hpt)]; exact hkt1⟩ + obtain ⟨σ₂, hf2, hp2, hct2, hkv2, ⟨x₂, hkt2⟩⟩ := hstep2 + have htail2 : + (do + archThreadSet (fun atcb => { atcb with tcbVcpu := some vc }) t + vcpuUpdate (Z := Unit) vc fun v => { v with vcpuTcb := some t }) σ₂ = + (do + archThreadSet (fun x => { x with tcbVcpu := some vc }) t + setVcpu vc { v₁ with vcpuTcb := some t } + let ct ← NonDet.gets fun s : State Unit => s.curThread + NonDet.when (t = ct) (vcpuSwitch (some vc))) σ₂ := by + rw [nd_bind_oneSc (archThreadSet_eqA _ hkt2), nd_bind_oneSc (archThreadSet_eqA _ hkt2)] + have hne_vct : vc ≠ t := by intro h; subst h; rw [hkv2] at hkt2; cases hkt2 + have hkv3 : (updKSc σ₂ t (.tcb { x₂ with tcbArch := { x₂.tcbArch with tcbVcpu := some vc } })).kheap vc = + some (.archObj (.vcpu { v₁ with vcpuTcb := none })) := by + rw [updKSc_other _ hne_vct]; exact hkv2 + rw [vcpuUpdate_eqA _ hkv3] + rw [nd_bind_oneSc (setVcpu_eqA hkv3)] + rw [nd_gets_bindSc] + have hct3 : (updKSc (updKSc σ₂ t (.tcb { x₂ with tcbArch := { x₂.tcbArch with tcbVcpu := some vc } })) + vc (.archObj (.vcpu { v₁ with vcpuTcb := some t }))).curThread = σ.curThread := hct2 + rw [hct3, NonDet.when, ite_eq_right hne] + rfl + cases hvt : v₁.vcpuTcb with + | none => + rw [hvt] at hf2 hp2; dsimp only at hf2 hp2 ⊢ + have hσ₂ : σ₁ = σ₂ := by + have hmem : ((), σ₁) ∈ ({((), σ₂)} : Set (Unit × State Unit)) := by + have h1 : ({((), σ₁)} : Set (Unit × State Unit)) = {((), σ₂)} := congrArg Prod.fst hf2 + rw [← h1]; exact Set.mem_singleton _ + exact (Prod.mk.inj hmem).2 + subst hσ₂; exact htail2 + | some p => + rw [hvt] at hf2 hp2; dsimp only at hf2 hp2 ⊢ + rw [nd_bind_oneSc hf2, nd_bind_oneSc hp2]; exact htail2 + cases htv : x.tcbArch.tcbVcpu with + | none => + rw [htv] at hf1 hp1; dsimp only at hf1 hp1 ⊢ + have hσ₁ : σ = σ₁ := by + have hmem : ((), σ) ∈ ({((), σ₁)} : Set (Unit × State Unit)) := by + have h1 : ({((), σ)} : Set (Unit × State Unit)) = {((), σ₁)} := congrArg Prod.fst hf1 + rw [← h1]; exact Set.mem_singleton _ + exact (Prod.mk.inj hmem).2 + subst hσ₁; exact htail1 + | some p => + rw [htv] at hf1 hp1; dsimp only at hf1 hp1 ⊢ + rw [nd_bind_oneSc hf1, nd_bind_oneSc hp1]; exact htail1 + +/-! ## `fl4CompleteRun` curThread preservation & D3 conservativity -/ + +private theorem mapMx_pres_curThread {α : Type} {f : α → SMonad Unit Unit} + (hf : ∀ a σ τ, ((), τ) ∈ (f a σ).1 → τ.curThread = σ.curThread) : + ∀ (xs : List α) (σ τ : State Unit), + ((), τ) ∈ (NonDet.mapMx f xs σ).1 → τ.curThread = σ.curThread + | [], σ, τ, h => by + simp only [NonDet.mapMx, pure, NonDet.pure, Set.mem_singleton_iff, Prod.mk.injEq] at h + rw [h.2] + | a :: xs, σ, τ, h => by + simp only [NonDet.mapMx] at h + rcases h with ⟨⟨_, σ₁⟩, h1, h2⟩ + exact (mapMx_pres_curThread hf xs σ₁ τ h2).trans (hf a σ σ₁ h1) + +private theorem getsThe_pres_state {α : Type} {f : State Unit → Option α} {σ τ : State Unit} {a : α} + (h : (a, τ) ∈ (NonDet.getsThe f σ).1) : τ = σ := by + unfold NonDet.getsThe at h + cases hf : f σ <;> simp [hf, NonDet.assertOpt, NonDet.fail, NonDet.pure] at h + exact h.2 + +private theorem setObject_pres_curThread {p : ObjRef} {o : KernelObject} {σ τ : State Unit} + (h : ((), τ) ∈ (setObject (Z := Unit) p o σ).1) : τ.curThread = σ.curThread := by + unfold setObject getObject at h + rcases h with ⟨⟨_, σ₁⟩, h1, h2⟩ + have hσ₁ : σ₁ = σ := getsThe_pres_state h1 + subst σ₁ + rcases h2 with ⟨⟨_, σ₂⟩, ha, h3⟩ + have hσ₂ : σ₂ = σ := assert_pres_state ha + subst σ₂ + rcases h3 with ⟨⟨_, σ₃⟩, hg, hput⟩ + simp only [NonDet.get, Set.mem_singleton_iff, Prod.mk.injEq] at hg + rcases hg with ⟨rfl, hs3⟩; subst σ₃ + simp only [NonDet.put, Set.mem_singleton_iff, Prod.mk.injEq] at hput + rw [hput.2] + +private theorem storeWordOffs_pres_curThread {p : ObjRef} {offs : Nat} {v : Word} {σ τ : State Unit} + (h : ((), τ) ∈ (storeWordOffs (Z := Unit) p offs v σ).1) : τ.curThread = σ.curThread := by + unfold storeWordOffs at h + rcases h with ⟨⟨s_, σ₁⟩, hg, h1⟩ + simp only [NonDet.get, Set.mem_singleton_iff, Prod.mk.injEq] at hg + rcases hg with ⟨hs_, hs1⟩; subst s_; subst σ₁ + rcases h1 with ⟨⟨_, σ₂⟩, ha, h2⟩ + have hσ₂ : σ₂ = σ := assert_pres_state ha + subst σ₂ + unfold doMachineOp at h2 + rcases h2 with ⟨⟨_, s0⟩, hg0, h2a⟩ + simp only [NonDet.gets, Set.mem_singleton_iff, Prod.mk.injEq] at hg0 + rcases hg0 with ⟨_, hs0⟩; subst s0 + rcases h2a with ⟨⟨_, s1⟩, hsf, h2b⟩ + simp only [NonDet.selectF, Set.mem_setOf_eq] at hsf + rcases hsf with ⟨_, hs1'⟩; subst s1 + rcases h2b with ⟨⟨_, s2⟩, hmod, hret⟩ + simp only [NonDet.modify, Set.mem_singleton_iff, Prod.mk.injEq] at hmod + rcases hmod with ⟨_, hs2⟩; subst s2 + simp only [pure, NonDet.pure, Set.mem_singleton_iff, Prod.mk.injEq] at hret + rw [hret.2] + +private theorem asUser_pres_curThread {α : Type} {t : ObjRef} {f : UserMonad α} + {σ τ : State Unit} {a : α} + (h : (a, τ) ∈ (asUser (Z := Unit) t f σ).1) : τ.curThread = σ.curThread := by + unfold asUser at h + rcases h with ⟨⟨_, σ₁⟩, h1, h2⟩ + have hσ₁ : σ₁ = σ := getsThe_pres_state h1 + subst σ₁ + rcases h2 with ⟨⟨_, σ₂⟩, hpure, h2'⟩ + simp only [pure, NonDet.pure, Set.mem_singleton_iff, Prod.mk.injEq] at hpure + rcases hpure with ⟨_, hσ₂⟩; subst σ₂ + rcases h2' with ⟨⟨_, σ₃⟩, hsel, h3⟩ + simp only [NonDet.selectF, Set.mem_setOf_eq] at hsel + rcases hsel with ⟨_, hσ₃⟩; subst σ₃ + rcases h3 with ⟨⟨_, σ₄⟩, hp2, h4⟩ + simp only [pure, NonDet.pure, Set.mem_singleton_iff, Prod.mk.injEq] at hp2 + rcases hp2 with ⟨_, hσ₄⟩; subst σ₄ + rcases h4 with ⟨⟨_, σ₅⟩, hset, hret⟩ + simp only [pure, NonDet.pure, Set.mem_singleton_iff, Prod.mk.injEq] at hret + rw [hret.2] + exact setObject_pres_curThread hset + +private theorem getVcpu_pres_curThread {p : ObjRef} {σ τ : State Unit} {v : Vcpu} + (h : (v, τ) ∈ (getVcpu (Z := Unit) p σ).1) : τ.curThread = σ.curThread := by + unfold getVcpu NonDet.getsMap at h + simp only [NonDet.bind, NonDet.gets, Set.mem_setOf_eq, Set.mem_singleton_iff] at h + rcases h with ⟨⟨_, s0⟩, hg, hopt⟩ + cases hg + cases hv : vcpusOf σ p <;> simp [hv, NonDet.assertOpt, NonDet.fail, NonDet.pure] at hopt + rw [hopt.2] + +private theorem fl4ReadStateToFrame_pres_curThread {vc frame : ObjRef} {m : Word} + {σ τ : State Unit} (h : ((), τ) ∈ (fl4ReadStateToFrame (Z := Unit) vc frame m σ).1) : + τ.curThread = σ.curThread := by + have hbind : ∀ {α β : Type} {m₁ : SMonad Unit α} {f : α → SMonad Unit β} + (hm : ∀ s a t, (a, t) ∈ (m₁ s).1 → t.curThread = s.curThread) + (hf : ∀ a s b t, (b, t) ∈ (f a s).1 → t.curThread = s.curThread) + {s t : State Unit} {b : β}, (b, t) ∈ ((m₁ >>= f) s).1 → t.curThread = s.curThread := by + intro _ _ _ _ hm hf s t b hb + rcases hb with ⟨⟨a, s₁⟩, h1, h2⟩ + exact (hf a s₁ b t h2).trans (hm s a s₁ h1) + have hwhen : ∀ {p : Prop} [Decidable p] {m₁ : SMonad Unit Unit}, + (∀ s t, ((), t) ∈ (m₁ s).1 → t.curThread = s.curThread) → + ∀ {s t : State Unit}, ((), t) ∈ (NonDet.when p m₁ s).1 → t.curThread = s.curThread := by + intro p _ m₁ hm s t ht + unfold NonDet.when at ht + split_ifs at ht + · exact hm s t ht + · simp only [NonDet.pure, Set.mem_singleton_iff, Prod.mk.injEq] at ht; rw [ht.2] + have hget : ∀ (r : Nat) (s : State Unit) (w : Word) (t : State Unit), + (w, t) ∈ (fl4VcpuGet (Z := Unit) vc r s).1 → t.curThread = s.curThread := by + intro r s w t hw + unfold fl4VcpuGet at hw + exact hbind (fun _ _ _ h1 => getVcpu_pres_curThread h1) (fun _ _ _ _ h2 => by + simp only [pure, NonDet.pure, Set.mem_singleton_iff, Prod.mk.injEq] at h2; rw [h2.2]) hw + have hcopy : ∀ (rs : List Nat) (s t : State Unit), + ((), t) ∈ (copyRegsToFrame (Z := Unit) vc frame rs s).1 → t.curThread = s.curThread := by + intro rs s t ht + unfold copyRegsToFrame at ht + exact mapMx_pres_curThread (fun r _ _ h1 => + hbind (hget r) (fun _ _ _ _ h2 => storeWordOffs_pres_curThread h2) h1) rs s t ht + unfold fl4ReadStateToFrame at h + refine hbind (fun _ _ _ h1 => hwhen (fun _ _ h2 => + hbind (fun _ _ _ h3 => hcopy _ _ _ h3) (fun _ _ _ _ h4 => hcopy _ _ _ h4) h2) h1) ?_ h + intro _ s1 _ t1 h1 + refine hbind (fun _ _ _ h2 => hwhen (fun _ _ h3 => + hbind (fun _ _ _ h4 => hcopy _ _ _ h4) + (fun _ _ _ _ h5 => hbind (fun _ _ _ h6 => hcopy _ _ _ h6) (fun _ _ _ _ h7 => hcopy _ _ _ h7) h5) h3) h2) ?_ h1 + intro _ s2 _ t2 h2 + refine hbind (fun _ _ _ h3 => hwhen (fun _ _ h4 => + hbind (fun _ _ _ h5 => hcopy _ _ _ h5) + (fun _ _ _ _ h6 => hbind (fun _ _ _ h7 => getVcpu_pres_curThread h7) + (fun _ _ _ _ h8 => storeWordOffs_pres_curThread h8) h6) h4) h3) ?_ h2 + intro _ s3 u3 t3 h3 + cases u3 + have hGpr : ∀ (_ : Unit) (s4 : State Unit) (u4 : Unit) (t4 : State Unit), + (u4, t4) ∈ (NonDet.when (m &&& smGprs ≠ 0) (do + let v ← getVcpu vc + NonDet.mapMx (fun (i, r) => do + let w ← match v.vcpuTcb with + | some t => asUser t (getRegister r) + | none => pure 0 + storeWordOffs frame (64 + i) w) (List.range fl4UserRegs.length |>.zip fl4UserRegs)) s4).1 → + t4.curThread = s4.curThread := by + intro _ s4 u4 t4 h4; cases u4 + exact hwhen (fun _ _ h5 => + hbind (fun _ _ _ h6 => getVcpu_pres_curThread h6) (fun v _ _ _ h7 => + mapMx_pres_curThread (fun ⟨i, r⟩ _ _ h8 => by + cases htv : v.vcpuTcb with + | some t => + simp only [htv] at h8 + exact hbind (fun _ _ _ h9 => asUser_pres_curThread h9) + (fun _ _ _ _ h9 => storeWordOffs_pres_curThread h9) h8 + | none => + simp only [htv] at h8 + exact hbind (fun _ _ _ h9 => by + simp only [pure, NonDet.pure, Set.mem_singleton_iff, Prod.mk.injEq] at h9; rw [h9.2]) + (fun _ _ _ _ h9 => storeWordOffs_pres_curThread h9) h8) _ _ _ h7) h5) h4 + by_cases hvg : m &&& smVgic ≠ 0 + · rw [if_pos hvg] at h3 + refine hbind (fun _ _ _ ha => storeWordOffs_pres_curThread ha) ?_ h3 + intro _ _ _ _ ha + refine hbind (fun _ _ _ hb => hcopy _ _ _ hb) ?_ ha + intro _ _ _ _ hb + refine hbind (fun _ _ _ hc => mapMx_pres_curThread (fun r _ _ h8 => by + by_cases hg : fl4GicV3 = true + · rw [if_pos hg] at h8 + exact hbind (hget r) (fun _ _ _ _ h9 => storeWordOffs_pres_curThread h9) h8 + · rw [if_neg hg] at h8 + exact hbind (fun _ _ _ h9 => congrArg (fun s : State Unit => s.curThread) (Prod.mk.inj h9).2) + (fun _ _ _ _ h9 => storeWordOffs_pres_curThread h9) h8) _ _ _ hc) ?_ hb + intro _ _ _ _ hc + refine hbind (fun _ _ _ hd => mapMx_pres_curThread (fun i _ _ h9 => by + by_cases hi : i < fl4NumLrs + · rw [if_pos hi] at h9 + exact hbind (hget (vrVgicLr0 + i)) (fun _ _ _ _ h10 => storeWordOffs_pres_curThread h10) h9 + · rw [if_neg hi] at h9 + exact hbind (fun _ _ _ h10 => congrArg (fun s : State Unit => s.curThread) (Prod.mk.inj h10).2) + (fun _ _ _ _ h10 => storeWordOffs_pres_curThread h10) h9) _ _ _ hd) hGpr hc + · rw [if_neg hvg] at h3 + rcases h3 with ⟨⟨u4, s4⟩, h4, h5⟩ + cases u4 + have h4eq : s4.curThread = s3.curThread := + hwhen (fun _ _ h5' => + hbind (fun _ _ _ h6 => hcopy _ _ _ h6) (fun _ _ _ _ h7 => + mapMx_pres_curThread (fun i _ _ h8 => by + by_cases hi : i < fl4NumLrs + · rw [if_pos hi] at h8 + exact hbind (hget (vrVgicLr0 + i)) (fun _ _ _ _ h9 => storeWordOffs_pres_curThread h9) h8 + · rw [if_neg hi] at h8 + exact hbind (fun _ _ _ h9 => congrArg (fun s : State Unit => s.curThread) (Prod.mk.inj h9).2) + (fun _ _ _ _ h9 => storeWordOffs_pres_curThread h9) h8) _ _ _ h7) h5') h4 + exact (hGpr () s4 () t3 h5).trans h4eq + +private theorem setThreadState_pres_curThread {t : ObjRef} {st : ThreadState} {σ τ : State Unit} + (h : ((), τ) ∈ (setThreadState (Z := Unit) t st σ).1) : τ.curThread = σ.curThread := by + unfold setThreadState setThreadStateAct threadSet at h + rcases h with ⟨⟨_, σ₁⟩, h1, h2⟩ + have hσ₁ : σ₁.curThread = σ.curThread := by + rcases h1 with ⟨⟨_, σa⟩, hget, hset⟩ + have hσa : σa = σ := getsThe_pres_state hget + subst σa + exact setObject_pres_curThread hset + rw [← hσ₁] + rcases h2 with ⟨⟨_, σ₂⟩, hget2, ⟨⟨_, σ₃⟩, hget3, ⟨⟨_, σ₄⟩, hget4, hwhen⟩⟩⟩ + simp only [NonDet.gets, Set.mem_singleton_iff, Prod.mk.injEq] at hget2 hget3 hget4 + rcases hget2 with ⟨rfl, h2e⟩; subst σ₂ + rcases hget3 with ⟨rfl, h3e⟩; subst σ₃ + rcases hget4 with ⟨rfl, h4e⟩; subst σ₄ + unfold NonDet.when setSchedulerAction at hwhen + dsimp only at hwhen + by_cases hc : t = σ₁.curThread ∧ σ₁.schedulerAction = .resumeCurThread ∧ ¬(schedulable t σ₁ = true) + · rw [if_pos hc] at hwhen + simp only [NonDet.modify, Set.mem_singleton_iff, Prod.mk.injEq] at hwhen + rw [hwhen.2] + · rw [if_neg hc] at hwhen + simp only [NonDet.pure, Set.mem_singleton_iff, Prod.mk.injEq] at hwhen + rw [hwhen.2] + +theorem fl4CompleteRun_preserves_curThread {ct : ObjRef} {stop : RunStop} {w1 w2 w3 : Word} + {σ τ : State Unit} (hcur : σ.archState.armCurrentVcpu = none) + (h : ((), τ) ∈ (fl4CompleteRun (Z := Unit) ct stop w1 w2 w3 σ).1) : + τ.curThread = σ.curThread := by + have hbind : ∀ {α β : Type} {m₁ : SMonad Unit α} {f : α → SMonad Unit β} + (hm : ∀ s a t, (a, t) ∈ (m₁ s).1 → t.curThread = s.curThread) + (hf : ∀ a s b t, (b, t) ∈ (f a s).1 → t.curThread = s.curThread) + {s t : State Unit} {b : β}, (b, t) ∈ ((m₁ >>= f) s).1 → t.curThread = s.curThread := by + intro _ _ _ _ hm hf s t b hb + rcases hb with ⟨⟨a, s₁⟩, h1, h2⟩ + exact (hf a s₁ b t h2).trans (hm s a s₁ h1) + unfold fl4CompleteRun at h + rcases h with ⟨⟨vcOpt, σ₁⟩, h1, h2⟩ + have hσ₁ : σ₁ = σ := by + unfold getRunVcpu threadGet at h1 + rcases h1 with ⟨⟨_, s₁⟩, ha, hb⟩ + have hs₁ : s₁ = σ := getsThe_pres_state ha + subst s₁ + simp only [NonDet.pure, Set.mem_singleton_iff] at hb + exact (Prod.mk.inj hb).2 + subst σ₁ + cases vcOpt with + | none => + simp only [pure, NonDet.pure, Set.mem_singleton_iff, Prod.mk.injEq] at h2; rw [h2.2] + | some vc => + dsimp only at h2 + rw [nd_bind_oneSc (fl4VcpuFlush_noneA hcur vc)] at h2 + refine hbind (fun _ _ _ ha => getVcpu_pres_curThread ha) ?_ h2 + intro v s1 _ t1 h3 + refine hbind (fun s _ t ha => ?_) ?_ h3 + · unfold NonDet.when at ha + split_ifs at ha + · rcases ha with ⟨⟨buf, sa⟩, hcap, hrest⟩ + have hsa : sa = s := getCap_pres_state hcap + subst sa + cases hfr : frameRightsDev buf with + | none => + simp only [hfr, NonDet.pure, Set.mem_singleton_iff] at hrest + rw [(Prod.mk.inj hrest).2] + | some rd => + cases rd with + | mk R dev => + cases dev with + | true => + simp only [hfr, NonDet.pure, Set.mem_singleton_iff] at hrest + rw [(Prod.mk.inj hrest).2] + | false => + cases hsw : stateWindow buf v.vcpuExt.runOffset with + | none => + simp only [hfr, hsw, NonDet.pure, Set.mem_singleton_iff] at hrest + rw [(Prod.mk.inj hrest).2] + | some w => + cases w with + | none => + simp only [hfr, hsw, NonDet.pure, Set.mem_singleton_iff] at hrest + rw [(Prod.mk.inj hrest).2] + | some frame => + simp only [hfr, hsw] at hrest + by_cases hrw : R = vmReadWrite + · simp only [hrw, ite_true] at hrest + exact fl4ReadStateToFrame_pres_curThread hrest + · simp only [hrw, ite_false, NonDet.pure, Set.mem_singleton_iff] at hrest + rw [(Prod.mk.inj hrest).2] + · simp only [NonDet.pure, Set.mem_singleton_iff, Prod.mk.injEq] at ha; rw [ha.2] + · intro _ s2 _ t2 h4 + refine hbind (fun s _ t ha => ?_) ?_ h4 + · unfold fl4VcpuExtUpdate setVcpu at ha + exact hbind (fun _ _ _ hb => getVcpu_pres_curThread hb) + (fun _ _ _ _ hb => setObject_pres_curThread hb) ha + · intro _ s3 _ t3 h5 + refine hbind (fun s _ t ha => ?_) ?_ h5 + · unfold setRunVcpu threadSet at ha + rcases ha with ⟨⟨_, sa⟩, hga, hset⟩ + have hsa : sa = s := getsThe_pres_state hga + subst sa + exact setObject_pres_curThread hset + · intro _ s4 _ t4 h6 + refine hbind (fun _ _ _ ha => asUser_pres_curThread ha) ?_ h6 + intro _ s5 _ t5 h7 + refine hbind (fun _ _ _ ha => asUser_pres_curThread ha) ?_ h7 + intro _ s6 _ t6 h8 + refine hbind (fun _ _ _ ha => asUser_pres_curThread ha) ?_ h8 + intro _ s7 _ t7 h9 + refine hbind (fun _ _ _ ha => asUser_pres_curThread ha) ?_ h9 + intro _ s8 _ t8 h10 + exact setThreadState_pres_curThread h10 + +/-- **D3 conservativity (`fl4CompleteRun_ne_cur`)**: when `ct ≠ σ.curThread` (and +`armCurrentVcpu = none`), omitting the trailing `setVmRoot ct` is exact. -/ +theorem fl4CompleteRun_ne_cur {ct : ObjRef} {stop : RunStop} {w1 w2 w3 : Word} + {σ : State Unit} (hne : ct ≠ σ.curThread) (hcur : σ.archState.armCurrentVcpu = none) : + ((fl4CompleteRun (Z := Unit) ct stop w1 w2 w3 >>= fun _ => do + let cur ← NonDet.gets fun s : State Unit => s.curThread + NonDet.when (ct = cur) <| setVmRoot ct) σ) = + fl4CompleteRun (Z := Unit) ct stop w1 w2 w3 σ := by + have htail : ∀ τ, ((), τ) ∈ (fl4CompleteRun (Z := Unit) ct stop w1 w2 w3 σ).1 → + (do let cur ← NonDet.gets fun s : State Unit => s.curThread + NonDet.when (ct = cur) <| setVmRoot ct) τ = ({((), τ)}, False) := by + intro τ hτ + have hct := fl4CompleteRun_preserves_curThread hcur hτ + rw [nd_gets_bindSc, hct, NonDet.when, if_neg hne] + rfl + rw [nd_bind_applySc] + ext p + · simp only [Set.mem_setOf_eq] + constructor + · rintro ⟨⟨⟨⟩, τ⟩, hτ, hp⟩ + rw [htail τ hτ] at hp + simp only [Set.mem_singleton_iff] at hp + rw [hp]; exact hτ + · intro hp + rcases p with ⟨⟨⟩, τ⟩ + refine ⟨((), τ), hp, ?_⟩ + rw [htail τ hp] + exact Set.mem_singleton _ + · simp only [or_iff_left_iff_imp] + rintro ⟨⟨⟨⟩, τ⟩, hτ, hf⟩ + rw [htail τ hτ] at hf + exact False.elim hf + +/-! ## Deterministic single-step equations for lifecycle & invocations -/ + +theorem fl4CancelRun_noneA {σ : State Unit} {t : ObjRef} {x : Tcb} + (hk : σ.kheap t = some (.tcb x)) (hnone : x.tcbExt.tcbRunVcpu = none) : + fl4CancelRun (Z := Unit) t σ = ({((), σ)}, False) := by + unfold fl4CancelRun + rw [nd_bind_oneSc (getRunVcpu_eqA hk), hnone] + rfl + +theorem fl4CancelRun_someA {σ : State Unit} {t vc : ObjRef} {x : Tcb} {v : Vcpu} + (hkt : σ.kheap t = some (.tcb x)) (hkv : σ.kheap vc = some (.archObj (.vcpu v))) + (hsome : x.tcbExt.tcbRunVcpu = some vc) : + fl4CancelRun (Z := Unit) t σ = + ({((), updKSc (updKSc σ vc (.archObj (.vcpu { v with vcpuExt := { v.vcpuExt with runCaller := none } }))) + t (.tcb { x with tcbExt := { x.tcbExt with tcbRunVcpu := none } }))}, False) := by + have hne : t ≠ vc := by intro h; subst h; rw [hkt] at hkv; cases hkv + unfold fl4CancelRun + rw [nd_bind_oneSc (getRunVcpu_eqA hkt), hsome] + dsimp only + rw [nd_bind_oneSc (fl4VcpuExtUpdate_eqA _ hkv)] + have hkt1 : (updKSc σ vc (.archObj (.vcpu { v with vcpuExt := { v.vcpuExt with runCaller := none } }))).kheap t = + some (.tcb x) := by + rw [updKSc_other _ hne]; exact hkt + exact setRunVcpu_eqA none hkt1 + +theorem fl4VcpuEndRun_noneA {σ : State Unit} {vc : ObjRef} {v : Vcpu} + (hkv : σ.kheap vc = some (.archObj (.vcpu v))) (hnone : v.vcpuExt.runCaller = none) : + fl4VcpuEndRun (Z := Unit) vc σ = ({((), σ)}, False) := by + unfold fl4VcpuEndRun + rw [nd_bind_oneSc (getVcpu_eqA hkv), hnone] + rfl + +theorem fl4VcpuEndRun_runningVmA {σ : State Unit} {vc c : ObjRef} {v : Vcpu} {x : Tcb} + (hkv : σ.kheap vc = some (.archObj (.vcpu v))) (hkc : σ.kheap c = some (.tcb x)) + (hsome : v.vcpuExt.runCaller = some c) (hvm : x.tcbState = .ext .runningVM) : + fl4VcpuEndRun (Z := Unit) vc σ = fl4CompleteRun (Z := Unit) c .interrupt 0 0 0 σ := by + unfold fl4VcpuEndRun getThreadState + rw [nd_bind_oneSc (getVcpu_eqA hkv), hsome] + dsimp only + rw [nd_bind_oneSc (threadGet_eqSc _ hkc), hvm, if_pos rfl] + +theorem fl4VcpuEndRun_staleA {σ : State Unit} {vc c : ObjRef} {v : Vcpu} {x : Tcb} + (hkv : σ.kheap vc = some (.archObj (.vcpu v))) (hkc : σ.kheap c = some (.tcb x)) + (hsome : v.vcpuExt.runCaller = some c) (hnot : x.tcbState ≠ .ext .runningVM) : + fl4VcpuEndRun (Z := Unit) vc σ = + ({((), updKSc (updKSc σ vc (.archObj (.vcpu { v with vcpuExt := { v.vcpuExt with runCaller := none } }))) + c (.tcb { x with tcbExt := { x.tcbExt with tcbRunVcpu := none } }))}, False) := by + have hne : c ≠ vc := by intro h; subst h; rw [hkc] at hkv; cases hkv + unfold fl4VcpuEndRun getThreadState + rw [nd_bind_oneSc (getVcpu_eqA hkv), hsome] + dsimp only + rw [nd_bind_oneSc (threadGet_eqSc _ hkc), if_neg hnot] + rw [nd_bind_oneSc (fl4VcpuExtUpdate_eqA _ hkv)] + have hkc1 : (updKSc σ vc (.archObj (.vcpu { v with vcpuExt := { v.vcpuExt with runCaller := none } }))).kheap c = + some (.tcb x) := by + rw [updKSc_other _ hne]; exact hkc + exact setRunVcpu_eqA none hkc1 + +theorem fl4VcpuCheck_notVmA {σ : State Unit} {t : ObjRef} {x : Tcb} + (hk : σ.kheap t = some (.tcb x)) (hnot : x.tcbState ≠ .ext .runningVM) : + fl4VcpuCheck (Z := Unit) t σ = ({((), σ)}, False) := by + unfold fl4VcpuCheck getThreadState + rw [nd_bind_oneSc (threadGet_eqSc _ hk), NonDet.when, if_neg hnot] + rfl + +theorem fl4VcpuCheck_noNtfnA {σ : State Unit} {t vc : ObjRef} {x : Tcb} + (hk : σ.kheap t = some (.tcb x)) (hvm : x.tcbState = .ext .runningVM) + (hrv : x.tcbExt.tcbRunVcpu = some vc) (hbn : x.tcbBoundNotification = none) : + fl4VcpuCheck (Z := Unit) t σ = ({((), σ)}, False) := by + unfold fl4VcpuCheck getThreadState getRunVcpu getTcbObjRef + rw [nd_bind_oneSc (threadGet_eqSc _ hk), hvm, NonDet.when, if_pos rfl] + rw [nd_bind_oneSc (threadGet_eqSc _ hk), hrv] + dsimp only + rw [nd_bind_oneSc (threadGet_eqSc _ hk), hbn] + rfl + +theorem fl4VcpuCheck_notActiveA {σ : State Unit} {t vc n : ObjRef} {x : Tcb} {ntfn : Notification} + (hk : σ.kheap t = some (.tcb x)) (hvm : x.tcbState = .ext .runningVM) + (hrv : x.tcbExt.tcbRunVcpu = some vc) (hbn : x.tcbBoundNotification = some n) + (hkn : σ.kheap n = some (.notification ntfn)) + (hna : ∀ b, ntfn.ntfnObj ≠ .activeNtfn b) : + fl4VcpuCheck (Z := Unit) t σ = ({((), σ)}, False) := by + unfold fl4VcpuCheck getThreadState getRunVcpu getTcbObjRef getNotification + rw [nd_bind_oneSc (threadGet_eqSc _ hk), hvm, NonDet.when, if_pos rfl] + rw [nd_bind_oneSc (threadGet_eqSc _ hk), hrv] + dsimp only + rw [nd_bind_oneSc (threadGet_eqSc _ hk), hbn] + dsimp only + have hgn : getNotification (Z := Unit) n σ = ({(ntfn, σ)}, False) := by + unfold getNotification getSimpleKo getObject + rw [nd_bind_oneSc (show NonDet.getsThe (readObject (Z := Unit) n) σ = ({(.notification ntfn, σ)}, False) by + simp [NonDet.getsThe, readObject, hkn, NonDet.assertOpt]; rfl)] + rw [nd_bind_oneSc (show NonDet.assert (σ := State Unit) + (isSimpleType (.notification ntfn)) σ = ({((), σ)}, False) by + simp [NonDet.assert, isSimpleType, aType])] + have hthe : (The fun y : Notification => y = ntfn) = ntfn := by + unfold The + have hex : ∃! x : Notification, x = ntfn := ⟨ntfn, rfl, fun _ hy => hy⟩ + rw [dif_pos hex] + exact (Classical.choose_spec hex).1 + simp [partialInv, hthe] + rw [nd_bind_oneSc hgn] + cases hobj : ntfn.ntfnObj with + | activeNtfn b => exact False.elim (hna b hobj) + | idleNtfn => rfl + | waitingNtfn _ => rfl + +theorem fl4PerformVcpuInv_readReg_eqA {σ : State Unit} {vc : ObjRef} {v : Vcpu} (field : Nat) + (hcur : σ.archState.armCurrentVcpu = none) (hkv : σ.kheap vc = some (.archObj (.vcpu v))) : + fl4PerformVcpuInv (Z := Unit) (.readReg vc field) σ = + ({([if field = vrVgicVtr then abiVgicVtr + else if ¬fl4GicV3 ∧ fl4IsVgicAp1r field then 0 + else fl4VcpuRegGet v field], σ)}, False) := by + unfold fl4PerformVcpuInv; dsimp only + rw [nd_bind_oneSc (fl4VcpuFlush_noneA hcur vc)] + split_ifs + · rw [nd_bind_oneSc (rfl : (pure abiVgicVtr : SMonad Unit Word) σ = ({(abiVgicVtr, σ)}, False))]; rfl + · rw [nd_bind_oneSc (rfl : (pure 0 : SMonad Unit Word) σ = ({(0, σ)}, False))]; rfl + · rw [nd_bind_oneSc (fl4VcpuGet_eqA field hkv)]; rfl + +theorem fl4PerformVcpuInv_writeReg_eqA {σ : State Unit} {vc : ObjRef} {v : Vcpu} + (field : Nat) (val : Word) + (hcur : σ.archState.armCurrentVcpu = none) (hkv : σ.kheap vc = some (.archObj (.vcpu v))) : + fl4PerformVcpuInv (Z := Unit) (.writeReg vc field val) σ = + if ¬fl4GicV3 ∧ fl4IsVgicAp1r field then ({([], σ)}, False) + else + let v1 := fl4VcpuRegSet field val v + let v2 := if field ≥ vrVgicLr0 ∨ field = vrVgicHcr ∨ field = vrVgicVmcr + then syncVgicStatus v1 fl4NumLrs fl4GicV3 else v1 + ({([], updKSc σ vc (.archObj (.vcpu v2)))}, False) := by + unfold fl4PerformVcpuInv; dsimp only + rw [nd_bind_oneSc (fl4VcpuFlush_noneA hcur vc)] + by_cases hap : ¬fl4GicV3 = true ∧ fl4IsVgicAp1r field = true + · simp only [hap, ite_true] + rw [nd_bind_oneSc (show NonDet.when (σ := State Unit) (¬(¬fl4GicV3 = true ∧ fl4IsVgicAp1r field = true)) _ σ = + ({((), σ)}, False) by rw [NonDet.when, if_neg (not_not.mpr hap)]; rfl)] + rfl + · simp only [hap, ite_false] + rw [NonDet.when, if_pos hap] + have hinner : (do + fl4VcpuSet (Z := Unit) vc field val + NonDet.when (field ≥ vrVgicLr0 ∨ field = vrVgicHcr ∨ field = vrVgicVmcr) do + vcpuUpdate vc fun v => syncVgicStatus v fl4NumLrs fl4GicV3) σ = + ({((), updKSc σ vc (.archObj (.vcpu (if field ≥ vrVgicLr0 ∨ field = vrVgicHcr ∨ field = vrVgicVmcr + then syncVgicStatus (fl4VcpuRegSet field val v) fl4NumLrs fl4GicV3 + else fl4VcpuRegSet field val v))))}, False) := by + rw [nd_bind_oneSc (fl4VcpuSet_eqA field val hkv)] + by_cases hsync : field ≥ vrVgicLr0 ∨ field = vrVgicHcr ∨ field = vrVgicVmcr + · rw [if_pos hsync, NonDet.when, if_pos hsync, vcpuUpdate_eqA _ (updKSc_same σ vc _), updKSc_updKSc] + · rw [if_neg hsync, NonDet.when, if_neg hsync]; rfl + rw [nd_bind_oneSc hinner]; rfl + +theorem fl4PerformVcpuInv_injectIrq_eqA {σ : State Unit} {vc : ObjRef} {v : Vcpu} + (index : Nat) (lr : Word) (hkv : σ.kheap vc = some (.archObj (.vcpu v))) : + fl4PerformVcpuInv (Z := Unit) (.injectIrq vc index lr) σ = + ({([], updKSc σ vc (.archObj (.vcpu (syncVgicStatus (fl4VcpuRegSet (vrVgicLr0 + index) lr v) + fl4NumLrs fl4GicV3))))}, False) := by + unfold fl4PerformVcpuInv; dsimp only + rw [nd_bind_oneSc (fl4VcpuSet_eqA (vrVgicLr0 + index) lr hkv)] + rw [nd_bind_oneSc (vcpuUpdate_eqA _ (updKSc_same σ vc _))] + rw [updKSc_updKSc]; rfl + +theorem fl4PerformVcpuInv_ackVppi_eqA {σ : State Unit} {vc : ObjRef} {v : Vcpu} + (hcur : σ.archState.armCurrentVcpu = none) (hkv : σ.kheap vc = some (.archObj (.vcpu v))) : + fl4PerformVcpuInv (Z := Unit) (.ackVppi vc) σ = + ({([], updKSc σ vc (.archObj (.vcpu + { v with vcpuVppiMasked := Function.update v.vcpuVppiMasked .vppiEventIRQ_VTimer false })))}, False) := by + unfold fl4PerformVcpuInv; dsimp only + rw [nd_bind_oneSc (fl4VcpuFlush_noneA hcur vc)] + rw [nd_bind_oneSc (vcpuUpdate_eqA _ hkv)] + rfl + +end + +end Fl4ProofsA64.P3.Vcpu + diff --git a/proofs/Fl4ProofsA64/P3/VcpuTimestampA.lean b/proofs/Fl4ProofsA64/P3/VcpuTimestampA.lean new file mode 100644 index 000000000..14d9d3adf --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/VcpuTimestampA.lean @@ -0,0 +1,391 @@ +import Fl4ProofsA64.P3.VcpuRunBaseA + +/-! # AArch64 P3 VCPU Timestamp & IRQ Mask Helpers (`VcpuXferStateA.update_timestamp`) -/ + +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3.Vcpu + +open Fl4Spec Fl4SpecA64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3 Fl4ProofsA64.P3.Sched +open Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +private theorem runVcpuA_eq (t : U64) : runVcpuA t = wa t 3672#u64 := rfl + +private theorem wa_zeroA (x : U64) : wa x 0#u64 = x := by + apply UScalar.eq_of_val_eq; rw [wa_valP]; have := x.bv.isLt; simp [Nat.mod_eq_of_lt this] + +private theorem aligned_of_wa24A {c : U64} (h : Aligned (wa c 24#u64)) : Aligned c := by + unfold Aligned at h ⊢; rw [wa_valP] at h + have h24 : (24#u64).val = 24 := rfl; rw [h24] at h; omega + +/-! ## Section 5: Guest exits (`handle_vcpu_run_exit` / `handle_vcpu_run_vppi` before `syscall.finish`) -/ + +section run_exits +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +theorem layout_irq_of_totalA (id cpu : U64) : layout.irq.of id cpu ⦃ _ => True ⦄ := by + unfold layout.irq.of + by_cases h1 : id >= layout.irq.LPI_ID_BASE + · rw [ite_eq_left h1, u64_sub_ok h1, bind_ok, lift_ok, bind_ok] + have hlb : layout.irq.LPI_BASE = ok 5120#u64 := by + unfold layout.irq.LPI_BASE layout.irq.BANKED_BASE layout.irq.BANKED_CORES layout.irq.NUM_BANKED + rw [u64_mul_ok (by decide), bind_ok, u64_add_ok (by decide)]; rfl + rw [hlb, bind_ok, WP.spec_ok]; trivial + · rw [ite_eq_right h1] + by_cases h2 : id < layout.irq.NUM_BANKED + · rw [ite_eq_left h2] + have hnz : layout.irq.BANKED_CORES ≠ 0#u64 := by unfold layout.irq.BANKED_CORES; decide + refine WP.spec_bind (U64.rem_spec (x := cpu) (y := layout.irq.BANKED_CORES) (by intro e; exact hnz (UScalar.eq_of_val_eq e))) ?_ + intro _ _; rw [lift_ok, bind_ok, lift_ok, bind_ok, WP.spec_ok]; trivial + · rw [ite_eq_right h2, WP.spec_ok]; trivial + +theorem maskInterrupt_stateA (m : Bool) (irq : Irq) (σ : State Unit) : + doMachineOp (Z := Unit) (maskInterrupt m irq) σ = + ({((), { σ with machineState := { σ.machineState with irqMasks := Function.update σ.machineState.irqMasks irq m } })}, False) := by + unfold doMachineOp maskInterrupt + rw [nd_gets_bindSc] + have hs : NonDet.selectF (NonDet.modify (fun s : MachineState => { s with irqMasks := Function.update s.irqMasks irq m }) σ.machineState) σ = + ({(((), { σ.machineState with irqMasks := Function.update σ.machineState.irqMasks irq m }), σ)}, False) := + selectF_oneA _ _ + rw [nd_bind_oneSc hs]; dsimp only; rw [nd_modify_bindA]; rfl + +theorem vcpuXferStateA_of_maskInterruptA {AR : A → HwRootA → Prop} {Z : XSetsA} + {OS Ub : Set U64} {m : Fl4SpecA64.McState} {a a' : A} {s : S} {σ : State Unit} + (X : VcpuXferStateA (L := L) AR Z OS Ub m a s σ) (hAR' : AR a' Z.hw) + (b : Bool) (irq : Irq) : + VcpuXferStateA (L := L) AR Z OS Ub m a' s + { σ with machineState := { σ.machineState with irqMasks := Function.update σ.machineState.irqMasks irq b } } := by + have X' := X.of_hw hAR' + exact ⟨⟨{ X'.j.1 with + st := ⟨X'.j.1.st.cnode, X'.j.1.st.tcb⟩ + cdt := ⟨X'.j.1.cdt.tree, X'.j.1.cdt.cdt, X'.j.1.cdt.cdt_slot, X'.j.1.cdt.orig, X'.j.1.cdt.par_slot, X'.j.1.cdt.prev⟩ + sch := { core := X'.j.1.sch.core, cpuCs := X'.j.1.sch.cpuCs, curOfW := X'.j.1.sch.curOfW, + curScOfW := X'.j.1.sch.curScOfW, cur := X'.j.1.sch.cur, csc := X'.j.1.sch.csc, + idle := X'.j.1.sch.idle, idleOf := X'.j.1.sch.idleOf, idleScOf := X'.j.1.sch.idleScOf, + act := X'.j.1.sch.act, time := X'.j.1.sch.time, aff := X'.j.1.sch.aff, affT := X'.j.1.sch.affT, + tcb := X'.j.1.sch.tcb, sc := X'.j.1.sch.sc, scOff := X'.j.1.sch.scOff } + irq := ⟨X'.j.1.irq.st, X'.j.1.irq.node, X'.j.1.irq.nodeHi⟩ + rest := { prio := X'.j.1.rest.prio, rq := X'.j.1.rest.rq, rqDom := X'.j.1.rest.rqDom, + rel := X'.j.1.rest.rel, curDom := X'.j.1.rest.curDom, domTime := X'.j.1.rest.domTime, + consumed := X'.j.1.rest.consumed, repr := X'.j.1.rest.repr, + scMem := X'.j.1.rest.scMem, refillMax := X'.j.1.rest.refillMax } + vs := X'.j.1.vs.of_spec rfl (fun _ _ => rfl) + ext := X'.j.1.ext.of_spec rfl (fun _ _ => rfl) }, + ⟨⟨X'.j.2.1.obj, X'.j.2.1.back, X'.j.2.1.tcb, X'.j.2.1.cap⟩, + ⟨X'.j.2.2.1.obj, X'.j.2.2.1.back, X'.j.2.2.1.cap⟩, + ⟨X'.j.2.2.2.obj, X'.j.2.2.2.back, X'.j.2.2.2.cap⟩⟩⟩, + X'.tr, + ⟨X'.car.1.of_capsA rfl (fun _ h => h) (fun _ h => h) (fun _ _ => Or.inl rfl), + X'.car.2.of_keepA (fun _ _ _ h => ⟨_, h⟩) (fun _ _ h => ⟨_, h⟩) (fun _ _ => CapFinEqA.refl _)⟩, + ⟨⟨X'.vcpu.core.obj, X'.vcpu.core.back, X'.vcpu.core.tcb, X'.vcpu.core.cap⟩, + X'.vcpu.owner, X'.vcpu.vppi, X'.vcpu.runOff, X'.vcpu.runOut, X'.vcpu.regs, + X'.vcpu.bind_v2t, X'.vcpu.bind_t2v, X'.vcpu.run_v2c, X'.vcpu.run_c2v, + X'.vcpu.run_state, X'.vcpu.bind_run_disj, X'.vcpu.idle_no_vcpu, X'.vcpu.live, + X'.vcpu.arch, X'.vcpu.place⟩, + X'.fmem, X'.regs⟩ + +set_option maxHeartbeats 800000 in +/-- **`sched.update_timestamp` refines `updateTimeStamp`** on `VcpuXferStateA` (single-domain), +preserving `VcpuXferStateA`, `Clock`, `VcpuRunPresA`, and `ScStateRelA`. -/ +theorem VcpuXferStateA.update_timestamp {AR : A → HwRootA → Prop} {Z : XSetsA} + {OS Ub : Set U64} {m : Fl4SpecA64.McState} {a : A} {s : S} {σ : State Unit} + (X : VcpuXferStateA (L := L) AR Z OS Ub m a s σ) + {Clock : A → MachineState → Prop} + (hNow : NowAgreeA ar Clock) (hNowAR : ∀ a0 h, AR a0 h → ar.now a0 ⦃ r => AR r.2 h ⦄) + (hClock : Clock a σ.machineState) + (hnd : numDomains = 1) + (hNowEq : fl4ReadRawCounter σ.machineState = getCurrentTime σ.machineState) + (hCs : ∀ c ∈ Z.Cs, c.val < 128) (hcpu : (L.cpu s).val < 128) + (hfa : ∀ q, FootA (L := L) Z s q → Aligned q) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hClkDisj : ∀ q, (q = L.nodeA s layout.node.CUR_TIME ∨ q = L.nodeA s layout.node.CONSUMED) → + (∀ y, SlotWA Z.th y → q ≠ y ∧ q ≠ wa y 8#u64 ∧ q ≠ wa y 16#u64 ∧ q ≠ wa y 24#u64) ∧ + (∀ y ∈ OS, q ≠ y ∧ q ≠ wa y 8#u64 ∧ q ≠ wa y 16#u64 ∧ q ≠ wa y 24#u64) ∧ + (∀ t ∈ Z.T, ∀ o : U64, o.val < 4096 → q ≠ wa t o) ∧ + (∀ c ∈ Z.C, ∀ o : U64, o.val < 4096 → q ≠ wa c o) ∧ + (¬ VsFootA Z.Pp Z.S1 Z.Y (L.ksb s) q) ∧ + (¬ ExtFootE Z.X Z.Y Z.T2 q) ∧ + (q ≠ wa (L.ksb s) 8#u64 ∧ q ≠ wa (L.ksb s) 16#u64) ∧ + (∀ i : U64, i.val < 5376 → q ≠ irqWordA (L.ksb s) i)) : + sched.update_timestamp st ar s a ⦃ res => + ∃ σ1, + updateTimeStamp (Z := Unit) σ = ({((), σ1)}, False) ∧ + σ1 = fl4TimestampState (rawNextA σ.machineState) { σ with machineState := rawStateA σ.machineState } ∧ + VcpuXferStateA (L := L) AR Z OS Ub m res.2 res.1 σ1 ∧ + Clock res.2 σ1.machineState ∧ + L.ksb res.1 = L.ksb s ∧ L.cpu res.1 = L.cpu s ∧ + VcpuRunPresA L Z OS s res.1 σ σ1 ∧ + (∀ q, Aligned q → q ≠ L.nodeA s layout.node.CUR_TIME → q ≠ L.nodeA s layout.node.CONSUMED → + L.mem res.1 q = L.mem s q) ⦄ := by + have halT : Aligned (L.nodeA s layout.node.CUR_TIME) := + X.vcpu.place.node_al (L.cpu s) layout.node.CUR_TIME (by unfold layout.node.CUR_TIME; decide) + have halC : Aligned (L.nodeA s layout.node.CONSUMED) := + X.vcpu.place.node_al (L.cpu s) layout.node.CONSUMED (by unfold layout.node.CONSUMED; decide) + have hne_tc : L.nodeA s layout.node.CUR_TIME ≠ L.nodeA s layout.node.CONSUMED := by + intro e + have h1 := nodeAt_valA (L := L) s (L.cpu s) layout.node.CUR_TIME + have h2 := nodeAt_valA (L := L) s (L.cpu s) layout.node.CONSUMED + have hfit := X.j.1.ksFit + have hct : layout.node.CUR_TIME.val = 48 := by unfold layout.node.CUR_TIME; rfl + have hco : layout.node.CONSUMED.val = 40 := by unfold layout.node.CONSUMED; rfl + have hv := congrArg UScalar.val e + unfold LawfulStore.nodeA at hv + rw [h1, h2, hct, hco, Nat.mod_eq_of_lt (by unfold ksSizeA at hfit; omega), + Nat.mod_eq_of_lt (by unfold ksSizeA at hfit; omega)] at hv + omega + have ep : sched.cur_time st s = ok (L.curTime s) := by + unfold sched.cur_time LawfulStore.curTime LawfulStore.nodeA + rw [L.cur_cpu_eq, bind_ok, eq_of_spec_fd (obj.node_get.spec s _ _)] + unfold sched.update_timestamp + rw [ep, bind_ok] + refine WP.spec_bind (WP.spec_and (hNow a σ.machineState hClock) (hNowAR a Z.hw X.j.1.ar)) ?_ + rintro ⟨now, a1⟩ ⟨⟨hnow_bv, hcl1⟩, hAR1⟩ + simp only [uncurry_apply_pair] + rw [L.cur_cpu_eq, bind_ok, eq_of_spec_fd (obj.node_set.spec s _ _ _)] + simp only [bind_ok, lift] + set s1 := L.wr s (L.nodeA s layout.node.CUR_TIME) now + have haddrC : L.nodeA s1 layout.node.CONSUMED = L.nodeA s layout.node.CONSUMED := by + unfold LawfulStore.nodeA s1; rw [L.cpu_wr, nodeAt_of_ksb (L.ksb_wr _ _ _)] + have hc_ok : sched.consumed st s1 = ok (L.mem s (L.nodeA s layout.node.CONSUMED)) := by + rw [eq_of_spec_fd (sched.consumed.specA s1), haddrC, mem_wr_neA halT halC hne_tc] + change sched.consumed st (L.wr s (L.nodeAt s (L.cpu s) layout.node.CUR_TIME) now) = _ at hc_ok + rw [hc_ok, bind_ok] + change (sched.set_consumed st s1 _ >>= _) ⦃ _ ⦄ + refine WP.spec_bind (sched.set_consumed.spec s1 _) ?_ + rintro s2 rfl + simp only [layout.NUM_DOMAINS, fl4_abi.config.NUM_DOMAINS, + show ¬ ((1#u64 : U64) > 1#u64) from by decide, ↓reduceIte, bind_ok, WP.spec_ok] + rw [haddrC] + set newCons := wa (L.mem s (L.nodeA s layout.node.CONSUMED)) (core.num.U64.wrapping_sub now (L.curTime s)) + set s' := L.wr s1 (L.nodeA s layout.node.CONSUMED) newCons + set σ1 := fl4TimestampState (rawNextA σ.machineState) { σ with machineState := rawStateA σ.machineState } + have hsp : updateTimeStamp (Z := Unit) σ = ({((), σ1)}, False) := by + rw [← fl4UpdateTimestamp_eq_l4vA σ hnd hNowEq, fl4UpdateTimestamp_eqA] + have hksb' : L.ksb s' = L.ksb s := by simp [s', s1, L.ksb_wr] + have hcpu' : L.cpu s' = L.cpu s := by simp [s', s1, L.cpu_wr] + have hmem_other : ∀ q, Aligned q → q ≠ L.nodeA s layout.node.CUR_TIME → q ≠ L.nodeA s layout.node.CONSUMED → + L.mem s' q = L.mem s q := fun q hq h1 h2 => by + simp only [s', s1]; rw [mem_wr_neA halC hq h2.symm, mem_wr_neA halT hq h1.symm] + have hnode_other : ∀ c o, c.val < 128 → o.val < 128 → o.val % 8 = 0 → + (c = L.cpu s → o ≠ layout.node.CUR_TIME ∧ o ≠ layout.node.CONSUMED) → + L.mem s' (L.nodeAt s c o) = L.mem s (L.nodeAt s c o) := fun c o hc ho ho8 hne => by + have hfit := X.j.1.ksFit + have hne_addr : ∀ k : U64, k.val < 128 → (c = L.cpu s → o ≠ k) → L.nodeAt s c o ≠ L.nodeA s k := fun k hk hok e => by + have hv := congrArg UScalar.val e + unfold LawfulStore.nodeA at hv + rw [nodeAt_valA, nodeAt_valA, Nat.mod_eq_of_lt (by unfold ksSizeA at hfit; omega), + Nat.mod_eq_of_lt (by unfold ksSizeA at hfit; omega)] at hv + by_cases hceq : c = L.cpu s + · subst hceq; exact hok rfl (UScalar.eq_of_val_eq (by omega)) + · have hcv : c.val ≠ (L.cpu s).val := fun ev => hceq (UScalar.eq_of_val_eq ev) + omega + exact hmem_other _ (X.vcpu.place.node_al c o ho8) + (hne_addr layout.node.CUR_TIME (by unfold layout.node.CUR_TIME; decide) (fun e => (hne e).1)) + (hne_addr layout.node.CONSUMED (by unfold layout.node.CONSUMED; decide) (fun e => (hne e).2)) + have hvc_eq : ∀ v ∈ Z.V, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → L.mem s' (wa v o) = L.mem s (wa v o) := fun v hv o ho ho8 => by + have hfit := X.j.1.ksFit + have hne_clk : ∀ k : U64, k.val < 128 → wa v o ≠ L.nodeA s k := fun k hk e => by + obtain ⟨ko, hko, hkeq⟩ := nodeAt_eq_wa_ksbA (L := L) s (L.cpu s) k hcpu (by omega) + have hv_val : (wa (L.ksb s) ko).val = (L.ksb s).val + ko.val := by rw [wa_valP, Nat.mod_eq_of_lt (by omega)] + exact X.vcpu.place.disj_ks _ ⟨v, hv, o, ho, ho8, rfl⟩ (by rw [e]; unfold LawfulStore.nodeA; rw [hkeq, hv_val]; omega) + exact hmem_other _ (aligned_wa (X.vcpu.place.vc_al hv) o ho8) + (hne_clk layout.node.CUR_TIME (by unfold layout.node.CUR_TIME; decide)) + (hne_clk layout.node.CONSUMED (by unfold layout.node.CONSUMED; decide)) + have htcb_eq : ∀ t ∈ Z.T, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → L.mem s' (wa t o) = L.mem s (wa t o) := fun t ht o ho ho8 => + hmem_other _ (aligned_wa (X.vcpu.place.al_t t ht) o ho8) + ((hClkDisj _ (Or.inl rfl)).2.2.1 t ht o ho).symm ((hClkDisj _ (Or.inr rfl)).2.2.1 t ht o ho).symm + have hsc_eq : ∀ c ∈ Z.C, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → L.mem s' (wa c o) = L.mem s (wa c o) := fun c hc o ho ho8 => + hmem_other _ (aligned_wa (aligned_of_wa24A (hfa _ (FootA.nslot _ (FootNA.sc c hc)))) o ho8) + ((hClkDisj _ (Or.inl rfl)).2.2.2.1 c hc o ho).symm ((hClkDisj _ (Or.inr rfl)).2.2.2.1 c hc o ho).symm + have hos' : OSKeep st OS s s' := fun y hy => by + have hal_y := X.vcpu.place.al_os y hy + have h1 := (hClkDisj _ (Or.inl rfl)).2.1 y hy + have h2 := (hClkDisj _ (Or.inr rfl)).2.1 y hy + exact ⟨hmem_other _ hal_y h1.1.symm h2.1.symm, + hmem_other _ (aligned_wa hal_y 8#u64 (by decide)) h1.2.1.symm h2.2.1.symm, + hmem_other _ (aligned_wa hal_y 16#u64 (by decide)) h1.2.2.1.symm h2.2.2.1.symm, + hmem_other _ (aligned_wa hal_y 24#u64 (by decide)) h1.2.2.2.symm h2.2.2.2.symm⟩ + have hcap_os : ∀ y ∈ OS, capAtA (L.mem s') y = capAtA (L.mem s) y := fun y hy => by + unfold capAtA; rw [(hos' y hy).1, (hos' y hy).2.1] + have hslot' : ∀ y, SlotWA Z.th y → + L.mem s' y = L.mem s y ∧ L.mem s' (wa y 8#u64) = L.mem s (wa y 8#u64) ∧ + L.mem s' (wa y 16#u64) = L.mem s (wa y 16#u64) ∧ L.mem s' (wa y 24#u64) = L.mem s (wa y 24#u64) := fun y hy => by + have h1 := (hClkDisj _ (Or.inl rfl)).1 y hy + have h2 := (hClkDisj _ (Or.inr rfl)).1 y hy + exact ⟨hmem_other _ (hfa _ (.slot y _ hy (Or.inl rfl))) h1.1.symm h2.1.symm, + hmem_other _ (hfa _ (.slot y _ hy (Or.inr (Or.inl rfl)))) h1.2.1.symm h2.2.1.symm, + hmem_other _ (hfa _ (.slot y _ hy (Or.inr (Or.inr (Or.inl rfl))))) h1.2.2.1.symm h2.2.2.1.symm, + hmem_other _ (hfa _ (.slot y _ hy (Or.inr (Or.inr (Or.inr rfl))))) h1.2.2.2.symm h2.2.2.2.symm⟩ + have hcurTime' : σ1.curTime = (L.curTime s').bv := by + unfold LawfulStore.curTime LawfulStore.nodeA; rw [hcpu', nodeAt_ksb hksb'] + change rawNextA σ.machineState = (L.mem s' (L.nodeA s layout.node.CUR_TIME)).bv + rw [mem_wr_neA halC halT hne_tc.symm, L.mem_wr_same halT]; exact hnow_bv.symm + have hcons' : σ1.consumedTime = (L.mem s' (L.nodeAt s' (L.cpu s') layout.node.CONSUMED)).bv := by + rw [hcpu', nodeAt_ksb hksb'] + change σ.consumedTime + (rawNextA σ.machineState - σ.curTime) = (L.mem s' (L.nodeA s layout.node.CONSUMED)).bv + rw [L.mem_wr_same halC] + simp only [newCons, core.num.U64.wrapping_add_bv_eq, core.num.U64.wrapping_sub_bv_eq, hnow_bv] + have hc0 : σ.consumedTime = (L.mem s (L.nodeA s layout.node.CONSUMED)).bv := X.j.1.rest.consumed + have ht0 : σ.curTime = (L.curTime s).bv := X.j.1.sch.time + rw [hc0, ht0, hnow_bv] + have hSt' : StateRelA Z.th (L.mem s') σ1 := by + refine ⟨fun p r hp => ?_, fun t ht => ?_⟩ + · obtain ⟨cs, hk, hwf, hsl⟩ := X.j.1.st.cnode p r hp + refine ⟨cs, hk, hwf, fun idx hidx => ?_⟩ + have hsr := hsl idx hidx; unfold SlotRelA at hsr ⊢ + have he := hslot' _ (Or.inr ⟨p, r, hp, idx, hidx, rfl⟩) + unfold capAtA; rw [he.1, he.2.1]; exact hsr + · obtain ⟨xt, hk, hsl⟩ := X.j.1.st.tcb t ht + refine ⟨xt, hk, fun i hi => ?_⟩ + have hsr := hsl i hi; unfold SlotRelA at hsr ⊢ + have he := hslot' _ (Or.inl ⟨t, Or.inr ⟨ht, i, hi, rfl⟩⟩) + unfold capAtA; rw [he.1, he.2.1]; exact hsr + have hCdt' : CdtRelA st Z.th s' σ1 Z.G := + ⟨X.j.1.cdt.tree.keepO (fun y hy => hslot' y (Or.inl hy)), X.j.1.cdt.cdt, X.j.1.cdt.cdt_slot, + fun sl hsl => by unfold revA; rw [(hslot' _ (absSlot_w hsl)).2.2.2]; exact X.j.1.cdt.orig sl hsl, + X.j.1.cdt.par_slot, fun a' p' hp' => by + have hp_sl : SlotWA Z.th p' := by + obtain ⟨sl, hsl, hsl_a⟩ := X.j.1.cdt.par_slot a' p' hp' + have hcdt := X.j.1.cdt.cdt sl hsl; rw [hsl_a, hp'] at hcdt + cases hq : σ.cdt sl with + | none => rw [hq] at hcdt; cases hcdt + | some q => rw [hq] at hcdt; simp only [Option.map_some, Option.some.injEq] at hcdt; rw [← hcdt]; exact absSlot_w (X.j.1.cdt.cdt_slot sl q hq).2 + unfold revA; rw [(hslot' p' hp_sl).2.2.2]; exact X.j.1.cdt.prev a' p' hp'⟩ + have hSch' : SchedRelA (L := L) Z.T Z.C Z.Cs Z.curOf Z.curScOf s' σ1 m := { + core := by rw [hcpu']; exact X.j.1.sch.core + cpuCs := by rw [hcpu']; exact X.j.1.sch.cpuCs + curOfW := fun c hc => by rw [nodeAt_ksb hksb', hnode_other c _ (hCs c hc) (by unfold layout.node.CUR_THREAD; decide) (by unfold layout.node.CUR_THREAD; decide) (fun _ => ⟨by unfold layout.node.CUR_THREAD layout.node.CUR_TIME; decide, by unfold layout.node.CUR_THREAD layout.node.CONSUMED; decide⟩)]; exact X.j.1.sch.curOfW c hc + curScOfW := fun c hc => by rw [nodeAt_ksb hksb', hnode_other c _ (hCs c hc) (by unfold layout.node.CUR_SC; decide) (by unfold layout.node.CUR_SC; decide) (fun _ => ⟨by unfold layout.node.CUR_SC layout.node.CUR_TIME; decide, by unfold layout.node.CUR_SC layout.node.CONSUMED; decide⟩)]; exact X.j.1.sch.curScOfW c hc + cur := X.j.1.sch.cur + csc := X.j.1.sch.csc + idle := by unfold LawfulStore.idleW; rw [hcpu', nodeAt_ksb hksb', hnode_other _ _ hcpu (by unfold layout.node.IDLE_THREAD; decide) (by unfold layout.node.IDLE_THREAD; decide) (fun _ => ⟨by unfold layout.node.IDLE_THREAD layout.node.CUR_TIME; decide, by unfold layout.node.IDLE_THREAD layout.node.CONSUMED; decide⟩)]; exact X.j.1.sch.idle + idleOf := fun c hc => by rw [nodeAt_ksb hksb', hnode_other c _ (hCs c hc) (by unfold layout.node.IDLE_THREAD; decide) (by unfold layout.node.IDLE_THREAD; decide) (fun _ => ⟨by unfold layout.node.IDLE_THREAD layout.node.CUR_TIME; decide, by unfold layout.node.IDLE_THREAD layout.node.CONSUMED; decide⟩)]; exact X.j.1.sch.idleOf c hc + idleScOf := fun c hc => by rw [nodeAt_ksb hksb', hnode_other c _ (hCs c hc) (by unfold layout.node.IDLE_SC; decide) (by unfold layout.node.IDLE_SC; decide) (fun _ => ⟨by unfold layout.node.IDLE_SC layout.node.CUR_TIME; decide, by unfold layout.node.IDLE_SC layout.node.CONSUMED; decide⟩)]; exact X.j.1.sch.idleScOf c hc + act := by unfold LawfulStore.actionW; rw [hcpu', nodeAt_ksb hksb', hnode_other _ _ hcpu (by unfold layout.node.SCHED_ACTION; decide) (by unfold layout.node.SCHED_ACTION; decide) (fun _ => ⟨by unfold layout.node.SCHED_ACTION layout.node.CUR_TIME; decide, by unfold layout.node.SCHED_ACTION layout.node.CONSUMED; decide⟩)]; exact X.j.1.sch.act + time := hcurTime' + aff := fun t ht => by unfold affA; rw [htcb_eq t ht 3520#u64 (by decide) (by decide)]; exact X.j.1.sch.aff t ht + affT := X.j.1.sch.affT + tcb := fun t ht => by + obtain ⟨xt, hk, hst, hsc⟩ := X.j.1.sch.tcb t ht + exact ⟨xt, hk, by rw [stA_eq, htcb_eq t ht 3328#u64 (by decide) (by decide), ← stA_eq]; exact hst, + by rw [scA_eq, htcb_eq t ht 3464#u64 (by decide) (by decide), ← scA_eq]; exact hsc⟩ + sc := fun c hc => by rw [hsc_eq c hc 24#u64 (by decide) (by decide)]; exact X.j.1.sch.sc c hc + scOff := X.j.1.sch.scOff } + have hIrq' : IrqRelA (L.mem s') (L.ksb s') σ1 := + ⟨fun i hi => by rw [hksb', hmem_other _ (hfa _ (.nslot _ (.irq i hi))) ((hClkDisj _ (Or.inl rfl)).2.2.2.2.2.2.2 i hi).symm ((hClkDisj _ (Or.inr rfl)).2.2.2.2.2.2.2 i hi).symm]; exact X.j.1.irq.st i hi, + by rw [hksb']; exact X.j.1.irq.node, by rw [hksb']; exact X.j.1.irq.nodeHi⟩ + have hRest' : SchedRestA (L := L) Z s' σ1 m := { + rq := fun p hp => by rw [hcpu']; exact X.j.1.rest.rq p hp + rqDom := X.j.1.rest.rqDom + rel := by rw [hcpu']; exact X.j.1.rest.rel + consumed := hcons' + repr := by rw [hcpu', nodeAt_ksb hksb', hnode_other _ _ hcpu (by unfold layout.node.REPROGRAM; decide) (by unfold layout.node.REPROGRAM; decide) (fun _ => ⟨by unfold layout.node.REPROGRAM layout.node.CUR_TIME; decide, by unfold layout.node.REPROGRAM layout.node.CONSUMED; decide⟩)]; exact X.j.1.rest.repr + curDom := by rw [hksb', hmem_other _ (hfa _ (.nslot _ (.glob _ (Or.inl rfl)))) (hClkDisj _ (Or.inl rfl)).2.2.2.2.2.2.1.1.symm (hClkDisj _ (Or.inr rfl)).2.2.2.2.2.2.1.1.symm]; exact X.j.1.rest.curDom + domTime := by + simp only [σ1, fl4TimestampState, hnd, ↓reduceIte] + rw [hksb', hmem_other _ (hfa _ (.nslot _ (.glob _ (Or.inr rfl)))) (hClkDisj _ (Or.inl rfl)).2.2.2.2.2.2.1.2.symm (hClkDisj _ (Or.inr rfl)).2.2.2.2.2.2.1.2.symm] + exact X.j.1.rest.domTime + prio := fun t ht => by + obtain ⟨xt, hk, h1, h2, h3⟩ := X.j.1.rest.prio t ht + exact ⟨xt, hk, h1, + by unfold domA at h2 ⊢; rw [htcb_eq t ht 3432#u64 (by decide) (by decide)]; exact h2, + by unfold prioA at h3 ⊢; rw [htcb_eq t ht 3448#u64 (by decide) (by decide)]; exact h3⟩ + scMem := fun t ht hne => by rw [scA_eq, htcb_eq t ht 3464#u64 (by decide) (by decide), ← scA_eq] at hne ⊢; exact X.j.1.rest.scMem t ht hne + refillMax := fun c hc => by + obtain ⟨y, n, hk, hrm⟩ := X.j.1.rest.refillMax c hc + exact ⟨y, n, hk, by rw [hsc_eq c hc 72#u64 (by decide) (by decide)]; exact hrm⟩ } + have hJ' : JAtZA (L := L) AR Z s' a1 σ1 m := { + st := hSt', cdt := hCdt', sch := hSch', irq := hIrq', rest := hRest', ar := hAR1, + place := by rw [hksb']; exact X.j.1.place, + thT := X.j.1.thT, csN := X.j.1.csN, + idleT := fun c hc => by rw [nodeAt_ksb hksb', hnode_other c _ (hCs c hc) (by unfold layout.node.IDLE_THREAD; decide) (by unfold layout.node.IDLE_THREAD; decide) (fun _ => ⟨by unfold layout.node.IDLE_THREAD layout.node.CUR_TIME; decide, by unfold layout.node.IDLE_THREAD layout.node.CONSUMED; decide⟩)]; exact X.j.1.idleT c hc + idleC := fun c hc => by rw [nodeAt_ksb hksb', hnode_other c _ (hCs c hc) (by unfold layout.node.IDLE_SC; decide) (by unfold layout.node.IDLE_SC; decide) (fun _ => ⟨by unfold layout.node.IDLE_SC layout.node.CUR_TIME; decide, by unfold layout.node.IDLE_SC layout.node.CONSUMED; decide⟩)]; exact X.j.1.idleC c hc + curT := fun c hc => by rw [nodeAt_ksb hksb', hnode_other c _ (hCs c hc) (by unfold layout.node.CUR_THREAD; decide) (by unfold layout.node.CUR_THREAD; decide) (fun _ => ⟨by unfold layout.node.CUR_THREAD layout.node.CUR_TIME; decide, by unfold layout.node.CUR_THREAD layout.node.CONSUMED; decide⟩)]; exact X.j.1.curT c hc + cscC := fun c hc => by rw [nodeAt_ksb hksb', hnode_other c _ (hCs c hc) (by unfold layout.node.CUR_SC; decide) (by unfold layout.node.CUR_SC; decide) (fun _ => ⟨by unfold layout.node.CUR_SC layout.node.CUR_TIME; decide, by unfold layout.node.CUR_SC layout.node.CONSUMED; decide⟩)]; exact X.j.1.cscC c hc + ksFit := by rw [hksb']; exact X.j.1.ksFit + scOffD := X.j.1.scOffD + cv := fun o ho => by unfold capAtA; rw [(hslot' o (Or.inl ho)).1, (hslot' o (Or.inl ho)).2.1]; exact X.j.1.cv o ho + pml := fun y hy p mp asid hc => by unfold capAtA at hc; rw [(hslot' y (Or.inl hy)).1, (hslot' y (Or.inl hy)).2.1] at hc; exact X.j.1.pml y hy p mp asid hc + vs := by + rw [hksb'] + exact (X.j.1.vs.of_spec (σ' := σ1) rfl (fun _ _ => rfl)).of_mem + (fun b hb => hmem_other b (hfa b (.nslot b (.vs b hb))) + (fun e => (hClkDisj _ (Or.inl rfl)).2.2.2.2.1 (e ▸ hb)) + (fun e => (hClkDisj _ (Or.inr rfl)).2.2.2.2.1 (e ▸ hb))) + ext := + (X.j.1.ext.of_spec (σ' := σ1) rfl (fun _ _ => rfl)).of_mem + (fun b hb => hmem_other b (hfa b (.nslot b (.ext b hb))) + (fun e => (hClkDisj _ (Or.inl rfl)).2.2.2.2.2.1 (e ▸ hb)) + (fun e => (hClkDisj _ (Or.inr rfl)).2.2.2.2.2.1 (e ▸ hb))) } + have hVcCore' : VcpuCoreA (L := L) Z s' σ1 := + ⟨fun v hv => let ⟨xv, hk, h1, h2⟩ := X.vcpu.core.obj v hv; ⟨xv, hk, by rw [← wa_zeroA v, hvc_eq v hv 0#u64 (by decide) (by decide), wa_zeroA]; exact h1, by rw [hvc_eq v hv 24#u64 (by decide) (by decide)]; exact h2⟩, + X.vcpu.core.back, + fun t ht => let ⟨xt, hk, h1, h2⟩ := X.vcpu.core.tcb t ht; ⟨xt, hk, by rw [htcb_eq t ht 3664#u64 (by decide) (by decide)]; exact h1, by rw [runVcpuA_eq, htcb_eq t ht 3672#u64 (by decide) (by decide), ← runVcpuA_eq]; exact h2⟩, + fun y hy p hc => by have he := hslot' y (Or.inl hy); unfold capAtA at hc; rw [he.1, he.2.1] at hc; exact X.vcpu.core.cap y hy p hc⟩ + have hS2' : Stage2CoreA (L := L) Z s' σ1 := + ⟨X.j.2.2.1.obj, X.j.2.2.1.back, fun y hy p l mp asid va hc => by + have he := hslot' y (Or.inl hy); unfold capAtA at hc; rw [he.1, he.2.1] at hc; exact X.j.2.2.1.cap y hy p l mp asid va hc⟩ + have hHV' : HighVSpaceCoreA (L := L) Z s' σ1 := + ⟨X.j.2.2.2.obj, X.j.2.2.2.back, fun y hy p mp asid hc => by + have he := hslot' y (Or.inl hy); unfold capAtA at hc; rw [he.1, he.2.1] at hc; exact X.j.2.2.2.cap y hy p mp asid hc⟩ + have hTr' : TreeA st OS Z.G.chains Z.G.par s' := X.tr.keepO hos' + have hCar' : CarryA Z (L.mem s') σ1 := + ⟨X.car.1.of_capsA rfl (fun _ h => h) (fun _ h => h) (fun y hy => Or.inl (hcap_os y (hsub y hy))), + X.car.2.of_keepA (fun _ _ _ h => ⟨_, h⟩) (fun _ _ h => ⟨_, h⟩) fun y hy => by rw [hcap_os y ((X.tr.lk.members y).1 hy).1]; exact CapFinEqA.refl _⟩ + have hV' : VcpuRelA (L := L) Z OS Ub s' σ1 := + ⟨hVcCore', + fun v hv => by rw [hvc_eq v hv 8#u64 (by decide) (by decide)]; exact X.vcpu.owner v hv, + fun v hv x hkx => by rw [hvc_eq v hv 16#u64 (by decide) (by decide)]; exact X.vcpu.vppi v hv x hkx, + fun v hv x hkx => by rw [hvc_eq v hv 32#u64 (by decide) (by decide)]; exact X.vcpu.runOff v hv x hkx, + fun v hv x hkx => by rw [hvc_eq v hv 40#u64 (by decide) (by decide)]; exact X.vcpu.runOut v hv x hkx, + fun v hv x hkx i hi => by rw [vcpuRegWA, hvc_eq v hv (vcpuRegOffA i) (by rw [vcpuRegOffA_val hi]; omega) (by rw [vcpuRegOffA_val hi]; omega)]; exact X.vcpu.regs v hv x hkx i hi, + fun v hv t ht hne => by rw [← wa_zeroA v, hvc_eq v hv 0#u64 (by decide) (by decide), wa_zeroA] at ht; obtain ⟨h1, h2⟩ := X.vcpu.bind_v2t v hv t ht hne; exact ⟨h1, by rw [htcb_eq t h1 3664#u64 (by decide) (by decide)]; exact h2⟩, + fun t ht v hv hne => by rw [htcb_eq t ht 3664#u64 (by decide) (by decide)] at hv; obtain ⟨h1, h2⟩ := X.vcpu.bind_t2v t ht v hv hne; exact ⟨h1, by rw [← wa_zeroA v, hvc_eq v h1 0#u64 (by decide) (by decide), wa_zeroA]; exact h2⟩, + fun v hv c hc hne => by rw [hvc_eq v hv 24#u64 (by decide) (by decide)] at hc; obtain ⟨h1, h2⟩ := X.vcpu.run_v2c v hv c hc hne; exact ⟨h1, by rw [runVcpuA_eq, htcb_eq c h1 3672#u64 (by decide) (by decide), ← runVcpuA_eq]; exact h2⟩, + fun c hc v hv hne => by rw [runVcpuA_eq, htcb_eq c hc 3672#u64 (by decide) (by decide), ← runVcpuA_eq] at hv; obtain ⟨h1, h2⟩ := X.vcpu.run_c2v c hc v hv hne; exact ⟨h1, by rw [hvc_eq v h1 24#u64 (by decide) (by decide)]; exact h2⟩, + fun c hc hne => by rw [runVcpuA_eq, htcb_eq c hc 3672#u64 (by decide) (by decide), ← runVcpuA_eq] at hne; rw [stA_eq, htcb_eq c hc 3328#u64 (by decide) (by decide), ← stA_eq]; exact X.vcpu.run_state c hc hne, + fun t ht hne => by rw [htcb_eq t ht 3664#u64 (by decide) (by decide)] at hne; rw [runVcpuA_eq, htcb_eq t ht 3672#u64 (by decide) (by decide), ← runVcpuA_eq]; exact X.vcpu.bind_run_disj t ht hne, + fun c hc => by + have hid : L.mem s' (L.nodeAt s' c layout.node.IDLE_THREAD) = L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) := by + rw [nodeAt_ksb hksb']; exact hnode_other c _ (hCs c hc) (by unfold layout.node.IDLE_THREAD; decide) (by unfold layout.node.IDLE_THREAD; decide) (fun _ => ⟨by unfold layout.node.IDLE_THREAD layout.node.CUR_TIME; decide, by unfold layout.node.IDLE_THREAD layout.node.CONSUMED; decide⟩) + rw [hid, htcb_eq _ (X.j.1.idleT c hc) 3664#u64 (by decide) (by decide), runVcpuA_eq, htcb_eq _ (X.j.1.idleT c hc) 3672#u64 (by decide) (by decide), ← runVcpuA_eq] + exact X.vcpu.idle_no_vcpu c hc, + fun v hv hact => by + rw [← wa_zeroA v, hvc_eq v hv 0#u64 (by decide) (by decide), wa_zeroA, hvc_eq v hv 24#u64 (by decide) (by decide)] at hact + obtain ⟨y, hy, hc⟩ := X.vcpu.live v hv hact + exact ⟨y, hy, by rw [hcap_os y hy]; exact hc⟩, + X.vcpu.arch, by rw [hksb']; exact X.vcpu.place⟩ + have hF' : FrameMemRelA Z OS Ub (L.ksb s') (L.mem s') σ1.machineState.underlyingMemory := + ⟨X.fmem.aligned, + fun b hb => by + have hfit := X.j.1.ksFit + have hne_clk : ∀ k : U64, k.val < 128 → b ≠ L.nodeA s k := fun k hk e => by + obtain ⟨ko, hko, hkeq⟩ := nodeAt_eq_wa_ksbA (L := L) s (L.cpu s) k hcpu (by omega) + have hv_val : (wa (L.ksb s) ko).val = (L.ksb s).val + ko.val := by rw [wa_valP, Nat.mod_eq_of_lt (by omega)] + exact X.fmem.disj_ks b hb (by rw [e]; unfold LawfulStore.nodeA; rw [hkeq, hv_val]; omega) + rw [hmem_other b (X.fmem.aligned b hb) (hne_clk layout.node.CUR_TIME (by unfold layout.node.CUR_TIME; decide)) (hne_clk layout.node.CONSUMED (by unfold layout.node.CONSUMED; decide))] + exact X.fmem.agree b hb, + fun y hy ptr sz r mp s2 asid va hcap => by rw [hcap_os y hy] at hcap; exact X.fmem.frames y hy ptr sz r mp s2 asid va hcap, + X.fmem.disj_os, X.fmem.disj_slots, X.fmem.disj_tcb, X.fmem.disj_sc, by rw [hksb']; exact X.fmem.disj_ks⟩ + have hR' : TcbRegsRelA (L.mem s') σ1 Z.T := fun t ht x hkx r => by + obtain ⟨hK, hreg⟩ := regA_eq_wa_KA t r + rw [hreg, htcb_eq t ht _ (by have := regIdxA_lt r; omega) (by omega), ← hreg]; exact X.regs t ht x hkx r + refine ⟨σ1, hsp, rfl, ⟨⟨hJ', ⟨hVcCore', hS2', hHV'⟩⟩, hTr', hCar', hV', hF', hR'⟩, hcl1, hksb', hcpu', + ⟨hos', + fun t ht => ⟨by rw [inqA_eq, htcb_eq t ht 3592#u64 (by decide) (by decide), ← inqA_eq], + by rw [scA_eq, htcb_eq t ht 3464#u64 (by decide) (by decide), ← scA_eq]⟩, + fun c hc => by rw [maxA_eqA, hsc_eq c hc 72#u64 (by decide) (by decide), ← maxA_eqA], + fun c hc => by rw [nodeAt_ksb hksb']; exact hnode_other c _ (hCs c hc) (by unfold layout.node.IDLE_THREAD; decide) (by unfold layout.node.IDLE_THREAD; decide) (fun _ => ⟨by unfold layout.node.IDLE_THREAD layout.node.CUR_TIME; decide, by unfold layout.node.IDLE_THREAD layout.node.CONSUMED; decide⟩), + fun _ h => h, + fun u hu h => by rwa [runVcpuA_eq, htcb_eq u hu 3672#u64 (by decide) (by decide), ← runVcpuA_eq] at h⟩, + hmem_other⟩ + + +end run_exits + +end +end Fl4ProofsA64.P3.Vcpu diff --git a/proofs/Fl4ProofsA64/P3/VcpuTopA.lean b/proofs/Fl4ProofsA64/P3/VcpuTopA.lean new file mode 100644 index 000000000..6e62c11ac --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/VcpuTopA.lean @@ -0,0 +1,326 @@ +/- +P3 (aarch64, A64-P3 S4 W4): **Unified VCPU invocation dispatcher (`vcpu.decode`) refinement** +across all 8 VCPU labels on `VcpuTopPostA`: +- Label 60 (`ARMVCPUSetTCB`) → `vcpu_set_tcb_jA` (`VcpuSetTcbJA`) +- Label 61 (`ARMVCPUInjectIRQ`) → `vcpu_inject_irq_jA` (`VcpuRegJA`) +- Label 62 (`ARMVCPUReadReg`) → `vcpu_read_reg_jA` (`VcpuRegJA`) +- Label 63 (`ARMVCPUWriteReg`) → `vcpu_write_reg_jA` (`VcpuRegJA`) +- Label 64 (`ARMVCPUAckVPPI`) → `vcpu_ack_vppi_jA` (`VcpuRegJA`) +- Label 67 (`ARMVCPUReadState`) → `vcpu_read_state_jA` (`VcpuXferJA`) +- Label 68 (`ARMVCPUWriteState`) → `vcpu_write_state_jA` (`VcpuXferJA`) +- Label 69 (`ARMVCPURun`) → `vcpu_run_jA` (`VcpuRunJA`) +- Any other label → `IllegalOperation` + +No new named premise (unions the W2a/W2b/W3/W4 premises). +-/ +import Fl4ProofsA64.P3.VcpuSetTcbJA +import Fl4ProofsA64.P3.VcpuRunJA +import Fl4ProofsA64.P3.VcpuExitJA +import Fl4ProofsA64.P3.VcpuXferJA + +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3.Vcpu + +open Fl4Spec Fl4SpecA64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3 Fl4ProofsA64.P3.Sched +open Fl4ProofsA64.P3.Endp Fl4ProofsA64.P3.Notif + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section vcpu_top +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] + +theorem fl4LabelOf_vcpu_eqA : + fl4LabelOf fl4_abi.label.ARMVCPUSetTCB.bv = .spec (.archInvocationLabel .armVCPUSetTCB) ∧ + fl4LabelOf fl4_abi.label.ARMVCPUInjectIRQ.bv = .spec (.archInvocationLabel .armVCPUInjectIRQ) ∧ + fl4LabelOf fl4_abi.label.ARMVCPUReadReg.bv = .spec (.archInvocationLabel .armVCPUReadReg) ∧ + fl4LabelOf fl4_abi.label.ARMVCPUWriteReg.bv = .spec (.archInvocationLabel .armVCPUWriteReg) ∧ + fl4LabelOf fl4_abi.label.ARMVCPUAckVPPI.bv = .spec (.archInvocationLabel .armVCPUAckVPPI) ∧ + fl4LabelOf fl4_abi.label.ARMVCPUReadState.bv = .ext .armVCPUReadState ∧ + fl4LabelOf fl4_abi.label.ARMVCPUWriteState.bv = .ext .armVCPUWriteState ∧ + fl4LabelOf fl4_abi.label.ARMVCPURun.bv = .ext .armVCPURun := by + refine ⟨by unfold fl4_abi.label.ARMVCPUSetTCB; decide, + by unfold fl4_abi.label.ARMVCPUInjectIRQ; decide, + by unfold fl4_abi.label.ARMVCPUReadReg; decide, + by unfold fl4_abi.label.ARMVCPUWriteReg; decide, + by unfold fl4_abi.label.ARMVCPUAckVPPI; decide, + by unfold fl4_abi.label.ARMVCPUReadState; decide, + by unfold fl4_abi.label.ARMVCPUWriteState; decide, + by unfold fl4_abi.label.ARMVCPURun; decide⟩ + +private theorem invocationType_specLabelWordA (l : Fl4Label) : + invocationType (specLabelWord l) = match l with + | .spec il => il + | .ext _ => .genInvocationLabel .invalidInvocation := by + cases l with + | ext _ => + unfold specLabelWord invocationType dataToNat; dsimp only; unfold unat + rw [if_pos ⟨.genInvocationLabel .invalidInvocation, by decide⟩]; rfl + | spec il => + unfold specLabelWord invocationType dataToNat; dsimp only; unfold unat + have hlt : il.fromEnum < 2 ^ 64 := by + cases il with + | genInvocationLabel g => cases g <;> decide + | archInvocationLabel a => cases a <;> decide + have hnat : (BitVec.ofNat 64 il.fromEnum).toNat = il.fromEnum := by + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt hlt] + rw [hnat, if_pos ⟨il, rfl⟩] + cases il with + | genInvocationLabel g => cases g <;> rfl + | archInvocationLabel a => cases a <;> rfl + +private theorem liftME_throw_errA {α β E : Type} (f : α → β) (e : E) (σ : State Unit) : + (NonDetE.liftME f (NonDetE.throwError e : NonDetE (State Unit) E α)).run σ = ({(.error e, σ)}, False) := by + unfold NonDetE.liftME NonDetE.throwError ExceptT.run + change ((NonDet.pure (Except.error e) >>= _) σ) = _ + rw [nd_bind_oneSc (m := NonDet.pure (Except.error e)) rfl]; rfl + +theorem fl4LabelOf_other_vcpu_illegalA (lbl : U64) + (h60 : lbl ≠ fl4_abi.label.ARMVCPUSetTCB) + (h61 : lbl ≠ fl4_abi.label.ARMVCPUInjectIRQ) + (h62 : lbl ≠ fl4_abi.label.ARMVCPUReadReg) + (h63 : lbl ≠ fl4_abi.label.ARMVCPUWriteReg) + (h64 : lbl ≠ fl4_abi.label.ARMVCPUAckVPPI) + (h67 : lbl ≠ fl4_abi.label.ARMVCPUReadState) + (h68 : lbl ≠ fl4_abi.label.ARMVCPUWriteState) + (h69 : lbl ≠ fl4_abi.label.ARMVCPURun) + (call : Bool) (args : List Data) (xSlot : CapRef) (cte : CslotPtr) (vc : ObjRef) + (extras : List (Cap × CslotPtr)) (σ : State Unit) : + (fl4ArchDecodeInvocation (Z := Unit) call (fl4LabelOf lbl.bv) args xSlot cte (.vcpuCap vc) extras).run σ = + ({(.error .illegalOperation, σ)}, False) := by + have hval_ne : lbl.val ≠ 60 ∧ lbl.val ≠ 61 ∧ lbl.val ≠ 62 ∧ lbl.val ≠ 63 ∧ + lbl.val ≠ 64 ∧ lbl.val ≠ 67 ∧ lbl.val ≠ 68 ∧ lbl.val ≠ 69 := by + refine ⟨fun h => h60 (UScalar.eq_of_val_eq (by unfold fl4_abi.label.ARMVCPUSetTCB; exact h)), + fun h => h61 (UScalar.eq_of_val_eq (by unfold fl4_abi.label.ARMVCPUInjectIRQ; exact h)), + fun h => h62 (UScalar.eq_of_val_eq (by unfold fl4_abi.label.ARMVCPUReadReg; exact h)), + fun h => h63 (UScalar.eq_of_val_eq (by unfold fl4_abi.label.ARMVCPUWriteReg; exact h)), + fun h => h64 (UScalar.eq_of_val_eq (by unfold fl4_abi.label.ARMVCPUAckVPPI; exact h)), + fun h => h67 (UScalar.eq_of_val_eq (by unfold fl4_abi.label.ARMVCPUReadState; exact h)), + fun h => h68 (UScalar.eq_of_val_eq (by unfold fl4_abi.label.ARMVCPUWriteState; exact h)), + fun h => h69 (UScalar.eq_of_val_eq (by unfold fl4_abi.label.ARMVCPURun; exact h))⟩ + have hlbl_lt64 : lbl.val < 2 ^ 64 := lbl.hBounds + have hlbl_bv : lbl.bv = BitVec.ofNat 64 lbl.val := by + apply BitVec.eq_of_toNat_eq; rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt hlbl_lt64]; rfl + have hcheck : + let l := fl4LabelOf lbl.bv + l ≠ .ext .armVCPUReadState ∧ l ≠ .ext .armVCPUWriteState ∧ l ≠ .ext .armVCPURun ∧ + l ≠ .spec (.archInvocationLabel .armVCPUReadReg) ∧ l ≠ .spec (.archInvocationLabel .armVCPUWriteReg) ∧ + l ≠ .spec (.archInvocationLabel .armVCPUInjectIRQ) ∧ l ≠ .spec (.archInvocationLabel .armVCPUAckVPPI) ∧ + l ≠ .spec (.archInvocationLabel .armVCPUSetTCB) := by + rw [hlbl_bv] + by_cases hlt : lbl.val < 74 + · have : lbl.val = 0 ∨ lbl.val = 1 ∨ lbl.val = 2 ∨ lbl.val = 3 ∨ lbl.val = 4 ∨ lbl.val = 5 ∨ lbl.val = 6 ∨ lbl.val = 7 ∨ lbl.val = 8 ∨ lbl.val = 9 ∨ + lbl.val = 10 ∨ lbl.val = 11 ∨ lbl.val = 12 ∨ lbl.val = 13 ∨ lbl.val = 14 ∨ lbl.val = 15 ∨ lbl.val = 16 ∨ lbl.val = 17 ∨ lbl.val = 18 ∨ lbl.val = 19 ∨ + lbl.val = 20 ∨ lbl.val = 21 ∨ lbl.val = 22 ∨ lbl.val = 23 ∨ lbl.val = 24 ∨ lbl.val = 25 ∨ lbl.val = 26 ∨ lbl.val = 27 ∨ lbl.val = 28 ∨ lbl.val = 29 ∨ + lbl.val = 30 ∨ lbl.val = 31 ∨ lbl.val = 32 ∨ lbl.val = 33 ∨ lbl.val = 34 ∨ lbl.val = 35 ∨ lbl.val = 36 ∨ lbl.val = 37 ∨ lbl.val = 38 ∨ lbl.val = 39 ∨ + lbl.val = 40 ∨ lbl.val = 41 ∨ lbl.val = 42 ∨ lbl.val = 43 ∨ lbl.val = 44 ∨ lbl.val = 45 ∨ lbl.val = 46 ∨ lbl.val = 47 ∨ lbl.val = 48 ∨ lbl.val = 49 ∨ + lbl.val = 50 ∨ lbl.val = 51 ∨ lbl.val = 52 ∨ lbl.val = 53 ∨ lbl.val = 54 ∨ lbl.val = 55 ∨ lbl.val = 56 ∨ lbl.val = 57 ∨ lbl.val = 58 ∨ lbl.val = 59 ∨ + lbl.val = 65 ∨ lbl.val = 66 ∨ lbl.val = 70 ∨ lbl.val = 71 ∨ lbl.val = 72 ∨ lbl.val = 73 := by omega + rcases this with h | h | h | h | h | h | h | h | h | h | + h | h | h | h | h | h | h | h | h | h | + h | h | h | h | h | h | h | h | h | h | + h | h | h | h | h | h | h | h | h | h | + h | h | h | h | h | h | h | h | h | h | + h | h | h | h | h | h | h | h | h | h | + h | h | h | h | h | h <;> (rw [h]; decide) + · have hinv : fl4LabelOf (BitVec.ofNat 64 lbl.val) = .spec (.genInvocationLabel .invalidInvocation) := by + unfold fl4LabelOf + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt hlbl_lt64] + have hget : fl4Numbering[lbl.val]? = none := List.getElem?_eq_none (by + have : fl4Numbering.length = 74 := by decide + omega) + rw [hget]; rfl + rw [hinv]; decide + obtain ⟨h1, h2, h3, h4, h5, h6, h7, h8⟩ := hcheck + unfold fl4ArchDecodeInvocation + dsimp only + rw [if_neg h1, if_neg h2, if_neg h3, if_neg h4, if_neg h5, if_neg h6, if_neg h7, if_neg h8] + unfold archDecodeInvocation decodeVcpuInvocation + dsimp only + rw [invocationType_specLabelWordA] + cases hl : fl4LabelOf lbl.bv with + | ext _ => dsimp only; exact liftME_throw_errA _ _ _ + | spec il => + cases il with + | genInvocationLabel _ => dsimp only; exact liftME_throw_errA _ _ _ + | archInvocationLabel al => + cases al with + | armVCPUSetTCB => exact absurd hl h8 + | armVCPUReadReg => exact absurd hl h4 + | armVCPUWriteReg => exact absurd hl h5 + | armVCPUInjectIRQ => exact absurd hl h6 + | armVCPUAckVPPI => exact absurd hl h7 + | _ => dsimp only; exact liftME_throw_errA _ _ _ + +set_option maxHeartbeats 800000 in +/-- **Unified VCPU invocation dispatcher (`vcpu.decode`) refinement on `VcpuTopPostA`**: +covers all 8 VCPU labels (`ARMVCPUSetTCB` 60, `ARMVCPUInjectIRQ` 61, `ARMVCPUReadReg` 62, +`ARMVCPUWriteReg` 63, `ARMVCPUAckVPPI` 64, `ARMVCPUReadState` 67, `ARMVCPUWriteState` 68, +`ARMVCPURun` 69) plus any illegal label against `fl4ArchDecodeInvocation`. -/ +theorem decInv_vcpu_jA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} + {OS U Ac Pp Tb Ub : Set U64} {Lv : U64 → Nat} {ia ie : Nat} + {s : S} {a : A} {σ : State Unit} {m : Fl4SpecA64.McState} {vc : U64} + (C : VcpuDecCtxA (L := L) (ar := ar) AR Z H OS U Ac Pp Tb Ub Lv ia ie s a σ m vc) + (hCar : CarryA Z (L.mem s) σ) + (hP : VcpuPlatAgreeA ar) (hAK : VcpuArchKeepsA ar AR) + (hVR : VcpuVmRootA st ar AR) (hS2R : VcpuStage2RootA st ar AR) (hAR : AR a Z.hw) + (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) + (hvcR : ∀ v ∈ Z.V, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → + wa (L.curW s) K ≠ v ∧ wa (L.curW s) K ≠ wa v 24#u64) + (hF : FrameMemRelA Z OS Ub (L.ksb s) (L.mem s) σ.machineState.underlyingMemory) + (hR : TcbRegsRelA (L.mem s) σ Z.T) + (hU : UbFootDisjA Z OS Ub) (hLD : TcbLinkDisjA Z (L.ksb s)) + (hUbUser : ∀ q ∈ Ub, inUserFrame (specPtr q) σ) + (hksTcb : ∀ t ∈ Z.T, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → + ∀ o : U64, o.val < ksSizeA → wa t K ≠ wa (L.ksb s) o) + (hSlotTcb : ∀ t ∈ Z.T, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → + ∀ y, SlotWA Z.th y → wa t K ≠ y ∧ wa t K ≠ wa y 8#u64 ∧ wa t K ≠ wa y 16#u64 ∧ wa t K ≠ wa y 24#u64) + (hOS_tcb : ∀ t ∈ Z.T, ∀ K : U64, (K = 3664#u64 ∨ K = 3672#u64) → ∀ y ∈ OS, + wa t K ≠ y ∧ wa t K ≠ wa y 8#u64 ∧ wa t K ≠ wa y 16#u64 ∧ wa t K ≠ wa y 24#u64) + (hStall : ∀ t ∈ Z.T, ∀ s' a', smp.remote_tcb_stall st ar s' a' t = ok (s', a')) + (hBufNotS2 : ∀ t ∈ Z.T, ∀ p sz r d mp asid va, + capAtA (L.mem s) (sbWordA t) ≠ .Frame p sz r d mp true asid va) + (hRunSched : ∀ t ∈ Z.T, L.mem s (runVcpuA t) ≠ 0#u64 → + L.mem s (scA t) ≠ 0#u64 ∧ L.rmax s (L.mem s (scA t)) ≠ 0#u64 ∧ + (L.mem s (inqA t) &&& sched.Q_RELEASE) = 0#u64 ∧ + specPtr t ∉ σ.releaseQueue ∧ + Aligned (maxA (L.mem s (scA t))) ∧ stA t ≠ maxA (L.mem s (scA t))) + (hCurNoRun : L.mem s (runVcpuA (L.curW s)) = 0#u64) + (hCurNotIdle : ∀ c ∈ Z.Cs, L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) ≠ L.curW s) + (hCurSched : L.mem s (scA (L.curW s)) ≠ 0#u64 ∧ L.rmax s (L.mem s (scA (L.curW s))) ≠ 0#u64 ∧ + (L.mem s (inqA (L.curW s)) &&& sched.Q_RELEASE) = 0#u64 ∧ + specPtr (L.curW s) ∉ σ.releaseQueue) + (hSlotSt : ∀ t ∈ Z.T, ∀ y, SlotWA Z.th y → stA t ≠ y ∧ stA t ≠ wa y 8#u64 ∧ stA t ≠ wa y 16#u64 ∧ stA t ≠ wa y 24#u64) + (hOS_st : ∀ t ∈ Z.T, ∀ y ∈ OS, stA t ≠ y ∧ stA t ≠ wa y 8#u64 ∧ stA t ≠ wa y 16#u64 ∧ stA t ≠ wa y 24#u64) + (hKs_st : ∀ t ∈ Z.T, ∀ o : U64, o.val < ksSizeA → stA t ≠ wa (L.ksb s) o) + (hSc_st : ∀ t ∈ Z.T, ∀ c ∈ Z.C, stA t ≠ wa c 24#u64 ∧ stA t ≠ wa c 72#u64 ∧ stA t ≠ maxA c ∧ Aligned (maxA c)) + (hSc_reg : ∀ c ∈ Z.C, Endp.regA (L.curW s) layout.reg.FAULT_IP ≠ maxA c) + (hVs_st : ∀ t ∈ Z.T, ¬ VsFootA Z.Pp Z.S1 Z.Y (L.ksb s) (stA t)) + (hExt_st : ∀ t ∈ Z.T, ¬ ExtFootE Z.X Z.Y Z.T2 (stA t)) + (hNtfn : ∀ xct, σ.kheap (specPtr (L.curW s)) = some (.tcb xct) → + FinishNotificationA (st := st) s σ (L.curW s) xct ∧ + (∀ n, L.mem s (boundA (L.curW s)) = n → n ≠ 0#u64 → + NtfnStateWordDisjA Z OS Ub (L.ksb s) n ∧ Aligned (ntfnBdA n) ∧ + (∀ b, (∃ i, i < 64 ∧ b = vcpuRegWA vc i) → b ≠ epTSA n ∧ b ≠ ntfnBdA n) ∧ + (∀ v_tcb ∈ Z.T, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → wa v_tcb K ≠ epTSA n ∧ wa v_tcb K ≠ ntfnBdA n) ∧ + (∀ b, VcPage Z.V b → b ≠ epTSA n ∧ b ≠ ntfnBdA n) ∧ + (∀ t ∈ Z.T, ∀ K : U64, K.val < 4096 → wa t K ≠ epTSA n ∧ wa t K ≠ ntfnBdA n))) + (hvc_cap : ∃ y ∈ OS, capAtA (L.mem s) y = .Vcpu vc) + {Rp Nt : Set U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} {slot : U64} + (Pl : SysPlace st H Z.Cs H.frame Rp Nt OS s extra slot) (Y : SymL st H Z.Cs s) + (hrun : Runnable (L.mem s (stA (L.curW s)))) + (hcur : (L.mem s (inqA (L.curW s)) &&& sched.Q_RELEASE) = 0#u64 ∧ L.mem s (scA (L.curW s)) ≠ 0#u64 ∧ + L.mem s (scA (L.curW s)) ∈ Ac) + (hvs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → Aligned b ∧ b ≠ stA (L.curW s)) + {lbl length buf : U64} {args : List U64} {ex : Array obj.Slot 3#usize} + {extras : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)} {xSlot : CapRef} {cte : CslotPtr} + (hlen : length.val = args.length) + (hargs : ∀ (i : U64) (w : U64), args[i.val]? = some w → msg.syscall_arg st s (L.curW s) buf i = ok w) + (hEx : ExtraOKA Z.th (L.mem s) ex extras) + (hExSlot : extras ≠ [] → exAtA ex 0 ≠ 0#u64 ∧ exAtA ex 0 ∈ OS) + (hExNotS2 : ∀ p sz r d mp asid va, + capAtA (L.mem s) (exAtA ex 0) ≠ .Frame p sz r d mp true asid va) + (hExTcb : ∀ ptr, capAtA (L.mem s) (exAtA ex 0) = .Thread ptr → + ptr ∈ Z.T ∧ ∀ c ∈ Z.Cs, L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) ≠ ptr) + (call : Bool) : + vcpu.decode st ar s a lbl length vc call buf ex ⦃ res => + VcpuTopPostA (L := L) AR Z OS m call σ + ((fl4ArchDecodeInvocation (Z := Unit) call (fl4LabelOf lbl.bv) (args.map UScalar.bv) + xSlot cte (.vcpuCap (specPtr vc)) extras).run σ) s res ⦄ := by + obtain ⟨hL60, hL61, hL62, hL63, hL64, hL67, hL68, hL69⟩ := fl4LabelOf_vcpu_eqA + have hcurT : L.curW s ∈ Z.T := C.E.j.1.curT (L.cpu s) C.E.j.1.sch.cpuCs + unfold vcpu.decode + by_cases h60 : lbl = fl4_abi.label.ARMVCPUSetTCB + · subst h60; rw [if_pos rfl] + have hdec : (fl4ArchDecodeInvocation (Z := Unit) call (fl4LabelOf fl4_abi.label.ARMVCPUSetTCB.bv) + (args.map UScalar.bv) xSlot cte (.vcpuCap (specPtr vc)) extras).run σ = + (decodeFl4VcpuSetTcb (specPtr vc) extras).run σ := by + unfold fl4ArchDecodeInvocation decodeFl4VcpuSetTcb; rw [hL60]; dsimp only [archL] + rw [if_neg (by decide), if_neg (by decide), if_neg (by decide), if_neg (by decide), + if_neg (by decide), if_neg (by decide), if_neg (by decide), if_pos rfl] + rfl + rw [hdec] + exact vcpu_set_tcb_jA C hCar hP hAK hVR hAR hF hR hU hLD hUbUser hksTcb hSlotTcb hOS_tcb + hStall hBufNotS2 hRunSched hSlotSt hOS_st hKs_st (fun t ht c hc => ⟨(hSc_st t ht c hc).1, (hSc_st t ht c hc).2.1⟩) + hVs_st hExt_st hvc_cap Pl Y hrun hcur hvs hEx hExSlot hExTcb call + · rw [if_neg h60] + by_cases h61 : lbl = fl4_abi.label.ARMVCPUInjectIRQ + · subst h61; rw [if_pos rfl] + have hdec : (fl4ArchDecodeInvocation (Z := Unit) call (fl4LabelOf fl4_abi.label.ARMVCPUInjectIRQ.bv) + (args.map UScalar.bv) xSlot cte (.vcpuCap (specPtr vc)) extras).run σ = + (NonDetE.liftME Fl4ArchInvocation.vcpu <| decodeFl4VcpuInjectIrq (Z := Unit) (specPtr vc) (args.map UScalar.bv)).run σ := by + unfold fl4ArchDecodeInvocation; rw [hL61]; simp [archL] + rw [hdec] + exact vcpu_inject_irq_jA hP hAK C Pl Y hrun hcur hvs length buf call args hlen hargs + · rw [if_neg h61] + by_cases h62 : lbl = fl4_abi.label.ARMVCPUReadReg + · subst h62; rw [if_pos rfl] + have hdec : (fl4ArchDecodeInvocation (Z := Unit) call (fl4LabelOf fl4_abi.label.ARMVCPUReadReg.bv) + (args.map UScalar.bv) xSlot cte (.vcpuCap (specPtr vc)) extras).run σ = + (NonDetE.liftME Fl4ArchInvocation.vcpu <| decodeFl4VcpuReadReg (Z := Unit) (specPtr vc) (args.map UScalar.bv)).run σ := by + unfold fl4ArchDecodeInvocation; rw [hL62]; simp [archL] + rw [hdec] + exact vcpu_read_reg_jA hP hAK C RS hvcR Pl Y hrun hcur hvs length buf call args hlen hargs + · rw [if_neg h62] + by_cases h63 : lbl = fl4_abi.label.ARMVCPUWriteReg + · subst h63; rw [if_pos rfl] + have hdec : (fl4ArchDecodeInvocation (Z := Unit) call (fl4LabelOf fl4_abi.label.ARMVCPUWriteReg.bv) + (args.map UScalar.bv) xSlot cte (.vcpuCap (specPtr vc)) extras).run σ = + (NonDetE.liftME Fl4ArchInvocation.vcpu <| decodeFl4VcpuWriteReg (Z := Unit) (specPtr vc) (args.map UScalar.bv)).run σ := by + unfold fl4ArchDecodeInvocation; rw [hL63]; simp [archL] + rw [hdec] + exact vcpu_write_reg_jA hP hAK C Pl Y hrun hcur hvs length buf call args hlen hargs + · rw [if_neg h63] + by_cases h64 : lbl = fl4_abi.label.ARMVCPUAckVPPI + · subst h64; rw [if_pos rfl] + have hdec : (fl4ArchDecodeInvocation (Z := Unit) call (fl4LabelOf fl4_abi.label.ARMVCPUAckVPPI.bv) + (args.map UScalar.bv) xSlot cte (.vcpuCap (specPtr vc)) extras).run σ = + (NonDetE.liftME Fl4ArchInvocation.vcpu <| decodeFl4VcpuAckVppi (Z := Unit) (specPtr vc) (args.map UScalar.bv)).run σ := by + unfold fl4ArchDecodeInvocation; rw [hL64]; simp [archL] + rw [hdec] + exact vcpu_ack_vppi_jA hAK C Pl Y hrun hcur hvs length buf call args hlen hargs + · rw [if_neg h64] + by_cases h67 : lbl = fl4_abi.label.ARMVCPUReadState + · subst h67; rw [if_pos rfl] + have hdec : (fl4ArchDecodeInvocation (Z := Unit) call (fl4LabelOf fl4_abi.label.ARMVCPUReadState.bv) + (args.map UScalar.bv) xSlot cte (.vcpuCap (specPtr vc)) extras).run σ = + (NonDetE.liftME Fl4ArchInvocation.vcpu <| decodeVcpuStateTransfer (Z := Unit) (specPtr vc) (args.map UScalar.bv) extras true).run σ := by + unfold fl4ArchDecodeInvocation; rw [hL67]; simp [archL] + rw [hdec] + exact vcpu_read_state_jA C hCar hP hAK hAR hF hR hU hUbUser hksTcb hSlotTcb + Pl Y hrun hcur hvs hlen hargs hEx hExSlot hExNotS2 call + · rw [if_neg h67] + by_cases h68 : lbl = fl4_abi.label.ARMVCPUWriteState + · subst h68; rw [if_pos rfl] + have hdec : (fl4ArchDecodeInvocation (Z := Unit) call (fl4LabelOf fl4_abi.label.ARMVCPUWriteState.bv) + (args.map UScalar.bv) xSlot cte (.vcpuCap (specPtr vc)) extras).run σ = + (NonDetE.liftME Fl4ArchInvocation.vcpu <| decodeVcpuStateTransfer (Z := Unit) (specPtr vc) (args.map UScalar.bv) extras false).run σ := by + unfold fl4ArchDecodeInvocation; rw [hL68]; simp [archL] + rw [hdec] + exact vcpu_write_state_jA C hCar hP hAK hAR hF hR hU hUbUser hksTcb hSlotTcb + Pl Y hrun hcur hvs hlen hargs hEx hExSlot hExNotS2 call + · rw [if_neg h68] + by_cases h69 : lbl = fl4_abi.label.ARMVCPURun + · subst h69; rw [if_pos rfl] + have hdec : (fl4ArchDecodeInvocation (Z := Unit) call (fl4LabelOf fl4_abi.label.ARMVCPURun.bv) + (args.map UScalar.bv) xSlot cte (.vcpuCap (specPtr vc)) extras).run σ = + (NonDetE.liftME Fl4ArchInvocation.vcpu <| decodeVcpuRun (Z := Unit) (specPtr vc) (args.map UScalar.bv) call).run σ := by + unfold fl4ArchDecodeInvocation; rw [hL69]; simp [archL] + rw [hdec] + exact vcpu_run_jA C hCar hP hAK hVR hS2R hAR hF hR hU hLD hUbUser hksTcb hSlotTcb hOS_tcb + hStall (hBufNotS2 _ hcurT) hCurNoRun hCurNotIdle hCurSched + (hSlotSt _ hcurT) (hOS_st _ hcurT) (hKs_st _ hcurT) (hSc_st _ hcurT) hSc_reg + (hVs_st _ hcurT) (hExt_st _ hcurT) hNtfn Pl Y hrun hcur hvs hlen hargs call + · rw [if_neg h69] + unfold error.illegal; rw [bind_ok, WP.spec_ok] + dsimp only [VcpuTopPostA] + have hD := fl4LabelOf_other_vcpu_illegalA lbl h60 h61 h62 h63 h64 h67 h68 h69 + call (args.map UScalar.bv) xSlot cte (specPtr vc) extras σ + exact ⟨rfl, hAR, .illegalOperation, hD, Or.inl trivial⟩ + +end vcpu_top + +end +end Fl4ProofsA64.P3.Vcpu diff --git a/proofs/Fl4ProofsA64/P3/VcpuUnbindJA.lean b/proofs/Fl4ProofsA64/P3/VcpuUnbindJA.lean new file mode 100644 index 000000000..8f68b8011 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/VcpuUnbindJA.lean @@ -0,0 +1,1390 @@ +/- +P3 (aarch64, A64-P3 S4 W2b-2): **VCPU unbind, finalise, detach, and `ARMVCPUSetTCB` (label 60) +refinement**. + +Provides: +- `regs_sanitise_spsr_eqA`: `regs.sanitise layout.reg.SPSR_EL1 v` matches `sanitiseRegister false .spsrEl1 v.bv`. +- `VcpuXferStateA.dissoc_bind`: lockstep clearing of `vc.TCB := 0`, `vc.OWNER_CPU := 0`, `t.VCPU := 0`, + and `t.SPSR_EL1` sanitisation (`fl4DissociateVcpuTcb`). +- `VcpuXferStateA.assoc_bind`: lockstep setting of `vc.TCB := t` and `t.VCPU := vc`. +- `unbind_refinesA`: `vcpu.unbind` refines `fl4VcpuEndRun` + optional `fl4DissociateVcpuTcb`. +- `fin_vcpu_jA`: `vcpu.finalise` refines `fl4VcpuEndRun` + optional `fl4DissociateVcpuTcb`. +- `detach_vcpu_jA`: `vcpu.detach` refines `fl4CancelRun` + optional `unbind`. +- `vcpu_set_tcb_jA`: label 60 `ARMVCPUSetTCB` (`vcpu.decode_set_tcb`) on `VcpuTopPostA` + (both `t = cur` and `t ≠ cur`, updating `Z.hw` via `VcpuVmRootA`). + +Named premises: `[P] VcpuPlatAgreeA ar`, `[P] VcpuArchKeepsA ar AR`, `[P] VcpuVmRootA st ar AR`, +`[S]` local-core `remote_tcb_stall` affinity. +-/ +import Fl4ProofsA64.P3.VcpuRunLinkJA + +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3.Vcpu + +open Fl4Spec Fl4SpecA64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3 Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Endp +open Fl4ProofsA64.P3.Notif + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## Section 1: `regs.sanitise` on `SPSR_EL1` -/ + +/-- `regs.sanitise layout.reg.SPSR_EL1 v` matches `sanitiseRegister false .spsrEl1 v.bv`. -/ +theorem regs_sanitise_spsr_eqA (v : U64) : + regs.sanitise layout.reg.SPSR_EL1 v = ok ⟨sanitiseRegister false .spsrEl1 v.bv⟩ := by + unfold regs.sanitise + rw [if_pos rfl, lift_ok, bind_ok] + have hpu : regs.PSTATE_USER = ok 320#u64 := by + unfold regs.PSTATE_USER + rw [u64_shl_i32_ok (by decide) (by decide), bind_ok, + u64_shl_i32_ok (by decide) (by decide), bind_ok] + congr 1 + rw [hpu, bind_ok] + congr 1 + apply u64_eq_of_bv_eq + unfold sanitiseRegister regs.PSTATE_FLAGS + simp only [Bool.false_eq_true, false_and, ↓reduceIte] + rfl + +/-! ## Section 2: Lockstep `dissoc_bind` and `assoc_bind` on `VcpuXferStateA` -/ + +section bind_ops +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + +/-- Lockstep dissociation of `(vc, t)`: clearing `vc.TCB := 0`, `vc.OWNER_CPU := 0`, `t.VCPU := 0`, +and sanitising `t.SPSR_EL1` matches `fl4DissociateVcpuTcb (specPtr vc) (specPtr t)` and +preserves `VcpuXferStateA`. -/ +theorem VcpuXferStateA.dissoc_bind {AR : A → HwRootA → Prop} {Z : XSetsA} + {OS Ub : Set U64} {m : Fl4SpecA64.McState} {a : A} {s : S} {σ : State Unit} + (X : VcpuXferStateA (L := L) AR Z OS Ub m a s σ) + (hU : UbFootDisjA Z OS Ub) (hLD : TcbLinkDisjA Z (L.ksb s)) + (hCs : ∀ c ∈ Z.Cs, c.val < 128) (hcpu : (L.cpu s).val < 128) + (hExt : ∀ b, VcPage Z.V b → ¬ ExtFootE Z.X Z.Y Z.T2 b) + (hSw : ∀ b, VcPage Z.V b → ∀ y, SlotWA Z.th y → + b ≠ y ∧ b ≠ wa y 8#u64 ∧ b ≠ wa y 16#u64 ∧ b ≠ wa y 24#u64) + (hfa : ∀ q, FootA (L := L) Z s q → Aligned q) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hIdleT : ∀ c ∈ Z.Cs, L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) ∈ Z.T) + (hksTcb : ∀ t ∈ Z.T, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → + ∀ o : U64, o.val < ksSizeA → wa t K ≠ wa (L.ksb s) o) + (hSlotTcb : ∀ t ∈ Z.T, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → + ∀ y, SlotWA Z.th y → wa t K ≠ y ∧ wa t K ≠ wa y 8#u64 ∧ wa t K ≠ wa y 16#u64 ∧ wa t K ≠ wa y 24#u64) + (hOS_tcb : ∀ t ∈ Z.T, ∀ K : U64, (K = 3664#u64 ∨ K = 3672#u64) → ∀ y ∈ OS, + wa t K ≠ y ∧ wa t K ≠ wa y 8#u64 ∧ wa t K ≠ wa y 16#u64 ∧ wa t K ≠ wa y 24#u64) + {vc t : U64} (hvc : vc ∈ Z.V) (ht : t ∈ Z.T) + (hlink : L.mem s vc = t) (hback : L.mem s (wa t 3664#u64) = vc) + {v : Vcpu} {x : Tcb} + (hkv : σ.kheap (specPtr vc) = some (.archObj (.vcpu v))) + (hkt : σ.kheap (specPtr t) = some (.tcb x)) : + let s1 := L.wr s vc 0#u64 + let s2 := L.wr s1 (wa vc 8#u64) 0#u64 + let s3 := L.wr s2 (wa t 3664#u64) 0#u64 + let spsr' : U64 := ⟨sanitiseRegister false .spsrEl1 (L.mem s3 (Endp.regA t (regIdxA .spsrEl1))).bv⟩ + let s4 := L.wr s3 (Endp.regA t (regIdxA .spsrEl1)) spsr' + let σ4 := updKSc (updKSc σ (specPtr t) (.tcb (dissociateTcbA x))) (specPtr vc) + (.archObj (.vcpu { v with vcpuTcb := none, vcpuExt := { v.vcpuExt with ownerCpu := 0 } })) + VcpuXferStateA (L := L) AR Z OS Ub m a s4 σ4 := by + dsimp only + set s1 := L.wr s vc 0#u64 + set s2 := L.wr s1 (wa vc 8#u64) 0#u64 + set s3 := L.wr s2 (wa t 3664#u64) 0#u64 + set spsr' : U64 := ⟨sanitiseRegister false .spsrEl1 (L.mem s3 (Endp.regA t (regIdxA .spsrEl1))).bv⟩ + set s4 := L.wr s3 (Endp.regA t (regIdxA .spsrEl1)) spsr' + set σ3 := updKSc (updKSc σ (specPtr t) (.tcb { x with tcbArch := { x.tcbArch with tcbVcpu := none } })) + (specPtr vc) (.archObj (.vcpu { v with vcpuTcb := none, vcpuExt := { v.vcpuExt with ownerCpu := 0 } })) + set σ4 := updKSc (updKSc σ (specPtr t) (.tcb (dissociateTcbA x))) (specPtr vc) + (.archObj (.vcpu { v with vcpuTcb := none, vcpuExt := { v.vcpuExt with ownerCpu := 0 } })) + have hne_vc_t : specPtr vc ≠ specPtr t := fun e => by rw [e, hkt] at hkv; cases hkv + have hwa0 : ∀ v' : U64, wa v' 0#u64 = v' := fun v' => by + apply UScalar.eq_of_val_eq; rw [wa_valP]; simp [UScalar.hBounds] + have hal_v0 : Aligned vc := X.vcpu.place.vc_al hvc + have hal_v8 : Aligned (wa vc 8#u64) := X.vcpu.place.vcPage_al (vcpuHdr_in_vcPage hvc (by decide) (by decide)) + have hta_t := hU.tcb_al t ht + have hal_tv : Aligned (wa t 3664#u64) := aligned_wa (by unfold Aligned; have := hta_t.align; omega) 3664#u64 (by decide) + have hksb3 : L.ksb s3 = L.ksb s := by simp [s3, s2, s1, L.ksb_wr] + have hcpu3 : L.cpu s3 = L.cpu s := by simp [s3, s2, s1, L.cpu_wr] + have hmem3 : ∀ b, Aligned b → b ≠ vc → b ≠ wa vc 8#u64 → b ≠ wa t 3664#u64 → L.mem s3 b = L.mem s b := by + intro b hb h1 h2 h3 + rw [mem_wr_neA hal_tv hb h3.symm, mem_wr_neA hal_v8 hb h2.symm, mem_wr_neA hal_v0 hb h1.symm] + -- First prove `X3 : VcpuXferStateA ... s3 σ3`, then apply `X3.tcb_reg_wr` for `SPSR_EL1`! + have hvp0 : VcPage Z.V vc := by rw [← hwa0 vc]; exact vcpuHdr_in_vcPage hvc (by decide) (by decide) + have hvp8 : VcPage Z.V (wa vc 8#u64) := vcpuHdr_in_vcPage hvc (by decide) (by decide) + have hnd_eq : ∀ c : U64, c.val < 128 → ∀ k : U64, k.val < 128 → k.val % 8 = 0 → + L.mem s3 (L.nodeAt s c k) = L.mem s (L.nodeAt s c k) := fun c hc k hk hk8 => by + obtain ⟨o, ho, heq⟩ := nodeAt_eq_wa_ksbA (L := L) s c k hc (by omega) + exact hmem3 _ (X.vcpu.place.node_al c k hk8) + (X.vcpu.place.disj_node X.j.1.ksFit hvp0 hc (by omega)).symm + (X.vcpu.place.disj_node X.j.1.ksFit hvp8 hc (by omega)).symm + (by rw [heq]; exact (hLD.offKs t ht 3664#u64 (Or.inl rfl) o ho).symm) + have hspec_tcb3 : ∀ u ∈ Z.T, ∀ xu, σ.kheap (specPtr u) = some (.tcb xu) → + ∃ x', σ3.kheap (specPtr u) = some (.tcb x') ∧ + x'.tcbState = xu.tcbState ∧ x'.tcbSchedContext = xu.tcbSchedContext ∧ + x'.tcbDomain = xu.tcbDomain ∧ x'.tcbPriority = xu.tcbPriority ∧ + (∀ i < tcbSlotsA, Fl4SpecA64.tcbCnodeMap x' (Fl4SpecA64.tcbCnodeIndex i) = + Fl4SpecA64.tcbCnodeMap xu (Fl4SpecA64.tcbCnodeIndex i)) := by + intro u hu xu hku + by_cases eu : u = t + · subst u; rw [hkt] at hku; cases hku + refine ⟨{ x with tcbArch := { x.tcbArch with tcbVcpu := none } }, ?_, ⟨rfl, rfl, rfl, rfl, fun _ _ => rfl⟩⟩ + rw [updKSc_other _ hne_vc_t.symm, updKSc_same] + · have hne1 : specPtr u ≠ specPtr vc := fun e => by rw [e, hkv] at hku; cases hku + have hne2 : specPtr u ≠ specPtr t := fun e => eu (specPtr_inj e) + exact ⟨xu, by rw [updKSc_other _ hne1, updKSc_other _ hne2]; exact hku, rfl, rfl, rfl, rfl, fun _ _ => rfl⟩ + have hspec_ty3 : TyKeepA σ σ3 := by + refine ⟨fun o r cs ho => ?_, fun o y ho => ?_⟩ + · have h1 : o ≠ specPtr vc := fun e => by subst e; rw [updKSc_same] at ho; cases ho + have h2 : o ≠ specPtr t := fun e => by subst e; rw [updKSc_other _ hne_vc_t.symm, updKSc_same] at ho; cases ho + rw [updKSc_other _ h1, updKSc_other _ h2] at ho; exact ⟨cs, ho⟩ + · by_cases e2 : o = specPtr t + · subst e2; exact ⟨x, hkt⟩ + · have h1 : o ≠ specPtr vc := fun e => by subst e; rw [updKSc_same] at ho; cases ho + rw [updKSc_other _ h1, updKSc_other _ e2] at ho; exact ⟨y, ho⟩ + obtain ⟨hJ3, hS23, hHV3, hTr3, hCar3, hF3⟩ := jAtZA_of_vcpuLinkStepA (σ' := σ3) X hksb3 hcpu3 + (fun y hy => by + obtain ⟨a0, a8, a16, a24⟩ := hSw _ hvp0 y hy + obtain ⟨b0, b8, b16, b24⟩ := hSw _ hvp8 y hy + obtain ⟨c0, c8, c16, c24⟩ := hLD.offSlot t ht 3664#u64 (Or.inl rfl) y hy + exact ⟨hmem3 _ (hfa _ (.slot y _ hy (Or.inl rfl))) a0.symm b0.symm c0.symm, + hmem3 _ (hfa _ (.slot y _ hy (Or.inr (Or.inl rfl)))) a8.symm b8.symm c8.symm, + hmem3 _ (hfa _ (.slot y _ hy (Or.inr (Or.inr (Or.inl rfl))))) a16.symm b16.symm c16.symm, + hmem3 _ (hfa _ (.slot y _ hy (Or.inr (Or.inr (Or.inr rfl))))) a24.symm b24.symm c24.symm⟩) + (fun y hy => by + have hya := hU.al_os y hy + have a := X.vcpu.place.disj_os _ hvp0 y hy + have b := X.vcpu.place.disj_os _ hvp8 y hy + obtain ⟨c0, c8, c16, c24⟩ := hOS_tcb t ht 3664#u64 (Or.inl rfl) y hy + exact ⟨hmem3 _ hya a.1.symm b.1.symm c0.symm, + hmem3 _ (aligned_wa hya 8#u64 (by decide)) a.2.1.symm b.2.1.symm c8.symm, + hmem3 _ (aligned_wa hya 16#u64 (by decide)) a.2.2.1.symm b.2.2.1.symm c16.symm, + hmem3 _ (aligned_wa hya 24#u64 (by decide)) a.2.2.2.symm b.2.2.2.symm c24.symm⟩) + (fun c hc => ⟨hnd_eq c (hCs c hc) layout.node.CUR_THREAD (by unfold layout.node.CUR_THREAD; decide) (by unfold layout.node.CUR_THREAD; decide), + hnd_eq c (hCs c hc) layout.node.IDLE_THREAD (by unfold layout.node.IDLE_THREAD; decide) (by unfold layout.node.IDLE_THREAD; decide), + hnd_eq c (hCs c hc) layout.node.CUR_SC (by unfold layout.node.CUR_SC; decide) (by unfold layout.node.CUR_SC; decide), + hnd_eq c (hCs c hc) layout.node.IDLE_SC (by unfold layout.node.IDLE_SC; decide) (by unfold layout.node.IDLE_SC; decide)⟩) + (fun u hu => by + have hua := hU.tcb_al u hu + exact ⟨hmem3 _ (by rw [stA_eq]; exact aligned_wa (X.vcpu.place.al_t u hu) 3328#u64 (by decide)) + (by rw [stA_eq, ← hwa0 vc]; exact (X.vcpu.place.disj_tcb vc hvc u hu 0#u64 3328#u64 (by decide) (by decide)).symm) + (by rw [stA_eq]; exact (X.vcpu.place.disj_tcb vc hvc u hu 8#u64 3328#u64 (by decide) (by decide)).symm) + (by rw [stA_eq]; exact (field_ne hta_t hua (by decide) (by decide) (fun _ => by decide)).symm), + hmem3 _ (by unfold scA; exact aligned_wa (X.vcpu.place.al_t u hu) 3464#u64 (by decide)) + (by unfold scA; rw [← hwa0 vc]; exact (X.vcpu.place.disj_tcb vc hvc u hu 0#u64 3464#u64 (by decide) (by decide)).symm) + (by unfold scA; exact (X.vcpu.place.disj_tcb vc hvc u hu 8#u64 3464#u64 (by decide) (by decide)).symm) + (by unfold scA; exact (field_ne hta_t hua (by decide) (by decide) (fun _ => by decide)).symm), + hmem3 _ (by unfold affA; exact aligned_wa (X.vcpu.place.al_t u hu) 3520#u64 (by decide)) + (by unfold affA; rw [← hwa0 vc]; exact (X.vcpu.place.disj_tcb vc hvc u hu 0#u64 3520#u64 (by decide) (by decide)).symm) + (by unfold affA; exact (X.vcpu.place.disj_tcb vc hvc u hu 8#u64 3520#u64 (by decide) (by decide)).symm) + (by unfold affA; exact (field_ne hta_t hua (by decide) (by decide) (fun _ => by decide)).symm), + hmem3 _ (hfa _ (.nslot _ (.tcb2 u _ hu (Or.inl rfl)))) + (by unfold prioA; rw [← hwa0 vc]; exact (X.vcpu.place.disj_tcb vc hvc u hu 0#u64 3448#u64 (by decide) (by decide)).symm) + (by unfold prioA; exact (X.vcpu.place.disj_tcb vc hvc u hu 8#u64 3448#u64 (by decide) (by decide)).symm) + (by unfold prioA; exact (field_ne hta_t hua (by decide) (by decide) (fun _ => by decide)).symm), + hmem3 _ (hfa _ (.nslot _ (.tcb2 u _ hu (Or.inr rfl)))) + (by unfold domA; rw [← hwa0 vc]; exact (X.vcpu.place.disj_tcb vc hvc u hu 0#u64 3432#u64 (by decide) (by decide)).symm) + (by unfold domA; exact (X.vcpu.place.disj_tcb vc hvc u hu 8#u64 3432#u64 (by decide) (by decide)).symm) + (by unfold domA; exact (field_ne hta_t hua (by decide) (by decide) (fun _ => by decide)).symm)⟩) + (fun c hc => by + obtain ⟨c24, c32⟩ := hLD.offSc t ht 3664#u64 (Or.inl rfl) c hc + exact ⟨hmem3 _ (hfa _ (.nslot _ (.sc c hc))) + (by rw [← hwa0 vc]; exact (X.vcpu.place.disj_sc vc hvc c hc 0#u64 24#u64 (by decide) (by decide)).symm) + (X.vcpu.place.disj_sc vc hvc c hc 8#u64 24#u64 (by decide) (by decide)).symm c24.symm, + hmem3 _ (hfa _ (.nslot _ (.sc2 c hc))) + (by rw [← hwa0 vc]; exact (X.vcpu.place.disj_sc vc hvc c hc 0#u64 72#u64 (by decide) (by decide)).symm) + (X.vcpu.place.disj_sc vc hvc c hc 8#u64 72#u64 (by decide) (by decide)).symm c32.symm⟩) + ⟨hnd_eq (L.cpu s) hcpu layout.node.SCHED_ACTION (by unfold layout.node.SCHED_ACTION; decide) (by unfold layout.node.SCHED_ACTION; decide), + hnd_eq (L.cpu s) hcpu layout.node.CUR_TIME (by unfold layout.node.CUR_TIME; decide) (by unfold layout.node.CUR_TIME; decide), + hnd_eq (L.cpu s) hcpu layout.node.CONSUMED (by unfold layout.node.CONSUMED; decide) (by unfold layout.node.CONSUMED; decide), + hnd_eq (L.cpu s) hcpu layout.node.REPROGRAM (by unfold layout.node.REPROGRAM; decide) (by unfold layout.node.REPROGRAM; decide)⟩ + ⟨hmem3 _ (hfa _ (.nslot _ (.glob _ (Or.inl rfl)))) + (vc_disj_ksA X.vcpu.place X.j.1.ksFit hvp0 8#u64 (by unfold ksSizeA; decide)).symm + (vc_disj_ksA X.vcpu.place X.j.1.ksFit hvp8 8#u64 (by unfold ksSizeA; decide)).symm + (hLD.offKs t ht 3664#u64 (Or.inl rfl) 8#u64 (by unfold ksSizeA; decide)).symm, + hmem3 _ (hfa _ (.nslot _ (.glob _ (Or.inr rfl)))) + (vc_disj_ksA X.vcpu.place X.j.1.ksFit hvp0 16#u64 (by unfold ksSizeA; decide)).symm + (vc_disj_ksA X.vcpu.place X.j.1.ksFit hvp8 16#u64 (by unfold ksSizeA; decide)).symm + (hLD.offKs t ht 3664#u64 (Or.inl rfl) 16#u64 (by unfold ksSizeA; decide)).symm⟩ + (fun i hi => by + have ho : (irqOffA i).val < ksSizeA := by + unfold irqOffA ksSizeA + simp only [core.num.U64.wrapping_add_val_eq, core.num.U64.wrapping_mul_val_eq, + UScalar.size, UScalarTy.numBits] + have h16 : (224#u64).val = 224 := rfl + have h8 : (8#u64).val = 8 := rfl + rw [h16, h8]; omega + exact hmem3 _ (hfa _ (.nslot _ (.irq i hi))) + (vc_disj_ksA X.vcpu.place X.j.1.ksFit hvp0 (irqOffA i) ho).symm + (vc_disj_ksA X.vcpu.place X.j.1.ksFit hvp8 (irqOffA i) ho).symm + (hLD.offKs t ht 3664#u64 (Or.inl rfl) (irqOffA i) ho).symm) + (fun b hb => hmem3 _ (hfa _ (.nslot _ (.vs b hb))) + (fun e => X.vcpu.place.disj_vs _ hvp0 (e.symm ▸ hb)) + (fun e => X.vcpu.place.disj_vs _ hvp8 (e.symm ▸ hb)) + (fun e => hLD.offVs t ht 3664#u64 (Or.inl rfl) (e.symm ▸ hb))) + (fun b hb => hmem3 _ (hfa _ (.nslot _ (.ext b hb))) + (fun e => hExt _ hvp0 (e.symm ▸ hb)) + (fun e => hExt _ hvp8 (e.symm ▸ hb)) + (fun e => hLD.offExt t ht 3664#u64 (Or.inl rfl) (e.symm ▸ hb))) + (fun b hb => hmem3 _ (X.fmem.aligned b hb) + (vc_disj_ubA X.vcpu.place hvp0 hb).symm + (vc_disj_ubA X.vcpu.place hvp8 hb).symm + (fun e => X.fmem.disj_tcb b hb t ht 3664#u64 (by decide) e)) + ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, ⟨rfl, rfl, rfl, rfl⟩⟩ rfl rfl ⟨rfl, rfl⟩ + (fun p hnv hnt => by + rw [updKSc_other _ (fun e => hnv vc hvc e.symm), updKSc_other _ (fun e => hnt t ht e.symm)]) + hspec_tcb3 hspec_ty3 + (fun p es hp => by + have h1 : p ≠ specPtr vc := fun e => by subst e; rw [updKSc_same] at hp; cases hp + have h2 : p ≠ specPtr t := fun e => by subst e; rw [updKSc_other _ hne_vc_t.symm, updKSc_same] at hp; cases hp + rw [updKSc_other _ h1, updKSc_other _ h2] at hp; exact hp) + (fun p l1 hp => by + have h1 : p ≠ specPtr vc := fun e => by subst e; rw [updKSc_same] at hp; cases hp + have h2 : p ≠ specPtr t := fun e => by subst e; rw [updKSc_other _ hne_vc_t.symm, updKSc_same] at hp; cases hp + rw [updKSc_other _ h1, updKSc_other _ h2] at hp; exact hp) + hsub + have hvc_mem3 : ∀ v' ∈ Z.V, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → + (v' ≠ vc ∨ (o ≠ 0#u64 ∧ o ≠ 8#u64)) → L.mem s3 (wa v' o) = L.mem s (wa v' o) := fun v' hv' o ho ho8 hne => by + have h1 : wa v' o ≠ vc := by + rw [← hwa0 vc] + by_cases ev : v' = vc + · subst ev + rcases hne with hvne | ⟨ho0, -⟩ + · exact absurd rfl hvne + · exact X.vcpu.place.same_vcpu_ne hvc ho (by decide) ho0 + · exact X.vcpu.place.disj_vcpu hv' hvc ev ho (by decide) + have h2 : wa v' o ≠ wa vc 8#u64 := by + by_cases ev : v' = vc + · subst ev + rcases hne with hvne | ⟨-, ho8'⟩ + · exact absurd rfl hvne + · exact X.vcpu.place.same_vcpu_ne hvc ho (by decide) ho8' + · exact X.vcpu.place.disj_vcpu hv' hvc ev ho (by decide) + have h3 : wa v' o ≠ wa t 3664#u64 := X.vcpu.place.disj_tcb v' hv' t ht o 3664#u64 ho (by decide) + exact hmem3 _ (aligned_wa (X.vcpu.place.vc_al hv') o ho8) h1 h2 h3 + have hv0_same : L.mem s3 vc = 0#u64 := by + have hne80 : wa vc 8#u64 ≠ vc := by + have h := X.vcpu.place.same_vcpu_ne hvc (o₁ := 8#u64) (o₂ := 0#u64) (by decide) (by decide) (by decide) + rwa [hwa0] at h + rw [mem_wr_neA hal_tv hal_v0 (by rw [← hwa0 vc]; exact (X.vcpu.place.disj_tcb vc hvc t ht 0#u64 3664#u64 (by decide) (by decide)).symm), + mem_wr_neA hal_v8 hal_v0 hne80, + L.mem_wr_same hal_v0] + have hv8_same : L.mem s3 (wa vc 8#u64) = 0#u64 := by + rw [mem_wr_neA hal_tv hal_v8 (X.vcpu.place.disj_tcb vc hvc t ht 8#u64 3664#u64 (by decide) (by decide)).symm, + L.mem_wr_same hal_v8] + have htv_same : L.mem s3 (wa t 3664#u64) = 0#u64 := L.mem_wr_same hal_tv + have htv_ne : ∀ u ∈ Z.T, u ≠ t → L.mem s3 (wa u 3664#u64) = L.mem s (wa u 3664#u64) := fun u hu hne => + hmem3 _ (aligned_wa (X.vcpu.place.al_t u hu) 3664#u64 (by decide)) + (by rw [← hwa0 vc]; exact (X.vcpu.place.disj_tcb vc hvc u hu 0#u64 3664#u64 (by decide) (by decide)).symm) + (X.vcpu.place.disj_tcb vc hvc u hu 8#u64 3664#u64 (by decide) (by decide)).symm + (field_ne (hU.tcb_al u hu) hta_t (by decide) (by decide) (fun e => absurd e hne)) + have hrv_all : ∀ u ∈ Z.T, L.mem s3 (runVcpuA u) = L.mem s (runVcpuA u) := fun u hu => by + have hreq : runVcpuA u = wa u 3672#u64 := by unfold runVcpuA; rfl + rw [hreq] + exact hmem3 _ (aligned_wa (X.vcpu.place.al_t u hu) 3672#u64 (by decide)) + (by rw [← hwa0 vc]; exact (X.vcpu.place.disj_tcb vc hvc u hu 0#u64 3672#u64 (by decide) (by decide)).symm) + (X.vcpu.place.disj_tcb vc hvc u hu 8#u64 3672#u64 (by decide) (by decide)).symm + (field_ne (hU.tcb_al u hu) hta_t (by decide) (by decide) (fun _ => by decide)) + have hVcore3 : VcpuCoreA (L := L) Z s3 σ3 := by + refine ⟨?_, ?_, ?_, ?_⟩ + · intro v' hv' + by_cases ev : v' = vc + · subst v' + refine ⟨{ v with vcpuTcb := none, vcpuExt := { v.vcpuExt with ownerCpu := 0 } }, updKSc_same _ _ _, + by rw [hv0_same]; rfl, ?_⟩ + rw [hvc_mem3 vc hvc 24#u64 (by decide) (by decide) (Or.inr ⟨by decide, by decide⟩)] + obtain ⟨v0, hkv0, -, hr0⟩ := X.vcpu.core.obj vc hvc; rw [hkv] at hkv0; cases hkv0; exact hr0 + · have hne1 : specPtr v' ≠ specPtr vc := fun e => ev (specPtr_inj e) + have hne2 : specPtr v' ≠ specPtr t := fun e => by + obtain ⟨v0, hkv0, -, -⟩ := X.vcpu.core.obj v' hv'; rw [e, hkt] at hkv0; cases hkv0 + obtain ⟨v0, hkv0, ht0, hr0⟩ := X.vcpu.core.obj v' hv' + exact ⟨v0, by rw [updKSc_other _ hne1, updKSc_other _ hne2]; exact hkv0, + by rw [← hwa0 v', hvc_mem3 v' hv' 0#u64 (by decide) (by decide) (Or.inl ev), hwa0]; exact ht0, + by rw [hvc_mem3 v' hv' 24#u64 (by decide) (by decide) (Or.inl ev)]; exact hr0⟩ + · intro p xv hkp + by_cases ep : p = specPtr vc + · subst ep; exact ⟨vc, hvc, rfl⟩ + · have hne2 : p ≠ specPtr t := fun e => by subst e; rw [updKSc_other _ ep, updKSc_same] at hkp; cases hkp + rw [updKSc_other _ ep, updKSc_other _ hne2] at hkp + exact X.vcpu.core.back p xv hkp + · intro u hu + by_cases eu : u = t + · subst u + refine ⟨{ x with tcbArch := { x.tcbArch with tcbVcpu := none } }, + by rw [updKSc_other _ hne_vc_t.symm, updKSc_same], by rw [htv_same]; rfl, ?_⟩ + rw [hrv_all t ht] + obtain ⟨x0, hkt0, -, hr0⟩ := X.vcpu.core.tcb t ht; rw [hkt] at hkt0; cases hkt0; exact hr0 + · have hne1 : specPtr u ≠ specPtr vc := fun e => by + obtain ⟨x0, hkt0, -, -⟩ := X.vcpu.core.tcb u hu; rw [e, hkv] at hkt0; cases hkt0 + have hne2 : specPtr u ≠ specPtr t := fun e => eu (specPtr_inj e) + obtain ⟨x0, hkt0, hv0, hr0⟩ := X.vcpu.core.tcb u hu + exact ⟨x0, by rw [updKSc_other _ hne1, updKSc_other _ hne2]; exact hkt0, + by rw [htv_ne u hu eu]; exact hv0, by rw [hrv_all u hu]; exact hr0⟩ + · intro y hy p hcap + have hc' : capAtA (L.mem s3) y = capAtA (L.mem s) y := by + obtain ⟨a0, a8, -, -⟩ := hSw _ hvp0 y (Or.inl hy) + obtain ⟨b0, b8, -, -⟩ := hSw _ hvp8 y (Or.inl hy) + obtain ⟨c0, c8, -, -⟩ := hLD.offSlot t ht 3664#u64 (Or.inl rfl) y (Or.inl hy) + unfold capAtA + rw [hmem3 _ (hfa _ (.slot y _ (Or.inl hy) (Or.inl rfl))) a0.symm b0.symm c0.symm, + hmem3 _ (hfa _ (.slot y _ (Or.inl hy) (Or.inr (Or.inl rfl)))) a8.symm b8.symm c8.symm] + rw [hc'] at hcap; exact X.vcpu.core.cap y hy p hcap + have hkv_cases3 : ∀ v' ∈ Z.V, ∀ x', σ3.kheap (specPtr v') = some (.archObj (.vcpu x')) → + (v' = vc ∧ x' = { v with vcpuTcb := none, vcpuExt := { v.vcpuExt with ownerCpu := 0 } }) ∨ + (v' ≠ vc ∧ σ.kheap (specPtr v') = some (.archObj (.vcpu x'))) := by + intro v' hv' x' hkx + by_cases ev : v' = vc + · subst v'; rw [updKSc_same] at hkx; cases hkx; exact Or.inl ⟨rfl, rfl⟩ + · have hne1 : specPtr v' ≠ specPtr vc := fun e => ev (specPtr_inj e) + have hne2 : specPtr v' ≠ specPtr t := fun e => by + obtain ⟨v0, hkv0, -, -⟩ := X.vcpu.core.obj v' hv'; rw [e, hkt] at hkv0; cases hkv0 + rw [updKSc_other _ hne1, updKSc_other _ hne2] at hkx; exact Or.inr ⟨ev, hkx⟩ + have hVrel3 : VcpuRelA (L := L) Z OS Ub s3 σ3 := by + refine ⟨hVcore3, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, X.vcpu.arch, by rw [hksb3]; exact X.vcpu.place⟩ + · intro v' hv' + by_cases ev : v' = vc + · subst v'; rw [hv8_same]; decide + · rw [hvc_mem3 v' hv' 8#u64 (by decide) (by decide) (Or.inl ev)]; exact X.vcpu.owner v' hv' + · intro v' hv' x' hkx + rw [hvc_mem3 v' hv' 16#u64 (by decide) (by decide) (Or.inr ⟨by decide, by decide⟩)] + rcases hkv_cases3 v' hv' x' hkx with ⟨rfl, rfl⟩ | ⟨-, h0⟩ + · exact X.vcpu.vppi v' hv' v hkv + · exact X.vcpu.vppi v' hv' x' h0 + · intro v' hv' x' hkx + rw [hvc_mem3 v' hv' 32#u64 (by decide) (by decide) (Or.inr ⟨by decide, by decide⟩)] + rcases hkv_cases3 v' hv' x' hkx with ⟨rfl, rfl⟩ | ⟨-, h0⟩ + · exact X.vcpu.runOff v' hv' v hkv + · exact X.vcpu.runOff v' hv' x' h0 + · intro v' hv' x' hkx + rw [hvc_mem3 v' hv' 40#u64 (by decide) (by decide) (Or.inr ⟨by decide, by decide⟩)] + rcases hkv_cases3 v' hv' x' hkx with ⟨rfl, rfl⟩ | ⟨-, h0⟩ + · exact X.vcpu.runOut v' hv' v hkv + · exact X.vcpu.runOut v' hv' x' h0 + · intro v' hv' x' hkx i hi + have hlt : (vcpuRegOffA i).val < 4096 := by rw [vcpuRegOffA_val hi]; omega + have h8 : (vcpuRegOffA i).val % 8 = 0 := by rw [vcpuRegOffA_val hi]; omega + have hne08 : vcpuRegOffA i ≠ 0#u64 ∧ vcpuRegOffA i ≠ 8#u64 := ⟨ + fun e => by have := congrArg UScalar.val e; rw [vcpuRegOffA_val hi] at this; have : (0#u64).val = 0 := rfl; omega, + fun e => by have := congrArg UScalar.val e; rw [vcpuRegOffA_val hi] at this; have : (8#u64).val = 8 := rfl; omega⟩ + rw [vcpuRegWA, hvc_mem3 v' hv' (vcpuRegOffA i) hlt h8 (Or.inr hne08)] + rcases hkv_cases3 v' hv' x' hkx with ⟨rfl, rfl⟩ | ⟨-, h0⟩ + · exact X.vcpu.regs v' hv' v hkv i hi + · exact X.vcpu.regs v' hv' x' h0 i hi + · intro v' hv' u hu hne + by_cases ev : v' = vc + · subst v'; rw [hv0_same] at hu; exact absurd hu.symm hne + · rw [← hwa0 v', hvc_mem3 v' hv' 0#u64 (by decide) (by decide) (Or.inl ev), hwa0] at hu + obtain ⟨huT, huv⟩ := X.vcpu.bind_v2t v' hv' u hu hne + have hne_u : u ≠ t := fun eu => by subst eu; rw [hback] at huv; exact ev huv.symm + exact ⟨huT, by rw [htv_ne u huT hne_u]; exact huv⟩ + · intro u hu v' hv' hne + by_cases eu : u = t + · subst u; rw [htv_same] at hv'; exact absurd hv'.symm hne + · rw [htv_ne u hu eu] at hv' + obtain ⟨hvV, hvu⟩ := X.vcpu.bind_t2v u hu v' hv' hne + have hne_v : v' ≠ vc := fun ev => by subst ev; rw [hlink] at hvu; exact eu hvu.symm + exact ⟨hvV, by rw [← hwa0 v', hvc_mem3 v' hvV 0#u64 (by decide) (by decide) (Or.inl hne_v), hwa0]; exact hvu⟩ + · intro v' hv' c hc hne + rw [hvc_mem3 v' hv' 24#u64 (by decide) (by decide) (Or.inr ⟨by decide, by decide⟩)] at hc + obtain ⟨hcT, hcv⟩ := X.vcpu.run_v2c v' hv' c hc hne + exact ⟨hcT, by rw [hrv_all c hcT]; exact hcv⟩ + · intro c hc v' hv' hne + rw [hrv_all c hc] at hv' + obtain ⟨hvV, hvc'⟩ := X.vcpu.run_c2v c hc v' hv' hne + exact ⟨hvV, by rw [hvc_mem3 v' hvV 24#u64 (by decide) (by decide) (Or.inr ⟨by decide, by decide⟩)]; exact hvc'⟩ + · intro c hc hne + rw [hrv_all c hc] at hne + have hst_eq' : L.mem s3 (stA c) = L.mem s (stA c) := + hmem3 _ (by rw [stA_eq]; exact aligned_wa (X.vcpu.place.al_t c hc) 3328#u64 (by decide)) + (by rw [stA_eq, ← hwa0 vc]; exact (X.vcpu.place.disj_tcb vc hvc c hc 0#u64 3328#u64 (by decide) (by decide)).symm) + (by rw [stA_eq]; exact (X.vcpu.place.disj_tcb vc hvc c hc 8#u64 3328#u64 (by decide) (by decide)).symm) + (by rw [stA_eq]; exact (field_ne hta_t (hU.tcb_al c hc) (by decide) (by decide) (fun _ => by decide)).symm) + rw [hst_eq']; exact X.vcpu.run_state c hc hne + · intro u hu hne + by_cases eu : u = t + · subst u; rw [htv_same] at hne; exact absurd rfl hne + · rw [htv_ne u hu eu] at hne; rw [hrv_all u hu]; exact X.vcpu.bind_run_disj u hu hne + · intro c hc + have hni : L.nodeAt s3 c layout.node.IDLE_THREAD = L.nodeAt s c layout.node.IDLE_THREAD := by + unfold LawfulStore.nodeAt; rw [hksb3] + rw [hni, hnd_eq c (hCs c hc) layout.node.IDLE_THREAD (by unfold layout.node.IDLE_THREAD; decide) (by unfold layout.node.IDLE_THREAD; decide)] + have hidT := hIdleT c hc + refine ⟨?_, by rw [hrv_all _ hidT]; exact (X.vcpu.idle_no_vcpu c hc).2⟩ + by_cases eu : L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) = t + · rw [eu]; exact htv_same + · rw [htv_ne _ hidT eu]; exact (X.vcpu.idle_no_vcpu c hc).1 + · intro v' hv' hor + have hor0 : L.mem s v' ≠ 0#u64 ∨ L.mem s (wa v' 24#u64) ≠ 0#u64 := by + rcases hor with h | h + · by_cases ev : v' = vc + · subst v'; rw [hv0_same] at h; exact absurd rfl h + · rw [← hwa0 v', hvc_mem3 v' hv' 0#u64 (by decide) (by decide) (Or.inl ev), hwa0] at h; exact Or.inl h + · rw [hvc_mem3 v' hv' 24#u64 (by decide) (by decide) (Or.inr ⟨by decide, by decide⟩)] at h; exact Or.inr h + obtain ⟨y, hy, hcap⟩ := X.vcpu.live v' hv' hor0 + refine ⟨y, hy, ?_⟩ + have hya := hU.al_os y hy + have a := X.vcpu.place.disj_os _ hvp0 y hy + have b := X.vcpu.place.disj_os _ hvp8 y hy + obtain ⟨c0, c8, -, -⟩ := hOS_tcb t ht 3664#u64 (Or.inl rfl) y hy + unfold capAtA + rw [hmem3 _ hya a.1.symm b.1.symm c0.symm, + hmem3 _ (aligned_wa hya 8#u64 (by decide)) a.2.1.symm b.2.1.symm c8.symm] + exact hcap + have hRegs3 : TcbRegsRelA (L.mem s3) σ3 Z.T := by + intro u hu xu hku r + obtain ⟨hKval, hreg_eq⟩ := regA_eq_wa_KA u r + have hlt := regIdxA_lt r + have hua := hU.tcb_al u hu + have hal_r : Aligned (Endp.regA u (regIdxA r)) := by + rw [hreg_eq]; exact aligned_wa (by unfold Aligned; have := hua.align; omega) _ (by omega) + rw [hmem3 _ hal_r + (by rw [hreg_eq, ← hwa0 vc]; exact (X.vcpu.place.disj_tcb vc hvc u hu 0#u64 _ (by decide) (by omega)).symm) + (by rw [hreg_eq]; exact (X.vcpu.place.disj_tcb vc hvc u hu 8#u64 _ (by decide) (by omega)).symm) + (by rw [hreg_eq]; exact field_ne hua hta_t (by omega) (by decide) (fun _ => by have h3664 : (3664#u64 : U64).val = 3664 := rfl; omega))] + by_cases eu : u = t + · subst u; rw [updKSc_other _ hne_vc_t.symm, updKSc_same] at hku; cases hku + exact X.regs t ht x hkt r + · have hne1 : specPtr u ≠ specPtr vc := fun e => by + obtain ⟨x0, hkt0, -, -⟩ := X.vcpu.core.tcb u hu; rw [e, hkv] at hkt0; cases hkt0 + have hne2 : specPtr u ≠ specPtr t := fun e => eu (specPtr_inj e) + rw [updKSc_other _ hne1, updKSc_other _ hne2] at hku + exact X.regs u hu xu hku r + have X3 : VcpuXferStateA (L := L) AR Z OS Ub m a s3 σ3 := + ⟨⟨hJ3, hVcore3, hS23, hHV3⟩, hTr3, hCar3, hVrel3, hF3, hRegs3⟩ + -- Now apply `X3.tcb_reg_wr` for `.spsrEl1` with `spsr'`! + have hkt3 : σ3.kheap (specPtr t) = some (.tcb { x with tcbArch := { x.tcbArch with tcbVcpu := none } }) := by + rw [updKSc_other _ hne_vc_t.symm, updKSc_same] + have X4 := X3.tcb_reg_wr hU hCs (by rw [hcpu3]; exact hcpu) + (fun q hq => hfa q (footA_of_ksb_cpuA hksb3 hcpu3 hq)) hsub + (fun c hc => X3.j.1.idleT c hc) (by rw [hksb3]; exact hksTcb) hSlotTcb ht hkt3 .spsrEl1 spsr' + -- Show `ctxUpdA σ3 (specPtr t) _ _ = σ4` + have hspsr_eq : (L.mem s3 (Endp.regA t (regIdxA .spsrEl1))).bv = x.tcbArch.tcbContext.userRegs .spsrEl1 := + X3.regs t ht _ hkt3 .spsrEl1 + have hσ4_eq : ctxUpdA σ3 (specPtr t) { x with tcbArch := { x.tcbArch with tcbVcpu := none } } + { x.tcbArch.tcbContext with userRegs := Function.update x.tcbArch.tcbContext.userRegs .spsrEl1 spsr'.bv } = σ4 := by + have hspsr_bv : spsr'.bv = sanitiseRegister false .spsrEl1 (x.tcbArch.tcbContext.userRegs .spsrEl1) := by + change sanitiseRegister false .spsrEl1 (L.mem s3 (Endp.regA t (regIdxA .spsrEl1))).bv = _ + rw [hspsr_eq] + have huc_eq : ({ x.tcbArch.tcbContext with userRegs := Function.update x.tcbArch.tcbContext.userRegs .spsrEl1 spsr'.bv } : UserContext) = + ⟨x.tcbArch.tcbContext.userFpuState, + Function.update x.tcbArch.tcbContext.userRegs .spsrEl1 (sanitiseRegister false .spsrEl1 (x.tcbArch.tcbContext.userRegs .spsrEl1))⟩ := by + rw [hspsr_bv] + have hkh : (ctxUpdA σ3 (specPtr t) { x with tcbArch := { x.tcbArch with tcbVcpu := none } } + { x.tcbArch.tcbContext with userRegs := Function.update x.tcbArch.tcbContext.userRegs .spsrEl1 spsr'.bv }).kheap = σ4.kheap := by + funext p + unfold ctxUpdA setObjA σ4 σ3 dissociateTcbA updKSc + dsimp only + by_cases ep : p = specPtr t + · subst ep; simp [Function.update, hne_vc_t.symm, huc_eq] + · by_cases ev : p = specPtr vc + · subst ev; simp [Function.update, hne_vc_t] + · simp [Function.update, ep, ev] + cases σ with + | mk a ex => + cases a + unfold ctxUpdA setObjA σ4 σ3 dissociateTcbA updKSc at hkh ⊢ + dsimp only at hkh ⊢ + rw [hkh] + exact hσ4_eq ▸ X4 + +/-- Lockstep association of `(vc, t)`: given `vc` unbound (`L.mem s vc = 0`), `t` unbound +(`L.mem s (wa t 3664) = 0`), `t` not running a VM (`L.mem s (runVcpuA t) = 0`), `t` not an idle +thread, and a live `.Vcpu vc` cap in `OS`, writing `vc.TCB := t` and `t.VCPU := vc` preserves +`VcpuXferStateA`. -/ +theorem VcpuXferStateA.assoc_bind {AR : A → HwRootA → Prop} {Z : XSetsA} + {OS Ub : Set U64} {m : Fl4SpecA64.McState} {a : A} {s : S} {σ : State Unit} + (X : VcpuXferStateA (L := L) AR Z OS Ub m a s σ) + (hU : UbFootDisjA Z OS Ub) (hLD : TcbLinkDisjA Z (L.ksb s)) + (hCs : ∀ c ∈ Z.Cs, c.val < 128) (hcpu : (L.cpu s).val < 128) + (hExt : ∀ b, VcPage Z.V b → ¬ ExtFootE Z.X Z.Y Z.T2 b) + (hSw : ∀ b, VcPage Z.V b → ∀ y, SlotWA Z.th y → + b ≠ y ∧ b ≠ wa y 8#u64 ∧ b ≠ wa y 16#u64 ∧ b ≠ wa y 24#u64) + (hfa : ∀ q, FootA (L := L) Z s q → Aligned q) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hIdleT : ∀ c ∈ Z.Cs, L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) ∈ Z.T) + (hOS_tcb : ∀ t ∈ Z.T, ∀ K : U64, (K = 3664#u64 ∨ K = 3672#u64) → ∀ y ∈ OS, + wa t K ≠ y ∧ wa t K ≠ wa y 8#u64 ∧ wa t K ≠ wa y 16#u64 ∧ wa t K ≠ wa y 24#u64) + {vc t : U64} (hvc : vc ∈ Z.V) (ht : t ∈ Z.T) + (hvc_nz : vc ≠ 0#u64) (ht_nz : t ≠ 0#u64) + (hvc_free : L.mem s vc = 0#u64) (ht_free : L.mem s (wa t 3664#u64) = 0#u64) + (ht_norun : L.mem s (runVcpuA t) = 0#u64) + (ht_not_idle : ∀ c ∈ Z.Cs, L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) ≠ t) + (hvc_cap : ∃ y ∈ OS, capAtA (L.mem s) y = .Vcpu vc) + {v : Vcpu} {x : Tcb} + (hkv : σ.kheap (specPtr vc) = some (.archObj (.vcpu v))) + (hkt : σ.kheap (specPtr t) = some (.tcb x)) : + let s1 := L.wr s vc t + let s2 := L.wr s1 (wa t 3664#u64) vc + let σ2 := updKSc (updKSc σ (specPtr t) (.tcb { x with tcbArch := { x.tcbArch with tcbVcpu := some (specPtr vc) } })) + (specPtr vc) (.archObj (.vcpu { v with vcpuTcb := some (specPtr t) })) + VcpuXferStateA (L := L) AR Z OS Ub m a s2 σ2 := by + dsimp only + set s1 := L.wr s vc t + set s2 := L.wr s1 (wa t 3664#u64) vc + set σ2 := updKSc (updKSc σ (specPtr t) (.tcb { x with tcbArch := { x.tcbArch with tcbVcpu := some (specPtr vc) } })) + (specPtr vc) (.archObj (.vcpu { v with vcpuTcb := some (specPtr t) })) + have hne_vc_t : specPtr vc ≠ specPtr t := fun e => by rw [e, hkt] at hkv; cases hkv + have hwa0 : ∀ v' : U64, wa v' 0#u64 = v' := fun v' => by + apply UScalar.eq_of_val_eq; rw [wa_valP]; simp [UScalar.hBounds] + have hal_v0 : Aligned vc := X.vcpu.place.vc_al hvc + have hta_t := hU.tcb_al t ht + have hal_tv : Aligned (wa t 3664#u64) := aligned_wa (by unfold Aligned; have := hta_t.align; omega) 3664#u64 (by decide) + have hksb2 : L.ksb s2 = L.ksb s := by simp [s2, s1, L.ksb_wr] + have hcpu2 : L.cpu s2 = L.cpu s := by simp [s2, s1, L.cpu_wr] + have hmem2 : ∀ b, Aligned b → b ≠ vc → b ≠ wa t 3664#u64 → L.mem s2 b = L.mem s b := by + intro b hb h1 h2; rw [mem_wr_neA hal_tv hb h2.symm, mem_wr_neA hal_v0 hb h1.symm] + have hvp0 : VcPage Z.V vc := by rw [← hwa0 vc]; exact vcpuHdr_in_vcPage hvc (by decide) (by decide) + have hnd_eq : ∀ c : U64, c.val < 128 → ∀ k : U64, k.val < 128 → k.val % 8 = 0 → + L.mem s2 (L.nodeAt s c k) = L.mem s (L.nodeAt s c k) := fun c hc k hk hk8 => by + obtain ⟨o, ho, heq⟩ := nodeAt_eq_wa_ksbA (L := L) s c k hc (by omega) + exact hmem2 _ (X.vcpu.place.node_al c k hk8) + (X.vcpu.place.disj_node X.j.1.ksFit hvp0 hc (by omega)).symm + (by rw [heq]; exact (hLD.offKs t ht 3664#u64 (Or.inl rfl) o ho).symm) + have hspec_tcb : ∀ u ∈ Z.T, ∀ xu, σ.kheap (specPtr u) = some (.tcb xu) → + ∃ x', σ2.kheap (specPtr u) = some (.tcb x') ∧ + x'.tcbState = xu.tcbState ∧ x'.tcbSchedContext = xu.tcbSchedContext ∧ + x'.tcbDomain = xu.tcbDomain ∧ x'.tcbPriority = xu.tcbPriority ∧ + (∀ i < tcbSlotsA, Fl4SpecA64.tcbCnodeMap x' (Fl4SpecA64.tcbCnodeIndex i) = + Fl4SpecA64.tcbCnodeMap xu (Fl4SpecA64.tcbCnodeIndex i)) := by + intro u hu xu hku + by_cases eu : u = t + · subst u; rw [hkt] at hku; cases hku + refine ⟨{ x with tcbArch := { x.tcbArch with tcbVcpu := some (specPtr vc) } }, ?_, ⟨rfl, rfl, rfl, rfl, fun _ _ => rfl⟩⟩ + rw [updKSc_other _ hne_vc_t.symm, updKSc_same] + · have hne1 : specPtr u ≠ specPtr vc := fun e => by rw [e, hkv] at hku; cases hku + have hne2 : specPtr u ≠ specPtr t := fun e => eu (specPtr_inj e) + exact ⟨xu, by rw [updKSc_other _ hne1, updKSc_other _ hne2]; exact hku, rfl, rfl, rfl, rfl, fun _ _ => rfl⟩ + have hspec_ty : TyKeepA σ σ2 := by + refine ⟨fun o r cs ho => ?_, fun o y ho => ?_⟩ + · have h1 : o ≠ specPtr vc := fun e => by subst e; rw [updKSc_same] at ho; cases ho + have h2 : o ≠ specPtr t := fun e => by subst e; rw [updKSc_other _ hne_vc_t.symm, updKSc_same] at ho; cases ho + rw [updKSc_other _ h1, updKSc_other _ h2] at ho; exact ⟨cs, ho⟩ + · by_cases e2 : o = specPtr t + · subst e2; exact ⟨x, hkt⟩ + · have h1 : o ≠ specPtr vc := fun e => by subst e; rw [updKSc_same] at ho; cases ho + rw [updKSc_other _ h1, updKSc_other _ e2] at ho; exact ⟨y, ho⟩ + obtain ⟨hJ, hS2, hHV, hTr, hCar, hF⟩ := jAtZA_of_vcpuLinkStepA (σ' := σ2) X hksb2 hcpu2 + (fun y hy => by + obtain ⟨a0, a8, a16, a24⟩ := hSw _ hvp0 y hy + obtain ⟨c0, c8, c16, c24⟩ := hLD.offSlot t ht 3664#u64 (Or.inl rfl) y hy + exact ⟨hmem2 _ (hfa _ (.slot y _ hy (Or.inl rfl))) a0.symm c0.symm, + hmem2 _ (hfa _ (.slot y _ hy (Or.inr (Or.inl rfl)))) a8.symm c8.symm, + hmem2 _ (hfa _ (.slot y _ hy (Or.inr (Or.inr (Or.inl rfl))))) a16.symm c16.symm, + hmem2 _ (hfa _ (.slot y _ hy (Or.inr (Or.inr (Or.inr rfl))))) a24.symm c24.symm⟩) + (fun y hy => by + have hya := hU.al_os y hy + have a := X.vcpu.place.disj_os _ hvp0 y hy + obtain ⟨c0, c8, c16, c24⟩ := hOS_tcb t ht 3664#u64 (Or.inl rfl) y hy + exact ⟨hmem2 _ hya a.1.symm c0.symm, + hmem2 _ (aligned_wa hya 8#u64 (by decide)) a.2.1.symm c8.symm, + hmem2 _ (aligned_wa hya 16#u64 (by decide)) a.2.2.1.symm c16.symm, + hmem2 _ (aligned_wa hya 24#u64 (by decide)) a.2.2.2.symm c24.symm⟩) + (fun c hc => ⟨hnd_eq c (hCs c hc) layout.node.CUR_THREAD (by unfold layout.node.CUR_THREAD; decide) (by unfold layout.node.CUR_THREAD; decide), + hnd_eq c (hCs c hc) layout.node.IDLE_THREAD (by unfold layout.node.IDLE_THREAD; decide) (by unfold layout.node.IDLE_THREAD; decide), + hnd_eq c (hCs c hc) layout.node.CUR_SC (by unfold layout.node.CUR_SC; decide) (by unfold layout.node.CUR_SC; decide), + hnd_eq c (hCs c hc) layout.node.IDLE_SC (by unfold layout.node.IDLE_SC; decide) (by unfold layout.node.IDLE_SC; decide)⟩) + (fun u hu => by + have hua := hU.tcb_al u hu + exact ⟨hmem2 _ (by rw [stA_eq]; exact aligned_wa (X.vcpu.place.al_t u hu) 3328#u64 (by decide)) + (by rw [stA_eq, ← hwa0 vc]; exact (X.vcpu.place.disj_tcb vc hvc u hu 0#u64 3328#u64 (by decide) (by decide)).symm) + (by rw [stA_eq]; exact (field_ne hta_t hua (by decide) (by decide) (fun _ => by decide)).symm), + hmem2 _ (by unfold scA; exact aligned_wa (X.vcpu.place.al_t u hu) 3464#u64 (by decide)) + (by unfold scA; rw [← hwa0 vc]; exact (X.vcpu.place.disj_tcb vc hvc u hu 0#u64 3464#u64 (by decide) (by decide)).symm) + (by unfold scA; exact (field_ne hta_t hua (by decide) (by decide) (fun _ => by decide)).symm), + hmem2 _ (by unfold affA; exact aligned_wa (X.vcpu.place.al_t u hu) 3520#u64 (by decide)) + (by unfold affA; rw [← hwa0 vc]; exact (X.vcpu.place.disj_tcb vc hvc u hu 0#u64 3520#u64 (by decide) (by decide)).symm) + (by unfold affA; exact (field_ne hta_t hua (by decide) (by decide) (fun _ => by decide)).symm), + hmem2 _ (hfa _ (.nslot _ (.tcb2 u _ hu (Or.inl rfl)))) + (by unfold prioA; rw [← hwa0 vc]; exact (X.vcpu.place.disj_tcb vc hvc u hu 0#u64 3448#u64 (by decide) (by decide)).symm) + (by unfold prioA; exact (field_ne hta_t hua (by decide) (by decide) (fun _ => by decide)).symm), + hmem2 _ (hfa _ (.nslot _ (.tcb2 u _ hu (Or.inr rfl)))) + (by unfold domA; rw [← hwa0 vc]; exact (X.vcpu.place.disj_tcb vc hvc u hu 0#u64 3432#u64 (by decide) (by decide)).symm) + (by unfold domA; exact (field_ne hta_t hua (by decide) (by decide) (fun _ => by decide)).symm)⟩) + (fun c hc => by + obtain ⟨c24, c32⟩ := hLD.offSc t ht 3664#u64 (Or.inl rfl) c hc + exact ⟨hmem2 _ (hfa _ (.nslot _ (.sc c hc))) + (by rw [← hwa0 vc]; exact (X.vcpu.place.disj_sc vc hvc c hc 0#u64 24#u64 (by decide) (by decide)).symm) c24.symm, + hmem2 _ (hfa _ (.nslot _ (.sc2 c hc))) + (by rw [← hwa0 vc]; exact (X.vcpu.place.disj_sc vc hvc c hc 0#u64 72#u64 (by decide) (by decide)).symm) c32.symm⟩) + ⟨hnd_eq (L.cpu s) hcpu layout.node.SCHED_ACTION (by unfold layout.node.SCHED_ACTION; decide) (by unfold layout.node.SCHED_ACTION; decide), + hnd_eq (L.cpu s) hcpu layout.node.CUR_TIME (by unfold layout.node.CUR_TIME; decide) (by unfold layout.node.CUR_TIME; decide), + hnd_eq (L.cpu s) hcpu layout.node.CONSUMED (by unfold layout.node.CONSUMED; decide) (by unfold layout.node.CONSUMED; decide), + hnd_eq (L.cpu s) hcpu layout.node.REPROGRAM (by unfold layout.node.REPROGRAM; decide) (by unfold layout.node.REPROGRAM; decide)⟩ + ⟨hmem2 _ (hfa _ (.nslot _ (.glob _ (Or.inl rfl)))) + (vc_disj_ksA X.vcpu.place X.j.1.ksFit hvp0 8#u64 (by unfold ksSizeA; decide)).symm + (hLD.offKs t ht 3664#u64 (Or.inl rfl) 8#u64 (by unfold ksSizeA; decide)).symm, + hmem2 _ (hfa _ (.nslot _ (.glob _ (Or.inr rfl)))) + (vc_disj_ksA X.vcpu.place X.j.1.ksFit hvp0 16#u64 (by unfold ksSizeA; decide)).symm + (hLD.offKs t ht 3664#u64 (Or.inl rfl) 16#u64 (by unfold ksSizeA; decide)).symm⟩ + (fun i hi => by + have ho : (irqOffA i).val < ksSizeA := by + unfold irqOffA ksSizeA + simp only [core.num.U64.wrapping_add_val_eq, core.num.U64.wrapping_mul_val_eq, + UScalar.size, UScalarTy.numBits] + have h16 : (224#u64).val = 224 := rfl + have h8 : (8#u64).val = 8 := rfl + rw [h16, h8]; omega + exact hmem2 _ (hfa _ (.nslot _ (.irq i hi))) + (vc_disj_ksA X.vcpu.place X.j.1.ksFit hvp0 (irqOffA i) ho).symm + (hLD.offKs t ht 3664#u64 (Or.inl rfl) (irqOffA i) ho).symm) + (fun b hb => hmem2 _ (hfa _ (.nslot _ (.vs b hb))) + (fun e => X.vcpu.place.disj_vs _ hvp0 (e.symm ▸ hb)) + (fun e => hLD.offVs t ht 3664#u64 (Or.inl rfl) (e.symm ▸ hb))) + (fun b hb => hmem2 _ (hfa _ (.nslot _ (.ext b hb))) + (fun e => hExt _ hvp0 (e.symm ▸ hb)) + (fun e => hLD.offExt t ht 3664#u64 (Or.inl rfl) (e.symm ▸ hb))) + (fun b hb => hmem2 _ (X.fmem.aligned b hb) + (vc_disj_ubA X.vcpu.place hvp0 hb).symm + (fun e => X.fmem.disj_tcb b hb t ht 3664#u64 (by decide) e)) + ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, ⟨rfl, rfl, rfl, rfl⟩⟩ rfl rfl ⟨rfl, rfl⟩ + (fun p hnv hnt => by + rw [updKSc_other _ (fun e => hnv vc hvc e.symm), updKSc_other _ (fun e => hnt t ht e.symm)]) + hspec_tcb hspec_ty + (fun p es hp => by + have h1 : p ≠ specPtr vc := fun e => by subst e; rw [updKSc_same] at hp; cases hp + have h2 : p ≠ specPtr t := fun e => by subst e; rw [updKSc_other _ hne_vc_t.symm, updKSc_same] at hp; cases hp + rw [updKSc_other _ h1, updKSc_other _ h2] at hp; exact hp) + (fun p l1 hp => by + have h1 : p ≠ specPtr vc := fun e => by subst e; rw [updKSc_same] at hp; cases hp + have h2 : p ≠ specPtr t := fun e => by subst e; rw [updKSc_other _ hne_vc_t.symm, updKSc_same] at hp; cases hp + rw [updKSc_other _ h1, updKSc_other _ h2] at hp; exact hp) + hsub + have hvc_mem2 : ∀ v' ∈ Z.V, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → + (v' ≠ vc ∨ o ≠ 0#u64) → L.mem s2 (wa v' o) = L.mem s (wa v' o) := fun v' hv' o ho ho8 hne => by + have h1 : wa v' o ≠ vc := by + rw [← hwa0 vc] + by_cases ev : v' = vc + · subst ev + rcases hne with hvne | ho0 + · exact absurd rfl hvne + · exact X.vcpu.place.same_vcpu_ne hvc ho (by decide) ho0 + · exact X.vcpu.place.disj_vcpu hv' hvc ev ho (by decide) + exact hmem2 _ (aligned_wa (X.vcpu.place.vc_al hv') o ho8) h1 + (X.vcpu.place.disj_tcb v' hv' t ht o 3664#u64 ho (by decide)) + have hv0_same : L.mem s2 vc = t := by + rw [mem_wr_neA hal_tv hal_v0 (by rw [← hwa0 vc]; exact (X.vcpu.place.disj_tcb vc hvc t ht 0#u64 3664#u64 (by decide) (by decide)).symm), + L.mem_wr_same hal_v0] + have htv_same : L.mem s2 (wa t 3664#u64) = vc := L.mem_wr_same hal_tv + have htv_ne : ∀ u ∈ Z.T, u ≠ t → L.mem s2 (wa u 3664#u64) = L.mem s (wa u 3664#u64) := fun u hu hne => + hmem2 _ (aligned_wa (X.vcpu.place.al_t u hu) 3664#u64 (by decide)) + (by rw [← hwa0 vc]; exact (X.vcpu.place.disj_tcb vc hvc u hu 0#u64 3664#u64 (by decide) (by decide)).symm) + (field_ne (hU.tcb_al u hu) hta_t (by decide) (by decide) (fun e => absurd e hne)) + have hrv_all : ∀ u ∈ Z.T, L.mem s2 (runVcpuA u) = L.mem s (runVcpuA u) := fun u hu => by + have hreq : runVcpuA u = wa u 3672#u64 := by unfold runVcpuA; rfl + rw [hreq] + exact hmem2 _ (aligned_wa (X.vcpu.place.al_t u hu) 3672#u64 (by decide)) + (by rw [← hwa0 vc]; exact (X.vcpu.place.disj_tcb vc hvc u hu 0#u64 3672#u64 (by decide) (by decide)).symm) + (field_ne (hU.tcb_al u hu) hta_t (by decide) (by decide) (fun _ => by decide)) + have hVcore : VcpuCoreA (L := L) Z s2 σ2 := by + refine ⟨?_, ?_, ?_, ?_⟩ + · intro v' hv' + by_cases ev : v' = vc + · subst v' + refine ⟨{ v with vcpuTcb := some (specPtr t) }, updKSc_same _ _ _, + by rw [hv0_same]; unfold optRefA; rw [if_neg ht_nz], ?_⟩ + rw [hvc_mem2 vc hvc 24#u64 (by decide) (by decide) (Or.inr (by decide))] + obtain ⟨v0, hkv0, -, hr0⟩ := X.vcpu.core.obj vc hvc; rw [hkv] at hkv0; cases hkv0; exact hr0 + · have hne1 : specPtr v' ≠ specPtr vc := fun e => ev (specPtr_inj e) + have hne2 : specPtr v' ≠ specPtr t := fun e => by + obtain ⟨v0, hkv0, -, -⟩ := X.vcpu.core.obj v' hv'; rw [e, hkt] at hkv0; cases hkv0 + obtain ⟨v0, hkv0, ht0, hr0⟩ := X.vcpu.core.obj v' hv' + exact ⟨v0, by rw [updKSc_other _ hne1, updKSc_other _ hne2]; exact hkv0, + by rw [← hwa0 v', hvc_mem2 v' hv' 0#u64 (by decide) (by decide) (Or.inl ev), hwa0]; exact ht0, + by rw [hvc_mem2 v' hv' 24#u64 (by decide) (by decide) (Or.inl ev)]; exact hr0⟩ + · intro p xv hkp + by_cases ep : p = specPtr vc + · subst ep; exact ⟨vc, hvc, rfl⟩ + · have hne2 : p ≠ specPtr t := fun e => by subst e; rw [updKSc_other _ ep, updKSc_same] at hkp; cases hkp + rw [updKSc_other _ ep, updKSc_other _ hne2] at hkp + exact X.vcpu.core.back p xv hkp + · intro u hu + by_cases eu : u = t + · subst u + refine ⟨{ x with tcbArch := { x.tcbArch with tcbVcpu := some (specPtr vc) } }, + by rw [updKSc_other _ hne_vc_t.symm, updKSc_same], + by rw [htv_same]; unfold optRefA; rw [if_neg hvc_nz], ?_⟩ + rw [hrv_all t ht] + obtain ⟨x0, hkt0, -, hr0⟩ := X.vcpu.core.tcb t ht; rw [hkt] at hkt0; cases hkt0; exact hr0 + · have hne1 : specPtr u ≠ specPtr vc := fun e => by + obtain ⟨x0, hkt0, -, -⟩ := X.vcpu.core.tcb u hu; rw [e, hkv] at hkt0; cases hkt0 + have hne2 : specPtr u ≠ specPtr t := fun e => eu (specPtr_inj e) + obtain ⟨x0, hkt0, hv0, hr0⟩ := X.vcpu.core.tcb u hu + exact ⟨x0, by rw [updKSc_other _ hne1, updKSc_other _ hne2]; exact hkt0, + by rw [htv_ne u hu eu]; exact hv0, by rw [hrv_all u hu]; exact hr0⟩ + · intro y hy p hcap + obtain ⟨a0, a8, -, -⟩ := hSw _ hvp0 y (Or.inl hy) + obtain ⟨c0, c8, -, -⟩ := hLD.offSlot t ht 3664#u64 (Or.inl rfl) y (Or.inl hy) + have hc' : capAtA (L.mem s2) y = capAtA (L.mem s) y := by + unfold capAtA + rw [hmem2 _ (hfa _ (.slot y _ (Or.inl hy) (Or.inl rfl))) a0.symm c0.symm, + hmem2 _ (hfa _ (.slot y _ (Or.inl hy) (Or.inr (Or.inl rfl)))) a8.symm c8.symm] + rw [hc'] at hcap; exact X.vcpu.core.cap y hy p hcap + have hkv_cases : ∀ v' ∈ Z.V, ∀ x', σ2.kheap (specPtr v') = some (.archObj (.vcpu x')) → + (v' = vc ∧ x' = { v with vcpuTcb := some (specPtr t) }) ∨ + (v' ≠ vc ∧ σ.kheap (specPtr v') = some (.archObj (.vcpu x'))) := by + intro v' hv' x' hkx + by_cases ev : v' = vc + · subst v'; rw [updKSc_same] at hkx; cases hkx; exact Or.inl ⟨rfl, rfl⟩ + · have hne1 : specPtr v' ≠ specPtr vc := fun e => ev (specPtr_inj e) + have hne2 : specPtr v' ≠ specPtr t := fun e => by + obtain ⟨v0, hkv0, -, -⟩ := X.vcpu.core.obj v' hv'; rw [e, hkt] at hkv0; cases hkv0 + rw [updKSc_other _ hne1, updKSc_other _ hne2] at hkx; exact Or.inr ⟨ev, hkx⟩ + have hVrel : VcpuRelA (L := L) Z OS Ub s2 σ2 := by + refine ⟨hVcore, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, X.vcpu.arch, by rw [hksb2]; exact X.vcpu.place⟩ + · intro v' hv'; rw [hvc_mem2 v' hv' 8#u64 (by decide) (by decide) (Or.inr (by decide))]; exact X.vcpu.owner v' hv' + · intro v' hv' x' hkx + rw [hvc_mem2 v' hv' 16#u64 (by decide) (by decide) (Or.inr (by decide))] + rcases hkv_cases v' hv' x' hkx with ⟨rfl, rfl⟩ | ⟨-, h0⟩ + · exact X.vcpu.vppi v' hv' v hkv + · exact X.vcpu.vppi v' hv' x' h0 + · intro v' hv' x' hkx + rw [hvc_mem2 v' hv' 32#u64 (by decide) (by decide) (Or.inr (by decide))] + rcases hkv_cases v' hv' x' hkx with ⟨rfl, rfl⟩ | ⟨-, h0⟩ + · exact X.vcpu.runOff v' hv' v hkv + · exact X.vcpu.runOff v' hv' x' h0 + · intro v' hv' x' hkx + rw [hvc_mem2 v' hv' 40#u64 (by decide) (by decide) (Or.inr (by decide))] + rcases hkv_cases v' hv' x' hkx with ⟨rfl, rfl⟩ | ⟨-, h0⟩ + · exact X.vcpu.runOut v' hv' v hkv + · exact X.vcpu.runOut v' hv' x' h0 + · intro v' hv' x' hkx i hi + have hlt : (vcpuRegOffA i).val < 4096 := by rw [vcpuRegOffA_val hi]; omega + have h8 : (vcpuRegOffA i).val % 8 = 0 := by rw [vcpuRegOffA_val hi]; omega + have hne0 : vcpuRegOffA i ≠ 0#u64 := fun e => by + have := congrArg UScalar.val e; rw [vcpuRegOffA_val hi] at this; have : (0#u64).val = 0 := rfl; omega + rw [vcpuRegWA, hvc_mem2 v' hv' (vcpuRegOffA i) hlt h8 (Or.inr hne0)] + rcases hkv_cases v' hv' x' hkx with ⟨rfl, rfl⟩ | ⟨-, h0⟩ + · exact X.vcpu.regs v' hv' v hkv i hi + · exact X.vcpu.regs v' hv' x' h0 i hi + · intro v' hv' u hu hne + by_cases ev : v' = vc + · subst v'; rw [hv0_same] at hu; subst hu; exact ⟨ht, htv_same⟩ + · rw [← hwa0 v', hvc_mem2 v' hv' 0#u64 (by decide) (by decide) (Or.inl ev), hwa0] at hu + obtain ⟨huT, huv⟩ := X.vcpu.bind_v2t v' hv' u hu hne + have hne_u : u ≠ t := fun eu => by subst eu; rw [ht_free] at huv; exact absurd huv.symm (X.vcpu.place.al v' hv').1 + exact ⟨huT, by rw [htv_ne u huT hne_u]; exact huv⟩ + · intro u hu v' hv' hne + by_cases eu : u = t + · subst u; rw [htv_same] at hv'; subst hv'; exact ⟨hvc, hv0_same⟩ + · rw [htv_ne u hu eu] at hv' + obtain ⟨hvV, hvu⟩ := X.vcpu.bind_t2v u hu v' hv' hne + have hne_v : v' ≠ vc := fun ev => by + subst ev; rw [hvc_free] at hvu + exact (hU.tcb_al u hu).ne0 (by rw [← hvu]; rfl) + exact ⟨hvV, by rw [← hwa0 v', hvc_mem2 v' hvV 0#u64 (by decide) (by decide) (Or.inl hne_v), hwa0]; exact hvu⟩ + · intro v' hv' c hc hne + rw [hvc_mem2 v' hv' 24#u64 (by decide) (by decide) (Or.inr (by decide))] at hc + obtain ⟨hcT, hcv⟩ := X.vcpu.run_v2c v' hv' c hc hne + exact ⟨hcT, by rw [hrv_all c hcT]; exact hcv⟩ + · intro c hc v' hv' hne + rw [hrv_all c hc] at hv' + obtain ⟨hvV, hvc'⟩ := X.vcpu.run_c2v c hc v' hv' hne + exact ⟨hvV, by rw [hvc_mem2 v' hvV 24#u64 (by decide) (by decide) (Or.inr (by decide))]; exact hvc'⟩ + · intro c hc hne + rw [hrv_all c hc] at hne + have hst_eq' : L.mem s2 (stA c) = L.mem s (stA c) := + hmem2 _ (by rw [stA_eq]; exact aligned_wa (X.vcpu.place.al_t c hc) 3328#u64 (by decide)) + (by rw [stA_eq, ← hwa0 vc]; exact (X.vcpu.place.disj_tcb vc hvc c hc 0#u64 3328#u64 (by decide) (by decide)).symm) + (by rw [stA_eq]; exact (field_ne hta_t (hU.tcb_al c hc) (by decide) (by decide) (fun _ => by decide)).symm) + rw [hst_eq']; exact X.vcpu.run_state c hc hne + · intro u hu hne + by_cases eu : u = t + · subst u; rw [hrv_all t ht]; exact ht_norun + · rw [htv_ne u hu eu] at hne; rw [hrv_all u hu]; exact X.vcpu.bind_run_disj u hu hne + · intro c hc + have hni : L.nodeAt s2 c layout.node.IDLE_THREAD = L.nodeAt s c layout.node.IDLE_THREAD := by + unfold LawfulStore.nodeAt; rw [hksb2] + rw [hni, hnd_eq c (hCs c hc) layout.node.IDLE_THREAD (by unfold layout.node.IDLE_THREAD; decide) (by unfold layout.node.IDLE_THREAD; decide)] + have hidT := hIdleT c hc + exact ⟨by rw [htv_ne _ hidT (ht_not_idle c hc)]; exact (X.vcpu.idle_no_vcpu c hc).1, + by rw [hrv_all _ hidT]; exact (X.vcpu.idle_no_vcpu c hc).2⟩ + · intro v' hv' hor + by_cases ev : v' = vc + · subst v' + obtain ⟨y, hy, hcap⟩ := hvc_cap + refine ⟨y, hy, ?_⟩ + have hya := hU.al_os y hy + have a := X.vcpu.place.disj_os _ hvp0 y hy + obtain ⟨c0, c8, -, -⟩ := hOS_tcb t ht 3664#u64 (Or.inl rfl) y hy + unfold capAtA + rw [hmem2 _ hya a.1.symm c0.symm, hmem2 _ (aligned_wa hya 8#u64 (by decide)) a.2.1.symm c8.symm] + exact hcap + · have hor0 : L.mem s v' ≠ 0#u64 ∨ L.mem s (wa v' 24#u64) ≠ 0#u64 := by + rcases hor with h | h + · rw [← hwa0 v', hvc_mem2 v' hv' 0#u64 (by decide) (by decide) (Or.inl ev), hwa0] at h; exact Or.inl h + · rw [hvc_mem2 v' hv' 24#u64 (by decide) (by decide) (Or.inl ev)] at h; exact Or.inr h + obtain ⟨y, hy, hcap⟩ := X.vcpu.live v' hv' hor0 + refine ⟨y, hy, ?_⟩ + have hya := hU.al_os y hy + have a := X.vcpu.place.disj_os _ hvp0 y hy + obtain ⟨c0, c8, -, -⟩ := hOS_tcb t ht 3664#u64 (Or.inl rfl) y hy + unfold capAtA + rw [hmem2 _ hya a.1.symm c0.symm, hmem2 _ (aligned_wa hya 8#u64 (by decide)) a.2.1.symm c8.symm] + exact hcap + have hRegs : TcbRegsRelA (L.mem s2) σ2 Z.T := by + intro u hu xu hku r + obtain ⟨hKval, hreg_eq⟩ := regA_eq_wa_KA u r + have hlt := regIdxA_lt r + have hua := hU.tcb_al u hu + have hal_r : Aligned (Endp.regA u (regIdxA r)) := by + rw [hreg_eq]; exact aligned_wa (by unfold Aligned; have := hua.align; omega) _ (by omega) + rw [hmem2 _ hal_r + (by rw [hreg_eq, ← hwa0 vc]; exact (X.vcpu.place.disj_tcb vc hvc u hu 0#u64 _ (by decide) (by omega)).symm) + (by rw [hreg_eq]; exact field_ne hua hta_t (by omega) (by decide) (fun _ => by have h3664 : (3664#u64 : U64).val = 3664 := rfl; omega))] + by_cases eu : u = t + · subst u; rw [updKSc_other _ hne_vc_t.symm, updKSc_same] at hku; cases hku + exact X.regs t ht x hkt r + · have hne1 : specPtr u ≠ specPtr vc := fun e => by + obtain ⟨x0, hkt0, -, -⟩ := X.vcpu.core.tcb u hu; rw [e, hkv] at hkt0; cases hkt0 + have hne2 : specPtr u ≠ specPtr t := fun e => eu (specPtr_inj e) + rw [updKSc_other _ hne1, updKSc_other _ hne2] at hku + exact X.regs u hu xu hku r + exact ⟨⟨hJ, hVcore, hS2, hHV⟩, hTr, hCar, hVrel, hF, hRegs⟩ + +/-! ## Section 3: `vcpu.unbind`, `vcpu.finalise`, `vcpu.detach` refinement -/ + +/-- Spec helper: the abstract action of `vcpu.unbind` (`fl4VcpuEndRun` followed by +`fl4DissociateVcpuTcb` if the VCPU is bound). -/ +def fl4VcpuUnbindExt (vc : ObjRef) : SMonad Unit Unit := do + fl4VcpuEndRun vc + let v ← getVcpu vc + match v.vcpuTcb with + | some t => fl4DissociateVcpuTcb vc t + | none => pure () + +set_option maxHeartbeats 400000 in +/-- **`vcpu.unbind` refines `fl4VcpuUnbindExt`**, preserving `VcpuXferStateA` (with updated `Z.hw` +if `complete_run` or `unbind` restores the current thread's VM root). -/ +theorem unbind_refinesA {AR : A → HwRootA → Prop} {Z : XSetsA} + {OS Ub : Set U64} {m : Fl4SpecA64.McState} {a : A} {s : S} {σ : State Unit} + (X : VcpuXferStateA (L := L) AR Z OS Ub m a s σ) + (hP : VcpuPlatAgreeA ar) (hAK : VcpuArchKeepsA ar AR) (hVR : VcpuVmRootA st ar AR) + (hU : UbFootDisjA Z OS Ub) (hLD : TcbLinkDisjA Z (L.ksb s)) + (hUbUser : ∀ q ∈ Ub, inUserFrame (specPtr q) σ) + (hCs : ∀ c ∈ Z.Cs, c.val < 128) (hcpu : (L.cpu s).val < 128) + (hExt : ∀ b, VcPage Z.V b → ¬ ExtFootE Z.X Z.Y Z.T2 b) + (hSw : ∀ b, VcPage Z.V b → ∀ y, SlotWA Z.th y → + b ≠ y ∧ b ≠ wa y 8#u64 ∧ b ≠ wa y 16#u64 ∧ b ≠ wa y 24#u64) + (hfa : ∀ q, FootA (L := L) Z s q → Aligned q) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hIdleT : ∀ c ∈ Z.Cs, L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) ∈ Z.T) + (hksTcb : ∀ t ∈ Z.T, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → + ∀ o : U64, o.val < ksSizeA → wa t K ≠ wa (L.ksb s) o) + (hSlotTcb : ∀ t ∈ Z.T, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → + ∀ y, SlotWA Z.th y → wa t K ≠ y ∧ wa t K ≠ wa y 8#u64 ∧ wa t K ≠ wa y 16#u64 ∧ wa t K ≠ wa y 24#u64) + (hOS_tcb : ∀ t ∈ Z.T, ∀ K : U64, (K = 3664#u64 ∨ K = 3672#u64) → ∀ y ∈ OS, + wa t K ≠ y ∧ wa t K ≠ wa y 8#u64 ∧ wa t K ≠ wa y 16#u64 ∧ wa t K ≠ wa y 24#u64) + {vc : U64} (hvc : vc ∈ Z.V) + (hStall : ∀ t ∈ Z.T, ∀ s' a', smp.remote_tcb_stall st ar s' a' t = ok (s', a')) + (hBufNotS2 : ∀ t ∈ Z.T, ∀ p sz r d mp asid va, + capAtA (L.mem s) (sbWordA t) ≠ .Frame p sz r d mp true asid va) + (hRunSched : ∀ t ∈ Z.T, L.mem s (runVcpuA t) ≠ 0#u64 → + L.mem s (scA t) ≠ 0#u64 ∧ L.rmax s (L.mem s (scA t)) ≠ 0#u64 ∧ + (L.mem s (inqA t) &&& sched.Q_RELEASE) = 0#u64 ∧ + specPtr t ∉ σ.releaseQueue ∧ + Aligned (maxA (L.mem s (scA t))) ∧ stA t ≠ maxA (L.mem s (scA t))) + (hSlotSt : ∀ t ∈ Z.T, ∀ y, SlotWA Z.th y → stA t ≠ y ∧ stA t ≠ wa y 8#u64 ∧ stA t ≠ wa y 16#u64 ∧ stA t ≠ wa y 24#u64) + (hOS_st : ∀ t ∈ Z.T, ∀ y ∈ OS, stA t ≠ y ∧ stA t ≠ wa y 8#u64 ∧ stA t ≠ wa y 16#u64 ∧ stA t ≠ wa y 24#u64) + (hKs_st : ∀ t ∈ Z.T, ∀ o : U64, o.val < ksSizeA → stA t ≠ wa (L.ksb s) o) + (hSc_st : ∀ t ∈ Z.T, ∀ c ∈ Z.C, stA t ≠ wa c 24#u64 ∧ stA t ≠ wa c 72#u64) + (hVs_st : ∀ t ∈ Z.T, ¬ VsFootA Z.Pp Z.S1 Z.Y (L.ksb s) (stA t)) + (hExt_st : ∀ t ∈ Z.T, ¬ ExtFootE Z.X Z.Y Z.T2 (stA t)) : + vcpu.unbind st ar s a vc ⦃ res => + ∃ hw' σ', + fl4VcpuUnbindExt (specPtr vc) σ = ({((), σ')}, False) ∧ + VcpuXferStateA (L := L) AR { Z with hw := hw' } OS Ub m res.2 res.1 σ' ∧ + L.ksb res.1 = L.ksb s ∧ L.cpu res.1 = L.cpu s ∧ + L.mem res.1 vc = 0#u64 ∧ L.mem res.1 (wa vc 24#u64) = 0#u64 ∧ + (∀ v' ∈ Z.V, v' ≠ vc → L.mem res.1 v' = L.mem s v') ∧ + (L.mem s vc ≠ 0#u64 → L.mem res.1 (wa (L.mem s vc) 3664#u64) = 0#u64) ∧ + VcpuRunPresA L Z OS s res.1 σ σ' ⦄ := by + unfold vcpu.unbind + rw [lift_ok, bind_ok, L.read_eq, bind_ok] + set caller := L.mem s (wa vc vcpu.RUN_CALLER) + have hcaller_def : caller = L.mem s (wa vc 24#u64) := by unfold caller vcpu.RUN_CALLER; rfl + obtain ⟨v, hkv, hvt_spec, hrc_spec⟩ := X.vcpu.core.obj vc hvc + -- Step 1: end run (`fl4VcpuEndRun`), then Step 2: dissociate TCB if `vc.TCB ≠ 0` + refine WP.spec_bind (Pₘ := fun r1 => + ∃ hw1 σ1, + fl4VcpuEndRun (Z := Unit) (specPtr vc) σ = ({((), σ1)}, False) ∧ + VcpuXferStateA (L := L) AR { Z with hw := hw1 } OS Ub m r1.2 r1.1 σ1 ∧ + L.ksb r1.1 = L.ksb s ∧ L.cpu r1.1 = L.cpu s ∧ + L.mem r1.1 (wa vc 24#u64) = 0#u64 ∧ + (∀ v' ∈ Z.V, L.mem r1.1 v' = L.mem s v') ∧ + VcpuRunPresA L Z OS s r1.1 σ σ1) ?_ ?_ + · by_cases hcnz : (caller != 0#u64) = true + · have hc_ne0 : caller ≠ 0#u64 := fun e => by rw [e] at hcnz; exact absurd hcnz (by decide) + rw [if_pos hcnz] + obtain ⟨hcallerT, hback⟩ := X.vcpu.run_v2c vc hvc caller hcaller_def.symm hc_ne0 + obtain ⟨xc, hkc, -, -⟩ := X.vcpu.core.tcb caller hcallerT + rw [hStall caller hcallerT s a, bind_ok] + have hst_ok : obj.Tcb.state st caller s = ok (L.mem s (stA caller)) := by + unfold obj.Tcb.state stA; rw [lift_ok, bind_ok, L.read_eq] + show (do let i1 ← obj.Tcb.state st caller s; if i1 = sched.TS_RUNNING_VM then _ else _) ⦃ _ ⦄ + rw [hst_ok, bind_ok] + have hst_vm : L.mem s (stA caller) = sched.TS_RUNNING_VM := + X.vcpu.run_state caller hcallerT (by rw [hback]; exact (X.vcpu.place.al vc hvc).1) + rw [if_pos hst_vm] + have hxc_vm : xc.tcbState = .ext .runningVM := by + obtain ⟨xt0, hkt0, hst_rel, _⟩ := X.j.1.sch.tcb caller hcallerT + have hx_eq : xt0 = xc := KernelObject.tcb.inj (Option.some.inj (hkt0.symm.trans hkc)) + rw [hx_eq, hst_vm] at hst_rel + cases hst_x : xc.tcbState with + | ext e => cases e <;> simp [hst_x, tsCodeA] at hst_rel ⊢ + | _ => simp [hst_x, tsCodeA] at hst_rel + have hsome_rc : v.vcpuExt.runCaller = some (specPtr caller) := by + rw [hrc_spec, ← hcaller_def]; unfold optRefA; rw [if_neg hc_ne0] + have hint_bv : fl4_abi.consts.vcpu_run_stop.INTERRUPT.bv = RunStop.interrupt.word := by + unfold fl4_abi.consts.vcpu_run_stop.INTERRUPT RunStop.word; rfl + refine WP.spec_mono (complete_run_refinesA X hP hAK hVR hU hLD hUbUser hCs hcpu hExt hSw hfa hsub + hIdleT hksTcb hSlotTcb hOS_tcb hcallerT (hBufNotS2 caller hcallerT) (hRunSched caller hcallerT) + (hRunSched caller hcallerT (by rw [hback]; exact (X.vcpu.place.al vc hvc).1)).2.2.2.1 + (hSlotSt caller hcallerT) (hOS_st caller hcallerT) (hKs_st caller hcallerT) + (hSc_st caller hcallerT) (hVs_st caller hcallerT) (hExt_st caller hcallerT) + hint_bv 0#u64 0#u64 0#u64) ?_ + rintro ⟨s1, a1⟩ ⟨hw1, σ1, hsp1, X1, hksb1, hcpu1, -, hrc1, hv_pres1, -, hpres1⟩ + have hrc1' : L.mem s1 (wa vc 24#u64) = 0#u64 := by + have h := hrc1 (by rw [hback]; exact (X.vcpu.place.al vc hvc).1) + rwa [hback] at h + refine ⟨hw1, σ1, ?_, X1, hksb1, hcpu1, hrc1', hv_pres1, hpres1⟩ + rw [fl4VcpuEndRun_runningVmA hkv hkc hsome_rc hxc_vm]; exact hsp1 + · have hc_eq0 : caller = 0#u64 := by + by_contra hne; exact hcnz (bne_iff_ne.2 hne) + rw [if_neg hcnz, WP.spec_ok] + have hnone_rc : v.vcpuExt.runCaller = none := by + rw [hrc_spec, ← hcaller_def]; change optRefA caller = none; rw [hc_eq0]; rfl + exact ⟨Z.hw, σ, fl4VcpuEndRun_noneA hkv hnone_rc, X, rfl, rfl, by rw [← hcaller_def]; exact hc_eq0, + fun _ _ => rfl, VcpuRunPresA.refl L Z OS s σ⟩ + rintro ⟨s1, a1⟩ ⟨hw1, σ1, hsp1, X1, hksb1, hcpu1, hrc1, hv_pres1, hpres1⟩ + show (do let i1 ← lift (wa vc vcpu.TCB); let t_ptr ← st.read s1 i1; if t_ptr = 0#u64 then _ else _) ⦃ _ ⦄ + have htcb_const : vcpu.TCB = 0#u64 := by unfold vcpu.TCB; rfl + have hwa0 : wa vc 0#u64 = vc := by apply UScalar.eq_of_val_eq; rw [wa_valP]; simp [UScalar.hBounds] + rw [htcb_const, lift_ok, bind_ok, hwa0, L.read_eq, bind_ok] + obtain ⟨v1, hkv1, hvt1_spec, -⟩ := X1.vcpu.core.obj vc hvc + by_cases ht0 : L.mem s1 vc = 0#u64 + · rw [if_pos ht0, WP.spec_ok] + have hnone_vt : v1.vcpuTcb = none := by rw [hvt1_spec, ht0]; rfl + have hsp_full : fl4VcpuUnbindExt (specPtr vc) σ = ({((), σ1)}, False) := by + unfold fl4VcpuUnbindExt + rw [nd_bind_oneSc hsp1, nd_bind_oneSc (getVcpu_eqA hkv1), hnone_vt]; rfl + exact ⟨hw1, σ1, hsp_full, X1, hksb1, hcpu1, ht0, hrc1, fun v' hv' _ => hv_pres1 v' hv', + fun h => absurd (by rw [← hv_pres1 vc hvc]; exact ht0) h, hpres1⟩ + · rw [if_neg ht0] + set t := L.mem s1 vc + obtain ⟨htT, hback1⟩ := X1.vcpu.bind_v2t vc hvc t rfl ht0 + obtain ⟨xt1, hkt1, htv1_spec, -⟩ := X1.vcpu.core.tcb t htT + rw [hStall t htT s1 a1, bind_ok] + refine WP.spec_bind (hAK.vcpu_flush a1 hw1 vc X1.j.1.ar) ?_ + intro a3 hAR3 + have X1_a3 : VcpuXferStateA (L := L) AR { Z with hw := hw1 } OS Ub m a3 s1 σ1 := X1.of_hw hAR3 + have hown_const : vcpu.OWNER_CPU = 8#u64 := by unfold vcpu.OWNER_CPU; rfl + rw [bind_ok, L.write_eq, bind_ok, + hown_const, lift_ok, bind_ok, L.write_eq, bind_ok] + have hset_bound : vcpu.set_bound st (L.wr (L.wr s1 vc 0#u64) (wa vc 8#u64) 0#u64) t 0#u64 = + ok (L.wr (L.wr (L.wr s1 vc 0#u64) (wa vc 8#u64) 0#u64) (wa t 3664#u64) 0#u64) := by + unfold vcpu.set_bound; rw [eq_of_spec_fd Fl4ProofsA64.P0.Consts.layout.tcb.VCPU.spec, bind_ok, lift_ok, bind_ok, L.write_eq] + rw [hset_bound, bind_ok] + set s3 := L.wr (L.wr (L.wr s1 vc 0#u64) (wa vc 8#u64) 0#u64) (wa t 3664#u64) 0#u64 + have hspsr_idx : regIdxA .spsrEl1 = layout.reg.SPSR_EL1 := by unfold regIdxA layout.reg.SPSR_EL1; rfl + have hreg_ok : obj.Tcb.reg st t s3 layout.reg.SPSR_EL1 = ok (L.mem s3 (Endp.regA t (regIdxA .spsrEl1))) := by + rw [hspsr_idx]; unfold obj.Tcb.reg Endp.regA; rw [lift_ok, bind_ok, lift_ok, bind_ok, lift_ok, bind_ok, L.read_eq] + rw [hreg_ok, bind_ok, regs_sanitise_spsr_eqA, bind_ok] + set spsr' : U64 := ⟨sanitiseRegister false .spsrEl1 (L.mem s3 (Endp.regA t (regIdxA .spsrEl1))).bv⟩ + have hset_spsr : obj.Tcb.set_reg st t s3 layout.reg.SPSR_EL1 spsr' = + ok (L.wr s3 (Endp.regA t (regIdxA .spsrEl1)) spsr') := by + rw [hspsr_idx]; unfold obj.Tcb.set_reg Endp.regA; rw [lift_ok, bind_ok, lift_ok, bind_ok, lift_ok, bind_ok, L.write_eq] + rw [hset_spsr, bind_ok] + have hU1 : UbFootDisjA { Z with hw := hw1 } OS Ub := + ⟨hU.al_os, hU.disj_slotW, hU.disj_vs, hU.disj_ext, hU.tcb_al, hU.tcb_offVs, hU.tcb_offExt, + hU.tcb_offOs, hU.tcb_offKs, hU.tcb_offSc⟩ + have hLD1 : TcbLinkDisjA { Z with hw := hw1 } (L.ksb s1) := by + rw [hksb1]; exact ⟨hLD.offSlot, hLD.offKs, hLD.offSc, hLD.offVs, hLD.offExt⟩ + have hfa1 : ∀ q, FootA (L := L) { Z with hw := hw1 } s1 q → Aligned q := fun q hq => by + have hq' : FootA (L := L) Z s1 q := by + match hq with + | .slot y _ hy hw => exact .slot y q hy hw + | .nslot _ hn => + refine .nslot q ?_ + match hn with + | .node c _ hc ho => exact .node c q hc ho + | .cur _ ho => exact .cur q ho + | .tcb t _ ht ho => exact .tcb t q ht ho + | .sc c hc => exact .sc c hc + | .vcpu v _ hv ho => exact .vcpu v q hv ho + | .irq i hi => exact .irq i hi + | .cur2 _ ho => exact .cur2 q ho + | .glob _ ho => exact .glob q ho + | .tcb2 t _ ht ho => exact .tcb2 t q ht ho + | .sc2 c hc => exact .sc2 c hc + | .vs _ hv => exact .vs q hv + | .ext _ he => exact .ext q he + exact hfa q (footA_of_ksb_cpuA hksb1 hcpu1 hq') + have X4 := X1_a3.dissoc_bind hU1 hLD1 hCs (by rw [hcpu1]; exact hcpu) + hExt hSw hfa1 hsub (fun c hc => X1_a3.j.1.idleT c hc) + (by rw [hksb1]; exact hksTcb) hSlotTcb hOS_tcb hvc htT rfl hback1 hkv1 hkt1 + set s4 := L.wr s3 (Endp.regA t (regIdxA .spsrEl1)) spsr' + set σ4 := updKSc (updKSc σ1 (specPtr t) (.tcb (dissociateTcbA xt1))) (specPtr vc) + (.archObj (.vcpu { v1 with vcpuTcb := none, vcpuExt := { v1.vcpuExt with ownerCpu := 0 } })) + have hsome_vt : v1.vcpuTcb = some (specPtr t) := by rw [hvt1_spec]; unfold optRefA; rw [if_neg ht0] + have hsome_tv : xt1.tcbArch.tcbVcpu = some (specPtr vc) := by + rw [htv1_spec, hback1]; unfold optRefA; rw [if_neg (X.vcpu.place.al vc hvc).1] + have hsp_full : fl4VcpuUnbindExt (specPtr vc) σ = ({((), σ4)}, False) := by + unfold fl4VcpuUnbindExt + rw [nd_bind_oneSc hsp1, nd_bind_oneSc (getVcpu_eqA hkv1), hsome_vt]; dsimp only + exact fl4DissociateVcpuTcb_eqA X1.vcpu.arch hkv1 hkt1 hsome_vt hsome_tv + have hksb4 : L.ksb s4 = L.ksb s := by simp [s4, s3, L.ksb_wr, hksb1] + have hcpu4 : L.cpu s4 = L.cpu s := by simp [s4, s3, L.cpu_wr, hcpu1] + have hv0_4 : L.mem s4 vc = 0#u64 := by + obtain ⟨v4, hkv4, hvt4, -⟩ := X4.vcpu.core.obj vc hvc + rw [updKSc_same] at hkv4; cases hkv4 + by_contra hne; unfold optRefA at hvt4; rw [if_neg hne] at hvt4; cases hvt4 + have hrc_4 : L.mem s4 (wa vc 24#u64) = 0#u64 := by + obtain ⟨v4, hkv4, -, hrc4⟩ := X4.vcpu.core.obj vc hvc + rw [updKSc_same] at hkv4; cases hkv4 + obtain ⟨v1', hkv1', -, hrc1'⟩ := X1.vcpu.core.obj vc hvc + rw [hkv1] at hkv1'; cases hkv1'; rw [hrc1] at hrc1' + by_contra hne; rw [hrc1'] at hrc4; unfold optRefA at hrc4; rw [if_neg hne] at hrc4; cases hrc4 + have hpres2 : VcpuRunPresA L Z OS s1 s4 σ1 σ4 := by + have hta_t := hU.tcb_al t htT + have hal_vc0 : Aligned vc := X.vcpu.place.vc_al hvc + have hal_vc8 : Aligned (wa vc 8#u64) := X.vcpu.place.vcPage_al (vcpuHdr_in_vcPage hvc (o := 8#u64) (by decide) (by decide)) + have hal_tv : Aligned (wa t 3664#u64) := aligned_wa (by unfold Aligned; have := hta_t.align; omega) 3664#u64 (by decide) + obtain ⟨hKval_spsr, hreg_spsr⟩ := regA_eq_wa_KA t .spsrEl1 + have hlt_spsr := regIdxA_lt .spsrEl1 + have hal_spsr : Aligned (Endp.regA t (regIdxA .spsrEl1)) := by + rw [hreg_spsr]; exact aligned_wa (by unfold Aligned; have := hta_t.align; omega) _ (by omega) + have hmem4 : ∀ q, Aligned q → vc ≠ q → wa vc 8#u64 ≠ q → wa t 3664#u64 ≠ q → + Endp.regA t (regIdxA .spsrEl1) ≠ q → L.mem s4 q = L.mem s1 q := fun q hq h1 h2 h3 h4 => by + change L.mem (L.wr (L.wr (L.wr (L.wr s1 vc 0#u64) (wa vc 8#u64) 0#u64) (wa t 3664#u64) 0#u64) (Endp.regA t (regIdxA .spsrEl1)) spsr') q = L.mem s1 q + rw [mem_wr_neA hal_spsr hq h4, mem_wr_neA hal_tv hq h3, mem_wr_neA hal_vc8 hq h2, mem_wr_neA hal_vc0 hq h1] + refine ⟨?_, ?_, ?_, ?_, ?_, ?_⟩ + · intro y hy + have hya := hU.al_os y hy + have hvp0 : VcPage Z.V vc := by rw [← hwa0]; exact vcpuHdr_in_vcPage hvc (o := 0#u64) (by decide) (by decide) + have hvp8 : VcPage Z.V (wa vc 8#u64) := vcpuHdr_in_vcPage hvc (o := 8#u64) (by decide) (by decide) + have d0 := X.vcpu.place.disj_os _ hvp0 y hy + have d8 := X.vcpu.place.disj_os _ hvp8 y hy + have dtv := hOS_tcb t htT 3664#u64 (Or.inl rfl) y hy + have dspsr : Endp.regA t (regIdxA .spsrEl1) ≠ y ∧ Endp.regA t (regIdxA .spsrEl1) ≠ wa y 8#u64 ∧ + Endp.regA t (regIdxA .spsrEl1) ≠ wa y 16#u64 ∧ Endp.regA t (regIdxA .spsrEl1) ≠ wa y 24#u64 := by + rw [hreg_spsr]; exact hU.tcb_offOs t htT _ (by omega) (by omega) y hy + exact ⟨hmem4 y hya d0.1 d8.1 dtv.1 dspsr.1, + hmem4 (wa y 8#u64) (aligned_wa hya 8#u64 (by decide)) d0.2.1 d8.2.1 dtv.2.1 dspsr.2.1, + hmem4 (wa y 16#u64) (aligned_wa hya 16#u64 (by decide)) d0.2.2.1 d8.2.2.1 dtv.2.2.1 dspsr.2.2.1, + hmem4 (wa y 24#u64) (aligned_wa hya 24#u64 (by decide)) d0.2.2.2 d8.2.2.2 dtv.2.2.2 dspsr.2.2.2⟩ + · intro u hu + have hta_u := hU.tcb_al u hu + have hu_al : Aligned u := by unfold Aligned; have := hta_u.align; omega + have hu_field : ∀ o : U64, (o = 3592#u64 ∨ o = 3464#u64) → L.mem s4 (wa u o) = L.mem s1 (wa u o) := fun o ho => by + have ho_lt : o.val < 4096 := by rcases ho with rfl | rfl <;> decide + have ho_8 : o.val % 8 = 0 := by rcases ho with rfl | rfl <;> decide + refine hmem4 (wa u o) (aligned_wa hu_al o ho_8) + (by rw [← hwa0]; exact X.vcpu.place.disj_tcb vc hvc u hu 0#u64 o (by decide) ho_lt) + (X.vcpu.place.disj_tcb vc hvc u hu 8#u64 o (by decide) ho_lt) + (field_ne hta_t hta_u (by decide) ho_lt (fun _ e => by rcases ho with rfl | rfl <;> exact absurd e (by decide))) + (by rw [hreg_spsr] + have h3592 : (3592#u64 : U64).val = 3592 := rfl + have h3464 : (3464#u64 : U64).val = 3464 := rfl + exact field_ne hta_t hta_u (by omega) ho_lt (fun _ e => by rcases ho with rfl | rfl <;> omega)) + exact ⟨by unfold inqA; exact hu_field 3592#u64 (Or.inl rfl), + by unfold scA; exact hu_field 3464#u64 (Or.inr rfl)⟩ + · intro c hc + have hma_al : Aligned (maxA c) := by + rw [maxA_eqA]; exact hfa _ (.nslot _ (.sc2 c hc)) + refine hmem4 (maxA c) hma_al + (by rw [← hwa0, maxA_eqA]; exact X.vcpu.place.disj_sc vc hvc c hc 0#u64 72#u64 (by decide) (by decide)) + (by rw [maxA_eqA]; exact X.vcpu.place.disj_sc vc hvc c hc 8#u64 72#u64 (by decide) (by decide)) + (by rw [maxA_eqA]; exact (hLD.offSc t htT 3664#u64 (Or.inl rfl) c hc).2) + (by rw [hreg_spsr, maxA_eqA]; exact hU.tcb_offSc t htT _ (by omega) (by omega) c hc 72#u64 (by decide)) + · intro c hc + have hnd_eq : L.nodeAt s4 c layout.node.IDLE_THREAD = L.nodeAt s1 c layout.node.IDLE_THREAD := by + unfold LawfulStore.nodeAt; rw [hksb4, hksb1] + rw [hnd_eq] + have hnd_al : Aligned (L.nodeAt s1 c layout.node.IDLE_THREAD) := + X1.vcpu.place.node_al c layout.node.IDLE_THREAD (by unfold layout.node.IDLE_THREAD; decide) + have hnd_lt : (layout.node.IDLE_THREAD : U64).val < 4288 := by unfold layout.node.IDLE_THREAD; decide + have hc128 := hCs c hc + have hksFit1 := X1.j.1.ksFit + have hnd_val : (L.nodeAt s1 c layout.node.IDLE_THREAD).val = + (L.ksb s1).val + 223488 + c.val * 4288 + (layout.node.IDLE_THREAD : U64).val := by + have hk := (L.ksb s1).hBounds; have hc' := c.hBounds + simp only [ksSizeA] at hksFit1 + simp [LawfulStore.nodeAt, core.num.U64.wrapping_add_val_eq, core.num.U64.wrapping_mul_val_eq, + U64.size_def, U64.numBits, UScalar.size] at hk hc' ⊢; omega + let kOff : U64 := ⟨BitVec.ofNat 64 (223488 + c.val * 4288 + (layout.node.IDLE_THREAD : U64).val)⟩ + have hkOff_val : kOff.val = 223488 + c.val * 4288 + (layout.node.IDLE_THREAD : U64).val := by + change (BitVec.ofNat 64 _).toNat = _; rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + have hks_lt : kOff.val < ksSizeA := by rw [hkOff_val]; unfold ksSizeA; omega + have hnd_wa : L.nodeAt s1 c layout.node.IDLE_THREAD = wa (L.ksb s) kOff := by + apply UScalar.eq_of_val_eq; rw [hnd_val, hksb1, wa_valP, hkOff_val] + rw [hksb1] at hksFit1; simp only [ksSizeA] at hksFit1; rw [Nat.mod_eq_of_lt (by omega)]; omega + have hvp0 : VcPage Z.V vc := by rw [← hwa0]; exact vcpuHdr_in_vcPage hvc (o := 0#u64) (by decide) (by decide) + have hvp8 : VcPage Z.V (wa vc 8#u64) := vcpuHdr_in_vcPage hvc (o := 8#u64) (by decide) (by decide) + refine hmem4 (L.nodeAt s1 c layout.node.IDLE_THREAD) hnd_al + (X1.vcpu.place.disj_node hksFit1 hvp0 hc128 hnd_lt) + (X1.vcpu.place.disj_node hksFit1 hvp8 hc128 hnd_lt) + (by rw [hnd_wa]; exact hLD.offKs t htT 3664#u64 (Or.inl rfl) kOff hks_lt) + (by rw [hreg_spsr, hnd_wa]; exact hksTcb t htT _ (by omega) (by omega) kOff hks_lt) + · intro q hq + have hq2 := inUserFrame_updKScA (o1 := .tcb (dissociateTcbA xt1)) hkt1 (fun _ h => by cases h) hq + have hne_vc_t' : specPtr vc ≠ specPtr t := fun e => by rw [e, hkt1] at hkv1; cases hkv1 + exact inUserFrame_updKScA (by rw [updKSc_other _ hne_vc_t']; exact hkv1) (fun _ h => by cases h) hq2 + · intro u hu hne + have hta_u := hU.tcb_al u hu + have hrv_eq : L.mem s4 (runVcpuA u) = L.mem s1 (runVcpuA u) := + hmem4 (runVcpuA u) (by unfold runVcpuA; exact aligned_wa (by unfold Aligned; have := hta_u.align; omega) 3672#u64 (by decide)) + (by rw [← hwa0]; unfold runVcpuA; exact X.vcpu.place.disj_tcb vc hvc u hu 0#u64 3672#u64 (by decide) (by decide)) + (by unfold runVcpuA; exact X.vcpu.place.disj_tcb vc hvc u hu 8#u64 3672#u64 (by decide) (by decide)) + (by unfold runVcpuA; exact field_ne hta_t hta_u (by decide) (by decide) (fun _ => by decide)) + (by rw [hreg_spsr]; unfold runVcpuA + have h3672 : (3672#u64 : U64).val = 3672 := rfl + exact field_ne hta_t hta_u (by omega) (by decide) (fun _ => by omega)) + rwa [hrv_eq] at hne + have hv_other4 : ∀ v' ∈ Z.V, v' ≠ vc → L.mem s4 v' = L.mem s v' := fun v' hv' hne => by + have hta_t := hU.tcb_al t htT + have hal_vc0 : Aligned vc := X.vcpu.place.vc_al hvc + have hal_vc8 : Aligned (wa vc 8#u64) := X.vcpu.place.vcPage_al (vcpuHdr_in_vcPage hvc (o := 8#u64) (by decide) (by decide)) + have hal_tv : Aligned (wa t 3664#u64) := aligned_wa (by unfold Aligned; have := hta_t.align; omega) 3664#u64 (by decide) + obtain ⟨hKval_spsr, hreg_spsr⟩ := regA_eq_wa_KA t .spsrEl1 + have hlt_spsr := regIdxA_lt .spsrEl1 + have hal_spsr : Aligned (Endp.regA t (regIdxA .spsrEl1)) := by + rw [hreg_spsr]; exact aligned_wa (by unfold Aligned; have := hta_t.align; omega) _ (by omega) + have hwa0' : wa v' 0#u64 = v' := by apply UScalar.eq_of_val_eq; rw [wa_valP]; simp [UScalar.hBounds] + rw [← hv_pres1 v' hv'] + change L.mem (L.wr (L.wr (L.wr (L.wr s1 vc 0#u64) (wa vc 8#u64) 0#u64) (wa t 3664#u64) 0#u64) (Endp.regA t (regIdxA .spsrEl1)) spsr') v' = L.mem s1 v' + rw [mem_wr_neA hal_spsr (X.vcpu.place.vc_al hv') (by rw [hreg_spsr, ← hwa0']; exact (X.vcpu.place.disj_tcb v' hv' t htT 0#u64 _ (by decide) (by omega)).symm), + mem_wr_neA hal_tv (X.vcpu.place.vc_al hv') (by rw [← hwa0']; exact (X.vcpu.place.disj_tcb v' hv' t htT 0#u64 3664#u64 (by decide) (by decide)).symm), + mem_wr_neA hal_vc8 (X.vcpu.place.vc_al hv') (by rw [← hwa0']; exact X.vcpu.place.disj_vcpu hvc hv' hne.symm (by decide) (by decide)), + mem_wr_neA hal_vc0 (X.vcpu.place.vc_al hv') hne.symm] + have htv_clr4 : L.mem s vc ≠ 0#u64 → L.mem s4 (wa (L.mem s vc) 3664#u64) = 0#u64 := fun _ => by + have ht_eq : L.mem s vc = t := (hv_pres1 vc hvc).symm + rw [ht_eq] + obtain ⟨xt4, hkt4, htv4, -⟩ := X4.vcpu.core.tcb t htT + have hne_vc_t' : specPtr vc ≠ specPtr t := fun e => by rw [e, hkt1] at hkv1; cases hkv1 + rw [updKSc_other _ hne_vc_t'.symm, updKSc_same] at hkt4; cases hkt4 + by_contra hne; unfold optRefA at htv4; rw [if_neg hne] at htv4; cases htv4 + have hpres4 : VcpuRunPresA L Z OS s s4 σ σ4 := hpres1.trans hpres2 + have hcur_ok : sched.cur_thread st s4 = ok (L.curW s4) := eq_of_spec_fd (sched.cur_thread.spec s4) + have heq_ok : obj.Tcb.Insts.CoreCmpPartialEqTcb.eq t (L.curW s4) = ok (decide (t = L.curW s4)) := by + unfold obj.Tcb.Insts.CoreCmpPartialEqTcb.eq; rfl + rw [hcur_ok, bind_ok, heq_ok, bind_ok] + by_cases hcur_eq : t = L.curW s4 + · rw [if_pos (by simp [hcur_eq])] + refine WP.spec_mono (hVR.set_vm_root s4 a3 hw1 t X4.j.1.ar) ?_ + rintro ⟨s5, a5⟩ ⟨hs5, hw5, hAR5⟩ + dsimp only at hs5; subst hs5 + exact ⟨hw5, σ4, hsp_full, X4.of_hw hAR5, hksb4, hcpu4, hv0_4, hrc_4, hv_other4, htv_clr4, hpres4⟩ + · rw [if_neg (by simp [hcur_eq]), WP.spec_ok] + exact ⟨hw1, σ4, hsp_full, X4, hksb4, hcpu4, hv0_4, hrc_4, hv_other4, htv_clr4, hpres4⟩ + +/-- **`vcpu.finalise` refines `fl4VcpuUnbindExt`** (`unbind` + `vcpu_flush`). -/ +theorem fin_vcpu_jA {AR : A → HwRootA → Prop} {Z : XSetsA} + {OS Ub : Set U64} {m : Fl4SpecA64.McState} {a : A} {s : S} {σ : State Unit} + (X : VcpuXferStateA (L := L) AR Z OS Ub m a s σ) + (hP : VcpuPlatAgreeA ar) (hAK : VcpuArchKeepsA ar AR) (hVR : VcpuVmRootA st ar AR) + (hU : UbFootDisjA Z OS Ub) (hLD : TcbLinkDisjA Z (L.ksb s)) + (hUbUser : ∀ q ∈ Ub, inUserFrame (specPtr q) σ) + (hCs : ∀ c ∈ Z.Cs, c.val < 128) (hcpu : (L.cpu s).val < 128) + (hExt : ∀ b, VcPage Z.V b → ¬ ExtFootE Z.X Z.Y Z.T2 b) + (hSw : ∀ b, VcPage Z.V b → ∀ y, SlotWA Z.th y → + b ≠ y ∧ b ≠ wa y 8#u64 ∧ b ≠ wa y 16#u64 ∧ b ≠ wa y 24#u64) + (hfa : ∀ q, FootA (L := L) Z s q → Aligned q) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hIdleT : ∀ c ∈ Z.Cs, L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) ∈ Z.T) + (hksTcb : ∀ t ∈ Z.T, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → + ∀ o : U64, o.val < ksSizeA → wa t K ≠ wa (L.ksb s) o) + (hSlotTcb : ∀ t ∈ Z.T, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → + ∀ y, SlotWA Z.th y → wa t K ≠ y ∧ wa t K ≠ wa y 8#u64 ∧ wa t K ≠ wa y 16#u64 ∧ wa t K ≠ wa y 24#u64) + (hOS_tcb : ∀ t ∈ Z.T, ∀ K : U64, (K = 3664#u64 ∨ K = 3672#u64) → ∀ y ∈ OS, + wa t K ≠ y ∧ wa t K ≠ wa y 8#u64 ∧ wa t K ≠ wa y 16#u64 ∧ wa t K ≠ wa y 24#u64) + {vc : U64} (hvc : vc ∈ Z.V) + (hStall : ∀ t ∈ Z.T, ∀ s' a', smp.remote_tcb_stall st ar s' a' t = ok (s', a')) + (hBufNotS2 : ∀ t ∈ Z.T, ∀ p sz r d mp asid va, + capAtA (L.mem s) (sbWordA t) ≠ .Frame p sz r d mp true asid va) + (hRunSched : ∀ t ∈ Z.T, L.mem s (runVcpuA t) ≠ 0#u64 → + L.mem s (scA t) ≠ 0#u64 ∧ L.rmax s (L.mem s (scA t)) ≠ 0#u64 ∧ + (L.mem s (inqA t) &&& sched.Q_RELEASE) = 0#u64 ∧ + specPtr t ∉ σ.releaseQueue ∧ + Aligned (maxA (L.mem s (scA t))) ∧ stA t ≠ maxA (L.mem s (scA t))) + (hSlotSt : ∀ t ∈ Z.T, ∀ y, SlotWA Z.th y → stA t ≠ y ∧ stA t ≠ wa y 8#u64 ∧ stA t ≠ wa y 16#u64 ∧ stA t ≠ wa y 24#u64) + (hOS_st : ∀ t ∈ Z.T, ∀ y ∈ OS, stA t ≠ y ∧ stA t ≠ wa y 8#u64 ∧ stA t ≠ wa y 16#u64 ∧ stA t ≠ wa y 24#u64) + (hKs_st : ∀ t ∈ Z.T, ∀ o : U64, o.val < ksSizeA → stA t ≠ wa (L.ksb s) o) + (hSc_st : ∀ t ∈ Z.T, ∀ c ∈ Z.C, stA t ≠ wa c 24#u64 ∧ stA t ≠ wa c 72#u64) + (hVs_st : ∀ t ∈ Z.T, ¬ VsFootA Z.Pp Z.S1 Z.Y (L.ksb s) (stA t)) + (hExt_st : ∀ t ∈ Z.T, ¬ ExtFootE Z.X Z.Y Z.T2 (stA t)) : + vcpu.finalise st ar s a vc ⦃ res => + ∃ hw' σ', + fl4VcpuUnbindExt (specPtr vc) σ = ({((), σ')}, False) ∧ + VcpuXferStateA (L := L) AR { Z with hw := hw' } OS Ub m res.2 res.1 σ' ∧ + L.mem res.1 vc = 0#u64 ∧ L.mem res.1 (wa vc 24#u64) = 0#u64 ⦄ := by + unfold vcpu.finalise + refine WP.spec_bind (unbind_refinesA X hP hAK hVR hU hLD hUbUser hCs hcpu hExt hSw hfa hsub + hIdleT hksTcb hSlotTcb hOS_tcb hvc hStall hBufNotS2 hRunSched hSlotSt hOS_st hKs_st hSc_st hVs_st hExt_st) ?_ + rintro ⟨s1, a1⟩ ⟨hw1, σ1, hsp1, X1, -, -, hv0, hrc0, -, -, -⟩ + refine WP.spec_bind (hAK.vcpu_flush a1 hw1 vc X1.j.1.ar) ?_ + intro a2 hAR2 + rw [WP.spec_ok] + exact ⟨hw1, σ1, hsp1, X1.of_hw hAR2, hv0, hrc0⟩ + +/-- **Architect E4 `DeadExtA` J2 corollary (`isFinalCap` post of `vcpu::finalise`)**: +for a `.Vcpu vc` slot `x`, if `isFinal = true` then `vcpu.finalise` dissociates `vc` +(`v'.vcpuTcb = none`), and if `isFinal = false` another `.Vcpu vc` cap exists in `OS \ {x}`. -/ +theorem fin_vcpu_live_or_dissocA {AR : A → HwRootA → Prop} {Z : XSetsA} + {OS Ub : Set U64} {m : Fl4SpecA64.McState} {a : A} {s : S} {σ : State Unit} + (X : VcpuXferStateA (L := L) AR Z OS Ub m a s σ) + (hP : VcpuPlatAgreeA ar) (hAK : VcpuArchKeepsA ar AR) (hVR : VcpuVmRootA st ar AR) + (hU : UbFootDisjA Z OS Ub) (hLD : TcbLinkDisjA Z (L.ksb s)) + (hUbUser : ∀ q ∈ Ub, inUserFrame (specPtr q) σ) + (hCs : ∀ c ∈ Z.Cs, c.val < 128) (hcpu : (L.cpu s).val < 128) + (hExt : ∀ b, VcPage Z.V b → ¬ ExtFootE Z.X Z.Y Z.T2 b) + (hSw : ∀ b, VcPage Z.V b → ∀ y, SlotWA Z.th y → + b ≠ y ∧ b ≠ wa y 8#u64 ∧ b ≠ wa y 16#u64 ∧ b ≠ wa y 24#u64) + (hfa : ∀ q, FootA (L := L) Z s q → Aligned q) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hIdleT : ∀ c ∈ Z.Cs, L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) ∈ Z.T) + (hksTcb : ∀ t ∈ Z.T, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → + ∀ o : U64, o.val < ksSizeA → wa t K ≠ wa (L.ksb s) o) + (hSlotTcb : ∀ t ∈ Z.T, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → + ∀ y, SlotWA Z.th y → wa t K ≠ y ∧ wa t K ≠ wa y 8#u64 ∧ wa t K ≠ wa y 16#u64 ∧ wa t K ≠ wa y 24#u64) + (hOS_tcb : ∀ t ∈ Z.T, ∀ K : U64, (K = 3664#u64 ∨ K = 3672#u64) → ∀ y ∈ OS, + wa t K ≠ y ∧ wa t K ≠ wa y 8#u64 ∧ wa t K ≠ wa y 16#u64 ∧ wa t K ≠ wa y 24#u64) + {vc x : U64} (hvc : vc ∈ Z.V) (isFinal : Bool) + (hNonFinal : isFinal = false → ∃ y ∈ OS, y ≠ x ∧ capAtA (L.mem s) y = .Vcpu vc) + (hStall : ∀ t ∈ Z.T, ∀ s' a', smp.remote_tcb_stall st ar s' a' t = ok (s', a')) + (hBufNotS2 : ∀ t ∈ Z.T, ∀ p sz r d mp asid va, + capAtA (L.mem s) (sbWordA t) ≠ .Frame p sz r d mp true asid va) + (hRunSched : ∀ t ∈ Z.T, L.mem s (runVcpuA t) ≠ 0#u64 → + L.mem s (scA t) ≠ 0#u64 ∧ L.rmax s (L.mem s (scA t)) ≠ 0#u64 ∧ + (L.mem s (inqA t) &&& sched.Q_RELEASE) = 0#u64 ∧ + specPtr t ∉ σ.releaseQueue ∧ + Aligned (maxA (L.mem s (scA t))) ∧ stA t ≠ maxA (L.mem s (scA t))) + (hSlotSt : ∀ t ∈ Z.T, ∀ y, SlotWA Z.th y → stA t ≠ y ∧ stA t ≠ wa y 8#u64 ∧ stA t ≠ wa y 16#u64 ∧ stA t ≠ wa y 24#u64) + (hOS_st : ∀ t ∈ Z.T, ∀ y ∈ OS, stA t ≠ y ∧ stA t ≠ wa y 8#u64 ∧ stA t ≠ wa y 16#u64 ∧ stA t ≠ wa y 24#u64) + (hKs_st : ∀ t ∈ Z.T, ∀ o : U64, o.val < ksSizeA → stA t ≠ wa (L.ksb s) o) + (hSc_st : ∀ t ∈ Z.T, ∀ c ∈ Z.C, stA t ≠ wa c 24#u64 ∧ stA t ≠ wa c 72#u64) + (hVs_st : ∀ t ∈ Z.T, ¬ VsFootA Z.Pp Z.S1 Z.Y (L.ksb s) (stA t)) + (hExt_st : ∀ t ∈ Z.T, ¬ ExtFootE Z.X Z.Y Z.T2 (stA t)) : + (if isFinal then vcpu.finalise st ar s a vc else ok (s, a)) ⦃ res => + ∃ hw' σ' v', + VcpuXferStateA (L := L) AR { Z with hw := hw' } OS Ub m res.2 res.1 σ' ∧ + σ'.kheap (specPtr vc) = some (.archObj (.vcpu v')) ∧ + (v'.vcpuTcb = none ∨ (∃ y ∈ OS, y ≠ x ∧ capAtA (L.mem res.1) y = .Vcpu vc)) ⦄ := by + cases isFinal with + | false => + rw [if_neg (by decide), WP.spec_ok] + obtain ⟨v0, hkv0, -, -⟩ := X.vcpu.core.obj vc hvc + exact ⟨Z.hw, σ, v0, X, hkv0, Or.inr (hNonFinal rfl)⟩ + | true => + rw [if_pos rfl] + refine WP.spec_mono (fin_vcpu_jA X hP hAK hVR hU hLD hUbUser hCs hcpu hExt hSw hfa hsub + hIdleT hksTcb hSlotTcb hOS_tcb hvc hStall hBufNotS2 hRunSched hSlotSt hOS_st hKs_st hSc_st hVs_st hExt_st) ?_ + rintro ⟨s1, a1⟩ ⟨hw1, σ1, -, X1, hv0, -⟩ + obtain ⟨v1, hkv1, hvt1, -⟩ := X1.vcpu.core.obj vc hvc + exact ⟨hw1, σ1, v1, X1, hkv1, Or.inl (by rw [hvt1, hv0]; rfl)⟩ + +/-- Spec helper for `vcpu.detach`: `fl4CancelRun` followed by `fl4VcpuUnbindExt` on any bound VCPU. -/ +def fl4VcpuDetachExt (t : ObjRef) : SMonad Unit Unit := do + fl4CancelRun t + let vOpt ← archThreadGet ArchTcb.tcbVcpu t + match vOpt with + | some vc => fl4VcpuUnbindExt vc + | none => pure () + +/-- **`vcpu.detach` refines `fl4VcpuDetachExt`** (`cancel_run` + conditional `unbind`). -/ +theorem detach_vcpu_jA {AR : A → HwRootA → Prop} {Z : XSetsA} + {OS Ub : Set U64} {m : Fl4SpecA64.McState} {a : A} {s : S} {σ : State Unit} + (X : VcpuXferStateA (L := L) AR Z OS Ub m a s σ) + (hP : VcpuPlatAgreeA ar) (hAK : VcpuArchKeepsA ar AR) (hVR : VcpuVmRootA st ar AR) + (hU : UbFootDisjA Z OS Ub) (hLD : TcbLinkDisjA Z (L.ksb s)) + (hUbUser : ∀ q ∈ Ub, inUserFrame (specPtr q) σ) + (hCs : ∀ c ∈ Z.Cs, c.val < 128) (hcpu : (L.cpu s).val < 128) + (hExt : ∀ b, VcPage Z.V b → ¬ ExtFootE Z.X Z.Y Z.T2 b) + (hSw : ∀ b, VcPage Z.V b → ∀ y, SlotWA Z.th y → + b ≠ y ∧ b ≠ wa y 8#u64 ∧ b ≠ wa y 16#u64 ∧ b ≠ wa y 24#u64) + (hfa : ∀ q, FootA (L := L) Z s q → Aligned q) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hIdleT : ∀ c ∈ Z.Cs, L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) ∈ Z.T) + (hksTcb : ∀ t ∈ Z.T, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → + ∀ o : U64, o.val < ksSizeA → wa t K ≠ wa (L.ksb s) o) + (hSlotTcb : ∀ t ∈ Z.T, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → + ∀ y, SlotWA Z.th y → wa t K ≠ y ∧ wa t K ≠ wa y 8#u64 ∧ wa t K ≠ wa y 16#u64 ∧ wa t K ≠ wa y 24#u64) + (hOS_tcb : ∀ t ∈ Z.T, ∀ K : U64, (K = 3664#u64 ∨ K = 3672#u64) → ∀ y ∈ OS, + wa t K ≠ y ∧ wa t K ≠ wa y 8#u64 ∧ wa t K ≠ wa y 16#u64 ∧ wa t K ≠ wa y 24#u64) + {t : U64} (ht : t ∈ Z.T) + (hStall : ∀ u ∈ Z.T, ∀ s' a', smp.remote_tcb_stall st ar s' a' u = ok (s', a')) + (hBufNotS2 : ∀ u ∈ Z.T, ∀ p sz r d mp asid va, + capAtA (L.mem s) (sbWordA u) ≠ .Frame p sz r d mp true asid va) + (hRunSched : ∀ u ∈ Z.T, L.mem s (runVcpuA u) ≠ 0#u64 → + L.mem s (scA u) ≠ 0#u64 ∧ L.rmax s (L.mem s (scA u)) ≠ 0#u64 ∧ + (L.mem s (inqA u) &&& sched.Q_RELEASE) = 0#u64 ∧ + specPtr u ∉ σ.releaseQueue ∧ + Aligned (maxA (L.mem s (scA u))) ∧ stA u ≠ maxA (L.mem s (scA u))) + (hSlotSt : ∀ u ∈ Z.T, ∀ y, SlotWA Z.th y → stA u ≠ y ∧ stA u ≠ wa y 8#u64 ∧ stA u ≠ wa y 16#u64 ∧ stA u ≠ wa y 24#u64) + (hOS_st : ∀ u ∈ Z.T, ∀ y ∈ OS, stA u ≠ y ∧ stA u ≠ wa y 8#u64 ∧ stA u ≠ wa y 16#u64 ∧ stA u ≠ wa y 24#u64) + (hKs_st : ∀ u ∈ Z.T, ∀ o : U64, o.val < ksSizeA → stA u ≠ wa (L.ksb s) o) + (hSc_st : ∀ u ∈ Z.T, ∀ c ∈ Z.C, stA u ≠ wa c 24#u64 ∧ stA u ≠ wa c 72#u64) + (hVs_st : ∀ u ∈ Z.T, ¬ VsFootA Z.Pp Z.S1 Z.Y (L.ksb s) (stA u)) + (hExt_st : ∀ u ∈ Z.T, ¬ ExtFootE Z.X Z.Y Z.T2 (stA u)) : + vcpu.detach st ar s a t ⦃ res => + ∃ hw' σ', + fl4VcpuDetachExt (specPtr t) σ = ({((), σ')}, False) ∧ + VcpuXferStateA (L := L) AR { Z with hw := hw' } OS Ub m res.2 res.1 σ' ⦄ := by + unfold vcpu.detach + refine WP.spec_bind (cancel_run_refinesA X hU hLD hCs hcpu hExt hSw hfa hsub hIdleT hOS_tcb ht) ?_ + rintro ⟨s1, a1⟩ ⟨ha1, -, -, hquiet, -, σ1, hsp1, X1⟩ + dsimp only at ha1 ⊢; subst ha1 + have hbound_ok : vcpu.bound st s1 t = ok (L.mem s1 (wa t 3664#u64)) := by + unfold vcpu.bound; rw [eq_of_spec_fd Fl4ProofsA64.P0.Consts.layout.tcb.VCPU.spec, bind_ok, lift_ok, bind_ok, L.read_eq] + show (do let v ← vcpu.bound st s1 t; if (v != 0#u64) = true then _ else _) ⦃ _ ⦄ + rw [hbound_ok, bind_ok] + obtain ⟨xt1, hkt1, htv1_spec, -⟩ := X1.vcpu.core.tcb t ht + have hatg : archThreadGet (Z := Unit) ArchTcb.tcbVcpu (specPtr t) σ1 = ({(xt1.tcbArch.tcbVcpu, σ1)}, False) := by + unfold archThreadGet; exact threadGet_eqSc _ hkt1 + by_cases hvnz : (L.mem s1 (wa t 3664#u64) != 0#u64) = true + · have hv_ne0 : L.mem s1 (wa t 3664#u64) ≠ 0#u64 := fun e => by rw [e] at hvnz; exact absurd hvnz (by decide) + rw [if_pos hvnz] + set v := L.mem s1 (wa t 3664#u64) + obtain ⟨hvV, -⟩ := X1.vcpu.bind_t2v t ht v rfl hv_ne0 + have hsome_tv : xt1.tcbArch.tcbVcpu = some (specPtr v) := by rw [htv1_spec]; unfold optRefA; rw [if_neg hv_ne0] + have hno_run0 : L.mem s (runVcpuA t) = 0#u64 := by + by_contra hr_nz + have htv0 : L.mem s (wa t 3664#u64) = 0#u64 := by + by_contra htv_nz; exact hr_nz (X.vcpu.bind_run_disj t ht htv_nz) + obtain ⟨xt0, hkt0, htv0_sp, hrv0_sp⟩ := X.vcpu.core.tcb t ht + set v0 := L.mem s (runVcpuA t) + obtain ⟨hv0V, -⟩ := X.vcpu.run_c2v t ht v0 rfl hr_nz + obtain ⟨xv0, hkv0, -, -⟩ := X.vcpu.core.obj v0 hv0V + have hsome0 : xt0.tcbExt.tcbRunVcpu = some (specPtr v0) := by + rw [hrv0_sp]; unfold optRefA; rw [if_neg hr_nz] + have hsp1' := fl4CancelRun_someA hkt0 hkv0 hsome0 + rw [hsp1'] at hsp1 + have hσ1_eq := (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (Prod.mk.inj hsp1).1)).2 + have hxt1_eq : xt1 = { xt0 with tcbExt := { xt0.tcbExt with tcbRunVcpu := none } } := by + rw [← hσ1_eq, updKSc_same] at hkt1; exact KernelObject.tcb.inj (Option.some.inj hkt1.symm) + rw [hxt1_eq, htv0_sp, htv0] at hsome_tv; cases hsome_tv + have hs1_eq : s1 = s := hquiet hno_run0 + have hσ1_eq : σ1 = σ := by + obtain ⟨xt0, hkt0, -, hrv0_sp⟩ := X.vcpu.core.tcb t ht + have hnone0 : xt0.tcbExt.tcbRunVcpu = none := by rw [hrv0_sp, hno_run0]; rfl + rw [fl4CancelRun_noneA hkt0 hnone0] at hsp1 + exact (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (Prod.mk.inj hsp1).1)).2.symm + subst hs1_eq; subst hσ1_eq + refine WP.spec_mono (unbind_refinesA X hP hAK hVR hU hLD hUbUser hCs hcpu hExt hSw hfa hsub + hIdleT hksTcb hSlotTcb hOS_tcb hvV hStall hBufNotS2 hRunSched hSlotSt hOS_st hKs_st hSc_st hVs_st hExt_st) ?_ + rintro ⟨s2, a2⟩ ⟨hw2, σ2, hsp2, X2, -, -, -, -, -, -, -⟩ + refine ⟨hw2, σ2, ?_, X2⟩ + unfold fl4VcpuDetachExt + rw [nd_bind_oneSc hsp1, nd_bind_oneSc hatg, hsome_tv]; exact hsp2 + · have hv_eq0 : L.mem s1 (wa t 3664#u64) = 0#u64 := by + by_contra hne; exact hvnz (bne_iff_ne.2 hne) + rw [if_neg hvnz, WP.spec_ok] + have hnone_tv : xt1.tcbArch.tcbVcpu = none := by rw [htv1_spec, hv_eq0]; rfl + have hsp_full : fl4VcpuDetachExt (specPtr t) σ = ({((), σ1)}, False) := by + unfold fl4VcpuDetachExt + rw [nd_bind_oneSc hsp1, nd_bind_oneSc hatg, hnone_tv]; rfl + exact ⟨Z.hw, σ1, hsp_full, X1⟩ + +end bind_ops + +end + +end Fl4ProofsA64.P3.Vcpu diff --git a/proofs/Fl4ProofsA64/P3/VcpuXferBaseA.lean b/proofs/Fl4ProofsA64/P3/VcpuXferBaseA.lean new file mode 100644 index 000000000..fb6f6c33b --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/VcpuXferBaseA.lean @@ -0,0 +1,1003 @@ +/- +P3 (aarch64, A64-P3 S4 W3): **VCPU state-transfer refinement** (`vcpu.read_vgic_frame`, +`vcpu.copy_reg_range_to_frame`, `vcpu.read_state_to_frame`, `vcpu.copy_reg_range_from_frame`, +`vcpu.write_state_from_frame`, and labels 67 `ARMVCPUReadState` / 68 `ARMVCPUWriteState` on +`VcpuTopPostA`). + +No new named premise. +-/ +import Fl4ProofsA64.P3.VcpuRegJA +import Fl4ProofsA64.P3.FrontEndExtraA + +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3.Vcpu + +open Fl4Spec Fl4SpecA64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3 Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Endp +open Fl4ProofsA64.P3.Notif + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## Local helpers & state-transfer bundle -/ + +theorem lift_ok {α : Type} (x : α) : (lift x : Result α) = ok x := rfl + +theorem u64_mul_ok {x y : U64} (h : x.val * y.val < 2 ^ 64) : + x * y = ok ⟨BitVec.ofNat 64 (x.val * y.val)⟩ := by + apply eq_of_spec_fd + refine WP.spec_mono (U64.mul_spec (x := x) (y := y) + (by simp [U64.max, U64.numBits, UScalar.size, UScalarTy.numBits]; omega)) ?_ + intro r hr + apply UScalar.eq_of_val_eq + rw [hr] + change x.val * y.val = (BitVec.ofNat 64 (x.val * y.val)).toNat + rw [BitVec.toNat_ofNat] + exact (Nat.mod_eq_of_lt h).symm + +theorem u64_sub_ok {x y : U64} (hle : y.val ≤ x.val) : + x - y = ok ⟨BitVec.ofNat 64 (x.val - y.val)⟩ := by + apply eq_of_spec_fd + refine WP.spec_mono (U64.sub_spec (x := x) (y := y) hle) ?_ + intro r hr + apply UScalar.eq_of_val_eq + rw [hr.1] + change x.val - y.val = (BitVec.ofNat 64 (x.val - y.val)).toNat + rw [BitVec.toNat_ofNat] + exact (Nat.mod_eq_of_lt (by have := x.hBounds; simp [U64.numBits, UScalar.size, UScalarTy.numBits] at this; omega)).symm + +theorem u64_shl_u64_ok {x y : U64} (hy : y.val < 64) : + x <<< y = ok ⟨x.bv <<< y.val⟩ := + u64_shl_ok hy + +theorem spec_bind_assoc {α β γ : Type} {x : Result α} {f : α → Result β} {g : β → Result γ} + {P : β → Prop} {Q : γ → Prop} + (h1 : (do let a ← x; f a) ⦃ P ⦄) + (h2 : ∀ b, P b → g b ⦃ Q ⦄) : + (do let a ← x; let b ← f a; g b) ⦃ Q ⦄ := by + rw [← Std.bind_assoc]; exact WP.spec_bind h1 h2 + +theorem ndE_throw_bind {α β : Type} (e : SyscallError) (f : α → SEMonad Unit β) (σ : State Unit) : + (NonDetE.throwError e >>= f).run σ = ({(.error e, σ)}, False) := by + unfold NonDetE.throwError + change ((NonDet.pure (Except.error e)) >>= (ExceptT.bindCont f)) σ = _ + rw [nd_bind_oneSc (m := NonDet.pure (Except.error e)) rfl]; rfl + +theorem ndE_whenE_true {β : Type} {P : Prop} [Decidable P] (h : P) + (e : SyscallError) (f : Unit → SEMonad Unit β) (σ : State Unit) : + (NonDetE.whenE P (NonDetE.throwError e) >>= f).run σ = ({(.error e, σ)}, False) := by + unfold NonDetE.whenE; rw [if_pos h]; exact ndE_throw_bind e f σ + +theorem ndE_whenE_false {β : Type} {P : Prop} [Decidable P] (h : ¬ P) + (e : SyscallError) (f : Unit → SEMonad Unit β) (σ : State Unit) : + (NonDetE.whenE P (NonDetE.throwError e) >>= f).run σ = (f ()).run σ := by + unfold NonDetE.whenE; rw [if_neg h] + change ((NonDet.pure (Except.ok ())) >>= (ExceptT.bindCont f)) σ = _ + rw [nd_bind_oneSc (m := NonDet.pure (Except.ok ())) rfl]; rfl + +theorem ndE_liftE_bind {α β : Type} (m : SMonad Unit α) (f : α → SEMonad Unit β) + (σ σ' : State Unit) (a : α) (hm : m σ = ({(a, σ')}, False)) : + (NonDetE.liftE m >>= f).run σ = (f a).run σ' := by + have h1 : (NonDetE.liftE (ε := SyscallError) m).run σ = ({(Except.ok a, σ')}, False) := by + change (m >>= fun x => NonDet.pure (Except.ok x)) σ = _ + rw [nd_bind_oneSc hm]; rfl + change (ExceptT.run (NonDetE.liftE (ε := SyscallError) m) >>= ExceptT.bindCont f) σ = _ + rw [nd_bind_oneSc h1]; rfl + +theorem doMachineOp_oneSc {α : Type} {mop : NonDet MachineState α} {σ : State Unit} + {r : α} {ms' : MachineState} (hm : mop σ.machineState = ({(r, ms')}, False)) : + doMachineOp mop σ = ({(r, { σ with machineState := ms' })}, False) := by + unfold doMachineOp + rw [nd_bind_oneSc (m := NonDet.gets (fun s : State Unit => s.machineState)) rfl] + have hsel : NonDet.selectF (mop σ.machineState) σ = ({((r, ms'), σ)}, False) := by + rw [hm]; unfold NonDet.selectF + exact Prod.ext (by ext ⟨x1, x2⟩; simp) rfl + rw [nd_bind_oneSc hsel] + dsimp only + rw [nd_bind_oneSc (m := NonDet.modify (fun state : State Unit => { state with machineState := ms' })) rfl] + rfl + +theorem al3_of_aligned_bufAddr {frame : U64} {k : Nat} + (hfit : frame.val + 8 * k < 2 ^ 64) + (hal : Aligned (wa frame ⟨BitVec.ofNat 64 (8 * k)⟩)) : + bufAddr (specPtr frame) k &&& mask 3 = 0 := by + rw [← specPtr_wa_wordA hfit] + apply BitVec.eq_of_toNat_eq + have hmod : (specPtr (wa frame ⟨BitVec.ofNat 64 (8 * k)⟩)).toNat % 8 = 0 := by + rw [show (8 : Nat) = 2 ^ 3 from rfl, specPtr_mod _ (by omega)]; exact hal + have hm3 : (mask 3 : Word).toNat = 7 := by unfold mask; decide + rw [BitVec.toNat_and, hm3] + change (specPtr (wa frame ⟨BitVec.ofNat 64 (8 * k)⟩)).toNat &&& (2 ^ 3 - 1) = 0 + rw [Nat.and_two_pow_sub_one_eq_mod]; exact hmod + +theorem loadWordOffs_eqA (frame : ObjRef) (k : Nat) (σ : State Unit) + (hal : bufAddr frame k &&& mask 3 = 0) : + loadWordOffs (Z := Unit) frame k σ = + ({(loadWordAt σ.machineState.underlyingMemory (bufAddr frame k), σ)}, False) := by + unfold loadWordOffs + have hlw : loadWord (bufAddr frame k) σ.machineState = + ({(loadWordAt σ.machineState.underlyingMemory (bufAddr frame k), σ.machineState)}, False) := by + unfold loadWord + rw [nd_bind_oneSc (m := NonDet.gets MachineState.underlyingMemory) rfl] + have ha : NonDet.assert (bufAddr frame k &&& mask 3 = 0) σ.machineState = ({((), σ.machineState)}, False) := by + unfold NonDet.assert; exact Prod.ext (by ext x; simp [hal]) (by simp [hal]) + rw [nd_bind_oneSc ha]; rfl + rw [show frame + BitVec.ofNat 64 (k * wordSize) = bufAddr frame k from rfl, doMachineOp_oneSc hlw] + +theorem storeWordOffs_eqA (frame : ObjRef) (k : Nat) (w : Word) (σ : State Unit) + (hal : bufAddr frame k &&& mask 3 = 0) (huf : inUserFrame (bufAddr frame k) σ) : + storeWordOffs (Z := Unit) frame k w σ = + ({((), setUm σ (storeMem σ.machineState.underlyingMemory (bufAddr frame k) w))}, False) := by + unfold storeWordOffs + rw [nd_bind_oneSc (m := NonDet.get) rfl] + have ha1 : NonDet.assert (inUserFrame (bufAddr frame k) σ) σ = ({((), σ)}, False) := by + unfold NonDet.assert; exact Prod.ext (by ext x; simp [huf]) (by simp [huf]) + rw [show frame + BitVec.ofNat 64 (k * wordSize) = bufAddr frame k from rfl, nd_bind_oneSc ha1] + have hsw : storeWord (bufAddr frame k) w σ.machineState = + ({((), { σ.machineState with underlyingMemory := storeMem σ.machineState.underlyingMemory (bufAddr frame k) w })}, False) := by + unfold storeWord + have ha2 : NonDet.assert (bufAddr frame k &&& mask 3 = 0) σ.machineState = ({((), σ.machineState)}, False) := by + unfold NonDet.assert; exact Prod.ext (by ext x; simp [hal]) (by simp [hal]) + rw [nd_bind_oneSc ha2]; rfl + rw [doMachineOp_oneSc hsw]; rfl + +theorem mapMx_appendA {α : Type} (f : α → SMonad Unit Unit) (xs ys : List α) (σ : State Unit) : + NonDet.mapMx f (xs ++ ys) σ = (NonDet.mapMx f xs >>= fun _ => NonDet.mapMx f ys) σ := by + induction xs generalizing σ with + | nil => + change NonDet.mapMx f ys σ = (NonDet.pure () >>= fun _ => NonDet.mapMx f ys) σ + rw [nd_bind_oneSc (m := NonDet.pure ()) rfl] + | cons a tl ih => + change (f a >>= fun _ => NonDet.mapMx f (tl ++ ys)) σ = + ((f a >>= fun _ => NonDet.mapMx f tl) >>= fun _ => NonDet.mapMx f ys) σ + rw [nd_bind_assocSc] + have hfn : (fun _ : Unit => NonDet.mapMx f (tl ++ ys)) = + (fun _ : Unit => NonDet.mapMx f tl >>= fun _ => NonDet.mapMx f ys) := by + funext _; funext σ1; exact ih σ1 + rw [hfn] + +theorem wm_valP (a b : U64) : + (core.num.U64.wrapping_mul a b).val = a.val * b.val % 2 ^ 64 := by + simp only [core.num.U64.wrapping_mul_val_eq, U64.numBits, UScalar.size, UScalarTy.numBits] + +theorem nodeAt_wrA {S : Type} {st : machine.Store S} [L : LawfulStore st] + (s : S) (b w c o : U64) : L.nodeAt (L.wr s b w) c o = L.nodeAt s c o := + nodeAt_of_ksb (L.ksb_wr s b w) c o + +theorem nodeAt_eq_wa_ksbA {S : Type} {st : machine.Store S} [L : LawfulStore st] + (s : S) (c o : U64) (hc : c.val < 128) (ho : o.val < 4288) : + ∃ k : U64, k.val < ksSizeA ∧ L.nodeAt s c o = wa (L.ksb s) k := by + have hm : (core.num.U64.wrapping_mul c 4288#u64).val = c.val * 4288 := by + rw [wm_valP]; change c.val * 4288 % 2 ^ 64 = _; exact Nat.mod_eq_of_lt (by omega) + have ha1 : (wa 223488#u64 (core.num.U64.wrapping_mul c 4288#u64)).val = 223488 + c.val * 4288 := by + rw [wa_valP, hm]; change (223488 + c.val * 4288) % 2 ^ 64 = _; exact Nat.mod_eq_of_lt (by omega) + have ha2 : (wa (wa 223488#u64 (core.num.U64.wrapping_mul c 4288#u64)) o).val = 223488 + c.val * 4288 + o.val := by + rw [wa_valP, ha1]; exact Nat.mod_eq_of_lt (by omega) + refine ⟨wa (wa 223488#u64 (core.num.U64.wrapping_mul c 4288#u64)) o, by rw [ha2]; unfold ksSizeA; omega, ?_⟩ + apply u64_eq_of_bv_eq + have hwa_bv : ∀ x y : U64, (wa x y).bv = x.bv + y.bv := fun x y => by + apply BitVec.eq_of_toNat_eq; change (wa x y).val = (x.bv + y.bv).toNat + rw [wa_valP, BitVec.toNat_add]; rfl + unfold LawfulStore.nodeAt + simp only [hwa_bv, BitVec.add_assoc] + +theorem FootA_of_ksb_cpu_impA {S : Type} {st : machine.Store S} [L : LawfulStore st] + (Z : XSetsA) {s s' : S} (hks : L.ksb s' = L.ksb s) (hcpu : L.cpu s' = L.cpu s) {q : U64} + (hf : FootA (L := L) Z s' q) : FootA (L := L) Z s q := by + have h1 : ∀ c o, L.nodeAt s' c o = L.nodeAt s c o := fun c o => nodeAt_of_ksb hks c o + cases hf with + | slot y _ hy hw => exact FootA.slot y _ hy hw + | nslot _ hn => + apply FootA.nslot + cases hn with + | node c _ hc ho => exact FootNA.node c _ hc (by simpa [h1] using ho) + | cur _ ho => exact FootNA.cur _ (by simpa [h1, hcpu] using ho) + | tcb t _ ht ho => exact FootNA.tcb t _ ht ho + | sc c hc => exact FootNA.sc c hc + | vcpu v _ hv ho => exact FootNA.vcpu v _ hv ho + | irq i hi => exact (by simpa [hks] using FootNA.irq (L := L) (s := s) (Z := Z) i hi) + | cur2 _ ho => exact FootNA.cur2 _ (by simpa [h1, hcpu] using ho) + | glob _ ho => exact FootNA.glob _ (by simpa [hks] using ho) + | tcb2 t _ ht ho => exact FootNA.tcb2 t _ ht ho + | sc2 c hc => exact FootNA.sc2 c hc + | vs _ h => exact FootNA.vs _ (by simpa [hks] using h) + | ext _ h => exact FootNA.ext _ h + +theorem FootA_of_ksb_cpuA {S : Type} {st : machine.Store S} [L : LawfulStore st] + (Z : XSetsA) {s s' : S} (hks : L.ksb s' = L.ksb s) (hcpu : L.cpu s' = L.cpu s) (q : U64) : + FootA (L := L) Z s' q ↔ FootA (L := L) Z s q := + ⟨FootA_of_ksb_cpu_impA Z hks hcpu, FootA_of_ksb_cpu_impA Z hks.symm hcpu.symm⟩ + +theorem FootA_wr_eqA {S : Type} {st : machine.Store S} [L : LawfulStore st] + (Z : XSetsA) (s : S) (b w : U64) (q : U64) : + FootA (L := L) Z (L.wr s b w) q ↔ FootA (L := L) Z s q := + FootA_of_ksb_cpuA Z (L.ksb_wr s b w) (L.cpu_wr s b w) q + +theorem sm_el1_ok : fl4_abi.consts.vcpu_state_mask.EL1_SYSREGS = ok 1#u64 := + eq_of_spec_fd Fl4ProofsA64.P0.Consts.fl4_abi.consts.vcpu_state_mask.EL1_SYSREGS.spec +theorem sm_hyp_ok : fl4_abi.consts.vcpu_state_mask.HYP = ok 2#u64 := + eq_of_spec_fd Fl4ProofsA64.P0.Consts.fl4_abi.consts.vcpu_state_mask.HYP.spec +theorem sm_timer_ok : fl4_abi.consts.vcpu_state_mask.TIMER = ok 4#u64 := + eq_of_spec_fd Fl4ProofsA64.P0.Consts.fl4_abi.consts.vcpu_state_mask.TIMER.spec +theorem sm_vgic_ok : fl4_abi.consts.vcpu_state_mask.VGIC = ok 8#u64 := + eq_of_spec_fd Fl4ProofsA64.P0.Consts.fl4_abi.consts.vcpu_state_mask.VGIC.spec +theorem sm_gprs_ok : fl4_abi.consts.vcpu_state_mask.GPRS = ok 16#u64 := + eq_of_spec_fd Fl4ProofsA64.P0.Consts.fl4_abi.consts.vcpu_state_mask.GPRS.spec +theorem sm_vgic_lr_ok : fl4_abi.consts.vcpu_state_mask.VGIC_LR = ok 32#u64 := + eq_of_spec_fd Fl4ProofsA64.P0.Consts.fl4_abi.consts.vcpu_state_mask.VGIC_LR.spec +theorem sm_all_ok : fl4_abi.consts.vcpu_state_mask.ALL = ok 63#u64 := + eq_of_spec_fd Fl4ProofsA64.P0.Consts.fl4_abi.consts.vcpu_state_mask.ALL.spec + +/-- `regs.sanitise_vcpu_spsr` equals `sanitiseVcpuSpsr`. -/ +theorem sanitise_vcpu_spsr_eqA (v : U64) : + regs.sanitise_vcpu_spsr v = ok ⟨sanitiseVcpuSpsr v.bv⟩ := by + unfold regs.sanitise_vcpu_spsr sanitiseVcpuSpsr + dsimp only + rw [lift_ok, bind_ok] + set m : U64 := v &&& 31#u64 + have hm : m.bv = v.bv &&& 0x1f := rfl + have hall : regs.VCPU_PSTATE_ALLOWED = + ok ⟨(0xf <<< 28) ||| (1 <<< 25) ||| (1 <<< 24) ||| (1 <<< 23) ||| (1 <<< 22) ||| + (1 <<< 21) ||| (1 <<< 20) ||| (1 <<< 12) ||| (3 <<< 10) ||| (0xf <<< 6)⟩ := + eq_of_spec_fd Fl4ProofsA64.P0.Consts.regs.VCPU_PSTATE_ALLOWED.spec + by_cases h0 : m = 0#u64 + · have h0b : v.bv &&& 0x1f = 0 := by rw [← hm, h0]; rfl + rw [if_pos h0, bind_ok, hall, bind_ok, lift_ok, bind_ok, if_pos (Or.inl h0b)] + exact congrArg ok (u64_eq_of_bv_eq (by simp [m, h0b])) + · rw [if_neg h0] + have h0b : v.bv &&& 0x1f ≠ 0 := fun e => h0 (u64_eq_of_bv_eq e) + by_cases h4 : m = 4#u64 + · have h4b : v.bv &&& 0x1f = 4 := by rw [← hm, h4]; rfl + rw [if_pos h4, bind_ok, hall, bind_ok, lift_ok, bind_ok, if_pos (Or.inr (Or.inl h4b))] + exact congrArg ok (u64_eq_of_bv_eq (by simp [m, h4b])) + · rw [if_neg h4] + have h4b : v.bv &&& 0x1f ≠ 4 := fun e => h4 (u64_eq_of_bv_eq e) + by_cases h5 : m = 5#u64 + · have h5b : v.bv &&& 0x1f = 5 := by rw [← hm, h5]; rfl + rw [if_pos h5, bind_ok, hall, bind_ok, lift_ok, bind_ok, if_pos (Or.inr (Or.inr h5b))] + exact congrArg ok (u64_eq_of_bv_eq (by simp [m, h5b])) + · have h5b : v.bv &&& 0x1f ≠ 5 := fun e => h5 (u64_eq_of_bv_eq e) + rw [if_neg h5, bind_ok, hall, bind_ok, lift_ok, bind_ok, + if_neg (by rintro (h | h | h); exact h0b h; exact h4b h; exact h5b h)] + exact congrArg ok (u64_eq_of_bv_eq rfl) + +/-- Every TCB in `T` has its 37 concrete user registers matching `tcb.tcbArch.tcbContext.userRegs`. -/ +def TcbRegsRelA (mem : U64 → U64) (σ : State Unit) (T : Set U64) : Prop := + ∀ t ∈ T, ∀ x : Tcb, σ.kheap (specPtr t) = some (.tcb x) → + ∀ r : Register, (mem (Endp.regA t (regIdxA r))).bv = x.tcbArch.tcbContext.userRegs r + +/-- Static placement side-conditions for user frames `Ub` and TCB registers. -/ +structure UbFootDisjA (Z : XSetsA) (OS Ub : Set U64) : Prop where + al_os : ∀ y ∈ OS, Aligned y + disj_slotW : ∀ b ∈ Ub, ∀ y, SlotWA Z.th y → + b ≠ y ∧ b ≠ wa y 8#u64 ∧ b ≠ wa y 16#u64 ∧ b ≠ wa y 24#u64 + disj_vs : ∀ b ∈ Ub, ∀ ksb, ¬ VsFootA Z.Pp Z.S1 Z.Y ksb b + disj_ext : ∀ b ∈ Ub, ¬ ExtFootE Z.X Z.Y Z.T2 b + tcb_al : ∀ t ∈ Z.T, TcbAddr t + tcb_offVs : ∀ t ∈ Z.T, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → ∀ ksb, ¬ VsFootA Z.Pp Z.S1 Z.Y ksb (wa t K) + tcb_offExt : ∀ t ∈ Z.T, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → ¬ ExtFootE Z.X Z.Y Z.T2 (wa t K) + tcb_offOs : ∀ t ∈ Z.T, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → + ∀ y ∈ OS, wa t K ≠ y ∧ wa t K ≠ wa y 8#u64 ∧ wa t K ≠ wa y 16#u64 ∧ wa t K ≠ wa y 24#u64 + tcb_offKs : ∀ t ∈ Z.T, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → + ∀ ksb : U64, ¬ (ksb.val ≤ (wa t K).val ∧ (wa t K).val < ksb.val + ksSizeA) → True + tcb_offSc : ∀ t ∈ Z.T, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → + ∀ c ∈ Z.C, ∀ o : U64, o.val < 4096 → wa t K ≠ wa c o + +/-- The 128-word state-transfer window `[frame, frame + 1024)` lies inside `Ub` without wrap. -/ +def FrameWinA (Ub : Set U64) (frame : U64) : Prop := + frame.val + 1024 ≤ 2 ^ 64 ∧ + ∀ k : Nat, k < 128 → wa frame ⟨BitVec.ofNat 64 (8 * k)⟩ ∈ Ub + +/-- Joint state bundle preserved across every step of `read_state_to_frame` and `write_state_from_frame`. -/ +structure VcpuXferStateA {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + (AR : A → HwRootA → Prop) (Z : XSetsA) (OS Ub : Set U64) + (m : Fl4SpecA64.McState) (a : A) (s : S) (σ : State Unit) : Prop where + j : JAtZAX (L := L) AR Z s a σ m + tr : TreeA st OS Z.G.chains Z.G.par s + car : CarryA Z (L.mem s) σ + vcpu : VcpuRelA (L := L) Z OS Ub s σ + fmem : FrameMemRelA Z OS Ub (L.ksb s) (L.mem s) σ.machineState.underlyingMemory + regs : TcbRegsRelA (L.mem s) σ Z.T + +section xfer_frame +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +theorem writesIn_wrA {S : Type} {st : machine.Store S} [L : LawfulStore st] + (s : S) (b w : U64) (hba : Aligned b) : WritesIn st s (L.wr s b w) (· = b) := + ⟨L.ksb_wr s b w, L.cpu_wr s b w, fun _ hqa hne => mem_wr_neA hba hqa (Ne.symm hne)⟩ + +/-- Every word `b ∈ Ub` misses `FootA Z s`. -/ +theorem not_footA_of_ubA {Z : XSetsA} {OS Ub : Set U64} {s : S} + (hV : VcPlacedA Z OS Ub (L.ksb s)) + (hF : FrameMemRelA Z OS Ub (L.ksb s) (L.mem s) um) + (hU : UbFootDisjA Z OS Ub) + (hksFit : (L.ksb s).val + ksSizeA ≤ 2 ^ 64) + (hCs : ∀ c ∈ Z.Cs, c.val < 128) (hcpu : (L.cpu s).val < 128) + {b : U64} (hb : b ∈ Ub) : ¬ FootA (L := L) Z s b := by + have hks : ∀ o : U64, o.val < ksSizeA → b ≠ wa (L.ksb s) o := by + intro o ho heq + apply hF.disj_ks b hb + rw [heq, wa_valP, Nat.mod_eq_of_lt (by omega)] + omega + have hnode : ∀ c : U64, c.val < 128 → ∀ o : U64, o.val < 128 → b ≠ L.nodeAt s c o := by + intro c hc o ho + obtain ⟨k, hk, hne⟩ := nodeAt_eq_wa_ksbA (L := L) s c o hc (by omega) + rw [hne]; exact hks k hk + have hne_vc : ∀ v ∈ Z.V, b ≠ v ∧ b ≠ wa v 24#u64 := by + intro v hv + refine ⟨fun e => hV.disj_ub b ?_ hb, fun e => hV.disj_ub b ?_ hb⟩ + · rw [e] + have h0 : v = wa v 0#u64 := by apply UScalar.eq_of_val_eq; rw [wa_valP]; simp + rw [h0]; exact vcpuHdr_in_vcPage hv (by decide) (by decide) + · rw [e]; exact vcpuHdr_in_vcPage hv (by decide) (by decide) + intro hf + cases hf with + | slot y _ hy hw => + obtain ⟨h0, h8, h16, h24⟩ := hU.disj_slotW b hb y hy + rcases hw with he | he | he | he + · exact h0 he + · exact h8 he + · exact h16 he + · exact h24 he + | nslot _ hn => + cases hn with + | node c _ hc ho => + rcases ho with he | he | he | he + · exact hnode c (hCs c hc) layout.node.CUR_THREAD (by unfold layout.node.CUR_THREAD; decide) he + · exact hnode c (hCs c hc) layout.node.IDLE_THREAD (by unfold layout.node.IDLE_THREAD; decide) he + · exact hnode c (hCs c hc) layout.node.CUR_SC (by unfold layout.node.CUR_SC; decide) he + · exact hnode c (hCs c hc) layout.node.IDLE_SC (by unfold layout.node.IDLE_SC; decide) he + | cur _ ho => + rcases ho with he | he + · exact hnode (L.cpu s) hcpu layout.node.SCHED_ACTION (by unfold layout.node.SCHED_ACTION; decide) he + · exact hnode (L.cpu s) hcpu layout.node.CUR_TIME (by unfold layout.node.CUR_TIME; decide) he + | tcb t _ ht ho => + rcases ho with he | he | he | he | he + · exact hF.disj_tcb b hb t ht 3328#u64 (by decide) (by rw [← stA_eq]; exact he) + · exact hF.disj_tcb b hb t ht 3464#u64 (by decide) (by unfold scA at he; exact he) + · exact hF.disj_tcb b hb t ht 3520#u64 (by decide) (by unfold affA at he; exact he) + · exact hF.disj_tcb b hb t ht 3664#u64 (by decide) he + · exact hF.disj_tcb b hb t ht 3672#u64 (by decide) (by rw [← runVcpuA]; exact he) + | sc c hc => + exact hF.disj_sc _ hb c hc 24#u64 (by decide) rfl + | vcpu v _ hv ho => + rcases ho with he | he + · exact (hne_vc v hv).1 he + · exact (hne_vc v hv).2 he + | irq i hi => + apply hF.disj_ks _ hb + have hval : (irqWordA (L.ksb s) i).val = (L.ksb s).val + 224 + 8 * i.val := by + have hk := (L.ksb s).hBounds + simp only [ksSizeA] at hksFit + unfold irqWordA irqOffA + simp only [core.num.U64.wrapping_add_val_eq, core.num.U64.wrapping_mul_val_eq, + U64.numBits, UScalar.size, UScalarTy.numBits] at hk ⊢ + have h224 : (224#u64 : U64).val = 224 := rfl + have h8 : (8#u64 : U64).val = 8 := rfl + rw [h224, h8]; omega + rw [hval]; simp only [ksSizeA]; omega + | cur2 _ ho => + rcases ho with he | he + · exact hnode (L.cpu s) hcpu layout.node.CONSUMED (by unfold layout.node.CONSUMED; decide) he + · exact hnode (L.cpu s) hcpu layout.node.REPROGRAM (by unfold layout.node.REPROGRAM; decide) he + | glob _ ho => + rcases ho with he | he + · exact hks 8#u64 (by unfold ksSizeA; decide) he + · exact hks 16#u64 (by unfold ksSizeA; decide) he + | tcb2 t _ ht ho => + rcases ho with he | he + · exact hF.disj_tcb b hb t ht 3448#u64 (by decide) (by unfold prioA at he; exact he) + · exact hF.disj_tcb b hb t ht 3432#u64 (by decide) (by unfold domA at he; exact he) + | sc2 c hc => + exact hF.disj_sc _ hb c hc 72#u64 (by decide) rfl + | vs _ h => exact hU.disj_vs b hb _ h + | ext _ h => exact hU.disj_ext b hb h + +/-- `setUm` (updating only `machineState.underlyingMemory`) preserves `JAtZAX` untouched. -/ +theorem JAtZAX.of_setUm {AR : A → HwRootA → Prop} {Z : XSetsA} {s : S} {a : A} + {σ : State Unit} {m : Fl4SpecA64.McState} + (J : JAtZAX (L := L) AR Z s a σ m) (um' : Word → Word8) : + JAtZAX (L := L) AR Z s a (setUm σ um') m := + J.of_spec ⟨⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, ⟨rfl, rfl, rfl, rfl⟩⟩, + fun _ _ => rfl, fun _ _ h _ => h, rfl, rfl⟩ + +/-- **Atomic user-frame word store on `VcpuXferStateA`**: writing `w` at `b ∈ Ub` paired with +`setUm σ (storeMem um (specPtr b) w.bv)` preserves the entire 6-part `VcpuXferStateA` bundle! -/ +theorem VcpuXferStateA.ub_wr {AR : A → HwRootA → Prop} {Z : XSetsA} {OS Ub : Set U64} + {m : Fl4SpecA64.McState} {a : A} {s : S} {σ : State Unit} + (X : VcpuXferStateA (L := L) AR Z OS Ub m a s σ) + (hU : UbFootDisjA Z OS Ub) + (hCs : ∀ c ∈ Z.Cs, c.val < 128) (hcpu : (L.cpu s).val < 128) + (hfa : ∀ q, FootA (L := L) Z s q → Aligned q) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hIdleT : ∀ c ∈ Z.Cs, L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) ∈ Z.T) + {b : U64} (hb : b ∈ Ub) (w : U64) : + VcpuXferStateA (L := L) AR Z OS Ub m a (L.wr s b w) + (setUm σ (storeMem σ.machineState.underlyingMemory (specPtr b) w.bv)) := by + have hba : Aligned b := X.fmem.aligned b hb + have hw : WritesIn st s (L.wr s b w) (· = b) := writesIn_wrA s b w hba + have hnf : ¬ FootA (L := L) Z s b := + not_footA_of_ubA X.vcpu.place X.fmem hU X.j.1.ksFit hCs hcpu hb + have hJ1 : JAtZAX (L := L) AR Z (L.wr s b w) a σ m := + X.j.of_writesIn hw (fun q hq => by subst hq; exact hnf) hfa + have hJ2 := JAtZAX.of_setUm hJ1 (storeMem σ.machineState.underlyingMemory (specPtr b) w.bv) + have hosKeep : OSKeep st OS s (L.wr s b w) := by + intro y hy + have hal := hU.al_os y hy + obtain ⟨h0, h8, h16, h24⟩ := X.fmem.disj_os b hb y hy + exact ⟨mem_wr_neA hba hal h0, + mem_wr_neA hba (aligned_wa hal 8#u64 (by decide)) h8, + mem_wr_neA hba (aligned_wa hal 16#u64 (by decide)) h16, + mem_wr_neA hba (aligned_wa hal 24#u64 (by decide)) h24⟩ + have hTr' : TreeA st OS Z.G.chains Z.G.par (L.wr s b w) := X.tr.keepO hosKeep + have hcap_os : ∀ y ∈ OS, capAtA (L.mem (L.wr s b w)) y = capAtA (L.mem s) y := fun y hy => by + obtain ⟨h0, h8, -, -⟩ := hosKeep y hy; unfold capAtA; rw [h0, h8] + have hCar' : CarryA Z (L.mem (L.wr s b w)) + (setUm σ (storeMem σ.machineState.underlyingMemory (specPtr b) w.bv)) := by + refine ⟨X.car.1.of_capsA rfl (fun _ h => h) (fun _ h => h) (fun y hy => Or.inl (hcap_os y (hsub y hy))), + X.car.2.of_keepA (fun _ _ _ h => ⟨_, h⟩) (fun _ _ h => ⟨_, h⟩) fun y hy => ?_⟩ + rw [hcap_os y ((X.tr.lk.members y).1 hy).1] + exact CapFinEqA.refl _ + have hV' := X.vcpu.frame_ub_wrA X.fmem X.j.1.ksFit hCs hIdleT hb w + have hF' := storeWordOffs_frameMemA X.fmem hU.al_os hb w + have hR' : TcbRegsRelA (L.mem (L.wr s b w)) + (setUm σ (storeMem σ.machineState.underlyingMemory (specPtr b) w.bv)) Z.T := by + intro t ht x hkt r + have hlt : (regIdxA r).val < 37 := regIdxA_lt r + have hm8 : (core.num.U64.wrapping_mul (regIdxA r) 8#u64).val = (regIdxA r).val * 8 := by + rw [wm_valP]; change (regIdxA r).val * 8 % 2 ^ 64 = _; exact Nat.mod_eq_of_lt (by omega) + have hK_val : (wa 256#u64 (core.num.U64.wrapping_mul (regIdxA r) 8#u64)).val = 256 + (regIdxA r).val * 8 := by + rw [wa_valP, hm8]; change (256 + (regIdxA r).val * 8) % 2 ^ 64 = _; exact Nat.mod_eq_of_lt (by omega) + have hK : (wa 256#u64 (core.num.U64.wrapping_mul (regIdxA r) 8#u64)).val < 4096 := by + rw [hK_val]; omega + have hreg_eq : Endp.regA t (regIdxA r) = wa t (wa 256#u64 (core.num.U64.wrapping_mul (regIdxA r) 8#u64)) := by + apply u64_eq_of_bv_eq + have hwa_bv : ∀ x y : U64, (wa x y).bv = x.bv + y.bv := fun x y => by + apply BitVec.eq_of_toNat_eq; change (wa x y).val = (x.bv + y.bv).toNat + rw [wa_valP, BitVec.toNat_add]; rfl + unfold Endp.regA layout.tcb.REGS + simp only [hwa_bv, BitVec.add_assoc] + have hne : Endp.regA t (regIdxA r) ≠ b := fun e => + X.fmem.disj_tcb b hb t ht _ hK (hreg_eq ▸ e.symm) + have hal_r : Aligned (Endp.regA t (regIdxA r)) := by + rw [hreg_eq] + have hta := hU.tcb_al t ht + have hal_t : Aligned t := by + unfold Aligned; have := hta.align; omega + exact aligned_wa hal_t _ (by rw [hK_val]; omega) + rw [mem_wr_neA hba hal_r hne.symm] + exact X.regs t ht x hkt r + exact ⟨hJ2, hTr', hCar', hV', hF', hR'⟩ + +theorem regA_eq_wa_KA (t : U64) (r : Register) : + let K := wa 256#u64 (core.num.U64.wrapping_mul (regIdxA r) 8#u64) + K.val = 256 + (regIdxA r).val * 8 ∧ Endp.regA t (regIdxA r) = wa t K := by + have hlt : (regIdxA r).val < 37 := regIdxA_lt r + have hm8 : (core.num.U64.wrapping_mul (regIdxA r) 8#u64).val = (regIdxA r).val * 8 := by + rw [wm_valP]; change (regIdxA r).val * 8 % 2 ^ 64 = _; exact Nat.mod_eq_of_lt (by omega) + have hKval : (wa 256#u64 (core.num.U64.wrapping_mul (regIdxA r) 8#u64)).val = 256 + (regIdxA r).val * 8 := by + rw [wa_valP, hm8]; change (256 + (regIdxA r).val * 8) % 2 ^ 64 = _; exact Nat.mod_eq_of_lt (by omega) + refine ⟨hKval, ?_⟩ + apply u64_eq_of_bv_eq + have hwa_bv : ∀ x y : U64, (wa x y).bv = x.bv + y.bv := fun x y => by + apply BitVec.eq_of_toNat_eq; change (wa x y).val = (x.bv + y.bv).toNat + rw [wa_valP, BitVec.toNat_add]; rfl + unfold Endp.regA layout.tcb.REGS + simp only [hwa_bv, BitVec.add_assoc] + +/-- **Atomic non-link VCPU write on `VcpuXferStateA`**: writing inside `VcPage Z.V` off `{v, wa v 24}` +paired with `updKSc σ (specPtr vc) (.archObj (.vcpu v'))` preserves `VcpuXferStateA`! -/ +theorem VcpuXferStateA.vc_nonlink_wr {AR : A → HwRootA → Prop} {Z : XSetsA} {OS Ub : Set U64} + {m : Fl4SpecA64.McState} {a : A} {s s' : S} {σ : State Unit} + (X : VcpuXferStateA (L := L) AR Z OS Ub m a s σ) + (hCs : ∀ c ∈ Z.Cs, c.val < 128) (hcpu : (L.cpu s).val < 128) + (hExt : ∀ b, VcPage Z.V b → ¬ ExtFootE Z.X Z.Y Z.T2 b) + (hSw : ∀ b, VcPage Z.V b → ∀ y, SlotWA Z.th y → + b ≠ y ∧ b ≠ wa y 8#u64 ∧ b ≠ wa y 16#u64 ∧ b ≠ wa y 24#u64) + (hfa : ∀ q, FootA (L := L) Z s q → Aligned q) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hU : UbFootDisjA Z OS Ub) + {W : U64 → Prop} (hw : WritesIn st s s' W) + (hWvc : ∀ b, W b → VcPage Z.V b ∧ ∀ v ∈ Z.V, b ≠ v ∧ b ≠ wa v 24#u64) + {vc : U64} (hvc : vc ∈ Z.V) {v v' : Vcpu} + (hk : σ.kheap (specPtr vc) = some (.archObj (.vcpu v))) + (htcb : v'.vcpuTcb = v.vcpuTcb) (hrc : v'.vcpuExt.runCaller = v.vcpuExt.runCaller) + (hV' : VcpuRelA (L := L) Z OS Ub s' (updKSc σ (specPtr vc) (.archObj (.vcpu v')))) : + VcpuXferStateA (L := L) AR Z OS Ub m a s' (updKSc σ (specPtr vc) (.archObj (.vcpu v'))) := by + obtain ⟨hJ', hTr', hCarF⟩ := + vcpuNonLinkWr_frameA X.j X.tr X.vcpu.place hCs hcpu hExt hSw hfa hw hWvc hvc hk htcb hrc hsub + have hF' : FrameMemRelA Z OS Ub (L.ksb s') (L.mem s') + (updKSc σ (specPtr vc) (.archObj (.vcpu v'))).machineState.underlyingMemory := by + have hks' : L.ksb s' = L.ksb s := hw.1 + have hosKeep : ∀ y ∈ OS, capAtA (L.mem s') y = capAtA (L.mem s) y := fun y hy => by + have hal := hU.al_os y hy + have hdisj : ∀ b, W b → b ≠ y ∧ b ≠ wa y 8#u64 := fun b hb => by + obtain ⟨h1, h2, -, -⟩ := X.vcpu.place.disj_os b (hWvc b hb).1 y hy; exact ⟨h1, h2⟩ + unfold capAtA + rw [hw.2.2 y hal (fun h => (hdisj y h).1 rfl), + hw.2.2 (wa y 8#u64) (aligned_wa hal 8#u64 (by decide)) (fun h => (hdisj _ h).2 rfl)] + refine ⟨X.fmem.aligned, fun b hb => ?_, fun y hy ptr sz r mp s2 asid va hc => ?_, + X.fmem.disj_os, X.fmem.disj_slots, X.fmem.disj_tcb, X.fmem.disj_sc, ?_⟩ + · rw [hw.2.2 b (X.fmem.aligned b hb) (fun h => X.vcpu.place.disj_ub b (hWvc b h).1 hb)] + exact X.fmem.agree b hb + · rw [hosKeep y hy] at hc; exact X.fmem.frames y hy ptr sz r mp s2 asid va hc + · rw [hks']; exact X.fmem.disj_ks + have hR' : TcbRegsRelA (L.mem s') (updKSc σ (specPtr vc) (.archObj (.vcpu v'))) Z.T := by + intro t ht x hkt r + have hne_sp : specPtr t ≠ specPtr vc := by + intro e; rw [e, updKSc_same] at hkt; cases hkt + dsimp only [updKSc] at hkt; rw [Function.update_of_ne hne_sp] at hkt + have hlt : (regIdxA r).val < 37 := regIdxA_lt r + obtain ⟨hKval, hreg_eq⟩ := regA_eq_wa_KA t r + have hal_r : Aligned (Endp.regA t (regIdxA r)) := by + rw [hreg_eq] + have hta := hU.tcb_al t ht + have hal_t : Aligned t := by unfold Aligned; have := hta.align; omega + exact aligned_wa hal_t _ (by omega) + have hnotW : ¬ W (Endp.regA t (regIdxA r)) := by + intro hW + obtain ⟨v0, hv0, ov, hov, _, hveq⟩ := (hWvc _ hW).1 + exact X.vcpu.place.disj_tcb v0 hv0 t ht ov _ hov (by omega) (hveq.symm.trans hreg_eq) + rw [hw.2.2 _ hal_r hnotW] + exact X.regs t ht x hkt r + exact ⟨hJ', hTr', hCarF X.car, hV', hF', hR'⟩ + +/-- **Atomic TCB user-register write on `VcpuXferStateA`**: writing `w` to `Endp.regA t (regIdxA r)` for +`t ∈ Z.T` paired with `ctxUpdA σ (specPtr t) x (setUserReg ...)` preserves `VcpuXferStateA`! -/ +theorem VcpuXferStateA.tcb_reg_wr {AR : A → HwRootA → Prop} {Z : XSetsA} {OS Ub : Set U64} + {m : Fl4SpecA64.McState} {a : A} {s : S} {σ : State Unit} + (X : VcpuXferStateA (L := L) AR Z OS Ub m a s σ) + (hU : UbFootDisjA Z OS Ub) + (hCs : ∀ c ∈ Z.Cs, c.val < 128) (hcpu : (L.cpu s).val < 128) + (hfa : ∀ q, FootA (L := L) Z s q → Aligned q) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hIdleT : ∀ c ∈ Z.Cs, L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) ∈ Z.T) + (hksTcb : ∀ t ∈ Z.T, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → + ∀ o : U64, o.val < ksSizeA → wa t K ≠ wa (L.ksb s) o) + (hSlotTcb : ∀ t ∈ Z.T, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → + ∀ y, SlotWA Z.th y → wa t K ≠ y ∧ wa t K ≠ wa y 8#u64 ∧ wa t K ≠ wa y 16#u64 ∧ wa t K ≠ wa y 24#u64) + {t : U64} (ht : t ∈ Z.T) {x : Tcb} + (hkt : σ.kheap (specPtr t) = some (.tcb x)) (r : Register) (w : U64) : + let uc' : UserContext := { x.tcbArch.tcbContext with userRegs := Function.update x.tcbArch.tcbContext.userRegs r w.bv } + VcpuXferStateA (L := L) AR Z OS Ub m a (L.wr s (Endp.regA t (regIdxA r)) w) + (ctxUpdA σ (specPtr t) x uc') := by + dsimp only + set K : U64 := wa 256#u64 (core.num.U64.wrapping_mul (regIdxA r) 8#u64) + have hlt : (regIdxA r).val < 37 := regIdxA_lt r + obtain ⟨hKval0, hreg_eq⟩ := regA_eq_wa_KA t r + have hKval : K.val = 256 + (regIdxA r).val * 8 := hKval0 + have hK1 : 256 ≤ K.val := by omega + have hK2 : K.val < 3328 := by omega + have hta : TcbAddr t := hU.tcb_al t ht + have hal_t : Aligned t := by unfold Aligned; have := hta.align; omega + have hal_r : Aligned (Endp.regA t (regIdxA r)) := by + rw [hreg_eq]; exact aligned_wa hal_t _ (by omega) + have hnf : ¬ FootA (L := L) Z s (wa t K) := by + have hnode : ∀ c : U64, c.val < 128 → ∀ o : U64, o.val < 128 → wa t K ≠ L.nodeAt s c o := by + intro c hc o ho + obtain ⟨k, hk, hne⟩ := nodeAt_eq_wa_ksbA (L := L) s c o hc (by omega) + rw [hne]; exact hksTcb t ht K hK1 hK2 k hk + have htcb_ne : ∀ y ∈ Z.T, ∀ o : U64, 3328 ≤ o.val → o.val < 4096 → wa t K ≠ wa y o := by + intro y hy o h1 h2 heq + have hya := hU.tcb_al y hy + exact field_ne hta hya (by omega) h2 (fun _ e => by omega) heq + intro hf + generalize hb : wa t K = b at hf + cases hf with + | slot y _ hy hw => + obtain ⟨h0, h8, h16, h24⟩ := hSlotTcb t ht K hK1 hK2 y hy + rcases hw with he | he | he | he + · exact h0 (hb.trans he) + · exact h8 (hb.trans he) + · exact h16 (hb.trans he) + · exact h24 (hb.trans he) + | nslot _ hn => + cases hn with + | node c _ hc ho => + rcases ho with he | he | he | he + · exact hnode c (hCs c hc) layout.node.CUR_THREAD (by unfold layout.node.CUR_THREAD; decide) (hb.trans he) + · exact hnode c (hCs c hc) layout.node.IDLE_THREAD (by unfold layout.node.IDLE_THREAD; decide) (hb.trans he) + · exact hnode c (hCs c hc) layout.node.CUR_SC (by unfold layout.node.CUR_SC; decide) (hb.trans he) + · exact hnode c (hCs c hc) layout.node.IDLE_SC (by unfold layout.node.IDLE_SC; decide) (hb.trans he) + | cur _ ho => + rcases ho with he | he + · exact hnode (L.cpu s) hcpu layout.node.SCHED_ACTION (by unfold layout.node.SCHED_ACTION; decide) (hb.trans he) + · exact hnode (L.cpu s) hcpu layout.node.CUR_TIME (by unfold layout.node.CUR_TIME; decide) (hb.trans he) + | tcb y _ hy ho => + rcases ho with he | he | he | he | he + · exact htcb_ne y hy 3328#u64 (by decide) (by decide) (by rw [← stA_eq]; exact hb.trans he) + · exact htcb_ne y hy 3464#u64 (by decide) (by decide) (by unfold scA at he; exact hb.trans he) + · exact htcb_ne y hy 3520#u64 (by decide) (by decide) (by unfold affA at he; exact hb.trans he) + · exact htcb_ne y hy 3664#u64 (by decide) (by decide) (hb.trans he) + · exact htcb_ne y hy 3672#u64 (by decide) (by decide) (by rw [← runVcpuA]; exact hb.trans he) + | sc c hc => + exact hU.tcb_offSc t ht K hK1 hK2 c hc 24#u64 (by decide) hb + | vcpu v _ hv ho => + rcases ho with he | he + · have h0 : v = wa v 0#u64 := by apply UScalar.eq_of_val_eq; rw [wa_valP]; simp + exact X.vcpu.place.disj_tcb v hv t ht 0#u64 K (by decide) (by omega) (h0.symm.trans (hb.trans he).symm) + · exact X.vcpu.place.disj_tcb v hv t ht 24#u64 K (by decide) (by omega) (hb.trans he).symm + | irq i hi => + exact hksTcb t ht K hK1 hK2 (irqOffA i) (by + unfold irqOffA ksSizeA + simp only [core.num.U64.wrapping_add_val_eq, core.num.U64.wrapping_mul_val_eq, + UScalar.size, UScalarTy.numBits] + have h224 : (224#u64 : U64).val = 224 := rfl + have h8 : (8#u64 : U64).val = 8 := rfl + rw [h224, h8]; omega) hb + | cur2 _ ho => + rcases ho with he | he + · exact hnode (L.cpu s) hcpu layout.node.CONSUMED (by unfold layout.node.CONSUMED; decide) (hb.trans he) + · exact hnode (L.cpu s) hcpu layout.node.REPROGRAM (by unfold layout.node.REPROGRAM; decide) (hb.trans he) + | glob _ ho => + rcases ho with he | he + · exact hksTcb t ht K hK1 hK2 8#u64 (by unfold ksSizeA; decide) (hb.trans he) + · exact hksTcb t ht K hK1 hK2 16#u64 (by unfold ksSizeA; decide) (hb.trans he) + | tcb2 y _ hy ho => + rcases ho with he | he + · exact htcb_ne y hy 3448#u64 (by decide) (by decide) (by unfold prioA at he; exact hb.trans he) + · exact htcb_ne y hy 3432#u64 (by decide) (by decide) (by unfold domA at he; exact hb.trans he) + | sc2 c hc => + exact hU.tcb_offSc t ht K hK1 hK2 c hc 72#u64 (by decide) hb + | vs _ h => exact hU.tcb_offVs t ht K hK1 hK2 _ (hb ▸ h) + | ext _ h => exact hU.tcb_offExt t ht K hK1 hK2 (hb ▸ h) + have hJ1 : JAtZAX (L := L) AR Z (L.wr s (Endp.regA t (regIdxA r)) w) a σ m := + X.j.of_writesIn (writesIn_wrA s (Endp.regA t (regIdxA r)) w hal_r) (fun q hq => by subst hq; rw [hreg_eq]; exact hnf) hfa + set uc' : UserContext := { x.tcbArch.tcbContext with userRegs := Function.update x.tcbArch.tcbContext.userRegs r w.bv } + have hJ2 : JAtZAX (L := L) AR Z (L.wr s (Endp.regA t (regIdxA r)) w) a (ctxUpdA σ (specPtr t) x uc') m := + hJ1.of_tcbCtxA hkt uc' + have hosKeep : OSKeep st OS s (L.wr s (Endp.regA t (regIdxA r)) w) := by + intro y hy + have hal := hU.al_os y hy + obtain ⟨h0, h8, h16, h24⟩ := hU.tcb_offOs t ht K hK1 hK2 y hy + rw [hreg_eq] + exact ⟨mem_wr_neA (hreg_eq ▸ hal_r) hal h0, + mem_wr_neA (hreg_eq ▸ hal_r) (aligned_wa hal 8#u64 (by decide)) h8, + mem_wr_neA (hreg_eq ▸ hal_r) (aligned_wa hal 16#u64 (by decide)) h16, + mem_wr_neA (hreg_eq ▸ hal_r) (aligned_wa hal 24#u64 (by decide)) h24⟩ + have hTr' : TreeA st OS Z.G.chains Z.G.par (L.wr s (Endp.regA t (regIdxA r)) w) := X.tr.keepO hosKeep + have hcap_os : ∀ y ∈ OS, capAtA (L.mem (L.wr s (Endp.regA t (regIdxA r)) w)) y = capAtA (L.mem s) y := fun y hy => by + obtain ⟨h0, h8, -, -⟩ := hosKeep y hy; unfold capAtA; rw [h0, h8] + have hTy := TyKeepA.ctxUpdA hkt uc' + have hCar' : CarryA Z (L.mem (L.wr s (Endp.regA t (regIdxA r)) w)) (ctxUpdA σ (specPtr t) x uc') := by + refine ⟨X.car.1.of_capsA rfl (fun _ h => h) (fun _ h => h) (fun y hy => Or.inl (hcap_os y (hsub y hy))), + X.car.2.of_keepA hTy.1 hTy.2 fun y hy => ?_⟩ + rw [hcap_os y ((X.tr.lk.members y).1 hy).1] + exact CapFinEqA.refl _ + have hne_vc : ∀ b, VcPage Z.V b → Endp.regA t (regIdxA r) ≠ b := by + intro b hb e + obtain ⟨v0, hv0, ov, hov, _, rfl⟩ := hb + exact X.vcpu.place.disj_tcb v0 hv0 t ht ov K hov (by omega) (e.symm.trans hreg_eq) + have hne_tcb_hi : ∀ y ∈ Z.T, ∀ o : U64, 3328 ≤ o.val → o.val < 4096 → Endp.regA t (regIdxA r) ≠ wa y o := by + intro y hy o h1 h2 e + rw [hreg_eq] at e + exact field_ne hta (hU.tcb_al y hy) (by omega) h2 (fun _ h => by omega) e + have hV' : VcpuRelA (L := L) Z OS Ub (L.wr s (Endp.regA t (regIdxA r)) w) (ctxUpdA σ (specPtr t) x uc') := by + have hmem_vc : ∀ v ∈ Z.V, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → + L.mem (L.wr s (Endp.regA t (regIdxA r)) w) (wa v o) = L.mem s (wa v o) := by + intro v hv o ho hal + exact mem_wr_neA hal_r (aligned_wa (X.vcpu.place.vc_al hv) o hal) (hne_vc _ (vcpuHdr_in_vcPage hv ho hal)) + have hmem_vc0 : ∀ v ∈ Z.V, L.mem (L.wr s (Endp.regA t (regIdxA r)) w) v = L.mem s v := fun v hv => by + have h0 : v = wa v 0#u64 := by apply UScalar.eq_of_val_eq; rw [wa_valP]; simp + rw [h0]; exact hmem_vc v hv 0#u64 (by decide) (by decide) + have hmem_tcb_hi : ∀ y ∈ Z.T, ∀ o : U64, 3328 ≤ o.val → o.val < 4096 → o.val % 8 = 0 → + L.mem (L.wr s (Endp.regA t (regIdxA r)) w) (wa y o) = L.mem s (wa y o) := by + intro y hy o h1 h2 hal + have hya := hU.tcb_al y hy + have hal_y : Aligned y := by unfold Aligned; have := hya.align; omega + exact mem_wr_neA hal_r (aligned_wa hal_y o hal) (hne_tcb_hi y hy o h1 h2) + have hksb' : L.ksb (L.wr s (Endp.regA t (regIdxA r)) w) = L.ksb s := L.ksb_wr _ _ _ + have hkh_vc : ∀ p v, (ctxUpdA σ (specPtr t) x uc').kheap p = some (.archObj (.vcpu v)) ↔ + σ.kheap p = some (.archObj (.vcpu v)) := by + intro p v + by_cases ep : p = specPtr t + · subst ep + have h1 : (ctxUpdA σ (specPtr t) x uc').kheap (specPtr t) = + some (.tcb { x with tcbArch := { x.tcbArch with tcbContext := uc' } }) := by + unfold ctxUpdA setObjA; exact Function.update_self _ _ _ + rw [h1, hkt]; constructor <;> (intro h; cases h) + · have h1 : (ctxUpdA σ (specPtr t) x uc').kheap p = σ.kheap p := by + unfold ctxUpdA setObjA; exact Function.update_of_ne ep _ _ + rw [h1] + refine ⟨hJ2.2.1, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, X.vcpu.arch, ?_⟩ + · intro v hv + rw [hmem_vc v hv 8#u64 (by decide) (by decide)]; exact X.vcpu.owner v hv + · intro v hv xv hkv + rw [hkh_vc] at hkv + rw [hmem_vc v hv 16#u64 (by decide) (by decide)]; exact X.vcpu.vppi v hv xv hkv + · intro v hv xv hkv + rw [hkh_vc] at hkv + rw [hmem_vc v hv 32#u64 (by decide) (by decide)]; exact X.vcpu.runOff v hv xv hkv + · intro v hv xv hkv + rw [hkh_vc] at hkv + rw [hmem_vc v hv 40#u64 (by decide) (by decide)]; exact X.vcpu.runOut v hv xv hkv + · intro v hv xv hkv i hi + rw [hkh_vc] at hkv + rw [vcpuRegWA, hmem_vc v hv (vcpuRegOffA i) (by rw [vcpuRegOffA_val hi]; omega) (by rw [vcpuRegOffA_val hi]; omega)] + exact X.vcpu.regs v hv xv hkv i hi + · intro v hv y hy hnz + rw [hmem_vc0 v hv] at hy + obtain ⟨ht', hbv⟩ := X.vcpu.bind_v2t v hv y hy hnz + exact ⟨ht', by rw [hmem_tcb_hi _ ht' 3664#u64 (by decide) (by decide) (by decide)]; exact hbv⟩ + · intro y hy v hv hnz + rw [hmem_tcb_hi y hy 3664#u64 (by decide) (by decide) (by decide)] at hv + obtain ⟨hv', htv⟩ := X.vcpu.bind_t2v y hy v hv hnz + exact ⟨hv', by rw [hmem_vc0 _ hv']; exact htv⟩ + · intro v hv c hc hnz + rw [hmem_vc v hv 24#u64 (by decide) (by decide)] at hc + obtain ⟨ht', hrv⟩ := X.vcpu.run_v2c v hv c hc hnz + exact ⟨ht', by rw [runVcpuA, hmem_tcb_hi _ ht' 3672#u64 (by decide) (by decide) (by decide)]; exact hrv⟩ + · intro c hc v hv hnz + rw [runVcpuA, hmem_tcb_hi c hc 3672#u64 (by decide) (by decide) (by decide)] at hv + obtain ⟨hv', hrc⟩ := X.vcpu.run_c2v c hc v hv hnz + exact ⟨hv', by rw [hmem_vc _ hv' 24#u64 (by decide) (by decide)]; exact hrc⟩ + · intro c hc hnz + rw [runVcpuA, hmem_tcb_hi c hc 3672#u64 (by decide) (by decide) (by decide)] at hnz + rw [stA_eq_wa, hmem_tcb_hi c hc 3328#u64 (by decide) (by decide) (by decide), ← stA_eq_wa] + exact X.vcpu.run_state c hc hnz + · intro y hy hnz + rw [hmem_tcb_hi y hy 3664#u64 (by decide) (by decide) (by decide)] at hnz + rw [runVcpuA, hmem_tcb_hi y hy 3672#u64 (by decide) (by decide) (by decide)] + exact X.vcpu.bind_run_disj y hy hnz + · intro c hc + have hit := hIdleT c hc + have hne_idle : Endp.regA t (regIdxA r) ≠ L.nodeAt s c layout.node.IDLE_THREAD := by + rw [hreg_eq] + obtain ⟨k, hk, hne⟩ := nodeAt_eq_wa_ksbA (L := L) s c layout.node.IDLE_THREAD (hCs c hc) (by unfold layout.node.IDLE_THREAD; decide) + rw [hne]; exact hksTcb t ht K hK1 hK2 k hk + have hal_idle : Aligned (L.nodeAt s c layout.node.IDLE_THREAD) := + hfa _ (FootA.nslot _ (FootNA.node c _ hc (Or.inr (Or.inl rfl)))) + rw [nodeAt_wrA, mem_wr_neA hal_r hal_idle hne_idle, + runVcpuA, + hmem_tcb_hi _ hit 3664#u64 (by decide) (by decide) (by decide), + hmem_tcb_hi _ hit 3672#u64 (by decide) (by decide) (by decide)] + exact X.vcpu.idle_no_vcpu c hc + · intro v hv h + rw [hmem_vc0 v hv, hmem_vc v hv 24#u64 (by decide) (by decide)] at h + obtain ⟨y, hy, hc⟩ := X.vcpu.live v hv h + exact ⟨y, hy, by rw [hcap_os y hy]; exact hc⟩ + · rw [hksb']; exact X.vcpu.place + have hF' : FrameMemRelA Z OS Ub (L.ksb (L.wr s (Endp.regA t (regIdxA r)) w)) + (L.mem (L.wr s (Endp.regA t (regIdxA r)) w)) (ctxUpdA σ (specPtr t) x uc').machineState.underlyingMemory := by + refine ⟨X.fmem.aligned, fun b hb => ?_, fun y hy ptr sz r' mp s2 asid va hc => ?_, + X.fmem.disj_os, X.fmem.disj_slots, X.fmem.disj_tcb, X.fmem.disj_sc, ?_⟩ + · rw [mem_wr_neA hal_r (X.fmem.aligned b hb) (fun e => X.fmem.disj_tcb b hb t ht K (by omega) (hreg_eq ▸ e.symm))] + exact X.fmem.agree b hb + · rw [hcap_os y hy] at hc; exact X.fmem.frames y hy ptr sz r' mp s2 asid va hc + · rw [L.ksb_wr]; exact X.fmem.disj_ks + have hR' : TcbRegsRelA (L.mem (L.wr s (Endp.regA t (regIdxA r)) w)) (ctxUpdA σ (specPtr t) x uc') Z.T := by + intro y hy xy hky r' + by_cases ey : y = t + · subst ey + have hxy : xy = { x with tcbArch := { x.tcbArch with tcbContext := uc' } } := by + have h1 : (ctxUpdA σ (specPtr y) x uc').kheap (specPtr y) = + some (.tcb { x with tcbArch := { x.tcbArch with tcbContext := uc' } }) := by + unfold ctxUpdA setObjA; exact Function.update_self _ _ _ + rw [h1] at hky; injection hky with hky; injection hky with hky; exact hky.symm + subst hxy + by_cases er : r' = r + · subst er + rw [L.mem_wr_same hal_r] + exact (Function.update_self r' w.bv x.tcbArch.tcbContext.userRegs).symm + · have hne_idx : (regIdxA r').val ≠ (regIdxA r).val := fun e => er (regIdxA_inj e) + have hlt' : (regIdxA r').val < 37 := regIdxA_lt r' + set K' : U64 := wa 256#u64 (core.num.U64.wrapping_mul (regIdxA r') 8#u64) + obtain ⟨hK'val, hreg'_eq⟩ := regA_eq_wa_KA y r' + have hal_r' : Aligned (Endp.regA y (regIdxA r')) := by + rw [hreg'_eq]; exact aligned_wa hal_t _ (by omega) + have hne_reg : Endp.regA y (regIdxA r) ≠ Endp.regA y (regIdxA r') := by + rw [hreg'_eq, hreg_eq] + exact field_ne hta hta (by omega) (by omega) (fun _ e => by omega) + rw [mem_wr_neA hal_r hal_r' hne_reg] + change (L.mem s (Endp.regA y (regIdxA r'))).bv = Function.update x.tcbArch.tcbContext.userRegs r w.bv r' + rw [Function.update_of_ne er] + exact X.regs y hy x hkt r' + · have hne_sp : specPtr y ≠ specPtr t := fun e => ey (specPtr_inj e) + have h1 : (ctxUpdA σ (specPtr t) x uc').kheap (specPtr y) = σ.kheap (specPtr y) := by + unfold ctxUpdA setObjA; exact Function.update_of_ne hne_sp _ _ + rw [h1] at hky + have hlt' : (regIdxA r').val < 37 := regIdxA_lt r' + set K' : U64 := wa 256#u64 (core.num.U64.wrapping_mul (regIdxA r') 8#u64) + obtain ⟨hK'val, hreg'_eq⟩ := regA_eq_wa_KA y r' + have hya := hU.tcb_al y hy + have hal_y : Aligned y := by unfold Aligned; have := hya.align; omega + have hal_r' : Aligned (Endp.regA y (regIdxA r')) := by + rw [hreg'_eq]; exact aligned_wa hal_y _ (by omega) + have hne_reg : Endp.regA t (regIdxA r) ≠ Endp.regA y (regIdxA r') := by + rw [hreg'_eq, hreg_eq] + exact field_ne hta hya (by omega) (by omega) (fun e _ => ey e.symm) + rw [mem_wr_neA hal_r hal_r' hne_reg] + exact X.regs y hy xy hky r' + exact ⟨hJ2, hTr', hCar', hV', hF', hR'⟩ + +end xfer_frame + +/-! ## Section 1: `vcpu.read_vgic_frame` refinement -/ + +section read_vgic +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +theorem al3_of_frameWinA {Z : XSetsA} {OS Ub : Set U64} {ksb : U64} {mem : U64 → U64} {um : Word → Word8} + (hF : FrameMemRelA Z OS Ub ksb mem um) {frame : U64} (hWin : FrameWinA Ub frame) : + ∀ k < 128, bufAddr (specPtr frame) k &&& mask 3 = 0 := + fun k hk => al3_of_aligned_bufAddr (by have := hWin.1; omega) (hF.aligned _ (hWin.2 k hk)) + +/-- Specification of `readVgicFrame` on a state `σ`. -/ +theorem readVgicFrame_eqA (frame : U64) (σ : State Unit) + (hal : ∀ k < 128, bufAddr (specPtr frame) k &&& mask 3 = 0) : + let lrs := (List.range fl4NumLrs).map fun i => loadWordAt σ.machineState.underlyingMemory (bufAddr (specPtr frame) (vrVgicLr0 + i)) + let hcr := loadWordAt σ.machineState.underlyingMemory (bufAddr (specPtr frame) vrVgicHcr) + let san := lrs.map fun w => sanitizeVgicLr w fl4GicV3 + readVgicFrame (specPtr frame) σ = + ({(if san.all Option.isSome then some (sanitizeVgicHcr hcr fl4GicV3, san.map fun o => o.getD 0) else none, σ)}, False) := by + dsimp only + unfold readVgicFrame + have hmap : ∀ (xs : List Nat), (∀ i ∈ xs, i < 16) → + NonDet.mapM (fun i => loadWordOffs (Z := Unit) (specPtr frame) (vrVgicLr0 + i)) xs σ = + ({(xs.map fun i => loadWordAt σ.machineState.underlyingMemory (bufAddr (specPtr frame) (vrVgicLr0 + i)), σ)}, False) := by + intro xs hxs; induction xs with + | nil => rfl + | cons a tl ih => + unfold NonDet.mapM + have ha16 : a < 16 := hxs a (List.mem_cons_self ..) + change (loadWordOffs (Z := Unit) (specPtr frame) (vrVgicLr0 + a) >>= fun b => + NonDet.mapM (fun i => loadWordOffs (Z := Unit) (specPtr frame) (vrVgicLr0 + i)) tl >>= fun bs => + NonDet.pure (b :: bs)) σ = _ + rw [nd_bind_oneSc (loadWordOffs_eqA (specPtr frame) (vrVgicLr0 + a) σ (hal _ (by unfold vrVgicLr0; omega))), + nd_bind_oneSc (ih (fun i hi => hxs i (List.mem_cons_of_mem _ hi)))]; rfl + rw [nd_bind_oneSc (hmap (List.range fl4NumLrs) (fun i hi => by have := fl4NumLrs_le_16; rw [List.mem_range] at hi; omega)), + nd_bind_oneSc (loadWordOffs_eqA (specPtr frame) vrVgicHcr σ (hal _ (by unfold vrVgicHcr; decide)))] + split <;> rfl + +/-- **`vcpu.read_vgic_frame` refines `readVgicFrame`**: returns `ok none` iff the spec returns +`none`, and `ok (some (hcr, arr))` iff the spec returns `some (hcr.bv, lrsList)` with matching +`lrsList` elements at `0 .. fl4NumLrs - 1`. -/ +theorem read_vgic_frame_refinesA {Z : XSetsA} {OS Ub : Set U64} {s : S} {σ : State Unit} + (hF : FrameMemRelA Z OS Ub (L.ksb s) (L.mem s) σ.machineState.underlyingMemory) + {frame : U64} (hWin : FrameWinA Ub frame) : + vcpu.read_vgic_frame st s frame ⟨BitVec.ofNat 64 fl4NumLrs⟩ fl4GicV3 ⦃ res => + match res with + | none => readVgicFrame (specPtr frame) σ = ({(none, σ)}, False) + | some (hcr, arr) => + ∃ lrsList : List Word, + lrsList.length = fl4NumLrs ∧ + (∀ j (hj : j < fl4NumLrs), (arr.val[j]!).bv = lrsList[j]!) ∧ + readVgicFrame (specPtr frame) σ = ({(some (hcr.bv, lrsList), σ)}, False) ⦄ := by + have hlrs16 : fl4NumLrs ≤ 16 := fl4NumLrs_le_16 + have hlrs_val : (⟨BitVec.ofNat 64 fl4NumLrs⟩ : U64).val = fl4NumLrs := by + change (BitVec.ofNat 64 fl4NumLrs).toNat = fl4NumLrs + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + let rawLr : Nat → Word := fun j => + loadWordAt σ.machineState.underlyingMemory (bufAddr (specPtr frame) (vrVgicLr0 + j)) + let sanLr : Nat → Option Word := fun j => sanitizeVgicLr (rawLr j) fl4GicV3 + let hcrWord : Word := loadWordAt σ.machineState.underlyingMemory (bufAddr (specPtr frame) vrVgicHcr) + have hmem_word : ∀ k, k < 128 → + (L.mem s (wa frame ⟨BitVec.ofNat 64 (8 * k)⟩)).bv = + loadWordAt σ.machineState.underlyingMemory (bufAddr (specPtr frame) k) := by + intro k hk + have h1 := hF.agree _ (hWin.2 k hk) + rw [specPtr_wa_wordA (by have := hWin.1; omega)] at h1 + exact h1 + unfold vcpu.read_vgic_frame vcpu.read_vgic_frame_loop + let Inv : (Array U64 16#usize × U64) → Prop := fun (out, i) => + i.val ≤ fl4NumLrs ∧ + ∀ j, j < i.val → ∃ w, sanLr j = some w ∧ (out.val[j]!).bv = w + let Post : Option (U64 × Array U64 16#usize) → Prop := fun res => + match res with + | none => readVgicFrame (specPtr frame) σ = ({(none, σ)}, False) + | some (hcr, arr) => + ∃ lrsList : List Word, + lrsList.length = fl4NumLrs ∧ + (∀ j (hj : j < fl4NumLrs), (arr.val[j]!).bv = lrsList[j]!) ∧ + readVgicFrame (specPtr frame) σ = ({(some (hcr.bv, lrsList), σ)}, False) + apply loop.spec_decr_nat (measure := fun p => fl4NumLrs - p.2.val) (inv := Inv) (post := Post) + swap + · exact ⟨Nat.zero_le _, fun _ h => absurd h (Nat.not_lt_zero _)⟩ + rintro ⟨out, i⟩ ⟨hle, hprev⟩ + dsimp only + unfold vcpu.read_vgic_frame_loop.body + by_cases hlt : i < ⟨BitVec.ofNat 64 fl4NumLrs⟩ + · have hilt : i.val < fl4NumLrs := by + have : i.val < (⟨BitVec.ofNat 64 fl4NumLrs⟩ : U64).val := hlt; omega + have hi16 : i < 16#u64 := by change i.val < 16; omega + rw [if_pos hlt, if_pos hi16, lift_ok, bind_ok] + have hr_val : (wa fl4_abi.consts.vcpu_reg.VGIC_LR0 i).val = vrVgicLr0 + i.val := by + rw [wa_valP] + have h48 : fl4_abi.consts.vcpu_reg.VGIC_LR0.val = 48 := by unfold fl4_abi.consts.vcpu_reg.VGIC_LR0; rfl + rw [h48]; unfold vrVgicLr0; exact Nat.mod_eq_of_lt (by omega) + rw [u64_mul_ok (by rw [hr_val]; change (vrVgicLr0 + i.val) * 8 < 2 ^ 64; unfold vrVgicLr0; omega), bind_ok, lift_ok, bind_ok, L.read_eq, bind_ok] + have hmul_eq : (⟨BitVec.ofNat 64 ((wa fl4_abi.consts.vcpu_reg.VGIC_LR0 i).val * 8#u64.val)⟩ : U64) = + ⟨BitVec.ofNat 64 (8 * (vrVgicLr0 + i.val))⟩ := by + congr 1; rw [hr_val]; congr 1; change (vrVgicLr0 + i.val) * 8 = 8 * (vrVgicLr0 + i.val); omega + rw [hmul_eq, vcpu_sanitize_vgic_lr_eqA, bind_ok] + have hval_bv : (L.mem s (wa frame ⟨BitVec.ofNat 64 (8 * (vrVgicLr0 + i.val))⟩)).bv = rawLr i.val := + hmem_word (vrVgicLr0 + i.val) (by unfold vrVgicLr0; omega) + rw [hval_bv] + cases hsan : sanLr i.val with + | none => + have hsan' : sanitizeVgicLr (rawLr i.val) fl4GicV3 = none := hsan + rw [hsan'] + simp only [Option.map_none, core.option.Option.Insts.CoreOpsTry_traitTry.branch, bind_ok, + core.option.Option.Insts.CoreOpsTry_traitFromResidualOptionNever.from_residual, WP.spec_ok] + dsimp only [Post] + rw [readVgicFrame_eqA _ _ (al3_of_frameWinA hF hWin)] + have hall_false : ((List.range fl4NumLrs).map fun k => + sanitizeVgicLr (loadWordAt σ.machineState.underlyingMemory (bufAddr (specPtr frame) (vrVgicLr0 + k))) fl4GicV3).all Option.isSome = false := by + rw [List.all_eq_false] + exact ⟨none, List.mem_map.2 ⟨i.val, List.mem_range.2 hilt, hsan'⟩, by decide⟩ + rw [List.map_map] + change ({(if ((List.range fl4NumLrs).map fun k => + sanitizeVgicLr (loadWordAt σ.machineState.underlyingMemory (bufAddr (specPtr frame) (vrVgicLr0 + k))) fl4GicV3).all Option.isSome + then _ else none, σ)}, False) = ({(none, σ)}, False) + rw [hall_false]; rfl + | some w => + have hsan' : sanitizeVgicLr (rawLr i.val) fl4GicV3 = some w := hsan + rw [hsan'] + simp only [Option.map_some, core.option.Option.Insts.CoreOpsTry_traitTry.branch, bind_ok, lift_ok] + have hcast_val : (UScalar.cast .Usize i).val = i.val := by + rw [UScalar.cast_val_eq] + apply Nat.mod_eq_of_lt + simp only [UScalar.size, UScalarTy.numBits] + rcases System.Platform.numBits_eq with h | h <;> rw [h] <;> omega + have hlen16 : out.val.length = 16 := by + have h16 : (16#usize : Usize).val = 16 := rfl + have := out.property; omega + rw [eq_of_spec_fd (Array.update_spec out (UScalar.cast .Usize i) (⟨w⟩ : U64) + (by rw [hcast_val]; change i.val < out.val.length; omega)), + bind_ok, u64_add_ok (by change i.val + 1 < 2 ^ 64; omega), bind_ok, WP.spec_ok] + have hi1_val : (⟨BitVec.ofNat 64 (i.val + 1#u64.val)⟩ : U64).val = i.val + 1 := by + change (BitVec.ofNat 64 (i.val + 1)).toNat = i.val + 1 + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + refine ⟨⟨by rw [hi1_val]; omega, fun j hj => ?_⟩, by rw [hi1_val]; omega⟩ + rw [hi1_val] at hj + have hset_val : (out.set (UScalar.cast .Usize i) (⟨w⟩ : U64)).val = out.val.set i.val ⟨w⟩ := by + rw [Aeneas.Std.Array.set_val_eq, hcast_val] + by_cases hji : j = i.val + · subst hji + refine ⟨w, hsan, ?_⟩ + have hlt_out : i.val < out.val.length := by omega + rw [hset_val, getElem!_pos _ _ (by rw [List.length_set]; exact hlt_out), + List.getElem_set_self (by rw [List.length_set]; exact hlt_out)] + · obtain ⟨wj, hsj, hgj⟩ := hprev j (by omega) + refine ⟨wj, hsj, ?_⟩ + have hlt_j : j < out.val.length := by omega + rw [hset_val, getElem!_pos _ _ (by rw [List.length_set]; exact hlt_j), + List.getElem_set_ne (Ne.symm hji) (by rw [List.length_set]; exact hlt_j), ← getElem!_pos out.val j hlt_j] + exact hgj + · rw [if_neg hlt] + have hieq : i.val = fl4NumLrs := by + have : ¬ i.val < (⟨BitVec.ofNat 64 fl4NumLrs⟩ : U64).val := hlt; omega + have hvhcr_val : fl4_abi.consts.vcpu_reg.VGIC_HCR.val = vrVgicHcr := by + unfold fl4_abi.consts.vcpu_reg.VGIC_HCR vrVgicHcr; rfl + rw [u64_mul_ok (by rw [hvhcr_val]; unfold vrVgicHcr; decide), bind_ok, lift_ok, bind_ok, + L.read_eq, bind_ok, vcpu_sanitize_vgic_hcr_eqA, bind_ok, WP.spec_ok] + dsimp only [Post] + have hmul_hcr : (⟨BitVec.ofNat 64 (fl4_abi.consts.vcpu_reg.VGIC_HCR.val * 8#u64.val)⟩ : U64) = + ⟨BitVec.ofNat 64 (8 * vrVgicHcr)⟩ := by + congr 1; rw [hvhcr_val]; rfl + rw [hmul_hcr, hmem_word vrVgicHcr (by unfold vrVgicHcr; decide)] + let lrsList := (List.range fl4NumLrs).map fun j => (sanLr j).getD 0 + refine ⟨lrsList, by simp only [lrsList, List.length_map, List.length_range], fun j hj => ?_, ?_⟩ + · obtain ⟨wj, hsj, hgj⟩ := hprev j (by omega) + have hget : lrsList[j]! = (sanLr j).getD 0 := by + rw [List.getElem!_eq_getElem?_getD, List.getElem?_map, List.getElem?_range hj]; rfl + rw [hget, hsj, Option.getD_some]; exact hgj + · rw [readVgicFrame_eqA _ _ (al3_of_frameWinA hF hWin)] + have hall_true : ((List.range fl4NumLrs).map fun k => sanLr k).all Option.isSome = true := by + rw [List.all_eq_true] + intro o ho + obtain ⟨j, hj, rfl⟩ := List.mem_map.1 ho + obtain ⟨wj, hsj, -⟩ := hprev j (by rw [List.mem_range] at hj; omega) + rw [hsj]; rfl + rw [List.map_map] + change ({(if ((List.range fl4NumLrs).map fun k => sanLr k).all Option.isSome + then some (sanitizeVgicHcr hcrWord fl4GicV3, ((List.range fl4NumLrs).map fun k => sanLr k).map fun o => o.getD 0) + else none, σ)}, False) = _ + rw [hall_true, if_pos rfl, List.map_map]; rfl + +end read_vgic + + +end +end Fl4ProofsA64.P3.Vcpu diff --git a/proofs/Fl4ProofsA64/P3/VcpuXferJA.lean b/proofs/Fl4ProofsA64/P3/VcpuXferJA.lean new file mode 100644 index 000000000..d1f473a20 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/VcpuXferJA.lean @@ -0,0 +1,911 @@ +/- +P3 (aarch64, A64-P3 S4 W3): **VCPU state-transfer refinement** (labels 67 `ARMVCPUReadState` / +68 `ARMVCPUWriteState` on `VcpuTopPostA`), re-exporting `VcpuXferBaseA`, `VcpuXferReadJA`, +and `VcpuXferWriteJA`. + +No new named premise. +-/ +import Fl4ProofsA64.P3.VcpuXferReadJA +import Fl4ProofsA64.P3.VcpuXferWriteJA + +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3.Vcpu + +open Fl4Spec Fl4SpecA64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3 Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Endp +open Fl4ProofsA64.P3.Notif + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## Section 4: Labels 67 (`ARMVCPUReadState`) and 68 (`ARMVCPUWriteState`) -/ + +section decode_xfer +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + +/-- Helper: `capability.FrameSize.bits` equals `frameBitsA` and `pageBitsForSize (frameSizeOfA sz)`. -/ +private theorem frameSize_bits_eqA (sz : capability.FrameSize) : + capability.FrameSize.bits sz = ok ⟨BitVec.ofNat 64 (frameBitsA sz)⟩ ∧ + frameBitsA sz = pageBitsForSize (frameSizeOfA sz) ∧ + frameBitsA sz < 64 := by + cases sz <;> + exact ⟨by unfold capability.FrameSize.bits frameBitsA; rfl, + by unfold frameBitsA frameSizeOfA pageBitsForSize; rfl, + by unfold frameBitsA; decide⟩ + +/-- Helper: `VcpuXferStateA` preserved across `TS_RESTART` on `L.curW s`. -/ +private theorem vcpuXferStateA_of_restartA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} + {OS U Ac Pp Tb Ub : Set U64} {Lv : U64 → Nat} {ia ie : Nat} + {s : S} {a : A} {σ : State Unit} {m : Fl4SpecA64.McState} {vc : U64} + (C : VcpuDecCtxA (L := L) (ar := ar) AR Z H OS U Ac Pp Tb Ub Lv ia ie s a σ m vc) + (hCar : CarryA Z (L.mem s) σ) + (hF : FrameMemRelA Z OS Ub (L.ksb s) (L.mem s) σ.machineState.underlyingMemory) + (hR : TcbRegsRelA (L.mem s) σ Z.T) + (hU : UbFootDisjA Z OS Ub) + {a1 : A} (hAR1 : AR a1 Z.hw) + {xct : Tcb} (hkct : σ.kheap σ.curThread = some (.tcb xct)) + (hJ1 : JAtZAX (L := L) AR Z (L.wr s (stA (L.curW s)) sched.TS_RESTART) a1 + (stUpdA σ σ.curThread xct .restart) m) + (hos1 : OSKeep st OS s (L.wr s (stA (L.curW s)) sched.TS_RESTART)) : + VcpuXferStateA (L := L) AR Z OS Ub m a1 (L.wr s (stA (L.curW s)) sched.TS_RESTART) + (stUpdA σ σ.curThread xct .restart) := by + have hcurT : L.curW s ∈ Z.T := C.E.j.1.curT (L.cpu s) C.E.j.1.sch.cpuCs + have hta : TcbAddr (L.curW s) := hU.tcb_al (L.curW s) hcurT + have hst_al : Aligned (stA (L.curW s)) := by + rw [stA_eq]; apply aligned_wa (by unfold Aligned; have := hta.align; omega); decide + have hTr1 : TreeA st OS Z.G.chains Z.G.par (L.wr s (stA (L.curW s)) sched.TS_RESTART) := C.hTr.keepO hos1 + have hcap_os : ∀ y ∈ OS, capAtA (L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART)) y = capAtA (L.mem s) y := by + intro y hy; obtain ⟨h0, h8, -, -⟩ := hos1 y hy; unfold capAtA; rw [h0, h8] + have hTy : TyKeepA σ (stUpdA σ σ.curThread xct .restart) := + TyKeepA.upd hkct (fun _ _ e => by cases e) (fun _ _ => ⟨xct, rfl⟩) + have hCar1 : CarryA Z (L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART)) + (stUpdA σ σ.curThread xct .restart) := by + refine ⟨hCar.1.of_capsA rfl (fun _ h => h) (fun _ h => h) (fun y hy => Or.inl (hcap_os y (C.hsub y hy))), + hCar.2.of_keepA hTy.1 hTy.2 fun y hy => ?_⟩ + rw [hcap_os y ((C.hTr.lk.members y).1 hy).1] + exact CapFinEqA.refl _ + have hV1 : VcpuRelA (L := L) Z OS Ub (L.wr s (stA (L.curW s)) sched.TS_RESTART) + (stUpdA σ σ.curThread xct .restart) := by + set s2 := L.wr s (stA (L.curW s)) sched.TS_RESTART + set σ1 := stUpdA σ σ.curThread xct .restart + have hvc_mem : ∀ v ∈ Z.V, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → + L.mem s2 (wa v o) = L.mem s (wa v o) := fun v hv o ho ho8 => by + have hne : stA (L.curW s) ≠ wa v o := by + rw [stA_eq]; exact (C.hV.place.disj_tcb v hv (L.curW s) hcurT o 3328#u64 ho (by decide)).symm + exact mem_wr_neA hst_al (aligned_wa (C.hV.place.vc_al hv) o ho8) hne + have hwa0 : ∀ v : U64, wa v 0#u64 = v := fun v => by + apply UScalar.eq_of_val_eq; rw [wa_valP]; simp [UScalar.hBounds] + have hvc0 : ∀ v ∈ Z.V, L.mem s2 v = L.mem s v := fun v hv => by + rw [← hwa0 v]; exact hvc_mem v hv 0#u64 (by decide) (by decide) + have htcb_other : ∀ t ∈ Z.T, ∀ o : U64, o.val < 4096 → o.val % 8 = 0 → o ≠ 3328#u64 → + L.mem s2 (wa t o) = L.mem s (wa t o) := fun t ht o ho ho8 hne => by + have hneq : stA (L.curW s) ≠ wa t o := by + rw [stA_eq] + exact field_ne hta (hU.tcb_al t ht) (by decide) ho (fun _ e => hne (UScalar.eq_of_val_eq e.symm)) + exact mem_wr_neA hst_al (aligned_wa (C.hV.place.al_t t ht) o ho8) hneq + have hrun_mem : ∀ t ∈ Z.T, L.mem s2 (runVcpuA t) = L.mem s (runVcpuA t) := fun t ht => by + have hreq : runVcpuA t = wa t 3672#u64 := by unfold runVcpuA; rfl + rw [hreq]; exact htcb_other t ht 3672#u64 (by decide) (by decide) (by decide) + have hkv_eq : ∀ v ∈ Z.V, ∀ x, σ1.kheap (specPtr v) = some (.archObj (.vcpu x)) → + σ.kheap (specPtr v) = some (.archObj (.vcpu x)) := fun v hv x hkx => by + obtain ⟨v0, hkv0, -, -⟩ := C.hV.core.obj v hv + have hne : specPtr v ≠ σ.curThread := fun h => by rw [h, hkct] at hkv0; cases hkv0 + unfold σ1 stUpdA setObjA at hkx + simpa [Function.update, hne] using hkx + refine ⟨hJ1.2.1, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, C.hV.arch, by rw [L.ksb_wr]; exact C.hV.place⟩ + · intro v hv; rw [hvc_mem v hv 8#u64 (by decide) (by decide)]; exact C.hV.owner v hv + · intro v hv x hkx; rw [hvc_mem v hv 16#u64 (by decide) (by decide)]; exact C.hV.vppi v hv x (hkv_eq v hv x hkx) + · intro v hv x hkx; rw [hvc_mem v hv 32#u64 (by decide) (by decide)]; exact C.hV.runOff v hv x (hkv_eq v hv x hkx) + · intro v hv x hkx; rw [hvc_mem v hv 40#u64 (by decide) (by decide)]; exact C.hV.runOut v hv x (hkv_eq v hv x hkx) + · intro v hv x hkx i hi + have hlt : (vcpuRegOffA i).val < 4096 := by rw [vcpuRegOffA_val hi]; omega + have h8 : (vcpuRegOffA i).val % 8 = 0 := by rw [vcpuRegOffA_val hi]; omega + rw [vcpuRegWA, hvc_mem v hv (vcpuRegOffA i) hlt h8] + exact C.hV.regs v hv x (hkv_eq v hv x hkx) i hi + · intro v hv t ht hne + rw [hvc0 v hv] at ht + obtain ⟨htT, htv⟩ := C.hV.bind_v2t v hv t ht hne + exact ⟨htT, by rw [htcb_other t htT 3664#u64 (by decide) (by decide) (by decide)]; exact htv⟩ + · intro t ht v hv hne + rw [htcb_other t ht 3664#u64 (by decide) (by decide) (by decide)] at hv + obtain ⟨hvV, hvt⟩ := C.hV.bind_t2v t ht v hv hne + exact ⟨hvV, by rw [hvc0 v hvV]; exact hvt⟩ + · intro v hv c hc hne + rw [hvc_mem v hv 24#u64 (by decide) (by decide)] at hc + obtain ⟨hcT, hcv⟩ := C.hV.run_v2c v hv c hc hne + exact ⟨hcT, by rw [hrun_mem c hcT]; exact hcv⟩ + · intro c hc v hv hne + rw [hrun_mem c hc] at hv + obtain ⟨hvV, hvc'⟩ := C.hV.run_c2v c hc v hv hne + exact ⟨hvV, by rw [hvc_mem v hvV 24#u64 (by decide) (by decide)]; exact hvc'⟩ + · intro c hc hne + rw [hrun_mem c hc] at hne + have hne_c : c ≠ L.curW s := fun h => by subst h; exact hne C.hCurNoRun + have hne_st : stA (L.curW s) ≠ stA c := by + rw [stA_eq, stA_eq] + exact field_ne hta (hU.tcb_al c hc) (by decide) (by decide) (fun e _ => hne_c e.symm) + rw [mem_wr_neA hst_al (by rw [stA_eq]; exact aligned_wa (C.hV.place.al_t c hc) 3328#u64 (by decide)) hne_st] + exact C.hV.run_state c hc hne + · intro t ht hne + rw [htcb_other t ht 3664#u64 (by decide) (by decide) (by decide)] at hne + rw [hrun_mem t ht]; exact C.hV.bind_run_disj t ht hne + · intro c hc + have hni : L.nodeAt s2 c layout.node.IDLE_THREAD = L.nodeAt s c layout.node.IDLE_THREAD := by + unfold LawfulStore.nodeAt; rw [L.ksb_wr] + have hal_ni : Aligned (L.nodeAt s c layout.node.IDLE_THREAD) := + C.hV.place.node_al c layout.node.IDLE_THREAD (by unfold layout.node.IDLE_THREAD; decide) + have hne_ni : L.nodeAt s c layout.node.IDLE_THREAD ≠ stA (L.curW s) := by + rw [stA_eq] + exact node_ne_field (C.E.sq.nodes c hc) (by simp [layout.node.IDLE_THREAD]) C.E.sq.curT hta (by decide) + have hmem_ni : L.mem s2 (L.nodeAt s2 c layout.node.IDLE_THREAD) = + L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) := by + rw [hni]; exact mem_wr_neA hst_al hal_ni hne_ni.symm + rw [hmem_ni] + have hidT := C.hIdleT c hc + rw [htcb_other _ hidT 3664#u64 (by decide) (by decide) (by decide), hrun_mem _ hidT] + exact C.hV.idle_no_vcpu c hc + · intro v hv hor + rw [hvc0 v hv, hvc_mem v hv 24#u64 (by decide) (by decide)] at hor + obtain ⟨y, hy, hcap⟩ := C.hV.live v hv hor + exact ⟨y, hy, by rw [hcap_os y hy]; exact hcap⟩ + have hF1 : FrameMemRelA Z OS Ub (L.ksb (L.wr s (stA (L.curW s)) sched.TS_RESTART)) + (L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART)) + (stUpdA σ σ.curThread xct .restart).machineState.underlyingMemory := by + refine ⟨hF.aligned, fun b hb => ?_, fun y hy ptr sz r mp s2 asid va hc => ?_, + hF.disj_os, hF.disj_slots, hF.disj_tcb, hF.disj_sc, ?_⟩ + · rw [mem_wr_neA hst_al (hF.aligned b hb) + (fun e => hF.disj_tcb b hb (L.curW s) hcurT 3328#u64 (by decide) (by rw [← stA_eq]; exact e.symm))] + exact hF.agree b hb + · rw [hcap_os y hy] at hc; exact hF.frames y hy ptr sz r mp s2 asid va hc + · rw [L.ksb_wr]; exact hF.disj_ks + have hR1 : TcbRegsRelA (L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART)) + (stUpdA σ σ.curThread xct .restart) Z.T := by + intro t ht xt hkt r + have hlt : (regIdxA r).val < 37 := regIdxA_lt r + set K : U64 := wa 256#u64 (core.num.U64.wrapping_mul (regIdxA r) 8#u64) + have h8v : (8#u64).val = 8 := rfl + have h256v : (256#u64).val = 256 := rfl + have hm : (core.num.U64.wrapping_mul (regIdxA r) 8#u64).val = (regIdxA r).val * 8 := by + rw [wm_valP, h8v]; exact Nat.mod_eq_of_lt (by omega) + have hKval : K.val = 256 + (regIdxA r).val * 8 := by + rw [wa_valP, h256v, hm]; exact Nat.mod_eq_of_lt (by omega) + have hreg_eq : Endp.regA t (regIdxA r) = wa t K := (regA_eq_wa_KA t r).2 + have hta_t := hU.tcb_al t ht + have hal_r : Aligned (Endp.regA t (regIdxA r)) := by + rw [hreg_eq]; apply aligned_wa (by unfold Aligned; have := hta_t.align; omega); rw [hKval]; omega + have hne : Endp.regA t (regIdxA r) ≠ stA (L.curW s) := by + rw [hreg_eq, stA_eq] + exact field_ne hta_t hta (by omega) (by decide) (fun _ e => by + have h3328 : (3328#u64).val = 3328 := rfl + omega) + rw [mem_wr_neA hst_al hal_r hne.symm] + by_cases et : specPtr t = σ.curThread + · rw [et] at hkt + unfold stUpdA setObjA at hkt; simp [Function.update] at hkt; subst hkt + exact hR t ht xct (et ▸ hkct) r + · unfold stUpdA setObjA at hkt; dsimp only at hkt; rw [Function.update_of_ne et] at hkt + exact hR t ht xt hkt r + exact ⟨hJ1, hTr1, hCar1, hV1, hF1, hR1⟩ + +/-- Helper: `fl4PerformVcpuInv (.stateTransfer vc frame mask toFrame vg)` produces `([], σ')` +when the underlying read/write transfer produces `((), σ')`. -/ +private theorem fl4PerformVcpuInv_stateTransfer_oneA {σ σ' : State Unit} + (hcur : σ.archState.armCurrentVcpu = none) (vc frame : ObjRef) (mask : Word) + (toFrame : Bool) (vg : Word × List Word) + (hsp : (if toFrame then fl4ReadStateToFrame vc frame mask else fl4WriteStateFromFrame vc frame mask vg) σ = + ({((), σ')}, False)) : + fl4PerformVcpuInv (.stateTransfer vc frame mask toFrame vg) σ = ({([], σ')}, False) := by + unfold fl4PerformVcpuInv + dsimp only + rw [nd_bind_oneSc (fl4VcpuFlush_noneA hcur vc)] + cases toFrame + · simp only [Bool.false_eq_true, ↓reduceIte] at hsp ⊢ + rw [nd_bind_oneSc hsp]; rfl + · simp only [↓reduceIte] at hsp ⊢ + rw [nd_bind_oneSc hsp]; rfl + +set_option maxHeartbeats 400000 in +/-- **Labels 67 (`ARMVCPUReadState`, `to_frame = true`) and 68 (`ARMVCPUWriteState`, `to_frame = false`)** +on `VcpuTopPostA`. -/ +theorem vcpu_state_transfer_jA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} + {OS U Ac Pp Tb Ub : Set U64} {Lv : U64 → Nat} {ia ie : Nat} + {s : S} {a : A} {σ : State Unit} {m : Fl4SpecA64.McState} {vc : U64} + (C : VcpuDecCtxA (L := L) (ar := ar) AR Z H OS U Ac Pp Tb Ub Lv ia ie s a σ m vc) + (hCar : CarryA Z (L.mem s) σ) + (hP : VcpuPlatAgreeA ar) (hAK : VcpuArchKeepsA ar AR) (hAR : AR a Z.hw) + (hF : FrameMemRelA Z OS Ub (L.ksb s) (L.mem s) σ.machineState.underlyingMemory) + (hR : TcbRegsRelA (L.mem s) σ Z.T) + (hU : UbFootDisjA Z OS Ub) + (hUbUser : ∀ q ∈ Ub, inUserFrame (specPtr q) σ) + (hksTcb : ∀ t ∈ Z.T, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → + ∀ o : U64, o.val < ksSizeA → wa t K ≠ wa (L.ksb s) o) + (hSlotTcb : ∀ t ∈ Z.T, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → + ∀ y, SlotWA Z.th y → wa t K ≠ y ∧ wa t K ≠ wa y 8#u64 ∧ wa t K ≠ wa y 16#u64 ∧ wa t K ≠ wa y 24#u64) + {Rp Nt : Set U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} {slot : U64} + (Pl : SysPlace st H Z.Cs H.frame Rp Nt OS s extra slot) (Y : SymL st H Z.Cs s) + (hrun : Runnable (L.mem s (stA (L.curW s)))) + (hcur : (L.mem s (inqA (L.curW s)) &&& sched.Q_RELEASE) = 0#u64 ∧ L.mem s (scA (L.curW s)) ≠ 0#u64 ∧ + L.mem s (scA (L.curW s)) ∈ Ac) + (hvs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → Aligned b ∧ b ≠ stA (L.curW s)) + {length buf : U64} {args : List U64} {ex : Array obj.Slot 3#usize} + {extras : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)} + (hlen : length.val = args.length) + (hargs : ∀ (i : U64) (w : U64), args[i.val]? = some w → msg.syscall_arg st s (L.curW s) buf i = ok w) + (hEx : ExtraOKA Z.th (L.mem s) ex extras) + (hExSlot : extras ≠ [] → exAtA ex 0 ≠ 0#u64 ∧ exAtA ex 0 ∈ OS) + (hExNotS2 : ∀ p sz r d mp asid va, + capAtA (L.mem s) (exAtA ex 0) ≠ .Frame p sz r d mp true asid va) + (to_frame call : Bool) : + vcpu.decode_state_transfer st ar s a vc length buf ex to_frame ⦃ res => + VcpuTopPostA (L := L) AR Z OS m call σ + ((NonDetE.liftME Fl4ArchInvocation.vcpu <| + decodeVcpuStateTransfer (Z := Unit) (specPtr vc) (args.map UScalar.bv) extras to_frame).run σ) s res ⦄ := by + unfold vcpu.decode_state_transfer + by_cases hlen2 : length < 2#u64 + · rw [if_pos hlen2] + unfold error.truncated; rw [bind_ok, WP.spec_ok] + have hlt : (args.map UScalar.bv).length < 2 := by rw [List.length_map, ← hlen]; exact hlen2 + have hd : (decodeVcpuStateTransfer (Z := Unit) (specPtr vc) (args.map UScalar.bv) extras to_frame).run σ = + ({(.error .truncatedMessage, σ)}, False) := by + unfold decodeVcpuStateTransfer; exact ndE_whenE_true (Or.inl hlt) _ _ _ + exact ⟨rfl, hAR, .truncatedMessage, liftME_vcpu_errA hd, Or.inl trivial⟩ + · rw [if_neg hlen2] + have hge2 : 2 ≤ args.length := by have : ¬ length.val < 2 := hlen2; omega + refine WP.spec_bind (Array.index_usize_spec ex 0#usize (by change 0 < ex.val.length; rw [ex.property]; decide)) ?_ + rintro s0 hs0_eq + have hs0_at : s0 = exAtA ex 0 := by simpa [exAtA] using hs0_eq + have hnull_ok : obj.Slot.is_null s0 = ok (decide (s0 = 0#u64)) := by + unfold obj.Slot.is_null; rfl + rw [hnull_ok, bind_ok] + cases hex_eq : extras with + | nil => + have h0 : s0 = 0#u64 := by rw [hs0_at]; exact hEx.2.2 0 (by simp [hex_eq]) (by decide) + rw [if_pos (by simp [h0])] + unfold error.truncated; rw [bind_ok, WP.spec_ok] + have hd : (decodeVcpuStateTransfer (Z := Unit) (specPtr vc) (args.map UScalar.bv) [] to_frame).run σ = + ({(.error .truncatedMessage, σ)}, False) := by + unfold decodeVcpuStateTransfer; exact ndE_whenE_true (Or.inr rfl) _ _ _ + exact ⟨rfl, hAR, .truncatedMessage, liftME_vcpu_errA hd, Or.inl trivial⟩ + | cons ep rest_ex => + obtain ⟨hnz0, hos0⟩ := hExSlot (by simp [hex_eq]) + have hne0 : s0 ≠ 0#u64 := hs0_at ▸ hnz0 + rw [if_neg (by simp [hne0])] + have hct : sched.cur_thread st s = ok (L.curW s) := eq_of_spec_fd (sched.cur_thread.spec s) + rw [hct, bind_ok] + cases hargs_eq : args with + | nil => simp [hargs_eq] at hge2 + | cons offset rest1 => + cases hrest1 : rest1 with + | nil => simp [hargs_eq, hrest1] at hge2 + | cons mask rest2 => + rw [hargs 0#u64 offset (by simp [hargs_eq]), bind_ok, + hargs 1#u64 mask (by simp [hargs_eq, hrest1]), bind_ok, + sm_all_ok, bind_ok, lift_ok, bind_ok, lift_ok, bind_ok] + have hnot_trunc : ¬ (((offset :: mask :: rest2).map UScalar.bv).length < 2 ∨ ep :: rest_ex = []) := by + simp + by_cases hbadm : (mask &&& ~~~63#u64 != 0#u64) = true + · have hbadm' : mask.bv &&& ~~~smAll ≠ 0 := fun e => by + have h0 : mask &&& ~~~63#u64 = 0#u64 := u64_eq_of_bv_eq (by unfold smAll at e; exact e) + rw [h0] at hbadm; exact absurd hbadm (by decide) + rw [if_pos hbadm] + unfold error.invalid_arg; rw [bind_ok, WP.spec_ok] + have hd : (decodeVcpuStateTransfer (Z := Unit) (specPtr vc) + ((offset :: mask :: rest2).map UScalar.bv) (ep :: rest_ex) to_frame).run σ = + ({(.error (.invalidArgument 1), σ)}, False) := by + unfold decodeVcpuStateTransfer + rw [ndE_whenE_false hnot_trunc] + exact ndE_whenE_true (by simpa [the] using hbadm') _ _ _ + exact ⟨rfl, hAR, .invalidArgument 1, liftME_vcpu_errA hd, Or.inl rfl⟩ + · have hokm : ¬ (mask.bv &&& ~~~smAll ≠ 0) := + fun hne => hbadm (bne_iff_ne.2 (fun e => hne (congrArg UScalar.bv e))) + rw [if_neg hbadm] + have hcap_ok : obj.Slot.cap st s0 s = ok (capAtA (L.mem s) s0) := by + rw [Fl4ProofsA64.P1.W7.R.ST.capA_eq, ← capAtA_eq] + rw [hcap_ok, bind_ok] + obtain ⟨ps, hps, -, -, hcapRel⟩ := hEx.2.1 0 (by simp [hex_eq]) + rw [← hs0_at] at hcapRel + simp only [hex_eq, List.getElem_cons_zero] at hcapRel + -- Helper for non-Frame cap -> InvalidCapability 1 + have hInvCap1 : (frameRightsDev ep.1 = none ∨ stateWindow ep.1 offset.bv = none) → + (do let r ← error.invalid_cap Unit 1#u64; ok (r, s, a)) ⦃ res => + VcpuTopPostA (L := L) AR Z OS m call σ + ((NonDetE.liftME Fl4ArchInvocation.vcpu <| + decodeVcpuStateTransfer (Z := Unit) (specPtr vc) + ((offset :: mask :: rest2).map UScalar.bv) (ep :: rest_ex) to_frame).run σ) s res ⦄ := by + intro hnone + unfold error.invalid_cap; rw [bind_ok, WP.spec_ok] + have hd : (decodeVcpuStateTransfer (Z := Unit) (specPtr vc) + ((offset :: mask :: rest2).map UScalar.bv) (ep :: rest_ex) to_frame).run σ = + ({(.error (.invalidCapability 1), σ)}, False) := by + unfold decodeVcpuStateTransfer + rw [ndE_whenE_false hnot_trunc, ndE_whenE_false (by simpa [the] using hokm)] + simp only [List.map_cons, List.getElem?_cons_zero, the] + rcases hnone with h | h <;> simp [h] <;> rfl + exact ⟨rfl, hAR, .invalidCapability 1, liftME_vcpu_errA hd, Or.inr ⟨1#u64, rfl, rfl⟩⟩ + -- Match on concrete cap + rcases hcapRel with ⟨hwf, habs⟩ + cases hc_eq : capAtA (L.mem s) s0 with + | Frame ptr size rights is_device mp is_s2 asid va => + have hs2f : is_s2 = false := by + cases is_s2 + · rfl + · exfalso; exact hExNotS2 ptr size rights is_device mp asid va (hs0_at ▸ hc_eq) + subst hs2f + rw [hc_eq] at habs hwf + dsimp only + have hep1 : ep.1 = .archObjectCap (.frameCap (specPtr ptr) (vmRightsOfA rights) + (frameSizeOfA size) is_device (mapDataA mp asid va)) := by + rw [← habs]; rfl + have hfrd : frameRightsDev ep.1 = some (vmRightsOfA rights, is_device) := by + rw [hep1]; rfl + set swOpt : Option ObjRef := + if offset.bv &&& 63 ≠ 0 ∨ offset.bv ≥ (1 : Word) <<< pageBitsForSize (frameSizeOfA size) ∨ + ((1 : Word) <<< pageBitsForSize (frameSizeOfA size)) - offset.bv < smSize + then none else some (specPtr ptr + offset.bv) + have hsw_some : stateWindow ep.1 offset.bv = some swOpt := by + rw [hep1]; unfold stateWindow; dsimp only [swOpt]; split_ifs <;> rfl + have hwf_r : 1 ≤ rights.val ∧ rights.val ≤ 3 := ⟨hwf.1, hwf.2.1⟩ + -- Rights & device checks + by_cases hdev : is_device = true + · rw [if_pos hdev] + unfold error.invalid_cap; rw [bind_ok, WP.spec_ok] + have hd : (decodeVcpuStateTransfer (Z := Unit) (specPtr vc) + ((offset :: mask :: rest2).map UScalar.bv) (ep :: rest_ex) to_frame).run σ = + ({(.error (.invalidCapability 1), σ)}, False) := by + unfold decodeVcpuStateTransfer + rw [ndE_whenE_false hnot_trunc, ndE_whenE_false (by simpa [the] using hokm)] + simp only [List.map_cons, List.getElem?_cons_zero, List.getElem?_cons_succ, the, hfrd, hsw_some] + unfold NonDetE.whenE; rw [if_pos (Or.inl hdev)]; exact ndE_throw_bind _ _ _ + exact ⟨rfl, hAR, .invalidCapability 1, liftME_vcpu_errA hd, Or.inr ⟨1#u64, rfl, rfl⟩⟩ + · have hdevf : is_device = false := by cases is_device <;> simp_all + rw [if_neg hdev] + have hko_iff : rights = capability.VM_KERNEL_ONLY ↔ vmRightsOfA rights = vmKernelOnly := by + have h1 : capability.VM_KERNEL_ONLY.val = 1 := by unfold capability.VM_KERNEL_ONLY; rfl + unfold vmRightsOfA + constructor + · intro e; rw [e, h1]; rfl + · intro e + by_cases h3 : rights.val = 3 + · rw [if_pos h3] at e; exfalso + have hm := (Set.ext_iff.1 e .allowRead).1 (by simp [vmReadWrite]) + simp [vmKernelOnly] at hm + · by_cases h2 : rights.val = 2 + · rw [if_neg h3, if_pos h2] at e; exfalso + have hm := (Set.ext_iff.1 e .allowRead).1 (by simp [vmReadOnly]) + simp [vmKernelOnly] at hm + · exact UScalar.eq_of_val_eq (by omega) + have hrw_iff : rights = capability.VM_READ_WRITE ↔ vmRightsOfA rights = vmReadWrite := by + have h3 : capability.VM_READ_WRITE.val = 3 := by unfold capability.VM_READ_WRITE; rfl + unfold vmRightsOfA + constructor + · intro e; rw [e, h3]; rfl + · intro e + by_cases hr3 : rights.val = 3 + · exact UScalar.eq_of_val_eq (by omega) + · by_cases hr2 : rights.val = 2 + · rw [if_neg hr3, if_pos hr2] at e; exfalso + have hm := (Set.ext_iff.1 e .allowWrite).2 (by simp [vmReadWrite]) + simp [vmReadOnly] at hm + · rw [if_neg hr3, if_neg hr2] at e; exfalso + have hm := (Set.ext_iff.1 e .allowRead).2 (by simp [vmReadWrite]) + simp [vmKernelOnly] at hm + by_cases hko : rights = capability.VM_KERNEL_ONLY + · rw [if_pos hko] + unfold error.invalid_cap; rw [bind_ok, WP.spec_ok] + have hd : (decodeVcpuStateTransfer (Z := Unit) (specPtr vc) + ((offset :: mask :: rest2).map UScalar.bv) (ep :: rest_ex) to_frame).run σ = + ({(.error (.invalidCapability 1), σ)}, False) := by + unfold decodeVcpuStateTransfer + rw [ndE_whenE_false hnot_trunc, ndE_whenE_false (by simpa [the] using hokm)] + simp only [List.map_cons, List.getElem?_cons_zero, List.getElem?_cons_succ, the, hfrd, hsw_some] + unfold NonDetE.whenE; rw [if_pos (Or.inr (Or.inl (hko_iff.1 hko)))]; exact ndE_throw_bind _ _ _ + exact ⟨rfl, hAR, .invalidCapability 1, liftME_vcpu_errA hd, Or.inr ⟨1#u64, rfl, rfl⟩⟩ + · rw [if_neg hko] + -- Shared post-rights-check body (identical for `to_frame = true` with RW and `to_frame = false`) + have hBody : ¬ (to_frame = true ∧ vmRightsOfA rights ≠ vmReadWrite) → + (do + let i3 ← capability.FrameSize.bits size + let i4 ← lift (i3 &&& 63#u64) + let size1 ← 1#u64 <<< i4 + let i5 ← lift (offset &&& 63#u64) + if i5 != 0#u64 then + let r ← error.invalid_arg Unit 0#u64; ok (r, s, a) + else if offset >= size1 then + let r ← error.invalid_arg Unit 0#u64; ok (r, s, a) + else + let i6 ← size1 - offset + if i6 < fl4_abi.consts.vcpu_state_mask.SIZE then + let r ← error.invalid_arg Unit 0#u64; ok (r, s, a) + else + let frame ← lift (wa ptr offset) + let lrs ← vcpu.num_lrs ar a + let i7 ← ar.vcpu_gic_version a + let a1 := Array.repeat 16#usize 0#u64 + if to_frame then + let (s2, a2) ← vcpu.stall_vcpu st ar s a vc + let s3 ← sched.set_thread_state st s2 (L.curW s) sched.TS_RESTART + if to_frame then + let s4 ← vcpu.read_state_to_frame st ar s3 a2 vc frame mask + ok (core.result.Result.Ok (), s4, a2) + else + let s4 ← vcpu.write_state_from_frame st ar s3 a2 vc frame mask (0#u64, a1) + ok (core.result.Result.Ok (), s4, a2) + else + let i8 ← fl4_abi.consts.vcpu_state_mask.VGIC + let i9 ← fl4_abi.consts.vcpu_state_mask.VGIC_LR + let i10 ← lift (i8 ||| i9) + let i11 ← lift (mask &&& i10) + if i11 != 0#u64 then + let o ← vcpu.read_vgic_frame st s frame lrs (i7 = 3#u64) + match o with + | none => let r ← error.invalid_arg Unit 1#u64; ok (r, s, a) + | some v => + let (s2, a2) ← vcpu.stall_vcpu st ar s a vc + let s3 ← sched.set_thread_state st s2 (L.curW s) sched.TS_RESTART + if to_frame then + let s4 ← vcpu.read_state_to_frame st ar s3 a2 vc frame mask + ok (core.result.Result.Ok (), s4, a2) + else + let s4 ← vcpu.write_state_from_frame st ar s3 a2 vc frame mask v + ok (core.result.Result.Ok (), s4, a2) + else + let (s2, a2) ← vcpu.stall_vcpu st ar s a vc + let s3 ← sched.set_thread_state st s2 (L.curW s) sched.TS_RESTART + if to_frame then + let s4 ← vcpu.read_state_to_frame st ar s3 a2 vc frame mask + ok (core.result.Result.Ok (), s4, a2) + else + let s4 ← vcpu.write_state_from_frame st ar s3 a2 vc frame mask (0#u64, a1) + ok (core.result.Result.Ok (), s4, a2)) ⦃ res => + VcpuTopPostA (L := L) AR Z OS m call σ + ((NonDetE.liftME Fl4ArchInvocation.vcpu <| + decodeVcpuStateTransfer (Z := Unit) (specPtr vc) + ((offset :: mask :: rest2).map UScalar.bv) (ep :: rest_ex) to_frame).run σ) s res ⦄ := by + intro hrights_ok + have hnot_bad_cap : ¬ (is_device = true ∨ vmRightsOfA rights = vmKernelOnly ∨ + (to_frame = true ∧ vmRightsOfA rights ≠ vmReadWrite)) := by + rintro (h | h | h) + · exact hdev h + · exact hko (hko_iff.2 h) + · exact hrights_ok h + obtain ⟨hbits_ok, hbits_eq, hbits_lt64⟩ := frameSize_bits_eqA size + have hbits_and : (⟨BitVec.ofNat 64 (frameBitsA size)⟩ &&& 63#u64 : U64) = + ⟨BitVec.ofNat 64 (frameBitsA size)⟩ := by + cases size <;> exact u64_eq_of_bv_eq (by decide) + have hbits_val : (⟨BitVec.ofNat 64 (frameBitsA size)⟩ : U64).val = frameBitsA size := by + cases size <;> rfl + rw [hbits_ok, bind_ok, lift_ok, bind_ok, hbits_and, + u64_shl_u64_ok (by rw [hbits_val]; exact hbits_lt64), hbits_val, bind_ok, lift_ok, bind_ok] + set size1 : U64 := ⟨(1#u64 : U64).bv <<< frameBitsA size⟩ + have hsize1_bv : size1.bv = (1 : Word) <<< pageBitsForSize (frameSizeOfA size) := by + rw [← hbits_eq]; rfl + have hsize1_val : size1.val = 2 ^ frameBitsA size := by + cases size <;> rfl + have hsw_bad : ∀ (hcond : offset.bv &&& 63 ≠ 0 ∨ offset.bv ≥ size1.bv ∨ size1.bv - offset.bv < smSize), + (do let r ← error.invalid_arg Unit 0#u64; ok (r, s, a)) ⦃ res => + VcpuTopPostA (L := L) AR Z OS m call σ + ((NonDetE.liftME Fl4ArchInvocation.vcpu <| + decodeVcpuStateTransfer (Z := Unit) (specPtr vc) + ((offset :: mask :: rest2).map UScalar.bv) (ep :: rest_ex) to_frame).run σ) s res ⦄ := by + intro hcond + unfold error.invalid_arg; rw [bind_ok, WP.spec_ok] + have hd : (decodeVcpuStateTransfer (Z := Unit) (specPtr vc) + ((offset :: mask :: rest2).map UScalar.bv) (ep :: rest_ex) to_frame).run σ = + ({(.error (.invalidArgument 0), σ)}, False) := by + unfold decodeVcpuStateTransfer + rw [ndE_whenE_false hnot_trunc, ndE_whenE_false (by simpa [the] using hokm)] + simp only [List.map_cons, List.getElem?_cons_zero, List.getElem?_cons_succ, the, hfrd, hsw_some] + unfold NonDetE.whenE; rw [if_neg hnot_bad_cap, ndE_pure_bindA] + dsimp only [swOpt]; rw [← hsize1_bv, if_pos hcond]; rfl + exact ⟨rfl, hAR, .invalidArgument 0, liftME_vcpu_errA hd, Or.inl rfl⟩ + by_cases hal64 : (offset &&& 63#u64 != 0#u64) = true + · rw [if_pos hal64] + exact hsw_bad (Or.inl (fun e => by + have : offset &&& 63#u64 = 0#u64 := u64_eq_of_bv_eq e; simp [this] at hal64)) + · rw [if_neg hal64] + have hal64' : offset.bv &&& 63 = 0 := by + by_contra hne; exact hal64 (bne_iff_ne.2 (fun e => hne (congrArg UScalar.bv e))) + by_cases hge_sz : offset ≥ size1 + · rw [if_pos hge_sz]; exact hsw_bad (Or.inr (Or.inl hge_sz)) + · rw [if_neg hge_sz] + have hlt_sz : offset.val < size1.val := by + have : ¬ size1.val ≤ offset.val := hge_sz; omega + rw [u64_sub_ok (by omega), bind_ok] + have hsm_sz : fl4_abi.consts.vcpu_state_mask.SIZE.val = 1024 := by + unfold fl4_abi.consts.vcpu_state_mask.SIZE; rfl + have hsub_val : (⟨BitVec.ofNat 64 (size1.val - offset.val)⟩ : U64).val = size1.val - offset.val := by + change (BitVec.ofNat 64 _).toNat = _ + have hsz_lt : size1.val < 2 ^ 64 := by rw [hsize1_val]; cases size <;> decide + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + have hsub_bv : (⟨BitVec.ofNat 64 (size1.val - offset.val)⟩ : U64).bv = size1.bv - offset.bv := by + apply BitVec.eq_of_toNat_eq + change (⟨BitVec.ofNat 64 (size1.val - offset.val)⟩ : U64).val = (size1.bv - offset.bv).toNat + rw [hsub_val, BitVec.toNat_sub_of_le (by exact le_of_lt hlt_sz)]; rfl + have hsmSize_nat : smSize.toNat = 1024 := rfl + by_cases hlt1024 : (⟨BitVec.ofNat 64 (size1.val - offset.val)⟩ : U64) < fl4_abi.consts.vcpu_state_mask.SIZE + · rw [if_pos hlt1024] + exact hsw_bad (Or.inr (Or.inr (by + rw [← hsub_bv]; change (⟨BitVec.ofNat 64 (size1.val - offset.val)⟩ : U64).val < smSize.toNat + have hlt' : (⟨BitVec.ofNat 64 (size1.val - offset.val)⟩ : U64).val < fl4_abi.consts.vcpu_state_mask.SIZE.val := hlt1024 + omega))) + · rw [if_neg hlt1024, lift_ok, bind_ok, vcpu_num_lrs_eqA hP a, bind_ok] + obtain ⟨g, hga, hgb⟩ := hP.gic a + rw [hga, bind_ok] + have hdec : (decide (g = 3#u64)) = fl4GicV3 := hgb + have hge1024 : 1024 ≤ size1.val - offset.val := by + have : ¬ (⟨BitVec.ofNat 64 (size1.val - offset.val)⟩ : U64).val < fl4_abi.consts.vcpu_state_mask.SIZE.val := hlt1024 + omega + have hsw_ok : ¬ (offset.bv &&& 63 ≠ 0 ∨ offset.bv ≥ size1.bv ∨ size1.bv - offset.bv < smSize) := by + rintro (h | h | h) + · exact h hal64' + · exact hge_sz h + · rw [← hsub_bv] at h + have hlt' : (⟨BitVec.ofNat 64 (size1.val - offset.val)⟩ : U64).val < smSize.toNat := h + omega + -- Derive `FrameWinA Ub (wa ptr offset)` from `hF.frames`! + obtain ⟨hptr_al, hptr_fit, hptr_ub⟩ := hF.frames s0 (hs0_at ▸ hos0) ptr size rights mp false asid va (by rwa [hdevf] at hc_eq) + have hoff_mod8 : offset.val % 8 = 0 := by + have h64 : offset.val % 64 = 0 := by + have := congrArg BitVec.toNat hal64' + simpa [BitVec.toNat_and, Nat.and_two_pow_sub_one_eq_mod _ 6] using this + omega + set frame : U64 := wa ptr offset + have hframe_val : frame.val = ptr.val + offset.val := by + rw [wa_valP, Nat.mod_eq_of_lt (by omega)] + have hWin : FrameWinA Ub frame := by + refine ⟨by omega, fun k hk => ?_⟩ + have hwa_eq : wa frame ⟨BitVec.ofNat 64 (8 * k)⟩ = wa ptr ⟨BitVec.ofNat 64 (offset.val + 8 * k)⟩ := by + apply UScalar.eq_of_val_eq + rw [wa_valP, hframe_val, wa_valP] + have h1 : (⟨BitVec.ofNat 64 (8 * k)⟩ : U64).val = 8 * k := by + change (BitVec.ofNat 64 _).toNat = _; rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + have h2 : (⟨BitVec.ofNat 64 (offset.val + 8 * k)⟩ : U64).val = offset.val + 8 * k := by + change (BitVec.ofNat 64 _).toNat = _; rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + rw [h1, h2]; congr 1; omega + rw [hwa_eq] + have hok_val : (⟨BitVec.ofNat 64 (offset.val + 8 * k)⟩ : U64).val = offset.val + 8 * k := by + change (BitVec.ofNat 64 _).toNat = _; rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + exact hptr_ub _ (by omega) (by omega) + have hsp_frame : specPtr frame = specPtr ptr + offset.bv := by + have hwa_bv : frame.bv = ptr.bv + offset.bv := by + apply BitVec.eq_of_toNat_eq + change frame.val = (ptr.bv + offset.bv).toNat + rw [hframe_val, BitVec.toNat_add]; exact (Nat.mod_eq_of_lt (by omega)).symm + unfold specPtr; rw [hwa_bv, BitVec.add_assoc, BitVec.add_comm offset.bv pptrShiftA, ← BitVec.add_assoc] + have hcurT : L.curW s ∈ Z.T := C.E.j.1.curT (L.cpu s) C.E.j.1.sch.cpuCs + have hvc_st : ∀ v ∈ Z.V, stA (L.curW s) ≠ v ∧ stA (L.curW s) ≠ wa v 24#u64 := fun v hv => by + have hwa0 : wa v 0#u64 = v := by + apply UScalar.eq_of_val_eq; rw [wa_valP]; simp [UScalar.hBounds] + rw [stA_eq] + exact ⟨by rw [← hwa0]; exact (C.hV.place.disj_tcb v hv _ hcurT 0#u64 3328#u64 (by decide) (by decide)).symm, + (C.hV.place.disj_tcb v hv _ hcurT 24#u64 3328#u64 (by decide) (by decide)).symm⟩ + have hCs128 : ∀ c ∈ Z.Cs, c.val < 128 := by + obtain ⟨n, -, hn128, hcn⟩ := C.E.j.1.csN + intro c hc; have := (hcn c).1 hc; omega + have hcpu128 : (L.cpu (L.wr s (stA (L.curW s)) sched.TS_RESTART)).val < 128 := by + rw [L.cpu_wr]; exact hCs128 _ C.E.j.1.sch.cpuCs + cases to_frame + · -- `to_frame = false` (ARMVCPUWriteState, label 68) + simp only [Bool.false_eq_true, ↓reduceIte] + rw [sm_vgic_ok, bind_ok, sm_vgic_lr_ok, bind_ok, lift_ok, bind_ok, lift_ok, bind_ok] + by_cases hvg_m : (mask &&& (8#u64 ||| 32#u64) != 0#u64) = true + · have hvg_mb : mask.bv &&& (smVgic ||| smVgicLr) ≠ 0 := fun e => by + have : mask &&& (8#u64 ||| 32#u64) = 0#u64 := u64_eq_of_bv_eq e; simp [this] at hvg_m + rw [if_pos hvg_m, hdec] + refine WP.spec_bind (read_vgic_frame_refinesA hF hWin) ?_ + rintro res hres + cases res with + | none => + unfold error.invalid_arg; rw [bind_ok, WP.spec_ok] + have hd : (decodeVcpuStateTransfer (Z := Unit) (specPtr vc) + ((offset :: mask :: rest2).map UScalar.bv) (ep :: rest_ex) false).run σ = + ({(.error (.invalidArgument 1), σ)}, False) := by + unfold decodeVcpuStateTransfer + rw [ndE_whenE_false hnot_trunc, ndE_whenE_false (by simpa [the] using hokm)] + simp only [List.map_cons, List.getElem?_cons_zero, List.getElem?_cons_succ, the, hfrd, hsw_some] + unfold NonDetE.whenE; rw [if_neg hnot_bad_cap, ndE_pure_bindA] + dsimp only [swOpt]; rw [← hsize1_bv, if_neg hsw_ok, ← hsp_frame]; dsimp only + rw [if_pos ⟨by decide, hvg_mb⟩, ndE_liftE_bind _ _ _ _ none hres]; rfl + exact ⟨rfl, hAR, .invalidArgument 1, liftME_vcpu_errA hd, Or.inl rfl⟩ + | some vg => + rcases vg with ⟨vgHcr, vgArr⟩ + obtain ⟨lrsList, hlrs_len, hlrs_arr, hread_spec⟩ := hres + refine WP.spec_bind (stall_vcpu_localA hAK hAR vc C.hStallT C.hStallC) ?_ + rintro ⟨s2, a2⟩ ⟨hs2, hAR2⟩ + dsimp only at hs2; subst s2 + change (Std.bind (sched.set_thread_state st s (L.curW s) sched.TS_RESTART) _) ⦃ _ ⦄ + obtain ⟨xct, hkct, hst_ok, hst_spec, E1, -, hos1, -, -⟩ := + C.E.restartA Pl Y hrun hcur hvs hvc_st + rw [hst_ok, bind_ok] + have hJ1_a2 : JAtZAX (L := L) AR Z (L.wr s (stA (L.curW s)) sched.TS_RESTART) a2 + (stUpdA σ σ.curThread xct .restart) m := + ⟨{ E1.j.1 with ar := hAR2 }, E1.j.2⟩ + have X1 := vcpuXferStateA_of_restartA C hCar hF hR hU hAR2 hkct hJ1_a2 hos1 + have hfa1 : ∀ q, FootA (L := L) Z (L.wr s (stA (L.curW s)) sched.TS_RESTART) q → Aligned q := + fun q hq => C.hfa q (by simpa [FootA_wr_eqA] using hq) + have hIdle1 : ∀ c ∈ Z.Cs, + L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART) + (L.nodeAt (L.wr s (stA (L.curW s)) sched.TS_RESTART) c layout.node.IDLE_THREAD) ∈ Z.T := by + intro c hc + have hta : TcbAddr (L.curW s) := hU.tcb_al (L.curW s) hcurT + have hst_al : Aligned (stA (L.curW s)) := by + rw [stA_eq]; apply aligned_wa (by unfold Aligned; have := hta.align; omega); decide + have hal_idle : Aligned (L.nodeAt s c layout.node.IDLE_THREAD) := + C.hfa _ (FootA.nslot _ (FootNA.node c _ hc (Or.inr (Or.inl rfl)))) + have hne_idle : L.nodeAt s c layout.node.IDLE_THREAD ≠ stA (L.curW s) := by + rw [stA_eq] + exact (node_ne_field (C.E.sq.nodes c hc) (by simp [layout.node.IDLE_THREAD]) + C.E.sq.curT hta (by decide)) + rw [nodeAt_wrA, mem_wr_neA hst_al hal_idle hne_idle.symm]; exact C.hIdleT c hc + have hvg_u32 : vgHcr.bv >>> 32 = 0 := by + rw [readVgicFrame_eqA _ _ (al3_of_frameWinA hF hWin)] at hread_spec + split_ifs at hread_spec with h_ok + · have heq := (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (Prod.mk.inj hread_spec).1)).1 + obtain ⟨hvg_eq, -⟩ := Prod.mk.inj (Option.some.inj heq) + rw [← hvg_eq] + have hlt : (sanitizeVgicHcr (loadWordAt σ.machineState.underlyingMemory (bufAddr (specPtr frame) vrVgicHcr)) fl4GicV3).toNat < 2 ^ 32 := by + unfold sanitizeVgicHcr + cases fl4GicV3 + · simp only [Bool.false_eq_true, ↓reduceIte, BitVec.toNat_and] + exact lt_of_le_of_lt Nat.and_le_right (by decide) + · simp only [↓reduceIte, BitVec.toNat_and] + exact lt_of_le_of_lt Nat.and_le_right (by decide) + apply BitVec.eq_of_toNat_eq + rw [BitVec.toNat_ushiftRight, Nat.shiftRight_eq_div_pow] + exact Nat.div_eq_of_lt hlt + · have heq := (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (Prod.mk.inj hread_spec).1)).1 + cases heq + refine WP.spec_bind (write_state_from_frame_refinesA X1 hP hU + hCs128 hcpu128 + C.hExt C.hSw hfa1 C.hsub hIdle1 + (by rw [L.ksb_wr]; exact hksTcb) hSlotTcb C.hvc hWin + (vgicSpec := (vgHcr.bv, lrsList)) + (fun _ => ⟨rfl, hvg_u32, hlrs_len, hlrs_arr⟩)) ?_ + rintro s4 ⟨σ4, hsp4, X4⟩ + rw [WP.spec_ok] + have hd : (decodeVcpuStateTransfer (Z := Unit) (specPtr vc) + ((offset :: mask :: rest2).map UScalar.bv) (ep :: rest_ex) false).run σ = + ({(.ok (.stateTransfer (specPtr vc) (specPtr frame) mask.bv false (vgHcr.bv, lrsList)), σ)}, False) := by + unfold decodeVcpuStateTransfer + rw [ndE_whenE_false hnot_trunc, ndE_whenE_false (by simpa [the] using hokm)] + simp only [List.map_cons, List.getElem?_cons_zero, List.getElem?_cons_succ, the, hfrd, hsw_some] + unfold NonDetE.whenE; rw [if_neg hnot_bad_cap, ndE_pure_bindA] + dsimp only [swOpt]; rw [← hsize1_bv, if_neg hsw_ok, ← hsp_frame]; dsimp only + rw [if_pos ⟨by decide, hvg_mb⟩, ndE_liftE_bind _ _ _ _ (some (vgHcr.bv, lrsList)) hread_spec]; rfl + refine ⟨.vcpu (.stateTransfer (specPtr vc) (specPtr frame) mask.bv false (vgHcr.bv, lrsList)), + liftME_vcpu_okA hd, stUpdA σ σ.curThread xct .restart, hst_spec, σ4, [], ?_, + Or.inl ⟨Z, X4.j, X4.tr, fun _ => X4.car⟩⟩ + unfold fl4ArchPerformInvocation NonDetE.liftE + change (Except.ok [], σ4) ∈ ((fl4PerformVcpuInv _ >>= _) _).1 + rw [nd_bind_oneSc (fl4PerformVcpuInv_stateTransfer_oneA X1.vcpu.arch _ _ _ false _ hsp4)] + exact Set.mem_singleton _ + · have hvg_mb : ¬ (mask.bv &&& (smVgic ||| smVgicLr) ≠ 0) := + fun hne => hvg_m (bne_iff_ne.2 (fun e => hne (congrArg UScalar.bv e))) + rw [if_neg hvg_m] + refine WP.spec_bind (stall_vcpu_localA hAK hAR vc C.hStallT C.hStallC) ?_ + rintro ⟨s2, a2⟩ ⟨hs2, hAR2⟩ + dsimp only at hs2; subst s2 + change (Std.bind (sched.set_thread_state st s (L.curW s) sched.TS_RESTART) _) ⦃ _ ⦄ + obtain ⟨xct, hkct, hst_ok, hst_spec, E1, -, hos1, -, -⟩ := + C.E.restartA Pl Y hrun hcur hvs hvc_st + rw [hst_ok, bind_ok] + have hJ1_a2 : JAtZAX (L := L) AR Z (L.wr s (stA (L.curW s)) sched.TS_RESTART) a2 + (stUpdA σ σ.curThread xct .restart) m := + ⟨{ E1.j.1 with ar := hAR2 }, E1.j.2⟩ + have X1 := vcpuXferStateA_of_restartA C hCar hF hR hU hAR2 hkct hJ1_a2 hos1 + have hfa1 : ∀ q, FootA (L := L) Z (L.wr s (stA (L.curW s)) sched.TS_RESTART) q → Aligned q := + fun q hq => C.hfa q (by simpa [FootA_wr_eqA] using hq) + have hIdle1 : ∀ c ∈ Z.Cs, + L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART) + (L.nodeAt (L.wr s (stA (L.curW s)) sched.TS_RESTART) c layout.node.IDLE_THREAD) ∈ Z.T := by + intro c hc + have hta : TcbAddr (L.curW s) := hU.tcb_al (L.curW s) hcurT + have hst_al : Aligned (stA (L.curW s)) := by + rw [stA_eq]; apply aligned_wa (by unfold Aligned; have := hta.align; omega); decide + have hal_idle : Aligned (L.nodeAt s c layout.node.IDLE_THREAD) := + C.hfa _ (FootA.nslot _ (FootNA.node c _ hc (Or.inr (Or.inl rfl)))) + have hne_idle : L.nodeAt s c layout.node.IDLE_THREAD ≠ stA (L.curW s) := by + rw [stA_eq] + exact (node_ne_field (C.E.sq.nodes c hc) (by simp [layout.node.IDLE_THREAD]) + C.E.sq.curT hta (by decide)) + rw [nodeAt_wrA, mem_wr_neA hst_al hal_idle hne_idle.symm]; exact C.hIdleT c hc + refine WP.spec_bind (write_state_from_frame_refinesA X1 hP hU + hCs128 hcpu128 + C.hExt C.hSw hfa1 C.hsub hIdle1 + (by rw [L.ksb_wr]; exact hksTcb) hSlotTcb C.hvc hWin + (vgicSpec := (0, [])) (fun h => absurd h hvg_mb)) ?_ + rintro s4 ⟨σ4, hsp4, X4⟩ + rw [WP.spec_ok] + have hd : (decodeVcpuStateTransfer (Z := Unit) (specPtr vc) + ((offset :: mask :: rest2).map UScalar.bv) (ep :: rest_ex) false).run σ = + ({(.ok (.stateTransfer (specPtr vc) (specPtr frame) mask.bv false (0, [])), σ)}, False) := by + unfold decodeVcpuStateTransfer + rw [ndE_whenE_false hnot_trunc, ndE_whenE_false (by simpa [the] using hokm)] + simp only [List.map_cons, List.getElem?_cons_zero, List.getElem?_cons_succ, the, hfrd, hsw_some] + unfold NonDetE.whenE; rw [if_neg hnot_bad_cap, ndE_pure_bindA] + dsimp only [swOpt]; rw [← hsize1_bv, if_neg hsw_ok, ← hsp_frame]; dsimp only + rw [if_neg (fun h => hvg_mb h.2)]; rfl + refine ⟨.vcpu (.stateTransfer (specPtr vc) (specPtr frame) mask.bv false (0, [])), + liftME_vcpu_okA hd, stUpdA σ σ.curThread xct .restart, hst_spec, σ4, [], ?_, + Or.inl ⟨Z, X4.j, X4.tr, fun _ => X4.car⟩⟩ + unfold fl4ArchPerformInvocation NonDetE.liftE + change (Except.ok [], σ4) ∈ ((fl4PerformVcpuInv _ >>= _) _).1 + rw [nd_bind_oneSc (fl4PerformVcpuInv_stateTransfer_oneA X1.vcpu.arch _ _ _ false _ hsp4)] + exact Set.mem_singleton _ + · -- `to_frame = true` (ARMVCPUReadState, label 67) + simp only [↓reduceIte] + refine WP.spec_bind (stall_vcpu_localA hAK hAR vc C.hStallT C.hStallC) ?_ + rintro ⟨s2, a2⟩ ⟨hs2, hAR2⟩ + dsimp only at hs2; subst s2 + change (Std.bind (sched.set_thread_state st s (L.curW s) sched.TS_RESTART) _) ⦃ _ ⦄ + obtain ⟨xct, hkct, hst_ok, hst_spec, E1, -, hos1, -, -⟩ := + C.E.restartA Pl Y hrun hcur hvs hvc_st + rw [hst_ok, bind_ok] + have hJ1_a2 : JAtZAX (L := L) AR Z (L.wr s (stA (L.curW s)) sched.TS_RESTART) a2 + (stUpdA σ σ.curThread xct .restart) m := + ⟨{ E1.j.1 with ar := hAR2 }, E1.j.2⟩ + have X1 := vcpuXferStateA_of_restartA C hCar hF hR hU hAR2 hkct hJ1_a2 hos1 + have hfa1 : ∀ q, FootA (L := L) Z (L.wr s (stA (L.curW s)) sched.TS_RESTART) q → Aligned q := + fun q hq => C.hfa q (by simpa [FootA_wr_eqA] using hq) + have hIdle1 : ∀ c ∈ Z.Cs, + L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART) + (L.nodeAt (L.wr s (stA (L.curW s)) sched.TS_RESTART) c layout.node.IDLE_THREAD) ∈ Z.T := by + intro c hc + have hta : TcbAddr (L.curW s) := hU.tcb_al (L.curW s) hcurT + have hst_al : Aligned (stA (L.curW s)) := by + rw [stA_eq]; apply aligned_wa (by unfold Aligned; have := hta.align; omega); decide + have hal_idle : Aligned (L.nodeAt s c layout.node.IDLE_THREAD) := + C.hfa _ (FootA.nslot _ (FootNA.node c _ hc (Or.inr (Or.inl rfl)))) + have hne_idle : L.nodeAt s c layout.node.IDLE_THREAD ≠ stA (L.curW s) := by + rw [stA_eq] + exact (node_ne_field (C.E.sq.nodes c hc) (by simp [layout.node.IDLE_THREAD]) + C.E.sq.curT hta (by decide)) + rw [nodeAt_wrA, mem_wr_neA hst_al hal_idle hne_idle.symm]; exact C.hIdleT c hc + have huf1 : ∀ k < 128, inUserFrame (bufAddr (specPtr frame) k) (stUpdA σ σ.curThread xct .restart) := by + intro k hk + have hq := hUbUser _ (hWin.2 k hk) + rw [specPtr_wa_wordA (by have := hWin.1; omega)] at hq + rcases hq with ⟨sz, hkb⟩ + refine ⟨sz, ?_⟩ + unfold stUpdA setObjA; dsimp only + rw [Function.update_of_ne (fun e => by rw [e, hkct] at hkb; cases hkb)] + exact hkb + refine WP.spec_bind (read_state_to_frame_refinesA X1 hP hU + hCs128 hcpu128 + hfa1 C.hsub hIdle1 C.hvc hWin huf1) ?_ + rintro s4 ⟨σ4, hsp4, X4, -, -, -, -, -⟩ + rw [WP.spec_ok] + have hd : (decodeVcpuStateTransfer (Z := Unit) (specPtr vc) + ((offset :: mask :: rest2).map UScalar.bv) (ep :: rest_ex) true).run σ = + ({(.ok (.stateTransfer (specPtr vc) (specPtr frame) mask.bv true (0, [])), σ)}, False) := by + unfold decodeVcpuStateTransfer + rw [ndE_whenE_false hnot_trunc, ndE_whenE_false (by simpa [the] using hokm)] + simp only [List.map_cons, List.getElem?_cons_zero, List.getElem?_cons_succ, the, hfrd, hsw_some] + unfold NonDetE.whenE; rw [if_neg hnot_bad_cap, ndE_pure_bindA] + dsimp only [swOpt]; rw [← hsize1_bv, if_neg hsw_ok, ← hsp_frame]; dsimp only + rw [if_neg (fun h => h.1 trivial)]; rfl + refine ⟨.vcpu (.stateTransfer (specPtr vc) (specPtr frame) mask.bv true (0, [])), + liftME_vcpu_okA hd, stUpdA σ σ.curThread xct .restart, hst_spec, σ4, [], ?_, + Or.inl ⟨Z, X4.j, X4.tr, fun _ => X4.car⟩⟩ + unfold fl4ArchPerformInvocation NonDetE.liftE + change (Except.ok [], σ4) ∈ ((fl4PerformVcpuInv _ >>= _) _).1 + rw [nd_bind_oneSc (fl4PerformVcpuInv_stateTransfer_oneA X1.vcpu.arch _ _ _ true _ hsp4)] + exact Set.mem_singleton _ + by_cases htf : to_frame = true + · rw [if_pos htf] + by_cases hnrw : (rights != capability.VM_READ_WRITE) = true + · rw [if_pos hnrw] + unfold error.invalid_cap; rw [bind_ok, WP.spec_ok] + have hnrw' : vmRightsOfA rights ≠ vmReadWrite := + fun e => by have := hrw_iff.2 e; simp [this] at hnrw + have hd : (decodeVcpuStateTransfer (Z := Unit) (specPtr vc) + ((offset :: mask :: rest2).map UScalar.bv) (ep :: rest_ex) to_frame).run σ = + ({(.error (.invalidCapability 1), σ)}, False) := by + unfold decodeVcpuStateTransfer + rw [ndE_whenE_false hnot_trunc, ndE_whenE_false (by simpa [the] using hokm)] + simp only [List.map_cons, List.getElem?_cons_zero, List.getElem?_cons_succ, the, hfrd, hsw_some] + unfold NonDetE.whenE; rw [if_pos (Or.inr (Or.inr ⟨htf, hnrw'⟩))]; exact ndE_throw_bind _ _ _ + exact ⟨rfl, hAR, .invalidCapability 1, liftME_vcpu_errA hd, Or.inr ⟨1#u64, rfl, rfl⟩⟩ + · rw [if_neg hnrw] + have hrw' : vmRightsOfA rights = vmReadWrite := by + apply hrw_iff.1; by_contra hne; exact hnrw (bne_iff_ne.2 hne) + exact hBody (fun h => h.2 hrw') + · rw [if_neg htf] + exact hBody (fun h => htf h.1) + | PageTable p m asid va => + rw [hc_eq] at habs + cases m + · exact hInvCap1 (Or.inl (by rw [← habs]; rfl)) + · cases hep1 : ep.1 with + | archObjectCap ac => + cases ac with + | pageTableCap _ _ _ => exact hInvCap1 (Or.inl (by rw [hep1]; rfl)) + | _ => rw [hep1] at habs; cases habs + | _ => rw [hep1] at habs; cases habs + | _ => + rw [hc_eq] at habs hwf + dsimp only + first + | exact False.elim hwf + | exact hInvCap1 (Or.inl (by rw [← habs]; rfl)) + +/-- **Label 67 (`ARMVCPUReadState`)** on `VcpuTopPostA`. -/ +theorem vcpu_read_state_jA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} + {OS U Ac Pp Tb Ub : Set U64} {Lv : U64 → Nat} {ia ie : Nat} + {s : S} {a : A} {σ : State Unit} {m : Fl4SpecA64.McState} {vc : U64} + (C : VcpuDecCtxA (L := L) (ar := ar) AR Z H OS U Ac Pp Tb Ub Lv ia ie s a σ m vc) + (hCar : CarryA Z (L.mem s) σ) + (hP : VcpuPlatAgreeA ar) (hAK : VcpuArchKeepsA ar AR) (hAR : AR a Z.hw) + (hF : FrameMemRelA Z OS Ub (L.ksb s) (L.mem s) σ.machineState.underlyingMemory) + (hR : TcbRegsRelA (L.mem s) σ Z.T) + (hU : UbFootDisjA Z OS Ub) + (hUbUser : ∀ q ∈ Ub, inUserFrame (specPtr q) σ) + (hksTcb : ∀ t ∈ Z.T, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → + ∀ o : U64, o.val < ksSizeA → wa t K ≠ wa (L.ksb s) o) + (hSlotTcb : ∀ t ∈ Z.T, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → + ∀ y, SlotWA Z.th y → wa t K ≠ y ∧ wa t K ≠ wa y 8#u64 ∧ wa t K ≠ wa y 16#u64 ∧ wa t K ≠ wa y 24#u64) + {Rp Nt : Set U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} {slot : U64} + (Pl : SysPlace st H Z.Cs H.frame Rp Nt OS s extra slot) (Y : SymL st H Z.Cs s) + (hrun : Runnable (L.mem s (stA (L.curW s)))) + (hcur : (L.mem s (inqA (L.curW s)) &&& sched.Q_RELEASE) = 0#u64 ∧ L.mem s (scA (L.curW s)) ≠ 0#u64 ∧ + L.mem s (scA (L.curW s)) ∈ Ac) + (hvs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → Aligned b ∧ b ≠ stA (L.curW s)) + {length buf : U64} {args : List U64} {ex : Array obj.Slot 3#usize} + {extras : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)} + (hlen : length.val = args.length) + (hargs : ∀ (i : U64) (w : U64), args[i.val]? = some w → msg.syscall_arg st s (L.curW s) buf i = ok w) + (hEx : ExtraOKA Z.th (L.mem s) ex extras) + (hExSlot : extras ≠ [] → exAtA ex 0 ≠ 0#u64 ∧ exAtA ex 0 ∈ OS) + (hExNotS2 : ∀ p sz r d mp asid va, + capAtA (L.mem s) (exAtA ex 0) ≠ .Frame p sz r d mp true asid va) + (call : Bool) : + vcpu.decode_state_transfer st ar s a vc length buf ex true ⦃ res => + VcpuTopPostA (L := L) AR Z OS m call σ + ((NonDetE.liftME Fl4ArchInvocation.vcpu <| + decodeVcpuStateTransfer (Z := Unit) (specPtr vc) (args.map UScalar.bv) extras true).run σ) s res ⦄ := + vcpu_state_transfer_jA C hCar hP hAK hAR hF hR hU hUbUser hksTcb hSlotTcb Pl Y hrun hcur hvs + hlen hargs hEx hExSlot hExNotS2 true call + +/-- **Label 68 (`ARMVCPUWriteState`)** on `VcpuTopPostA`. -/ +theorem vcpu_write_state_jA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} + {OS U Ac Pp Tb Ub : Set U64} {Lv : U64 → Nat} {ia ie : Nat} + {s : S} {a : A} {σ : State Unit} {m : Fl4SpecA64.McState} {vc : U64} + (C : VcpuDecCtxA (L := L) (ar := ar) AR Z H OS U Ac Pp Tb Ub Lv ia ie s a σ m vc) + (hCar : CarryA Z (L.mem s) σ) + (hP : VcpuPlatAgreeA ar) (hAK : VcpuArchKeepsA ar AR) (hAR : AR a Z.hw) + (hF : FrameMemRelA Z OS Ub (L.ksb s) (L.mem s) σ.machineState.underlyingMemory) + (hR : TcbRegsRelA (L.mem s) σ Z.T) + (hU : UbFootDisjA Z OS Ub) + (hUbUser : ∀ q ∈ Ub, inUserFrame (specPtr q) σ) + (hksTcb : ∀ t ∈ Z.T, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → + ∀ o : U64, o.val < ksSizeA → wa t K ≠ wa (L.ksb s) o) + (hSlotTcb : ∀ t ∈ Z.T, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → + ∀ y, SlotWA Z.th y → wa t K ≠ y ∧ wa t K ≠ wa y 8#u64 ∧ wa t K ≠ wa y 16#u64 ∧ wa t K ≠ wa y 24#u64) + {Rp Nt : Set U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} {slot : U64} + (Pl : SysPlace st H Z.Cs H.frame Rp Nt OS s extra slot) (Y : SymL st H Z.Cs s) + (hrun : Runnable (L.mem s (stA (L.curW s)))) + (hcur : (L.mem s (inqA (L.curW s)) &&& sched.Q_RELEASE) = 0#u64 ∧ L.mem s (scA (L.curW s)) ≠ 0#u64 ∧ + L.mem s (scA (L.curW s)) ∈ Ac) + (hvs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → Aligned b ∧ b ≠ stA (L.curW s)) + {length buf : U64} {args : List U64} {ex : Array obj.Slot 3#usize} + {extras : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)} + (hlen : length.val = args.length) + (hargs : ∀ (i : U64) (w : U64), args[i.val]? = some w → msg.syscall_arg st s (L.curW s) buf i = ok w) + (hEx : ExtraOKA Z.th (L.mem s) ex extras) + (hExSlot : extras ≠ [] → exAtA ex 0 ≠ 0#u64 ∧ exAtA ex 0 ∈ OS) + (hExNotS2 : ∀ p sz r d mp asid va, + capAtA (L.mem s) (exAtA ex 0) ≠ .Frame p sz r d mp true asid va) + (call : Bool) : + vcpu.decode_state_transfer st ar s a vc length buf ex false ⦃ res => + VcpuTopPostA (L := L) AR Z OS m call σ + ((NonDetE.liftME Fl4ArchInvocation.vcpu <| + decodeVcpuStateTransfer (Z := Unit) (specPtr vc) (args.map UScalar.bv) extras false).run σ) s res ⦄ := + vcpu_state_transfer_jA C hCar hP hAK hAR hF hR hU hUbUser hksTcb hSlotTcb Pl Y hrun hcur hvs + hlen hargs hEx hExSlot hExNotS2 false call + +end decode_xfer + +end + +end Fl4ProofsA64.P3.Vcpu diff --git a/proofs/Fl4ProofsA64/P3/VcpuXferReadJA.lean b/proofs/Fl4ProofsA64/P3/VcpuXferReadJA.lean new file mode 100644 index 000000000..ca52011ec --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/VcpuXferReadJA.lean @@ -0,0 +1,1024 @@ +/- +P3 (aarch64, A64-P3 S4 W3): **VCPU read-state-to-frame refinement** (`vcpu.copy_reg_range_to_frame`, +`vcpu.read_state_to_frame`). +-/ +import Fl4ProofsA64.P3.VcpuXferBaseA + +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3.Vcpu + +open Fl4Spec Fl4SpecA64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3 Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Endp +open Fl4ProofsA64.P3.Notif + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## Section 2: `vcpu.copy_reg_range_to_frame` and `vcpu.read_state_to_frame` -/ + +section read_state +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + +theorem inUserFrame_of_kheap {p : ObjRef} {σ σ' : State Unit} + (hkh : σ'.kheap = σ.kheap) (h : inUserFrame p σ) : inUserFrame p σ' := by + simpa [inUserFrame, hkh] using h + +theorem u64_ofNat_val (r : U64) : (⟨BitVec.ofNat 64 r.val⟩ : U64) = r := + UScalar.eq_of_val_eq (by + change (BitVec.ofNat 64 r.val).toNat = r.val + rw [BitVec.toNat_ofNat] + have := r.hBounds; simp only [UScalar.size, UScalarTy.numBits] at this + exact Nat.mod_eq_of_lt this) + +theorem vcpu_reg_get_u64_eqA (s : S) (vc r : U64) (hr : r.val < 64) : + vcpu.reg_get st s vc r = ok (L.mem s (vcpuRegWA vc r.val)) := by + have h := vcpu_reg_get_eqA (L := L) s vc hr + rwa [u64_ofNat_val] at h + +/-- Spec reduction of `copyRegsToFrame` over a list of register indices. -/ +private theorem copyRegsToFrame_consA (vc frame : ObjRef) (r : Nat) (rs : List Nat) + {σ : State Unit} {v : Vcpu} (hk : σ.kheap vc = some (.archObj (.vcpu v))) + (hal : bufAddr frame r &&& mask 3 = 0) (huf : inUserFrame (bufAddr frame r) σ) : + copyRegsToFrame vc frame (r :: rs) σ = + copyRegsToFrame vc frame rs + (setUm σ (storeMem σ.machineState.underlyingMemory (bufAddr frame r) (fl4VcpuRegGet v r))) := by + unfold copyRegsToFrame + change ((do let w ← fl4VcpuGet vc r; storeWordOffs frame r w) >>= fun _ => NonDet.mapMx _ rs) σ = _ + rw [nd_bind_oneSc (by + rw [nd_bind_oneSc (fl4VcpuGet_eqA r hk), storeWordOffs_eqA frame r (fl4VcpuRegGet v r) σ hal huf])] + +/-- **`vcpu.copy_reg_range_to_frame` refines `copyRegsToFrame`**: for `start ≤ end_incl < 64`, +copies registers `start..end_incl` into `frame` and preserves `VcpuXferStateA`. -/ +theorem copy_reg_range_to_frame_refinesA {AR : A → HwRootA → Prop} {Z : XSetsA} {OS Ub : Set U64} + {m : Fl4SpecA64.McState} {a : A} {s : S} {σ : State Unit} + (X : VcpuXferStateA (L := L) AR Z OS Ub m a s σ) + (hU : UbFootDisjA Z OS Ub) + (hCs : ∀ c ∈ Z.Cs, c.val < 128) (hcpu : (L.cpu s).val < 128) + (hfa : ∀ q, FootA (L := L) Z s q → Aligned q) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hIdleT : ∀ c ∈ Z.Cs, L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) ∈ Z.T) + {vc frame start end_incl : U64} (hvc : vc ∈ Z.V) (hWin : FrameWinA Ub frame) + (huf : ∀ k < 128, inUserFrame (bufAddr (specPtr frame) k) σ) + (hle : start.val ≤ end_incl.val) (hend : end_incl.val < 64) : + vcpu.copy_reg_range_to_frame st s vc frame start end_incl ⦃ s' => + ∃ σ', copyRegsToFrame (specPtr vc) (specPtr frame) (idxRange start.val end_incl.val) σ = ({((), σ')}, False) ∧ + VcpuXferStateA (L := L) AR Z OS Ub m a s' σ' ∧ + σ'.kheap = σ.kheap ∧ L.ksb s' = L.ksb s ∧ L.cpu s' = L.cpu s ∧ + (∀ q, FootA (L := L) Z s' q → Aligned q) ∧ + (∀ c ∈ Z.Cs, L.mem s' (L.nodeAt s' c layout.node.IDLE_THREAD) ∈ Z.T) ⦄ := by + unfold vcpu.copy_reg_range_to_frame vcpu.copy_reg_range_to_frame_loop + let Inv : (S × U64) → Prop := fun (s1, r) => + start.val ≤ r.val ∧ r.val ≤ end_incl.val + 1 ∧ + ∃ σ1, copyRegsToFrame (specPtr vc) (specPtr frame) (List.range' start.val (r.val - start.val)) σ = ({((), σ1)}, False) ∧ + VcpuXferStateA (L := L) AR Z OS Ub m a s1 σ1 ∧ + σ1.kheap = σ.kheap ∧ L.ksb s1 = L.ksb s ∧ L.cpu s1 = L.cpu s ∧ + (∀ q, FootA (L := L) Z s1 q → Aligned q) ∧ + (∀ c ∈ Z.Cs, L.mem s1 (L.nodeAt s1 c layout.node.IDLE_THREAD) ∈ Z.T) + let Post : S → Prop := fun s' => + ∃ σ', copyRegsToFrame (specPtr vc) (specPtr frame) (idxRange start.val end_incl.val) σ = ({((), σ')}, False) ∧ + VcpuXferStateA (L := L) AR Z OS Ub m a s' σ' ∧ + σ'.kheap = σ.kheap ∧ L.ksb s' = L.ksb s ∧ L.cpu s' = L.cpu s ∧ + (∀ q, FootA (L := L) Z s' q → Aligned q) ∧ + (∀ c ∈ Z.Cs, L.mem s' (L.nodeAt s' c layout.node.IDLE_THREAD) ∈ Z.T) + apply loop.spec_decr_nat (measure := fun p => end_incl.val + 1 - p.2.val) (inv := Inv) (post := Post) + swap + · exact ⟨le_rfl, by omega, σ, by simp [copyRegsToFrame, NonDet.mapMx]; rfl, X, rfl, rfl, rfl, hfa, hIdleT⟩ + rintro ⟨s1, r⟩ ⟨hsr, hre, σ1, hspec1, X1, hkh1, hksb1, hcpu1, hfa1, hIdle1⟩ + dsimp only + unfold vcpu.copy_reg_range_to_frame_loop.body + by_cases hle_r : r ≤ end_incl + · have hle_rv : r.val ≤ end_incl.val := hle_r + have hr_lt : r.val < 64 := by omega + rw [if_pos hle_r, u64_mul_ok (by change r.val * 8 < 2 ^ 64; omega), bind_ok, lift_ok, bind_ok, + vcpu_reg_get_u64_eqA s1 vc r hr_lt, bind_ok, L.write_eq, bind_ok, + u64_add_ok (by change r.val + 1 < 2 ^ 64; omega), bind_ok, WP.spec_ok] + obtain ⟨v, hkv, -, -⟩ := X1.vcpu.core.obj vc hvc + have hreg_bv : (L.mem s1 (vcpuRegWA vc r.val)).bv = fl4VcpuRegGet v r.val := + X1.vcpu.regs vc hvc v hkv r.val hr_lt + have hmul_eq : (⟨BitVec.ofNat 64 (r.val * 8#u64.val)⟩ : U64) = ⟨BitVec.ofNat 64 (8 * r.val)⟩ := by + congr 1; congr 1; change r.val * 8 = 8 * r.val; omega + set b : U64 := wa frame ⟨BitVec.ofNat 64 (8 * r.val)⟩ + have hb : b ∈ Ub := hWin.2 r.val (by omega) + have hsp_b : specPtr b = bufAddr (specPtr frame) r.val := specPtr_wa_wordA (by have := hWin.1; omega) + have X2 := X1.ub_wr hU hCs (hcpu1 ▸ hcpu) hfa1 hsub hIdle1 hb (L.mem s1 (vcpuRegWA vc r.val)) + rw [hmul_eq] + have hstep_spec : copyRegsToFrame (specPtr vc) (specPtr frame) + (List.range' start.val (r.val + 1 - start.val)) σ = + ({((), setUm σ1 (storeMem σ1.machineState.underlyingMemory (specPtr b) (L.mem s1 (vcpuRegWA vc r.val)).bv))}, False) := by + have hrg : List.range' start.val (r.val + 1 - start.val) = + List.range' start.val (r.val - start.val) ++ [r.val] := by + have : r.val + 1 - start.val = (r.val - start.val) + 1 := by omega + rw [this, List.range'_concat]; congr 1; congr 1; simp only [Nat.one_mul]; omega + rw [hrg]; unfold copyRegsToFrame at hspec1 ⊢ + rw [mapMx_appendA, nd_bind_oneSc hspec1] + unfold NonDet.mapMx + change ((do let w ← fl4VcpuGet (specPtr vc) r.val; storeWordOffs (specPtr frame) r.val w) >>= fun _ => NonDet.pure ()) σ1 = _ + rw [nd_bind_oneSc (by + rw [nd_bind_oneSc (fl4VcpuGet_eqA r.val hkv), + storeWordOffs_eqA (specPtr frame) r.val (fl4VcpuRegGet v r.val) σ1 + (al3_of_frameWinA X1.fmem hWin r.val (by omega)) + (inUserFrame_of_kheap hkh1 (huf r.val (by omega)))])] + rw [hsp_b, hreg_bv]; rfl + have hr1_val : (⟨BitVec.ofNat 64 (r.val + 1#u64.val)⟩ : U64).val = r.val + 1 := by + change (BitVec.ofNat 64 (r.val + 1)).toNat = r.val + 1 + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + have hba : Aligned b := X1.fmem.aligned b hb + have hfa2 : ∀ q, FootA (L := L) Z (L.wr s1 b (L.mem s1 (vcpuRegWA vc r.val))) q → Aligned q := by + intro q hq; exact hfa1 q (by simpa [FootA_wr_eqA] using hq) + have hIdle2 : ∀ c ∈ Z.Cs, + L.mem (L.wr s1 b (L.mem s1 (vcpuRegWA vc r.val))) + (L.nodeAt (L.wr s1 b (L.mem s1 (vcpuRegWA vc r.val))) c layout.node.IDLE_THREAD) ∈ Z.T := by + intro c hc + have hne_idle : L.nodeAt s1 c layout.node.IDLE_THREAD ≠ b := by + intro e + have hnf := not_footA_of_ubA X1.vcpu.place X1.fmem hU X1.j.1.ksFit hCs (hcpu1 ▸ hcpu) hb + exact hnf (e ▸ FootA.nslot _ (FootNA.node c _ hc (Or.inr (Or.inl rfl)))) + have hal_idle : Aligned (L.nodeAt s1 c layout.node.IDLE_THREAD) := + hfa1 _ (FootA.nslot _ (FootNA.node c _ hc (Or.inr (Or.inl rfl)))) + rw [nodeAt_wrA, mem_wr_neA hba hal_idle hne_idle.symm]; exact hIdle1 c hc + refine ⟨⟨by rw [hr1_val]; omega, by rw [hr1_val]; omega, _, by rw [hr1_val]; exact hstep_spec, X2, hkh1, + by rw [L.ksb_wr]; exact hksb1, by rw [L.cpu_wr]; exact hcpu1, hfa2, hIdle2⟩, by dsimp only; rw [hr1_val]; omega⟩ + · rw [if_neg hle_r, WP.spec_ok] + have hreq : r.val = end_incl.val + 1 := by + have : ¬ r.val ≤ end_incl.val := hle_r; omega + dsimp only [Post] + refine ⟨σ1, ?_, X1, hkh1, hksb1, hcpu1, hfa1, hIdle1⟩ + unfold idxRange; rwa [← hreq] + +/-- Helper: `vcpu.read_state_to_frame_loop0` (`VGIC_AP1R0..3`) refines the spec AP1R loop. -/ +private theorem read_ap1r_loop_refinesA {AR : A → HwRootA → Prop} {Z : XSetsA} {OS Ub : Set U64} + {m : Fl4SpecA64.McState} {a : A} {s : S} {σ : State Unit} + (X : VcpuXferStateA (L := L) AR Z OS Ub m a s σ) + (hU : UbFootDisjA Z OS Ub) + (hCs : ∀ c ∈ Z.Cs, c.val < 128) (hcpu : (L.cpu s).val < 128) + (hfa : ∀ q, FootA (L := L) Z s q → Aligned q) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hIdleT : ∀ c ∈ Z.Cs, L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) ∈ Z.T) + {vc frame : U64} (hvc : vc ∈ Z.V) (hWin : FrameWinA Ub frame) + (huf : ∀ k < 128, inUserFrame (bufAddr (specPtr frame) k) σ) : + vcpu.read_state_to_frame_loop0 st s vc frame fl4GicV3 fl4_abi.consts.vcpu_reg.VGIC_AP1R0 ⦃ s' => + ∃ σ', NonDet.mapMx (fun r => do + let w ← if fl4GicV3 then fl4VcpuGet (specPtr vc) r else pure 0 + storeWordOffs (specPtr frame) r w) (idxRange vrVgicAp1r0 vrVgicAp1r3) σ = ({((), σ')}, False) ∧ + VcpuXferStateA (L := L) AR Z OS Ub m a s' σ' ∧ + σ'.kheap = σ.kheap ∧ L.ksb s' = L.ksb s ∧ L.cpu s' = L.cpu s ∧ + (∀ q, FootA (L := L) Z s' q → Aligned q) ∧ + (∀ c ∈ Z.Cs, L.mem s' (L.nodeAt s' c layout.node.IDLE_THREAD) ∈ Z.T) ⦄ := by + have h44 : fl4_abi.consts.vcpu_reg.VGIC_AP1R0.val = vrVgicAp1r0 := by + unfold fl4_abi.consts.vcpu_reg.VGIC_AP1R0 vrVgicAp1r0; rfl + have h47 : fl4_abi.consts.vcpu_reg.VGIC_AP1R3.val = vrVgicAp1r3 := by + unfold fl4_abi.consts.vcpu_reg.VGIC_AP1R3 vrVgicAp1r3; rfl + unfold vcpu.read_state_to_frame_loop0 + let stepSpec : Nat → SMonad Unit Unit := fun r => do + let w ← if fl4GicV3 then fl4VcpuGet (specPtr vc) r else pure 0 + storeWordOffs (specPtr frame) r w + let Inv : (S × U64) → Prop := fun (s1, r) => + vrVgicAp1r0 ≤ r.val ∧ r.val ≤ vrVgicAp1r3 + 1 ∧ + ∃ σ1, NonDet.mapMx stepSpec (List.range' vrVgicAp1r0 (r.val - vrVgicAp1r0)) σ = ({((), σ1)}, False) ∧ + VcpuXferStateA (L := L) AR Z OS Ub m a s1 σ1 ∧ + σ1.kheap = σ.kheap ∧ L.ksb s1 = L.ksb s ∧ L.cpu s1 = L.cpu s ∧ + (∀ q, FootA (L := L) Z s1 q → Aligned q) ∧ + (∀ c ∈ Z.Cs, L.mem s1 (L.nodeAt s1 c layout.node.IDLE_THREAD) ∈ Z.T) + let Post : S → Prop := fun s' => + ∃ σ', NonDet.mapMx stepSpec (idxRange vrVgicAp1r0 vrVgicAp1r3) σ = ({((), σ')}, False) ∧ + VcpuXferStateA (L := L) AR Z OS Ub m a s' σ' ∧ + σ'.kheap = σ.kheap ∧ L.ksb s' = L.ksb s ∧ L.cpu s' = L.cpu s ∧ + (∀ q, FootA (L := L) Z s' q → Aligned q) ∧ + (∀ c ∈ Z.Cs, L.mem s' (L.nodeAt s' c layout.node.IDLE_THREAD) ∈ Z.T) + apply loop.spec_decr_nat (measure := fun p => vrVgicAp1r3 + 1 - p.2.val) (inv := Inv) (post := Post) + swap + · exact ⟨by omega, by unfold vrVgicAp1r0 vrVgicAp1r3 at *; omega, σ, + by rw [h44]; simp [NonDet.mapMx]; rfl, X, rfl, rfl, rfl, hfa, hIdleT⟩ + rintro ⟨s1, r⟩ ⟨hsr, hre, σ1, hspec1, X1, hkh1, hksb1, hcpu1, hfa1, hIdle1⟩ + dsimp only + unfold vcpu.read_state_to_frame_loop0.body + by_cases hle_r : r ≤ fl4_abi.consts.vcpu_reg.VGIC_AP1R3 + · have hle_rv : r.val ≤ vrVgicAp1r3 := by + have : r.val ≤ fl4_abi.consts.vcpu_reg.VGIC_AP1R3.val := hle_r; omega + have hr_lt : r.val < 64 := by unfold vrVgicAp1r3 at *; omega + obtain ⟨v, hkv, -, -⟩ := X1.vcpu.core.obj vc hvc + have hval_ok : (if fl4GicV3 then vcpu.reg_get st s1 vc r else ok 0#u64) = + ok (if fl4GicV3 then L.mem s1 (vcpuRegWA vc r.val) else 0#u64) := by + cases fl4GicV3 + · rfl + · exact vcpu_reg_get_u64_eqA s1 vc r hr_lt + rw [if_pos hle_r, hval_ok, bind_ok, u64_mul_ok (by change r.val * 8 < 2 ^ 64; omega), bind_ok, lift_ok, bind_ok, + L.write_eq, bind_ok, u64_add_ok (by change r.val + 1 < 2 ^ 64; omega), bind_ok, WP.spec_ok] + set w : U64 := if fl4GicV3 then L.mem s1 (vcpuRegWA vc r.val) else 0#u64 + have hw_bv : w.bv = if fl4GicV3 then fl4VcpuRegGet v r.val else 0 := by + dsimp only [w]; cases fl4GicV3 + · rfl + · exact X1.vcpu.regs vc hvc v hkv r.val hr_lt + have hmul_eq : (⟨BitVec.ofNat 64 (r.val * 8#u64.val)⟩ : U64) = ⟨BitVec.ofNat 64 (8 * r.val)⟩ := by + congr 1; congr 1; change r.val * 8 = 8 * r.val; omega + set b : U64 := wa frame ⟨BitVec.ofNat 64 (8 * r.val)⟩ + have hb : b ∈ Ub := hWin.2 r.val (by omega) + have hsp_b : specPtr b = bufAddr (specPtr frame) r.val := specPtr_wa_wordA (by have := hWin.1; omega) + have X2 := X1.ub_wr hU hCs (hcpu1 ▸ hcpu) hfa1 hsub hIdle1 hb w + rw [hmul_eq] + have hstep_spec : NonDet.mapMx stepSpec (List.range' vrVgicAp1r0 (r.val + 1 - vrVgicAp1r0)) σ = + ({((), setUm σ1 (storeMem σ1.machineState.underlyingMemory (specPtr b) w.bv))}, False) := by + have hrg : List.range' vrVgicAp1r0 (r.val + 1 - vrVgicAp1r0) = + List.range' vrVgicAp1r0 (r.val - vrVgicAp1r0) ++ [r.val] := by + have : r.val + 1 - vrVgicAp1r0 = (r.val - vrVgicAp1r0) + 1 := by omega + rw [this, List.range'_concat]; congr 1; congr 1; simp only [Nat.one_mul]; unfold vrVgicAp1r0 at *; omega + rw [hrg, mapMx_appendA, nd_bind_oneSc hspec1] + unfold NonDet.mapMx + change (stepSpec r.val >>= fun _ => NonDet.pure ()) σ1 = _ + have h1 : stepSpec r.val σ1 = + ({((), setUm σ1 (storeMem σ1.machineState.underlyingMemory (specPtr b) w.bv))}, False) := by + dsimp only [stepSpec] + cases hgv : fl4GicV3 + · simp only [Bool.false_eq_true, ↓reduceIte] + change (NonDet.pure 0 >>= _) σ1 = _ + rw [nd_bind_oneSc (m := NonDet.pure 0) rfl, + storeWordOffs_eqA _ _ _ _ (al3_of_frameWinA X1.fmem hWin r.val (by omega)) (inUserFrame_of_kheap hkh1 (huf r.val (by omega))), + hsp_b, hw_bv]; simp [hgv] + · simp only [↓reduceIte] + rw [nd_bind_oneSc (fl4VcpuGet_eqA r.val hkv), + storeWordOffs_eqA _ _ _ _ (al3_of_frameWinA X1.fmem hWin r.val (by omega)) (inUserFrame_of_kheap hkh1 (huf r.val (by omega))), + hsp_b, hw_bv]; simp [hgv] + rw [nd_bind_oneSc h1]; rfl + have hr1_val : (⟨BitVec.ofNat 64 (r.val + 1#u64.val)⟩ : U64).val = r.val + 1 := by + change (BitVec.ofNat 64 (r.val + 1)).toNat = r.val + 1 + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + have hba : Aligned b := X1.fmem.aligned b hb + have hfa2 : ∀ q, FootA (L := L) Z (L.wr s1 b w) q → Aligned q := fun q hq => + hfa1 q (by simpa [FootA_wr_eqA] using hq) + have hIdle2 : ∀ c ∈ Z.Cs, + L.mem (L.wr s1 b w) (L.nodeAt (L.wr s1 b w) c layout.node.IDLE_THREAD) ∈ Z.T := by + intro c hc + have hne_idle : L.nodeAt s1 c layout.node.IDLE_THREAD ≠ b := fun e => + not_footA_of_ubA X1.vcpu.place X1.fmem hU X1.j.1.ksFit hCs (hcpu1 ▸ hcpu) hb + (e ▸ FootA.nslot _ (FootNA.node c _ hc (Or.inr (Or.inl rfl)))) + have hal_idle : Aligned (L.nodeAt s1 c layout.node.IDLE_THREAD) := + hfa1 _ (FootA.nslot _ (FootNA.node c _ hc (Or.inr (Or.inl rfl)))) + rw [nodeAt_wrA, mem_wr_neA hba hal_idle hne_idle.symm]; exact hIdle1 c hc + refine ⟨⟨by rw [hr1_val]; omega, by rw [hr1_val]; omega, _, by rw [hr1_val]; exact hstep_spec, X2, hkh1, + by rw [L.ksb_wr]; exact hksb1, by rw [L.cpu_wr]; exact hcpu1, hfa2, hIdle2⟩, by dsimp only; rw [hr1_val]; omega⟩ + · rw [if_neg hle_r, WP.spec_ok] + have hreq : r.val = vrVgicAp1r3 + 1 := by + have : ¬ r.val ≤ fl4_abi.consts.vcpu_reg.VGIC_AP1R3.val := hle_r; omega + dsimp only [Post] + refine ⟨σ1, ?_, X1, hkh1, hksb1, hcpu1, hfa1, hIdle1⟩ + unfold idxRange; rwa [← hreq] + +/-- Helper: `vcpu.read_state_to_frame_loop1` (and identically `loop3`) refines the 16-LR read loop. -/ +private theorem read_lrs_loop_refinesA {AR : A → HwRootA → Prop} {Z : XSetsA} {OS Ub : Set U64} + {m : Fl4SpecA64.McState} {a : A} {s : S} {σ : State Unit} + (X : VcpuXferStateA (L := L) AR Z OS Ub m a s σ) + (hU : UbFootDisjA Z OS Ub) + (hCs : ∀ c ∈ Z.Cs, c.val < 128) (hcpu : (L.cpu s).val < 128) + (hfa : ∀ q, FootA (L := L) Z s q → Aligned q) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hIdleT : ∀ c ∈ Z.Cs, L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) ∈ Z.T) + {vc frame : U64} (hvc : vc ∈ Z.V) (hWin : FrameWinA Ub frame) + (huf : ∀ k < 128, inUserFrame (bufAddr (specPtr frame) k) σ) : + vcpu.read_state_to_frame_loop1 st s vc frame ⟨BitVec.ofNat 64 fl4NumLrs⟩ 0#u64 ⦃ s' => + ∃ σ', NonDet.mapMx (fun i => do + let w ← if i < fl4NumLrs then fl4VcpuGet (specPtr vc) (vrVgicLr0 + i) else pure 0 + storeWordOffs (specPtr frame) (vrVgicLr0 + i) w) (List.range 16) σ = ({((), σ')}, False) ∧ + VcpuXferStateA (L := L) AR Z OS Ub m a s' σ' ∧ + σ'.kheap = σ.kheap ∧ L.ksb s' = L.ksb s ∧ L.cpu s' = L.cpu s ∧ + (∀ q, FootA (L := L) Z s' q → Aligned q) ∧ + (∀ c ∈ Z.Cs, L.mem s' (L.nodeAt s' c layout.node.IDLE_THREAD) ∈ Z.T) ⦄ := by + have hlrs16 : fl4NumLrs ≤ 16 := fl4NumLrs_le_16 + have hlrs_val : (⟨BitVec.ofNat 64 fl4NumLrs⟩ : U64).val = fl4NumLrs := by + change (BitVec.ofNat 64 fl4NumLrs).toNat = fl4NumLrs + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + unfold vcpu.read_state_to_frame_loop1 + let stepSpec : Nat → SMonad Unit Unit := fun i => do + let w ← if i < fl4NumLrs then fl4VcpuGet (specPtr vc) (vrVgicLr0 + i) else pure 0 + storeWordOffs (specPtr frame) (vrVgicLr0 + i) w + let Inv : (S × U64) → Prop := fun (s1, i) => + i.val ≤ 16 ∧ + ∃ σ1, NonDet.mapMx stepSpec (List.range i.val) σ = ({((), σ1)}, False) ∧ + VcpuXferStateA (L := L) AR Z OS Ub m a s1 σ1 ∧ + σ1.kheap = σ.kheap ∧ L.ksb s1 = L.ksb s ∧ L.cpu s1 = L.cpu s ∧ + (∀ q, FootA (L := L) Z s1 q → Aligned q) ∧ + (∀ c ∈ Z.Cs, L.mem s1 (L.nodeAt s1 c layout.node.IDLE_THREAD) ∈ Z.T) + let Post : S → Prop := fun s' => + ∃ σ', NonDet.mapMx stepSpec (List.range 16) σ = ({((), σ')}, False) ∧ + VcpuXferStateA (L := L) AR Z OS Ub m a s' σ' ∧ + σ'.kheap = σ.kheap ∧ L.ksb s' = L.ksb s ∧ L.cpu s' = L.cpu s ∧ + (∀ q, FootA (L := L) Z s' q → Aligned q) ∧ + (∀ c ∈ Z.Cs, L.mem s' (L.nodeAt s' c layout.node.IDLE_THREAD) ∈ Z.T) + apply loop.spec_decr_nat (measure := fun p => 16 - p.2.val) (inv := Inv) (post := Post) + swap + · exact ⟨by decide, σ, by simp [NonDet.mapMx]; rfl, X, rfl, rfl, rfl, hfa, hIdleT⟩ + rintro ⟨s1, i⟩ ⟨hle16, σ1, hspec1, X1, hkh1, hksb1, hcpu1, hfa1, hIdle1⟩ + dsimp only + unfold vcpu.read_state_to_frame_loop1.body + by_cases hi16 : i < 16#u64 + · have hilt : i.val < 16 := hi16 + have hr_val : (wa fl4_abi.consts.vcpu_reg.VGIC_LR0 i).val = vrVgicLr0 + i.val := by + rw [wa_valP] + have h48 : fl4_abi.consts.vcpu_reg.VGIC_LR0.val = 48 := by unfold fl4_abi.consts.vcpu_reg.VGIC_LR0; rfl + rw [h48]; unfold vrVgicLr0; exact Nat.mod_eq_of_lt (by omega) + have hr_lt64 : (wa fl4_abi.consts.vcpu_reg.VGIC_LR0 i).val < 64 := by rw [hr_val]; unfold vrVgicLr0; omega + obtain ⟨v, hkv, -, -⟩ := X1.vcpu.core.obj vc hvc + have hval_ok : (if i < ⟨BitVec.ofNat 64 fl4NumLrs⟩ then + vcpu.reg_get st s1 vc (wa fl4_abi.consts.vcpu_reg.VGIC_LR0 i) else ok 0#u64) = + ok (if i.val < fl4NumLrs then L.mem s1 (vcpuRegWA vc (vrVgicLr0 + i.val)) else 0#u64) := by + by_cases hlt : i.val < fl4NumLrs + · rw [if_pos (by change i.val < (⟨BitVec.ofNat 64 fl4NumLrs⟩ : U64).val; omega), if_pos hlt] + rw [vcpu_reg_get_u64_eqA s1 vc _ hr_lt64, hr_val] + · rw [if_neg (by change ¬ i.val < (⟨BitVec.ofNat 64 fl4NumLrs⟩ : U64).val; omega), if_neg hlt] + rw [if_pos hi16, lift_ok, bind_ok, hval_ok, bind_ok, + u64_mul_ok (by rw [hr_val]; change (vrVgicLr0 + i.val) * 8 < 2 ^ 64; unfold vrVgicLr0; omega), bind_ok, lift_ok, bind_ok, L.write_eq, bind_ok, + u64_add_ok (by change i.val + 1 < 2 ^ 64; omega), bind_ok, WP.spec_ok] + set w : U64 := if i.val < fl4NumLrs then L.mem s1 (vcpuRegWA vc (vrVgicLr0 + i.val)) else 0#u64 + have hw_bv : w.bv = if i.val < fl4NumLrs then fl4VcpuRegGet v (vrVgicLr0 + i.val) else 0 := by + dsimp only [w] + by_cases hlt : i.val < fl4NumLrs + · rw [if_pos hlt, if_pos hlt]; exact X1.vcpu.regs vc hvc v hkv (vrVgicLr0 + i.val) (by unfold vrVgicLr0; omega) + · rw [if_neg hlt, if_neg hlt]; rfl + have hmul_eq : (⟨BitVec.ofNat 64 ((wa fl4_abi.consts.vcpu_reg.VGIC_LR0 i).val * 8#u64.val)⟩ : U64) = + ⟨BitVec.ofNat 64 (8 * (vrVgicLr0 + i.val))⟩ := by + congr 1; rw [hr_val]; congr 1; change (vrVgicLr0 + i.val) * 8 = 8 * (vrVgicLr0 + i.val); omega + set b : U64 := wa frame ⟨BitVec.ofNat 64 (8 * (vrVgicLr0 + i.val))⟩ + have hb : b ∈ Ub := hWin.2 (vrVgicLr0 + i.val) (by unfold vrVgicLr0; omega) + have hsp_b : specPtr b = bufAddr (specPtr frame) (vrVgicLr0 + i.val) := + specPtr_wa_wordA (by have := hWin.1; unfold vrVgicLr0; omega) + have X2 := X1.ub_wr hU hCs (hcpu1 ▸ hcpu) hfa1 hsub hIdle1 hb w + rw [hmul_eq] + have hstep_spec : NonDet.mapMx stepSpec (List.range (i.val + 1)) σ = + ({((), setUm σ1 (storeMem σ1.machineState.underlyingMemory (specPtr b) w.bv))}, False) := by + rw [List.range_succ, mapMx_appendA, nd_bind_oneSc hspec1] + unfold NonDet.mapMx + change (stepSpec i.val >>= fun _ => NonDet.pure ()) σ1 = _ + have h1 : stepSpec i.val σ1 = + ({((), setUm σ1 (storeMem σ1.machineState.underlyingMemory (specPtr b) w.bv))}, False) := by + dsimp only [stepSpec] + have hal_lr := al3_of_frameWinA X1.fmem hWin (vrVgicLr0 + i.val) (by unfold vrVgicLr0; omega) + have huf_lr := inUserFrame_of_kheap hkh1 (huf (vrVgicLr0 + i.val) (by unfold vrVgicLr0; omega)) + by_cases hlt : i.val < fl4NumLrs + · rw [if_pos hlt, nd_bind_oneSc (fl4VcpuGet_eqA (vrVgicLr0 + i.val) hkv), + storeWordOffs_eqA _ _ _ _ hal_lr huf_lr, hsp_b, hw_bv, if_pos hlt] + · rw [if_neg hlt] + change (NonDet.pure 0 >>= _) σ1 = _ + rw [nd_bind_oneSc (m := NonDet.pure 0) rfl, + storeWordOffs_eqA _ _ _ _ hal_lr huf_lr, hsp_b, hw_bv, if_neg hlt] + rw [nd_bind_oneSc h1]; rfl + have hi1_val : (⟨BitVec.ofNat 64 (i.val + 1#u64.val)⟩ : U64).val = i.val + 1 := by + change (BitVec.ofNat 64 (i.val + 1)).toNat = i.val + 1 + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + have hba : Aligned b := X1.fmem.aligned b hb + have hfa2 : ∀ q, FootA (L := L) Z (L.wr s1 b w) q → Aligned q := fun q hq => + hfa1 q (by simpa [FootA_wr_eqA] using hq) + have hIdle2 : ∀ c ∈ Z.Cs, + L.mem (L.wr s1 b w) (L.nodeAt (L.wr s1 b w) c layout.node.IDLE_THREAD) ∈ Z.T := by + intro c hc + have hne_idle : L.nodeAt s1 c layout.node.IDLE_THREAD ≠ b := fun e => + not_footA_of_ubA X1.vcpu.place X1.fmem hU X1.j.1.ksFit hCs (hcpu1 ▸ hcpu) hb + (e ▸ FootA.nslot _ (FootNA.node c _ hc (Or.inr (Or.inl rfl)))) + have hal_idle : Aligned (L.nodeAt s1 c layout.node.IDLE_THREAD) := + hfa1 _ (FootA.nslot _ (FootNA.node c _ hc (Or.inr (Or.inl rfl)))) + rw [nodeAt_wrA, mem_wr_neA hba hal_idle hne_idle.symm]; exact hIdle1 c hc + refine ⟨⟨by rw [hi1_val]; omega, _, by rw [hi1_val]; exact hstep_spec, X2, hkh1, + by rw [L.ksb_wr]; exact hksb1, by rw [L.cpu_wr]; exact hcpu1, hfa2, hIdle2⟩, by dsimp only; rw [hi1_val]; omega⟩ + · rw [if_neg hi16, WP.spec_ok] + have hieq : i.val = 16 := by have : ¬ i.val < 16 := hi16; omega + dsimp only [Post] + exact ⟨σ1, by rwa [← hieq], X1, hkh1, hksb1, hcpu1, hfa1, hIdle1⟩ + +/-- Helper: `fl4UserRegs` agrees with `idxRegA` at every index `i < 37`. -/ +theorem fl4UserRegs_getElemA (i : Nat) (hi : i < 37) : + fl4UserRegs[i]'(by simp [fl4UserRegs]; omega) = idxRegA i := by + have : i < 37 := hi + interval_cases i <;> rfl + +theorem regIdxA_idxRegA (i : Nat) (hi : i < 37) : + regIdxA (idxRegA i) = ⟨BitVec.ofNat 64 i⟩ := by + interval_cases i <;> rfl + +/-- Helper: `asUser t (getRegister r)` on a TCB `x`. -/ +private theorem asUser_getReg_eqA {σ : State Unit} {t : ObjRef} {x : Tcb} + (hk : σ.kheap t = some (.tcb x)) (r : Register) : + asUser t (getRegister r) σ = ({(x.tcbArch.tcbContext.userRegs r, σ)}, False) := by + have hg : NonDet.getsThe (getTcb (Z := Unit) t) σ = ({(x, σ)}, False) := by + simp [NonDet.getsThe, getTcb, readObject, hk, NonDet.assertOpt]; rfl + have ho : getObject (Z := Unit) t σ = ({(.tcb x, σ)}, False) := by + simp [getObject, NonDet.getsThe, readObject, hk, NonDet.assertOpt]; rfl + have hsel : NonDet.selectF (getRegister r (archTcbContextGet x.tcbArch)) σ = + ({((x.tcbArch.tcbContext.userRegs r, x.tcbArch.tcbContext), σ)}, False) := by + have h1 : getRegister r (archTcbContextGet x.tcbArch) = + ({(x.tcbArch.tcbContext.userRegs r, x.tcbArch.tcbContext)}, False) := rfl + rw [h1] + simp only [NonDet.selectF, Set.mem_singleton_iff, Prod.mk.injEq] + refine ⟨Set.ext fun y => ?_, trivial⟩ + simp [Prod.ext_iff] + have hso : setObject (Z := Unit) t (.tcb { x with tcbArch := archTcbContextSet x.tcbArch.tcbContext x.tcbArch }) σ = + ({((), σ)}, False) := by + unfold setObject + rw [nd_bind_singleA ho, nd_bind_singleA (show (NonDet.assert (aType (.tcb x) = aType (.tcb _)) : + NonDet (State Unit) Unit) σ = ({((), σ)}, False) by simp [NonDet.assert, aType]), + nd_bind_singleA (show NonDet.get σ = ({(σ, σ)}, False) from rfl)] + have hupd : { σ with kheap := Function.update σ.kheap t (some (.tcb x)) } = σ := by + have hfe : Function.update σ.kheap t (some (.tcb x)) = σ.kheap := Function.update_eq_self_iff.2 hk.symm + rw [hfe] + exact congrArg (fun s => ({((), s)}, False)) hupd + unfold asUser + rw [nd_bind_oneA hg, nd_bind_oneA (show (pure (archTcbContextGet x.tcbArch) : SMonad Unit UserContext) σ = _ from rfl), + nd_bind_oneA hsel] + dsimp only + rw [nd_bind_oneA (show (pure _ : SMonad Unit Tcb) σ = _ from rfl), nd_bind_oneA hso] + rfl + +/-- Helper: `vcpu.read_state_to_frame_loop2` (and identically `loop4`, `loop5`) refines the 37-GPR read loop. -/ +private theorem read_gprs_loop_refinesA {AR : A → HwRootA → Prop} {Z : XSetsA} {OS Ub : Set U64} + {m : Fl4SpecA64.McState} {a : A} {s : S} {σ : State Unit} + (X : VcpuXferStateA (L := L) AR Z OS Ub m a s σ) + (hU : UbFootDisjA Z OS Ub) + (hCs : ∀ c ∈ Z.Cs, c.val < 128) (hcpu : (L.cpu s).val < 128) + (hfa : ∀ q, FootA (L := L) Z s q → Aligned q) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hIdleT : ∀ c ∈ Z.Cs, L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) ∈ Z.T) + {vc frame : U64} (hvc : vc ∈ Z.V) (hWin : FrameWinA Ub frame) + (huf : ∀ k < 128, inUserFrame (bufAddr (specPtr frame) k) σ) + {v : Vcpu} (hkv : σ.kheap (specPtr vc) = some (.archObj (.vcpu v))) : + vcpu.read_state_to_frame_loop2 st s (wa frame 512#u64) (L.mem s vc) 0#u64 ⦃ s' => + ∃ σ', NonDet.mapMx (fun (i, r) => do + let w ← match v.vcpuTcb with + | some t => asUser t (getRegister r) + | none => pure 0 + storeWordOffs (specPtr frame) (64 + i) w) + (List.range fl4UserRegs.length |>.zip fl4UserRegs) σ = ({((), σ')}, False) ∧ + VcpuXferStateA (L := L) AR Z OS Ub m a s' σ' ∧ + σ'.kheap = σ.kheap ∧ L.ksb s' = L.ksb s ∧ L.cpu s' = L.cpu s ∧ + (∀ q, FootA (L := L) Z s' q → Aligned q) ∧ + (∀ c ∈ Z.Cs, L.mem s' (L.nodeAt s' c layout.node.IDLE_THREAD) ∈ Z.T) ⦄ := by + have hcnt : layout.reg.COUNT.val = 37 := by unfold layout.reg.COUNT; rfl + have hlen37 : fl4UserRegs.length = 37 := rfl + set t_ptr : U64 := L.mem s vc + have hvtcb : v.vcpuTcb = optRefA t_ptr := by + obtain ⟨v0, hk0, ht0, -⟩ := X.vcpu.core.obj vc hvc + rw [hkv] at hk0; cases hk0; exact ht0 + unfold vcpu.read_state_to_frame_loop2 + let stepSpec : Nat × Register → SMonad Unit Unit := fun (i, r) => do + let w ← match v.vcpuTcb with + | some t => asUser t (getRegister r) + | none => pure 0 + storeWordOffs (specPtr frame) (64 + i) w + let pairs : List (Nat × Register) := (List.range fl4UserRegs.length).zip fl4UserRegs + have hpairs_len : pairs.length = 37 := by simp [pairs, hlen37] + let Inv : (S × U64) → Prop := fun (s1, i) => + i.val ≤ 37 ∧ + ∃ σ1, NonDet.mapMx stepSpec (pairs.take i.val) σ = ({((), σ1)}, False) ∧ + VcpuXferStateA (L := L) AR Z OS Ub m a s1 σ1 ∧ + σ1.kheap = σ.kheap ∧ L.ksb s1 = L.ksb s ∧ L.cpu s1 = L.cpu s ∧ + (∀ q, FootA (L := L) Z s1 q → Aligned q) ∧ + (∀ c ∈ Z.Cs, L.mem s1 (L.nodeAt s1 c layout.node.IDLE_THREAD) ∈ Z.T) + let Post : S → Prop := fun s' => + ∃ σ', NonDet.mapMx stepSpec pairs σ = ({((), σ')}, False) ∧ + VcpuXferStateA (L := L) AR Z OS Ub m a s' σ' ∧ + σ'.kheap = σ.kheap ∧ L.ksb s' = L.ksb s ∧ L.cpu s' = L.cpu s ∧ + (∀ q, FootA (L := L) Z s' q → Aligned q) ∧ + (∀ c ∈ Z.Cs, L.mem s' (L.nodeAt s' c layout.node.IDLE_THREAD) ∈ Z.T) + apply loop.spec_decr_nat (measure := fun p => 37 - p.2.val) (inv := Inv) (post := Post) + swap + · exact ⟨by decide, σ, by simp [NonDet.mapMx]; rfl, X, rfl, rfl, rfl, hfa, hIdleT⟩ + rintro ⟨s1, i⟩ ⟨hle37, σ1, hspec1, X1, hkh1, hksb1, hcpu1, hfa1, hIdle1⟩ + dsimp only + unfold vcpu.read_state_to_frame_loop2.body + by_cases hi37 : i < layout.reg.COUNT + · have hilt : i.val < 37 := by have : i.val < layout.reg.COUNT.val := hi37; omega + have hval_ok : (if t_ptr != 0#u64 then obj.Tcb.reg st t_ptr s1 i else ok 0#u64) = + ok (if t_ptr != 0#u64 then L.mem s1 (Endp.regA t_ptr i) else 0#u64) := by + by_cases hnz : (t_ptr != 0#u64) = true + · rw [if_pos hnz, if_pos hnz]; exact eq_of_spec_fd (obj.Tcb.reg.spec s1 t_ptr i) + · rw [if_neg hnz, if_neg hnz] + rw [if_pos hi37, hval_ok, bind_ok, u64_mul_ok (by change i.val * 8 < 2 ^ 64; omega), bind_ok, lift_ok, bind_ok, + L.write_eq, bind_ok, u64_add_ok (by change i.val + 1 < 2 ^ 64; omega), bind_ok, WP.spec_ok] + set w : U64 := if t_ptr != 0#u64 then L.mem s1 (Endp.regA t_ptr i) else 0#u64 + have hwa_eq : wa (wa frame 512#u64) ⟨BitVec.ofNat 64 (i.val * 8#u64.val)⟩ = + wa frame ⟨BitVec.ofNat 64 (8 * (64 + i.val))⟩ := by + apply UScalar.eq_of_val_eq + rw [wa_valP, wa_valP, wa_valP] + have h512 : (512#u64 : U64).val = 512 := rfl + have h8i : (⟨BitVec.ofNat 64 (i.val * 8#u64.val)⟩ : U64).val = 8 * i.val := by + change (BitVec.ofNat 64 (i.val * 8)).toNat = 8 * i.val + rw [BitVec.toNat_ofNat]; rw [Nat.mod_eq_of_lt (by omega)]; omega + have h864 : (⟨BitVec.ofNat 64 (8 * (64 + i.val))⟩ : U64).val = 512 + 8 * i.val := by + change (BitVec.ofNat 64 _).toNat = _ + rw [BitVec.toNat_ofNat]; rw [Nat.mod_eq_of_lt (by omega)]; omega + rw [h512, h8i, h864, Nat.mod_eq_of_lt (by have := hWin.1; omega), + Nat.mod_eq_of_lt (by have := hWin.1; omega), Nat.mod_eq_of_lt (by have := hWin.1; omega)] + omega + set b : U64 := wa frame ⟨BitVec.ofNat 64 (8 * (64 + i.val))⟩ + have hb : b ∈ Ub := hWin.2 (64 + i.val) (by omega) + have hsp_b : specPtr b = bufAddr (specPtr frame) (64 + i.val) := + specPtr_wa_wordA (by have := hWin.1; omega) + have X2 := X1.ub_wr hU hCs (hcpu1 ▸ hcpu) hfa1 hsub hIdle1 hb w + rw [hwa_eq] + have hstep_spec : NonDet.mapMx stepSpec (pairs.take (i.val + 1)) σ = + ({((), setUm σ1 (storeMem σ1.machineState.underlyingMemory (specPtr b) w.bv))}, False) := by + have htake : pairs.take (i.val + 1) = pairs.take i.val ++ [(i.val, idxRegA i.val)] := by + rw [List.take_succ_eq_append_getElem (by omega)] + congr 1; congr 1 + simp only [pairs, List.getElem_zip, List.getElem_range, fl4UserRegs_getElemA i.val hilt] + rw [htake, mapMx_appendA, nd_bind_oneSc hspec1] + unfold NonDet.mapMx + change (stepSpec (i.val, idxRegA i.val) >>= fun _ => NonDet.pure ()) σ1 = _ + have h1 : stepSpec (i.val, idxRegA i.val) σ1 = + ({((), setUm σ1 (storeMem σ1.machineState.underlyingMemory (specPtr b) w.bv))}, False) := by + dsimp only [stepSpec] + have hal_g := al3_of_frameWinA X1.fmem hWin (64 + i.val) (by omega) + have huf_g := inUserFrame_of_kheap hkh1 (huf (64 + i.val) (by omega)) + by_cases hnz : t_ptr = 0#u64 + · have hbnz : (t_ptr != 0#u64) = false := by simp [hnz] + have hv0 : v.vcpuTcb = none := by rw [hvtcb, hnz]; rfl + rw [hv0]; dsimp only + change (NonDet.pure 0 >>= _) σ1 = _ + rw [nd_bind_oneSc (m := NonDet.pure 0) rfl, storeWordOffs_eqA _ _ _ _ hal_g huf_g, hsp_b] + simp [w, hbnz] + · have hbnz : (t_ptr != 0#u64) = true := bne_iff_ne.2 hnz + have hv1 : v.vcpuTcb = some (specPtr t_ptr) := by rw [hvtcb]; unfold optRefA; simp [hnz] + obtain ⟨ht_in, -⟩ := X.vcpu.bind_v2t vc hvc t_ptr rfl hnz + obtain ⟨xt, hkt, -⟩ := X1.j.1.sch.tcb t_ptr ht_in + rw [hv1]; dsimp only + rw [nd_bind_oneSc (asUser_getReg_eqA hkt (idxRegA i.val)), storeWordOffs_eqA _ _ _ _ hal_g huf_g, hsp_b] + have hi_eq : i = regIdxA (idxRegA i.val) := by + rw [regIdxA_idxRegA i.val hilt] + exact UScalar.eq_of_val_eq (by + change i.val = (BitVec.ofNat 64 i.val).toNat + rw [BitVec.toNat_ofNat]; exact (Nat.mod_eq_of_lt (by omega)).symm) + have hreg_eq : (L.mem s1 (Endp.regA t_ptr i)).bv = xt.tcbArch.tcbContext.userRegs (idxRegA i.val) := by + have h1 := X1.regs t_ptr ht_in xt hkt (idxRegA i.val) + rwa [← hi_eq] at h1 + simp [w, hbnz, hreg_eq] + rw [nd_bind_oneSc h1]; rfl + have hi1_val : (⟨BitVec.ofNat 64 (i.val + 1#u64.val)⟩ : U64).val = i.val + 1 := by + change (BitVec.ofNat 64 (i.val + 1)).toNat = i.val + 1 + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + have hba : Aligned b := X1.fmem.aligned b hb + have hfa2 : ∀ q, FootA (L := L) Z (L.wr s1 b w) q → Aligned q := fun q hq => + hfa1 q (by simpa [FootA_wr_eqA] using hq) + have hIdle2 : ∀ c ∈ Z.Cs, + L.mem (L.wr s1 b w) (L.nodeAt (L.wr s1 b w) c layout.node.IDLE_THREAD) ∈ Z.T := by + intro c hc + have hne_idle : L.nodeAt s1 c layout.node.IDLE_THREAD ≠ b := fun e => + not_footA_of_ubA X1.vcpu.place X1.fmem hU X1.j.1.ksFit hCs (hcpu1 ▸ hcpu) hb + (e ▸ FootA.nslot _ (FootNA.node c _ hc (Or.inr (Or.inl rfl)))) + have hal_idle : Aligned (L.nodeAt s1 c layout.node.IDLE_THREAD) := + hfa1 _ (FootA.nslot _ (FootNA.node c _ hc (Or.inr (Or.inl rfl)))) + rw [nodeAt_wrA, mem_wr_neA hba hal_idle hne_idle.symm]; exact hIdle1 c hc + refine ⟨⟨by rw [hi1_val]; omega, _, by rw [hi1_val]; exact hstep_spec, X2, hkh1, + by rw [L.ksb_wr]; exact hksb1, by rw [L.cpu_wr]; exact hcpu1, hfa2, hIdle2⟩, by dsimp only; rw [hi1_val]; omega⟩ + · rw [if_neg hi37, WP.spec_ok] + have hieq : i.val = 37 := by have : ¬ i.val < layout.reg.COUNT.val := hi37; omega + dsimp only [Post] + refine ⟨σ1, ?_, X1, hkh1, hksb1, hcpu1, hfa1, hIdle1⟩ + rw [hieq, ← hpairs_len, List.take_length] at hspec1; exact hspec1 + +/-- Helper: single frame-word store refinement step. -/ +private theorem frame_store_oneA {AR : A → HwRootA → Prop} {Z : XSetsA} {OS Ub : Set U64} + {m : Fl4SpecA64.McState} {a : A} {s : S} {σ : State Unit} + (X : VcpuXferStateA (L := L) AR Z OS Ub m a s σ) + (hU : UbFootDisjA Z OS Ub) + (hCs : ∀ c ∈ Z.Cs, c.val < 128) (hcpu : (L.cpu s).val < 128) + (hfa : ∀ q, FootA (L := L) Z s q → Aligned q) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hIdleT : ∀ c ∈ Z.Cs, L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) ∈ Z.T) + {frame : U64} (hWin : FrameWinA Ub frame) + (huf : ∀ k < 128, inUserFrame (bufAddr (specPtr frame) k) σ) {k : Nat} (hk : k < 128) (w : U64) : + let b := wa frame ⟨BitVec.ofNat 64 (8 * k)⟩ + let σ' := setUm σ (storeMem σ.machineState.underlyingMemory (bufAddr (specPtr frame) k) w.bv) + storeWordOffs (Z := Unit) (specPtr frame) k w.bv σ = ({((), σ')}, False) ∧ + VcpuXferStateA (L := L) AR Z OS Ub m a (L.wr s b w) σ' ∧ + σ'.kheap = σ.kheap ∧ L.ksb (L.wr s b w) = L.ksb s ∧ L.cpu (L.wr s b w) = L.cpu s ∧ + (∀ q, FootA (L := L) Z (L.wr s b w) q → Aligned q) ∧ + (∀ c ∈ Z.Cs, L.mem (L.wr s b w) (L.nodeAt (L.wr s b w) c layout.node.IDLE_THREAD) ∈ Z.T) := by + dsimp only + set b : U64 := wa frame ⟨BitVec.ofNat 64 (8 * k)⟩ + have hb : b ∈ Ub := hWin.2 k hk + have hsp : specPtr b = bufAddr (specPtr frame) k := specPtr_wa_wordA (by have := hWin.1; omega) + have hba : Aligned b := X.fmem.aligned b hb + have X' := X.ub_wr hU hCs hcpu hfa hsub hIdleT hb w + rw [hsp] at X' + refine ⟨storeWordOffs_eqA (specPtr frame) k w.bv σ (al3_of_frameWinA X.fmem hWin k hk) (huf k hk), + X', rfl, L.ksb_wr _ _ _, L.cpu_wr _ _ _, + fun q hq => hfa q (by simpa [FootA_wr_eqA] using hq), fun c hc => ?_⟩ + have hne_idle : L.nodeAt s c layout.node.IDLE_THREAD ≠ b := fun e => + not_footA_of_ubA X.vcpu.place X.fmem hU X.j.1.ksFit hCs hcpu hb + (e ▸ FootA.nslot _ (FootNA.node c _ hc (Or.inr (Or.inl rfl)))) + have hal_idle : Aligned (L.nodeAt s c layout.node.IDLE_THREAD) := + hfa _ (FootA.nslot _ (FootNA.node c _ hc (Or.inr (Or.inl rfl)))) + rw [nodeAt_wrA, mem_wr_neA hba hal_idle hne_idle.symm]; exact hIdleT c hc + +/-- **`vcpu.read_state_to_frame` refines `fl4ReadStateToFrame`** across all 6 mask bits +(`EL1_SYSREGS`, `HYP`, `TIMER`, `VGIC` / `VGIC_LR`, `GPRS`) and `mask = 0`, preserving +`VcpuXferStateA`. Reused by both label 67 (`vcpu_read_state_jA`) and W2b (`complete_run_refinesA`). -/ +theorem read_state_to_frame_refinesA {AR : A → HwRootA → Prop} {Z : XSetsA} {OS Ub : Set U64} + {m : Fl4SpecA64.McState} {a : A} {s : S} {σ : State Unit} + (X : VcpuXferStateA (L := L) AR Z OS Ub m a s σ) + (hP : VcpuPlatAgreeA ar) + (hU : UbFootDisjA Z OS Ub) + (hCs : ∀ c ∈ Z.Cs, c.val < 128) (hcpu : (L.cpu s).val < 128) + (hfa : ∀ q, FootA (L := L) Z s q → Aligned q) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hIdleT : ∀ c ∈ Z.Cs, L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) ∈ Z.T) + {vc frame mask : U64} (hvc : vc ∈ Z.V) (hWin : FrameWinA Ub frame) + (huf : ∀ k < 128, inUserFrame (bufAddr (specPtr frame) k) σ) : + vcpu.read_state_to_frame st ar s a vc frame mask ⦃ s' => + ∃ σ', fl4ReadStateToFrame (specPtr vc) (specPtr frame) mask.bv σ = ({((), σ')}, False) ∧ + VcpuXferStateA (L := L) AR Z OS Ub m a s' σ' ∧ + σ'.kheap = σ.kheap ∧ L.ksb s' = L.ksb s ∧ L.cpu s' = L.cpu s ∧ + (∀ q, FootA (L := L) Z s' q → Aligned q) ∧ + (∀ c ∈ Z.Cs, L.mem s' (L.nodeAt s' c layout.node.IDLE_THREAD) ∈ Z.T) ⦄ := by + unfold vcpu.read_state_to_frame + by_cases hm0 : mask = 0#u64 + · rw [if_pos hm0, WP.spec_ok] + refine ⟨σ, ?_, X, rfl, rfl, rfl, hfa, hIdleT⟩ + subst hm0 + unfold fl4ReadStateToFrame NonDet.when smEl1 smHyp smTimer smVgic smVgicLr smGprs + have hz : ∀ c : Word, (0 : Word) &&& c = 0 := fun _ => BitVec.zero_and + rw [show U64.bv 0#u64 = (0 : Word) from rfl] + simp only [hz, ne_eq, not_true_eq_false, ↓reduceIte] + rw [nd_bind_oneSc (m := NonDet.pure ()) rfl, nd_bind_oneSc (m := NonDet.pure ()) rfl, + nd_bind_oneSc (m := NonDet.pure ()) rfl, nd_bind_oneSc (m := NonDet.pure ()) rfl]; rfl + · rw [if_neg hm0] + -- Helper to run the trailing GPRS block (identical across loop2, loop4, loop5) + have hGprs_step : ∀ (sG : S) (σG : State Unit) + (XG : VcpuXferStateA (L := L) AR Z OS Ub m a sG σG) + (hkhG : σG.kheap = σ.kheap) (hksbG : L.ksb sG = L.ksb s) (hcpuG : L.cpu sG = L.cpu s) + (hfaG : ∀ q, FootA (L := L) Z sG q → Aligned q) + (hIdleG : ∀ c ∈ Z.Cs, L.mem sG (L.nodeAt sG c layout.node.IDLE_THREAD) ∈ Z.T), + (do + let i13 ← fl4_abi.consts.vcpu_state_mask.GPRS + let i14 ← lift (mask &&& i13) + if i14 != 0#u64 then do + let i15 ← 64#u64 * 8#u64 + let gprs_base ← lift (wa frame i15) + let i16 ← lift (wa vc vcpu.TCB) + let t_ptr ← st.read sG i16 + vcpu.read_state_to_frame_loop2 st sG gprs_base t_ptr 0#u64 + else ok sG) ⦃ s' => + ∃ σ', (NonDet.when (mask.bv &&& smGprs ≠ 0) do + let v ← getVcpu (specPtr vc) + NonDet.mapMx (fun (i, r) => do + let w ← match v.vcpuTcb with | some t => asUser t (getRegister r) | none => pure 0 + storeWordOffs (specPtr frame) (64 + i) w) + (List.range fl4UserRegs.length |>.zip fl4UserRegs)) σG = ({((), σ')}, False) ∧ + VcpuXferStateA (L := L) AR Z OS Ub m a s' σ' ∧ + σ'.kheap = σ.kheap ∧ L.ksb s' = L.ksb s ∧ L.cpu s' = L.cpu s ∧ + (∀ q, FootA (L := L) Z s' q → Aligned q) ∧ + (∀ c ∈ Z.Cs, L.mem s' (L.nodeAt s' c layout.node.IDLE_THREAD) ∈ Z.T) ⦄ := by + intro sG σG XG hkhG hksbG hcpuG hfaG hIdleG + rw [sm_gprs_ok, bind_ok, lift_ok, bind_ok] + by_cases hg : (mask &&& 16#u64 != 0#u64) = true + · have hgb : mask.bv &&& smGprs ≠ 0 := fun e => by + have : mask &&& 16#u64 = 0#u64 := u64_eq_of_bv_eq e + simp [this] at hg + rw [if_pos hg, u64_mul_ok (by decide), bind_ok, lift_ok, bind_ok, lift_ok, bind_ok, + L.read_eq, bind_ok] + have hwa0 : wa vc vcpu.TCB = vc := by + apply UScalar.eq_of_val_eq; rw [wa_valP]; unfold vcpu.TCB; simp [UScalar.hBounds] + rw [hwa0] + obtain ⟨v, hkv, -, -⟩ := XG.vcpu.core.obj vc hvc + refine WP.spec_mono (read_gprs_loop_refinesA XG hU hCs (hcpuG ▸ hcpu) hfaG hsub hIdleG hvc hWin + (fun k hk => inUserFrame_of_kheap hkhG (huf k hk)) hkv) ?_ + rintro s' ⟨σ', hsp', X', hkh', hksb', hcpu', hfa', hIdle'⟩ + refine ⟨σ', ?_, X', hkh'.trans hkhG, hksb'.trans hksbG, hcpu'.trans hcpuG, hfa', hIdle'⟩ + unfold NonDet.when; rw [if_pos hgb, nd_bind_oneSc (getVcpu_eqA hkv)]; exact hsp' + · have hgb : ¬ (mask.bv &&& smGprs ≠ 0) := by + intro hne; apply hg; exact bne_iff_ne.2 (fun e => hne (congrArg UScalar.bv e)) + rw [if_neg hg, WP.spec_ok] + refine ⟨σG, ?_, XG, hkhG, hksbG, hcpuG, hfaG, hIdleG⟩ + unfold NonDet.when; rw [if_neg hgb]; rfl + -- Step 1: EL1_SYSREGS + have hStep1 : (do + let i ← fl4_abi.consts.vcpu_state_mask.EL1_SYSREGS + let i1 ← lift (mask &&& i) + if i1 != 0#u64 then do + let s2 ← vcpu.copy_reg_range_to_frame st s vc frame 0#u64 fl4_abi.consts.vcpu_reg.CSSELR_EL1 + vcpu.copy_reg_range_to_frame st s2 vc frame fl4_abi.consts.vcpu_reg.ACTLR_EL1 fl4_abi.consts.vcpu_reg.MDCCINT_EL1 + else ok s) ⦃ s1 => + ∃ σ1, (NonDet.when (mask.bv &&& smEl1 ≠ 0) do + copyRegsToFrame (specPtr vc) (specPtr frame) (idxRange 0 vrCsselr) + copyRegsToFrame (specPtr vc) (specPtr frame) (idxRange vrActlr vrMdccint)) σ = ({((), σ1)}, False) ∧ + VcpuXferStateA (L := L) AR Z OS Ub m a s1 σ1 ∧ + σ1.kheap = σ.kheap ∧ L.ksb s1 = L.ksb s ∧ L.cpu s1 = L.cpu s ∧ + (∀ q, FootA (L := L) Z s1 q → Aligned q) ∧ + (∀ c ∈ Z.Cs, L.mem s1 (L.nodeAt s1 c layout.node.IDLE_THREAD) ∈ Z.T) ⦄ := by + rw [sm_el1_ok, bind_ok, lift_ok, bind_ok] + by_cases h1 : (mask &&& 1#u64 != 0#u64) = true + · have h1b : mask.bv &&& smEl1 ≠ 0 := fun e => by + have : mask &&& 1#u64 = 0#u64 := u64_eq_of_bv_eq e; simp [this] at h1 + rw [if_pos h1] + have hcs : fl4_abi.consts.vcpu_reg.CSSELR_EL1.val = vrCsselr := by + unfold fl4_abi.consts.vcpu_reg.CSSELR_EL1 vrCsselr; rfl + have hact : fl4_abi.consts.vcpu_reg.ACTLR_EL1.val = vrActlr := by + unfold fl4_abi.consts.vcpu_reg.ACTLR_EL1 vrActlr; rfl + have hmdc : fl4_abi.consts.vcpu_reg.MDCCINT_EL1.val = vrMdccint := by + unfold fl4_abi.consts.vcpu_reg.MDCCINT_EL1 vrMdccint; rfl + refine WP.spec_bind (copy_reg_range_to_frame_refinesA X hU hCs hcpu hfa hsub hIdleT hvc hWin huf + (by rw [hcs]; unfold vrCsselr; decide) (by rw [hcs]; unfold vrCsselr; decide)) ?_ + rintro s2 ⟨σ2, hsp2, X2, hkh2, hksb2, hcpu2, hfa2, hIdle2⟩ + refine WP.spec_mono (copy_reg_range_to_frame_refinesA X2 hU hCs (hcpu2 ▸ hcpu) hfa2 hsub hIdle2 hvc hWin + (fun k hk => inUserFrame_of_kheap hkh2 (huf k hk)) + (by rw [hact, hmdc]; unfold vrActlr vrMdccint; decide) (by rw [hmdc]; unfold vrMdccint; decide)) ?_ + rintro s1 ⟨σ1, hsp1, X1, hkh1, hksb1, hcpu1, hfa1, hIdle1⟩ + refine ⟨σ1, ?_, X1, hkh1.trans hkh2, hksb1.trans hksb2, hcpu1.trans hcpu2, hfa1, hIdle1⟩ + unfold NonDet.when; rw [if_pos h1b] + rw [show (0#u64 : U64).val = 0 from rfl, hcs] at hsp2; rw [hact, hmdc] at hsp1 + rw [nd_bind_oneSc hsp2]; exact hsp1 + · have h1b : ¬ (mask.bv &&& smEl1 ≠ 0) := fun hne => h1 (bne_iff_ne.2 (fun e => hne (congrArg UScalar.bv e))) + rw [if_neg h1, WP.spec_ok] + exact ⟨σ, by unfold NonDet.when; rw [if_neg h1b]; rfl, X, rfl, rfl, rfl, hfa, hIdleT⟩ + -- Bind Step 1 -> Step 2 (HYP) -> Step 3 (TIMER) -> Step 4 (VGIC / VGIC_LR / GPRS) + rw [sm_el1_ok, bind_ok, lift_ok, bind_ok] at hStep1 ⊢ + refine WP.spec_bind hStep1 ?_ + rintro s1 ⟨σ1, hsp1, X1, hkh1, hksb1, hcpu1, hfa1, hIdle1⟩ + -- Step 2: HYP + have hStep2 : (do + let i2 ← fl4_abi.consts.vcpu_state_mask.HYP + let i3 ← lift (mask &&& i2) + if i3 != 0#u64 then do + let i4 ← fl4_abi.consts.vcpu_reg.VMPIDR_EL2 * 8#u64 + let i5 ← lift (wa frame i4) + let i6 ← vcpu.reg_get st s1 vc fl4_abi.consts.vcpu_reg.VMPIDR_EL2 + let s3 ← st.write s1 i5 i6 + let i7 ← fl4_abi.consts.vcpu_reg.VDISR_EL2 * 8#u64 + let i8 ← lift (wa frame i7) + let i9 ← vcpu.reg_get st s3 vc fl4_abi.consts.vcpu_reg.VDISR_EL2 + let s4 ← st.write s3 i8 i9 + let s5 ← vcpu.copy_reg_range_to_frame st s4 vc frame fl4_abi.consts.vcpu_reg.VPIDR_EL2 fl4_abi.consts.vcpu_reg.VSESR_EL2 + vcpu.copy_reg_range_to_frame st s5 vc frame fl4_abi.consts.vcpu_reg.FAR_EL2 fl4_abi.consts.vcpu_reg.ESR_EL2 + else ok s1) ⦃ s2 => + ∃ σ2, (NonDet.when (mask.bv &&& smHyp ≠ 0) do + copyRegsToFrame (specPtr vc) (specPtr frame) [vrVmpidr, vrVdisr] + copyRegsToFrame (specPtr vc) (specPtr frame) (idxRange vrVpidr vrVsesr) + copyRegsToFrame (specPtr vc) (specPtr frame) (idxRange vrFarEl2 vrEsrEl2)) σ1 = ({((), σ2)}, False) ∧ + VcpuXferStateA (L := L) AR Z OS Ub m a s2 σ2 ∧ + σ2.kheap = σ.kheap ∧ L.ksb s2 = L.ksb s ∧ L.cpu s2 = L.cpu s ∧ + (∀ q, FootA (L := L) Z s2 q → Aligned q) ∧ + (∀ c ∈ Z.Cs, L.mem s2 (L.nodeAt s2 c layout.node.IDLE_THREAD) ∈ Z.T) ⦄ := by + rw [sm_hyp_ok, bind_ok, lift_ok, bind_ok] + by_cases h2 : (mask &&& 2#u64 != 0#u64) = true + · have h2b : mask.bv &&& smHyp ≠ 0 := fun e => by + have : mask &&& 2#u64 = 0#u64 := u64_eq_of_bv_eq e; simp [this] at h2 + rw [if_pos h2] + have hvmp : fl4_abi.consts.vcpu_reg.VMPIDR_EL2.val = vrVmpidr := by + unfold fl4_abi.consts.vcpu_reg.VMPIDR_EL2 vrVmpidr; rfl + have hvdi : fl4_abi.consts.vcpu_reg.VDISR_EL2.val = vrVdisr := by + unfold fl4_abi.consts.vcpu_reg.VDISR_EL2 vrVdisr; rfl + have hvpi : fl4_abi.consts.vcpu_reg.VPIDR_EL2.val = vrVpidr := by + unfold fl4_abi.consts.vcpu_reg.VPIDR_EL2 vrVpidr; rfl + have hvse : fl4_abi.consts.vcpu_reg.VSESR_EL2.val = vrVsesr := by + unfold fl4_abi.consts.vcpu_reg.VSESR_EL2 vrVsesr; rfl + have hfar : fl4_abi.consts.vcpu_reg.FAR_EL2.val = vrFarEl2 := by + unfold fl4_abi.consts.vcpu_reg.FAR_EL2 vrFarEl2; rfl + have hesr : fl4_abi.consts.vcpu_reg.ESR_EL2.val = vrEsrEl2 := by + unfold fl4_abi.consts.vcpu_reg.ESR_EL2 vrEsrEl2; rfl + rw [u64_mul_ok (by rw [hvmp]; unfold vrVmpidr; decide), bind_ok, lift_ok, bind_ok, + vcpu_reg_get_u64_eqA s1 vc _ (by rw [hvmp]; unfold vrVmpidr; decide), bind_ok, L.write_eq, bind_ok] + obtain ⟨v, hkv1, -, -⟩ := X1.vcpu.core.obj vc hvc + have hm1 : (⟨BitVec.ofNat 64 (fl4_abi.consts.vcpu_reg.VMPIDR_EL2.val * 8#u64.val)⟩ : U64) = + ⟨BitVec.ofNat 64 (8 * vrVmpidr)⟩ := by congr 1; rw [hvmp]; unfold vrVmpidr; rfl + rw [hm1, hvmp] + obtain ⟨hst_vmp, Xs3, hkhs3, hksbs3, hcpus3, hfas3, hIdles3⟩ := + frame_store_oneA X1 hU hCs (hcpu1 ▸ hcpu) hfa1 hsub hIdle1 hWin + (fun k hk => inUserFrame_of_kheap hkh1 (huf k hk)) (k := vrVmpidr) (by unfold vrVmpidr; decide) + (L.mem s1 (vcpuRegWA vc vrVmpidr)) + set s3 := L.wr s1 (wa frame ⟨BitVec.ofNat 64 (8 * vrVmpidr)⟩) (L.mem s1 (vcpuRegWA vc vrVmpidr)) + set σs3 := setUm σ1 (storeMem σ1.machineState.underlyingMemory (bufAddr (specPtr frame) vrVmpidr) + (L.mem s1 (vcpuRegWA vc vrVmpidr)).bv) + rw [u64_mul_ok (by rw [hvdi]; unfold vrVdisr; decide), bind_ok, lift_ok, bind_ok, + vcpu_reg_get_u64_eqA s3 vc _ (by rw [hvdi]; unfold vrVdisr; decide), bind_ok, L.write_eq, bind_ok] + have hm2 : (⟨BitVec.ofNat 64 (fl4_abi.consts.vcpu_reg.VDISR_EL2.val * 8#u64.val)⟩ : U64) = + ⟨BitVec.ofNat 64 (8 * vrVdisr)⟩ := by congr 1; rw [hvdi]; unfold vrVdisr; rfl + rw [hm2, hvdi] + obtain ⟨hst_vdi, Xs4, hkhs4, hksbs4, hcpus4, hfas4, hIdles4⟩ := + frame_store_oneA Xs3 hU hCs (hcpus3.trans hcpu1 ▸ hcpu) hfas3 hsub hIdles3 hWin + (fun k hk => inUserFrame_of_kheap (hkhs3.trans hkh1) (huf k hk)) (k := vrVdisr) (by unfold vrVdisr; decide) + (L.mem s3 (vcpuRegWA vc vrVdisr)) + set s4 := L.wr s3 (wa frame ⟨BitVec.ofNat 64 (8 * vrVdisr)⟩) (L.mem s3 (vcpuRegWA vc vrVdisr)) + set σs4 := setUm σs3 (storeMem σs3.machineState.underlyingMemory (bufAddr (specPtr frame) vrVdisr) + (L.mem s3 (vcpuRegWA vc vrVdisr)).bv) + have hkv3 : σs3.kheap (specPtr vc) = some (.archObj (.vcpu v)) := by rw [hkhs3]; exact hkv1 + have hreg_vmp : (L.mem s1 (vcpuRegWA vc vrVmpidr)).bv = fl4VcpuRegGet v vrVmpidr := + X1.vcpu.regs vc hvc v hkv1 vrVmpidr (by unfold vrVmpidr; decide) + have hreg_vdi : (L.mem s3 (vcpuRegWA vc vrVdisr)).bv = fl4VcpuRegGet v vrVdisr := + Xs3.vcpu.regs vc hvc v hkv3 vrVdisr (by unfold vrVdisr; decide) + have hpair_spec : copyRegsToFrame (specPtr vc) (specPtr frame) [vrVmpidr, vrVdisr] σ1 = ({((), σs4)}, False) := by + rw [copyRegsToFrame_consA _ _ vrVmpidr [vrVdisr] hkv1 + (al3_of_frameWinA X1.fmem hWin vrVmpidr (by unfold vrVmpidr; decide)) + (inUserFrame_of_kheap hkh1 (huf vrVmpidr (by unfold vrVmpidr; decide))), ← hreg_vmp, + copyRegsToFrame_consA _ _ vrVdisr [] hkv3 + (al3_of_frameWinA Xs3.fmem hWin vrVdisr (by unfold vrVdisr; decide)) + (inUserFrame_of_kheap (hkhs3.trans hkh1) (huf vrVdisr (by unfold vrVdisr; decide))), ← hreg_vdi] + simp [copyRegsToFrame, NonDet.mapMx]; rfl + refine WP.spec_bind (copy_reg_range_to_frame_refinesA Xs4 hU hCs (hcpus4.trans (hcpus3.trans hcpu1) ▸ hcpu) + hfas4 hsub hIdles4 hvc hWin (fun k hk => inUserFrame_of_kheap (hkhs4.trans (hkhs3.trans hkh1)) (huf k hk)) + (by rw [hvpi, hvse]; unfold vrVpidr vrVsesr; decide) + (by rw [hvse]; unfold vrVsesr; decide)) ?_ + rintro s5 ⟨σ5, hsp5, X5, hkh5, hksb5, hcpu5, hfa5, hIdle5⟩ + refine WP.spec_mono (copy_reg_range_to_frame_refinesA X5 hU hCs + (hcpu5.trans (hcpus4.trans (hcpus3.trans hcpu1)) ▸ hcpu) hfa5 hsub hIdle5 hvc hWin + (fun k hk => inUserFrame_of_kheap (hkh5.trans (hkhs4.trans (hkhs3.trans hkh1))) (huf k hk)) + (by rw [hfar, hesr]; unfold vrFarEl2 vrEsrEl2; decide) (by rw [hesr]; unfold vrEsrEl2; decide)) ?_ + rintro s2 ⟨σ2, hsp2, X2, hkh2, hksb2, hcpu2, hfa2, hIdle2⟩ + refine ⟨σ2, ?_, X2, + hkh2.trans (hkh5.trans (hkhs4.trans (hkhs3.trans hkh1))), + hksb2.trans (hksb5.trans (hksbs4.trans (hksbs3.trans hksb1))), + hcpu2.trans (hcpu5.trans (hcpus4.trans (hcpus3.trans hcpu1))), hfa2, hIdle2⟩ + unfold NonDet.when; rw [if_pos h2b, nd_bind_oneSc hpair_spec] + rw [hvpi, hvse] at hsp5; rw [hfar, hesr] at hsp2 + rw [nd_bind_oneSc hsp5]; exact hsp2 + · have h2b : ¬ (mask.bv &&& smHyp ≠ 0) := fun hne => h2 (bne_iff_ne.2 (fun e => hne (congrArg UScalar.bv e))) + rw [if_neg h2, WP.spec_ok] + exact ⟨σ1, by unfold NonDet.when; rw [if_neg h2b]; rfl, X1, hkh1, hksb1, hcpu1, hfa1, hIdle1⟩ + rw [sm_hyp_ok, bind_ok, lift_ok, bind_ok] at hStep2 ⊢ + refine WP.spec_bind hStep2 ?_ + rintro s2 ⟨σ2, hsp2, X2, hkh2, hksb2, hcpu2, hfa2, hIdle2⟩ + -- Step 3: TIMER + have hStep3 : (do + let i4 ← fl4_abi.consts.vcpu_state_mask.TIMER + let i5 ← lift (mask &&& i4) + if i5 != 0#u64 then do + let s4 ← vcpu.copy_reg_range_to_frame st s2 vc frame fl4_abi.consts.vcpu_reg.CNTV_CTL fl4_abi.consts.vcpu_reg.CNTKCTL_EL1 + let i6 ← 64#u64 + 37#u64 + let i7 ← i6 * 8#u64 + let vppi_addr ← lift (wa frame i7) + let i8 ← lift (wa vc vcpu.VPPI_MASKED) + let i9 ← st.read s4 i8 + st.write s4 vppi_addr i9 + else ok s2) ⦃ s3 => + ∃ σ3, (NonDet.when (mask.bv &&& smTimer ≠ 0) do + copyRegsToFrame (specPtr vc) (specPtr frame) (idxRange vrCntvCtl vrCntkctl) + let v ← getVcpu (specPtr vc) + storeWordOffs (specPtr frame) vppiWord (if v.vcpuVppiMasked .vppiEventIRQ_VTimer then 1 else 0)) σ2 = ({((), σ3)}, False) ∧ + VcpuXferStateA (L := L) AR Z OS Ub m a s3 σ3 ∧ + σ3.kheap = σ.kheap ∧ L.ksb s3 = L.ksb s ∧ L.cpu s3 = L.cpu s ∧ + (∀ q, FootA (L := L) Z s3 q → Aligned q) ∧ + (∀ c ∈ Z.Cs, L.mem s3 (L.nodeAt s3 c layout.node.IDLE_THREAD) ∈ Z.T) ⦄ := by + rw [sm_timer_ok, bind_ok, lift_ok, bind_ok] + by_cases h3 : (mask &&& 4#u64 != 0#u64) = true + · have h3b : mask.bv &&& smTimer ≠ 0 := fun e => by + have : mask &&& 4#u64 = 0#u64 := u64_eq_of_bv_eq e; simp [this] at h3 + rw [if_pos h3] + have hctl : fl4_abi.consts.vcpu_reg.CNTV_CTL.val = vrCntvCtl := by + unfold fl4_abi.consts.vcpu_reg.CNTV_CTL vrCntvCtl; rfl + have hkctl : fl4_abi.consts.vcpu_reg.CNTKCTL_EL1.val = vrCntkctl := by + unfold fl4_abi.consts.vcpu_reg.CNTKCTL_EL1 vrCntkctl; rfl + refine WP.spec_bind (copy_reg_range_to_frame_refinesA X2 hU hCs (hcpu2 ▸ hcpu) hfa2 hsub hIdle2 hvc hWin + (fun k hk => inUserFrame_of_kheap hkh2 (huf k hk)) + (by rw [hctl, hkctl]; unfold vrCntvCtl vrCntkctl; decide) (by rw [hkctl]; unfold vrCntkctl; decide)) ?_ + rintro s4 ⟨σ4, hsp4, X4, hkh4, hksb4, hcpu4, hfa4, hIdle4⟩ + rw [u64_add_ok (by decide), bind_ok, u64_mul_ok (by decide), bind_ok, lift_ok, bind_ok, + lift_ok, bind_ok, L.read_eq, bind_ok, L.write_eq, WP.spec_ok] + have hvppi_off : wa vc vcpu.VPPI_MASKED = wa vc 16#u64 := by unfold vcpu.VPPI_MASKED; rfl + rw [hvppi_off] + obtain ⟨v, hkv4, -, -⟩ := X4.vcpu.core.obj vc hvc + have hvppi_bv : (L.mem s4 (wa vc 16#u64)).bv = if v.vcpuVppiMasked .vppiEventIRQ_VTimer then 1 else 0 := by + have h := X4.vcpu.vppi vc hvc v hkv4 + cases hv : v.vcpuVppiMasked .vppiEventIRQ_VTimer <;> simp [hv] at h ⊢ <;> rw [h] <;> rfl + obtain ⟨hst_vp, X3, hkh3, hksb3, hcpu3, hfa3, hIdle3⟩ := + frame_store_oneA X4 hU hCs (hcpu4.trans hcpu2 ▸ hcpu) hfa4 hsub hIdle4 hWin + (fun k hk => inUserFrame_of_kheap (hkh4.trans hkh2) (huf k hk)) + (k := vppiWord) (by unfold vppiWord; decide) (L.mem s4 (wa vc 16#u64)) + refine ⟨_, ?_, X3, hkh3.trans (hkh4.trans hkh2), hksb3.trans (hksb4.trans hksb2), + hcpu3.trans (hcpu4.trans hcpu2), hfa3, hIdle3⟩ + unfold NonDet.when; rw [if_pos h3b] + rw [hctl, hkctl] at hsp4 + rw [nd_bind_oneSc hsp4, nd_bind_oneSc (getVcpu_eqA hkv4), ← hvppi_bv] + exact hst_vp + · have h3b : ¬ (mask.bv &&& smTimer ≠ 0) := fun hne => h3 (bne_iff_ne.2 (fun e => hne (congrArg UScalar.bv e))) + rw [if_neg h3, WP.spec_ok] + exact ⟨σ2, by unfold NonDet.when; rw [if_neg h3b]; rfl, X2, hkh2, hksb2, hcpu2, hfa2, hIdle2⟩ + rw [sm_timer_ok, bind_ok, lift_ok, bind_ok] at hStep3 ⊢ + refine WP.spec_bind hStep3 ?_ + rintro s3 ⟨σ3, hsp3, X3, hkh3, hksb3, hcpu3, hfa3, hIdle3⟩ + -- Step 4: VGIC / VGIC_LR / GPRS + rw [sm_vgic_ok, bind_ok, lift_ok, bind_ok] + by_cases h4 : (mask &&& 8#u64 != 0#u64) = true + · have h4b : mask.bv &&& smVgic ≠ 0 := fun e => by + have : mask &&& 8#u64 = 0#u64 := u64_eq_of_bv_eq e; simp [this] at h4 + rw [if_pos h4, vcpu_num_lrs_eqA hP a, bind_ok] + obtain ⟨g, hga, hgb⟩ := hP.gic a + rw [hga, bind_ok] + have hgvtr : fl4_abi.consts.vcpu_reg.VGIC_VTR.val = vrVgicVtr := by + unfold fl4_abi.consts.vcpu_reg.VGIC_VTR vrVgicVtr; rfl + have hvhcr : fl4_abi.consts.vcpu_reg.VGIC_HCR.val = vrVgicHcr := by + unfold fl4_abi.consts.vcpu_reg.VGIC_HCR vrVgicHcr; rfl + have hap03 : fl4_abi.consts.vcpu_reg.VGIC_AP0R3.val = vrVgicAp0r3 := by + unfold fl4_abi.consts.vcpu_reg.VGIC_AP0R3 vrVgicAp0r3; rfl + have hdec : (decide (g = 3#u64)) = fl4GicV3 := hgb + rw [u64_mul_ok (by rw [hgvtr]; unfold vrVgicVtr; decide), bind_ok, lift_ok, bind_ok, + hP.vtr a, bind_ok, hdec, vcpu_abi_vgic_vtr_eqA hP a, bind_ok, L.write_eq, bind_ok] + have hm_vtr : (⟨BitVec.ofNat 64 (fl4_abi.consts.vcpu_reg.VGIC_VTR.val * 8#u64.val)⟩ : U64) = + ⟨BitVec.ofNat 64 (8 * vrVgicVtr)⟩ := by congr 1; rw [hgvtr]; unfold vrVgicVtr; rfl + rw [hm_vtr] + obtain ⟨hst_vtr, Xs4, hkhs4, hksbs4, hcpus4, hfas4, hIdles4⟩ := + frame_store_oneA X3 hU hCs (hcpu3 ▸ hcpu) hfa3 hsub hIdle3 hWin + (fun k hk => inUserFrame_of_kheap hkh3 (huf k hk)) + (k := vrVgicVtr) (by unfold vrVgicVtr; decide) ⟨abiVgicVtr⟩ + refine WP.spec_bind (copy_reg_range_to_frame_refinesA Xs4 hU hCs (hcpus4.trans hcpu3 ▸ hcpu) + hfas4 hsub hIdles4 hvc hWin (fun k hk => inUserFrame_of_kheap (hkhs4.trans hkh3) (huf k hk)) + (by rw [hvhcr, hap03]; unfold vrVgicHcr vrVgicAp0r3; decide) + (by rw [hap03]; unfold vrVgicAp0r3; decide)) ?_ + rintro s5 ⟨σ5, hsp5, X5, hkh5, hksb5, hcpu5, hfa5, hIdle5⟩ + refine WP.spec_bind (read_ap1r_loop_refinesA X5 hU hCs (hcpu5.trans (hcpus4.trans hcpu3) ▸ hcpu) + hfa5 hsub hIdle5 hvc hWin (fun k hk => inUserFrame_of_kheap (hkh5.trans (hkhs4.trans hkh3)) (huf k hk))) ?_ + rintro s6 ⟨σ6, hsp6, X6, hkh6, hksb6, hcpu6, hfa6, hIdle6⟩ + refine WP.spec_bind (read_lrs_loop_refinesA X6 hU hCs + (hcpu6.trans (hcpu5.trans (hcpus4.trans hcpu3)) ▸ hcpu) hfa6 hsub hIdle6 hvc hWin + (fun k hk => inUserFrame_of_kheap (hkh6.trans (hkh5.trans (hkhs4.trans hkh3))) (huf k hk))) ?_ + rintro s7 ⟨σ7, hsp7, X7, hkh7, hksb7, hcpu7, hfa7, hIdle7⟩ + refine WP.spec_mono (hGprs_step s7 σ7 X7 + (hkh7.trans (hkh6.trans (hkh5.trans (hkhs4.trans hkh3)))) + (hksb7.trans (hksb6.trans (hksb5.trans (hksbs4.trans hksb3)))) + (hcpu7.trans (hcpu6.trans (hcpu5.trans (hcpus4.trans hcpu3)))) hfa7 hIdle7) ?_ + rintro s' ⟨σ', hsp', X', hkh', hksb', hcpu', hfa', hIdle'⟩ + refine ⟨σ', ?_, X', hkh', hksb', hcpu', hfa', hIdle'⟩ + unfold fl4ReadStateToFrame + rw [nd_bind_oneSc hsp1, nd_bind_oneSc hsp2, nd_bind_oneSc hsp3, if_pos h4b, + nd_bind_oneSc hst_vtr] + rw [hvhcr, hap03] at hsp5 + rw [nd_bind_oneSc hsp5, nd_bind_oneSc hsp6, nd_bind_oneSc hsp7]; exact hsp' + · have h4b : ¬ (mask.bv &&& smVgic ≠ 0) := fun hne => h4 (bne_iff_ne.2 (fun e => hne (congrArg UScalar.bv e))) + rw [if_neg h4, sm_vgic_lr_ok, bind_ok, lift_ok, bind_ok] + by_cases h5 : (mask &&& 32#u64 != 0#u64) = true + · have h5b : mask.bv &&& smVgicLr ≠ 0 := fun e => by + have : mask &&& 32#u64 = 0#u64 := u64_eq_of_bv_eq e; simp [this] at h5 + rw [if_pos h5, vcpu_num_lrs_eqA hP a, bind_ok] + have hvhcr : fl4_abi.consts.vcpu_reg.VGIC_HCR.val = vrVgicHcr := by + unfold fl4_abi.consts.vcpu_reg.VGIC_HCR vrVgicHcr; rfl + have helrsr : fl4_abi.consts.vcpu_reg.VGIC_ELRSR.val = vrVgicElrsr := by + unfold fl4_abi.consts.vcpu_reg.VGIC_ELRSR vrVgicElrsr; rfl + rw [u64_mul_ok (by rw [hvhcr]; unfold vrVgicHcr; decide), bind_ok, lift_ok, bind_ok, + vcpu_reg_get_u64_eqA s3 vc _ (by rw [hvhcr]; unfold vrVgicHcr; decide), bind_ok, L.write_eq, bind_ok] + obtain ⟨v, hkv3, -, -⟩ := X3.vcpu.core.obj vc hvc + have hm_hcr : (⟨BitVec.ofNat 64 (fl4_abi.consts.vcpu_reg.VGIC_HCR.val * 8#u64.val)⟩ : U64) = + ⟨BitVec.ofNat 64 (8 * vrVgicHcr)⟩ := by congr 1; rw [hvhcr]; unfold vrVgicHcr; rfl + rw [hm_hcr, hvhcr] + obtain ⟨hst_hcr, Xs4, hkhs4, hksbs4, hcpus4, hfas4, hIdles4⟩ := + frame_store_oneA X3 hU hCs (hcpu3 ▸ hcpu) hfa3 hsub hIdle3 hWin + (fun k hk => inUserFrame_of_kheap hkh3 (huf k hk)) + (k := vrVgicHcr) (by unfold vrVgicHcr; decide) (L.mem s3 (vcpuRegWA vc vrVgicHcr)) + set s4 := L.wr s3 (wa frame ⟨BitVec.ofNat 64 (8 * vrVgicHcr)⟩) (L.mem s3 (vcpuRegWA vc vrVgicHcr)) + set σs4 := setUm σ3 (storeMem σ3.machineState.underlyingMemory (bufAddr (specPtr frame) vrVgicHcr) + (L.mem s3 (vcpuRegWA vc vrVgicHcr)).bv) + rw [u64_mul_ok (by rw [helrsr]; unfold vrVgicElrsr; decide), bind_ok, lift_ok, bind_ok, + vcpu_reg_get_u64_eqA s4 vc _ (by rw [helrsr]; unfold vrVgicElrsr; decide), bind_ok, L.write_eq, bind_ok] + have hm_elr : (⟨BitVec.ofNat 64 (fl4_abi.consts.vcpu_reg.VGIC_ELRSR.val * 8#u64.val)⟩ : U64) = + ⟨BitVec.ofNat 64 (8 * vrVgicElrsr)⟩ := by congr 1; rw [helrsr]; unfold vrVgicElrsr; rfl + rw [hm_elr, helrsr] + obtain ⟨hst_elr, Xs5, hkhs5, hksbs5, hcpus5, hfas5, hIdles5⟩ := + frame_store_oneA Xs4 hU hCs (hcpus4.trans hcpu3 ▸ hcpu) hfas4 hsub hIdles4 hWin + (fun k hk => inUserFrame_of_kheap (hkhs4.trans hkh3) (huf k hk)) + (k := vrVgicElrsr) (by unfold vrVgicElrsr; decide) (L.mem s4 (vcpuRegWA vc vrVgicElrsr)) + set s5 := L.wr s4 (wa frame ⟨BitVec.ofNat 64 (8 * vrVgicElrsr)⟩) (L.mem s4 (vcpuRegWA vc vrVgicElrsr)) + set σs5 := setUm σs4 (storeMem σs4.machineState.underlyingMemory (bufAddr (specPtr frame) vrVgicElrsr) + (L.mem s4 (vcpuRegWA vc vrVgicElrsr)).bv) + have hkv4 : σs4.kheap (specPtr vc) = some (.archObj (.vcpu v)) := by rw [hkhs4]; exact hkv3 + have hreg_hcr : (L.mem s3 (vcpuRegWA vc vrVgicHcr)).bv = fl4VcpuRegGet v vrVgicHcr := + X3.vcpu.regs vc hvc v hkv3 vrVgicHcr (by unfold vrVgicHcr; decide) + have hreg_elr : (L.mem s4 (vcpuRegWA vc vrVgicElrsr)).bv = fl4VcpuRegGet v vrVgicElrsr := + Xs4.vcpu.regs vc hvc v hkv4 vrVgicElrsr (by unfold vrVgicElrsr; decide) + have hpair_spec : copyRegsToFrame (specPtr vc) (specPtr frame) [vrVgicHcr, vrVgicElrsr] σ3 = ({((), σs5)}, False) := by + rw [copyRegsToFrame_consA _ _ vrVgicHcr [vrVgicElrsr] hkv3 + (al3_of_frameWinA X3.fmem hWin vrVgicHcr (by unfold vrVgicHcr; decide)) + (inUserFrame_of_kheap hkh3 (huf vrVgicHcr (by unfold vrVgicHcr; decide))), ← hreg_hcr, + copyRegsToFrame_consA _ _ vrVgicElrsr [] hkv4 + (al3_of_frameWinA Xs4.fmem hWin vrVgicElrsr (by unfold vrVgicElrsr; decide)) + (inUserFrame_of_kheap (hkhs4.trans hkh3) (huf vrVgicElrsr (by unfold vrVgicElrsr; decide))), ← hreg_elr] + simp [copyRegsToFrame, NonDet.mapMx]; rfl + -- `loop3` is definitionally identical to `loop1`! + have hloop3 : vcpu.read_state_to_frame_loop3 st s5 vc frame ⟨BitVec.ofNat 64 fl4NumLrs⟩ 0#u64 = + vcpu.read_state_to_frame_loop1 st s5 vc frame ⟨BitVec.ofNat 64 fl4NumLrs⟩ 0#u64 := by + unfold vcpu.read_state_to_frame_loop3 vcpu.read_state_to_frame_loop1; congr 1 + rw [hloop3] + refine WP.spec_bind (read_lrs_loop_refinesA Xs5 hU hCs + (hcpus5.trans (hcpus4.trans hcpu3) ▸ hcpu) hfas5 hsub hIdles5 hvc hWin + (fun k hk => inUserFrame_of_kheap (hkhs5.trans (hkhs4.trans hkh3)) (huf k hk))) ?_ + rintro s6 ⟨σ6, hsp6, X6, hkh6, hksb6, hcpu6, hfa6, hIdle6⟩ + -- `loop4` is definitionally identical to `loop2`! + refine WP.spec_mono (hGprs_step s6 σ6 X6 + (hkh6.trans (hkhs5.trans (hkhs4.trans hkh3))) + (hksb6.trans (hksbs5.trans (hksbs4.trans hksb3))) + (hcpu6.trans (hcpus5.trans (hcpus4.trans hcpu3))) hfa6 hIdle6) ?_ + rintro s' ⟨σ', hsp', X', hkh', hksb', hcpu', hfa', hIdle'⟩ + refine ⟨σ', ?_, X', hkh', hksb', hcpu', hfa', hIdle'⟩ + unfold fl4ReadStateToFrame + rw [nd_bind_oneSc hsp1, nd_bind_oneSc hsp2, nd_bind_oneSc hsp3, if_neg h4b] + rw [nd_bind_oneSc (by unfold NonDet.when; rw [if_pos h5b, nd_bind_oneSc hpair_spec]; exact hsp6)] + exact hsp' + · have h5b : ¬ (mask.bv &&& smVgicLr ≠ 0) := fun hne => h5 (bne_iff_ne.2 (fun e => hne (congrArg UScalar.bv e))) + rw [if_neg h5] + -- `loop5` is definitionally identical to `loop2`! + refine WP.spec_mono (hGprs_step s3 σ3 X3 hkh3 hksb3 hcpu3 hfa3 hIdle3) ?_ + rintro s' ⟨σ', hsp', X', hkh', hksb', hcpu', hfa', hIdle'⟩ + refine ⟨σ', ?_, X', hkh', hksb', hcpu', hfa', hIdle'⟩ + unfold fl4ReadStateToFrame + rw [nd_bind_oneSc hsp1, nd_bind_oneSc hsp2, nd_bind_oneSc hsp3, if_neg h4b] + rw [nd_bind_oneSc (by unfold NonDet.when; rw [if_neg h5b]; rfl)] + exact hsp' + +end read_state + + +end +end Fl4ProofsA64.P3.Vcpu diff --git a/proofs/Fl4ProofsA64/P3/VcpuXferWriteJA.lean b/proofs/Fl4ProofsA64/P3/VcpuXferWriteJA.lean new file mode 100644 index 000000000..c3e3fba92 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/VcpuXferWriteJA.lean @@ -0,0 +1,1424 @@ +/- +P3 (aarch64, A64-P3 S4 W3): **VCPU write-state-from-frame refinement** (`fl4VcpuRegSet_comm`, +`vcpu.copy_reg_range_from_frame`, `vcpu.write_state_from_frame`). +-/ +import Fl4ProofsA64.P3.VcpuXferReadJA + +open Aeneas Aeneas.Std Result fl4_core_a64 + +namespace Fl4ProofsA64.P3.Vcpu + +open Fl4Spec Fl4SpecA64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P3 Fl4ProofsA64.P3.Sched Fl4ProofsA64.P3.Endp +open Fl4ProofsA64.P3.Notif + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## Section 3: `vcpu.copy_reg_range_from_frame` and `vcpu.write_state_from_frame` -/ + +section write_state +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + +private theorem fl4VcpuRegSet_lt25 {i : Nat} (hi : i < 25) (w : Word) (v : Vcpu) : + fl4VcpuRegSet i w v = { v with vcpuRegs := Function.update v.vcpuRegs (VCPUReg.toEnum i) w } := by + unfold fl4VcpuRegSet; rw [if_pos hi] + +private theorem fl4VcpuRegSet_hcr (w : Word) (v : Vcpu) : + fl4VcpuRegSet vrVgicHcr w v = { v with vcpuVgic := { v.vcpuVgic with vgicHcr := ucast 32 w } } := by + unfold fl4VcpuRegSet; rw [if_neg (by decide), if_pos rfl] + +private theorem fl4VcpuRegSet_vmcr (w : Word) (v : Vcpu) : + fl4VcpuRegSet vrVgicVmcr w v = + { v with vcpuVgic := { v.vcpuVgic with vgicVmcr := ucast 32 w }, + vcpuExt := { v.vcpuExt with regs := Function.update v.vcpuExt.regs ⟨vrVgicVmcr, by decide⟩ w } } := by + unfold fl4VcpuRegSet; rw [if_neg (by decide), if_neg (by decide), if_pos rfl] + +private theorem fl4VcpuRegSet_lr {i : Nat} (h1 : vrVgicLr0 ≤ i) (h2 : i < 64) (w : Word) (v : Vcpu) : + fl4VcpuRegSet i w v = + { v with vcpuVgic := { v.vcpuVgic with vgicLr := Function.update v.vcpuVgic.vgicLr (i - vrVgicLr0) w } } := by + unfold fl4VcpuRegSet + rw [if_neg (by unfold vrVgicLr0 at h1; omega), if_neg (by unfold vrVgicHcr vrVgicLr0 at *; omega), + if_neg (by unfold vrVgicVmcr vrVgicLr0 at *; omega), if_pos ⟨h1, h2⟩] + +private theorem fl4VcpuRegSet_ext {i : Nat} (h1 : 25 ≤ i) (h2 : i ≠ vrVgicHcr) (h3 : i ≠ vrVgicVmcr) + (h4 : ¬ vrVgicLr0 ≤ i) (h5 : i < 64) (w : Word) (v : Vcpu) : + fl4VcpuRegSet i w v = { v with vcpuExt := { v.vcpuExt with regs := Function.update v.vcpuExt.regs ⟨i, h5⟩ w } } := by + unfold fl4VcpuRegSet + rw [if_neg (by omega), if_neg h2, if_neg h3, if_neg (fun h => h4 h.1), dif_pos h5] + +/-- Disjoint register updates on `Vcpu` commute (`i ≠ j`, `i < 64`, `j < 64`). -/ +theorem fl4VcpuRegSet_comm {i j : Nat} (hi : i < 64) (hj : j < 64) (hne : i ≠ j) + (wi wj : Word) (v : Vcpu) : + fl4VcpuRegSet i wi (fl4VcpuRegSet j wj v) = fl4VcpuRegSet j wj (fl4VcpuRegSet i wi v) := by + have hcls : ∀ k, k < 64 → + k < 25 ∨ k = vrVgicHcr ∨ k = vrVgicVmcr ∨ (vrVgicLr0 ≤ k ∧ k < 64) ∨ + (25 ≤ k ∧ k ≠ vrVgicHcr ∧ k ≠ vrVgicVmcr ∧ ¬ vrVgicLr0 ≤ k) := by + intro k hk; unfold vrVgicHcr vrVgicVmcr vrVgicLr0; omega + have htoEnum_idx : ∀ a < 25, (VCPUReg.toEnum a).ctorIdx = a := by + intro a ha; interval_cases a <;> rfl + have htoEnum_ne : ∀ {a b : Nat}, a < 25 → b < 25 → a ≠ b → VCPUReg.toEnum a ≠ VCPUReg.toEnum b := by + intro a b ha hb hab heq + have := congrArg VCPUReg.ctorIdx heq + rw [htoEnum_idx a ha, htoEnum_idx b hb] at this; exact hab this + rcases hcls i hi with hi1 | rfl | rfl | ⟨hi4a, hi4b⟩ | ⟨hi5a, hi5b, hi5c, hi5d⟩ <;> + rcases hcls j hj with hj1 | rfl | rfl | ⟨hj4a, hj4b⟩ | ⟨hj5a, hj5b, hj5c, hj5d⟩ + · rw [fl4VcpuRegSet_lt25 hj1, fl4VcpuRegSet_lt25 hi1, fl4VcpuRegSet_lt25 hi1, fl4VcpuRegSet_lt25 hj1] + dsimp only; rw [Function.update_comm (htoEnum_ne hi1 hj1 hne)] + · rw [fl4VcpuRegSet_hcr, fl4VcpuRegSet_lt25 hi1, fl4VcpuRegSet_lt25 hi1, fl4VcpuRegSet_hcr] + · rw [fl4VcpuRegSet_vmcr, fl4VcpuRegSet_lt25 hi1, fl4VcpuRegSet_lt25 hi1, fl4VcpuRegSet_vmcr] + · rw [fl4VcpuRegSet_lr hj4a hj4b, fl4VcpuRegSet_lt25 hi1, fl4VcpuRegSet_lt25 hi1, fl4VcpuRegSet_lr hj4a hj4b] + · rw [fl4VcpuRegSet_ext hj5a hj5b hj5c hj5d hj, fl4VcpuRegSet_lt25 hi1, fl4VcpuRegSet_lt25 hi1, fl4VcpuRegSet_ext hj5a hj5b hj5c hj5d hj] + · rw [fl4VcpuRegSet_lt25 hj1, fl4VcpuRegSet_hcr, fl4VcpuRegSet_hcr, fl4VcpuRegSet_lt25 hj1] + · exact absurd rfl hne + · rw [fl4VcpuRegSet_vmcr, fl4VcpuRegSet_hcr, fl4VcpuRegSet_hcr, fl4VcpuRegSet_vmcr] + · rw [fl4VcpuRegSet_lr hj4a hj4b, fl4VcpuRegSet_hcr, fl4VcpuRegSet_hcr, fl4VcpuRegSet_lr hj4a hj4b] + · rw [fl4VcpuRegSet_ext hj5a hj5b hj5c hj5d hj, fl4VcpuRegSet_hcr, fl4VcpuRegSet_hcr, fl4VcpuRegSet_ext hj5a hj5b hj5c hj5d hj] + · rw [fl4VcpuRegSet_lt25 hj1, fl4VcpuRegSet_vmcr, fl4VcpuRegSet_vmcr, fl4VcpuRegSet_lt25 hj1] + · rw [fl4VcpuRegSet_hcr, fl4VcpuRegSet_vmcr, fl4VcpuRegSet_vmcr, fl4VcpuRegSet_hcr] + · exact absurd rfl hne + · rw [fl4VcpuRegSet_lr hj4a hj4b, fl4VcpuRegSet_vmcr, fl4VcpuRegSet_vmcr, fl4VcpuRegSet_lr hj4a hj4b] + · rw [fl4VcpuRegSet_ext hj5a hj5b hj5c hj5d hj, fl4VcpuRegSet_vmcr, fl4VcpuRegSet_vmcr, fl4VcpuRegSet_ext hj5a hj5b hj5c hj5d hj] + have hne_f : (⟨vrVgicVmcr, by decide⟩ : Fin 64) ≠ ⟨j, hj⟩ := fun e => hj5c (congrArg Fin.val e).symm + dsimp only; rw [Function.update_comm hne_f] + · rw [fl4VcpuRegSet_lt25 hj1, fl4VcpuRegSet_lr hi4a hi4b, fl4VcpuRegSet_lr hi4a hi4b, fl4VcpuRegSet_lt25 hj1] + · rw [fl4VcpuRegSet_hcr, fl4VcpuRegSet_lr hi4a hi4b, fl4VcpuRegSet_lr hi4a hi4b, fl4VcpuRegSet_hcr] + · rw [fl4VcpuRegSet_vmcr, fl4VcpuRegSet_lr hi4a hi4b, fl4VcpuRegSet_lr hi4a hi4b, fl4VcpuRegSet_vmcr] + · rw [fl4VcpuRegSet_lr hj4a hj4b, fl4VcpuRegSet_lr hi4a hi4b, fl4VcpuRegSet_lr hi4a hi4b, fl4VcpuRegSet_lr hj4a hj4b] + have hne_lr : i - vrVgicLr0 ≠ j - vrVgicLr0 := by omega + dsimp only; rw [Function.update_comm hne_lr] + · rw [fl4VcpuRegSet_ext hj5a hj5b hj5c hj5d hj, fl4VcpuRegSet_lr hi4a hi4b, fl4VcpuRegSet_lr hi4a hi4b, fl4VcpuRegSet_ext hj5a hj5b hj5c hj5d hj] + · rw [fl4VcpuRegSet_lt25 hj1, fl4VcpuRegSet_ext hi5a hi5b hi5c hi5d hi, fl4VcpuRegSet_ext hi5a hi5b hi5c hi5d hi, fl4VcpuRegSet_lt25 hj1] + · rw [fl4VcpuRegSet_hcr, fl4VcpuRegSet_ext hi5a hi5b hi5c hi5d hi, fl4VcpuRegSet_ext hi5a hi5b hi5c hi5d hi, fl4VcpuRegSet_hcr] + · rw [fl4VcpuRegSet_vmcr, fl4VcpuRegSet_ext hi5a hi5b hi5c hi5d hi, fl4VcpuRegSet_ext hi5a hi5b hi5c hi5d hi, fl4VcpuRegSet_vmcr] + have hne_f : (⟨i, hi⟩ : Fin 64) ≠ ⟨vrVgicVmcr, by decide⟩ := fun e => hi5c (congrArg Fin.val e) + dsimp only; rw [Function.update_comm hne_f] + · rw [fl4VcpuRegSet_lr hj4a hj4b, fl4VcpuRegSet_ext hi5a hi5b hi5c hi5d hi, fl4VcpuRegSet_ext hi5a hi5b hi5c hi5d hi, fl4VcpuRegSet_lr hj4a hj4b] + · rw [fl4VcpuRegSet_ext hj5a hj5b hj5c hj5d hj, fl4VcpuRegSet_ext hi5a hi5b hi5c hi5d hi, + fl4VcpuRegSet_ext hi5a hi5b hi5c hi5d hi, fl4VcpuRegSet_ext hj5a hj5b hj5c hj5d hj] + have hne_f : (⟨i, hi⟩ : Fin 64) ≠ ⟨j, hj⟩ := fun e => hne (congrArg Fin.val e) + dsimp only; rw [Function.update_comm hne_f] + +private theorem vcpu_reg_set_u64_eqA (s : S) (vc r : U64) (hr : r.val < 64) (w : U64) : + vcpu.reg_set st s vc r w = ok (L.wr s (vcpuRegWA vc r.val) w) := by + have h := vcpu_reg_set_eqA (L := L) s vc hr w + rwa [u64_ofNat_val] at h + +private theorem ucast_u32_eq_of_shr32A {w : Word} (h : w >>> 32 = 0) : + ucast 64 (ucast 32 w) = w := by + apply BitVec.eq_of_toNat_eq + have hshr : w.toNat / 2 ^ 32 = 0 := by + have h1 := congrArg BitVec.toNat h + simpa [BitVec.toNat_ushiftRight, Nat.shiftRight_eq_div_pow] using h1 + have hlt : w.toNat < 2 ^ 32 := Nat.lt_of_div_eq_zero (by decide) hshr + simp only [ucast, BitVec.toNat_setWidth, Nat.mod_eq_of_lt hlt, + Nat.mod_eq_of_lt (lt_trans hlt (by decide : 2 ^ 32 < 2 ^ 64))] + +private theorem vcpuRegWA_ne_hdrA {Z : XSetsA} {OS Ub : Set U64} {ksb : U64} + (hP : VcPlacedA Z OS Ub ksb) {vc : U64} (hvc : vc ∈ Z.V) {i : Nat} (hi : i < 64) + {q : U64} (hq : q ∈ Z.V) : + vcpuRegWA vc i ≠ q ∧ vcpuRegWA vc i ≠ wa q 24#u64 := by + have hne : ∀ o : U64, o.val < 64 → vcpuRegWA vc i ≠ wa q o := fun o ho => by + by_cases heq : q = vc + · subst heq + exact hP.same_vcpu_ne hq (by rw [vcpuRegOffA_val hi]; omega) (by omega) + (fun e => by have := congrArg UScalar.val e; rw [vcpuRegOffA_val hi] at this; omega) + · exact hP.disj_vcpu hvc hq (Ne.symm heq) (by rw [vcpuRegOffA_val hi]; omega) (by omega) + have h0 := hne 0#u64 (by decide) + have hwa0 : wa q 0#u64 = q := by apply UScalar.eq_of_val_eq; rw [wa_valP]; simp [UScalar.hBounds] + rw [hwa0] at h0 + exact ⟨h0, hne 24#u64 (by decide)⟩ + +private theorem vcpuHdr16_ne_hdrA {Z : XSetsA} {OS Ub : Set U64} {ksb : U64} + (hP : VcPlacedA Z OS Ub ksb) {vc : U64} (hvc : vc ∈ Z.V) {q : U64} (hq : q ∈ Z.V) : + wa vc 16#u64 ≠ q ∧ wa vc 16#u64 ≠ wa q 24#u64 := by + have hne : ∀ o : U64, o.val < 4096 → o ≠ 16#u64 → wa vc 16#u64 ≠ wa q o := fun o ho hno => by + by_cases heq : q = vc + · subst heq; exact hP.same_vcpu_ne hq (by decide) ho hno.symm + · exact hP.disj_vcpu hvc hq (Ne.symm heq) (by decide) ho + have h0 := hne 0#u64 (by decide) (by decide) + have hwa0 : wa q 0#u64 = q := by apply UScalar.eq_of_val_eq; rw [wa_valP]; simp [UScalar.hBounds] + rw [hwa0] at h0 + exact ⟨h0, hne 24#u64 (by decide) (by decide)⟩ + +/-- Helper: single VCPU register write on `VcpuXferStateA`. -/ +private theorem vcpu_reg_wr_oneA {AR : A → HwRootA → Prop} {Z : XSetsA} {OS Ub : Set U64} + {m : Fl4SpecA64.McState} {a : A} {s : S} {σ : State Unit} + (X : VcpuXferStateA (L := L) AR Z OS Ub m a s σ) + (hU : UbFootDisjA Z OS Ub) + (hCs : ∀ c ∈ Z.Cs, c.val < 128) (hcpu : (L.cpu s).val < 128) + (hExt : ∀ b, VcPage Z.V b → ¬ ExtFootE Z.X Z.Y Z.T2 b) + (hSw : ∀ b, VcPage Z.V b → ∀ y, SlotWA Z.th y → + b ≠ y ∧ b ≠ wa y 8#u64 ∧ b ≠ wa y 16#u64 ∧ b ≠ wa y 24#u64) + (hfa : ∀ q, FootA (L := L) Z s q → Aligned q) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hIdleT : ∀ c ∈ Z.Cs, L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) ∈ Z.T) + {vc : U64} (hvc : vc ∈ Z.V) {v : Vcpu} + (hkv : σ.kheap (specPtr vc) = some (.archObj (.vcpu v))) + {r : U64} (hr : r.val < 64) (w : U64) (hhcr : r.val = vrVgicHcr → w.bv >>> 32 = 0) : + let v' := fl4VcpuRegSet r.val w.bv v + let σ' := updKSc σ (specPtr vc) (.archObj (.vcpu v')) + vcpu.reg_set st s vc r w ⦃ s' => + fl4VcpuSet (specPtr vc) r.val w.bv σ = ({((), σ')}, False) ∧ + VcpuXferStateA (L := L) AR Z OS Ub m a s' σ' ∧ + σ'.machineState = σ.machineState ∧ L.ksb s' = L.ksb s ∧ L.cpu s' = L.cpu s ∧ + (∀ q, FootA (L := L) Z s' q → Aligned q) ∧ + (∀ c ∈ Z.Cs, L.mem s' (L.nodeAt s' c layout.node.IDLE_THREAD) ∈ Z.T) ⦄ := by + dsimp only + rw [vcpu_reg_set_u64_eqA s vc r hr w, WP.spec_ok] + set b : U64 := vcpuRegWA vc r.val + have hb_vc : VcPage Z.V b := vcpuRegWA_in_vcPage hvc hr + have hba : Aligned b := X.vcpu.place.vcPage_al hb_vc + have hnl : ∀ q ∈ Z.V, b ≠ q ∧ b ≠ wa q 24#u64 := fun q hq => + vcpuRegWA_ne_hdrA X.vcpu.place hvc hr hq + have hV' := VcpuRelA.upd_regA X.vcpu X.j.1.ksFit hCs hIdleT hvc hkv hr w + (fun h => ucast_u32_eq_of_shr32A (hhcr h)) + have hmeta := fl4VcpuRegSet_pres_metaA r.val w.bv v + have X' := X.vc_nonlink_wr hCs hcpu hExt hSw hfa hsub hU (writesIn_wrA s b w hba) + (fun q hq => by subst hq; exact ⟨hb_vc, hnl⟩) hvc hkv hmeta.1 hmeta.2.2.1 hV' + refine ⟨fl4VcpuSet_eqA r.val w.bv hkv, X', rfl, L.ksb_wr _ _ _, L.cpu_wr _ _ _, + fun q hq => hfa q ((FootA_wr_eqA Z s b w q).1 hq), fun c hc => ?_⟩ + have hne_idle : L.nodeAt s c layout.node.IDLE_THREAD ≠ b := fun e => + not_footA_of_vcNonLinkA X.vcpu.place X.j.1.ksFit hCs hcpu hExt hSw hb_vc hnl + (e ▸ FootA.nslot _ (FootNA.node c _ hc (Or.inr (Or.inl rfl)))) + have hal_idle : Aligned (L.nodeAt s c layout.node.IDLE_THREAD) := + hfa _ (FootA.nslot _ (FootNA.node c _ hc (Or.inr (Or.inl rfl)))) + rw [nodeAt_wrA, mem_wr_neA hba hal_idle hne_idle.symm]; exact hIdleT c hc + +/-- **`vcpu.copy_reg_range_from_frame` refines `copyRegsFromFrame`** for `start ≤ end_incl < 64` +when `vrVgicHcr` is not in `[start..end_incl]`. -/ +theorem copy_reg_range_from_frame_refinesA {AR : A → HwRootA → Prop} {Z : XSetsA} {OS Ub : Set U64} + {m : Fl4SpecA64.McState} {a : A} {s : S} {σ : State Unit} + (X : VcpuXferStateA (L := L) AR Z OS Ub m a s σ) + (hU : UbFootDisjA Z OS Ub) + (hCs : ∀ c ∈ Z.Cs, c.val < 128) (hcpu : (L.cpu s).val < 128) + (hExt : ∀ b, VcPage Z.V b → ¬ ExtFootE Z.X Z.Y Z.T2 b) + (hSw : ∀ b, VcPage Z.V b → ∀ y, SlotWA Z.th y → + b ≠ y ∧ b ≠ wa y 8#u64 ∧ b ≠ wa y 16#u64 ∧ b ≠ wa y 24#u64) + (hfa : ∀ q, FootA (L := L) Z s q → Aligned q) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hIdleT : ∀ c ∈ Z.Cs, L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) ∈ Z.T) + {vc frame start end_incl : U64} (hvc : vc ∈ Z.V) (hWin : FrameWinA Ub frame) + (hle : start.val ≤ end_incl.val) (hend : end_incl.val < 64) + (hnoHcr : ∀ k, start.val ≤ k → k ≤ end_incl.val → k ≠ vrVgicHcr) : + vcpu.copy_reg_range_from_frame st s vc frame start end_incl ⦃ s' => + ∃ σ', copyRegsFromFrame (specPtr vc) (specPtr frame) (idxRange start.val end_incl.val) σ = ({((), σ')}, False) ∧ + VcpuXferStateA (L := L) AR Z OS Ub m a s' σ' ∧ + σ'.machineState = σ.machineState ∧ L.ksb s' = L.ksb s ∧ L.cpu s' = L.cpu s ∧ + (∀ q, FootA (L := L) Z s' q → Aligned q) ∧ + (∀ c ∈ Z.Cs, L.mem s' (L.nodeAt s' c layout.node.IDLE_THREAD) ∈ Z.T) ⦄ := by + unfold vcpu.copy_reg_range_from_frame vcpu.copy_reg_range_from_frame_loop + let Inv : (S × U64) → Prop := fun (s1, r) => + start.val ≤ r.val ∧ r.val ≤ end_incl.val + 1 ∧ + ∃ σ1, copyRegsFromFrame (specPtr vc) (specPtr frame) (List.range' start.val (r.val - start.val)) σ = ({((), σ1)}, False) ∧ + VcpuXferStateA (L := L) AR Z OS Ub m a s1 σ1 ∧ + σ1.machineState = σ.machineState ∧ L.ksb s1 = L.ksb s ∧ L.cpu s1 = L.cpu s ∧ + (∀ q, FootA (L := L) Z s1 q → Aligned q) ∧ + (∀ c ∈ Z.Cs, L.mem s1 (L.nodeAt s1 c layout.node.IDLE_THREAD) ∈ Z.T) + let Post : S → Prop := fun s' => + ∃ σ', copyRegsFromFrame (specPtr vc) (specPtr frame) (idxRange start.val end_incl.val) σ = ({((), σ')}, False) ∧ + VcpuXferStateA (L := L) AR Z OS Ub m a s' σ' ∧ + σ'.machineState = σ.machineState ∧ L.ksb s' = L.ksb s ∧ L.cpu s' = L.cpu s ∧ + (∀ q, FootA (L := L) Z s' q → Aligned q) ∧ + (∀ c ∈ Z.Cs, L.mem s' (L.nodeAt s' c layout.node.IDLE_THREAD) ∈ Z.T) + apply loop.spec_decr_nat (measure := fun p => end_incl.val + 1 - p.2.val) (inv := Inv) (post := Post) + swap + · exact ⟨le_rfl, by omega, σ, by simp [copyRegsFromFrame, NonDet.mapMx]; rfl, X, rfl, rfl, rfl, hfa, hIdleT⟩ + rintro ⟨s1, r⟩ ⟨hsr, hre, σ1, hspec1, X1, hms1, hksb1, hcpu1, hfa1, hIdle1⟩ + dsimp only + unfold vcpu.copy_reg_range_from_frame_loop.body + by_cases hle_r : r ≤ end_incl + · have hr_lt : r.val < 64 := by have : r.val ≤ end_incl.val := hle_r; omega + have hle_val : r.val ≤ end_incl.val := hle_r + rw [if_pos hle_r, u64_mul_ok (by change r.val * 8 < 2 ^ 64; omega), bind_ok, lift_ok, bind_ok, L.read_eq, bind_ok] + have hmul_eq : (⟨BitVec.ofNat 64 (r.val * 8#u64.val)⟩ : U64) = ⟨BitVec.ofNat 64 (8 * r.val)⟩ := by + congr 1; congr 1; change r.val * 8 = 8 * r.val; omega + rw [hmul_eq] + set w : U64 := L.mem s1 (wa frame ⟨BitVec.ofNat 64 (8 * r.val)⟩) + obtain ⟨v, hkv1, -, -⟩ := X1.vcpu.core.obj vc hvc + have hw_bv : w.bv = loadWordAt σ1.machineState.underlyingMemory (bufAddr (specPtr frame) r.val) := by + have h1 := X1.fmem.agree _ (hWin.2 r.val (by omega)) + rw [specPtr_wa_wordA (by have := hWin.1; omega)] at h1; exact h1 + refine WP.spec_bind (vcpu_reg_wr_oneA X1 hU hCs (hcpu1 ▸ hcpu) hExt hSw hfa1 hsub hIdle1 hvc hkv1 hr_lt w + (fun h => absurd h (hnoHcr r.val hsr hle_val))) ?_ + rintro s2 ⟨hset2, X2, hms2, hksb2, hcpu2, hfa2, hIdle2⟩ + rw [u64_add_ok (by change r.val + 1 < 2 ^ 64; omega), bind_ok, WP.spec_ok] + have hr1_val : (⟨BitVec.ofNat 64 (r.val + 1#u64.val)⟩ : U64).val = r.val + 1 := by + change (BitVec.ofNat 64 (r.val + 1)).toNat = r.val + 1 + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + have hstep_spec : copyRegsFromFrame (specPtr vc) (specPtr frame) + (List.range' start.val (r.val + 1 - start.val)) σ = + ({((), updKSc σ1 (specPtr vc) (.archObj (.vcpu (fl4VcpuRegSet r.val w.bv v))))}, False) := by + have hrg : List.range' start.val (r.val + 1 - start.val) = + List.range' start.val (r.val - start.val) ++ [r.val] := by + have : r.val + 1 - start.val = (r.val - start.val) + 1 := by omega + rw [this, List.range'_concat]; congr 1; congr 1; simp only [Nat.one_mul]; omega + rw [hrg]; unfold copyRegsFromFrame at hspec1 ⊢ + rw [mapMx_appendA, nd_bind_oneSc hspec1] + unfold NonDet.mapMx + change ((do let w ← loadWordOffs (specPtr frame) r.val; fl4VcpuSet (specPtr vc) r.val w) >>= fun _ => NonDet.pure ()) σ1 = _ + rw [nd_bind_oneSc (by + rw [nd_bind_oneSc (loadWordOffs_eqA (specPtr frame) r.val σ1 (al3_of_frameWinA X1.fmem hWin r.val (by omega))), + ← hw_bv, hset2])] + rfl + refine ⟨⟨by rw [hr1_val]; omega, by rw [hr1_val]; omega, _, by rw [hr1_val]; exact hstep_spec, X2, + hms2.trans hms1, hksb2.trans hksb1, hcpu2.trans hcpu1, hfa2, hIdle2⟩, by rw [hr1_val]; omega⟩ + · rw [if_neg hle_r, WP.spec_ok] + have hreq : r.val = end_incl.val + 1 := by + have : ¬ r.val ≤ end_incl.val := hle_r; omega + dsimp only [Post] + refine ⟨σ1, ?_, X1, hms1, hksb1, hcpu1, hfa1, hIdle1⟩ + unfold idxRange; rwa [← hreq] + +/-- Helper: `vcpu.write_state_from_frame_loop0` (and identically `loop2`) refines the LR write loop. -/ +private theorem write_lrs_loop_refinesA {AR : A → HwRootA → Prop} {Z : XSetsA} {OS Ub : Set U64} + {m : Fl4SpecA64.McState} {a : A} {s : S} {σ : State Unit} + (X : VcpuXferStateA (L := L) AR Z OS Ub m a s σ) + (hU : UbFootDisjA Z OS Ub) + (hCs : ∀ c ∈ Z.Cs, c.val < 128) (hcpu : (L.cpu s).val < 128) + (hExt : ∀ b, VcPage Z.V b → ¬ ExtFootE Z.X Z.Y Z.T2 b) + (hSw : ∀ b, VcPage Z.V b → ∀ y, SlotWA Z.th y → + b ≠ y ∧ b ≠ wa y 8#u64 ∧ b ≠ wa y 16#u64 ∧ b ≠ wa y 24#u64) + (hfa : ∀ q, FootA (L := L) Z s q → Aligned q) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hIdleT : ∀ c ∈ Z.Cs, L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) ∈ Z.T) + {vc : U64} (hvc : vc ∈ Z.V) + {arr : Array U64 16#usize} {lrsList : List Word} + (hlen : lrsList.length = fl4NumLrs) + (harr : ∀ j, j < fl4NumLrs → (arr.val[j]!).bv = lrsList[j]!) : + vcpu.write_state_from_frame_loop0 st s vc arr ⟨BitVec.ofNat 64 fl4NumLrs⟩ 0#u64 ⦃ s' => + ∃ σ', NonDet.mapMx (fun (i, w) => fl4VcpuSet (specPtr vc) (vrVgicLr0 + i) w) + ((List.range fl4NumLrs).zip lrsList) σ = ({((), σ')}, False) ∧ + VcpuXferStateA (L := L) AR Z OS Ub m a s' σ' ∧ + σ'.machineState = σ.machineState ∧ L.ksb s' = L.ksb s ∧ L.cpu s' = L.cpu s ∧ + (∀ q, FootA (L := L) Z s' q → Aligned q) ∧ + (∀ c ∈ Z.Cs, L.mem s' (L.nodeAt s' c layout.node.IDLE_THREAD) ∈ Z.T) ⦄ := by + have hlrs16 : fl4NumLrs ≤ 16 := fl4NumLrs_le_16 + have hlrs_val : (⟨BitVec.ofNat 64 fl4NumLrs⟩ : U64).val = fl4NumLrs := by + change (BitVec.ofNat 64 fl4NumLrs).toNat = fl4NumLrs + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + let pairs := (List.range fl4NumLrs).zip lrsList + have hpairs_len : pairs.length = fl4NumLrs := by simp [pairs, hlen] + unfold vcpu.write_state_from_frame_loop0 + let stepSpec : Nat × Word → SMonad Unit Unit := fun (i, w) => fl4VcpuSet (specPtr vc) (vrVgicLr0 + i) w + let Inv : (S × U64) → Prop := fun (s1, i) => + i.val ≤ fl4NumLrs ∧ + ∃ σ1, NonDet.mapMx stepSpec (pairs.take i.val) σ = ({((), σ1)}, False) ∧ + VcpuXferStateA (L := L) AR Z OS Ub m a s1 σ1 ∧ + σ1.machineState = σ.machineState ∧ L.ksb s1 = L.ksb s ∧ L.cpu s1 = L.cpu s ∧ + (∀ q, FootA (L := L) Z s1 q → Aligned q) ∧ + (∀ c ∈ Z.Cs, L.mem s1 (L.nodeAt s1 c layout.node.IDLE_THREAD) ∈ Z.T) + let Post : S → Prop := fun s' => + ∃ σ', NonDet.mapMx stepSpec pairs σ = ({((), σ')}, False) ∧ + VcpuXferStateA (L := L) AR Z OS Ub m a s' σ' ∧ + σ'.machineState = σ.machineState ∧ L.ksb s' = L.ksb s ∧ L.cpu s' = L.cpu s ∧ + (∀ q, FootA (L := L) Z s' q → Aligned q) ∧ + (∀ c ∈ Z.Cs, L.mem s' (L.nodeAt s' c layout.node.IDLE_THREAD) ∈ Z.T) + apply loop.spec_decr_nat (measure := fun p => fl4NumLrs - p.2.val) (inv := Inv) (post := Post) + swap + · exact ⟨by simp, σ, by simp [NonDet.mapMx]; rfl, X, rfl, rfl, rfl, hfa, hIdleT⟩ + rintro ⟨s1, i⟩ ⟨hle, σ1, hspec1, X1, hms1, hksb1, hcpu1, hfa1, hIdle1⟩ + dsimp only + unfold vcpu.write_state_from_frame_loop0.body + by_cases hlt : i < ⟨BitVec.ofNat 64 fl4NumLrs⟩ + · have hilt : i.val < fl4NumLrs := by + have : i.val < (⟨BitVec.ofNat 64 fl4NumLrs⟩ : U64).val := hlt; omega + have hi16 : i < 16#u64 := by change i.val < 16; omega + rw [if_pos hlt, if_pos hi16, lift_ok, bind_ok, lift_ok, bind_ok] + have hcast_val : (UScalar.cast .Usize i).val = i.val := by + rw [UScalar.cast_val_eq] + change i.val % 2 ^ System.Platform.numBits = i.val + exact Nat.mod_eq_of_lt (by rcases System.Platform.numBits_eq with h | h <;> rw [h] <;> omega) + have harr_len : arr.length = 16 := arr.property + refine WP.spec_bind (Array.index_usize_spec arr (UScalar.cast .Usize i) (by rw [hcast_val]; omega)) ?_ + rintro w hw_eq + have hr_val : (wa fl4_abi.consts.vcpu_reg.VGIC_LR0 i).val = vrVgicLr0 + i.val := by + rw [wa_valP] + have h48 : fl4_abi.consts.vcpu_reg.VGIC_LR0.val = 48 := by unfold fl4_abi.consts.vcpu_reg.VGIC_LR0; rfl + rw [h48]; unfold vrVgicLr0; exact Nat.mod_eq_of_lt (by omega) + have hr_lt64 : (wa fl4_abi.consts.vcpu_reg.VGIC_LR0 i).val < 64 := by rw [hr_val]; unfold vrVgicLr0; omega + obtain ⟨v, hkv1, -, -⟩ := X1.vcpu.core.obj vc hvc + refine WP.spec_bind (vcpu_reg_wr_oneA X1 hU hCs (hcpu1 ▸ hcpu) hExt hSw hfa1 hsub hIdle1 hvc hkv1 hr_lt64 w + (by intro h; rw [hr_val] at h; unfold vrVgicLr0 vrVgicHcr at h; omega)) ?_ + rintro s2 ⟨hset2, X2, hms2, hksb2, hcpu2, hfa2, hIdle2⟩ + rw [u64_add_ok (by change i.val + 1 < 2 ^ 64; omega), bind_ok, WP.spec_ok] + have hw_bv : w.bv = lrsList[i.val]! := by + rw [hw_eq]; simp only [hcast_val] + have harr_vlen : arr.val.length = 16 := arr.property + have hget : arr.val[i.val]! = arr.val[i.val]'(by omega) := getElem!_pos _ _ (by omega) + rw [← hget]; exact harr i.val hilt + have hstep_spec : NonDet.mapMx stepSpec (pairs.take (i.val + 1)) σ = + ({((), updKSc σ1 (specPtr vc) (.archObj (.vcpu (fl4VcpuRegSet (wa fl4_abi.consts.vcpu_reg.VGIC_LR0 i).val w.bv v))))}, False) := by + have htake : pairs.take (i.val + 1) = pairs.take i.val ++ [(i.val, lrsList[i.val]!)] := by + rw [List.take_succ_eq_append_getElem (by omega)] + congr 1; congr 1 + simp only [pairs, List.getElem_zip, List.getElem_range] + congr 1; exact (getElem!_pos _ _ (by omega)).symm + rw [htake, mapMx_appendA, nd_bind_oneSc hspec1] + unfold NonDet.mapMx + change (stepSpec (i.val, lrsList[i.val]!) >>= fun _ => NonDet.pure ()) σ1 = _ + rw [hr_val] at hset2 ⊢ + rw [nd_bind_oneSc (by dsimp only [stepSpec]; rw [← hw_bv]; exact hset2)]; rfl + have hi1_val : (⟨BitVec.ofNat 64 (i.val + 1#u64.val)⟩ : U64).val = i.val + 1 := by + change (BitVec.ofNat 64 (i.val + 1)).toNat = i.val + 1 + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + refine ⟨⟨by rw [hi1_val]; omega, _, by rw [hi1_val]; exact hstep_spec, X2, + hms2.trans hms1, hksb2.trans hksb1, hcpu2.trans hcpu1, hfa2, hIdle2⟩, by rw [hi1_val]; omega⟩ + · rw [if_neg hlt, WP.spec_ok] + have hieq : i.val = fl4NumLrs := by + have : ¬ i.val < (⟨BitVec.ofNat 64 fl4NumLrs⟩ : U64).val := hlt; omega + dsimp only [Post] + refine ⟨σ1, ?_, X1, hms1, hksb1, hcpu1, hfa1, hIdle1⟩ + rw [hieq, ← hpairs_len, List.take_length] at hspec1; exact hspec1 + +/-- Helper: `asUser t (setRegister r w)` on a TCB `x`. -/ +private theorem asUser_setReg_eqA {σ : State Unit} {t : ObjRef} {x : Tcb} + (hk : σ.kheap t = some (.tcb x)) (r : Register) (w : Word) : + asUser t (setRegister r w) σ = + ({((), ctxUpdA σ t x { x.tcbArch.tcbContext with userRegs := Function.update x.tcbArch.tcbContext.userRegs r w })}, False) := + asUser_setReg_oneA hk r w + +set_option maxHeartbeats 400000 in +/-- Helper: `vcpu.write_state_from_frame_loop1` (and identically `loop3`, `loop4`) refines +the 37-GPR write loop + `FAULT_IP := ELR_EL1`. -/ +private theorem write_gprs_loop_refinesA {AR : A → HwRootA → Prop} {Z : XSetsA} {OS Ub : Set U64} + {m : Fl4SpecA64.McState} {a : A} {s : S} {σ : State Unit} + (X : VcpuXferStateA (L := L) AR Z OS Ub m a s σ) + (hU : UbFootDisjA Z OS Ub) + (hCs : ∀ c ∈ Z.Cs, c.val < 128) (hcpu : (L.cpu s).val < 128) + (hfa : ∀ q, FootA (L := L) Z s q → Aligned q) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hIdleT : ∀ c ∈ Z.Cs, L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) ∈ Z.T) + (hksTcb : ∀ t ∈ Z.T, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → + ∀ o : U64, o.val < ksSizeA → wa t K ≠ wa (L.ksb s) o) + (hSlotTcb : ∀ t ∈ Z.T, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → + ∀ y, SlotWA Z.th y → wa t K ≠ y ∧ wa t K ≠ wa y 8#u64 ∧ wa t K ≠ wa y 16#u64 ∧ wa t K ≠ wa y 24#u64) + {frame t_ptr : U64} (hWin : FrameWinA Ub frame) (ht : t_ptr ∈ Z.T) : + (do + let s1 ← vcpu.write_state_from_frame_loop1 st s (wa frame 512#u64) t_ptr 0#u64 + let pc ← obj.Tcb.reg st t_ptr s1 layout.reg.ELR_EL1 + obj.Tcb.set_reg st t_ptr s1 layout.reg.FAULT_IP pc) ⦃ s' => + ∃ σ', (do + NonDet.mapMx (fun (i, r) => do + let w ← loadWordOffs (specPtr frame) (64 + i) + asUser (specPtr t_ptr) (setRegister r (if r = Register.spsrEl1 then sanitiseVcpuSpsr w else w))) + (List.range fl4UserRegs.length |>.zip fl4UserRegs) + asUser (specPtr t_ptr) do + let pc ← getRegister nextInstructionRegister + setRegister faultRegister pc) σ = ({((), σ')}, False) ∧ + VcpuXferStateA (L := L) AR Z OS Ub m a s' σ' ⦄ := by + have hcnt : layout.reg.COUNT.val = 37 := by unfold layout.reg.COUNT; rfl + have hlen37 : fl4UserRegs.length = 37 := rfl + let pairs : List (Nat × Register) := (List.range fl4UserRegs.length).zip fl4UserRegs + have hpairs_len : pairs.length = 37 := by simp [pairs, hlen37] + let stepSpec : Nat × Register → SMonad Unit Unit := fun (i, r) => do + let w ← loadWordOffs (specPtr frame) (64 + i) + asUser (specPtr t_ptr) (setRegister r (if r = Register.spsrEl1 then sanitiseVcpuSpsr w else w)) + have hloop : vcpu.write_state_from_frame_loop1 st s (wa frame 512#u64) t_ptr 0#u64 ⦃ s1 => + ∃ σ1, NonDet.mapMx stepSpec pairs σ = ({((), σ1)}, False) ∧ + VcpuXferStateA (L := L) AR Z OS Ub m a s1 σ1 ∧ + L.ksb s1 = L.ksb s ∧ L.cpu s1 = L.cpu s ∧ + (∀ q, FootA (L := L) Z s1 q → Aligned q) ∧ + (∀ c ∈ Z.Cs, L.mem s1 (L.nodeAt s1 c layout.node.IDLE_THREAD) ∈ Z.T) ⦄ := by + unfold vcpu.write_state_from_frame_loop1 + let Inv : (S × U64) → Prop := fun (s1, i) => + i.val ≤ 37 ∧ + ∃ σ1, NonDet.mapMx stepSpec (pairs.take i.val) σ = ({((), σ1)}, False) ∧ + VcpuXferStateA (L := L) AR Z OS Ub m a s1 σ1 ∧ + L.ksb s1 = L.ksb s ∧ L.cpu s1 = L.cpu s ∧ + (∀ q, FootA (L := L) Z s1 q → Aligned q) ∧ + (∀ c ∈ Z.Cs, L.mem s1 (L.nodeAt s1 c layout.node.IDLE_THREAD) ∈ Z.T) + let Post : S → Prop := fun s1 => + ∃ σ1, NonDet.mapMx stepSpec pairs σ = ({((), σ1)}, False) ∧ + VcpuXferStateA (L := L) AR Z OS Ub m a s1 σ1 ∧ + L.ksb s1 = L.ksb s ∧ L.cpu s1 = L.cpu s ∧ + (∀ q, FootA (L := L) Z s1 q → Aligned q) ∧ + (∀ c ∈ Z.Cs, L.mem s1 (L.nodeAt s1 c layout.node.IDLE_THREAD) ∈ Z.T) + apply loop.spec_decr_nat (measure := fun p => 37 - p.2.val) (inv := Inv) (post := Post) + swap + · exact ⟨by decide, σ, by simp [NonDet.mapMx]; rfl, X, rfl, rfl, hfa, hIdleT⟩ + rintro ⟨s1, i⟩ ⟨hle37, σ1, hspec1, X1, hksb1, hcpu1, hfa1, hIdle1⟩ + dsimp only + unfold vcpu.write_state_from_frame_loop1.body + by_cases hi37 : i < layout.reg.COUNT + · have hilt : i.val < 37 := by have : i.val < layout.reg.COUNT.val := hi37; omega + rw [if_pos hi37, u64_mul_ok (by change i.val * 8 < 2 ^ 64; omega), bind_ok, lift_ok, bind_ok, L.read_eq, bind_ok] + have hwa_eq : wa (wa frame 512#u64) ⟨BitVec.ofNat 64 (i.val * 8#u64.val)⟩ = + wa frame ⟨BitVec.ofNat 64 (8 * (64 + i.val))⟩ := by + apply UScalar.eq_of_val_eq + rw [wa_valP, wa_valP, wa_valP] + have h512 : (512#u64 : U64).val = 512 := rfl + have h8i : (⟨BitVec.ofNat 64 (i.val * 8#u64.val)⟩ : U64).val = 8 * i.val := by + change (BitVec.ofNat 64 (i.val * 8)).toNat = 8 * i.val + rw [BitVec.toNat_ofNat]; rw [Nat.mod_eq_of_lt (by omega)]; omega + have h864 : (⟨BitVec.ofNat 64 (8 * (64 + i.val))⟩ : U64).val = 512 + 8 * i.val := by + change (BitVec.ofNat 64 _).toNat = _ + rw [BitVec.toNat_ofNat]; rw [Nat.mod_eq_of_lt (by omega)]; omega + rw [h512, h8i, h864, Nat.mod_eq_of_lt (by have := hWin.1; omega), + Nat.mod_eq_of_lt (by have := hWin.1; omega), Nat.mod_eq_of_lt (by have := hWin.1; omega)] + omega + rw [hwa_eq] + set raw : U64 := L.mem s1 (wa frame ⟨BitVec.ofNat 64 (8 * (64 + i.val))⟩) + have hraw_bv : raw.bv = loadWordAt σ1.machineState.underlyingMemory (bufAddr (specPtr frame) (64 + i.val)) := by + have h1 := X1.fmem.agree _ (hWin.2 (64 + i.val) (by omega)) + rw [specPtr_wa_wordA (by have := hWin.1; omega)] at h1; exact h1 + have hspsr_iff : i = layout.reg.SPSR_EL1 ↔ idxRegA i.val = .spsrEl1 := by + have h33 : layout.reg.SPSR_EL1.val = 33 := by unfold layout.reg.SPSR_EL1; rfl + constructor + · intro e; have : i.val = 33 := by rw [e, h33] + rw [this]; rfl + · intro e + apply UScalar.eq_of_val_eq; rw [h33] + have : (regIdxA (idxRegA i.val)).val = (regIdxA .spsrEl1).val := by rw [e] + rw [regIdxA_idxRegA i.val hilt] at this + simpa [regIdxA, Nat.mod_eq_of_lt (by omega : i.val < 2 ^ 64)] using this + have hsan_ok : (if i = layout.reg.SPSR_EL1 then regs.sanitise_vcpu_spsr raw else ok raw) = + ok ⟨if idxRegA i.val = .spsrEl1 then sanitiseVcpuSpsr raw.bv else raw.bv⟩ := by + by_cases hs : i = layout.reg.SPSR_EL1 + · rw [if_pos hs, if_pos (hspsr_iff.1 hs), sanitise_vcpu_spsr_eqA] + · rw [if_neg hs, if_neg (fun h => hs (hspsr_iff.2 h))] + set wv : U64 := ⟨if idxRegA i.val = .spsrEl1 then sanitiseVcpuSpsr raw.bv else raw.bv⟩ + rw [hsan_ok, bind_ok, eq_of_spec_fd (obj.Tcb.set_reg.spec s1 t_ptr i wv), bind_ok, + u64_add_ok (by change i.val + 1 < 2 ^ 64; omega), bind_ok, WP.spec_ok] + have hi_eq : i = regIdxA (idxRegA i.val) := by + rw [regIdxA_idxRegA i.val hilt] + exact UScalar.eq_of_val_eq (by + change i.val = (BitVec.ofNat 64 i.val).toNat + rw [BitVec.toNat_ofNat]; exact (Nat.mod_eq_of_lt (by omega)).symm) + have hreg_set_eq : L.wr s1 (Endp.regA t_ptr i) wv = L.wr s1 (Endp.regA t_ptr (regIdxA (idxRegA i.val))) wv := by + rw [← hi_eq] + rw [hreg_set_eq] + obtain ⟨xt, hkt, -⟩ := X1.j.1.sch.tcb t_ptr ht + have X2 := X1.tcb_reg_wr hU hCs (hcpu1 ▸ hcpu) hfa1 hsub hIdle1 + (by rw [hksb1]; exact hksTcb) hSlotTcb ht hkt (idxRegA i.val) wv + have hstep_spec : NonDet.mapMx stepSpec (pairs.take (i.val + 1)) σ = + ({((), ctxUpdA σ1 (specPtr t_ptr) xt + { xt.tcbArch.tcbContext with userRegs := Function.update xt.tcbArch.tcbContext.userRegs (idxRegA i.val) wv.bv })}, False) := by + have htake : pairs.take (i.val + 1) = pairs.take i.val ++ [(i.val, idxRegA i.val)] := by + rw [List.take_succ_eq_append_getElem (by omega)] + congr 1; congr 1 + simp only [pairs, List.getElem_zip, List.getElem_range, fl4UserRegs_getElemA i.val hilt] + rw [htake, mapMx_appendA, nd_bind_oneSc hspec1] + unfold NonDet.mapMx + change (stepSpec (i.val, idxRegA i.val) >>= fun _ => NonDet.pure ()) σ1 = _ + rw [nd_bind_oneSc (by + dsimp only [stepSpec] + rw [nd_bind_oneSc (loadWordOffs_eqA (specPtr frame) (64 + i.val) σ1 (al3_of_frameWinA X1.fmem hWin (64 + i.val) (by omega))), ← hraw_bv] + exact asUser_setReg_eqA hkt (idxRegA i.val) wv.bv)] + rfl + have hi1_val : (⟨BitVec.ofNat 64 (i.val + 1#u64.val)⟩ : U64).val = i.val + 1 := by + change (BitVec.ofNat 64 (i.val + 1)).toNat = i.val + 1 + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + have hta := hU.tcb_al t_ptr ht + have hreg := regA_eq_wa_KA t_ptr (idxRegA i.val) + set K : U64 := wa 256#u64 (core.num.U64.wrapping_mul (regIdxA (idxRegA i.val)) 8#u64) + have hKval : K.val = 256 + (regIdxA (idxRegA i.val)).val * 8 := hreg.1 + have hregK : Endp.regA t_ptr (regIdxA (idxRegA i.val)) = wa t_ptr K := hreg.2 + have hlt_r : (regIdxA (idxRegA i.val)).val < 37 := regIdxA_lt _ + have hK_ge : 256 ≤ K.val := by omega + have hK_lt : K.val < 3328 := by omega + have hK_al : K.val % 8 = 0 := by omega + have hba : Aligned (Endp.regA t_ptr (regIdxA (idxRegA i.val))) := by + rw [hregK] + exact aligned_wa (by unfold Aligned; have := hta.align; omega) K hK_al + have hfa2 : ∀ q, FootA (L := L) Z (L.wr s1 (Endp.regA t_ptr (regIdxA (idxRegA i.val))) wv) q → Aligned q := + fun q hq => hfa1 q ((FootA_wr_eqA Z s1 _ _ q).1 hq) + have hIdle2 : ∀ c ∈ Z.Cs, + L.mem (L.wr s1 (Endp.regA t_ptr (regIdxA (idxRegA i.val))) wv) + (L.nodeAt (L.wr s1 (Endp.regA t_ptr (regIdxA (idxRegA i.val))) wv) c layout.node.IDLE_THREAD) ∈ Z.T := by + intro c hc + have hne_idle : L.nodeAt s1 c layout.node.IDLE_THREAD ≠ Endp.regA t_ptr (regIdxA (idxRegA i.val)) := by + obtain ⟨k, hk, hne⟩ := nodeAt_eq_wa_ksbA (L := L) s1 c layout.node.IDLE_THREAD (hCs c hc) (by unfold layout.node.IDLE_THREAD; decide) + rw [hne, hksb1, hregK] + exact (hksTcb t_ptr ht K hK_ge hK_lt k hk).symm + have hal_idle : Aligned (L.nodeAt s1 c layout.node.IDLE_THREAD) := + hfa1 _ (FootA.nslot _ (FootNA.node c _ hc (Or.inr (Or.inl rfl)))) + rw [nodeAt_wrA, mem_wr_neA hba hal_idle hne_idle.symm]; exact hIdle1 c hc + refine ⟨⟨by rw [hi1_val]; omega, _, by rw [hi1_val]; exact hstep_spec, X2, + by rw [L.ksb_wr]; exact hksb1, by rw [L.cpu_wr]; exact hcpu1, hfa2, hIdle2⟩, by rw [hi1_val]; omega⟩ + · rw [if_neg hi37, WP.spec_ok] + have hieq : i.val = 37 := by have : ¬ i.val < layout.reg.COUNT.val := hi37; omega + dsimp only [Post] + refine ⟨σ1, ?_, X1, hksb1, hcpu1, hfa1, hIdle1⟩ + rw [hieq, ← hpairs_len, List.take_length] at hspec1; exact hspec1 + refine WP.spec_bind hloop ?_ + rintro s1 ⟨σ1, hsp1, X1, hksb1, hcpu1, hfa1, hIdle1⟩ + rw [eq_of_spec_fd (obj.Tcb.reg.spec s1 t_ptr layout.reg.ELR_EL1), bind_ok, + eq_of_spec_fd (obj.Tcb.set_reg.spec s1 t_ptr layout.reg.FAULT_IP _), WP.spec_ok] + obtain ⟨xt, hkt, -⟩ := X1.j.1.sch.tcb t_ptr ht + have helr_eq : layout.reg.ELR_EL1 = regIdxA .nextIP := by unfold layout.reg.ELR_EL1 regIdxA; rfl + have hflt_eq : layout.reg.FAULT_IP = regIdxA .faultIP := by unfold layout.reg.FAULT_IP regIdxA; rfl + have hpc_bv : (L.mem s1 (Endp.regA t_ptr layout.reg.ELR_EL1)).bv = xt.tcbArch.tcbContext.userRegs .nextIP := by + rw [helr_eq]; exact X1.regs t_ptr ht xt hkt .nextIP + rw [hflt_eq] + have X2 := X1.tcb_reg_wr hU hCs (hcpu1 ▸ hcpu) hfa1 hsub hIdle1 + (by rw [hksb1]; exact hksTcb) hSlotTcb ht hkt .faultIP (L.mem s1 (Endp.regA t_ptr layout.reg.ELR_EL1)) + refine ⟨_, ?_, X2⟩ + rw [nd_bind_oneSc hsp1] + have hasu_eq : (asUser (specPtr t_ptr) do + let pc ← getRegister nextInstructionRegister + setRegister faultRegister pc) σ1 = + asUser (specPtr t_ptr) (setRegister faultRegister (xt.tcbArch.tcbContext.userRegs .nextIP)) σ1 := by + have hg : NonDet.getsThe (getTcb (Z := Unit) (specPtr t_ptr)) σ1 = ({(xt, σ1)}, False) := by + simp [NonDet.getsThe, getTcb, readObject, hkt, NonDet.assertOpt]; rfl + have hpure : ∀ {α} (x : α), (pure x : SMonad Unit α) σ1 = ({(x, σ1)}, False) := fun _ => rfl + unfold asUser + rw [nd_bind_oneSc hg, nd_bind_oneSc (hpure _), + nd_bind_oneSc hg, nd_bind_oneSc (hpure _)] + congr 1; congr 1 + unfold getRegister setRegister nextInstructionRegister faultRegister archTcbContextGet + simp [Bind.bind, NonDet.instMonad, NonDet.bind, NonDet.gets, NonDet.modify] + rw [hasu_eq, ← hpc_bv] + exact asUser_setReg_oneA hkt .faultIP (L.mem s1 (Endp.regA t_ptr layout.reg.ELR_EL1)).bv + +/-- Helper: commuting `copyRegsFromFrame` (over registers `< 48`) past `setLrs` (over `48..48+lrs-1`) +on the spec side, because both only update `σ.kheap (specPtr vc)` and neither touches `underlyingMemory`. -/ +private theorem fl4VcpuSet_lrs_comm_regA {vc : ObjRef} {σ : State Unit} {v : Vcpu} + (hk : σ.kheap vc = some (.archObj (.vcpu v))) + {r : Nat} (hr : r < 48) (wr : Word) {lrs : List (Nat × Word)} + (hlrs : ∀ p ∈ lrs, p.1 < 16) : + (do + fl4VcpuSet vc r wr + NonDet.mapMx (fun (i, w) => fl4VcpuSet vc (vrVgicLr0 + i) w) lrs) σ = + (do + NonDet.mapMx (fun (i, w) => fl4VcpuSet vc (vrVgicLr0 + i) w) lrs + fl4VcpuSet vc r wr) σ := by + induction lrs generalizing σ v with + | nil => + rw [nd_bind_oneSc (fl4VcpuSet_eqA r wr hk)] + have hnil : NonDet.mapMx (fun (i, w) => fl4VcpuSet (Z := Unit) vc (vrVgicLr0 + i) w) [] = NonDet.pure () := rfl + rw [hnil, nd_bind_oneSc (m := NonDet.pure ()) rfl, fl4VcpuSet_eqA r wr hk]; rfl + | cons p tl ih => + rcases p with ⟨i, wi⟩ + have hi16 : i < 16 := hlrs (i, wi) (List.mem_cons_self ..) + have hne : r ≠ vrVgicLr0 + i := by unfold vrVgicLr0; omega + have h1 : NonDet.mapMx (fun (k, w) => fl4VcpuSet (Z := Unit) vc (vrVgicLr0 + k) w) ((i, wi) :: tl) = + (fl4VcpuSet vc (vrVgicLr0 + i) wi >>= fun _ => + NonDet.mapMx (fun (k, w) => fl4VcpuSet vc (vrVgicLr0 + k) w) tl) := rfl + rw [h1] + -- LHS: set r, set lr_i, set tl + rw [nd_bind_oneSc (fl4VcpuSet_eqA r wr hk), + nd_bind_oneSc (fl4VcpuSet_eqA (vrVgicLr0 + i) wi (updKSc_same _ _ _))] + -- RHS: set lr_i, set tl, set r + rw [nd_bind_assocSc, nd_bind_oneSc (fl4VcpuSet_eqA (vrVgicLr0 + i) wi hk)] + have hcomm := fl4VcpuRegSet_comm (by omega) (by unfold vrVgicLr0; omega) hne wr wi v + have hupd_eq : updKSc (updKSc σ vc (.archObj (.vcpu (fl4VcpuRegSet r wr v)))) vc + (.archObj (.vcpu (fl4VcpuRegSet (vrVgicLr0 + i) wi (fl4VcpuRegSet r wr v)))) = + updKSc (updKSc σ vc (.archObj (.vcpu (fl4VcpuRegSet (vrVgicLr0 + i) wi v)))) vc + (.archObj (.vcpu (fl4VcpuRegSet r wr (fl4VcpuRegSet (vrVgicLr0 + i) wi v)))) := by + rw [hcomm]; simp [updKSc, Function.update_idem] + rw [hupd_eq] + have hih := @ih (updKSc σ vc (.archObj (.vcpu (fl4VcpuRegSet (vrVgicLr0 + i) wi v)))) + (fl4VcpuRegSet (vrVgicLr0 + i) wi v) (updKSc_same _ _ _) (fun q hq => hlrs q (List.mem_cons_of_mem _ hq)) + rw [nd_bind_oneSc (fl4VcpuSet_eqA r wr (updKSc_same _ _ _))] at hih + exact hih + +/-- Commuting `copyRegsFromFrame` (over `rs` with all `< 48`) past `setLrs`. -/ +private theorem copyRegsFromFrame_lrs_commA {vc frame : ObjRef} {σ : State Unit} {v : Vcpu} + (hk : σ.kheap vc = some (.archObj (.vcpu v))) + {rs : List Nat} (hrs : ∀ r ∈ rs, r < 48) + (hal : ∀ r ∈ rs, bufAddr frame r &&& mask 3 = 0) + {lrs : List (Nat × Word)} (hlrs : ∀ p ∈ lrs, p.1 < 16) : + (do + copyRegsFromFrame vc frame rs + NonDet.mapMx (fun (i, w) => fl4VcpuSet vc (vrVgicLr0 + i) w) lrs) σ = + (do + NonDet.mapMx (fun (i, w) => fl4VcpuSet vc (vrVgicLr0 + i) w) lrs + copyRegsFromFrame vc frame rs) σ := by + induction rs generalizing σ v with + | nil => + have hnil : copyRegsFromFrame (Z := Unit) vc frame [] = NonDet.pure () := rfl + rw [hnil, nd_bind_oneSc (m := NonDet.pure ()) rfl] + change NonDet.mapMx _ lrs σ = (NonDet.mapMx _ lrs >>= fun _ => NonDet.pure ()) σ + rcases h : NonDet.mapMx (fun (i, w) => fl4VcpuSet (Z := Unit) vc (vrVgicLr0 + i) w) lrs σ with ⟨S, f⟩ + unfold Bind.bind NonDet.instMonad NonDet.bind NonDet.pure + dsimp only; rw [h]; simp only [Set.mem_singleton_iff, Prod.mk.injEq] + refine ⟨?_, by simp⟩ + ext ⟨a, s⟩; simp + | cons r tl ih => + have hr48 : r < 48 := hrs r (List.mem_cons_self ..) + have hal_r : bufAddr frame r &&& mask 3 = 0 := hal r (List.mem_cons_self ..) + have hcons : copyRegsFromFrame (Z := Unit) vc frame (r :: tl) = + (loadWordOffs frame r >>= fun w => fl4VcpuSet vc r w >>= fun _ => copyRegsFromFrame vc frame tl) := by + funext s0 + change ((loadWordOffs frame r >>= fun w => fl4VcpuSet vc r w) >>= fun _ => + copyRegsFromFrame vc frame tl) s0 = _ + rw [nd_bind_assocSc] + rw [hcons, nd_bind_assocSc, nd_bind_oneSc (loadWordOffs_eqA frame r σ hal_r), nd_bind_assocSc] + set wr := loadWordAt σ.machineState.underlyingMemory (bufAddr frame r) + rw [nd_bind_oneSc (fl4VcpuSet_eqA r wr hk)] + rw [ih (updKSc_same _ _ _) (fun q hq => hrs q (List.mem_cons_of_mem _ hq)) + (fun q hq => hal q (List.mem_cons_of_mem _ hq))] + -- Now LHS is: (setLrs >>= copyRegs tl) (updKSc σ vc (set r wr v)) + -- = (set r wr >>= setLrs >>= copyRegs tl) σ + -- = (setLrs >>= set r wr >>= copyRegs tl) σ (by fl4VcpuSet_lrs_comm_regA) + have hlhs : (do + NonDet.mapMx (fun (i, w) => fl4VcpuSet vc (vrVgicLr0 + i) w) lrs + copyRegsFromFrame vc frame tl) (updKSc σ vc (.archObj (.vcpu (fl4VcpuRegSet r wr v)))) = + (do + (do fl4VcpuSet vc r wr; NonDet.mapMx (fun (i, w) => fl4VcpuSet vc (vrVgicLr0 + i) w) lrs) + copyRegsFromFrame vc frame tl) σ := by + rw [nd_bind_assocSc, nd_bind_oneSc (fl4VcpuSet_eqA r wr hk)] + rw [hlhs, nd_bind_assocSc] + -- Show setLrs preserves underlyingMemory so loadWordOffs frame r gives the same wr! + have hlrs_um : ∀ (l : List (Nat × Word)) (s0 : State Unit) (v0 : Vcpu), + s0.kheap vc = some (.archObj (.vcpu v0)) → + ∃ v1, NonDet.mapMx (fun (i, w) => fl4VcpuSet vc (vrVgicLr0 + i) w) l s0 = + ({((), updKSc s0 vc (.archObj (.vcpu v1)))}, False) := by + intro l; induction l with + | nil => + intro s0 v0 hk0; refine ⟨v0, ?_⟩ + unfold NonDet.mapMx + have : updKSc s0 vc (.archObj (.vcpu v0)) = s0 := by + have hkeq : Function.update s0.kheap vc (some (.archObj (.vcpu v0))) = s0.kheap := + Function.update_eq_self_iff.2 hk0.symm + cases s0; unfold updKSc; simp only [hkeq] + rw [this]; rfl + | cons q ltl ihl => + intro s0 v0 hk0 + rcases q with ⟨qi, qw⟩ + obtain ⟨v1, hv1⟩ := ihl _ _ (updKSc_same s0 vc (.archObj (.vcpu (fl4VcpuRegSet (vrVgicLr0 + qi) qw v0)))) + refine ⟨v1, ?_⟩ + unfold NonDet.mapMx + change (fl4VcpuSet vc (vrVgicLr0 + qi) qw >>= fun _ => + NonDet.mapMx (fun (i, w) => fl4VcpuSet vc (vrVgicLr0 + i) w) ltl) s0 = _ + rw [nd_bind_oneSc (fl4VcpuSet_eqA (vrVgicLr0 + qi) qw hk0), hv1] + simp [updKSc, Function.update_idem] + obtain ⟨v1, hv1⟩ := hlrs_um lrs σ v hk + have hcomm := fl4VcpuSet_lrs_comm_regA hk hr48 wr hlrs + have hstep1 : (do fl4VcpuSet vc r wr; NonDet.mapMx (fun (i, w) => fl4VcpuSet vc (vrVgicLr0 + i) w) lrs) σ = + ({((), updKSc σ vc (.archObj (.vcpu (fl4VcpuRegSet r wr v1))))}, False) := by + rw [hcomm, nd_bind_oneSc hv1, fl4VcpuSet_eqA r wr (updKSc_same _ _ _)] + simp [updKSc, Function.update_idem] + rw [← nd_bind_assocSc, nd_bind_oneSc hstep1, nd_bind_oneSc hv1, + nd_bind_oneSc (loadWordOffs_eqA frame r _ hal_r)] + -- `updKSc` preserves `machineState.underlyingMemory`! + have hum : (updKSc σ vc (.archObj (.vcpu v1))).machineState.underlyingMemory = + σ.machineState.underlyingMemory := rfl + rw [hum, nd_bind_oneSc (fl4VcpuSet_eqA r wr (updKSc_same _ _ _))] + simp [updKSc, Function.update_idem] + +/-- **`vcpu.write_state_from_frame` refines `fl4WriteStateFromFrame`** across all 6 mask bits +(`EL1_SYSREGS`, `HYP`, `TIMER`, `VGIC` / `VGIC_LR`, `GPRS`) and `mask = 0`, preserving +`VcpuXferStateA`. Reused by both label 68 (`vcpu_write_state_jA`) and W4 (`vcpu_run_jA`). -/ +theorem write_state_from_frame_refinesA {AR : A → HwRootA → Prop} {Z : XSetsA} {OS Ub : Set U64} + {m : Fl4SpecA64.McState} {a : A} {s : S} {σ : State Unit} + (X : VcpuXferStateA (L := L) AR Z OS Ub m a s σ) + (hP : VcpuPlatAgreeA ar) + (hU : UbFootDisjA Z OS Ub) + (hCs : ∀ c ∈ Z.Cs, c.val < 128) (hcpu : (L.cpu s).val < 128) + (hExt : ∀ b, VcPage Z.V b → ¬ ExtFootE Z.X Z.Y Z.T2 b) + (hSw : ∀ b, VcPage Z.V b → ∀ y, SlotWA Z.th y → + b ≠ y ∧ b ≠ wa y 8#u64 ∧ b ≠ wa y 16#u64 ∧ b ≠ wa y 24#u64) + (hfa : ∀ q, FootA (L := L) Z s q → Aligned q) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (hIdleT : ∀ c ∈ Z.Cs, L.mem s (L.nodeAt s c layout.node.IDLE_THREAD) ∈ Z.T) + (hksTcb : ∀ t ∈ Z.T, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → + ∀ o : U64, o.val < ksSizeA → wa t K ≠ wa (L.ksb s) o) + (hSlotTcb : ∀ t ∈ Z.T, ∀ K : U64, 256 ≤ K.val → K.val < 3328 → + ∀ y, SlotWA Z.th y → wa t K ≠ y ∧ wa t K ≠ wa y 8#u64 ∧ wa t K ≠ wa y 16#u64 ∧ wa t K ≠ wa y 24#u64) + {vc frame mask : U64} (hvc : vc ∈ Z.V) (hWin : FrameWinA Ub frame) + {vgic : U64 × Array U64 16#usize} {vgicSpec : Word × List Word} + (hVgic : mask.bv &&& (smVgic ||| smVgicLr) ≠ 0 → + vgic.1.bv = vgicSpec.1 ∧ vgicSpec.1 >>> 32 = 0 ∧ + vgicSpec.2.length = fl4NumLrs ∧ ∀ j, j < fl4NumLrs → (vgic.2.val[j]!).bv = vgicSpec.2[j]!) : + vcpu.write_state_from_frame st ar s a vc frame mask vgic ⦃ s' => + ∃ σ', fl4WriteStateFromFrame (specPtr vc) (specPtr frame) mask.bv vgicSpec σ = ({((), σ')}, False) ∧ + VcpuXferStateA (L := L) AR Z OS Ub m a s' σ' ⦄ := by + unfold vcpu.write_state_from_frame + by_cases hm0 : mask = 0#u64 + · rw [if_pos hm0, WP.spec_ok] + refine ⟨σ, ?_, X⟩ + subst hm0 + unfold fl4WriteStateFromFrame NonDet.when + simp only [show (0#u64 : U64).bv = 0 from rfl, + show (0 : Word) &&& smEl1 = 0 from rfl, show (0 : Word) &&& smHyp = 0 from rfl, + show (0 : Word) &&& smTimer = 0 from rfl, show (0 : Word) &&& smVgic = 0 from rfl, + show (0 : Word) &&& smVgicLr = 0 from rfl, show (0 : Word) &&& smGprs = 0 from rfl, + ne_eq, not_true_eq_false, ↓reduceIte] + rw [nd_bind_oneSc (m := NonDet.pure ()) rfl, nd_bind_oneSc (m := NonDet.pure ()) rfl, + nd_bind_oneSc (m := NonDet.pure ()) rfl, nd_bind_oneSc (m := NonDet.pure ()) rfl]; rfl + · rw [if_neg hm0] + -- Helper for trailing GPRS block (identical across loop1, loop3, loop4) + have hGprs_step : ∀ (sG : S) (σG : State Unit) + (XG : VcpuXferStateA (L := L) AR Z OS Ub m a sG σG) + (hksbG : L.ksb sG = L.ksb s) (hcpuG : L.cpu sG = L.cpu s) + (hfaG : ∀ q, FootA (L := L) Z sG q → Aligned q) + (hIdleG : ∀ c ∈ Z.Cs, L.mem sG (L.nodeAt sG c layout.node.IDLE_THREAD) ∈ Z.T), + (do + let i13 ← fl4_abi.consts.vcpu_state_mask.GPRS + let i14 ← lift (mask &&& i13) + if i14 != 0#u64 then do + let i15 ← 64#u64 * 8#u64 + let gprs_base ← lift (wa frame i15) + let i16 ← lift (wa vc vcpu.TCB) + let t_ptr ← st.read sG i16 + if t_ptr != 0#u64 then do + let s10 ← vcpu.write_state_from_frame_loop1 st sG gprs_base t_ptr 0#u64 + let pc ← obj.Tcb.reg st t_ptr s10 layout.reg.ELR_EL1 + obj.Tcb.set_reg st t_ptr s10 layout.reg.FAULT_IP pc + else ok sG + else ok sG) ⦃ s' => + ∃ σ', (NonDet.when (mask.bv &&& smGprs ≠ 0) do + let v ← getVcpu (specPtr vc) + match v.vcpuTcb with + | some t => do + NonDet.mapMx (fun (i, r) => do + let w ← loadWordOffs (specPtr frame) (64 + i) + asUser t (setRegister r (if r = Register.spsrEl1 then sanitiseVcpuSpsr w else w))) + (List.range fl4UserRegs.length |>.zip fl4UserRegs) + asUser t do + let pc ← getRegister nextInstructionRegister + setRegister faultRegister pc + | none => pure ()) σG = ({((), σ')}, False) ∧ + VcpuXferStateA (L := L) AR Z OS Ub m a s' σ' ⦄ := by + intro sG σG XG hksbG hcpuG hfaG hIdleG + rw [sm_gprs_ok, bind_ok, lift_ok, bind_ok] + by_cases hg : (mask &&& 16#u64 != 0#u64) = true + · have hgb : mask.bv &&& smGprs ≠ 0 := fun e => by + have : mask &&& 16#u64 = 0#u64 := u64_eq_of_bv_eq e; simp [this] at hg + rw [if_pos hg, u64_mul_ok (by decide), bind_ok, lift_ok, bind_ok, lift_ok, bind_ok, L.read_eq, bind_ok] + have hwa0 : wa vc vcpu.TCB = vc := by + apply UScalar.eq_of_val_eq; rw [wa_valP]; unfold vcpu.TCB; simp [UScalar.hBounds] + rw [hwa0] + obtain ⟨v, hkv, hvtcb, -⟩ := XG.vcpu.core.obj vc hvc + by_cases hnz : (L.mem sG vc != 0#u64) = true + · have hne0 : L.mem sG vc ≠ 0#u64 := bne_iff_ne.1 hnz + have hv1 : v.vcpuTcb = some (specPtr (L.mem sG vc)) := by rw [hvtcb]; unfold optRefA; simp [hne0] + obtain ⟨ht_in, -⟩ := XG.vcpu.bind_v2t vc hvc (L.mem sG vc) rfl hne0 + rw [if_pos hnz] + refine WP.spec_mono (write_gprs_loop_refinesA XG hU hCs (hcpuG ▸ hcpu) hfaG hsub hIdleG + (by rw [hksbG]; exact hksTcb) hSlotTcb hWin ht_in) ?_ + rintro s' ⟨σ', hsp', X'⟩ + refine ⟨σ', ?_, X'⟩ + unfold NonDet.when; rw [if_pos hgb, nd_bind_oneSc (getVcpu_eqA hkv), hv1]; exact hsp' + · have heq0 : L.mem sG vc = 0#u64 := by + by_contra hne; exact hnz (bne_iff_ne.2 hne) + have hv0 : v.vcpuTcb = none := by rw [hvtcb, heq0]; rfl + rw [if_neg hnz, WP.spec_ok] + refine ⟨σG, ?_, XG⟩ + unfold NonDet.when; rw [if_pos hgb, nd_bind_oneSc (getVcpu_eqA hkv), hv0]; rfl + · have hgb : ¬ (mask.bv &&& smGprs ≠ 0) := fun hne => hg (bne_iff_ne.2 (fun e => hne (congrArg UScalar.bv e))) + rw [if_neg hg, WP.spec_ok] + exact ⟨σG, by unfold NonDet.when; rw [if_neg hgb]; rfl, XG⟩ + -- Step 1: EL1_SYSREGS + have hStep1 : (do + let i ← fl4_abi.consts.vcpu_state_mask.EL1_SYSREGS + let i1 ← lift (mask &&& i) + if i1 != 0#u64 then do + let s2 ← vcpu.copy_reg_range_from_frame st s vc frame 0#u64 fl4_abi.consts.vcpu_reg.CSSELR_EL1 + vcpu.copy_reg_range_from_frame st s2 vc frame fl4_abi.consts.vcpu_reg.ACTLR_EL1 fl4_abi.consts.vcpu_reg.MDCCINT_EL1 + else ok s) ⦃ s1 => + ∃ σ1, (NonDet.when (mask.bv &&& smEl1 ≠ 0) do + copyRegsFromFrame (specPtr vc) (specPtr frame) (idxRange 0 vrCsselr) + copyRegsFromFrame (specPtr vc) (specPtr frame) (idxRange vrActlr vrMdccint)) σ = ({((), σ1)}, False) ∧ + VcpuXferStateA (L := L) AR Z OS Ub m a s1 σ1 ∧ + σ1.machineState = σ.machineState ∧ L.ksb s1 = L.ksb s ∧ L.cpu s1 = L.cpu s ∧ + (∀ q, FootA (L := L) Z s1 q → Aligned q) ∧ + (∀ c ∈ Z.Cs, L.mem s1 (L.nodeAt s1 c layout.node.IDLE_THREAD) ∈ Z.T) ⦄ := by + rw [sm_el1_ok, bind_ok, lift_ok, bind_ok] + by_cases h1 : (mask &&& 1#u64 != 0#u64) = true + · have h1b : mask.bv &&& smEl1 ≠ 0 := fun e => by + have : mask &&& 1#u64 = 0#u64 := u64_eq_of_bv_eq e; simp [this] at h1 + rw [if_pos h1] + have hcs : fl4_abi.consts.vcpu_reg.CSSELR_EL1.val = vrCsselr := by + unfold fl4_abi.consts.vcpu_reg.CSSELR_EL1 vrCsselr; rfl + have hact : fl4_abi.consts.vcpu_reg.ACTLR_EL1.val = vrActlr := by + unfold fl4_abi.consts.vcpu_reg.ACTLR_EL1 vrActlr; rfl + have hmdc : fl4_abi.consts.vcpu_reg.MDCCINT_EL1.val = vrMdccint := by + unfold fl4_abi.consts.vcpu_reg.MDCCINT_EL1 vrMdccint; rfl + refine WP.spec_bind (copy_reg_range_from_frame_refinesA X hU hCs hcpu hExt hSw hfa hsub hIdleT hvc hWin + (Nat.zero_le _) (by rw [hcs]; unfold vrCsselr; decide) + (fun k _ hk => by rw [hcs] at hk; unfold vrCsselr vrVgicHcr at *; omega)) ?_ + rintro s2 ⟨σ2, hsp2, X2, hms2, hksb2, hcpu2, hfa2, hIdle2⟩ + refine WP.spec_mono (copy_reg_range_from_frame_refinesA X2 hU hCs (hcpu2 ▸ hcpu) hExt hSw hfa2 hsub hIdle2 hvc hWin + (by rw [hact, hmdc]; unfold vrActlr vrMdccint; decide) (by rw [hmdc]; unfold vrMdccint; decide) + (fun k _ hk => by rw [hmdc] at hk; unfold vrMdccint vrVgicHcr at *; omega)) ?_ + rintro s1 ⟨σ1, hsp1, X1, hms1, hksb1, hcpu1, hfa1, hIdle1⟩ + refine ⟨σ1, ?_, X1, hms1.trans hms2, hksb1.trans hksb2, hcpu1.trans hcpu2, hfa1, hIdle1⟩ + unfold NonDet.when; rw [if_pos h1b] + have h0val : (0#u64 : U64).val = 0 := rfl + rw [h0val, hcs] at hsp2; rw [hact, hmdc] at hsp1 + rw [nd_bind_oneSc hsp2]; exact hsp1 + · have h1b : ¬ (mask.bv &&& smEl1 ≠ 0) := fun hne => h1 (bne_iff_ne.2 (fun e => hne (congrArg UScalar.bv e))) + rw [if_neg h1, WP.spec_ok] + exact ⟨σ, by unfold NonDet.when; rw [if_neg h1b]; rfl, X, rfl, rfl, rfl, hfa, hIdleT⟩ + rw [sm_el1_ok, bind_ok, lift_ok, bind_ok] at hStep1 ⊢ + refine WP.spec_bind hStep1 ?_ + rintro s1 ⟨σ1, hsp1, X1, hms1, hksb1, hcpu1, hfa1, hIdle1⟩ + -- Step 2: HYP + have hStep2 : (do + let i2 ← fl4_abi.consts.vcpu_state_mask.HYP + let i3 ← lift (mask &&& i2) + if i3 != 0#u64 then do + let i4 ← lift (mask &&& 1#u64) + let s3 ← + if i4 = 0#u64 then do + let i5 ← fl4_abi.consts.vcpu_reg.VMPIDR_EL2 * 8#u64 + let i6 ← lift (wa frame i5) + let i7 ← st.read s1 i6 + vcpu.reg_set st s1 vc fl4_abi.consts.vcpu_reg.VMPIDR_EL2 i7 + else ok s1 + let i5 ← fl4_abi.consts.vcpu_reg.VDISR_EL2 * 8#u64 + let i6 ← lift (wa frame i5) + let i7 ← st.read s3 i6 + let s4 ← vcpu.reg_set st s3 vc fl4_abi.consts.vcpu_reg.VDISR_EL2 i7 + let i8 ← fl4_abi.consts.vcpu_reg.VPIDR_EL2 * 8#u64 + let i9 ← lift (wa frame i8) + let i10 ← st.read s4 i9 + let s5 ← vcpu.reg_set st s4 vc fl4_abi.consts.vcpu_reg.VPIDR_EL2 i10 + let i11 ← fl4_abi.consts.vcpu_reg.HCR_EL2 * 8#u64 + let i12 ← lift (wa frame i11) + let i13 ← st.read s5 i12 + let i14 ← vcpu.sanitize_hcr i13 + let s6 ← vcpu.reg_set st s5 vc fl4_abi.consts.vcpu_reg.HCR_EL2 i14 + let i15 ← fl4_abi.consts.vcpu_reg.VSESR_EL2 * 8#u64 + let i16 ← lift (wa frame i15) + let i17 ← st.read s6 i16 + vcpu.reg_set st s6 vc fl4_abi.consts.vcpu_reg.VSESR_EL2 i17 + else ok s1) ⦃ s2 => + ∃ σ2, (NonDet.when (mask.bv &&& smHyp ≠ 0) do + NonDet.when (mask.bv &&& smEl1 = 0) <| copyRegsFromFrame (specPtr vc) (specPtr frame) [vrVmpidr] + copyRegsFromFrame (specPtr vc) (specPtr frame) [vrVdisr, vrVpidr] + let hcr ← loadWordOffs (specPtr frame) vrHcr + fl4VcpuSet (specPtr vc) vrHcr (sanitizeHcr hcr) + copyRegsFromFrame (specPtr vc) (specPtr frame) [vrVsesr]) σ1 = ({((), σ2)}, False) ∧ + VcpuXferStateA (L := L) AR Z OS Ub m a s2 σ2 ∧ + σ2.machineState = σ.machineState ∧ L.ksb s2 = L.ksb s ∧ L.cpu s2 = L.cpu s ∧ + (∀ q, FootA (L := L) Z s2 q → Aligned q) ∧ + (∀ c ∈ Z.Cs, L.mem s2 (L.nodeAt s2 c layout.node.IDLE_THREAD) ∈ Z.T) ⦄ := by + rw [sm_hyp_ok, bind_ok, lift_ok, bind_ok] + by_cases h2 : (mask &&& 2#u64 != 0#u64) = true + · have h2b : mask.bv &&& smHyp ≠ 0 := fun e => by + have : mask &&& 2#u64 = 0#u64 := u64_eq_of_bv_eq e; simp [this] at h2 + rw [if_pos h2, lift_ok, bind_ok] + have hvmp : fl4_abi.consts.vcpu_reg.VMPIDR_EL2.val = vrVmpidr := by + unfold fl4_abi.consts.vcpu_reg.VMPIDR_EL2 vrVmpidr; rfl + have hvdi : fl4_abi.consts.vcpu_reg.VDISR_EL2.val = vrVdisr := by + unfold fl4_abi.consts.vcpu_reg.VDISR_EL2 vrVdisr; rfl + have hvpi : fl4_abi.consts.vcpu_reg.VPIDR_EL2.val = vrVpidr := by + unfold fl4_abi.consts.vcpu_reg.VPIDR_EL2 vrVpidr; rfl + have hhcr_v : fl4_abi.consts.vcpu_reg.HCR_EL2.val = vrHcr := by + unfold fl4_abi.consts.vcpu_reg.HCR_EL2 vrHcr; rfl + have hvse : fl4_abi.consts.vcpu_reg.VSESR_EL2.val = vrVsesr := by + unfold fl4_abi.consts.vcpu_reg.VSESR_EL2 vrVsesr; rfl + have hVmpStep : (if mask &&& 1#u64 = 0#u64 then do + let i5 ← fl4_abi.consts.vcpu_reg.VMPIDR_EL2 * 8#u64 + let i6 ← lift (wa frame i5) + let i7 ← st.read s1 i6 + vcpu.reg_set st s1 vc fl4_abi.consts.vcpu_reg.VMPIDR_EL2 i7 + else ok s1) ⦃ s3 => + ∃ σ3, (NonDet.when (mask.bv &&& smEl1 = 0) <| copyRegsFromFrame (specPtr vc) (specPtr frame) [vrVmpidr]) σ1 = ({((), σ3)}, False) ∧ + VcpuXferStateA (L := L) AR Z OS Ub m a s3 σ3 ∧ + σ3.machineState = σ.machineState ∧ L.ksb s3 = L.ksb s ∧ L.cpu s3 = L.cpu s ∧ + (∀ q, FootA (L := L) Z s3 q → Aligned q) ∧ + (∀ c ∈ Z.Cs, L.mem s3 (L.nodeAt s3 c layout.node.IDLE_THREAD) ∈ Z.T) ⦄ := by + by_cases he0 : mask &&& 1#u64 = 0#u64 + · have he0b : mask.bv &&& smEl1 = 0 := congrArg UScalar.bv he0 + rw [if_pos he0, u64_mul_ok (by rw [hvmp]; unfold vrVmpidr; decide), bind_ok, lift_ok, bind_ok, L.read_eq, bind_ok] + have hm1 : (⟨BitVec.ofNat 64 (fl4_abi.consts.vcpu_reg.VMPIDR_EL2.val * 8#u64.val)⟩ : U64) = + ⟨BitVec.ofNat 64 (8 * vrVmpidr)⟩ := by congr 1; rw [hvmp]; unfold vrVmpidr; rfl + rw [hm1] + obtain ⟨v1, hkv1, -, -⟩ := X1.vcpu.core.obj vc hvc + have hw_bv : (L.mem s1 (wa frame ⟨BitVec.ofNat 64 (8 * vrVmpidr)⟩)).bv = + loadWordAt σ1.machineState.underlyingMemory (bufAddr (specPtr frame) vrVmpidr) := by + have h := X1.fmem.agree _ (hWin.2 vrVmpidr (by unfold vrVmpidr; decide)) + rw [specPtr_wa_wordA (by have := hWin.1; unfold vrVmpidr; omega)] at h; exact h + refine WP.spec_mono (vcpu_reg_wr_oneA X1 hU hCs (hcpu1 ▸ hcpu) hExt hSw hfa1 hsub hIdle1 hvc hkv1 + (by rw [hvmp]; unfold vrVmpidr; decide) _ (by intro h; rw [hvmp] at h; unfold vrVmpidr vrVgicHcr at h; omega)) ?_ + rintro s3 ⟨hset3, X3, hms3, hksb3, hcpu3, hfa3, hIdle3⟩ + refine ⟨_, ?_, X3, hms3.trans hms1, hksb3.trans hksb1, hcpu3.trans hcpu1, hfa3, hIdle3⟩ + unfold NonDet.when copyRegsFromFrame NonDet.mapMx + rw [if_pos he0b] + change ((do let w ← loadWordOffs (specPtr frame) vrVmpidr; fl4VcpuSet (specPtr vc) vrVmpidr w) >>= fun _ => NonDet.pure ()) σ1 = _ + rw [nd_bind_oneSc (by + rw [nd_bind_oneSc (loadWordOffs_eqA (specPtr frame) vrVmpidr σ1 (al3_of_frameWinA X1.fmem hWin vrVmpidr (by unfold vrVmpidr; decide))), ← hw_bv] + rw [hvmp] at hset3; exact hset3)] + rw [hvmp]; rfl + · have he0b : ¬ (mask.bv &&& smEl1 = 0) := fun e => he0 (u64_eq_of_bv_eq e) + rw [if_neg he0, WP.spec_ok] + exact ⟨σ1, by unfold NonDet.when; rw [if_neg he0b]; rfl, X1, hms1, hksb1, hcpu1, hfa1, hIdle1⟩ + refine WP.spec_bind hVmpStep ?_ + rintro s3 ⟨σ3, hsp3, X3, hms3, hksb3, hcpu3, hfa3, hIdle3⟩ + -- VDISR + rw [u64_mul_ok (by rw [hvdi]; unfold vrVdisr; decide), bind_ok, lift_ok, bind_ok, L.read_eq, bind_ok] + have hm2 : (⟨BitVec.ofNat 64 (fl4_abi.consts.vcpu_reg.VDISR_EL2.val * 8#u64.val)⟩ : U64) = + ⟨BitVec.ofNat 64 (8 * vrVdisr)⟩ := by congr 1; rw [hvdi]; unfold vrVdisr; rfl + rw [hm2] + obtain ⟨v3, hkv3, -, -⟩ := X3.vcpu.core.obj vc hvc + have hw_vdi : (L.mem s3 (wa frame ⟨BitVec.ofNat 64 (8 * vrVdisr)⟩)).bv = + loadWordAt σ3.machineState.underlyingMemory (bufAddr (specPtr frame) vrVdisr) := by + have h := X3.fmem.agree _ (hWin.2 vrVdisr (by unfold vrVdisr; decide)) + rw [specPtr_wa_wordA (by have := hWin.1; unfold vrVdisr; omega)] at h; exact h + refine WP.spec_bind (vcpu_reg_wr_oneA X3 hU hCs (hcpu3 ▸ hcpu) hExt hSw hfa3 hsub hIdle3 hvc hkv3 + (by rw [hvdi]; unfold vrVdisr; decide) _ (by intro h; rw [hvdi] at h; unfold vrVdisr vrVgicHcr at h; omega)) ?_ + rintro s4 ⟨hset4, X4, hms4, hksb4, hcpu4, hfa4, hIdle4⟩ + -- VPIDR + rw [u64_mul_ok (by rw [hvpi]; unfold vrVpidr; decide), bind_ok, lift_ok, bind_ok, L.read_eq, bind_ok] + have hm3 : (⟨BitVec.ofNat 64 (fl4_abi.consts.vcpu_reg.VPIDR_EL2.val * 8#u64.val)⟩ : U64) = + ⟨BitVec.ofNat 64 (8 * vrVpidr)⟩ := by congr 1; rw [hvpi]; unfold vrVpidr; rfl + rw [hm3] + obtain ⟨v4, hkv4, -, -⟩ := X4.vcpu.core.obj vc hvc + have hw_vpi : (L.mem s4 (wa frame ⟨BitVec.ofNat 64 (8 * vrVpidr)⟩)).bv = + loadWordAt σ3.machineState.underlyingMemory (bufAddr (specPtr frame) vrVpidr) := by + have h := X4.fmem.agree _ (hWin.2 vrVpidr (by unfold vrVpidr; decide)) + rw [specPtr_wa_wordA (by have := hWin.1; unfold vrVpidr; omega)] at h; exact h + refine WP.spec_bind (vcpu_reg_wr_oneA X4 hU hCs (hcpu4.trans hcpu3 ▸ hcpu) hExt hSw hfa4 hsub hIdle4 hvc hkv4 + (by rw [hvpi]; unfold vrVpidr; decide) _ (by intro h; rw [hvpi] at h; unfold vrVpidr vrVgicHcr at h; omega)) ?_ + rintro s5 ⟨hset5, X5, hms5, hksb5, hcpu5, hfa5, hIdle5⟩ + -- HCR_EL2 (sanitized) + rw [u64_mul_ok (by rw [hhcr_v]; unfold vrHcr; decide), bind_ok, lift_ok, bind_ok, L.read_eq, bind_ok, + vcpu_sanitize_hcr_eqA, bind_ok] + have hm4 : (⟨BitVec.ofNat 64 (fl4_abi.consts.vcpu_reg.HCR_EL2.val * 8#u64.val)⟩ : U64) = + ⟨BitVec.ofNat 64 (8 * vrHcr)⟩ := by congr 1; rw [hhcr_v]; unfold vrHcr; rfl + rw [hm4] + obtain ⟨v5, hkv5, -, -⟩ := X5.vcpu.core.obj vc hvc + have hw_hcr : (L.mem s5 (wa frame ⟨BitVec.ofNat 64 (8 * vrHcr)⟩)).bv = + loadWordAt σ3.machineState.underlyingMemory (bufAddr (specPtr frame) vrHcr) := by + have h := X5.fmem.agree _ (hWin.2 vrHcr (by unfold vrHcr; decide)) + rw [specPtr_wa_wordA (by have := hWin.1; unfold vrHcr; omega)] at h; exact h + refine WP.spec_bind (vcpu_reg_wr_oneA X5 hU hCs (hcpu5.trans (hcpu4.trans hcpu3) ▸ hcpu) hExt hSw hfa5 hsub hIdle5 hvc hkv5 + (by rw [hhcr_v]; unfold vrHcr; decide) _ (by intro h; rw [hhcr_v] at h; unfold vrHcr vrVgicHcr at h; omega)) ?_ + rintro s6 ⟨hset6, X6, hms6, hksb6, hcpu6, hfa6, hIdle6⟩ + -- VSESR_EL2 + rw [u64_mul_ok (by rw [hvse]; unfold vrVsesr; decide), bind_ok, lift_ok, bind_ok, L.read_eq, bind_ok] + have hm5 : (⟨BitVec.ofNat 64 (fl4_abi.consts.vcpu_reg.VSESR_EL2.val * 8#u64.val)⟩ : U64) = + ⟨BitVec.ofNat 64 (8 * vrVsesr)⟩ := by congr 1; rw [hvse]; unfold vrVsesr; rfl + rw [hm5] + obtain ⟨v6, hkv6, -, -⟩ := X6.vcpu.core.obj vc hvc + have hw_vse : (L.mem s6 (wa frame ⟨BitVec.ofNat 64 (8 * vrVsesr)⟩)).bv = + loadWordAt σ3.machineState.underlyingMemory (bufAddr (specPtr frame) vrVsesr) := by + have h := X6.fmem.agree _ (hWin.2 vrVsesr (by unfold vrVsesr; decide)) + rw [specPtr_wa_wordA (by have := hWin.1; unfold vrVsesr; omega)] at h; exact h + refine WP.spec_mono (vcpu_reg_wr_oneA X6 hU hCs (hcpu6.trans (hcpu5.trans (hcpu4.trans hcpu3)) ▸ hcpu) hExt hSw hfa6 hsub hIdle6 hvc hkv6 + (by rw [hvse]; unfold vrVsesr; decide) _ (by intro h; rw [hvse] at h; unfold vrVsesr vrVgicHcr at h; omega)) ?_ + rintro s2 ⟨hset2, X2, hms2, hksb2, hcpu2, hfa2, hIdle2⟩ + refine ⟨_, ?_, X2, + hms2.trans (hms6.trans (hms5.trans (hms4.trans hms3))), + hksb2.trans (hksb6.trans (hksb5.trans (hksb4.trans hksb3))), + hcpu2.trans (hcpu6.trans (hcpu5.trans (hcpu4.trans hcpu3))), hfa2, hIdle2⟩ + unfold NonDet.when at hsp3 ⊢; rw [if_pos h2b, nd_bind_oneSc hsp3] + simp only [hvdi, hvpi, hhcr_v, hvse] at hset4 hset5 hset6 hset2 + have hum_upd : ∀ (s0 : State Unit) (p : ObjRef) (o : KernelObject), + (updKSc s0 p o).machineState.underlyingMemory = s0.machineState.underlyingMemory := fun _ _ _ => rfl + have hbv_mk : ∀ (w : Word), U64.bv ⟨w⟩ = w := fun _ => rfl + rw [nd_bind_oneSc (by + unfold copyRegsFromFrame NonDet.mapMx + change ((do let w ← loadWordOffs (specPtr frame) vrVdisr; fl4VcpuSet (specPtr vc) vrVdisr w) >>= fun _ => + (do let w ← loadWordOffs (specPtr frame) vrVpidr; fl4VcpuSet (specPtr vc) vrVpidr w) >>= fun _ => NonDet.pure ()) σ3 = _ + rw [nd_bind_oneSc (by rw [nd_bind_oneSc (loadWordOffs_eqA (specPtr frame) vrVdisr σ3 (al3_of_frameWinA X3.fmem hWin vrVdisr (by unfold vrVdisr; decide))), ← hw_vdi]; exact hset4), + nd_bind_oneSc (by rw [nd_bind_oneSc (loadWordOffs_eqA (specPtr frame) vrVpidr _ (al3_of_frameWinA X4.fmem hWin vrVpidr (by unfold vrVpidr; decide))), hum_upd, ← hw_vpi]; exact hset5)] + rfl)] + rw [nd_bind_oneSc (loadWordOffs_eqA (specPtr frame) vrHcr _ (al3_of_frameWinA X5.fmem hWin vrHcr (by unfold vrHcr; decide))), hum_upd, hum_upd, ← hw_hcr] + rw [hbv_mk] at hset6 + rw [nd_bind_oneSc hset6] + unfold copyRegsFromFrame NonDet.mapMx + change ((do let w ← loadWordOffs (specPtr frame) vrVsesr; fl4VcpuSet (specPtr vc) vrVsesr w) >>= fun _ => NonDet.pure ()) _ = _ + rw [nd_bind_oneSc (by rw [nd_bind_oneSc (loadWordOffs_eqA (specPtr frame) vrVsesr _ (al3_of_frameWinA X6.fmem hWin vrVsesr (by unfold vrVsesr; decide))), hum_upd, hum_upd, hum_upd, ← hw_vse]; exact hset2)] + simp only [hvdi, hvpi, hhcr_v, hvse, hbv_mk]; rfl + · have h2b : ¬ (mask.bv &&& smHyp ≠ 0) := fun hne => h2 (bne_iff_ne.2 (fun e => hne (congrArg UScalar.bv e))) + rw [if_neg h2, WP.spec_ok] + exact ⟨σ1, by unfold NonDet.when; rw [if_neg h2b]; rfl, X1, hms1, hksb1, hcpu1, hfa1, hIdle1⟩ + rw [sm_hyp_ok, bind_ok, lift_ok, bind_ok] at hStep2 ⊢ + refine WP.spec_bind hStep2 ?_ + rintro s2 ⟨σ2, hsp2, X2, hms2, hksb2, hcpu2, hfa2, hIdle2⟩ + -- Step 3: TIMER + have hStep3 : (do + let i4 ← fl4_abi.consts.vcpu_state_mask.TIMER + let i5 ← lift (mask &&& i4) + if i5 != 0#u64 then do + let s4 ← vcpu.copy_reg_range_from_frame st s2 vc frame fl4_abi.consts.vcpu_reg.CNTV_CTL fl4_abi.consts.vcpu_reg.CNTKCTL_EL1 + let i6 ← 64#u64 + 37#u64 + let i7 ← i6 * 8#u64 + let vppi_addr ← lift (wa frame i7) + let i8 ← st.read s4 vppi_addr + let m1 ← lift (i8 &&& 1#u64) + let i9 ← lift (wa vc vcpu.VPPI_MASKED) + st.write s4 i9 m1 + else ok s2) ⦃ s3 => + ∃ σ3, (NonDet.when (mask.bv &&& smTimer ≠ 0) do + copyRegsFromFrame (specPtr vc) (specPtr frame) (idxRange vrCntvCtl vrCntkctl) + let w ← loadWordOffs (specPtr frame) vppiWord + let v ← getVcpu (specPtr vc) + let mk := Function.update v.vcpuVppiMasked .vppiEventIRQ_VTimer (decide (w &&& 1 ≠ 0)) + setVcpu (specPtr vc) { v with vcpuVppiMasked := mk }) σ2 = ({((), σ3)}, False) ∧ + VcpuXferStateA (L := L) AR Z OS Ub m a s3 σ3 ∧ + σ3.machineState = σ.machineState ∧ L.ksb s3 = L.ksb s ∧ L.cpu s3 = L.cpu s ∧ + (∀ q, FootA (L := L) Z s3 q → Aligned q) ∧ + (∀ c ∈ Z.Cs, L.mem s3 (L.nodeAt s3 c layout.node.IDLE_THREAD) ∈ Z.T) ⦄ := by + rw [sm_timer_ok, bind_ok, lift_ok, bind_ok] + by_cases h3 : (mask &&& 4#u64 != 0#u64) = true + · have h3b : mask.bv &&& smTimer ≠ 0 := fun e => by + have : mask &&& 4#u64 = 0#u64 := u64_eq_of_bv_eq e; simp [this] at h3 + rw [if_pos h3] + have hctl : fl4_abi.consts.vcpu_reg.CNTV_CTL.val = vrCntvCtl := by + unfold fl4_abi.consts.vcpu_reg.CNTV_CTL vrCntvCtl; rfl + have hkctl : fl4_abi.consts.vcpu_reg.CNTKCTL_EL1.val = vrCntkctl := by + unfold fl4_abi.consts.vcpu_reg.CNTKCTL_EL1 vrCntkctl; rfl + refine WP.spec_bind (copy_reg_range_from_frame_refinesA X2 hU hCs (hcpu2 ▸ hcpu) hExt hSw hfa2 hsub hIdle2 hvc hWin + (by rw [hctl, hkctl]; unfold vrCntvCtl vrCntkctl; decide) (by rw [hkctl]; unfold vrCntkctl; decide) + (fun k _ hk => by rw [hkctl] at hk; unfold vrCntkctl vrVgicHcr at *; omega)) ?_ + rintro s4 ⟨σ4, hsp4, X4, hms4, hksb4, hcpu4, hfa4, hIdle4⟩ + rw [u64_add_ok (by decide), bind_ok, u64_mul_ok (by decide), bind_ok, lift_ok, bind_ok, + L.read_eq, bind_ok, lift_ok, bind_ok, lift_ok, bind_ok, L.write_eq, WP.spec_ok] + have hvppi_off : wa vc vcpu.VPPI_MASKED = wa vc 16#u64 := by unfold vcpu.VPPI_MASKED; rfl + have hm_vp : (⟨BitVec.ofNat 64 ((⟨BitVec.ofNat 64 (64#u64.val + 37#u64.val)⟩ : U64).val * 8#u64.val)⟩ : U64) = + ⟨BitVec.ofNat 64 (8 * vppiWord)⟩ := by unfold vppiWord; rfl + rw [hvppi_off, hm_vp] + set wvp : U64 := L.mem s4 (wa frame ⟨BitVec.ofNat 64 (8 * vppiWord)⟩) + have hwvp_bv : wvp.bv = loadWordAt σ4.machineState.underlyingMemory (bufAddr (specPtr frame) vppiWord) := by + have h := X4.fmem.agree _ (hWin.2 vppiWord (by unfold vppiWord; decide)) + rw [specPtr_wa_wordA (by have := hWin.1; unfold vppiWord; omega)] at h + exact h + obtain ⟨v4, hkv4, -, -⟩ := X4.vcpu.core.obj vc hvc + set mk := Function.update v4.vcpuVppiMasked .vppiEventIRQ_VTimer (decide (wvp.bv &&& 1 ≠ 0)) + have hb_vc : VcPage Z.V (wa vc 16#u64) := vcpuHdr_in_vcPage hvc (by decide) (by decide) + have hba : Aligned (wa vc 16#u64) := aligned_wa (by have := X4.vcpu.place.al vc hvc; unfold Aligned; omega) 16#u64 (by decide) + have hnl : ∀ q ∈ Z.V, wa vc 16#u64 ≠ q ∧ wa vc 16#u64 ≠ wa q 24#u64 := fun q hq => + vcpuHdr16_ne_hdrA X4.vcpu.place hvc hq + have hbit : wvp &&& 1#u64 = if decide (wvp.bv &&& 1 ≠ 0) then 1#u64 else 0#u64 := by + by_cases hz : wvp.bv &&& 1 = 0 + · rw [decide_eq_false (not_not.2 hz), if_neg (by decide)] + exact u64_eq_of_bv_eq hz + · rw [decide_eq_true hz, if_pos rfl] + apply u64_eq_of_bv_eq + have hle1 : (wvp.bv &&& 1).toNat ≤ 1 := by + have := @Nat.and_le_right wvp.bv.toNat 1; simpa using this + have hne0 : (wvp.bv &&& 1).toNat ≠ 0 := fun e => hz (BitVec.eq_of_toNat_eq e) + exact BitVec.eq_of_toNat_eq (by change (wvp.bv &&& 1).toNat = 1; omega) + have hV3 := VcpuRelA.upd_vppiA X4.vcpu X4.j.1.ksFit hCs hIdle4 hvc hkv4 (decide (wvp.bv &&& 1 ≠ 0)) + rw [← hbit] at hV3 + have X3 := X4.vc_nonlink_wr (v' := { v4 with vcpuVppiMasked := mk }) hCs (hcpu4.trans hcpu2 ▸ hcpu) hExt hSw hfa4 hsub hU + (writesIn_wrA s4 (wa vc 16#u64) (wvp &&& 1#u64) hba) + (fun q hq => by subst hq; exact ⟨hb_vc, hnl⟩) hvc hkv4 rfl rfl hV3 + refine ⟨_, ?_, X3, hms4.trans hms2, + (L.ksb_wr _ _ _).trans (hksb4.trans hksb2), + (L.cpu_wr _ _ _).trans (hcpu4.trans hcpu2), + fun q hq => hfa4 q ((FootA_wr_eqA Z s4 _ _ q).1 hq), fun c hc => ?_⟩ + · unfold NonDet.when; rw [if_pos h3b] + rw [hctl, hkctl] at hsp4 + rw [nd_bind_oneSc hsp4, nd_bind_oneSc (loadWordOffs_eqA (specPtr frame) vppiWord σ4 (al3_of_frameWinA X4.fmem hWin vppiWord (by unfold vppiWord; decide))), + ← hwvp_bv, nd_bind_oneSc (getVcpu_eqA hkv4), setVcpu_eqA hkv4] + · have hne_idle : L.nodeAt s4 c layout.node.IDLE_THREAD ≠ wa vc 16#u64 := fun e => + not_footA_of_vcNonLinkA X4.vcpu.place X4.j.1.ksFit hCs (hcpu4.trans hcpu2 ▸ hcpu) hExt hSw hb_vc hnl + (e ▸ FootA.nslot _ (FootNA.node c _ hc (Or.inr (Or.inl rfl)))) + have hal_idle : Aligned (L.nodeAt s4 c layout.node.IDLE_THREAD) := + hfa4 _ (FootA.nslot _ (FootNA.node c _ hc (Or.inr (Or.inl rfl)))) + rw [nodeAt_wrA, mem_wr_neA hba hal_idle hne_idle.symm]; exact hIdle4 c hc + · have h3b : ¬ (mask.bv &&& smTimer ≠ 0) := fun hne => h3 (bne_iff_ne.2 (fun e => hne (congrArg UScalar.bv e))) + rw [if_neg h3, WP.spec_ok] + exact ⟨σ2, by unfold NonDet.when; rw [if_neg h3b]; rfl, X2, hms2, hksb2, hcpu2, hfa2, hIdle2⟩ + rw [sm_timer_ok, bind_ok, lift_ok, bind_ok] at hStep3 ⊢ + refine WP.spec_bind hStep3 ?_ + rintro s3 ⟨σ3, hsp3, X3, hms3, hksb3, hcpu3, hfa3, hIdle3⟩ + -- Step 4: VGIC / VGIC_LR / GPRS + rw [sm_vgic_ok, bind_ok, lift_ok, bind_ok] + by_cases h4 : (mask &&& 8#u64 != 0#u64) = true + · have h4b : mask.bv &&& smVgic ≠ 0 := fun e => by + have : mask &&& 8#u64 = 0#u64 := u64_eq_of_bv_eq e; simp [this] at h4 + have hvg_or : mask.bv &&& (smVgic ||| smVgicLr) ≠ 0 := by + intro hz; apply h4b + have : (mask.bv &&& (smVgic ||| smVgicLr)) &&& smVgic = 0 := by rw [hz]; simp + simpa [BitVec.and_assoc, smVgic, smVgicLr] using this + obtain ⟨hvg1, hvg1_u32, hvg2_len, hvg2_arr⟩ := hVgic hvg_or + rw [if_pos h4, vcpu_num_lrs_eqA hP a, bind_ok] + obtain ⟨g, hga, hgb⟩ := hP.gic a + rw [hga, bind_ok] + have hdec : (decide (g = 3#u64)) = fl4GicV3 := hgb + rcases vgic with ⟨vgHcr, vgArr⟩ + have hvhcr : fl4_abi.consts.vcpu_reg.VGIC_HCR.val = vrVgicHcr := by + unfold fl4_abi.consts.vcpu_reg.VGIC_HCR vrVgicHcr; rfl + have hvmcr : fl4_abi.consts.vcpu_reg.VGIC_VMCR.val = vrVgicVmcr := by + unfold fl4_abi.consts.vcpu_reg.VGIC_VMCR vrVgicVmcr; rfl + have hap00 : fl4_abi.consts.vcpu_reg.VGIC_AP0R0.val = vrVgicAp0r0 := by + unfold fl4_abi.consts.vcpu_reg.VGIC_AP0R0 vrVgicAp0r0; rfl + have hap03 : fl4_abi.consts.vcpu_reg.VGIC_AP0R3.val = vrVgicAp0r3 := by + unfold fl4_abi.consts.vcpu_reg.VGIC_AP0R3 vrVgicAp0r3; rfl + have hap10 : fl4_abi.consts.vcpu_reg.VGIC_AP1R0.val = vrVgicAp1r0 := by + unfold fl4_abi.consts.vcpu_reg.VGIC_AP1R0 vrVgicAp1r0; rfl + have hap13 : fl4_abi.consts.vcpu_reg.VGIC_AP1R3.val = vrVgicAp1r3 := by + unfold fl4_abi.consts.vcpu_reg.VGIC_AP1R3 vrVgicAp1r3; rfl + -- Write VGIC_HCR + obtain ⟨v3, hkv3, -, -⟩ := X3.vcpu.core.obj vc hvc + refine WP.spec_bind (vcpu_reg_wr_oneA X3 hU hCs (hcpu3 ▸ hcpu) hExt hSw hfa3 hsub hIdle3 hvc hkv3 + (by rw [hvhcr]; unfold vrVgicHcr; decide) vgHcr (fun _ => by rw [hvg1]; exact hvg1_u32)) ?_ + rintro s4 ⟨hset4, X4, hms4, hksb4, hcpu4, hfa4, hIdle4⟩ + set σ4 := updKSc σ3 (specPtr vc) (.archObj (.vcpu (fl4VcpuRegSet fl4_abi.consts.vcpu_reg.VGIC_HCR.val vgHcr.bv v3))) + -- Write VGIC_VMCR + rw [u64_mul_ok (by rw [hvmcr]; unfold vrVgicVmcr; decide), bind_ok, lift_ok, bind_ok, L.read_eq, bind_ok] + have hm_vmcr : (⟨BitVec.ofNat 64 (fl4_abi.consts.vcpu_reg.VGIC_VMCR.val * 8#u64.val)⟩ : U64) = + ⟨BitVec.ofNat 64 (8 * vrVgicVmcr)⟩ := by congr 1; rw [hvmcr]; unfold vrVgicVmcr; rfl + rw [hm_vmcr] + obtain ⟨v4, hkv4, -, -⟩ := X4.vcpu.core.obj vc hvc + have hw_vmcr : (L.mem s4 (wa frame ⟨BitVec.ofNat 64 (8 * vrVgicVmcr)⟩)).bv = + loadWordAt σ4.machineState.underlyingMemory (bufAddr (specPtr frame) vrVgicVmcr) := by + have h := X4.fmem.agree _ (hWin.2 vrVgicVmcr (by unfold vrVgicVmcr; decide)) + rw [specPtr_wa_wordA (by have := hWin.1; unfold vrVgicVmcr; omega)] at h; exact h + refine WP.spec_bind (vcpu_reg_wr_oneA X4 hU hCs (hcpu4.trans hcpu3 ▸ hcpu) hExt hSw hfa4 hsub hIdle4 hvc hkv4 + (by rw [hvmcr]; unfold vrVgicVmcr; decide) _ (by intro h; rw [hvmcr] at h; unfold vrVgicVmcr vrVgicHcr at h; omega)) ?_ + rintro s5 ⟨hset5, X5, hms5, hksb5, hcpu5, hfa5, hIdle5⟩ + -- Write AP0R0..3 + refine WP.spec_bind (copy_reg_range_from_frame_refinesA X5 hU hCs (hcpu5.trans (hcpu4.trans hcpu3) ▸ hcpu) + hExt hSw hfa5 hsub hIdle5 hvc hWin (by rw [hap00, hap03]; unfold vrVgicAp0r0 vrVgicAp0r3; decide) + (by rw [hap03]; unfold vrVgicAp0r3; decide) + (fun k h1 _ => by rw [hap00] at h1; unfold vrVgicAp0r0 vrVgicHcr at *; omega)) ?_ + rintro s6 ⟨σ6, hsp6, X6, hms6, hksb6, hcpu6, hfa6, hIdle6⟩ + -- Write AP1R0..3 if GICv3 + have hAp1Step : (if g = 3#u64 then + vcpu.copy_reg_range_from_frame st s6 vc frame fl4_abi.consts.vcpu_reg.VGIC_AP1R0 fl4_abi.consts.vcpu_reg.VGIC_AP1R3 + else ok s6) ⦃ s7 => + ∃ σ7, (NonDet.when fl4GicV3 <| copyRegsFromFrame (specPtr vc) (specPtr frame) (idxRange vrVgicAp1r0 vrVgicAp1r3)) σ6 = ({((), σ7)}, False) ∧ + VcpuXferStateA (L := L) AR Z OS Ub m a s7 σ7 ∧ + σ7.machineState = σ.machineState ∧ L.ksb s7 = L.ksb s ∧ L.cpu s7 = L.cpu s ∧ + (∀ q, FootA (L := L) Z s7 q → Aligned q) ∧ + (∀ c ∈ Z.Cs, L.mem s7 (L.nodeAt s7 c layout.node.IDLE_THREAD) ∈ Z.T) ⦄ := by + by_cases hg3 : g = 3#u64 + · have hfl : fl4GicV3 = true := by rw [← hdec, decide_eq_true hg3] + rw [if_pos hg3] + refine WP.spec_mono (copy_reg_range_from_frame_refinesA X6 hU hCs + (hcpu6.trans (hcpu5.trans (hcpu4.trans hcpu3)) ▸ hcpu) hExt hSw hfa6 hsub hIdle6 hvc hWin + (by rw [hap10, hap13]; unfold vrVgicAp1r0 vrVgicAp1r3; decide) (by rw [hap13]; unfold vrVgicAp1r3; decide) + (fun k h1 _ => by rw [hap10] at h1; unfold vrVgicAp1r0 vrVgicHcr at *; omega)) ?_ + rintro s7 ⟨σ7, hsp7, X7, hms7, hksb7, hcpu7, hfa7, hIdle7⟩ + refine ⟨σ7, ?_, X7, + hms7.trans (hms6.trans (hms5.trans (hms4.trans hms3))), + hksb7.trans (hksb6.trans (hksb5.trans (hksb4.trans hksb3))), + hcpu7.trans (hcpu6.trans (hcpu5.trans (hcpu4.trans hcpu3))), hfa7, hIdle7⟩ + unfold NonDet.when; rw [if_pos (by simp [hfl])]; rw [hap10, hap13] at hsp7; exact hsp7 + · have hfl : fl4GicV3 = false := by rw [← hdec, decide_eq_false hg3] + rw [if_neg hg3, WP.spec_ok] + refine ⟨σ6, by unfold NonDet.when; rw [if_neg (by simp [hfl])]; rfl, X6, + hms6.trans (hms5.trans (hms4.trans hms3)), + hksb6.trans (hksb5.trans (hksb4.trans hksb3)), + hcpu6.trans (hcpu5.trans (hcpu4.trans hcpu3)), hfa6, hIdle6⟩ + refine WP.spec_bind hAp1Step ?_ + rintro s7 ⟨σ7, hsp7, X7, hms7, hksb7, hcpu7, hfa7, hIdle7⟩ + -- Write LR0..lrs-1 + refine WP.spec_bind (write_lrs_loop_refinesA X7 hU hCs (hcpu7 ▸ hcpu) hExt hSw hfa7 hsub hIdle7 hvc + hvg2_len hvg2_arr) ?_ + rintro s8 ⟨σ8, hsp8, X8, hms8, hksb8, hcpu8, hfa8, hIdle8⟩ + -- sync_vgic_status + obtain ⟨v8, hkv8, -, -⟩ := X8.vcpu.core.obj vc hvc + rw [hdec] + refine WP.spec_bind (sync_vgic_status_refinesA X8.vcpu X8.j.1.ksFit hCs hIdle8 hvc hkv8) ?_ + rintro s9 ⟨hV9, hW9⟩ + have hsync_meta := syncVgicStatus_pres_metaA v8 fl4NumLrs fl4GicV3 + have X9 := X8.vc_nonlink_wr hCs (hcpu8.trans hcpu7 ▸ hcpu) hExt hSw hfa8 hsub hU hW9 + (fun b hb => by + rcases hb with rfl | rfl | rfl + · exact ⟨vcpuRegWA_in_vcPage hvc (by unfold vrVgicElrsr; decide), fun q hq => vcpuRegWA_ne_hdrA X8.vcpu.place hvc (by unfold vrVgicElrsr; decide) hq⟩ + · exact ⟨vcpuRegWA_in_vcPage hvc (by unfold vrVgicEisr; decide), fun q hq => vcpuRegWA_ne_hdrA X8.vcpu.place hvc (by unfold vrVgicEisr; decide) hq⟩ + · exact ⟨vcpuRegWA_in_vcPage hvc (by unfold vrVgicMisr; decide), fun q hq => vcpuRegWA_ne_hdrA X8.vcpu.place hvc (by unfold vrVgicMisr; decide) hq⟩) + hvc hkv8 hsync_meta.1 hsync_meta.2.2.1 hV9 + have hfa9 : ∀ q, FootA (L := L) Z s9 q → Aligned q := fun q hq => + hfa8 q ((FootA_of_ksb_cpuA Z hW9.1 hW9.2.1 q).1 hq) + have hIdle9 : ∀ c ∈ Z.Cs, L.mem s9 (L.nodeAt s9 c layout.node.IDLE_THREAD) ∈ Z.T := by + intro c hc + have hks9 : L.ksb s9 = L.ksb s8 := hW9.1 + have hnd9 : L.nodeAt s9 c layout.node.IDLE_THREAD = L.nodeAt s8 c layout.node.IDLE_THREAD := + nodeAt_of_ksb hks9 c layout.node.IDLE_THREAD + have hal_idle : Aligned (L.nodeAt s8 c layout.node.IDLE_THREAD) := + hfa8 _ (FootA.nslot _ (FootNA.node c _ hc (Or.inr (Or.inl rfl)))) + rw [hnd9, hW9.2.2 _ hal_idle (fun h => by + rcases h with h | h | h + · exact not_footA_of_vcNonLinkA X8.vcpu.place X8.j.1.ksFit hCs (hcpu8.trans hcpu7 ▸ hcpu) hExt hSw + (h ▸ vcpuRegWA_in_vcPage hvc (by unfold vrVgicElrsr; decide)) + (fun q hq => h ▸ vcpuRegWA_ne_hdrA X8.vcpu.place hvc (by unfold vrVgicElrsr; decide) hq) + (FootA.nslot _ (FootNA.node c _ hc (Or.inr (Or.inl rfl)))) + · exact not_footA_of_vcNonLinkA X8.vcpu.place X8.j.1.ksFit hCs (hcpu8.trans hcpu7 ▸ hcpu) hExt hSw + (h ▸ vcpuRegWA_in_vcPage hvc (by unfold vrVgicEisr; decide)) + (fun q hq => h ▸ vcpuRegWA_ne_hdrA X8.vcpu.place hvc (by unfold vrVgicEisr; decide) hq) + (FootA.nslot _ (FootNA.node c _ hc (Or.inr (Or.inl rfl)))) + · exact not_footA_of_vcNonLinkA X8.vcpu.place X8.j.1.ksFit hCs (hcpu8.trans hcpu7 ▸ hcpu) hExt hSw + (h ▸ vcpuRegWA_in_vcPage hvc (by unfold vrVgicMisr; decide)) + (fun q hq => h ▸ vcpuRegWA_ne_hdrA X8.vcpu.place hvc (by unfold vrVgicMisr; decide) hq) + (FootA.nslot _ (FootNA.node c _ hc (Or.inr (Or.inl rfl)))))] + exact hIdle8 c hc + -- Trailing GPRS + refine WP.spec_mono (hGprs_step s9 _ X9 + (hW9.1.trans (hksb8.trans hksb7)) (hW9.2.1.trans (hcpu8.trans hcpu7)) hfa9 hIdle9) ?_ + rintro s' ⟨σ', hsp', X'⟩ + refine ⟨σ', ?_, X'⟩ + -- Reconcile concrete VGIC order (HCR, VMCR, AP0R, AP1R, LRs) with spec order (HCR, LRs, VMCR, AP0R, AP1R)! + unfold fl4WriteStateFromFrame + rw [nd_bind_oneSc hsp1, nd_bind_oneSc hsp2, nd_bind_oneSc hsp3] + have hbind_assoc : ∀ {α β γ} (m : SMonad Unit α) (f : α → SMonad Unit β) (g : β → SMonad Unit γ), + (m >>= f) >>= g = m >>= fun x => f x >>= g := fun m f g => funext fun s => nd_bind_assocSc m f g s + have hlrs_b : ∀ p ∈ (List.range fl4NumLrs).zip vgicSpec.2, p.1 < 16 := fun p hp => by + have := (List.of_mem_zip hp).1; rw [List.mem_range] at this; have := fl4NumLrs_le_16; omega + have hregs_all : (do + copyRegsFromFrame (Z := Unit) (specPtr vc) (specPtr frame) [vrVgicVmcr] + copyRegsFromFrame (specPtr vc) (specPtr frame) (idxRange vrVgicAp0r0 vrVgicAp0r3) + NonDet.when fl4GicV3 <| copyRegsFromFrame (specPtr vc) (specPtr frame) (idxRange vrVgicAp1r0 vrVgicAp1r3)) = + copyRegsFromFrame (Z := Unit) (specPtr vc) (specPtr frame) + ([vrVgicVmcr] ++ idxRange vrVgicAp0r0 vrVgicAp0r3 ++ + if fl4GicV3 then idxRange vrVgicAp1r0 vrVgicAp1r3 else []) := by + have hmap_app : ∀ (xs ys : List Nat), + copyRegsFromFrame (Z := Unit) (specPtr vc) (specPtr frame) (xs ++ ys) = + (copyRegsFromFrame (specPtr vc) (specPtr frame) xs >>= fun _ => + copyRegsFromFrame (specPtr vc) (specPtr frame) ys) := + fun xs ys => funext fun s0 => mapMx_appendA _ xs ys s0 + rw [hmap_app, hmap_app, hbind_assoc] + congr 1; funext _; congr 1; funext _ + cases fl4GicV3 + · unfold NonDet.when copyRegsFromFrame; simp [NonDet.mapMx] + · unfold NonDet.when; simp + have hstep_comb : (do + copyRegsFromFrame (specPtr vc) (specPtr frame) + ([vrVgicVmcr] ++ idxRange vrVgicAp0r0 vrVgicAp0r3 ++ + if fl4GicV3 then idxRange vrVgicAp1r0 vrVgicAp1r3 else []) + NonDet.mapMx (fun (i, w) => fl4VcpuSet (specPtr vc) (vrVgicLr0 + i) w) ((List.range fl4NumLrs).zip vgicSpec.2)) σ4 = + ({((), σ8)}, False) := by + rw [← hregs_all, nd_bind_assocSc] + rw [hvmcr] at hset5 + rw [nd_bind_oneSc (by + unfold copyRegsFromFrame NonDet.mapMx + change ((do let w ← loadWordOffs (specPtr frame) vrVgicVmcr; fl4VcpuSet (specPtr vc) vrVgicVmcr w) >>= fun _ => NonDet.pure ()) σ4 = _ + rw [nd_bind_oneSc (by rw [nd_bind_oneSc (loadWordOffs_eqA (specPtr frame) vrVgicVmcr σ4 (al3_of_frameWinA X4.fmem hWin vrVgicVmcr (by unfold vrVgicVmcr; decide))), ← hw_vmcr]; exact hset5)]; rfl)] + rw [nd_bind_assocSc] + rw [hvmcr, hap00, hap03] at hsp6 + rw [nd_bind_oneSc hsp6, nd_bind_oneSc hsp7]; exact hsp8 + have hrs_lt48 : ∀ r ∈ ([vrVgicVmcr] ++ idxRange vrVgicAp0r0 vrVgicAp0r3 ++ + if fl4GicV3 then idxRange vrVgicAp1r0 vrVgicAp1r3 else []), r < 48 := by + intro r hr + cases fl4GicV3 <;> simp [idxRange, vrVgicVmcr, vrVgicAp0r0, vrVgicAp0r3, vrVgicAp1r0, vrVgicAp1r3] at hr <;> omega + have hcomm := copyRegsFromFrame_lrs_commA hkv4 hrs_lt48 + (fun r hr => al3_of_frameWinA X4.fmem hWin r (by have := hrs_lt48 r hr; omega)) hlrs_b + rw [← hregs_all] at hcomm hstep_comb + have hcomm_s4 : (NonDet.mapMx (fun (i, w) => fl4VcpuSet (specPtr vc) (vrVgicLr0 + i) w) ((List.range fl4NumLrs).zip vgicSpec.2) >>= fun _ => do + copyRegsFromFrame (specPtr vc) (specPtr frame) [vrVgicVmcr] + copyRegsFromFrame (specPtr vc) (specPtr frame) (idxRange vrVgicAp0r0 vrVgicAp0r3) + NonDet.when fl4GicV3 <| copyRegsFromFrame (specPtr vc) (specPtr frame) (idxRange vrVgicAp1r0 vrVgicAp1r3)) σ4 = + ({((), σ8)}, False) := hcomm.symm.trans hstep_comb + have hcomm_k : ∀ (k : Unit → SMonad Unit Unit), + (NonDet.mapMx (fun (i, w) => fl4VcpuSet (specPtr vc) (vrVgicLr0 + i) w) ((List.range fl4NumLrs).zip vgicSpec.2) >>= fun _ => + copyRegsFromFrame (specPtr vc) (specPtr frame) [vrVgicVmcr] >>= fun _ => + copyRegsFromFrame (specPtr vc) (specPtr frame) (idxRange vrVgicAp0r0 vrVgicAp0r3) >>= fun _ => + NonDet.when fl4GicV3 (copyRegsFromFrame (specPtr vc) (specPtr frame) (idxRange vrVgicAp1r0 vrVgicAp1r3)) >>= k) σ4 = + k () σ8 := by + intro k + have hlhs : (NonDet.mapMx (fun (i, w) => fl4VcpuSet (specPtr vc) (vrVgicLr0 + i) w) ((List.range fl4NumLrs).zip vgicSpec.2) >>= fun _ => + copyRegsFromFrame (specPtr vc) (specPtr frame) [vrVgicVmcr] >>= fun _ => + copyRegsFromFrame (specPtr vc) (specPtr frame) (idxRange vrVgicAp0r0 vrVgicAp0r3) >>= fun _ => + NonDet.when fl4GicV3 (copyRegsFromFrame (specPtr vc) (specPtr frame) (idxRange vrVgicAp1r0 vrVgicAp1r3)) >>= k) = + ((NonDet.mapMx (fun (i, w) => fl4VcpuSet (specPtr vc) (vrVgicLr0 + i) w) ((List.range fl4NumLrs).zip vgicSpec.2) >>= fun _ => do + copyRegsFromFrame (specPtr vc) (specPtr frame) [vrVgicVmcr] + copyRegsFromFrame (specPtr vc) (specPtr frame) (idxRange vrVgicAp0r0 vrVgicAp0r3) + NonDet.when fl4GicV3 <| copyRegsFromFrame (specPtr vc) (specPtr frame) (idxRange vrVgicAp1r0 vrVgicAp1r3)) >>= k) := by + simp only [hbind_assoc] + rw [hlhs, nd_bind_oneSc hcomm_s4] + have hset4' : fl4VcpuSet (specPtr vc) vrVgicHcr vgicSpec.1 σ3 = ({((), σ4)}, False) := by + rw [← hvg1]; dsimp only; rw [← hvhcr]; exact hset4 + dsimp only + rw [if_pos h4b, nd_bind_assocSc, nd_bind_oneSc hset4', hcomm_k, + nd_bind_oneSc (getVcpu_eqA hkv8), nd_bind_oneSc (setVcpu_eqA hkv8)] + exact hsp' + · have h4b : ¬ (mask.bv &&& smVgic ≠ 0) := fun hne => h4 (bne_iff_ne.2 (fun e => hne (congrArg UScalar.bv e))) + rw [if_neg h4, sm_vgic_lr_ok, bind_ok, lift_ok, bind_ok] + by_cases h5 : (mask &&& 32#u64 != 0#u64) = true + · have h5b : mask.bv &&& smVgicLr ≠ 0 := fun e => by + have : mask &&& 32#u64 = 0#u64 := u64_eq_of_bv_eq e; simp [this] at h5 + have hvg_or : mask.bv &&& (smVgic ||| smVgicLr) ≠ 0 := by + intro hz; apply h5b + have : (mask.bv &&& (smVgic ||| smVgicLr)) &&& smVgicLr = 0 := by rw [hz]; simp + simpa [BitVec.and_assoc, smVgic, smVgicLr] using this + obtain ⟨hvg1, hvg1_u32, hvg2_len, hvg2_arr⟩ := hVgic hvg_or + rw [if_pos h5, vcpu_num_lrs_eqA hP a, bind_ok] + obtain ⟨g, hga, hgb⟩ := hP.gic a + rw [hga, bind_ok] + have hdec : (decide (g = 3#u64)) = fl4GicV3 := hgb + rcases vgic with ⟨vgHcr, vgArr⟩ + have hvhcr : fl4_abi.consts.vcpu_reg.VGIC_HCR.val = vrVgicHcr := by + unfold fl4_abi.consts.vcpu_reg.VGIC_HCR vrVgicHcr; rfl + obtain ⟨v3, hkv3, -, -⟩ := X3.vcpu.core.obj vc hvc + refine WP.spec_bind (vcpu_reg_wr_oneA X3 hU hCs (hcpu3 ▸ hcpu) hExt hSw hfa3 hsub hIdle3 hvc hkv3 + (by rw [hvhcr]; unfold vrVgicHcr; decide) vgHcr (fun _ => by rw [hvg1]; exact hvg1_u32)) ?_ + rintro s4 ⟨hset4, X4, hms4, hksb4, hcpu4, hfa4, hIdle4⟩ + -- `loop2` is definitionally identical to `loop0`! + have hloop2 : vcpu.write_state_from_frame_loop2 st s4 vc vgArr ⟨BitVec.ofNat 64 fl4NumLrs⟩ 0#u64 = + vcpu.write_state_from_frame_loop0 st s4 vc vgArr ⟨BitVec.ofNat 64 fl4NumLrs⟩ 0#u64 := by + unfold vcpu.write_state_from_frame_loop2 vcpu.write_state_from_frame_loop0; congr 1 + rw [hloop2] + refine WP.spec_bind (write_lrs_loop_refinesA X4 hU hCs (hcpu4.trans hcpu3 ▸ hcpu) hExt hSw hfa4 hsub hIdle4 hvc + hvg2_len hvg2_arr) ?_ + rintro s5 ⟨σ5, hsp5, X5, hms5, hksb5, hcpu5, hfa5, hIdle5⟩ + obtain ⟨v5, hkv5, -, -⟩ := X5.vcpu.core.obj vc hvc + rw [hdec] + refine WP.spec_bind (sync_vgic_status_refinesA X5.vcpu X5.j.1.ksFit hCs hIdle5 hvc hkv5) ?_ + rintro s6 ⟨hV6, hW6⟩ + have hsync_meta := syncVgicStatus_pres_metaA v5 fl4NumLrs fl4GicV3 + have X6 := X5.vc_nonlink_wr hCs (hcpu5.trans (hcpu4.trans hcpu3) ▸ hcpu) hExt hSw hfa5 hsub hU hW6 + (fun b hb => by + rcases hb with rfl | rfl | rfl + · exact ⟨vcpuRegWA_in_vcPage hvc (by unfold vrVgicElrsr; decide), fun q hq => vcpuRegWA_ne_hdrA X5.vcpu.place hvc (by unfold vrVgicElrsr; decide) hq⟩ + · exact ⟨vcpuRegWA_in_vcPage hvc (by unfold vrVgicEisr; decide), fun q hq => vcpuRegWA_ne_hdrA X5.vcpu.place hvc (by unfold vrVgicEisr; decide) hq⟩ + · exact ⟨vcpuRegWA_in_vcPage hvc (by unfold vrVgicMisr; decide), fun q hq => vcpuRegWA_ne_hdrA X5.vcpu.place hvc (by unfold vrVgicMisr; decide) hq⟩) + hvc hkv5 hsync_meta.1 hsync_meta.2.2.1 hV6 + have hfa6 : ∀ q, FootA (L := L) Z s6 q → Aligned q := fun q hq => + hfa5 q ((FootA_of_ksb_cpuA Z hW6.1 hW6.2.1 q).1 hq) + have hIdle6 : ∀ c ∈ Z.Cs, L.mem s6 (L.nodeAt s6 c layout.node.IDLE_THREAD) ∈ Z.T := by + intro c hc + have hks6 : L.ksb s6 = L.ksb s5 := hW6.1 + have hnd6 : L.nodeAt s6 c layout.node.IDLE_THREAD = L.nodeAt s5 c layout.node.IDLE_THREAD := + nodeAt_of_ksb hks6 c layout.node.IDLE_THREAD + have hal_idle : Aligned (L.nodeAt s5 c layout.node.IDLE_THREAD) := + hfa5 _ (FootA.nslot _ (FootNA.node c _ hc (Or.inr (Or.inl rfl)))) + rw [hnd6, hW6.2.2 _ hal_idle (fun h => by + rcases h with h | h | h + · exact not_footA_of_vcNonLinkA X5.vcpu.place X5.j.1.ksFit hCs (hcpu5.trans (hcpu4.trans hcpu3) ▸ hcpu) hExt hSw + (h ▸ vcpuRegWA_in_vcPage hvc (by unfold vrVgicElrsr; decide)) + (fun q hq => h ▸ vcpuRegWA_ne_hdrA X5.vcpu.place hvc (by unfold vrVgicElrsr; decide) hq) + (FootA.nslot _ (FootNA.node c _ hc (Or.inr (Or.inl rfl)))) + · exact not_footA_of_vcNonLinkA X5.vcpu.place X5.j.1.ksFit hCs (hcpu5.trans (hcpu4.trans hcpu3) ▸ hcpu) hExt hSw + (h ▸ vcpuRegWA_in_vcPage hvc (by unfold vrVgicEisr; decide)) + (fun q hq => h ▸ vcpuRegWA_ne_hdrA X5.vcpu.place hvc (by unfold vrVgicEisr; decide) hq) + (FootA.nslot _ (FootNA.node c _ hc (Or.inr (Or.inl rfl)))) + · exact not_footA_of_vcNonLinkA X5.vcpu.place X5.j.1.ksFit hCs (hcpu5.trans (hcpu4.trans hcpu3) ▸ hcpu) hExt hSw + (h ▸ vcpuRegWA_in_vcPage hvc (by unfold vrVgicMisr; decide)) + (fun q hq => h ▸ vcpuRegWA_ne_hdrA X5.vcpu.place hvc (by unfold vrVgicMisr; decide) hq) + (FootA.nslot _ (FootNA.node c _ hc (Or.inr (Or.inl rfl)))))] + exact hIdle5 c hc + -- `loop3` is definitionally identical to `loop1`! + have hloop3 : ∀ (s0 : S) (gb tp idx : U64), + vcpu.write_state_from_frame_loop3 st s0 gb tp idx = + vcpu.write_state_from_frame_loop1 st s0 gb tp idx := fun s0 gb tp idx => by + unfold vcpu.write_state_from_frame_loop3 vcpu.write_state_from_frame_loop1; congr 1 + simp only [hloop3] + refine WP.spec_mono (hGprs_step s6 _ X6 + (hW6.1.trans (hksb5.trans (hksb4.trans hksb3))) + (hW6.2.1.trans (hcpu5.trans (hcpu4.trans hcpu3))) hfa6 hIdle6) ?_ + rintro s' ⟨σ', hsp', X'⟩ + refine ⟨σ', ?_, X'⟩ + unfold fl4WriteStateFromFrame + rw [nd_bind_oneSc hsp1, nd_bind_oneSc hsp2, nd_bind_oneSc hsp3, if_neg h4b] + rw [hvhcr] at hset4 + have hfn_lr : (fun (i, w) => fl4VcpuSet (Z := Unit) (specPtr vc) (vrVgicLr0 + i) w) = + (fun p : Nat × Word => fl4VcpuSet (Z := Unit) (specPtr vc) (vrVgicLr0 + p.1) p.2) := by + funext ⟨i, w⟩; rfl + rw [hfn_lr, hvhcr] at hsp5 + rw [nd_bind_oneSc (by + unfold NonDet.when; rw [if_pos h5b, ← hvg1]; dsimp only + rw [nd_bind_assocSc, nd_bind_oneSc hset4, nd_bind_oneSc hsp5, + nd_bind_oneSc (getVcpu_eqA hkv5), setVcpu_eqA hkv5])] + exact hsp' + · have h5b : ¬ (mask.bv &&& smVgicLr ≠ 0) := fun hne => h5 (bne_iff_ne.2 (fun e => hne (congrArg UScalar.bv e))) + rw [if_neg h5] + -- `loop4` is definitionally identical to `loop1`! + have hloop4 : ∀ (s0 : S) (gb tp idx : U64), + vcpu.write_state_from_frame_loop4 st s0 gb tp idx = + vcpu.write_state_from_frame_loop1 st s0 gb tp idx := fun s0 gb tp idx => by + unfold vcpu.write_state_from_frame_loop4 vcpu.write_state_from_frame_loop1; congr 1 + simp only [hloop4] + refine WP.spec_mono (hGprs_step s3 σ3 X3 hksb3 hcpu3 hfa3 hIdle3) ?_ + rintro s' ⟨σ', hsp', X'⟩ + refine ⟨σ', ?_, X'⟩ + unfold fl4WriteStateFromFrame + rw [nd_bind_oneSc hsp1, nd_bind_oneSc hsp2, nd_bind_oneSc hsp3, if_neg h4b] + rw [nd_bind_oneSc (by unfold NonDet.when; rw [if_neg h5b]; rfl)] + exact hsp' + +end write_state + + +end +end Fl4ProofsA64.P3.Vcpu diff --git a/proofs/Fl4ProofsA64/P3/VsArmA.lean b/proofs/Fl4ProofsA64/P3/VsArmA.lean new file mode 100644 index 000000000..f06655dee --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/VsArmA.lean @@ -0,0 +1,43 @@ +/- +P3 (aarch64, A64-P3 S3 gen 30, ruling 01:13Z item 3, first step): **the VSpace arm of `decode_invocation`** — +FL4's `syscall::decode_invocation` on a frame or page-table cap is `vspace::decode`'s arm for it, i.e. exactly the +decoders the map / unmap tops are stated on (`vspace.decode_frame`: `page_map_jA` / `page_map_jTA`, +`page_unmap_decJA`, `page_unmapU_decJA`, `page_unmapS1_decJA`, `page_unmapZ_decJA`, `page_unmapS2U_decJA`; +`vspace.decode_page_table`: `pt_map_jA` / `pt_map_jTA`, `pt_unmap_decJA`, `pt_unmapU_decJA`, `pt_unmapS1_decJA`). + +* **`decInv_frameA`**, **`decInv_ptA`**: the two equations (`cur_thread` read twice, no state change before the + arch decoder). The x86 counterpart is the `vspace.decode` dispatch of `P3/VsDecTop`. + +So every VSpace top is a statement about `decode_invocation` after one rewrite. No named premise; new module, no +existing statement changed. +-/ +import Fl4ProofsA64.P3.VsMapJA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-- **`decode_invocation` on a frame cap is `vspace::decode_frame`** (module docstring). -/ +theorem decInv_frameA (ar : machine.Arch A) (s : S) (a : A) (lbl length : U64) (slot : obj.Slot) + (p : U64) (sz : capability.FrameSize) (rt : U64) (d m e : Bool) (asid va : U64) (block call cd fp : Bool) + (buf : U64) (extra : Aeneas.Std.Array obj.Slot 3#usize) : + syscall.decode_invocation st ar s a lbl length slot (.Frame p sz rt d m e asid va) block call cd fp buf extra = + vspace.decode_frame st ar s a lbl length slot (.Frame p sz rt d m e asid va) call buf extra := by + unfold syscall.decode_invocation vspace.decode + simp only [cur_thread_ok, bind_ok] + +/-- **`decode_invocation` on a page-table cap is `vspace::decode_page_table`** (module docstring). -/ +theorem decInv_ptA (ar : machine.Arch A) (s : S) (a : A) (lbl length : U64) (slot : obj.Slot) + (p : U64) (m : Bool) (asid va : U64) (block call cd fp : Bool) + (buf : U64) (extra : Aeneas.Std.Array obj.Slot 3#usize) : + syscall.decode_invocation st ar s a lbl length slot (.PageTable p m asid va) block call cd fp buf extra = + vspace.decode_page_table st ar s a lbl length slot (.PageTable p m asid va) buf extra := by + unfold syscall.decode_invocation vspace.decode + simp only [cur_thread_ok, bind_ok] + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/VsAssignA.lean b/proofs/Fl4ProofsA64/P3/VsAssignA.lean new file mode 100644 index 000000000..ab91fad9b --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/VsAssignA.lean @@ -0,0 +1,1476 @@ +/- +P3 (aarch64): ARMASIDPoolAssign from syscall decode through the cap and pool writes. +The common search returns the concrete first free offset. The abstract decoder +may select that offset, and the shared perform proof keeps JAtZAX and CarryA. +The three arms are Pml4, level-zero Ept and HighVSpace. The cap-kind top also +covers missing or ineligible extras and illegal labels. + +Modelled on x86 P3/VsAssign and P3/VsAssignRel. No new named premises. +-/ +import Fl4ProofsA64.P3.VsTopRootA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section Search +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- FL4's search for a free pool entry: the first `i < 512` with +`base + i ≠ 0` and entry `i` empty, or 512. -/ +theorem assign_searchA (s : S) (base pool : U64) : + vspace.decode_loop1 st s base pool 0#u64 ⦃ i1 => i1.val ≤ 512 ∧ + (∀ j : U64, j.val < i1.val → core.num.U64.wrapping_add base j = 0#u64 ∨ L.mem s (peAF pool j) ≠ 0#u64) ∧ + (i1.val < 512 → core.num.U64.wrapping_add base i1 ≠ 0#u64 ∧ L.mem s (peAF pool i1) = 0#u64) ⦄ := by + unfold vspace.decode_loop1 + apply loop.spec_decr_nat (measure := fun i => 512 - i.val) + (inv := fun i => i.val ≤ 512 ∧ + ∀ j : U64, j.val < i.val → core.num.U64.wrapping_add base j = 0#u64 ∨ L.mem s (peAF pool j) ≠ 0#u64) + · rintro i ⟨hi5, hall⟩ + unfold vspace.decode_loop1.body + by_cases h1 : i < 512#u64 + · have hi : i.val < 512 := by have := (gt_iff_val).1 h1; simpa using this + simp only [h1, ↓reduceIte] + simp only [lift, bind_ok, bind_tc_ok] + have ext : ∀ j : U64, j.val < i.val + 1 → j.val < i.val ∨ j = i := fun j hj => by + by_cases e : j.val = i.val + · exact Or.inr (UScalar.eq_of_val_eq e) + · exact Or.inl (by omega) + by_cases h0 : core.num.U64.wrapping_add base i = 0#u64 + · simp only [h0, ↓reduceIte] + step as ⟨i2, hi2⟩ + have hi2' : i2.val = i.val + 1 := by simpa using hi2 + refine ⟨by omega, fun j hj => ?_, by omega⟩ + rcases ext j (by omega) with h | rfl + · exact hall j h + · exact Or.inl h0 + · simp only [h0, ↓reduceIte] + step as ⟨i2, hi2⟩ + step as ⟨v, hv⟩ + have he : core.num.U64.wrapping_add pool i2 = peAF pool i := by + unfold peAF + congr 1 + exact UScalar.eq_of_val_eq (by rw [hi2, wm8_valA hi]; omega) + rw [he] at hv + by_cases hv0 : v = 0#u64 + · simp only [hv0, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + exact ⟨hi5, hall, fun _ => ⟨h0, hv ▸ hv0⟩⟩ + · have hne : (v != 0#u64) = true := by simp [hv0] + simp only [hne, ↓reduceIte] + step as ⟨i5, hi5'⟩ + have hi5v : i5.val = i.val + 1 := by simpa using hi5' + refine ⟨by omega, fun j hj => ?_, by omega⟩ + rcases ext j (by omega) with h | rfl + · exact hall j h + · exact Or.inr (hv ▸ hv0) + · simp only [h1, ↓reduceIte, WP.spec_ok] + have hi : i.val = 512 := by + have : ¬ i.val < 512 := fun h => h1 ((gt_iff_val).2 (by simpa using h)) + omega + exact ⟨hi5, hall, fun h => by omega⟩ + · exact ⟨by simp, fun j hj => by simp at hj⟩ + +/-- The Stage-2 and high-window branches perform the same search as Stage 1. -/ +theorem assign_searchLoopsA (s : S) (base pool start : U64) : + vspace.decode_loop2 st s base pool start = vspace.decode_loop1 st s base pool start ∧ + vspace.decode_loop3 st s base pool start = vspace.decode_loop1 st s base pool start := + ⟨rfl, rfl⟩ + +end Search + +/-- The selected ASID fits l4v's 16 bits and its low nine bits recover the offset FL4 searched. -/ +theorem assign_sumA {p base i : U64} (hbase : CapWfA (.AsidPool p base)) (hi : i.val < 512) : + (wa base i).val = base.val + i.val ∧ (wa base i).val < 2 ^ 16 ∧ (wa base i &&& 511#u64) = i := by + obtain ⟨hb, hal⟩ := hbase + have hb16 : base.val < 65536 := hb + have hv : base.val + i.val < 65536 := by omega + have hsum := wadd_val (x := base) (y := i) (by omega : base.val + i.val < 2 ^ 64) + refine ⟨hsum, by rw [hsum]; exact hv, ?_⟩ + apply UScalar.eq_of_val_eq + rw [and_maskA _ 9 _ rfl, hsum] + change (base.val + i.val) % 512 = i.val + omega + +/-- l4v computes the same ASID as FL4 for a free offset of an aligned pool. -/ +theorem assign_asidA {p base i : U64} (hbase : CapWfA (.AsidPool p base)) (hi : i.val < 512) : + Fl4SpecA64.ucast Fl4SpecA64.asidRepLen (BitVec.ofNat 9 i.val) + asidOfA base = asidOfA (wa base i) := by + have ⟨hsum, _, _⟩ := assign_sumA hbase hi + have hb : base.val < 65536 := hbase.1 + apply BitVec.eq_of_toNat_eq + simp only [Fl4SpecA64.ucast, asidOfA, BitVec.toNat_add, BitVec.toNat_setWidth, BitVec.toNat_ofNat] + change ((i.val % 512) % 65536 + base.val % 65536) % 65536 = (wa base i).val % 65536 + rw [Nat.mod_eq_of_lt hi, Nat.mod_eq_of_lt (show i.val < 65536 by omega), + Nat.mod_eq_of_lt hb, hsum, Nat.add_comm i.val] + +section Select +open Fl4SpecA64 + +/-- l4v's eligible offsets in a pool. -/ +def assignFreeA (f : AsidPool) (base : Asid) : Set AsidLowIndex := + (dom f)ᶜ ∩ {x | (ucast asidRepLen x : Asid) + base ≠ 0} + +/-- Both decoders accept the same offset when the pool entries agree. -/ +theorem assign_free_iffA {p base i : U64} {mem : U64 → U64} {f : AsidPool} + (hbase : CapWfA (.AsidPool p base)) (hi : i.val < 512) + (hpool : f (BitVec.ofNat 9 i.val) = poolEntA (mem (peAF p i))) : + BitVec.ofNat 9 i.val ∈ assignFreeA f (asidOfA base) ↔ + wa base i ≠ 0#u64 ∧ mem (peAF p i) = 0#u64 := by + change (¬ f (BitVec.ofNat 9 i.val) ≠ none ∧ + ucast asidRepLen (BitVec.ofNat 9 i.val) + asidOfA base ≠ 0) ↔ _ + rw [assign_asidA hbase hi, hpool] + have h16 := (assign_sumA hbase hi).2.1 + have ha : asidOfA (wa base i) ≠ 0 ↔ wa base i ≠ 0#u64 := by + constructor + · intro h e + apply h + rw [e] + rfl + · intro h e + apply h + apply UScalar.eq_of_val_eq + have he := congrArg BitVec.toNat e + change (BitVec.ofNat 16 (wa base i).val).toNat = 0 at he + rwa [BitVec.toNat_ofNat, Nat.mod_eq_of_lt h16] at he + rw [ha] + unfold poolEntA + by_cases e : mem (peAF p i) = 0#u64 <;> simp [e] + +/-- If FL4 exhausts the pool, l4v's selection set is empty. -/ +theorem assign_free_emptyA {p base : U64} {mem : U64 → U64} {f : AsidPool} + (hbase : CapWfA (.AsidPool p base)) + (hp : ∀ i : U64, i.val < 512 → f (BitVec.ofNat 9 i.val) = poolEntA (mem (peAF p i))) + (hall : ∀ j : U64, j.val < 512 → wa base j = 0#u64 ∨ mem (peAF p j) ≠ 0#u64) : + assignFreeA f (asidOfA base) = ∅ := by + apply Set.eq_empty_iff_forall_notMem.2 + intro off hoff + let i : U64 := ⟨BitVec.ofNat 64 off.toNat⟩ + have hlt : off.toNat < 512 := off.isLt + have hv : i.val = off.toNat := by + change (BitVec.ofNat 64 off.toNat).toNat = _ + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)] + have hi : i.val < 512 := hv ▸ hlt + have he : BitVec.ofNat 9 i.val = off := by + apply BitVec.eq_of_toNat_eq + rw [BitVec.toNat_ofNat, hv, Nat.mod_eq_of_lt hlt] + have ⟨ha, hw⟩ := (assign_free_iffA hbase hi (hp i hi)).1 (by rw [he]; exact hoff) + exact (hall i hi).elim ha (fun h => h hw) + +end Select + +section Write +open Fl4SpecA64 +variable {Pp Tb X Y : Set U64} {Lv : U64 → Nat} {mem mem' : U64 → U64} + {ksb p i word : U64} {σ : State Unit} + +/-- The spec's matching pool write. -/ +def poolSetA (σ : State Unit) (p : U64) (f : AsidPool) : State Unit := + setObjA σ (specPtr p) (.archObj (.asidPool f)) + +theorem poolSetA_get {f : AsidPool} {q : U64} (h : q ≠ p) : + (poolSetA σ p f).kheap (specPtr q) = σ.kheap (specPtr q) := by + simp [poolSetA, setObjA, Function.update_of_ne (fun e => h (specPtr_inj e))] + +theorem getPool_runA {p : ObjRef} {f : AsidPool} (hk : σ.kheap p = some (.archObj (.asidPool f))) : + getAsidPool (Z := Unit) p σ = ({(f, σ)}, False) := by + show (Fl4Spec.NonDet.gets (asidPoolsOf (Z := Unit)) >>= fun m => Fl4Spec.NonDet.assertOpt (m p)) σ = _ + rw [lfA_bind_one (s' := σ) (a := asidPoolsOf σ) rfl] + show Fl4Spec.NonDet.assertOpt (asidPoolsOf σ p) σ = _ + rw [asidPoolsOf_eqA hk] + rfl + +theorem storePool_runA {f : AsidPool} (hk : σ.kheap (specPtr p) = some (.archObj (.asidPool f))) + (asid : Asid) (ent : Option AsidPoolEntry) : + storeAsidPoolEntry (Z := Unit) (specPtr p) asid ent σ = + ({((), poolSetA σ p (Function.update f (asidLowBitsOf asid) ent))}, False) := by + unfold storeAsidPoolEntry + rw [lfA_bind_one (getPool_runA hk)] + try beta_reduce + rw [lfA_pure_bind] + try beta_reduce + unfold setAsidPool + rw [lfA_bind_one (getPool_runA hk)] + try beta_reduce + unfold setObject + rw [lfA_bind_one (getObject_runA hk)] + try beta_reduce + rw [lfA_bind_one (lfA_assert (show aType (.archObj (.asidPool f)) = + aType (.archObj (.asidPool (Function.update f (asidLowBitsOf asid) ent))) from rfl) σ)] + try beta_reduce + rw [lfA_bind_one (show (Fl4Spec.NonDet.get : Fl4Spec.NonDet (State Unit) _) σ = ({(σ, σ)}, False) from rfl)] + rfl + +/-- A pool entry cannot be a stage-1 table word, a window word or a table-index word. -/ +theorem VsRelA.pool_offA (R : VsRelA Pp Tb Lv X Y mem ksb σ) + (hY : ∀ y ∈ Y, wa y 4096#u64 ∉ Pp) (hp : p ∈ Pp) + (hi : i.val < 512) (hA : ¬ AsW ksb (peAF p i)) : + ∀ b, VsFootA ∅ Tb Y ksb b → b ≠ peAF p i := by + have ha := Nat.mod_eq_zero_of_dvd (R.pal p hp) + intro b hb he + cases hb with + | asid j hj => exact hA (he ▸ atAF_asWA hj) + | pool q j hq hj => exact Set.notMem_empty q hq + | pt q j hq hj => + have e := (peAF_injA (R.al_mod hq) ha hj hi he).1 + obtain ⟨f, hf, _⟩ := R.pool p hp + obtain ⟨t, ht, _, _⟩ := R.pt q hq + rw [e, hf] at ht + cases ht + | high y j hy hj => + have e := (peAF_injA (waWin_modA (R.yal y hy)) ha hj hi he).1 + exact hY y hy (e ▸ hp) + +/-- Unmapped VSpace root hypothesis for `ARMASIDPoolAssign` (discharged at kernel entry by `UnmPtSpecA`'s `.vs`, `.s2`, +and `.hv` fields). -/ +def UnmRootAssignA (k : ExtRootKind) (mem : U64 → U64) (σ : State Unit) (root : U64) : Prop := + match k with + | .stage1 => + (∀ i : U64, i.val < 512 → dBitA (mem (peAF root i)) 0 = false) ∧ + ∃ t, σ.kheap (specPtr root) = some (.archObj (.pageTable t)) ∧ + ∀ i : U64, i.val < 512 → ptApply t i.bv = .invalidPTE + | .stage2 => + (∀ i : U64, i.val < 1024 → dBitA (mem (peAF root i)) 0 = false) ∧ + ∃ es, σ.kheap (specPtr root) = some (.archObj (.ext (.stage2Root es))) ∧ + ∀ i : Fin 1024, es i = .invalidPTE + | .high => + (∀ i : U64, i.val < 512 → dBitA (mem (peAF (wa root 4096#u64) i)) 0 = false) ∧ + ∃ l1, σ.kheap (specPtr root) = some (.archObj (.ext (.highVSpace l1))) ∧ + ∀ i : Fin 512, l1 i = .invalidPTE + +/-- Pool update preserves the VSpace relation when the write avoids the reserved zero entry and any newly installed root +is empty. -/ +theorem VsRelA.of_storePoolA (R : VsRelA Pp Tb Lv X Y mem ksb σ) {f : AsidPool} + (hp : p ∈ Pp) (hi : i.val < 512) (hk : σ.kheap (specPtr p) = some (.archObj (.asidPool f))) + (hKind : EntKindA Tb Lv X Y word) + (hZero : mem (atAF ksb 0#u64) = p → i ≠ 0#u64) + (hY : ∀ y ∈ Y, wa y 4096#u64 ∉ Pp) (hA : ¬ AsW ksb (peAF p i)) + (hm : ∀ b, VsFootA Pp Tb Y ksb b → b ≠ peAF p i → mem' b = mem b) + (hwrite : mem' (peAF p i) = word) + (hunm : (tagA word = 0 → word ≠ 0#u64 → + ((∀ j : U64, j.val < 512 → dBitA (mem (peAF (untagA word) j)) 0 = false) ∧ + ∀ t, σ.kheap (specPtr (untagA word)) = some (.archObj (.pageTable t)) → + ∀ j : U64, j.val < 512 → ptApply t j.bv = .invalidPTE)) ∧ + (tagA word = 2 → + ((∀ j : U64, j.val < 512 → dBitA (mem (peAF (wa (untagA word) 4096#u64) j)) 0 = false) ∧ + ∀ l1, σ.kheap (specPtr (untagA word)) = some (.archObj (.ext (.highVSpace l1))) → + ∀ j : Fin 512, l1 j = .invalidPTE))) : + VsRelA Pp Tb Lv X Y mem' ksb (poolSetA σ p (Function.update f (BitVec.ofNat 9 i.val) (poolEntA word))) := by + have ha := Nat.mod_eq_zero_of_dvd (R.pal p hp) + have hword : ∀ q ∈ Pp, ∀ j : U64, j.val < 512 → peAF q j = peAF p i → q = p ∧ j = i := + fun q hq j hj => peAF_injA (Nat.mod_eq_zero_of_dvd (R.pal q hq)) ha hj hi + have htab : ∀ j : U64, j.val < 128 → mem' (atAF ksb j) = mem (atAF ksb j) := + fun j hj => hm _ (.asid j hj) (R.pool_offA hY hp hi hA _ (.asid j hj)) + have hActP : ∀ {q}, ActivePoolA mem' ksb q → ActivePoolA mem ksb q := by + rintro q ⟨hqne, hj, hhj, htab'⟩ + rw [htab hj hhj] at htab' + exact ⟨hqne, hj, hhj, htab'⟩ + have hActR : ∀ {asid raw}, ActiveAsidRootA mem' ksb Pp asid raw → + ActiveAsidRootA mem ksb Pp asid raw ∨ (raw = word ∧ word ≠ 0#u64) := by + rintro asid raw ⟨hj, lo, hhj, hlo, ha', pool, hpool, hpne, htab', hpe, hrne⟩ + rw [htab hj hhj] at htab' + by_cases he : peAF pool lo = peAF p i + · rw [he, hwrite] at hpe + exact Or.inr ⟨hpe.symm, hpe ▸ hrne⟩ + · rw [hm _ (.pool pool lo hpool hlo) he] at hpe + exact Or.inl ⟨hj, lo, hhj, hlo, ha', pool, hpool, hpne, htab', hpe, hrne⟩ + have hActH : ∀ {y}, ActiveHighRootA mem' ksb Pp y → + ActiveHighRootA mem ksb Pp y ∨ (tagA word = 2 ∧ y = untagA word) := by + rintro y ⟨asid, raw, ha', htag, hy⟩ + rcases hActR ha' with ha0 | ⟨rfl, -⟩ + · exact Or.inl ⟨asid, raw, ha0, htag, hy⟩ + · exact Or.inr ⟨htag, hy.symm⟩ + have hPath : ∀ {tag sh q}, VsPathA Pp Tb Y mem' ksb tag sh q → + VsPathA Pp Tb Y mem ksb tag sh q ∨ + (tagA word = 0 ∧ word ≠ 0#u64 ∧ q = untagA word) ∨ + (tagA word = 2 ∧ q = wa (untagA word) 4096#u64) := by + intro tag sh q hpth + induction hpth with + | native ha' htag htb => + rcases hActR ha' with ha0 | ⟨rfl, hwne⟩ + · exact Or.inl (.native ha0 htag htb) + · exact Or.inr (Or.inl ⟨htag, hwne, (untagA_of htag).symm⟩) + | high ha' htag hy' => + rcases hActR ha' with ha0 | ⟨rfl, -⟩ + · exact Or.inl (.high ha0 htag hy') + · exact Or.inr (Or.inr ⟨htag, rfl⟩) + | @child tag tb j sh _ htY h12 hj h3 ih => + have hfoot : VsFootA ∅ Tb Y ksb (peAF tb j) := by + rcases htY with htb | ⟨y, hy', rfl⟩ + · exact .pt tb j htb hj + · exact .high y j hy' hj + have hfootP : VsFootA Pp Tb Y ksb (peAF tb j) := by + rcases htY with htb | ⟨y, hy', rfl⟩ + · exact .pt tb j htb hj + · exact .high y j hy' hj + rw [hm _ hfootP (R.pool_offA hY hp hi hA _ hfoot)] at h3 ⊢ + rcases ih with hpar | ⟨ht0, hwne, rfl⟩ | ⟨ht2, rfl⟩ + · exact Or.inl (.child hpar htY h12 hj h3) + · have hb0 := ((and3_bitsA _).1 h3).1 + exact absurd ((hunm.1 ht0 hwne).1 j hj) (by rw [hb0]; decide) + · have hb0 := ((and3_bitsA _).1 h3).1 + exact absurd ((hunm.2 ht2).1 j hj) (by rw [hb0]; decide) + refine { R with + table := fun j hj => by rw [htab j hj]; exact R.table j hj + tmem := fun j hj hn => by rw [htab j hj] at hn ⊢; exact R.tmem j hj hn + pool := ?_, kind := ?_, asid0 := ?_, pt := ?_, high := ?_ } + · intro q hq + obtain ⟨g, hkg, hg⟩ := R.pool q hq + by_cases eq : q = p + · subst q + have eqg : g = f := by rw [hk] at hkg; cases hkg; rfl + subst g + refine ⟨Function.update f (BitVec.ofNat 9 i.val) (poolEntA word), ?_, fun ha' j hj => ?_⟩ + · simp [poolSetA, setObjA] + · by_cases e : j = i + · subst j + rw [Function.update_self, hwrite] + · have ec : BitVec.ofNat 9 j.val ≠ BitVec.ofNat 9 i.val := by + intro he + have h := congrArg BitVec.toNat he + rw [BitVec.toNat_ofNat, BitVec.toNat_ofNat, Nat.mod_eq_of_lt hj, Nat.mod_eq_of_lt hi] at h + exact e (UScalar.eq_of_val_eq h) + rw [Function.update_of_ne ec, hg (hActP ha') j hj, hm _ (.pool p j hp hj) + (fun he => e (hword p hp j hj he).2)] + · exact ⟨g, (poolSetA_get eq).trans hkg, fun ha' j hj => by + rw [hm _ (.pool q j hq hj) (fun he => eq (hword q hq j hj he).1)] + exact hg (hActP ha') j hj⟩ + · intro q hq ha' j hj + by_cases e : peAF q j = peAF p i + · rw [e, hwrite]; exact hKind + · rw [hm _ (.pool q j hq hj) e]; exact R.kind q hq (hActP ha') j hj + · intro h0 + rw [htab 0#u64 (by decide)] at h0 ⊢ + have hq := R.tmem 0#u64 (by decide) h0 + rw [hm _ (.pool _ 0#u64 hq (by decide)) (fun e => by + obtain ⟨eq, ei⟩ := hword _ hq _ (by decide) e + exact hZero eq ei.symm)] + exact R.asid0 h0 + · intro q hq + obtain ⟨t, ht, hty, he⟩ := R.pt q hq + have hn : q ≠ p := fun h => by rw [h, hk] at ht; cases ht + refine ⟨t, (poolSetA_get hn).trans ht, hty, fun hrc j hj => ?_⟩ + rw [hm _ (.pt q j hq hj) (R.pool_offA hY hp hi hA _ (.pt q j hq hj))] + obtain ⟨tag, sh, htag, hpth⟩ := hrc + rcases hPath hpth with hpar | ⟨ht0, hwne, rfl⟩ | ⟨ht2, rfl⟩ + · exact he ⟨tag, sh, htag, hpar⟩ j hj + · rw [(hunm.1 ht0 hwne).2 t ht j hj] + exact (hunm.1 ht0 hwne).1 j hj + · exfalso + rcases hKind with rfl | ⟨h0, -⟩ | ⟨h1, -⟩ | ⟨-, hy⟩ + · simp [tagA] at ht2 + · omega + · omega + · exact R.ytb _ hy hq + · intro y hy + obtain ⟨t, ht, he⟩ := R.high y hy + have hn : y ≠ p := fun h => by rw [h, hk] at ht; cases ht + refine ⟨t, (poolSetA_get hn).trans ht, fun ha' j hj => ?_⟩ + rw [hm _ (.high y j hy hj) (R.pool_offA hY hp hi hA _ (.high y j hy hj))] + rcases hActH ha' with ha0 | ⟨ht2, rfl⟩ + · exact he ha0 j hj + · rw [(hunm.2 ht2).2 t ht ⟨j.val, hj⟩] + exact (hunm.2 ht2).1 j hj + +/-- Pool update preserves `ExtVsRelA` when any newly installed Stage-2 root is empty. -/ +theorem ExtVsRelA.of_storePoolE {T2 : Set U64} {Sh2 : U64 → Nat} + (E : ExtVsRelA Pp Tb X Y T2 Sh2 mem σ) {f : AsidPool} + (hp : p ∈ Pp) (hk : σ.kheap (specPtr p) = some (.archObj (.asidPool f))) + (hKind : EntKindA Tb Lv X Y word) + (hm : ∀ b, ExtFootE X Y T2 b → mem' b = mem b) + (hs2 : tagA word = 1 → + ((∀ j : U64, j.val < 1024 → dBitA (mem (peAF (untagA word) j)) 0 = false) ∧ + ∀ es, σ.kheap (specPtr (untagA word)) = some (.archObj (.ext (.stage2Root es))) → + ∀ j : Fin 1024, es j = .invalidPTE)) : + ExtVsRelA Pp Tb X Y T2 Sh2 mem' (poolSetA σ p (Function.update f (BitVec.ofNat 9 i.val) (poolEntA word))) := by + have hActS2 : ∀ {r}, + ActiveS2RootA Pp X (poolSetA σ p (Function.update f (BitVec.ofNat 9 i.val) (poolEntA word))) r → + ActiveS2RootA Pp X σ r ∨ (tagA word = 1 ∧ r = untagA word) := by + rintro r ⟨hrX, asid, q, g, ha0, hqP, htab, hkhg, hfg⟩ + change σ.archState.armAsidTable _ = _ at htab + by_cases hqp : q = p + · subst hqp + simp only [poolSetA, setObjA, Function.update_self] at hkhg + cases hkhg + by_cases hlo : asidLowBitsOf asid = BitVec.ofNat 9 i.val + · rw [hlo, Function.update_self] at hfg + have hwne : word ≠ 0#u64 := by + intro hz; rw [hz, poolEntA_zero] at hfg; cases hfg + rw [poolEntA_ne hwne] at hfg + injection hfg with hfg; injection hfg with hfg + have hrEq : r = untagA word := (specPtr_inj hfg).symm + subst hrEq + rcases hKind with rfl | ⟨-, htb, -⟩ | ⟨ht1, -⟩ | ⟨-, hy⟩ + · exact absurd rfl hwne + · exact absurd (Or.inr (Or.inl htb)) (E.pgd _ (Or.inr ⟨_, hrX, Or.inl rfl⟩)).1 + · exact Or.inr ⟨ht1, rfl⟩ + · exact absurd hy (E.pgd _ (Or.inr ⟨_, hrX, Or.inl rfl⟩)).2 + · rw [Function.update_of_ne hlo] at hfg + exact Or.inl ⟨hrX, asid, q, f, ha0, hqP, htab, hk, hfg⟩ + · rw [poolSetA_get hqp] at hkhg + exact Or.inl ⟨hrX, asid, q, g, ha0, hqP, htab, hkhg, hfg⟩ + have hReachS2 : ∀ {q}, + S2ReachA Pp X T2 Sh2 mem' (poolSetA σ p (Function.update f (BitVec.ofNat 9 i.val) (poolEntA word))) q → + S2ReachA Pp X T2 Sh2 mem σ q ∨ (tagA word = 1 ∧ q = untagA word) := by + intro q hq + induction hq with + | root ha' => + rcases hActS2 ha' with ha0 | hnew + · exact Or.inl (.root ha0) + · exact Or.inr hnew + | @stepRoot r j ha' hj h3 ht2 => + have hj1k : j.val < 1024 := by + unfold s2Geom s2GeomOf at hj + split_ifs at hj <;> dsimp only at hj <;> omega + rw [hm _ (rootFootE ha'.1 (E.xal r ha'.1) hj1k)] at h3 ht2 ⊢ + rcases hActS2 ha' with ha0 | ⟨ht1, rfl⟩ + · exact Or.inl (.stepRoot ha0 hj h3 ht2) + · have hb0 := ((and3_bitsA _).1 h3).1 + exact absurd ((hs2 ht1).1 j hj1k) (by rw [hb0]; decide) + | @stepPt t j _ ht h12 hj h3 ht2 ih => + rw [hm _ (.s2pt t j ht hj)] at h3 ht2 ⊢ + rcases ih with hpar | ⟨ht1, rfl⟩ + · exact Or.inl (.stepPt hpar ht h12 hj h3 ht2) + · exfalso + rcases hKind with rfl | ⟨h0, -⟩ | ⟨-, hx⟩ | ⟨h2, -⟩ + · simp [tagA] at ht1 + · omega + · exact (E.t2x _ ht _ hx).1 rfl + · omega + refine ⟨?_, ?_, E.tsh, fun y hy => by rw [hm _ (.wl0 y hy)]; exact E.wl0 y hy, + fun y hy j hj => by rw [hm _ (.wlo y j hy hj)]; exact E.wlo y hy j hj, + E.xal, E.tal, E.pgd, E.t2x, E.ypg, E.ypp⟩ + · intro r hr + obtain ⟨es, hkes, he⟩ := E.s2root r hr + have hne : r ≠ p := fun e => (E.pgd r (Or.inr ⟨r, hr, Or.inl rfl⟩)).1 (Or.inl (e ▸ hp)) + refine ⟨es, (poolSetA_get hne).trans hkes, fun ha' j hj hmj => ?_⟩ + rw [hm _ (rootFootE hr (E.xal r hr) hj)] + rcases hActS2 ha' with ha0 | ⟨ht1, rfl⟩ + · exact he ha0 j hj hmj + · rw [(hs2 ht1).2 es hkes ⟨j.val, hj⟩] + exact ⟨(hs2 ht1).1 j hj, trivial⟩ + · intro t ht + obtain ⟨pt, hkpt, hty, he⟩ := E.s2pt t ht + have hne : t ≠ p := fun e => (E.pgd t (Or.inl ht)).1 (Or.inl (e ▸ hp)) + refine ⟨pt, (poolSetA_get hne).trans hkpt, hty, fun hrc j hj => ?_⟩ + rw [hm _ (.s2pt t j ht hj)] + rcases hReachS2 hrc with hrc0 | ⟨ht1, rfl⟩ + · exact he hrc0 j hj + · exfalso + rcases hKind with rfl | ⟨h0, -⟩ | ⟨-, hx⟩ | ⟨h2, -⟩ + · simp [tagA] at ht1 + · omega + · exact (E.t2x _ ht _ hx).1 rfl + · omega + +end Write + +section Decode +open Fl4Spec Fl4SpecA64 +variable {σ : State Unit} + +/-- Common part of the specification's Assign decoder after recognizing the root cap. -/ +def assignDecA {α : Type} (wrap : Asid → α) (p : ObjRef) (base : Asid) : SEMonad Unit α := do + let tab ← NonDetE.liftE (NonDet.gets fun s : State Unit => asidTable s) + let q := tab (asidHighBitsOf base) + NonDetE.whenE (q = none) (NonDetE.throwError (.failedLookup false .invalidRoot)) + NonDetE.whenE (p ≠ the q) (NonDetE.throwError (.invalidCapability 0)) + let f ← NonDetE.liftE (getAsidPool p) + let S := assignFreeA f base + NonDetE.whenE (S = ∅) (NonDetE.throwError .deleteFirst) + let off ← NonDetE.liftE (selectExt (fun _ => freeAsidPoolSelect f base) S) + NonDetE.returnOk (wrap (ucast asidRepLen off + base)) + +theorem assignDec_noPoolA {α : Type} (wrap : Asid → α) {p : ObjRef} {base : Asid} + (h : asidTable σ (asidHighBitsOf base) = none) : + (assignDecA wrap p base).run σ = ({(.error (.failedLookup false .invalidRoot), σ)}, False) := by + unfold assignDecA + rw [run_bind_okD (run_liftE_stD (m := NonDet.gets _) rfl), h] + unfold NonDetE.whenE + try dsimp only + rw [if_pos rfl] + exact run_bind_errorD (run_throwErrorD _ σ) _ + +theorem assignDec_wrongA {α : Type} (wrap : Asid → α) {p q : ObjRef} {base : Asid} + (h : asidTable σ (asidHighBitsOf base) = some q) (hne : p ≠ q) : + (assignDecA wrap p base).run σ = ({(.error (.invalidCapability 0), σ)}, False) := by + unfold assignDecA + rw [run_bind_okD (run_liftE_stD (m := NonDet.gets _) rfl), h] + unfold NonDetE.whenE + try dsimp only + rw [if_neg (by simp : some q ≠ (none : Option ObjRef))] + rw [ndE_pure_bindD] + rw [if_pos (show p ≠ the (some q) from hne)] + exact run_bind_errorD (run_throwErrorD _ σ) _ + +theorem assignDec_tailA {α : Type} (wrap : Asid → α) {p : ObjRef} {base : Asid} {f : AsidPool} + (ht : asidTable σ (asidHighBitsOf base) = some p) (hk : σ.kheap p = some (.archObj (.asidPool f))) : + (assignDecA wrap p base).run σ = (do + NonDetE.whenE (assignFreeA f base = ∅) (NonDetE.throwError SyscallError.deleteFirst) + let off ← NonDetE.liftE (selectExt (fun _ => freeAsidPoolSelect f base) (assignFreeA f base)) + NonDetE.returnOk (wrap (ucast asidRepLen off + base)) : SEMonad Unit α).run σ := by + unfold assignDecA + rw [run_bind_okD (run_liftE_stD (m := NonDet.gets _) rfl), ht] + unfold NonDetE.whenE + try dsimp only + rw [if_neg (by simp : some p ≠ (none : Option ObjRef)), ndE_pure_bindD] + rw [if_neg (show ¬ p ≠ the (some p) from not_not_intro rfl), ndE_pure_bindD] + rw [run_bind_okD (run_liftE_stD (getPool_runA hk))] + +theorem assignDec_fullA {α : Type} (wrap : Asid → α) {p : ObjRef} {base : Asid} {f : AsidPool} + (ht : asidTable σ (asidHighBitsOf base) = some p) (hk : σ.kheap p = some (.archObj (.asidPool f))) + (h : assignFreeA f base = ∅) : + (assignDecA wrap p base).run σ = ({(.error .deleteFirst, σ)}, False) := by + rw [assignDec_tailA wrap ht hk] + unfold NonDetE.whenE + try dsimp only + rw [if_pos h] + exact run_bind_errorD (run_throwErrorD _ σ) _ + +theorem assignDec_memA {α : Type} (wrap : Asid → α) {p : ObjRef} {base : Asid} {f : AsidPool} + (ht : asidTable σ (asidHighBitsOf base) = some p) (hk : σ.kheap p = some (.archObj (.asidPool f))) + {off : AsidLowIndex} (h : off ∈ assignFreeA f base) : + (.ok (wrap (ucast asidRepLen off + base)), σ) ∈ ((assignDecA wrap p base).run σ).1 ∧ + ¬ ((assignDecA wrap p base).run σ).2 := by + rw [assignDec_tailA wrap ht hk] + unfold NonDetE.whenE + try dsimp only + rw [if_neg (fun he => by rw [he] at h; exact Set.notMem_empty off h), ndE_pure_bindD] + refine ⟨mem_bindE_okD (mem_liftED (selectExt_memA _ h σ)) (by rfl), ?_⟩ + simp [ExceptT.run_bind, liftE_runD, selectExt, nd_bind_applyD, NonDet.get, NonDet.select, NonDet.assert, + StateExt.selectSwitch, NonDet.pure, pure, NonDetE.returnOk] + exact fun _ _ hfail => hfail + +end Decode + +section Frame +open Fl4ProofsA64.P1.W7.R.ST +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- P1 placement, the heap relation, and the VCPU frame place a pool word off the non-VSpace footprint. -/ +theorem JEntA.poolWord_offA (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) {p i : U64} (hp : p ∈ Z.Pp) (hi : i.val < 512) + (w : PoolW st H Z.Cs OS s (peAF p i)) : + ¬ FootA (L := L) { extZA Z with Pp := ∅ } s (peAF p i) := by + have hpf := E.ap.vs _ (.pool p i hp hi) + have hfit : (L.ksb s).val + 788736 ≤ 2 ^ 64 := E.sys.ent.ksw + have hKs (off : U64) (ho : off.val < 788736) : peAF p i ≠ wa (L.ksb s) off := by + intro e + have h := w.ks.2 + rw [e, wadd_val (by omega)] at h + omega + have hNode (c : U64) (hc : c ∈ Z.Cs) (off : U64) (ho : off.val < 4288) : + peAF p i ≠ L.nodeAt s c off := by + intro e + have h := w.ks.1 c hc + rw [e, nodeAt_val (E.sq.nodes c hc) ho] at h + omega + have hTcb (t : U64) (ht : t ∈ Z.T) (off : U64) (ho : off.val < 4096) : peAF p i ≠ wa t off := by + rw [← E.tcbs] at ht + intro e + have h := hpf.2 t ht + have hf := (E.sq.tcbs t ht).fits + simp only [Nat.reducePow] at hf + rw [e, wadd_val (by omega)] at h + omega + have hSc (c : U64) (hc : c ∈ Z.C) (off : U64) (ho : off.val < 128) : peAF p i ≠ wa c off := by + rw [← E.scs] at hc + obtain ⟨b, hb⟩ := hc + obtain ⟨q, z, hq, hle, hpg⟩ := E.ptH p (Or.inl hp) + have hne : q ≠ c := fun e => by rw [e, hb] at hq; cases hq + have hdis := E.sys.ent.heap.disj q _ c _ hq hb hne + have hqfit := E.sys.ent.heap.fits q _ hq + have hcfit := E.sys.ent.heap.fits c _ hb + have hs : 128 ≤ 2 ^ b := Nat.pow_le_pow_right (by decide : 0 < (2 : Nat)) (E.sys.ent.heap.scmin c _ hb) + simp only [HKind.bits] at hdis hqfit hcfit + intro e + have hv := congrArg UScalar.val e + rw [peAF_valA (by omega) hi, wadd_val (by omega)] at hv + omega + have hSlot (y : U64) (hy : SlotWA Z.th y) (off : U64) (ho : off.val < 32) : peAF p i ≠ wa y off := by + have hy0 : IsSlotA Z.th y := by + rcases hy with hy | ⟨q, r, hq, idx, hl, rfl⟩ + · exact hy + · have hab : AbsSlotA Z.th (specPtr q, idx) := ⟨q, rfl, Or.inl (by rw [hl]; exact hq)⟩ + exact hab.isSlot E.j.1.place + obtain ⟨q, k, j, hq, hSize, hv, _⟩ := slotW_valA E.j.1.place hy + have hf := E.j.1.place.fit q k hq + intro e + exact w.os y (RS.sub y hy0) (by rw [e, wadd_val (by omega)]; omega) + intro h + generalize heq : peAF p i = waddr at h + cases h with + | slot y _ hy he => + rw [← heq] at he + rcases he with e | e | e | e + · have hh := hSlot y hy 0#u64 (by decide) + exact hh (e.trans (by apply UScalar.eq_of_val_eq; rw [wa_valm]; simp)) + · exact hSlot y hy 8#u64 (by decide) e + · exact hSlot y hy 16#u64 (by decide) e + · exact hSlot y hy 24#u64 (by decide) e + | nslot b h => + cases h with + | node c _ hc he => + rw [← heq] at he + rcases he with h | h | h | h + · exact hNode c hc _ (by simp [layout.node.CUR_THREAD]) h + · exact hNode c hc _ (by simp [layout.node.IDLE_THREAD]) h + · exact hNode c hc _ (by simp [layout.node.CUR_SC]) h + · exact hNode c hc _ (by simp [layout.node.IDLE_SC]) h + | cur _ he => + rw [← heq] at he + rcases he with h | h + · exact hNode _ E.sq.cur _ (by simp [layout.node.SCHED_ACTION]) h + · exact hNode _ E.sq.cur _ (by simp [layout.node.CUR_TIME]) h + | tcb t _ ht he => + rw [← heq] at he + rcases he with h | h | h | h | h + · rw [stA_eq] at h; exact hTcb t ht _ (by decide) h + · exact hTcb t ht _ (by decide) h + · exact hTcb t ht _ (by decide) h + · exact hTcb t ht _ (by decide) h + · exact hTcb t ht _ (by decide) h + | sc c hc => exact hSc c hc 24#u64 (by decide) heq + | vcpu v _ hv he => + rw [← heq] at he + apply E.vsVc _ (.pool p i hp hi) + rcases he with e | e + · exact ⟨v, hv, 0#u64, by decide, by decide, e.trans (by + apply UScalar.eq_of_val_eq; rw [wa_valm]; simp)⟩ + · exact ⟨v, hv, 24#u64, by decide, by decide, e⟩ + | irq j hj => + have e := irqWordA_val (kb := L.ksb s) hfit hj + have h := w.ks.2 + rw [heq, e] at h + omega + | cur2 _ he => + rw [← heq] at he + rcases he with e | e + · exact hNode _ E.sq.cur _ (by simp [layout.node.CONSUMED]) e + · exact hNode _ E.sq.cur _ (by simp [layout.node.REPROGRAM]) e + | glob _ he => + rw [← heq] at he + rcases he with e | e + · exact hKs 8#u64 (by decide) e + · exact hKs 16#u64 (by decide) e + | tcb2 t _ ht he => + rw [← heq] at he + rcases he with e | e + · exact hTcb t ht _ (by decide) e + · exact hTcb t ht _ (by decide) e + | sc2 c hc => exact hSc c hc 72#u64 (by decide) heq + | vs _ he => + cases he with + | asid j hj => exact E.vsLeafA.2.2 p hp i hi (by rw [heq]; exact atAF_asWA hj) + | pool q _ hq _ => exact Set.notMem_empty q hq + | pt q _ hq _ => exact Set.notMem_empty q hq + | high q _ hq _ => exact Set.notMem_empty q hq + | ext _ he => exact extFoot_emptyE he + + +/-- The physical and abstract pool writes preserve the joint relation and the existing CDT. -/ +theorem assign_write_jointA (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) {s2 : S} {σ2 : Fl4SpecA64.State Unit} + (J : JAtZAX (L := L) AR Z s2 a σ2 m) (hks : L.ksb s2 = L.ksb s) (hc : L.cpu s2 = L.cpu s) + {p i word : U64} (hp : p ∈ Z.Pp) (hi : i.val < 512) (w : PoolW st H Z.Cs OS s (peAF p i)) + {f : Fl4SpecA64.AsidPool} (hk : σ2.kheap (specPtr p) = some (.archObj (.asidPool f))) + (hkind : EntKindA Z.S1 Z.Lv Z.X Z.Y word) + (hzero : L.mem s2 (atAF (L.ksb s2) 0#u64) = p → i ≠ 0#u64) + (hunmVs : (tagA word = 0 → word ≠ 0#u64 → + ((∀ j : U64, j.val < 512 → dBitA (L.mem s2 (peAF (untagA word) j)) 0 = false) ∧ + ∀ t, σ2.kheap (specPtr (untagA word)) = some (.archObj (.pageTable t)) → + ∀ j : U64, j.val < 512 → Fl4SpecA64.ptApply t j.bv = .invalidPTE)) ∧ + (tagA word = 2 → + ((∀ j : U64, j.val < 512 → dBitA (L.mem s2 (peAF (wa (untagA word) 4096#u64) j)) 0 = false) ∧ + ∀ l1, σ2.kheap (specPtr (untagA word)) = some (.archObj (.ext (.highVSpace l1))) → + ∀ j : Fin 512, l1 j = .invalidPTE))) + (hunmS2 : tagA word = 1 → + ((∀ j : U64, j.val < 1024 → dBitA (L.mem s2 (peAF (untagA word) j)) 0 = false) ∧ + ∀ es, σ2.kheap (specPtr (untagA word)) = some (.archObj (.ext (.stage2Root es))) → + ∀ j : Fin 1024, es j = .invalidPTE)) : + let σ3 := poolSetA σ2 p (Function.update f (BitVec.ofNat 9 i.val) (poolEntA word)) + let s3 := L.wr s2 (peAF p i) word + JAtZAX (L := L) AR Z s3 a σ3 m ∧ OSKeep st OS s2 s3 ∧ TyKeepA σ2 σ3 := by + dsimp only + have hOff := E.poolWord_offA RS hp hi w + have hall := FootA.aligned J.1 (fun c hc' off ho hal => by + rw [nodeAt_ksb hks]; exact bxi_nodes E.sys.ent.bxi c hc' off ho hal) E.scAl E.ap.vcAl + have hmem : ∀ b, Aligned b → b ≠ peAF p i → L.mem (L.wr s2 (peAF p i) word) b = L.mem s2 b := + fun b hb hn => mem_wr_ne' w.al hb hn.symm + have Hm : SameA (L := L) { extZA Z with Pp := ∅ } s2 (L.wr s2 (peAF p i) word) := + ⟨L.ksb_wr _ _ _, L.cpu_wr _ _ _, fun b hb => hmem b (hall b + (FootA.monoA (Z := { extZA Z with Pp := ∅ }) (Z' := Z) rfl rfl + (fun _ e => e) (fun _ e => e) (fun _ e => e) + (fun _ e => (Set.notMem_empty _ e).elim) (fun _ e => (Set.notMem_empty _ e).elim) + (fun _ e => (Set.notMem_empty _ e).elim) (fun _ e => (Set.notMem_empty _ e).elim) + (fun _ e => (Set.notMem_empty _ e).elim) hb)) (fun e => + hOff (e ▸ hb.of_ksA hks.symm hc.symm))⟩ + have hn : ¬ AsW (L.ksb s2) (peAF p i) := by rw [hks]; exact E.vsLeafA.2.2 p hp i hi + have hVR := J.1.vs.of_storePoolA hp hi hk hkind hzero J.1.ext.ypp hn + (fun b hb => hmem b (hall b (.nslot b (.vs b hb)))) (L.mem_wr_same w.al) hunmVs + have pal := Nat.mod_eq_zero_of_dvd (J.1.vs.pal p hp) + have hExt (b : U64) (hb : ExtFootE Z.X Z.Y Z.T2 b) : b ≠ peAF p i := by + obtain ⟨q, j, hj, rfl, hq, htype⟩ := hb.pgE J.1.ext J.1.vs.yal + intro h + have e := (peAF_injA (Nat.mod_eq_zero_of_dvd hq) pal hj hi h).1 + subst q + rcases htype with hq' | hq' + · exact (J.1.ext.pgd p hq').1 (Or.inl hp) + · exact J.1.ext.ypg p hq' (Or.inl hp) + have hkh (q : Fl4SpecA64.ObjRef) (hq : q ≠ specPtr p) : + (poolSetA σ2 p (Function.update f (BitVec.ofNat 9 i.val) (poolEntA word))).kheap q = σ2.kheap q := by + simp [poolSetA, setObjA, Function.update_of_ne hq] + have hnot (q : Fl4SpecA64.ObjRef) {o : Fl4SpecA64.KernelObject} (hq : σ2.kheap q = some o) + (hn' : o ≠ .archObj (.asidPool f)) : q ≠ specPtr p := + fun e => hn' (Option.some.inj (hq.symm.trans (e ▸ hk))) + have hER := J.1.ext.of_storePoolE (Lv := Z.Lv) (i := i) hp hk hkind + (fun b hb => hmem b (hall b (.nslot b (.ext b hb))) (hExt b hb)) hunmS2 + refine ⟨J.of_sameVsA (σ' := poolSetA σ2 p (Function.update f (BitVec.ofNat 9 i.val) (poolEntA word))) + (P0 := ∅) Hm (by constructor <;> first | rfl | exact ⟨rfl, rfl, rfl, rfl⟩) + (fun q r hq => ?_) (fun q hq => ?_) (fun t ht => ?_) (fun c hc' => ?_) (fun v hv => ?_) + rfl rfl (fun q v h => ?_) (fun q es h => ?_) (fun q l1 h => ?_) + (fun _ h1 h2 => ⟨h1, h2⟩) (by rw [L.ksb_wr]; exact hVR) hER, + osKeep_tabWriteA E.sys.ent.os (fun ⟨y, hy, ho⟩ => w.os y hy ho) hmem, + TyKeepA.upd hk (fun _ _ e => by cases e) (fun _ e => by cases e)⟩ + · obtain ⟨cs, he, _, _⟩ := J.1.st.cnode q r hq + exact hkh _ (hnot _ he (by intro e; cases e)) + · obtain ⟨x, he, _⟩ := J.1.st.tcb q hq + exact hkh _ (hnot _ he (by intro e; cases e)) + · obtain ⟨x, he, _, _⟩ := J.1.sch.tcb t ht + exact hkh _ (hnot _ he (by intro e; cases e)) + · obtain ⟨x, n, he, _⟩ := J.1.sch.sc c hc' + exact hkh _ (hnot _ he (by intro e; cases e)) + · obtain ⟨x, he, _, _⟩ := J.2.1.obj v hv + exact hkh _ (hnot _ he (by intro e; cases e)) + · by_cases e : q = specPtr p + · rw [e] at h + simp [poolSetA, setObjA] at h + · rwa [hkh q e] at h + · by_cases e : q = specPtr p + · rw [e] at h + simp [poolSetA, setObjA] at h + · exact J.2.2.1.back q es (by rwa [hkh q e] at h) + · by_cases e : q = specPtr p + · rw [e] at h + simp [poolSetA, setObjA] at h + · exact J.2.2.2.back q l1 (by rwa [hkh q e] at h) + +end Frame + + +section AssignRoots +open Fl4Spec Fl4SpecA64 + +/-- An aligned root address leaves the low tag bits free, and stripping the tag recovers the root. -/ +theorem assignTagA {root tag : U64} (hr : root.val % 4096 = 0) (ht : tag.val < 4) : + tagA (root ||| tag) = tag.val ∧ untagA (root ||| tag) = root := by + have hl := orC_lowA (C := tag.val) hr + rw [Nat.mod_eq_of_lt (show tag.val < 4096 by omega)] at hl + have hlo : (root.val ||| tag.val) % 4 = tag.val := by + rw [← Nat.mod_mod_of_dvd (root.val ||| tag.val) (by decide : 4 ∣ 4096), hl, + Nat.mod_eq_of_lt ht] + refine ⟨by rw [tagA, UScalar.val_or, hlo], ?_⟩ + apply UScalar.eq_of_val_eq + rw [untagA, UScalar.val_and, show (18446744073709551612#u64 : U64).val = + 2 ^ 64 - 1 - (2 ^ 2 - 1) from rfl, nat_and_hiA (root ||| tag).val 2 (root ||| tag).bv.isLt (by decide), + UScalar.val_or] + have he : (root.val ||| tag.val) / 4 = root.val / 4 := by + apply Nat.eq_of_testBit_eq + intro j + rw [show (4 : Nat) = 2 ^ 2 by decide, Nat.testBit_div_two_pow, Nat.testBit_div_two_pow, Nat.testBit_or] + have hn : 2 ≤ j + 2 := by omega + have hp : 2 ^ 2 ≤ 2 ^ (j + 2) := Nat.pow_le_pow_right (by decide : 0 < (2 : Nat)) hn + have hb : tag.val < 2 ^ (j + 2) := lt_of_lt_of_le ht hp + have ht' : tag.val.testBit (j + 2) = false := Nat.testBit_lt_two_pow hb + rw [ht', Bool.or_false] + change 4 * ((root.val ||| tag.val) / 4) = root.val + rw [he] + omega + +def assignRootCapA (k : ExtRootKind) (root : ObjRef) (m : Option Asid) : ArchCap := + match k with + | .stage1 => .pageTableCap root .vsRootPtT (m.map fun asid => (asid, 0)) + | .stage2 => .ext (.stage2VSpaceCap root m) + | .high => .ext (.highVSpaceCap root m) + +def assignInvA (k : ExtRootKind) (p : ObjRef) (dst : CslotPtr) (asid : Asid) : Fl4ArchInvocation := + match k with + | .stage1 => .spec (.invokeASIDPool (.assign asid p dst)) + | .stage2 | .high => .extVSpace (.assign asid p dst) + + +private theorem assignNdAssocA {s α β γ : Type} (m : NonDet s α) (f : α → NonDet s β) (g : β → NonDet s γ) : + ((m >>= f) >>= g) = (m >>= fun x => f x >>= g) := by + funext σ + apply Prod.ext + · ext r + simp only [nd_bind_applyD, Set.mem_setOf_eq] + aesop + · simp only [nd_bind_applyD, Set.mem_setOf_eq] + apply propext + aesop + +private theorem assignEAssocA {s ε α β γ : Type} (m : NonDetE s ε α) + (f : α → NonDetE s ε β) (g : β → NonDetE s ε γ) : + ((m >>= f) >>= g) = (m >>= fun x => f x >>= g) := by + change ExceptT.mk ((m.run >>= ExceptT.bindCont f) >>= ExceptT.bindCont g) = + ExceptT.mk (m.run >>= ExceptT.bindCont fun x => f x >>= g) + rw [assignNdAssocA] + congr 1 + congr 1 + funext x + cases x with + | error e => + change (pure (Except.error e) >>= ExceptT.bindCont g) = _ + rw [nd_pure_bindD] + rfl + | ok a => rfl + +private theorem liftME_assignDecA {α β : Type} (g : α → β) (wrap : Asid → α) (p : ObjRef) (base : Asid) + (σ : State Unit) : + (NonDetE.liftME g (assignDecA wrap p base)).run σ = + (assignDecA (g ∘ wrap) p base).run σ := by + unfold assignDecA NonDetE.liftME + simp only [assignEAssocA, NonDetE.returnOk, ndE_pure_bindD, Function.comp_apply] + + +theorem specDecodeAssignA (k : ExtRootKind) (calling : Bool) (args : List Data) (xSlot : CapRef) + (cte dst : CslotPtr) (p root : ObjRef) (base : Asid) (rest : List (Cap × CslotPtr)) (σ : State Unit) : + (fl4ArchDecodeInvocation (Z := Unit) calling (archL .armASIDPoolAssign) args xSlot cte (.asidPoolCap p base) + ((.archObjectCap (assignRootCapA k root none), dst) :: rest)).run σ = + (assignDecA (assignInvA k p dst) p base).run σ := by + have hl : invocationType (specLabelWord (archL .armASIDPoolAssign)) = .archInvocationLabel .armASIDPoolAssign := + by rw [invType_specA]; rfl + cases k with + | stage1 => + have he : archDecodeInvocation (Z := Unit) (specLabelWord (archL .armASIDPoolAssign)) args xSlot cte + (.asidPoolCap p base) ((.archObjectCap (.pageTableCap root .vsRootPtT none), dst) :: rest) = + assignDecA (fun asid => .invokeASIDPool (.assign asid p dst)) p base := by + unfold archDecodeInvocation decodeAsidPoolInvocation assignDecA + simp only [hl, ↓reduceIte, List.length_cons, Nat.zero_lt_succ, List.getElem?_cons_zero, + the, ArchCap.acapObj, ArchCap.acapAsidBase, assignFreeA] + simp only [show ∀ (T : Type) (x : T), (NonDetE.returnOk x : SEMonad Unit T) = pure x from fun _ _ => rfl, + ndE_pure_bindD] + rfl + dsimp only [assignRootCapA, Option.map] + change (NonDetE.liftME Fl4ArchInvocation.spec _).run σ = _ + rw [he, liftME_assignDecA] + rfl + | stage2 => + change (NonDetE.liftME Fl4ArchInvocation.extVSpace + (assignDecA (fun asid => .assign asid p dst) p base)).run σ = _ + rw [liftME_assignDecA] + rfl + | high => + change (NonDetE.liftME Fl4ArchInvocation.extVSpace + (assignDecA (fun asid => .assign asid p dst) p base)).run σ = _ + rw [liftME_assignDecA] + rfl + +end AssignRoots + +section ConcreteAssign +open Fl4SpecA64 +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +def assignMappedA (k : ExtRootKind) (root asid : U64) : capability.Cap := + match k with + | .stage1 => .Pml4 root true asid + | .stage2 => .Ept root 0#u64 true asid 0#u64 + | .high => .HighVSpace root true asid + +def assignTagWordA : ExtRootKind → U64 + | .stage1 => 0#u64 + | .stage2 => 1#u64 + | .high => 2#u64 + +def assignFl4A (k : ExtRootKind) (s : S) (a : A) (ct x root p base : U64) : + Result (core.result.Result Unit error.Exc × S × A) := do + let hi ← base >>> vspace.ASID_LOW_BITS + let pool ← vspace.asid_table st s hi + if pool = 0#u64 then + ok (.Err (.Sys (.FailedLookup false .InvalidRoot)), s, a) + else if pool != p then + let r ← error.invalid_cap Unit 0#u64 + ok (r, s, a) + else + let i ← vspace.decode_loop1 st s base pool 0#u64 + if i = 512#u64 then + ok (.Err (.Sys .DeleteFirst), s, a) + else + let asid ← lift (wa base i) + let s1 ← sched.set_thread_state st s ct sched.TS_RESTART + let s2 ← obj.Slot.set_cap st x s1 (assignMappedA k root asid) + let off ← (asid &&& 511#u64) * 8#u64 + let s3 ← st.write s2 (wa pool off) (root ||| assignTagWordA k) + ok (.Ok (), s3, a) + + +def assignUnmappedA (k : ExtRootKind) (root oldAsid oldVa : U64) : capability.Cap := + match k with + | .stage1 => .Pml4 root false oldAsid + | .stage2 => .Ept root 0#u64 false oldAsid oldVa + | .high => .HighVSpace root false oldAsid + +theorem decodeAssignRunA (ar : machine.Arch A) (s : S) (a : A) (k : ExtRootKind) + (root p base oldAsid oldVa : U64) (cte dst : CslotPtr) (length buf : U64) (block call cd fp : Bool) + (ex : Aeneas.Std.Array obj.Slot 3#usize) (hex : vspace.extra ex 0#usize = ok (some (cslotAddrA dst))) + (hcap : capA st s (cslotAddrA dst) = assignUnmappedA k root oldAsid oldVa) : + syscall.decode_invocation st ar s a fl4_abi.label.ARMASIDPoolAssign length (cslotAddrA cte) + (.AsidPool p base) block call cd fp buf ex = + assignFl4A (st := st) k s a (L.curW s) (cslotAddrA dst) root p base := by + have h01 : vspace.ROOT_S1 ≠ vspace.ROOT_S2 := by unfold vspace.ROOT_S1 vspace.ROOT_S2; decide + have h02 : vspace.ROOT_S1 ≠ vspace.ROOT_HIGH := by unfold vspace.ROOT_S1 vspace.ROOT_HIGH; decide + have h21 : vspace.ROOT_HIGH ≠ vspace.ROOT_S2 := by unfold vspace.ROOT_HIGH vspace.ROOT_S2; decide + cases k <;> simp only [syscall.decode_invocation, cur_thread_ok, bind_ok, vspace.decode, + bne_self_eq_false, Bool.false_eq_true, ↓reduceIte, hex, capA_eq, hcap, assignUnmappedA, assignFl4A, + assignMappedA, (assign_searchLoopsA (st := st) s base _ 0#u64).1, + (assign_searchLoopsA (st := st) s base _ 0#u64).2, h01, h02, h21, + vspace.ROOT_S1, vspace.ROOT_S2, vspace.ROOT_HIGH, assignTagWordA, lift, bind_tc_ok, + show (0#u64 : U64) ≠ 1#u64 by decide, show (0#u64 : U64) ≠ 2#u64 by decide, + show (2#u64 : U64) ≠ 1#u64 by decide] + rfl + +theorem assignCapFactsA (k : ExtRootKind) {root oldAsid oldVa asid : U64} + (hOld : CapRt.EncWf (assignUnmappedA k root oldAsid oldVa)) (h16 : asid.val < 2 ^ 16) : + CapRt.EncWf (assignMappedA k root asid) ∧ + CapRelA (assignMappedA k root asid) (.archObjectCap (assignRootCapA k (specPtr root) (some (asidOfA asid)))) ∧ + absCapA (assignUnmappedA k root oldAsid oldVa) = .archObjectCap (assignRootCapA k (specPtr root) none) ∧ + shapeA (assignMappedA k root asid) = shapeA (assignUnmappedA k root oldAsid oldVa) ∧ + CapFinEqA (assignMappedA k root asid) (assignUnmappedA k root oldAsid oldVa) := by + have hb : asid.val < 2 ^ 20 := by omega + cases k with + | stage1 => + exact ⟨hOld, ⟨h16, rfl⟩, rfl, rfl, + ⟨Set.ext fun _ => by rw [genObjRefs_absA, genObjRefs_absA]; exact Iff.rfl, rfl, rfl, rfl⟩⟩ + | stage2 => + exact ⟨⟨hOld.1, by decide, hb, by decide⟩, ⟨⟨rfl, h16⟩, rfl⟩, rfl, rfl, + ⟨Set.ext fun _ => by rw [genObjRefs_absA, genObjRefs_absA]; exact Iff.rfl, rfl, rfl, rfl⟩⟩ + | high => + exact ⟨hOld, ⟨h16, rfl⟩, rfl, rfl, + ⟨Set.ext fun _ => by rw [genObjRefs_absA, genObjRefs_absA]; exact Iff.rfl, rfl, rfl, rfl⟩⟩ + +theorem assignRootWordA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} + {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} {ia ie : Nat} {s : S} {a : A} + {σ : State Unit} {m : McState} + (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) {dst : CslotPtr} (hab : AbsSlotA Z.th dst) + (k : ExtRootKind) {root oldAsid oldVa : U64} (hcap : capA st s (cslotAddrA dst) = assignUnmappedA k root oldAsid oldVa) : + EntKindA Z.S1 Z.Lv Z.X Z.Y (root ||| assignTagWordA k) ∧ + poolEntA (root ||| assignTagWordA k) = some (.asidPoolVSpace (specPtr root)) := by + have hok := E.j.capObj (hab.isSlot E.j.1.place) + change CapObjOkA Z (capA st s (cslotAddrA dst)) at hok + rw [hcap] at hok + cases k with + | stage1 => + have ⟨hm, hl⟩ := hok.2.2.2.1 root false oldAsid rfl + have hn : root ≠ 0#u64 := by + intro e + have ⟨q, z, hq, hle, _⟩ := E.ptH root (Or.inr hm) + have eq : q = 0#u64 := UScalar.eq_of_val_eq (by rw [e] at hle; simpa using hle) + rw [eq, E.sys.sysl.ly.h0] at hq + cases hq + obtain ⟨ht, hu⟩ := assignTagA (tag := assignTagWordA .stage1) (E.j.1.vs.al_mod hm) (by decide) + have hn' : (root ||| assignTagWordA .stage1) ≠ 0#u64 := fun e => + hn (hu.symm.trans (by rw [e]; rfl)) + exact ⟨Or.inr (Or.inl ⟨ht, by rw [hu]; exact hm, by rw [hu]; exact hl⟩), (poolEntA_ne hn').trans (by rw [hu])⟩ + | stage2 => + have hm := hok.2.1 root 0#u64 false oldAsid oldVa rfl + have hr : root.val % 4096 = 0 := Nat.mod_eq_zero_of_dvd (dvd_trans (by decide : 4096 ∣ 8192) (E.j.1.ext.xal root hm)) + obtain ⟨ht, hu⟩ := assignTagA (tag := assignTagWordA .stage2) hr (by decide) + have hn : (root ||| assignTagWordA .stage2) ≠ 0#u64 := fun e => by rw [e] at ht; contradiction + exact ⟨Or.inr (Or.inr (Or.inl ⟨ht, by rw [hu]; exact hm⟩)), (poolEntA_ne hn).trans (by rw [hu])⟩ + | high => + have hm := hok.2.2.1 root false oldAsid rfl + have hr : root.val % 4096 = 0 := Nat.mod_eq_zero_of_dvd (dvd_trans (by decide : 4096 ∣ 8192) (E.j.1.vs.yal root hm)) + obtain ⟨ht, hu⟩ := assignTagA (tag := assignTagWordA .high) hr (by decide) + have hn : (root ||| assignTagWordA .high) ≠ 0#u64 := fun e => by rw [e] at ht; contradiction + exact ⟨Or.inr (Or.inr (Or.inr ⟨ht, by rw [hu]; exact hm⟩)), (poolEntA_ne hn).trans (by rw [hu])⟩ + +end ConcreteAssign + +section SpecPerform +open Fl4Spec Fl4SpecA64 + +theorem assignPerformA (k : ExtRootKind) (p root : ObjRef) (dst : CslotPtr) (asid : Asid) + {σ1 σ2 σ3 : State Unit} + (hg : (.archObjectCap (assignRootCapA k root none), σ1) ∈ (getCap (Z := Unit) dst σ1).1) + (hs : ((), σ2) ∈ (setCap (Z := Unit) (.archObjectCap (assignRootCapA k root (some asid))) dst σ1).1) + (hw : ((), σ3) ∈ (storeAsidPoolEntry (Z := Unit) p asid (some (.asidPoolVSpace root)) σ2).1) : + (.ok ([] : List Data), σ3) ∈ ((fl4ArchPerformInvocation (Z := Unit) (assignInvA k p dst asid)).run σ1).1 := by + cases k with + | stage1 => + apply mem_liftED + change (([] : List Data), σ3) ∈ + (archNoReturn (performAsidPoolInvocation (.assign asid p dst)) σ1).1 + unfold archNoReturn + refine mem_bind_ndD (x := ((), σ3)) ?_ rfl + unfold performAsidPoolInvocation + refine mem_bind_ndD hg ?_ + dsimp only [assignRootCapA, Cap.theArchCap, isArchCap, ArchCap.isPageTableCap, ArchCap.acapObj, + updateMapData, Option.map] + refine mem_bind_ndD (x := ((), σ1)) (assert_memA rfl σ1) ?_ + rw [nd_pure_bindD] + refine mem_bind_ndD (x := ((), σ1)) (assert_memA rfl σ1) ?_ + exact mem_bind_ndD hs (by rw [nd_pure_bindD]; exact hw) + | stage2 => + apply mem_archExtA + unfold performExtVSpace + refine mem_bind_ndD hg ?_ + exact mem_bind_ndD hs (mem_bind_ndD hw rfl) + | high => + apply mem_archExtA + unfold performExtVSpace + refine mem_bind_ndD hg ?_ + exact mem_bind_ndD hs (mem_bind_ndD hw rfl) + +end SpecPerform + +section Finish +open Fl4Spec Fl4SpecA64 +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {s : S} {a : A} {σ : State Unit} {m : McState} + +theorem assignBankZeroA (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + {p base hi i : U64} (hbase : CapWfA (.AsidPool p base)) (hhi : hi.val < 128) (hv : hi.val = base.val / 512) + (hp : p ≠ 0#u64) (hword : L.mem s (atAF (L.ksb s) hi) = p) (hfree : wa base i ≠ 0#u64) + {s2 : S} {σ2 : State Unit} (R : VsRelA Z.Pp Z.S1 Z.Lv Z.X Z.Y (L.mem s2) (L.ksb s2) σ2) + (htab : σ2.archState.armAsidTable = σ.archState.armAsidTable) + (hz : L.mem s2 (atAF (L.ksb s2) 0#u64) = p) : i ≠ 0#u64 := by + have ht2 := R.table 0#u64 (by decide) + rw [htab, hz, optPtrA_ne hp] at ht2 + have ht0 := E.j.1.vs.table 0#u64 (by decide) + rw [ht2] at ht0 + have hh : L.mem s (atAF (L.ksb s) 0#u64) = p := by + by_cases e : L.mem s (atAF (L.ksb s) 0#u64) = 0#u64 + · rw [e, optPtrA_zero] at ht0 + cases ht0 + · rw [optPtrA_ne e] at ht0 + exact specPtr_inj (Option.some.inj ht0.symm) + have hh0 := E.poolTable_injA hhi (by decide) hp hword hh + have hv0 : hi.val = 0 := by rw [hh0]; rfl + have hal := hbase.2 + have hb : base = 0#u64 := UScalar.eq_of_val_eq (by change base.val = 0; omega) + intro e + apply hfree + rw [hb, e] + rfl + +private theorem assignFinishA (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) (T : TreeA st OS Z.G.chains Z.G.par s) + (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) + (k : ExtRootKind) (dst : CslotPtr) (hab : AbsSlotA Z.th dst) {root oldAsid oldVa p base hi i : U64} + (hcap : capA st s (cslotAddrA dst) = assignUnmappedA k root oldAsid oldVa) + (hbase : CapWfA (.AsidPool p base)) (hhi : hi.val < 128) (hv : hi.val = base.val / 512) + (hp : p ≠ 0#u64) (hword : L.mem s (atAF (L.ksb s) hi) = p) + (hi512 : i.val < 512) (hfree : wa base i ≠ 0#u64) (w : PoolW st H Z.Cs OS s (peAF p i)) + (hpP : p ∈ Z.Pp) {f : AsidPool} (hk : σ.kheap (specPtr p) = some (.archObj (.asidPool f))) + (hunm : UnmRootAssignA k (L.mem s) σ root) : + ∃ s' σ' σ1, + (do + let s1 ← sched.set_thread_state st s (L.curW s) sched.TS_RESTART + let s2 ← obj.Slot.set_cap st (cslotAddrA dst) s1 (assignMappedA k root (wa base i)) + let s3 ← st.write s2 (peAF p i) (root ||| assignTagWordA k) + ok (.Ok (), s3, a) : Result (core.result.Result Unit error.Exc × S × A)) = ok (.Ok (), s', a) ∧ + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + (.ok ([] : List Data), σ') ∈ ((fl4ArchPerformInvocation (Z := Unit) + (assignInvA k (specPtr p) dst (asidOfA (wa base i)))).run σ1).1 ∧ + JAtZAX (L := L) AR Z s' a σ' m ∧ TreeA st OS Z.G.chains Z.G.par s' ∧ + (CarryA Z (L.mem s) σ → CarryA Z (L.mem s') σ') := by + have hA := hab.isSlot E.j.1.place + have hos := RS.sub _ hA + have hOld : CapRt.EncWf (assignUnmappedA k root oldAsid oldVa) := + hcap ▸ (CapRt.decodeF_encWf _ _ : CapRt.EncWf (capA st s (cslotAddrA dst))) + obtain ⟨_, h16, hmask⟩ := assign_sumA hbase hi512 + obtain ⟨hEnc, hrel, habs, hshape, hfin⟩ := assignCapFactsA k hOld h16 + have hOldObj := E.j.capObj hA + change CapObjOkA Z (capA st s (cslotAddrA dst)) at hOldObj + rw [hcap] at hOldObj + have hObj := CapObjOkA.of_shapeA hshape hOldObj (fun _ => by cases k <;> trivial) + have hUA := UA.of_shapeA hshape (fun h => by cases k <;> cases h) + have hn1 : assignMappedA k root (wa base i) ≠ .Null := by cases k <;> intro h <;> cases h + have hn0 : assignUnmappedA k root oldAsid oldVa ≠ .Null := by cases k <;> intro h <;> cases h + obtain ⟨x, s2, σ2, hx, hrestart, hrestartS, hs, hsS, Hs, hks, R, hcpu, hw2, hJ, Fr⟩ := + restart_setcapJA E RS dst hab hos hEnc hrel + obtain ⟨J, T2⟩ := hJ T hnul hn1 (by rw [hcap]; exact hn0) (by rw [hcap]; exact hUA) hObj + have htyR := TyKeepA.stUpdA (ts := .restart) hx + have hk1 : (stUpdA σ σ.curThread x .restart).kheap (specPtr p) = some (.archObj (.asidPool f)) := by + have hn : specPtr p ≠ σ.curThread := fun e => by rw [e, hx] at hk; cases hk + simp [stUpdA, setObjA, Function.update_of_ne hn, hk] + have hk2 := Hs.keep hk1 (fun _ _ e => by cases e) (fun _ e => by cases e) + obtain ⟨hkind, hent⟩ := assignRootWordA E hab k hcap + have hzero := assignBankZeroA E hbase hhi hv hp hword hfree R Hs.asid + have ⟨⟨Rp, Nt, ex, sp, P⟩, Y, hrun, hcur, hvs, hvc, hsub⟩ := RS + obtain ⟨x', hx', _, _, E1, _, OK1, _, _⟩ := E.restartA P Y hrun hcur hvs hvc + have exx : x' = x := by rw [hx] at hx'; cases hx'; rfl + subst x' + have halN := FootNA.aligned E.j.1 (bxi_nodes E.sys.ent.bxi) E.scAl E.ap.vcAl + have sA : Aligned (stA (L.curW s)) := by + rw [stA_eq]; exact field_aligned (E.sq.tcbs _ E.sq.curT) (by simp) (by simp) + have hOSt := E.stOffA hvc + have hVsMem : ∀ b, VsFootA Z.Pp Z.S1 Z.Y (L.ksb s) b → L.mem s2 b = L.mem s b := fun b hb => by + rw [hw2.2.2 b (halN _ (.vs b hb)) (footN_offA E1 b (.vs b (by rw [L.ksb_wr]; exact hb))), + mem_wr_ne' sA (halN _ (.vs b hb)) (Ne.symm (hOSt.vs b hb))] + have hExtMem : ∀ b, ExtFootE Z.X Z.Y Z.T2 b → L.mem s2 b = L.mem s b := fun b hb => by + rw [hw2.2.2 b (halN _ (.ext b hb)) (footN_offA E1 b (.ext b hb)), + mem_wr_ne' sA (halN _ (.ext b hb)) (Ne.symm (hOSt.ext b hb))] + have hunmVs2 : (tagA (root ||| assignTagWordA k) = 0 → (root ||| assignTagWordA k) ≠ 0#u64 → + ((∀ j : U64, j.val < 512 → dBitA (L.mem s2 (peAF (untagA (root ||| assignTagWordA k)) j)) 0 = false) ∧ + ∀ t, σ2.kheap (specPtr (untagA (root ||| assignTagWordA k))) = some (.archObj (.pageTable t)) → + ∀ j : U64, j.val < 512 → ptApply t j.bv = .invalidPTE)) ∧ + (tagA (root ||| assignTagWordA k) = 2 → + ((∀ j : U64, j.val < 512 → dBitA (L.mem s2 (peAF (wa (untagA (root ||| assignTagWordA k)) 4096#u64) j)) 0 = false) ∧ + ∀ l1, σ2.kheap (specPtr (untagA (root ||| assignTagWordA k))) = some (.archObj (.ext (.highVSpace l1))) → + ∀ j : Fin 512, l1 j = .invalidPTE)) := by + cases k with + | stage1 => + have hrootS1 : root ∈ Z.S1 := (hOldObj.2.2.2.1 root false oldAsid rfl).1 + obtain ⟨ht, hu⟩ := assignTagA (tag := assignTagWordA .stage1) (E.j.1.vs.al_mod hrootS1) (by decide) + obtain ⟨hinv0, t0, hkt0, het0⟩ := hunm + have hkt2 := Hs.keep (stUpd_keep hx hkt0 (fun _ h => by cases h)) (fun _ _ h => by cases h) (fun _ h => by cases h) + refine ⟨fun _ _ => ?_, fun ht2 => by rw [ht] at ht2; cases ht2⟩ + rw [hu] + refine ⟨fun j hj => by rw [hVsMem _ (.pt root j hrootS1 hj)]; exact hinv0 j hj, fun t hkt => ?_⟩ + rw [hkt2] at hkt; cases hkt; exact het0 + | stage2 => + have hrootX : root ∈ Z.X := hOldObj.2.1 root 0#u64 false oldAsid oldVa rfl + have hr : root.val % 4096 = 0 := Nat.mod_eq_zero_of_dvd (dvd_trans (by decide : 4096 ∣ 8192) (E.j.1.ext.xal root hrootX)) + obtain ⟨ht, -⟩ := assignTagA (tag := assignTagWordA .stage2) hr (by decide) + exact ⟨fun ht0 _ => (by rw [ht] at ht0; cases ht0), fun ht2 => (by rw [ht] at ht2; cases ht2)⟩ + | high => + have hrootY : root ∈ Z.Y := hOldObj.2.2.1 root false oldAsid rfl + have hr : root.val % 4096 = 0 := Nat.mod_eq_zero_of_dvd (dvd_trans (by decide : 4096 ∣ 8192) (E.j.1.vs.yal root hrootY)) + obtain ⟨ht, hu⟩ := assignTagA (tag := assignTagWordA .high) hr (by decide) + obtain ⟨hinv0, l1, hkl0, hel0⟩ := hunm + have hkl2 := Hs.keep (stUpd_keep hx hkl0 (fun _ h => by cases h)) (fun _ _ h => by cases h) (fun _ h => by cases h) + refine ⟨fun ht0 _ => (by rw [ht] at ht0; cases ht0), fun _ => ?_⟩ + rw [hu] + refine ⟨fun j hj => by rw [hVsMem _ (.high root j hrootY hj)]; exact hinv0 j hj, fun l1' hkl => ?_⟩ + rw [hkl2] at hkl; cases hkl; exact hel0 + have hunmS2' : tagA (root ||| assignTagWordA k) = 1 → + ((∀ j : U64, j.val < 1024 → dBitA (L.mem s2 (peAF (untagA (root ||| assignTagWordA k)) j)) 0 = false) ∧ + ∀ es, σ2.kheap (specPtr (untagA (root ||| assignTagWordA k))) = some (.archObj (.ext (.stage2Root es))) → + ∀ j : Fin 1024, es j = .invalidPTE) := by + cases k with + | stage1 => + have hrootS1 : root ∈ Z.S1 := (hOldObj.2.2.2.1 root false oldAsid rfl).1 + obtain ⟨ht, -⟩ := assignTagA (tag := assignTagWordA .stage1) (E.j.1.vs.al_mod hrootS1) (by decide) + exact fun ht1 => by rw [ht] at ht1; cases ht1 + | stage2 => + have hrootX : root ∈ Z.X := hOldObj.2.1 root 0#u64 false oldAsid oldVa rfl + have hr : root.val % 4096 = 0 := Nat.mod_eq_zero_of_dvd (dvd_trans (by decide : 4096 ∣ 8192) (E.j.1.ext.xal root hrootX)) + obtain ⟨-, hu⟩ := assignTagA (tag := assignTagWordA .stage2) hr (by decide) + obtain ⟨hinv0, es0, hke0, hee0⟩ := hunm + have hke2 := Hs.keep (stUpd_keep hx hke0 (fun _ h => by cases h)) (fun _ _ h => by cases h) (fun _ h => by cases h) + intro _ + rw [hu] + refine ⟨fun j hj => by rw [hExtMem _ (rootFootE hrootX (E.j.1.ext.xal root hrootX) hj)]; exact hinv0 j hj, + fun es hke => ?_⟩ + rw [hke2] at hke; cases hke; exact hee0 + | high => + have hrootY : root ∈ Z.Y := hOldObj.2.2.1 root false oldAsid rfl + have hr : root.val % 4096 = 0 := Nat.mod_eq_zero_of_dvd (dvd_trans (by decide : 4096 ∣ 8192) (E.j.1.vs.yal root hrootY)) + obtain ⟨ht, -⟩ := assignTagA (tag := assignTagWordA .high) hr (by decide) + exact fun ht1 => by rw [ht] at ht1; cases ht1 + obtain ⟨J3, OK3, HT3⟩ := assign_write_jointA E RS J hks hcpu hpP hi512 w hk2 hkind hzero hunmVs2 hunmS2' + obtain ⟨ac, hg, heq⟩ := getCap_eqA E1.j.1.st hab + change CapEqA (capA st (L.wr s (stA (L.curW s)) sched.TS_RESTART) (cslotAddrA dst)) ac at heq + rw [OK1.capA hos, hcap] at heq + have hac : ac = .archObjectCap (assignRootCapA k (specPtr root) none) := by + rw [← habs]; exact (heq.eq_abs (by cases k <;> simp [assignUnmappedA])).symm + subst hac + have hget : (.archObjectCap (assignRootCapA k (specPtr root) none), stUpdA σ σ.curThread x .restart) ∈ + (getCap (Z := Unit) dst (stUpdA σ σ.curThread x .restart)).1 := by rw [hg]; rfl + have hlo : asidLowBitsOf (asidOfA (wa base i)) = BitVec.ofNat 9 i.val := by + have hh := congrArg UScalar.val hmask + rw [and_maskA _ 9 _ rfl] at hh + exact (asidLo_eqA h16).trans (congrArg (BitVec.ofNat 9) hh) + have hstore := storePool_runA hk2 (asidOfA (wa base i)) (some (.asidPoolVSpace (specPtr root))) + rw [hlo] at hstore + refine ⟨L.wr s2 (peAF p i) (root ||| assignTagWordA k), + poolSetA σ2 p (Function.update f (BitVec.ofNat 9 i.val) (poolEntA (root ||| assignTagWordA k))), + _, ?_, hrestartS, assignPerformA k (specPtr p) (specPtr root) dst (asidOfA (wa base i)) + hget hsS (by rw [hstore, hent]; rfl), J3, + T2.keepO OK3, fun C => ?_⟩ + · rw [hrestart, bind_ok, hs, bind_ok, L.write_eq, bind_ok] + · exact C.of_slotFrA hsub T (Fr.keepR hos OK3) rfl rfl (fun _ h => h) (fun _ h => h) + (fun _ _ _ e => by cases k <;> cases e) (by change CapFinEqA _ (capA st s _); rw [hcap]; exact hfin) + (htyR.trans ((setCap_tyA _ _ _ _ hsS).trans HT3)) + +end Finish + +section AssignRefinement +open Fl4Spec Fl4SpecA64 +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {s : S} {a : A} {σ : State Unit} {m : McState} + +private theorem assignRootRelA (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) (T : TreeA st OS Z.G.chains Z.G.par s) + (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) + (k : ExtRootKind) (dst : CslotPtr) (hab : AbsSlotA Z.th dst) {root oldAsid oldVa p base : U64} + (hcap : capA st s (cslotAddrA dst) = assignUnmappedA k root oldAsid oldVa) + (hbase : CapWfA (.AsidPool p base)) + (hunm : UnmRootAssignA k (L.mem s) σ root) : + assignFl4A (st := st) k s a (L.curW s) (cslotAddrA dst) root p base + ⦃ res => VsTopPostA (L := L) AR Z OS m σ ((assignDecA (assignInvA k (specPtr p) dst) + (specPtr p) (asidOfA base)).run σ) s a res ⦄ := by + have hb : base.val < 65536 := hbase.1 + obtain ⟨hi, hshr, hv⟩ := shrA base 9#u64 512 (by decide) rfl + have hhi : hi.val < 128 := by omega + have hm : hi &&& 127#u64 = hi := + UScalar.eq_of_val_eq (by rw [and_maskA _ 7 _ rfl]; exact Nat.mod_eq_of_lt hhi) + have hread := asid_table_eq (st := st) s hi + rw [hm] at hread + have ht := E.j.1.vs.table hi hhi + rw [hv, ← asidHi_eqA hb] at ht + change asidTable σ (asidHighBitsOf (asidOfA base)) = _ at ht + unfold assignFl4A + rw [asidLowA, hshr, bind_ok, hread, bind_ok] + by_cases h0 : L.mem s (atAF (L.ksb s) hi) = 0#u64 + · rw [h0, optPtrA_zero] at ht + rw [if_pos h0, assignDec_noPoolA _ ht] + exact (WP.spec_ok _).2 ⟨rfl, rfl, .failedLookup false .invalidRoot, rfl, Or.inl ⟨rfl, trivial⟩⟩ + rw [if_neg h0] + rw [optPtrA_ne h0] at ht + by_cases hq : L.mem s (atAF (L.ksb s) hi) = p + · have hp : p ≠ 0#u64 := hq ▸ h0 + have hpP : p ∈ Z.Pp := hq ▸ E.j.1.vs.tmem hi hhi h0 + have hactP : ActivePoolA (L.mem s) (L.ksb s) p := ⟨hp, hi, hhi, hq⟩ + rw [hq] at ht + rw [hq] + simp only [bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + obtain ⟨f, hk, hpool0⟩ := E.j.1.vs.pool p hpP + have hpool := hpool0 hactP + have hPoolOff := E.poolOff hi + rw [hread, hq, WP.spec_ok] at hPoolOff + refine WP.spec_bind (assign_searchA (st := st) s base p) fun i ⟨hiLe, hall, hfree⟩ => ?_ + by_cases he : i = 512#u64 + · rw [if_pos he] + have hiV : i.val = 512 := by rw [he]; rfl + rw [assignDec_fullA _ ht hk (assign_free_emptyA hbase hpool (by simpa only [hiV] using hall))] + exact (WP.spec_ok _).2 ⟨rfl, rfl, .deleteFirst, rfl, Or.inl trivial⟩ + · have hi512 : i.val < 512 := by + exact Nat.lt_of_le_of_ne hiLe (fun h => he (UScalar.eq_of_val_eq h)) + have ⟨hasid, hentry⟩ := hfree hi512 + obtain ⟨_, _, hmask⟩ := assign_sumA hbase hi512 + have w : PoolW st H Z.Cs OS s (peAF p i) := by + simpa only [hmask, peAF] using hPoolOff hp (wa base i) + obtain ⟨s', σ', σ1, hRun, hRestart, hPerf, J, T', C⟩ := + assignFinishA E RS T hnul k dst hab hcap hbase hhi hv hp hq hi512 hasid w hpP hk hunm + have hf := (assign_free_iffA hbase hi512 (hpool i hi512)).2 ⟨hasid, hentry⟩ + have hDec := assignDec_memA (assignInvA k (specPtr p) dst) ht hk hf + rw [assign_asidA hbase hi512] at hDec + rw [if_neg he] + simp only [lift, bind_ok, bind_tc_ok, hmask] + have hm8 : i * 8#u64 = ok (core.num.U64.wrapping_mul i 8#u64) := by + apply eq_of_spec_fd + step as ⟨x, hx⟩ + apply UScalar.eq_of_val_eq + rw [wm8_valA hi512] + simpa [Nat.mul_comm] using hx + simp only [hm8, bind_ok, bind_tc_ok] + rw [show (do + let s1 ← sched.set_thread_state st s (L.curW s) sched.TS_RESTART + let s2 ← obj.Slot.set_cap st (cslotAddrA dst) s1 (assignMappedA k root (wa base i)) + let s3 ← st.write s2 (wa p (core.num.U64.wrapping_mul i 8#u64)) (root ||| assignTagWordA k) + ok (.Ok (), s3, a) : Result (core.result.Result Unit error.Exc × S × A)) = + ok (.Ok (), s', a) from hRun] + exact (WP.spec_ok _).2 ⟨_, hDec, σ1, hRestart, σ', [], hPerf, Z, rfl, rfl, J, T', C⟩ + · have hq' : (L.mem s (atAF (L.ksb s) hi) != p) = true := bne_iff_ne.2 hq + rw [if_pos hq', error.invalid_cap, bind_ok, + assignDec_wrongA _ ht (fun e => hq (specPtr_inj e).symm)] + exact (WP.spec_ok _).2 ⟨rfl, rfl, .invalidCapability 0, rfl, Or.inr ⟨0#u64, rfl, rfl⟩⟩ + + +private theorem rejectAssignRunA (ar : machine.Arch A) (p base : U64) (cte : CslotPtr) + (length buf : U64) (block call cd fp : Bool) (ex : Aeneas.Std.Array obj.Slot 3#usize) + {x : U64} (hex : vspace.extra ex 0#usize = ok (some x)) {c : capability.Cap} + (hcap : capA st s x = c) (hwf : CapWfA c) + (hbad : ¬ ∃ k root oldAsid oldVa, c = assignUnmappedA k root oldAsid oldVa) : + syscall.decode_invocation st ar s a fl4_abi.label.ARMASIDPoolAssign length (cslotAddrA cte) (.AsidPool p base) + block call cd fp buf ex = ok (.Err (.Sys (.InvalidCapability 1#u64)), s, a) := by + unfold syscall.decode_invocation vspace.decode + simp only [cur_thread_ok, bind_ok, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte, hex, capA_eq, hcap] + cases c <;> try solve | simp only [error.invalid_cap, bind_ok, bind_tc_ok, ↓reduceIte] + case Pml4 p mp asid => + cases mp + · exact (hbad ⟨.stage1, p, asid, 0#u64, rfl⟩).elim + · simp only [error.invalid_cap, bind_ok, bind_tc_ok, ↓reduceIte] + case Ept p lev mp asid va => + have e : lev = 0#u64 := UScalar.eq_of_val_eq hwf.1 + subst lev + cases mp + · exact (hbad ⟨.stage2, p, asid, va, rfl⟩).elim + · simp only [error.invalid_cap, bind_ok, ↓reduceIte] + rfl + case HighVSpace p mp asid => + cases mp + · exact (hbad ⟨.high, p, asid, 0#u64, rfl⟩).elim + · simp only [error.invalid_cap, bind_ok, ↓reduceIte] + +private theorem rejectAssignSpecA (calling : Bool) (args : List Data) (xSlot : CapRef) (cte dst : CslotPtr) + (p : ObjRef) (base : Asid) (rest : List (Cap × CslotPtr)) (σ : State Unit) + {c : capability.Cap} (hwf : CapWfA c) + (hbad : ¬ ∃ k root oldAsid oldVa, c = assignUnmappedA k root oldAsid oldVa) : + (fl4ArchDecodeInvocation (Z := Unit) calling (archL .armASIDPoolAssign) args xSlot cte (.asidPoolCap p base) + ((absCapA c, dst) :: rest)).run σ = ({(.error (.invalidCapability 1), σ)}, False) := by + have hl : invocationType (specLabelWord (archL .armASIDPoolAssign)) = .archInvocationLabel .armASIDPoolAssign := + by rw [invType_specA]; rfl + cases c <;> try + (unfold fl4ArchDecodeInvocation; dsimp only [absCapA]; apply run_liftME_errA + simp only [archDecodeInvocation, decodeAsidPoolInvocation, hl, ↓reduceIte, List.length_cons, Nat.zero_lt_succ, + List.getElem?_cons_zero, the]; rfl) + + case Frame p size rights dev mp ept asid va => + cases ept + · change (NonDetE.liftME Fl4ArchInvocation.spec _).run σ = _ + apply run_liftME_errA + simp only [archDecodeInvocation, decodeAsidPoolInvocation, hl, ↓reduceIte, List.length_cons, Nat.zero_lt_succ, + List.getElem?_cons_zero, the, absCapA, absFrameE, Bool.false_eq_true] + rfl + · change (NonDetE.liftME Fl4ArchInvocation.extVSpace + (decodeExtAsidPoolAssign (.asidPoolCap _ base) ((absCapA (.Frame p size rights dev mp true asid va), dst) :: rest))).run σ = _ + apply run_liftME_errA + simp only [decodeExtAsidPoolAssign, absCapA, absFrameE, ↓reduceIte, ExtArchCap.isRoot, Bool.false_eq_true, + false_and] + rfl + case Pml4 p mp asid => + cases mp + · exact (hbad ⟨.stage1, p, asid, 0#u64, rfl⟩).elim + · unfold fl4ArchDecodeInvocation + dsimp only [absCapA, mapDataA] + apply run_liftME_errA + simp only [archDecodeInvocation, decodeAsidPoolInvocation, hl, ↓reduceIte, List.length_cons, Nat.zero_lt_succ, + List.getElem?_cons_zero, the] + rfl + case Ept p lev mp asid va => + cases mp + · have e : lev = 0#u64 := UScalar.eq_of_val_eq hwf.1 + subst lev + exact (hbad ⟨.stage2, p, asid, va, rfl⟩).elim + · change (NonDetE.liftME Fl4ArchInvocation.extVSpace + (decodeExtAsidPoolAssign (.asidPoolCap _ base) ((absCapA (.Ept p lev true asid va), dst) :: rest))).run σ = _ + apply run_liftME_errA + simp only [decodeExtAsidPoolAssign, absCapA, asidDataA, ↓reduceIte, ExtArchCap.mapData, Option.some_ne_none, and_false] + rfl + case HighVSpace p mp asid => + cases mp + · exact (hbad ⟨.high, p, asid, 0#u64, rfl⟩).elim + · change (NonDetE.liftME Fl4ArchInvocation.extVSpace + (decodeExtAsidPoolAssign (.asidPoolCap _ base) ((absCapA (.HighVSpace p true asid), dst) :: rest))).run σ = _ + apply run_liftME_errA + simp only [decodeExtAsidPoolAssign, absCapA, asidDataA, ↓reduceIte, ExtArchCap.mapData, Option.some_ne_none, and_false] + rfl + +set_option maxHeartbeats 2000000 in +/-- ARMASIDPoolAssign refines l4v for Stage-1, Stage-2 and high-window root caps. -/ +theorem decInv_asidPool_assign_jA (ar : machine.Arch A) (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) (T : TreeA st OS Z.G.chains Z.G.par s) + (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) + (cte : CslotPtr) (hab : AbsSlotA Z.th cte) {p base : U64} + (hpool : capA st s (cslotAddrA cte) = .AsidPool p base) + (length buf : U64) (block call cd fp calling : Bool) (ex : Aeneas.Std.Array obj.Slot 3#usize) + (args : List U64) (xSlot : CapRef) (exs : List (Cap × CslotPtr)) + (hex : ExtraOKA Z.th (L.mem s) ex exs) + (hAssign : ∀ u ∈ OS, ∀ k root oldAsid oldVa, + capA st s u = assignUnmappedA k root oldAsid oldVa → UnmRootAssignA k (L.mem s) σ root) : + syscall.decode_invocation st ar s a fl4_abi.label.ARMASIDPoolAssign length (cslotAddrA cte) + (.AsidPool p base) block call cd fp buf ex + ⦃ res => VsTopPostA (L := L) AR Z OS m σ ((fl4ArchDecodeInvocation (Z := Unit) calling + (fl4LabelOf fl4_abi.label.ARMASIDPoolAssign.bv) (args.map UScalar.bv) xSlot cte + (.asidPoolCap (specPtr p) (asidOfA base)) exs).run σ) s a res ⦄ := by + have hl : fl4LabelOf fl4_abi.label.ARMASIDPoolAssign.bv = archL .armASIDPoolAssign := by + rw [fl4LabelOf_bvA, Fl4ProofsA64.P0.Consts.fl4_abi.label.ARMASIDPoolAssign.val_eq] + decide + rw [hl] + obtain ⟨_, _, hwf, _⟩ := StateRelA.getAbsA E.j.1.st hab + change CapWfA (capA st s (cslotAddrA cte)) at hwf + rw [hpool] at hwf + have hread := extra0_eqA (exAtA_idx0 ex) + rcases hex.relS E.j.1.place with ⟨h0, rfl⟩ | ⟨hn, q, idx, rest, hcap0, hq, haddr, hce, rfl⟩ + · rw [h0, if_pos rfl] at hread + have hRun : syscall.decode_invocation st ar s a fl4_abi.label.ARMASIDPoolAssign length (cslotAddrA cte) + (.AsidPool p base) block call cd fp buf ex = ok (.Err (.Sys .TruncatedMessage), s, a) := by + simp only [syscall.decode_invocation, cur_thread_ok, bind_ok, vspace.decode, bne_self_eq_false, + Bool.false_eq_true, ↓reduceIte, hread, error.truncated] + have hSpec : (fl4ArchDecodeInvocation (Z := Unit) calling (archL .armASIDPoolAssign) (args.map UScalar.bv) + xSlot cte (.asidPoolCap (specPtr p) (asidOfA base)) []).run σ = ({(.error .truncatedMessage, σ)}, False) := by + change (NonDetE.liftME Fl4ArchInvocation.spec _).run σ = _ + apply run_liftME_errA + have hLab : invocationType (specLabelWord (archL .armASIDPoolAssign)) = .archInvocationLabel .armASIDPoolAssign := + by rw [invType_specA]; rfl + simp only [archDecodeInvocation, decodeAsidPoolInvocation, hLab, ↓reduceIte, List.length_nil, Nat.lt_irrefl] + rfl + rw [hRun, hSpec] + exact (WP.spec_ok _).2 ⟨rfl, rfl, .truncatedMessage, rfl, Or.inl trivial⟩ + · rw [if_neg hn, haddr] at hread + rw [haddr] at hce + let dst : CslotPtr := (specPtr q, idx) + have hd : AbsSlotA Z.th dst := ⟨q, rfl, Or.inl hq⟩ + change CapEqA (capA st s (cslotAddrA dst)) hcap0 at hce + by_cases hg : ∃ k root oldAsid oldVa, capA st s (cslotAddrA dst) = assignUnmappedA k root oldAsid oldVa + · obtain ⟨k, root, oldAsid, oldVa, he⟩ := hg + have hceq : hcap0 = absCapA (assignUnmappedA k root oldAsid oldVa) := by + rw [he] at hce; exact (hce.eq_abs (by cases k <;> simp [assignUnmappedA])).symm + subst hceq + have ha : absCapA (assignUnmappedA k root oldAsid oldVa) = + .archObjectCap (assignRootCapA k (specPtr root) none) := by cases k <;> rfl + rw [decodeAssignRunA ar s a k root p base oldAsid oldVa cte dst length buf block call cd fp ex hread he, + ha, specDecodeAssignA] + exact assignRootRelA E RS T hnul k dst hd he hwf + (hAssign (cslotAddrA dst) (RS.sub _ (hd.isSlot E.j.1.place)) k root oldAsid oldVa he) + · obtain ⟨_, _, hw, _⟩ := StateRelA.getAbsA E.j.1.st hd + change CapWfA (capA st s (cslotAddrA dst)) at hw + rw [rejectAssignRunA ar p base cte length buf block call cd fp ex hread rfl hw hg] + have hSpec : (fl4ArchDecodeInvocation (Z := Unit) calling (archL .armASIDPoolAssign) (args.map UScalar.bv) + xSlot cte (.asidPoolCap (specPtr p) (asidOfA base)) ((hcap0, dst) :: rest)).run σ = + ({(.error (.invalidCapability 1), σ)}, False) := by + by_cases hpt : ∃ pt asid va, capA st s (cslotAddrA dst) = .PageTable pt true asid va + · obtain ⟨pt, asid, va, hpt⟩ := hpt + rw [hpt] at hce + obtain ⟨asid', va', rfl, _, _⟩ := CapEqA.pt_mapped_iff.mp hce + have hl : invocationType (specLabelWord (archL .armASIDPoolAssign)) = .archInvocationLabel .armASIDPoolAssign := + by rw [invType_specA]; rfl + unfold fl4ArchDecodeInvocation + apply run_liftME_errA + simp only [archDecodeInvocation, decodeAsidPoolInvocation, hl, ↓reduceIte, List.length_cons, Nat.zero_lt_succ, + List.getElem?_cons_zero, the] + rfl + · have hceq : hcap0 = absCapA (capA st s (cslotAddrA dst)) := + (hce.eq_abs (fun pt asid va h => hpt ⟨pt, asid, va, h⟩)).symm + subst hceq + exact rejectAssignSpecA calling (args.map UScalar.bv) xSlot cte dst _ _ rest σ hw hg + rw [hSpec] + exact (WP.spec_ok _).2 ⟨rfl, rfl, .invalidCapability 1, rfl, Or.inr ⟨1#u64, rfl, rfl⟩⟩ + + +/-- The ASID-pool cap kind, including Assign and the illegal-label fallthrough. -/ +theorem decInv_asidPool_jA (ar : machine.Arch A) (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) (T : TreeA st OS Z.G.chains Z.G.par s) + (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) + (cte : CslotPtr) (hab : AbsSlotA Z.th cte) {p base : U64} + (hpool : capA st s (cslotAddrA cte) = .AsidPool p base) + (lbl length buf : U64) (block call cd fp calling : Bool) (ex : Aeneas.Std.Array obj.Slot 3#usize) + (args : List U64) (xSlot : CapRef) (exs : List (Cap × CslotPtr)) + (hex : ExtraOKA Z.th (L.mem s) ex exs) + (hAssign : lbl = fl4_abi.label.ARMASIDPoolAssign → ∀ u ∈ OS, ∀ k root oldAsid oldVa, + capA st s u = assignUnmappedA k root oldAsid oldVa → UnmRootAssignA k (L.mem s) σ root) : + syscall.decode_invocation st ar s a lbl length (cslotAddrA cte) (.AsidPool p base) block call cd fp buf ex + ⦃ res => VsTopPostA (L := L) AR Z OS m σ ((fl4ArchDecodeInvocation (Z := Unit) calling + (fl4LabelOf lbl.bv) (args.map UScalar.bv) xSlot cte (.asidPoolCap (specPtr p) (asidOfA base)) exs).run σ) + s a res ⦄ := by + by_cases he : lbl = fl4_abi.label.ARMASIDPoolAssign + · subst lbl + exact decInv_asidPool_assign_jA ar E RS T hnul cte hab hpool length buf block call cd fp calling ex args xSlot exs hex (hAssign rfl) + · exact decInv_asidPool_other_jA ar cte p base lbl length buf block call cd fp calling ex args xSlot exs he + +end AssignRefinement + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/VsBootA.lean b/proofs/Fl4ProofsA64/P3/VsBootA.lean new file mode 100644 index 000000000..b059d60a5 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/VsBootA.lean @@ -0,0 +1,119 @@ +/- +P3 (aarch64, A64-P3 VS gen 1): **the VSpace boot witness at A64-Boot's `w0`** — `VsRelA`, the spec invariant +`SpecVsA` and the hardware-root tie `HwTieA` at the pair `jbootA_w0` relates (implementation `bootS kb ib nc r now +mt`, abstract `bootSA kb ib nc now mt`, ghosts `bootZA kb ib nc`). + +* **`boot_asidW`**: every ASID-table word of `w0` is `0` (the block's words past the nodes that `ksW` does not set); +* **`boot_vsRelA`**: `VsRelA ∅ ∅ Lv ∅ ∅` at `w0` (no pool, table or window; the spec's ASID table is + `init_arch_state`'s empty one), for any `Lv` (as `boot_sysInvA`'s `Lv`); `Stage2CoreA` / `HighVSpaceCoreA` already + have `X = Y = ∅` there (`bootZA`); +* **`boot_specVsA`**: `SpecVsA` at `bootSA` (no pool, page table or FL4 root in the boot heap); +* **`boot_hwTieA`** (gen 2: for every Stage-2 ghost `s2`, the Stage-2 part vacuous, `boot_vcpuOfA`): + `HwTieA (bootZA kb ib nc) s2 (bootSA kb ib nc now mt)`: the boot ghost root `bootHwA` (l4v's global + user VSpace, no ASID) is what `set_vm_root` of core 0's idle thread installs (its slot 1 is null); +* **`boot_vsA_ex`**: all three at A64-Boot's concrete layout (`kb = PPTR_BASE + 2^30`, `ib = PPTR_BASE + 2^31`, 128 cores). + +Not covered: `w1` (A64-Boot's `BootW1`: the root task's ASID pool, VSpace root and the 0 → 0 → 2 → L3 walk); sized +in the handoff. No named premise; the layout facts are A64-Boot's `BootLay` (`BootLayA` for the idle area). +-/ +import Fl4ProofsA64.P3.VsInvA +import Fl4ProofsA64.P3.BootAddrA +import Fl4ProofsA64.P3.JBootW0A +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P1.W7.R.ST.BootWit + +noncomputable section + +section +variable {kb ib r now mt : U64} {nc : Nat} + +/-- **`VsRelA` at `w0`** (module docstring; gen 12: `JBootA.boot_vsRelA_of` at `boot_asidW`, both moved upstream for +`JAtZA.vs`). -/ +theorem boot_vsRelA (P : BootLay kb ib nc) (Lv : U64 → Nat) : + VsRelA ∅ ∅ Lv ∅ ∅ (bootM kb ib nc r now mt) kb (bootSA kb ib nc now mt) := + boot_vsRelA_of (fun _ hh => boot_asidW P hh) Lv + +/-- **`VsRelA` at the store state `bootS`** (its memory and kernel base are `bootM` and `kb`). -/ +theorem boot_vsRelA_s (P : BootLay kb ib nc) (Lv : U64 → Nat) : + VsRelA ∅ ∅ Lv ∅ ∅ (bL.mem (bootS kb ib nc r now mt)) (bL.ksb (bootS kb ib nc r now mt)) + (bootSA kb ib nc now mt) := + boot_vsRelA P Lv + +/-- **`SpecVsA` at the abstract boot state** (module docstring). -/ +theorem boot_specVsA (hL : BootLayA ib nc) : SpecVsA (bootSA kb ib nc now mt) where + tab hi p h := by + have e : (bootSA kb ib nc now mt).archState.armAsidTable hi = none := rfl + rw [e] at h; cases h + tabInj hi hi' p h := by + have e : (bootSA kb ib nc now mt).archState.armAsidTable hi = none := rfl + rw [e] at h; cases h + asid0 p f h := by + have e : (bootSA kb ib nc now mt).archState.armAsidTable 0 = none := rfl + rw [e] at h; cases h + pool p f lo e hk := by + rcases bootSA_cases hL hk with ⟨c, -, -, sc, e'⟩ | ⟨t, e'⟩ | e' <;> cases e' + pte p t i ppn hk := by + rcases bootSA_cases hL hk with ⟨c, -, -, sc, e'⟩ | ⟨t, e'⟩ | e' <;> cases e' + hpte p l1 i ppn hk := by + rcases bootSA_cases hL hk with ⟨c, -, -, sc, e'⟩ | ⟨t, e'⟩ | e' <;> cases e' + +/-- Core `c`'s idle TCB in the boot state. -/ +theorem bootSA_tcbA (hL : BootLayA ib nc) {c : Nat} (hc : c < nc) : + (bootSA kb ib nc now mt).kheap (specPtr (idleTA ib c)) = + some (.tcb (idleTcbA (scRenA (bootMA ib nc) (idleSA ib c)))) := by + rw [bootSA_kheap]; exact bootKA_tcb hL hc + +theorem vtCapA_of {σ : Fl4SpecA64.State Unit} {t : Fl4SpecA64.ObjRef} {x : Fl4SpecA64.Tcb} + (h : σ.kheap t = some (.tcb x)) : vtCapA σ t = Fl4SpecA64.tcbCnodeMap x (Fl4SpecA64.tcbCnodeIndex 1) := by + unfold vtCapA; rw [h] + +theorem vcpuOfA_of {σ : Fl4SpecA64.State Unit} {t : Fl4SpecA64.ObjRef} {x : Fl4SpecA64.Tcb} + (h : σ.kheap t = some (.tcb x)) : vcpuOfA σ t = x.tcbArch.tcbVcpu := by + unfold vcpuOfA; rw [h] + +theorem idleTcbA_vcpu (sc : Fl4SpecA64.ObjRef) : (idleTcbA sc).tcbArch.tcbVcpu = none := rfl + +/-- Core `c`'s idle thread holds a null cap in slot 1. -/ +theorem boot_vtCapA_c (hL : BootLayA ib nc) {c : Nat} (hc : c < nc) : + vtCapA (bootSA kb ib nc now mt) (specPtr (idleTA ib c)) = some .nullCap := + (vtCapA_of (bootSA_tcbA hL hc)).trans (idleTcbA_slot _ (by decide)) + +/-- Core 0's idle thread holds a null cap in slot 1. -/ +theorem boot_vtCapA (hL : BootLayA ib nc) : + vtCapA (bootSA kb ib nc now mt) (specPtr (idleTA ib 0)) = some .nullCap := boot_vtCapA_c hL hL.nc0 + +/-- **The hardware-root tie at boot** (module docstring). -/ +theorem boot_hwS1TieA (hL : BootLayA ib nc) : HwS1TieA (bootZA kb ib nc) (bootSA kb ib nc now mt) := by + unfold HwS1TieA + show bootHwA = vmRootA (bootSA kb ib nc now mt) (specPtr (idleTA ib 0)) + exact (vmRootA_global (σ := bootSA kb ib nc now mt) (t := (specPtr (idleTA ib 0))) + (fun pt asid v => by rw [boot_vtCapA hL]; exact fun e => by cases e)).symm + +/-- Core 0's idle thread has no VCPU. -/ +theorem boot_vcpuOfA_c (hL : BootLayA ib nc) {c : Nat} (hc : c < nc) : + vcpuOfA (bootSA kb ib nc now mt) (specPtr (idleTA ib c)) = none := + (vcpuOfA_of (bootSA_tcbA hL hc)).trans (idleTcbA_vcpu _) + +theorem boot_vcpuOfA (hL : BootLayA ib nc) : vcpuOfA (bootSA kb ib nc now mt) (specPtr (idleTA ib 0)) = none := + boot_vcpuOfA_c hL hL.nc0 + +/-- **The hardware-root tie at boot** (module docstring; gen 2: Stage-1 and the vacuous Stage-2 part, any `s2`). -/ +theorem boot_hwTieA (hL : BootLayA ib nc) (s2 : Option HwRootA) : + HwTieA (bootZA kb ib nc) s2 (bootSA kb ib nc now mt) := + ⟨boot_hwS1TieA hL, HwS2TieA.of_none (boot_vcpuOfA hL)⟩ + +end + +/-- **The VSpace witness at A64-Boot's concrete `w0`** (module docstring; the layout of `jbootA_w0_ex`). -/ +theorem boot_vsA_ex (r now mt : U64) (Lv : U64 → Nat) (s2 : Option HwRootA) : + VsRelA ∅ ∅ Lv ∅ ∅ (bL.mem (bootS 18446603337294938112#u64 18446603338368679936#u64 128 r now mt)) + (bL.ksb (bootS 18446603337294938112#u64 18446603338368679936#u64 128 r now mt)) (bootSA 18446603337294938112#u64 18446603338368679936#u64 128 now mt) ∧ + SpecVsA (bootSA 18446603337294938112#u64 18446603338368679936#u64 128 now mt) ∧ + HwTieA (bootZA 18446603337294938112#u64 18446603338368679936#u64 128) s2 (bootSA 18446603337294938112#u64 18446603338368679936#u64 128 now mt) := + ⟨boot_vsRelA_s bootLayC_ex Lv, boot_specVsA bootLayA_ex, boot_hwTieA bootLayA_ex s2⟩ + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/VsFindA.lean b/proofs/Fl4ProofsA64/P3/VsFindA.lean new file mode 100644 index 000000000..caf61c05b --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/VsFindA.lean @@ -0,0 +1,477 @@ +/- +P3 (aarch64, A64-P3 VS gen 1, gen 2): **the first VSpace lookup's correspondence** — FL4's `find_asid_raw` / `find_asid` +(`vspace.rs` 229–251; `find_vspace_for_asid` is `find_asid`) against l4v's `vspace_for_asid` / `find_vspace_for_asid` +(`AARCH64/VSpace`), under `VsRelA`. The twin of x86's `find_asid_native_rel` (`P3/VsRel`). + +* `specVsA σ a` and **`vspaceForAsid_posA`**: l4v's `vspace_for_asid` of a nonzero ASID, the reader-option binds + written out (pool of the high bits, entry of the low bits, its `ap_vspace`); +* **`find_asid_rawA`**: for every 16-bit ASID (gen 2: ASID 0 included, by `VsRelA.asid0` and + `vspaceForAsid_zeroA`), FL4's raw lookup errs iff l4v's `vspace_for_asid` is `None`, and an `Ok w` is l4v's root + untagged and shifted (`specPtr (untagA w)`), nonzero, of the kind its tag says (`EntKindA`); +* **`find_asid_findA`** (`find_asid` = `find_vspace_for_asid`): an `Ok w` is l4v's `vspace_for_asid` and a Stage-1 + root (`rootKindAt σ w = stage1`, `Ext/Stage2`); an `Err` is l4v's `None` or a root of another kind (a Stage-2 root + or a high window: FL4's tag check, which l4v's `find_vspace_for_asid` leaves to `set_vm_root`'s `pt ≠ pt'` and + `ArmHighVSpace`'s own lookup). The Stage-2 roots' objects are taken from `Stage2CoreA.obj` (`hX`, stated at the + shifted pointer: `Stage2CoreA` moves to `specPtr` with the CSpace relations at the merge). + +No named premise (`hX` is `Stage2CoreA.obj` of the joint relation). No `Fl4SpecA64` edit. +-/ +import Fl4ProofsA64.P3.VsTieA +import Mathlib.Tactic.NormNum +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 + +noncomputable section + +/-! ## The spec's lookup -/ + +/-- l4v's `vspace_for_asid` past its `0 < asid` assertion (module docstring). -/ +def specVsA (σ : Fl4SpecA64.State Unit) (a : Fl4SpecA64.Asid) : Option Fl4SpecA64.ObjRef := + Option.bind (Option.bind (σ.archState.armAsidTable (Fl4SpecA64.asidHighBitsOf a)) fun p => + Option.bind (Fl4SpecA64.asidPoolsOf σ p) fun pool => pool (Fl4SpecA64.asidLowBitsOf a)) fun e => some e.apVspace + +theorem vspaceForAsid_posA {σ : Fl4SpecA64.State Unit} {a : Fl4SpecA64.Asid} (h : 0 < a) : + Fl4SpecA64.vspaceForAsid a σ = specVsA σ a := by + unfold Fl4SpecA64.vspaceForAsid Fl4Spec.Lookup.oassert + rw [if_pos h] + rfl + +theorem asidPoolsOf_eqA {σ : Fl4SpecA64.State Unit} {p : Fl4SpecA64.ObjRef} {f : Fl4SpecA64.AsidPool} + (hk : σ.kheap p = some (.archObj (.asidPool f))) : Fl4SpecA64.asidPoolsOf σ p = some f := by + unfold Fl4SpecA64.asidPoolsOf Fl4SpecA64.aobjsOf Fl4Spec.optMap + rw [hk] <;> rfl + +theorem specVsA_eqA {σ : Fl4SpecA64.State Unit} {a : Fl4SpecA64.Asid} {p : Fl4SpecA64.ObjRef} + {f : Fl4SpecA64.AsidPool} (ht : σ.archState.armAsidTable (Fl4SpecA64.asidHighBitsOf a) = some p) + (hk : σ.kheap p = some (.archObj (.asidPool f))) : + specVsA σ a = Option.bind (f (Fl4SpecA64.asidLowBitsOf a)) fun e => some e.apVspace := by + unfold specVsA + rw [ht] + show Option.bind (Option.bind (Fl4SpecA64.asidPoolsOf σ p) fun pool => pool (Fl4SpecA64.asidLowBitsOf a)) + (fun e => some e.apVspace) = _ + rw [asidPoolsOf_eqA hk] <;> rfl + +theorem asidHi_eqA {v : Nat} (hv : v < 65536) : + Fl4SpecA64.asidHighBitsOf (BitVec.ofNat 16 v) = BitVec.ofNat 7 (v / 512) := by + apply BitVec.eq_of_toNat_eq + simp [Fl4SpecA64.asidHighBitsOf, Fl4SpecA64.asidLowBits, Fl4SpecA64.asidLowLen, Fl4SpecA64.asidHighLen, + Fl4SpecA64.asidRepLen, Fl4SpecA64.asidLen, Fl4SpecA64.ucast, BitVec.toNat_ushiftRight, + Nat.shiftRight_eq_div_pow] <;> omega + +theorem asidLo_eqA {v : Nat} (hv : v < 65536) : + Fl4SpecA64.asidLowBitsOf (BitVec.ofNat 16 v) = BitVec.ofNat 9 (v % 512) := by + apply BitVec.eq_of_toNat_eq + simp [Fl4SpecA64.asidLowBitsOf, Fl4SpecA64.asidLowLen, Fl4SpecA64.asidRepLen, Fl4SpecA64.asidLen, + Fl4SpecA64.ucast] <;> omega + +/-! ## Words -/ + +theorem optPtrA_zero : optPtrA 0#u64 = none := by unfold optPtrA; rw [if_pos rfl] + +theorem optPtrA_ne {w : U64} (h : w ≠ 0#u64) : optPtrA w = some (specPtr w) := by unfold optPtrA; rw [if_neg h] + +theorem poolEntA_zero : poolEntA 0#u64 = none := by unfold poolEntA; rw [if_pos rfl] + +theorem poolEntA_ne {w : U64} (h : w ≠ 0#u64) : poolEntA w = some (.asidPoolVSpace (specPtr (untagA w))) := by + unfold poolEntA; rw [if_neg h] + +theorem shrA (x k : U64) (d : Nat) (hk : k.val < 64) (hd : 2 ^ k.val = d) : + ∃ i : U64, x >>> k = ok i ∧ i.val = x.val / d := + ⟨_, CapRt.shr_u64_eq x k hk, by + show (x.bv >>> k.val).toNat = _ + rw [BitVec.toNat_ushiftRight, Nat.shiftRight_eq_div_pow, hd] <;> rfl⟩ + +theorem and_maskA (x : U64) (k : Nat) (K : U64) (hK : K.val = 2 ^ k - 1) : (x &&& K).val = x.val % 2 ^ k := by + rw [UScalar.val_and, hK, Nat.and_two_pow_sub_one_eq_mod] + +theorem and3_valA (w : U64) : (w &&& 3#u64).val = w.val % 4 := and_maskA w 2 _ rfl + +theorem tag_iffA (w : U64) : w &&& 3#u64 = 0#u64 ↔ tagA w = 0 := by + unfold tagA + constructor + · intro h + have := congrArg UScalar.val h + rw [and3_valA] at this + exact this.trans rfl + · intro h + apply UScalar.eq_of_val_eq + rw [and3_valA] + exact h.trans rfl + +/-- The high bits of a word (gen 2: any `k`, for the tag and the table base). -/ +theorem nat_and_hiA (n k : Nat) (hn : n < 2 ^ 64) (hk : k ≤ 64) : + n &&& (2 ^ 64 - 1 - (2 ^ k - 1)) = 2 ^ k * (n / 2 ^ k) := by + have h1 : 1 ≤ 2 ^ k := Nat.one_le_two_pow + have h2 : 2 ^ k * 2 ^ (64 - k) = 2 ^ 64 := by rw [← Nat.pow_add, Nat.add_sub_cancel' hk] + have e : (2 ^ 64 - 1 - (2 ^ k - 1) : Nat) = 2 ^ k * (2 ^ (64 - k) - 1) := by + rw [Nat.mul_sub_one, h2]; omega + apply Nat.eq_of_testBit_eq + intro i + rw [Nat.testBit_and, e, Nat.testBit_two_pow_mul, Nat.testBit_two_pow_mul, Nat.testBit_two_pow_sub_one, + Nat.testBit_div_two_pow] + by_cases h : k ≤ i + · have e2 : i - k + k = i := by omega + rw [e2] + by_cases hlt : i < 64 + · simp [h, show i - k < 64 - k by omega] + · have e3 : n.testBit i = false := + Nat.testBit_lt_two_pow (lt_of_lt_of_le hn (Nat.pow_le_pow_right (by norm_num) (by omega))) + simp [h, e3] + · simp [h] + +/-- A tag-0 entry is its own root. -/ +theorem untagA_of {w : U64} (h : tagA w = 0) : untagA w = w := by + apply UScalar.eq_of_val_eq + unfold untagA + rw [UScalar.val_and, show (18446744073709551612#u64 : U64).val = 2 ^ 64 - 1 - (2 ^ 2 - 1) from rfl, + nat_and_hiA w.val 2 w.bv.isLt (by norm_num)] + unfold tagA at h + have e : (2 : Nat) ^ 2 = 4 := rfl + rw [e]; omega + +/-! ## The kinds of roots -/ + +theorem rootKind_ptA {σ : Fl4SpecA64.State Unit} {r : Fl4SpecA64.ObjRef} {t : Fl4SpecA64.PT} + (hk : σ.kheap r = some (.archObj (.pageTable t))) : Fl4SpecA64.rootKindAt σ r = .stage1 := by + simp [Fl4SpecA64.rootKindAt, Fl4SpecA64.extObjAt, hk] + +theorem rootKind_s2A {σ : Fl4SpecA64.State Unit} {r : Fl4SpecA64.ObjRef} {es : Fin 1024 → Fl4SpecA64.PTE} + (hk : σ.kheap r = some (.archObj (.ext (.stage2Root es)))) : Fl4SpecA64.rootKindAt σ r = .stage2 := by + simp [Fl4SpecA64.rootKindAt, Fl4SpecA64.extObjAt, hk] + +theorem rootKind_highA {σ : Fl4SpecA64.State Unit} {r : Fl4SpecA64.ObjRef} {l1 : Fin 512 → Fl4SpecA64.PTE} + (hk : σ.kheap r = some (.archObj (.ext (.highVSpace l1)))) : Fl4SpecA64.rootKindAt σ r = .high := by + simp [Fl4SpecA64.rootKindAt, Fl4SpecA64.extObjAt, hk] + +/-! ## FL4's lookups -/ + +/-- What `find_asid_raw`'s result says of l4v's `vspace_for_asid` (module docstring). -/ +def RawRelA (Tb : Set U64) (Lv : U64 → Nat) (X Y : Set U64) (σ : Fl4SpecA64.State Unit) (a : Fl4SpecA64.Asid) : + core.result.Result U64 error.LookupFault → Prop + | .Ok w => Fl4SpecA64.vspaceForAsid a σ = some (specPtr (untagA w)) ∧ w ≠ 0#u64 ∧ EntKindA Tb Lv X Y w + | .Err _ => Fl4SpecA64.vspaceForAsid a σ = none + +/-- What `find_asid`'s (`find_vspace_for_asid`'s) result says of l4v's (module docstring). -/ +def FindRelA (σ : Fl4SpecA64.State Unit) (a : Fl4SpecA64.Asid) : + core.result.Result U64 error.LookupFault → Prop + | .Ok w => Fl4SpecA64.vspaceForAsid a σ = some (specPtr w) ∧ Fl4SpecA64.rootKindAt σ (specPtr w) = .stage1 + | .Err _ => ∀ p, Fl4SpecA64.vspaceForAsid a σ = some p → Fl4SpecA64.rootKindAt σ p ≠ .stage1 + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {Pp Tb : Set U64} {Lv : U64 → Nat} {X Y : Set U64} {σ : Fl4SpecA64.State Unit} + +theorem asidBitsA : vspace.ASID_BITS = 16#u64 := by unfold vspace.ASID_BITS; rfl +theorem asidLowA : vspace.ASID_LOW_BITS = 9#u64 := by unfold vspace.ASID_LOW_BITS; rfl + +/-- l4v's `vspace_for_asid` of ASID 0 is `None` (its `0 < asid` assertion). -/ +theorem vspaceForAsid_zeroA (σ : Fl4SpecA64.State Unit) : Fl4SpecA64.vspaceForAsid (BitVec.ofNat 16 0) σ = none := by + unfold Fl4SpecA64.vspaceForAsid Fl4Spec.Lookup.oassert + rw [if_neg (by rw [BitVec.lt_def]; simp)] + rfl + +set_option maxHeartbeats 2000000 in +/-- **`find_asid_raw` against l4v's `vspace_for_asid`** (module docstring; gen 2: every 16-bit ASID, ASID 0 by +`VsRelA.asid0`). -/ +theorem find_asid_rawA {s : S} (hR : VsRelA Pp Tb Lv X Y (L.mem s) (L.ksb s) σ) {asid : U64} + (h16 : asid.val < 65536) : + vspace.find_asid_raw st s asid ⦃ r => RawRelA Tb Lv X Y σ (BitVec.ofNat 16 asid.val) r ⦄ := by + have hspec : asid.val ≠ 0 → Fl4SpecA64.vspaceForAsid (BitVec.ofNat 16 asid.val) σ = + specVsA σ (BitVec.ofNat 16 asid.val) := fun h => by + apply vspaceForAsid_posA + rw [BitVec.lt_def] + simp [Fl4SpecA64.asidRepLen, Fl4SpecA64.asidLen] + omega + have hzero : asid.val = 0 → Fl4SpecA64.vspaceForAsid (BitVec.ofNat 16 asid.val) σ = none := fun h => by + rw [h]; exact vspaceForAsid_zeroA σ + unfold vspace.find_asid_raw + obtain ⟨i, ei, vi⟩ := shrA asid 16#u64 65536 (by decide) rfl + rw [asidBitsA, ei] + simp only [bind_ok] + have hi0 : i = 0#u64 := UScalar.eq_of_val_eq (by rw [vi]; exact (Nat.div_eq_of_lt h16).trans rfl) + subst hi0 + simp only [bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + obtain ⟨i1, e1, v1⟩ := shrA asid 9#u64 512 (by decide) rfl + rw [asidLowA, e1] + simp only [bind_ok] + rw [asid_table_eq] + simp only [bind_ok] + have hv127 : (i1 &&& 127#u64).val = asid.val / 512 := by + rw [and_maskA i1 7 _ rfl, v1] + have e : (2 : Nat) ^ 7 = 128 := rfl + rw [e]; omega + have hlt : (i1 &&& 127#u64).val < 128 := by omega + have ht := hR.table _ hlt + rw [hv127, ← asidHi_eqA h16] at ht + obtain ⟨pool, hpool⟩ : ∃ p, L.mem s (atAF (L.ksb s) (i1 &&& 127#u64)) = p := ⟨_, rfl⟩ + have hP0 := hR.tmem _ hlt + rw [hpool] at ht hP0 ⊢ + by_cases hp : pool = 0#u64 + · rw [if_pos hp] + refine (WP.spec_ok _).2 ?_ + show Fl4SpecA64.vspaceForAsid _ σ = none + by_cases ha : asid.val = 0 + · exact hzero ha + rw [hspec ha] + unfold specVsA + rw [ht, hp, optPtrA_zero] <;> rfl + · rw [if_neg hp] + simp only [lift, bind_ok, L.read_eq] + have hP := hP0 hp + have hactP : ActivePoolA (L.mem s) (L.ksb s) pool := ⟨hp, i1 &&& 127#u64, hlt, hpool⟩ + obtain ⟨f, hk, hf⟩ := hR.pool pool hP + have hlov : (asid &&& 511#u64).val = asid.val % 512 := by + rw [and_maskA asid 9 _ rfl] <;> rfl + have hlo : (asid &&& 511#u64).val < 512 := by omega + have he := hf hactP _ hlo + have hkd := hR.kind pool hP hactP _ hlo + rw [hlov, ← asidLo_eqA h16] at he + obtain ⟨root, hroot⟩ : ∃ r, L.mem s (peAF pool (asid &&& 511#u64)) = r := ⟨_, rfl⟩ + rw [hroot] at he hkd + have hs : asid.val ≠ 0 → Fl4SpecA64.vspaceForAsid (BitVec.ofNat 16 asid.val) σ = + Option.bind (poolEntA root) fun e => some e.apVspace := fun ha => by + rw [hspec ha, specVsA_eqA (by rw [ht, optPtrA_ne hp]) hk, he] + rw [show core.num.U64.wrapping_add pool (core.num.U64.wrapping_mul (asid &&& 511#u64) 8#u64) = + peAF pool (asid &&& 511#u64) from rfl, hroot] + by_cases hr : root = 0#u64 + · rw [if_pos hr] + refine (WP.spec_ok _).2 ?_ + show Fl4SpecA64.vspaceForAsid _ σ = none + by_cases ha : asid.val = 0 + · exact hzero ha + rw [hs ha, hr, poolEntA_zero] <;> rfl + · rw [if_neg hr] + have ha : asid.val ≠ 0 := by + intro ha + apply hr + have e1 : (i1 &&& 127#u64) = 0#u64 := UScalar.eq_of_val_eq (by rw [hv127, ha]; rfl) + have e2 : (asid &&& 511#u64) = 0#u64 := UScalar.eq_of_val_eq (by rw [hlov, ha]; rfl) + rw [e1] at hpool + have h' : L.mem s (atAF (L.ksb s) 0#u64) ≠ 0#u64 := by rw [hpool]; exact hp + rw [← hroot, e2, ← hpool] + exact hR.asid0 h' + refine (WP.spec_ok _).2 ⟨?_, hr, hkd⟩ + rw [hs ha, poolEntA_ne hr] <;> rfl + +set_option maxHeartbeats 2000000 in +/-- **`find_asid` (`find_vspace_for_asid`) against l4v's** (module docstring). -/ +theorem find_asid_findA {s : S} (hR : VsRelA Pp Tb Lv X Y (L.mem s) (L.ksb s) σ) + (hX : ∀ r ∈ X, ∃ es, σ.kheap (specPtr r) = some (.archObj (.ext (.stage2Root es)))) {asid : U64} + (h16 : asid.val < 65536) : + vspace.find_asid st s asid ⦃ r => FindRelA σ (BitVec.ofNat 16 asid.val) r ⦄ := by + unfold vspace.find_asid + refine WP.spec_bind (find_asid_rawA hR h16) fun r hr => ?_ + rcases r with w | e + · obtain ⟨hs, hw0, hk⟩ := hr + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok, lift, rtag, rs1] + split + · rename_i hne + have hne' : w &&& 3#u64 ≠ 0#u64 := by simpa using hne + have ht0 : tagA w ≠ 0 := fun h => hne' ((tag_iffA w).2 h) + refine (WP.spec_ok _).2 ?_ + show ∀ p, Fl4SpecA64.vspaceForAsid _ σ = some p → Fl4SpecA64.rootKindAt σ p ≠ .stage1 + intro p hp + rw [hs] at hp + obtain rfl := Option.some.inj hp + rcases hk with h | ⟨h, -, -⟩ | ⟨-, hx⟩ | ⟨-, hy⟩ + · exact absurd h hw0 + · exact absurd h ht0 + · obtain ⟨es, hes⟩ := hX _ hx + rw [rootKind_s2A hes]; decide + · obtain ⟨l1, hl, -⟩ := hR.high _ hy + rw [rootKind_highA hl]; decide + · rename_i hz + have ht : w &&& 3#u64 = 0#u64 := by apply UScalar.eq_of_val_eq; simpa using hz + have ht0 : tagA w = 0 := (tag_iffA w).1 ht + have hu := untagA_of ht0 + refine (WP.spec_ok _).2 ?_ + show Fl4SpecA64.vspaceForAsid _ σ = some (specPtr w) ∧ Fl4SpecA64.rootKindAt σ (specPtr w) = .stage1 + refine ⟨by rw [hs, hu], ?_⟩ + rcases hk with h | ⟨-, hT, -⟩ | ⟨h, -⟩ | ⟨h, -⟩ + · exact absurd h hw0 + · obtain ⟨t, hkt, -⟩ := hR.pt _ hT + rw [hu] at hkt + exact rootKind_ptA hkt + · omega + · omega + · simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame.from, + bind_tc_ok] + refine (WP.spec_ok _).2 ?_ + show ∀ p, Fl4SpecA64.vspaceForAsid _ σ = some p → Fl4SpecA64.rootKindAt σ p ≠ .stage1 + exact fun p hp => by + have hr' : Fl4SpecA64.vspaceForAsid (BitVec.ofNat 16 asid.val) σ = none := hr + rw [hr'] at hp; cases hp + +/-! ## Reachability from `vspaceForAsid` (Design 1A) -/ + +/-- Any `vspaceForAsid a σ = some (specPtr root)` under `VsRelA` comes from an `ActiveAsidRootA` entry whose untagged +root is `root`. -/ +theorem activeAsidRootA_of_vspaceForAsid {mem : U64 → U64} {ksb : U64} + (hR : VsRelA Pp Tb Lv X Y mem ksb σ) {a : Fl4SpecA64.Asid} {root : U64} + (hv : Fl4SpecA64.vspaceForAsid a σ = some (specPtr root)) : + 0 < a ∧ ∃ raw : U64, ActiveAsidRootA mem ksb Pp ⟨BitVec.ofNat 64 a.toNat⟩ raw ∧ untagA raw = root := by + have ha0 : 0 < a := by + by_contra hle + unfold Fl4SpecA64.vspaceForAsid Fl4Spec.Lookup.oassert at hv + rw [ite_eq_right_iff.mpr (fun h => absurd h hle)] at hv; cases hv + rw [vspaceForAsid_posA ha0] at hv + unfold specVsA at hv + cases htab : σ.archState.armAsidTable (Fl4SpecA64.asidHighBitsOf a) with + | none => simp [htab] at hv + | some pref => + cases hpo : Fl4SpecA64.asidPoolsOf σ pref with + | none => simp [htab, hpo] at hv + | some f => + cases hfe : f (Fl4SpecA64.asidLowBitsOf a) with + | none => simp [htab, hpo, hfe] at hv + | some ent => + simp only [htab, hpo, hfe, Option.bind, Option.some.injEq] at hv + have ha16 : a.toNat < 65536 := by + have := a.isLt; simpa [Fl4SpecA64.asidRepLen, Fl4SpecA64.asidLen] using this + have haEq : BitVec.ofNat 16 a.toNat = a := by + apply BitVec.eq_of_toNat_eq + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt ha16] + let hi : U64 := ⟨BitVec.ofNat 64 (a.toNat / 512)⟩ + let lo : U64 := ⟨BitVec.ofNat 64 (a.toNat % 512)⟩ + let asidU : U64 := ⟨BitVec.ofNat 64 a.toNat⟩ + have hiv : hi.val = a.toNat / 512 := by + show (BitVec.ofNat 64 (a.toNat / 512)).toNat = _; rw [BitVec.toNat_ofNat] + exact Nat.mod_eq_of_lt (by simp only [Nat.reducePow]; omega) + have hlov : lo.val = a.toNat % 512 := by + show (BitVec.ofNat 64 (a.toNat % 512)).toNat = _; rw [BitVec.toNat_ofNat] + exact Nat.mod_eq_of_lt (by simp only [Nat.reducePow]; omega) + have hasidv : asidU.val = a.toNat := by + show (BitVec.ofNat 64 a.toNat).toNat = _; rw [BitVec.toNat_ofNat] + exact Nat.mod_eq_of_lt (by simp only [Nat.reducePow]; omega) + have hhi : hi.val < 128 := by omega + have hlo : lo.val < 512 := by omega + have htabR := hR.table hi hhi + rw [hiv, ← asidHi_eqA ha16, haEq, htab] at htabR + have hpne : mem (atAF ksb hi) ≠ 0#u64 := by + intro hz; rw [hz, optPtrA_zero] at htabR; cases htabR + rw [optPtrA_ne hpne] at htabR + have hpref : pref = specPtr (mem (atAF ksb hi)) := Option.some.inj htabR + subst hpref + have hP : mem (atAF ksb hi) ∈ Pp := hR.tmem hi hhi hpne + have hactP : ActivePoolA mem ksb (mem (atAF ksb hi)) := ⟨hpne, hi, hhi, rfl⟩ + obtain ⟨f0, hkh0, hf0⟩ := hR.pool _ hP + rw [asidPoolsOf_eqA hkh0] at hpo + obtain rfl := Option.some.inj hpo + have hloR := hf0 hactP lo hlo + rw [hlov, ← asidLo_eqA ha16, haEq, hfe] at hloR + have hrne : mem (peAF (mem (atAF ksb hi)) lo) ≠ 0#u64 := by + intro hz; rw [hz, poolEntA_zero] at hloR; cases hloR + rw [poolEntA_ne hrne] at hloR + cases ent with + | asidPoolVSpace v => + have hvEq : v = specPtr (untagA (mem (peAF (mem (atAF ksb hi)) lo))) := by + injection hloR with h; injection h + have hun : untagA (mem (peAF (mem (atAF ksb hi)) lo)) = root := + specPtr_inj (hvEq.symm.trans hv) + exact ⟨ha0, _, ⟨hi, lo, hhi, hlo, + by change asidU.val = _; omega, _, hP, hpne, rfl, rfl, hrne⟩, hun⟩ + +/-- Stage-1 root reachability from `vspaceForAsid a σ = some (specPtr root)`. -/ +theorem vsReachA_of_vspaceForAsid {mem : U64 → U64} {ksb : U64} + (hR : VsRelA Pp Tb Lv X Y mem ksb σ) + (hX : ∀ r ∈ X, ∃ es, σ.kheap (specPtr r) = some (.archObj (.ext (.stage2Root es)))) + {a : Fl4SpecA64.Asid} {root : U64} (hr : root ∈ Tb) + (hv : Fl4SpecA64.vspaceForAsid a σ = some (specPtr root)) : + VsReachA Pp Tb Y mem ksb root := by + obtain ⟨-, raw, hact, hun⟩ := activeAsidRootA_of_vspaceForAsid hR hv + obtain ⟨pool, hP, hactP, lo, hlo, hw, hrne⟩ := hact.activePool + have hk := hR.kind pool hP hactP lo hlo + rw [hw] at hk + rcases hk with rfl | ⟨htag0, -, -⟩ | ⟨-, hx⟩ | ⟨-, hy⟩ + · exact absurd rfl hrne + · have hraw : raw = root := by rw [← hun, untagA_of htag0] + subst hraw + exact ⟨0#u64, 39, Or.inl rfl, .native hact htag0 hr⟩ + · rw [hun] at hx + obtain ⟨es, hes⟩ := hX root hx + obtain ⟨t, hpt, -⟩ := hR.pt root hr + rw [hes] at hpt; cases hpt + · rw [hun] at hy + obtain ⟨l1, hl1, -⟩ := hR.high root hy + obtain ⟨t, hpt, -⟩ := hR.pt root hr + rw [hl1] at hpt; cases hpt + +/-- `ArmHighVSpace` root reachability from `vspaceForAsid a σ = some (specPtr y)`. -/ +theorem activeHighRootA_of_vspaceForAsid {mem : U64 → U64} {ksb : U64} + (hR : VsRelA Pp Tb Lv X Y mem ksb σ) + (hX : ∀ r ∈ X, ∃ es, σ.kheap (specPtr r) = some (.archObj (.ext (.stage2Root es)))) + {a : Fl4SpecA64.Asid} {y : U64} (hy : y ∈ Y) + (hv : Fl4SpecA64.vspaceForAsid a σ = some (specPtr y)) : + ActiveHighRootA mem ksb Pp y := by + obtain ⟨-, raw, hact, hun⟩ := activeAsidRootA_of_vspaceForAsid hR hv + obtain ⟨pool, hP, hactP, lo, hlo, hw, hrne⟩ := hact.activePool + have hk := hR.kind pool hP hactP lo hlo + rw [hw] at hk + rcases hk with rfl | ⟨-, htb, -⟩ | ⟨-, hx⟩ | ⟨htag2, -⟩ + · exact absurd rfl hrne + · rw [hun] at htb + obtain ⟨t, hpt, -⟩ := hR.pt y htb + obtain ⟨l1, hl1, -⟩ := hR.high y hy + rw [hl1] at hpt; cases hpt + · rw [hun] at hx + obtain ⟨es, hes⟩ := hX y hx + obtain ⟨l1, hl1, -⟩ := hR.high y hy + rw [hl1] at hes; cases hes + · exact ⟨_, raw, hact, htag2, hun⟩ + +/-- Stage-2 root reachability from `vspaceForAsid a σ = some (specPtr r)`. -/ +theorem activeS2RootA_of_vspaceForAsid {mem : U64 → U64} {ksb : U64} + (hR : VsRelA Pp Tb Lv X Y mem ksb σ) {a : Fl4SpecA64.Asid} {r : U64} (hr : r ∈ X) + (hv : Fl4SpecA64.vspaceForAsid a σ = some (specPtr r)) : + ActiveS2RootA Pp X σ r := by + have ha0 : 0 < a := by + by_contra hle + unfold Fl4SpecA64.vspaceForAsid Fl4Spec.Lookup.oassert at hv + rw [ite_eq_right_iff.mpr (fun h => absurd h hle)] at hv; cases hv + rw [vspaceForAsid_posA ha0] at hv + unfold specVsA at hv + cases htab : σ.archState.armAsidTable (Fl4SpecA64.asidHighBitsOf a) with + | none => simp [htab] at hv + | some pref => + cases hpo : Fl4SpecA64.asidPoolsOf σ pref with + | none => simp [htab, hpo] at hv + | some f => + cases hfe : f (Fl4SpecA64.asidLowBitsOf a) with + | none => simp [htab, hpo, hfe] at hv + | some ent => + simp only [htab, hpo, hfe, Option.bind, Option.some.injEq] at hv + have ha16 : a.toNat < 65536 := by + have := a.isLt; simpa [Fl4SpecA64.asidRepLen, Fl4SpecA64.asidLen] using this + have haEq : BitVec.ofNat 16 a.toNat = a := by + apply BitVec.eq_of_toNat_eq + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt ha16] + let hi : U64 := ⟨BitVec.ofNat 64 (a.toNat / 512)⟩ + have hiv : hi.val = a.toNat / 512 := by + show (BitVec.ofNat 64 (a.toNat / 512)).toNat = _; rw [BitVec.toNat_ofNat] + exact Nat.mod_eq_of_lt (by simp only [Nat.reducePow]; omega) + have hhi : hi.val < 128 := by omega + have htabR := hR.table hi hhi + rw [hiv, ← asidHi_eqA ha16, haEq, htab] at htabR + have hpne : mem (atAF ksb hi) ≠ 0#u64 := by + intro hz; rw [hz, optPtrA_zero] at htabR; cases htabR + rw [optPtrA_ne hpne] at htabR + have hpref : pref = specPtr (mem (atAF ksb hi)) := Option.some.inj htabR + subst hpref + have hP : mem (atAF ksb hi) ∈ Pp := hR.tmem hi hhi hpne + obtain ⟨f0, hkh0, -⟩ := hR.pool _ hP + rw [asidPoolsOf_eqA hkh0] at hpo + obtain rfl := Option.some.inj hpo + cases ent with + | asidPoolVSpace v => + change v = specPtr r at hv + subst hv + exact ⟨hr, a, _, f0, ha0, hP, htab, hkh0, hfe⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/VsFlushA.lean b/proofs/Fl4ProofsA64/P3/VsFlushA.lean new file mode 100644 index 000000000..38a2c1b48 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/VsFlushA.lean @@ -0,0 +1,605 @@ +/- +P3 (aarch64, A64-P3 S3 gen 33, ruling 02:15Z item 1): **the VSpace cache-maintenance labels and the total VSpace-root +top** — FL4's `decode_vspace_flush` (`crates/fl4-core/src/arch/aarch64/vspace.rs` 923–966) against the spec's EL1 +decoder `decodeFl4VsInvFlush` (`Fl4SpecA64/Ext/VSpace` item 3, dispatched by `fl4ArchDecodeInvocation` on a +`VSRootPT_T` cap at the VSpace cache labels), with `P3/VsTopPtA`'s uniform outcome `VsTopPostA`. The x86 counterpart is +`P3/VsDecTop`'s PML4 cap-kind top (x86 has no VSpace cache labels; the shape is the frame tops'). + +* **The labels**: **`labNA_vsFlA`** (FL4's 42..45 are the VSpace cache labels, `Invalidate_Data` exactly 43), + **`vsFlush_inNA`**, **`is_inv_vsA`** (FL4's `is_invalidate` there), **`decode_vs_flushA`** (`decode_invocation` on a + `Cap::Pml4` at 42..45 is `decode_vspace_flush`). +* **The spec's decoder**: **`dec_vsFlushA`** (the dispatcher's arm), the runs of `decodeFl4VsInvFlush` — **`vsFl_truncA`**, + **`vsFl_leA`**, **`vsFl_topA`**, **`vsFl_unmA`**, **`vsFl_bodyA`** (to the tail **`vsFlTailA`** after the + `user_vtop` check and `check_vspace_root`), **`vsFlTail_errA`** / **`_neA`** / **`_okA`** (the ASID's root), the + frame tail `fl4VsFlushFrame`'s **`vsFF_noneA`** / **`_rangeA`** / **`_rightsA`** / **`_okA`**, and + **`lookupFramePte_atA`** (l4v's `lookup_frame` with the descriptor, from a found slot). +* **The descriptor**: **`rights_ofA`** (FL4's `rights_of` is read-write iff AP is 1), **`pteRights_rwA`** (the same for + `pteRightsA`), **`page_cache_op_okA`**, `pbfs_lvA`, `mask_pgA`, `lvOf_lvA`. +* **The performs**: **`doFlush_okA`**, **`performFlush_okA`**, **`perfVs_memA`** (every VSpace invocation's perform has + an outcome, `[]`, changing only the machine state). +* **The restart**: **`restart_onlyJA`** (the caller's restart with `JAtZAX`, the tree and `CarryA` after it), + **`CarryA.of_tyA`** (`CarryA` across a spec step keeping the kinds). +* **`vs_flushJA`**: `decode_vspace_flush` at 42..45 against `liftME .spec (decodeFl4VsInvFlush …)`, every path: the + front errors, the ASID's root (gen 19's `find_asid_findA` / `find_asid_tbA`), FL4's `lookup_pt_slot` against l4v's + walk (`lookup_pt_slotA`), no page (restart, `vspaceNothing`), the page-crossing `RangeError`, the EL1 rights check, and + the flush (restart, `ArchKeepsA.cacheOp`; l4v's `perform_flush`, `MsA`). +* **`decInv_vspace_jA`** (moved from `P3/VsTopRootA`, now total): the cache labels by `vs_flushJA`, every other label + `IllegalOperation` (`decode_vs_otherA` / `dec_vsOtherA`). + +Named premise: `ArchKeepsA` [P] (as the map / unmap tops). Decode-site hypotheses (standing): `RsSiteA`, the front +end's argument facts (`hlen`, `hargs`). **Changed statement**: `decInv_vspace_jA` (ruling: total) drops its selector +`hLbl` and takes `K`, `E`, `RS`, `hTr`, `hab`, `hcap`, `hlen`, `hargs` (the facts its flush arm reads), as +`decInv_pt_jA`. No new named premise. +-/ +import Fl4ProofsA64.P3.VsTopRootA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## The labels -/ + +section lbl +open Fl4SpecA64 + +set_option maxRecDepth 8000 in +/-- FL4's VSpace cache labels 42..45: l4v's VSpace flush labels, `Invalidate_Data` exactly at 43. -/ +theorem labNA_vsFlA : ∀ n, 42 ≤ n → n ≤ 45 → + isVSpaceFlushLabel (specInvA (labNA n)) = true ∧ + (labelToFlushType (specLabelWord (labNA n)) = .invalidate ↔ n = 43) := by + intro n h1 h2 + unfold labelToFlushType + rw [invType_specA] + interval_cases n <;> decide + +theorem vsFlush_inNA {n : Nat} (h : 42 ≤ n ∧ n ≤ 45) : + isVSpaceFlushLabel (invocationType (specLabelWord (labNA n))) = true := by + rw [invType_specA]; exact (labNA_vsFlA n h.1 h.2).1 + +end lbl + +section fl4lbl +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {s : S} {a : A} + +theorem is_inv_vsA {lbl : U64} (hl : 42 ≤ lbl.val ∧ lbl.val ≤ 45) : + vspace.is_invalidate lbl = ok (decide (lbl.val = 43)) := by + have v52 : fl4_abi.label.ARMPageInvalidate_Data.val = 52 := by simp [fl4_abi.label.ARMPageInvalidate_Data] + have v43 : fl4_abi.label.ARMVSpaceInvalidate_Data.val = 43 := by simp [fl4_abi.label.ARMVSpaceInvalidate_Data] + unfold vspace.is_invalidate + rw [if_neg (fun e => by rw [e, v52] at hl; omega)] + congr 1 + exact decide_eq_decide.2 ⟨fun e => by rw [e, v43], fun e => UScalar.eq_of_val_eq (by rw [e, v43])⟩ + +/-- FL4's `decode_invocation` on a `Cap::Pml4` at a VSpace cache label is `decode_vspace_flush`. -/ +theorem decode_vs_flushA (ar : machine.Arch A) (lbl length : U64) (slot : obj.Slot) (ptr asid : U64) (mp : Bool) + (block call cd fp : Bool) (buf : U64) (ex : Aeneas.Std.Array obj.Slot 3#usize) + (h : 42 ≤ lbl.val ∧ lbl.val ≤ 45) : + syscall.decode_invocation st ar s a lbl length slot (.Pml4 ptr mp asid) block call cd fp buf ex = + vspace.decode_vspace_flush st ar s a lbl length (.Pml4 ptr mp asid) buf := by + have v42 : fl4_abi.label.ARMVSpaceClean_Data.val = 42 := by simp [fl4_abi.label.ARMVSpaceClean_Data] + have v43 : fl4_abi.label.ARMVSpaceInvalidate_Data.val = 43 := by + simp [fl4_abi.label.ARMVSpaceInvalidate_Data] + have v44 : fl4_abi.label.ARMVSpaceCleanInvalidate_Data.val = 44 := by + simp [fl4_abi.label.ARMVSpaceCleanInvalidate_Data] + have v45 : fl4_abi.label.ARMVSpaceUnify_Instruction.val = 45 := by + simp [fl4_abi.label.ARMVSpaceUnify_Instruction] + have ne : ∀ {x y : U64} {i j : Nat}, x.val = i → y.val = j → i ≠ j → x ≠ y := fun hx hy hij e => by + subst e; exact hij (hx.symm.trans hy) + have hc : lbl.val = 42 ∨ lbl.val = 43 ∨ lbl.val = 44 ∨ lbl.val = 45 := by omega + unfold syscall.decode_invocation vspace.decode + rcases hc with e | e | e | e + · obtain rfl : lbl = fl4_abi.label.ARMVSpaceClean_Data := UScalar.eq_of_val_eq (by rw [e, v42]) + simp only [cur_thread_ok, bind_ok, ↓reduceIte] + · obtain rfl : lbl = fl4_abi.label.ARMVSpaceInvalidate_Data := UScalar.eq_of_val_eq (by rw [e, v43]) + simp only [cur_thread_ok, bind_ok, ↓reduceIte, ne v43 v42 (by decide)] + · obtain rfl : lbl = fl4_abi.label.ARMVSpaceCleanInvalidate_Data := UScalar.eq_of_val_eq (by rw [e, v44]) + simp only [cur_thread_ok, bind_ok, ↓reduceIte, ne v44 v42 (by decide), ne v44 v43 (by decide)] + · obtain rfl : lbl = fl4_abi.label.ARMVSpaceUnify_Instruction := UScalar.eq_of_val_eq (by rw [e, v45]) + simp only [cur_thread_ok, bind_ok, ↓reduceIte, ne v45 v42 (by decide), ne v45 v43 (by decide), + ne v45 v44 (by decide)] + +end fl4lbl + +/-! ## The spec's decoder -/ + +section spec +open Fl4Spec Fl4SpecA64 + +variable (calling : Bool) (xSlot : CapRef) (cte : CslotPtr) (exs : List (Cap × CslotPtr)) {σ : State Unit} + +/-- **The dispatcher's VSpace cache arm** (module docstring). -/ +theorem dec_vsFlushA (args : List Data) (l : Fl4Label) + (hf : isVSpaceFlushLabel (invocationType (specLabelWord l)) = true) (p : ObjRef) + (mp : Option (Asid × VSpaceRef)) : + fl4ArchDecodeInvocation (Z := Unit) calling l args xSlot cte (.pageTableCap p .vsRootPtT mp) exs = + NonDetE.liftME Fl4ArchInvocation.spec + (decodeFl4VsInvFlush (specLabelWord l) args cte (.pageTableCap p .vsRootPtT mp) exs) := by + unfold fl4ArchDecodeInvocation + dsimp only + rw [if_pos hf] + +/-- `decodeFl4VsInvFlush` after the `user_vtop` check and `check_vspace_root` (module docstring). -/ +def vsFlTailA (lab a0 a1 : Data) (pt : ObjRef) (asid : Asid) : SEMonad Unit ArchInvocation := do + let vspace' ← lookupErrorOnFailure false <| findVspaceForAsid asid + NonDetE.whenE (vspace' ≠ pt) <| NonDetE.throwError (.invalidCapability 0) + let frameInfo ← NonDetE.liftE (NonDet.gets fun s : State Unit => lookupFramePte pt a0 (ptesOf s)) + fl4VsFlushFrame lab a0 a1 pt asid frameInfo + +theorem vsFl_truncA (lab : Data) (args : List Data) (p : ObjRef) (md : Option (Asid × VSpaceRef)) + (h : ¬ args.length > 1) : + (decodeFl4VsInvFlush (Z := Unit) lab args cte (.pageTableCap p .vsRootPtT md) exs).run σ = + ({(.error .truncatedMessage, σ)}, False) := by + simp only [decodeFl4VsInvFlush, if_neg h]; rfl + +theorem vsFl_leA (lab a0 a1 : Data) (r : List Data) (p : ObjRef) (md : Option (Asid × VSpaceRef)) (h : a1 ≤ a0) : + (decodeFl4VsInvFlush (Z := Unit) lab (a0 :: a1 :: r) cte (.pageTableCap p .vsRootPtT md) exs).run σ = + ({(.error (.invalidArgument 1), σ)}, False) := by + simp only [decodeFl4VsInvFlush, List.length_cons, gt_iff_lt, Nat.lt_add_left_iff_pos, Nat.zero_lt_succ, + ↓reduceIte, List.getElem?_cons_zero, List.getElem?_cons_succ, the, NonDetE.whenE, h, NonDetE.throwError, + ndE_throw_bindA] + rfl + +theorem vsFl_topA (lab a0 a1 : Data) (r : List Data) (p : ObjRef) (md : Option (Asid × VSpaceRef)) + (h1 : ¬ a1 ≤ a0) (h2 : userVtop < a1) : + (decodeFl4VsInvFlush (Z := Unit) lab (a0 :: a1 :: r) cte (.pageTableCap p .vsRootPtT md) exs).run σ = + ({(.error .illegalOperation, σ)}, False) := by + simp only [decodeFl4VsInvFlush, List.length_cons, gt_iff_lt, Nat.lt_add_left_iff_pos, Nat.zero_lt_succ, + ↓reduceIte, List.getElem?_cons_zero, List.getElem?_cons_succ, the, NonDetE.whenE, h1, h2, ndE_pure_bindA, + NonDetE.returnOk, NonDetE.throwError, ndE_throw_bindA] + rfl + +theorem vsFl_unmA (lab a0 a1 : Data) (r : List Data) (p : ObjRef) (h1 : ¬ a1 ≤ a0) (h2 : ¬ userVtop < a1) : + (decodeFl4VsInvFlush (Z := Unit) lab (a0 :: a1 :: r) cte (.pageTableCap p .vsRootPtT none) exs).run σ = + ({(.error (.invalidCapability 0), σ)}, False) := by + simp only [decodeFl4VsInvFlush, List.length_cons, gt_iff_lt, Nat.lt_add_left_iff_pos, Nat.zero_lt_succ, + ↓reduceIte, List.getElem?_cons_zero, List.getElem?_cons_succ, the, NonDetE.whenE, h1, h2, ndE_pure_bindA, + NonDetE.returnOk, checkVspaceRoot, NonDetE.throwError, ndE_throw_bindA] + rfl + +theorem vsFl_bodyA (lab a0 a1 : Data) (r : List Data) (p : ObjRef) (asid : Asid) (x : VSpaceRef) + (h1 : ¬ a1 ≤ a0) (h2 : ¬ userVtop < a1) : + (decodeFl4VsInvFlush (Z := Unit) lab (a0 :: a1 :: r) cte (.pageTableCap p .vsRootPtT (some (asid, x))) exs).run σ = + (vsFlTailA lab a0 a1 p asid).run σ := by + simp only [decodeFl4VsInvFlush, List.length_cons, gt_iff_lt, Nat.lt_add_left_iff_pos, Nat.zero_lt_succ, + ↓reduceIte, List.getElem?_cons_zero, List.getElem?_cons_succ, the, NonDetE.whenE, h1, h2, ndE_pure_bindA, + NonDetE.returnOk, checkVspaceRoot, vsFlTailA] + try rfl + +theorem vsFlTail_errA {lab a0 a1 : Data} {pt : ObjRef} {asid : Asid} + (h : (findVspaceForAsid (Z := Unit) asid).run σ = ({(.error .invalidRoot, σ)}, False)) : + (vsFlTailA lab a0 a1 pt asid).run σ = ({(.error (.failedLookup false .invalidRoot), σ)}, False) := by + unfold vsFlTailA + exact run_bind_errorA (lefo_errA h) _ + +theorem vsFlTail_neA {lab a0 a1 : Data} {pt w : ObjRef} {asid : Asid} + (h : (lookupErrorOnFailure (Z := Unit) false (findVspaceForAsid asid)).run σ = ({(.ok w, σ)}, False)) + (hne : w ≠ pt) : + (vsFlTailA lab a0 a1 pt asid).run σ = ({(.error (.invalidCapability 0), σ)}, False) := by + unfold vsFlTailA + rw [run_bind_okA h] + simp only [NonDetE.whenE, hne, ne_eq, not_false_eq_true, ↓reduceIte, NonDetE.throwError, ndE_throw_bindA] + rfl + +theorem vsFlTail_okA {lab a0 a1 : Data} {pt : ObjRef} {asid : Asid} + (h : (lookupErrorOnFailure (Z := Unit) false (findVspaceForAsid asid)).run σ = ({(.ok pt, σ)}, False)) : + (vsFlTailA lab a0 a1 pt asid).run σ = + (fl4VsFlushFrame (Z := Unit) lab a0 a1 pt asid (lookupFramePte pt a0 (ptesOf σ))).run σ := by + unfold vsFlTailA + rw [run_bind_okA h, run_bind_okA (run_whenE_falseA (by simp) _ _)] + exact run_bind_okA (liftE_runA (m := NonDet.gets _) rfl) _ + +variable {lab st en : Data} {pt : ObjRef} {asid : Asid} + +theorem vsFF_noneA : (fl4VsFlushFrame (Z := Unit) lab st en pt asid none).run σ = + ({(.ok (.invokeVSpace .vspaceNothing), σ)}, False) := rfl + +theorem vsFF_rangeA {sz : VMPageSize} {pte : PTE} (h : pageBase st sz ≠ pageBase (en - 1) sz) : + (fl4VsFlushFrame (Z := Unit) lab st en pt asid (some (sz, pte))).run σ = + ({(.error (.rangeError st (pageBase st sz + mask (ptBitsLeft (levelOfVmsize sz)))), σ)}, False) := by + unfold fl4VsFlushFrame + dsimp only + rw [if_pos h] + rfl + +theorem vsFF_rightsA {sz : VMPageSize} {pte : PTE} (h1 : pageBase st sz = pageBase (en - 1) sz) + (h2 : labelToFlushType lab = .invalidate ∧ pte.pteRights ≠ vmReadWrite) : + (fl4VsFlushFrame (Z := Unit) lab st en pt asid (some (sz, pte))).run σ = + ({(.error .illegalOperation, σ)}, False) := by + unfold fl4VsFlushFrame + dsimp only + rw [if_neg (fun h => h h1), if_pos h2] + rfl + +theorem vsFF_okA {sz : VMPageSize} {pte : PTE} (h1 : pageBase st sz = pageBase (en - 1) sz) + (h2 : ¬ (labelToFlushType lab = .invalidate ∧ pte.pteRights ≠ vmReadWrite)) : + (fl4VsFlushFrame (Z := Unit) lab st en pt asid (some (sz, pte))).run σ = + ({(.ok (.invokeVSpace (.vspaceFlush (labelToFlushType lab) st (en - 1) + (pteBaseAddr pte + (st &&& mask (ptBitsLeft (levelOfVmsize sz)))) pt asid)), σ)}, False) := by + unfold fl4VsFlushFrame + dsimp only + rw [if_neg (fun h => h h1), if_neg h2] + rfl + +/-- **l4v's `lookup_frame` with the descriptor, from a found slot** (module docstring). -/ +theorem lookupFramePte_atA {v : VSpaceRef} {lv : VmLevel} {sl : ObjRef} {pte : PTE} + (hL : ptLookupSlot pt v (ptesOf σ) = some (lv, sl)) (hP : ptesOf σ (levelType lv) sl = some pte) : + lookupFramePte pt v (ptesOf σ) = + if pte.isPagePTE = true ∧ lv ≤ 2 then some (vmsizeOfLevel lv, pte) else none := by + unfold lookupFramePte + simp only [rbindA, hL, Option.bind, oapply2, hP] + by_cases h1 : pte.isPagePTE = true <;> by_cases h2 : lv ≤ 2 <;> + simp [h1, h2, rbindA, Lookup.oassert, Lookup.oreturn, Lookup.ofail] + +/-! ### The performs -/ + +theorem doFlush_okA (t : FlushType) (v e p : Word) : NdOkA (fun _ _ => True) (doFlush t v e p) := by + cases t + · exact machineOpLift_okA _ + · exact machineOpLift_okA _ + · exact machineOpLift_okA _ + · exact NdOkA.bind triv_trans (machineOpLift_okA _) fun _ => NdOkA.bind triv_trans (machineOpLift_okA _) fun _ => + NdOkA.bind triv_trans (machineOpLift_okA _) fun _ => NdOkA.bind triv_trans (machineOpLift_okA _) fun _ => + machineOpLift_okA _ + +/-- l4v's `perform_flush` has an outcome and changes only the machine state. -/ +theorem performFlush_okA (t : FlushType) (vs ve : VSpaceRef) (ps : Paddr) (sp : ObjRef) (asd : Asid) : + NdOkA MsA (performFlush (Z := Unit) t vs ve ps sp asd) := by + unfold performFlush + refine NdOkA.bind msA_trans (NdOkA.pure msA_refl _) fun _ => NdOkA.bind msA_trans (NdOkA.pure msA_refl _) fun _ => ?_ + unfold NonDet.when + split + · exact doMachineOp_okA (doFlush_okA _ _ _ _) + · exact NdOkA.pure msA_refl _ + +/-- **Every VSpace invocation's perform has an outcome `[]` changing only the machine state** (module docstring). -/ +theorem perfVs_memA (iv : VSpaceInvocation) (σ1 : State Unit) : + ∃ σ', (.ok ([] : List Data), σ') ∈ ((fl4ArchPerformInvocation (Z := Unit) (.spec (.invokeVSpace iv))).run σ1).1 ∧ + MsA σ1 σ' := by + have h : NdOkA MsA (performVspaceInvocation (Z := Unit) iv) := by + unfold performVspaceInvocation + cases iv with + | vspaceNothing => exact NdOkA.pure msA_refl _ + | vspaceFlush t vs ve ps sp asd => exact performFlush_okA t vs ve ps sp asd + obtain ⟨-, ⟨⟨⟨⟩, σ'⟩, hx⟩, hq⟩ := h σ1 + exact ⟨σ', mem_liftED (mem_bind_ndD hx rfl), hq _ hx⟩ + +end spec + +/-! ## The descriptor and the levels -/ + +section desc + +theorem rights_ofA (e : U64) : ∃ r, vspace.rights_of e = ok r ∧ (r = capability.VM_READ_WRITE ↔ apA e = 1) := by + have hv : ((⟨e.bv >>> (6#u64 : U64).val⟩ : U64) &&& 3#u64).val = apA e := by + rw [and3_valA] + show (e.bv >>> 6).toNat % 4 = (e.val / 64) % 4 + rw [BitVec.toNat_ushiftRight, Nat.shiftRight_eq_div_pow] + rfl + have h13 : capability.VM_READ_ONLY ≠ capability.VM_READ_WRITE := by + unfold capability.VM_READ_ONLY capability.VM_READ_WRITE; decide + have h12 : capability.VM_KERNEL_ONLY ≠ capability.VM_READ_WRITE := by + unfold capability.VM_KERNEL_ONLY capability.VM_READ_WRITE; decide + unfold vspace.rights_of vspace.D_AP_SHIFT + rw [CapRt.shr_u64_eq e 6#u64 (by decide)] + simp only [lift, bind_ok] + by_cases h1 : apA e = 1 + · rw [if_pos (UScalar.eq_of_val_eq (by rw [hv, h1]; rfl))] + exact ⟨_, rfl, iff_of_true rfl h1⟩ + · rw [if_neg (fun h => h1 (by rw [← hv, h]; rfl))] + split + · exact ⟨_, rfl, iff_of_false h13 h1⟩ + · exact ⟨_, rfl, iff_of_false h12 h1⟩ + +theorem pteRights_rwA (w : U64) : pteRightsA w = Fl4SpecA64.vmReadWrite ↔ apA w = 1 := by + unfold pteRightsA + by_cases h1 : apA w = 1 + · rw [if_pos h1]; exact iff_of_true rfl h1 + · rw [if_neg h1] + refine iff_of_false (fun h => ?_) h1 + have hw : Fl4SpecA64.Rights.allowWrite ∈ Fl4SpecA64.vmReadWrite := by simp [Fl4SpecA64.vmReadWrite] + rw [← h] at hw + split at hw <;> simp [Fl4SpecA64.vmReadOnly, Fl4SpecA64.vmKernelOnly] at hw + +theorem page_cache_op_okA (lbl : U64) : ∃ c, vspace.page_cache_op lbl = ok c := by + unfold vspace.page_cache_op + split_ifs <;> exact ⟨_, rfl⟩ + +theorem pbfs_lvA {l : Nat} (h : l ≤ 2) : Fl4SpecA64.pageBitsForSize (Fl4SpecA64.vmsizeOfLevel (lvA l)) = 12 + 9 * l := by + interval_cases l <;> decide + +theorem lvOf_lvA {l : Nat} (h : l ≤ 2) : Fl4SpecA64.levelOfVmsize (Fl4SpecA64.vmsizeOfLevel (lvA l)) = lvA l := by + interval_cases l <;> decide + +theorem mask_pgA {l : Nat} (h : l ≤ 2) : + (Fl4SpecA64.mask (12 + 9 * l) : BitVec 64) = BitVec.ofNat 64 (2 ^ (12 + 9 * l) - 1) := by + interval_cases l <;> decide + +end desc + +/-! ## The restart -/ + +section rs +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} +variable {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} {ia ie : Nat} + +/-- `CarryA` across a spec step keeping the kinds (FL4's memory unchanged). -/ +theorem CarryA.of_tyA {mem : U64 → U64} {σ' : Fl4SpecA64.State Unit} (C : CarryA Z mem σ) (h : TyKeepA σ σ') : + CarryA Z mem σ' := + ⟨C.1, C.2.of_keepA h.1 h.2 fun _ _ => CapFinEqA.refl _⟩ + +/-- **The caller's restart alone, with `JAtZAX`, the tree and `CarryA` after it** (module docstring). -/ +theorem restart_onlyJA (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) (hTr : TreeA st OS Z.G.chains Z.G.par s) : + ∃ x, σ.kheap σ.curThread = some (.tcb x) ∧ + sched.set_thread_state st s (L.curW s) sched.TS_RESTART = ok (L.wr s (stA (L.curW s)) sched.TS_RESTART) ∧ + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + JAtZAX (L := L) AR Z (L.wr s (stA (L.curW s)) sched.TS_RESTART) a (stUpdA σ σ.curThread x .restart) m ∧ + TreeA st OS Z.G.chains Z.G.par (L.wr s (stA (L.curW s)) sched.TS_RESTART) ∧ + (CarryA Z (L.mem s) σ → + CarryA Z (L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART)) (stUpdA σ σ.curThread x .restart)) := by + obtain ⟨Rp, Nt, extra, sp, P⟩ := RS.pl + obtain ⟨x, hkx, hst, hset, E1, -, hOK, -, -⟩ := E.restartA P RS.sym RS.run RS.cur RS.vs RS.vc + refine ⟨x, hkx, hst, hset, E1.j, treeA_of_mem hTr hOK, fun C => ⟨?_, ?_⟩⟩ + · exact C.1.of_capsA rfl (fun _ h => h) (fun _ h => h) fun y hy => + Or.inl (by rw [capAtA_eq, capAtA_eq]; exact hOK.capA (RS.sub y hy)) + · have hty := TyKeepA.stUpdA hkx Fl4SpecA64.ThreadState.restart + exact C.2.of_keepA hty.1 hty.2 fun y hy => by + rw [capAtA_eq, capAtA_eq, hOK.capA ((hTr.lk.members y).1 hy).1]; exact CapFinEqA.refl _ + +/-! ## FL4's `decode_vspace_flush` -/ + +set_option maxHeartbeats 4000000 in +/-- **`decode_vspace_flush` at the VSpace cache labels against the spec's EL1 decoder** (module docstring). -/ +theorem vs_flushJA {ar : machine.Arch A} (K : ArchKeepsA ar AR) + (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) + (hTr : TreeA st OS Z.G.chains Z.G.par s) (cte : Fl4SpecA64.CslotPtr) (hab : AbsSlotA Z.th cte) + (ptr asid : U64) (mp : Bool) (hcap : capAtA (L.mem s) (cslotAddrA cte) = .Pml4 ptr mp asid) + (lbl : U64) (hl : 42 ≤ lbl.val ∧ lbl.val ≤ 45) (length buf : U64) (args : List U64) + (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) + (exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) : + vspace.decode_vspace_flush st ar s a lbl length (.Pml4 ptr mp asid) buf ⦃ res => VsTopPostA (L := L) AR Z OS m σ + ((Fl4Spec.NonDetE.liftME Fl4SpecA64.Fl4ArchInvocation.spec (Fl4SpecA64.decodeFl4VsInvFlush (Z := Unit) + (Fl4SpecA64.specLabelWord (labNA lbl.val)) (args.map UScalar.bv) cte + (.pageTableCap (specPtr ptr) .vsRootPtT (mapDataA mp asid 0#u64)) exs)).run σ) s a res ⦄ := by + have hct : sched.cur_thread st s = ok (L.curW s) := cur_thread_ok s + unfold vspace.decode_vspace_flush + simp only [hct, bind_ok] + by_cases h2 : length < 2#u64 + · rw [if_pos h2] + simp only [error.truncated, bind_ok, WP.spec_ok] + have : length.val < 2 := by scalar_tac + exact ⟨rfl, rfl, .truncatedMessage, run_liftME_errA (vsFl_truncA cte exs _ _ _ _ (by simp only [List.length_map]; omega)), + Or.inl trivial⟩ + rw [if_neg h2] + have h2' : 2 ≤ args.length := by + have : ¬ length.val < 2 := fun h => h2 (by scalar_tac) + omega + obtain ⟨a0, a1, r', rfl⟩ : ∃ a0 a1 r', args = a0 :: a1 :: r' := by + match args, h2' with + | a0 :: a1 :: r', _ => exact ⟨_, _, _, rfl⟩ + simp only [List.map_cons] + rw [hargs 0#u64 a0 (by simp), bind_ok, hargs 1#u64 a1 (by simp), bind_ok] + have hle : a1 ≤ a0 ↔ a1.bv ≤ a0.bv := by rw [BitVec.le_def]; exact Iff.rfl + by_cases h3 : a1 ≤ a0 + · rw [if_pos h3] + simp only [error.invalid_arg, bind_ok, WP.spec_ok] + exact ⟨rfl, rfl, .invalidArgument 1, run_liftME_errA (vsFl_leA cte exs _ _ _ _ _ _ (hle.1 h3)), Or.inl rfl⟩ + rw [if_neg h3] + have h3' : ¬ a1.bv ≤ a0.bv := fun h => h3 (hle.2 h) + by_cases h4 : a1 > fl4_abi.consts.USER_TOP + · rw [if_pos h4] + simp only [error.illegal, bind_ok, WP.spec_ok] + exact ⟨rfl, rfl, .illegalOperation, run_liftME_errA (vsFl_topA cte exs _ _ _ _ _ _ h3' ((top_iffA a1).2 h4)), + Or.inl trivial⟩ + rw [if_neg h4] + have h4' : ¬ Fl4SpecA64.userVtop < a1.bv := fun h => h4 ((top_iffA a1).1 h) + cases mp + · simp only [Bool.false_eq_true, ↓reduceIte, error.invalid_cap, bind_ok, WP.spec_ok] + exact ⟨rfl, rfl, .invalidCapability 0, run_liftME_errA (vsFl_unmA cte exs _ _ _ _ _ h3' h4'), + Or.inr ⟨0#u64, rfl, rfl⟩⟩ + -- a mapped root cap: the ASID's root + have hwf : asid.val < 65536 := by + obtain ⟨c0, -, hrel0⟩ := StateRelA.getAbsA E.j.1.st hab + rw [hcap] at hrel0 + exact lt_of_lt_of_eq hrel0.1 (by norm_num) + rw [show mapDataA true asid 0#u64 = some (asidOfA asid, (0#u64 : U64).bv) from rfl] + have hD0 := vsFl_bodyA (σ := σ) cte exs (Fl4SpecA64.specLabelWord (labNA lbl.val)) a0.bv a1.bv (r'.map UScalar.bv) + (specPtr ptr) (asidOfA asid) (0#u64 : U64).bv h3' h4' + simp only [↓reduceIte, vspace.find_vspace_for_asid] + refine WP.spec_bind (WP.spec_and (find_asid_findA E.j.1.vs E.j.2.2.1.obj hwf) (find_asid_tbA E.j.1.vs hwf)) + fun res ⟨hf, ht⟩ => ?_ + rcases res with w | e + swap + · have he : e = .InvalidRoot := ht + subst he + simp only [WP.spec_ok] + exact ⟨rfl, rfl, .failedLookup false .invalidRoot, run_liftME_errA (hD0.trans (vsFlTail_errA (findVs_errA hf))), + Or.inl ⟨rfl, trivial⟩⟩ + obtain ⟨hv, hk⟩ := hf + have hrun := lefo_okA (findVs_runA hv hk) + by_cases hw : w = ptr + swap + · simp only [show (w != ptr) = true by simpa using hw, ↓reduceIte, error.invalid_cap, bind_ok, WP.spec_ok] + exact ⟨rfl, rfl, .invalidCapability 0, + run_liftME_errA (hD0.trans (vsFlTail_neA hrun (fun e => hw (specPtr_inj e)))), Or.inr ⟨0#u64, rfl, rfl⟩⟩ + subst hw + simp only [bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + have hT2 := hD0.trans (vsFlTail_okA (lab := Fl4SpecA64.specLabelWord (labNA lbl.val)) (a0 := a0.bv) (a1 := a1.bv) hrun) + obtain ⟨hrT, hrL⟩ := ht + have R := E.j.1.vs + refine WP.spec_bind (lookup_pt_slotA R hrT hrL (vsReachA_of_vspaceForAsid R E.j.2.2.1.obj hrT hv) a0) + fun lu ⟨p, hp, l, hl3, hsh, hlv, hsl, _, hLs, hreachP⟩ => ?_ + rw [L.read_eq, bind_ok] + have hk3 : Z.Lv p ≤ 3 := by omega + rw [is_page_shA (k := Z.Lv p) (by rw [hsh]; omega) hk3, bind_ok] + obtain ⟨tq, hkq, htq, hrq⟩ := R.pt p hp + have hidx := idxA_lt a0 lu.shift + have hpte := ptesOf_atA hkq (ptT := Fl4SpecA64.levelType (lvA l)) + (by rw [htq, levelType_lvA hl3]; congr 1; omega) (R.al_mod hp) hidx + rw [← hsl] at hpte + have hpre : PteRelA Z.S1 Z.Lv (Z.Lv p) (L.mem s lu.slot) (Fl4SpecA64.ptApply tq (idxA a0 lu.shift).bv) := by + rw [hsl]; exact hrq hreachP _ hidx + generalize Fl4SpecA64.ptApply tq (idxA a0 lu.shift).bv = pte at hpte hpre + rw [lookupFramePte_atA hLs hpte] at hT2 + obtain ⟨x, hkx, hst, hset, J1, T1, C1⟩ := restart_onlyJA E RS hTr + -- the descriptor is a page exactly when l4v's `lookup_frame` finds one + have hpg : isPageA (L.mem s lu.slot) (Z.Lv p) = true ↔ (pte.isPagePTE = true ∧ lvA l ≤ 2) := by + rw [isPageA_iff, lvA_le2 hl3] + cases pte with + | invalidPTE => + have h0 : dBitA (L.mem s lu.slot) 0 = false := hpre + simp [h0, Fl4SpecA64.PTE.isPagePTE] + | pageTablePTE _ => + obtain ⟨hk, -, hb1, -⟩ := hpre + simp only [Fl4SpecA64.PTE.isPagePTE, Bool.false_eq_true, false_and, iff_false, not_and] + intro _ h; exact absurd (h.1 hb1) (by omega) + | pagePTE _ _ _ _ => + obtain ⟨hk0, -, hb0, hb1, -⟩ := hpre + exact iff_of_true ⟨hb0, hb1⟩ ⟨rfl, by omega⟩ + by_cases hpg' : isPageA (L.mem s lu.slot) (Z.Lv p) = true + swap + · -- nothing is mapped at `start`: the restart, `vspaceNothing` + rw [if_neg hpg', hst, bind_ok] + rw [if_neg (fun h => hpg' (hpg.2 h))] at hT2 + obtain ⟨σ', hp', hms⟩ := perfVs_memA .vspaceNothing (stUpdA σ σ.curThread x .restart) + refine (WP.spec_ok _).2 ⟨_, okD_memA (run_liftME_okA (hT2.trans vsFF_noneA)), _, hset, σ', [], hp', Z, rfl, rfl, + J1.of_msA hms, T1, fun C => (C1 C).of_tyA (TyKeepA.of_kheap (by obtain ⟨ms, rfl⟩ := hms; rfl))⟩ + rw [if_pos hpg'] + obtain ⟨hpP, hl2⟩ := hpg.1 hpg' + rw [if_pos ⟨hpP, hl2⟩] at hT2 + have hl2' : l ≤ 2 := (lvA_le2 hl3).1 hl2 + cases pte with + | invalidPTE => simp [Fl4SpecA64.PTE.isPagePTE] at hpP + | pageTablePTE _ => simp [Fl4SpecA64.PTE.isPagePTE] at hpP + | pagePTE pa sm at' rt => + obtain ⟨-, -, -, -, -, -, -, hrt, -⟩ := hpre + -- the page-crossing check + have hs64 : lu.shift.val < 64 := by omega + have e63 : (lu.shift &&& 63#u64) = lu.shift := + UScalar.eq_of_val_eq (by + rw [and_maskA lu.shift 6 _ rfl]; exact Nat.mod_eq_of_lt (by simp only [Nat.reducePow]; omega)) + simp only [lift, bind_ok] + rw [e63, CapRt.shl_u64_eq 1#u64 lu.shift hs64, bind_ok] + try simp only [lift, bind_ok] + rw [ws1A lu.shift hs64] + have ha1 : 1 ≤ a1.val := by + have : ¬ a1.val ≤ a0.val := h3 + omega + obtain ⟨z, hz, hzv⟩ := spec_ex_fd (U64.sub_spec (x := a1) (y := 1#u64) (by scalar_tac)) + rw [hz, bind_ok] + try simp only [lift, bind_ok] + have hzv' : z.val = a1.val - 1 := by scalar_tac + have hz1 : a1.bv - 1 = z.bv := by + apply BitVec.eq_of_toNat_eq + have e1 : z.bv.toNat = a1.bv.toNat - 1 := hzv' + have e2 : 1 ≤ a1.bv.toNat := ha1 + rw [e1] + bv_omega + generalize hM : (⟨BitVec.ofNat 64 (2 ^ lu.shift.val - 1)⟩ : U64) = M + have hmaskM : Fl4SpecA64.mask (12 + 9 * l) = M.bv := by rw [mask_pgA hl2', ← hsh, ← hM] + have hpb : ∀ v : U64, Fl4SpecA64.pageBase v.bv (Fl4SpecA64.vmsizeOfLevel (lvA l)) = (v &&& ~~~M).bv := fun v => by + unfold Fl4SpecA64.pageBase + rw [pbfs_lvA hl2', hmaskM] + rfl + have hbl : Fl4SpecA64.mask (Fl4SpecA64.ptBitsLeft (Fl4SpecA64.levelOfVmsize (Fl4SpecA64.vmsizeOfLevel (lvA l)))) = + M.bv := by + rw [lvOf_lvA hl2', ptBitsLeft_lvA hl3, hmaskM] + by_cases hr : (a0 &&& ~~~M) = (z &&& ~~~M) + swap + · rw [if_pos (bne_iff_ne.2 hr)] + simp only [error.range, bind_ok, WP.spec_ok] + have hne : Fl4SpecA64.pageBase a0.bv (Fl4SpecA64.vmsizeOfLevel (lvA l)) ≠ + Fl4SpecA64.pageBase (a1.bv - 1) (Fl4SpecA64.vmsizeOfLevel (lvA l)) := by + rw [hz1, hpb a0, hpb z]; exact fun e => hr ((UScalar.eq_equiv_bv_eq _ _).2 e) + refine ⟨rfl, rfl, _, run_liftME_errA (hT2.trans (vsFF_rangeA hne)), Or.inl ⟨rfl, ?_⟩⟩ + rw [hpb a0, hbl] + apply BitVec.eq_of_toNat_eq + rw [BitVec.toNat_add] + exact (wa_valm _ _).symm + rw [if_neg (show ¬ ((a0 &&& ~~~M) != (z &&& ~~~M)) = true by rw [hr, bne_self_eq_false]; exact Bool.false_ne_true)] + have heq : Fl4SpecA64.pageBase a0.bv (Fl4SpecA64.vmsizeOfLevel (lvA l)) = + Fl4SpecA64.pageBase (a1.bv - 1) (Fl4SpecA64.vmsizeOfLevel (lvA l)) := by + rw [hz1, hpb a0, hpb z, hr] + -- the EL1 rights check + have hinvI := (labNA_vsFlA lbl.val hl.1 hl.2).2 + have hrw : (Fl4SpecA64.PTE.pagePTE pa sm at' rt).pteRights = Fl4SpecA64.vmReadWrite ↔ + apA (L.mem s lu.slot) = 1 := by + show rt = _ ↔ _; rw [hrt]; exact pteRights_rwA _ + rw [is_inv_vsA hl, bind_ok] + obtain ⟨c, hc⟩ := page_cache_op_okA lbl + have hn1 : lbl.val = 43 → apA (L.mem s lu.slot) = 1 → + ¬ (Fl4SpecA64.labelToFlushType (Fl4SpecA64.specLabelWord (labNA lbl.val)) = .invalidate ∧ + (Fl4SpecA64.PTE.pagePTE pa sm at' rt).pteRights ≠ Fl4SpecA64.vmReadWrite) := + fun _ h1 h => h.2 (hrw.2 h1) + have hn2 : ¬ lbl.val = 43 → + ¬ (Fl4SpecA64.labelToFlushType (Fl4SpecA64.specLabelWord (labNA lbl.val)) = .invalidate ∧ + (Fl4SpecA64.PTE.pagePTE pa sm at' rt).pteRights ≠ Fl4SpecA64.vmReadWrite) := + fun hi h => hi (hinvI.1 h.1) + obtain ⟨d, hd, -⟩ := spec_ex_fd (U64.sub_spec (x := a1) (y := a0) (by scalar_tac)) + have hpbs : vspace.page_base (L.mem s lu.slot) lu.shift = ok ⟨pageBaseA (L.mem s lu.slot) (Z.Lv p)⟩ := + page_base_shA (by rw [hsh]; unfold shiftA; omega) + obtain ⟨σ', hp', hms⟩ := perfVs_memA (.vspaceFlush (Fl4SpecA64.labelToFlushType + (Fl4SpecA64.specLabelWord (labNA lbl.val))) a0.bv (a1.bv - 1) + (Fl4SpecA64.pteBaseAddr (.pagePTE pa sm at' rt) + (a0.bv &&& Fl4SpecA64.mask (Fl4SpecA64.ptBitsLeft + (Fl4SpecA64.levelOfVmsize (Fl4SpecA64.vmsizeOfLevel (lvA l)))))) (specPtr w) (asidOfA asid)) + (stUpdA σ σ.curThread x .restart) + have hTC : CarryA Z (L.mem s) σ → CarryA Z (L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART)) σ' := + fun C => (C1 C).of_tyA (TyKeepA.of_kheap (by obtain ⟨ms, rfl⟩ := hms; rfl)) + by_cases hinv : lbl.val = 43 + · rw [if_pos (decide_eq_true hinv)] + obtain ⟨r, hr', hriff⟩ := rights_ofA (L.mem s lu.slot) + rw [hr', bind_ok] + by_cases hrr : r = capability.VM_READ_WRITE + · rw [if_neg (show ¬ ((r != capability.VM_READ_WRITE) = true) by rw [hrr, bne_self_eq_false]; exact Bool.false_ne_true), hst, bind_ok, hpbs, bind_ok, hc, bind_ok, hd, bind_ok] + exact WP.spec_bind (K.cacheOp a Z.hw _ _ _ _ _ _ J1.1.ar) fun a' ha' => (WP.spec_ok _).2 + ⟨_, okD_memA (run_liftME_okA (hT2.trans (vsFF_okA heq (hn1 hinv (hriff.1 hrr))))), _, hset, σ', [], hp', Z, rfl, rfl, + (J1.of_arA ha').of_msA hms, T1, hTC⟩ + · rw [if_pos (bne_iff_ne.2 hrr)] + simp only [error.illegal, bind_ok, WP.spec_ok] + exact ⟨rfl, rfl, .illegalOperation, + run_liftME_errA (hT2.trans (vsFF_rightsA heq ⟨hinvI.2 hinv, fun h => hrr (hriff.2 (hrw.1 h))⟩)), + Or.inl trivial⟩ + · rw [if_neg (by simpa using hinv), hst, bind_ok, hpbs, bind_ok, hc, bind_ok, hd, bind_ok] + exact WP.spec_bind (K.cacheOp a Z.hw _ _ _ _ _ _ J1.1.ar) fun a' ha' => (WP.spec_ok _).2 + ⟨_, okD_memA (run_liftME_okA (hT2.trans (vsFF_okA heq (hn2 hinv)))), _, hset, σ', [], hp', Z, rfl, rfl, + (J1.of_arA ha').of_msA hms, T1, hTC⟩ + +/-! ## The top -/ + +set_option maxHeartbeats 1000000 in +/-- **The VSpace-root cap-kind top, total** (module docstring; ruling 02:15Z item 1): `decode_invocation` on a +`Cap::Pml4` against `fl4ArchDecodeInvocation` at FL4's label, with `VsTopPostA`, every label. -/ +theorem decInv_vspace_jA {ar : machine.Arch A} (K : ArchKeepsA ar AR) + (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) + (hTr : TreeA st OS Z.G.chains Z.G.par s) (cte : Fl4SpecA64.CslotPtr) (hab : AbsSlotA Z.th cte) + (ptr asid : U64) (mp : Bool) (hcap : capAtA (L.mem s) (cslotAddrA cte) = .Pml4 ptr mp asid) + (lbl length buf : U64) (block call cd fp calling : Bool) (ex : Aeneas.Std.Array obj.Slot 3#usize) + (args : List U64) (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) + (xSlot : Fl4SpecA64.CapRef) (exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) : + syscall.decode_invocation st ar s a lbl length (cslotAddrA cte) (.Pml4 ptr mp asid) block call cd fp buf ex + ⦃ res => VsTopPostA (L := L) AR Z OS m σ ((Fl4SpecA64.fl4ArchDecodeInvocation (Z := Unit) calling + (Fl4SpecA64.fl4LabelOf lbl.bv) (args.map UScalar.bv) xSlot cte + (.pageTableCap (specPtr ptr) .vsRootPtT (mapDataA mp asid 0#u64)) exs).run σ) s a res ⦄ := by + by_cases hfl : 42 ≤ lbl.val ∧ lbl.val ≤ 45 + · rw [decode_vs_flushA ar lbl length _ ptr asid mp block call cd fp buf ex hfl, fl4LabelOf_bvA, + dec_vsFlushA calling xSlot cte exs _ _ (vsFlush_inNA hfl)] + exact vs_flushJA K E RS hTr cte hab ptr asid mp hcap lbl hfl length buf args hlen hargs exs + have hLbl : lbl.val < 42 ∨ 45 < lbl.val := by omega + rw [decode_vs_otherA ar lbl length _ ptr asid mp block call cd fp buf ex hLbl, + dec_vsOtherA calling _ xSlot cte exs _ (by rw [fl4LabelOf_bvA]; exact vsFlush_ofNA hLbl)] + exact (WP.spec_ok _).2 ⟨rfl, rfl, .illegalOperation, rfl, Or.inl trivial⟩ + +end rs + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/VsInvA.lean b/proofs/Fl4ProofsA64/P3/VsInvA.lean new file mode 100644 index 000000000..542b2d450 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/VsInvA.lean @@ -0,0 +1,117 @@ +/- +P3 (aarch64, A64-P3 VS gen 1, gen 2): **the spec-side VSpace well-formedness invariant `SpecVsA`** — the AArch64 analogue +of the VSpace clauses x86 keeps beside `SInvX` (l4v `valid_asid_table`, `valid_vspace_objs`' pool and table +parts), pure spec, and its preservation by spec steps that do not touch the VSpace objects. + +| field | l4v (`ArchInvariants_AI`, AARCH64) | statement | +|---|---|---| +| `tab` | `valid_asid_table` (`ran asid_table ⊆ dom asid_pools`) | every ASID-table entry names an `ASIDPool` | +| `tabInj` | `valid_asid_table` (`inj_on`) | no pool is named by two ASID-table entries | +| `asid0` | (ruling Q5; l4v `vspace_for_asid` asserts `0 < asid`) | ASID 0's pool entry is empty | +| `pool` | `valid_vspace_objs` (`ASIDPool` case), FL4 roots | a pool entry names a `VSRootPT_T` table or an FL4 root (`Ext/Stage2` `rootKindAt`) | +| `pte` | `valid_vspace_objs` (`PageTable` case) | a `PageTablePTE` names a `NormalPT_T` table | +| `hpte` | (FL4, `Ext/ArmHighVSpace`) | the same for a high window's level-1 entries | + +**The pointer of a `PageTablePTE` is l4v's** (gen 2, ruling Q1): `ptrFromPAddr (paddr_from_ppn ppn)`, l4v's +`pptr_from_pte`. Gen 1 took it as a parameter `pp`; with the additive shift `specPtr` (`PtrA`) the relation's tables +sit at `specPtr p` and FL4's `table_of` is `ptrFromPAddr` there (`ptrFromPAddr_wa`), so the parameter is gone. +**`asid0`** (gen 2, ruling Q5): the pool of ASID-table entry 0 has no entry 0 (ASID 0 names nothing, so +`vspace_for_asid`'s `0 < asid` assertion and the pool agree). + +* **`SpecVsSameA σ σ'`**: the ASID table kept and every VSpace object (`ASIDPool`, `PageTable`, FL4 root) the same + at every pointer both ways; **`SpecVsA.of_same`** (the spec-side frame); **`SpecVsSameA.ofSlot`**: a `SpecSlotA` + step (CNode contents only, `FrameA`) with the ASID table kept is one. + +No named premise. No `Fl4SpecA64` edit. +-/ +import Fl4ProofsA64.P3.VsTieA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 + +noncomputable section + +/-- A VSpace root as an ASID pool entry may name it (module docstring): a `VSRootPT_T` page table or an FL4 root. -/ +def VsRootObjA (σ : Fl4SpecA64.State Unit) (r : Fl4SpecA64.ObjRef) : Prop := + (∃ t, σ.kheap r = some (.archObj (.pageTable t)) ∧ Fl4SpecA64.ptType t = .vsRootPtT) ∨ + ∃ o, σ.kheap r = some (.archObj (.ext o)) + +/-- **The spec's VSpace well-formedness** (module docstring). -/ +structure SpecVsA (σ : Fl4SpecA64.State Unit) : Prop where + tab : ∀ hi p, σ.archState.armAsidTable hi = some p → ∃ f, σ.kheap p = some (.archObj (.asidPool f)) + tabInj : ∀ hi hi' p, σ.archState.armAsidTable hi = some p → σ.archState.armAsidTable hi' = some p → hi = hi' + asid0 : ∀ p f, σ.archState.armAsidTable 0 = some p → σ.kheap p = some (.archObj (.asidPool f)) → f 0 = none + pool : ∀ p f lo e, σ.kheap p = some (.archObj (.asidPool f)) → f lo = some e → VsRootObjA σ e.apVspace + pte : ∀ p t i ppn, σ.kheap p = some (.archObj (.pageTable t)) → Fl4SpecA64.ptApply t i = .pageTablePTE ppn → + ∃ t', σ.kheap (Fl4SpecA64.ptrFromPAddr (Fl4SpecA64.paddrFromPpn ppn)) = some (.archObj (.pageTable t')) ∧ + Fl4SpecA64.ptType t' = .normalPtT + hpte : ∀ p l1 i ppn, σ.kheap p = some (.archObj (.ext (.highVSpace l1))) → l1 i = .pageTablePTE ppn → + ∃ t', σ.kheap (Fl4SpecA64.ptrFromPAddr (Fl4SpecA64.paddrFromPpn ppn)) = some (.archObj (.pageTable t')) ∧ + Fl4SpecA64.ptType t' = .normalPtT + +/-- A VSpace object (module docstring). -/ +def VsObjA (ko : Fl4SpecA64.KernelObject) : Prop := + (∃ f, ko = .archObj (.asidPool f)) ∨ (∃ t, ko = .archObj (.pageTable t)) ∨ ∃ o, ko = .archObj (.ext o) + +/-- **A spec step that keeps the VSpace** (module docstring). -/ +structure SpecVsSameA (σ σ' : Fl4SpecA64.State Unit) : Prop where + tab : σ'.archState.armAsidTable = σ.archState.armAsidTable + kh : ∀ p ko, VsObjA ko → (σ'.kheap p = some ko ↔ σ.kheap p = some ko) + +theorem VsRootObjA.of_same {σ σ' : Fl4SpecA64.State Unit} (H : SpecVsSameA σ σ') {r : Fl4SpecA64.ObjRef} + (h : VsRootObjA σ r) : VsRootObjA σ' r := by + rcases h with ⟨t, hk, ht⟩ | ⟨o, hk⟩ + · exact Or.inl ⟨t, (H.kh _ _ (Or.inr (Or.inl ⟨t, rfl⟩))).2 hk, ht⟩ + · exact Or.inr ⟨o, (H.kh _ _ (Or.inr (Or.inr ⟨o, rfl⟩))).2 hk⟩ + +/-- **The spec-side frame of `SpecVsA`** (module docstring). -/ +theorem SpecVsA.of_same {σ σ' : Fl4SpecA64.State Unit} + (I : SpecVsA σ) (H : SpecVsSameA σ σ') : SpecVsA σ' where + tab hi p h := by + rw [H.tab] at h + obtain ⟨f, hk⟩ := I.tab hi p h + exact ⟨f, (H.kh _ _ (Or.inl ⟨f, rfl⟩)).2 hk⟩ + tabInj hi hi' p h h' := by rw [H.tab] at h h'; exact I.tabInj hi hi' p h h' + asid0 p f h hk := by + rw [H.tab] at h + exact I.asid0 p f h ((H.kh _ _ (Or.inl ⟨f, rfl⟩)).1 hk) + pool p f lo e hk he := + (I.pool p f lo e ((H.kh _ _ (Or.inl ⟨f, rfl⟩)).1 hk) he).of_same H + pte p t i ppn hk he := by + obtain ⟨t', hk', ht'⟩ := I.pte p t i ppn ((H.kh _ _ (Or.inr (Or.inl ⟨t, rfl⟩))).1 hk) he + exact ⟨t', (H.kh _ _ (Or.inr (Or.inl ⟨t', rfl⟩))).2 hk', ht'⟩ + hpte p l1 i ppn hk he := by + obtain ⟨t', hk', ht'⟩ := I.hpte p l1 i ppn ((H.kh _ _ (Or.inr (Or.inr ⟨_, rfl⟩))).1 hk) he + exact ⟨t', (H.kh _ _ (Or.inr (Or.inl ⟨t', rfl⟩))).2 hk', ht'⟩ + +theorem SpecVsSameA.refl (σ : Fl4SpecA64.State Unit) : SpecVsSameA σ σ := ⟨rfl, fun _ _ _ => Iff.rfl⟩ + +theorem SpecVsSameA.trans {σ₁ σ₂ σ₃ : Fl4SpecA64.State Unit} (h₁ : SpecVsSameA σ₁ σ₂) (h₂ : SpecVsSameA σ₂ σ₃) : + SpecVsSameA σ₁ σ₃ := ⟨h₂.tab.trans h₁.tab, fun p ko h => (h₂.kh p ko h).trans (h₁.kh p ko h)⟩ + +/-- No VSpace object is a CNode. -/ +theorem vsObjA_not_cnode {ko : Fl4SpecA64.KernelObject} (h : VsObjA ko) {r : Nat} + {cs : Fl4SpecA64.CnodeContents} : ko ≠ .cnode r cs := by + rcases h with ⟨f, rfl⟩ | ⟨t, rfl⟩ | ⟨o, rfl⟩ <;> exact fun e => by cases e + +/-- No VSpace object is a TCB. -/ +theorem vsObjA_not_tcb {ko : Fl4SpecA64.KernelObject} (h : VsObjA ko) {x : Fl4SpecA64.Tcb} : ko ≠ .tcb x := by + rcases h with ⟨f, rfl⟩ | ⟨t, rfl⟩ | ⟨o, rfl⟩ <;> exact fun e => by cases e + +/-- **A slot-only spec step keeps the VSpace** (module docstring): `SpecSlotA` changes only CNodes. -/ +theorem SpecVsSameA.ofSlot {σ σ' : Fl4SpecA64.State Unit} (HS : SpecSlotA σ σ') + (ht : σ'.archState.armAsidTable = σ.archState.armAsidTable) : SpecVsSameA σ σ' where + tab := ht + kh p ko hv := by + rcases HS.kh p with e | ⟨r, cs, cs', h1, h2⟩ | ⟨x, x', h1, h2, -⟩ + · rw [e] + · rw [h1, h2] + exact ⟨fun e => absurd (Option.some.inj e).symm (vsObjA_not_cnode hv), + fun e => absurd (Option.some.inj e).symm (vsObjA_not_cnode hv)⟩ + · rw [h1, h2] + exact ⟨fun e => absurd (Option.some.inj e).symm (vsObjA_not_tcb hv), + fun e => absurd (Option.some.inj e).symm (vsObjA_not_tcb hv)⟩ + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/VsLeafA.lean b/proofs/Fl4ProofsA64/P3/VsLeafA.lean new file mode 100644 index 000000000..7e9a5391c --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/VsLeafA.lean @@ -0,0 +1,396 @@ +/- +P3 (aarch64, A64-P3 VS gen 3, gen 4): **Stage-1 leaf helpers on `VsRelA`** — l4v's `store_pte` (`ArchVSpaceAcc_A`, ported +as `storePte` in `Fl4SpecA64/KHeap`) against FL4's descriptor write, and l4v's `lookup_frame` (`ArchDecode_A`, +`lookupFrame` in `Fl4SpecA64/Decode`) against the descriptor at the slot FL4's `lookup_pt_slot` finds +(`VsWalkA`). The twins of x86's `store_pte` / `lookup_frame` steps of `P3/VsPt`. + +* **Table updates**: `ptUpd_typeA` (l4v's `pt_upd` keeps the table type), **`ptApply_updA`** (`pt_apply` after + `pt_upd` at in-range indices); +* **`storePte_runA`**: at the shifted slot `i` of a related page-aligned table `p` of type `ptT`, l4v's `store_pte` + succeeds deterministically (its two asserts hold) and leaves `ptSetA σ (specPtr p) (pt_upd t i pte)`: the table + updated, nothing else; +* **`VsRelA.of_storePteA`** (the frame of the store, both sides): `VsRelA` holds again after FL4's memory changes + the slot's word to `w` and l4v stores `pte`, when `w` and `pte` are related at the table's depth (`PteRelA`) and + the slot is no ASID-table word (gen 4: `hA`, `¬ AsW ksb slot`; `VsRelA.slot_offA` shows it is no pool or window + word from the fields `pal` / `yal` / `ytb`, page alignment and the object kinds). Other tables' words differ from the slot by page alignment + (`VsRelA.al`, `peAF_injA`); the pools, other tables and windows are other spec objects because their kinds differ + (`ASIDPool` / `HighVSpace` vs `PageTable`) or by `specPtr`'s injectivity; +* **`write_storePteA`**: FL4's `Store::write` of the slot (A64 P1's `LawfulStore.mem_wr`) against l4v's `store_pte + (level_type …)`, and **`write_clearPteA`**, the zero write of `unmap_page` / `unmap_page_table` against + `store_pte … InvalidPTE`; +* **`lookupFrameA`**: from the slot `lookup_pt_slotA` returns, l4v's `lookup_frame` of the shifted root is `Some + (vmsize_of_level l, page_base)` exactly when FL4's descriptor there is a page or block descriptor (valid, bit 1 + iff the last level, below the root), else `None`. + +**Gen 4 (ruling 14:37Z)**: the gen 3 hypotheses `hD` (slot off the ASID-table / pool / window words) and `hal` +(footprint alignment) are narrowed to the ASID-table / kernel-state boundary: `hA : ¬ AsW ksb slot` and `hK : +Aligned ksb` (`VsRelA.foot_alignedA`: the footprint is aligned with no bound on the objects, `4096 ∣ 2^64`; +`waWin_valA`: a window's level-1 table `y + 4096` does not wrap). + +Hypotheses (not premises): `hA` (A64 P1's `HeapOK`: tables are off the kernel-state block), `hK` (the kernel-state +base is aligned). No named premise. No `Fl4SpecA64` edit. +-/ +import Fl4ProofsA64.P3.VsWalkA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 + +noncomputable section + +/-! ## Table updates -/ + +theorem ptUpd_typeA (t : Fl4SpecA64.PT) (x : Fl4SpecA64.Word) (pte : Fl4SpecA64.PTE) : + Fl4SpecA64.ptType (Fl4SpecA64.ptUpd t x pte) = Fl4SpecA64.ptType t := by + cases t <;> rfl + +theorem setWidth_injA {n : Nat} (hn : 9 ≤ n) {i j : U64} (hi : i.val < 512) (hj : j.val < 512) + (h : i.bv.setWidth n = j.bv.setWidth n) : i = j := by + have h2 := congrArg BitVec.toNat h + rw [BitVec.toNat_setWidth, BitVec.toNat_setWidth] at h2 + have hp : 512 ≤ 2 ^ n := le_trans (by decide : 512 ≤ 2 ^ 9) (Nat.pow_le_pow_right (by decide) hn) + have ei : i.bv.toNat = i.val := rfl + have ej : j.bv.toNat = j.val := rfl + rw [ei, ej, Nat.mod_eq_of_lt (by omega), Nat.mod_eq_of_lt (by omega)] at h2 + exact UScalar.eq_of_val_eq h2 + +theorem vsIndexLen_geA : 9 ≤ Fl4SpecA64.vsIndexLen := by + show 9 ≤ Fl4SpecA64.vsIndexBits + unfold Fl4SpecA64.vsIndexBits + split <;> omega + +/-- **`pt_apply` after `pt_upd`** (module docstring). -/ +theorem ptApply_updA (t : Fl4SpecA64.PT) {i j : U64} (hi : i.val < 512) (hj : j.val < 512) + (pte : Fl4SpecA64.PTE) : + Fl4SpecA64.ptApply (Fl4SpecA64.ptUpd t i.bv pte) j.bv = if j = i then pte else Fl4SpecA64.ptApply t j.bv := by + by_cases h : j = i + · subst h + rw [if_pos rfl] + cases t <;> simp [Fl4SpecA64.ptUpd, Fl4SpecA64.ptApply] + · rw [if_neg h] + cases t with + | vsRootPT vs => exact Function.update_of_ne (fun e => h (setWidth_injA vsIndexLen_geA hj hi e)) _ _ + | normalPT npt => exact Function.update_of_ne (fun e => h (setWidth_injA (n := 9) le_rfl hj hi e)) _ _ + +/-! ## l4v's `store_pte` -/ + +section nd +open Fl4Spec +variable {σ α β : Type} + +theorem lfA_assert {P : Prop} (h : P) (s : σ) : NonDet.assert P s = ({((), s)}, False) := by + show ({x | P ∧ x = ((), s)}, ¬P) = _ + refine Prod.ext ?_ (propext ⟨fun hn => absurd h hn, False.elim⟩) + ext x + simp [h] + +theorem lfA_pure_bind (a : α) (f : α → NonDet σ β) (s : σ) : (pure a >>= f) s = f a s := + lfA_bind_one (m := pure a) (s := s) (s' := s) (a := a) rfl f + +end nd + +/-- **The spec state after `set_pt q t'`** (module docstring). -/ +def ptSetA (σ : Fl4SpecA64.State Unit) (q : Fl4SpecA64.ObjRef) (t' : Fl4SpecA64.PT) : Fl4SpecA64.State Unit := + { σ with kheap := Function.update σ.kheap q (some (.archObj (.pageTable t'))) } + +theorem ptSetA_kheap (σ : Fl4SpecA64.State Unit) (q : Fl4SpecA64.ObjRef) (t' : Fl4SpecA64.PT) + (r : Fl4SpecA64.ObjRef) : + (ptSetA σ q t').kheap r = if r = q then some (.archObj (.pageTable t')) else σ.kheap r := by + unfold ptSetA + simp only [Function.update_apply] + +theorem getPt_runA {σ : Fl4SpecA64.State Unit} {q : Fl4SpecA64.ObjRef} {t : Fl4SpecA64.PT} + (hk : σ.kheap q = some (.archObj (.pageTable t))) : Fl4SpecA64.getPt (Z := Unit) q σ = ({(t, σ)}, False) := by + show (Fl4Spec.NonDet.gets (Fl4SpecA64.ptsOf (Z := Unit)) >>= fun m => Fl4Spec.NonDet.assertOpt (m q)) σ = _ + rw [lfA_bind_one (s' := σ) (a := Fl4SpecA64.ptsOf σ) rfl] + show Fl4Spec.NonDet.assertOpt (Fl4SpecA64.ptsOf σ q) σ = _ + rw [ptsOf_eqA hk] + rfl + +theorem getObject_runA {σ : Fl4SpecA64.State Unit} {q : Fl4SpecA64.ObjRef} {o : Fl4SpecA64.KernelObject} + (hk : σ.kheap q = some o) : Fl4SpecA64.getObject (Z := Unit) q σ = ({(o, σ)}, False) := by + show Fl4Spec.NonDet.assertOpt (σ.kheap q) σ = _ + rw [hk] + rfl + +/-- **l4v's `store_pte` at a related slot** (module docstring). -/ +theorem storePte_runA {σ : Fl4SpecA64.State Unit} {p i : U64} {t : Fl4SpecA64.PT} {ptT : Fl4SpecA64.PtType} + (hk : σ.kheap (specPtr p) = some (.archObj (.pageTable t))) (ht : Fl4SpecA64.ptType t = ptT) + (hp : p.val % 4096 = 0) (hi : i.val < 512) (pte : Fl4SpecA64.PTE) : + Fl4SpecA64.storePte (Z := Unit) ptT (specPtr (peAF p i)) pte σ = + ({((), ptSetA σ (specPtr p) (Fl4SpecA64.ptUpd t i.bv pte))}, False) := by + obtain ⟨hal, hb, hx⟩ := slot_geomA ptT hp hi + have hty : Fl4SpecA64.aType (.archObj (.pageTable t)) = + Fl4SpecA64.aType (.archObj (.pageTable (Fl4SpecA64.ptUpd t i.bv pte))) := by + show Fl4SpecA64.AType.aArch (.aPageTable (Fl4SpecA64.ptType t)) = + .aArch (.aPageTable (Fl4SpecA64.ptType (Fl4SpecA64.ptUpd t i.bv pte))) + rw [ptUpd_typeA] + unfold Fl4SpecA64.storePte + rw [lfA_bind_one (lfA_assert hal σ)] + try beta_reduce + rw [lfA_pure_bind] + try beta_reduce + rw [hb, hx, lfA_bind_one (getPt_runA hk)] + try beta_reduce + rw [lfA_bind_one (lfA_assert ht σ)] + try beta_reduce + unfold Fl4SpecA64.setPt + rw [lfA_bind_one (getPt_runA hk)] + try beta_reduce + unfold Fl4SpecA64.setObject + rw [lfA_bind_one (getObject_runA hk)] + try beta_reduce + rw [lfA_bind_one (lfA_assert hty σ)] + try beta_reduce + rw [lfA_bind_one (show (Fl4Spec.NonDet.get : Fl4Spec.NonDet (Fl4SpecA64.State Unit) _) σ = ({(σ, σ)}, False) + from rfl)] + rfl + +/-! ## The store's frame -/ + +/-- Words of page-aligned tables are distinct unless table and index are. -/ +theorem peAF_injA {p q i j : U64} (hp : p.val % 4096 = 0) (hq : q.val % 4096 = 0) (hi : i.val < 512) + (hj : j.val < 512) (h : peAF p i = peAF q j) : p = q ∧ i = j := by + have bp : p.val < 2 ^ 64 := p.bv.isLt + have bq : q.val < 2 ^ 64 := q.bv.isLt + have e := congrArg UScalar.val h + rw [peAF_valA (by omega) hi, peAF_valA (by omega) hj] at e + exact ⟨UScalar.eq_of_val_eq (by omega), UScalar.eq_of_val_eq (by omega)⟩ + +section frame +variable {Pp Tb : Set U64} {Lv : U64 → Nat} {X Y : Set U64} {ksb : U64} {σ : Fl4SpecA64.State Unit} + +/-- An ASID-table word is an `AsW` word of A64 P1's `VsR` vocabulary. -/ +theorem atAF_asWA {kb hi : U64} (hh : hi.val < 128) : AsW kb (atAF kb hi) := by + refine ⟨core.num.U64.wrapping_add 772352#u64 (core.num.U64.wrapping_mul hi 8#u64), ?_, ?_, ?_, rfl⟩ <;> + simp [core.num.U64.wrapping_add_val_eq, core.num.U64.wrapping_mul_val_eq, U64.size_def, U64.numBits] <;> + omega + +/-- **A table's word is no other footprint word** (gen 4) unless it is an ASID-table word: a pool word would make +the pool and the table one object (page alignment, `peAF_injA`) of two kinds; a window word would put the window's +level-1 table in `Tb` (`VsRelA.ytb`). -/ +theorem VsRelA.slot_offA {mem : U64 → U64} (R : VsRelA Pp Tb Lv X Y mem ksb σ) {p i : U64} (hp : p ∈ Tb) + (hi : i.val < 512) (hA : ¬ AsW ksb (peAF p i)) : ∀ b, VsFootA Pp ∅ Y ksb b → b ≠ peAF p i := by + intro b h e + cases h with + | asid hi' hh => exact hA (e ▸ atAF_asWA hh) + | pool q lo hq hl => + have hqp := (peAF_injA (Nat.mod_eq_zero_of_dvd (R.pal q hq)) (R.al_mod hp) hl hi e).1 + obtain ⟨f, hkq, -⟩ := R.pool q hq + obtain ⟨t, hkp, -⟩ := R.pt p hp + rw [hqp, hkp] at hkq + cases hkq + | pt q j hq hj => simp at hq + | high y j hy hj => + have hyp := (peAF_injA (waWin_modA (R.yal y hy)) (R.al_mod hp) hj hi e).1 + exact R.ytb y hy (hyp ▸ hp) + +/-- **`VsRelA` across a descriptor store** (module docstring). -/ +theorem VsRelA.of_storePteA {mem mem' : U64 → U64} (R : VsRelA Pp Tb Lv X Y mem ksb σ) {p i w : U64} + {t : Fl4SpecA64.PT} {pte : Fl4SpecA64.PTE} (hp : p ∈ Tb) (hi : i.val < 512) + (hk : σ.kheap (specPtr p) = some (.archObj (.pageTable t))) (hw : PteRelA Tb Lv (Lv p) w pte) + (hA : ¬ AsW ksb (peAF p i)) + (hm : ∀ b, VsFootA Pp Tb Y ksb b → b ≠ peAF p i → mem' b = mem b) (hm' : mem' (peAF p i) = w) + (hch : ∀ ppn, pte = .pageTablePTE ppn → dTabA w ≠ p ∧ + ∀ j : U64, j.val < 512 → dBitA (mem (peAF (dTabA w) j)) 0 = false ∧ + ∀ t0, σ.kheap (specPtr (dTabA w)) = some (.archObj (.pageTable t0)) → + Fl4SpecA64.ptApply t0 j.bv = .invalidPTE) : + VsRelA Pp Tb Lv X Y mem' ksb (ptSetA σ (specPtr p) (Fl4SpecA64.ptUpd t i.bv pte)) := by + have hActP : ∀ {q}, ActivePoolA mem' ksb q → ActivePoolA mem ksb q := by + rintro q ⟨hqne, hi', hh, htab⟩ + rw [hm _ (.asid hi' hh) (R.slot_offA hp hi hA _ (.asid hi' hh))] at htab + exact ⟨hqne, hi', hh, htab⟩ + have hActR : ∀ {asid raw}, ActiveAsidRootA mem' ksb Pp asid raw → ActiveAsidRootA mem ksb Pp asid raw := by + rintro asid raw ⟨hi', lo, hh, hl, ha', pool, hpool, hpne, htab, hword, hrne⟩ + rw [hm _ (.asid hi' hh) (R.slot_offA hp hi hA _ (.asid hi' hh))] at htab + rw [hm _ (.pool pool lo hpool hl) (R.slot_offA hp hi hA _ (.pool pool lo hpool hl))] at hword + exact ⟨hi', lo, hh, hl, ha', pool, hpool, hpne, htab, hword, hrne⟩ + have hActH : ∀ {y}, ActiveHighRootA mem' ksb Pp y → ActiveHighRootA mem ksb Pp y := by + rintro y ⟨asid, raw, ha, htag, hy⟩ + exact ⟨asid, raw, hActR ha, htag, hy⟩ + have hPathBack : ∀ {tag sh q}, VsPathA Pp Tb Y mem' ksb tag sh q → + VsPathA Pp Tb Y mem ksb tag sh q ∨ (∃ ppn, pte = .pageTablePTE ppn ∧ q = dTabA w) := by + intro tag sh q hpth + induction hpth with + | native ha htag htb => exact Or.inl (.native (hActR ha) htag htb) + | high ha htag hy => exact Or.inl (.high (hActR ha) htag hy) + | @child tag tb j sh _ htY h12 hj h3' ih => + rcases ih with hpar | ⟨ppn, hpte, rfl⟩ + · by_cases heq : peAF tb j = peAF p i + · rcases htY with htb | ⟨y, hy, rfl⟩ + · obtain ⟨rfl, rfl⟩ := peAF_injA (R.al_mod htb) (R.al_mod hp) hj hi heq + rw [hm'] at h3' ⊢ + obtain ⟨hb0, hb1⟩ := (and3_bitsA w).1 h3' + have hsh : sh = 39 - 9 * Lv tb := by + rcases (R.path_props hpar).2 with ⟨-, -, hsh⟩ | ⟨-, -, y, hy, -, hq⟩ + · exact hsh + · exact absurd (hq ▸ htb) (R.ytb y hy) + cases pte with + | invalidPTE => exact absurd (hw.symm.trans hb0) Bool.false_ne_true + | pageTablePTE ppn => exact Or.inr ⟨ppn, rfl, rfl⟩ + | pagePTE _ _ _ _ => + obtain ⟨-, -, -, hiff, -⟩ := hw + have := hiff.1 hb1; omega + · exact absurd heq (R.slot_offA hp hi hA _ (.high y j hy hj)) + · have hmemEq : mem' (peAF tb j) = mem (peAF tb j) := by + rcases htY with htb | ⟨y, hy, rfl⟩ + · exact hm _ (.pt tb j htb hj) heq + · exact hm _ (.high y j hy hj) heq + rw [hmemEq] at h3' ⊢ + exact Or.inl (.child hpar htY h12 hj h3') + · subst hpte + obtain ⟨hdne, hinv⟩ := hch ppn rfl + have hdTb : dTabA w ∈ Tb := hw.2.2.2.2.2.1 + have heqN : peAF (dTabA w) j ≠ peAF p i := + fun e => hdne (peAF_injA (R.al_mod hdTb) (R.al_mod hp) hj hi e).1 + rw [hm _ (.pt (dTabA w) j hdTb hj) heqN] at h3' + obtain ⟨hb0, -⟩ := (and3_bitsA _).1 h3' + exact absurd ((hinv j hj).1.symm.trans hb0) Bool.false_ne_true + refine + { table := fun hi' hh => by rw [hm _ (.asid hi' hh) (R.slot_offA hp hi hA _ (.asid hi' hh))]; exact R.table hi' hh + tmem := fun hi' hh h0 => by + rw [hm _ (.asid hi' hh) (R.slot_offA hp hi hA _ (.asid hi' hh))] at h0 ⊢; exact R.tmem hi' hh h0 + pool := fun q hq => by + obtain ⟨f, hkq, hf⟩ := R.pool q hq + have hne : specPtr q ≠ specPtr p := fun e => by rw [e, hk] at hkq; cases hkq + refine ⟨f, ?_, fun ha lo hl => ?_⟩ + · rw [ptSetA_kheap, if_neg hne]; exact hkq + · rw [hm _ (.pool q lo hq hl) (R.slot_offA hp hi hA _ (.pool q lo hq hl))]; exact hf (hActP ha) lo hl + kind := fun q hq ha lo hl => by + rw [hm _ (.pool q lo hq hl) (R.slot_offA hp hi hA _ (.pool q lo hq hl))] + exact R.kind q hq (hActP ha) lo hl + asid0 := fun h0 => by + rw [hm _ (.asid 0#u64 (by decide)) (R.slot_offA hp hi hA _ (.asid 0#u64 (by decide)))] at h0 ⊢ + have hq := R.tmem 0#u64 (by decide) h0 + rw [hm _ (.pool _ 0#u64 hq (by decide)) (R.slot_offA hp hi hA _ (.pool _ 0#u64 hq (by decide)))] + exact R.asid0 h0 + pt := fun q hq => by + obtain ⟨t0, hk0, ht0, he0⟩ := R.pt q hq + by_cases hqp : q = p + · subst hqp + rw [hk] at hk0 + cases hk0 + refine ⟨Fl4SpecA64.ptUpd t i.bv pte, ?_, ?_, fun hr' j hj => ?_⟩ + · rw [ptSetA_kheap, if_pos rfl] + · rw [ptUpd_typeA]; exact ht0 + · rw [ptApply_updA t hi hj] + by_cases hji : j = i + · subst hji; rw [if_pos rfl, hm']; exact hw + · rw [if_neg hji, hm _ (.pt q j hq hj) (fun e => hji (peAF_injA (R.al_mod hq) (R.al_mod hq) hj hi e).2)] + obtain ⟨tag, sh, htag, hpth⟩ := hr' + rcases hPathBack hpth with hpar | ⟨ppn, hpte, hdw⟩ + · exact he0 ⟨tag, sh, htag, hpar⟩ j hj + · exact absurd hdw.symm (hch ppn hpte).1 + · refine ⟨t0, ?_, ht0, fun hr' j hj => ?_⟩ + · rw [ptSetA_kheap, if_neg (fun e => hqp (specPtr_inj e))]; exact hk0 + · rw [hm _ (.pt q j hq hj) (fun e => hqp (peAF_injA (R.al_mod hq) (R.al_mod hp) hj hi e).1)] + obtain ⟨tag, sh, htag, hpth⟩ := hr' + rcases hPathBack hpth with hpar | ⟨ppn, hpte, rfl⟩ + · exact he0 ⟨tag, sh, htag, hpar⟩ j hj + · obtain ⟨-, hinv⟩ := hch ppn hpte + rw [(hinv j hj).2 t0 hk0] + exact (hinv j hj).1 + high := fun y hy => by + obtain ⟨l1, hky, he⟩ := R.high y hy + have hne : specPtr y ≠ specPtr p := fun e => by rw [e, hk] at hky; cases hky + refine ⟨l1, ?_, fun ha j hj => ?_⟩ + · rw [ptSetA_kheap, if_neg hne]; exact hky + · rw [hm _ (.high y j hy hj) (R.slot_offA hp hi hA _ (.high y j hy hj))]; exact he (hActH ha) j hj + lv := R.lv + al := R.al + pal := R.pal + yal := R.yal + ytb := R.ytb } + +end frame + +/-! ## FL4's write against `store_pte` -/ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {Pp Tb : Set U64} {Lv : U64 → Nat} {X Y : Set U64} {σ : Fl4SpecA64.State Unit} + +/-- **A descriptor write against `store_pte`** (module docstring). -/ +theorem write_storePteA {s : S} (R : VsRelA Pp Tb Lv X Y (L.mem s) (L.ksb s) σ) {p i w : U64} + {pte : Fl4SpecA64.PTE} (hp : p ∈ Tb) (hi : i.val < 512) (hw : PteRelA Tb Lv (Lv p) w pte) + (hA : ¬ AsW (L.ksb s) (peAF p i)) (hK : Aligned (L.ksb s)) + (hch : ∀ ppn, pte = .pageTablePTE ppn → dTabA w ≠ p ∧ + ∀ j : U64, j.val < 512 → dBitA (L.mem s (peAF (dTabA w) j)) 0 = false ∧ + ∀ t0, σ.kheap (specPtr (dTabA w)) = some (.archObj (.pageTable t0)) → + Fl4SpecA64.ptApply t0 j.bv = .invalidPTE) : + st.write s (peAF p i) w = ok (L.wr s (peAF p i) w) ∧ ∃ σ', + Fl4SpecA64.storePte (Z := Unit) (ptTyA (Lv p)) (specPtr (peAF p i)) pte σ = ({((), σ')}, False) ∧ + VsRelA Pp Tb Lv X Y (L.mem (L.wr s (peAF p i) w)) (L.ksb (L.wr s (peAF p i) w)) σ' ∧ + ∀ q, q ≠ specPtr p → σ'.kheap q = σ.kheap q := by + obtain ⟨t, hk, ht, -⟩ := R.pt p hp + have hsa := peAF_alignedA (R.al_mod hp) hi + refine ⟨L.write_eq _ _ _, _, storePte_runA hk ht (R.al_mod hp) hi pte, ?_, + fun q hq => by rw [ptSetA_kheap, if_neg hq]⟩ + rw [L.ksb_wr] + exact R.of_storePteA hp hi hk hw hA + (fun b hb hne => by rw [L.mem_wr _ _ _ _ hsa (R.foot_alignedA hK b hb), if_neg (Ne.symm hne)]) + (by rw [L.mem_wr _ _ _ _ hsa hsa, if_pos rfl]) hch + +/-- **The zero write of `unmap_page` / `unmap_page_table`** against `store_pte … InvalidPTE` (module docstring). -/ +theorem write_clearPteA {s : S} (R : VsRelA Pp Tb Lv X Y (L.mem s) (L.ksb s) σ) {p i : U64} (hp : p ∈ Tb) + (hi : i.val < 512) (hA : ¬ AsW (L.ksb s) (peAF p i)) (hK : Aligned (L.ksb s)) : + st.write s (peAF p i) 0#u64 = ok (L.wr s (peAF p i) 0#u64) ∧ ∃ σ', + Fl4SpecA64.storePte (Z := Unit) (ptTyA (Lv p)) (specPtr (peAF p i)) .invalidPTE σ = ({((), σ')}, False) ∧ + VsRelA Pp Tb Lv X Y (L.mem (L.wr s (peAF p i) 0#u64)) (L.ksb (L.wr s (peAF p i) 0#u64)) σ' ∧ + ∀ q, q ≠ specPtr p → σ'.kheap q = σ.kheap q := + write_storePteA R hp hi (pteRelA_zero_inv Tb Lv (Lv p)) hA hK (fun _ h => by cases h) + +end + +/-! ## l4v's `lookup_frame` -/ + +theorem pteBaseAddr_pageA (a : Fl4SpecA64.Paddr) (sm : Bool) (at' : Fl4SpecA64.VMAttributes) + (r : Fl4SpecA64.VMRights) : Fl4SpecA64.pteBaseAddr (.pagePTE a sm at' r) = a := rfl + +theorem lvA_le2 {l : Nat} (hl : l ≤ 3) : lvA l ≤ 2 ↔ l ≤ 2 := by + rw [Fin.le_def, lvA_val hl] + exact Iff.rfl + +/-- **`lookup_frame` from FL4's slot** (module docstring): the hypotheses are `lookup_pt_slotA`'s post. -/ +theorem lookupFrameA {Pp Tb : Set U64} {Lv : U64 → Nat} {X Y : Set U64} {mem : U64 → U64} {ksb : U64} + {σ : Fl4SpecA64.State Unit} (R : VsRelA Pp Tb Lv X Y mem ksb σ) {root v slot p sh : U64} {l : Nat} + (hp : p ∈ Tb) (hreach : VsReachA Pp Tb Y mem ksb p) (hl : l ≤ 3) (hsh : sh.val = 12 + 9 * l) + (hlv : Lv p + l = 3) (hs : slot = peAF p (idxA v sh)) + (hL : Fl4SpecA64.ptLookupSlot (specPtr root) v.bv (Fl4SpecA64.ptesOf σ) = some (lvA l, specPtr slot)) : + Fl4SpecA64.lookupFrame (specPtr root) v.bv (Fl4SpecA64.ptesOf σ) = + if dBitA (mem slot) 0 = true ∧ (dBitA (mem slot) 1 = true ↔ Lv p = 3) ∧ 0 < Lv p then + some (Fl4SpecA64.vmsizeOfLevel (lvA l), pageBaseA (mem slot) (Lv p)) + else none := by + obtain ⟨t, hk, hty, hrel⟩ := R.pt p hp + have hidx := idxA_lt v sh + have hread := ptesOf_atA hk (ptT := Fl4SpecA64.levelType (lvA l)) + (by rw [hty, levelType_lvA hl]; congr 1; omega) (R.al_mod hp) hidx + have hpre := hrel hreach _ hidx + rw [← hs] at hread hpre + generalize Fl4SpecA64.ptApply t (idxA v sh).bv = pte at hpre hread + have key : Fl4SpecA64.lookupFrame (specPtr root) v.bv (Fl4SpecA64.ptesOf σ) = + if pte.isPagePTE = true then + (if lvA l ≤ 2 then some (Fl4SpecA64.vmsizeOfLevel (lvA l), Fl4SpecA64.pteBaseAddr pte) else none) + else none := by + unfold Fl4SpecA64.lookupFrame + simp only [rbindA, hL, Option.bind, Fl4SpecA64.oapply2, hread] + by_cases h1 : pte.isPagePTE = true <;> by_cases h2 : lvA l ≤ 2 <;> + simp [h1, h2, rbindA, Fl4Spec.Lookup.oassert, Fl4Spec.Lookup.oreturn, Fl4Spec.Lookup.ofail] + rw [key] + generalize mem slot = w at hpre ⊢ + cases pte with + | invalidPTE => + have h0 : dBitA w 0 = false := hpre + simp [Fl4SpecA64.PTE.isPagePTE, h0] + | pageTablePTE ppn => + obtain ⟨hk3, -, hb1, -⟩ := hpre + rw [if_neg (show ¬ (Fl4SpecA64.PTE.pageTablePTE ppn).isPagePTE = true from Bool.false_ne_true), + if_neg (fun h => by have := h.2.1.1 hb1; omega)] + | pagePTE a sm at' rt => + obtain ⟨hk0, hk3, hb0, hb1, -, ha, -⟩ := hpre + rw [if_pos (show (Fl4SpecA64.PTE.pagePTE a sm at' rt).isPagePTE = true from rfl), + if_pos ((lvA_le2 hl).2 (by omega)), if_pos ⟨hb0, hb1, hk0⟩, pteBaseAddr_pageA, ha] + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/VsMakePoolA.lean b/proofs/Fl4ProofsA64/P3/VsMakePoolA.lean new file mode 100644 index 000000000..6f8821416 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/VsMakePoolA.lean @@ -0,0 +1,43 @@ +/- +P3 (aarch64): the MakePool post with the new ASID pool and post-insertion CDT. +The placement argument for the retained joint objects is `ObjAllocA.disjoint_of_drop` (Architect 21:51Z D1). +-/ +import Fl4ProofsA64.P3.VsAssignA +import Fl4ProofsA64.P1.MdbPoolA + +open Aeneas Aeneas.Std Result +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4SpecA64 + +noncomputable section + +/-- MakePool relates the returned state at the new CDT ghost and at the extended pool set. -/ +def VsMakePoolPostA {S A : Type} {st : machine.Store S} [L : LawfulStore st] + (AR : A → HwRootA → Prop) (Z : XSetsA) (m : McState) (σ : State Unit) + (D : Set (Except SyscallError Fl4ArchInvocation × State Unit) × Prop) (s : S) (a : A) : + core.result.Result Unit error.Exc × S × A → Prop + | (.Err e, s', a') => + s' = s ∧ a' = a ∧ ∃ e', D = ({(.error e', σ)}, False) ∧ VsErrRelA e e' + | (.Ok (), s', a') => + ∃ (frame : U64) (dest parent : CslotPtr) (base : Asid), + let iv := Fl4ArchInvocation.spec (.invokeASIDControl (.makePool (specPtr frame) dest parent base)) + ((.ok iv, σ) ∈ D.1 ∧ ¬ D.2) ∧ ∃ σ1, + setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ∃ (σ' : State Unit) (rv : List Data) (Z' : XSetsA) (H' : Heap) (OS' : Set U64), + (.ok rv, σ') ∈ ((fl4ArchPerformInvocation (Z := Unit) iv).run σ1).1 ∧ + Z'.Pp = (insert frame Z.Pp : Set U64) ∧ TreeA st OS' Z'.G.chains Z'.G.par s' ∧ + JAtZAX (L := L) AR Z' s' a' σ' m ∧ + (∀ i j p, σ'.archState.armAsidTable i = some p → σ'.archState.armAsidTable j = some p → i = j) ∧ + ObjAllocA (st := st) Z' H' OS' s' + +/-- The first free bank produces a 16-bit, 512-aligned base for the new pool cap. -/ +theorem makePoolBaseWf (p : U64) {i : U64} (hi : i.val < 128) : + (i <<< vspace.ASID_LOW_BITS : Result U64) ⦃ base => CapWfA (.AsidPool p base) ⦄ := by + refine WP.spec_mono (shl9_val hi) fun base hb => ?_ + change base.val < 2 ^ 16 ∧ base.val % 512 = 0 + rw [hb] + constructor <;> omega + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/VsMapA.lean b/proofs/Fl4ProofsA64/P3/VsMapA.lean new file mode 100644 index 000000000..363cadb63 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/VsMapA.lean @@ -0,0 +1,898 @@ +/- +P3 (aarch64, A64-P3 S3 gen 19, ruling 19:10Z item 2): **Stage-1 `ARMPageTableMap`** — FL4's `vspace::decode_page_table` +(`vspace.rs` 1091–1144) at label `ARMPageTableMap` on a plain `Cap::PageTable` with a Stage-1 root (`Cap::Pml4`) in the +first extra slot, against l4v's `decode_page_table_invocation` (`decode_pt_inv_map`, `ArchDecode_A`) followed by +`set_thread_state Restart` and `perform_page_table_invocation` (`perform_pt_inv_map`, `Arch_A`), as +`handle_invocation` runs them. The A64 twin of x86's `P3/VsPtMap` / `P3/VsPtMapRel` (`pt_map_rel`). + +* **The words** (`make_user_table`, `vspace.rs` 199): A64 P1's `s1tab_v` (QueueS1Map: `e & 3 = 3`, `e & ADDR_MASK = + paddr`), `ppn_specA` (l4v's `ppn_from_pptr` of the shifted pointer is the address bits' page number), + **`pteRel_mutA`** (the descriptor is `PteRelA`-related to l4v's `PageTablePTE (ppn_from_pptr p)` one level up); + `top_iffA` (l4v's `user_vtop < vaddr`, with the generation-7 `userVtop` hook, is FL4's `vaddr > USER_TOP`), + `ws1A` / `mask_lvA` (FL4's `vaddr & !(2^shift - 1)` is l4v's `vaddr && ~~ mask (pt_bits_left level)`), + `canon_ltA` (the new cap's address encodes), `valid_bitA` / `pteRel_invA` (FL4's `D_VALID` test is l4v's + `old_pte ≠ InvalidPTE`). +* **The new table joins the Stage-1 tables** (pure): `PteRelA.monoA` (`PteRelA` reads `Lv` only at a related + table), **`VsRelA.addPtA`**: a page table `ptr ∉ Tb` whose FL4 words are invalid and whose spec object is a + `NormalPT_T` table of `InvalidPTE`s joins `Tb` at any depth `n ∈ 1..3` (`Lv` updated at `ptr` only); with + `write_storePteA` this is A64 P1's `VsTy.map_table` on the P3 relation. +* **The lookups**: `find_asid_raw_errA` / **`find_asid_tbA`** (`find_asid`'s errors are `InvalidRoot`; its root is a + Stage-1 root of `Tb` at depth 0), `findVs_errA` (l4v's hooked `find_vspace_for_asid` answers `InvalidRoot` when + the ASID names no root or an FL4 root), **`check_vspaceA`** (FL4's `check_vspace` of a mapped `Pml4` cap against + l4v's `find_vspace_for_asid` and the root compare: `ChkRelA`). +* **The spec side**: `ptTailA` / `ptRestA` (l4v's `decode_pt_inv_map` after `check_vspace_root` and the `user_vtop` + check, and after the root compare), `ptDec_truncA`, `ptDec_mappedA`, `ptDec_noRootA`, `ptDec_topA`, `ptDec_bodyA`, + **`ptRest_runA`**, `ptInv_mapA` (the label dispatch of `decode_page_table_invocation`). +* **`pt_map_refinesA`** (x86 `pt_map_rel`): under `JEntA`, FL4's decode either fails with an error related to the + spec decode's (`VsErrRelA`: `SysErrRelA` or `InvalidCapability`), nothing changed, or succeeds, the spec decodes + the map deterministically, and `set_thread_state cur Restart` followed by `perform_page_table_invocation` has an + outcome whose `VsRelA` holds against FL4's state **at the ghosts `insert ptr S1` and `Lv` updated at `ptr`** + (`PtMapPostA`; the restart by `JEntA.restartA`, the cap write by `StateRelA.setAbsA` and A64 P1's `set_cap_eq`, + the descriptor by `VsRelA.addPtA` and `write_storePteA`, the cache clean by `doMachineOp_okA`). (Gen 22) It is + the corollary of **`pt_map_refinesGA`**, whose post `PtMapPostGA` adds the lift's obligation `MapJA` (`P3/VsTabJA`: + the restart and cap write by `restart_setcapJA`, the descriptor write by `map_finJA`), read by `pt_map_jA` + (`P3/VsMapJA`); its restart-site hypotheses are bundled as `RsSiteA`. + +**Decode-site hypotheses** (not named premises; listed for the premise accounting as `RegViewA` / `BufViewA`): +* **`UnmPtA`** [J-side, decode-site] — *an unmapped page table is all-invalid and unreferenced* (ruling 19:10Z item + 2): `ptr` is no Stage-1 table of the relation (so no related descriptor names it: a `PteRelA` table descriptor names + a table of `Tb`), page-aligned, no window's level-1 table, its physical address below 2^44 (PORTING.md's **[P]**), + its 512 FL4 words invalid and in no `OS` slot and no TCB, and the spec's object at `specPtr ptr` a `NormalPT_T` + page table of `InvalidPTE`s. Discharged later by A64-S1A's B3 (A64 P1's `UnmT` / `S1Pl` and the object creation). +* **`hS1` / `hHi`** — the extra cap is no `Ept` and no `HighVSpace` cap (ruling: strictly Stage-1 roots; FL4's other + two branches, `ept::map_table` and `map_high_page_table`, are `performExtVSpace` work). +* The restart's case hypotheses (`P`, `Y`, `hrun`, `hcur`, `hvs`, `hvc`, as `JEntA.restartA`), the slot's place + (`hos`, `hsub`, `hab`), the message (`hlen`, `hargs`: `argsJ`) and the extra cap (`ExtraRelA`: `ExtraOKA.rel1`). + +**Changed statement (gen 30, forced by ruling 01:13Z item 1):** `PtMapPostGA` takes the invoked slot `sl` and adds +`1 ≤ n` and `∃ asid va` with `MapJA … sl (.PageTable ptr true asid va)` (the slot frame); `pt_map_refinesGA`'s post +at `sl := cslotAddrA cte`. `pt_map_refinesA` unchanged. + +No named premise. +-/ +import Fl4ProofsA64.P3.VsTabJA +import Fl4ProofsA64.P3.VsUnmapA +import Fl4ProofsA64.P1.QueueS1Map +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## Words -/ + +theorem ws_valA (ptr : U64) : + (core.num.U64.wrapping_sub ptr layout.PPTR_BASE).val = (ptr.val + 140737488355328) % 2 ^ 64 := by + rw [pptrBase_litA] + simp [core.num.U64.wrapping_sub_val_eq, U64.size_def, U64.numBits] + +/-- l4v's `ppn_from_pptr` of FL4's shifted pointer: the page number of `paddr(ptr)`. -/ +theorem ppn_specA (ptr : U64) : + Fl4SpecA64.ppnFromPptr (specPtr ptr) = + BitVec.ofNat 32 ((core.num.U64.wrapping_sub ptr layout.PPTR_BASE).val / 4096) := by + unfold Fl4SpecA64.ppnFromPptr + rw [addrFromPPtr_specPtr] + apply BitVec.eq_of_toNat_eq + have e1 : (ptr.bv - fl4PptrBaseA).toNat = (ptr.val + 140737488355328) % 2 ^ 64 := by + rw [BitVec.toNat_sub] + show (2 ^ 64 - 18446603336221196288 + ptr.bv.toNat) % 2 ^ 64 = _ + have : ptr.bv.toNat = ptr.val := rfl + omega + show (BitVec.setWidth Fl4SpecA64.ppnLen ((ptr.bv - fl4PptrBaseA) >>> Fl4SpecA64.pageBits)).toNat = + (BitVec.ofNat 32 ((core.num.U64.wrapping_sub ptr layout.PPTR_BASE).val / 4096)).toNat + have hipa : Fl4SpecA64.ipaSize = 44 := rfl + simp only [BitVec.toNat_ofNat, BitVec.toNat_setWidth, BitVec.toNat_ushiftRight, e1, Nat.shiftRight_eq_div_pow, + ws_valA, Fl4SpecA64.ppnLen, Fl4SpecA64.pageBits, hipa, Nat.reducePow] + +/-- **`make_user_table`'s descriptor against l4v's `PageTablePTE`** (module docstring): a table descriptor (`e & 3 = +3`) whose address bits are `paddr(ptr)` (below 2^44) names `ptr`, a table of `Tb` one level below. -/ +theorem pteRel_mutA {Tb : Set U64} {Lv : U64 → Nat} {k : Nat} {ptr v : U64} (hk : k < 3) + (hv3 : (v &&& 3#u64) = 3#u64) (hvm : (v &&& vspace.ADDR_MASK) = core.num.U64.wrapping_sub ptr layout.PPTR_BASE) + (hpa : (core.num.U64.wrapping_sub ptr layout.PPTR_BASE).val < 2 ^ 44) (hT : ptr ∈ Tb) (hL : Lv ptr = k + 1) : + PteRelA Tb Lv k v (.pageTablePTE (Fl4SpecA64.ppnFromPptr (specPtr ptr))) := by + have hd : dAddrA v = core.num.U64.wrapping_sub ptr layout.PPTR_BASE := by + rw [← hvm]; unfold vspace.ADDR_MASK dAddrA; rfl + obtain ⟨h0, h1⟩ := (and3_bitsA v).1 hv3 + have ht : dTabA v = ptr := by unfold dTabA; rw [hd, ← pptrBase_litA]; exact wa_ws ptr layout.PPTR_BASE + refine ⟨hk, h0, h1, by rw [hd]; exact hpa, ?_, by rw [ht]; exact hT, by rw [ht]; exact hL⟩ + unfold ppnA + rw [ppn_specA, hd] + +/-- l4v's `user_vtop < vaddr` (the generation-7 `userVtop` hook) is FL4's `vaddr > USER_TOP`. -/ +theorem top_iffA (v : U64) : Fl4SpecA64.userVtop < v.bv ↔ v > fl4_abi.consts.USER_TOP := by + have hU : fl4_abi.consts.USER_TOP.val = 140737488355327 := by unfold fl4_abi.consts.USER_TOP; rfl + have hV : Fl4SpecA64.userVtop.toNat = 140737488355327 := by + unfold Fl4SpecA64.userVtop Fl4SpecA64.fl4UserVtop; rfl + rw [BitVec.lt_def, hV] + constructor + · intro h + show fl4_abi.consts.USER_TOP.val < v.val + rw [hU]; exact h + · intro h + have h' : fl4_abi.consts.USER_TOP.val < v.val := h + rw [hU] at h'; exact h' + +/-- A value below 2^47 is canonical (A64 P1's cap encoding). -/ +theorem canon_ltA {p : U64} (h : p.val < 2 ^ 47) : CapRt.Canonical p := by + left + intro j hj hj' + rw [BitVec.getElem_eq_testBit_toNat] + exact Nat.testBit_lt_two_pow (lt_of_lt_of_le h (Nat.pow_le_pow_right (by decide) hj')) + +/-- The mask of a level's shift (`pt_bits_left`), for the levels a table can be mapped at. -/ +theorem mask_lvA {l : Nat} (h1 : 1 ≤ l) (h3 : l ≤ 3) : + (Fl4SpecA64.mask (12 + 9 * l) : BitVec 64) = BitVec.ofNat 64 (2 ^ (12 + 9 * l) - 1) := by + interval_cases l <;> decide + +/-! ## The new table joins the Stage-1 tables -/ + +/-- `PteRelA` is monotone in the tables and reads `Lv` only at a related table. -/ +theorem PteRelA.monoA {Tb Tb' : Set U64} {Lv Lv' : U64 → Nat} {k : Nat} {w : U64} {pte : Fl4SpecA64.PTE} + (h : PteRelA Tb Lv k w pte) (hT : Tb ⊆ Tb') (hL : ∀ q ∈ Tb, Lv' q = Lv q) : PteRelA Tb' Lv' k w pte := by + cases pte with + | invalidPTE => exact h + | pageTablePTE ppn => + obtain ⟨h1, h2, h3, h4, h5, h6, h7⟩ := h + exact ⟨h1, h2, h3, h4, h5, hT h6, by rw [hL _ h6]; exact h7⟩ + | pagePTE a sm at' r => exact h + +/-- **An unmapped page table** (decode-site hypothesis, module docstring). -/ +structure UnmPtA (Tb Y OS T : Set U64) (mem : U64 → U64) (σ : Fl4SpecA64.State Unit) (ptr : U64) : Prop where + nt : ptr ∉ Tb + al : 4096 ∣ ptr.val + ytb : ∀ y ∈ Y, wa y 4096#u64 ≠ ptr + pa : (core.num.U64.wrapping_sub ptr layout.PPTR_BASE).val < 2 ^ 44 + inv : ∀ i : U64, i.val < 512 → dBitA (mem (peAF ptr i)) 0 = false + os : ∀ i : U64, i.val < 512 → ¬ InOS OS (peAF ptr i) + tcb : ∀ i : U64, i.val < 512 → ∀ t ∈ T, (peAF ptr i).val < t.val ∨ t.val + 4096 ≤ (peAF ptr i).val + spec : ∃ t, σ.kheap (specPtr ptr) = some (.archObj (.pageTable t)) ∧ Fl4SpecA64.ptType t = .normalPtT ∧ + ∀ i : U64, i.val < 512 → Fl4SpecA64.ptApply t i.bv = .invalidPTE + +section add +variable {Pp Tb : Set U64} {Lv : U64 → Nat} {X Y : Set U64} {ksb : U64} {σ : Fl4SpecA64.State Unit} + +/-- **The new table joins `Tb` at depth `n`** (module docstring). -/ +theorem VsRelA.addPtA {mem : U64 → U64} (R : VsRelA Pp Tb Lv X Y mem ksb σ) {ptr : U64} (hnt : ptr ∉ Tb) + (hal : 4096 ∣ ptr.val) (hy : ∀ y ∈ Y, wa y 4096#u64 ≠ ptr) + (hinv : ∀ i : U64, i.val < 512 → dBitA (mem (peAF ptr i)) 0 = false) {t : Fl4SpecA64.PT} + (hk : σ.kheap (specPtr ptr) = some (.archObj (.pageTable t))) (ht : Fl4SpecA64.ptType t = .normalPtT) + (he : ∀ i : U64, i.val < 512 → Fl4SpecA64.ptApply t i.bv = .invalidPTE) {n : Nat} (hn1 : 0 < n) (hn3 : n ≤ 3) : + VsRelA Pp (insert ptr Tb) (Function.update Lv ptr n) X Y mem ksb σ := by + have hL : ∀ q ∈ Tb, Function.update Lv ptr n q = Lv q := fun q hq => + Function.update_of_ne (fun (e : q = ptr) => hnt (by rw [← e]; exact hq)) _ _ + have hsub : Tb ⊆ insert ptr Tb := Set.subset_insert _ _ + have hPath : ∀ {tag sh q}, VsPathA Pp (insert ptr Tb) Y mem ksb tag sh q → VsPathA Pp Tb Y mem ksb tag sh q := by + intro tag sh q hpth + induction hpth with + | @native asid raw ha htag _ => + obtain ⟨pool, hpool, hactP, lo, hlo, hw, hrne⟩ := ha.activePool + have hk0 := R.kind pool hpool hactP lo hlo + rw [hw] at hk0 + rcases hk0 with rfl | ⟨-, htb, -⟩ | ⟨h1, -⟩ | ⟨h2, -⟩ + · exact absurd rfl hrne + · rw [untagA_of htag] at htb + exact .native ha htag htb + · omega + · omega + | high ha htag hy' => exact .high ha htag hy' + | @child tag tb i sh _ _ h12 hi h3 ih => + have htY : tb ∈ Tb ∨ ∃ y ∈ Y, tb = wa y 4096#u64 := by + rcases (R.path_props ih).2 with ⟨htb, -⟩ | ⟨-, -, y, hy', -, hq⟩ + · exact Or.inl htb + · exact Or.inr ⟨y, hy', hq⟩ + exact .child ih htY h12 hi h3 + have hReach : ∀ {q}, VsReachA Pp (insert ptr Tb) Y mem ksb q → VsReachA Pp Tb Y mem ksb q := by + rintro q ⟨tag, sh, htag, hpth⟩ + exact ⟨tag, sh, htag, hPath hpth⟩ + refine ⟨R.table, R.tmem, R.pool, fun q hq ha lo hl => ?_, R.asid0, fun q hq => ?_, fun y hy' => ?_, fun q hq => ?_, + fun q hq => ?_, R.pal, R.yal, fun y hy' => ?_⟩ + · rcases R.kind q hq ha lo hl with h | ⟨h1, h2, h3⟩ | h | h + · exact Or.inl h + · exact Or.inr (Or.inl ⟨h1, hsub h2, by rw [hL _ h2]; exact h3⟩) + · exact Or.inr (Or.inr (Or.inl h)) + · exact Or.inr (Or.inr (Or.inr h)) + · rcases Set.mem_insert_iff.1 hq with rfl | hq + · refine ⟨t, hk, ?_, fun _ i hi => ?_⟩ + · rw [ht, Function.update_self] + unfold ptTyA; rw [if_neg (by omega)] + · rw [Function.update_self, he i hi] + exact hinv i hi + · obtain ⟨t0, hk0, ht0, hr⟩ := R.pt q hq + exact ⟨t0, hk0, by rw [hL q hq]; exact ht0, + fun hrc i hi => by rw [hL q hq]; exact (hr (hReach hrc) i hi).monoA hsub hL⟩ + · obtain ⟨l1, hk1, hr⟩ := R.high y hy' + exact ⟨l1, hk1, fun ha i hi => (hr ha i hi).monoA hsub hL⟩ + · rcases Set.mem_insert_iff.1 hq with rfl | hq + · rw [Function.update_self]; exact hn3 + · rw [hL q hq]; exact R.lv q hq + · rcases Set.mem_insert_iff.1 hq with rfl | hq + · exact hal + · exact R.al q hq + · rintro (h | h) + · exact hy y hy' h + · exact R.ytb y hy' h + +/-- `VsRelA` across a machine-state-only spec step. -/ +theorem VsRelA.of_msA {mem : U64 → U64} (R : VsRelA Pp Tb Lv X Y mem ksb σ) {σ' : Fl4SpecA64.State Unit} + (h : MsA σ σ') : VsRelA Pp Tb Lv X Y mem ksb σ' := by + obtain ⟨ms, rfl⟩ := h + exact ⟨R.table, R.tmem, R.pool, R.kind, R.asid0, R.pt, R.high, R.lv, R.al, R.pal, R.yal, R.ytb⟩ + +end add + +/-! ## The lookups -/ + +section look +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {Pp Tb : Set U64} {Lv : U64 → Nat} {X Y : Set U64} {σ : Fl4SpecA64.State Unit} + +/-- `find_asid_raw`'s errors are `InvalidRoot`. -/ +theorem find_asid_raw_errA (s : S) {asid : U64} (h16 : asid.val < 65536) : + vspace.find_asid_raw st s asid ⦃ r => ∀ e, r = .Err e → e = .InvalidRoot ⦄ := by + unfold vspace.find_asid_raw + obtain ⟨i, ei, vi⟩ := shrA asid 16#u64 65536 (by decide) rfl + rw [asidBitsA, ei] + simp only [bind_ok] + have hi0 : i = 0#u64 := UScalar.eq_of_val_eq (by rw [vi]; exact (Nat.div_eq_of_lt h16).trans rfl) + subst hi0 + simp only [bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + obtain ⟨i1, e1, -⟩ := shrA asid 9#u64 512 (by decide) rfl + rw [asidLowA, e1] + simp only [bind_ok] + rw [asid_table_eq] + simp only [bind_ok] + by_cases hp : L.mem s (atAF (L.ksb s) (i1 &&& 127#u64)) = 0#u64 + · rw [if_pos hp] + exact (WP.spec_ok _).2 fun e h => by injection h with h; exact h.symm + · rw [if_neg hp] + simp only [lift, bind_ok, L.read_eq] + by_cases hr : L.mem s (wa (L.mem s (atAF (L.ksb s) (i1 &&& 127#u64))) + (core.num.U64.wrapping_mul (asid &&& 511#u64) 8#u64)) = 0#u64 + · rw [if_pos hr] + exact (WP.spec_ok _).2 fun e h => by injection h with h; exact h.symm + · rw [if_neg hr] + exact (WP.spec_ok _).2 fun e h => by cases h + +/-- `find_asid`'s answers on the Stage-1 tables (module docstring). -/ +def FindTbA (Tb : Set U64) (Lv : U64 → Nat) : core.result.Result U64 error.LookupFault → Prop + | .Ok w => w ∈ Tb ∧ Lv w = 0 + | .Err e => e = .InvalidRoot + +/-- **`find_asid`: an error is `InvalidRoot`, a root a Stage-1 root of `Tb`** (module docstring). -/ +theorem find_asid_tbA {s : S} (hR : VsRelA Pp Tb Lv X Y (L.mem s) (L.ksb s) σ) {asid : U64} + (h16 : asid.val < 65536) : vspace.find_asid st s asid ⦃ r => FindTbA Tb Lv r ⦄ := by + unfold vspace.find_asid + refine WP.spec_bind (WP.spec_and (find_asid_rawA hR h16) (find_asid_raw_errA s h16)) fun r ⟨hr, he⟩ => ?_ + rcases r with w | e + · obtain ⟨-, hw0, hk⟩ := hr + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok, lift, rtag, rs1] + split + · exact (WP.spec_ok _).2 rfl + · rename_i hz + have ht0 : tagA w = 0 := (tag_iffA w).1 (by apply UScalar.eq_of_val_eq; simpa using hz) + refine (WP.spec_ok _).2 ?_ + rcases hk with h | ⟨-, hT, hL⟩ | ⟨h, -⟩ | ⟨h, -⟩ + · exact absurd h hw0 + · rw [untagA_of ht0] at hT hL; exact ⟨hT, hL⟩ + · omega + · omega + · simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame.from, + bind_tc_ok] + exact (WP.spec_ok _).2 (he e rfl) + +end look + +/-! ## The spec side -/ + +section spec +open Fl4Spec Fl4SpecA64 + +/-- l4v's hooked `find_vspace_for_asid` answers `InvalidRoot` when the ASID names no root or an FL4 root. -/ +theorem findVs_errA {σ : State Unit} {a : Asid} + (h : ∀ p, vspaceForAsid a σ = some p → rootKindAt σ p ≠ .stage1) : + (findVspaceForAsid (Z := Unit) a).run σ = ({(.error .invalidRoot, σ)}, False) := by + cases hv : vspaceForAsid a σ with + | none => exact findVs_noneA hv + | some r => + have hx := h r hv + obtain ⟨o, ho⟩ : ∃ o, σ.kheap r = some (.archObj (.ext o)) := by + cases he : extObjAt σ r with + | none => exact absurd (by simp [rootKindAt, he]) hx + | some o => + unfold extObjAt at he + split at he + · rename_i o' heq; exact ⟨o', heq⟩ + · cases he + unfold findVspaceForAsid + rw [runA_bind_ok (liftE_runA (show NonDet.gets (vspaceForAsid (Z := Unit) a) σ = + ({(vspaceForAsid a σ, σ)}, False) from rfl)), hv] + refine (runA_bind_ok (liftE_runA (m := NonDet.gets _) rfl) _).trans ?_ + simp only [Option.filter_some, ho] + rfl + +theorem lefo_okA {α : Type} {m : LFMonad Unit α} {σ : State Unit} {a : α} (h : m.run σ = ({(.ok a, σ)}, False)) : + (lookupErrorOnFailure (Z := Unit) false m).run σ = ({(.ok a, σ)}, False) := + run_handleE_okA h + +theorem lefo_errA {α : Type} {m : LFMonad Unit α} {σ : State Unit} {e : LookupFailure} + (h : m.run σ = ({(.error e, σ)}, False)) : + (lookupErrorOnFailure (Z := Unit) false m).run σ = ({(.error (.failedLookup false e), σ)}, False) := by + unfold lookupErrorOnFailure + rw [run_handleE_errA h] + rfl + +/-- **l4v's `decode_pt_inv_map` after the root compare** (module docstring). -/ +def ptRestA (p : ObjRef) (t : PtType) (pt : ObjRef) (asid : Asid) (v : Data) (cte : CslotPtr) : + SEMonad Unit ArchInvocation := do + let (level, slot) ← NonDetE.liftE (NonDet.getsThe fun s : State Unit => ptLookupSlot pt v (ptesOf s)) + let oldPte ← NonDetE.liftE <| getPte (levelType level) slot + NonDetE.whenE (ptBitsLeft level = pageBits ∨ oldPte ≠ .invalidPTE) <| NonDetE.throwError .deleteFirst + let pte ← NonDetE.returnOk (PTE.pageTablePTE (ppnFromPptr p)) + let cap' ← NonDetE.returnOk (ArchCap.pageTableCap p t (some (asid, v &&& ~~~(mask (ptBitsLeft level))))) + NonDetE.returnOk <| .invokePageTable <| .pageTableMap cap' cte pte slot level + +/-- **l4v's `decode_pt_inv_map` after `check_vspace_root` and the `user_vtop` check** (module docstring). -/ +def ptTailA (p : ObjRef) (t : PtType) (pt : ObjRef) (asid : Asid) (v : Data) (cte : CslotPtr) : + SEMonad Unit ArchInvocation := do + let pt' ← lookupErrorOnFailure false <| findVspaceForAsid asid + NonDetE.whenE (pt' ≠ pt) <| NonDetE.throwError (.invalidCapability 1) + ptRestA p t pt asid v cte + +theorem ptInv_mapA {lbl : Data} (hl : invocationType lbl = .archInvocationLabel .armPageTableMap) (args : List Data) + (cte : CslotPtr) (cap : ArchCap) (exs : List (Cap × CslotPtr)) : + decodePageTableInvocation (Z := Unit) lbl args cte cap exs = decodePtInvMap lbl args cte cap exs := by + unfold decodePageTableInvocation + rw [if_pos hl] + +theorem ptDec_truncA (lbl : Data) (args : List Data) (cte : CslotPtr) (p : ObjRef) (t : PtType) + (md : Option (Asid × VSpaceRef)) (exs : List (Cap × CslotPtr)) (σ : State Unit) + (h : ¬ (args.length > 1 ∧ exs.length > 0)) : + (decodePtInvMap (Z := Unit) lbl args cte (.pageTableCap p t md) exs).run σ = + ({(.error .truncatedMessage, σ)}, False) := by + simp only [decodePtInvMap, if_neg h]; rfl + +theorem ptDec_mappedA (lbl : Data) (args : List Data) (cte : CslotPtr) (p : ObjRef) (t : PtType) + (md : Option (Asid × VSpaceRef)) (exs : List (Cap × CslotPtr)) (σ : State Unit) + (h : args.length > 1 ∧ exs.length > 0) (hm : md ≠ none) : + (decodePtInvMap (Z := Unit) lbl args cte (.pageTableCap p t md) exs).run σ = + ({(.error (.invalidCapability 0), σ)}, False) := by + simp only [decodePtInvMap, if_pos h, NonDetE.whenE, hm, ne_eq, not_false_eq_true, ↓reduceIte, + NonDetE.throwError, ndE_throw_bindA] + rfl + +theorem checkRoot_noA {vc : Cap} (hv : ∀ pt asid x, vc ≠ .archObjectCap (.pageTableCap pt .vsRootPtT (some (asid, x)))) : + checkVspaceRoot (Z := Unit) vc 1 = NonDetE.throwError (.invalidCapability 1) := by + unfold checkVspaceRoot + split + · exact absurd rfl (hv _ _ _) + · rfl + +theorem ptDec_noRootA (lbl v a1 : Data) (r : List Data) (cte : CslotPtr) (p : ObjRef) (t : PtType) (vc : Cap) + (sl : CslotPtr) (rest : List (Cap × CslotPtr)) (σ : State Unit) + (hv : ∀ pt asid x, vc ≠ .archObjectCap (.pageTableCap pt .vsRootPtT (some (asid, x)))) : + (decodePtInvMap (Z := Unit) lbl (v :: a1 :: r) cte (.pageTableCap p t none) ((vc, sl) :: rest)).run σ = + ({(.error (.invalidCapability 1), σ)}, False) := by + simp only [decodePtInvMap, List.length_cons, gt_iff_lt, Nat.lt_add_left_iff_pos, Nat.zero_lt_succ, and_self, + ↓reduceIte, NonDetE.whenE, ne_eq, not_true_eq_false, ndE_pure_bindA, List.getElem?_cons_zero, the, + checkRoot_noA hv, NonDetE.throwError, ndE_throw_bindA] + rfl + +theorem ptDec_topA (lbl v a1 : Data) (r : List Data) (cte : CslotPtr) (p : ObjRef) (t : PtType) (pt : ObjRef) + (asid : Asid) (x : VSpaceRef) (sl : CslotPtr) (rest : List (Cap × CslotPtr)) (σ : State Unit) + (hv : userVtop < v) : + (decodePtInvMap (Z := Unit) lbl (v :: a1 :: r) cte (.pageTableCap p t none) + ((.archObjectCap (.pageTableCap pt .vsRootPtT (some (asid, x))), sl) :: rest)).run σ = + ({(.error (.invalidArgument 0), σ)}, False) := by + simp only [decodePtInvMap, List.length_cons, gt_iff_lt, Nat.lt_add_left_iff_pos, Nat.zero_lt_succ, and_self, + ↓reduceIte, NonDetE.whenE, ne_eq, not_true_eq_false, ndE_pure_bindA, List.getElem?_cons_zero, the, + checkVspaceRoot, NonDetE.returnOk, hv, NonDetE.throwError, ndE_throw_bindA] + rfl + +theorem ptDec_bodyA (lbl v a1 : Data) (r : List Data) (cte : CslotPtr) (p : ObjRef) (t : PtType) (pt : ObjRef) + (asid : Asid) (x : VSpaceRef) (sl : CslotPtr) (rest : List (Cap × CslotPtr)) (σ : State Unit) + (hv : ¬ userVtop < v) : + (decodePtInvMap (Z := Unit) lbl (v :: a1 :: r) cte (.pageTableCap p t none) + ((.archObjectCap (.pageTableCap pt .vsRootPtT (some (asid, x))), sl) :: rest)).run σ = + (ptTailA p t pt asid v cte).run σ := by + simp only [decodePtInvMap, List.length_cons, gt_iff_lt, Nat.lt_add_left_iff_pos, Nat.zero_lt_succ, and_self, + ↓reduceIte, NonDetE.whenE, ne_eq, not_true_eq_false, ndE_pure_bindA, List.getElem?_cons_zero, the, + checkVspaceRoot, NonDetE.returnOk, hv, ptTailA, ptRestA] + try rfl + +/-- **l4v's tail after the root compare at a found slot** (module docstring). -/ +theorem ptRest_runA {p : ObjRef} {t : PtType} {pt : ObjRef} {asid : Asid} {v : Data} {cte : CslotPtr} + {σ : State Unit} {lv : VmLevel} {sl : ObjRef} {pte : PTE} + (hL : ptLookupSlot pt v (ptesOf σ) = some (lv, sl)) (hP : getPte (Z := Unit) (levelType lv) sl σ = ({(pte, σ)}, False)) : + (ptRestA p t pt asid v cte).run σ = + if ptBitsLeft lv = pageBits ∨ pte ≠ .invalidPTE then ({(.error .deleteFirst, σ)}, False) + else ({(.ok (.invokePageTable (.pageTableMap (.pageTableCap p t (some (asid, v &&& ~~~(mask (ptBitsLeft lv))))) + cte (.pageTablePTE (ppnFromPptr p)) sl lv)), σ)}, False) := by + unfold ptRestA + rw [lfA_run_getsThe_bind (f := fun s : State Unit => ptLookupSlot pt v (ptesOf s)) (s := σ) hL] + dsimp only + rw [run_bind_okA (run_liftEA hP)] + by_cases hc : ptBitsLeft lv = pageBits ∨ pte ≠ .invalidPTE + · rw [if_pos hc] + simp only [NonDetE.whenE, hc, ↓reduceIte, NonDetE.throwError, ndE_throw_bindA] + rfl + · rw [if_neg hc] + simp only [NonDetE.whenE, hc, ↓reduceIte, ndE_pure_bindA, NonDetE.returnOk] + rfl + +end spec + +/-! ## FL4's `check_vspace` -/ + +/-- Errors of the VSpace decodes (module docstring): `SysErrRelA`, or `InvalidCapability` (no CNode decode raises +it). -/ +def VsErrRelA (e : error.Exc) (e' : Fl4SpecA64.SyscallError) : Prop := + SysErrRelA e e' ∨ ∃ n : U64, e = .Sys (.InvalidCapability n) ∧ e' = .invalidCapability n.val + +/-- What `check_vspace` of a mapped `Pml4` cap says against l4v (module docstring). -/ +def ChkRelA (Tb : Set U64) (Lv : U64 → Nat) (σ : Fl4SpecA64.State Unit) (r asid : U64) (p : Fl4SpecA64.ObjRef) + (t : Fl4SpecA64.PtType) (v : Fl4SpecA64.Data) (cte : Fl4SpecA64.CslotPtr) : + core.result.Result (U64 × U64) error.Exc → Prop + | .Ok x => x = (r, asid) ∧ r ∈ Tb ∧ Lv r = 0 ∧ + (ptTailA p t (specPtr r) (asidOfA asid) v cte).run σ = (ptRestA p t (specPtr r) (asidOfA asid) v cte).run σ ∧ + Fl4SpecA64.vspaceForAsid (asidOfA asid) σ = some (specPtr r) + | .Err e => ∃ e', (ptTailA p t (specPtr r) (asidOfA asid) v cte).run σ = ({(.error e', σ)}, False) ∧ VsErrRelA e e' + +section chk +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {Pp Tb : Set U64} {Lv : U64 → Nat} {X Y : Set U64} {σ : Fl4SpecA64.State Unit} + +/-- **`check_vspace` against l4v's root check** (module docstring). -/ +theorem check_vspaceA {s : S} (hR : VsRelA Pp Tb Lv X Y (L.mem s) (L.ksb s) σ) + (hX : ∀ r ∈ X, ∃ es, σ.kheap (specPtr r) = some (.archObj (.ext (.stage2Root es)))) + {r asid : U64} (h16 : asid.val < 65536) (p : Fl4SpecA64.ObjRef) (t : Fl4SpecA64.PtType) (v : Fl4SpecA64.Data) + (cte : Fl4SpecA64.CslotPtr) : + vspace.check_vspace st s (.Pml4 r true asid) 1#u64 ⦃ res => ChkRelA Tb Lv σ r asid p t v cte res ⦄ := by + simp only [vspace.check_vspace, ↓reduceIte, vspace.find_vspace_for_asid] + refine WP.spec_bind (WP.spec_and (find_asid_findA hR hX h16) (find_asid_tbA hR h16)) fun res ⟨hf, ht⟩ => ?_ + rcases res with w | e + · obtain ⟨hv, hk⟩ := hf + obtain ⟨hwT, hwL⟩ := ht + have hrun : (Fl4SpecA64.lookupErrorOnFailure (Z := Unit) false + (Fl4SpecA64.findVspaceForAsid (asidOfA asid))).run σ = ({(.ok (specPtr w), σ)}, False) := + lefo_okA (findVs_runA hv hk) + by_cases hw : w = r + · subst hw + simp only [bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + refine (WP.spec_ok _).2 ⟨rfl, hwT, hwL, ?_, hv⟩ + unfold ptTailA + rw [run_bind_okA hrun, run_bind_okA (run_whenE_falseA (by simp) _ _)] + · have hb : (w != r) = true := by simpa using hw + simp only [hb, ↓reduceIte, error.invalid_cap] + refine (WP.spec_ok _).2 ⟨.invalidCapability 1, ?_, Or.inr ⟨1#u64, rfl, rfl⟩⟩ + unfold ptTailA + rw [run_bind_okA hrun] + have hne : specPtr w ≠ specPtr r := fun e => hw (specPtr_inj e) + simp only [Fl4Spec.NonDetE.whenE, hne, ne_eq, not_false_eq_true, ↓reduceIte, Fl4Spec.NonDetE.throwError, + ndE_throw_bindA] + rfl + · have he : e = .InvalidRoot := ht + subst he + simp only + refine (WP.spec_ok _).2 ⟨.failedLookup false .invalidRoot, ?_, Or.inl ⟨rfl, trivial⟩⟩ + unfold ptTailA + rw [run_bind_errorA (lefo_errA (m := Fl4SpecA64.findVspaceForAsid (asidOfA asid)) (findVs_errA hf))] + +end chk + +/-! ## The refinement -/ + +section top +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-- **The outcome of the page-table map against the spec decode's run `D` at `σ`** (module docstring; x86 +`pt_map_rel`'s post, the VSpace part). -/ +def PtMapPostA (Z : XSetsA) (ptr : U64) (σ : Fl4SpecA64.State Unit) + (D : Set (Except Fl4SpecA64.SyscallError Fl4SpecA64.ArchInvocation × Fl4SpecA64.State Unit) × Prop) + (s : S) (a : A) : core.result.Result Unit error.Exc × S × A → Prop + | (.Err e, s', a') => s' = s ∧ a' = a ∧ ∃ e', D = ({(.error e', σ)}, False) ∧ VsErrRelA e e' + | (.Ok (), s', a') => a' = a ∧ ∃ iv, D = ({(.ok (.invokePageTable iv), σ)}, False) ∧ ∃ σ1, + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ ∃ σ' n, + ((), σ') ∈ (Fl4SpecA64.performPageTableInvocation (Z := Unit) iv σ1).1 ∧ + VsRelA Z.Pp (insert ptr Z.S1) (Function.update Z.Lv ptr n) Z.X Z.Y (L.mem s') (L.ksb s') σ' + +/-- (gen 22) **`PtMapPostA` with the lift's obligation** (`MapJA`, `P3/VsTabJA`; the extra condition is ruling (c)'s +`ptr ∉ S2PgE Z.X Z.T2 ∧ ptr ∉ Z.Y`): the post `pt_map_refinesGA` proves, `pt_map_refinesA` / `pt_map_jA` read. -/ +def PtMapPostGA (AR : A → HwRootA → Prop) (Z : XSetsA) (OS : Set U64) (sl : U64) (m : Fl4SpecA64.McState) (ptr : U64) + (σ : Fl4SpecA64.State Unit) + (D : Set (Except Fl4SpecA64.SyscallError Fl4SpecA64.ArchInvocation × Fl4SpecA64.State Unit) × Prop) + (s : S) (a : A) : core.result.Result Unit error.Exc × S × A → Prop + | (.Err e, s', a') => s' = s ∧ a' = a ∧ ∃ e', D = ({(.error e', σ)}, False) ∧ VsErrRelA e e' + | (.Ok (), s', a') => a' = a ∧ ∃ iv, D = ({(.ok (.invokePageTable iv), σ)}, False) ∧ ∃ σ1, + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ ∃ σ' n, + ((), σ') ∈ (Fl4SpecA64.performPageTableInvocation (Z := Unit) iv σ1).1 ∧ + VsRelA Z.Pp (insert ptr Z.S1) (Function.update Z.Lv ptr n) Z.X Z.Y (L.mem s') (L.ksb s') σ' ∧ + 1 ≤ n ∧ ∃ asid va, MapJA (L := L) AR Z OS m s (ptr ∉ S2PgE Z.X Z.T2 ∧ ptr ∉ Z.Y) + { Z with S1 := insert ptr Z.S1, Lv := Function.update Z.Lv ptr n } s' a σ' sl (.PageTable ptr true asid va) + +theorem PtMapPostGA.weakA {AR : A → HwRootA → Prop} {Z : XSetsA} {OS : Set U64} {sl : U64} {m : Fl4SpecA64.McState} + {ptr : U64} {σ : Fl4SpecA64.State Unit} + {D : Set (Except Fl4SpecA64.SyscallError Fl4SpecA64.ArchInvocation × Fl4SpecA64.State Unit) × Prop} + {s : S} {a : A} : ∀ {res}, PtMapPostGA (L := L) AR Z OS sl m ptr σ D s a res → PtMapPostA (L := L) Z ptr σ D s a res + | (.Err _, _, _), h => h + | (.Ok (), _, _), ⟨h1, iv, h2, σ1, h3, σ', n, h4, h5, _⟩ => ⟨h1, iv, h2, σ1, h3, σ', n, h4, h5⟩ + +theorem extra0_eqA {ex : Aeneas.Std.Array obj.Slot 3#usize} {x0 : U64} + (hx0 : Aeneas.Std.Array.index_usize ex 0#usize = ok x0) : + vspace.extra ex 0#usize = ok (if x0 = 0#u64 then none else some x0) := by + unfold vspace.extra + split + · rename_i h; exfalso; simp at h <;> scalar_tac + · rw [hx0, bind_ok] + simp only [obj.Slot.is_null, bind_ok] + by_cases h : x0 = 0#u64 <;> simp [h] + +/-- A related cap that is no mapped `Pml4`, `Ept` or `HighVSpace` cap is no mapped l4v VSpace-root cap. -/ +theorem absCap_noRootA {vc : capability.Cap} (hr : ∀ r asid, vc ≠ .Pml4 r true asid) + (hE : ∀ p l mp asid va, vc ≠ .Ept p l mp asid va) (hH : ∀ p mp asid, vc ≠ .HighVSpace p mp asid) : + ∀ pt asid x, absCapA vc ≠ .archObjectCap (.pageTableCap pt .vsRootPtT (some (asid, x))) := by + intro pt asid x h + cases vc with + | Pml4 r m a => + cases m + · simp [absCapA, mapDataA] at h + · exact hr r a rfl + | Frame p sz r dev m ept asid va => + simp only [absCapA, absFrameE] at h + split at h <;> cases h + | Ept p l mp asid va => exact hE p l mp asid va rfl + | HighVSpace p mp asid => exact hH p mp asid rfl + | _ => simp [absCapA] at h + +/-- FL4's root tests on a cap that is no mapped `Pml4`, `Ept` or `HighVSpace` cap: not a high window, not a valid +root. -/ +theorem noRoot_testsA {vc : capability.Cap} (hr : ∀ r asid, vc ≠ .Pml4 r true asid) + (hE : ∀ p l mp asid va, vc ≠ .Ept p l mp asid va) : + vspace.is_valid_vtable_root vc = ok false := by + cases vc with + | Pml4 r m a => + cases m + · rfl + · exact absurd rfl (hr r a) + | Ept p l mp asid va => exact absurd rfl (hE p l mp asid va) + | _ => rfl + +/-- `(w & 1) != 0` is the valid bit. -/ +theorem valid_bitA (w : U64) : ((w &&& 1#u64) != 0#u64) = dBitA w 0 := by + rw [dBitA_eq] + have h1 : (w &&& 1#u64).val = w.val % 2 := and_maskA w 1 _ rfl + have z : (0#u64 : U64).val = 0 := rfl + by_cases h : w.val % 2 = 1 + · have hn : (w &&& 1#u64) ≠ 0#u64 := fun e => by rw [e] at h1; omega + simp [hn, h] + · have hz : (w &&& 1#u64) = 0#u64 := UScalar.eq_of_val_eq (by rw [h1, z]; omega) + simp [hz, h] + +/-- A related descriptor is `InvalidPTE` exactly when its valid bit is clear. -/ +theorem pteRel_invA {Tb : Set U64} {Lv : U64 → Nat} {k : Nat} {w : U64} {pte : Fl4SpecA64.PTE} + (h : PteRelA Tb Lv k w pte) : pte = .invalidPTE ↔ dBitA w 0 = false := by + cases pte with + | invalidPTE => exact ⟨fun _ => h, fun _ => rfl⟩ + | pageTablePTE _ => exact ⟨fun e => (by cases e), fun e => (by rw [h.2.1] at e; cases e)⟩ + | pagePTE _ _ _ _ => exact ⟨fun e => (by cases e), fun e => (by rw [h.2.2.1] at e; cases e)⟩ + +/-- `2^sh - 1` as FL4 computes it (`wrapping_sub (1 << sh) 1`). -/ +theorem ws1A (sh : U64) (hs : sh.val < 64) : + core.num.U64.wrapping_sub ⟨(1#u64 : U64).bv <<< sh.val⟩ 1#u64 = ⟨BitVec.ofNat 64 (2 ^ sh.val - 1)⟩ := by + have hp : 2 ^ sh.val < 2 ^ 64 := Nat.pow_lt_pow_right (by decide) hs + have hp1 : 1 ≤ 2 ^ sh.val := Nat.one_le_two_pow + have hx : (⟨(1#u64 : U64).bv <<< sh.val⟩ : U64).val = 2 ^ sh.val := by + show ((1#u64 : U64).bv <<< sh.val).toNat = _ + rw [BitVec.toNat_shiftLeft, Nat.shiftLeft_eq, show (1#u64 : U64).bv.toNat = 1 from rfl, Nat.one_mul] + exact Nat.mod_eq_of_lt hp + apply UScalar.eq_of_val_eq + generalize (⟨(1#u64 : U64).bv <<< sh.val⟩ : U64) = x at hx + show _ = (BitVec.ofNat 64 (2 ^ sh.val - 1)).toNat + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)] + simp [core.num.U64.wrapping_sub_val_eq, U64.size_def, U64.numBits, hx] + omega + +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +set_option maxHeartbeats 4000000 in +/-- **Stage-1 `ARMPageTableMap` refines l4v** (module docstring; x86 `pt_map_rel`). -/ +theorem pt_map_refinesGA (ar : machine.Arch A) (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) + (cte : Fl4SpecA64.CslotPtr) (hab : AbsSlotA Z.th cte) (hos : cslotAddrA cte ∈ OS) + {ptr asid0 va0 : U64} {mp : Bool} (hcap : capAtA (L.mem s) (cslotAddrA cte) = .PageTable ptr mp asid0 va0) + (UP : UnmPtA Z.S1 Z.Y OS H.tcbs (L.mem s) σ ptr) + (slbl : Fl4SpecA64.Data) (hl : Fl4SpecA64.invocationType slbl = .archInvocationLabel .armPageTableMap) + (length buf : U64) (args : List U64) (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) + (x0 : U64) (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) + (exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) (hex : ExtraRelA Z.th (L.mem s) x0 (exs.map Prod.fst)) + (hS1 : ∀ p l mp' asid va, capAtA (L.mem s) x0 ≠ .Ept p l mp' asid va) + (hHi : ∀ p mp' asid, capAtA (L.mem s) x0 ≠ .HighVSpace p mp' asid) : + vspace.decode_page_table st ar s a fl4_abi.label.ARMPageTableMap length (cslotAddrA cte) + (.PageTable ptr mp asid0 va0) buf extra ⦃ res => PtMapPostGA (L := L) AR Z OS (cslotAddrA cte) m ptr σ + ((Fl4SpecA64.decodePageTableInvocation (Z := Unit) slbl (args.map UScalar.bv) cte + (.pageTableCap (specPtr ptr) .normalPtT (mapDataA mp asid0 va0)) exs).run σ) s a res ⦄ := by + rw [ptInv_mapA hl] + have hct : sched.cur_thread st s = ok (L.curW s) := cur_thread_ok s + have hne : ¬ fl4_abi.label.ARMPageTableMap = fl4_abi.label.ARMPageTableUnmap := by + unfold fl4_abi.label.ARMPageTableMap fl4_abi.label.ARMPageTableUnmap; decide + unfold vspace.decode_page_table + simp only [hct, bind_ok, hne, ↓reduceIte, bne_self_eq_false, Bool.false_eq_true, extra0_eqA hx0] + rcases hex with ⟨rfl, hnil⟩ | ⟨hx0n, -, vc', vrest, hvc', hmap⟩ + · -- no extra cap + have he : exs = [] := List.map_eq_nil_iff.1 hnil + subst he + rw [if_pos rfl] + simp only [error.truncated, bind_ok, WP.spec_ok] + exact ⟨rfl, rfl, .truncatedMessage, ptDec_truncA _ _ _ _ _ _ _ σ (fun ⟨_, h⟩ => by simp at h), Or.inl trivial⟩ + rw [if_neg hx0n] + obtain ⟨⟨vc'', xsl⟩, rest, rfl⟩ : ∃ e rest, exs = e :: rest := by + cases exs with + | nil => simp at hmap + | cons e rest => exact ⟨e, rest, rfl⟩ + simp only [List.map_cons, List.cons.injEq] at hmap + obtain ⟨rfl, -⟩ := hmap + dsimp only + by_cases h2 : length >= 2#u64 + swap + · rw [if_neg h2] + simp only [error.truncated, bind_ok, WP.spec_ok] + refine ⟨rfl, rfl, .truncatedMessage, ptDec_truncA _ _ _ _ _ _ _ σ (fun ⟨h, _⟩ => h2 ?_), Or.inl trivial⟩ + simp only [List.length_map] at h + scalar_tac + rw [if_pos h2] + have h2' : 2 ≤ args.length := by + have : 2 ≤ length.val := by scalar_tac + omega + obtain ⟨a0, a1, r', rfl⟩ : ∃ a0 a1 r', args = a0 :: a1 :: r' := by + match args, h2' with + | a0 :: a1 :: r', _ => exact ⟨_, _, _, rfl⟩ + simp only [List.map_cons] + cases mp + swap + · -- a mapped page-table cap + simp only [↓reduceIte, error.invalid_cap, bind_ok, WP.spec_ok] + exact ⟨rfl, rfl, .invalidCapability 0, ptDec_mappedA _ _ _ _ _ _ _ σ (by simp) (by simp [mapDataA]), + Or.inr ⟨0#u64, rfl, rfl⟩⟩ + rw [show mapDataA false asid0 va0 = none from rfl] + simp only [Bool.false_eq_true, ↓reduceIte, hargs 0#u64 a0 (by simp), capA_eq, bind_ok] + simp only [capAtA_eq] at hvc' hS1 hHi + generalize capA st s x0 = vc at hvc' hS1 hHi ⊢ + have nonroot : ∀ (vc : capability.Cap), CapRelA vc vc'' → (∀ r asid, vc ≠ .Pml4 r true asid) → + (∀ p l mp asid va, vc ≠ .Ept p l mp asid va) → (∀ p mp asid, vc ≠ .HighVSpace p mp asid) → + ∃ e', (Fl4SpecA64.decodePtInvMap (Z := Unit) slbl (a0.bv :: a1.bv :: List.map UScalar.bv r') cte + (.pageTableCap (specPtr ptr) .normalPtT none) ((vc'', xsl) :: rest)).run σ = ({(.error e', σ)}, False) ∧ + VsErrRelA (.Sys (.InvalidCapability 1#u64)) e' := fun vc hr h1 h2 h3 => + ⟨.invalidCapability 1, ptDec_noRootA _ _ _ _ _ _ _ _ _ _ σ (by + cases vc with + | PageTable p m asid va => + cases m + · rw [← hr.2]; exact absCap_noRootA h1 h2 h3 + · obtain ⟨_, _, rfl, _, _⟩ := CapEqA.pt_mapped_iff.mp hr.2; intro _ _ _ h; cases h + | _ => rw [← hr.2]; exact absCap_noRootA h1 h2 h3), + Or.inr ⟨1#u64, rfl, rfl⟩⟩ + cases vc with + | Ept p l mp' asid va => exact absurd rfl (hS1 p l mp' asid va) + | HighVSpace p mp' asid => exact absurd rfl (hHi p mp' asid) + | Pml4 r0 mr asid1 => + cases mr + · simp only [bind_ok, Bool.false_eq_true, ↓reduceIte, vspace.is_valid_vtable_root, error.invalid_cap, + WP.spec_ok] + obtain ⟨e', h1, h2⟩ := nonroot _ hvc' (by simp) (by simp) (by simp) + exact ⟨rfl, rfl, e', h1, h2⟩ + -- the main path: a mapped Stage-1 root cap + have hwf : asid1.val < 2 ^ 16 := hvc'.1 + have hvc'' : vc'' = .archObjectCap (.pageTableCap (specPtr r0) .vsRootPtT + (some (asidOfA asid1, (0#u64 : U64).bv))) := hvc'.2.symm + subst hvc'' + simp only [bind_ok, Bool.false_eq_true, ↓reduceIte, vspace.is_valid_vtable_root, eq_self] + by_cases htop : a0 > fl4_abi.consts.USER_TOP + · rw [if_pos htop] + simp only [error.invalid_arg, bind_ok, WP.spec_ok] + exact ⟨rfl, rfl, .invalidArgument 0, ptDec_topA _ _ _ _ _ _ _ _ _ _ _ _ σ ((top_iffA a0).2 htop), Or.inl rfl⟩ + rw [if_neg htop, ptDec_bodyA _ _ _ _ _ _ _ _ _ _ _ _ σ (fun h => htop ((top_iffA a0).1 h))] + have h16 : asid1.val < 65536 := lt_of_lt_of_eq hwf (by norm_num) + refine WP.spec_bind (check_vspaceA E.j.1.vs E.j.2.2.1.obj (r := r0) h16 (specPtr ptr) .normalPtT a0.bv cte) + fun res hres => ?_ + rcases res with ⟨rr, aa⟩ | e + swap + · obtain ⟨e', he', hrel⟩ := hres + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame.from, + bind_tc_ok, WP.spec_ok] + exact ⟨rfl, rfl, e', he', hrel⟩ + obtain ⟨he, hrT, hrL, hrun', hvRoot⟩ := hres + simp only [Prod.mk.injEq] at he + obtain ⟨he1, he2⟩ := he + subst rr + subst aa + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok] + rw [hrun'] + have R := E.j.1.vs + obtain ⟨hKal, hS1A, -⟩ := E.vsLeafA + refine WP.spec_bind (lookup_pt_slotA R hrT hrL (vsReachA_of_vspaceForAsid R E.j.2.2.1.obj hrT hvRoot) a0) + fun lu ⟨p, hp, l, hl3, hsh, hlv, hsl, _, hLs, hreachP⟩ => ?_ + obtain ⟨tq, hkq, htq, hrq⟩ := R.pt p hp + have hidx := idxA_lt a0 lu.shift + have hpte := ptesOf_atA hkq (ptT := Fl4SpecA64.levelType (lvA l)) + (by rw [htq, levelType_lvA hl3]; congr 1; omega) (R.al_mod hp) hidx + rw [← hsl] at hpte + rw [ptRest_runA hLs (getPte_runA hpte)] + have hpre : PteRelA Z.S1 Z.Lv (Z.Lv p) (L.mem s lu.slot) (Fl4SpecA64.ptApply tq (idxA a0 lu.shift).bv) := by + rw [hsl]; exact hrq hreachP _ hidx + have e12 : vspace.PAGE_BITS = 12#u64 := by unfold vspace.PAGE_BITS; rfl + have v12 : (12#u64 : U64).val = 12 := rfl + by_cases hl0 : l = 0 + · subst hl0 + have hpb : lu.shift = vspace.PAGE_BITS := by rw [e12]; exact UScalar.eq_of_val_eq (by rw [hsh, v12]) + rw [if_pos hpb, if_pos (Or.inl (by rw [ptBitsLeft_lvA (by omega)]; rfl))] + exact (WP.spec_ok _).2 ⟨rfl, rfl, .deleteFirst, rfl, Or.inl trivial⟩ + have hpb : ¬ lu.shift = vspace.PAGE_BITS := fun e => hl0 (by + have := congrArg UScalar.val e; rw [e12, hsh, v12] at this; omega) + rw [if_neg hpb, L.read_eq, bind_ok, eq_of_spec_fd P0.Consts.vspace.D_VALID.spec, bind_ok] + simp only [lift, bind_ok, valid_bitA] + have hb0 : ¬ Fl4SpecA64.ptBitsLeft (lvA l) = Fl4SpecA64.pageBits := by + rw [ptBitsLeft_lvA hl3]; unfold Fl4SpecA64.pageBits; omega + by_cases hv : dBitA (L.mem s lu.slot) 0 = true + · rw [if_pos hv, if_pos (Or.inr fun e => by rw [(pteRel_invA hpre).1 e] at hv; cases hv)] + exact (WP.spec_ok _).2 ⟨rfl, rfl, .deleteFirst, rfl, Or.inl trivial⟩ + have hv' : dBitA (L.mem s lu.slot) 0 = false := by simpa using hv + rw [if_neg hv, if_neg (by rintro (h | h); exact hb0 h; exact h ((pteRel_invA hpre).2 hv'))] + -- the base address + have hs64 : lu.shift.val < 64 := by omega + have e63 : (lu.shift &&& 63#u64) = lu.shift := + UScalar.eq_of_val_eq (by + rw [and_maskA lu.shift 6 _ rfl]; exact Nat.mod_eq_of_lt (by simp only [Nat.reducePow]; omega)) + rw [e63, CapRt.shl_u64_eq 1#u64 lu.shift hs64, bind_ok] + try simp only [lift, bind_ok] + rw [ws1A lu.shift hs64] + generalize hbase : a0 &&& ~~~(⟨BitVec.ofNat 64 (2 ^ lu.shift.val - 1)⟩ : U64) = base + have hbv : base.bv = a0.bv &&& ~~~(Fl4SpecA64.mask (Fl4SpecA64.ptBitsLeft (lvA l))) := by + rw [← hbase, ptBitsLeft_lvA hl3, mask_lvA (by omega) hl3, ← hsh]; rfl + have hU : fl4_abi.consts.USER_TOP.val = 140737488355327 := by unfold fl4_abi.consts.USER_TOP; rfl + have ha0 : a0.val ≤ 140737488355327 := by + have : ¬ fl4_abi.consts.USER_TOP.val < a0.val := htop + omega + have hbl : base.val < 2 ^ 47 := by + rw [← hbase, UScalar.val_and]; exact lt_of_le_of_lt Nat.and_le_left (by omega) + -- the restart and the cap write + have hcanon : CapRt.Canonical ptr := by + have h : CapRt.EncWf (capAtA (L.mem s) (cslotAddrA cte)) := CapRt.decodeF_encWf _ _ + rw [hcap] at h + simp only [CapRt.EncWf] at h + exact h.1 + have hEnc : CapRt.EncWf (.PageTable ptr true asid1 base) := by + simp only [CapRt.EncWf]; exact ⟨hcanon, by omega, canon_ltA hbl⟩ + have hca : CapRelA (.PageTable ptr true asid1 base) (.archObjectCap (.pageTableCap (specPtr ptr) + .normalPtT (some (asidOfA asid1, a0.bv &&& ~~~(Fl4SpecA64.mask (Fl4SpecA64.ptBitsLeft (lvA l))))))) := by + exact ⟨hwf, rfl, rfl, Or.inl hbv⟩ + obtain ⟨xc, s2, σ2, hkx, hst, hset, hsc, hσ2, HS, hksb, R2, hcpu2, hw2, hJ2, hFr2⟩ := + restart_setcapJA E RS cte hab hos hEnc hca + rw [hst, bind_ok, hsc, bind_ok] + -- the new table at the written states + have hpal : ptr.val % 4096 = 0 := Nat.mod_eq_zero_of_dvd UP.al + have hcurT : L.curW s ∈ H.tcbs := E.sq.curT + have hta := E.sq.tcbs _ hcurT + have sA : Aligned (stA (L.curW s)) := by rw [stA_eq]; exact field_aligned hta (by simp) (by simp) + have hmem2 : ∀ i : U64, i.val < 512 → L.mem s2 (peAF ptr i) = L.mem s (peAF ptr i) := by + intro i hi + have ha := peAF_alignedA hpal hi + have hT := UP.tcb i hi + have hnc : ¬ W7.CapW H.tcbs {b | InOS OS b} ∅ (peAF ptr i) := by + rintro (⟨t, ht, K, hK, he⟩ | ⟨hin, -⟩) + · have hfit := (E.sq.tcbs t ht).fits + simp only [Nat.reducePow] at hfit + have := congrArg UScalar.val he + rw [wa_valm] at this + rcases hT t ht with h | h <;> omega + · exact UP.os i hi hin + have hns : stA (L.curW s) ≠ peAF ptr i := fun e => by + have := congrArg UScalar.val e + have e3 : (3328#u64 : U64).val = 3328 := rfl + have hfit := hta.fits + simp only [Nat.reducePow] at hfit + rw [stA_eq, wa_valm, e3] at this + rcases hT _ hcurT with h | h <;> omega + rw [hw2.2.2 _ ha hnc, mem_wr_ne' sA ha hns] + obtain ⟨tp, hkp, htp, hep⟩ := UP.spec + have hkp1 : (stUpdA σ σ.curThread xc .restart).kheap (specPtr ptr) = some (.archObj (.pageTable tp)) := + stUpd_keep hkx hkp (fun _ h => by cases h) + have hkp2 := HS.keep hkp1 (fun _ _ h => by cases h) (fun _ h => by cases h) + have hpn : p ≠ ptr := fun e => UP.nt (by rw [← e]; exact hp) + have hn3 : Z.Lv p + 1 ≤ 3 := by omega + have R3 := R2.addPtA UP.nt UP.al UP.ytb (fun i hi => by rw [hmem2 i hi]; exact UP.inv i hi) hkp2 htp hep + (n := Z.Lv p + 1) (by omega) hn3 + have hLp : Function.update Z.Lv ptr (Z.Lv p + 1) p = Z.Lv p := Function.update_of_ne hpn _ _ + -- `make_user_table` and the descriptor write + rw [show layout.paddr ptr = ok (core.num.U64.wrapping_sub ptr layout.PPTR_BASE) from rfl, bind_ok] + have hpa := UP.pa + refine WP.spec_bind (s1tab_v _ (by rw [ws_valA]; omega) (by omega)) fun v ⟨hv3, hvm⟩ => ?_ + have hwr := pteRel_mutA (Tb := insert ptr Z.S1) (Lv := Function.update Z.Lv ptr (Z.Lv p + 1)) (k := Z.Lv p) + (by omega) hv3 hvm hpa (Set.mem_insert _ _) (by rw [Function.update_self]) + have hwr' : PteRelA (insert ptr Z.S1) (Function.update Z.Lv ptr (Z.Lv p + 1)) + (Function.update Z.Lv ptr (Z.Lv p + 1) p) v (.pageTablePTE (Fl4SpecA64.ppnFromPptr (specPtr ptr))) := by + rw [hLp]; exact hwr + have hA : ¬ AsW (L.ksb s2) (peAF p (idxA a0 lu.shift)) := by rw [hksb]; exact hS1A p hp _ hidx + have hK2 : Aligned (L.ksb s2) := by rw [hksb]; exact hKal + have hdTab : dTabA v = ptr := by + have hd : dAddrA v = core.num.U64.wrapping_sub ptr layout.PPTR_BASE := by + rw [← hvm]; unfold vspace.ADDR_MASK dAddrA; rfl + unfold dTabA; rw [hd, ← pptrBase_litA]; exact wa_ws ptr layout.PPTR_BASE + obtain ⟨hwrite, σ3, hstore, R4, -⟩ := write_storePteA R3 (Set.mem_insert_of_mem _ hp) hidx hwr' hA hK2 + (fun _ _ => ⟨by rw [hdTab]; exact hpn.symm, fun j hj => ⟨by rw [hdTab, hmem2 j hj]; exact UP.inv j hj, + fun t0 ht0 => by + rw [hdTab, hkp2] at ht0 + cases ht0 + exact hep j hj⟩⟩) + rw [hsl, hwrite, bind_ok] + obtain ⟨-, ⟨⟨⟨⟩, σ4⟩, h4⟩, h4m⟩ := doMachineOp_okA (mop := Fl4SpecA64.cleanByVA_PoU + (specPtr (peAF p (idxA a0 lu.shift))) (Fl4SpecA64.addrFromPPtr (specPtr (peAF p (idxA a0 lu.shift))))) + (machineOpLift_okA _) σ3 + have hms : MsA σ3 σ4 := h4m _ h4 + have hMJ : MapJA (L := L) AR Z OS m s (ptr ∉ S2PgE Z.X Z.T2 ∧ ptr ∉ Z.Y) + { Z with S1 := insert ptr Z.S1, Lv := Function.update Z.Lv ptr (Z.Lv p + 1) } + (L.wr s2 (peAF p (idxA a0 lu.shift)) v) a σ4 (cslotAddrA cte) (.PageTable ptr true asid1 base) := by + intro hT hnul hTO hc + have hco : capA st s (cslotAddrA cte) = .PageTable ptr false asid0 va0 := by rw [← capAtA_eq, hcap] + obtain ⟨J2, hT2⟩ := hJ2 hT hnul (by simp) (by rw [hco]; simp) + (UA.of_shapeA (by rw [hco]; rfl) fun h => by rw [hco] at h; simp [isUtA] at h) + ⟨fun _ h => (by cases h), fun _ _ _ _ _ h => (by cases h), fun _ _ _ h => (by cases h), fun _ _ _ h => (by cases h), trivial⟩ + obtain ⟨t', rfl⟩ := storePte_eqA R3 (Set.mem_insert_of_mem _ hp) hidx hstore + exact map_finJA E J2 hT2 hksb hcpu2 hp (hTO p hp) hidx (L.ksb_wr _ _ _) (L.cpu_wr _ _ _) + (fun w hw hne => mem_wr_ne' (peAF_alignedA (R.al_mod hp) hidx) hw (Ne.symm hne)) hms + (fun q hq => (Set.mem_insert_iff.1 hq).elim (fun e => by subst e; exact Or.inr hc) Or.inl) + (fun q hq hl => by + have hne : q ≠ ptr := fun e => UP.nt (e ▸ hq) + exact ⟨Set.mem_insert_of_mem _ hq, by rw [Function.update_of_ne hne]; exact hl⟩) + (R4.of_msA hms) hFr2 hos + refine (WP.spec_ok _).2 ⟨rfl, _, rfl, _, hset, σ4, Z.Lv p + 1, ?_, R4.of_msA hms, by omega, asid1, base, hMJ⟩ + simp only [Fl4SpecA64.performPageTableInvocation, Fl4SpecA64.performPtInvMap, nd_bind_applyA, + Set.mem_setOf_eq] + refine ⟨((), _), hσ2, ?_⟩ + dsimp only + have hlt : Fl4SpecA64.levelType (lvA l) = ptTyA (Function.update Z.Lv ptr (Z.Lv p + 1) p) := by + rw [levelType_lvA hl3, hLp]; congr 1; omega + rw [hlt, hstore] + exact ⟨((), σ3), Set.mem_singleton _, h4⟩ + | _ => + simp only [bind_ok, Bool.false_eq_true, ↓reduceIte, vspace.is_valid_vtable_root, error.invalid_cap, WP.spec_ok] + obtain ⟨e', h1, h2⟩ := nonroot _ hvc' (by simp) (by simp) (by simp) + exact ⟨rfl, rfl, e', h1, h2⟩ + +/-- **Stage-1 `ARMPageTableMap` refines l4v** (module docstring; x86 `pt_map_rel`): `pt_map_refinesGA`'s post +without the lift's obligation. -/ +theorem pt_map_refinesA (ar : machine.Arch A) (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + {Rp Nt : Set U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} {sp : U64} + (P : SysPlace st H Z.Cs H.frame Rp Nt OS s extra sp) (Y : SymL st H Z.Cs s) + (hrun : Runnable (L.mem s (stA (L.curW s)))) + (hcur : (L.mem s (inqA (L.curW s)) &&& sched.Q_RELEASE) = 0#u64 ∧ L.mem s (scA (L.curW s)) ≠ 0#u64 ∧ + L.mem s (scA (L.curW s)) ∈ Ac) + (hvs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → Aligned b ∧ b ≠ stA (L.curW s)) + (hvc : ∀ v ∈ Z.V, stA (L.curW s) ≠ v ∧ stA (L.curW s) ≠ wa v 24#u64) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (cte : Fl4SpecA64.CslotPtr) (hab : AbsSlotA Z.th cte) (hos : cslotAddrA cte ∈ OS) + {ptr asid0 va0 : U64} {mp : Bool} (hcap : capAtA (L.mem s) (cslotAddrA cte) = .PageTable ptr mp asid0 va0) + (UP : UnmPtA Z.S1 Z.Y OS H.tcbs (L.mem s) σ ptr) + (slbl : Fl4SpecA64.Data) (hl : Fl4SpecA64.invocationType slbl = .archInvocationLabel .armPageTableMap) + (length buf : U64) (args : List U64) (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) + (x0 : U64) (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) + (exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) (hex : ExtraRelA Z.th (L.mem s) x0 (exs.map Prod.fst)) + (hS1 : ∀ p l mp' asid va, capAtA (L.mem s) x0 ≠ .Ept p l mp' asid va) + (hHi : ∀ p mp' asid, capAtA (L.mem s) x0 ≠ .HighVSpace p mp' asid) : + vspace.decode_page_table st ar s a fl4_abi.label.ARMPageTableMap length (cslotAddrA cte) + (.PageTable ptr mp asid0 va0) buf extra ⦃ res => PtMapPostA (L := L) Z ptr σ + ((Fl4SpecA64.decodePageTableInvocation (Z := Unit) slbl (args.map UScalar.bv) cte + (.pageTableCap (specPtr ptr) .normalPtT (mapDataA mp asid0 va0)) exs).run σ) s a res ⦄ := + WP.spec_mono (pt_map_refinesGA ar E ⟨⟨_, _, _, _, P⟩, Y, hrun, hcur, hvs, hvc, hsub⟩ cte hab hos hcap UP slbl hl + length buf args hlen hargs x0 hx0 exs hex hS1 hHi) fun _ h => PtMapPostGA.weakA h + +end top + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/VsMapJA.lean b/proofs/Fl4ProofsA64/P3/VsMapJA.lean new file mode 100644 index 000000000..874caeb90 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/VsMapJA.lean @@ -0,0 +1,242 @@ +/- +P3 (aarch64, A64-P3 S3 gen 21, ruling on gen 20's items (a)–(d)): **the `JAtZAX` frame of the Stage-1 maps** — the +pieces the `JAtZAX` lifts of `ARMPageTableMap` (`P3/VsMapA`, `pt_map_refinesA`) and `ARMPageMap` (`P3/VsMapPageA`, +`page_map_refinesA`) are built from. The x86 counterparts are `XTopJointVsMap`'s `JAtZ.vsStepJ` and `VsArch`'s +`ArchRefines`. + +* **`ArchKeepsA ar AR`** **[P]** (ruling (d); x86 `ArchRefines`' flush half): FL4's AArch64 backend's + `invalidate_page`, `invalidate_asid` and `cache_op` (S3 gen 26, ruling Q4: and the Ext unmaps' flushes + `invalidate_stage2_page`, `invalidate_stage2_vmid`, `invalidate_high_page`, `invalidate_high_vspace`) never fail + and keep the arch relation `AR` at the same hardware root (they flush TLBs and caches only; the root is changed + only by `set_vspace`). A trusted-backend obligation, a + property of `(ar, AR)` alone: it reads no kernel state, so the boot witnesses `w0` / `w1` need nothing for it beyond + the boot arch relation `hAR` they already take. **`FlA.arA`**: under it, a page map's flush chain (`FlA`, + `P3/VsMapPageA`) ends in an arch state related at the same root. +* **`TabOffA Z m s p`** (decode-site hypothesis, ruling (b)): the 512 words of the table `p` are no word of the relation's + footprint outside the Stage-1 tables (`FootA` at `{Z with S1 := ∅}`: slots, nodes, TCB / SC / VCPU fields, IRQ and + global words, ASID table, pools, windows, Stage-2 pages), and the spec name `specPtr p` is no related object's + name outside the Stage-1 tables (`RelPA` at the same ghosts). Not derivable from `JEntA` today: A64 P1's + `HeapKsOff` allows the `other` objects holding the tables (`PtHA`) inside the kernel-state block's static region + that holds the IRQ-state and node words, and `th`'s `other` objects have no spec object. **At `w0`** it is vacuous + for every Stage-1 table (`tabOff_w0A`: `bootZA`'s `S1` is empty). +* **`ExtVsRelA.of_tabWriteA`**: the Stage-2 / window relation across a write of one word of a table `p` outside the + Stage-2 pages, with the spec changing only `specPtr p`, at new Stage-1 tables `S1'` whose new members are neither + Stage-2 pages nor windows (ruling (c): taken here, `UnmPtA` unchanged). +* **`JAtZAX.of_tabWriteA`**: `JAtZAX` across such a write at the new ghosts `{Z with S1 := S1', Lv := Lv'}`, given + `VsRelA` and `ExtVsRelA` there: every other component is carried by `SameA` / `SpecSameA` at `{Z with S1 := ∅}`. + +(Gen 22: `TabOffA`, `ExtVsRelA.of_tabWriteA` and `JAtZAX.of_tabWriteA` moved to `P3/VsTabJA`, unchanged, so the map +proofs can use them.) + +**The lifts** (gen 22, ruling 20:56Z item 2; x86 `JAtZ.vsStepJ` on `pt_map_rel` / `frame_map_rel`): +* **`pt_map_jA`**: `pt_map_refinesGA` read under the call-site tree at `OS` and the null non-relation slots (`hT` / + `hnul`, as `decode_copy_relA`), `TabOffA` for every Stage-1 table, and ruling (c)'s `ptr ∉ S2PgE Z.X Z.T2 ∧ ptr ∉ + Z.Y`: the outcome (`PtMapJPostA`) is the spec's error with nothing changed, or the restart and + `perform_page_table_invocation` ending in `JAtZAX` at `{Z with S1 := insert ptr Z.S1, Lv := …}` with the tree. +* **`page_map_jA`**: `page_map_refinesGA` under `ArchKeepsA` and the same call-site facts: the outcome (`PgMapJA`) + is the spec's error, or the restart and `perform_page_invocation` with FL4's flush chain ending in `(Ok, s', a')`, + `AR a' Z.hw` (`FlA.arA`) and `JAtZAX` at the same ghosts with the tree. + +**Gen 30** (ruling 01:13Z item 1): `MapJA` (`P3/VsTabJA`) carries the slot frame `SlotFrA` of the invoked slot, so +the lifts project the carried invariants: **`carry_ptMapA`** / **`carry_pgMapA`** (`CarryA.of_slotFrA`: the other +slots' caps kept, the new cap `CapFinEqA` to the old one, a mapped table cap naming `ptr` at the new level `n ≥ 1`; +the spec side `restart_tyA` and `perfPt_tyA` / `perfPg_tyA`). **Changed statements (forced):** `PtMapJPostA` and +`PgMapJA` add `CarryA Z (L.mem s) σ → CarryA Z' (L.mem s') σ'`; `PtMapPostGA.liftA` / `PgMapRelGA.liftA` take `hsub` +and the invoked cap `hcap`; the statements of `pt_map_jA` / `page_map_jA` are otherwise unchanged. + +Named premise: `ArchKeepsA` **[P]**. Decode-site hypothesis: `TabOffA`. +-/ +import Fl4ProofsA64.P3.VsMapPageA +import Fl4ProofsA64.P3.JBootA +import Fl4ProofsA64.P3.CarryA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## The arch backend -/ + +/-- **[P] The AArch64 arch backend agrees with the platform and its flushes keep the arch relation**. -/ +structure ArchKeepsA {A : Type} (ar : machine.Arch A) (AR : A → HwRootA → Prop) : Prop where + invPage : ∀ a h (asid va : U64), AR a h → ar.invalidate_page a asid va ⦃ a' => AR a' h ⦄ + invAsid : ∀ a h (asid : U64), AR a h → ar.invalidate_asid a asid ⦃ a' => AR a' h ⦄ + cacheOp : ∀ a h (op root asid va n pa : U64), AR a h → ar.cache_op a op root asid va n pa ⦃ a' => AR a' h ⦄ + /-- (S3 gen 26, ruling Q4) The Ext unmaps' flushes (`P3/ExtVsTopA`'s `FlushE`): Stage-2 TLB maintenance by IPA + and by VMID, the high window's by address and whole. -/ + invS2Page : ∀ a h (asid va : U64), AR a h → ar.invalidate_stage2_page a asid va ⦃ a' => AR a' h ⦄ + invS2Vmid : ∀ a h (asid : U64), AR a h → ar.invalidate_stage2_vmid a asid ⦃ a' => AR a' h ⦄ + invHiPage : ∀ a h (va : U64), AR a h → ar.invalidate_high_page a va ⦃ a' => AR a' h ⦄ + invHiVs : ∀ a h (y : U64), AR a h → ar.invalidate_high_vspace a y ⦃ a' => AR a' h ⦄ + /-- **[P]** `hw_access_flag` agrees with the spec's platform value from + `ID_AA64MMFR1_EL1.HAFDBS` (fl4-arch-aarch64/cpu.rs and arch.rs). -/ + hwAccessFlag : ∀ a, ar.hw_access_flag a = ok Fl4SpecA64.fl4A64Platform.hwAccessFlag + +/-- **A flush chain keeps the arch relation** (module docstring). -/ +theorem FlA.arA {A B : Type} {ar : machine.Arch A} {AR : A → HwRootA → Prop} (K : ArchKeepsA ar AR) + {P : A → B} {a : A} {r : Result B} (h : FlA ar P a r) {hw : HwRootA} (ha : AR a hw) : + r ⦃ x => ∃ a', x = P a' ∧ AR a' hw ⦄ := by + induction h with + | ret a => exact (WP.spec_ok _).2 ⟨a, rfl, ha⟩ + | inv a asid va k _ ih => exact WP.spec_bind (K.invPage a hw asid va ha) fun a' h' => ih a' h' + | cache a op root asid va n pa k _ ih => exact WP.spec_bind (K.cacheOp a hw op root asid va n pa ha) fun a' h' => ih a' h' + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-! ## The table's words at `w0` (`TabOffA`: `P3/VsTabJA`) -/ + +/-- At `w0` every Stage-1 table is off the footprint: there is none. -/ +theorem tabOff_w0A (kb ib : U64) (nc : Nat) (m : Fl4SpecA64.McState) (s : S) : + ∀ p ∈ (bootZA kb ib nc).S1, TabOffA (L := L) (bootZA kb ib nc) m s p := fun _ h => absurd h (Set.notMem_empty _) + + +/-! ## The lifts (gen 22, ruling 20:56Z item 2) -/ + +/-- **The page-table map's outcome with `JAtZAX`** (module docstring; x86 `pt_map_rel` lifted by `JAtZ.vsStepJ`). -/ +def PtMapJPostA (AR : A → HwRootA → Prop) (Z : XSetsA) (OS : Set U64) (m : Fl4SpecA64.McState) (ptr : U64) + (σ : Fl4SpecA64.State Unit) + (D : Set (Except Fl4SpecA64.SyscallError Fl4SpecA64.ArchInvocation × Fl4SpecA64.State Unit) × Prop) + (s : S) (a : A) : core.result.Result Unit error.Exc × S × A → Prop + | (.Err e, s', a') => s' = s ∧ a' = a ∧ ∃ e', D = ({(.error e', σ)}, False) ∧ VsErrRelA e e' + | (.Ok (), s', a') => a' = a ∧ ∃ iv, D = ({(.ok (.invokePageTable iv), σ)}, False) ∧ ∃ σ1, + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ ∃ σ' n, + ((), σ') ∈ (Fl4SpecA64.performPageTableInvocation (Z := Unit) iv σ1).1 ∧ + JAtZAX (L := L) AR { Z with S1 := insert ptr Z.S1, Lv := Function.update Z.Lv ptr n } s' a σ' m ∧ + TreeA st OS Z.G.chains Z.G.par s' ∧ + (CarryA Z (L.mem s) σ → CarryA { Z with S1 := insert ptr Z.S1, Lv := Function.update Z.Lv ptr n } (L.mem s') σ') + +/-- (gen 30) **`CarryA` across the page-table map** (module docstring). -/ +theorem carry_ptMapA {Z : XSetsA} {OS : Set U64} {sl ptr asid0 va0 asid va : U64} {mp : Bool} {n : Nat} {s s' : S} + {σ σ1 σ' : Fl4SpecA64.State Unit} {iv : Fl4SpecA64.PageTableInvocation} + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) (hT : TreeA st OS Z.G.chains Z.G.par s) + (hcap : capAtA (L.mem s) sl = .PageTable ptr mp asid0 va0) + (h3 : Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False)) + (h4 : ((), σ') ∈ (Fl4SpecA64.performPageTableInvocation (Z := Unit) iv σ1).1) (hn : 1 ≤ n) + (F : SlotFrA (L := L) OS sl (.PageTable ptr true asid va) s s') : + CarryA Z (L.mem s) σ → CarryA { Z with S1 := insert ptr Z.S1, Lv := Function.update Z.Lv ptr n } (L.mem s') σ' := + fun C => C.of_slotFrA hsub hT F rfl rfl + (fun p ⟨hp, hl⟩ => ⟨Set.mem_insert_of_mem _ hp, by + show 1 ≤ Function.update Z.Lv ptr n p + by_cases e : p = ptr + · rw [e, Function.update_self]; exact hn + · rw [Function.update_of_ne e]; exact hl⟩) + (fun _ h => h) + (fun p _ _ e => by + cases e + exact Or.inl ⟨Set.mem_insert _ _, by show 1 ≤ Function.update Z.Lv _ n _; rw [Function.update_self]; exact hn⟩) + (by rw [hcap]; exact CapFinEqA.pt2A _ _ _ _ _ _ _) ((restart_tyA h3).trans (perfPt_tyA iv σ1 _ h4)) + +theorem PtMapPostGA.liftA {AR : A → HwRootA → Prop} {Z : XSetsA} {OS : Set U64} {sl : U64} {m : Fl4SpecA64.McState} + {ptr : U64} {σ : Fl4SpecA64.State Unit} + {D : Set (Except Fl4SpecA64.SyscallError Fl4SpecA64.ArchInvocation × Fl4SpecA64.State Unit) × Prop} + {s : S} {a : A} (hT : TreeA st OS Z.G.chains Z.G.par s) + (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) (hTO : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s q) + (hc : ptr ∉ S2PgE Z.X Z.T2 ∧ ptr ∉ Z.Y) (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) {mp : Bool} {asid0 va0 : U64} + (hcap : capAtA (L.mem s) sl = .PageTable ptr mp asid0 va0) : + ∀ {res}, PtMapPostGA (L := L) AR Z OS sl m ptr σ D s a res → PtMapJPostA (L := L) AR Z OS m ptr σ D s a res + | (.Err _, _, _), h => h + | (.Ok (), _, _), ⟨h1, iv, h2, σ1, h3, σ', n, h4, _, hn, _, _, h6⟩ => + ⟨h1, iv, h2, σ1, h3, σ', n, h4, (h6 hT hnul hTO hc).1, (h6 hT hnul hTO hc).2.1, + carry_ptMapA hsub hT hcap h3 h4 hn (h6 hT hnul hTO hc).2.2.2.2⟩ + +/-- **The page map's outcome with `JAtZAX`** (module docstring; x86 `frame_map_rel` lifted by `JAtZ.vsStepJ`). -/ +def PgMapJA (ar : machine.Arch A) (AR : A → HwRootA → Prop) (Z : XSetsA) (OS : Set U64) + (m : Fl4SpecA64.McState) (σ : Fl4SpecA64.State Unit) + (D : Set (Except Fl4SpecA64.SyscallError Fl4SpecA64.ArchInvocation × Fl4SpecA64.State Unit) × Prop) + (s : S) (a : A) (r : Result (core.result.Result Unit error.Exc × S × A)) : Prop := + (∃ e e', r = ok (.Err e, s, a) ∧ D = ({(.error e', σ)}, False) ∧ VsErrRelA e e') ∨ + ∃ s' iv, D = ({(.ok (.invokePage iv), σ)}, False) ∧ ∃ σ1, + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ ∃ σ', + (([] : List Fl4SpecA64.Data), σ') ∈ (Fl4SpecA64.performPageInvocation (Z := Unit) iv σ1).1 ∧ + r ⦃ x => ∃ a', x = ((.Ok () : core.result.Result Unit error.Exc), s', a') ∧ + JAtZAX (L := L) AR Z s' a' σ' m ∧ TreeA st OS Z.G.chains Z.G.par s' ∧ + (CarryA Z (L.mem s) σ → CarryA Z (L.mem s') σ') ⦄ + +/-- (gen 30) **`CarryA` across the page map** (module docstring). -/ +theorem carry_pgMapA {Z : XSetsA} {OS : Set U64} {sl fp rt asid0 va0 asid va : U64} {sz : capability.FrameSize} + {dev mp : Bool} {s s' : S} {σ σ1 σ' : Fl4SpecA64.State Unit} {iv : Fl4SpecA64.PageInvocation} + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) (hT : TreeA st OS Z.G.chains Z.G.par s) + (hcap : capAtA (L.mem s) sl = .Frame fp sz rt dev mp false asid0 va0) + (h3 : Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False)) + (h4 : (([] : List Fl4SpecA64.Data), σ') ∈ (Fl4SpecA64.performPageInvocation (Z := Unit) iv σ1).1) + (F : SlotFrA (L := L) OS sl (frMapA (capAtA (L.mem s) sl) asid va) s s') : + CarryA Z (L.mem s) σ → CarryA Z (L.mem s') σ' := by + rw [hcap] at F + exact fun C => C.of_slotFrA hsub hT F rfl rfl (fun _ h => h) (fun _ h => h) (fun _ _ _ e => by cases e) + (by rw [hcap]; exact CapFinEqA.frame2A _ _ _ _ _ _ _ _ _ _ _) ((restart_tyA h3).trans (perfPg_tyA iv σ1 _ h4)) + +theorem PgMapRelGA.liftA {ar : machine.Arch A} {AR : A → HwRootA → Prop} (K : ArchKeepsA ar AR) {Z : XSetsA} + {OS : Set U64} {sl : U64} {m : Fl4SpecA64.McState} {σ : Fl4SpecA64.State Unit} + {D : Set (Except Fl4SpecA64.SyscallError Fl4SpecA64.ArchInvocation × Fl4SpecA64.State Unit) × Prop} + {s : S} {a : A} {r : Result (core.result.Result Unit error.Exc × S × A)} (ha : AR a Z.hw) + (hT : TreeA st OS Z.G.chains Z.G.par s) + (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) (hTO : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s q) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) {fp rt asid0 va0 : U64} {sz : capability.FrameSize} {dev mp : Bool} + (hcap : capAtA (L.mem s) sl = .Frame fp sz rt dev mp false asid0 va0) + (h : PgMapRelGA (L := L) ar AR Z OS sl m σ D s a r) : PgMapJA (L := L) ar AR Z OS m σ D s a r := + h.imp_right fun ⟨s', hF, iv, h2, σ1, h3, σ', h4, _, _, _, h6⟩ => ⟨s', iv, h2, σ1, h3, σ', h4, + WP.spec_mono (hF.arA K ha) fun _ ⟨a', hx, ha'⟩ => + ⟨a', hx, (h6 a' ha' hT hnul hTO trivial).1, (h6 a' ha' hT hnul hTO trivial).2.1, + carry_pgMapA hsub hT hcap h3 h4 (h6 a' ha' hT hnul hTO trivial).2.2.2.2⟩⟩ + +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- **Stage-1 `ARMPageTableMap` with `JAtZAX` after it** (module docstring): `pt_map_refinesGA` under the call-site +tree, null non-relation slots, `TabOffA` and ruling (c)'s `ptr ∉ S2PgE ∧ ptr ∉ Y`, at `insert ptr S1`. -/ +theorem pt_map_jA (ar : machine.Arch A) (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) + (cte : Fl4SpecA64.CslotPtr) (hab : AbsSlotA Z.th cte) (hos : cslotAddrA cte ∈ OS) + {ptr asid0 va0 : U64} {mp : Bool} (hcap : capAtA (L.mem s) (cslotAddrA cte) = .PageTable ptr mp asid0 va0) + (UP : UnmPtA Z.S1 Z.Y OS H.tcbs (L.mem s) σ ptr) + (slbl : Fl4SpecA64.Data) (hl : Fl4SpecA64.invocationType slbl = .archInvocationLabel .armPageTableMap) + (length buf : U64) (args : List U64) (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) + (x0 : U64) (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) + (exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) (hex : ExtraRelA Z.th (L.mem s) x0 (exs.map Prod.fst)) + (hS1 : ∀ p l mp' asid va, capAtA (L.mem s) x0 ≠ .Ept p l mp' asid va) + (hHi : ∀ p mp' asid, capAtA (L.mem s) x0 ≠ .HighVSpace p mp' asid) + (hT : TreeA st OS Z.G.chains Z.G.par s) (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) + (hTO : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s q) (hS2 : ptr ∉ S2PgE Z.X Z.T2) (hY : ptr ∉ Z.Y) : + vspace.decode_page_table st ar s a fl4_abi.label.ARMPageTableMap length (cslotAddrA cte) + (.PageTable ptr mp asid0 va0) buf extra ⦃ res => PtMapJPostA (L := L) AR Z OS m ptr σ + ((Fl4SpecA64.decodePageTableInvocation (Z := Unit) slbl (args.map UScalar.bv) cte + (.pageTableCap (specPtr ptr) .normalPtT (mapDataA mp asid0 va0)) exs).run σ) s a res ⦄ := + WP.spec_mono (pt_map_refinesGA ar E RS cte hab hos hcap UP slbl hl length buf args hlen + hargs x0 hx0 exs hex hS1 hHi) fun _ h => PtMapPostGA.liftA hT hnul hTO ⟨hS2, hY⟩ RS.sub hcap h + +/-- **Stage-1 `ARMPageMap` with `JAtZAX` after it** (module docstring): `page_map_refinesGA` under `ArchKeepsA` +**[P]**, the call-site tree, null non-relation slots and `TabOffA`, at the same ghosts; the arch state after the +flushes is related at `Z.hw` (`FlA.arA`). -/ +theorem page_map_jA (ar : machine.Arch A) (K : ArchKeepsA ar AR) + (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) + (cte : Fl4SpecA64.CslotPtr) (hab : AbsSlotA Z.th cte) (hos : cslotAddrA cte ∈ OS) + {fp asid0 va0 rt : U64} {sz : capability.FrameSize} {dev mp : Bool} + (hcap : capAtA (L.mem s) (cslotAddrA cte) = .Frame fp sz rt dev mp false asid0 va0) + (FP : FrPlA sz fp mp asid0) + (slbl : Fl4SpecA64.Data) (hl : Fl4SpecA64.invocationType slbl = .archInvocationLabel .armPageMap) + (length buf : U64) (args : List U64) (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) (call : Bool) + (x0 : U64) (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) + (exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) (hex : ExtraRelA Z.th (L.mem s) x0 (exs.map Prod.fst)) + (hS1 : ∀ p l mp' asid va, capAtA (L.mem s) x0 ≠ .Ept p l mp' asid va) + (hHi : ∀ p mp' asid, capAtA (L.mem s) x0 ≠ .HighVSpace p mp' asid) + (hT : TreeA st OS Z.G.chains Z.G.par s) (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) + (hTO : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s q) : + PgMapJA (L := L) ar AR Z OS m σ ((Fl4SpecA64.decodeFrameInvocation (Z := Unit) slbl (args.map UScalar.bv) cte + (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev (mapDataA mp asid0 va0)) exs).run σ) s a + (vspace.decode_frame st ar s a fl4_abi.label.ARMPageMap length (cslotAddrA cte) + (.Frame fp sz rt dev mp false asid0 va0) call buf extra) := + (page_map_refinesGA ar E RS cte hab hos hcap FP slbl hl length buf args hlen hargs call x0 + hx0 exs hex hS1 hHi).liftA K E.j.1.ar hT hnul hTO RS.sub hcap + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/VsMapPageA.lean b/proofs/Fl4ProofsA64/P3/VsMapPageA.lean new file mode 100644 index 000000000..d6bea6a11 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/VsMapPageA.lean @@ -0,0 +1,1073 @@ +/- +P3 (aarch64, A64-P3 S3 gen 20, ruling 19:57Z item 1): **Stage-1 `ARMPageMap`** — FL4's `vspace::decode_frame` +(`vspace.rs` 687–729) at label `ARMPageMap`, i.e. `decode_page_map` (731–785), on a plain (`is_ept = false`) +`Cap::Frame` with a Stage-1 root (`Cap::Pml4`) in the first extra slot, against l4v's `decode_frame_invocation` +(`decode_fr_inv_map`, `ArchDecode_A`) followed by `set_thread_state Restart` and `perform_page_invocation` +(`perform_pg_inv_map`, `Arch_A`). The A64 twin of x86's `P3/VsFrameMap` / `P3/VsFrameMapRel`, on gen 19's +`P3/VsMapA` pieces (`check_vspace`, `top_iffA`, `lookup_pt_slotA`, `write_storePteA`). + +* **Rights** (`capability.rs`, `vspace.rs` 135–181): `rights_readableA` (FL4's `rights_readable` is l4v's + `AllowRead ∈ R`), `mask_vmA` (`mask_vm_rights` of `Rights::from_word` is l4v's `mask_vm_rights R (data_to_rights + w)`), `ap_bits_eqA` / `pteRights_apA` (the AP bits read back by `rights_of` are the masked rights). +* **The descriptor** (`make_user_page`, `vspace.rs` 184–197): `mup_specA` (its value is the address bits of + `paddr` or-ed with a constant `mupCA` of the device / AP / page / execute-never choices), `mupC_factsA` (the + constant's bits, by evaluation), `mup_fieldsA`, `attrs_mupA`, **`pteRel_mupA`** (the descriptor is + `PteRelA`-related at depth `k` to l4v's `make_user_pte (addrFromPPtr p) (attribs_from_word attr) vm R sz`). +* **`install_page`** (`vspace.rs` 854–869): `install_pageA` — its memory effect is the single descriptor write (a + break-before-make writes 0 first), its arch effect a chain of `invalidate_page` / `cache_op` (`FlA`). +* **The spec side**: `rootCmpA` (l4v's `find_vspace_for_asid` and root compare, shared with `decode_pt_inv_map`: + `ptTailA` is `rootCmpA … ptRestA`), `check_vspaceGA` (gen 19's `check_vspaceA` for any continuation), `frRestA` + / `frRest_runA` (`decode_fr_inv_map` after the root compare), `frDec_truncA`, `frDec_noRootA`, `frDec_bodyA`, + `frInv_mapA`, `perfPgMap_memA` (an outcome of `perform_pg_inv_map`, machine-only after the store). +* **`restart_setcapA`**: the caller's restart and the in-place cap write of the invoked slot, both sides + (`JEntA.restartA`, A64 P1's `set_cap_eq`, `StateRelA.setAbsA`), with `VsRelA` after them (as in gen 19's + `pt_map_refinesA`, factored for the page map and the `JAtZAX` lifts). +* **`page_map_refinesA`** (x86 `frame_map_rel`): FL4's decode either fails with an error `VsErrRelA`-related to the + spec decode's and nothing changed, or succeeds: the spec decodes the map deterministically, and after + `set_thread_state cur Restart`, `perform_page_invocation` has an outcome `VsRelA`-related to FL4's written memory + at the same ghosts (`PgMapRelA`). FL4's result is the arch flush chain `FlA` ending in `Ok` (the TLB / cache + maintenance is not related: `machine.Arch` is trusted, x86's `ArchRefines`). + +**Decode-site hypotheses** (not named premises; listed for the premise accounting as gen 19's `UnmPtA`): +* **`FrPlA`** [J-side, decode-site] — the invoked frame's physical address is aligned to its size and below 2^44 + (PORTING.md's **[P]** bound), and FL4's mapped test `asid != 0` agrees with the cap's `mapped` flag (`mapped ↔ asid + ≠ 0`: `unmapped_frame` clears both, ASID 0 is never assigned, `VsRelA.asid0`). Discharged later from A64 P1's + frame-cap invariants (object alignment, `UtInvA`) and S1A's ASID facts. +* `hS1` / `hHi` (the extra cap is no `Ept` / `HighVSpace` cap: Stage-1 roots only, ruling), the restart's case + hypotheses, `hsub`, `hab`, `hos`, `hcap`, `hlen`, `hargs`, `hex` as gen 19's `pt_map_refinesA`. + +No named premise. No existing statement changed. +(Gen 22) `page_map_refinesA` is the corollary of **`page_map_refinesGA`** (post `PgMapRelGA`: `PgMapRelA` with the +lift's obligation `MapJA` at every arch state related at `Z.hw`, read by `page_map_jA` in `P3/VsMapJA`), and +`restart_setcapA` the corollary of `restart_setcapJA` (`P3/VsTabJA`); statements unchanged. + +**Changed statement (gen 30, forced by ruling 01:13Z item 1):** `PgMapRelGA` takes the invoked slot `sl` and its +`MapJA` is `∃ asid va`, at `sl` and `frMapA (capAtA (L.mem s) sl) asid va` (the slot frame); `page_map_refinesGA`'s +post at `sl := cslotAddrA cte`. `page_map_refinesA`, `restart_setcapA` unchanged. +-/ +import Fl4ProofsA64.P3.VsMapA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## Rights -/ + +theorem vmRO_litA : capability.VM_READ_ONLY = 2#u64 := by unfold capability.VM_READ_ONLY; rfl +theorem vmRW_litA : capability.VM_READ_WRITE = 3#u64 := by unfold capability.VM_READ_WRITE; rfl +theorem vmKO_litA : capability.VM_KERNEL_ONLY = 1#u64 := by unfold capability.VM_KERNEL_ONLY; rfl + +theorem u64_ne_of_valA {r : U64} {n : Nat} (h : ¬ r.val = n) (c : U64) (hc : c.val = n) : r ≠ c := + fun e => h (by rw [e, hc]) + +/-- `rights_readable` is l4v's `AllowRead ∈ R`. -/ +theorem rights_readableA (r : U64) : + vspace.rights_readable r = ok (decide (Fl4SpecA64.Rights.allowRead ∈ vmRightsOfA r)) := by + unfold vspace.rights_readable vmRightsOfA + rw [vmRO_litA, vmRW_litA] + by_cases h3 : r.val = 3 + · have e3 : r = 3#u64 := UScalar.eq_of_val_eq (by rw [h3]; rfl) + rw [if_neg (by rw [e3]; decide), if_pos e3, if_pos h3] + simp [Fl4SpecA64.vmReadWrite] + · by_cases h2 : r.val = 2 + · have e2 : r = 2#u64 := UScalar.eq_of_val_eq (by rw [h2]; rfl) + rw [if_pos e2, if_neg h3, if_pos h2] + simp [Fl4SpecA64.vmReadOnly] + · rw [if_neg (u64_ne_of_valA h2 _ rfl), if_neg (u64_ne_of_valA h3 _ rfl), if_neg h3, if_neg h2] + simp [Fl4SpecA64.vmKernelOnly] + +/-- **`mask_vm_rights` is l4v's** (module docstring). -/ +theorem mask_vmA (rt : U64) (r : capability.Rights) : + capability.mask_vm_rights rt r ⦃ vm => vmRightsOfA vm = + Fl4SpecA64.maskVmRights (vmRightsOfA rt) (rightsOfA r.write r.read r.grant r.grant_reply) ⦄ := by + unfold capability.mask_vm_rights + rw [vmRO_litA, vmRW_litA, vmKO_litA] + have n23 : (2#u64 : U64) ≠ 3#u64 := by decide + have v1 : (1#u64 : U64).val = 1 := rfl + have v2 : (2#u64 : U64).val = 2 := rfl + have v3 : (3#u64 : U64).val = 3 := rfl + by_cases h3 : rt.val = 3 + · have e3 : rt = 3#u64 := UScalar.eq_of_val_eq (by rw [h3]; rfl) + subst e3 + rw [if_neg (Ne.symm n23), if_pos rfl] + cases hr : r.read <;> cases hw : r.write <;> + simp [hr, hw, v1, v2, v3, vmRightsOfA, Fl4SpecA64.maskVmRights, Fl4SpecA64.validateVmRights, rightsOfA, + Fl4SpecA64.vmReadWrite, Fl4SpecA64.vmReadOnly, Fl4SpecA64.vmKernelOnly, WP.spec_ok] + · by_cases h2 : rt.val = 2 + · have e2 : rt = 2#u64 := UScalar.eq_of_val_eq (by rw [h2]; rfl) + subst e2 + rw [if_pos rfl] + cases hr : r.read <;> + simp [hr, n23, v1, v2, v3, vmRightsOfA, Fl4SpecA64.maskVmRights, Fl4SpecA64.validateVmRights, rightsOfA, + Fl4SpecA64.vmReadWrite, Fl4SpecA64.vmReadOnly, Fl4SpecA64.vmKernelOnly, WP.spec_ok] + · rw [if_neg (u64_ne_of_valA h2 _ rfl), if_neg (u64_ne_of_valA h3 _ rfl)] + simp [h2, h3, v1, vmRightsOfA, Fl4SpecA64.maskVmRights, Fl4SpecA64.validateVmRights, + Fl4SpecA64.vmKernelOnly, WP.spec_ok] + +/-- AP[2:1] of a VM rights word (`ap_bits`). -/ +def apNA (vm : U64) : Nat := if vm.val = 3 then 1 else if vm.val = 2 then 3 else 0 + +theorem apNA_cases (vm : U64) : apNA vm = 0 ∨ apNA vm = 1 ∨ apNA vm = 3 := by + unfold apNA; split_ifs <;> simp + +theorem ap_bits_eqA (vm : U64) : vspace.ap_bits vm = ok ⟨BitVec.ofNat 64 (apNA vm)⟩ := by + unfold vspace.ap_bits apNA + rw [vmRW_litA, vmRO_litA] + by_cases h3 : vm.val = 3 + · rw [if_pos (UScalar.eq_of_val_eq (by rw [h3]; rfl) : vm = 3#u64), if_pos h3]; rfl + · by_cases h2 : vm.val = 2 + · rw [if_neg (u64_ne_of_valA h3 _ rfl), if_pos (UScalar.eq_of_val_eq (by rw [h2]; rfl) : vm = 2#u64), if_neg h3, + if_pos h2]; rfl + · rw [if_neg (u64_ne_of_valA h3 _ rfl), if_neg (u64_ne_of_valA h2 _ rfl), if_neg h3, if_neg h2]; rfl + +/-- The rights `rights_of` reads back from the AP bits `ap_bits vm` are `vm`'s. -/ +theorem pteRights_apA {w vm : U64} (h : apA w = apNA vm) : pteRightsA w = vmRightsOfA vm := by + unfold pteRightsA vmRightsOfA + rw [h] + unfold apNA + by_cases h3 : vm.val = 3 + · simp [h3] + · by_cases h2 : vm.val = 2 + · simp [h3, h2] + · simp [h3, h2] + +/-! ## The descriptor -/ + +/-- The flag bits of `make_user_page`'s descriptor (module docstring): `VALID` (and `TABLE_OR_PAGE` at the last +level), the MAIR index (`DEVICE_NGNRNE` = 1 for device memory), AP, inner shareability for normal memory, `AF`, +`nG`, `PXN`, and `UXN` when execute-never. -/ +def mupCA (dv : Bool) (ap : Nat) (pg xn : Bool) : Nat := + (if pg then 3 else 1) ||| (if dv then 4 else 0) ||| ap * 64 ||| (if dv then 0 else 768) ||| 1024 ||| 2048 ||| + 9007199254740992 ||| (if xn then 18014398509481984 else 0) + +/-- The constant's bits (by evaluation). -/ +theorem mupC_factsA (dv pg xn : Bool) {ap : Nat} (hap : ap = 0 ∨ ap = 1 ∨ ap = 3) : + mupCA dv ap pg xn &&& 281474976706560 = 0 ∧ (mupCA dv ap pg xn).testBit 0 = true ∧ + (mupCA dv ap pg xn).testBit 1 = pg ∧ mupCA dv ap pg xn % 4096 / 64 % 4 = ap ∧ + mupCA dv ap pg xn % 4096 / 4 % 8 = (if dv then 1 else 0) ∧ (mupCA dv ap pg xn).testBit 54 = xn ∧ + mupCA dv ap pg xn < 2 ^ 64 := by + rcases hap with rfl | rfl | rfl <;> cases dv <;> cases pg <;> cases xn <;> decide + +theorem tbM_lowA {y : Nat} (hy : y % 4096 = 0) {i : Nat} (hi : i < 12) : y.testBit i = false := by + have h := Nat.testBit_mod_two_pow y 12 i + rw [show y % 2 ^ 12 = 0 by omega, Nat.zero_testBit] at h + cases hb : y.testBit i + · rfl + · rw [hb] at h; simp [hi] at h + +/-- The address bits of any word are page-aligned and below 2^48. -/ +theorem landM_A (x : Nat) : (x &&& 281474976706560) % 4096 = 0 ∧ (x &&& 281474976706560) < 2 ^ 48 := by + refine ⟨?_, lt_of_le_of_lt Nat.and_le_right (by norm_num)⟩ + apply Nat.eq_of_testBit_eq + intro i + rw [show (4096 : Nat) = 2 ^ 12 by norm_num, Nat.testBit_mod_two_pow, Nat.zero_testBit, Nat.testBit_and, tb_M i] + by_cases hi : i < 12 + · have : ¬ 12 ≤ i := by omega + simp [this] + · simp [hi] + +theorem orC_lowA {y C : Nat} (hy : y % 4096 = 0) : (y ||| C) % 4096 = C % 4096 := by + apply Nat.eq_of_testBit_eq + intro i + rw [show (4096 : Nat) = 2 ^ 12 by norm_num, Nat.testBit_mod_two_pow, Nat.testBit_mod_two_pow, Nat.testBit_or] + by_cases hi : i < 12 + · rw [tbM_lowA hy hi]; simp + · simp [hi] + +theorem orC_hiA {y C : Nat} (hy : y < 2 ^ 48) {i : Nat} (hi : 48 ≤ i) : (y ||| C).testBit i = C.testBit i := by + rw [Nat.testBit_or, Nat.testBit_eq_false_of_lt (lt_of_lt_of_le hy (Nat.pow_le_pow_right (by norm_num) hi))] + simp + +theorem orC_MA {y C : Nat} (hy1 : y % 4096 = 0) (hy2 : y < 2 ^ 48) (hC : C &&& 281474976706560 = 0) : + (y ||| C) &&& 281474976706560 = y := by + apply Nat.eq_of_testBit_eq + intro i + have hc := congrArg (fun n => n.testBit i) hC + simp only [Nat.testBit_and, Nat.zero_testBit, tb_M i] at hc + rw [Nat.testBit_and, Nat.testBit_or, tb_M i] + by_cases hi : 12 ≤ i ∧ i < 48 + · have h1 : decide (12 ≤ i) = true := decide_eq_true hi.1 + have h2 : decide (i < 48) = true := decide_eq_true hi.2 + rw [h1, h2] at hc ⊢ + simp only [Bool.and_true] at hc ⊢ + rw [hc, Bool.or_false] + · have hy : y.testBit i = false := by + rcases Nat.lt_or_ge i 12 with h | h + · exact tbM_lowA hy1 h + · exact Nat.testBit_eq_false_of_lt (lt_of_lt_of_le hy2 (Nat.pow_le_pow_right (by norm_num) (by omega))) + have hd : (decide (12 ≤ i) && decide (i < 48)) = false := by + simp only [Bool.and_eq_false_iff, decide_eq_false_iff_not]; omega + rw [hd, hy]; simp + +/-- **`make_user_page`'s value** (module docstring). -/ +theorem mup_specA (paddr vm attr shift : U64) (cr : Bool) : + vspace.make_user_page paddr vm cr attr shift ⦃ e => e.val = (paddr.val &&& 281474976706560) ||| + mupCA (!attr.bv.getLsbD 0) (apNA vm) (decide (shift = 12#u64)) (attr.bv.getLsbD 2 || !cr) ⦄ := by + have hxn : vspace.exec_never attr cr = ok (attr.bv.getLsbD 2 || !cr) := by + unfold vspace.exec_never + simp only [lift, bind_ok] + rw [and_pow_neA attr 2 (by decide) _ (by unfold fl4_abi.consts.arm_vm_attr.EXECUTE_NEVER; decide)] + cases attr.bv.getLsbD 2 <;> cases cr <;> rfl + have eN : vspace.ATTR_NORMAL = 0#u64 := by unfold vspace.ATTR_NORMAL; rfl + have eD : vspace.ATTR_DEVICE_NGNRNE = 1#u64 := by unfold vspace.ATTR_DEVICE_NGNRNE; rfl + have eA : vspace.D_ATTR_SHIFT = 2#u64 := by unfold vspace.D_ATTR_SHIFT; rfl + have eP : vspace.D_AP_SHIFT = 6#u64 := by unfold vspace.D_AP_SHIFT; rfl + have e12 : vspace.PAGE_BITS = 12#u64 := by unfold vspace.PAGE_BITS; rfl + have sh2 : ∀ x : U64, x <<< (2#u64 : U64) = ok ⟨x.bv <<< 2⟩ := fun x => CapRt.shl_u64_eq x 2#u64 (by decide) + have sh6 : ∀ x : U64, x <<< (6#u64 : U64) = ok ⟨x.bv <<< 6⟩ := fun x => CapRt.shl_u64_eq x 6#u64 (by decide) + have hor : ∀ a b c : U64, (a ||| b) ||| c = a ||| (b ||| c) := fun a b c => + UScalar.eq_of_val_eq (by simp only [UScalar.val_or, Nat.lor_assoc]) + unfold vspace.make_user_page + rw [hxn, bind_ok, ap_bits_eqA] + simp only [eq_of_spec_fd P0.Consts.vspace.D_SH_INNER.spec, eq_of_spec_fd P0.Consts.vspace.D_VALID.spec, + eq_of_spec_fd P0.Consts.vspace.D_TABLE_OR_PAGE.spec, eq_of_spec_fd P0.Consts.vspace.D_AF.spec, + eq_of_spec_fd P0.Consts.vspace.D_NG.spec, eq_of_spec_fd P0.Consts.vspace.D_PXN.spec, + eq_of_spec_fd P0.Consts.vspace.D_UXN.spec, lift, bind_ok, eN, eD, eA, eP, e12, sh2, sh6] + rw [and_pow_neA attr 0 (by decide) _ (by unfold fl4_abi.consts.arm_vm_attr.PAGE_CACHEABLE; decide)] + have hap := apNA_cases vm + generalize apNA vm = ap at hap ⊢ + generalize (attr.bv.getLsbD 2 || !cr) = xn + generalize attr.bv.getLsbD 0 = b0 + by_cases hs : shift = 12#u64 + · simp only [hs, decide_true, ↓reduceIte] + rcases hap with rfl | rfl | rfl <;> cases b0 <;> cases xn <;> + (simp only [Bool.false_eq_true, ↓reduceIte, bind_ok, Bool.not_false, Bool.not_true, hor] + refine (WP.spec_ok _).2 ?_ + rw [UScalar.val_or, UScalar.val_and, Fl4ProofsA64.P0.Consts.vspace.ADDR_MASK.val_eq] + congr 1) + all_goals decide + · simp only [hs, decide_false, ↓reduceIte] + rcases hap with rfl | rfl | rfl <;> cases b0 <;> cases xn <;> + (simp only [Bool.false_eq_true, ↓reduceIte, bind_ok, Bool.not_false, Bool.not_true, hor] + refine (WP.spec_ok _).2 ?_ + rw [UScalar.val_or, UScalar.val_and, Fl4ProofsA64.P0.Consts.vspace.ADDR_MASK.val_eq] + congr 1) + all_goals decide + +/-- The fields of `make_user_page`'s descriptor (module docstring). -/ +theorem mup_fieldsA {e paddr : U64} {dv pg xn : Bool} {ap : Nat} (hap : ap = 0 ∨ ap = 1 ∨ ap = 3) + (he : e.val = (paddr.val &&& 281474976706560) ||| mupCA dv ap pg xn) : + dBitA e 0 = true ∧ dBitA e 1 = pg ∧ (dAddrA e).val = paddr.val &&& 281474976706560 ∧ apA e = ap ∧ + attrIdxA e = (if dv then 1 else 0) ∧ dBitA e 54 = xn := by + obtain ⟨hC, h0, h1, hapx, hidx, h54, -⟩ := mupC_factsA dv pg xn hap + obtain ⟨hy1, hy2⟩ := landM_A paddr.val + have hlow := orC_lowA (C := mupCA dv ap pg xn) hy1 + rw [← he] at hlow + have tb : ∀ i, dBitA e i = e.val.testBit i := fun i => rfl + refine ⟨?_, ?_, ?_, ?_, ?_, ?_⟩ + · rw [tb, he, Nat.testBit_or, tbM_lowA hy1 (by norm_num), h0]; rfl + · rw [tb, he, Nat.testBit_or, tbM_lowA hy1 (by norm_num), h1]; simp + · show (e &&& 281474976706560#u64).val = _ + rw [UScalar.val_and, he, show (281474976706560#u64 : U64).val = 281474976706560 from rfl] + exact orC_MA hy1 hy2 hC + · unfold apA; rw [← hapx]; omega + · unfold attrIdxA; rw [← hidx]; omega + · rw [tb, he, orC_hiA hy2 (by norm_num), h54] + +/-- The attributes of `make_user_page`'s descriptor are l4v's `make_user_pte` attributes (module docstring). -/ +theorem attrs_mupA (attr : U64) (R : Fl4SpecA64.VMRights) {e : U64} {cr : Bool} + (hcr : cr = decide (Fl4SpecA64.Rights.allowRead ∈ R)) (h54 : dBitA e 54 = (attr.bv.getLsbD 2 || !cr)) + (hidx : attrIdxA e = if (!attr.bv.getLsbD 0) then 1 else 0) : + PteAttrsA e ((Fl4SpecA64.attribsFromWord attr.bv \ {Fl4SpecA64.VMAttribute.global}) \ + (if Fl4SpecA64.Rights.allowRead ∉ R then {Fl4SpecA64.VMAttribute.execute} else ∅)) where + exec := by + rw [h54, hcr] + by_cases hr : Fl4SpecA64.Rights.allowRead ∈ R + · simp [hr, Fl4SpecA64.attribsFromWord] + · simp [hr, Fl4SpecA64.attribsFromWord] + dev := by + rw [hidx] + cases hb : attr.bv.getLsbD 0 <;> simp [Fl4SpecA64.attribsFromWord, hb] <;> simpa using hb + glob := by simp [Fl4SpecA64.attribsFromWord] + +/-- A word aligned to `2^n` is kept by clearing its low `n` bits. -/ +theorem and_not_maskA (x : BitVec 64) {n : Nat} (h : x.toNat % 2 ^ n = 0) : + x &&& ~~~(BitVec.ofNat 64 (2 ^ n - 1)) = x := by + apply BitVec.eq_of_getLsbD_eq + intro i hi + rw [BitVec.getLsbD_and, BitVec.getLsbD_not, BitVec.getLsbD_ofNat, Nat.testBit_two_pow_sub_one] + by_cases hin : i < n + · have hx : x.getLsbD i = false := by + have e := Nat.testBit_mod_two_pow x.toNat n i + rw [h, Nat.zero_testBit] at e + simp only [hin, decide_true, Bool.true_and] at e + exact e.symm + rw [hx]; simp + · simp [hin, hi] + +/-- l4v's `addrFromPPtr` of FL4's shifted pointer is `paddr(p)`. -/ +theorem addr_specA (p : U64) : + Fl4SpecA64.addrFromPPtr (specPtr p) = (core.num.U64.wrapping_sub p layout.PPTR_BASE).bv := by + rw [addrFromPPtr_specPtr] + apply BitVec.eq_of_toNat_eq + rw [BitVec.toNat_sub] + show (2 ^ 64 - 18446603336221196288 + p.bv.toNat) % 2 ^ 64 = (core.num.U64.wrapping_sub p layout.PPTR_BASE).val + rw [ws_valA] + have : p.bv.toNat = p.val := rfl + omega + +/-- **`make_user_page`'s descriptor against l4v's `make_user_pte`** (module docstring). -/ +theorem pteRel_mupA {Tb : Set U64} {Lv : U64 → Nat} {k : Nat} (hk1 : 0 < k) (hk3 : k ≤ 3) {e paddr vm attr : U64} + {cr : Bool} {R : Fl4SpecA64.VMRights} (hcr : cr = decide (Fl4SpecA64.Rights.allowRead ∈ R)) + (he : e.val = (paddr.val &&& 281474976706560) ||| + mupCA (!attr.bv.getLsbD 0) (apNA vm) (decide (k = 3)) (attr.bv.getLsbD 2 || !cr)) + (hal : paddr.val % 2 ^ shiftA k = 0) (hpa : paddr.val < 2 ^ 44) : + PteRelA Tb Lv k e (.pagePTE paddr.bv (decide (k = 3)) ((Fl4SpecA64.attribsFromWord attr.bv \ + {Fl4SpecA64.VMAttribute.global}) \ (if Fl4SpecA64.Rights.allowRead ∉ R then {Fl4SpecA64.VMAttribute.execute} + else ∅)) (vmRightsOfA vm)) := by + obtain ⟨b0, b1, ha, hapx, hidx, h54⟩ := mup_fieldsA (apNA_cases vm) he + have h12 : 12 ≤ shiftA k := by unfold shiftA; omega + have hal12 : paddr.val % 4096 = 0 := by + have hd : (4096 : Nat) ∣ 2 ^ shiftA k := by + rw [show (4096 : Nat) = 2 ^ 12 by norm_num]; exact Nat.pow_dvd_pow 2 h12 + rw [← Nat.mod_mod_of_dvd paddr.val hd, hal, Nat.zero_mod] + have hd : dAddrA e = paddr := UScalar.eq_of_val_eq (by rw [ha]; exact nat_and_M hal12 (by omega)) + refine ⟨hk1, hk3, b0, ?_, rfl, ?_, ?_, ?_, attrs_mupA attr R hcr h54 hidx⟩ + · rw [b1]; simp + · unfold pageBaseA; rw [hd]; exact (and_not_maskA paddr.bv hal).symm + · rw [hd]; omega + · exact (pteRights_apA hapx).symm + +/-! ## `install_page` -/ + +/-- **An arch flush chain ending in `P a'`** (module docstring): `ok (P a)`, or `invalidate_page` / `cache_op` +followed by such a chain from every arch state. -/ +inductive FlA {A B : Type} (ar : machine.Arch A) (P : A → B) : A → Result B → Prop + | ret (a : A) : FlA ar P a (ok (P a)) + | inv (a : A) (asid va : U64) (k : A → Result B) : (∀ a', FlA ar P a' (k a')) → + FlA ar P a (Std.bind (ar.invalidate_page a asid va) k) + | cache (a : A) (op root asid va n pa : U64) (k : A → Result B) : (∀ a', FlA ar P a' (k a')) → + FlA ar P a (Std.bind (ar.cache_op a op root asid va n pa) k) + +theorem FlA.bind {A B C : Type} {ar : machine.Arch A} {P : A → B} {Q : A → C} {a : A} {m : Result B} + (h : FlA ar P a m) {g : B → Result C} (hg : ∀ a', g (P a') = ok (Q a')) : FlA ar Q a (Std.bind m g) := by + induction h with + | ret a => simp only [bind_ok, hg]; exact .ret a + | inv a asid va k _ ih => simp only [Aeneas.Std.bind_assoc, bind_assoc_eq]; exact .inv a asid va _ ih + | cache a op root asid va n pa k _ ih => simp only [Aeneas.Std.bind_assoc, bind_assoc_eq]; exact .cache a op root asid va n pa _ ih + +/-- The flush after a descriptor write (`install_page`): none for device memory, else a clean-invalidate of the +frame when it is mapped uncached. -/ +theorem flushA {A B : Type} {ar : machine.Arch A} {P : A → B} (a : A) (dev : Bool) (c : Prop) [Decidable c] + (op root asid va n pa : U64) : + FlA ar P a (Std.bind (if dev = true then ok a else if c then ar.cache_op a op root asid va n pa else ok a) + fun a' => ok (P a')) := by + by_cases hd : dev = true + · simp only [hd, ↓reduceIte, bind_ok]; exact .ret a + · by_cases hc : c + · simp only [hd, hc, ↓reduceIte, Bool.false_eq_true]; exact .cache a op root asid va n pa _ fun a' => .ret a' + · simp only [hd, hc, ↓reduceIte, Bool.false_eq_true, bind_ok]; exact .ret a + +section inst +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-- **`install_page` writes the descriptor and flushes** (module docstring). -/ +theorem install_pageA (ar : machine.Arch A) (s : S) (a : A) (lu : vspace.PtSlot) (e root asid vaddr : U64) + (attr : U64) (hal : Aligned lu.slot) : + ∃ s', L.ksb s' = L.ksb s ∧ L.cpu s' = L.cpu s ∧ (∀ b, Aligned b → L.mem s' b = L.mem (L.wr s lu.slot e) b) ∧ + ∀ f : vspace.FrameCap, + FlA ar (fun a' => (s', a')) a (vspace.install_page st ar s a lu e root asid vaddr f attr false) := by + have h63 : (lu.shift &&& 63#u64).val < 64 := by + rw [and_maskA lu.shift 6 _ rfl]; exact Nat.mod_lt _ (by norm_num) + have hbbm := eq_of_spec_fd P0.Consts.vspace.BBM_MASK.spec + have hm2 : ∀ b, Aligned b → L.mem (L.wr (L.wr s lu.slot 0#u64) lu.slot e) b = L.mem (L.wr s lu.slot e) b := by + intro b hb + rw [L.mem_wr _ _ _ _ hal hb, L.mem_wr _ _ _ _ hal hb, L.mem_wr _ _ _ _ hal hb] + split_ifs <;> rfl + by_cases hpg : (if lu.shift = 12#u64 then decide ((L.mem s lu.slot &&& 3#u64) = 3#u64) + else decide ((L.mem s lu.slot &&& 3#u64) = 1#u64)) = true + · by_cases hb : ((L.mem s lu.slot ^^^ e) &&& 18419722475945327423#u64) = 0#u64 + · refine ⟨L.wr s lu.slot e, L.ksb_wr _ _ _, L.cpu_wr _ _ _, fun _ _ => rfl, fun f => ?_⟩ + unfold vspace.install_page vspace.invalidate_mapping + simp only [L.read_eq, bind_ok, is_page_eqA, hpg, ↓reduceIte, lift, hbbm, hb, L.write_eq] + exact .inv a asid vaddr _ fun a' => .ret a' + · refine ⟨L.wr (L.wr s lu.slot 0#u64) lu.slot e, by simp only [L.ksb_wr], by simp only [L.cpu_wr], hm2, + fun f => ?_⟩ + have hpd : layout.paddr f.ptr = ok (core.num.U64.wrapping_sub f.ptr layout.PPTR_BASE) := rfl + unfold vspace.install_page vspace.invalidate_mapping + simp only [L.read_eq, bind_ok, is_page_eqA, hpg, ↓reduceIte, lift, hbbm, hb, L.write_eq, + CapRt.shl_u64_eq 1#u64 _ h63, hpd] + exact .inv a asid vaddr _ fun a1 => flushA (P := fun a' => (_, a')) a1 _ _ _ _ _ _ _ _ + · refine ⟨L.wr s lu.slot e, L.ksb_wr _ _ _, L.cpu_wr _ _ _, fun _ _ => rfl, fun f => ?_⟩ + have hpd : layout.paddr f.ptr = ok (core.num.U64.wrapping_sub f.ptr layout.PPTR_BASE) := rfl + unfold vspace.install_page vspace.invalidate_mapping + simp only [L.read_eq, bind_ok, is_page_eqA, hpg, ↓reduceIte, Bool.false_eq_true, lift, L.write_eq, + CapRt.shl_u64_eq 1#u64 _ h63, hpd] + exact flushA (P := fun a' => (_, a')) a _ _ _ _ _ _ _ _ + +end inst + +/-! ## The spec side -/ + +section spec +open Fl4Spec Fl4SpecA64 + +/-- l4v's `find_vspace_for_asid` and the root compare before a continuation `k` (module docstring). -/ +def rootCmpA {β : Type} (pt : ObjRef) (asid : Asid) (k : SEMonad Unit β) : SEMonad Unit β := do + let pt' ← lookupErrorOnFailure false <| findVspaceForAsid asid + NonDetE.whenE (pt' ≠ pt) <| NonDetE.throwError (.invalidCapability 1) + k + +theorem ptTail_rootA (p : ObjRef) (t : PtType) (pt : ObjRef) (asid : Asid) (v : Data) (cte : CslotPtr) : + ptTailA p t pt asid v cte = rootCmpA pt asid (ptRestA p t pt asid v cte) := rfl + +/-- **l4v's `decode_fr_inv_map` after the root compare** (module docstring). -/ +def frRestA (p : ObjRef) (R : VMRights) (pgsz : VMPageSize) (dev : Bool) (md : Option (Asid × VSpaceRef)) + (pt : ObjRef) (asid : Asid) (vaddr rightsMask attr : Data) (cte : CslotPtr) : SEMonad Unit ArchInvocation := do + checkVpAlignment pgsz vaddr + let pgBits ← NonDetE.returnOk (pageBitsForSize pgsz) + match md with + | some (asid', vaddr') => do + NonDetE.whenE (asid' ≠ asid) <| NonDetE.throwError (.invalidCapability 1) + NonDetE.whenE (vaddr' ≠ vaddr) <| NonDetE.throwError (.invalidArgument 0) + | none => do + let vtop ← NonDetE.returnOk (vaddr + mask (pageBitsForSize pgsz)) + NonDetE.whenE (vtop > userVtop) <| NonDetE.throwError (.invalidArgument 0) + let (level, slot) ← NonDetE.liftE + (NonDet.getsThe fun s : State Unit => ptLookupSlot pt vaddr (ptesOf s)) + NonDetE.unlessE (ptBitsLeft level = pgBits) <| + NonDetE.throwError (.failedLookup false (.missingCapability (ptBitsLeft level))) + let vmRights ← NonDetE.returnOk (maskVmRights R (dataToRights rightsMask)) + let attribs ← NonDetE.returnOk (attribsFromWord attr) + let pte ← NonDetE.returnOk (makeUserPte (addrFromPPtr p) attribs vmRights R pgsz) + NonDetE.returnOk <| .invokePage <| + .pageMap (.frameCap p R pgsz dev (some (asid, vaddr))) cte (pte, slot, level) + +/-- The mapped-address checks of `decode_fr_inv_map`: the ASID check, then the address / `user_vtop` check. -/ +def frC1A (md : Option (Asid × VSpaceRef)) (asid : Asid) : Prop := + match md with + | some (asid', _) => asid' ≠ asid + | none => False + +def frC2A (md : Option (Asid × VSpaceRef)) (vaddr : Data) (sz : VMPageSize) : Prop := + match md with + | some (_, vaddr') => vaddr' ≠ vaddr + | none => vaddr + mask (pageBitsForSize sz) > userVtop + +theorem run_whenE_throwA {σ ε α : Type} {P : Prop} [Decidable P] (h : P) (e : ε) (s : σ) : + (NonDetE.whenE P (NonDetE.throwError e : NonDetE σ ε Unit) >>= fun _ => (show NonDetE σ ε α from + NonDetE.throwError e)).run s = ({(.error e, s)}, False) := by + simp only [NonDetE.whenE, h, ↓reduceIte, NonDetE.throwError, ndE_throw_bindA]; rfl + +/-- **The run of `frRestA` at a found slot** (module docstring). -/ +theorem frRest_runA {p : ObjRef} {R : VMRights} {sz : VMPageSize} {dev : Bool} {md : Option (Asid × VSpaceRef)} + {pt : ObjRef} {asid : Asid} {v rm at' : Data} {cte : CslotPtr} {σ : State Unit} {lv : VmLevel} {sl : ObjRef} + (hL : ptLookupSlot pt v (ptesOf σ) = some (lv, sl)) : + (frRestA p R sz dev md pt asid v rm at' cte).run σ = + if ¬ isAligned v (pageBitsForSize sz) then ({(.error .alignmentError, σ)}, False) + else if frC1A md asid then ({(.error (.invalidCapability 1), σ)}, False) + else if frC2A md v sz then ({(.error (.invalidArgument 0), σ)}, False) + else if ¬ ptBitsLeft lv = pageBitsForSize sz then + ({(.error (.failedLookup false (.missingCapability (ptBitsLeft lv))), σ)}, False) + else ({(.ok (.invokePage (.pageMap (.frameCap p R sz dev (some (asid, v))) cte + (makeUserPte (addrFromPPtr p) (attribsFromWord at') (maskVmRights R (dataToRights rm)) R sz, sl, lv))), σ)}, + False) := by + unfold frRestA checkVpAlignment + by_cases ha : isAligned v (pageBitsForSize sz) + swap + · rw [if_pos ha, run_bind_errorA (unlessE_negA ha _ σ)] + rw [if_neg (not_not.2 ha), run_bind_okA (unlessE_posA ha _ σ)] + simp only [NonDetE.returnOk, ndE_pure_bindA] + cases md + case' some x => + obtain ⟨a', v'⟩ := x + simp only [frC1A, frC2A] + by_cases h1 : a' = asid + swap + · simp only [h1, ne_eq, not_false_eq_true, ↓reduceIte, NonDetE.whenE, NonDetE.throwError, ndE_throw_bindA] + rfl + subst a' + by_cases h2 : v' = v + swap + · simp only [h2, ne_eq, not_true_eq_false, not_false_eq_true, ↓reduceIte, NonDetE.whenE, NonDetE.throwError, + ndE_throw_bindA, ndE_pure_bindA] + rfl + subst v' + simp only [ne_eq, not_true_eq_false, ↓reduceIte, NonDetE.whenE, ndE_pure_bindA] + case' none => + simp only [frC1A, frC2A, ndE_pure_bindA, ↓reduceIte] + by_cases h2 : v + mask (pageBitsForSize sz) > userVtop + · simp only [h2, ↓reduceIte, NonDetE.whenE, NonDetE.throwError, ndE_throw_bindA] + rfl + simp only [h2, ↓reduceIte, NonDetE.whenE, ndE_pure_bindA] + all_goals + rw [lfA_run_getsThe_bind (f := fun s : State Unit => ptLookupSlot pt v (ptesOf s)) (s := σ) hL] + dsimp only + by_cases h3 : ptBitsLeft lv = pageBitsForSize sz + · rw [run_bind_okA (unlessE_posA h3 _ σ)] + simp only [h3, not_true_eq_false, ↓reduceIte, ndE_pure_bindA] + rfl + · rw [run_bind_errorA (unlessE_negA h3 _ σ)] + simp only [h3, not_false_eq_true, ↓reduceIte] + +theorem frInv_mapA {lbl : Data} (hl : invocationType lbl = .archInvocationLabel .armPageMap) (args : List Data) + (cte : CslotPtr) (cap : ArchCap) (exs : List (Cap × CslotPtr)) : + decodeFrameInvocation (Z := Unit) lbl args cte cap exs = decodeFrInvMap lbl args cte cap exs := by + unfold decodeFrameInvocation + rw [if_pos hl] + +theorem frDec_truncA (lbl : Data) (args : List Data) (cte : CslotPtr) (p : ObjRef) (R : VMRights) + (sz : VMPageSize) (dev : Bool) (md : Option (Asid × VSpaceRef)) (exs : List (Cap × CslotPtr)) (σ : State Unit) + (h : ¬ (args.length > 2 ∧ exs.length > 0)) : + (decodeFrInvMap (Z := Unit) lbl args cte (.frameCap p R sz dev md) exs).run σ = + ({(.error .truncatedMessage, σ)}, False) := by + simp only [decodeFrInvMap, if_neg h]; rfl + +theorem frDec_noRootA (lbl v a1 a2 : Data) (r : List Data) (cte : CslotPtr) (p : ObjRef) (R : VMRights) + (sz : VMPageSize) (dev : Bool) (md : Option (Asid × VSpaceRef)) (vc : Cap) (sl : CslotPtr) + (rest : List (Cap × CslotPtr)) (σ : State Unit) + (hv : ∀ pt asid x, vc ≠ .archObjectCap (.pageTableCap pt .vsRootPtT (some (asid, x)))) : + (decodeFrInvMap (Z := Unit) lbl (v :: a1 :: a2 :: r) cte (.frameCap p R sz dev md) ((vc, sl) :: rest)).run σ = + ({(.error (.invalidCapability 1), σ)}, False) := by + simp only [decodeFrInvMap, List.length_cons, gt_iff_lt, Nat.lt_add_left_iff_pos, Nat.zero_lt_succ, and_self, + ↓reduceIte, List.getElem?_cons_zero, the, checkRoot_noA hv, NonDetE.throwError, ndE_throw_bindA] + rfl + +theorem frDec_bodyA (lbl v a1 a2 : Data) (r : List Data) (cte : CslotPtr) (p : ObjRef) (R : VMRights) + (sz : VMPageSize) (dev : Bool) (md : Option (Asid × VSpaceRef)) (pt : ObjRef) (asid : Asid) (x : VSpaceRef) + (sl : CslotPtr) (rest : List (Cap × CslotPtr)) (σ : State Unit) : + (decodeFrInvMap (Z := Unit) lbl (v :: a1 :: a2 :: r) cte (.frameCap p R sz dev md) + ((.archObjectCap (.pageTableCap pt .vsRootPtT (some (asid, x))), sl) :: rest)).run σ = + (rootCmpA pt asid (frRestA p R sz dev md pt asid v a1 a2 cte)).run σ := by + simp only [decodeFrInvMap, List.length_cons, gt_iff_lt, Nat.lt_add_left_iff_pos, Nat.zero_lt_succ, and_self, + ↓reduceIte, List.getElem?_cons_zero, List.getElem?_cons_succ, the, checkVspaceRoot, NonDetE.returnOk, + ndE_pure_bindA, rootCmpA, frRestA] + try rfl + +/-- **An outcome of `perform_pg_inv_map`** (module docstring): after the old entry's read, the cap write and the +store, only machine-state steps. -/ +theorem perfPgMap_memA {σ1 σ2 σ3 : State Unit} {cap : ArchCap} {cte : CslotPtr} {pte old : PTE} {sl : ObjRef} + {lv : VmLevel} {asid : Asid} {v : VSpaceRef} (hmd : cap.acapMapData = some (asid, v)) + (h1 : getPte (Z := Unit) (levelType lv) sl σ1 = ({(old, σ1)}, False)) + (h2 : ((), σ2) ∈ (setCap (Z := Unit) (.archObjectCap cap) cte σ1).1) + (h3 : storePte (Z := Unit) (levelType lv) sl pte σ2 = ({((), σ3)}, False)) : + ∃ σ', (([] : List Data), σ') ∈ (performPageInvocation (Z := Unit) (.pageMap cap cte (pte, sl, lv)) σ1).1 ∧ + MsA σ3 σ' := by + obtain ⟨-, ⟨⟨⟨⟩, σ4⟩, h4⟩, h4m⟩ := doMachineOp_okA (mop := cleanByVA_PoU sl (addrFromPPtr sl)) + (machineOpLift_okA _) σ3 + have hm4 : MsA σ3 σ4 := h4m _ h4 + have hg : (old, σ1) ∈ (getPte (Z := Unit) (levelType lv) sl σ1).1 := by rw [h1]; exact Set.mem_singleton _ + have hs : ((), σ3) ∈ (storePte (Z := Unit) (levelType lv) sl pte σ2).1 := by rw [h3]; exact Set.mem_singleton _ + by_cases ho : old ≠ .invalidPTE + · obtain ⟨-, ⟨⟨⟨⟩, σ5⟩, h5⟩, h5m⟩ := invTlbVa_okA asid v σ4 + refine ⟨σ5, ?_, msA_trans _ _ _ hm4 (h5m _ h5)⟩ + simp only [performPageInvocation, performPgInvMap, nd_bind_applyA, Set.mem_setOf_eq] + refine ⟨((), σ5), ⟨(old, σ1), hg, ((), σ2), h2, ((), σ3), hs, ((), σ4), h4, ?_⟩, Set.mem_singleton _⟩ + simp only [NonDet.when, hmd, NonDet.assertOpt] + rw [if_pos ho] + show ((), σ5) ∈ ((NonDet.pure (asid, v) >>= fun x => invalidateTlbByAsidVa (Z := Unit) x.1 x.2) σ4).1 + rw [nd_bind_applyA] + exact ⟨((asid, v), σ4), Set.mem_singleton _, h5⟩ + · refine ⟨σ4, ?_, hm4⟩ + simp only [performPageInvocation, performPgInvMap, nd_bind_applyA, Set.mem_setOf_eq] + refine ⟨((), σ4), ⟨(old, σ1), hg, ((), σ2), h2, ((), σ3), hs, ((), σ4), h4, ?_⟩, Set.mem_singleton _⟩ + simp only [NonDet.when, ho, ↓reduceIte] + exact Set.mem_singleton _ + +end spec + +/-! ## The root check for any continuation -/ + +section chk +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {Pp Tb : Set U64} {Lv : U64 → Nat} {X Y : Set U64} {σ : Fl4SpecA64.State Unit} + +/-- What `check_vspace` of a mapped `Pml4` cap says against l4v's root compare before `k` (module docstring). -/ +def ChkGA {β : Type} (Tb : Set U64) (Lv : U64 → Nat) (σ : Fl4SpecA64.State Unit) (r asid : U64) + (k : Fl4SpecA64.SEMonad Unit β) : core.result.Result (U64 × U64) error.Exc → Prop + | .Ok x => x = (r, asid) ∧ r ∈ Tb ∧ Lv r = 0 ∧ asid ≠ 0#u64 ∧ + (rootCmpA (specPtr r) (asidOfA asid) k).run σ = k.run σ ∧ + Fl4SpecA64.vspaceForAsid (asidOfA asid) σ = some (specPtr r) + | .Err e => ∃ e', (rootCmpA (specPtr r) (asidOfA asid) k).run σ = ({(.error e', σ)}, False) ∧ VsErrRelA e e' + +/-- **`check_vspace` against l4v's root compare, for any continuation** (module docstring). -/ +theorem check_vspaceGA {β : Type} {s : S} (hR : VsRelA Pp Tb Lv X Y (L.mem s) (L.ksb s) σ) + (hX : ∀ r ∈ X, ∃ es, σ.kheap (specPtr r) = some (.archObj (.ext (.stage2Root es)))) + {r asid : U64} (h16 : asid.val < 65536) (k : Fl4SpecA64.SEMonad Unit β) : + vspace.check_vspace st s (.Pml4 r true asid) 1#u64 ⦃ res => ChkGA Tb Lv σ r asid k res ⦄ := by + simp only [vspace.check_vspace, ↓reduceIte, vspace.find_vspace_for_asid] + refine WP.spec_bind (WP.spec_and (find_asid_findA hR hX h16) (find_asid_tbA hR h16)) fun res ⟨hf, ht⟩ => ?_ + rcases res with w | e + · obtain ⟨hv, hk⟩ := hf + have hrun := lefo_okA (findVs_runA hv hk) + by_cases hw : w = r + · subst hw + simp only [bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + have hne : asid ≠ 0#u64 := by + intro h + subst asid + change Fl4SpecA64.vspaceForAsid (BitVec.ofNat 16 0) σ = _ at hv + rw [vspaceForAsid_zeroA] at hv + cases hv + refine (WP.spec_ok _).2 ⟨rfl, ht.1, ht.2, hne, ?_, hv⟩ + unfold rootCmpA asidOfA + rw [run_bind_okA hrun] + exact run_bind_okA (run_whenE_falseA (by simp) _ _) _ + · have hne : specPtr w ≠ specPtr r := fun e => hw (specPtr_inj e) + simp only [show (w != r) = true by simpa using hw, ↓reduceIte, error.invalid_cap] + refine (WP.spec_ok _).2 ⟨.invalidCapability 1, ?_, Or.inr ⟨1#u64, rfl, rfl⟩⟩ + unfold rootCmpA asidOfA + rw [run_bind_okA hrun] + simp only [Fl4Spec.NonDetE.whenE, hne, ne_eq, not_false_eq_true, ↓reduceIte, Fl4Spec.NonDetE.throwError, + ndE_throw_bindA] + rfl + · have he : e = .InvalidRoot := ht + subst he + refine (WP.spec_ok _).2 ⟨.failedLookup false .invalidRoot, ?_, Or.inl ⟨rfl, trivial⟩⟩ + unfold rootCmpA + rw [run_bind_errorA (lefo_errA (m := Fl4SpecA64.findVspaceForAsid (asidOfA asid)) (findVs_errA hf))] + +end chk + +/-! ## FL4's words of the map -/ + +theorem aligned_iffA (v : U64) {n : Nat} (hn : n < 64) : + Fl4SpecA64.isAligned v.bv n ↔ (v &&& (⟨BitVec.ofNat 64 (2 ^ n - 1)⟩ : U64)) = 0#u64 := by + have hk : (⟨BitVec.ofNat 64 (2 ^ n - 1)⟩ : U64).val = 2 ^ n - 1 := by + show (BitVec.ofNat 64 (2 ^ n - 1)).toNat = _ + rw [BitVec.toNat_ofNat] + apply Nat.mod_eq_of_lt + have := Nat.pow_lt_pow_right (by norm_num : 1 < 2) hn + omega + have h1 := and_maskA v n _ hk + unfold Fl4SpecA64.isAligned + rw [Nat.dvd_iff_mod_eq_zero] + constructor + · intro h; apply UScalar.eq_of_val_eq; rw [h1]; exact h + · intro h; have := congrArg UScalar.val h; rw [h1] at this; exact this + +theorem psA_63A (sz : capability.FrameSize) : (psA sz &&& 63#u64) = psA sz := by cases sz <;> decide + +theorem mask_szA (sz : capability.FrameSize) : + (Fl4SpecA64.mask (Fl4SpecA64.pageBitsForSize (frameSizeOfA sz)) : BitVec 64) = + BitVec.ofNat 64 (2 ^ (psA sz).val - 1) := by + cases sz <;> decide + +/-- An address aligned to a frame size does not wrap at its last byte. -/ +theorem last_valA (v : U64) (sz : capability.FrameSize) (h : v.val % 2 ^ (psA sz).val = 0) : + (wa v ⟨BitVec.ofNat 64 (2 ^ (psA sz).val - 1)⟩).val = v.val + (2 ^ (psA sz).val - 1) := by + rw [wa_valm] + have hv : v.val < 2 ^ 64 := v.bv.isLt + have hk : (⟨BitVec.ofNat 64 (2 ^ (psA sz).val - 1)⟩ : U64).val = 2 ^ (psA sz).val - 1 := by + show (BitVec.ofNat 64 _).toNat = _ + rw [BitVec.toNat_ofNat] + rcases psA_valA sz with e | e | e <;> rw [e] <;> norm_num + rw [hk] + rcases psA_valA sz with e | e | e <;> rw [e] at h ⊢ <;> norm_num at h ⊢ <;> omega + +theorem asidOf_injA {x y : U64} (hx : x.val < 2 ^ 16) (hy : y.val < 2 ^ 16) (h : asidOfA x = asidOfA y) : x = y := by + apply UScalar.eq_of_val_eq + have := congrArg BitVec.toNat h + simp only [asidOfA, BitVec.toNat_ofNat] at this + rwa [Nat.mod_eq_of_lt hx, Nat.mod_eq_of_lt hy] at this + +/-- **The invoked frame's placement** (decode-site hypothesis, module docstring). -/ +structure FrPlA (sz : capability.FrameSize) (fp : U64) (mp : Bool) (asid0 : U64) : Prop where + al : (core.num.U64.wrapping_sub fp layout.PPTR_BASE).val % 2 ^ (psA sz).val = 0 + pa : (core.num.U64.wrapping_sub fp layout.PPTR_BASE).val < 2 ^ 44 + mz : mp = true ↔ asid0 ≠ 0#u64 + +/-! ## The restart and the cap write -/ + +section top +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} + {ia ie : Nat} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- **The caller's restart and the in-place cap write** (module docstring). -/ +theorem restart_setcapA (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + {Rp Nt : Set U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} {sp : U64} + (P : SysPlace st H Z.Cs H.frame Rp Nt OS s extra sp) (Y : SymL st H Z.Cs s) + (hrun : Runnable (L.mem s (stA (L.curW s)))) + (hcur : (L.mem s (inqA (L.curW s)) &&& sched.Q_RELEASE) = 0#u64 ∧ L.mem s (scA (L.curW s)) ≠ 0#u64 ∧ + L.mem s (scA (L.curW s)) ∈ Ac) + (hvs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → Aligned b ∧ b ≠ stA (L.curW s)) + (hvc : ∀ v ∈ Z.V, stA (L.curW s) ≠ v ∧ stA (L.curW s) ≠ wa v 24#u64) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (cte : Fl4SpecA64.CslotPtr) (hab : AbsSlotA Z.th cte) (hos : cslotAddrA cte ∈ OS) + {c : capability.Cap} {sc : Fl4SpecA64.Cap} (hEnc : CapRt.EncWf c) (hca : CapRelA c sc) : + ∃ x s2 σ2, σ.kheap σ.curThread = some (.tcb x) ∧ + sched.set_thread_state st s (L.curW s) sched.TS_RESTART = ok (L.wr s (stA (L.curW s)) sched.TS_RESTART) ∧ + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + obj.Slot.set_cap st (cslotAddrA cte) (L.wr s (stA (L.curW s)) sched.TS_RESTART) c = ok s2 ∧ + ((), σ2) ∈ (Fl4SpecA64.setCap (Z := Unit) sc cte (stUpdA σ σ.curThread x .restart)).1 ∧ + SpecSlotA (stUpdA σ σ.curThread x .restart) σ2 ∧ L.ksb s2 = L.ksb s ∧ + VsRelA Z.Pp Z.S1 Z.Lv Z.X Z.Y (L.mem s2) (L.ksb s2) σ2 := by + obtain ⟨x, s2, σ2, h1, h2, h3, h4, h5, h6, h7, h8, -⟩ := + restart_setcapJA E ⟨⟨_, _, _, _, P⟩, Y, hrun, hcur, hvs, hvc, hsub⟩ cte hab hos hEnc hca + exact ⟨x, s2, σ2, h1, h2, h3, h4, h5, h6, h7, h8⟩ + +/-! ## The refinement -/ + +/-- **The outcome of the page map against the spec decode's run `D` at `σ`** (module docstring; x86 +`frame_map_rel`'s post, the VSpace part). -/ +def PgMapRelA (ar : machine.Arch A) (Z : XSetsA) (σ : Fl4SpecA64.State Unit) + (D : Set (Except Fl4SpecA64.SyscallError Fl4SpecA64.ArchInvocation × Fl4SpecA64.State Unit) × Prop) + (s : S) (a : A) (r : Result (core.result.Result Unit error.Exc × S × A)) : Prop := + (∃ e e', r = ok (.Err e, s, a) ∧ D = ({(.error e', σ)}, False) ∧ VsErrRelA e e') ∨ + ∃ s', FlA ar (fun a' => ((.Ok () : core.result.Result Unit error.Exc), s', a')) a r ∧ + ∃ iv, D = ({(.ok (.invokePage iv), σ)}, False) ∧ ∃ σ1, + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ ∃ σ', + (([] : List Fl4SpecA64.Data), σ') ∈ (Fl4SpecA64.performPageInvocation (Z := Unit) iv σ1).1 ∧ + VsRelA Z.Pp Z.S1 Z.Lv Z.X Z.Y (L.mem s') (L.ksb s') σ' + +/-- (gen 22) **`PgMapRelA` with the lift's obligation** (`MapJA`, `P3/VsTabJA`, at every arch state related at +`Z.hw`): the post `page_map_refinesGA` proves, `page_map_refinesA` / `page_map_jA` read. -/ +def PgMapRelGA (ar : machine.Arch A) (AR : A → HwRootA → Prop) (Z : XSetsA) (OS : Set U64) (sl : U64) + (m : Fl4SpecA64.McState) (σ : Fl4SpecA64.State Unit) + (D : Set (Except Fl4SpecA64.SyscallError Fl4SpecA64.ArchInvocation × Fl4SpecA64.State Unit) × Prop) + (s : S) (a : A) (r : Result (core.result.Result Unit error.Exc × S × A)) : Prop := + (∃ e e', r = ok (.Err e, s, a) ∧ D = ({(.error e', σ)}, False) ∧ VsErrRelA e e') ∨ + ∃ s', FlA ar (fun a' => ((.Ok () : core.result.Result Unit error.Exc), s', a')) a r ∧ + ∃ iv, D = ({(.ok (.invokePage iv), σ)}, False) ∧ ∃ σ1, + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ ∃ σ', + (([] : List Fl4SpecA64.Data), σ') ∈ (Fl4SpecA64.performPageInvocation (Z := Unit) iv σ1).1 ∧ + VsRelA Z.Pp Z.S1 Z.Lv Z.X Z.Y (L.mem s') (L.ksb s') σ' ∧ ∃ asid va, + ∀ a', AR a' Z.hw → MapJA (L := L) AR Z OS m s True Z s' a' σ' sl (frMapA (capAtA (L.mem s) sl) asid va) + +theorem PgMapRelGA.weakA {ar : machine.Arch A} {AR : A → HwRootA → Prop} {Z : XSetsA} {OS : Set U64} {sl : U64} + {m : Fl4SpecA64.McState} {σ : Fl4SpecA64.State Unit} + {D : Set (Except Fl4SpecA64.SyscallError Fl4SpecA64.ArchInvocation × Fl4SpecA64.State Unit) × Prop} + {s : S} {a : A} {r : Result (core.result.Result Unit error.Exc × S × A)} + (h : PgMapRelGA (L := L) ar AR Z OS sl m σ D s a r) : PgMapRelA (L := L) ar Z σ D s a r := + h.imp_right fun ⟨s', h1, iv, h2, σ1, h3, σ', h4, h5, _⟩ => ⟨s', h1, iv, h2, σ1, h3, σ', h4, h5⟩ + +set_option maxHeartbeats 4000000 in +/-- **Stage-1 `ARMPageMap` refines l4v** (module docstring; x86 `frame_map_rel`). -/ +theorem page_map_refinesGA (ar : machine.Arch A) (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) + (cte : Fl4SpecA64.CslotPtr) (hab : AbsSlotA Z.th cte) (hos : cslotAddrA cte ∈ OS) + {fp asid0 va0 rt : U64} {sz : capability.FrameSize} {dev mp : Bool} + (hcap : capAtA (L.mem s) (cslotAddrA cte) = .Frame fp sz rt dev mp false asid0 va0) + (FP : FrPlA sz fp mp asid0) + (slbl : Fl4SpecA64.Data) (hl : Fl4SpecA64.invocationType slbl = .archInvocationLabel .armPageMap) + (length buf : U64) (args : List U64) (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) (call : Bool) + (x0 : U64) (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) + (exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) (hex : ExtraRelA Z.th (L.mem s) x0 (exs.map Prod.fst)) + (hS1 : ∀ p l mp' asid va, capAtA (L.mem s) x0 ≠ .Ept p l mp' asid va) + (hHi : ∀ p mp' asid, capAtA (L.mem s) x0 ≠ .HighVSpace p mp' asid) : + PgMapRelGA (L := L) ar AR Z OS (cslotAddrA cte) m σ ((Fl4SpecA64.decodeFrameInvocation (Z := Unit) slbl (args.map UScalar.bv) cte + (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev (mapDataA mp asid0 va0)) exs).run σ) s a + (vspace.decode_frame st ar s a fl4_abi.label.ARMPageMap length (cslotAddrA cte) + (.Frame fp sz rt dev mp false asid0 va0) call buf extra) := by + rw [frInv_mapA hl] + have hct : sched.cur_thread st s = ok (L.curW s) := cur_thread_ok s + -- the invoked cap's facts + obtain ⟨c0, -, hrel0⟩ := StateRelA.getAbsA E.j.1.st hab + rw [hcap] at hrel0 + obtain ⟨⟨hr1, hr3, ⟨h16a, -⟩, hov⟩, -⟩ := hrel0 + have hEnc0 : CapRt.EncWf (capAtA (L.mem s) (cslotAddrA cte)) := CapRt.decodeF_encWf _ _ + rw [hcap] at hEnc0 + obtain ⟨hcanP, -, -, hcanV⟩ := hEnc0 + unfold vspace.decode_frame vspace.decode_page_map + simp only [vspace.as_frame, hct, bind_ok, eq_self_iff_true, ↓reduceIte, extra0_eqA hx0] + rcases hex with ⟨rfl, hnil⟩ | ⟨hx0n, -, vc', vrest, hvc', hmap⟩ + · have he : exs = [] := List.map_eq_nil_iff.1 hnil + subst he + simp only [↓reduceIte, error.truncated, bind_ok] + exact Or.inl ⟨_, .truncatedMessage, rfl, frDec_truncA _ _ _ _ _ _ _ _ _ σ (fun ⟨_, h⟩ => by simp at h), + Or.inl trivial⟩ + rw [if_neg hx0n] + obtain ⟨⟨vc'', xsl⟩, rest, rfl⟩ : ∃ e rest, exs = e :: rest := by + cases exs with + | nil => simp at hmap + | cons e rest => exact ⟨e, rest, rfl⟩ + simp only [List.map_cons, List.cons.injEq] at hmap + obtain ⟨rfl, -⟩ := hmap + dsimp only + by_cases h3 : length >= 3#u64 + swap + · rw [if_neg h3] + simp only [error.truncated, bind_ok] + refine Or.inl ⟨_, .truncatedMessage, rfl, frDec_truncA _ _ _ _ _ _ _ _ _ σ (fun ⟨h, _⟩ => h3 ?_), + Or.inl trivial⟩ + simp only [List.length_map] at h + scalar_tac + rw [if_pos h3] + have h3' : 3 ≤ args.length := by + have : 3 ≤ length.val := by scalar_tac + omega + obtain ⟨a0, a1, a2, r', rfl⟩ : ∃ a0 a1 a2 r', args = a0 :: a1 :: a2 :: r' := by + match args, h3' with + | a0 :: a1 :: a2 :: r', _ => exact ⟨_, _, _, _, rfl⟩ + simp only [List.map_cons] + obtain ⟨vm, hvm, hvmr⟩ := spec_ex_fd (mask_vmA rt (rightsWA a1)) + rw [hargs 0#u64 a0 rfl, hargs 1#u64 a1 rfl, hargs 2#u64 a2 rfl] + simp only [bind_ok, from_word_eqA, hvm, capA_eq] + simp only [capAtA_eq] at hvc' hS1 hHi + generalize capA st s x0 = vc at hvc' hS1 hHi ⊢ + have nonroot : ∀ (vc : capability.Cap), CapRelA vc vc'' → (∀ r asid, vc ≠ .Pml4 r true asid) → + (∀ p l mp asid va, vc ≠ .Ept p l mp asid va) → (∀ p mp asid, vc ≠ .HighVSpace p mp asid) → + (Fl4SpecA64.decodeFrInvMap (Z := Unit) slbl (a0.bv :: a1.bv :: a2.bv :: List.map UScalar.bv r') cte + (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev (mapDataA mp asid0 va0)) + ((vc'', xsl) :: rest)).run σ = ({(.error (.invalidCapability 1), σ)}, False) := fun vc hr h1 h2 h3 => by + exact frDec_noRootA _ _ _ _ _ _ _ _ _ _ _ _ _ _ σ (by + cases vc with + | PageTable p m asid va => + cases m + · rw [← hr.2]; exact absCap_noRootA h1 h2 h3 + · obtain ⟨_, _, rfl, _, _⟩ := CapEqA.pt_mapped_iff.mp hr.2; intro _ _ _ h; cases h + | _ => rw [← hr.2]; exact absCap_noRootA h1 h2 h3) + have errNR : VsErrRelA (.Sys (.InvalidCapability 1#u64)) (.invalidCapability 1) := Or.inr ⟨1#u64, rfl, rfl⟩ + cases vc with + | Ept p l mp' asid va => exact absurd rfl (hS1 p l mp' asid va) + | HighVSpace p mp' asid => exact absurd rfl (hHi p mp' asid) + | Pml4 r0 mr asid1 => + cases mr + · simp only [bind_ok, Bool.false_eq_true, ↓reduceIte, vspace.check_vspace, error.invalid_cap, + core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame.from, + bind_tc_ok, ite_self] + exact Or.inl ⟨_, _, rfl, nonroot _ hvc' (by simp) (by simp) (by simp), errNR⟩ + -- the main path: a mapped Stage-1 root cap + have hwf : asid1.val < 2 ^ 16 := hvc'.1 + have hvc2 : vc'' = .archObjectCap (.pageTableCap (specPtr r0) .vsRootPtT + (some (asidOfA asid1, (0#u64 : U64).bv))) := hvc'.2.symm + subst hvc2 + simp only [bind_ok, Bool.false_eq_true, ↓reduceIte] + rw [frDec_bodyA] + have h16 : asid1.val < 65536 := lt_of_lt_of_eq hwf (by norm_num) + have R := E.j.1.vs + obtain ⟨res, hres, hpost⟩ := spec_ex_fd (check_vspaceGA R E.j.2.2.1.obj (r := r0) h16 + (frRestA (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev (mapDataA mp asid0 va0) (specPtr r0) + (asidOfA asid1) a0.bv a1.bv a2.bv cte)) + rw [hres] + rcases res with ⟨rr, aa⟩ | e + swap + · obtain ⟨e', he', hrel⟩ := hpost + simp only [bind_ok, core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame.from, + bind_tc_ok, ite_self, Bool.false_eq_true, ↓reduceIte] + exact Or.inl ⟨_, _, rfl, he', hrel⟩ + obtain ⟨he, hrT, hrL, hasid1, hrun', hvRoot⟩ := hpost + simp only [Prod.mk.injEq] at he + obtain ⟨he1, he2⟩ := he + subst rr + subst aa + rw [hrun'] + have hps := psA_valA sz + have hs64 : (psA sz).val < 64 := by omega + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok, page_shift_eqA, psA_63A, uncurry_apply_pair, + Aeneas.Std.uncurry, lift, CapRt.shl_u64_eq 1#u64 (psA sz) hs64, ws1A (psA sz) hs64] + -- the lookup, at the spec's level + obtain ⟨lu, hlu, p, hp, l, hl3, hsh, hlv, hsl, -, hLs, -⟩ := + spec_ex_fd (lookup_pt_slotA R hrT hrL (vsReachA_of_vspaceForAsid R E.j.2.2.1.obj hrT hvRoot) a0) + rw [frRest_runA hLs, pbfs_eqA] + -- the alignment + by_cases hal0 : (a0 &&& (⟨BitVec.ofNat 64 (2 ^ (psA sz).val - 1)⟩ : U64)) = 0#u64 + swap + · have hna : ¬ Fl4SpecA64.isAligned a0.bv (psA sz).val := fun h => hal0 ((aligned_iffA a0 hs64).1 h) + simp only [hal0, bne_iff_ne, ne_eq, not_false_eq_true, ↓reduceIte, ite_self] + rw [if_pos hna] + exact Or.inl ⟨_, _, rfl, rfl, Or.inl trivial⟩ + have halg : Fl4SpecA64.isAligned a0.bv (psA sz).val := (aligned_iffA a0 hs64).2 hal0 + have hamod : a0.val % 2 ^ (psA sz).val = 0 := by + have := congrArg UScalar.val hal0 + rw [and_maskA a0 (psA sz).val _ (by + show (BitVec.ofNat 64 _).toNat = _ + rw [BitVec.toNat_ofNat] + rcases hps with e | e | e <;> rw [e] <;> norm_num)] at this + exact this + simp only [hal0, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + rw [if_neg (not_not.2 halg)] + by_cases hz : asid0 = 0#u64 + case' pos => + have hmp : mp = false := by + cases mp + · rfl + · exact absurd hz (FP.mz.1 rfl) + subst hmp + simp only [show (asid0 != 0#u64) = false by simp [hz], Bool.false_eq_true, ↓reduceIte] + have hlast := last_valA a0 sz hamod + have hc1 : ¬ frC1A (mapDataA false asid0 va0) (asidOfA asid1) := fun h => h + have hc2iff : frC2A (mapDataA false asid0 va0) a0.bv (frameSizeOfA sz) ↔ + wa a0 ⟨BitVec.ofNat 64 (2 ^ (psA sz).val - 1)⟩ > fl4_abi.consts.USER_TOP := by + show (a0.bv + Fl4SpecA64.mask (Fl4SpecA64.pageBitsForSize (frameSizeOfA sz)) > Fl4SpecA64.userVtop) ↔ _ + rw [mask_szA]; exact top_iffA _ + rw [if_neg hc1] + by_cases htop : wa a0 ⟨BitVec.ofNat 64 (2 ^ (psA sz).val - 1)⟩ > fl4_abi.consts.USER_TOP + · rw [if_pos htop, if_pos (hc2iff.2 htop)] + simp only [error.invalid_arg, bind_ok] + exact Or.inl ⟨_, _, rfl, rfl, Or.inl rfl⟩ + have hnl : ¬ wa a0 ⟨BitVec.ofNat 64 (2 ^ (psA sz).val - 1)⟩ < a0 := by + intro h; have h' : (wa a0 ⟨BitVec.ofNat 64 (2 ^ (psA sz).val - 1)⟩).val < a0.val := h + rw [hlast] at h'; omega + rw [if_neg htop, if_neg hnl] + have hc2 : ¬ frC2A (mapDataA false asid0 va0) a0.bv (frameSizeOfA sz) := fun h => htop (hc2iff.1 h) + have hcan : CapRt.Canonical a0 := by + have hU : fl4_abi.consts.USER_TOP.val = 140737488355327 := by unfold fl4_abi.consts.USER_TOP; rfl + have : ¬ fl4_abi.consts.USER_TOP.val < (wa a0 ⟨BitVec.ofNat 64 (2 ^ (psA sz).val - 1)⟩).val := htop + rw [hlast, hU] at this + exact canon_ltA (by omega) + have hva : VaOkE a0 := by + have hU : fl4_abi.consts.USER_TOP.val = 140737488355327 := by unfold fl4_abi.consts.USER_TOP; rfl + have : ¬ fl4_abi.consts.USER_TOP.val < (wa a0 ⟨BitVec.ofNat 64 (2 ^ (psA sz).val - 1)⟩).val := htop + rw [hlast, hU] at this + exact Or.inl (by omega) + rw [if_neg hc2] + case' neg => + have hmp : mp = true := FP.mz.2 hz + subst hmp + simp only [show (asid0 != 0#u64) = true by simpa using hz, ↓reduceIte, Bool.false_eq_true] + by_cases ha1 : asid0 = asid1 + swap + · have hc1 : frC1A (mapDataA true asid0 va0) (asidOfA asid1) := + show asidOfA asid0 ≠ asidOfA asid1 from fun e => ha1 (asidOf_injA h16a hwf e) + simp only [show (asid0 != asid1) = true by simpa using ha1, ↓reduceIte, error.invalid_cap, bind_ok] + rw [if_pos hc1] + exact Or.inl ⟨_, _, rfl, rfl, errNR⟩ + subst asid0 + have hc1 : ¬ frC1A (mapDataA true asid1 va0) (asidOfA asid1) := fun h => h rfl + simp only [bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + rw [if_neg hc1] + by_cases hva : va0 = a0 + swap + · have hc2 : frC2A (mapDataA true asid1 va0) a0.bv (frameSizeOfA sz) := + show va0.bv ≠ a0.bv from fun e => hva (UScalar.eq_of_val_eq (congrArg BitVec.toNat e)) + simp only [show (va0 != a0) = true by simpa using hva, ↓reduceIte, error.invalid_arg, bind_ok] + rw [if_pos hc2] + exact Or.inl ⟨_, _, rfl, rfl, Or.inl rfl⟩ + subst va0 + have hc2 : ¬ frC2A (mapDataA true asid1 a0) a0.bv (frameSizeOfA sz) := fun h => h rfl + have hcan : CapRt.Canonical a0 := hcanV + have hva : VaOkE a0 := by + have h := E.j.1.cv _ (hab.isSlot E.j.1.place) + rw [hcap] at h + exact h rfl rfl + simp only [bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + rw [if_neg hc2] + all_goals + rw [hlu, bind_ok] + have hpb_iff : Fl4SpecA64.ptBitsLeft (lvA l) = (psA sz).val ↔ lu.shift = psA sz := by + rw [ptBitsLeft_lvA hl3, ← hsh] + exact ⟨fun e => UScalar.eq_of_val_eq e, fun e => by rw [e]⟩ + by_cases hls : lu.shift = psA sz + swap + · simp only [show (lu.shift != psA sz) = true by simpa using hls, ↓reduceIte] + rw [if_pos (fun e => hls (hpb_iff.1 e))] + exact Or.inl ⟨_, _, rfl, rfl, Or.inl ⟨rfl, show Fl4SpecA64.ptBitsLeft (lvA l) = lu.shift.val by rw [ptBitsLeft_lvA hl3, hsh]⟩⟩ + simp only [hls, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + rw [if_neg (not_not.2 (hpb_iff.2 hls))] + rw [rights_readableA, bind_ok, + show layout.paddr fp = ok (core.num.U64.wrapping_sub fp layout.PPTR_BASE) from rfl, bind_ok] + obtain ⟨e, hme, he⟩ := spec_ex_fd (mup_specA (core.num.U64.wrapping_sub fp layout.PPTR_BASE) vm a2 (psA sz) + (decide (Fl4SpecA64.Rights.allowRead ∈ vmRightsOfA rt))) + rw [hme, bind_ok] + -- the restart and the cap write + have hEnc : CapRt.EncWf (.Frame fp sz rt dev true false asid1 a0) := by + simp only [CapRt.EncWf]; exact ⟨hcanP, by omega, by omega, hcan⟩ + have hca : CapRelA (.Frame fp sz rt dev true false asid1 a0) (.archObjectCap (.frameCap (specPtr fp) + (vmRightsOfA rt) (frameSizeOfA sz) dev (some (asidOfA asid1, a0.bv)))) := + ⟨⟨hr1, hr3, ⟨hwf, by simp, fun _ => hasid1, by simp⟩, hov⟩, rfl⟩ + obtain ⟨x, s2, σ2, hkx, hst, hset, hsc, hσ2, HS, hksb2, R2, hcpu2, -, hJ2, hFr2⟩ := + restart_setcapJA E RS cte hab hos hEnc hca + rw [hst, bind_ok, hsc, bind_ok] + -- the descriptor + obtain ⟨hKal, hS1A, -⟩ := E.vsLeafA + obtain ⟨tq, hkq, htq, -⟩ := R.pt p hp + have hidx := idxA_lt a0 lu.shift + have hlvp : Z.Lv p = 3 - l := by omega + have hl2 : l ≤ 2 := by + have := congrArg UScalar.val hls; rw [hsh] at this; omega + have hk1 : 0 < Z.Lv p := by omega + have hpg : decide (psA sz = 12#u64) = decide (Z.Lv p = 3) := by + have e12 : (12#u64 : U64).val = 12 := rfl + apply decide_eq_decide.2 + constructor + · intro e; have := congrArg UScalar.val (hls.trans e); rw [hsh, e12] at this; omega + · intro e; apply UScalar.eq_of_val_eq; rw [← hls, hsh, e12]; omega + have hsm : decide (frameSizeOfA sz = .armSmallPage) = decide (Z.Lv p = 3) := by + rw [← hpg]; cases sz <;> decide + have hshA : shiftA (Z.Lv p) = (psA sz).val := by + unfold shiftA; have := congrArg UScalar.val hls; rw [hsh] at this; omega + rw [hpg] at he + have hwr : PteRelA Z.S1 Z.Lv (Z.Lv p) e (Fl4SpecA64.makeUserPte + (Fl4SpecA64.addrFromPPtr (specPtr fp)) (Fl4SpecA64.attribsFromWord a2.bv) + (Fl4SpecA64.maskVmRights (vmRightsOfA rt) (Fl4SpecA64.dataToRights a1.bv)) (vmRightsOfA rt) + (frameSizeOfA sz)) := by + unfold Fl4SpecA64.makeUserPte + rw [addr_specA, hsm, ← rightsW_specA, ← hvmr] + exact pteRel_mupA hk1 (R.lv p hp) rfl he (by rw [hshA]; exact FP.al) FP.pa + have hA : ¬ AsW (L.ksb s2) (peAF p (idxA a0 lu.shift)) := by rw [hksb2]; exact hS1A p hp _ hidx + have hK2 : Aligned (L.ksb s2) := by rw [hksb2]; exact hKal + obtain ⟨-, σ3, hstore, R4, -⟩ := write_storePteA R2 hp hidx hwr hA hK2 + (fun _ h => by unfold Fl4SpecA64.makeUserPte at h; cases h) + obtain ⟨t', hσ3⟩ := storePte_eqA R2 hp hidx hstore + -- `install_page` + have hsla : Aligned lu.slot := by rw [hsl]; exact peAF_alignedA (R.al_mod hp) hidx + obtain ⟨s', hks', hcpu', hm', hfl⟩ := install_pageA (L := L) ar s2 a lu e r0 asid1 a0 a2 hsla + -- the spec's perform + have hkq1 : (stUpdA σ σ.curThread x .restart).kheap (specPtr p) = some (.archObj (.pageTable tq)) := + stUpd_keep hkx hkq (fun _ h => by cases h) + have hlt : Fl4SpecA64.levelType (lvA l) = ptTyA (Z.Lv p) := by rw [levelType_lvA hl3, hlvp] + have hpte := ptesOf_atA hkq1 (ptT := Fl4SpecA64.levelType (lvA l)) (by rw [htq, hlt]) (R.al_mod hp) hidx + rw [← hsl] at hpte + rw [← hsl] at hstore + rw [← hlt] at hstore + obtain ⟨σ', hσ', hms⟩ := perfPgMap_memA (cap := .frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev + (some (asidOfA asid1, a0.bv))) rfl (getPte_runA hpte) hσ2 hstore + refine Or.inr ⟨s', (hfl _).bind (fun _ => rfl), _, rfl, _, hset, σ', hσ', ?_⟩ + have R5 : VsRelA Z.Pp Z.S1 Z.Lv Z.X Z.Y (L.mem s') (L.ksb (L.wr s2 lu.slot e)) σ3 := by + rw [hsl] + refine R4.of_mem fun b hb => ?_ + rw [← hsl] + exact hm' b (R4.foot_alignedA (by rw [L.ksb_wr]; exact hK2) b hb) + have VR : VsRelA Z.Pp Z.S1 Z.Lv Z.X Z.Y (L.mem s') (L.ksb s') σ' := by + rw [hks', ← L.ksb_wr s2 lu.slot e] + exact R5.of_msA hms + refine ⟨VR, asid1, a0, fun a' har hT hnul hTO _ => ?_⟩ + rw [hcap] + obtain ⟨J2, hT2⟩ := hJ2 hT hnul (by simp) (by rw [← capAtA_eq, hcap]; simp) + (UA.of_shapeA (by rw [← capAtA_eq, hcap] <;> rfl) fun h => by rw [← capAtA_eq, hcap] at h; simp [isUtA] at h) + ⟨fun _ h => (by cases h), fun _ _ _ _ _ h => (by cases h), fun _ _ _ h => (by cases h), fun _ _ _ h => (by cases h), fun _ _ => hva⟩ + subst hσ3 + exact (map_finJA (S1' := Z.S1) (Lv' := Z.Lv) E J2 hT2 hksb2 hcpu2 hp (hTO p hp) hidx hks' hcpu' + (fun w hw hne => by rw [hm' w hw, hsl, mem_wr_ne' (peAF_alignedA (R.al_mod hp) hidx) hw (Ne.symm hne)]) + hms (fun q hq => Or.inl hq) (fun _ hq hl => ⟨hq, hl⟩) VR hFr2 hos).imp_left fun J => J.of_arA har + | _ => + simp only [bind_ok, Bool.false_eq_true, ↓reduceIte, vspace.check_vspace, error.invalid_cap, + core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame.from, + bind_tc_ok, ite_self] + exact Or.inl ⟨_, _, rfl, nonroot _ hvc' (by simp) (by simp) (by simp), errNR⟩ + +/-- **Stage-1 `ARMPageMap` refines l4v** (module docstring; x86 `frame_map_rel`): `page_map_refinesGA`'s post +without the lift's obligation. -/ +theorem page_map_refinesA (ar : machine.Arch A) (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + {Rp Nt : Set U64} {extra : Aeneas.Std.Array obj.Slot 3#usize} {sp : U64} + (P : SysPlace st H Z.Cs H.frame Rp Nt OS s extra sp) (Y : SymL st H Z.Cs s) + (hrun : Runnable (L.mem s (stA (L.curW s)))) + (hcur : (L.mem s (inqA (L.curW s)) &&& sched.Q_RELEASE) = 0#u64 ∧ L.mem s (scA (L.curW s)) ≠ 0#u64 ∧ + L.mem s (scA (L.curW s)) ∈ Ac) + (hvs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → Aligned b ∧ b ≠ stA (L.curW s)) + (hvc : ∀ v ∈ Z.V, stA (L.curW s) ≠ v ∧ stA (L.curW s) ≠ wa v 24#u64) + (hsub : ∀ y, IsSlotA Z.th y → y ∈ OS) + (cte : Fl4SpecA64.CslotPtr) (hab : AbsSlotA Z.th cte) (hos : cslotAddrA cte ∈ OS) + {fp asid0 va0 rt : U64} {sz : capability.FrameSize} {dev mp : Bool} + (hcap : capAtA (L.mem s) (cslotAddrA cte) = .Frame fp sz rt dev mp false asid0 va0) + (FP : FrPlA sz fp mp asid0) + (slbl : Fl4SpecA64.Data) (hl : Fl4SpecA64.invocationType slbl = .archInvocationLabel .armPageMap) + (length buf : U64) (args : List U64) (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) (call : Bool) + (x0 : U64) (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) + (exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) (hex : ExtraRelA Z.th (L.mem s) x0 (exs.map Prod.fst)) + (hS1 : ∀ p l mp' asid va, capAtA (L.mem s) x0 ≠ .Ept p l mp' asid va) + (hHi : ∀ p mp' asid, capAtA (L.mem s) x0 ≠ .HighVSpace p mp' asid) : + PgMapRelA (L := L) ar Z σ ((Fl4SpecA64.decodeFrameInvocation (Z := Unit) slbl (args.map UScalar.bv) cte + (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev (mapDataA mp asid0 va0)) exs).run σ) s a + (vspace.decode_frame st ar s a fl4_abi.label.ARMPageMap length (cslotAddrA cte) + (.Frame fp sz rt dev mp false asid0 va0) call buf extra) := + (page_map_refinesGA ar E ⟨⟨_, _, _, _, P⟩, Y, hrun, hcur, hvs, hvc, hsub⟩ cte hab hos hcap FP slbl hl + length buf args hlen hargs call x0 hx0 exs hex hS1 hHi).weakA + +end top + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/VsRelA.lean b/proofs/Fl4ProofsA64/P3/VsRelA.lean new file mode 100644 index 000000000..aaf1146d5 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/VsRelA.lean @@ -0,0 +1,560 @@ +/- +P3 (aarch64, A64-P3 VS gen 1–4): **the A64 VSpace relation `VsRelA`** — FL4's AArch64 ASID table, ASID pools and +Stage-1 page tables against `Fl4SpecA64`'s `arm_asid_table`, `ASIDPool` objects and 4-level `PageTable` objects +(`AARCH64/VSpace`, `AARCH64/Structs`), with the `ArmHighVSpace` windows' level-1 tables (`Ext/ArmHighVSpace`), and +the hardware-root tie `HwTieA` of the ghost `Z.hw`. The twin of x86's `P3/VsRel` (`VsRel`: ASID table and pools) and +`P3/VsPt` (`PtRel`: the page tables of each level), in one record. + +**The AArch64 layout** (`vspace.rs`, `layout.rs`; PORTING.md "Concrete widths"): the ASID table is 128 words at +`ks + 772352` (`ks::ASID_TABLE`, `N_ASID_POOLS = 1 << 7`, l4v `asid_high_len = 7`); an ASID pool is a 4 KiB page of +512 words (l4v `asid_low_len = 9`), each `0` or a 4 KiB-aligned root with its kind in the low two bits (`ROOT_S1` = 0, +`ROOT_S2` = 1, `ROOT_HIGH` = 2); a page table is a 4 KiB page of 512 descriptors (`vs_index` = `pt_index` = 9 bits, +`config_ARM_PA_SIZE_BITS_40 = false`); FL4's walk depth `k` (0 for a root, 3 for the last level) is l4v's level +`3 - k` (`max_pt_level = 3`). The addresses are A64 P1's `atAF ksb hi` / `peAF p lo` (`VmRootRd`), the ghost sets are +A64 P1's `VsTy` vocabulary: pools `Pp`, page tables `Tb` with their depth `Lv`, and the joint relation's Stage-2 +roots `X` and high windows `Y` (`XSetsA`). + +* **`PteRelA Tb Lv k w pte`** (x86 `PML4ERel` … `PTERel`): descriptor word `w` at depth `k` against l4v's `pte`: + `InvalidPTE` ↔ valid bit clear; `PageTablePTE ppn` ↔ a table descriptor (`e & 3 = 3`) above the last level whose + address bits (`ADDR_MASK`) are below 2^44 and are `ppn`'s (`ppn_from_pptr`), naming (`table_of`: `pptr(e & + ADDR_MASK)`) a table of `Tb` one level down; `PagePTE` ↔ a page (`k = 3`, `e & 3 = 3`) or block (`k ∈ {1, 2}`, + `e & 3 = 1`) descriptor with l4v's `pte_page_addr` FL4's `page_base`, `pte_rights` FL4's `rights_of` (AP[2:1]), + and the attributes FL4's (`Execute` ↔ UXN clear, `Device` ↔ a non-normal MAIR index, never `Global`: nG is set). +* **`VsRelA Pp Tb Lv X Y mem ksb σ`** (x86 `VsRel` + `PtRel`): `table` (the spec's ASID table entry `hi` is FL4's + table word, 0 for none), `tmem` (nonzero table words are pools), `pool` (each pool is a spec `ASIDPool` whose + entry `lo` is FL4's word, untagged: `poolEntA`), `kind` (a nonzero entry's tag names a Stage-1 root of `Tb` at + depth 0, a Stage-2 root of `X` or a high window of `Y`: `EntKindA`), `pt` (each table of `Tb` is a spec `PageTable` + of type `VSRootPT_T` at depth 0, `NormalPT_T` below, whose 512 entries are `PteRelA` at its depth), `high` (each + window of `Y` is the spec's `ext (highVSpace l1)` whose 512 entries are its level-1 table's, `y + 4096`, at depth + 1), `lv` (depths at most 3), `al` (gen 3: every table of `Tb`, the Stage-1 roots included, is page-aligned, + `4096 ∣ p`; A64 P2's `VsLayA` alignment), and (gen 4) `pal` (pools page-aligned, `bits::ASID_POOL = 12`), `yal` + (windows 8 KiB-aligned, `bits::HIGH_VSPACE = 13`, so `y + 4096` is page-aligned and `≠ y`), `ytb` (a window's + level-1 table `y + 4096` is no Stage-1 table of `Tb`). Stage-2 roots' own entries are the next step (Stage-2 descriptor format, P1's + `QueueS2Ty`); `Stage2CoreA` already relates the objects. +* **The footprint `VsFootA Pp Tb Y ksb`** (x86 `VsLay`'s words): the 128 ASID-table words, the 512 words of every + pool, every table, and every window's level-1 table. Every such word is a word of A64 P1's `VsR ksb (VsPtA Pp Tb Y)` + (`VsFootA.vsR`: `PtW` page words or `AsW` table words), so the P1 frames over `VsR` / `VsZ` apply. In `FrameA`'s + vocabulary they are **non-slot** words: `VsRelA` adds one `FootNA` constructor (`vs`) at integration. +* **Frame lemmas**: `VsRelA.of_mem` (the footprint's words kept), **`VsRelA.of_writesIn`** (a step whose `WritesIn` + set misses the footprint: the implementation-side frame), `VsFootA.aligned` (the footprint is aligned at aligned + objects); spec side `VsRelA.of_spec` (the ASID table and the related objects kept) and (`P3/VsTieA`, gen 12) + `VsRelA.of_specSlot` (a `SpecSlotA` step, which changes only CNodes and TCB cap fields, keeps every related object). +* **`vmRootA σ t`** (l4v `set_vm_root`'s choice, x86 `Cr3`'s spec field): the root and ASID `set_vm_root t` installs + — the thread's slot-1 cap a mapped `VSRootPT_T` page-table cap whose ASID's `vspace_for_asid` is its table: that + table and ASID; else the global user VSpace with no ASID. **`HwTieA Z s2 σ`** (gen 2, ruling Q3 / Q4): the + Stage-1 tie `HwS1TieA` (`Z.hw = vmRootA σ σ.curThread`) and the Stage-2 tie `HwS2TieA s2 σ` (a thread with a bound + VCPU: the ghost VTTBR_EL2 root `s2` is `vmS2RootA`, FL4's `set_stage2_root` choice; vacuous otherwise), a + separate flat predicate beside `JAtZA` (false between a slot-1 change and the next `set_vm_root`, as in seL4). + +**Gen 2 (Architect ruling 13:38Z)**: objects are named by the shifted pointer `specPtr p` (`PtrA`, Q1) in `table` +(`optPtrA`), `pool` / `poolEntA`, `pt`, `high` and `VsObjPA`; new field **`asid0`** (Q5: the pool of ASID-table +entry 0 has entry 0 empty); `HwTieA` split (Q4). `Tb` here is the P3 Stage-1 ghost subset of A64 P1's `Tb` (Q2: +P1's `Tb` also holds the windows' level-0 tables and kernel tables, which are no spec `PageTable`s); integration +passes it as a separate ghost `S1Tb ⊆ Tb`. + +**Gen 3 (Architect ruling 14:12Z)**: new field **`al`** (every table of `Tb`, the Stage-1 roots included, is +page-aligned; `VsRelA.al_mod`), so the walk lemmas (`VsWalkA`) and the leaf helpers (`VsLeafA`) need no external +alignment hypothesis. No statement here assumes FL4's RAM below l4v's 511 GiB window (ruling: relations are relative +to `specPtr`'s image). + +**Gen 4 (Architect ruling 14:37Z)**: new fields **`pal`**, **`yal`**, **`ytb`**, so a Stage-1 table word is off the +pools and windows by construction (`VsLeafA`'s `VsRelA.slot_offA`); `w0` has no pools, tables or windows +(`VsBootA` vacuous). + +No named premise (definitions and lemmas only; the physical-memory bound `paddr < 2^44`, PORTING.md's **[P]**, is a +field of `PteRelA` to be established, not assumed). No `Fl4SpecA64` edit. +-/ +import Fl4ProofsA64.P3.PtrA +import Fl4ProofsA64.P1.VmRootRd +import Fl4ProofsA64.P1.QueueVsTy +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +/-! ## Descriptor fields (`vspace.rs`, Arm ARM D8.3) -/ + +/-- The address bits of a descriptor (`ADDR_MASK = 0x0000_ffff_ffff_f000`). -/ +def dAddrA (w : U64) : U64 := w &&& 281474976706560#u64 + +/-- The kernel pointer of the table a table descriptor names (`table_of`: `pptr(e & ADDR_MASK)`, `PPTR_BASE = +0xffff_8000_0000_0000`). -/ +def dTabA (w : U64) : U64 := wa (dAddrA w) 18446603336221196288#u64 + +/-- Bit `k` of a descriptor. -/ +def dBitA (w : U64) (k : Nat) : Bool := w.bv.getLsbD k + +/-- The shift of the addresses a slot at depth `k` maps (`TOP_LEVEL_SHIFT - 9 k`). -/ +def shiftA (k : Nat) : Nat := 39 - 9 * k + +/-- A page or block descriptor's output address (`page_base`: the address bits above the shift). -/ +def pageBaseA (w : U64) (k : Nat) : Fl4SpecA64.Paddr := + (dAddrA w).bv &&& ~~~(BitVec.ofNat 64 (2 ^ shiftA k - 1)) + +/-- A table descriptor's `ppn` (l4v `ppn_from_pptr`: the address bits from 12 to 44). -/ +def ppnA (w : U64) : Fl4SpecA64.Ppn := BitVec.ofNat 32 ((dAddrA w).val / 4096) + +/-- AP[2:1] of a descriptor (bits 6–7). -/ +def apA (w : U64) : Nat := (w.val / 64) % 4 + +/-- The rights of a page or block descriptor (`rights_of`, seL4 `vmRightsFromPTE`: AP 1 read-write, 3 read-only, +else kernel only). -/ +def pteRightsA (w : U64) : Fl4SpecA64.VMRights := + if apA w = 1 then Fl4SpecA64.vmReadWrite else if apA w = 3 then Fl4SpecA64.vmReadOnly + else Fl4SpecA64.vmKernelOnly + +/-- The MAIR index of a descriptor (AttrIndx, bits 2–4; FL4's `ATTR_NORMAL = 0`). -/ +def attrIdxA (w : U64) : Nat := (w.val / 4) % 8 + +/-- The attributes of a page or block descriptor (`make_user_page`): `Execute` iff UXN (bit 54) is clear, `Device` +iff the MAIR index is not normal memory, never `Global` (FL4 sets nG, l4v's `make_user_pte` drops `Global`). -/ +structure PteAttrsA (w : U64) (a : Fl4SpecA64.VMAttributes) : Prop where + exec : Fl4SpecA64.VMAttribute.execute ∈ a ↔ dBitA w 54 = false + dev : Fl4SpecA64.VMAttribute.device ∈ a ↔ attrIdxA w ≠ 0 + glob : Fl4SpecA64.VMAttribute.global ∉ a + +/-- The l4v table type at FL4's depth `k` (`level_type (3 - k)`: the root `VSRootPT_T`, the rest `NormalPT_T`). -/ +def ptTyA (k : Nat) : Fl4SpecA64.PtType := if k = 0 then .vsRootPtT else .normalPtT + +/-- **A descriptor at depth `k` against l4v's `pte`** (module docstring; x86 `PML4ERel` … `PTERel`). -/ +def PteRelA (Tb : Set U64) (Lv : U64 → Nat) (k : Nat) (w : U64) : Fl4SpecA64.PTE → Prop + | .invalidPTE => dBitA w 0 = false + | .pageTablePTE ppn => k < 3 ∧ dBitA w 0 = true ∧ dBitA w 1 = true ∧ (dAddrA w).val < 2 ^ 44 ∧ ppn = ppnA w ∧ + dTabA w ∈ Tb ∧ Lv (dTabA w) = k + 1 + | .pagePTE a small attrs rights => 0 < k ∧ k ≤ 3 ∧ dBitA w 0 = true ∧ (dBitA w 1 = true ↔ k = 3) ∧ + small = decide (k = 3) ∧ a = pageBaseA w k ∧ (dAddrA w).val < 2 ^ 44 ∧ rights = pteRightsA w ∧ + PteAttrsA w attrs + +/-- A zero word is the invalid descriptor and nothing else. -/ +theorem pteRelA_zero {Tb : Set U64} {Lv : U64 → Nat} {k : Nat} {pte : Fl4SpecA64.PTE} + (h : PteRelA Tb Lv k 0#u64 pte) : pte = .invalidPTE := by + cases pte with + | invalidPTE => rfl + | pageTablePTE _ => exact absurd h.2.1 (by decide) + | pagePTE _ _ _ _ => exact absurd h.2.2.1 (by decide) + +theorem pteRelA_zero_inv (Tb : Set U64) (Lv : U64 → Nat) (k : Nat) : PteRelA Tb Lv k 0#u64 .invalidPTE := by + show dBitA 0#u64 0 = false; decide + +/-! ## ASID pool entries -/ + +/-- An entry's root, its tag cleared (`raw & !ROOT_TAG`). -/ +def untagA (w : U64) : U64 := w &&& 18446744073709551612#u64 + +/-- An entry's tag (`raw & ROOT_TAG`). -/ +def tagA (w : U64) : Nat := w.val % 4 + +/-- **FL4's pool entry as l4v's `asid_pool_entry`**: `0` is none, else `ASIDPoolVSpace` of the untagged root (the +kind is the object's, `Ext/Stage2` `rootKindAt`). -/ +def poolEntA (w : U64) : Option Fl4SpecA64.AsidPoolEntry := + if w = 0#u64 then none else some (.asidPoolVSpace (specPtr (untagA w))) + +/-- **FL4's ASID-table word as l4v's `arm_asid_table` entry** (gen 2): `0` is none, else the pool's pointer shifted +(`specPtr`, `PtrA`). -/ +def optPtrA (w : U64) : Option Fl4SpecA64.ObjRef := if w = 0#u64 then none else some (specPtr w) + +/-- **A pool entry's kind** (module docstring): `0`, or tag `ROOT_S1` naming a root of `Tb` (depth 0), tag +`ROOT_S2` a Stage-2 root of `X`, tag `ROOT_HIGH` a high window of `Y`. -/ +def EntKindA (Tb : Set U64) (Lv : U64 → Nat) (X Y : Set U64) (w : U64) : Prop := + w = 0#u64 ∨ (tagA w = 0 ∧ untagA w ∈ Tb ∧ Lv (untagA w) = 0) ∨ (tagA w = 1 ∧ untagA w ∈ X) ∨ + (tagA w = 2 ∧ untagA w ∈ Y) + +/-! ## Reachability from `asidTable` (Design 1A) -/ + +/-- A pool installed in a live ASID-table slot (`0..127`). -/ +def ActivePoolA (mem : U64 → U64) (ksb p : U64) : Prop := + p ≠ 0#u64 ∧ ∃ hi : U64, hi.val < 128 ∧ mem (atAF ksb hi) = p + +/-- A nonzero pool word installed at `asid = hi * 512 + lo` in an active pool of `Pp`. -/ +def ActiveAsidRootA (mem : U64 → U64) (ksb : U64) (Pp : Set U64) (asid raw : U64) : Prop := + ∃ hi lo : U64, hi.val < 128 ∧ lo.val < 512 ∧ asid.val = hi.val * 512 + lo.val ∧ + ∃ pool ∈ Pp, pool ≠ 0#u64 ∧ mem (atAF ksb hi) = pool ∧ mem (peAF pool lo) = raw ∧ raw ≠ 0#u64 + +theorem ActiveAsidRootA.activePool {mem : U64 → U64} {ksb : U64} {Pp : Set U64} {asid raw : U64} + (h : ActiveAsidRootA mem ksb Pp asid raw) : + ∃ pool ∈ Pp, ActivePoolA mem ksb pool ∧ ∃ lo : U64, lo.val < 512 ∧ mem (peAF pool lo) = raw ∧ raw ≠ 0#u64 := by + obtain ⟨hi, lo, hhi, hlo, -, pool, hp, hpne, htab, hw, hrne⟩ := h + exact ⟨pool, hp, ⟨hpne, hi, hhi, htab⟩, lo, hlo, hw, hrne⟩ + +/-- An `ArmHighVSpace` window `y` installed at a tag-2 (`ROOT_HIGH`) active ASID entry. -/ +def ActiveHighRootA (mem : U64 → U64) (ksb : U64) (Pp : Set U64) (y : U64) : Prop := + ∃ asid raw, ActiveAsidRootA mem ksb Pp asid raw ∧ tagA raw = 2 ∧ untagA raw = y + +/-- Pure `(mem, ksb)` Stage-1 / `ArmHighVSpace` walk path from `asidTable` (matching P1's `S1aPath` for tags 0 and 2). -/ +inductive VsPathA (Pp Tb Y : Set U64) (mem : U64 → U64) (ksb : U64) : U64 → Nat → U64 → Prop + | native {asid raw : U64} : + ActiveAsidRootA mem ksb Pp asid raw → tagA raw = 0 → raw ∈ Tb → + VsPathA Pp Tb Y mem ksb 0#u64 39 raw + | high {asid raw : U64} : + ActiveAsidRootA mem ksb Pp asid raw → tagA raw = 2 → untagA raw ∈ Y → + VsPathA Pp Tb Y mem ksb 2#u64 30 (wa (untagA raw) 4096#u64) + | child {tag t i : U64} {sh : Nat} : + VsPathA Pp Tb Y mem ksb tag sh t → + (t ∈ Tb ∨ ∃ y ∈ Y, t = wa y 4096#u64) → + 12 < sh → i.val < 512 → + (mem (peAF t i) &&& 3#u64) = 3#u64 → + VsPathA Pp Tb Y mem ksb tag (sh - 9) (dTabA (mem (peAF t i))) + +/-- A page table reachable from `asidTable` via a Stage-1 (`tag = 0`) or `ArmHighVSpace` (`tag = 2`) walk. -/ +def VsReachA (Pp Tb Y : Set U64) (mem : U64 → U64) (ksb p : U64) : Prop := + ∃ tag sh, (tag = 0#u64 ∨ tag = 2#u64) ∧ VsPathA Pp Tb Y mem ksb tag sh p + +private theorem nat_and_hiA_vs (n k : Nat) (hn : n < 2 ^ 64) (hk : k ≤ 64) : + n &&& (2 ^ 64 - 1 - (2 ^ k - 1)) = 2 ^ k * (n / 2 ^ k) := by + have h1 : 1 ≤ 2 ^ k := Nat.one_le_two_pow + have h2 : 2 ^ k * 2 ^ (64 - k) = 2 ^ 64 := by rw [← Nat.pow_add, Nat.add_sub_cancel' hk] + have e : (2 ^ 64 - 1 - (2 ^ k - 1) : Nat) = 2 ^ k * (2 ^ (64 - k) - 1) := by + rw [Nat.mul_sub_one, h2]; omega + apply Nat.eq_of_testBit_eq + intro i + rw [Nat.testBit_and, e, Nat.testBit_two_pow_mul, Nat.testBit_two_pow_mul, Nat.testBit_two_pow_sub_one, + Nat.testBit_div_two_pow] + by_cases h : k ≤ i + · have e2 : i - k + k = i := by omega + rw [e2] + by_cases hlt : i < 64 + · simp [h, show i - k < 64 - k by omega] + · have e3 : n.testBit i = false := + Nat.testBit_lt_two_pow (lt_of_lt_of_le hn (Nat.pow_le_pow_right (by decide) (by omega))) + simp [h, e3] + · simp [h] + +private theorem untagA_of_vs {w : U64} (h : tagA w = 0) : untagA w = w := by + apply UScalar.eq_of_val_eq + unfold untagA + rw [UScalar.val_and, show (18446744073709551612#u64 : U64).val = 2 ^ 64 - 1 - (2 ^ 2 - 1) from rfl, + nat_and_hiA_vs w.val 2 w.bv.isLt (by decide)] + unfold tagA at h + have e : (2 : Nat) ^ 2 = 4 := rfl + rw [e]; omega + +private theorem and3_bitsA_vs (e : U64) : (e &&& 3#u64) = 3#u64 ↔ dBitA e 0 = true ∧ dBitA e 1 = true := by + have d0 : dBitA e 0 = decide (e.val % 2 = 1) := by + unfold dBitA; rw [BitVec.getLsbD, Nat.testBit_zero]; rfl + have d1 : dBitA e 1 = decide (e.val / 2 % 2 = 1) := by + unfold dBitA; rw [BitVec.getLsbD, ← Nat.zero_add 1, ← Nat.testBit_div_two_pow, Nat.testBit_zero]; rfl + rw [d0, d1] + simp only [decide_eq_true_eq] + have h3v : (e &&& 3#u64).val = e.val % 4 := by + rw [UScalar.val_and, show (3#u64 : U64).val = 2 ^ 2 - 1 from rfl, Nat.and_two_pow_sub_one_eq_mod] + have c3 : (3#u64 : U64).val = 3 := rfl + constructor + · intro h; have := congrArg UScalar.val h; rw [h3v, c3] at this; omega + · rintro ⟨h0, h1⟩; apply UScalar.eq_of_val_eq; rw [h3v, c3]; omega + +/-! ## The relation -/ + +/-- **The A64 VSpace relation** (module docstring; x86 `VsRel` with `PtRel`; Design 1A reachability guards). -/ +structure VsRelA (Pp Tb : Set U64) (Lv : U64 → Nat) (X Y : Set U64) (mem : U64 → U64) (ksb : U64) + (σ : Fl4SpecA64.State Unit) : Prop where + table : ∀ hi : U64, hi.val < 128 → + σ.archState.armAsidTable (BitVec.ofNat 7 hi.val) = optPtrA (mem (atAF ksb hi)) + tmem : ∀ hi : U64, hi.val < 128 → mem (atAF ksb hi) ≠ 0#u64 → mem (atAF ksb hi) ∈ Pp + pool : ∀ p ∈ Pp, ∃ f, σ.kheap (specPtr p) = some (.archObj (.asidPool f)) ∧ + (ActivePoolA mem ksb p → ∀ lo : U64, lo.val < 512 → f (BitVec.ofNat 9 lo.val) = poolEntA (mem (peAF p lo))) + kind : ∀ p ∈ Pp, ActivePoolA mem ksb p → ∀ lo : U64, lo.val < 512 → EntKindA Tb Lv X Y (mem (peAF p lo)) + asid0 : mem (atAF ksb 0#u64) ≠ 0#u64 → mem (peAF (mem (atAF ksb 0#u64)) 0#u64) = 0#u64 + pt : ∀ p ∈ Tb, ∃ t, σ.kheap (specPtr p) = some (.archObj (.pageTable t)) ∧ Fl4SpecA64.ptType t = ptTyA (Lv p) ∧ + (VsReachA Pp Tb Y mem ksb p → ∀ i : U64, i.val < 512 → PteRelA Tb Lv (Lv p) (mem (peAF p i)) (Fl4SpecA64.ptApply t i.bv)) + high : ∀ y ∈ Y, ∃ l1, σ.kheap (specPtr y) = some (.archObj (.ext (.highVSpace l1))) ∧ + (ActiveHighRootA mem ksb Pp y → ∀ i : U64, (h : i.val < 512) → PteRelA Tb Lv 1 (mem (peAF (wa y 4096#u64) i)) (l1 ⟨i.val, h⟩)) + lv : ∀ p ∈ Tb, Lv p ≤ 3 + al : ∀ p ∈ Tb, 4096 ∣ p.val + pal : ∀ p ∈ Pp, 4096 ∣ p.val + yal : ∀ y ∈ Y, 8192 ∣ y.val + ytb : ∀ y ∈ Y, wa y 4096#u64 ∉ Tb + +/-- Every `VsPathA` node under `VsRelA` is either a `Tb` table at shift `39 - 9 * Lv q` or an active high window's L1 +table at shift `30`. -/ +theorem VsRelA.path_props {Pp Tb : Set U64} {Lv : U64 → Nat} {X Y : Set U64} {mem : U64 → U64} {ksb : U64} + {σ : Fl4SpecA64.State Unit} (R : VsRelA Pp Tb Lv X Y mem ksb σ) {tag : U64} {sh : Nat} {q : U64} + (hp : VsPathA Pp Tb Y mem ksb tag sh q) : + (tag = 0#u64 ∨ tag = 2#u64) ∧ + ((q ∈ Tb ∧ Lv q ≤ 3 ∧ sh = 39 - 9 * Lv q) ∨ + (tag = 2#u64 ∧ sh = 30 ∧ ∃ y ∈ Y, ActiveHighRootA mem ksb Pp y ∧ q = wa y 4096#u64)) := by + induction hp with + | @native asid raw hact htag htb => + obtain ⟨pool, hpool, hactP, lo, hlo, hw, hrne⟩ := hact.activePool + have hk := R.kind pool hpool hactP lo hlo + rw [hw] at hk + rcases hk with rfl | ⟨-, -, hlv0⟩ | ⟨h1, -⟩ | ⟨h2, -⟩ + · exact absurd rfl hrne + · rw [untagA_of_vs htag] at hlv0 + exact ⟨Or.inl rfl, Or.inl ⟨htb, R.lv _ htb, by omega⟩⟩ + · omega + · omega + | @high asid raw hact htag hy => + exact ⟨Or.inr rfl, Or.inr ⟨rfl, rfl, untagA raw, hy, ⟨asid, raw, hact, htag, rfl⟩, rfl⟩⟩ + | @child tag t i sh hpar _ h12 hi h3 ih => + obtain ⟨htag, hcase⟩ := ih + obtain ⟨hb0, hb1⟩ := (and3_bitsA_vs _).1 h3 + rcases hcase with ⟨ht, hlv, hsh⟩ | ⟨rfl, rfl, y, hy, hactY, rfl⟩ + · obtain ⟨pt, -, -, hpt⟩ := R.pt t ht + have hrel := hpt ⟨tag, sh, htag, hpar⟩ i hi + generalize Fl4SpecA64.ptApply pt i.bv = pte at hrel + cases pte with + | invalidPTE => rw [hrel] at hb0; cases hb0 + | pageTablePTE _ => + obtain ⟨-, -, -, -, -, hdTb, hdLv⟩ := hrel + exact ⟨htag, Or.inl ⟨hdTb, R.lv _ hdTb, by omega⟩⟩ + | pagePTE _ _ _ _ => + obtain ⟨-, -, -, hiff, -⟩ := hrel + have := hiff.1 hb1; omega + · obtain ⟨l1, -, hl1⟩ := R.high y hy + have hrel := hl1 hactY i hi + generalize l1 ⟨i.val, hi⟩ = pte at hrel + cases pte with + | invalidPTE => rw [hrel] at hb0; cases hb0 + | pageTablePTE _ => + obtain ⟨-, -, -, -, -, hdTb, hdLv⟩ := hrel + exact ⟨Or.inr rfl, Or.inl ⟨hdTb, R.lv _ hdTb, by omega⟩⟩ + | pagePTE _ _ _ _ => + obtain ⟨-, -, -, hiff, -⟩ := hrel + have := hiff.1 hb1; omega + +/-- A related table is page-aligned (the field `al`, as a remainder). -/ +theorem VsRelA.al_mod {Pp Tb : Set U64} {Lv : U64 → Nat} {X Y : Set U64} {mem : U64 → U64} {ksb : U64} + {σ : Fl4SpecA64.State Unit} (R : VsRelA Pp Tb Lv X Y mem ksb σ) {p : U64} (hp : p ∈ Tb) : p.val % 4096 = 0 := + Nat.mod_eq_zero_of_dvd (R.al p hp) + +/-! ## The footprint -/ + +/-- **The words `VsRelA` reads** (module docstring). -/ +inductive VsFootA (Pp Tb Y : Set U64) (ksb : U64) : U64 → Prop + | asid (hi : U64) : hi.val < 128 → VsFootA Pp Tb Y ksb (atAF ksb hi) + | pool (p lo : U64) : p ∈ Pp → lo.val < 512 → VsFootA Pp Tb Y ksb (peAF p lo) + | pt (p i : U64) : p ∈ Tb → i.val < 512 → VsFootA Pp Tb Y ksb (peAF p i) + | high (y i : U64) : y ∈ Y → i.val < 512 → VsFootA Pp Tb Y ksb (peAF (wa y 4096#u64) i) + +/-- The pages `VsRelA` reads (pools, tables, the windows' level-1 tables), as A64 P1's `PtW` set. -/ +def VsPtA (Pp Tb Y : Set U64) : Set U64 := {p | p ∈ Pp ∨ p ∈ Tb ∨ ∃ y ∈ Y, p = wa y 4096#u64} + +theorem wm8_valA {x : U64} (hx : x.val < 512) : (core.num.U64.wrapping_mul x 8#u64).val = 8 * x.val := by + have := mul8_val (x := x) (by omega); omega + +/-- **The footprint is A64 P1's `VsR` vocabulary** (module docstring): every word is a `PtW` page word of `VsPtA` +or an `AsW` ASID-table word. -/ +theorem VsFootA.vsR {Pp Tb Y : Set U64} {ksb b : U64} (h : VsFootA Pp Tb Y ksb b) : VsR ksb (VsPtA Pp Tb Y) b := by + cases h with + | asid hi hh => + refine Or.inr ⟨wa 772352#u64 (core.num.U64.wrapping_mul hi 8#u64), ?_, ?_, ?_, rfl⟩ <;> + simp [core.num.U64.wrapping_add_val_eq, core.num.U64.wrapping_mul_val_eq, U64.size_def, U64.numBits] <;> + omega + | pool p lo hp hl => + exact Or.inl ⟨p, Or.inl hp, _, by rw [wm8_valA hl]; omega, by rw [wm8_valA hl]; omega, rfl⟩ + | pt p i hp hi => + exact Or.inl ⟨p, Or.inr (Or.inl hp), _, by rw [wm8_valA hi]; omega, by rw [wm8_valA hi]; omega, rfl⟩ + | high y i hy hi => + exact Or.inl ⟨_, Or.inr (Or.inr ⟨y, hy, rfl⟩), _, by rw [wm8_valA hi]; omega, by rw [wm8_valA hi]; omega, rfl⟩ + +theorem peAF_valA {p lo : U64} (hp : p.val + 4096 ≤ 2 ^ 64) (hl : lo.val < 512) : + (peAF p lo).val = p.val + 8 * lo.val := by + unfold peAF + rw [wa_valP, wm8_valA hl] + omega + +theorem atAF_valA {kb hi : U64} (hk : kb.val + 788736 ≤ 2 ^ 64) (hh : hi.val < 128) : + (atAF kb hi).val = kb.val + 772352 + 8 * hi.val := by + have hkb := kb.hBounds + have hib := hi.hBounds + unfold atAF + simp [core.num.U64.wrapping_add_val_eq, core.num.U64.wrapping_mul_val_eq, U64.size_def, U64.numBits, + UScalar.size] at hkb hib ⊢ + omega + +/-- **The footprint is aligned** at an aligned kernel-state block and page-aligned pools, tables and windows (A64 +P1's `VsLayA.al`; the windows are 8 KiB objects, `bits::HIGH_VSPACE = 13`). -/ +theorem VsFootA.aligned {Pp Tb Y : Set U64} {ksb : U64} (hk : ksb.val % 8 = 0 ∧ ksb.val + 788736 ≤ 2 ^ 64) + (hP : ∀ p, p ∈ Pp ∨ p ∈ Tb → p.val % 4096 = 0 ∧ p.val + 4096 ≤ 2 ^ 64) + (hY : ∀ y ∈ Y, y.val % 4096 = 0 ∧ y.val + 8192 ≤ 2 ^ 64) : + ∀ b, VsFootA Pp Tb Y ksb b → Aligned b := by + intro b h + cases h with + | asid hi hh => show _ % 8 = 0; rw [atAF_valA hk.2 hh]; omega + | pool p lo hp hl => + have := hP p (Or.inl hp); show _ % 8 = 0; rw [peAF_valA this.2 hl]; omega + | pt p i hp hi => + have := hP p (Or.inr hp); show _ % 8 = 0; rw [peAF_valA this.2 hi]; omega + | high y i hy hi => + have := hY y hy + have e : (wa y 4096#u64).val = y.val + 4096 := by + rw [wa_valP] + have : (4096#u64 : U64).val = 4096 := rfl + rw [this]; omega + show _ % 8 = 0 + rw [peAF_valA (by omega) hi, e]; omega + +theorem peAF_alignedA {p i : U64} (hp : p.val % 4096 = 0) (hi : i.val < 512) : Aligned (peAF p i) := by + have bp : p.val < 2 ^ 64 := p.bv.isLt + show _ % 8 = 0 + rw [peAF_valA (by omega) hi] + omega + +section al +variable {Pp Tb : Set U64} {Lv : U64 → Nat} {X Y : Set U64} {ksb : U64} {σ : Fl4SpecA64.State Unit} + +/-- A window's level-1 table `y + 4096` does not wrap (gen 4: windows are 8 KiB-aligned, `VsRelA.yal`). -/ +theorem waWin_valA {y : U64} (h : 8192 ∣ y.val) : (core.num.U64.wrapping_add y 4096#u64).val = y.val + 4096 := by + have hb : y.val < 2 ^ 64 := y.bv.isLt + rw [wa_valP] + have e : (4096#u64 : U64).val = 4096 := rfl + rw [e] + omega + +theorem waWin_modA {y : U64} (h : 8192 ∣ y.val) : (core.num.U64.wrapping_add y 4096#u64).val % 4096 = 0 := by + rw [waWin_valA h]; omega + +/-- **The footprint is aligned** (VS gen 4; gen 12: moved here from `P3/VsLeafA` for `FrameA`) at an aligned kernel-state base: pools, tables and windows by `VsRelA`'s +fields `pal` / `al` / `yal`, with no bound on the objects (`4096 ∣ 2^64`). -/ +theorem VsRelA.foot_alignedA {mem : U64 → U64} (R : VsRelA Pp Tb Lv X Y mem ksb σ) (hk : Aligned ksb) : + ∀ b, VsFootA Pp Tb Y ksb b → Aligned b := by + intro b h + cases h with + | asid hi hh => + have hk' : ksb.val % 8 = 0 := hk + show _ % 8 = 0 + unfold atAF + rw [wa_valP, wa_valP, wm8_valA (by omega)] + have e : (772352#u64 : U64).val = 772352 := rfl + rw [e] + omega + | pool p lo hp hl => exact peAF_alignedA (Nat.mod_eq_zero_of_dvd (R.pal p hp)) hl + | pt p i hp hi => exact peAF_alignedA (R.al_mod hp) hi + | high y i hy hi => exact peAF_alignedA (waWin_modA (R.yal y hy)) hi + +end al + +/-! ## Frame lemmas, implementation side -/ + +section frame +variable {Pp Tb : Set U64} {Lv : U64 → Nat} {X Y : Set U64} {ksb : U64} {σ : Fl4SpecA64.State Unit} + +theorem ActivePoolA.of_mem {mem mem' : U64 → U64} {p : U64} + (h : ∀ b, VsFootA Pp Tb Y ksb b → mem' b = mem b) + (ha : ActivePoolA mem' ksb p) : ActivePoolA mem ksb p := by + obtain ⟨hp, hi, hhi, htab⟩ := ha + exact ⟨hp, hi, hhi, by rw [← h _ (.asid hi hhi)]; exact htab⟩ + +theorem ActiveAsidRootA.of_mem {mem mem' : U64 → U64} {asid raw : U64} + (h : ∀ b, VsFootA Pp Tb Y ksb b → mem' b = mem b) + (ha : ActiveAsidRootA mem' ksb Pp asid raw) : ActiveAsidRootA mem ksb Pp asid raw := by + obtain ⟨hi, lo, hhi, hlo, ha', pool, hp, hpne, htab, hw, hrne⟩ := ha + refine ⟨hi, lo, hhi, hlo, ha', pool, hp, hpne, ?_, ?_, hrne⟩ + · rw [← h _ (.asid hi hhi)]; exact htab + · rw [← h _ (.pool pool lo hp hlo)]; exact hw + +theorem ActiveHighRootA.of_mem {mem mem' : U64 → U64} {y : U64} + (h : ∀ b, VsFootA Pp Tb Y ksb b → mem' b = mem b) + (ha : ActiveHighRootA mem' ksb Pp y) : ActiveHighRootA mem ksb Pp y := by + obtain ⟨asid, raw, hact, htag, hy⟩ := ha + exact ⟨asid, raw, hact.of_mem h, htag, hy⟩ + +theorem VsPathA.of_mem {mem mem' : U64 → U64} {tag : U64} {sh : Nat} {p : U64} + (h : ∀ b, VsFootA Pp Tb Y ksb b → mem' b = mem b) + (hp : VsPathA Pp Tb Y mem' ksb tag sh p) : VsPathA Pp Tb Y mem ksb tag sh p := by + induction hp with + | native hact htag htb => exact .native (hact.of_mem h) htag htb + | high hact htag hy => exact .high (hact.of_mem h) htag hy + | @child tag t i sh _ htY h12 hi h3 ih => + have hpe : mem' (peAF t i) = mem (peAF t i) := by + rcases htY with ht | ⟨y, hy, rfl⟩ + · exact h _ (.pt t i ht hi) + · exact h _ (.high y i hy hi) + rw [hpe] at h3 ⊢ + exact .child ih htY h12 hi h3 + +theorem VsReachA.of_mem {mem mem' : U64 → U64} {p : U64} + (h : ∀ b, VsFootA Pp Tb Y ksb b → mem' b = mem b) + (hr : VsReachA Pp Tb Y mem' ksb p) : VsReachA Pp Tb Y mem ksb p := by + obtain ⟨tag, sh, htag, hp⟩ := hr + exact ⟨tag, sh, htag, hp.of_mem h⟩ + +/-- **`VsRelA` reads only its footprint** (x86 `VsRel` / `PtRel` frames). -/ +theorem VsRelA.of_mem {mem mem' : U64 → U64} (R : VsRelA Pp Tb Lv X Y mem ksb σ) + (h : ∀ b, VsFootA Pp Tb Y ksb b → mem' b = mem b) : VsRelA Pp Tb Lv X Y mem' ksb σ where + table hi hh := by rw [h _ (.asid hi hh)]; exact R.table hi hh + tmem hi hh h0 := by + rw [h _ (.asid hi hh)] at h0 ⊢; exact R.tmem hi hh h0 + pool p hp := by + obtain ⟨f, hk, hf⟩ := R.pool p hp + exact ⟨f, hk, fun ha lo hl => by rw [h _ (.pool p lo hp hl)]; exact hf (ha.of_mem h) lo hl⟩ + kind p hp ha lo hl := by rw [h _ (.pool p lo hp hl)]; exact R.kind p hp (ha.of_mem h) lo hl + asid0 h0 := by + rw [h _ (.asid 0#u64 (by decide))] at h0 ⊢ + rw [h _ (.pool _ 0#u64 (R.tmem 0#u64 (by decide) h0) (by decide))] + exact R.asid0 h0 + pt p hp := by + obtain ⟨t, hk, ht, he⟩ := R.pt p hp + exact ⟨t, hk, ht, fun hr i hi => by rw [h _ (.pt p i hp hi)]; exact he (hr.of_mem h) i hi⟩ + high y hy := by + obtain ⟨l1, hk, he⟩ := R.high y hy + exact ⟨l1, hk, fun ha i hi => by rw [h _ (.high y i hy hi)]; exact he (ha.of_mem h) i hi⟩ + lv := R.lv + al := R.al + pal := R.pal + yal := R.yal + ytb := R.ytb + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **The implementation-side frame** (module docstring): a step that writes only `W` words (A64 P1's `WritesIn`), +none of them a footprint word, keeps `VsRelA` (the kernel base is kept by `WritesIn`). -/ +theorem VsRelA.of_writesIn {s s' : S} (R : VsRelA Pp Tb Lv X Y (L.mem s) (L.ksb s) σ) {W : U64 → Prop} + (hw : WritesIn st s s' W) (hW : ∀ b, W b → ¬ VsFootA Pp Tb Y (L.ksb s) b) + (hal : ∀ b, VsFootA Pp Tb Y (L.ksb s) b → Aligned b) : VsRelA Pp Tb Lv X Y (L.mem s') (L.ksb s') σ := by + rw [hw.1] + exact R.of_mem fun b hb => hw.2.2 b (hal b hb) fun e => hW b e hb + +end + +/-! ## Frame lemmas, spec side -/ + +/-- An object `VsRelA` relates: a pool, a table or a window. -/ +def VsObjPA (Pp Tb Y : Set U64) (p : Fl4SpecA64.ObjRef) : Prop := + (∃ q ∈ Pp, specPtr q = p) ∨ (∃ q ∈ Tb, specPtr q = p) ∨ (∃ q ∈ Y, specPtr q = p) + +/-- **The spec-side frame**: the ASID table and the related objects kept. -/ +theorem VsRelA.of_spec {mem : U64 → U64} {σ' : Fl4SpecA64.State Unit} (R : VsRelA Pp Tb Lv X Y mem ksb σ) + (ht : σ'.archState.armAsidTable = σ.archState.armAsidTable) + (hk : ∀ p, VsObjPA Pp Tb Y p → σ'.kheap p = σ.kheap p) : VsRelA Pp Tb Lv X Y mem ksb σ' where + table hi hh := by rw [ht]; exact R.table hi hh + tmem := R.tmem + pool p hp := by rw [hk _ (Or.inl ⟨p, hp, rfl⟩)]; exact R.pool p hp + kind := R.kind + asid0 := R.asid0 + pt p hp := by rw [hk _ (Or.inr (Or.inl ⟨p, hp, rfl⟩))]; exact R.pt p hp + high y hy := by rw [hk _ (Or.inr (Or.inr ⟨y, hy, rfl⟩))]; exact R.high y hy + lv := R.lv + al := R.al + pal := R.pal + yal := R.yal + ytb := R.ytb + +/-- The related objects are no CNodes. -/ +theorem VsRelA.not_cnode {mem : U64 → U64} (R : VsRelA Pp Tb Lv X Y mem ksb σ) {p : Fl4SpecA64.ObjRef} + (hp : VsObjPA Pp Tb Y p) {r : Nat} {cs : Fl4SpecA64.CnodeContents} : σ.kheap p ≠ some (.cnode r cs) := by + intro e + rcases hp with ⟨q, hq, rfl⟩ | ⟨q, hq, rfl⟩ | ⟨q, hq, rfl⟩ + · obtain ⟨f, hk, -⟩ := R.pool q hq; rw [hk] at e; cases e + · obtain ⟨t, hk, -⟩ := R.pt q hq; rw [hk] at e; cases e + · obtain ⟨l1, hk, -⟩ := R.high q hq; rw [hk] at e; cases e + +/-- The related objects are no TCBs. -/ +theorem VsRelA.not_tcb {mem : U64 → U64} (R : VsRelA Pp Tb Lv X Y mem ksb σ) {p : Fl4SpecA64.ObjRef} + (hp : VsObjPA Pp Tb Y p) {x : Fl4SpecA64.Tcb} : σ.kheap p ≠ some (.tcb x) := by + intro e + rcases hp with ⟨q, hq, rfl⟩ | ⟨q, hq, rfl⟩ | ⟨q, hq, rfl⟩ + · obtain ⟨f, hk, -⟩ := R.pool q hq; rw [hk] at e; cases e + · obtain ⟨t, hk, -⟩ := R.pt q hq; rw [hk] at e; cases e + · obtain ⟨l1, hk, -⟩ := R.high q hq; rw [hk] at e; cases e + +end frame + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/VsS2FlushA.lean b/proofs/Fl4ProofsA64/P3/VsS2FlushA.lean new file mode 100644 index 000000000..e7b9d39c6 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/VsS2FlushA.lean @@ -0,0 +1,532 @@ +/- +P3 (aarch64): Stage-2 root cache invocations and the HighVSpace cap-kind dispatcher. +Uses the existing ArchKeepsA [P] and S2IpaE [P] for the Stage-2 walk and cache operation. +-/ +import Fl4ProofsA64.P3.FrFlushA + +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Classical + +noncomputable section + +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {OS : Set U64} {s : S} {a : A} +variable {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + +/-- A HighVSpace cap is accepted as an extra cap but has no direct invocations. -/ +theorem decInv_highVspace_jA (ar : machine.Arch A) (cte : Fl4SpecA64.CslotPtr) + (ptr asid : U64) (mp : Bool) (lbl length buf : U64) (block call cd fp calling : Bool) + (ex : Aeneas.Std.Array obj.Slot 3#usize) (args : List U64) (xSlot : Fl4SpecA64.CapRef) + (exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) : + syscall.decode_invocation st ar s a lbl length (cslotAddrA cte) (.HighVSpace ptr mp asid) block call cd fp buf ex + ⦃ res => VsTopPostA (L := L) AR Z OS m σ ((Fl4SpecA64.fl4ArchDecodeInvocation (Z := Unit) calling + (Fl4SpecA64.fl4LabelOf lbl.bv) (args.map UScalar.bv) xSlot cte + (.ext (.highVSpaceCap (specPtr ptr) (if mp then some (asidOfA asid) else none))) exs).run σ) s a res ⦄ := by + simp only [syscall.decode_invocation, cur_thread_ok, bind_ok, vspace.decode, error.illegal, WP.spec_ok] + exact ⟨rfl, rfl, .illegalOperation, rfl, Or.inl trivial⟩ + +private def s2FlushRunA (ar : machine.Arch A) (s : S) (a : A) (lbl op length : U64) + (cap : capability.Cap) (buf : U64) : Result (core.result.Result Unit error.Exc × S × A) := do + let ct ← sched.cur_thread st s + if length < 2#u64 + then let r ← error.truncated Unit + ok (r, s, a) + else + let start ← msg.syscall_arg st s ct buf 0#u64 + let «end» ← msg.syscall_arg st s ct buf 1#u64 + if «end» <= start + then let r ← error.invalid_arg Unit 1#u64 + ok (r, s, a) + else + let ipa_bits ← ar.s2_ipa_bits a + let i ← «end» - 1#u64 + let i1 ← ept.ipa_limit ipa_bits + if i > i1 + then let r ← error.illegal Unit + ok (r, s, a) + else + let r ← ept.root st s cap 0#u64 + let cf ← core.result.Result.Insts.CoreOpsTry.branch r + match cf with + | core.ops.control_flow.ControlFlow.Continue val => + let (r1, asid) := val + let lu ← ept.lookup_slot st s r1 start ipa_bits + let e ← st.read s lu.slot + let b ← ept.is_leaf e lu.shift + if b + then + let i2 ← lift (lu.shift &&& 63#u64) + let i3 ← 1#u64 <<< i2 + let m ← lift (core.num.U64.wrapping_sub i3 1#u64) + let i4 ← lift (~~~ m) + let i5 ← lift (start &&& i4) + let i6 ← lift (~~~ m) + let i7 ← lift (i &&& i6) + if i5 != i7 + then + let i8 ← lift (~~~ m) + let i9 ← lift (start &&& i8) + let i10 ← lift (core.num.U64.wrapping_add i9 m) + let r2 ← error.range Unit start i10 + ok (r2, s, a) + else + let perform := do + let s1 ← sched.set_thread_state st s ct sched.TS_RESTART + let pa ← ept.leaf_base e lu.shift + let off ← lift (start &&& m) + let pstart ← lift (core.num.U64.wrapping_add pa off) + let d ← «end» - start + let a1 ← ar.cache_op a op 0#u64 asid start d pstart + ok (core.result.Result.Ok (), s1, a1) + if lbl = fl4_abi.label.ARMVSpaceInvalidate_Data then + let rights ← ept.s2_rights_of e + if rights != capability.VM_READ_WRITE then + let r ← error.illegal Unit + ok (r, s, a) + else perform + else perform + else + let s1 ← + sched.set_thread_state st s ct sched.TS_RESTART + ok (core.result.Result.Ok (), s1, a) + | core.ops.control_flow.ControlFlow.Break residual => + let r1 ← + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual + Unit (core.convert.FromSame error.Exc) residual + ok (r1, s, a) + +set_option maxHeartbeats 4000000 in +private theorem ept_decode_eqA (ar : machine.Arch A) (lbl length : U64) (cap : capability.Cap) + (slot : obj.Slot) (buf : U64) (ex : Aeneas.Std.Array obj.Slot 3#usize) : + ept.decode st ar s a lbl length cap slot buf ex = + if lbl = fl4_abi.label.ARMVSpaceClean_Data then s2FlushRunA (st := st) ar s a lbl vspace.CACHE_CLEAN length cap buf + else if lbl = fl4_abi.label.ARMVSpaceInvalidate_Data then s2FlushRunA (st := st) ar s a lbl vspace.CACHE_INVALIDATE length cap buf + else if lbl = fl4_abi.label.ARMVSpaceCleanInvalidate_Data then s2FlushRunA (st := st) ar s a lbl vspace.CACHE_CLEAN_INVALIDATE length cap buf + else if lbl = fl4_abi.label.ARMVSpaceUnify_Instruction then s2FlushRunA (st := st) ar s a lbl vspace.CACHE_UNIFY length cap buf + else ok (.Err (.Sys .IllegalOperation), s, a) := by + unfold ept.decode + by_cases hc : lbl = fl4_abi.label.ARMVSpaceClean_Data + · rw [if_pos hc, if_pos hc] + rfl + rw [if_neg hc, if_neg hc] + by_cases hi : lbl = fl4_abi.label.ARMVSpaceInvalidate_Data + · rw [if_pos hi, if_pos hi] + rfl + rw [if_neg hi, if_neg hi] + by_cases hci : lbl = fl4_abi.label.ARMVSpaceCleanInvalidate_Data + · rw [if_pos hci, if_pos hci] + rfl + rw [if_neg hci, if_neg hci] + by_cases hu : lbl = fl4_abi.label.ARMVSpaceUnify_Instruction + · rw [if_pos hu, if_pos hu] + rfl + rw [if_neg hu, if_neg hu] + exact bind_ok _ _ + +section spec +open Fl4Spec Fl4SpecA64 + +private def s2FlushTailA (lab start last : Data) (p : ObjRef) (asid : Asid) + (pos : ExtPos) (pte : PTE) : SEMonad Unit ExtVSpaceInvocation := + if pte.isPagePTE then do + let mask := extOffsetMask pos.shift + NonDetE.whenE (start &&& ~~~mask ≠ last &&& ~~~mask) <| + NonDetE.throwError (.rangeError start ((start &&& ~~~mask) + mask)) + NonDetE.whenE (labelToFlushType lab = .invalidate ∧ pte.pteRights ≠ vmReadWrite) <| + NonDetE.throwError .illegalOperation + NonDetE.returnOk (.flush (.vspaceFlush (labelToFlushType lab) start last + ((pteBaseAddr pte &&& ~~~mask) + (start &&& mask)) p asid)) + else NonDetE.returnOk (.flush .vspaceNothing) + +private theorem s2Flush_truncA (lab : Data) (args : List Data) (cap : Cap) + (hl : isVSpaceFlushLabel (invocationType lab) = true) (h : args.length < 2) : + (decodeS2Flush (Z := Unit) lab args cap).run σ = ({(.error .truncatedMessage, σ)}, False) := by + simp only [decodeS2Flush, NonDetE.unlessE, hl, if_pos (by trivial : (true : Bool) = true), not_true_eq_false, ↓reduceIte, + NonDetE.whenE, if_pos h, ndE_pure_bindA, NonDetE.throwError, ndE_throw_bindA] + rfl + +private theorem s2Flush_leA (lab start finish : Data) (args : List Data) (cap : Cap) + (hl : isVSpaceFlushLabel (invocationType lab) = true) (h : finish ≤ start) : + (decodeS2Flush (Z := Unit) lab (start :: finish :: args) cap).run σ = + ({(.error (.invalidArgument 1), σ)}, False) := by + have hlen : ¬ (start :: finish :: args).length < 2 := by simp + simp only [decodeS2Flush, NonDetE.unlessE, hl, if_pos (by trivial : (true : Bool) = true), not_true_eq_false, ↓reduceIte, + NonDetE.whenE, if_neg hlen, List.getElem?_cons_zero, List.getElem?_cons_succ, the, gt_iff_lt, h, ↓reduceIte, + ndE_pure_bindA, NonDetE.throwError, ndE_throw_bindA] + rfl + +private theorem s2Flush_limitA (lab start finish : Data) (args : List Data) (cap : Cap) + (hl : isVSpaceFlushLabel (invocationType lab) = true) (hle : ¬ finish ≤ start) (h : s2IpaLimit < finish - 1) : + (decodeS2Flush (Z := Unit) lab (start :: finish :: args) cap).run σ = + ({(.error .illegalOperation, σ)}, False) := by + have hlen : ¬ (start :: finish :: args).length < 2 := by simp + simp only [decodeS2Flush, NonDetE.unlessE, hl, if_pos (by trivial : (true : Bool) = true), not_true_eq_false, ↓reduceIte, + NonDetE.whenE, if_neg hlen, List.getElem?_cons_zero, List.getElem?_cons_succ, the, gt_iff_lt, hle, h, ↓reduceIte, + ndE_pure_bindA, NonDetE.throwError, ndE_throw_bindA] + rfl + +private theorem s2Flush_prefixA (lab start finish : Data) (args : List Data) (cap : Cap) + (hl : isVSpaceFlushLabel (invocationType lab) = true) (hle : ¬ finish ≤ start) + (h : ¬ s2IpaLimit < finish - 1) (r : Except SyscallError (ObjRef × Asid)) + (hc : (checkExtRoot (Z := Unit) .stage2 cap 0).run σ = ({(r, σ)}, False)) : + (decodeS2Flush (Z := Unit) lab (start :: finish :: args) cap).run σ = + (match r with + | .error e => ({(.error e, σ)}, False) + | .ok (p, asid) => + let pos := extLookupSlot σ s2Geom p start + (s2FlushTailA lab start (finish - 1) p asid pos (pos.slot.read σ)).run σ) := by + have hlen : ¬ (start :: finish :: args).length < 2 := by simp + simp only [decodeS2Flush, NonDetE.unlessE, hl, if_pos (by trivial : (true : Bool) = true), not_true_eq_false, ↓reduceIte, + NonDetE.whenE, if_neg hlen, List.getElem?_cons_zero, List.getElem?_cons_succ, the, gt_iff_lt, hle, h, ↓reduceIte, + ndE_pure_bindA] + cases r with + | error e => exact run_bind_errorA hc _ + | ok val => + obtain ⟨p, asid⟩ := val + rw [run_bind_okA hc] + dsimp only + rw [run_bind_okA (liftE_runA (m := NonDet.gets _) rfl)] + rw [run_bind_okA (liftE_runA (m := NonDet.gets _) rfl)] + rfl + +private theorem s2Root_errA {ptr : ObjRef} {asid : Asid} + (h : extRootResX σ .stage2 asid = .error .invalidRoot) : + (checkExtRoot (Z := Unit) .stage2 (.archObjectCap (.ext (.stage2VSpaceCap ptr (some asid)))) 0).run σ = + ({(.error (.failedLookup false .invalidRoot), σ)}, False) := by + unfold checkExtRoot + dsimp only [ExtArchCap.mapData, ExtArchCap.rootKind, if_pos (rfl : ExtRootKind.stage2 = ExtRootKind.stage2)] + exact run_bind_errorA (lefo_errA (by rw [findExtRoot_runX, h])) _ + +private theorem s2Root_foundA {ptr p : ObjRef} {asid : Asid} + (h : extRootResX σ .stage2 asid = .ok p) : + (checkExtRoot (Z := Unit) .stage2 (.archObjectCap (.ext (.stage2VSpaceCap ptr (some asid)))) 0).run σ = + if p = ptr then ({(.ok (ptr, asid), σ)}, False) else ({(.error (.invalidCapability 0), σ)}, False) := by + unfold checkExtRoot + simp only [ExtArchCap.mapData, ExtArchCap.rootKind, ExtArchCap.objRef, if_true, ↓reduceIte] + rw [run_bind_okA (lefo_okA (by rw [findExtRoot_runX, h]))] + by_cases he : p = ptr + · subst p + simp only [if_pos rfl, NonDetE.whenE, ne_self_iff_false, ↓reduceIte, ndE_pure_bindA] + rfl + · rw [if_neg he] + simp only [NonDetE.whenE, ne_eq, he, not_false_eq_true, ↓reduceIte, NonDetE.throwError, ndE_throw_bindA] + rfl + +private theorem s2LimitEqA {n : U64} (h : n.val = fl4A64Platform.s2IpaBits) : + ept.ipa_limit n = ok (⟨s2IpaLimit⟩ : U64) := by + unfold ept.ipa_limit ept.geom s2IpaLimit s2IpaBitsOf + by_cases h42 : n.val ≤ 42 + · have hc : n ≤ 42#u64 := h42 + rw [if_pos hc, bind_ok, ← h, if_pos h42] + rw [CapRt.shl_u64_eq 1#u64 40#u64 (by decide), bind_ok] + rfl + · have hc : ¬ n ≤ 42#u64 := h42 + by_cases h44 : n.val = 44 + · have hn : n = 44#u64 := UScalar.eq_of_val_eq h44 + rw [if_neg hc, if_pos hn, bind_ok, ← h, if_neg h42, if_pos h44] + rw [CapRt.shl_u64_eq 1#u64 44#u64 (by decide), bind_ok] + rfl + · have hn : n ≠ 44#u64 := fun e => h44 (congrArg UScalar.val e) + rw [if_neg hc, if_neg hn, bind_ok, ← h, if_neg h42, if_neg h44] + rw [CapRt.shl_u64_eq 1#u64 48#u64 (by decide), bind_ok] + rfl + +private theorem s2LeafA {G : U64 → Nat → Prop} {sh : Nat} {w : U64} {pte : PTE} + (hd : DescRelE G sh w pte) : isPgE w sh = true ↔ pte.isPagePTE = true := by + unfold isPgE + rw [isPageA_iff] + cases pte with + | invalidPTE => simp [PTE.isPagePTE, show dBitA w 0 = false from hd] + | pageTablePTE _ => + obtain ⟨hs, h0, h1, _⟩ := hd + have hn : sh ≠ 12 := by omega + simp [hn, h0, h1, PTE.isPagePTE] + | pagePTE _ _ _ _ => + obtain ⟨h0, h1, _⟩ := hd + by_cases hs : sh = 12 + · simp [hs, h0, h1.2 hs, PTE.isPagePTE] + · have hn : dBitA w 1 = false := by cases e : dBitA w 1; rfl; exact (hs (h1.1 e)).elim + simp [hs, h0, hn, PTE.isPagePTE] + +end spec + +section top +open Fl4Spec Fl4SpecA64 + +variable {H : Heap} {U Ac Pp Tb : Set U64} {Lv : U64 → Nat} {ia ie : Nat} + +set_option maxHeartbeats 4000000 in +private theorem s2_flushJA {ar : machine.Arch A} (K : ArchKeepsA ar AR) + (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) (hTr : TreeA st OS Z.G.chains Z.G.par s) + (hIpa : S2IpaE ar a) (ptr asid va : U64) (mp : Bool) (h16 : asid.val < 65536) + (lbl op length buf : U64) (hl : 42 ≤ lbl.val ∧ lbl.val ≤ 45) + (args : List U64) (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) : + s2FlushRunA (st := st) ar s a lbl op length (.Ept ptr 0#u64 mp asid va) buf + ⦃ res => VsTopPostA (L := L) AR Z OS m σ + ((NonDetE.liftME Fl4ArchInvocation.extVSpace (decodeS2Flush (Z := Unit) + (specLabelWord (labNA lbl.val)) (args.map UScalar.bv) + (.archObjectCap (.ext (.stage2VSpaceCap (specPtr ptr) (asidDataA mp asid)))))).run σ) s a res ⦄ := by + let lab := specLabelWord (labNA lbl.val) + have hLab : isVSpaceFlushLabel (invocationType lab) = true := vsFlush_inNA hl + unfold s2FlushRunA + rw [cur_thread_ok, bind_ok] + by_cases hlen2 : length < 2#u64 + · rw [if_pos hlen2] + simp only [error.truncated, bind_ok] + have h : (args.map UScalar.bv).length < 2 := by simp only [List.length_map, ← hlen]; exact hlen2 + exact (WP.spec_ok _).2 ⟨rfl, rfl, .truncatedMessage, + run_liftME_errA (s2Flush_truncA lab _ _ hLab h), Or.inl trivial⟩ + rw [if_neg hlen2] + have hl2 : 2 ≤ args.length := by rw [← hlen]; exact Nat.le_of_not_gt hlen2 + obtain ⟨start, finish, rest, rfl⟩ : ∃ x y r, args = x :: y :: r := by + match args, hl2 with + | x :: y :: r, _ => exact ⟨x, y, r, rfl⟩ + simp only [List.map_cons] + rw [hargs 0#u64 start (by simp), bind_ok, hargs 1#u64 finish (by simp), bind_ok] + have hle : finish ≤ start ↔ finish.bv ≤ start.bv := by rw [BitVec.le_def]; rfl + by_cases hlt : finish ≤ start + · rw [if_pos hlt] + simp only [error.invalid_arg, bind_ok] + exact (WP.spec_ok _).2 ⟨rfl, rfl, .invalidArgument 1, + run_liftME_errA (s2Flush_leA lab _ _ _ _ hLab (hle.1 hlt)), Or.inl rfl⟩ + rw [if_neg hlt] + obtain ⟨n, hn, hnv⟩ := hIpa + have hpos : 1 ≤ finish.val := by have h : ¬ finish.val ≤ start.val := hlt; omega + obtain ⟨last, hlast, hlastVal⟩ := spec_ex_fd (U64.sub_spec (x := finish) (y := 1#u64) (by scalar_tac)) + have hlastBv : finish.bv - 1 = last.bv := by + apply BitVec.eq_of_toNat_eq + have he : last.bv.toNat = finish.bv.toNat - 1 := by scalar_tac + rw [he] + have h : 1 ≤ finish.bv.toNat := hpos + bv_omega + rw [hn, bind_ok, hlast, bind_ok, s2LimitEqA hnv, bind_ok] + have hlim : ((⟨s2IpaLimit⟩ : U64) < last) ↔ s2IpaLimit < finish.bv - 1 := by + rw [hlastBv, BitVec.lt_def]; rfl + by_cases hb : (⟨s2IpaLimit⟩ : U64) < last + · rw [if_pos hb] + simp only [error.illegal, bind_ok] + exact (WP.spec_ok _).2 ⟨rfl, rfl, .illegalOperation, + run_liftME_errA (s2Flush_limitA lab _ _ _ _ hLab (hle.not.1 hlt) (hlim.1 hb)), Or.inl trivial⟩ + rw [if_neg hb] + have hD := s2Flush_prefixA (σ := σ) lab start.bv finish.bv (rest.map UScalar.bv) + (.archObjectCap (.ext (.stage2VSpaceCap (specPtr ptr) (asidDataA mp asid)))) hLab (hle.not.1 hlt) (hlim.not.1 hb) + cases mp + · have hc : ept.root st s (.Ept ptr 0#u64 false asid va) 0#u64 = ok (.Err (.Sys (.InvalidCapability 0#u64))) := by rfl + simp only [hc, bind_ok, core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame.from, bind_tc_ok] + exact (WP.spec_ok _).2 ⟨rfl, rfl, .invalidCapability 0, run_liftME_errA (hD (.error _) rfl), + Or.inr ⟨0#u64, rfl, rfl⟩⟩ + have hept : ept.root st s (.Ept ptr 0#u64 true asid va) 0#u64 = (do + let r ← ept.find st s asid + match r with + | .Ok w => if w != ptr then error.invalid_cap (U64 × U64) 0#u64 else ok (.Ok (w, asid)) + | .Err e => ok (.Err (.Sys (.FailedLookup false e)))) := by + change (ept.find st s asid >>= _) = (ept.find st s asid >>= _) + congr 1 + funext r + cases r <;> simp only [core.result.Result.map_err, bind_ok, bind_tc_ok, + ept.root.closure.Insts.CoreOpsFunctionFnOnceTupleLookupFaultExc, + ept.root.closure.Insts.CoreOpsFunctionFnOnceTupleLookupFaultExc.call_once, + core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame.from] + rw [hept] + obtain ⟨r, hr, ⟨hok, herr⟩, herr2⟩ := spec_ex_fd (WP.spec_and + (ept_findE (st := st) E.j.1.vs E.j.2.2.1.obj h16) (ept_find_errE (st := st) (s := s) h16)) + rw [hr, bind_ok] + rcases r with p | e + swap + · obtain rfl := herr2 e rfl + simp only [core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame.from, bind_ok, bind_tc_ok] + exact (WP.spec_ok _).2 ⟨rfl, rfl, .failedLookup false .invalidRoot, + run_liftME_errA (hD (.error _) (s2Root_errA (herr _ rfl))), Or.inl ⟨rfl, trivial⟩⟩ + obtain ⟨hpX, hfound⟩ := hok p rfl + by_cases hptr : p = ptr + swap + · simp only [bne_iff_ne.2 hptr, ↓reduceIte, error.invalid_cap, bind_ok, bind_tc_ok, + core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame.from] + exact (WP.spec_ok _).2 ⟨rfl, rfl, .invalidCapability 0, + run_liftME_errA (hD (.error _) ((s2Root_foundA hfound).trans (if_neg (fun h => hptr (specPtr_inj h))))), + Or.inr ⟨0#u64, rfl, rfl⟩⟩ + subst ptr + simp only [bne_self_eq_false, Bool.false_eq_true, ↓reduceIte, bind_ok, core.result.Result.Insts.CoreOpsTry.branch] + have hTail := hD (.ok (specPtr p, asidOfA asid)) ((s2Root_foundA hfound).trans (if_pos rfl)) + dsimp only at hTail + rw [hlastBv] at hTail + let pos := extLookupSlot σ s2Geom (specPtr p) start.bv + let pte := pos.slot.read σ + let EX := E.j.1.ext + obtain ⟨g, kk, hg, hkk, hmk, h64, hts, hrem, htop⟩ := s2StartE hnv + obtain ⟨hj, hsl⟩ := htop start p + have hactP := activeS2RootA_of_vspaceForAsid E.j.1.vs hpX (extRootResX_okE hfound).1 + obtain ⟨⟨lsh, lsl⟩, hlu, hpost⟩ := spec_ex_fd (lookup_loopE (st := st) EX.goodE (topOkS2E EX) + start pos False 0#u64 (fun h => h.elim) g.top_shift (peAF p (idxME start g.top_shift kk)) + (core.num.U64.wrapping_sub g.levels 1#u64) + (Or.inl ⟨extTopPos s2Geom (specPtr p) start.bv, by rw [hrem]; rfl, hts.symm, + Or.inr ⟨p, _, hpX, hactP, hj, rfl, hsl, rfl⟩⟩)) + have hlk : ept.lookup_slot st s p start n = ok { slot := lsl, shift := lsh } := by + unfold ept.lookup_slot + rw [hg, bind_ok, ept_entry_addrE _ _ _ _ h64 hmk, bind_ok] + simp only [lift, bind_ok] + rw [ept_lookup_loopE, hlu, bind_ok] + all_goals rfl + simp only [Aeneas.Std.uncurry, hlk, bind_ok, L.read_eq, ept_is_leafE, is_pageE] + obtain ⟨hs, hsr⟩ := hpost + dsimp only at hs hsr + obtain ⟨h12, h39, hd⟩ := descAtE EX.goodE (topOkS2E EX) hsr + rw [hs] at h12 h39 hd + have hpg : isPgE (L.mem s lsl) lsh.val = true ↔ pte.isPagePTE = true := s2LeafA hd + obtain ⟨x, hkx, hst, hset, J1, T1, C1⟩ := restart_onlyJA E RS hTr + have hCarry : ∀ σ', MsA (stUpdA σ σ.curThread x .restart) σ' → CarryA Z (L.mem s) σ → + CarryA Z (L.mem (L.wr s (stA (L.curW s)) sched.TS_RESTART)) σ' := + fun σ' hms C => (C1 C).of_tyA (TyKeepA.of_kheap (by obtain ⟨ms, rfl⟩ := hms; rfl)) + by_cases hleaf : isPgE (L.mem s lsl) lsh.val = true + swap + · rw [if_neg hleaf, hst, bind_ok] + have hT : (s2FlushTailA lab start.bv last.bv (specPtr p) (asidOfA asid) pos pte).run σ = + ({(.ok (.flush .vspaceNothing), σ)}, False) := by + unfold s2FlushTailA + rw [if_neg (hpg.not.1 hleaf)] + rfl + obtain ⟨σ', hp', hms⟩ := perfExtFl_memA .vspaceNothing (stUpdA σ σ.curThread x .restart) + exact (WP.spec_ok _).2 ⟨_, okD_memA (run_liftME_okA (hTail.trans hT)), _, hset, σ', [], + mem_archExtA hp', Z, rfl, rfl, J1.of_msA hms, T1, hCarry σ' hms⟩ + rw [if_pos hleaf] + have hs64 : lsh.val < 64 := by omega + have h63 : (lsh &&& 63#u64) = lsh := UScalar.eq_of_val_eq (by + rw [and_maskA lsh 6 _ rfl]; exact Nat.mod_eq_of_lt (by omega)) + simp only [lift, bind_ok] + rw [h63, CapRt.shl_u64_eq 1#u64 lsh hs64, bind_ok] + simp only [lift, bind_ok, ws1A lsh hs64] + let mask : U64 := ⟨BitVec.ofNat 64 (2 ^ lsh.val - 1)⟩ + have hm : extOffsetMask pos.shift = mask.bv := by + rw [hs] + change (BitVec.ofNat 64 (1 <<< lsh.val) - 1) = BitVec.ofNat 64 (2 ^ lsh.val - 1) + rw [Nat.shiftLeft_eq, Nat.one_mul] + have hpow : 2 ^ lsh.val < 2 ^ 64 := Nat.pow_lt_pow_right (by decide) hs64 + have hpos : 1 ≤ 2 ^ lsh.val := Nat.one_le_two_pow + apply BitVec.eq_of_toNat_eq + rw [BitVec.toNat_sub] + simp only [BitVec.toNat_ofNat, BitVec.toNat_one] + rw [Nat.mod_eq_of_lt hpow, Nat.mod_eq_of_lt (by omega : 2 ^ lsh.val - 1 < 2 ^ 64)] + change (2 ^ 64 - 1 + 2 ^ lsh.val) % 2 ^ 64 = 2 ^ lsh.val - 1 + rw [show 2 ^ 64 - 1 + 2 ^ lsh.val = (2 ^ lsh.val - 1) + 2 ^ 64 by omega, + Nat.add_mod_right, Nat.mod_eq_of_lt (by omega)] + have hc := hTail + change _ = (s2FlushTailA lab start.bv last.bv (specPtr p) (asidOfA asid) pos pte).run σ at hc + unfold s2FlushTailA at hc + rw [if_pos (hpg.1 hleaf)] at hc + rw [hm] at hc + simp only [NonDetE.whenE] at hc + have ho : ((start &&& ~~~mask) = (last &&& ~~~mask)) ↔ + (show BitVec 64 from start.bv) &&& ~~~mask.bv = (show BitVec 64 from last.bv) &&& ~~~mask.bv := UScalar.eq_equiv_bv_eq _ _ + by_cases hsame : (start &&& ~~~mask) = (last &&& ~~~mask) + swap + · rw [if_pos (bne_iff_ne.2 hsame)] + simp only [NonDetE.whenE, if_pos (ho.not.1 hsame), NonDetE.throwError, ndE_throw_bindA] at hc + simp only [error.range, bind_ok] + refine (WP.spec_ok _).2 ⟨rfl, rfl, _, run_liftME_errA (hc.trans rfl), Or.inl ⟨rfl, ?_⟩⟩ + apply BitVec.eq_of_toNat_eq + rw [BitVec.toNat_add] + exact (wa_valm _ _).symm + rw [if_neg (fun h => (bne_iff_ne.1 h) hsame)] + simp only [NonDetE.whenE, if_neg (not_not_intro (ho.1 hsame)), ndE_pure_bindA] at hc + have hrw := rightsS2E EX hsr (hpg.1 hleaf) + have hInv := (labNA_vsFlA lbl.val hl.1 hl.2).2 + have heq : lbl = fl4_abi.label.ARMVSpaceInvalidate_Data ↔ lbl.val = 43 := + ⟨fun e => by rw [e]; simp [fl4_abi.label.ARMVSpaceInvalidate_Data], + fun e => UScalar.eq_of_val_eq (by rw [e]; simp [fl4_abi.label.ARMVSpaceInvalidate_Data])⟩ + obtain ⟨d, hd', -⟩ := spec_ex_fd (U64.sub_spec (x := finish) (y := start) (by scalar_tac)) + have hpa : ept.leaf_base (L.mem s lsl) lsh = ok (⟨baseShE (L.mem s lsl) lsh.val⟩ : U64) := by + rw [ept_leaf_baseE, page_baseE _ _ (by omega)] + let iv := VSpaceInvocation.vspaceFlush (labelToFlushType lab) start.bv last.bv + ((pteBaseAddr pte &&& ~~~mask.bv) + (start.bv &&& mask.bv)) (specPtr p) (asidOfA asid) + obtain ⟨σ', hp', hms⟩ := perfExtFl_memA iv (stUpdA σ σ.curThread x .restart) + have hGood : ∀ (hR : ¬ (labelToFlushType lab = .invalidate ∧ pte.pteRights ≠ vmReadWrite)) a', + AR a' Z.hw → + VsTopPostA (L := L) AR Z OS m σ + ((NonDetE.liftME Fl4ArchInvocation.extVSpace (decodeS2Flush (Z := Unit) lab + (start.bv :: finish.bv :: rest.map UScalar.bv) + (.archObjectCap (.ext (.stage2VSpaceCap (specPtr p) (some (asidOfA asid))))))).run σ) s a + (.Ok (), L.wr s (stA (L.curW s)) sched.TS_RESTART, a') := by + intro hR a' ha' + have hc' := hc + rw [if_neg hR, ndE_pure_bindA] at hc' + exact ⟨_, okD_memA (run_liftME_okA (hc'.trans rfl)), _, hset, σ', [], mem_archExtA hp', + Z, rfl, rfl, (J1.of_arA ha').of_msA hms, T1, hCarry σ' hms⟩ + by_cases hInv' : lbl = fl4_abi.label.ARMVSpaceInvalidate_Data + · rw [if_pos hInv'] + obtain ⟨r, hr, hiff⟩ := s2_rights_ofE (L.mem s lsl) + rw [hr, bind_ok] + by_cases hpR : r = capability.VM_READ_WRITE + · simp only [hpR, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte, hst, bind_ok, lift, hpa, hd'] + exact WP.spec_bind (K.cacheOp a Z.hw _ _ _ _ _ _ J1.1.ar) fun a' ha' => + (WP.spec_ok _).2 (hGood (fun h => h.2 (hrw.2 (hiff.1 hpR))) a' ha') + · simp only [bne_iff_ne.2 hpR, ↓reduceIte, bind_ok, error.illegal] + have hR : labelToFlushType lab = .invalidate ∧ pte.pteRights ≠ vmReadWrite := + ⟨hInv.2 (heq.1 hInv'), fun h => hpR (hiff.2 (hrw.1 h))⟩ + simp only [if_pos hR, NonDetE.throwError, ndE_throw_bindA] at hc + exact (WP.spec_ok _).2 ⟨rfl, rfl, .illegalOperation, run_liftME_errA (hc.trans rfl), Or.inl trivial⟩ + · simp only [if_neg hInv', hst, bind_ok, lift, hpa, hd'] + exact WP.spec_bind (K.cacheOp a Z.hw _ _ _ _ _ _ J1.1.ar) fun a' ha' => + (WP.spec_ok _).2 (hGood (fun h => hInv' (heq.2 (hInv.1 h.1))) a' ha') + +end top + +section dispatch +open Fl4Spec Fl4SpecA64 + +variable {H : Heap} {U Ac Pp Tb : Set U64} {Lv : U64 → Nat} {ia ie : Nat} + +set_option maxHeartbeats 2000000 in +/-- The level-zero Stage-2 root decoder at every label, including the illegal fallback. -/ +theorem decInv_s2Vspace_jA {ar : machine.Arch A} (K : ArchKeepsA ar AR) + (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) (hTr : TreeA st OS Z.G.chains Z.G.par s) + (hIpa : S2IpaE ar a) (cte : CslotPtr) (hab : AbsSlotA Z.th cte) + (ptr asid va : U64) (mp : Bool) (hcap : capAtA (L.mem s) (cslotAddrA cte) = .Ept ptr 0#u64 mp asid va) + (lbl length buf : U64) (block call cd fp calling : Bool) (ex : Aeneas.Std.Array obj.Slot 3#usize) + (args : List U64) (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) + (xSlot : CapRef) (exs : List (Cap × CslotPtr)) : + syscall.decode_invocation st ar s a lbl length (cslotAddrA cte) (.Ept ptr 0#u64 mp asid va) block call cd fp buf ex + ⦃ res => VsTopPostA (L := L) AR Z OS m σ ((fl4ArchDecodeInvocation (Z := Unit) calling + (fl4LabelOf lbl.bv) (args.map UScalar.bv) xSlot cte + (.ext (.stage2VSpaceCap (specPtr ptr) (asidDataA mp asid))) exs).run σ) s a res ⦄ := by + obtain ⟨c, _, hc⟩ := StateRelA.getAbsA E.j.1.st hab + rw [hcap] at hc + have h16 : asid.val < 65536 := hc.1.2 + simp only [syscall.decode_invocation, cur_thread_ok, bind_ok, ept_decode_eqA, fl4LabelOf_bvA, + fl4ArchDecodeInvocation] + by_cases hi : 42 ≤ lbl.val ∧ lbl.val ≤ 45 + · split <;> try split <;> try split <;> try split + all_goals + try exact s2_flushJA K E RS hTr hIpa ptr asid va mp h16 lbl _ length buf hi args hlen hargs + exfalso + have h : lbl.val = 42 ∨ lbl.val = 43 ∨ lbl.val = 44 ∨ lbl.val = 45 := by omega + rcases h with h | h | h | h + · exact ‹lbl ≠ fl4_abi.label.ARMVSpaceClean_Data› (UScalar.eq_of_val_eq (by rw [h]; simp [fl4_abi.label.ARMVSpaceClean_Data])) + · exact ‹lbl ≠ fl4_abi.label.ARMVSpaceInvalidate_Data› (UScalar.eq_of_val_eq (by rw [h]; simp [fl4_abi.label.ARMVSpaceInvalidate_Data])) + · exact ‹lbl ≠ fl4_abi.label.ARMVSpaceCleanInvalidate_Data› (UScalar.eq_of_val_eq (by rw [h]; simp [fl4_abi.label.ARMVSpaceCleanInvalidate_Data])) + · exact ‹lbl ≠ fl4_abi.label.ARMVSpaceUnify_Instruction› (UScalar.eq_of_val_eq (by rw [h]; simp [fl4_abi.label.ARMVSpaceUnify_Instruction])) + · have ho : lbl.val < 42 ∨ 45 < lbl.val := by omega + have h42 : lbl ≠ fl4_abi.label.ARMVSpaceClean_Data := fun e => by rw [e] at ho; simp [fl4_abi.label.ARMVSpaceClean_Data] at ho + have h43 : lbl ≠ fl4_abi.label.ARMVSpaceInvalidate_Data := fun e => by rw [e] at ho; simp [fl4_abi.label.ARMVSpaceInvalidate_Data] at ho + have h44 : lbl ≠ fl4_abi.label.ARMVSpaceCleanInvalidate_Data := fun e => by rw [e] at ho; simp [fl4_abi.label.ARMVSpaceCleanInvalidate_Data] at ho + have h45 : lbl ≠ fl4_abi.label.ARMVSpaceUnify_Instruction := fun e => by rw [e] at ho; simp [fl4_abi.label.ARMVSpaceUnify_Instruction] at ho + simp only [h42, h43, h44, h45, ↓reduceIte, WP.spec_ok] + refine ⟨rfl, rfl, .illegalOperation, run_liftME_errA ?_, Or.inl trivial⟩ + simp only [decodeS2Flush, NonDetE.unlessE, vsFlush_ofNA ho, Bool.false_eq_true, ↓reduceIte, + NonDetE.throwError, ndE_throw_bindA] + rfl + +end dispatch + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/VsTabJA.lean b/proofs/Fl4ProofsA64/P3/VsTabJA.lean new file mode 100644 index 000000000..f5feb640c --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/VsTabJA.lean @@ -0,0 +1,608 @@ +/- +P3 (aarch64, A64-P3 S3 gen 21–22): **the `JAtZAX` steps of the Stage-1 maps** — the restart with the in-place cap +write, and the descriptor write — that `P3/VsMapA` (`pt_map_refinesGA`) and `P3/VsMapPageA` (`page_map_refinesGA`) +carry to the `JAtZAX` lifts `pt_map_jA` / `page_map_jA` (`P3/VsMapJA`). The x86 counterparts are +`XTopJointVsMap`'s `JAtZ.vsStepJ` and the restart / cap-write steps of `P3/VsPtMapRel` / `P3/VsFrameMapRel`. + +Gen 21 (moved here from `P3/VsMapJA` at gen 22, unchanged, so that the map proofs can use them): +* **`TabOffA Z m s p`** (decode-site hypothesis, ruling (b)): the 512 words of the table `p` are no word of the + relation's footprint outside the Stage-1 tables (`FootA` at `{Z with S1 := ∅}`), and the spec name `specPtr p` is + no related object's name outside the Stage-1 tables (`RelPA` at the same ghosts). +* **`ExtVsRelA.of_tabWriteA`**, **`JAtZAX.of_tabWriteA`**: the Stage-2 relation and `JAtZAX` across a write of one + word of such a table, at new Stage-1 ghosts `S1'` / `Lv'` (new members neither Stage-2 pages nor windows: ruling + (c), taken on the lift). + +Gen 22 (ruling 20:56Z items 1–2): +* `FootA.of_ksA` (the footprint reads the state only through `ksb` / `cpu`), `FootA.s1_monoA`, `TabOffA.of_ksA`, + `JAtZAX.of_arA` (the arch state enters only through `XTopAtA.ar`), `revA_wr2A` (an in-place cap write keeps every + slot's revocable bit), `tree_tabWriteA` (a write off the `OS` slots keeps A64 P1's MDB tree), `storePte_eqA` + (`store_pte`'s outcome is `ptSetA`). +* **`restart_setcapJA`** (x86 `restart_rel` then the cap write of `VsFrameMapRel`): gen 20's `restart_setcapA` with, + in addition, the cpu kept, the cap write's `WritesIn`, and — given the call-site MDB tree at `OS` and the null + non-relation slots (`hT` / `hnul`, as `decode_copy_relA`), the old cap non-null, the new cap non-null, `UA`-alike to + the old one (A64 P1's `TreeA.set_cap`) and `CapObjOkA` — `JAtZAX` at `(s2, σ2)` (`JAtZAX.of_slotWrites`, + `CdtRelA.mkA`, `cdtTree_of`) and the tree at `OS`. +* **`JAtZAX.of_pteWriteA`**: `JAtZAX` across the descriptor write (`p ∈ S1`, `TabOffA`), the spec side `ptSetA` then + a machine-state change (`MsA`), at new ghosts `S1'` / `Lv'` given `VsRelA` there (`JAtZAX.of_tabWriteA` and + `ExtVsRelA.of_tabWriteA`; `p ∉ S2PgE` from `ExtVsRelA.pgd`). +* **`MapJA`** (the lift's obligation in the maps' generalized posts) and **`map_finJA`**: from `restart_setcapJA`'s + `JAtZAX` and tree, the descriptor write gives `JAtZAX` and the tree after it (`TabOffA` read at the entry state). + +Gen 23 (ruling 21:26Z item 1): `FootA.monoA` (the footprint grows with the object sets; `FootA.s1_monoA` now its +corollary, statement unchanged), **`TabOffA.liftA`** (`TabOffA` at new Stage-1 ghosts `S1'` / `Lv'` and a state with +the same `ksb` / `cpu`). **Changed statements (forced, strengthened):** `MapJA`'s conclusion and `map_finJA`'s add +`L.ksb s' = L.ksb s ∧ L.cpu s' = L.cpu s` (the post state's kernel-state base and core are the entry's), which +`P3/TabNewA`'s `pt_map_jTA` needs to carry `TabOffA` to the post state; `PtMapPostGA` / `PgMapRelGA` (gen 22) inherit +it; the statements of `pt_map_refinesA`, `page_map_refinesA`, `pt_map_jA`, `page_map_jA` are unchanged. + +Gen 24 (ruling 21:50Z item 2): **`JAtZAX.of_sameS1A`**, the core of `JAtZAX.of_tabWriteA` (`SameA` / `SpecSameA` at +`{Z with S1 := ∅}` in place of the one-word write; `JAtZAX.of_tabWriteA` now builds them and applies it, statement +unchanged), shared with `P3/FinVsJA`'s Stage-1 unmap lift. + +Gen 25 (ruling 22:14Z item 1): **`extZA Z`** (every VSpace table / root / window set emptied), `FootA.extMonoA`, +`RelPA.extMonoA`, **`JAtZAX.of_sameXA`** (`SameA` at `extZA Z`, the spec's globals and `RelPA (extZA Z)` objects kept, +every VCPU an old one and every Stage-2 root / window a member of `Z.X` / `Z.Y`): the core shared by +`JAtZAX.of_sameS1A` (now its corollary, statement unchanged) and `P3/FinVsXA`'s Ext unmap lift. `FootA.monoA` moved +above it (unchanged). + +Gen 30 (ruling 01:13Z item 1): **`SlotFrA OS sl c s s'`** (the slot frame of a map: the invoked slot holds `c`, +every `OS` slot keeps its MDB links and, other than `sl`, its cap), `SlotFrA.keepL` / `.keepR`, `slotFr_wr2A`, +`osKeep_tabWriteA` (`tree_tabWriteA` now its corollary, statement unchanged), **`frMapA`** (a frame cap with new +mapping data). **Changed statements (forced):** `restart_setcapJA` adds `SlotFrA OS (cslotAddrA cte) c s s2`; +`MapJA` takes `sl c` and adds `SlotFrA OS sl c s s'`; `map_finJA` takes `F2 : SlotFrA OS sl c s s2`, `sl ∈ OS` and +adds `SlotFrA OS sl c s s3`. `MapJA` stays decoupled from `CarryA`. + +Decode-site hypothesis: `TabOffA` (gen 21). No named premise. +-/ +import Fl4ProofsA64.P3.FrontEndExtraA +import Fl4ProofsA64.P3.VsUnmapA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Classical + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-! ## The table's words and name -/ + +/-- **A table off the non-Stage-1 footprint** (decode-site hypothesis, module docstring). -/ +structure TabOffA (Z : XSetsA) (m : Fl4SpecA64.McState) (s : S) (p : U64) : Prop where + mem : ∀ i : U64, i.val < 512 → ¬ FootA (L := L) { Z with S1 := ∅ } s (peAF p i) + obj : ∀ q, RelPA { Z with S1 := ∅ } m q → q ≠ specPtr p + +/-! ## The Stage-2 relation across a table write -/ + +/-- **`ExtVsRelA` across a write into a table outside the Stage-2 pages** (module docstring). -/ +theorem ExtVsRelA.of_tabWriteA {Pp Tb Tb' X Y T2 : Set U64} {Sh2 : U64 → Nat} {mem mem' : U64 → U64} + {σ σ' : Fl4SpecA64.State Unit} (E : ExtVsRelA Pp Tb X Y T2 Sh2 mem σ) + (hm : ∀ b, ExtFootE X Y T2 b → mem' b = mem b) {p : U64} (hp : p ∉ S2PgE X T2) (hpP : p ∉ Pp) + (ht : σ'.archState.armAsidTable = σ.archState.armAsidTable) + (hk : ∀ q, q ≠ specPtr p → σ'.kheap q = σ.kheap q) + (hT : ∀ q ∈ Tb', q ∈ Tb ∨ (q ∉ S2PgE X T2 ∧ q ∉ Y)) : ExtVsRelA Pp Tb' X Y T2 Sh2 mem' σ' := by + have E1 := (E.of_mem hm).of_spec ht fun q hq => hk _ fun e => by + rw [specPtr_inj e] at hq + rcases hq with h | h | h + · exact hpP h + · exact hp (Or.inr ⟨p, h, Or.inl rfl⟩) + · exact hp (Or.inl h) + refine { E1 with pgd := fun q hq => ⟨fun hv => ?_, (E.pgd q hq).2⟩, ypg := fun y hy hv => ?_ } + · rcases hv with h | h | h + · exact (E.pgd q hq).1 (Or.inl h) + · rcases hT q h with h' | ⟨h', -⟩ + · exact (E.pgd q hq).1 (Or.inr (Or.inl h')) + · exact h' hq + · exact (E.pgd q hq).1 (Or.inr (Or.inr h)) + · rcases hv with h | h | h + · exact E.ypg y hy (Or.inl h) + · rcases hT y h with h' | ⟨-, h'⟩ + · exact E.ypg y hy (Or.inr (Or.inl h')) + · exact h' hy + · exact E.ypg y hy (Or.inr (Or.inr h)) + +/-! ## `JAtZAX` across a table write -/ + +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {s s' : S} {a : A} {σ σ' : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} + +/-- (gen 23) **The footprint grows with the object sets** (the typed heap and the cores fixed). -/ +theorem FootA.monoA {Z Z' : XSetsA} {s : S} {w : U64} (hth : Z'.th = Z.th) (hCs : Z'.Cs = Z.Cs) + (hT : Z.T ⊆ Z'.T) (hC : Z.C ⊆ Z'.C) (hV : Z.V ⊆ Z'.V) (hPp : Z.Pp ⊆ Z'.Pp) (hS1 : Z.S1 ⊆ Z'.S1) + (hY : Z.Y ⊆ Z'.Y) (hX : Z.X ⊆ Z'.X) (hT2 : Z.T2 ⊆ Z'.T2) (h : FootA (L := L) Z s w) : + FootA (L := L) Z' s w := by + cases h with + | slot y w hy hw => exact .slot y w (by rw [hth]; exact hy) hw + | nslot w h => + refine .nslot w ?_ + cases h with + | node c w hc hw => exact .node c w (by rw [hCs]; exact hc) hw + | cur w hw => exact .cur w hw + | tcb t w ht hw => exact .tcb t w (hT ht) hw + | sc c hc => exact .sc c (hC hc) + | vcpu v w hv hw => exact .vcpu v w (hV hv) hw + | irq i hi => exact .irq i hi + | cur2 w hw => exact .cur2 w hw + | glob w hw => exact .glob w hw + | tcb2 t w ht hw => exact .tcb2 t w (hT ht) hw + | sc2 c hc => exact .sc2 c (hC hc) + | vs w hw => + refine .vs w ?_ + cases hw with + | asid hi h => exact .asid hi h + | pool p lo hp h => exact .pool p lo (hPp hp) h + | pt p i hp h => exact .pt p i (hS1 hp) h + | high y i hy h => exact .high y i (hY hy) h + | ext w hw => + refine .ext w ?_ + cases hw with + | s2lo r i hr hi => exact .s2lo r i (hX hr) hi + | s2hi r i hr hi => exact .s2hi r i (hX hr) hi + | s2pt t i ht hi => exact .s2pt t i (hT2 ht) hi + | wl0 y hy => exact .wl0 y (hY hy) + | wlo y i hy hi => exact .wlo y i (hY hy) hi + +/-- (gen 25) **The relation's sets with every VSpace table / root / window set emptied** (the pools kept): the +footprint and related objects an Ext unmap's write and spec change stay off (`P3/FinVsXA`). -/ +abbrev extZA (Z : XSetsA) : XSetsA := { Z with S1 := ∅, X := ∅, Y := ∅, T2 := ∅ } + +theorem FootA.extMonoA {Z : XSetsA} {s : S} {w : U64} (h : FootA (L := L) (extZA Z) s w) : + FootA (L := L) { Z with S1 := ∅ } s w := + FootA.monoA (Z := extZA Z) rfl rfl (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) + (fun _ h => absurd h (Set.notMem_empty _)) (fun _ h => absurd h (Set.notMem_empty _)) + (fun _ h => absurd h (Set.notMem_empty _)) h + +theorem RelPA.extMonoA {Z : XSetsA} {m : Fl4SpecA64.McState} {p : Fl4SpecA64.ObjRef} (h : RelPA (extZA Z) m p) : + RelPA { Z with S1 := ∅ } m p := by + rcases h with h | h | h | h | ⟨_, h, -⟩ | ⟨_, h, -⟩ | h | ⟨_, h, -⟩ | ⟨_, h, -⟩ + · exact Or.inl h + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr (Or.inl h)) + · exact Or.inr (Or.inr (Or.inr (Or.inl h))) + · exact absurd h (Set.notMem_empty _) + · exact absurd h (Set.notMem_empty _) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl h)))))) + · exact absurd h (Set.notMem_empty _) + · exact absurd h (Set.notMem_empty _) + +set_option maxHeartbeats 1000000 in +/-- Frame the non-VSpace relation while replacing the VSpace relations. -/ +theorem JAtZAX.of_sameVsA (J : JAtZAX (L := L) AR Z s a σ m) {P0 : Set U64} + (H : SameA (L := L) { extZA Z with Pp := P0 } s s') + (HG : SpecGlobA σ σ') + (hcn : ∀ p r, Z.th p = some (.cnode r) → σ'.kheap (specPtr p) = σ.kheap (specPtr p)) + (htc : ∀ p, Z.th p = some .tcb → σ'.kheap (specPtr p) = σ.kheap (specPtr p)) + (ht : ∀ t ∈ Z.T, σ'.kheap (specPtr t) = σ.kheap (specPtr t)) + (hC : ∀ c ∈ Z.C, σ'.kheap (scRenA m c) = σ.kheap (scRenA m c)) + (hV : ∀ v ∈ Z.V, σ'.kheap (specPtr v) = σ.kheap (specPtr v)) + (hcdt : σ'.cdt = σ.cdt) (horig : σ'.isOriginalCap = σ.isOriginalCap) + (hv : ∀ p x, σ'.kheap p = some (.archObj (.vcpu x)) → σ.kheap p = some (.archObj (.vcpu x))) + (hx : ∀ p es, σ'.kheap p = some (.archObj (.ext (.stage2Root es))) → ∃ r ∈ Z.X, specPtr r = p) + (hy : ∀ p l1, σ'.kheap p = some (.archObj (.ext (.highVSpace l1))) → ∃ r ∈ Z.Y, specPtr r = p) + {S1' : Set U64} {Lv' : U64 → Nat} (hroot : ∀ q ∈ Z.S1, Z.Lv q = 0 → q ∈ S1' ∧ Lv' q = 0) + (VR : VsRelA Z.Pp S1' Lv' Z.X Z.Y (L.mem s') (L.ksb s') σ') + (ER : ExtVsRelA Z.Pp S1' Z.X Z.Y Z.T2 Z.Sh2 (L.mem s') σ') : + JAtZAX (L := L) AR { Z with S1 := S1', Lv := Lv' } s' a σ' m := by + obtain ⟨J, V, X0, Y0⟩ := J + have hT : TcbKeepA Z.T σ σ' := TcbKeepA.of_eq ht + have R0 : SchedRestA (L := L) { extZA Z with Pp := P0 } s σ m := + ⟨J.rest.rq, J.rest.rqDom, J.rest.rel, J.rest.consumed, J.rest.repr, J.rest.curDom, J.rest.domTime, J.rest.prio, + J.rest.scMem, J.rest.refillMax⟩ + have hR := (R0.of_mem H.toN).of_spec hT hC HG + have S0 := StateRelA.of_mem (Z := { extZA Z with Pp := P0 }) J.st H + have C0 := CdtRelA.of_mem (Z := { extZA Z with Pp := P0 }) J.cdt H + have J' : JAtZA (L := L) AR { Z with S1 := S1', Lv := Lv' } s' a σ' m := + { st := ⟨fun p r hp => by rw [hcn p r hp]; exact S0.cnode p r hp, fun t' h => by rw [htc t' h]; exact S0.tcb t' h⟩ + cdt := ⟨C0.tree, by rw [hcdt]; exact C0.cdt, by rw [hcdt]; exact C0.cdt_slot, by rw [horig]; exact C0.orig, + C0.par_slot, C0.prev⟩ + sch := (SchedRelA.of_mem (Z := { extZA Z with Pp := P0 }) J.sch H.toN).of_spec hT hC HG + ar := J.ar + place := by rw [H.ksb]; exact J.place + irq := (IrqRelA.of_mem (Z := { extZA Z with Pp := P0 }) J.irq H.toN).of_spec HG + rest := ⟨hR.rq, hR.rqDom, hR.rel, hR.consumed, hR.repr, hR.curDom, hR.domTime, hR.prio, hR.scMem, + hR.refillMax⟩ + thT := J.thT + csN := J.csN + idleT := fun c hc => by rw [(H.toN.node hc).2.1]; exact J.idleT c hc + idleC := fun c hc => by rw [(H.toN.node hc).2.2.2]; exact J.idleC c hc + curT := fun c hc => by rw [(H.toN.node hc).1]; exact J.curT c hc + cscC := fun c hc => by rw [(H.toN.node hc).2.2.1]; exact J.cscC c hc + ksFit := by rw [H.ksb]; exact J.ksFit + scOffD := J.scOffD + cv := fun y hy => by rw [H.cap (Or.inl hy)]; exact J.cv y hy + pml := fun y hy q mp asid e => by + rw [H.cap (Or.inl hy)] at e + obtain ⟨hq, hl⟩ := J.pml y hy q mp asid e + exact hroot q hq hl + vs := VR + ext := ER } + refine ⟨J', ?_, ?_, ?_⟩ + · refine ⟨fun v hv' => ?_, fun q x e => V.back q x (hv q x e), fun t h => ?_, + fun y hy q e => V.cap y hy q (by rw [← H.cap (Or.inl hy)]; exact e)⟩ + · obtain ⟨x, e, h2⟩ := V.obj v hv' + rw [(H.toN.vcW hv').1, (H.toN.vcW hv').2, hV v hv'] + exact ⟨x, e, h2⟩ + · obtain ⟨x, e, h1, h2⟩ := V.tcb t h + rw [(H.toN.tcbW h).2.2.2.1, (H.toN.tcbW h).2.2.2.2, ht t h] + exact ⟨x, e, h1, h2⟩ + · refine ⟨fun r hr => ?_, fun q es e => hx q es e, fun y hy q l mp asid va e => X0.cap y hy q l mp asid va + (by rw [← H.cap (Or.inl hy)]; exact e)⟩ + obtain ⟨es, e, -⟩ := ER.s2root r hr + exact ⟨es, e⟩ + · refine ⟨fun r hr => ?_, fun q l1 e => hy q l1 e, fun y hy q mp asid e => Y0.cap y hy q mp asid + (by rw [← H.cap (Or.inl hy)]; exact e)⟩ + obtain ⟨l1, e, -⟩ := VR.high r hr + exact ⟨l1, e⟩ + +/-- (gen 25) **`JAtZAX` across a change that keeps the footprint and related objects outside every VSpace table / +root / window** (`SameA` at `extZA Z`; the spec: the globals, CDT, original-cap map and the objects of `RelPA` at +`extZA Z` kept, every VCPU an old one, every Stage-2 root / window a member of `Z.X` / `Z.Y`), at new Stage-1 ghosts +given `VsRelA` and `ExtVsRelA` there (which give the Stage-2 / window records' `obj`). The core of +`JAtZAX.of_sameS1A` (gen 24, now its corollary) and of `P3/FinVsXA`'s Ext unmap lift. -/ +theorem JAtZAX.of_sameXA (J : JAtZAX (L := L) AR Z s a σ m) (H : SameA (L := L) (extZA Z) s s') + (HG : SpecGlobA σ σ') (hkh : ∀ p, RelPA (extZA Z) m p → σ'.kheap p = σ.kheap p) + (hcdt : σ'.cdt = σ.cdt) (horig : σ'.isOriginalCap = σ.isOriginalCap) + (hv : ∀ p x, σ'.kheap p = some (.archObj (.vcpu x)) → σ.kheap p = some (.archObj (.vcpu x))) + (hx : ∀ p es, σ'.kheap p = some (.archObj (.ext (.stage2Root es))) → ∃ r ∈ Z.X, specPtr r = p) + (hy : ∀ p l1, σ'.kheap p = some (.archObj (.ext (.highVSpace l1))) → ∃ r ∈ Z.Y, specPtr r = p) + {S1' : Set U64} {Lv' : U64 → Nat} (hroot : ∀ q ∈ Z.S1, Z.Lv q = 0 → q ∈ S1' ∧ Lv' q = 0) + (VR : VsRelA Z.Pp S1' Lv' Z.X Z.Y (L.mem s') (L.ksb s') σ') + (ER : ExtVsRelA Z.Pp S1' Z.X Z.Y Z.T2 Z.Sh2 (L.mem s') σ') : + JAtZAX (L := L) AR { Z with S1 := S1', Lv := Lv' } s' a σ' m := + J.of_sameVsA (P0 := Z.Pp) H HG + (fun p _ h => hkh _ (Or.inl ⟨p, by rw [h]; exact Option.some_ne_none _, rfl⟩)) + (fun p h => hkh _ (Or.inl ⟨p, by rw [h]; exact Option.some_ne_none _, rfl⟩)) + (fun t h => hkh _ (Or.inr (Or.inl ⟨t, h, rfl⟩))) + (fun c h => hkh _ (Or.inr (Or.inr (Or.inl ⟨c, h, rfl⟩)))) + (fun v h => hkh _ (Or.inr (Or.inr (Or.inr (Or.inl ⟨v, h, rfl⟩))))) + hcdt horig hv hx hy hroot VR ER + +/-- (gen 24) **`JAtZAX` across a change that keeps the non-Stage-1 footprint and related objects** (`SameA` / +`SpecSameA` at `{Z with S1 := ∅}`), at new Stage-1 ghosts given `VsRelA` and `ExtVsRelA` there: the core of +`JAtZAX.of_tabWriteA` (one table word) and of `P3/FinVsJA`'s `JAtZAX.of_s1ClearA` (a Stage-1 unmap's outcome). +(Gen 25: a corollary of `JAtZAX.of_sameXA`, statement unchanged.) -/ +theorem JAtZAX.of_sameS1A (J : JAtZAX (L := L) AR Z s a σ m) (H : SameA (L := L) { Z with S1 := ∅ } s s') + (HS : SpecSameA { Z with S1 := ∅ } m σ σ') {S1' : Set U64} {Lv' : U64 → Nat} + (hroot : ∀ q ∈ Z.S1, Z.Lv q = 0 → q ∈ S1' ∧ Lv' q = 0) + (VR : VsRelA Z.Pp S1' Lv' Z.X Z.Y (L.mem s') (L.ksb s') σ') + (ER : ExtVsRelA Z.Pp S1' Z.X Z.Y Z.T2 Z.Sh2 (L.mem s') σ') : + JAtZAX (L := L) AR { Z with S1 := S1', Lv := Lv' } s' a σ' m := + J.of_sameXA ⟨H.ksb, H.cpu, fun w hw => H.mem w hw.extMonoA⟩ HS.toSpecGlobA (fun p hp => HS.kh p hp.extMonoA) + HS.cdt HS.orig (fun p x e => HS.ext p _ e (Or.inl ⟨x, rfl⟩)) + (fun p es e => J.2.2.1.back p es (HS.ext p _ e (Or.inr (Or.inl ⟨es, rfl⟩)))) + (fun p l1 e => J.2.2.2.back p l1 (HS.ext p _ e (Or.inr (Or.inr ⟨l1, rfl⟩)))) hroot VR ER + +/-- **`JAtZAX` across a write of one word of a table off the non-Stage-1 footprint** (module docstring). -/ +theorem JAtZAX.of_tabWriteA (J : JAtZAX (L := L) AR Z s a σ m) {p i : U64} (T : TabOffA (L := L) Z m s p) + (hi : i.val < 512) (hfa : ∀ w, FootA (L := L) { Z with S1 := ∅ } s w → Aligned w) + (hks : L.ksb s' = L.ksb s) (hcpu : L.cpu s' = L.cpu s) + (hm : ∀ w, Aligned w → w ≠ peAF p i → L.mem s' w = L.mem s w) + (HG : SpecGlobA σ σ') (hcdt : σ'.cdt = σ.cdt) (horig : σ'.isOriginalCap = σ.isOriginalCap) + (hk : ∀ q, q ≠ specPtr p → σ'.kheap q = σ.kheap q) + (hpx : ∀ ko, σ'.kheap (specPtr p) = some ko → ¬ ExtObjA ko) + {S1' : Set U64} {Lv' : U64 → Nat} (hroot : ∀ q ∈ Z.S1, Z.Lv q = 0 → q ∈ S1' ∧ Lv' q = 0) + (VR : VsRelA Z.Pp S1' Lv' Z.X Z.Y (L.mem s') (L.ksb s') σ') + (ER : ExtVsRelA Z.Pp S1' Z.X Z.Y Z.T2 Z.Sh2 (L.mem s') σ') : + JAtZAX (L := L) AR { Z with S1 := S1', Lv := Lv' } s' a σ' m := by + have H : SameA (L := L) { Z with S1 := ∅ } s s' := + ⟨hks, hcpu, fun w hw => hm w (hfa w hw) fun e => T.mem i hi (e ▸ hw)⟩ + have HS : SpecSameA { Z with S1 := ∅ } m σ σ' := + { toSpecGlobA := HG + kh := fun q hq => hk q (T.obj q hq) + ext := fun q ko e hx => by + by_cases hq : q = specPtr p + · subst hq; exact absurd hx (hpx ko e) + · rwa [hk q hq] at e + cdt := hcdt + orig := horig } + exact J.of_sameS1A H HS hroot VR ER + +/-! ## Small frames (gen 22) -/ + +/-- (gen 25) The outside-the-slots footprint reads the state only through `ksb` and `cpu`. -/ +theorem FootNA.of_ksA {Z : XSetsA} {s s' : S} (hks : L.ksb s' = L.ksb s) (hcpu : L.cpu s' = L.cpu s) {w : U64} + (h : FootNA (L := L) Z s w) : FootNA (L := L) Z s' w := by + cases h with + | node c w hc hw => exact .node c w hc (by simpa only [nodeAt_ksb hks] using hw) + | cur w hw => exact .cur w (by simpa only [nodeAt_ksb hks, hcpu] using hw) + | tcb t w ht hw => exact .tcb t w ht hw + | sc c hc => exact .sc c hc + | vcpu v w hv hw => exact .vcpu v w hv hw + | irq i hi => rw [← hks]; exact .irq i hi + | cur2 w hw => exact .cur2 w (by simpa only [nodeAt_ksb hks, hcpu] using hw) + | glob w hw => exact .glob w (by rw [hks]; exact hw) + | tcb2 t w ht hw => exact .tcb2 t w ht hw + | sc2 c hc => exact .sc2 c hc + | vs w hw => exact .vs w (by rw [hks]; exact hw) + | ext w hw => exact .ext w hw + +/-- The footprint reads the state only through `ksb` and `cpu` (gen 25: through `FootNA.of_ksA`). -/ +theorem FootA.of_ksA {Z : XSetsA} {s s' : S} (hks : L.ksb s' = L.ksb s) (hcpu : L.cpu s' = L.cpu s) {w : U64} + (h : FootA (L := L) Z s w) : FootA (L := L) Z s' w := by + cases h with + | slot y w hy hw => exact .slot y w hy hw + | nslot w h => exact .nslot w (FootNA.of_ksA hks hcpu h) + +/-- The footprint without the Stage-1 tables is part of the footprint. -/ +theorem FootA.s1_monoA {Z : XSetsA} {s : S} {w : U64} (h : FootA (L := L) { Z with S1 := ∅ } s w) : + FootA (L := L) Z s w := + FootA.monoA (Z := { Z with S1 := ∅ }) rfl rfl (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) + (fun _ h => absurd h (Set.notMem_empty _)) (fun _ h => h) (fun _ h => h) (fun _ h => h) h + +theorem TabOffA.of_ksA {Z : XSetsA} {m : Fl4SpecA64.McState} {s s' : S} {p : U64} (T : TabOffA (L := L) Z m s p) + (hks : L.ksb s' = L.ksb s) (hcpu : L.cpu s' = L.cpu s) : TabOffA (L := L) Z m s' p := + ⟨fun i hi h => T.mem i hi (FootA.of_ksA hks.symm hcpu.symm h), T.obj⟩ + +/-- (gen 23) **`TabOffA` at new Stage-1 ghosts and a state with the same `ksb` / `cpu`** (it reads neither `S1` nor +`Lv`: both are emptied or unread in `FootA` / `RelPA` at `{Z with S1 := ∅}`). -/ +theorem TabOffA.liftA {Z : XSetsA} {m : Fl4SpecA64.McState} {s s' : S} {p : U64} (T : TabOffA (L := L) Z m s p) + (hks : L.ksb s' = L.ksb s) (hcpu : L.cpu s' = L.cpu s) (S1' : Set U64) (Lv' : U64 → Nat) : + TabOffA (L := L) { Z with S1 := S1', Lv := Lv' } m s' p := + ⟨fun i hi h => T.mem i hi (FootA.monoA (Z := { Z with S1 := ∅, Lv := Lv' }) rfl rfl (fun _ h => h) (fun _ h => h) + (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) (fun _ h => h) + (FootA.of_ksA hks.symm hcpu.symm h)), T.obj⟩ + +/-- The arch state enters `JAtZAX` only through `XTopAtA.ar`. -/ +theorem JAtZAX.of_arA (J : JAtZAX (L := L) AR Z s a σ m) {a' : A} (h : AR a' Z.hw) : + JAtZAX (L := L) AR Z s a' σ m := + ⟨{ J.1 with ar := h }, J.2.1, J.2.2.1, J.2.2.2⟩ + +/-- An in-place cap write keeps every `OS` slot's revocable bit. -/ +theorem revA_wr2A {T Ω Sg OS : Set U64} (hO : OSOK T Ω Sg OS) {s : S} {x0 w0 w1 y : U64} (hx0 : x0 ∈ OS) + (hy : y ∈ OS) : revA (L.mem (L.wr (L.wr s x0 w0) (wa x0 8#u64) w1)) y = revA (L.mem s) y := by + unfold revA + by_cases hyx : y = x0 + · subst hyx + obtain ⟨v8, -, v24⟩ := vals hO hy + have a0 := hO.al0 hy + have a8 := hO.al hy (k := 8#u64) (by simp) (by simp) + have a24 := hO.al hy (k := 24#u64) (by simp) (by simp) + rw [L.mem_wr_ne a8 a24 (by omega), L.mem_wr_ne a0 a24 (by omega)] + · obtain ⟨v8, -, -⟩ := vals hO hx0 + obtain ⟨-, -, -, e⟩ := other_keep (st := st) (s := L.wr s x0 w0) (v := w1) hO hx0 hy hyx + (hO.al hx0 (k := 8#u64) (by simp) (by simp)) ⟨by omega, by omega⟩ + obtain ⟨-, -, -, f⟩ := other_keep (st := st) (s := s) (v := w0) hO hx0 hy hyx (hO.al0 hx0) ⟨by omega, by omega⟩ + rw [e, f] + +/-- (gen 30) A write of a word in no `OS` slot keeps every `OS` slot's words. -/ +theorem osKeep_tabWriteA {T Ω Sg OS : Set U64} (hO : OSOK T Ω Sg OS) {s s' : S} {b : U64} (hb : ¬ InOS OS b) + (hm : ∀ w, Aligned w → w ≠ b → L.mem s' w = L.mem s w) : OSKeep st OS s s' := fun y hy => by + obtain ⟨v8, v16, v24⟩ := vals hO hy + refine ⟨hm _ (hO.al0 hy) ?_, hm _ (hO.al hy (k := 8#u64) (by simp) (by simp)) ?_, + hm _ (hO.al hy (k := 16#u64) (by simp) (by simp)) ?_, hm _ (hO.al hy (k := 24#u64) (by simp) (by simp)) ?_⟩ <;> + · intro e; subst e; exact hb ⟨y, hy, by omega, by omega⟩ + +/-- A write of a word in no `OS` slot keeps A64 P1's MDB tree. -/ +theorem tree_tabWriteA {T Ω Sg OS : Set U64} (hO : OSOK T Ω Sg OS) {G : List (List U64)} {par : U64 → Option U64} + {s s' : S} (h : TreeA st OS G par s) {b : U64} (hb : ¬ InOS OS b) + (hm : ∀ w, Aligned w → w ≠ b → L.mem s' w = L.mem s w) : TreeA st OS G par s' := + h.keepO (osKeep_tabWriteA hO hb hm) + +/-- `store_pte`'s outcome at a related table is `ptSetA`. -/ +theorem storePte_eqA {Pp Tb : Set U64} {Lv : U64 → Nat} {X Y : Set U64} {mem : U64 → U64} {ksb : U64} + {σ σ' : Fl4SpecA64.State Unit} (R : VsRelA Pp Tb Lv X Y mem ksb σ) {p i : U64} (hp : p ∈ Tb) (hi : i.val < 512) + {pte : Fl4SpecA64.PTE} + (h : Fl4SpecA64.storePte (Z := Unit) (ptTyA (Lv p)) (specPtr (peAF p i)) pte σ = ({((), σ')}, False)) : + ∃ t', σ' = ptSetA σ (specPtr p) t' := by + obtain ⟨t, hk, ht, -⟩ := R.pt p hp + rw [storePte_runA hk ht (R.al_mod hp) hi pte] at h + have e := congrArg Prod.fst h + simp only [Set.singleton_eq_singleton_iff, Prod.mk.injEq, true_and] at e + exact ⟨_, e.symm⟩ + +/-! ## The slot frame of a map (gen 30) -/ + +/-- (gen 30, ruling 01:13Z item 1) **The slot frame of a map**: the invoked slot `sl` holds `c`, and every `OS` slot +keeps its MDB links and, other than `sl`, its cap. -/ +def SlotFrA (OS : Set U64) (sl : U64) (c : capability.Cap) (s0 s1 : S) : Prop := + capAtA (L.mem s1) sl = c ∧ ∀ y ∈ OS, (y ≠ sl → capAtA (L.mem s1) y = capAtA (L.mem s0) y) ∧ + L.mem s1 (wa y 16#u64) = L.mem s0 (wa y 16#u64) ∧ L.mem s1 (wa y 24#u64) = L.mem s0 (wa y 24#u64) + +/-- A slot frame after a step keeping the `OS` slots. -/ +theorem SlotFrA.keepL {OS : Set U64} {sl : U64} {c : capability.Cap} {s0 s1 s2 : S} (hk : OSKeep st OS s0 s1) + (h : SlotFrA (L := L) OS sl c s1 s2) : SlotFrA (L := L) OS sl c s0 s2 := + ⟨h.1, fun y hy => ⟨fun hne => ((h.2 y hy).1 hne).trans (by rw [capAtA_eq, capAtA_eq]; exact hk.capA hy), + (h.2 y hy).2.1.trans (hk y hy).2.2.1, (h.2 y hy).2.2.trans (hk y hy).2.2.2⟩⟩ + +/-- A slot frame before a step keeping the `OS` slots. -/ +theorem SlotFrA.keepR {OS : Set U64} {sl : U64} {c : capability.Cap} {s0 s1 s2 : S} (hsl : sl ∈ OS) + (h : SlotFrA (L := L) OS sl c s0 s1) (hk : OSKeep st OS s1 s2) : SlotFrA (L := L) OS sl c s0 s2 := + ⟨by rw [capAtA_eq, hk.capA hsl, ← capAtA_eq]; exact h.1, fun y hy => + ⟨fun hne => by rw [capAtA_eq, hk.capA hy, ← capAtA_eq]; exact (h.2 y hy).1 hne, + (hk y hy).2.2.1.trans (h.2 y hy).2.1, (hk y hy).2.2.2.trans (h.2 y hy).2.2⟩⟩ + +/-- An in-place cap write at `x0`: every other `OS` slot keeps its cap, every `OS` slot its MDB links. -/ +theorem slotFr_wr2A {T Ω Sg OS : Set U64} (hO : OSOK T Ω Sg OS) {s0 : S} {x0 w0 w1 : U64} (hx0 : x0 ∈ OS) + {y : U64} (hy : y ∈ OS) : + (y ≠ x0 → capAtA (L.mem (L.wr (L.wr s0 x0 w0) (wa x0 8#u64) w1)) y = capAtA (L.mem s0) y) ∧ + L.mem (L.wr (L.wr s0 x0 w0) (wa x0 8#u64) w1) (wa y 16#u64) = L.mem s0 (wa y 16#u64) ∧ + L.mem (L.wr (L.wr s0 x0 w0) (wa x0 8#u64) w1) (wa y 24#u64) = L.mem s0 (wa y 24#u64) := by + refine ⟨fun hne => by rw [capAtA_eq, capAtA_eq]; exact capA_wr2_other hO hx0 hy hne, ?_⟩ + by_cases hyx : y = x0 + · subst hyx + obtain ⟨v8, v16, v24⟩ := vals hO hy + have a0 := hO.al0 hy + have a8 := hO.al hy (k := 8#u64) (by simp) (by simp) + have a16 := hO.al hy (k := 16#u64) (by simp) (by simp) + have a24 := hO.al hy (k := 24#u64) (by simp) (by simp) + exact ⟨by rw [L.mem_wr_ne a8 a16 (by omega), L.mem_wr_ne a0 a16 (by omega)], + by rw [L.mem_wr_ne a8 a24 (by omega), L.mem_wr_ne a0 a24 (by omega)]⟩ + · obtain ⟨v8, -, -⟩ := vals hO hx0 + obtain ⟨-, -, e16, e24⟩ := other_keep (st := st) (s := L.wr s0 x0 w0) (v := w1) hO hx0 hy hyx + (hO.al hx0 (k := 8#u64) (by simp) (by simp)) ⟨by omega, by omega⟩ + obtain ⟨-, -, f16, f24⟩ := other_keep (st := st) (s := s0) (v := w0) hO hx0 hy hyx (hO.al0 hx0) + ⟨by omega, by omega⟩ + exact ⟨e16.trans f16, e24.trans f24⟩ + +/-- (gen 30) The frame cap `c` mapped at `asid` / `va` (the cap FL4's `ARMPageMap` writes). -/ +def frMapA : capability.Cap → U64 → U64 → capability.Cap + | .Frame p sz rt d _ e _ _, asid, va => .Frame p sz rt d true e asid va + | c, _, _ => c + +/-! ## The restart and the in-place cap write -/ + +variable {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} {ia ie : Nat} + +/-- (gen 22) **The restart site** (the case hypotheses of `JEntA.restartA` and the relation's slots in `OS`, as +`restart_setcapA` takes them), bundled. -/ +structure RsSiteA (Z : XSetsA) (H : Heap) (OS Ac Pp Tb : Set U64) (Lv : U64 → Nat) (s : S) : Prop where + pl : ∃ (Rp Nt : Set U64) (extra : Aeneas.Std.Array obj.Slot 3#usize) (sp : U64), + SysPlace st H Z.Cs H.frame Rp Nt OS s extra sp + sym : SymL st H Z.Cs s + run : Runnable (L.mem s (stA (L.curW s))) + cur : (L.mem s (inqA (L.curW s)) &&& sched.Q_RELEASE) = 0#u64 ∧ L.mem s (scA (L.curW s)) ≠ 0#u64 ∧ + L.mem s (scA (L.curW s)) ∈ Ac + vs : ∀ b, VsR (L.ksb s) (utVsPtA st Pp Tb Lv s) b → Aligned b ∧ b ≠ stA (L.curW s) + vc : ∀ v ∈ Z.V, stA (L.curW s) ≠ v ∧ stA (L.curW s) ≠ wa v 24#u64 + sub : ∀ y, IsSlotA Z.th y → y ∈ OS + +set_option maxHeartbeats 2000000 in +/-- **The caller's restart and the in-place cap write, with `JAtZAX` after them** (module docstring). -/ +theorem restart_setcapJA (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) + (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) + (cte : Fl4SpecA64.CslotPtr) (hab : AbsSlotA Z.th cte) (hos : cslotAddrA cte ∈ OS) + {c : capability.Cap} {sc : Fl4SpecA64.Cap} (hEnc : CapRt.EncWf c) (hca : CapRelA c sc) : + ∃ x s2 σ2, σ.kheap σ.curThread = some (.tcb x) ∧ + sched.set_thread_state st s (L.curW s) sched.TS_RESTART = ok (L.wr s (stA (L.curW s)) sched.TS_RESTART) ∧ + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + obj.Slot.set_cap st (cslotAddrA cte) (L.wr s (stA (L.curW s)) sched.TS_RESTART) c = ok s2 ∧ + ((), σ2) ∈ (Fl4SpecA64.setCap (Z := Unit) sc cte (stUpdA σ σ.curThread x .restart)).1 ∧ + SpecSlotA (stUpdA σ σ.curThread x .restart) σ2 ∧ L.ksb s2 = L.ksb s ∧ + VsRelA Z.Pp Z.S1 Z.Lv Z.X Z.Y (L.mem s2) (L.ksb s2) σ2 ∧ L.cpu s2 = L.cpu s ∧ + WritesIn st (L.wr s (stA (L.curW s)) sched.TS_RESTART) s2 (W7.CapW H.tcbs {b | InOS OS b} ∅) ∧ + (TreeA st OS Z.G.chains Z.G.par s → (∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) → + c ≠ .Null → capA st s (cslotAddrA cte) ≠ .Null → UA c (capA st s (cslotAddrA cte)) → CapObjOkA Z c → + JAtZAX (L := L) AR Z s2 a σ2 m ∧ TreeA st OS Z.G.chains Z.G.par s2) ∧ + SlotFrA (L := L) OS (cslotAddrA cte) c s s2 := by + obtain ⟨⟨Rp, Nt, extra, sp, P⟩, Y, hrun, hcur, hvs, hvc, hsub⟩ := RS + obtain ⟨x, hkx, hst, hset, E1, -, hOK, -, -⟩ := E.restartA P Y hrun hcur hvs hvc + obtain ⟨w0, w1, hen, hd⟩ := CapRt.decode_encode _ hEnc + have hO := E1.sys.ent.os + have a0x := hO.al0 hos + have a8x := hO.al hos (k := 8#u64) (by simp) (by simp) + obtain ⟨v8, -, -⟩ := vals hO hos + have hw2 : WritesIn st (L.wr s (stA (L.curW s)) sched.TS_RESTART) + (L.wr (L.wr (L.wr s (stA (L.curW s)) sched.TS_RESTART) (cslotAddrA cte) w0) (wa (cslotAddrA cte) 8#u64) w1) + (W7.CapW H.tcbs {b | InOS OS b} ∅) := by + have := W7.obj.Slot.set_cap.capw (st := st) (s := L.wr s (stA (L.curW s)) sched.TS_RESTART) (c := c) + (osw hO hos) + rw [set_cap_eq hen, WP.spec_ok] at this + exact this + have hcpu1 : L.cpu (L.wr s (stA (L.curW s)) sched.TS_RESTART) = L.cpu s := L.cpu_wr _ _ _ + generalize hs1 : L.wr s (stA (L.curW s)) sched.TS_RESTART = s1 at hw2 E1 hst hOK hcpu1 ⊢ + generalize hs2 : L.wr (L.wr s1 (cslotAddrA cte) w0) (wa (cslotAddrA cte) 8#u64) w1 = s2 at hw2 + have hksb : L.ksb s2 = L.ksb s := hw2.1.trans (by rw [← hs1]; exact L.ksb_wr _ _ _) + have hcx : capAtA (L.mem s2) (cslotAddrA cte) = c := by + have hd' := hd + unfold capAtA + rw [← hs2, L.mem_wr_ne a8x a0x (by omega), L.mem_wr_same a0x, L.mem_wr_same a8x] + rw [CapRt.decode_eq] at hd' + exact okinj hd' + have hcapo : ∀ y, IsSlotA Z.th y → y ≠ cslotAddrA cte → capAtA (L.mem s2) y = capAtA (L.mem s1) y := by + intro y hy hne + rw [capAtA_eq, capAtA_eq, ← hs2] + exact capA_wr2_other hO hos (hsub y hy) hne + have hFr : SlotFrA (L := L) OS (cslotAddrA cte) c s s2 := + SlotFrA.keepL hOK ⟨hcx, fun y hy => by rw [← hs2]; exact slotFr_wr2A hO hos hy⟩ + obtain ⟨o, hσ2, hSR, HS⟩ := StateRelA.setAbsA E1.j.1.st E1.j.1.place hab (by rw [hcx]; exact hca) hcapo + have hW2 : ∀ b, W7.CapW H.tcbs {b | InOS OS b} ∅ b → ¬ VsFootA Z.Pp Z.S1 Z.Y (L.ksb s1) b := by + intro b hb hf + obtain ⟨hno, hnt⟩ := E1.ap.vs b hf + rcases hb with ⟨t, ht, K, hK, rfl⟩ | ⟨hin, -⟩ + · have hfit := (E1.sq.tcbs t ht).fits + simp only [Nat.reducePow] at hfit + rcases hnt t ht with h | h <;> rw [wa_valm] at h <;> omega + · exact hno hin + refine ⟨x, s2, _, hkx, hst, hset, by rw [← hs2]; exact set_cap_eq hen, hσ2, HS, hksb, + ((E1.j.1.vs).of_writesIn hw2 hW2 ((E1.j.1.vs).foot_alignedA E1.vsLeafA.1)).of_specSlot HS HS.asid, + hw2.2.1.trans hcpu1, hw2, ?_, hFr⟩ + intro hT hnul hcn hc0 hua hok + have hP1 := E1.j.1.place + have hcs : IsSlotA Z.th (cslotAddrA cte) := hab.isSlot hP1 + have hc1 : capA st s1 (cslotAddrA cte) = capA st s (cslotAddrA cte) := hOK.capA hos + have hT2 : TreeA st OS Z.G.chains Z.G.par s2 := by + rw [← hs2]; exact (hT.keepO hOK).set_cap hO hos hd hcn (by rw [hc1]; exact hc0) (by rw [hc1]; exact hua) + have C1 := E1.j.1.cdt + have hC : CdtRelA st Z.th s2 (setObjA (stUpdA σ σ.curThread x .restart) cte.1 o) Z.G := + CdtRelA.mkA (cdtTree_of hT2 hsub fun y hy hn => by + rw [← hs2, capA_wr2_other hO hos hy (fun e => hn (e ▸ hcs)), hOK.capA hy]; exact hnul y hy hn) + C1.cdt C1.cdt_slot + (fun sl hsl => by rw [← hs2, revA_wr2A hO hos (hsub _ (hsl.isSlot hP1))]; exact C1.orig sl hsl) + C1.par_slot C1.po + refine ⟨E1.j.of_slotWrites hw2 (fun b hb hf => footN_offA E1 b hf hb) + (FootNA.aligned E1.j.1 (bxi_nodes E1.sys.ent.bxi) E1.scAl E1.ap.vcAl) Z.G HS hSR hC fun y hy => ?_, hT2⟩ + by_cases hne : y = cslotAddrA cte + · subst hne; rw [hcx]; exact hok + · rw [hcapo y hy hne]; exact E1.j.capObj hy + +/-! ## The descriptor write -/ + +/-- **`JAtZAX` across the descriptor write** (module docstring). -/ +theorem JAtZAX.of_pteWriteA (J : JAtZAX (L := L) AR Z s a σ m) {p i : U64} (hp : p ∈ Z.S1) + (T : TabOffA (L := L) Z m s p) (hi : i.val < 512) (hfa : ∀ w, FootA (L := L) Z s w → Aligned w) + (hks : L.ksb s' = L.ksb s) (hcpu : L.cpu s' = L.cpu s) + (hm : ∀ w, Aligned w → w ≠ peAF p i → L.mem s' w = L.mem s w) + {t' : Fl4SpecA64.PT} (hms : MsA (ptSetA σ (specPtr p) t') σ') + {S1' : Set U64} {Lv' : U64 → Nat} (hT : ∀ q ∈ S1', q ∈ Z.S1 ∨ (q ∉ S2PgE Z.X Z.T2 ∧ q ∉ Z.Y)) + (hroot : ∀ q ∈ Z.S1, Z.Lv q = 0 → q ∈ S1' ∧ Lv' q = 0) + (VR : VsRelA Z.Pp S1' Lv' Z.X Z.Y (L.mem s') (L.ksb s') σ') : + JAtZAX (L := L) AR { Z with S1 := S1', Lv := Lv' } s' a σ' m := by + obtain ⟨ms, rfl⟩ := hms + have hk : ∀ q, q ≠ specPtr p → + ({ ptSetA σ (specPtr p) t' with machineState := ms } : Fl4SpecA64.State Unit).kheap q = σ.kheap q := + fun q hq => by show (ptSetA σ (specPtr p) t').kheap q = _; rw [ptSetA_kheap, if_neg hq] + have hpS : p ∉ S2PgE Z.X Z.T2 := fun h => (J.1.ext.pgd p h).1 (Or.inr (Or.inl hp)) + have hpP : p ∉ Z.Pp := fun h => by + obtain ⟨_, h1, -⟩ := J.1.vs.pool p h + obtain ⟨_, h2, -⟩ := J.1.vs.pt p hp + rw [h1] at h2; cases h2 + refine JAtZAX.of_tabWriteA (σ' := { ptSetA σ (specPtr p) t' with machineState := ms }) J T hi (fun w h => hfa w (FootA.s1_monoA h)) hks hcpu hm + ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, ⟨rfl, rfl, rfl, rfl⟩⟩ rfl rfl hk ?_ hroot VR + (J.1.ext.of_tabWriteA (fun b hb => hm b (hfa b (.nslot b (.ext b hb))) + fun e => T.mem i hi (e ▸ .nslot b (.ext b hb))) hpS hpP rfl hk hT) + intro ko e hx + have e' : (ptSetA σ (specPtr p) t').kheap (specPtr p) = some ko := e + rw [ptSetA_kheap, if_pos rfl] at e' + cases e' + rcases hx with ⟨_, h⟩ | ⟨_, h⟩ | ⟨_, h⟩ <;> cases h + +/-- **The lift's obligation** (module docstring): under the call-site MDB tree at `OS`, the null non-relation slots, +every Stage-1 table off the non-Stage-1 footprint and the extra condition `hc`, `JAtZAX` at the ghosts `Z'` and the +tree hold at the outcome (gen 23: and its `ksb` / `cpu` are the entry's, so `TabOffA` carries over, `TabOffA.liftA`; +gen 30: and the slot frame `SlotFrA` of the invoked slot `sl` written with `c`). -/ +def MapJA (AR : A → HwRootA → Prop) (Z : XSetsA) (OS : Set U64) (m : Fl4SpecA64.McState) (s : S) (hc : Prop) + (Z' : XSetsA) (s' : S) (a' : A) (σ' : Fl4SpecA64.State Unit) (sl : U64) (c : capability.Cap) : Prop := + TreeA st OS Z.G.chains Z.G.par s → (∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) → + (∀ q ∈ Z.S1, TabOffA (L := L) Z m s q) → hc → + JAtZAX (L := L) AR Z' s' a' σ' m ∧ TreeA st OS Z.G.chains Z.G.par s' ∧ L.ksb s' = L.ksb s ∧ L.cpu s' = L.cpu s ∧ + SlotFrA (L := L) OS sl c s s' + +/-- **The descriptor write after `restart_setcapJA`** (module docstring). -/ +theorem map_finJA (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) {s2 s3 : S} {σ2 σ3 : Fl4SpecA64.State Unit} + (J2 : JAtZAX (L := L) AR Z s2 a σ2 m) (hT2 : TreeA st OS Z.G.chains Z.G.par s2) + (hks2 : L.ksb s2 = L.ksb s) (hcpu2 : L.cpu s2 = L.cpu s) + {p i : U64} (hp : p ∈ Z.S1) (T : TabOffA (L := L) Z m s p) (hi : i.val < 512) + (hks : L.ksb s3 = L.ksb s2) (hcpu : L.cpu s3 = L.cpu s2) + (hm : ∀ w, Aligned w → w ≠ peAF p i → L.mem s3 w = L.mem s2 w) + {t' : Fl4SpecA64.PT} (hms : MsA (ptSetA σ2 (specPtr p) t') σ3) + {S1' : Set U64} {Lv' : U64 → Nat} (hT : ∀ q ∈ S1', q ∈ Z.S1 ∨ (q ∉ S2PgE Z.X Z.T2 ∧ q ∉ Z.Y)) + (hroot : ∀ q ∈ Z.S1, Z.Lv q = 0 → q ∈ S1' ∧ Lv' q = 0) + (VR : VsRelA Z.Pp S1' Lv' Z.X Z.Y (L.mem s3) (L.ksb s3) σ3) + {sl : U64} {c : capability.Cap} (F2 : SlotFrA (L := L) OS sl c s s2) (hsl : sl ∈ OS) : + JAtZAX (L := L) AR { Z with S1 := S1', Lv := Lv' } s3 a σ3 m ∧ TreeA st OS Z.G.chains Z.G.par s3 ∧ + L.ksb s3 = L.ksb s ∧ L.cpu s3 = L.cpu s ∧ SlotFrA (L := L) OS sl c s s3 := by + have hN : ∀ c ∈ Z.Cs, ∀ o : U64, o.val < 4288 → o.val % 8 = 0 → Aligned (L.nodeAt s2 c o) := + fun c hc o h1 h2 => by rw [nodeAt_ksb hks2]; exact bxi_nodes E.sys.ent.bxi c hc o h1 h2 + have hK := osKeep_tabWriteA E.sys.ent.os (E.ap.vs _ (.pt p i hp hi)).1 hm + exact ⟨J2.of_pteWriteA hp (T.of_ksA hks2 hcpu2) hi (FootA.aligned J2.1 hN E.scAl E.ap.vcAl) hks hcpu hm hms hT hroot VR, + hT2.keepO hK, hks.trans hks2, hcpu.trans hcpu2, F2.keepR hsl hK⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/VsTieA.lean b/proofs/Fl4ProofsA64/P3/VsTieA.lean new file mode 100644 index 000000000..d8d049878 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/VsTieA.lean @@ -0,0 +1,124 @@ +/- +P3 (aarch64, A64-P3 S3 gen 12, from VS gens 1–4's `P3/VsRelA`): **the parts of the VSpace relation that read the +joint relation's vocabulary** — split off `P3/VsRelA` when `VsRelA` became a field of `JAtZA` (Architect ruling +15:08Z: `P3/JointA` imports `P3/VsRelA`, so `VsRelA` itself cannot import `P3/FrameA` / `P3/JointA`). + +* (`VsRelA.of_specSlot`, VS gen 1, moved to `P3/FrameA`: `JAtZAX.of_slot` uses it.) +* **The hardware-root tie** (VS gen 1–2, unchanged): `vtCapA`, `vmRootA` (l4v `set_vm_root`'s choice), `HwS1TieA Z σ` + (`Z.hw = vmRootA σ σ.curThread`), `vcpuOfA`, `vmS2RootA` (FL4's `set_stage2_root` choice), `HwS2TieA s2 σ`, + **`HwTieA Z s2 σ`** (a flat predicate beside `JAtZA`, `s2` a parameter: ruling 14:12Z / 15:01Z item 3) and their + spec-side frames. + +No named premise. No `Fl4SpecA64` edit. +-/ +import Fl4ProofsA64.P3.FrameA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 + +noncomputable section + +/-! ## The hardware-root tie -/ + +/-- The cap in a thread's slot 1 (`tcb_vtable`), if the thread is a TCB. -/ +def vtCapA (σ : Fl4SpecA64.State Unit) (t : Fl4SpecA64.ObjRef) : Option Fl4SpecA64.Cap := + match σ.kheap t with + | some (.tcb x) => Fl4SpecA64.tcbCnodeMap x (Fl4SpecA64.tcbCnodeIndex 1) + | _ => none + +/-- **The root `set_vm_root t` installs** (module docstring; l4v `set_vm_root`: `arm_context_switch pt asid` for a +mapped `VSRootPT_T` cap whose ASID's `vspace_for_asid` is its table, else `set_global_user_vspace`). -/ +def vmRootA (σ : Fl4SpecA64.State Unit) (t : Fl4SpecA64.ObjRef) : HwRootA := + match vtCapA σ t with + | some (.archObjectCap (.pageTableCap pt .vsRootPtT (some (asid, _)))) => + if Fl4SpecA64.vspaceForAsid asid σ = some pt then ⟨pt, some asid⟩ + else ⟨σ.archState.armUsGlobalVspace, none⟩ + | _ => ⟨σ.archState.armUsGlobalVspace, none⟩ + +/-- **The Stage-1 tie** (module docstring; x86 `x64_current_cr3`): the ghost root is what `set_vm_root` of the +current thread installs. -/ +def HwS1TieA (Z : XSetsA) (σ : Fl4SpecA64.State Unit) : Prop := Z.hw = vmRootA σ σ.curThread + +/-- The VCPU bound to a thread (l4v `tcb_vcpu`), if the thread is a TCB. -/ +def vcpuOfA (σ : Fl4SpecA64.State Unit) (t : Fl4SpecA64.ObjRef) : Option Fl4SpecA64.ObjRef := + match σ.kheap t with + | some (.tcb x) => x.tcbArch.tcbVcpu + | _ => none + +/-- **The Stage-2 root `set_vm_root t` installs for a VCPU thread** (gen 2, ruling Q4; FL4 `set_stage2_root`, +`vspace.rs` 345–353): the thread's slot-1 cap an assigned Stage-2 VSpace cap whose ASID still names it as a +Stage-2 root (`ept::find`, `Ext/Stage2` `findExtRoot .stage2`): that root and ASID; else `none` (VTTBR_EL2 `0`, +VMID `0`). -/ +def vmS2RootA (σ : Fl4SpecA64.State Unit) (t : Fl4SpecA64.ObjRef) : Option HwRootA := + match vtCapA σ t with + | some (.archObjectCap (.ext (.stage2VSpaceCap r (some asid)))) => + if Fl4SpecA64.vspaceForAsid asid σ = some r ∧ Fl4SpecA64.rootKindAt σ r = .stage2 then some ⟨r, some asid⟩ + else none + | _ => none + +/-- **The Stage-2 tie** (gen 2, ruling Q4): when the current thread has a bound VCPU, the ghost Stage-2 root `s2` +(VTTBR_EL2: `none` for `0`) is what FL4's `set_vm_root` installs (`set_kernel_vm_root` + `set_stage2_root`); vacuous +for a thread with no VCPU (every thread at `w0` / `w1`, where `Z.V = ∅`). The ghost `s2` is not a field of +`XSetsA` (JointA is not edited): a parameter until the merge (handoff). The `TS_RUNNING_VM` case (a native thread +running a guest's Stage-2 root, `vspace.rs` 394–402) is not covered: no Stage-2 operation yet. -/ +def HwS2TieA (s2 : Option HwRootA) (σ : Fl4SpecA64.State Unit) : Prop := + vcpuOfA σ σ.curThread ≠ none → s2 = vmS2RootA σ σ.curThread + +/-- **The hardware-root tie** (module docstring; gen 2: the Stage-1 / Stage-2 split of ruling Q4). -/ +def HwTieA (Z : XSetsA) (s2 : Option HwRootA) (σ : Fl4SpecA64.State Unit) : Prop := + HwS1TieA Z σ ∧ HwS2TieA s2 σ + +/-- A thread without a VCPU makes the Stage-2 tie vacuous. -/ +theorem HwS2TieA.of_none {s2 : Option HwRootA} {σ : Fl4SpecA64.State Unit} (h : vcpuOfA σ σ.curThread = none) : + HwS2TieA s2 σ := fun h' => absurd h h' + +/-- A thread whose slot 1 holds no mapped `VSRootPT_T` cap runs on the global user VSpace. -/ +theorem vmRootA_global {σ : Fl4SpecA64.State Unit} {t : Fl4SpecA64.ObjRef} + (h : ∀ pt asid v, vtCapA σ t ≠ some (.archObjectCap (.pageTableCap pt .vsRootPtT (some (asid, v))))) : + vmRootA σ t = ⟨σ.archState.armUsGlobalVspace, none⟩ := by + unfold vmRootA + split + next pt asid v heq => exact absurd heq (h pt asid v) + next => rfl + +/-- The Stage-1 tie across a spec step that keeps the current thread's TCB, the global VSpace, and +`vspace_for_asid`. -/ +theorem HwS1TieA.of_spec {Z : XSetsA} {σ σ' : Fl4SpecA64.State Unit} (h : HwS1TieA Z σ) + (hc : σ'.curThread = σ.curThread) (hk : σ'.kheap σ.curThread = σ.kheap σ.curThread) + (hg : σ'.archState.armUsGlobalVspace = σ.archState.armUsGlobalVspace) + (hv : ∀ a, Fl4SpecA64.vspaceForAsid a σ' = Fl4SpecA64.vspaceForAsid a σ) : HwS1TieA Z σ' := by + unfold HwS1TieA at h ⊢ + rw [h, hc] + have e : vtCapA σ' σ.curThread = vtCapA σ σ.curThread := by unfold vtCapA; rw [hk] + unfold vmRootA + rw [e, hg] + simp only [hv] + +/-- The Stage-2 tie across the same steps, the root kinds kept. -/ +theorem HwS2TieA.of_spec {s2 : Option HwRootA} {σ σ' : Fl4SpecA64.State Unit} (h : HwS2TieA s2 σ) + (hc : σ'.curThread = σ.curThread) (hk : σ'.kheap σ.curThread = σ.kheap σ.curThread) + (hv : ∀ a, Fl4SpecA64.vspaceForAsid a σ' = Fl4SpecA64.vspaceForAsid a σ) + (hr : ∀ r, Fl4SpecA64.rootKindAt σ' r = Fl4SpecA64.rootKindAt σ r) : HwS2TieA s2 σ' := by + unfold HwS2TieA at h ⊢ + have e : vtCapA σ' σ.curThread = vtCapA σ σ.curThread := by unfold vtCapA; rw [hk] + have ev : vcpuOfA σ' σ.curThread = vcpuOfA σ σ.curThread := by unfold vcpuOfA; rw [hk] + rw [hc, ev] + intro hn + rw [h hn] + unfold vmS2RootA + rw [e] + simp only [hv, hr] + +/-- **The tie across a spec step** keeping the current thread's TCB, the global VSpace, `vspace_for_asid` and the +root kinds. -/ +theorem HwTieA.of_spec {Z : XSetsA} {s2 : Option HwRootA} {σ σ' : Fl4SpecA64.State Unit} (h : HwTieA Z s2 σ) + (hc : σ'.curThread = σ.curThread) (hk : σ'.kheap σ.curThread = σ.kheap σ.curThread) + (hg : σ'.archState.armUsGlobalVspace = σ.archState.armUsGlobalVspace) + (hv : ∀ a, Fl4SpecA64.vspaceForAsid a σ' = Fl4SpecA64.vspaceForAsid a σ) + (hr : ∀ r, Fl4SpecA64.rootKindAt σ' r = Fl4SpecA64.rootKindAt σ r) : HwTieA Z s2 σ' := + ⟨h.1.of_spec hc hk hg hv, h.2.of_spec hc hk hv hr⟩ + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/VsTopFrA.lean b/proofs/Fl4ProofsA64/P3/VsTopFrA.lean new file mode 100644 index 000000000..e51486a93 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/VsTopFrA.lean @@ -0,0 +1,312 @@ +/- +P3 (aarch64, A64-P3 S3 gen 31, ruling 01:37Z item 2): **the frame cap-kind top** — FL4's `syscall::decode_invocation` +on a `Cap::Frame` (plain or Stage-2, `is_ept`) against the spec's `fl4ArchDecodeInvocation` at FL4's label and +`fl4ArchPerformInvocation`, with `P3/VsTopPtA`'s uniform outcome `VsTopPostA`. The x86 counterpart is +`XTopJointVsMap`'s `decode_invocation_frameJV`. + +* **The labels** (`specInvA`, **`invType_specA`**: l4v's `invocation_type` of `specLabelWord l`; **`labNA_flushA`**: + FL4's cache labels are 51..54; **`flush_ofNA`**). +* **The spec's decoder on a frame cap**: **`dec_pgMap_specA`** (a plain frame at `ARMPageMap` with no FL4-Ext first + extra cap is l4v's `decodeFrameInvocation`), **`dec_frOtherA`** (a plain or Stage-2 frame at a label outside FL4's + frame labels: `IllegalOperation`). +* **FL4's decoder**: **`decode_frame_otherA`** (a label outside 49..57: `IllegalOperation`, nothing written). +* **`vsTop_ofPgMapA`**: `page_map_jA`'s `PgMapJA` through `liftME .spec`. +* (S3 gen 34: **`decInv_frame_jA`** moved to `P3/FrFlushA`, with the cache labels 51..54.) The top's arms: + `ARMPageMap` (a plain frame: `page_map_jA`); `ARMPageUnmap` by mapping, `is_ept`, ASID and the ASID's root kind + (`page_unmapU_decJA`, `page_unmapS2U_decJA`, `page_unmapZ_decJA`, `page_unmapS2Z_decJA`, `page_unmapS1_decJA`, + `page_unmap_decJA`); every label outside 49..57 `IllegalOperation` on both sides. + +**Case selectors (ruling: non-Ext selectors on the pure l4v top; not named premises).** `hLbl` — the label is not one +of FL4's 55..57 (S3 gen 34: was 51..57; the four cache flushes are `P3/FrFlushA`'s `fr_flushJA`): +`ARMPageDeriveSubframe`, `ARMPageTestAndClearAccessed`, `ARMPageGetAddress` (gen-32 arms, see the handoff: `GetAddress` / `TestAndClearAccessed` reply inside FL4's +decode, so their outcome is not `VsTopPostA`'s); `hMap` — at `ARMPageMap` the frame is plain (`is_ept = false`, the Stage-2 map is +the Ext `decodeS2PageMap`) with `FrPlA` (gen 20's decode-site placement); `hNX` (`NoExtXA`) — the first extra cap is no +FL4-Ext cap. `ARMPageUnmap` is total: the one arm without a top before this gen, a Stage-2 frame mapped with ASID 0, is +**`page_unmapS2Z_decJA`** (`extUnmapPage_zeroA`, `perfS2Unmap0_memA`, `page_unmapS20_eqA`; the `is_ept` twin of +`page_unmapZ_decJA`). + +Named premises: `ArchKeepsA` [P], `S2IpaE` [P] (as the unmap tops). Decode-site hypotheses (standing): `TabOffA`, +`ExtTabOffA`, `RsSiteA`, `FrPlA`. No new named premise; new module, no existing statement changed. +-/ +import Fl4ProofsA64.P3.VsTopPtA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Classical + +noncomputable section + +/-! ## The labels -/ + +section lbl +open Fl4SpecA64 + +/-- The l4v label `invocation_type` reads off `specLabelWord l`. -/ +def specInvA : Fl4Label → InvocationLabel + | .spec y => y + | .ext _ => .genInvocationLabel .invalidInvocation + +theorem invType_specA (l : Fl4Label) : invocationType (specLabelWord l) = specInvA l := by + cases l with + | spec y => exact invocationType_fromEnumA y + | ext e => + have h0 : specLabelWord (.ext e) = + BitVec.ofNat 64 (InvocationLabel.genInvocationLabel .invalidInvocation).fromEnum := by + show (0 : Data) = _; decide + rw [h0, invocationType_fromEnumA]; rfl + +set_option maxRecDepth 8000 in +/-- FL4's cache-maintenance label numbers are 51..54. -/ +theorem labNA_flushA : ∀ n, n < 74 → isPageFlushLabel (specInvA (labNA n)) = true → 51 ≤ n ∧ n ≤ 54 := by + decide + +theorem flush_ofNA {n : Nat} (h : n < 51 ∨ 54 < n) : + isPageFlushLabel (invocationType (specLabelWord (labNA n))) = false := by + rw [invType_specA] + cases hb : isPageFlushLabel (specInvA (labNA n)) + · rfl + exfalso + by_cases hn : n < 74 + · have := labNA_flushA n hn hb; omega + · rw [labNA_geA (by omega)] at hb; exact absurd hb (by decide) + +theorem invType_pgMapA : invocationType (specLabelWord (archL .armPageMap)) = .archInvocationLabel .armPageMap := + invocationType_fromEnumA _ + +theorem flushLbl_pgMapA : isPageFlushLabel (invocationType (specLabelWord (archL .armPageMap))) = false := by + rw [invType_pgMapA]; rfl + +end lbl + +/-! ## The spec's decoder on a frame cap -/ + +section dec +open Fl4SpecA64 + +variable (calling : Bool) (args : List Data) (xSlot : CapRef) (cte : CslotPtr) (exs : List (Cap × CslotPtr)) + {σ : State Unit} + +/-- **`ARMPageMap` of a plain frame with no FL4-Ext first extra cap is l4v's decoder** (module docstring). -/ +theorem dec_pgMap_specA (p : ObjRef) (R : CapRights) (sz : VMPageSize) (dev : Bool) (mp : Option (Asid × VSpaceRef)) + (hN : ∀ c sl rest, exs = (c, sl) :: rest → ∀ e, c ≠ .archObjectCap (.ext e)) : + fl4ArchDecodeInvocation (Z := Unit) calling (archL .armPageMap) args xSlot cte (.frameCap p R sz dev mp) exs = + Fl4Spec.NonDetE.liftME Fl4ArchInvocation.spec (decodeFrameInvocation (Z := Unit) + (specLabelWord (archL .armPageMap)) args cte (.frameCap p R sz dev mp) exs) := by + have h1 : ¬ (archL .armPageMap = Fl4Label.ext .armPageDeriveSubframe) := fun h => by cases h + have h2 : ¬ (archL .armPageMap = Fl4Label.ext .armPageTestAndClearAccessed) := fun h => by cases h + have h3 : ¬ (archL .armPageMap = archL .armPageUnmap) := fun h => by cases h + unfold fl4ArchDecodeInvocation + dsimp only + rw [if_neg h1, if_neg h2, flushLbl_pgMapA, if_neg Bool.false_ne_true] + by_cases h : archL .armPageMap = archL .armPageMap ∧ args.length > 2 ∧ exs.length > 0 + · rw [if_pos h] + obtain ⟨⟨c, sl⟩, rest, rfl⟩ : ∃ e rest, exs = e :: rest := by + cases exs with + | nil => simp at h + | cons e rest => exact ⟨e, rest, rfl⟩ + dsimp only [List.getElem?_cons_zero, Fl4SpecA64.the] + split + · exact absurd rfl (hN _ _ _ rfl _) + · exact absurd rfl (hN _ _ _ rfl _) + · rfl + · rw [if_neg h, if_neg h3] + rfl + +/-- **A frame cap at a label outside FL4's frame labels: `IllegalOperation`** (module docstring). -/ +theorem dec_frOtherA (l : Fl4Label) (hd : l ≠ .ext .armPageDeriveSubframe) + (ht : l ≠ .ext .armPageTestAndClearAccessed) (hf : isPageFlushLabel (invocationType (specLabelWord l)) = false) + (hm : l ≠ archL .armPageMap) (hu : l ≠ archL .armPageUnmap) (hg : l ≠ archL .armPageGetAddress) + (isEpt : Bool) (p : ObjRef) (R : CapRights) (sz : VMPageSize) (dev : Bool) (mp : Option (Asid × VSpaceRef)) : + (fl4ArchDecodeInvocation (Z := Unit) calling l args xSlot cte (absFrameE isEpt p R sz dev mp) exs).run σ = + ({(.error .illegalOperation, σ)}, False) := by + have hsp : (Fl4Spec.NonDetE.liftME Fl4ArchInvocation.spec (archDecodeInvocation (Z := Unit) (specLabelWord l) + args xSlot cte (.frameCap p R sz dev mp) exs)).run σ = ({(.error .illegalOperation, σ)}, False) := by + unfold Fl4Spec.NonDetE.liftME + refine run_bind_errorD ?_ _ + unfold archDecodeInvocation + dsimp only + unfold decodeFrameInvocation + try dsimp only + rw [if_neg (fun h => hm (invType_archA h)), if_neg (fun h => hu (invType_archA h)), + if_neg (fun h => hg (invType_archA h)), hf] + rfl + cases isEpt with + | false => + show (fl4ArchDecodeInvocation (Z := Unit) calling l args xSlot cte (.frameCap p R sz dev mp) exs).run σ = _ + unfold fl4ArchDecodeInvocation + dsimp only + rw [if_neg hd, if_neg ht, hf, if_neg Bool.false_ne_true, if_neg (fun h => hm h.1), if_neg hu] + exact hsp + | true => + show (fl4ArchDecodeInvocation (Z := Unit) calling l args xSlot cte (.ext (.stage2FrameCap p R sz dev mp)) + exs).run σ = _ + unfold fl4ArchDecodeInvocation + dsimp only + rw [if_neg hd, if_neg ht, hf, if_neg Bool.false_ne_true, if_neg (fun h => hm h.1), if_neg hu] + exact hsp + +end dec + +/-! ## The mapped Stage-2 frame with ASID 0 (`ARMPageUnmap`) -/ + +section s2z +open Fl4SpecA64 + +/-- The Ext `ept::unmap` of ASID 0: no root names it, the lookup fails and is caught; nothing changes. -/ +theorem extUnmapPage_zeroA (sz : VMPageSize) (va : VSpaceRef) (p : ObjRef) (σ : State Unit) : + ((), σ) ∈ (extUnmapPage (Z := Unit) .stage2 sz (BitVec.ofNat 16 0) va p σ).1 := by + rw [extUnmapPage_runX] + have hr : extRootResX σ .stage2 (BitVec.ofNat 16 0) = .error .invalidRoot := by + simp only [extRootResX, vspaceForAsid_zeroA] + have hst : extUnmapPageStX σ .stage2 sz (BitVec.ofNat 16 0) va p = σ := by + apply extUnmapPageStX_noneX + by_cases hg : extVaOk .stage2 va + · simp only [extPageSlotX, if_pos hg, hr] + · simp only [extPageSlotX, if_neg hg] + rw [hst]; exact Set.mem_singleton _ + +/-- The Ext `.pageUnmap .stage2` of a mapped Stage-2 frame cap whose unmap changes nothing: the cap write. -/ +theorem perfS2Unmap0_memA {σ1 σ3 : State Unit} {p : ObjRef} {R : CapRights} {sz : VMPageSize} {dev : Bool} + {asid : Asid} {v : VSpaceRef} {cte : CslotPtr} + (hu : ((), σ1) ∈ (extUnmapPage (Z := Unit) .stage2 sz asid v p σ1).1) + (hs : ((), σ3) ∈ (setCap (Z := Unit) (.archObjectCap (.frameCap p R sz dev none)) cte σ1).1) : + (([] : List Data), σ3) ∈ (performExtVSpace (Z := Unit) + (.pageUnmap .stage2 (.ext (.stage2FrameCap p R sz dev (some (asid, v)))) cte) σ1).1 := by + unfold performExtVSpace + dsimp only + exact bind_memA hu (bind_memA hs (Set.mem_singleton _)) + +end s2z + +/-! ## The top -/ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} +variable {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} {ia ie : Nat} + +/-- **`PgMapJA` through `liftME .spec`** (module docstring). -/ +theorem vsTop_ofPgMapA {ar : machine.Arch A} {m0 : Fl4SpecA64.SEMonad Unit Fl4SpecA64.ArchInvocation} + {r : Result (core.result.Result Unit error.Exc × S × A)} + (h : PgMapJA (L := L) ar AR Z OS m σ (m0.run σ) s a r) : + r ⦃ res => VsTopPostA (L := L) AR Z OS m σ + ((Fl4Spec.NonDetE.liftME Fl4SpecA64.Fl4ArchInvocation.spec m0).run σ) s a res ⦄ := by + rcases h with ⟨e, e', hr, hD, he⟩ | ⟨s', iv, hD, σ1, h3, σ', h4, hr⟩ + · rw [hr]; exact (WP.spec_ok _).2 ⟨rfl, rfl, e', run_liftME_errA hD, he⟩ + · exact WP.spec_mono hr fun _ ⟨a', hx, J, T, C⟩ => by + subst hx + exact ⟨.spec (.invokePage iv), okD_memA (run_liftME_okA hD), σ1, h3, σ', [], mem_archPgA h4, Z, rfl, rfl, J, T, C⟩ + +/-- FL4's `decode_frame` at a label outside 49..57: `IllegalOperation`, nothing written. -/ +theorem decode_frame_otherA {ar : machine.Arch A} (lbl length slot buf : U64) (ex : Aeneas.Std.Array obj.Slot 3#usize) + (fp rt asid va : U64) (sz : capability.FrameSize) (d mp e call : Bool) (h : lbl.val < 49 ∨ 57 < lbl.val) : + vspace.decode_frame st ar s a lbl length slot (.Frame fp sz rt d mp e asid va) call buf ex = + ok (.Err (.Sys .IllegalOperation), s, a) := by + have hct : sched.cur_thread st s = ok (L.curW s) := cur_thread_ok s + have h49 : lbl ≠ fl4_abi.label.ARMPageMap := fun e => by subst e; simp [fl4_abi.label.ARMPageMap] at h + have h50 : lbl ≠ fl4_abi.label.ARMPageUnmap := fun e => by subst e; simp [fl4_abi.label.ARMPageUnmap] at h + have h51 : lbl ≠ fl4_abi.label.ARMPageClean_Data := fun e => by + subst e; simp [fl4_abi.label.ARMPageClean_Data] at h + have h52 : lbl ≠ fl4_abi.label.ARMPageInvalidate_Data := fun e => by + subst e; simp [fl4_abi.label.ARMPageInvalidate_Data] at h + have h53 : lbl ≠ fl4_abi.label.ARMPageCleanInvalidate_Data := fun e => by + subst e; simp [fl4_abi.label.ARMPageCleanInvalidate_Data] at h + have h54 : lbl ≠ fl4_abi.label.ARMPageUnify_Instruction := fun e => by + subst e; simp [fl4_abi.label.ARMPageUnify_Instruction] at h + have h55 : lbl ≠ fl4_abi.label.ARMPageDeriveSubframe := fun e => by + subst e; simp [fl4_abi.label.ARMPageDeriveSubframe] at h + have h56 : lbl ≠ fl4_abi.label.ARMPageTestAndClearAccessed := fun e => by + subst e; simp [fl4_abi.label.ARMPageTestAndClearAccessed] at h + have h57 : lbl ≠ fl4_abi.label.ARMPageGetAddress := fun e => by + subst e; simp [fl4_abi.label.ARMPageGetAddress] at h + unfold vspace.decode_frame vspace.as_frame vspace.is_cache_label + simp [hct, h49, h50, h51, h52, h53, h54, h55, h56, h57, error.illegal] + +/-- **FL4's `ARMPageUnmap` of a Stage-2 frame mapped with ASID 0**: the restart and the cap write (the unmap is +skipped, `f.asid != 0`). -/ +theorem page_unmapS20_eqA (ar : machine.Arch A) (s : S) (a : A) (length : U64) (slot : obj.Slot) + (fp rt va : U64) (sz : capability.FrameSize) (dev call : Bool) (buf : U64) + (ex : Aeneas.Std.Array obj.Slot 3#usize) : + vspace.decode_frame st ar s a fl4_abi.label.ARMPageUnmap length slot (.Frame fp sz rt dev true true 0#u64 va) + call buf ex = + (do let s1 ← sched.set_thread_state st s (L.curW s) sched.TS_RESTART + let s3 ← obj.Slot.set_cap st slot s1 (.Frame fp sz rt dev false false 0#u64 0#u64) + ok (core.result.Result.Ok (), s3, a)) := by + have hct : sched.cur_thread st s = ok (L.curW s) := cur_thread_ok s + have hne : ¬ fl4_abi.label.ARMPageUnmap = fl4_abi.label.ARMPageMap := by + unfold fl4_abi.label.ARMPageUnmap fl4_abi.label.ARMPageMap; decide + have hb : ((0#u64 : U64) != 0#u64) = false := by decide + unfold vspace.decode_frame + simp only [vspace.as_frame, hct, bind_ok, hne, hb, eq_self_iff_true, if_true, ↓reduceIte, vspace.unmapped_frame, + Bool.false_eq_true] + rfl + +set_option maxHeartbeats 2000000 in +/-- **`ARMPageUnmap` of a Stage-2 frame cap mapped with ASID 0** (the arm gen 29's `page_unmapZ_decJA` leaves for +`is_ept`): the spec's Ext `.pageUnmap .stage2` at the decoder; FL4 skips the unmap and writes the unmapped cap, the +spec's `ept::unmap` of ASID 0 is a no-op (`extUnmapPage_zeroA`) and it writes the plain unmapped frame +(`extUnmappedFrame`); `JAtZAX`, the tree and `CarryA` after. -/ +theorem page_unmapS2Z_decJA {ar : machine.Arch A} + (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) + (length buf : U64) (call calling : Bool) (ex : Aeneas.Std.Array obj.Slot 3#usize) (args : List Fl4SpecA64.Data) + (xSlot : Fl4SpecA64.CapRef) (exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) + (cte : Fl4SpecA64.CslotPtr) (hab : AbsSlotA Z.th cte) + (fp rt va : U64) (sz : capability.FrameSize) (dev : Bool) + (hcap : capA st s (cslotAddrA cte) = .Frame fp sz rt dev true true 0#u64 va) : + (Fl4SpecA64.fl4ArchDecodeInvocation (Z := Unit) calling (Fl4SpecA64.archL .armPageUnmap) args xSlot cte + (.ext (.stage2FrameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev + (some (BitVec.ofNat 16 (0#u64 : U64).val, va.bv)))) exs).run σ = + ({(.ok (.extVSpace (.pageUnmap .stage2 (.ext (.stage2FrameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) + dev (some (BitVec.ofNat 16 (0#u64 : U64).val, va.bv)))) cte)), σ)}, False) ∧ + ∃ x, σ.kheap σ.curThread = some (.tcb x) ∧ + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), stUpdA σ σ.curThread x .restart)}, False) ∧ + (TreeA st OS Z.G.chains Z.G.par s → (∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) → + ∃ (s3 : S) (σ3 : Fl4SpecA64.State Unit), + vspace.decode_frame st ar s a fl4_abi.label.ARMPageUnmap length (cslotAddrA cte) + (.Frame fp sz rt dev true true 0#u64 va) call buf ex = + ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s3, a) ∧ + (([] : List Fl4SpecA64.Data), σ3) ∈ (Fl4SpecA64.performExtVSpace (Z := Unit) + (.pageUnmap .stage2 (.ext (.stage2FrameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev + (some (BitVec.ofNat 16 (0#u64 : U64).val, va.bv)))) cte) (stUpdA σ σ.curThread x .restart)).1 ∧ + JAtZAX (L := L) AR Z s3 a σ3 m ∧ TreeA st OS Z.G.chains Z.G.par s3 ∧ + (CarryA Z (L.mem s) σ → CarryA Z (L.mem s3) σ3)) := by + have hcap' : capAtA (L.mem s) (cslotAddrA cte) = .Frame fp sz rt dev true true 0#u64 va := hcap + have e0 : (0#u64 : U64).val = 0 := rfl + obtain ⟨c0, -, hrel0⟩ := StateRelA.getAbsA E.j.1.st hab + rw [hcap'] at hrel0 + obtain ⟨⟨hr1, hr3, -, hov⟩, -⟩ := hrel0 + have hEnc0 : CapRt.EncWf (capAtA (L.mem s) (cslotAddrA cte)) := CapRt.decodeF_encWf _ _ + rw [hcap'] at hEnc0 + have hcan : CapRt.Canonical fp ∧ rt.val < 4 := by simp only [CapRt.EncWf] at hEnc0; exact ⟨hEnc0.1, hEnc0.2.1⟩ + have hEnc : CapRt.EncWf (.Frame fp sz rt dev false false 0#u64 0#u64) := by + simp only [CapRt.EncWf]; exact ⟨hcan.1, hcan.2, by simp, Or.inl fun j hj _ => by simp⟩ + have hca : CapRelA (.Frame fp sz rt dev false false 0#u64 0#u64) + (.archObjectCap (.frameCap (specPtr fp) (vmRightsOfA rt) (frameSizeOfA sz) dev none)) := + ⟨⟨hr1, hr3, by simp, hov⟩, rfl⟩ + have hok : CapObjOkA Z (.Frame fp sz rt dev false false 0#u64 0#u64) := + ⟨fun _ h => (by cases h), fun _ _ _ _ _ h => (by cases h), fun _ _ _ h => (by cases h), fun _ _ _ h => (by cases h), (by intro h; cases h)⟩ + obtain ⟨Rp, Nt, extra, sp, P⟩ := RS.pl + obtain ⟨x, hkx, hst, hset, E1, -, hOK, -, -⟩ := E.restartA P RS.sym RS.run RS.cur RS.vs RS.vc + refine ⟨dec_pgUnmapS2A calling args xSlot cte exs _ _ _ dev _, x, hkx, hset, fun hTr hnul => ?_⟩ + have hks : L.ksb (L.wr s (stA (L.curW s)) sched.TS_RESTART) = L.ksb s := L.ksb_wr _ _ _ + have hcpu : L.cpu (L.wr s (stA (L.curW s)) sched.TS_RESTART) = L.cpu s := L.cpu_wr _ _ _ + generalize L.wr s (stA (L.curW s)) sched.TS_RESTART = s1 at hst E1 hOK hks hcpu + obtain ⟨s3, σ3, hsc, -, hσ3, J3, T3⟩ := setcap_coreA E RS E1.j hOK hks hcpu cte hab hcap hEnc hca + (UA.of_shapeA rfl fun h => by cases h) hok (fun h => by cases h) (fun h => by cases h) hTr hnul + have hu0 : ((), stUpdA σ σ.curThread x .restart) ∈ (Fl4SpecA64.extUnmapPage (Z := Unit) .stage2 (frameSizeOfA sz) + (BitVec.ofNat 16 (0#u64 : U64).val) va.bv (specPtr fp) (stUpdA σ σ.curThread x .restart)).1 := by + rw [e0]; exact extUnmapPage_zeroA _ _ _ _ + have hP := perfS2Unmap0_memA (R := vmRightsOfA rt) (dev := dev) (cte := cte) hu0 hσ3 + refine ⟨s3, σ3, ?_, hP, J3, T3, fun C => C.of_setcapA E1.sys.ent.os RS.sub hTr hOK + (RS.sub _ (hab.isSlot E.j.1.place)) hEnc hsc (fun _ _ _ h => by cases h) + (by show CapFinEqA _ (capA st s _); rw [hcap]; exact CapFinEqA.frameA _ _ _ _ _ _ _ _) + ((TyKeepA.stUpdA hkx _).trans (perfExtPgUnmap_tyA _ _ _ _ _ hP))⟩ + rw [page_unmapS20_eqA ar s a length (cslotAddrA cte) fp rt va sz dev call buf ex, hst, bind_ok, hsc, bind_ok] + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/VsTopPtA.lean b/proofs/Fl4ProofsA64/P3/VsTopPtA.lean new file mode 100644 index 000000000..506eb00ee --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/VsTopPtA.lean @@ -0,0 +1,369 @@ +/- +P3 (aarch64, A64-P3 S3 gen 31, ruling 01:37Z items 1): **the page-table cap-kind top** — FL4's +`syscall::decode_invocation` on a `Cap::PageTable`, every label, against the spec's `fl4ArchDecodeInvocation` at FL4's +label (`fl4LabelOf`, as `fl4DecodeInvocation` calls it) and `fl4ArchPerformInvocation`, with one uniform outcome +(`VsTopPostA`). The x86 counterpart is `P3/VsDecTop` / `XTopJointVsMap`'s `decode_invocation_page_tableJV` (one top per +cap kind, every label, `TopPost`). + +* **`VsTopPostA`** (x86 `TopPost`, A64 shape of `PtMapJPostA`): an error leaves FL4's state and arch state unchanged, + the spec's decode has the single related error outcome (`VsErrRelA`) at `σ`; a success is an outcome `(.ok inv, σ)` of + the spec's decode with no failure (S3 gen 33, ruling 02:15Z F2: membership, `okD_memA`; l4v's `select_ext` is + nondeterministic), the caller's restart `σ1`, an outcome of `fl4ArchPerformInvocation inv` at `σ1`, and at FL4's returned state + `JAtZAX` at some ghosts `Z'` (same `th`, same MDB ghost `G`), the MDB tree, and `CarryA Z → CarryA Z'`. + Bridges: **`vsTop_ofPtMapA`** (from `PtMapJPostA` through `liftME .spec`), **`vsTop_ofFaA`** (from the unmap tops' + `fa ⦃ JAtZAX ⦄` shape), **`mem_archNRA`** (l4v's `arch_no_return` of a page-table perform), **`run_liftME_okA`** / + **`run_liftME_errA`**. +* **The label** (`labNA`, **`fl4LabelOf_bvA`**, **`labNA_tabA`** — FL4's numbers 47..50, 55..57 are exactly the + VSpace labels' positions — **`labNA_geA`**, **`invType_archA`**: l4v's `invocation_type` of `specLabelWord l` is an + arch label only for that label). +* **The spec's decoder on a page-table cap**: **`dec_ptOtherA`** (a label other than `ARMPageTableMap` / + `ARMPageTableUnmap`: `IllegalOperation`, state unchanged) and **`dec_ptMap_specA`** (at `ARMPageTableMap` with no + FL4-Ext first extra cap, `fl4ArchDecodeInvocation` is l4v's `decodePageTableInvocation` under `.spec`). +* **The non-Ext selector** (ruling: a clean case selector, no Stage-2 / HighVSpace Ext spec built here): + **`NoExtXA`** — FL4's first extra cap is no `Ept`, no `HighVSpace` and no Stage-2 (`is_ept`) frame cap; + **`NoExtXA.s1`** / **`.hi`** (the map tops' `hS1` / `hHi`), **`NoExtXA.spec`** (through `ExtraRelA`, the spec's first + extra cap is no `.archObjectCap (.ext _)`). +* **`decInv_pt_jA`**: the top. `ARMPageTableUnmap` by FL4's `is_final_capability` (total, `P0`), the mapping and the + ASID's root kind: non-final `pt_unmap_nfJA`, unmapped `pt_unmapU_decJA`, on no root / a Stage-1 root + `pt_unmapS1_decJA`, on an FL4 root `pt_unmap_decJA`; `ARMPageTableMap` `pt_map_jA` (`dec_ptMap_specA`); any other + label FL4's `IllegalOperation` against `dec_ptOtherA`. + +Named premises: `ArchKeepsA` [P] and `S2IpaE` [P] (as the unmap tops). Decode-site hypotheses (standing): `TabOffA`, +`ExtTabOffA`, `RsSiteA`; at `ARMPageTableMap` only (`hMap`), `UnmPtA` and ruling (c)'s `ptr ∉ S2PgE ∧ ptr ∉ Y`. +Lemma hypotheses: `PtCapInA`, `FinInvA` (as the unmap tops), the front end's argument / extra-cap facts (`hlen`, +`hargs`, `hx0`, `hex`, as `pt_map_jA`), the selector `NoExtXA`. No new named premise; new module, no existing statement +changed. +-/ +import Fl4ProofsA64.P3.VsArmA +import Fl4ProofsA64.P3.UnmapPtS1JA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Classical + +noncomputable section + +/-! ## The uniform outcome -/ + +section post +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-- **The outcome of a VSpace cap-kind top** (module docstring; x86 `TopPost`). -/ +def VsTopPostA (AR : A → HwRootA → Prop) (Z : XSetsA) (OS : Set U64) (m : Fl4SpecA64.McState) + (σ : Fl4SpecA64.State Unit) + (D : Set (Except Fl4SpecA64.SyscallError Fl4SpecA64.Fl4ArchInvocation × Fl4SpecA64.State Unit) × Prop) + (s : S) (a : A) : core.result.Result Unit error.Exc × S × A → Prop + | (.Err e, s', a') => s' = s ∧ a' = a ∧ ∃ e', D = ({(.error e', σ)}, False) ∧ VsErrRelA e e' + | (.Ok (), s', a') => ∃ inv, ((.ok inv, σ) ∈ D.1 ∧ ¬ D.2) ∧ ∃ σ1, + Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ∃ (σ' : Fl4SpecA64.State Unit) (rv : List Fl4SpecA64.Data), + (.ok rv, σ') ∈ ((Fl4SpecA64.fl4ArchPerformInvocation (Z := Unit) inv).run σ1).1 ∧ + ∃ Z' : XSetsA, Z'.th = Z.th ∧ Z'.G = Z.G ∧ JAtZAX (L := L) AR Z' s' a' σ' m ∧ + TreeA st OS Z.G.chains Z.G.par s' ∧ (CarryA Z (L.mem s) σ → CarryA Z' (L.mem s') σ') + +/-- (S3 gen 33, ruling 02:15Z F2) A single decode outcome is one of the decode's outcomes. -/ +theorem okD_memA {α : Type} {D : Set (Except Fl4SpecA64.SyscallError α × Fl4SpecA64.State Unit) × Prop} + {σ : Fl4SpecA64.State Unit} {x : α} (h : D = ({(.ok x, σ)}, False)) : (.ok x, σ) ∈ D.1 ∧ ¬ D.2 := by + subst h; exact ⟨rfl, id⟩ + +theorem run_liftME_okA {α β : Type} {f : α → β} {m : Fl4Spec.NonDetE (Fl4SpecA64.State Unit) Fl4SpecA64.SyscallError α} + {σ : Fl4SpecA64.State Unit} {x : α} (h : m.run σ = ({(.ok x, σ)}, False)) : + (Fl4Spec.NonDetE.liftME f m).run σ = ({(.ok (f x), σ)}, False) := by + unfold Fl4Spec.NonDetE.liftME + rw [run_bind_okD h]; rfl + +theorem run_liftME_errA {α β : Type} {f : α → β} {m : Fl4Spec.NonDetE (Fl4SpecA64.State Unit) Fl4SpecA64.SyscallError α} + {σ : Fl4SpecA64.State Unit} {e : Fl4SpecA64.SyscallError} (h : m.run σ = ({(.error e, σ)}, False)) : + (Fl4Spec.NonDetE.liftME f m).run σ = ({(.error e, σ)}, False) := by + unfold Fl4Spec.NonDetE.liftME + exact run_bind_errorD h _ + +/-- l4v's `arch_no_return` of a page-table perform, at `fl4ArchPerformInvocation`. -/ +theorem mem_archNRA {iv : Fl4SpecA64.PageTableInvocation} {σ1 σ' : Fl4SpecA64.State Unit} + (h : ((), σ') ∈ (Fl4SpecA64.performPageTableInvocation (Z := Unit) iv σ1).1) : + (.ok ([] : List Fl4SpecA64.Data), σ') ∈ + ((Fl4SpecA64.fl4ArchPerformInvocation (Z := Unit) (.spec (.invokePageTable iv))).run σ1).1 := + mem_liftED (mem_bind_ndD h rfl) + +/-- A page perform at `fl4ArchPerformInvocation`. -/ +theorem mem_archPgA {iv : Fl4SpecA64.PageInvocation} {σ1 σ' : Fl4SpecA64.State Unit} {rv : List Fl4SpecA64.Data} + (h : (rv, σ') ∈ (Fl4SpecA64.performPageInvocation (Z := Unit) iv σ1).1) : + (.ok rv, σ') ∈ ((Fl4SpecA64.fl4ArchPerformInvocation (Z := Unit) (.spec (.invokePage iv))).run σ1).1 := + mem_liftED h + +/-- An FL4-Ext VSpace perform at `fl4ArchPerformInvocation`. -/ +theorem mem_archExtA {iv : Fl4SpecA64.ExtVSpaceInvocation} {σ1 σ' : Fl4SpecA64.State Unit} + {rv : List Fl4SpecA64.Data} (h : (rv, σ') ∈ (Fl4SpecA64.performExtVSpace (Z := Unit) iv σ1).1) : + (.ok rv, σ') ∈ ((Fl4SpecA64.fl4ArchPerformInvocation (Z := Unit) (.extVSpace iv)).run σ1).1 := + mem_liftED h + +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {OS : Set U64} {m : Fl4SpecA64.McState} {σ : Fl4SpecA64.State Unit} + {s : S} {a : A} + +/-- **`PtMapJPostA` through `liftME .spec`** (module docstring). -/ +theorem vsTop_ofPtMapA {ptr : U64} {m0 : Fl4SpecA64.SEMonad Unit Fl4SpecA64.ArchInvocation} : + ∀ {res}, PtMapJPostA (L := L) AR Z OS m ptr σ (m0.run σ) s a res → + VsTopPostA (L := L) AR Z OS m σ ((Fl4Spec.NonDetE.liftME Fl4SpecA64.Fl4ArchInvocation.spec m0).run σ) s a res + | (.Err _, _, _), ⟨h1, h2, e', hD, he⟩ => ⟨h1, h2, e', run_liftME_errA hD, he⟩ + | (.Ok (), _, _), ⟨h1, iv, hD, σ1, h3, σ', n, h4, J, T, C⟩ => by + subst h1 + exact ⟨.spec (.invokePageTable iv), okD_memA (run_liftME_okA hD), σ1, h3, σ', [], mem_archNRA h4, + { Z with S1 := insert ptr Z.S1, Lv := Function.update Z.Lv ptr n }, rfl, rfl, J, T, C⟩ + +/-- **The unmap tops' outcome shape** (module docstring). -/ +theorem vsTop_ofFaA {D : Set (Except Fl4SpecA64.SyscallError Fl4SpecA64.Fl4ArchInvocation × Fl4SpecA64.State Unit) × Prop} + {inv : Fl4SpecA64.Fl4ArchInvocation} (hD : D = ({(.ok inv, σ)}, False)) {σ1 : Fl4SpecA64.State Unit} + (h1 : Fl4SpecA64.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False)) + {σ3 : Fl4SpecA64.State Unit} {rv : List Fl4SpecA64.Data} + (hp : (.ok rv, σ3) ∈ ((Fl4SpecA64.fl4ArchPerformInvocation (Z := Unit) inv).run σ1).1) {s3 : S} {fa : Result A} + (hJ : fa ⦃ a' => JAtZAX (L := L) AR Z s3 a' σ3 m ⦄) (hT3 : TreeA st OS Z.G.chains Z.G.par s3) + (hC : CarryA Z (L.mem s) σ → CarryA Z (L.mem s3) σ3) : + (do let a1 ← fa; ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s3, a1)) ⦃ res => + VsTopPostA (L := L) AR Z OS m σ D s a res ⦄ := + WP.spec_bind hJ fun _ hJ1 => (WP.spec_ok _).2 ⟨inv, okD_memA hD, σ1, h1, σ3, rv, hp, Z, rfl, rfl, hJ1, hT3, hC⟩ + +end post + +/-! ## The label -/ + +section lbl +open Fl4SpecA64 + +/-- FL4's label number `n` as the spec reads it (`fl4LabelOf`). -/ +def labNA (n : Nat) : Fl4Label := (fl4Numbering[n]?).getD (.spec (.genInvocationLabel .invalidInvocation)) + +theorem fl4LabelOf_bvA (lbl : U64) : fl4LabelOf lbl.bv = labNA lbl.val := rfl + +set_option maxRecDepth 8000 in +/-- FL4's VSpace label numbers are exactly the VSpace labels' positions. -/ +theorem labNA_tabA : ∀ n, n < 74 → + (labNA n = archL .armPageTableMap → n = 47) ∧ (labNA n = archL .armPageTableUnmap → n = 48) ∧ + (labNA n = archL .armPageMap → n = 49) ∧ (labNA n = archL .armPageUnmap → n = 50) ∧ + (labNA n = .ext .armPageDeriveSubframe → n = 55) ∧ (labNA n = .ext .armPageTestAndClearAccessed → n = 56) ∧ + (labNA n = archL .armPageGetAddress → n = 57) := by + decide + +theorem labNA_geA {n : Nat} (h : 74 ≤ n) : labNA n = .spec (.genInvocationLabel .invalidInvocation) := by + unfold labNA + rw [List.getElem?_eq_none (by show n ≥ 74; exact h)]; rfl + +/-- A label number other than `k` is not the label at `k` (for the labels of `labNA_tabA`). -/ +theorem labNA_neA {n k : Nat} {l : Fl4Label} (hl : ∀ n, n < 74 → labNA n = l → n = k) + (hd : l ≠ .spec (.genInvocationLabel .invalidInvocation)) (h : n ≠ k) : labNA n ≠ l := fun e => by + by_cases hn : n < 74 + · exact h (hl n hn e) + · rw [labNA_geA (by omega)] at e; exact hd e.symm + +/-- l4v's `invocation_type` of `specLabelWord l` is an arch label only for that label. -/ +theorem invType_archA {l : Fl4Label} {x : ArchInvocationLabel} + (h : invocationType (specLabelWord l) = .archInvocationLabel x) : l = archL x := by + cases l with + | spec y => + have e : invocationType (specLabelWord (.spec y)) = y := invocationType_fromEnumA y + rw [e] at h; subst h; rfl + | ext e => + have h0 : specLabelWord (.ext e) = + BitVec.ofNat 64 (InvocationLabel.genInvocationLabel .invalidInvocation).fromEnum := by + show (0 : Data) = _; decide + rw [h0, invocationType_fromEnumA] at h; cases h + +theorem invType_ptMapA : + invocationType (specLabelWord (archL .armPageTableMap)) = .archInvocationLabel .armPageTableMap := + invocationType_fromEnumA _ + +end lbl + +/-! ## The spec's decoder on a page-table cap -/ + +section dec +open Fl4SpecA64 + +variable (calling : Bool) (args : List Data) (xSlot : CapRef) (cte : CslotPtr) (exs : List (Cap × CslotPtr)) + {σ : State Unit} + +/-- **A page-table cap at any other label: `IllegalOperation`** (module docstring). -/ +theorem dec_ptOtherA (l : Fl4Label) (h1 : l ≠ archL .armPageTableMap) (h2 : l ≠ archL .armPageTableUnmap) + (p : ObjRef) (mp : Option (Asid × VSpaceRef)) : + (fl4ArchDecodeInvocation (Z := Unit) calling l args xSlot cte (.pageTableCap p .normalPtT mp) exs).run σ = + ({(.error .illegalOperation, σ)}, False) := by + unfold fl4ArchDecodeInvocation + dsimp only + rw [if_neg (fun h => h1 h.1), if_neg h2] + unfold Fl4Spec.NonDetE.liftME + refine run_bind_errorD ?_ _ + unfold archDecodeInvocation + dsimp only + unfold decodePageTableInvocation + try dsimp only + rw [if_neg (fun h => h1 (invType_archA h)), if_neg (fun h => h2 (invType_archA h))] + rfl + +/-- **`ARMPageTableMap` with no FL4-Ext first extra cap is l4v's decoder** (module docstring). -/ +theorem dec_ptMap_specA (p : ObjRef) (mp : Option (Asid × VSpaceRef)) + (hN : ∀ c sl rest, exs = (c, sl) :: rest → ∀ e, c ≠ .archObjectCap (.ext e)) : + fl4ArchDecodeInvocation (Z := Unit) calling (archL .armPageTableMap) args xSlot cte + (.pageTableCap p .normalPtT mp) exs = + Fl4Spec.NonDetE.liftME Fl4ArchInvocation.spec (decodePageTableInvocation (Z := Unit) + (specLabelWord (archL .armPageTableMap)) args cte (.pageTableCap p .normalPtT mp) exs) := by + unfold fl4ArchDecodeInvocation + dsimp only + by_cases h : archL .armPageTableMap = archL .armPageTableMap ∧ args.length > 1 ∧ exs.length > 0 + · rw [if_pos h] + obtain ⟨⟨c, sl⟩, rest, rfl⟩ : ∃ e rest, exs = e :: rest := by + cases exs with + | nil => simp at h + | cons e rest => exact ⟨e, rest, rfl⟩ + dsimp only [List.getElem?_cons_zero, Fl4SpecA64.the] + split + · exact absurd rfl (hN _ _ _ rfl _) + · rfl + · rw [if_neg h] + rfl + +end dec + +/-! ## The non-Ext selector -/ + +section sel + +/-- **FL4's first extra cap is no FL4-Ext VSpace cap** (module docstring). -/ +def NoExtXA : capability.Cap → Prop + | .Ept .. => False + | .HighVSpace .. => False + | .Frame _ _ _ _ _ true _ _ => False + | _ => True + +theorem NoExtXA.s1 {c : capability.Cap} (h : NoExtXA c) : ∀ p l mp asid va, c ≠ .Ept p l mp asid va := + fun _ _ _ _ _ e => by subst e; exact h + +theorem NoExtXA.hi {c : capability.Cap} (h : NoExtXA c) : ∀ p mp asid, c ≠ .HighVSpace p mp asid := + fun _ _ _ e => by subst e; exact h + +/-- Through `ExtraRelA`, the spec's first extra cap is no FL4-Ext cap. -/ +theorem NoExtXA.spec {th : TypedHeapA} {mem : U64 → U64} {x0 : U64} + {exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)} (hex : ExtraRelA th mem x0 (exs.map Prod.fst)) + (hN : NoExtXA (capAtA mem x0)) : + ∀ c sl rest, exs = (c, sl) :: rest → ∀ e, c ≠ .archObjectCap (.ext e) := by + rintro c sl rest rfl e he + rcases hex with ⟨-, hnil⟩ | ⟨-, -, a', rest', ⟨-, habs⟩, hm⟩ + · simp at hnil + simp only [List.map_cons, List.cons.injEq] at hm + obtain ⟨rfl, -⟩ := hm + subst he + generalize capAtA mem x0 = cc at hN habs + cases cc with + | Frame p sz r dev mp ept asid va => + cases ept + · simp [CapEqA, absCapA, absFrameE] at habs + · exact hN + | Ept _ _ _ _ _ => exact hN + | HighVSpace _ _ _ => exact hN + | PageTable p m asid va => cases m <;> simp [CapEqA, absCapA] at habs + | _ => simp [CapEqA, absCapA] at habs + +end sel + +/-! ## The top -/ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} +variable {H : Heap} {OS U Ac Pp Tb : Set U64} {Lv : U64 → Nat} {ia ie : Nat} + +/-- FL4's `decode_page_table` at a label other than `ARMPageTableMap` / `ARMPageTableUnmap`. -/ +theorem decode_pt_otherA {ar : machine.Arch A} (lbl length slot buf : U64) (ex : Aeneas.Std.Array obj.Slot 3#usize) + (ptr asid va : U64) (mp : Bool) (h48 : lbl ≠ fl4_abi.label.ARMPageTableUnmap) + (h47 : lbl ≠ fl4_abi.label.ARMPageTableMap) : + vspace.decode_page_table st ar s a lbl length slot (.PageTable ptr mp asid va) buf ex = + ok (.Err (.Sys .IllegalOperation), s, a) := by + have hct : sched.cur_thread st s = ok (L.curW s) := cur_thread_ok s + have h47' : (lbl != fl4_abi.label.ARMPageTableMap) = true := bne_iff_ne.2 h47 + unfold vspace.decode_page_table + simp only [hct, bind_ok, h48, h47', ↓reduceIte, error.illegal] + +set_option maxHeartbeats 4000000 in +/-- **The page-table cap-kind top** (module docstring; ruling 01:37Z item 1): `decode_invocation` on a `Cap::PageTable`, +every label, against `fl4ArchDecodeInvocation` at FL4's label, with `VsTopPostA`. -/ +theorem decInv_pt_jA {ar : machine.Arch A} (K : ArchKeepsA ar AR) + (E : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) (RS : RsSiteA (L := L) Z H OS Ac Pp Tb Lv s) + (hTO : ∀ q ∈ Z.S1, TabOffA (L := L) Z m s q) (hEO : ExtTabOffA (L := L) Z m s) + (hK : Aligned (L.ksb s)) (hT : OffAsE Z.Pp Z.S1 Z.X Z.Y Z.T2 (L.ksb s)) (hIpa : S2IpaE ar a) + (hPC : PtCapInA Z (L.mem s)) (hFI : FinInvA Z.th (L.mem s) σ Z.G) + (hTr : TreeA st OS Z.G.chains Z.G.par s) (hnul : ∀ y ∈ OS, ¬ IsSlotA Z.th y → capA st s y = .Null) + (cte : Fl4SpecA64.CslotPtr) (hab : AbsSlotA Z.th cte) (hos : cslotAddrA cte ∈ OS) + (ptr asid va : U64) (mp : Bool) (hcap : capAtA (L.mem s) (cslotAddrA cte) = .PageTable ptr mp asid va) + (lbl length buf : U64) (block call cd fp calling : Bool) (ex : Aeneas.Std.Array obj.Slot 3#usize) + (args : List U64) (hlen : length.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s (L.curW s) buf i = ok x) + (x0 : U64) (hx0 : Aeneas.Std.Array.index_usize ex 0#usize = ok x0) + (xSlot : Fl4SpecA64.CapRef) (exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) + (hex : ExtraRelA Z.th (L.mem s) x0 (exs.map Prod.fst)) (hNX : NoExtXA (capAtA (L.mem s) x0)) + (hMap : lbl = fl4_abi.label.ARMPageTableMap → + UnmPtA Z.S1 Z.Y OS H.tcbs (L.mem s) σ ptr ∧ ptr ∉ S2PgE Z.X Z.T2 ∧ ptr ∉ Z.Y) : + syscall.decode_invocation st ar s a lbl length (cslotAddrA cte) (.PageTable ptr mp asid va) block call cd fp buf + ex ⦃ res => VsTopPostA (L := L) AR Z OS m σ ((Fl4SpecA64.fl4ArchDecodeInvocation (Z := Unit) calling + (Fl4SpecA64.fl4LabelOf lbl.bv) (args.map UScalar.bv) xSlot cte + (.pageTableCap (specPtr ptr) .normalPtT (mapDataA mp asid va)) exs).run σ) s a res ⦄ := by + rw [decInv_ptA] + by_cases h48 : lbl = fl4_abi.label.ARMPageTableUnmap + · subst h48 + have hlab : Fl4SpecA64.fl4LabelOf fl4_abi.label.ARMPageTableUnmap.bv = Fl4SpecA64.archL .armPageTableUnmap := by + rw [fl4LabelOf_bvA, show fl4_abi.label.ARMPageTableUnmap.val = 48 by simp [fl4_abi.label.ARMPageTableUnmap]]; rfl + rw [hlab] + obtain ⟨b, hf, -⟩ := spec_existsA (Fl4ProofsA64.P0.cdt.is_final_capability.p0 st s (cslotAddrA cte) L.total) + cases b + · obtain ⟨hD, hdec, hrel, -⟩ := pt_unmap_nfJA (ar := ar) E hFI length buf calling ex (args.map UScalar.bv) xSlot + exs cte hab ptr asid va mp hcap hf + rw [hdec] + exact (WP.spec_ok _).2 ⟨rfl, rfl, .revokeFirst, hD, Or.inl hrel⟩ + cases mp + · rw [show mapDataA false asid va = none from rfl] + obtain ⟨hD, x, -, hset, hrest⟩ := pt_unmapU_decJA (ar := ar) E RS length buf calling ex (args.map UScalar.bv) + xSlot exs cte hab ptr asid va hcap hFI hf + obtain ⟨s3, σ3, hdec, hp, J3, T3, C3⟩ := hrest hTr hnul + rw [hdec] + exact (WP.spec_ok _).2 ⟨_, okD_memA hD, _, hset, σ3, [], mem_archNRA hp, Z, rfl, rfl, J3, T3, C3⟩ + rw [show mapDataA true asid va = some (BitVec.ofNat 16 asid.val, va.bv) from rfl] + have hS1 : ∀ k, Fl4SpecA64.asidRootKind σ (BitVec.ofNat 16 asid.val) = k → (k = none ∨ k = some .stage1) → + vspace.decode_page_table st ar s a fl4_abi.label.ARMPageTableUnmap length (cslotAddrA cte) + (.PageTable ptr true asid va) buf ex ⦃ res => VsTopPostA (L := L) AR Z OS m σ + ((Fl4SpecA64.fl4ArchDecodeInvocation (Z := Unit) calling (Fl4SpecA64.archL .armPageTableUnmap) + (args.map UScalar.bv) xSlot cte (.pageTableCap (specPtr ptr) .normalPtT + (some (BitVec.ofNat 16 asid.val, va.bv))) exs).run σ) s a res ⦄ := by + rintro k hk hk' + obtain ⟨hD, x, -, hset, hrest⟩ := pt_unmapS1_decJA K E RS hTO hEO hK hT length buf calling ex + (args.map UScalar.bv) xSlot exs cte hab ptr asid va hPC hcap hFI hf (by subst hk; exact hk') + obtain ⟨s3, σ3, fa, hdec, hp, hJ, T3, C3⟩ := hrest hTr hnul + rw [hdec] + exact vsTop_ofFaA hD hset (mem_archNRA hp) hJ T3 C3 + rcases hk : Fl4SpecA64.asidRootKind σ (BitVec.ofNat 16 asid.val) with _ | k + · exact hS1 _ hk (Or.inl rfl) + by_cases hk1 : k = .stage1 + · subst hk1; exact hS1 _ hk (Or.inr rfl) + obtain ⟨hD, x, -, hset, hrest⟩ := pt_unmap_decJA K E RS hTO hEO hK hT hIpa length buf calling ex + (args.map UScalar.bv) xSlot exs cte hab ptr asid va hPC hcap hFI hf k hk hk1 + obtain ⟨s3, σ3, fa, hdec, hp, hJ, T3, C3⟩ := hrest hTr hnul + rw [hdec] + exact vsTop_ofFaA hD hset (mem_archExtA hp) hJ T3 C3 + by_cases h47 : lbl = fl4_abi.label.ARMPageTableMap + · subst h47 + obtain ⟨UP, hS2, hY⟩ := hMap rfl + have hlab : Fl4SpecA64.fl4LabelOf fl4_abi.label.ARMPageTableMap.bv = Fl4SpecA64.archL .armPageTableMap := by + rw [fl4LabelOf_bvA, show fl4_abi.label.ARMPageTableMap.val = 47 by simp [fl4_abi.label.ARMPageTableMap]]; rfl + rw [hlab, dec_ptMap_specA calling _ xSlot cte exs _ _ (hNX.spec hex)] + exact WP.spec_mono (pt_map_jA ar E RS cte hab hos hcap UP _ invType_ptMapA length buf args hlen hargs x0 hx0 exs + hex hNX.s1 hNX.hi hTr hnul hTO hS2 hY) fun _ h => vsTop_ofPtMapA h + rw [decode_pt_otherA lbl length _ buf ex ptr asid va mp h48 h47] + have hv48 : lbl.val ≠ 48 := fun e => h48 (UScalar.eq_of_val_eq (by rw [e]; simp [fl4_abi.label.ARMPageTableUnmap])) + have hv47 : lbl.val ≠ 47 := fun e => h47 (UScalar.eq_of_val_eq (by rw [e]; simp [fl4_abi.label.ARMPageTableMap])) + rw [dec_ptOtherA calling _ xSlot cte exs _ + (by rw [fl4LabelOf_bvA]; exact labNA_neA (fun n hn => (labNA_tabA n hn).1) (by decide) hv47) + (by rw [fl4LabelOf_bvA]; exact labNA_neA (fun n hn => (labNA_tabA n hn).2.1) (by decide) hv48)] + exact (WP.spec_ok _).2 ⟨rfl, rfl, .illegalOperation, rfl, Or.inl trivial⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/VsTopRootA.lean b/proofs/Fl4ProofsA64/P3/VsTopRootA.lean new file mode 100644 index 000000000..3bd0bd56f --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/VsTopRootA.lean @@ -0,0 +1,215 @@ +/- +P3 (aarch64, A64-P3 S3 gen 32, ruling 02:01Z items 1 and 3): **the VSpace-root, ASID-pool and ASID-control cap-kind +tops, at the labels that agree with the spec as written** — FL4's `syscall::decode_invocation` on a `Cap::Pml4`, a +`Cap::AsidPool` and `Cap::AsidControl` against the spec's `fl4ArchDecodeInvocation` at FL4's label, with +`P3/VsTopPtA`'s uniform outcome `VsTopPostA`. The x86 counterparts are `P3/VsDecTop`'s per-cap-kind tops. + +* **The labels** (**`labNA_rootA`**: FL4's VSpace cache labels are 42..45, `ARMASIDControlMakePool` is 58, + `ARMASIDPoolAssign` is 59; **`vsFlush_ofNA`**). +* **The spec's decoder**: **`dec_vsOtherA`** (a VSpace root cap at a label that is no VSpace cache label), + **`dec_poolOtherA`** (an ASID pool cap at a label other than `ARMASIDPoolAssign`, whatever the extra caps), + **`dec_ctlOtherA`** (the ASID control cap at a label other than `ARMASIDControlMakePool`): `IllegalOperation`, state + unchanged. +* **FL4's decoder**: **`decode_vs_otherA`**, **`decode_pool_otherA`**, **`decode_ctl_otherA`** (the same labels: + `IllegalOperation`, nothing written). +* **The tops** **`decInv_asidPool_other_jA`**, **`decInv_asidControl_jA`**: the illegal-label fallthrough, under the label + selector `hLbl`. (S3 gen 33: `decInv_vspace_jA` moved to `P3/VsFlushA`, total; `dec_vsOtherA` follows the + dispatcher's new VSpace cache arm, `Fl4SpecA64/Ext/Invocation`.) + +**Case selectors (not named premises).** `hLbl` excludes, for now, the labels whose arm does not meet `VsTopPostA` +against the spec as written (gen-32 findings, handoff `# A64-P3 S3 gen 32`): +* `decInv_vspace_jA`: the four VSpace cache labels 42..45 (`decode_vspace_flush`). FL4 implements seL4's EL1 + (`#ifndef CONFIG_ARM_HYPERVISOR_SUPPORT`) path — write rights required for `ARMVSpaceInvalidate_Data`, no kernel-window + check on `pstart` — while l4v's `decode_vs_inv_flush` (`Fl4SpecA64/Decode.lean` `decodeVsInvFlush`) is the EL2 path + (no rights check, `pstart + (end - start) > paddrTop` → `IllegalOperation`, `paddrTop = 2^39 - 2^30`). +* `decInv_asidPool_other_jA`: the illegal-label case used by the total `decInv_asidPool_jA` in `P3/VsAssignA`. +* `decInv_asidControl_jA`: `ARMASIDControlMakePool` (the same `select_ext` over the free ASID-table set, plus the + retype). + +No named premise. New module; no existing statement changed. +-/ +import Fl4ProofsA64.P3.VsTopFrA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Classical + +noncomputable section + +/-! ## The labels -/ + +section lbl +open Fl4SpecA64 + +set_option maxRecDepth 8000 in +/-- FL4's VSpace cache labels are 42..45; `ARMASIDControlMakePool` is 58, `ARMASIDPoolAssign` 59. -/ +theorem labNA_rootA : ∀ n, n < 74 → + (isVSpaceFlushLabel (specInvA (labNA n)) = true → 42 ≤ n ∧ n ≤ 45) ∧ + (labNA n = archL .armASIDControlMakePool → n = 58) ∧ (labNA n = archL .armASIDPoolAssign → n = 59) := by + decide + +theorem vsFlush_ofNA {n : Nat} (h : n < 42 ∨ 45 < n) : + isVSpaceFlushLabel (invocationType (specLabelWord (labNA n))) = false := by + rw [invType_specA] + cases hb : isVSpaceFlushLabel (specInvA (labNA n)) + · rfl + exfalso + by_cases hn : n < 74 + · have := (labNA_rootA n hn).1 hb; omega + · rw [labNA_geA (by omega)] at hb; exact absurd hb (by decide) + +end lbl + +/-! ## The spec's decoder -/ + +section dec +open Fl4SpecA64 + +variable (calling : Bool) (args : List Data) (xSlot : CapRef) (cte : CslotPtr) (exs : List (Cap × CslotPtr)) + {σ : State Unit} + +/-- **A VSpace root cap at a label that is no VSpace cache label: `IllegalOperation`** (module docstring). -/ +theorem dec_vsOtherA (l : Fl4Label) (hf : isVSpaceFlushLabel (invocationType (specLabelWord l)) = false) + (p : ObjRef) (mp : Option (Asid × VSpaceRef)) : + (fl4ArchDecodeInvocation (Z := Unit) calling l args xSlot cte (.pageTableCap p .vsRootPtT mp) exs).run σ = + ({(.error .illegalOperation, σ)}, False) := by + unfold fl4ArchDecodeInvocation + dsimp only + rw [hf, if_neg Bool.false_ne_true] + unfold Fl4Spec.NonDetE.liftME + refine run_bind_errorD ?_ _ + unfold archDecodeInvocation + dsimp only + unfold decodeVspaceInvocation + try dsimp only + rw [hf] + rfl + +/-- **An ASID pool cap at a label other than `ARMASIDPoolAssign`: `IllegalOperation`** (module docstring). -/ +theorem dec_poolOtherA (l : Fl4Label) (h : l ≠ archL .armASIDPoolAssign) (p : ObjRef) (b : Asid) : + (fl4ArchDecodeInvocation (Z := Unit) calling l args xSlot cte (.asidPoolCap p b) exs).run σ = + ({(.error .illegalOperation, σ)}, False) := by + have hsp : (Fl4Spec.NonDetE.liftME Fl4ArchInvocation.spec (archDecodeInvocation (Z := Unit) (specLabelWord l) + args xSlot cte (.asidPoolCap p b) exs)).run σ = ({(.error .illegalOperation, σ)}, False) := by + unfold Fl4Spec.NonDetE.liftME + refine run_bind_errorD ?_ _ + unfold archDecodeInvocation + dsimp only + unfold decodeAsidPoolInvocation + try dsimp only + rw [if_neg (fun h' => h (invType_archA h'))] + rfl + unfold fl4ArchDecodeInvocation + dsimp only + split + · rw [if_neg h]; exact hsp + · exact hsp + +/-- **The ASID control cap at a label other than `ARMASIDControlMakePool`: `IllegalOperation`** (module +docstring). -/ +theorem dec_ctlOtherA (l : Fl4Label) (h : l ≠ archL .armASIDControlMakePool) : + (fl4ArchDecodeInvocation (Z := Unit) calling l args xSlot cte .asidControlCap exs).run σ = + ({(.error .illegalOperation, σ)}, False) := by + unfold fl4ArchDecodeInvocation + dsimp only + unfold Fl4Spec.NonDetE.liftME + refine run_bind_errorD ?_ _ + unfold archDecodeInvocation + dsimp only + unfold decodeAsidControlInvocation + try dsimp only + rw [if_neg (fun h' => h (invType_archA h'))] + rfl + +end dec + +/-! ## FL4's decoder -/ + +section fl4 +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {s : S} {a : A} + +/-- FL4's `decode_invocation` on a `Cap::Pml4` at a label outside 42..45: `IllegalOperation`, nothing written. -/ +theorem decode_vs_otherA (ar : machine.Arch A) (lbl length : U64) (slot : obj.Slot) (ptr asid : U64) (mp : Bool) + (block call cd fp : Bool) (buf : U64) (ex : Aeneas.Std.Array obj.Slot 3#usize) + (h : lbl.val < 42 ∨ 45 < lbl.val) : + syscall.decode_invocation st ar s a lbl length slot (.Pml4 ptr mp asid) block call cd fp buf ex = + ok (.Err (.Sys .IllegalOperation), s, a) := by + have h42 : lbl ≠ fl4_abi.label.ARMVSpaceClean_Data := fun e => by + subst e; simp [fl4_abi.label.ARMVSpaceClean_Data] at h + have h43 : lbl ≠ fl4_abi.label.ARMVSpaceInvalidate_Data := fun e => by + subst e; simp [fl4_abi.label.ARMVSpaceInvalidate_Data] at h + have h44 : lbl ≠ fl4_abi.label.ARMVSpaceCleanInvalidate_Data := fun e => by + subst e; simp [fl4_abi.label.ARMVSpaceCleanInvalidate_Data] at h + have h45 : lbl ≠ fl4_abi.label.ARMVSpaceUnify_Instruction := fun e => by + subst e; simp [fl4_abi.label.ARMVSpaceUnify_Instruction] at h + unfold syscall.decode_invocation vspace.decode + simp only [cur_thread_ok, bind_ok, h42, h43, h44, h45, ↓reduceIte, error.illegal] + +/-- FL4's `decode_invocation` on a `Cap::AsidPool` at a label other than `ARMASIDPoolAssign`: `IllegalOperation`. -/ +theorem decode_pool_otherA (ar : machine.Arch A) (lbl length : U64) (slot : obj.Slot) (ptr base : U64) + (block call cd fp : Bool) (buf : U64) (ex : Aeneas.Std.Array obj.Slot 3#usize) + (h : lbl ≠ fl4_abi.label.ARMASIDPoolAssign) : + syscall.decode_invocation st ar s a lbl length slot (.AsidPool ptr base) block call cd fp buf ex = + ok (.Err (.Sys .IllegalOperation), s, a) := by + have h' : (lbl != fl4_abi.label.ARMASIDPoolAssign) = true := bne_iff_ne.2 h + unfold syscall.decode_invocation vspace.decode + simp only [cur_thread_ok, bind_ok, h', ↓reduceIte, error.illegal] + +/-- FL4's `decode_invocation` on `Cap::AsidControl` at a label other than `ARMASIDControlMakePool`: +`IllegalOperation`. -/ +theorem decode_ctl_otherA (ar : machine.Arch A) (lbl length : U64) (slot : obj.Slot) + (block call cd fp : Bool) (buf : U64) (ex : Aeneas.Std.Array obj.Slot 3#usize) + (h : lbl ≠ fl4_abi.label.ARMASIDControlMakePool) : + syscall.decode_invocation st ar s a lbl length slot .AsidControl block call cd fp buf ex = + ok (.Err (.Sys .IllegalOperation), s, a) := by + have h' : (lbl != fl4_abi.label.ARMASIDControlMakePool) = true := bne_iff_ne.2 h + unfold syscall.decode_invocation vspace.decode + simp only [cur_thread_ok, bind_ok, h', ↓reduceIte, error.illegal] + +end fl4 + +/-! ## The tops -/ + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} +variable {AR : A → HwRootA → Prop} {Z : XSetsA} {OS : Set U64} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} + +/-- **The ASID-pool cap-kind top** (module docstring; ruling 02:01Z item 3): `ARMASIDPoolAssign` excluded by +`hLbl`. -/ +theorem decInv_asidPool_other_jA (ar : machine.Arch A) (cte : Fl4SpecA64.CslotPtr) (ptr base : U64) + (lbl length buf : U64) (block call cd fp calling : Bool) (ex : Aeneas.Std.Array obj.Slot 3#usize) + (args : List U64) (xSlot : Fl4SpecA64.CapRef) (exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) + (hLbl : lbl ≠ fl4_abi.label.ARMASIDPoolAssign) : + syscall.decode_invocation st ar s a lbl length (cslotAddrA cte) (.AsidPool ptr base) block call cd fp buf ex + ⦃ res => VsTopPostA (L := L) AR Z OS m σ ((Fl4SpecA64.fl4ArchDecodeInvocation (Z := Unit) calling + (Fl4SpecA64.fl4LabelOf lbl.bv) (args.map UScalar.bv) xSlot cte + (.asidPoolCap (specPtr ptr) (asidOfA base)) exs).run σ) s a res ⦄ := by + have hv : lbl.val ≠ 59 := fun e => + hLbl (UScalar.eq_of_val_eq (by rw [e]; simp [fl4_abi.label.ARMASIDPoolAssign])) + rw [decode_pool_otherA ar lbl length _ ptr base block call cd fp buf ex hLbl, + dec_poolOtherA calling _ xSlot cte exs _ + (by rw [fl4LabelOf_bvA]; exact labNA_neA (fun n hn => (labNA_rootA n hn).2.2) (by decide) hv)] + exact (WP.spec_ok _).2 ⟨rfl, rfl, .illegalOperation, rfl, Or.inl trivial⟩ + +/-- **The ASID-control cap-kind top** (module docstring; ruling 02:01Z item 3): `ARMASIDControlMakePool` excluded by +`hLbl`. -/ +theorem decInv_asidControl_jA (ar : machine.Arch A) (cte : Fl4SpecA64.CslotPtr) + (lbl length buf : U64) (block call cd fp calling : Bool) (ex : Aeneas.Std.Array obj.Slot 3#usize) + (args : List U64) (xSlot : Fl4SpecA64.CapRef) (exs : List (Fl4SpecA64.Cap × Fl4SpecA64.CslotPtr)) + (hLbl : lbl ≠ fl4_abi.label.ARMASIDControlMakePool) : + syscall.decode_invocation st ar s a lbl length (cslotAddrA cte) .AsidControl block call cd fp buf ex + ⦃ res => VsTopPostA (L := L) AR Z OS m σ ((Fl4SpecA64.fl4ArchDecodeInvocation (Z := Unit) calling + (Fl4SpecA64.fl4LabelOf lbl.bv) (args.map UScalar.bv) xSlot cte .asidControlCap exs).run σ) s a res ⦄ := by + have hv : lbl.val ≠ 58 := fun e => + hLbl (UScalar.eq_of_val_eq (by rw [e]; simp [fl4_abi.label.ARMASIDControlMakePool])) + rw [decode_ctl_otherA ar lbl length _ block call cd fp buf ex hLbl, + dec_ctlOtherA calling _ xSlot cte exs _ + (by rw [fl4LabelOf_bvA]; exact labNA_neA (fun n hn => (labNA_rootA n hn).2.1) (by decide) hv)] + exact (WP.spec_ok _).2 ⟨rfl, rfl, .illegalOperation, rfl, Or.inl trivial⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/VsUnmapA.lean b/proofs/Fl4ProofsA64/P3/VsUnmapA.lean new file mode 100644 index 000000000..73cdf8c02 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/VsUnmapA.lean @@ -0,0 +1,672 @@ +/- +P3 (aarch64, A64-P3 VS gen 4): **Stage-1 `unmap_page` and `unmap_page_table`** — FL4's `vspace::unmap_page` +(`vspace.rs` 449–468) against l4v's `unmap_page` (`ArchVSpace_A`, ported `unmapPage` in `Fl4SpecA64/AARCH64/VSpace`), +and FL4's `vspace::unmap_page_table` (`vspace.rs` 474–517) against l4v's `unmap_page_table` (`unmapPageTable`), under +`VsRelA`, on the **Stage-1 / unmapped-ASID branch** (Architect ruling 14:37Z): the ASID names no root, or a Stage-1 +root (`hS1`: l4v's `vspace_for_asid` is a `PageTable`; FL4's tag `ROOT_S1`). FL4's `ROOT_S2` / `ROOT_HIGH` branches +(Stage-2 `ept::unmap_*`, the `ArmHighVSpace` window walk) are deferred to the shared `P3/ExtVsA` after the merge. +The twins of x86's `unmap_page_rel` (`P3/VsFrameUnmapRel`) and `unmap_page_table_rel` (`P3/VsPtUnmapRel`). + +* **l4v's monads at one state**: `NdOkA Q m` (no failure, an outcome, every outcome `Q`-related to the start) closed + under `pure` / `gets` / `modify` / `selectF` / bind, `liftE`, the exception bind and `catch`; the machine operations + (`machine_op_lift`, `ignore_failure`: never fail, always an outcome) under `do_machine_op` change only the machine + state (`MsA`, `doMachineOp_okA`), and so do l4v's TLB invalidations (`invTlbVa_okA`, `invTlb_okA`): the cache clean + and TLB flush that follow the store are invisible to `VsRelA`. `catch_stepA` / `catch_bind_errA` run a `catch`ed + `do` block one deterministic step at a time. +* **FL4's words**: `psA` (`page_shift`), `isPageA` (`is_page` at depth `k`: `e & 3 = 3` at the last level, `= 1` + above; `isPageA_iff`: valid, bit 1 iff the last level), `page_base_shA` (`page_base` is `pageBaseA`), + `paddr_iffA` (FL4's `paddr(pptr)` against l4v's `ptrFromPAddr`, through `specPtr`), **`pageMatchA`** (l4v's + check `is_PagePTE pte ∧ pptr_from_pte pte = pptr` is FL4's `is_page e ∧ page_base e = paddr(pptr)` for a related + descriptor). +* **`UnmapRelA`** (the post of both): FL4 returns its inputs and l4v's run is the identity (`{((), σ)}`, no failure), + or FL4 writes `0` to one word `slot` of a Stage-1 table of `Tb` and then runs its flush `fl` (`invalidate_page` / + `invalidate_asid`, left to the machine-state relation), and l4v's run never fails, has an outcome, and every + outcome is `VsRelA`-related to the written memory (`SpecOkA`). +* **`unmap_pageA`**: `unmap_root` (`find_asid_raw`, `find_asid_rawA`) ↔ `find_vspace_for_asid`; `lookup_pt_slot` + (`lookup_pt_slotA`) ↔ `pt_lookup_slot`; `lu.shift = page_shift size` ↔ `pt_bits_left level = pageBitsForSize`; + the descriptor check (`pageMatchA`); the zero write ↔ `store_pte … InvalidPTE` (`write_clearPteA`); the clean and + `invalidate_tlb_by_asid_va` machine-only. +* **`unmap_page_tableA`**: `find_asid_raw` with tag `ROOT_S1` ↔ `find_vspace_for_asid`; FL4's walk from the root + (`pt_lookup_from_levelA`) ↔ `pt_lookup_from_level max_pt_level`; the zero write ↔ `store_pte (level_type level) + … InvalidPTE`; the clean and `invalidate_tlb_by_asid` machine-only. + +**S3 gen 18.** `SpecOkA` takes the start state and adds `KeepTbA Tb σ x.2` (l4v's outcome changes no object +outside the Stage-1 tables `Tb`; from `write_storePteA` / `write_clearPteA`'s new conclusion), which +`P3/ExtVsRelA`'s `UnmapRelA.extE` uses to carry `ExtVsRelA` across both unmaps. `findVs_runA` takes +`rootKindAt σ r = .stage1` (the spec's `find_vspace_for_asid` hook hides FL4's roots; at both call sites from +`hS1`). Statements of `unmap_pageA` / `unmap_page_tableA` unchanged. + +Lemma hypotheses (not premises): `hK` (the kernel-state base is aligned), `hT` (no table word is an ASID-table +word: the ASID-table / `kb` boundary of gen 4's ruling, A64 P1's `HeapOK` at integration), `hX` (`Stage2CoreA.obj`, +as `find_asid_findA`), `hS1` (the branch restriction). No named premise; no `Fl4SpecA64` edit. +-/ +import Fl4ProofsA64.P3.VsLeafA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 + +noncomputable section + +/-! ## l4v's monads: runs that cannot fail -/ + +section nd +open Fl4Spec +variable {σ ε α β : Type} + +/-- **A run with no failure and an outcome, every outcome `Q`-related to its start** (module docstring). -/ +def NdOkA (Q : σ → σ → Prop) (m : NonDet σ α) : Prop := + ∀ s, ¬ (m s).2 ∧ (∃ x, x ∈ (m s).1) ∧ ∀ x ∈ (m s).1, Q s x.2 + +theorem ndBindA (m : NonDet σ α) (f : α → NonDet σ β) (s : σ) : + NonDet.bind m f s = ({r | ∃ x ∈ (m s).1, r ∈ (f x.1 x.2).1}, (m s).2 ∨ ∃ x ∈ (m s).1, (f x.1 x.2).2) := rfl + +theorem NdOkA.pure {Q : σ → σ → Prop} (hr : ∀ s, Q s s) (a : α) : NdOkA Q (pure a : NonDet σ α) := fun s => + ⟨fun h => h, ⟨(a, s), Set.mem_singleton _⟩, fun x hx => by rw [Set.mem_singleton_iff.1 hx]; exact hr s⟩ + +theorem NdOkA.bind {Q : σ → σ → Prop} (ht : ∀ a b c, Q a b → Q b c → Q a c) {m : NonDet σ α} + {f : α → NonDet σ β} (hm : NdOkA Q m) (hf : ∀ a, NdOkA Q (f a)) : NdOkA Q (m >>= f) := by + intro s + obtain ⟨h1, ⟨x, hx⟩, h3⟩ := hm s + obtain ⟨-, ⟨y, hy⟩, -⟩ := hf x.1 x.2 + show ¬ (NonDet.bind m f s).2 ∧ (∃ r, r ∈ (NonDet.bind m f s).1) ∧ ∀ r ∈ (NonDet.bind m f s).1, Q s r.2 + rw [ndBindA] + refine ⟨?_, ⟨y, x, hx, hy⟩, ?_⟩ + · rintro (h | ⟨z, -, h⟩) + · exact h1 h + · exact (hf z.1 z.2).1 h + · rintro r ⟨z, hz, hr⟩ + exact ht _ _ _ (h3 z hz) ((hf z.1 z.2).2.2 r hr) + +theorem NdOkA.gets {Q : σ → σ → Prop} (hr : ∀ s, Q s s) (g : σ → α) : NdOkA Q (NonDet.gets g) := fun s => + ⟨fun h => h, ⟨(g s, s), Set.mem_singleton _⟩, fun x hx => by rw [Set.mem_singleton_iff.1 hx]; exact hr s⟩ + +theorem NdOkA.modify {Q : σ → σ → Prop} {g : σ → σ} (hg : ∀ s, Q s (g s)) : NdOkA Q (NonDet.modify g) := fun s => + ⟨fun h => h, ⟨((), g s), Set.mem_singleton _⟩, fun x hx => by rw [Set.mem_singleton_iff.1 hx]; exact hg s⟩ + +theorem NdOkA.selectF {Q : σ → σ → Prop} (hr : ∀ s, Q s s) {S : Set α × Prop} (h1 : ¬ S.2) (h2 : ∃ a, a ∈ S.1) : + NdOkA Q (NonDet.selectF S : NonDet σ α) := fun s => by + obtain ⟨a, ha⟩ := h2 + refine ⟨h1, ⟨(a, s), ha, rfl⟩, ?_⟩ + rintro x ⟨-, hx⟩ + rw [hx]; exact hr s + +theorem NdOkA.liftE {Q : σ → σ → Prop} (hr : ∀ s, Q s s) (ht : ∀ a b c, Q a b → Q b c → Q a c) {m : NonDet σ α} + (hm : NdOkA Q m) : NdOkA Q (NonDetE.liftE m : NonDetE σ ε α).run := by + unfold NonDetE.liftE + rw [ExceptT.run_lift] + show NdOkA Q (m >>= fun a => Pure.pure (Except.ok a)) + exact NdOkA.bind ht hm fun a => NdOkA.pure hr _ + +theorem NdOkA.runBind {Q : σ → σ → Prop} (hr : ∀ s, Q s s) (ht : ∀ a b c, Q a b → Q b c → Q a c) + {x : NonDetE σ ε α} {f : α → NonDetE σ ε β} (hx : NdOkA Q x.run) (hf : ∀ a, NdOkA Q (f a).run) : + NdOkA Q (x >>= f).run := by + rw [ExceptT.run_bind] + refine NdOkA.bind ht hx fun r => ?_ + cases r with + | error e => exact NdOkA.pure hr _ + | ok a => exact hf a + +theorem NdOkA.catch {Q : σ → σ → Prop} (hr : ∀ s, Q s s) (ht : ∀ a b c, Q a b → Q b c → Q a c) + {m : NonDetE σ ε α} {h : ε → NonDet σ α} (hm : NdOkA Q m.run) (hh : ∀ e, NdOkA Q (h e)) : + NdOkA Q (NonDetE.catch m h) := by + unfold NonDetE.catch + refine NdOkA.bind ht hm fun r => ?_ + cases r with + | error e => exact hh e + | ok a => exact NdOkA.pure hr a + +/-- A lifted deterministic step. -/ +theorem liftE_runA {m : NonDet σ α} {s s' : σ} {a : α} (h : m s = ({(a, s')}, False)) : + (NonDetE.liftE m : NonDetE σ ε α).run s = ({(.ok a, s')}, False) := by + unfold NonDetE.liftE + rw [ExceptT.run_lift] + show (m >>= _) s = _ + rw [lfA_bind_one h] + rfl + +theorem runA_bind_ok {m : NonDetE σ ε α} {s s' : σ} {a : α} (h : m.run s = ({(.ok a, s')}, False)) + (f : α → NonDetE σ ε β) : (m >>= f).run s = (f a).run s' := by + rw [ExceptT.run_bind, lfA_bind_one h] + +/-- **One deterministic step of a `catch`ed block** (module docstring). -/ +theorem catch_stepA {m : NonDetE σ ε α} {f : α → NonDetE σ ε β} {h : ε → NonDet σ β} {s s' : σ} {a : α} + (hm : m.run s = ({(.ok a, s')}, False)) : NonDetE.catch (m >>= f) h s = NonDetE.catch (f a) h s' := by + unfold NonDetE.catch + rw [ndBindA, ndBindA, runA_bind_ok hm] + +/-- **A `catch`ed block whose first step throws** runs the handler. -/ +theorem catch_bind_errA {m : NonDetE σ ε α} {f : α → NonDetE σ ε β} {h : ε → NonDet σ β} {s : σ} {e : ε} + (hm : m.run s = ({(.error e, s)}, False)) : NonDetE.catch (m >>= f) h s = h e s := by + unfold NonDetE.catch + show ((m >>= f).run >>= _) s = _ + rw [lfA_bind_one (lfA_run_bind_err hm f)] + +theorem unlessE_posA {P : Prop} [Decidable P] (hP : P) (x : NonDetE σ ε Unit) (s : σ) : + (NonDetE.unlessE P x).run s = ({(.ok (), s)}, False) := by + unfold NonDetE.unlessE; rw [if_pos hP]; rfl + +theorem unlessE_negA {P : Prop} [Decidable P] (hP : ¬ P) (e : ε) (s : σ) : + (NonDetE.unlessE P (NonDetE.throwError e : NonDetE σ ε Unit)).run s = ({(.error e, s)}, False) := by + unfold NonDetE.unlessE; rw [if_neg hP]; rfl + +theorem getsThe_runA {f : σ → Option α} {s : σ} {a : α} (h : f s = some a) : + NonDet.getsThe f s = ({(a, s)}, False) := by + show NonDet.assertOpt (f s) s = _ + rw [h]; rfl + +end nd + +/-! ## l4v's machine operations -/ + +/-- **Outcomes that change only the machine state** (module docstring). -/ +def MsA (σ σ' : Fl4SpecA64.State Unit) : Prop := ∃ ms, σ' = { σ with machineState := ms } + +theorem msA_refl (σ : Fl4SpecA64.State Unit) : MsA σ σ := ⟨σ.machineState, rfl⟩ + +theorem msA_trans : ∀ a b c : Fl4SpecA64.State Unit, MsA a b → MsA b c → MsA a c := by + rintro a b c ⟨m1, rfl⟩ ⟨m2, rfl⟩ + exact ⟨m2, rfl⟩ + +theorem triv_trans {σ : Type} : ∀ a b c : σ, (fun _ _ : σ => True) a b → (fun _ _ : σ => True) b c → + (fun _ _ : σ => True) a c := fun _ _ _ _ _ => trivial + +theorem ignoreFailure_okA {σ : Type} (f : Fl4Spec.NonDet σ Unit) : + NdOkA (fun _ _ => True) (Fl4SpecA64.ignoreFailure f) := by + intro s + unfold Fl4SpecA64.ignoreFailure + by_cases h : (f s).1 = ∅ + · rw [if_pos h] + exact ⟨fun h => h, ⟨((), s), Set.mem_singleton _⟩, fun _ _ => trivial⟩ + · rw [if_neg h] + obtain ⟨x, hx⟩ := Set.nonempty_iff_ne_empty.2 h + exact ⟨fun h => h, ⟨x, hx⟩, fun _ _ => trivial⟩ + +/-- `machine_op_lift` never fails and always has an outcome. -/ +theorem machineOpLift_okA (g : Fl4SpecA64.MachineRestMonad Unit) : + NdOkA (fun _ _ => True) (Fl4SpecA64.machineOpLift g) := by + unfold Fl4SpecA64.machineOpLift Fl4SpecA64.machineRestLift + refine NdOkA.bind triv_trans (NdOkA.gets (fun _ => trivial) _) fun mr => + NdOkA.bind triv_trans (NdOkA.selectF (fun _ => trivial) ((ignoreFailure_okA g) mr).1 + ((ignoreFailure_okA g) mr).2.1) fun x => ?_ + rcases x with ⟨r, mr'⟩ + exact NdOkA.bind triv_trans (NdOkA.modify fun _ => trivial) fun _ => NdOkA.pure (fun _ => trivial) r + +/-- **`do_machine_op` of a total machine operation changes only the machine state** (module docstring). -/ +theorem doMachineOp_okA {α : Type} {mop : Fl4Spec.NonDet Fl4SpecA64.MachineState α} + (h : NdOkA (fun _ _ => True) mop) : NdOkA MsA (Fl4SpecA64.doMachineOp (Z := Unit) mop) := by + unfold Fl4SpecA64.doMachineOp + refine NdOkA.bind msA_trans (NdOkA.gets msA_refl _) fun ms => + NdOkA.bind msA_trans (NdOkA.selectF msA_refl (h ms).1 (h ms).2.1) fun x => ?_ + rcases x with ⟨r, ms'⟩ + exact NdOkA.bind msA_trans (NdOkA.modify fun σ => ⟨ms', rfl⟩) fun _ => NdOkA.pure msA_refl r + +theorem loadVmid_okA (asid : Fl4SpecA64.Asid) : NdOkA MsA (Fl4SpecA64.loadVmid (Z := Unit) asid) := by + unfold Fl4SpecA64.loadVmid + exact NdOkA.bind msA_trans (NdOkA.gets msA_refl _) fun _ => NdOkA.pure msA_refl _ + +/-- **`invalidate_tlb_by_asid_va` changes only the machine state.** -/ +theorem invTlbVa_okA (asid : Fl4SpecA64.Asid) (v : Fl4SpecA64.VSpaceRef) : + NdOkA MsA (Fl4SpecA64.invalidateTlbByAsidVa (Z := Unit) asid v) := by + unfold Fl4SpecA64.invalidateTlbByAsidVa + refine NdOkA.bind msA_trans (loadVmid_okA asid) fun mv => ?_ + cases mv with + | none => exact NdOkA.pure msA_refl _ + | some vmid => exact doMachineOp_okA (machineOpLift_okA _) + +/-- **`invalidate_tlb_by_asid` changes only the machine state.** -/ +theorem invTlb_okA (asid : Fl4SpecA64.Asid) : NdOkA MsA (Fl4SpecA64.invalidateTlbByAsid (Z := Unit) asid) := by + unfold Fl4SpecA64.invalidateTlbByAsid + refine NdOkA.bind msA_trans (loadVmid_okA asid) fun mv => ?_ + cases mv with + | none => exact NdOkA.pure msA_refl _ + | some vmid => exact doMachineOp_okA (machineOpLift_okA _) + +/-- The tail of both unmaps (cache clean, TLB flush), `catch`ed, changes only the machine state. -/ +theorem tail_okA (m₁ m₂ : Fl4Spec.NonDet (Fl4SpecA64.State Unit) Unit) (h₁ : NdOkA MsA m₁) (h₂ : NdOkA MsA m₂) : + NdOkA MsA (Fl4Spec.NonDetE.catch ((Fl4Spec.NonDetE.liftE m₁ : + Fl4Spec.NonDetE (Fl4SpecA64.State Unit) Fl4SpecA64.LookupFailure Unit) >>= + fun _ => Fl4Spec.NonDetE.liftE m₂) fun _ => return ()) := + NdOkA.catch msA_refl msA_trans (NdOkA.runBind msA_refl msA_trans (NdOkA.liftE msA_refl msA_trans h₁) + fun _ => NdOkA.liftE msA_refl msA_trans h₂) fun _ => NdOkA.pure msA_refl _ + +/-! ## l4v's `find_vspace_for_asid` and `get_pte` at one state -/ + +/-- (S3 gen 18: the root is a Stage-1 root, `hx`: the spec's `find_vspace_for_asid` hook hides FL4's roots) -/ +theorem findVs_runA {σ : Fl4SpecA64.State Unit} {a : Fl4SpecA64.Asid} {r : Fl4SpecA64.ObjRef} + (h : Fl4SpecA64.vspaceForAsid a σ = some r) (hx : Fl4SpecA64.rootKindAt σ r = .stage1) : + (Fl4SpecA64.findVspaceForAsid (Z := Unit) a).run σ = ({(.ok r, σ)}, False) := by + unfold Fl4SpecA64.findVspaceForAsid + rw [runA_bind_ok (liftE_runA (show Fl4Spec.NonDet.gets (Fl4SpecA64.vspaceForAsid (Z := Unit) a) σ = + ({(Fl4SpecA64.vspaceForAsid a σ, σ)}, False) from rfl)), h] + refine (runA_bind_ok (liftE_runA (m := Fl4Spec.NonDet.gets _) rfl) _).trans ?_ + unfold Fl4SpecA64.rootKindAt Fl4SpecA64.extObjAt at hx + simp only [Option.filter_some] + split + · rename_i o heq + rw [heq] at hx + cases o <;> simp at hx + · rfl + +theorem findVs_noneA {σ : Fl4SpecA64.State Unit} {a : Fl4SpecA64.Asid} + (h : Fl4SpecA64.vspaceForAsid a σ = none) : + (Fl4SpecA64.findVspaceForAsid (Z := Unit) a).run σ = ({(.error .invalidRoot, σ)}, False) := by + unfold Fl4SpecA64.findVspaceForAsid + rw [runA_bind_ok (liftE_runA (show Fl4Spec.NonDet.gets (Fl4SpecA64.vspaceForAsid (Z := Unit) a) σ = + ({(Fl4SpecA64.vspaceForAsid a σ, σ)}, False) from rfl)), h] + refine (runA_bind_ok (liftE_runA (m := Fl4Spec.NonDet.gets _) rfl) _).trans ?_ + rfl + +theorem getPte_runA {σ : Fl4SpecA64.State Unit} {ptT : Fl4SpecA64.PtType} {q : Fl4SpecA64.ObjRef} + {pte : Fl4SpecA64.PTE} (h : Fl4SpecA64.ptesOf σ ptT q = some pte) : + Fl4SpecA64.getPte (Z := Unit) ptT q σ = ({(pte, σ)}, False) := by + show (Fl4Spec.NonDet.gets (fun s : Fl4SpecA64.State Unit => Fl4SpecA64.ptesOf s ptT) >>= + fun m => Fl4Spec.NonDet.assertOpt (m q)) σ = _ + rw [lfA_bind_one (s' := σ) (a := Fl4SpecA64.ptesOf σ ptT) rfl] + show Fl4Spec.NonDet.assertOpt (Fl4SpecA64.ptesOf σ ptT q) σ = _ + rw [h]; rfl + +/-! ## FL4's words -/ + +/-- FL4's `page_shift` (`FrameSize::bits`). -/ +def psA : capability.FrameSize → U64 + | .Small => 12#u64 + | .Large => 21#u64 + | .Huge => 30#u64 + +theorem page_shift_eqA (sz : capability.FrameSize) : vspace.page_shift sz = ok (psA sz) := by + cases sz <;> rfl + +theorem psA_valA (sz : capability.FrameSize) : (psA sz).val = 12 ∨ (psA sz).val = 21 ∨ (psA sz).val = 30 := by + cases sz + · exact Or.inl rfl + · exact Or.inr (Or.inl rfl) + · exact Or.inr (Or.inr rfl) + +theorem pbfs_eqA (sz : capability.FrameSize) : + Fl4SpecA64.pageBitsForSize (frameSizeOfA sz) = (psA sz).val := by + cases sz <;> simp only [frameSizeOfA, Fl4SpecA64.pageBitsForSize, ptTB9] <;> rfl + +/-- **FL4's `is_page` at depth `k`** (module docstring). -/ +def isPageA (w : U64) (k : Nat) : Bool := + if k = 3 then decide ((w &&& 3#u64) = 3#u64) else decide ((w &&& 3#u64) = 1#u64) + +theorem is_page_eqA (e sh : U64) : vspace.is_page e sh = + ok (if sh = 12#u64 then decide ((e &&& 3#u64) = 3#u64) else decide ((e &&& 3#u64) = 1#u64)) := by + unfold vspace.is_page + rw [eq_of_spec_fd P0.Consts.vspace.D_VALID.spec, eq_of_spec_fd P0.Consts.vspace.D_TABLE_OR_PAGE.spec, + show vspace.PAGE_BITS = 12#u64 by unfold vspace.PAGE_BITS; rfl] + simp only [lift, bind_ok] + have e3 : (1#u64 ||| 2#u64) = 3#u64 := by decide + rw [e3] + by_cases h : sh = 12#u64 + · rw [if_pos h, if_pos h] + · rw [if_neg h, if_neg h] + +theorem is_page_shA {e sh : U64} {k : Nat} (hsh : sh.val = 39 - 9 * k) (hk : k ≤ 3) : + vspace.is_page e sh = ok (isPageA e k) := by + rw [is_page_eqA] + unfold isPageA + have h12 : (12#u64 : U64).val = 12 := rfl + by_cases h3 : k = 3 + · rw [if_pos (UScalar.eq_of_val_eq (by rw [hsh, h12, h3]) : sh = 12#u64), if_pos h3] + · rw [if_neg (fun e => h3 (by have := congrArg UScalar.val e; rw [hsh, h12] at this; omega)), if_neg h3] + +/-- `is_page` is "valid, bit 1 iff the last level". -/ +theorem isPageA_iff (w : U64) (k : Nat) : + isPageA w k = true ↔ dBitA w 0 = true ∧ (dBitA w 1 = true ↔ k = 3) := by + have h3v := and3_valA w + have h3 : (3#u64 : U64).val = 3 := rfl + have h1 : (1#u64 : U64).val = 1 := rfl + rw [dBitA_eq, dBitA_eq] + unfold isPageA + by_cases hk : k = 3 + · rw [if_pos hk] + simp only [decide_eq_true_eq, hk, iff_true, Nat.pow_zero, Nat.pow_one, Nat.div_one] + constructor + · intro h; have := congrArg UScalar.val h; rw [h3v, h3] at this; omega + · rintro ⟨a, b⟩; apply UScalar.eq_of_val_eq; rw [h3v, h3]; omega + · rw [if_neg hk] + simp only [decide_eq_true_eq, hk, iff_false, Nat.pow_zero, Nat.pow_one, Nat.div_one] + constructor + · intro h; have := congrArg UScalar.val h; rw [h3v, h1] at this; omega + · rintro ⟨a, b⟩; apply UScalar.eq_of_val_eq; rw [h3v, h1]; omega + +theorem page_base_eqA (e sh : U64) (hs : sh.val < 64) : + vspace.page_base e sh = ok ⟨(dAddrA e).bv &&& ~~~(BitVec.ofNat 64 (2 ^ sh.val - 1))⟩ := by + unfold vspace.page_base + simp only [lift, bind_ok] + have e63 : (sh &&& 63#u64) = sh := + UScalar.eq_of_val_eq (by rw [and_maskA sh 6 _ rfl]; exact Nat.mod_eq_of_lt (by simp only [Nat.reducePow]; omega)) + rw [e63, CapRt.shl_u64_eq 1#u64 sh hs, bind_ok] + have hp : 2 ^ sh.val < 2 ^ 64 := Nat.pow_lt_pow_right (by decide) hs + have hp1 : 1 ≤ 2 ^ sh.val := Nat.one_le_two_pow + have hx : (⟨(1#u64 : U64).bv <<< sh.val⟩ : U64).val = 2 ^ sh.val := by + show ((1#u64 : U64).bv <<< sh.val).toNat = _ + rw [BitVec.toNat_shiftLeft, Nat.shiftLeft_eq] + show 1 * 2 ^ sh.val % 2 ^ 64 = _ + rw [Nat.one_mul, Nat.mod_eq_of_lt hp] + have hm : core.num.U64.wrapping_sub ⟨(1#u64 : U64).bv <<< sh.val⟩ 1#u64 = + ⟨BitVec.ofNat 64 (2 ^ sh.val - 1)⟩ := by + apply UScalar.eq_of_val_eq + have e1 : (⟨BitVec.ofNat 64 (2 ^ sh.val - 1)⟩ : U64).val = 2 ^ sh.val - 1 := by + show (BitVec.ofNat 64 (2 ^ sh.val - 1)).toNat = _ + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + rw [e1] + generalize (⟨(1#u64 : U64).bv <<< sh.val⟩ : U64) = x at hx ⊢ + simp [core.num.U64.wrapping_sub_val_eq, U64.size_def, U64.numBits, hx] + omega + rw [hm] + unfold vspace.ADDR_MASK dAddrA + rfl + +/-- **`page_base` is `pageBaseA`** at the depth whose shift it is. -/ +theorem page_base_shA {e sh : U64} {k : Nat} (hsh : sh.val = shiftA k) : + vspace.page_base e sh = ok ⟨pageBaseA e k⟩ := by + rw [page_base_eqA e sh (by rw [hsh]; unfold shiftA; omega)] + unfold pageBaseA + rw [hsh] + +/-- **FL4's `paddr(pptr)` against l4v's `ptrFromPAddr`** (module docstring). -/ +theorem paddr_iffA (a : BitVec 64) (pptr : U64) : + (⟨a + fl4PptrBaseA⟩ : U64) = pptr ↔ (⟨a⟩ : U64) = core.num.U64.wrapping_sub pptr layout.PPTR_BASE := by + have hws : (core.num.U64.wrapping_sub pptr layout.PPTR_BASE).val = (pptr.val + 140737488355328) % 2 ^ 64 := by + rw [pptrBase_litA] + simp [core.num.U64.wrapping_sub_val_eq, U64.size_def, U64.numBits] + have ha : (⟨a + fl4PptrBaseA⟩ : U64).val = (a.toNat + 18446603336221196288) % 2 ^ 64 := by + show (a + fl4PptrBaseA).toNat = _ + rw [BitVec.toNat_add]; rfl + have ha' : (⟨a⟩ : U64).val = a.toNat := rfl + have hal : a.toNat < 2 ^ 64 := a.isLt + have hpl : pptr.val < 2 ^ 64 := pptr.bv.isLt + constructor + · intro h + apply UScalar.eq_of_val_eq + rw [ha', hws, ← h, ha] + omega + · intro h + apply UScalar.eq_of_val_eq + have e := congrArg UScalar.val h + rw [ha', hws] at e + rw [ha] + omega + +/-- **l4v's mapping check is FL4's** (module docstring): for a descriptor related at depth `k`, `is_PagePTE pte ∧ +pptr_from_pte pte = pptr` iff FL4's `is_page` holds and `page_base` is `paddr(pptr)`. -/ +theorem pageMatchA {Tb : Set U64} {Lv : U64 → Nat} {k : Nat} {w : U64} {pte : Fl4SpecA64.PTE} + (hpre : PteRelA Tb Lv k w pte) (pptr : U64) : + (pte.isPagePTE = true ∧ Fl4SpecA64.pptrFromPte pte = specPtr pptr) ↔ + (isPageA w k = true ∧ (⟨pageBaseA w k⟩ : U64) = core.num.U64.wrapping_sub pptr layout.PPTR_BASE) := by + rw [isPageA_iff] + cases pte with + | invalidPTE => + have h0 : dBitA w 0 = false := hpre + constructor + · rintro ⟨h, -⟩; exact absurd h Bool.false_ne_true + · rintro ⟨⟨h, -⟩, -⟩; rw [h0] at h; exact absurd h Bool.false_ne_true + | pageTablePTE ppn => + obtain ⟨hk3, -, hb1, -⟩ := hpre + constructor + · rintro ⟨h, -⟩; exact absurd h Bool.false_ne_true + · rintro ⟨⟨-, hi⟩, -⟩; have := hi.1 hb1; omega + | pagePTE pa sm pat prt => + obtain ⟨-, -, hb0, hb1, -, ha, -⟩ := hpre + have hpp : Fl4SpecA64.pptrFromPte (.pagePTE pa sm pat prt) = specPtr ⟨pa + fl4PptrBaseA⟩ := ptrFromPAddr_eq pa + rw [hpp, specPtr_iff, ha, paddr_iffA] + exact ⟨fun h => ⟨⟨hb0, hb1⟩, h.2⟩, fun h => ⟨rfl, h.2⟩⟩ + +/-! ## FL4's root lookup on the Stage-1 branch -/ + +/-- **The Stage-1 branch** (module docstring): when l4v's `vspace_for_asid` names only Stage-1 roots, a raw lookup's +`Ok w` is a tag-0 root of `Tb` at depth 0, l4v's root shifted. -/ +theorem raw_s1A {Pp Tb : Set U64} {Lv : U64 → Nat} {X Y : Set U64} {mem : U64 → U64} {ksb : U64} + {σ : Fl4SpecA64.State Unit} (R : VsRelA Pp Tb Lv X Y mem ksb σ) + (hX : ∀ r ∈ X, ∃ es, σ.kheap (specPtr r) = some (.archObj (.ext (.stage2Root es)))) + {a : Fl4SpecA64.Asid} + (hS1 : ∀ r, Fl4SpecA64.vspaceForAsid a σ = some r → Fl4SpecA64.rootKindAt σ r = .stage1) + {w : U64} (hw : RawRelA Tb Lv X Y σ a (core.result.Result.Ok w)) : + tagA w = 0 ∧ w ∈ Tb ∧ Lv w = 0 ∧ Fl4SpecA64.vspaceForAsid a σ = some (specPtr w) := by + obtain ⟨hs, hw0, hk⟩ := hw + have hk1 := hS1 _ hs + rcases hk with h | ⟨h0, hT, hL⟩ | ⟨-, hx⟩ | ⟨-, hy⟩ + · exact absurd h hw0 + · rw [untagA_of h0] at hT hL hs + exact ⟨h0, hT, hL, hs⟩ + · obtain ⟨es, hes⟩ := hX _ hx + rw [rootKind_s2A hes] at hk1 + cases hk1 + · obtain ⟨l1, hl, -⟩ := R.high _ hy + rw [rootKind_highA hl] at hk1 + cases hk1 + +section +variable {S : Type} {st : machine.Store S} + +theorem unmap_root_s1A {s : S} {asid w : U64} (h : vspace.find_asid_raw st s asid = ok (.Ok w)) + (ht : tagA w = 0) : vspace.unmap_root st s asid = ok (some (w, false)) := by + unfold vspace.unmap_root + rw [h, bind_ok] + simp only [lift, bind_ok, rtag, rs1] + rw [(tag_iffA w).2 ht, if_pos rfl] + +theorem unmap_root_errA {s : S} {asid : U64} {e : error.LookupFault} + (h : vspace.find_asid_raw st s asid = ok (.Err e)) : vspace.unmap_root st s asid = ok none := by + unfold vspace.unmap_root + rw [h, bind_ok] + +end + +/-! ## The refinements -/ + +/-- (S3 gen 18) **Only Stage-1 tables changed**: every object not named by a table of `Tb` is kept (what the +Stage-2 relation `ExtVsRelA` needs across l4v's Stage-1 unmaps, `P3/ExtVsRelA` `UnmapRelA.extE`). -/ +def KeepTbA (Tb : Set U64) (σ σ' : Fl4SpecA64.State Unit) : Prop := + ∀ q, (∀ p ∈ Tb, q ≠ specPtr p) → σ'.kheap q = σ.kheap q + +/-- **l4v's side of an unmap that wrote** (module docstring): no failure, an outcome, every outcome related. -/ +def SpecOkA (Pp Tb : Set U64) (Lv : U64 → Nat) (X Y : Set U64) (mem : U64 → U64) (ksb : U64) + (σ0 : Fl4SpecA64.State Unit) (W : Set (Unit × Fl4SpecA64.State Unit) × Prop) : Prop := + ¬ W.2 ∧ (∃ x, x ∈ W.1) ∧ ∀ x ∈ W.1, VsRelA Pp Tb Lv X Y mem ksb x.2 ∧ KeepTbA Tb σ0 x.2 + +/-- **The post of both unmaps** (module docstring). -/ +def UnmapRelA (Pp Tb : Set U64) (Lv : U64 → Nat) (X Y : Set U64) (σ : Fl4SpecA64.State Unit) {S A : Type} + (st : machine.Store S) [L : LawfulStore st] (s : S) (a : A) (fl : Result A) (r : Result (S × A)) + (W : Set (Unit × Fl4SpecA64.State Unit) × Prop) : Prop := + (r = ok (s, a) ∧ W = ({((), σ)}, False)) ∨ + ∃ slot, (∃ p ∈ Tb, ∃ i : U64, i.val < 512 ∧ slot = peAF p i) ∧ + r = (do let a1 ← fl; ok (L.wr s slot 0#u64, a1)) ∧ + SpecOkA Pp Tb Lv X Y (L.mem (L.wr s slot 0#u64)) (L.ksb (L.wr s slot 0#u64)) σ W ∧ + ∀ x ∈ W.1, x.2.archState.armAsidTable = σ.archState.armAsidTable + +theorem specOk_msA {Pp Tb : Set U64} {Lv : U64 → Nat} {X Y : Set U64} {mem : U64 → U64} {ksb : U64} + {σ0 σ1 : Fl4SpecA64.State Unit} (R1 : VsRelA Pp Tb Lv X Y mem ksb σ1) (hk : KeepTbA Tb σ0 σ1) + {m : Fl4Spec.NonDet (Fl4SpecA64.State Unit) Unit} (h : NdOkA MsA m) : SpecOkA Pp Tb Lv X Y mem ksb σ0 (m σ1) := by + obtain ⟨h1, h2, h3⟩ := h σ1 + refine ⟨h1, h2, fun x hx => ?_⟩ + obtain ⟨ms, e⟩ := h3 x hx + rcases x with ⟨u, σx⟩ + simp only at e + subst e + exact ⟨R1.of_spec rfl fun _ _ => rfl, hk⟩ + +theorem asidKeep_msA {σ0 σ1 : Fl4SpecA64.State Unit} (ht : σ1.archState.armAsidTable = σ0.archState.armAsidTable) + {m : Fl4Spec.NonDet (Fl4SpecA64.State Unit) Unit} (h : NdOkA MsA m) : + ∀ x ∈ (m σ1).1, x.2.archState.armAsidTable = σ0.archState.armAsidTable := by + obtain ⟨-, -, h3⟩ := h σ1 + intro x hx + obtain ⟨ms, e⟩ := h3 x hx + rcases x with ⟨_, σx⟩ + simp only at e + subst e + exact ht + +/-- A store into the table `p ∈ Tb` keeps every object outside `Tb`. -/ +theorem keepTb_ofA {Tb : Set U64} {σ σ1 : Fl4SpecA64.State Unit} {p : U64} (hp : p ∈ Tb) + (h : ∀ q, q ≠ specPtr p → σ1.kheap q = σ.kheap q) : KeepTbA Tb σ σ1 := + fun q hq => h q (hq p hp) + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {Pp Tb : Set U64} {Lv : U64 → Nat} {X Y : Set U64} {σ : Fl4SpecA64.State Unit} + +set_option maxHeartbeats 4000000 in +/-- **`unmap_page` against l4v's `unmap_page`**, Stage-1 branch (module docstring). -/ +theorem unmap_pageA {A : Type} {ar : machine.Arch A} {s : S} {a : A} + (R : VsRelA Pp Tb Lv X Y (L.mem s) (L.ksb s) σ) (hK : Aligned (L.ksb s)) + (hT : ∀ p ∈ Tb, ∀ i : U64, i.val < 512 → ¬ AsW (L.ksb s) (peAF p i)) + (hX : ∀ r ∈ X, ∃ es, σ.kheap (specPtr r) = some (.archObj (.ext (.stage2Root es)))) + (sz : capability.FrameSize) {asid : U64} (h16 : asid.val < 65536) + (hS1 : ∀ r, Fl4SpecA64.vspaceForAsid (BitVec.ofNat 16 asid.val) σ = some r → + Fl4SpecA64.rootKindAt σ r = .stage1) + (vaddr pptr : U64) : + UnmapRelA Pp Tb Lv X Y σ st s a (ar.invalidate_page a asid vaddr) + (vspace.unmap_page st ar s a sz asid vaddr pptr) + (Fl4SpecA64.unmapPage (Z := Unit) (frameSizeOfA sz) (BitVec.ofNat 16 asid.val) vaddr.bv (specPtr pptr) σ) := by + obtain ⟨r, hr, hrel⟩ := spec_ex_fd (find_asid_rawA (st := st) R h16) + unfold vspace.unmap_page Fl4SpecA64.unmapPage + rcases r with w | e + · obtain ⟨ht0, hwT, hw0, hvs⟩ := raw_s1A R hX hS1 hrel + have hreachW := vsReachA_of_vspaceForAsid R hX hwT hvs + rw [unmap_root_s1A hr ht0, bind_ok] + show UnmapRelA _ _ _ _ _ _ _ _ _ _ (Std.bind (vspace.lookup_pt_slot st s w vaddr) _) _ + obtain ⟨lu, hlu, p, hp, l, hl3, hsh, hlv, hsl, -, hL, hreachP⟩ := + spec_ex_fd (lookup_pt_slotA R hwT hw0 hreachW vaddr) + rw [hlu, bind_ok, page_shift_eqA, bind_ok] + rw [catch_stepA (a := specPtr w) (s' := σ) ?_]; rotate_left + · exact findVs_runA hvs (hS1 _ hvs) + try dsimp only + rw [catch_stepA (a := (lvA l, specPtr lu.slot)) (s' := σ) ?_]; rotate_left + · exact liftE_runA (getsThe_runA hL) + try dsimp only + by_cases hs : lu.shift = psA sz + swap + · have hc : ¬ Fl4SpecA64.ptBitsLeft (lvA l) = Fl4SpecA64.pageBitsForSize (frameSizeOfA sz) := by + rw [ptBitsLeft_lvA hl3, pbfs_eqA, ← hsh] + exact fun h => hs (UScalar.eq_of_val_eq h) + rw [catch_bind_errA (unlessE_negA hc _ σ)] + left + refine ⟨?_, rfl⟩ + rw [if_pos (bne_iff_ne.2 hs)] + have hc : Fl4SpecA64.ptBitsLeft (lvA l) = Fl4SpecA64.pageBitsForSize (frameSizeOfA sz) := by + rw [ptBitsLeft_lvA hl3, pbfs_eqA, ← hsh, hs] + rw [catch_stepA (unlessE_posA hc _ σ)] + try dsimp only + rw [if_neg (by rw [hs, bne_self_eq_false]; exact Bool.false_ne_true)] + have hl2 : l ≤ 2 := by + have e := congrArg UScalar.val hs + rw [hsh] at e + rcases psA_valA sz with h | h | h <;> omega + obtain ⟨t, hk, hty, hrel⟩ := R.pt p hp + have hidx := idxA_lt vaddr lu.shift + have hread := ptesOf_atA hk (ptT := Fl4SpecA64.levelType (lvA l)) + (by rw [hty, levelType_lvA hl3]; congr 1; omega) (R.al_mod hp) hidx + have hpre := hrel hreachP _ hidx + rw [← hsl] at hread hpre + generalize Fl4SpecA64.ptApply t (idxA vaddr lu.shift).bv = pte at hpre hread + rw [catch_stepA (liftE_runA (getPte_runA hread))] + try dsimp only + rw [L.read_eq, bind_ok, is_page_shA (k := Lv p) (by omega) (by omega), bind_ok] + have hm := pageMatchA hpre pptr + by_cases hb : isPageA (L.mem s lu.slot) (Lv p) = true + swap + · rw [if_neg hb, catch_bind_errA (unlessE_negA (fun h => hb (hm.1 h).1) _ σ)] + exact Or.inl ⟨rfl, rfl⟩ + rw [if_pos hb, page_base_shA (k := Lv p) (by unfold shiftA; omega), bind_ok, + show layout.paddr pptr = ok (core.num.U64.wrapping_sub pptr layout.PPTR_BASE) from rfl, bind_ok] + by_cases hq : (⟨pageBaseA (L.mem s lu.slot) (Lv p)⟩ : U64) = core.num.U64.wrapping_sub pptr layout.PPTR_BASE + swap + · rw [if_pos (bne_iff_ne.2 hq), catch_bind_errA (unlessE_negA (fun h => hq (hm.1 h).2) _ σ)] + exact Or.inl ⟨rfl, rfl⟩ + rw [if_neg (by rw [hq, bne_self_eq_false]; exact Bool.false_ne_true), + catch_stepA (unlessE_posA (hm.2 ⟨hb, hq⟩) _ σ)] + try dsimp only + obtain ⟨hwr, σ1, hst, R1, hk1⟩ := write_clearPteA R hp hidx (hT p hp _ hidx) hK + rw [← hsl] at hwr hst R1 + have hty' : Fl4SpecA64.levelType (lvA l) = ptTyA (Lv p) := by rw [levelType_lvA hl3]; congr 1; omega + rw [← hty'] at hst + rw [catch_stepA (a := ()) (s' := σ1) ?_]; rotate_left + · exact liftE_runA hst + rw [hwr, bind_ok] + try dsimp only + right + have hstAsid : σ1.archState.armAsidTable = σ.archState.armAsidTable := by + obtain ⟨t0, hkt0, hty0, -⟩ := R.pt p hp + rw [hsl, hty'] at hst + rw [storePte_runA hkt0 hty0 (R.al_mod hp) hidx .invalidPTE] at hst + have e := congrArg Prod.fst hst + simp only [Set.singleton_eq_singleton_iff, Prod.mk.injEq, true_and] at e + subst e; rfl + refine ⟨lu.slot, ⟨p, hp, _, hidx, hsl⟩, ?_, + specOk_msA R1 (keepTb_ofA hp hk1) (tail_okA _ _ (doMachineOp_okA (machineOpLift_okA _)) (invTlbVa_okA _ _)), + asidKeep_msA hstAsid (tail_okA _ _ (doMachineOp_okA (machineOpLift_okA _)) (invTlbVa_okA _ _))⟩ + simp only [Bool.false_eq_true, ↓reduceIte] + · rw [unmap_root_errA hr, bind_ok, catch_bind_errA (findVs_noneA hrel)] + exact Or.inl ⟨rfl, rfl⟩ + +set_option maxHeartbeats 4000000 in +/-- **`unmap_page_table` against l4v's `unmap_page_table`**, Stage-1 branch (module docstring). -/ +theorem unmap_page_tableA {A : Type} {ar : machine.Arch A} {s : S} {a : A} + (R : VsRelA Pp Tb Lv X Y (L.mem s) (L.ksb s) σ) (hK : Aligned (L.ksb s)) + (hT : ∀ p ∈ Tb, ∀ i : U64, i.val < 512 → ¬ AsW (L.ksb s) (peAF p i)) + (hX : ∀ r ∈ X, ∃ es, σ.kheap (specPtr r) = some (.archObj (.ext (.stage2Root es)))) + {asid : U64} (h16 : asid.val < 65536) + (hS1 : ∀ r, Fl4SpecA64.vspaceForAsid (BitVec.ofNat 16 asid.val) σ = some r → + Fl4SpecA64.rootKindAt σ r = .stage1) + (vaddr pt : U64) : + UnmapRelA Pp Tb Lv X Y σ st s a (ar.invalidate_asid a asid) + (vspace.unmap_page_table st ar s a asid vaddr pt) + (Fl4SpecA64.unmapPageTable (Z := Unit) (BitVec.ofNat 16 asid.val) vaddr.bv (specPtr pt) σ) := by + obtain ⟨r, hr, hrel⟩ := spec_ex_fd (find_asid_rawA (st := st) R h16) + unfold vspace.unmap_page_table Fl4SpecA64.unmapPageTable + rw [hr, bind_ok] + rcases r with w | e + · obtain ⟨ht0, hwT, hw0, hvs⟩ := raw_s1A R hX hS1 hrel + have hreachW := vsReachA_of_vspaceForAsid R hX hwT hvs + have hu : w &&& 3#u64 = 0#u64 := (tag_iffA w).2 ht0 + have hn : (~~~ (3#u64 : U64)) = 18446744073709551612#u64 := by decide + have hroot : (w &&& 18446744073709551612#u64) = w := untagA_of ht0 + have h01 : ¬ (0#u64 : U64) = 1#u64 := by decide + have h02 : ¬ (0#u64 : U64) = 2#u64 := by decide + simp only [lift, bind_ok, rtag, rs2, rhigh, hu, hn, hroot, h01, h02, ↓reduceIte] + obtain ⟨fd, hfd, hpost⟩ := spec_ex_fd (pt_lookup_from_levelA R hwT hw0 hreachW vaddr pt) + show UnmapRelA _ _ _ _ _ _ _ _ _ _ + (Std.bind (vspace.unmap_page_table_loop st s vaddr pt w vspace.TOP_LEVEL_SHIFT 0#u64) _) _ + rw [hfd, bind_ok] + rw [catch_stepA (a := specPtr w) (s' := σ) ?_]; rotate_left + · exact findVs_runA hvs (hS1 _ hvs) + try dsimp only + cases fd with + | none => + rw [catch_bind_errA hpost] + exact Or.inl ⟨rfl, rfl⟩ + | some slot => + obtain ⟨p, hp, l, -, hl3, hlv, ⟨sh, -, hsl⟩, -, hW, -⟩ := hpost + rw [catch_stepA hW] + try dsimp only + have hidx := idxA_lt vaddr sh + obtain ⟨hwr, σ1, hst, R1, hk1⟩ := write_clearPteA R hp hidx (hT p hp _ hidx) hK + rw [← hsl] at hwr hst R1 + have hty' : Fl4SpecA64.levelType (lvA l) = ptTyA (Lv p) := by rw [levelType_lvA hl3]; congr 1; omega + rw [← hty'] at hst + rw [catch_stepA (a := ()) (s' := σ1) ?_]; rotate_left + · exact liftE_runA hst + rw [hwr, bind_ok] + try dsimp only + right + have hstAsid : σ1.archState.armAsidTable = σ.archState.armAsidTable := by + obtain ⟨t0, hkt0, hty0, -⟩ := R.pt p hp + rw [hsl, hty'] at hst + rw [storePte_runA hkt0 hty0 (R.al_mod hp) hidx .invalidPTE] at hst + have e := congrArg Prod.fst hst + simp only [Set.singleton_eq_singleton_iff, Prod.mk.injEq, true_and] at e + subst e; rfl + exact ⟨slot, ⟨p, hp, _, hidx, hsl⟩, rfl, + specOk_msA R1 (keepTb_ofA hp hk1) (tail_okA _ _ (doMachineOp_okA (machineOpLift_okA _)) (invTlb_okA _)), + asidKeep_msA hstAsid (tail_okA _ _ (doMachineOp_okA (machineOpLift_okA _)) (invTlb_okA _))⟩ + · exact Or.inl ⟨rfl, by rw [catch_bind_errA (findVs_noneA hrel)]; rfl⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/VsWalkA.lean b/proofs/Fl4ProofsA64/P3/VsWalkA.lean new file mode 100644 index 000000000..d58c439c6 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/VsWalkA.lean @@ -0,0 +1,662 @@ +/- +P3 (aarch64, A64-P3 VS gen 2, gen 3): **the 4-level Stage-1 page-table walk refinement** — FL4's `lookup_pt_slot` +(`vspace.rs` 120–130) against l4v's `pt_walk` / `pt_lookup_slot` (`ArchVSpaceAcc_A`, ported as `ptWalk` / +`ptLookupSlot` in `Fl4SpecA64/KHeap`), under `VsRelA`, through the pointer shift `specPtr` (`PtrA`). The twin of +x86's `lookup_pt_slot_rel` (`P3/VsPt`; x86 walks one function per level, l4v AArch64 one recursive walk). + +**Levels.** FL4's loop counter `level` (3 at the root, 0 at the last level) is l4v's `vm_level` (`max_pt_level = +3`), `lvA`; the shift is `12 + 9 level` (l4v `pt_bits_left`, `ptBitsLeft_lvA`); FL4's table depth `Lv p` is +`3 - level`, so l4v's `level_type` is the relation's `ptTyA (Lv p)` (`levelType_lvA`). + +* **Words and pointers**: `entry_addr_eqA` (FL4's `entry_addr` is `peAF t (idxA v sh)`, `idxA` the 9-bit index), + `is_table_eqA` (above the last level, `e & 3 = 3`), `table_of_eqA` (`dTabA`), **`slotOffA`** (l4v's + `pt_slot_offset` at the shifted table is the shift of FL4's slot), **`pptrFromPte_tabA`** (l4v's `pptr_from_pte` + of the related `PageTablePTE` is the shift of FL4's `table_of`; needs the field `(dAddrA w).val < 2^44` of + `PteRelA`); +* **`ptesOf_atA`**: l4v's `ptes_of` at the shifted slot of a related table of the right type is the entry + `VsRelA.pt` relates (`is_aligned`, `table_base`, `table_index` computed on `specPtr`); +* **`ptWalkN_succA`**: one step of l4v's walk; +* **`lookup_pt_slot_loopA`** (invariant `LkWA`: the slot is FL4's slot of a table of `Tb` at depth `3 - level`, and + l4v's walk from that table at `level` is the whole walk `W`) and **`lookup_pt_slotA`**: from a Stage-1 root of + `Tb` (depth 0), FL4's slot `lu.slot` and shift `lu.shift = 12 + 9 l` are l4v's: `pt_walk max_pt_level 0` stops + at level `l` in the table `specPtr p` whose word `lu.slot` is, and **`pt_lookup_slot (specPtr root) vaddr = + Some (l, specPtr lu.slot)`**. + +**Gen 3**: the tables' page alignment is `VsRelA`'s field `al` (the gen 2 hypothesis `hal` is gone), and +**`pt_lookup_from_level`** (l4v `pt_lookup_from_level`, ported `ptLookupFromLevel`, the walk of `unmap_page_table` to +the slot naming a given table) against FL4's `unmap_page_table` loop (`vspace.rs` 495–508): +* l4v's exception monad at one state: `lfA_bind_one`, `lfA_run_bind_ok` / `_err`, `lfA_run_returnOk_bind`, + `lfA_run_getsThe_bind`; **`ptLookupFromLevelN_zeroA`** (level 0 throws `InvalidRoot`) and + **`ptLookupFromLevelN_succA`** (one step: not a table, `InvalidRoot`; a table naming the target, the slot; else one + level down); +* **`unmap_page_table_loopA`** (invariant `LfWA`: FL4's `(table, shift, level)`, `level` the depth, against l4v's + lookup from `specPtr table` at level `3 - level`, which is the whole lookup `W`; post `LfPostA`: FL4's `None` is l4v's + `InvalidRoot`, FL4's `Some slot` a slot of a table of `Tb` at level `l > 0` whose descriptor's `table_of` is the + target, and l4v returns `(specPtr slot, l)`) and **`pt_lookup_from_levelA`** (from a Stage-1 root: l4v's + `pt_lookup_from_level max_pt_level (specPtr root) vaddr (specPtr pt)`, deterministic, no failure). The loop lemma + is stated from any depth of a `Tb` table; FL4's high-window start (shift 30 from the window's level-1 table, not + a `Tb` table: an `ArmHighVSpace` object) is not covered. + +Hypotheses: `VsRelA` only. No named premise. No `Fl4SpecA64` edit. +-/ +import Fl4ProofsA64.P3.VsFindA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 + +noncomputable section + +local notation "wa" => core.num.U64.wrapping_add +local notation "wm" => core.num.U64.wrapping_mul + +/-! ## Levels -/ + +theorem vmLevelCount_eqA : Fl4SpecA64.vmLevelCount = 5 := rfl + +/-- **l4v's level of FL4's loop counter** (module docstring). -/ +def lvA (l : Nat) : Fl4SpecA64.VmLevel := ⟨min l 3, by rw [vmLevelCount_eqA]; omega⟩ + +theorem lvA_val {l : Nat} (h : l ≤ 3) : (lvA l).val = l := by + show min l 3 = l; omega + +theorem maxPtLevel_valA : Fl4SpecA64.maxPtLevel.val = 3 := rfl + +theorem maxPtLevel_eqA : Fl4SpecA64.maxPtLevel = lvA 3 := Fin.ext (by rw [maxPtLevel_valA, lvA_val le_rfl]) + +theorem lvA_pred {l : Nat} (h0 : 0 < l) (h3 : l ≤ 3) : lvA l - 1 = lvA (l - 1) := by + interval_cases l <;> first | rfl | decide + +theorem lvA_pos {l : Nat} (h0 : 0 < l) (h3 : l ≤ 3) : (0 : Fl4SpecA64.VmLevel) < lvA l := by + rw [Fin.lt_def, lvA_val h3]; exact h0 + +theorem ptTB9 (t : Fl4SpecA64.PtType) : Fl4SpecA64.ptTranslationBits t = 9 := by + unfold Fl4SpecA64.ptTranslationBits + rw [if_neg (show ¬ (t = .vsRootPtT ∧ Fl4SpecA64.config_ARM_PA_SIZE_BITS_40 = true) from fun h => by + have h2 := h.2; unfold Fl4SpecA64.config_ARM_PA_SIZE_BITS_40 at h2; cases h2)] + +theorem ptBits12 (t : Fl4SpecA64.PtType) : Fl4SpecA64.ptBits t = 12 := by + unfold Fl4SpecA64.ptBits Fl4SpecA64.tableSize + rw [ptTB9]; rfl + +theorem pteBits3 : Fl4SpecA64.pteBits = 3 := rfl + +theorem levelType_lvA {l : Nat} (h : l ≤ 3) : Fl4SpecA64.levelType (lvA l) = ptTyA (3 - l) := by + unfold Fl4SpecA64.levelType ptTyA + by_cases e : l = 3 + · subst e + rw [if_pos maxPtLevel_eqA.symm, if_pos (show 3 - 3 = 0 from rfl)] + · rw [if_neg (show ¬ (lvA l = Fl4SpecA64.maxPtLevel) from fun h' => e (by + have h2 := congrArg Fin.val h'; rw [lvA_val h, maxPtLevel_valA] at h2; exact h2)), + if_neg (show ¬ (3 - l = 0) by omega)] + +theorem ptBitsLeft_lvA {l : Nat} (h : l ≤ 3) : Fl4SpecA64.ptBitsLeft (lvA l) = 12 + 9 * l := by + unfold Fl4SpecA64.ptBitsLeft + have h4 : Fl4SpecA64.asidPoolLevel.val = 4 := rfl + rw [if_neg (show ¬ (lvA l = Fl4SpecA64.asidPoolLevel) from fun h' => by + have h2 := congrArg Fin.val h'; rw [lvA_val h, h4] at h2; omega), ptTB9, lvA_val h] + unfold Fl4SpecA64.pageBits + omega + +/-! ## FL4's words -/ + +/-- The 9-bit index of `v` at shift `sh` (FL4's `(vaddr >> shift) & 511`). -/ +def idxA (v sh : U64) : U64 := ⟨BitVec.ofNat 64 (v.val / 2 ^ sh.val % 512)⟩ + +theorem idxA_val (v sh : U64) : (idxA v sh).val = v.val / 2 ^ sh.val % 512 := by + show (BitVec.ofNat 64 _).toNat = _ + rw [BitVec.toNat_ofNat] + apply Nat.mod_eq_of_lt + have := Nat.mod_lt (v.val / 2 ^ sh.val) (by decide : 512 > 0) + omega + +theorem idxA_lt (v sh : U64) : (idxA v sh).val < 512 := by + rw [idxA_val]; exact Nat.mod_lt _ (by decide) + +/-- **`entry_addr`** (module docstring). -/ +theorem entry_addr_eqA (t v sh : U64) (hs : sh.val < 64) : + vspace.entry_addr t v sh = ok (peAF t (idxA v sh)) := by + unfold vspace.entry_addr + simp only [lift, bind_ok] + have e63 : (sh &&& 63#u64) = sh := + UScalar.eq_of_val_eq (by rw [and_maskA sh 6 _ rfl]; exact Nat.mod_eq_of_lt (by simp only [Nat.reducePow]; omega)) + rw [e63] + obtain ⟨i, ei, vi⟩ := shrA v sh (2 ^ sh.val) hs rfl + rw [ei] + simp only [bind_ok] + have e : (i &&& 511#u64) = idxA v sh := UScalar.eq_of_val_eq (by rw [and_maskA i 9 _ rfl, vi, idxA_val]; rfl) + rw [e]; rfl + +/-- **`is_table` above the last level** (module docstring). -/ +theorem is_table_eqA (e sh : U64) (h : 12 < sh.val) : + vspace.is_table e sh = ok (decide ((e &&& 3#u64) = 3#u64)) := by + unfold vspace.is_table + rw [if_pos (by scalar_tac)] + rw [eq_of_spec_fd P0.Consts.vspace.D_VALID.spec, eq_of_spec_fd P0.Consts.vspace.D_TABLE_OR_PAGE.spec] + simp only [lift, bind_ok] + have e3 : (1#u64 ||| 2#u64) = 3#u64 := by decide + rw [e3] + +theorem pptrBase_litA : layout.PPTR_BASE = 18446603336221196288#u64 := by unfold layout.PPTR_BASE; rfl + +/-- **`table_of`** is `dTabA`. -/ +theorem table_of_eqA (e : U64) : vspace.table_of e = ok (dTabA e) := by + unfold vspace.table_of layout.pptr + simp only [lift, bind_ok] + unfold vspace.ADDR_MASK layout.PPTR_BASE dTabA dAddrA + rfl + +/-! ## Descriptor bits -/ + +theorem dBitA_eq (w : U64) (k : Nat) : dBitA w k = decide (w.val / 2 ^ k % 2 = 1) := by + unfold dBitA + rw [BitVec.getLsbD, ← Nat.zero_add k, ← Nat.testBit_div_two_pow, Nat.testBit_zero, Nat.zero_add] + rfl + +/-- A table / page tag (`e & 3 = 3`) is the two low bits. -/ +theorem and3_bitsA (e : U64) : (e &&& 3#u64) = 3#u64 ↔ dBitA e 0 = true ∧ dBitA e 1 = true := by + rw [dBitA_eq, dBitA_eq] + simp only [decide_eq_true_eq, Nat.pow_zero, Nat.pow_one, Nat.div_one] + have h3 : (3#u64 : U64).val = 3 := rfl + constructor + · intro h + have := congrArg UScalar.val h + rw [and3_valA, h3] at this + omega + · rintro ⟨h0, h1⟩ + apply UScalar.eq_of_val_eq + rw [and3_valA, h3] + omega + +theorem dAddrA_mod (w : U64) : (dAddrA w).val % 4096 = 0 := by + unfold dAddrA + rw [UScalar.val_and, show (4096 : Nat) = 2 ^ 12 from rfl, ← Nat.and_two_pow_sub_one_eq_mod, Nat.land_assoc] + show w.val &&& (281474976706560 &&& (2 ^ 12 - 1)) = 0 + rw [show (281474976706560 &&& (2 ^ 12 - 1) : Nat) = 0 by decide, Nat.and_zero] + +theorem paddrFromPpn_ppnA (w : U64) (h : (dAddrA w).val < 2 ^ 44) : + Fl4SpecA64.paddrFromPpn (ppnA w) = (dAddrA w).bv := by + apply BitVec.eq_of_toNat_eq + unfold Fl4SpecA64.paddrFromPpn ppnA + rw [BitVec.toNat_shiftLeft] + show (BitVec.setWidth 64 (BitVec.ofNat 32 ((dAddrA w).val / 4096))).toNat <<< Fl4SpecA64.pageBits % 2 ^ 64 = + (dAddrA w).bv.toNat + rw [BitVec.toNat_setWidth, BitVec.toNat_ofNat, Nat.shiftLeft_eq] + have hm := dAddrA_mod w + have e : (dAddrA w).bv.toNat = (dAddrA w).val := rfl + rw [e] + unfold Fl4SpecA64.pageBits + generalize (dAddrA w).val = d at h hm ⊢ + simp only [Nat.reducePow] at h ⊢ + omega + +/-- **l4v's `pptr_from_pte` of a related table descriptor** (module docstring). -/ +theorem pptrFromPte_tabA (w : U64) (h : (dAddrA w).val < 2 ^ 44) : + Fl4SpecA64.pptrFromPte (.pageTablePTE (ppnA w)) = specPtr (dTabA w) := by + unfold Fl4SpecA64.pptrFromPte Fl4SpecA64.pteBaseAddr + rw [if_pos (show (Fl4SpecA64.PTE.pageTablePTE (ppnA w)).isPageTablePTE = true from rfl)] + show Fl4SpecA64.ptrFromPAddr (Fl4SpecA64.paddrFromPpn (ppnA w)) = _ + rw [paddrFromPpn_ppnA w h, ptrFromPAddr_wa, pptrBase_litA] + rfl + +/-! ## l4v's slot offsets and reads -/ + +theorem mask9_toNatA : (Fl4SpecA64.mask 9 : BitVec 64).toNat = 511 := by unfold Fl4SpecA64.mask; decide +theorem mask12_toNatA : (Fl4SpecA64.mask 12 : BitVec 64).toNat = 4095 := by unfold Fl4SpecA64.mask; decide +theorem notMask12_toNatA : (~~~(Fl4SpecA64.mask 12 : BitVec 64)).toNat = 18446744073709547520 := by + unfold Fl4SpecA64.mask; decide + +/-- **`pt_slot_offset` at the shifted table** (module docstring). -/ +theorem slotOffA (v p sh : U64) {l : Nat} (hl : l ≤ 3) (hsh : sh.val = 12 + 9 * l) : + Fl4SpecA64.ptSlotOffset (lvA l) (specPtr p) v.bv = specPtr (peAF p (idxA v sh)) := by + unfold Fl4SpecA64.ptSlotOffset peAF + rw [specPtr_wa] + apply congrArg (specPtr p + ·) + apply BitVec.eq_of_toNat_eq + unfold Fl4SpecA64.ptIndex + rw [ptBitsLeft_lvA hl, ptTB9, pteBits3, BitVec.toNat_shiftLeft, BitVec.toNat_and, BitVec.toNat_ushiftRight, + mask9_toNatA] + show _ = (wm (idxA v sh) 8#u64).val + rw [wm8_valA (idxA_lt v sh), idxA_val, hsh, Nat.shiftRight_eq_div_pow, Nat.shiftLeft_eq, + show (511 : Nat) = 2 ^ 9 - 1 from rfl, Nat.and_two_pow_sub_one_eq_mod] + have e : v.bv.toNat = v.val := rfl + rw [e] + generalize v.val / 2 ^ (12 + 9 * l) = x + simp only [Nat.reducePow] + omega + +theorem slot_toNatA {p i : U64} (hp : p.val % 4096 = 0) (hi : i.val < 512) : + (specPtr (peAF p i)).toNat = (specPtr p).toNat + 8 * i.val ∧ (specPtr p).toNat % 4096 = 0 := by + have h1 := specPtr_mod p (n := 12) (by omega) + have hlt := (specPtr p).isLt + unfold peAF + rw [specPtr_wa, BitVec.toNat_add] + have e : (wm i 8#u64).bv.toNat = 8 * i.val := wm8_valA hi + rw [e] + simp only [Nat.reducePow] at h1 hlt ⊢ + constructor <;> omega + +theorem ptsOf_eqA {σ : Fl4SpecA64.State Unit} {p : Fl4SpecA64.ObjRef} {t : Fl4SpecA64.PT} + (hk : σ.kheap p = some (.archObj (.pageTable t))) : Fl4SpecA64.ptsOf σ p = some t := by + unfold Fl4SpecA64.ptsOf Fl4SpecA64.aobjsOf Fl4Spec.optMap + rw [hk] <;> rfl + +theorem rbindA {ρ α β : Type} (x : Fl4Spec.Lookup ρ α) (f : α → Fl4Spec.Lookup ρ β) (r : ρ) : + (x >>= f) r = Option.bind (x r) (fun a => f a r) := rfl + +/-- **The geometry of a shifted slot** (gen 3): l4v's `is_aligned`, `table_base` and `table_index` at the shifted +slot `i` of a page-aligned table `p`. -/ +theorem slot_geomA {p i : U64} (ptT : Fl4SpecA64.PtType) (hp : p.val % 4096 = 0) (hi : i.val < 512) : + Fl4SpecA64.isAligned (specPtr (peAF p i)) Fl4SpecA64.pteBits ∧ + Fl4SpecA64.tableBase ptT (specPtr (peAF p i)) = specPtr p ∧ + Fl4SpecA64.tableIndex ptT (specPtr (peAF p i)) = i.bv := by + obtain ⟨e, hm⟩ := slot_toNatA hp hi + have hsl := (specPtr (peAF p i)).isLt + have hal : Fl4SpecA64.isAligned (specPtr (peAF p i)) Fl4SpecA64.pteBits := by + unfold Fl4SpecA64.isAligned + rw [pteBits3, e] + exact Nat.dvd_of_mod_eq_zero (by simp only [Nat.reducePow]; omega) + have hb : Fl4SpecA64.tableBase ptT (specPtr (peAF p i)) = specPtr p := by + apply BitVec.eq_of_toNat_eq + show ((specPtr (peAF p i)) &&& ~~~(Fl4SpecA64.mask (Fl4SpecA64.ptBits ptT))).toNat = _ + rw [ptBits12, BitVec.toNat_and, notMask12_toNatA, e, + show (18446744073709547520 : Nat) = 2 ^ 64 - 1 - (2 ^ 12 - 1) from rfl, + nat_and_hiA _ 12 (by rw [← e]; exact hsl) (by omega)] + simp only [Nat.reducePow] + omega + have hx : Fl4SpecA64.tableIndex ptT (specPtr (peAF p i)) = i.bv := by + apply BitVec.eq_of_toNat_eq + show ((specPtr (peAF p i) &&& Fl4SpecA64.mask (Fl4SpecA64.ptBits ptT)) >>> Fl4SpecA64.pteBits).toNat = i.val + rw [ptBits12, pteBits3, BitVec.toNat_ushiftRight, BitVec.toNat_and, mask12_toNatA, e, + Nat.shiftRight_eq_div_pow, show (4095 : Nat) = 2 ^ 12 - 1 from rfl, Nat.and_two_pow_sub_one_eq_mod] + simp only [Nat.reducePow] + omega + exact ⟨hal, hb, hx⟩ + +/-- **l4v's `ptes_of` at a related table's shifted slot** (module docstring). -/ +theorem ptesOf_atA {σ : Fl4SpecA64.State Unit} {p i : U64} {t : Fl4SpecA64.PT} {ptT : Fl4SpecA64.PtType} + (hk : σ.kheap (specPtr p) = some (.archObj (.pageTable t))) (ht : Fl4SpecA64.ptType t = ptT) + (hp : p.val % 4096 = 0) (hi : i.val < 512) : + Fl4SpecA64.ptesOf σ ptT (specPtr (peAF p i)) = some (Fl4SpecA64.ptApply t i.bv) := by + obtain ⟨hal, hb, hx⟩ := slot_geomA ptT hp hi + have hpt := ptsOf_eqA hk + show Fl4SpecA64.levelPteOf ptT (specPtr (peAF p i)) (Fl4SpecA64.ptsOf σ) = _ + unfold Fl4SpecA64.levelPteOf Fl4Spec.Lookup.oassert + simp only [rbindA, if_pos hal, hb, hx, Fl4SpecA64.oapply, hpt, Option.bind, if_pos ht, + Fl4Spec.Lookup.oreturn] + +/-! ## l4v's walk -/ + +/-- **One step of l4v's walk** (module docstring). -/ +theorem ptWalkN_succA (n : Nat) (ℓ : Fl4SpecA64.VmLevel) (P v : Fl4SpecA64.Word) (ptes : Fl4SpecA64.PtesOf) + (pte : Fl4SpecA64.PTE) (hl : (0 : Fl4SpecA64.VmLevel) < ℓ) + (he : ptes (Fl4SpecA64.levelType ℓ) (Fl4SpecA64.ptSlotOffset ℓ P v) = some pte) : + Fl4SpecA64.ptWalkN (n + 1) ℓ 0 P v ptes = + if pte.isPageTablePTE then Fl4SpecA64.ptWalkN n (ℓ - 1) 0 (Fl4SpecA64.pptrFromPte pte) v ptes + else some (ℓ, P) := by + simp only [Fl4SpecA64.ptWalkN] + rw [if_pos hl] + simp only [rbindA, Fl4SpecA64.oapply2, he, Option.bind] + by_cases hc : pte.isPageTablePTE = true <;> simp only [hc, ↓reduceIte, Bool.false_eq_true] <;> rfl + +/-! ## l4v's exception monad at one state -/ + +section ndrun +open Fl4Spec +variable {σ ε α β : Type} + +theorem lfA_bind_one {m : NonDet σ α} {s s' : σ} {a : α} (h : m s = ({(a, s')}, False)) (f : α → NonDet σ β) : + (m >>= f) s = f a s' := by + show ({r | ∃ x ∈ (m s).1, r ∈ (f x.1 x.2).1}, (m s).2 ∨ ∃ x ∈ (m s).1, (f x.1 x.2).2) = _ + simp only [h, Set.mem_singleton_iff, exists_eq_left, false_or] + ext <;> simp + +theorem lfA_run_bind_ok {m : NonDetE σ ε α} {s : σ} {a : α} (h : m.run s = ({(.ok a, s)}, False)) + (f : α → NonDetE σ ε β) : (m >>= f).run s = (f a).run s := by + rw [ExceptT.run_bind, lfA_bind_one h] + +theorem lfA_run_bind_err {m : NonDetE σ ε α} {s : σ} {e : ε} (h : m.run s = ({(.error e, s)}, False)) + (f : α → NonDetE σ ε β) : (m >>= f).run s = ({(.error e, s)}, False) := by + rw [ExceptT.run_bind, lfA_bind_one h]; rfl + +theorem lfA_run_returnOk_bind {s : σ} (a : α) (f : α → NonDetE σ ε β) : + (NonDetE.returnOk a >>= f).run s = (f a).run s := + lfA_run_bind_ok (m := NonDetE.returnOk a) (s := s) (a := a) rfl f + +theorem lfA_run_getsThe_bind {f : σ → Option α} {s : σ} {a : α} (h : f s = some a) (g : α → NonDetE σ ε β) : + (NonDetE.liftE (NonDet.getsThe f) >>= g).run s = (g a).run s := by + refine lfA_run_bind_ok ?_ g + unfold NonDetE.liftE + rw [ExceptT.run_lift] + show (NonDet.getsThe f >>= _) s = _ + rw [lfA_bind_one (show NonDet.getsThe f s = ({(a, s)}, False) by + show NonDet.assertOpt (f s) s = _; rw [h]; rfl)] + rfl + +end ndrun + +/-! ## l4v's `pt_lookup_from_level` -/ + +/-- **`pt_lookup_from_level` at level 0** throws `InvalidRoot` (both fuel arms start with `unlessE (0 < level)`). -/ +theorem ptLookupFromLevelN_zeroA (n : Nat) (P v T : Fl4SpecA64.Word) (σ : Fl4SpecA64.State Unit) : + (Fl4SpecA64.ptLookupFromLevelN (Z := Unit) n (lvA 0) P v T).run σ = ({(.error .invalidRoot, σ)}, False) := by + have h0 : ¬ ((0 : Fl4SpecA64.VmLevel) < lvA 0) := by rw [Fin.lt_def, lvA_val (by omega)]; exact Nat.lt_irrefl 0 + have hu : (Fl4Spec.NonDetE.unlessE ((0 : Fl4SpecA64.VmLevel) < lvA 0) + (Fl4Spec.NonDetE.throwError Fl4SpecA64.LookupFailure.invalidRoot) : + Fl4SpecA64.LFMonad Unit Unit).run σ = ({(.error .invalidRoot, σ)}, False) := by + unfold Fl4Spec.NonDetE.unlessE; rw [if_neg h0]; rfl + cases n with + | zero => simp only [Fl4SpecA64.ptLookupFromLevelN]; exact lfA_run_bind_err hu _ + | succ n => simp only [Fl4SpecA64.ptLookupFromLevelN]; exact lfA_run_bind_err hu _ + +/-- **One step of l4v's `pt_lookup_from_level`** (module docstring): above level 0, the entry at the slot decides: not +a table, `InvalidRoot`; a table naming the target, the slot and level; else the walk goes one level down. -/ +theorem ptLookupFromLevelN_succA (n : Nat) (ℓ : Fl4SpecA64.VmLevel) (P v T : Fl4SpecA64.Word) + (σ : Fl4SpecA64.State Unit) (pte : Fl4SpecA64.PTE) (hl : (0 : Fl4SpecA64.VmLevel) < ℓ) + (he : Fl4SpecA64.ptesOf σ (Fl4SpecA64.levelType ℓ) (Fl4SpecA64.ptSlotOffset ℓ P v) = some pte) : + (Fl4SpecA64.ptLookupFromLevelN (Z := Unit) (n + 1) ℓ P v T).run σ = + if pte.isPageTablePTE then + (if Fl4SpecA64.pptrFromPte pte = T then ({(.ok (Fl4SpecA64.ptSlotOffset ℓ P v, ℓ), σ)}, False) + else (Fl4SpecA64.ptLookupFromLevelN (Z := Unit) n (ℓ - 1) (Fl4SpecA64.pptrFromPte pte) v T).run σ) + else ({(.error .invalidRoot, σ)}, False) := by + simp only [Fl4SpecA64.ptLookupFromLevelN] + have hu : (Fl4Spec.NonDetE.unlessE ((0 : Fl4SpecA64.VmLevel) < ℓ) + (Fl4Spec.NonDetE.throwError Fl4SpecA64.LookupFailure.invalidRoot) : + Fl4SpecA64.LFMonad Unit Unit).run σ = ({(.ok (), σ)}, False) := by + unfold Fl4Spec.NonDetE.unlessE; rw [if_pos hl]; rfl + rw [lfA_run_bind_ok hu] + try beta_reduce + rw [lfA_run_returnOk_bind] + try beta_reduce + rw [lfA_run_getsThe_bind (f := fun s : Fl4SpecA64.State Unit => + Fl4SpecA64.ptesOf s (Fl4SpecA64.levelType ℓ) (Fl4SpecA64.ptSlotOffset ℓ P v)) he] + by_cases hc : pte.isPageTablePTE = true + · have hu' : (Fl4Spec.NonDetE.unlessE (pte.isPageTablePTE = true) + (Fl4Spec.NonDetE.throwError Fl4SpecA64.LookupFailure.invalidRoot) : + Fl4SpecA64.LFMonad Unit Unit).run σ = ({(.ok (), σ)}, False) := by + unfold Fl4Spec.NonDetE.unlessE; rw [if_pos hc]; rfl + rw [lfA_run_bind_ok hu'] + try beta_reduce + rw [lfA_run_returnOk_bind, if_pos hc] + try beta_reduce + by_cases ht : Fl4SpecA64.pptrFromPte pte = T + · rw [if_pos ht, if_pos ht]; rfl + · rw [if_neg ht, if_neg ht] + · have hu' : (Fl4Spec.NonDetE.unlessE (pte.isPageTablePTE = true) + (Fl4Spec.NonDetE.throwError Fl4SpecA64.LookupFailure.invalidRoot) : + Fl4SpecA64.LFMonad Unit Unit).run σ = ({(.error .invalidRoot, σ)}, False) := by + unfold Fl4Spec.NonDetE.unlessE; rw [if_neg hc]; rfl + rw [lfA_run_bind_err hu', if_neg hc] + +/-! ## The walk refinement -/ + +/-- **The loop invariant** (module docstring): FL4's loop state `(shift, slot, level)`. -/ +def LkWA (Pp Tb Y : Set U64) (Lv : U64 → Nat) (mem : U64 → U64) (ksb : U64) (σ : Fl4SpecA64.State Unit) (v : U64) + (W : Option (Fl4SpecA64.VmLevel × Fl4SpecA64.ObjRef)) (x : U64 × U64 × U64) : Prop := + x.2.2.val ≤ 3 ∧ x.1.val = 12 + 9 * x.2.2.val ∧ ∃ p ∈ Tb, VsReachA Pp Tb Y mem ksb p ∧ + Lv p + x.2.2.val = 3 ∧ x.2.1 = peAF p (idxA v x.1) ∧ + Fl4SpecA64.ptWalkN x.2.2.val (lvA x.2.2.val) 0 (specPtr p) v.bv (Fl4SpecA64.ptesOf σ) = W + +/-- **Where the loop stops** (module docstring). -/ +def LkPostA (Pp Tb Y : Set U64) (Lv : U64 → Nat) (mem : U64 → U64) (ksb : U64) (v : U64) + (W : Option (Fl4SpecA64.VmLevel × Fl4SpecA64.ObjRef)) (r : U64 × U64) : Prop := + ∃ p ∈ Tb, ∃ l, l ≤ 3 ∧ r.1.val = 12 + 9 * l ∧ Lv p + l = 3 ∧ r.2 = peAF p (idxA v r.1) ∧ + W = some (lvA l, specPtr p) ∧ VsReachA Pp Tb Y mem ksb p + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {Pp Tb : Set U64} {Lv : U64 → Nat} {X Y : Set U64} {σ : Fl4SpecA64.State Unit} + +private theorem vsReachA_step_tbA {mem : U64 → U64} {ksb : U64} + (hR : VsRelA Pp Tb Lv X Y mem ksb σ) {p i : U64} (hp : p ∈ Tb) (hlv : Lv p < 3) (hi : i.val < 512) + (hreach : VsReachA Pp Tb Y mem ksb p) (ht3 : (mem (peAF p i) &&& 3#u64) = 3#u64) : + VsReachA Pp Tb Y mem ksb (dTabA (mem (peAF p i))) := by + obtain ⟨tag, sh0, htag, hpath⟩ := hreach + have hsh0 : sh0 = 39 - 9 * Lv p := by + rcases (hR.path_props hpath).2 with ⟨-, -, hsh0⟩ | ⟨-, -, y, hy, -, hq⟩ + · exact hsh0 + · exact absurd (hq ▸ hp) (hR.ytb y hy) + exact ⟨tag, sh0 - 9, htag, .child hpath (Or.inl hp) (by omega) hi ht3⟩ + +set_option maxHeartbeats 4000000 in +/-- **The loop against l4v's walk** (module docstring). -/ +theorem lookup_pt_slot_loopA {s : S} (hR : VsRelA Pp Tb Lv X Y (L.mem s) (L.ksb s) σ) + (vaddr : U64) (W : Option (Fl4SpecA64.VmLevel × Fl4SpecA64.ObjRef)) + (shift slot level : U64) (hi : LkWA Pp Tb Y Lv (L.mem s) (L.ksb s) σ vaddr W (shift, slot, level)) : + vspace.lookup_pt_slot_loop st s vaddr shift slot level ⦃ r => + LkPostA Pp Tb Y Lv (L.mem s) (L.ksb s) vaddr W r ⦄ := by + unfold vspace.lookup_pt_slot_loop + apply loop.spec_decr_nat (measure := fun x => x.2.2.val) (inv := LkWA Pp Tb Y Lv (L.mem s) (L.ksb s) σ vaddr W) + · rintro ⟨sh, sl, lv⟩ ⟨hl3, hsh, p, hp, hreach, hlv, e, hw⟩ + simp only at hl3 hsh hlv e hw + subst e + dsimp only + unfold vspace.lookup_pt_slot_loop.body + split + · rename_i hpos + have hpos' : 0 < lv.val := by simpa using hpos + rw [L.read_eq, bind_ok, is_table_eqA _ _ (by omega), bind_ok] + obtain ⟨t, hk, hty, hrel⟩ := hR.pt p hp + have hidx := idxA_lt vaddr sh + have hpre := hrel hreach _ hidx + have hread := ptesOf_atA hk (ptT := Fl4SpecA64.levelType (lvA lv.val)) + (by rw [hty, levelType_lvA hl3]; congr 1; omega) (hR.al_mod hp) hidx + rw [← slotOffA vaddr p sh hl3 hsh] at hread + obtain ⟨m, hm⟩ : ∃ m, lv.val = m + 1 := ⟨lv.val - 1, by omega⟩ + rw [hm] at hw hread + generalize hpe : Fl4SpecA64.ptApply t (idxA vaddr sh).bv = pte at hpre hread + rw [ptWalkN_succA m (lvA (m + 1)) _ _ _ pte (lvA_pos (by omega) (by omega)) hread] at hw + generalize hew : L.mem s (peAF p (idxA vaddr sh)) = w at hpre ⊢ + by_cases ht3 : (w &&& 3#u64) = 3#u64 + · rw [if_pos (by simp [ht3])] + obtain ⟨hb0, hb1⟩ := (and3_bitsA w).1 ht3 + cases pte with + | invalidPTE => exact absurd (hpre.symm.trans hb0) Bool.false_ne_true + | pagePTE pa sm pat prt => + exfalso + obtain ⟨-, -, -, hk3, -⟩ := hpre + have := hk3.1 hb1 + omega + | pageTablePTE ppn => + obtain ⟨-, -, -, h44, hppn, hT, hL⟩ := hpre + subst hppn + have hreach' : VsReachA Pp Tb Y (L.mem s) (L.ksb s) (dTabA w) := by + rw [← hew] at ht3 ⊢ + exact vsReachA_step_tbA hR hp (by omega) hidx hreach ht3 + rw [if_pos (show (Fl4SpecA64.PTE.pageTablePTE (ppnA w)).isPageTablePTE = true from rfl), + lvA_pred (by omega) (by omega), Nat.add_sub_cancel, pptrFromPte_tabA w h44] at hw + step as ⟨lv1, hlv1⟩ + have h9 : (core.num.U64.wrapping_sub sh 9#u64).val = sh.val - 9 := by + simp [core.num.U64.wrapping_sub_val_eq, U64.size_def, U64.numBits]; omega + simp only [lift, bind_ok, table_of_eqA] + rw [entry_addr_eqA _ _ _ (by rw [h9]; omega)] + simp only [bind_ok] + refine (WP.spec_ok _).2 ⟨⟨?_, ?_, _, hT, hreach', ?_, rfl, ?_⟩, ?_⟩ + · simp only; omega + · simp only; rw [h9]; omega + · simp only; rw [hL]; omega + · simp only + rw [show lv1.val = m by omega] + exact hw + · show lv1.val < lv.val + omega + · rw [if_neg (by simp [ht3])] + have hnt : pte.isPageTablePTE = false := by + cases pte with + | pageTablePTE ppn => + exact absurd ((and3_bitsA w).2 ⟨hpre.2.1, hpre.2.2.1⟩) ht3 + | _ => rfl + simp only [hnt, Bool.false_eq_true, ↓reduceIte] at hw + refine (WP.spec_ok _).2 ⟨p, hp, lv.val, hl3, hsh, hlv, rfl, ?_, hreach⟩ + rw [hm] + exact hw.symm + · rename_i hz + have h0 : lv.val = 0 := by scalar_tac + rw [h0] at hw hlv hsh + refine (WP.spec_ok _).2 ⟨p, hp, 0, by omega, hsh, hlv, rfl, ?_, hreach⟩ + exact hw.symm.trans rfl + · exact hi + +set_option maxHeartbeats 2000000 in +/-- **`lookup_pt_slot` against l4v's `pt_walk` / `pt_lookup_slot`** (module docstring). -/ +theorem lookup_pt_slotA {s : S} (hR : VsRelA Pp Tb Lv X Y (L.mem s) (L.ksb s) σ) + {root : U64} (hr : root ∈ Tb) (h0 : Lv root = 0) + (hreach : VsReachA Pp Tb Y (L.mem s) (L.ksb s) root) (vaddr : U64) : + vspace.lookup_pt_slot st s root vaddr ⦃ lu => ∃ p ∈ Tb, ∃ l, l ≤ 3 ∧ lu.shift.val = 12 + 9 * l ∧ + Lv p + l = 3 ∧ lu.slot = peAF p (idxA vaddr lu.shift) ∧ + Fl4SpecA64.ptWalk Fl4SpecA64.maxPtLevel 0 (specPtr root) vaddr.bv (Fl4SpecA64.ptesOf σ) = + some (lvA l, specPtr p) ∧ + Fl4SpecA64.ptLookupSlot (specPtr root) vaddr.bv (Fl4SpecA64.ptesOf σ) = some (lvA l, specPtr lu.slot) ∧ + VsReachA Pp Tb Y (L.mem s) (L.ksb s) p ⦄ := by + unfold vspace.lookup_pt_slot + rw [entry_addr_eqA _ _ _ (by simp)] + simp only [bind_ok] + refine WP.spec_bind (lookup_pt_slot_loopA hR vaddr + (Fl4SpecA64.ptWalk Fl4SpecA64.maxPtLevel 0 (specPtr root) vaddr.bv (Fl4SpecA64.ptesOf σ)) vspace.TOP_LEVEL_SHIFT + (peAF root (idxA vaddr vspace.TOP_LEVEL_SHIFT)) 3#u64 + ⟨by simp, by simp, root, hr, hreach, by rw [h0]; rfl, rfl, by + unfold Fl4SpecA64.ptWalk; rw [maxPtLevel_eqA]; rfl⟩) ?_ + rintro ⟨sh, sl⟩ ⟨p, hp, l, hl, hsh, hlv, e, hW, hreachP⟩ + refine (WP.spec_ok _).2 ⟨p, hp, l, hl, hsh, hlv, e, hW, ?_, hreachP⟩ + show Fl4SpecA64.ptLookupSlot (specPtr root) vaddr.bv (Fl4SpecA64.ptesOf σ) = some (lvA l, specPtr sl) + unfold Fl4SpecA64.ptLookupSlot Fl4SpecA64.ptLookupSlotFromLevel + have e' : sl = peAF p (idxA vaddr sh) := e + simp only [rbindA, hW, Option.bind, Fl4Spec.Lookup.oreturn] + show some (lvA l, Fl4SpecA64.ptSlotOffset (lvA l) (specPtr p) vaddr.bv) = _ + rw [slotOffA vaddr p sh hl hsh, e'] + +end + + +/-! ## `pt_lookup_from_level` -/ + +/-- The value of l4v's `pt_lookup_from_level` at one state (deterministic, no failure). -/ +abbrev LfResA := Set (Except Fl4SpecA64.LookupFailure (Fl4SpecA64.Word × Fl4SpecA64.VmLevel) × + Fl4SpecA64.State Unit) × Prop + +/-- **The loop invariant of `unmap_page_table`'s walk** (module docstring): FL4's loop state `(table, shift, +level)`, `level` FL4's depth (0 at a root), against l4v's `pt_lookup_from_level` from `table` at level +`3 - level`, which is the whole lookup `W`. -/ +def LfWA (Pp Tb Y : Set U64) (Lv : U64 → Nat) (mem : U64 → U64) (ksb : U64) (σ : Fl4SpecA64.State Unit) + (v pt : U64) (W : LfResA) (x : U64 × U64 × U64) : Prop := + x.2.2.val ≤ 3 ∧ x.2.1.val = 39 - 9 * x.2.2.val ∧ x.1 ∈ Tb ∧ VsReachA Pp Tb Y mem ksb x.1 ∧ + Lv x.1 = x.2.2.val ∧ + (Fl4SpecA64.ptLookupFromLevelN (Z := Unit) (3 - x.2.2.val) (lvA (3 - x.2.2.val)) (specPtr x.1) v.bv + (specPtr pt)).run σ = W + +/-- **Where `unmap_page_table`'s walk stops** (module docstring): `none` is l4v's `InvalidRoot`; `some slot` is a slot +of a table of `Tb` at l4v's level `l > 0` whose descriptor names `pt`, and l4v's result is its shifted pointer and +`l`. -/ +def LfPostA (Pp Tb Y : Set U64) (Lv : U64 → Nat) (mem : U64 → U64) (ksb : U64) (σ : Fl4SpecA64.State Unit) + (v pt : U64) (W : LfResA) : Option U64 → Prop + | none => W = ({(.error .invalidRoot, σ)}, False) + | some slot => ∃ p ∈ Tb, ∃ l, 0 < l ∧ l ≤ 3 ∧ Lv p + l = 3 ∧ (∃ sh : U64, sh.val = 12 + 9 * l ∧ + slot = peAF p (idxA v sh)) ∧ dTabA (mem slot) = pt ∧ W = ({(.ok (specPtr slot, lvA l), σ)}, False) ∧ + VsReachA Pp Tb Y mem ksb p + +section +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {Pp Tb : Set U64} {Lv : U64 → Nat} {X Y : Set U64} {σ : Fl4SpecA64.State Unit} + +set_option maxHeartbeats 4000000 in +/-- **`unmap_page_table`'s walk against l4v's `pt_lookup_from_level`** (module docstring). -/ +theorem unmap_page_table_loopA {s : S} (hR : VsRelA Pp Tb Lv X Y (L.mem s) (L.ksb s) σ) (vaddr pt : U64) + (W : LfResA) (table shift level : U64) (hi : LfWA Pp Tb Y Lv (L.mem s) (L.ksb s) σ vaddr pt W (table, shift, level)) : + vspace.unmap_page_table_loop st s vaddr pt table shift level ⦃ r => + LfPostA Pp Tb Y Lv (L.mem s) (L.ksb s) σ vaddr pt W r ⦄ := by + unfold vspace.unmap_page_table_loop + apply loop.spec_decr_nat (measure := fun x => 3 - x.2.2.val) (inv := LfWA Pp Tb Y Lv (L.mem s) (L.ksb s) σ vaddr pt W) + · rintro ⟨tb, sh, lv⟩ ⟨hl3, hsh, htb, hreach, hlv, hw⟩ + simp only at hl3 hsh htb hlv hw + dsimp only + unfold vspace.unmap_page_table_loop.body + split + · rename_i hlt + have hlt' : lv.val < 3 := by scalar_tac + rw [entry_addr_eqA _ _ _ (by omega), bind_ok, L.read_eq, bind_ok, is_table_eqA _ _ (by omega), bind_ok] + obtain ⟨m, hm⟩ : ∃ m, 3 - lv.val = m + 1 := ⟨2 - lv.val, by omega⟩ + have hsh' : sh.val = 12 + 9 * (m + 1) := by omega + obtain ⟨t, hk, hty, hrel⟩ := hR.pt tb htb + have hidx := idxA_lt vaddr sh + have hpre := hrel hreach _ hidx + have hread := ptesOf_atA hk (ptT := Fl4SpecA64.levelType (lvA (m + 1))) + (by rw [hty, levelType_lvA (by omega)]; congr 1; omega) (hR.al_mod htb) hidx + have hso := slotOffA vaddr tb sh (l := m + 1) (by omega) hsh' + rw [← hso] at hread + rw [hm] at hw + generalize hpe : Fl4SpecA64.ptApply t (idxA vaddr sh).bv = pte at hpre hread + rw [ptLookupFromLevelN_succA m (lvA (m + 1)) _ _ _ σ pte (lvA_pos (by omega) (by omega)) hread, hso] at hw + generalize hew : L.mem s (peAF tb (idxA vaddr sh)) = w at hpre ⊢ + by_cases ht3 : (w &&& 3#u64) = 3#u64 + · rw [if_pos (by simp [ht3])] + obtain ⟨hb0, hb1⟩ := (and3_bitsA w).1 ht3 + cases pte with + | invalidPTE => exact absurd (hpre.symm.trans hb0) Bool.false_ne_true + | pagePTE pa sm pat prt => + exfalso + obtain ⟨-, -, -, hk3, -⟩ := hpre + have := hk3.1 hb1 + omega + | pageTablePTE ppn => + obtain ⟨-, -, -, h44, hppn, hT, hL⟩ := hpre + subst hppn + rw [if_pos (show (Fl4SpecA64.PTE.pageTablePTE (ppnA w)).isPageTablePTE = true from rfl), + pptrFromPte_tabA w h44] at hw + rw [table_of_eqA, bind_ok] + by_cases hq : dTabA w = pt + · rw [if_pos hq] + rw [if_pos (show specPtr (dTabA w) = specPtr pt by rw [hq])] at hw + refine (WP.spec_ok _).2 ⟨tb, htb, m + 1, by omega, by omega, by omega, ⟨sh, hsh', rfl⟩, ?_, hw.symm, hreach⟩ + rw [hew]; exact hq + · rw [if_neg hq] + have hreach' : VsReachA Pp Tb Y (L.mem s) (L.ksb s) (dTabA w) := by + rw [← hew] at ht3 ⊢ + exact vsReachA_step_tbA hR htb (by omega) hidx hreach ht3 + rw [if_neg (show ¬ specPtr (dTabA w) = specPtr pt from fun e => hq (specPtr_inj e)), lvA_pred (by omega) (by omega), Nat.add_sub_cancel] at hw + simp only [lift, bind_ok] + step as ⟨lv1, hlv1⟩ + have h9 : (core.num.U64.wrapping_sub sh 9#u64).val = sh.val - 9 := by + simp [core.num.U64.wrapping_sub_val_eq, U64.size_def, U64.numBits]; omega + first + | refine (WP.spec_ok _).2 ⟨⟨?_, ?_, hT, hreach', ?_, ?_⟩, ?_⟩ + | refine ⟨⟨?_, ?_, hT, hreach', ?_, ?_⟩, ?_⟩ + · (try simp only); omega + · (try simp only); rw [h9]; omega + · (try simp only); rw [hL]; omega + · (try simp only) + rw [show 3 - lv1.val = m by omega] + exact hw + · show 3 - lv1.val < 3 - lv.val + omega + · rw [if_neg (by simp [ht3])] + have hnt : pte.isPageTablePTE = false := by + cases pte with + | pageTablePTE ppn => + exact absurd ((and3_bitsA w).2 ⟨hpre.2.1, hpre.2.2.1⟩) ht3 + | _ => rfl + simp only [hnt, Bool.false_eq_true, ↓reduceIte] at hw + exact (WP.spec_ok _).2 hw.symm + · rename_i hge + have h3 : lv.val = 3 := by scalar_tac + rw [h3] at hw + exact (WP.spec_ok _).2 (hw.symm.trans (ptLookupFromLevelN_zeroA _ _ _ _ σ)) + · exact hi + +set_option maxHeartbeats 2000000 in +/-- **`pt_lookup_from_level` from a Stage-1 root** (module docstring): FL4's `unmap_page_table` walk from a root of +`Tb` (shift `TOP_LEVEL_SHIFT`, depth 0) against l4v's `pt_lookup_from_level max_pt_level (specPtr root) vaddr +(specPtr pt)`, the call in l4v's `unmap_page_table`. -/ +theorem pt_lookup_from_levelA {s : S} (hR : VsRelA Pp Tb Lv X Y (L.mem s) (L.ksb s) σ) {root : U64} + (hr : root ∈ Tb) (h0 : Lv root = 0) (hreach : VsReachA Pp Tb Y (L.mem s) (L.ksb s) root) (vaddr pt : U64) : + vspace.unmap_page_table_loop st s vaddr pt root vspace.TOP_LEVEL_SHIFT 0#u64 ⦃ r => + LfPostA Pp Tb Y Lv (L.mem s) (L.ksb s) σ vaddr pt + ((Fl4SpecA64.ptLookupFromLevel (Z := Unit) Fl4SpecA64.maxPtLevel (specPtr root) vaddr.bv + (specPtr pt)).run σ) r ⦄ := + unmap_page_table_loopA hR vaddr pt _ root _ _ ⟨by simp, by simp, hr, hreach, by rw [h0]; rfl, by + unfold Fl4SpecA64.ptLookupFromLevel; rw [maxPtLevel_eqA]; rfl⟩ + +end + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/W1UserBytesA.lean b/proofs/Fl4ProofsA64/P3/W1UserBytesA.lean new file mode 100644 index 000000000..dffcc44d5 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/W1UserBytesA.lean @@ -0,0 +1,342 @@ +/- +P3 (aarch64, A64-P3 Stage-3 integration step 1; Architect's ruling on the `w1` user bytes): **the abstract state at +`w1` with the root task's user words as its byte memory**, and the root IPC buffer as the tracked buffer `Bf`. + +S3's `w1SA` keeps `initASt`'s byte memory (`initUnderlyingMemory`, all zero) and does not depend on the concrete user +words `uw` of A64-Boot's `w1M`; `EpStateRel.buf` relates a tracked buffer's words to the spec's bytes. The ruling +parameterises the spec memory by `uw` (the buffer is **not** ruled zero): + +* **`withUmA σ um`**: `σ` with its byte memory `um`; **`userBytesA uw`** (byte `a % 8` of the word + `uw (fl4Ptr (a - a % 8))`, little-endian, as AArch64 `storeWord` lays it out); **`w1SUA … uw`** + `= {w1SA … with machineState.underlyingMemory := userBytesA uw}`. +* **`loadWordAt_userBytesA`**: at an aligned spec address `q`, l4v's `loadWord` of `userBytesA uw` is `uw (fl4Ptr q)`. +* Congruence under the memory field (none of these reads `underlyingMemory`): **`SpecSameA_withUmA`** + (hence **`JAtZAX_withUmA`** by `JAtZAX.of_spec`), **`JEntA_withUmA`**, **`SInvXA_withUmA`** (`SInvXA.of_spec`), + **`SpecVsA_withUmA`** (`SpecVsA.of_same`), **`HwTieA_withUmA`**; **`w1_jEntA_exU`**: `w1_jEntA_ex` at `w1SUA`. +* **The root buffer** **`w1BfA`** `= {(bwv IPb, specPtr (bwv IPb))}`: **`w1_bufRelA`** (`EpStateRel.buf` at `w1M` and + `w1SUA`: each of the 128 words is `uw` of it, by `w1M_root`'s user-frame arm), **`w1_lookup_rtA`** (FL4's + `lookup_ipc_buffer` of the root TCB is `bwv IPb`), **`ipcBufOfA_rootA`** (the spec's is `specPtr (bwv IPb)`), + **`w1_bufCoverA`** (`BufCover` at `w1`: the idle TCBs' buffer caps are null), and the buffer clauses of `EPlaced` + (**`w1Bf_align`**, **`w1Bf_tcb`**, **`w1Bf_inj`**). + +Premises: none. New module; no existing statement changed. +-/ +import Fl4ProofsA64.P3.JEntW1A +import Fl4ProofsA64.P3.FrameA +import Fl4ProofsA64.P3.EpBufA +import Fl4ProofsA64.P3.UnmapDecJA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P3 +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P1.W7.R.ST.BootWit Fl4ProofsA64.P3.Endp + +noncomputable section + +/-! ## The spec state with a byte memory -/ + +/-- **`σ` with its byte memory `um`** (module docstring). -/ +def withUmA (σ : Fl4SpecA64.State Unit) (um : Fl4SpecA64.Word → Fl4SpecA64.Word8) : Fl4SpecA64.State Unit := + { σ with machineState.underlyingMemory := um } + +/-- **The user words as bytes** (module docstring): byte `a % 8` of the FL4 word holding spec address `a`. -/ +def userBytesA (uw : U64 → U64) (a : Fl4SpecA64.Word) : Fl4SpecA64.Word8 := + if UTb ≤ (fl4Ptr a).val ∧ (fl4Ptr a).val < UTb + 1048576 then 0 else + ((uw (fl4Ptr (a - BitVec.ofNat 64 (a.toNat % 8)))).bv >>> (8 * (a.toNat % 8))).setWidth 8 + +/-- **The abstract state at `w1` with the user words `uw`** (module docstring; the Architect's ruling). -/ +def w1SUA (kb ib : U64) (nc : Nat) (now mt sl : U64) (uw : U64 → U64) : Fl4SpecA64.State Unit := + { w1SA kb ib nc now mt sl with machineState.underlyingMemory := userBytesA uw } + +theorem w1SUA_eq (kb ib : U64) (nc : Nat) (now mt sl : U64) (uw : U64 → U64) : + w1SUA kb ib nc now mt sl uw = withUmA (w1SA kb ib nc now mt sl) (userBytesA uw) := rfl + +/-- **The free RAM untyped is zero** (ruling 21:51Z item 3, J10 `SpecZeroFreeA` at `w1SUA`): A64-Boot's RAM untyped +`[UTb, UTb + 2^20)` is all free at `w1`. -/ +theorem userBytesA_ut (uw : U64 → U64) {a : Fl4SpecA64.Word} (h1 : UTb ≤ (fl4Ptr a).val) + (h2 : (fl4Ptr a).val < UTb + 1048576) : userBytesA uw a = 0 := by + unfold userBytesA; rw [if_pos ⟨h1, h2⟩] + +theorem fl4Ptr_add_val {q c : Fl4SpecA64.Word} (h : (fl4Ptr q).val + c.toNat < 2 ^ 64) : + (fl4Ptr (q + c)).val = (fl4Ptr q).val + c.toNat := by + rw [fl4Ptr_def, fl4Ptr_def] at * + have e : q + c - pptrShiftA = q - pptrShiftA + c := by abel + have h' : (q - pptrShiftA).toNat + c.toNat < 2 ^ 64 := h + change (q + c - pptrShiftA).toNat = (q - pptrShiftA).toNat + c.toNat + rw [e, BitVec.toNat_add, Nat.mod_eq_of_lt h'] + +theorem userBytesA_at (uw : U64 → U64) {q c : Fl4SpecA64.Word} (hq : q.toNat % 8 = 0) (hc : c.toNat < 8) + (hU : (fl4Ptr q).val + 8 ≤ UTb ∨ UTb + 1048576 ≤ (fl4Ptr q).val) (hw : (fl4Ptr q).val + 8 < 2 ^ 64) : + userBytesA uw (q + c) = ((uw (fl4Ptr q)).bv >>> (8 * c.toNat)).setWidth 8 := by + have hv := fl4Ptr_add_val (q := q) (c := c) (by omega) + have h1 : (q + c).toNat % 8 = c.toNat := by + rw [BitVec.toNat_add]; have := q.isLt; omega + have h2 : q + c - BitVec.ofNat 64 c.toNat = q := by + have e : BitVec.ofNat 64 c.toNat = c := by simp + rw [e, BitVec.add_sub_cancel] + unfold userBytesA; rw [if_neg (by omega), h1, h2] + +/-- **l4v's `loadWord` of the user bytes** (module docstring). -/ +theorem loadWordAt_userBytesA (uw : U64 → U64) {q : Fl4SpecA64.Word} (hq : q.toNat % 8 = 0) + (hU : (fl4Ptr q).val + 8 ≤ UTb ∨ UTb + 1048576 ≤ (fl4Ptr q).val) (hw : (fl4Ptr q).val + 8 < 2 ^ 64) : + loadWordAt (userBytesA uw) q = (uw (fl4Ptr q)).bv := by + have key : ∀ i ∈ Fl4SpecA64.intUpto 0 7, userBytesA uw (q + (7 - BitVec.ofInt 64 i)) = + ((uw (fl4Ptr q)).bv >>> (8 * (7 - BitVec.ofInt 64 i).toNat)).setWidth 8 := by + intro i hi + rw [intUpto_07] at hi + refine userBytesA_at uw hq ?_ hU hw + simp only [List.mem_cons, List.not_mem_nil, or_false] at hi + rcases hi with rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> decide + unfold loadWordAt + rw [List.map_congr_left key] + generalize (uw (fl4Ptr q)).bv = v + rw [intUpto_07] + have e0 : (7 - BitVec.ofInt 64 (0 : Int)).toNat = 7 := by decide + have e1 : (7 - BitVec.ofInt 64 (1 : Int)).toNat = 6 := by decide + have e2 : (7 - BitVec.ofInt 64 (2 : Int)).toNat = 5 := by decide + have e3 : (7 - BitVec.ofInt 64 (3 : Int)).toNat = 4 := by decide + have e4 : (7 - BitVec.ofInt 64 (4 : Int)).toNat = 3 := by decide + have e5 : (7 - BitVec.ofInt 64 (5 : Int)).toNat = 2 := by decide + have e6 : (7 - BitVec.ofInt 64 (6 : Int)).toNat = 1 := by decide + have e7 : (7 - BitVec.ofInt 64 (7 : Int)).toNat = 0 := by decide + simp only [List.map_cons, List.map_nil, e0, e1, e2, e3, e4, e5, e6, e7] + unfold Fl4SpecA64.wordRcat + simp only [List.foldl_cons, List.foldl_nil] + apply BitVec.eq_of_getLsbD_eq + intro i hi + simp only [BitVec.getLsbD_or, BitVec.getLsbD_shiftLeft, BitVec.getLsbD_setWidth, BitVec.getLsbD_ushiftRight] + interval_cases i <;> simp + +/-! ## Congruence under the byte memory -/ + +section Cong +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} + +/-- The byte memory is outside every related object and global (`SpecSameA`). -/ +theorem SpecSameA_withUmA (Z : XSetsA) (m : Fl4SpecA64.McState) (σ : Fl4SpecA64.State Unit) + (um : Fl4SpecA64.Word → Fl4SpecA64.Word8) : SpecSameA Z m σ (withUmA σ um) where + cur := rfl + idle := rfl + csc := rfl + act := rfl + time := rfl + irqS := rfl + irqN := rfl + rq := rfl + rls := rfl + cons := rfl + repr := rfl + dom := rfl + domT := rfl + asid := rfl + vcur := rfl + domk := ⟨rfl, rfl, rfl, rfl⟩ + kh _ _ := rfl + ext _ _ h _ := h + cdt := rfl + orig := rfl + +theorem JAtZAX_withUmA {AR : A → HwRootA → Prop} {Z : XSetsA} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} + {m : Fl4SpecA64.McState} (J : JAtZAX (L := L) AR Z s a σ m) (um : Fl4SpecA64.Word → Fl4SpecA64.Word8) : + JAtZAX (L := L) AR Z s a (withUmA σ um) m := + JAtZAX.of_spec J (SpecSameA_withUmA Z m σ um) + +theorem JEntA_withUmA {AR : A → HwRootA → Prop} {Z : XSetsA} {H : Heap} {OS U Ac Pp Tb : Set U64} + {Lv : U64 → Nat} {ia ie : Nat} {s : S} {a : A} {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} + (J : JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a σ m) (um : Fl4SpecA64.Word → Fl4SpecA64.Word8) : + JEntA (L := L) AR Z H OS U Ac Pp Tb Lv ia ie s a (withUmA σ um) m := + { J with + j := JAtZAX_withUmA J.j um + tabInj := J.tabInj } + +end Cong + +theorem SInvXA_withUmA {σ : Fl4SpecA64.State Unit} {m : Fl4SpecA64.McState} {n : Nat} (h : SInvXA σ m n) + (um : Fl4SpecA64.Word → Fl4SpecA64.Word8) : SInvXA (withUmA σ um) m n := + SInvXA.of_spec h ⟨fun _ _ _ => Iff.rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ rfl ⟨h.irqN.node, h.irqN.slot, h.irqN.ref⟩ + +theorem SpecVsA_withUmA {σ : Fl4SpecA64.State Unit} (h : SpecVsA σ) (um : Fl4SpecA64.Word → Fl4SpecA64.Word8) : + SpecVsA (withUmA σ um) := + SpecVsA.of_same h ⟨rfl, fun _ _ _ => Iff.rfl⟩ + +theorem HwTieA_withUmA {Z : XSetsA} {s2 : Option HwRootA} {σ : Fl4SpecA64.State Unit} (h : HwTieA Z s2 σ) + (um : Fl4SpecA64.Word → Fl4SpecA64.Word8) : HwTieA Z s2 (withUmA σ um) := + HwTieA.of_spec h rfl rfl rfl (vspaceForAsid_congrA rfl rfl) (fun _ => rfl) + +/-- **`w1_jEntA_ex` at `w1SUA`** (module docstring). -/ +theorem w1_jEntA_exU {A : Type} (AR : A → HwRootA → Prop) {a : A} (r now mt sl : U64) (uw : U64 → U64) + (s2 : Option HwRootA) (hAR : AR a bootHwA) : + JEntA (L := bL) AR (w1ZA 18446603337294938112#u64 18446603338368679936#u64 128) (w1H 18446603337294938112#u64 18446603338368679936#u64 128) + (w1OS 18446603337294938112#u64 18446603338368679936#u64 128) w1U (w1H 18446603337294938112#u64 18446603338368679936#u64 128).scs + (Fl4ProofsA64.P2.BootEpB.w1Pp 18446603337294938112#u64 18446603338368679936#u64 128 r now mt sl uw) w1Tb w1Lv + (18446603338368679936#u64 : U64).val ((18446603338368679936#u64 : U64).val + 128 * 8192) + (w1S 18446603337294938112#u64 18446603338368679936#u64 128 r now mt sl uw) a + (w1SUA 18446603337294938112#u64 18446603338368679936#u64 128 now mt sl uw) (w1MA 18446603338368679936#u64 128) ∧ + SInvXA (w1SUA 18446603337294938112#u64 18446603338368679936#u64 128 now mt sl uw) (w1MA 18446603338368679936#u64 128) 128 ∧ + SpecVsA (w1SUA 18446603337294938112#u64 18446603338368679936#u64 128 now mt sl uw) ∧ + HwTieA (w1ZA 18446603337294938112#u64 18446603338368679936#u64 128) s2 (w1SUA 18446603337294938112#u64 18446603338368679936#u64 128 now mt sl uw) := by + obtain ⟨h1, h2, h3, h4⟩ := w1_jEntA_ex AR r now mt sl uw s2 hAR + rw [w1SUA_eq] + exact ⟨JEntA_withUmA h1 _, SInvXA_withUmA h2 _, SpecVsA_withUmA h3 _, HwTieA_withUmA h4 _⟩ + +/-! ## The root IPC buffer -/ + +/-- **The tracked buffers at `w1`** (module docstring): the root TCB's IPC buffer. -/ +def w1BfA : Set (U64 × Fl4SpecA64.Word) := {(bwv IPb, specPtr (bwv IPb))} + +theorem waV {a b : U64} (h : a.val + b.val < 2 ^ 64) : (core.num.U64.wrapping_add a b).val = a.val + b.val := by + simp [core.num.U64.wrapping_add_val_eq, U64.size_def, U64.numBits]; omega + +theorem bufW_valA {b k : U64} (h : b.val + 8 * k.val < 2 ^ 64) : (bufW b k).val = b.val + 8 * k.val := by + unfold bufW + simp [core.num.U64.wrapping_add_val_eq, core.num.U64.wrapping_mul_val_eq, U64.size_def, U64.numBits] + have := k.hBounds + omega + +theorem bufAddr_specPtrA {b k : U64} (h : b.val + 8 * k.val < 2 ^ 64) : + bufAddr (specPtr b) k.val = specPtr (bufW b k) := by + apply BitVec.eq_of_toNat_eq + unfold bufAddr + rw [BitVec.toNat_add, specPtr_toNat, specPtr_toNat, BitVec.toNat_ofNat, bufW_valA h] + simp only [Fl4SpecA64.wordSize] + omega + +section Boot +variable {kb ib r now mt sl : U64} {nc : Nat} {uw : U64 → U64} + +set_option hygiene false in +local notation "S1" => w1S kb ib nc r now mt sl uw + +theorem bufW_ipb {k : U64} (hk : k.val < 128) : (bufW (bwv IPb) k).val = IPb + 8 * k.val := by + rw [bufW_valA (by rw [vIP]; omega), vIP] + +/-- The root IPC buffer's words are user words of `w1M`. -/ +theorem w1M_ipbA (P : BootLay1 kb ib nc) {k : U64} (hk : k.val < 128) : + w1M kb ib nc r now mt sl uw (bufW (bwv IPb) k) = uw (bufW (bwv IPb) k) := by + rw [w1M_root P (o := 401408 + 8 * k.val) (by omega) (by rw [bufW_ipb hk]; omega)] + unfold rootW + rw [if_neg (by omega), if_neg (by omega), if_neg (by omega), if_neg (by omega), if_pos (by omega)] + +/-- **`EpStateRel.buf` at `w1`** (module docstring): the root buffer's words against `userBytesA uw`. -/ +theorem w1_bufRelA (P : BootLay1 kb ib nc) : ∀ bp ∈ w1BfA, ∀ k : U64, k.val < 128 → + (w1M kb ib nc r now mt sl uw (bufW bp.1 k)).bv = + loadWordAt (w1SUA kb ib nc now mt sl uw).machineState.underlyingMemory (bufAddr bp.2 k.val) ∧ + (bufAddr bp.2 k.val).toNat % 8 = 0 := by + intro bp hbp k hk + obtain rfl : bp = (bwv IPb, specPtr (bwv IPb)) := hbp + have hv := bufW_ipb hk + have ha : bufAddr (specPtr (bwv IPb)) k.val = specPtr (bufW (bwv IPb) k) := + bufAddr_specPtrA (by rw [vIP]; omega) + have hal : (specPtr (bufW (bwv IPb) k)).toNat % 8 = 0 := by rw [specPtr_toNat, hv]; omega + dsimp only + rw [ha] + refine ⟨?_, hal⟩ + show _ = loadWordAt (userBytesA uw) _ + have hU : (fl4Ptr (specPtr (bufW (bwv IPb) k))).val + 8 ≤ UTb ∨ + UTb + 1048576 ≤ (fl4Ptr (specPtr (bufW (bwv IPb) k))).val := by + rw [fl4Ptr_specPtr, hv]; omega + have hw : (fl4Ptr (specPtr (bufW (bwv IPb) k))).val + 8 < 2 ^ 64 := by rw [fl4Ptr_specPtr, hv]; omega + rw [loadWordAt_userBytesA uw hal hU hw, fl4Ptr_specPtr, w1M_ipbA P hk] + +/-- **FL4's IPC-buffer lookup of the root TCB at `w1`** (module docstring). -/ +theorem w1_lookup_rtA (P : BootLay1 kb ib nc) (b : Bool) : + msg.lookup_ipc_buffer bst S1 b (bwv RTb) = ok (bwv IPb) := by + have hm : obj.Tcb.ipc_buffer bst (bwv RTb) S1 = ok (bwv 4198400) := by + rw [ipc_buffer_word] + congr 1 + exact (w1_tR P (K := 3512#u64) (by simp)).trans (by simp [rtW]) + have hc : obj.Slot.cap bst (slotAddrA (specPtr (bwv RTb)) (BitVec.ofNat 64 2)) S1 = ok (rtCapA 2) := by + rw [Fl4ProofsA64.P1.W7.R.ST.capA_eq] + congr 1 + exact rt_capA P (by decide) + unfold msg.lookup_ipc_buffer + have hr2 : rtCapA 2 = .Frame (bwv IPb) .Small 3#u64 false false false 0#u64 (bwv 0) := by simp [rtCapA] + rw [hm, bind_ok, tcb_slot_bufferA, bind_ok, hc, bind_ok, hr2] + simp only [capability.FrameSize.bits, capability.VM_READ_WRITE, lift, bind_ok, Bool.false_eq_true, ↓reduceIte] + obtain ⟨v, hv, hP⟩ := spec_existsA (m := (do + let i1 ← 1#u64 <<< (12#u64 &&& 63#u64) + ok (core.num.U64.wrapping_add (bwv IPb) (bwv 4198400 &&& core.num.U64.wrapping_sub i1 1#u64)) : Result U64)) + (P := fun w => w = bwv IPb) (by + step* + apply UScalar.eq_of_val_eq + simp_all [core.num.U64.wrapping_add_val_eq, core.num.U64.wrapping_sub_val_eq, U64.size_def, U64.numBits] + rw [vIP, BootWit.wv (n := 4198400) (by norm_num)] + decide) + first + | (rw [hv, hP]) + | (simp only [] at hv ⊢; rw [hv, hP]) + | (simp at hv ⊢; done) + +/-- **The spec's IPC buffer of the root TCB** (module docstring). -/ +theorem ipcBufOfA_rootA (b : Bool) : ipcBufOfA b rootTcbA = some (specPtr (bwv IPb)) := by + have e : (BitVec.ofNat 64 4198400 &&& Fl4SpecA64.mask (Fl4SpecA64.pageBitsForSize .armSmallPage) : + Fl4SpecA64.Word) = 0 := by decide + have hr : Fl4SpecA64.vmReadWrite ⊆ vmRightsOfA 3#u64 := vmRightsOfA_rw rfl + unfold ipcBufOfA + simp only [rootTcbA, rtCapA, absCapA, absFrameE, frameSizeOfA, if_neg (show (2 : Nat) ≠ 0 by decide), + if_neg (show (2 : Nat) ≠ 1 by decide), if_pos rfl, Bool.false_eq_true, ↓reduceIte] + rw [if_pos (Or.inl hr), e] + simp + +/-- **`BufCover` at `w1`** (module docstring). -/ +theorem w1_bufCoverA (hL : BootLayA ib nc) (hK : BootKsA kb ib nc) (P : BootLay1 kb ib nc) : + BufCover bst (w1H kb ib nc).tcbs w1BfA S1 (w1SUA kb ib nc now mt sl uw) := by + intro t ht x b sb w hk hb hw + rcases w1T P ht with hi | rfl + · obtain ⟨c, hc, rfl⟩ := bootT_of hL hi + obtain ⟨x', hk', hn⟩ := (w1SA_cs (now := now) (mt := mt) (sl := sl) hL hK P).idle c hc + have hx : x' = x := by + have e : (w1SA kb ib nc now mt sl).kheap (specPtr (idleTA ib c)) = some (.tcb x) := hk + rw [hk'] at e; cases e; rfl + subst hx + have h2 := hn 2 (by decide) + rw [tcbCnodeMap_twoA] at h2 + unfold ipcBufOfA at hb + rw [Option.some.inj h2] at hb + simp at hb + · have e : (w1SA kb ib nc now mt sl).kheap (specPtr (bwv RTb)) = some (.tcb x) := hk + change w1KA kb ib nc now mt sl (specPtr (bwv RTb)) = _ at e + rw [w1KA_rt] at e + cases e + rw [ipcBufOfA_rootA] at hb + cases hb + rw [w1_lookup_rtA P] at hw + have hw' : bwv IPb = w := by simpa using hw + subst hw' + refine ⟨rfl, fun e => ?_⟩ + have := congrArg UScalar.val e + rw [vIP] at this + simp at this + +/-- The root buffer's words are aligned (`EPlaced.balign`). -/ +theorem w1Bf_align : ∀ a, BufW w1BfA a → Aligned a := by + rintro a ⟨bp, hbp, k, hk, rfl⟩ + obtain rfl : bp = (bwv IPb, specPtr (bwv IPb)) := hbp + show (bufW (bwv IPb) k).val % 8 = 0 + rw [bufW_ipb hk]; omega + +/-- The root buffer is off every TCB of `w1` (`EPlaced.bt`). -/ +theorem w1Bf_tcb (hL : BootLayA ib nc) (P : BootLay1 kb ib nc) : + ∀ a, BufW w1BfA a → ∀ t ∈ (w1H kb ib nc).tcbs, a.val < t.val ∨ t.val + 4096 ≤ a.val := by + rintro a ⟨bp, hbp, k, hk, rfl⟩ t ht + obtain rfl : bp = (bwv IPb, specPtr (bwv IPb)) := hbp + rw [bufW_ipb hk] + rcases w1T P ht with hi | rfl + · obtain ⟨c, hc, rfl⟩ := bootT_of hL hi + have := idleTA_val' hL hc; have := P.ibR + right; omega + · left; rw [vRT]; omega + +/-- Distinct words of the root buffer are distinct on both sides (`EPlaced.binj`). -/ +theorem w1Bf_inj : ∀ bp ∈ w1BfA, ∀ bq ∈ w1BfA, ∀ k k' : U64, k.val < 128 → k'.val < 128 → + (bufW bp.1 k = bufW bq.1 k' ↔ bufAddr bp.2 k.val = bufAddr bq.2 k'.val) := by + intro bp hbp bq hbq k k' hk hk' + obtain rfl : bp = (bwv IPb, specPtr (bwv IPb)) := hbp + obtain rfl : bq = (bwv IPb, specPtr (bwv IPb)) := hbq + dsimp only + rw [bufAddr_specPtrA (by rw [vIP]; omega), bufAddr_specPtrA (by rw [vIP]; omega), specPtr_iff] + +end Boot + +end + +end Fl4ProofsA64.P3 diff --git a/proofs/Fl4ProofsA64/P3/ZProbeDH.lean b/proofs/Fl4ProofsA64/P3/ZProbeDH.lean new file mode 100644 index 000000000..2bc809b49 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ZProbeDH.lean @@ -0,0 +1,204 @@ +import Fl4ProofsA64.P3.ReplyHookA +import Fl4ProofsA64.P3.DelRdThA +import Fl4ProofsA64.P3.DelClockA +import Fl4ProofsA64.P3.DelTimeSpecA +import Fl4ProofsA64.P3.DelPpLiftA +import Fl4ProofsA64.P3.DelPpA +import Fl4ProofsA64.P3.Stage3SchedOpsTA +import Fl4ProofsA64.P3.Stage3SchedWrNTA +import Fl4ProofsA64.P3.Stage3PreemptTA +import Fl4ProofsA64.P3.Stage3SchedWrTA +import Fl4ProofsA64.P3.DelStepAsmA +import Fl4ProofsA64.P3.DelBufKeepA +import Fl4ProofsA64.P3.DelCtOffA +import Fl4ProofsA64.P3.DelStepKeepA +import Fl4ProofsA64.P3.DelStepSpecA +import Fl4ProofsA64.P3.DelSwapMdbA +import Fl4ProofsA64.P3.DelRestoreA +import Fl4ProofsA64.P3.DelJ5StepA +import Fl4ProofsA64.P3.DelMdbLeafA +import Fl4ProofsA64.P3.DelCapKeepA +import Fl4ProofsA64.P3.DelPoolKeepA +import Fl4ProofsA64.P3.DelRootA +import Fl4ProofsA64.P3.FinHookRootA +import Fl4ProofsA64.P3.FinHookRootKA +import Fl4ProofsA64.P3.FinHookPoolA +import Fl4ProofsA64.P3.DelVsKeepA +import Fl4ProofsA64.P3.FinHookPtA +import Fl4ProofsA64.P3.FinHookFrameA +import Fl4ProofsA64.P3.DelUnmapSpecA +import Fl4ProofsA64.P3.Stage3VsWriteA +import Fl4ProofsA64.P3.DelRecA +import Fl4ProofsA64.P3.DelAsidSpecA +import Fl4ProofsA64.P3.DelAsidA +import Fl4ProofsA64.P3.DelAsidPoolA +import Fl4ProofsA64.P3.ReplyHookRecvArmA +import Fl4ProofsA64.P3.ReplyRemoveFootQA +open Fl4ProofsA64.P3 +#print axioms finHookA_of_kinds +#print axioms delete_asid_jA +#print axioms atOffA +#print axioms VsRelA.of_clearA +#print axioms ExtVsRelA.of_clearA +#print axioms JAtZAX.of_tabClearA +#print axioms delPool_loopA +#print axioms delete_asid_pool_jA +#print axioms Fl4ProofsA64.P3.JStage3A.of_vsWrite +#print axioms Fl4ProofsA64.P3.VsSpecA.of_vmA +#print axioms Fl4ProofsA64.P3.vsW_pageA +#print axioms Fl4ProofsA64.P3.vsW_asidA +#print axioms Fl4ProofsA64.P3.vsW_asid_bufA +#print axioms Fl4ProofsA64.P3.unmapPage_vsA +#print axioms Fl4ProofsA64.P3.unmapPageTable_vsA +#print axioms Fl4ProofsA64.P3.JStage3TA.vsW_osKeepA +#print axioms Fl4ProofsA64.P3.JStage3TA.j5_vsWriteA +#print axioms Fl4ProofsA64.P1.W7.R.ST.SysL.ptW_noSymA +#print axioms Fl4ProofsA64.P3.delVs_zeroKeepA +#print axioms Fl4ProofsA64.P3.unmap_pt_leafA +#print axioms Fl4ProofsA64.P3.fl4FinaliseCap_pt_unpackA +#print axioms Fl4ProofsA64.P3.S1aR2.zeroedKeepA +#print axioms Fl4ProofsA64.P3.finHookPtA +#print axioms Fl4ProofsA64.P3.fl4FinaliseCap_frame_vcA +#print axioms Fl4ProofsA64.P3.unmap_frame_leafA +#print axioms Fl4ProofsA64.P3.s1aWordsKeptA +#print axioms Fl4ProofsA64.P3.finHookFrameA +#print axioms Fl4ProofsA64.P3.delVs_zeroKeepHwA +#print axioms Fl4ProofsA64.P3.JStage3TA.of_hwA +#print axioms Fl4ProofsA64.P3.poolZeroR2A +#print axioms Fl4ProofsA64.P3.VsSpecA.of_poolVmA +#print axioms Fl4ProofsA64.P3.ClrViewA.of_poolVmA +#print axioms Fl4ProofsA64.P3.delete_asid_recA +#print axioms Fl4ProofsA64.P3.poolZeroR2A +#print axioms Fl4ProofsA64.P3.finRoot_runA +#print axioms Fl4ProofsA64.P3.rootSplitA +#print axioms Fl4ProofsA64.P3.rootOrA +#print axioms Fl4ProofsA64.P3.finHookPml4A +#print axioms Fl4ProofsA64.P3.finHookHighA +#print axioms Fl4ProofsA64.P3.finHookEptA +#print axioms Fl4ProofsA64.P3.finHookPoolA +#print axioms Fl4ProofsA64.P3.delete_asid_pool_recA +#print axioms Fl4ProofsA64.P3.delTab_zeroKeepHwA +#print axioms Fl4ProofsA64.P3.J7LA.ofTabZA +#print axioms Fl4ProofsA64.P1.W7.R.ST.S1aR2.poolClearedA +#print axioms Fl4ProofsA64.P3.JStage3TA.mapJ7A +#print axioms Fl4ProofsA64.P3.rdXA_of_J7A +#print axioms Fl4ProofsA64.P3.pnFrameA +#print axioms Fl4ProofsA64.P3.pnPtA +#print axioms Fl4ProofsA64.P3.pnRootA +#print axioms Fl4ProofsA64.P3.J7R2xA.restoreA +#print axioms Fl4ProofsA64.P3.J7LA.restoreA +#print axioms Fl4ProofsA64.P3.JStage3A.toTA +#print axioms Fl4ProofsA64.P3.JStage3TA.toJA +#print axioms Fl4ProofsA64.P3.JEntA.toTA +#print axioms Fl4ProofsA64.P3.JStage3TA.of_vsWrite +#print axioms Fl4ProofsA64.P3.DStateA.ofTA + +#print axioms JStage3TA.of_slotW +#print axioms capw_apartA +#print axioms SchedPageA.fromA +#print axioms BufOffA.fromA +#print axioms DeadA.backA +#print axioms SpecFreeA.heapFromA +#print axioms rdThA_of_ne +#print axioms finHookPoolA +#print axioms Fl4ProofsA64.P3.J7LA.swapA +#print axioms Fl4ProofsA64.P3.J7LA.zombieA +#print axioms Fl4ProofsA64.P3.J7LA.emptyA +#print axioms Fl4ProofsA64.P3.J5A.delSwapA +#print axioms Fl4ProofsA64.P3.J5A.emptyA +#print axioms Fl4ProofsA64.P3.J5A.zombieA +#print axioms Fl4ProofsA64.P3.empty_slot_mdbWA + +#print axioms Fl4ProofsA64.P3.cdt_swap_mdbWA +#print axioms Fl4ProofsA64.P3.set_zombie_sA +#print axioms Fl4ProofsA64.P3.cap_swap_for_delete_hsA +#print axioms Fl4ProofsA64.P3.empty_slot_sA +#print axioms Fl4ProofsA64.P3.specFree_stepA +#print axioms Fl4ProofsA64.P3.nullOrig_swapA +#print axioms Fl4ProofsA64.P3.nullOrig_emptyA +#print axioms Fl4ProofsA64.P3.nullOrig_zombieA +#print axioms Fl4ProofsA64.P3.emptySlot_provA +#print axioms Fl4ProofsA64.P3.capSwapForDelete_provA +#print axioms Fl4ProofsA64.P3.UnmPtSpecA.stepA +#print axioms Fl4ProofsA64.P3.DeadExtA.stepA +#print axioms Fl4ProofsA64.P3.DeadExtA.emptyA +#print axioms Fl4ProofsA64.P3.zombieNoVcpu_keepA +#print axioms Fl4ProofsA64.P3.zombieNoVcpu_zombieA +#print axioms Fl4ProofsA64.P3.tcbFld_keptA +#print axioms Fl4ProofsA64.P3.bufHold_emptyA +#print axioms Fl4ProofsA64.P3.bufHold_swapA +#print axioms Fl4ProofsA64.P3.bufHold_zombieA +#print axioms Fl4ProofsA64.P3.base_not_bufSA +#print axioms Fl4ProofsA64.P3.BufNativeA.stepA +#print axioms Fl4ProofsA64.P3.BufCover.stepA +#print axioms Fl4ProofsA64.P3.ctOff_sameA +#print axioms Fl4ProofsA64.P3.ctOff_emptyA +#print axioms Fl4ProofsA64.P3.ctOff_swapA +#print axioms Fl4ProofsA64.P3.ctOff_zombieA +#print axioms Fl4ProofsA64.P3.offNode_homeA +#print axioms Fl4ProofsA64.P3.offNode_slotA +#print axioms Fl4ProofsA64.P3.offNode_contA +#print axioms Fl4ProofsA64.P3.sinv_zombieA +#print axioms Fl4ProofsA64.P3.cnode_not_tcbZombieA +#print axioms Fl4ProofsA64.P3.swapHyp_delA +#print axioms Fl4ProofsA64.P3.zombieHyp_delA +#print axioms Fl4ProofsA64.P3.emptyHyp_delA +#print axioms Fl4ProofsA64.P3.emptyDead_delA +#print axioms Fl4ProofsA64.P3.poolEm_ofA +#print axioms Fl4ProofsA64.P3.JStage3TA.of_schedWr +#print axioms Fl4ProofsA64.P3.JStage3TA.workUnitsA +#print axioms Fl4ProofsA64.P3.JStage3TA.timestampA +#print axioms Fl4ProofsA64.P3.JStage3TA.machineA +#print axioms Fl4ProofsA64.P3.JStage3TA.scActiveA +#print axioms Fl4ProofsA64.P3.JStage3TA.sufficientA +#print axioms Fl4ProofsA64.P3.Sched.preemption_point_actA +#print axioms Fl4ProofsA64.P3.DelXA.stepPpA +#print axioms Fl4ProofsA64.P3.delPp_hookA +#print axioms Fl4ProofsA64.P3.swapHyp_timeA +#print axioms Fl4ProofsA64.P3.zombieHyp_timeA +#print axioms Fl4ProofsA64.P3.emptyHyp_timeA +#print axioms Fl4ProofsA64.P3.rdTriv_timeA +#print axioms Fl4ProofsA64.P3.FinHookKA.timeA +#print axioms Fl4ProofsA64.P3.JStage3TA.mapARA +#print axioms Fl4ProofsA64.P3.DelXA.mapARA +#print axioms Fl4ProofsA64.P3.ArchKeepsA.andA +#print axioms Fl4ProofsA64.P3.fl4FinaliseCap_tmA +#print axioms Fl4ProofsA64.P3.finTimeSpec_vsA +#print axioms Fl4ProofsA64.P3.finHookFrameTA +#print axioms Fl4ProofsA64.P3.finHookPtTA +#print axioms Fl4ProofsA64.P3.finHookPml4TA +#print axioms Fl4ProofsA64.P3.finHookHighTA +#print axioms Fl4ProofsA64.P3.finHookEptTA +#print axioms Fl4ProofsA64.P3.finHookPoolTA +#print axioms Fl4ProofsA64.P3.rdThA_of_nonfinalA +#print axioms Fl4ProofsA64.P3.utVsZero_keepA +#print axioms Fl4ProofsA64.P3.utVsOk_zeroA +#print axioms Fl4ProofsA64.P3.JStage3TA.vsr_offPPWA +#print axioms Fl4ProofsA64.P3.JStage3TA.vsr_offCapWA +#print axioms Fl4ProofsA64.P3.DevUtOffA.fromA +#print axioms Fl4ProofsA64.P3.UtZombieA.ofSrcA +#print axioms Fl4ProofsA64.P3.swapHyp_delA +#print axioms Fl4ProofsA64.P3.zombieHyp_delA +#print axioms Fl4ProofsA64.P3.emptyHyp_delA +#print axioms Fl4ProofsA64.P3.JStage3TA.schedOpsA +#print axioms Fl4ProofsA64.P3.JStage3TA.schedOpsEA +#print axioms Fl4ProofsA64.P3.JStage3TA.schedFootEA +#print axioms Fl4ProofsA64.P3.JStage3TA.schedFootNA +#print axioms Fl4ProofsA64.P3.JStage3TA.of_schedWrX +#print axioms Fl4ProofsA64.P3.JStage3TA.of_schedWrN +#print axioms Fl4ProofsA64.P3.JStage3TA.replyCallerA +#print axioms Fl4ProofsA64.P3.Endp.cancel_ipc_recvF +#print axioms Fl4ProofsA64.P3.Endp.cancelRecv_specA +#print axioms Fl4ProofsA64.P3.JStage3TA.cancelRecv_blkA +#print axioms Fl4ProofsA64.P3.JStage3TA.replyRecv_recA +#print axioms Fl4ProofsA64.P3.JStage3TA.reply_offStacksA +#print axioms Fl4ProofsA64.P3.finHookReplyA_recvA +#print axioms Fl4ProofsA64.P3.Endp.reply_remove_footQA +#print axioms Fl4ProofsA64.P3.LoopInvA.heapD +#print axioms Fl4ProofsA64.P3.replyRemove_stepA +#print axioms Fl4ProofsA64.P3.JStage3TA.of_schedWrSX +#print axioms Fl4ProofsA64.P3.JStage3TA.of_schedWrSN +#print axioms Fl4ProofsA64.P3.Endp.reply_remove_relQA_sub +#print axioms Fl4ProofsA64.P3.JStage3TA.replyRemove_recA +#print axioms Fl4ProofsA64.P3.finHookReplyA_removeA +#print axioms Fl4ProofsA64.P3.finHookReplyA diff --git a/proofs/Fl4ProofsA64/P3/ZProbeS11A.lean b/proofs/Fl4ProofsA64/P3/ZProbeS11A.lean new file mode 100644 index 000000000..201130d0b --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ZProbeS11A.lean @@ -0,0 +1,24 @@ +import Lean +import Fl4ProofsA64 +open Lean Meta + +def stdAxS11 : List Name := [``propext, ``Classical.choice, ``Quot.sound] + +-- AX probe: every constant of the Fl4ProofsA64.P3.Ut* modules (merged Untyped line) depends only on the three +-- standard axioms (no sorryAx, no ofReduceBool, no user axiom). +#eval show MetaM Unit from do + let env ← getEnv + let mut bad : Array (Name × Name) := #[] + let mut n := 0 + for i in [0:env.header.moduleNames.size] do + let mn := env.header.moduleNames[i]! + if mn.toString.startsWith "Fl4ProofsA64.P3.Ut" then + for c in env.header.moduleData[i]!.constNames do + n := n + 1 + let axs ← collectAxioms c + for a in axs do + if !stdAxS11.contains a then bad := bad.push (c, a) + IO.println s!"does not depend on non-standard axioms: checked {n} constants, bad {bad.size}" + for (c, a) in bad do IO.println s!"BAD {c} {a}" +#print axioms Fl4ProofsA64.P3.Sched.handleInterrupt_presC +#print axioms Fl4ProofsA64.P3.Sched.fl4HandleTimeout_presA diff --git a/proofs/Fl4ProofsA64/P3/ZProbeS12A.lean b/proofs/Fl4ProofsA64/P3/ZProbeS12A.lean new file mode 100644 index 000000000..168a93750 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ZProbeS12A.lean @@ -0,0 +1,3 @@ +import Fl4ProofsA64.P3.Stage3BufWrA +#print axioms Fl4ProofsA64.P3.JStage3A.of_schedWrU +#print axioms Fl4ProofsA64.P3.JStage3A.of_schedWrN diff --git a/proofs/Fl4ProofsA64/P3/ZProbeS13A.lean b/proofs/Fl4ProofsA64/P3/ZProbeS13A.lean new file mode 100644 index 000000000..6a7307104 --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ZProbeS13A.lean @@ -0,0 +1,8 @@ +import Fl4ProofsA64.P3.Stage3IpcFootA +import Fl4ProofsA64.P3.EpSendBlkA +import Fl4ProofsA64.P3.EpSendRecvA +#print axioms Fl4ProofsA64.P3.Endp.send_ipc_recvA +#print axioms Fl4ProofsA64.P3.Endp.send_ipc_blockA +#print axioms Fl4ProofsA64.P3.Endp.schedFootEA_of_sched +#print axioms Fl4ProofsA64.P3.Endp.FpA.stepN +#print axioms Fl4ProofsA64.P3.Endp.reply_unlink_relF diff --git a/proofs/Fl4ProofsA64/P3/ZProbeS8A.lean b/proofs/Fl4ProofsA64/P3/ZProbeS8A.lean new file mode 100644 index 000000000..5c839532d --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ZProbeS8A.lean @@ -0,0 +1,3 @@ +import Fl4ProofsA64.P3.ReplyLiveJointA +#print axioms Fl4ProofsA64.P3.Endp.do_reply_transfer_jointA +#print axioms Fl4ProofsA64.P3.Endp.replyRemoveUnblockJointBorA diff --git a/proofs/Fl4ProofsA64/P3/ZProbeS9A.lean b/proofs/Fl4ProofsA64/P3/ZProbeS9A.lean new file mode 100644 index 000000000..6a5d0fbcd --- /dev/null +++ b/proofs/Fl4ProofsA64/P3/ZProbeS9A.lean @@ -0,0 +1,7 @@ +import Fl4ProofsA64.P3.IpcExtraCapTransferDischA +#print axioms Fl4ProofsA64.P3.Endp.ipcExtraCapTransferA_of_jEntA +#print axioms Fl4ProofsA64.P3.Endp.transfer_caps_relA +#print axioms Fl4ProofsA64.P3.Endp.transfer_caps_loop_relA +#print axioms Fl4ProofsA64.P3.Endp.get_receive_slot_relA +#print axioms Fl4ProofsA64.P3.cap_insert_dBA +#print axioms Fl4ProofsA64.P3.Endp.derive_relEqA diff --git a/proofs/Fl4SpecA64.lean b/proofs/Fl4SpecA64.lean index d4977e1c9..fe3bb10da 100644 --- a/proofs/Fl4SpecA64.lean +++ b/proofs/Fl4SpecA64.lean @@ -29,6 +29,7 @@ import Fl4SpecA64.Decode import Fl4SpecA64.Init import Fl4SpecA64.Syscall import Fl4SpecA64.Ext.ArchTypes +import Fl4SpecA64.Ext.UserTop import Fl4SpecA64.Ext.MultiCore import Fl4SpecA64.Ext.ArmHighVSpace import Fl4SpecA64.Ext.Timer @@ -38,10 +39,13 @@ import Fl4SpecA64.Ext.IrqNode import Fl4SpecA64.Ext.Stage2 import Fl4SpecA64.Ext.ARMVCPURun import Fl4SpecA64.Ext.Finalise +import Fl4SpecA64.Ext.UntypedVirt import Fl4SpecA64.Ext.Platform import Fl4SpecA64.Ext.Label import Fl4SpecA64.Ext.IrqControl +import Fl4SpecA64.Ext.IrqSignal import Fl4SpecA64.Ext.VSpace +import Fl4SpecA64.Ext.FaultReply import Fl4SpecA64.Ext.Tcb import Fl4SpecA64.Ext.Invocation import Fl4SpecA64.Ext.ReplyError diff --git a/proofs/Fl4SpecA64/AARCH64/Arch.lean b/proofs/Fl4SpecA64/AARCH64/Arch.lean index a5a4015c5..52f18ecc5 100644 --- a/proofs/Fl4SpecA64/AARCH64/Arch.lean +++ b/proofs/Fl4SpecA64/AARCH64/Arch.lean @@ -263,7 +263,7 @@ def performPgInvUnmap (cap : ArchCap) (ctSlot : CslotPtr) : SMonad Z Unit := do NonDet.assert (cap.isFrameCap = true) match cap.acapMapData with | some (asid, vaddr) => unmapPage cap.acapFsize asid vaddr cap.acapObj - | _ => return () + | _ => pure () let oldCap ← getCap ctSlot setCap (.archObjectCap (updateMapData oldCap.theArchCap none)) ctSlot @@ -338,7 +338,7 @@ def performPtInvUnmap (cap : ArchCap) (ctSlot : CslotPtr) : SMonad Z Unit := do (p + mask (ptBits cap.acapPtType))) NonDet.mapMx (fun slot => storePte cap.acapPtType slot .invalidPTE) slots doMachineOp (cleanCacheRange_PoU p (p + mask (ptBits cap.acapPtType)) (addrFromPPtr p)) - | _ => return () + | _ => pure () let oldCap ← NonDet.liftM Cap.theArchCap (getCap ctSlot) setCap (.archObjectCap (updateMapData oldCap none)) ctSlot diff --git a/proofs/Fl4SpecA64/AARCH64/Structs.lean b/proofs/Fl4SpecA64/AARCH64/Structs.lean index 37f8bcf87..ba9189366 100644 --- a/proofs/Fl4SpecA64/AARCH64/Structs.lean +++ b/proofs/Fl4SpecA64/AARCH64/Structs.lean @@ -17,8 +17,10 @@ The one change is the in-port `ext` hook (Fl4SpecA64/PORTING.md, "The `ext` hook"): the constructors `ArchCap.ext`, `ArchKernelObj.ext`, `AObjectType.ext`, `AAType.ext`, the field `Vcpu.vcpuExt`, and their arms in the total data functions (`acap_obj`, `arch_obj_size`, `arch_kobj_size`, -`arch_default_cap`, `default_arch_object`, `aa_type`); payloads in -`Ext/ArchTypes`. +`arch_default_cap`, `default_arch_object`, `aa_type`), and (generation 6) the +frame functions' arms for a Stage-2 frame cap (`acap_rights`, `acap_fsize`, +`acap_is_device`, `acap_map_data`, `update_map_data`, `arch_cap_is_device`, +`acap_rights_update`); payloads in `Ext/ArchTypes`. Isabelle's `value_type`s whose size depends on `Kernel_Config` are `BitVec` / `Fin` of the configuration-dependent length: `ppn` is `BitVec (ipaSize - @@ -146,19 +148,23 @@ def ArchCap.acapAsidBase : ArchCap → Asid /-- l4v: Arch_Structs_A.AARCH64_A.acap_rights (selector). -/ def ArchCap.acapRights : ArchCap → CapRights | .frameCap _ r _ _ _ => r + | .ext e => e.rights -- FL4 hook (generation 6): a Stage-2 frame cap's rights | _ => undefined /-- l4v: Arch_Structs_A.AARCH64_A.acap_fsize (selector). -/ def ArchCap.acapFsize : ArchCap → VMPageSize | .frameCap _ _ sz _ _ => sz + | .ext e => e.fsize -- FL4 hook (generation 6) | _ => undefined /-- l4v: Arch_Structs_A.AARCH64_A.acap_is_device (selector). -/ def ArchCap.acapIsDevice : ArchCap → Bool | .frameCap _ _ _ d _ => d + | .ext e => e.acapIsDevice -- FL4 hook (generation 6) | _ => undefined /-- l4v: Arch_Structs_A.AARCH64_A.acap_map_data (selector of two constructors). -/ def ArchCap.acapMapData : ArchCap → Option (Asid × VSpaceRef) | .frameCap _ _ _ _ m => m | .pageTableCap _ _ m => m + | .ext e => e.frameMapData -- FL4 hook (generation 6) | _ => undefined /-- l4v: Arch_Structs_A.AARCH64_A.acap_pt_type (selector). -/ def ArchCap.acapPtType : ArchCap → PtType @@ -183,6 +189,7 @@ def updateMapData (cap : ArchCap) (m : Option (Asid × VSpaceRef)) : ArchCap := match cap with | .frameCap p R sz dev _ => .frameCap p R sz dev m | .pageTableCap p t _ => .pageTableCap p t m + | .ext e => .ext (e.updateMapData m) -- FL4 hook (generation 6) | _ => undefined /-! ### Page tables -/ @@ -383,6 +390,7 @@ def archObjSize : ArchCap → Nat /-- l4v: Arch_Structs_A.AARCH64_A.arch_cap_is_device. -/ def archCapIsDevice : ArchCap → Bool | .frameCap _ _ _ isDev _ => isDev + | .ext e => e.isDevice -- FL4 hook (generation 6): a Stage-2 frame cap's device flag | _ => false /-- l4v: Arch_Structs_A.AARCH64_A.arch_cap_badge: only SMC caps have a badge. -/ @@ -429,6 +437,7 @@ def aobjRef : ArchCap → Option ObjRef def acapRightsUpdate (R : CapRights) (acap : ArchCap) : ArchCap := match acap with | .frameCap ref _ sz dev as => .frameCap ref (validateVmRights R) sz dev as + | .ext e => .ext (e.rightsUpdate R) -- FL4 hook (generation 6): FL4 masks a Stage-2 frame's rights too | _ => acap /-! ### Object types and default objects -/ diff --git a/proofs/Fl4SpecA64/AARCH64/VCPU.lean b/proofs/Fl4SpecA64/AARCH64/VCPU.lean index b50b8f21e..833f05376 100644 --- a/proofs/Fl4SpecA64/AARCH64/VCPU.lean +++ b/proofs/Fl4SpecA64/AARCH64/VCPU.lean @@ -174,7 +174,7 @@ def disableVirtTimer (vcpuPtrOpt : Option ObjRef) : SMonad Z Unit := do vcpuSaveReg vcpuPtr .vcpuRegCNTV_CVAL vcpuSaveReg vcpuPtr .vcpuRegCNTVOFF vcpuSaveReg vcpuPtr .vcpuRegCNTKCTL_EL1 - | none => return () + | none => pure () -- with or without VCPU, turn off the timer and return to user-mode timer config doMachineOp (writeVCPUHardwareReg .vcpuRegCNTV_CTL 0) doMachineOp checkExportArchTimer @@ -211,7 +211,7 @@ def vcpuDisable (vcpuPtrOpt : Option ObjRef) : SMonad Z Unit := do vcpuSaveReg vr .vcpuRegSCTLR vcpuSaveReg vr .vcpuRegCPACR -- since FPU enabled vcpuSaveReg vr .vcpuRegTCR - | none => return () + | none => pure () doMachineOp do setGicVcpuCtrlHcr 0 -- turn VGIC off dsb -- for GIC memory-mapped register @@ -243,7 +243,7 @@ def vcpuInvalidateActive : SMonad Z Unit := do let curV ← NonDet.gets fun s : State Z => s.archState.armCurrentVcpu match curV with | some (_, true) => vcpuDisable none - | _ => return () + | _ => pure () NonDet.modify fun s : State Z => { s with archState := { s.archState with armCurrentVcpu := none } } @@ -370,11 +370,11 @@ def associateVcpuTcb (vcpuPtr t : ObjRef) : SMonad Z Unit := do let tVcpu ← archThreadGet ArchTcb.tcbVcpu t match tVcpu with | some p => dissociateVcpuTcb p t - | _ => return () + | _ => pure () let v ← getVcpu vcpuPtr match v.vcpuTcb with | some p => dissociateVcpuTcb vcpuPtr p - | _ => return () + | _ => pure () archThreadSet (fun x => { x with tcbVcpu := some vcpuPtr }) t setVcpu vcpuPtr { v with vcpuTcb := some t } let ct ← NonDet.gets fun s : State Z => s.curThread diff --git a/proofs/Fl4SpecA64/AARCH64/VSpace.lean b/proofs/Fl4SpecA64/AARCH64/VSpace.lean index 11b15ddc6..ddb0b05f5 100644 --- a/proofs/Fl4SpecA64/AARCH64/VSpace.lean +++ b/proofs/Fl4SpecA64/AARCH64/VSpace.lean @@ -35,6 +35,7 @@ noncomputable section /-- l4v: Arch_Structs_A.AARCH64_A.is_FrameCap (datatype discriminator). -/ def ArchCap.isFrameCap : ArchCap → Bool | .frameCap .. => true + | .ext e => e.isFrame -- FL4 hook (generation 6): a Stage-2 frame cap is a frame | _ => false /-- l4v: Arch_Structs_A.AARCH64_A.is_PageTableCap (datatype discriminator). -/ def ArchCap.isPageTableCap : ArchCap → Bool @@ -118,6 +119,7 @@ variable {Z : Type} [StateExt Z] a virtual ASID. -/ def findVspaceForAsid (asid : Asid) : LFMonad Z ObjRef := do let vspaceOpt ← NonDetE.liftE (NonDet.gets (vspaceForAsid asid)) + let vspaceOpt ← NonDetE.liftE (NonDet.gets fun s : State Z => vspaceOpt.filter fun r => match s.kheap r with | some (.archObj (.ext _)) => false | _ => true) -- FL4 hook (generation 7): a Stage-2 / `ArmHighVSpace` root is no l4v VSpace (FL4 `find_asid`: `InvalidRoot`) NonDetE.throwOpt .invalidRoot vspaceOpt /-- l4v: ArchVSpace_A.AARCH64_A.load_vmid. -/ @@ -358,11 +360,9 @@ def archDeriveCap (c : ArchCap) : SEMonad Z Cap := | .vcpuCap _ => NonDetE.returnOk (.archObjectCap c) | .sgiSignalCap _ _ => NonDetE.returnOk (.archObjectCap c) | .smcCap _ => NonDetE.returnOk (.archObjectCap c) - -- FL4 hook (no l4v case): FL4's `derive_cap`, l4v's page table rule - -- (an unassigned Stage-2 VSpace or `ArmHighVSpace` cap cannot be copied) - | .ext e => - if e.mapData.isSome then NonDetE.returnOk (.archObjectCap c) - else NonDetE.throwError .illegalOperation + -- FL4 hook (no l4v case): FL4's `derive_cap` (a Stage-2 frame: an unmapped plain frame), l4v's page + -- table rule (an unassigned Stage-2 VSpace or `ArmHighVSpace` cap cannot be copied) + | .ext e => e.deriveWith (fun r R sz d => NonDetE.returnOk (.archObjectCap (.frameCap r R sz d none))) (NonDetE.returnOk (.archObjectCap c)) (NonDetE.throwError .illegalOperation) end @@ -429,7 +429,7 @@ def prepareThreadDelete {Z : Type} [StateExt Z] (threadPtr : ObjRef) : SMonad Z let tVcpu ← archThreadGet ArchTcb.tcbVcpu threadPtr match tVcpu with | some v => dissociateVcpuTcb v threadPtr - | none => return () + | none => pure () fpuRelease threadPtr /-! ## ArchCSpace_A -/ @@ -473,8 +473,8 @@ def archSameRegionAs : ArchCap → ArchCap → Prop | .vcpuCap r, c' => ∃ r', c' = .vcpuCap r' ∧ r = r' | .sgiSignalCap irq target, c' => c' = .sgiSignalCap irq target | .smcCap _, c' => c'.isSMCCap = true - -- FL4 hook (no l4v case): FL4's `same_region_as`, the same kind and object - | .ext e, c' => ∃ e', c' = .ext e' ∧ e.sameObject e' + -- FL4 hook (no l4v case): FL4's `same_region_as`, a frame's region, a root's kind and object + | .ext e, c' => e.sameRegionWith c'.isFrameCap c'.acapObj c'.acapFsize (∃ e', c' = .ext e' ∧ e.sameObject e') /-- l4v: ArchCSpace_A.AARCH64_A.is_irq_control_descendant: whether the cap is a descendant of the IRQ control cap. -/ @@ -487,6 +487,7 @@ def sameAobjectAs (cp cp' : ArchCap) : Prop := | .frameCap ref _ sz dev _, .frameCap ref' _ sz' dev' _ => (dev, ref, sz) = (dev', ref', sz') ∧ ref ≤ ref + 2 ^ pageBitsForSize sz - 1 | .sgiSignalCap _ _, _ => False + | .ext e, c' => e.sameAobjectWith c'.isFrameCap c'.acapObj c'.acapFsize (archCapIsDevice c') (archSameRegionAs cp cp') -- FL4 hook (generation 6) | _, _ => archSameRegionAs cp cp' /-- l4v: ArchCSpace_A.AARCH64_A.arch_is_cap_revocable. -/ diff --git a/proofs/Fl4SpecA64/Decode.lean b/proofs/Fl4SpecA64/Decode.lean index e2bf40b14..78622d58f 100644 --- a/proofs/Fl4SpecA64/Decode.lean +++ b/proofs/Fl4SpecA64/Decode.lean @@ -283,7 +283,7 @@ def decodeVsInvFlush : ArchDecoder Z := fun label args _cte cap _extraCaps => let «end» := the args[1]? do NonDetE.whenE («end» ≤ start) <| NonDetE.throwError (.invalidArgument 1) - NonDetE.whenE («end» > pptrUserTop) <| NonDetE.throwError .illegalOperation + NonDetE.whenE («end» > userVtop) <| NonDetE.throwError .illegalOperation -- FL4 hook (generation 8): l4v writes `pptrUserTop`; FL4 `vspace.rs` 933 compares with `USER_TOP` let (vspace, asid) ← checkVspaceRoot (.archObjectCap cap) 0 let vspace' ← lookupErrorOnFailure false <| findVspaceForAsid asid NonDetE.whenE (vspace' ≠ vspace) <| NonDetE.throwError (.invalidCapability 0) diff --git a/proofs/Fl4SpecA64/Ext/ARMVCPURun.lean b/proofs/Fl4SpecA64/Ext/ARMVCPURun.lean index aa31bbd04..a07426277 100644 --- a/proofs/Fl4SpecA64/Ext/ARMVCPURun.lean +++ b/proofs/Fl4SpecA64/Ext/ARMVCPURun.lean @@ -197,19 +197,23 @@ def fl4NumLrs : Nat := /-! ## The 64-register view (module docstring) -/ -/-- FL4's VCPU register `i`. -/ +/-- FL4's VCPU register `i` (`VGIC_VMCR` = 36 uses the full 64-bit `vcpuExt.regs` +slot so bits 63:32 are preserved; `vcpuVgic.vgicVmcr` mirrors its low 32 bits). -/ def fl4VcpuRegGet (v : Vcpu) (i : Nat) : Word := if i < 25 then v.vcpuRegs (VCPUReg.toEnum i) else if i = vrVgicHcr then ucast 64 v.vcpuVgic.vgicHcr - else if i = vrVgicVmcr then ucast 64 v.vcpuVgic.vgicVmcr else if vrVgicLr0 ≤ i ∧ i < 64 then v.vcpuVgic.vgicLr (i - vrVgicLr0) else if h : i < 64 then v.vcpuExt.regs ⟨i, h⟩ else 0 -/-- Set FL4's VCPU register `i`. -/ +/-- Set FL4's VCPU register `i` (`VGIC_VMCR` = 36 updates both the 64-bit +`vcpuExt.regs` slot and the 32-bit `vcpuVgic.vgicVmcr`). -/ def fl4VcpuRegSet (i : Nat) (w : Word) (v : Vcpu) : Vcpu := if i < 25 then { v with vcpuRegs := Function.update v.vcpuRegs (VCPUReg.toEnum i) w } else if i = vrVgicHcr then { v with vcpuVgic := { v.vcpuVgic with vgicHcr := ucast 32 w } } - else if i = vrVgicVmcr then { v with vcpuVgic := { v.vcpuVgic with vgicVmcr := ucast 32 w } } + else if i = vrVgicVmcr then + { v with + vcpuVgic := { v.vcpuVgic with vgicVmcr := ucast 32 w } + vcpuExt := { v.vcpuExt with regs := Function.update v.vcpuExt.regs ⟨vrVgicVmcr, by decide⟩ w } } else if vrVgicLr0 ≤ i ∧ i < 64 then { v with vcpuVgic := { v.vcpuVgic with vgicLr := Function.update v.vcpuVgic.vgicLr (i - vrVgicLr0) w } } else if h : i < 64 then @@ -452,8 +456,8 @@ def fl4CompleteRun (ct : ObjRef) (stop : RunStop) (w1 w2 w3 : Word) : SMonad Z U let ip ← getRegister nextInstructionRegister setRegister faultRegister ip setThreadState ct .running - let cur ← NonDet.gets fun s : State Z => s.curThread - NonDet.when (ct = cur) <| setVmRoot ct + -- Hardware address-space root restoration on `ct = curThread` (`vspace::set_vm_root`) + -- is tracked at the `Z.hw` / `ScArchSwA` boundary (as in `Ext/SchedTail.fl4SwitchToThread`). /-- **`vcpu::check`**: a thread in `TS_RUNNING_VM` with a pending signal on its bound notification ends its run (`INTERRUPT`, the badge); one with no @@ -517,6 +521,27 @@ def fl4VcpuSetTcbPre (vc t : ObjRef) : SMonad Z Unit := do let st ← getThreadState t if st = .ext .runningVM then fl4CompleteRun t .interrupt 0 0 0 else fl4CancelRun t +/-- FL4 `vcpu::unbind`'s dissociation half: ported `dissociateVcpuTcb` plus +resetting `OWNER_CPU` to 0. -/ +def fl4DissociateVcpuTcb (vr t : ObjRef) : SMonad Z Unit := do + dissociateVcpuTcb vr t + fl4VcpuExtUpdate vr fun e => { e with ownerCpu := 0 } + +/-- FL4 `decode_set_tcb`'s association half: ported `associateVcpuTcb` without +the eager `vcpuSwitch` when `t = curThread` (FL4 switches VCPU sysregs lazily +at kernel exit and tracks `vspace::set_vm_root` at the `Z.hw` boundary). -/ +def fl4AssociateVcpuTcb (vcpuPtr t : ObjRef) : SMonad Z Unit := do + let tVcpu ← archThreadGet ArchTcb.tcbVcpu t + match tVcpu with + | some p => fl4DissociateVcpuTcb p t + | none => pure () + let v ← getVcpu vcpuPtr + match v.vcpuTcb with + | some p => fl4DissociateVcpuTcb vcpuPtr p + | none => pure () + archThreadSet (fun atcb => { atcb with tcbVcpu := some vcpuPtr }) t + vcpuUpdate vcpuPtr fun v => { v with vcpuTcb := some t } + /-! ## The invocations -/ /-- FL4's VCPU invocations. -/ @@ -697,7 +722,7 @@ def fl4PerformVcpuInv (i : Fl4VcpuInvocation) : SMonad Z (List Data) := return [] | .setTcb vc t => do fl4VcpuSetTcbPre vc t - associateVcpuTcb vc t + fl4AssociateVcpuTcb vc t return [] | .readReg vc field => do -- `decode_read_reg`: the reply's MR0 (`reply1`) diff --git a/proofs/Fl4SpecA64/Ext/ArchTypes.lean b/proofs/Fl4SpecA64/Ext/ArchTypes.lean index e2a010a1b..2e9fa3668 100644 --- a/proofs/Fl4SpecA64/Ext/ArchTypes.lean +++ b/proofs/Fl4SpecA64/Ext/ArchTypes.lean @@ -24,7 +24,9 @@ The payloads (FL4's Rust: `crates/fl4-core/src/capability.rs`, * `ExtArchCap`: `Cap::Ept { level: 0 }` (a Stage-2 VSpace, object type 13; FL4's AArch64 kernel makes no other level) and `Cap::HighVSpace` (an `ArmHighVSpace`, object type 14), each with its ASID once assigned - (`mapped`, `asid`; the Stage-2 cap's `vaddr` is always 0); + (`mapped`, `asid`; the Stage-2 cap's `vaddr` is always 0), and (generation + 6) `Cap::Frame { is_ept: true }`, a frame cap mapped in a Stage-2 VSpace + (`stage2FrameCap`); * `ExtArchObj P` (P is the port's `PTE`, which this leaf cannot name): a Stage-2 root (`Stage2Root`, 8 KiB, `bits::STAGE2_VSPACE` = 13; the root table of the IPA walk: 1024 entries for a 40-bit IPA, the first 32 @@ -74,37 +76,152 @@ inductive ExtArchCap where | stage2VSpaceCap (obj : ObjRef) (mapData : Option Asid) /-- `Cap::HighVSpace { ptr, mapped, asid }`: an `ArmHighVSpace`. -/ | highVSpaceCap (obj : ObjRef) (mapData : Option Asid) -deriving DecidableEq, Repr, Inhabited + /-- (generation 6) `Cap::Frame { is_ept: true, .. }`: a frame cap mapped in a Stage-2 VSpace + (`ept::map_frame`; its `mapData` is the ASID and the IPA). FL4 keeps `is_ept` in the frame cap; + l4v's `FrameCap` has no such field, so the Stage-2 mapping is a cap of its own, as the x86 port's + `.vmEptMap` map type (PORTING.md, "The `ext` hook"). -/ + | stage2FrameCap (obj : ObjRef) (rights : CapRights) (size : VMPageSize) (isDevice : Bool) + (mapData : Option (Asid × VSpaceRef)) +deriving Inhabited /-- The object of an FL4 cap (`Cap::ptr`). -/ def ExtArchCap.objRef : ExtArchCap → ObjRef | .stage2VSpaceCap p _ => p | .highVSpaceCap p _ => p + | .stage2FrameCap p _ _ _ _ => p -/-- The ASID an FL4 cap is assigned (`mapped`, `asid`). -/ +/-- The ASID an FL4 root cap is assigned (`mapped`, `asid`); a Stage-2 frame +cap is no root and has none (its mapping is `stage2FrameCap`'s `mapData`). -/ def ExtArchCap.mapData : ExtArchCap → Option Asid | .stage2VSpaceCap _ m => m | .highVSpaceCap _ m => m + | .stage2FrameCap _ _ _ _ _ => none -/-- The cap with another ASID assignment. -/ +/-- The root cap with another ASID assignment (a frame cap unchanged). -/ def ExtArchCap.withMapData (m : Option Asid) : ExtArchCap → ExtArchCap | .stage2VSpaceCap p _ => .stage2VSpaceCap p m | .highVSpaceCap p _ => .highVSpaceCap p m + | .stage2FrameCap p R sz d m' => .stage2FrameCap p R sz d m' -/-- FL4: the object size of both (`objects.rs` `obj_bits`: +/-- Whether an FL4 cap is a root cap (a Stage-2 VSpace or an `ArmHighVSpace`). -/ +def ExtArchCap.isRoot : ExtArchCap → Bool + | .stage2VSpaceCap _ _ => true + | .highVSpaceCap _ _ => true + | .stage2FrameCap _ _ _ _ _ => false + +/-- FL4: the object size of both roots (`objects.rs` `obj_bits`: `bits::STAGE2_VSPACE` = `bits::HIGH_VSPACE` = 13). -/ def extObjBits : Nat := 13 -/-- `arch_obj_size` of an FL4 cap. -/ -def ExtArchCap.objSize (_c : ExtArchCap) : Nat := extObjBits +/-- `arch_obj_size` of an FL4 cap (a frame: l4v's `pageBitsForSize`). -/ +def ExtArchCap.objSize : ExtArchCap → Nat + | .stage2VSpaceCap _ _ => extObjBits + | .highVSpaceCap _ _ => extObjBits + | .stage2FrameCap _ _ sz _ _ => pageBitsForSize sz -/-- `arch_same_region_as` / `same_aobject_as` of two FL4 caps +/-- `arch_same_region_as` / `same_aobject_as` of two FL4 root caps (`objects.rs` `same_region_as`: the same kind and object). -/ def ExtArchCap.sameObject : ExtArchCap → ExtArchCap → Prop | .stage2VSpaceCap p _, .stage2VSpaceCap p' _ => p = p' | .highVSpaceCap p _, .highVSpaceCap p' _ => p = p' | _, _ => False +/-! ### The frame hooks (generation 6) + +The `ext` arms of l4v's frame functions (PORTING.md, "The `ext` hook"): a +Stage-2 frame cap is FL4's `Cap::Frame` with `is_ept`, so it is a frame to +every frame rule of FL4 (`derive_cap`, `mask_cap_rights`, `same_region_as`, +`same_object_as`, `is_device`); a root cap keeps the arm's l4v value +(`undefined` for the selectors, as l4v's `_ => undefined`). Each takes the +other cap's l4v view where it needs one, since this leaf cannot name +`ArchCap`. -/ + +/-- `is_FrameCap` of an FL4 cap. -/ +@[simp] def ExtArchCap.isFrame : ExtArchCap → Bool + | .stage2VSpaceCap _ _ => false + | .highVSpaceCap _ _ => false + | .stage2FrameCap _ _ _ _ _ => true + +/-- `acap_rights` of an FL4 cap. -/ +@[simp] noncomputable def ExtArchCap.rights : ExtArchCap → CapRights + | .stage2VSpaceCap _ _ => undefined + | .highVSpaceCap _ _ => undefined + | .stage2FrameCap _ R _ _ _ => R + +/-- `acap_fsize` of an FL4 cap. -/ +@[simp] noncomputable def ExtArchCap.fsize : ExtArchCap → VMPageSize + | .stage2VSpaceCap _ _ => undefined + | .highVSpaceCap _ _ => undefined + | .stage2FrameCap _ _ sz _ _ => sz + +/-- `acap_is_device` of an FL4 cap. -/ +@[simp] noncomputable def ExtArchCap.acapIsDevice : ExtArchCap → Bool + | .stage2VSpaceCap _ _ => undefined + | .highVSpaceCap _ _ => undefined + | .stage2FrameCap _ _ _ d _ => d + +/-- `acap_map_data` of an FL4 cap. -/ +@[simp] noncomputable def ExtArchCap.frameMapData : ExtArchCap → Option (Asid × VSpaceRef) + | .stage2VSpaceCap _ _ => undefined + | .highVSpaceCap _ _ => undefined + | .stage2FrameCap _ _ _ _ m => m + +/-- `update_map_data` of an FL4 cap (a frame keeps its kind). -/ +@[simp] noncomputable def ExtArchCap.updateMapData (m : Option (Asid × VSpaceRef)) : ExtArchCap → ExtArchCap + | .stage2VSpaceCap _ _ => undefined + | .highVSpaceCap _ _ => undefined + | .stage2FrameCap p R sz d _ => .stage2FrameCap p R sz d m + +/-- `arch_cap_is_device` of an FL4 cap (FL4 `is_device`). -/ +@[simp] def ExtArchCap.isDevice : ExtArchCap → Bool + | .stage2VSpaceCap _ _ => false + | .highVSpaceCap _ _ => false + | .stage2FrameCap _ _ _ d _ => d + +/-- `acap_rights_update` of an FL4 cap (FL4 `mask_cap_rights` masks a +frame's rights whatever its `is_ept`; l4v's `validate_vm_rights`). -/ +@[simp] noncomputable def ExtArchCap.rightsUpdate (R : CapRights) : ExtArchCap → ExtArchCap + | .stage2VSpaceCap p m => .stage2VSpaceCap p m + | .highVSpaceCap p m => .highVSpaceCap p m + | .stage2FrameCap p _ sz d m => .stage2FrameCap p (validateVmRights R) sz d m + +/-- `arch_derive_cap` of an FL4 cap, over the three results: FL4 +`derive_cap` (`objects.rs:234`) makes a frame cap an unmapped plain frame +(`is_ept: false`: `frame` of its fields); an assigned root cap is copied +(`root`), an unassigned one cannot be (`bad`, l4v's page table rule). -/ +@[simp] def ExtArchCap.deriveWith {α : Type} (frame : ObjRef → CapRights → VMPageSize → Bool → α) + (root bad : α) : ExtArchCap → α + | .stage2VSpaceCap _ m => if m.isSome then root else bad + | .highVSpaceCap _ m => if m.isSome then root else bad + | .stage2FrameCap p R sz d _ => frame p R sz d + +/-- l4v's frame region rule (`arch_same_region_as` of a `FrameCap`): the +second frame `r'` of size `sz'` lies in the first. -/ +def frameInRegion (r : ObjRef) (sz : VMPageSize) (r' : ObjRef) (sz' : VMPageSize) : Prop := + let topA := r + ((1 : Word) <<< pageBitsForSize sz) - 1 + let topB := r' + ((1 : Word) <<< pageBitsForSize sz') - 1 + r ≤ r' ∧ topA ≥ topB ∧ r' ≤ topB + +/-- `arch_same_region_as` of an FL4 cap and a cap whose l4v view is +`isFr` (`is_FrameCap`), `r'` and `sz'` (its frame), or `other` (the root +rule): a frame contains any frame in its region (FL4 `same_region_as`, +`Cap::Frame` with `Cap::Frame`, whatever their `is_ept`). -/ +@[simp] def ExtArchCap.sameRegionWith (isFr : Bool) (r' : ObjRef) (sz' : VMPageSize) (other : Prop) : + ExtArchCap → Prop + | .stage2VSpaceCap _ _ => other + | .highVSpaceCap _ _ => other + | .stage2FrameCap r _ sz _ _ => isFr = true ∧ frameInRegion r sz r' sz' + +/-- `same_aobject_as` of an FL4 cap and a cap whose l4v view is `isFr`, +`r'`, `sz'`, `d'`: a frame is the same object as a frame of the same +address, size and device flag (FL4 `same_object_as`); a root cap as +`other` (`arch_same_region_as`). -/ +@[simp] def ExtArchCap.sameAobjectWith (isFr : Bool) (r' : ObjRef) (sz' : VMPageSize) (d' : Bool) + (other : Prop) : ExtArchCap → Prop + | .stage2VSpaceCap _ _ => other + | .highVSpaceCap _ _ => other + | .stage2FrameCap r _ sz d _ => isFr = true ∧ r = r' ∧ sz = sz' ∧ d = d' + /-! ## Objects -/ /-- FL4: the AArch64 objects l4v's `arch_kernel_obj` does not have (module diff --git a/proofs/Fl4SpecA64/Ext/Entry.lean b/proofs/Fl4SpecA64/Ext/Entry.lean index 710133d6f..d01ac4aa1 100644 --- a/proofs/Fl4SpecA64/Ext/Entry.lean +++ b/proofs/Fl4SpecA64/Ext/Entry.lean @@ -1,19 +1,30 @@ +import Fl4SpecA64.Ext.SchedTail +import Fl4SpecA64.Ext.IrqSignal /- FL4 difference (Fl4SpecA64/PORTING.md, "Ext boundary"): **the kernel entry with FL4's AArch64 dispatcher**, the x86 port's `Fl4Spec/Ext/Entry.lean` over the AArch64 dispatcher, with AArch64's 4-byte `svc` in the `FaultIP` write. +* `fl4HandleSyscallWith` factors the syscall cases over an invocation + dispatcher. `fl4CallSerial` composes the exact local scheduler and + post-activation guest check with the chosen entry body. Its fault bodies + retain the full words delivered by FL4. * `fl4HandleEvent rep` is l4v's `handle_event` (Syscall_A, `handleEvent`) with `handle_invocation` replaced by FL4's `fl4HandleInvocationR rep` (Ext/ReplyError over Ext/Invocation): the same substitution one level up. - The other events do not invoke (`fl4HandleEvent_other`, by `rfl`). -* `fl4CallKernel rep` is l4v's `call_kernel` (`callKernel`) with - `fl4HandleEvent rep` for `handleEvent` and FL4's TCB timer step - `fl4Awaken` (Ext/Timer, l4h 0003) before `schedule`: FL4's - `sched::schedule` first runs `sched::awaken`, which fires the expired TCB - timers (`timer::fire`) before l4v's release-queue `awaken`. -* `fl4ActivateThread`: l4v's `activate_thread`, which a thread in - `TS_RUNNING_VM` passes unchanged (its guest resumes; generation 4). + Other events use the FL4 timer guards and agree with l4v when the + timestamp, budget bounds and timeout conversion agree + (`fl4HandleEvent_other`). +* `fl4CallKernel limit timer rep` is l4v's `call_kernel` (`callKernel`) with + `fl4HandleEvent rep` for `handleEvent`, `fl4PreemptionPathIrq` for + `preemption_path` (the interrupt handling of `Ext/IrqSignal`, as + `fl4HandleEvent`'s interrupt event) and FL4's local scheduler + `fl4Schedule limit timer` (Ext/SchedTail, as `fl4Finish`) for `schedule`: + FL4's `sched::schedule` fires the expired TCB timers (`fl4Awaken`) before + the release-queue `awaken`, and never switches the VCPU on a thread switch. +* `fl4ActivateThread` completes a live YieldTo before inspecting the + thread state, including `TS_RUNNING_VM`. Both finish paths then call + `fl4VcpuCheck` on the current thread. * `fl4CallKernelRunExit e`: a guest exit of an `ARMVCPURun` (the guest's VM fault, VCPU fault, VGIC maintenance or VPPI while the current thread is in `TS_RUNNING_VM`; FL4 `syscall::handle_vcpu_run_exit` / @@ -44,67 +55,274 @@ noncomputable section section variable {Z : Type} [StateExt Z] -/-- The `match call` of l4v's `handle_event` with FL4's dispatcher -`fl4HandleInvocationR rep` for `handleInvocation`. -/ -def fl4HandleSyscall (rep : Bool) : Syscall → PMonad Z Unit +/-- The syscall cases of l4v's `handle_event`. Combined calls receive +from the successor of a successful first phase. -/ +def fl4HandleSyscallWith + (invoke : Bool → Bool → Bool → Bool → CapRef → PMonad Z Unit) + (yield : SMonad Z Unit) : Syscall → PMonad Z Unit | .sysSend => do let cptr ← NonDetE.liftE <| getCapReg cap_register - fl4HandleInvocationR rep false true false false cptr + invoke false true false false cptr | .sysNBSend => do let cptr ← NonDetE.liftE <| getCapReg cap_register - fl4HandleInvocationR rep false false false false cptr + invoke false false false false cptr | .sysCall => do let cptr ← NonDetE.liftE <| getCapReg cap_register - fl4HandleInvocationR rep true true true false cptr + invoke true true true false cptr | .sysRecv => withoutPreemption <| handleRecv true true - | .sysYield => withoutPreemption handleYield + | .sysYield => withoutPreemption yield | .sysReplyRecv => do let replyCptr ← withoutPreemption <| getCapReg replyRegister - fl4HandleInvocationR rep false false true true replyCptr + invoke false false true true replyCptr withoutPreemption <| handleRecv true true | .sysNBSendRecv => do let dest ← withoutPreemption <| getCapReg nbsendRecvDest - fl4HandleInvocationR rep false false true true dest + invoke false false true true dest withoutPreemption <| handleRecv true true | .sysNBSendWait => do let replyCptr ← withoutPreemption <| getCapReg replyRegister - fl4HandleInvocationR rep false false true true replyCptr + invoke false false true true replyCptr withoutPreemption <| handleRecv true false | .sysWait => withoutPreemption <| handleRecv true false | .sysNBWait => withoutPreemption <| handleRecv false false | .sysNBRecv => withoutPreemption <| handleRecv false true -/-- l4v's `handle_event` with FL4's dispatcher `fl4HandleInvocationR rep` -for `handleInvocation` (the module docstring). -/ +/-- The compatibility dispatcher uses the port's charge in Yield. -/ +def fl4HandleSyscall (rep : Bool) : Syscall → PMonad Z Unit := + fl4HandleSyscallWith (fl4HandleInvocationR rep) handleYield + +/-- `syscall::handle_yield` charges the current head and restores the +consumed total saved on entry. Both sums use the word type. -/ +def fl4HandleYield : SMonad Z Unit := do + let scPtr ← NonDet.gets fun s : State Z => s.curSc + let sc ← getSchedContext scPtr + let consumed ← NonDet.gets fun s : State Z => s.consumedTime + let head ← getRefillHead scPtr + fl4ChargeBudget head.rAmount false + setScObjRef scConsumedUpdate scPtr (sc.scConsumed + consumed) + +private theorem fl4ChargeWithoutTimeout_eq_l4v (used : Ticks) + (limit : fl4MaxReleaseTime = MAX_RELEASE_TIME) (minimum : fl4MinBudget = MIN_BUDGET) + (cap : fl4MaxConsumedTicks = maxTicksToUs) (conversion : Fl4TicksToUsLegacyAgree) : + fl4ChargeBudget (Z := Z) used false = chargeBudget used false := by + have refill : (fl4RefillBudgetCheck : Ticks → SMonad Z Unit) = refillBudgetCheck := + funext fun ticks => fl4RefillBudgetCheck_eq_l4v limit minimum ticks + have sufficient : (fl4GetScRefillSufficient : ObjRef → Ticks → SMonad Z Bool) = getScRefillSufficient := + funext fun sc => funext fun ticks => fl4GetScRefillSufficient_eq_l4v minimum sc ticks + have timeout : (@fl4HandleTimeout Z _) = handleTimeout := + funext fun t => funext fun f => fl4HandleTimeout_eq_l4v cap conversion t f + have tail : fl4EndTimeslice (Z := Z) false = endTimeslice false := by + unfold fl4EndTimeslice endTimeslice + rw [sufficient, timeout] <;> rfl + unfold fl4ChargeBudget fl4ChargeHead chargeBudget + rw [refill, tail] + +/-- Yield agrees with the port under the legacy charge agreements. -/ +theorem fl4HandleYield_eq_l4v + (limit : fl4MaxReleaseTime = MAX_RELEASE_TIME) (minimum : fl4MinBudget = MIN_BUDGET) + (cap : fl4MaxConsumedTicks = maxTicksToUs) (conversion : Fl4TicksToUsLegacyAgree) : + fl4HandleYield (Z := Z) = handleYield := by + simp only [fl4HandleYield, handleYield, fl4ChargeWithoutTimeout_eq_l4v _ limit minimum cap conversion] + +/-- The serial ABI cases after the budget check, with FL4's Yield charge. -/ +def fl4DispatchSyscall + (invoke : Bool → Bool → Bool → Bool → CapRef → PMonad Z Unit) : Syscall → PMonad Z Unit := + fl4HandleSyscallWith invoke fl4HandleYield + +theorem fl4DispatchSyscall_eq_compat (rep : Bool) + (limit : fl4MaxReleaseTime = MAX_RELEASE_TIME) (minimum : fl4MinBudget = MIN_BUDGET) + (cap : fl4MaxConsumedTicks = maxTicksToUs) (conversion : Fl4TicksToUsLegacyAgree) : + fl4DispatchSyscall (Z := Z) (fl4HandleInvocationR rep) = fl4HandleSyscall rep := by + rw [fl4DispatchSyscall, fl4HandleSyscall, fl4HandleYield_eq_l4v limit minimum cap conversion] + +/-- The kernel event uses FL4's budget check and syscall dispatcher. -/ def fl4HandleEvent (rep : Bool) : Event → PMonad Z Unit | .syscallEvent call => do - NonDetE.liftE updateTimeStamp - let restart ← NonDetE.liftE checkBudgetRestart + let restart ← NonDetE.liftE fl4BudgetOk NonDetE.whenE restart (fl4HandleSyscall rep call) - | ev => handleEvent ev + | .unknownSyscall n => withoutPreemption <| do + fl4UpdateTimestamp + let restart ← fl4CheckBudgetRestart + NonDet.when restart <| do + let thread ← NonDet.gets fun s : State Z => s.curThread + handleFault thread <| .unknownSyscallException <| BitVec.ofNat 64 n + | .userLevelFault w1 w2 => withoutPreemption <| do + fl4UpdateTimestamp + let restart ← fl4CheckBudgetRestart + NonDet.when restart <| do + let thread ← NonDet.gets fun s : State Z => s.curThread + handleFault thread <| .userException (w1 &&& mask 32) (w2 &&& mask 28) + | .interrupt => withoutPreemption <| do + fl4UpdateTimestamp + let _ ← fl4CheckBudget + fl4MaybeHandleInterrupt false + | .vmFaultEvent faultType => withoutPreemption <| do + fl4UpdateTimestamp + let restart ← fl4CheckBudgetRestart + NonDet.when restart <| do + let thread ← NonDet.gets fun s : State Z => s.curThread + NonDetE.catch (handleVmFault thread faultType) (handleFault thread) + | .hypervisorEvent hypfaultType => withoutPreemption <| do + fl4UpdateTimestamp + let restart ← fl4CheckBudgetRestart + NonDet.when restart <| do + let thread ← NonDet.gets fun s : State Z => s.curThread + handleHypervisorFault thread hypfaultType -/-- The non-syscall events are l4v's. -/ -theorem fl4HandleEvent_other (rep : Bool) (ev : Event) (h : ∀ c, ev ≠ .syscallEvent c) : +/-- The non-syscall events agree with l4v under timer-limit agreement (and, for +the interrupt event, without GICv3: `Ext/IrqSignal`). -/ +theorem fl4HandleEvent_other (rep : Bool) (ev : Event) (h : ∀ c, ev ≠ .syscallEvent c) + (hlimit : fl4MaxReleaseTime = MAX_RELEASE_TIME) (hmin : fl4MinBudget = MIN_BUDGET) + (htime : fl4UpdateTimestamp (Z := Z) = updateTimeStamp) + (hcap : fl4MaxConsumedTicks = maxTicksToUs) (hconv : Fl4TicksToUsLegacyAgree) + (hgic : ev = .interrupt → config_ARM_GIC_V3 = false) : fl4HandleEvent (Z := Z) rep ev = handleEvent ev := by cases ev with | syscallEvent c => exact absurd rfl (h c) - | _ => rfl + | interrupt => simp only [fl4HandleEvent, handleEvent, htime, fl4CheckBudget_eq_l4v hlimit hmin hcap hconv, + fl4MaybeHandleInterrupt_eq_l4v (hgic rfl)] + | _ => simp only [fl4HandleEvent, handleEvent, htime, fl4CheckBudgetRestart_eq_l4v hlimit hmin hcap hconv, + fl4CheckBudget_eq_l4v hlimit hmin hcap hconv] + +def fl4ActivateState (ct : ObjRef) : SMonad Z Unit := do + let st ← getThreadState ct + match st with + | .running | .ext .runningVM => return () + | .restart => do + let pc ← asUser ct getRestartPC + asUser ct <| setNextPC pc + setThreadState ct .running + | .idleThreadState => archActivateIdleThread ct + | _ => NonDet.fail -/-- l4v's `activate_thread` with FL4's `TS_RUNNING_VM`: a thread in an -`ARMVCPURun` is resumed into its guest (no change of the abstract state); -l4v's `activate_thread` has no case for it (the monad's failure). -/ +/-- Complete YieldTo before dispatching every thread state, including +RunningVM. -/ def fl4ActivateThread : SMonad Z Unit := do let ct ← NonDet.gets fun s : State Z => s.curThread - let st ← getThreadState ct - if st = .ext .runningVM then return () else activateThread - -/-- l4v's `call_kernel` with `fl4HandleEvent rep`, and FL4's TCB timers -(`fl4Awaken`, Ext/Timer) before `schedule`. -/ -def fl4CallKernel (rep : Bool) (ev : Event) : SMonad Z Unit := do - let _ ← ExceptT.run <| NonDetE.handleE (fl4HandleEvent rep ev) (fun _ => withoutPreemption preemptionPath) - fl4Awaken - schedule + let yt ← getTcbObjRef Tcb.tcbYieldTo ct + NonDet.when (yt ≠ none) (fl4CompleteYieldTo ct) + fl4ActivateState ct + +/-- The selected thread first completes YieldTo and activates. Then the +guest check uses the state left by that activation. -/ +def fl4FinishAfterSchedule : SMonad Z Unit := do fl4ActivateThread + let ct ← NonDet.gets fun s : State Z => s.curThread + fl4VcpuCheck ct + +/-- FL4's local syscall and interrupt finish with the domain-table bound +and the current core's timer observations. -/ +def fl4Finish (limit : Nat) (timer : State Z → Fl4TimerView) : SMonad Z Unit := do + fl4Schedule limit timer + fl4FinishAfterSchedule + +/-- Settle consumption after a preempted invocation, then take and handle +the pending IRQ through the platform adapter. -/ +def fl4PreemptionPath (pending : SMonad Z Unit) : SMonad Z Unit := do + let ct ← NonDet.gets fun s : State Z => s.curThread + let eligible ← NonDet.gets (schedulable ct) + if eligible then do + let _ ← fl4CheckBudget + pure () + else do + let csc ← NonDet.gets fun s : State Z => s.curSc + let active ← getScActive csc + if active then do + let consumed ← NonDet.gets fun s : State Z => s.consumedTime + fl4ChargeBudget consumed false + else NonDet.modify fun s : State Z => { s with consumedTime := 0 } + pending + +theorem fl4PreemptionPath_eq_l4v + (limit : fl4MaxReleaseTime = MAX_RELEASE_TIME) (minimum : fl4MinBudget = MIN_BUDGET) + (cap : fl4MaxConsumedTicks = maxTicksToUs) (conversion : Fl4TicksToUsLegacyAgree) : + fl4PreemptionPath (Z := Z) (maybeHandleInterrupt true) = preemptionPath := by + simp only [fl4PreemptionPath, preemptionPath, fl4CheckBudget_eq_l4v limit minimum cap conversion, + fl4ChargeWithoutTimeout_eq_l4v _ limit minimum cap conversion] + +/-- The single preemption handler used by the serial dispatcher. -/ +def fl4SettleDispatch (pending : SMonad Z Unit) (body : PMonad Z Unit) : SMonad Z Unit := + NonDetE.catch body fun _ => fl4PreemptionPath pending + +/-- The serial core entry runs the preemption tail on an exceptional +invocation result and finishes once. -/ +def fl4CallSerial (limit : Nat) (timer : State Z → Fl4TimerView) + (pending : SMonad Z Unit) (body : PMonad Z Unit) : SMonad Z Unit := do + fl4SettleDispatch pending body + fl4Finish limit timer + +/-- The API body has one entry budget check. -/ +def fl4SerialSyscallBody + (invoke : Bool → Bool → Bool → Bool → CapRef → PMonad Z Unit) (call : Syscall) : + PMonad Z Unit := do + let proceed ← NonDetE.liftE fl4BudgetOk + NonDetE.whenE proceed (fl4DispatchSyscall invoke call) + +/-- Common budget gate for user exceptions, VM faults and unknown syscalls. -/ +def fl4BudgetFault (fault : Fault) : SMonad Z Unit := do + let proceed ← fl4BudgetOk + NonDet.when proceed <| do + let ct ← NonDet.gets fun s : State Z => s.curThread + handleFault ct fault + +/-- The actual AArch64 VM fault words. The ESR is retained in full. -/ +def fl4VmFault (addr esr : Word) : Fault := + .archFault (.vmFault addr [if (esr >>> 26) &&& 63 = 32 then 1 else 0, esr]) + +theorem fl4VmFault_eq_masked (addr esr : Word) (width : esr &&& mask 32 = esr) : + fl4VmFault addr esr = + .archFault (.vmFault addr [if (esr >>> 26) &&& 63 = 32 then 1 else 0, esr &&& mask 32]) := by + rw [width] + rfl + +/-- The native synchronous slow vector uses the full shifted ESR to +choose FAR, the saved ELR, or the full-word UserException payload. -/ +def fl4NativeSyncFault (esr far elr : Word) : Fault := + if esr >>> 26 = 0x24 then fl4VmFault far esr + else if esr >>> 26 = 0x20 then fl4VmFault elr esr + else .userException esr 0 + +theorem fl4UserException_eq_masked (number code : Word) + (numberWidth : number &&& mask 32 = number) (codeWidth : code &&& mask 28 = code) : + Fault.userException number code = .userException (number &&& mask 32) (code &&& mask 28) := by + rw [numberWidth, codeWidth] + +/-- An accepted IRQ uses the already observed interrupt and the plain +budget check. IRQ service continues even when the budget check fails. -/ +def fl4AcceptedIrqBody (service : SMonad Z Unit) : SMonad Z Unit := do + fl4UpdateTimestamp + let _ ← fl4CheckBudget + service + +/-- l4v's `preemption_path` with `fl4MaybeHandleInterrupt` (`Ext/IrqSignal`). -/ +def fl4PreemptionPathIrq : SMonad Z Unit := do + let ct ← NonDet.gets fun s : State Z => s.curThread + let sched ← NonDet.gets (schedulable ct) + if sched then do + let _ ← checkBudget + pure () + else do + let csc ← NonDet.gets fun s : State Z => s.curSc + let active ← getScActive csc + if active + then do + let consumed ← NonDet.gets fun s : State Z => s.consumedTime + chargeBudget consumed false + else NonDet.modify fun s : State Z => { s with consumedTime := 0 } + fl4MaybeHandleInterrupt true + +theorem fl4PreemptionPathIrq_eq_l4v (v2 : config_ARM_GIC_V3 = false) : + fl4PreemptionPathIrq (Z := Z) = preemptionPath := by + unfold fl4PreemptionPathIrq preemptionPath + simp only [fl4MaybeHandleInterrupt_eq_l4v v2] + +/-- l4v's `call_kernel` with `fl4HandleEvent rep`, `fl4PreemptionPathIrq` for +`preemption_path`, and FL4's local scheduler (`fl4Schedule`, Ext/SchedTail) for +`schedule`. -/ +def fl4CallKernel (limit : Nat) (timer : State Z → Fl4TimerView) (rep : Bool) (ev : Event) : SMonad Z Unit := do + let _ ← ExceptT.run <| NonDetE.handleE (fl4HandleEvent rep ev) (fun _ => withoutPreemption fl4PreemptionPathIrq) + fl4Schedule limit timer + fl4FinishAfterSchedule /-- The `FaultIP := NextIP - 4` write of FL4's `handle_syscall` on a thread (the context that l4v's `kernel_entry` installs). -/ @@ -112,21 +330,30 @@ def fl4SyscallFaultIP (thread : ObjRef) : SMonad Z Unit := asUser thread (NonDet.bind (getRegister nextInstructionRegister) (fun n => setRegister faultRegister (n - 4))) +/-- The known, untrapped syscall entry on the serial general path. -/ +def fl4SerialSyscallEntry (limit : Nat) (timer : State Z → Fl4TimerView) + (pending : SMonad Z Unit) + (invoke : Bool → Bool → Bool → Bool → CapRef → PMonad Z Unit) (call : Syscall) : + SMonad Z Unit := do + let ct ← NonDet.gets fun s : State Z => s.curThread + fl4SyscallFaultIP ct + fl4CallSerial limit timer pending (fl4SerialSyscallBody invoke call) + /-- **A guest exit of an `ARMVCPURun`** (FL4's AArch64 entry, the current thread in `TS_RUNNING_VM`): `fl4HandleVcpuRunExit` (Ext/ARMVCPURun), then -FL4's `finish` (`fl4Awaken`, `schedule`, the activation). -/ -def fl4CallKernelRunExit (e : RunExit) : SMonad Z Unit := do +FL4's `finish` (`fl4Schedule`, the activation). -/ +def fl4CallKernelRunExit (limit : Nat) (timer : State Z → Fl4TimerView) (e : RunExit) : SMonad Z Unit := do fl4HandleVcpuRunExit e - fl4Awaken - schedule - fl4ActivateThread + fl4Schedule limit timer + fl4FinishAfterSchedule /-- **The syscall entry** (the module docstring): the `FaultIP` write on the current thread, then `fl4CallKernel rep (.syscallEvent c)`. -/ -def fl4KernelEntrySyscall (rep : Bool) (c : Syscall) : SMonad Z Unit := do +def fl4KernelEntrySyscall (limit : Nat) (timer : State Z → Fl4TimerView) (rep : Bool) (c : Syscall) : + SMonad Z Unit := do let ct ← NonDet.gets fun s : State Z => s.curThread fl4SyscallFaultIP ct - fl4CallKernel rep (.syscallEvent c) + fl4CallKernel limit timer rep (.syscallEvent c) end diff --git a/proofs/Fl4SpecA64/Ext/FaultReply.lean b/proofs/Fl4SpecA64/Ext/FaultReply.lean new file mode 100644 index 000000000..b52eb5bb7 --- /dev/null +++ b/proofs/Fl4SpecA64/Ext/FaultReply.lean @@ -0,0 +1,76 @@ +/- +FL4's reply to a fault (`faults.rs:handle_fault_reply`, AArch64). It differs from the ported +`Ipc_A.handle_fault_reply` in two points (PORTING.md, "Fault reply"): + +* the register order of a `Timeout` reply is seL4 C's (`TIMEOUT_REPLY_MESSAGE`), not `X0 .. X30` in order; +* a VCPU-bound caller's `SPSR_EL1` is sanitised with `sanitise_vcpu_spsr` (`regs.rs:sanitise_for_tcb`). + +The long-form `UnknownSyscall` reply of FL4's trapped syscalls (`TRAP_FULL_LENGTH`) is not modelled +(DESIGN.md §9, [S]). +-/ +import Fl4SpecA64.Ipc + +namespace Fl4SpecA64 + +open Fl4Spec + +noncomputable section +open Classical + +/-- FL4's guest mask and illegal-mode fallback from AArch64 `regs.rs`. -/ +def fl4SanitiseRegister (guest : Bool) (r : Register) (v : Word) : Word := + match r with + | .spsrEl1 => + if guest then + let mode := v &&& 0x1f + (v &&& 0x00000000f3f01fc0) ||| + (if mode = 0 ∨ mode = 4 ∨ mode = 5 then mode else 5) + else sanitiseRegister false r v + | _ => v + +theorem fl4SanitiseRegister_native (r : Register) (v : Word) : + fl4SanitiseRegister false r v = sanitiseRegister false r v := by + cases r <;> rfl + +theorem fl4SanitiseRegister_guest_agrees (v : Word) + (hmode : v &&& 0x1f = 0 ∨ v &&& 0x1f = 4 ∨ v &&& 0x1f = 5) + (hbits : (v &&& 0x00000000f3f01fc0) ||| (v &&& 0x1f) = v) : + fl4SanitiseRegister true .spsrEl1 v = sanitiseRegister true .spsrEl1 v := by + simp only [fl4SanitiseRegister, sanitiseRegister, if_true, true_and, Set.mem_insert_iff, + Set.mem_singleton_iff] + rw [if_pos hmode, if_pos hmode] + exact hbits + +/-- FL4's `TIMEOUT_REPLY_MESSAGE` (seL4 C's order). -/ +def fl4TimeoutMessage : List Register := + [.faultIP, .spEl0, .spsrEl1, .x0, .x1, .x2, .x3, .x4, .x5, .x6, .x7, .x8, .x16, .x17, .x18, .x29, .x30, + .x9, .x10, .x11, .x12, .x13, .x14, .x15, .x19, .x20, .x21, .x22, .x23, .x24, .x25, .x26, .x27, .x28] + +variable {Z : Type} [StateExt Z] + +/-- `faults.rs:handle_fault_reply`: whether the thread should be restarted. -/ +def fl4HandleFaultReply : Fault → ObjRef → Data → List Data → SMonad Z Bool + | .capFault _cptr _rp _lf, _thread, _x, _y => return true + | .unknownSyscallException _n, thread, label, msg => do + let t ← archGetSanitiseRegisterInfo thread + asUser thread <| NonDet.zipWithMx + (fun r v => setRegister r <| fl4SanitiseRegister t r v) + syscallMessage msg + return decide (label = 0) + | .userException _exception _code, thread, label, msg => do + let t ← archGetSanitiseRegisterInfo thread + asUser thread <| NonDet.zipWithMx + (fun r v => setRegister r <| fl4SanitiseRegister t r v) + exceptionMessage msg + return decide (label = 0) + | .timeout _badge, thread, label, msg => do + let t ← archGetSanitiseRegisterInfo thread + asUser thread <| NonDet.zipWithMx + (fun r v => setRegister r <| fl4SanitiseRegister t r v) + fl4TimeoutMessage msg + return decide (label = 0) + | .archFault af, thread, label, msg => handleArchFaultReply af thread label msg + +end + +end Fl4SpecA64 diff --git a/proofs/Fl4SpecA64/Ext/Finalise.lean b/proofs/Fl4SpecA64/Ext/Finalise.lean index dc8986931..93bb63bbf 100644 --- a/proofs/Fl4SpecA64/Ext/Finalise.lean +++ b/proofs/Fl4SpecA64/Ext/Finalise.lean @@ -7,9 +7,14 @@ Finalisation (FL4 `objects.rs` `finalise_cap_object`): `fl4FinaliseCap` is l4v's `finalise_cap` (CSpace_A, ported unchanged as `finaliseCap`) with these hooks, the ported behaviour otherwise: -* a frame cap mapped in a Stage-2 VSpace or an `ArmHighVSpace` (final or - not): `extUnmapPage` of its entry (FL4 `ept::unmap` / `unmap_page`; the - ported `unmap_page` walks only l4v's page tables and would find none); +* (generation 6, by the cap's kind as FL4's `is_ept`) a Stage-2 frame cap + (`stage2FrameCap`, final or not): `extUnmapPage .stage2` of its entry + (FL4 `ept::unmap`, a no-op unless its ASID still names a Stage-2 root); a + plain frame cap whose ASID names an `ArmHighVSpace`: `extUnmapPage .high` + (FL4 `unmap_page`'s `ROOT_HIGH` walk), one whose ASID names a Stage-2 + root: nothing (FL4 `unmap_page`'s `unmap_root` finds no Stage-1 or high + root); otherwise l4v's (the ported `unmap_page` walks only l4v's page + tables); * a final page table cap mapped in one: `extUnmapTable` (FL4 `vspace::unmap_page_table`, which dispatches on the root's kind); * a final assigned Stage-2 VSpace or `ArmHighVSpace` cap: l4v's @@ -18,8 +23,10 @@ hooks, the ported behaviour otherwise: * a final VCPU cap: the end of its run (`fl4VcpuEndRun`, FL4 `vcpu::unbind`'s first half), then l4v's (`vcpu_finalise`, the rest of the unbinding); -* a final thread cap: `fl4CancelRun` (FL4 `vcpu::detach`), then l4v's - (`prepare_thread_delete` dissociates the bound VCPU). +* a final thread cap: `fl4CancelRun` and, if bound to a VCPU `v`, + `fl4VcpuEndRun v` (FL4 `vcpu::detach`, `vcpu.rs:324-330`; a no-op when + `runCaller = none`), then l4v's (`prepare_thread_delete` dissociates the + bound VCPU). l4v's recursive delete and revoke call `finalise_cap` directly, so this module has their copies with `fl4FinaliseCap` for `finaliseCap`, as the x86 @@ -37,6 +44,7 @@ The ported modules are not changed. -/ import Fl4SpecA64.Ext.ARMVCPURun import Fl4SpecA64.Ext.TcbSize +import Fl4SpecA64.Ext.Tcb namespace Fl4SpecA64 @@ -63,10 +71,14 @@ def fl4FinaliseCap (cap : Cap) (final : Bool) : SMonad Z (Cap × Cap) := | .archObjectCap (.frameCap p _ sz _ (some (asid, va))) => do let k ← extKindOfAsid asid match k with - | some k => do - extUnmapPage k sz asid va p + | some .high => do + extUnmapPage .high sz asid va p return (.nullCap, .nullCap) + | some _ => return (.nullCap, .nullCap) | none => finaliseCap cap final + | .archObjectCap (.ext (.stage2FrameCap p _ sz _ (some (asid, va)))) => do + extUnmapPage .stage2 sz asid va p + return (.nullCap, .nullCap) | .archObjectCap (.pageTableCap p .normalPtT (some (asid, va))) => do let k ← extKindOfAsid asid match k with @@ -88,7 +100,12 @@ def fl4FinaliseCap (cap : Cap) (final : Bool) : SMonad Z (Cap × Cap) := NonDet.when final <| fl4VcpuEndRun v finaliseCap cap final | .threadCap t => do - NonDet.when final <| fl4CancelRun t + NonDet.when final <| do + fl4CancelRun t + let vOpt ← archThreadGet ArchTcb.tcbVcpu t + match vOpt with + | some v => fl4VcpuEndRun v + | none => pure () finaliseCap cap final | _ => finaliseCap cap final @@ -166,6 +183,11 @@ def fl4InvokeCnode (i : CnodeInvocation) : PMonad Z Unit := | .deleteCall destSlot => fl4CapDelete destSlot | i => invokeCnode i +/-- Read the live cap after deletion and apply FL4's object test. -/ +private def fl4CheckTcbCapAt (cap : Cap) (slot : CslotPtr) (body : SMonad Z Unit) : SMonad Z Unit := do + let live ← getCap slot + NonDet.when (fl4TcbSameObject cap live) body + /-- l4v `Tcb_A.install_tcb_cap` with `fl4CapDelete`. -/ def fl4InstallTcbCap (target : ObjRef) (slot : CslotPtr) (n : Nat) (slotOpt : Option (Cap × CslotPtr)) : PMonad Z Unit := @@ -174,8 +196,8 @@ def fl4InstallTcbCap (target : ObjRef) (slot : CslotPtr) (n : Nat) | some (newCap, srcSlot) => do fl4CapDelete (target, tcbCnodeIndex n) NonDetE.unlessE (newCap = .nullCap) <| - NonDetE.liftE <| checkCapAt newCap srcSlot - <| checkCapAt (.threadCap target) slot + NonDetE.liftE <| fl4CheckTcbCapAt newCap srcSlot + <| fl4CheckTcbCapAt (.threadCap target) slot <| capInsert newCap srcSlot (target, tcbCnodeIndex n) /-- l4v `Tcb_A.install_tcb_frame_cap` with `fl4CapDelete`. -/ @@ -189,15 +211,15 @@ def fl4InstallTcbFrameCap (target : ObjRef) (slot : CslotPtr) NonDetE.liftE <| match frame with | none => pure () | some (newCap, srcSlot) => - checkCapAt newCap srcSlot - <| checkCapAt (.threadCap target) slot + fl4CheckTcbCapAt newCap srcSlot + <| fl4CheckTcbCapAt (.threadCap target) slot <| capInsert newCap srcSlot (target, tcbCnodeIndex 2) let cur ← NonDetE.liftE <| NonDet.gets fun s : State Z => s.curThread NonDetE.liftE <| NonDet.when (target = cur) rescheduleRequired /-- l4v `Tcb_A.invoke_tcb` with `fl4InstallTcbCap` / `fl4InstallTcbFrameCap` -(the two thread-control cases) and `fl4CancelRun` before a suspension (the -module docstring); the other cases are l4v's. -/ +for slot replacement, `fl4CancelRun` on suspension, and the FL4 +register and shared budget operations. -/ def fl4InvokeTcb : TcbInvocation → PMonad Z (List Data) | .suspend thread => do NonDetE.liftE <| fl4CancelRun thread @@ -217,13 +239,45 @@ def fl4InvokeTcb : TcbInvocation → PMonad Z (List Data) match scopt with | none => do let scPtrOpt ← getTcbObjRef Tcb.tcbSchedContext target - let scPtr ← NonDet.assertOpt scPtrOpt - schedContextUnbindTcb scPtr - | some scPtr => schedContextBindTcb scPtr target) sc + NonDet.maybeM schedContextUnbindTcb scPtrOpt + | some scPtr => fl4TcbBindSc scPtr target) sc NonDetE.returnOk [] - | i@(.copyRegisters _ src suspendSource _ _ _ _) => do - NonDetE.liftE <| NonDet.when suspendSource <| fl4CancelRun src + | .resume t => NonDetE.liftE (do fl4TcbRestart t; return []) + | i@(.readRegisters src suspendSource _ _) => do + NonDetE.liftE <| NonDet.when suspendSource (fl4CancelRun src) invokeTcb i + | .copyRegisters dest src suspendSource resumeTarget transferFrame transferInteger _ => + NonDetE.liftE <| do + NonDet.when suspendSource <| do + fl4CancelRun src + suspend src + NonDet.when resumeTarget (fl4TcbRestart dest) + let guest ← archGetSanitiseRegisterInfo dest + let copy := fun r => do + let v ← asUser src (getRegister r) + asUser dest (setRegister r (fl4SanitiseRegister guest r v)) + NonDet.when transferFrame <| do + NonDet.mapMx copy frameRegisters + let pc ← asUser dest getRestartPC + asUser dest (setNextPC pc) + NonDet.when transferInteger (NonDet.mapMx copy gpRegisters) + let cur ← NonDet.gets fun s : State Z => s.curThread + archPostModifyRegisters cur dest + NonDet.when (dest = cur) rescheduleRequired + return [] + | .writeRegisters dest resumeTarget values _ => + NonDetE.liftE <| do + let cur ← NonDet.gets fun s : State Z => s.curThread + let guest ← archGetSanitiseRegisterInfo dest + asUser dest <| do + let _ ← NonDet.zipWithM (fun r v => setRegister r (fl4SanitiseRegister guest r v)) + (frameRegisters ++ gpRegisters) values + let pc ← getRestartPC + setNextPC pc + archPostModifyRegisters cur dest + NonDet.when resumeTarget (fl4TcbRestart dest) + NonDet.when (dest = cur) rescheduleRequired + return [] | i => invokeTcb i /-! ## Retype -/ diff --git a/proofs/Fl4SpecA64/Ext/Invocation.lean b/proofs/Fl4SpecA64/Ext/Invocation.lean index 64689b938..8d71304cb 100644 --- a/proofs/Fl4SpecA64/Ext/Invocation.lean +++ b/proofs/Fl4SpecA64/Ext/Invocation.lean @@ -13,14 +13,18 @@ these substitutions: 1. untyped caps: FL4's 4 KiB TCBs (`Ext/TcbSize`, `decodeUntypedInvocationWith fl4ObjBitsApi fl4DataToObjType`, `invokeUntypedWith fl4ObjBitsApi`). `UntypedRetypeShared` decodes as FL4's AArch64 kernel - answers it (`IllegalOperation`, no shared frames on AArch64); + answers it (`IllegalOperation`, no shared frames on AArch64); without virtualization a retype into `ArmVcpu` or + `ArmStage2VSpace` is `InvalidArgument 0` (`fl4DecodeUntypedInvocationVirt`, `Ext/UntypedVirt`); 2. the IRQControl cap: `decodeFl4IrqControl` / `invokeFl4IrqControl` (`Ext/IrqControl`: FL4's checks, `GetTriggerCore`, `GetMSI`); 3. thread caps: FL4's `TCBSetFlags`, `TCBReadFPU`, `TCBWriteFPU`, `TCBSetTimer` and `TCBSetPACKeys` (`Ext/Tcb`); every other label is l4v's `decode_tcb_invocation`; 4. frame caps: `ARMPageDeriveSubframe` and `ARMPageTestAndClearAccessed` - (`Ext/VSpace`); every other arch label is l4v's `arch_decode_invocation`. + (`Ext/VSpace`); (P3 S3 gen 33) the cache-maintenance labels of VSpace root + caps and of frames in l4v's tables are FL4's EL1 decoders + `decodeFl4VsInvFlush` / `decodeFl4FrInvFlush` (`Ext/VSpace` item 3, l4v's + invocations); every other arch label is l4v's `arch_decode_invocation`. `fl4HandleInvocation` is l4v's `handle_invocation` (Syscall_A) with this dispatcher. FL4's run never replies in `handle_invocation`'s tail: it @@ -28,7 +32,11 @@ leaves its caller in `TS_RUNNING_VM`, or `Running` after its own reply. 5. FL4's AArch64 objects (generation 4; `Ext/ArchTypes`, the in-port `ext` hook): Stage-2 VSpace caps (the VSpace cache invocations, `Ext/Stage2`) - and `ArmHighVSpace` caps (no invocations); `ARMPageMap` / + and `ArmHighVSpace` caps (no invocations); (generation 6) frame caps + mapped in a Stage-2 VSpace (`stage2FrameCap`, FL4's `is_ept`): `ARMPageMap` + only into a Stage-2 VSpace (`InvalidCapability 1` otherwise), `ARMPageUnmap`, + the cache labels and `ARMPageTestAndClearAccessed` (`IllegalOperation`) + as `Ext/Stage2`, every other label l4v's on the frame's plain view; `ARMPageMap` / `ARMPageTableMap` with a Stage-2 VSpace or `ArmHighVSpace` cap, and `ARMPageUnmap` / `ARMPageTableUnmap` of a frame / table mapped in one (`Ext/Stage2`, `Ext/ArmHighVSpace`); `ARMASIDPoolAssign` of either @@ -51,10 +59,14 @@ The ported modules are not changed. import Fl4SpecA64.Syscall import Fl4SpecA64.Ext.Label import Fl4SpecA64.Ext.TcbSize +import Fl4SpecA64.Ext.UntypedVirt import Fl4SpecA64.Ext.IrqControl +import Fl4SpecA64.Ext.IrqSignal import Fl4SpecA64.Ext.VSpace import Fl4SpecA64.Ext.Tcb import Fl4SpecA64.Ext.ArmHighVSpace +import Fl4SpecA64.Ext.SchedBudget +import Fl4SpecA64.Ext.Reply namespace Fl4SpecA64 @@ -110,6 +122,25 @@ def fl4ArchDecodeInvocation (calling : Bool) (l : Fl4Label) (args : List Data) ( match cap with | .ext (.stage2VSpaceCap _ _) => ext <| decodeS2Flush (specLabelWord l) args (.archObjectCap cap) | .ext (.highVSpaceCap _ _) => NonDetE.throwError .illegalOperation + | .ext (.stage2FrameCap p R sz dev mapped) => + -- generation 6: a frame cap mapped in a Stage-2 VSpace (FL4 `decode_frame` with `is_ept`); + -- the labels without an `is_ept` case are l4v's on the frame's plain view + let fcap : ArchCap := .frameCap p R sz dev mapped + if l = .ext .armPageDeriveSubframe then + NonDetE.liftME Fl4ArchInvocation.deriveSubframe <| decodeDeriveSubframe args cte fcap extraCaps + else if l = .ext .armPageTestAndClearAccessed then + NonDetE.liftME Fl4ArchInvocation.testAndClearAccessed <| decodeExtTestAndClearAccessed cap + else if isPageFlushLabel (invocationType (specLabelWord l)) then + ext <| decodeExtPageFlush (specLabelWord l) args cap + else if l = archL .armPageMap ∧ args.length > 2 ∧ extraCaps.length > 0 then + let vcap := (the extraCaps[0]?).1 + match vcap with + | .archObjectCap (.ext (.stage2VSpaceCap _ _)) => + ext <| decodeS2PageMap cte cap vcap (the args[0]?) (the args[1]?) (the args[2]?) + | _ => NonDetE.throwError (.invalidCapability 1) + else if l = archL .armPageUnmap then NonDetE.returnOk (.extVSpace (.pageUnmap .stage2 cap cte)) + else NonDetE.liftME Fl4ArchInvocation.spec <| + archDecodeInvocation (specLabelWord l) args xSlot cte fcap extraCaps | .frameCap _ _ _ _ mapped => if l = .ext .armPageDeriveSubframe then NonDetE.liftME Fl4ArchInvocation.deriveSubframe <| decodeDeriveSubframe args cte cap extraCaps @@ -118,8 +149,8 @@ def fl4ArchDecodeInvocation (calling : Bool) (l : Fl4Label) (args : List Data) ( | some (asid, _) => NonDetE.liftE <| extKindOfAsid asid | none => NonDetE.returnOk none match k with - | some k => - NonDetE.liftME Fl4ArchInvocation.testAndClearAccessed <| decodeExtTestAndClearAccessed k cap + | some _ => + NonDetE.liftME Fl4ArchInvocation.testAndClearAccessed <| decodeExtTestAndClearAccessed cap | none => NonDetE.liftME Fl4ArchInvocation.testAndClearAccessed <| decodeTestAndClearAccessed cap else if isPageFlushLabel (invocationType (specLabelWord l)) then do @@ -127,8 +158,10 @@ def fl4ArchDecodeInvocation (calling : Bool) (l : Fl4Label) (args : List Data) ( | some (asid, _) => NonDetE.liftE <| extKindOfAsid asid | none => NonDetE.returnOk none match k with - | some k => ext <| decodeExtPageFlush k (specLabelWord l) args cap - | none => spec + | some _ => ext <| decodeExtPageFlush (specLabelWord l) args cap + | none => + -- P3 S3 gen 33: FL4's EL1 decoder (`Ext/VSpace` item 3), in place of l4v's EL2 `decode_fr_inv_flush` + NonDetE.liftME Fl4ArchInvocation.spec <| decodeFl4FrInvFlush (specLabelWord l) args cte cap extraCaps else if l = archL .armPageMap ∧ args.length > 2 ∧ extraCaps.length > 0 then let vcap := (the extraCaps[0]?).1 match vcap with @@ -146,6 +179,11 @@ def fl4ArchDecodeInvocation (calling : Bool) (l : Fl4Label) (args : List Data) ( | none => spec | none => spec else spec + | .pageTableCap _ .vsRootPtT _ => + -- P3 S3 gen 33: FL4's EL1 decoder (`Ext/VSpace` item 3), in place of l4v's EL2 `decode_vs_inv_flush` + if isVSpaceFlushLabel (invocationType (specLabelWord l)) then + NonDetE.liftME Fl4ArchInvocation.spec <| decodeFl4VsInvFlush (specLabelWord l) args cte cap extraCaps + else spec | .pageTableCap _ .normalPtT mapped => if l = archL .armPageTableMap ∧ args.length > 1 ∧ extraCaps.length > 0 then let vcap := (the extraCaps[0]?).1 @@ -155,6 +193,7 @@ def fl4ArchDecodeInvocation (calling : Bool) (l : Fl4Label) (args : List Data) ( match e with | .stage2VSpaceCap _ _ => ext <| decodeS2PageTableMap cte cap vcap (the args[0]?) | .highVSpaceCap _ _ => ext <| decodeHighPageTableMap cte cap vcap (the args[0]?) + | .stage2FrameCap _ _ _ _ _ => spec -- no VSpace cap: l4v's `InvalidCapability 1` | _ => spec else if l = archL .armPageTableUnmap then match mapped with @@ -213,6 +252,10 @@ def fl4ArchPerformInvocation : Fl4ArchInvocation → PMonad Z (List Data) def fl4DecodeTcbInvocation (l : Fl4Label) (args : List Data) (cap : Cap) (slot : CslotPtr) (excaps : List (Cap × CslotPtr)) (buffer : Option ObjRef) : SEMonad Z Fl4Invocation := match l with + | .spec (.genInvocationLabel .tcbConfigure) => + NonDetE.liftME Fl4Invocation.invokeTCB <| fl4DecodeTcbConfigure args cap slot excaps + | .spec (.genInvocationLabel .tcbSetSpace) => + NonDetE.liftME Fl4Invocation.invokeTCB <| fl4DecodeSetSpace args cap slot excaps | .spec (.genInvocationLabel .tcbSetFlags) => NonDetE.liftME Fl4Invocation.invokeFl4TCB <| decodeFl4SetFlags args (objRefOf cap) | .ext .tcbSetHighVSpace => @@ -266,7 +309,7 @@ def fl4DecodeInvocation (calling firstPhase : Bool) (label : Data) (args : List if firstPhase then NonDetE.throwError <| .invalidCapability 0 else NonDetE.liftME Fl4Invocation.invokeSchedControl <| - decodeSchedControlInvocationFlags specL args (excaps.map Prod.fst) + decodeFl4SchedControlInvocation specL args (excaps.map Prod.fst) | .cnodeCap _ptr _bits _ => if firstPhase then NonDetE.throwError <| .invalidCapability 0 @@ -274,15 +317,22 @@ def fl4DecodeInvocation (calling firstPhase : Bool) (label : Data) (args : List decodeCnodeInvocation specL args cap (excaps.map Prod.fst) | .untypedCap _dev _ptr _sz _fi => NonDetE.liftME Fl4Invocation.invokeUntyped <| - fl4DecodeUntypedInvocation specL args slot cap (excaps.map Prod.fst) + fl4DecodeUntypedInvocationVirt specL args slot cap (excaps.map Prod.fst) | .archObjectCap archCap => NonDetE.liftME Fl4Invocation.invokeArchObject <| fl4ArchDecodeInvocation calling l args capIndex slot archCap excaps | _ => NonDetE.throwError <| .invalidCapability 0 +/-- FL4 completes these TCB replies inside the invocation. Running for +both Call and Send prevents an empty success reply at entry finish. -/ +def fl4FinishTcbReply (call : Bool) (words : List Data) : SMonad Z Unit := do + let thread ← NonDet.gets fun s : State Z => s.curThread + NonDet.when call (replyFromKernel thread (0, words)) + setThreadState thread .running + /-- l4v: Syscall_A.perform_invocation, with FL4's untyped, IRQControl, TCB, -CNode and arch invocations. -/ +CNode and arch invocations, and the FL4 budget and timeout Reply tail. -/ def fl4PerformInvocation : Bool → Bool → Bool → Fl4Invocation → PMonad Z (List Data) | _, _, _, .invokeUntyped i => do fl4InvokeUntypedA64 i @@ -295,7 +345,15 @@ def fl4PerformInvocation : Bool → Bool → Bool → Fl4Invocation → PMonad Z | _, _, _, .invokeNotification ep badge => do withoutPreemption <| sendSignal ep badge NonDetE.returnOk [] + | _, call, _, .invokeTCB i@(.readRegisters ..) => do + let words ← fl4InvokeTcb i + NonDetE.liftE (fl4FinishTcbReply call words) + NonDetE.returnOk [] | _, _, _, .invokeTCB i => fl4InvokeTcb i + | _, call, _, .invokeFl4TCB i@(.setFlags ..) => do + let words ← invokeFl4Tcb i + NonDetE.liftE (fl4FinishTcbReply call words) + NonDetE.returnOk [] | _, _, _, .invokeFl4TCB i => invokeFl4Tcb i | _, _, _, .invokeSetHighVSpace i => do invokeSetHighVSpace i @@ -307,7 +365,7 @@ def fl4PerformInvocation : Bool → Bool → Bool → Fl4Invocation → PMonad Z | _, _, _, .invokeReply reply grant => NonDetE.liftE (do let sender ← NonDet.gets fun s : State Z => s.curThread - doReplyTransfer sender reply grant + fl4DoReplyTransfer sender reply grant return []) | _, _, _, .invokeCNode i => do fl4InvokeCnode i @@ -316,12 +374,12 @@ def fl4PerformInvocation : Bool → Bool → Bool → Fl4Invocation → PMonad Z invokeFl4IrqControl i NonDetE.returnOk [] | _, _, _, .invokeIRQHandler i => do - NonDetE.liftE <| invokeIrqHandler i + NonDetE.liftE <| fl4InvokeIrqHandler i NonDetE.returnOk [] | _, _, _, .invokeSchedContext i => withoutPreemption (invokeSchedContext i) | _, _, _, .invokeSchedControl i => do - NonDetE.liftE <| invokeSchedControlConfigureFlags i + NonDetE.liftE <| invokeFl4SchedControlConfigureFlags i NonDetE.returnOk [] | _, _, _, .invokeArchObject i => fl4ArchPerformInvocation i diff --git a/proofs/Fl4SpecA64/Ext/IrqControl.lean b/proofs/Fl4SpecA64/Ext/IrqControl.lean index 209b81308..fb1248cba 100644 --- a/proofs/Fl4SpecA64/Ext/IrqControl.lean +++ b/proofs/Fl4SpecA64/Ext/IrqControl.lean @@ -11,8 +11,8 @@ AArch64**, modelled on FL4's `crates/fl4-core/src/arch/aarch64/interrupt.rs` 2. `ARMIRQIssueIRQHandlerTrigger` (l4v's label): as the plain label with the trigger argument, and with SMP (always, in FL4) only for SPIs (`IllegalOperation` for an INTID below 32; seL4's SMP - `Arch_decodeIRQControlInvocation`). The invocation is l4v's - (`armIRQControlInvocation`, `setIRQTrigger`). + `Arch_decodeIRQControlInvocation`). FL4 also routes the SPI to core 0, + so this shares `setIRQTriggerCore` with `GetTriggerCore`. 3. `ARMIRQIssueIRQHandlerTriggerCore` (seL4's SMP `GetTriggerCore`, not in l4v): five arguments (INTID, trigger, index, depth with its low 8 bits, core); the core must be online (`InvalidArgument 4`); the IRQ is @@ -106,8 +106,6 @@ def decodeFl4IrqIssue (kind : Nat) (args : List Data) (srcSlot : CslotPtr) (cps let irq : Irq := ucast irqLen (fl4IrqOf irqW cpu) let dest ← fl4IrqTarget irq cnode index depth if kind = 0 then NonDetE.returnOk (.spec (.irqControl irq dest srcSlot)) - else if kind = 1 then - NonDetE.returnOk (.spec (.archIRQControl (.armIRQControlInvocation irq dest srcSlot trigger))) else NonDetE.returnOk (.triggerCore irq dest srcSlot trigger cpu) else NonDetE.throwError .truncatedMessage @@ -157,6 +155,14 @@ def invokeFl4IrqControl : Fl4IrqControlInvocation → PMonad Z Unit setIrqState .irqSignal irq capInsert (.irqHandlerCap irq) src dest) +/-- Core-zero issue agrees with the legacy trigger invocation on a platform +where the two machine operations agree. -/ +theorem invokeFl4IrqControl_coreZero_conservative (irq : Irq) (dest src : CslotPtr) (trigger : Bool) + (h : setIRQTriggerCore irq trigger 0 = setIRQTrigger irq trigger) : + invokeFl4IrqControl (Z := Z) (.triggerCore irq dest src trigger 0) = + invokeIrqControl (.archIRQControl (.armIRQControlInvocation irq dest src trigger)) := by + simp only [invokeFl4IrqControl, invokeIrqControl, archInvokeIrqControl, h] + end end diff --git a/proofs/Fl4SpecA64/Ext/IrqSignal.lean b/proofs/Fl4SpecA64/Ext/IrqSignal.lean new file mode 100644 index 000000000..34ad9da71 --- /dev/null +++ b/proofs/Fl4SpecA64/Ext/IrqSignal.lean @@ -0,0 +1,96 @@ +/- +FL4 difference (Fl4SpecA64/PORTING.md, "Ext boundary"): **a signalled IRQ is +masked, and ack unmasks it, whatever the GIC version**, modelled on FL4's +`crates/fl4-core/src/interrupt.rs` (`handle_interrupt`: `a.mask_irq(irq, true)` +after the signal; `decode_irq_handler`, `IRQAckIRQ`: `a.mask_irq(irq, false)`). + +l4v's `arch_mask_irq_signal` masks only without `config_ARM_GIC_V3` (with GICv3 +it relies on the priority drop and `deactivateInterrupt`). + +* `fl4ArchMaskIrqSignal`: mask. +* `fl4HandleInterrupt` / `fl4MaybeHandleInterrupt`: l4v's `handle_interrupt` / + `maybe_handle_interrupt` with `fl4ArchMaskIrqSignal` for + `arch_mask_irq_signal`; every other arm as ported. Used by the FL4 kernel + entry (`Ext/Entry`). +* `fl4InvokeIrqHandler`: l4v's `invoke_irq_handler` with Ack as an unmask. Used + by the FL4 dispatcher (`Ext/Invocation`). + +Without GICv3 the first two are l4v's (`fl4HandleInterrupt_eq_l4v`, +`fl4MaybeHandleInterrupt_eq_l4v`). l4v's GICv3 `deactivateInterrupt` asserts the +configuration and unmasks, so `fl4InvokeIrqHandler` has l4v's outcomes on both. + +The ported modules are not changed. +-/ +import Fl4SpecA64.Interrupt + +namespace Fl4SpecA64 + +open Fl4Spec +open Classical + +noncomputable section + +section +variable {Z : Type} [StateExt Z] + +/-- FL4: mask a signalled IRQ (`handle_interrupt`'s `mask_irq(irq, true)`). -/ +def fl4ArchMaskIrqSignal (irq : Irq) : SMonad Z Unit := + doMachineOp (maskInterrupt true irq) + +/-- FL4: `invoke_irq_handler`, Ack unmasking the IRQ (`mask_irq(irq, false)`); +Set and Clear are l4v's. -/ +def fl4InvokeIrqHandler : IrqHandlerInvocation → SMonad Z Unit + | .aCKIrq irq => doMachineOp (maskInterrupt false irq) + | .setIRQHandler irq cap slot => invokeIrqHandler (.setIRQHandler irq cap slot) + | .clearIRQHandler irq => invokeIrqHandler (.clearIRQHandler irq) + +/-- l4v: Interrupt_A.handle_interrupt, with `fl4ArchMaskIrqSignal`. -/ +def fl4HandleInterrupt (irq : Irq) : SMonad Z Unit := + if irq > maxIRQ then doMachineOp (do + maskInterrupt true irq + ackInterrupt irq) + else do + let st ← getIrqState irq + match st with + | .irqSignal => do + let slot ← getIrqSlot irq + let cap ← getCap slot + NonDet.when (isNtfnCap cap ∧ AllowSend ∈ capRights cap) <| + sendSignal (objRefOf cap) (capEpBadge cap) + fl4ArchMaskIrqSignal irq + | .irqTimer => do + doMachineOp ackDeadlineIRQ + NonDet.modify fun s : State Z => { s with reprogramTimer := true } + | .irqInactive => NonDet.fail -- not meant to be able to get IRQs from inactive lines + | .irqReserved => handleReservedIrq irq + doMachineOp <| ackInterrupt irq + +/-- l4v: Interrupt_A.maybe_handle_interrupt, with `fl4HandleInterrupt`. -/ +def fl4MaybeHandleInterrupt (inKernel : Bool) : SMonad Z Unit := do + let irqOpt ← doMachineOp <| getActiveIRQ inKernel + match irqOpt with + | none => handleSpuriousIrq + | some irq => fl4HandleInterrupt irq + +theorem fl4ArchMaskIrqSignal_eq_l4v (v2 : config_ARM_GIC_V3 = false) (irq : Irq) : + fl4ArchMaskIrqSignal (Z := Z) irq = archMaskIrqSignal irq := by + unfold fl4ArchMaskIrqSignal archMaskIrqSignal + simp [v2, NonDet.when] + +theorem fl4HandleInterrupt_eq_l4v (v2 : config_ARM_GIC_V3 = false) (irq : Irq) : + fl4HandleInterrupt (Z := Z) irq = handleInterrupt irq := by + unfold fl4HandleInterrupt handleInterrupt + simp only [fl4ArchMaskIrqSignal_eq_l4v v2] + rfl + +theorem fl4MaybeHandleInterrupt_eq_l4v (v2 : config_ARM_GIC_V3 = false) (inKernel : Bool) : + fl4MaybeHandleInterrupt (Z := Z) inKernel = maybeHandleInterrupt inKernel := by + unfold fl4MaybeHandleInterrupt maybeHandleInterrupt + simp only [fl4HandleInterrupt_eq_l4v v2] + rfl + +end + +end + +end Fl4SpecA64 diff --git a/proofs/Fl4SpecA64/Ext/Reply.lean b/proofs/Fl4SpecA64/Ext/Reply.lean new file mode 100644 index 000000000..29f332165 --- /dev/null +++ b/proofs/Fl4SpecA64/Ext/Reply.lean @@ -0,0 +1,184 @@ +import Fl4SpecA64.Ipc +import Fl4SpecA64.Ext.FaultReply +import Fl4SpecA64.Ext.SchedControl + +/-! +FL4 Reply and timeout delivery. The l4v IPC control flow is shared with +`Ipc_A`. The Reply budget branch uses FL4's minimum, fault transfer +uses FL4's counter conversion and consumed-time cap, and a fault reply +uses FL4's handler (`Ext/FaultReply`). +-/ + +namespace Fl4SpecA64 + +open Fl4Spec Classical + +noncomputable section + +set_option maxRecDepth 4096 +variable {Z : Type} [StateExt Z] + +/-- `faults.rs:set_mrs_fault`: use FL4's consumed-time word on timeout. +The other fault messages use the existing architecture cases. -/ +def fl4MakeFaultMsg (fault : Fault) (thread : ObjRef) : SMonad Z (Data × List Data) := + match fault with + | .timeout badge => do + let tcb ← NonDet.getsThe (getTcb thread) + match tcb.tcbSchedContext with + | none => return (5, [badge]) + | some sc => do + let consumed ← fl4SchedContextUpdateConsumed sc + return (5, badge :: wordsFromTime consumed) + | _ => makeFaultMsg fault thread + +theorem fl4MakeFaultMsg_eq_l4v + (hcap : fl4MaxConsumedTicks = maxTicksToUs) (hconv : Fl4TicksToUsLegacyAgree) + (fault : Fault) (thread : ObjRef) : + fl4MakeFaultMsg (Z := Z) fault thread = makeFaultMsg fault thread := by + cases fault <;> simp only [fl4MakeFaultMsg, makeFaultMsg, fl4SchedContextUpdateConsumed_eq_l4v hcap hconv] <;> rfl + +def fl4DoFaultTransfer (badge : Data) (sender receiver : ObjRef) (buf : Option ObjRef) : + SMonad Z Unit := do + let fault ← threadGet Tcb.tcbFault sender + let f ← match fault with + | some f => pure f + | none => NonDet.fail + let (label, msg) ← fl4MakeFaultMsg f sender + let sent ← setMrs receiver buf msg + setMessageInfo receiver ⟨sent, 0, 0, label⟩ + asUser receiver <| setRegister badge_register badge + +private theorem fl4DoFaultTransfer_eq_l4v + (hcap : fl4MaxConsumedTicks = maxTicksToUs) (hconv : Fl4TicksToUsLegacyAgree) + (badge : Data) (sender receiver : ObjRef) (buf : Option ObjRef) : + fl4DoFaultTransfer (Z := Z) badge sender receiver buf = doFaultTransfer badge sender receiver buf := by + simp only [fl4DoFaultTransfer, doFaultTransfer, fl4MakeFaultMsg_eq_l4v hcap hconv] <;> rfl + +def fl4DoIpcTransfer (sender : ObjRef) (ep : Option ObjRef) (badge : Badge) (grant : Bool) + (receiver : ObjRef) : SMonad Z Unit := do + let recvBuffer ← lookupIpcBuffer true receiver + let fault ← threadGet Tcb.tcbFault sender + match fault with + | none => do + let sendBuffer ← lookupIpcBuffer false sender + doNormalTransfer sender sendBuffer ep badge grant receiver recvBuffer + | some _f => fl4DoFaultTransfer badge sender receiver recvBuffer + +theorem fl4DoIpcTransfer_eq_l4v + (hcap : fl4MaxConsumedTicks = maxTicksToUs) (hconv : Fl4TicksToUsLegacyAgree) + (sender : ObjRef) (ep : Option ObjRef) (badge : Badge) (grant : Bool) (receiver : ObjRef) : + fl4DoIpcTransfer (Z := Z) sender ep badge grant receiver = doIpcTransfer sender ep badge grant receiver := by + simp only [fl4DoIpcTransfer, doIpcTransfer, fl4DoFaultTransfer_eq_l4v hcap hconv] <;> rfl + +def fl4SendIpc (block call : Bool) (badge : Badge) (canGrant canGrantReply canDonate : Bool) + (thread epptr : ObjRef) : SMonad Z Unit := do + let ep ← getEndpoint epptr + match ep, block with + | .idleEP, true => sendIpcBlocked thread epptr badge canGrant canGrantReply call + | .sendEP _queue, true => sendIpcBlocked thread epptr badge canGrant canGrantReply call + | .idleEP, false => return () + | .sendEP _queue, false => return () + | .recvEP (dest :: _queue), _ => do + tcbEpDequeue dest epptr + let recvState ← getThreadState dest + let reply ← match recvState with + | .blockedOnReceive _ reply _data => pure reply + | _ => NonDet.fail + fl4DoIpcTransfer thread (some epptr) badge canGrant dest + NonDet.maybeM (replyUnlinkTcb dest) reply + let scOpt ← getTcbObjRef Tcb.tcbSchedContext dest + let fault ← threadGet Tcb.tcbFault thread + if call ∨ fault ≠ none then + if (canGrant ∨ canGrantReply) ∧ reply ≠ none then + replyPush thread dest (the reply) canDonate + else + setThreadState thread .inactive + else NonDet.when (canDonate ∧ scOpt = none) <| do + let threadSc ← getTcbObjRef Tcb.tcbSchedContext thread + schedContextDonate (the threadSc) dest + setThreadState dest .running + let destSc ← getTcbObjRef Tcb.tcbSchedContext dest + ifSporadicCurScTestRefillUnblockCheck destSc + possibleSwitchTo dest + | .recvEP [], _ => NonDet.fail + +theorem fl4SendIpc_eq_l4v + (hcap : fl4MaxConsumedTicks = maxTicksToUs) (hconv : Fl4TicksToUsLegacyAgree) + (block call : Bool) (badge : Badge) (grant grantReply donate : Bool) (thread ep : ObjRef) : + fl4SendIpc (Z := Z) block call badge grant grantReply donate thread ep = sendIpc block call badge grant grantReply donate thread ep := by + simp only [fl4SendIpc, sendIpc, fl4DoIpcTransfer_eq_l4v hcap hconv] <;> rfl + +def fl4SendFaultIpc (tptr : ObjRef) (handlerCap : Cap) (fault : Fault) (canDonate : Bool) : + SMonad Z Bool := + match handlerCap with + | .endpointCap _ref _badge rights => do + threadSet (fun tcb => { tcb with tcbFault := some fault }) tptr + fl4SendIpc true false (capEpBadge handlerCap) + (decide (Rights.allowGrant ∈ rights)) (decide (Rights.allowGrantReply ∈ rights)) canDonate tptr + (capEpPtr handlerCap) + return true + | .nullCap => return false + | _ => NonDet.fail + +private theorem fl4SendFaultIpc_eq_l4v + (hcap : fl4MaxConsumedTicks = maxTicksToUs) (hconv : Fl4TicksToUsLegacyAgree) + (tptr : ObjRef) (handler : Cap) (fault : Fault) (canDonate : Bool) : + fl4SendFaultIpc (Z := Z) tptr handler fault canDonate = sendFaultIpc tptr handler fault canDonate := by + simp only [fl4SendFaultIpc, sendFaultIpc, fl4SendIpc_eq_l4v hcap hconv] <;> rfl + +def fl4HandleTimeout (tptr : ObjRef) (ex : Fault) : SMonad Z Unit := do + let valid ← isValidTimeoutHandler tptr + NonDet.assert valid + let timeoutHandlerSlot ← pure (getTcbTimeoutHandlerPtr tptr) + let timeoutHandlerCap ← getCap timeoutHandlerSlot + let _ ← fl4SendFaultIpc tptr timeoutHandlerCap ex false + return () + +theorem fl4HandleTimeout_eq_l4v + (hcap : fl4MaxConsumedTicks = maxTicksToUs) (hconv : Fl4TicksToUsLegacyAgree) + (tptr : ObjRef) (fault : Fault) : + fl4HandleTimeout (Z := Z) tptr fault = handleTimeout tptr fault := by + simp only [fl4HandleTimeout, handleTimeout, fl4SendFaultIpc_eq_l4v hcap hconv] <;> rfl + +def fl4DoReplyTransfer (sender reply : ObjRef) (grant : Bool) : SMonad Z Unit := do + let recvOpt ← getReplyTcb reply + NonDet.maybeM (fun receiver => do + let state ← getThreadState receiver + match state with + | .blockedOnReply r => do + NonDet.assert (r = reply) + replyRemove receiver reply + let scOpt ← getTcbObjRef Tcb.tcbSchedContext receiver + ifSporadicCurScTestRefillUnblockCheck scOpt + let fault ← threadGet Tcb.tcbFault receiver + match fault with + | none => do + fl4DoIpcTransfer sender none 0 grant receiver + setThreadState receiver .running + | some f => do + let mi ← getMessageInfo sender + let buf ← lookupIpcBuffer false sender + let mrs ← getMrs sender buf mi + let restart ← fl4HandleFaultReply f receiver mi.miLabel mrs + threadSet (fun tcb => { tcb with tcbFault := none }) receiver + setThreadState receiver (if restart then .restart else .inactive) + let scOpt ← getTcbObjRef Tcb.tcbSchedContext receiver + let state ← getThreadState receiver + NonDet.when (scOpt ≠ none ∧ runnable state) <| do + let scPtr ← NonDet.assertOpt scOpt + let sc ← getSchedContext scPtr + let curtime ← NonDet.gets fun s : State Z => s.curTime + if refillReady curtime (refillHd sc) ∧ fl4RefillSufficient 0 (refillHd sc) + then possibleSwitchTo receiver + else do + let tcb ← NonDet.getsThe (getTcb receiver) + if isEpCap tcb.tcbTimeoutHandler ∧ + ¬(fault.elim false isTimeoutFault = true) + then fl4HandleTimeout receiver (.timeout sc.scBadge) + else postpone scPtr + | _ => return ()) + recvOpt + +end + +end Fl4SpecA64 diff --git a/proofs/Fl4SpecA64/Ext/SchedBudget.lean b/proofs/Fl4SpecA64/Ext/SchedBudget.lean new file mode 100644 index 000000000..6b3b49362 --- /dev/null +++ b/proofs/Fl4SpecA64/Ext/SchedBudget.lean @@ -0,0 +1,258 @@ +import Fl4SpecA64.Ext.SchedControl +import Fl4SpecA64.Ext.Reply +import Fl4SpecA64.Ext.Timestamp + +/-! +FL4's AArch64 budget charging and scheduler-control execution use the +platform release-time limit and the minimum in counter ticks. See pinned +seL4 `include/kernel/sporadic.h:36–50`, FL4's `sporadic::max_release_time` +and `sched::commit_time`. +-/ + +namespace Fl4SpecA64 + +open Fl4Spec Classical + +noncomputable section + +variable {Z : Type} [StateExt Z] + +/-- The head can cover the usage and the FL4 kernel's minimum budget. -/ +def fl4GetScRefillSufficient (scPtr : ObjRef) (usage : Ticks) : SMonad Z Bool := + NonDet.getsThe fun s => (do + let head ← readRefillHead scPtr + Lookup.oreturn (fl4RefillSufficient usage head)) s + +theorem fl4GetScRefillSufficient_eq_l4v (hmin : fl4MinBudget = MIN_BUDGET) + (scPtr : ObjRef) (usage : Ticks) : + fl4GetScRefillSufficient (Z := Z) scPtr usage = getScRefillSufficient scPtr usage := by + unfold fl4GetScRefillSufficient getScRefillSufficient readScRefillSufficient + rw [fl4RefillSufficient_eq_l4v hmin] + +/-- The first overrun loop stops at the FL4 platform release-time limit. -/ +def headRefillOverrunWith (limit : Ticks) (scPtr : ObjRef) (usage : Ticks) : RMonad Z Bool := do + let head ← readRefillHead scPtr + Lookup.oreturn (decide (head.rAmount ≤ usage ∧ head.rTime < limit)) + +def handleOverrunWith (limit : Ticks) (scPtr : ObjRef) (usage : Ticks) : SMonad Z Ticks := + NonDet.whileLoop (fun usage s => the (headRefillOverrunWith limit scPtr usage s) = true) + (fun usage => chargeEntireHeadRefill scPtr usage) usage + +def refillHeadInsufficientWith (minimum : Ticks) (scPtr : ObjRef) : RMonad Z Bool := do + let head ← readRefillHead scPtr + Lookup.oreturn (decide (head.rAmount < minimum)) + +def headInsufficientLoopWith (minimum : Ticks) (scPtr : ObjRef) : SMonad Z Unit := + NonDet.whileLoop (fun _ s => the (refillHeadInsufficientWith minimum scPtr s) = true) + (fun _ => mergeNonoverlappingHeadRefill scPtr) () + +/-- Charge a periodic SC using the FL4 limits for the overrun, partial +charge and insufficient-head stages. -/ +def refillBudgetCheckWith (limit minimum : Ticks) (usage : Ticks) : SMonad Z Unit := do + let scPtr ← NonDet.gets fun s : State Z => s.curSc + let robin ← isRoundRobin scPtr + NonDet.assert (¬robin) + let usage' ← handleOverrunWith limit scPtr usage + let head ← getRefillHead scPtr + NonDet.when (usage' > 0 ∧ head.rTime < limit) <| do + let sc ← getSchedContext scPtr + let used ← pure { rTime := head.rTime + sc.scPeriod, rAmount := usage' : Refill } + updateRefillHd scPtr fun r => { r with rAmount := head.rAmount - usage' } + updateRefillHd scPtr fun r => { r with rTime := head.rTime + usage' } + scheduleUsed scPtr used + headInsufficientLoopWith minimum scPtr + +/-- Charge a periodic SC with the FL4 release-time limit and minimum budget. -/ +def fl4RefillBudgetCheck (usage : Ticks) : SMonad Z Unit := + refillBudgetCheckWith fl4MaxReleaseTime fl4MinBudget usage + +theorem fl4RefillBudgetCheck_eq_l4v (hlimit : fl4MaxReleaseTime = MAX_RELEASE_TIME) + (hmin : fl4MinBudget = MIN_BUDGET) (usage : Ticks) : + fl4RefillBudgetCheck (Z := Z) usage = refillBudgetCheck usage := by + unfold fl4RefillBudgetCheck + rw [hlimit, hmin] + rfl + +/-- FL4's `sched::commit_time` on the local scheduler view. -/ +def fl4CommitTime : SMonad Z Unit := do + let csc ← NonDet.gets fun s : State Z => s.curSc + let active ← getScActive csc + NonDet.when (active ∧ csc ≠ idleScPtr) <| do + let consumed ← NonDet.gets fun s : State Z => s.consumedTime + NonDet.when (0 < consumed) <| do + let robin ← isRoundRobin csc + if robin + then refillBudgetCheckRoundRobin consumed + else fl4RefillBudgetCheck consumed + updateSchedContext csc fun sc => { sc with scConsumed := sc.scConsumed + consumed } + NonDet.modify fun s => { s with consumedTime := 0 } + +/-! ### Global time -/ + +theorem fl4CommitTime_eq_l4v (hlimit : fl4MaxReleaseTime = MAX_RELEASE_TIME) + (hmin : fl4MinBudget = MIN_BUDGET) : + fl4CommitTime (Z := Z) = commitTime := by + simp only [fl4CommitTime, commitTime, fl4RefillBudgetCheck_eq_l4v hlimit hmin] + +/-- Resume with the FL4 minimum budget on the local view. -/ +def fl4SchedContextResume (scPtr : ObjRef) : SMonad Z Unit := do + let sc ← getSchedContext scPtr + let tptr ← NonDet.assertOpt sc.scTcb + let sched ← NonDet.gets (schedulable tptr) + NonDet.when sched <| do + let ts ← threadGet Tcb.tcbState tptr + let ready ← getScRefillReady scPtr + let sufficient ← fl4GetScRefillSufficient scPtr 0 + NonDet.when (runnable ts ∧ scActive sc ∧ ¬(ready ∧ sufficient)) <| do + -- C code also asserts that tptr is not in ready q (l4v comment) + let d ← threadGet Tcb.tcbDomain tptr + let prio ← threadGet Tcb.tcbPriority tptr + let queue ← getTcbQueue d prio + NonDet.assert (¬(tptr ∈ queue)) + postpone scPtr + +/-! ### Consumed time -/ + +theorem fl4SchedContextResume_eq_l4v (hmin : fl4MinBudget = MIN_BUDGET) (scPtr : ObjRef) : + fl4SchedContextResume (Z := Z) scPtr = schedContextResume scPtr := by + simp only [fl4SchedContextResume, schedContextResume, fl4GetScRefillSufficient_eq_l4v hmin] + +/-- FL4 ConfigureFlags for a local SC and, when bound, a local thread. +The same-core stall and migration have no abstract effect. -/ +def invokeFl4SchedControlConfigureFlags (iv : SchedControlInvocation) : SMonad Z Unit := + match iv with + | .invokeSchedControlConfigureFlags scPtr budget period mrefills badge flag => do + let sc ← getSchedContext scPtr + NonDet.when (sc.scTcb ≠ none) <| do + let tcbPtr ← NonDet.assertOpt sc.scTcb + tcbReleaseRemove tcbPtr + tcbSchedAction tcbSchedDequeue tcbPtr + let curSc ← NonDet.gets fun s : State Z => s.curSc + NonDet.when (curSc = scPtr) fl4CommitTime + if period = budget + then refillNew scPtr MIN_REFILLS budget 0 + else do + let active ← getScActive scPtr + if active ∧ sc.scTcb ≠ none + then do + let tcbPtr ← NonDet.assertOpt sc.scTcb + let st ← getThreadState tcbPtr + if runnable st + then refillUpdate scPtr period budget mrefills + else refillNew scPtr mrefills budget period + else refillNew scPtr mrefills budget period + NonDet.when (sc.scTcb ≠ none) <| do + fl4SchedContextResume scPtr + let tcbPtr ← NonDet.assertOpt sc.scTcb + let st ← getThreadState tcbPtr + let ct ← NonDet.gets fun s : State Z => s.curThread + if tcbPtr = ct then rescheduleRequired + else NonDet.when (runnable st) <| possibleSwitchTo tcbPtr + setScObjRef scBadgeUpdate scPtr badge + setScObjRef scSporadicUpdate scPtr (testBit flag scSporadicFlag) + +theorem invokeFl4SchedControlConfigureFlags_eq_l4v + (hlimit : fl4MaxReleaseTime = MAX_RELEASE_TIME) (hmin : fl4MinBudget = MIN_BUDGET) + (iv : SchedControlInvocation) : + invokeFl4SchedControlConfigureFlags (Z := Z) iv = invokeSchedControlConfigureFlags iv := by + cases iv + simp only [invokeFl4SchedControlConfigureFlags, invokeSchedControlConfigureFlags, + fl4CommitTime_eq_l4v hlimit hmin, fl4SchedContextResume_eq_l4v hmin] + +def fl4EndTimeslice (canTimeout : Bool) : SMonad Z Unit := do + let scPtr ← NonDet.gets fun s : State Z => s.curSc + let roundRobin ← isRoundRobin scPtr + let ct ← NonDet.gets fun s : State Z => s.curThread + let ts ← getThreadState ct + let valid ← match ts with + | .ext .runningVM => pure false -- FL4 hook + | _ => isValidTimeoutHandler ct + if canTimeout ∧ ¬roundRobin ∧ valid + then do + let sc ← getSchedContext scPtr + fl4HandleTimeout ct (.timeout sc.scBadge) + else do + let ready ← getScRefillReady scPtr + let sufficient ← fl4GetScRefillSufficient scPtr 0 + if ready ∧ sufficient + then tcbSchedAction tcbSchedAppend ct -- not_queued & ready & sufficient & runnable + else postpone scPtr + +def fl4ChargeHead (cscPtr : ObjRef) (consumed : Ticks) : SMonad Z Unit := + NonDet.when (cscPtr ≠ idleScPtr) <| do + let robin ← isRoundRobin cscPtr + if robin + then refillResetRr cscPtr + else fl4RefillBudgetCheck consumed + updateSchedContext cscPtr (fun sc => { sc with scConsumed := sc.scConsumed + consumed }) + +def fl4ChargeBudget (consumed : Ticks) (canTimeout : Bool) : SMonad Z Unit := do + let cscPtr ← NonDet.gets fun s : State Z => s.curSc + fl4ChargeHead cscPtr consumed + NonDet.modify fun s : State Z => { s with consumedTime := 0 } + let ct ← NonDet.gets fun s : State Z => s.curThread + let sched ← NonDet.gets (schedulable ct) + NonDet.when sched <| do + fl4EndTimeslice canTimeout + rescheduleRequired + NonDet.modify fun s : State Z => { s with reprogramTimer := true } + +def fl4CheckBudget : SMonad Z Bool := do + let csc ← NonDet.gets fun s : State Z => s.curSc + let consumed ← NonDet.gets fun s : State Z => s.consumedTime + let sufficient ← fl4GetScRefillSufficient csc consumed + if sufficient + then do + let domExp ← NonDet.gets isCurDomainExpired + return !domExp + else do + let consumed ← NonDet.gets fun s : State Z => s.consumedTime + fl4ChargeBudget consumed true + return false + +def fl4CheckBudgetRestart : SMonad Z Bool := do + let result ← fl4CheckBudget + let cur ← NonDet.gets fun s : State Z => s.curThread + let st ← getThreadState cur + NonDet.when (¬result ∧ runnable st) <| setThreadState cur .restart + return result + +private theorem fl4EndTimeslice_eq_l4v (hmin : fl4MinBudget = MIN_BUDGET) + (hcap : fl4MaxConsumedTicks = maxTicksToUs) (hconv : Fl4TicksToUsLegacyAgree) : + (fl4EndTimeslice : Bool → SMonad Z Unit) = endTimeslice := by + have e : (fl4GetScRefillSufficient : ObjRef → Ticks → SMonad Z Bool) = getScRefillSufficient := + funext fun sc => funext fun ticks => fl4GetScRefillSufficient_eq_l4v hmin sc ticks + unfold fl4EndTimeslice endTimeslice + rw [e, show (@fl4HandleTimeout Z _) = handleTimeout from funext fun t => funext fun f => + fl4HandleTimeout_eq_l4v hcap hconv t f] <;> rfl + +private theorem fl4ChargeBudget_eq_l4v (hlimit : fl4MaxReleaseTime = MAX_RELEASE_TIME) + (hmin : fl4MinBudget = MIN_BUDGET) + (hcap : fl4MaxConsumedTicks = maxTicksToUs) (hconv : Fl4TicksToUsLegacyAgree) : + (fl4ChargeBudget : Ticks → Bool → SMonad Z Unit) = chargeBudget := by + have e : (fl4RefillBudgetCheck : Ticks → SMonad Z Unit) = refillBudgetCheck := + funext fun ticks => fl4RefillBudgetCheck_eq_l4v hlimit hmin ticks + unfold fl4ChargeBudget fl4ChargeHead chargeBudget + rw [e, fl4EndTimeslice_eq_l4v hmin hcap hconv] + +theorem fl4CheckBudget_eq_l4v (hlimit : fl4MaxReleaseTime = MAX_RELEASE_TIME) + (hmin : fl4MinBudget = MIN_BUDGET) + (hcap : fl4MaxConsumedTicks = maxTicksToUs) (hconv : Fl4TicksToUsLegacyAgree) : + fl4CheckBudget (Z := Z) = checkBudget := by + simp only [fl4CheckBudget, checkBudget, fl4GetScRefillSufficient_eq_l4v hmin, + fl4ChargeBudget_eq_l4v hlimit hmin hcap hconv] + +theorem fl4CheckBudgetRestart_eq_l4v (hlimit : fl4MaxReleaseTime = MAX_RELEASE_TIME) + (hmin : fl4MinBudget = MIN_BUDGET) + (hcap : fl4MaxConsumedTicks = maxTicksToUs) (hconv : Fl4TicksToUsLegacyAgree) : + fl4CheckBudgetRestart (Z := Z) = checkBudgetRestart := by + simp only [fl4CheckBudgetRestart, checkBudgetRestart, fl4CheckBudget_eq_l4v hlimit hmin hcap hconv] + +/-- The clock reading and budget check at FL4 syscall entry. -/ +def fl4BudgetOk : SMonad Z Bool := do + fl4UpdateTimestamp + fl4CheckBudgetRestart + +end + +end Fl4SpecA64 diff --git a/proofs/Fl4SpecA64/Ext/SchedControl.lean b/proofs/Fl4SpecA64/Ext/SchedControl.lean new file mode 100644 index 000000000..5ae25b9e7 --- /dev/null +++ b/proofs/Fl4SpecA64/Ext/SchedControl.lean @@ -0,0 +1,190 @@ +import Fl4SpecA64.Decode + +/-! +FL4's platform-dependent scheduling-context time bound and tick comparisons. +See pinned seL4 `c98d70ad`, `include/kernel/sporadic.h:36–50` and +`src/object/schedcontrol.c:118–140`, and FL4's +`crates/fl4-core/src/sporadic.rs:30–38` and `schedcontext.rs:367–379`. +-/ + +namespace Fl4SpecA64 + +open Fl4Spec Classical + +noncomputable section + +/-- The platform's maximum period in microseconds, `getMaxUsToTicks() / 8` +when `CONFIG_KERNEL_STATIC_MAX_PERIOD_US = 0`. -/ +opaque fl4MaxPeriodUs : Word64 + +/-- The AArch64 counter's boot frequency, read from `CNTFRQ_EL0`. -/ +opaque fl4TimerFrequencyHz : Word64 + +/-- `crates/fl4-arch-aarch64/src/timer.rs`: compute the product and +quotient before saturating to a 64-bit tick count. -/ +def fl4UsToTicks (us : Word64) : Ticks := + BitVec.ofNat 64 (min maxTime.toNat (us.toNat * fl4TimerFrequencyHz.toNat / 1000000)) + +/-- `sporadic::min_budget`: twice the converted kernel WCET. -/ +def fl4MinBudget : Ticks := 2 * fl4UsToTicks kernelWCETUs + +/-- [P] The supported timer platform has a positive minimum in ticks. +This is the FL4 counterpart of l4v's `MIN_BUDGET_pos'`. -/ +def Fl4MinBudgetPos : Prop := 0 < fl4MinBudget + +theorem fl4MinBudget_eq_l4v (h : fl4UsToTicks kernelWCETUs = usToTicks kernelWCETUs) : + fl4MinBudget = MIN_BUDGET := by + unfold fl4MinBudget MIN_BUDGET kernelWCETTicks + rw [h] + +/-- `timer::ticks_to_us` divides the full u128 product before casting the +quotient to u64. The effective boot frequency is positive. -/ +def fl4TicksToUs (ticks : Ticks) : Word64 := + BitVec.ofNat 64 (ticks.toNat * 1000000 / fl4TimerFrequencyHz.toNat) + +/-- [P] Conditional agreement with l4v's ticks-to-microseconds conversion. -/ +def Fl4TicksToUsLegacyAgree : Prop := ∀ ticks, fl4TicksToUs ticks = ticksToUs ticks + +/-- `sporadic::sufficient`, including the Reply tail's zero-usage test. -/ +def fl4RefillSufficient (usage : Ticks) (refill : Refill) : Bool := + decide (fl4MinBudget ≤ refillCapacity usage refill) + +theorem fl4RefillSufficient_eq_l4v (hmin : fl4MinBudget = MIN_BUDGET) : + fl4RefillSufficient = refillSufficient := by + unfold fl4RefillSufficient refillSufficient + rw [hmin] + +/-- FL4's `schedcontext::update_consumed` uses the full u64 tick cap. -/ +abbrev fl4MaxConsumedTicks : Ticks := maxTime + +/-- The release-time limit reserves five maximum periods in counter ticks. -/ +def fl4MaxReleaseTime : Time := maxTime - 5 * fl4UsToTicks fl4MaxPeriodUs + +set_option maxRecDepth 1024 in +theorem fl4MaxReleaseTime_eq_l4v (hmax : fl4MaxPeriodUs = MAX_PERIOD_US) + (hconv : fl4UsToTicks MAX_PERIOD_US = usToTicks MAX_PERIOD_US) : + fl4MaxReleaseTime = MAX_RELEASE_TIME := by + unfold fl4MaxReleaseTime MAX_RELEASE_TIME MAX_PERIOD + rw [hmax, hconv] + +variable {Z : Type} [StateExt Z] + +/-- Reduce the SC's consumed ticks by the capped amount and return the +converted value. The timer model supplies the cap and conversion. -/ +def schedContextUpdateConsumedWith (cap : Ticks) (toUs : Ticks → Word64) (scPtr : ObjRef) : SMonad Z Time := do + let sc ← getSchedContext scPtr + let consumed ← pure sc.scConsumed + if cap ≤ consumed then + setScObjRef scConsumedUpdate scPtr (consumed - cap) + return toUs cap + else + setScObjRef scConsumedUpdate scPtr 0 + return toUs sc.scConsumed + +theorem schedContextUpdateConsumedWith_eq_l4v (scPtr : ObjRef) : + schedContextUpdateConsumedWith (Z := Z) maxTicksToUs ticksToUs scPtr = schedContextUpdateConsumed scPtr := rfl + +/-- The SC read/reset and conversion that supplies the FL4 timeout payload. -/ +def fl4SchedContextUpdateConsumed (scPtr : ObjRef) : SMonad Z Time := + schedContextUpdateConsumedWith fl4MaxConsumedTicks fl4TicksToUs scPtr + +theorem fl4SchedContextUpdateConsumed_eq_l4v (hcap : fl4MaxConsumedTicks = maxTicksToUs) + (hconv : Fl4TicksToUsLegacyAgree) (scPtr : ObjRef) : + fl4SchedContextUpdateConsumed (Z := Z) scPtr = schedContextUpdateConsumed scPtr := by + unfold fl4SchedContextUpdateConsumed + rw [hcap, funext hconv, schedContextUpdateConsumedWith_eq_l4v] + +/-- The consumed reply's time word with an explicit platform conversion. -/ +def returnConsumedWith (cap : Ticks) (toUs : Ticks → Word64) (scPtr : ObjRef) : SMonad Z (List Data) := do + let consumed ← schedContextUpdateConsumedWith cap toUs scPtr + return wordsFromTime consumed + +theorem returnConsumedWith_eq_l4v (scPtr : ObjRef) : + returnConsumedWith (Z := Z) maxTicksToUs ticksToUs scPtr = returnConsumed scPtr := rfl + +/-- Complete a live yield by sending its time word before clearing the two links. -/ +def completeYieldToWith (cap : Ticks) (toUs : Ticks → Word64) (t : ObjRef) : SMonad Z Unit := do + let yt ← getTcbObjRef Tcb.tcbYieldTo t + NonDet.maybeM (fun sc => do + (do + let consumed ← schedContextUpdateConsumedWith cap toUs sc + replyFromKernel t (0, wordsFromTime consumed)) + schedContextCancelYieldTo t) yt + +theorem completeYieldToWith_eq_l4v (t : ObjRef) : + completeYieldToWith (Z := Z) maxTicksToUs ticksToUs t = completeYieldTo t := by + simp only [completeYieldToWith, completeYieldTo, setConsumed, schedContextUpdateConsumedWith_eq_l4v] + +/-- The activation path sends the FL4 conversion of the consumed time. -/ +def fl4CompleteYieldTo (t : ObjRef) : SMonad Z Unit := + completeYieldToWith fl4MaxConsumedTicks fl4TicksToUs t + +/-- ConfigureFlags uses FL4's platform maximum and tests converted ticks +for the minima and ordering, as in seL4 `src/object/schedcontrol.c:118–140`. +The range-error bounds are in microseconds. -/ +def decodeFl4SchedControlInvocation (label : Data) (args : List Data) (excaps : List Cap) : + SEMonad Z SchedControlInvocation := do + NonDetE.unlessE (genInvocationType label = .schedControlConfigureFlags) <| + NonDetE.throwError .illegalOperation + NonDetE.whenE (excaps.length = 0) <| NonDetE.throwError .truncatedMessage + NonDetE.whenE (args.length < timeArgLen * 2 + 3) <| NonDetE.throwError .truncatedMessage + let budgetUs ← NonDetE.returnOk <| parseTimeArgA 0 args + NonDetE.whenE (budgetUs > fl4MaxPeriodUs) <| + NonDetE.throwError (.rangeError (ucast 64 MIN_BUDGET_US) (ucast 64 fl4MaxPeriodUs)) + let periodUs ← NonDetE.returnOk <| parseTimeArgA timeArgLen args + NonDetE.whenE (periodUs > fl4MaxPeriodUs) <| + NonDetE.throwError (.rangeError (ucast 64 MIN_BUDGET_US) (ucast 64 fl4MaxPeriodUs)) + let budgetTicks := fl4UsToTicks budgetUs + let periodTicks := fl4UsToTicks periodUs + NonDetE.whenE (budgetTicks < fl4MinBudget) <| + NonDetE.throwError (.rangeError (ucast 64 MIN_BUDGET_US) (ucast 64 fl4MaxPeriodUs)) + NonDetE.whenE (periodTicks < fl4MinBudget) <| + NonDetE.throwError (.rangeError (ucast 64 MIN_BUDGET_US) (ucast 64 fl4MaxPeriodUs)) + NonDetE.whenE (periodTicks < budgetTicks) <| + NonDetE.throwError (.rangeError (ucast 64 MIN_BUDGET_US) (ucast 64 periodUs)) + let targetCap ← NonDetE.returnOk <| the excaps.head? + NonDetE.whenE (¬isSchedContextCap targetCap) <| NonDetE.throwError (.invalidCapability 1) + let scPtr ← NonDetE.returnOk <| objRefOf targetCap + NonDetE.assertE (MIN_REFILLS ≤ maxRefillsCap targetCap) + let extraRefills ← NonDetE.returnOk <| the args[2 * timeArgLen]? + NonDetE.whenE (unat extraRefills > maxRefillsCap targetCap - MIN_REFILLS) <| + NonDetE.throwError (.rangeError 0 (BitVec.ofNat 64 (maxRefillsCap targetCap - MIN_REFILLS))) + let badge ← NonDetE.returnOk <| the args[2 * timeArgLen + 1]? + let flags ← NonDetE.returnOk <| the args[2 * timeArgLen + 2]? + NonDetE.returnOk <| .invokeSchedControlConfigureFlags scPtr + budgetTicks periodTicks (unat extraRefills + MIN_REFILLS) badge flags + +omit [StateExt Z] in +private theorem returnOk_bind {α β : Type} (x : α) (f : α → SEMonad Z β) : + (NonDetE.returnOk x >>= f) = f x := by + have hp {γ δ : Type} (y : γ) (g : γ → NonDet (State Z) δ) : + NonDet.bind (NonDet.pure y) g = g y := by + funext s + simp only [NonDet.bind, NonDet.pure, Set.mem_singleton_iff, exists_eq_left, false_or] + ext <;> simp + show ExceptT.mk (NonDet.bind (NonDet.pure (.ok x)) (ExceptT.bindCont f)) = f x + rw [hp] + rfl + +/-- The FL4 decoder agrees with l4v when the maximum and the three time +comparisons and the converted values agree for the arguments being decoded. -/ +theorem decodeFl4SchedControlInvocation_eq_l4v (label : Data) (args : List Data) (excaps : List Cap) + (hmax : fl4MaxPeriodUs = MAX_PERIOD_US) + (hbudgetTicks : fl4UsToTicks (parseTimeArgA 0 args) = usToTicks (parseTimeArgA 0 args)) + (hperiodTicks : fl4UsToTicks (parseTimeArgA timeArgLen args) = usToTicks (parseTimeArgA timeArgLen args)) + (hbudget : fl4UsToTicks (parseTimeArgA 0 args) < fl4MinBudget ↔ + parseTimeArgA 0 args < MIN_BUDGET_US) + (hperiod : fl4UsToTicks (parseTimeArgA timeArgLen args) < fl4MinBudget ↔ + parseTimeArgA timeArgLen args < MIN_BUDGET_US) + (horder : fl4UsToTicks (parseTimeArgA timeArgLen args) < fl4UsToTicks (parseTimeArgA 0 args) ↔ + parseTimeArgA timeArgLen args < parseTimeArgA 0 args) : + decodeFl4SchedControlInvocation (Z := Z) label args excaps = + decodeSchedControlInvocationFlags label args excaps := by + unfold decodeFl4SchedControlInvocation decodeSchedControlInvocationFlags + rw [hmax] + simp only [returnOk_bind, NonDetE.whenE, hbudget, hperiod, horder] + simp only [hbudgetTicks, hperiodTicks] + +end + +end Fl4SpecA64 diff --git a/proofs/Fl4SpecA64/Ext/SchedTail.lean b/proofs/Fl4SpecA64/Ext/SchedTail.lean new file mode 100644 index 000000000..817cda9ae --- /dev/null +++ b/proofs/Fl4SpecA64/Ext/SchedTail.lean @@ -0,0 +1,160 @@ +import Fl4SpecA64.Ext.SchedBudget +import Fl4SpecA64.Ext.Timer + +/-! +FL4 schedule and the reprogram decision. The timer view supplies ARMED +and the earliest custom TCB timer from the local core. A deadline +request carries the logical time. The platform adapter handles the +physical compare value for a logical zero. +-/ + +namespace Fl4SpecA64 + +open Fl4Spec Classical + +noncomputable section + +structure Fl4TimerView where + armed : Time + earliest : Time + +def fl4Deadline (now amount : Time) (domainTime release : Option Time) + (precision earliest : Time) : Time := + let expiry := now + amount + let expiry := match domainTime with + | none => expiry + | some left => if now + left < expiry then now + left else expiry + let expiry := match release with + | none => expiry + | some time => if time < expiry then time else expiry + let deadline := expiry - precision + if earliest ≠ 0 ∧ earliest < deadline then earliest else deadline + +abbrev fl4KeepsDeadline (now armed deadline : Time) (isIdle : Bool) : Prop := + now < armed ∧ (armed = deadline ∨ (armed < deadline ∧ isIdle = false)) + +def fl4ProgrammedDeadline (deadline : Time) : Time := + if deadline = 0 then 1 else deadline + +/-- Compare the concrete advance against the allocation limit before +reading the zero-duration sentinel. -/ +def fl4NextDomainIndex (limit index start : Nat) (domains : List (Domain × DomainDuration)) : Nat := + let advance := (index + 1) % 2^64 + if advance ≥ limit then start + else if ((domains[advance]?).map Prod.snd).getD 0 = 0 then start else advance + +section +variable {Z : Type} [StateExt Z] + +def fl4NextDomain (limit : Nat) : SMonad Z Unit := do + NonDet.modify fun s => + let index := fl4NextDomainIndex limit s.domainIndex s.domainStartIndex s.domainList + let entry := the s.domainList[index]? + { s with + domainIndex := index + curDomain := entry.1 + domainTime := ucast 64 entry.2 + reprogramTimer := true } + doExtendedOp <| NonDet.modify fun s => workUnitsCompletedUpdate (fun _ => 0) s + +def fl4ReleaseDeadline (queue : List ObjRef) : SMonad Z (Option Time) := + match queue.head? with + | none => pure none + | some t => do + let sc ← getTcbObjRef Tcb.tcbSchedContext t >>= NonDet.assertOpt + let head ← getRefillHead sc + pure (some head.rTime) + +def fl4SetNextInterrupt (timer : State Z → Fl4TimerView) : SMonad Z Unit := do + let s ← NonDet.get + let sc ← getTcbObjRef Tcb.tcbSchedContext s.curThread >>= NonDet.assertOpt + let head ← getRefillHead sc + let release ← fl4ReleaseDeadline s.releaseQueue + let domain := if numDomains > 1 then some s.domainTime else none + let inputs := timer s + let deadline := fl4Deadline s.curTime head.rAmount domain release (fl4UsToTicks 1) inputs.earliest + unless fl4KeepsDeadline s.curTime inputs.armed deadline (decide (s.curThread = s.idleThread)) do + doMachineOp <| setDeadline deadline + +def fl4SwitchSchedContext : SMonad Z Unit := do + let s ← NonDet.get + let target ← getTcbObjRef Tcb.tcbSchedContext s.curThread >>= NonDet.assertOpt + NonDet.when (target ≠ s.curSc) do + NonDet.modify fun r => { r with reprogramTimer := true } + ifConstantBandwidthRefillUnblockCheck (some target) + let reprogram ← NonDet.gets fun r => r.reprogramTimer + NonDet.when reprogram fl4CommitTime + NonDet.modify fun r => { r with curSc := target } + +def fl4ScAndTimer (timer : State Z → Fl4TimerView) : SMonad Z Unit := do + fl4SwitchSchedContext + let reprogram ← NonDet.gets fun s => s.reprogramTimer + NonDet.when reprogram do + fl4SetNextInterrupt timer + NonDet.modify fun s => { s with reprogramTimer := false } + +/-- Execution changes the chosen TCB and its ready membership. The +ordered hardware root trace is handled by the architecture boundary. -/ +def fl4SwitchToThread (t : ObjRef) : SMonad Z Unit := do + tcbSchedAction tcbSchedDequeue t + NonDet.modify fun s => { s with curThread := t } + +def fl4ChooseThread : SMonad Z Unit := do + let s ← NonDet.get + let q := s.readyQueues s.curDomain + if ∀ prio, q prio = [] then + NonDet.modify fun r => { r with curThread := s.idleThread } + else fl4SwitchToThread (the (maxNonEmptyQueue q).head?) + +def fl4ChooseNewThread (limit : Nat) : SMonad Z Unit := do + let domTime ← NonDet.gets fun s => s.domainTime + NonDet.when (domTime = 0) (fl4NextDomain limit) + fl4ChooseThread + +/-- Select a pending switch target using the queues after the outgoing +thread has been offered to its ready queue. -/ +def fl4SelectCandidate (limit : Nat) (candidate current idle : ObjRef) + (domain : Domain) (wasRunnable : Bool) : SMonad Z Unit := do + let prio ← threadGet Tcb.tcbPriority candidate + let currentPrio ← if current = idle then pure 0 else threadGet Tcb.tcbPriority current + let fastFail := current = idle ∨ prio < currentPrio + let highest ← NonDet.gets (isHighestPrio domain prio) + if fastFail ∧ ¬highest then + tcbSchedAction tcbSchedEnqueue candidate + setSchedulerAction .chooseNewThread + fl4ChooseNewThread limit + else if wasRunnable ∧ prio = currentPrio then + tcbSchedAction tcbSchedAppend candidate + setSchedulerAction .chooseNewThread + fl4ChooseNewThread limit + else fl4SwitchToThread candidate + +def fl4SelectAction (limit : Nat) : SMonad Z Unit := do + let s ← NonDet.get + NonDet.when (s.schedulerAction ≠ .resumeCurThread) do + let wasRunnable := schedulable s.curThread s + NonDet.when wasRunnable (tcbSchedAction tcbSchedEnqueue s.curThread) + match s.schedulerAction with + | .resumeCurThread => pure () + | .chooseNewThread => fl4ChooseNewThread limit + | .switchThread candidate => + fl4SelectCandidate limit candidate s.curThread s.idleThread s.curDomain wasRunnable + +def fl4SelectThread (limit : Nat) : SMonad Z Unit := do + fl4SelectAction limit + setSchedulerAction .resumeCurThread + +/-- Local schedule with the silent pending-IPI tail. TCB timers run once +before the release queue is traversed. -/ +def fl4Schedule (limit : Nat) (timer : State Z → Fl4TimerView) : SMonad Z Unit := do + fl4Awaken + awaken + checkDomainTime + fl4SelectThread limit + fl4ScAndTimer timer + +end + +end + +end Fl4SpecA64 diff --git a/proofs/Fl4SpecA64/Ext/Stage2.lean b/proofs/Fl4SpecA64/Ext/Stage2.lean index 1b6d10e73..58f1b5241 100644 --- a/proofs/Fl4SpecA64/Ext/Stage2.lean +++ b/proofs/Fl4SpecA64/Ext/Stage2.lean @@ -45,6 +45,15 @@ or an `ArmHighVSpace` (`decode_page_flush`), with FL4's "still mapped" and frame (a Stage-2 one is `IllegalOperation`, a high-window one walks the window's tables). +**Stage-2 frame caps** (generation 6, Architect's ruling (b′)): a frame +mapped by `decodeS2PageMap` is the cap `stage2FrameCap` (FL4's +`Cap::Frame { is_ept: true }`); the frame invocations, `ARMPageUnmap` and +finalisation dispatch on it as FL4 on `is_ept` (`decodeExtPageFlush`, +`decodeExtTestAndClearAccessed`, `performExtVSpace`, `Ext/Finalise`). A +plain frame cap whose ASID names a Stage-2 root is no Stage-2 mapping (FL4 +`unmap_page` / `frame_root` find no root for it). In a high window a frame's +address outside `HIGH_L0_INDEX` maps nothing (`extVaOk`). + The invocations decode into `ExtVSpaceInvocation` (`performExtVSpace`). Not modelled (no abstract effect): the TLB invalidations by VMID and the @@ -186,10 +195,18 @@ def storeExtSlot (sl : ExtSlot) (pte : PTE) : SMonad Z Unit := setObject r (.archObj (.ext (o.setEntry i pte))) | .pt p => storePte .normalPtT p pte +/-- The root the ASID's pool entry names, of any kind (`InvalidRoot` if +none): l4v's `find_vspace_for_asid` without the generation-7 hook, which +hides FL4's roots from l4v's Stage-1 code (`AARCH64/VSpace`); FL4's +`find_asid_raw`. -/ +def findAnyRootForAsid (asid : Asid) : LFMonad Z ObjRef := do + let vspaceOpt ← NonDetE.liftE (NonDet.gets (vspaceForAsid asid)) + NonDetE.throwOpt .invalidRoot vspaceOpt + /-- **`ept::find` / `find_high_asid`**: the root of kind `k` the ASID names. -/ def findExtRoot (k : ExtRootKind) (asid : Asid) : LFMonad Z ObjRef := do - let r ← findVspaceForAsid asid + let r ← findAnyRootForAsid asid let kind ← NonDetE.liftE <| NonDet.gets fun s : State Z => rootKindAt s r NonDetE.unlessE (kind = k) <| NonDetE.throwError .invalidRoot NonDetE.returnOk r @@ -198,6 +215,7 @@ def findExtRoot (k : ExtRootKind) (asid : Asid) : LFMonad Z ObjRef := do def ExtArchCap.rootKind : ExtArchCap → ExtRootKind | .stage2VSpaceCap _ _ => .stage2 | .highVSpaceCap _ _ => .high + | .stage2FrameCap _ _ _ _ _ => .stage2 /-- **`ept::root` / `check_high_vspace`**: an assigned FL4 root cap whose ASID still names it (`InvalidCapability argNo` otherwise; a failed ASID lookup @@ -244,15 +262,31 @@ def ExtRootKind.geom (k : ExtRootKind) : ExtGeom := /-! ## Unmapping -/ +/-- FL4: the level-0 index of the high window (`vspace.rs` `HIGH_L0_INDEX`). -/ +def highL0Index : Nat := 511 + +/-- **The high-window address guard** (generation 6): FL4 walks a frame's +address in an `ArmHighVSpace` from the window's level-0 table, whose only +entry not the kernel's is `HIGH_L0_INDEX` (`init_high_vspace`); the model's +walk starts at the level-1 table (`ExtRootKind.high.geom`, which reads +only bits 12..38). An address of another level-0 index maps nothing of the +window: the model's high-window lookups of a frame (`extUnmapPage`, +`decodeExtPageFlush`, `decodeExtTestAndClearAccessed`) find nothing there. +A Stage-2 walk reads the whole IPA, so no guard. -/ +def extVaOk (k : ExtRootKind) (va : Word) : Prop := + k ≠ .high ∨ (va.toNat >>> 39) % 512 = highL0Index + section variable {Z : Type} [StateExt Z] /-- **`ept::unmap` / `unmap_page` in a high window**: clear the page or block descriptor of `pptr` at `va` in the FL4 root the ASID names, if it -still maps it (otherwise nothing). -/ +still maps it (otherwise nothing; in a high window, nothing outside +`HIGH_L0_INDEX`, `extVaOk`). -/ def extUnmapPage (k : ExtRootKind) (pgsz : VMPageSize) (asid : Asid) (va : VSpaceRef) (pptr : ObjRef) : SMonad Z Unit := NonDetE.catch (do + NonDetE.unlessE (extVaOk k va) <| NonDetE.throwError .invalidRoot let r ← findExtRoot k asid let pos ← NonDetE.liftE <| NonDet.gets fun s : State Z => extLookupSlot s k.geom r va NonDetE.unlessE (pos.shift = pageBitsForSize pgsz) <| NonDetE.throwError .invalidRoot @@ -331,86 +365,116 @@ def extMappedEntry {Z : Type} (s : State Z) (k : ExtRootKind) (r : ObjRef) (p : if pos.shift = pageBitsForSize sz ∧ pte.isPagePTE = true ∧ pptrFromPte pte = p then some (pos, pte) else none -/-- **The frame cache invocations of a frame mapped in an FL4 root** (FL4 -`vspace.rs` `decode_page_flush`: for a Stage-2 frame, `f.is_ept`, +/-- **The frame cache invocations of a frame mapped in an FL4 root of kind +`k`** (FL4 `vspace.rs` `decode_page_flush`: for a Stage-2 frame, `ept::flush_frame`; for a high-window frame, its `frame_root` path), in place of l4v's `decode_fr_inv_flush`: the arguments (`TruncatedMessage`), an assigned cap (`IllegalOperation`), `start < end` (`InvalidArgument 1`) within the frame (`InvalidArgument 0`); the ASID's root (`FailedLookup`); -the mapping still in place at the cap's size (`InvalidCapability 0`); for -`Invalidate`, writable (`IllegalOperation`). The flush is l4v's -`perform_flush` over the frame's range; FL4 does no cache maintenance on a -device frame in a Stage-2 VSpace. `label` is l4v's flush label. -/ -def decodeExtPageFlush (k : ExtRootKind) (label : Data) (args : List Data) (cap : ArchCap) : +the mapping still in place at the cap's size (`InvalidCapability 0`; none +in a high window outside `HIGH_L0_INDEX`, `extVaOk`); for `Invalidate`, +writable (`IllegalOperation`). The flush is l4v's `perform_flush` over the +frame's range; FL4 does no cache maintenance on a device frame in a +Stage-2 VSpace. `label` is l4v's flush label. -/ +def decodeExtPageFlushAt (k : ExtRootKind) (label : Data) (args : List Data) (p : ObjRef) + (sz : VMPageSize) (dev : Bool) (mapped : Option (Asid × VSpaceRef)) : + SEMonad Z ExtVSpaceInvocation := do + NonDetE.whenE (args.length < 2) <| NonDetE.throwError .truncatedMessage + let (asid, va) ← match mapped with + | some (asid, va) => if asid = 0 then NonDetE.throwError .illegalOperation else NonDetE.returnOk (asid, va) + | none => NonDetE.throwError .illegalOperation + let start := the args[0]? + let «end» := the args[1]? + NonDetE.whenE («end» ≤ start) <| NonDetE.throwError (.invalidArgument 1) + let size : Word := (1 : Word) <<< pageBitsForSize sz + NonDetE.whenE (start ≥ size ∨ «end» > size) <| NonDetE.throwError (.invalidArgument 0) + let r ← lookupErrorOnFailure false <| findExtRoot k asid + let me ← NonDetE.liftE <| NonDet.gets fun s : State Z => + if extVaOk k va then extMappedEntry s k r p sz va else none + match me with + | none => NonDetE.throwError (.invalidCapability 0) + | some (_, pte) => do + NonDetE.whenE (labelToFlushType label = .invalidate ∧ pte.pteRights ≠ vmReadWrite) <| + NonDetE.throwError .illegalOperation + if k = .stage2 ∧ dev then NonDetE.returnOk (.flush .vspaceNothing) + else NonDetE.returnOk <| .flush <| + .vspaceFlush (labelToFlushType label) (va + start) (va + «end» - 1) (addrFromPPtr p + start) r asid + +/-- **The frame cache invocations of a frame whose ASID names an FL4 root** +(generation 6: by the cap's kind, FL4's `is_ept`): a Stage-2 frame cap is +`ept::flush_frame` (`decodeExtPageFlushAt .stage2`); a plain frame cap is +the `frame_root` path, whose root must be a high window (a Stage-2 root is +`FailedLookup` `InvalidRoot`, as `findExtRoot .high` gives it). -/ +def decodeExtPageFlush (label : Data) (args : List Data) (cap : ArchCap) : SEMonad Z ExtVSpaceInvocation := match cap with - | .frameCap p _ sz dev mapped => do - NonDetE.whenE (args.length < 2) <| NonDetE.throwError .truncatedMessage - let (asid, va) ← match mapped with - | some (asid, va) => if asid = 0 then NonDetE.throwError .illegalOperation else NonDetE.returnOk (asid, va) - | none => NonDetE.throwError .illegalOperation - let start := the args[0]? - let «end» := the args[1]? - NonDetE.whenE («end» ≤ start) <| NonDetE.throwError (.invalidArgument 1) - let size : Word := (1 : Word) <<< pageBitsForSize sz - NonDetE.whenE (start ≥ size ∨ «end» > size) <| NonDetE.throwError (.invalidArgument 0) - let r ← lookupErrorOnFailure false <| findExtRoot k asid - let me ← NonDetE.liftE <| NonDet.gets fun s : State Z => extMappedEntry s k r p sz va - match me with - | none => NonDetE.throwError (.invalidCapability 0) - | some (_, pte) => do - NonDetE.whenE (labelToFlushType label = .invalidate ∧ pte.pteRights ≠ vmReadWrite) <| - NonDetE.throwError .illegalOperation - if k = .stage2 ∧ dev then NonDetE.returnOk (.flush .vspaceNothing) - else NonDetE.returnOk <| .flush <| - .vspaceFlush (labelToFlushType label) (va + start) (va + «end» - 1) (addrFromPPtr p + start) r asid + | .ext (.stage2FrameCap p _ sz dev mapped) => decodeExtPageFlushAt .stage2 label args p sz dev mapped + | .frameCap p _ sz dev mapped => decodeExtPageFlushAt .high label args p sz dev mapped | _ => NonDetE.liftE NonDet.fail -/-- **`ARMPageTestAndClearAccessed` of a frame mapped in an FL4 root** (FL4 -`vspace.rs` `decode_test_and_clear_accessed`), in place of `Ext/VSpace`'s -l4v-table walk: a Stage-2 frame (`f.is_ept`), a huge frame, ASID 0 or no -hardware access-flag updates is `IllegalOperation`; the root of a -high-window frame (`frame_root`) and its mapping still in place -(`mapped_entry`), else `InvalidCapability 0`. The result is the frame -(`performTestAndClearAccessed`: either answer). -/ -def decodeExtTestAndClearAccessed (k : ExtRootKind) (cap : ArchCap) : SEMonad Z ObjRef := +/-- **`ARMPageTestAndClearAccessed` of a frame whose ASID names an FL4 root** +(FL4 `vspace.rs` `decode_test_and_clear_accessed`), in place of +`Ext/VSpace`'s l4v-table walk: a Stage-2 frame cap (`f.is_ept`), an +unmapped one, a huge frame, ASID 0 or no hardware access-flag updates is +`IllegalOperation`; then the root (`frame_root`: a high window, else +`InvalidCapability 0`) and the mapping still in place (`mapped_entry`; none +outside `HIGH_L0_INDEX`), else `InvalidCapability 0`. The result is the +frame (`performTestAndClearAccessed`: either answer). -/ +def decodeExtTestAndClearAccessed (cap : ArchCap) : SEMonad Z ObjRef := match cap with | .frameCap p _ sz _ (some (asid, va)) => - if k = .stage2 ∨ sz = .armHugePage ∨ asid = 0 ∨ fl4A64Platform.hwAccessFlag = false + if sz = .armHugePage ∨ asid = 0 ∨ fl4A64Platform.hwAccessFlag = false then NonDetE.throwError .illegalOperation else do - let r ← NonDetE.handleE (findExtRoot k asid) fun _ => NonDetE.throwError (.invalidCapability 0) - let me ← NonDetE.liftE <| NonDet.gets fun s : State Z => extMappedEntry s k r p sz va + let r ← NonDetE.handleE (findExtRoot .high asid) fun _ => NonDetE.throwError (.invalidCapability 0) + let me ← NonDetE.liftE <| NonDet.gets fun s : State Z => + if extVaOk .high va then extMappedEntry s .high r p sz va else none match me with | some _ => NonDetE.returnOk p | none => NonDetE.throwError (.invalidCapability 0) | _ => NonDetE.throwError .illegalOperation +/-- **`ept::map_frame`**'s checks and result for the frame `p` (rights +`R`, size `pgsz`, device `dev`, current mapping `mapped`) into the Stage-2 +root `r` of ASID `asid` at the IPA `vaddr`: an unmapped frame comes out a +plain frame cap when the rights mask leaves no rights (FL4 `e == 0`: +`mapped: false, is_ept: false`), else a Stage-2 frame cap (`is_ept: true`). -/ +def decodeS2PageMapAt (cte : CslotPtr) (p : ObjRef) (R : CapRights) (pgsz : VMPageSize) (dev : Bool) + (mapped : Option (Asid × VSpaceRef)) (r : ObjRef) (asid : Asid) (vaddr rightsMask attr : Data) : + SEMonad Z ExtVSpaceInvocation := do + checkVpAlignment pgsz vaddr + let shift := pageBitsForSize pgsz + match mapped with + | some (asid', vaddr') => do + NonDetE.whenE (asid' ≠ asid) <| NonDetE.throwError (.invalidCapability 1) + NonDetE.whenE (vaddr' ≠ vaddr) <| NonDetE.throwError (.invalidArgument 0) + | none => do + let last := vaddr + mask shift + NonDetE.whenE (last > s2IpaLimit ∨ last < vaddr) <| NonDetE.throwError (.invalidArgument 0) + let pos ← NonDetE.liftE <| NonDet.gets fun s : State Z => extLookupSlot s s2Geom r vaddr + NonDetE.unlessE (pos.shift = shift) <| + NonDetE.throwError (.failedLookup false (.missingCapability pos.shift)) + let vm := maskVmRights R (dataToRights rightsMask) + if vm = ∅ then + NonDetE.returnOk <| .map (.frameCap p R pgsz dev none) cte .invalidPTE pos.slot + else + NonDetE.returnOk <| .map (.ext (.stage2FrameCap p R pgsz dev (some (asid, vaddr)))) cte + (makeUserPte (addrFromPPtr p) (attribsFromWord attr) vm R pgsz) pos.slot + /-- **`ept::map_frame`** (`ARMPageMap` into a Stage-2 VSpace): `vcap` is the -extra cap, `vaddr` the IPA. -/ +extra cap, `vaddr` the IPA. A plain frame cap mapped elsewhere is +`InvalidCapability 1` (FL4 `decode_page_map`: `f.asid != 0 && !f.is_ept`); +a Stage-2 frame cap may be remapped at its own ASID and IPA. -/ def decodeS2PageMap (cte : CslotPtr) (frame : ArchCap) (vcap : Cap) (vaddr rightsMask attr : Data) : SEMonad Z ExtVSpaceInvocation := match frame with | .frameCap p R pgsz dev mapped => do let (r, asid) ← checkExtRoot .stage2 vcap 1 - checkVpAlignment pgsz vaddr - let shift := pageBitsForSize pgsz - match mapped with - | some (asid', vaddr') => do - NonDetE.whenE (asid' ≠ asid) <| NonDetE.throwError (.invalidCapability 1) - NonDetE.whenE (vaddr' ≠ vaddr) <| NonDetE.throwError (.invalidArgument 0) - | none => do - let last := vaddr + mask shift - NonDetE.whenE (last > s2IpaLimit ∨ last < vaddr) <| NonDetE.throwError (.invalidArgument 0) - let pos ← NonDetE.liftE <| NonDet.gets fun s : State Z => extLookupSlot s s2Geom r vaddr - NonDetE.unlessE (pos.shift = shift) <| - NonDetE.throwError (.failedLookup false (.missingCapability pos.shift)) - let vm := maskVmRights R (dataToRights rightsMask) - if vm = ∅ then - NonDetE.returnOk <| .map (.frameCap p R pgsz dev none) cte .invalidPTE pos.slot - else - NonDetE.returnOk <| .map (.frameCap p R pgsz dev (some (asid, vaddr))) cte - (makeUserPte (addrFromPPtr p) (attribsFromWord attr) vm R pgsz) pos.slot + NonDetE.whenE (mapped ≠ none) <| NonDetE.throwError (.invalidCapability 1) + decodeS2PageMapAt cte p R pgsz dev none r asid vaddr rightsMask attr + | .ext (.stage2FrameCap p R pgsz dev mapped) => do + let (r, asid) ← checkExtRoot .stage2 vcap 1 + decodeS2PageMapAt cte p R pgsz dev mapped r asid vaddr rightsMask attr | _ => NonDetE.liftE NonDet.fail /-- **`ept::map_table`** (`ARMPageTableMap` into a Stage-2 VSpace; the @@ -438,7 +502,7 @@ def decodeExtAsidPoolAssign (pool : ArchCap) (extraCaps : List (Cap × CslotPtr) | (vcap, vslot) :: _ => match vcap with | .archObjectCap (.ext c) => - if c.mapData = none then do + if c.isRoot ∧ c.mapData = none then do let p := pool.acapObj let base := pool.acapAsidBase let asidTable ← NonDetE.liftE <| NonDet.gets fun s : State Z => asidTable s @@ -453,8 +517,16 @@ def decodeExtAsidPoolAssign (pool : ArchCap) (extraCaps : List (Cap × CslotPtr) else NonDetE.throwError (.invalidCapability 1) | _ => NonDetE.throwError (.invalidCapability 1) +/-- FL4 `unmapped_frame`: the cap of an unmapped plain frame (`mapped: +false, is_ept: false`). -/ +def extUnmappedFrame : ArchCap → ArchCap + | .ext (.stage2FrameCap p R sz d _) => .frameCap p R sz d none + | c => updateMapData c none + /-- **The effects** (FL4's `slot.set_cap` then the descriptor write; the -unmaps of `ARMPageUnmap` / `ARMPageTableUnmap`, the latter clearing the +unmaps of `ARMPageUnmap` (by the cap's kind: a Stage-2 frame cap in its +Stage-2 root, a plain one only in a high window; the cap left an unmapped +plain frame, `unmapped_frame`) / `ARMPageTableUnmap`, the latter clearing the table, FL4 `s.zero(ptr, 4096)`; FL4's ASID assignment, the pool entry naming the root). -/ def performExtVSpace (i : ExtVSpaceInvocation) : SMonad Z (List Data) := @@ -465,9 +537,10 @@ def performExtVSpace (i : ExtVSpaceInvocation) : SMonad Z (List Data) := return [] | .pageUnmap k cap slot => do match cap with - | .frameCap p _ sz _ (some (asid, va)) => extUnmapPage k sz asid va p + | .ext (.stage2FrameCap p _ sz _ (some (asid, va))) => extUnmapPage .stage2 sz asid va p + | .frameCap p _ sz _ (some (asid, va)) => NonDet.when (k = .high) <| extUnmapPage .high sz asid va p | _ => pure () - setCap (.archObjectCap (updateMapData cap none)) slot + setCap (.archObjectCap (extUnmappedFrame cap)) slot return [] | .tableUnmap k cap slot => do match cap with diff --git a/proofs/Fl4SpecA64/Ext/Tcb.lean b/proofs/Fl4SpecA64/Ext/Tcb.lean index 91bd0c27f..ed7e26a83 100644 --- a/proofs/Fl4SpecA64/Ext/Tcb.lean +++ b/proofs/Fl4SpecA64/Ext/Tcb.lean @@ -1,48 +1,15 @@ /- -FL4 difference (Fl4SpecA64/PORTING.md, "Ext boundary"): **TCB extensions on -AArch64**, the TCB invocations of FL4 that l4v's `decode_tcb_invocation` -does not have, modelled on FL4's Rust (`crates/fl4-core/src/tcb.rs`, -`crates/fl4-core/src/arch/aarch64/tcb.rs`). The x86 port's -`Fl4Spec/Ext/Tcb` with the AArch64 differences: no debug API (FL4's AArch64 -build has no `HARDWARE_DEBUG_API`), a 520-byte FPU image, l4v's AArch64 -FPU state, and the AArch64 labels `TCBSetPACKeys` and `TCBSetHighVSpace`. - -1. l4h 0002: `TCBReadFPU` (to a frame) and `TCBWriteFPU` (from a frame): - the first extra cap, a non-device frame cap that is not kernel-only (and - read-write for ReadFPU), and an offset in it, 64-byte aligned, with room - for the FPU image (`FPU_STATE_SIZE`, `64 * 8 + 8` = 520 bytes: V0..V31, - FPSR, FPCR). FL4 first saves the target's live FPU registers into its TCB - (`Arch::fpu_flush`; the registers and the owner are left as they were), - which is l4v's `save_fpu_state` when the target is the FPU owner. ReadFPU - then writes the 520 bytes at the frame offset (`fl4FpuToFrame`: only those - bytes change, their values are left open, since the byte layout of l4v's - `fpu_state` is not modelled). WriteFPU sets the target's saved FPU state - (l4v's `user_context` keeps it) to the frame's image (any value here, the - decoding is not modelled), and reloads the registers when the target owns - the FPU (`fpu_reload`, l4v's `load_fpu_state`). -2. l4h 0003: `TCBSetTimer` (two arguments): a deadline and a badge for the - thread's TCB timer (`Ext/Timer`; the timer queue is not modelled: the - invocation may set `reprogram_timer`, never clears it). -3. FL4's `TCBSetFlags`: l4v's, except that the flags word is FL4's (the - `TRAP_SYSCALLS` bit of l4h 0001 is not one of l4v's `TcbFlag`s, so it is - not kept, and the reply's word is any word of the new flags), and FL4 - does not restore the FPU lazily when the FPU is enabled - (`arch_post_set_flags`'s `lazy_fpu_restore`). -4. FL4 AArch64: `TCBSetPACKeys(flags, ten key words)`: eleven words - (`TruncatedMessage`); flags within `pac_flag::MASK` (`InvalidArgument - 0`); non-zero flags only with pointer authentication - (`Fl4A64Platform.hasPauth`, else `IllegalOperation`). The keys, the - enables and the key epoch are TCB state that l4v's `arch_tcb` does not - have: the invocation changes no modelled state. -5. FL4 AArch64: `TCBSetHighVSpace` binds an `ArmHighVSpace` cap in the - seventh TCB slot (6): `Ext/ArmHighVSpace` (generation 4); the - dispatcher does not route it here. - -The ported modules are not changed. +FL4's AArch64 TCB invocation extensions. See PORTING.md for the comparison +with the ported TCB operations and seL4. These definitions use the existing +architecture cap, FPU state, timer, and MCS budget types. -/ import Fl4SpecA64.Decode +import Fl4SpecA64.Ext.FaultReply import Fl4SpecA64.Ext.Platform import Fl4SpecA64.Ext.Label +import Fl4SpecA64.Ext.SchedControl +import Fl4SpecA64.Ext.Timer +import Fl4SpecA64.Ext.SchedBudget namespace Fl4SpecA64 @@ -65,14 +32,50 @@ def fl4FpuStateSize : Nat := 64 * 8 + 8 /-- `fl4_abi::consts::pac_flag::MASK`. -/ def fl4PacFlagMask : Data := 0xf -/-- **FL4's ReadFPU write of the FPU image into the user frame**: only -`underlyingMemory` changes, at the addresses whose offset from `frame` -(modulo `2^64`) is below `fl4FpuStateSize`; the values are left open. -/ -def fl4FpuToFrame (frame : Word) : MachineMonad Unit := do - let um ← NonDet.gets MachineState.underlyingMemory - let um' ← NonDet.select - {um' : Word → Word8 | ∀ a : Word, fl4FpuStateSize ≤ (a - frame).toNat → um' a = um a} - NonDet.modify fun ms => { ms with underlyingMemory := um' } +/-- A native or Stage-2 frame has the same object, geometry, and rights for +TCB operations. Mapping metadata does not participate. -/ +def fl4TcbFrame : Cap → Option (ObjRef × CapRights × VMPageSize × Bool) + | .archObjectCap c => + if c.isFrameCap then some (c.acapObj, c.acapRights, c.acapFsize, archCapIsDevice c) else none + | _ => none + +/-- The slot replacement frame test uses exact object identity in either +mapping kind. For other cap families it uses the existing object test. -/ +def fl4TcbSameObject (cap live : Cap) : Prop := + match fl4TcbFrame cap, fl4TcbFrame live with + | some (p, _, sz, dev), some (q, _, sz', dev') => p = q ∧ sz = sz' ∧ dev = dev' + | some _, none => False + | none, _ => sameObjectAs cap live + +/-- AArch64 saves 64 little-endian vector words followed by FPSR/FPCR. -/ +def fl4FpuToFrame (frame : Word) (image : FpuState) : MachineMonad Unit := do + NonDet.mapMx (fun i : Fin fpuRegsLen => + storeWord (frame + BitVec.ofNat 64 (8 * i.val)) (image.fpuRegs i)) (List.finRange fpuRegsLen) + storeWord (frame + 512) + (BitVec.ofNat 64 (image.fpuSr.toNat + image.fpuCr.toNat * 2 ^ 32)) + +/-- FL4 sanitises the two 32-bit control words of the user-written image. -/ +def fl4FpuFromFrame (frame : Word) : MachineMonad FpuState := do + let words ← NonDet.mapM (fun i => loadWord (frame + BitVec.ofNat 64 (8 * i))) (List.range fpuRegsLen) + let control ← loadWord (frame + 512) + return ⟨fun i => words.getD i.val 0, + BitVec.ofNat 32 control.toNat &&& 0xf800009f, + BitVec.ofNat 32 (control.toNat / 2 ^ 32) &&& 0x07ff9f07⟩ + +/-- FL4's raw flags word. Its ported projection forgets TRAP_SYSCALLS. -/ +def fl4TcbFlagsWord (old clear set : Word) : Word := (old &&& ~~~clear) ||| (set &&& 3) + +/-- The local per-core deadline queue. The order among existing equal deadlines +is retained. FL4 removes the old target entry first, then inserts after all +existing entries with deadline ≤ the supplied deadline. -/ +def fl4SetTcbTimer (queue : List (ObjRef × Fl4TcbTimer)) (target : ObjRef) + (deadline : Time) (badge : Data) : List (ObjRef × Fl4TcbTimer) × Bool := + let rest := queue.filter fun e => e.1 != target + if deadline = 0 then (rest, false) + else + let before := fun e : ObjRef × Fl4TcbTimer => decide (e.2.deadline ≤ deadline) + let earlier := rest.takeWhile before + (earlier ++ [(target, ⟨deadline, badge⟩)] ++ rest.dropWhile before, earlier.isEmpty) section variable {Z : Type} [StateExt Z] @@ -96,8 +99,8 @@ def decodeFl4FpuTransfer (toFrame : Bool) (args : List Data) (t : ObjRef) (excaps : List (Cap × CslotPtr)) : SEMonad Z Fl4TcbInvocation := do NonDetE.whenE (excaps = [] ∨ args.length < 1) <| NonDetE.throwError .truncatedMessage let offset ← NonDetE.returnOk <| the args[0]? - match (the excaps[0]?).1 with - | .archObjectCap (.frameCap base rights sz dev _) => + match fl4TcbFrame (the excaps[0]?).1 with + | some (base, rights, sz, dev) => if dev ∨ rights = vmKernelOnly ∨ (toFrame ∧ rights ≠ vmReadWrite) then NonDetE.throwError (.invalidCapability 1) else do @@ -138,6 +141,90 @@ def decodeFl4TcbExt? (l : Fl4ExtLabel) (args : List Data) (t : ObjRef) | .tcbSetPACKeys => some (decodeFl4SetPacKeys args buf t) | _ => none +/-- FL4 accepts mapped Stage-2 VSpace roots in the ordinary VTable slot. -/ +def fl4MappedStage2Root : Cap → Bool + | .archObjectCap (.ext (.stage2VSpaceCap _ (some _))) => true + | _ => false + +/-- The common CSpace/VSpace decode, extending the derived root check. -/ +def fl4DecodeCvSpace (args : List Data) (cap : Cap) (slot : CslotPtr) + (excaps : List (Cap × CslotPtr)) : SEMonad Z TcbInvocation := do + NonDetE.whenE (args.length < 2 ∨ excaps.length < 2) <| NonDetE.throwError .truncatedMessage + let crootData := the args[0]? + let vrootData := the args[1]? + let (crootCap, crootSlot) := the excaps[0]? + let (vrootCap, vrootSlot) := the excaps[1]? + let canChgCr ← NonDetE.liftE <| NonDet.liftM not <| slotCapLongRunningDelete <| + getTcbCtablePtr (objRefOf cap) + let canChgVr ← NonDetE.liftE <| NonDet.liftM not <| slotCapLongRunningDelete <| + getTcbVtablePtr (objRefOf cap) + NonDetE.unlessE (canChgCr ∧ canChgVr) <| NonDetE.throwError .illegalOperation + let croot ← deriveCap crootSlot <| + (if crootData = 0 then id else updateCapData false crootData) crootCap + NonDetE.unlessE (isCnodeCap croot) <| NonDetE.throwError .illegalOperation + let vroot ← deriveCap vrootSlot <| + (if vrootData = 0 then id else updateCapData false vrootData) vrootCap + NonDetE.unlessE (isValidVtableRoot vroot ∨ fl4MappedStage2Root vroot) <| + NonDetE.throwError .illegalOperation + NonDetE.returnOk <| .threadControlCaps (objRefOf cap) slot none none + (some (croot, crootSlot)) (some (vroot, vrootSlot)) none + +/-- The native and unmatched-cap cases run the ported decode directly, +including its derivation and error order. Inspecting the Stage-2 variant +does not perform a lookup or a failing state action. -/ +def fl4DecodeTcbConfigure (args : List Data) (cap : Cap) (slot : CslotPtr) + (excaps : List (Cap × CslotPtr)) : SEMonad Z TcbInvocation := + if fl4MappedStage2Root (the excaps[1]?).1 then do + NonDetE.whenE (args.length < 3) <| NonDetE.throwError .truncatedMessage + NonDetE.whenE (excaps.length < 3) <| NonDetE.throwError .truncatedMessage + let buffer ← decodeSetIpcBuffer [the args[2]?] cap slot [the excaps[2]?] + let space ← fl4DecodeCvSpace (args.take 2) cap slot (excaps.take 2) + NonDetE.returnOk <| .threadControlCaps (objRefOf cap) slot none none + space.tcNewCroot space.tcNewVroot buffer.tcNewBuffer + else decodeTcbConfigure args cap slot excaps + +def fl4DecodeSetSpace (args : List Data) (cap : Cap) (slot : CslotPtr) + (excaps : List (Cap × CslotPtr)) : SEMonad Z TcbInvocation := + if fl4MappedStage2Root (the excaps[2]?).1 then do + NonDetE.whenE (args.length < 2 ∨ excaps.length < 3) <| NonDetE.throwError .truncatedMessage + let space ← fl4DecodeCvSpace (args.take 2) cap slot ((excaps.drop 1).take 2) + let faultHandler ← checkHandlerEp 1 (the excaps[0]?) + NonDetE.returnOk <| .threadControlCaps (objRefOf cap) slot (some faultHandler) none + space.tcNewCroot space.tcNewVroot none + else decodeSetSpace args cap slot excaps + +theorem fl4DecodeTcbConfigure_eq_l4v (args : List Data) (cap : Cap) (slot : CslotPtr) + (excaps : List (Cap × CslotPtr)) (h : fl4MappedStage2Root (the excaps[1]?).1 = false) : + fl4DecodeTcbConfigure (Z := Z) args cap slot excaps = decodeTcbConfigure args cap slot excaps := by + simp [fl4DecodeTcbConfigure, h] + +theorem fl4DecodeSetSpace_eq_l4v (args : List Data) (cap : Cap) (slot : CslotPtr) + (excaps : List (Cap × CslotPtr)) (h : fl4MappedStage2Root (the excaps[2]?).1 = false) : + fl4DecodeSetSpace (Z := Z) args cap slot excaps = decodeSetSpace args cap slot excaps := by + simp [fl4DecodeSetSpace, h] + +/-- FL4 re-reads the SC after IPC cancellation and the Restart write. -/ +def fl4TcbRestart (t : ObjRef) : SMonad Z Unit := do + let state ← getThreadState t + NonDet.when (¬runnable state ∧ ¬idle state) <| do + cancelIpc t + setThreadState t .restart + let sc ← getTcbObjRef Tcb.tcbSchedContext t + ifSporadicCurScTestRefillUnblockCheck sc + NonDet.maybeM fl4SchedContextResume sc + testPossibleSwitchTo t + +/-- Binding a TCB to an SC in the local view, where migration is identity. -/ +def fl4TcbBindSc (sc t : ObjRef) : SMonad Z Unit := do + setTcbObjRef tcbSchedContextUpdate t (some sc) + setScObjRef scTcbUpdate sc (some t) + ifSporadicActiveCurScTestRefillUnblockCheck (some sc) + fl4SchedContextResume sc + let eligible ← NonDet.gets (schedulable t) + NonDet.when eligible <| do + tcbSchedAction tcbSchedEnqueue t + rescheduleRequired + /-- FL4's `Arch::fpu_flush` of a thread: its live FPU registers saved into its TCB if it owns the FPU. -/ def fl4FpuFlush (t : ObjRef) : SMonad Z Unit := do @@ -149,24 +236,27 @@ def invokeFl4Tcb : Fl4TcbInvocation → PMonad Z (List Data) | .fpuTransfer t toFrame frame => NonDetE.liftE <| do fl4FpuFlush t if toFrame then - doMachineOp (fl4FpuToFrame frame) + let image ← asUser t getFPUState + doMachineOp (fl4FpuToFrame frame image) else do - let v ← NonDet.select (Set.univ : Set FpuState) - asUser t (NonDet.modify fun uc => { uc with userFpuState := v }) + let image ← doMachineOp (fl4FpuFromFrame frame) + asUser t (setFPUState image) let owner ← NonDet.gets fun s : State Z => s.archState.armCurrentFpuOwner - NonDet.when (owner = some t) (loadFpuState t) + NonDet.when (owner = some t) (setArmCurrentFpuOwner none) return [] - | .setTimer .. => NonDetE.liftE <| do - let b ← NonDet.select (Set.univ : Set Bool) - NonDet.modify fun σ => { σ with reprogramTimer := σ.reprogramTimer || b } + | .setTimer target deadline badge => NonDetE.liftE <| do + -- The per-core timer footprint is related outside the ported state. + let queue ← NonDet.select (Set.univ : Set (List (ObjRef × Fl4TcbTimer))) + let (_, earliest) := fl4SetTcbTimer queue target deadline badge + NonDet.modify fun σ => { σ with reprogramTimer := σ.reprogramTimer || earliest } return [] | .setFlags t clearFlags setFlags' => NonDetE.liftE <| do let flags ← threadGet Tcb.tcbFlags t - let newFlags ← pure (flags \ wordToTcbFlags clearFlags ∪ wordToTcbFlags setFlags') - Fl4SpecA64.setFlags t newFlags - NonDet.when (TcbFlag.fpuDisabled ∈ newFlags) (fpuRelease t) - let w ← NonDet.select {w | wordToTcbFlags w = newFlags} - return [w] + let old ← NonDet.select {old : Word | wordToTcbFlags old = flags} + let newWord := fl4TcbFlagsWord old clearFlags setFlags' + Fl4SpecA64.setFlags t (wordToTcbFlags newWord) + NonDet.when (newWord &&& 1 ≠ 0) (fpuRelease t) + return [newWord] | .setPacKeys .. => NonDetE.returnOk [] end diff --git a/proofs/Fl4SpecA64/Ext/Timer.lean b/proofs/Fl4SpecA64/Ext/Timer.lean index 853d5bfd8..81e866633 100644 --- a/proofs/Fl4SpecA64/Ext/Timer.lean +++ b/proofs/Fl4SpecA64/Ext/Timer.lean @@ -16,8 +16,9 @@ FL4's `timer::fire` / `sched::awaken` are architecture-independent timer). * `fl4TimerFires`: any finite sequence of fired timers (none included). * `fl4Awaken`: the nondeterministic step with every `fl4TimerFires` - outcome; it does not fail. `Ext/Entry`'s `fl4CallKernel` runs it before - `schedule` (whose own `awaken` is l4v's release-queue one). + outcome; it does not fail. `Ext/SchedTail`'s `fl4Schedule` (which + `Ext/Entry`'s `fl4CallKernel` runs) starts with it, before l4v's + release-queue `awaken`. The ported modules are not changed. -/ diff --git a/proofs/Fl4SpecA64/Ext/Timestamp.lean b/proofs/Fl4SpecA64/Ext/Timestamp.lean new file mode 100644 index 000000000..b1b288831 --- /dev/null +++ b/proofs/Fl4SpecA64/Ext/Timestamp.lean @@ -0,0 +1,45 @@ +import Fl4SpecA64.KHeap +import Fl4SpecA64.Ext.SchedControl + +/-! +FL4 records the raw AArch64 counter from CNTPCT_EL0 at EL2 or CNTVCT_EL0 +at EL1, after ISB. The clock observation is independent of the l4v +clamped clock. See `fl4-arch-aarch64/src/timer.rs:now` and +`fl4-core/src/sched.rs:update_timestamp`. +-/ + +namespace Fl4SpecA64 + +open Fl4Spec Classical + +noncomputable section + +/-- The raw counter sample at each observation, supplied by the platform. -/ +opaque fl4RawCounterOracle : Nat → Ticks + +/-- Record one post-ISB counter observation without clamping it. -/ +def fl4ReadRawCounter : MachineMonad Ticks := fun ms => + let index := ms.timeState + 1 + let ticks := fl4RawCounterOracle index + ({(ticks, { ms with timeState := index, lastMachineTime := ticks })}, False) + +/-- FL4's wrapping elapsed and accumulated times, with the concrete +multidomain saturating expiration comparison. -/ +def fl4TimestampState {Z : Type} (now : Ticks) (s : State Z) : State Z := + let used := now - s.curTime + { s with + curTime := now + consumedTime := s.consumedTime + used + domainTime := if numDomains > 1 then + if min maxTime.toNat (used.toNat + fl4MinBudget.toNat) ≥ s.domainTime.toNat then 0 + else s.domainTime - used + else s.domainTime } + +/-- Read the hardware once, then update the scheduler's timestamp. -/ +def fl4UpdateTimestamp {Z : Type} : SMonad Z Unit := do + let now ← doMachineOp fl4ReadRawCounter + NonDet.modify (fl4TimestampState now) + +end + +end Fl4SpecA64 diff --git a/proofs/Fl4SpecA64/Ext/UntypedVirt.lean b/proofs/Fl4SpecA64/Ext/UntypedVirt.lean new file mode 100644 index 000000000..5220df8e1 --- /dev/null +++ b/proofs/Fl4SpecA64/Ext/UntypedVirt.lean @@ -0,0 +1,69 @@ +/- +FL4 difference (Fl4SpecA64/PORTING.md, "Untyped retype without virtualization"): **the untyped decode's +virtualization check**. + +FL4's `untyped::decode` (`crates/fl4-core/src/untyped.rs`, AArch64) answers `InvalidArgument 0` to a retype into +`ArmVcpu` (12) or `ArmStage2VSpace` (13) when `Arch::virtualization()` is 0 (AArch64 without EL2). The check follows +the label, length and extra-cap checks and precedes every other argument check. seL4 and l4v decide this at build time +(`CONFIG_ARM_HYPERVISOR_SUPPORT`), so `decode_untyped_invocation` has no such arm. + +* `fl4A64Virt`: whether virtualization is available (an opaque platform value, as `fl4A64Platform`). +* `fl4DecodeUntypedInvocationVirt`: the check, then `fl4DecodeUntypedInvocation` (`Ext/TcbSize`). +* Conservativity: `fl4DecodeUntypedInvocationVirt_virt` (with virtualization it is `fl4DecodeUntypedInvocation`) and + `fl4DecodeUntypedInvocationVirt_other` (so it is for every other request). +-/ +import Fl4SpecA64.Ext.TcbSize + +namespace Fl4SpecA64 + +open Fl4Spec + +noncomputable section +open Classical + +/-- FL4: whether virtualization is available (`Arch::virtualization() != 0`). -/ +opaque fl4A64Virt : Bool + +/-- FL4: the requests `untyped::decode` refuses for lack of virtualization: a well-formed `Retype` message (label, +six arguments, an extra cap) asking for `ArmVcpu` (12) or `ArmStage2VSpace` (13). -/ +def fl4UntypedVirtRefused (label : Data) (args : List Data) (excaps : List Cap) : Prop := + fl4A64Virt = false ∧ genInvocationType label = .untypedRetype ∧ 6 ≤ args.length ∧ excaps.length ≠ 0 ∧ + (dataToNat (the args[0]?) = 12 ∨ dataToNat (the args[0]?) = 13) + +section +variable {Z : Type} [StateExt Z] + +/-- **FL4's untyped decode** with its virtualization check. -/ +def fl4DecodeUntypedInvocationVirt (label : Data) (args : List Data) (slot : CslotPtr) (cap : Cap) + (excaps : List Cap) : SEMonad Z UntypedInvocation := + if fl4UntypedVirtRefused label args excaps then NonDetE.throwError (.invalidArgument 0) + else fl4DecodeUntypedInvocation label args slot cap excaps + +set_option linter.unusedSectionVars false in +/-- A refused request. -/ +theorem fl4DecodeUntypedInvocationVirt_refused {label : Data} {args : List Data} {excaps : List Cap} + (h : fl4UntypedVirtRefused label args excaps) (slot : CslotPtr) (cap : Cap) : + fl4DecodeUntypedInvocationVirt (Z := Z) label args slot cap excaps = + NonDetE.throwError (.invalidArgument 0) := + ite_eq_left h + +set_option linter.unusedSectionVars false in +/-- Every request not refused decodes as without the check. -/ +theorem fl4DecodeUntypedInvocationVirt_other {label : Data} {args : List Data} {excaps : List Cap} + (h : ¬ fl4UntypedVirtRefused label args excaps) (slot : CslotPtr) (cap : Cap) : + fl4DecodeUntypedInvocationVirt (Z := Z) label args slot cap excaps = + fl4DecodeUntypedInvocation label args slot cap excaps := + ite_eq_right h + +/-- **Conservativity**: with virtualization the check does nothing. -/ +theorem fl4DecodeUntypedInvocationVirt_virt (h : fl4A64Virt = true) : + fl4DecodeUntypedInvocationVirt (Z := Z) = fl4DecodeUntypedInvocation := by + funext label args slot cap excaps + exact fl4DecodeUntypedInvocationVirt_other + (fun hr : fl4UntypedVirtRefused label args excaps => Bool.noConfusion (h.symm.trans hr.1)) slot cap + +end + +end + +end Fl4SpecA64 diff --git a/proofs/Fl4SpecA64/Ext/UserTop.lean b/proofs/Fl4SpecA64/Ext/UserTop.lean new file mode 100644 index 000000000..9f210f4bf --- /dev/null +++ b/proofs/Fl4SpecA64/Ext/UserTop.lean @@ -0,0 +1,25 @@ +/- +FL4 difference (Fl4SpecA64/PORTING.md, "Ext boundary"): **FL4's AArch64 +`USER_TOP`** (generation 7). + +l4v's `user_vtop` is `pptrUserTop` (`Machine_A`, l4v's hypervisor configuration: +`mask 44`, or `mask 40` on 40-bit PA platforms; the EL2 Stage-2 input range). +FL4's AArch64 kernel runs at EL1 with a 48-bit, 4-level Stage-1 user half and +checks every user virtual address against `fl4_abi::consts::USER_TOP = +0x0000_7fff_ffff_ffff` (`crates/fl4-abi/src/consts.rs:126-129`: "AArch64 without +EL2 has the same value"; `vspace.rs` `decode_page_table` 1131, `decode_page_map` +771). `fl4UserVtop` is that value; `Machine.userVtop` reads it (an `-- FL4 hook` +line), so l4v's `ARMPageTableMap` / `ARMPageMap` decoders reject exactly the +addresses FL4 rejects. + +A leaf module (only `BitVec`), so `Fl4SpecA64.Machine` can import it. +-/ +import Fl4Spec.Monad + +namespace Fl4SpecA64 + +/-- FL4: the highest user virtual address (`fl4_abi::consts::USER_TOP`, +2^47 - 1). -/ +def fl4UserVtop : BitVec 64 := 0x0000_7fff_ffff_ffff + +end Fl4SpecA64 diff --git a/proofs/Fl4SpecA64/Ext/VSpace.lean b/proofs/Fl4SpecA64/Ext/VSpace.lean index 60f3e0a28..2daa51d8b 100644 --- a/proofs/Fl4SpecA64/Ext/VSpace.lean +++ b/proofs/Fl4SpecA64/Ext/VSpace.lean @@ -31,6 +31,30 @@ on AArch64** (l4h patches), the AArch64 counterparts of the x86 port's 1)`) and the run fitting (`RangeError 1 (size - node_offset)`); the slots empty (`DeleteFirst`). The rights are the frame's masked with the sixth argument. The same as x86's, with l4v's AArch64 frame caps. +3. (P3 S3 gen 33, Architect ruling 02:15Z) **The EL1 cache-maintenance + decoders** `decodeFl4VsInvFlush` (`ARMVSpaceClean_Data` … + `ARMVSpaceUnify_Instruction`) and `decodeFl4FrInvFlush` (`ARMPageClean_Data` + … `ARMPageUnify_Instruction` of a frame in l4v's tables). l4v's AARCH64 + `decode_vs_inv_flush` / `decode_fr_inv_flush` (`Decode.lean` + `decodeVsInvFlush` / `decodeFrInvFlush`, unchanged) are seL4's + `CONFIG_ARM_HYPERVISOR_SUPPORT` (EL2) path, which flushes through the kernel + window and so rejects `pstart` outside it. FL4 runs its kernel at EL1: + `mmu::cache_op` (`crates/fl4-arch-aarch64/src/mmu.rs` 639–659) flushes through + the kernel window when the range is inside it and otherwise borrows the user + page table (`CacheVia::Borrow`, switching `TTBR0`), as seL4's EL1 `#else` + branch (`src/arch/arm/64/kernel/vspace.c` at c98d70ad, 1499–1506 and + 1779–1807). So FL4's decoders (`crates/fl4-core/src/arch/aarch64/vspace.rs` + `decode_vspace_flush` 923–966, `decode_page_flush` 884–921) have no window + check, need a read-write mapping for the `Invalidate_Data` labels + (`IllegalOperation`), and the frame decoder rejects a cap whose recorded + mapping is stale (`mapped_entry`: the descriptor at the cap's address is no + page of the cap's size naming the frame; `InvalidCapability 0`). The frame + decoder's checks run in FL4's order: the message length, ASID 0 (or an + unmapped cap), `end <= start` and the page-size bounds before the ASID's + root. Both return l4v's `.invokeVSpace` / `.invokePage` invocations, so + `perform_vspace_invocation` / `perform_page_invocation` are l4v's. The + descriptor's rights are read by `lookupFramePte` (l4v's `lookup_frame` + returning the page PTE in place of its address). The ported modules are not changed. -/ @@ -74,6 +98,102 @@ def performTestAndClearAccessed (_frame : ObjRef) : SMonad Z (List Data) := end +/-- (P3 S3 gen 33) **l4v's `lookup_frame` with the descriptor** (module docstring, item 3): `lookupFrame` +returning the page PTE itself (FL4 reads its rights, `vspace.rs` 912 and 957, seL4 `vmRightsFromPTE(pte)`). -/ +def lookupFramePte (vspace : ObjRef) (vaddr : VSpaceRef) : Lookup PtesOf (VMPageSize × PTE) := do + let (level, slot) ← ptLookupSlot vspace vaddr + let pte ← oapply2 (levelType level) slot + Lookup.oassert (pte.isPagePTE = true) + Lookup.oassert (level ≤ 2) + Lookup.oreturn (vmsizeOfLevel level, pte) + +section +variable {Z : Type} [StateExt Z] + +/-- (P3 S3 gen 33) The tail of `decodeFl4VsInvFlush` on the frame looked up at `start` (FL4 `vspace.rs` 942–965; +seL4 `vspace.c` 1478–1520, EL1): no page, nothing to flush (the caller restarts); a range crossing the page, +`RangeError`; `Invalidate_Data` on a mapping without write rights, `IllegalOperation` (EL1, 1499–1506); else the +flush of l4v's `vspace_flush`. -/ +def fl4VsFlushFrame (label start «end» : Data) (vspace : ObjRef) (asid : Asid) : + Option (VMPageSize × PTE) → SEMonad Z ArchInvocation + | none => NonDetE.returnOk <| .invokeVSpace .vspaceNothing + | some (pgsz, pte) => + let bitsLeft := ptBitsLeft (levelOfVmsize pgsz) + let baseStart := pageBase start pgsz + let baseEnd := pageBase («end» - 1) pgsz + if baseStart ≠ baseEnd then NonDetE.throwError (.rangeError start (baseStart + mask bitsLeft)) + else if labelToFlushType label = .invalidate ∧ pte.pteRights ≠ vmReadWrite then + NonDetE.throwError .illegalOperation + else NonDetE.returnOk <| .invokeVSpace <| + .vspaceFlush (labelToFlushType label) start («end» - 1) (pteBaseAddr pte + (start &&& mask bitsLeft)) vspace + asid + +/-- (P3 S3 gen 33) **FL4's VSpace cache-maintenance decoder** (module docstring, item 3): l4v's +`decode_vs_inv_flush` (ArchDecode_A.AARCH64_A) on seL4's EL1 path, as FL4's `decode_vspace_flush` +(`crates/fl4-core/src/arch/aarch64/vspace.rs` 923–966; seL4 `src/arch/arm/64/kernel/vspace.c` at c98d70ad, +`decodeARMVSpaceRootInvocation`, `#ifndef CONFIG_ARM_HYPERVISOR_SUPPORT`): no kernel-window check, write rights +for `Invalidate_Data`. The checks up to the root are l4v's (with `userVtop`, FL4's `USER_TOP`). -/ +def decodeFl4VsInvFlush : ArchDecoder Z := fun label args _cte cap _extraCaps => + match cap with + | .pageTableCap _ .vsRootPtT _ => + if args.length > 1 then + let start := the args[0]? + let «end» := the args[1]? + do + NonDetE.whenE («end» ≤ start) <| NonDetE.throwError (.invalidArgument 1) + NonDetE.whenE («end» > userVtop) <| NonDetE.throwError .illegalOperation + let (vspace, asid) ← checkVspaceRoot (.archObjectCap cap) 0 + let vspace' ← lookupErrorOnFailure false <| findVspaceForAsid asid + NonDetE.whenE (vspace' ≠ vspace) <| NonDetE.throwError (.invalidCapability 0) + let frameInfo ← NonDetE.liftE + (NonDet.gets fun s : State Z => lookupFramePte vspace start (ptesOf s)) + fl4VsFlushFrame label start «end» vspace asid frameInfo + else NonDetE.throwError .truncatedMessage + | _ => NonDetE.liftE NonDet.fail + +/-- (P3 S3 gen 33) The tail of `decodeFl4FrInvFlush` at the frame's root (FL4 `vspace.rs` 908–920; seL4 `vspace.c` +1790–1807, EL1): the descriptor at the cap's address must be a page of the cap's size naming the frame +(`mapped_entry`; else `InvalidCapability 0`), with write rights for `Invalidate_Data`; then l4v's `page_flush`. -/ +def fl4FrFlushFrame (label start «end» : Data) (p : ObjRef) (pgsz : VMPageSize) (vaddr : VSpaceRef) + (vspace : ObjRef) (asid : Asid) : Option (VMPageSize × PTE) → SEMonad Z ArchInvocation + | some (sz, pte) => + if sz = pgsz ∧ pteBaseAddr pte = addrFromPPtr p then + if labelToFlushType label = .invalidate ∧ pte.pteRights ≠ vmReadWrite then + NonDetE.throwError .illegalOperation + else NonDetE.returnOk <| .invokePage <| + .pageFlush (labelToFlushType label) (vaddr + start) (vaddr + «end» - 1) (addrFromPPtr p + start) vspace asid + else NonDetE.throwError (.invalidCapability 0) + | none => NonDetE.throwError (.invalidCapability 0) + +/-- (P3 S3 gen 33) **FL4's frame cache-maintenance decoder** (module docstring, item 3): l4v's +`decode_fr_inv_flush` (ArchDecode_A.AARCH64_A) on seL4's EL1 path, as FL4's `decode_page_flush` for a frame in l4v's +tables (`crates/fl4-core/src/arch/aarch64/vspace.rs` 884–921, `frame_root` / `mapped_entry`; seL4 +`src/arch/arm/64/kernel/vspace.c` at c98d70ad, `decodeARMFrameInvocation`, 1779–1807): FL4's order (the length, ASID +0, `end <= start`, the page-size bounds, then the ASID's root), the stale-mapping and rights checks, no kernel-window +check. -/ +def decodeFl4FrInvFlush : ArchDecoder Z := fun label args _cte cap _extraCaps => + match cap with + | .frameCap p _ pgsz _ mappedAddress => + if args.length > 1 then + let start := the args[0]? + let «end» := the args[1]? + do + let (asid, vaddr) ← match mappedAddress with + | some (asid, vaddr) => + if asid = 0 then NonDetE.throwError .illegalOperation else NonDetE.returnOk (asid, vaddr) + | none => NonDetE.throwError .illegalOperation + NonDetE.whenE («end» ≤ start) <| NonDetE.throwError (.invalidArgument 1) + let pageSize : Word := (1 : Word) <<< pageBitsForSize pgsz + NonDetE.whenE (start ≥ pageSize ∨ «end» > pageSize) <| NonDetE.throwError (.invalidArgument 0) + let vspace ← lookupErrorOnFailure false <| findVspaceForAsid asid + let frameInfo ← NonDetE.liftE + (NonDet.gets fun s : State Z => lookupFramePte vspace vaddr (ptesOf s)) + fl4FrFlushFrame label start «end» p pgsz vaddr vspace asid frameInfo + else NonDetE.throwError .truncatedMessage + | _ => NonDetE.liftE NonDet.fail + +end + /-- l4h 0029: a `DeriveSubframe` invocation: the large frame cap's slot, the first page, the rights, and the destination slots. -/ structure Fl4SubframeInvocation where diff --git a/proofs/Fl4SpecA64/Machine.lean b/proofs/Fl4SpecA64/Machine.lean index cebbddf63..afa0f16a1 100644 --- a/proofs/Fl4SpecA64/Machine.lean +++ b/proofs/Fl4SpecA64/Machine.lean @@ -25,6 +25,7 @@ The AArch64 theories of l4v `rt` are MCS already (unlike X64): nothing here is taken from another architecture. -/ import Fl4Spec.Monad +import Fl4SpecA64.Ext.UserTop import Mathlib.Logic.Function.Basic namespace Fl4SpecA64 @@ -1457,7 +1458,7 @@ kernel window. -/ def pptr_base : Word := pptrBase /-- l4v: Machine_A.AARCH64_A.user_vtop: the virtual address space available to users. -/ -def userVtop : Word := pptrUserTop +def userVtop : Word := fl4UserVtop -- FL4 hook (generation 7): FL4's EL1 `USER_TOP` (2^47 - 1), not l4v's `pptrUserTop` (Ext/UserTop) /-- l4v: Machine_A.AARCH64_A.kdev_base: the start of the kernel device mapping region in the highest 1GiB. -/ def kdev_base : Word := kdevBase diff --git a/proofs/Fl4SpecA64/PORTING.md b/proofs/Fl4SpecA64/PORTING.md index b21e38432..a321ec8d0 100644 --- a/proofs/Fl4SpecA64/PORTING.md +++ b/proofs/Fl4SpecA64/PORTING.md @@ -40,7 +40,9 @@ deterministic; no `sorry` / `axiom`). In addition, as ruled for this library: types (Architect's ruling of generation 3, decision 1 (i); "The `ext` hook" below): one `| ext …` constructor or `ext` field per type, its payload in `Ext/ArchTypes`, every ported function unchanged on every - non-`ext` value. One function also tests an `ext` value outside a match + non-`ext` value, with two named exceptions in value (not in shape): `userVtop := fl4UserVtop` (`Machine.lean`, + generation 7) and the `Decode.lean` flush bound `end > userVtop` (generation 8), Architect's rulings in "Two FL4 hooks" + (ll. 424–439). One function also tests an `ext` value outside a match (`end_timeslice`, Architect's ruling of generation 4; "In-port hooks" below). - **Multi-core-ready.** Nothing in the port or in `Ext/` assumes a single core @@ -52,6 +54,15 @@ deterministic; no `sorry` / `axiom`). In addition, as ruled for this library: `extends` where l4v has a record extension, `vcpu = vcpu_state + ...`); the platform is only `Kernel_Config`'s constants (one opaque `kernelConfig`). +### No non-tail `return ()` in a `do` `match` / `if` + +**Permanent rule: never write a non-tail `return ()` inside a `do` `match` or `if` arm; always use `pure ()`.** In +Lean's `do` notation `return` is an *early exit* of the whole block, so `match x with | some y => f y | _ => return ()` +followed by more statements skips every following statement in the other arm, while Isabelle's `case x of None ⇒ return () +| Some y ⇒ f y` is an ordinary value and the statements after it still run. (Found by the 2026-12-03 audit, +`handoff/spec-return-audit.md`: A64 `performPgInvUnmap`, `performPtInvUnmap`, `prepareThreadDelete`, and five sites in +`AARCH64/VCPU.lean`.) Source for the l4v texts: seL4/l4v `rt`, commit `c98d70ad`. + ## Module map | Lean module | l4v theories | @@ -79,7 +90,7 @@ deterministic; no `sorry` / `axiom`). In addition, as ruled for this library: | `Fl4SpecA64/Decode.lean` | AARCH64/ArchDecode_A, Decode_A | | `Fl4SpecA64/Init.lean` | AARCH64/Init_A | | `Fl4SpecA64/Syscall.lean` | Event_H (Haskell `SEL4/API/Syscall.lhs`), AARCH64/Hypervisor_A, Syscall_A | -| `Fl4SpecA64/Ext/*.lean` | FL4 only (see "Ext boundary"): ArchTypes, MultiCore, Platform, Label, IrqControl, VSpace, Tcb, Stage2, ARMVCPURun, Finalise, ArmHighVSpace, Invocation, ReplyError, Entry, Timer, Tick, TcbSize, IrqNode | +| `Fl4SpecA64/Ext/*.lean` | FL4 only (see "Ext boundary"): ArchTypes, MultiCore, Platform, Label, IrqControl, VSpace, Tcb, Stage2, ARMVCPURun, Finalise, ArmHighVSpace, Invocation, ReplyError, Entry, Timer, Tick, TcbSize, IrqNode, SchedControl, SchedBudget, SchedTail, Timestamp, Reply | The module order is l4v `rt`'s AARCH64 import graph: KHeap_A (with ArchVSpaceAcc_A) → CSpaceAcc_A → Sporadic_A → ArchInvocation_A/Invocations_A @@ -317,7 +328,7 @@ with its payload in the leaf module `Ext/ArchTypes` (which imports only | port type (l4v) | hook | payload (`Ext/ArchTypes`) | |---|---|---| -| `ArchCap` (`arch_cap`) | `ext (c : ExtArchCap)` | `stage2VSpaceCap obj mapData`, `highVSpaceCap obj mapData` (FL4 `Cap::Ept { level: 0 }`, `Cap::HighVSpace`) | +| `ArchCap` (`arch_cap`) | `ext (c : ExtArchCap)` | `stage2VSpaceCap obj mapData`, `highVSpaceCap obj mapData` (FL4 `Cap::Ept { level: 0 }`, `Cap::HighVSpace`), `stage2FrameCap obj rights size isDevice mapData` (generation 6: FL4 `Cap::Frame { is_ept: true }`) | | `ArchKernelObj` (`arch_kernel_obj`) | `ext (o : ExtArchObj PTE)` | `stage2Root` (1024 top entries), `highVSpace` (its level-1 table) | | `AObjectType` (`aobject_type`) | `ext (t : ExtAObjectType)` | FL4's object types 13 and 14 | | `AAType` (`aa_type`) | `ext (t : ExtAAType)` | `aStage2Root`, `aHighVSpace` (generation 5) | @@ -327,7 +338,8 @@ with its payload in the leaf module `Ext/ArchTypes` (which imports only `ExtArchObj` and `ExtTcb` take the port's `PTE` and `Cap` as parameters (the leaf cannot name them). Every ported function is unchanged on every -non-`ext` value. The `ext` arms, each marked `FL4 hook` in the source: +non-`ext` value, except the two value changes of "Two FL4 hooks" (`userVtop` in `Machine.lean`, generation 7; the `Decode.lean` +flush bound, generation 8). The `ext` arms, each marked `FL4 hook` in the source: | function (l4v) | `ext` arm | why | |---|---|---| @@ -341,10 +353,54 @@ non-`ext` value. The `ext` arms, each marked `FL4 hook` in the source: | `tcb_cnode_map` | slots 5 and 6 map to the two caps | the slots exist in FL4's TCB CNode (so they hold CDT caps and are deleted with the TCB) | | `set_cap` (CSpaceAcc_A) | writes slots 5 and 6 | as above; l4v's `fail` for other indices unchanged | | `finalise_cap` of a final thread cap (CSpace_A) | `Zombie r None 7` (`fl4TcbSlotCount`; l4v 5) | the zombie must clear all seven slots | -| `arch_derive_cap` | assigned: the cap; unassigned: `IllegalOperation` | no l4v case; FL4 `derive_cap`, l4v's rule for page tables | -| `arch_same_region_as` | same kind and object (`ExtArchCap.sameObject`) | no l4v case; FL4 `same_region_as`; `same_aobject_as` falls through to it | +| `arch_derive_cap` | root assigned: the cap; root unassigned: `IllegalOperation`; Stage-2 frame: the unmapped plain `FrameCap` (`ExtArchCap.deriveWith`) | no l4v case; FL4 `derive_cap` (`objects.rs:234`: a `Frame` becomes `mapped: false, is_ept: false`), l4v's rule for page tables | +| `arch_same_region_as` | root: same kind and object (`ExtArchCap.sameObject`); Stage-2 frame: l4v's frame region rule against any frame (`ExtArchCap.sameRegionWith`, `frameInRegion`) | no l4v case; FL4 `same_region_as` (`Frame` with `Frame`, whatever `is_ept`) | | `arch_decode_invocation` | the monad's failure | no l4v case; `Ext/Invocation` decodes FL4's caps | +**The frame hooks (generation 6, Architect's ruling (b′) of A64-P3 ExtVsA gen 2).** +FL4's frame cap carries `is_ept` (`capability.rs:61`, set by `ept::map_frame`, +`ept.rs:283`); l4v's `FrameCap` has no such field and, unlike the x86 port +(whose `.vmEptMap` is a value of l4v's own map-type field), no in-port place +for one. A frame mapped in a Stage-2 VSpace is therefore the cap +`ExtArchCap.stage2FrameCap`, and the `ext` arms of l4v's frame functions treat +it as the frame FL4 sees. Each is one line delegating to `Ext/ArchTypes`; no +l4v constructor and no non-`ext` arm changed: + +| function (l4v) | file | `ext` arm (FL4 source) | +|---|---|---| +| `is_FrameCap` | `AARCH64/VSpace` | `e.isFrame` (a Stage-2 frame is a frame) | +| `acap_rights`, `acap_fsize`, `acap_is_device`, `acap_map_data` | `AARCH64/Structs` | `e.rights`, `e.fsize`, `e.acapIsDevice`, `e.frameMapData` (the frame's fields; a root keeps l4v's `undefined`) | +| `update_map_data` | `AARCH64/Structs` | `.ext (e.updateMapData m)` (a Stage-2 frame keeps its kind; a root `undefined`) | +| `arch_cap_is_device` | `AARCH64/Structs` | `e.isDevice` (FL4 `is_device`; a root `false`) | +| `acap_rights_update` | `AARCH64/Structs` | `.ext (e.rightsUpdate R)` (FL4 `mask_cap_rights`, `capability.rs:374`, masks a frame's rights whatever `is_ept`: `validate_vm_rights`) | +| `arch_derive_cap` | `AARCH64/VSpace` | `e.deriveWith …` (above) | +| `arch_same_region_as` | `AARCH64/VSpace` | `e.sameRegionWith c'.isFrameCap c'.acapObj c'.acapFsize (root rule)` (FL4 `same_region_as`, `objects.rs:109`) | +| `same_aobject_as` | `AARCH64/VSpace` | `e.sameAobjectWith c'.isFrameCap c'.acapObj c'.acapFsize (arch_cap_is_device c') (arch_same_region_as)` (FL4 `same_object_as`, `objects.rs:147`: same address, size and device flag) | + +`arch_obj_size` of a Stage-2 frame is `pageBitsForSize` (`ExtArchCap.objSize`). +`Ext/*` dispatches on the cap's kind as FL4 on `is_ept`: `decodeS2PageMap` +gives a `stage2FrameCap` when the rights mask leaves rights (else the +unmapped plain frame, FL4's `e == 0`) and refuses a plain frame mapped +elsewhere (`InvalidCapability 1`); `fl4FinaliseCap` unmaps a Stage-2 frame +cap with `extUnmapPage .stage2` only, a plain frame cap with `extUnmapPage +.high` when its ASID names a high window, nothing when it names a Stage-2 +root, l4v's `unmap_page` otherwise; `ARMPageUnmap` (`performExtVSpace`), the +cache labels (`decodeExtPageFlush`) and `ARMPageTestAndClearAccessed` +(`decodeExtTestAndClearAccessed`) likewise; the other labels on a Stage-2 +frame cap are l4v's on its plain view (`Ext/Invocation`). One l4v arm keeps +its value against a Stage-2 frame: `same_aobject_as (FrameCap …) (ext …)` +falls through l4v's `_` arm to the frame region rule (FL4: same address, +size and device), so two caps to different-size frames, one inside the +other, are the same object only with the plain frame first. + +**The high-window guard** (generation 6, the same ruling): FL4 walks a +frame's address in an `ArmHighVSpace` from the window's level-0 table, the +model from its level-1 table (`ExtRootKind.high.geom` reads bits 12..38 +only). `extVaOk k va` (`k ≠ .high` or level-0 index `HIGH_L0_INDEX` = 511) +guards `extUnmapPage` and the high-window `extMappedEntry` lookups of +`decodeExtPageFlush` / `decodeExtTestAndClearAccessed`: at another index the +model finds nothing, as FL4 (whose level-0 entries 0..255 are zero). + Every other match on these types has an l4v "other" case, which the `ext` value takes (e.g. `arch_finalise_cap`: `(NullCap, NullCap)`, whose FL4 behaviour is `Ext/Finalise`'s; `arch_is_physical`: `True`; @@ -368,31 +424,79 @@ TS_RUNNING_VM`; the x86 port's `Fl4Spec/Ipc.lean` has the same line). On every non-`ext` state it is l4v's `is_valid_timeout_handler` (the added `get_thread_state` of the current thread only reads). +### Two FL4 hooks (generation 7, Architect's ruling of A64-P3 S3 gen 18) + +Two one-line `-- FL4 hook` lines, each identical to l4v on every l4v state +that has no FL4 object: + +- **`userVtop`** (`Machine.lean`, `Machine_A.user_vtop`) is + **`fl4UserVtop`** (`Ext/UserTop`, a leaf module `Machine.lean` imports): + FL4's `fl4_abi::consts::USER_TOP = 0x0000_7fff_ffff_ffff` (2^47 - 1, + `crates/fl4-abi/src/consts.rs:126-129`, "AArch64 without EL2 has the same + value"), not l4v's hypervisor-mode `pptrUserTop` (`mask 44`). l4v's + `decode_fr_inv_map` (`vtop > user_vtop`) and `decode_pt_inv_map` + (`user_vtop < vaddr`) then reject exactly what FL4's `decode_page_map` + (`vspace.rs` 771, `last > USER_TOP`) and `decode_page_table` (1131, + `vaddr > USER_TOP`) reject. (Generation 8, third hook, Architect's ruling + of A64-P3 S3 gen 19: `decode_vs_inv_flush`'s `end > pptrUserTop`, which l4v + states with `pptrUserTop` directly, reads `userVtop` (`Decode.lean`, one + `-- FL4 hook` line), as FL4's `vspace.rs` 933 compares with `USER_TOP`.) + This is a difference in value, not in shape: FL4's AArch64 kernel runs at + EL1 with a 48-bit Stage-1 user half, l4v's AARCH64 at EL2. +- **`findVspaceForAsid`** (`AARCH64/VSpace.lean`, + `ArchVSpace_A.find_vspace_for_asid`) hides FL4's roots: the pool entry's + root is dropped (`InvalidRoot`) when it is a Stage-2 root or an + `ArmHighVSpace` (an `ext` object), as FL4's `find_asid` answers + `FailedLookup false InvalidRoot` for a `ROOT_S2` / `ROOT_HIGH` tag. So a + stale Stage-1 cap whose ASID now names an FL4 root fails as in FL4 + (`check_vspace`, `unmap_page`, `unmap_page_table`, `set_vm_root`), not + with `InvalidCapability` or a walk of the FL4 root. The ruling named + `vspace_for_asid`; the hook sits one function later because + `Ext/Stage2.asidRootKind` reads `vspace_for_asid` to find FL4's roots. + FL4's own lookups use **`findAnyRootForAsid`** (`Ext/Stage2`, l4v's + `find_vspace_for_asid` body, FL4's `find_asid_raw`): `findExtRoot` calls + it instead of `findVspaceForAsid`. + ## Ext boundary `Fl4SpecA64/Ext/` holds FL4's differences, modelled on FL4's AArch64 Rust (`crates/fl4-core/src/arch/aarch64`, `crates/fl4-core/src/*.rs`) as the x86 port's `Fl4Spec/Ext/*` models the x86 ones. The port depends only on -`Ext/ArchTypes` (the hook's payloads). +`Ext/ArchTypes` (the hook's payloads) and (generation 7) `Ext/UserTop`. | module | what it is | x86 counterpart | |---|---|---| | `Ext/ArchTypes` | the `ext` hook's payloads (above) | `Ext/VirtObj` | +| `Ext/UserTop` | (generation 7) FL4's `USER_TOP`, `fl4UserVtop` (the `userVtop` hook, above) | none | | `Ext/MultiCore` | `CoreId`, `PerCore`, `McState` (current core, per-core idle thread and SC, per-TCB affinity), `McState.WF`, `McView`, `onCore`, `switchTo` | none (x86 has `idleSc` in-port) | | `Ext/Platform` | FL4's boot-time values (`maxSpi`, `numNodes`, `hasPauth`, `hwAccessFlag`, `s2IpaBits`, `vgicVtr`, `vcpuMidr`; opaque, with their bounds) and FL4's IRQ numbering `fl4IrqOf` | `x64Plat` (in-port) | +| `Ext/SchedControl` | FL4's platform-dependent maximum scheduling-context period, release-time limit and ConfigureFlags decoder | `TimeConsts.maxPeriodUs` (in-port) | +| `Ext/SchedBudget` | The FL4 release-time and minimum-budget guards in charging, ConfigureFlags execution, and the kernel entry budget check | in-port scheduling-context proofs | +| `Ext/Timestamp` | Raw AArch64 counter observation with wrapping elapsed and accumulated time, as used by the syscall and interrupt budget checks | `Machine.getCurrentTime` and `updateTimeStamp` | +| `Ext/UntypedVirt` | FL4's virtualization check of `Retype` (`fl4A64Virt`, `fl4DecodeUntypedInvocationVirt`, conservativity) | none | | `Ext/Label` | FL4's 74 AArch64 labels (`fl4Numbering`, `fl4LabelOf`, `specLabelWord`) | `fl4Numbering` (in-port) | -| `Ext/IrqControl` | FL4's IRQControl: plain and `GetTrigger` with FL4's checks, `GetTriggerCore`, `GetMSI` | `Ext/IrqControl` | -| `Ext/VSpace` | `ARMPageTestAndClearAccessed` (frames in l4v's tables), `ARMPageDeriveSubframe` | `Ext/VSpace` (frame part) | +| `Ext/IrqControl` | FL4's IRQControl: plain, `GetTrigger` with an SPI route to core 0, `GetTriggerCore`, `GetMSI` | `Ext/IrqControl` | +| `Ext/VSpace` | `ARMPageTestAndClearAccessed` (frames in l4v's tables), `ARMPageDeriveSubframe`; (P3 S3 gen 33) FL4's EL1 cache-maintenance decoders `decodeFl4VsInvFlush` / `decodeFl4FrInvFlush` (below) | `Ext/VSpace` (frame part) | | `Ext/Tcb` | `TCBReadFPU`/`WriteFPU` (520 bytes, l4v's AArch64 FPU state), `TCBSetTimer`, FL4's `TCBSetFlags`, `TCBSetPACKeys` | `Ext/Tcb` (no debug API on AArch64) | | `Ext/Stage2` | FL4's roots (`rootKindAt`: the kind of object an ASID pool entry names, FL4's `ROOT_*` tag), FL4's walk (`extLookupSlot`), unmapping; Stage-2 VSpaces: `ARMPageMap` / `ARMPageTableMap` into one, its cache invocations, `ARMASIDPoolAssign` of an FL4 root; frames in FL4 roots: the frame cache invocations and `ARMPageTestAndClearAccessed` (`decodeExtPageFlush`, `decodeExtTestAndClearAccessed`) | `Ext/VirtEpt`, `Ext/VirtEptInv` | | `Ext/ARMVCPURun` | FL4's 64-register view, the state window, `ARMVCPUReadState` / `WriteState` / `Run`, FL4's `ARMVCPUReadReg` / `WriteReg` / `InjectIRQ` / `AckVPPI`, `complete_run`, `vcpu::check`, `cancel_run`, guest exits, `vcpu::init` | `Ext/VirtVcpu`, `Ext/VirtEntry` | | `Ext/Finalise` | `fl4FinaliseCap` and the delete / revoke / TCB copies, suspension's `cancel_run`, retype with `vcpu::init` | `Ext/VirtFinalise` | | `Ext/ArmHighVSpace` | `ArmHighVSpace`: `ARMPageMap` / `ARMPageTableMap` into one, `TCBSetHighVSpace` | none (AArch64 only) | | `Ext/Invocation` | the dispatcher: `fl4DecodeInvocation`, `fl4PerformInvocation`, `fl4HandleInvocation` | `Ext/Invocation`, `Ext/VirtDispatch` | +| `Ext/FaultReply` | `fl4SanitiseRegister`, FL4's `TIMEOUT_REPLY_MESSAGE`, `fl4HandleFaultReply` ("Fault reply" below) | `Ipc_A.handle_fault_reply` | +| `Ext/Reply` | FL4 Reply refill sufficiency, timeout consumed-time IPC and FL4's fault reply | `Ipc_A.do_reply_transfer` | | `Ext/ReplyError` | the `KERNEL_INVOCATION_REPORT_ERROR_IPC` error reply | verbatim copy | -| `Ext/Entry` | `fl4HandleEvent`, `fl4CallKernel` (with `fl4Awaken`), `fl4ActivateThread`, the guest-exit entry `fl4CallKernelRunExit`, the syscall entry's `FaultIP := NextIP - 4` | copy, `- 2` → `- 4` | +| `Ext/IrqSignal` | A signalled IRQ is masked and Ack unmasks it whatever the GIC version (`fl4ArchMaskIrqSignal`, `fl4HandleInterrupt`, `fl4MaybeHandleInterrupt`, `fl4InvokeIrqHandler`; `crates/fl4-core/src/interrupt.rs` `handle_interrupt` and `IRQAckIRQ`). l4v masks only without GICv3. `Ext/Invocation` and `Ext/Entry` (the interrupt event and `fl4PreemptionPathIrq`) use them | `ArchInterrupt_A.arch_mask_irq_signal` (in-port, unchanged) | +| `Ext/Entry` | FL4 event and guest-exit entry, syscall `FaultIP := NextIP - 4`, activation with YieldTo completion before the state dispatch, and the post-activation VCPU check | `Ext/Entry` | +| `Ext/SchedTail` | Local scheduler model. TCB timers fire before release-queue processing. The domain advance uses the bounded schedule and duration sentinel. Root writes are observed at execution and FPU/VCPU changes at return. The timer compares the computed deadline with the armed and custom timer words. | `Schedule_A` | | `Ext/Timer`, `Ext/Tick`, `Ext/TcbSize`, `Ext/IrqNode` | TCB timers, the clock tick, 4 KiB TCBs and FL4's object type numbers (`fl4DataToObjType`), the 5376-IRQ node | copies (gen 2) | +FL4's AArch64 `GetTrigger` (label 65) configures the trigger and routes the SPI +to core 0. The extension therefore uses the same trigger-and-route invocation +as `GetTriggerCore` (label 66), with a fixed core argument. The lemma +`invokeFl4IrqControl_coreZero_conservative` recovers the ported trigger-only +invocation when the core-zero machine operation equals `setIRQTrigger`. + **No frame batch on AArch64** (generation 3 finding, Architect's decision 2 of generation 3): the only frame-batch method is `X86PML4FrameBatch`, whose condition in `interfaces/object-api-extensions.xml` is `not @@ -400,6 +504,106 @@ CONFIG_ARCH_AARCH64`, and `arch/aarch64/vspace.rs` has no batch path. The generation 1 stub `Ext/FrameBatch` was deleted in generation 4; the frame batch is x86-only. +### Scheduling-context time bound + +Pinned seL4 `c98d70ad`, `include/kernel/sporadic.h:36–50`, sets the +maximum period to `getMaxUsToTicks() / 8` when +`CONFIG_KERNEL_STATIC_MAX_PERIOD_US = 0`, and reserves five maximum +periods in ticks when computing `MAX_RELEASE_TIME`. +FL4 implements those formulas in `crates/fl4-core/src/sporadic.rs:30–38`. +`Ext/SchedControl` supplies the opaque platform value `fl4MaxPeriodUs` +and derives `fl4MaxReleaseTime`. Its `fl4UsToTicks` models the u128 +multiply and divide followed by saturation in +`crates/fl4-arch-aarch64/src/timer.rs`. The decoder and release-time +limit use that full-width conversion, as does `fl4MinBudget` for the +kernel WCET. The named platform premise `Fl4MinBudgetPos` requires +the tick minimum to be positive for scheduler refinement. This matches +l4v's `MIN_BUDGET_pos'` and excludes counters so slow that the converted +kernel WCET rounds to zero. The ConfigureFlags decoder uses the platform +maximum for the time checks and range-error bounds. The minimum-budget +and budget-versus-period checks compare ticks, as in seL4 +`src/object/schedcontrol.c:118–140`. Error payloads stay in microseconds. +The release-time limit agrees with l4v when the maximum periods and +their conversions agree. Decoder agreement requires the same maximum, +matching argument conversions, and agreement on the three guards. +`Ext/SchedBudget` uses the dynamic release time in the overrun loop +and the partial charge, including when `sched::commit_time` charges a +bound current SC during ConfigureFlags. The entry budget check uses the +same charge operation. The corresponding l4v operations agree when the +release-time limit and minimum budget in ticks agree. + +### Reply budget and timeout consumed time + +The Reply tail uses the same effective timer frequency and +`fl4MinBudget`. `Ext/SchedControl.fl4TicksToUs` computes the exact product by 1,000,000 and +divides by the frequency before truncating to u64, as in +`fl4-arch-aarch64/src/timer.rs`. `fl4SchedContextUpdateConsumed` uses +the full u64 tick cap from `schedcontext.rs:update_consumed`. It +writes the remaining ticks and returns the converted amount. +`Fl4TicksToUsAgreeA` relates the concrete timer operation to the conversion. +`fl4RefillSufficient` compares saturating head capacity with +`fl4MinBudget`, including with zero usage in `ipc.rs:do_reply_transfer`. +`Fl4MinBudgetAgreeA` relates the concrete minimum to the same FL4 value. + +`Ext/Reply` threads the converted timeout payload through fault +transfer and send. Its `fl4DoReplyTransfer` uses both the FL4 refill +test and timeout helper, and FL4's fault reply (below). +`Ext/Invocation.fl4PerformInvocation` calls that operation for Reply. +It is not equal to the ported `doReplyTransfer` on a fault reply, so +there is no `eq_l4v` theorem for it; `fl4SendIpc`, `fl4DoIpcTransfer` +and `fl4HandleTimeout` keep theirs. + +### Fault reply + +`Ext/FaultReply.fl4HandleFaultReply` follows `faults.rs:handle_fault_reply` +and differs from `Ipc_A.handle_fault_reply` in two points: + +- `Timeout`: the reply registers are FL4's `TIMEOUT_REPLY_MESSAGE` + (`arch/aarch64/faults.rs`), seL4 C's order (`FaultIP, SP_EL0, SPSR_EL1, + X0..X8, X16..X18, X29, X30, X9..X15, X19..X28`). The ported + `timeoutMessage` is `X0..X30` in sequence; they agree on 12 words. +- every register arm sanitises with `fl4SanitiseRegister` + (`arch/aarch64/regs.rs:sanitise_for_tcb`): without a VCPU it is + `sanitiseRegister false` (`fl4SanitiseRegister_native`); with one, + `SPSR_EL1` keeps `VCPU_PSTATE_ALLOWED` and EL0t / EL1t / EL1h, an + illegal mode becoming EL1h (l4v keeps or replaces the whole word). + +The long-form `UnknownSyscall` reply of FL4's trapped syscalls is not +modelled (DESIGN.md §9). + +### EL1 cache maintenance (P3 S3 gen 33, Architect ruling 02:15Z) + +l4v's AARCH64 `decode_vs_inv_flush` / `decode_fr_inv_flush` (ported unchanged +as `decodeVsInvFlush` / `decodeFrInvFlush`) are seL4's +`CONFIG_ARM_HYPERVISOR_SUPPORT` (EL2) path: they flush through the kernel +window, so a `pstart` outside `[paddrBase, paddrTop]` is `IllegalOperation`. +FL4's kernel runs at EL1 and implements seL4's `#ifndef` branch +(`src/arch/arm/64/kernel/vspace.c` at c98d70ad, 1499–1506, 1779–1807): +`mmu::cache_op` borrows the user page table when the range is outside the +window, `ARMVSpaceInvalidate_Data` / `ARMPageInvalidate_Data` need a +read-write mapping, and a frame cap whose recorded mapping is stale is +`InvalidCapability 0`. This is FL4 behaviour, not a port deviation: +`Ext/VSpace` defines `decodeFl4VsInvFlush` and `decodeFl4FrInvFlush` from +FL4's `decode_vspace_flush` / `decode_page_flush` (FL4's check order, l4v's +`.invokeVSpace` / `.invokePage` invocations, `lookupFramePte` for the +descriptor's rights), and `fl4ArchDecodeInvocation` (`Ext/Invocation`) +dispatches the VSpace cache labels of a `VSRootPT_T` cap and the frame cache +labels of a frame in l4v's tables to them. The ported decoders are not +called by FL4's dispatcher. + +### Untyped retype without virtualization (A64-P3 Untyped, Architect D3) + +FL4's `untyped::decode` (`crates/fl4-core/src/untyped.rs`) refuses a retype +into `ArmVcpu` (12) or `ArmStage2VSpace` (13) with `InvalidArgument 0` when +`Arch::virtualization()` is 0 (AArch64 without EL2), after the label, length +and extra-cap checks and before every other argument check. l4v's +`decode_untyped_invocation` has no such arm (seL4 decides virtualization at +build time). `Ext/UntypedVirt` adds it on top of `fl4DecodeUntypedInvocation`: +`opaque fl4A64Virt` and `fl4DecodeUntypedInvocationVirt`, which +`fl4DecodeInvocation`'s untyped arm (`Ext/Invocation`) calls. With +virtualization it is `fl4DecodeUntypedInvocation` +(`fl4DecodeUntypedInvocationVirt_virt`). No ported file is changed. + ### The three extensions (generation 4) - **Stage-2 VSpaces** (`Ext/Stage2`): object 13 and its cap; an ASID pool @@ -431,11 +635,12 @@ maintenance of FL4's roots, break-before-make, SMP remote stalls, the Known differences from FL4 left for P3: -1. **A frame's `is_ept`** is not a field of l4v's frame cap; the model - reads the kind of the root its ASID names (`extKindOfAsid`). They - differ only for a cap whose ASID was deleted and reassigned to a root - of another kind (FL4 then unmaps nothing; the model unmaps a frame at - the same address in the new root only if it maps this very frame). +1. (Closed in generation 6.) **A frame's `is_ept`** was not modelled (the + model read the kind of the root its ASID names, `extKindOfAsid`, which + differed for a cap whose ASID was deleted and reassigned to a root of + another kind, and for a high-window alias outside `HIGH_L0_INDEX`). It + is now the cap `stage2FrameCap` with the frame hooks, and the + high-window guard `extVaOk` (above, "The frame hooks"). 2. **`SLOT_EPT`** (FL4's sixth TCB slot) is null on AArch64: only the x86-only `TCBSetEPTRoot` writes it; a guest's Stage-2 root is its VSpace, slot 1. @@ -474,8 +679,15 @@ Resolved in generation 5 (Architect's rulings of generation 4): - **`Ext/Timer`**: an armed TCB timer (`Fl4TcbTimer`: deadline, badge) and FL4's timer step `fl4Awaken` (fired timers signal through l4v's - `send_signal`), as on x86; `Ext/Entry`'s `fl4CallKernel` runs it before - `schedule`. + `send_signal`), as on x86; `Ext/SchedTail`'s `fl4Schedule` runs it first. +- **`Ext/IrqSignal`**: FL4 always masks a signalled IRQ and unmasks it on + `IRQAckIRQ`, GICv3 included, where l4v's `arch_mask_irq_signal` does nothing + and `arch_invoke_irq_handler` deactivates. `fl4HandleEvent_other` takes + `config_ARM_GIC_V3 = false` for the interrupt event. +- **`Ext/Entry` exits** (`fl4CallKernel`, `fl4CallKernelRunExit`, + `fl4KernelEntrySyscall`): they schedule with `fl4Schedule limit timer` + instead of the ported `schedule`, whose `arch_switch_to_thread` runs + `vcpu_switch`; FL4's `sched::schedule` never calls `vcpu_switch`. - **`Ext/Tick`**: one clock-oracle reading as an environment step (`fl4ClockTick`; a declined fl4-sync local entry), as on x86. - **`Ext/TcbSize`**: 4 KiB TCBs (`fl4TcbBits = 12`; l4v AARCH64 `tcb_bits = @@ -490,4 +702,94 @@ Resolved in generation 5 (Architect's rulings of generation 4): `cap_delete`, `cap_revoke`, `invoke_cnode`, `install_tcb_cap`, `install_tcb_frame_cap` and `invoke_tcb`'s thread-control cases (verbatim, with `fl4FinaliseCap` for `finaliseCap`); the hooks of - `fl4FinaliseCap` are AArch64's. + `fl4FinaliseCap` are AArch64's. For a final `.threadCap t`, + `fl4FinaliseCap` runs `fl4CancelRun t` followed by `fl4VcpuEndRun v` + when `t` is bound to a VCPU `v`, matching `vcpu::detach` + (`crates/fl4-core/src/arch/aarch64/vcpu.rs:324-330`; a no-op when + `runCaller = none`), before `finaliseCap`. + +### AArch64 TCB invocation refinements + +- `Ext/Tcb` and `Ext/Invocation` accept mapped native and Stage-2 + VSpace roots in the ordinary TCB VTable slot for Configure and SetSpace. + The decode keeps the ported error order, including Configure's buffer + check first and SetSpace's handler check after the root checks. The + conditional equations use the original decoders on the native path. + Compare `fl4-core/src/arch/aarch64/vspace.rs:315–317` and the pinned + [AArch64 native root check](https://github.com/seL4/seL4/blob/c98d70adf15e8be7d3b99085c28d057069ba7155/src/arch/arm/64/kernel/vspace.c#L855-L868). +- `Ext/Finalise` uses full Ext deletion for TCB slot replacement, then + reads both the source and invoking caps. The frame identity check uses + base, size and device bit across both mapping kinds. It retains the + capped object's existing derivation and CDT insertion semantics. +- Suspension through Suspend, CopyRegisters and ReadRegisters cancels + the FL4 run link first. Copy and Write use `fl4SanitiseRegister`. + The native SPSR case agrees with the port. The guest case follows + FL4's mask and illegal-mode fallback, with a conditional equality on + legal inputs whose bits are unchanged. Resume re-reads the SC after + IPC cancellation and Restart. In the local view, SetSchedParams bind + and Resume use the shared `fl4RefillSufficient` from `Ext/SchedControl`. + Both use MCS-18's `fl4SchedContextResume` from `Ext/SchedBudget`, + including its bound-thread assertion and ready-queue check before + postponing an active runnable thread. +- ReadFPU and WriteFPU accept native and Stage-2 frames with the same + rights, non-device, alignment and bounds checks. The saved image is + 520 bytes. WriteFPU applies the FPSR and FPCR masks and forgets the + local owner without loading the new image. The platform relation + carries saved words and local owner state. Remote owner effects use + the multicore boundary. +- SetFlags computes the raw word with FL4's two-bit set mask, projects + the ported flag set and releases the FPU if disabled. ReadRegisters + and SetFlags complete the concrete-style reply at perform time, + writing it for Call and making the caller Running for Call and Send. + PAC keys, flags and epoch occupy FL4-only TCB words, with the concrete + frame and epoch observation at the platform boundary. +- SetTimer projects the local ordered timer queue's remove/reinsert and + earliest decision into the port's reprogram flag. The timer tree + remains a related Store footprint. Deadline zero disarms. Nonzero + deadlines are raw ticks, and a new entry follows all equal deadlines + already on the current core. The existing MCS timer/return boundary + owns firing and hardware reprogramming. + +### AArch64 VCPU transcription fixes & refinement deltas (S4) + +- **Lean 4 `do`-block early `return ()` transcription fix (`AARCH64/VCPU.lean`)**: + Replaced the five non-tail `return ()` branches inside `do` blocks with + `pure ()`: `disableVirtTimer` (`:177`), `vcpuDisable` (`:214`), + `vcpuInvalidateActive` (`:246`), and `associateVcpuTcb` (`:373`, `:377`). + In Lean 4 `do` notation, `return ()` exits the enclosing `do` block early + (skipping subsequent statements), whereas Isabelle's `return ()` in the + pinned seL4/l4v spec (`c98d70ad`) is a no-op. This is a transcription fix + toward seL4 `c98d70ad`, not an `Ext` delta. + **Permanent rule:** `Fl4Spec*` code must use `pure ()`, never `return ()`, + for no-op branches inside `do`. + +- **`VGIC_VMCR` (reg 36) 64-bit view (`Ext/ARMVCPURun`)**: `fl4VcpuRegGet` + reads register `36` (`vrVgicVmcr`) from `v.vcpuExt.regs ⟨36, _⟩ : Word` + and `fl4VcpuRegSet` writes both `v.vcpuExt.regs ⟨36, _⟩` and + `v.vcpuVgic.vgicVmcr := ucast 32 w`. *Conservativity* + (`P3/VcpuSpecA.fl4VcpuRegGet_vmcr_u32`): whenever the upper 32 bits of + register 36 are 0 (including at `defaultVcpu`, `fl4VcpuInit defaultVcpu`, + and every architectural 32-bit `ICH_VMCR_EL2` / `GICH_VMCR` value), the + view agrees with `ucast 64 v.vcpuVgic.vgicVmcr`; `syncVgicStatus` only + reads bits 0 and 1. *Minor kernel note*: FL4 stores and returns all 64 + bits of `VGIC_VMCR` in `Store` without masking the upper 32 architectural + RES0 bits (`crates/fl4-core/src/arch/aarch64/vcpu.rs`). +- **`fl4AssociateVcpuTcb` / `fl4DissociateVcpuTcb` (`Ext/ARMVCPURun`)**: + `fl4PerformVcpuInv (.setTcb vc t)` uses `fl4AssociateVcpuTcb` instead of + the ported `associateVcpuTcb`, omitting the eager `vcpuSwitch (some vc)` + when `t = curThread` (FL4 switches VCPU sysregs lazily at kernel exit and + tracks `vspace::set_vm_root` at the `Z.hw` / `ScArchSwA` boundary) and + resetting `vcpuExt.ownerCpu := 0` on dissociation. *Conservativity* + (`P3/VcpuSpecA.fl4AssociateVcpuTcb_ne_cur`): when `t ≠ σ.curThread`, + every ported state field (`kheap` modulo `ownerCpu`, `archState`, + `machineState`, scheduler/CSpace state) agrees with `associateVcpuTcb`. +- **Deterministic `fl4CompleteRun` (`Ext/ARMVCPURun`)**: `fl4CompleteRun` + omits the trailing `NonDet.when (ct = cur) <| setVmRoot ct`, matching + `Ext/SchedTail.fl4SwitchToThread`; the concrete refinements of + `vcpu::complete_run` and `vcpu::decode_set_tcb` still verify the real + `vspace::set_vm_root` call through `Z.hw` / `ScArchSwA` / `HwTieA`. + *Side lemma* (`P3/VcpuSpecA.setVmRoot_preserves_state`): every outcome + `(_, τ) ∈ (setVmRoot ct σ).1` leaves every non-`machineState` field of + `State` unchanged except the VMID cache (`armAsidMap`, `armVmidTable`, + `armNextVmid`) inside `archState`, and leaves + `machineState.underlyingMemory` unchanged.